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