1 #define pr_fmt(fmt) "IPsec: " fmt
3 #include <crypto/aead.h>
4 #include <crypto/authenc.h>
6 #include <linux/module.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>
18 #include <net/protocol.h>
21 #include <linux/highmem.h>
24 struct xfrm_skb_cb xfrm;
28 struct esp_output_extra {
33 #define ESP_SKB_CB(__skb) ((struct esp_skb_cb *)&((__skb)->cb[0]))
35 static u32 esp4_get_mtu(struct xfrm_state *x, int mtu);
38 * Allocate an AEAD request structure with extra space for SG and IV.
40 * For alignment considerations the IV is placed at the front, followed
41 * by the request and finally the SG list.
43 * TODO: Use spare space in skb for this where possible.
45 static void *esp_alloc_tmp(struct crypto_aead *aead, int nfrags, int extralen)
51 len += crypto_aead_ivsize(aead);
54 len += crypto_aead_alignmask(aead) &
55 ~(crypto_tfm_ctx_alignment() - 1);
56 len = ALIGN(len, crypto_tfm_ctx_alignment());
59 len += sizeof(struct aead_request) + crypto_aead_reqsize(aead);
60 len = ALIGN(len, __alignof__(struct scatterlist));
62 len += sizeof(struct scatterlist) * nfrags;
64 return kmalloc(len, GFP_ATOMIC);
67 static inline void *esp_tmp_extra(void *tmp)
69 return PTR_ALIGN(tmp, __alignof__(struct esp_output_extra));
72 static inline u8 *esp_tmp_iv(struct crypto_aead *aead, void *tmp, int extralen)
74 return crypto_aead_ivsize(aead) ?
75 PTR_ALIGN((u8 *)tmp + extralen,
76 crypto_aead_alignmask(aead) + 1) : tmp + extralen;
79 static inline struct aead_request *esp_tmp_req(struct crypto_aead *aead, u8 *iv)
81 struct aead_request *req;
83 req = (void *)PTR_ALIGN(iv + crypto_aead_ivsize(aead),
84 crypto_tfm_ctx_alignment());
85 aead_request_set_tfm(req, aead);
89 static inline struct scatterlist *esp_req_sg(struct crypto_aead *aead,
90 struct aead_request *req)
92 return (void *)ALIGN((unsigned long)(req + 1) +
93 crypto_aead_reqsize(aead),
94 __alignof__(struct scatterlist));
97 static void esp_ssg_unref(struct xfrm_state *x, void *tmp)
99 struct esp_output_extra *extra = esp_tmp_extra(tmp);
100 struct crypto_aead *aead = x->data;
103 struct aead_request *req;
104 struct scatterlist *sg;
106 if (x->props.flags & XFRM_STATE_ESN)
107 extralen += sizeof(*extra);
109 extra = esp_tmp_extra(tmp);
110 iv = esp_tmp_iv(aead, tmp, extralen);
111 req = esp_tmp_req(aead, iv);
113 /* Unref skb_frag_pages in the src scatterlist if necessary.
114 * Skip the first sg which comes from skb->data.
116 if (req->src != req->dst)
117 for (sg = sg_next(req->src); sg; sg = sg_next(sg))
118 put_page(sg_page(sg));
121 static void esp_output_done(struct crypto_async_request *base, int err)
123 struct sk_buff *skb = base->data;
124 struct xfrm_offload *xo = xfrm_offload(skb);
126 struct xfrm_state *x;
128 if (xo && (xo->flags & XFRM_DEV_RESUME))
129 x = skb->sp->xvec[skb->sp->len - 1];
131 x = skb_dst(skb)->xfrm;
133 tmp = ESP_SKB_CB(skb)->tmp;
134 esp_ssg_unref(x, tmp);
137 if (xo && (xo->flags & XFRM_DEV_RESUME)) {
139 XFRM_INC_STATS(xs_net(x), LINUX_MIB_XFRMOUTSTATEPROTOERROR);
144 skb_push(skb, skb->data - skb_mac_header(skb));
146 xfrm_dev_resume(skb);
148 xfrm_output_resume(skb, err);
152 /* Move ESP header back into place. */
153 static void esp_restore_header(struct sk_buff *skb, unsigned int offset)
155 struct ip_esp_hdr *esph = (void *)(skb->data + offset);
156 void *tmp = ESP_SKB_CB(skb)->tmp;
157 __be32 *seqhi = esp_tmp_extra(tmp);
159 esph->seq_no = esph->spi;
163 static void esp_output_restore_header(struct sk_buff *skb)
165 void *tmp = ESP_SKB_CB(skb)->tmp;
166 struct esp_output_extra *extra = esp_tmp_extra(tmp);
168 esp_restore_header(skb, skb_transport_offset(skb) + extra->esphoff -
172 static struct ip_esp_hdr *esp_output_set_extra(struct sk_buff *skb,
173 struct xfrm_state *x,
174 struct ip_esp_hdr *esph,
175 struct esp_output_extra *extra)
177 /* For ESN we move the header forward by 4 bytes to
178 * accomodate the high bits. We will move it back after
181 if ((x->props.flags & XFRM_STATE_ESN)) {
183 struct xfrm_offload *xo = xfrm_offload(skb);
188 seqhi = XFRM_SKB_CB(skb)->seq.output.hi;
190 extra->esphoff = (unsigned char *)esph -
191 skb_transport_header(skb);
192 esph = (struct ip_esp_hdr *)((unsigned char *)esph - 4);
193 extra->seqhi = esph->spi;
194 esph->seq_no = htonl(seqhi);
197 esph->spi = x->id.spi;
202 static void esp_output_done_esn(struct crypto_async_request *base, int err)
204 struct sk_buff *skb = base->data;
206 esp_output_restore_header(skb);
207 esp_output_done(base, err);
210 static void esp_output_fill_trailer(u8 *tail, int tfclen, int plen, __u8 proto)
212 /* Fill padding... */
214 memset(tail, 0, tfclen);
219 for (i = 0; i < plen - 2; i++)
222 tail[plen - 2] = plen - 2;
223 tail[plen - 1] = proto;
226 static int esp_output_udp_encap(struct xfrm_state *x, struct sk_buff *skb, struct esp_info *esp)
232 struct xfrm_encap_tmpl *encap = x->encap;
233 struct ip_esp_hdr *esph = esp->esph;
236 spin_lock_bh(&x->lock);
237 sport = encap->encap_sport;
238 dport = encap->encap_dport;
239 encap_type = encap->encap_type;
240 spin_unlock_bh(&x->lock);
242 len = skb->len + esp->tailen - skb_transport_offset(skb);
243 if (len + sizeof(struct iphdr) >= IP_MAX_MTU)
246 uh = (struct udphdr *)esph;
249 uh->len = htons(len);
252 switch (encap_type) {
254 case UDP_ENCAP_ESPINUDP:
255 esph = (struct ip_esp_hdr *)(uh + 1);
257 case UDP_ENCAP_ESPINUDP_NON_IKE:
258 udpdata32 = (__be32 *)(uh + 1);
259 udpdata32[0] = udpdata32[1] = 0;
260 esph = (struct ip_esp_hdr *)(udpdata32 + 2);
264 *skb_mac_header(skb) = IPPROTO_UDP;
270 int esp_output_head(struct xfrm_state *x, struct sk_buff *skb, struct esp_info *esp)
276 struct sk_buff *trailer;
277 int tailen = esp->tailen;
278 unsigned int allocsz;
280 /* this is non-NULL only with UDP Encapsulation */
282 int err = esp_output_udp_encap(x, skb, esp);
288 allocsz = ALIGN(skb->data_len + tailen, L1_CACHE_BYTES);
289 if (allocsz > ESP_SKB_FRAG_MAXSIZE)
292 if (!skb_cloned(skb)) {
293 if (tailen <= skb_tailroom(skb)) {
296 tail = skb_tail_pointer(trailer);
299 } else if ((skb_shinfo(skb)->nr_frags < MAX_SKB_FRAGS)
300 && !skb_has_frag_list(skb)) {
302 struct sock *sk = skb->sk;
303 struct page_frag *pfrag = &x->xfrag;
305 esp->inplace = false;
307 allocsize = ALIGN(tailen, L1_CACHE_BYTES);
309 spin_lock_bh(&x->lock);
311 if (unlikely(!skb_page_frag_refill(allocsize, pfrag, GFP_ATOMIC))) {
312 spin_unlock_bh(&x->lock);
319 tail = page_address(page) + pfrag->offset;
321 esp_output_fill_trailer(tail, esp->tfclen, esp->plen, esp->proto);
323 nfrags = skb_shinfo(skb)->nr_frags;
325 __skb_fill_page_desc(skb, nfrags, page, pfrag->offset,
327 skb_shinfo(skb)->nr_frags = ++nfrags;
329 pfrag->offset = pfrag->offset + allocsize;
331 spin_unlock_bh(&x->lock);
336 skb->data_len += tailen;
337 skb->truesize += tailen;
338 if (sk && sk_fullsock(sk))
339 refcount_add(tailen, &sk->sk_wmem_alloc);
346 esph_offset = (unsigned char *)esp->esph - skb_transport_header(skb);
348 nfrags = skb_cow_data(skb, tailen, &trailer);
351 tail = skb_tail_pointer(trailer);
352 esp->esph = (struct ip_esp_hdr *)(skb_transport_header(skb) + esph_offset);
355 esp_output_fill_trailer(tail, esp->tfclen, esp->plen, esp->proto);
356 pskb_put(skb, trailer, tailen);
361 EXPORT_SYMBOL_GPL(esp_output_head);
363 int esp_output_tail(struct xfrm_state *x, struct sk_buff *skb, struct esp_info *esp)
372 struct ip_esp_hdr *esph;
373 struct crypto_aead *aead;
374 struct aead_request *req;
375 struct scatterlist *sg, *dsg;
376 struct esp_output_extra *extra;
379 assoclen = sizeof(struct ip_esp_hdr);
382 if (x->props.flags & XFRM_STATE_ESN) {
383 extralen += sizeof(*extra);
384 assoclen += sizeof(__be32);
388 alen = crypto_aead_authsize(aead);
389 ivlen = crypto_aead_ivsize(aead);
391 tmp = esp_alloc_tmp(aead, esp->nfrags + 2, extralen);
395 extra = esp_tmp_extra(tmp);
396 iv = esp_tmp_iv(aead, tmp, extralen);
397 req = esp_tmp_req(aead, iv);
398 sg = esp_req_sg(aead, req);
403 dsg = &sg[esp->nfrags];
405 esph = esp_output_set_extra(skb, x, esp->esph, extra);
408 sg_init_table(sg, esp->nfrags);
409 err = skb_to_sgvec(skb, sg,
410 (unsigned char *)esph - skb->data,
411 assoclen + ivlen + esp->clen + alen);
412 if (unlikely(err < 0))
417 struct page_frag *pfrag = &x->xfrag;
419 allocsize = ALIGN(skb->data_len, L1_CACHE_BYTES);
421 spin_lock_bh(&x->lock);
422 if (unlikely(!skb_page_frag_refill(allocsize, pfrag, GFP_ATOMIC))) {
423 spin_unlock_bh(&x->lock);
427 skb_shinfo(skb)->nr_frags = 1;
431 /* replace page frags in skb with new page */
432 __skb_fill_page_desc(skb, 0, page, pfrag->offset, skb->data_len);
433 pfrag->offset = pfrag->offset + allocsize;
434 spin_unlock_bh(&x->lock);
436 sg_init_table(dsg, skb_shinfo(skb)->nr_frags + 1);
437 err = skb_to_sgvec(skb, dsg,
438 (unsigned char *)esph - skb->data,
439 assoclen + ivlen + esp->clen + alen);
440 if (unlikely(err < 0))
444 if ((x->props.flags & XFRM_STATE_ESN))
445 aead_request_set_callback(req, 0, esp_output_done_esn, skb);
447 aead_request_set_callback(req, 0, esp_output_done, skb);
449 aead_request_set_crypt(req, sg, dsg, ivlen + esp->clen, iv);
450 aead_request_set_ad(req, assoclen);
452 memset(iv, 0, ivlen);
453 memcpy(iv + ivlen - min(ivlen, 8), (u8 *)&esp->seqno + 8 - min(ivlen, 8),
456 ESP_SKB_CB(skb)->tmp = tmp;
457 err = crypto_aead_encrypt(req);
468 if ((x->props.flags & XFRM_STATE_ESN))
469 esp_output_restore_header(skb);
473 esp_ssg_unref(x, tmp);
480 EXPORT_SYMBOL_GPL(esp_output_tail);
482 static int esp_output(struct xfrm_state *x, struct sk_buff *skb)
486 struct ip_esp_hdr *esph;
487 struct crypto_aead *aead;
492 esp.proto = *skb_mac_header(skb);
493 *skb_mac_header(skb) = IPPROTO_ESP;
495 /* skb is pure payload to encrypt */
498 alen = crypto_aead_authsize(aead);
502 struct xfrm_dst *dst = (struct xfrm_dst *)skb_dst(skb);
505 padto = min(x->tfcpad, esp4_get_mtu(x, dst->child_mtu_cached));
506 if (skb->len < padto)
507 esp.tfclen = padto - skb->len;
509 blksize = ALIGN(crypto_aead_blocksize(aead), 4);
510 esp.clen = ALIGN(skb->len + 2 + esp.tfclen, blksize);
511 esp.plen = esp.clen - skb->len - esp.tfclen;
512 esp.tailen = esp.tfclen + esp.plen + alen;
514 esp.esph = ip_esp_hdr(skb);
516 esp.nfrags = esp_output_head(x, skb, &esp);
521 esph->spi = x->id.spi;
523 esph->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output.low);
524 esp.seqno = cpu_to_be64(XFRM_SKB_CB(skb)->seq.output.low +
525 ((u64)XFRM_SKB_CB(skb)->seq.output.hi << 32));
527 skb_push(skb, -skb_network_offset(skb));
529 return esp_output_tail(x, skb, &esp);
532 static inline int esp_remove_trailer(struct sk_buff *skb)
534 struct xfrm_state *x = xfrm_input_state(skb);
535 struct xfrm_offload *xo = xfrm_offload(skb);
536 struct crypto_aead *aead = x->data;
537 int alen, hlen, elen;
543 alen = crypto_aead_authsize(aead);
544 hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead);
545 elen = skb->len - hlen;
547 if (xo && (xo->flags & XFRM_ESP_NO_TRAILER)) {
552 if (skb_copy_bits(skb, skb->len - alen - 2, nexthdr, 2))
557 if (padlen + 2 + alen >= elen) {
558 net_dbg_ratelimited("ipsec esp packet is garbage padlen=%d, elen=%d\n",
559 padlen + 2, elen - alen);
563 trimlen = alen + padlen + 2;
564 if (skb->ip_summed == CHECKSUM_COMPLETE) {
565 csumdiff = skb_checksum(skb, skb->len - trimlen, trimlen, 0);
566 skb->csum = csum_block_sub(skb->csum, csumdiff,
569 pskb_trim(skb, skb->len - trimlen);
577 int esp_input_done2(struct sk_buff *skb, int err)
579 const struct iphdr *iph;
580 struct xfrm_state *x = xfrm_input_state(skb);
581 struct xfrm_offload *xo = xfrm_offload(skb);
582 struct crypto_aead *aead = x->data;
583 int hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead);
586 if (!xo || (xo && !(xo->flags & CRYPTO_DONE)))
587 kfree(ESP_SKB_CB(skb)->tmp);
592 err = esp_remove_trailer(skb);
593 if (unlikely(err < 0))
600 struct xfrm_encap_tmpl *encap = x->encap;
601 struct udphdr *uh = (void *)(skb_network_header(skb) + ihl);
604 * 1) if the NAT-T peer's IP or port changed then
605 * advertize the change to the keying daemon.
606 * This is an inbound SA, so just compare
609 if (iph->saddr != x->props.saddr.a4 ||
610 uh->source != encap->encap_sport) {
611 xfrm_address_t ipaddr;
613 ipaddr.a4 = iph->saddr;
614 km_new_mapping(x, &ipaddr, uh->source);
616 /* XXX: perhaps add an extra
617 * policy check here, to see
618 * if we should allow or
619 * reject a packet from a
626 * 2) ignore UDP/TCP checksums in case
627 * of NAT-T in Transport Mode, or
628 * perform other post-processing fixes
629 * as per draft-ietf-ipsec-udp-encaps-06,
632 if (x->props.mode == XFRM_MODE_TRANSPORT)
633 skb->ip_summed = CHECKSUM_UNNECESSARY;
636 skb_pull_rcsum(skb, hlen);
637 if (x->props.mode == XFRM_MODE_TUNNEL)
638 skb_reset_transport_header(skb);
640 skb_set_transport_header(skb, -ihl);
642 /* RFC4303: Drop dummy packets without any error */
643 if (err == IPPROTO_NONE)
649 EXPORT_SYMBOL_GPL(esp_input_done2);
651 static void esp_input_done(struct crypto_async_request *base, int err)
653 struct sk_buff *skb = base->data;
655 xfrm_input_resume(skb, esp_input_done2(skb, err));
658 static void esp_input_restore_header(struct sk_buff *skb)
660 esp_restore_header(skb, 0);
664 static void esp_input_set_header(struct sk_buff *skb, __be32 *seqhi)
666 struct xfrm_state *x = xfrm_input_state(skb);
667 struct ip_esp_hdr *esph;
669 /* For ESN we move the header forward by 4 bytes to
670 * accomodate the high bits. We will move it back after
673 if ((x->props.flags & XFRM_STATE_ESN)) {
674 esph = skb_push(skb, 4);
676 esph->spi = esph->seq_no;
677 esph->seq_no = XFRM_SKB_CB(skb)->seq.input.hi;
681 static void esp_input_done_esn(struct crypto_async_request *base, int err)
683 struct sk_buff *skb = base->data;
685 esp_input_restore_header(skb);
686 esp_input_done(base, err);
690 * Note: detecting truncated vs. non-truncated authentication data is very
691 * expensive, so we only support truncated data, which is the recommended
694 static int esp_input(struct xfrm_state *x, struct sk_buff *skb)
696 struct ip_esp_hdr *esph;
697 struct crypto_aead *aead = x->data;
698 struct aead_request *req;
699 struct sk_buff *trailer;
700 int ivlen = crypto_aead_ivsize(aead);
701 int elen = skb->len - sizeof(*esph) - ivlen;
708 struct scatterlist *sg;
711 if (!pskb_may_pull(skb, sizeof(*esph) + ivlen))
717 assoclen = sizeof(*esph);
720 if (x->props.flags & XFRM_STATE_ESN) {
721 seqhilen += sizeof(__be32);
722 assoclen += seqhilen;
725 if (!skb_cloned(skb)) {
726 if (!skb_is_nonlinear(skb)) {
730 } else if (!skb_has_frag_list(skb)) {
731 nfrags = skb_shinfo(skb)->nr_frags;
738 err = skb_cow_data(skb, 0, &trailer);
746 tmp = esp_alloc_tmp(aead, nfrags, seqhilen);
750 ESP_SKB_CB(skb)->tmp = tmp;
751 seqhi = esp_tmp_extra(tmp);
752 iv = esp_tmp_iv(aead, tmp, seqhilen);
753 req = esp_tmp_req(aead, iv);
754 sg = esp_req_sg(aead, req);
756 esp_input_set_header(skb, seqhi);
758 sg_init_table(sg, nfrags);
759 err = skb_to_sgvec(skb, sg, 0, skb->len);
760 if (unlikely(err < 0)) {
765 skb->ip_summed = CHECKSUM_NONE;
767 if ((x->props.flags & XFRM_STATE_ESN))
768 aead_request_set_callback(req, 0, esp_input_done_esn, skb);
770 aead_request_set_callback(req, 0, esp_input_done, skb);
772 aead_request_set_crypt(req, sg, sg, elen + ivlen, iv);
773 aead_request_set_ad(req, assoclen);
775 err = crypto_aead_decrypt(req);
776 if (err == -EINPROGRESS)
779 if ((x->props.flags & XFRM_STATE_ESN))
780 esp_input_restore_header(skb);
782 err = esp_input_done2(skb, err);
788 static u32 esp4_get_mtu(struct xfrm_state *x, int mtu)
790 struct crypto_aead *aead = x->data;
791 u32 blksize = ALIGN(crypto_aead_blocksize(aead), 4);
792 unsigned int net_adj;
794 switch (x->props.mode) {
795 case XFRM_MODE_TRANSPORT:
797 net_adj = sizeof(struct iphdr);
799 case XFRM_MODE_TUNNEL:
806 return ((mtu - x->props.header_len - crypto_aead_authsize(aead) -
807 net_adj) & ~(blksize - 1)) + net_adj - 2;
810 static int esp4_err(struct sk_buff *skb, u32 info)
812 struct net *net = dev_net(skb->dev);
813 const struct iphdr *iph = (const struct iphdr *)skb->data;
814 struct ip_esp_hdr *esph = (struct ip_esp_hdr *)(skb->data+(iph->ihl<<2));
815 struct xfrm_state *x;
817 switch (icmp_hdr(skb)->type) {
818 case ICMP_DEST_UNREACH:
819 if (icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
827 x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr,
828 esph->spi, IPPROTO_ESP, AF_INET);
832 if (icmp_hdr(skb)->type == ICMP_DEST_UNREACH)
833 ipv4_update_pmtu(skb, net, info, 0, 0, IPPROTO_ESP, 0);
835 ipv4_redirect(skb, net, 0, 0, IPPROTO_ESP, 0);
841 static void esp_destroy(struct xfrm_state *x)
843 struct crypto_aead *aead = x->data;
848 crypto_free_aead(aead);
851 static int esp_init_aead(struct xfrm_state *x)
853 char aead_name[CRYPTO_MAX_ALG_NAME];
854 struct crypto_aead *aead;
858 if (snprintf(aead_name, CRYPTO_MAX_ALG_NAME, "%s(%s)",
859 x->geniv, x->aead->alg_name) >= CRYPTO_MAX_ALG_NAME)
862 aead = crypto_alloc_aead(aead_name, 0, 0);
869 err = crypto_aead_setkey(aead, x->aead->alg_key,
870 (x->aead->alg_key_len + 7) / 8);
874 err = crypto_aead_setauthsize(aead, x->aead->alg_icv_len / 8);
882 static int esp_init_authenc(struct xfrm_state *x)
884 struct crypto_aead *aead;
885 struct crypto_authenc_key_param *param;
889 char authenc_name[CRYPTO_MAX_ALG_NAME];
899 if ((x->props.flags & XFRM_STATE_ESN)) {
900 if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME,
901 "%s%sauthencesn(%s,%s)%s",
902 x->geniv ?: "", x->geniv ? "(" : "",
903 x->aalg ? x->aalg->alg_name : "digest_null",
905 x->geniv ? ")" : "") >= CRYPTO_MAX_ALG_NAME)
908 if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME,
909 "%s%sauthenc(%s,%s)%s",
910 x->geniv ?: "", x->geniv ? "(" : "",
911 x->aalg ? x->aalg->alg_name : "digest_null",
913 x->geniv ? ")" : "") >= CRYPTO_MAX_ALG_NAME)
917 aead = crypto_alloc_aead(authenc_name, 0, 0);
924 keylen = (x->aalg ? (x->aalg->alg_key_len + 7) / 8 : 0) +
925 (x->ealg->alg_key_len + 7) / 8 + RTA_SPACE(sizeof(*param));
927 key = kmalloc(keylen, GFP_KERNEL);
933 rta->rta_type = CRYPTO_AUTHENC_KEYA_PARAM;
934 rta->rta_len = RTA_LENGTH(sizeof(*param));
935 param = RTA_DATA(rta);
936 p += RTA_SPACE(sizeof(*param));
939 struct xfrm_algo_desc *aalg_desc;
941 memcpy(p, x->aalg->alg_key, (x->aalg->alg_key_len + 7) / 8);
942 p += (x->aalg->alg_key_len + 7) / 8;
944 aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
948 if (aalg_desc->uinfo.auth.icv_fullbits / 8 !=
949 crypto_aead_authsize(aead)) {
950 pr_info("ESP: %s digestsize %u != %hu\n",
952 crypto_aead_authsize(aead),
953 aalg_desc->uinfo.auth.icv_fullbits / 8);
957 err = crypto_aead_setauthsize(
958 aead, x->aalg->alg_trunc_len / 8);
963 param->enckeylen = cpu_to_be32((x->ealg->alg_key_len + 7) / 8);
964 memcpy(p, x->ealg->alg_key, (x->ealg->alg_key_len + 7) / 8);
966 err = crypto_aead_setkey(aead, key, keylen);
975 static int esp_init_state(struct xfrm_state *x)
977 struct crypto_aead *aead;
984 err = esp_init_aead(x);
986 err = esp_init_authenc(x);
993 x->props.header_len = sizeof(struct ip_esp_hdr) +
994 crypto_aead_ivsize(aead);
995 if (x->props.mode == XFRM_MODE_TUNNEL)
996 x->props.header_len += sizeof(struct iphdr);
997 else if (x->props.mode == XFRM_MODE_BEET && x->sel.family != AF_INET6)
998 x->props.header_len += IPV4_BEET_PHMAXLEN;
1000 struct xfrm_encap_tmpl *encap = x->encap;
1002 switch (encap->encap_type) {
1006 case UDP_ENCAP_ESPINUDP:
1007 x->props.header_len += sizeof(struct udphdr);
1009 case UDP_ENCAP_ESPINUDP_NON_IKE:
1010 x->props.header_len += sizeof(struct udphdr) + 2 * sizeof(u32);
1015 align = ALIGN(crypto_aead_blocksize(aead), 4);
1016 x->props.trailer_len = align + 1 + crypto_aead_authsize(aead);
1022 static int esp4_rcv_cb(struct sk_buff *skb, int err)
1027 static const struct xfrm_type esp_type =
1029 .description = "ESP4",
1030 .owner = THIS_MODULE,
1031 .proto = IPPROTO_ESP,
1032 .flags = XFRM_TYPE_REPLAY_PROT,
1033 .init_state = esp_init_state,
1034 .destructor = esp_destroy,
1035 .get_mtu = esp4_get_mtu,
1037 .output = esp_output,
1040 static struct xfrm4_protocol esp4_protocol = {
1041 .handler = xfrm4_rcv,
1042 .input_handler = xfrm_input,
1043 .cb_handler = esp4_rcv_cb,
1044 .err_handler = esp4_err,
1048 static int __init esp4_init(void)
1050 if (xfrm_register_type(&esp_type, AF_INET) < 0) {
1051 pr_info("%s: can't add xfrm type\n", __func__);
1054 if (xfrm4_protocol_register(&esp4_protocol, IPPROTO_ESP) < 0) {
1055 pr_info("%s: can't add protocol\n", __func__);
1056 xfrm_unregister_type(&esp_type, AF_INET);
1062 static void __exit esp4_fini(void)
1064 if (xfrm4_protocol_deregister(&esp4_protocol, IPPROTO_ESP) < 0)
1065 pr_info("%s: can't remove protocol\n", __func__);
1066 if (xfrm_unregister_type(&esp_type, AF_INET) < 0)
1067 pr_info("%s: can't remove xfrm type\n", __func__);
1070 module_init(esp4_init);
1071 module_exit(esp4_fini);
1072 MODULE_LICENSE("GPL");
1073 MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_ESP);