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