1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * SR-IPv6 implementation
6 * David Lebrun <david.lebrun@uclouvain.be>
7 * eBPF support: Mathieu Xhonneux <m.xhonneux@gmail.com>
10 #include <linux/filter.h>
11 #include <linux/types.h>
12 #include <linux/skbuff.h>
13 #include <linux/net.h>
14 #include <linux/module.h>
16 #include <net/lwtunnel.h>
17 #include <net/netevent.h>
18 #include <net/netns/generic.h>
19 #include <net/ip6_fib.h>
20 #include <net/route.h>
22 #include <linux/seg6.h>
23 #include <linux/seg6_local.h>
24 #include <net/addrconf.h>
25 #include <net/ip6_route.h>
26 #include <net/dst_cache.h>
27 #include <net/ip_tunnels.h>
28 #ifdef CONFIG_IPV6_SEG6_HMAC
29 #include <net/seg6_hmac.h>
31 #include <net/seg6_local.h>
32 #include <linux/etherdevice.h>
33 #include <linux/bpf.h>
34 #include <linux/netfilter.h>
36 #define SEG6_F_ATTR(i) BIT(i)
38 struct seg6_local_lwt;
40 /* callbacks used for customizing the creation and destruction of a behavior */
41 struct seg6_local_lwtunnel_ops {
42 int (*build_state)(struct seg6_local_lwt *slwt, const void *cfg,
43 struct netlink_ext_ack *extack);
44 void (*destroy_state)(struct seg6_local_lwt *slwt);
47 struct seg6_action_desc {
51 /* The optattrs field is used for specifying all the optional
52 * attributes supported by a specific behavior.
53 * It means that if one of these attributes is not provided in the
54 * netlink message during the behavior creation, no errors will be
55 * returned to the userspace.
57 * Each attribute can be only of two types (mutually exclusive):
58 * 1) required or 2) optional.
59 * Every user MUST obey to this rule! If you set an attribute as
60 * required the same attribute CANNOT be set as optional and vice
63 unsigned long optattrs;
65 int (*input)(struct sk_buff *skb, struct seg6_local_lwt *slwt);
68 struct seg6_local_lwtunnel_ops slwt_ops;
72 struct bpf_prog *prog;
76 /* default length values (expressed in bits) for both Locator-Block and
77 * Locator-Node Function.
79 * Both SEG6_LOCAL_LCBLOCK_DBITS and SEG6_LOCAL_LCNODE_FN_DBITS *must* be:
81 * ii) evenly divisible by 8. In other terms, the lengths of the
82 * Locator-Block and Locator-Node Function must be byte-aligned (we can
83 * relax this constraint in the future if really needed).
85 * Moreover, a third condition must hold:
86 * iii) SEG6_LOCAL_LCBLOCK_DBITS + SEG6_LOCAL_LCNODE_FN_DBITS <= 128.
88 * The correctness of SEG6_LOCAL_LCBLOCK_DBITS and SEG6_LOCAL_LCNODE_FN_DBITS
89 * values are checked during the kernel compilation. If the compilation stops,
90 * check the value of these parameters to see if they meet conditions (i), (ii)
93 #define SEG6_LOCAL_LCBLOCK_DBITS 32
94 #define SEG6_LOCAL_LCNODE_FN_DBITS 16
96 /* The following next_csid_chk_{cntr,lcblock,lcblock_fn}_bits macros can be
97 * used directly to check whether the lengths (in bits) of Locator-Block and
98 * Locator-Node Function are valid according to (i), (ii), (iii).
100 #define next_csid_chk_cntr_bits(blen, flen) \
101 ((blen) + (flen) > 128)
103 #define next_csid_chk_lcblock_bits(blen) \
105 typeof(blen) __tmp = blen; \
106 (!__tmp || __tmp > 120 || (__tmp & 0x07)); \
109 #define next_csid_chk_lcnode_fn_bits(flen) \
110 next_csid_chk_lcblock_bits(flen)
112 #define SEG6_F_LOCAL_FLV_OP(flvname) BIT(SEG6_LOCAL_FLV_OP_##flvname)
113 #define SEG6_F_LOCAL_FLV_PSP SEG6_F_LOCAL_FLV_OP(PSP)
115 /* Supported RFC8986 Flavor operations are reported in this bitmask */
116 #define SEG6_LOCAL_FLV8986_SUPP_OPS SEG6_F_LOCAL_FLV_PSP
118 /* Supported Flavor operations are reported in this bitmask */
119 #define SEG6_LOCAL_FLV_SUPP_OPS (SEG6_F_LOCAL_FLV_OP(NEXT_CSID) | \
120 SEG6_LOCAL_FLV8986_SUPP_OPS)
122 struct seg6_flavors_info {
123 /* Flavor operations */
126 /* Locator-Block length, expressed in bits */
128 /* Locator-Node Function length, expressed in bits*/
129 __u8 lcnode_func_bits;
132 enum seg6_end_dt_mode {
133 DT_INVALID_MODE = -EINVAL,
138 struct seg6_end_dt_info {
139 enum seg6_end_dt_mode mode;
142 /* VRF device associated to the routing table used by the SRv6
143 * End.DT4/DT6 behavior for routing IPv4/IPv6 packets.
148 /* tunneled packet family (IPv4 or IPv6).
149 * Protocol and header length are inferred from family.
154 struct pcpu_seg6_local_counters {
159 struct u64_stats_sync syncp;
162 /* This struct groups all the SRv6 Behavior counters supported so far.
164 * put_nla_counters() makes use of this data structure to collect all counter
165 * values after the per-CPU counter evaluation has been performed.
166 * Finally, each counter value (in seg6_local_counters) is stored in the
167 * corresponding netlink attribute and sent to user space.
169 * NB: we don't want to expose this structure to user space!
171 struct seg6_local_counters {
177 #define seg6_local_alloc_pcpu_counters(__gfp) \
178 __netdev_alloc_pcpu_stats(struct pcpu_seg6_local_counters, \
179 ((__gfp) | __GFP_ZERO))
181 #define SEG6_F_LOCAL_COUNTERS SEG6_F_ATTR(SEG6_LOCAL_COUNTERS)
183 struct seg6_local_lwt {
185 struct ipv6_sr_hdr *srh;
191 struct bpf_lwt_prog bpf;
192 #ifdef CONFIG_NET_L3_MASTER_DEV
193 struct seg6_end_dt_info dt_info;
195 struct seg6_flavors_info flv_info;
197 struct pcpu_seg6_local_counters __percpu *pcpu_counters;
200 struct seg6_action_desc *desc;
201 /* unlike the required attrs, we have to track the optional attributes
202 * that have been effectively parsed.
204 unsigned long parsed_optattrs;
207 static struct seg6_local_lwt *seg6_local_lwtunnel(struct lwtunnel_state *lwt)
209 return (struct seg6_local_lwt *)lwt->data;
212 static struct ipv6_sr_hdr *get_and_validate_srh(struct sk_buff *skb)
214 struct ipv6_sr_hdr *srh;
216 srh = seg6_get_srh(skb, IP6_FH_F_SKIP_RH);
220 #ifdef CONFIG_IPV6_SEG6_HMAC
221 if (!seg6_hmac_validate_skb(skb))
228 static bool decap_and_validate(struct sk_buff *skb, int proto)
230 struct ipv6_sr_hdr *srh;
231 unsigned int off = 0;
233 srh = seg6_get_srh(skb, 0);
234 if (srh && srh->segments_left > 0)
237 #ifdef CONFIG_IPV6_SEG6_HMAC
238 if (srh && !seg6_hmac_validate_skb(skb))
242 if (ipv6_find_hdr(skb, &off, proto, NULL, NULL) < 0)
245 if (!pskb_pull(skb, off))
248 skb_postpull_rcsum(skb, skb_network_header(skb), off);
250 skb_reset_network_header(skb);
251 skb_reset_transport_header(skb);
252 if (iptunnel_pull_offloads(skb))
258 static void advance_nextseg(struct ipv6_sr_hdr *srh, struct in6_addr *daddr)
260 struct in6_addr *addr;
262 srh->segments_left--;
263 addr = srh->segments + srh->segments_left;
268 seg6_lookup_any_nexthop(struct sk_buff *skb, struct in6_addr *nhaddr,
269 u32 tbl_id, bool local_delivery)
271 struct net *net = dev_net(skb->dev);
272 struct ipv6hdr *hdr = ipv6_hdr(skb);
273 int flags = RT6_LOOKUP_F_HAS_SADDR;
274 struct dst_entry *dst = NULL;
279 memset(&fl6, 0, sizeof(fl6));
280 fl6.flowi6_iif = skb->dev->ifindex;
281 fl6.daddr = nhaddr ? *nhaddr : hdr->daddr;
282 fl6.saddr = hdr->saddr;
283 fl6.flowlabel = ip6_flowinfo(hdr);
284 fl6.flowi6_mark = skb->mark;
285 fl6.flowi6_proto = hdr->nexthdr;
288 fl6.flowi6_flags = FLOWI_FLAG_KNOWN_NH;
291 dst = ip6_route_input_lookup(net, skb->dev, &fl6, skb, flags);
293 struct fib6_table *table;
295 table = fib6_get_table(net, tbl_id);
299 rt = ip6_pol_route(net, table, 0, &fl6, skb, flags);
303 /* we want to discard traffic destined for local packet processing,
304 * if @local_delivery is set to false.
307 dev_flags |= IFF_LOOPBACK;
309 if (dst && (dst->dev->flags & dev_flags) && !dst->error) {
316 rt = net->ipv6.ip6_blk_hole_entry;
322 skb_dst_set(skb, dst);
326 int seg6_lookup_nexthop(struct sk_buff *skb,
327 struct in6_addr *nhaddr, u32 tbl_id)
329 return seg6_lookup_any_nexthop(skb, nhaddr, tbl_id, false);
332 static __u8 seg6_flv_lcblock_octects(const struct seg6_flavors_info *finfo)
334 return finfo->lcblock_bits >> 3;
337 static __u8 seg6_flv_lcnode_func_octects(const struct seg6_flavors_info *finfo)
339 return finfo->lcnode_func_bits >> 3;
342 static bool seg6_next_csid_is_arg_zero(const struct in6_addr *addr,
343 const struct seg6_flavors_info *finfo)
345 __u8 fnc_octects = seg6_flv_lcnode_func_octects(finfo);
346 __u8 blk_octects = seg6_flv_lcblock_octects(finfo);
350 arg_octects = 16 - blk_octects - fnc_octects;
351 for (i = 0; i < arg_octects; ++i) {
352 if (addr->s6_addr[blk_octects + fnc_octects + i] != 0x00)
359 /* assume that DA.Argument length > 0 */
360 static void seg6_next_csid_advance_arg(struct in6_addr *addr,
361 const struct seg6_flavors_info *finfo)
363 __u8 fnc_octects = seg6_flv_lcnode_func_octects(finfo);
364 __u8 blk_octects = seg6_flv_lcblock_octects(finfo);
366 /* advance DA.Argument */
367 memmove(&addr->s6_addr[blk_octects],
368 &addr->s6_addr[blk_octects + fnc_octects],
369 16 - blk_octects - fnc_octects);
371 memset(&addr->s6_addr[16 - fnc_octects], 0x00, fnc_octects);
374 static int input_action_end_finish(struct sk_buff *skb,
375 struct seg6_local_lwt *slwt)
377 seg6_lookup_nexthop(skb, NULL, 0);
379 return dst_input(skb);
382 static int input_action_end_core(struct sk_buff *skb,
383 struct seg6_local_lwt *slwt)
385 struct ipv6_sr_hdr *srh;
387 srh = get_and_validate_srh(skb);
391 advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
393 return input_action_end_finish(skb, slwt);
400 static int end_next_csid_core(struct sk_buff *skb, struct seg6_local_lwt *slwt)
402 const struct seg6_flavors_info *finfo = &slwt->flv_info;
403 struct in6_addr *daddr = &ipv6_hdr(skb)->daddr;
405 if (seg6_next_csid_is_arg_zero(daddr, finfo))
406 return input_action_end_core(skb, slwt);
409 seg6_next_csid_advance_arg(daddr, finfo);
411 return input_action_end_finish(skb, slwt);
414 static bool seg6_next_csid_enabled(__u32 fops)
416 return fops & BIT(SEG6_LOCAL_FLV_OP_NEXT_CSID);
419 /* We describe the packet state in relation to the absence/presence of the SRH
420 * and the Segment Left (SL) field.
421 * For our purposes, it is not necessary to record the exact value of the SL
422 * when the SID List consists of two or more segments.
424 enum seg6_local_pktinfo {
425 /* the order really matters! */
426 SEG6_LOCAL_PKTINFO_NOHDR = 0,
427 SEG6_LOCAL_PKTINFO_SL_ZERO,
428 SEG6_LOCAL_PKTINFO_SL_ONE,
429 SEG6_LOCAL_PKTINFO_SL_MORE,
430 __SEG6_LOCAL_PKTINFO_MAX,
433 #define SEG6_LOCAL_PKTINFO_MAX (__SEG6_LOCAL_PKTINFO_MAX - 1)
435 static enum seg6_local_pktinfo seg6_get_srh_pktinfo(struct ipv6_sr_hdr *srh)
440 return SEG6_LOCAL_PKTINFO_NOHDR;
442 sgl = srh->segments_left;
444 return SEG6_LOCAL_PKTINFO_SL_ZERO + sgl;
446 return SEG6_LOCAL_PKTINFO_SL_MORE;
449 enum seg6_local_flv_action {
450 SEG6_LOCAL_FLV_ACT_UNSPEC = 0,
451 SEG6_LOCAL_FLV_ACT_END,
452 SEG6_LOCAL_FLV_ACT_PSP,
453 SEG6_LOCAL_FLV_ACT_USP,
454 SEG6_LOCAL_FLV_ACT_USD,
455 __SEG6_LOCAL_FLV_ACT_MAX
458 #define SEG6_LOCAL_FLV_ACT_MAX (__SEG6_LOCAL_FLV_ACT_MAX - 1)
460 /* The action table for RFC8986 flavors (see the flv8986_act_tbl below)
461 * contains the actions (i.e. processing operations) to be applied on packets
462 * when flavors are configured for an End* behavior.
463 * By combining the pkinfo data and from the flavors mask, the macro
464 * computes the index used to access the elements (actions) stored in the
465 * action table. The index is structured as follows:
468 * _______________/\________________
470 * +----------------+----------------+
472 * +----------------+----------------+
473 * ph-1 ... p1 p0 fk-1 ... f1 f0
477 * - 'afm' (adjusted flavor mask) is the mask containing a combination of the
478 * RFC8986 flavors currently supported. 'afm' corresponds to the @fm
479 * argument of the macro whose value is righ-shifted by 1 bit. By doing so,
480 * we discard the SEG6_LOCAL_FLV_OP_UNSPEC flag (bit 0 in @fm) which is
482 * - 'pf' encodes the packet info (pktinfo) regarding the presence/absence of
483 * the SRH, SL = 0, etc. 'pf' is set with the value of @pf provided as
484 * argument to the macro.
486 #define flv8986_act_tbl_idx(pf, fm) \
487 ((((pf) << bits_per(SEG6_LOCAL_FLV8986_SUPP_OPS)) | \
488 ((fm) & SEG6_LOCAL_FLV8986_SUPP_OPS)) >> SEG6_LOCAL_FLV_OP_PSP)
490 /* We compute the size of the action table by considering the RFC8986 flavors
491 * actually supported by the kernel. In this way, the size is automatically
492 * adjusted when new flavors are supported.
494 #define FLV8986_ACT_TBL_SIZE \
495 roundup_pow_of_two(flv8986_act_tbl_idx(SEG6_LOCAL_PKTINFO_MAX, \
496 SEG6_LOCAL_FLV8986_SUPP_OPS))
498 /* tbl_cfg(act, pf, fm) macro is used to easily configure the action
499 * table; it accepts 3 arguments:
500 * i) @act, the suffix from SEG6_LOCAL_FLV_ACT_{act} representing
501 * the action that should be applied on the packet;
502 * ii) @pf, the suffix from SEG6_LOCAL_PKTINFO_{pf} reporting the packet
503 * info about the lack/presence of SRH, SRH with SL = 0, etc;
504 * iii) @fm, the mask of flavors.
506 #define tbl_cfg(act, pf, fm) \
507 [flv8986_act_tbl_idx(SEG6_LOCAL_PKTINFO_##pf, \
508 (fm))] = SEG6_LOCAL_FLV_ACT_##act
510 /* shorthand for improving readability */
511 #define F_PSP SEG6_F_LOCAL_FLV_PSP
513 /* The table contains, for each combination of the pktinfo data and
514 * flavors, the action that should be taken on a packet (e.g.
515 * "standard" Endpoint processing, Penultimate Segment Pop, etc).
517 * By default, table entries not explicitly configured are initialized with the
518 * SEG6_LOCAL_FLV_ACT_UNSPEC action, which generally has the effect of
519 * discarding the processed packet.
521 static const u8 flv8986_act_tbl[FLV8986_ACT_TBL_SIZE] = {
522 /* PSP variant for packet where SRH with SL = 1 */
523 tbl_cfg(PSP, SL_ONE, F_PSP),
524 /* End for packet where the SRH with SL > 1*/
525 tbl_cfg(END, SL_MORE, F_PSP),
531 /* For each flavor defined in RFC8986 (or a combination of them) an action is
532 * performed on the packet. The specific action depends on:
533 * - info extracted from the packet (i.e. pktinfo data) regarding the
534 * lack/presence of the SRH, and if the SRH is available, on the value of
535 * Segment Left field;
536 * - the mask of flavors configured for the specific SRv6 End* behavior.
538 * The function combines both the pkinfo and the flavors mask to evaluate the
539 * corresponding action to be taken on the packet.
541 static enum seg6_local_flv_action
542 seg6_local_flv8986_act_lookup(enum seg6_local_pktinfo pinfo, __u32 flvmask)
546 /* check if the provided mask of flavors is supported */
547 if (unlikely(flvmask & ~SEG6_LOCAL_FLV8986_SUPP_OPS))
548 return SEG6_LOCAL_FLV_ACT_UNSPEC;
550 index = flv8986_act_tbl_idx(pinfo, flvmask);
551 if (unlikely(index >= FLV8986_ACT_TBL_SIZE))
552 return SEG6_LOCAL_FLV_ACT_UNSPEC;
554 return flv8986_act_tbl[index];
557 /* skb->data must be aligned with skb->network_header */
558 static bool seg6_pop_srh(struct sk_buff *skb, int srhoff)
560 struct ipv6_sr_hdr *srh;
567 if (unlikely(srhoff < sizeof(*iph) ||
568 !pskb_may_pull(skb, srhoff + sizeof(*srh))))
571 srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
572 srhlen = ipv6_optlen(srh);
574 /* we are about to mangle the pkt, let's check if we can write on it */
575 if (unlikely(skb_ensure_writable(skb, srhoff + srhlen)))
578 /* skb_ensure_writable() may change skb pointers; evaluate srh again */
579 srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
580 srh_nexthdr = srh->nexthdr;
582 if (unlikely(!skb_transport_header_was_set(skb)))
585 nhlen = skb_network_header_len(skb);
586 /* we have to deal with the transport header: it could be set before
587 * the SRH, after the SRH, or within it (which is considered wrong,
590 if (likely(nhlen <= srhoff))
592 else if (nhlen >= srhoff + srhlen)
593 /* transport_header is set after the SRH */
594 thoff = nhlen - srhlen;
596 /* transport_header falls inside the SRH; hence, we can't
597 * restore the transport_header pointer properly after
598 * SRH removing operation.
602 /* we need to pop the SRH:
603 * 1) first of all, we pull out everything from IPv6 header up to SRH
604 * (included) evaluating also the rcsum;
605 * 2) we overwrite (and then remove) the SRH by properly moving the
606 * IPv6 along with any extension header that precedes the SRH;
607 * 3) At the end, we push back the pulled headers (except for SRH,
610 skb_pull_rcsum(skb, srhoff + srhlen);
611 memmove(skb_network_header(skb) + srhlen, skb_network_header(skb),
613 skb_push(skb, srhoff);
615 skb_reset_network_header(skb);
616 skb_mac_header_rebuild(skb);
617 if (likely(thoff >= 0))
618 skb_set_transport_header(skb, thoff);
621 if (iph->nexthdr == NEXTHDR_ROUTING) {
622 iph->nexthdr = srh_nexthdr;
624 /* we must look for the extension header (EXTH, for short) that
625 * immediately precedes the SRH we have just removed.
626 * Then, we update the value of the EXTH nexthdr with the one
627 * contained in the SRH nexthdr.
629 unsigned int off = sizeof(*iph);
630 struct ipv6_opt_hdr *hp, _hdr;
631 __u8 nexthdr = iph->nexthdr;
634 if (unlikely(!ipv6_ext_hdr(nexthdr) ||
635 nexthdr == NEXTHDR_NONE))
638 hp = skb_header_pointer(skb, off, sizeof(_hdr), &_hdr);
642 if (hp->nexthdr == NEXTHDR_ROUTING) {
643 hp->nexthdr = srh_nexthdr;
648 case NEXTHDR_FRAGMENT:
651 /* we expect SRH before FRAG and AUTH */
654 off += ipv6_optlen(hp);
658 nexthdr = hp->nexthdr;
662 iph->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
664 skb_postpush_rcsum(skb, iph, srhoff);
669 /* process the packet on the basis of the RFC8986 flavors set for the given
670 * SRv6 End behavior instance.
672 static int end_flv8986_core(struct sk_buff *skb, struct seg6_local_lwt *slwt)
674 const struct seg6_flavors_info *finfo = &slwt->flv_info;
675 enum seg6_local_flv_action action;
676 enum seg6_local_pktinfo pinfo;
677 struct ipv6_sr_hdr *srh;
681 srh = seg6_get_srh(skb, 0);
682 srhoff = srh ? ((unsigned char *)srh - skb->data) : 0;
683 pinfo = seg6_get_srh_pktinfo(srh);
684 #ifdef CONFIG_IPV6_SEG6_HMAC
685 if (srh && !seg6_hmac_validate_skb(skb))
688 flvmask = finfo->flv_ops;
689 if (unlikely(flvmask & ~SEG6_LOCAL_FLV8986_SUPP_OPS)) {
690 pr_warn_once("seg6local: invalid RFC8986 flavors\n");
694 /* retrieve the action triggered by the combination of pktinfo data and
697 action = seg6_local_flv8986_act_lookup(pinfo, flvmask);
699 case SEG6_LOCAL_FLV_ACT_END:
700 /* process the packet as the "standard" End behavior */
701 advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
703 case SEG6_LOCAL_FLV_ACT_PSP:
704 advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
706 if (unlikely(!seg6_pop_srh(skb, srhoff)))
709 case SEG6_LOCAL_FLV_ACT_UNSPEC:
712 /* by default, we drop the packet since we could not find a
718 return input_action_end_finish(skb, slwt);
725 /* regular endpoint function */
726 static int input_action_end(struct sk_buff *skb, struct seg6_local_lwt *slwt)
728 const struct seg6_flavors_info *finfo = &slwt->flv_info;
729 __u32 fops = finfo->flv_ops;
732 return input_action_end_core(skb, slwt);
734 /* check for the presence of NEXT-C-SID since it applies first */
735 if (seg6_next_csid_enabled(fops))
736 return end_next_csid_core(skb, slwt);
738 /* the specific processing function to be performed on the packet
739 * depends on the combination of flavors defined in RFC8986 and some
740 * information extracted from the packet, e.g. presence/absence of SRH,
741 * Segment Left = 0, etc.
743 return end_flv8986_core(skb, slwt);
746 /* regular endpoint, and forward to specified nexthop */
747 static int input_action_end_x(struct sk_buff *skb, struct seg6_local_lwt *slwt)
749 struct ipv6_sr_hdr *srh;
751 srh = get_and_validate_srh(skb);
755 advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
757 seg6_lookup_nexthop(skb, &slwt->nh6, 0);
759 return dst_input(skb);
766 static int input_action_end_t(struct sk_buff *skb, struct seg6_local_lwt *slwt)
768 struct ipv6_sr_hdr *srh;
770 srh = get_and_validate_srh(skb);
774 advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
776 seg6_lookup_nexthop(skb, NULL, slwt->table);
778 return dst_input(skb);
785 /* decapsulate and forward inner L2 frame on specified interface */
786 static int input_action_end_dx2(struct sk_buff *skb,
787 struct seg6_local_lwt *slwt)
789 struct net *net = dev_net(skb->dev);
790 struct net_device *odev;
793 if (!decap_and_validate(skb, IPPROTO_ETHERNET))
796 if (!pskb_may_pull(skb, ETH_HLEN))
799 skb_reset_mac_header(skb);
800 eth = (struct ethhdr *)skb->data;
802 /* To determine the frame's protocol, we assume it is 802.3. This avoids
803 * a call to eth_type_trans(), which is not really relevant for our
806 if (!eth_proto_is_802_3(eth->h_proto))
809 odev = dev_get_by_index_rcu(net, slwt->oif);
813 /* As we accept Ethernet frames, make sure the egress device is of
816 if (odev->type != ARPHRD_ETHER)
819 if (!(odev->flags & IFF_UP) || !netif_carrier_ok(odev))
824 if (skb_warn_if_lro(skb))
827 skb_forward_csum(skb);
829 if (skb->len - ETH_HLEN > odev->mtu)
833 skb->protocol = eth->h_proto;
835 return dev_queue_xmit(skb);
842 static int input_action_end_dx6_finish(struct net *net, struct sock *sk,
845 struct dst_entry *orig_dst = skb_dst(skb);
846 struct in6_addr *nhaddr = NULL;
847 struct seg6_local_lwt *slwt;
849 slwt = seg6_local_lwtunnel(orig_dst->lwtstate);
851 /* The inner packet is not associated to any local interface,
852 * so we do not call netif_rx().
854 * If slwt->nh6 is set to ::, then lookup the nexthop for the
855 * inner packet's DA. Otherwise, use the specified nexthop.
857 if (!ipv6_addr_any(&slwt->nh6))
860 seg6_lookup_nexthop(skb, nhaddr, 0);
862 return dst_input(skb);
865 /* decapsulate and forward to specified nexthop */
866 static int input_action_end_dx6(struct sk_buff *skb,
867 struct seg6_local_lwt *slwt)
869 /* this function accepts IPv6 encapsulated packets, with either
870 * an SRH with SL=0, or no SRH.
873 if (!decap_and_validate(skb, IPPROTO_IPV6))
876 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
879 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
882 if (static_branch_unlikely(&nf_hooks_lwtunnel_enabled))
883 return NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING,
884 dev_net(skb->dev), NULL, skb, NULL,
885 skb_dst(skb)->dev, input_action_end_dx6_finish);
887 return input_action_end_dx6_finish(dev_net(skb->dev), NULL, skb);
893 static int input_action_end_dx4_finish(struct net *net, struct sock *sk,
896 struct dst_entry *orig_dst = skb_dst(skb);
897 struct seg6_local_lwt *slwt;
902 slwt = seg6_local_lwtunnel(orig_dst->lwtstate);
906 nhaddr = slwt->nh4.s_addr ?: iph->daddr;
910 err = ip_route_input(skb, nhaddr, iph->saddr, 0, skb->dev);
916 return dst_input(skb);
919 static int input_action_end_dx4(struct sk_buff *skb,
920 struct seg6_local_lwt *slwt)
922 if (!decap_and_validate(skb, IPPROTO_IPIP))
925 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
928 skb->protocol = htons(ETH_P_IP);
929 skb_set_transport_header(skb, sizeof(struct iphdr));
932 if (static_branch_unlikely(&nf_hooks_lwtunnel_enabled))
933 return NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING,
934 dev_net(skb->dev), NULL, skb, NULL,
935 skb_dst(skb)->dev, input_action_end_dx4_finish);
937 return input_action_end_dx4_finish(dev_net(skb->dev), NULL, skb);
943 #ifdef CONFIG_NET_L3_MASTER_DEV
944 static struct net *fib6_config_get_net(const struct fib6_config *fib6_cfg)
946 const struct nl_info *nli = &fib6_cfg->fc_nlinfo;
951 static int __seg6_end_dt_vrf_build(struct seg6_local_lwt *slwt, const void *cfg,
952 u16 family, struct netlink_ext_ack *extack)
954 struct seg6_end_dt_info *info = &slwt->dt_info;
958 net = fib6_config_get_net(cfg);
960 /* note that vrf_table was already set by parse_nla_vrftable() */
961 vrf_ifindex = l3mdev_ifindex_lookup_by_table_id(L3MDEV_TYPE_VRF, net,
963 if (vrf_ifindex < 0) {
964 if (vrf_ifindex == -EPERM) {
965 NL_SET_ERR_MSG(extack,
966 "Strict mode for VRF is disabled");
967 } else if (vrf_ifindex == -ENODEV) {
968 NL_SET_ERR_MSG(extack,
969 "Table has no associated VRF device");
971 pr_debug("seg6local: SRv6 End.DT* creation error=%d\n",
979 info->vrf_ifindex = vrf_ifindex;
981 info->family = family;
982 info->mode = DT_VRF_MODE;
987 /* The SRv6 End.DT4/DT6 behavior extracts the inner (IPv4/IPv6) packet and
988 * routes the IPv4/IPv6 packet by looking at the configured routing table.
990 * In the SRv6 End.DT4/DT6 use case, we can receive traffic (IPv6+Segment
991 * Routing Header packets) from several interfaces and the outer IPv6
992 * destination address (DA) is used for retrieving the specific instance of the
993 * End.DT4/DT6 behavior that should process the packets.
995 * However, the inner IPv4/IPv6 packet is not really bound to any receiving
996 * interface and thus the End.DT4/DT6 sets the VRF (associated with the
997 * corresponding routing table) as the *receiving* interface.
998 * In other words, the End.DT4/DT6 processes a packet as if it has been received
999 * directly by the VRF (and not by one of its slave devices, if any).
1000 * In this way, the VRF interface is used for routing the IPv4/IPv6 packet in
1001 * according to the routing table configured by the End.DT4/DT6 instance.
1003 * This design allows you to get some interesting features like:
1004 * 1) the statistics on rx packets;
1005 * 2) the possibility to install a packet sniffer on the receiving interface
1006 * (the VRF one) for looking at the incoming packets;
1007 * 3) the possibility to leverage the netfilter prerouting hook for the inner
1010 * This function returns:
1011 * - the sk_buff* when the VRF rcv handler has processed the packet correctly;
1012 * - NULL when the skb is consumed by the VRF rcv handler;
1013 * - a pointer which encodes a negative error number in case of error.
1014 * Note that in this case, the function takes care of freeing the skb.
1016 static struct sk_buff *end_dt_vrf_rcv(struct sk_buff *skb, u16 family,
1017 struct net_device *dev)
1019 /* based on l3mdev_ip_rcv; we are only interested in the master */
1020 if (unlikely(!netif_is_l3_master(dev) && !netif_has_l3_rx_handler(dev)))
1023 if (unlikely(!dev->l3mdev_ops->l3mdev_l3_rcv))
1026 /* the decap packet IPv4/IPv6 does not come with any mac header info.
1027 * We must unset the mac header to allow the VRF device to rebuild it,
1028 * just in case there is a sniffer attached on the device.
1030 skb_unset_mac_header(skb);
1032 skb = dev->l3mdev_ops->l3mdev_l3_rcv(dev, skb, family);
1034 /* the skb buffer was consumed by the handler */
1037 /* when a packet is received by a VRF or by one of its slaves, the
1038 * master device reference is set into the skb.
1040 if (unlikely(skb->dev != dev || skb->skb_iif != dev->ifindex))
1047 return ERR_PTR(-EINVAL);
1050 static struct net_device *end_dt_get_vrf_rcu(struct sk_buff *skb,
1051 struct seg6_end_dt_info *info)
1053 int vrf_ifindex = info->vrf_ifindex;
1054 struct net *net = info->net;
1056 if (unlikely(vrf_ifindex < 0))
1059 if (unlikely(!net_eq(dev_net(skb->dev), net)))
1062 return dev_get_by_index_rcu(net, vrf_ifindex);
1068 static struct sk_buff *end_dt_vrf_core(struct sk_buff *skb,
1069 struct seg6_local_lwt *slwt, u16 family)
1071 struct seg6_end_dt_info *info = &slwt->dt_info;
1072 struct net_device *vrf;
1076 vrf = end_dt_get_vrf_rcu(skb, info);
1082 protocol = htons(ETH_P_IP);
1083 hdrlen = sizeof(struct iphdr);
1086 protocol = htons(ETH_P_IPV6);
1087 hdrlen = sizeof(struct ipv6hdr);
1095 if (unlikely(info->family != AF_UNSPEC && info->family != family)) {
1096 pr_warn_once("seg6local: SRv6 End.DT* family mismatch");
1100 skb->protocol = protocol;
1104 skb_set_transport_header(skb, hdrlen);
1107 return end_dt_vrf_rcv(skb, family, vrf);
1111 return ERR_PTR(-EINVAL);
1114 static int input_action_end_dt4(struct sk_buff *skb,
1115 struct seg6_local_lwt *slwt)
1120 if (!decap_and_validate(skb, IPPROTO_IPIP))
1123 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1126 skb = end_dt_vrf_core(skb, slwt, AF_INET);
1128 /* packet has been processed and consumed by the VRF */
1132 return PTR_ERR(skb);
1136 err = ip_route_input(skb, iph->daddr, iph->saddr, 0, skb->dev);
1140 return dst_input(skb);
1147 static int seg6_end_dt4_build(struct seg6_local_lwt *slwt, const void *cfg,
1148 struct netlink_ext_ack *extack)
1150 return __seg6_end_dt_vrf_build(slwt, cfg, AF_INET, extack);
1154 seg6_end_dt_mode seg6_end_dt6_parse_mode(struct seg6_local_lwt *slwt)
1156 unsigned long parsed_optattrs = slwt->parsed_optattrs;
1157 bool legacy, vrfmode;
1159 legacy = !!(parsed_optattrs & SEG6_F_ATTR(SEG6_LOCAL_TABLE));
1160 vrfmode = !!(parsed_optattrs & SEG6_F_ATTR(SEG6_LOCAL_VRFTABLE));
1162 if (!(legacy ^ vrfmode))
1163 /* both are absent or present: invalid DT6 mode */
1164 return DT_INVALID_MODE;
1166 return legacy ? DT_LEGACY_MODE : DT_VRF_MODE;
1169 static enum seg6_end_dt_mode seg6_end_dt6_get_mode(struct seg6_local_lwt *slwt)
1171 struct seg6_end_dt_info *info = &slwt->dt_info;
1176 static int seg6_end_dt6_build(struct seg6_local_lwt *slwt, const void *cfg,
1177 struct netlink_ext_ack *extack)
1179 enum seg6_end_dt_mode mode = seg6_end_dt6_parse_mode(slwt);
1180 struct seg6_end_dt_info *info = &slwt->dt_info;
1183 case DT_LEGACY_MODE:
1184 info->mode = DT_LEGACY_MODE;
1187 return __seg6_end_dt_vrf_build(slwt, cfg, AF_INET6, extack);
1189 NL_SET_ERR_MSG(extack, "table or vrftable must be specified");
1195 static int input_action_end_dt6(struct sk_buff *skb,
1196 struct seg6_local_lwt *slwt)
1198 if (!decap_and_validate(skb, IPPROTO_IPV6))
1201 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
1204 #ifdef CONFIG_NET_L3_MASTER_DEV
1205 if (seg6_end_dt6_get_mode(slwt) == DT_LEGACY_MODE)
1209 skb = end_dt_vrf_core(skb, slwt, AF_INET6);
1211 /* packet has been processed and consumed by the VRF */
1215 return PTR_ERR(skb);
1217 /* note: this time we do not need to specify the table because the VRF
1218 * takes care of selecting the correct table.
1220 seg6_lookup_any_nexthop(skb, NULL, 0, true);
1222 return dst_input(skb);
1226 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
1228 seg6_lookup_any_nexthop(skb, NULL, slwt->table, true);
1230 return dst_input(skb);
1237 #ifdef CONFIG_NET_L3_MASTER_DEV
1238 static int seg6_end_dt46_build(struct seg6_local_lwt *slwt, const void *cfg,
1239 struct netlink_ext_ack *extack)
1241 return __seg6_end_dt_vrf_build(slwt, cfg, AF_UNSPEC, extack);
1244 static int input_action_end_dt46(struct sk_buff *skb,
1245 struct seg6_local_lwt *slwt)
1247 unsigned int off = 0;
1250 nexthdr = ipv6_find_hdr(skb, &off, -1, NULL, NULL);
1251 if (unlikely(nexthdr < 0))
1256 return input_action_end_dt4(skb, slwt);
1258 return input_action_end_dt6(skb, slwt);
1267 /* push an SRH on top of the current one */
1268 static int input_action_end_b6(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1270 struct ipv6_sr_hdr *srh;
1273 srh = get_and_validate_srh(skb);
1277 err = seg6_do_srh_inline(skb, slwt->srh);
1281 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
1283 seg6_lookup_nexthop(skb, NULL, 0);
1285 return dst_input(skb);
1292 /* encapsulate within an outer IPv6 header and a specified SRH */
1293 static int input_action_end_b6_encap(struct sk_buff *skb,
1294 struct seg6_local_lwt *slwt)
1296 struct ipv6_sr_hdr *srh;
1299 srh = get_and_validate_srh(skb);
1303 advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
1305 skb_reset_inner_headers(skb);
1306 skb->encapsulation = 1;
1308 err = seg6_do_srh_encap(skb, slwt->srh, IPPROTO_IPV6);
1312 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
1314 seg6_lookup_nexthop(skb, NULL, 0);
1316 return dst_input(skb);
1323 DEFINE_PER_CPU(struct seg6_bpf_srh_state, seg6_bpf_srh_states);
1325 bool seg6_bpf_has_valid_srh(struct sk_buff *skb)
1327 struct seg6_bpf_srh_state *srh_state =
1328 this_cpu_ptr(&seg6_bpf_srh_states);
1329 struct ipv6_sr_hdr *srh = srh_state->srh;
1331 if (unlikely(srh == NULL))
1334 if (unlikely(!srh_state->valid)) {
1335 if ((srh_state->hdrlen & 7) != 0)
1338 srh->hdrlen = (u8)(srh_state->hdrlen >> 3);
1339 if (!seg6_validate_srh(srh, (srh->hdrlen + 1) << 3, true))
1342 srh_state->valid = true;
1348 static int input_action_end_bpf(struct sk_buff *skb,
1349 struct seg6_local_lwt *slwt)
1351 struct seg6_bpf_srh_state *srh_state =
1352 this_cpu_ptr(&seg6_bpf_srh_states);
1353 struct ipv6_sr_hdr *srh;
1356 srh = get_and_validate_srh(skb);
1361 advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
1363 /* preempt_disable is needed to protect the per-CPU buffer srh_state,
1364 * which is also accessed by the bpf_lwt_seg6_* helpers
1367 srh_state->srh = srh;
1368 srh_state->hdrlen = srh->hdrlen << 3;
1369 srh_state->valid = true;
1372 bpf_compute_data_pointers(skb);
1373 ret = bpf_prog_run_save_cb(slwt->bpf.prog, skb);
1383 pr_warn_once("bpf-seg6local: Illegal return value %u\n", ret);
1387 if (srh_state->srh && !seg6_bpf_has_valid_srh(skb))
1391 if (ret != BPF_REDIRECT)
1392 seg6_lookup_nexthop(skb, NULL, 0);
1394 return dst_input(skb);
1402 static struct seg6_action_desc seg6_action_table[] = {
1404 .action = SEG6_LOCAL_ACTION_END,
1406 .optattrs = SEG6_F_LOCAL_COUNTERS |
1407 SEG6_F_ATTR(SEG6_LOCAL_FLAVORS),
1408 .input = input_action_end,
1411 .action = SEG6_LOCAL_ACTION_END_X,
1412 .attrs = SEG6_F_ATTR(SEG6_LOCAL_NH6),
1413 .optattrs = SEG6_F_LOCAL_COUNTERS,
1414 .input = input_action_end_x,
1417 .action = SEG6_LOCAL_ACTION_END_T,
1418 .attrs = SEG6_F_ATTR(SEG6_LOCAL_TABLE),
1419 .optattrs = SEG6_F_LOCAL_COUNTERS,
1420 .input = input_action_end_t,
1423 .action = SEG6_LOCAL_ACTION_END_DX2,
1424 .attrs = SEG6_F_ATTR(SEG6_LOCAL_OIF),
1425 .optattrs = SEG6_F_LOCAL_COUNTERS,
1426 .input = input_action_end_dx2,
1429 .action = SEG6_LOCAL_ACTION_END_DX6,
1430 .attrs = SEG6_F_ATTR(SEG6_LOCAL_NH6),
1431 .optattrs = SEG6_F_LOCAL_COUNTERS,
1432 .input = input_action_end_dx6,
1435 .action = SEG6_LOCAL_ACTION_END_DX4,
1436 .attrs = SEG6_F_ATTR(SEG6_LOCAL_NH4),
1437 .optattrs = SEG6_F_LOCAL_COUNTERS,
1438 .input = input_action_end_dx4,
1441 .action = SEG6_LOCAL_ACTION_END_DT4,
1442 .attrs = SEG6_F_ATTR(SEG6_LOCAL_VRFTABLE),
1443 .optattrs = SEG6_F_LOCAL_COUNTERS,
1444 #ifdef CONFIG_NET_L3_MASTER_DEV
1445 .input = input_action_end_dt4,
1447 .build_state = seg6_end_dt4_build,
1452 .action = SEG6_LOCAL_ACTION_END_DT6,
1453 #ifdef CONFIG_NET_L3_MASTER_DEV
1455 .optattrs = SEG6_F_LOCAL_COUNTERS |
1456 SEG6_F_ATTR(SEG6_LOCAL_TABLE) |
1457 SEG6_F_ATTR(SEG6_LOCAL_VRFTABLE),
1459 .build_state = seg6_end_dt6_build,
1462 .attrs = SEG6_F_ATTR(SEG6_LOCAL_TABLE),
1463 .optattrs = SEG6_F_LOCAL_COUNTERS,
1465 .input = input_action_end_dt6,
1468 .action = SEG6_LOCAL_ACTION_END_DT46,
1469 .attrs = SEG6_F_ATTR(SEG6_LOCAL_VRFTABLE),
1470 .optattrs = SEG6_F_LOCAL_COUNTERS,
1471 #ifdef CONFIG_NET_L3_MASTER_DEV
1472 .input = input_action_end_dt46,
1474 .build_state = seg6_end_dt46_build,
1479 .action = SEG6_LOCAL_ACTION_END_B6,
1480 .attrs = SEG6_F_ATTR(SEG6_LOCAL_SRH),
1481 .optattrs = SEG6_F_LOCAL_COUNTERS,
1482 .input = input_action_end_b6,
1485 .action = SEG6_LOCAL_ACTION_END_B6_ENCAP,
1486 .attrs = SEG6_F_ATTR(SEG6_LOCAL_SRH),
1487 .optattrs = SEG6_F_LOCAL_COUNTERS,
1488 .input = input_action_end_b6_encap,
1489 .static_headroom = sizeof(struct ipv6hdr),
1492 .action = SEG6_LOCAL_ACTION_END_BPF,
1493 .attrs = SEG6_F_ATTR(SEG6_LOCAL_BPF),
1494 .optattrs = SEG6_F_LOCAL_COUNTERS,
1495 .input = input_action_end_bpf,
1500 static struct seg6_action_desc *__get_action_desc(int action)
1502 struct seg6_action_desc *desc;
1505 count = ARRAY_SIZE(seg6_action_table);
1506 for (i = 0; i < count; i++) {
1507 desc = &seg6_action_table[i];
1508 if (desc->action == action)
1515 static bool seg6_lwtunnel_counters_enabled(struct seg6_local_lwt *slwt)
1517 return slwt->parsed_optattrs & SEG6_F_LOCAL_COUNTERS;
1520 static void seg6_local_update_counters(struct seg6_local_lwt *slwt,
1521 unsigned int len, int err)
1523 struct pcpu_seg6_local_counters *pcounters;
1525 pcounters = this_cpu_ptr(slwt->pcpu_counters);
1526 u64_stats_update_begin(&pcounters->syncp);
1529 u64_stats_inc(&pcounters->packets);
1530 u64_stats_add(&pcounters->bytes, len);
1532 u64_stats_inc(&pcounters->errors);
1535 u64_stats_update_end(&pcounters->syncp);
1538 static int seg6_local_input_core(struct net *net, struct sock *sk,
1539 struct sk_buff *skb)
1541 struct dst_entry *orig_dst = skb_dst(skb);
1542 struct seg6_action_desc *desc;
1543 struct seg6_local_lwt *slwt;
1544 unsigned int len = skb->len;
1547 slwt = seg6_local_lwtunnel(orig_dst->lwtstate);
1550 rc = desc->input(skb, slwt);
1552 if (!seg6_lwtunnel_counters_enabled(slwt))
1555 seg6_local_update_counters(slwt, len, rc);
1560 static int seg6_local_input(struct sk_buff *skb)
1562 if (skb->protocol != htons(ETH_P_IPV6)) {
1567 if (static_branch_unlikely(&nf_hooks_lwtunnel_enabled))
1568 return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_IN,
1569 dev_net(skb->dev), NULL, skb, skb->dev, NULL,
1570 seg6_local_input_core);
1572 return seg6_local_input_core(dev_net(skb->dev), NULL, skb);
1575 static const struct nla_policy seg6_local_policy[SEG6_LOCAL_MAX + 1] = {
1576 [SEG6_LOCAL_ACTION] = { .type = NLA_U32 },
1577 [SEG6_LOCAL_SRH] = { .type = NLA_BINARY },
1578 [SEG6_LOCAL_TABLE] = { .type = NLA_U32 },
1579 [SEG6_LOCAL_VRFTABLE] = { .type = NLA_U32 },
1580 [SEG6_LOCAL_NH4] = { .type = NLA_BINARY,
1581 .len = sizeof(struct in_addr) },
1582 [SEG6_LOCAL_NH6] = { .type = NLA_BINARY,
1583 .len = sizeof(struct in6_addr) },
1584 [SEG6_LOCAL_IIF] = { .type = NLA_U32 },
1585 [SEG6_LOCAL_OIF] = { .type = NLA_U32 },
1586 [SEG6_LOCAL_BPF] = { .type = NLA_NESTED },
1587 [SEG6_LOCAL_COUNTERS] = { .type = NLA_NESTED },
1588 [SEG6_LOCAL_FLAVORS] = { .type = NLA_NESTED },
1591 static int parse_nla_srh(struct nlattr **attrs, struct seg6_local_lwt *slwt,
1592 struct netlink_ext_ack *extack)
1594 struct ipv6_sr_hdr *srh;
1597 srh = nla_data(attrs[SEG6_LOCAL_SRH]);
1598 len = nla_len(attrs[SEG6_LOCAL_SRH]);
1600 /* SRH must contain at least one segment */
1601 if (len < sizeof(*srh) + sizeof(struct in6_addr))
1604 if (!seg6_validate_srh(srh, len, false))
1607 slwt->srh = kmemdup(srh, len, GFP_KERNEL);
1611 slwt->headroom += len;
1616 static int put_nla_srh(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1618 struct ipv6_sr_hdr *srh;
1623 len = (srh->hdrlen + 1) << 3;
1625 nla = nla_reserve(skb, SEG6_LOCAL_SRH, len);
1629 memcpy(nla_data(nla), srh, len);
1634 static int cmp_nla_srh(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1636 int len = (a->srh->hdrlen + 1) << 3;
1638 if (len != ((b->srh->hdrlen + 1) << 3))
1641 return memcmp(a->srh, b->srh, len);
1644 static void destroy_attr_srh(struct seg6_local_lwt *slwt)
1649 static int parse_nla_table(struct nlattr **attrs, struct seg6_local_lwt *slwt,
1650 struct netlink_ext_ack *extack)
1652 slwt->table = nla_get_u32(attrs[SEG6_LOCAL_TABLE]);
1657 static int put_nla_table(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1659 if (nla_put_u32(skb, SEG6_LOCAL_TABLE, slwt->table))
1665 static int cmp_nla_table(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1667 if (a->table != b->table)
1674 seg6_end_dt_info *seg6_possible_end_dt_info(struct seg6_local_lwt *slwt)
1676 #ifdef CONFIG_NET_L3_MASTER_DEV
1677 return &slwt->dt_info;
1679 return ERR_PTR(-EOPNOTSUPP);
1683 static int parse_nla_vrftable(struct nlattr **attrs,
1684 struct seg6_local_lwt *slwt,
1685 struct netlink_ext_ack *extack)
1687 struct seg6_end_dt_info *info = seg6_possible_end_dt_info(slwt);
1690 return PTR_ERR(info);
1692 info->vrf_table = nla_get_u32(attrs[SEG6_LOCAL_VRFTABLE]);
1697 static int put_nla_vrftable(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1699 struct seg6_end_dt_info *info = seg6_possible_end_dt_info(slwt);
1702 return PTR_ERR(info);
1704 if (nla_put_u32(skb, SEG6_LOCAL_VRFTABLE, info->vrf_table))
1710 static int cmp_nla_vrftable(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1712 struct seg6_end_dt_info *info_a = seg6_possible_end_dt_info(a);
1713 struct seg6_end_dt_info *info_b = seg6_possible_end_dt_info(b);
1715 if (info_a->vrf_table != info_b->vrf_table)
1721 static int parse_nla_nh4(struct nlattr **attrs, struct seg6_local_lwt *slwt,
1722 struct netlink_ext_ack *extack)
1724 memcpy(&slwt->nh4, nla_data(attrs[SEG6_LOCAL_NH4]),
1725 sizeof(struct in_addr));
1730 static int put_nla_nh4(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1734 nla = nla_reserve(skb, SEG6_LOCAL_NH4, sizeof(struct in_addr));
1738 memcpy(nla_data(nla), &slwt->nh4, sizeof(struct in_addr));
1743 static int cmp_nla_nh4(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1745 return memcmp(&a->nh4, &b->nh4, sizeof(struct in_addr));
1748 static int parse_nla_nh6(struct nlattr **attrs, struct seg6_local_lwt *slwt,
1749 struct netlink_ext_ack *extack)
1751 memcpy(&slwt->nh6, nla_data(attrs[SEG6_LOCAL_NH6]),
1752 sizeof(struct in6_addr));
1757 static int put_nla_nh6(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1761 nla = nla_reserve(skb, SEG6_LOCAL_NH6, sizeof(struct in6_addr));
1765 memcpy(nla_data(nla), &slwt->nh6, sizeof(struct in6_addr));
1770 static int cmp_nla_nh6(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1772 return memcmp(&a->nh6, &b->nh6, sizeof(struct in6_addr));
1775 static int parse_nla_iif(struct nlattr **attrs, struct seg6_local_lwt *slwt,
1776 struct netlink_ext_ack *extack)
1778 slwt->iif = nla_get_u32(attrs[SEG6_LOCAL_IIF]);
1783 static int put_nla_iif(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1785 if (nla_put_u32(skb, SEG6_LOCAL_IIF, slwt->iif))
1791 static int cmp_nla_iif(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1793 if (a->iif != b->iif)
1799 static int parse_nla_oif(struct nlattr **attrs, struct seg6_local_lwt *slwt,
1800 struct netlink_ext_ack *extack)
1802 slwt->oif = nla_get_u32(attrs[SEG6_LOCAL_OIF]);
1807 static int put_nla_oif(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1809 if (nla_put_u32(skb, SEG6_LOCAL_OIF, slwt->oif))
1815 static int cmp_nla_oif(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1817 if (a->oif != b->oif)
1823 #define MAX_PROG_NAME 256
1824 static const struct nla_policy bpf_prog_policy[SEG6_LOCAL_BPF_PROG_MAX + 1] = {
1825 [SEG6_LOCAL_BPF_PROG] = { .type = NLA_U32, },
1826 [SEG6_LOCAL_BPF_PROG_NAME] = { .type = NLA_NUL_STRING,
1827 .len = MAX_PROG_NAME },
1830 static int parse_nla_bpf(struct nlattr **attrs, struct seg6_local_lwt *slwt,
1831 struct netlink_ext_ack *extack)
1833 struct nlattr *tb[SEG6_LOCAL_BPF_PROG_MAX + 1];
1838 ret = nla_parse_nested_deprecated(tb, SEG6_LOCAL_BPF_PROG_MAX,
1839 attrs[SEG6_LOCAL_BPF],
1840 bpf_prog_policy, NULL);
1844 if (!tb[SEG6_LOCAL_BPF_PROG] || !tb[SEG6_LOCAL_BPF_PROG_NAME])
1847 slwt->bpf.name = nla_memdup(tb[SEG6_LOCAL_BPF_PROG_NAME], GFP_KERNEL);
1848 if (!slwt->bpf.name)
1851 fd = nla_get_u32(tb[SEG6_LOCAL_BPF_PROG]);
1852 p = bpf_prog_get_type(fd, BPF_PROG_TYPE_LWT_SEG6LOCAL);
1854 kfree(slwt->bpf.name);
1862 static int put_nla_bpf(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1864 struct nlattr *nest;
1866 if (!slwt->bpf.prog)
1869 nest = nla_nest_start_noflag(skb, SEG6_LOCAL_BPF);
1873 if (nla_put_u32(skb, SEG6_LOCAL_BPF_PROG, slwt->bpf.prog->aux->id))
1876 if (slwt->bpf.name &&
1877 nla_put_string(skb, SEG6_LOCAL_BPF_PROG_NAME, slwt->bpf.name))
1880 return nla_nest_end(skb, nest);
1883 static int cmp_nla_bpf(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1885 if (!a->bpf.name && !b->bpf.name)
1888 if (!a->bpf.name || !b->bpf.name)
1891 return strcmp(a->bpf.name, b->bpf.name);
1894 static void destroy_attr_bpf(struct seg6_local_lwt *slwt)
1896 kfree(slwt->bpf.name);
1898 bpf_prog_put(slwt->bpf.prog);
1902 nla_policy seg6_local_counters_policy[SEG6_LOCAL_CNT_MAX + 1] = {
1903 [SEG6_LOCAL_CNT_PACKETS] = { .type = NLA_U64 },
1904 [SEG6_LOCAL_CNT_BYTES] = { .type = NLA_U64 },
1905 [SEG6_LOCAL_CNT_ERRORS] = { .type = NLA_U64 },
1908 static int parse_nla_counters(struct nlattr **attrs,
1909 struct seg6_local_lwt *slwt,
1910 struct netlink_ext_ack *extack)
1912 struct pcpu_seg6_local_counters __percpu *pcounters;
1913 struct nlattr *tb[SEG6_LOCAL_CNT_MAX + 1];
1916 ret = nla_parse_nested_deprecated(tb, SEG6_LOCAL_CNT_MAX,
1917 attrs[SEG6_LOCAL_COUNTERS],
1918 seg6_local_counters_policy, NULL);
1922 /* basic support for SRv6 Behavior counters requires at least:
1923 * packets, bytes and errors.
1925 if (!tb[SEG6_LOCAL_CNT_PACKETS] || !tb[SEG6_LOCAL_CNT_BYTES] ||
1926 !tb[SEG6_LOCAL_CNT_ERRORS])
1929 /* counters are always zero initialized */
1930 pcounters = seg6_local_alloc_pcpu_counters(GFP_KERNEL);
1934 slwt->pcpu_counters = pcounters;
1939 static int seg6_local_fill_nla_counters(struct sk_buff *skb,
1940 struct seg6_local_counters *counters)
1942 if (nla_put_u64_64bit(skb, SEG6_LOCAL_CNT_PACKETS, counters->packets,
1943 SEG6_LOCAL_CNT_PAD))
1946 if (nla_put_u64_64bit(skb, SEG6_LOCAL_CNT_BYTES, counters->bytes,
1947 SEG6_LOCAL_CNT_PAD))
1950 if (nla_put_u64_64bit(skb, SEG6_LOCAL_CNT_ERRORS, counters->errors,
1951 SEG6_LOCAL_CNT_PAD))
1957 static int put_nla_counters(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1959 struct seg6_local_counters counters = { 0, 0, 0 };
1960 struct nlattr *nest;
1963 nest = nla_nest_start(skb, SEG6_LOCAL_COUNTERS);
1967 for_each_possible_cpu(i) {
1968 struct pcpu_seg6_local_counters *pcounters;
1969 u64 packets, bytes, errors;
1972 pcounters = per_cpu_ptr(slwt->pcpu_counters, i);
1974 start = u64_stats_fetch_begin(&pcounters->syncp);
1976 packets = u64_stats_read(&pcounters->packets);
1977 bytes = u64_stats_read(&pcounters->bytes);
1978 errors = u64_stats_read(&pcounters->errors);
1980 } while (u64_stats_fetch_retry(&pcounters->syncp, start));
1982 counters.packets += packets;
1983 counters.bytes += bytes;
1984 counters.errors += errors;
1987 rc = seg6_local_fill_nla_counters(skb, &counters);
1989 nla_nest_cancel(skb, nest);
1993 return nla_nest_end(skb, nest);
1996 static int cmp_nla_counters(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1998 /* a and b are equal if both have pcpu_counters set or not */
1999 return (!!((unsigned long)a->pcpu_counters)) ^
2000 (!!((unsigned long)b->pcpu_counters));
2003 static void destroy_attr_counters(struct seg6_local_lwt *slwt)
2005 free_percpu(slwt->pcpu_counters);
2009 struct nla_policy seg6_local_flavors_policy[SEG6_LOCAL_FLV_MAX + 1] = {
2010 [SEG6_LOCAL_FLV_OPERATION] = { .type = NLA_U32 },
2011 [SEG6_LOCAL_FLV_LCBLOCK_BITS] = { .type = NLA_U8 },
2012 [SEG6_LOCAL_FLV_LCNODE_FN_BITS] = { .type = NLA_U8 },
2015 /* check whether the lengths of the Locator-Block and Locator-Node Function
2016 * are compatible with the dimension of a C-SID container.
2018 static int seg6_chk_next_csid_cfg(__u8 block_len, __u8 func_len)
2020 /* Locator-Block and Locator-Node Function cannot exceed 128 bits
2021 * (i.e. C-SID container lenghts).
2023 if (next_csid_chk_cntr_bits(block_len, func_len))
2026 /* Locator-Block length must be greater than zero and evenly divisible
2027 * by 8. There must be room for a Locator-Node Function, at least.
2029 if (next_csid_chk_lcblock_bits(block_len))
2032 /* Locator-Node Function length must be greater than zero and evenly
2033 * divisible by 8. There must be room for the Locator-Block.
2035 if (next_csid_chk_lcnode_fn_bits(func_len))
2041 static int seg6_parse_nla_next_csid_cfg(struct nlattr **tb,
2042 struct seg6_flavors_info *finfo,
2043 struct netlink_ext_ack *extack)
2045 __u8 func_len = SEG6_LOCAL_LCNODE_FN_DBITS;
2046 __u8 block_len = SEG6_LOCAL_LCBLOCK_DBITS;
2049 if (tb[SEG6_LOCAL_FLV_LCBLOCK_BITS])
2050 block_len = nla_get_u8(tb[SEG6_LOCAL_FLV_LCBLOCK_BITS]);
2052 if (tb[SEG6_LOCAL_FLV_LCNODE_FN_BITS])
2053 func_len = nla_get_u8(tb[SEG6_LOCAL_FLV_LCNODE_FN_BITS]);
2055 rc = seg6_chk_next_csid_cfg(block_len, func_len);
2057 NL_SET_ERR_MSG(extack,
2058 "Invalid Locator Block/Node Function lengths");
2062 finfo->lcblock_bits = block_len;
2063 finfo->lcnode_func_bits = func_len;
2068 static int parse_nla_flavors(struct nlattr **attrs, struct seg6_local_lwt *slwt,
2069 struct netlink_ext_ack *extack)
2071 struct seg6_flavors_info *finfo = &slwt->flv_info;
2072 struct nlattr *tb[SEG6_LOCAL_FLV_MAX + 1];
2076 rc = nla_parse_nested_deprecated(tb, SEG6_LOCAL_FLV_MAX,
2077 attrs[SEG6_LOCAL_FLAVORS],
2078 seg6_local_flavors_policy, NULL);
2082 /* this attribute MUST always be present since it represents the Flavor
2083 * operation(s) to be carried out.
2085 if (!tb[SEG6_LOCAL_FLV_OPERATION])
2088 fops = nla_get_u32(tb[SEG6_LOCAL_FLV_OPERATION]);
2089 if (fops & ~SEG6_LOCAL_FLV_SUPP_OPS) {
2090 NL_SET_ERR_MSG(extack, "Unsupported Flavor operation(s)");
2094 finfo->flv_ops = fops;
2096 if (seg6_next_csid_enabled(fops)) {
2097 /* Locator-Block and Locator-Node Function lengths can be
2098 * provided by the user space. Otherwise, default values are
2101 rc = seg6_parse_nla_next_csid_cfg(tb, finfo, extack);
2109 static int seg6_fill_nla_next_csid_cfg(struct sk_buff *skb,
2110 struct seg6_flavors_info *finfo)
2112 if (nla_put_u8(skb, SEG6_LOCAL_FLV_LCBLOCK_BITS, finfo->lcblock_bits))
2115 if (nla_put_u8(skb, SEG6_LOCAL_FLV_LCNODE_FN_BITS,
2116 finfo->lcnode_func_bits))
2122 static int put_nla_flavors(struct sk_buff *skb, struct seg6_local_lwt *slwt)
2124 struct seg6_flavors_info *finfo = &slwt->flv_info;
2125 __u32 fops = finfo->flv_ops;
2126 struct nlattr *nest;
2129 nest = nla_nest_start(skb, SEG6_LOCAL_FLAVORS);
2133 if (nla_put_u32(skb, SEG6_LOCAL_FLV_OPERATION, fops)) {
2138 if (seg6_next_csid_enabled(fops)) {
2139 rc = seg6_fill_nla_next_csid_cfg(skb, finfo);
2144 return nla_nest_end(skb, nest);
2147 nla_nest_cancel(skb, nest);
2151 static int seg6_cmp_nla_next_csid_cfg(struct seg6_flavors_info *finfo_a,
2152 struct seg6_flavors_info *finfo_b)
2154 if (finfo_a->lcblock_bits != finfo_b->lcblock_bits)
2157 if (finfo_a->lcnode_func_bits != finfo_b->lcnode_func_bits)
2163 static int cmp_nla_flavors(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
2165 struct seg6_flavors_info *finfo_a = &a->flv_info;
2166 struct seg6_flavors_info *finfo_b = &b->flv_info;
2168 if (finfo_a->flv_ops != finfo_b->flv_ops)
2171 if (seg6_next_csid_enabled(finfo_a->flv_ops)) {
2172 if (seg6_cmp_nla_next_csid_cfg(finfo_a, finfo_b))
2179 static int encap_size_flavors(struct seg6_local_lwt *slwt)
2181 struct seg6_flavors_info *finfo = &slwt->flv_info;
2184 nlsize = nla_total_size(0) + /* nest SEG6_LOCAL_FLAVORS */
2185 nla_total_size(4); /* SEG6_LOCAL_FLV_OPERATION */
2187 if (seg6_next_csid_enabled(finfo->flv_ops))
2188 nlsize += nla_total_size(1) + /* SEG6_LOCAL_FLV_LCBLOCK_BITS */
2189 nla_total_size(1); /* SEG6_LOCAL_FLV_LCNODE_FN_BITS */
2194 struct seg6_action_param {
2195 int (*parse)(struct nlattr **attrs, struct seg6_local_lwt *slwt,
2196 struct netlink_ext_ack *extack);
2197 int (*put)(struct sk_buff *skb, struct seg6_local_lwt *slwt);
2198 int (*cmp)(struct seg6_local_lwt *a, struct seg6_local_lwt *b);
2200 /* optional destroy() callback useful for releasing resources which
2201 * have been previously acquired in the corresponding parse()
2204 void (*destroy)(struct seg6_local_lwt *slwt);
2207 static struct seg6_action_param seg6_action_params[SEG6_LOCAL_MAX + 1] = {
2208 [SEG6_LOCAL_SRH] = { .parse = parse_nla_srh,
2211 .destroy = destroy_attr_srh },
2213 [SEG6_LOCAL_TABLE] = { .parse = parse_nla_table,
2214 .put = put_nla_table,
2215 .cmp = cmp_nla_table },
2217 [SEG6_LOCAL_NH4] = { .parse = parse_nla_nh4,
2219 .cmp = cmp_nla_nh4 },
2221 [SEG6_LOCAL_NH6] = { .parse = parse_nla_nh6,
2223 .cmp = cmp_nla_nh6 },
2225 [SEG6_LOCAL_IIF] = { .parse = parse_nla_iif,
2227 .cmp = cmp_nla_iif },
2229 [SEG6_LOCAL_OIF] = { .parse = parse_nla_oif,
2231 .cmp = cmp_nla_oif },
2233 [SEG6_LOCAL_BPF] = { .parse = parse_nla_bpf,
2236 .destroy = destroy_attr_bpf },
2238 [SEG6_LOCAL_VRFTABLE] = { .parse = parse_nla_vrftable,
2239 .put = put_nla_vrftable,
2240 .cmp = cmp_nla_vrftable },
2242 [SEG6_LOCAL_COUNTERS] = { .parse = parse_nla_counters,
2243 .put = put_nla_counters,
2244 .cmp = cmp_nla_counters,
2245 .destroy = destroy_attr_counters },
2247 [SEG6_LOCAL_FLAVORS] = { .parse = parse_nla_flavors,
2248 .put = put_nla_flavors,
2249 .cmp = cmp_nla_flavors },
2252 /* call the destroy() callback (if available) for each set attribute in
2253 * @parsed_attrs, starting from the first attribute up to the @max_parsed
2254 * (excluded) attribute.
2256 static void __destroy_attrs(unsigned long parsed_attrs, int max_parsed,
2257 struct seg6_local_lwt *slwt)
2259 struct seg6_action_param *param;
2262 /* Every required seg6local attribute is identified by an ID which is
2263 * encoded as a flag (i.e: 1 << ID) in the 'attrs' bitmask;
2265 * We scan the 'parsed_attrs' bitmask, starting from the first attribute
2266 * up to the @max_parsed (excluded) attribute.
2267 * For each set attribute, we retrieve the corresponding destroy()
2268 * callback. If the callback is not available, then we skip to the next
2269 * attribute; otherwise, we call the destroy() callback.
2271 for (i = SEG6_LOCAL_SRH; i < max_parsed; ++i) {
2272 if (!(parsed_attrs & SEG6_F_ATTR(i)))
2275 param = &seg6_action_params[i];
2278 param->destroy(slwt);
2282 /* release all the resources that may have been acquired during parsing
2285 static void destroy_attrs(struct seg6_local_lwt *slwt)
2287 unsigned long attrs = slwt->desc->attrs | slwt->parsed_optattrs;
2289 __destroy_attrs(attrs, SEG6_LOCAL_MAX + 1, slwt);
2292 static int parse_nla_optional_attrs(struct nlattr **attrs,
2293 struct seg6_local_lwt *slwt,
2294 struct netlink_ext_ack *extack)
2296 struct seg6_action_desc *desc = slwt->desc;
2297 unsigned long parsed_optattrs = 0;
2298 struct seg6_action_param *param;
2301 for (i = SEG6_LOCAL_SRH; i < SEG6_LOCAL_MAX + 1; ++i) {
2302 if (!(desc->optattrs & SEG6_F_ATTR(i)) || !attrs[i])
2305 /* once here, the i-th attribute is provided by the
2306 * userspace AND it is identified optional as well.
2308 param = &seg6_action_params[i];
2310 err = param->parse(attrs, slwt, extack);
2312 goto parse_optattrs_err;
2314 /* current attribute has been correctly parsed */
2315 parsed_optattrs |= SEG6_F_ATTR(i);
2318 /* store in the tunnel state all the optional attributed successfully
2321 slwt->parsed_optattrs = parsed_optattrs;
2326 __destroy_attrs(parsed_optattrs, i, slwt);
2331 /* call the custom constructor of the behavior during its initialization phase
2332 * and after that all its attributes have been parsed successfully.
2335 seg6_local_lwtunnel_build_state(struct seg6_local_lwt *slwt, const void *cfg,
2336 struct netlink_ext_ack *extack)
2338 struct seg6_action_desc *desc = slwt->desc;
2339 struct seg6_local_lwtunnel_ops *ops;
2341 ops = &desc->slwt_ops;
2342 if (!ops->build_state)
2345 return ops->build_state(slwt, cfg, extack);
2348 /* call the custom destructor of the behavior which is invoked before the
2349 * tunnel is going to be destroyed.
2351 static void seg6_local_lwtunnel_destroy_state(struct seg6_local_lwt *slwt)
2353 struct seg6_action_desc *desc = slwt->desc;
2354 struct seg6_local_lwtunnel_ops *ops;
2356 ops = &desc->slwt_ops;
2357 if (!ops->destroy_state)
2360 ops->destroy_state(slwt);
2363 static int parse_nla_action(struct nlattr **attrs, struct seg6_local_lwt *slwt,
2364 struct netlink_ext_ack *extack)
2366 struct seg6_action_param *param;
2367 struct seg6_action_desc *desc;
2368 unsigned long invalid_attrs;
2371 desc = __get_action_desc(slwt->action);
2379 slwt->headroom += desc->static_headroom;
2381 /* Forcing the desc->optattrs *set* and the desc->attrs *set* to be
2382 * disjoined, this allow us to release acquired resources by optional
2383 * attributes and by required attributes independently from each other
2384 * without any interference.
2385 * In other terms, we are sure that we do not release some the acquired
2388 * Note that if an attribute is configured both as required and as
2389 * optional, it means that the user has messed something up in the
2390 * seg6_action_table. Therefore, this check is required for SRv6
2391 * behaviors to work properly.
2393 invalid_attrs = desc->attrs & desc->optattrs;
2394 if (invalid_attrs) {
2396 "An attribute cannot be both required AND optional");
2400 /* parse the required attributes */
2401 for (i = SEG6_LOCAL_SRH; i < SEG6_LOCAL_MAX + 1; i++) {
2402 if (desc->attrs & SEG6_F_ATTR(i)) {
2406 param = &seg6_action_params[i];
2408 err = param->parse(attrs, slwt, extack);
2410 goto parse_attrs_err;
2414 /* parse the optional attributes, if any */
2415 err = parse_nla_optional_attrs(attrs, slwt, extack);
2417 goto parse_attrs_err;
2422 /* release any resource that may have been acquired during the i-1
2423 * parse() operations.
2425 __destroy_attrs(desc->attrs, i, slwt);
2430 static int seg6_local_build_state(struct net *net, struct nlattr *nla,
2431 unsigned int family, const void *cfg,
2432 struct lwtunnel_state **ts,
2433 struct netlink_ext_ack *extack)
2435 struct nlattr *tb[SEG6_LOCAL_MAX + 1];
2436 struct lwtunnel_state *newts;
2437 struct seg6_local_lwt *slwt;
2440 if (family != AF_INET6)
2443 err = nla_parse_nested_deprecated(tb, SEG6_LOCAL_MAX, nla,
2444 seg6_local_policy, extack);
2449 if (!tb[SEG6_LOCAL_ACTION])
2452 newts = lwtunnel_state_alloc(sizeof(*slwt));
2456 slwt = seg6_local_lwtunnel(newts);
2457 slwt->action = nla_get_u32(tb[SEG6_LOCAL_ACTION]);
2459 err = parse_nla_action(tb, slwt, extack);
2463 err = seg6_local_lwtunnel_build_state(slwt, cfg, extack);
2465 goto out_destroy_attrs;
2467 newts->type = LWTUNNEL_ENCAP_SEG6_LOCAL;
2468 newts->flags = LWTUNNEL_STATE_INPUT_REDIRECT;
2469 newts->headroom = slwt->headroom;
2476 destroy_attrs(slwt);
2482 static void seg6_local_destroy_state(struct lwtunnel_state *lwt)
2484 struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
2486 seg6_local_lwtunnel_destroy_state(slwt);
2488 destroy_attrs(slwt);
2493 static int seg6_local_fill_encap(struct sk_buff *skb,
2494 struct lwtunnel_state *lwt)
2496 struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
2497 struct seg6_action_param *param;
2498 unsigned long attrs;
2501 if (nla_put_u32(skb, SEG6_LOCAL_ACTION, slwt->action))
2504 attrs = slwt->desc->attrs | slwt->parsed_optattrs;
2506 for (i = SEG6_LOCAL_SRH; i < SEG6_LOCAL_MAX + 1; i++) {
2507 if (attrs & SEG6_F_ATTR(i)) {
2508 param = &seg6_action_params[i];
2509 err = param->put(skb, slwt);
2518 static int seg6_local_get_encap_size(struct lwtunnel_state *lwt)
2520 struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
2521 unsigned long attrs;
2524 nlsize = nla_total_size(4); /* action */
2526 attrs = slwt->desc->attrs | slwt->parsed_optattrs;
2528 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_SRH))
2529 nlsize += nla_total_size((slwt->srh->hdrlen + 1) << 3);
2531 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_TABLE))
2532 nlsize += nla_total_size(4);
2534 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_NH4))
2535 nlsize += nla_total_size(4);
2537 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_NH6))
2538 nlsize += nla_total_size(16);
2540 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_IIF))
2541 nlsize += nla_total_size(4);
2543 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_OIF))
2544 nlsize += nla_total_size(4);
2546 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_BPF))
2547 nlsize += nla_total_size(sizeof(struct nlattr)) +
2548 nla_total_size(MAX_PROG_NAME) +
2551 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_VRFTABLE))
2552 nlsize += nla_total_size(4);
2554 if (attrs & SEG6_F_LOCAL_COUNTERS)
2555 nlsize += nla_total_size(0) + /* nest SEG6_LOCAL_COUNTERS */
2556 /* SEG6_LOCAL_CNT_PACKETS */
2557 nla_total_size_64bit(sizeof(__u64)) +
2558 /* SEG6_LOCAL_CNT_BYTES */
2559 nla_total_size_64bit(sizeof(__u64)) +
2560 /* SEG6_LOCAL_CNT_ERRORS */
2561 nla_total_size_64bit(sizeof(__u64));
2563 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_FLAVORS))
2564 nlsize += encap_size_flavors(slwt);
2569 static int seg6_local_cmp_encap(struct lwtunnel_state *a,
2570 struct lwtunnel_state *b)
2572 struct seg6_local_lwt *slwt_a, *slwt_b;
2573 struct seg6_action_param *param;
2574 unsigned long attrs_a, attrs_b;
2577 slwt_a = seg6_local_lwtunnel(a);
2578 slwt_b = seg6_local_lwtunnel(b);
2580 if (slwt_a->action != slwt_b->action)
2583 attrs_a = slwt_a->desc->attrs | slwt_a->parsed_optattrs;
2584 attrs_b = slwt_b->desc->attrs | slwt_b->parsed_optattrs;
2586 if (attrs_a != attrs_b)
2589 for (i = SEG6_LOCAL_SRH; i < SEG6_LOCAL_MAX + 1; i++) {
2590 if (attrs_a & SEG6_F_ATTR(i)) {
2591 param = &seg6_action_params[i];
2592 if (param->cmp(slwt_a, slwt_b))
2600 static const struct lwtunnel_encap_ops seg6_local_ops = {
2601 .build_state = seg6_local_build_state,
2602 .destroy_state = seg6_local_destroy_state,
2603 .input = seg6_local_input,
2604 .fill_encap = seg6_local_fill_encap,
2605 .get_encap_size = seg6_local_get_encap_size,
2606 .cmp_encap = seg6_local_cmp_encap,
2607 .owner = THIS_MODULE,
2610 int __init seg6_local_init(void)
2612 /* If the max total number of defined attributes is reached, then your
2613 * kernel build stops here.
2615 * This check is required to avoid arithmetic overflows when processing
2616 * behavior attributes and the maximum number of defined attributes
2617 * exceeds the allowed value.
2619 BUILD_BUG_ON(SEG6_LOCAL_MAX + 1 > BITS_PER_TYPE(unsigned long));
2621 /* If the default NEXT-C-SID Locator-Block/Node Function lengths (in
2622 * bits) have been changed with invalid values, kernel build stops
2625 BUILD_BUG_ON(next_csid_chk_cntr_bits(SEG6_LOCAL_LCBLOCK_DBITS,
2626 SEG6_LOCAL_LCNODE_FN_DBITS));
2627 BUILD_BUG_ON(next_csid_chk_lcblock_bits(SEG6_LOCAL_LCBLOCK_DBITS));
2628 BUILD_BUG_ON(next_csid_chk_lcnode_fn_bits(SEG6_LOCAL_LCNODE_FN_DBITS));
2630 /* To be memory efficient, we use 'u8' to represent the different
2631 * actions related to RFC8986 flavors. If the kernel build stops here,
2632 * it means that it is not possible to correctly encode these actions
2633 * with the data type chosen for the action table.
2635 BUILD_BUG_ON(SEG6_LOCAL_FLV_ACT_MAX > (typeof(flv8986_act_tbl[0]))~0U);
2637 return lwtunnel_encap_add_ops(&seg6_local_ops,
2638 LWTUNNEL_ENCAP_SEG6_LOCAL);
2641 void seg6_local_exit(void)
2643 lwtunnel_encap_del_ops(&seg6_local_ops, LWTUNNEL_ENCAP_SEG6_LOCAL);