1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C)2002 USAGI/WIDE Project
7 * Mitsuru KANDA @USAGI : IPv6 Support
8 * Kazunori MIYAZAWA @USAGI :
11 * This file is derived from net/ipv4/esp.c
14 #define pr_fmt(fmt) "IPv6: " fmt
16 #include <crypto/aead.h>
17 #include <crypto/authenc.h>
18 #include <linux/err.h>
19 #include <linux/module.h>
23 #include <linux/scatterlist.h>
24 #include <linux/kernel.h>
25 #include <linux/pfkeyv2.h>
26 #include <linux/random.h>
27 #include <linux/slab.h>
28 #include <linux/spinlock.h>
29 #include <net/ip6_checksum.h>
30 #include <net/ip6_route.h>
33 #include <net/protocol.h>
35 #include <linux/icmpv6.h>
37 #include <net/espintcp.h>
38 #include <net/inet6_hashtables.h>
39 #include <linux/skbuff_ref.h>
41 #include <linux/highmem.h>
44 struct xfrm_skb_cb xfrm;
48 struct esp_output_extra {
53 #define ESP_SKB_CB(__skb) ((struct esp_skb_cb *)&((__skb)->cb[0]))
56 * Allocate an AEAD request structure with extra space for SG and IV.
58 * For alignment considerations the upper 32 bits of the sequence number are
59 * placed at the front, if present. Followed by the IV, the request and finally
62 * TODO: Use spare space in skb for this where possible.
64 static void *esp_alloc_tmp(struct crypto_aead *aead, int nfrags, int seqihlen)
70 len += crypto_aead_ivsize(aead);
73 len += crypto_aead_alignmask(aead) &
74 ~(crypto_tfm_ctx_alignment() - 1);
75 len = ALIGN(len, crypto_tfm_ctx_alignment());
78 len += sizeof(struct aead_request) + crypto_aead_reqsize(aead);
79 len = ALIGN(len, __alignof__(struct scatterlist));
81 len += sizeof(struct scatterlist) * nfrags;
83 return kmalloc(len, GFP_ATOMIC);
86 static inline void *esp_tmp_extra(void *tmp)
88 return PTR_ALIGN(tmp, __alignof__(struct esp_output_extra));
91 static inline u8 *esp_tmp_iv(struct crypto_aead *aead, void *tmp, int seqhilen)
93 return crypto_aead_ivsize(aead) ?
94 PTR_ALIGN((u8 *)tmp + seqhilen,
95 crypto_aead_alignmask(aead) + 1) : tmp + seqhilen;
98 static inline struct aead_request *esp_tmp_req(struct crypto_aead *aead, u8 *iv)
100 struct aead_request *req;
102 req = (void *)PTR_ALIGN(iv + crypto_aead_ivsize(aead),
103 crypto_tfm_ctx_alignment());
104 aead_request_set_tfm(req, aead);
108 static inline struct scatterlist *esp_req_sg(struct crypto_aead *aead,
109 struct aead_request *req)
111 return (void *)ALIGN((unsigned long)(req + 1) +
112 crypto_aead_reqsize(aead),
113 __alignof__(struct scatterlist));
116 static void esp_ssg_unref(struct xfrm_state *x, void *tmp, struct sk_buff *skb)
118 struct crypto_aead *aead = x->data;
121 struct aead_request *req;
122 struct scatterlist *sg;
124 if (x->props.flags & XFRM_STATE_ESN)
125 extralen += sizeof(struct esp_output_extra);
127 iv = esp_tmp_iv(aead, tmp, extralen);
128 req = esp_tmp_req(aead, iv);
130 /* Unref skb_frag_pages in the src scatterlist if necessary.
131 * Skip the first sg which comes from skb->data.
133 if (req->src != req->dst)
134 for (sg = sg_next(req->src); sg; sg = sg_next(sg))
135 skb_page_unref(sg_page(sg), skb->pp_recycle);
138 #ifdef CONFIG_INET6_ESPINTCP
144 static void esp_free_tcp_sk(struct rcu_head *head)
146 struct esp_tcp_sk *esk = container_of(head, struct esp_tcp_sk, rcu);
152 static struct sock *esp6_find_tcp_sk(struct xfrm_state *x)
154 struct xfrm_encap_tmpl *encap = x->encap;
155 struct net *net = xs_net(x);
156 struct esp_tcp_sk *esk;
161 sk = rcu_dereference(x->encap_sk);
162 if (sk && sk->sk_state == TCP_ESTABLISHED)
165 spin_lock_bh(&x->lock);
166 sport = encap->encap_sport;
167 dport = encap->encap_dport;
168 nsk = rcu_dereference_protected(x->encap_sk,
169 lockdep_is_held(&x->lock));
170 if (sk && sk == nsk) {
171 esk = kmalloc(sizeof(*esk), GFP_ATOMIC);
173 spin_unlock_bh(&x->lock);
174 return ERR_PTR(-ENOMEM);
176 RCU_INIT_POINTER(x->encap_sk, NULL);
178 call_rcu(&esk->rcu, esp_free_tcp_sk);
180 spin_unlock_bh(&x->lock);
182 sk = __inet6_lookup_established(net, net->ipv4.tcp_death_row.hashinfo, &x->id.daddr.in6,
183 dport, &x->props.saddr.in6, ntohs(sport), 0, 0);
185 return ERR_PTR(-ENOENT);
187 if (!tcp_is_ulp_esp(sk)) {
189 return ERR_PTR(-EINVAL);
192 spin_lock_bh(&x->lock);
193 nsk = rcu_dereference_protected(x->encap_sk,
194 lockdep_is_held(&x->lock));
195 if (encap->encap_sport != sport ||
196 encap->encap_dport != dport) {
198 sk = nsk ?: ERR_PTR(-EREMCHG);
199 } else if (sk == nsk) {
202 rcu_assign_pointer(x->encap_sk, sk);
204 spin_unlock_bh(&x->lock);
209 static int esp_output_tcp_finish(struct xfrm_state *x, struct sk_buff *skb)
216 sk = esp6_find_tcp_sk(x);
217 err = PTR_ERR_OR_ZERO(sk);
222 if (sock_owned_by_user(sk))
223 err = espintcp_queue_out(sk, skb);
225 err = espintcp_push_skb(sk, skb);
233 static int esp_output_tcp_encap_cb(struct net *net, struct sock *sk,
236 struct dst_entry *dst = skb_dst(skb);
237 struct xfrm_state *x = dst->xfrm;
239 return esp_output_tcp_finish(x, skb);
242 static int esp_output_tail_tcp(struct xfrm_state *x, struct sk_buff *skb)
247 err = xfrm_trans_queue_net(xs_net(x), skb, esp_output_tcp_encap_cb);
250 /* EINPROGRESS just happens to do the right thing. It
251 * actually means that the skb has been consumed and
254 return err ?: -EINPROGRESS;
257 static int esp_output_tail_tcp(struct xfrm_state *x, struct sk_buff *skb)
264 static void esp_output_encap_csum(struct sk_buff *skb)
266 /* UDP encap with IPv6 requires a valid checksum */
267 if (*skb_mac_header(skb) == IPPROTO_UDP) {
268 struct udphdr *uh = udp_hdr(skb);
269 struct ipv6hdr *ip6h = ipv6_hdr(skb);
270 int len = ntohs(uh->len);
271 unsigned int offset = skb_transport_offset(skb);
272 __wsum csum = skb_checksum(skb, offset, skb->len - offset, 0);
274 uh->check = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
275 len, IPPROTO_UDP, csum);
277 uh->check = CSUM_MANGLED_0;
281 static void esp_output_done(void *data, int err)
283 struct sk_buff *skb = data;
284 struct xfrm_offload *xo = xfrm_offload(skb);
286 struct xfrm_state *x;
288 if (xo && (xo->flags & XFRM_DEV_RESUME)) {
289 struct sec_path *sp = skb_sec_path(skb);
291 x = sp->xvec[sp->len - 1];
293 x = skb_dst(skb)->xfrm;
296 tmp = ESP_SKB_CB(skb)->tmp;
297 esp_ssg_unref(x, tmp, skb);
300 esp_output_encap_csum(skb);
302 if (xo && (xo->flags & XFRM_DEV_RESUME)) {
304 XFRM_INC_STATS(xs_net(x), LINUX_MIB_XFRMOUTSTATEPROTOERROR);
309 skb_push(skb, skb->data - skb_mac_header(skb));
311 xfrm_dev_resume(skb);
314 x->encap && x->encap->encap_type == TCP_ENCAP_ESPINTCP)
315 esp_output_tail_tcp(x, skb);
317 xfrm_output_resume(skb->sk, skb, err);
321 /* Move ESP header back into place. */
322 static void esp_restore_header(struct sk_buff *skb, unsigned int offset)
324 struct ip_esp_hdr *esph = (void *)(skb->data + offset);
325 void *tmp = ESP_SKB_CB(skb)->tmp;
326 __be32 *seqhi = esp_tmp_extra(tmp);
328 esph->seq_no = esph->spi;
332 static void esp_output_restore_header(struct sk_buff *skb)
334 void *tmp = ESP_SKB_CB(skb)->tmp;
335 struct esp_output_extra *extra = esp_tmp_extra(tmp);
337 esp_restore_header(skb, skb_transport_offset(skb) + extra->esphoff -
341 static struct ip_esp_hdr *esp_output_set_esn(struct sk_buff *skb,
342 struct xfrm_state *x,
343 struct ip_esp_hdr *esph,
344 struct esp_output_extra *extra)
346 /* For ESN we move the header forward by 4 bytes to
347 * accommodate the high bits. We will move it back after
350 if ((x->props.flags & XFRM_STATE_ESN)) {
352 struct xfrm_offload *xo = xfrm_offload(skb);
357 seqhi = XFRM_SKB_CB(skb)->seq.output.hi;
359 extra->esphoff = (unsigned char *)esph -
360 skb_transport_header(skb);
361 esph = (struct ip_esp_hdr *)((unsigned char *)esph - 4);
362 extra->seqhi = esph->spi;
363 esph->seq_no = htonl(seqhi);
366 esph->spi = x->id.spi;
371 static void esp_output_done_esn(void *data, int err)
373 struct sk_buff *skb = data;
375 esp_output_restore_header(skb);
376 esp_output_done(data, err);
379 static struct ip_esp_hdr *esp6_output_udp_encap(struct sk_buff *skb,
381 struct esp_info *esp,
388 len = skb->len + esp->tailen - skb_transport_offset(skb);
390 return ERR_PTR(-EMSGSIZE);
392 uh = (struct udphdr *)esp->esph;
395 uh->len = htons(len);
398 *skb_mac_header(skb) = IPPROTO_UDP;
400 return (struct ip_esp_hdr *)(uh + 1);
403 #ifdef CONFIG_INET6_ESPINTCP
404 static struct ip_esp_hdr *esp6_output_tcp_encap(struct xfrm_state *x,
406 struct esp_info *esp)
408 __be16 *lenp = (void *)esp->esph;
409 struct ip_esp_hdr *esph;
413 len = skb->len + esp->tailen - skb_transport_offset(skb);
414 if (len > IP_MAX_MTU)
415 return ERR_PTR(-EMSGSIZE);
418 sk = esp6_find_tcp_sk(x);
425 esph = (struct ip_esp_hdr *)(lenp + 1);
430 static struct ip_esp_hdr *esp6_output_tcp_encap(struct xfrm_state *x,
432 struct esp_info *esp)
434 return ERR_PTR(-EOPNOTSUPP);
438 static int esp6_output_encap(struct xfrm_state *x, struct sk_buff *skb,
439 struct esp_info *esp)
441 struct xfrm_encap_tmpl *encap = x->encap;
442 struct ip_esp_hdr *esph;
446 spin_lock_bh(&x->lock);
447 sport = encap->encap_sport;
448 dport = encap->encap_dport;
449 encap_type = encap->encap_type;
450 spin_unlock_bh(&x->lock);
452 switch (encap_type) {
454 case UDP_ENCAP_ESPINUDP:
455 esph = esp6_output_udp_encap(skb, encap_type, esp, sport, dport);
457 case TCP_ENCAP_ESPINTCP:
458 esph = esp6_output_tcp_encap(x, skb, esp);
463 return PTR_ERR(esph);
470 int esp6_output_head(struct xfrm_state *x, struct sk_buff *skb, struct esp_info *esp)
476 struct sk_buff *trailer;
477 int tailen = esp->tailen;
480 int err = esp6_output_encap(x, skb, esp);
486 if (ALIGN(tailen, L1_CACHE_BYTES) > PAGE_SIZE ||
487 ALIGN(skb->data_len, L1_CACHE_BYTES) > PAGE_SIZE)
490 if (!skb_cloned(skb)) {
491 if (tailen <= skb_tailroom(skb)) {
494 tail = skb_tail_pointer(trailer);
497 } else if ((skb_shinfo(skb)->nr_frags < MAX_SKB_FRAGS)
498 && !skb_has_frag_list(skb)) {
500 struct sock *sk = skb->sk;
501 struct page_frag *pfrag = &x->xfrag;
503 esp->inplace = false;
505 allocsize = ALIGN(tailen, L1_CACHE_BYTES);
507 spin_lock_bh(&x->lock);
509 if (unlikely(!skb_page_frag_refill(allocsize, pfrag, GFP_ATOMIC))) {
510 spin_unlock_bh(&x->lock);
517 tail = page_address(page) + pfrag->offset;
519 esp_output_fill_trailer(tail, esp->tfclen, esp->plen, esp->proto);
521 nfrags = skb_shinfo(skb)->nr_frags;
523 __skb_fill_page_desc(skb, nfrags, page, pfrag->offset,
525 skb_shinfo(skb)->nr_frags = ++nfrags;
527 pfrag->offset = pfrag->offset + allocsize;
529 spin_unlock_bh(&x->lock);
534 skb->data_len += tailen;
535 skb->truesize += tailen;
536 if (sk && sk_fullsock(sk))
537 refcount_add(tailen, &sk->sk_wmem_alloc);
544 esph_offset = (unsigned char *)esp->esph - skb_transport_header(skb);
546 nfrags = skb_cow_data(skb, tailen, &trailer);
549 tail = skb_tail_pointer(trailer);
550 esp->esph = (struct ip_esp_hdr *)(skb_transport_header(skb) + esph_offset);
553 esp_output_fill_trailer(tail, esp->tfclen, esp->plen, esp->proto);
554 pskb_put(skb, trailer, tailen);
559 EXPORT_SYMBOL_GPL(esp6_output_head);
561 int esp6_output_tail(struct xfrm_state *x, struct sk_buff *skb, struct esp_info *esp)
570 struct ip_esp_hdr *esph;
571 struct aead_request *req;
572 struct crypto_aead *aead;
573 struct scatterlist *sg, *dsg;
574 struct esp_output_extra *extra;
577 assoclen = sizeof(struct ip_esp_hdr);
580 if (x->props.flags & XFRM_STATE_ESN) {
581 extralen += sizeof(*extra);
582 assoclen += sizeof(__be32);
586 alen = crypto_aead_authsize(aead);
587 ivlen = crypto_aead_ivsize(aead);
589 tmp = esp_alloc_tmp(aead, esp->nfrags + 2, extralen);
593 extra = esp_tmp_extra(tmp);
594 iv = esp_tmp_iv(aead, tmp, extralen);
595 req = esp_tmp_req(aead, iv);
596 sg = esp_req_sg(aead, req);
601 dsg = &sg[esp->nfrags];
603 esph = esp_output_set_esn(skb, x, esp->esph, extra);
606 sg_init_table(sg, esp->nfrags);
607 err = skb_to_sgvec(skb, sg,
608 (unsigned char *)esph - skb->data,
609 assoclen + ivlen + esp->clen + alen);
610 if (unlikely(err < 0))
615 struct page_frag *pfrag = &x->xfrag;
617 allocsize = ALIGN(skb->data_len, L1_CACHE_BYTES);
619 spin_lock_bh(&x->lock);
620 if (unlikely(!skb_page_frag_refill(allocsize, pfrag, GFP_ATOMIC))) {
621 spin_unlock_bh(&x->lock);
625 skb_shinfo(skb)->nr_frags = 1;
629 /* replace page frags in skb with new page */
630 __skb_fill_page_desc(skb, 0, page, pfrag->offset, skb->data_len);
631 pfrag->offset = pfrag->offset + allocsize;
632 spin_unlock_bh(&x->lock);
634 sg_init_table(dsg, skb_shinfo(skb)->nr_frags + 1);
635 err = skb_to_sgvec(skb, dsg,
636 (unsigned char *)esph - skb->data,
637 assoclen + ivlen + esp->clen + alen);
638 if (unlikely(err < 0))
642 if ((x->props.flags & XFRM_STATE_ESN))
643 aead_request_set_callback(req, 0, esp_output_done_esn, skb);
645 aead_request_set_callback(req, 0, esp_output_done, skb);
647 aead_request_set_crypt(req, sg, dsg, ivlen + esp->clen, iv);
648 aead_request_set_ad(req, assoclen);
650 memset(iv, 0, ivlen);
651 memcpy(iv + ivlen - min(ivlen, 8), (u8 *)&esp->seqno + 8 - min(ivlen, 8),
654 ESP_SKB_CB(skb)->tmp = tmp;
655 err = crypto_aead_encrypt(req);
666 if ((x->props.flags & XFRM_STATE_ESN))
667 esp_output_restore_header(skb);
668 esp_output_encap_csum(skb);
672 esp_ssg_unref(x, tmp, skb);
674 if (!err && x->encap && x->encap->encap_type == TCP_ENCAP_ESPINTCP)
675 err = esp_output_tail_tcp(x, skb);
682 EXPORT_SYMBOL_GPL(esp6_output_tail);
684 static int esp6_output(struct xfrm_state *x, struct sk_buff *skb)
688 struct ip_esp_hdr *esph;
689 struct crypto_aead *aead;
694 esp.proto = *skb_mac_header(skb);
695 *skb_mac_header(skb) = IPPROTO_ESP;
697 /* skb is pure payload to encrypt */
700 alen = crypto_aead_authsize(aead);
704 struct xfrm_dst *dst = (struct xfrm_dst *)skb_dst(skb);
707 padto = min(x->tfcpad, xfrm_state_mtu(x, dst->child_mtu_cached));
708 if (skb->len < padto)
709 esp.tfclen = padto - skb->len;
711 blksize = ALIGN(crypto_aead_blocksize(aead), 4);
712 esp.clen = ALIGN(skb->len + 2 + esp.tfclen, blksize);
713 esp.plen = esp.clen - skb->len - esp.tfclen;
714 esp.tailen = esp.tfclen + esp.plen + alen;
716 esp.esph = ip_esp_hdr(skb);
718 esp.nfrags = esp6_output_head(x, skb, &esp);
723 esph->spi = x->id.spi;
725 esph->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output.low);
726 esp.seqno = cpu_to_be64(XFRM_SKB_CB(skb)->seq.output.low +
727 ((u64)XFRM_SKB_CB(skb)->seq.output.hi << 32));
729 skb_push(skb, -skb_network_offset(skb));
731 return esp6_output_tail(x, skb, &esp);
734 static inline int esp_remove_trailer(struct sk_buff *skb)
736 struct xfrm_state *x = xfrm_input_state(skb);
737 struct crypto_aead *aead = x->data;
738 int alen, hlen, elen;
744 alen = crypto_aead_authsize(aead);
745 hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead);
746 elen = skb->len - hlen;
748 ret = skb_copy_bits(skb, skb->len - alen - 2, nexthdr, 2);
753 if (padlen + 2 + alen >= elen) {
754 net_dbg_ratelimited("ipsec esp packet is garbage padlen=%d, elen=%d\n",
755 padlen + 2, elen - alen);
759 trimlen = alen + padlen + 2;
760 if (skb->ip_summed == CHECKSUM_COMPLETE) {
761 csumdiff = skb_checksum(skb, skb->len - trimlen, trimlen, 0);
762 skb->csum = csum_block_sub(skb->csum, csumdiff,
765 ret = pskb_trim(skb, skb->len - trimlen);
775 int esp6_input_done2(struct sk_buff *skb, int err)
777 struct xfrm_state *x = xfrm_input_state(skb);
778 struct xfrm_offload *xo = xfrm_offload(skb);
779 struct crypto_aead *aead = x->data;
780 int hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead);
781 int hdr_len = skb_network_header_len(skb);
783 if (!xo || !(xo->flags & CRYPTO_DONE))
784 kfree(ESP_SKB_CB(skb)->tmp);
789 err = esp_remove_trailer(skb);
790 if (unlikely(err < 0))
794 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
795 int offset = skb_network_offset(skb) + sizeof(*ip6h);
796 struct xfrm_encap_tmpl *encap = x->encap;
797 u8 nexthdr = ip6h->nexthdr;
798 __be16 frag_off, source;
802 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
808 uh = (void *)(skb->data + offset);
809 th = (void *)(skb->data + offset);
812 switch (x->encap->encap_type) {
813 case TCP_ENCAP_ESPINTCP:
816 case UDP_ENCAP_ESPINUDP:
826 * 1) if the NAT-T peer's IP or port changed then
827 * advertise the change to the keying daemon.
828 * This is an inbound SA, so just compare
831 if (!ipv6_addr_equal(&ip6h->saddr, &x->props.saddr.in6) ||
832 source != encap->encap_sport) {
833 xfrm_address_t ipaddr;
835 memcpy(&ipaddr.a6, &ip6h->saddr.s6_addr, sizeof(ipaddr.a6));
836 km_new_mapping(x, &ipaddr, source);
838 /* XXX: perhaps add an extra
839 * policy check here, to see
840 * if we should allow or
841 * reject a packet from a
848 * 2) ignore UDP/TCP checksums in case
849 * of NAT-T in Transport Mode, or
850 * perform other post-processing fixes
851 * as per draft-ietf-ipsec-udp-encaps-06,
854 if (x->props.mode == XFRM_MODE_TRANSPORT)
855 skb->ip_summed = CHECKSUM_UNNECESSARY;
858 skb_postpull_rcsum(skb, skb_network_header(skb),
859 skb_network_header_len(skb));
860 skb_pull_rcsum(skb, hlen);
861 if (x->props.mode == XFRM_MODE_TUNNEL)
862 skb_reset_transport_header(skb);
864 skb_set_transport_header(skb, -hdr_len);
866 /* RFC4303: Drop dummy packets without any error */
867 if (err == IPPROTO_NONE)
873 EXPORT_SYMBOL_GPL(esp6_input_done2);
875 static void esp_input_done(void *data, int err)
877 struct sk_buff *skb = data;
879 xfrm_input_resume(skb, esp6_input_done2(skb, err));
882 static void esp_input_restore_header(struct sk_buff *skb)
884 esp_restore_header(skb, 0);
888 static void esp_input_set_header(struct sk_buff *skb, __be32 *seqhi)
890 struct xfrm_state *x = xfrm_input_state(skb);
892 /* For ESN we move the header forward by 4 bytes to
893 * accommodate the high bits. We will move it back after
896 if ((x->props.flags & XFRM_STATE_ESN)) {
897 struct ip_esp_hdr *esph = skb_push(skb, 4);
900 esph->spi = esph->seq_no;
901 esph->seq_no = XFRM_SKB_CB(skb)->seq.input.hi;
905 static void esp_input_done_esn(void *data, int err)
907 struct sk_buff *skb = data;
909 esp_input_restore_header(skb);
910 esp_input_done(data, err);
913 static int esp6_input(struct xfrm_state *x, struct sk_buff *skb)
915 struct crypto_aead *aead = x->data;
916 struct aead_request *req;
917 struct sk_buff *trailer;
918 int ivlen = crypto_aead_ivsize(aead);
919 int elen = skb->len - sizeof(struct ip_esp_hdr) - ivlen;
927 struct scatterlist *sg;
929 if (!pskb_may_pull(skb, sizeof(struct ip_esp_hdr) + ivlen)) {
939 assoclen = sizeof(struct ip_esp_hdr);
942 if (x->props.flags & XFRM_STATE_ESN) {
943 seqhilen += sizeof(__be32);
944 assoclen += seqhilen;
947 if (!skb_cloned(skb)) {
948 if (!skb_is_nonlinear(skb)) {
952 } else if (!skb_has_frag_list(skb)) {
953 nfrags = skb_shinfo(skb)->nr_frags;
960 nfrags = skb_cow_data(skb, 0, &trailer);
968 tmp = esp_alloc_tmp(aead, nfrags, seqhilen);
972 ESP_SKB_CB(skb)->tmp = tmp;
973 seqhi = esp_tmp_extra(tmp);
974 iv = esp_tmp_iv(aead, tmp, seqhilen);
975 req = esp_tmp_req(aead, iv);
976 sg = esp_req_sg(aead, req);
978 esp_input_set_header(skb, seqhi);
980 sg_init_table(sg, nfrags);
981 ret = skb_to_sgvec(skb, sg, 0, skb->len);
982 if (unlikely(ret < 0)) {
987 skb->ip_summed = CHECKSUM_NONE;
989 if ((x->props.flags & XFRM_STATE_ESN))
990 aead_request_set_callback(req, 0, esp_input_done_esn, skb);
992 aead_request_set_callback(req, 0, esp_input_done, skb);
994 aead_request_set_crypt(req, sg, sg, elen + ivlen, iv);
995 aead_request_set_ad(req, assoclen);
997 ret = crypto_aead_decrypt(req);
998 if (ret == -EINPROGRESS)
1001 if ((x->props.flags & XFRM_STATE_ESN))
1002 esp_input_restore_header(skb);
1004 ret = esp6_input_done2(skb, ret);
1010 static int esp6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
1011 u8 type, u8 code, int offset, __be32 info)
1013 struct net *net = dev_net(skb->dev);
1014 const struct ipv6hdr *iph = (const struct ipv6hdr *)skb->data;
1015 struct ip_esp_hdr *esph = (struct ip_esp_hdr *)(skb->data + offset);
1016 struct xfrm_state *x;
1018 if (type != ICMPV6_PKT_TOOBIG &&
1019 type != NDISC_REDIRECT)
1022 x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr,
1023 esph->spi, IPPROTO_ESP, AF_INET6);
1027 if (type == NDISC_REDIRECT)
1028 ip6_redirect(skb, net, skb->dev->ifindex, 0,
1029 sock_net_uid(net, NULL));
1031 ip6_update_pmtu(skb, net, info, 0, 0, sock_net_uid(net, NULL));
1037 static void esp6_destroy(struct xfrm_state *x)
1039 struct crypto_aead *aead = x->data;
1044 crypto_free_aead(aead);
1047 static int esp_init_aead(struct xfrm_state *x, struct netlink_ext_ack *extack)
1049 char aead_name[CRYPTO_MAX_ALG_NAME];
1050 struct crypto_aead *aead;
1053 if (snprintf(aead_name, CRYPTO_MAX_ALG_NAME, "%s(%s)",
1054 x->geniv, x->aead->alg_name) >= CRYPTO_MAX_ALG_NAME) {
1055 NL_SET_ERR_MSG(extack, "Algorithm name is too long");
1056 return -ENAMETOOLONG;
1059 aead = crypto_alloc_aead(aead_name, 0, 0);
1060 err = PTR_ERR(aead);
1066 err = crypto_aead_setkey(aead, x->aead->alg_key,
1067 (x->aead->alg_key_len + 7) / 8);
1071 err = crypto_aead_setauthsize(aead, x->aead->alg_icv_len / 8);
1078 NL_SET_ERR_MSG(extack, "Kernel was unable to initialize cryptographic operations");
1082 static int esp_init_authenc(struct xfrm_state *x,
1083 struct netlink_ext_ack *extack)
1085 struct crypto_aead *aead;
1086 struct crypto_authenc_key_param *param;
1090 char authenc_name[CRYPTO_MAX_ALG_NAME];
1091 unsigned int keylen;
1094 err = -ENAMETOOLONG;
1096 if ((x->props.flags & XFRM_STATE_ESN)) {
1097 if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME,
1098 "%s%sauthencesn(%s,%s)%s",
1099 x->geniv ?: "", x->geniv ? "(" : "",
1100 x->aalg ? x->aalg->alg_name : "digest_null",
1102 x->geniv ? ")" : "") >= CRYPTO_MAX_ALG_NAME) {
1103 NL_SET_ERR_MSG(extack, "Algorithm name is too long");
1107 if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME,
1108 "%s%sauthenc(%s,%s)%s",
1109 x->geniv ?: "", x->geniv ? "(" : "",
1110 x->aalg ? x->aalg->alg_name : "digest_null",
1112 x->geniv ? ")" : "") >= CRYPTO_MAX_ALG_NAME) {
1113 NL_SET_ERR_MSG(extack, "Algorithm name is too long");
1118 aead = crypto_alloc_aead(authenc_name, 0, 0);
1119 err = PTR_ERR(aead);
1121 NL_SET_ERR_MSG(extack, "Kernel was unable to initialize cryptographic operations");
1127 keylen = (x->aalg ? (x->aalg->alg_key_len + 7) / 8 : 0) +
1128 (x->ealg->alg_key_len + 7) / 8 + RTA_SPACE(sizeof(*param));
1130 key = kmalloc(keylen, GFP_KERNEL);
1136 rta->rta_type = CRYPTO_AUTHENC_KEYA_PARAM;
1137 rta->rta_len = RTA_LENGTH(sizeof(*param));
1138 param = RTA_DATA(rta);
1139 p += RTA_SPACE(sizeof(*param));
1142 struct xfrm_algo_desc *aalg_desc;
1144 memcpy(p, x->aalg->alg_key, (x->aalg->alg_key_len + 7) / 8);
1145 p += (x->aalg->alg_key_len + 7) / 8;
1147 aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
1151 if (aalg_desc->uinfo.auth.icv_fullbits / 8 !=
1152 crypto_aead_authsize(aead)) {
1153 NL_SET_ERR_MSG(extack, "Kernel was unable to initialize cryptographic operations");
1157 err = crypto_aead_setauthsize(
1158 aead, x->aalg->alg_trunc_len / 8);
1160 NL_SET_ERR_MSG(extack, "Kernel was unable to initialize cryptographic operations");
1165 param->enckeylen = cpu_to_be32((x->ealg->alg_key_len + 7) / 8);
1166 memcpy(p, x->ealg->alg_key, (x->ealg->alg_key_len + 7) / 8);
1168 err = crypto_aead_setkey(aead, key, keylen);
1177 static int esp6_init_state(struct xfrm_state *x, struct netlink_ext_ack *extack)
1179 struct crypto_aead *aead;
1186 err = esp_init_aead(x, extack);
1187 } else if (x->ealg) {
1188 err = esp_init_authenc(x, extack);
1190 NL_SET_ERR_MSG(extack, "ESP: AEAD or CRYPT must be provided");
1199 x->props.header_len = sizeof(struct ip_esp_hdr) +
1200 crypto_aead_ivsize(aead);
1201 switch (x->props.mode) {
1202 case XFRM_MODE_BEET:
1203 if (x->sel.family != AF_INET6)
1204 x->props.header_len += IPV4_BEET_PHMAXLEN +
1205 (sizeof(struct ipv6hdr) - sizeof(struct iphdr));
1208 case XFRM_MODE_TRANSPORT:
1210 case XFRM_MODE_TUNNEL:
1211 x->props.header_len += sizeof(struct ipv6hdr);
1216 struct xfrm_encap_tmpl *encap = x->encap;
1218 switch (encap->encap_type) {
1220 NL_SET_ERR_MSG(extack, "Unsupported encapsulation type for ESP");
1223 case UDP_ENCAP_ESPINUDP:
1224 x->props.header_len += sizeof(struct udphdr);
1226 #ifdef CONFIG_INET6_ESPINTCP
1227 case TCP_ENCAP_ESPINTCP:
1228 /* only the length field, TCP encap is done by
1231 x->props.header_len += 2;
1237 align = ALIGN(crypto_aead_blocksize(aead), 4);
1238 x->props.trailer_len = align + 1 + crypto_aead_authsize(aead);
1244 static int esp6_rcv_cb(struct sk_buff *skb, int err)
1249 static const struct xfrm_type esp6_type = {
1250 .owner = THIS_MODULE,
1251 .proto = IPPROTO_ESP,
1252 .flags = XFRM_TYPE_REPLAY_PROT,
1253 .init_state = esp6_init_state,
1254 .destructor = esp6_destroy,
1255 .input = esp6_input,
1256 .output = esp6_output,
1259 static struct xfrm6_protocol esp6_protocol = {
1260 .handler = xfrm6_rcv,
1261 .input_handler = xfrm_input,
1262 .cb_handler = esp6_rcv_cb,
1263 .err_handler = esp6_err,
1267 static int __init esp6_init(void)
1269 if (xfrm_register_type(&esp6_type, AF_INET6) < 0) {
1270 pr_info("%s: can't add xfrm type\n", __func__);
1273 if (xfrm6_protocol_register(&esp6_protocol, IPPROTO_ESP) < 0) {
1274 pr_info("%s: can't add protocol\n", __func__);
1275 xfrm_unregister_type(&esp6_type, AF_INET6);
1282 static void __exit esp6_fini(void)
1284 if (xfrm6_protocol_deregister(&esp6_protocol, IPPROTO_ESP) < 0)
1285 pr_info("%s: can't remove protocol\n", __func__);
1286 xfrm_unregister_type(&esp6_type, AF_INET6);
1289 module_init(esp6_init);
1290 module_exit(esp6_fini);
1292 MODULE_DESCRIPTION("IPv6 ESP transformation helpers");
1293 MODULE_LICENSE("GPL");
1294 MODULE_ALIAS_XFRM_TYPE(AF_INET6, XFRM_PROTO_ESP);