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/types.h>
11 #include <linux/skbuff.h>
12 #include <linux/net.h>
13 #include <linux/module.h>
15 #include <net/lwtunnel.h>
16 #include <net/netevent.h>
17 #include <net/netns/generic.h>
18 #include <net/ip6_fib.h>
19 #include <net/route.h>
21 #include <linux/seg6.h>
22 #include <linux/seg6_local.h>
23 #include <net/addrconf.h>
24 #include <net/ip6_route.h>
25 #include <net/dst_cache.h>
26 #include <net/ip_tunnels.h>
27 #ifdef CONFIG_IPV6_SEG6_HMAC
28 #include <net/seg6_hmac.h>
30 #include <net/seg6_local.h>
31 #include <linux/etherdevice.h>
32 #include <linux/bpf.h>
33 #include <linux/netfilter.h>
35 #define SEG6_F_ATTR(i) BIT(i)
37 struct seg6_local_lwt;
39 /* callbacks used for customizing the creation and destruction of a behavior */
40 struct seg6_local_lwtunnel_ops {
41 int (*build_state)(struct seg6_local_lwt *slwt, const void *cfg,
42 struct netlink_ext_ack *extack);
43 void (*destroy_state)(struct seg6_local_lwt *slwt);
46 struct seg6_action_desc {
50 /* The optattrs field is used for specifying all the optional
51 * attributes supported by a specific behavior.
52 * It means that if one of these attributes is not provided in the
53 * netlink message during the behavior creation, no errors will be
54 * returned to the userspace.
56 * Each attribute can be only of two types (mutually exclusive):
57 * 1) required or 2) optional.
58 * Every user MUST obey to this rule! If you set an attribute as
59 * required the same attribute CANNOT be set as optional and vice
62 unsigned long optattrs;
64 int (*input)(struct sk_buff *skb, struct seg6_local_lwt *slwt);
67 struct seg6_local_lwtunnel_ops slwt_ops;
71 struct bpf_prog *prog;
75 enum seg6_end_dt_mode {
76 DT_INVALID_MODE = -EINVAL,
81 struct seg6_end_dt_info {
82 enum seg6_end_dt_mode mode;
85 /* VRF device associated to the routing table used by the SRv6
86 * End.DT4/DT6 behavior for routing IPv4/IPv6 packets.
91 /* tunneled packet family (IPv4 or IPv6).
92 * Protocol and header length are inferred from family.
97 struct pcpu_seg6_local_counters {
102 struct u64_stats_sync syncp;
105 /* This struct groups all the SRv6 Behavior counters supported so far.
107 * put_nla_counters() makes use of this data structure to collect all counter
108 * values after the per-CPU counter evaluation has been performed.
109 * Finally, each counter value (in seg6_local_counters) is stored in the
110 * corresponding netlink attribute and sent to user space.
112 * NB: we don't want to expose this structure to user space!
114 struct seg6_local_counters {
120 #define seg6_local_alloc_pcpu_counters(__gfp) \
121 __netdev_alloc_pcpu_stats(struct pcpu_seg6_local_counters, \
122 ((__gfp) | __GFP_ZERO))
124 #define SEG6_F_LOCAL_COUNTERS SEG6_F_ATTR(SEG6_LOCAL_COUNTERS)
126 struct seg6_local_lwt {
128 struct ipv6_sr_hdr *srh;
134 struct bpf_lwt_prog bpf;
135 #ifdef CONFIG_NET_L3_MASTER_DEV
136 struct seg6_end_dt_info dt_info;
138 struct pcpu_seg6_local_counters __percpu *pcpu_counters;
141 struct seg6_action_desc *desc;
142 /* unlike the required attrs, we have to track the optional attributes
143 * that have been effectively parsed.
145 unsigned long parsed_optattrs;
148 static struct seg6_local_lwt *seg6_local_lwtunnel(struct lwtunnel_state *lwt)
150 return (struct seg6_local_lwt *)lwt->data;
153 static struct ipv6_sr_hdr *get_srh(struct sk_buff *skb, int flags)
155 struct ipv6_sr_hdr *srh;
158 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, &flags) < 0)
161 if (!pskb_may_pull(skb, srhoff + sizeof(*srh)))
164 srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
166 len = (srh->hdrlen + 1) << 3;
168 if (!pskb_may_pull(skb, srhoff + len))
171 /* note that pskb_may_pull may change pointers in header;
172 * for this reason it is necessary to reload them when needed.
174 srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
176 if (!seg6_validate_srh(srh, len, true))
182 static struct ipv6_sr_hdr *get_and_validate_srh(struct sk_buff *skb)
184 struct ipv6_sr_hdr *srh;
186 srh = get_srh(skb, IP6_FH_F_SKIP_RH);
190 #ifdef CONFIG_IPV6_SEG6_HMAC
191 if (!seg6_hmac_validate_skb(skb))
198 static bool decap_and_validate(struct sk_buff *skb, int proto)
200 struct ipv6_sr_hdr *srh;
201 unsigned int off = 0;
203 srh = get_srh(skb, 0);
204 if (srh && srh->segments_left > 0)
207 #ifdef CONFIG_IPV6_SEG6_HMAC
208 if (srh && !seg6_hmac_validate_skb(skb))
212 if (ipv6_find_hdr(skb, &off, proto, NULL, NULL) < 0)
215 if (!pskb_pull(skb, off))
218 skb_postpull_rcsum(skb, skb_network_header(skb), off);
220 skb_reset_network_header(skb);
221 skb_reset_transport_header(skb);
222 if (iptunnel_pull_offloads(skb))
228 static void advance_nextseg(struct ipv6_sr_hdr *srh, struct in6_addr *daddr)
230 struct in6_addr *addr;
232 srh->segments_left--;
233 addr = srh->segments + srh->segments_left;
238 seg6_lookup_any_nexthop(struct sk_buff *skb, struct in6_addr *nhaddr,
239 u32 tbl_id, bool local_delivery)
241 struct net *net = dev_net(skb->dev);
242 struct ipv6hdr *hdr = ipv6_hdr(skb);
243 int flags = RT6_LOOKUP_F_HAS_SADDR;
244 struct dst_entry *dst = NULL;
249 fl6.flowi6_iif = skb->dev->ifindex;
250 fl6.daddr = nhaddr ? *nhaddr : hdr->daddr;
251 fl6.saddr = hdr->saddr;
252 fl6.flowlabel = ip6_flowinfo(hdr);
253 fl6.flowi6_mark = skb->mark;
254 fl6.flowi6_proto = hdr->nexthdr;
257 fl6.flowi6_flags = FLOWI_FLAG_KNOWN_NH;
260 dst = ip6_route_input_lookup(net, skb->dev, &fl6, skb, flags);
262 struct fib6_table *table;
264 table = fib6_get_table(net, tbl_id);
268 rt = ip6_pol_route(net, table, 0, &fl6, skb, flags);
272 /* we want to discard traffic destined for local packet processing,
273 * if @local_delivery is set to false.
276 dev_flags |= IFF_LOOPBACK;
278 if (dst && (dst->dev->flags & dev_flags) && !dst->error) {
285 rt = net->ipv6.ip6_blk_hole_entry;
291 skb_dst_set(skb, dst);
295 int seg6_lookup_nexthop(struct sk_buff *skb,
296 struct in6_addr *nhaddr, u32 tbl_id)
298 return seg6_lookup_any_nexthop(skb, nhaddr, tbl_id, false);
301 /* regular endpoint function */
302 static int input_action_end(struct sk_buff *skb, struct seg6_local_lwt *slwt)
304 struct ipv6_sr_hdr *srh;
306 srh = get_and_validate_srh(skb);
310 advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
312 seg6_lookup_nexthop(skb, NULL, 0);
314 return dst_input(skb);
321 /* regular endpoint, and forward to specified nexthop */
322 static int input_action_end_x(struct sk_buff *skb, struct seg6_local_lwt *slwt)
324 struct ipv6_sr_hdr *srh;
326 srh = get_and_validate_srh(skb);
330 advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
332 seg6_lookup_nexthop(skb, &slwt->nh6, 0);
334 return dst_input(skb);
341 static int input_action_end_t(struct sk_buff *skb, struct seg6_local_lwt *slwt)
343 struct ipv6_sr_hdr *srh;
345 srh = get_and_validate_srh(skb);
349 advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
351 seg6_lookup_nexthop(skb, NULL, slwt->table);
353 return dst_input(skb);
360 /* decapsulate and forward inner L2 frame on specified interface */
361 static int input_action_end_dx2(struct sk_buff *skb,
362 struct seg6_local_lwt *slwt)
364 struct net *net = dev_net(skb->dev);
365 struct net_device *odev;
368 if (!decap_and_validate(skb, IPPROTO_ETHERNET))
371 if (!pskb_may_pull(skb, ETH_HLEN))
374 skb_reset_mac_header(skb);
375 eth = (struct ethhdr *)skb->data;
377 /* To determine the frame's protocol, we assume it is 802.3. This avoids
378 * a call to eth_type_trans(), which is not really relevant for our
381 if (!eth_proto_is_802_3(eth->h_proto))
384 odev = dev_get_by_index_rcu(net, slwt->oif);
388 /* As we accept Ethernet frames, make sure the egress device is of
391 if (odev->type != ARPHRD_ETHER)
394 if (!(odev->flags & IFF_UP) || !netif_carrier_ok(odev))
399 if (skb_warn_if_lro(skb))
402 skb_forward_csum(skb);
404 if (skb->len - ETH_HLEN > odev->mtu)
408 skb->protocol = eth->h_proto;
410 return dev_queue_xmit(skb);
417 static int input_action_end_dx6_finish(struct net *net, struct sock *sk,
420 struct dst_entry *orig_dst = skb_dst(skb);
421 struct in6_addr *nhaddr = NULL;
422 struct seg6_local_lwt *slwt;
424 slwt = seg6_local_lwtunnel(orig_dst->lwtstate);
426 /* The inner packet is not associated to any local interface,
427 * so we do not call netif_rx().
429 * If slwt->nh6 is set to ::, then lookup the nexthop for the
430 * inner packet's DA. Otherwise, use the specified nexthop.
432 if (!ipv6_addr_any(&slwt->nh6))
435 seg6_lookup_nexthop(skb, nhaddr, 0);
437 return dst_input(skb);
440 /* decapsulate and forward to specified nexthop */
441 static int input_action_end_dx6(struct sk_buff *skb,
442 struct seg6_local_lwt *slwt)
444 /* this function accepts IPv6 encapsulated packets, with either
445 * an SRH with SL=0, or no SRH.
448 if (!decap_and_validate(skb, IPPROTO_IPV6))
451 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
454 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
457 if (static_branch_unlikely(&nf_hooks_lwtunnel_enabled))
458 return NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING,
459 dev_net(skb->dev), NULL, skb, NULL,
460 skb_dst(skb)->dev, input_action_end_dx6_finish);
462 return input_action_end_dx6_finish(dev_net(skb->dev), NULL, skb);
468 static int input_action_end_dx4_finish(struct net *net, struct sock *sk,
471 struct dst_entry *orig_dst = skb_dst(skb);
472 struct seg6_local_lwt *slwt;
477 slwt = seg6_local_lwtunnel(orig_dst->lwtstate);
481 nhaddr = slwt->nh4.s_addr ?: iph->daddr;
485 err = ip_route_input(skb, nhaddr, iph->saddr, 0, skb->dev);
491 return dst_input(skb);
494 static int input_action_end_dx4(struct sk_buff *skb,
495 struct seg6_local_lwt *slwt)
497 if (!decap_and_validate(skb, IPPROTO_IPIP))
500 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
503 skb->protocol = htons(ETH_P_IP);
504 skb_set_transport_header(skb, sizeof(struct iphdr));
507 if (static_branch_unlikely(&nf_hooks_lwtunnel_enabled))
508 return NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING,
509 dev_net(skb->dev), NULL, skb, NULL,
510 skb_dst(skb)->dev, input_action_end_dx4_finish);
512 return input_action_end_dx4_finish(dev_net(skb->dev), NULL, skb);
518 #ifdef CONFIG_NET_L3_MASTER_DEV
519 static struct net *fib6_config_get_net(const struct fib6_config *fib6_cfg)
521 const struct nl_info *nli = &fib6_cfg->fc_nlinfo;
526 static int __seg6_end_dt_vrf_build(struct seg6_local_lwt *slwt, const void *cfg,
527 u16 family, struct netlink_ext_ack *extack)
529 struct seg6_end_dt_info *info = &slwt->dt_info;
533 net = fib6_config_get_net(cfg);
535 /* note that vrf_table was already set by parse_nla_vrftable() */
536 vrf_ifindex = l3mdev_ifindex_lookup_by_table_id(L3MDEV_TYPE_VRF, net,
538 if (vrf_ifindex < 0) {
539 if (vrf_ifindex == -EPERM) {
540 NL_SET_ERR_MSG(extack,
541 "Strict mode for VRF is disabled");
542 } else if (vrf_ifindex == -ENODEV) {
543 NL_SET_ERR_MSG(extack,
544 "Table has no associated VRF device");
546 pr_debug("seg6local: SRv6 End.DT* creation error=%d\n",
554 info->vrf_ifindex = vrf_ifindex;
556 info->family = family;
557 info->mode = DT_VRF_MODE;
562 /* The SRv6 End.DT4/DT6 behavior extracts the inner (IPv4/IPv6) packet and
563 * routes the IPv4/IPv6 packet by looking at the configured routing table.
565 * In the SRv6 End.DT4/DT6 use case, we can receive traffic (IPv6+Segment
566 * Routing Header packets) from several interfaces and the outer IPv6
567 * destination address (DA) is used for retrieving the specific instance of the
568 * End.DT4/DT6 behavior that should process the packets.
570 * However, the inner IPv4/IPv6 packet is not really bound to any receiving
571 * interface and thus the End.DT4/DT6 sets the VRF (associated with the
572 * corresponding routing table) as the *receiving* interface.
573 * In other words, the End.DT4/DT6 processes a packet as if it has been received
574 * directly by the VRF (and not by one of its slave devices, if any).
575 * In this way, the VRF interface is used for routing the IPv4/IPv6 packet in
576 * according to the routing table configured by the End.DT4/DT6 instance.
578 * This design allows you to get some interesting features like:
579 * 1) the statistics on rx packets;
580 * 2) the possibility to install a packet sniffer on the receiving interface
581 * (the VRF one) for looking at the incoming packets;
582 * 3) the possibility to leverage the netfilter prerouting hook for the inner
585 * This function returns:
586 * - the sk_buff* when the VRF rcv handler has processed the packet correctly;
587 * - NULL when the skb is consumed by the VRF rcv handler;
588 * - a pointer which encodes a negative error number in case of error.
589 * Note that in this case, the function takes care of freeing the skb.
591 static struct sk_buff *end_dt_vrf_rcv(struct sk_buff *skb, u16 family,
592 struct net_device *dev)
594 /* based on l3mdev_ip_rcv; we are only interested in the master */
595 if (unlikely(!netif_is_l3_master(dev) && !netif_has_l3_rx_handler(dev)))
598 if (unlikely(!dev->l3mdev_ops->l3mdev_l3_rcv))
601 /* the decap packet IPv4/IPv6 does not come with any mac header info.
602 * We must unset the mac header to allow the VRF device to rebuild it,
603 * just in case there is a sniffer attached on the device.
605 skb_unset_mac_header(skb);
607 skb = dev->l3mdev_ops->l3mdev_l3_rcv(dev, skb, family);
609 /* the skb buffer was consumed by the handler */
612 /* when a packet is received by a VRF or by one of its slaves, the
613 * master device reference is set into the skb.
615 if (unlikely(skb->dev != dev || skb->skb_iif != dev->ifindex))
622 return ERR_PTR(-EINVAL);
625 static struct net_device *end_dt_get_vrf_rcu(struct sk_buff *skb,
626 struct seg6_end_dt_info *info)
628 int vrf_ifindex = info->vrf_ifindex;
629 struct net *net = info->net;
631 if (unlikely(vrf_ifindex < 0))
634 if (unlikely(!net_eq(dev_net(skb->dev), net)))
637 return dev_get_by_index_rcu(net, vrf_ifindex);
643 static struct sk_buff *end_dt_vrf_core(struct sk_buff *skb,
644 struct seg6_local_lwt *slwt, u16 family)
646 struct seg6_end_dt_info *info = &slwt->dt_info;
647 struct net_device *vrf;
651 vrf = end_dt_get_vrf_rcu(skb, info);
657 protocol = htons(ETH_P_IP);
658 hdrlen = sizeof(struct iphdr);
661 protocol = htons(ETH_P_IPV6);
662 hdrlen = sizeof(struct ipv6hdr);
670 if (unlikely(info->family != AF_UNSPEC && info->family != family)) {
671 pr_warn_once("seg6local: SRv6 End.DT* family mismatch");
675 skb->protocol = protocol;
679 skb_set_transport_header(skb, hdrlen);
682 return end_dt_vrf_rcv(skb, family, vrf);
686 return ERR_PTR(-EINVAL);
689 static int input_action_end_dt4(struct sk_buff *skb,
690 struct seg6_local_lwt *slwt)
695 if (!decap_and_validate(skb, IPPROTO_IPIP))
698 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
701 skb = end_dt_vrf_core(skb, slwt, AF_INET);
703 /* packet has been processed and consumed by the VRF */
711 err = ip_route_input(skb, iph->daddr, iph->saddr, 0, skb->dev);
715 return dst_input(skb);
722 static int seg6_end_dt4_build(struct seg6_local_lwt *slwt, const void *cfg,
723 struct netlink_ext_ack *extack)
725 return __seg6_end_dt_vrf_build(slwt, cfg, AF_INET, extack);
729 seg6_end_dt_mode seg6_end_dt6_parse_mode(struct seg6_local_lwt *slwt)
731 unsigned long parsed_optattrs = slwt->parsed_optattrs;
732 bool legacy, vrfmode;
734 legacy = !!(parsed_optattrs & SEG6_F_ATTR(SEG6_LOCAL_TABLE));
735 vrfmode = !!(parsed_optattrs & SEG6_F_ATTR(SEG6_LOCAL_VRFTABLE));
737 if (!(legacy ^ vrfmode))
738 /* both are absent or present: invalid DT6 mode */
739 return DT_INVALID_MODE;
741 return legacy ? DT_LEGACY_MODE : DT_VRF_MODE;
744 static enum seg6_end_dt_mode seg6_end_dt6_get_mode(struct seg6_local_lwt *slwt)
746 struct seg6_end_dt_info *info = &slwt->dt_info;
751 static int seg6_end_dt6_build(struct seg6_local_lwt *slwt, const void *cfg,
752 struct netlink_ext_ack *extack)
754 enum seg6_end_dt_mode mode = seg6_end_dt6_parse_mode(slwt);
755 struct seg6_end_dt_info *info = &slwt->dt_info;
759 info->mode = DT_LEGACY_MODE;
762 return __seg6_end_dt_vrf_build(slwt, cfg, AF_INET6, extack);
764 NL_SET_ERR_MSG(extack, "table or vrftable must be specified");
770 static int input_action_end_dt6(struct sk_buff *skb,
771 struct seg6_local_lwt *slwt)
773 if (!decap_and_validate(skb, IPPROTO_IPV6))
776 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
779 #ifdef CONFIG_NET_L3_MASTER_DEV
780 if (seg6_end_dt6_get_mode(slwt) == DT_LEGACY_MODE)
784 skb = end_dt_vrf_core(skb, slwt, AF_INET6);
786 /* packet has been processed and consumed by the VRF */
792 /* note: this time we do not need to specify the table because the VRF
793 * takes care of selecting the correct table.
795 seg6_lookup_any_nexthop(skb, NULL, 0, true);
797 return dst_input(skb);
801 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
803 seg6_lookup_any_nexthop(skb, NULL, slwt->table, true);
805 return dst_input(skb);
812 #ifdef CONFIG_NET_L3_MASTER_DEV
813 static int seg6_end_dt46_build(struct seg6_local_lwt *slwt, const void *cfg,
814 struct netlink_ext_ack *extack)
816 return __seg6_end_dt_vrf_build(slwt, cfg, AF_UNSPEC, extack);
819 static int input_action_end_dt46(struct sk_buff *skb,
820 struct seg6_local_lwt *slwt)
822 unsigned int off = 0;
825 nexthdr = ipv6_find_hdr(skb, &off, -1, NULL, NULL);
826 if (unlikely(nexthdr < 0))
831 return input_action_end_dt4(skb, slwt);
833 return input_action_end_dt6(skb, slwt);
842 /* push an SRH on top of the current one */
843 static int input_action_end_b6(struct sk_buff *skb, struct seg6_local_lwt *slwt)
845 struct ipv6_sr_hdr *srh;
848 srh = get_and_validate_srh(skb);
852 err = seg6_do_srh_inline(skb, slwt->srh);
856 ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
857 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
859 seg6_lookup_nexthop(skb, NULL, 0);
861 return dst_input(skb);
868 /* encapsulate within an outer IPv6 header and a specified SRH */
869 static int input_action_end_b6_encap(struct sk_buff *skb,
870 struct seg6_local_lwt *slwt)
872 struct ipv6_sr_hdr *srh;
875 srh = get_and_validate_srh(skb);
879 advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
881 skb_reset_inner_headers(skb);
882 skb->encapsulation = 1;
884 err = seg6_do_srh_encap(skb, slwt->srh, IPPROTO_IPV6);
888 ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
889 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
891 seg6_lookup_nexthop(skb, NULL, 0);
893 return dst_input(skb);
900 DEFINE_PER_CPU(struct seg6_bpf_srh_state, seg6_bpf_srh_states);
902 bool seg6_bpf_has_valid_srh(struct sk_buff *skb)
904 struct seg6_bpf_srh_state *srh_state =
905 this_cpu_ptr(&seg6_bpf_srh_states);
906 struct ipv6_sr_hdr *srh = srh_state->srh;
908 if (unlikely(srh == NULL))
911 if (unlikely(!srh_state->valid)) {
912 if ((srh_state->hdrlen & 7) != 0)
915 srh->hdrlen = (u8)(srh_state->hdrlen >> 3);
916 if (!seg6_validate_srh(srh, (srh->hdrlen + 1) << 3, true))
919 srh_state->valid = true;
925 static int input_action_end_bpf(struct sk_buff *skb,
926 struct seg6_local_lwt *slwt)
928 struct seg6_bpf_srh_state *srh_state =
929 this_cpu_ptr(&seg6_bpf_srh_states);
930 struct ipv6_sr_hdr *srh;
933 srh = get_and_validate_srh(skb);
938 advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
940 /* preempt_disable is needed to protect the per-CPU buffer srh_state,
941 * which is also accessed by the bpf_lwt_seg6_* helpers
944 srh_state->srh = srh;
945 srh_state->hdrlen = srh->hdrlen << 3;
946 srh_state->valid = true;
949 bpf_compute_data_pointers(skb);
950 ret = bpf_prog_run_save_cb(slwt->bpf.prog, skb);
960 pr_warn_once("bpf-seg6local: Illegal return value %u\n", ret);
964 if (srh_state->srh && !seg6_bpf_has_valid_srh(skb))
968 if (ret != BPF_REDIRECT)
969 seg6_lookup_nexthop(skb, NULL, 0);
971 return dst_input(skb);
979 static struct seg6_action_desc seg6_action_table[] = {
981 .action = SEG6_LOCAL_ACTION_END,
983 .optattrs = SEG6_F_LOCAL_COUNTERS,
984 .input = input_action_end,
987 .action = SEG6_LOCAL_ACTION_END_X,
988 .attrs = SEG6_F_ATTR(SEG6_LOCAL_NH6),
989 .optattrs = SEG6_F_LOCAL_COUNTERS,
990 .input = input_action_end_x,
993 .action = SEG6_LOCAL_ACTION_END_T,
994 .attrs = SEG6_F_ATTR(SEG6_LOCAL_TABLE),
995 .optattrs = SEG6_F_LOCAL_COUNTERS,
996 .input = input_action_end_t,
999 .action = SEG6_LOCAL_ACTION_END_DX2,
1000 .attrs = SEG6_F_ATTR(SEG6_LOCAL_OIF),
1001 .optattrs = SEG6_F_LOCAL_COUNTERS,
1002 .input = input_action_end_dx2,
1005 .action = SEG6_LOCAL_ACTION_END_DX6,
1006 .attrs = SEG6_F_ATTR(SEG6_LOCAL_NH6),
1007 .optattrs = SEG6_F_LOCAL_COUNTERS,
1008 .input = input_action_end_dx6,
1011 .action = SEG6_LOCAL_ACTION_END_DX4,
1012 .attrs = SEG6_F_ATTR(SEG6_LOCAL_NH4),
1013 .optattrs = SEG6_F_LOCAL_COUNTERS,
1014 .input = input_action_end_dx4,
1017 .action = SEG6_LOCAL_ACTION_END_DT4,
1018 .attrs = SEG6_F_ATTR(SEG6_LOCAL_VRFTABLE),
1019 .optattrs = SEG6_F_LOCAL_COUNTERS,
1020 #ifdef CONFIG_NET_L3_MASTER_DEV
1021 .input = input_action_end_dt4,
1023 .build_state = seg6_end_dt4_build,
1028 .action = SEG6_LOCAL_ACTION_END_DT6,
1029 #ifdef CONFIG_NET_L3_MASTER_DEV
1031 .optattrs = SEG6_F_LOCAL_COUNTERS |
1032 SEG6_F_ATTR(SEG6_LOCAL_TABLE) |
1033 SEG6_F_ATTR(SEG6_LOCAL_VRFTABLE),
1035 .build_state = seg6_end_dt6_build,
1038 .attrs = SEG6_F_ATTR(SEG6_LOCAL_TABLE),
1039 .optattrs = SEG6_F_LOCAL_COUNTERS,
1041 .input = input_action_end_dt6,
1044 .action = SEG6_LOCAL_ACTION_END_DT46,
1045 .attrs = SEG6_F_ATTR(SEG6_LOCAL_VRFTABLE),
1046 .optattrs = SEG6_F_LOCAL_COUNTERS,
1047 #ifdef CONFIG_NET_L3_MASTER_DEV
1048 .input = input_action_end_dt46,
1050 .build_state = seg6_end_dt46_build,
1055 .action = SEG6_LOCAL_ACTION_END_B6,
1056 .attrs = SEG6_F_ATTR(SEG6_LOCAL_SRH),
1057 .optattrs = SEG6_F_LOCAL_COUNTERS,
1058 .input = input_action_end_b6,
1061 .action = SEG6_LOCAL_ACTION_END_B6_ENCAP,
1062 .attrs = SEG6_F_ATTR(SEG6_LOCAL_SRH),
1063 .optattrs = SEG6_F_LOCAL_COUNTERS,
1064 .input = input_action_end_b6_encap,
1065 .static_headroom = sizeof(struct ipv6hdr),
1068 .action = SEG6_LOCAL_ACTION_END_BPF,
1069 .attrs = SEG6_F_ATTR(SEG6_LOCAL_BPF),
1070 .optattrs = SEG6_F_LOCAL_COUNTERS,
1071 .input = input_action_end_bpf,
1076 static struct seg6_action_desc *__get_action_desc(int action)
1078 struct seg6_action_desc *desc;
1081 count = ARRAY_SIZE(seg6_action_table);
1082 for (i = 0; i < count; i++) {
1083 desc = &seg6_action_table[i];
1084 if (desc->action == action)
1091 static bool seg6_lwtunnel_counters_enabled(struct seg6_local_lwt *slwt)
1093 return slwt->parsed_optattrs & SEG6_F_LOCAL_COUNTERS;
1096 static void seg6_local_update_counters(struct seg6_local_lwt *slwt,
1097 unsigned int len, int err)
1099 struct pcpu_seg6_local_counters *pcounters;
1101 pcounters = this_cpu_ptr(slwt->pcpu_counters);
1102 u64_stats_update_begin(&pcounters->syncp);
1105 u64_stats_inc(&pcounters->packets);
1106 u64_stats_add(&pcounters->bytes, len);
1108 u64_stats_inc(&pcounters->errors);
1111 u64_stats_update_end(&pcounters->syncp);
1114 static int seg6_local_input_core(struct net *net, struct sock *sk,
1115 struct sk_buff *skb)
1117 struct dst_entry *orig_dst = skb_dst(skb);
1118 struct seg6_action_desc *desc;
1119 struct seg6_local_lwt *slwt;
1120 unsigned int len = skb->len;
1123 slwt = seg6_local_lwtunnel(orig_dst->lwtstate);
1126 rc = desc->input(skb, slwt);
1128 if (!seg6_lwtunnel_counters_enabled(slwt))
1131 seg6_local_update_counters(slwt, len, rc);
1136 static int seg6_local_input(struct sk_buff *skb)
1138 if (skb->protocol != htons(ETH_P_IPV6)) {
1143 if (static_branch_unlikely(&nf_hooks_lwtunnel_enabled))
1144 return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_IN,
1145 dev_net(skb->dev), NULL, skb, skb->dev, NULL,
1146 seg6_local_input_core);
1148 return seg6_local_input_core(dev_net(skb->dev), NULL, skb);
1151 static const struct nla_policy seg6_local_policy[SEG6_LOCAL_MAX + 1] = {
1152 [SEG6_LOCAL_ACTION] = { .type = NLA_U32 },
1153 [SEG6_LOCAL_SRH] = { .type = NLA_BINARY },
1154 [SEG6_LOCAL_TABLE] = { .type = NLA_U32 },
1155 [SEG6_LOCAL_VRFTABLE] = { .type = NLA_U32 },
1156 [SEG6_LOCAL_NH4] = { .type = NLA_BINARY,
1157 .len = sizeof(struct in_addr) },
1158 [SEG6_LOCAL_NH6] = { .type = NLA_BINARY,
1159 .len = sizeof(struct in6_addr) },
1160 [SEG6_LOCAL_IIF] = { .type = NLA_U32 },
1161 [SEG6_LOCAL_OIF] = { .type = NLA_U32 },
1162 [SEG6_LOCAL_BPF] = { .type = NLA_NESTED },
1163 [SEG6_LOCAL_COUNTERS] = { .type = NLA_NESTED },
1166 static int parse_nla_srh(struct nlattr **attrs, struct seg6_local_lwt *slwt)
1168 struct ipv6_sr_hdr *srh;
1171 srh = nla_data(attrs[SEG6_LOCAL_SRH]);
1172 len = nla_len(attrs[SEG6_LOCAL_SRH]);
1174 /* SRH must contain at least one segment */
1175 if (len < sizeof(*srh) + sizeof(struct in6_addr))
1178 if (!seg6_validate_srh(srh, len, false))
1181 slwt->srh = kmemdup(srh, len, GFP_KERNEL);
1185 slwt->headroom += len;
1190 static int put_nla_srh(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1192 struct ipv6_sr_hdr *srh;
1197 len = (srh->hdrlen + 1) << 3;
1199 nla = nla_reserve(skb, SEG6_LOCAL_SRH, len);
1203 memcpy(nla_data(nla), srh, len);
1208 static int cmp_nla_srh(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1210 int len = (a->srh->hdrlen + 1) << 3;
1212 if (len != ((b->srh->hdrlen + 1) << 3))
1215 return memcmp(a->srh, b->srh, len);
1218 static void destroy_attr_srh(struct seg6_local_lwt *slwt)
1223 static int parse_nla_table(struct nlattr **attrs, struct seg6_local_lwt *slwt)
1225 slwt->table = nla_get_u32(attrs[SEG6_LOCAL_TABLE]);
1230 static int put_nla_table(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1232 if (nla_put_u32(skb, SEG6_LOCAL_TABLE, slwt->table))
1238 static int cmp_nla_table(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1240 if (a->table != b->table)
1247 seg6_end_dt_info *seg6_possible_end_dt_info(struct seg6_local_lwt *slwt)
1249 #ifdef CONFIG_NET_L3_MASTER_DEV
1250 return &slwt->dt_info;
1252 return ERR_PTR(-EOPNOTSUPP);
1256 static int parse_nla_vrftable(struct nlattr **attrs,
1257 struct seg6_local_lwt *slwt)
1259 struct seg6_end_dt_info *info = seg6_possible_end_dt_info(slwt);
1262 return PTR_ERR(info);
1264 info->vrf_table = nla_get_u32(attrs[SEG6_LOCAL_VRFTABLE]);
1269 static int put_nla_vrftable(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1271 struct seg6_end_dt_info *info = seg6_possible_end_dt_info(slwt);
1274 return PTR_ERR(info);
1276 if (nla_put_u32(skb, SEG6_LOCAL_VRFTABLE, info->vrf_table))
1282 static int cmp_nla_vrftable(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1284 struct seg6_end_dt_info *info_a = seg6_possible_end_dt_info(a);
1285 struct seg6_end_dt_info *info_b = seg6_possible_end_dt_info(b);
1287 if (info_a->vrf_table != info_b->vrf_table)
1293 static int parse_nla_nh4(struct nlattr **attrs, struct seg6_local_lwt *slwt)
1295 memcpy(&slwt->nh4, nla_data(attrs[SEG6_LOCAL_NH4]),
1296 sizeof(struct in_addr));
1301 static int put_nla_nh4(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1305 nla = nla_reserve(skb, SEG6_LOCAL_NH4, sizeof(struct in_addr));
1309 memcpy(nla_data(nla), &slwt->nh4, sizeof(struct in_addr));
1314 static int cmp_nla_nh4(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1316 return memcmp(&a->nh4, &b->nh4, sizeof(struct in_addr));
1319 static int parse_nla_nh6(struct nlattr **attrs, struct seg6_local_lwt *slwt)
1321 memcpy(&slwt->nh6, nla_data(attrs[SEG6_LOCAL_NH6]),
1322 sizeof(struct in6_addr));
1327 static int put_nla_nh6(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1331 nla = nla_reserve(skb, SEG6_LOCAL_NH6, sizeof(struct in6_addr));
1335 memcpy(nla_data(nla), &slwt->nh6, sizeof(struct in6_addr));
1340 static int cmp_nla_nh6(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1342 return memcmp(&a->nh6, &b->nh6, sizeof(struct in6_addr));
1345 static int parse_nla_iif(struct nlattr **attrs, struct seg6_local_lwt *slwt)
1347 slwt->iif = nla_get_u32(attrs[SEG6_LOCAL_IIF]);
1352 static int put_nla_iif(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1354 if (nla_put_u32(skb, SEG6_LOCAL_IIF, slwt->iif))
1360 static int cmp_nla_iif(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1362 if (a->iif != b->iif)
1368 static int parse_nla_oif(struct nlattr **attrs, struct seg6_local_lwt *slwt)
1370 slwt->oif = nla_get_u32(attrs[SEG6_LOCAL_OIF]);
1375 static int put_nla_oif(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1377 if (nla_put_u32(skb, SEG6_LOCAL_OIF, slwt->oif))
1383 static int cmp_nla_oif(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1385 if (a->oif != b->oif)
1391 #define MAX_PROG_NAME 256
1392 static const struct nla_policy bpf_prog_policy[SEG6_LOCAL_BPF_PROG_MAX + 1] = {
1393 [SEG6_LOCAL_BPF_PROG] = { .type = NLA_U32, },
1394 [SEG6_LOCAL_BPF_PROG_NAME] = { .type = NLA_NUL_STRING,
1395 .len = MAX_PROG_NAME },
1398 static int parse_nla_bpf(struct nlattr **attrs, struct seg6_local_lwt *slwt)
1400 struct nlattr *tb[SEG6_LOCAL_BPF_PROG_MAX + 1];
1405 ret = nla_parse_nested_deprecated(tb, SEG6_LOCAL_BPF_PROG_MAX,
1406 attrs[SEG6_LOCAL_BPF],
1407 bpf_prog_policy, NULL);
1411 if (!tb[SEG6_LOCAL_BPF_PROG] || !tb[SEG6_LOCAL_BPF_PROG_NAME])
1414 slwt->bpf.name = nla_memdup(tb[SEG6_LOCAL_BPF_PROG_NAME], GFP_KERNEL);
1415 if (!slwt->bpf.name)
1418 fd = nla_get_u32(tb[SEG6_LOCAL_BPF_PROG]);
1419 p = bpf_prog_get_type(fd, BPF_PROG_TYPE_LWT_SEG6LOCAL);
1421 kfree(slwt->bpf.name);
1429 static int put_nla_bpf(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1431 struct nlattr *nest;
1433 if (!slwt->bpf.prog)
1436 nest = nla_nest_start_noflag(skb, SEG6_LOCAL_BPF);
1440 if (nla_put_u32(skb, SEG6_LOCAL_BPF_PROG, slwt->bpf.prog->aux->id))
1443 if (slwt->bpf.name &&
1444 nla_put_string(skb, SEG6_LOCAL_BPF_PROG_NAME, slwt->bpf.name))
1447 return nla_nest_end(skb, nest);
1450 static int cmp_nla_bpf(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1452 if (!a->bpf.name && !b->bpf.name)
1455 if (!a->bpf.name || !b->bpf.name)
1458 return strcmp(a->bpf.name, b->bpf.name);
1461 static void destroy_attr_bpf(struct seg6_local_lwt *slwt)
1463 kfree(slwt->bpf.name);
1465 bpf_prog_put(slwt->bpf.prog);
1469 nla_policy seg6_local_counters_policy[SEG6_LOCAL_CNT_MAX + 1] = {
1470 [SEG6_LOCAL_CNT_PACKETS] = { .type = NLA_U64 },
1471 [SEG6_LOCAL_CNT_BYTES] = { .type = NLA_U64 },
1472 [SEG6_LOCAL_CNT_ERRORS] = { .type = NLA_U64 },
1475 static int parse_nla_counters(struct nlattr **attrs,
1476 struct seg6_local_lwt *slwt)
1478 struct pcpu_seg6_local_counters __percpu *pcounters;
1479 struct nlattr *tb[SEG6_LOCAL_CNT_MAX + 1];
1482 ret = nla_parse_nested_deprecated(tb, SEG6_LOCAL_CNT_MAX,
1483 attrs[SEG6_LOCAL_COUNTERS],
1484 seg6_local_counters_policy, NULL);
1488 /* basic support for SRv6 Behavior counters requires at least:
1489 * packets, bytes and errors.
1491 if (!tb[SEG6_LOCAL_CNT_PACKETS] || !tb[SEG6_LOCAL_CNT_BYTES] ||
1492 !tb[SEG6_LOCAL_CNT_ERRORS])
1495 /* counters are always zero initialized */
1496 pcounters = seg6_local_alloc_pcpu_counters(GFP_KERNEL);
1500 slwt->pcpu_counters = pcounters;
1505 static int seg6_local_fill_nla_counters(struct sk_buff *skb,
1506 struct seg6_local_counters *counters)
1508 if (nla_put_u64_64bit(skb, SEG6_LOCAL_CNT_PACKETS, counters->packets,
1509 SEG6_LOCAL_CNT_PAD))
1512 if (nla_put_u64_64bit(skb, SEG6_LOCAL_CNT_BYTES, counters->bytes,
1513 SEG6_LOCAL_CNT_PAD))
1516 if (nla_put_u64_64bit(skb, SEG6_LOCAL_CNT_ERRORS, counters->errors,
1517 SEG6_LOCAL_CNT_PAD))
1523 static int put_nla_counters(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1525 struct seg6_local_counters counters = { 0, 0, 0 };
1526 struct nlattr *nest;
1529 nest = nla_nest_start(skb, SEG6_LOCAL_COUNTERS);
1533 for_each_possible_cpu(i) {
1534 struct pcpu_seg6_local_counters *pcounters;
1535 u64 packets, bytes, errors;
1538 pcounters = per_cpu_ptr(slwt->pcpu_counters, i);
1540 start = u64_stats_fetch_begin_irq(&pcounters->syncp);
1542 packets = u64_stats_read(&pcounters->packets);
1543 bytes = u64_stats_read(&pcounters->bytes);
1544 errors = u64_stats_read(&pcounters->errors);
1546 } while (u64_stats_fetch_retry_irq(&pcounters->syncp, start));
1548 counters.packets += packets;
1549 counters.bytes += bytes;
1550 counters.errors += errors;
1553 rc = seg6_local_fill_nla_counters(skb, &counters);
1555 nla_nest_cancel(skb, nest);
1559 return nla_nest_end(skb, nest);
1562 static int cmp_nla_counters(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1564 /* a and b are equal if both have pcpu_counters set or not */
1565 return (!!((unsigned long)a->pcpu_counters)) ^
1566 (!!((unsigned long)b->pcpu_counters));
1569 static void destroy_attr_counters(struct seg6_local_lwt *slwt)
1571 free_percpu(slwt->pcpu_counters);
1574 struct seg6_action_param {
1575 int (*parse)(struct nlattr **attrs, struct seg6_local_lwt *slwt);
1576 int (*put)(struct sk_buff *skb, struct seg6_local_lwt *slwt);
1577 int (*cmp)(struct seg6_local_lwt *a, struct seg6_local_lwt *b);
1579 /* optional destroy() callback useful for releasing resources which
1580 * have been previously acquired in the corresponding parse()
1583 void (*destroy)(struct seg6_local_lwt *slwt);
1586 static struct seg6_action_param seg6_action_params[SEG6_LOCAL_MAX + 1] = {
1587 [SEG6_LOCAL_SRH] = { .parse = parse_nla_srh,
1590 .destroy = destroy_attr_srh },
1592 [SEG6_LOCAL_TABLE] = { .parse = parse_nla_table,
1593 .put = put_nla_table,
1594 .cmp = cmp_nla_table },
1596 [SEG6_LOCAL_NH4] = { .parse = parse_nla_nh4,
1598 .cmp = cmp_nla_nh4 },
1600 [SEG6_LOCAL_NH6] = { .parse = parse_nla_nh6,
1602 .cmp = cmp_nla_nh6 },
1604 [SEG6_LOCAL_IIF] = { .parse = parse_nla_iif,
1606 .cmp = cmp_nla_iif },
1608 [SEG6_LOCAL_OIF] = { .parse = parse_nla_oif,
1610 .cmp = cmp_nla_oif },
1612 [SEG6_LOCAL_BPF] = { .parse = parse_nla_bpf,
1615 .destroy = destroy_attr_bpf },
1617 [SEG6_LOCAL_VRFTABLE] = { .parse = parse_nla_vrftable,
1618 .put = put_nla_vrftable,
1619 .cmp = cmp_nla_vrftable },
1621 [SEG6_LOCAL_COUNTERS] = { .parse = parse_nla_counters,
1622 .put = put_nla_counters,
1623 .cmp = cmp_nla_counters,
1624 .destroy = destroy_attr_counters },
1627 /* call the destroy() callback (if available) for each set attribute in
1628 * @parsed_attrs, starting from the first attribute up to the @max_parsed
1629 * (excluded) attribute.
1631 static void __destroy_attrs(unsigned long parsed_attrs, int max_parsed,
1632 struct seg6_local_lwt *slwt)
1634 struct seg6_action_param *param;
1637 /* Every required seg6local attribute is identified by an ID which is
1638 * encoded as a flag (i.e: 1 << ID) in the 'attrs' bitmask;
1640 * We scan the 'parsed_attrs' bitmask, starting from the first attribute
1641 * up to the @max_parsed (excluded) attribute.
1642 * For each set attribute, we retrieve the corresponding destroy()
1643 * callback. If the callback is not available, then we skip to the next
1644 * attribute; otherwise, we call the destroy() callback.
1646 for (i = 0; i < max_parsed; ++i) {
1647 if (!(parsed_attrs & SEG6_F_ATTR(i)))
1650 param = &seg6_action_params[i];
1653 param->destroy(slwt);
1657 /* release all the resources that may have been acquired during parsing
1660 static void destroy_attrs(struct seg6_local_lwt *slwt)
1662 unsigned long attrs = slwt->desc->attrs | slwt->parsed_optattrs;
1664 __destroy_attrs(attrs, SEG6_LOCAL_MAX + 1, slwt);
1667 static int parse_nla_optional_attrs(struct nlattr **attrs,
1668 struct seg6_local_lwt *slwt)
1670 struct seg6_action_desc *desc = slwt->desc;
1671 unsigned long parsed_optattrs = 0;
1672 struct seg6_action_param *param;
1675 for (i = 0; i < SEG6_LOCAL_MAX + 1; ++i) {
1676 if (!(desc->optattrs & SEG6_F_ATTR(i)) || !attrs[i])
1679 /* once here, the i-th attribute is provided by the
1680 * userspace AND it is identified optional as well.
1682 param = &seg6_action_params[i];
1684 err = param->parse(attrs, slwt);
1686 goto parse_optattrs_err;
1688 /* current attribute has been correctly parsed */
1689 parsed_optattrs |= SEG6_F_ATTR(i);
1692 /* store in the tunnel state all the optional attributed successfully
1695 slwt->parsed_optattrs = parsed_optattrs;
1700 __destroy_attrs(parsed_optattrs, i, slwt);
1705 /* call the custom constructor of the behavior during its initialization phase
1706 * and after that all its attributes have been parsed successfully.
1709 seg6_local_lwtunnel_build_state(struct seg6_local_lwt *slwt, const void *cfg,
1710 struct netlink_ext_ack *extack)
1712 struct seg6_action_desc *desc = slwt->desc;
1713 struct seg6_local_lwtunnel_ops *ops;
1715 ops = &desc->slwt_ops;
1716 if (!ops->build_state)
1719 return ops->build_state(slwt, cfg, extack);
1722 /* call the custom destructor of the behavior which is invoked before the
1723 * tunnel is going to be destroyed.
1725 static void seg6_local_lwtunnel_destroy_state(struct seg6_local_lwt *slwt)
1727 struct seg6_action_desc *desc = slwt->desc;
1728 struct seg6_local_lwtunnel_ops *ops;
1730 ops = &desc->slwt_ops;
1731 if (!ops->destroy_state)
1734 ops->destroy_state(slwt);
1737 static int parse_nla_action(struct nlattr **attrs, struct seg6_local_lwt *slwt)
1739 struct seg6_action_param *param;
1740 struct seg6_action_desc *desc;
1741 unsigned long invalid_attrs;
1744 desc = __get_action_desc(slwt->action);
1752 slwt->headroom += desc->static_headroom;
1754 /* Forcing the desc->optattrs *set* and the desc->attrs *set* to be
1755 * disjoined, this allow us to release acquired resources by optional
1756 * attributes and by required attributes independently from each other
1757 * without any interference.
1758 * In other terms, we are sure that we do not release some the acquired
1761 * Note that if an attribute is configured both as required and as
1762 * optional, it means that the user has messed something up in the
1763 * seg6_action_table. Therefore, this check is required for SRv6
1764 * behaviors to work properly.
1766 invalid_attrs = desc->attrs & desc->optattrs;
1767 if (invalid_attrs) {
1769 "An attribute cannot be both required AND optional");
1773 /* parse the required attributes */
1774 for (i = 0; i < SEG6_LOCAL_MAX + 1; i++) {
1775 if (desc->attrs & SEG6_F_ATTR(i)) {
1779 param = &seg6_action_params[i];
1781 err = param->parse(attrs, slwt);
1783 goto parse_attrs_err;
1787 /* parse the optional attributes, if any */
1788 err = parse_nla_optional_attrs(attrs, slwt);
1790 goto parse_attrs_err;
1795 /* release any resource that may have been acquired during the i-1
1796 * parse() operations.
1798 __destroy_attrs(desc->attrs, i, slwt);
1803 static int seg6_local_build_state(struct net *net, struct nlattr *nla,
1804 unsigned int family, const void *cfg,
1805 struct lwtunnel_state **ts,
1806 struct netlink_ext_ack *extack)
1808 struct nlattr *tb[SEG6_LOCAL_MAX + 1];
1809 struct lwtunnel_state *newts;
1810 struct seg6_local_lwt *slwt;
1813 if (family != AF_INET6)
1816 err = nla_parse_nested_deprecated(tb, SEG6_LOCAL_MAX, nla,
1817 seg6_local_policy, extack);
1822 if (!tb[SEG6_LOCAL_ACTION])
1825 newts = lwtunnel_state_alloc(sizeof(*slwt));
1829 slwt = seg6_local_lwtunnel(newts);
1830 slwt->action = nla_get_u32(tb[SEG6_LOCAL_ACTION]);
1832 err = parse_nla_action(tb, slwt);
1836 err = seg6_local_lwtunnel_build_state(slwt, cfg, extack);
1838 goto out_destroy_attrs;
1840 newts->type = LWTUNNEL_ENCAP_SEG6_LOCAL;
1841 newts->flags = LWTUNNEL_STATE_INPUT_REDIRECT;
1842 newts->headroom = slwt->headroom;
1849 destroy_attrs(slwt);
1855 static void seg6_local_destroy_state(struct lwtunnel_state *lwt)
1857 struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
1859 seg6_local_lwtunnel_destroy_state(slwt);
1861 destroy_attrs(slwt);
1866 static int seg6_local_fill_encap(struct sk_buff *skb,
1867 struct lwtunnel_state *lwt)
1869 struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
1870 struct seg6_action_param *param;
1871 unsigned long attrs;
1874 if (nla_put_u32(skb, SEG6_LOCAL_ACTION, slwt->action))
1877 attrs = slwt->desc->attrs | slwt->parsed_optattrs;
1879 for (i = 0; i < SEG6_LOCAL_MAX + 1; i++) {
1880 if (attrs & SEG6_F_ATTR(i)) {
1881 param = &seg6_action_params[i];
1882 err = param->put(skb, slwt);
1891 static int seg6_local_get_encap_size(struct lwtunnel_state *lwt)
1893 struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
1894 unsigned long attrs;
1897 nlsize = nla_total_size(4); /* action */
1899 attrs = slwt->desc->attrs | slwt->parsed_optattrs;
1901 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_SRH))
1902 nlsize += nla_total_size((slwt->srh->hdrlen + 1) << 3);
1904 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_TABLE))
1905 nlsize += nla_total_size(4);
1907 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_NH4))
1908 nlsize += nla_total_size(4);
1910 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_NH6))
1911 nlsize += nla_total_size(16);
1913 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_IIF))
1914 nlsize += nla_total_size(4);
1916 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_OIF))
1917 nlsize += nla_total_size(4);
1919 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_BPF))
1920 nlsize += nla_total_size(sizeof(struct nlattr)) +
1921 nla_total_size(MAX_PROG_NAME) +
1924 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_VRFTABLE))
1925 nlsize += nla_total_size(4);
1927 if (attrs & SEG6_F_LOCAL_COUNTERS)
1928 nlsize += nla_total_size(0) + /* nest SEG6_LOCAL_COUNTERS */
1929 /* SEG6_LOCAL_CNT_PACKETS */
1930 nla_total_size_64bit(sizeof(__u64)) +
1931 /* SEG6_LOCAL_CNT_BYTES */
1932 nla_total_size_64bit(sizeof(__u64)) +
1933 /* SEG6_LOCAL_CNT_ERRORS */
1934 nla_total_size_64bit(sizeof(__u64));
1939 static int seg6_local_cmp_encap(struct lwtunnel_state *a,
1940 struct lwtunnel_state *b)
1942 struct seg6_local_lwt *slwt_a, *slwt_b;
1943 struct seg6_action_param *param;
1944 unsigned long attrs_a, attrs_b;
1947 slwt_a = seg6_local_lwtunnel(a);
1948 slwt_b = seg6_local_lwtunnel(b);
1950 if (slwt_a->action != slwt_b->action)
1953 attrs_a = slwt_a->desc->attrs | slwt_a->parsed_optattrs;
1954 attrs_b = slwt_b->desc->attrs | slwt_b->parsed_optattrs;
1956 if (attrs_a != attrs_b)
1959 for (i = 0; i < SEG6_LOCAL_MAX + 1; i++) {
1960 if (attrs_a & SEG6_F_ATTR(i)) {
1961 param = &seg6_action_params[i];
1962 if (param->cmp(slwt_a, slwt_b))
1970 static const struct lwtunnel_encap_ops seg6_local_ops = {
1971 .build_state = seg6_local_build_state,
1972 .destroy_state = seg6_local_destroy_state,
1973 .input = seg6_local_input,
1974 .fill_encap = seg6_local_fill_encap,
1975 .get_encap_size = seg6_local_get_encap_size,
1976 .cmp_encap = seg6_local_cmp_encap,
1977 .owner = THIS_MODULE,
1980 int __init seg6_local_init(void)
1982 /* If the max total number of defined attributes is reached, then your
1983 * kernel build stops here.
1985 * This check is required to avoid arithmetic overflows when processing
1986 * behavior attributes and the maximum number of defined attributes
1987 * exceeds the allowed value.
1989 BUILD_BUG_ON(SEG6_LOCAL_MAX + 1 > BITS_PER_TYPE(unsigned long));
1991 return lwtunnel_encap_add_ops(&seg6_local_ops,
1992 LWTUNNEL_ENCAP_SEG6_LOCAL);
1995 void seg6_local_exit(void)
1997 lwtunnel_encap_del_ops(&seg6_local_ops, LWTUNNEL_ENCAP_SEG6_LOCAL);