~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/ipv4/esp4.c

Version: ~ [ linux-5.12-rc7 ] ~ [ linux-5.11.13 ] ~ [ linux-5.10.29 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.111 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.186 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.230 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.266 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.266 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp