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;
125 struct dst_entry *dst = skb_dst(skb);
126 struct xfrm_state *x = dst->xfrm;
128 tmp = ESP_SKB_CB(skb)->tmp;
129 esp_ssg_unref(x, tmp);
131 xfrm_output_resume(skb, err);
134 /* Move ESP header back into place. */
135 static void esp_restore_header(struct sk_buff *skb, unsigned int offset)
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);
141 esph->seq_no = esph->spi;
145 static void esp_output_restore_header(struct sk_buff *skb)
147 void *tmp = ESP_SKB_CB(skb)->tmp;
148 struct esp_output_extra *extra = esp_tmp_extra(tmp);
150 esp_restore_header(skb, skb_transport_offset(skb) + extra->esphoff -
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)
159 /* For ESN we move the header forward by 4 bytes to
160 * accomodate the high bits. We will move it back after
163 if ((x->props.flags & XFRM_STATE_ESN)) {
165 struct xfrm_offload *xo = xfrm_offload(skb);
170 seqhi = XFRM_SKB_CB(skb)->seq.output.hi;
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);
179 esph->spi = x->id.spi;
184 static void esp_output_done_esn(struct crypto_async_request *base, int err)
186 struct sk_buff *skb = base->data;
188 esp_output_restore_header(skb);
189 esp_output_done(base, err);
192 static void esp_output_fill_trailer(u8 *tail, int tfclen, int plen, __u8 proto)
194 /* Fill padding... */
196 memset(tail, 0, tfclen);
201 for (i = 0; i < plen - 2; i++)
204 tail[plen - 2] = plen - 2;
205 tail[plen - 1] = proto;
208 static int esp_output_udp_encap(struct xfrm_state *x, struct sk_buff *skb, struct esp_info *esp)
214 struct xfrm_encap_tmpl *encap = x->encap;
215 struct ip_esp_hdr *esph = esp->esph;
218 spin_lock_bh(&x->lock);
219 sport = encap->encap_sport;
220 dport = encap->encap_dport;
221 encap_type = encap->encap_type;
222 spin_unlock_bh(&x->lock);
224 len = skb->len + esp->tailen - skb_transport_offset(skb);
225 if (len + sizeof(struct iphdr) >= IP_MAX_MTU)
228 uh = (struct udphdr *)esph;
231 uh->len = htons(len);
234 switch (encap_type) {
236 case UDP_ENCAP_ESPINUDP:
237 esph = (struct ip_esp_hdr *)(uh + 1);
239 case UDP_ENCAP_ESPINUDP_NON_IKE:
240 udpdata32 = (__be32 *)(uh + 1);
241 udpdata32[0] = udpdata32[1] = 0;
242 esph = (struct ip_esp_hdr *)(udpdata32 + 2);
246 *skb_mac_header(skb) = IPPROTO_UDP;
252 int esp_output_head(struct xfrm_state *x, struct sk_buff *skb, struct esp_info *esp)
258 struct sk_buff *trailer;
259 int tailen = esp->tailen;
261 /* this is non-NULL only with UDP Encapsulation */
263 int err = esp_output_udp_encap(x, skb, esp);
269 if (ALIGN(tailen, L1_CACHE_BYTES) > PAGE_SIZE ||
270 ALIGN(skb->data_len, L1_CACHE_BYTES) > PAGE_SIZE)
273 if (!skb_cloned(skb)) {
274 if (tailen <= skb_tailroom(skb)) {
277 tail = skb_tail_pointer(trailer);
280 } else if ((skb_shinfo(skb)->nr_frags < MAX_SKB_FRAGS)
281 && !skb_has_frag_list(skb)) {
283 struct sock *sk = skb->sk;
284 struct page_frag *pfrag = &x->xfrag;
286 esp->inplace = false;
288 allocsize = ALIGN(tailen, L1_CACHE_BYTES);
290 spin_lock_bh(&x->lock);
292 if (unlikely(!skb_page_frag_refill(allocsize, pfrag, GFP_ATOMIC))) {
293 spin_unlock_bh(&x->lock);
300 tail = page_address(page) + pfrag->offset;
302 esp_output_fill_trailer(tail, esp->tfclen, esp->plen, esp->proto);
304 nfrags = skb_shinfo(skb)->nr_frags;
306 __skb_fill_page_desc(skb, nfrags, page, pfrag->offset,
308 skb_shinfo(skb)->nr_frags = ++nfrags;
310 pfrag->offset = pfrag->offset + allocsize;
312 spin_unlock_bh(&x->lock);
317 skb->data_len += tailen;
318 skb->truesize += tailen;
319 if (sk && sk_fullsock(sk))
320 refcount_add(tailen, &sk->sk_wmem_alloc);
327 esph_offset = (unsigned char *)esp->esph - skb_transport_header(skb);
329 nfrags = skb_cow_data(skb, tailen, &trailer);
332 tail = skb_tail_pointer(trailer);
333 esp->esph = (struct ip_esp_hdr *)(skb_transport_header(skb) + esph_offset);
336 esp_output_fill_trailer(tail, esp->tfclen, esp->plen, esp->proto);
337 pskb_put(skb, trailer, tailen);
342 EXPORT_SYMBOL_GPL(esp_output_head);
344 int esp_output_tail(struct xfrm_state *x, struct sk_buff *skb, struct esp_info *esp)
353 struct ip_esp_hdr *esph;
354 struct crypto_aead *aead;
355 struct aead_request *req;
356 struct scatterlist *sg, *dsg;
357 struct esp_output_extra *extra;
360 assoclen = sizeof(struct ip_esp_hdr);
363 if (x->props.flags & XFRM_STATE_ESN) {
364 extralen += sizeof(*extra);
365 assoclen += sizeof(__be32);
369 alen = crypto_aead_authsize(aead);
370 ivlen = crypto_aead_ivsize(aead);
372 tmp = esp_alloc_tmp(aead, esp->nfrags + 2, extralen);
376 extra = esp_tmp_extra(tmp);
377 iv = esp_tmp_iv(aead, tmp, extralen);
378 req = esp_tmp_req(aead, iv);
379 sg = esp_req_sg(aead, req);
384 dsg = &sg[esp->nfrags];
386 esph = esp_output_set_extra(skb, x, esp->esph, extra);
389 sg_init_table(sg, esp->nfrags);
390 err = skb_to_sgvec(skb, sg,
391 (unsigned char *)esph - skb->data,
392 assoclen + ivlen + esp->clen + alen);
393 if (unlikely(err < 0))
398 struct page_frag *pfrag = &x->xfrag;
400 allocsize = ALIGN(skb->data_len, L1_CACHE_BYTES);
402 spin_lock_bh(&x->lock);
403 if (unlikely(!skb_page_frag_refill(allocsize, pfrag, GFP_ATOMIC))) {
404 spin_unlock_bh(&x->lock);
408 skb_shinfo(skb)->nr_frags = 1;
412 /* replace page frags in skb with new page */
413 __skb_fill_page_desc(skb, 0, page, pfrag->offset, skb->data_len);
414 pfrag->offset = pfrag->offset + allocsize;
415 spin_unlock_bh(&x->lock);
417 sg_init_table(dsg, skb_shinfo(skb)->nr_frags + 1);
418 err = skb_to_sgvec(skb, dsg,
419 (unsigned char *)esph - skb->data,
420 assoclen + ivlen + esp->clen + alen);
421 if (unlikely(err < 0))
425 if ((x->props.flags & XFRM_STATE_ESN))
426 aead_request_set_callback(req, 0, esp_output_done_esn, skb);
428 aead_request_set_callback(req, 0, esp_output_done, skb);
430 aead_request_set_crypt(req, sg, dsg, ivlen + esp->clen, iv);
431 aead_request_set_ad(req, assoclen);
433 memset(iv, 0, ivlen);
434 memcpy(iv + ivlen - min(ivlen, 8), (u8 *)&esp->seqno + 8 - min(ivlen, 8),
437 ESP_SKB_CB(skb)->tmp = tmp;
438 err = crypto_aead_encrypt(req);
449 if ((x->props.flags & XFRM_STATE_ESN))
450 esp_output_restore_header(skb);
454 esp_ssg_unref(x, tmp);
461 EXPORT_SYMBOL_GPL(esp_output_tail);
463 static int esp_output(struct xfrm_state *x, struct sk_buff *skb)
467 struct ip_esp_hdr *esph;
468 struct crypto_aead *aead;
473 esp.proto = *skb_mac_header(skb);
474 *skb_mac_header(skb) = IPPROTO_ESP;
476 /* skb is pure payload to encrypt */
479 alen = crypto_aead_authsize(aead);
483 struct xfrm_dst *dst = (struct xfrm_dst *)skb_dst(skb);
486 padto = min(x->tfcpad, esp4_get_mtu(x, dst->child_mtu_cached));
487 if (skb->len < padto)
488 esp.tfclen = padto - skb->len;
490 blksize = ALIGN(crypto_aead_blocksize(aead), 4);
491 esp.clen = ALIGN(skb->len + 2 + esp.tfclen, blksize);
492 esp.plen = esp.clen - skb->len - esp.tfclen;
493 esp.tailen = esp.tfclen + esp.plen + alen;
495 esp.esph = ip_esp_hdr(skb);
497 esp.nfrags = esp_output_head(x, skb, &esp);
502 esph->spi = x->id.spi;
504 esph->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output.low);
505 esp.seqno = cpu_to_be64(XFRM_SKB_CB(skb)->seq.output.low +
506 ((u64)XFRM_SKB_CB(skb)->seq.output.hi << 32));
508 skb_push(skb, -skb_network_offset(skb));
510 return esp_output_tail(x, skb, &esp);
513 static inline int esp_remove_trailer(struct sk_buff *skb)
515 struct xfrm_state *x = xfrm_input_state(skb);
516 struct xfrm_offload *xo = xfrm_offload(skb);
517 struct crypto_aead *aead = x->data;
518 int alen, hlen, elen;
524 alen = crypto_aead_authsize(aead);
525 hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead);
526 elen = skb->len - hlen;
528 if (xo && (xo->flags & XFRM_ESP_NO_TRAILER)) {
533 if (skb_copy_bits(skb, skb->len - alen - 2, nexthdr, 2))
538 if (padlen + 2 + alen >= elen) {
539 net_dbg_ratelimited("ipsec esp packet is garbage padlen=%d, elen=%d\n",
540 padlen + 2, elen - alen);
544 trimlen = alen + padlen + 2;
545 if (skb->ip_summed == CHECKSUM_COMPLETE) {
546 csumdiff = skb_checksum(skb, skb->len - trimlen, trimlen, 0);
547 skb->csum = csum_block_sub(skb->csum, csumdiff,
550 pskb_trim(skb, skb->len - trimlen);
558 int esp_input_done2(struct sk_buff *skb, int err)
560 const struct iphdr *iph;
561 struct xfrm_state *x = xfrm_input_state(skb);
562 struct xfrm_offload *xo = xfrm_offload(skb);
563 struct crypto_aead *aead = x->data;
564 int hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead);
567 if (!xo || (xo && !(xo->flags & CRYPTO_DONE)))
568 kfree(ESP_SKB_CB(skb)->tmp);
573 err = esp_remove_trailer(skb);
574 if (unlikely(err < 0))
581 struct xfrm_encap_tmpl *encap = x->encap;
582 struct udphdr *uh = (void *)(skb_network_header(skb) + ihl);
585 * 1) if the NAT-T peer's IP or port changed then
586 * advertize the change to the keying daemon.
587 * This is an inbound SA, so just compare
590 if (iph->saddr != x->props.saddr.a4 ||
591 uh->source != encap->encap_sport) {
592 xfrm_address_t ipaddr;
594 ipaddr.a4 = iph->saddr;
595 km_new_mapping(x, &ipaddr, uh->source);
597 /* XXX: perhaps add an extra
598 * policy check here, to see
599 * if we should allow or
600 * reject a packet from a
607 * 2) ignore UDP/TCP checksums in case
608 * of NAT-T in Transport Mode, or
609 * perform other post-processing fixes
610 * as per draft-ietf-ipsec-udp-encaps-06,
613 if (x->props.mode == XFRM_MODE_TRANSPORT)
614 skb->ip_summed = CHECKSUM_UNNECESSARY;
617 skb_pull_rcsum(skb, hlen);
618 if (x->props.mode == XFRM_MODE_TUNNEL)
619 skb_reset_transport_header(skb);
621 skb_set_transport_header(skb, -ihl);
623 /* RFC4303: Drop dummy packets without any error */
624 if (err == IPPROTO_NONE)
630 EXPORT_SYMBOL_GPL(esp_input_done2);
632 static void esp_input_done(struct crypto_async_request *base, int err)
634 struct sk_buff *skb = base->data;
636 xfrm_input_resume(skb, esp_input_done2(skb, err));
639 static void esp_input_restore_header(struct sk_buff *skb)
641 esp_restore_header(skb, 0);
645 static void esp_input_set_header(struct sk_buff *skb, __be32 *seqhi)
647 struct xfrm_state *x = xfrm_input_state(skb);
648 struct ip_esp_hdr *esph = (struct ip_esp_hdr *)skb->data;
650 /* For ESN we move the header forward by 4 bytes to
651 * accomodate the high bits. We will move it back after
654 if ((x->props.flags & XFRM_STATE_ESN)) {
655 esph = skb_push(skb, 4);
657 esph->spi = esph->seq_no;
658 esph->seq_no = XFRM_SKB_CB(skb)->seq.input.hi;
662 static void esp_input_done_esn(struct crypto_async_request *base, int err)
664 struct sk_buff *skb = base->data;
666 esp_input_restore_header(skb);
667 esp_input_done(base, err);
671 * Note: detecting truncated vs. non-truncated authentication data is very
672 * expensive, so we only support truncated data, which is the recommended
675 static int esp_input(struct xfrm_state *x, struct sk_buff *skb)
677 struct ip_esp_hdr *esph;
678 struct crypto_aead *aead = x->data;
679 struct aead_request *req;
680 struct sk_buff *trailer;
681 int ivlen = crypto_aead_ivsize(aead);
682 int elen = skb->len - sizeof(*esph) - ivlen;
689 struct scatterlist *sg;
692 if (!pskb_may_pull(skb, sizeof(*esph) + ivlen))
698 assoclen = sizeof(*esph);
701 if (x->props.flags & XFRM_STATE_ESN) {
702 seqhilen += sizeof(__be32);
703 assoclen += seqhilen;
706 if (!skb_cloned(skb)) {
707 if (!skb_is_nonlinear(skb)) {
711 } else if (!skb_has_frag_list(skb)) {
712 nfrags = skb_shinfo(skb)->nr_frags;
719 err = skb_cow_data(skb, 0, &trailer);
727 tmp = esp_alloc_tmp(aead, nfrags, seqhilen);
731 ESP_SKB_CB(skb)->tmp = tmp;
732 seqhi = esp_tmp_extra(tmp);
733 iv = esp_tmp_iv(aead, tmp, seqhilen);
734 req = esp_tmp_req(aead, iv);
735 sg = esp_req_sg(aead, req);
737 esp_input_set_header(skb, seqhi);
739 sg_init_table(sg, nfrags);
740 err = skb_to_sgvec(skb, sg, 0, skb->len);
741 if (unlikely(err < 0)) {
746 skb->ip_summed = CHECKSUM_NONE;
748 if ((x->props.flags & XFRM_STATE_ESN))
749 aead_request_set_callback(req, 0, esp_input_done_esn, skb);
751 aead_request_set_callback(req, 0, esp_input_done, skb);
753 aead_request_set_crypt(req, sg, sg, elen + ivlen, iv);
754 aead_request_set_ad(req, assoclen);
756 err = crypto_aead_decrypt(req);
757 if (err == -EINPROGRESS)
760 if ((x->props.flags & XFRM_STATE_ESN))
761 esp_input_restore_header(skb);
763 err = esp_input_done2(skb, err);
769 static u32 esp4_get_mtu(struct xfrm_state *x, int mtu)
771 struct crypto_aead *aead = x->data;
772 u32 blksize = ALIGN(crypto_aead_blocksize(aead), 4);
773 unsigned int net_adj;
775 switch (x->props.mode) {
776 case XFRM_MODE_TRANSPORT:
778 net_adj = sizeof(struct iphdr);
780 case XFRM_MODE_TUNNEL:
787 return ((mtu - x->props.header_len - crypto_aead_authsize(aead) -
788 net_adj) & ~(blksize - 1)) + net_adj - 2;
791 static int esp4_err(struct sk_buff *skb, u32 info)
793 struct net *net = dev_net(skb->dev);
794 const struct iphdr *iph = (const struct iphdr *)skb->data;
795 struct ip_esp_hdr *esph = (struct ip_esp_hdr *)(skb->data+(iph->ihl<<2));
796 struct xfrm_state *x;
798 switch (icmp_hdr(skb)->type) {
799 case ICMP_DEST_UNREACH:
800 if (icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
808 x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr,
809 esph->spi, IPPROTO_ESP, AF_INET);
813 if (icmp_hdr(skb)->type == ICMP_DEST_UNREACH)
814 ipv4_update_pmtu(skb, net, info, 0, 0, IPPROTO_ESP, 0);
816 ipv4_redirect(skb, net, 0, 0, IPPROTO_ESP, 0);
822 static void esp_destroy(struct xfrm_state *x)
824 struct crypto_aead *aead = x->data;
829 crypto_free_aead(aead);
832 static int esp_init_aead(struct xfrm_state *x)
834 char aead_name[CRYPTO_MAX_ALG_NAME];
835 struct crypto_aead *aead;
840 if (snprintf(aead_name, CRYPTO_MAX_ALG_NAME, "%s(%s)",
841 x->geniv, x->aead->alg_name) >= CRYPTO_MAX_ALG_NAME)
844 if (x->xso.offload_handle)
845 mask |= CRYPTO_ALG_ASYNC;
847 aead = crypto_alloc_aead(aead_name, 0, mask);
854 err = crypto_aead_setkey(aead, x->aead->alg_key,
855 (x->aead->alg_key_len + 7) / 8);
859 err = crypto_aead_setauthsize(aead, x->aead->alg_icv_len / 8);
867 static int esp_init_authenc(struct xfrm_state *x)
869 struct crypto_aead *aead;
870 struct crypto_authenc_key_param *param;
874 char authenc_name[CRYPTO_MAX_ALG_NAME];
885 if ((x->props.flags & XFRM_STATE_ESN)) {
886 if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME,
887 "%s%sauthencesn(%s,%s)%s",
888 x->geniv ?: "", x->geniv ? "(" : "",
889 x->aalg ? x->aalg->alg_name : "digest_null",
891 x->geniv ? ")" : "") >= CRYPTO_MAX_ALG_NAME)
894 if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME,
895 "%s%sauthenc(%s,%s)%s",
896 x->geniv ?: "", x->geniv ? "(" : "",
897 x->aalg ? x->aalg->alg_name : "digest_null",
899 x->geniv ? ")" : "") >= CRYPTO_MAX_ALG_NAME)
903 if (x->xso.offload_handle)
904 mask |= CRYPTO_ALG_ASYNC;
906 aead = crypto_alloc_aead(authenc_name, 0, mask);
913 keylen = (x->aalg ? (x->aalg->alg_key_len + 7) / 8 : 0) +
914 (x->ealg->alg_key_len + 7) / 8 + RTA_SPACE(sizeof(*param));
916 key = kmalloc(keylen, GFP_KERNEL);
922 rta->rta_type = CRYPTO_AUTHENC_KEYA_PARAM;
923 rta->rta_len = RTA_LENGTH(sizeof(*param));
924 param = RTA_DATA(rta);
925 p += RTA_SPACE(sizeof(*param));
928 struct xfrm_algo_desc *aalg_desc;
930 memcpy(p, x->aalg->alg_key, (x->aalg->alg_key_len + 7) / 8);
931 p += (x->aalg->alg_key_len + 7) / 8;
933 aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
937 if (aalg_desc->uinfo.auth.icv_fullbits / 8 !=
938 crypto_aead_authsize(aead)) {
939 pr_info("ESP: %s digestsize %u != %hu\n",
941 crypto_aead_authsize(aead),
942 aalg_desc->uinfo.auth.icv_fullbits / 8);
946 err = crypto_aead_setauthsize(
947 aead, x->aalg->alg_trunc_len / 8);
952 param->enckeylen = cpu_to_be32((x->ealg->alg_key_len + 7) / 8);
953 memcpy(p, x->ealg->alg_key, (x->ealg->alg_key_len + 7) / 8);
955 err = crypto_aead_setkey(aead, key, keylen);
964 static int esp_init_state(struct xfrm_state *x)
966 struct crypto_aead *aead;
973 err = esp_init_aead(x);
975 err = esp_init_authenc(x);
982 x->props.header_len = sizeof(struct ip_esp_hdr) +
983 crypto_aead_ivsize(aead);
984 if (x->props.mode == XFRM_MODE_TUNNEL)
985 x->props.header_len += sizeof(struct iphdr);
986 else if (x->props.mode == XFRM_MODE_BEET && x->sel.family != AF_INET6)
987 x->props.header_len += IPV4_BEET_PHMAXLEN;
989 struct xfrm_encap_tmpl *encap = x->encap;
991 switch (encap->encap_type) {
994 case UDP_ENCAP_ESPINUDP:
995 x->props.header_len += sizeof(struct udphdr);
997 case UDP_ENCAP_ESPINUDP_NON_IKE:
998 x->props.header_len += sizeof(struct udphdr) + 2 * sizeof(u32);
1003 align = ALIGN(crypto_aead_blocksize(aead), 4);
1004 x->props.trailer_len = align + 1 + crypto_aead_authsize(aead);
1010 static int esp4_rcv_cb(struct sk_buff *skb, int err)
1015 static const struct xfrm_type esp_type =
1017 .description = "ESP4",
1018 .owner = THIS_MODULE,
1019 .proto = IPPROTO_ESP,
1020 .flags = XFRM_TYPE_REPLAY_PROT,
1021 .init_state = esp_init_state,
1022 .destructor = esp_destroy,
1023 .get_mtu = esp4_get_mtu,
1025 .output = esp_output,
1028 static struct xfrm4_protocol esp4_protocol = {
1029 .handler = xfrm4_rcv,
1030 .input_handler = xfrm_input,
1031 .cb_handler = esp4_rcv_cb,
1032 .err_handler = esp4_err,
1036 static int __init esp4_init(void)
1038 if (xfrm_register_type(&esp_type, AF_INET) < 0) {
1039 pr_info("%s: can't add xfrm type\n", __func__);
1042 if (xfrm4_protocol_register(&esp4_protocol, IPPROTO_ESP) < 0) {
1043 pr_info("%s: can't add protocol\n", __func__);
1044 xfrm_unregister_type(&esp_type, AF_INET);
1050 static void __exit esp4_fini(void)
1052 if (xfrm4_protocol_deregister(&esp4_protocol, IPPROTO_ESP) < 0)
1053 pr_info("%s: can't remove protocol\n", __func__);
1054 if (xfrm_unregister_type(&esp_type, AF_INET) < 0)
1055 pr_info("%s: can't remove xfrm type\n", __func__);
1058 module_init(esp4_init);
1059 module_exit(esp4_fini);
1060 MODULE_LICENSE("GPL");
1061 MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_ESP);