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>
34 #define SEG6_F_ATTR(i) BIT(i)
36 struct seg6_local_lwt;
38 /* callbacks used for customizing the creation and destruction of a behavior */
39 struct seg6_local_lwtunnel_ops {
40 int (*build_state)(struct seg6_local_lwt *slwt, const void *cfg,
41 struct netlink_ext_ack *extack);
42 void (*destroy_state)(struct seg6_local_lwt *slwt);
45 struct seg6_action_desc {
49 /* The optattrs field is used for specifying all the optional
50 * attributes supported by a specific behavior.
51 * It means that if one of these attributes is not provided in the
52 * netlink message during the behavior creation, no errors will be
53 * returned to the userspace.
55 * Each attribute can be only of two types (mutually exclusive):
56 * 1) required or 2) optional.
57 * Every user MUST obey to this rule! If you set an attribute as
58 * required the same attribute CANNOT be set as optional and vice
61 unsigned long optattrs;
63 int (*input)(struct sk_buff *skb, struct seg6_local_lwt *slwt);
66 struct seg6_local_lwtunnel_ops slwt_ops;
70 struct bpf_prog *prog;
74 enum seg6_end_dt_mode {
75 DT_INVALID_MODE = -EINVAL,
80 struct seg6_end_dt_info {
81 enum seg6_end_dt_mode mode;
84 /* VRF device associated to the routing table used by the SRv6
85 * End.DT4/DT6 behavior for routing IPv4/IPv6 packets.
90 /* tunneled packet family (IPv4 or IPv6).
91 * Protocol and header length are inferred from family.
96 struct pcpu_seg6_local_counters {
101 struct u64_stats_sync syncp;
104 /* This struct groups all the SRv6 Behavior counters supported so far.
106 * put_nla_counters() makes use of this data structure to collect all counter
107 * values after the per-CPU counter evaluation has been performed.
108 * Finally, each counter value (in seg6_local_counters) is stored in the
109 * corresponding netlink attribute and sent to user space.
111 * NB: we don't want to expose this structure to user space!
113 struct seg6_local_counters {
119 #define seg6_local_alloc_pcpu_counters(__gfp) \
120 __netdev_alloc_pcpu_stats(struct pcpu_seg6_local_counters, \
121 ((__gfp) | __GFP_ZERO))
123 #define SEG6_F_LOCAL_COUNTERS SEG6_F_ATTR(SEG6_LOCAL_COUNTERS)
125 struct seg6_local_lwt {
127 struct ipv6_sr_hdr *srh;
133 struct bpf_lwt_prog bpf;
134 #ifdef CONFIG_NET_L3_MASTER_DEV
135 struct seg6_end_dt_info dt_info;
137 struct pcpu_seg6_local_counters __percpu *pcpu_counters;
140 struct seg6_action_desc *desc;
141 /* unlike the required attrs, we have to track the optional attributes
142 * that have been effectively parsed.
144 unsigned long parsed_optattrs;
147 static struct seg6_local_lwt *seg6_local_lwtunnel(struct lwtunnel_state *lwt)
149 return (struct seg6_local_lwt *)lwt->data;
152 static struct ipv6_sr_hdr *get_srh(struct sk_buff *skb, int flags)
154 struct ipv6_sr_hdr *srh;
157 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, &flags) < 0)
160 if (!pskb_may_pull(skb, srhoff + sizeof(*srh)))
163 srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
165 len = (srh->hdrlen + 1) << 3;
167 if (!pskb_may_pull(skb, srhoff + len))
170 /* note that pskb_may_pull may change pointers in header;
171 * for this reason it is necessary to reload them when needed.
173 srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
175 if (!seg6_validate_srh(srh, len, true))
181 static struct ipv6_sr_hdr *get_and_validate_srh(struct sk_buff *skb)
183 struct ipv6_sr_hdr *srh;
185 srh = get_srh(skb, IP6_FH_F_SKIP_RH);
189 #ifdef CONFIG_IPV6_SEG6_HMAC
190 if (!seg6_hmac_validate_skb(skb))
197 static bool decap_and_validate(struct sk_buff *skb, int proto)
199 struct ipv6_sr_hdr *srh;
200 unsigned int off = 0;
202 srh = get_srh(skb, 0);
203 if (srh && srh->segments_left > 0)
206 #ifdef CONFIG_IPV6_SEG6_HMAC
207 if (srh && !seg6_hmac_validate_skb(skb))
211 if (ipv6_find_hdr(skb, &off, proto, NULL, NULL) < 0)
214 if (!pskb_pull(skb, off))
217 skb_postpull_rcsum(skb, skb_network_header(skb), off);
219 skb_reset_network_header(skb);
220 skb_reset_transport_header(skb);
221 if (iptunnel_pull_offloads(skb))
227 static void advance_nextseg(struct ipv6_sr_hdr *srh, struct in6_addr *daddr)
229 struct in6_addr *addr;
231 srh->segments_left--;
232 addr = srh->segments + srh->segments_left;
237 seg6_lookup_any_nexthop(struct sk_buff *skb, struct in6_addr *nhaddr,
238 u32 tbl_id, bool local_delivery)
240 struct net *net = dev_net(skb->dev);
241 struct ipv6hdr *hdr = ipv6_hdr(skb);
242 int flags = RT6_LOOKUP_F_HAS_SADDR;
243 struct dst_entry *dst = NULL;
248 fl6.flowi6_iif = skb->dev->ifindex;
249 fl6.daddr = nhaddr ? *nhaddr : hdr->daddr;
250 fl6.saddr = hdr->saddr;
251 fl6.flowlabel = ip6_flowinfo(hdr);
252 fl6.flowi6_mark = skb->mark;
253 fl6.flowi6_proto = hdr->nexthdr;
256 fl6.flowi6_flags = FLOWI_FLAG_KNOWN_NH;
259 dst = ip6_route_input_lookup(net, skb->dev, &fl6, skb, flags);
261 struct fib6_table *table;
263 table = fib6_get_table(net, tbl_id);
267 rt = ip6_pol_route(net, table, 0, &fl6, skb, flags);
271 /* we want to discard traffic destined for local packet processing,
272 * if @local_delivery is set to false.
275 dev_flags |= IFF_LOOPBACK;
277 if (dst && (dst->dev->flags & dev_flags) && !dst->error) {
284 rt = net->ipv6.ip6_blk_hole_entry;
290 skb_dst_set(skb, dst);
294 int seg6_lookup_nexthop(struct sk_buff *skb,
295 struct in6_addr *nhaddr, u32 tbl_id)
297 return seg6_lookup_any_nexthop(skb, nhaddr, tbl_id, false);
300 /* regular endpoint function */
301 static int input_action_end(struct sk_buff *skb, struct seg6_local_lwt *slwt)
303 struct ipv6_sr_hdr *srh;
305 srh = get_and_validate_srh(skb);
309 advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
311 seg6_lookup_nexthop(skb, NULL, 0);
313 return dst_input(skb);
320 /* regular endpoint, and forward to specified nexthop */
321 static int input_action_end_x(struct sk_buff *skb, struct seg6_local_lwt *slwt)
323 struct ipv6_sr_hdr *srh;
325 srh = get_and_validate_srh(skb);
329 advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
331 seg6_lookup_nexthop(skb, &slwt->nh6, 0);
333 return dst_input(skb);
340 static int input_action_end_t(struct sk_buff *skb, struct seg6_local_lwt *slwt)
342 struct ipv6_sr_hdr *srh;
344 srh = get_and_validate_srh(skb);
348 advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
350 seg6_lookup_nexthop(skb, NULL, slwt->table);
352 return dst_input(skb);
359 /* decapsulate and forward inner L2 frame on specified interface */
360 static int input_action_end_dx2(struct sk_buff *skb,
361 struct seg6_local_lwt *slwt)
363 struct net *net = dev_net(skb->dev);
364 struct net_device *odev;
367 if (!decap_and_validate(skb, IPPROTO_ETHERNET))
370 if (!pskb_may_pull(skb, ETH_HLEN))
373 skb_reset_mac_header(skb);
374 eth = (struct ethhdr *)skb->data;
376 /* To determine the frame's protocol, we assume it is 802.3. This avoids
377 * a call to eth_type_trans(), which is not really relevant for our
380 if (!eth_proto_is_802_3(eth->h_proto))
383 odev = dev_get_by_index_rcu(net, slwt->oif);
387 /* As we accept Ethernet frames, make sure the egress device is of
390 if (odev->type != ARPHRD_ETHER)
393 if (!(odev->flags & IFF_UP) || !netif_carrier_ok(odev))
398 if (skb_warn_if_lro(skb))
401 skb_forward_csum(skb);
403 if (skb->len - ETH_HLEN > odev->mtu)
407 skb->protocol = eth->h_proto;
409 return dev_queue_xmit(skb);
416 /* decapsulate and forward to specified nexthop */
417 static int input_action_end_dx6(struct sk_buff *skb,
418 struct seg6_local_lwt *slwt)
420 struct in6_addr *nhaddr = NULL;
422 /* this function accepts IPv6 encapsulated packets, with either
423 * an SRH with SL=0, or no SRH.
426 if (!decap_and_validate(skb, IPPROTO_IPV6))
429 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
432 /* The inner packet is not associated to any local interface,
433 * so we do not call netif_rx().
435 * If slwt->nh6 is set to ::, then lookup the nexthop for the
436 * inner packet's DA. Otherwise, use the specified nexthop.
439 if (!ipv6_addr_any(&slwt->nh6))
442 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
444 seg6_lookup_nexthop(skb, nhaddr, 0);
446 return dst_input(skb);
452 static int input_action_end_dx4(struct sk_buff *skb,
453 struct seg6_local_lwt *slwt)
459 if (!decap_and_validate(skb, IPPROTO_IPIP))
462 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
465 skb->protocol = htons(ETH_P_IP);
469 nhaddr = slwt->nh4.s_addr ?: iph->daddr;
473 skb_set_transport_header(skb, sizeof(struct iphdr));
475 err = ip_route_input(skb, nhaddr, iph->saddr, 0, skb->dev);
479 return dst_input(skb);
486 #ifdef CONFIG_NET_L3_MASTER_DEV
487 static struct net *fib6_config_get_net(const struct fib6_config *fib6_cfg)
489 const struct nl_info *nli = &fib6_cfg->fc_nlinfo;
494 static int __seg6_end_dt_vrf_build(struct seg6_local_lwt *slwt, const void *cfg,
495 u16 family, struct netlink_ext_ack *extack)
497 struct seg6_end_dt_info *info = &slwt->dt_info;
501 net = fib6_config_get_net(cfg);
503 /* note that vrf_table was already set by parse_nla_vrftable() */
504 vrf_ifindex = l3mdev_ifindex_lookup_by_table_id(L3MDEV_TYPE_VRF, net,
506 if (vrf_ifindex < 0) {
507 if (vrf_ifindex == -EPERM) {
508 NL_SET_ERR_MSG(extack,
509 "Strict mode for VRF is disabled");
510 } else if (vrf_ifindex == -ENODEV) {
511 NL_SET_ERR_MSG(extack,
512 "Table has no associated VRF device");
514 pr_debug("seg6local: SRv6 End.DT* creation error=%d\n",
522 info->vrf_ifindex = vrf_ifindex;
524 info->family = family;
525 info->mode = DT_VRF_MODE;
530 /* The SRv6 End.DT4/DT6 behavior extracts the inner (IPv4/IPv6) packet and
531 * routes the IPv4/IPv6 packet by looking at the configured routing table.
533 * In the SRv6 End.DT4/DT6 use case, we can receive traffic (IPv6+Segment
534 * Routing Header packets) from several interfaces and the outer IPv6
535 * destination address (DA) is used for retrieving the specific instance of the
536 * End.DT4/DT6 behavior that should process the packets.
538 * However, the inner IPv4/IPv6 packet is not really bound to any receiving
539 * interface and thus the End.DT4/DT6 sets the VRF (associated with the
540 * corresponding routing table) as the *receiving* interface.
541 * In other words, the End.DT4/DT6 processes a packet as if it has been received
542 * directly by the VRF (and not by one of its slave devices, if any).
543 * In this way, the VRF interface is used for routing the IPv4/IPv6 packet in
544 * according to the routing table configured by the End.DT4/DT6 instance.
546 * This design allows you to get some interesting features like:
547 * 1) the statistics on rx packets;
548 * 2) the possibility to install a packet sniffer on the receiving interface
549 * (the VRF one) for looking at the incoming packets;
550 * 3) the possibility to leverage the netfilter prerouting hook for the inner
553 * This function returns:
554 * - the sk_buff* when the VRF rcv handler has processed the packet correctly;
555 * - NULL when the skb is consumed by the VRF rcv handler;
556 * - a pointer which encodes a negative error number in case of error.
557 * Note that in this case, the function takes care of freeing the skb.
559 static struct sk_buff *end_dt_vrf_rcv(struct sk_buff *skb, u16 family,
560 struct net_device *dev)
562 /* based on l3mdev_ip_rcv; we are only interested in the master */
563 if (unlikely(!netif_is_l3_master(dev) && !netif_has_l3_rx_handler(dev)))
566 if (unlikely(!dev->l3mdev_ops->l3mdev_l3_rcv))
569 /* the decap packet IPv4/IPv6 does not come with any mac header info.
570 * We must unset the mac header to allow the VRF device to rebuild it,
571 * just in case there is a sniffer attached on the device.
573 skb_unset_mac_header(skb);
575 skb = dev->l3mdev_ops->l3mdev_l3_rcv(dev, skb, family);
577 /* the skb buffer was consumed by the handler */
580 /* when a packet is received by a VRF or by one of its slaves, the
581 * master device reference is set into the skb.
583 if (unlikely(skb->dev != dev || skb->skb_iif != dev->ifindex))
590 return ERR_PTR(-EINVAL);
593 static struct net_device *end_dt_get_vrf_rcu(struct sk_buff *skb,
594 struct seg6_end_dt_info *info)
596 int vrf_ifindex = info->vrf_ifindex;
597 struct net *net = info->net;
599 if (unlikely(vrf_ifindex < 0))
602 if (unlikely(!net_eq(dev_net(skb->dev), net)))
605 return dev_get_by_index_rcu(net, vrf_ifindex);
611 static struct sk_buff *end_dt_vrf_core(struct sk_buff *skb,
612 struct seg6_local_lwt *slwt, u16 family)
614 struct seg6_end_dt_info *info = &slwt->dt_info;
615 struct net_device *vrf;
619 vrf = end_dt_get_vrf_rcu(skb, info);
625 protocol = htons(ETH_P_IP);
626 hdrlen = sizeof(struct iphdr);
629 protocol = htons(ETH_P_IPV6);
630 hdrlen = sizeof(struct ipv6hdr);
638 if (unlikely(info->family != AF_UNSPEC && info->family != family)) {
639 pr_warn_once("seg6local: SRv6 End.DT* family mismatch");
643 skb->protocol = protocol;
647 skb_set_transport_header(skb, hdrlen);
649 return end_dt_vrf_rcv(skb, family, vrf);
653 return ERR_PTR(-EINVAL);
656 static int input_action_end_dt4(struct sk_buff *skb,
657 struct seg6_local_lwt *slwt)
662 if (!decap_and_validate(skb, IPPROTO_IPIP))
665 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
668 skb = end_dt_vrf_core(skb, slwt, AF_INET);
670 /* packet has been processed and consumed by the VRF */
678 err = ip_route_input(skb, iph->daddr, iph->saddr, 0, skb->dev);
682 return dst_input(skb);
689 static int seg6_end_dt4_build(struct seg6_local_lwt *slwt, const void *cfg,
690 struct netlink_ext_ack *extack)
692 return __seg6_end_dt_vrf_build(slwt, cfg, AF_INET, extack);
696 seg6_end_dt_mode seg6_end_dt6_parse_mode(struct seg6_local_lwt *slwt)
698 unsigned long parsed_optattrs = slwt->parsed_optattrs;
699 bool legacy, vrfmode;
701 legacy = !!(parsed_optattrs & SEG6_F_ATTR(SEG6_LOCAL_TABLE));
702 vrfmode = !!(parsed_optattrs & SEG6_F_ATTR(SEG6_LOCAL_VRFTABLE));
704 if (!(legacy ^ vrfmode))
705 /* both are absent or present: invalid DT6 mode */
706 return DT_INVALID_MODE;
708 return legacy ? DT_LEGACY_MODE : DT_VRF_MODE;
711 static enum seg6_end_dt_mode seg6_end_dt6_get_mode(struct seg6_local_lwt *slwt)
713 struct seg6_end_dt_info *info = &slwt->dt_info;
718 static int seg6_end_dt6_build(struct seg6_local_lwt *slwt, const void *cfg,
719 struct netlink_ext_ack *extack)
721 enum seg6_end_dt_mode mode = seg6_end_dt6_parse_mode(slwt);
722 struct seg6_end_dt_info *info = &slwt->dt_info;
726 info->mode = DT_LEGACY_MODE;
729 return __seg6_end_dt_vrf_build(slwt, cfg, AF_INET6, extack);
731 NL_SET_ERR_MSG(extack, "table or vrftable must be specified");
737 static int input_action_end_dt6(struct sk_buff *skb,
738 struct seg6_local_lwt *slwt)
740 if (!decap_and_validate(skb, IPPROTO_IPV6))
743 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
746 #ifdef CONFIG_NET_L3_MASTER_DEV
747 if (seg6_end_dt6_get_mode(slwt) == DT_LEGACY_MODE)
751 skb = end_dt_vrf_core(skb, slwt, AF_INET6);
753 /* packet has been processed and consumed by the VRF */
759 /* note: this time we do not need to specify the table because the VRF
760 * takes care of selecting the correct table.
762 seg6_lookup_any_nexthop(skb, NULL, 0, true);
764 return dst_input(skb);
768 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
770 seg6_lookup_any_nexthop(skb, NULL, slwt->table, true);
772 return dst_input(skb);
779 #ifdef CONFIG_NET_L3_MASTER_DEV
780 static int seg6_end_dt46_build(struct seg6_local_lwt *slwt, const void *cfg,
781 struct netlink_ext_ack *extack)
783 return __seg6_end_dt_vrf_build(slwt, cfg, AF_UNSPEC, extack);
786 static int input_action_end_dt46(struct sk_buff *skb,
787 struct seg6_local_lwt *slwt)
789 unsigned int off = 0;
792 nexthdr = ipv6_find_hdr(skb, &off, -1, NULL, NULL);
793 if (unlikely(nexthdr < 0))
798 return input_action_end_dt4(skb, slwt);
800 return input_action_end_dt6(skb, slwt);
809 /* push an SRH on top of the current one */
810 static int input_action_end_b6(struct sk_buff *skb, struct seg6_local_lwt *slwt)
812 struct ipv6_sr_hdr *srh;
815 srh = get_and_validate_srh(skb);
819 err = seg6_do_srh_inline(skb, slwt->srh);
823 ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
824 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
826 seg6_lookup_nexthop(skb, NULL, 0);
828 return dst_input(skb);
835 /* encapsulate within an outer IPv6 header and a specified SRH */
836 static int input_action_end_b6_encap(struct sk_buff *skb,
837 struct seg6_local_lwt *slwt)
839 struct ipv6_sr_hdr *srh;
842 srh = get_and_validate_srh(skb);
846 advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
848 skb_reset_inner_headers(skb);
849 skb->encapsulation = 1;
851 err = seg6_do_srh_encap(skb, slwt->srh, IPPROTO_IPV6);
855 ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
856 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
858 seg6_lookup_nexthop(skb, NULL, 0);
860 return dst_input(skb);
867 DEFINE_PER_CPU(struct seg6_bpf_srh_state, seg6_bpf_srh_states);
869 bool seg6_bpf_has_valid_srh(struct sk_buff *skb)
871 struct seg6_bpf_srh_state *srh_state =
872 this_cpu_ptr(&seg6_bpf_srh_states);
873 struct ipv6_sr_hdr *srh = srh_state->srh;
875 if (unlikely(srh == NULL))
878 if (unlikely(!srh_state->valid)) {
879 if ((srh_state->hdrlen & 7) != 0)
882 srh->hdrlen = (u8)(srh_state->hdrlen >> 3);
883 if (!seg6_validate_srh(srh, (srh->hdrlen + 1) << 3, true))
886 srh_state->valid = true;
892 static int input_action_end_bpf(struct sk_buff *skb,
893 struct seg6_local_lwt *slwt)
895 struct seg6_bpf_srh_state *srh_state =
896 this_cpu_ptr(&seg6_bpf_srh_states);
897 struct ipv6_sr_hdr *srh;
900 srh = get_and_validate_srh(skb);
905 advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
907 /* preempt_disable is needed to protect the per-CPU buffer srh_state,
908 * which is also accessed by the bpf_lwt_seg6_* helpers
911 srh_state->srh = srh;
912 srh_state->hdrlen = srh->hdrlen << 3;
913 srh_state->valid = true;
916 bpf_compute_data_pointers(skb);
917 ret = bpf_prog_run_save_cb(slwt->bpf.prog, skb);
927 pr_warn_once("bpf-seg6local: Illegal return value %u\n", ret);
931 if (srh_state->srh && !seg6_bpf_has_valid_srh(skb))
935 if (ret != BPF_REDIRECT)
936 seg6_lookup_nexthop(skb, NULL, 0);
938 return dst_input(skb);
946 static struct seg6_action_desc seg6_action_table[] = {
948 .action = SEG6_LOCAL_ACTION_END,
950 .optattrs = SEG6_F_LOCAL_COUNTERS,
951 .input = input_action_end,
954 .action = SEG6_LOCAL_ACTION_END_X,
955 .attrs = SEG6_F_ATTR(SEG6_LOCAL_NH6),
956 .optattrs = SEG6_F_LOCAL_COUNTERS,
957 .input = input_action_end_x,
960 .action = SEG6_LOCAL_ACTION_END_T,
961 .attrs = SEG6_F_ATTR(SEG6_LOCAL_TABLE),
962 .optattrs = SEG6_F_LOCAL_COUNTERS,
963 .input = input_action_end_t,
966 .action = SEG6_LOCAL_ACTION_END_DX2,
967 .attrs = SEG6_F_ATTR(SEG6_LOCAL_OIF),
968 .optattrs = SEG6_F_LOCAL_COUNTERS,
969 .input = input_action_end_dx2,
972 .action = SEG6_LOCAL_ACTION_END_DX6,
973 .attrs = SEG6_F_ATTR(SEG6_LOCAL_NH6),
974 .optattrs = SEG6_F_LOCAL_COUNTERS,
975 .input = input_action_end_dx6,
978 .action = SEG6_LOCAL_ACTION_END_DX4,
979 .attrs = SEG6_F_ATTR(SEG6_LOCAL_NH4),
980 .optattrs = SEG6_F_LOCAL_COUNTERS,
981 .input = input_action_end_dx4,
984 .action = SEG6_LOCAL_ACTION_END_DT4,
985 .attrs = SEG6_F_ATTR(SEG6_LOCAL_VRFTABLE),
986 .optattrs = SEG6_F_LOCAL_COUNTERS,
987 #ifdef CONFIG_NET_L3_MASTER_DEV
988 .input = input_action_end_dt4,
990 .build_state = seg6_end_dt4_build,
995 .action = SEG6_LOCAL_ACTION_END_DT6,
996 #ifdef CONFIG_NET_L3_MASTER_DEV
998 .optattrs = SEG6_F_LOCAL_COUNTERS |
999 SEG6_F_ATTR(SEG6_LOCAL_TABLE) |
1000 SEG6_F_ATTR(SEG6_LOCAL_VRFTABLE),
1002 .build_state = seg6_end_dt6_build,
1005 .attrs = SEG6_F_ATTR(SEG6_LOCAL_TABLE),
1006 .optattrs = SEG6_F_LOCAL_COUNTERS,
1008 .input = input_action_end_dt6,
1011 .action = SEG6_LOCAL_ACTION_END_DT46,
1012 .attrs = SEG6_F_ATTR(SEG6_LOCAL_VRFTABLE),
1013 .optattrs = SEG6_F_LOCAL_COUNTERS,
1014 #ifdef CONFIG_NET_L3_MASTER_DEV
1015 .input = input_action_end_dt46,
1017 .build_state = seg6_end_dt46_build,
1022 .action = SEG6_LOCAL_ACTION_END_B6,
1023 .attrs = SEG6_F_ATTR(SEG6_LOCAL_SRH),
1024 .optattrs = SEG6_F_LOCAL_COUNTERS,
1025 .input = input_action_end_b6,
1028 .action = SEG6_LOCAL_ACTION_END_B6_ENCAP,
1029 .attrs = SEG6_F_ATTR(SEG6_LOCAL_SRH),
1030 .optattrs = SEG6_F_LOCAL_COUNTERS,
1031 .input = input_action_end_b6_encap,
1032 .static_headroom = sizeof(struct ipv6hdr),
1035 .action = SEG6_LOCAL_ACTION_END_BPF,
1036 .attrs = SEG6_F_ATTR(SEG6_LOCAL_BPF),
1037 .optattrs = SEG6_F_LOCAL_COUNTERS,
1038 .input = input_action_end_bpf,
1043 static struct seg6_action_desc *__get_action_desc(int action)
1045 struct seg6_action_desc *desc;
1048 count = ARRAY_SIZE(seg6_action_table);
1049 for (i = 0; i < count; i++) {
1050 desc = &seg6_action_table[i];
1051 if (desc->action == action)
1058 static bool seg6_lwtunnel_counters_enabled(struct seg6_local_lwt *slwt)
1060 return slwt->parsed_optattrs & SEG6_F_LOCAL_COUNTERS;
1063 static void seg6_local_update_counters(struct seg6_local_lwt *slwt,
1064 unsigned int len, int err)
1066 struct pcpu_seg6_local_counters *pcounters;
1068 pcounters = this_cpu_ptr(slwt->pcpu_counters);
1069 u64_stats_update_begin(&pcounters->syncp);
1072 u64_stats_inc(&pcounters->packets);
1073 u64_stats_add(&pcounters->bytes, len);
1075 u64_stats_inc(&pcounters->errors);
1078 u64_stats_update_end(&pcounters->syncp);
1081 static int seg6_local_input(struct sk_buff *skb)
1083 struct dst_entry *orig_dst = skb_dst(skb);
1084 struct seg6_action_desc *desc;
1085 struct seg6_local_lwt *slwt;
1086 unsigned int len = skb->len;
1089 if (skb->protocol != htons(ETH_P_IPV6)) {
1094 slwt = seg6_local_lwtunnel(orig_dst->lwtstate);
1097 rc = desc->input(skb, slwt);
1099 if (!seg6_lwtunnel_counters_enabled(slwt))
1102 seg6_local_update_counters(slwt, len, rc);
1107 static const struct nla_policy seg6_local_policy[SEG6_LOCAL_MAX + 1] = {
1108 [SEG6_LOCAL_ACTION] = { .type = NLA_U32 },
1109 [SEG6_LOCAL_SRH] = { .type = NLA_BINARY },
1110 [SEG6_LOCAL_TABLE] = { .type = NLA_U32 },
1111 [SEG6_LOCAL_VRFTABLE] = { .type = NLA_U32 },
1112 [SEG6_LOCAL_NH4] = { .type = NLA_BINARY,
1113 .len = sizeof(struct in_addr) },
1114 [SEG6_LOCAL_NH6] = { .type = NLA_BINARY,
1115 .len = sizeof(struct in6_addr) },
1116 [SEG6_LOCAL_IIF] = { .type = NLA_U32 },
1117 [SEG6_LOCAL_OIF] = { .type = NLA_U32 },
1118 [SEG6_LOCAL_BPF] = { .type = NLA_NESTED },
1119 [SEG6_LOCAL_COUNTERS] = { .type = NLA_NESTED },
1122 static int parse_nla_srh(struct nlattr **attrs, struct seg6_local_lwt *slwt)
1124 struct ipv6_sr_hdr *srh;
1127 srh = nla_data(attrs[SEG6_LOCAL_SRH]);
1128 len = nla_len(attrs[SEG6_LOCAL_SRH]);
1130 /* SRH must contain at least one segment */
1131 if (len < sizeof(*srh) + sizeof(struct in6_addr))
1134 if (!seg6_validate_srh(srh, len, false))
1137 slwt->srh = kmemdup(srh, len, GFP_KERNEL);
1141 slwt->headroom += len;
1146 static int put_nla_srh(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1148 struct ipv6_sr_hdr *srh;
1153 len = (srh->hdrlen + 1) << 3;
1155 nla = nla_reserve(skb, SEG6_LOCAL_SRH, len);
1159 memcpy(nla_data(nla), srh, len);
1164 static int cmp_nla_srh(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1166 int len = (a->srh->hdrlen + 1) << 3;
1168 if (len != ((b->srh->hdrlen + 1) << 3))
1171 return memcmp(a->srh, b->srh, len);
1174 static void destroy_attr_srh(struct seg6_local_lwt *slwt)
1179 static int parse_nla_table(struct nlattr **attrs, struct seg6_local_lwt *slwt)
1181 slwt->table = nla_get_u32(attrs[SEG6_LOCAL_TABLE]);
1186 static int put_nla_table(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1188 if (nla_put_u32(skb, SEG6_LOCAL_TABLE, slwt->table))
1194 static int cmp_nla_table(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1196 if (a->table != b->table)
1203 seg6_end_dt_info *seg6_possible_end_dt_info(struct seg6_local_lwt *slwt)
1205 #ifdef CONFIG_NET_L3_MASTER_DEV
1206 return &slwt->dt_info;
1208 return ERR_PTR(-EOPNOTSUPP);
1212 static int parse_nla_vrftable(struct nlattr **attrs,
1213 struct seg6_local_lwt *slwt)
1215 struct seg6_end_dt_info *info = seg6_possible_end_dt_info(slwt);
1218 return PTR_ERR(info);
1220 info->vrf_table = nla_get_u32(attrs[SEG6_LOCAL_VRFTABLE]);
1225 static int put_nla_vrftable(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1227 struct seg6_end_dt_info *info = seg6_possible_end_dt_info(slwt);
1230 return PTR_ERR(info);
1232 if (nla_put_u32(skb, SEG6_LOCAL_VRFTABLE, info->vrf_table))
1238 static int cmp_nla_vrftable(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1240 struct seg6_end_dt_info *info_a = seg6_possible_end_dt_info(a);
1241 struct seg6_end_dt_info *info_b = seg6_possible_end_dt_info(b);
1243 if (info_a->vrf_table != info_b->vrf_table)
1249 static int parse_nla_nh4(struct nlattr **attrs, struct seg6_local_lwt *slwt)
1251 memcpy(&slwt->nh4, nla_data(attrs[SEG6_LOCAL_NH4]),
1252 sizeof(struct in_addr));
1257 static int put_nla_nh4(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1261 nla = nla_reserve(skb, SEG6_LOCAL_NH4, sizeof(struct in_addr));
1265 memcpy(nla_data(nla), &slwt->nh4, sizeof(struct in_addr));
1270 static int cmp_nla_nh4(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1272 return memcmp(&a->nh4, &b->nh4, sizeof(struct in_addr));
1275 static int parse_nla_nh6(struct nlattr **attrs, struct seg6_local_lwt *slwt)
1277 memcpy(&slwt->nh6, nla_data(attrs[SEG6_LOCAL_NH6]),
1278 sizeof(struct in6_addr));
1283 static int put_nla_nh6(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1287 nla = nla_reserve(skb, SEG6_LOCAL_NH6, sizeof(struct in6_addr));
1291 memcpy(nla_data(nla), &slwt->nh6, sizeof(struct in6_addr));
1296 static int cmp_nla_nh6(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1298 return memcmp(&a->nh6, &b->nh6, sizeof(struct in6_addr));
1301 static int parse_nla_iif(struct nlattr **attrs, struct seg6_local_lwt *slwt)
1303 slwt->iif = nla_get_u32(attrs[SEG6_LOCAL_IIF]);
1308 static int put_nla_iif(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1310 if (nla_put_u32(skb, SEG6_LOCAL_IIF, slwt->iif))
1316 static int cmp_nla_iif(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1318 if (a->iif != b->iif)
1324 static int parse_nla_oif(struct nlattr **attrs, struct seg6_local_lwt *slwt)
1326 slwt->oif = nla_get_u32(attrs[SEG6_LOCAL_OIF]);
1331 static int put_nla_oif(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1333 if (nla_put_u32(skb, SEG6_LOCAL_OIF, slwt->oif))
1339 static int cmp_nla_oif(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1341 if (a->oif != b->oif)
1347 #define MAX_PROG_NAME 256
1348 static const struct nla_policy bpf_prog_policy[SEG6_LOCAL_BPF_PROG_MAX + 1] = {
1349 [SEG6_LOCAL_BPF_PROG] = { .type = NLA_U32, },
1350 [SEG6_LOCAL_BPF_PROG_NAME] = { .type = NLA_NUL_STRING,
1351 .len = MAX_PROG_NAME },
1354 static int parse_nla_bpf(struct nlattr **attrs, struct seg6_local_lwt *slwt)
1356 struct nlattr *tb[SEG6_LOCAL_BPF_PROG_MAX + 1];
1361 ret = nla_parse_nested_deprecated(tb, SEG6_LOCAL_BPF_PROG_MAX,
1362 attrs[SEG6_LOCAL_BPF],
1363 bpf_prog_policy, NULL);
1367 if (!tb[SEG6_LOCAL_BPF_PROG] || !tb[SEG6_LOCAL_BPF_PROG_NAME])
1370 slwt->bpf.name = nla_memdup(tb[SEG6_LOCAL_BPF_PROG_NAME], GFP_KERNEL);
1371 if (!slwt->bpf.name)
1374 fd = nla_get_u32(tb[SEG6_LOCAL_BPF_PROG]);
1375 p = bpf_prog_get_type(fd, BPF_PROG_TYPE_LWT_SEG6LOCAL);
1377 kfree(slwt->bpf.name);
1385 static int put_nla_bpf(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1387 struct nlattr *nest;
1389 if (!slwt->bpf.prog)
1392 nest = nla_nest_start_noflag(skb, SEG6_LOCAL_BPF);
1396 if (nla_put_u32(skb, SEG6_LOCAL_BPF_PROG, slwt->bpf.prog->aux->id))
1399 if (slwt->bpf.name &&
1400 nla_put_string(skb, SEG6_LOCAL_BPF_PROG_NAME, slwt->bpf.name))
1403 return nla_nest_end(skb, nest);
1406 static int cmp_nla_bpf(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1408 if (!a->bpf.name && !b->bpf.name)
1411 if (!a->bpf.name || !b->bpf.name)
1414 return strcmp(a->bpf.name, b->bpf.name);
1417 static void destroy_attr_bpf(struct seg6_local_lwt *slwt)
1419 kfree(slwt->bpf.name);
1421 bpf_prog_put(slwt->bpf.prog);
1425 nla_policy seg6_local_counters_policy[SEG6_LOCAL_CNT_MAX + 1] = {
1426 [SEG6_LOCAL_CNT_PACKETS] = { .type = NLA_U64 },
1427 [SEG6_LOCAL_CNT_BYTES] = { .type = NLA_U64 },
1428 [SEG6_LOCAL_CNT_ERRORS] = { .type = NLA_U64 },
1431 static int parse_nla_counters(struct nlattr **attrs,
1432 struct seg6_local_lwt *slwt)
1434 struct pcpu_seg6_local_counters __percpu *pcounters;
1435 struct nlattr *tb[SEG6_LOCAL_CNT_MAX + 1];
1438 ret = nla_parse_nested_deprecated(tb, SEG6_LOCAL_CNT_MAX,
1439 attrs[SEG6_LOCAL_COUNTERS],
1440 seg6_local_counters_policy, NULL);
1444 /* basic support for SRv6 Behavior counters requires at least:
1445 * packets, bytes and errors.
1447 if (!tb[SEG6_LOCAL_CNT_PACKETS] || !tb[SEG6_LOCAL_CNT_BYTES] ||
1448 !tb[SEG6_LOCAL_CNT_ERRORS])
1451 /* counters are always zero initialized */
1452 pcounters = seg6_local_alloc_pcpu_counters(GFP_KERNEL);
1456 slwt->pcpu_counters = pcounters;
1461 static int seg6_local_fill_nla_counters(struct sk_buff *skb,
1462 struct seg6_local_counters *counters)
1464 if (nla_put_u64_64bit(skb, SEG6_LOCAL_CNT_PACKETS, counters->packets,
1465 SEG6_LOCAL_CNT_PAD))
1468 if (nla_put_u64_64bit(skb, SEG6_LOCAL_CNT_BYTES, counters->bytes,
1469 SEG6_LOCAL_CNT_PAD))
1472 if (nla_put_u64_64bit(skb, SEG6_LOCAL_CNT_ERRORS, counters->errors,
1473 SEG6_LOCAL_CNT_PAD))
1479 static int put_nla_counters(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1481 struct seg6_local_counters counters = { 0, 0, 0 };
1482 struct nlattr *nest;
1485 nest = nla_nest_start(skb, SEG6_LOCAL_COUNTERS);
1489 for_each_possible_cpu(i) {
1490 struct pcpu_seg6_local_counters *pcounters;
1491 u64 packets, bytes, errors;
1494 pcounters = per_cpu_ptr(slwt->pcpu_counters, i);
1496 start = u64_stats_fetch_begin_irq(&pcounters->syncp);
1498 packets = u64_stats_read(&pcounters->packets);
1499 bytes = u64_stats_read(&pcounters->bytes);
1500 errors = u64_stats_read(&pcounters->errors);
1502 } while (u64_stats_fetch_retry_irq(&pcounters->syncp, start));
1504 counters.packets += packets;
1505 counters.bytes += bytes;
1506 counters.errors += errors;
1509 rc = seg6_local_fill_nla_counters(skb, &counters);
1511 nla_nest_cancel(skb, nest);
1515 return nla_nest_end(skb, nest);
1518 static int cmp_nla_counters(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1520 /* a and b are equal if both have pcpu_counters set or not */
1521 return (!!((unsigned long)a->pcpu_counters)) ^
1522 (!!((unsigned long)b->pcpu_counters));
1525 static void destroy_attr_counters(struct seg6_local_lwt *slwt)
1527 free_percpu(slwt->pcpu_counters);
1530 struct seg6_action_param {
1531 int (*parse)(struct nlattr **attrs, struct seg6_local_lwt *slwt);
1532 int (*put)(struct sk_buff *skb, struct seg6_local_lwt *slwt);
1533 int (*cmp)(struct seg6_local_lwt *a, struct seg6_local_lwt *b);
1535 /* optional destroy() callback useful for releasing resources which
1536 * have been previously acquired in the corresponding parse()
1539 void (*destroy)(struct seg6_local_lwt *slwt);
1542 static struct seg6_action_param seg6_action_params[SEG6_LOCAL_MAX + 1] = {
1543 [SEG6_LOCAL_SRH] = { .parse = parse_nla_srh,
1546 .destroy = destroy_attr_srh },
1548 [SEG6_LOCAL_TABLE] = { .parse = parse_nla_table,
1549 .put = put_nla_table,
1550 .cmp = cmp_nla_table },
1552 [SEG6_LOCAL_NH4] = { .parse = parse_nla_nh4,
1554 .cmp = cmp_nla_nh4 },
1556 [SEG6_LOCAL_NH6] = { .parse = parse_nla_nh6,
1558 .cmp = cmp_nla_nh6 },
1560 [SEG6_LOCAL_IIF] = { .parse = parse_nla_iif,
1562 .cmp = cmp_nla_iif },
1564 [SEG6_LOCAL_OIF] = { .parse = parse_nla_oif,
1566 .cmp = cmp_nla_oif },
1568 [SEG6_LOCAL_BPF] = { .parse = parse_nla_bpf,
1571 .destroy = destroy_attr_bpf },
1573 [SEG6_LOCAL_VRFTABLE] = { .parse = parse_nla_vrftable,
1574 .put = put_nla_vrftable,
1575 .cmp = cmp_nla_vrftable },
1577 [SEG6_LOCAL_COUNTERS] = { .parse = parse_nla_counters,
1578 .put = put_nla_counters,
1579 .cmp = cmp_nla_counters,
1580 .destroy = destroy_attr_counters },
1583 /* call the destroy() callback (if available) for each set attribute in
1584 * @parsed_attrs, starting from the first attribute up to the @max_parsed
1585 * (excluded) attribute.
1587 static void __destroy_attrs(unsigned long parsed_attrs, int max_parsed,
1588 struct seg6_local_lwt *slwt)
1590 struct seg6_action_param *param;
1593 /* Every required seg6local attribute is identified by an ID which is
1594 * encoded as a flag (i.e: 1 << ID) in the 'attrs' bitmask;
1596 * We scan the 'parsed_attrs' bitmask, starting from the first attribute
1597 * up to the @max_parsed (excluded) attribute.
1598 * For each set attribute, we retrieve the corresponding destroy()
1599 * callback. If the callback is not available, then we skip to the next
1600 * attribute; otherwise, we call the destroy() callback.
1602 for (i = 0; i < max_parsed; ++i) {
1603 if (!(parsed_attrs & SEG6_F_ATTR(i)))
1606 param = &seg6_action_params[i];
1609 param->destroy(slwt);
1613 /* release all the resources that may have been acquired during parsing
1616 static void destroy_attrs(struct seg6_local_lwt *slwt)
1618 unsigned long attrs = slwt->desc->attrs | slwt->parsed_optattrs;
1620 __destroy_attrs(attrs, SEG6_LOCAL_MAX + 1, slwt);
1623 static int parse_nla_optional_attrs(struct nlattr **attrs,
1624 struct seg6_local_lwt *slwt)
1626 struct seg6_action_desc *desc = slwt->desc;
1627 unsigned long parsed_optattrs = 0;
1628 struct seg6_action_param *param;
1631 for (i = 0; i < SEG6_LOCAL_MAX + 1; ++i) {
1632 if (!(desc->optattrs & SEG6_F_ATTR(i)) || !attrs[i])
1635 /* once here, the i-th attribute is provided by the
1636 * userspace AND it is identified optional as well.
1638 param = &seg6_action_params[i];
1640 err = param->parse(attrs, slwt);
1642 goto parse_optattrs_err;
1644 /* current attribute has been correctly parsed */
1645 parsed_optattrs |= SEG6_F_ATTR(i);
1648 /* store in the tunnel state all the optional attributed successfully
1651 slwt->parsed_optattrs = parsed_optattrs;
1656 __destroy_attrs(parsed_optattrs, i, slwt);
1661 /* call the custom constructor of the behavior during its initialization phase
1662 * and after that all its attributes have been parsed successfully.
1665 seg6_local_lwtunnel_build_state(struct seg6_local_lwt *slwt, const void *cfg,
1666 struct netlink_ext_ack *extack)
1668 struct seg6_action_desc *desc = slwt->desc;
1669 struct seg6_local_lwtunnel_ops *ops;
1671 ops = &desc->slwt_ops;
1672 if (!ops->build_state)
1675 return ops->build_state(slwt, cfg, extack);
1678 /* call the custom destructor of the behavior which is invoked before the
1679 * tunnel is going to be destroyed.
1681 static void seg6_local_lwtunnel_destroy_state(struct seg6_local_lwt *slwt)
1683 struct seg6_action_desc *desc = slwt->desc;
1684 struct seg6_local_lwtunnel_ops *ops;
1686 ops = &desc->slwt_ops;
1687 if (!ops->destroy_state)
1690 ops->destroy_state(slwt);
1693 static int parse_nla_action(struct nlattr **attrs, struct seg6_local_lwt *slwt)
1695 struct seg6_action_param *param;
1696 struct seg6_action_desc *desc;
1697 unsigned long invalid_attrs;
1700 desc = __get_action_desc(slwt->action);
1708 slwt->headroom += desc->static_headroom;
1710 /* Forcing the desc->optattrs *set* and the desc->attrs *set* to be
1711 * disjoined, this allow us to release acquired resources by optional
1712 * attributes and by required attributes independently from each other
1713 * without any interference.
1714 * In other terms, we are sure that we do not release some the acquired
1717 * Note that if an attribute is configured both as required and as
1718 * optional, it means that the user has messed something up in the
1719 * seg6_action_table. Therefore, this check is required for SRv6
1720 * behaviors to work properly.
1722 invalid_attrs = desc->attrs & desc->optattrs;
1723 if (invalid_attrs) {
1725 "An attribute cannot be both required AND optional");
1729 /* parse the required attributes */
1730 for (i = 0; i < SEG6_LOCAL_MAX + 1; i++) {
1731 if (desc->attrs & SEG6_F_ATTR(i)) {
1735 param = &seg6_action_params[i];
1737 err = param->parse(attrs, slwt);
1739 goto parse_attrs_err;
1743 /* parse the optional attributes, if any */
1744 err = parse_nla_optional_attrs(attrs, slwt);
1746 goto parse_attrs_err;
1751 /* release any resource that may have been acquired during the i-1
1752 * parse() operations.
1754 __destroy_attrs(desc->attrs, i, slwt);
1759 static int seg6_local_build_state(struct net *net, struct nlattr *nla,
1760 unsigned int family, const void *cfg,
1761 struct lwtunnel_state **ts,
1762 struct netlink_ext_ack *extack)
1764 struct nlattr *tb[SEG6_LOCAL_MAX + 1];
1765 struct lwtunnel_state *newts;
1766 struct seg6_local_lwt *slwt;
1769 if (family != AF_INET6)
1772 err = nla_parse_nested_deprecated(tb, SEG6_LOCAL_MAX, nla,
1773 seg6_local_policy, extack);
1778 if (!tb[SEG6_LOCAL_ACTION])
1781 newts = lwtunnel_state_alloc(sizeof(*slwt));
1785 slwt = seg6_local_lwtunnel(newts);
1786 slwt->action = nla_get_u32(tb[SEG6_LOCAL_ACTION]);
1788 err = parse_nla_action(tb, slwt);
1792 err = seg6_local_lwtunnel_build_state(slwt, cfg, extack);
1794 goto out_destroy_attrs;
1796 newts->type = LWTUNNEL_ENCAP_SEG6_LOCAL;
1797 newts->flags = LWTUNNEL_STATE_INPUT_REDIRECT;
1798 newts->headroom = slwt->headroom;
1805 destroy_attrs(slwt);
1811 static void seg6_local_destroy_state(struct lwtunnel_state *lwt)
1813 struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
1815 seg6_local_lwtunnel_destroy_state(slwt);
1817 destroy_attrs(slwt);
1822 static int seg6_local_fill_encap(struct sk_buff *skb,
1823 struct lwtunnel_state *lwt)
1825 struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
1826 struct seg6_action_param *param;
1827 unsigned long attrs;
1830 if (nla_put_u32(skb, SEG6_LOCAL_ACTION, slwt->action))
1833 attrs = slwt->desc->attrs | slwt->parsed_optattrs;
1835 for (i = 0; i < SEG6_LOCAL_MAX + 1; i++) {
1836 if (attrs & SEG6_F_ATTR(i)) {
1837 param = &seg6_action_params[i];
1838 err = param->put(skb, slwt);
1847 static int seg6_local_get_encap_size(struct lwtunnel_state *lwt)
1849 struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
1850 unsigned long attrs;
1853 nlsize = nla_total_size(4); /* action */
1855 attrs = slwt->desc->attrs | slwt->parsed_optattrs;
1857 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_SRH))
1858 nlsize += nla_total_size((slwt->srh->hdrlen + 1) << 3);
1860 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_TABLE))
1861 nlsize += nla_total_size(4);
1863 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_NH4))
1864 nlsize += nla_total_size(4);
1866 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_NH6))
1867 nlsize += nla_total_size(16);
1869 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_IIF))
1870 nlsize += nla_total_size(4);
1872 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_OIF))
1873 nlsize += nla_total_size(4);
1875 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_BPF))
1876 nlsize += nla_total_size(sizeof(struct nlattr)) +
1877 nla_total_size(MAX_PROG_NAME) +
1880 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_VRFTABLE))
1881 nlsize += nla_total_size(4);
1883 if (attrs & SEG6_F_LOCAL_COUNTERS)
1884 nlsize += nla_total_size(0) + /* nest SEG6_LOCAL_COUNTERS */
1885 /* SEG6_LOCAL_CNT_PACKETS */
1886 nla_total_size_64bit(sizeof(__u64)) +
1887 /* SEG6_LOCAL_CNT_BYTES */
1888 nla_total_size_64bit(sizeof(__u64)) +
1889 /* SEG6_LOCAL_CNT_ERRORS */
1890 nla_total_size_64bit(sizeof(__u64));
1895 static int seg6_local_cmp_encap(struct lwtunnel_state *a,
1896 struct lwtunnel_state *b)
1898 struct seg6_local_lwt *slwt_a, *slwt_b;
1899 struct seg6_action_param *param;
1900 unsigned long attrs_a, attrs_b;
1903 slwt_a = seg6_local_lwtunnel(a);
1904 slwt_b = seg6_local_lwtunnel(b);
1906 if (slwt_a->action != slwt_b->action)
1909 attrs_a = slwt_a->desc->attrs | slwt_a->parsed_optattrs;
1910 attrs_b = slwt_b->desc->attrs | slwt_b->parsed_optattrs;
1912 if (attrs_a != attrs_b)
1915 for (i = 0; i < SEG6_LOCAL_MAX + 1; i++) {
1916 if (attrs_a & SEG6_F_ATTR(i)) {
1917 param = &seg6_action_params[i];
1918 if (param->cmp(slwt_a, slwt_b))
1926 static const struct lwtunnel_encap_ops seg6_local_ops = {
1927 .build_state = seg6_local_build_state,
1928 .destroy_state = seg6_local_destroy_state,
1929 .input = seg6_local_input,
1930 .fill_encap = seg6_local_fill_encap,
1931 .get_encap_size = seg6_local_get_encap_size,
1932 .cmp_encap = seg6_local_cmp_encap,
1933 .owner = THIS_MODULE,
1936 int __init seg6_local_init(void)
1938 /* If the max total number of defined attributes is reached, then your
1939 * kernel build stops here.
1941 * This check is required to avoid arithmetic overflows when processing
1942 * behavior attributes and the maximum number of defined attributes
1943 * exceeds the allowed value.
1945 BUILD_BUG_ON(SEG6_LOCAL_MAX + 1 > BITS_PER_TYPE(unsigned long));
1947 return lwtunnel_encap_add_ops(&seg6_local_ops,
1948 LWTUNNEL_ENCAP_SEG6_LOCAL);
1951 void seg6_local_exit(void)
1953 lwtunnel_encap_del_ops(&seg6_local_ops, LWTUNNEL_ENCAP_SEG6_LOCAL);