blob: 033c61d27148ffd31b31f4b40251a1ba2e7ff98a [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Joe Perchesafd465032012-03-12 07:03:32 +00002#define pr_fmt(fmt) "IPsec: " fmt
3
Herbert Xu38320c72008-01-28 19:35:05 -08004#include <crypto/aead.h>
5#include <crypto/authenc.h>
Herbert Xu6b7326c2006-07-30 15:41:01 +10006#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/module.h>
8#include <net/ip.h>
9#include <net/xfrm.h>
10#include <net/esp.h>
Adrian Bunk72998d82007-10-26 22:53:58 -070011#include <linux/scatterlist.h>
Herbert Xua02a6422005-10-10 21:11:08 -070012#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/pfkeyv2.h>
Herbert Xu38320c72008-01-28 19:35:05 -080014#include <linux/rtnetlink.h>
15#include <linux/slab.h>
Herbert Xub7c6538c2007-10-09 13:33:35 -070016#include <linux/spinlock.h>
Thomas Graf2017a722007-12-10 16:53:05 -080017#include <linux/in6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <net/icmp.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020019#include <net/protocol.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <net/udp.h>
21
Steffen Klassertcac26612017-01-17 10:22:57 +010022#include <linux/highmem.h>
23
Herbert Xu38320c72008-01-28 19:35:05 -080024struct esp_skb_cb {
25 struct xfrm_skb_cb xfrm;
26 void *tmp;
27};
28
Herbert Xu962fcef2016-06-18 13:03:36 +080029struct esp_output_extra {
30 __be32 seqhi;
31 u32 esphoff;
32};
33
Herbert Xu38320c72008-01-28 19:35:05 -080034#define ESP_SKB_CB(__skb) ((struct esp_skb_cb *)&((__skb)->cb[0]))
35
36/*
37 * Allocate an AEAD request structure with extra space for SG and IV.
38 *
39 * For alignment considerations the IV is placed at the front, followed
40 * by the request and finally the SG list.
41 *
42 * TODO: Use spare space in skb for this where possible.
43 */
Herbert Xu962fcef2016-06-18 13:03:36 +080044static void *esp_alloc_tmp(struct crypto_aead *aead, int nfrags, int extralen)
Herbert Xu38320c72008-01-28 19:35:05 -080045{
46 unsigned int len;
47
Herbert Xu962fcef2016-06-18 13:03:36 +080048 len = extralen;
Steffen Klassert0dc49e92011-03-08 00:07:14 +000049
50 len += crypto_aead_ivsize(aead);
51
Herbert Xu38320c72008-01-28 19:35:05 -080052 if (len) {
53 len += crypto_aead_alignmask(aead) &
54 ~(crypto_tfm_ctx_alignment() - 1);
55 len = ALIGN(len, crypto_tfm_ctx_alignment());
56 }
57
Herbert Xu7021b2e2015-05-27 16:03:46 +080058 len += sizeof(struct aead_request) + crypto_aead_reqsize(aead);
Herbert Xu38320c72008-01-28 19:35:05 -080059 len = ALIGN(len, __alignof__(struct scatterlist));
60
61 len += sizeof(struct scatterlist) * nfrags;
62
63 return kmalloc(len, GFP_ATOMIC);
64}
65
Herbert Xu962fcef2016-06-18 13:03:36 +080066static inline void *esp_tmp_extra(void *tmp)
Steffen Klassert0dc49e92011-03-08 00:07:14 +000067{
Herbert Xu962fcef2016-06-18 13:03:36 +080068 return PTR_ALIGN(tmp, __alignof__(struct esp_output_extra));
Steffen Klassert0dc49e92011-03-08 00:07:14 +000069}
Herbert Xu962fcef2016-06-18 13:03:36 +080070
71static inline u8 *esp_tmp_iv(struct crypto_aead *aead, void *tmp, int extralen)
Herbert Xu38320c72008-01-28 19:35:05 -080072{
73 return crypto_aead_ivsize(aead) ?
Herbert Xu962fcef2016-06-18 13:03:36 +080074 PTR_ALIGN((u8 *)tmp + extralen,
75 crypto_aead_alignmask(aead) + 1) : tmp + extralen;
Herbert Xu38320c72008-01-28 19:35:05 -080076}
77
Herbert Xu38320c72008-01-28 19:35:05 -080078static inline struct aead_request *esp_tmp_req(struct crypto_aead *aead, u8 *iv)
79{
80 struct aead_request *req;
81
82 req = (void *)PTR_ALIGN(iv + crypto_aead_ivsize(aead),
83 crypto_tfm_ctx_alignment());
84 aead_request_set_tfm(req, aead);
85 return req;
86}
87
88static inline struct scatterlist *esp_req_sg(struct crypto_aead *aead,
89 struct aead_request *req)
90{
91 return (void *)ALIGN((unsigned long)(req + 1) +
92 crypto_aead_reqsize(aead),
93 __alignof__(struct scatterlist));
94}
95
Steffen Klassertcac26612017-01-17 10:22:57 +010096static void esp_ssg_unref(struct xfrm_state *x, void *tmp)
97{
98 struct esp_output_extra *extra = esp_tmp_extra(tmp);
99 struct crypto_aead *aead = x->data;
100 int extralen = 0;
101 u8 *iv;
102 struct aead_request *req;
103 struct scatterlist *sg;
104
105 if (x->props.flags & XFRM_STATE_ESN)
106 extralen += sizeof(*extra);
107
108 extra = esp_tmp_extra(tmp);
109 iv = esp_tmp_iv(aead, tmp, extralen);
110 req = esp_tmp_req(aead, iv);
111
112 /* Unref skb_frag_pages in the src scatterlist if necessary.
113 * Skip the first sg which comes from skb->data.
114 */
115 if (req->src != req->dst)
116 for (sg = sg_next(req->src); sg; sg = sg_next(sg))
117 put_page(sg_page(sg));
118}
119
Herbert Xu38320c72008-01-28 19:35:05 -0800120static void esp_output_done(struct crypto_async_request *base, int err)
121{
122 struct sk_buff *skb = base->data;
Steffen Klassertf53c7232017-12-20 10:41:36 +0100123 struct xfrm_offload *xo = xfrm_offload(skb);
Steffen Klassertcac26612017-01-17 10:22:57 +0100124 void *tmp;
Steffen Klassertf53c7232017-12-20 10:41:36 +0100125 struct xfrm_state *x;
126
Florian Westphal2294be0f2018-12-18 17:15:20 +0100127 if (xo && (xo->flags & XFRM_DEV_RESUME)) {
128 struct sec_path *sp = skb_sec_path(skb);
129
130 x = sp->xvec[sp->len - 1];
131 } else {
Steffen Klassertf53c7232017-12-20 10:41:36 +0100132 x = skb_dst(skb)->xfrm;
Florian Westphal2294be0f2018-12-18 17:15:20 +0100133 }
Herbert Xu38320c72008-01-28 19:35:05 -0800134
Steffen Klassertcac26612017-01-17 10:22:57 +0100135 tmp = ESP_SKB_CB(skb)->tmp;
136 esp_ssg_unref(x, tmp);
137 kfree(tmp);
Steffen Klassertf53c7232017-12-20 10:41:36 +0100138
139 if (xo && (xo->flags & XFRM_DEV_RESUME)) {
140 if (err) {
141 XFRM_INC_STATS(xs_net(x), LINUX_MIB_XFRMOUTSTATEPROTOERROR);
142 kfree_skb(skb);
143 return;
144 }
145
146 skb_push(skb, skb->data - skb_mac_header(skb));
147 secpath_reset(skb);
148 xfrm_dev_resume(skb);
149 } else {
150 xfrm_output_resume(skb, err);
151 }
Herbert Xu38320c72008-01-28 19:35:05 -0800152}
153
Herbert Xu7021b2e2015-05-27 16:03:46 +0800154/* Move ESP header back into place. */
155static void esp_restore_header(struct sk_buff *skb, unsigned int offset)
156{
157 struct ip_esp_hdr *esph = (void *)(skb->data + offset);
158 void *tmp = ESP_SKB_CB(skb)->tmp;
Herbert Xu962fcef2016-06-18 13:03:36 +0800159 __be32 *seqhi = esp_tmp_extra(tmp);
Herbert Xu7021b2e2015-05-27 16:03:46 +0800160
161 esph->seq_no = esph->spi;
162 esph->spi = *seqhi;
163}
164
165static void esp_output_restore_header(struct sk_buff *skb)
166{
Herbert Xu962fcef2016-06-18 13:03:36 +0800167 void *tmp = ESP_SKB_CB(skb)->tmp;
168 struct esp_output_extra *extra = esp_tmp_extra(tmp);
169
170 esp_restore_header(skb, skb_transport_offset(skb) + extra->esphoff -
171 sizeof(__be32));
Herbert Xu7021b2e2015-05-27 16:03:46 +0800172}
173
Steffen Klassertcac26612017-01-17 10:22:57 +0100174static struct ip_esp_hdr *esp_output_set_extra(struct sk_buff *skb,
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200175 struct xfrm_state *x,
Steffen Klassertcac26612017-01-17 10:22:57 +0100176 struct ip_esp_hdr *esph,
177 struct esp_output_extra *extra)
178{
Steffen Klassertcac26612017-01-17 10:22:57 +0100179 /* For ESN we move the header forward by 4 bytes to
180 * accomodate the high bits. We will move it back after
181 * encryption.
182 */
183 if ((x->props.flags & XFRM_STATE_ESN)) {
Steffen Klassert7862b402017-04-14 10:06:50 +0200184 __u32 seqhi;
185 struct xfrm_offload *xo = xfrm_offload(skb);
186
187 if (xo)
188 seqhi = xo->seq.hi;
189 else
190 seqhi = XFRM_SKB_CB(skb)->seq.output.hi;
191
Steffen Klassertcac26612017-01-17 10:22:57 +0100192 extra->esphoff = (unsigned char *)esph -
193 skb_transport_header(skb);
194 esph = (struct ip_esp_hdr *)((unsigned char *)esph - 4);
195 extra->seqhi = esph->spi;
Steffen Klassert7862b402017-04-14 10:06:50 +0200196 esph->seq_no = htonl(seqhi);
Steffen Klassertcac26612017-01-17 10:22:57 +0100197 }
198
199 esph->spi = x->id.spi;
200
201 return esph;
202}
203
Herbert Xu7021b2e2015-05-27 16:03:46 +0800204static void esp_output_done_esn(struct crypto_async_request *base, int err)
205{
206 struct sk_buff *skb = base->data;
207
208 esp_output_restore_header(skb);
209 esp_output_done(base, err);
210}
211
Steffen Klasserteb758c82017-01-17 10:23:08 +0100212static void esp_output_fill_trailer(u8 *tail, int tfclen, int plen, __u8 proto)
213{
214 /* Fill padding... */
215 if (tfclen) {
216 memset(tail, 0, tfclen);
217 tail += tfclen;
218 }
219 do {
220 int i;
221 for (i = 0; i < plen - 2; i++)
222 tail[i] = i + 1;
223 } while (0);
224 tail[plen - 2] = plen - 2;
225 tail[plen - 1] = proto;
226}
227
Sabrina Dubrocaeecd227a2019-11-25 14:49:01 +0100228static struct ip_esp_hdr *esp_output_udp_encap(struct sk_buff *skb,
229 int encap_type,
230 struct esp_info *esp,
231 __be16 sport,
232 __be16 dport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200234 struct udphdr *uh;
235 __be32 *udpdata32;
Sabrina Dubroca8dfb4eb2019-03-25 14:30:00 +0100236 unsigned int len;
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200237
Sabrina Dubrocaeecd227a2019-11-25 14:49:01 +0100238 len = skb->len + esp->tailen - skb_transport_offset(skb);
239 if (len + sizeof(struct iphdr) >= IP_MAX_MTU)
240 return ERR_PTR(-EMSGSIZE);
241
242 uh = (struct udphdr *)esp->esph;
243 uh->source = sport;
244 uh->dest = dport;
245 uh->len = htons(len);
246 uh->check = 0;
247
248 *skb_mac_header(skb) = IPPROTO_UDP;
249
250 if (encap_type == UDP_ENCAP_ESPINUDP_NON_IKE) {
251 udpdata32 = (__be32 *)(uh + 1);
252 udpdata32[0] = udpdata32[1] = 0;
253 return (struct ip_esp_hdr *)(udpdata32 + 2);
254 }
255
256 return (struct ip_esp_hdr *)(uh + 1);
257}
258
259static int esp_output_encap(struct xfrm_state *x, struct sk_buff *skb,
260 struct esp_info *esp)
261{
262 struct xfrm_encap_tmpl *encap = x->encap;
263 struct ip_esp_hdr *esph;
264 __be16 sport, dport;
265 int encap_type;
266
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200267 spin_lock_bh(&x->lock);
268 sport = encap->encap_sport;
269 dport = encap->encap_dport;
270 encap_type = encap->encap_type;
271 spin_unlock_bh(&x->lock);
272
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200273 switch (encap_type) {
274 default:
275 case UDP_ENCAP_ESPINUDP:
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200276 case UDP_ENCAP_ESPINUDP_NON_IKE:
Sabrina Dubrocaeecd227a2019-11-25 14:49:01 +0100277 esph = esp_output_udp_encap(skb, encap_type, esp, sport, dport);
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200278 break;
279 }
280
Sabrina Dubrocaeecd227a2019-11-25 14:49:01 +0100281 if (IS_ERR(esph))
282 return PTR_ERR(esph);
283
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200284 esp->esph = esph;
Sabrina Dubroca8dfb4eb2019-03-25 14:30:00 +0100285
286 return 0;
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200287}
288
289int esp_output_head(struct xfrm_state *x, struct sk_buff *skb, struct esp_info *esp)
290{
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700291 u8 *tail;
Steffen Klassertcac26612017-01-17 10:22:57 +0100292 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 int nfrags;
Steffen Klassert0e78a872017-05-03 08:44:27 +0200294 int esph_offset;
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200295 struct page *page;
296 struct sk_buff *trailer;
297 int tailen = esp->tailen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299 /* this is non-NULL only with UDP Encapsulation */
Sabrina Dubroca8dfb4eb2019-03-25 14:30:00 +0100300 if (x->encap) {
Sabrina Dubrocaeecd227a2019-11-25 14:49:01 +0100301 int err = esp_output_encap(x, skb, esp);
Sabrina Dubroca8dfb4eb2019-03-25 14:30:00 +0100302
303 if (err < 0)
304 return err;
305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Steffen Klassertcac26612017-01-17 10:22:57 +0100307 if (!skb_cloned(skb)) {
Steffen Klassert54ffd792017-08-25 07:34:35 +0200308 if (tailen <= skb_tailroom(skb)) {
Steffen Klassertcac26612017-01-17 10:22:57 +0100309 nfrags = 1;
310 trailer = skb;
311 tail = skb_tail_pointer(trailer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Steffen Klassertcac26612017-01-17 10:22:57 +0100313 goto skip_cow;
314 } else if ((skb_shinfo(skb)->nr_frags < MAX_SKB_FRAGS)
315 && !skb_has_frag_list(skb)) {
316 int allocsize;
317 struct sock *sk = skb->sk;
318 struct page_frag *pfrag = &x->xfrag;
Herbert Xu7021b2e2015-05-27 16:03:46 +0800319
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200320 esp->inplace = false;
321
Steffen Klassertcac26612017-01-17 10:22:57 +0100322 allocsize = ALIGN(tailen, L1_CACHE_BYTES);
323
324 spin_lock_bh(&x->lock);
325
326 if (unlikely(!skb_page_frag_refill(allocsize, pfrag, GFP_ATOMIC))) {
327 spin_unlock_bh(&x->lock);
328 goto cow;
329 }
330
331 page = pfrag->page;
332 get_page(page);
333
334 vaddr = kmap_atomic(page);
335
336 tail = vaddr + pfrag->offset;
337
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200338 esp_output_fill_trailer(tail, esp->tfclen, esp->plen, esp->proto);
Steffen Klassertcac26612017-01-17 10:22:57 +0100339
340 kunmap_atomic(vaddr);
341
342 nfrags = skb_shinfo(skb)->nr_frags;
343
344 __skb_fill_page_desc(skb, nfrags, page, pfrag->offset,
345 tailen);
346 skb_shinfo(skb)->nr_frags = ++nfrags;
347
348 pfrag->offset = pfrag->offset + allocsize;
Steffen Klassert36ff0dd2017-08-25 07:16:07 +0200349
350 spin_unlock_bh(&x->lock);
351
Steffen Klassertcac26612017-01-17 10:22:57 +0100352 nfrags++;
353
354 skb->len += tailen;
355 skb->data_len += tailen;
356 skb->truesize += tailen;
Martin Willi09db5122019-01-28 09:35:35 +0100357 if (sk && sk_fullsock(sk))
Reshetova, Elena14afee42017-06-30 13:08:00 +0300358 refcount_add(tailen, &sk->sk_wmem_alloc);
Steffen Klassertcac26612017-01-17 10:22:57 +0100359
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200360 goto out;
Steffen Klassertcac26612017-01-17 10:22:57 +0100361 }
Herbert Xu7021b2e2015-05-27 16:03:46 +0800362 }
363
Steffen Klassertcac26612017-01-17 10:22:57 +0100364cow:
Steffen Klassert0e78a872017-05-03 08:44:27 +0200365 esph_offset = (unsigned char *)esp->esph - skb_transport_header(skb);
366
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200367 nfrags = skb_cow_data(skb, tailen, &trailer);
368 if (nfrags < 0)
369 goto out;
Steffen Klassertcac26612017-01-17 10:22:57 +0100370 tail = skb_tail_pointer(trailer);
Steffen Klassert0e78a872017-05-03 08:44:27 +0200371 esp->esph = (struct ip_esp_hdr *)(skb_transport_header(skb) + esph_offset);
Steffen Klassertcac26612017-01-17 10:22:57 +0100372
373skip_cow:
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200374 esp_output_fill_trailer(tail, esp->tfclen, esp->plen, esp->proto);
375 pskb_put(skb, trailer, tailen);
Steffen Klassertcac26612017-01-17 10:22:57 +0100376
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200377out:
378 return nfrags;
379}
380EXPORT_SYMBOL_GPL(esp_output_head);
Herbert Xu7021b2e2015-05-27 16:03:46 +0800381
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200382int esp_output_tail(struct xfrm_state *x, struct sk_buff *skb, struct esp_info *esp)
383{
384 u8 *iv;
385 int alen;
386 void *tmp;
387 int ivlen;
388 int assoclen;
389 int extralen;
390 struct page *page;
391 struct ip_esp_hdr *esph;
392 struct crypto_aead *aead;
393 struct aead_request *req;
394 struct scatterlist *sg, *dsg;
395 struct esp_output_extra *extra;
396 int err = -ENOMEM;
397
398 assoclen = sizeof(struct ip_esp_hdr);
399 extralen = 0;
400
401 if (x->props.flags & XFRM_STATE_ESN) {
402 extralen += sizeof(*extra);
403 assoclen += sizeof(__be32);
404 }
405
406 aead = x->data;
407 alen = crypto_aead_authsize(aead);
408 ivlen = crypto_aead_ivsize(aead);
409
410 tmp = esp_alloc_tmp(aead, esp->nfrags + 2, extralen);
Steffen Klasserte892d2d2017-04-24 07:33:56 +0200411 if (!tmp)
Steffen Klassertcac26612017-01-17 10:22:57 +0100412 goto error;
Steffen Klassertcac26612017-01-17 10:22:57 +0100413
414 extra = esp_tmp_extra(tmp);
415 iv = esp_tmp_iv(aead, tmp, extralen);
416 req = esp_tmp_req(aead, iv);
417 sg = esp_req_sg(aead, req);
Steffen Klassertcac26612017-01-17 10:22:57 +0100418
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200419 if (esp->inplace)
420 dsg = sg;
421 else
422 dsg = &sg[esp->nfrags];
Steffen Klassertcac26612017-01-17 10:22:57 +0100423
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200424 esph = esp_output_set_extra(skb, x, esp->esph, extra);
425 esp->esph = esph;
426
427 sg_init_table(sg, esp->nfrags);
Jason A. Donenfeld3f297702017-06-04 04:16:23 +0200428 err = skb_to_sgvec(skb, sg,
429 (unsigned char *)esph - skb->data,
430 assoclen + ivlen + esp->clen + alen);
431 if (unlikely(err < 0))
Steffen Klasserte6194922017-07-13 09:13:30 +0200432 goto error_free;
Steffen Klassert0dc49e92011-03-08 00:07:14 +0000433
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200434 if (!esp->inplace) {
435 int allocsize;
436 struct page_frag *pfrag = &x->xfrag;
437
438 allocsize = ALIGN(skb->data_len, L1_CACHE_BYTES);
439
440 spin_lock_bh(&x->lock);
441 if (unlikely(!skb_page_frag_refill(allocsize, pfrag, GFP_ATOMIC))) {
442 spin_unlock_bh(&x->lock);
Steffen Klasserte6194922017-07-13 09:13:30 +0200443 goto error_free;
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200444 }
445
446 skb_shinfo(skb)->nr_frags = 1;
447
448 page = pfrag->page;
449 get_page(page);
450 /* replace page frags in skb with new page */
451 __skb_fill_page_desc(skb, 0, page, pfrag->offset, skb->data_len);
452 pfrag->offset = pfrag->offset + allocsize;
453 spin_unlock_bh(&x->lock);
454
455 sg_init_table(dsg, skb_shinfo(skb)->nr_frags + 1);
Jason A. Donenfeld3f297702017-06-04 04:16:23 +0200456 err = skb_to_sgvec(skb, dsg,
457 (unsigned char *)esph - skb->data,
458 assoclen + ivlen + esp->clen + alen);
459 if (unlikely(err < 0))
Steffen Klasserte6194922017-07-13 09:13:30 +0200460 goto error_free;
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200461 }
462
Steffen Klassertcac26612017-01-17 10:22:57 +0100463 if ((x->props.flags & XFRM_STATE_ESN))
464 aead_request_set_callback(req, 0, esp_output_done_esn, skb);
465 else
466 aead_request_set_callback(req, 0, esp_output_done, skb);
467
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200468 aead_request_set_crypt(req, sg, dsg, ivlen + esp->clen, iv);
Herbert Xu7021b2e2015-05-27 16:03:46 +0800469 aead_request_set_ad(req, assoclen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Herbert Xu7021b2e2015-05-27 16:03:46 +0800471 memset(iv, 0, ivlen);
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200472 memcpy(iv + ivlen - min(ivlen, 8), (u8 *)&esp->seqno + 8 - min(ivlen, 8),
Herbert Xu7021b2e2015-05-27 16:03:46 +0800473 min(ivlen, 8));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Herbert Xu38320c72008-01-28 19:35:05 -0800475 ESP_SKB_CB(skb)->tmp = tmp;
Herbert Xu7021b2e2015-05-27 16:03:46 +0800476 err = crypto_aead_encrypt(req);
477
478 switch (err) {
479 case -EINPROGRESS:
Herbert Xu38320c72008-01-28 19:35:05 -0800480 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Gilad Ben-Yossef068c2e72017-10-18 08:00:35 +0100482 case -ENOSPC:
Herbert Xu38320c72008-01-28 19:35:05 -0800483 err = NET_XMIT_DROP;
Herbert Xu7021b2e2015-05-27 16:03:46 +0800484 break;
485
486 case 0:
487 if ((x->props.flags & XFRM_STATE_ESN))
488 esp_output_restore_header(skb);
489 }
Herbert Xu6b7326c2006-07-30 15:41:01 +1000490
Steffen Klassertcac26612017-01-17 10:22:57 +0100491 if (sg != dsg)
492 esp_ssg_unref(x, tmp);
Herbert Xub7c6538c2007-10-09 13:33:35 -0700493
Steffen Klasserte6194922017-07-13 09:13:30 +0200494error_free:
495 kfree(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496error:
497 return err;
498}
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200499EXPORT_SYMBOL_GPL(esp_output_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200501static int esp_output(struct xfrm_state *x, struct sk_buff *skb)
502{
503 int alen;
504 int blksize;
505 struct ip_esp_hdr *esph;
506 struct crypto_aead *aead;
507 struct esp_info esp;
508
509 esp.inplace = true;
510
511 esp.proto = *skb_mac_header(skb);
512 *skb_mac_header(skb) = IPPROTO_ESP;
513
514 /* skb is pure payload to encrypt */
515
516 aead = x->data;
517 alen = crypto_aead_authsize(aead);
518
519 esp.tfclen = 0;
520 if (x->tfcpad) {
521 struct xfrm_dst *dst = (struct xfrm_dst *)skb_dst(skb);
522 u32 padto;
523
Florian Westphalc7b37c72019-06-24 22:04:48 +0200524 padto = min(x->tfcpad, xfrm_state_mtu(x, dst->child_mtu_cached));
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200525 if (skb->len < padto)
526 esp.tfclen = padto - skb->len;
527 }
528 blksize = ALIGN(crypto_aead_blocksize(aead), 4);
529 esp.clen = ALIGN(skb->len + 2 + esp.tfclen, blksize);
530 esp.plen = esp.clen - skb->len - esp.tfclen;
531 esp.tailen = esp.tfclen + esp.plen + alen;
532
533 esp.esph = ip_esp_hdr(skb);
534
535 esp.nfrags = esp_output_head(x, skb, &esp);
536 if (esp.nfrags < 0)
537 return esp.nfrags;
538
539 esph = esp.esph;
540 esph->spi = x->id.spi;
541
542 esph->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output.low);
543 esp.seqno = cpu_to_be64(XFRM_SKB_CB(skb)->seq.output.low +
544 ((u64)XFRM_SKB_CB(skb)->seq.output.hi << 32));
545
546 skb_push(skb, -skb_network_offset(skb));
547
548 return esp_output_tail(x, skb, &esp);
549}
550
Yossi Kuperman47ebcc02017-08-30 11:30:39 +0300551static inline int esp_remove_trailer(struct sk_buff *skb)
552{
553 struct xfrm_state *x = xfrm_input_state(skb);
554 struct xfrm_offload *xo = xfrm_offload(skb);
555 struct crypto_aead *aead = x->data;
556 int alen, hlen, elen;
557 int padlen, trimlen;
558 __wsum csumdiff;
559 u8 nexthdr[2];
560 int ret;
561
562 alen = crypto_aead_authsize(aead);
563 hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead);
564 elen = skb->len - hlen;
565
566 if (xo && (xo->flags & XFRM_ESP_NO_TRAILER)) {
567 ret = xo->proto;
568 goto out;
569 }
570
571 if (skb_copy_bits(skb, skb->len - alen - 2, nexthdr, 2))
572 BUG();
573
574 ret = -EINVAL;
575 padlen = nexthdr[0];
576 if (padlen + 2 + alen >= elen) {
577 net_dbg_ratelimited("ipsec esp packet is garbage padlen=%d, elen=%d\n",
578 padlen + 2, elen - alen);
579 goto out;
580 }
581
582 trimlen = alen + padlen + 2;
583 if (skb->ip_summed == CHECKSUM_COMPLETE) {
584 csumdiff = skb_checksum(skb, skb->len - trimlen, trimlen, 0);
585 skb->csum = csum_block_sub(skb->csum, csumdiff,
586 skb->len - trimlen);
587 }
588 pskb_trim(skb, skb->len - trimlen);
589
590 ret = nexthdr[1];
591
592out:
593 return ret;
594}
595
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200596int esp_input_done2(struct sk_buff *skb, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000598 const struct iphdr *iph;
Herbert Xu38320c72008-01-28 19:35:05 -0800599 struct xfrm_state *x = xfrm_input_state(skb);
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200600 struct xfrm_offload *xo = xfrm_offload(skb);
Mathias Krause1c5ad132013-10-18 12:09:05 +0200601 struct crypto_aead *aead = x->data;
Herbert Xu38320c72008-01-28 19:35:05 -0800602 int hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead);
Herbert Xu31a4ab92006-05-27 23:06:13 -0700603 int ihl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200605 if (!xo || (xo && !(xo->flags & CRYPTO_DONE)))
606 kfree(ESP_SKB_CB(skb)->tmp);
Herbert Xu0ebea8e2007-11-13 21:45:58 -0800607
Herbert Xu6b7326c2006-07-30 15:41:01 +1000608 if (unlikely(err))
Herbert Xu668dc8a2007-12-16 15:55:02 -0800609 goto out;
Herbert Xu4bf05ec2006-02-27 13:00:01 -0800610
Yossi Kuperman47ebcc02017-08-30 11:30:39 +0300611 err = esp_remove_trailer(skb);
612 if (unlikely(err < 0))
Herbert Xu4bf05ec2006-02-27 13:00:01 -0800613 goto out;
614
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700615 iph = ip_hdr(skb);
Herbert Xu31a4ab92006-05-27 23:06:13 -0700616 ihl = iph->ihl * 4;
617
Herbert Xu752c1f42006-02-27 13:00:40 -0800618 if (x->encap) {
619 struct xfrm_encap_tmpl *encap = x->encap;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700620 struct udphdr *uh = (void *)(skb_network_header(skb) + ihl);
Sabrina Dubroca25f68022019-11-25 14:49:00 +0100621 __be16 source;
622
623 switch (x->encap->encap_type) {
624 case UDP_ENCAP_ESPINUDP:
625 case UDP_ENCAP_ESPINUDP_NON_IKE:
626 source = uh->source;
627 break;
628 default:
629 WARN_ON_ONCE(1);
630 err = -EINVAL;
631 goto out;
632 }
Herbert Xu752c1f42006-02-27 13:00:40 -0800633
634 /*
635 * 1) if the NAT-T peer's IP or port changed then
636 * advertize the change to the keying daemon.
637 * This is an inbound SA, so just compare
638 * SRC ports.
639 */
640 if (iph->saddr != x->props.saddr.a4 ||
Sabrina Dubroca25f68022019-11-25 14:49:00 +0100641 source != encap->encap_sport) {
Herbert Xu752c1f42006-02-27 13:00:40 -0800642 xfrm_address_t ipaddr;
643
644 ipaddr.a4 = iph->saddr;
Sabrina Dubroca25f68022019-11-25 14:49:00 +0100645 km_new_mapping(x, &ipaddr, source);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900646
Herbert Xu752c1f42006-02-27 13:00:40 -0800647 /* XXX: perhaps add an extra
648 * policy check here, to see
649 * if we should allow or
650 * reject a packet from a
651 * different source
652 * address/port.
653 */
Herbert Xu4bf05ec2006-02-27 13:00:01 -0800654 }
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900655
Herbert Xu752c1f42006-02-27 13:00:40 -0800656 /*
657 * 2) ignore UDP/TCP checksums in case
658 * of NAT-T in Transport Mode, or
659 * perform other post-processing fixes
660 * as per draft-ietf-ipsec-udp-encaps-06,
661 * section 3.1.2
662 */
Herbert Xu8bd17072007-10-10 15:41:41 -0700663 if (x->props.mode == XFRM_MODE_TRANSPORT)
Herbert Xu752c1f42006-02-27 13:00:40 -0800664 skb->ip_summed = CHECKSUM_UNNECESSARY;
Herbert Xu4bf05ec2006-02-27 13:00:01 -0800665 }
666
Ilan Tayariec9567a2017-08-01 12:49:04 +0300667 skb_pull_rcsum(skb, hlen);
Li RongQing7143dfa2012-12-28 16:07:16 +0800668 if (x->props.mode == XFRM_MODE_TUNNEL)
669 skb_reset_transport_header(skb);
670 else
671 skb_set_transport_header(skb, -ihl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Herbert Xu38320c72008-01-28 19:35:05 -0800673 /* RFC4303: Drop dummy packets without any error */
674 if (err == IPPROTO_NONE)
675 err = -EINVAL;
676
677out:
678 return err;
679}
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200680EXPORT_SYMBOL_GPL(esp_input_done2);
Herbert Xu38320c72008-01-28 19:35:05 -0800681
682static void esp_input_done(struct crypto_async_request *base, int err)
683{
684 struct sk_buff *skb = base->data;
685
686 xfrm_input_resume(skb, esp_input_done2(skb, err));
687}
688
Herbert Xu7021b2e2015-05-27 16:03:46 +0800689static void esp_input_restore_header(struct sk_buff *skb)
690{
691 esp_restore_header(skb, 0);
692 __skb_pull(skb, 4);
693}
694
Steffen Klassertcac26612017-01-17 10:22:57 +0100695static void esp_input_set_header(struct sk_buff *skb, __be32 *seqhi)
696{
697 struct xfrm_state *x = xfrm_input_state(skb);
Colin Ian King60aa8042018-01-30 14:53:48 +0000698 struct ip_esp_hdr *esph;
Steffen Klassertcac26612017-01-17 10:22:57 +0100699
700 /* For ESN we move the header forward by 4 bytes to
701 * accomodate the high bits. We will move it back after
702 * decryption.
703 */
704 if ((x->props.flags & XFRM_STATE_ESN)) {
Johannes Bergd58ff352017-06-16 14:29:23 +0200705 esph = skb_push(skb, 4);
Steffen Klassertcac26612017-01-17 10:22:57 +0100706 *seqhi = esph->spi;
707 esph->spi = esph->seq_no;
708 esph->seq_no = XFRM_SKB_CB(skb)->seq.input.hi;
709 }
710}
711
Herbert Xu7021b2e2015-05-27 16:03:46 +0800712static void esp_input_done_esn(struct crypto_async_request *base, int err)
713{
714 struct sk_buff *skb = base->data;
715
716 esp_input_restore_header(skb);
717 esp_input_done(base, err);
718}
719
Herbert Xu38320c72008-01-28 19:35:05 -0800720/*
721 * Note: detecting truncated vs. non-truncated authentication data is very
722 * expensive, so we only support truncated data, which is the recommended
723 * and common case.
724 */
725static int esp_input(struct xfrm_state *x, struct sk_buff *skb)
726{
Mathias Krause1c5ad132013-10-18 12:09:05 +0200727 struct crypto_aead *aead = x->data;
Herbert Xu38320c72008-01-28 19:35:05 -0800728 struct aead_request *req;
729 struct sk_buff *trailer;
Herbert Xu7021b2e2015-05-27 16:03:46 +0800730 int ivlen = crypto_aead_ivsize(aead);
Haishuang Yan0c05f982018-08-17 15:51:00 +0800731 int elen = skb->len - sizeof(struct ip_esp_hdr) - ivlen;
Herbert Xu38320c72008-01-28 19:35:05 -0800732 int nfrags;
Steffen Klassert0dc49e92011-03-08 00:07:14 +0000733 int assoclen;
Steffen Klassert0dc49e92011-03-08 00:07:14 +0000734 int seqhilen;
735 __be32 *seqhi;
Herbert Xu38320c72008-01-28 19:35:05 -0800736 void *tmp;
737 u8 *iv;
738 struct scatterlist *sg;
Herbert Xu38320c72008-01-28 19:35:05 -0800739 int err = -EINVAL;
740
Haishuang Yan0c05f982018-08-17 15:51:00 +0800741 if (!pskb_may_pull(skb, sizeof(struct ip_esp_hdr) + ivlen))
Herbert Xu38320c72008-01-28 19:35:05 -0800742 goto out;
743
744 if (elen <= 0)
745 goto out;
746
Haishuang Yan0c05f982018-08-17 15:51:00 +0800747 assoclen = sizeof(struct ip_esp_hdr);
Steffen Klassert0dc49e92011-03-08 00:07:14 +0000748 seqhilen = 0;
749
750 if (x->props.flags & XFRM_STATE_ESN) {
Steffen Klassert0dc49e92011-03-08 00:07:14 +0000751 seqhilen += sizeof(__be32);
752 assoclen += seqhilen;
753 }
754
Steffen Klassertcac26612017-01-17 10:22:57 +0100755 if (!skb_cloned(skb)) {
756 if (!skb_is_nonlinear(skb)) {
757 nfrags = 1;
758
759 goto skip_cow;
760 } else if (!skb_has_frag_list(skb)) {
761 nfrags = skb_shinfo(skb)->nr_frags;
762 nfrags++;
763
764 goto skip_cow;
765 }
766 }
767
768 err = skb_cow_data(skb, 0, &trailer);
769 if (err < 0)
770 goto out;
771
772 nfrags = err;
773
774skip_cow:
Herbert Xu38320c72008-01-28 19:35:05 -0800775 err = -ENOMEM;
Herbert Xu7021b2e2015-05-27 16:03:46 +0800776 tmp = esp_alloc_tmp(aead, nfrags, seqhilen);
Herbert Xu38320c72008-01-28 19:35:05 -0800777 if (!tmp)
778 goto out;
779
780 ESP_SKB_CB(skb)->tmp = tmp;
Herbert Xu962fcef2016-06-18 13:03:36 +0800781 seqhi = esp_tmp_extra(tmp);
Steffen Klassert0dc49e92011-03-08 00:07:14 +0000782 iv = esp_tmp_iv(aead, tmp, seqhilen);
Herbert Xu38320c72008-01-28 19:35:05 -0800783 req = esp_tmp_req(aead, iv);
Herbert Xu7021b2e2015-05-27 16:03:46 +0800784 sg = esp_req_sg(aead, req);
Herbert Xu38320c72008-01-28 19:35:05 -0800785
Steffen Klassertcac26612017-01-17 10:22:57 +0100786 esp_input_set_header(skb, seqhi);
Herbert Xu38320c72008-01-28 19:35:05 -0800787
788 sg_init_table(sg, nfrags);
Jason A. Donenfeld3f297702017-06-04 04:16:23 +0200789 err = skb_to_sgvec(skb, sg, 0, skb->len);
Steffen Klasserte6194922017-07-13 09:13:30 +0200790 if (unlikely(err < 0)) {
791 kfree(tmp);
Jason A. Donenfeld3f297702017-06-04 04:16:23 +0200792 goto out;
Steffen Klasserte6194922017-07-13 09:13:30 +0200793 }
Steffen Klassert0dc49e92011-03-08 00:07:14 +0000794
Steffen Klassertcac26612017-01-17 10:22:57 +0100795 skb->ip_summed = CHECKSUM_NONE;
796
797 if ((x->props.flags & XFRM_STATE_ESN))
798 aead_request_set_callback(req, 0, esp_input_done_esn, skb);
799 else
800 aead_request_set_callback(req, 0, esp_input_done, skb);
801
Herbert Xu7021b2e2015-05-27 16:03:46 +0800802 aead_request_set_crypt(req, sg, sg, elen + ivlen, iv);
803 aead_request_set_ad(req, assoclen);
Herbert Xu38320c72008-01-28 19:35:05 -0800804
805 err = crypto_aead_decrypt(req);
806 if (err == -EINPROGRESS)
807 goto out;
808
Herbert Xu7021b2e2015-05-27 16:03:46 +0800809 if ((x->props.flags & XFRM_STATE_ESN))
810 esp_input_restore_header(skb);
811
Herbert Xu38320c72008-01-28 19:35:05 -0800812 err = esp_input_done2(skb, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814out:
Herbert Xu668dc8a2007-12-16 15:55:02 -0800815 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816}
817
Steffen Klassert827789c2014-02-21 08:41:08 +0100818static int esp4_err(struct sk_buff *skb, u32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819{
Alexey Dobriyan4fb236b2008-11-25 17:59:27 -0800820 struct net *net = dev_net(skb->dev);
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000821 const struct iphdr *iph = (const struct iphdr *)skb->data;
Jianjun Kongd93191002008-11-03 00:23:42 -0800822 struct ip_esp_hdr *esph = (struct ip_esp_hdr *)(skb->data+(iph->ihl<<2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 struct xfrm_state *x;
824
David S. Miller55be7a92012-07-11 21:27:49 -0700825 switch (icmp_hdr(skb)->type) {
826 case ICMP_DEST_UNREACH:
827 if (icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
Steffen Klassert827789c2014-02-21 08:41:08 +0100828 return 0;
David S. Miller55be7a92012-07-11 21:27:49 -0700829 case ICMP_REDIRECT:
830 break;
831 default:
Steffen Klassert827789c2014-02-21 08:41:08 +0100832 return 0;
David S. Miller55be7a92012-07-11 21:27:49 -0700833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000835 x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr,
836 esph->spi, IPPROTO_ESP, AF_INET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 if (!x)
Steffen Klassert827789c2014-02-21 08:41:08 +0100838 return 0;
David S. Miller55be7a92012-07-11 21:27:49 -0700839
Timo Teräs387aa652013-05-27 20:46:31 +0000840 if (icmp_hdr(skb)->type == ICMP_DEST_UNREACH)
Maciej Żenczykowskid888f392018-09-25 20:56:26 -0700841 ipv4_update_pmtu(skb, net, info, 0, IPPROTO_ESP);
Timo Teräs387aa652013-05-27 20:46:31 +0000842 else
Maciej Żenczykowski1042caa2018-09-25 20:56:27 -0700843 ipv4_redirect(skb, net, 0, IPPROTO_ESP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 xfrm_state_put(x);
Steffen Klassert827789c2014-02-21 08:41:08 +0100845
846 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847}
848
849static void esp_destroy(struct xfrm_state *x)
850{
Mathias Krause1c5ad132013-10-18 12:09:05 +0200851 struct crypto_aead *aead = x->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Mathias Krause1c5ad132013-10-18 12:09:05 +0200853 if (!aead)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 return;
855
Mathias Krause1c5ad132013-10-18 12:09:05 +0200856 crypto_free_aead(aead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857}
858
Herbert Xu1a6509d2008-01-28 19:37:29 -0800859static int esp_init_aead(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
Herbert Xu7021b2e2015-05-27 16:03:46 +0800861 char aead_name[CRYPTO_MAX_ALG_NAME];
Herbert Xu1a6509d2008-01-28 19:37:29 -0800862 struct crypto_aead *aead;
863 int err;
864
Herbert Xu7021b2e2015-05-27 16:03:46 +0800865 err = -ENAMETOOLONG;
866 if (snprintf(aead_name, CRYPTO_MAX_ALG_NAME, "%s(%s)",
867 x->geniv, x->aead->alg_name) >= CRYPTO_MAX_ALG_NAME)
868 goto error;
869
Steffen Klassertf58869c2017-12-20 10:41:53 +0100870 aead = crypto_alloc_aead(aead_name, 0, 0);
Herbert Xu1a6509d2008-01-28 19:37:29 -0800871 err = PTR_ERR(aead);
872 if (IS_ERR(aead))
873 goto error;
874
Mathias Krause1c5ad132013-10-18 12:09:05 +0200875 x->data = aead;
Herbert Xu1a6509d2008-01-28 19:37:29 -0800876
877 err = crypto_aead_setkey(aead, x->aead->alg_key,
878 (x->aead->alg_key_len + 7) / 8);
879 if (err)
880 goto error;
881
882 err = crypto_aead_setauthsize(aead, x->aead->alg_icv_len / 8);
883 if (err)
884 goto error;
885
886error:
887 return err;
888}
889
890static int esp_init_authenc(struct xfrm_state *x)
891{
Herbert Xu38320c72008-01-28 19:35:05 -0800892 struct crypto_aead *aead;
893 struct crypto_authenc_key_param *param;
894 struct rtattr *rta;
895 char *key;
896 char *p;
897 char authenc_name[CRYPTO_MAX_ALG_NAME];
Herbert Xu38320c72008-01-28 19:35:05 -0800898 unsigned int keylen;
899 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Herbert Xu1a6509d2008-01-28 19:37:29 -0800901 err = -EINVAL;
Ian Morris51456b22015-04-03 09:17:26 +0100902 if (!x->ealg)
Herbert Xu1a6509d2008-01-28 19:37:29 -0800903 goto error;
Herbert Xu38320c72008-01-28 19:35:05 -0800904
Herbert Xu1a6509d2008-01-28 19:37:29 -0800905 err = -ENAMETOOLONG;
Steffen Klassert0dc49e92011-03-08 00:07:14 +0000906
907 if ((x->props.flags & XFRM_STATE_ESN)) {
908 if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME,
Herbert Xu7021b2e2015-05-27 16:03:46 +0800909 "%s%sauthencesn(%s,%s)%s",
910 x->geniv ?: "", x->geniv ? "(" : "",
Steffen Klassert0dc49e92011-03-08 00:07:14 +0000911 x->aalg ? x->aalg->alg_name : "digest_null",
Herbert Xu7021b2e2015-05-27 16:03:46 +0800912 x->ealg->alg_name,
913 x->geniv ? ")" : "") >= CRYPTO_MAX_ALG_NAME)
Steffen Klassert0dc49e92011-03-08 00:07:14 +0000914 goto error;
915 } else {
916 if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME,
Herbert Xu7021b2e2015-05-27 16:03:46 +0800917 "%s%sauthenc(%s,%s)%s",
918 x->geniv ?: "", x->geniv ? "(" : "",
Steffen Klassert0dc49e92011-03-08 00:07:14 +0000919 x->aalg ? x->aalg->alg_name : "digest_null",
Herbert Xu7021b2e2015-05-27 16:03:46 +0800920 x->ealg->alg_name,
921 x->geniv ? ")" : "") >= CRYPTO_MAX_ALG_NAME)
Steffen Klassert0dc49e92011-03-08 00:07:14 +0000922 goto error;
923 }
Herbert Xu38320c72008-01-28 19:35:05 -0800924
Steffen Klassertf58869c2017-12-20 10:41:53 +0100925 aead = crypto_alloc_aead(authenc_name, 0, 0);
Herbert Xu38320c72008-01-28 19:35:05 -0800926 err = PTR_ERR(aead);
927 if (IS_ERR(aead))
928 goto error;
929
Mathias Krause1c5ad132013-10-18 12:09:05 +0200930 x->data = aead;
Herbert Xu38320c72008-01-28 19:35:05 -0800931
932 keylen = (x->aalg ? (x->aalg->alg_key_len + 7) / 8 : 0) +
933 (x->ealg->alg_key_len + 7) / 8 + RTA_SPACE(sizeof(*param));
934 err = -ENOMEM;
935 key = kmalloc(keylen, GFP_KERNEL);
936 if (!key)
937 goto error;
938
939 p = key;
940 rta = (void *)p;
941 rta->rta_type = CRYPTO_AUTHENC_KEYA_PARAM;
942 rta->rta_len = RTA_LENGTH(sizeof(*param));
943 param = RTA_DATA(rta);
944 p += RTA_SPACE(sizeof(*param));
945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 if (x->aalg) {
947 struct xfrm_algo_desc *aalg_desc;
948
Herbert Xu38320c72008-01-28 19:35:05 -0800949 memcpy(p, x->aalg->alg_key, (x->aalg->alg_key_len + 7) / 8);
950 p += (x->aalg->alg_key_len + 7) / 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952 aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
953 BUG_ON(!aalg_desc);
954
Herbert Xu38320c72008-01-28 19:35:05 -0800955 err = -EINVAL;
Joe Perches45083492014-11-05 15:36:08 -0800956 if (aalg_desc->uinfo.auth.icv_fullbits / 8 !=
Herbert Xu38320c72008-01-28 19:35:05 -0800957 crypto_aead_authsize(aead)) {
Joe Perches45083492014-11-05 15:36:08 -0800958 pr_info("ESP: %s digestsize %u != %hu\n",
959 x->aalg->alg_name,
960 crypto_aead_authsize(aead),
961 aalg_desc->uinfo.auth.icv_fullbits / 8);
Herbert Xu38320c72008-01-28 19:35:05 -0800962 goto free_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 }
964
Herbert Xu38320c72008-01-28 19:35:05 -0800965 err = crypto_aead_setauthsize(
Martin Willi8f8a0882009-11-25 00:29:53 +0000966 aead, x->aalg->alg_trunc_len / 8);
Herbert Xu38320c72008-01-28 19:35:05 -0800967 if (err)
968 goto free_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 }
Herbert Xu4b7137f2007-10-08 17:13:44 -0700970
Herbert Xu38320c72008-01-28 19:35:05 -0800971 param->enckeylen = cpu_to_be32((x->ealg->alg_key_len + 7) / 8);
972 memcpy(p, x->ealg->alg_key, (x->ealg->alg_key_len + 7) / 8);
973
974 err = crypto_aead_setkey(aead, key, keylen);
975
976free_key:
977 kfree(key);
978
Herbert Xu1a6509d2008-01-28 19:37:29 -0800979error:
980 return err;
981}
982
983static int esp_init_state(struct xfrm_state *x)
984{
Herbert Xu1a6509d2008-01-28 19:37:29 -0800985 struct crypto_aead *aead;
986 u32 align;
987 int err;
988
Mathias Krause1c5ad132013-10-18 12:09:05 +0200989 x->data = NULL;
Herbert Xu1a6509d2008-01-28 19:37:29 -0800990
991 if (x->aead)
992 err = esp_init_aead(x);
993 else
994 err = esp_init_authenc(x);
995
Herbert Xu38320c72008-01-28 19:35:05 -0800996 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 goto error;
Herbert Xu38320c72008-01-28 19:35:05 -0800998
Mathias Krause1c5ad132013-10-18 12:09:05 +0200999 aead = x->data;
Herbert Xu1a6509d2008-01-28 19:37:29 -08001000
Herbert Xu38320c72008-01-28 19:35:05 -08001001 x->props.header_len = sizeof(struct ip_esp_hdr) +
1002 crypto_aead_ivsize(aead);
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07001003 if (x->props.mode == XFRM_MODE_TUNNEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 x->props.header_len += sizeof(struct iphdr);
Joakim Koskelaeb49e632008-08-06 02:39:30 -07001005 else if (x->props.mode == XFRM_MODE_BEET && x->sel.family != AF_INET6)
Patrick McHardyac758e32007-04-09 11:47:58 -07001006 x->props.header_len += IPV4_BEET_PHMAXLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 if (x->encap) {
1008 struct xfrm_encap_tmpl *encap = x->encap;
1009
1010 switch (encap->encap_type) {
1011 default:
Herbert Xubcfd09f2018-01-05 22:12:32 +11001012 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 goto error;
1014 case UDP_ENCAP_ESPINUDP:
1015 x->props.header_len += sizeof(struct udphdr);
1016 break;
1017 case UDP_ENCAP_ESPINUDP_NON_IKE:
1018 x->props.header_len += sizeof(struct udphdr) + 2 * sizeof(u32);
1019 break;
1020 }
1021 }
Herbert Xu38320c72008-01-28 19:35:05 -08001022
1023 align = ALIGN(crypto_aead_blocksize(aead), 4);
Mathias Krause1c5ad132013-10-18 12:09:05 +02001024 x->props.trailer_len = align + 1 + crypto_aead_authsize(aead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
1026error:
Herbert Xu38320c72008-01-28 19:35:05 -08001027 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028}
1029
Steffen Klassert827789c2014-02-21 08:41:08 +01001030static int esp4_rcv_cb(struct sk_buff *skb, int err)
1031{
1032 return 0;
1033}
1034
Eric Dumazet533cb5b2008-01-30 19:11:50 -08001035static const struct xfrm_type esp_type =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
1037 .description = "ESP4",
1038 .owner = THIS_MODULE,
1039 .proto = IPPROTO_ESP,
Herbert Xu436a0a42007-10-08 17:25:53 -07001040 .flags = XFRM_TYPE_REPLAY_PROT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 .init_state = esp_init_state,
1042 .destructor = esp_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 .input = esp_input,
Steffen Klassertfca11eb2017-04-14 10:06:33 +02001044 .output = esp_output,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045};
1046
Steffen Klassert827789c2014-02-21 08:41:08 +01001047static struct xfrm4_protocol esp4_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 .handler = xfrm4_rcv,
Steffen Klassert827789c2014-02-21 08:41:08 +01001049 .input_handler = xfrm_input,
1050 .cb_handler = esp4_rcv_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 .err_handler = esp4_err,
Steffen Klassert827789c2014-02-21 08:41:08 +01001052 .priority = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053};
1054
1055static int __init esp4_init(void)
1056{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 if (xfrm_register_type(&esp_type, AF_INET) < 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +00001058 pr_info("%s: can't add xfrm type\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 return -EAGAIN;
1060 }
Steffen Klassert827789c2014-02-21 08:41:08 +01001061 if (xfrm4_protocol_register(&esp4_protocol, IPPROTO_ESP) < 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +00001062 pr_info("%s: can't add protocol\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 xfrm_unregister_type(&esp_type, AF_INET);
1064 return -EAGAIN;
1065 }
1066 return 0;
1067}
1068
1069static void __exit esp4_fini(void)
1070{
Steffen Klassert827789c2014-02-21 08:41:08 +01001071 if (xfrm4_protocol_deregister(&esp4_protocol, IPPROTO_ESP) < 0)
Joe Perches058bd4d2012-03-11 18:36:11 +00001072 pr_info("%s: can't remove protocol\n", __func__);
Florian Westphal4f518e82019-05-03 17:46:19 +02001073 xfrm_unregister_type(&esp_type, AF_INET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074}
1075
1076module_init(esp4_init);
1077module_exit(esp4_fini);
1078MODULE_LICENSE("GPL");
Masahide NAKAMURAd3d6dd32007-06-26 23:57:49 -07001079MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_ESP);