]> git.ipfire.org Git - thirdparty/linux.git/blame - net/ipv4/esp4.c
treewide: Add SPDX license identifier for more missed files
[thirdparty/linux.git] / net / ipv4 / esp4.c
CommitLineData
09c434b8 1// SPDX-License-Identifier: GPL-2.0-only
afd46503
JP
2#define pr_fmt(fmt) "IPsec: " fmt
3
38320c70
HX
4#include <crypto/aead.h>
5#include <crypto/authenc.h>
6b7326c8 6#include <linux/err.h>
1da177e4
LT
7#include <linux/module.h>
8#include <net/ip.h>
9#include <net/xfrm.h>
10#include <net/esp.h>
72998d8c 11#include <linux/scatterlist.h>
a02a6422 12#include <linux/kernel.h>
1da177e4 13#include <linux/pfkeyv2.h>
38320c70
HX
14#include <linux/rtnetlink.h>
15#include <linux/slab.h>
b7c6538c 16#include <linux/spinlock.h>
2017a72c 17#include <linux/in6.h>
1da177e4 18#include <net/icmp.h>
14c85021 19#include <net/protocol.h>
1da177e4
LT
20#include <net/udp.h>
21
cac2661c
SK
22#include <linux/highmem.h>
23
38320c70
HX
24struct esp_skb_cb {
25 struct xfrm_skb_cb xfrm;
26 void *tmp;
27};
28
962fcef3
HX
29struct esp_output_extra {
30 __be32 seqhi;
31 u32 esphoff;
32};
33
38320c70
HX
34#define ESP_SKB_CB(__skb) ((struct esp_skb_cb *)&((__skb)->cb[0]))
35
d979e20f
MW
36static u32 esp4_get_mtu(struct xfrm_state *x, int mtu);
37
38320c70
HX
38/*
39 * Allocate an AEAD request structure with extra space for SG and IV.
40 *
41 * For alignment considerations the IV is placed at the front, followed
42 * by the request and finally the SG list.
43 *
44 * TODO: Use spare space in skb for this where possible.
45 */
962fcef3 46static void *esp_alloc_tmp(struct crypto_aead *aead, int nfrags, int extralen)
38320c70
HX
47{
48 unsigned int len;
49
962fcef3 50 len = extralen;
0dc49e9b
SK
51
52 len += crypto_aead_ivsize(aead);
53
38320c70
HX
54 if (len) {
55 len += crypto_aead_alignmask(aead) &
56 ~(crypto_tfm_ctx_alignment() - 1);
57 len = ALIGN(len, crypto_tfm_ctx_alignment());
58 }
59
7021b2e1 60 len += sizeof(struct aead_request) + crypto_aead_reqsize(aead);
38320c70
HX
61 len = ALIGN(len, __alignof__(struct scatterlist));
62
63 len += sizeof(struct scatterlist) * nfrags;
64
65 return kmalloc(len, GFP_ATOMIC);
66}
67
962fcef3 68static inline void *esp_tmp_extra(void *tmp)
0dc49e9b 69{
962fcef3 70 return PTR_ALIGN(tmp, __alignof__(struct esp_output_extra));
0dc49e9b 71}
962fcef3
HX
72
73static inline u8 *esp_tmp_iv(struct crypto_aead *aead, void *tmp, int extralen)
38320c70
HX
74{
75 return crypto_aead_ivsize(aead) ?
962fcef3
HX
76 PTR_ALIGN((u8 *)tmp + extralen,
77 crypto_aead_alignmask(aead) + 1) : tmp + extralen;
38320c70
HX
78}
79
38320c70
HX
80static inline struct aead_request *esp_tmp_req(struct crypto_aead *aead, u8 *iv)
81{
82 struct aead_request *req;
83
84 req = (void *)PTR_ALIGN(iv + crypto_aead_ivsize(aead),
85 crypto_tfm_ctx_alignment());
86 aead_request_set_tfm(req, aead);
87 return req;
88}
89
90static inline struct scatterlist *esp_req_sg(struct crypto_aead *aead,
91 struct aead_request *req)
92{
93 return (void *)ALIGN((unsigned long)(req + 1) +
94 crypto_aead_reqsize(aead),
95 __alignof__(struct scatterlist));
96}
97
cac2661c
SK
98static void esp_ssg_unref(struct xfrm_state *x, void *tmp)
99{
100 struct esp_output_extra *extra = esp_tmp_extra(tmp);
101 struct crypto_aead *aead = x->data;
102 int extralen = 0;
103 u8 *iv;
104 struct aead_request *req;
105 struct scatterlist *sg;
106
107 if (x->props.flags & XFRM_STATE_ESN)
108 extralen += sizeof(*extra);
109
110 extra = esp_tmp_extra(tmp);
111 iv = esp_tmp_iv(aead, tmp, extralen);
112 req = esp_tmp_req(aead, iv);
113
114 /* Unref skb_frag_pages in the src scatterlist if necessary.
115 * Skip the first sg which comes from skb->data.
116 */
117 if (req->src != req->dst)
118 for (sg = sg_next(req->src); sg; sg = sg_next(sg))
119 put_page(sg_page(sg));
120}
121
38320c70
HX
122static void esp_output_done(struct crypto_async_request *base, int err)
123{
124 struct sk_buff *skb = base->data;
f53c7239 125 struct xfrm_offload *xo = xfrm_offload(skb);
cac2661c 126 void *tmp;
f53c7239
SK
127 struct xfrm_state *x;
128
2294be0f
FW
129 if (xo && (xo->flags & XFRM_DEV_RESUME)) {
130 struct sec_path *sp = skb_sec_path(skb);
131
132 x = sp->xvec[sp->len - 1];
133 } else {
f53c7239 134 x = skb_dst(skb)->xfrm;
2294be0f 135 }
38320c70 136
cac2661c
SK
137 tmp = ESP_SKB_CB(skb)->tmp;
138 esp_ssg_unref(x, tmp);
139 kfree(tmp);
f53c7239
SK
140
141 if (xo && (xo->flags & XFRM_DEV_RESUME)) {
142 if (err) {
143 XFRM_INC_STATS(xs_net(x), LINUX_MIB_XFRMOUTSTATEPROTOERROR);
144 kfree_skb(skb);
145 return;
146 }
147
148 skb_push(skb, skb->data - skb_mac_header(skb));
149 secpath_reset(skb);
150 xfrm_dev_resume(skb);
151 } else {
152 xfrm_output_resume(skb, err);
153 }
38320c70
HX
154}
155
7021b2e1
HX
156/* Move ESP header back into place. */
157static void esp_restore_header(struct sk_buff *skb, unsigned int offset)
158{
159 struct ip_esp_hdr *esph = (void *)(skb->data + offset);
160 void *tmp = ESP_SKB_CB(skb)->tmp;
962fcef3 161 __be32 *seqhi = esp_tmp_extra(tmp);
7021b2e1
HX
162
163 esph->seq_no = esph->spi;
164 esph->spi = *seqhi;
165}
166
167static void esp_output_restore_header(struct sk_buff *skb)
168{
962fcef3
HX
169 void *tmp = ESP_SKB_CB(skb)->tmp;
170 struct esp_output_extra *extra = esp_tmp_extra(tmp);
171
172 esp_restore_header(skb, skb_transport_offset(skb) + extra->esphoff -
173 sizeof(__be32));
7021b2e1
HX
174}
175
cac2661c 176static struct ip_esp_hdr *esp_output_set_extra(struct sk_buff *skb,
fca11ebd 177 struct xfrm_state *x,
cac2661c
SK
178 struct ip_esp_hdr *esph,
179 struct esp_output_extra *extra)
180{
cac2661c
SK
181 /* For ESN we move the header forward by 4 bytes to
182 * accomodate the high bits. We will move it back after
183 * encryption.
184 */
185 if ((x->props.flags & XFRM_STATE_ESN)) {
7862b405
SK
186 __u32 seqhi;
187 struct xfrm_offload *xo = xfrm_offload(skb);
188
189 if (xo)
190 seqhi = xo->seq.hi;
191 else
192 seqhi = XFRM_SKB_CB(skb)->seq.output.hi;
193
cac2661c
SK
194 extra->esphoff = (unsigned char *)esph -
195 skb_transport_header(skb);
196 esph = (struct ip_esp_hdr *)((unsigned char *)esph - 4);
197 extra->seqhi = esph->spi;
7862b405 198 esph->seq_no = htonl(seqhi);
cac2661c
SK
199 }
200
201 esph->spi = x->id.spi;
202
203 return esph;
204}
205
7021b2e1
HX
206static void esp_output_done_esn(struct crypto_async_request *base, int err)
207{
208 struct sk_buff *skb = base->data;
209
210 esp_output_restore_header(skb);
211 esp_output_done(base, err);
212}
213
eb758c88
SK
214static void esp_output_fill_trailer(u8 *tail, int tfclen, int plen, __u8 proto)
215{
216 /* Fill padding... */
217 if (tfclen) {
218 memset(tail, 0, tfclen);
219 tail += tfclen;
220 }
221 do {
222 int i;
223 for (i = 0; i < plen - 2; i++)
224 tail[i] = i + 1;
225 } while (0);
226 tail[plen - 2] = plen - 2;
227 tail[plen - 1] = proto;
228}
229
8dfb4eba 230static int esp_output_udp_encap(struct xfrm_state *x, struct sk_buff *skb, struct esp_info *esp)
1da177e4 231{
fca11ebd
SK
232 int encap_type;
233 struct udphdr *uh;
234 __be32 *udpdata32;
235 __be16 sport, dport;
236 struct xfrm_encap_tmpl *encap = x->encap;
237 struct ip_esp_hdr *esph = esp->esph;
8dfb4eba 238 unsigned int len;
fca11ebd
SK
239
240 spin_lock_bh(&x->lock);
241 sport = encap->encap_sport;
242 dport = encap->encap_dport;
243 encap_type = encap->encap_type;
244 spin_unlock_bh(&x->lock);
245
8dfb4eba
SD
246 len = skb->len + esp->tailen - skb_transport_offset(skb);
247 if (len + sizeof(struct iphdr) >= IP_MAX_MTU)
248 return -EMSGSIZE;
249
fca11ebd
SK
250 uh = (struct udphdr *)esph;
251 uh->source = sport;
252 uh->dest = dport;
8dfb4eba 253 uh->len = htons(len);
fca11ebd
SK
254 uh->check = 0;
255
256 switch (encap_type) {
257 default:
258 case UDP_ENCAP_ESPINUDP:
259 esph = (struct ip_esp_hdr *)(uh + 1);
260 break;
261 case UDP_ENCAP_ESPINUDP_NON_IKE:
262 udpdata32 = (__be32 *)(uh + 1);
263 udpdata32[0] = udpdata32[1] = 0;
264 esph = (struct ip_esp_hdr *)(udpdata32 + 2);
265 break;
d979e20f 266 }
0dc49e9b 267
fca11ebd
SK
268 *skb_mac_header(skb) = IPPROTO_UDP;
269 esp->esph = esph;
8dfb4eba
SD
270
271 return 0;
fca11ebd 272}
0dc49e9b 273
fca11ebd
SK
274int esp_output_head(struct xfrm_state *x, struct sk_buff *skb, struct esp_info *esp)
275{
276 u8 *tail;
277 u8 *vaddr;
278 int nfrags;
0e78a873 279 int esph_offset;
fca11ebd
SK
280 struct page *page;
281 struct sk_buff *trailer;
282 int tailen = esp->tailen;
1da177e4
LT
283
284 /* this is non-NULL only with UDP Encapsulation */
8dfb4eba
SD
285 if (x->encap) {
286 int err = esp_output_udp_encap(x, skb, esp);
287
288 if (err < 0)
289 return err;
290 }
1da177e4 291
cac2661c 292 if (!skb_cloned(skb)) {
54ffd790 293 if (tailen <= skb_tailroom(skb)) {
cac2661c
SK
294 nfrags = 1;
295 trailer = skb;
296 tail = skb_tail_pointer(trailer);
1da177e4 297
cac2661c
SK
298 goto skip_cow;
299 } else if ((skb_shinfo(skb)->nr_frags < MAX_SKB_FRAGS)
300 && !skb_has_frag_list(skb)) {
301 int allocsize;
302 struct sock *sk = skb->sk;
303 struct page_frag *pfrag = &x->xfrag;
7021b2e1 304
fca11ebd
SK
305 esp->inplace = false;
306
cac2661c
SK
307 allocsize = ALIGN(tailen, L1_CACHE_BYTES);
308
309 spin_lock_bh(&x->lock);
310
311 if (unlikely(!skb_page_frag_refill(allocsize, pfrag, GFP_ATOMIC))) {
312 spin_unlock_bh(&x->lock);
313 goto cow;
314 }
315
316 page = pfrag->page;
317 get_page(page);
318
319 vaddr = kmap_atomic(page);
320
321 tail = vaddr + pfrag->offset;
322
fca11ebd 323 esp_output_fill_trailer(tail, esp->tfclen, esp->plen, esp->proto);
cac2661c
SK
324
325 kunmap_atomic(vaddr);
326
327 nfrags = skb_shinfo(skb)->nr_frags;
328
329 __skb_fill_page_desc(skb, nfrags, page, pfrag->offset,
330 tailen);
331 skb_shinfo(skb)->nr_frags = ++nfrags;
332
333 pfrag->offset = pfrag->offset + allocsize;
36ff0dd3
SK
334
335 spin_unlock_bh(&x->lock);
336
cac2661c
SK
337 nfrags++;
338
339 skb->len += tailen;
340 skb->data_len += tailen;
341 skb->truesize += tailen;
09db5124 342 if (sk && sk_fullsock(sk))
14afee4b 343 refcount_add(tailen, &sk->sk_wmem_alloc);
cac2661c 344
fca11ebd 345 goto out;
cac2661c 346 }
7021b2e1
HX
347 }
348
cac2661c 349cow:
0e78a873
SK
350 esph_offset = (unsigned char *)esp->esph - skb_transport_header(skb);
351
fca11ebd
SK
352 nfrags = skb_cow_data(skb, tailen, &trailer);
353 if (nfrags < 0)
354 goto out;
cac2661c 355 tail = skb_tail_pointer(trailer);
0e78a873 356 esp->esph = (struct ip_esp_hdr *)(skb_transport_header(skb) + esph_offset);
cac2661c
SK
357
358skip_cow:
fca11ebd
SK
359 esp_output_fill_trailer(tail, esp->tfclen, esp->plen, esp->proto);
360 pskb_put(skb, trailer, tailen);
cac2661c 361
fca11ebd
SK
362out:
363 return nfrags;
364}
365EXPORT_SYMBOL_GPL(esp_output_head);
366
367int esp_output_tail(struct xfrm_state *x, struct sk_buff *skb, struct esp_info *esp)
368{
369 u8 *iv;
370 int alen;
371 void *tmp;
372 int ivlen;
373 int assoclen;
374 int extralen;
375 struct page *page;
376 struct ip_esp_hdr *esph;
377 struct crypto_aead *aead;
378 struct aead_request *req;
379 struct scatterlist *sg, *dsg;
380 struct esp_output_extra *extra;
381 int err = -ENOMEM;
382
383 assoclen = sizeof(struct ip_esp_hdr);
384 extralen = 0;
385
386 if (x->props.flags & XFRM_STATE_ESN) {
387 extralen += sizeof(*extra);
388 assoclen += sizeof(__be32);
389 }
7021b2e1 390
fca11ebd
SK
391 aead = x->data;
392 alen = crypto_aead_authsize(aead);
393 ivlen = crypto_aead_ivsize(aead);
394
395 tmp = esp_alloc_tmp(aead, esp->nfrags + 2, extralen);
e892d2d4 396 if (!tmp)
cac2661c 397 goto error;
cac2661c
SK
398
399 extra = esp_tmp_extra(tmp);
400 iv = esp_tmp_iv(aead, tmp, extralen);
401 req = esp_tmp_req(aead, iv);
402 sg = esp_req_sg(aead, req);
cac2661c 403
fca11ebd
SK
404 if (esp->inplace)
405 dsg = sg;
406 else
407 dsg = &sg[esp->nfrags];
cac2661c 408
fca11ebd
SK
409 esph = esp_output_set_extra(skb, x, esp->esph, extra);
410 esp->esph = esph;
411
412 sg_init_table(sg, esp->nfrags);
3f297707
JD
413 err = skb_to_sgvec(skb, sg,
414 (unsigned char *)esph - skb->data,
415 assoclen + ivlen + esp->clen + alen);
416 if (unlikely(err < 0))
e6194923 417 goto error_free;
fca11ebd
SK
418
419 if (!esp->inplace) {
420 int allocsize;
421 struct page_frag *pfrag = &x->xfrag;
422
423 allocsize = ALIGN(skb->data_len, L1_CACHE_BYTES);
424
425 spin_lock_bh(&x->lock);
426 if (unlikely(!skb_page_frag_refill(allocsize, pfrag, GFP_ATOMIC))) {
427 spin_unlock_bh(&x->lock);
e6194923 428 goto error_free;
fca11ebd
SK
429 }
430
431 skb_shinfo(skb)->nr_frags = 1;
432
433 page = pfrag->page;
434 get_page(page);
435 /* replace page frags in skb with new page */
436 __skb_fill_page_desc(skb, 0, page, pfrag->offset, skb->data_len);
437 pfrag->offset = pfrag->offset + allocsize;
438 spin_unlock_bh(&x->lock);
439
440 sg_init_table(dsg, skb_shinfo(skb)->nr_frags + 1);
3f297707
JD
441 err = skb_to_sgvec(skb, dsg,
442 (unsigned char *)esph - skb->data,
443 assoclen + ivlen + esp->clen + alen);
444 if (unlikely(err < 0))
e6194923 445 goto error_free;
fca11ebd 446 }
0dc49e9b 447
cac2661c
SK
448 if ((x->props.flags & XFRM_STATE_ESN))
449 aead_request_set_callback(req, 0, esp_output_done_esn, skb);
450 else
451 aead_request_set_callback(req, 0, esp_output_done, skb);
452
fca11ebd 453 aead_request_set_crypt(req, sg, dsg, ivlen + esp->clen, iv);
7021b2e1
HX
454 aead_request_set_ad(req, assoclen);
455
7021b2e1 456 memset(iv, 0, ivlen);
fca11ebd 457 memcpy(iv + ivlen - min(ivlen, 8), (u8 *)&esp->seqno + 8 - min(ivlen, 8),
7021b2e1 458 min(ivlen, 8));
38320c70
HX
459
460 ESP_SKB_CB(skb)->tmp = tmp;
7021b2e1
HX
461 err = crypto_aead_encrypt(req);
462
463 switch (err) {
464 case -EINPROGRESS:
38320c70 465 goto error;
1da177e4 466
068c2e70 467 case -ENOSPC:
38320c70 468 err = NET_XMIT_DROP;
7021b2e1
HX
469 break;
470
471 case 0:
472 if ((x->props.flags & XFRM_STATE_ESN))
473 esp_output_restore_header(skb);
474 }
1da177e4 475
cac2661c
SK
476 if (sg != dsg)
477 esp_ssg_unref(x, tmp);
b7c6538c 478
e6194923
SK
479error_free:
480 kfree(tmp);
1da177e4
LT
481error:
482 return err;
483}
fca11ebd
SK
484EXPORT_SYMBOL_GPL(esp_output_tail);
485
486static int esp_output(struct xfrm_state *x, struct sk_buff *skb)
487{
488 int alen;
489 int blksize;
490 struct ip_esp_hdr *esph;
491 struct crypto_aead *aead;
492 struct esp_info esp;
493
494 esp.inplace = true;
495
496 esp.proto = *skb_mac_header(skb);
497 *skb_mac_header(skb) = IPPROTO_ESP;
498
499 /* skb is pure payload to encrypt */
500
501 aead = x->data;
502 alen = crypto_aead_authsize(aead);
503
504 esp.tfclen = 0;
505 if (x->tfcpad) {
506 struct xfrm_dst *dst = (struct xfrm_dst *)skb_dst(skb);
507 u32 padto;
508
509 padto = min(x->tfcpad, esp4_get_mtu(x, dst->child_mtu_cached));
510 if (skb->len < padto)
511 esp.tfclen = padto - skb->len;
512 }
513 blksize = ALIGN(crypto_aead_blocksize(aead), 4);
514 esp.clen = ALIGN(skb->len + 2 + esp.tfclen, blksize);
515 esp.plen = esp.clen - skb->len - esp.tfclen;
516 esp.tailen = esp.tfclen + esp.plen + alen;
517
518 esp.esph = ip_esp_hdr(skb);
519
520 esp.nfrags = esp_output_head(x, skb, &esp);
521 if (esp.nfrags < 0)
522 return esp.nfrags;
523
524 esph = esp.esph;
525 esph->spi = x->id.spi;
526
527 esph->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output.low);
528 esp.seqno = cpu_to_be64(XFRM_SKB_CB(skb)->seq.output.low +
529 ((u64)XFRM_SKB_CB(skb)->seq.output.hi << 32));
530
531 skb_push(skb, -skb_network_offset(skb));
532
533 return esp_output_tail(x, skb, &esp);
534}
1da177e4 535
47ebcc0b
YK
536static inline int esp_remove_trailer(struct sk_buff *skb)
537{
538 struct xfrm_state *x = xfrm_input_state(skb);
539 struct xfrm_offload *xo = xfrm_offload(skb);
540 struct crypto_aead *aead = x->data;
541 int alen, hlen, elen;
542 int padlen, trimlen;
543 __wsum csumdiff;
544 u8 nexthdr[2];
545 int ret;
546
547 alen = crypto_aead_authsize(aead);
548 hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead);
549 elen = skb->len - hlen;
550
551 if (xo && (xo->flags & XFRM_ESP_NO_TRAILER)) {
552 ret = xo->proto;
553 goto out;
554 }
555
556 if (skb_copy_bits(skb, skb->len - alen - 2, nexthdr, 2))
557 BUG();
558
559 ret = -EINVAL;
560 padlen = nexthdr[0];
561 if (padlen + 2 + alen >= elen) {
562 net_dbg_ratelimited("ipsec esp packet is garbage padlen=%d, elen=%d\n",
563 padlen + 2, elen - alen);
564 goto out;
565 }
566
567 trimlen = alen + padlen + 2;
568 if (skb->ip_summed == CHECKSUM_COMPLETE) {
569 csumdiff = skb_checksum(skb, skb->len - trimlen, trimlen, 0);
570 skb->csum = csum_block_sub(skb->csum, csumdiff,
571 skb->len - trimlen);
572 }
573 pskb_trim(skb, skb->len - trimlen);
574
575 ret = nexthdr[1];
576
577out:
578 return ret;
579}
580
fca11ebd 581int esp_input_done2(struct sk_buff *skb, int err)
1da177e4 582{
b71d1d42 583 const struct iphdr *iph;
38320c70 584 struct xfrm_state *x = xfrm_input_state(skb);
d77e38e6 585 struct xfrm_offload *xo = xfrm_offload(skb);
1c5ad13f 586 struct crypto_aead *aead = x->data;
38320c70 587 int hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead);
31a4ab93 588 int ihl;
1da177e4 589
d77e38e6
SK
590 if (!xo || (xo && !(xo->flags & CRYPTO_DONE)))
591 kfree(ESP_SKB_CB(skb)->tmp);
0ebea8ef 592
6b7326c8 593 if (unlikely(err))
668dc8af 594 goto out;
1da177e4 595
47ebcc0b
YK
596 err = esp_remove_trailer(skb);
597 if (unlikely(err < 0))
4bf05ece 598 goto out;
1da177e4 599
eddc9ec5 600 iph = ip_hdr(skb);
31a4ab93
HX
601 ihl = iph->ihl * 4;
602
752c1f4c
HX
603 if (x->encap) {
604 struct xfrm_encap_tmpl *encap = x->encap;
d56f90a7 605 struct udphdr *uh = (void *)(skb_network_header(skb) + ihl);
752c1f4c
HX
606
607 /*
608 * 1) if the NAT-T peer's IP or port changed then
609 * advertize the change to the keying daemon.
610 * This is an inbound SA, so just compare
611 * SRC ports.
612 */
613 if (iph->saddr != x->props.saddr.a4 ||
614 uh->source != encap->encap_sport) {
615 xfrm_address_t ipaddr;
616
617 ipaddr.a4 = iph->saddr;
618 km_new_mapping(x, &ipaddr, uh->source);
e905a9ed 619
752c1f4c
HX
620 /* XXX: perhaps add an extra
621 * policy check here, to see
622 * if we should allow or
623 * reject a packet from a
624 * different source
625 * address/port.
626 */
1da177e4 627 }
e905a9ed 628
752c1f4c
HX
629 /*
630 * 2) ignore UDP/TCP checksums in case
631 * of NAT-T in Transport Mode, or
632 * perform other post-processing fixes
633 * as per draft-ietf-ipsec-udp-encaps-06,
634 * section 3.1.2
635 */
8bd17075 636 if (x->props.mode == XFRM_MODE_TRANSPORT)
752c1f4c 637 skb->ip_summed = CHECKSUM_UNNECESSARY;
1da177e4
LT
638 }
639
ec9567a9 640 skb_pull_rcsum(skb, hlen);
7143dfac
LR
641 if (x->props.mode == XFRM_MODE_TUNNEL)
642 skb_reset_transport_header(skb);
643 else
644 skb_set_transport_header(skb, -ihl);
4bf05ece 645
38320c70
HX
646 /* RFC4303: Drop dummy packets without any error */
647 if (err == IPPROTO_NONE)
648 err = -EINVAL;
649
650out:
651 return err;
652}
fca11ebd 653EXPORT_SYMBOL_GPL(esp_input_done2);
38320c70
HX
654
655static void esp_input_done(struct crypto_async_request *base, int err)
656{
657 struct sk_buff *skb = base->data;
658
659 xfrm_input_resume(skb, esp_input_done2(skb, err));
660}
661
7021b2e1
HX
662static void esp_input_restore_header(struct sk_buff *skb)
663{
664 esp_restore_header(skb, 0);
665 __skb_pull(skb, 4);
666}
667
cac2661c
SK
668static void esp_input_set_header(struct sk_buff *skb, __be32 *seqhi)
669{
670 struct xfrm_state *x = xfrm_input_state(skb);
60aa8046 671 struct ip_esp_hdr *esph;
cac2661c
SK
672
673 /* For ESN we move the header forward by 4 bytes to
674 * accomodate the high bits. We will move it back after
675 * decryption.
676 */
677 if ((x->props.flags & XFRM_STATE_ESN)) {
d58ff351 678 esph = skb_push(skb, 4);
cac2661c
SK
679 *seqhi = esph->spi;
680 esph->spi = esph->seq_no;
681 esph->seq_no = XFRM_SKB_CB(skb)->seq.input.hi;
682 }
683}
684
7021b2e1
HX
685static void esp_input_done_esn(struct crypto_async_request *base, int err)
686{
687 struct sk_buff *skb = base->data;
688
689 esp_input_restore_header(skb);
690 esp_input_done(base, err);
691}
692
38320c70
HX
693/*
694 * Note: detecting truncated vs. non-truncated authentication data is very
695 * expensive, so we only support truncated data, which is the recommended
696 * and common case.
697 */
698static int esp_input(struct xfrm_state *x, struct sk_buff *skb)
699{
1c5ad13f 700 struct crypto_aead *aead = x->data;
38320c70
HX
701 struct aead_request *req;
702 struct sk_buff *trailer;
7021b2e1 703 int ivlen = crypto_aead_ivsize(aead);
0c05f983 704 int elen = skb->len - sizeof(struct ip_esp_hdr) - ivlen;
38320c70 705 int nfrags;
0dc49e9b 706 int assoclen;
0dc49e9b
SK
707 int seqhilen;
708 __be32 *seqhi;
38320c70
HX
709 void *tmp;
710 u8 *iv;
711 struct scatterlist *sg;
38320c70
HX
712 int err = -EINVAL;
713
0c05f983 714 if (!pskb_may_pull(skb, sizeof(struct ip_esp_hdr) + ivlen))
38320c70
HX
715 goto out;
716
717 if (elen <= 0)
718 goto out;
719
0c05f983 720 assoclen = sizeof(struct ip_esp_hdr);
0dc49e9b
SK
721 seqhilen = 0;
722
723 if (x->props.flags & XFRM_STATE_ESN) {
0dc49e9b
SK
724 seqhilen += sizeof(__be32);
725 assoclen += seqhilen;
726 }
727
cac2661c
SK
728 if (!skb_cloned(skb)) {
729 if (!skb_is_nonlinear(skb)) {
730 nfrags = 1;
731
732 goto skip_cow;
733 } else if (!skb_has_frag_list(skb)) {
734 nfrags = skb_shinfo(skb)->nr_frags;
735 nfrags++;
736
737 goto skip_cow;
738 }
739 }
740
741 err = skb_cow_data(skb, 0, &trailer);
742 if (err < 0)
743 goto out;
744
745 nfrags = err;
746
747skip_cow:
38320c70 748 err = -ENOMEM;
7021b2e1 749 tmp = esp_alloc_tmp(aead, nfrags, seqhilen);
38320c70
HX
750 if (!tmp)
751 goto out;
752
753 ESP_SKB_CB(skb)->tmp = tmp;
962fcef3 754 seqhi = esp_tmp_extra(tmp);
0dc49e9b 755 iv = esp_tmp_iv(aead, tmp, seqhilen);
38320c70 756 req = esp_tmp_req(aead, iv);
7021b2e1 757 sg = esp_req_sg(aead, req);
38320c70 758
cac2661c 759 esp_input_set_header(skb, seqhi);
38320c70 760
cac2661c 761 sg_init_table(sg, nfrags);
3f297707 762 err = skb_to_sgvec(skb, sg, 0, skb->len);
e6194923
SK
763 if (unlikely(err < 0)) {
764 kfree(tmp);
3f297707 765 goto out;
e6194923 766 }
38320c70 767
cac2661c 768 skb->ip_summed = CHECKSUM_NONE;
0dc49e9b 769
cac2661c 770 if ((x->props.flags & XFRM_STATE_ESN))
7021b2e1 771 aead_request_set_callback(req, 0, esp_input_done_esn, skb);
cac2661c
SK
772 else
773 aead_request_set_callback(req, 0, esp_input_done, skb);
7021b2e1
HX
774
775 aead_request_set_crypt(req, sg, sg, elen + ivlen, iv);
776 aead_request_set_ad(req, assoclen);
38320c70
HX
777
778 err = crypto_aead_decrypt(req);
779 if (err == -EINPROGRESS)
780 goto out;
781
7021b2e1
HX
782 if ((x->props.flags & XFRM_STATE_ESN))
783 esp_input_restore_header(skb);
784
38320c70 785 err = esp_input_done2(skb, err);
1da177e4
LT
786
787out:
668dc8af 788 return err;
1da177e4
LT
789}
790
c5c25238 791static u32 esp4_get_mtu(struct xfrm_state *x, int mtu)
1da177e4 792{
1c5ad13f
MK
793 struct crypto_aead *aead = x->data;
794 u32 blksize = ALIGN(crypto_aead_blocksize(aead), 4);
91657eaf 795 unsigned int net_adj;
0a69452c
DB
796
797 switch (x->props.mode) {
0a69452c 798 case XFRM_MODE_TRANSPORT:
0a69452c 799 case XFRM_MODE_BEET:
91657eaf 800 net_adj = sizeof(struct iphdr);
0a69452c 801 break;
91657eaf
BP
802 case XFRM_MODE_TUNNEL:
803 net_adj = 0;
804 break;
805 default:
806 BUG();
1da177e4 807 }
0a69452c 808
1c5ad13f 809 return ((mtu - x->props.header_len - crypto_aead_authsize(aead) -
123b0d1b 810 net_adj) & ~(blksize - 1)) + net_adj - 2;
1da177e4
LT
811}
812
827789cb 813static int esp4_err(struct sk_buff *skb, u32 info)
1da177e4 814{
4fb236ba 815 struct net *net = dev_net(skb->dev);
b71d1d42 816 const struct iphdr *iph = (const struct iphdr *)skb->data;
d9319100 817 struct ip_esp_hdr *esph = (struct ip_esp_hdr *)(skb->data+(iph->ihl<<2));
1da177e4
LT
818 struct xfrm_state *x;
819
55be7a9c
DM
820 switch (icmp_hdr(skb)->type) {
821 case ICMP_DEST_UNREACH:
822 if (icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
827789cb 823 return 0;
55be7a9c
DM
824 case ICMP_REDIRECT:
825 break;
826 default:
827789cb 827 return 0;
55be7a9c 828 }
1da177e4 829
b71d1d42
ED
830 x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr,
831 esph->spi, IPPROTO_ESP, AF_INET);
1da177e4 832 if (!x)
827789cb 833 return 0;
55be7a9c 834
387aa65a 835 if (icmp_hdr(skb)->type == ICMP_DEST_UNREACH)
d888f396 836 ipv4_update_pmtu(skb, net, info, 0, IPPROTO_ESP);
387aa65a 837 else
1042caa7 838 ipv4_redirect(skb, net, 0, IPPROTO_ESP);
1da177e4 839 xfrm_state_put(x);
827789cb
SK
840
841 return 0;
1da177e4
LT
842}
843
844static void esp_destroy(struct xfrm_state *x)
845{
1c5ad13f 846 struct crypto_aead *aead = x->data;
1da177e4 847
1c5ad13f 848 if (!aead)
1da177e4
LT
849 return;
850
1c5ad13f 851 crypto_free_aead(aead);
1da177e4
LT
852}
853
1a6509d9 854static int esp_init_aead(struct xfrm_state *x)
1da177e4 855{
7021b2e1 856 char aead_name[CRYPTO_MAX_ALG_NAME];
1a6509d9
HX
857 struct crypto_aead *aead;
858 int err;
859
7021b2e1
HX
860 err = -ENAMETOOLONG;
861 if (snprintf(aead_name, CRYPTO_MAX_ALG_NAME, "%s(%s)",
862 x->geniv, x->aead->alg_name) >= CRYPTO_MAX_ALG_NAME)
863 goto error;
864
f58869c4 865 aead = crypto_alloc_aead(aead_name, 0, 0);
1a6509d9
HX
866 err = PTR_ERR(aead);
867 if (IS_ERR(aead))
868 goto error;
869
1c5ad13f 870 x->data = aead;
1a6509d9
HX
871
872 err = crypto_aead_setkey(aead, x->aead->alg_key,
873 (x->aead->alg_key_len + 7) / 8);
874 if (err)
875 goto error;
876
877 err = crypto_aead_setauthsize(aead, x->aead->alg_icv_len / 8);
878 if (err)
879 goto error;
880
881error:
882 return err;
883}
884
885static int esp_init_authenc(struct xfrm_state *x)
886{
38320c70
HX
887 struct crypto_aead *aead;
888 struct crypto_authenc_key_param *param;
889 struct rtattr *rta;
890 char *key;
891 char *p;
892 char authenc_name[CRYPTO_MAX_ALG_NAME];
38320c70
HX
893 unsigned int keylen;
894 int err;
1da177e4 895
1a6509d9 896 err = -EINVAL;
51456b29 897 if (!x->ealg)
1a6509d9 898 goto error;
38320c70 899
1a6509d9 900 err = -ENAMETOOLONG;
0dc49e9b
SK
901
902 if ((x->props.flags & XFRM_STATE_ESN)) {
903 if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME,
7021b2e1
HX
904 "%s%sauthencesn(%s,%s)%s",
905 x->geniv ?: "", x->geniv ? "(" : "",
0dc49e9b 906 x->aalg ? x->aalg->alg_name : "digest_null",
7021b2e1
HX
907 x->ealg->alg_name,
908 x->geniv ? ")" : "") >= CRYPTO_MAX_ALG_NAME)
0dc49e9b
SK
909 goto error;
910 } else {
911 if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME,
7021b2e1
HX
912 "%s%sauthenc(%s,%s)%s",
913 x->geniv ?: "", x->geniv ? "(" : "",
0dc49e9b 914 x->aalg ? x->aalg->alg_name : "digest_null",
7021b2e1
HX
915 x->ealg->alg_name,
916 x->geniv ? ")" : "") >= CRYPTO_MAX_ALG_NAME)
0dc49e9b
SK
917 goto error;
918 }
38320c70 919
f58869c4 920 aead = crypto_alloc_aead(authenc_name, 0, 0);
38320c70
HX
921 err = PTR_ERR(aead);
922 if (IS_ERR(aead))
923 goto error;
924
1c5ad13f 925 x->data = aead;
38320c70
HX
926
927 keylen = (x->aalg ? (x->aalg->alg_key_len + 7) / 8 : 0) +
928 (x->ealg->alg_key_len + 7) / 8 + RTA_SPACE(sizeof(*param));
929 err = -ENOMEM;
930 key = kmalloc(keylen, GFP_KERNEL);
931 if (!key)
932 goto error;
933
934 p = key;
935 rta = (void *)p;
936 rta->rta_type = CRYPTO_AUTHENC_KEYA_PARAM;
937 rta->rta_len = RTA_LENGTH(sizeof(*param));
938 param = RTA_DATA(rta);
939 p += RTA_SPACE(sizeof(*param));
940
1da177e4
LT
941 if (x->aalg) {
942 struct xfrm_algo_desc *aalg_desc;
943
38320c70
HX
944 memcpy(p, x->aalg->alg_key, (x->aalg->alg_key_len + 7) / 8);
945 p += (x->aalg->alg_key_len + 7) / 8;
1da177e4
LT
946
947 aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
948 BUG_ON(!aalg_desc);
949
38320c70 950 err = -EINVAL;
45083497 951 if (aalg_desc->uinfo.auth.icv_fullbits / 8 !=
38320c70 952 crypto_aead_authsize(aead)) {
45083497
JP
953 pr_info("ESP: %s digestsize %u != %hu\n",
954 x->aalg->alg_name,
955 crypto_aead_authsize(aead),
956 aalg_desc->uinfo.auth.icv_fullbits / 8);
38320c70 957 goto free_key;
1da177e4
LT
958 }
959
38320c70 960 err = crypto_aead_setauthsize(
8f8a088c 961 aead, x->aalg->alg_trunc_len / 8);
38320c70
HX
962 if (err)
963 goto free_key;
1da177e4 964 }
4b7137ff 965
38320c70
HX
966 param->enckeylen = cpu_to_be32((x->ealg->alg_key_len + 7) / 8);
967 memcpy(p, x->ealg->alg_key, (x->ealg->alg_key_len + 7) / 8);
968
969 err = crypto_aead_setkey(aead, key, keylen);
970
971free_key:
972 kfree(key);
973
1a6509d9
HX
974error:
975 return err;
976}
977
978static int esp_init_state(struct xfrm_state *x)
979{
1a6509d9
HX
980 struct crypto_aead *aead;
981 u32 align;
982 int err;
983
1c5ad13f 984 x->data = NULL;
1a6509d9
HX
985
986 if (x->aead)
987 err = esp_init_aead(x);
988 else
989 err = esp_init_authenc(x);
990
38320c70 991 if (err)
1da177e4 992 goto error;
38320c70 993
1c5ad13f 994 aead = x->data;
1a6509d9 995
38320c70
HX
996 x->props.header_len = sizeof(struct ip_esp_hdr) +
997 crypto_aead_ivsize(aead);
7e49e6de 998 if (x->props.mode == XFRM_MODE_TUNNEL)
1da177e4 999 x->props.header_len += sizeof(struct iphdr);
eb49e630 1000 else if (x->props.mode == XFRM_MODE_BEET && x->sel.family != AF_INET6)
ac758e3c 1001 x->props.header_len += IPV4_BEET_PHMAXLEN;
1da177e4
LT
1002 if (x->encap) {
1003 struct xfrm_encap_tmpl *encap = x->encap;
1004
1005 switch (encap->encap_type) {
1006 default:
bcfd09f7 1007 err = -EINVAL;
1da177e4
LT
1008 goto error;
1009 case UDP_ENCAP_ESPINUDP:
1010 x->props.header_len += sizeof(struct udphdr);
1011 break;
1012 case UDP_ENCAP_ESPINUDP_NON_IKE:
1013 x->props.header_len += sizeof(struct udphdr) + 2 * sizeof(u32);
1014 break;
1015 }
1016 }
38320c70
HX
1017
1018 align = ALIGN(crypto_aead_blocksize(aead), 4);
1c5ad13f 1019 x->props.trailer_len = align + 1 + crypto_aead_authsize(aead);
1da177e4
LT
1020
1021error:
38320c70 1022 return err;
1da177e4
LT
1023}
1024
827789cb
SK
1025static int esp4_rcv_cb(struct sk_buff *skb, int err)
1026{
1027 return 0;
1028}
1029
533cb5b0 1030static const struct xfrm_type esp_type =
1da177e4
LT
1031{
1032 .description = "ESP4",
1033 .owner = THIS_MODULE,
1034 .proto = IPPROTO_ESP,
436a0a40 1035 .flags = XFRM_TYPE_REPLAY_PROT,
1da177e4
LT
1036 .init_state = esp_init_state,
1037 .destructor = esp_destroy,
c5c25238 1038 .get_mtu = esp4_get_mtu,
1da177e4 1039 .input = esp_input,
fca11ebd 1040 .output = esp_output,
1da177e4
LT
1041};
1042
827789cb 1043static struct xfrm4_protocol esp4_protocol = {
1da177e4 1044 .handler = xfrm4_rcv,
827789cb
SK
1045 .input_handler = xfrm_input,
1046 .cb_handler = esp4_rcv_cb,
1da177e4 1047 .err_handler = esp4_err,
827789cb 1048 .priority = 0,
1da177e4
LT
1049};
1050
1051static int __init esp4_init(void)
1052{
1da177e4 1053 if (xfrm_register_type(&esp_type, AF_INET) < 0) {
058bd4d2 1054 pr_info("%s: can't add xfrm type\n", __func__);
1da177e4
LT
1055 return -EAGAIN;
1056 }
827789cb 1057 if (xfrm4_protocol_register(&esp4_protocol, IPPROTO_ESP) < 0) {
058bd4d2 1058 pr_info("%s: can't add protocol\n", __func__);
1da177e4
LT
1059 xfrm_unregister_type(&esp_type, AF_INET);
1060 return -EAGAIN;
1061 }
1062 return 0;
1063}
1064
1065static void __exit esp4_fini(void)
1066{
827789cb 1067 if (xfrm4_protocol_deregister(&esp4_protocol, IPPROTO_ESP) < 0)
058bd4d2 1068 pr_info("%s: can't remove protocol\n", __func__);
1da177e4 1069 if (xfrm_unregister_type(&esp_type, AF_INET) < 0)
058bd4d2 1070 pr_info("%s: can't remove xfrm type\n", __func__);
1da177e4
LT
1071}
1072
1073module_init(esp4_init);
1074module_exit(esp4_fini);
1075MODULE_LICENSE("GPL");
d3d6dd3a 1076MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_ESP);