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 #ifdef CONFIG_IPV6_SEG6_HMAC
27 #include <net/seg6_hmac.h>
29 #include <net/seg6_local.h>
30 #include <linux/etherdevice.h>
31 #include <linux/bpf.h>
33 struct seg6_local_lwt;
35 struct seg6_action_desc {
38 int (*input)(struct sk_buff *skb, struct seg6_local_lwt *slwt);
43 struct bpf_prog *prog;
47 struct seg6_local_lwt {
49 struct ipv6_sr_hdr *srh;
55 struct bpf_lwt_prog bpf;
58 struct seg6_action_desc *desc;
61 static struct seg6_local_lwt *seg6_local_lwtunnel(struct lwtunnel_state *lwt)
63 return (struct seg6_local_lwt *)lwt->data;
66 static struct ipv6_sr_hdr *get_srh(struct sk_buff *skb)
68 struct ipv6_sr_hdr *srh;
71 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
74 if (!pskb_may_pull(skb, srhoff + sizeof(*srh)))
77 srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
79 len = (srh->hdrlen + 1) << 3;
81 if (!pskb_may_pull(skb, srhoff + len))
84 /* note that pskb_may_pull may change pointers in header;
85 * for this reason it is necessary to reload them when needed.
87 srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
89 if (!seg6_validate_srh(srh, len))
95 static struct ipv6_sr_hdr *get_and_validate_srh(struct sk_buff *skb)
97 struct ipv6_sr_hdr *srh;
103 if (srh->segments_left == 0)
106 #ifdef CONFIG_IPV6_SEG6_HMAC
107 if (!seg6_hmac_validate_skb(skb))
114 static bool decap_and_validate(struct sk_buff *skb, int proto)
116 struct ipv6_sr_hdr *srh;
117 unsigned int off = 0;
120 if (srh && srh->segments_left > 0)
123 #ifdef CONFIG_IPV6_SEG6_HMAC
124 if (srh && !seg6_hmac_validate_skb(skb))
128 if (ipv6_find_hdr(skb, &off, proto, NULL, NULL) < 0)
131 if (!pskb_pull(skb, off))
134 skb_postpull_rcsum(skb, skb_network_header(skb), off);
136 skb_reset_network_header(skb);
137 skb_reset_transport_header(skb);
138 skb->encapsulation = 0;
143 static void advance_nextseg(struct ipv6_sr_hdr *srh, struct in6_addr *daddr)
145 struct in6_addr *addr;
147 srh->segments_left--;
148 addr = srh->segments + srh->segments_left;
152 int seg6_lookup_nexthop(struct sk_buff *skb, struct in6_addr *nhaddr,
155 struct net *net = dev_net(skb->dev);
156 struct ipv6hdr *hdr = ipv6_hdr(skb);
157 int flags = RT6_LOOKUP_F_HAS_SADDR;
158 struct dst_entry *dst = NULL;
162 fl6.flowi6_iif = skb->dev->ifindex;
163 fl6.daddr = nhaddr ? *nhaddr : hdr->daddr;
164 fl6.saddr = hdr->saddr;
165 fl6.flowlabel = ip6_flowinfo(hdr);
166 fl6.flowi6_mark = skb->mark;
167 fl6.flowi6_proto = hdr->nexthdr;
170 fl6.flowi6_flags = FLOWI_FLAG_KNOWN_NH;
173 dst = ip6_route_input_lookup(net, skb->dev, &fl6, skb, flags);
175 struct fib6_table *table;
177 table = fib6_get_table(net, tbl_id);
181 rt = ip6_pol_route(net, table, 0, &fl6, skb, flags);
185 if (dst && dst->dev->flags & IFF_LOOPBACK && !dst->error) {
192 rt = net->ipv6.ip6_blk_hole_entry;
198 skb_dst_set(skb, dst);
202 /* regular endpoint function */
203 static int input_action_end(struct sk_buff *skb, struct seg6_local_lwt *slwt)
205 struct ipv6_sr_hdr *srh;
207 srh = get_and_validate_srh(skb);
211 advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
213 seg6_lookup_nexthop(skb, NULL, 0);
215 return dst_input(skb);
222 /* regular endpoint, and forward to specified nexthop */
223 static int input_action_end_x(struct sk_buff *skb, struct seg6_local_lwt *slwt)
225 struct ipv6_sr_hdr *srh;
227 srh = get_and_validate_srh(skb);
231 advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
233 seg6_lookup_nexthop(skb, &slwt->nh6, 0);
235 return dst_input(skb);
242 static int input_action_end_t(struct sk_buff *skb, struct seg6_local_lwt *slwt)
244 struct ipv6_sr_hdr *srh;
246 srh = get_and_validate_srh(skb);
250 advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
252 seg6_lookup_nexthop(skb, NULL, slwt->table);
254 return dst_input(skb);
261 /* decapsulate and forward inner L2 frame on specified interface */
262 static int input_action_end_dx2(struct sk_buff *skb,
263 struct seg6_local_lwt *slwt)
265 struct net *net = dev_net(skb->dev);
266 struct net_device *odev;
269 if (!decap_and_validate(skb, NEXTHDR_NONE))
272 if (!pskb_may_pull(skb, ETH_HLEN))
275 skb_reset_mac_header(skb);
276 eth = (struct ethhdr *)skb->data;
278 /* To determine the frame's protocol, we assume it is 802.3. This avoids
279 * a call to eth_type_trans(), which is not really relevant for our
282 if (!eth_proto_is_802_3(eth->h_proto))
285 odev = dev_get_by_index_rcu(net, slwt->oif);
289 /* As we accept Ethernet frames, make sure the egress device is of
292 if (odev->type != ARPHRD_ETHER)
295 if (!(odev->flags & IFF_UP) || !netif_carrier_ok(odev))
300 if (skb_warn_if_lro(skb))
303 skb_forward_csum(skb);
305 if (skb->len - ETH_HLEN > odev->mtu)
309 skb->protocol = eth->h_proto;
311 return dev_queue_xmit(skb);
318 /* decapsulate and forward to specified nexthop */
319 static int input_action_end_dx6(struct sk_buff *skb,
320 struct seg6_local_lwt *slwt)
322 struct in6_addr *nhaddr = NULL;
324 /* this function accepts IPv6 encapsulated packets, with either
325 * an SRH with SL=0, or no SRH.
328 if (!decap_and_validate(skb, IPPROTO_IPV6))
331 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
334 /* The inner packet is not associated to any local interface,
335 * so we do not call netif_rx().
337 * If slwt->nh6 is set to ::, then lookup the nexthop for the
338 * inner packet's DA. Otherwise, use the specified nexthop.
341 if (!ipv6_addr_any(&slwt->nh6))
344 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
346 seg6_lookup_nexthop(skb, nhaddr, 0);
348 return dst_input(skb);
354 static int input_action_end_dx4(struct sk_buff *skb,
355 struct seg6_local_lwt *slwt)
361 if (!decap_and_validate(skb, IPPROTO_IPIP))
364 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
367 skb->protocol = htons(ETH_P_IP);
371 nhaddr = slwt->nh4.s_addr ?: iph->daddr;
375 skb_set_transport_header(skb, sizeof(struct iphdr));
377 err = ip_route_input(skb, nhaddr, iph->saddr, 0, skb->dev);
381 return dst_input(skb);
388 static int input_action_end_dt6(struct sk_buff *skb,
389 struct seg6_local_lwt *slwt)
391 if (!decap_and_validate(skb, IPPROTO_IPV6))
394 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
397 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
399 seg6_lookup_nexthop(skb, NULL, slwt->table);
401 return dst_input(skb);
408 /* push an SRH on top of the current one */
409 static int input_action_end_b6(struct sk_buff *skb, struct seg6_local_lwt *slwt)
411 struct ipv6_sr_hdr *srh;
414 srh = get_and_validate_srh(skb);
418 err = seg6_do_srh_inline(skb, slwt->srh);
422 ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
423 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
425 seg6_lookup_nexthop(skb, NULL, 0);
427 return dst_input(skb);
434 /* encapsulate within an outer IPv6 header and a specified SRH */
435 static int input_action_end_b6_encap(struct sk_buff *skb,
436 struct seg6_local_lwt *slwt)
438 struct ipv6_sr_hdr *srh;
441 srh = get_and_validate_srh(skb);
445 advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
447 skb_reset_inner_headers(skb);
448 skb->encapsulation = 1;
450 err = seg6_do_srh_encap(skb, slwt->srh, IPPROTO_IPV6);
454 ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
455 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
457 seg6_lookup_nexthop(skb, NULL, 0);
459 return dst_input(skb);
466 DEFINE_PER_CPU(struct seg6_bpf_srh_state, seg6_bpf_srh_states);
468 bool seg6_bpf_has_valid_srh(struct sk_buff *skb)
470 struct seg6_bpf_srh_state *srh_state =
471 this_cpu_ptr(&seg6_bpf_srh_states);
472 struct ipv6_sr_hdr *srh = srh_state->srh;
474 if (unlikely(srh == NULL))
477 if (unlikely(!srh_state->valid)) {
478 if ((srh_state->hdrlen & 7) != 0)
481 srh->hdrlen = (u8)(srh_state->hdrlen >> 3);
482 if (!seg6_validate_srh(srh, (srh->hdrlen + 1) << 3))
485 srh_state->valid = true;
491 static int input_action_end_bpf(struct sk_buff *skb,
492 struct seg6_local_lwt *slwt)
494 struct seg6_bpf_srh_state *srh_state =
495 this_cpu_ptr(&seg6_bpf_srh_states);
496 struct ipv6_sr_hdr *srh;
499 srh = get_and_validate_srh(skb);
504 advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
506 /* preempt_disable is needed to protect the per-CPU buffer srh_state,
507 * which is also accessed by the bpf_lwt_seg6_* helpers
510 srh_state->srh = srh;
511 srh_state->hdrlen = srh->hdrlen << 3;
512 srh_state->valid = true;
515 bpf_compute_data_pointers(skb);
516 ret = bpf_prog_run_save_cb(slwt->bpf.prog, skb);
526 pr_warn_once("bpf-seg6local: Illegal return value %u\n", ret);
530 if (srh_state->srh && !seg6_bpf_has_valid_srh(skb))
534 if (ret != BPF_REDIRECT)
535 seg6_lookup_nexthop(skb, NULL, 0);
537 return dst_input(skb);
545 static struct seg6_action_desc seg6_action_table[] = {
547 .action = SEG6_LOCAL_ACTION_END,
549 .input = input_action_end,
552 .action = SEG6_LOCAL_ACTION_END_X,
553 .attrs = (1 << SEG6_LOCAL_NH6),
554 .input = input_action_end_x,
557 .action = SEG6_LOCAL_ACTION_END_T,
558 .attrs = (1 << SEG6_LOCAL_TABLE),
559 .input = input_action_end_t,
562 .action = SEG6_LOCAL_ACTION_END_DX2,
563 .attrs = (1 << SEG6_LOCAL_OIF),
564 .input = input_action_end_dx2,
567 .action = SEG6_LOCAL_ACTION_END_DX6,
568 .attrs = (1 << SEG6_LOCAL_NH6),
569 .input = input_action_end_dx6,
572 .action = SEG6_LOCAL_ACTION_END_DX4,
573 .attrs = (1 << SEG6_LOCAL_NH4),
574 .input = input_action_end_dx4,
577 .action = SEG6_LOCAL_ACTION_END_DT6,
578 .attrs = (1 << SEG6_LOCAL_TABLE),
579 .input = input_action_end_dt6,
582 .action = SEG6_LOCAL_ACTION_END_B6,
583 .attrs = (1 << SEG6_LOCAL_SRH),
584 .input = input_action_end_b6,
587 .action = SEG6_LOCAL_ACTION_END_B6_ENCAP,
588 .attrs = (1 << SEG6_LOCAL_SRH),
589 .input = input_action_end_b6_encap,
590 .static_headroom = sizeof(struct ipv6hdr),
593 .action = SEG6_LOCAL_ACTION_END_BPF,
594 .attrs = (1 << SEG6_LOCAL_BPF),
595 .input = input_action_end_bpf,
600 static struct seg6_action_desc *__get_action_desc(int action)
602 struct seg6_action_desc *desc;
605 count = ARRAY_SIZE(seg6_action_table);
606 for (i = 0; i < count; i++) {
607 desc = &seg6_action_table[i];
608 if (desc->action == action)
615 static int seg6_local_input(struct sk_buff *skb)
617 struct dst_entry *orig_dst = skb_dst(skb);
618 struct seg6_action_desc *desc;
619 struct seg6_local_lwt *slwt;
621 if (skb->protocol != htons(ETH_P_IPV6)) {
626 slwt = seg6_local_lwtunnel(orig_dst->lwtstate);
629 return desc->input(skb, slwt);
632 static const struct nla_policy seg6_local_policy[SEG6_LOCAL_MAX + 1] = {
633 [SEG6_LOCAL_ACTION] = { .type = NLA_U32 },
634 [SEG6_LOCAL_SRH] = { .type = NLA_BINARY },
635 [SEG6_LOCAL_TABLE] = { .type = NLA_U32 },
636 [SEG6_LOCAL_NH4] = { .type = NLA_BINARY,
637 .len = sizeof(struct in_addr) },
638 [SEG6_LOCAL_NH6] = { .type = NLA_BINARY,
639 .len = sizeof(struct in6_addr) },
640 [SEG6_LOCAL_IIF] = { .type = NLA_U32 },
641 [SEG6_LOCAL_OIF] = { .type = NLA_U32 },
642 [SEG6_LOCAL_BPF] = { .type = NLA_NESTED },
645 static int parse_nla_srh(struct nlattr **attrs, struct seg6_local_lwt *slwt)
647 struct ipv6_sr_hdr *srh;
650 srh = nla_data(attrs[SEG6_LOCAL_SRH]);
651 len = nla_len(attrs[SEG6_LOCAL_SRH]);
653 /* SRH must contain at least one segment */
654 if (len < sizeof(*srh) + sizeof(struct in6_addr))
657 if (!seg6_validate_srh(srh, len))
660 slwt->srh = kmemdup(srh, len, GFP_KERNEL);
664 slwt->headroom += len;
669 static int put_nla_srh(struct sk_buff *skb, struct seg6_local_lwt *slwt)
671 struct ipv6_sr_hdr *srh;
676 len = (srh->hdrlen + 1) << 3;
678 nla = nla_reserve(skb, SEG6_LOCAL_SRH, len);
682 memcpy(nla_data(nla), srh, len);
687 static int cmp_nla_srh(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
689 int len = (a->srh->hdrlen + 1) << 3;
691 if (len != ((b->srh->hdrlen + 1) << 3))
694 return memcmp(a->srh, b->srh, len);
697 static int parse_nla_table(struct nlattr **attrs, struct seg6_local_lwt *slwt)
699 slwt->table = nla_get_u32(attrs[SEG6_LOCAL_TABLE]);
704 static int put_nla_table(struct sk_buff *skb, struct seg6_local_lwt *slwt)
706 if (nla_put_u32(skb, SEG6_LOCAL_TABLE, slwt->table))
712 static int cmp_nla_table(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
714 if (a->table != b->table)
720 static int parse_nla_nh4(struct nlattr **attrs, struct seg6_local_lwt *slwt)
722 memcpy(&slwt->nh4, nla_data(attrs[SEG6_LOCAL_NH4]),
723 sizeof(struct in_addr));
728 static int put_nla_nh4(struct sk_buff *skb, struct seg6_local_lwt *slwt)
732 nla = nla_reserve(skb, SEG6_LOCAL_NH4, sizeof(struct in_addr));
736 memcpy(nla_data(nla), &slwt->nh4, sizeof(struct in_addr));
741 static int cmp_nla_nh4(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
743 return memcmp(&a->nh4, &b->nh4, sizeof(struct in_addr));
746 static int parse_nla_nh6(struct nlattr **attrs, struct seg6_local_lwt *slwt)
748 memcpy(&slwt->nh6, nla_data(attrs[SEG6_LOCAL_NH6]),
749 sizeof(struct in6_addr));
754 static int put_nla_nh6(struct sk_buff *skb, struct seg6_local_lwt *slwt)
758 nla = nla_reserve(skb, SEG6_LOCAL_NH6, sizeof(struct in6_addr));
762 memcpy(nla_data(nla), &slwt->nh6, sizeof(struct in6_addr));
767 static int cmp_nla_nh6(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
769 return memcmp(&a->nh6, &b->nh6, sizeof(struct in6_addr));
772 static int parse_nla_iif(struct nlattr **attrs, struct seg6_local_lwt *slwt)
774 slwt->iif = nla_get_u32(attrs[SEG6_LOCAL_IIF]);
779 static int put_nla_iif(struct sk_buff *skb, struct seg6_local_lwt *slwt)
781 if (nla_put_u32(skb, SEG6_LOCAL_IIF, slwt->iif))
787 static int cmp_nla_iif(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
789 if (a->iif != b->iif)
795 static int parse_nla_oif(struct nlattr **attrs, struct seg6_local_lwt *slwt)
797 slwt->oif = nla_get_u32(attrs[SEG6_LOCAL_OIF]);
802 static int put_nla_oif(struct sk_buff *skb, struct seg6_local_lwt *slwt)
804 if (nla_put_u32(skb, SEG6_LOCAL_OIF, slwt->oif))
810 static int cmp_nla_oif(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
812 if (a->oif != b->oif)
818 #define MAX_PROG_NAME 256
819 static const struct nla_policy bpf_prog_policy[SEG6_LOCAL_BPF_PROG_MAX + 1] = {
820 [SEG6_LOCAL_BPF_PROG] = { .type = NLA_U32, },
821 [SEG6_LOCAL_BPF_PROG_NAME] = { .type = NLA_NUL_STRING,
822 .len = MAX_PROG_NAME },
825 static int parse_nla_bpf(struct nlattr **attrs, struct seg6_local_lwt *slwt)
827 struct nlattr *tb[SEG6_LOCAL_BPF_PROG_MAX + 1];
832 ret = nla_parse_nested_deprecated(tb, SEG6_LOCAL_BPF_PROG_MAX,
833 attrs[SEG6_LOCAL_BPF],
834 bpf_prog_policy, NULL);
838 if (!tb[SEG6_LOCAL_BPF_PROG] || !tb[SEG6_LOCAL_BPF_PROG_NAME])
841 slwt->bpf.name = nla_memdup(tb[SEG6_LOCAL_BPF_PROG_NAME], GFP_KERNEL);
845 fd = nla_get_u32(tb[SEG6_LOCAL_BPF_PROG]);
846 p = bpf_prog_get_type(fd, BPF_PROG_TYPE_LWT_SEG6LOCAL);
848 kfree(slwt->bpf.name);
856 static int put_nla_bpf(struct sk_buff *skb, struct seg6_local_lwt *slwt)
863 nest = nla_nest_start_noflag(skb, SEG6_LOCAL_BPF);
867 if (nla_put_u32(skb, SEG6_LOCAL_BPF_PROG, slwt->bpf.prog->aux->id))
870 if (slwt->bpf.name &&
871 nla_put_string(skb, SEG6_LOCAL_BPF_PROG_NAME, slwt->bpf.name))
874 return nla_nest_end(skb, nest);
877 static int cmp_nla_bpf(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
879 if (!a->bpf.name && !b->bpf.name)
882 if (!a->bpf.name || !b->bpf.name)
885 return strcmp(a->bpf.name, b->bpf.name);
888 struct seg6_action_param {
889 int (*parse)(struct nlattr **attrs, struct seg6_local_lwt *slwt);
890 int (*put)(struct sk_buff *skb, struct seg6_local_lwt *slwt);
891 int (*cmp)(struct seg6_local_lwt *a, struct seg6_local_lwt *b);
894 static struct seg6_action_param seg6_action_params[SEG6_LOCAL_MAX + 1] = {
895 [SEG6_LOCAL_SRH] = { .parse = parse_nla_srh,
897 .cmp = cmp_nla_srh },
899 [SEG6_LOCAL_TABLE] = { .parse = parse_nla_table,
900 .put = put_nla_table,
901 .cmp = cmp_nla_table },
903 [SEG6_LOCAL_NH4] = { .parse = parse_nla_nh4,
905 .cmp = cmp_nla_nh4 },
907 [SEG6_LOCAL_NH6] = { .parse = parse_nla_nh6,
909 .cmp = cmp_nla_nh6 },
911 [SEG6_LOCAL_IIF] = { .parse = parse_nla_iif,
913 .cmp = cmp_nla_iif },
915 [SEG6_LOCAL_OIF] = { .parse = parse_nla_oif,
917 .cmp = cmp_nla_oif },
919 [SEG6_LOCAL_BPF] = { .parse = parse_nla_bpf,
921 .cmp = cmp_nla_bpf },
925 static int parse_nla_action(struct nlattr **attrs, struct seg6_local_lwt *slwt)
927 struct seg6_action_param *param;
928 struct seg6_action_desc *desc;
931 desc = __get_action_desc(slwt->action);
939 slwt->headroom += desc->static_headroom;
941 for (i = 0; i < SEG6_LOCAL_MAX + 1; i++) {
942 if (desc->attrs & (1 << i)) {
946 param = &seg6_action_params[i];
948 err = param->parse(attrs, slwt);
957 static int seg6_local_build_state(struct nlattr *nla, unsigned int family,
958 const void *cfg, struct lwtunnel_state **ts,
959 struct netlink_ext_ack *extack)
961 struct nlattr *tb[SEG6_LOCAL_MAX + 1];
962 struct lwtunnel_state *newts;
963 struct seg6_local_lwt *slwt;
966 if (family != AF_INET6)
969 err = nla_parse_nested_deprecated(tb, SEG6_LOCAL_MAX, nla,
970 seg6_local_policy, extack);
975 if (!tb[SEG6_LOCAL_ACTION])
978 newts = lwtunnel_state_alloc(sizeof(*slwt));
982 slwt = seg6_local_lwtunnel(newts);
983 slwt->action = nla_get_u32(tb[SEG6_LOCAL_ACTION]);
985 err = parse_nla_action(tb, slwt);
989 newts->type = LWTUNNEL_ENCAP_SEG6_LOCAL;
990 newts->flags = LWTUNNEL_STATE_INPUT_REDIRECT;
991 newts->headroom = slwt->headroom;
1003 static void seg6_local_destroy_state(struct lwtunnel_state *lwt)
1005 struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
1009 if (slwt->desc->attrs & (1 << SEG6_LOCAL_BPF)) {
1010 kfree(slwt->bpf.name);
1011 bpf_prog_put(slwt->bpf.prog);
1017 static int seg6_local_fill_encap(struct sk_buff *skb,
1018 struct lwtunnel_state *lwt)
1020 struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
1021 struct seg6_action_param *param;
1024 if (nla_put_u32(skb, SEG6_LOCAL_ACTION, slwt->action))
1027 for (i = 0; i < SEG6_LOCAL_MAX + 1; i++) {
1028 if (slwt->desc->attrs & (1 << i)) {
1029 param = &seg6_action_params[i];
1030 err = param->put(skb, slwt);
1039 static int seg6_local_get_encap_size(struct lwtunnel_state *lwt)
1041 struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
1042 unsigned long attrs;
1045 nlsize = nla_total_size(4); /* action */
1047 attrs = slwt->desc->attrs;
1049 if (attrs & (1 << SEG6_LOCAL_SRH))
1050 nlsize += nla_total_size((slwt->srh->hdrlen + 1) << 3);
1052 if (attrs & (1 << SEG6_LOCAL_TABLE))
1053 nlsize += nla_total_size(4);
1055 if (attrs & (1 << SEG6_LOCAL_NH4))
1056 nlsize += nla_total_size(4);
1058 if (attrs & (1 << SEG6_LOCAL_NH6))
1059 nlsize += nla_total_size(16);
1061 if (attrs & (1 << SEG6_LOCAL_IIF))
1062 nlsize += nla_total_size(4);
1064 if (attrs & (1 << SEG6_LOCAL_OIF))
1065 nlsize += nla_total_size(4);
1067 if (attrs & (1 << SEG6_LOCAL_BPF))
1068 nlsize += nla_total_size(sizeof(struct nlattr)) +
1069 nla_total_size(MAX_PROG_NAME) +
1075 static int seg6_local_cmp_encap(struct lwtunnel_state *a,
1076 struct lwtunnel_state *b)
1078 struct seg6_local_lwt *slwt_a, *slwt_b;
1079 struct seg6_action_param *param;
1082 slwt_a = seg6_local_lwtunnel(a);
1083 slwt_b = seg6_local_lwtunnel(b);
1085 if (slwt_a->action != slwt_b->action)
1088 if (slwt_a->desc->attrs != slwt_b->desc->attrs)
1091 for (i = 0; i < SEG6_LOCAL_MAX + 1; i++) {
1092 if (slwt_a->desc->attrs & (1 << i)) {
1093 param = &seg6_action_params[i];
1094 if (param->cmp(slwt_a, slwt_b))
1102 static const struct lwtunnel_encap_ops seg6_local_ops = {
1103 .build_state = seg6_local_build_state,
1104 .destroy_state = seg6_local_destroy_state,
1105 .input = seg6_local_input,
1106 .fill_encap = seg6_local_fill_encap,
1107 .get_encap_size = seg6_local_get_encap_size,
1108 .cmp_encap = seg6_local_cmp_encap,
1109 .owner = THIS_MODULE,
1112 int __init seg6_local_init(void)
1114 return lwtunnel_encap_add_ops(&seg6_local_ops,
1115 LWTUNNEL_ENCAP_SEG6_LOCAL);
1118 void seg6_local_exit(void)
1120 lwtunnel_encap_del_ops(&seg6_local_ops, LWTUNNEL_ENCAP_SEG6_LOCAL);