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 if (!seg6_validate_srh(srh
, len
))
90 static struct ipv6_sr_hdr
*get_and_validate_srh(struct sk_buff
*skb
)
92 struct ipv6_sr_hdr
*srh
;
98 if (srh
->segments_left
== 0)
101 #ifdef CONFIG_IPV6_SEG6_HMAC
102 if (!seg6_hmac_validate_skb(skb
))
109 static bool decap_and_validate(struct sk_buff
*skb
, int proto
)
111 struct ipv6_sr_hdr
*srh
;
112 unsigned int off
= 0;
115 if (srh
&& srh
->segments_left
> 0)
118 #ifdef CONFIG_IPV6_SEG6_HMAC
119 if (srh
&& !seg6_hmac_validate_skb(skb
))
123 if (ipv6_find_hdr(skb
, &off
, proto
, NULL
, NULL
) < 0)
126 if (!pskb_pull(skb
, off
))
129 skb_postpull_rcsum(skb
, skb_network_header(skb
), off
);
131 skb_reset_network_header(skb
);
132 skb_reset_transport_header(skb
);
133 skb
->encapsulation
= 0;
138 static void advance_nextseg(struct ipv6_sr_hdr
*srh
, struct in6_addr
*daddr
)
140 struct in6_addr
*addr
;
142 srh
->segments_left
--;
143 addr
= srh
->segments
+ srh
->segments_left
;
147 int seg6_lookup_nexthop(struct sk_buff
*skb
, struct in6_addr
*nhaddr
,
150 struct net
*net
= dev_net(skb
->dev
);
151 struct ipv6hdr
*hdr
= ipv6_hdr(skb
);
152 int flags
= RT6_LOOKUP_F_HAS_SADDR
;
153 struct dst_entry
*dst
= NULL
;
157 fl6
.flowi6_iif
= skb
->dev
->ifindex
;
158 fl6
.daddr
= nhaddr
? *nhaddr
: hdr
->daddr
;
159 fl6
.saddr
= hdr
->saddr
;
160 fl6
.flowlabel
= ip6_flowinfo(hdr
);
161 fl6
.flowi6_mark
= skb
->mark
;
162 fl6
.flowi6_proto
= hdr
->nexthdr
;
165 fl6
.flowi6_flags
= FLOWI_FLAG_KNOWN_NH
;
168 dst
= ip6_route_input_lookup(net
, skb
->dev
, &fl6
, skb
, flags
);
170 struct fib6_table
*table
;
172 table
= fib6_get_table(net
, tbl_id
);
176 rt
= ip6_pol_route(net
, table
, 0, &fl6
, skb
, flags
);
180 if (dst
&& dst
->dev
->flags
& IFF_LOOPBACK
&& !dst
->error
) {
187 rt
= net
->ipv6
.ip6_blk_hole_entry
;
193 skb_dst_set(skb
, dst
);
197 /* regular endpoint function */
198 static int input_action_end(struct sk_buff
*skb
, struct seg6_local_lwt
*slwt
)
200 struct ipv6_sr_hdr
*srh
;
202 srh
= get_and_validate_srh(skb
);
206 advance_nextseg(srh
, &ipv6_hdr(skb
)->daddr
);
208 seg6_lookup_nexthop(skb
, NULL
, 0);
210 return dst_input(skb
);
217 /* regular endpoint, and forward to specified nexthop */
218 static int input_action_end_x(struct sk_buff
*skb
, struct seg6_local_lwt
*slwt
)
220 struct ipv6_sr_hdr
*srh
;
222 srh
= get_and_validate_srh(skb
);
226 advance_nextseg(srh
, &ipv6_hdr(skb
)->daddr
);
228 seg6_lookup_nexthop(skb
, &slwt
->nh6
, 0);
230 return dst_input(skb
);
237 static int input_action_end_t(struct sk_buff
*skb
, struct seg6_local_lwt
*slwt
)
239 struct ipv6_sr_hdr
*srh
;
241 srh
= get_and_validate_srh(skb
);
245 advance_nextseg(srh
, &ipv6_hdr(skb
)->daddr
);
247 seg6_lookup_nexthop(skb
, NULL
, slwt
->table
);
249 return dst_input(skb
);
256 /* decapsulate and forward inner L2 frame on specified interface */
257 static int input_action_end_dx2(struct sk_buff
*skb
,
258 struct seg6_local_lwt
*slwt
)
260 struct net
*net
= dev_net(skb
->dev
);
261 struct net_device
*odev
;
264 if (!decap_and_validate(skb
, NEXTHDR_NONE
))
267 if (!pskb_may_pull(skb
, ETH_HLEN
))
270 skb_reset_mac_header(skb
);
271 eth
= (struct ethhdr
*)skb
->data
;
273 /* To determine the frame's protocol, we assume it is 802.3. This avoids
274 * a call to eth_type_trans(), which is not really relevant for our
277 if (!eth_proto_is_802_3(eth
->h_proto
))
280 odev
= dev_get_by_index_rcu(net
, slwt
->oif
);
284 /* As we accept Ethernet frames, make sure the egress device is of
287 if (odev
->type
!= ARPHRD_ETHER
)
290 if (!(odev
->flags
& IFF_UP
) || !netif_carrier_ok(odev
))
295 if (skb_warn_if_lro(skb
))
298 skb_forward_csum(skb
);
300 if (skb
->len
- ETH_HLEN
> odev
->mtu
)
304 skb
->protocol
= eth
->h_proto
;
306 return dev_queue_xmit(skb
);
313 /* decapsulate and forward to specified nexthop */
314 static int input_action_end_dx6(struct sk_buff
*skb
,
315 struct seg6_local_lwt
*slwt
)
317 struct in6_addr
*nhaddr
= NULL
;
319 /* this function accepts IPv6 encapsulated packets, with either
320 * an SRH with SL=0, or no SRH.
323 if (!decap_and_validate(skb
, IPPROTO_IPV6
))
326 if (!pskb_may_pull(skb
, sizeof(struct ipv6hdr
)))
329 /* The inner packet is not associated to any local interface,
330 * so we do not call netif_rx().
332 * If slwt->nh6 is set to ::, then lookup the nexthop for the
333 * inner packet's DA. Otherwise, use the specified nexthop.
336 if (!ipv6_addr_any(&slwt
->nh6
))
339 seg6_lookup_nexthop(skb
, nhaddr
, 0);
341 return dst_input(skb
);
347 static int input_action_end_dx4(struct sk_buff
*skb
,
348 struct seg6_local_lwt
*slwt
)
354 if (!decap_and_validate(skb
, IPPROTO_IPIP
))
357 if (!pskb_may_pull(skb
, sizeof(struct iphdr
)))
360 skb
->protocol
= htons(ETH_P_IP
);
364 nhaddr
= slwt
->nh4
.s_addr
?: iph
->daddr
;
368 err
= ip_route_input(skb
, nhaddr
, iph
->saddr
, 0, skb
->dev
);
372 return dst_input(skb
);
379 static int input_action_end_dt6(struct sk_buff
*skb
,
380 struct seg6_local_lwt
*slwt
)
382 if (!decap_and_validate(skb
, IPPROTO_IPV6
))
385 if (!pskb_may_pull(skb
, sizeof(struct ipv6hdr
)))
388 seg6_lookup_nexthop(skb
, NULL
, slwt
->table
);
390 return dst_input(skb
);
397 /* push an SRH on top of the current one */
398 static int input_action_end_b6(struct sk_buff
*skb
, struct seg6_local_lwt
*slwt
)
400 struct ipv6_sr_hdr
*srh
;
403 srh
= get_and_validate_srh(skb
);
407 err
= seg6_do_srh_inline(skb
, slwt
->srh
);
411 ipv6_hdr(skb
)->payload_len
= htons(skb
->len
- sizeof(struct ipv6hdr
));
412 skb_set_transport_header(skb
, sizeof(struct ipv6hdr
));
414 seg6_lookup_nexthop(skb
, NULL
, 0);
416 return dst_input(skb
);
423 /* encapsulate within an outer IPv6 header and a specified SRH */
424 static int input_action_end_b6_encap(struct sk_buff
*skb
,
425 struct seg6_local_lwt
*slwt
)
427 struct ipv6_sr_hdr
*srh
;
430 srh
= get_and_validate_srh(skb
);
434 advance_nextseg(srh
, &ipv6_hdr(skb
)->daddr
);
436 skb_reset_inner_headers(skb
);
437 skb
->encapsulation
= 1;
439 err
= seg6_do_srh_encap(skb
, slwt
->srh
, IPPROTO_IPV6
);
443 ipv6_hdr(skb
)->payload_len
= htons(skb
->len
- sizeof(struct ipv6hdr
));
444 skb_set_transport_header(skb
, sizeof(struct ipv6hdr
));
446 seg6_lookup_nexthop(skb
, NULL
, 0);
448 return dst_input(skb
);
455 DEFINE_PER_CPU(struct seg6_bpf_srh_state
, seg6_bpf_srh_states
);
457 bool seg6_bpf_has_valid_srh(struct sk_buff
*skb
)
459 struct seg6_bpf_srh_state
*srh_state
=
460 this_cpu_ptr(&seg6_bpf_srh_states
);
461 struct ipv6_sr_hdr
*srh
= srh_state
->srh
;
463 if (unlikely(srh
== NULL
))
466 if (unlikely(!srh_state
->valid
)) {
467 if ((srh_state
->hdrlen
& 7) != 0)
470 srh
->hdrlen
= (u8
)(srh_state
->hdrlen
>> 3);
471 if (!seg6_validate_srh(srh
, (srh
->hdrlen
+ 1) << 3))
474 srh_state
->valid
= true;
480 static int input_action_end_bpf(struct sk_buff
*skb
,
481 struct seg6_local_lwt
*slwt
)
483 struct seg6_bpf_srh_state
*srh_state
=
484 this_cpu_ptr(&seg6_bpf_srh_states
);
485 struct ipv6_sr_hdr
*srh
;
488 srh
= get_and_validate_srh(skb
);
493 advance_nextseg(srh
, &ipv6_hdr(skb
)->daddr
);
495 /* preempt_disable is needed to protect the per-CPU buffer srh_state,
496 * which is also accessed by the bpf_lwt_seg6_* helpers
499 srh_state
->srh
= srh
;
500 srh_state
->hdrlen
= srh
->hdrlen
<< 3;
501 srh_state
->valid
= true;
504 bpf_compute_data_pointers(skb
);
505 ret
= bpf_prog_run_save_cb(slwt
->bpf
.prog
, skb
);
515 pr_warn_once("bpf-seg6local: Illegal return value %u\n", ret
);
519 if (srh_state
->srh
&& !seg6_bpf_has_valid_srh(skb
))
523 if (ret
!= BPF_REDIRECT
)
524 seg6_lookup_nexthop(skb
, NULL
, 0);
526 return dst_input(skb
);
534 static struct seg6_action_desc seg6_action_table
[] = {
536 .action
= SEG6_LOCAL_ACTION_END
,
538 .input
= input_action_end
,
541 .action
= SEG6_LOCAL_ACTION_END_X
,
542 .attrs
= (1 << SEG6_LOCAL_NH6
),
543 .input
= input_action_end_x
,
546 .action
= SEG6_LOCAL_ACTION_END_T
,
547 .attrs
= (1 << SEG6_LOCAL_TABLE
),
548 .input
= input_action_end_t
,
551 .action
= SEG6_LOCAL_ACTION_END_DX2
,
552 .attrs
= (1 << SEG6_LOCAL_OIF
),
553 .input
= input_action_end_dx2
,
556 .action
= SEG6_LOCAL_ACTION_END_DX6
,
557 .attrs
= (1 << SEG6_LOCAL_NH6
),
558 .input
= input_action_end_dx6
,
561 .action
= SEG6_LOCAL_ACTION_END_DX4
,
562 .attrs
= (1 << SEG6_LOCAL_NH4
),
563 .input
= input_action_end_dx4
,
566 .action
= SEG6_LOCAL_ACTION_END_DT6
,
567 .attrs
= (1 << SEG6_LOCAL_TABLE
),
568 .input
= input_action_end_dt6
,
571 .action
= SEG6_LOCAL_ACTION_END_B6
,
572 .attrs
= (1 << SEG6_LOCAL_SRH
),
573 .input
= input_action_end_b6
,
576 .action
= SEG6_LOCAL_ACTION_END_B6_ENCAP
,
577 .attrs
= (1 << SEG6_LOCAL_SRH
),
578 .input
= input_action_end_b6_encap
,
579 .static_headroom
= sizeof(struct ipv6hdr
),
582 .action
= SEG6_LOCAL_ACTION_END_BPF
,
583 .attrs
= (1 << SEG6_LOCAL_BPF
),
584 .input
= input_action_end_bpf
,
589 static struct seg6_action_desc
*__get_action_desc(int action
)
591 struct seg6_action_desc
*desc
;
594 count
= ARRAY_SIZE(seg6_action_table
);
595 for (i
= 0; i
< count
; i
++) {
596 desc
= &seg6_action_table
[i
];
597 if (desc
->action
== action
)
604 static int seg6_local_input(struct sk_buff
*skb
)
606 struct dst_entry
*orig_dst
= skb_dst(skb
);
607 struct seg6_action_desc
*desc
;
608 struct seg6_local_lwt
*slwt
;
610 if (skb
->protocol
!= htons(ETH_P_IPV6
)) {
615 slwt
= seg6_local_lwtunnel(orig_dst
->lwtstate
);
618 return desc
->input(skb
, slwt
);
621 static const struct nla_policy seg6_local_policy
[SEG6_LOCAL_MAX
+ 1] = {
622 [SEG6_LOCAL_ACTION
] = { .type
= NLA_U32
},
623 [SEG6_LOCAL_SRH
] = { .type
= NLA_BINARY
},
624 [SEG6_LOCAL_TABLE
] = { .type
= NLA_U32
},
625 [SEG6_LOCAL_NH4
] = { .type
= NLA_BINARY
,
626 .len
= sizeof(struct in_addr
) },
627 [SEG6_LOCAL_NH6
] = { .type
= NLA_BINARY
,
628 .len
= sizeof(struct in6_addr
) },
629 [SEG6_LOCAL_IIF
] = { .type
= NLA_U32
},
630 [SEG6_LOCAL_OIF
] = { .type
= NLA_U32
},
631 [SEG6_LOCAL_BPF
] = { .type
= NLA_NESTED
},
634 static int parse_nla_srh(struct nlattr
**attrs
, struct seg6_local_lwt
*slwt
)
636 struct ipv6_sr_hdr
*srh
;
639 srh
= nla_data(attrs
[SEG6_LOCAL_SRH
]);
640 len
= nla_len(attrs
[SEG6_LOCAL_SRH
]);
642 /* SRH must contain at least one segment */
643 if (len
< sizeof(*srh
) + sizeof(struct in6_addr
))
646 if (!seg6_validate_srh(srh
, len
))
649 slwt
->srh
= kmemdup(srh
, len
, GFP_KERNEL
);
653 slwt
->headroom
+= len
;
658 static int put_nla_srh(struct sk_buff
*skb
, struct seg6_local_lwt
*slwt
)
660 struct ipv6_sr_hdr
*srh
;
665 len
= (srh
->hdrlen
+ 1) << 3;
667 nla
= nla_reserve(skb
, SEG6_LOCAL_SRH
, len
);
671 memcpy(nla_data(nla
), srh
, len
);
676 static int cmp_nla_srh(struct seg6_local_lwt
*a
, struct seg6_local_lwt
*b
)
678 int len
= (a
->srh
->hdrlen
+ 1) << 3;
680 if (len
!= ((b
->srh
->hdrlen
+ 1) << 3))
683 return memcmp(a
->srh
, b
->srh
, len
);
686 static int parse_nla_table(struct nlattr
**attrs
, struct seg6_local_lwt
*slwt
)
688 slwt
->table
= nla_get_u32(attrs
[SEG6_LOCAL_TABLE
]);
693 static int put_nla_table(struct sk_buff
*skb
, struct seg6_local_lwt
*slwt
)
695 if (nla_put_u32(skb
, SEG6_LOCAL_TABLE
, slwt
->table
))
701 static int cmp_nla_table(struct seg6_local_lwt
*a
, struct seg6_local_lwt
*b
)
703 if (a
->table
!= b
->table
)
709 static int parse_nla_nh4(struct nlattr
**attrs
, struct seg6_local_lwt
*slwt
)
711 memcpy(&slwt
->nh4
, nla_data(attrs
[SEG6_LOCAL_NH4
]),
712 sizeof(struct in_addr
));
717 static int put_nla_nh4(struct sk_buff
*skb
, struct seg6_local_lwt
*slwt
)
721 nla
= nla_reserve(skb
, SEG6_LOCAL_NH4
, sizeof(struct in_addr
));
725 memcpy(nla_data(nla
), &slwt
->nh4
, sizeof(struct in_addr
));
730 static int cmp_nla_nh4(struct seg6_local_lwt
*a
, struct seg6_local_lwt
*b
)
732 return memcmp(&a
->nh4
, &b
->nh4
, sizeof(struct in_addr
));
735 static int parse_nla_nh6(struct nlattr
**attrs
, struct seg6_local_lwt
*slwt
)
737 memcpy(&slwt
->nh6
, nla_data(attrs
[SEG6_LOCAL_NH6
]),
738 sizeof(struct in6_addr
));
743 static int put_nla_nh6(struct sk_buff
*skb
, struct seg6_local_lwt
*slwt
)
747 nla
= nla_reserve(skb
, SEG6_LOCAL_NH6
, sizeof(struct in6_addr
));
751 memcpy(nla_data(nla
), &slwt
->nh6
, sizeof(struct in6_addr
));
756 static int cmp_nla_nh6(struct seg6_local_lwt
*a
, struct seg6_local_lwt
*b
)
758 return memcmp(&a
->nh6
, &b
->nh6
, sizeof(struct in6_addr
));
761 static int parse_nla_iif(struct nlattr
**attrs
, struct seg6_local_lwt
*slwt
)
763 slwt
->iif
= nla_get_u32(attrs
[SEG6_LOCAL_IIF
]);
768 static int put_nla_iif(struct sk_buff
*skb
, struct seg6_local_lwt
*slwt
)
770 if (nla_put_u32(skb
, SEG6_LOCAL_IIF
, slwt
->iif
))
776 static int cmp_nla_iif(struct seg6_local_lwt
*a
, struct seg6_local_lwt
*b
)
778 if (a
->iif
!= b
->iif
)
784 static int parse_nla_oif(struct nlattr
**attrs
, struct seg6_local_lwt
*slwt
)
786 slwt
->oif
= nla_get_u32(attrs
[SEG6_LOCAL_OIF
]);
791 static int put_nla_oif(struct sk_buff
*skb
, struct seg6_local_lwt
*slwt
)
793 if (nla_put_u32(skb
, SEG6_LOCAL_OIF
, slwt
->oif
))
799 static int cmp_nla_oif(struct seg6_local_lwt
*a
, struct seg6_local_lwt
*b
)
801 if (a
->oif
!= b
->oif
)
807 #define MAX_PROG_NAME 256
808 static const struct nla_policy bpf_prog_policy
[SEG6_LOCAL_BPF_PROG_MAX
+ 1] = {
809 [SEG6_LOCAL_BPF_PROG
] = { .type
= NLA_U32
, },
810 [SEG6_LOCAL_BPF_PROG_NAME
] = { .type
= NLA_NUL_STRING
,
811 .len
= MAX_PROG_NAME
},
814 static int parse_nla_bpf(struct nlattr
**attrs
, struct seg6_local_lwt
*slwt
)
816 struct nlattr
*tb
[SEG6_LOCAL_BPF_PROG_MAX
+ 1];
821 ret
= nla_parse_nested_deprecated(tb
, SEG6_LOCAL_BPF_PROG_MAX
,
822 attrs
[SEG6_LOCAL_BPF
],
823 bpf_prog_policy
, NULL
);
827 if (!tb
[SEG6_LOCAL_BPF_PROG
] || !tb
[SEG6_LOCAL_BPF_PROG_NAME
])
830 slwt
->bpf
.name
= nla_memdup(tb
[SEG6_LOCAL_BPF_PROG_NAME
], GFP_KERNEL
);
834 fd
= nla_get_u32(tb
[SEG6_LOCAL_BPF_PROG
]);
835 p
= bpf_prog_get_type(fd
, BPF_PROG_TYPE_LWT_SEG6LOCAL
);
837 kfree(slwt
->bpf
.name
);
845 static int put_nla_bpf(struct sk_buff
*skb
, struct seg6_local_lwt
*slwt
)
852 nest
= nla_nest_start_noflag(skb
, SEG6_LOCAL_BPF
);
856 if (nla_put_u32(skb
, SEG6_LOCAL_BPF_PROG
, slwt
->bpf
.prog
->aux
->id
))
859 if (slwt
->bpf
.name
&&
860 nla_put_string(skb
, SEG6_LOCAL_BPF_PROG_NAME
, slwt
->bpf
.name
))
863 return nla_nest_end(skb
, nest
);
866 static int cmp_nla_bpf(struct seg6_local_lwt
*a
, struct seg6_local_lwt
*b
)
868 if (!a
->bpf
.name
&& !b
->bpf
.name
)
871 if (!a
->bpf
.name
|| !b
->bpf
.name
)
874 return strcmp(a
->bpf
.name
, b
->bpf
.name
);
877 struct seg6_action_param
{
878 int (*parse
)(struct nlattr
**attrs
, struct seg6_local_lwt
*slwt
);
879 int (*put
)(struct sk_buff
*skb
, struct seg6_local_lwt
*slwt
);
880 int (*cmp
)(struct seg6_local_lwt
*a
, struct seg6_local_lwt
*b
);
883 static struct seg6_action_param seg6_action_params
[SEG6_LOCAL_MAX
+ 1] = {
884 [SEG6_LOCAL_SRH
] = { .parse
= parse_nla_srh
,
886 .cmp
= cmp_nla_srh
},
888 [SEG6_LOCAL_TABLE
] = { .parse
= parse_nla_table
,
889 .put
= put_nla_table
,
890 .cmp
= cmp_nla_table
},
892 [SEG6_LOCAL_NH4
] = { .parse
= parse_nla_nh4
,
894 .cmp
= cmp_nla_nh4
},
896 [SEG6_LOCAL_NH6
] = { .parse
= parse_nla_nh6
,
898 .cmp
= cmp_nla_nh6
},
900 [SEG6_LOCAL_IIF
] = { .parse
= parse_nla_iif
,
902 .cmp
= cmp_nla_iif
},
904 [SEG6_LOCAL_OIF
] = { .parse
= parse_nla_oif
,
906 .cmp
= cmp_nla_oif
},
908 [SEG6_LOCAL_BPF
] = { .parse
= parse_nla_bpf
,
910 .cmp
= cmp_nla_bpf
},
914 static int parse_nla_action(struct nlattr
**attrs
, struct seg6_local_lwt
*slwt
)
916 struct seg6_action_param
*param
;
917 struct seg6_action_desc
*desc
;
920 desc
= __get_action_desc(slwt
->action
);
928 slwt
->headroom
+= desc
->static_headroom
;
930 for (i
= 0; i
< SEG6_LOCAL_MAX
+ 1; i
++) {
931 if (desc
->attrs
& (1 << i
)) {
935 param
= &seg6_action_params
[i
];
937 err
= param
->parse(attrs
, slwt
);
946 static int seg6_local_build_state(struct nlattr
*nla
, unsigned int family
,
947 const void *cfg
, struct lwtunnel_state
**ts
,
948 struct netlink_ext_ack
*extack
)
950 struct nlattr
*tb
[SEG6_LOCAL_MAX
+ 1];
951 struct lwtunnel_state
*newts
;
952 struct seg6_local_lwt
*slwt
;
955 if (family
!= AF_INET6
)
958 err
= nla_parse_nested_deprecated(tb
, SEG6_LOCAL_MAX
, nla
,
959 seg6_local_policy
, extack
);
964 if (!tb
[SEG6_LOCAL_ACTION
])
967 newts
= lwtunnel_state_alloc(sizeof(*slwt
));
971 slwt
= seg6_local_lwtunnel(newts
);
972 slwt
->action
= nla_get_u32(tb
[SEG6_LOCAL_ACTION
]);
974 err
= parse_nla_action(tb
, slwt
);
978 newts
->type
= LWTUNNEL_ENCAP_SEG6_LOCAL
;
979 newts
->flags
= LWTUNNEL_STATE_INPUT_REDIRECT
;
980 newts
->headroom
= slwt
->headroom
;
992 static void seg6_local_destroy_state(struct lwtunnel_state
*lwt
)
994 struct seg6_local_lwt
*slwt
= seg6_local_lwtunnel(lwt
);
998 if (slwt
->desc
->attrs
& (1 << SEG6_LOCAL_BPF
)) {
999 kfree(slwt
->bpf
.name
);
1000 bpf_prog_put(slwt
->bpf
.prog
);
1006 static int seg6_local_fill_encap(struct sk_buff
*skb
,
1007 struct lwtunnel_state
*lwt
)
1009 struct seg6_local_lwt
*slwt
= seg6_local_lwtunnel(lwt
);
1010 struct seg6_action_param
*param
;
1013 if (nla_put_u32(skb
, SEG6_LOCAL_ACTION
, slwt
->action
))
1016 for (i
= 0; i
< SEG6_LOCAL_MAX
+ 1; i
++) {
1017 if (slwt
->desc
->attrs
& (1 << i
)) {
1018 param
= &seg6_action_params
[i
];
1019 err
= param
->put(skb
, slwt
);
1028 static int seg6_local_get_encap_size(struct lwtunnel_state
*lwt
)
1030 struct seg6_local_lwt
*slwt
= seg6_local_lwtunnel(lwt
);
1031 unsigned long attrs
;
1034 nlsize
= nla_total_size(4); /* action */
1036 attrs
= slwt
->desc
->attrs
;
1038 if (attrs
& (1 << SEG6_LOCAL_SRH
))
1039 nlsize
+= nla_total_size((slwt
->srh
->hdrlen
+ 1) << 3);
1041 if (attrs
& (1 << SEG6_LOCAL_TABLE
))
1042 nlsize
+= nla_total_size(4);
1044 if (attrs
& (1 << SEG6_LOCAL_NH4
))
1045 nlsize
+= nla_total_size(4);
1047 if (attrs
& (1 << SEG6_LOCAL_NH6
))
1048 nlsize
+= nla_total_size(16);
1050 if (attrs
& (1 << SEG6_LOCAL_IIF
))
1051 nlsize
+= nla_total_size(4);
1053 if (attrs
& (1 << SEG6_LOCAL_OIF
))
1054 nlsize
+= nla_total_size(4);
1056 if (attrs
& (1 << SEG6_LOCAL_BPF
))
1057 nlsize
+= nla_total_size(sizeof(struct nlattr
)) +
1058 nla_total_size(MAX_PROG_NAME
) +
1064 static int seg6_local_cmp_encap(struct lwtunnel_state
*a
,
1065 struct lwtunnel_state
*b
)
1067 struct seg6_local_lwt
*slwt_a
, *slwt_b
;
1068 struct seg6_action_param
*param
;
1071 slwt_a
= seg6_local_lwtunnel(a
);
1072 slwt_b
= seg6_local_lwtunnel(b
);
1074 if (slwt_a
->action
!= slwt_b
->action
)
1077 if (slwt_a
->desc
->attrs
!= slwt_b
->desc
->attrs
)
1080 for (i
= 0; i
< SEG6_LOCAL_MAX
+ 1; i
++) {
1081 if (slwt_a
->desc
->attrs
& (1 << i
)) {
1082 param
= &seg6_action_params
[i
];
1083 if (param
->cmp(slwt_a
, slwt_b
))
1091 static const struct lwtunnel_encap_ops seg6_local_ops
= {
1092 .build_state
= seg6_local_build_state
,
1093 .destroy_state
= seg6_local_destroy_state
,
1094 .input
= seg6_local_input
,
1095 .fill_encap
= seg6_local_fill_encap
,
1096 .get_encap_size
= seg6_local_get_encap_size
,
1097 .cmp_encap
= seg6_local_cmp_encap
,
1098 .owner
= THIS_MODULE
,
1101 int __init
seg6_local_init(void)
1103 return lwtunnel_encap_add_ops(&seg6_local_ops
,
1104 LWTUNNEL_ENCAP_SEG6_LOCAL
);
1107 void seg6_local_exit(void)
1109 lwtunnel_encap_del_ops(&seg6_local_ops
, LWTUNNEL_ENCAP_SEG6_LOCAL
);