]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - net/ipv6/seg6_local.c
Merge tag 'batadv-next-pullrequest-20210208' of git://git.open-mesh.org/linux-merge
[mirror_ubuntu-jammy-kernel.git] / net / ipv6 / seg6_local.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
d1df6fd8
DL
2/*
3 * SR-IPv6 implementation
4 *
004d4b27 5 * Authors:
d1df6fd8 6 * David Lebrun <david.lebrun@uclouvain.be>
004d4b27 7 * eBPF support: Mathieu Xhonneux <m.xhonneux@gmail.com>
d1df6fd8
DL
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>
62ebaeae 26#include <net/ip_tunnels.h>
d1df6fd8
DL
27#ifdef CONFIG_IPV6_SEG6_HMAC
28#include <net/seg6_hmac.h>
29#endif
1c1e761e 30#include <net/seg6_local.h>
891ef8dd 31#include <linux/etherdevice.h>
004d4b27 32#include <linux/bpf.h>
d1df6fd8
DL
33
34struct seg6_local_lwt;
35
cfdf64a0
AM
36/* callbacks used for customizing the creation and destruction of a behavior */
37struct seg6_local_lwtunnel_ops {
38 int (*build_state)(struct seg6_local_lwt *slwt, const void *cfg,
39 struct netlink_ext_ack *extack);
40 void (*destroy_state)(struct seg6_local_lwt *slwt);
41};
42
d1df6fd8
DL
43struct seg6_action_desc {
44 int action;
45 unsigned long attrs;
0a3021f1
AM
46
47 /* The optattrs field is used for specifying all the optional
48 * attributes supported by a specific behavior.
49 * It means that if one of these attributes is not provided in the
50 * netlink message during the behavior creation, no errors will be
51 * returned to the userspace.
52 *
53 * Each attribute can be only of two types (mutually exclusive):
54 * 1) required or 2) optional.
55 * Every user MUST obey to this rule! If you set an attribute as
56 * required the same attribute CANNOT be set as optional and vice
57 * versa.
58 */
59 unsigned long optattrs;
60
d1df6fd8
DL
61 int (*input)(struct sk_buff *skb, struct seg6_local_lwt *slwt);
62 int static_headroom;
cfdf64a0
AM
63
64 struct seg6_local_lwtunnel_ops slwt_ops;
d1df6fd8
DL
65};
66
004d4b27
MX
67struct bpf_lwt_prog {
68 struct bpf_prog *prog;
69 char *name;
70};
71
664d6f86
AM
72enum seg6_end_dt_mode {
73 DT_INVALID_MODE = -EINVAL,
74 DT_LEGACY_MODE = 0,
75 DT_VRF_MODE = 1,
76};
77
78struct seg6_end_dt_info {
79 enum seg6_end_dt_mode mode;
80
81 struct net *net;
82 /* VRF device associated to the routing table used by the SRv6
83 * End.DT4/DT6 behavior for routing IPv4/IPv6 packets.
84 */
85 int vrf_ifindex;
86 int vrf_table;
87
88 /* tunneled packet proto and family (IPv4 or IPv6) */
89 __be16 proto;
90 u16 family;
91 int hdrlen;
92};
93
d1df6fd8
DL
94struct seg6_local_lwt {
95 int action;
96 struct ipv6_sr_hdr *srh;
97 int table;
98 struct in_addr nh4;
99 struct in6_addr nh6;
100 int iif;
101 int oif;
004d4b27 102 struct bpf_lwt_prog bpf;
664d6f86
AM
103#ifdef CONFIG_NET_L3_MASTER_DEV
104 struct seg6_end_dt_info dt_info;
105#endif
d1df6fd8
DL
106
107 int headroom;
108 struct seg6_action_desc *desc;
0a3021f1
AM
109 /* unlike the required attrs, we have to track the optional attributes
110 * that have been effectively parsed.
111 */
112 unsigned long parsed_optattrs;
d1df6fd8
DL
113};
114
115static struct seg6_local_lwt *seg6_local_lwtunnel(struct lwtunnel_state *lwt)
116{
117 return (struct seg6_local_lwt *)lwt->data;
118}
119
140f04c3
DL
120static struct ipv6_sr_hdr *get_srh(struct sk_buff *skb)
121{
122 struct ipv6_sr_hdr *srh;
5829d70b 123 int len, srhoff = 0;
140f04c3 124
5829d70b
AA
125 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
126 return NULL;
127
128 if (!pskb_may_pull(skb, srhoff + sizeof(*srh)))
129 return NULL;
130
131 srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
132
140f04c3
DL
133 len = (srh->hdrlen + 1) << 3;
134
5829d70b 135 if (!pskb_may_pull(skb, srhoff + len))
140f04c3
DL
136 return NULL;
137
7f91ed8c
AM
138 /* note that pskb_may_pull may change pointers in header;
139 * for this reason it is necessary to reload them when needed.
140 */
141 srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
142
bb986a50 143 if (!seg6_validate_srh(srh, len, true))
140f04c3
DL
144 return NULL;
145
146 return srh;
147}
148
149static struct ipv6_sr_hdr *get_and_validate_srh(struct sk_buff *skb)
150{
151 struct ipv6_sr_hdr *srh;
152
153 srh = get_srh(skb);
154 if (!srh)
155 return NULL;
156
157 if (srh->segments_left == 0)
158 return NULL;
159
160#ifdef CONFIG_IPV6_SEG6_HMAC
161 if (!seg6_hmac_validate_skb(skb))
162 return NULL;
163#endif
164
165 return srh;
166}
167
d7a669dd
DL
168static bool decap_and_validate(struct sk_buff *skb, int proto)
169{
170 struct ipv6_sr_hdr *srh;
171 unsigned int off = 0;
172
173 srh = get_srh(skb);
174 if (srh && srh->segments_left > 0)
175 return false;
176
177#ifdef CONFIG_IPV6_SEG6_HMAC
178 if (srh && !seg6_hmac_validate_skb(skb))
179 return false;
180#endif
181
182 if (ipv6_find_hdr(skb, &off, proto, NULL, NULL) < 0)
183 return false;
184
185 if (!pskb_pull(skb, off))
186 return false;
187
188 skb_postpull_rcsum(skb, skb_network_header(skb), off);
189
190 skb_reset_network_header(skb);
191 skb_reset_transport_header(skb);
62ebaeae
YT
192 if (iptunnel_pull_offloads(skb))
193 return false;
d7a669dd
DL
194
195 return true;
196}
197
198static void advance_nextseg(struct ipv6_sr_hdr *srh, struct in6_addr *daddr)
199{
200 struct in6_addr *addr;
201
202 srh->segments_left--;
203 addr = srh->segments + srh->segments_left;
204 *daddr = *addr;
205}
206
fd1fef0c
AM
207static int
208seg6_lookup_any_nexthop(struct sk_buff *skb, struct in6_addr *nhaddr,
209 u32 tbl_id, bool local_delivery)
d7a669dd
DL
210{
211 struct net *net = dev_net(skb->dev);
212 struct ipv6hdr *hdr = ipv6_hdr(skb);
213 int flags = RT6_LOOKUP_F_HAS_SADDR;
214 struct dst_entry *dst = NULL;
215 struct rt6_info *rt;
216 struct flowi6 fl6;
fd1fef0c 217 int dev_flags = 0;
d7a669dd
DL
218
219 fl6.flowi6_iif = skb->dev->ifindex;
220 fl6.daddr = nhaddr ? *nhaddr : hdr->daddr;
221 fl6.saddr = hdr->saddr;
222 fl6.flowlabel = ip6_flowinfo(hdr);
223 fl6.flowi6_mark = skb->mark;
224 fl6.flowi6_proto = hdr->nexthdr;
225
226 if (nhaddr)
227 fl6.flowi6_flags = FLOWI_FLAG_KNOWN_NH;
228
229 if (!tbl_id) {
b75cc8f9 230 dst = ip6_route_input_lookup(net, skb->dev, &fl6, skb, flags);
d7a669dd
DL
231 } else {
232 struct fib6_table *table;
233
234 table = fib6_get_table(net, tbl_id);
235 if (!table)
236 goto out;
237
b75cc8f9 238 rt = ip6_pol_route(net, table, 0, &fl6, skb, flags);
d7a669dd
DL
239 dst = &rt->dst;
240 }
241
fd1fef0c
AM
242 /* we want to discard traffic destined for local packet processing,
243 * if @local_delivery is set to false.
244 */
245 if (!local_delivery)
246 dev_flags |= IFF_LOOPBACK;
247
248 if (dst && (dst->dev->flags & dev_flags) && !dst->error) {
d7a669dd
DL
249 dst_release(dst);
250 dst = NULL;
251 }
252
253out:
254 if (!dst) {
255 rt = net->ipv6.ip6_blk_hole_entry;
256 dst = &rt->dst;
257 dst_hold(dst);
258 }
259
260 skb_dst_drop(skb);
261 skb_dst_set(skb, dst);
1c1e761e 262 return dst->error;
d7a669dd
DL
263}
264
fd1fef0c
AM
265int seg6_lookup_nexthop(struct sk_buff *skb,
266 struct in6_addr *nhaddr, u32 tbl_id)
267{
268 return seg6_lookup_any_nexthop(skb, nhaddr, tbl_id, false);
269}
270
140f04c3
DL
271/* regular endpoint function */
272static int input_action_end(struct sk_buff *skb, struct seg6_local_lwt *slwt)
273{
274 struct ipv6_sr_hdr *srh;
140f04c3
DL
275
276 srh = get_and_validate_srh(skb);
277 if (!srh)
278 goto drop;
279
d7a669dd 280 advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
140f04c3 281
1c1e761e 282 seg6_lookup_nexthop(skb, NULL, 0);
140f04c3
DL
283
284 return dst_input(skb);
285
286drop:
287 kfree_skb(skb);
288 return -EINVAL;
289}
290
291/* regular endpoint, and forward to specified nexthop */
292static int input_action_end_x(struct sk_buff *skb, struct seg6_local_lwt *slwt)
293{
140f04c3 294 struct ipv6_sr_hdr *srh;
140f04c3
DL
295
296 srh = get_and_validate_srh(skb);
297 if (!srh)
298 goto drop;
299
d7a669dd 300 advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
140f04c3 301
1c1e761e 302 seg6_lookup_nexthop(skb, &slwt->nh6, 0);
140f04c3
DL
303
304 return dst_input(skb);
305
306drop:
307 kfree_skb(skb);
308 return -EINVAL;
309}
310
891ef8dd
DL
311static int input_action_end_t(struct sk_buff *skb, struct seg6_local_lwt *slwt)
312{
313 struct ipv6_sr_hdr *srh;
314
315 srh = get_and_validate_srh(skb);
316 if (!srh)
317 goto drop;
318
319 advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
320
1c1e761e 321 seg6_lookup_nexthop(skb, NULL, slwt->table);
891ef8dd
DL
322
323 return dst_input(skb);
324
325drop:
326 kfree_skb(skb);
327 return -EINVAL;
328}
329
330/* decapsulate and forward inner L2 frame on specified interface */
331static int input_action_end_dx2(struct sk_buff *skb,
332 struct seg6_local_lwt *slwt)
333{
334 struct net *net = dev_net(skb->dev);
335 struct net_device *odev;
336 struct ethhdr *eth;
337
26776253 338 if (!decap_and_validate(skb, IPPROTO_ETHERNET))
891ef8dd
DL
339 goto drop;
340
341 if (!pskb_may_pull(skb, ETH_HLEN))
342 goto drop;
343
344 skb_reset_mac_header(skb);
345 eth = (struct ethhdr *)skb->data;
346
347 /* To determine the frame's protocol, we assume it is 802.3. This avoids
348 * a call to eth_type_trans(), which is not really relevant for our
349 * use case.
350 */
351 if (!eth_proto_is_802_3(eth->h_proto))
352 goto drop;
353
354 odev = dev_get_by_index_rcu(net, slwt->oif);
355 if (!odev)
356 goto drop;
357
358 /* As we accept Ethernet frames, make sure the egress device is of
359 * the correct type.
360 */
361 if (odev->type != ARPHRD_ETHER)
362 goto drop;
363
364 if (!(odev->flags & IFF_UP) || !netif_carrier_ok(odev))
365 goto drop;
366
367 skb_orphan(skb);
368
369 if (skb_warn_if_lro(skb))
370 goto drop;
371
372 skb_forward_csum(skb);
373
374 if (skb->len - ETH_HLEN > odev->mtu)
375 goto drop;
376
377 skb->dev = odev;
378 skb->protocol = eth->h_proto;
379
380 return dev_queue_xmit(skb);
381
382drop:
383 kfree_skb(skb);
384 return -EINVAL;
385}
386
140f04c3
DL
387/* decapsulate and forward to specified nexthop */
388static int input_action_end_dx6(struct sk_buff *skb,
389 struct seg6_local_lwt *slwt)
390{
d7a669dd 391 struct in6_addr *nhaddr = NULL;
140f04c3
DL
392
393 /* this function accepts IPv6 encapsulated packets, with either
394 * an SRH with SL=0, or no SRH.
395 */
396
d7a669dd 397 if (!decap_and_validate(skb, IPPROTO_IPV6))
140f04c3 398 goto drop;
140f04c3 399
d7a669dd 400 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
140f04c3
DL
401 goto drop;
402
140f04c3
DL
403 /* The inner packet is not associated to any local interface,
404 * so we do not call netif_rx().
405 *
406 * If slwt->nh6 is set to ::, then lookup the nexthop for the
407 * inner packet's DA. Otherwise, use the specified nexthop.
408 */
409
d7a669dd
DL
410 if (!ipv6_addr_any(&slwt->nh6))
411 nhaddr = &slwt->nh6;
140f04c3 412
c71644d0
AM
413 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
414
1c1e761e 415 seg6_lookup_nexthop(skb, nhaddr, 0);
140f04c3
DL
416
417 return dst_input(skb);
418drop:
419 kfree_skb(skb);
420 return -EINVAL;
421}
422
891ef8dd
DL
423static int input_action_end_dx4(struct sk_buff *skb,
424 struct seg6_local_lwt *slwt)
425{
426 struct iphdr *iph;
427 __be32 nhaddr;
428 int err;
429
430 if (!decap_and_validate(skb, IPPROTO_IPIP))
431 goto drop;
432
433 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
434 goto drop;
435
436 skb->protocol = htons(ETH_P_IP);
437
438 iph = ip_hdr(skb);
439
440 nhaddr = slwt->nh4.s_addr ?: iph->daddr;
441
442 skb_dst_drop(skb);
443
c71644d0
AM
444 skb_set_transport_header(skb, sizeof(struct iphdr));
445
891ef8dd
DL
446 err = ip_route_input(skb, nhaddr, iph->saddr, 0, skb->dev);
447 if (err)
448 goto drop;
449
450 return dst_input(skb);
451
452drop:
453 kfree_skb(skb);
454 return -EINVAL;
455}
456
664d6f86
AM
457#ifdef CONFIG_NET_L3_MASTER_DEV
458static struct net *fib6_config_get_net(const struct fib6_config *fib6_cfg)
459{
460 const struct nl_info *nli = &fib6_cfg->fc_nlinfo;
461
462 return nli->nl_net;
463}
464
465static int __seg6_end_dt_vrf_build(struct seg6_local_lwt *slwt, const void *cfg,
466 u16 family, struct netlink_ext_ack *extack)
467{
468 struct seg6_end_dt_info *info = &slwt->dt_info;
469 int vrf_ifindex;
470 struct net *net;
471
472 net = fib6_config_get_net(cfg);
473
474 /* note that vrf_table was already set by parse_nla_vrftable() */
475 vrf_ifindex = l3mdev_ifindex_lookup_by_table_id(L3MDEV_TYPE_VRF, net,
476 info->vrf_table);
477 if (vrf_ifindex < 0) {
478 if (vrf_ifindex == -EPERM) {
479 NL_SET_ERR_MSG(extack,
480 "Strict mode for VRF is disabled");
481 } else if (vrf_ifindex == -ENODEV) {
482 NL_SET_ERR_MSG(extack,
483 "Table has no associated VRF device");
484 } else {
485 pr_debug("seg6local: SRv6 End.DT* creation error=%d\n",
486 vrf_ifindex);
487 }
488
489 return vrf_ifindex;
490 }
491
492 info->net = net;
493 info->vrf_ifindex = vrf_ifindex;
494
495 switch (family) {
496 case AF_INET:
497 info->proto = htons(ETH_P_IP);
498 info->hdrlen = sizeof(struct iphdr);
499 break;
20a081b7
AM
500 case AF_INET6:
501 info->proto = htons(ETH_P_IPV6);
502 info->hdrlen = sizeof(struct ipv6hdr);
503 break;
664d6f86
AM
504 default:
505 return -EINVAL;
506 }
507
508 info->family = family;
509 info->mode = DT_VRF_MODE;
510
511 return 0;
512}
513
514/* The SRv6 End.DT4/DT6 behavior extracts the inner (IPv4/IPv6) packet and
515 * routes the IPv4/IPv6 packet by looking at the configured routing table.
516 *
517 * In the SRv6 End.DT4/DT6 use case, we can receive traffic (IPv6+Segment
518 * Routing Header packets) from several interfaces and the outer IPv6
519 * destination address (DA) is used for retrieving the specific instance of the
520 * End.DT4/DT6 behavior that should process the packets.
521 *
522 * However, the inner IPv4/IPv6 packet is not really bound to any receiving
523 * interface and thus the End.DT4/DT6 sets the VRF (associated with the
524 * corresponding routing table) as the *receiving* interface.
525 * In other words, the End.DT4/DT6 processes a packet as if it has been received
526 * directly by the VRF (and not by one of its slave devices, if any).
527 * In this way, the VRF interface is used for routing the IPv4/IPv6 packet in
528 * according to the routing table configured by the End.DT4/DT6 instance.
529 *
530 * This design allows you to get some interesting features like:
531 * 1) the statistics on rx packets;
532 * 2) the possibility to install a packet sniffer on the receiving interface
533 * (the VRF one) for looking at the incoming packets;
534 * 3) the possibility to leverage the netfilter prerouting hook for the inner
535 * IPv4 packet.
536 *
537 * This function returns:
538 * - the sk_buff* when the VRF rcv handler has processed the packet correctly;
539 * - NULL when the skb is consumed by the VRF rcv handler;
540 * - a pointer which encodes a negative error number in case of error.
541 * Note that in this case, the function takes care of freeing the skb.
542 */
543static struct sk_buff *end_dt_vrf_rcv(struct sk_buff *skb, u16 family,
544 struct net_device *dev)
545{
546 /* based on l3mdev_ip_rcv; we are only interested in the master */
547 if (unlikely(!netif_is_l3_master(dev) && !netif_has_l3_rx_handler(dev)))
548 goto drop;
549
550 if (unlikely(!dev->l3mdev_ops->l3mdev_l3_rcv))
551 goto drop;
552
553 /* the decap packet IPv4/IPv6 does not come with any mac header info.
554 * We must unset the mac header to allow the VRF device to rebuild it,
555 * just in case there is a sniffer attached on the device.
556 */
557 skb_unset_mac_header(skb);
558
559 skb = dev->l3mdev_ops->l3mdev_l3_rcv(dev, skb, family);
560 if (!skb)
561 /* the skb buffer was consumed by the handler */
562 return NULL;
563
564 /* when a packet is received by a VRF or by one of its slaves, the
565 * master device reference is set into the skb.
566 */
567 if (unlikely(skb->dev != dev || skb->skb_iif != dev->ifindex))
568 goto drop;
569
570 return skb;
571
572drop:
573 kfree_skb(skb);
574 return ERR_PTR(-EINVAL);
575}
576
577static struct net_device *end_dt_get_vrf_rcu(struct sk_buff *skb,
578 struct seg6_end_dt_info *info)
579{
580 int vrf_ifindex = info->vrf_ifindex;
581 struct net *net = info->net;
582
583 if (unlikely(vrf_ifindex < 0))
584 goto error;
585
586 if (unlikely(!net_eq(dev_net(skb->dev), net)))
587 goto error;
588
589 return dev_get_by_index_rcu(net, vrf_ifindex);
590
591error:
592 return NULL;
593}
594
595static struct sk_buff *end_dt_vrf_core(struct sk_buff *skb,
596 struct seg6_local_lwt *slwt)
597{
598 struct seg6_end_dt_info *info = &slwt->dt_info;
599 struct net_device *vrf;
600
601 vrf = end_dt_get_vrf_rcu(skb, info);
602 if (unlikely(!vrf))
603 goto drop;
604
605 skb->protocol = info->proto;
606
607 skb_dst_drop(skb);
608
609 skb_set_transport_header(skb, info->hdrlen);
610
611 return end_dt_vrf_rcv(skb, info->family, vrf);
612
613drop:
614 kfree_skb(skb);
615 return ERR_PTR(-EINVAL);
616}
617
618static int input_action_end_dt4(struct sk_buff *skb,
619 struct seg6_local_lwt *slwt)
620{
621 struct iphdr *iph;
622 int err;
623
624 if (!decap_and_validate(skb, IPPROTO_IPIP))
625 goto drop;
626
627 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
628 goto drop;
629
630 skb = end_dt_vrf_core(skb, slwt);
631 if (!skb)
632 /* packet has been processed and consumed by the VRF */
633 return 0;
634
635 if (IS_ERR(skb))
636 return PTR_ERR(skb);
637
638 iph = ip_hdr(skb);
639
640 err = ip_route_input(skb, iph->daddr, iph->saddr, 0, skb->dev);
641 if (unlikely(err))
642 goto drop;
643
644 return dst_input(skb);
645
646drop:
647 kfree_skb(skb);
648 return -EINVAL;
649}
650
651static int seg6_end_dt4_build(struct seg6_local_lwt *slwt, const void *cfg,
652 struct netlink_ext_ack *extack)
653{
654 return __seg6_end_dt_vrf_build(slwt, cfg, AF_INET, extack);
655}
20a081b7
AM
656
657static enum
658seg6_end_dt_mode seg6_end_dt6_parse_mode(struct seg6_local_lwt *slwt)
659{
660 unsigned long parsed_optattrs = slwt->parsed_optattrs;
661 bool legacy, vrfmode;
662
663 legacy = !!(parsed_optattrs & (1 << SEG6_LOCAL_TABLE));
664 vrfmode = !!(parsed_optattrs & (1 << SEG6_LOCAL_VRFTABLE));
665
666 if (!(legacy ^ vrfmode))
667 /* both are absent or present: invalid DT6 mode */
668 return DT_INVALID_MODE;
669
670 return legacy ? DT_LEGACY_MODE : DT_VRF_MODE;
671}
672
673static enum seg6_end_dt_mode seg6_end_dt6_get_mode(struct seg6_local_lwt *slwt)
674{
675 struct seg6_end_dt_info *info = &slwt->dt_info;
676
677 return info->mode;
678}
679
680static int seg6_end_dt6_build(struct seg6_local_lwt *slwt, const void *cfg,
681 struct netlink_ext_ack *extack)
682{
683 enum seg6_end_dt_mode mode = seg6_end_dt6_parse_mode(slwt);
684 struct seg6_end_dt_info *info = &slwt->dt_info;
685
686 switch (mode) {
687 case DT_LEGACY_MODE:
688 info->mode = DT_LEGACY_MODE;
689 return 0;
690 case DT_VRF_MODE:
691 return __seg6_end_dt_vrf_build(slwt, cfg, AF_INET6, extack);
692 default:
693 NL_SET_ERR_MSG(extack, "table or vrftable must be specified");
694 return -EINVAL;
695 }
696}
664d6f86
AM
697#endif
698
891ef8dd
DL
699static int input_action_end_dt6(struct sk_buff *skb,
700 struct seg6_local_lwt *slwt)
701{
702 if (!decap_and_validate(skb, IPPROTO_IPV6))
703 goto drop;
704
705 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
706 goto drop;
707
20a081b7
AM
708#ifdef CONFIG_NET_L3_MASTER_DEV
709 if (seg6_end_dt6_get_mode(slwt) == DT_LEGACY_MODE)
710 goto legacy_mode;
711
712 /* DT6_VRF_MODE */
713 skb = end_dt_vrf_core(skb, slwt);
714 if (!skb)
715 /* packet has been processed and consumed by the VRF */
716 return 0;
717
718 if (IS_ERR(skb))
719 return PTR_ERR(skb);
720
721 /* note: this time we do not need to specify the table because the VRF
722 * takes care of selecting the correct table.
723 */
724 seg6_lookup_any_nexthop(skb, NULL, 0, true);
725
726 return dst_input(skb);
727
728legacy_mode:
729#endif
c71644d0
AM
730 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
731
fd1fef0c 732 seg6_lookup_any_nexthop(skb, NULL, slwt->table, true);
891ef8dd
DL
733
734 return dst_input(skb);
735
736drop:
737 kfree_skb(skb);
738 return -EINVAL;
739}
740
140f04c3
DL
741/* push an SRH on top of the current one */
742static int input_action_end_b6(struct sk_buff *skb, struct seg6_local_lwt *slwt)
743{
744 struct ipv6_sr_hdr *srh;
745 int err = -EINVAL;
746
747 srh = get_and_validate_srh(skb);
748 if (!srh)
749 goto drop;
750
751 err = seg6_do_srh_inline(skb, slwt->srh);
752 if (err)
753 goto drop;
754
755 ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
756 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
757
1c1e761e 758 seg6_lookup_nexthop(skb, NULL, 0);
140f04c3
DL
759
760 return dst_input(skb);
761
762drop:
763 kfree_skb(skb);
764 return err;
765}
766
767/* encapsulate within an outer IPv6 header and a specified SRH */
768static int input_action_end_b6_encap(struct sk_buff *skb,
769 struct seg6_local_lwt *slwt)
770{
771 struct ipv6_sr_hdr *srh;
140f04c3
DL
772 int err = -EINVAL;
773
774 srh = get_and_validate_srh(skb);
775 if (!srh)
776 goto drop;
777
d7a669dd 778 advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
140f04c3
DL
779
780 skb_reset_inner_headers(skb);
781 skb->encapsulation = 1;
782
32d99d0b 783 err = seg6_do_srh_encap(skb, slwt->srh, IPPROTO_IPV6);
140f04c3
DL
784 if (err)
785 goto drop;
786
787 ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
788 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
789
1c1e761e 790 seg6_lookup_nexthop(skb, NULL, 0);
140f04c3
DL
791
792 return dst_input(skb);
793
794drop:
795 kfree_skb(skb);
796 return err;
797}
798
fe94cc29
MX
799DEFINE_PER_CPU(struct seg6_bpf_srh_state, seg6_bpf_srh_states);
800
486cdf21
MX
801bool seg6_bpf_has_valid_srh(struct sk_buff *skb)
802{
803 struct seg6_bpf_srh_state *srh_state =
804 this_cpu_ptr(&seg6_bpf_srh_states);
805 struct ipv6_sr_hdr *srh = srh_state->srh;
806
807 if (unlikely(srh == NULL))
808 return false;
809
810 if (unlikely(!srh_state->valid)) {
811 if ((srh_state->hdrlen & 7) != 0)
812 return false;
813
814 srh->hdrlen = (u8)(srh_state->hdrlen >> 3);
bb986a50 815 if (!seg6_validate_srh(srh, (srh->hdrlen + 1) << 3, true))
486cdf21
MX
816 return false;
817
818 srh_state->valid = true;
819 }
820
821 return true;
822}
823
004d4b27
MX
824static int input_action_end_bpf(struct sk_buff *skb,
825 struct seg6_local_lwt *slwt)
826{
827 struct seg6_bpf_srh_state *srh_state =
828 this_cpu_ptr(&seg6_bpf_srh_states);
004d4b27 829 struct ipv6_sr_hdr *srh;
004d4b27
MX
830 int ret;
831
832 srh = get_and_validate_srh(skb);
486cdf21
MX
833 if (!srh) {
834 kfree_skb(skb);
835 return -EINVAL;
836 }
004d4b27
MX
837 advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
838
839 /* preempt_disable is needed to protect the per-CPU buffer srh_state,
840 * which is also accessed by the bpf_lwt_seg6_* helpers
841 */
842 preempt_disable();
486cdf21 843 srh_state->srh = srh;
004d4b27 844 srh_state->hdrlen = srh->hdrlen << 3;
486cdf21 845 srh_state->valid = true;
004d4b27
MX
846
847 rcu_read_lock();
848 bpf_compute_data_pointers(skb);
849 ret = bpf_prog_run_save_cb(slwt->bpf.prog, skb);
850 rcu_read_unlock();
851
004d4b27
MX
852 switch (ret) {
853 case BPF_OK:
854 case BPF_REDIRECT:
855 break;
856 case BPF_DROP:
857 goto drop;
858 default:
859 pr_warn_once("bpf-seg6local: Illegal return value %u\n", ret);
860 goto drop;
861 }
862
486cdf21 863 if (srh_state->srh && !seg6_bpf_has_valid_srh(skb))
004d4b27
MX
864 goto drop;
865
486cdf21 866 preempt_enable();
004d4b27
MX
867 if (ret != BPF_REDIRECT)
868 seg6_lookup_nexthop(skb, NULL, 0);
869
870 return dst_input(skb);
871
872drop:
486cdf21 873 preempt_enable();
004d4b27
MX
874 kfree_skb(skb);
875 return -EINVAL;
876}
877
d1df6fd8
DL
878static struct seg6_action_desc seg6_action_table[] = {
879 {
880 .action = SEG6_LOCAL_ACTION_END,
881 .attrs = 0,
140f04c3
DL
882 .input = input_action_end,
883 },
884 {
885 .action = SEG6_LOCAL_ACTION_END_X,
886 .attrs = (1 << SEG6_LOCAL_NH6),
887 .input = input_action_end_x,
d1df6fd8 888 },
891ef8dd
DL
889 {
890 .action = SEG6_LOCAL_ACTION_END_T,
891 .attrs = (1 << SEG6_LOCAL_TABLE),
892 .input = input_action_end_t,
893 },
894 {
895 .action = SEG6_LOCAL_ACTION_END_DX2,
896 .attrs = (1 << SEG6_LOCAL_OIF),
897 .input = input_action_end_dx2,
898 },
140f04c3
DL
899 {
900 .action = SEG6_LOCAL_ACTION_END_DX6,
901 .attrs = (1 << SEG6_LOCAL_NH6),
902 .input = input_action_end_dx6,
903 },
891ef8dd
DL
904 {
905 .action = SEG6_LOCAL_ACTION_END_DX4,
906 .attrs = (1 << SEG6_LOCAL_NH4),
907 .input = input_action_end_dx4,
908 },
664d6f86
AM
909 {
910 .action = SEG6_LOCAL_ACTION_END_DT4,
911 .attrs = (1 << SEG6_LOCAL_VRFTABLE),
912#ifdef CONFIG_NET_L3_MASTER_DEV
913 .input = input_action_end_dt4,
914 .slwt_ops = {
915 .build_state = seg6_end_dt4_build,
916 },
917#endif
918 },
891ef8dd
DL
919 {
920 .action = SEG6_LOCAL_ACTION_END_DT6,
20a081b7
AM
921#ifdef CONFIG_NET_L3_MASTER_DEV
922 .attrs = 0,
923 .optattrs = (1 << SEG6_LOCAL_TABLE) |
924 (1 << SEG6_LOCAL_VRFTABLE),
925 .slwt_ops = {
926 .build_state = seg6_end_dt6_build,
927 },
928#else
891ef8dd 929 .attrs = (1 << SEG6_LOCAL_TABLE),
20a081b7 930#endif
891ef8dd
DL
931 .input = input_action_end_dt6,
932 },
140f04c3
DL
933 {
934 .action = SEG6_LOCAL_ACTION_END_B6,
935 .attrs = (1 << SEG6_LOCAL_SRH),
936 .input = input_action_end_b6,
937 },
938 {
939 .action = SEG6_LOCAL_ACTION_END_B6_ENCAP,
940 .attrs = (1 << SEG6_LOCAL_SRH),
941 .input = input_action_end_b6_encap,
942 .static_headroom = sizeof(struct ipv6hdr),
004d4b27
MX
943 },
944 {
945 .action = SEG6_LOCAL_ACTION_END_BPF,
946 .attrs = (1 << SEG6_LOCAL_BPF),
947 .input = input_action_end_bpf,
948 },
949
d1df6fd8
DL
950};
951
952static struct seg6_action_desc *__get_action_desc(int action)
953{
954 struct seg6_action_desc *desc;
955 int i, count;
956
709af180 957 count = ARRAY_SIZE(seg6_action_table);
d1df6fd8
DL
958 for (i = 0; i < count; i++) {
959 desc = &seg6_action_table[i];
960 if (desc->action == action)
961 return desc;
962 }
963
964 return NULL;
965}
966
967static int seg6_local_input(struct sk_buff *skb)
968{
969 struct dst_entry *orig_dst = skb_dst(skb);
970 struct seg6_action_desc *desc;
971 struct seg6_local_lwt *slwt;
972
6285217f
DL
973 if (skb->protocol != htons(ETH_P_IPV6)) {
974 kfree_skb(skb);
975 return -EINVAL;
976 }
977
d1df6fd8
DL
978 slwt = seg6_local_lwtunnel(orig_dst->lwtstate);
979 desc = slwt->desc;
980
981 return desc->input(skb, slwt);
982}
983
984static const struct nla_policy seg6_local_policy[SEG6_LOCAL_MAX + 1] = {
985 [SEG6_LOCAL_ACTION] = { .type = NLA_U32 },
986 [SEG6_LOCAL_SRH] = { .type = NLA_BINARY },
987 [SEG6_LOCAL_TABLE] = { .type = NLA_U32 },
664d6f86 988 [SEG6_LOCAL_VRFTABLE] = { .type = NLA_U32 },
d1df6fd8
DL
989 [SEG6_LOCAL_NH4] = { .type = NLA_BINARY,
990 .len = sizeof(struct in_addr) },
991 [SEG6_LOCAL_NH6] = { .type = NLA_BINARY,
992 .len = sizeof(struct in6_addr) },
993 [SEG6_LOCAL_IIF] = { .type = NLA_U32 },
994 [SEG6_LOCAL_OIF] = { .type = NLA_U32 },
004d4b27 995 [SEG6_LOCAL_BPF] = { .type = NLA_NESTED },
d1df6fd8
DL
996};
997
2d9cc60a
DL
998static int parse_nla_srh(struct nlattr **attrs, struct seg6_local_lwt *slwt)
999{
1000 struct ipv6_sr_hdr *srh;
1001 int len;
1002
1003 srh = nla_data(attrs[SEG6_LOCAL_SRH]);
1004 len = nla_len(attrs[SEG6_LOCAL_SRH]);
1005
1006 /* SRH must contain at least one segment */
1007 if (len < sizeof(*srh) + sizeof(struct in6_addr))
1008 return -EINVAL;
1009
bb986a50 1010 if (!seg6_validate_srh(srh, len, false))
2d9cc60a
DL
1011 return -EINVAL;
1012
7fa41efa 1013 slwt->srh = kmemdup(srh, len, GFP_KERNEL);
2d9cc60a
DL
1014 if (!slwt->srh)
1015 return -ENOMEM;
1016
2d9cc60a
DL
1017 slwt->headroom += len;
1018
1019 return 0;
1020}
1021
1022static int put_nla_srh(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1023{
1024 struct ipv6_sr_hdr *srh;
1025 struct nlattr *nla;
1026 int len;
1027
1028 srh = slwt->srh;
1029 len = (srh->hdrlen + 1) << 3;
1030
1031 nla = nla_reserve(skb, SEG6_LOCAL_SRH, len);
1032 if (!nla)
1033 return -EMSGSIZE;
1034
1035 memcpy(nla_data(nla), srh, len);
1036
1037 return 0;
1038}
1039
1040static int cmp_nla_srh(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1041{
1042 int len = (a->srh->hdrlen + 1) << 3;
1043
1044 if (len != ((b->srh->hdrlen + 1) << 3))
1045 return 1;
1046
1047 return memcmp(a->srh, b->srh, len);
1048}
1049
964adce5
AM
1050static void destroy_attr_srh(struct seg6_local_lwt *slwt)
1051{
1052 kfree(slwt->srh);
1053}
1054
2d9cc60a
DL
1055static int parse_nla_table(struct nlattr **attrs, struct seg6_local_lwt *slwt)
1056{
1057 slwt->table = nla_get_u32(attrs[SEG6_LOCAL_TABLE]);
1058
1059 return 0;
1060}
1061
1062static int put_nla_table(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1063{
1064 if (nla_put_u32(skb, SEG6_LOCAL_TABLE, slwt->table))
1065 return -EMSGSIZE;
1066
1067 return 0;
1068}
1069
1070static int cmp_nla_table(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1071{
1072 if (a->table != b->table)
1073 return 1;
1074
1075 return 0;
1076}
1077
664d6f86
AM
1078static struct
1079seg6_end_dt_info *seg6_possible_end_dt_info(struct seg6_local_lwt *slwt)
1080{
1081#ifdef CONFIG_NET_L3_MASTER_DEV
1082 return &slwt->dt_info;
1083#else
1084 return ERR_PTR(-EOPNOTSUPP);
1085#endif
1086}
1087
1088static int parse_nla_vrftable(struct nlattr **attrs,
1089 struct seg6_local_lwt *slwt)
1090{
1091 struct seg6_end_dt_info *info = seg6_possible_end_dt_info(slwt);
1092
1093 if (IS_ERR(info))
1094 return PTR_ERR(info);
1095
1096 info->vrf_table = nla_get_u32(attrs[SEG6_LOCAL_VRFTABLE]);
1097
1098 return 0;
1099}
1100
1101static int put_nla_vrftable(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1102{
1103 struct seg6_end_dt_info *info = seg6_possible_end_dt_info(slwt);
1104
1105 if (IS_ERR(info))
1106 return PTR_ERR(info);
1107
1108 if (nla_put_u32(skb, SEG6_LOCAL_VRFTABLE, info->vrf_table))
1109 return -EMSGSIZE;
1110
1111 return 0;
1112}
1113
1114static int cmp_nla_vrftable(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1115{
1116 struct seg6_end_dt_info *info_a = seg6_possible_end_dt_info(a);
1117 struct seg6_end_dt_info *info_b = seg6_possible_end_dt_info(b);
1118
1119 if (info_a->vrf_table != info_b->vrf_table)
1120 return 1;
1121
1122 return 0;
1123}
1124
2d9cc60a
DL
1125static int parse_nla_nh4(struct nlattr **attrs, struct seg6_local_lwt *slwt)
1126{
1127 memcpy(&slwt->nh4, nla_data(attrs[SEG6_LOCAL_NH4]),
1128 sizeof(struct in_addr));
1129
1130 return 0;
1131}
1132
1133static int put_nla_nh4(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1134{
1135 struct nlattr *nla;
1136
1137 nla = nla_reserve(skb, SEG6_LOCAL_NH4, sizeof(struct in_addr));
1138 if (!nla)
1139 return -EMSGSIZE;
1140
1141 memcpy(nla_data(nla), &slwt->nh4, sizeof(struct in_addr));
1142
1143 return 0;
1144}
1145
1146static int cmp_nla_nh4(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1147{
1148 return memcmp(&a->nh4, &b->nh4, sizeof(struct in_addr));
1149}
1150
1151static int parse_nla_nh6(struct nlattr **attrs, struct seg6_local_lwt *slwt)
1152{
1153 memcpy(&slwt->nh6, nla_data(attrs[SEG6_LOCAL_NH6]),
1154 sizeof(struct in6_addr));
1155
1156 return 0;
1157}
1158
1159static int put_nla_nh6(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1160{
1161 struct nlattr *nla;
1162
1163 nla = nla_reserve(skb, SEG6_LOCAL_NH6, sizeof(struct in6_addr));
1164 if (!nla)
1165 return -EMSGSIZE;
1166
1167 memcpy(nla_data(nla), &slwt->nh6, sizeof(struct in6_addr));
1168
1169 return 0;
1170}
1171
1172static int cmp_nla_nh6(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1173{
1174 return memcmp(&a->nh6, &b->nh6, sizeof(struct in6_addr));
1175}
1176
1177static int parse_nla_iif(struct nlattr **attrs, struct seg6_local_lwt *slwt)
1178{
1179 slwt->iif = nla_get_u32(attrs[SEG6_LOCAL_IIF]);
1180
1181 return 0;
1182}
1183
1184static int put_nla_iif(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1185{
1186 if (nla_put_u32(skb, SEG6_LOCAL_IIF, slwt->iif))
1187 return -EMSGSIZE;
1188
1189 return 0;
1190}
1191
1192static int cmp_nla_iif(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1193{
1194 if (a->iif != b->iif)
1195 return 1;
1196
1197 return 0;
1198}
1199
1200static int parse_nla_oif(struct nlattr **attrs, struct seg6_local_lwt *slwt)
1201{
1202 slwt->oif = nla_get_u32(attrs[SEG6_LOCAL_OIF]);
1203
1204 return 0;
1205}
1206
1207static int put_nla_oif(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1208{
1209 if (nla_put_u32(skb, SEG6_LOCAL_OIF, slwt->oif))
1210 return -EMSGSIZE;
1211
1212 return 0;
1213}
1214
1215static int cmp_nla_oif(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1216{
1217 if (a->oif != b->oif)
1218 return 1;
1219
1220 return 0;
1221}
1222
004d4b27
MX
1223#define MAX_PROG_NAME 256
1224static const struct nla_policy bpf_prog_policy[SEG6_LOCAL_BPF_PROG_MAX + 1] = {
1225 [SEG6_LOCAL_BPF_PROG] = { .type = NLA_U32, },
1226 [SEG6_LOCAL_BPF_PROG_NAME] = { .type = NLA_NUL_STRING,
1227 .len = MAX_PROG_NAME },
1228};
1229
1230static int parse_nla_bpf(struct nlattr **attrs, struct seg6_local_lwt *slwt)
1231{
1232 struct nlattr *tb[SEG6_LOCAL_BPF_PROG_MAX + 1];
1233 struct bpf_prog *p;
1234 int ret;
1235 u32 fd;
1236
8cb08174
JB
1237 ret = nla_parse_nested_deprecated(tb, SEG6_LOCAL_BPF_PROG_MAX,
1238 attrs[SEG6_LOCAL_BPF],
1239 bpf_prog_policy, NULL);
004d4b27
MX
1240 if (ret < 0)
1241 return ret;
1242
1243 if (!tb[SEG6_LOCAL_BPF_PROG] || !tb[SEG6_LOCAL_BPF_PROG_NAME])
1244 return -EINVAL;
1245
1246 slwt->bpf.name = nla_memdup(tb[SEG6_LOCAL_BPF_PROG_NAME], GFP_KERNEL);
1247 if (!slwt->bpf.name)
1248 return -ENOMEM;
1249
1250 fd = nla_get_u32(tb[SEG6_LOCAL_BPF_PROG]);
1251 p = bpf_prog_get_type(fd, BPF_PROG_TYPE_LWT_SEG6LOCAL);
1252 if (IS_ERR(p)) {
1253 kfree(slwt->bpf.name);
1254 return PTR_ERR(p);
1255 }
1256
1257 slwt->bpf.prog = p;
1258 return 0;
1259}
1260
1261static int put_nla_bpf(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1262{
1263 struct nlattr *nest;
1264
1265 if (!slwt->bpf.prog)
1266 return 0;
1267
ae0be8de 1268 nest = nla_nest_start_noflag(skb, SEG6_LOCAL_BPF);
004d4b27
MX
1269 if (!nest)
1270 return -EMSGSIZE;
1271
1272 if (nla_put_u32(skb, SEG6_LOCAL_BPF_PROG, slwt->bpf.prog->aux->id))
1273 return -EMSGSIZE;
1274
1275 if (slwt->bpf.name &&
1276 nla_put_string(skb, SEG6_LOCAL_BPF_PROG_NAME, slwt->bpf.name))
1277 return -EMSGSIZE;
1278
1279 return nla_nest_end(skb, nest);
1280}
1281
1282static int cmp_nla_bpf(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1283{
1284 if (!a->bpf.name && !b->bpf.name)
1285 return 0;
1286
1287 if (!a->bpf.name || !b->bpf.name)
1288 return 1;
1289
1290 return strcmp(a->bpf.name, b->bpf.name);
1291}
1292
964adce5
AM
1293static void destroy_attr_bpf(struct seg6_local_lwt *slwt)
1294{
1295 kfree(slwt->bpf.name);
1296 if (slwt->bpf.prog)
1297 bpf_prog_put(slwt->bpf.prog);
1298}
1299
d1df6fd8
DL
1300struct seg6_action_param {
1301 int (*parse)(struct nlattr **attrs, struct seg6_local_lwt *slwt);
1302 int (*put)(struct sk_buff *skb, struct seg6_local_lwt *slwt);
1303 int (*cmp)(struct seg6_local_lwt *a, struct seg6_local_lwt *b);
964adce5
AM
1304
1305 /* optional destroy() callback useful for releasing resources which
1306 * have been previously acquired in the corresponding parse()
1307 * function.
1308 */
1309 void (*destroy)(struct seg6_local_lwt *slwt);
d1df6fd8
DL
1310};
1311
1312static struct seg6_action_param seg6_action_params[SEG6_LOCAL_MAX + 1] = {
2d9cc60a
DL
1313 [SEG6_LOCAL_SRH] = { .parse = parse_nla_srh,
1314 .put = put_nla_srh,
964adce5
AM
1315 .cmp = cmp_nla_srh,
1316 .destroy = destroy_attr_srh },
d1df6fd8 1317
2d9cc60a
DL
1318 [SEG6_LOCAL_TABLE] = { .parse = parse_nla_table,
1319 .put = put_nla_table,
1320 .cmp = cmp_nla_table },
d1df6fd8 1321
2d9cc60a
DL
1322 [SEG6_LOCAL_NH4] = { .parse = parse_nla_nh4,
1323 .put = put_nla_nh4,
1324 .cmp = cmp_nla_nh4 },
d1df6fd8 1325
2d9cc60a
DL
1326 [SEG6_LOCAL_NH6] = { .parse = parse_nla_nh6,
1327 .put = put_nla_nh6,
1328 .cmp = cmp_nla_nh6 },
d1df6fd8 1329
2d9cc60a
DL
1330 [SEG6_LOCAL_IIF] = { .parse = parse_nla_iif,
1331 .put = put_nla_iif,
1332 .cmp = cmp_nla_iif },
d1df6fd8 1333
2d9cc60a
DL
1334 [SEG6_LOCAL_OIF] = { .parse = parse_nla_oif,
1335 .put = put_nla_oif,
1336 .cmp = cmp_nla_oif },
004d4b27
MX
1337
1338 [SEG6_LOCAL_BPF] = { .parse = parse_nla_bpf,
1339 .put = put_nla_bpf,
964adce5
AM
1340 .cmp = cmp_nla_bpf,
1341 .destroy = destroy_attr_bpf },
004d4b27 1342
664d6f86
AM
1343 [SEG6_LOCAL_VRFTABLE] = { .parse = parse_nla_vrftable,
1344 .put = put_nla_vrftable,
1345 .cmp = cmp_nla_vrftable },
1346
d1df6fd8
DL
1347};
1348
964adce5 1349/* call the destroy() callback (if available) for each set attribute in
0a3021f1
AM
1350 * @parsed_attrs, starting from the first attribute up to the @max_parsed
1351 * (excluded) attribute.
964adce5 1352 */
0a3021f1
AM
1353static void __destroy_attrs(unsigned long parsed_attrs, int max_parsed,
1354 struct seg6_local_lwt *slwt)
964adce5 1355{
964adce5
AM
1356 struct seg6_action_param *param;
1357 int i;
1358
1359 /* Every required seg6local attribute is identified by an ID which is
1360 * encoded as a flag (i.e: 1 << ID) in the 'attrs' bitmask;
1361 *
0a3021f1 1362 * We scan the 'parsed_attrs' bitmask, starting from the first attribute
964adce5
AM
1363 * up to the @max_parsed (excluded) attribute.
1364 * For each set attribute, we retrieve the corresponding destroy()
1365 * callback. If the callback is not available, then we skip to the next
1366 * attribute; otherwise, we call the destroy() callback.
1367 */
1368 for (i = 0; i < max_parsed; ++i) {
0a3021f1 1369 if (!(parsed_attrs & (1 << i)))
964adce5
AM
1370 continue;
1371
1372 param = &seg6_action_params[i];
1373
1374 if (param->destroy)
1375 param->destroy(slwt);
1376 }
1377}
1378
1379/* release all the resources that may have been acquired during parsing
1380 * operations.
1381 */
1382static void destroy_attrs(struct seg6_local_lwt *slwt)
1383{
0a3021f1
AM
1384 unsigned long attrs = slwt->desc->attrs | slwt->parsed_optattrs;
1385
1386 __destroy_attrs(attrs, SEG6_LOCAL_MAX + 1, slwt);
1387}
1388
1389static int parse_nla_optional_attrs(struct nlattr **attrs,
1390 struct seg6_local_lwt *slwt)
1391{
1392 struct seg6_action_desc *desc = slwt->desc;
1393 unsigned long parsed_optattrs = 0;
1394 struct seg6_action_param *param;
1395 int err, i;
1396
1397 for (i = 0; i < SEG6_LOCAL_MAX + 1; ++i) {
1398 if (!(desc->optattrs & (1 << i)) || !attrs[i])
1399 continue;
1400
1401 /* once here, the i-th attribute is provided by the
1402 * userspace AND it is identified optional as well.
1403 */
1404 param = &seg6_action_params[i];
1405
1406 err = param->parse(attrs, slwt);
1407 if (err < 0)
1408 goto parse_optattrs_err;
1409
1410 /* current attribute has been correctly parsed */
1411 parsed_optattrs |= (1 << i);
1412 }
1413
1414 /* store in the tunnel state all the optional attributed successfully
1415 * parsed.
1416 */
1417 slwt->parsed_optattrs = parsed_optattrs;
1418
1419 return 0;
1420
1421parse_optattrs_err:
1422 __destroy_attrs(parsed_optattrs, i, slwt);
1423
1424 return err;
964adce5
AM
1425}
1426
cfdf64a0
AM
1427/* call the custom constructor of the behavior during its initialization phase
1428 * and after that all its attributes have been parsed successfully.
1429 */
1430static int
1431seg6_local_lwtunnel_build_state(struct seg6_local_lwt *slwt, const void *cfg,
1432 struct netlink_ext_ack *extack)
1433{
1434 struct seg6_action_desc *desc = slwt->desc;
1435 struct seg6_local_lwtunnel_ops *ops;
1436
1437 ops = &desc->slwt_ops;
1438 if (!ops->build_state)
1439 return 0;
1440
1441 return ops->build_state(slwt, cfg, extack);
1442}
1443
1444/* call the custom destructor of the behavior which is invoked before the
1445 * tunnel is going to be destroyed.
1446 */
1447static void seg6_local_lwtunnel_destroy_state(struct seg6_local_lwt *slwt)
1448{
1449 struct seg6_action_desc *desc = slwt->desc;
1450 struct seg6_local_lwtunnel_ops *ops;
1451
1452 ops = &desc->slwt_ops;
1453 if (!ops->destroy_state)
1454 return;
1455
1456 ops->destroy_state(slwt);
1457}
1458
d1df6fd8
DL
1459static int parse_nla_action(struct nlattr **attrs, struct seg6_local_lwt *slwt)
1460{
1461 struct seg6_action_param *param;
1462 struct seg6_action_desc *desc;
0a3021f1 1463 unsigned long invalid_attrs;
d1df6fd8
DL
1464 int i, err;
1465
1466 desc = __get_action_desc(slwt->action);
1467 if (!desc)
1468 return -EINVAL;
1469
1470 if (!desc->input)
1471 return -EOPNOTSUPP;
1472
1473 slwt->desc = desc;
1474 slwt->headroom += desc->static_headroom;
1475
0a3021f1
AM
1476 /* Forcing the desc->optattrs *set* and the desc->attrs *set* to be
1477 * disjoined, this allow us to release acquired resources by optional
1478 * attributes and by required attributes independently from each other
1479 * without any interfarence.
1480 * In other terms, we are sure that we do not release some the acquired
1481 * resources twice.
1482 *
1483 * Note that if an attribute is configured both as required and as
1484 * optional, it means that the user has messed something up in the
1485 * seg6_action_table. Therefore, this check is required for SRv6
1486 * behaviors to work properly.
1487 */
1488 invalid_attrs = desc->attrs & desc->optattrs;
1489 if (invalid_attrs) {
1490 WARN_ONCE(1,
1491 "An attribute cannot be both required AND optional");
1492 return -EINVAL;
1493 }
1494
1495 /* parse the required attributes */
d1df6fd8
DL
1496 for (i = 0; i < SEG6_LOCAL_MAX + 1; i++) {
1497 if (desc->attrs & (1 << i)) {
1498 if (!attrs[i])
1499 return -EINVAL;
1500
1501 param = &seg6_action_params[i];
1502
1503 err = param->parse(attrs, slwt);
1504 if (err < 0)
0a3021f1 1505 goto parse_attrs_err;
d1df6fd8
DL
1506 }
1507 }
1508
0a3021f1
AM
1509 /* parse the optional attributes, if any */
1510 err = parse_nla_optional_attrs(attrs, slwt);
1511 if (err < 0)
1512 goto parse_attrs_err;
1513
d1df6fd8 1514 return 0;
964adce5 1515
0a3021f1 1516parse_attrs_err:
964adce5
AM
1517 /* release any resource that may have been acquired during the i-1
1518 * parse() operations.
1519 */
0a3021f1 1520 __destroy_attrs(desc->attrs, i, slwt);
964adce5
AM
1521
1522 return err;
d1df6fd8
DL
1523}
1524
faee6769
AA
1525static int seg6_local_build_state(struct net *net, struct nlattr *nla,
1526 unsigned int family, const void *cfg,
1527 struct lwtunnel_state **ts,
d1df6fd8
DL
1528 struct netlink_ext_ack *extack)
1529{
1530 struct nlattr *tb[SEG6_LOCAL_MAX + 1];
1531 struct lwtunnel_state *newts;
1532 struct seg6_local_lwt *slwt;
1533 int err;
1534
6285217f
DL
1535 if (family != AF_INET6)
1536 return -EINVAL;
1537
8cb08174
JB
1538 err = nla_parse_nested_deprecated(tb, SEG6_LOCAL_MAX, nla,
1539 seg6_local_policy, extack);
d1df6fd8
DL
1540
1541 if (err < 0)
1542 return err;
1543
1544 if (!tb[SEG6_LOCAL_ACTION])
1545 return -EINVAL;
1546
1547 newts = lwtunnel_state_alloc(sizeof(*slwt));
1548 if (!newts)
1549 return -ENOMEM;
1550
1551 slwt = seg6_local_lwtunnel(newts);
1552 slwt->action = nla_get_u32(tb[SEG6_LOCAL_ACTION]);
1553
1554 err = parse_nla_action(tb, slwt);
1555 if (err < 0)
1556 goto out_free;
1557
cfdf64a0
AM
1558 err = seg6_local_lwtunnel_build_state(slwt, cfg, extack);
1559 if (err < 0)
1560 goto out_destroy_attrs;
1561
d1df6fd8
DL
1562 newts->type = LWTUNNEL_ENCAP_SEG6_LOCAL;
1563 newts->flags = LWTUNNEL_STATE_INPUT_REDIRECT;
1564 newts->headroom = slwt->headroom;
1565
1566 *ts = newts;
1567
1568 return 0;
1569
cfdf64a0
AM
1570out_destroy_attrs:
1571 destroy_attrs(slwt);
d1df6fd8 1572out_free:
d1df6fd8
DL
1573 kfree(newts);
1574 return err;
1575}
1576
1577static void seg6_local_destroy_state(struct lwtunnel_state *lwt)
1578{
1579 struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
1580
cfdf64a0
AM
1581 seg6_local_lwtunnel_destroy_state(slwt);
1582
964adce5 1583 destroy_attrs(slwt);
004d4b27
MX
1584
1585 return;
d1df6fd8
DL
1586}
1587
1588static int seg6_local_fill_encap(struct sk_buff *skb,
1589 struct lwtunnel_state *lwt)
1590{
1591 struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
1592 struct seg6_action_param *param;
0a3021f1 1593 unsigned long attrs;
d1df6fd8
DL
1594 int i, err;
1595
1596 if (nla_put_u32(skb, SEG6_LOCAL_ACTION, slwt->action))
1597 return -EMSGSIZE;
1598
0a3021f1
AM
1599 attrs = slwt->desc->attrs | slwt->parsed_optattrs;
1600
d1df6fd8 1601 for (i = 0; i < SEG6_LOCAL_MAX + 1; i++) {
0a3021f1 1602 if (attrs & (1 << i)) {
d1df6fd8
DL
1603 param = &seg6_action_params[i];
1604 err = param->put(skb, slwt);
1605 if (err < 0)
1606 return err;
1607 }
1608 }
1609
1610 return 0;
1611}
1612
1613static int seg6_local_get_encap_size(struct lwtunnel_state *lwt)
1614{
1615 struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
1616 unsigned long attrs;
1617 int nlsize;
1618
1619 nlsize = nla_total_size(4); /* action */
1620
0a3021f1 1621 attrs = slwt->desc->attrs | slwt->parsed_optattrs;
d1df6fd8
DL
1622
1623 if (attrs & (1 << SEG6_LOCAL_SRH))
1624 nlsize += nla_total_size((slwt->srh->hdrlen + 1) << 3);
1625
1626 if (attrs & (1 << SEG6_LOCAL_TABLE))
1627 nlsize += nla_total_size(4);
1628
1629 if (attrs & (1 << SEG6_LOCAL_NH4))
1630 nlsize += nla_total_size(4);
1631
1632 if (attrs & (1 << SEG6_LOCAL_NH6))
1633 nlsize += nla_total_size(16);
1634
1635 if (attrs & (1 << SEG6_LOCAL_IIF))
1636 nlsize += nla_total_size(4);
1637
1638 if (attrs & (1 << SEG6_LOCAL_OIF))
1639 nlsize += nla_total_size(4);
1640
004d4b27
MX
1641 if (attrs & (1 << SEG6_LOCAL_BPF))
1642 nlsize += nla_total_size(sizeof(struct nlattr)) +
1643 nla_total_size(MAX_PROG_NAME) +
1644 nla_total_size(4);
1645
664d6f86
AM
1646 if (attrs & (1 << SEG6_LOCAL_VRFTABLE))
1647 nlsize += nla_total_size(4);
1648
d1df6fd8
DL
1649 return nlsize;
1650}
1651
1652static int seg6_local_cmp_encap(struct lwtunnel_state *a,
1653 struct lwtunnel_state *b)
1654{
1655 struct seg6_local_lwt *slwt_a, *slwt_b;
1656 struct seg6_action_param *param;
0a3021f1 1657 unsigned long attrs_a, attrs_b;
d1df6fd8
DL
1658 int i;
1659
1660 slwt_a = seg6_local_lwtunnel(a);
1661 slwt_b = seg6_local_lwtunnel(b);
1662
1663 if (slwt_a->action != slwt_b->action)
1664 return 1;
1665
0a3021f1
AM
1666 attrs_a = slwt_a->desc->attrs | slwt_a->parsed_optattrs;
1667 attrs_b = slwt_b->desc->attrs | slwt_b->parsed_optattrs;
1668
1669 if (attrs_a != attrs_b)
d1df6fd8
DL
1670 return 1;
1671
1672 for (i = 0; i < SEG6_LOCAL_MAX + 1; i++) {
0a3021f1 1673 if (attrs_a & (1 << i)) {
d1df6fd8
DL
1674 param = &seg6_action_params[i];
1675 if (param->cmp(slwt_a, slwt_b))
1676 return 1;
1677 }
1678 }
1679
1680 return 0;
1681}
1682
1683static const struct lwtunnel_encap_ops seg6_local_ops = {
1684 .build_state = seg6_local_build_state,
1685 .destroy_state = seg6_local_destroy_state,
1686 .input = seg6_local_input,
1687 .fill_encap = seg6_local_fill_encap,
1688 .get_encap_size = seg6_local_get_encap_size,
1689 .cmp_encap = seg6_local_cmp_encap,
1690 .owner = THIS_MODULE,
1691};
1692
1693int __init seg6_local_init(void)
1694{
1695 return lwtunnel_encap_add_ops(&seg6_local_ops,
1696 LWTUNNEL_ENCAP_SEG6_LOCAL);
1697}
1698
1699void seg6_local_exit(void)
1700{
1701 lwtunnel_encap_del_ops(&seg6_local_ops, LWTUNNEL_ENCAP_SEG6_LOCAL);
1702}