]>
Commit | Line | Data |
---|---|---|
d1df6fd8 DL |
1 | /* |
2 | * SR-IPv6 implementation | |
3 | * | |
4 | * Author: | |
5 | * David Lebrun <david.lebrun@uclouvain.be> | |
6 | * | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU General Public License | |
10 | * as published by the Free Software Foundation; either version | |
11 | * 2 of the License, or (at your option) any later version. | |
12 | */ | |
13 | ||
14 | #include <linux/types.h> | |
15 | #include <linux/skbuff.h> | |
16 | #include <linux/net.h> | |
17 | #include <linux/module.h> | |
18 | #include <net/ip.h> | |
19 | #include <net/lwtunnel.h> | |
20 | #include <net/netevent.h> | |
21 | #include <net/netns/generic.h> | |
22 | #include <net/ip6_fib.h> | |
23 | #include <net/route.h> | |
24 | #include <net/seg6.h> | |
25 | #include <linux/seg6.h> | |
26 | #include <linux/seg6_local.h> | |
27 | #include <net/addrconf.h> | |
28 | #include <net/ip6_route.h> | |
29 | #include <net/dst_cache.h> | |
30 | #ifdef CONFIG_IPV6_SEG6_HMAC | |
31 | #include <net/seg6_hmac.h> | |
32 | #endif | |
33 | ||
34 | struct seg6_local_lwt; | |
35 | ||
36 | struct seg6_action_desc { | |
37 | int action; | |
38 | unsigned long attrs; | |
39 | int (*input)(struct sk_buff *skb, struct seg6_local_lwt *slwt); | |
40 | int static_headroom; | |
41 | }; | |
42 | ||
43 | struct seg6_local_lwt { | |
44 | int action; | |
45 | struct ipv6_sr_hdr *srh; | |
46 | int table; | |
47 | struct in_addr nh4; | |
48 | struct in6_addr nh6; | |
49 | int iif; | |
50 | int oif; | |
51 | ||
52 | int headroom; | |
53 | struct seg6_action_desc *desc; | |
54 | }; | |
55 | ||
56 | static struct seg6_local_lwt *seg6_local_lwtunnel(struct lwtunnel_state *lwt) | |
57 | { | |
58 | return (struct seg6_local_lwt *)lwt->data; | |
59 | } | |
60 | ||
140f04c3 DL |
61 | static struct ipv6_sr_hdr *get_srh(struct sk_buff *skb) |
62 | { | |
63 | struct ipv6_sr_hdr *srh; | |
64 | struct ipv6hdr *hdr; | |
65 | int len; | |
66 | ||
67 | hdr = ipv6_hdr(skb); | |
68 | if (hdr->nexthdr != IPPROTO_ROUTING) | |
69 | return NULL; | |
70 | ||
71 | srh = (struct ipv6_sr_hdr *)(hdr + 1); | |
72 | len = (srh->hdrlen + 1) << 3; | |
73 | ||
74 | if (!pskb_may_pull(skb, sizeof(*hdr) + len)) | |
75 | return NULL; | |
76 | ||
77 | if (!seg6_validate_srh(srh, len)) | |
78 | return NULL; | |
79 | ||
80 | return srh; | |
81 | } | |
82 | ||
83 | static struct ipv6_sr_hdr *get_and_validate_srh(struct sk_buff *skb) | |
84 | { | |
85 | struct ipv6_sr_hdr *srh; | |
86 | ||
87 | srh = get_srh(skb); | |
88 | if (!srh) | |
89 | return NULL; | |
90 | ||
91 | if (srh->segments_left == 0) | |
92 | return NULL; | |
93 | ||
94 | #ifdef CONFIG_IPV6_SEG6_HMAC | |
95 | if (!seg6_hmac_validate_skb(skb)) | |
96 | return NULL; | |
97 | #endif | |
98 | ||
99 | return srh; | |
100 | } | |
101 | ||
d7a669dd DL |
102 | static bool decap_and_validate(struct sk_buff *skb, int proto) |
103 | { | |
104 | struct ipv6_sr_hdr *srh; | |
105 | unsigned int off = 0; | |
106 | ||
107 | srh = get_srh(skb); | |
108 | if (srh && srh->segments_left > 0) | |
109 | return false; | |
110 | ||
111 | #ifdef CONFIG_IPV6_SEG6_HMAC | |
112 | if (srh && !seg6_hmac_validate_skb(skb)) | |
113 | return false; | |
114 | #endif | |
115 | ||
116 | if (ipv6_find_hdr(skb, &off, proto, NULL, NULL) < 0) | |
117 | return false; | |
118 | ||
119 | if (!pskb_pull(skb, off)) | |
120 | return false; | |
121 | ||
122 | skb_postpull_rcsum(skb, skb_network_header(skb), off); | |
123 | ||
124 | skb_reset_network_header(skb); | |
125 | skb_reset_transport_header(skb); | |
126 | skb->encapsulation = 0; | |
127 | ||
128 | return true; | |
129 | } | |
130 | ||
131 | static void advance_nextseg(struct ipv6_sr_hdr *srh, struct in6_addr *daddr) | |
132 | { | |
133 | struct in6_addr *addr; | |
134 | ||
135 | srh->segments_left--; | |
136 | addr = srh->segments + srh->segments_left; | |
137 | *daddr = *addr; | |
138 | } | |
139 | ||
140 | static void lookup_nexthop(struct sk_buff *skb, struct in6_addr *nhaddr, | |
141 | u32 tbl_id) | |
142 | { | |
143 | struct net *net = dev_net(skb->dev); | |
144 | struct ipv6hdr *hdr = ipv6_hdr(skb); | |
145 | int flags = RT6_LOOKUP_F_HAS_SADDR; | |
146 | struct dst_entry *dst = NULL; | |
147 | struct rt6_info *rt; | |
148 | struct flowi6 fl6; | |
149 | ||
150 | fl6.flowi6_iif = skb->dev->ifindex; | |
151 | fl6.daddr = nhaddr ? *nhaddr : hdr->daddr; | |
152 | fl6.saddr = hdr->saddr; | |
153 | fl6.flowlabel = ip6_flowinfo(hdr); | |
154 | fl6.flowi6_mark = skb->mark; | |
155 | fl6.flowi6_proto = hdr->nexthdr; | |
156 | ||
157 | if (nhaddr) | |
158 | fl6.flowi6_flags = FLOWI_FLAG_KNOWN_NH; | |
159 | ||
160 | if (!tbl_id) { | |
161 | dst = ip6_route_input_lookup(net, skb->dev, &fl6, flags); | |
162 | } else { | |
163 | struct fib6_table *table; | |
164 | ||
165 | table = fib6_get_table(net, tbl_id); | |
166 | if (!table) | |
167 | goto out; | |
168 | ||
169 | rt = ip6_pol_route(net, table, 0, &fl6, flags); | |
170 | dst = &rt->dst; | |
171 | } | |
172 | ||
173 | if (dst && dst->dev->flags & IFF_LOOPBACK && !dst->error) { | |
174 | dst_release(dst); | |
175 | dst = NULL; | |
176 | } | |
177 | ||
178 | out: | |
179 | if (!dst) { | |
180 | rt = net->ipv6.ip6_blk_hole_entry; | |
181 | dst = &rt->dst; | |
182 | dst_hold(dst); | |
183 | } | |
184 | ||
185 | skb_dst_drop(skb); | |
186 | skb_dst_set(skb, dst); | |
187 | } | |
188 | ||
140f04c3 DL |
189 | /* regular endpoint function */ |
190 | static int input_action_end(struct sk_buff *skb, struct seg6_local_lwt *slwt) | |
191 | { | |
192 | struct ipv6_sr_hdr *srh; | |
140f04c3 DL |
193 | |
194 | srh = get_and_validate_srh(skb); | |
195 | if (!srh) | |
196 | goto drop; | |
197 | ||
d7a669dd | 198 | advance_nextseg(srh, &ipv6_hdr(skb)->daddr); |
140f04c3 | 199 | |
d7a669dd | 200 | lookup_nexthop(skb, NULL, 0); |
140f04c3 DL |
201 | |
202 | return dst_input(skb); | |
203 | ||
204 | drop: | |
205 | kfree_skb(skb); | |
206 | return -EINVAL; | |
207 | } | |
208 | ||
209 | /* regular endpoint, and forward to specified nexthop */ | |
210 | static int input_action_end_x(struct sk_buff *skb, struct seg6_local_lwt *slwt) | |
211 | { | |
140f04c3 | 212 | struct ipv6_sr_hdr *srh; |
140f04c3 DL |
213 | |
214 | srh = get_and_validate_srh(skb); | |
215 | if (!srh) | |
216 | goto drop; | |
217 | ||
d7a669dd | 218 | advance_nextseg(srh, &ipv6_hdr(skb)->daddr); |
140f04c3 | 219 | |
d7a669dd | 220 | lookup_nexthop(skb, &slwt->nh6, 0); |
140f04c3 DL |
221 | |
222 | return dst_input(skb); | |
223 | ||
224 | drop: | |
225 | kfree_skb(skb); | |
226 | return -EINVAL; | |
227 | } | |
228 | ||
229 | /* decapsulate and forward to specified nexthop */ | |
230 | static int input_action_end_dx6(struct sk_buff *skb, | |
231 | struct seg6_local_lwt *slwt) | |
232 | { | |
d7a669dd | 233 | struct in6_addr *nhaddr = NULL; |
140f04c3 DL |
234 | |
235 | /* this function accepts IPv6 encapsulated packets, with either | |
236 | * an SRH with SL=0, or no SRH. | |
237 | */ | |
238 | ||
d7a669dd | 239 | if (!decap_and_validate(skb, IPPROTO_IPV6)) |
140f04c3 | 240 | goto drop; |
140f04c3 | 241 | |
d7a669dd | 242 | if (!pskb_may_pull(skb, sizeof(struct ipv6hdr))) |
140f04c3 DL |
243 | goto drop; |
244 | ||
140f04c3 DL |
245 | /* The inner packet is not associated to any local interface, |
246 | * so we do not call netif_rx(). | |
247 | * | |
248 | * If slwt->nh6 is set to ::, then lookup the nexthop for the | |
249 | * inner packet's DA. Otherwise, use the specified nexthop. | |
250 | */ | |
251 | ||
d7a669dd DL |
252 | if (!ipv6_addr_any(&slwt->nh6)) |
253 | nhaddr = &slwt->nh6; | |
140f04c3 | 254 | |
d7a669dd | 255 | lookup_nexthop(skb, nhaddr, 0); |
140f04c3 DL |
256 | |
257 | return dst_input(skb); | |
258 | drop: | |
259 | kfree_skb(skb); | |
260 | return -EINVAL; | |
261 | } | |
262 | ||
263 | /* push an SRH on top of the current one */ | |
264 | static int input_action_end_b6(struct sk_buff *skb, struct seg6_local_lwt *slwt) | |
265 | { | |
266 | struct ipv6_sr_hdr *srh; | |
267 | int err = -EINVAL; | |
268 | ||
269 | srh = get_and_validate_srh(skb); | |
270 | if (!srh) | |
271 | goto drop; | |
272 | ||
273 | err = seg6_do_srh_inline(skb, slwt->srh); | |
274 | if (err) | |
275 | goto drop; | |
276 | ||
277 | ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr)); | |
278 | skb_set_transport_header(skb, sizeof(struct ipv6hdr)); | |
279 | ||
d7a669dd | 280 | lookup_nexthop(skb, NULL, 0); |
140f04c3 DL |
281 | |
282 | return dst_input(skb); | |
283 | ||
284 | drop: | |
285 | kfree_skb(skb); | |
286 | return err; | |
287 | } | |
288 | ||
289 | /* encapsulate within an outer IPv6 header and a specified SRH */ | |
290 | static int input_action_end_b6_encap(struct sk_buff *skb, | |
291 | struct seg6_local_lwt *slwt) | |
292 | { | |
293 | struct ipv6_sr_hdr *srh; | |
140f04c3 DL |
294 | int err = -EINVAL; |
295 | ||
296 | srh = get_and_validate_srh(skb); | |
297 | if (!srh) | |
298 | goto drop; | |
299 | ||
d7a669dd | 300 | advance_nextseg(srh, &ipv6_hdr(skb)->daddr); |
140f04c3 DL |
301 | |
302 | skb_reset_inner_headers(skb); | |
303 | skb->encapsulation = 1; | |
304 | ||
32d99d0b | 305 | err = seg6_do_srh_encap(skb, slwt->srh, IPPROTO_IPV6); |
140f04c3 DL |
306 | if (err) |
307 | goto drop; | |
308 | ||
309 | ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr)); | |
310 | skb_set_transport_header(skb, sizeof(struct ipv6hdr)); | |
311 | ||
d7a669dd | 312 | lookup_nexthop(skb, NULL, 0); |
140f04c3 DL |
313 | |
314 | return dst_input(skb); | |
315 | ||
316 | drop: | |
317 | kfree_skb(skb); | |
318 | return err; | |
319 | } | |
320 | ||
d1df6fd8 DL |
321 | static struct seg6_action_desc seg6_action_table[] = { |
322 | { | |
323 | .action = SEG6_LOCAL_ACTION_END, | |
324 | .attrs = 0, | |
140f04c3 DL |
325 | .input = input_action_end, |
326 | }, | |
327 | { | |
328 | .action = SEG6_LOCAL_ACTION_END_X, | |
329 | .attrs = (1 << SEG6_LOCAL_NH6), | |
330 | .input = input_action_end_x, | |
d1df6fd8 | 331 | }, |
140f04c3 DL |
332 | { |
333 | .action = SEG6_LOCAL_ACTION_END_DX6, | |
334 | .attrs = (1 << SEG6_LOCAL_NH6), | |
335 | .input = input_action_end_dx6, | |
336 | }, | |
337 | { | |
338 | .action = SEG6_LOCAL_ACTION_END_B6, | |
339 | .attrs = (1 << SEG6_LOCAL_SRH), | |
340 | .input = input_action_end_b6, | |
341 | }, | |
342 | { | |
343 | .action = SEG6_LOCAL_ACTION_END_B6_ENCAP, | |
344 | .attrs = (1 << SEG6_LOCAL_SRH), | |
345 | .input = input_action_end_b6_encap, | |
346 | .static_headroom = sizeof(struct ipv6hdr), | |
347 | } | |
d1df6fd8 DL |
348 | }; |
349 | ||
350 | static struct seg6_action_desc *__get_action_desc(int action) | |
351 | { | |
352 | struct seg6_action_desc *desc; | |
353 | int i, count; | |
354 | ||
355 | count = sizeof(seg6_action_table) / sizeof(struct seg6_action_desc); | |
356 | for (i = 0; i < count; i++) { | |
357 | desc = &seg6_action_table[i]; | |
358 | if (desc->action == action) | |
359 | return desc; | |
360 | } | |
361 | ||
362 | return NULL; | |
363 | } | |
364 | ||
365 | static int seg6_local_input(struct sk_buff *skb) | |
366 | { | |
367 | struct dst_entry *orig_dst = skb_dst(skb); | |
368 | struct seg6_action_desc *desc; | |
369 | struct seg6_local_lwt *slwt; | |
370 | ||
6285217f DL |
371 | if (skb->protocol != htons(ETH_P_IPV6)) { |
372 | kfree_skb(skb); | |
373 | return -EINVAL; | |
374 | } | |
375 | ||
d1df6fd8 DL |
376 | slwt = seg6_local_lwtunnel(orig_dst->lwtstate); |
377 | desc = slwt->desc; | |
378 | ||
379 | return desc->input(skb, slwt); | |
380 | } | |
381 | ||
382 | static const struct nla_policy seg6_local_policy[SEG6_LOCAL_MAX + 1] = { | |
383 | [SEG6_LOCAL_ACTION] = { .type = NLA_U32 }, | |
384 | [SEG6_LOCAL_SRH] = { .type = NLA_BINARY }, | |
385 | [SEG6_LOCAL_TABLE] = { .type = NLA_U32 }, | |
386 | [SEG6_LOCAL_NH4] = { .type = NLA_BINARY, | |
387 | .len = sizeof(struct in_addr) }, | |
388 | [SEG6_LOCAL_NH6] = { .type = NLA_BINARY, | |
389 | .len = sizeof(struct in6_addr) }, | |
390 | [SEG6_LOCAL_IIF] = { .type = NLA_U32 }, | |
391 | [SEG6_LOCAL_OIF] = { .type = NLA_U32 }, | |
392 | }; | |
393 | ||
2d9cc60a DL |
394 | static int parse_nla_srh(struct nlattr **attrs, struct seg6_local_lwt *slwt) |
395 | { | |
396 | struct ipv6_sr_hdr *srh; | |
397 | int len; | |
398 | ||
399 | srh = nla_data(attrs[SEG6_LOCAL_SRH]); | |
400 | len = nla_len(attrs[SEG6_LOCAL_SRH]); | |
401 | ||
402 | /* SRH must contain at least one segment */ | |
403 | if (len < sizeof(*srh) + sizeof(struct in6_addr)) | |
404 | return -EINVAL; | |
405 | ||
406 | if (!seg6_validate_srh(srh, len)) | |
407 | return -EINVAL; | |
408 | ||
409 | slwt->srh = kmalloc(len, GFP_KERNEL); | |
410 | if (!slwt->srh) | |
411 | return -ENOMEM; | |
412 | ||
413 | memcpy(slwt->srh, srh, len); | |
414 | ||
415 | slwt->headroom += len; | |
416 | ||
417 | return 0; | |
418 | } | |
419 | ||
420 | static int put_nla_srh(struct sk_buff *skb, struct seg6_local_lwt *slwt) | |
421 | { | |
422 | struct ipv6_sr_hdr *srh; | |
423 | struct nlattr *nla; | |
424 | int len; | |
425 | ||
426 | srh = slwt->srh; | |
427 | len = (srh->hdrlen + 1) << 3; | |
428 | ||
429 | nla = nla_reserve(skb, SEG6_LOCAL_SRH, len); | |
430 | if (!nla) | |
431 | return -EMSGSIZE; | |
432 | ||
433 | memcpy(nla_data(nla), srh, len); | |
434 | ||
435 | return 0; | |
436 | } | |
437 | ||
438 | static int cmp_nla_srh(struct seg6_local_lwt *a, struct seg6_local_lwt *b) | |
439 | { | |
440 | int len = (a->srh->hdrlen + 1) << 3; | |
441 | ||
442 | if (len != ((b->srh->hdrlen + 1) << 3)) | |
443 | return 1; | |
444 | ||
445 | return memcmp(a->srh, b->srh, len); | |
446 | } | |
447 | ||
448 | static int parse_nla_table(struct nlattr **attrs, struct seg6_local_lwt *slwt) | |
449 | { | |
450 | slwt->table = nla_get_u32(attrs[SEG6_LOCAL_TABLE]); | |
451 | ||
452 | return 0; | |
453 | } | |
454 | ||
455 | static int put_nla_table(struct sk_buff *skb, struct seg6_local_lwt *slwt) | |
456 | { | |
457 | if (nla_put_u32(skb, SEG6_LOCAL_TABLE, slwt->table)) | |
458 | return -EMSGSIZE; | |
459 | ||
460 | return 0; | |
461 | } | |
462 | ||
463 | static int cmp_nla_table(struct seg6_local_lwt *a, struct seg6_local_lwt *b) | |
464 | { | |
465 | if (a->table != b->table) | |
466 | return 1; | |
467 | ||
468 | return 0; | |
469 | } | |
470 | ||
471 | static int parse_nla_nh4(struct nlattr **attrs, struct seg6_local_lwt *slwt) | |
472 | { | |
473 | memcpy(&slwt->nh4, nla_data(attrs[SEG6_LOCAL_NH4]), | |
474 | sizeof(struct in_addr)); | |
475 | ||
476 | return 0; | |
477 | } | |
478 | ||
479 | static int put_nla_nh4(struct sk_buff *skb, struct seg6_local_lwt *slwt) | |
480 | { | |
481 | struct nlattr *nla; | |
482 | ||
483 | nla = nla_reserve(skb, SEG6_LOCAL_NH4, sizeof(struct in_addr)); | |
484 | if (!nla) | |
485 | return -EMSGSIZE; | |
486 | ||
487 | memcpy(nla_data(nla), &slwt->nh4, sizeof(struct in_addr)); | |
488 | ||
489 | return 0; | |
490 | } | |
491 | ||
492 | static int cmp_nla_nh4(struct seg6_local_lwt *a, struct seg6_local_lwt *b) | |
493 | { | |
494 | return memcmp(&a->nh4, &b->nh4, sizeof(struct in_addr)); | |
495 | } | |
496 | ||
497 | static int parse_nla_nh6(struct nlattr **attrs, struct seg6_local_lwt *slwt) | |
498 | { | |
499 | memcpy(&slwt->nh6, nla_data(attrs[SEG6_LOCAL_NH6]), | |
500 | sizeof(struct in6_addr)); | |
501 | ||
502 | return 0; | |
503 | } | |
504 | ||
505 | static int put_nla_nh6(struct sk_buff *skb, struct seg6_local_lwt *slwt) | |
506 | { | |
507 | struct nlattr *nla; | |
508 | ||
509 | nla = nla_reserve(skb, SEG6_LOCAL_NH6, sizeof(struct in6_addr)); | |
510 | if (!nla) | |
511 | return -EMSGSIZE; | |
512 | ||
513 | memcpy(nla_data(nla), &slwt->nh6, sizeof(struct in6_addr)); | |
514 | ||
515 | return 0; | |
516 | } | |
517 | ||
518 | static int cmp_nla_nh6(struct seg6_local_lwt *a, struct seg6_local_lwt *b) | |
519 | { | |
520 | return memcmp(&a->nh6, &b->nh6, sizeof(struct in6_addr)); | |
521 | } | |
522 | ||
523 | static int parse_nla_iif(struct nlattr **attrs, struct seg6_local_lwt *slwt) | |
524 | { | |
525 | slwt->iif = nla_get_u32(attrs[SEG6_LOCAL_IIF]); | |
526 | ||
527 | return 0; | |
528 | } | |
529 | ||
530 | static int put_nla_iif(struct sk_buff *skb, struct seg6_local_lwt *slwt) | |
531 | { | |
532 | if (nla_put_u32(skb, SEG6_LOCAL_IIF, slwt->iif)) | |
533 | return -EMSGSIZE; | |
534 | ||
535 | return 0; | |
536 | } | |
537 | ||
538 | static int cmp_nla_iif(struct seg6_local_lwt *a, struct seg6_local_lwt *b) | |
539 | { | |
540 | if (a->iif != b->iif) | |
541 | return 1; | |
542 | ||
543 | return 0; | |
544 | } | |
545 | ||
546 | static int parse_nla_oif(struct nlattr **attrs, struct seg6_local_lwt *slwt) | |
547 | { | |
548 | slwt->oif = nla_get_u32(attrs[SEG6_LOCAL_OIF]); | |
549 | ||
550 | return 0; | |
551 | } | |
552 | ||
553 | static int put_nla_oif(struct sk_buff *skb, struct seg6_local_lwt *slwt) | |
554 | { | |
555 | if (nla_put_u32(skb, SEG6_LOCAL_OIF, slwt->oif)) | |
556 | return -EMSGSIZE; | |
557 | ||
558 | return 0; | |
559 | } | |
560 | ||
561 | static int cmp_nla_oif(struct seg6_local_lwt *a, struct seg6_local_lwt *b) | |
562 | { | |
563 | if (a->oif != b->oif) | |
564 | return 1; | |
565 | ||
566 | return 0; | |
567 | } | |
568 | ||
d1df6fd8 DL |
569 | struct seg6_action_param { |
570 | int (*parse)(struct nlattr **attrs, struct seg6_local_lwt *slwt); | |
571 | int (*put)(struct sk_buff *skb, struct seg6_local_lwt *slwt); | |
572 | int (*cmp)(struct seg6_local_lwt *a, struct seg6_local_lwt *b); | |
573 | }; | |
574 | ||
575 | static struct seg6_action_param seg6_action_params[SEG6_LOCAL_MAX + 1] = { | |
2d9cc60a DL |
576 | [SEG6_LOCAL_SRH] = { .parse = parse_nla_srh, |
577 | .put = put_nla_srh, | |
578 | .cmp = cmp_nla_srh }, | |
d1df6fd8 | 579 | |
2d9cc60a DL |
580 | [SEG6_LOCAL_TABLE] = { .parse = parse_nla_table, |
581 | .put = put_nla_table, | |
582 | .cmp = cmp_nla_table }, | |
d1df6fd8 | 583 | |
2d9cc60a DL |
584 | [SEG6_LOCAL_NH4] = { .parse = parse_nla_nh4, |
585 | .put = put_nla_nh4, | |
586 | .cmp = cmp_nla_nh4 }, | |
d1df6fd8 | 587 | |
2d9cc60a DL |
588 | [SEG6_LOCAL_NH6] = { .parse = parse_nla_nh6, |
589 | .put = put_nla_nh6, | |
590 | .cmp = cmp_nla_nh6 }, | |
d1df6fd8 | 591 | |
2d9cc60a DL |
592 | [SEG6_LOCAL_IIF] = { .parse = parse_nla_iif, |
593 | .put = put_nla_iif, | |
594 | .cmp = cmp_nla_iif }, | |
d1df6fd8 | 595 | |
2d9cc60a DL |
596 | [SEG6_LOCAL_OIF] = { .parse = parse_nla_oif, |
597 | .put = put_nla_oif, | |
598 | .cmp = cmp_nla_oif }, | |
d1df6fd8 DL |
599 | }; |
600 | ||
601 | static int parse_nla_action(struct nlattr **attrs, struct seg6_local_lwt *slwt) | |
602 | { | |
603 | struct seg6_action_param *param; | |
604 | struct seg6_action_desc *desc; | |
605 | int i, err; | |
606 | ||
607 | desc = __get_action_desc(slwt->action); | |
608 | if (!desc) | |
609 | return -EINVAL; | |
610 | ||
611 | if (!desc->input) | |
612 | return -EOPNOTSUPP; | |
613 | ||
614 | slwt->desc = desc; | |
615 | slwt->headroom += desc->static_headroom; | |
616 | ||
617 | for (i = 0; i < SEG6_LOCAL_MAX + 1; i++) { | |
618 | if (desc->attrs & (1 << i)) { | |
619 | if (!attrs[i]) | |
620 | return -EINVAL; | |
621 | ||
622 | param = &seg6_action_params[i]; | |
623 | ||
624 | err = param->parse(attrs, slwt); | |
625 | if (err < 0) | |
626 | return err; | |
627 | } | |
628 | } | |
629 | ||
630 | return 0; | |
631 | } | |
632 | ||
633 | static int seg6_local_build_state(struct nlattr *nla, unsigned int family, | |
634 | const void *cfg, struct lwtunnel_state **ts, | |
635 | struct netlink_ext_ack *extack) | |
636 | { | |
637 | struct nlattr *tb[SEG6_LOCAL_MAX + 1]; | |
638 | struct lwtunnel_state *newts; | |
639 | struct seg6_local_lwt *slwt; | |
640 | int err; | |
641 | ||
6285217f DL |
642 | if (family != AF_INET6) |
643 | return -EINVAL; | |
644 | ||
d1df6fd8 DL |
645 | err = nla_parse_nested(tb, SEG6_LOCAL_MAX, nla, seg6_local_policy, |
646 | extack); | |
647 | ||
648 | if (err < 0) | |
649 | return err; | |
650 | ||
651 | if (!tb[SEG6_LOCAL_ACTION]) | |
652 | return -EINVAL; | |
653 | ||
654 | newts = lwtunnel_state_alloc(sizeof(*slwt)); | |
655 | if (!newts) | |
656 | return -ENOMEM; | |
657 | ||
658 | slwt = seg6_local_lwtunnel(newts); | |
659 | slwt->action = nla_get_u32(tb[SEG6_LOCAL_ACTION]); | |
660 | ||
661 | err = parse_nla_action(tb, slwt); | |
662 | if (err < 0) | |
663 | goto out_free; | |
664 | ||
665 | newts->type = LWTUNNEL_ENCAP_SEG6_LOCAL; | |
666 | newts->flags = LWTUNNEL_STATE_INPUT_REDIRECT; | |
667 | newts->headroom = slwt->headroom; | |
668 | ||
669 | *ts = newts; | |
670 | ||
671 | return 0; | |
672 | ||
673 | out_free: | |
674 | kfree(slwt->srh); | |
675 | kfree(newts); | |
676 | return err; | |
677 | } | |
678 | ||
679 | static void seg6_local_destroy_state(struct lwtunnel_state *lwt) | |
680 | { | |
681 | struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt); | |
682 | ||
683 | kfree(slwt->srh); | |
684 | } | |
685 | ||
686 | static int seg6_local_fill_encap(struct sk_buff *skb, | |
687 | struct lwtunnel_state *lwt) | |
688 | { | |
689 | struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt); | |
690 | struct seg6_action_param *param; | |
691 | int i, err; | |
692 | ||
693 | if (nla_put_u32(skb, SEG6_LOCAL_ACTION, slwt->action)) | |
694 | return -EMSGSIZE; | |
695 | ||
696 | for (i = 0; i < SEG6_LOCAL_MAX + 1; i++) { | |
697 | if (slwt->desc->attrs & (1 << i)) { | |
698 | param = &seg6_action_params[i]; | |
699 | err = param->put(skb, slwt); | |
700 | if (err < 0) | |
701 | return err; | |
702 | } | |
703 | } | |
704 | ||
705 | return 0; | |
706 | } | |
707 | ||
708 | static int seg6_local_get_encap_size(struct lwtunnel_state *lwt) | |
709 | { | |
710 | struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt); | |
711 | unsigned long attrs; | |
712 | int nlsize; | |
713 | ||
714 | nlsize = nla_total_size(4); /* action */ | |
715 | ||
716 | attrs = slwt->desc->attrs; | |
717 | ||
718 | if (attrs & (1 << SEG6_LOCAL_SRH)) | |
719 | nlsize += nla_total_size((slwt->srh->hdrlen + 1) << 3); | |
720 | ||
721 | if (attrs & (1 << SEG6_LOCAL_TABLE)) | |
722 | nlsize += nla_total_size(4); | |
723 | ||
724 | if (attrs & (1 << SEG6_LOCAL_NH4)) | |
725 | nlsize += nla_total_size(4); | |
726 | ||
727 | if (attrs & (1 << SEG6_LOCAL_NH6)) | |
728 | nlsize += nla_total_size(16); | |
729 | ||
730 | if (attrs & (1 << SEG6_LOCAL_IIF)) | |
731 | nlsize += nla_total_size(4); | |
732 | ||
733 | if (attrs & (1 << SEG6_LOCAL_OIF)) | |
734 | nlsize += nla_total_size(4); | |
735 | ||
736 | return nlsize; | |
737 | } | |
738 | ||
739 | static int seg6_local_cmp_encap(struct lwtunnel_state *a, | |
740 | struct lwtunnel_state *b) | |
741 | { | |
742 | struct seg6_local_lwt *slwt_a, *slwt_b; | |
743 | struct seg6_action_param *param; | |
744 | int i; | |
745 | ||
746 | slwt_a = seg6_local_lwtunnel(a); | |
747 | slwt_b = seg6_local_lwtunnel(b); | |
748 | ||
749 | if (slwt_a->action != slwt_b->action) | |
750 | return 1; | |
751 | ||
752 | if (slwt_a->desc->attrs != slwt_b->desc->attrs) | |
753 | return 1; | |
754 | ||
755 | for (i = 0; i < SEG6_LOCAL_MAX + 1; i++) { | |
756 | if (slwt_a->desc->attrs & (1 << i)) { | |
757 | param = &seg6_action_params[i]; | |
758 | if (param->cmp(slwt_a, slwt_b)) | |
759 | return 1; | |
760 | } | |
761 | } | |
762 | ||
763 | return 0; | |
764 | } | |
765 | ||
766 | static const struct lwtunnel_encap_ops seg6_local_ops = { | |
767 | .build_state = seg6_local_build_state, | |
768 | .destroy_state = seg6_local_destroy_state, | |
769 | .input = seg6_local_input, | |
770 | .fill_encap = seg6_local_fill_encap, | |
771 | .get_encap_size = seg6_local_get_encap_size, | |
772 | .cmp_encap = seg6_local_cmp_encap, | |
773 | .owner = THIS_MODULE, | |
774 | }; | |
775 | ||
776 | int __init seg6_local_init(void) | |
777 | { | |
778 | return lwtunnel_encap_add_ops(&seg6_local_ops, | |
779 | LWTUNNEL_ENCAP_SEG6_LOCAL); | |
780 | } | |
781 | ||
782 | void seg6_local_exit(void) | |
783 | { | |
784 | lwtunnel_encap_del_ops(&seg6_local_ops, LWTUNNEL_ENCAP_SEG6_LOCAL); | |
785 | } |