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 struct sec_path *sp = skb_sec_path(skb);
131 x = sp->xvec[sp->len - 1];
133 x = skb_dst(skb)->xfrm;
136 tmp = ESP_SKB_CB(skb)->tmp;
137 esp_ssg_unref(x, tmp);
140 if (xo && (xo->flags & XFRM_DEV_RESUME)) {
142 XFRM_INC_STATS(xs_net(x), LINUX_MIB_XFRMOUTSTATEPROTOERROR);
147 skb_push(skb, skb->data - skb_mac_header(skb));
149 xfrm_dev_resume(skb);
151 xfrm_output_resume(skb, err);
155 /* Move ESP header back into place. */
156 static void esp_restore_header(struct sk_buff *skb, unsigned int offset)
158 struct ip_esp_hdr *esph = (void *)(skb->data + offset);
159 void *tmp = ESP_SKB_CB(skb)->tmp;
160 __be32 *seqhi = esp_tmp_extra(tmp);
162 esph->seq_no = esph->spi;
166 static void esp_output_restore_header(struct sk_buff *skb)
168 void *tmp = ESP_SKB_CB(skb)->tmp;
169 struct esp_output_extra *extra = esp_tmp_extra(tmp);
171 esp_restore_header(skb, skb_transport_offset(skb) + extra->esphoff -
175 static struct ip_esp_hdr *esp_output_set_extra(struct sk_buff *skb,
176 struct xfrm_state *x,
177 struct ip_esp_hdr *esph,
178 struct esp_output_extra *extra)
180 /* For ESN we move the header forward by 4 bytes to
181 * accomodate the high bits. We will move it back after
184 if ((x->props.flags & XFRM_STATE_ESN)) {
186 struct xfrm_offload *xo = xfrm_offload(skb);
191 seqhi = XFRM_SKB_CB(skb)->seq.output.hi;
193 extra->esphoff = (unsigned char *)esph -
194 skb_transport_header(skb);
195 esph = (struct ip_esp_hdr *)((unsigned char *)esph - 4);
196 extra->seqhi = esph->spi;
197 esph->seq_no = htonl(seqhi);
200 esph->spi = x->id.spi;
205 static void esp_output_done_esn(struct crypto_async_request *base, int err)
207 struct sk_buff *skb = base->data;
209 esp_output_restore_header(skb);
210 esp_output_done(base, err);
213 static void esp_output_fill_trailer(u8 *tail, int tfclen, int plen, __u8 proto)
215 /* Fill padding... */
217 memset(tail, 0, tfclen);
222 for (i = 0; i < plen - 2; i++)
225 tail[plen - 2] = plen - 2;
226 tail[plen - 1] = proto;
229 static void esp_output_udp_encap(struct xfrm_state *x, struct sk_buff *skb, struct esp_info *esp)
235 struct xfrm_encap_tmpl *encap = x->encap;
236 struct ip_esp_hdr *esph = esp->esph;
238 spin_lock_bh(&x->lock);
239 sport = encap->encap_sport;
240 dport = encap->encap_dport;
241 encap_type = encap->encap_type;
242 spin_unlock_bh(&x->lock);
244 uh = (struct udphdr *)esph;
247 uh->len = htons(skb->len + esp->tailen
248 - skb_transport_offset(skb));
251 switch (encap_type) {
253 case UDP_ENCAP_ESPINUDP:
254 esph = (struct ip_esp_hdr *)(uh + 1);
256 case UDP_ENCAP_ESPINUDP_NON_IKE:
257 udpdata32 = (__be32 *)(uh + 1);
258 udpdata32[0] = udpdata32[1] = 0;
259 esph = (struct ip_esp_hdr *)(udpdata32 + 2);
263 *skb_mac_header(skb) = IPPROTO_UDP;
267 int esp_output_head(struct xfrm_state *x, struct sk_buff *skb, struct esp_info *esp)
274 struct sk_buff *trailer;
275 int tailen = esp->tailen;
277 /* this is non-NULL only with UDP Encapsulation */
279 esp_output_udp_encap(x, skb, esp);
281 if (!skb_cloned(skb)) {
282 if (tailen <= skb_tailroom(skb)) {
285 tail = skb_tail_pointer(trailer);
288 } else if ((skb_shinfo(skb)->nr_frags < MAX_SKB_FRAGS)
289 && !skb_has_frag_list(skb)) {
291 struct sock *sk = skb->sk;
292 struct page_frag *pfrag = &x->xfrag;
294 esp->inplace = false;
296 allocsize = ALIGN(tailen, L1_CACHE_BYTES);
298 spin_lock_bh(&x->lock);
300 if (unlikely(!skb_page_frag_refill(allocsize, pfrag, GFP_ATOMIC))) {
301 spin_unlock_bh(&x->lock);
308 vaddr = kmap_atomic(page);
310 tail = vaddr + pfrag->offset;
312 esp_output_fill_trailer(tail, esp->tfclen, esp->plen, esp->proto);
314 kunmap_atomic(vaddr);
316 nfrags = skb_shinfo(skb)->nr_frags;
318 __skb_fill_page_desc(skb, nfrags, page, pfrag->offset,
320 skb_shinfo(skb)->nr_frags = ++nfrags;
322 pfrag->offset = pfrag->offset + allocsize;
324 spin_unlock_bh(&x->lock);
329 skb->data_len += tailen;
330 skb->truesize += tailen;
331 if (sk && sk_fullsock(sk))
332 refcount_add(tailen, &sk->sk_wmem_alloc);
339 esph_offset = (unsigned char *)esp->esph - skb_transport_header(skb);
341 nfrags = skb_cow_data(skb, tailen, &trailer);
344 tail = skb_tail_pointer(trailer);
345 esp->esph = (struct ip_esp_hdr *)(skb_transport_header(skb) + esph_offset);
348 esp_output_fill_trailer(tail, esp->tfclen, esp->plen, esp->proto);
349 pskb_put(skb, trailer, tailen);
354 EXPORT_SYMBOL_GPL(esp_output_head);
356 int esp_output_tail(struct xfrm_state *x, struct sk_buff *skb, struct esp_info *esp)
365 struct ip_esp_hdr *esph;
366 struct crypto_aead *aead;
367 struct aead_request *req;
368 struct scatterlist *sg, *dsg;
369 struct esp_output_extra *extra;
372 assoclen = sizeof(struct ip_esp_hdr);
375 if (x->props.flags & XFRM_STATE_ESN) {
376 extralen += sizeof(*extra);
377 assoclen += sizeof(__be32);
381 alen = crypto_aead_authsize(aead);
382 ivlen = crypto_aead_ivsize(aead);
384 tmp = esp_alloc_tmp(aead, esp->nfrags + 2, extralen);
388 extra = esp_tmp_extra(tmp);
389 iv = esp_tmp_iv(aead, tmp, extralen);
390 req = esp_tmp_req(aead, iv);
391 sg = esp_req_sg(aead, req);
396 dsg = &sg[esp->nfrags];
398 esph = esp_output_set_extra(skb, x, esp->esph, extra);
401 sg_init_table(sg, esp->nfrags);
402 err = skb_to_sgvec(skb, sg,
403 (unsigned char *)esph - skb->data,
404 assoclen + ivlen + esp->clen + alen);
405 if (unlikely(err < 0))
410 struct page_frag *pfrag = &x->xfrag;
412 allocsize = ALIGN(skb->data_len, L1_CACHE_BYTES);
414 spin_lock_bh(&x->lock);
415 if (unlikely(!skb_page_frag_refill(allocsize, pfrag, GFP_ATOMIC))) {
416 spin_unlock_bh(&x->lock);
420 skb_shinfo(skb)->nr_frags = 1;
424 /* replace page frags in skb with new page */
425 __skb_fill_page_desc(skb, 0, page, pfrag->offset, skb->data_len);
426 pfrag->offset = pfrag->offset + allocsize;
427 spin_unlock_bh(&x->lock);
429 sg_init_table(dsg, skb_shinfo(skb)->nr_frags + 1);
430 err = skb_to_sgvec(skb, dsg,
431 (unsigned char *)esph - skb->data,
432 assoclen + ivlen + esp->clen + alen);
433 if (unlikely(err < 0))
437 if ((x->props.flags & XFRM_STATE_ESN))
438 aead_request_set_callback(req, 0, esp_output_done_esn, skb);
440 aead_request_set_callback(req, 0, esp_output_done, skb);
442 aead_request_set_crypt(req, sg, dsg, ivlen + esp->clen, iv);
443 aead_request_set_ad(req, assoclen);
445 memset(iv, 0, ivlen);
446 memcpy(iv + ivlen - min(ivlen, 8), (u8 *)&esp->seqno + 8 - min(ivlen, 8),
449 ESP_SKB_CB(skb)->tmp = tmp;
450 err = crypto_aead_encrypt(req);
461 if ((x->props.flags & XFRM_STATE_ESN))
462 esp_output_restore_header(skb);
466 esp_ssg_unref(x, tmp);
473 EXPORT_SYMBOL_GPL(esp_output_tail);
475 static int esp_output(struct xfrm_state *x, struct sk_buff *skb)
479 struct ip_esp_hdr *esph;
480 struct crypto_aead *aead;
485 esp.proto = *skb_mac_header(skb);
486 *skb_mac_header(skb) = IPPROTO_ESP;
488 /* skb is pure payload to encrypt */
491 alen = crypto_aead_authsize(aead);
495 struct xfrm_dst *dst = (struct xfrm_dst *)skb_dst(skb);
498 padto = min(x->tfcpad, esp4_get_mtu(x, dst->child_mtu_cached));
499 if (skb->len < padto)
500 esp.tfclen = padto - skb->len;
502 blksize = ALIGN(crypto_aead_blocksize(aead), 4);
503 esp.clen = ALIGN(skb->len + 2 + esp.tfclen, blksize);
504 esp.plen = esp.clen - skb->len - esp.tfclen;
505 esp.tailen = esp.tfclen + esp.plen + alen;
507 esp.esph = ip_esp_hdr(skb);
509 esp.nfrags = esp_output_head(x, skb, &esp);
514 esph->spi = x->id.spi;
516 esph->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output.low);
517 esp.seqno = cpu_to_be64(XFRM_SKB_CB(skb)->seq.output.low +
518 ((u64)XFRM_SKB_CB(skb)->seq.output.hi << 32));
520 skb_push(skb, -skb_network_offset(skb));
522 return esp_output_tail(x, skb, &esp);
525 static inline int esp_remove_trailer(struct sk_buff *skb)
527 struct xfrm_state *x = xfrm_input_state(skb);
528 struct xfrm_offload *xo = xfrm_offload(skb);
529 struct crypto_aead *aead = x->data;
530 int alen, hlen, elen;
536 alen = crypto_aead_authsize(aead);
537 hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead);
538 elen = skb->len - hlen;
540 if (xo && (xo->flags & XFRM_ESP_NO_TRAILER)) {
545 if (skb_copy_bits(skb, skb->len - alen - 2, nexthdr, 2))
550 if (padlen + 2 + alen >= elen) {
551 net_dbg_ratelimited("ipsec esp packet is garbage padlen=%d, elen=%d\n",
552 padlen + 2, elen - alen);
556 trimlen = alen + padlen + 2;
557 if (skb->ip_summed == CHECKSUM_COMPLETE) {
558 csumdiff = skb_checksum(skb, skb->len - trimlen, trimlen, 0);
559 skb->csum = csum_block_sub(skb->csum, csumdiff,
562 pskb_trim(skb, skb->len - trimlen);
570 int esp_input_done2(struct sk_buff *skb, int err)
572 const struct iphdr *iph;
573 struct xfrm_state *x = xfrm_input_state(skb);
574 struct xfrm_offload *xo = xfrm_offload(skb);
575 struct crypto_aead *aead = x->data;
576 int hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead);
579 if (!xo || (xo && !(xo->flags & CRYPTO_DONE)))
580 kfree(ESP_SKB_CB(skb)->tmp);
585 err = esp_remove_trailer(skb);
586 if (unlikely(err < 0))
593 struct xfrm_encap_tmpl *encap = x->encap;
594 struct udphdr *uh = (void *)(skb_network_header(skb) + ihl);
597 * 1) if the NAT-T peer's IP or port changed then
598 * advertize the change to the keying daemon.
599 * This is an inbound SA, so just compare
602 if (iph->saddr != x->props.saddr.a4 ||
603 uh->source != encap->encap_sport) {
604 xfrm_address_t ipaddr;
606 ipaddr.a4 = iph->saddr;
607 km_new_mapping(x, &ipaddr, uh->source);
609 /* XXX: perhaps add an extra
610 * policy check here, to see
611 * if we should allow or
612 * reject a packet from a
619 * 2) ignore UDP/TCP checksums in case
620 * of NAT-T in Transport Mode, or
621 * perform other post-processing fixes
622 * as per draft-ietf-ipsec-udp-encaps-06,
625 if (x->props.mode == XFRM_MODE_TRANSPORT)
626 skb->ip_summed = CHECKSUM_UNNECESSARY;
629 skb_pull_rcsum(skb, hlen);
630 if (x->props.mode == XFRM_MODE_TUNNEL)
631 skb_reset_transport_header(skb);
633 skb_set_transport_header(skb, -ihl);
635 /* RFC4303: Drop dummy packets without any error */
636 if (err == IPPROTO_NONE)
642 EXPORT_SYMBOL_GPL(esp_input_done2);
644 static void esp_input_done(struct crypto_async_request *base, int err)
646 struct sk_buff *skb = base->data;
648 xfrm_input_resume(skb, esp_input_done2(skb, err));
651 static void esp_input_restore_header(struct sk_buff *skb)
653 esp_restore_header(skb, 0);
657 static void esp_input_set_header(struct sk_buff *skb, __be32 *seqhi)
659 struct xfrm_state *x = xfrm_input_state(skb);
660 struct ip_esp_hdr *esph;
662 /* For ESN we move the header forward by 4 bytes to
663 * accomodate the high bits. We will move it back after
666 if ((x->props.flags & XFRM_STATE_ESN)) {
667 esph = skb_push(skb, 4);
669 esph->spi = esph->seq_no;
670 esph->seq_no = XFRM_SKB_CB(skb)->seq.input.hi;
674 static void esp_input_done_esn(struct crypto_async_request *base, int err)
676 struct sk_buff *skb = base->data;
678 esp_input_restore_header(skb);
679 esp_input_done(base, err);
683 * Note: detecting truncated vs. non-truncated authentication data is very
684 * expensive, so we only support truncated data, which is the recommended
687 static int esp_input(struct xfrm_state *x, struct sk_buff *skb)
689 struct crypto_aead *aead = x->data;
690 struct aead_request *req;
691 struct sk_buff *trailer;
692 int ivlen = crypto_aead_ivsize(aead);
693 int elen = skb->len - sizeof(struct ip_esp_hdr) - ivlen;
700 struct scatterlist *sg;
703 if (!pskb_may_pull(skb, sizeof(struct ip_esp_hdr) + ivlen))
709 assoclen = sizeof(struct ip_esp_hdr);
712 if (x->props.flags & XFRM_STATE_ESN) {
713 seqhilen += sizeof(__be32);
714 assoclen += seqhilen;
717 if (!skb_cloned(skb)) {
718 if (!skb_is_nonlinear(skb)) {
722 } else if (!skb_has_frag_list(skb)) {
723 nfrags = skb_shinfo(skb)->nr_frags;
730 err = skb_cow_data(skb, 0, &trailer);
738 tmp = esp_alloc_tmp(aead, nfrags, seqhilen);
742 ESP_SKB_CB(skb)->tmp = tmp;
743 seqhi = esp_tmp_extra(tmp);
744 iv = esp_tmp_iv(aead, tmp, seqhilen);
745 req = esp_tmp_req(aead, iv);
746 sg = esp_req_sg(aead, req);
748 esp_input_set_header(skb, seqhi);
750 sg_init_table(sg, nfrags);
751 err = skb_to_sgvec(skb, sg, 0, skb->len);
752 if (unlikely(err < 0)) {
757 skb->ip_summed = CHECKSUM_NONE;
759 if ((x->props.flags & XFRM_STATE_ESN))
760 aead_request_set_callback(req, 0, esp_input_done_esn, skb);
762 aead_request_set_callback(req, 0, esp_input_done, skb);
764 aead_request_set_crypt(req, sg, sg, elen + ivlen, iv);
765 aead_request_set_ad(req, assoclen);
767 err = crypto_aead_decrypt(req);
768 if (err == -EINPROGRESS)
771 if ((x->props.flags & XFRM_STATE_ESN))
772 esp_input_restore_header(skb);
774 err = esp_input_done2(skb, err);
780 static u32 esp4_get_mtu(struct xfrm_state *x, int mtu)
782 struct crypto_aead *aead = x->data;
783 u32 blksize = ALIGN(crypto_aead_blocksize(aead), 4);
784 unsigned int net_adj;
786 switch (x->props.mode) {
787 case XFRM_MODE_TRANSPORT:
789 net_adj = sizeof(struct iphdr);
791 case XFRM_MODE_TUNNEL:
798 return ((mtu - x->props.header_len - crypto_aead_authsize(aead) -
799 net_adj) & ~(blksize - 1)) + net_adj - 2;
802 static int esp4_err(struct sk_buff *skb, u32 info)
804 struct net *net = dev_net(skb->dev);
805 const struct iphdr *iph = (const struct iphdr *)skb->data;
806 struct ip_esp_hdr *esph = (struct ip_esp_hdr *)(skb->data+(iph->ihl<<2));
807 struct xfrm_state *x;
809 switch (icmp_hdr(skb)->type) {
810 case ICMP_DEST_UNREACH:
811 if (icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
819 x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr,
820 esph->spi, IPPROTO_ESP, AF_INET);
824 if (icmp_hdr(skb)->type == ICMP_DEST_UNREACH)
825 ipv4_update_pmtu(skb, net, info, 0, IPPROTO_ESP);
827 ipv4_redirect(skb, net, 0, IPPROTO_ESP);
833 static void esp_destroy(struct xfrm_state *x)
835 struct crypto_aead *aead = x->data;
840 crypto_free_aead(aead);
843 static int esp_init_aead(struct xfrm_state *x)
845 char aead_name[CRYPTO_MAX_ALG_NAME];
846 struct crypto_aead *aead;
850 if (snprintf(aead_name, CRYPTO_MAX_ALG_NAME, "%s(%s)",
851 x->geniv, x->aead->alg_name) >= CRYPTO_MAX_ALG_NAME)
854 aead = crypto_alloc_aead(aead_name, 0, 0);
861 err = crypto_aead_setkey(aead, x->aead->alg_key,
862 (x->aead->alg_key_len + 7) / 8);
866 err = crypto_aead_setauthsize(aead, x->aead->alg_icv_len / 8);
874 static int esp_init_authenc(struct xfrm_state *x)
876 struct crypto_aead *aead;
877 struct crypto_authenc_key_param *param;
881 char authenc_name[CRYPTO_MAX_ALG_NAME];
891 if ((x->props.flags & XFRM_STATE_ESN)) {
892 if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME,
893 "%s%sauthencesn(%s,%s)%s",
894 x->geniv ?: "", x->geniv ? "(" : "",
895 x->aalg ? x->aalg->alg_name : "digest_null",
897 x->geniv ? ")" : "") >= CRYPTO_MAX_ALG_NAME)
900 if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME,
901 "%s%sauthenc(%s,%s)%s",
902 x->geniv ?: "", x->geniv ? "(" : "",
903 x->aalg ? x->aalg->alg_name : "digest_null",
905 x->geniv ? ")" : "") >= CRYPTO_MAX_ALG_NAME)
909 aead = crypto_alloc_aead(authenc_name, 0, 0);
916 keylen = (x->aalg ? (x->aalg->alg_key_len + 7) / 8 : 0) +
917 (x->ealg->alg_key_len + 7) / 8 + RTA_SPACE(sizeof(*param));
919 key = kmalloc(keylen, GFP_KERNEL);
925 rta->rta_type = CRYPTO_AUTHENC_KEYA_PARAM;
926 rta->rta_len = RTA_LENGTH(sizeof(*param));
927 param = RTA_DATA(rta);
928 p += RTA_SPACE(sizeof(*param));
931 struct xfrm_algo_desc *aalg_desc;
933 memcpy(p, x->aalg->alg_key, (x->aalg->alg_key_len + 7) / 8);
934 p += (x->aalg->alg_key_len + 7) / 8;
936 aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
940 if (aalg_desc->uinfo.auth.icv_fullbits / 8 !=
941 crypto_aead_authsize(aead)) {
942 pr_info("ESP: %s digestsize %u != %hu\n",
944 crypto_aead_authsize(aead),
945 aalg_desc->uinfo.auth.icv_fullbits / 8);
949 err = crypto_aead_setauthsize(
950 aead, x->aalg->alg_trunc_len / 8);
955 param->enckeylen = cpu_to_be32((x->ealg->alg_key_len + 7) / 8);
956 memcpy(p, x->ealg->alg_key, (x->ealg->alg_key_len + 7) / 8);
958 err = crypto_aead_setkey(aead, key, keylen);
967 static int esp_init_state(struct xfrm_state *x)
969 struct crypto_aead *aead;
976 err = esp_init_aead(x);
978 err = esp_init_authenc(x);
985 x->props.header_len = sizeof(struct ip_esp_hdr) +
986 crypto_aead_ivsize(aead);
987 if (x->props.mode == XFRM_MODE_TUNNEL)
988 x->props.header_len += sizeof(struct iphdr);
989 else if (x->props.mode == XFRM_MODE_BEET && x->sel.family != AF_INET6)
990 x->props.header_len += IPV4_BEET_PHMAXLEN;
992 struct xfrm_encap_tmpl *encap = x->encap;
994 switch (encap->encap_type) {
998 case UDP_ENCAP_ESPINUDP:
999 x->props.header_len += sizeof(struct udphdr);
1001 case UDP_ENCAP_ESPINUDP_NON_IKE:
1002 x->props.header_len += sizeof(struct udphdr) + 2 * sizeof(u32);
1007 align = ALIGN(crypto_aead_blocksize(aead), 4);
1008 x->props.trailer_len = align + 1 + crypto_aead_authsize(aead);
1014 static int esp4_rcv_cb(struct sk_buff *skb, int err)
1019 static const struct xfrm_type esp_type =
1021 .description = "ESP4",
1022 .owner = THIS_MODULE,
1023 .proto = IPPROTO_ESP,
1024 .flags = XFRM_TYPE_REPLAY_PROT,
1025 .init_state = esp_init_state,
1026 .destructor = esp_destroy,
1027 .get_mtu = esp4_get_mtu,
1029 .output = esp_output,
1032 static struct xfrm4_protocol esp4_protocol = {
1033 .handler = xfrm4_rcv,
1034 .input_handler = xfrm_input,
1035 .cb_handler = esp4_rcv_cb,
1036 .err_handler = esp4_err,
1040 static int __init esp4_init(void)
1042 if (xfrm_register_type(&esp_type, AF_INET) < 0) {
1043 pr_info("%s: can't add xfrm type\n", __func__);
1046 if (xfrm4_protocol_register(&esp4_protocol, IPPROTO_ESP) < 0) {
1047 pr_info("%s: can't add protocol\n", __func__);
1048 xfrm_unregister_type(&esp_type, AF_INET);
1054 static void __exit esp4_fini(void)
1056 if (xfrm4_protocol_deregister(&esp4_protocol, IPPROTO_ESP) < 0)
1057 pr_info("%s: can't remove protocol\n", __func__);
1058 if (xfrm_unregister_type(&esp_type, AF_INET) < 0)
1059 pr_info("%s: can't remove xfrm type\n", __func__);
1062 module_init(esp4_init);
1063 module_exit(esp4_fini);
1064 MODULE_LICENSE("GPL");
1065 MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_ESP);