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 fl6.flowi6_iif = skb->dev->ifindex;
222 fl6.daddr = nhaddr ? *nhaddr : hdr->daddr;
223 fl6.saddr = hdr->saddr;
224 fl6.flowlabel = ip6_flowinfo(hdr);
225 fl6.flowi6_mark = skb->mark;
226 fl6.flowi6_proto = hdr->nexthdr;
229 fl6.flowi6_flags = FLOWI_FLAG_KNOWN_NH;
232 dst = ip6_route_input_lookup(net, skb->dev, &fl6, skb, flags);
234 struct fib6_table *table;
236 table = fib6_get_table(net, tbl_id);
240 rt = ip6_pol_route(net, table, 0, &fl6, skb, flags);
244 /* we want to discard traffic destined for local packet processing,
245 * if @local_delivery is set to false.
248 dev_flags |= IFF_LOOPBACK;
250 if (dst && (dst->dev->flags & dev_flags) && !dst->error) {
257 rt = net->ipv6.ip6_blk_hole_entry;
263 skb_dst_set(skb, dst);
267 int seg6_lookup_nexthop(struct sk_buff *skb,
268 struct in6_addr *nhaddr, u32 tbl_id)
270 return seg6_lookup_any_nexthop(skb, nhaddr, tbl_id, false);
273 /* regular endpoint function */
274 static int input_action_end(struct sk_buff *skb, struct seg6_local_lwt *slwt)
276 struct ipv6_sr_hdr *srh;
278 srh = get_and_validate_srh(skb);
282 advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
284 seg6_lookup_nexthop(skb, NULL, 0);
286 return dst_input(skb);
293 /* regular endpoint, and forward to specified nexthop */
294 static int input_action_end_x(struct sk_buff *skb, struct seg6_local_lwt *slwt)
296 struct ipv6_sr_hdr *srh;
298 srh = get_and_validate_srh(skb);
302 advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
304 seg6_lookup_nexthop(skb, &slwt->nh6, 0);
306 return dst_input(skb);
313 static int input_action_end_t(struct sk_buff *skb, struct seg6_local_lwt *slwt)
315 struct ipv6_sr_hdr *srh;
317 srh = get_and_validate_srh(skb);
321 advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
323 seg6_lookup_nexthop(skb, NULL, slwt->table);
325 return dst_input(skb);
332 /* decapsulate and forward inner L2 frame on specified interface */
333 static int input_action_end_dx2(struct sk_buff *skb,
334 struct seg6_local_lwt *slwt)
336 struct net *net = dev_net(skb->dev);
337 struct net_device *odev;
340 if (!decap_and_validate(skb, IPPROTO_ETHERNET))
343 if (!pskb_may_pull(skb, ETH_HLEN))
346 skb_reset_mac_header(skb);
347 eth = (struct ethhdr *)skb->data;
349 /* To determine the frame's protocol, we assume it is 802.3. This avoids
350 * a call to eth_type_trans(), which is not really relevant for our
353 if (!eth_proto_is_802_3(eth->h_proto))
356 odev = dev_get_by_index_rcu(net, slwt->oif);
360 /* As we accept Ethernet frames, make sure the egress device is of
363 if (odev->type != ARPHRD_ETHER)
366 if (!(odev->flags & IFF_UP) || !netif_carrier_ok(odev))
371 if (skb_warn_if_lro(skb))
374 skb_forward_csum(skb);
376 if (skb->len - ETH_HLEN > odev->mtu)
380 skb->protocol = eth->h_proto;
382 return dev_queue_xmit(skb);
389 static int input_action_end_dx6_finish(struct net *net, struct sock *sk,
392 struct dst_entry *orig_dst = skb_dst(skb);
393 struct in6_addr *nhaddr = NULL;
394 struct seg6_local_lwt *slwt;
396 slwt = seg6_local_lwtunnel(orig_dst->lwtstate);
398 /* The inner packet is not associated to any local interface,
399 * so we do not call netif_rx().
401 * If slwt->nh6 is set to ::, then lookup the nexthop for the
402 * inner packet's DA. Otherwise, use the specified nexthop.
404 if (!ipv6_addr_any(&slwt->nh6))
407 seg6_lookup_nexthop(skb, nhaddr, 0);
409 return dst_input(skb);
412 /* decapsulate and forward to specified nexthop */
413 static int input_action_end_dx6(struct sk_buff *skb,
414 struct seg6_local_lwt *slwt)
416 /* this function accepts IPv6 encapsulated packets, with either
417 * an SRH with SL=0, or no SRH.
420 if (!decap_and_validate(skb, IPPROTO_IPV6))
423 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
426 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
429 if (static_branch_unlikely(&nf_hooks_lwtunnel_enabled))
430 return NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING,
431 dev_net(skb->dev), NULL, skb, NULL,
432 skb_dst(skb)->dev, input_action_end_dx6_finish);
434 return input_action_end_dx6_finish(dev_net(skb->dev), NULL, skb);
440 static int input_action_end_dx4_finish(struct net *net, struct sock *sk,
443 struct dst_entry *orig_dst = skb_dst(skb);
444 struct seg6_local_lwt *slwt;
449 slwt = seg6_local_lwtunnel(orig_dst->lwtstate);
453 nhaddr = slwt->nh4.s_addr ?: iph->daddr;
457 err = ip_route_input(skb, nhaddr, iph->saddr, 0, skb->dev);
463 return dst_input(skb);
466 static int input_action_end_dx4(struct sk_buff *skb,
467 struct seg6_local_lwt *slwt)
469 if (!decap_and_validate(skb, IPPROTO_IPIP))
472 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
475 skb->protocol = htons(ETH_P_IP);
476 skb_set_transport_header(skb, sizeof(struct iphdr));
479 if (static_branch_unlikely(&nf_hooks_lwtunnel_enabled))
480 return NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING,
481 dev_net(skb->dev), NULL, skb, NULL,
482 skb_dst(skb)->dev, input_action_end_dx4_finish);
484 return input_action_end_dx4_finish(dev_net(skb->dev), NULL, skb);
490 #ifdef CONFIG_NET_L3_MASTER_DEV
491 static struct net *fib6_config_get_net(const struct fib6_config *fib6_cfg)
493 const struct nl_info *nli = &fib6_cfg->fc_nlinfo;
498 static int __seg6_end_dt_vrf_build(struct seg6_local_lwt *slwt, const void *cfg,
499 u16 family, struct netlink_ext_ack *extack)
501 struct seg6_end_dt_info *info = &slwt->dt_info;
505 net = fib6_config_get_net(cfg);
507 /* note that vrf_table was already set by parse_nla_vrftable() */
508 vrf_ifindex = l3mdev_ifindex_lookup_by_table_id(L3MDEV_TYPE_VRF, net,
510 if (vrf_ifindex < 0) {
511 if (vrf_ifindex == -EPERM) {
512 NL_SET_ERR_MSG(extack,
513 "Strict mode for VRF is disabled");
514 } else if (vrf_ifindex == -ENODEV) {
515 NL_SET_ERR_MSG(extack,
516 "Table has no associated VRF device");
518 pr_debug("seg6local: SRv6 End.DT* creation error=%d\n",
526 info->vrf_ifindex = vrf_ifindex;
528 info->family = family;
529 info->mode = DT_VRF_MODE;
534 /* The SRv6 End.DT4/DT6 behavior extracts the inner (IPv4/IPv6) packet and
535 * routes the IPv4/IPv6 packet by looking at the configured routing table.
537 * In the SRv6 End.DT4/DT6 use case, we can receive traffic (IPv6+Segment
538 * Routing Header packets) from several interfaces and the outer IPv6
539 * destination address (DA) is used for retrieving the specific instance of the
540 * End.DT4/DT6 behavior that should process the packets.
542 * However, the inner IPv4/IPv6 packet is not really bound to any receiving
543 * interface and thus the End.DT4/DT6 sets the VRF (associated with the
544 * corresponding routing table) as the *receiving* interface.
545 * In other words, the End.DT4/DT6 processes a packet as if it has been received
546 * directly by the VRF (and not by one of its slave devices, if any).
547 * In this way, the VRF interface is used for routing the IPv4/IPv6 packet in
548 * according to the routing table configured by the End.DT4/DT6 instance.
550 * This design allows you to get some interesting features like:
551 * 1) the statistics on rx packets;
552 * 2) the possibility to install a packet sniffer on the receiving interface
553 * (the VRF one) for looking at the incoming packets;
554 * 3) the possibility to leverage the netfilter prerouting hook for the inner
557 * This function returns:
558 * - the sk_buff* when the VRF rcv handler has processed the packet correctly;
559 * - NULL when the skb is consumed by the VRF rcv handler;
560 * - a pointer which encodes a negative error number in case of error.
561 * Note that in this case, the function takes care of freeing the skb.
563 static struct sk_buff *end_dt_vrf_rcv(struct sk_buff *skb, u16 family,
564 struct net_device *dev)
566 /* based on l3mdev_ip_rcv; we are only interested in the master */
567 if (unlikely(!netif_is_l3_master(dev) && !netif_has_l3_rx_handler(dev)))
570 if (unlikely(!dev->l3mdev_ops->l3mdev_l3_rcv))
573 /* the decap packet IPv4/IPv6 does not come with any mac header info.
574 * We must unset the mac header to allow the VRF device to rebuild it,
575 * just in case there is a sniffer attached on the device.
577 skb_unset_mac_header(skb);
579 skb = dev->l3mdev_ops->l3mdev_l3_rcv(dev, skb, family);
581 /* the skb buffer was consumed by the handler */
584 /* when a packet is received by a VRF or by one of its slaves, the
585 * master device reference is set into the skb.
587 if (unlikely(skb->dev != dev || skb->skb_iif != dev->ifindex))
594 return ERR_PTR(-EINVAL);
597 static struct net_device *end_dt_get_vrf_rcu(struct sk_buff *skb,
598 struct seg6_end_dt_info *info)
600 int vrf_ifindex = info->vrf_ifindex;
601 struct net *net = info->net;
603 if (unlikely(vrf_ifindex < 0))
606 if (unlikely(!net_eq(dev_net(skb->dev), net)))
609 return dev_get_by_index_rcu(net, vrf_ifindex);
615 static struct sk_buff *end_dt_vrf_core(struct sk_buff *skb,
616 struct seg6_local_lwt *slwt, u16 family)
618 struct seg6_end_dt_info *info = &slwt->dt_info;
619 struct net_device *vrf;
623 vrf = end_dt_get_vrf_rcu(skb, info);
629 protocol = htons(ETH_P_IP);
630 hdrlen = sizeof(struct iphdr);
633 protocol = htons(ETH_P_IPV6);
634 hdrlen = sizeof(struct ipv6hdr);
642 if (unlikely(info->family != AF_UNSPEC && info->family != family)) {
643 pr_warn_once("seg6local: SRv6 End.DT* family mismatch");
647 skb->protocol = protocol;
651 skb_set_transport_header(skb, hdrlen);
654 return end_dt_vrf_rcv(skb, family, vrf);
658 return ERR_PTR(-EINVAL);
661 static int input_action_end_dt4(struct sk_buff *skb,
662 struct seg6_local_lwt *slwt)
667 if (!decap_and_validate(skb, IPPROTO_IPIP))
670 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
673 skb = end_dt_vrf_core(skb, slwt, AF_INET);
675 /* packet has been processed and consumed by the VRF */
683 err = ip_route_input(skb, iph->daddr, iph->saddr, 0, skb->dev);
687 return dst_input(skb);
694 static int seg6_end_dt4_build(struct seg6_local_lwt *slwt, const void *cfg,
695 struct netlink_ext_ack *extack)
697 return __seg6_end_dt_vrf_build(slwt, cfg, AF_INET, extack);
701 seg6_end_dt_mode seg6_end_dt6_parse_mode(struct seg6_local_lwt *slwt)
703 unsigned long parsed_optattrs = slwt->parsed_optattrs;
704 bool legacy, vrfmode;
706 legacy = !!(parsed_optattrs & SEG6_F_ATTR(SEG6_LOCAL_TABLE));
707 vrfmode = !!(parsed_optattrs & SEG6_F_ATTR(SEG6_LOCAL_VRFTABLE));
709 if (!(legacy ^ vrfmode))
710 /* both are absent or present: invalid DT6 mode */
711 return DT_INVALID_MODE;
713 return legacy ? DT_LEGACY_MODE : DT_VRF_MODE;
716 static enum seg6_end_dt_mode seg6_end_dt6_get_mode(struct seg6_local_lwt *slwt)
718 struct seg6_end_dt_info *info = &slwt->dt_info;
723 static int seg6_end_dt6_build(struct seg6_local_lwt *slwt, const void *cfg,
724 struct netlink_ext_ack *extack)
726 enum seg6_end_dt_mode mode = seg6_end_dt6_parse_mode(slwt);
727 struct seg6_end_dt_info *info = &slwt->dt_info;
731 info->mode = DT_LEGACY_MODE;
734 return __seg6_end_dt_vrf_build(slwt, cfg, AF_INET6, extack);
736 NL_SET_ERR_MSG(extack, "table or vrftable must be specified");
742 static int input_action_end_dt6(struct sk_buff *skb,
743 struct seg6_local_lwt *slwt)
745 if (!decap_and_validate(skb, IPPROTO_IPV6))
748 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
751 #ifdef CONFIG_NET_L3_MASTER_DEV
752 if (seg6_end_dt6_get_mode(slwt) == DT_LEGACY_MODE)
756 skb = end_dt_vrf_core(skb, slwt, AF_INET6);
758 /* packet has been processed and consumed by the VRF */
764 /* note: this time we do not need to specify the table because the VRF
765 * takes care of selecting the correct table.
767 seg6_lookup_any_nexthop(skb, NULL, 0, true);
769 return dst_input(skb);
773 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
775 seg6_lookup_any_nexthop(skb, NULL, slwt->table, true);
777 return dst_input(skb);
784 #ifdef CONFIG_NET_L3_MASTER_DEV
785 static int seg6_end_dt46_build(struct seg6_local_lwt *slwt, const void *cfg,
786 struct netlink_ext_ack *extack)
788 return __seg6_end_dt_vrf_build(slwt, cfg, AF_UNSPEC, extack);
791 static int input_action_end_dt46(struct sk_buff *skb,
792 struct seg6_local_lwt *slwt)
794 unsigned int off = 0;
797 nexthdr = ipv6_find_hdr(skb, &off, -1, NULL, NULL);
798 if (unlikely(nexthdr < 0))
803 return input_action_end_dt4(skb, slwt);
805 return input_action_end_dt6(skb, slwt);
814 /* push an SRH on top of the current one */
815 static int input_action_end_b6(struct sk_buff *skb, struct seg6_local_lwt *slwt)
817 struct ipv6_sr_hdr *srh;
820 srh = get_and_validate_srh(skb);
824 err = seg6_do_srh_inline(skb, slwt->srh);
828 ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
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 ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
861 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
863 seg6_lookup_nexthop(skb, NULL, 0);
865 return dst_input(skb);
872 DEFINE_PER_CPU(struct seg6_bpf_srh_state, seg6_bpf_srh_states);
874 bool seg6_bpf_has_valid_srh(struct sk_buff *skb)
876 struct seg6_bpf_srh_state *srh_state =
877 this_cpu_ptr(&seg6_bpf_srh_states);
878 struct ipv6_sr_hdr *srh = srh_state->srh;
880 if (unlikely(srh == NULL))
883 if (unlikely(!srh_state->valid)) {
884 if ((srh_state->hdrlen & 7) != 0)
887 srh->hdrlen = (u8)(srh_state->hdrlen >> 3);
888 if (!seg6_validate_srh(srh, (srh->hdrlen + 1) << 3, true))
891 srh_state->valid = true;
897 static int input_action_end_bpf(struct sk_buff *skb,
898 struct seg6_local_lwt *slwt)
900 struct seg6_bpf_srh_state *srh_state =
901 this_cpu_ptr(&seg6_bpf_srh_states);
902 struct ipv6_sr_hdr *srh;
905 srh = get_and_validate_srh(skb);
910 advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
912 /* preempt_disable is needed to protect the per-CPU buffer srh_state,
913 * which is also accessed by the bpf_lwt_seg6_* helpers
916 srh_state->srh = srh;
917 srh_state->hdrlen = srh->hdrlen << 3;
918 srh_state->valid = true;
921 bpf_compute_data_pointers(skb);
922 ret = bpf_prog_run_save_cb(slwt->bpf.prog, skb);
932 pr_warn_once("bpf-seg6local: Illegal return value %u\n", ret);
936 if (srh_state->srh && !seg6_bpf_has_valid_srh(skb))
940 if (ret != BPF_REDIRECT)
941 seg6_lookup_nexthop(skb, NULL, 0);
943 return dst_input(skb);
951 static struct seg6_action_desc seg6_action_table[] = {
953 .action = SEG6_LOCAL_ACTION_END,
955 .optattrs = SEG6_F_LOCAL_COUNTERS,
956 .input = input_action_end,
959 .action = SEG6_LOCAL_ACTION_END_X,
960 .attrs = SEG6_F_ATTR(SEG6_LOCAL_NH6),
961 .optattrs = SEG6_F_LOCAL_COUNTERS,
962 .input = input_action_end_x,
965 .action = SEG6_LOCAL_ACTION_END_T,
966 .attrs = SEG6_F_ATTR(SEG6_LOCAL_TABLE),
967 .optattrs = SEG6_F_LOCAL_COUNTERS,
968 .input = input_action_end_t,
971 .action = SEG6_LOCAL_ACTION_END_DX2,
972 .attrs = SEG6_F_ATTR(SEG6_LOCAL_OIF),
973 .optattrs = SEG6_F_LOCAL_COUNTERS,
974 .input = input_action_end_dx2,
977 .action = SEG6_LOCAL_ACTION_END_DX6,
978 .attrs = SEG6_F_ATTR(SEG6_LOCAL_NH6),
979 .optattrs = SEG6_F_LOCAL_COUNTERS,
980 .input = input_action_end_dx6,
983 .action = SEG6_LOCAL_ACTION_END_DX4,
984 .attrs = SEG6_F_ATTR(SEG6_LOCAL_NH4),
985 .optattrs = SEG6_F_LOCAL_COUNTERS,
986 .input = input_action_end_dx4,
989 .action = SEG6_LOCAL_ACTION_END_DT4,
990 .attrs = SEG6_F_ATTR(SEG6_LOCAL_VRFTABLE),
991 .optattrs = SEG6_F_LOCAL_COUNTERS,
992 #ifdef CONFIG_NET_L3_MASTER_DEV
993 .input = input_action_end_dt4,
995 .build_state = seg6_end_dt4_build,
1000 .action = SEG6_LOCAL_ACTION_END_DT6,
1001 #ifdef CONFIG_NET_L3_MASTER_DEV
1003 .optattrs = SEG6_F_LOCAL_COUNTERS |
1004 SEG6_F_ATTR(SEG6_LOCAL_TABLE) |
1005 SEG6_F_ATTR(SEG6_LOCAL_VRFTABLE),
1007 .build_state = seg6_end_dt6_build,
1010 .attrs = SEG6_F_ATTR(SEG6_LOCAL_TABLE),
1011 .optattrs = SEG6_F_LOCAL_COUNTERS,
1013 .input = input_action_end_dt6,
1016 .action = SEG6_LOCAL_ACTION_END_DT46,
1017 .attrs = SEG6_F_ATTR(SEG6_LOCAL_VRFTABLE),
1018 .optattrs = SEG6_F_LOCAL_COUNTERS,
1019 #ifdef CONFIG_NET_L3_MASTER_DEV
1020 .input = input_action_end_dt46,
1022 .build_state = seg6_end_dt46_build,
1027 .action = SEG6_LOCAL_ACTION_END_B6,
1028 .attrs = SEG6_F_ATTR(SEG6_LOCAL_SRH),
1029 .optattrs = SEG6_F_LOCAL_COUNTERS,
1030 .input = input_action_end_b6,
1033 .action = SEG6_LOCAL_ACTION_END_B6_ENCAP,
1034 .attrs = SEG6_F_ATTR(SEG6_LOCAL_SRH),
1035 .optattrs = SEG6_F_LOCAL_COUNTERS,
1036 .input = input_action_end_b6_encap,
1037 .static_headroom = sizeof(struct ipv6hdr),
1040 .action = SEG6_LOCAL_ACTION_END_BPF,
1041 .attrs = SEG6_F_ATTR(SEG6_LOCAL_BPF),
1042 .optattrs = SEG6_F_LOCAL_COUNTERS,
1043 .input = input_action_end_bpf,
1048 static struct seg6_action_desc *__get_action_desc(int action)
1050 struct seg6_action_desc *desc;
1053 count = ARRAY_SIZE(seg6_action_table);
1054 for (i = 0; i < count; i++) {
1055 desc = &seg6_action_table[i];
1056 if (desc->action == action)
1063 static bool seg6_lwtunnel_counters_enabled(struct seg6_local_lwt *slwt)
1065 return slwt->parsed_optattrs & SEG6_F_LOCAL_COUNTERS;
1068 static void seg6_local_update_counters(struct seg6_local_lwt *slwt,
1069 unsigned int len, int err)
1071 struct pcpu_seg6_local_counters *pcounters;
1073 pcounters = this_cpu_ptr(slwt->pcpu_counters);
1074 u64_stats_update_begin(&pcounters->syncp);
1077 u64_stats_inc(&pcounters->packets);
1078 u64_stats_add(&pcounters->bytes, len);
1080 u64_stats_inc(&pcounters->errors);
1083 u64_stats_update_end(&pcounters->syncp);
1086 static int seg6_local_input_core(struct net *net, struct sock *sk,
1087 struct sk_buff *skb)
1089 struct dst_entry *orig_dst = skb_dst(skb);
1090 struct seg6_action_desc *desc;
1091 struct seg6_local_lwt *slwt;
1092 unsigned int len = skb->len;
1095 slwt = seg6_local_lwtunnel(orig_dst->lwtstate);
1098 rc = desc->input(skb, slwt);
1100 if (!seg6_lwtunnel_counters_enabled(slwt))
1103 seg6_local_update_counters(slwt, len, rc);
1108 static int seg6_local_input(struct sk_buff *skb)
1110 if (skb->protocol != htons(ETH_P_IPV6)) {
1115 if (static_branch_unlikely(&nf_hooks_lwtunnel_enabled))
1116 return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_IN,
1117 dev_net(skb->dev), NULL, skb, skb->dev, NULL,
1118 seg6_local_input_core);
1120 return seg6_local_input_core(dev_net(skb->dev), NULL, skb);
1123 static const struct nla_policy seg6_local_policy[SEG6_LOCAL_MAX + 1] = {
1124 [SEG6_LOCAL_ACTION] = { .type = NLA_U32 },
1125 [SEG6_LOCAL_SRH] = { .type = NLA_BINARY },
1126 [SEG6_LOCAL_TABLE] = { .type = NLA_U32 },
1127 [SEG6_LOCAL_VRFTABLE] = { .type = NLA_U32 },
1128 [SEG6_LOCAL_NH4] = { .type = NLA_BINARY,
1129 .len = sizeof(struct in_addr) },
1130 [SEG6_LOCAL_NH6] = { .type = NLA_BINARY,
1131 .len = sizeof(struct in6_addr) },
1132 [SEG6_LOCAL_IIF] = { .type = NLA_U32 },
1133 [SEG6_LOCAL_OIF] = { .type = NLA_U32 },
1134 [SEG6_LOCAL_BPF] = { .type = NLA_NESTED },
1135 [SEG6_LOCAL_COUNTERS] = { .type = NLA_NESTED },
1138 static int parse_nla_srh(struct nlattr **attrs, struct seg6_local_lwt *slwt)
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)
1197 slwt->table = nla_get_u32(attrs[SEG6_LOCAL_TABLE]);
1202 static int put_nla_table(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1204 if (nla_put_u32(skb, SEG6_LOCAL_TABLE, slwt->table))
1210 static int cmp_nla_table(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1212 if (a->table != b->table)
1219 seg6_end_dt_info *seg6_possible_end_dt_info(struct seg6_local_lwt *slwt)
1221 #ifdef CONFIG_NET_L3_MASTER_DEV
1222 return &slwt->dt_info;
1224 return ERR_PTR(-EOPNOTSUPP);
1228 static int parse_nla_vrftable(struct nlattr **attrs,
1229 struct seg6_local_lwt *slwt)
1231 struct seg6_end_dt_info *info = seg6_possible_end_dt_info(slwt);
1234 return PTR_ERR(info);
1236 info->vrf_table = nla_get_u32(attrs[SEG6_LOCAL_VRFTABLE]);
1241 static int put_nla_vrftable(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1243 struct seg6_end_dt_info *info = seg6_possible_end_dt_info(slwt);
1246 return PTR_ERR(info);
1248 if (nla_put_u32(skb, SEG6_LOCAL_VRFTABLE, info->vrf_table))
1254 static int cmp_nla_vrftable(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1256 struct seg6_end_dt_info *info_a = seg6_possible_end_dt_info(a);
1257 struct seg6_end_dt_info *info_b = seg6_possible_end_dt_info(b);
1259 if (info_a->vrf_table != info_b->vrf_table)
1265 static int parse_nla_nh4(struct nlattr **attrs, struct seg6_local_lwt *slwt)
1267 memcpy(&slwt->nh4, nla_data(attrs[SEG6_LOCAL_NH4]),
1268 sizeof(struct in_addr));
1273 static int put_nla_nh4(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1277 nla = nla_reserve(skb, SEG6_LOCAL_NH4, sizeof(struct in_addr));
1281 memcpy(nla_data(nla), &slwt->nh4, sizeof(struct in_addr));
1286 static int cmp_nla_nh4(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1288 return memcmp(&a->nh4, &b->nh4, sizeof(struct in_addr));
1291 static int parse_nla_nh6(struct nlattr **attrs, struct seg6_local_lwt *slwt)
1293 memcpy(&slwt->nh6, nla_data(attrs[SEG6_LOCAL_NH6]),
1294 sizeof(struct in6_addr));
1299 static int put_nla_nh6(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1303 nla = nla_reserve(skb, SEG6_LOCAL_NH6, sizeof(struct in6_addr));
1307 memcpy(nla_data(nla), &slwt->nh6, sizeof(struct in6_addr));
1312 static int cmp_nla_nh6(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1314 return memcmp(&a->nh6, &b->nh6, sizeof(struct in6_addr));
1317 static int parse_nla_iif(struct nlattr **attrs, struct seg6_local_lwt *slwt)
1319 slwt->iif = nla_get_u32(attrs[SEG6_LOCAL_IIF]);
1324 static int put_nla_iif(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1326 if (nla_put_u32(skb, SEG6_LOCAL_IIF, slwt->iif))
1332 static int cmp_nla_iif(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1334 if (a->iif != b->iif)
1340 static int parse_nla_oif(struct nlattr **attrs, struct seg6_local_lwt *slwt)
1342 slwt->oif = nla_get_u32(attrs[SEG6_LOCAL_OIF]);
1347 static int put_nla_oif(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1349 if (nla_put_u32(skb, SEG6_LOCAL_OIF, slwt->oif))
1355 static int cmp_nla_oif(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1357 if (a->oif != b->oif)
1363 #define MAX_PROG_NAME 256
1364 static const struct nla_policy bpf_prog_policy[SEG6_LOCAL_BPF_PROG_MAX + 1] = {
1365 [SEG6_LOCAL_BPF_PROG] = { .type = NLA_U32, },
1366 [SEG6_LOCAL_BPF_PROG_NAME] = { .type = NLA_NUL_STRING,
1367 .len = MAX_PROG_NAME },
1370 static int parse_nla_bpf(struct nlattr **attrs, struct seg6_local_lwt *slwt)
1372 struct nlattr *tb[SEG6_LOCAL_BPF_PROG_MAX + 1];
1377 ret = nla_parse_nested_deprecated(tb, SEG6_LOCAL_BPF_PROG_MAX,
1378 attrs[SEG6_LOCAL_BPF],
1379 bpf_prog_policy, NULL);
1383 if (!tb[SEG6_LOCAL_BPF_PROG] || !tb[SEG6_LOCAL_BPF_PROG_NAME])
1386 slwt->bpf.name = nla_memdup(tb[SEG6_LOCAL_BPF_PROG_NAME], GFP_KERNEL);
1387 if (!slwt->bpf.name)
1390 fd = nla_get_u32(tb[SEG6_LOCAL_BPF_PROG]);
1391 p = bpf_prog_get_type(fd, BPF_PROG_TYPE_LWT_SEG6LOCAL);
1393 kfree(slwt->bpf.name);
1401 static int put_nla_bpf(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1403 struct nlattr *nest;
1405 if (!slwt->bpf.prog)
1408 nest = nla_nest_start_noflag(skb, SEG6_LOCAL_BPF);
1412 if (nla_put_u32(skb, SEG6_LOCAL_BPF_PROG, slwt->bpf.prog->aux->id))
1415 if (slwt->bpf.name &&
1416 nla_put_string(skb, SEG6_LOCAL_BPF_PROG_NAME, slwt->bpf.name))
1419 return nla_nest_end(skb, nest);
1422 static int cmp_nla_bpf(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1424 if (!a->bpf.name && !b->bpf.name)
1427 if (!a->bpf.name || !b->bpf.name)
1430 return strcmp(a->bpf.name, b->bpf.name);
1433 static void destroy_attr_bpf(struct seg6_local_lwt *slwt)
1435 kfree(slwt->bpf.name);
1437 bpf_prog_put(slwt->bpf.prog);
1441 nla_policy seg6_local_counters_policy[SEG6_LOCAL_CNT_MAX + 1] = {
1442 [SEG6_LOCAL_CNT_PACKETS] = { .type = NLA_U64 },
1443 [SEG6_LOCAL_CNT_BYTES] = { .type = NLA_U64 },
1444 [SEG6_LOCAL_CNT_ERRORS] = { .type = NLA_U64 },
1447 static int parse_nla_counters(struct nlattr **attrs,
1448 struct seg6_local_lwt *slwt)
1450 struct pcpu_seg6_local_counters __percpu *pcounters;
1451 struct nlattr *tb[SEG6_LOCAL_CNT_MAX + 1];
1454 ret = nla_parse_nested_deprecated(tb, SEG6_LOCAL_CNT_MAX,
1455 attrs[SEG6_LOCAL_COUNTERS],
1456 seg6_local_counters_policy, NULL);
1460 /* basic support for SRv6 Behavior counters requires at least:
1461 * packets, bytes and errors.
1463 if (!tb[SEG6_LOCAL_CNT_PACKETS] || !tb[SEG6_LOCAL_CNT_BYTES] ||
1464 !tb[SEG6_LOCAL_CNT_ERRORS])
1467 /* counters are always zero initialized */
1468 pcounters = seg6_local_alloc_pcpu_counters(GFP_KERNEL);
1472 slwt->pcpu_counters = pcounters;
1477 static int seg6_local_fill_nla_counters(struct sk_buff *skb,
1478 struct seg6_local_counters *counters)
1480 if (nla_put_u64_64bit(skb, SEG6_LOCAL_CNT_PACKETS, counters->packets,
1481 SEG6_LOCAL_CNT_PAD))
1484 if (nla_put_u64_64bit(skb, SEG6_LOCAL_CNT_BYTES, counters->bytes,
1485 SEG6_LOCAL_CNT_PAD))
1488 if (nla_put_u64_64bit(skb, SEG6_LOCAL_CNT_ERRORS, counters->errors,
1489 SEG6_LOCAL_CNT_PAD))
1495 static int put_nla_counters(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1497 struct seg6_local_counters counters = { 0, 0, 0 };
1498 struct nlattr *nest;
1501 nest = nla_nest_start(skb, SEG6_LOCAL_COUNTERS);
1505 for_each_possible_cpu(i) {
1506 struct pcpu_seg6_local_counters *pcounters;
1507 u64 packets, bytes, errors;
1510 pcounters = per_cpu_ptr(slwt->pcpu_counters, i);
1512 start = u64_stats_fetch_begin_irq(&pcounters->syncp);
1514 packets = u64_stats_read(&pcounters->packets);
1515 bytes = u64_stats_read(&pcounters->bytes);
1516 errors = u64_stats_read(&pcounters->errors);
1518 } while (u64_stats_fetch_retry_irq(&pcounters->syncp, start));
1520 counters.packets += packets;
1521 counters.bytes += bytes;
1522 counters.errors += errors;
1525 rc = seg6_local_fill_nla_counters(skb, &counters);
1527 nla_nest_cancel(skb, nest);
1531 return nla_nest_end(skb, nest);
1534 static int cmp_nla_counters(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1536 /* a and b are equal if both have pcpu_counters set or not */
1537 return (!!((unsigned long)a->pcpu_counters)) ^
1538 (!!((unsigned long)b->pcpu_counters));
1541 static void destroy_attr_counters(struct seg6_local_lwt *slwt)
1543 free_percpu(slwt->pcpu_counters);
1546 struct seg6_action_param {
1547 int (*parse)(struct nlattr **attrs, struct seg6_local_lwt *slwt);
1548 int (*put)(struct sk_buff *skb, struct seg6_local_lwt *slwt);
1549 int (*cmp)(struct seg6_local_lwt *a, struct seg6_local_lwt *b);
1551 /* optional destroy() callback useful for releasing resources which
1552 * have been previously acquired in the corresponding parse()
1555 void (*destroy)(struct seg6_local_lwt *slwt);
1558 static struct seg6_action_param seg6_action_params[SEG6_LOCAL_MAX + 1] = {
1559 [SEG6_LOCAL_SRH] = { .parse = parse_nla_srh,
1562 .destroy = destroy_attr_srh },
1564 [SEG6_LOCAL_TABLE] = { .parse = parse_nla_table,
1565 .put = put_nla_table,
1566 .cmp = cmp_nla_table },
1568 [SEG6_LOCAL_NH4] = { .parse = parse_nla_nh4,
1570 .cmp = cmp_nla_nh4 },
1572 [SEG6_LOCAL_NH6] = { .parse = parse_nla_nh6,
1574 .cmp = cmp_nla_nh6 },
1576 [SEG6_LOCAL_IIF] = { .parse = parse_nla_iif,
1578 .cmp = cmp_nla_iif },
1580 [SEG6_LOCAL_OIF] = { .parse = parse_nla_oif,
1582 .cmp = cmp_nla_oif },
1584 [SEG6_LOCAL_BPF] = { .parse = parse_nla_bpf,
1587 .destroy = destroy_attr_bpf },
1589 [SEG6_LOCAL_VRFTABLE] = { .parse = parse_nla_vrftable,
1590 .put = put_nla_vrftable,
1591 .cmp = cmp_nla_vrftable },
1593 [SEG6_LOCAL_COUNTERS] = { .parse = parse_nla_counters,
1594 .put = put_nla_counters,
1595 .cmp = cmp_nla_counters,
1596 .destroy = destroy_attr_counters },
1599 /* call the destroy() callback (if available) for each set attribute in
1600 * @parsed_attrs, starting from the first attribute up to the @max_parsed
1601 * (excluded) attribute.
1603 static void __destroy_attrs(unsigned long parsed_attrs, int max_parsed,
1604 struct seg6_local_lwt *slwt)
1606 struct seg6_action_param *param;
1609 /* Every required seg6local attribute is identified by an ID which is
1610 * encoded as a flag (i.e: 1 << ID) in the 'attrs' bitmask;
1612 * We scan the 'parsed_attrs' bitmask, starting from the first attribute
1613 * up to the @max_parsed (excluded) attribute.
1614 * For each set attribute, we retrieve the corresponding destroy()
1615 * callback. If the callback is not available, then we skip to the next
1616 * attribute; otherwise, we call the destroy() callback.
1618 for (i = 0; i < max_parsed; ++i) {
1619 if (!(parsed_attrs & SEG6_F_ATTR(i)))
1622 param = &seg6_action_params[i];
1625 param->destroy(slwt);
1629 /* release all the resources that may have been acquired during parsing
1632 static void destroy_attrs(struct seg6_local_lwt *slwt)
1634 unsigned long attrs = slwt->desc->attrs | slwt->parsed_optattrs;
1636 __destroy_attrs(attrs, SEG6_LOCAL_MAX + 1, slwt);
1639 static int parse_nla_optional_attrs(struct nlattr **attrs,
1640 struct seg6_local_lwt *slwt)
1642 struct seg6_action_desc *desc = slwt->desc;
1643 unsigned long parsed_optattrs = 0;
1644 struct seg6_action_param *param;
1647 for (i = 0; i < SEG6_LOCAL_MAX + 1; ++i) {
1648 if (!(desc->optattrs & SEG6_F_ATTR(i)) || !attrs[i])
1651 /* once here, the i-th attribute is provided by the
1652 * userspace AND it is identified optional as well.
1654 param = &seg6_action_params[i];
1656 err = param->parse(attrs, slwt);
1658 goto parse_optattrs_err;
1660 /* current attribute has been correctly parsed */
1661 parsed_optattrs |= SEG6_F_ATTR(i);
1664 /* store in the tunnel state all the optional attributed successfully
1667 slwt->parsed_optattrs = parsed_optattrs;
1672 __destroy_attrs(parsed_optattrs, i, slwt);
1677 /* call the custom constructor of the behavior during its initialization phase
1678 * and after that all its attributes have been parsed successfully.
1681 seg6_local_lwtunnel_build_state(struct seg6_local_lwt *slwt, const void *cfg,
1682 struct netlink_ext_ack *extack)
1684 struct seg6_action_desc *desc = slwt->desc;
1685 struct seg6_local_lwtunnel_ops *ops;
1687 ops = &desc->slwt_ops;
1688 if (!ops->build_state)
1691 return ops->build_state(slwt, cfg, extack);
1694 /* call the custom destructor of the behavior which is invoked before the
1695 * tunnel is going to be destroyed.
1697 static void seg6_local_lwtunnel_destroy_state(struct seg6_local_lwt *slwt)
1699 struct seg6_action_desc *desc = slwt->desc;
1700 struct seg6_local_lwtunnel_ops *ops;
1702 ops = &desc->slwt_ops;
1703 if (!ops->destroy_state)
1706 ops->destroy_state(slwt);
1709 static int parse_nla_action(struct nlattr **attrs, struct seg6_local_lwt *slwt)
1711 struct seg6_action_param *param;
1712 struct seg6_action_desc *desc;
1713 unsigned long invalid_attrs;
1716 desc = __get_action_desc(slwt->action);
1724 slwt->headroom += desc->static_headroom;
1726 /* Forcing the desc->optattrs *set* and the desc->attrs *set* to be
1727 * disjoined, this allow us to release acquired resources by optional
1728 * attributes and by required attributes independently from each other
1729 * without any interference.
1730 * In other terms, we are sure that we do not release some the acquired
1733 * Note that if an attribute is configured both as required and as
1734 * optional, it means that the user has messed something up in the
1735 * seg6_action_table. Therefore, this check is required for SRv6
1736 * behaviors to work properly.
1738 invalid_attrs = desc->attrs & desc->optattrs;
1739 if (invalid_attrs) {
1741 "An attribute cannot be both required AND optional");
1745 /* parse the required attributes */
1746 for (i = 0; i < SEG6_LOCAL_MAX + 1; i++) {
1747 if (desc->attrs & SEG6_F_ATTR(i)) {
1751 param = &seg6_action_params[i];
1753 err = param->parse(attrs, slwt);
1755 goto parse_attrs_err;
1759 /* parse the optional attributes, if any */
1760 err = parse_nla_optional_attrs(attrs, slwt);
1762 goto parse_attrs_err;
1767 /* release any resource that may have been acquired during the i-1
1768 * parse() operations.
1770 __destroy_attrs(desc->attrs, i, slwt);
1775 static int seg6_local_build_state(struct net *net, struct nlattr *nla,
1776 unsigned int family, const void *cfg,
1777 struct lwtunnel_state **ts,
1778 struct netlink_ext_ack *extack)
1780 struct nlattr *tb[SEG6_LOCAL_MAX + 1];
1781 struct lwtunnel_state *newts;
1782 struct seg6_local_lwt *slwt;
1785 if (family != AF_INET6)
1788 err = nla_parse_nested_deprecated(tb, SEG6_LOCAL_MAX, nla,
1789 seg6_local_policy, extack);
1794 if (!tb[SEG6_LOCAL_ACTION])
1797 newts = lwtunnel_state_alloc(sizeof(*slwt));
1801 slwt = seg6_local_lwtunnel(newts);
1802 slwt->action = nla_get_u32(tb[SEG6_LOCAL_ACTION]);
1804 err = parse_nla_action(tb, slwt);
1808 err = seg6_local_lwtunnel_build_state(slwt, cfg, extack);
1810 goto out_destroy_attrs;
1812 newts->type = LWTUNNEL_ENCAP_SEG6_LOCAL;
1813 newts->flags = LWTUNNEL_STATE_INPUT_REDIRECT;
1814 newts->headroom = slwt->headroom;
1821 destroy_attrs(slwt);
1827 static void seg6_local_destroy_state(struct lwtunnel_state *lwt)
1829 struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
1831 seg6_local_lwtunnel_destroy_state(slwt);
1833 destroy_attrs(slwt);
1838 static int seg6_local_fill_encap(struct sk_buff *skb,
1839 struct lwtunnel_state *lwt)
1841 struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
1842 struct seg6_action_param *param;
1843 unsigned long attrs;
1846 if (nla_put_u32(skb, SEG6_LOCAL_ACTION, slwt->action))
1849 attrs = slwt->desc->attrs | slwt->parsed_optattrs;
1851 for (i = 0; i < SEG6_LOCAL_MAX + 1; i++) {
1852 if (attrs & SEG6_F_ATTR(i)) {
1853 param = &seg6_action_params[i];
1854 err = param->put(skb, slwt);
1863 static int seg6_local_get_encap_size(struct lwtunnel_state *lwt)
1865 struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
1866 unsigned long attrs;
1869 nlsize = nla_total_size(4); /* action */
1871 attrs = slwt->desc->attrs | slwt->parsed_optattrs;
1873 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_SRH))
1874 nlsize += nla_total_size((slwt->srh->hdrlen + 1) << 3);
1876 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_TABLE))
1877 nlsize += nla_total_size(4);
1879 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_NH4))
1880 nlsize += nla_total_size(4);
1882 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_NH6))
1883 nlsize += nla_total_size(16);
1885 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_IIF))
1886 nlsize += nla_total_size(4);
1888 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_OIF))
1889 nlsize += nla_total_size(4);
1891 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_BPF))
1892 nlsize += nla_total_size(sizeof(struct nlattr)) +
1893 nla_total_size(MAX_PROG_NAME) +
1896 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_VRFTABLE))
1897 nlsize += nla_total_size(4);
1899 if (attrs & SEG6_F_LOCAL_COUNTERS)
1900 nlsize += nla_total_size(0) + /* nest SEG6_LOCAL_COUNTERS */
1901 /* SEG6_LOCAL_CNT_PACKETS */
1902 nla_total_size_64bit(sizeof(__u64)) +
1903 /* SEG6_LOCAL_CNT_BYTES */
1904 nla_total_size_64bit(sizeof(__u64)) +
1905 /* SEG6_LOCAL_CNT_ERRORS */
1906 nla_total_size_64bit(sizeof(__u64));
1911 static int seg6_local_cmp_encap(struct lwtunnel_state *a,
1912 struct lwtunnel_state *b)
1914 struct seg6_local_lwt *slwt_a, *slwt_b;
1915 struct seg6_action_param *param;
1916 unsigned long attrs_a, attrs_b;
1919 slwt_a = seg6_local_lwtunnel(a);
1920 slwt_b = seg6_local_lwtunnel(b);
1922 if (slwt_a->action != slwt_b->action)
1925 attrs_a = slwt_a->desc->attrs | slwt_a->parsed_optattrs;
1926 attrs_b = slwt_b->desc->attrs | slwt_b->parsed_optattrs;
1928 if (attrs_a != attrs_b)
1931 for (i = 0; i < SEG6_LOCAL_MAX + 1; i++) {
1932 if (attrs_a & SEG6_F_ATTR(i)) {
1933 param = &seg6_action_params[i];
1934 if (param->cmp(slwt_a, slwt_b))
1942 static const struct lwtunnel_encap_ops seg6_local_ops = {
1943 .build_state = seg6_local_build_state,
1944 .destroy_state = seg6_local_destroy_state,
1945 .input = seg6_local_input,
1946 .fill_encap = seg6_local_fill_encap,
1947 .get_encap_size = seg6_local_get_encap_size,
1948 .cmp_encap = seg6_local_cmp_encap,
1949 .owner = THIS_MODULE,
1952 int __init seg6_local_init(void)
1954 /* If the max total number of defined attributes is reached, then your
1955 * kernel build stops here.
1957 * This check is required to avoid arithmetic overflows when processing
1958 * behavior attributes and the maximum number of defined attributes
1959 * exceeds the allowed value.
1961 BUILD_BUG_ON(SEG6_LOCAL_MAX + 1 > BITS_PER_TYPE(unsigned long));
1963 return lwtunnel_encap_add_ops(&seg6_local_ops,
1964 LWTUNNEL_ENCAP_SEG6_LOCAL);
1967 void seg6_local_exit(void)
1969 lwtunnel_encap_del_ops(&seg6_local_ops, LWTUNNEL_ENCAP_SEG6_LOCAL);