1 // SPDX-License-Identifier: GPL-2.0
6 * YOSHIFUJI Hideaki @USAGI
7 * Split up af-specific portion
11 #include <linux/bottom_half.h>
12 #include <linux/cache.h>
13 #include <linux/interrupt.h>
14 #include <linux/slab.h>
15 #include <linux/module.h>
16 #include <linux/netdevice.h>
17 #include <linux/percpu.h>
21 #include <net/ip_tunnels.h>
22 #include <net/ip6_tunnel.h>
24 #include "xfrm_inout.h"
26 struct xfrm_trans_tasklet {
27 struct tasklet_struct tasklet;
28 struct sk_buff_head queue;
31 struct xfrm_trans_cb {
33 struct inet_skb_parm h4;
34 #if IS_ENABLED(CONFIG_IPV6)
35 struct inet6_skb_parm h6;
38 int (*finish)(struct net *net, struct sock *sk, struct sk_buff *skb);
42 #define XFRM_TRANS_SKB_CB(__skb) ((struct xfrm_trans_cb *)&((__skb)->cb[0]))
44 static DEFINE_SPINLOCK(xfrm_input_afinfo_lock);
45 static struct xfrm_input_afinfo const __rcu *xfrm_input_afinfo[2][AF_INET6 + 1];
47 static struct gro_cells gro_cells;
48 static struct net_device xfrm_napi_dev;
50 static DEFINE_PER_CPU(struct xfrm_trans_tasklet, xfrm_trans_tasklet);
52 int xfrm_input_register_afinfo(const struct xfrm_input_afinfo *afinfo)
56 if (WARN_ON(afinfo->family > AF_INET6))
59 spin_lock_bh(&xfrm_input_afinfo_lock);
60 if (unlikely(xfrm_input_afinfo[afinfo->is_ipip][afinfo->family]))
63 rcu_assign_pointer(xfrm_input_afinfo[afinfo->is_ipip][afinfo->family], afinfo);
64 spin_unlock_bh(&xfrm_input_afinfo_lock);
67 EXPORT_SYMBOL(xfrm_input_register_afinfo);
69 int xfrm_input_unregister_afinfo(const struct xfrm_input_afinfo *afinfo)
73 spin_lock_bh(&xfrm_input_afinfo_lock);
74 if (likely(xfrm_input_afinfo[afinfo->is_ipip][afinfo->family])) {
75 if (unlikely(xfrm_input_afinfo[afinfo->is_ipip][afinfo->family] != afinfo))
78 RCU_INIT_POINTER(xfrm_input_afinfo[afinfo->is_ipip][afinfo->family], NULL);
80 spin_unlock_bh(&xfrm_input_afinfo_lock);
84 EXPORT_SYMBOL(xfrm_input_unregister_afinfo);
86 static const struct xfrm_input_afinfo *xfrm_input_get_afinfo(u8 family, bool is_ipip)
88 const struct xfrm_input_afinfo *afinfo;
90 if (WARN_ON_ONCE(family > AF_INET6))
94 afinfo = rcu_dereference(xfrm_input_afinfo[is_ipip][family]);
95 if (unlikely(!afinfo))
100 static int xfrm_rcv_cb(struct sk_buff *skb, unsigned int family, u8 protocol,
103 bool is_ipip = (protocol == IPPROTO_IPIP || protocol == IPPROTO_IPV6);
104 const struct xfrm_input_afinfo *afinfo;
107 afinfo = xfrm_input_get_afinfo(family, is_ipip);
109 return -EAFNOSUPPORT;
111 ret = afinfo->callback(skb, protocol, err);
117 struct sec_path *secpath_set(struct sk_buff *skb)
119 struct sec_path *sp, *tmp = skb_ext_find(skb, SKB_EXT_SEC_PATH);
121 sp = skb_ext_add(skb, SKB_EXT_SEC_PATH);
125 if (tmp) /* reused existing one (was COW'd if needed) */
128 /* allocated new secpath */
129 memset(sp->ovec, 0, sizeof(sp->ovec));
132 sp->verified_cnt = 0;
136 EXPORT_SYMBOL(secpath_set);
138 /* Fetch spi and seq from ipsec header */
140 int xfrm_parse_spi(struct sk_buff *skb, u8 nexthdr, __be32 *spi, __be32 *seq)
142 int offset, offset_seq;
147 hlen = sizeof(struct ip_auth_hdr);
148 offset = offsetof(struct ip_auth_hdr, spi);
149 offset_seq = offsetof(struct ip_auth_hdr, seq_no);
152 hlen = sizeof(struct ip_esp_hdr);
153 offset = offsetof(struct ip_esp_hdr, spi);
154 offset_seq = offsetof(struct ip_esp_hdr, seq_no);
157 if (!pskb_may_pull(skb, sizeof(struct ip_comp_hdr)))
159 *spi = htonl(ntohs(*(__be16 *)(skb_transport_header(skb) + 2)));
166 if (!pskb_may_pull(skb, hlen))
169 *spi = *(__be32 *)(skb_transport_header(skb) + offset);
170 *seq = *(__be32 *)(skb_transport_header(skb) + offset_seq);
173 EXPORT_SYMBOL(xfrm_parse_spi);
175 static int xfrm4_remove_beet_encap(struct xfrm_state *x, struct sk_buff *skb)
181 if (unlikely(XFRM_MODE_SKB_CB(skb)->protocol == IPPROTO_BEETPH)) {
182 struct ip_beet_phdr *ph;
185 if (!pskb_may_pull(skb, sizeof(*ph)))
188 ph = (struct ip_beet_phdr *)skb->data;
190 phlen = sizeof(*ph) + ph->padlen;
191 optlen = ph->hdrlen * 8 + (IPV4_BEET_PHMAXLEN - phlen);
192 if (optlen < 0 || optlen & 3 || optlen > 250)
195 XFRM_MODE_SKB_CB(skb)->protocol = ph->nexthdr;
197 if (!pskb_may_pull(skb, phlen))
199 __skb_pull(skb, phlen);
202 skb_push(skb, sizeof(*iph));
203 skb_reset_network_header(skb);
204 skb_mac_header_rebuild(skb);
206 xfrm4_beet_make_header(skb);
210 iph->ihl += optlen / 4;
211 iph->tot_len = htons(skb->len);
212 iph->daddr = x->sel.daddr.a4;
213 iph->saddr = x->sel.saddr.a4;
215 iph->check = ip_fast_csum(skb_network_header(skb), iph->ihl);
221 static void ipip_ecn_decapsulate(struct sk_buff *skb)
223 struct iphdr *inner_iph = ipip_hdr(skb);
225 if (INET_ECN_is_ce(XFRM_MODE_SKB_CB(skb)->tos))
226 IP_ECN_set_ce(inner_iph);
229 static int xfrm4_remove_tunnel_encap(struct xfrm_state *x, struct sk_buff *skb)
233 if (XFRM_MODE_SKB_CB(skb)->protocol != IPPROTO_IPIP)
236 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
239 err = skb_unclone(skb, GFP_ATOMIC);
243 if (x->props.flags & XFRM_STATE_DECAP_DSCP)
244 ipv4_copy_dscp(XFRM_MODE_SKB_CB(skb)->tos, ipip_hdr(skb));
245 if (!(x->props.flags & XFRM_STATE_NOECN))
246 ipip_ecn_decapsulate(skb);
248 skb_reset_network_header(skb);
249 skb_mac_header_rebuild(skb);
251 eth_hdr(skb)->h_proto = skb->protocol;
259 static void ipip6_ecn_decapsulate(struct sk_buff *skb)
261 struct ipv6hdr *inner_iph = ipipv6_hdr(skb);
263 if (INET_ECN_is_ce(XFRM_MODE_SKB_CB(skb)->tos))
264 IP6_ECN_set_ce(skb, inner_iph);
267 static int xfrm6_remove_tunnel_encap(struct xfrm_state *x, struct sk_buff *skb)
271 if (XFRM_MODE_SKB_CB(skb)->protocol != IPPROTO_IPV6)
273 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
276 err = skb_unclone(skb, GFP_ATOMIC);
280 if (x->props.flags & XFRM_STATE_DECAP_DSCP)
281 ipv6_copy_dscp(XFRM_MODE_SKB_CB(skb)->tos, ipipv6_hdr(skb));
282 if (!(x->props.flags & XFRM_STATE_NOECN))
283 ipip6_ecn_decapsulate(skb);
285 skb_reset_network_header(skb);
286 skb_mac_header_rebuild(skb);
288 eth_hdr(skb)->h_proto = skb->protocol;
296 static int xfrm6_remove_beet_encap(struct xfrm_state *x, struct sk_buff *skb)
298 struct ipv6hdr *ip6h;
299 int size = sizeof(struct ipv6hdr);
302 err = skb_cow_head(skb, size + skb->mac_len);
306 __skb_push(skb, size);
307 skb_reset_network_header(skb);
308 skb_mac_header_rebuild(skb);
310 xfrm6_beet_make_header(skb);
312 ip6h = ipv6_hdr(skb);
313 ip6h->payload_len = htons(skb->len - size);
314 ip6h->daddr = x->sel.daddr.in6;
315 ip6h->saddr = x->sel.saddr.in6;
321 /* Remove encapsulation header.
323 * The IP header will be moved over the top of the encapsulation
326 * On entry, the transport header shall point to where the IP header
327 * should be and the network header shall be set to where the IP
328 * header currently is. skb->data shall point to the start of the
332 xfrm_inner_mode_encap_remove(struct xfrm_state *x,
333 const struct xfrm_mode *inner_mode,
336 switch (inner_mode->encap) {
338 if (inner_mode->family == AF_INET)
339 return xfrm4_remove_beet_encap(x, skb);
340 if (inner_mode->family == AF_INET6)
341 return xfrm6_remove_beet_encap(x, skb);
343 case XFRM_MODE_TUNNEL:
344 if (inner_mode->family == AF_INET)
345 return xfrm4_remove_tunnel_encap(x, skb);
346 if (inner_mode->family == AF_INET6)
347 return xfrm6_remove_tunnel_encap(x, skb);
355 static int xfrm_prepare_input(struct xfrm_state *x, struct sk_buff *skb)
357 const struct xfrm_mode *inner_mode = &x->inner_mode;
359 switch (x->outer_mode.family) {
361 xfrm4_extract_header(skb);
364 xfrm6_extract_header(skb);
368 return -EAFNOSUPPORT;
371 if (x->sel.family == AF_UNSPEC) {
372 inner_mode = xfrm_ip2inner_mode(x, XFRM_MODE_SKB_CB(skb)->protocol);
374 return -EAFNOSUPPORT;
377 switch (inner_mode->family) {
379 skb->protocol = htons(ETH_P_IP);
382 skb->protocol = htons(ETH_P_IPV6);
389 return xfrm_inner_mode_encap_remove(x, inner_mode, skb);
392 /* Remove encapsulation header.
394 * The IP header will be moved over the top of the encapsulation header.
396 * On entry, skb_transport_header() shall point to where the IP header
397 * should be and skb_network_header() shall be set to where the IP header
398 * currently is. skb->data shall point to the start of the payload.
400 static int xfrm4_transport_input(struct xfrm_state *x, struct sk_buff *skb)
402 struct xfrm_offload *xo = xfrm_offload(skb);
403 int ihl = skb->data - skb_transport_header(skb);
405 if (skb->transport_header != skb->network_header) {
406 memmove(skb_transport_header(skb),
407 skb_network_header(skb), ihl);
410 skb_mac_header_was_set(skb) ? skb_mac_header_len(skb) : 0;
411 skb->network_header = skb->transport_header;
413 ip_hdr(skb)->tot_len = htons(skb->len + ihl);
414 skb_reset_transport_header(skb);
418 static int xfrm6_transport_input(struct xfrm_state *x, struct sk_buff *skb)
420 #if IS_ENABLED(CONFIG_IPV6)
421 struct xfrm_offload *xo = xfrm_offload(skb);
422 int ihl = skb->data - skb_transport_header(skb);
424 if (skb->transport_header != skb->network_header) {
425 memmove(skb_transport_header(skb),
426 skb_network_header(skb), ihl);
429 skb_mac_header_was_set(skb) ? skb_mac_header_len(skb) : 0;
430 skb->network_header = skb->transport_header;
432 ipv6_hdr(skb)->payload_len = htons(skb->len + ihl -
433 sizeof(struct ipv6hdr));
434 skb_reset_transport_header(skb);
438 return -EAFNOSUPPORT;
442 static int xfrm_inner_mode_input(struct xfrm_state *x,
443 const struct xfrm_mode *inner_mode,
446 switch (inner_mode->encap) {
448 case XFRM_MODE_TUNNEL:
449 return xfrm_prepare_input(x, skb);
450 case XFRM_MODE_TRANSPORT:
451 if (inner_mode->family == AF_INET)
452 return xfrm4_transport_input(x, skb);
453 if (inner_mode->family == AF_INET6)
454 return xfrm6_transport_input(x, skb);
456 case XFRM_MODE_ROUTEOPTIMIZATION:
467 int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type)
469 const struct xfrm_state_afinfo *afinfo;
470 struct net *net = dev_net(skb->dev);
471 const struct xfrm_mode *inner_mode;
475 struct xfrm_state *x = NULL;
476 xfrm_address_t *daddr;
477 u32 mark = skb->mark;
478 unsigned int family = AF_UNSPEC;
481 bool xfrm_gro = false;
482 bool crypto_done = false;
483 struct xfrm_offload *xo = xfrm_offload(skb);
486 if (encap_type < 0) {
487 x = xfrm_input_state(skb);
489 if (unlikely(x->km.state != XFRM_STATE_VALID)) {
490 if (x->km.state == XFRM_STATE_ACQ)
491 XFRM_INC_STATS(net, LINUX_MIB_XFRMACQUIREERROR);
494 LINUX_MIB_XFRMINSTATEINVALID);
496 if (encap_type == -1)
501 family = x->outer_mode.family;
503 /* An encap_type of -1 indicates async resumption. */
504 if (encap_type == -1) {
506 seq = XFRM_SKB_CB(skb)->seq.input.low;
510 /* encap_type < -1 indicates a GRO call. */
512 seq = XFRM_SPI_SKB_CB(skb)->seq;
514 if (xo && (xo->flags & CRYPTO_DONE)) {
516 family = XFRM_SPI_SKB_CB(skb)->family;
518 if (!(xo->status & CRYPTO_SUCCESS)) {
520 (CRYPTO_TRANSPORT_AH_AUTH_FAILED |
521 CRYPTO_TRANSPORT_ESP_AUTH_FAILED |
522 CRYPTO_TUNNEL_AH_AUTH_FAILED |
523 CRYPTO_TUNNEL_ESP_AUTH_FAILED)) {
525 xfrm_audit_state_icvfail(x, skb,
527 x->stats.integrity_failed++;
528 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEPROTOERROR);
532 if (xo->status & CRYPTO_INVALID_PROTOCOL) {
533 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEPROTOERROR);
537 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
541 if ((err = xfrm_parse_spi(skb, nexthdr, &spi, &seq)) != 0) {
542 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
550 family = XFRM_SPI_SKB_CB(skb)->family;
552 /* if tunnel is present override skb->mark value with tunnel i_key */
555 if (XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4)
556 mark = be32_to_cpu(XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4->parms.i_key);
559 if (XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6)
560 mark = be32_to_cpu(XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6->parms.i_key);
564 sp = secpath_set(skb);
566 XFRM_INC_STATS(net, LINUX_MIB_XFRMINERROR);
571 if (!spi && (err = xfrm_parse_spi(skb, nexthdr, &spi, &seq)) != 0) {
573 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
577 daddr = (xfrm_address_t *)(skb_network_header(skb) +
578 XFRM_SPI_SKB_CB(skb)->daddroff);
580 sp = skb_sec_path(skb);
582 if (sp->len == XFRM_MAX_DEPTH) {
584 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
588 x = xfrm_state_lookup(net, mark, daddr, spi, nexthdr, family);
591 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOSTATES);
592 xfrm_audit_state_notfound(skb, family, spi, seq);
596 skb->mark = xfrm_smark_get(skb->mark, x);
598 sp->xvec[sp->len++] = x;
602 XFRM_INC_STATS(net, LINUX_MIB_XFRMINERROR);
609 if (unlikely(x->km.state != XFRM_STATE_VALID)) {
610 if (x->km.state == XFRM_STATE_ACQ)
611 XFRM_INC_STATS(net, LINUX_MIB_XFRMACQUIREERROR);
614 LINUX_MIB_XFRMINSTATEINVALID);
618 if ((x->encap ? x->encap->encap_type : 0) != encap_type) {
619 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
623 if (x->repl->check(x, skb, seq)) {
624 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATESEQERROR);
628 if (xfrm_state_check_expire(x)) {
629 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEEXPIRED);
633 spin_unlock(&x->lock);
635 if (xfrm_tunnel_check(skb, x, family)) {
636 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMODEERROR);
640 seq_hi = htonl(xfrm_replay_seqhi(x, seq));
642 XFRM_SKB_CB(skb)->seq.input.low = seq;
643 XFRM_SKB_CB(skb)->seq.input.hi = seq_hi;
648 nexthdr = x->type_offload->input_tail(x, skb);
650 nexthdr = x->type->input(x, skb);
652 if (nexthdr == -EINPROGRESS)
659 if (nexthdr == -EBADMSG) {
660 xfrm_audit_state_icvfail(x, skb,
662 x->stats.integrity_failed++;
664 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEPROTOERROR);
668 /* only the first xfrm gets the encap type */
671 if (x->repl->recheck(x, skb, seq)) {
672 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATESEQERROR);
676 x->repl->advance(x, seq);
678 x->curlft.bytes += skb->len;
681 spin_unlock(&x->lock);
683 XFRM_MODE_SKB_CB(skb)->protocol = nexthdr;
685 inner_mode = &x->inner_mode;
687 if (x->sel.family == AF_UNSPEC) {
688 inner_mode = xfrm_ip2inner_mode(x, XFRM_MODE_SKB_CB(skb)->protocol);
689 if (inner_mode == NULL) {
690 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMODEERROR);
695 if (xfrm_inner_mode_input(x, inner_mode, skb)) {
696 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMODEERROR);
700 if (x->outer_mode.flags & XFRM_MODE_FLAG_TUNNEL) {
706 * We need the inner address. However, we only get here for
707 * transport mode so the outer address is identical.
709 daddr = &x->id.daddr;
710 family = x->outer_mode.family;
712 err = xfrm_parse_spi(skb, nexthdr, &spi, &seq);
714 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
720 err = xfrm_rcv_cb(skb, family, x->type->proto, 0);
727 sp = skb_sec_path(skb);
731 gro_cells_receive(&gro_cells, skb);
734 xo = xfrm_offload(skb);
736 xfrm_gro = xo->flags & XFRM_GRO;
740 afinfo = xfrm_state_afinfo_get_rcu(x->inner_mode.family);
742 err = afinfo->transport_finish(skb, xfrm_gro || async);
745 sp = skb_sec_path(skb);
749 gro_cells_receive(&gro_cells, skb);
757 spin_unlock(&x->lock);
759 xfrm_rcv_cb(skb, family, x && x->type ? x->type->proto : nexthdr, -1);
763 EXPORT_SYMBOL(xfrm_input);
765 int xfrm_input_resume(struct sk_buff *skb, int nexthdr)
767 return xfrm_input(skb, nexthdr, 0, -1);
769 EXPORT_SYMBOL(xfrm_input_resume);
771 static void xfrm_trans_reinject(unsigned long data)
773 struct xfrm_trans_tasklet *trans = (void *)data;
774 struct sk_buff_head queue;
777 __skb_queue_head_init(&queue);
778 skb_queue_splice_init(&trans->queue, &queue);
780 while ((skb = __skb_dequeue(&queue)))
781 XFRM_TRANS_SKB_CB(skb)->finish(XFRM_TRANS_SKB_CB(skb)->net,
785 int xfrm_trans_queue_net(struct net *net, struct sk_buff *skb,
786 int (*finish)(struct net *, struct sock *,
789 struct xfrm_trans_tasklet *trans;
791 trans = this_cpu_ptr(&xfrm_trans_tasklet);
793 if (skb_queue_len(&trans->queue) >= READ_ONCE(netdev_max_backlog))
796 BUILD_BUG_ON(sizeof(struct xfrm_trans_cb) > sizeof(skb->cb));
798 XFRM_TRANS_SKB_CB(skb)->finish = finish;
799 XFRM_TRANS_SKB_CB(skb)->net = net;
800 __skb_queue_tail(&trans->queue, skb);
801 tasklet_schedule(&trans->tasklet);
804 EXPORT_SYMBOL(xfrm_trans_queue_net);
806 int xfrm_trans_queue(struct sk_buff *skb,
807 int (*finish)(struct net *, struct sock *,
810 return xfrm_trans_queue_net(dev_net(skb->dev), skb, finish);
812 EXPORT_SYMBOL(xfrm_trans_queue);
814 void __init xfrm_input_init(void)
819 init_dummy_netdev(&xfrm_napi_dev);
820 err = gro_cells_init(&gro_cells, &xfrm_napi_dev);
822 gro_cells.cells = NULL;
824 for_each_possible_cpu(i) {
825 struct xfrm_trans_tasklet *trans;
827 trans = &per_cpu(xfrm_trans_tasklet, i);
828 __skb_queue_head_init(&trans->queue);
829 tasklet_init(&trans->tasklet, xfrm_trans_reinject,
830 (unsigned long)trans);