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