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/filter.h>
11 #include <linux/types.h>
12 #include <linux/skbuff.h>
13 #include <linux/net.h>
14 #include <linux/module.h>
15 #include <net/ip.h>
16 #include <net/lwtunnel.h>
17 #include <net/netevent.h>
18 #include <net/netns/generic.h>
19 #include <net/ip6_fib.h>
20 #include <net/route.h>
21 #include <net/seg6.h>
22 #include <linux/seg6.h>
23 #include <linux/seg6_local.h>
24 #include <net/addrconf.h>
25 #include <net/ip6_route.h>
26 #include <net/dst_cache.h>
27 #include <net/ip_tunnels.h>
28 #ifdef CONFIG_IPV6_SEG6_HMAC
29 #include <net/seg6_hmac.h>
30 #endif
31 #include <net/seg6_local.h>
32 #include <linux/etherdevice.h>
33 #include <linux/bpf.h>
34 #include <linux/netfilter.h>
35
36 #define SEG6_F_ATTR(i) BIT(i)
37
38 struct seg6_local_lwt;
39
40 /* callbacks used for customizing the creation and destruction of a behavior */
41 struct seg6_local_lwtunnel_ops {
42 int (*build_state)(struct seg6_local_lwt *slwt, const void *cfg,
43 struct netlink_ext_ack *extack);
44 void (*destroy_state)(struct seg6_local_lwt *slwt);
45 };
46
47 struct seg6_action_desc {
48 int action;
49 unsigned long attrs;
50
51 /* The optattrs field is used for specifying all the optional
52 * attributes supported by a specific behavior.
53 * It means that if one of these attributes is not provided in the
54 * netlink message during the behavior creation, no errors will be
55 * returned to the userspace.
56 *
57 * Each attribute can be only of two types (mutually exclusive):
58 * 1) required or 2) optional.
59 * Every user MUST obey to this rule! If you set an attribute as
60 * required the same attribute CANNOT be set as optional and vice
61 * versa.
62 */
63 unsigned long optattrs;
64
65 int (*input)(struct sk_buff *skb, struct seg6_local_lwt *slwt);
66 int static_headroom;
67
68 struct seg6_local_lwtunnel_ops slwt_ops;
69 };
70
71 struct bpf_lwt_prog {
72 struct bpf_prog *prog;
73 char *name;
74 };
75
76 /* default length values (expressed in bits) for both Locator-Block and
77 * Locator-Node Function.
78 *
79 * Both SEG6_LOCAL_LCBLOCK_DBITS and SEG6_LOCAL_LCNODE_FN_DBITS *must* be:
80 * i) greater than 0;
81 * ii) evenly divisible by 8. In other terms, the lengths of the
82 * Locator-Block and Locator-Node Function must be byte-aligned (we can
83 * relax this constraint in the future if really needed).
84 *
85 * Moreover, a third condition must hold:
86 * iii) SEG6_LOCAL_LCBLOCK_DBITS + SEG6_LOCAL_LCNODE_FN_DBITS <= 128.
87 *
88 * The correctness of SEG6_LOCAL_LCBLOCK_DBITS and SEG6_LOCAL_LCNODE_FN_DBITS
89 * values are checked during the kernel compilation. If the compilation stops,
90 * check the value of these parameters to see if they meet conditions (i), (ii)
91 * and (iii).
92 */
93 #define SEG6_LOCAL_LCBLOCK_DBITS 32
94 #define SEG6_LOCAL_LCNODE_FN_DBITS 16
95
96 /* The following next_csid_chk_{cntr,lcblock,lcblock_fn}_bits macros can be
97 * used directly to check whether the lengths (in bits) of Locator-Block and
98 * Locator-Node Function are valid according to (i), (ii), (iii).
99 */
100 #define next_csid_chk_cntr_bits(blen, flen) \
101 ((blen) + (flen) > 128)
102
103 #define next_csid_chk_lcblock_bits(blen) \
104 ({ \
105 typeof(blen) __tmp = blen; \
106 (!__tmp || __tmp > 120 || (__tmp & 0x07)); \
107 })
108
109 #define next_csid_chk_lcnode_fn_bits(flen) \
110 next_csid_chk_lcblock_bits(flen)
111
112 /* flag indicating that flavors are set up for a given End* behavior */
113 #define SEG6_F_LOCAL_FLAVORS SEG6_F_ATTR(SEG6_LOCAL_FLAVORS)
114
115 #define SEG6_F_LOCAL_FLV_OP(flvname) BIT(SEG6_LOCAL_FLV_OP_##flvname)
116 #define SEG6_F_LOCAL_FLV_NEXT_CSID SEG6_F_LOCAL_FLV_OP(NEXT_CSID)
117 #define SEG6_F_LOCAL_FLV_PSP SEG6_F_LOCAL_FLV_OP(PSP)
118
119 /* Supported RFC8986 Flavor operations are reported in this bitmask */
120 #define SEG6_LOCAL_FLV8986_SUPP_OPS SEG6_F_LOCAL_FLV_PSP
121
122 #define SEG6_LOCAL_END_FLV_SUPP_OPS (SEG6_F_LOCAL_FLV_NEXT_CSID | \
123 SEG6_LOCAL_FLV8986_SUPP_OPS)
124 #define SEG6_LOCAL_END_X_FLV_SUPP_OPS SEG6_F_LOCAL_FLV_NEXT_CSID
125
126 struct seg6_flavors_info {
127 /* Flavor operations */
128 __u32 flv_ops;
129
130 /* Locator-Block length, expressed in bits */
131 __u8 lcblock_bits;
132 /* Locator-Node Function length, expressed in bits*/
133 __u8 lcnode_func_bits;
134 };
135
136 enum seg6_end_dt_mode {
137 DT_INVALID_MODE = -EINVAL,
138 DT_LEGACY_MODE = 0,
139 DT_VRF_MODE = 1,
140 };
141
142 struct seg6_end_dt_info {
143 enum seg6_end_dt_mode mode;
144
145 struct net *net;
146 /* VRF device associated to the routing table used by the SRv6
147 * End.DT4/DT6 behavior for routing IPv4/IPv6 packets.
148 */
149 int vrf_ifindex;
150 int vrf_table;
151
152 /* tunneled packet family (IPv4 or IPv6).
153 * Protocol and header length are inferred from family.
154 */
155 u16 family;
156 };
157
158 struct pcpu_seg6_local_counters {
159 u64_stats_t packets;
160 u64_stats_t bytes;
161 u64_stats_t errors;
162
163 struct u64_stats_sync syncp;
164 };
165
166 /* This struct groups all the SRv6 Behavior counters supported so far.
167 *
168 * put_nla_counters() makes use of this data structure to collect all counter
169 * values after the per-CPU counter evaluation has been performed.
170 * Finally, each counter value (in seg6_local_counters) is stored in the
171 * corresponding netlink attribute and sent to user space.
172 *
173 * NB: we don't want to expose this structure to user space!
174 */
175 struct seg6_local_counters {
176 __u64 packets;
177 __u64 bytes;
178 __u64 errors;
179 };
180
181 #define seg6_local_alloc_pcpu_counters(__gfp) \
182 __netdev_alloc_pcpu_stats(struct pcpu_seg6_local_counters, \
183 ((__gfp) | __GFP_ZERO))
184
185 #define SEG6_F_LOCAL_COUNTERS SEG6_F_ATTR(SEG6_LOCAL_COUNTERS)
186
187 struct seg6_local_lwt {
188 int action;
189 struct ipv6_sr_hdr *srh;
190 int table;
191 struct in_addr nh4;
192 struct in6_addr nh6;
193 int iif;
194 int oif;
195 struct bpf_lwt_prog bpf;
196 #ifdef CONFIG_NET_L3_MASTER_DEV
197 struct seg6_end_dt_info dt_info;
198 #endif
199 struct seg6_flavors_info flv_info;
200
201 struct pcpu_seg6_local_counters __percpu *pcpu_counters;
202
203 int headroom;
204 struct seg6_action_desc *desc;
205 /* unlike the required attrs, we have to track the optional attributes
206 * that have been effectively parsed.
207 */
208 unsigned long parsed_optattrs;
209 };
210
seg6_local_lwtunnel(struct lwtunnel_state * lwt)211 static struct seg6_local_lwt *seg6_local_lwtunnel(struct lwtunnel_state *lwt)
212 {
213 return (struct seg6_local_lwt *)lwt->data;
214 }
215
get_and_validate_srh(struct sk_buff * skb)216 static struct ipv6_sr_hdr *get_and_validate_srh(struct sk_buff *skb)
217 {
218 struct ipv6_sr_hdr *srh;
219
220 srh = seg6_get_srh(skb, IP6_FH_F_SKIP_RH);
221 if (!srh)
222 return NULL;
223
224 #ifdef CONFIG_IPV6_SEG6_HMAC
225 if (!seg6_hmac_validate_skb(skb))
226 return NULL;
227 #endif
228
229 return srh;
230 }
231
decap_and_validate(struct sk_buff * skb,int proto)232 static bool decap_and_validate(struct sk_buff *skb, int proto)
233 {
234 struct ipv6_sr_hdr *srh;
235 unsigned int off = 0;
236
237 srh = seg6_get_srh(skb, 0);
238 if (srh && srh->segments_left > 0)
239 return false;
240
241 #ifdef CONFIG_IPV6_SEG6_HMAC
242 if (srh && !seg6_hmac_validate_skb(skb))
243 return false;
244 #endif
245
246 if (ipv6_find_hdr(skb, &off, proto, NULL, NULL) < 0)
247 return false;
248
249 if (!pskb_pull(skb, off))
250 return false;
251
252 skb_postpull_rcsum(skb, skb_network_header(skb), off);
253
254 skb_reset_network_header(skb);
255 skb_reset_transport_header(skb);
256 if (iptunnel_pull_offloads(skb))
257 return false;
258
259 if (proto == IPPROTO_IPIP) {
260 bool l3slave = ipv6_l3mdev_skb(IP6CB(skb)->flags);
261 int iif = IP6CB(skb)->iif;
262
263 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
264 IPCB(skb)->iif = iif;
265 if (l3slave)
266 IPCB(skb)->flags |= IPSKB_L3SLAVE;
267 } else if (proto == IPPROTO_IPV6) {
268 bool l3slave = ipv6_l3mdev_skb(IP6CB(skb)->flags);
269 int iif = IP6CB(skb)->iif;
270
271 memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
272 IP6CB(skb)->iif = iif;
273 IP6CB(skb)->nhoff = offsetof(struct ipv6hdr, nexthdr);
274 if (l3slave)
275 IP6CB(skb)->flags |= IP6SKB_L3SLAVE;
276 }
277
278 return true;
279 }
280
advance_nextseg(struct ipv6_sr_hdr * srh,struct in6_addr * daddr)281 static void advance_nextseg(struct ipv6_sr_hdr *srh, struct in6_addr *daddr)
282 {
283 struct in6_addr *addr;
284
285 srh->segments_left--;
286 addr = srh->segments + srh->segments_left;
287 *daddr = *addr;
288 }
289
290 static int
seg6_lookup_any_nexthop(struct sk_buff * skb,struct in6_addr * nhaddr,u32 tbl_id,bool local_delivery,int oif)291 seg6_lookup_any_nexthop(struct sk_buff *skb, struct in6_addr *nhaddr,
292 u32 tbl_id, bool local_delivery, int oif)
293 {
294 struct net *net = dev_net(skb->dev);
295 struct ipv6hdr *hdr = ipv6_hdr(skb);
296 int flags = RT6_LOOKUP_F_HAS_SADDR;
297 struct dst_entry *dst = NULL;
298 struct rt6_info *rt;
299 struct flowi6 fl6;
300 int dev_flags = 0;
301
302 memset(&fl6, 0, sizeof(fl6));
303 fl6.flowi6_iif = skb->dev->ifindex;
304 fl6.flowi6_oif = oif;
305 fl6.daddr = nhaddr ? *nhaddr : hdr->daddr;
306 fl6.saddr = hdr->saddr;
307 fl6.flowlabel = ip6_flowinfo(hdr);
308 fl6.flowi6_mark = skb->mark;
309 fl6.flowi6_proto = hdr->nexthdr;
310
311 if (nhaddr)
312 fl6.flowi6_flags = FLOWI_FLAG_KNOWN_NH;
313
314 if (!tbl_id && !oif) {
315 dst = ip6_route_input_lookup(net, skb->dev, &fl6, skb, flags);
316 } else if (tbl_id) {
317 struct fib6_table *table;
318
319 table = fib6_get_table(net, tbl_id);
320 if (!table)
321 goto out;
322
323 rt = ip6_pol_route(net, table, oif, &fl6, skb, flags);
324 dst = &rt->dst;
325 } else {
326 dst = ip6_route_output(net, NULL, &fl6);
327 }
328
329 /* we want to discard traffic destined for local packet processing,
330 * if @local_delivery is set to false.
331 */
332 if (!local_delivery)
333 dev_flags |= IFF_LOOPBACK;
334
335 if (dst && (dst_dev(dst)->flags & dev_flags) && !dst->error) {
336 dst_release(dst);
337 dst = NULL;
338 }
339
340 out:
341 if (!dst) {
342 rt = net->ipv6.ip6_blk_hole_entry;
343 dst = &rt->dst;
344 dst_hold(dst);
345 }
346
347 skb_dst_drop(skb);
348 skb_dst_set(skb, dst);
349 return dst->error;
350 }
351
seg6_lookup_nexthop(struct sk_buff * skb,struct in6_addr * nhaddr,u32 tbl_id)352 int seg6_lookup_nexthop(struct sk_buff *skb,
353 struct in6_addr *nhaddr, u32 tbl_id)
354 {
355 return seg6_lookup_any_nexthop(skb, nhaddr, tbl_id, false, 0);
356 }
357
seg6_flv_lcblock_octects(const struct seg6_flavors_info * finfo)358 static __u8 seg6_flv_lcblock_octects(const struct seg6_flavors_info *finfo)
359 {
360 return finfo->lcblock_bits >> 3;
361 }
362
seg6_flv_lcnode_func_octects(const struct seg6_flavors_info * finfo)363 static __u8 seg6_flv_lcnode_func_octects(const struct seg6_flavors_info *finfo)
364 {
365 return finfo->lcnode_func_bits >> 3;
366 }
367
seg6_next_csid_is_arg_zero(const struct in6_addr * addr,const struct seg6_flavors_info * finfo)368 static bool seg6_next_csid_is_arg_zero(const struct in6_addr *addr,
369 const struct seg6_flavors_info *finfo)
370 {
371 __u8 fnc_octects = seg6_flv_lcnode_func_octects(finfo);
372 __u8 blk_octects = seg6_flv_lcblock_octects(finfo);
373 __u8 arg_octects;
374 int i;
375
376 arg_octects = 16 - blk_octects - fnc_octects;
377 for (i = 0; i < arg_octects; ++i) {
378 if (addr->s6_addr[blk_octects + fnc_octects + i] != 0x00)
379 return false;
380 }
381
382 return true;
383 }
384
385 /* assume that DA.Argument length > 0 */
seg6_next_csid_advance_arg(struct in6_addr * addr,const struct seg6_flavors_info * finfo)386 static void seg6_next_csid_advance_arg(struct in6_addr *addr,
387 const struct seg6_flavors_info *finfo)
388 {
389 __u8 fnc_octects = seg6_flv_lcnode_func_octects(finfo);
390 __u8 blk_octects = seg6_flv_lcblock_octects(finfo);
391
392 /* advance DA.Argument */
393 memmove(&addr->s6_addr[blk_octects],
394 &addr->s6_addr[blk_octects + fnc_octects],
395 16 - blk_octects - fnc_octects);
396
397 memset(&addr->s6_addr[16 - fnc_octects], 0x00, fnc_octects);
398 }
399
input_action_end_finish(struct sk_buff * skb,struct seg6_local_lwt * slwt)400 static int input_action_end_finish(struct sk_buff *skb,
401 struct seg6_local_lwt *slwt)
402 {
403 seg6_lookup_nexthop(skb, NULL, 0);
404
405 return dst_input(skb);
406 }
407
input_action_end_core(struct sk_buff * skb,struct seg6_local_lwt * slwt)408 static int input_action_end_core(struct sk_buff *skb,
409 struct seg6_local_lwt *slwt)
410 {
411 struct ipv6_sr_hdr *srh;
412
413 srh = get_and_validate_srh(skb);
414 if (!srh)
415 goto drop;
416
417 advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
418
419 return input_action_end_finish(skb, slwt);
420
421 drop:
422 kfree_skb(skb);
423 return -EINVAL;
424 }
425
end_next_csid_core(struct sk_buff * skb,struct seg6_local_lwt * slwt)426 static int end_next_csid_core(struct sk_buff *skb, struct seg6_local_lwt *slwt)
427 {
428 const struct seg6_flavors_info *finfo = &slwt->flv_info;
429 struct in6_addr *daddr = &ipv6_hdr(skb)->daddr;
430
431 if (seg6_next_csid_is_arg_zero(daddr, finfo))
432 return input_action_end_core(skb, slwt);
433
434 /* update DA */
435 seg6_next_csid_advance_arg(daddr, finfo);
436
437 return input_action_end_finish(skb, slwt);
438 }
439
input_action_end_x_finish(struct sk_buff * skb,struct seg6_local_lwt * slwt)440 static int input_action_end_x_finish(struct sk_buff *skb,
441 struct seg6_local_lwt *slwt)
442 {
443 seg6_lookup_any_nexthop(skb, &slwt->nh6, 0, false, slwt->oif);
444
445 return dst_input(skb);
446 }
447
input_action_end_x_core(struct sk_buff * skb,struct seg6_local_lwt * slwt)448 static int input_action_end_x_core(struct sk_buff *skb,
449 struct seg6_local_lwt *slwt)
450 {
451 struct ipv6_sr_hdr *srh;
452
453 srh = get_and_validate_srh(skb);
454 if (!srh)
455 goto drop;
456
457 advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
458
459 return input_action_end_x_finish(skb, slwt);
460
461 drop:
462 kfree_skb(skb);
463 return -EINVAL;
464 }
465
end_x_next_csid_core(struct sk_buff * skb,struct seg6_local_lwt * slwt)466 static int end_x_next_csid_core(struct sk_buff *skb,
467 struct seg6_local_lwt *slwt)
468 {
469 const struct seg6_flavors_info *finfo = &slwt->flv_info;
470 struct in6_addr *daddr = &ipv6_hdr(skb)->daddr;
471
472 if (seg6_next_csid_is_arg_zero(daddr, finfo))
473 return input_action_end_x_core(skb, slwt);
474
475 /* update DA */
476 seg6_next_csid_advance_arg(daddr, finfo);
477
478 return input_action_end_x_finish(skb, slwt);
479 }
480
seg6_next_csid_enabled(__u32 fops)481 static bool seg6_next_csid_enabled(__u32 fops)
482 {
483 return fops & SEG6_F_LOCAL_FLV_NEXT_CSID;
484 }
485
486 /* Processing of SRv6 End, End.X, and End.T behaviors can be extended through
487 * the flavors framework. These behaviors must report the subset of (flavor)
488 * operations they currently implement. In this way, if a user specifies a
489 * flavor combination that is not supported by a given End* behavior, the
490 * kernel refuses to instantiate the tunnel reporting the error.
491 */
seg6_flv_supp_ops_by_action(int action,__u32 * fops)492 static int seg6_flv_supp_ops_by_action(int action, __u32 *fops)
493 {
494 switch (action) {
495 case SEG6_LOCAL_ACTION_END:
496 *fops = SEG6_LOCAL_END_FLV_SUPP_OPS;
497 break;
498 case SEG6_LOCAL_ACTION_END_X:
499 *fops = SEG6_LOCAL_END_X_FLV_SUPP_OPS;
500 break;
501 default:
502 return -EOPNOTSUPP;
503 }
504
505 return 0;
506 }
507
508 /* We describe the packet state in relation to the absence/presence of the SRH
509 * and the Segment Left (SL) field.
510 * For our purposes, it is not necessary to record the exact value of the SL
511 * when the SID List consists of two or more segments.
512 */
513 enum seg6_local_pktinfo {
514 /* the order really matters! */
515 SEG6_LOCAL_PKTINFO_NOHDR = 0,
516 SEG6_LOCAL_PKTINFO_SL_ZERO,
517 SEG6_LOCAL_PKTINFO_SL_ONE,
518 SEG6_LOCAL_PKTINFO_SL_MORE,
519 __SEG6_LOCAL_PKTINFO_MAX,
520 };
521
522 #define SEG6_LOCAL_PKTINFO_MAX (__SEG6_LOCAL_PKTINFO_MAX - 1)
523
seg6_get_srh_pktinfo(struct ipv6_sr_hdr * srh)524 static enum seg6_local_pktinfo seg6_get_srh_pktinfo(struct ipv6_sr_hdr *srh)
525 {
526 __u8 sgl;
527
528 if (!srh)
529 return SEG6_LOCAL_PKTINFO_NOHDR;
530
531 sgl = srh->segments_left;
532 if (sgl < 2)
533 return SEG6_LOCAL_PKTINFO_SL_ZERO + sgl;
534
535 return SEG6_LOCAL_PKTINFO_SL_MORE;
536 }
537
538 enum seg6_local_flv_action {
539 SEG6_LOCAL_FLV_ACT_UNSPEC = 0,
540 SEG6_LOCAL_FLV_ACT_END,
541 SEG6_LOCAL_FLV_ACT_PSP,
542 SEG6_LOCAL_FLV_ACT_USP,
543 SEG6_LOCAL_FLV_ACT_USD,
544 __SEG6_LOCAL_FLV_ACT_MAX
545 };
546
547 #define SEG6_LOCAL_FLV_ACT_MAX (__SEG6_LOCAL_FLV_ACT_MAX - 1)
548
549 /* The action table for RFC8986 flavors (see the flv8986_act_tbl below)
550 * contains the actions (i.e. processing operations) to be applied on packets
551 * when flavors are configured for an End* behavior.
552 * By combining the pkinfo data and from the flavors mask, the macro
553 * computes the index used to access the elements (actions) stored in the
554 * action table. The index is structured as follows:
555 *
556 * index
557 * _______________/\________________
558 * / \
559 * +----------------+----------------+
560 * | pf | afm |
561 * +----------------+----------------+
562 * ph-1 ... p1 p0 fk-1 ... f1 f0
563 * MSB LSB
564 *
565 * where:
566 * - 'afm' (adjusted flavor mask) is the mask containing a combination of the
567 * RFC8986 flavors currently supported. 'afm' corresponds to the @fm
568 * argument of the macro whose value is righ-shifted by 1 bit. By doing so,
569 * we discard the SEG6_LOCAL_FLV_OP_UNSPEC flag (bit 0 in @fm) which is
570 * never used here;
571 * - 'pf' encodes the packet info (pktinfo) regarding the presence/absence of
572 * the SRH, SL = 0, etc. 'pf' is set with the value of @pf provided as
573 * argument to the macro.
574 */
575 #define flv8986_act_tbl_idx(pf, fm) \
576 ((((pf) << bits_per(SEG6_LOCAL_FLV8986_SUPP_OPS)) | \
577 ((fm) & SEG6_LOCAL_FLV8986_SUPP_OPS)) >> SEG6_LOCAL_FLV_OP_PSP)
578
579 /* We compute the size of the action table by considering the RFC8986 flavors
580 * actually supported by the kernel. In this way, the size is automatically
581 * adjusted when new flavors are supported.
582 */
583 #define FLV8986_ACT_TBL_SIZE \
584 roundup_pow_of_two(flv8986_act_tbl_idx(SEG6_LOCAL_PKTINFO_MAX, \
585 SEG6_LOCAL_FLV8986_SUPP_OPS))
586
587 /* tbl_cfg(act, pf, fm) macro is used to easily configure the action
588 * table; it accepts 3 arguments:
589 * i) @act, the suffix from SEG6_LOCAL_FLV_ACT_{act} representing
590 * the action that should be applied on the packet;
591 * ii) @pf, the suffix from SEG6_LOCAL_PKTINFO_{pf} reporting the packet
592 * info about the lack/presence of SRH, SRH with SL = 0, etc;
593 * iii) @fm, the mask of flavors.
594 */
595 #define tbl_cfg(act, pf, fm) \
596 [flv8986_act_tbl_idx(SEG6_LOCAL_PKTINFO_##pf, \
597 (fm))] = SEG6_LOCAL_FLV_ACT_##act
598
599 /* shorthand for improving readability */
600 #define F_PSP SEG6_F_LOCAL_FLV_PSP
601
602 /* The table contains, for each combination of the pktinfo data and
603 * flavors, the action that should be taken on a packet (e.g.
604 * "standard" Endpoint processing, Penultimate Segment Pop, etc).
605 *
606 * By default, table entries not explicitly configured are initialized with the
607 * SEG6_LOCAL_FLV_ACT_UNSPEC action, which generally has the effect of
608 * discarding the processed packet.
609 */
610 static const u8 flv8986_act_tbl[FLV8986_ACT_TBL_SIZE] = {
611 /* PSP variant for packet where SRH with SL = 1 */
612 tbl_cfg(PSP, SL_ONE, F_PSP),
613 /* End for packet where the SRH with SL > 1*/
614 tbl_cfg(END, SL_MORE, F_PSP),
615 };
616
617 #undef F_PSP
618 #undef tbl_cfg
619
620 /* For each flavor defined in RFC8986 (or a combination of them) an action is
621 * performed on the packet. The specific action depends on:
622 * - info extracted from the packet (i.e. pktinfo data) regarding the
623 * lack/presence of the SRH, and if the SRH is available, on the value of
624 * Segment Left field;
625 * - the mask of flavors configured for the specific SRv6 End* behavior.
626 *
627 * The function combines both the pkinfo and the flavors mask to evaluate the
628 * corresponding action to be taken on the packet.
629 */
630 static enum seg6_local_flv_action
seg6_local_flv8986_act_lookup(enum seg6_local_pktinfo pinfo,__u32 flvmask)631 seg6_local_flv8986_act_lookup(enum seg6_local_pktinfo pinfo, __u32 flvmask)
632 {
633 unsigned long index;
634
635 /* check if the provided mask of flavors is supported */
636 if (unlikely(flvmask & ~SEG6_LOCAL_FLV8986_SUPP_OPS))
637 return SEG6_LOCAL_FLV_ACT_UNSPEC;
638
639 index = flv8986_act_tbl_idx(pinfo, flvmask);
640 if (unlikely(index >= FLV8986_ACT_TBL_SIZE))
641 return SEG6_LOCAL_FLV_ACT_UNSPEC;
642
643 return flv8986_act_tbl[index];
644 }
645
646 /* skb->data must be aligned with skb->network_header */
seg6_pop_srh(struct sk_buff * skb,int srhoff)647 static bool seg6_pop_srh(struct sk_buff *skb, int srhoff)
648 {
649 struct ipv6_sr_hdr *srh;
650 struct ipv6hdr *iph;
651 __u8 srh_nexthdr;
652 int thoff = -1;
653 int srhlen;
654 int nhlen;
655
656 if (unlikely(srhoff < sizeof(*iph) ||
657 !pskb_may_pull(skb, srhoff + sizeof(*srh))))
658 return false;
659
660 srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
661 srhlen = ipv6_optlen(srh);
662
663 /* we are about to mangle the pkt, let's check if we can write on it */
664 if (unlikely(skb_ensure_writable(skb, srhoff + srhlen)))
665 return false;
666
667 /* skb_ensure_writable() may change skb pointers; evaluate srh again */
668 srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
669 srh_nexthdr = srh->nexthdr;
670
671 if (unlikely(!skb_transport_header_was_set(skb)))
672 goto pull;
673
674 nhlen = skb_network_header_len(skb);
675 /* we have to deal with the transport header: it could be set before
676 * the SRH, after the SRH, or within it (which is considered wrong,
677 * however).
678 */
679 if (likely(nhlen <= srhoff))
680 thoff = nhlen;
681 else if (nhlen >= srhoff + srhlen)
682 /* transport_header is set after the SRH */
683 thoff = nhlen - srhlen;
684 else
685 /* transport_header falls inside the SRH; hence, we can't
686 * restore the transport_header pointer properly after
687 * SRH removing operation.
688 */
689 return false;
690 pull:
691 /* we need to pop the SRH:
692 * 1) first of all, we pull out everything from IPv6 header up to SRH
693 * (included) evaluating also the rcsum;
694 * 2) we overwrite (and then remove) the SRH by properly moving the
695 * IPv6 along with any extension header that precedes the SRH;
696 * 3) At the end, we push back the pulled headers (except for SRH,
697 * obviously).
698 */
699 skb_pull_rcsum(skb, srhoff + srhlen);
700 memmove(skb_network_header(skb) + srhlen, skb_network_header(skb),
701 srhoff);
702 skb_push(skb, srhoff);
703
704 skb_reset_network_header(skb);
705 skb_mac_header_rebuild(skb);
706 if (likely(thoff >= 0))
707 skb_set_transport_header(skb, thoff);
708
709 iph = ipv6_hdr(skb);
710 if (iph->nexthdr == NEXTHDR_ROUTING) {
711 iph->nexthdr = srh_nexthdr;
712 } else {
713 /* we must look for the extension header (EXTH, for short) that
714 * immediately precedes the SRH we have just removed.
715 * Then, we update the value of the EXTH nexthdr with the one
716 * contained in the SRH nexthdr.
717 */
718 unsigned int off = sizeof(*iph);
719 struct ipv6_opt_hdr *hp, _hdr;
720 __u8 nexthdr = iph->nexthdr;
721
722 for (;;) {
723 if (unlikely(!ipv6_ext_hdr(nexthdr) ||
724 nexthdr == NEXTHDR_NONE))
725 return false;
726
727 hp = skb_header_pointer(skb, off, sizeof(_hdr), &_hdr);
728 if (unlikely(!hp))
729 return false;
730
731 if (hp->nexthdr == NEXTHDR_ROUTING) {
732 hp->nexthdr = srh_nexthdr;
733 break;
734 }
735
736 switch (nexthdr) {
737 case NEXTHDR_FRAGMENT:
738 fallthrough;
739 case NEXTHDR_AUTH:
740 /* we expect SRH before FRAG and AUTH */
741 return false;
742 default:
743 off += ipv6_optlen(hp);
744 break;
745 }
746
747 nexthdr = hp->nexthdr;
748 }
749 }
750
751 iph->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
752
753 skb_postpush_rcsum(skb, iph, srhoff);
754
755 return true;
756 }
757
758 /* process the packet on the basis of the RFC8986 flavors set for the given
759 * SRv6 End behavior instance.
760 */
end_flv8986_core(struct sk_buff * skb,struct seg6_local_lwt * slwt)761 static int end_flv8986_core(struct sk_buff *skb, struct seg6_local_lwt *slwt)
762 {
763 const struct seg6_flavors_info *finfo = &slwt->flv_info;
764 enum seg6_local_flv_action action;
765 enum seg6_local_pktinfo pinfo;
766 struct ipv6_sr_hdr *srh;
767 __u32 flvmask;
768 int srhoff;
769
770 srh = seg6_get_srh(skb, 0);
771 srhoff = srh ? ((unsigned char *)srh - skb->data) : 0;
772 pinfo = seg6_get_srh_pktinfo(srh);
773 #ifdef CONFIG_IPV6_SEG6_HMAC
774 if (srh && !seg6_hmac_validate_skb(skb))
775 goto drop;
776 #endif
777 flvmask = finfo->flv_ops;
778 if (unlikely(flvmask & ~SEG6_LOCAL_FLV8986_SUPP_OPS)) {
779 pr_warn_once("seg6local: invalid RFC8986 flavors\n");
780 goto drop;
781 }
782
783 /* retrieve the action triggered by the combination of pktinfo data and
784 * the flavors mask.
785 */
786 action = seg6_local_flv8986_act_lookup(pinfo, flvmask);
787 switch (action) {
788 case SEG6_LOCAL_FLV_ACT_END:
789 /* process the packet as the "standard" End behavior */
790 advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
791 break;
792 case SEG6_LOCAL_FLV_ACT_PSP:
793 advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
794
795 if (unlikely(!seg6_pop_srh(skb, srhoff)))
796 goto drop;
797 break;
798 case SEG6_LOCAL_FLV_ACT_UNSPEC:
799 fallthrough;
800 default:
801 /* by default, we drop the packet since we could not find a
802 * suitable action.
803 */
804 goto drop;
805 }
806
807 return input_action_end_finish(skb, slwt);
808
809 drop:
810 kfree_skb(skb);
811 return -EINVAL;
812 }
813
814 /* regular endpoint function */
input_action_end(struct sk_buff * skb,struct seg6_local_lwt * slwt)815 static int input_action_end(struct sk_buff *skb, struct seg6_local_lwt *slwt)
816 {
817 const struct seg6_flavors_info *finfo = &slwt->flv_info;
818 __u32 fops = finfo->flv_ops;
819
820 if (!fops)
821 return input_action_end_core(skb, slwt);
822
823 /* check for the presence of NEXT-C-SID since it applies first */
824 if (seg6_next_csid_enabled(fops))
825 return end_next_csid_core(skb, slwt);
826
827 /* the specific processing function to be performed on the packet
828 * depends on the combination of flavors defined in RFC8986 and some
829 * information extracted from the packet, e.g. presence/absence of SRH,
830 * Segment Left = 0, etc.
831 */
832 return end_flv8986_core(skb, slwt);
833 }
834
835 /* regular endpoint, and forward to specified nexthop */
input_action_end_x(struct sk_buff * skb,struct seg6_local_lwt * slwt)836 static int input_action_end_x(struct sk_buff *skb, struct seg6_local_lwt *slwt)
837 {
838 const struct seg6_flavors_info *finfo = &slwt->flv_info;
839 __u32 fops = finfo->flv_ops;
840
841 /* check for the presence of NEXT-C-SID since it applies first */
842 if (seg6_next_csid_enabled(fops))
843 return end_x_next_csid_core(skb, slwt);
844
845 return input_action_end_x_core(skb, slwt);
846 }
847
input_action_end_t(struct sk_buff * skb,struct seg6_local_lwt * slwt)848 static int input_action_end_t(struct sk_buff *skb, struct seg6_local_lwt *slwt)
849 {
850 struct ipv6_sr_hdr *srh;
851
852 srh = get_and_validate_srh(skb);
853 if (!srh)
854 goto drop;
855
856 advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
857
858 seg6_lookup_nexthop(skb, NULL, slwt->table);
859
860 return dst_input(skb);
861
862 drop:
863 kfree_skb(skb);
864 return -EINVAL;
865 }
866
867 /* decapsulate and forward inner L2 frame on specified interface */
input_action_end_dx2(struct sk_buff * skb,struct seg6_local_lwt * slwt)868 static int input_action_end_dx2(struct sk_buff *skb,
869 struct seg6_local_lwt *slwt)
870 {
871 struct net *net = dev_net(skb->dev);
872 struct net_device *odev;
873 struct ethhdr *eth;
874
875 if (!decap_and_validate(skb, IPPROTO_ETHERNET))
876 goto drop;
877
878 if (!pskb_may_pull(skb, ETH_HLEN))
879 goto drop;
880
881 skb_reset_mac_header(skb);
882 eth = (struct ethhdr *)skb->data;
883
884 /* To determine the frame's protocol, we assume it is 802.3. This avoids
885 * a call to eth_type_trans(), which is not really relevant for our
886 * use case.
887 */
888 if (!eth_proto_is_802_3(eth->h_proto))
889 goto drop;
890
891 odev = dev_get_by_index_rcu(net, slwt->oif);
892 if (!odev)
893 goto drop;
894
895 /* As we accept Ethernet frames, make sure the egress device is of
896 * the correct type.
897 */
898 if (odev->type != ARPHRD_ETHER)
899 goto drop;
900
901 if (!(odev->flags & IFF_UP) || !netif_carrier_ok(odev))
902 goto drop;
903
904 skb_orphan(skb);
905
906 if (skb_warn_if_lro(skb))
907 goto drop;
908
909 skb_forward_csum(skb);
910
911 if (skb->len - ETH_HLEN > odev->mtu)
912 goto drop;
913
914 skb->dev = odev;
915 skb->protocol = eth->h_proto;
916
917 return dev_queue_xmit(skb);
918
919 drop:
920 kfree_skb(skb);
921 return -EINVAL;
922 }
923
input_action_end_dx6_finish(struct net * net,struct sock * sk,struct sk_buff * skb)924 static int input_action_end_dx6_finish(struct net *net, struct sock *sk,
925 struct sk_buff *skb)
926 {
927 struct dst_entry *orig_dst = skb_dst(skb);
928 struct in6_addr *nhaddr = NULL;
929 struct seg6_local_lwt *slwt;
930
931 slwt = seg6_local_lwtunnel(orig_dst->lwtstate);
932
933 /* The inner packet is not associated to any local interface,
934 * so we do not call netif_rx().
935 *
936 * If slwt->nh6 is set to ::, then lookup the nexthop for the
937 * inner packet's DA. Otherwise, use the specified nexthop.
938 */
939 if (!ipv6_addr_any(&slwt->nh6))
940 nhaddr = &slwt->nh6;
941
942 seg6_lookup_nexthop(skb, nhaddr, 0);
943
944 return dst_input(skb);
945 }
946
947 /* decapsulate and forward to specified nexthop */
input_action_end_dx6(struct sk_buff * skb,struct seg6_local_lwt * slwt)948 static int input_action_end_dx6(struct sk_buff *skb,
949 struct seg6_local_lwt *slwt)
950 {
951 /* this function accepts IPv6 encapsulated packets, with either
952 * an SRH with SL=0, or no SRH.
953 */
954
955 if (!decap_and_validate(skb, IPPROTO_IPV6))
956 goto drop;
957
958 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
959 goto drop;
960
961 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
962 nf_reset_ct(skb);
963
964 if (static_branch_unlikely(&nf_hooks_lwtunnel_enabled))
965 return NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING,
966 dev_net(skb->dev), NULL, skb, skb->dev,
967 NULL, input_action_end_dx6_finish);
968
969 return input_action_end_dx6_finish(dev_net(skb->dev), NULL, skb);
970 drop:
971 kfree_skb(skb);
972 return -EINVAL;
973 }
974
input_action_end_dx4_finish(struct net * net,struct sock * sk,struct sk_buff * skb)975 static int input_action_end_dx4_finish(struct net *net, struct sock *sk,
976 struct sk_buff *skb)
977 {
978 struct dst_entry *orig_dst = skb_dst(skb);
979 enum skb_drop_reason reason;
980 struct seg6_local_lwt *slwt;
981 struct iphdr *iph;
982 __be32 nhaddr;
983
984 slwt = seg6_local_lwtunnel(orig_dst->lwtstate);
985
986 iph = ip_hdr(skb);
987
988 nhaddr = slwt->nh4.s_addr ?: iph->daddr;
989
990 skb_dst_drop(skb);
991
992 reason = ip_route_input(skb, nhaddr, iph->saddr, 0, skb->dev);
993 if (reason) {
994 kfree_skb_reason(skb, reason);
995 return -EINVAL;
996 }
997
998 return dst_input(skb);
999 }
1000
input_action_end_dx4(struct sk_buff * skb,struct seg6_local_lwt * slwt)1001 static int input_action_end_dx4(struct sk_buff *skb,
1002 struct seg6_local_lwt *slwt)
1003 {
1004 if (!decap_and_validate(skb, IPPROTO_IPIP))
1005 goto drop;
1006
1007 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1008 goto drop;
1009
1010 skb->protocol = htons(ETH_P_IP);
1011 skb_set_transport_header(skb, sizeof(struct iphdr));
1012 nf_reset_ct(skb);
1013
1014 if (static_branch_unlikely(&nf_hooks_lwtunnel_enabled))
1015 return NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING,
1016 dev_net(skb->dev), NULL, skb, skb->dev,
1017 NULL, input_action_end_dx4_finish);
1018
1019 return input_action_end_dx4_finish(dev_net(skb->dev), NULL, skb);
1020 drop:
1021 kfree_skb(skb);
1022 return -EINVAL;
1023 }
1024
1025 #ifdef CONFIG_NET_L3_MASTER_DEV
fib6_config_get_net(const struct fib6_config * fib6_cfg)1026 static struct net *fib6_config_get_net(const struct fib6_config *fib6_cfg)
1027 {
1028 const struct nl_info *nli = &fib6_cfg->fc_nlinfo;
1029
1030 return nli->nl_net;
1031 }
1032
__seg6_end_dt_vrf_build(struct seg6_local_lwt * slwt,const void * cfg,u16 family,struct netlink_ext_ack * extack)1033 static int __seg6_end_dt_vrf_build(struct seg6_local_lwt *slwt, const void *cfg,
1034 u16 family, struct netlink_ext_ack *extack)
1035 {
1036 struct seg6_end_dt_info *info = &slwt->dt_info;
1037 int vrf_ifindex;
1038 struct net *net;
1039
1040 net = fib6_config_get_net(cfg);
1041
1042 /* note that vrf_table was already set by parse_nla_vrftable() */
1043 vrf_ifindex = l3mdev_ifindex_lookup_by_table_id(L3MDEV_TYPE_VRF, net,
1044 info->vrf_table);
1045 if (vrf_ifindex < 0) {
1046 if (vrf_ifindex == -EPERM) {
1047 NL_SET_ERR_MSG(extack,
1048 "Strict mode for VRF is disabled");
1049 } else if (vrf_ifindex == -ENODEV) {
1050 NL_SET_ERR_MSG(extack,
1051 "Table has no associated VRF device");
1052 } else {
1053 pr_debug("seg6local: SRv6 End.DT* creation error=%d\n",
1054 vrf_ifindex);
1055 }
1056
1057 return vrf_ifindex;
1058 }
1059
1060 info->net = net;
1061 info->vrf_ifindex = vrf_ifindex;
1062
1063 info->family = family;
1064 info->mode = DT_VRF_MODE;
1065
1066 return 0;
1067 }
1068
1069 /* The SRv6 End.DT4/DT6 behavior extracts the inner (IPv4/IPv6) packet and
1070 * routes the IPv4/IPv6 packet by looking at the configured routing table.
1071 *
1072 * In the SRv6 End.DT4/DT6 use case, we can receive traffic (IPv6+Segment
1073 * Routing Header packets) from several interfaces and the outer IPv6
1074 * destination address (DA) is used for retrieving the specific instance of the
1075 * End.DT4/DT6 behavior that should process the packets.
1076 *
1077 * However, the inner IPv4/IPv6 packet is not really bound to any receiving
1078 * interface and thus the End.DT4/DT6 sets the VRF (associated with the
1079 * corresponding routing table) as the *receiving* interface.
1080 * In other words, the End.DT4/DT6 processes a packet as if it has been received
1081 * directly by the VRF (and not by one of its slave devices, if any).
1082 * In this way, the VRF interface is used for routing the IPv4/IPv6 packet in
1083 * according to the routing table configured by the End.DT4/DT6 instance.
1084 *
1085 * This design allows you to get some interesting features like:
1086 * 1) the statistics on rx packets;
1087 * 2) the possibility to install a packet sniffer on the receiving interface
1088 * (the VRF one) for looking at the incoming packets;
1089 * 3) the possibility to leverage the netfilter prerouting hook for the inner
1090 * IPv4 packet.
1091 *
1092 * This function returns:
1093 * - the sk_buff* when the VRF rcv handler has processed the packet correctly;
1094 * - NULL when the skb is consumed by the VRF rcv handler;
1095 * - a pointer which encodes a negative error number in case of error.
1096 * Note that in this case, the function takes care of freeing the skb.
1097 */
end_dt_vrf_rcv(struct sk_buff * skb,u16 family,struct net_device * dev)1098 static struct sk_buff *end_dt_vrf_rcv(struct sk_buff *skb, u16 family,
1099 struct net_device *dev)
1100 {
1101 /* based on l3mdev_ip_rcv; we are only interested in the master */
1102 if (unlikely(!netif_is_l3_master(dev) && !netif_has_l3_rx_handler(dev)))
1103 goto drop;
1104
1105 if (unlikely(!dev->l3mdev_ops->l3mdev_l3_rcv))
1106 goto drop;
1107
1108 /* the decap packet IPv4/IPv6 does not come with any mac header info.
1109 * We must unset the mac header to allow the VRF device to rebuild it,
1110 * just in case there is a sniffer attached on the device.
1111 */
1112 skb_unset_mac_header(skb);
1113
1114 skb = dev->l3mdev_ops->l3mdev_l3_rcv(dev, skb, family);
1115 if (!skb)
1116 /* the skb buffer was consumed by the handler */
1117 return NULL;
1118
1119 /* when a packet is received by a VRF or by one of its slaves, the
1120 * master device reference is set into the skb.
1121 */
1122 if (unlikely(skb->dev != dev || skb->skb_iif != dev->ifindex))
1123 goto drop;
1124
1125 return skb;
1126
1127 drop:
1128 kfree_skb(skb);
1129 return ERR_PTR(-EINVAL);
1130 }
1131
end_dt_get_vrf_rcu(struct sk_buff * skb,struct seg6_end_dt_info * info)1132 static struct net_device *end_dt_get_vrf_rcu(struct sk_buff *skb,
1133 struct seg6_end_dt_info *info)
1134 {
1135 int vrf_ifindex = info->vrf_ifindex;
1136 struct net *net = info->net;
1137
1138 if (unlikely(vrf_ifindex < 0))
1139 goto error;
1140
1141 if (unlikely(!net_eq(dev_net(skb->dev), net)))
1142 goto error;
1143
1144 return dev_get_by_index_rcu(net, vrf_ifindex);
1145
1146 error:
1147 return NULL;
1148 }
1149
end_dt_vrf_core(struct sk_buff * skb,struct seg6_local_lwt * slwt,u16 family)1150 static struct sk_buff *end_dt_vrf_core(struct sk_buff *skb,
1151 struct seg6_local_lwt *slwt, u16 family)
1152 {
1153 struct seg6_end_dt_info *info = &slwt->dt_info;
1154 struct net_device *vrf;
1155 __be16 protocol;
1156 int hdrlen;
1157
1158 vrf = end_dt_get_vrf_rcu(skb, info);
1159 if (unlikely(!vrf))
1160 goto drop;
1161
1162 switch (family) {
1163 case AF_INET:
1164 protocol = htons(ETH_P_IP);
1165 hdrlen = sizeof(struct iphdr);
1166 break;
1167 case AF_INET6:
1168 protocol = htons(ETH_P_IPV6);
1169 hdrlen = sizeof(struct ipv6hdr);
1170 break;
1171 case AF_UNSPEC:
1172 fallthrough;
1173 default:
1174 goto drop;
1175 }
1176
1177 if (unlikely(info->family != AF_UNSPEC && info->family != family)) {
1178 pr_warn_once("seg6local: SRv6 End.DT* family mismatch");
1179 goto drop;
1180 }
1181
1182 skb->protocol = protocol;
1183
1184 skb_dst_drop(skb);
1185
1186 skb_set_transport_header(skb, hdrlen);
1187 nf_reset_ct(skb);
1188
1189 return end_dt_vrf_rcv(skb, family, vrf);
1190
1191 drop:
1192 kfree_skb(skb);
1193 return ERR_PTR(-EINVAL);
1194 }
1195
input_action_end_dt4(struct sk_buff * skb,struct seg6_local_lwt * slwt)1196 static int input_action_end_dt4(struct sk_buff *skb,
1197 struct seg6_local_lwt *slwt)
1198 {
1199 enum skb_drop_reason reason;
1200 struct iphdr *iph;
1201
1202 if (!decap_and_validate(skb, IPPROTO_IPIP))
1203 goto drop;
1204
1205 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1206 goto drop;
1207
1208 skb = end_dt_vrf_core(skb, slwt, AF_INET);
1209 if (!skb)
1210 /* packet has been processed and consumed by the VRF */
1211 return 0;
1212
1213 if (IS_ERR(skb))
1214 return PTR_ERR(skb);
1215
1216 iph = ip_hdr(skb);
1217
1218 reason = ip_route_input(skb, iph->daddr, iph->saddr, 0, skb->dev);
1219 if (unlikely(reason))
1220 goto drop;
1221
1222 return dst_input(skb);
1223
1224 drop:
1225 kfree_skb(skb);
1226 return -EINVAL;
1227 }
1228
seg6_end_dt4_build(struct seg6_local_lwt * slwt,const void * cfg,struct netlink_ext_ack * extack)1229 static int seg6_end_dt4_build(struct seg6_local_lwt *slwt, const void *cfg,
1230 struct netlink_ext_ack *extack)
1231 {
1232 return __seg6_end_dt_vrf_build(slwt, cfg, AF_INET, extack);
1233 }
1234
1235 static enum
seg6_end_dt6_parse_mode(struct seg6_local_lwt * slwt)1236 seg6_end_dt_mode seg6_end_dt6_parse_mode(struct seg6_local_lwt *slwt)
1237 {
1238 unsigned long parsed_optattrs = slwt->parsed_optattrs;
1239 bool legacy, vrfmode;
1240
1241 legacy = !!(parsed_optattrs & SEG6_F_ATTR(SEG6_LOCAL_TABLE));
1242 vrfmode = !!(parsed_optattrs & SEG6_F_ATTR(SEG6_LOCAL_VRFTABLE));
1243
1244 if (!(legacy ^ vrfmode))
1245 /* both are absent or present: invalid DT6 mode */
1246 return DT_INVALID_MODE;
1247
1248 return legacy ? DT_LEGACY_MODE : DT_VRF_MODE;
1249 }
1250
seg6_end_dt6_get_mode(struct seg6_local_lwt * slwt)1251 static enum seg6_end_dt_mode seg6_end_dt6_get_mode(struct seg6_local_lwt *slwt)
1252 {
1253 struct seg6_end_dt_info *info = &slwt->dt_info;
1254
1255 return info->mode;
1256 }
1257
seg6_end_dt6_build(struct seg6_local_lwt * slwt,const void * cfg,struct netlink_ext_ack * extack)1258 static int seg6_end_dt6_build(struct seg6_local_lwt *slwt, const void *cfg,
1259 struct netlink_ext_ack *extack)
1260 {
1261 enum seg6_end_dt_mode mode = seg6_end_dt6_parse_mode(slwt);
1262 struct seg6_end_dt_info *info = &slwt->dt_info;
1263
1264 switch (mode) {
1265 case DT_LEGACY_MODE:
1266 info->mode = DT_LEGACY_MODE;
1267 return 0;
1268 case DT_VRF_MODE:
1269 return __seg6_end_dt_vrf_build(slwt, cfg, AF_INET6, extack);
1270 default:
1271 NL_SET_ERR_MSG(extack, "table or vrftable must be specified");
1272 return -EINVAL;
1273 }
1274 }
1275 #endif
1276
input_action_end_dt6(struct sk_buff * skb,struct seg6_local_lwt * slwt)1277 static int input_action_end_dt6(struct sk_buff *skb,
1278 struct seg6_local_lwt *slwt)
1279 {
1280 if (!decap_and_validate(skb, IPPROTO_IPV6))
1281 goto drop;
1282
1283 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
1284 goto drop;
1285
1286 #ifdef CONFIG_NET_L3_MASTER_DEV
1287 if (seg6_end_dt6_get_mode(slwt) == DT_LEGACY_MODE)
1288 goto legacy_mode;
1289
1290 /* DT6_VRF_MODE */
1291 skb = end_dt_vrf_core(skb, slwt, AF_INET6);
1292 if (!skb)
1293 /* packet has been processed and consumed by the VRF */
1294 return 0;
1295
1296 if (IS_ERR(skb))
1297 return PTR_ERR(skb);
1298
1299 /* note: this time we do not need to specify the table because the VRF
1300 * takes care of selecting the correct table.
1301 */
1302 seg6_lookup_any_nexthop(skb, NULL, 0, true, 0);
1303
1304 return dst_input(skb);
1305
1306 legacy_mode:
1307 #endif
1308 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
1309
1310 seg6_lookup_any_nexthop(skb, NULL, slwt->table, true, 0);
1311
1312 return dst_input(skb);
1313
1314 drop:
1315 kfree_skb(skb);
1316 return -EINVAL;
1317 }
1318
1319 #ifdef CONFIG_NET_L3_MASTER_DEV
seg6_end_dt46_build(struct seg6_local_lwt * slwt,const void * cfg,struct netlink_ext_ack * extack)1320 static int seg6_end_dt46_build(struct seg6_local_lwt *slwt, const void *cfg,
1321 struct netlink_ext_ack *extack)
1322 {
1323 return __seg6_end_dt_vrf_build(slwt, cfg, AF_UNSPEC, extack);
1324 }
1325
input_action_end_dt46(struct sk_buff * skb,struct seg6_local_lwt * slwt)1326 static int input_action_end_dt46(struct sk_buff *skb,
1327 struct seg6_local_lwt *slwt)
1328 {
1329 unsigned int off = 0;
1330 int nexthdr;
1331
1332 nexthdr = ipv6_find_hdr(skb, &off, -1, NULL, NULL);
1333 if (unlikely(nexthdr < 0))
1334 goto drop;
1335
1336 switch (nexthdr) {
1337 case IPPROTO_IPIP:
1338 return input_action_end_dt4(skb, slwt);
1339 case IPPROTO_IPV6:
1340 return input_action_end_dt6(skb, slwt);
1341 }
1342
1343 drop:
1344 kfree_skb(skb);
1345 return -EINVAL;
1346 }
1347 #endif
1348
1349 /* push an SRH on top of the current one */
input_action_end_b6(struct sk_buff * skb,struct seg6_local_lwt * slwt)1350 static int input_action_end_b6(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1351 {
1352 struct ipv6_sr_hdr *srh;
1353 int err = -EINVAL;
1354
1355 srh = get_and_validate_srh(skb);
1356 if (!srh)
1357 goto drop;
1358
1359 err = seg6_do_srh_inline(skb, slwt->srh);
1360 if (err)
1361 goto drop;
1362
1363 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
1364
1365 seg6_lookup_nexthop(skb, NULL, 0);
1366
1367 return dst_input(skb);
1368
1369 drop:
1370 kfree_skb(skb);
1371 return err;
1372 }
1373
1374 /* encapsulate within an outer IPv6 header and a specified SRH */
input_action_end_b6_encap(struct sk_buff * skb,struct seg6_local_lwt * slwt)1375 static int input_action_end_b6_encap(struct sk_buff *skb,
1376 struct seg6_local_lwt *slwt)
1377 {
1378 struct ipv6_sr_hdr *srh;
1379 int err = -EINVAL;
1380
1381 srh = get_and_validate_srh(skb);
1382 if (!srh)
1383 goto drop;
1384
1385 advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
1386
1387 skb_reset_inner_headers(skb);
1388 skb->encapsulation = 1;
1389
1390 err = seg6_do_srh_encap(skb, slwt->srh, IPPROTO_IPV6);
1391 if (err)
1392 goto drop;
1393
1394 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
1395
1396 seg6_lookup_nexthop(skb, NULL, 0);
1397
1398 return dst_input(skb);
1399
1400 drop:
1401 kfree_skb(skb);
1402 return err;
1403 }
1404
1405 DEFINE_PER_CPU(struct seg6_bpf_srh_state, seg6_bpf_srh_states) = {
1406 .bh_lock = INIT_LOCAL_LOCK(bh_lock),
1407 };
1408
seg6_bpf_has_valid_srh(struct sk_buff * skb)1409 bool seg6_bpf_has_valid_srh(struct sk_buff *skb)
1410 {
1411 struct seg6_bpf_srh_state *srh_state =
1412 this_cpu_ptr(&seg6_bpf_srh_states);
1413 struct ipv6_sr_hdr *srh = srh_state->srh;
1414
1415 lockdep_assert_held(&srh_state->bh_lock);
1416 if (unlikely(srh == NULL))
1417 return false;
1418
1419 if (unlikely(!srh_state->valid)) {
1420 if ((srh_state->hdrlen & 7) != 0)
1421 return false;
1422
1423 srh->hdrlen = (u8)(srh_state->hdrlen >> 3);
1424 if (!seg6_validate_srh(srh, (srh->hdrlen + 1) << 3, true))
1425 return false;
1426
1427 srh_state->valid = true;
1428 }
1429
1430 return true;
1431 }
1432
input_action_end_bpf(struct sk_buff * skb,struct seg6_local_lwt * slwt)1433 static int input_action_end_bpf(struct sk_buff *skb,
1434 struct seg6_local_lwt *slwt)
1435 {
1436 struct seg6_bpf_srh_state *srh_state;
1437 struct ipv6_sr_hdr *srh;
1438 int ret;
1439
1440 srh = get_and_validate_srh(skb);
1441 if (!srh) {
1442 kfree_skb(skb);
1443 return -EINVAL;
1444 }
1445 advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
1446
1447 /* The access to the per-CPU buffer srh_state is protected by running
1448 * always in softirq context (with disabled BH). On PREEMPT_RT the
1449 * required locking is provided by the following local_lock_nested_bh()
1450 * statement. It is also accessed by the bpf_lwt_seg6_* helpers via
1451 * bpf_prog_run_save_cb().
1452 */
1453 local_lock_nested_bh(&seg6_bpf_srh_states.bh_lock);
1454 srh_state = this_cpu_ptr(&seg6_bpf_srh_states);
1455 srh_state->srh = srh;
1456 srh_state->hdrlen = srh->hdrlen << 3;
1457 srh_state->valid = true;
1458
1459 rcu_read_lock();
1460 bpf_compute_data_pointers(skb);
1461 ret = bpf_prog_run_save_cb(slwt->bpf.prog, skb);
1462 rcu_read_unlock();
1463
1464 switch (ret) {
1465 case BPF_OK:
1466 case BPF_REDIRECT:
1467 break;
1468 case BPF_DROP:
1469 goto drop;
1470 default:
1471 pr_warn_once("bpf-seg6local: Illegal return value %u\n", ret);
1472 goto drop;
1473 }
1474
1475 if (srh_state->srh && !seg6_bpf_has_valid_srh(skb))
1476 goto drop;
1477 local_unlock_nested_bh(&seg6_bpf_srh_states.bh_lock);
1478
1479 if (ret != BPF_REDIRECT)
1480 seg6_lookup_nexthop(skb, NULL, 0);
1481
1482 return dst_input(skb);
1483
1484 drop:
1485 local_unlock_nested_bh(&seg6_bpf_srh_states.bh_lock);
1486 kfree_skb(skb);
1487 return -EINVAL;
1488 }
1489
1490 static struct seg6_action_desc seg6_action_table[] = {
1491 {
1492 .action = SEG6_LOCAL_ACTION_END,
1493 .attrs = 0,
1494 .optattrs = SEG6_F_LOCAL_COUNTERS |
1495 SEG6_F_LOCAL_FLAVORS,
1496 .input = input_action_end,
1497 },
1498 {
1499 .action = SEG6_LOCAL_ACTION_END_X,
1500 .attrs = SEG6_F_ATTR(SEG6_LOCAL_NH6),
1501 .optattrs = SEG6_F_LOCAL_COUNTERS |
1502 SEG6_F_LOCAL_FLAVORS |
1503 SEG6_F_ATTR(SEG6_LOCAL_OIF),
1504 .input = input_action_end_x,
1505 },
1506 {
1507 .action = SEG6_LOCAL_ACTION_END_T,
1508 .attrs = SEG6_F_ATTR(SEG6_LOCAL_TABLE),
1509 .optattrs = SEG6_F_LOCAL_COUNTERS,
1510 .input = input_action_end_t,
1511 },
1512 {
1513 .action = SEG6_LOCAL_ACTION_END_DX2,
1514 .attrs = SEG6_F_ATTR(SEG6_LOCAL_OIF),
1515 .optattrs = SEG6_F_LOCAL_COUNTERS,
1516 .input = input_action_end_dx2,
1517 },
1518 {
1519 .action = SEG6_LOCAL_ACTION_END_DX6,
1520 .attrs = SEG6_F_ATTR(SEG6_LOCAL_NH6),
1521 .optattrs = SEG6_F_LOCAL_COUNTERS,
1522 .input = input_action_end_dx6,
1523 },
1524 {
1525 .action = SEG6_LOCAL_ACTION_END_DX4,
1526 .attrs = SEG6_F_ATTR(SEG6_LOCAL_NH4),
1527 .optattrs = SEG6_F_LOCAL_COUNTERS,
1528 .input = input_action_end_dx4,
1529 },
1530 {
1531 .action = SEG6_LOCAL_ACTION_END_DT4,
1532 .attrs = SEG6_F_ATTR(SEG6_LOCAL_VRFTABLE),
1533 .optattrs = SEG6_F_LOCAL_COUNTERS,
1534 #ifdef CONFIG_NET_L3_MASTER_DEV
1535 .input = input_action_end_dt4,
1536 .slwt_ops = {
1537 .build_state = seg6_end_dt4_build,
1538 },
1539 #endif
1540 },
1541 {
1542 .action = SEG6_LOCAL_ACTION_END_DT6,
1543 #ifdef CONFIG_NET_L3_MASTER_DEV
1544 .attrs = 0,
1545 .optattrs = SEG6_F_LOCAL_COUNTERS |
1546 SEG6_F_ATTR(SEG6_LOCAL_TABLE) |
1547 SEG6_F_ATTR(SEG6_LOCAL_VRFTABLE),
1548 .slwt_ops = {
1549 .build_state = seg6_end_dt6_build,
1550 },
1551 #else
1552 .attrs = SEG6_F_ATTR(SEG6_LOCAL_TABLE),
1553 .optattrs = SEG6_F_LOCAL_COUNTERS,
1554 #endif
1555 .input = input_action_end_dt6,
1556 },
1557 {
1558 .action = SEG6_LOCAL_ACTION_END_DT46,
1559 .attrs = SEG6_F_ATTR(SEG6_LOCAL_VRFTABLE),
1560 .optattrs = SEG6_F_LOCAL_COUNTERS,
1561 #ifdef CONFIG_NET_L3_MASTER_DEV
1562 .input = input_action_end_dt46,
1563 .slwt_ops = {
1564 .build_state = seg6_end_dt46_build,
1565 },
1566 #endif
1567 },
1568 {
1569 .action = SEG6_LOCAL_ACTION_END_B6,
1570 .attrs = SEG6_F_ATTR(SEG6_LOCAL_SRH),
1571 .optattrs = SEG6_F_LOCAL_COUNTERS,
1572 .input = input_action_end_b6,
1573 },
1574 {
1575 .action = SEG6_LOCAL_ACTION_END_B6_ENCAP,
1576 .attrs = SEG6_F_ATTR(SEG6_LOCAL_SRH),
1577 .optattrs = SEG6_F_LOCAL_COUNTERS,
1578 .input = input_action_end_b6_encap,
1579 .static_headroom = sizeof(struct ipv6hdr),
1580 },
1581 {
1582 .action = SEG6_LOCAL_ACTION_END_BPF,
1583 .attrs = SEG6_F_ATTR(SEG6_LOCAL_BPF),
1584 .optattrs = SEG6_F_LOCAL_COUNTERS,
1585 .input = input_action_end_bpf,
1586 },
1587
1588 };
1589
__get_action_desc(int action)1590 static struct seg6_action_desc *__get_action_desc(int action)
1591 {
1592 struct seg6_action_desc *desc;
1593 int i, count;
1594
1595 count = ARRAY_SIZE(seg6_action_table);
1596 for (i = 0; i < count; i++) {
1597 desc = &seg6_action_table[i];
1598 if (desc->action == action)
1599 return desc;
1600 }
1601
1602 return NULL;
1603 }
1604
seg6_lwtunnel_counters_enabled(struct seg6_local_lwt * slwt)1605 static bool seg6_lwtunnel_counters_enabled(struct seg6_local_lwt *slwt)
1606 {
1607 return slwt->parsed_optattrs & SEG6_F_LOCAL_COUNTERS;
1608 }
1609
seg6_local_update_counters(struct seg6_local_lwt * slwt,unsigned int len,int err)1610 static void seg6_local_update_counters(struct seg6_local_lwt *slwt,
1611 unsigned int len, int err)
1612 {
1613 struct pcpu_seg6_local_counters *pcounters;
1614
1615 pcounters = this_cpu_ptr(slwt->pcpu_counters);
1616 u64_stats_update_begin(&pcounters->syncp);
1617
1618 if (likely(!err)) {
1619 u64_stats_inc(&pcounters->packets);
1620 u64_stats_add(&pcounters->bytes, len);
1621 } else {
1622 u64_stats_inc(&pcounters->errors);
1623 }
1624
1625 u64_stats_update_end(&pcounters->syncp);
1626 }
1627
seg6_local_input_core(struct net * net,struct sock * sk,struct sk_buff * skb)1628 static int seg6_local_input_core(struct net *net, struct sock *sk,
1629 struct sk_buff *skb)
1630 {
1631 struct dst_entry *orig_dst = skb_dst(skb);
1632 struct seg6_action_desc *desc;
1633 struct seg6_local_lwt *slwt;
1634 unsigned int len = skb->len;
1635 int rc;
1636
1637 slwt = seg6_local_lwtunnel(orig_dst->lwtstate);
1638 desc = slwt->desc;
1639
1640 rc = desc->input(skb, slwt);
1641
1642 if (!seg6_lwtunnel_counters_enabled(slwt))
1643 return rc;
1644
1645 seg6_local_update_counters(slwt, len, rc);
1646
1647 return rc;
1648 }
1649
seg6_local_input(struct sk_buff * skb)1650 static int seg6_local_input(struct sk_buff *skb)
1651 {
1652 if (skb->protocol != htons(ETH_P_IPV6)) {
1653 kfree_skb(skb);
1654 return -EINVAL;
1655 }
1656
1657 if (static_branch_unlikely(&nf_hooks_lwtunnel_enabled))
1658 return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_IN,
1659 dev_net(skb->dev), NULL, skb, skb->dev, NULL,
1660 seg6_local_input_core);
1661
1662 return seg6_local_input_core(dev_net(skb->dev), NULL, skb);
1663 }
1664
1665 static const struct nla_policy seg6_local_policy[SEG6_LOCAL_MAX + 1] = {
1666 [SEG6_LOCAL_ACTION] = { .type = NLA_U32 },
1667 [SEG6_LOCAL_SRH] = { .type = NLA_BINARY },
1668 [SEG6_LOCAL_TABLE] = { .type = NLA_U32 },
1669 [SEG6_LOCAL_VRFTABLE] = { .type = NLA_U32 },
1670 [SEG6_LOCAL_NH4] = NLA_POLICY_EXACT_LEN(sizeof(struct in_addr)),
1671 [SEG6_LOCAL_NH6] = NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)),
1672 [SEG6_LOCAL_IIF] = { .type = NLA_U32 },
1673 [SEG6_LOCAL_OIF] = { .type = NLA_U32 },
1674 [SEG6_LOCAL_BPF] = { .type = NLA_NESTED },
1675 [SEG6_LOCAL_COUNTERS] = { .type = NLA_NESTED },
1676 [SEG6_LOCAL_FLAVORS] = { .type = NLA_NESTED },
1677 };
1678
parse_nla_srh(struct nlattr ** attrs,struct seg6_local_lwt * slwt,struct netlink_ext_ack * extack)1679 static int parse_nla_srh(struct nlattr **attrs, struct seg6_local_lwt *slwt,
1680 struct netlink_ext_ack *extack)
1681 {
1682 struct ipv6_sr_hdr *srh;
1683 int len;
1684
1685 srh = nla_data(attrs[SEG6_LOCAL_SRH]);
1686 len = nla_len(attrs[SEG6_LOCAL_SRH]);
1687
1688 /* SRH must contain at least one segment */
1689 if (len < sizeof(*srh) + sizeof(struct in6_addr))
1690 return -EINVAL;
1691
1692 if (!seg6_validate_srh(srh, len, false))
1693 return -EINVAL;
1694
1695 slwt->srh = kmemdup(srh, len, GFP_KERNEL);
1696 if (!slwt->srh)
1697 return -ENOMEM;
1698
1699 slwt->headroom += len;
1700
1701 return 0;
1702 }
1703
put_nla_srh(struct sk_buff * skb,struct seg6_local_lwt * slwt)1704 static int put_nla_srh(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1705 {
1706 struct ipv6_sr_hdr *srh;
1707 struct nlattr *nla;
1708 int len;
1709
1710 srh = slwt->srh;
1711 len = (srh->hdrlen + 1) << 3;
1712
1713 nla = nla_reserve(skb, SEG6_LOCAL_SRH, len);
1714 if (!nla)
1715 return -EMSGSIZE;
1716
1717 memcpy(nla_data(nla), srh, len);
1718
1719 return 0;
1720 }
1721
cmp_nla_srh(struct seg6_local_lwt * a,struct seg6_local_lwt * b)1722 static int cmp_nla_srh(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1723 {
1724 int len = (a->srh->hdrlen + 1) << 3;
1725
1726 if (len != ((b->srh->hdrlen + 1) << 3))
1727 return 1;
1728
1729 return memcmp(a->srh, b->srh, len);
1730 }
1731
destroy_attr_srh(struct seg6_local_lwt * slwt)1732 static void destroy_attr_srh(struct seg6_local_lwt *slwt)
1733 {
1734 kfree(slwt->srh);
1735 }
1736
parse_nla_table(struct nlattr ** attrs,struct seg6_local_lwt * slwt,struct netlink_ext_ack * extack)1737 static int parse_nla_table(struct nlattr **attrs, struct seg6_local_lwt *slwt,
1738 struct netlink_ext_ack *extack)
1739 {
1740 slwt->table = nla_get_u32(attrs[SEG6_LOCAL_TABLE]);
1741
1742 return 0;
1743 }
1744
put_nla_table(struct sk_buff * skb,struct seg6_local_lwt * slwt)1745 static int put_nla_table(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1746 {
1747 if (nla_put_u32(skb, SEG6_LOCAL_TABLE, slwt->table))
1748 return -EMSGSIZE;
1749
1750 return 0;
1751 }
1752
cmp_nla_table(struct seg6_local_lwt * a,struct seg6_local_lwt * b)1753 static int cmp_nla_table(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1754 {
1755 if (a->table != b->table)
1756 return 1;
1757
1758 return 0;
1759 }
1760
1761 static struct
seg6_possible_end_dt_info(struct seg6_local_lwt * slwt)1762 seg6_end_dt_info *seg6_possible_end_dt_info(struct seg6_local_lwt *slwt)
1763 {
1764 #ifdef CONFIG_NET_L3_MASTER_DEV
1765 return &slwt->dt_info;
1766 #else
1767 return ERR_PTR(-EOPNOTSUPP);
1768 #endif
1769 }
1770
parse_nla_vrftable(struct nlattr ** attrs,struct seg6_local_lwt * slwt,struct netlink_ext_ack * extack)1771 static int parse_nla_vrftable(struct nlattr **attrs,
1772 struct seg6_local_lwt *slwt,
1773 struct netlink_ext_ack *extack)
1774 {
1775 struct seg6_end_dt_info *info = seg6_possible_end_dt_info(slwt);
1776
1777 if (IS_ERR(info))
1778 return PTR_ERR(info);
1779
1780 info->vrf_table = nla_get_u32(attrs[SEG6_LOCAL_VRFTABLE]);
1781
1782 return 0;
1783 }
1784
put_nla_vrftable(struct sk_buff * skb,struct seg6_local_lwt * slwt)1785 static int put_nla_vrftable(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1786 {
1787 struct seg6_end_dt_info *info = seg6_possible_end_dt_info(slwt);
1788
1789 if (IS_ERR(info))
1790 return PTR_ERR(info);
1791
1792 if (nla_put_u32(skb, SEG6_LOCAL_VRFTABLE, info->vrf_table))
1793 return -EMSGSIZE;
1794
1795 return 0;
1796 }
1797
cmp_nla_vrftable(struct seg6_local_lwt * a,struct seg6_local_lwt * b)1798 static int cmp_nla_vrftable(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1799 {
1800 struct seg6_end_dt_info *info_a = seg6_possible_end_dt_info(a);
1801 struct seg6_end_dt_info *info_b = seg6_possible_end_dt_info(b);
1802
1803 if (info_a->vrf_table != info_b->vrf_table)
1804 return 1;
1805
1806 return 0;
1807 }
1808
parse_nla_nh4(struct nlattr ** attrs,struct seg6_local_lwt * slwt,struct netlink_ext_ack * extack)1809 static int parse_nla_nh4(struct nlattr **attrs, struct seg6_local_lwt *slwt,
1810 struct netlink_ext_ack *extack)
1811 {
1812 memcpy(&slwt->nh4, nla_data(attrs[SEG6_LOCAL_NH4]),
1813 sizeof(struct in_addr));
1814
1815 return 0;
1816 }
1817
put_nla_nh4(struct sk_buff * skb,struct seg6_local_lwt * slwt)1818 static int put_nla_nh4(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1819 {
1820 struct nlattr *nla;
1821
1822 nla = nla_reserve(skb, SEG6_LOCAL_NH4, sizeof(struct in_addr));
1823 if (!nla)
1824 return -EMSGSIZE;
1825
1826 memcpy(nla_data(nla), &slwt->nh4, sizeof(struct in_addr));
1827
1828 return 0;
1829 }
1830
cmp_nla_nh4(struct seg6_local_lwt * a,struct seg6_local_lwt * b)1831 static int cmp_nla_nh4(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1832 {
1833 return memcmp(&a->nh4, &b->nh4, sizeof(struct in_addr));
1834 }
1835
parse_nla_nh6(struct nlattr ** attrs,struct seg6_local_lwt * slwt,struct netlink_ext_ack * extack)1836 static int parse_nla_nh6(struct nlattr **attrs, struct seg6_local_lwt *slwt,
1837 struct netlink_ext_ack *extack)
1838 {
1839 memcpy(&slwt->nh6, nla_data(attrs[SEG6_LOCAL_NH6]),
1840 sizeof(struct in6_addr));
1841
1842 return 0;
1843 }
1844
put_nla_nh6(struct sk_buff * skb,struct seg6_local_lwt * slwt)1845 static int put_nla_nh6(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1846 {
1847 struct nlattr *nla;
1848
1849 nla = nla_reserve(skb, SEG6_LOCAL_NH6, sizeof(struct in6_addr));
1850 if (!nla)
1851 return -EMSGSIZE;
1852
1853 memcpy(nla_data(nla), &slwt->nh6, sizeof(struct in6_addr));
1854
1855 return 0;
1856 }
1857
cmp_nla_nh6(struct seg6_local_lwt * a,struct seg6_local_lwt * b)1858 static int cmp_nla_nh6(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1859 {
1860 return memcmp(&a->nh6, &b->nh6, sizeof(struct in6_addr));
1861 }
1862
parse_nla_iif(struct nlattr ** attrs,struct seg6_local_lwt * slwt,struct netlink_ext_ack * extack)1863 static int parse_nla_iif(struct nlattr **attrs, struct seg6_local_lwt *slwt,
1864 struct netlink_ext_ack *extack)
1865 {
1866 slwt->iif = nla_get_u32(attrs[SEG6_LOCAL_IIF]);
1867
1868 return 0;
1869 }
1870
put_nla_iif(struct sk_buff * skb,struct seg6_local_lwt * slwt)1871 static int put_nla_iif(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1872 {
1873 if (nla_put_u32(skb, SEG6_LOCAL_IIF, slwt->iif))
1874 return -EMSGSIZE;
1875
1876 return 0;
1877 }
1878
cmp_nla_iif(struct seg6_local_lwt * a,struct seg6_local_lwt * b)1879 static int cmp_nla_iif(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1880 {
1881 if (a->iif != b->iif)
1882 return 1;
1883
1884 return 0;
1885 }
1886
parse_nla_oif(struct nlattr ** attrs,struct seg6_local_lwt * slwt,struct netlink_ext_ack * extack)1887 static int parse_nla_oif(struct nlattr **attrs, struct seg6_local_lwt *slwt,
1888 struct netlink_ext_ack *extack)
1889 {
1890 slwt->oif = nla_get_u32(attrs[SEG6_LOCAL_OIF]);
1891
1892 return 0;
1893 }
1894
put_nla_oif(struct sk_buff * skb,struct seg6_local_lwt * slwt)1895 static int put_nla_oif(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1896 {
1897 if (nla_put_u32(skb, SEG6_LOCAL_OIF, slwt->oif))
1898 return -EMSGSIZE;
1899
1900 return 0;
1901 }
1902
cmp_nla_oif(struct seg6_local_lwt * a,struct seg6_local_lwt * b)1903 static int cmp_nla_oif(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1904 {
1905 if (a->oif != b->oif)
1906 return 1;
1907
1908 return 0;
1909 }
1910
1911 #define MAX_PROG_NAME 256
1912 static const struct nla_policy bpf_prog_policy[SEG6_LOCAL_BPF_PROG_MAX + 1] = {
1913 [SEG6_LOCAL_BPF_PROG] = { .type = NLA_U32, },
1914 [SEG6_LOCAL_BPF_PROG_NAME] = { .type = NLA_NUL_STRING,
1915 .len = MAX_PROG_NAME },
1916 };
1917
parse_nla_bpf(struct nlattr ** attrs,struct seg6_local_lwt * slwt,struct netlink_ext_ack * extack)1918 static int parse_nla_bpf(struct nlattr **attrs, struct seg6_local_lwt *slwt,
1919 struct netlink_ext_ack *extack)
1920 {
1921 struct nlattr *tb[SEG6_LOCAL_BPF_PROG_MAX + 1];
1922 struct bpf_prog *p;
1923 int ret;
1924 u32 fd;
1925
1926 ret = nla_parse_nested_deprecated(tb, SEG6_LOCAL_BPF_PROG_MAX,
1927 attrs[SEG6_LOCAL_BPF],
1928 bpf_prog_policy, NULL);
1929 if (ret < 0)
1930 return ret;
1931
1932 if (!tb[SEG6_LOCAL_BPF_PROG] || !tb[SEG6_LOCAL_BPF_PROG_NAME])
1933 return -EINVAL;
1934
1935 slwt->bpf.name = nla_memdup(tb[SEG6_LOCAL_BPF_PROG_NAME], GFP_KERNEL);
1936 if (!slwt->bpf.name)
1937 return -ENOMEM;
1938
1939 fd = nla_get_u32(tb[SEG6_LOCAL_BPF_PROG]);
1940 p = bpf_prog_get_type(fd, BPF_PROG_TYPE_LWT_SEG6LOCAL);
1941 if (IS_ERR(p)) {
1942 kfree(slwt->bpf.name);
1943 return PTR_ERR(p);
1944 }
1945
1946 slwt->bpf.prog = p;
1947 return 0;
1948 }
1949
put_nla_bpf(struct sk_buff * skb,struct seg6_local_lwt * slwt)1950 static int put_nla_bpf(struct sk_buff *skb, struct seg6_local_lwt *slwt)
1951 {
1952 struct nlattr *nest;
1953
1954 if (!slwt->bpf.prog)
1955 return 0;
1956
1957 nest = nla_nest_start_noflag(skb, SEG6_LOCAL_BPF);
1958 if (!nest)
1959 return -EMSGSIZE;
1960
1961 if (nla_put_u32(skb, SEG6_LOCAL_BPF_PROG, slwt->bpf.prog->aux->id))
1962 return -EMSGSIZE;
1963
1964 if (slwt->bpf.name &&
1965 nla_put_string(skb, SEG6_LOCAL_BPF_PROG_NAME, slwt->bpf.name))
1966 return -EMSGSIZE;
1967
1968 return nla_nest_end(skb, nest);
1969 }
1970
cmp_nla_bpf(struct seg6_local_lwt * a,struct seg6_local_lwt * b)1971 static int cmp_nla_bpf(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
1972 {
1973 if (!a->bpf.name && !b->bpf.name)
1974 return 0;
1975
1976 if (!a->bpf.name || !b->bpf.name)
1977 return 1;
1978
1979 return strcmp(a->bpf.name, b->bpf.name);
1980 }
1981
destroy_attr_bpf(struct seg6_local_lwt * slwt)1982 static void destroy_attr_bpf(struct seg6_local_lwt *slwt)
1983 {
1984 kfree(slwt->bpf.name);
1985 if (slwt->bpf.prog)
1986 bpf_prog_put(slwt->bpf.prog);
1987 }
1988
1989 static const struct
1990 nla_policy seg6_local_counters_policy[SEG6_LOCAL_CNT_MAX + 1] = {
1991 [SEG6_LOCAL_CNT_PACKETS] = { .type = NLA_U64 },
1992 [SEG6_LOCAL_CNT_BYTES] = { .type = NLA_U64 },
1993 [SEG6_LOCAL_CNT_ERRORS] = { .type = NLA_U64 },
1994 };
1995
parse_nla_counters(struct nlattr ** attrs,struct seg6_local_lwt * slwt,struct netlink_ext_ack * extack)1996 static int parse_nla_counters(struct nlattr **attrs,
1997 struct seg6_local_lwt *slwt,
1998 struct netlink_ext_ack *extack)
1999 {
2000 struct pcpu_seg6_local_counters __percpu *pcounters;
2001 struct nlattr *tb[SEG6_LOCAL_CNT_MAX + 1];
2002 int ret;
2003
2004 ret = nla_parse_nested_deprecated(tb, SEG6_LOCAL_CNT_MAX,
2005 attrs[SEG6_LOCAL_COUNTERS],
2006 seg6_local_counters_policy, NULL);
2007 if (ret < 0)
2008 return ret;
2009
2010 /* basic support for SRv6 Behavior counters requires at least:
2011 * packets, bytes and errors.
2012 */
2013 if (!tb[SEG6_LOCAL_CNT_PACKETS] || !tb[SEG6_LOCAL_CNT_BYTES] ||
2014 !tb[SEG6_LOCAL_CNT_ERRORS])
2015 return -EINVAL;
2016
2017 /* counters are always zero initialized */
2018 pcounters = seg6_local_alloc_pcpu_counters(GFP_KERNEL);
2019 if (!pcounters)
2020 return -ENOMEM;
2021
2022 slwt->pcpu_counters = pcounters;
2023
2024 return 0;
2025 }
2026
seg6_local_fill_nla_counters(struct sk_buff * skb,struct seg6_local_counters * counters)2027 static int seg6_local_fill_nla_counters(struct sk_buff *skb,
2028 struct seg6_local_counters *counters)
2029 {
2030 if (nla_put_u64_64bit(skb, SEG6_LOCAL_CNT_PACKETS, counters->packets,
2031 SEG6_LOCAL_CNT_PAD))
2032 return -EMSGSIZE;
2033
2034 if (nla_put_u64_64bit(skb, SEG6_LOCAL_CNT_BYTES, counters->bytes,
2035 SEG6_LOCAL_CNT_PAD))
2036 return -EMSGSIZE;
2037
2038 if (nla_put_u64_64bit(skb, SEG6_LOCAL_CNT_ERRORS, counters->errors,
2039 SEG6_LOCAL_CNT_PAD))
2040 return -EMSGSIZE;
2041
2042 return 0;
2043 }
2044
put_nla_counters(struct sk_buff * skb,struct seg6_local_lwt * slwt)2045 static int put_nla_counters(struct sk_buff *skb, struct seg6_local_lwt *slwt)
2046 {
2047 struct seg6_local_counters counters = { 0, 0, 0 };
2048 struct nlattr *nest;
2049 int rc, i;
2050
2051 nest = nla_nest_start(skb, SEG6_LOCAL_COUNTERS);
2052 if (!nest)
2053 return -EMSGSIZE;
2054
2055 for_each_possible_cpu(i) {
2056 struct pcpu_seg6_local_counters *pcounters;
2057 u64 packets, bytes, errors;
2058 unsigned int start;
2059
2060 pcounters = per_cpu_ptr(slwt->pcpu_counters, i);
2061 do {
2062 start = u64_stats_fetch_begin(&pcounters->syncp);
2063
2064 packets = u64_stats_read(&pcounters->packets);
2065 bytes = u64_stats_read(&pcounters->bytes);
2066 errors = u64_stats_read(&pcounters->errors);
2067
2068 } while (u64_stats_fetch_retry(&pcounters->syncp, start));
2069
2070 counters.packets += packets;
2071 counters.bytes += bytes;
2072 counters.errors += errors;
2073 }
2074
2075 rc = seg6_local_fill_nla_counters(skb, &counters);
2076 if (rc < 0) {
2077 nla_nest_cancel(skb, nest);
2078 return rc;
2079 }
2080
2081 return nla_nest_end(skb, nest);
2082 }
2083
cmp_nla_counters(struct seg6_local_lwt * a,struct seg6_local_lwt * b)2084 static int cmp_nla_counters(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
2085 {
2086 /* a and b are equal if both have pcpu_counters set or not */
2087 return (!!((unsigned long)a->pcpu_counters)) ^
2088 (!!((unsigned long)b->pcpu_counters));
2089 }
2090
destroy_attr_counters(struct seg6_local_lwt * slwt)2091 static void destroy_attr_counters(struct seg6_local_lwt *slwt)
2092 {
2093 free_percpu(slwt->pcpu_counters);
2094 }
2095
2096 static const
2097 struct nla_policy seg6_local_flavors_policy[SEG6_LOCAL_FLV_MAX + 1] = {
2098 [SEG6_LOCAL_FLV_OPERATION] = { .type = NLA_U32 },
2099 [SEG6_LOCAL_FLV_LCBLOCK_BITS] = { .type = NLA_U8 },
2100 [SEG6_LOCAL_FLV_LCNODE_FN_BITS] = { .type = NLA_U8 },
2101 };
2102
2103 /* check whether the lengths of the Locator-Block and Locator-Node Function
2104 * are compatible with the dimension of a C-SID container.
2105 */
seg6_chk_next_csid_cfg(__u8 block_len,__u8 func_len)2106 static int seg6_chk_next_csid_cfg(__u8 block_len, __u8 func_len)
2107 {
2108 /* Locator-Block and Locator-Node Function cannot exceed 128 bits
2109 * (i.e. C-SID container length).
2110 */
2111 if (next_csid_chk_cntr_bits(block_len, func_len))
2112 return -EINVAL;
2113
2114 /* Locator-Block length must be greater than zero and evenly divisible
2115 * by 8. There must be room for a Locator-Node Function, at least.
2116 */
2117 if (next_csid_chk_lcblock_bits(block_len))
2118 return -EINVAL;
2119
2120 /* Locator-Node Function length must be greater than zero and evenly
2121 * divisible by 8. There must be room for the Locator-Block.
2122 */
2123 if (next_csid_chk_lcnode_fn_bits(func_len))
2124 return -EINVAL;
2125
2126 return 0;
2127 }
2128
seg6_parse_nla_next_csid_cfg(struct nlattr ** tb,struct seg6_flavors_info * finfo,struct netlink_ext_ack * extack)2129 static int seg6_parse_nla_next_csid_cfg(struct nlattr **tb,
2130 struct seg6_flavors_info *finfo,
2131 struct netlink_ext_ack *extack)
2132 {
2133 __u8 func_len = SEG6_LOCAL_LCNODE_FN_DBITS;
2134 __u8 block_len = SEG6_LOCAL_LCBLOCK_DBITS;
2135 int rc;
2136
2137 if (tb[SEG6_LOCAL_FLV_LCBLOCK_BITS])
2138 block_len = nla_get_u8(tb[SEG6_LOCAL_FLV_LCBLOCK_BITS]);
2139
2140 if (tb[SEG6_LOCAL_FLV_LCNODE_FN_BITS])
2141 func_len = nla_get_u8(tb[SEG6_LOCAL_FLV_LCNODE_FN_BITS]);
2142
2143 rc = seg6_chk_next_csid_cfg(block_len, func_len);
2144 if (rc < 0) {
2145 NL_SET_ERR_MSG(extack,
2146 "Invalid Locator Block/Node Function lengths");
2147 return rc;
2148 }
2149
2150 finfo->lcblock_bits = block_len;
2151 finfo->lcnode_func_bits = func_len;
2152
2153 return 0;
2154 }
2155
parse_nla_flavors(struct nlattr ** attrs,struct seg6_local_lwt * slwt,struct netlink_ext_ack * extack)2156 static int parse_nla_flavors(struct nlattr **attrs, struct seg6_local_lwt *slwt,
2157 struct netlink_ext_ack *extack)
2158 {
2159 struct seg6_flavors_info *finfo = &slwt->flv_info;
2160 struct nlattr *tb[SEG6_LOCAL_FLV_MAX + 1];
2161 int action = slwt->action;
2162 __u32 fops, supp_fops;
2163 int rc;
2164
2165 rc = nla_parse_nested_deprecated(tb, SEG6_LOCAL_FLV_MAX,
2166 attrs[SEG6_LOCAL_FLAVORS],
2167 seg6_local_flavors_policy, NULL);
2168 if (rc < 0)
2169 return rc;
2170
2171 /* this attribute MUST always be present since it represents the Flavor
2172 * operation(s) to be carried out.
2173 */
2174 if (!tb[SEG6_LOCAL_FLV_OPERATION])
2175 return -EINVAL;
2176
2177 fops = nla_get_u32(tb[SEG6_LOCAL_FLV_OPERATION]);
2178 rc = seg6_flv_supp_ops_by_action(action, &supp_fops);
2179 if (rc < 0 || (fops & ~supp_fops)) {
2180 NL_SET_ERR_MSG(extack, "Unsupported Flavor operation(s)");
2181 return -EOPNOTSUPP;
2182 }
2183
2184 finfo->flv_ops = fops;
2185
2186 if (seg6_next_csid_enabled(fops)) {
2187 /* Locator-Block and Locator-Node Function lengths can be
2188 * provided by the user space. Otherwise, default values are
2189 * applied.
2190 */
2191 rc = seg6_parse_nla_next_csid_cfg(tb, finfo, extack);
2192 if (rc < 0)
2193 return rc;
2194 }
2195
2196 return 0;
2197 }
2198
seg6_fill_nla_next_csid_cfg(struct sk_buff * skb,struct seg6_flavors_info * finfo)2199 static int seg6_fill_nla_next_csid_cfg(struct sk_buff *skb,
2200 struct seg6_flavors_info *finfo)
2201 {
2202 if (nla_put_u8(skb, SEG6_LOCAL_FLV_LCBLOCK_BITS, finfo->lcblock_bits))
2203 return -EMSGSIZE;
2204
2205 if (nla_put_u8(skb, SEG6_LOCAL_FLV_LCNODE_FN_BITS,
2206 finfo->lcnode_func_bits))
2207 return -EMSGSIZE;
2208
2209 return 0;
2210 }
2211
put_nla_flavors(struct sk_buff * skb,struct seg6_local_lwt * slwt)2212 static int put_nla_flavors(struct sk_buff *skb, struct seg6_local_lwt *slwt)
2213 {
2214 struct seg6_flavors_info *finfo = &slwt->flv_info;
2215 __u32 fops = finfo->flv_ops;
2216 struct nlattr *nest;
2217 int rc;
2218
2219 nest = nla_nest_start(skb, SEG6_LOCAL_FLAVORS);
2220 if (!nest)
2221 return -EMSGSIZE;
2222
2223 if (nla_put_u32(skb, SEG6_LOCAL_FLV_OPERATION, fops)) {
2224 rc = -EMSGSIZE;
2225 goto err;
2226 }
2227
2228 if (seg6_next_csid_enabled(fops)) {
2229 rc = seg6_fill_nla_next_csid_cfg(skb, finfo);
2230 if (rc < 0)
2231 goto err;
2232 }
2233
2234 return nla_nest_end(skb, nest);
2235
2236 err:
2237 nla_nest_cancel(skb, nest);
2238 return rc;
2239 }
2240
seg6_cmp_nla_next_csid_cfg(struct seg6_flavors_info * finfo_a,struct seg6_flavors_info * finfo_b)2241 static int seg6_cmp_nla_next_csid_cfg(struct seg6_flavors_info *finfo_a,
2242 struct seg6_flavors_info *finfo_b)
2243 {
2244 if (finfo_a->lcblock_bits != finfo_b->lcblock_bits)
2245 return 1;
2246
2247 if (finfo_a->lcnode_func_bits != finfo_b->lcnode_func_bits)
2248 return 1;
2249
2250 return 0;
2251 }
2252
cmp_nla_flavors(struct seg6_local_lwt * a,struct seg6_local_lwt * b)2253 static int cmp_nla_flavors(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
2254 {
2255 struct seg6_flavors_info *finfo_a = &a->flv_info;
2256 struct seg6_flavors_info *finfo_b = &b->flv_info;
2257
2258 if (finfo_a->flv_ops != finfo_b->flv_ops)
2259 return 1;
2260
2261 if (seg6_next_csid_enabled(finfo_a->flv_ops)) {
2262 if (seg6_cmp_nla_next_csid_cfg(finfo_a, finfo_b))
2263 return 1;
2264 }
2265
2266 return 0;
2267 }
2268
encap_size_flavors(struct seg6_local_lwt * slwt)2269 static int encap_size_flavors(struct seg6_local_lwt *slwt)
2270 {
2271 struct seg6_flavors_info *finfo = &slwt->flv_info;
2272 int nlsize;
2273
2274 nlsize = nla_total_size(0) + /* nest SEG6_LOCAL_FLAVORS */
2275 nla_total_size(4); /* SEG6_LOCAL_FLV_OPERATION */
2276
2277 if (seg6_next_csid_enabled(finfo->flv_ops))
2278 nlsize += nla_total_size(1) + /* SEG6_LOCAL_FLV_LCBLOCK_BITS */
2279 nla_total_size(1); /* SEG6_LOCAL_FLV_LCNODE_FN_BITS */
2280
2281 return nlsize;
2282 }
2283
2284 struct seg6_action_param {
2285 int (*parse)(struct nlattr **attrs, struct seg6_local_lwt *slwt,
2286 struct netlink_ext_ack *extack);
2287 int (*put)(struct sk_buff *skb, struct seg6_local_lwt *slwt);
2288 int (*cmp)(struct seg6_local_lwt *a, struct seg6_local_lwt *b);
2289
2290 /* optional destroy() callback useful for releasing resources which
2291 * have been previously acquired in the corresponding parse()
2292 * function.
2293 */
2294 void (*destroy)(struct seg6_local_lwt *slwt);
2295 };
2296
2297 static struct seg6_action_param seg6_action_params[SEG6_LOCAL_MAX + 1] = {
2298 [SEG6_LOCAL_SRH] = { .parse = parse_nla_srh,
2299 .put = put_nla_srh,
2300 .cmp = cmp_nla_srh,
2301 .destroy = destroy_attr_srh },
2302
2303 [SEG6_LOCAL_TABLE] = { .parse = parse_nla_table,
2304 .put = put_nla_table,
2305 .cmp = cmp_nla_table },
2306
2307 [SEG6_LOCAL_NH4] = { .parse = parse_nla_nh4,
2308 .put = put_nla_nh4,
2309 .cmp = cmp_nla_nh4 },
2310
2311 [SEG6_LOCAL_NH6] = { .parse = parse_nla_nh6,
2312 .put = put_nla_nh6,
2313 .cmp = cmp_nla_nh6 },
2314
2315 [SEG6_LOCAL_IIF] = { .parse = parse_nla_iif,
2316 .put = put_nla_iif,
2317 .cmp = cmp_nla_iif },
2318
2319 [SEG6_LOCAL_OIF] = { .parse = parse_nla_oif,
2320 .put = put_nla_oif,
2321 .cmp = cmp_nla_oif },
2322
2323 [SEG6_LOCAL_BPF] = { .parse = parse_nla_bpf,
2324 .put = put_nla_bpf,
2325 .cmp = cmp_nla_bpf,
2326 .destroy = destroy_attr_bpf },
2327
2328 [SEG6_LOCAL_VRFTABLE] = { .parse = parse_nla_vrftable,
2329 .put = put_nla_vrftable,
2330 .cmp = cmp_nla_vrftable },
2331
2332 [SEG6_LOCAL_COUNTERS] = { .parse = parse_nla_counters,
2333 .put = put_nla_counters,
2334 .cmp = cmp_nla_counters,
2335 .destroy = destroy_attr_counters },
2336
2337 [SEG6_LOCAL_FLAVORS] = { .parse = parse_nla_flavors,
2338 .put = put_nla_flavors,
2339 .cmp = cmp_nla_flavors },
2340 };
2341
2342 /* call the destroy() callback (if available) for each set attribute in
2343 * @parsed_attrs, starting from the first attribute up to the @max_parsed
2344 * (excluded) attribute.
2345 */
__destroy_attrs(unsigned long parsed_attrs,int max_parsed,struct seg6_local_lwt * slwt)2346 static void __destroy_attrs(unsigned long parsed_attrs, int max_parsed,
2347 struct seg6_local_lwt *slwt)
2348 {
2349 struct seg6_action_param *param;
2350 int i;
2351
2352 /* Every required seg6local attribute is identified by an ID which is
2353 * encoded as a flag (i.e: 1 << ID) in the 'attrs' bitmask;
2354 *
2355 * We scan the 'parsed_attrs' bitmask, starting from the first attribute
2356 * up to the @max_parsed (excluded) attribute.
2357 * For each set attribute, we retrieve the corresponding destroy()
2358 * callback. If the callback is not available, then we skip to the next
2359 * attribute; otherwise, we call the destroy() callback.
2360 */
2361 for (i = SEG6_LOCAL_SRH; i < max_parsed; ++i) {
2362 if (!(parsed_attrs & SEG6_F_ATTR(i)))
2363 continue;
2364
2365 param = &seg6_action_params[i];
2366
2367 if (param->destroy)
2368 param->destroy(slwt);
2369 }
2370 }
2371
2372 /* release all the resources that may have been acquired during parsing
2373 * operations.
2374 */
destroy_attrs(struct seg6_local_lwt * slwt)2375 static void destroy_attrs(struct seg6_local_lwt *slwt)
2376 {
2377 unsigned long attrs = slwt->desc->attrs | slwt->parsed_optattrs;
2378
2379 __destroy_attrs(attrs, SEG6_LOCAL_MAX + 1, slwt);
2380 }
2381
parse_nla_optional_attrs(struct nlattr ** attrs,struct seg6_local_lwt * slwt,struct netlink_ext_ack * extack)2382 static int parse_nla_optional_attrs(struct nlattr **attrs,
2383 struct seg6_local_lwt *slwt,
2384 struct netlink_ext_ack *extack)
2385 {
2386 struct seg6_action_desc *desc = slwt->desc;
2387 unsigned long parsed_optattrs = 0;
2388 struct seg6_action_param *param;
2389 int err, i;
2390
2391 for (i = SEG6_LOCAL_SRH; i < SEG6_LOCAL_MAX + 1; ++i) {
2392 if (!(desc->optattrs & SEG6_F_ATTR(i)) || !attrs[i])
2393 continue;
2394
2395 /* once here, the i-th attribute is provided by the
2396 * userspace AND it is identified optional as well.
2397 */
2398 param = &seg6_action_params[i];
2399
2400 err = param->parse(attrs, slwt, extack);
2401 if (err < 0)
2402 goto parse_optattrs_err;
2403
2404 /* current attribute has been correctly parsed */
2405 parsed_optattrs |= SEG6_F_ATTR(i);
2406 }
2407
2408 /* store in the tunnel state all the optional attributed successfully
2409 * parsed.
2410 */
2411 slwt->parsed_optattrs = parsed_optattrs;
2412
2413 return 0;
2414
2415 parse_optattrs_err:
2416 __destroy_attrs(parsed_optattrs, i, slwt);
2417
2418 return err;
2419 }
2420
2421 /* call the custom constructor of the behavior during its initialization phase
2422 * and after that all its attributes have been parsed successfully.
2423 */
2424 static int
seg6_local_lwtunnel_build_state(struct seg6_local_lwt * slwt,const void * cfg,struct netlink_ext_ack * extack)2425 seg6_local_lwtunnel_build_state(struct seg6_local_lwt *slwt, const void *cfg,
2426 struct netlink_ext_ack *extack)
2427 {
2428 struct seg6_action_desc *desc = slwt->desc;
2429 struct seg6_local_lwtunnel_ops *ops;
2430
2431 ops = &desc->slwt_ops;
2432 if (!ops->build_state)
2433 return 0;
2434
2435 return ops->build_state(slwt, cfg, extack);
2436 }
2437
2438 /* call the custom destructor of the behavior which is invoked before the
2439 * tunnel is going to be destroyed.
2440 */
seg6_local_lwtunnel_destroy_state(struct seg6_local_lwt * slwt)2441 static void seg6_local_lwtunnel_destroy_state(struct seg6_local_lwt *slwt)
2442 {
2443 struct seg6_action_desc *desc = slwt->desc;
2444 struct seg6_local_lwtunnel_ops *ops;
2445
2446 ops = &desc->slwt_ops;
2447 if (!ops->destroy_state)
2448 return;
2449
2450 ops->destroy_state(slwt);
2451 }
2452
parse_nla_action(struct nlattr ** attrs,struct seg6_local_lwt * slwt,struct netlink_ext_ack * extack)2453 static int parse_nla_action(struct nlattr **attrs, struct seg6_local_lwt *slwt,
2454 struct netlink_ext_ack *extack)
2455 {
2456 struct seg6_action_param *param;
2457 struct seg6_action_desc *desc;
2458 unsigned long invalid_attrs;
2459 int i, err;
2460
2461 desc = __get_action_desc(slwt->action);
2462 if (!desc)
2463 return -EINVAL;
2464
2465 if (!desc->input)
2466 return -EOPNOTSUPP;
2467
2468 slwt->desc = desc;
2469 slwt->headroom += desc->static_headroom;
2470
2471 /* Forcing the desc->optattrs *set* and the desc->attrs *set* to be
2472 * disjoined, this allow us to release acquired resources by optional
2473 * attributes and by required attributes independently from each other
2474 * without any interference.
2475 * In other terms, we are sure that we do not release some the acquired
2476 * resources twice.
2477 *
2478 * Note that if an attribute is configured both as required and as
2479 * optional, it means that the user has messed something up in the
2480 * seg6_action_table. Therefore, this check is required for SRv6
2481 * behaviors to work properly.
2482 */
2483 invalid_attrs = desc->attrs & desc->optattrs;
2484 if (invalid_attrs) {
2485 WARN_ONCE(1,
2486 "An attribute cannot be both required AND optional");
2487 return -EINVAL;
2488 }
2489
2490 /* parse the required attributes */
2491 for (i = SEG6_LOCAL_SRH; i < SEG6_LOCAL_MAX + 1; i++) {
2492 if (desc->attrs & SEG6_F_ATTR(i)) {
2493 if (!attrs[i])
2494 return -EINVAL;
2495
2496 param = &seg6_action_params[i];
2497
2498 err = param->parse(attrs, slwt, extack);
2499 if (err < 0)
2500 goto parse_attrs_err;
2501 }
2502 }
2503
2504 /* parse the optional attributes, if any */
2505 err = parse_nla_optional_attrs(attrs, slwt, extack);
2506 if (err < 0)
2507 goto parse_attrs_err;
2508
2509 return 0;
2510
2511 parse_attrs_err:
2512 /* release any resource that may have been acquired during the i-1
2513 * parse() operations.
2514 */
2515 __destroy_attrs(desc->attrs, i, slwt);
2516
2517 return err;
2518 }
2519
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)2520 static int seg6_local_build_state(struct net *net, struct nlattr *nla,
2521 unsigned int family, const void *cfg,
2522 struct lwtunnel_state **ts,
2523 struct netlink_ext_ack *extack)
2524 {
2525 struct nlattr *tb[SEG6_LOCAL_MAX + 1];
2526 struct lwtunnel_state *newts;
2527 struct seg6_local_lwt *slwt;
2528 int err;
2529
2530 if (family != AF_INET6)
2531 return -EINVAL;
2532
2533 err = nla_parse_nested_deprecated(tb, SEG6_LOCAL_MAX, nla,
2534 seg6_local_policy, extack);
2535
2536 if (err < 0)
2537 return err;
2538
2539 if (!tb[SEG6_LOCAL_ACTION])
2540 return -EINVAL;
2541
2542 newts = lwtunnel_state_alloc(sizeof(*slwt));
2543 if (!newts)
2544 return -ENOMEM;
2545
2546 slwt = seg6_local_lwtunnel(newts);
2547 slwt->action = nla_get_u32(tb[SEG6_LOCAL_ACTION]);
2548
2549 err = parse_nla_action(tb, slwt, extack);
2550 if (err < 0)
2551 goto out_free;
2552
2553 err = seg6_local_lwtunnel_build_state(slwt, cfg, extack);
2554 if (err < 0)
2555 goto out_destroy_attrs;
2556
2557 newts->type = LWTUNNEL_ENCAP_SEG6_LOCAL;
2558 newts->flags = LWTUNNEL_STATE_INPUT_REDIRECT;
2559 newts->headroom = slwt->headroom;
2560
2561 *ts = newts;
2562
2563 return 0;
2564
2565 out_destroy_attrs:
2566 destroy_attrs(slwt);
2567 out_free:
2568 kfree(newts);
2569 return err;
2570 }
2571
seg6_local_destroy_state(struct lwtunnel_state * lwt)2572 static void seg6_local_destroy_state(struct lwtunnel_state *lwt)
2573 {
2574 struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
2575
2576 seg6_local_lwtunnel_destroy_state(slwt);
2577
2578 destroy_attrs(slwt);
2579
2580 return;
2581 }
2582
seg6_local_fill_encap(struct sk_buff * skb,struct lwtunnel_state * lwt)2583 static int seg6_local_fill_encap(struct sk_buff *skb,
2584 struct lwtunnel_state *lwt)
2585 {
2586 struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
2587 struct seg6_action_param *param;
2588 unsigned long attrs;
2589 int i, err;
2590
2591 if (nla_put_u32(skb, SEG6_LOCAL_ACTION, slwt->action))
2592 return -EMSGSIZE;
2593
2594 attrs = slwt->desc->attrs | slwt->parsed_optattrs;
2595
2596 for (i = SEG6_LOCAL_SRH; i < SEG6_LOCAL_MAX + 1; i++) {
2597 if (attrs & SEG6_F_ATTR(i)) {
2598 param = &seg6_action_params[i];
2599 err = param->put(skb, slwt);
2600 if (err < 0)
2601 return err;
2602 }
2603 }
2604
2605 return 0;
2606 }
2607
seg6_local_get_encap_size(struct lwtunnel_state * lwt)2608 static int seg6_local_get_encap_size(struct lwtunnel_state *lwt)
2609 {
2610 struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
2611 unsigned long attrs;
2612 int nlsize;
2613
2614 nlsize = nla_total_size(4); /* action */
2615
2616 attrs = slwt->desc->attrs | slwt->parsed_optattrs;
2617
2618 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_SRH))
2619 nlsize += nla_total_size((slwt->srh->hdrlen + 1) << 3);
2620
2621 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_TABLE))
2622 nlsize += nla_total_size(4);
2623
2624 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_NH4))
2625 nlsize += nla_total_size(4);
2626
2627 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_NH6))
2628 nlsize += nla_total_size(16);
2629
2630 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_IIF))
2631 nlsize += nla_total_size(4);
2632
2633 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_OIF))
2634 nlsize += nla_total_size(4);
2635
2636 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_BPF))
2637 nlsize += nla_total_size(sizeof(struct nlattr)) +
2638 nla_total_size(MAX_PROG_NAME) +
2639 nla_total_size(4);
2640
2641 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_VRFTABLE))
2642 nlsize += nla_total_size(4);
2643
2644 if (attrs & SEG6_F_LOCAL_COUNTERS)
2645 nlsize += nla_total_size(0) + /* nest SEG6_LOCAL_COUNTERS */
2646 /* SEG6_LOCAL_CNT_PACKETS */
2647 nla_total_size_64bit(sizeof(__u64)) +
2648 /* SEG6_LOCAL_CNT_BYTES */
2649 nla_total_size_64bit(sizeof(__u64)) +
2650 /* SEG6_LOCAL_CNT_ERRORS */
2651 nla_total_size_64bit(sizeof(__u64));
2652
2653 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_FLAVORS))
2654 nlsize += encap_size_flavors(slwt);
2655
2656 return nlsize;
2657 }
2658
seg6_local_cmp_encap(struct lwtunnel_state * a,struct lwtunnel_state * b)2659 static int seg6_local_cmp_encap(struct lwtunnel_state *a,
2660 struct lwtunnel_state *b)
2661 {
2662 struct seg6_local_lwt *slwt_a, *slwt_b;
2663 struct seg6_action_param *param;
2664 unsigned long attrs_a, attrs_b;
2665 int i;
2666
2667 slwt_a = seg6_local_lwtunnel(a);
2668 slwt_b = seg6_local_lwtunnel(b);
2669
2670 if (slwt_a->action != slwt_b->action)
2671 return 1;
2672
2673 attrs_a = slwt_a->desc->attrs | slwt_a->parsed_optattrs;
2674 attrs_b = slwt_b->desc->attrs | slwt_b->parsed_optattrs;
2675
2676 if (attrs_a != attrs_b)
2677 return 1;
2678
2679 for (i = SEG6_LOCAL_SRH; i < SEG6_LOCAL_MAX + 1; i++) {
2680 if (attrs_a & SEG6_F_ATTR(i)) {
2681 param = &seg6_action_params[i];
2682 if (param->cmp(slwt_a, slwt_b))
2683 return 1;
2684 }
2685 }
2686
2687 return 0;
2688 }
2689
2690 static const struct lwtunnel_encap_ops seg6_local_ops = {
2691 .build_state = seg6_local_build_state,
2692 .destroy_state = seg6_local_destroy_state,
2693 .input = seg6_local_input,
2694 .fill_encap = seg6_local_fill_encap,
2695 .get_encap_size = seg6_local_get_encap_size,
2696 .cmp_encap = seg6_local_cmp_encap,
2697 .owner = THIS_MODULE,
2698 };
2699
seg6_local_init(void)2700 int __init seg6_local_init(void)
2701 {
2702 /* If the max total number of defined attributes is reached, then your
2703 * kernel build stops here.
2704 *
2705 * This check is required to avoid arithmetic overflows when processing
2706 * behavior attributes and the maximum number of defined attributes
2707 * exceeds the allowed value.
2708 */
2709 BUILD_BUG_ON(SEG6_LOCAL_MAX + 1 > BITS_PER_TYPE(unsigned long));
2710
2711 /* Check whether the number of defined flavors exceeds the maximum
2712 * allowed value.
2713 */
2714 BUILD_BUG_ON(SEG6_LOCAL_FLV_OP_MAX + 1 > BITS_PER_TYPE(__u32));
2715
2716 /* If the default NEXT-C-SID Locator-Block/Node Function lengths (in
2717 * bits) have been changed with invalid values, kernel build stops
2718 * here.
2719 */
2720 BUILD_BUG_ON(next_csid_chk_cntr_bits(SEG6_LOCAL_LCBLOCK_DBITS,
2721 SEG6_LOCAL_LCNODE_FN_DBITS));
2722 BUILD_BUG_ON(next_csid_chk_lcblock_bits(SEG6_LOCAL_LCBLOCK_DBITS));
2723 BUILD_BUG_ON(next_csid_chk_lcnode_fn_bits(SEG6_LOCAL_LCNODE_FN_DBITS));
2724
2725 /* To be memory efficient, we use 'u8' to represent the different
2726 * actions related to RFC8986 flavors. If the kernel build stops here,
2727 * it means that it is not possible to correctly encode these actions
2728 * with the data type chosen for the action table.
2729 */
2730 BUILD_BUG_ON(SEG6_LOCAL_FLV_ACT_MAX > (typeof(flv8986_act_tbl[0]))~0U);
2731
2732 return lwtunnel_encap_add_ops(&seg6_local_ops,
2733 LWTUNNEL_ENCAP_SEG6_LOCAL);
2734 }
2735
seg6_local_exit(void)2736 void seg6_local_exit(void)
2737 {
2738 lwtunnel_encap_del_ops(&seg6_local_ops, LWTUNNEL_ENCAP_SEG6_LOCAL);
2739 }
2740