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