xref: /openbmc/linux/net/ipv6/seg6_local.c (revision 004d4b274e2a1a895a0e5dc66158b90a7d463d44)
1d1df6fd8SDavid Lebrun /*
2d1df6fd8SDavid Lebrun  *  SR-IPv6 implementation
3d1df6fd8SDavid Lebrun  *
4*004d4b27SMathieu Xhonneux  *  Authors:
5d1df6fd8SDavid Lebrun  *  David Lebrun <david.lebrun@uclouvain.be>
6*004d4b27SMathieu Xhonneux  *  eBPF support: Mathieu Xhonneux <m.xhonneux@gmail.com>
7d1df6fd8SDavid Lebrun  *
8d1df6fd8SDavid Lebrun  *
9d1df6fd8SDavid Lebrun  *  This program is free software; you can redistribute it and/or
10d1df6fd8SDavid Lebrun  *        modify it under the terms of the GNU General Public License
11d1df6fd8SDavid Lebrun  *        as published by the Free Software Foundation; either version
12d1df6fd8SDavid Lebrun  *        2 of the License, or (at your option) any later version.
13d1df6fd8SDavid Lebrun  */
14d1df6fd8SDavid Lebrun 
15d1df6fd8SDavid Lebrun #include <linux/types.h>
16d1df6fd8SDavid Lebrun #include <linux/skbuff.h>
17d1df6fd8SDavid Lebrun #include <linux/net.h>
18d1df6fd8SDavid Lebrun #include <linux/module.h>
19d1df6fd8SDavid Lebrun #include <net/ip.h>
20d1df6fd8SDavid Lebrun #include <net/lwtunnel.h>
21d1df6fd8SDavid Lebrun #include <net/netevent.h>
22d1df6fd8SDavid Lebrun #include <net/netns/generic.h>
23d1df6fd8SDavid Lebrun #include <net/ip6_fib.h>
24d1df6fd8SDavid Lebrun #include <net/route.h>
25d1df6fd8SDavid Lebrun #include <net/seg6.h>
26d1df6fd8SDavid Lebrun #include <linux/seg6.h>
27d1df6fd8SDavid Lebrun #include <linux/seg6_local.h>
28d1df6fd8SDavid Lebrun #include <net/addrconf.h>
29d1df6fd8SDavid Lebrun #include <net/ip6_route.h>
30d1df6fd8SDavid Lebrun #include <net/dst_cache.h>
31d1df6fd8SDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC
32d1df6fd8SDavid Lebrun #include <net/seg6_hmac.h>
33d1df6fd8SDavid Lebrun #endif
341c1e761eSMathieu Xhonneux #include <net/seg6_local.h>
35891ef8ddSDavid Lebrun #include <linux/etherdevice.h>
36*004d4b27SMathieu Xhonneux #include <linux/bpf.h>
37d1df6fd8SDavid Lebrun 
38d1df6fd8SDavid Lebrun struct seg6_local_lwt;
39d1df6fd8SDavid Lebrun 
40d1df6fd8SDavid Lebrun struct seg6_action_desc {
41d1df6fd8SDavid Lebrun 	int action;
42d1df6fd8SDavid Lebrun 	unsigned long attrs;
43d1df6fd8SDavid Lebrun 	int (*input)(struct sk_buff *skb, struct seg6_local_lwt *slwt);
44d1df6fd8SDavid Lebrun 	int static_headroom;
45d1df6fd8SDavid Lebrun };
46d1df6fd8SDavid Lebrun 
47*004d4b27SMathieu Xhonneux struct bpf_lwt_prog {
48*004d4b27SMathieu Xhonneux 	struct bpf_prog *prog;
49*004d4b27SMathieu Xhonneux 	char *name;
50*004d4b27SMathieu Xhonneux };
51*004d4b27SMathieu Xhonneux 
52d1df6fd8SDavid Lebrun struct seg6_local_lwt {
53d1df6fd8SDavid Lebrun 	int action;
54d1df6fd8SDavid Lebrun 	struct ipv6_sr_hdr *srh;
55d1df6fd8SDavid Lebrun 	int table;
56d1df6fd8SDavid Lebrun 	struct in_addr nh4;
57d1df6fd8SDavid Lebrun 	struct in6_addr nh6;
58d1df6fd8SDavid Lebrun 	int iif;
59d1df6fd8SDavid Lebrun 	int oif;
60*004d4b27SMathieu Xhonneux 	struct bpf_lwt_prog bpf;
61d1df6fd8SDavid Lebrun 
62d1df6fd8SDavid Lebrun 	int headroom;
63d1df6fd8SDavid Lebrun 	struct seg6_action_desc *desc;
64d1df6fd8SDavid Lebrun };
65d1df6fd8SDavid Lebrun 
66d1df6fd8SDavid Lebrun static struct seg6_local_lwt *seg6_local_lwtunnel(struct lwtunnel_state *lwt)
67d1df6fd8SDavid Lebrun {
68d1df6fd8SDavid Lebrun 	return (struct seg6_local_lwt *)lwt->data;
69d1df6fd8SDavid Lebrun }
70d1df6fd8SDavid Lebrun 
71140f04c3SDavid Lebrun static struct ipv6_sr_hdr *get_srh(struct sk_buff *skb)
72140f04c3SDavid Lebrun {
73140f04c3SDavid Lebrun 	struct ipv6_sr_hdr *srh;
745829d70bSAhmed Abdelsalam 	int len, srhoff = 0;
75140f04c3SDavid Lebrun 
765829d70bSAhmed Abdelsalam 	if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
77140f04c3SDavid Lebrun 		return NULL;
78140f04c3SDavid Lebrun 
795829d70bSAhmed Abdelsalam 	if (!pskb_may_pull(skb, srhoff + sizeof(*srh)))
805829d70bSAhmed Abdelsalam 		return NULL;
815829d70bSAhmed Abdelsalam 
825829d70bSAhmed Abdelsalam 	srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
835829d70bSAhmed Abdelsalam 
84140f04c3SDavid Lebrun 	len = (srh->hdrlen + 1) << 3;
85140f04c3SDavid Lebrun 
865829d70bSAhmed Abdelsalam 	if (!pskb_may_pull(skb, srhoff + len))
87140f04c3SDavid Lebrun 		return NULL;
88140f04c3SDavid Lebrun 
89140f04c3SDavid Lebrun 	if (!seg6_validate_srh(srh, len))
90140f04c3SDavid Lebrun 		return NULL;
91140f04c3SDavid Lebrun 
92140f04c3SDavid Lebrun 	return srh;
93140f04c3SDavid Lebrun }
94140f04c3SDavid Lebrun 
95140f04c3SDavid Lebrun static struct ipv6_sr_hdr *get_and_validate_srh(struct sk_buff *skb)
96140f04c3SDavid Lebrun {
97140f04c3SDavid Lebrun 	struct ipv6_sr_hdr *srh;
98140f04c3SDavid Lebrun 
99140f04c3SDavid Lebrun 	srh = get_srh(skb);
100140f04c3SDavid Lebrun 	if (!srh)
101140f04c3SDavid Lebrun 		return NULL;
102140f04c3SDavid Lebrun 
103140f04c3SDavid Lebrun 	if (srh->segments_left == 0)
104140f04c3SDavid Lebrun 		return NULL;
105140f04c3SDavid Lebrun 
106140f04c3SDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC
107140f04c3SDavid Lebrun 	if (!seg6_hmac_validate_skb(skb))
108140f04c3SDavid Lebrun 		return NULL;
109140f04c3SDavid Lebrun #endif
110140f04c3SDavid Lebrun 
111140f04c3SDavid Lebrun 	return srh;
112140f04c3SDavid Lebrun }
113140f04c3SDavid Lebrun 
114d7a669ddSDavid Lebrun static bool decap_and_validate(struct sk_buff *skb, int proto)
115d7a669ddSDavid Lebrun {
116d7a669ddSDavid Lebrun 	struct ipv6_sr_hdr *srh;
117d7a669ddSDavid Lebrun 	unsigned int off = 0;
118d7a669ddSDavid Lebrun 
119d7a669ddSDavid Lebrun 	srh = get_srh(skb);
120d7a669ddSDavid Lebrun 	if (srh && srh->segments_left > 0)
121d7a669ddSDavid Lebrun 		return false;
122d7a669ddSDavid Lebrun 
123d7a669ddSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC
124d7a669ddSDavid Lebrun 	if (srh && !seg6_hmac_validate_skb(skb))
125d7a669ddSDavid Lebrun 		return false;
126d7a669ddSDavid Lebrun #endif
127d7a669ddSDavid Lebrun 
128d7a669ddSDavid Lebrun 	if (ipv6_find_hdr(skb, &off, proto, NULL, NULL) < 0)
129d7a669ddSDavid Lebrun 		return false;
130d7a669ddSDavid Lebrun 
131d7a669ddSDavid Lebrun 	if (!pskb_pull(skb, off))
132d7a669ddSDavid Lebrun 		return false;
133d7a669ddSDavid Lebrun 
134d7a669ddSDavid Lebrun 	skb_postpull_rcsum(skb, skb_network_header(skb), off);
135d7a669ddSDavid Lebrun 
136d7a669ddSDavid Lebrun 	skb_reset_network_header(skb);
137d7a669ddSDavid Lebrun 	skb_reset_transport_header(skb);
138d7a669ddSDavid Lebrun 	skb->encapsulation = 0;
139d7a669ddSDavid Lebrun 
140d7a669ddSDavid Lebrun 	return true;
141d7a669ddSDavid Lebrun }
142d7a669ddSDavid Lebrun 
143d7a669ddSDavid Lebrun static void advance_nextseg(struct ipv6_sr_hdr *srh, struct in6_addr *daddr)
144d7a669ddSDavid Lebrun {
145d7a669ddSDavid Lebrun 	struct in6_addr *addr;
146d7a669ddSDavid Lebrun 
147d7a669ddSDavid Lebrun 	srh->segments_left--;
148d7a669ddSDavid Lebrun 	addr = srh->segments + srh->segments_left;
149d7a669ddSDavid Lebrun 	*daddr = *addr;
150d7a669ddSDavid Lebrun }
151d7a669ddSDavid Lebrun 
1521c1e761eSMathieu Xhonneux int seg6_lookup_nexthop(struct sk_buff *skb, struct in6_addr *nhaddr,
153d7a669ddSDavid Lebrun 			u32 tbl_id)
154d7a669ddSDavid Lebrun {
155d7a669ddSDavid Lebrun 	struct net *net = dev_net(skb->dev);
156d7a669ddSDavid Lebrun 	struct ipv6hdr *hdr = ipv6_hdr(skb);
157d7a669ddSDavid Lebrun 	int flags = RT6_LOOKUP_F_HAS_SADDR;
158d7a669ddSDavid Lebrun 	struct dst_entry *dst = NULL;
159d7a669ddSDavid Lebrun 	struct rt6_info *rt;
160d7a669ddSDavid Lebrun 	struct flowi6 fl6;
161d7a669ddSDavid Lebrun 
162d7a669ddSDavid Lebrun 	fl6.flowi6_iif = skb->dev->ifindex;
163d7a669ddSDavid Lebrun 	fl6.daddr = nhaddr ? *nhaddr : hdr->daddr;
164d7a669ddSDavid Lebrun 	fl6.saddr = hdr->saddr;
165d7a669ddSDavid Lebrun 	fl6.flowlabel = ip6_flowinfo(hdr);
166d7a669ddSDavid Lebrun 	fl6.flowi6_mark = skb->mark;
167d7a669ddSDavid Lebrun 	fl6.flowi6_proto = hdr->nexthdr;
168d7a669ddSDavid Lebrun 
169d7a669ddSDavid Lebrun 	if (nhaddr)
170d7a669ddSDavid Lebrun 		fl6.flowi6_flags = FLOWI_FLAG_KNOWN_NH;
171d7a669ddSDavid Lebrun 
172d7a669ddSDavid Lebrun 	if (!tbl_id) {
173b75cc8f9SDavid Ahern 		dst = ip6_route_input_lookup(net, skb->dev, &fl6, skb, flags);
174d7a669ddSDavid Lebrun 	} else {
175d7a669ddSDavid Lebrun 		struct fib6_table *table;
176d7a669ddSDavid Lebrun 
177d7a669ddSDavid Lebrun 		table = fib6_get_table(net, tbl_id);
178d7a669ddSDavid Lebrun 		if (!table)
179d7a669ddSDavid Lebrun 			goto out;
180d7a669ddSDavid Lebrun 
181b75cc8f9SDavid Ahern 		rt = ip6_pol_route(net, table, 0, &fl6, skb, flags);
182d7a669ddSDavid Lebrun 		dst = &rt->dst;
183d7a669ddSDavid Lebrun 	}
184d7a669ddSDavid Lebrun 
185d7a669ddSDavid Lebrun 	if (dst && dst->dev->flags & IFF_LOOPBACK && !dst->error) {
186d7a669ddSDavid Lebrun 		dst_release(dst);
187d7a669ddSDavid Lebrun 		dst = NULL;
188d7a669ddSDavid Lebrun 	}
189d7a669ddSDavid Lebrun 
190d7a669ddSDavid Lebrun out:
191d7a669ddSDavid Lebrun 	if (!dst) {
192d7a669ddSDavid Lebrun 		rt = net->ipv6.ip6_blk_hole_entry;
193d7a669ddSDavid Lebrun 		dst = &rt->dst;
194d7a669ddSDavid Lebrun 		dst_hold(dst);
195d7a669ddSDavid Lebrun 	}
196d7a669ddSDavid Lebrun 
197d7a669ddSDavid Lebrun 	skb_dst_drop(skb);
198d7a669ddSDavid Lebrun 	skb_dst_set(skb, dst);
1991c1e761eSMathieu Xhonneux 	return dst->error;
200d7a669ddSDavid Lebrun }
201d7a669ddSDavid Lebrun 
202140f04c3SDavid Lebrun /* regular endpoint function */
203140f04c3SDavid Lebrun static int input_action_end(struct sk_buff *skb, struct seg6_local_lwt *slwt)
204140f04c3SDavid Lebrun {
205140f04c3SDavid Lebrun 	struct ipv6_sr_hdr *srh;
206140f04c3SDavid Lebrun 
207140f04c3SDavid Lebrun 	srh = get_and_validate_srh(skb);
208140f04c3SDavid Lebrun 	if (!srh)
209140f04c3SDavid Lebrun 		goto drop;
210140f04c3SDavid Lebrun 
211d7a669ddSDavid Lebrun 	advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
212140f04c3SDavid Lebrun 
2131c1e761eSMathieu Xhonneux 	seg6_lookup_nexthop(skb, NULL, 0);
214140f04c3SDavid Lebrun 
215140f04c3SDavid Lebrun 	return dst_input(skb);
216140f04c3SDavid Lebrun 
217140f04c3SDavid Lebrun drop:
218140f04c3SDavid Lebrun 	kfree_skb(skb);
219140f04c3SDavid Lebrun 	return -EINVAL;
220140f04c3SDavid Lebrun }
221140f04c3SDavid Lebrun 
222140f04c3SDavid Lebrun /* regular endpoint, and forward to specified nexthop */
223140f04c3SDavid Lebrun static int input_action_end_x(struct sk_buff *skb, struct seg6_local_lwt *slwt)
224140f04c3SDavid Lebrun {
225140f04c3SDavid Lebrun 	struct ipv6_sr_hdr *srh;
226140f04c3SDavid Lebrun 
227140f04c3SDavid Lebrun 	srh = get_and_validate_srh(skb);
228140f04c3SDavid Lebrun 	if (!srh)
229140f04c3SDavid Lebrun 		goto drop;
230140f04c3SDavid Lebrun 
231d7a669ddSDavid Lebrun 	advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
232140f04c3SDavid Lebrun 
2331c1e761eSMathieu Xhonneux 	seg6_lookup_nexthop(skb, &slwt->nh6, 0);
234140f04c3SDavid Lebrun 
235140f04c3SDavid Lebrun 	return dst_input(skb);
236140f04c3SDavid Lebrun 
237140f04c3SDavid Lebrun drop:
238140f04c3SDavid Lebrun 	kfree_skb(skb);
239140f04c3SDavid Lebrun 	return -EINVAL;
240140f04c3SDavid Lebrun }
241140f04c3SDavid Lebrun 
242891ef8ddSDavid Lebrun static int input_action_end_t(struct sk_buff *skb, struct seg6_local_lwt *slwt)
243891ef8ddSDavid Lebrun {
244891ef8ddSDavid Lebrun 	struct ipv6_sr_hdr *srh;
245891ef8ddSDavid Lebrun 
246891ef8ddSDavid Lebrun 	srh = get_and_validate_srh(skb);
247891ef8ddSDavid Lebrun 	if (!srh)
248891ef8ddSDavid Lebrun 		goto drop;
249891ef8ddSDavid Lebrun 
250891ef8ddSDavid Lebrun 	advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
251891ef8ddSDavid Lebrun 
2521c1e761eSMathieu Xhonneux 	seg6_lookup_nexthop(skb, NULL, slwt->table);
253891ef8ddSDavid Lebrun 
254891ef8ddSDavid Lebrun 	return dst_input(skb);
255891ef8ddSDavid Lebrun 
256891ef8ddSDavid Lebrun drop:
257891ef8ddSDavid Lebrun 	kfree_skb(skb);
258891ef8ddSDavid Lebrun 	return -EINVAL;
259891ef8ddSDavid Lebrun }
260891ef8ddSDavid Lebrun 
261891ef8ddSDavid Lebrun /* decapsulate and forward inner L2 frame on specified interface */
262891ef8ddSDavid Lebrun static int input_action_end_dx2(struct sk_buff *skb,
263891ef8ddSDavid Lebrun 				struct seg6_local_lwt *slwt)
264891ef8ddSDavid Lebrun {
265891ef8ddSDavid Lebrun 	struct net *net = dev_net(skb->dev);
266891ef8ddSDavid Lebrun 	struct net_device *odev;
267891ef8ddSDavid Lebrun 	struct ethhdr *eth;
268891ef8ddSDavid Lebrun 
269891ef8ddSDavid Lebrun 	if (!decap_and_validate(skb, NEXTHDR_NONE))
270891ef8ddSDavid Lebrun 		goto drop;
271891ef8ddSDavid Lebrun 
272891ef8ddSDavid Lebrun 	if (!pskb_may_pull(skb, ETH_HLEN))
273891ef8ddSDavid Lebrun 		goto drop;
274891ef8ddSDavid Lebrun 
275891ef8ddSDavid Lebrun 	skb_reset_mac_header(skb);
276891ef8ddSDavid Lebrun 	eth = (struct ethhdr *)skb->data;
277891ef8ddSDavid Lebrun 
278891ef8ddSDavid Lebrun 	/* To determine the frame's protocol, we assume it is 802.3. This avoids
279891ef8ddSDavid Lebrun 	 * a call to eth_type_trans(), which is not really relevant for our
280891ef8ddSDavid Lebrun 	 * use case.
281891ef8ddSDavid Lebrun 	 */
282891ef8ddSDavid Lebrun 	if (!eth_proto_is_802_3(eth->h_proto))
283891ef8ddSDavid Lebrun 		goto drop;
284891ef8ddSDavid Lebrun 
285891ef8ddSDavid Lebrun 	odev = dev_get_by_index_rcu(net, slwt->oif);
286891ef8ddSDavid Lebrun 	if (!odev)
287891ef8ddSDavid Lebrun 		goto drop;
288891ef8ddSDavid Lebrun 
289891ef8ddSDavid Lebrun 	/* As we accept Ethernet frames, make sure the egress device is of
290891ef8ddSDavid Lebrun 	 * the correct type.
291891ef8ddSDavid Lebrun 	 */
292891ef8ddSDavid Lebrun 	if (odev->type != ARPHRD_ETHER)
293891ef8ddSDavid Lebrun 		goto drop;
294891ef8ddSDavid Lebrun 
295891ef8ddSDavid Lebrun 	if (!(odev->flags & IFF_UP) || !netif_carrier_ok(odev))
296891ef8ddSDavid Lebrun 		goto drop;
297891ef8ddSDavid Lebrun 
298891ef8ddSDavid Lebrun 	skb_orphan(skb);
299891ef8ddSDavid Lebrun 
300891ef8ddSDavid Lebrun 	if (skb_warn_if_lro(skb))
301891ef8ddSDavid Lebrun 		goto drop;
302891ef8ddSDavid Lebrun 
303891ef8ddSDavid Lebrun 	skb_forward_csum(skb);
304891ef8ddSDavid Lebrun 
305891ef8ddSDavid Lebrun 	if (skb->len - ETH_HLEN > odev->mtu)
306891ef8ddSDavid Lebrun 		goto drop;
307891ef8ddSDavid Lebrun 
308891ef8ddSDavid Lebrun 	skb->dev = odev;
309891ef8ddSDavid Lebrun 	skb->protocol = eth->h_proto;
310891ef8ddSDavid Lebrun 
311891ef8ddSDavid Lebrun 	return dev_queue_xmit(skb);
312891ef8ddSDavid Lebrun 
313891ef8ddSDavid Lebrun drop:
314891ef8ddSDavid Lebrun 	kfree_skb(skb);
315891ef8ddSDavid Lebrun 	return -EINVAL;
316891ef8ddSDavid Lebrun }
317891ef8ddSDavid Lebrun 
318140f04c3SDavid Lebrun /* decapsulate and forward to specified nexthop */
319140f04c3SDavid Lebrun static int input_action_end_dx6(struct sk_buff *skb,
320140f04c3SDavid Lebrun 				struct seg6_local_lwt *slwt)
321140f04c3SDavid Lebrun {
322d7a669ddSDavid Lebrun 	struct in6_addr *nhaddr = NULL;
323140f04c3SDavid Lebrun 
324140f04c3SDavid Lebrun 	/* this function accepts IPv6 encapsulated packets, with either
325140f04c3SDavid Lebrun 	 * an SRH with SL=0, or no SRH.
326140f04c3SDavid Lebrun 	 */
327140f04c3SDavid Lebrun 
328d7a669ddSDavid Lebrun 	if (!decap_and_validate(skb, IPPROTO_IPV6))
329140f04c3SDavid Lebrun 		goto drop;
330140f04c3SDavid Lebrun 
331d7a669ddSDavid Lebrun 	if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
332140f04c3SDavid Lebrun 		goto drop;
333140f04c3SDavid Lebrun 
334140f04c3SDavid Lebrun 	/* The inner packet is not associated to any local interface,
335140f04c3SDavid Lebrun 	 * so we do not call netif_rx().
336140f04c3SDavid Lebrun 	 *
337140f04c3SDavid Lebrun 	 * If slwt->nh6 is set to ::, then lookup the nexthop for the
338140f04c3SDavid Lebrun 	 * inner packet's DA. Otherwise, use the specified nexthop.
339140f04c3SDavid Lebrun 	 */
340140f04c3SDavid Lebrun 
341d7a669ddSDavid Lebrun 	if (!ipv6_addr_any(&slwt->nh6))
342d7a669ddSDavid Lebrun 		nhaddr = &slwt->nh6;
343140f04c3SDavid Lebrun 
3441c1e761eSMathieu Xhonneux 	seg6_lookup_nexthop(skb, nhaddr, 0);
345140f04c3SDavid Lebrun 
346140f04c3SDavid Lebrun 	return dst_input(skb);
347140f04c3SDavid Lebrun drop:
348140f04c3SDavid Lebrun 	kfree_skb(skb);
349140f04c3SDavid Lebrun 	return -EINVAL;
350140f04c3SDavid Lebrun }
351140f04c3SDavid Lebrun 
352891ef8ddSDavid Lebrun static int input_action_end_dx4(struct sk_buff *skb,
353891ef8ddSDavid Lebrun 				struct seg6_local_lwt *slwt)
354891ef8ddSDavid Lebrun {
355891ef8ddSDavid Lebrun 	struct iphdr *iph;
356891ef8ddSDavid Lebrun 	__be32 nhaddr;
357891ef8ddSDavid Lebrun 	int err;
358891ef8ddSDavid Lebrun 
359891ef8ddSDavid Lebrun 	if (!decap_and_validate(skb, IPPROTO_IPIP))
360891ef8ddSDavid Lebrun 		goto drop;
361891ef8ddSDavid Lebrun 
362891ef8ddSDavid Lebrun 	if (!pskb_may_pull(skb, sizeof(struct iphdr)))
363891ef8ddSDavid Lebrun 		goto drop;
364891ef8ddSDavid Lebrun 
365891ef8ddSDavid Lebrun 	skb->protocol = htons(ETH_P_IP);
366891ef8ddSDavid Lebrun 
367891ef8ddSDavid Lebrun 	iph = ip_hdr(skb);
368891ef8ddSDavid Lebrun 
369891ef8ddSDavid Lebrun 	nhaddr = slwt->nh4.s_addr ?: iph->daddr;
370891ef8ddSDavid Lebrun 
371891ef8ddSDavid Lebrun 	skb_dst_drop(skb);
372891ef8ddSDavid Lebrun 
373891ef8ddSDavid Lebrun 	err = ip_route_input(skb, nhaddr, iph->saddr, 0, skb->dev);
374891ef8ddSDavid Lebrun 	if (err)
375891ef8ddSDavid Lebrun 		goto drop;
376891ef8ddSDavid Lebrun 
377891ef8ddSDavid Lebrun 	return dst_input(skb);
378891ef8ddSDavid Lebrun 
379891ef8ddSDavid Lebrun drop:
380891ef8ddSDavid Lebrun 	kfree_skb(skb);
381891ef8ddSDavid Lebrun 	return -EINVAL;
382891ef8ddSDavid Lebrun }
383891ef8ddSDavid Lebrun 
384891ef8ddSDavid Lebrun static int input_action_end_dt6(struct sk_buff *skb,
385891ef8ddSDavid Lebrun 				struct seg6_local_lwt *slwt)
386891ef8ddSDavid Lebrun {
387891ef8ddSDavid Lebrun 	if (!decap_and_validate(skb, IPPROTO_IPV6))
388891ef8ddSDavid Lebrun 		goto drop;
389891ef8ddSDavid Lebrun 
390891ef8ddSDavid Lebrun 	if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
391891ef8ddSDavid Lebrun 		goto drop;
392891ef8ddSDavid Lebrun 
3931c1e761eSMathieu Xhonneux 	seg6_lookup_nexthop(skb, NULL, slwt->table);
394891ef8ddSDavid Lebrun 
395891ef8ddSDavid Lebrun 	return dst_input(skb);
396891ef8ddSDavid Lebrun 
397891ef8ddSDavid Lebrun drop:
398891ef8ddSDavid Lebrun 	kfree_skb(skb);
399891ef8ddSDavid Lebrun 	return -EINVAL;
400891ef8ddSDavid Lebrun }
401891ef8ddSDavid Lebrun 
402140f04c3SDavid Lebrun /* push an SRH on top of the current one */
403140f04c3SDavid Lebrun static int input_action_end_b6(struct sk_buff *skb, struct seg6_local_lwt *slwt)
404140f04c3SDavid Lebrun {
405140f04c3SDavid Lebrun 	struct ipv6_sr_hdr *srh;
406140f04c3SDavid Lebrun 	int err = -EINVAL;
407140f04c3SDavid Lebrun 
408140f04c3SDavid Lebrun 	srh = get_and_validate_srh(skb);
409140f04c3SDavid Lebrun 	if (!srh)
410140f04c3SDavid Lebrun 		goto drop;
411140f04c3SDavid Lebrun 
412140f04c3SDavid Lebrun 	err = seg6_do_srh_inline(skb, slwt->srh);
413140f04c3SDavid Lebrun 	if (err)
414140f04c3SDavid Lebrun 		goto drop;
415140f04c3SDavid Lebrun 
416140f04c3SDavid Lebrun 	ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
417140f04c3SDavid Lebrun 	skb_set_transport_header(skb, sizeof(struct ipv6hdr));
418140f04c3SDavid Lebrun 
4191c1e761eSMathieu Xhonneux 	seg6_lookup_nexthop(skb, NULL, 0);
420140f04c3SDavid Lebrun 
421140f04c3SDavid Lebrun 	return dst_input(skb);
422140f04c3SDavid Lebrun 
423140f04c3SDavid Lebrun drop:
424140f04c3SDavid Lebrun 	kfree_skb(skb);
425140f04c3SDavid Lebrun 	return err;
426140f04c3SDavid Lebrun }
427140f04c3SDavid Lebrun 
428140f04c3SDavid Lebrun /* encapsulate within an outer IPv6 header and a specified SRH */
429140f04c3SDavid Lebrun static int input_action_end_b6_encap(struct sk_buff *skb,
430140f04c3SDavid Lebrun 				     struct seg6_local_lwt *slwt)
431140f04c3SDavid Lebrun {
432140f04c3SDavid Lebrun 	struct ipv6_sr_hdr *srh;
433140f04c3SDavid Lebrun 	int err = -EINVAL;
434140f04c3SDavid Lebrun 
435140f04c3SDavid Lebrun 	srh = get_and_validate_srh(skb);
436140f04c3SDavid Lebrun 	if (!srh)
437140f04c3SDavid Lebrun 		goto drop;
438140f04c3SDavid Lebrun 
439d7a669ddSDavid Lebrun 	advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
440140f04c3SDavid Lebrun 
441140f04c3SDavid Lebrun 	skb_reset_inner_headers(skb);
442140f04c3SDavid Lebrun 	skb->encapsulation = 1;
443140f04c3SDavid Lebrun 
44432d99d0bSDavid Lebrun 	err = seg6_do_srh_encap(skb, slwt->srh, IPPROTO_IPV6);
445140f04c3SDavid Lebrun 	if (err)
446140f04c3SDavid Lebrun 		goto drop;
447140f04c3SDavid Lebrun 
448140f04c3SDavid Lebrun 	ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
449140f04c3SDavid Lebrun 	skb_set_transport_header(skb, sizeof(struct ipv6hdr));
450140f04c3SDavid Lebrun 
4511c1e761eSMathieu Xhonneux 	seg6_lookup_nexthop(skb, NULL, 0);
452140f04c3SDavid Lebrun 
453140f04c3SDavid Lebrun 	return dst_input(skb);
454140f04c3SDavid Lebrun 
455140f04c3SDavid Lebrun drop:
456140f04c3SDavid Lebrun 	kfree_skb(skb);
457140f04c3SDavid Lebrun 	return err;
458140f04c3SDavid Lebrun }
459140f04c3SDavid Lebrun 
460fe94cc29SMathieu Xhonneux DEFINE_PER_CPU(struct seg6_bpf_srh_state, seg6_bpf_srh_states);
461fe94cc29SMathieu Xhonneux 
462*004d4b27SMathieu Xhonneux static int input_action_end_bpf(struct sk_buff *skb,
463*004d4b27SMathieu Xhonneux 				struct seg6_local_lwt *slwt)
464*004d4b27SMathieu Xhonneux {
465*004d4b27SMathieu Xhonneux 	struct seg6_bpf_srh_state *srh_state =
466*004d4b27SMathieu Xhonneux 		this_cpu_ptr(&seg6_bpf_srh_states);
467*004d4b27SMathieu Xhonneux 	struct seg6_bpf_srh_state local_srh_state;
468*004d4b27SMathieu Xhonneux 	struct ipv6_sr_hdr *srh;
469*004d4b27SMathieu Xhonneux 	int srhoff = 0;
470*004d4b27SMathieu Xhonneux 	int ret;
471*004d4b27SMathieu Xhonneux 
472*004d4b27SMathieu Xhonneux 	srh = get_and_validate_srh(skb);
473*004d4b27SMathieu Xhonneux 	if (!srh)
474*004d4b27SMathieu Xhonneux 		goto drop;
475*004d4b27SMathieu Xhonneux 	advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
476*004d4b27SMathieu Xhonneux 
477*004d4b27SMathieu Xhonneux 	/* preempt_disable is needed to protect the per-CPU buffer srh_state,
478*004d4b27SMathieu Xhonneux 	 * which is also accessed by the bpf_lwt_seg6_* helpers
479*004d4b27SMathieu Xhonneux 	 */
480*004d4b27SMathieu Xhonneux 	preempt_disable();
481*004d4b27SMathieu Xhonneux 	srh_state->hdrlen = srh->hdrlen << 3;
482*004d4b27SMathieu Xhonneux 	srh_state->valid = 1;
483*004d4b27SMathieu Xhonneux 
484*004d4b27SMathieu Xhonneux 	rcu_read_lock();
485*004d4b27SMathieu Xhonneux 	bpf_compute_data_pointers(skb);
486*004d4b27SMathieu Xhonneux 	ret = bpf_prog_run_save_cb(slwt->bpf.prog, skb);
487*004d4b27SMathieu Xhonneux 	rcu_read_unlock();
488*004d4b27SMathieu Xhonneux 
489*004d4b27SMathieu Xhonneux 	local_srh_state = *srh_state;
490*004d4b27SMathieu Xhonneux 	preempt_enable();
491*004d4b27SMathieu Xhonneux 
492*004d4b27SMathieu Xhonneux 	switch (ret) {
493*004d4b27SMathieu Xhonneux 	case BPF_OK:
494*004d4b27SMathieu Xhonneux 	case BPF_REDIRECT:
495*004d4b27SMathieu Xhonneux 		break;
496*004d4b27SMathieu Xhonneux 	case BPF_DROP:
497*004d4b27SMathieu Xhonneux 		goto drop;
498*004d4b27SMathieu Xhonneux 	default:
499*004d4b27SMathieu Xhonneux 		pr_warn_once("bpf-seg6local: Illegal return value %u\n", ret);
500*004d4b27SMathieu Xhonneux 		goto drop;
501*004d4b27SMathieu Xhonneux 	}
502*004d4b27SMathieu Xhonneux 
503*004d4b27SMathieu Xhonneux 	if (unlikely((local_srh_state.hdrlen & 7) != 0))
504*004d4b27SMathieu Xhonneux 		goto drop;
505*004d4b27SMathieu Xhonneux 
506*004d4b27SMathieu Xhonneux 	if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
507*004d4b27SMathieu Xhonneux 		goto drop;
508*004d4b27SMathieu Xhonneux 	srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
509*004d4b27SMathieu Xhonneux 	srh->hdrlen = (u8)(local_srh_state.hdrlen >> 3);
510*004d4b27SMathieu Xhonneux 
511*004d4b27SMathieu Xhonneux 	if (!local_srh_state.valid &&
512*004d4b27SMathieu Xhonneux 	    unlikely(!seg6_validate_srh(srh, (srh->hdrlen + 1) << 3)))
513*004d4b27SMathieu Xhonneux 		goto drop;
514*004d4b27SMathieu Xhonneux 
515*004d4b27SMathieu Xhonneux 	if (ret != BPF_REDIRECT)
516*004d4b27SMathieu Xhonneux 		seg6_lookup_nexthop(skb, NULL, 0);
517*004d4b27SMathieu Xhonneux 
518*004d4b27SMathieu Xhonneux 	return dst_input(skb);
519*004d4b27SMathieu Xhonneux 
520*004d4b27SMathieu Xhonneux drop:
521*004d4b27SMathieu Xhonneux 	kfree_skb(skb);
522*004d4b27SMathieu Xhonneux 	return -EINVAL;
523*004d4b27SMathieu Xhonneux }
524*004d4b27SMathieu Xhonneux 
525d1df6fd8SDavid Lebrun static struct seg6_action_desc seg6_action_table[] = {
526d1df6fd8SDavid Lebrun 	{
527d1df6fd8SDavid Lebrun 		.action		= SEG6_LOCAL_ACTION_END,
528d1df6fd8SDavid Lebrun 		.attrs		= 0,
529140f04c3SDavid Lebrun 		.input		= input_action_end,
530d1df6fd8SDavid Lebrun 	},
531140f04c3SDavid Lebrun 	{
532140f04c3SDavid Lebrun 		.action		= SEG6_LOCAL_ACTION_END_X,
533140f04c3SDavid Lebrun 		.attrs		= (1 << SEG6_LOCAL_NH6),
534140f04c3SDavid Lebrun 		.input		= input_action_end_x,
535140f04c3SDavid Lebrun 	},
536140f04c3SDavid Lebrun 	{
537891ef8ddSDavid Lebrun 		.action		= SEG6_LOCAL_ACTION_END_T,
538891ef8ddSDavid Lebrun 		.attrs		= (1 << SEG6_LOCAL_TABLE),
539891ef8ddSDavid Lebrun 		.input		= input_action_end_t,
540891ef8ddSDavid Lebrun 	},
541891ef8ddSDavid Lebrun 	{
542891ef8ddSDavid Lebrun 		.action		= SEG6_LOCAL_ACTION_END_DX2,
543891ef8ddSDavid Lebrun 		.attrs		= (1 << SEG6_LOCAL_OIF),
544891ef8ddSDavid Lebrun 		.input		= input_action_end_dx2,
545891ef8ddSDavid Lebrun 	},
546891ef8ddSDavid Lebrun 	{
547140f04c3SDavid Lebrun 		.action		= SEG6_LOCAL_ACTION_END_DX6,
548140f04c3SDavid Lebrun 		.attrs		= (1 << SEG6_LOCAL_NH6),
549140f04c3SDavid Lebrun 		.input		= input_action_end_dx6,
550140f04c3SDavid Lebrun 	},
551140f04c3SDavid Lebrun 	{
552891ef8ddSDavid Lebrun 		.action		= SEG6_LOCAL_ACTION_END_DX4,
553891ef8ddSDavid Lebrun 		.attrs		= (1 << SEG6_LOCAL_NH4),
554891ef8ddSDavid Lebrun 		.input		= input_action_end_dx4,
555891ef8ddSDavid Lebrun 	},
556891ef8ddSDavid Lebrun 	{
557891ef8ddSDavid Lebrun 		.action		= SEG6_LOCAL_ACTION_END_DT6,
558891ef8ddSDavid Lebrun 		.attrs		= (1 << SEG6_LOCAL_TABLE),
559891ef8ddSDavid Lebrun 		.input		= input_action_end_dt6,
560891ef8ddSDavid Lebrun 	},
561891ef8ddSDavid Lebrun 	{
562140f04c3SDavid Lebrun 		.action		= SEG6_LOCAL_ACTION_END_B6,
563140f04c3SDavid Lebrun 		.attrs		= (1 << SEG6_LOCAL_SRH),
564140f04c3SDavid Lebrun 		.input		= input_action_end_b6,
565140f04c3SDavid Lebrun 	},
566140f04c3SDavid Lebrun 	{
567140f04c3SDavid Lebrun 		.action		= SEG6_LOCAL_ACTION_END_B6_ENCAP,
568140f04c3SDavid Lebrun 		.attrs		= (1 << SEG6_LOCAL_SRH),
569140f04c3SDavid Lebrun 		.input		= input_action_end_b6_encap,
570140f04c3SDavid Lebrun 		.static_headroom	= sizeof(struct ipv6hdr),
571*004d4b27SMathieu Xhonneux 	},
572*004d4b27SMathieu Xhonneux 	{
573*004d4b27SMathieu Xhonneux 		.action		= SEG6_LOCAL_ACTION_END_BPF,
574*004d4b27SMathieu Xhonneux 		.attrs		= (1 << SEG6_LOCAL_BPF),
575*004d4b27SMathieu Xhonneux 		.input		= input_action_end_bpf,
576*004d4b27SMathieu Xhonneux 	},
577*004d4b27SMathieu Xhonneux 
578d1df6fd8SDavid Lebrun };
579d1df6fd8SDavid Lebrun 
580d1df6fd8SDavid Lebrun static struct seg6_action_desc *__get_action_desc(int action)
581d1df6fd8SDavid Lebrun {
582d1df6fd8SDavid Lebrun 	struct seg6_action_desc *desc;
583d1df6fd8SDavid Lebrun 	int i, count;
584d1df6fd8SDavid Lebrun 
585709af180SColin Ian King 	count = ARRAY_SIZE(seg6_action_table);
586d1df6fd8SDavid Lebrun 	for (i = 0; i < count; i++) {
587d1df6fd8SDavid Lebrun 		desc = &seg6_action_table[i];
588d1df6fd8SDavid Lebrun 		if (desc->action == action)
589d1df6fd8SDavid Lebrun 			return desc;
590d1df6fd8SDavid Lebrun 	}
591d1df6fd8SDavid Lebrun 
592d1df6fd8SDavid Lebrun 	return NULL;
593d1df6fd8SDavid Lebrun }
594d1df6fd8SDavid Lebrun 
595d1df6fd8SDavid Lebrun static int seg6_local_input(struct sk_buff *skb)
596d1df6fd8SDavid Lebrun {
597d1df6fd8SDavid Lebrun 	struct dst_entry *orig_dst = skb_dst(skb);
598d1df6fd8SDavid Lebrun 	struct seg6_action_desc *desc;
599d1df6fd8SDavid Lebrun 	struct seg6_local_lwt *slwt;
600d1df6fd8SDavid Lebrun 
6016285217fSDavid Lebrun 	if (skb->protocol != htons(ETH_P_IPV6)) {
6026285217fSDavid Lebrun 		kfree_skb(skb);
6036285217fSDavid Lebrun 		return -EINVAL;
6046285217fSDavid Lebrun 	}
6056285217fSDavid Lebrun 
606d1df6fd8SDavid Lebrun 	slwt = seg6_local_lwtunnel(orig_dst->lwtstate);
607d1df6fd8SDavid Lebrun 	desc = slwt->desc;
608d1df6fd8SDavid Lebrun 
609d1df6fd8SDavid Lebrun 	return desc->input(skb, slwt);
610d1df6fd8SDavid Lebrun }
611d1df6fd8SDavid Lebrun 
612d1df6fd8SDavid Lebrun static const struct nla_policy seg6_local_policy[SEG6_LOCAL_MAX + 1] = {
613d1df6fd8SDavid Lebrun 	[SEG6_LOCAL_ACTION]	= { .type = NLA_U32 },
614d1df6fd8SDavid Lebrun 	[SEG6_LOCAL_SRH]	= { .type = NLA_BINARY },
615d1df6fd8SDavid Lebrun 	[SEG6_LOCAL_TABLE]	= { .type = NLA_U32 },
616d1df6fd8SDavid Lebrun 	[SEG6_LOCAL_NH4]	= { .type = NLA_BINARY,
617d1df6fd8SDavid Lebrun 				    .len = sizeof(struct in_addr) },
618d1df6fd8SDavid Lebrun 	[SEG6_LOCAL_NH6]	= { .type = NLA_BINARY,
619d1df6fd8SDavid Lebrun 				    .len = sizeof(struct in6_addr) },
620d1df6fd8SDavid Lebrun 	[SEG6_LOCAL_IIF]	= { .type = NLA_U32 },
621d1df6fd8SDavid Lebrun 	[SEG6_LOCAL_OIF]	= { .type = NLA_U32 },
622*004d4b27SMathieu Xhonneux 	[SEG6_LOCAL_BPF]	= { .type = NLA_NESTED },
623d1df6fd8SDavid Lebrun };
624d1df6fd8SDavid Lebrun 
6252d9cc60aSDavid Lebrun static int parse_nla_srh(struct nlattr **attrs, struct seg6_local_lwt *slwt)
6262d9cc60aSDavid Lebrun {
6272d9cc60aSDavid Lebrun 	struct ipv6_sr_hdr *srh;
6282d9cc60aSDavid Lebrun 	int len;
6292d9cc60aSDavid Lebrun 
6302d9cc60aSDavid Lebrun 	srh = nla_data(attrs[SEG6_LOCAL_SRH]);
6312d9cc60aSDavid Lebrun 	len = nla_len(attrs[SEG6_LOCAL_SRH]);
6322d9cc60aSDavid Lebrun 
6332d9cc60aSDavid Lebrun 	/* SRH must contain at least one segment */
6342d9cc60aSDavid Lebrun 	if (len < sizeof(*srh) + sizeof(struct in6_addr))
6352d9cc60aSDavid Lebrun 		return -EINVAL;
6362d9cc60aSDavid Lebrun 
6372d9cc60aSDavid Lebrun 	if (!seg6_validate_srh(srh, len))
6382d9cc60aSDavid Lebrun 		return -EINVAL;
6392d9cc60aSDavid Lebrun 
6402d9cc60aSDavid Lebrun 	slwt->srh = kmalloc(len, GFP_KERNEL);
6412d9cc60aSDavid Lebrun 	if (!slwt->srh)
6422d9cc60aSDavid Lebrun 		return -ENOMEM;
6432d9cc60aSDavid Lebrun 
6442d9cc60aSDavid Lebrun 	memcpy(slwt->srh, srh, len);
6452d9cc60aSDavid Lebrun 
6462d9cc60aSDavid Lebrun 	slwt->headroom += len;
6472d9cc60aSDavid Lebrun 
6482d9cc60aSDavid Lebrun 	return 0;
6492d9cc60aSDavid Lebrun }
6502d9cc60aSDavid Lebrun 
6512d9cc60aSDavid Lebrun static int put_nla_srh(struct sk_buff *skb, struct seg6_local_lwt *slwt)
6522d9cc60aSDavid Lebrun {
6532d9cc60aSDavid Lebrun 	struct ipv6_sr_hdr *srh;
6542d9cc60aSDavid Lebrun 	struct nlattr *nla;
6552d9cc60aSDavid Lebrun 	int len;
6562d9cc60aSDavid Lebrun 
6572d9cc60aSDavid Lebrun 	srh = slwt->srh;
6582d9cc60aSDavid Lebrun 	len = (srh->hdrlen + 1) << 3;
6592d9cc60aSDavid Lebrun 
6602d9cc60aSDavid Lebrun 	nla = nla_reserve(skb, SEG6_LOCAL_SRH, len);
6612d9cc60aSDavid Lebrun 	if (!nla)
6622d9cc60aSDavid Lebrun 		return -EMSGSIZE;
6632d9cc60aSDavid Lebrun 
6642d9cc60aSDavid Lebrun 	memcpy(nla_data(nla), srh, len);
6652d9cc60aSDavid Lebrun 
6662d9cc60aSDavid Lebrun 	return 0;
6672d9cc60aSDavid Lebrun }
6682d9cc60aSDavid Lebrun 
6692d9cc60aSDavid Lebrun static int cmp_nla_srh(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
6702d9cc60aSDavid Lebrun {
6712d9cc60aSDavid Lebrun 	int len = (a->srh->hdrlen + 1) << 3;
6722d9cc60aSDavid Lebrun 
6732d9cc60aSDavid Lebrun 	if (len != ((b->srh->hdrlen + 1) << 3))
6742d9cc60aSDavid Lebrun 		return 1;
6752d9cc60aSDavid Lebrun 
6762d9cc60aSDavid Lebrun 	return memcmp(a->srh, b->srh, len);
6772d9cc60aSDavid Lebrun }
6782d9cc60aSDavid Lebrun 
6792d9cc60aSDavid Lebrun static int parse_nla_table(struct nlattr **attrs, struct seg6_local_lwt *slwt)
6802d9cc60aSDavid Lebrun {
6812d9cc60aSDavid Lebrun 	slwt->table = nla_get_u32(attrs[SEG6_LOCAL_TABLE]);
6822d9cc60aSDavid Lebrun 
6832d9cc60aSDavid Lebrun 	return 0;
6842d9cc60aSDavid Lebrun }
6852d9cc60aSDavid Lebrun 
6862d9cc60aSDavid Lebrun static int put_nla_table(struct sk_buff *skb, struct seg6_local_lwt *slwt)
6872d9cc60aSDavid Lebrun {
6882d9cc60aSDavid Lebrun 	if (nla_put_u32(skb, SEG6_LOCAL_TABLE, slwt->table))
6892d9cc60aSDavid Lebrun 		return -EMSGSIZE;
6902d9cc60aSDavid Lebrun 
6912d9cc60aSDavid Lebrun 	return 0;
6922d9cc60aSDavid Lebrun }
6932d9cc60aSDavid Lebrun 
6942d9cc60aSDavid Lebrun static int cmp_nla_table(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
6952d9cc60aSDavid Lebrun {
6962d9cc60aSDavid Lebrun 	if (a->table != b->table)
6972d9cc60aSDavid Lebrun 		return 1;
6982d9cc60aSDavid Lebrun 
6992d9cc60aSDavid Lebrun 	return 0;
7002d9cc60aSDavid Lebrun }
7012d9cc60aSDavid Lebrun 
7022d9cc60aSDavid Lebrun static int parse_nla_nh4(struct nlattr **attrs, struct seg6_local_lwt *slwt)
7032d9cc60aSDavid Lebrun {
7042d9cc60aSDavid Lebrun 	memcpy(&slwt->nh4, nla_data(attrs[SEG6_LOCAL_NH4]),
7052d9cc60aSDavid Lebrun 	       sizeof(struct in_addr));
7062d9cc60aSDavid Lebrun 
7072d9cc60aSDavid Lebrun 	return 0;
7082d9cc60aSDavid Lebrun }
7092d9cc60aSDavid Lebrun 
7102d9cc60aSDavid Lebrun static int put_nla_nh4(struct sk_buff *skb, struct seg6_local_lwt *slwt)
7112d9cc60aSDavid Lebrun {
7122d9cc60aSDavid Lebrun 	struct nlattr *nla;
7132d9cc60aSDavid Lebrun 
7142d9cc60aSDavid Lebrun 	nla = nla_reserve(skb, SEG6_LOCAL_NH4, sizeof(struct in_addr));
7152d9cc60aSDavid Lebrun 	if (!nla)
7162d9cc60aSDavid Lebrun 		return -EMSGSIZE;
7172d9cc60aSDavid Lebrun 
7182d9cc60aSDavid Lebrun 	memcpy(nla_data(nla), &slwt->nh4, sizeof(struct in_addr));
7192d9cc60aSDavid Lebrun 
7202d9cc60aSDavid Lebrun 	return 0;
7212d9cc60aSDavid Lebrun }
7222d9cc60aSDavid Lebrun 
7232d9cc60aSDavid Lebrun static int cmp_nla_nh4(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
7242d9cc60aSDavid Lebrun {
7252d9cc60aSDavid Lebrun 	return memcmp(&a->nh4, &b->nh4, sizeof(struct in_addr));
7262d9cc60aSDavid Lebrun }
7272d9cc60aSDavid Lebrun 
7282d9cc60aSDavid Lebrun static int parse_nla_nh6(struct nlattr **attrs, struct seg6_local_lwt *slwt)
7292d9cc60aSDavid Lebrun {
7302d9cc60aSDavid Lebrun 	memcpy(&slwt->nh6, nla_data(attrs[SEG6_LOCAL_NH6]),
7312d9cc60aSDavid Lebrun 	       sizeof(struct in6_addr));
7322d9cc60aSDavid Lebrun 
7332d9cc60aSDavid Lebrun 	return 0;
7342d9cc60aSDavid Lebrun }
7352d9cc60aSDavid Lebrun 
7362d9cc60aSDavid Lebrun static int put_nla_nh6(struct sk_buff *skb, struct seg6_local_lwt *slwt)
7372d9cc60aSDavid Lebrun {
7382d9cc60aSDavid Lebrun 	struct nlattr *nla;
7392d9cc60aSDavid Lebrun 
7402d9cc60aSDavid Lebrun 	nla = nla_reserve(skb, SEG6_LOCAL_NH6, sizeof(struct in6_addr));
7412d9cc60aSDavid Lebrun 	if (!nla)
7422d9cc60aSDavid Lebrun 		return -EMSGSIZE;
7432d9cc60aSDavid Lebrun 
7442d9cc60aSDavid Lebrun 	memcpy(nla_data(nla), &slwt->nh6, sizeof(struct in6_addr));
7452d9cc60aSDavid Lebrun 
7462d9cc60aSDavid Lebrun 	return 0;
7472d9cc60aSDavid Lebrun }
7482d9cc60aSDavid Lebrun 
7492d9cc60aSDavid Lebrun static int cmp_nla_nh6(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
7502d9cc60aSDavid Lebrun {
7512d9cc60aSDavid Lebrun 	return memcmp(&a->nh6, &b->nh6, sizeof(struct in6_addr));
7522d9cc60aSDavid Lebrun }
7532d9cc60aSDavid Lebrun 
7542d9cc60aSDavid Lebrun static int parse_nla_iif(struct nlattr **attrs, struct seg6_local_lwt *slwt)
7552d9cc60aSDavid Lebrun {
7562d9cc60aSDavid Lebrun 	slwt->iif = nla_get_u32(attrs[SEG6_LOCAL_IIF]);
7572d9cc60aSDavid Lebrun 
7582d9cc60aSDavid Lebrun 	return 0;
7592d9cc60aSDavid Lebrun }
7602d9cc60aSDavid Lebrun 
7612d9cc60aSDavid Lebrun static int put_nla_iif(struct sk_buff *skb, struct seg6_local_lwt *slwt)
7622d9cc60aSDavid Lebrun {
7632d9cc60aSDavid Lebrun 	if (nla_put_u32(skb, SEG6_LOCAL_IIF, slwt->iif))
7642d9cc60aSDavid Lebrun 		return -EMSGSIZE;
7652d9cc60aSDavid Lebrun 
7662d9cc60aSDavid Lebrun 	return 0;
7672d9cc60aSDavid Lebrun }
7682d9cc60aSDavid Lebrun 
7692d9cc60aSDavid Lebrun static int cmp_nla_iif(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
7702d9cc60aSDavid Lebrun {
7712d9cc60aSDavid Lebrun 	if (a->iif != b->iif)
7722d9cc60aSDavid Lebrun 		return 1;
7732d9cc60aSDavid Lebrun 
7742d9cc60aSDavid Lebrun 	return 0;
7752d9cc60aSDavid Lebrun }
7762d9cc60aSDavid Lebrun 
7772d9cc60aSDavid Lebrun static int parse_nla_oif(struct nlattr **attrs, struct seg6_local_lwt *slwt)
7782d9cc60aSDavid Lebrun {
7792d9cc60aSDavid Lebrun 	slwt->oif = nla_get_u32(attrs[SEG6_LOCAL_OIF]);
7802d9cc60aSDavid Lebrun 
7812d9cc60aSDavid Lebrun 	return 0;
7822d9cc60aSDavid Lebrun }
7832d9cc60aSDavid Lebrun 
7842d9cc60aSDavid Lebrun static int put_nla_oif(struct sk_buff *skb, struct seg6_local_lwt *slwt)
7852d9cc60aSDavid Lebrun {
7862d9cc60aSDavid Lebrun 	if (nla_put_u32(skb, SEG6_LOCAL_OIF, slwt->oif))
7872d9cc60aSDavid Lebrun 		return -EMSGSIZE;
7882d9cc60aSDavid Lebrun 
7892d9cc60aSDavid Lebrun 	return 0;
7902d9cc60aSDavid Lebrun }
7912d9cc60aSDavid Lebrun 
7922d9cc60aSDavid Lebrun static int cmp_nla_oif(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
7932d9cc60aSDavid Lebrun {
7942d9cc60aSDavid Lebrun 	if (a->oif != b->oif)
7952d9cc60aSDavid Lebrun 		return 1;
7962d9cc60aSDavid Lebrun 
7972d9cc60aSDavid Lebrun 	return 0;
7982d9cc60aSDavid Lebrun }
7992d9cc60aSDavid Lebrun 
800*004d4b27SMathieu Xhonneux #define MAX_PROG_NAME 256
801*004d4b27SMathieu Xhonneux static const struct nla_policy bpf_prog_policy[SEG6_LOCAL_BPF_PROG_MAX + 1] = {
802*004d4b27SMathieu Xhonneux 	[SEG6_LOCAL_BPF_PROG]	   = { .type = NLA_U32, },
803*004d4b27SMathieu Xhonneux 	[SEG6_LOCAL_BPF_PROG_NAME] = { .type = NLA_NUL_STRING,
804*004d4b27SMathieu Xhonneux 				       .len = MAX_PROG_NAME },
805*004d4b27SMathieu Xhonneux };
806*004d4b27SMathieu Xhonneux 
807*004d4b27SMathieu Xhonneux static int parse_nla_bpf(struct nlattr **attrs, struct seg6_local_lwt *slwt)
808*004d4b27SMathieu Xhonneux {
809*004d4b27SMathieu Xhonneux 	struct nlattr *tb[SEG6_LOCAL_BPF_PROG_MAX + 1];
810*004d4b27SMathieu Xhonneux 	struct bpf_prog *p;
811*004d4b27SMathieu Xhonneux 	int ret;
812*004d4b27SMathieu Xhonneux 	u32 fd;
813*004d4b27SMathieu Xhonneux 
814*004d4b27SMathieu Xhonneux 	ret = nla_parse_nested(tb, SEG6_LOCAL_BPF_PROG_MAX,
815*004d4b27SMathieu Xhonneux 			       attrs[SEG6_LOCAL_BPF], bpf_prog_policy, NULL);
816*004d4b27SMathieu Xhonneux 	if (ret < 0)
817*004d4b27SMathieu Xhonneux 		return ret;
818*004d4b27SMathieu Xhonneux 
819*004d4b27SMathieu Xhonneux 	if (!tb[SEG6_LOCAL_BPF_PROG] || !tb[SEG6_LOCAL_BPF_PROG_NAME])
820*004d4b27SMathieu Xhonneux 		return -EINVAL;
821*004d4b27SMathieu Xhonneux 
822*004d4b27SMathieu Xhonneux 	slwt->bpf.name = nla_memdup(tb[SEG6_LOCAL_BPF_PROG_NAME], GFP_KERNEL);
823*004d4b27SMathieu Xhonneux 	if (!slwt->bpf.name)
824*004d4b27SMathieu Xhonneux 		return -ENOMEM;
825*004d4b27SMathieu Xhonneux 
826*004d4b27SMathieu Xhonneux 	fd = nla_get_u32(tb[SEG6_LOCAL_BPF_PROG]);
827*004d4b27SMathieu Xhonneux 	p = bpf_prog_get_type(fd, BPF_PROG_TYPE_LWT_SEG6LOCAL);
828*004d4b27SMathieu Xhonneux 	if (IS_ERR(p)) {
829*004d4b27SMathieu Xhonneux 		kfree(slwt->bpf.name);
830*004d4b27SMathieu Xhonneux 		return PTR_ERR(p);
831*004d4b27SMathieu Xhonneux 	}
832*004d4b27SMathieu Xhonneux 
833*004d4b27SMathieu Xhonneux 	slwt->bpf.prog = p;
834*004d4b27SMathieu Xhonneux 	return 0;
835*004d4b27SMathieu Xhonneux }
836*004d4b27SMathieu Xhonneux 
837*004d4b27SMathieu Xhonneux static int put_nla_bpf(struct sk_buff *skb, struct seg6_local_lwt *slwt)
838*004d4b27SMathieu Xhonneux {
839*004d4b27SMathieu Xhonneux 	struct nlattr *nest;
840*004d4b27SMathieu Xhonneux 
841*004d4b27SMathieu Xhonneux 	if (!slwt->bpf.prog)
842*004d4b27SMathieu Xhonneux 		return 0;
843*004d4b27SMathieu Xhonneux 
844*004d4b27SMathieu Xhonneux 	nest = nla_nest_start(skb, SEG6_LOCAL_BPF);
845*004d4b27SMathieu Xhonneux 	if (!nest)
846*004d4b27SMathieu Xhonneux 		return -EMSGSIZE;
847*004d4b27SMathieu Xhonneux 
848*004d4b27SMathieu Xhonneux 	if (nla_put_u32(skb, SEG6_LOCAL_BPF_PROG, slwt->bpf.prog->aux->id))
849*004d4b27SMathieu Xhonneux 		return -EMSGSIZE;
850*004d4b27SMathieu Xhonneux 
851*004d4b27SMathieu Xhonneux 	if (slwt->bpf.name &&
852*004d4b27SMathieu Xhonneux 	    nla_put_string(skb, SEG6_LOCAL_BPF_PROG_NAME, slwt->bpf.name))
853*004d4b27SMathieu Xhonneux 		return -EMSGSIZE;
854*004d4b27SMathieu Xhonneux 
855*004d4b27SMathieu Xhonneux 	return nla_nest_end(skb, nest);
856*004d4b27SMathieu Xhonneux }
857*004d4b27SMathieu Xhonneux 
858*004d4b27SMathieu Xhonneux static int cmp_nla_bpf(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
859*004d4b27SMathieu Xhonneux {
860*004d4b27SMathieu Xhonneux 	if (!a->bpf.name && !b->bpf.name)
861*004d4b27SMathieu Xhonneux 		return 0;
862*004d4b27SMathieu Xhonneux 
863*004d4b27SMathieu Xhonneux 	if (!a->bpf.name || !b->bpf.name)
864*004d4b27SMathieu Xhonneux 		return 1;
865*004d4b27SMathieu Xhonneux 
866*004d4b27SMathieu Xhonneux 	return strcmp(a->bpf.name, b->bpf.name);
867*004d4b27SMathieu Xhonneux }
868*004d4b27SMathieu Xhonneux 
869d1df6fd8SDavid Lebrun struct seg6_action_param {
870d1df6fd8SDavid Lebrun 	int (*parse)(struct nlattr **attrs, struct seg6_local_lwt *slwt);
871d1df6fd8SDavid Lebrun 	int (*put)(struct sk_buff *skb, struct seg6_local_lwt *slwt);
872d1df6fd8SDavid Lebrun 	int (*cmp)(struct seg6_local_lwt *a, struct seg6_local_lwt *b);
873d1df6fd8SDavid Lebrun };
874d1df6fd8SDavid Lebrun 
875d1df6fd8SDavid Lebrun static struct seg6_action_param seg6_action_params[SEG6_LOCAL_MAX + 1] = {
8762d9cc60aSDavid Lebrun 	[SEG6_LOCAL_SRH]	= { .parse = parse_nla_srh,
8772d9cc60aSDavid Lebrun 				    .put = put_nla_srh,
8782d9cc60aSDavid Lebrun 				    .cmp = cmp_nla_srh },
879d1df6fd8SDavid Lebrun 
8802d9cc60aSDavid Lebrun 	[SEG6_LOCAL_TABLE]	= { .parse = parse_nla_table,
8812d9cc60aSDavid Lebrun 				    .put = put_nla_table,
8822d9cc60aSDavid Lebrun 				    .cmp = cmp_nla_table },
883d1df6fd8SDavid Lebrun 
8842d9cc60aSDavid Lebrun 	[SEG6_LOCAL_NH4]	= { .parse = parse_nla_nh4,
8852d9cc60aSDavid Lebrun 				    .put = put_nla_nh4,
8862d9cc60aSDavid Lebrun 				    .cmp = cmp_nla_nh4 },
887d1df6fd8SDavid Lebrun 
8882d9cc60aSDavid Lebrun 	[SEG6_LOCAL_NH6]	= { .parse = parse_nla_nh6,
8892d9cc60aSDavid Lebrun 				    .put = put_nla_nh6,
8902d9cc60aSDavid Lebrun 				    .cmp = cmp_nla_nh6 },
891d1df6fd8SDavid Lebrun 
8922d9cc60aSDavid Lebrun 	[SEG6_LOCAL_IIF]	= { .parse = parse_nla_iif,
8932d9cc60aSDavid Lebrun 				    .put = put_nla_iif,
8942d9cc60aSDavid Lebrun 				    .cmp = cmp_nla_iif },
895d1df6fd8SDavid Lebrun 
8962d9cc60aSDavid Lebrun 	[SEG6_LOCAL_OIF]	= { .parse = parse_nla_oif,
8972d9cc60aSDavid Lebrun 				    .put = put_nla_oif,
8982d9cc60aSDavid Lebrun 				    .cmp = cmp_nla_oif },
899*004d4b27SMathieu Xhonneux 
900*004d4b27SMathieu Xhonneux 	[SEG6_LOCAL_BPF]	= { .parse = parse_nla_bpf,
901*004d4b27SMathieu Xhonneux 				    .put = put_nla_bpf,
902*004d4b27SMathieu Xhonneux 				    .cmp = cmp_nla_bpf },
903*004d4b27SMathieu Xhonneux 
904d1df6fd8SDavid Lebrun };
905d1df6fd8SDavid Lebrun 
906d1df6fd8SDavid Lebrun static int parse_nla_action(struct nlattr **attrs, struct seg6_local_lwt *slwt)
907d1df6fd8SDavid Lebrun {
908d1df6fd8SDavid Lebrun 	struct seg6_action_param *param;
909d1df6fd8SDavid Lebrun 	struct seg6_action_desc *desc;
910d1df6fd8SDavid Lebrun 	int i, err;
911d1df6fd8SDavid Lebrun 
912d1df6fd8SDavid Lebrun 	desc = __get_action_desc(slwt->action);
913d1df6fd8SDavid Lebrun 	if (!desc)
914d1df6fd8SDavid Lebrun 		return -EINVAL;
915d1df6fd8SDavid Lebrun 
916d1df6fd8SDavid Lebrun 	if (!desc->input)
917d1df6fd8SDavid Lebrun 		return -EOPNOTSUPP;
918d1df6fd8SDavid Lebrun 
919d1df6fd8SDavid Lebrun 	slwt->desc = desc;
920d1df6fd8SDavid Lebrun 	slwt->headroom += desc->static_headroom;
921d1df6fd8SDavid Lebrun 
922d1df6fd8SDavid Lebrun 	for (i = 0; i < SEG6_LOCAL_MAX + 1; i++) {
923d1df6fd8SDavid Lebrun 		if (desc->attrs & (1 << i)) {
924d1df6fd8SDavid Lebrun 			if (!attrs[i])
925d1df6fd8SDavid Lebrun 				return -EINVAL;
926d1df6fd8SDavid Lebrun 
927d1df6fd8SDavid Lebrun 			param = &seg6_action_params[i];
928d1df6fd8SDavid Lebrun 
929d1df6fd8SDavid Lebrun 			err = param->parse(attrs, slwt);
930d1df6fd8SDavid Lebrun 			if (err < 0)
931d1df6fd8SDavid Lebrun 				return err;
932d1df6fd8SDavid Lebrun 		}
933d1df6fd8SDavid Lebrun 	}
934d1df6fd8SDavid Lebrun 
935d1df6fd8SDavid Lebrun 	return 0;
936d1df6fd8SDavid Lebrun }
937d1df6fd8SDavid Lebrun 
938d1df6fd8SDavid Lebrun static int seg6_local_build_state(struct nlattr *nla, unsigned int family,
939d1df6fd8SDavid Lebrun 				  const void *cfg, struct lwtunnel_state **ts,
940d1df6fd8SDavid Lebrun 				  struct netlink_ext_ack *extack)
941d1df6fd8SDavid Lebrun {
942d1df6fd8SDavid Lebrun 	struct nlattr *tb[SEG6_LOCAL_MAX + 1];
943d1df6fd8SDavid Lebrun 	struct lwtunnel_state *newts;
944d1df6fd8SDavid Lebrun 	struct seg6_local_lwt *slwt;
945d1df6fd8SDavid Lebrun 	int err;
946d1df6fd8SDavid Lebrun 
9476285217fSDavid Lebrun 	if (family != AF_INET6)
9486285217fSDavid Lebrun 		return -EINVAL;
9496285217fSDavid Lebrun 
950d1df6fd8SDavid Lebrun 	err = nla_parse_nested(tb, SEG6_LOCAL_MAX, nla, seg6_local_policy,
951d1df6fd8SDavid Lebrun 			       extack);
952d1df6fd8SDavid Lebrun 
953d1df6fd8SDavid Lebrun 	if (err < 0)
954d1df6fd8SDavid Lebrun 		return err;
955d1df6fd8SDavid Lebrun 
956d1df6fd8SDavid Lebrun 	if (!tb[SEG6_LOCAL_ACTION])
957d1df6fd8SDavid Lebrun 		return -EINVAL;
958d1df6fd8SDavid Lebrun 
959d1df6fd8SDavid Lebrun 	newts = lwtunnel_state_alloc(sizeof(*slwt));
960d1df6fd8SDavid Lebrun 	if (!newts)
961d1df6fd8SDavid Lebrun 		return -ENOMEM;
962d1df6fd8SDavid Lebrun 
963d1df6fd8SDavid Lebrun 	slwt = seg6_local_lwtunnel(newts);
964d1df6fd8SDavid Lebrun 	slwt->action = nla_get_u32(tb[SEG6_LOCAL_ACTION]);
965d1df6fd8SDavid Lebrun 
966d1df6fd8SDavid Lebrun 	err = parse_nla_action(tb, slwt);
967d1df6fd8SDavid Lebrun 	if (err < 0)
968d1df6fd8SDavid Lebrun 		goto out_free;
969d1df6fd8SDavid Lebrun 
970d1df6fd8SDavid Lebrun 	newts->type = LWTUNNEL_ENCAP_SEG6_LOCAL;
971d1df6fd8SDavid Lebrun 	newts->flags = LWTUNNEL_STATE_INPUT_REDIRECT;
972d1df6fd8SDavid Lebrun 	newts->headroom = slwt->headroom;
973d1df6fd8SDavid Lebrun 
974d1df6fd8SDavid Lebrun 	*ts = newts;
975d1df6fd8SDavid Lebrun 
976d1df6fd8SDavid Lebrun 	return 0;
977d1df6fd8SDavid Lebrun 
978d1df6fd8SDavid Lebrun out_free:
979d1df6fd8SDavid Lebrun 	kfree(slwt->srh);
980d1df6fd8SDavid Lebrun 	kfree(newts);
981d1df6fd8SDavid Lebrun 	return err;
982d1df6fd8SDavid Lebrun }
983d1df6fd8SDavid Lebrun 
984d1df6fd8SDavid Lebrun static void seg6_local_destroy_state(struct lwtunnel_state *lwt)
985d1df6fd8SDavid Lebrun {
986d1df6fd8SDavid Lebrun 	struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
987d1df6fd8SDavid Lebrun 
988d1df6fd8SDavid Lebrun 	kfree(slwt->srh);
989*004d4b27SMathieu Xhonneux 
990*004d4b27SMathieu Xhonneux 	if (slwt->desc->attrs & (1 << SEG6_LOCAL_BPF)) {
991*004d4b27SMathieu Xhonneux 		kfree(slwt->bpf.name);
992*004d4b27SMathieu Xhonneux 		bpf_prog_put(slwt->bpf.prog);
993*004d4b27SMathieu Xhonneux 	}
994*004d4b27SMathieu Xhonneux 
995*004d4b27SMathieu Xhonneux 	return;
996d1df6fd8SDavid Lebrun }
997d1df6fd8SDavid Lebrun 
998d1df6fd8SDavid Lebrun static int seg6_local_fill_encap(struct sk_buff *skb,
999d1df6fd8SDavid Lebrun 				 struct lwtunnel_state *lwt)
1000d1df6fd8SDavid Lebrun {
1001d1df6fd8SDavid Lebrun 	struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
1002d1df6fd8SDavid Lebrun 	struct seg6_action_param *param;
1003d1df6fd8SDavid Lebrun 	int i, err;
1004d1df6fd8SDavid Lebrun 
1005d1df6fd8SDavid Lebrun 	if (nla_put_u32(skb, SEG6_LOCAL_ACTION, slwt->action))
1006d1df6fd8SDavid Lebrun 		return -EMSGSIZE;
1007d1df6fd8SDavid Lebrun 
1008d1df6fd8SDavid Lebrun 	for (i = 0; i < SEG6_LOCAL_MAX + 1; i++) {
1009d1df6fd8SDavid Lebrun 		if (slwt->desc->attrs & (1 << i)) {
1010d1df6fd8SDavid Lebrun 			param = &seg6_action_params[i];
1011d1df6fd8SDavid Lebrun 			err = param->put(skb, slwt);
1012d1df6fd8SDavid Lebrun 			if (err < 0)
1013d1df6fd8SDavid Lebrun 				return err;
1014d1df6fd8SDavid Lebrun 		}
1015d1df6fd8SDavid Lebrun 	}
1016d1df6fd8SDavid Lebrun 
1017d1df6fd8SDavid Lebrun 	return 0;
1018d1df6fd8SDavid Lebrun }
1019d1df6fd8SDavid Lebrun 
1020d1df6fd8SDavid Lebrun static int seg6_local_get_encap_size(struct lwtunnel_state *lwt)
1021d1df6fd8SDavid Lebrun {
1022d1df6fd8SDavid Lebrun 	struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
1023d1df6fd8SDavid Lebrun 	unsigned long attrs;
1024d1df6fd8SDavid Lebrun 	int nlsize;
1025d1df6fd8SDavid Lebrun 
1026d1df6fd8SDavid Lebrun 	nlsize = nla_total_size(4); /* action */
1027d1df6fd8SDavid Lebrun 
1028d1df6fd8SDavid Lebrun 	attrs = slwt->desc->attrs;
1029d1df6fd8SDavid Lebrun 
1030d1df6fd8SDavid Lebrun 	if (attrs & (1 << SEG6_LOCAL_SRH))
1031d1df6fd8SDavid Lebrun 		nlsize += nla_total_size((slwt->srh->hdrlen + 1) << 3);
1032d1df6fd8SDavid Lebrun 
1033d1df6fd8SDavid Lebrun 	if (attrs & (1 << SEG6_LOCAL_TABLE))
1034d1df6fd8SDavid Lebrun 		nlsize += nla_total_size(4);
1035d1df6fd8SDavid Lebrun 
1036d1df6fd8SDavid Lebrun 	if (attrs & (1 << SEG6_LOCAL_NH4))
1037d1df6fd8SDavid Lebrun 		nlsize += nla_total_size(4);
1038d1df6fd8SDavid Lebrun 
1039d1df6fd8SDavid Lebrun 	if (attrs & (1 << SEG6_LOCAL_NH6))
1040d1df6fd8SDavid Lebrun 		nlsize += nla_total_size(16);
1041d1df6fd8SDavid Lebrun 
1042d1df6fd8SDavid Lebrun 	if (attrs & (1 << SEG6_LOCAL_IIF))
1043d1df6fd8SDavid Lebrun 		nlsize += nla_total_size(4);
1044d1df6fd8SDavid Lebrun 
1045d1df6fd8SDavid Lebrun 	if (attrs & (1 << SEG6_LOCAL_OIF))
1046d1df6fd8SDavid Lebrun 		nlsize += nla_total_size(4);
1047d1df6fd8SDavid Lebrun 
1048*004d4b27SMathieu Xhonneux 	if (attrs & (1 << SEG6_LOCAL_BPF))
1049*004d4b27SMathieu Xhonneux 		nlsize += nla_total_size(sizeof(struct nlattr)) +
1050*004d4b27SMathieu Xhonneux 		       nla_total_size(MAX_PROG_NAME) +
1051*004d4b27SMathieu Xhonneux 		       nla_total_size(4);
1052*004d4b27SMathieu Xhonneux 
1053d1df6fd8SDavid Lebrun 	return nlsize;
1054d1df6fd8SDavid Lebrun }
1055d1df6fd8SDavid Lebrun 
1056d1df6fd8SDavid Lebrun static int seg6_local_cmp_encap(struct lwtunnel_state *a,
1057d1df6fd8SDavid Lebrun 				struct lwtunnel_state *b)
1058d1df6fd8SDavid Lebrun {
1059d1df6fd8SDavid Lebrun 	struct seg6_local_lwt *slwt_a, *slwt_b;
1060d1df6fd8SDavid Lebrun 	struct seg6_action_param *param;
1061d1df6fd8SDavid Lebrun 	int i;
1062d1df6fd8SDavid Lebrun 
1063d1df6fd8SDavid Lebrun 	slwt_a = seg6_local_lwtunnel(a);
1064d1df6fd8SDavid Lebrun 	slwt_b = seg6_local_lwtunnel(b);
1065d1df6fd8SDavid Lebrun 
1066d1df6fd8SDavid Lebrun 	if (slwt_a->action != slwt_b->action)
1067d1df6fd8SDavid Lebrun 		return 1;
1068d1df6fd8SDavid Lebrun 
1069d1df6fd8SDavid Lebrun 	if (slwt_a->desc->attrs != slwt_b->desc->attrs)
1070d1df6fd8SDavid Lebrun 		return 1;
1071d1df6fd8SDavid Lebrun 
1072d1df6fd8SDavid Lebrun 	for (i = 0; i < SEG6_LOCAL_MAX + 1; i++) {
1073d1df6fd8SDavid Lebrun 		if (slwt_a->desc->attrs & (1 << i)) {
1074d1df6fd8SDavid Lebrun 			param = &seg6_action_params[i];
1075d1df6fd8SDavid Lebrun 			if (param->cmp(slwt_a, slwt_b))
1076d1df6fd8SDavid Lebrun 				return 1;
1077d1df6fd8SDavid Lebrun 		}
1078d1df6fd8SDavid Lebrun 	}
1079d1df6fd8SDavid Lebrun 
1080d1df6fd8SDavid Lebrun 	return 0;
1081d1df6fd8SDavid Lebrun }
1082d1df6fd8SDavid Lebrun 
1083d1df6fd8SDavid Lebrun static const struct lwtunnel_encap_ops seg6_local_ops = {
1084d1df6fd8SDavid Lebrun 	.build_state	= seg6_local_build_state,
1085d1df6fd8SDavid Lebrun 	.destroy_state	= seg6_local_destroy_state,
1086d1df6fd8SDavid Lebrun 	.input		= seg6_local_input,
1087d1df6fd8SDavid Lebrun 	.fill_encap	= seg6_local_fill_encap,
1088d1df6fd8SDavid Lebrun 	.get_encap_size	= seg6_local_get_encap_size,
1089d1df6fd8SDavid Lebrun 	.cmp_encap	= seg6_local_cmp_encap,
1090d1df6fd8SDavid Lebrun 	.owner		= THIS_MODULE,
1091d1df6fd8SDavid Lebrun };
1092d1df6fd8SDavid Lebrun 
1093d1df6fd8SDavid Lebrun int __init seg6_local_init(void)
1094d1df6fd8SDavid Lebrun {
1095d1df6fd8SDavid Lebrun 	return lwtunnel_encap_add_ops(&seg6_local_ops,
1096d1df6fd8SDavid Lebrun 				      LWTUNNEL_ENCAP_SEG6_LOCAL);
1097d1df6fd8SDavid Lebrun }
1098d1df6fd8SDavid Lebrun 
1099d1df6fd8SDavid Lebrun void seg6_local_exit(void)
1100d1df6fd8SDavid Lebrun {
1101d1df6fd8SDavid Lebrun 	lwtunnel_encap_del_ops(&seg6_local_ops, LWTUNNEL_ENCAP_SEG6_LOCAL);
1102d1df6fd8SDavid Lebrun }
1103