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 /* default length values (expressed in bits) for both Locator-Block and
77 * Locator-Node Function.
79 * Both SEG6_LOCAL_LCBLOCK_DBITS and SEG6_LOCAL_LCNODE_FN_DBITS *must* be:
81 * ii) evenly divisible by 8. In other terms, the lengths of the
82 * Locator-Block and Locator-Node Function must be byte-aligned (we can
83 * relax this constraint in the future if really needed).
85 * Moreover, a third condition must hold:
86 * iii) SEG6_LOCAL_LCBLOCK_DBITS + SEG6_LOCAL_LCNODE_FN_DBITS <= 128.
88 * The correctness of SEG6_LOCAL_LCBLOCK_DBITS and SEG6_LOCAL_LCNODE_FN_DBITS
89 * values are checked during the kernel compilation. If the compilation stops,
90 * check the value of these parameters to see if they meet conditions (i), (ii)
93 #define SEG6_LOCAL_LCBLOCK_DBITS 32
94 #define SEG6_LOCAL_LCNODE_FN_DBITS 16
96 /* The following next_csid_chk_{cntr,lcblock,lcblock_fn}_bits macros can be
97 * used directly to check whether the lengths (in bits) of Locator-Block and
98 * Locator-Node Function are valid according to (i), (ii), (iii).
100 #define next_csid_chk_cntr_bits(blen, flen) \
101 ((blen) + (flen) > 128)
103 #define next_csid_chk_lcblock_bits(blen) \
105 typeof(blen) __tmp = blen; \
106 (!__tmp || __tmp > 120 || (__tmp & 0x07)); \
109 #define next_csid_chk_lcnode_fn_bits(flen) \
110 next_csid_chk_lcblock_bits(flen)
112 /* Supported Flavor operations are reported in this bitmask */
113 #define SEG6_LOCAL_FLV_SUPP_OPS (BIT(SEG6_LOCAL_FLV_OP_NEXT_CSID))
115 struct seg6_flavors_info {
116 /* Flavor operations */
119 /* Locator-Block length, expressed in bits */
121 /* Locator-Node Function length, expressed in bits*/
122 __u8 lcnode_func_bits;
125 enum seg6_end_dt_mode {
126 DT_INVALID_MODE = -EINVAL,
131 struct seg6_end_dt_info {
132 enum seg6_end_dt_mode mode;
135 /* VRF device associated to the routing table used by the SRv6
136 * End.DT4/DT6 behavior for routing IPv4/IPv6 packets.
141 /* tunneled packet family (IPv4 or IPv6).
142 * Protocol and header length are inferred from family.
147 struct pcpu_seg6_local_counters {
152 struct u64_stats_sync syncp;
155 /* This struct groups all the SRv6 Behavior counters supported so far.
157 * put_nla_counters() makes use of this data structure to collect all counter
158 * values after the per-CPU counter evaluation has been performed.
159 * Finally, each counter value (in seg6_local_counters) is stored in the
160 * corresponding netlink attribute and sent to user space.
162 * NB: we don't want to expose this structure to user space!
164 struct seg6_local_counters {
170 #define seg6_local_alloc_pcpu_counters(__gfp) \
171 __netdev_alloc_pcpu_stats(struct pcpu_seg6_local_counters, \
172 ((__gfp) | __GFP_ZERO))
174 #define SEG6_F_LOCAL_COUNTERS SEG6_F_ATTR(SEG6_LOCAL_COUNTERS)
176 struct seg6_local_lwt {
178 struct ipv6_sr_hdr *srh;
184 struct bpf_lwt_prog bpf;
185 #ifdef CONFIG_NET_L3_MASTER_DEV
186 struct seg6_end_dt_info dt_info;
188 struct seg6_flavors_info flv_info;
190 struct pcpu_seg6_local_counters __percpu *pcpu_counters;
193 struct seg6_action_desc *desc;
194 /* unlike the required attrs, we have to track the optional attributes
195 * that have been effectively parsed.
197 unsigned long parsed_optattrs;
200 static struct seg6_local_lwt *seg6_local_lwtunnel(struct lwtunnel_state *lwt)
202 return (struct seg6_local_lwt *)lwt->data;
205 static struct ipv6_sr_hdr *get_and_validate_srh(struct sk_buff *skb)
207 struct ipv6_sr_hdr *srh;
209 srh = seg6_get_srh(skb, IP6_FH_F_SKIP_RH);
213 #ifdef CONFIG_IPV6_SEG6_HMAC
214 if (!seg6_hmac_validate_skb(skb))
221 static bool decap_and_validate(struct sk_buff *skb, int proto)
223 struct ipv6_sr_hdr *srh;
224 unsigned int off = 0;
226 srh = seg6_get_srh(skb, 0);
227 if (srh && srh->segments_left > 0)
230 #ifdef CONFIG_IPV6_SEG6_HMAC
231 if (srh && !seg6_hmac_validate_skb(skb))
235 if (ipv6_find_hdr(skb, &off, proto, NULL, NULL) < 0)
238 if (!pskb_pull(skb, off))
241 skb_postpull_rcsum(skb, skb_network_header(skb), off);
243 skb_reset_network_header(skb);
244 skb_reset_transport_header(skb);
245 if (iptunnel_pull_offloads(skb))
251 static void advance_nextseg(struct ipv6_sr_hdr *srh, struct in6_addr *daddr)
253 struct in6_addr *addr;
255 srh->segments_left--;
256 addr = srh->segments + srh->segments_left;
261 seg6_lookup_any_nexthop(struct sk_buff *skb, struct in6_addr *nhaddr,
262 u32 tbl_id, bool local_delivery)
264 struct net *net = dev_net(skb->dev);
265 struct ipv6hdr *hdr = ipv6_hdr(skb);
266 int flags = RT6_LOOKUP_F_HAS_SADDR;
267 struct dst_entry *dst = NULL;
272 memset(&fl6, 0, sizeof(fl6));
273 fl6.flowi6_iif = skb->dev->ifindex;
274 fl6.daddr = nhaddr ? *nhaddr : hdr->daddr;
275 fl6.saddr = hdr->saddr;
276 fl6.flowlabel = ip6_flowinfo(hdr);
277 fl6.flowi6_mark = skb->mark;
278 fl6.flowi6_proto = hdr->nexthdr;
281 fl6.flowi6_flags = FLOWI_FLAG_KNOWN_NH;
284 dst = ip6_route_input_lookup(net, skb->dev, &fl6, skb, flags);
286 struct fib6_table *table;
288 table = fib6_get_table(net, tbl_id);
292 rt = ip6_pol_route(net, table, 0, &fl6, skb, flags);
296 /* we want to discard traffic destined for local packet processing,
297 * if @local_delivery is set to false.
300 dev_flags |= IFF_LOOPBACK;
302 if (dst && (dst->dev->flags & dev_flags) && !dst->error) {
309 rt = net->ipv6.ip6_blk_hole_entry;
315 skb_dst_set(skb, dst);
319 int seg6_lookup_nexthop(struct sk_buff *skb,
320 struct in6_addr *nhaddr, u32 tbl_id)
322 return seg6_lookup_any_nexthop(skb, nhaddr, tbl_id, false);
325 static __u8 seg6_flv_lcblock_octects(const struct seg6_flavors_info *finfo)
327 return finfo->lcblock_bits >> 3;
330 static __u8 seg6_flv_lcnode_func_octects(const struct seg6_flavors_info *finfo)
332 return finfo->lcnode_func_bits >> 3;
335 static bool seg6_next_csid_is_arg_zero(const struct in6_addr *addr,
336 const struct seg6_flavors_info *finfo)
338 __u8 fnc_octects = seg6_flv_lcnode_func_octects(finfo);
339 __u8 blk_octects = seg6_flv_lcblock_octects(finfo);
343 arg_octects = 16 - blk_octects - fnc_octects;
344 for (i = 0; i < arg_octects; ++i) {
345 if (addr->s6_addr[blk_octects + fnc_octects + i] != 0x00)
352 /* assume that DA.Argument length > 0 */
353 static void seg6_next_csid_advance_arg(struct in6_addr *addr,
354 const struct seg6_flavors_info *finfo)
356 __u8 fnc_octects = seg6_flv_lcnode_func_octects(finfo);
357 __u8 blk_octects = seg6_flv_lcblock_octects(finfo);
359 /* advance DA.Argument */
360 memmove(&addr->s6_addr[blk_octects],
361 &addr->s6_addr[blk_octects + fnc_octects],
362 16 - blk_octects - fnc_octects);
364 memset(&addr->s6_addr[16 - fnc_octects], 0x00, fnc_octects);
367 static int input_action_end_core(struct sk_buff *skb,
368 struct seg6_local_lwt *slwt)
370 struct ipv6_sr_hdr *srh;
372 srh = get_and_validate_srh(skb);
376 advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
378 seg6_lookup_nexthop(skb, NULL, 0);
380 return dst_input(skb);
387 static int end_next_csid_core(struct sk_buff *skb, struct seg6_local_lwt *slwt)
389 const struct seg6_flavors_info *finfo = &slwt->flv_info;
390 struct in6_addr *daddr = &ipv6_hdr(skb)->daddr;
392 if (seg6_next_csid_is_arg_zero(daddr, finfo))
393 return input_action_end_core(skb, slwt);
396 seg6_next_csid_advance_arg(daddr, finfo);
398 seg6_lookup_nexthop(skb, NULL, 0);
400 return dst_input(skb);
403 static bool seg6_next_csid_enabled(__u32 fops)
405 return fops & BIT(SEG6_LOCAL_FLV_OP_NEXT_CSID);
408 /* regular endpoint function */
409 static int input_action_end(struct sk_buff *skb, struct seg6_local_lwt *slwt)
411 const struct seg6_flavors_info *finfo = &slwt->flv_info;
413 if (seg6_next_csid_enabled(finfo->flv_ops))
414 return end_next_csid_core(skb, slwt);
416 return input_action_end_core(skb, slwt);
419 /* regular endpoint, and forward to specified nexthop */
420 static int input_action_end_x(struct sk_buff *skb, struct seg6_local_lwt *slwt)
422 struct ipv6_sr_hdr *srh;
424 srh = get_and_validate_srh(skb);
428 advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
430 seg6_lookup_nexthop(skb, &slwt->nh6, 0);
432 return dst_input(skb);
439 static int input_action_end_t(struct sk_buff *skb, struct seg6_local_lwt *slwt)
441 struct ipv6_sr_hdr *srh;
443 srh = get_and_validate_srh(skb);
447 advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
449 seg6_lookup_nexthop(skb, NULL, slwt->table);
451 return dst_input(skb);
458 /* decapsulate and forward inner L2 frame on specified interface */
459 static int input_action_end_dx2(struct sk_buff *skb,
460 struct seg6_local_lwt *slwt)
462 struct net *net = dev_net(skb->dev);
463 struct net_device *odev;
466 if (!decap_and_validate(skb, IPPROTO_ETHERNET))
469 if (!pskb_may_pull(skb, ETH_HLEN))
472 skb_reset_mac_header(skb);
473 eth = (struct ethhdr *)skb->data;
475 /* To determine the frame's protocol, we assume it is 802.3. This avoids
476 * a call to eth_type_trans(), which is not really relevant for our
479 if (!eth_proto_is_802_3(eth->h_proto))
482 odev = dev_get_by_index_rcu(net, slwt->oif);
486 /* As we accept Ethernet frames, make sure the egress device is of
489 if (odev->type != ARPHRD_ETHER)
492 if (!(odev->flags & IFF_UP) || !netif_carrier_ok(odev))
497 if (skb_warn_if_lro(skb))
500 skb_forward_csum(skb);
502 if (skb->len - ETH_HLEN > odev->mtu)
506 skb->protocol = eth->h_proto;
508 return dev_queue_xmit(skb);
515 static int input_action_end_dx6_finish(struct net *net, struct sock *sk,
518 struct dst_entry *orig_dst = skb_dst(skb);
519 struct in6_addr *nhaddr = NULL;
520 struct seg6_local_lwt *slwt;
522 slwt = seg6_local_lwtunnel(orig_dst->lwtstate);
524 /* The inner packet is not associated to any local interface,
525 * so we do not call netif_rx().
527 * If slwt->nh6 is set to ::, then lookup the nexthop for the
528 * inner packet's DA. Otherwise, use the specified nexthop.
530 if (!ipv6_addr_any(&slwt->nh6))
533 seg6_lookup_nexthop(skb, nhaddr, 0);
535 return dst_input(skb);
538 /* decapsulate and forward to specified nexthop */
539 static int input_action_end_dx6(struct sk_buff *skb,
540 struct seg6_local_lwt *slwt)
542 /* this function accepts IPv6 encapsulated packets, with either
543 * an SRH with SL=0, or no SRH.
546 if (!decap_and_validate(skb, IPPROTO_IPV6))
549 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
552 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
555 if (static_branch_unlikely(&nf_hooks_lwtunnel_enabled))
556 return NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING,
557 dev_net(skb->dev), NULL, skb, NULL,
558 skb_dst(skb)->dev, input_action_end_dx6_finish);
560 return input_action_end_dx6_finish(dev_net(skb->dev), NULL, skb);
566 static int input_action_end_dx4_finish(struct net *net, struct sock *sk,
569 struct dst_entry *orig_dst = skb_dst(skb);
570 struct seg6_local_lwt *slwt;
575 slwt = seg6_local_lwtunnel(orig_dst->lwtstate);
579 nhaddr = slwt->nh4.s_addr ?: iph->daddr;
583 err = ip_route_input(skb, nhaddr, iph->saddr, 0, skb->dev);
589 return dst_input(skb);
592 static int input_action_end_dx4(struct sk_buff *skb,
593 struct seg6_local_lwt *slwt)
595 if (!decap_and_validate(skb, IPPROTO_IPIP))
598 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
601 skb->protocol = htons(ETH_P_IP);
602 skb_set_transport_header(skb, sizeof(struct iphdr));
605 if (static_branch_unlikely(&nf_hooks_lwtunnel_enabled))
606 return NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING,
607 dev_net(skb->dev), NULL, skb, NULL,
608 skb_dst(skb)->dev, input_action_end_dx4_finish);
610 return input_action_end_dx4_finish(dev_net(skb->dev), NULL, skb);
616 #ifdef CONFIG_NET_L3_MASTER_DEV
617 static struct net *fib6_config_get_net(const struct fib6_config *fib6_cfg)
619 const struct nl_info *nli = &fib6_cfg->fc_nlinfo;
624 static int __seg6_end_dt_vrf_build(struct seg6_local_lwt *slwt, const void *cfg,
625 u16 family, struct netlink_ext_ack *extack)
627 struct seg6_end_dt_info *info = &slwt->dt_info;
631 net = fib6_config_get_net(cfg);
633 /* note that vrf_table was already set by parse_nla_vrftable() */
634 vrf_ifindex = l3mdev_ifindex_lookup_by_table_id(L3MDEV_TYPE_VRF, net,
636 if (vrf_ifindex < 0) {
637 if (vrf_ifindex == -EPERM) {
638 NL_SET_ERR_MSG(extack,
639 "Strict mode for VRF is disabled");
640 } else if (vrf_ifindex == -ENODEV) {
641 NL_SET_ERR_MSG(extack,
642 "Table has no associated VRF device");
644 pr_debug("seg6local: SRv6 End.DT* creation error=%d\n",
652 info->vrf_ifindex = vrf_ifindex;
654 info->family = family;
655 info->mode = DT_VRF_MODE;
660 /* The SRv6 End.DT4/DT6 behavior extracts the inner (IPv4/IPv6) packet and
661 * routes the IPv4/IPv6 packet by looking at the configured routing table.
663 * In the SRv6 End.DT4/DT6 use case, we can receive traffic (IPv6+Segment
664 * Routing Header packets) from several interfaces and the outer IPv6
665 * destination address (DA) is used for retrieving the specific instance of the
666 * End.DT4/DT6 behavior that should process the packets.
668 * However, the inner IPv4/IPv6 packet is not really bound to any receiving
669 * interface and thus the End.DT4/DT6 sets the VRF (associated with the
670 * corresponding routing table) as the *receiving* interface.
671 * In other words, the End.DT4/DT6 processes a packet as if it has been received
672 * directly by the VRF (and not by one of its slave devices, if any).
673 * In this way, the VRF interface is used for routing the IPv4/IPv6 packet in
674 * according to the routing table configured by the End.DT4/DT6 instance.
676 * This design allows you to get some interesting features like:
677 * 1) the statistics on rx packets;
678 * 2) the possibility to install a packet sniffer on the receiving interface
679 * (the VRF one) for looking at the incoming packets;
680 * 3) the possibility to leverage the netfilter prerouting hook for the inner
683 * This function returns:
684 * - the sk_buff* when the VRF rcv handler has processed the packet correctly;
685 * - NULL when the skb is consumed by the VRF rcv handler;
686 * - a pointer which encodes a negative error number in case of error.
687 * Note that in this case, the function takes care of freeing the skb.
689 static struct sk_buff *end_dt_vrf_rcv(struct sk_buff *skb, u16 family,
690 struct net_device *dev)
692 /* based on l3mdev_ip_rcv; we are only interested in the master */
693 if (unlikely(!netif_is_l3_master(dev) && !netif_has_l3_rx_handler(dev)))
696 if (unlikely(!dev->l3mdev_ops->l3mdev_l3_rcv))
699 /* the decap packet IPv4/IPv6 does not come with any mac header info.
700 * We must unset the mac header to allow the VRF device to rebuild it,
701 * just in case there is a sniffer attached on the device.
703 skb_unset_mac_header(skb);
705 skb = dev->l3mdev_ops->l3mdev_l3_rcv(dev, skb, family);
707 /* the skb buffer was consumed by the handler */
710 /* when a packet is received by a VRF or by one of its slaves, the
711 * master device reference is set into the skb.
713 if (unlikely(skb->dev != dev || skb->skb_iif != dev->ifindex))
720 return ERR_PTR(-EINVAL);
723 static struct net_device *end_dt_get_vrf_rcu(struct sk_buff *skb,
724 struct seg6_end_dt_info *info)
726 int vrf_ifindex = info->vrf_ifindex;
727 struct net *net = info->net;
729 if (unlikely(vrf_ifindex < 0))
732 if (unlikely(!net_eq(dev_net(skb->dev), net)))
735 return dev_get_by_index_rcu(net, vrf_ifindex);
741 static struct sk_buff *end_dt_vrf_core(struct sk_buff *skb,
742 struct seg6_local_lwt *slwt, u16 family)
744 struct seg6_end_dt_info *info = &slwt->dt_info;
745 struct net_device *vrf;
749 vrf = end_dt_get_vrf_rcu(skb, info);
755 protocol = htons(ETH_P_IP);
756 hdrlen = sizeof(struct iphdr);
759 protocol = htons(ETH_P_IPV6);
760 hdrlen = sizeof(struct ipv6hdr);
768 if (unlikely(info->family != AF_UNSPEC && info->family != family)) {
769 pr_warn_once("seg6local: SRv6 End.DT* family mismatch");
773 skb->protocol = protocol;
777 skb_set_transport_header(skb, hdrlen);
780 return end_dt_vrf_rcv(skb, family, vrf);
784 return ERR_PTR(-EINVAL);
787 static int input_action_end_dt4(struct sk_buff *skb,
788 struct seg6_local_lwt *slwt)
793 if (!decap_and_validate(skb, IPPROTO_IPIP))
796 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
799 skb = end_dt_vrf_core(skb, slwt, AF_INET);
801 /* packet has been processed and consumed by the VRF */
809 err = ip_route_input(skb, iph->daddr, iph->saddr, 0, skb->dev);
813 return dst_input(skb);
820 static int seg6_end_dt4_build(struct seg6_local_lwt *slwt, const void *cfg,
821 struct netlink_ext_ack *extack)
823 return __seg6_end_dt_vrf_build(slwt, cfg, AF_INET, extack);
827 seg6_end_dt_mode seg6_end_dt6_parse_mode(struct seg6_local_lwt *slwt)
829 unsigned long parsed_optattrs = slwt->parsed_optattrs;
830 bool legacy, vrfmode;
832 legacy = !!(parsed_optattrs & SEG6_F_ATTR(SEG6_LOCAL_TABLE));
833 vrfmode = !!(parsed_optattrs & SEG6_F_ATTR(SEG6_LOCAL_VRFTABLE));
835 if (!(legacy ^ vrfmode))
836 /* both are absent or present: invalid DT6 mode */
837 return DT_INVALID_MODE;
839 return legacy ? DT_LEGACY_MODE : DT_VRF_MODE;
842 static enum seg6_end_dt_mode seg6_end_dt6_get_mode(struct seg6_local_lwt *slwt)
844 struct seg6_end_dt_info *info = &slwt->dt_info;
849 static int seg6_end_dt6_build(struct seg6_local_lwt *slwt, const void *cfg,
850 struct netlink_ext_ack *extack)
852 enum seg6_end_dt_mode mode = seg6_end_dt6_parse_mode(slwt);
853 struct seg6_end_dt_info *info = &slwt->dt_info;
857 info->mode = DT_LEGACY_MODE;
860 return __seg6_end_dt_vrf_build(slwt, cfg, AF_INET6, extack);
862 NL_SET_ERR_MSG(extack, "table or vrftable must be specified");
868 static int input_action_end_dt6(struct sk_buff *skb,
869 struct seg6_local_lwt *slwt)
871 if (!decap_and_validate(skb, IPPROTO_IPV6))
874 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
877 #ifdef CONFIG_NET_L3_MASTER_DEV
878 if (seg6_end_dt6_get_mode(slwt) == DT_LEGACY_MODE)
882 skb = end_dt_vrf_core(skb, slwt, AF_INET6);
884 /* packet has been processed and consumed by the VRF */
890 /* note: this time we do not need to specify the table because the VRF
891 * takes care of selecting the correct table.
893 seg6_lookup_any_nexthop(skb, NULL, 0, true);
895 return dst_input(skb);
899 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
901 seg6_lookup_any_nexthop(skb, NULL, slwt->table, true);
903 return dst_input(skb);
910 #ifdef CONFIG_NET_L3_MASTER_DEV
911 static int seg6_end_dt46_build(struct seg6_local_lwt *slwt, const void *cfg,
912 struct netlink_ext_ack *extack)
914 return __seg6_end_dt_vrf_build(slwt, cfg, AF_UNSPEC, extack);
917 static int input_action_end_dt46(struct sk_buff *skb,
918 struct seg6_local_lwt *slwt)
920 unsigned int off = 0;
923 nexthdr = ipv6_find_hdr(skb, &off, -1, NULL, NULL);
924 if (unlikely(nexthdr < 0))
929 return input_action_end_dt4(skb, slwt);
931 return input_action_end_dt6(skb, slwt);
940 /* push an SRH on top of the current one */
941 static int input_action_end_b6(struct sk_buff *skb, struct seg6_local_lwt *slwt)
943 struct ipv6_sr_hdr *srh;
946 srh = get_and_validate_srh(skb);
950 err = seg6_do_srh_inline(skb, slwt->srh);
954 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
956 seg6_lookup_nexthop(skb, NULL, 0);
958 return dst_input(skb);
965 /* encapsulate within an outer IPv6 header and a specified SRH */
966 static int input_action_end_b6_encap(struct sk_buff *skb,
967 struct seg6_local_lwt *slwt)
969 struct ipv6_sr_hdr *srh;
972 srh = get_and_validate_srh(skb);
976 advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
978 skb_reset_inner_headers(skb);
979 skb->encapsulation = 1;
981 err = seg6_do_srh_encap(skb, slwt->srh, IPPROTO_IPV6);
985 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
987 seg6_lookup_nexthop(skb, NULL, 0);
989 return dst_input(skb);
996 DEFINE_PER_CPU(struct seg6_bpf_srh_state, seg6_bpf_srh_states);
998 bool seg6_bpf_has_valid_srh(struct sk_buff *skb)
1000 struct seg6_bpf_srh_state *srh_state =
1001 this_cpu_ptr(&seg6_bpf_srh_states);
1002 struct ipv6_sr_hdr *srh = srh_state->srh;
1004 if (unlikely(srh == NULL))
1007 if (unlikely(!srh_state->valid)) {
1008 if ((srh_state->hdrlen & 7) != 0)
1011 srh->hdrlen = (u8)(srh_state->hdrlen >> 3);
1012 if (!seg6_validate_srh(srh, (srh->hdrlen + 1) << 3, true))
1015 srh_state->valid = true;
1021 static int input_action_end_bpf(struct sk_buff *skb,
1022 struct seg6_local_lwt *slwt)
1024 struct seg6_bpf_srh_state *srh_state =
1025 this_cpu_ptr(&seg6_bpf_srh_states);
1026 struct ipv6_sr_hdr *srh;
1029 srh = get_and_validate_srh(skb);
1034 advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
1036 /* preempt_disable is needed to protect the per-CPU buffer srh_state,
1037 * which is also accessed by the bpf_lwt_seg6_* helpers
1040 srh_state->srh = srh;
1041 srh_state->hdrlen = srh->hdrlen << 3;
1042 srh_state->valid = true;
1045 bpf_compute_data_pointers(skb);
1046 ret = bpf_prog_run_save_cb(slwt->bpf.prog, skb);
1056 pr_warn_once("bpf-seg6local: Illegal return value %u\n", ret);
1060 if (srh_state->srh && !seg6_bpf_has_valid_srh(skb))
1064 if (ret != BPF_REDIRECT)
1065 seg6_lookup_nexthop(skb, NULL, 0);
1067 return dst_input(skb);
1075 static struct seg6_action_desc seg6_action_table[] = {
1077 .action = SEG6_LOCAL_ACTION_END,
1079 .optattrs = SEG6_F_LOCAL_COUNTERS |
1080 SEG6_F_ATTR(SEG6_LOCAL_FLAVORS),
1081 .input = input_action_end,
1084 .action = SEG6_LOCAL_ACTION_END_X,
1085 .attrs = SEG6_F_ATTR(SEG6_LOCAL_NH6),
1086 .optattrs = SEG6_F_LOCAL_COUNTERS,
1087 .input = input_action_end_x,
1090 .action = SEG6_LOCAL_ACTION_END_T,
1091 .attrs = SEG6_F_ATTR(SEG6_LOCAL_TABLE),
1092 .optattrs = SEG6_F_LOCAL_COUNTERS,
1093 .input = input_action_end_t,
1096 .action = SEG6_LOCAL_ACTION_END_DX2,
1097 .attrs = SEG6_F_ATTR(SEG6_LOCAL_OIF),
1098 .optattrs = SEG6_F_LOCAL_COUNTERS,
1099 .input = input_action_end_dx2,
1102 .action = SEG6_LOCAL_ACTION_END_DX6,
1103 .attrs = SEG6_F_ATTR(SEG6_LOCAL_NH6),
1104 .optattrs = SEG6_F_LOCAL_COUNTERS,
1105 .input = input_action_end_dx6,
1108 .action = SEG6_LOCAL_ACTION_END_DX4,
1109 .attrs = SEG6_F_ATTR(SEG6_LOCAL_NH4),
1110 .optattrs = SEG6_F_LOCAL_COUNTERS,
1111 .input = input_action_end_dx4,
1114 .action = SEG6_LOCAL_ACTION_END_DT4,
1115 .attrs = SEG6_F_ATTR(SEG6_LOCAL_VRFTABLE),
1116 .optattrs = SEG6_F_LOCAL_COUNTERS,
1117 #ifdef CONFIG_NET_L3_MASTER_DEV
1118 .input = input_action_end_dt4,
1120 .build_state = seg6_end_dt4_build,
1125 .action = SEG6_LOCAL_ACTION_END_DT6,
1126 #ifdef CONFIG_NET_L3_MASTER_DEV
1128 .optattrs = SEG6_F_LOCAL_COUNTERS |
1129 SEG6_F_ATTR(SEG6_LOCAL_TABLE) |
1130 SEG6_F_ATTR(SEG6_LOCAL_VRFTABLE),
1132 .build_state = seg6_end_dt6_build,
1135 .attrs = SEG6_F_ATTR(SEG6_LOCAL_TABLE),
1136 .optattrs = SEG6_F_LOCAL_COUNTERS,
1138 .input = input_action_end_dt6,
1141 .action = SEG6_LOCAL_ACTION_END_DT46,
1142 .attrs = SEG6_F_ATTR(SEG6_LOCAL_VRFTABLE),
1143 .optattrs = SEG6_F_LOCAL_COUNTERS,
1144 #ifdef CONFIG_NET_L3_MASTER_DEV
1145 .input = input_action_end_dt46,
1147 .build_state = seg6_end_dt46_build,
1152 .action = SEG6_LOCAL_ACTION_END_B6,
1153 .attrs = SEG6_F_ATTR(SEG6_LOCAL_SRH),
1154 .optattrs = SEG6_F_LOCAL_COUNTERS,
1155 .input = input_action_end_b6,
1158 .action = SEG6_LOCAL_ACTION_END_B6_ENCAP,
1159 .attrs = SEG6_F_ATTR(SEG6_LOCAL_SRH),
1160 .optattrs = SEG6_F_LOCAL_COUNTERS,
1161 .input = input_action_end_b6_encap,
1162 .static_headroom = sizeof(struct ipv6hdr),
1165 .action = SEG6_LOCAL_ACTION_END_BPF,
1166 .attrs = SEG6_F_ATTR(SEG6_LOCAL_BPF),
1167 .optattrs = SEG6_F_LOCAL_COUNTERS,
1168 .input = input_action_end_bpf,
1173 static struct seg6_action_desc *__get_action_desc(int action)
1175 struct seg6_action_desc *desc;
1178 count = ARRAY_SIZE(seg6_action_table);
1179 for (i = 0; i < count; i++) {
1180 desc = &seg6_action_table[i];
1181 if (desc->action == action)
1188 static bool seg6_lwtunnel_counters_enabled(struct seg6_local_lwt *slwt)
1190 return slwt->parsed_optattrs & SEG6_F_LOCAL_COUNTERS;
1193 static void seg6_local_update_counters(struct seg6_local_lwt *slwt,
1194 unsigned int len, int err)
1196 struct pcpu_seg6_local_counters *pcounters;
1198 pcounters = this_cpu_ptr(slwt->pcpu_counters);
1199 u64_stats_update_begin(&pcounters->syncp);
1202 u64_stats_inc(&pcounters->packets);
1203 u64_stats_add(&pcounters->bytes, len);
1205 u64_stats_inc(&pcounters->errors);
1208 u64_stats_update_end(&pcounters->syncp);
1211 static int seg6_local_input_core(struct net *net, struct sock *sk,
1212 struct sk_buff *skb)
1214 struct dst_entry *orig_dst = skb_dst(skb);
1215 struct seg6_action_desc *desc;
1216 struct seg6_local_lwt *slwt;
1217 unsigned int len = skb->len;
1220 slwt = seg6_local_lwtunnel(orig_dst->lwtstate);
1223 rc = desc->input(skb, slwt);
1225 if (!seg6_lwtunnel_counters_enabled(slwt))
1228 seg6_local_update_counters(slwt, len, rc);
1233 static int seg6_local_input(struct sk_buff *skb)
1235 if (skb->protocol != htons(ETH_P_IPV6)) {
1240 if (static_branch_unlikely(&nf_hooks_lwtunnel_enabled))
1241 return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_IN,
1242 dev_net(skb->dev), NULL, skb, skb->dev, NULL,
1243 seg6_local_input_core);
1245 return seg6_local_input_core(dev_net(skb->dev), NULL, skb);
1248 static const struct nla_policy seg6_local_policy[SEG6_LOCAL_MAX + 1] = {
1249 [SEG6_LOCAL_ACTION] = { .type = NLA_U32 },
1250 [SEG6_LOCAL_SRH] = { .type = NLA_BINARY },
1251 [SEG6_LOCAL_TABLE] = { .type = NLA_U32 },
1252 [SEG6_LOCAL_VRFTABLE] = { .type = NLA_U32 },
1253 [SEG6_LOCAL_NH4] = { .type = NLA_BINARY,
1254 .len = sizeof(struct in_addr) },
1255 [SEG6_LOCAL_NH6] = { .type = NLA_BINARY,
1256 .len = sizeof(struct in6_addr) },
1257 [SEG6_LOCAL_IIF] = { .type = NLA_U32 },
1258 [SEG6_LOCAL_OIF] = { .type = NLA_U32 },
1259 [SEG6_LOCAL_BPF] = { .type = NLA_NESTED },
1260 [SEG6_LOCAL_COUNTERS] = { .type = NLA_NESTED },
1261 [SEG6_LOCAL_FLAVORS] = { .type = NLA_NESTED },
1264 static int parse_nla_srh(struct nlattr **attrs, struct seg6_local_lwt *slwt,
1265 struct netlink_ext_ack *extack)
1267 struct ipv6_sr_hdr *srh;
1270 srh = nla_data(attrs[SEG6_LOCAL_SRH]);
1271 len = nla_len(attrs[SEG6_LOCAL_SRH]);
1273 /* SRH must contain at least one segment */
1274 if (len < sizeof(*srh) + sizeof(struct in6_addr))
1277 if (!seg6_validate_srh(srh, len, false))
1280 slwt->srh = kmemdup(srh, len, GFP_KERNEL);
1284 slwt->headroom += len;
1289 static int put_nla_srh(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1291 struct ipv6_sr_hdr *srh;
1296 len = (srh->hdrlen + 1) << 3;
1298 nla = nla_reserve(skb, SEG6_LOCAL_SRH, len);
1302 memcpy(nla_data(nla), srh, len);
1307 static int cmp_nla_srh(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1309 int len = (a->srh->hdrlen + 1) << 3;
1311 if (len != ((b->srh->hdrlen + 1) << 3))
1314 return memcmp(a->srh, b->srh, len);
1317 static void destroy_attr_srh(struct seg6_local_lwt *slwt)
1322 static int parse_nla_table(struct nlattr **attrs, struct seg6_local_lwt *slwt,
1323 struct netlink_ext_ack *extack)
1325 slwt->table = nla_get_u32(attrs[SEG6_LOCAL_TABLE]);
1330 static int put_nla_table(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1332 if (nla_put_u32(skb, SEG6_LOCAL_TABLE, slwt->table))
1338 static int cmp_nla_table(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1340 if (a->table != b->table)
1347 seg6_end_dt_info *seg6_possible_end_dt_info(struct seg6_local_lwt *slwt)
1349 #ifdef CONFIG_NET_L3_MASTER_DEV
1350 return &slwt->dt_info;
1352 return ERR_PTR(-EOPNOTSUPP);
1356 static int parse_nla_vrftable(struct nlattr **attrs,
1357 struct seg6_local_lwt *slwt,
1358 struct netlink_ext_ack *extack)
1360 struct seg6_end_dt_info *info = seg6_possible_end_dt_info(slwt);
1363 return PTR_ERR(info);
1365 info->vrf_table = nla_get_u32(attrs[SEG6_LOCAL_VRFTABLE]);
1370 static int put_nla_vrftable(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1372 struct seg6_end_dt_info *info = seg6_possible_end_dt_info(slwt);
1375 return PTR_ERR(info);
1377 if (nla_put_u32(skb, SEG6_LOCAL_VRFTABLE, info->vrf_table))
1383 static int cmp_nla_vrftable(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1385 struct seg6_end_dt_info *info_a = seg6_possible_end_dt_info(a);
1386 struct seg6_end_dt_info *info_b = seg6_possible_end_dt_info(b);
1388 if (info_a->vrf_table != info_b->vrf_table)
1394 static int parse_nla_nh4(struct nlattr **attrs, struct seg6_local_lwt *slwt,
1395 struct netlink_ext_ack *extack)
1397 memcpy(&slwt->nh4, nla_data(attrs[SEG6_LOCAL_NH4]),
1398 sizeof(struct in_addr));
1403 static int put_nla_nh4(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1407 nla = nla_reserve(skb, SEG6_LOCAL_NH4, sizeof(struct in_addr));
1411 memcpy(nla_data(nla), &slwt->nh4, sizeof(struct in_addr));
1416 static int cmp_nla_nh4(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1418 return memcmp(&a->nh4, &b->nh4, sizeof(struct in_addr));
1421 static int parse_nla_nh6(struct nlattr **attrs, struct seg6_local_lwt *slwt,
1422 struct netlink_ext_ack *extack)
1424 memcpy(&slwt->nh6, nla_data(attrs[SEG6_LOCAL_NH6]),
1425 sizeof(struct in6_addr));
1430 static int put_nla_nh6(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1434 nla = nla_reserve(skb, SEG6_LOCAL_NH6, sizeof(struct in6_addr));
1438 memcpy(nla_data(nla), &slwt->nh6, sizeof(struct in6_addr));
1443 static int cmp_nla_nh6(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1445 return memcmp(&a->nh6, &b->nh6, sizeof(struct in6_addr));
1448 static int parse_nla_iif(struct nlattr **attrs, struct seg6_local_lwt *slwt,
1449 struct netlink_ext_ack *extack)
1451 slwt->iif = nla_get_u32(attrs[SEG6_LOCAL_IIF]);
1456 static int put_nla_iif(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1458 if (nla_put_u32(skb, SEG6_LOCAL_IIF, slwt->iif))
1464 static int cmp_nla_iif(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1466 if (a->iif != b->iif)
1472 static int parse_nla_oif(struct nlattr **attrs, struct seg6_local_lwt *slwt,
1473 struct netlink_ext_ack *extack)
1475 slwt->oif = nla_get_u32(attrs[SEG6_LOCAL_OIF]);
1480 static int put_nla_oif(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1482 if (nla_put_u32(skb, SEG6_LOCAL_OIF, slwt->oif))
1488 static int cmp_nla_oif(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1490 if (a->oif != b->oif)
1496 #define MAX_PROG_NAME 256
1497 static const struct nla_policy bpf_prog_policy[SEG6_LOCAL_BPF_PROG_MAX + 1] = {
1498 [SEG6_LOCAL_BPF_PROG] = { .type = NLA_U32, },
1499 [SEG6_LOCAL_BPF_PROG_NAME] = { .type = NLA_NUL_STRING,
1500 .len = MAX_PROG_NAME },
1503 static int parse_nla_bpf(struct nlattr **attrs, struct seg6_local_lwt *slwt,
1504 struct netlink_ext_ack *extack)
1506 struct nlattr *tb[SEG6_LOCAL_BPF_PROG_MAX + 1];
1511 ret = nla_parse_nested_deprecated(tb, SEG6_LOCAL_BPF_PROG_MAX,
1512 attrs[SEG6_LOCAL_BPF],
1513 bpf_prog_policy, NULL);
1517 if (!tb[SEG6_LOCAL_BPF_PROG] || !tb[SEG6_LOCAL_BPF_PROG_NAME])
1520 slwt->bpf.name = nla_memdup(tb[SEG6_LOCAL_BPF_PROG_NAME], GFP_KERNEL);
1521 if (!slwt->bpf.name)
1524 fd = nla_get_u32(tb[SEG6_LOCAL_BPF_PROG]);
1525 p = bpf_prog_get_type(fd, BPF_PROG_TYPE_LWT_SEG6LOCAL);
1527 kfree(slwt->bpf.name);
1535 static int put_nla_bpf(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1537 struct nlattr *nest;
1539 if (!slwt->bpf.prog)
1542 nest = nla_nest_start_noflag(skb, SEG6_LOCAL_BPF);
1546 if (nla_put_u32(skb, SEG6_LOCAL_BPF_PROG, slwt->bpf.prog->aux->id))
1549 if (slwt->bpf.name &&
1550 nla_put_string(skb, SEG6_LOCAL_BPF_PROG_NAME, slwt->bpf.name))
1553 return nla_nest_end(skb, nest);
1556 static int cmp_nla_bpf(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1558 if (!a->bpf.name && !b->bpf.name)
1561 if (!a->bpf.name || !b->bpf.name)
1564 return strcmp(a->bpf.name, b->bpf.name);
1567 static void destroy_attr_bpf(struct seg6_local_lwt *slwt)
1569 kfree(slwt->bpf.name);
1571 bpf_prog_put(slwt->bpf.prog);
1575 nla_policy seg6_local_counters_policy[SEG6_LOCAL_CNT_MAX + 1] = {
1576 [SEG6_LOCAL_CNT_PACKETS] = { .type = NLA_U64 },
1577 [SEG6_LOCAL_CNT_BYTES] = { .type = NLA_U64 },
1578 [SEG6_LOCAL_CNT_ERRORS] = { .type = NLA_U64 },
1581 static int parse_nla_counters(struct nlattr **attrs,
1582 struct seg6_local_lwt *slwt,
1583 struct netlink_ext_ack *extack)
1585 struct pcpu_seg6_local_counters __percpu *pcounters;
1586 struct nlattr *tb[SEG6_LOCAL_CNT_MAX + 1];
1589 ret = nla_parse_nested_deprecated(tb, SEG6_LOCAL_CNT_MAX,
1590 attrs[SEG6_LOCAL_COUNTERS],
1591 seg6_local_counters_policy, NULL);
1595 /* basic support for SRv6 Behavior counters requires at least:
1596 * packets, bytes and errors.
1598 if (!tb[SEG6_LOCAL_CNT_PACKETS] || !tb[SEG6_LOCAL_CNT_BYTES] ||
1599 !tb[SEG6_LOCAL_CNT_ERRORS])
1602 /* counters are always zero initialized */
1603 pcounters = seg6_local_alloc_pcpu_counters(GFP_KERNEL);
1607 slwt->pcpu_counters = pcounters;
1612 static int seg6_local_fill_nla_counters(struct sk_buff *skb,
1613 struct seg6_local_counters *counters)
1615 if (nla_put_u64_64bit(skb, SEG6_LOCAL_CNT_PACKETS, counters->packets,
1616 SEG6_LOCAL_CNT_PAD))
1619 if (nla_put_u64_64bit(skb, SEG6_LOCAL_CNT_BYTES, counters->bytes,
1620 SEG6_LOCAL_CNT_PAD))
1623 if (nla_put_u64_64bit(skb, SEG6_LOCAL_CNT_ERRORS, counters->errors,
1624 SEG6_LOCAL_CNT_PAD))
1630 static int put_nla_counters(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1632 struct seg6_local_counters counters = { 0, 0, 0 };
1633 struct nlattr *nest;
1636 nest = nla_nest_start(skb, SEG6_LOCAL_COUNTERS);
1640 for_each_possible_cpu(i) {
1641 struct pcpu_seg6_local_counters *pcounters;
1642 u64 packets, bytes, errors;
1645 pcounters = per_cpu_ptr(slwt->pcpu_counters, i);
1647 start = u64_stats_fetch_begin_irq(&pcounters->syncp);
1649 packets = u64_stats_read(&pcounters->packets);
1650 bytes = u64_stats_read(&pcounters->bytes);
1651 errors = u64_stats_read(&pcounters->errors);
1653 } while (u64_stats_fetch_retry_irq(&pcounters->syncp, start));
1655 counters.packets += packets;
1656 counters.bytes += bytes;
1657 counters.errors += errors;
1660 rc = seg6_local_fill_nla_counters(skb, &counters);
1662 nla_nest_cancel(skb, nest);
1666 return nla_nest_end(skb, nest);
1669 static int cmp_nla_counters(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1671 /* a and b are equal if both have pcpu_counters set or not */
1672 return (!!((unsigned long)a->pcpu_counters)) ^
1673 (!!((unsigned long)b->pcpu_counters));
1676 static void destroy_attr_counters(struct seg6_local_lwt *slwt)
1678 free_percpu(slwt->pcpu_counters);
1682 struct nla_policy seg6_local_flavors_policy[SEG6_LOCAL_FLV_MAX + 1] = {
1683 [SEG6_LOCAL_FLV_OPERATION] = { .type = NLA_U32 },
1684 [SEG6_LOCAL_FLV_LCBLOCK_BITS] = { .type = NLA_U8 },
1685 [SEG6_LOCAL_FLV_LCNODE_FN_BITS] = { .type = NLA_U8 },
1688 /* check whether the lengths of the Locator-Block and Locator-Node Function
1689 * are compatible with the dimension of a C-SID container.
1691 static int seg6_chk_next_csid_cfg(__u8 block_len, __u8 func_len)
1693 /* Locator-Block and Locator-Node Function cannot exceed 128 bits
1694 * (i.e. C-SID container lenghts).
1696 if (next_csid_chk_cntr_bits(block_len, func_len))
1699 /* Locator-Block length must be greater than zero and evenly divisible
1700 * by 8. There must be room for a Locator-Node Function, at least.
1702 if (next_csid_chk_lcblock_bits(block_len))
1705 /* Locator-Node Function length must be greater than zero and evenly
1706 * divisible by 8. There must be room for the Locator-Block.
1708 if (next_csid_chk_lcnode_fn_bits(func_len))
1714 static int seg6_parse_nla_next_csid_cfg(struct nlattr **tb,
1715 struct seg6_flavors_info *finfo,
1716 struct netlink_ext_ack *extack)
1718 __u8 func_len = SEG6_LOCAL_LCNODE_FN_DBITS;
1719 __u8 block_len = SEG6_LOCAL_LCBLOCK_DBITS;
1722 if (tb[SEG6_LOCAL_FLV_LCBLOCK_BITS])
1723 block_len = nla_get_u8(tb[SEG6_LOCAL_FLV_LCBLOCK_BITS]);
1725 if (tb[SEG6_LOCAL_FLV_LCNODE_FN_BITS])
1726 func_len = nla_get_u8(tb[SEG6_LOCAL_FLV_LCNODE_FN_BITS]);
1728 rc = seg6_chk_next_csid_cfg(block_len, func_len);
1730 NL_SET_ERR_MSG(extack,
1731 "Invalid Locator Block/Node Function lengths");
1735 finfo->lcblock_bits = block_len;
1736 finfo->lcnode_func_bits = func_len;
1741 static int parse_nla_flavors(struct nlattr **attrs, struct seg6_local_lwt *slwt,
1742 struct netlink_ext_ack *extack)
1744 struct seg6_flavors_info *finfo = &slwt->flv_info;
1745 struct nlattr *tb[SEG6_LOCAL_FLV_MAX + 1];
1749 rc = nla_parse_nested_deprecated(tb, SEG6_LOCAL_FLV_MAX,
1750 attrs[SEG6_LOCAL_FLAVORS],
1751 seg6_local_flavors_policy, NULL);
1755 /* this attribute MUST always be present since it represents the Flavor
1756 * operation(s) to be carried out.
1758 if (!tb[SEG6_LOCAL_FLV_OPERATION])
1761 fops = nla_get_u32(tb[SEG6_LOCAL_FLV_OPERATION]);
1762 if (fops & ~SEG6_LOCAL_FLV_SUPP_OPS) {
1763 NL_SET_ERR_MSG(extack, "Unsupported Flavor operation(s)");
1767 finfo->flv_ops = fops;
1769 if (seg6_next_csid_enabled(fops)) {
1770 /* Locator-Block and Locator-Node Function lengths can be
1771 * provided by the user space. Otherwise, default values are
1774 rc = seg6_parse_nla_next_csid_cfg(tb, finfo, extack);
1782 static int seg6_fill_nla_next_csid_cfg(struct sk_buff *skb,
1783 struct seg6_flavors_info *finfo)
1785 if (nla_put_u8(skb, SEG6_LOCAL_FLV_LCBLOCK_BITS, finfo->lcblock_bits))
1788 if (nla_put_u8(skb, SEG6_LOCAL_FLV_LCNODE_FN_BITS,
1789 finfo->lcnode_func_bits))
1795 static int put_nla_flavors(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1797 struct seg6_flavors_info *finfo = &slwt->flv_info;
1798 __u32 fops = finfo->flv_ops;
1799 struct nlattr *nest;
1802 nest = nla_nest_start(skb, SEG6_LOCAL_FLAVORS);
1806 if (nla_put_u32(skb, SEG6_LOCAL_FLV_OPERATION, fops)) {
1811 if (seg6_next_csid_enabled(fops)) {
1812 rc = seg6_fill_nla_next_csid_cfg(skb, finfo);
1817 return nla_nest_end(skb, nest);
1820 nla_nest_cancel(skb, nest);
1824 static int seg6_cmp_nla_next_csid_cfg(struct seg6_flavors_info *finfo_a,
1825 struct seg6_flavors_info *finfo_b)
1827 if (finfo_a->lcblock_bits != finfo_b->lcblock_bits)
1830 if (finfo_a->lcnode_func_bits != finfo_b->lcnode_func_bits)
1836 static int cmp_nla_flavors(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1838 struct seg6_flavors_info *finfo_a = &a->flv_info;
1839 struct seg6_flavors_info *finfo_b = &b->flv_info;
1841 if (finfo_a->flv_ops != finfo_b->flv_ops)
1844 if (seg6_next_csid_enabled(finfo_a->flv_ops)) {
1845 if (seg6_cmp_nla_next_csid_cfg(finfo_a, finfo_b))
1852 static int encap_size_flavors(struct seg6_local_lwt *slwt)
1854 struct seg6_flavors_info *finfo = &slwt->flv_info;
1857 nlsize = nla_total_size(0) + /* nest SEG6_LOCAL_FLAVORS */
1858 nla_total_size(4); /* SEG6_LOCAL_FLV_OPERATION */
1860 if (seg6_next_csid_enabled(finfo->flv_ops))
1861 nlsize += nla_total_size(1) + /* SEG6_LOCAL_FLV_LCBLOCK_BITS */
1862 nla_total_size(1); /* SEG6_LOCAL_FLV_LCNODE_FN_BITS */
1867 struct seg6_action_param {
1868 int (*parse)(struct nlattr **attrs, struct seg6_local_lwt *slwt,
1869 struct netlink_ext_ack *extack);
1870 int (*put)(struct sk_buff *skb, struct seg6_local_lwt *slwt);
1871 int (*cmp)(struct seg6_local_lwt *a, struct seg6_local_lwt *b);
1873 /* optional destroy() callback useful for releasing resources which
1874 * have been previously acquired in the corresponding parse()
1877 void (*destroy)(struct seg6_local_lwt *slwt);
1880 static struct seg6_action_param seg6_action_params[SEG6_LOCAL_MAX + 1] = {
1881 [SEG6_LOCAL_SRH] = { .parse = parse_nla_srh,
1884 .destroy = destroy_attr_srh },
1886 [SEG6_LOCAL_TABLE] = { .parse = parse_nla_table,
1887 .put = put_nla_table,
1888 .cmp = cmp_nla_table },
1890 [SEG6_LOCAL_NH4] = { .parse = parse_nla_nh4,
1892 .cmp = cmp_nla_nh4 },
1894 [SEG6_LOCAL_NH6] = { .parse = parse_nla_nh6,
1896 .cmp = cmp_nla_nh6 },
1898 [SEG6_LOCAL_IIF] = { .parse = parse_nla_iif,
1900 .cmp = cmp_nla_iif },
1902 [SEG6_LOCAL_OIF] = { .parse = parse_nla_oif,
1904 .cmp = cmp_nla_oif },
1906 [SEG6_LOCAL_BPF] = { .parse = parse_nla_bpf,
1909 .destroy = destroy_attr_bpf },
1911 [SEG6_LOCAL_VRFTABLE] = { .parse = parse_nla_vrftable,
1912 .put = put_nla_vrftable,
1913 .cmp = cmp_nla_vrftable },
1915 [SEG6_LOCAL_COUNTERS] = { .parse = parse_nla_counters,
1916 .put = put_nla_counters,
1917 .cmp = cmp_nla_counters,
1918 .destroy = destroy_attr_counters },
1920 [SEG6_LOCAL_FLAVORS] = { .parse = parse_nla_flavors,
1921 .put = put_nla_flavors,
1922 .cmp = cmp_nla_flavors },
1925 /* call the destroy() callback (if available) for each set attribute in
1926 * @parsed_attrs, starting from the first attribute up to the @max_parsed
1927 * (excluded) attribute.
1929 static void __destroy_attrs(unsigned long parsed_attrs, int max_parsed,
1930 struct seg6_local_lwt *slwt)
1932 struct seg6_action_param *param;
1935 /* Every required seg6local attribute is identified by an ID which is
1936 * encoded as a flag (i.e: 1 << ID) in the 'attrs' bitmask;
1938 * We scan the 'parsed_attrs' bitmask, starting from the first attribute
1939 * up to the @max_parsed (excluded) attribute.
1940 * For each set attribute, we retrieve the corresponding destroy()
1941 * callback. If the callback is not available, then we skip to the next
1942 * attribute; otherwise, we call the destroy() callback.
1944 for (i = SEG6_LOCAL_SRH; i < max_parsed; ++i) {
1945 if (!(parsed_attrs & SEG6_F_ATTR(i)))
1948 param = &seg6_action_params[i];
1951 param->destroy(slwt);
1955 /* release all the resources that may have been acquired during parsing
1958 static void destroy_attrs(struct seg6_local_lwt *slwt)
1960 unsigned long attrs = slwt->desc->attrs | slwt->parsed_optattrs;
1962 __destroy_attrs(attrs, SEG6_LOCAL_MAX + 1, slwt);
1965 static int parse_nla_optional_attrs(struct nlattr **attrs,
1966 struct seg6_local_lwt *slwt,
1967 struct netlink_ext_ack *extack)
1969 struct seg6_action_desc *desc = slwt->desc;
1970 unsigned long parsed_optattrs = 0;
1971 struct seg6_action_param *param;
1974 for (i = SEG6_LOCAL_SRH; i < SEG6_LOCAL_MAX + 1; ++i) {
1975 if (!(desc->optattrs & SEG6_F_ATTR(i)) || !attrs[i])
1978 /* once here, the i-th attribute is provided by the
1979 * userspace AND it is identified optional as well.
1981 param = &seg6_action_params[i];
1983 err = param->parse(attrs, slwt, extack);
1985 goto parse_optattrs_err;
1987 /* current attribute has been correctly parsed */
1988 parsed_optattrs |= SEG6_F_ATTR(i);
1991 /* store in the tunnel state all the optional attributed successfully
1994 slwt->parsed_optattrs = parsed_optattrs;
1999 __destroy_attrs(parsed_optattrs, i, slwt);
2004 /* call the custom constructor of the behavior during its initialization phase
2005 * and after that all its attributes have been parsed successfully.
2008 seg6_local_lwtunnel_build_state(struct seg6_local_lwt *slwt, const void *cfg,
2009 struct netlink_ext_ack *extack)
2011 struct seg6_action_desc *desc = slwt->desc;
2012 struct seg6_local_lwtunnel_ops *ops;
2014 ops = &desc->slwt_ops;
2015 if (!ops->build_state)
2018 return ops->build_state(slwt, cfg, extack);
2021 /* call the custom destructor of the behavior which is invoked before the
2022 * tunnel is going to be destroyed.
2024 static void seg6_local_lwtunnel_destroy_state(struct seg6_local_lwt *slwt)
2026 struct seg6_action_desc *desc = slwt->desc;
2027 struct seg6_local_lwtunnel_ops *ops;
2029 ops = &desc->slwt_ops;
2030 if (!ops->destroy_state)
2033 ops->destroy_state(slwt);
2036 static int parse_nla_action(struct nlattr **attrs, struct seg6_local_lwt *slwt,
2037 struct netlink_ext_ack *extack)
2039 struct seg6_action_param *param;
2040 struct seg6_action_desc *desc;
2041 unsigned long invalid_attrs;
2044 desc = __get_action_desc(slwt->action);
2052 slwt->headroom += desc->static_headroom;
2054 /* Forcing the desc->optattrs *set* and the desc->attrs *set* to be
2055 * disjoined, this allow us to release acquired resources by optional
2056 * attributes and by required attributes independently from each other
2057 * without any interference.
2058 * In other terms, we are sure that we do not release some the acquired
2061 * Note that if an attribute is configured both as required and as
2062 * optional, it means that the user has messed something up in the
2063 * seg6_action_table. Therefore, this check is required for SRv6
2064 * behaviors to work properly.
2066 invalid_attrs = desc->attrs & desc->optattrs;
2067 if (invalid_attrs) {
2069 "An attribute cannot be both required AND optional");
2073 /* parse the required attributes */
2074 for (i = SEG6_LOCAL_SRH; i < SEG6_LOCAL_MAX + 1; i++) {
2075 if (desc->attrs & SEG6_F_ATTR(i)) {
2079 param = &seg6_action_params[i];
2081 err = param->parse(attrs, slwt, extack);
2083 goto parse_attrs_err;
2087 /* parse the optional attributes, if any */
2088 err = parse_nla_optional_attrs(attrs, slwt, extack);
2090 goto parse_attrs_err;
2095 /* release any resource that may have been acquired during the i-1
2096 * parse() operations.
2098 __destroy_attrs(desc->attrs, i, slwt);
2103 static int seg6_local_build_state(struct net *net, struct nlattr *nla,
2104 unsigned int family, const void *cfg,
2105 struct lwtunnel_state **ts,
2106 struct netlink_ext_ack *extack)
2108 struct nlattr *tb[SEG6_LOCAL_MAX + 1];
2109 struct lwtunnel_state *newts;
2110 struct seg6_local_lwt *slwt;
2113 if (family != AF_INET6)
2116 err = nla_parse_nested_deprecated(tb, SEG6_LOCAL_MAX, nla,
2117 seg6_local_policy, extack);
2122 if (!tb[SEG6_LOCAL_ACTION])
2125 newts = lwtunnel_state_alloc(sizeof(*slwt));
2129 slwt = seg6_local_lwtunnel(newts);
2130 slwt->action = nla_get_u32(tb[SEG6_LOCAL_ACTION]);
2132 err = parse_nla_action(tb, slwt, extack);
2136 err = seg6_local_lwtunnel_build_state(slwt, cfg, extack);
2138 goto out_destroy_attrs;
2140 newts->type = LWTUNNEL_ENCAP_SEG6_LOCAL;
2141 newts->flags = LWTUNNEL_STATE_INPUT_REDIRECT;
2142 newts->headroom = slwt->headroom;
2149 destroy_attrs(slwt);
2155 static void seg6_local_destroy_state(struct lwtunnel_state *lwt)
2157 struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
2159 seg6_local_lwtunnel_destroy_state(slwt);
2161 destroy_attrs(slwt);
2166 static int seg6_local_fill_encap(struct sk_buff *skb,
2167 struct lwtunnel_state *lwt)
2169 struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
2170 struct seg6_action_param *param;
2171 unsigned long attrs;
2174 if (nla_put_u32(skb, SEG6_LOCAL_ACTION, slwt->action))
2177 attrs = slwt->desc->attrs | slwt->parsed_optattrs;
2179 for (i = SEG6_LOCAL_SRH; i < SEG6_LOCAL_MAX + 1; i++) {
2180 if (attrs & SEG6_F_ATTR(i)) {
2181 param = &seg6_action_params[i];
2182 err = param->put(skb, slwt);
2191 static int seg6_local_get_encap_size(struct lwtunnel_state *lwt)
2193 struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
2194 unsigned long attrs;
2197 nlsize = nla_total_size(4); /* action */
2199 attrs = slwt->desc->attrs | slwt->parsed_optattrs;
2201 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_SRH))
2202 nlsize += nla_total_size((slwt->srh->hdrlen + 1) << 3);
2204 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_TABLE))
2205 nlsize += nla_total_size(4);
2207 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_NH4))
2208 nlsize += nla_total_size(4);
2210 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_NH6))
2211 nlsize += nla_total_size(16);
2213 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_IIF))
2214 nlsize += nla_total_size(4);
2216 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_OIF))
2217 nlsize += nla_total_size(4);
2219 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_BPF))
2220 nlsize += nla_total_size(sizeof(struct nlattr)) +
2221 nla_total_size(MAX_PROG_NAME) +
2224 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_VRFTABLE))
2225 nlsize += nla_total_size(4);
2227 if (attrs & SEG6_F_LOCAL_COUNTERS)
2228 nlsize += nla_total_size(0) + /* nest SEG6_LOCAL_COUNTERS */
2229 /* SEG6_LOCAL_CNT_PACKETS */
2230 nla_total_size_64bit(sizeof(__u64)) +
2231 /* SEG6_LOCAL_CNT_BYTES */
2232 nla_total_size_64bit(sizeof(__u64)) +
2233 /* SEG6_LOCAL_CNT_ERRORS */
2234 nla_total_size_64bit(sizeof(__u64));
2236 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_FLAVORS))
2237 nlsize += encap_size_flavors(slwt);
2242 static int seg6_local_cmp_encap(struct lwtunnel_state *a,
2243 struct lwtunnel_state *b)
2245 struct seg6_local_lwt *slwt_a, *slwt_b;
2246 struct seg6_action_param *param;
2247 unsigned long attrs_a, attrs_b;
2250 slwt_a = seg6_local_lwtunnel(a);
2251 slwt_b = seg6_local_lwtunnel(b);
2253 if (slwt_a->action != slwt_b->action)
2256 attrs_a = slwt_a->desc->attrs | slwt_a->parsed_optattrs;
2257 attrs_b = slwt_b->desc->attrs | slwt_b->parsed_optattrs;
2259 if (attrs_a != attrs_b)
2262 for (i = SEG6_LOCAL_SRH; i < SEG6_LOCAL_MAX + 1; i++) {
2263 if (attrs_a & SEG6_F_ATTR(i)) {
2264 param = &seg6_action_params[i];
2265 if (param->cmp(slwt_a, slwt_b))
2273 static const struct lwtunnel_encap_ops seg6_local_ops = {
2274 .build_state = seg6_local_build_state,
2275 .destroy_state = seg6_local_destroy_state,
2276 .input = seg6_local_input,
2277 .fill_encap = seg6_local_fill_encap,
2278 .get_encap_size = seg6_local_get_encap_size,
2279 .cmp_encap = seg6_local_cmp_encap,
2280 .owner = THIS_MODULE,
2283 int __init seg6_local_init(void)
2285 /* If the max total number of defined attributes is reached, then your
2286 * kernel build stops here.
2288 * This check is required to avoid arithmetic overflows when processing
2289 * behavior attributes and the maximum number of defined attributes
2290 * exceeds the allowed value.
2292 BUILD_BUG_ON(SEG6_LOCAL_MAX + 1 > BITS_PER_TYPE(unsigned long));
2294 /* If the default NEXT-C-SID Locator-Block/Node Function lengths (in
2295 * bits) have been changed with invalid values, kernel build stops
2298 BUILD_BUG_ON(next_csid_chk_cntr_bits(SEG6_LOCAL_LCBLOCK_DBITS,
2299 SEG6_LOCAL_LCNODE_FN_DBITS));
2300 BUILD_BUG_ON(next_csid_chk_lcblock_bits(SEG6_LOCAL_LCBLOCK_DBITS));
2301 BUILD_BUG_ON(next_csid_chk_lcnode_fn_bits(SEG6_LOCAL_LCNODE_FN_DBITS));
2303 return lwtunnel_encap_add_ops(&seg6_local_ops,
2304 LWTUNNEL_ENCAP_SEG6_LOCAL);
2307 void seg6_local_exit(void)
2309 lwtunnel_encap_del_ops(&seg6_local_ops, LWTUNNEL_ENCAP_SEG6_LOCAL);