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 */
esp_alloc_tmp(struct crypto_aead * aead,int nfrags,int extralen)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
esp_tmp_extra(void * tmp)67 static inline void *esp_tmp_extra(void *tmp)
68 {
69 return PTR_ALIGN(tmp, __alignof__(struct esp_output_extra));
70 }
71
esp_tmp_iv(struct crypto_aead * aead,void * tmp,int extralen)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
esp_tmp_req(struct crypto_aead * aead,u8 * iv)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
esp_req_sg(struct crypto_aead * aead,struct aead_request * req)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
esp_ssg_unref(struct xfrm_state * x,void * tmp)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
esp_output_done(struct crypto_async_request * base,int err)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. */
esp_restore_header(struct sk_buff * skb,unsigned int offset)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
esp_output_restore_header(struct sk_buff * skb)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
esp_output_set_extra(struct sk_buff * skb,struct xfrm_state * x,struct ip_esp_hdr * esph,struct esp_output_extra * extra)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
esp_output_done_esn(struct crypto_async_request * base,int err)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
esp_output_fill_trailer(u8 * tail,int tfclen,int plen,__u8 proto)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
esp_output_udp_encap(struct xfrm_state * x,struct sk_buff * skb,struct esp_info * esp)208 static int 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 unsigned int len;
217
218 spin_lock_bh(&x->lock);
219 sport = encap->encap_sport;
220 dport = encap->encap_dport;
221 encap_type = encap->encap_type;
222 spin_unlock_bh(&x->lock);
223
224 len = skb->len + esp->tailen - skb_transport_offset(skb);
225 if (len + sizeof(struct iphdr) >= IP_MAX_MTU)
226 return -EMSGSIZE;
227
228 uh = (struct udphdr *)esph;
229 uh->source = sport;
230 uh->dest = dport;
231 uh->len = htons(len);
232 uh->check = 0;
233
234 switch (encap_type) {
235 default:
236 case UDP_ENCAP_ESPINUDP:
237 esph = (struct ip_esp_hdr *)(uh + 1);
238 break;
239 case UDP_ENCAP_ESPINUDP_NON_IKE:
240 udpdata32 = (__be32 *)(uh + 1);
241 udpdata32[0] = udpdata32[1] = 0;
242 esph = (struct ip_esp_hdr *)(udpdata32 + 2);
243 break;
244 }
245
246 *skb_mac_header(skb) = IPPROTO_UDP;
247 esp->esph = esph;
248
249 return 0;
250 }
251
esp_output_head(struct xfrm_state * x,struct sk_buff * skb,struct esp_info * esp)252 int esp_output_head(struct xfrm_state *x, struct sk_buff *skb, struct esp_info *esp)
253 {
254 u8 *tail;
255 u8 *vaddr;
256 int nfrags;
257 int esph_offset;
258 struct page *page;
259 struct sk_buff *trailer;
260 int tailen = esp->tailen;
261
262 /* this is non-NULL only with UDP Encapsulation */
263 if (x->encap) {
264 int err = esp_output_udp_encap(x, skb, esp);
265
266 if (err < 0)
267 return err;
268 }
269
270 if (!skb_cloned(skb)) {
271 if (tailen <= skb_tailroom(skb)) {
272 nfrags = 1;
273 trailer = skb;
274 tail = skb_tail_pointer(trailer);
275
276 goto skip_cow;
277 } else if ((skb_shinfo(skb)->nr_frags < MAX_SKB_FRAGS)
278 && !skb_has_frag_list(skb)) {
279 int allocsize;
280 struct sock *sk = skb->sk;
281 struct page_frag *pfrag = &x->xfrag;
282
283 esp->inplace = false;
284
285 allocsize = ALIGN(tailen, L1_CACHE_BYTES);
286
287 spin_lock_bh(&x->lock);
288
289 if (unlikely(!skb_page_frag_refill(allocsize, pfrag, GFP_ATOMIC))) {
290 spin_unlock_bh(&x->lock);
291 goto cow;
292 }
293
294 page = pfrag->page;
295 get_page(page);
296
297 vaddr = kmap_atomic(page);
298
299 tail = vaddr + pfrag->offset;
300
301 esp_output_fill_trailer(tail, esp->tfclen, esp->plen, esp->proto);
302
303 kunmap_atomic(vaddr);
304
305 nfrags = skb_shinfo(skb)->nr_frags;
306
307 __skb_fill_page_desc(skb, nfrags, page, pfrag->offset,
308 tailen);
309 skb_shinfo(skb)->nr_frags = ++nfrags;
310
311 pfrag->offset = pfrag->offset + allocsize;
312
313 spin_unlock_bh(&x->lock);
314
315 nfrags++;
316
317 skb->len += tailen;
318 skb->data_len += tailen;
319 skb->truesize += tailen;
320 if (sk && sk_fullsock(sk))
321 refcount_add(tailen, &sk->sk_wmem_alloc);
322
323 goto out;
324 }
325 }
326
327 cow:
328 esph_offset = (unsigned char *)esp->esph - skb_transport_header(skb);
329
330 nfrags = skb_cow_data(skb, tailen, &trailer);
331 if (nfrags < 0)
332 goto out;
333 tail = skb_tail_pointer(trailer);
334 esp->esph = (struct ip_esp_hdr *)(skb_transport_header(skb) + esph_offset);
335
336 skip_cow:
337 esp_output_fill_trailer(tail, esp->tfclen, esp->plen, esp->proto);
338 pskb_put(skb, trailer, tailen);
339
340 out:
341 return nfrags;
342 }
343 EXPORT_SYMBOL_GPL(esp_output_head);
344
esp_output_tail(struct xfrm_state * x,struct sk_buff * skb,struct esp_info * esp)345 int esp_output_tail(struct xfrm_state *x, struct sk_buff *skb, struct esp_info *esp)
346 {
347 u8 *iv;
348 int alen;
349 void *tmp;
350 int ivlen;
351 int assoclen;
352 int extralen;
353 struct page *page;
354 struct ip_esp_hdr *esph;
355 struct crypto_aead *aead;
356 struct aead_request *req;
357 struct scatterlist *sg, *dsg;
358 struct esp_output_extra *extra;
359 int err = -ENOMEM;
360
361 assoclen = sizeof(struct ip_esp_hdr);
362 extralen = 0;
363
364 if (x->props.flags & XFRM_STATE_ESN) {
365 extralen += sizeof(*extra);
366 assoclen += sizeof(__be32);
367 }
368
369 aead = x->data;
370 alen = crypto_aead_authsize(aead);
371 ivlen = crypto_aead_ivsize(aead);
372
373 tmp = esp_alloc_tmp(aead, esp->nfrags + 2, extralen);
374 if (!tmp)
375 goto error;
376
377 extra = esp_tmp_extra(tmp);
378 iv = esp_tmp_iv(aead, tmp, extralen);
379 req = esp_tmp_req(aead, iv);
380 sg = esp_req_sg(aead, req);
381
382 if (esp->inplace)
383 dsg = sg;
384 else
385 dsg = &sg[esp->nfrags];
386
387 esph = esp_output_set_extra(skb, x, esp->esph, extra);
388 esp->esph = esph;
389
390 sg_init_table(sg, esp->nfrags);
391 err = skb_to_sgvec(skb, sg,
392 (unsigned char *)esph - skb->data,
393 assoclen + ivlen + esp->clen + alen);
394 if (unlikely(err < 0))
395 goto error_free;
396
397 if (!esp->inplace) {
398 int allocsize;
399 struct page_frag *pfrag = &x->xfrag;
400
401 allocsize = ALIGN(skb->data_len, L1_CACHE_BYTES);
402
403 spin_lock_bh(&x->lock);
404 if (unlikely(!skb_page_frag_refill(allocsize, pfrag, GFP_ATOMIC))) {
405 spin_unlock_bh(&x->lock);
406 goto error_free;
407 }
408
409 skb_shinfo(skb)->nr_frags = 1;
410
411 page = pfrag->page;
412 get_page(page);
413 /* replace page frags in skb with new page */
414 __skb_fill_page_desc(skb, 0, page, pfrag->offset, skb->data_len);
415 pfrag->offset = pfrag->offset + allocsize;
416 spin_unlock_bh(&x->lock);
417
418 sg_init_table(dsg, skb_shinfo(skb)->nr_frags + 1);
419 err = skb_to_sgvec(skb, dsg,
420 (unsigned char *)esph - skb->data,
421 assoclen + ivlen + esp->clen + alen);
422 if (unlikely(err < 0))
423 goto error_free;
424 }
425
426 if ((x->props.flags & XFRM_STATE_ESN))
427 aead_request_set_callback(req, 0, esp_output_done_esn, skb);
428 else
429 aead_request_set_callback(req, 0, esp_output_done, skb);
430
431 aead_request_set_crypt(req, sg, dsg, ivlen + esp->clen, iv);
432 aead_request_set_ad(req, assoclen);
433
434 memset(iv, 0, ivlen);
435 memcpy(iv + ivlen - min(ivlen, 8), (u8 *)&esp->seqno + 8 - min(ivlen, 8),
436 min(ivlen, 8));
437
438 ESP_SKB_CB(skb)->tmp = tmp;
439 err = crypto_aead_encrypt(req);
440
441 switch (err) {
442 case -EINPROGRESS:
443 goto error;
444
445 case -EBUSY:
446 err = NET_XMIT_DROP;
447 break;
448
449 case 0:
450 if ((x->props.flags & XFRM_STATE_ESN))
451 esp_output_restore_header(skb);
452 }
453
454 if (sg != dsg)
455 esp_ssg_unref(x, tmp);
456
457 error_free:
458 kfree(tmp);
459 error:
460 return err;
461 }
462 EXPORT_SYMBOL_GPL(esp_output_tail);
463
esp_output(struct xfrm_state * x,struct sk_buff * skb)464 static int esp_output(struct xfrm_state *x, struct sk_buff *skb)
465 {
466 int alen;
467 int blksize;
468 struct ip_esp_hdr *esph;
469 struct crypto_aead *aead;
470 struct esp_info esp;
471
472 esp.inplace = true;
473
474 esp.proto = *skb_mac_header(skb);
475 *skb_mac_header(skb) = IPPROTO_ESP;
476
477 /* skb is pure payload to encrypt */
478
479 aead = x->data;
480 alen = crypto_aead_authsize(aead);
481
482 esp.tfclen = 0;
483 if (x->tfcpad) {
484 struct xfrm_dst *dst = (struct xfrm_dst *)skb_dst(skb);
485 u32 padto;
486
487 padto = min(x->tfcpad, esp4_get_mtu(x, dst->child_mtu_cached));
488 if (skb->len < padto)
489 esp.tfclen = padto - skb->len;
490 }
491 blksize = ALIGN(crypto_aead_blocksize(aead), 4);
492 esp.clen = ALIGN(skb->len + 2 + esp.tfclen, blksize);
493 esp.plen = esp.clen - skb->len - esp.tfclen;
494 esp.tailen = esp.tfclen + esp.plen + alen;
495
496 esp.esph = ip_esp_hdr(skb);
497
498 esp.nfrags = esp_output_head(x, skb, &esp);
499 if (esp.nfrags < 0)
500 return esp.nfrags;
501
502 esph = esp.esph;
503 esph->spi = x->id.spi;
504
505 esph->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output.low);
506 esp.seqno = cpu_to_be64(XFRM_SKB_CB(skb)->seq.output.low +
507 ((u64)XFRM_SKB_CB(skb)->seq.output.hi << 32));
508
509 skb_push(skb, -skb_network_offset(skb));
510
511 return esp_output_tail(x, skb, &esp);
512 }
513
esp_remove_trailer(struct sk_buff * skb)514 static inline int esp_remove_trailer(struct sk_buff *skb)
515 {
516 struct xfrm_state *x = xfrm_input_state(skb);
517 struct xfrm_offload *xo = xfrm_offload(skb);
518 struct crypto_aead *aead = x->data;
519 int alen, hlen, elen;
520 int padlen, trimlen;
521 __wsum csumdiff;
522 u8 nexthdr[2];
523 int ret;
524
525 alen = crypto_aead_authsize(aead);
526 hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead);
527 elen = skb->len - hlen;
528
529 if (xo && (xo->flags & XFRM_ESP_NO_TRAILER)) {
530 ret = xo->proto;
531 goto out;
532 }
533
534 if (skb_copy_bits(skb, skb->len - alen - 2, nexthdr, 2))
535 BUG();
536
537 ret = -EINVAL;
538 padlen = nexthdr[0];
539 if (padlen + 2 + alen >= elen) {
540 net_dbg_ratelimited("ipsec esp packet is garbage padlen=%d, elen=%d\n",
541 padlen + 2, elen - alen);
542 goto out;
543 }
544
545 trimlen = alen + padlen + 2;
546 if (skb->ip_summed == CHECKSUM_COMPLETE) {
547 csumdiff = skb_checksum(skb, skb->len - trimlen, trimlen, 0);
548 skb->csum = csum_block_sub(skb->csum, csumdiff,
549 skb->len - trimlen);
550 }
551 pskb_trim(skb, skb->len - trimlen);
552
553 ret = nexthdr[1];
554
555 out:
556 return ret;
557 }
558
esp_input_done2(struct sk_buff * skb,int err)559 int esp_input_done2(struct sk_buff *skb, int err)
560 {
561 const struct iphdr *iph;
562 struct xfrm_state *x = xfrm_input_state(skb);
563 struct xfrm_offload *xo = xfrm_offload(skb);
564 struct crypto_aead *aead = x->data;
565 int hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead);
566 int ihl;
567
568 if (!xo || (xo && !(xo->flags & CRYPTO_DONE)))
569 kfree(ESP_SKB_CB(skb)->tmp);
570
571 if (unlikely(err))
572 goto out;
573
574 err = esp_remove_trailer(skb);
575 if (unlikely(err < 0))
576 goto out;
577
578 iph = ip_hdr(skb);
579 ihl = iph->ihl * 4;
580
581 if (x->encap) {
582 struct xfrm_encap_tmpl *encap = x->encap;
583 struct udphdr *uh = (void *)(skb_network_header(skb) + ihl);
584
585 /*
586 * 1) if the NAT-T peer's IP or port changed then
587 * advertize the change to the keying daemon.
588 * This is an inbound SA, so just compare
589 * SRC ports.
590 */
591 if (iph->saddr != x->props.saddr.a4 ||
592 uh->source != encap->encap_sport) {
593 xfrm_address_t ipaddr;
594
595 ipaddr.a4 = iph->saddr;
596 km_new_mapping(x, &ipaddr, uh->source);
597
598 /* XXX: perhaps add an extra
599 * policy check here, to see
600 * if we should allow or
601 * reject a packet from a
602 * different source
603 * address/port.
604 */
605 }
606
607 /*
608 * 2) ignore UDP/TCP checksums in case
609 * of NAT-T in Transport Mode, or
610 * perform other post-processing fixes
611 * as per draft-ietf-ipsec-udp-encaps-06,
612 * section 3.1.2
613 */
614 if (x->props.mode == XFRM_MODE_TRANSPORT)
615 skb->ip_summed = CHECKSUM_UNNECESSARY;
616 }
617
618 skb_pull_rcsum(skb, hlen);
619 if (x->props.mode == XFRM_MODE_TUNNEL)
620 skb_reset_transport_header(skb);
621 else
622 skb_set_transport_header(skb, -ihl);
623
624 /* RFC4303: Drop dummy packets without any error */
625 if (err == IPPROTO_NONE)
626 err = -EINVAL;
627
628 out:
629 return err;
630 }
631 EXPORT_SYMBOL_GPL(esp_input_done2);
632
esp_input_done(struct crypto_async_request * base,int err)633 static void esp_input_done(struct crypto_async_request *base, int err)
634 {
635 struct sk_buff *skb = base->data;
636
637 xfrm_input_resume(skb, esp_input_done2(skb, err));
638 }
639
esp_input_restore_header(struct sk_buff * skb)640 static void esp_input_restore_header(struct sk_buff *skb)
641 {
642 esp_restore_header(skb, 0);
643 __skb_pull(skb, 4);
644 }
645
esp_input_set_header(struct sk_buff * skb,__be32 * seqhi)646 static void esp_input_set_header(struct sk_buff *skb, __be32 *seqhi)
647 {
648 struct xfrm_state *x = xfrm_input_state(skb);
649 struct ip_esp_hdr *esph = (struct ip_esp_hdr *)skb->data;
650
651 /* For ESN we move the header forward by 4 bytes to
652 * accomodate the high bits. We will move it back after
653 * decryption.
654 */
655 if ((x->props.flags & XFRM_STATE_ESN)) {
656 esph = skb_push(skb, 4);
657 *seqhi = esph->spi;
658 esph->spi = esph->seq_no;
659 esph->seq_no = XFRM_SKB_CB(skb)->seq.input.hi;
660 }
661 }
662
esp_input_done_esn(struct crypto_async_request * base,int err)663 static void esp_input_done_esn(struct crypto_async_request *base, int err)
664 {
665 struct sk_buff *skb = base->data;
666
667 esp_input_restore_header(skb);
668 esp_input_done(base, err);
669 }
670
671 /*
672 * Note: detecting truncated vs. non-truncated authentication data is very
673 * expensive, so we only support truncated data, which is the recommended
674 * and common case.
675 */
esp_input(struct xfrm_state * x,struct sk_buff * skb)676 static int esp_input(struct xfrm_state *x, struct sk_buff *skb)
677 {
678 struct ip_esp_hdr *esph;
679 struct crypto_aead *aead = x->data;
680 struct aead_request *req;
681 struct sk_buff *trailer;
682 int ivlen = crypto_aead_ivsize(aead);
683 int elen = skb->len - sizeof(*esph) - ivlen;
684 int nfrags;
685 int assoclen;
686 int seqhilen;
687 __be32 *seqhi;
688 void *tmp;
689 u8 *iv;
690 struct scatterlist *sg;
691 int err = -EINVAL;
692
693 if (!pskb_may_pull(skb, sizeof(*esph) + ivlen))
694 goto out;
695
696 if (elen <= 0)
697 goto out;
698
699 assoclen = sizeof(*esph);
700 seqhilen = 0;
701
702 if (x->props.flags & XFRM_STATE_ESN) {
703 seqhilen += sizeof(__be32);
704 assoclen += seqhilen;
705 }
706
707 if (!skb_cloned(skb)) {
708 if (!skb_is_nonlinear(skb)) {
709 nfrags = 1;
710
711 goto skip_cow;
712 } else if (!skb_has_frag_list(skb)) {
713 nfrags = skb_shinfo(skb)->nr_frags;
714 nfrags++;
715
716 goto skip_cow;
717 }
718 }
719
720 err = skb_cow_data(skb, 0, &trailer);
721 if (err < 0)
722 goto out;
723
724 nfrags = err;
725
726 skip_cow:
727 err = -ENOMEM;
728 tmp = esp_alloc_tmp(aead, nfrags, seqhilen);
729 if (!tmp)
730 goto out;
731
732 ESP_SKB_CB(skb)->tmp = tmp;
733 seqhi = esp_tmp_extra(tmp);
734 iv = esp_tmp_iv(aead, tmp, seqhilen);
735 req = esp_tmp_req(aead, iv);
736 sg = esp_req_sg(aead, req);
737
738 esp_input_set_header(skb, seqhi);
739
740 sg_init_table(sg, nfrags);
741 err = skb_to_sgvec(skb, sg, 0, skb->len);
742 if (unlikely(err < 0)) {
743 kfree(tmp);
744 goto out;
745 }
746
747 skb->ip_summed = CHECKSUM_NONE;
748
749 if ((x->props.flags & XFRM_STATE_ESN))
750 aead_request_set_callback(req, 0, esp_input_done_esn, skb);
751 else
752 aead_request_set_callback(req, 0, esp_input_done, skb);
753
754 aead_request_set_crypt(req, sg, sg, elen + ivlen, iv);
755 aead_request_set_ad(req, assoclen);
756
757 err = crypto_aead_decrypt(req);
758 if (err == -EINPROGRESS)
759 goto out;
760
761 if ((x->props.flags & XFRM_STATE_ESN))
762 esp_input_restore_header(skb);
763
764 err = esp_input_done2(skb, err);
765
766 out:
767 return err;
768 }
769
esp4_get_mtu(struct xfrm_state * x,int mtu)770 static u32 esp4_get_mtu(struct xfrm_state *x, int mtu)
771 {
772 struct crypto_aead *aead = x->data;
773 u32 blksize = ALIGN(crypto_aead_blocksize(aead), 4);
774 unsigned int net_adj;
775
776 switch (x->props.mode) {
777 case XFRM_MODE_TRANSPORT:
778 case XFRM_MODE_BEET:
779 net_adj = sizeof(struct iphdr);
780 break;
781 case XFRM_MODE_TUNNEL:
782 net_adj = 0;
783 break;
784 default:
785 BUG();
786 }
787
788 return ((mtu - x->props.header_len - crypto_aead_authsize(aead) -
789 net_adj) & ~(blksize - 1)) + net_adj - 2;
790 }
791
esp4_err(struct sk_buff * skb,u32 info)792 static int esp4_err(struct sk_buff *skb, u32 info)
793 {
794 struct net *net = dev_net(skb->dev);
795 const struct iphdr *iph = (const struct iphdr *)skb->data;
796 struct ip_esp_hdr *esph = (struct ip_esp_hdr *)(skb->data+(iph->ihl<<2));
797 struct xfrm_state *x;
798
799 switch (icmp_hdr(skb)->type) {
800 case ICMP_DEST_UNREACH:
801 if (icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
802 return 0;
803 case ICMP_REDIRECT:
804 break;
805 default:
806 return 0;
807 }
808
809 x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr,
810 esph->spi, IPPROTO_ESP, AF_INET);
811 if (!x)
812 return 0;
813
814 if (icmp_hdr(skb)->type == ICMP_DEST_UNREACH)
815 ipv4_update_pmtu(skb, net, info, 0, 0, IPPROTO_ESP, 0);
816 else
817 ipv4_redirect(skb, net, 0, 0, IPPROTO_ESP, 0);
818 xfrm_state_put(x);
819
820 return 0;
821 }
822
esp_destroy(struct xfrm_state * x)823 static void esp_destroy(struct xfrm_state *x)
824 {
825 struct crypto_aead *aead = x->data;
826
827 if (!aead)
828 return;
829
830 crypto_free_aead(aead);
831 }
832
esp_init_aead(struct xfrm_state * x)833 static int esp_init_aead(struct xfrm_state *x)
834 {
835 char aead_name[CRYPTO_MAX_ALG_NAME];
836 struct crypto_aead *aead;
837 int err;
838 u32 mask = 0;
839
840 err = -ENAMETOOLONG;
841 if (snprintf(aead_name, CRYPTO_MAX_ALG_NAME, "%s(%s)",
842 x->geniv, x->aead->alg_name) >= CRYPTO_MAX_ALG_NAME)
843 goto error;
844
845 if (x->xso.offload_handle)
846 mask |= CRYPTO_ALG_ASYNC;
847
848 aead = crypto_alloc_aead(aead_name, 0, mask);
849 err = PTR_ERR(aead);
850 if (IS_ERR(aead))
851 goto error;
852
853 x->data = aead;
854
855 err = crypto_aead_setkey(aead, x->aead->alg_key,
856 (x->aead->alg_key_len + 7) / 8);
857 if (err)
858 goto error;
859
860 err = crypto_aead_setauthsize(aead, x->aead->alg_icv_len / 8);
861 if (err)
862 goto error;
863
864 error:
865 return err;
866 }
867
esp_init_authenc(struct xfrm_state * x)868 static int esp_init_authenc(struct xfrm_state *x)
869 {
870 struct crypto_aead *aead;
871 struct crypto_authenc_key_param *param;
872 struct rtattr *rta;
873 char *key;
874 char *p;
875 char authenc_name[CRYPTO_MAX_ALG_NAME];
876 unsigned int keylen;
877 int err;
878 u32 mask = 0;
879
880 err = -EINVAL;
881 if (!x->ealg)
882 goto error;
883
884 err = -ENAMETOOLONG;
885
886 if ((x->props.flags & XFRM_STATE_ESN)) {
887 if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME,
888 "%s%sauthencesn(%s,%s)%s",
889 x->geniv ?: "", x->geniv ? "(" : "",
890 x->aalg ? x->aalg->alg_name : "digest_null",
891 x->ealg->alg_name,
892 x->geniv ? ")" : "") >= CRYPTO_MAX_ALG_NAME)
893 goto error;
894 } else {
895 if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME,
896 "%s%sauthenc(%s,%s)%s",
897 x->geniv ?: "", x->geniv ? "(" : "",
898 x->aalg ? x->aalg->alg_name : "digest_null",
899 x->ealg->alg_name,
900 x->geniv ? ")" : "") >= CRYPTO_MAX_ALG_NAME)
901 goto error;
902 }
903
904 if (x->xso.offload_handle)
905 mask |= CRYPTO_ALG_ASYNC;
906
907 aead = crypto_alloc_aead(authenc_name, 0, mask);
908 err = PTR_ERR(aead);
909 if (IS_ERR(aead))
910 goto error;
911
912 x->data = aead;
913
914 keylen = (x->aalg ? (x->aalg->alg_key_len + 7) / 8 : 0) +
915 (x->ealg->alg_key_len + 7) / 8 + RTA_SPACE(sizeof(*param));
916 err = -ENOMEM;
917 key = kmalloc(keylen, GFP_KERNEL);
918 if (!key)
919 goto error;
920
921 p = key;
922 rta = (void *)p;
923 rta->rta_type = CRYPTO_AUTHENC_KEYA_PARAM;
924 rta->rta_len = RTA_LENGTH(sizeof(*param));
925 param = RTA_DATA(rta);
926 p += RTA_SPACE(sizeof(*param));
927
928 if (x->aalg) {
929 struct xfrm_algo_desc *aalg_desc;
930
931 memcpy(p, x->aalg->alg_key, (x->aalg->alg_key_len + 7) / 8);
932 p += (x->aalg->alg_key_len + 7) / 8;
933
934 aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
935 BUG_ON(!aalg_desc);
936
937 err = -EINVAL;
938 if (aalg_desc->uinfo.auth.icv_fullbits / 8 !=
939 crypto_aead_authsize(aead)) {
940 pr_info("ESP: %s digestsize %u != %hu\n",
941 x->aalg->alg_name,
942 crypto_aead_authsize(aead),
943 aalg_desc->uinfo.auth.icv_fullbits / 8);
944 goto free_key;
945 }
946
947 err = crypto_aead_setauthsize(
948 aead, x->aalg->alg_trunc_len / 8);
949 if (err)
950 goto free_key;
951 }
952
953 param->enckeylen = cpu_to_be32((x->ealg->alg_key_len + 7) / 8);
954 memcpy(p, x->ealg->alg_key, (x->ealg->alg_key_len + 7) / 8);
955
956 err = crypto_aead_setkey(aead, key, keylen);
957
958 free_key:
959 kfree(key);
960
961 error:
962 return err;
963 }
964
esp_init_state(struct xfrm_state * x)965 static int esp_init_state(struct xfrm_state *x)
966 {
967 struct crypto_aead *aead;
968 u32 align;
969 int err;
970
971 x->data = NULL;
972
973 if (x->aead)
974 err = esp_init_aead(x);
975 else
976 err = esp_init_authenc(x);
977
978 if (err)
979 goto error;
980
981 aead = x->data;
982
983 x->props.header_len = sizeof(struct ip_esp_hdr) +
984 crypto_aead_ivsize(aead);
985 if (x->props.mode == XFRM_MODE_TUNNEL)
986 x->props.header_len += sizeof(struct iphdr);
987 else if (x->props.mode == XFRM_MODE_BEET && x->sel.family != AF_INET6)
988 x->props.header_len += IPV4_BEET_PHMAXLEN;
989 if (x->encap) {
990 struct xfrm_encap_tmpl *encap = x->encap;
991
992 switch (encap->encap_type) {
993 default:
994 goto error;
995 case UDP_ENCAP_ESPINUDP:
996 x->props.header_len += sizeof(struct udphdr);
997 break;
998 case UDP_ENCAP_ESPINUDP_NON_IKE:
999 x->props.header_len += sizeof(struct udphdr) + 2 * sizeof(u32);
1000 break;
1001 }
1002 }
1003
1004 align = ALIGN(crypto_aead_blocksize(aead), 4);
1005 x->props.trailer_len = align + 1 + crypto_aead_authsize(aead);
1006
1007 error:
1008 return err;
1009 }
1010
esp4_rcv_cb(struct sk_buff * skb,int err)1011 static int esp4_rcv_cb(struct sk_buff *skb, int err)
1012 {
1013 return 0;
1014 }
1015
1016 static const struct xfrm_type esp_type =
1017 {
1018 .description = "ESP4",
1019 .owner = THIS_MODULE,
1020 .proto = IPPROTO_ESP,
1021 .flags = XFRM_TYPE_REPLAY_PROT,
1022 .init_state = esp_init_state,
1023 .destructor = esp_destroy,
1024 .get_mtu = esp4_get_mtu,
1025 .input = esp_input,
1026 .output = esp_output,
1027 };
1028
1029 static struct xfrm4_protocol esp4_protocol = {
1030 .handler = xfrm4_rcv,
1031 .input_handler = xfrm_input,
1032 .cb_handler = esp4_rcv_cb,
1033 .err_handler = esp4_err,
1034 .priority = 0,
1035 };
1036
esp4_init(void)1037 static int __init esp4_init(void)
1038 {
1039 if (xfrm_register_type(&esp_type, AF_INET) < 0) {
1040 pr_info("%s: can't add xfrm type\n", __func__);
1041 return -EAGAIN;
1042 }
1043 if (xfrm4_protocol_register(&esp4_protocol, IPPROTO_ESP) < 0) {
1044 pr_info("%s: can't add protocol\n", __func__);
1045 xfrm_unregister_type(&esp_type, AF_INET);
1046 return -EAGAIN;
1047 }
1048 return 0;
1049 }
1050
esp4_fini(void)1051 static void __exit esp4_fini(void)
1052 {
1053 if (xfrm4_protocol_deregister(&esp4_protocol, IPPROTO_ESP) < 0)
1054 pr_info("%s: can't remove protocol\n", __func__);
1055 if (xfrm_unregister_type(&esp_type, AF_INET) < 0)
1056 pr_info("%s: can't remove xfrm type\n", __func__);
1057 }
1058
1059 module_init(esp4_init);
1060 module_exit(esp4_fini);
1061 MODULE_LICENSE("GPL");
1062 MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_ESP);
1063