• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  SR-IPv6 implementation
4  *
5  *  Authors:
6  *  David Lebrun <david.lebrun@uclouvain.be>
7  *  eBPF support: Mathieu Xhonneux <m.xhonneux@gmail.com>
8  */
9 
10 #include <linux/types.h>
11 #include <linux/skbuff.h>
12 #include <linux/net.h>
13 #include <linux/module.h>
14 #include <net/ip.h>
15 #include <net/lwtunnel.h>
16 #include <net/netevent.h>
17 #include <net/netns/generic.h>
18 #include <net/ip6_fib.h>
19 #include <net/route.h>
20 #include <net/seg6.h>
21 #include <linux/seg6.h>
22 #include <linux/seg6_local.h>
23 #include <net/addrconf.h>
24 #include <net/ip6_route.h>
25 #include <net/dst_cache.h>
26 #include <net/ip_tunnels.h>
27 #ifdef CONFIG_IPV6_SEG6_HMAC
28 #include <net/seg6_hmac.h>
29 #endif
30 #include <net/seg6_local.h>
31 #include <linux/etherdevice.h>
32 #include <linux/bpf.h>
33 
34 struct seg6_local_lwt;
35 
36 struct seg6_action_desc {
37 	int action;
38 	unsigned long attrs;
39 	int (*input)(struct sk_buff *skb, struct seg6_local_lwt *slwt);
40 	int static_headroom;
41 };
42 
43 struct bpf_lwt_prog {
44 	struct bpf_prog *prog;
45 	char *name;
46 };
47 
48 struct seg6_local_lwt {
49 	int action;
50 	struct ipv6_sr_hdr *srh;
51 	int table;
52 	struct in_addr nh4;
53 	struct in6_addr nh6;
54 	int iif;
55 	int oif;
56 	struct bpf_lwt_prog bpf;
57 
58 	int headroom;
59 	struct seg6_action_desc *desc;
60 };
61 
seg6_local_lwtunnel(struct lwtunnel_state * lwt)62 static struct seg6_local_lwt *seg6_local_lwtunnel(struct lwtunnel_state *lwt)
63 {
64 	return (struct seg6_local_lwt *)lwt->data;
65 }
66 
get_srh(struct sk_buff * skb)67 static struct ipv6_sr_hdr *get_srh(struct sk_buff *skb)
68 {
69 	struct ipv6_sr_hdr *srh;
70 	int len, srhoff = 0;
71 
72 	if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
73 		return NULL;
74 
75 	if (!pskb_may_pull(skb, srhoff + sizeof(*srh)))
76 		return NULL;
77 
78 	srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
79 
80 	len = (srh->hdrlen + 1) << 3;
81 
82 	if (!pskb_may_pull(skb, srhoff + len))
83 		return NULL;
84 
85 	/* note that pskb_may_pull may change pointers in header;
86 	 * for this reason it is necessary to reload them when needed.
87 	 */
88 	srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
89 
90 	if (!seg6_validate_srh(srh, len, true))
91 		return NULL;
92 
93 	return srh;
94 }
95 
get_and_validate_srh(struct sk_buff * skb)96 static struct ipv6_sr_hdr *get_and_validate_srh(struct sk_buff *skb)
97 {
98 	struct ipv6_sr_hdr *srh;
99 
100 	srh = get_srh(skb);
101 	if (!srh)
102 		return NULL;
103 
104 	if (srh->segments_left == 0)
105 		return NULL;
106 
107 #ifdef CONFIG_IPV6_SEG6_HMAC
108 	if (!seg6_hmac_validate_skb(skb))
109 		return NULL;
110 #endif
111 
112 	return srh;
113 }
114 
decap_and_validate(struct sk_buff * skb,int proto)115 static bool decap_and_validate(struct sk_buff *skb, int proto)
116 {
117 	struct ipv6_sr_hdr *srh;
118 	unsigned int off = 0;
119 
120 	srh = get_srh(skb);
121 	if (srh && srh->segments_left > 0)
122 		return false;
123 
124 #ifdef CONFIG_IPV6_SEG6_HMAC
125 	if (srh && !seg6_hmac_validate_skb(skb))
126 		return false;
127 #endif
128 
129 	if (ipv6_find_hdr(skb, &off, proto, NULL, NULL) < 0)
130 		return false;
131 
132 	if (!pskb_pull(skb, off))
133 		return false;
134 
135 	skb_postpull_rcsum(skb, skb_network_header(skb), off);
136 
137 	skb_reset_network_header(skb);
138 	skb_reset_transport_header(skb);
139 	if (iptunnel_pull_offloads(skb))
140 		return false;
141 
142 	return true;
143 }
144 
advance_nextseg(struct ipv6_sr_hdr * srh,struct in6_addr * daddr)145 static void advance_nextseg(struct ipv6_sr_hdr *srh, struct in6_addr *daddr)
146 {
147 	struct in6_addr *addr;
148 
149 	srh->segments_left--;
150 	addr = srh->segments + srh->segments_left;
151 	*daddr = *addr;
152 }
153 
154 static int
seg6_lookup_any_nexthop(struct sk_buff * skb,struct in6_addr * nhaddr,u32 tbl_id,bool local_delivery)155 seg6_lookup_any_nexthop(struct sk_buff *skb, struct in6_addr *nhaddr,
156 			u32 tbl_id, bool local_delivery)
157 {
158 	struct net *net = dev_net(skb->dev);
159 	struct ipv6hdr *hdr = ipv6_hdr(skb);
160 	int flags = RT6_LOOKUP_F_HAS_SADDR;
161 	struct dst_entry *dst = NULL;
162 	struct rt6_info *rt;
163 	struct flowi6 fl6;
164 	int dev_flags = 0;
165 
166 	fl6.flowi6_iif = skb->dev->ifindex;
167 	fl6.daddr = nhaddr ? *nhaddr : hdr->daddr;
168 	fl6.saddr = hdr->saddr;
169 	fl6.flowlabel = ip6_flowinfo(hdr);
170 	fl6.flowi6_mark = skb->mark;
171 	fl6.flowi6_proto = hdr->nexthdr;
172 
173 	if (nhaddr)
174 		fl6.flowi6_flags = FLOWI_FLAG_KNOWN_NH;
175 
176 	if (!tbl_id) {
177 		dst = ip6_route_input_lookup(net, skb->dev, &fl6, skb, flags);
178 	} else {
179 		struct fib6_table *table;
180 
181 		table = fib6_get_table(net, tbl_id);
182 		if (!table)
183 			goto out;
184 
185 		rt = ip6_pol_route(net, table, 0, &fl6, skb, flags);
186 		dst = &rt->dst;
187 	}
188 
189 	/* we want to discard traffic destined for local packet processing,
190 	 * if @local_delivery is set to false.
191 	 */
192 	if (!local_delivery)
193 		dev_flags |= IFF_LOOPBACK;
194 
195 	if (dst && (dst->dev->flags & dev_flags) && !dst->error) {
196 		dst_release(dst);
197 		dst = NULL;
198 	}
199 
200 out:
201 	if (!dst) {
202 		rt = net->ipv6.ip6_blk_hole_entry;
203 		dst = &rt->dst;
204 		dst_hold(dst);
205 	}
206 
207 	skb_dst_drop(skb);
208 	skb_dst_set(skb, dst);
209 	return dst->error;
210 }
211 
seg6_lookup_nexthop(struct sk_buff * skb,struct in6_addr * nhaddr,u32 tbl_id)212 int seg6_lookup_nexthop(struct sk_buff *skb,
213 			struct in6_addr *nhaddr, u32 tbl_id)
214 {
215 	return seg6_lookup_any_nexthop(skb, nhaddr, tbl_id, false);
216 }
217 
218 /* regular endpoint function */
input_action_end(struct sk_buff * skb,struct seg6_local_lwt * slwt)219 static int input_action_end(struct sk_buff *skb, struct seg6_local_lwt *slwt)
220 {
221 	struct ipv6_sr_hdr *srh;
222 
223 	srh = get_and_validate_srh(skb);
224 	if (!srh)
225 		goto drop;
226 
227 	advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
228 
229 	seg6_lookup_nexthop(skb, NULL, 0);
230 
231 	return dst_input(skb);
232 
233 drop:
234 	kfree_skb(skb);
235 	return -EINVAL;
236 }
237 
238 /* regular endpoint, and forward to specified nexthop */
input_action_end_x(struct sk_buff * skb,struct seg6_local_lwt * slwt)239 static int input_action_end_x(struct sk_buff *skb, struct seg6_local_lwt *slwt)
240 {
241 	struct ipv6_sr_hdr *srh;
242 
243 	srh = get_and_validate_srh(skb);
244 	if (!srh)
245 		goto drop;
246 
247 	advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
248 
249 	seg6_lookup_nexthop(skb, &slwt->nh6, 0);
250 
251 	return dst_input(skb);
252 
253 drop:
254 	kfree_skb(skb);
255 	return -EINVAL;
256 }
257 
input_action_end_t(struct sk_buff * skb,struct seg6_local_lwt * slwt)258 static int input_action_end_t(struct sk_buff *skb, struct seg6_local_lwt *slwt)
259 {
260 	struct ipv6_sr_hdr *srh;
261 
262 	srh = get_and_validate_srh(skb);
263 	if (!srh)
264 		goto drop;
265 
266 	advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
267 
268 	seg6_lookup_nexthop(skb, NULL, slwt->table);
269 
270 	return dst_input(skb);
271 
272 drop:
273 	kfree_skb(skb);
274 	return -EINVAL;
275 }
276 
277 /* decapsulate and forward inner L2 frame on specified interface */
input_action_end_dx2(struct sk_buff * skb,struct seg6_local_lwt * slwt)278 static int input_action_end_dx2(struct sk_buff *skb,
279 				struct seg6_local_lwt *slwt)
280 {
281 	struct net *net = dev_net(skb->dev);
282 	struct net_device *odev;
283 	struct ethhdr *eth;
284 
285 	if (!decap_and_validate(skb, IPPROTO_ETHERNET))
286 		goto drop;
287 
288 	if (!pskb_may_pull(skb, ETH_HLEN))
289 		goto drop;
290 
291 	skb_reset_mac_header(skb);
292 	eth = (struct ethhdr *)skb->data;
293 
294 	/* To determine the frame's protocol, we assume it is 802.3. This avoids
295 	 * a call to eth_type_trans(), which is not really relevant for our
296 	 * use case.
297 	 */
298 	if (!eth_proto_is_802_3(eth->h_proto))
299 		goto drop;
300 
301 	odev = dev_get_by_index_rcu(net, slwt->oif);
302 	if (!odev)
303 		goto drop;
304 
305 	/* As we accept Ethernet frames, make sure the egress device is of
306 	 * the correct type.
307 	 */
308 	if (odev->type != ARPHRD_ETHER)
309 		goto drop;
310 
311 	if (!(odev->flags & IFF_UP) || !netif_carrier_ok(odev))
312 		goto drop;
313 
314 	skb_orphan(skb);
315 
316 	if (skb_warn_if_lro(skb))
317 		goto drop;
318 
319 	skb_forward_csum(skb);
320 
321 	if (skb->len - ETH_HLEN > odev->mtu)
322 		goto drop;
323 
324 	skb->dev = odev;
325 	skb->protocol = eth->h_proto;
326 
327 	return dev_queue_xmit(skb);
328 
329 drop:
330 	kfree_skb(skb);
331 	return -EINVAL;
332 }
333 
334 /* decapsulate and forward to specified nexthop */
input_action_end_dx6(struct sk_buff * skb,struct seg6_local_lwt * slwt)335 static int input_action_end_dx6(struct sk_buff *skb,
336 				struct seg6_local_lwt *slwt)
337 {
338 	struct in6_addr *nhaddr = NULL;
339 
340 	/* this function accepts IPv6 encapsulated packets, with either
341 	 * an SRH with SL=0, or no SRH.
342 	 */
343 
344 	if (!decap_and_validate(skb, IPPROTO_IPV6))
345 		goto drop;
346 
347 	if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
348 		goto drop;
349 
350 	/* The inner packet is not associated to any local interface,
351 	 * so we do not call netif_rx().
352 	 *
353 	 * If slwt->nh6 is set to ::, then lookup the nexthop for the
354 	 * inner packet's DA. Otherwise, use the specified nexthop.
355 	 */
356 
357 	if (!ipv6_addr_any(&slwt->nh6))
358 		nhaddr = &slwt->nh6;
359 
360 	skb_set_transport_header(skb, sizeof(struct ipv6hdr));
361 
362 	seg6_lookup_nexthop(skb, nhaddr, 0);
363 
364 	return dst_input(skb);
365 drop:
366 	kfree_skb(skb);
367 	return -EINVAL;
368 }
369 
input_action_end_dx4(struct sk_buff * skb,struct seg6_local_lwt * slwt)370 static int input_action_end_dx4(struct sk_buff *skb,
371 				struct seg6_local_lwt *slwt)
372 {
373 	struct iphdr *iph;
374 	__be32 nhaddr;
375 	int err;
376 
377 	if (!decap_and_validate(skb, IPPROTO_IPIP))
378 		goto drop;
379 
380 	if (!pskb_may_pull(skb, sizeof(struct iphdr)))
381 		goto drop;
382 
383 	skb->protocol = htons(ETH_P_IP);
384 
385 	iph = ip_hdr(skb);
386 
387 	nhaddr = slwt->nh4.s_addr ?: iph->daddr;
388 
389 	skb_dst_drop(skb);
390 
391 	skb_set_transport_header(skb, sizeof(struct iphdr));
392 
393 	err = ip_route_input(skb, nhaddr, iph->saddr, 0, skb->dev);
394 	if (err)
395 		goto drop;
396 
397 	return dst_input(skb);
398 
399 drop:
400 	kfree_skb(skb);
401 	return -EINVAL;
402 }
403 
input_action_end_dt6(struct sk_buff * skb,struct seg6_local_lwt * slwt)404 static int input_action_end_dt6(struct sk_buff *skb,
405 				struct seg6_local_lwt *slwt)
406 {
407 	if (!decap_and_validate(skb, IPPROTO_IPV6))
408 		goto drop;
409 
410 	if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
411 		goto drop;
412 
413 	skb_set_transport_header(skb, sizeof(struct ipv6hdr));
414 
415 	seg6_lookup_any_nexthop(skb, NULL, slwt->table, true);
416 
417 	return dst_input(skb);
418 
419 drop:
420 	kfree_skb(skb);
421 	return -EINVAL;
422 }
423 
424 /* push an SRH on top of the current one */
input_action_end_b6(struct sk_buff * skb,struct seg6_local_lwt * slwt)425 static int input_action_end_b6(struct sk_buff *skb, struct seg6_local_lwt *slwt)
426 {
427 	struct ipv6_sr_hdr *srh;
428 	int err = -EINVAL;
429 
430 	srh = get_and_validate_srh(skb);
431 	if (!srh)
432 		goto drop;
433 
434 	err = seg6_do_srh_inline(skb, slwt->srh);
435 	if (err)
436 		goto drop;
437 
438 	skb_set_transport_header(skb, sizeof(struct ipv6hdr));
439 
440 	seg6_lookup_nexthop(skb, NULL, 0);
441 
442 	return dst_input(skb);
443 
444 drop:
445 	kfree_skb(skb);
446 	return err;
447 }
448 
449 /* encapsulate within an outer IPv6 header and a specified SRH */
input_action_end_b6_encap(struct sk_buff * skb,struct seg6_local_lwt * slwt)450 static int input_action_end_b6_encap(struct sk_buff *skb,
451 				     struct seg6_local_lwt *slwt)
452 {
453 	struct ipv6_sr_hdr *srh;
454 	int err = -EINVAL;
455 
456 	srh = get_and_validate_srh(skb);
457 	if (!srh)
458 		goto drop;
459 
460 	advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
461 
462 	skb_reset_inner_headers(skb);
463 	skb->encapsulation = 1;
464 
465 	err = seg6_do_srh_encap(skb, slwt->srh, IPPROTO_IPV6);
466 	if (err)
467 		goto drop;
468 
469 	skb_set_transport_header(skb, sizeof(struct ipv6hdr));
470 
471 	seg6_lookup_nexthop(skb, NULL, 0);
472 
473 	return dst_input(skb);
474 
475 drop:
476 	kfree_skb(skb);
477 	return err;
478 }
479 
480 DEFINE_PER_CPU(struct seg6_bpf_srh_state, seg6_bpf_srh_states);
481 
seg6_bpf_has_valid_srh(struct sk_buff * skb)482 bool seg6_bpf_has_valid_srh(struct sk_buff *skb)
483 {
484 	struct seg6_bpf_srh_state *srh_state =
485 		this_cpu_ptr(&seg6_bpf_srh_states);
486 	struct ipv6_sr_hdr *srh = srh_state->srh;
487 
488 	if (unlikely(srh == NULL))
489 		return false;
490 
491 	if (unlikely(!srh_state->valid)) {
492 		if ((srh_state->hdrlen & 7) != 0)
493 			return false;
494 
495 		srh->hdrlen = (u8)(srh_state->hdrlen >> 3);
496 		if (!seg6_validate_srh(srh, (srh->hdrlen + 1) << 3, true))
497 			return false;
498 
499 		srh_state->valid = true;
500 	}
501 
502 	return true;
503 }
504 
input_action_end_bpf(struct sk_buff * skb,struct seg6_local_lwt * slwt)505 static int input_action_end_bpf(struct sk_buff *skb,
506 				struct seg6_local_lwt *slwt)
507 {
508 	struct seg6_bpf_srh_state *srh_state =
509 		this_cpu_ptr(&seg6_bpf_srh_states);
510 	struct ipv6_sr_hdr *srh;
511 	int ret;
512 
513 	srh = get_and_validate_srh(skb);
514 	if (!srh) {
515 		kfree_skb(skb);
516 		return -EINVAL;
517 	}
518 	advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
519 
520 	/* preempt_disable is needed to protect the per-CPU buffer srh_state,
521 	 * which is also accessed by the bpf_lwt_seg6_* helpers
522 	 */
523 	preempt_disable();
524 	srh_state->srh = srh;
525 	srh_state->hdrlen = srh->hdrlen << 3;
526 	srh_state->valid = true;
527 
528 	rcu_read_lock();
529 	bpf_compute_data_pointers(skb);
530 	ret = bpf_prog_run_save_cb(slwt->bpf.prog, skb);
531 	rcu_read_unlock();
532 
533 	switch (ret) {
534 	case BPF_OK:
535 	case BPF_REDIRECT:
536 		break;
537 	case BPF_DROP:
538 		goto drop;
539 	default:
540 		pr_warn_once("bpf-seg6local: Illegal return value %u\n", ret);
541 		goto drop;
542 	}
543 
544 	if (srh_state->srh && !seg6_bpf_has_valid_srh(skb))
545 		goto drop;
546 
547 	preempt_enable();
548 	if (ret != BPF_REDIRECT)
549 		seg6_lookup_nexthop(skb, NULL, 0);
550 
551 	return dst_input(skb);
552 
553 drop:
554 	preempt_enable();
555 	kfree_skb(skb);
556 	return -EINVAL;
557 }
558 
559 static struct seg6_action_desc seg6_action_table[] = {
560 	{
561 		.action		= SEG6_LOCAL_ACTION_END,
562 		.attrs		= 0,
563 		.input		= input_action_end,
564 	},
565 	{
566 		.action		= SEG6_LOCAL_ACTION_END_X,
567 		.attrs		= (1 << SEG6_LOCAL_NH6),
568 		.input		= input_action_end_x,
569 	},
570 	{
571 		.action		= SEG6_LOCAL_ACTION_END_T,
572 		.attrs		= (1 << SEG6_LOCAL_TABLE),
573 		.input		= input_action_end_t,
574 	},
575 	{
576 		.action		= SEG6_LOCAL_ACTION_END_DX2,
577 		.attrs		= (1 << SEG6_LOCAL_OIF),
578 		.input		= input_action_end_dx2,
579 	},
580 	{
581 		.action		= SEG6_LOCAL_ACTION_END_DX6,
582 		.attrs		= (1 << SEG6_LOCAL_NH6),
583 		.input		= input_action_end_dx6,
584 	},
585 	{
586 		.action		= SEG6_LOCAL_ACTION_END_DX4,
587 		.attrs		= (1 << SEG6_LOCAL_NH4),
588 		.input		= input_action_end_dx4,
589 	},
590 	{
591 		.action		= SEG6_LOCAL_ACTION_END_DT6,
592 		.attrs		= (1 << SEG6_LOCAL_TABLE),
593 		.input		= input_action_end_dt6,
594 	},
595 	{
596 		.action		= SEG6_LOCAL_ACTION_END_B6,
597 		.attrs		= (1 << SEG6_LOCAL_SRH),
598 		.input		= input_action_end_b6,
599 	},
600 	{
601 		.action		= SEG6_LOCAL_ACTION_END_B6_ENCAP,
602 		.attrs		= (1 << SEG6_LOCAL_SRH),
603 		.input		= input_action_end_b6_encap,
604 		.static_headroom	= sizeof(struct ipv6hdr),
605 	},
606 	{
607 		.action		= SEG6_LOCAL_ACTION_END_BPF,
608 		.attrs		= (1 << SEG6_LOCAL_BPF),
609 		.input		= input_action_end_bpf,
610 	},
611 
612 };
613 
__get_action_desc(int action)614 static struct seg6_action_desc *__get_action_desc(int action)
615 {
616 	struct seg6_action_desc *desc;
617 	int i, count;
618 
619 	count = ARRAY_SIZE(seg6_action_table);
620 	for (i = 0; i < count; i++) {
621 		desc = &seg6_action_table[i];
622 		if (desc->action == action)
623 			return desc;
624 	}
625 
626 	return NULL;
627 }
628 
seg6_local_input(struct sk_buff * skb)629 static int seg6_local_input(struct sk_buff *skb)
630 {
631 	struct dst_entry *orig_dst = skb_dst(skb);
632 	struct seg6_action_desc *desc;
633 	struct seg6_local_lwt *slwt;
634 
635 	if (skb->protocol != htons(ETH_P_IPV6)) {
636 		kfree_skb(skb);
637 		return -EINVAL;
638 	}
639 
640 	slwt = seg6_local_lwtunnel(orig_dst->lwtstate);
641 	desc = slwt->desc;
642 
643 	return desc->input(skb, slwt);
644 }
645 
646 static const struct nla_policy seg6_local_policy[SEG6_LOCAL_MAX + 1] = {
647 	[SEG6_LOCAL_ACTION]	= { .type = NLA_U32 },
648 	[SEG6_LOCAL_SRH]	= { .type = NLA_BINARY },
649 	[SEG6_LOCAL_TABLE]	= { .type = NLA_U32 },
650 	[SEG6_LOCAL_NH4]	= NLA_POLICY_EXACT_LEN(sizeof(struct in_addr)),
651 	[SEG6_LOCAL_NH6]	= NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)),
652 	[SEG6_LOCAL_IIF]	= { .type = NLA_U32 },
653 	[SEG6_LOCAL_OIF]	= { .type = NLA_U32 },
654 	[SEG6_LOCAL_BPF]	= { .type = NLA_NESTED },
655 };
656 
parse_nla_srh(struct nlattr ** attrs,struct seg6_local_lwt * slwt)657 static int parse_nla_srh(struct nlattr **attrs, struct seg6_local_lwt *slwt)
658 {
659 	struct ipv6_sr_hdr *srh;
660 	int len;
661 
662 	srh = nla_data(attrs[SEG6_LOCAL_SRH]);
663 	len = nla_len(attrs[SEG6_LOCAL_SRH]);
664 
665 	/* SRH must contain at least one segment */
666 	if (len < sizeof(*srh) + sizeof(struct in6_addr))
667 		return -EINVAL;
668 
669 	if (!seg6_validate_srh(srh, len, false))
670 		return -EINVAL;
671 
672 	slwt->srh = kmemdup(srh, len, GFP_KERNEL);
673 	if (!slwt->srh)
674 		return -ENOMEM;
675 
676 	slwt->headroom += len;
677 
678 	return 0;
679 }
680 
put_nla_srh(struct sk_buff * skb,struct seg6_local_lwt * slwt)681 static int put_nla_srh(struct sk_buff *skb, struct seg6_local_lwt *slwt)
682 {
683 	struct ipv6_sr_hdr *srh;
684 	struct nlattr *nla;
685 	int len;
686 
687 	srh = slwt->srh;
688 	len = (srh->hdrlen + 1) << 3;
689 
690 	nla = nla_reserve(skb, SEG6_LOCAL_SRH, len);
691 	if (!nla)
692 		return -EMSGSIZE;
693 
694 	memcpy(nla_data(nla), srh, len);
695 
696 	return 0;
697 }
698 
cmp_nla_srh(struct seg6_local_lwt * a,struct seg6_local_lwt * b)699 static int cmp_nla_srh(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
700 {
701 	int len = (a->srh->hdrlen + 1) << 3;
702 
703 	if (len != ((b->srh->hdrlen + 1) << 3))
704 		return 1;
705 
706 	return memcmp(a->srh, b->srh, len);
707 }
708 
parse_nla_table(struct nlattr ** attrs,struct seg6_local_lwt * slwt)709 static int parse_nla_table(struct nlattr **attrs, struct seg6_local_lwt *slwt)
710 {
711 	slwt->table = nla_get_u32(attrs[SEG6_LOCAL_TABLE]);
712 
713 	return 0;
714 }
715 
put_nla_table(struct sk_buff * skb,struct seg6_local_lwt * slwt)716 static int put_nla_table(struct sk_buff *skb, struct seg6_local_lwt *slwt)
717 {
718 	if (nla_put_u32(skb, SEG6_LOCAL_TABLE, slwt->table))
719 		return -EMSGSIZE;
720 
721 	return 0;
722 }
723 
cmp_nla_table(struct seg6_local_lwt * a,struct seg6_local_lwt * b)724 static int cmp_nla_table(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
725 {
726 	if (a->table != b->table)
727 		return 1;
728 
729 	return 0;
730 }
731 
parse_nla_nh4(struct nlattr ** attrs,struct seg6_local_lwt * slwt)732 static int parse_nla_nh4(struct nlattr **attrs, struct seg6_local_lwt *slwt)
733 {
734 	memcpy(&slwt->nh4, nla_data(attrs[SEG6_LOCAL_NH4]),
735 	       sizeof(struct in_addr));
736 
737 	return 0;
738 }
739 
put_nla_nh4(struct sk_buff * skb,struct seg6_local_lwt * slwt)740 static int put_nla_nh4(struct sk_buff *skb, struct seg6_local_lwt *slwt)
741 {
742 	struct nlattr *nla;
743 
744 	nla = nla_reserve(skb, SEG6_LOCAL_NH4, sizeof(struct in_addr));
745 	if (!nla)
746 		return -EMSGSIZE;
747 
748 	memcpy(nla_data(nla), &slwt->nh4, sizeof(struct in_addr));
749 
750 	return 0;
751 }
752 
cmp_nla_nh4(struct seg6_local_lwt * a,struct seg6_local_lwt * b)753 static int cmp_nla_nh4(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
754 {
755 	return memcmp(&a->nh4, &b->nh4, sizeof(struct in_addr));
756 }
757 
parse_nla_nh6(struct nlattr ** attrs,struct seg6_local_lwt * slwt)758 static int parse_nla_nh6(struct nlattr **attrs, struct seg6_local_lwt *slwt)
759 {
760 	memcpy(&slwt->nh6, nla_data(attrs[SEG6_LOCAL_NH6]),
761 	       sizeof(struct in6_addr));
762 
763 	return 0;
764 }
765 
put_nla_nh6(struct sk_buff * skb,struct seg6_local_lwt * slwt)766 static int put_nla_nh6(struct sk_buff *skb, struct seg6_local_lwt *slwt)
767 {
768 	struct nlattr *nla;
769 
770 	nla = nla_reserve(skb, SEG6_LOCAL_NH6, sizeof(struct in6_addr));
771 	if (!nla)
772 		return -EMSGSIZE;
773 
774 	memcpy(nla_data(nla), &slwt->nh6, sizeof(struct in6_addr));
775 
776 	return 0;
777 }
778 
cmp_nla_nh6(struct seg6_local_lwt * a,struct seg6_local_lwt * b)779 static int cmp_nla_nh6(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
780 {
781 	return memcmp(&a->nh6, &b->nh6, sizeof(struct in6_addr));
782 }
783 
parse_nla_iif(struct nlattr ** attrs,struct seg6_local_lwt * slwt)784 static int parse_nla_iif(struct nlattr **attrs, struct seg6_local_lwt *slwt)
785 {
786 	slwt->iif = nla_get_u32(attrs[SEG6_LOCAL_IIF]);
787 
788 	return 0;
789 }
790 
put_nla_iif(struct sk_buff * skb,struct seg6_local_lwt * slwt)791 static int put_nla_iif(struct sk_buff *skb, struct seg6_local_lwt *slwt)
792 {
793 	if (nla_put_u32(skb, SEG6_LOCAL_IIF, slwt->iif))
794 		return -EMSGSIZE;
795 
796 	return 0;
797 }
798 
cmp_nla_iif(struct seg6_local_lwt * a,struct seg6_local_lwt * b)799 static int cmp_nla_iif(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
800 {
801 	if (a->iif != b->iif)
802 		return 1;
803 
804 	return 0;
805 }
806 
parse_nla_oif(struct nlattr ** attrs,struct seg6_local_lwt * slwt)807 static int parse_nla_oif(struct nlattr **attrs, struct seg6_local_lwt *slwt)
808 {
809 	slwt->oif = nla_get_u32(attrs[SEG6_LOCAL_OIF]);
810 
811 	return 0;
812 }
813 
put_nla_oif(struct sk_buff * skb,struct seg6_local_lwt * slwt)814 static int put_nla_oif(struct sk_buff *skb, struct seg6_local_lwt *slwt)
815 {
816 	if (nla_put_u32(skb, SEG6_LOCAL_OIF, slwt->oif))
817 		return -EMSGSIZE;
818 
819 	return 0;
820 }
821 
cmp_nla_oif(struct seg6_local_lwt * a,struct seg6_local_lwt * b)822 static int cmp_nla_oif(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
823 {
824 	if (a->oif != b->oif)
825 		return 1;
826 
827 	return 0;
828 }
829 
830 #define MAX_PROG_NAME 256
831 static const struct nla_policy bpf_prog_policy[SEG6_LOCAL_BPF_PROG_MAX + 1] = {
832 	[SEG6_LOCAL_BPF_PROG]	   = { .type = NLA_U32, },
833 	[SEG6_LOCAL_BPF_PROG_NAME] = { .type = NLA_NUL_STRING,
834 				       .len = MAX_PROG_NAME },
835 };
836 
parse_nla_bpf(struct nlattr ** attrs,struct seg6_local_lwt * slwt)837 static int parse_nla_bpf(struct nlattr **attrs, struct seg6_local_lwt *slwt)
838 {
839 	struct nlattr *tb[SEG6_LOCAL_BPF_PROG_MAX + 1];
840 	struct bpf_prog *p;
841 	int ret;
842 	u32 fd;
843 
844 	ret = nla_parse_nested_deprecated(tb, SEG6_LOCAL_BPF_PROG_MAX,
845 					  attrs[SEG6_LOCAL_BPF],
846 					  bpf_prog_policy, NULL);
847 	if (ret < 0)
848 		return ret;
849 
850 	if (!tb[SEG6_LOCAL_BPF_PROG] || !tb[SEG6_LOCAL_BPF_PROG_NAME])
851 		return -EINVAL;
852 
853 	slwt->bpf.name = nla_memdup(tb[SEG6_LOCAL_BPF_PROG_NAME], GFP_KERNEL);
854 	if (!slwt->bpf.name)
855 		return -ENOMEM;
856 
857 	fd = nla_get_u32(tb[SEG6_LOCAL_BPF_PROG]);
858 	p = bpf_prog_get_type(fd, BPF_PROG_TYPE_LWT_SEG6LOCAL);
859 	if (IS_ERR(p)) {
860 		kfree(slwt->bpf.name);
861 		return PTR_ERR(p);
862 	}
863 
864 	slwt->bpf.prog = p;
865 	return 0;
866 }
867 
put_nla_bpf(struct sk_buff * skb,struct seg6_local_lwt * slwt)868 static int put_nla_bpf(struct sk_buff *skb, struct seg6_local_lwt *slwt)
869 {
870 	struct nlattr *nest;
871 
872 	if (!slwt->bpf.prog)
873 		return 0;
874 
875 	nest = nla_nest_start_noflag(skb, SEG6_LOCAL_BPF);
876 	if (!nest)
877 		return -EMSGSIZE;
878 
879 	if (nla_put_u32(skb, SEG6_LOCAL_BPF_PROG, slwt->bpf.prog->aux->id))
880 		return -EMSGSIZE;
881 
882 	if (slwt->bpf.name &&
883 	    nla_put_string(skb, SEG6_LOCAL_BPF_PROG_NAME, slwt->bpf.name))
884 		return -EMSGSIZE;
885 
886 	return nla_nest_end(skb, nest);
887 }
888 
cmp_nla_bpf(struct seg6_local_lwt * a,struct seg6_local_lwt * b)889 static int cmp_nla_bpf(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
890 {
891 	if (!a->bpf.name && !b->bpf.name)
892 		return 0;
893 
894 	if (!a->bpf.name || !b->bpf.name)
895 		return 1;
896 
897 	return strcmp(a->bpf.name, b->bpf.name);
898 }
899 
900 struct seg6_action_param {
901 	int (*parse)(struct nlattr **attrs, struct seg6_local_lwt *slwt);
902 	int (*put)(struct sk_buff *skb, struct seg6_local_lwt *slwt);
903 	int (*cmp)(struct seg6_local_lwt *a, struct seg6_local_lwt *b);
904 };
905 
906 static struct seg6_action_param seg6_action_params[SEG6_LOCAL_MAX + 1] = {
907 	[SEG6_LOCAL_SRH]	= { .parse = parse_nla_srh,
908 				    .put = put_nla_srh,
909 				    .cmp = cmp_nla_srh },
910 
911 	[SEG6_LOCAL_TABLE]	= { .parse = parse_nla_table,
912 				    .put = put_nla_table,
913 				    .cmp = cmp_nla_table },
914 
915 	[SEG6_LOCAL_NH4]	= { .parse = parse_nla_nh4,
916 				    .put = put_nla_nh4,
917 				    .cmp = cmp_nla_nh4 },
918 
919 	[SEG6_LOCAL_NH6]	= { .parse = parse_nla_nh6,
920 				    .put = put_nla_nh6,
921 				    .cmp = cmp_nla_nh6 },
922 
923 	[SEG6_LOCAL_IIF]	= { .parse = parse_nla_iif,
924 				    .put = put_nla_iif,
925 				    .cmp = cmp_nla_iif },
926 
927 	[SEG6_LOCAL_OIF]	= { .parse = parse_nla_oif,
928 				    .put = put_nla_oif,
929 				    .cmp = cmp_nla_oif },
930 
931 	[SEG6_LOCAL_BPF]	= { .parse = parse_nla_bpf,
932 				    .put = put_nla_bpf,
933 				    .cmp = cmp_nla_bpf },
934 
935 };
936 
parse_nla_action(struct nlattr ** attrs,struct seg6_local_lwt * slwt)937 static int parse_nla_action(struct nlattr **attrs, struct seg6_local_lwt *slwt)
938 {
939 	struct seg6_action_param *param;
940 	struct seg6_action_desc *desc;
941 	int i, err;
942 
943 	desc = __get_action_desc(slwt->action);
944 	if (!desc)
945 		return -EINVAL;
946 
947 	if (!desc->input)
948 		return -EOPNOTSUPP;
949 
950 	slwt->desc = desc;
951 	slwt->headroom += desc->static_headroom;
952 
953 	for (i = 0; i < SEG6_LOCAL_MAX + 1; i++) {
954 		if (desc->attrs & (1 << i)) {
955 			if (!attrs[i])
956 				return -EINVAL;
957 
958 			param = &seg6_action_params[i];
959 
960 			err = param->parse(attrs, slwt);
961 			if (err < 0)
962 				return err;
963 		}
964 	}
965 
966 	return 0;
967 }
968 
seg6_local_build_state(struct net * net,struct nlattr * nla,unsigned int family,const void * cfg,struct lwtunnel_state ** ts,struct netlink_ext_ack * extack)969 static int seg6_local_build_state(struct net *net, struct nlattr *nla,
970 				  unsigned int family, const void *cfg,
971 				  struct lwtunnel_state **ts,
972 				  struct netlink_ext_ack *extack)
973 {
974 	struct nlattr *tb[SEG6_LOCAL_MAX + 1];
975 	struct lwtunnel_state *newts;
976 	struct seg6_local_lwt *slwt;
977 	int err;
978 
979 	if (family != AF_INET6)
980 		return -EINVAL;
981 
982 	err = nla_parse_nested_deprecated(tb, SEG6_LOCAL_MAX, nla,
983 					  seg6_local_policy, extack);
984 
985 	if (err < 0)
986 		return err;
987 
988 	if (!tb[SEG6_LOCAL_ACTION])
989 		return -EINVAL;
990 
991 	newts = lwtunnel_state_alloc(sizeof(*slwt));
992 	if (!newts)
993 		return -ENOMEM;
994 
995 	slwt = seg6_local_lwtunnel(newts);
996 	slwt->action = nla_get_u32(tb[SEG6_LOCAL_ACTION]);
997 
998 	err = parse_nla_action(tb, slwt);
999 	if (err < 0)
1000 		goto out_free;
1001 
1002 	newts->type = LWTUNNEL_ENCAP_SEG6_LOCAL;
1003 	newts->flags = LWTUNNEL_STATE_INPUT_REDIRECT;
1004 	newts->headroom = slwt->headroom;
1005 
1006 	*ts = newts;
1007 
1008 	return 0;
1009 
1010 out_free:
1011 	kfree(slwt->srh);
1012 	kfree(newts);
1013 	return err;
1014 }
1015 
seg6_local_destroy_state(struct lwtunnel_state * lwt)1016 static void seg6_local_destroy_state(struct lwtunnel_state *lwt)
1017 {
1018 	struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
1019 
1020 	kfree(slwt->srh);
1021 
1022 	if (slwt->desc->attrs & (1 << SEG6_LOCAL_BPF)) {
1023 		kfree(slwt->bpf.name);
1024 		bpf_prog_put(slwt->bpf.prog);
1025 	}
1026 
1027 	return;
1028 }
1029 
seg6_local_fill_encap(struct sk_buff * skb,struct lwtunnel_state * lwt)1030 static int seg6_local_fill_encap(struct sk_buff *skb,
1031 				 struct lwtunnel_state *lwt)
1032 {
1033 	struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
1034 	struct seg6_action_param *param;
1035 	int i, err;
1036 
1037 	if (nla_put_u32(skb, SEG6_LOCAL_ACTION, slwt->action))
1038 		return -EMSGSIZE;
1039 
1040 	for (i = 0; i < SEG6_LOCAL_MAX + 1; i++) {
1041 		if (slwt->desc->attrs & (1 << i)) {
1042 			param = &seg6_action_params[i];
1043 			err = param->put(skb, slwt);
1044 			if (err < 0)
1045 				return err;
1046 		}
1047 	}
1048 
1049 	return 0;
1050 }
1051 
seg6_local_get_encap_size(struct lwtunnel_state * lwt)1052 static int seg6_local_get_encap_size(struct lwtunnel_state *lwt)
1053 {
1054 	struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
1055 	unsigned long attrs;
1056 	int nlsize;
1057 
1058 	nlsize = nla_total_size(4); /* action */
1059 
1060 	attrs = slwt->desc->attrs;
1061 
1062 	if (attrs & (1 << SEG6_LOCAL_SRH))
1063 		nlsize += nla_total_size((slwt->srh->hdrlen + 1) << 3);
1064 
1065 	if (attrs & (1 << SEG6_LOCAL_TABLE))
1066 		nlsize += nla_total_size(4);
1067 
1068 	if (attrs & (1 << SEG6_LOCAL_NH4))
1069 		nlsize += nla_total_size(4);
1070 
1071 	if (attrs & (1 << SEG6_LOCAL_NH6))
1072 		nlsize += nla_total_size(16);
1073 
1074 	if (attrs & (1 << SEG6_LOCAL_IIF))
1075 		nlsize += nla_total_size(4);
1076 
1077 	if (attrs & (1 << SEG6_LOCAL_OIF))
1078 		nlsize += nla_total_size(4);
1079 
1080 	if (attrs & (1 << SEG6_LOCAL_BPF))
1081 		nlsize += nla_total_size(sizeof(struct nlattr)) +
1082 		       nla_total_size(MAX_PROG_NAME) +
1083 		       nla_total_size(4);
1084 
1085 	return nlsize;
1086 }
1087 
seg6_local_cmp_encap(struct lwtunnel_state * a,struct lwtunnel_state * b)1088 static int seg6_local_cmp_encap(struct lwtunnel_state *a,
1089 				struct lwtunnel_state *b)
1090 {
1091 	struct seg6_local_lwt *slwt_a, *slwt_b;
1092 	struct seg6_action_param *param;
1093 	int i;
1094 
1095 	slwt_a = seg6_local_lwtunnel(a);
1096 	slwt_b = seg6_local_lwtunnel(b);
1097 
1098 	if (slwt_a->action != slwt_b->action)
1099 		return 1;
1100 
1101 	if (slwt_a->desc->attrs != slwt_b->desc->attrs)
1102 		return 1;
1103 
1104 	for (i = 0; i < SEG6_LOCAL_MAX + 1; i++) {
1105 		if (slwt_a->desc->attrs & (1 << i)) {
1106 			param = &seg6_action_params[i];
1107 			if (param->cmp(slwt_a, slwt_b))
1108 				return 1;
1109 		}
1110 	}
1111 
1112 	return 0;
1113 }
1114 
1115 static const struct lwtunnel_encap_ops seg6_local_ops = {
1116 	.build_state	= seg6_local_build_state,
1117 	.destroy_state	= seg6_local_destroy_state,
1118 	.input		= seg6_local_input,
1119 	.fill_encap	= seg6_local_fill_encap,
1120 	.get_encap_size	= seg6_local_get_encap_size,
1121 	.cmp_encap	= seg6_local_cmp_encap,
1122 	.owner		= THIS_MODULE,
1123 };
1124 
seg6_local_init(void)1125 int __init seg6_local_init(void)
1126 {
1127 	return lwtunnel_encap_add_ops(&seg6_local_ops,
1128 				      LWTUNNEL_ENCAP_SEG6_LOCAL);
1129 }
1130 
seg6_local_exit(void)1131 void seg6_local_exit(void)
1132 {
1133 	lwtunnel_encap_del_ops(&seg6_local_ops, LWTUNNEL_ENCAP_SEG6_LOCAL);
1134 }
1135