1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * SR-IPv6 implementation
6 * David Lebrun <david.lebrun@uclouvain.be>
7 * eBPF support: Mathieu Xhonneux <m.xhonneux@gmail.com>
10 #include <linux/filter.h>
11 #include <linux/types.h>
12 #include <linux/skbuff.h>
13 #include <linux/net.h>
14 #include <linux/module.h>
16 #include <net/lwtunnel.h>
17 #include <net/netevent.h>
18 #include <net/netns/generic.h>
19 #include <net/ip6_fib.h>
20 #include <net/route.h>
22 #include <linux/seg6.h>
23 #include <linux/seg6_local.h>
24 #include <net/addrconf.h>
25 #include <net/ip6_route.h>
26 #include <net/dst_cache.h>
27 #include <net/ip_tunnels.h>
28 #ifdef CONFIG_IPV6_SEG6_HMAC
29 #include <net/seg6_hmac.h>
31 #include <net/seg6_local.h>
32 #include <linux/etherdevice.h>
33 #include <linux/bpf.h>
34 #include <linux/netfilter.h>
36 #define SEG6_F_ATTR(i) BIT(i)
38 struct seg6_local_lwt;
40 /* callbacks used for customizing the creation and destruction of a behavior */
41 struct seg6_local_lwtunnel_ops {
42 int (*build_state)(struct seg6_local_lwt *slwt, const void *cfg,
43 struct netlink_ext_ack *extack);
44 void (*destroy_state)(struct seg6_local_lwt *slwt);
47 struct seg6_action_desc {
51 /* The optattrs field is used for specifying all the optional
52 * attributes supported by a specific behavior.
53 * It means that if one of these attributes is not provided in the
54 * netlink message during the behavior creation, no errors will be
55 * returned to the userspace.
57 * Each attribute can be only of two types (mutually exclusive):
58 * 1) required or 2) optional.
59 * Every user MUST obey to this rule! If you set an attribute as
60 * required the same attribute CANNOT be set as optional and vice
63 unsigned long optattrs;
65 int (*input)(struct sk_buff *skb, struct seg6_local_lwt *slwt);
68 struct seg6_local_lwtunnel_ops slwt_ops;
72 struct bpf_prog *prog;
76 enum seg6_end_dt_mode {
77 DT_INVALID_MODE = -EINVAL,
82 struct seg6_end_dt_info {
83 enum seg6_end_dt_mode mode;
86 /* VRF device associated to the routing table used by the SRv6
87 * End.DT4/DT6 behavior for routing IPv4/IPv6 packets.
92 /* tunneled packet family (IPv4 or IPv6).
93 * Protocol and header length are inferred from family.
98 struct pcpu_seg6_local_counters {
103 struct u64_stats_sync syncp;
106 /* This struct groups all the SRv6 Behavior counters supported so far.
108 * put_nla_counters() makes use of this data structure to collect all counter
109 * values after the per-CPU counter evaluation has been performed.
110 * Finally, each counter value (in seg6_local_counters) is stored in the
111 * corresponding netlink attribute and sent to user space.
113 * NB: we don't want to expose this structure to user space!
115 struct seg6_local_counters {
121 #define seg6_local_alloc_pcpu_counters(__gfp) \
122 __netdev_alloc_pcpu_stats(struct pcpu_seg6_local_counters, \
123 ((__gfp) | __GFP_ZERO))
125 #define SEG6_F_LOCAL_COUNTERS SEG6_F_ATTR(SEG6_LOCAL_COUNTERS)
127 struct seg6_local_lwt {
129 struct ipv6_sr_hdr *srh;
135 struct bpf_lwt_prog bpf;
136 #ifdef CONFIG_NET_L3_MASTER_DEV
137 struct seg6_end_dt_info dt_info;
139 struct pcpu_seg6_local_counters __percpu *pcpu_counters;
142 struct seg6_action_desc *desc;
143 /* unlike the required attrs, we have to track the optional attributes
144 * that have been effectively parsed.
146 unsigned long parsed_optattrs;
149 static struct seg6_local_lwt *seg6_local_lwtunnel(struct lwtunnel_state *lwt)
151 return (struct seg6_local_lwt *)lwt->data;
154 static struct ipv6_sr_hdr *get_and_validate_srh(struct sk_buff *skb)
156 struct ipv6_sr_hdr *srh;
158 srh = seg6_get_srh(skb, IP6_FH_F_SKIP_RH);
162 #ifdef CONFIG_IPV6_SEG6_HMAC
163 if (!seg6_hmac_validate_skb(skb))
170 static bool decap_and_validate(struct sk_buff *skb, int proto)
172 struct ipv6_sr_hdr *srh;
173 unsigned int off = 0;
175 srh = seg6_get_srh(skb, 0);
176 if (srh && srh->segments_left > 0)
179 #ifdef CONFIG_IPV6_SEG6_HMAC
180 if (srh && !seg6_hmac_validate_skb(skb))
184 if (ipv6_find_hdr(skb, &off, proto, NULL, NULL) < 0)
187 if (!pskb_pull(skb, off))
190 skb_postpull_rcsum(skb, skb_network_header(skb), off);
192 skb_reset_network_header(skb);
193 skb_reset_transport_header(skb);
194 if (iptunnel_pull_offloads(skb))
200 static void advance_nextseg(struct ipv6_sr_hdr *srh, struct in6_addr *daddr)
202 struct in6_addr *addr;
204 srh->segments_left--;
205 addr = srh->segments + srh->segments_left;
210 seg6_lookup_any_nexthop(struct sk_buff *skb, struct in6_addr *nhaddr,
211 u32 tbl_id, bool local_delivery)
213 struct net *net = dev_net(skb->dev);
214 struct ipv6hdr *hdr = ipv6_hdr(skb);
215 int flags = RT6_LOOKUP_F_HAS_SADDR;
216 struct dst_entry *dst = NULL;
221 memset(&fl6, 0, sizeof(fl6));
222 fl6.flowi6_iif = skb->dev->ifindex;
223 fl6.daddr = nhaddr ? *nhaddr : hdr->daddr;
224 fl6.saddr = hdr->saddr;
225 fl6.flowlabel = ip6_flowinfo(hdr);
226 fl6.flowi6_mark = skb->mark;
227 fl6.flowi6_proto = hdr->nexthdr;
230 fl6.flowi6_flags = FLOWI_FLAG_KNOWN_NH;
233 dst = ip6_route_input_lookup(net, skb->dev, &fl6, skb, flags);
235 struct fib6_table *table;
237 table = fib6_get_table(net, tbl_id);
241 rt = ip6_pol_route(net, table, 0, &fl6, skb, flags);
245 /* we want to discard traffic destined for local packet processing,
246 * if @local_delivery is set to false.
249 dev_flags |= IFF_LOOPBACK;
251 if (dst && (dst->dev->flags & dev_flags) && !dst->error) {
258 rt = net->ipv6.ip6_blk_hole_entry;
264 skb_dst_set(skb, dst);
268 int seg6_lookup_nexthop(struct sk_buff *skb,
269 struct in6_addr *nhaddr, u32 tbl_id)
271 return seg6_lookup_any_nexthop(skb, nhaddr, tbl_id, false);
274 /* regular endpoint function */
275 static int input_action_end(struct sk_buff *skb, struct seg6_local_lwt *slwt)
277 struct ipv6_sr_hdr *srh;
279 srh = get_and_validate_srh(skb);
283 advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
285 seg6_lookup_nexthop(skb, NULL, 0);
287 return dst_input(skb);
294 /* regular endpoint, and forward to specified nexthop */
295 static int input_action_end_x(struct sk_buff *skb, struct seg6_local_lwt *slwt)
297 struct ipv6_sr_hdr *srh;
299 srh = get_and_validate_srh(skb);
303 advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
305 seg6_lookup_nexthop(skb, &slwt->nh6, 0);
307 return dst_input(skb);
314 static int input_action_end_t(struct sk_buff *skb, struct seg6_local_lwt *slwt)
316 struct ipv6_sr_hdr *srh;
318 srh = get_and_validate_srh(skb);
322 advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
324 seg6_lookup_nexthop(skb, NULL, slwt->table);
326 return dst_input(skb);
333 /* decapsulate and forward inner L2 frame on specified interface */
334 static int input_action_end_dx2(struct sk_buff *skb,
335 struct seg6_local_lwt *slwt)
337 struct net *net = dev_net(skb->dev);
338 struct net_device *odev;
341 if (!decap_and_validate(skb, IPPROTO_ETHERNET))
344 if (!pskb_may_pull(skb, ETH_HLEN))
347 skb_reset_mac_header(skb);
348 eth = (struct ethhdr *)skb->data;
350 /* To determine the frame's protocol, we assume it is 802.3. This avoids
351 * a call to eth_type_trans(), which is not really relevant for our
354 if (!eth_proto_is_802_3(eth->h_proto))
357 odev = dev_get_by_index_rcu(net, slwt->oif);
361 /* As we accept Ethernet frames, make sure the egress device is of
364 if (odev->type != ARPHRD_ETHER)
367 if (!(odev->flags & IFF_UP) || !netif_carrier_ok(odev))
372 if (skb_warn_if_lro(skb))
375 skb_forward_csum(skb);
377 if (skb->len - ETH_HLEN > odev->mtu)
381 skb->protocol = eth->h_proto;
383 return dev_queue_xmit(skb);
390 static int input_action_end_dx6_finish(struct net *net, struct sock *sk,
393 struct dst_entry *orig_dst = skb_dst(skb);
394 struct in6_addr *nhaddr = NULL;
395 struct seg6_local_lwt *slwt;
397 slwt = seg6_local_lwtunnel(orig_dst->lwtstate);
399 /* The inner packet is not associated to any local interface,
400 * so we do not call netif_rx().
402 * If slwt->nh6 is set to ::, then lookup the nexthop for the
403 * inner packet's DA. Otherwise, use the specified nexthop.
405 if (!ipv6_addr_any(&slwt->nh6))
408 seg6_lookup_nexthop(skb, nhaddr, 0);
410 return dst_input(skb);
413 /* decapsulate and forward to specified nexthop */
414 static int input_action_end_dx6(struct sk_buff *skb,
415 struct seg6_local_lwt *slwt)
417 /* this function accepts IPv6 encapsulated packets, with either
418 * an SRH with SL=0, or no SRH.
421 if (!decap_and_validate(skb, IPPROTO_IPV6))
424 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
427 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
430 if (static_branch_unlikely(&nf_hooks_lwtunnel_enabled))
431 return NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING,
432 dev_net(skb->dev), NULL, skb, NULL,
433 skb_dst(skb)->dev, input_action_end_dx6_finish);
435 return input_action_end_dx6_finish(dev_net(skb->dev), NULL, skb);
441 static int input_action_end_dx4_finish(struct net *net, struct sock *sk,
444 struct dst_entry *orig_dst = skb_dst(skb);
445 struct seg6_local_lwt *slwt;
450 slwt = seg6_local_lwtunnel(orig_dst->lwtstate);
454 nhaddr = slwt->nh4.s_addr ?: iph->daddr;
458 err = ip_route_input(skb, nhaddr, iph->saddr, 0, skb->dev);
464 return dst_input(skb);
467 static int input_action_end_dx4(struct sk_buff *skb,
468 struct seg6_local_lwt *slwt)
470 if (!decap_and_validate(skb, IPPROTO_IPIP))
473 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
476 skb->protocol = htons(ETH_P_IP);
477 skb_set_transport_header(skb, sizeof(struct iphdr));
480 if (static_branch_unlikely(&nf_hooks_lwtunnel_enabled))
481 return NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING,
482 dev_net(skb->dev), NULL, skb, NULL,
483 skb_dst(skb)->dev, input_action_end_dx4_finish);
485 return input_action_end_dx4_finish(dev_net(skb->dev), NULL, skb);
491 #ifdef CONFIG_NET_L3_MASTER_DEV
492 static struct net *fib6_config_get_net(const struct fib6_config *fib6_cfg)
494 const struct nl_info *nli = &fib6_cfg->fc_nlinfo;
499 static int __seg6_end_dt_vrf_build(struct seg6_local_lwt *slwt, const void *cfg,
500 u16 family, struct netlink_ext_ack *extack)
502 struct seg6_end_dt_info *info = &slwt->dt_info;
506 net = fib6_config_get_net(cfg);
508 /* note that vrf_table was already set by parse_nla_vrftable() */
509 vrf_ifindex = l3mdev_ifindex_lookup_by_table_id(L3MDEV_TYPE_VRF, net,
511 if (vrf_ifindex < 0) {
512 if (vrf_ifindex == -EPERM) {
513 NL_SET_ERR_MSG(extack,
514 "Strict mode for VRF is disabled");
515 } else if (vrf_ifindex == -ENODEV) {
516 NL_SET_ERR_MSG(extack,
517 "Table has no associated VRF device");
519 pr_debug("seg6local: SRv6 End.DT* creation error=%d\n",
527 info->vrf_ifindex = vrf_ifindex;
529 info->family = family;
530 info->mode = DT_VRF_MODE;
535 /* The SRv6 End.DT4/DT6 behavior extracts the inner (IPv4/IPv6) packet and
536 * routes the IPv4/IPv6 packet by looking at the configured routing table.
538 * In the SRv6 End.DT4/DT6 use case, we can receive traffic (IPv6+Segment
539 * Routing Header packets) from several interfaces and the outer IPv6
540 * destination address (DA) is used for retrieving the specific instance of the
541 * End.DT4/DT6 behavior that should process the packets.
543 * However, the inner IPv4/IPv6 packet is not really bound to any receiving
544 * interface and thus the End.DT4/DT6 sets the VRF (associated with the
545 * corresponding routing table) as the *receiving* interface.
546 * In other words, the End.DT4/DT6 processes a packet as if it has been received
547 * directly by the VRF (and not by one of its slave devices, if any).
548 * In this way, the VRF interface is used for routing the IPv4/IPv6 packet in
549 * according to the routing table configured by the End.DT4/DT6 instance.
551 * This design allows you to get some interesting features like:
552 * 1) the statistics on rx packets;
553 * 2) the possibility to install a packet sniffer on the receiving interface
554 * (the VRF one) for looking at the incoming packets;
555 * 3) the possibility to leverage the netfilter prerouting hook for the inner
558 * This function returns:
559 * - the sk_buff* when the VRF rcv handler has processed the packet correctly;
560 * - NULL when the skb is consumed by the VRF rcv handler;
561 * - a pointer which encodes a negative error number in case of error.
562 * Note that in this case, the function takes care of freeing the skb.
564 static struct sk_buff *end_dt_vrf_rcv(struct sk_buff *skb, u16 family,
565 struct net_device *dev)
567 /* based on l3mdev_ip_rcv; we are only interested in the master */
568 if (unlikely(!netif_is_l3_master(dev) && !netif_has_l3_rx_handler(dev)))
571 if (unlikely(!dev->l3mdev_ops->l3mdev_l3_rcv))
574 /* the decap packet IPv4/IPv6 does not come with any mac header info.
575 * We must unset the mac header to allow the VRF device to rebuild it,
576 * just in case there is a sniffer attached on the device.
578 skb_unset_mac_header(skb);
580 skb = dev->l3mdev_ops->l3mdev_l3_rcv(dev, skb, family);
582 /* the skb buffer was consumed by the handler */
585 /* when a packet is received by a VRF or by one of its slaves, the
586 * master device reference is set into the skb.
588 if (unlikely(skb->dev != dev || skb->skb_iif != dev->ifindex))
595 return ERR_PTR(-EINVAL);
598 static struct net_device *end_dt_get_vrf_rcu(struct sk_buff *skb,
599 struct seg6_end_dt_info *info)
601 int vrf_ifindex = info->vrf_ifindex;
602 struct net *net = info->net;
604 if (unlikely(vrf_ifindex < 0))
607 if (unlikely(!net_eq(dev_net(skb->dev), net)))
610 return dev_get_by_index_rcu(net, vrf_ifindex);
616 static struct sk_buff *end_dt_vrf_core(struct sk_buff *skb,
617 struct seg6_local_lwt *slwt, u16 family)
619 struct seg6_end_dt_info *info = &slwt->dt_info;
620 struct net_device *vrf;
624 vrf = end_dt_get_vrf_rcu(skb, info);
630 protocol = htons(ETH_P_IP);
631 hdrlen = sizeof(struct iphdr);
634 protocol = htons(ETH_P_IPV6);
635 hdrlen = sizeof(struct ipv6hdr);
643 if (unlikely(info->family != AF_UNSPEC && info->family != family)) {
644 pr_warn_once("seg6local: SRv6 End.DT* family mismatch");
648 skb->protocol = protocol;
652 skb_set_transport_header(skb, hdrlen);
655 return end_dt_vrf_rcv(skb, family, vrf);
659 return ERR_PTR(-EINVAL);
662 static int input_action_end_dt4(struct sk_buff *skb,
663 struct seg6_local_lwt *slwt)
668 if (!decap_and_validate(skb, IPPROTO_IPIP))
671 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
674 skb = end_dt_vrf_core(skb, slwt, AF_INET);
676 /* packet has been processed and consumed by the VRF */
684 err = ip_route_input(skb, iph->daddr, iph->saddr, 0, skb->dev);
688 return dst_input(skb);
695 static int seg6_end_dt4_build(struct seg6_local_lwt *slwt, const void *cfg,
696 struct netlink_ext_ack *extack)
698 return __seg6_end_dt_vrf_build(slwt, cfg, AF_INET, extack);
702 seg6_end_dt_mode seg6_end_dt6_parse_mode(struct seg6_local_lwt *slwt)
704 unsigned long parsed_optattrs = slwt->parsed_optattrs;
705 bool legacy, vrfmode;
707 legacy = !!(parsed_optattrs & SEG6_F_ATTR(SEG6_LOCAL_TABLE));
708 vrfmode = !!(parsed_optattrs & SEG6_F_ATTR(SEG6_LOCAL_VRFTABLE));
710 if (!(legacy ^ vrfmode))
711 /* both are absent or present: invalid DT6 mode */
712 return DT_INVALID_MODE;
714 return legacy ? DT_LEGACY_MODE : DT_VRF_MODE;
717 static enum seg6_end_dt_mode seg6_end_dt6_get_mode(struct seg6_local_lwt *slwt)
719 struct seg6_end_dt_info *info = &slwt->dt_info;
724 static int seg6_end_dt6_build(struct seg6_local_lwt *slwt, const void *cfg,
725 struct netlink_ext_ack *extack)
727 enum seg6_end_dt_mode mode = seg6_end_dt6_parse_mode(slwt);
728 struct seg6_end_dt_info *info = &slwt->dt_info;
732 info->mode = DT_LEGACY_MODE;
735 return __seg6_end_dt_vrf_build(slwt, cfg, AF_INET6, extack);
737 NL_SET_ERR_MSG(extack, "table or vrftable must be specified");
743 static int input_action_end_dt6(struct sk_buff *skb,
744 struct seg6_local_lwt *slwt)
746 if (!decap_and_validate(skb, IPPROTO_IPV6))
749 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
752 #ifdef CONFIG_NET_L3_MASTER_DEV
753 if (seg6_end_dt6_get_mode(slwt) == DT_LEGACY_MODE)
757 skb = end_dt_vrf_core(skb, slwt, AF_INET6);
759 /* packet has been processed and consumed by the VRF */
765 /* note: this time we do not need to specify the table because the VRF
766 * takes care of selecting the correct table.
768 seg6_lookup_any_nexthop(skb, NULL, 0, true);
770 return dst_input(skb);
774 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
776 seg6_lookup_any_nexthop(skb, NULL, slwt->table, true);
778 return dst_input(skb);
785 #ifdef CONFIG_NET_L3_MASTER_DEV
786 static int seg6_end_dt46_build(struct seg6_local_lwt *slwt, const void *cfg,
787 struct netlink_ext_ack *extack)
789 return __seg6_end_dt_vrf_build(slwt, cfg, AF_UNSPEC, extack);
792 static int input_action_end_dt46(struct sk_buff *skb,
793 struct seg6_local_lwt *slwt)
795 unsigned int off = 0;
798 nexthdr = ipv6_find_hdr(skb, &off, -1, NULL, NULL);
799 if (unlikely(nexthdr < 0))
804 return input_action_end_dt4(skb, slwt);
806 return input_action_end_dt6(skb, slwt);
815 /* push an SRH on top of the current one */
816 static int input_action_end_b6(struct sk_buff *skb, struct seg6_local_lwt *slwt)
818 struct ipv6_sr_hdr *srh;
821 srh = get_and_validate_srh(skb);
825 err = seg6_do_srh_inline(skb, slwt->srh);
829 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
831 seg6_lookup_nexthop(skb, NULL, 0);
833 return dst_input(skb);
840 /* encapsulate within an outer IPv6 header and a specified SRH */
841 static int input_action_end_b6_encap(struct sk_buff *skb,
842 struct seg6_local_lwt *slwt)
844 struct ipv6_sr_hdr *srh;
847 srh = get_and_validate_srh(skb);
851 advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
853 skb_reset_inner_headers(skb);
854 skb->encapsulation = 1;
856 err = seg6_do_srh_encap(skb, slwt->srh, IPPROTO_IPV6);
860 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
862 seg6_lookup_nexthop(skb, NULL, 0);
864 return dst_input(skb);
871 DEFINE_PER_CPU(struct seg6_bpf_srh_state, seg6_bpf_srh_states);
873 bool seg6_bpf_has_valid_srh(struct sk_buff *skb)
875 struct seg6_bpf_srh_state *srh_state =
876 this_cpu_ptr(&seg6_bpf_srh_states);
877 struct ipv6_sr_hdr *srh = srh_state->srh;
879 if (unlikely(srh == NULL))
882 if (unlikely(!srh_state->valid)) {
883 if ((srh_state->hdrlen & 7) != 0)
886 srh->hdrlen = (u8)(srh_state->hdrlen >> 3);
887 if (!seg6_validate_srh(srh, (srh->hdrlen + 1) << 3, true))
890 srh_state->valid = true;
896 static int input_action_end_bpf(struct sk_buff *skb,
897 struct seg6_local_lwt *slwt)
899 struct seg6_bpf_srh_state *srh_state =
900 this_cpu_ptr(&seg6_bpf_srh_states);
901 struct ipv6_sr_hdr *srh;
904 srh = get_and_validate_srh(skb);
909 advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
911 /* preempt_disable is needed to protect the per-CPU buffer srh_state,
912 * which is also accessed by the bpf_lwt_seg6_* helpers
915 srh_state->srh = srh;
916 srh_state->hdrlen = srh->hdrlen << 3;
917 srh_state->valid = true;
920 bpf_compute_data_pointers(skb);
921 ret = bpf_prog_run_save_cb(slwt->bpf.prog, skb);
931 pr_warn_once("bpf-seg6local: Illegal return value %u\n", ret);
935 if (srh_state->srh && !seg6_bpf_has_valid_srh(skb))
939 if (ret != BPF_REDIRECT)
940 seg6_lookup_nexthop(skb, NULL, 0);
942 return dst_input(skb);
950 static struct seg6_action_desc seg6_action_table[] = {
952 .action = SEG6_LOCAL_ACTION_END,
954 .optattrs = SEG6_F_LOCAL_COUNTERS,
955 .input = input_action_end,
958 .action = SEG6_LOCAL_ACTION_END_X,
959 .attrs = SEG6_F_ATTR(SEG6_LOCAL_NH6),
960 .optattrs = SEG6_F_LOCAL_COUNTERS,
961 .input = input_action_end_x,
964 .action = SEG6_LOCAL_ACTION_END_T,
965 .attrs = SEG6_F_ATTR(SEG6_LOCAL_TABLE),
966 .optattrs = SEG6_F_LOCAL_COUNTERS,
967 .input = input_action_end_t,
970 .action = SEG6_LOCAL_ACTION_END_DX2,
971 .attrs = SEG6_F_ATTR(SEG6_LOCAL_OIF),
972 .optattrs = SEG6_F_LOCAL_COUNTERS,
973 .input = input_action_end_dx2,
976 .action = SEG6_LOCAL_ACTION_END_DX6,
977 .attrs = SEG6_F_ATTR(SEG6_LOCAL_NH6),
978 .optattrs = SEG6_F_LOCAL_COUNTERS,
979 .input = input_action_end_dx6,
982 .action = SEG6_LOCAL_ACTION_END_DX4,
983 .attrs = SEG6_F_ATTR(SEG6_LOCAL_NH4),
984 .optattrs = SEG6_F_LOCAL_COUNTERS,
985 .input = input_action_end_dx4,
988 .action = SEG6_LOCAL_ACTION_END_DT4,
989 .attrs = SEG6_F_ATTR(SEG6_LOCAL_VRFTABLE),
990 .optattrs = SEG6_F_LOCAL_COUNTERS,
991 #ifdef CONFIG_NET_L3_MASTER_DEV
992 .input = input_action_end_dt4,
994 .build_state = seg6_end_dt4_build,
999 .action = SEG6_LOCAL_ACTION_END_DT6,
1000 #ifdef CONFIG_NET_L3_MASTER_DEV
1002 .optattrs = SEG6_F_LOCAL_COUNTERS |
1003 SEG6_F_ATTR(SEG6_LOCAL_TABLE) |
1004 SEG6_F_ATTR(SEG6_LOCAL_VRFTABLE),
1006 .build_state = seg6_end_dt6_build,
1009 .attrs = SEG6_F_ATTR(SEG6_LOCAL_TABLE),
1010 .optattrs = SEG6_F_LOCAL_COUNTERS,
1012 .input = input_action_end_dt6,
1015 .action = SEG6_LOCAL_ACTION_END_DT46,
1016 .attrs = SEG6_F_ATTR(SEG6_LOCAL_VRFTABLE),
1017 .optattrs = SEG6_F_LOCAL_COUNTERS,
1018 #ifdef CONFIG_NET_L3_MASTER_DEV
1019 .input = input_action_end_dt46,
1021 .build_state = seg6_end_dt46_build,
1026 .action = SEG6_LOCAL_ACTION_END_B6,
1027 .attrs = SEG6_F_ATTR(SEG6_LOCAL_SRH),
1028 .optattrs = SEG6_F_LOCAL_COUNTERS,
1029 .input = input_action_end_b6,
1032 .action = SEG6_LOCAL_ACTION_END_B6_ENCAP,
1033 .attrs = SEG6_F_ATTR(SEG6_LOCAL_SRH),
1034 .optattrs = SEG6_F_LOCAL_COUNTERS,
1035 .input = input_action_end_b6_encap,
1036 .static_headroom = sizeof(struct ipv6hdr),
1039 .action = SEG6_LOCAL_ACTION_END_BPF,
1040 .attrs = SEG6_F_ATTR(SEG6_LOCAL_BPF),
1041 .optattrs = SEG6_F_LOCAL_COUNTERS,
1042 .input = input_action_end_bpf,
1047 static struct seg6_action_desc *__get_action_desc(int action)
1049 struct seg6_action_desc *desc;
1052 count = ARRAY_SIZE(seg6_action_table);
1053 for (i = 0; i < count; i++) {
1054 desc = &seg6_action_table[i];
1055 if (desc->action == action)
1062 static bool seg6_lwtunnel_counters_enabled(struct seg6_local_lwt *slwt)
1064 return slwt->parsed_optattrs & SEG6_F_LOCAL_COUNTERS;
1067 static void seg6_local_update_counters(struct seg6_local_lwt *slwt,
1068 unsigned int len, int err)
1070 struct pcpu_seg6_local_counters *pcounters;
1072 pcounters = this_cpu_ptr(slwt->pcpu_counters);
1073 u64_stats_update_begin(&pcounters->syncp);
1076 u64_stats_inc(&pcounters->packets);
1077 u64_stats_add(&pcounters->bytes, len);
1079 u64_stats_inc(&pcounters->errors);
1082 u64_stats_update_end(&pcounters->syncp);
1085 static int seg6_local_input_core(struct net *net, struct sock *sk,
1086 struct sk_buff *skb)
1088 struct dst_entry *orig_dst = skb_dst(skb);
1089 struct seg6_action_desc *desc;
1090 struct seg6_local_lwt *slwt;
1091 unsigned int len = skb->len;
1094 slwt = seg6_local_lwtunnel(orig_dst->lwtstate);
1097 rc = desc->input(skb, slwt);
1099 if (!seg6_lwtunnel_counters_enabled(slwt))
1102 seg6_local_update_counters(slwt, len, rc);
1107 static int seg6_local_input(struct sk_buff *skb)
1109 if (skb->protocol != htons(ETH_P_IPV6)) {
1114 if (static_branch_unlikely(&nf_hooks_lwtunnel_enabled))
1115 return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_IN,
1116 dev_net(skb->dev), NULL, skb, skb->dev, NULL,
1117 seg6_local_input_core);
1119 return seg6_local_input_core(dev_net(skb->dev), NULL, skb);
1122 static const struct nla_policy seg6_local_policy[SEG6_LOCAL_MAX + 1] = {
1123 [SEG6_LOCAL_ACTION] = { .type = NLA_U32 },
1124 [SEG6_LOCAL_SRH] = { .type = NLA_BINARY },
1125 [SEG6_LOCAL_TABLE] = { .type = NLA_U32 },
1126 [SEG6_LOCAL_VRFTABLE] = { .type = NLA_U32 },
1127 [SEG6_LOCAL_NH4] = { .type = NLA_BINARY,
1128 .len = sizeof(struct in_addr) },
1129 [SEG6_LOCAL_NH6] = { .type = NLA_BINARY,
1130 .len = sizeof(struct in6_addr) },
1131 [SEG6_LOCAL_IIF] = { .type = NLA_U32 },
1132 [SEG6_LOCAL_OIF] = { .type = NLA_U32 },
1133 [SEG6_LOCAL_BPF] = { .type = NLA_NESTED },
1134 [SEG6_LOCAL_COUNTERS] = { .type = NLA_NESTED },
1137 static int parse_nla_srh(struct nlattr **attrs, struct seg6_local_lwt *slwt,
1138 struct netlink_ext_ack *extack)
1140 struct ipv6_sr_hdr *srh;
1143 srh = nla_data(attrs[SEG6_LOCAL_SRH]);
1144 len = nla_len(attrs[SEG6_LOCAL_SRH]);
1146 /* SRH must contain at least one segment */
1147 if (len < sizeof(*srh) + sizeof(struct in6_addr))
1150 if (!seg6_validate_srh(srh, len, false))
1153 slwt->srh = kmemdup(srh, len, GFP_KERNEL);
1157 slwt->headroom += len;
1162 static int put_nla_srh(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1164 struct ipv6_sr_hdr *srh;
1169 len = (srh->hdrlen + 1) << 3;
1171 nla = nla_reserve(skb, SEG6_LOCAL_SRH, len);
1175 memcpy(nla_data(nla), srh, len);
1180 static int cmp_nla_srh(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1182 int len = (a->srh->hdrlen + 1) << 3;
1184 if (len != ((b->srh->hdrlen + 1) << 3))
1187 return memcmp(a->srh, b->srh, len);
1190 static void destroy_attr_srh(struct seg6_local_lwt *slwt)
1195 static int parse_nla_table(struct nlattr **attrs, struct seg6_local_lwt *slwt,
1196 struct netlink_ext_ack *extack)
1198 slwt->table = nla_get_u32(attrs[SEG6_LOCAL_TABLE]);
1203 static int put_nla_table(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1205 if (nla_put_u32(skb, SEG6_LOCAL_TABLE, slwt->table))
1211 static int cmp_nla_table(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1213 if (a->table != b->table)
1220 seg6_end_dt_info *seg6_possible_end_dt_info(struct seg6_local_lwt *slwt)
1222 #ifdef CONFIG_NET_L3_MASTER_DEV
1223 return &slwt->dt_info;
1225 return ERR_PTR(-EOPNOTSUPP);
1229 static int parse_nla_vrftable(struct nlattr **attrs,
1230 struct seg6_local_lwt *slwt,
1231 struct netlink_ext_ack *extack)
1233 struct seg6_end_dt_info *info = seg6_possible_end_dt_info(slwt);
1236 return PTR_ERR(info);
1238 info->vrf_table = nla_get_u32(attrs[SEG6_LOCAL_VRFTABLE]);
1243 static int put_nla_vrftable(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1245 struct seg6_end_dt_info *info = seg6_possible_end_dt_info(slwt);
1248 return PTR_ERR(info);
1250 if (nla_put_u32(skb, SEG6_LOCAL_VRFTABLE, info->vrf_table))
1256 static int cmp_nla_vrftable(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1258 struct seg6_end_dt_info *info_a = seg6_possible_end_dt_info(a);
1259 struct seg6_end_dt_info *info_b = seg6_possible_end_dt_info(b);
1261 if (info_a->vrf_table != info_b->vrf_table)
1267 static int parse_nla_nh4(struct nlattr **attrs, struct seg6_local_lwt *slwt,
1268 struct netlink_ext_ack *extack)
1270 memcpy(&slwt->nh4, nla_data(attrs[SEG6_LOCAL_NH4]),
1271 sizeof(struct in_addr));
1276 static int put_nla_nh4(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1280 nla = nla_reserve(skb, SEG6_LOCAL_NH4, sizeof(struct in_addr));
1284 memcpy(nla_data(nla), &slwt->nh4, sizeof(struct in_addr));
1289 static int cmp_nla_nh4(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1291 return memcmp(&a->nh4, &b->nh4, sizeof(struct in_addr));
1294 static int parse_nla_nh6(struct nlattr **attrs, struct seg6_local_lwt *slwt,
1295 struct netlink_ext_ack *extack)
1297 memcpy(&slwt->nh6, nla_data(attrs[SEG6_LOCAL_NH6]),
1298 sizeof(struct in6_addr));
1303 static int put_nla_nh6(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1307 nla = nla_reserve(skb, SEG6_LOCAL_NH6, sizeof(struct in6_addr));
1311 memcpy(nla_data(nla), &slwt->nh6, sizeof(struct in6_addr));
1316 static int cmp_nla_nh6(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1318 return memcmp(&a->nh6, &b->nh6, sizeof(struct in6_addr));
1321 static int parse_nla_iif(struct nlattr **attrs, struct seg6_local_lwt *slwt,
1322 struct netlink_ext_ack *extack)
1324 slwt->iif = nla_get_u32(attrs[SEG6_LOCAL_IIF]);
1329 static int put_nla_iif(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1331 if (nla_put_u32(skb, SEG6_LOCAL_IIF, slwt->iif))
1337 static int cmp_nla_iif(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1339 if (a->iif != b->iif)
1345 static int parse_nla_oif(struct nlattr **attrs, struct seg6_local_lwt *slwt,
1346 struct netlink_ext_ack *extack)
1348 slwt->oif = nla_get_u32(attrs[SEG6_LOCAL_OIF]);
1353 static int put_nla_oif(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1355 if (nla_put_u32(skb, SEG6_LOCAL_OIF, slwt->oif))
1361 static int cmp_nla_oif(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1363 if (a->oif != b->oif)
1369 #define MAX_PROG_NAME 256
1370 static const struct nla_policy bpf_prog_policy[SEG6_LOCAL_BPF_PROG_MAX + 1] = {
1371 [SEG6_LOCAL_BPF_PROG] = { .type = NLA_U32, },
1372 [SEG6_LOCAL_BPF_PROG_NAME] = { .type = NLA_NUL_STRING,
1373 .len = MAX_PROG_NAME },
1376 static int parse_nla_bpf(struct nlattr **attrs, struct seg6_local_lwt *slwt,
1377 struct netlink_ext_ack *extack)
1379 struct nlattr *tb[SEG6_LOCAL_BPF_PROG_MAX + 1];
1384 ret = nla_parse_nested_deprecated(tb, SEG6_LOCAL_BPF_PROG_MAX,
1385 attrs[SEG6_LOCAL_BPF],
1386 bpf_prog_policy, NULL);
1390 if (!tb[SEG6_LOCAL_BPF_PROG] || !tb[SEG6_LOCAL_BPF_PROG_NAME])
1393 slwt->bpf.name = nla_memdup(tb[SEG6_LOCAL_BPF_PROG_NAME], GFP_KERNEL);
1394 if (!slwt->bpf.name)
1397 fd = nla_get_u32(tb[SEG6_LOCAL_BPF_PROG]);
1398 p = bpf_prog_get_type(fd, BPF_PROG_TYPE_LWT_SEG6LOCAL);
1400 kfree(slwt->bpf.name);
1408 static int put_nla_bpf(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1410 struct nlattr *nest;
1412 if (!slwt->bpf.prog)
1415 nest = nla_nest_start_noflag(skb, SEG6_LOCAL_BPF);
1419 if (nla_put_u32(skb, SEG6_LOCAL_BPF_PROG, slwt->bpf.prog->aux->id))
1422 if (slwt->bpf.name &&
1423 nla_put_string(skb, SEG6_LOCAL_BPF_PROG_NAME, slwt->bpf.name))
1426 return nla_nest_end(skb, nest);
1429 static int cmp_nla_bpf(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1431 if (!a->bpf.name && !b->bpf.name)
1434 if (!a->bpf.name || !b->bpf.name)
1437 return strcmp(a->bpf.name, b->bpf.name);
1440 static void destroy_attr_bpf(struct seg6_local_lwt *slwt)
1442 kfree(slwt->bpf.name);
1444 bpf_prog_put(slwt->bpf.prog);
1448 nla_policy seg6_local_counters_policy[SEG6_LOCAL_CNT_MAX + 1] = {
1449 [SEG6_LOCAL_CNT_PACKETS] = { .type = NLA_U64 },
1450 [SEG6_LOCAL_CNT_BYTES] = { .type = NLA_U64 },
1451 [SEG6_LOCAL_CNT_ERRORS] = { .type = NLA_U64 },
1454 static int parse_nla_counters(struct nlattr **attrs,
1455 struct seg6_local_lwt *slwt,
1456 struct netlink_ext_ack *extack)
1458 struct pcpu_seg6_local_counters __percpu *pcounters;
1459 struct nlattr *tb[SEG6_LOCAL_CNT_MAX + 1];
1462 ret = nla_parse_nested_deprecated(tb, SEG6_LOCAL_CNT_MAX,
1463 attrs[SEG6_LOCAL_COUNTERS],
1464 seg6_local_counters_policy, NULL);
1468 /* basic support for SRv6 Behavior counters requires at least:
1469 * packets, bytes and errors.
1471 if (!tb[SEG6_LOCAL_CNT_PACKETS] || !tb[SEG6_LOCAL_CNT_BYTES] ||
1472 !tb[SEG6_LOCAL_CNT_ERRORS])
1475 /* counters are always zero initialized */
1476 pcounters = seg6_local_alloc_pcpu_counters(GFP_KERNEL);
1480 slwt->pcpu_counters = pcounters;
1485 static int seg6_local_fill_nla_counters(struct sk_buff *skb,
1486 struct seg6_local_counters *counters)
1488 if (nla_put_u64_64bit(skb, SEG6_LOCAL_CNT_PACKETS, counters->packets,
1489 SEG6_LOCAL_CNT_PAD))
1492 if (nla_put_u64_64bit(skb, SEG6_LOCAL_CNT_BYTES, counters->bytes,
1493 SEG6_LOCAL_CNT_PAD))
1496 if (nla_put_u64_64bit(skb, SEG6_LOCAL_CNT_ERRORS, counters->errors,
1497 SEG6_LOCAL_CNT_PAD))
1503 static int put_nla_counters(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1505 struct seg6_local_counters counters = { 0, 0, 0 };
1506 struct nlattr *nest;
1509 nest = nla_nest_start(skb, SEG6_LOCAL_COUNTERS);
1513 for_each_possible_cpu(i) {
1514 struct pcpu_seg6_local_counters *pcounters;
1515 u64 packets, bytes, errors;
1518 pcounters = per_cpu_ptr(slwt->pcpu_counters, i);
1520 start = u64_stats_fetch_begin_irq(&pcounters->syncp);
1522 packets = u64_stats_read(&pcounters->packets);
1523 bytes = u64_stats_read(&pcounters->bytes);
1524 errors = u64_stats_read(&pcounters->errors);
1526 } while (u64_stats_fetch_retry_irq(&pcounters->syncp, start));
1528 counters.packets += packets;
1529 counters.bytes += bytes;
1530 counters.errors += errors;
1533 rc = seg6_local_fill_nla_counters(skb, &counters);
1535 nla_nest_cancel(skb, nest);
1539 return nla_nest_end(skb, nest);
1542 static int cmp_nla_counters(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1544 /* a and b are equal if both have pcpu_counters set or not */
1545 return (!!((unsigned long)a->pcpu_counters)) ^
1546 (!!((unsigned long)b->pcpu_counters));
1549 static void destroy_attr_counters(struct seg6_local_lwt *slwt)
1551 free_percpu(slwt->pcpu_counters);
1554 struct seg6_action_param {
1555 int (*parse)(struct nlattr **attrs, struct seg6_local_lwt *slwt,
1556 struct netlink_ext_ack *extack);
1557 int (*put)(struct sk_buff *skb, struct seg6_local_lwt *slwt);
1558 int (*cmp)(struct seg6_local_lwt *a, struct seg6_local_lwt *b);
1560 /* optional destroy() callback useful for releasing resources which
1561 * have been previously acquired in the corresponding parse()
1564 void (*destroy)(struct seg6_local_lwt *slwt);
1567 static struct seg6_action_param seg6_action_params[SEG6_LOCAL_MAX + 1] = {
1568 [SEG6_LOCAL_SRH] = { .parse = parse_nla_srh,
1571 .destroy = destroy_attr_srh },
1573 [SEG6_LOCAL_TABLE] = { .parse = parse_nla_table,
1574 .put = put_nla_table,
1575 .cmp = cmp_nla_table },
1577 [SEG6_LOCAL_NH4] = { .parse = parse_nla_nh4,
1579 .cmp = cmp_nla_nh4 },
1581 [SEG6_LOCAL_NH6] = { .parse = parse_nla_nh6,
1583 .cmp = cmp_nla_nh6 },
1585 [SEG6_LOCAL_IIF] = { .parse = parse_nla_iif,
1587 .cmp = cmp_nla_iif },
1589 [SEG6_LOCAL_OIF] = { .parse = parse_nla_oif,
1591 .cmp = cmp_nla_oif },
1593 [SEG6_LOCAL_BPF] = { .parse = parse_nla_bpf,
1596 .destroy = destroy_attr_bpf },
1598 [SEG6_LOCAL_VRFTABLE] = { .parse = parse_nla_vrftable,
1599 .put = put_nla_vrftable,
1600 .cmp = cmp_nla_vrftable },
1602 [SEG6_LOCAL_COUNTERS] = { .parse = parse_nla_counters,
1603 .put = put_nla_counters,
1604 .cmp = cmp_nla_counters,
1605 .destroy = destroy_attr_counters },
1608 /* call the destroy() callback (if available) for each set attribute in
1609 * @parsed_attrs, starting from the first attribute up to the @max_parsed
1610 * (excluded) attribute.
1612 static void __destroy_attrs(unsigned long parsed_attrs, int max_parsed,
1613 struct seg6_local_lwt *slwt)
1615 struct seg6_action_param *param;
1618 /* Every required seg6local attribute is identified by an ID which is
1619 * encoded as a flag (i.e: 1 << ID) in the 'attrs' bitmask;
1621 * We scan the 'parsed_attrs' bitmask, starting from the first attribute
1622 * up to the @max_parsed (excluded) attribute.
1623 * For each set attribute, we retrieve the corresponding destroy()
1624 * callback. If the callback is not available, then we skip to the next
1625 * attribute; otherwise, we call the destroy() callback.
1627 for (i = SEG6_LOCAL_SRH; i < max_parsed; ++i) {
1628 if (!(parsed_attrs & SEG6_F_ATTR(i)))
1631 param = &seg6_action_params[i];
1634 param->destroy(slwt);
1638 /* release all the resources that may have been acquired during parsing
1641 static void destroy_attrs(struct seg6_local_lwt *slwt)
1643 unsigned long attrs = slwt->desc->attrs | slwt->parsed_optattrs;
1645 __destroy_attrs(attrs, SEG6_LOCAL_MAX + 1, slwt);
1648 static int parse_nla_optional_attrs(struct nlattr **attrs,
1649 struct seg6_local_lwt *slwt,
1650 struct netlink_ext_ack *extack)
1652 struct seg6_action_desc *desc = slwt->desc;
1653 unsigned long parsed_optattrs = 0;
1654 struct seg6_action_param *param;
1657 for (i = SEG6_LOCAL_SRH; i < SEG6_LOCAL_MAX + 1; ++i) {
1658 if (!(desc->optattrs & SEG6_F_ATTR(i)) || !attrs[i])
1661 /* once here, the i-th attribute is provided by the
1662 * userspace AND it is identified optional as well.
1664 param = &seg6_action_params[i];
1666 err = param->parse(attrs, slwt, extack);
1668 goto parse_optattrs_err;
1670 /* current attribute has been correctly parsed */
1671 parsed_optattrs |= SEG6_F_ATTR(i);
1674 /* store in the tunnel state all the optional attributed successfully
1677 slwt->parsed_optattrs = parsed_optattrs;
1682 __destroy_attrs(parsed_optattrs, i, slwt);
1687 /* call the custom constructor of the behavior during its initialization phase
1688 * and after that all its attributes have been parsed successfully.
1691 seg6_local_lwtunnel_build_state(struct seg6_local_lwt *slwt, const void *cfg,
1692 struct netlink_ext_ack *extack)
1694 struct seg6_action_desc *desc = slwt->desc;
1695 struct seg6_local_lwtunnel_ops *ops;
1697 ops = &desc->slwt_ops;
1698 if (!ops->build_state)
1701 return ops->build_state(slwt, cfg, extack);
1704 /* call the custom destructor of the behavior which is invoked before the
1705 * tunnel is going to be destroyed.
1707 static void seg6_local_lwtunnel_destroy_state(struct seg6_local_lwt *slwt)
1709 struct seg6_action_desc *desc = slwt->desc;
1710 struct seg6_local_lwtunnel_ops *ops;
1712 ops = &desc->slwt_ops;
1713 if (!ops->destroy_state)
1716 ops->destroy_state(slwt);
1719 static int parse_nla_action(struct nlattr **attrs, struct seg6_local_lwt *slwt,
1720 struct netlink_ext_ack *extack)
1722 struct seg6_action_param *param;
1723 struct seg6_action_desc *desc;
1724 unsigned long invalid_attrs;
1727 desc = __get_action_desc(slwt->action);
1735 slwt->headroom += desc->static_headroom;
1737 /* Forcing the desc->optattrs *set* and the desc->attrs *set* to be
1738 * disjoined, this allow us to release acquired resources by optional
1739 * attributes and by required attributes independently from each other
1740 * without any interference.
1741 * In other terms, we are sure that we do not release some the acquired
1744 * Note that if an attribute is configured both as required and as
1745 * optional, it means that the user has messed something up in the
1746 * seg6_action_table. Therefore, this check is required for SRv6
1747 * behaviors to work properly.
1749 invalid_attrs = desc->attrs & desc->optattrs;
1750 if (invalid_attrs) {
1752 "An attribute cannot be both required AND optional");
1756 /* parse the required attributes */
1757 for (i = SEG6_LOCAL_SRH; i < SEG6_LOCAL_MAX + 1; i++) {
1758 if (desc->attrs & SEG6_F_ATTR(i)) {
1762 param = &seg6_action_params[i];
1764 err = param->parse(attrs, slwt, extack);
1766 goto parse_attrs_err;
1770 /* parse the optional attributes, if any */
1771 err = parse_nla_optional_attrs(attrs, slwt, extack);
1773 goto parse_attrs_err;
1778 /* release any resource that may have been acquired during the i-1
1779 * parse() operations.
1781 __destroy_attrs(desc->attrs, i, slwt);
1786 static int seg6_local_build_state(struct net *net, struct nlattr *nla,
1787 unsigned int family, const void *cfg,
1788 struct lwtunnel_state **ts,
1789 struct netlink_ext_ack *extack)
1791 struct nlattr *tb[SEG6_LOCAL_MAX + 1];
1792 struct lwtunnel_state *newts;
1793 struct seg6_local_lwt *slwt;
1796 if (family != AF_INET6)
1799 err = nla_parse_nested_deprecated(tb, SEG6_LOCAL_MAX, nla,
1800 seg6_local_policy, extack);
1805 if (!tb[SEG6_LOCAL_ACTION])
1808 newts = lwtunnel_state_alloc(sizeof(*slwt));
1812 slwt = seg6_local_lwtunnel(newts);
1813 slwt->action = nla_get_u32(tb[SEG6_LOCAL_ACTION]);
1815 err = parse_nla_action(tb, slwt, extack);
1819 err = seg6_local_lwtunnel_build_state(slwt, cfg, extack);
1821 goto out_destroy_attrs;
1823 newts->type = LWTUNNEL_ENCAP_SEG6_LOCAL;
1824 newts->flags = LWTUNNEL_STATE_INPUT_REDIRECT;
1825 newts->headroom = slwt->headroom;
1832 destroy_attrs(slwt);
1838 static void seg6_local_destroy_state(struct lwtunnel_state *lwt)
1840 struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
1842 seg6_local_lwtunnel_destroy_state(slwt);
1844 destroy_attrs(slwt);
1849 static int seg6_local_fill_encap(struct sk_buff *skb,
1850 struct lwtunnel_state *lwt)
1852 struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
1853 struct seg6_action_param *param;
1854 unsigned long attrs;
1857 if (nla_put_u32(skb, SEG6_LOCAL_ACTION, slwt->action))
1860 attrs = slwt->desc->attrs | slwt->parsed_optattrs;
1862 for (i = SEG6_LOCAL_SRH; i < SEG6_LOCAL_MAX + 1; i++) {
1863 if (attrs & SEG6_F_ATTR(i)) {
1864 param = &seg6_action_params[i];
1865 err = param->put(skb, slwt);
1874 static int seg6_local_get_encap_size(struct lwtunnel_state *lwt)
1876 struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
1877 unsigned long attrs;
1880 nlsize = nla_total_size(4); /* action */
1882 attrs = slwt->desc->attrs | slwt->parsed_optattrs;
1884 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_SRH))
1885 nlsize += nla_total_size((slwt->srh->hdrlen + 1) << 3);
1887 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_TABLE))
1888 nlsize += nla_total_size(4);
1890 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_NH4))
1891 nlsize += nla_total_size(4);
1893 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_NH6))
1894 nlsize += nla_total_size(16);
1896 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_IIF))
1897 nlsize += nla_total_size(4);
1899 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_OIF))
1900 nlsize += nla_total_size(4);
1902 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_BPF))
1903 nlsize += nla_total_size(sizeof(struct nlattr)) +
1904 nla_total_size(MAX_PROG_NAME) +
1907 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_VRFTABLE))
1908 nlsize += nla_total_size(4);
1910 if (attrs & SEG6_F_LOCAL_COUNTERS)
1911 nlsize += nla_total_size(0) + /* nest SEG6_LOCAL_COUNTERS */
1912 /* SEG6_LOCAL_CNT_PACKETS */
1913 nla_total_size_64bit(sizeof(__u64)) +
1914 /* SEG6_LOCAL_CNT_BYTES */
1915 nla_total_size_64bit(sizeof(__u64)) +
1916 /* SEG6_LOCAL_CNT_ERRORS */
1917 nla_total_size_64bit(sizeof(__u64));
1922 static int seg6_local_cmp_encap(struct lwtunnel_state *a,
1923 struct lwtunnel_state *b)
1925 struct seg6_local_lwt *slwt_a, *slwt_b;
1926 struct seg6_action_param *param;
1927 unsigned long attrs_a, attrs_b;
1930 slwt_a = seg6_local_lwtunnel(a);
1931 slwt_b = seg6_local_lwtunnel(b);
1933 if (slwt_a->action != slwt_b->action)
1936 attrs_a = slwt_a->desc->attrs | slwt_a->parsed_optattrs;
1937 attrs_b = slwt_b->desc->attrs | slwt_b->parsed_optattrs;
1939 if (attrs_a != attrs_b)
1942 for (i = SEG6_LOCAL_SRH; i < SEG6_LOCAL_MAX + 1; i++) {
1943 if (attrs_a & SEG6_F_ATTR(i)) {
1944 param = &seg6_action_params[i];
1945 if (param->cmp(slwt_a, slwt_b))
1953 static const struct lwtunnel_encap_ops seg6_local_ops = {
1954 .build_state = seg6_local_build_state,
1955 .destroy_state = seg6_local_destroy_state,
1956 .input = seg6_local_input,
1957 .fill_encap = seg6_local_fill_encap,
1958 .get_encap_size = seg6_local_get_encap_size,
1959 .cmp_encap = seg6_local_cmp_encap,
1960 .owner = THIS_MODULE,
1963 int __init seg6_local_init(void)
1965 /* If the max total number of defined attributes is reached, then your
1966 * kernel build stops here.
1968 * This check is required to avoid arithmetic overflows when processing
1969 * behavior attributes and the maximum number of defined attributes
1970 * exceeds the allowed value.
1972 BUILD_BUG_ON(SEG6_LOCAL_MAX + 1 > BITS_PER_TYPE(unsigned long));
1974 return lwtunnel_encap_add_ops(&seg6_local_ops,
1975 LWTUNNEL_ENCAP_SEG6_LOCAL);
1978 void seg6_local_exit(void)
1980 lwtunnel_encap_del_ops(&seg6_local_ops, LWTUNNEL_ENCAP_SEG6_LOCAL);