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