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