xref: /openbmc/linux/net/ipv6/seg6_local.c (revision a3bd2102e464202b58d57390a538d96f57ffc361)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2d1df6fd8SDavid Lebrun /*
3d1df6fd8SDavid Lebrun  *  SR-IPv6 implementation
4d1df6fd8SDavid Lebrun  *
5004d4b27SMathieu Xhonneux  *  Authors:
6d1df6fd8SDavid Lebrun  *  David Lebrun <david.lebrun@uclouvain.be>
7004d4b27SMathieu Xhonneux  *  eBPF support: Mathieu Xhonneux <m.xhonneux@gmail.com>
8d1df6fd8SDavid Lebrun  */
9d1df6fd8SDavid Lebrun 
10b6459415SJakub Kicinski #include <linux/filter.h>
11d1df6fd8SDavid Lebrun #include <linux/types.h>
12d1df6fd8SDavid Lebrun #include <linux/skbuff.h>
13d1df6fd8SDavid Lebrun #include <linux/net.h>
14d1df6fd8SDavid Lebrun #include <linux/module.h>
15d1df6fd8SDavid Lebrun #include <net/ip.h>
16d1df6fd8SDavid Lebrun #include <net/lwtunnel.h>
17d1df6fd8SDavid Lebrun #include <net/netevent.h>
18d1df6fd8SDavid Lebrun #include <net/netns/generic.h>
19d1df6fd8SDavid Lebrun #include <net/ip6_fib.h>
20d1df6fd8SDavid Lebrun #include <net/route.h>
21d1df6fd8SDavid Lebrun #include <net/seg6.h>
22d1df6fd8SDavid Lebrun #include <linux/seg6.h>
23d1df6fd8SDavid Lebrun #include <linux/seg6_local.h>
24d1df6fd8SDavid Lebrun #include <net/addrconf.h>
25d1df6fd8SDavid Lebrun #include <net/ip6_route.h>
26d1df6fd8SDavid Lebrun #include <net/dst_cache.h>
2762ebaeaeSYuki Taguchi #include <net/ip_tunnels.h>
28d1df6fd8SDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC
29d1df6fd8SDavid Lebrun #include <net/seg6_hmac.h>
30d1df6fd8SDavid Lebrun #endif
311c1e761eSMathieu Xhonneux #include <net/seg6_local.h>
32891ef8ddSDavid Lebrun #include <linux/etherdevice.h>
33004d4b27SMathieu Xhonneux #include <linux/bpf.h>
347a3f5b0dSRyoga Saito #include <linux/netfilter.h>
35d1df6fd8SDavid Lebrun 
36300a0fd8SAndrea Mayer #define SEG6_F_ATTR(i)		BIT(i)
37300a0fd8SAndrea Mayer 
38d1df6fd8SDavid Lebrun struct seg6_local_lwt;
39d1df6fd8SDavid Lebrun 
40cfdf64a0SAndrea Mayer /* callbacks used for customizing the creation and destruction of a behavior */
41cfdf64a0SAndrea Mayer struct seg6_local_lwtunnel_ops {
42cfdf64a0SAndrea Mayer 	int (*build_state)(struct seg6_local_lwt *slwt, const void *cfg,
43cfdf64a0SAndrea Mayer 			   struct netlink_ext_ack *extack);
44cfdf64a0SAndrea Mayer 	void (*destroy_state)(struct seg6_local_lwt *slwt);
45cfdf64a0SAndrea Mayer };
46cfdf64a0SAndrea Mayer 
47d1df6fd8SDavid Lebrun struct seg6_action_desc {
48d1df6fd8SDavid Lebrun 	int action;
49d1df6fd8SDavid Lebrun 	unsigned long attrs;
500a3021f1SAndrea Mayer 
510a3021f1SAndrea Mayer 	/* The optattrs field is used for specifying all the optional
520a3021f1SAndrea Mayer 	 * attributes supported by a specific behavior.
530a3021f1SAndrea Mayer 	 * It means that if one of these attributes is not provided in the
540a3021f1SAndrea Mayer 	 * netlink message during the behavior creation, no errors will be
550a3021f1SAndrea Mayer 	 * returned to the userspace.
560a3021f1SAndrea Mayer 	 *
570a3021f1SAndrea Mayer 	 * Each attribute can be only of two types (mutually exclusive):
580a3021f1SAndrea Mayer 	 * 1) required or 2) optional.
590a3021f1SAndrea Mayer 	 * Every user MUST obey to this rule! If you set an attribute as
600a3021f1SAndrea Mayer 	 * required the same attribute CANNOT be set as optional and vice
610a3021f1SAndrea Mayer 	 * versa.
620a3021f1SAndrea Mayer 	 */
630a3021f1SAndrea Mayer 	unsigned long optattrs;
640a3021f1SAndrea Mayer 
65d1df6fd8SDavid Lebrun 	int (*input)(struct sk_buff *skb, struct seg6_local_lwt *slwt);
66d1df6fd8SDavid Lebrun 	int static_headroom;
67cfdf64a0SAndrea Mayer 
68cfdf64a0SAndrea Mayer 	struct seg6_local_lwtunnel_ops slwt_ops;
69d1df6fd8SDavid Lebrun };
70d1df6fd8SDavid Lebrun 
71004d4b27SMathieu Xhonneux struct bpf_lwt_prog {
72004d4b27SMathieu Xhonneux 	struct bpf_prog *prog;
73004d4b27SMathieu Xhonneux 	char *name;
74004d4b27SMathieu Xhonneux };
75004d4b27SMathieu Xhonneux 
76664d6f86SAndrea Mayer enum seg6_end_dt_mode {
77664d6f86SAndrea Mayer 	DT_INVALID_MODE	= -EINVAL,
78664d6f86SAndrea Mayer 	DT_LEGACY_MODE	= 0,
79664d6f86SAndrea Mayer 	DT_VRF_MODE	= 1,
80664d6f86SAndrea Mayer };
81664d6f86SAndrea Mayer 
82664d6f86SAndrea Mayer struct seg6_end_dt_info {
83664d6f86SAndrea Mayer 	enum seg6_end_dt_mode mode;
84664d6f86SAndrea Mayer 
85664d6f86SAndrea Mayer 	struct net *net;
86664d6f86SAndrea Mayer 	/* VRF device associated to the routing table used by the SRv6
87664d6f86SAndrea Mayer 	 * End.DT4/DT6 behavior for routing IPv4/IPv6 packets.
88664d6f86SAndrea Mayer 	 */
89664d6f86SAndrea Mayer 	int vrf_ifindex;
90664d6f86SAndrea Mayer 	int vrf_table;
91664d6f86SAndrea Mayer 
928b532109SAndrea Mayer 	/* tunneled packet family (IPv4 or IPv6).
938b532109SAndrea Mayer 	 * Protocol and header length are inferred from family.
948b532109SAndrea Mayer 	 */
95664d6f86SAndrea Mayer 	u16 family;
96664d6f86SAndrea Mayer };
97664d6f86SAndrea Mayer 
9894604548SAndrea Mayer struct pcpu_seg6_local_counters {
9994604548SAndrea Mayer 	u64_stats_t packets;
10094604548SAndrea Mayer 	u64_stats_t bytes;
10194604548SAndrea Mayer 	u64_stats_t errors;
10294604548SAndrea Mayer 
10394604548SAndrea Mayer 	struct u64_stats_sync syncp;
10494604548SAndrea Mayer };
10594604548SAndrea Mayer 
10694604548SAndrea Mayer /* This struct groups all the SRv6 Behavior counters supported so far.
10794604548SAndrea Mayer  *
10894604548SAndrea Mayer  * put_nla_counters() makes use of this data structure to collect all counter
10994604548SAndrea Mayer  * values after the per-CPU counter evaluation has been performed.
11094604548SAndrea Mayer  * Finally, each counter value (in seg6_local_counters) is stored in the
11194604548SAndrea Mayer  * corresponding netlink attribute and sent to user space.
11294604548SAndrea Mayer  *
11394604548SAndrea Mayer  * NB: we don't want to expose this structure to user space!
11494604548SAndrea Mayer  */
11594604548SAndrea Mayer struct seg6_local_counters {
11694604548SAndrea Mayer 	__u64 packets;
11794604548SAndrea Mayer 	__u64 bytes;
11894604548SAndrea Mayer 	__u64 errors;
11994604548SAndrea Mayer };
12094604548SAndrea Mayer 
12194604548SAndrea Mayer #define seg6_local_alloc_pcpu_counters(__gfp)				\
12294604548SAndrea Mayer 	__netdev_alloc_pcpu_stats(struct pcpu_seg6_local_counters,	\
12394604548SAndrea Mayer 				  ((__gfp) | __GFP_ZERO))
12494604548SAndrea Mayer 
12594604548SAndrea Mayer #define SEG6_F_LOCAL_COUNTERS	SEG6_F_ATTR(SEG6_LOCAL_COUNTERS)
12694604548SAndrea Mayer 
127d1df6fd8SDavid Lebrun struct seg6_local_lwt {
128d1df6fd8SDavid Lebrun 	int action;
129d1df6fd8SDavid Lebrun 	struct ipv6_sr_hdr *srh;
130d1df6fd8SDavid Lebrun 	int table;
131d1df6fd8SDavid Lebrun 	struct in_addr nh4;
132d1df6fd8SDavid Lebrun 	struct in6_addr nh6;
133d1df6fd8SDavid Lebrun 	int iif;
134d1df6fd8SDavid Lebrun 	int oif;
135004d4b27SMathieu Xhonneux 	struct bpf_lwt_prog bpf;
136664d6f86SAndrea Mayer #ifdef CONFIG_NET_L3_MASTER_DEV
137664d6f86SAndrea Mayer 	struct seg6_end_dt_info dt_info;
138664d6f86SAndrea Mayer #endif
13994604548SAndrea Mayer 	struct pcpu_seg6_local_counters __percpu *pcpu_counters;
140d1df6fd8SDavid Lebrun 
141d1df6fd8SDavid Lebrun 	int headroom;
142d1df6fd8SDavid Lebrun 	struct seg6_action_desc *desc;
1430a3021f1SAndrea Mayer 	/* unlike the required attrs, we have to track the optional attributes
1440a3021f1SAndrea Mayer 	 * that have been effectively parsed.
1450a3021f1SAndrea Mayer 	 */
1460a3021f1SAndrea Mayer 	unsigned long parsed_optattrs;
147d1df6fd8SDavid Lebrun };
148d1df6fd8SDavid Lebrun 
149d1df6fd8SDavid Lebrun static struct seg6_local_lwt *seg6_local_lwtunnel(struct lwtunnel_state *lwt)
150d1df6fd8SDavid Lebrun {
151d1df6fd8SDavid Lebrun 	return (struct seg6_local_lwt *)lwt->data;
152d1df6fd8SDavid Lebrun }
153d1df6fd8SDavid Lebrun 
154140f04c3SDavid Lebrun static struct ipv6_sr_hdr *get_and_validate_srh(struct sk_buff *skb)
155140f04c3SDavid Lebrun {
156140f04c3SDavid Lebrun 	struct ipv6_sr_hdr *srh;
157140f04c3SDavid Lebrun 
158fa55a7d7SAndrew Lunn 	srh = seg6_get_srh(skb, IP6_FH_F_SKIP_RH);
159140f04c3SDavid Lebrun 	if (!srh)
160140f04c3SDavid Lebrun 		return NULL;
161140f04c3SDavid Lebrun 
162140f04c3SDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC
163140f04c3SDavid Lebrun 	if (!seg6_hmac_validate_skb(skb))
164140f04c3SDavid Lebrun 		return NULL;
165140f04c3SDavid Lebrun #endif
166140f04c3SDavid Lebrun 
167140f04c3SDavid Lebrun 	return srh;
168140f04c3SDavid Lebrun }
169140f04c3SDavid Lebrun 
170d7a669ddSDavid Lebrun static bool decap_and_validate(struct sk_buff *skb, int proto)
171d7a669ddSDavid Lebrun {
172d7a669ddSDavid Lebrun 	struct ipv6_sr_hdr *srh;
173d7a669ddSDavid Lebrun 	unsigned int off = 0;
174d7a669ddSDavid Lebrun 
175fa55a7d7SAndrew Lunn 	srh = seg6_get_srh(skb, 0);
176d7a669ddSDavid Lebrun 	if (srh && srh->segments_left > 0)
177d7a669ddSDavid Lebrun 		return false;
178d7a669ddSDavid Lebrun 
179d7a669ddSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC
180d7a669ddSDavid Lebrun 	if (srh && !seg6_hmac_validate_skb(skb))
181d7a669ddSDavid Lebrun 		return false;
182d7a669ddSDavid Lebrun #endif
183d7a669ddSDavid Lebrun 
184d7a669ddSDavid Lebrun 	if (ipv6_find_hdr(skb, &off, proto, NULL, NULL) < 0)
185d7a669ddSDavid Lebrun 		return false;
186d7a669ddSDavid Lebrun 
187d7a669ddSDavid Lebrun 	if (!pskb_pull(skb, off))
188d7a669ddSDavid Lebrun 		return false;
189d7a669ddSDavid Lebrun 
190d7a669ddSDavid Lebrun 	skb_postpull_rcsum(skb, skb_network_header(skb), off);
191d7a669ddSDavid Lebrun 
192d7a669ddSDavid Lebrun 	skb_reset_network_header(skb);
193d7a669ddSDavid Lebrun 	skb_reset_transport_header(skb);
19462ebaeaeSYuki Taguchi 	if (iptunnel_pull_offloads(skb))
19562ebaeaeSYuki Taguchi 		return false;
196d7a669ddSDavid Lebrun 
197d7a669ddSDavid Lebrun 	return true;
198d7a669ddSDavid Lebrun }
199d7a669ddSDavid Lebrun 
200d7a669ddSDavid Lebrun static void advance_nextseg(struct ipv6_sr_hdr *srh, struct in6_addr *daddr)
201d7a669ddSDavid Lebrun {
202d7a669ddSDavid Lebrun 	struct in6_addr *addr;
203d7a669ddSDavid Lebrun 
204d7a669ddSDavid Lebrun 	srh->segments_left--;
205d7a669ddSDavid Lebrun 	addr = srh->segments + srh->segments_left;
206d7a669ddSDavid Lebrun 	*daddr = *addr;
207d7a669ddSDavid Lebrun }
208d7a669ddSDavid Lebrun 
209fd1fef0cSAndrea Mayer static int
210fd1fef0cSAndrea Mayer seg6_lookup_any_nexthop(struct sk_buff *skb, struct in6_addr *nhaddr,
211fd1fef0cSAndrea Mayer 			u32 tbl_id, bool local_delivery)
212d7a669ddSDavid Lebrun {
213d7a669ddSDavid Lebrun 	struct net *net = dev_net(skb->dev);
214d7a669ddSDavid Lebrun 	struct ipv6hdr *hdr = ipv6_hdr(skb);
215d7a669ddSDavid Lebrun 	int flags = RT6_LOOKUP_F_HAS_SADDR;
216d7a669ddSDavid Lebrun 	struct dst_entry *dst = NULL;
217d7a669ddSDavid Lebrun 	struct rt6_info *rt;
218d7a669ddSDavid Lebrun 	struct flowi6 fl6;
219fd1fef0cSAndrea Mayer 	int dev_flags = 0;
220d7a669ddSDavid Lebrun 
221*a3bd2102SAndrea Mayer 	memset(&fl6, 0, sizeof(fl6));
222d7a669ddSDavid Lebrun 	fl6.flowi6_iif = skb->dev->ifindex;
223d7a669ddSDavid Lebrun 	fl6.daddr = nhaddr ? *nhaddr : hdr->daddr;
224d7a669ddSDavid Lebrun 	fl6.saddr = hdr->saddr;
225d7a669ddSDavid Lebrun 	fl6.flowlabel = ip6_flowinfo(hdr);
226d7a669ddSDavid Lebrun 	fl6.flowi6_mark = skb->mark;
227d7a669ddSDavid Lebrun 	fl6.flowi6_proto = hdr->nexthdr;
228d7a669ddSDavid Lebrun 
229d7a669ddSDavid Lebrun 	if (nhaddr)
230d7a669ddSDavid Lebrun 		fl6.flowi6_flags = FLOWI_FLAG_KNOWN_NH;
231d7a669ddSDavid Lebrun 
232d7a669ddSDavid Lebrun 	if (!tbl_id) {
233b75cc8f9SDavid Ahern 		dst = ip6_route_input_lookup(net, skb->dev, &fl6, skb, flags);
234d7a669ddSDavid Lebrun 	} else {
235d7a669ddSDavid Lebrun 		struct fib6_table *table;
236d7a669ddSDavid Lebrun 
237d7a669ddSDavid Lebrun 		table = fib6_get_table(net, tbl_id);
238d7a669ddSDavid Lebrun 		if (!table)
239d7a669ddSDavid Lebrun 			goto out;
240d7a669ddSDavid Lebrun 
241b75cc8f9SDavid Ahern 		rt = ip6_pol_route(net, table, 0, &fl6, skb, flags);
242d7a669ddSDavid Lebrun 		dst = &rt->dst;
243d7a669ddSDavid Lebrun 	}
244d7a669ddSDavid Lebrun 
245fd1fef0cSAndrea Mayer 	/* we want to discard traffic destined for local packet processing,
246fd1fef0cSAndrea Mayer 	 * if @local_delivery is set to false.
247fd1fef0cSAndrea Mayer 	 */
248fd1fef0cSAndrea Mayer 	if (!local_delivery)
249fd1fef0cSAndrea Mayer 		dev_flags |= IFF_LOOPBACK;
250fd1fef0cSAndrea Mayer 
251fd1fef0cSAndrea Mayer 	if (dst && (dst->dev->flags & dev_flags) && !dst->error) {
252d7a669ddSDavid Lebrun 		dst_release(dst);
253d7a669ddSDavid Lebrun 		dst = NULL;
254d7a669ddSDavid Lebrun 	}
255d7a669ddSDavid Lebrun 
256d7a669ddSDavid Lebrun out:
257d7a669ddSDavid Lebrun 	if (!dst) {
258d7a669ddSDavid Lebrun 		rt = net->ipv6.ip6_blk_hole_entry;
259d7a669ddSDavid Lebrun 		dst = &rt->dst;
260d7a669ddSDavid Lebrun 		dst_hold(dst);
261d7a669ddSDavid Lebrun 	}
262d7a669ddSDavid Lebrun 
263d7a669ddSDavid Lebrun 	skb_dst_drop(skb);
264d7a669ddSDavid Lebrun 	skb_dst_set(skb, dst);
2651c1e761eSMathieu Xhonneux 	return dst->error;
266d7a669ddSDavid Lebrun }
267d7a669ddSDavid Lebrun 
268fd1fef0cSAndrea Mayer int seg6_lookup_nexthop(struct sk_buff *skb,
269fd1fef0cSAndrea Mayer 			struct in6_addr *nhaddr, u32 tbl_id)
270fd1fef0cSAndrea Mayer {
271fd1fef0cSAndrea Mayer 	return seg6_lookup_any_nexthop(skb, nhaddr, tbl_id, false);
272fd1fef0cSAndrea Mayer }
273fd1fef0cSAndrea Mayer 
274140f04c3SDavid Lebrun /* regular endpoint function */
275140f04c3SDavid Lebrun static int input_action_end(struct sk_buff *skb, struct seg6_local_lwt *slwt)
276140f04c3SDavid Lebrun {
277140f04c3SDavid Lebrun 	struct ipv6_sr_hdr *srh;
278140f04c3SDavid Lebrun 
279140f04c3SDavid Lebrun 	srh = get_and_validate_srh(skb);
280140f04c3SDavid Lebrun 	if (!srh)
281140f04c3SDavid Lebrun 		goto drop;
282140f04c3SDavid Lebrun 
283d7a669ddSDavid Lebrun 	advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
284140f04c3SDavid Lebrun 
2851c1e761eSMathieu Xhonneux 	seg6_lookup_nexthop(skb, NULL, 0);
286140f04c3SDavid Lebrun 
287140f04c3SDavid Lebrun 	return dst_input(skb);
288140f04c3SDavid Lebrun 
289140f04c3SDavid Lebrun drop:
290140f04c3SDavid Lebrun 	kfree_skb(skb);
291140f04c3SDavid Lebrun 	return -EINVAL;
292140f04c3SDavid Lebrun }
293140f04c3SDavid Lebrun 
294140f04c3SDavid Lebrun /* regular endpoint, and forward to specified nexthop */
295140f04c3SDavid Lebrun static int input_action_end_x(struct sk_buff *skb, struct seg6_local_lwt *slwt)
296140f04c3SDavid Lebrun {
297140f04c3SDavid Lebrun 	struct ipv6_sr_hdr *srh;
298140f04c3SDavid Lebrun 
299140f04c3SDavid Lebrun 	srh = get_and_validate_srh(skb);
300140f04c3SDavid Lebrun 	if (!srh)
301140f04c3SDavid Lebrun 		goto drop;
302140f04c3SDavid Lebrun 
303d7a669ddSDavid Lebrun 	advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
304140f04c3SDavid Lebrun 
3051c1e761eSMathieu Xhonneux 	seg6_lookup_nexthop(skb, &slwt->nh6, 0);
306140f04c3SDavid Lebrun 
307140f04c3SDavid Lebrun 	return dst_input(skb);
308140f04c3SDavid Lebrun 
309140f04c3SDavid Lebrun drop:
310140f04c3SDavid Lebrun 	kfree_skb(skb);
311140f04c3SDavid Lebrun 	return -EINVAL;
312140f04c3SDavid Lebrun }
313140f04c3SDavid Lebrun 
314891ef8ddSDavid Lebrun static int input_action_end_t(struct sk_buff *skb, struct seg6_local_lwt *slwt)
315891ef8ddSDavid Lebrun {
316891ef8ddSDavid Lebrun 	struct ipv6_sr_hdr *srh;
317891ef8ddSDavid Lebrun 
318891ef8ddSDavid Lebrun 	srh = get_and_validate_srh(skb);
319891ef8ddSDavid Lebrun 	if (!srh)
320891ef8ddSDavid Lebrun 		goto drop;
321891ef8ddSDavid Lebrun 
322891ef8ddSDavid Lebrun 	advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
323891ef8ddSDavid Lebrun 
3241c1e761eSMathieu Xhonneux 	seg6_lookup_nexthop(skb, NULL, slwt->table);
325891ef8ddSDavid Lebrun 
326891ef8ddSDavid Lebrun 	return dst_input(skb);
327891ef8ddSDavid Lebrun 
328891ef8ddSDavid Lebrun drop:
329891ef8ddSDavid Lebrun 	kfree_skb(skb);
330891ef8ddSDavid Lebrun 	return -EINVAL;
331891ef8ddSDavid Lebrun }
332891ef8ddSDavid Lebrun 
333891ef8ddSDavid Lebrun /* decapsulate and forward inner L2 frame on specified interface */
334891ef8ddSDavid Lebrun static int input_action_end_dx2(struct sk_buff *skb,
335891ef8ddSDavid Lebrun 				struct seg6_local_lwt *slwt)
336891ef8ddSDavid Lebrun {
337891ef8ddSDavid Lebrun 	struct net *net = dev_net(skb->dev);
338891ef8ddSDavid Lebrun 	struct net_device *odev;
339891ef8ddSDavid Lebrun 	struct ethhdr *eth;
340891ef8ddSDavid Lebrun 
34126776253SPaolo Lungaroni 	if (!decap_and_validate(skb, IPPROTO_ETHERNET))
342891ef8ddSDavid Lebrun 		goto drop;
343891ef8ddSDavid Lebrun 
344891ef8ddSDavid Lebrun 	if (!pskb_may_pull(skb, ETH_HLEN))
345891ef8ddSDavid Lebrun 		goto drop;
346891ef8ddSDavid Lebrun 
347891ef8ddSDavid Lebrun 	skb_reset_mac_header(skb);
348891ef8ddSDavid Lebrun 	eth = (struct ethhdr *)skb->data;
349891ef8ddSDavid Lebrun 
350891ef8ddSDavid Lebrun 	/* To determine the frame's protocol, we assume it is 802.3. This avoids
351891ef8ddSDavid Lebrun 	 * a call to eth_type_trans(), which is not really relevant for our
352891ef8ddSDavid Lebrun 	 * use case.
353891ef8ddSDavid Lebrun 	 */
354891ef8ddSDavid Lebrun 	if (!eth_proto_is_802_3(eth->h_proto))
355891ef8ddSDavid Lebrun 		goto drop;
356891ef8ddSDavid Lebrun 
357891ef8ddSDavid Lebrun 	odev = dev_get_by_index_rcu(net, slwt->oif);
358891ef8ddSDavid Lebrun 	if (!odev)
359891ef8ddSDavid Lebrun 		goto drop;
360891ef8ddSDavid Lebrun 
361891ef8ddSDavid Lebrun 	/* As we accept Ethernet frames, make sure the egress device is of
362891ef8ddSDavid Lebrun 	 * the correct type.
363891ef8ddSDavid Lebrun 	 */
364891ef8ddSDavid Lebrun 	if (odev->type != ARPHRD_ETHER)
365891ef8ddSDavid Lebrun 		goto drop;
366891ef8ddSDavid Lebrun 
367891ef8ddSDavid Lebrun 	if (!(odev->flags & IFF_UP) || !netif_carrier_ok(odev))
368891ef8ddSDavid Lebrun 		goto drop;
369891ef8ddSDavid Lebrun 
370891ef8ddSDavid Lebrun 	skb_orphan(skb);
371891ef8ddSDavid Lebrun 
372891ef8ddSDavid Lebrun 	if (skb_warn_if_lro(skb))
373891ef8ddSDavid Lebrun 		goto drop;
374891ef8ddSDavid Lebrun 
375891ef8ddSDavid Lebrun 	skb_forward_csum(skb);
376891ef8ddSDavid Lebrun 
377891ef8ddSDavid Lebrun 	if (skb->len - ETH_HLEN > odev->mtu)
378891ef8ddSDavid Lebrun 		goto drop;
379891ef8ddSDavid Lebrun 
380891ef8ddSDavid Lebrun 	skb->dev = odev;
381891ef8ddSDavid Lebrun 	skb->protocol = eth->h_proto;
382891ef8ddSDavid Lebrun 
383891ef8ddSDavid Lebrun 	return dev_queue_xmit(skb);
384891ef8ddSDavid Lebrun 
385891ef8ddSDavid Lebrun drop:
386891ef8ddSDavid Lebrun 	kfree_skb(skb);
387891ef8ddSDavid Lebrun 	return -EINVAL;
388891ef8ddSDavid Lebrun }
389891ef8ddSDavid Lebrun 
3907a3f5b0dSRyoga Saito static int input_action_end_dx6_finish(struct net *net, struct sock *sk,
3917a3f5b0dSRyoga Saito 				       struct sk_buff *skb)
3927a3f5b0dSRyoga Saito {
3937a3f5b0dSRyoga Saito 	struct dst_entry *orig_dst = skb_dst(skb);
3947a3f5b0dSRyoga Saito 	struct in6_addr *nhaddr = NULL;
3957a3f5b0dSRyoga Saito 	struct seg6_local_lwt *slwt;
3967a3f5b0dSRyoga Saito 
3977a3f5b0dSRyoga Saito 	slwt = seg6_local_lwtunnel(orig_dst->lwtstate);
3987a3f5b0dSRyoga Saito 
3997a3f5b0dSRyoga Saito 	/* The inner packet is not associated to any local interface,
4007a3f5b0dSRyoga Saito 	 * so we do not call netif_rx().
4017a3f5b0dSRyoga Saito 	 *
4027a3f5b0dSRyoga Saito 	 * If slwt->nh6 is set to ::, then lookup the nexthop for the
4037a3f5b0dSRyoga Saito 	 * inner packet's DA. Otherwise, use the specified nexthop.
4047a3f5b0dSRyoga Saito 	 */
4057a3f5b0dSRyoga Saito 	if (!ipv6_addr_any(&slwt->nh6))
4067a3f5b0dSRyoga Saito 		nhaddr = &slwt->nh6;
4077a3f5b0dSRyoga Saito 
4087a3f5b0dSRyoga Saito 	seg6_lookup_nexthop(skb, nhaddr, 0);
4097a3f5b0dSRyoga Saito 
4107a3f5b0dSRyoga Saito 	return dst_input(skb);
4117a3f5b0dSRyoga Saito }
4127a3f5b0dSRyoga Saito 
413140f04c3SDavid Lebrun /* decapsulate and forward to specified nexthop */
414140f04c3SDavid Lebrun static int input_action_end_dx6(struct sk_buff *skb,
415140f04c3SDavid Lebrun 				struct seg6_local_lwt *slwt)
416140f04c3SDavid Lebrun {
417140f04c3SDavid Lebrun 	/* this function accepts IPv6 encapsulated packets, with either
418140f04c3SDavid Lebrun 	 * an SRH with SL=0, or no SRH.
419140f04c3SDavid Lebrun 	 */
420140f04c3SDavid Lebrun 
421d7a669ddSDavid Lebrun 	if (!decap_and_validate(skb, IPPROTO_IPV6))
422140f04c3SDavid Lebrun 		goto drop;
423140f04c3SDavid Lebrun 
424d7a669ddSDavid Lebrun 	if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
425140f04c3SDavid Lebrun 		goto drop;
426140f04c3SDavid Lebrun 
427c71644d0SAndrea Mayer 	skb_set_transport_header(skb, sizeof(struct ipv6hdr));
4287a3f5b0dSRyoga Saito 	nf_reset_ct(skb);
429c71644d0SAndrea Mayer 
4307a3f5b0dSRyoga Saito 	if (static_branch_unlikely(&nf_hooks_lwtunnel_enabled))
4317a3f5b0dSRyoga Saito 		return NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING,
4327a3f5b0dSRyoga Saito 			       dev_net(skb->dev), NULL, skb, NULL,
4337a3f5b0dSRyoga Saito 			       skb_dst(skb)->dev, input_action_end_dx6_finish);
434140f04c3SDavid Lebrun 
4357a3f5b0dSRyoga Saito 	return input_action_end_dx6_finish(dev_net(skb->dev), NULL, skb);
436140f04c3SDavid Lebrun drop:
437140f04c3SDavid Lebrun 	kfree_skb(skb);
438140f04c3SDavid Lebrun 	return -EINVAL;
439140f04c3SDavid Lebrun }
440140f04c3SDavid Lebrun 
4417a3f5b0dSRyoga Saito static int input_action_end_dx4_finish(struct net *net, struct sock *sk,
4427a3f5b0dSRyoga Saito 				       struct sk_buff *skb)
443891ef8ddSDavid Lebrun {
4447a3f5b0dSRyoga Saito 	struct dst_entry *orig_dst = skb_dst(skb);
4457a3f5b0dSRyoga Saito 	struct seg6_local_lwt *slwt;
446891ef8ddSDavid Lebrun 	struct iphdr *iph;
447891ef8ddSDavid Lebrun 	__be32 nhaddr;
448891ef8ddSDavid Lebrun 	int err;
449891ef8ddSDavid Lebrun 
4507a3f5b0dSRyoga Saito 	slwt = seg6_local_lwtunnel(orig_dst->lwtstate);
451891ef8ddSDavid Lebrun 
452891ef8ddSDavid Lebrun 	iph = ip_hdr(skb);
453891ef8ddSDavid Lebrun 
454891ef8ddSDavid Lebrun 	nhaddr = slwt->nh4.s_addr ?: iph->daddr;
455891ef8ddSDavid Lebrun 
456891ef8ddSDavid Lebrun 	skb_dst_drop(skb);
457891ef8ddSDavid Lebrun 
458891ef8ddSDavid Lebrun 	err = ip_route_input(skb, nhaddr, iph->saddr, 0, skb->dev);
4597a3f5b0dSRyoga Saito 	if (err) {
4607a3f5b0dSRyoga Saito 		kfree_skb(skb);
4617a3f5b0dSRyoga Saito 		return -EINVAL;
4627a3f5b0dSRyoga Saito 	}
463891ef8ddSDavid Lebrun 
464891ef8ddSDavid Lebrun 	return dst_input(skb);
4657a3f5b0dSRyoga Saito }
466891ef8ddSDavid Lebrun 
4677a3f5b0dSRyoga Saito static int input_action_end_dx4(struct sk_buff *skb,
4687a3f5b0dSRyoga Saito 				struct seg6_local_lwt *slwt)
4697a3f5b0dSRyoga Saito {
4707a3f5b0dSRyoga Saito 	if (!decap_and_validate(skb, IPPROTO_IPIP))
4717a3f5b0dSRyoga Saito 		goto drop;
4727a3f5b0dSRyoga Saito 
4737a3f5b0dSRyoga Saito 	if (!pskb_may_pull(skb, sizeof(struct iphdr)))
4747a3f5b0dSRyoga Saito 		goto drop;
4757a3f5b0dSRyoga Saito 
4767a3f5b0dSRyoga Saito 	skb->protocol = htons(ETH_P_IP);
4777a3f5b0dSRyoga Saito 	skb_set_transport_header(skb, sizeof(struct iphdr));
4787a3f5b0dSRyoga Saito 	nf_reset_ct(skb);
4797a3f5b0dSRyoga Saito 
4807a3f5b0dSRyoga Saito 	if (static_branch_unlikely(&nf_hooks_lwtunnel_enabled))
4817a3f5b0dSRyoga Saito 		return NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING,
4827a3f5b0dSRyoga Saito 			       dev_net(skb->dev), NULL, skb, NULL,
4837a3f5b0dSRyoga Saito 			       skb_dst(skb)->dev, input_action_end_dx4_finish);
4847a3f5b0dSRyoga Saito 
4857a3f5b0dSRyoga Saito 	return input_action_end_dx4_finish(dev_net(skb->dev), NULL, skb);
486891ef8ddSDavid Lebrun drop:
487891ef8ddSDavid Lebrun 	kfree_skb(skb);
488891ef8ddSDavid Lebrun 	return -EINVAL;
489891ef8ddSDavid Lebrun }
490891ef8ddSDavid Lebrun 
491664d6f86SAndrea Mayer #ifdef CONFIG_NET_L3_MASTER_DEV
492664d6f86SAndrea Mayer static struct net *fib6_config_get_net(const struct fib6_config *fib6_cfg)
493664d6f86SAndrea Mayer {
494664d6f86SAndrea Mayer 	const struct nl_info *nli = &fib6_cfg->fc_nlinfo;
495664d6f86SAndrea Mayer 
496664d6f86SAndrea Mayer 	return nli->nl_net;
497664d6f86SAndrea Mayer }
498664d6f86SAndrea Mayer 
499664d6f86SAndrea Mayer static int __seg6_end_dt_vrf_build(struct seg6_local_lwt *slwt, const void *cfg,
500664d6f86SAndrea Mayer 				   u16 family, struct netlink_ext_ack *extack)
501664d6f86SAndrea Mayer {
502664d6f86SAndrea Mayer 	struct seg6_end_dt_info *info = &slwt->dt_info;
503664d6f86SAndrea Mayer 	int vrf_ifindex;
504664d6f86SAndrea Mayer 	struct net *net;
505664d6f86SAndrea Mayer 
506664d6f86SAndrea Mayer 	net = fib6_config_get_net(cfg);
507664d6f86SAndrea Mayer 
508664d6f86SAndrea Mayer 	/* note that vrf_table was already set by parse_nla_vrftable() */
509664d6f86SAndrea Mayer 	vrf_ifindex = l3mdev_ifindex_lookup_by_table_id(L3MDEV_TYPE_VRF, net,
510664d6f86SAndrea Mayer 							info->vrf_table);
511664d6f86SAndrea Mayer 	if (vrf_ifindex < 0) {
512664d6f86SAndrea Mayer 		if (vrf_ifindex == -EPERM) {
513664d6f86SAndrea Mayer 			NL_SET_ERR_MSG(extack,
514664d6f86SAndrea Mayer 				       "Strict mode for VRF is disabled");
515664d6f86SAndrea Mayer 		} else if (vrf_ifindex == -ENODEV) {
516664d6f86SAndrea Mayer 			NL_SET_ERR_MSG(extack,
517664d6f86SAndrea Mayer 				       "Table has no associated VRF device");
518664d6f86SAndrea Mayer 		} else {
519664d6f86SAndrea Mayer 			pr_debug("seg6local: SRv6 End.DT* creation error=%d\n",
520664d6f86SAndrea Mayer 				 vrf_ifindex);
521664d6f86SAndrea Mayer 		}
522664d6f86SAndrea Mayer 
523664d6f86SAndrea Mayer 		return vrf_ifindex;
524664d6f86SAndrea Mayer 	}
525664d6f86SAndrea Mayer 
526664d6f86SAndrea Mayer 	info->net = net;
527664d6f86SAndrea Mayer 	info->vrf_ifindex = vrf_ifindex;
528664d6f86SAndrea Mayer 
529664d6f86SAndrea Mayer 	info->family = family;
530664d6f86SAndrea Mayer 	info->mode = DT_VRF_MODE;
531664d6f86SAndrea Mayer 
532664d6f86SAndrea Mayer 	return 0;
533664d6f86SAndrea Mayer }
534664d6f86SAndrea Mayer 
535664d6f86SAndrea Mayer /* The SRv6 End.DT4/DT6 behavior extracts the inner (IPv4/IPv6) packet and
536664d6f86SAndrea Mayer  * routes the IPv4/IPv6 packet by looking at the configured routing table.
537664d6f86SAndrea Mayer  *
538664d6f86SAndrea Mayer  * In the SRv6 End.DT4/DT6 use case, we can receive traffic (IPv6+Segment
539664d6f86SAndrea Mayer  * Routing Header packets) from several interfaces and the outer IPv6
540664d6f86SAndrea Mayer  * destination address (DA) is used for retrieving the specific instance of the
541664d6f86SAndrea Mayer  * End.DT4/DT6 behavior that should process the packets.
542664d6f86SAndrea Mayer  *
543664d6f86SAndrea Mayer  * However, the inner IPv4/IPv6 packet is not really bound to any receiving
544664d6f86SAndrea Mayer  * interface and thus the End.DT4/DT6 sets the VRF (associated with the
545664d6f86SAndrea Mayer  * corresponding routing table) as the *receiving* interface.
546664d6f86SAndrea Mayer  * In other words, the End.DT4/DT6 processes a packet as if it has been received
547664d6f86SAndrea Mayer  * directly by the VRF (and not by one of its slave devices, if any).
548664d6f86SAndrea Mayer  * In this way, the VRF interface is used for routing the IPv4/IPv6 packet in
549664d6f86SAndrea Mayer  * according to the routing table configured by the End.DT4/DT6 instance.
550664d6f86SAndrea Mayer  *
551664d6f86SAndrea Mayer  * This design allows you to get some interesting features like:
552664d6f86SAndrea Mayer  *  1) the statistics on rx packets;
553664d6f86SAndrea Mayer  *  2) the possibility to install a packet sniffer on the receiving interface
554664d6f86SAndrea Mayer  *     (the VRF one) for looking at the incoming packets;
555664d6f86SAndrea Mayer  *  3) the possibility to leverage the netfilter prerouting hook for the inner
556664d6f86SAndrea Mayer  *     IPv4 packet.
557664d6f86SAndrea Mayer  *
558664d6f86SAndrea Mayer  * This function returns:
559664d6f86SAndrea Mayer  *  - the sk_buff* when the VRF rcv handler has processed the packet correctly;
560664d6f86SAndrea Mayer  *  - NULL when the skb is consumed by the VRF rcv handler;
561664d6f86SAndrea Mayer  *  - a pointer which encodes a negative error number in case of error.
562664d6f86SAndrea Mayer  *    Note that in this case, the function takes care of freeing the skb.
563664d6f86SAndrea Mayer  */
564664d6f86SAndrea Mayer static struct sk_buff *end_dt_vrf_rcv(struct sk_buff *skb, u16 family,
565664d6f86SAndrea Mayer 				      struct net_device *dev)
566664d6f86SAndrea Mayer {
567664d6f86SAndrea Mayer 	/* based on l3mdev_ip_rcv; we are only interested in the master */
568664d6f86SAndrea Mayer 	if (unlikely(!netif_is_l3_master(dev) && !netif_has_l3_rx_handler(dev)))
569664d6f86SAndrea Mayer 		goto drop;
570664d6f86SAndrea Mayer 
571664d6f86SAndrea Mayer 	if (unlikely(!dev->l3mdev_ops->l3mdev_l3_rcv))
572664d6f86SAndrea Mayer 		goto drop;
573664d6f86SAndrea Mayer 
574664d6f86SAndrea Mayer 	/* the decap packet IPv4/IPv6 does not come with any mac header info.
575664d6f86SAndrea Mayer 	 * We must unset the mac header to allow the VRF device to rebuild it,
576664d6f86SAndrea Mayer 	 * just in case there is a sniffer attached on the device.
577664d6f86SAndrea Mayer 	 */
578664d6f86SAndrea Mayer 	skb_unset_mac_header(skb);
579664d6f86SAndrea Mayer 
580664d6f86SAndrea Mayer 	skb = dev->l3mdev_ops->l3mdev_l3_rcv(dev, skb, family);
581664d6f86SAndrea Mayer 	if (!skb)
582664d6f86SAndrea Mayer 		/* the skb buffer was consumed by the handler */
583664d6f86SAndrea Mayer 		return NULL;
584664d6f86SAndrea Mayer 
585664d6f86SAndrea Mayer 	/* when a packet is received by a VRF or by one of its slaves, the
586664d6f86SAndrea Mayer 	 * master device reference is set into the skb.
587664d6f86SAndrea Mayer 	 */
588664d6f86SAndrea Mayer 	if (unlikely(skb->dev != dev || skb->skb_iif != dev->ifindex))
589664d6f86SAndrea Mayer 		goto drop;
590664d6f86SAndrea Mayer 
591664d6f86SAndrea Mayer 	return skb;
592664d6f86SAndrea Mayer 
593664d6f86SAndrea Mayer drop:
594664d6f86SAndrea Mayer 	kfree_skb(skb);
595664d6f86SAndrea Mayer 	return ERR_PTR(-EINVAL);
596664d6f86SAndrea Mayer }
597664d6f86SAndrea Mayer 
598664d6f86SAndrea Mayer static struct net_device *end_dt_get_vrf_rcu(struct sk_buff *skb,
599664d6f86SAndrea Mayer 					     struct seg6_end_dt_info *info)
600664d6f86SAndrea Mayer {
601664d6f86SAndrea Mayer 	int vrf_ifindex = info->vrf_ifindex;
602664d6f86SAndrea Mayer 	struct net *net = info->net;
603664d6f86SAndrea Mayer 
604664d6f86SAndrea Mayer 	if (unlikely(vrf_ifindex < 0))
605664d6f86SAndrea Mayer 		goto error;
606664d6f86SAndrea Mayer 
607664d6f86SAndrea Mayer 	if (unlikely(!net_eq(dev_net(skb->dev), net)))
608664d6f86SAndrea Mayer 		goto error;
609664d6f86SAndrea Mayer 
610664d6f86SAndrea Mayer 	return dev_get_by_index_rcu(net, vrf_ifindex);
611664d6f86SAndrea Mayer 
612664d6f86SAndrea Mayer error:
613664d6f86SAndrea Mayer 	return NULL;
614664d6f86SAndrea Mayer }
615664d6f86SAndrea Mayer 
616664d6f86SAndrea Mayer static struct sk_buff *end_dt_vrf_core(struct sk_buff *skb,
6178b532109SAndrea Mayer 				       struct seg6_local_lwt *slwt, u16 family)
618664d6f86SAndrea Mayer {
619664d6f86SAndrea Mayer 	struct seg6_end_dt_info *info = &slwt->dt_info;
620664d6f86SAndrea Mayer 	struct net_device *vrf;
6218b532109SAndrea Mayer 	__be16 protocol;
6228b532109SAndrea Mayer 	int hdrlen;
623664d6f86SAndrea Mayer 
624664d6f86SAndrea Mayer 	vrf = end_dt_get_vrf_rcu(skb, info);
625664d6f86SAndrea Mayer 	if (unlikely(!vrf))
626664d6f86SAndrea Mayer 		goto drop;
627664d6f86SAndrea Mayer 
6288b532109SAndrea Mayer 	switch (family) {
6298b532109SAndrea Mayer 	case AF_INET:
6308b532109SAndrea Mayer 		protocol = htons(ETH_P_IP);
6318b532109SAndrea Mayer 		hdrlen = sizeof(struct iphdr);
6328b532109SAndrea Mayer 		break;
6338b532109SAndrea Mayer 	case AF_INET6:
6348b532109SAndrea Mayer 		protocol = htons(ETH_P_IPV6);
6358b532109SAndrea Mayer 		hdrlen = sizeof(struct ipv6hdr);
6368b532109SAndrea Mayer 		break;
6378b532109SAndrea Mayer 	case AF_UNSPEC:
6388b532109SAndrea Mayer 		fallthrough;
6398b532109SAndrea Mayer 	default:
6408b532109SAndrea Mayer 		goto drop;
6418b532109SAndrea Mayer 	}
6428b532109SAndrea Mayer 
6438b532109SAndrea Mayer 	if (unlikely(info->family != AF_UNSPEC && info->family != family)) {
6448b532109SAndrea Mayer 		pr_warn_once("seg6local: SRv6 End.DT* family mismatch");
6458b532109SAndrea Mayer 		goto drop;
6468b532109SAndrea Mayer 	}
6478b532109SAndrea Mayer 
6488b532109SAndrea Mayer 	skb->protocol = protocol;
649664d6f86SAndrea Mayer 
650664d6f86SAndrea Mayer 	skb_dst_drop(skb);
651664d6f86SAndrea Mayer 
6528b532109SAndrea Mayer 	skb_set_transport_header(skb, hdrlen);
6537a3f5b0dSRyoga Saito 	nf_reset_ct(skb);
654664d6f86SAndrea Mayer 
6558b532109SAndrea Mayer 	return end_dt_vrf_rcv(skb, family, vrf);
656664d6f86SAndrea Mayer 
657664d6f86SAndrea Mayer drop:
658664d6f86SAndrea Mayer 	kfree_skb(skb);
659664d6f86SAndrea Mayer 	return ERR_PTR(-EINVAL);
660664d6f86SAndrea Mayer }
661664d6f86SAndrea Mayer 
662664d6f86SAndrea Mayer static int input_action_end_dt4(struct sk_buff *skb,
663664d6f86SAndrea Mayer 				struct seg6_local_lwt *slwt)
664664d6f86SAndrea Mayer {
665664d6f86SAndrea Mayer 	struct iphdr *iph;
666664d6f86SAndrea Mayer 	int err;
667664d6f86SAndrea Mayer 
668664d6f86SAndrea Mayer 	if (!decap_and_validate(skb, IPPROTO_IPIP))
669664d6f86SAndrea Mayer 		goto drop;
670664d6f86SAndrea Mayer 
671664d6f86SAndrea Mayer 	if (!pskb_may_pull(skb, sizeof(struct iphdr)))
672664d6f86SAndrea Mayer 		goto drop;
673664d6f86SAndrea Mayer 
6748b532109SAndrea Mayer 	skb = end_dt_vrf_core(skb, slwt, AF_INET);
675664d6f86SAndrea Mayer 	if (!skb)
676664d6f86SAndrea Mayer 		/* packet has been processed and consumed by the VRF */
677664d6f86SAndrea Mayer 		return 0;
678664d6f86SAndrea Mayer 
679664d6f86SAndrea Mayer 	if (IS_ERR(skb))
680664d6f86SAndrea Mayer 		return PTR_ERR(skb);
681664d6f86SAndrea Mayer 
682664d6f86SAndrea Mayer 	iph = ip_hdr(skb);
683664d6f86SAndrea Mayer 
684664d6f86SAndrea Mayer 	err = ip_route_input(skb, iph->daddr, iph->saddr, 0, skb->dev);
685664d6f86SAndrea Mayer 	if (unlikely(err))
686664d6f86SAndrea Mayer 		goto drop;
687664d6f86SAndrea Mayer 
688664d6f86SAndrea Mayer 	return dst_input(skb);
689664d6f86SAndrea Mayer 
690664d6f86SAndrea Mayer drop:
691664d6f86SAndrea Mayer 	kfree_skb(skb);
692664d6f86SAndrea Mayer 	return -EINVAL;
693664d6f86SAndrea Mayer }
694664d6f86SAndrea Mayer 
695664d6f86SAndrea Mayer static int seg6_end_dt4_build(struct seg6_local_lwt *slwt, const void *cfg,
696664d6f86SAndrea Mayer 			      struct netlink_ext_ack *extack)
697664d6f86SAndrea Mayer {
698664d6f86SAndrea Mayer 	return __seg6_end_dt_vrf_build(slwt, cfg, AF_INET, extack);
699664d6f86SAndrea Mayer }
70020a081b7SAndrea Mayer 
70120a081b7SAndrea Mayer static enum
70220a081b7SAndrea Mayer seg6_end_dt_mode seg6_end_dt6_parse_mode(struct seg6_local_lwt *slwt)
70320a081b7SAndrea Mayer {
70420a081b7SAndrea Mayer 	unsigned long parsed_optattrs = slwt->parsed_optattrs;
70520a081b7SAndrea Mayer 	bool legacy, vrfmode;
70620a081b7SAndrea Mayer 
707300a0fd8SAndrea Mayer 	legacy	= !!(parsed_optattrs & SEG6_F_ATTR(SEG6_LOCAL_TABLE));
708300a0fd8SAndrea Mayer 	vrfmode	= !!(parsed_optattrs & SEG6_F_ATTR(SEG6_LOCAL_VRFTABLE));
70920a081b7SAndrea Mayer 
71020a081b7SAndrea Mayer 	if (!(legacy ^ vrfmode))
71120a081b7SAndrea Mayer 		/* both are absent or present: invalid DT6 mode */
71220a081b7SAndrea Mayer 		return DT_INVALID_MODE;
71320a081b7SAndrea Mayer 
71420a081b7SAndrea Mayer 	return legacy ? DT_LEGACY_MODE : DT_VRF_MODE;
71520a081b7SAndrea Mayer }
71620a081b7SAndrea Mayer 
71720a081b7SAndrea Mayer static enum seg6_end_dt_mode seg6_end_dt6_get_mode(struct seg6_local_lwt *slwt)
71820a081b7SAndrea Mayer {
71920a081b7SAndrea Mayer 	struct seg6_end_dt_info *info = &slwt->dt_info;
72020a081b7SAndrea Mayer 
72120a081b7SAndrea Mayer 	return info->mode;
72220a081b7SAndrea Mayer }
72320a081b7SAndrea Mayer 
72420a081b7SAndrea Mayer static int seg6_end_dt6_build(struct seg6_local_lwt *slwt, const void *cfg,
72520a081b7SAndrea Mayer 			      struct netlink_ext_ack *extack)
72620a081b7SAndrea Mayer {
72720a081b7SAndrea Mayer 	enum seg6_end_dt_mode mode = seg6_end_dt6_parse_mode(slwt);
72820a081b7SAndrea Mayer 	struct seg6_end_dt_info *info = &slwt->dt_info;
72920a081b7SAndrea Mayer 
73020a081b7SAndrea Mayer 	switch (mode) {
73120a081b7SAndrea Mayer 	case DT_LEGACY_MODE:
73220a081b7SAndrea Mayer 		info->mode = DT_LEGACY_MODE;
73320a081b7SAndrea Mayer 		return 0;
73420a081b7SAndrea Mayer 	case DT_VRF_MODE:
73520a081b7SAndrea Mayer 		return __seg6_end_dt_vrf_build(slwt, cfg, AF_INET6, extack);
73620a081b7SAndrea Mayer 	default:
73720a081b7SAndrea Mayer 		NL_SET_ERR_MSG(extack, "table or vrftable must be specified");
73820a081b7SAndrea Mayer 		return -EINVAL;
73920a081b7SAndrea Mayer 	}
74020a081b7SAndrea Mayer }
741664d6f86SAndrea Mayer #endif
742664d6f86SAndrea Mayer 
743891ef8ddSDavid Lebrun static int input_action_end_dt6(struct sk_buff *skb,
744891ef8ddSDavid Lebrun 				struct seg6_local_lwt *slwt)
745891ef8ddSDavid Lebrun {
746891ef8ddSDavid Lebrun 	if (!decap_and_validate(skb, IPPROTO_IPV6))
747891ef8ddSDavid Lebrun 		goto drop;
748891ef8ddSDavid Lebrun 
749891ef8ddSDavid Lebrun 	if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
750891ef8ddSDavid Lebrun 		goto drop;
751891ef8ddSDavid Lebrun 
75220a081b7SAndrea Mayer #ifdef CONFIG_NET_L3_MASTER_DEV
75320a081b7SAndrea Mayer 	if (seg6_end_dt6_get_mode(slwt) == DT_LEGACY_MODE)
75420a081b7SAndrea Mayer 		goto legacy_mode;
75520a081b7SAndrea Mayer 
75620a081b7SAndrea Mayer 	/* DT6_VRF_MODE */
7578b532109SAndrea Mayer 	skb = end_dt_vrf_core(skb, slwt, AF_INET6);
75820a081b7SAndrea Mayer 	if (!skb)
75920a081b7SAndrea Mayer 		/* packet has been processed and consumed by the VRF */
76020a081b7SAndrea Mayer 		return 0;
76120a081b7SAndrea Mayer 
76220a081b7SAndrea Mayer 	if (IS_ERR(skb))
76320a081b7SAndrea Mayer 		return PTR_ERR(skb);
76420a081b7SAndrea Mayer 
76520a081b7SAndrea Mayer 	/* note: this time we do not need to specify the table because the VRF
76620a081b7SAndrea Mayer 	 * takes care of selecting the correct table.
76720a081b7SAndrea Mayer 	 */
76820a081b7SAndrea Mayer 	seg6_lookup_any_nexthop(skb, NULL, 0, true);
76920a081b7SAndrea Mayer 
77020a081b7SAndrea Mayer 	return dst_input(skb);
77120a081b7SAndrea Mayer 
77220a081b7SAndrea Mayer legacy_mode:
77320a081b7SAndrea Mayer #endif
774c71644d0SAndrea Mayer 	skb_set_transport_header(skb, sizeof(struct ipv6hdr));
775c71644d0SAndrea Mayer 
776fd1fef0cSAndrea Mayer 	seg6_lookup_any_nexthop(skb, NULL, slwt->table, true);
777891ef8ddSDavid Lebrun 
778891ef8ddSDavid Lebrun 	return dst_input(skb);
779891ef8ddSDavid Lebrun 
780891ef8ddSDavid Lebrun drop:
781891ef8ddSDavid Lebrun 	kfree_skb(skb);
782891ef8ddSDavid Lebrun 	return -EINVAL;
783891ef8ddSDavid Lebrun }
784891ef8ddSDavid Lebrun 
7858b532109SAndrea Mayer #ifdef CONFIG_NET_L3_MASTER_DEV
7868b532109SAndrea Mayer static int seg6_end_dt46_build(struct seg6_local_lwt *slwt, const void *cfg,
7878b532109SAndrea Mayer 			       struct netlink_ext_ack *extack)
7888b532109SAndrea Mayer {
7898b532109SAndrea Mayer 	return __seg6_end_dt_vrf_build(slwt, cfg, AF_UNSPEC, extack);
7908b532109SAndrea Mayer }
7918b532109SAndrea Mayer 
7928b532109SAndrea Mayer static int input_action_end_dt46(struct sk_buff *skb,
7938b532109SAndrea Mayer 				 struct seg6_local_lwt *slwt)
7948b532109SAndrea Mayer {
7958b532109SAndrea Mayer 	unsigned int off = 0;
7968b532109SAndrea Mayer 	int nexthdr;
7978b532109SAndrea Mayer 
7988b532109SAndrea Mayer 	nexthdr = ipv6_find_hdr(skb, &off, -1, NULL, NULL);
7998b532109SAndrea Mayer 	if (unlikely(nexthdr < 0))
8008b532109SAndrea Mayer 		goto drop;
8018b532109SAndrea Mayer 
8028b532109SAndrea Mayer 	switch (nexthdr) {
8038b532109SAndrea Mayer 	case IPPROTO_IPIP:
8048b532109SAndrea Mayer 		return input_action_end_dt4(skb, slwt);
8058b532109SAndrea Mayer 	case IPPROTO_IPV6:
8068b532109SAndrea Mayer 		return input_action_end_dt6(skb, slwt);
8078b532109SAndrea Mayer 	}
8088b532109SAndrea Mayer 
8098b532109SAndrea Mayer drop:
8108b532109SAndrea Mayer 	kfree_skb(skb);
8118b532109SAndrea Mayer 	return -EINVAL;
8128b532109SAndrea Mayer }
8138b532109SAndrea Mayer #endif
8148b532109SAndrea Mayer 
815140f04c3SDavid Lebrun /* push an SRH on top of the current one */
816140f04c3SDavid Lebrun static int input_action_end_b6(struct sk_buff *skb, struct seg6_local_lwt *slwt)
817140f04c3SDavid Lebrun {
818140f04c3SDavid Lebrun 	struct ipv6_sr_hdr *srh;
819140f04c3SDavid Lebrun 	int err = -EINVAL;
820140f04c3SDavid Lebrun 
821140f04c3SDavid Lebrun 	srh = get_and_validate_srh(skb);
822140f04c3SDavid Lebrun 	if (!srh)
823140f04c3SDavid Lebrun 		goto drop;
824140f04c3SDavid Lebrun 
825140f04c3SDavid Lebrun 	err = seg6_do_srh_inline(skb, slwt->srh);
826140f04c3SDavid Lebrun 	if (err)
827140f04c3SDavid Lebrun 		goto drop;
828140f04c3SDavid Lebrun 
829140f04c3SDavid Lebrun 	ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
830140f04c3SDavid Lebrun 	skb_set_transport_header(skb, sizeof(struct ipv6hdr));
831140f04c3SDavid Lebrun 
8321c1e761eSMathieu Xhonneux 	seg6_lookup_nexthop(skb, NULL, 0);
833140f04c3SDavid Lebrun 
834140f04c3SDavid Lebrun 	return dst_input(skb);
835140f04c3SDavid Lebrun 
836140f04c3SDavid Lebrun drop:
837140f04c3SDavid Lebrun 	kfree_skb(skb);
838140f04c3SDavid Lebrun 	return err;
839140f04c3SDavid Lebrun }
840140f04c3SDavid Lebrun 
841140f04c3SDavid Lebrun /* encapsulate within an outer IPv6 header and a specified SRH */
842140f04c3SDavid Lebrun static int input_action_end_b6_encap(struct sk_buff *skb,
843140f04c3SDavid Lebrun 				     struct seg6_local_lwt *slwt)
844140f04c3SDavid Lebrun {
845140f04c3SDavid Lebrun 	struct ipv6_sr_hdr *srh;
846140f04c3SDavid Lebrun 	int err = -EINVAL;
847140f04c3SDavid Lebrun 
848140f04c3SDavid Lebrun 	srh = get_and_validate_srh(skb);
849140f04c3SDavid Lebrun 	if (!srh)
850140f04c3SDavid Lebrun 		goto drop;
851140f04c3SDavid Lebrun 
852d7a669ddSDavid Lebrun 	advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
853140f04c3SDavid Lebrun 
854140f04c3SDavid Lebrun 	skb_reset_inner_headers(skb);
855140f04c3SDavid Lebrun 	skb->encapsulation = 1;
856140f04c3SDavid Lebrun 
85732d99d0bSDavid Lebrun 	err = seg6_do_srh_encap(skb, slwt->srh, IPPROTO_IPV6);
858140f04c3SDavid Lebrun 	if (err)
859140f04c3SDavid Lebrun 		goto drop;
860140f04c3SDavid Lebrun 
861140f04c3SDavid Lebrun 	ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
862140f04c3SDavid Lebrun 	skb_set_transport_header(skb, sizeof(struct ipv6hdr));
863140f04c3SDavid Lebrun 
8641c1e761eSMathieu Xhonneux 	seg6_lookup_nexthop(skb, NULL, 0);
865140f04c3SDavid Lebrun 
866140f04c3SDavid Lebrun 	return dst_input(skb);
867140f04c3SDavid Lebrun 
868140f04c3SDavid Lebrun drop:
869140f04c3SDavid Lebrun 	kfree_skb(skb);
870140f04c3SDavid Lebrun 	return err;
871140f04c3SDavid Lebrun }
872140f04c3SDavid Lebrun 
873fe94cc29SMathieu Xhonneux DEFINE_PER_CPU(struct seg6_bpf_srh_state, seg6_bpf_srh_states);
874fe94cc29SMathieu Xhonneux 
875486cdf21SMathieu Xhonneux bool seg6_bpf_has_valid_srh(struct sk_buff *skb)
876486cdf21SMathieu Xhonneux {
877486cdf21SMathieu Xhonneux 	struct seg6_bpf_srh_state *srh_state =
878486cdf21SMathieu Xhonneux 		this_cpu_ptr(&seg6_bpf_srh_states);
879486cdf21SMathieu Xhonneux 	struct ipv6_sr_hdr *srh = srh_state->srh;
880486cdf21SMathieu Xhonneux 
881486cdf21SMathieu Xhonneux 	if (unlikely(srh == NULL))
882486cdf21SMathieu Xhonneux 		return false;
883486cdf21SMathieu Xhonneux 
884486cdf21SMathieu Xhonneux 	if (unlikely(!srh_state->valid)) {
885486cdf21SMathieu Xhonneux 		if ((srh_state->hdrlen & 7) != 0)
886486cdf21SMathieu Xhonneux 			return false;
887486cdf21SMathieu Xhonneux 
888486cdf21SMathieu Xhonneux 		srh->hdrlen = (u8)(srh_state->hdrlen >> 3);
889bb986a50SAhmed Abdelsalam 		if (!seg6_validate_srh(srh, (srh->hdrlen + 1) << 3, true))
890486cdf21SMathieu Xhonneux 			return false;
891486cdf21SMathieu Xhonneux 
892486cdf21SMathieu Xhonneux 		srh_state->valid = true;
893486cdf21SMathieu Xhonneux 	}
894486cdf21SMathieu Xhonneux 
895486cdf21SMathieu Xhonneux 	return true;
896486cdf21SMathieu Xhonneux }
897486cdf21SMathieu Xhonneux 
898004d4b27SMathieu Xhonneux static int input_action_end_bpf(struct sk_buff *skb,
899004d4b27SMathieu Xhonneux 				struct seg6_local_lwt *slwt)
900004d4b27SMathieu Xhonneux {
901004d4b27SMathieu Xhonneux 	struct seg6_bpf_srh_state *srh_state =
902004d4b27SMathieu Xhonneux 		this_cpu_ptr(&seg6_bpf_srh_states);
903004d4b27SMathieu Xhonneux 	struct ipv6_sr_hdr *srh;
904004d4b27SMathieu Xhonneux 	int ret;
905004d4b27SMathieu Xhonneux 
906004d4b27SMathieu Xhonneux 	srh = get_and_validate_srh(skb);
907486cdf21SMathieu Xhonneux 	if (!srh) {
908486cdf21SMathieu Xhonneux 		kfree_skb(skb);
909486cdf21SMathieu Xhonneux 		return -EINVAL;
910486cdf21SMathieu Xhonneux 	}
911004d4b27SMathieu Xhonneux 	advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
912004d4b27SMathieu Xhonneux 
913004d4b27SMathieu Xhonneux 	/* preempt_disable is needed to protect the per-CPU buffer srh_state,
914004d4b27SMathieu Xhonneux 	 * which is also accessed by the bpf_lwt_seg6_* helpers
915004d4b27SMathieu Xhonneux 	 */
916004d4b27SMathieu Xhonneux 	preempt_disable();
917486cdf21SMathieu Xhonneux 	srh_state->srh = srh;
918004d4b27SMathieu Xhonneux 	srh_state->hdrlen = srh->hdrlen << 3;
919486cdf21SMathieu Xhonneux 	srh_state->valid = true;
920004d4b27SMathieu Xhonneux 
921004d4b27SMathieu Xhonneux 	rcu_read_lock();
922004d4b27SMathieu Xhonneux 	bpf_compute_data_pointers(skb);
923004d4b27SMathieu Xhonneux 	ret = bpf_prog_run_save_cb(slwt->bpf.prog, skb);
924004d4b27SMathieu Xhonneux 	rcu_read_unlock();
925004d4b27SMathieu Xhonneux 
926004d4b27SMathieu Xhonneux 	switch (ret) {
927004d4b27SMathieu Xhonneux 	case BPF_OK:
928004d4b27SMathieu Xhonneux 	case BPF_REDIRECT:
929004d4b27SMathieu Xhonneux 		break;
930004d4b27SMathieu Xhonneux 	case BPF_DROP:
931004d4b27SMathieu Xhonneux 		goto drop;
932004d4b27SMathieu Xhonneux 	default:
933004d4b27SMathieu Xhonneux 		pr_warn_once("bpf-seg6local: Illegal return value %u\n", ret);
934004d4b27SMathieu Xhonneux 		goto drop;
935004d4b27SMathieu Xhonneux 	}
936004d4b27SMathieu Xhonneux 
937486cdf21SMathieu Xhonneux 	if (srh_state->srh && !seg6_bpf_has_valid_srh(skb))
938004d4b27SMathieu Xhonneux 		goto drop;
939004d4b27SMathieu Xhonneux 
940486cdf21SMathieu Xhonneux 	preempt_enable();
941004d4b27SMathieu Xhonneux 	if (ret != BPF_REDIRECT)
942004d4b27SMathieu Xhonneux 		seg6_lookup_nexthop(skb, NULL, 0);
943004d4b27SMathieu Xhonneux 
944004d4b27SMathieu Xhonneux 	return dst_input(skb);
945004d4b27SMathieu Xhonneux 
946004d4b27SMathieu Xhonneux drop:
947486cdf21SMathieu Xhonneux 	preempt_enable();
948004d4b27SMathieu Xhonneux 	kfree_skb(skb);
949004d4b27SMathieu Xhonneux 	return -EINVAL;
950004d4b27SMathieu Xhonneux }
951004d4b27SMathieu Xhonneux 
952d1df6fd8SDavid Lebrun static struct seg6_action_desc seg6_action_table[] = {
953d1df6fd8SDavid Lebrun 	{
954d1df6fd8SDavid Lebrun 		.action		= SEG6_LOCAL_ACTION_END,
955d1df6fd8SDavid Lebrun 		.attrs		= 0,
95694604548SAndrea Mayer 		.optattrs	= SEG6_F_LOCAL_COUNTERS,
957140f04c3SDavid Lebrun 		.input		= input_action_end,
958d1df6fd8SDavid Lebrun 	},
959140f04c3SDavid Lebrun 	{
960140f04c3SDavid Lebrun 		.action		= SEG6_LOCAL_ACTION_END_X,
961300a0fd8SAndrea Mayer 		.attrs		= SEG6_F_ATTR(SEG6_LOCAL_NH6),
96294604548SAndrea Mayer 		.optattrs	= SEG6_F_LOCAL_COUNTERS,
963140f04c3SDavid Lebrun 		.input		= input_action_end_x,
964140f04c3SDavid Lebrun 	},
965140f04c3SDavid Lebrun 	{
966891ef8ddSDavid Lebrun 		.action		= SEG6_LOCAL_ACTION_END_T,
967300a0fd8SAndrea Mayer 		.attrs		= SEG6_F_ATTR(SEG6_LOCAL_TABLE),
96894604548SAndrea Mayer 		.optattrs	= SEG6_F_LOCAL_COUNTERS,
969891ef8ddSDavid Lebrun 		.input		= input_action_end_t,
970891ef8ddSDavid Lebrun 	},
971891ef8ddSDavid Lebrun 	{
972891ef8ddSDavid Lebrun 		.action		= SEG6_LOCAL_ACTION_END_DX2,
973300a0fd8SAndrea Mayer 		.attrs		= SEG6_F_ATTR(SEG6_LOCAL_OIF),
97494604548SAndrea Mayer 		.optattrs	= SEG6_F_LOCAL_COUNTERS,
975891ef8ddSDavid Lebrun 		.input		= input_action_end_dx2,
976891ef8ddSDavid Lebrun 	},
977891ef8ddSDavid Lebrun 	{
978140f04c3SDavid Lebrun 		.action		= SEG6_LOCAL_ACTION_END_DX6,
979300a0fd8SAndrea Mayer 		.attrs		= SEG6_F_ATTR(SEG6_LOCAL_NH6),
98094604548SAndrea Mayer 		.optattrs	= SEG6_F_LOCAL_COUNTERS,
981140f04c3SDavid Lebrun 		.input		= input_action_end_dx6,
982140f04c3SDavid Lebrun 	},
983140f04c3SDavid Lebrun 	{
984891ef8ddSDavid Lebrun 		.action		= SEG6_LOCAL_ACTION_END_DX4,
985300a0fd8SAndrea Mayer 		.attrs		= SEG6_F_ATTR(SEG6_LOCAL_NH4),
98694604548SAndrea Mayer 		.optattrs	= SEG6_F_LOCAL_COUNTERS,
987891ef8ddSDavid Lebrun 		.input		= input_action_end_dx4,
988891ef8ddSDavid Lebrun 	},
989891ef8ddSDavid Lebrun 	{
990664d6f86SAndrea Mayer 		.action		= SEG6_LOCAL_ACTION_END_DT4,
991300a0fd8SAndrea Mayer 		.attrs		= SEG6_F_ATTR(SEG6_LOCAL_VRFTABLE),
99294604548SAndrea Mayer 		.optattrs	= SEG6_F_LOCAL_COUNTERS,
993664d6f86SAndrea Mayer #ifdef CONFIG_NET_L3_MASTER_DEV
994664d6f86SAndrea Mayer 		.input		= input_action_end_dt4,
995664d6f86SAndrea Mayer 		.slwt_ops	= {
996664d6f86SAndrea Mayer 					.build_state = seg6_end_dt4_build,
997664d6f86SAndrea Mayer 				  },
998664d6f86SAndrea Mayer #endif
999664d6f86SAndrea Mayer 	},
1000664d6f86SAndrea Mayer 	{
1001891ef8ddSDavid Lebrun 		.action		= SEG6_LOCAL_ACTION_END_DT6,
100220a081b7SAndrea Mayer #ifdef CONFIG_NET_L3_MASTER_DEV
100320a081b7SAndrea Mayer 		.attrs		= 0,
100494604548SAndrea Mayer 		.optattrs	= SEG6_F_LOCAL_COUNTERS		|
100594604548SAndrea Mayer 				  SEG6_F_ATTR(SEG6_LOCAL_TABLE) |
1006300a0fd8SAndrea Mayer 				  SEG6_F_ATTR(SEG6_LOCAL_VRFTABLE),
100720a081b7SAndrea Mayer 		.slwt_ops	= {
100820a081b7SAndrea Mayer 					.build_state = seg6_end_dt6_build,
100920a081b7SAndrea Mayer 				  },
101020a081b7SAndrea Mayer #else
1011300a0fd8SAndrea Mayer 		.attrs		= SEG6_F_ATTR(SEG6_LOCAL_TABLE),
101294604548SAndrea Mayer 		.optattrs	= SEG6_F_LOCAL_COUNTERS,
101320a081b7SAndrea Mayer #endif
1014891ef8ddSDavid Lebrun 		.input		= input_action_end_dt6,
1015891ef8ddSDavid Lebrun 	},
1016891ef8ddSDavid Lebrun 	{
10178b532109SAndrea Mayer 		.action		= SEG6_LOCAL_ACTION_END_DT46,
10188b532109SAndrea Mayer 		.attrs		= SEG6_F_ATTR(SEG6_LOCAL_VRFTABLE),
10198b532109SAndrea Mayer 		.optattrs	= SEG6_F_LOCAL_COUNTERS,
10208b532109SAndrea Mayer #ifdef CONFIG_NET_L3_MASTER_DEV
10218b532109SAndrea Mayer 		.input		= input_action_end_dt46,
10228b532109SAndrea Mayer 		.slwt_ops	= {
10238b532109SAndrea Mayer 					.build_state = seg6_end_dt46_build,
10248b532109SAndrea Mayer 				  },
10258b532109SAndrea Mayer #endif
10268b532109SAndrea Mayer 	},
10278b532109SAndrea Mayer 	{
1028140f04c3SDavid Lebrun 		.action		= SEG6_LOCAL_ACTION_END_B6,
1029300a0fd8SAndrea Mayer 		.attrs		= SEG6_F_ATTR(SEG6_LOCAL_SRH),
103094604548SAndrea Mayer 		.optattrs	= SEG6_F_LOCAL_COUNTERS,
1031140f04c3SDavid Lebrun 		.input		= input_action_end_b6,
1032140f04c3SDavid Lebrun 	},
1033140f04c3SDavid Lebrun 	{
1034140f04c3SDavid Lebrun 		.action		= SEG6_LOCAL_ACTION_END_B6_ENCAP,
1035300a0fd8SAndrea Mayer 		.attrs		= SEG6_F_ATTR(SEG6_LOCAL_SRH),
103694604548SAndrea Mayer 		.optattrs	= SEG6_F_LOCAL_COUNTERS,
1037140f04c3SDavid Lebrun 		.input		= input_action_end_b6_encap,
1038140f04c3SDavid Lebrun 		.static_headroom	= sizeof(struct ipv6hdr),
1039004d4b27SMathieu Xhonneux 	},
1040004d4b27SMathieu Xhonneux 	{
1041004d4b27SMathieu Xhonneux 		.action		= SEG6_LOCAL_ACTION_END_BPF,
1042300a0fd8SAndrea Mayer 		.attrs		= SEG6_F_ATTR(SEG6_LOCAL_BPF),
104394604548SAndrea Mayer 		.optattrs	= SEG6_F_LOCAL_COUNTERS,
1044004d4b27SMathieu Xhonneux 		.input		= input_action_end_bpf,
1045004d4b27SMathieu Xhonneux 	},
1046004d4b27SMathieu Xhonneux 
1047d1df6fd8SDavid Lebrun };
1048d1df6fd8SDavid Lebrun 
1049d1df6fd8SDavid Lebrun static struct seg6_action_desc *__get_action_desc(int action)
1050d1df6fd8SDavid Lebrun {
1051d1df6fd8SDavid Lebrun 	struct seg6_action_desc *desc;
1052d1df6fd8SDavid Lebrun 	int i, count;
1053d1df6fd8SDavid Lebrun 
1054709af180SColin Ian King 	count = ARRAY_SIZE(seg6_action_table);
1055d1df6fd8SDavid Lebrun 	for (i = 0; i < count; i++) {
1056d1df6fd8SDavid Lebrun 		desc = &seg6_action_table[i];
1057d1df6fd8SDavid Lebrun 		if (desc->action == action)
1058d1df6fd8SDavid Lebrun 			return desc;
1059d1df6fd8SDavid Lebrun 	}
1060d1df6fd8SDavid Lebrun 
1061d1df6fd8SDavid Lebrun 	return NULL;
1062d1df6fd8SDavid Lebrun }
1063d1df6fd8SDavid Lebrun 
106494604548SAndrea Mayer static bool seg6_lwtunnel_counters_enabled(struct seg6_local_lwt *slwt)
106594604548SAndrea Mayer {
106694604548SAndrea Mayer 	return slwt->parsed_optattrs & SEG6_F_LOCAL_COUNTERS;
106794604548SAndrea Mayer }
106894604548SAndrea Mayer 
106994604548SAndrea Mayer static void seg6_local_update_counters(struct seg6_local_lwt *slwt,
107094604548SAndrea Mayer 				       unsigned int len, int err)
107194604548SAndrea Mayer {
107294604548SAndrea Mayer 	struct pcpu_seg6_local_counters *pcounters;
107394604548SAndrea Mayer 
107494604548SAndrea Mayer 	pcounters = this_cpu_ptr(slwt->pcpu_counters);
107594604548SAndrea Mayer 	u64_stats_update_begin(&pcounters->syncp);
107694604548SAndrea Mayer 
107794604548SAndrea Mayer 	if (likely(!err)) {
107894604548SAndrea Mayer 		u64_stats_inc(&pcounters->packets);
107994604548SAndrea Mayer 		u64_stats_add(&pcounters->bytes, len);
108094604548SAndrea Mayer 	} else {
108194604548SAndrea Mayer 		u64_stats_inc(&pcounters->errors);
108294604548SAndrea Mayer 	}
108394604548SAndrea Mayer 
108494604548SAndrea Mayer 	u64_stats_update_end(&pcounters->syncp);
108594604548SAndrea Mayer }
108694604548SAndrea Mayer 
10877a3f5b0dSRyoga Saito static int seg6_local_input_core(struct net *net, struct sock *sk,
10887a3f5b0dSRyoga Saito 				 struct sk_buff *skb)
1089d1df6fd8SDavid Lebrun {
1090d1df6fd8SDavid Lebrun 	struct dst_entry *orig_dst = skb_dst(skb);
1091d1df6fd8SDavid Lebrun 	struct seg6_action_desc *desc;
1092d1df6fd8SDavid Lebrun 	struct seg6_local_lwt *slwt;
109394604548SAndrea Mayer 	unsigned int len = skb->len;
109494604548SAndrea Mayer 	int rc;
1095d1df6fd8SDavid Lebrun 
1096d1df6fd8SDavid Lebrun 	slwt = seg6_local_lwtunnel(orig_dst->lwtstate);
1097d1df6fd8SDavid Lebrun 	desc = slwt->desc;
1098d1df6fd8SDavid Lebrun 
109994604548SAndrea Mayer 	rc = desc->input(skb, slwt);
110094604548SAndrea Mayer 
110194604548SAndrea Mayer 	if (!seg6_lwtunnel_counters_enabled(slwt))
110294604548SAndrea Mayer 		return rc;
110394604548SAndrea Mayer 
110494604548SAndrea Mayer 	seg6_local_update_counters(slwt, len, rc);
110594604548SAndrea Mayer 
110694604548SAndrea Mayer 	return rc;
1107d1df6fd8SDavid Lebrun }
1108d1df6fd8SDavid Lebrun 
11097a3f5b0dSRyoga Saito static int seg6_local_input(struct sk_buff *skb)
11107a3f5b0dSRyoga Saito {
11117a3f5b0dSRyoga Saito 	if (skb->protocol != htons(ETH_P_IPV6)) {
11127a3f5b0dSRyoga Saito 		kfree_skb(skb);
11137a3f5b0dSRyoga Saito 		return -EINVAL;
11147a3f5b0dSRyoga Saito 	}
11157a3f5b0dSRyoga Saito 
11167a3f5b0dSRyoga Saito 	if (static_branch_unlikely(&nf_hooks_lwtunnel_enabled))
11177a3f5b0dSRyoga Saito 		return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_IN,
11187a3f5b0dSRyoga Saito 			       dev_net(skb->dev), NULL, skb, skb->dev, NULL,
11197a3f5b0dSRyoga Saito 			       seg6_local_input_core);
11207a3f5b0dSRyoga Saito 
11217a3f5b0dSRyoga Saito 	return seg6_local_input_core(dev_net(skb->dev), NULL, skb);
11227a3f5b0dSRyoga Saito }
11237a3f5b0dSRyoga Saito 
1124d1df6fd8SDavid Lebrun static const struct nla_policy seg6_local_policy[SEG6_LOCAL_MAX + 1] = {
1125d1df6fd8SDavid Lebrun 	[SEG6_LOCAL_ACTION]	= { .type = NLA_U32 },
1126d1df6fd8SDavid Lebrun 	[SEG6_LOCAL_SRH]	= { .type = NLA_BINARY },
1127d1df6fd8SDavid Lebrun 	[SEG6_LOCAL_TABLE]	= { .type = NLA_U32 },
1128664d6f86SAndrea Mayer 	[SEG6_LOCAL_VRFTABLE]	= { .type = NLA_U32 },
1129d1df6fd8SDavid Lebrun 	[SEG6_LOCAL_NH4]	= { .type = NLA_BINARY,
1130d1df6fd8SDavid Lebrun 				    .len = sizeof(struct in_addr) },
1131d1df6fd8SDavid Lebrun 	[SEG6_LOCAL_NH6]	= { .type = NLA_BINARY,
1132d1df6fd8SDavid Lebrun 				    .len = sizeof(struct in6_addr) },
1133d1df6fd8SDavid Lebrun 	[SEG6_LOCAL_IIF]	= { .type = NLA_U32 },
1134d1df6fd8SDavid Lebrun 	[SEG6_LOCAL_OIF]	= { .type = NLA_U32 },
1135004d4b27SMathieu Xhonneux 	[SEG6_LOCAL_BPF]	= { .type = NLA_NESTED },
113694604548SAndrea Mayer 	[SEG6_LOCAL_COUNTERS]	= { .type = NLA_NESTED },
1137d1df6fd8SDavid Lebrun };
1138d1df6fd8SDavid Lebrun 
11392d9cc60aSDavid Lebrun static int parse_nla_srh(struct nlattr **attrs, struct seg6_local_lwt *slwt)
11402d9cc60aSDavid Lebrun {
11412d9cc60aSDavid Lebrun 	struct ipv6_sr_hdr *srh;
11422d9cc60aSDavid Lebrun 	int len;
11432d9cc60aSDavid Lebrun 
11442d9cc60aSDavid Lebrun 	srh = nla_data(attrs[SEG6_LOCAL_SRH]);
11452d9cc60aSDavid Lebrun 	len = nla_len(attrs[SEG6_LOCAL_SRH]);
11462d9cc60aSDavid Lebrun 
11472d9cc60aSDavid Lebrun 	/* SRH must contain at least one segment */
11482d9cc60aSDavid Lebrun 	if (len < sizeof(*srh) + sizeof(struct in6_addr))
11492d9cc60aSDavid Lebrun 		return -EINVAL;
11502d9cc60aSDavid Lebrun 
1151bb986a50SAhmed Abdelsalam 	if (!seg6_validate_srh(srh, len, false))
11522d9cc60aSDavid Lebrun 		return -EINVAL;
11532d9cc60aSDavid Lebrun 
11547fa41efaSYueHaibing 	slwt->srh = kmemdup(srh, len, GFP_KERNEL);
11552d9cc60aSDavid Lebrun 	if (!slwt->srh)
11562d9cc60aSDavid Lebrun 		return -ENOMEM;
11572d9cc60aSDavid Lebrun 
11582d9cc60aSDavid Lebrun 	slwt->headroom += len;
11592d9cc60aSDavid Lebrun 
11602d9cc60aSDavid Lebrun 	return 0;
11612d9cc60aSDavid Lebrun }
11622d9cc60aSDavid Lebrun 
11632d9cc60aSDavid Lebrun static int put_nla_srh(struct sk_buff *skb, struct seg6_local_lwt *slwt)
11642d9cc60aSDavid Lebrun {
11652d9cc60aSDavid Lebrun 	struct ipv6_sr_hdr *srh;
11662d9cc60aSDavid Lebrun 	struct nlattr *nla;
11672d9cc60aSDavid Lebrun 	int len;
11682d9cc60aSDavid Lebrun 
11692d9cc60aSDavid Lebrun 	srh = slwt->srh;
11702d9cc60aSDavid Lebrun 	len = (srh->hdrlen + 1) << 3;
11712d9cc60aSDavid Lebrun 
11722d9cc60aSDavid Lebrun 	nla = nla_reserve(skb, SEG6_LOCAL_SRH, len);
11732d9cc60aSDavid Lebrun 	if (!nla)
11742d9cc60aSDavid Lebrun 		return -EMSGSIZE;
11752d9cc60aSDavid Lebrun 
11762d9cc60aSDavid Lebrun 	memcpy(nla_data(nla), srh, len);
11772d9cc60aSDavid Lebrun 
11782d9cc60aSDavid Lebrun 	return 0;
11792d9cc60aSDavid Lebrun }
11802d9cc60aSDavid Lebrun 
11812d9cc60aSDavid Lebrun static int cmp_nla_srh(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
11822d9cc60aSDavid Lebrun {
11832d9cc60aSDavid Lebrun 	int len = (a->srh->hdrlen + 1) << 3;
11842d9cc60aSDavid Lebrun 
11852d9cc60aSDavid Lebrun 	if (len != ((b->srh->hdrlen + 1) << 3))
11862d9cc60aSDavid Lebrun 		return 1;
11872d9cc60aSDavid Lebrun 
11882d9cc60aSDavid Lebrun 	return memcmp(a->srh, b->srh, len);
11892d9cc60aSDavid Lebrun }
11902d9cc60aSDavid Lebrun 
1191964adce5SAndrea Mayer static void destroy_attr_srh(struct seg6_local_lwt *slwt)
1192964adce5SAndrea Mayer {
1193964adce5SAndrea Mayer 	kfree(slwt->srh);
1194964adce5SAndrea Mayer }
1195964adce5SAndrea Mayer 
11962d9cc60aSDavid Lebrun static int parse_nla_table(struct nlattr **attrs, struct seg6_local_lwt *slwt)
11972d9cc60aSDavid Lebrun {
11982d9cc60aSDavid Lebrun 	slwt->table = nla_get_u32(attrs[SEG6_LOCAL_TABLE]);
11992d9cc60aSDavid Lebrun 
12002d9cc60aSDavid Lebrun 	return 0;
12012d9cc60aSDavid Lebrun }
12022d9cc60aSDavid Lebrun 
12032d9cc60aSDavid Lebrun static int put_nla_table(struct sk_buff *skb, struct seg6_local_lwt *slwt)
12042d9cc60aSDavid Lebrun {
12052d9cc60aSDavid Lebrun 	if (nla_put_u32(skb, SEG6_LOCAL_TABLE, slwt->table))
12062d9cc60aSDavid Lebrun 		return -EMSGSIZE;
12072d9cc60aSDavid Lebrun 
12082d9cc60aSDavid Lebrun 	return 0;
12092d9cc60aSDavid Lebrun }
12102d9cc60aSDavid Lebrun 
12112d9cc60aSDavid Lebrun static int cmp_nla_table(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
12122d9cc60aSDavid Lebrun {
12132d9cc60aSDavid Lebrun 	if (a->table != b->table)
12142d9cc60aSDavid Lebrun 		return 1;
12152d9cc60aSDavid Lebrun 
12162d9cc60aSDavid Lebrun 	return 0;
12172d9cc60aSDavid Lebrun }
12182d9cc60aSDavid Lebrun 
1219664d6f86SAndrea Mayer static struct
1220664d6f86SAndrea Mayer seg6_end_dt_info *seg6_possible_end_dt_info(struct seg6_local_lwt *slwt)
1221664d6f86SAndrea Mayer {
1222664d6f86SAndrea Mayer #ifdef CONFIG_NET_L3_MASTER_DEV
1223664d6f86SAndrea Mayer 	return &slwt->dt_info;
1224664d6f86SAndrea Mayer #else
1225664d6f86SAndrea Mayer 	return ERR_PTR(-EOPNOTSUPP);
1226664d6f86SAndrea Mayer #endif
1227664d6f86SAndrea Mayer }
1228664d6f86SAndrea Mayer 
1229664d6f86SAndrea Mayer static int parse_nla_vrftable(struct nlattr **attrs,
1230664d6f86SAndrea Mayer 			      struct seg6_local_lwt *slwt)
1231664d6f86SAndrea Mayer {
1232664d6f86SAndrea Mayer 	struct seg6_end_dt_info *info = seg6_possible_end_dt_info(slwt);
1233664d6f86SAndrea Mayer 
1234664d6f86SAndrea Mayer 	if (IS_ERR(info))
1235664d6f86SAndrea Mayer 		return PTR_ERR(info);
1236664d6f86SAndrea Mayer 
1237664d6f86SAndrea Mayer 	info->vrf_table = nla_get_u32(attrs[SEG6_LOCAL_VRFTABLE]);
1238664d6f86SAndrea Mayer 
1239664d6f86SAndrea Mayer 	return 0;
1240664d6f86SAndrea Mayer }
1241664d6f86SAndrea Mayer 
1242664d6f86SAndrea Mayer static int put_nla_vrftable(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1243664d6f86SAndrea Mayer {
1244664d6f86SAndrea Mayer 	struct seg6_end_dt_info *info = seg6_possible_end_dt_info(slwt);
1245664d6f86SAndrea Mayer 
1246664d6f86SAndrea Mayer 	if (IS_ERR(info))
1247664d6f86SAndrea Mayer 		return PTR_ERR(info);
1248664d6f86SAndrea Mayer 
1249664d6f86SAndrea Mayer 	if (nla_put_u32(skb, SEG6_LOCAL_VRFTABLE, info->vrf_table))
1250664d6f86SAndrea Mayer 		return -EMSGSIZE;
1251664d6f86SAndrea Mayer 
1252664d6f86SAndrea Mayer 	return 0;
1253664d6f86SAndrea Mayer }
1254664d6f86SAndrea Mayer 
1255664d6f86SAndrea Mayer static int cmp_nla_vrftable(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1256664d6f86SAndrea Mayer {
1257664d6f86SAndrea Mayer 	struct seg6_end_dt_info *info_a = seg6_possible_end_dt_info(a);
1258664d6f86SAndrea Mayer 	struct seg6_end_dt_info *info_b = seg6_possible_end_dt_info(b);
1259664d6f86SAndrea Mayer 
1260664d6f86SAndrea Mayer 	if (info_a->vrf_table != info_b->vrf_table)
1261664d6f86SAndrea Mayer 		return 1;
1262664d6f86SAndrea Mayer 
1263664d6f86SAndrea Mayer 	return 0;
1264664d6f86SAndrea Mayer }
1265664d6f86SAndrea Mayer 
12662d9cc60aSDavid Lebrun static int parse_nla_nh4(struct nlattr **attrs, struct seg6_local_lwt *slwt)
12672d9cc60aSDavid Lebrun {
12682d9cc60aSDavid Lebrun 	memcpy(&slwt->nh4, nla_data(attrs[SEG6_LOCAL_NH4]),
12692d9cc60aSDavid Lebrun 	       sizeof(struct in_addr));
12702d9cc60aSDavid Lebrun 
12712d9cc60aSDavid Lebrun 	return 0;
12722d9cc60aSDavid Lebrun }
12732d9cc60aSDavid Lebrun 
12742d9cc60aSDavid Lebrun static int put_nla_nh4(struct sk_buff *skb, struct seg6_local_lwt *slwt)
12752d9cc60aSDavid Lebrun {
12762d9cc60aSDavid Lebrun 	struct nlattr *nla;
12772d9cc60aSDavid Lebrun 
12782d9cc60aSDavid Lebrun 	nla = nla_reserve(skb, SEG6_LOCAL_NH4, sizeof(struct in_addr));
12792d9cc60aSDavid Lebrun 	if (!nla)
12802d9cc60aSDavid Lebrun 		return -EMSGSIZE;
12812d9cc60aSDavid Lebrun 
12822d9cc60aSDavid Lebrun 	memcpy(nla_data(nla), &slwt->nh4, sizeof(struct in_addr));
12832d9cc60aSDavid Lebrun 
12842d9cc60aSDavid Lebrun 	return 0;
12852d9cc60aSDavid Lebrun }
12862d9cc60aSDavid Lebrun 
12872d9cc60aSDavid Lebrun static int cmp_nla_nh4(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
12882d9cc60aSDavid Lebrun {
12892d9cc60aSDavid Lebrun 	return memcmp(&a->nh4, &b->nh4, sizeof(struct in_addr));
12902d9cc60aSDavid Lebrun }
12912d9cc60aSDavid Lebrun 
12922d9cc60aSDavid Lebrun static int parse_nla_nh6(struct nlattr **attrs, struct seg6_local_lwt *slwt)
12932d9cc60aSDavid Lebrun {
12942d9cc60aSDavid Lebrun 	memcpy(&slwt->nh6, nla_data(attrs[SEG6_LOCAL_NH6]),
12952d9cc60aSDavid Lebrun 	       sizeof(struct in6_addr));
12962d9cc60aSDavid Lebrun 
12972d9cc60aSDavid Lebrun 	return 0;
12982d9cc60aSDavid Lebrun }
12992d9cc60aSDavid Lebrun 
13002d9cc60aSDavid Lebrun static int put_nla_nh6(struct sk_buff *skb, struct seg6_local_lwt *slwt)
13012d9cc60aSDavid Lebrun {
13022d9cc60aSDavid Lebrun 	struct nlattr *nla;
13032d9cc60aSDavid Lebrun 
13042d9cc60aSDavid Lebrun 	nla = nla_reserve(skb, SEG6_LOCAL_NH6, sizeof(struct in6_addr));
13052d9cc60aSDavid Lebrun 	if (!nla)
13062d9cc60aSDavid Lebrun 		return -EMSGSIZE;
13072d9cc60aSDavid Lebrun 
13082d9cc60aSDavid Lebrun 	memcpy(nla_data(nla), &slwt->nh6, sizeof(struct in6_addr));
13092d9cc60aSDavid Lebrun 
13102d9cc60aSDavid Lebrun 	return 0;
13112d9cc60aSDavid Lebrun }
13122d9cc60aSDavid Lebrun 
13132d9cc60aSDavid Lebrun static int cmp_nla_nh6(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
13142d9cc60aSDavid Lebrun {
13152d9cc60aSDavid Lebrun 	return memcmp(&a->nh6, &b->nh6, sizeof(struct in6_addr));
13162d9cc60aSDavid Lebrun }
13172d9cc60aSDavid Lebrun 
13182d9cc60aSDavid Lebrun static int parse_nla_iif(struct nlattr **attrs, struct seg6_local_lwt *slwt)
13192d9cc60aSDavid Lebrun {
13202d9cc60aSDavid Lebrun 	slwt->iif = nla_get_u32(attrs[SEG6_LOCAL_IIF]);
13212d9cc60aSDavid Lebrun 
13222d9cc60aSDavid Lebrun 	return 0;
13232d9cc60aSDavid Lebrun }
13242d9cc60aSDavid Lebrun 
13252d9cc60aSDavid Lebrun static int put_nla_iif(struct sk_buff *skb, struct seg6_local_lwt *slwt)
13262d9cc60aSDavid Lebrun {
13272d9cc60aSDavid Lebrun 	if (nla_put_u32(skb, SEG6_LOCAL_IIF, slwt->iif))
13282d9cc60aSDavid Lebrun 		return -EMSGSIZE;
13292d9cc60aSDavid Lebrun 
13302d9cc60aSDavid Lebrun 	return 0;
13312d9cc60aSDavid Lebrun }
13322d9cc60aSDavid Lebrun 
13332d9cc60aSDavid Lebrun static int cmp_nla_iif(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
13342d9cc60aSDavid Lebrun {
13352d9cc60aSDavid Lebrun 	if (a->iif != b->iif)
13362d9cc60aSDavid Lebrun 		return 1;
13372d9cc60aSDavid Lebrun 
13382d9cc60aSDavid Lebrun 	return 0;
13392d9cc60aSDavid Lebrun }
13402d9cc60aSDavid Lebrun 
13412d9cc60aSDavid Lebrun static int parse_nla_oif(struct nlattr **attrs, struct seg6_local_lwt *slwt)
13422d9cc60aSDavid Lebrun {
13432d9cc60aSDavid Lebrun 	slwt->oif = nla_get_u32(attrs[SEG6_LOCAL_OIF]);
13442d9cc60aSDavid Lebrun 
13452d9cc60aSDavid Lebrun 	return 0;
13462d9cc60aSDavid Lebrun }
13472d9cc60aSDavid Lebrun 
13482d9cc60aSDavid Lebrun static int put_nla_oif(struct sk_buff *skb, struct seg6_local_lwt *slwt)
13492d9cc60aSDavid Lebrun {
13502d9cc60aSDavid Lebrun 	if (nla_put_u32(skb, SEG6_LOCAL_OIF, slwt->oif))
13512d9cc60aSDavid Lebrun 		return -EMSGSIZE;
13522d9cc60aSDavid Lebrun 
13532d9cc60aSDavid Lebrun 	return 0;
13542d9cc60aSDavid Lebrun }
13552d9cc60aSDavid Lebrun 
13562d9cc60aSDavid Lebrun static int cmp_nla_oif(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
13572d9cc60aSDavid Lebrun {
13582d9cc60aSDavid Lebrun 	if (a->oif != b->oif)
13592d9cc60aSDavid Lebrun 		return 1;
13602d9cc60aSDavid Lebrun 
13612d9cc60aSDavid Lebrun 	return 0;
13622d9cc60aSDavid Lebrun }
13632d9cc60aSDavid Lebrun 
1364004d4b27SMathieu Xhonneux #define MAX_PROG_NAME 256
1365004d4b27SMathieu Xhonneux static const struct nla_policy bpf_prog_policy[SEG6_LOCAL_BPF_PROG_MAX + 1] = {
1366004d4b27SMathieu Xhonneux 	[SEG6_LOCAL_BPF_PROG]	   = { .type = NLA_U32, },
1367004d4b27SMathieu Xhonneux 	[SEG6_LOCAL_BPF_PROG_NAME] = { .type = NLA_NUL_STRING,
1368004d4b27SMathieu Xhonneux 				       .len = MAX_PROG_NAME },
1369004d4b27SMathieu Xhonneux };
1370004d4b27SMathieu Xhonneux 
1371004d4b27SMathieu Xhonneux static int parse_nla_bpf(struct nlattr **attrs, struct seg6_local_lwt *slwt)
1372004d4b27SMathieu Xhonneux {
1373004d4b27SMathieu Xhonneux 	struct nlattr *tb[SEG6_LOCAL_BPF_PROG_MAX + 1];
1374004d4b27SMathieu Xhonneux 	struct bpf_prog *p;
1375004d4b27SMathieu Xhonneux 	int ret;
1376004d4b27SMathieu Xhonneux 	u32 fd;
1377004d4b27SMathieu Xhonneux 
13788cb08174SJohannes Berg 	ret = nla_parse_nested_deprecated(tb, SEG6_LOCAL_BPF_PROG_MAX,
13798cb08174SJohannes Berg 					  attrs[SEG6_LOCAL_BPF],
13808cb08174SJohannes Berg 					  bpf_prog_policy, NULL);
1381004d4b27SMathieu Xhonneux 	if (ret < 0)
1382004d4b27SMathieu Xhonneux 		return ret;
1383004d4b27SMathieu Xhonneux 
1384004d4b27SMathieu Xhonneux 	if (!tb[SEG6_LOCAL_BPF_PROG] || !tb[SEG6_LOCAL_BPF_PROG_NAME])
1385004d4b27SMathieu Xhonneux 		return -EINVAL;
1386004d4b27SMathieu Xhonneux 
1387004d4b27SMathieu Xhonneux 	slwt->bpf.name = nla_memdup(tb[SEG6_LOCAL_BPF_PROG_NAME], GFP_KERNEL);
1388004d4b27SMathieu Xhonneux 	if (!slwt->bpf.name)
1389004d4b27SMathieu Xhonneux 		return -ENOMEM;
1390004d4b27SMathieu Xhonneux 
1391004d4b27SMathieu Xhonneux 	fd = nla_get_u32(tb[SEG6_LOCAL_BPF_PROG]);
1392004d4b27SMathieu Xhonneux 	p = bpf_prog_get_type(fd, BPF_PROG_TYPE_LWT_SEG6LOCAL);
1393004d4b27SMathieu Xhonneux 	if (IS_ERR(p)) {
1394004d4b27SMathieu Xhonneux 		kfree(slwt->bpf.name);
1395004d4b27SMathieu Xhonneux 		return PTR_ERR(p);
1396004d4b27SMathieu Xhonneux 	}
1397004d4b27SMathieu Xhonneux 
1398004d4b27SMathieu Xhonneux 	slwt->bpf.prog = p;
1399004d4b27SMathieu Xhonneux 	return 0;
1400004d4b27SMathieu Xhonneux }
1401004d4b27SMathieu Xhonneux 
1402004d4b27SMathieu Xhonneux static int put_nla_bpf(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1403004d4b27SMathieu Xhonneux {
1404004d4b27SMathieu Xhonneux 	struct nlattr *nest;
1405004d4b27SMathieu Xhonneux 
1406004d4b27SMathieu Xhonneux 	if (!slwt->bpf.prog)
1407004d4b27SMathieu Xhonneux 		return 0;
1408004d4b27SMathieu Xhonneux 
1409ae0be8deSMichal Kubecek 	nest = nla_nest_start_noflag(skb, SEG6_LOCAL_BPF);
1410004d4b27SMathieu Xhonneux 	if (!nest)
1411004d4b27SMathieu Xhonneux 		return -EMSGSIZE;
1412004d4b27SMathieu Xhonneux 
1413004d4b27SMathieu Xhonneux 	if (nla_put_u32(skb, SEG6_LOCAL_BPF_PROG, slwt->bpf.prog->aux->id))
1414004d4b27SMathieu Xhonneux 		return -EMSGSIZE;
1415004d4b27SMathieu Xhonneux 
1416004d4b27SMathieu Xhonneux 	if (slwt->bpf.name &&
1417004d4b27SMathieu Xhonneux 	    nla_put_string(skb, SEG6_LOCAL_BPF_PROG_NAME, slwt->bpf.name))
1418004d4b27SMathieu Xhonneux 		return -EMSGSIZE;
1419004d4b27SMathieu Xhonneux 
1420004d4b27SMathieu Xhonneux 	return nla_nest_end(skb, nest);
1421004d4b27SMathieu Xhonneux }
1422004d4b27SMathieu Xhonneux 
1423004d4b27SMathieu Xhonneux static int cmp_nla_bpf(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1424004d4b27SMathieu Xhonneux {
1425004d4b27SMathieu Xhonneux 	if (!a->bpf.name && !b->bpf.name)
1426004d4b27SMathieu Xhonneux 		return 0;
1427004d4b27SMathieu Xhonneux 
1428004d4b27SMathieu Xhonneux 	if (!a->bpf.name || !b->bpf.name)
1429004d4b27SMathieu Xhonneux 		return 1;
1430004d4b27SMathieu Xhonneux 
1431004d4b27SMathieu Xhonneux 	return strcmp(a->bpf.name, b->bpf.name);
1432004d4b27SMathieu Xhonneux }
1433004d4b27SMathieu Xhonneux 
1434964adce5SAndrea Mayer static void destroy_attr_bpf(struct seg6_local_lwt *slwt)
1435964adce5SAndrea Mayer {
1436964adce5SAndrea Mayer 	kfree(slwt->bpf.name);
1437964adce5SAndrea Mayer 	if (slwt->bpf.prog)
1438964adce5SAndrea Mayer 		bpf_prog_put(slwt->bpf.prog);
1439964adce5SAndrea Mayer }
1440964adce5SAndrea Mayer 
144194604548SAndrea Mayer static const struct
144294604548SAndrea Mayer nla_policy seg6_local_counters_policy[SEG6_LOCAL_CNT_MAX + 1] = {
144394604548SAndrea Mayer 	[SEG6_LOCAL_CNT_PACKETS]	= { .type = NLA_U64 },
144494604548SAndrea Mayer 	[SEG6_LOCAL_CNT_BYTES]		= { .type = NLA_U64 },
144594604548SAndrea Mayer 	[SEG6_LOCAL_CNT_ERRORS]		= { .type = NLA_U64 },
144694604548SAndrea Mayer };
144794604548SAndrea Mayer 
144894604548SAndrea Mayer static int parse_nla_counters(struct nlattr **attrs,
144994604548SAndrea Mayer 			      struct seg6_local_lwt *slwt)
145094604548SAndrea Mayer {
145194604548SAndrea Mayer 	struct pcpu_seg6_local_counters __percpu *pcounters;
145294604548SAndrea Mayer 	struct nlattr *tb[SEG6_LOCAL_CNT_MAX + 1];
145394604548SAndrea Mayer 	int ret;
145494604548SAndrea Mayer 
145594604548SAndrea Mayer 	ret = nla_parse_nested_deprecated(tb, SEG6_LOCAL_CNT_MAX,
145694604548SAndrea Mayer 					  attrs[SEG6_LOCAL_COUNTERS],
145794604548SAndrea Mayer 					  seg6_local_counters_policy, NULL);
145894604548SAndrea Mayer 	if (ret < 0)
145994604548SAndrea Mayer 		return ret;
146094604548SAndrea Mayer 
146194604548SAndrea Mayer 	/* basic support for SRv6 Behavior counters requires at least:
146294604548SAndrea Mayer 	 * packets, bytes and errors.
146394604548SAndrea Mayer 	 */
146494604548SAndrea Mayer 	if (!tb[SEG6_LOCAL_CNT_PACKETS] || !tb[SEG6_LOCAL_CNT_BYTES] ||
146594604548SAndrea Mayer 	    !tb[SEG6_LOCAL_CNT_ERRORS])
146694604548SAndrea Mayer 		return -EINVAL;
146794604548SAndrea Mayer 
146894604548SAndrea Mayer 	/* counters are always zero initialized */
146994604548SAndrea Mayer 	pcounters = seg6_local_alloc_pcpu_counters(GFP_KERNEL);
147094604548SAndrea Mayer 	if (!pcounters)
147194604548SAndrea Mayer 		return -ENOMEM;
147294604548SAndrea Mayer 
147394604548SAndrea Mayer 	slwt->pcpu_counters = pcounters;
147494604548SAndrea Mayer 
147594604548SAndrea Mayer 	return 0;
147694604548SAndrea Mayer }
147794604548SAndrea Mayer 
147894604548SAndrea Mayer static int seg6_local_fill_nla_counters(struct sk_buff *skb,
147994604548SAndrea Mayer 					struct seg6_local_counters *counters)
148094604548SAndrea Mayer {
148194604548SAndrea Mayer 	if (nla_put_u64_64bit(skb, SEG6_LOCAL_CNT_PACKETS, counters->packets,
148294604548SAndrea Mayer 			      SEG6_LOCAL_CNT_PAD))
148394604548SAndrea Mayer 		return -EMSGSIZE;
148494604548SAndrea Mayer 
148594604548SAndrea Mayer 	if (nla_put_u64_64bit(skb, SEG6_LOCAL_CNT_BYTES, counters->bytes,
148694604548SAndrea Mayer 			      SEG6_LOCAL_CNT_PAD))
148794604548SAndrea Mayer 		return -EMSGSIZE;
148894604548SAndrea Mayer 
148994604548SAndrea Mayer 	if (nla_put_u64_64bit(skb, SEG6_LOCAL_CNT_ERRORS, counters->errors,
149094604548SAndrea Mayer 			      SEG6_LOCAL_CNT_PAD))
149194604548SAndrea Mayer 		return -EMSGSIZE;
149294604548SAndrea Mayer 
149394604548SAndrea Mayer 	return 0;
149494604548SAndrea Mayer }
149594604548SAndrea Mayer 
149694604548SAndrea Mayer static int put_nla_counters(struct sk_buff *skb, struct seg6_local_lwt *slwt)
149794604548SAndrea Mayer {
149894604548SAndrea Mayer 	struct seg6_local_counters counters = { 0, 0, 0 };
149994604548SAndrea Mayer 	struct nlattr *nest;
150094604548SAndrea Mayer 	int rc, i;
150194604548SAndrea Mayer 
150294604548SAndrea Mayer 	nest = nla_nest_start(skb, SEG6_LOCAL_COUNTERS);
150394604548SAndrea Mayer 	if (!nest)
150494604548SAndrea Mayer 		return -EMSGSIZE;
150594604548SAndrea Mayer 
150694604548SAndrea Mayer 	for_each_possible_cpu(i) {
150794604548SAndrea Mayer 		struct pcpu_seg6_local_counters *pcounters;
150894604548SAndrea Mayer 		u64 packets, bytes, errors;
150994604548SAndrea Mayer 		unsigned int start;
151094604548SAndrea Mayer 
151194604548SAndrea Mayer 		pcounters = per_cpu_ptr(slwt->pcpu_counters, i);
151294604548SAndrea Mayer 		do {
151394604548SAndrea Mayer 			start = u64_stats_fetch_begin_irq(&pcounters->syncp);
151494604548SAndrea Mayer 
151594604548SAndrea Mayer 			packets = u64_stats_read(&pcounters->packets);
151694604548SAndrea Mayer 			bytes = u64_stats_read(&pcounters->bytes);
151794604548SAndrea Mayer 			errors = u64_stats_read(&pcounters->errors);
151894604548SAndrea Mayer 
151994604548SAndrea Mayer 		} while (u64_stats_fetch_retry_irq(&pcounters->syncp, start));
152094604548SAndrea Mayer 
152194604548SAndrea Mayer 		counters.packets += packets;
152294604548SAndrea Mayer 		counters.bytes += bytes;
152394604548SAndrea Mayer 		counters.errors += errors;
152494604548SAndrea Mayer 	}
152594604548SAndrea Mayer 
152694604548SAndrea Mayer 	rc = seg6_local_fill_nla_counters(skb, &counters);
152794604548SAndrea Mayer 	if (rc < 0) {
152894604548SAndrea Mayer 		nla_nest_cancel(skb, nest);
152994604548SAndrea Mayer 		return rc;
153094604548SAndrea Mayer 	}
153194604548SAndrea Mayer 
153294604548SAndrea Mayer 	return nla_nest_end(skb, nest);
153394604548SAndrea Mayer }
153494604548SAndrea Mayer 
153594604548SAndrea Mayer static int cmp_nla_counters(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
153694604548SAndrea Mayer {
153794604548SAndrea Mayer 	/* a and b are equal if both have pcpu_counters set or not */
153894604548SAndrea Mayer 	return (!!((unsigned long)a->pcpu_counters)) ^
153994604548SAndrea Mayer 		(!!((unsigned long)b->pcpu_counters));
154094604548SAndrea Mayer }
154194604548SAndrea Mayer 
154294604548SAndrea Mayer static void destroy_attr_counters(struct seg6_local_lwt *slwt)
154394604548SAndrea Mayer {
154494604548SAndrea Mayer 	free_percpu(slwt->pcpu_counters);
154594604548SAndrea Mayer }
154694604548SAndrea Mayer 
1547d1df6fd8SDavid Lebrun struct seg6_action_param {
1548d1df6fd8SDavid Lebrun 	int (*parse)(struct nlattr **attrs, struct seg6_local_lwt *slwt);
1549d1df6fd8SDavid Lebrun 	int (*put)(struct sk_buff *skb, struct seg6_local_lwt *slwt);
1550d1df6fd8SDavid Lebrun 	int (*cmp)(struct seg6_local_lwt *a, struct seg6_local_lwt *b);
1551964adce5SAndrea Mayer 
1552964adce5SAndrea Mayer 	/* optional destroy() callback useful for releasing resources which
1553964adce5SAndrea Mayer 	 * have been previously acquired in the corresponding parse()
1554964adce5SAndrea Mayer 	 * function.
1555964adce5SAndrea Mayer 	 */
1556964adce5SAndrea Mayer 	void (*destroy)(struct seg6_local_lwt *slwt);
1557d1df6fd8SDavid Lebrun };
1558d1df6fd8SDavid Lebrun 
1559d1df6fd8SDavid Lebrun static struct seg6_action_param seg6_action_params[SEG6_LOCAL_MAX + 1] = {
15602d9cc60aSDavid Lebrun 	[SEG6_LOCAL_SRH]	= { .parse = parse_nla_srh,
15612d9cc60aSDavid Lebrun 				    .put = put_nla_srh,
1562964adce5SAndrea Mayer 				    .cmp = cmp_nla_srh,
1563964adce5SAndrea Mayer 				    .destroy = destroy_attr_srh },
1564d1df6fd8SDavid Lebrun 
15652d9cc60aSDavid Lebrun 	[SEG6_LOCAL_TABLE]	= { .parse = parse_nla_table,
15662d9cc60aSDavid Lebrun 				    .put = put_nla_table,
15672d9cc60aSDavid Lebrun 				    .cmp = cmp_nla_table },
1568d1df6fd8SDavid Lebrun 
15692d9cc60aSDavid Lebrun 	[SEG6_LOCAL_NH4]	= { .parse = parse_nla_nh4,
15702d9cc60aSDavid Lebrun 				    .put = put_nla_nh4,
15712d9cc60aSDavid Lebrun 				    .cmp = cmp_nla_nh4 },
1572d1df6fd8SDavid Lebrun 
15732d9cc60aSDavid Lebrun 	[SEG6_LOCAL_NH6]	= { .parse = parse_nla_nh6,
15742d9cc60aSDavid Lebrun 				    .put = put_nla_nh6,
15752d9cc60aSDavid Lebrun 				    .cmp = cmp_nla_nh6 },
1576d1df6fd8SDavid Lebrun 
15772d9cc60aSDavid Lebrun 	[SEG6_LOCAL_IIF]	= { .parse = parse_nla_iif,
15782d9cc60aSDavid Lebrun 				    .put = put_nla_iif,
15792d9cc60aSDavid Lebrun 				    .cmp = cmp_nla_iif },
1580d1df6fd8SDavid Lebrun 
15812d9cc60aSDavid Lebrun 	[SEG6_LOCAL_OIF]	= { .parse = parse_nla_oif,
15822d9cc60aSDavid Lebrun 				    .put = put_nla_oif,
15832d9cc60aSDavid Lebrun 				    .cmp = cmp_nla_oif },
1584004d4b27SMathieu Xhonneux 
1585004d4b27SMathieu Xhonneux 	[SEG6_LOCAL_BPF]	= { .parse = parse_nla_bpf,
1586004d4b27SMathieu Xhonneux 				    .put = put_nla_bpf,
1587964adce5SAndrea Mayer 				    .cmp = cmp_nla_bpf,
1588964adce5SAndrea Mayer 				    .destroy = destroy_attr_bpf },
1589004d4b27SMathieu Xhonneux 
1590664d6f86SAndrea Mayer 	[SEG6_LOCAL_VRFTABLE]	= { .parse = parse_nla_vrftable,
1591664d6f86SAndrea Mayer 				    .put = put_nla_vrftable,
1592664d6f86SAndrea Mayer 				    .cmp = cmp_nla_vrftable },
1593664d6f86SAndrea Mayer 
159494604548SAndrea Mayer 	[SEG6_LOCAL_COUNTERS]	= { .parse = parse_nla_counters,
159594604548SAndrea Mayer 				    .put = put_nla_counters,
159694604548SAndrea Mayer 				    .cmp = cmp_nla_counters,
159794604548SAndrea Mayer 				    .destroy = destroy_attr_counters },
1598d1df6fd8SDavid Lebrun };
1599d1df6fd8SDavid Lebrun 
1600964adce5SAndrea Mayer /* call the destroy() callback (if available) for each set attribute in
16010a3021f1SAndrea Mayer  * @parsed_attrs, starting from the first attribute up to the @max_parsed
16020a3021f1SAndrea Mayer  * (excluded) attribute.
1603964adce5SAndrea Mayer  */
16040a3021f1SAndrea Mayer static void __destroy_attrs(unsigned long parsed_attrs, int max_parsed,
16050a3021f1SAndrea Mayer 			    struct seg6_local_lwt *slwt)
1606964adce5SAndrea Mayer {
1607964adce5SAndrea Mayer 	struct seg6_action_param *param;
1608964adce5SAndrea Mayer 	int i;
1609964adce5SAndrea Mayer 
1610964adce5SAndrea Mayer 	/* Every required seg6local attribute is identified by an ID which is
1611964adce5SAndrea Mayer 	 * encoded as a flag (i.e: 1 << ID) in the 'attrs' bitmask;
1612964adce5SAndrea Mayer 	 *
16130a3021f1SAndrea Mayer 	 * We scan the 'parsed_attrs' bitmask, starting from the first attribute
1614964adce5SAndrea Mayer 	 * up to the @max_parsed (excluded) attribute.
1615964adce5SAndrea Mayer 	 * For each set attribute, we retrieve the corresponding destroy()
1616964adce5SAndrea Mayer 	 * callback. If the callback is not available, then we skip to the next
1617964adce5SAndrea Mayer 	 * attribute; otherwise, we call the destroy() callback.
1618964adce5SAndrea Mayer 	 */
1619964adce5SAndrea Mayer 	for (i = 0; i < max_parsed; ++i) {
1620300a0fd8SAndrea Mayer 		if (!(parsed_attrs & SEG6_F_ATTR(i)))
1621964adce5SAndrea Mayer 			continue;
1622964adce5SAndrea Mayer 
1623964adce5SAndrea Mayer 		param = &seg6_action_params[i];
1624964adce5SAndrea Mayer 
1625964adce5SAndrea Mayer 		if (param->destroy)
1626964adce5SAndrea Mayer 			param->destroy(slwt);
1627964adce5SAndrea Mayer 	}
1628964adce5SAndrea Mayer }
1629964adce5SAndrea Mayer 
1630964adce5SAndrea Mayer /* release all the resources that may have been acquired during parsing
1631964adce5SAndrea Mayer  * operations.
1632964adce5SAndrea Mayer  */
1633964adce5SAndrea Mayer static void destroy_attrs(struct seg6_local_lwt *slwt)
1634964adce5SAndrea Mayer {
16350a3021f1SAndrea Mayer 	unsigned long attrs = slwt->desc->attrs | slwt->parsed_optattrs;
16360a3021f1SAndrea Mayer 
16370a3021f1SAndrea Mayer 	__destroy_attrs(attrs, SEG6_LOCAL_MAX + 1, slwt);
16380a3021f1SAndrea Mayer }
16390a3021f1SAndrea Mayer 
16400a3021f1SAndrea Mayer static int parse_nla_optional_attrs(struct nlattr **attrs,
16410a3021f1SAndrea Mayer 				    struct seg6_local_lwt *slwt)
16420a3021f1SAndrea Mayer {
16430a3021f1SAndrea Mayer 	struct seg6_action_desc *desc = slwt->desc;
16440a3021f1SAndrea Mayer 	unsigned long parsed_optattrs = 0;
16450a3021f1SAndrea Mayer 	struct seg6_action_param *param;
16460a3021f1SAndrea Mayer 	int err, i;
16470a3021f1SAndrea Mayer 
16480a3021f1SAndrea Mayer 	for (i = 0; i < SEG6_LOCAL_MAX + 1; ++i) {
1649300a0fd8SAndrea Mayer 		if (!(desc->optattrs & SEG6_F_ATTR(i)) || !attrs[i])
16500a3021f1SAndrea Mayer 			continue;
16510a3021f1SAndrea Mayer 
16520a3021f1SAndrea Mayer 		/* once here, the i-th attribute is provided by the
16530a3021f1SAndrea Mayer 		 * userspace AND it is identified optional as well.
16540a3021f1SAndrea Mayer 		 */
16550a3021f1SAndrea Mayer 		param = &seg6_action_params[i];
16560a3021f1SAndrea Mayer 
16570a3021f1SAndrea Mayer 		err = param->parse(attrs, slwt);
16580a3021f1SAndrea Mayer 		if (err < 0)
16590a3021f1SAndrea Mayer 			goto parse_optattrs_err;
16600a3021f1SAndrea Mayer 
16610a3021f1SAndrea Mayer 		/* current attribute has been correctly parsed */
1662300a0fd8SAndrea Mayer 		parsed_optattrs |= SEG6_F_ATTR(i);
16630a3021f1SAndrea Mayer 	}
16640a3021f1SAndrea Mayer 
16650a3021f1SAndrea Mayer 	/* store in the tunnel state all the optional attributed successfully
16660a3021f1SAndrea Mayer 	 * parsed.
16670a3021f1SAndrea Mayer 	 */
16680a3021f1SAndrea Mayer 	slwt->parsed_optattrs = parsed_optattrs;
16690a3021f1SAndrea Mayer 
16700a3021f1SAndrea Mayer 	return 0;
16710a3021f1SAndrea Mayer 
16720a3021f1SAndrea Mayer parse_optattrs_err:
16730a3021f1SAndrea Mayer 	__destroy_attrs(parsed_optattrs, i, slwt);
16740a3021f1SAndrea Mayer 
16750a3021f1SAndrea Mayer 	return err;
1676964adce5SAndrea Mayer }
1677964adce5SAndrea Mayer 
1678cfdf64a0SAndrea Mayer /* call the custom constructor of the behavior during its initialization phase
1679cfdf64a0SAndrea Mayer  * and after that all its attributes have been parsed successfully.
1680cfdf64a0SAndrea Mayer  */
1681cfdf64a0SAndrea Mayer static int
1682cfdf64a0SAndrea Mayer seg6_local_lwtunnel_build_state(struct seg6_local_lwt *slwt, const void *cfg,
1683cfdf64a0SAndrea Mayer 				struct netlink_ext_ack *extack)
1684cfdf64a0SAndrea Mayer {
1685cfdf64a0SAndrea Mayer 	struct seg6_action_desc *desc = slwt->desc;
1686cfdf64a0SAndrea Mayer 	struct seg6_local_lwtunnel_ops *ops;
1687cfdf64a0SAndrea Mayer 
1688cfdf64a0SAndrea Mayer 	ops = &desc->slwt_ops;
1689cfdf64a0SAndrea Mayer 	if (!ops->build_state)
1690cfdf64a0SAndrea Mayer 		return 0;
1691cfdf64a0SAndrea Mayer 
1692cfdf64a0SAndrea Mayer 	return ops->build_state(slwt, cfg, extack);
1693cfdf64a0SAndrea Mayer }
1694cfdf64a0SAndrea Mayer 
1695cfdf64a0SAndrea Mayer /* call the custom destructor of the behavior which is invoked before the
1696cfdf64a0SAndrea Mayer  * tunnel is going to be destroyed.
1697cfdf64a0SAndrea Mayer  */
1698cfdf64a0SAndrea Mayer static void seg6_local_lwtunnel_destroy_state(struct seg6_local_lwt *slwt)
1699cfdf64a0SAndrea Mayer {
1700cfdf64a0SAndrea Mayer 	struct seg6_action_desc *desc = slwt->desc;
1701cfdf64a0SAndrea Mayer 	struct seg6_local_lwtunnel_ops *ops;
1702cfdf64a0SAndrea Mayer 
1703cfdf64a0SAndrea Mayer 	ops = &desc->slwt_ops;
1704cfdf64a0SAndrea Mayer 	if (!ops->destroy_state)
1705cfdf64a0SAndrea Mayer 		return;
1706cfdf64a0SAndrea Mayer 
1707cfdf64a0SAndrea Mayer 	ops->destroy_state(slwt);
1708cfdf64a0SAndrea Mayer }
1709cfdf64a0SAndrea Mayer 
1710d1df6fd8SDavid Lebrun static int parse_nla_action(struct nlattr **attrs, struct seg6_local_lwt *slwt)
1711d1df6fd8SDavid Lebrun {
1712d1df6fd8SDavid Lebrun 	struct seg6_action_param *param;
1713d1df6fd8SDavid Lebrun 	struct seg6_action_desc *desc;
17140a3021f1SAndrea Mayer 	unsigned long invalid_attrs;
1715d1df6fd8SDavid Lebrun 	int i, err;
1716d1df6fd8SDavid Lebrun 
1717d1df6fd8SDavid Lebrun 	desc = __get_action_desc(slwt->action);
1718d1df6fd8SDavid Lebrun 	if (!desc)
1719d1df6fd8SDavid Lebrun 		return -EINVAL;
1720d1df6fd8SDavid Lebrun 
1721d1df6fd8SDavid Lebrun 	if (!desc->input)
1722d1df6fd8SDavid Lebrun 		return -EOPNOTSUPP;
1723d1df6fd8SDavid Lebrun 
1724d1df6fd8SDavid Lebrun 	slwt->desc = desc;
1725d1df6fd8SDavid Lebrun 	slwt->headroom += desc->static_headroom;
1726d1df6fd8SDavid Lebrun 
17270a3021f1SAndrea Mayer 	/* Forcing the desc->optattrs *set* and the desc->attrs *set* to be
17280a3021f1SAndrea Mayer 	 * disjoined, this allow us to release acquired resources by optional
17290a3021f1SAndrea Mayer 	 * attributes and by required attributes independently from each other
17300d770360SAndrea Mayer 	 * without any interference.
17310a3021f1SAndrea Mayer 	 * In other terms, we are sure that we do not release some the acquired
17320a3021f1SAndrea Mayer 	 * resources twice.
17330a3021f1SAndrea Mayer 	 *
17340a3021f1SAndrea Mayer 	 * Note that if an attribute is configured both as required and as
17350a3021f1SAndrea Mayer 	 * optional, it means that the user has messed something up in the
17360a3021f1SAndrea Mayer 	 * seg6_action_table. Therefore, this check is required for SRv6
17370a3021f1SAndrea Mayer 	 * behaviors to work properly.
17380a3021f1SAndrea Mayer 	 */
17390a3021f1SAndrea Mayer 	invalid_attrs = desc->attrs & desc->optattrs;
17400a3021f1SAndrea Mayer 	if (invalid_attrs) {
17410a3021f1SAndrea Mayer 		WARN_ONCE(1,
17420a3021f1SAndrea Mayer 			  "An attribute cannot be both required AND optional");
17430a3021f1SAndrea Mayer 		return -EINVAL;
17440a3021f1SAndrea Mayer 	}
17450a3021f1SAndrea Mayer 
17460a3021f1SAndrea Mayer 	/* parse the required attributes */
1747d1df6fd8SDavid Lebrun 	for (i = 0; i < SEG6_LOCAL_MAX + 1; i++) {
1748300a0fd8SAndrea Mayer 		if (desc->attrs & SEG6_F_ATTR(i)) {
1749d1df6fd8SDavid Lebrun 			if (!attrs[i])
1750d1df6fd8SDavid Lebrun 				return -EINVAL;
1751d1df6fd8SDavid Lebrun 
1752d1df6fd8SDavid Lebrun 			param = &seg6_action_params[i];
1753d1df6fd8SDavid Lebrun 
1754d1df6fd8SDavid Lebrun 			err = param->parse(attrs, slwt);
1755d1df6fd8SDavid Lebrun 			if (err < 0)
17560a3021f1SAndrea Mayer 				goto parse_attrs_err;
1757d1df6fd8SDavid Lebrun 		}
1758d1df6fd8SDavid Lebrun 	}
1759d1df6fd8SDavid Lebrun 
17600a3021f1SAndrea Mayer 	/* parse the optional attributes, if any */
17610a3021f1SAndrea Mayer 	err = parse_nla_optional_attrs(attrs, slwt);
17620a3021f1SAndrea Mayer 	if (err < 0)
17630a3021f1SAndrea Mayer 		goto parse_attrs_err;
17640a3021f1SAndrea Mayer 
1765d1df6fd8SDavid Lebrun 	return 0;
1766964adce5SAndrea Mayer 
17670a3021f1SAndrea Mayer parse_attrs_err:
1768964adce5SAndrea Mayer 	/* release any resource that may have been acquired during the i-1
1769964adce5SAndrea Mayer 	 * parse() operations.
1770964adce5SAndrea Mayer 	 */
17710a3021f1SAndrea Mayer 	__destroy_attrs(desc->attrs, i, slwt);
1772964adce5SAndrea Mayer 
1773964adce5SAndrea Mayer 	return err;
1774d1df6fd8SDavid Lebrun }
1775d1df6fd8SDavid Lebrun 
1776faee6769SAlexander Aring static int seg6_local_build_state(struct net *net, struct nlattr *nla,
1777faee6769SAlexander Aring 				  unsigned int family, const void *cfg,
1778faee6769SAlexander Aring 				  struct lwtunnel_state **ts,
1779d1df6fd8SDavid Lebrun 				  struct netlink_ext_ack *extack)
1780d1df6fd8SDavid Lebrun {
1781d1df6fd8SDavid Lebrun 	struct nlattr *tb[SEG6_LOCAL_MAX + 1];
1782d1df6fd8SDavid Lebrun 	struct lwtunnel_state *newts;
1783d1df6fd8SDavid Lebrun 	struct seg6_local_lwt *slwt;
1784d1df6fd8SDavid Lebrun 	int err;
1785d1df6fd8SDavid Lebrun 
17866285217fSDavid Lebrun 	if (family != AF_INET6)
17876285217fSDavid Lebrun 		return -EINVAL;
17886285217fSDavid Lebrun 
17898cb08174SJohannes Berg 	err = nla_parse_nested_deprecated(tb, SEG6_LOCAL_MAX, nla,
17908cb08174SJohannes Berg 					  seg6_local_policy, extack);
1791d1df6fd8SDavid Lebrun 
1792d1df6fd8SDavid Lebrun 	if (err < 0)
1793d1df6fd8SDavid Lebrun 		return err;
1794d1df6fd8SDavid Lebrun 
1795d1df6fd8SDavid Lebrun 	if (!tb[SEG6_LOCAL_ACTION])
1796d1df6fd8SDavid Lebrun 		return -EINVAL;
1797d1df6fd8SDavid Lebrun 
1798d1df6fd8SDavid Lebrun 	newts = lwtunnel_state_alloc(sizeof(*slwt));
1799d1df6fd8SDavid Lebrun 	if (!newts)
1800d1df6fd8SDavid Lebrun 		return -ENOMEM;
1801d1df6fd8SDavid Lebrun 
1802d1df6fd8SDavid Lebrun 	slwt = seg6_local_lwtunnel(newts);
1803d1df6fd8SDavid Lebrun 	slwt->action = nla_get_u32(tb[SEG6_LOCAL_ACTION]);
1804d1df6fd8SDavid Lebrun 
1805d1df6fd8SDavid Lebrun 	err = parse_nla_action(tb, slwt);
1806d1df6fd8SDavid Lebrun 	if (err < 0)
1807d1df6fd8SDavid Lebrun 		goto out_free;
1808d1df6fd8SDavid Lebrun 
1809cfdf64a0SAndrea Mayer 	err = seg6_local_lwtunnel_build_state(slwt, cfg, extack);
1810cfdf64a0SAndrea Mayer 	if (err < 0)
1811cfdf64a0SAndrea Mayer 		goto out_destroy_attrs;
1812cfdf64a0SAndrea Mayer 
1813d1df6fd8SDavid Lebrun 	newts->type = LWTUNNEL_ENCAP_SEG6_LOCAL;
1814d1df6fd8SDavid Lebrun 	newts->flags = LWTUNNEL_STATE_INPUT_REDIRECT;
1815d1df6fd8SDavid Lebrun 	newts->headroom = slwt->headroom;
1816d1df6fd8SDavid Lebrun 
1817d1df6fd8SDavid Lebrun 	*ts = newts;
1818d1df6fd8SDavid Lebrun 
1819d1df6fd8SDavid Lebrun 	return 0;
1820d1df6fd8SDavid Lebrun 
1821cfdf64a0SAndrea Mayer out_destroy_attrs:
1822cfdf64a0SAndrea Mayer 	destroy_attrs(slwt);
1823d1df6fd8SDavid Lebrun out_free:
1824d1df6fd8SDavid Lebrun 	kfree(newts);
1825d1df6fd8SDavid Lebrun 	return err;
1826d1df6fd8SDavid Lebrun }
1827d1df6fd8SDavid Lebrun 
1828d1df6fd8SDavid Lebrun static void seg6_local_destroy_state(struct lwtunnel_state *lwt)
1829d1df6fd8SDavid Lebrun {
1830d1df6fd8SDavid Lebrun 	struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
1831d1df6fd8SDavid Lebrun 
1832cfdf64a0SAndrea Mayer 	seg6_local_lwtunnel_destroy_state(slwt);
1833cfdf64a0SAndrea Mayer 
1834964adce5SAndrea Mayer 	destroy_attrs(slwt);
1835004d4b27SMathieu Xhonneux 
1836004d4b27SMathieu Xhonneux 	return;
1837d1df6fd8SDavid Lebrun }
1838d1df6fd8SDavid Lebrun 
1839d1df6fd8SDavid Lebrun static int seg6_local_fill_encap(struct sk_buff *skb,
1840d1df6fd8SDavid Lebrun 				 struct lwtunnel_state *lwt)
1841d1df6fd8SDavid Lebrun {
1842d1df6fd8SDavid Lebrun 	struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
1843d1df6fd8SDavid Lebrun 	struct seg6_action_param *param;
18440a3021f1SAndrea Mayer 	unsigned long attrs;
1845d1df6fd8SDavid Lebrun 	int i, err;
1846d1df6fd8SDavid Lebrun 
1847d1df6fd8SDavid Lebrun 	if (nla_put_u32(skb, SEG6_LOCAL_ACTION, slwt->action))
1848d1df6fd8SDavid Lebrun 		return -EMSGSIZE;
1849d1df6fd8SDavid Lebrun 
18500a3021f1SAndrea Mayer 	attrs = slwt->desc->attrs | slwt->parsed_optattrs;
18510a3021f1SAndrea Mayer 
1852d1df6fd8SDavid Lebrun 	for (i = 0; i < SEG6_LOCAL_MAX + 1; i++) {
1853300a0fd8SAndrea Mayer 		if (attrs & SEG6_F_ATTR(i)) {
1854d1df6fd8SDavid Lebrun 			param = &seg6_action_params[i];
1855d1df6fd8SDavid Lebrun 			err = param->put(skb, slwt);
1856d1df6fd8SDavid Lebrun 			if (err < 0)
1857d1df6fd8SDavid Lebrun 				return err;
1858d1df6fd8SDavid Lebrun 		}
1859d1df6fd8SDavid Lebrun 	}
1860d1df6fd8SDavid Lebrun 
1861d1df6fd8SDavid Lebrun 	return 0;
1862d1df6fd8SDavid Lebrun }
1863d1df6fd8SDavid Lebrun 
1864d1df6fd8SDavid Lebrun static int seg6_local_get_encap_size(struct lwtunnel_state *lwt)
1865d1df6fd8SDavid Lebrun {
1866d1df6fd8SDavid Lebrun 	struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
1867d1df6fd8SDavid Lebrun 	unsigned long attrs;
1868d1df6fd8SDavid Lebrun 	int nlsize;
1869d1df6fd8SDavid Lebrun 
1870d1df6fd8SDavid Lebrun 	nlsize = nla_total_size(4); /* action */
1871d1df6fd8SDavid Lebrun 
18720a3021f1SAndrea Mayer 	attrs = slwt->desc->attrs | slwt->parsed_optattrs;
1873d1df6fd8SDavid Lebrun 
1874300a0fd8SAndrea Mayer 	if (attrs & SEG6_F_ATTR(SEG6_LOCAL_SRH))
1875d1df6fd8SDavid Lebrun 		nlsize += nla_total_size((slwt->srh->hdrlen + 1) << 3);
1876d1df6fd8SDavid Lebrun 
1877300a0fd8SAndrea Mayer 	if (attrs & SEG6_F_ATTR(SEG6_LOCAL_TABLE))
1878d1df6fd8SDavid Lebrun 		nlsize += nla_total_size(4);
1879d1df6fd8SDavid Lebrun 
1880300a0fd8SAndrea Mayer 	if (attrs & SEG6_F_ATTR(SEG6_LOCAL_NH4))
1881d1df6fd8SDavid Lebrun 		nlsize += nla_total_size(4);
1882d1df6fd8SDavid Lebrun 
1883300a0fd8SAndrea Mayer 	if (attrs & SEG6_F_ATTR(SEG6_LOCAL_NH6))
1884d1df6fd8SDavid Lebrun 		nlsize += nla_total_size(16);
1885d1df6fd8SDavid Lebrun 
1886300a0fd8SAndrea Mayer 	if (attrs & SEG6_F_ATTR(SEG6_LOCAL_IIF))
1887d1df6fd8SDavid Lebrun 		nlsize += nla_total_size(4);
1888d1df6fd8SDavid Lebrun 
1889300a0fd8SAndrea Mayer 	if (attrs & SEG6_F_ATTR(SEG6_LOCAL_OIF))
1890d1df6fd8SDavid Lebrun 		nlsize += nla_total_size(4);
1891d1df6fd8SDavid Lebrun 
1892300a0fd8SAndrea Mayer 	if (attrs & SEG6_F_ATTR(SEG6_LOCAL_BPF))
1893004d4b27SMathieu Xhonneux 		nlsize += nla_total_size(sizeof(struct nlattr)) +
1894004d4b27SMathieu Xhonneux 		       nla_total_size(MAX_PROG_NAME) +
1895004d4b27SMathieu Xhonneux 		       nla_total_size(4);
1896004d4b27SMathieu Xhonneux 
1897300a0fd8SAndrea Mayer 	if (attrs & SEG6_F_ATTR(SEG6_LOCAL_VRFTABLE))
1898664d6f86SAndrea Mayer 		nlsize += nla_total_size(4);
1899664d6f86SAndrea Mayer 
190094604548SAndrea Mayer 	if (attrs & SEG6_F_LOCAL_COUNTERS)
190194604548SAndrea Mayer 		nlsize += nla_total_size(0) + /* nest SEG6_LOCAL_COUNTERS */
190294604548SAndrea Mayer 			  /* SEG6_LOCAL_CNT_PACKETS */
190394604548SAndrea Mayer 			  nla_total_size_64bit(sizeof(__u64)) +
190494604548SAndrea Mayer 			  /* SEG6_LOCAL_CNT_BYTES */
190594604548SAndrea Mayer 			  nla_total_size_64bit(sizeof(__u64)) +
190694604548SAndrea Mayer 			  /* SEG6_LOCAL_CNT_ERRORS */
190794604548SAndrea Mayer 			  nla_total_size_64bit(sizeof(__u64));
190894604548SAndrea Mayer 
1909d1df6fd8SDavid Lebrun 	return nlsize;
1910d1df6fd8SDavid Lebrun }
1911d1df6fd8SDavid Lebrun 
1912d1df6fd8SDavid Lebrun static int seg6_local_cmp_encap(struct lwtunnel_state *a,
1913d1df6fd8SDavid Lebrun 				struct lwtunnel_state *b)
1914d1df6fd8SDavid Lebrun {
1915d1df6fd8SDavid Lebrun 	struct seg6_local_lwt *slwt_a, *slwt_b;
1916d1df6fd8SDavid Lebrun 	struct seg6_action_param *param;
19170a3021f1SAndrea Mayer 	unsigned long attrs_a, attrs_b;
1918d1df6fd8SDavid Lebrun 	int i;
1919d1df6fd8SDavid Lebrun 
1920d1df6fd8SDavid Lebrun 	slwt_a = seg6_local_lwtunnel(a);
1921d1df6fd8SDavid Lebrun 	slwt_b = seg6_local_lwtunnel(b);
1922d1df6fd8SDavid Lebrun 
1923d1df6fd8SDavid Lebrun 	if (slwt_a->action != slwt_b->action)
1924d1df6fd8SDavid Lebrun 		return 1;
1925d1df6fd8SDavid Lebrun 
19260a3021f1SAndrea Mayer 	attrs_a = slwt_a->desc->attrs | slwt_a->parsed_optattrs;
19270a3021f1SAndrea Mayer 	attrs_b = slwt_b->desc->attrs | slwt_b->parsed_optattrs;
19280a3021f1SAndrea Mayer 
19290a3021f1SAndrea Mayer 	if (attrs_a != attrs_b)
1930d1df6fd8SDavid Lebrun 		return 1;
1931d1df6fd8SDavid Lebrun 
1932d1df6fd8SDavid Lebrun 	for (i = 0; i < SEG6_LOCAL_MAX + 1; i++) {
1933300a0fd8SAndrea Mayer 		if (attrs_a & SEG6_F_ATTR(i)) {
1934d1df6fd8SDavid Lebrun 			param = &seg6_action_params[i];
1935d1df6fd8SDavid Lebrun 			if (param->cmp(slwt_a, slwt_b))
1936d1df6fd8SDavid Lebrun 				return 1;
1937d1df6fd8SDavid Lebrun 		}
1938d1df6fd8SDavid Lebrun 	}
1939d1df6fd8SDavid Lebrun 
1940d1df6fd8SDavid Lebrun 	return 0;
1941d1df6fd8SDavid Lebrun }
1942d1df6fd8SDavid Lebrun 
1943d1df6fd8SDavid Lebrun static const struct lwtunnel_encap_ops seg6_local_ops = {
1944d1df6fd8SDavid Lebrun 	.build_state	= seg6_local_build_state,
1945d1df6fd8SDavid Lebrun 	.destroy_state	= seg6_local_destroy_state,
1946d1df6fd8SDavid Lebrun 	.input		= seg6_local_input,
1947d1df6fd8SDavid Lebrun 	.fill_encap	= seg6_local_fill_encap,
1948d1df6fd8SDavid Lebrun 	.get_encap_size	= seg6_local_get_encap_size,
1949d1df6fd8SDavid Lebrun 	.cmp_encap	= seg6_local_cmp_encap,
1950d1df6fd8SDavid Lebrun 	.owner		= THIS_MODULE,
1951d1df6fd8SDavid Lebrun };
1952d1df6fd8SDavid Lebrun 
1953d1df6fd8SDavid Lebrun int __init seg6_local_init(void)
1954d1df6fd8SDavid Lebrun {
1955300a0fd8SAndrea Mayer 	/* If the max total number of defined attributes is reached, then your
1956300a0fd8SAndrea Mayer 	 * kernel build stops here.
1957300a0fd8SAndrea Mayer 	 *
1958300a0fd8SAndrea Mayer 	 * This check is required to avoid arithmetic overflows when processing
1959300a0fd8SAndrea Mayer 	 * behavior attributes and the maximum number of defined attributes
1960300a0fd8SAndrea Mayer 	 * exceeds the allowed value.
1961300a0fd8SAndrea Mayer 	 */
1962300a0fd8SAndrea Mayer 	BUILD_BUG_ON(SEG6_LOCAL_MAX + 1 > BITS_PER_TYPE(unsigned long));
1963300a0fd8SAndrea Mayer 
1964d1df6fd8SDavid Lebrun 	return lwtunnel_encap_add_ops(&seg6_local_ops,
1965d1df6fd8SDavid Lebrun 				      LWTUNNEL_ENCAP_SEG6_LOCAL);
1966d1df6fd8SDavid Lebrun }
1967d1df6fd8SDavid Lebrun 
1968d1df6fd8SDavid Lebrun void seg6_local_exit(void)
1969d1df6fd8SDavid Lebrun {
1970d1df6fd8SDavid Lebrun 	lwtunnel_encap_del_ops(&seg6_local_ops, LWTUNNEL_ENCAP_SEG6_LOCAL);
1971d1df6fd8SDavid Lebrun }
1972