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