blob: e9b23fb924adb99aa008728fd71d95df4c21b92d [file] [log] [blame]
David Lebrund1df6fd2017-08-05 12:38:26 +02001/*
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
Mathieu Xhonneux1c1e7612018-05-20 14:58:13 +010033#include <net/seg6_local.h>
David Lebrun891ef8d2017-08-25 09:58:17 +020034#include <linux/etherdevice.h>
David Lebrund1df6fd2017-08-05 12:38:26 +020035
36struct seg6_local_lwt;
37
38struct seg6_action_desc {
39 int action;
40 unsigned long attrs;
41 int (*input)(struct sk_buff *skb, struct seg6_local_lwt *slwt);
42 int static_headroom;
43};
44
45struct seg6_local_lwt {
46 int action;
47 struct ipv6_sr_hdr *srh;
48 int table;
49 struct in_addr nh4;
50 struct in6_addr nh6;
51 int iif;
52 int oif;
53
54 int headroom;
55 struct seg6_action_desc *desc;
56};
57
58static struct seg6_local_lwt *seg6_local_lwtunnel(struct lwtunnel_state *lwt)
59{
60 return (struct seg6_local_lwt *)lwt->data;
61}
62
David Lebrun140f04c2017-08-05 12:39:48 +020063static struct ipv6_sr_hdr *get_srh(struct sk_buff *skb)
64{
65 struct ipv6_sr_hdr *srh;
Ahmed Abdelsalam5829d702017-08-30 10:50:37 +020066 int len, srhoff = 0;
David Lebrun140f04c2017-08-05 12:39:48 +020067
Ahmed Abdelsalam5829d702017-08-30 10:50:37 +020068 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
David Lebrun140f04c2017-08-05 12:39:48 +020069 return NULL;
70
Ahmed Abdelsalam5829d702017-08-30 10:50:37 +020071 if (!pskb_may_pull(skb, srhoff + sizeof(*srh)))
72 return NULL;
73
74 srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
75
David Lebrun140f04c2017-08-05 12:39:48 +020076 len = (srh->hdrlen + 1) << 3;
77
Ahmed Abdelsalam5829d702017-08-30 10:50:37 +020078 if (!pskb_may_pull(skb, srhoff + len))
David Lebrun140f04c2017-08-05 12:39:48 +020079 return NULL;
80
81 if (!seg6_validate_srh(srh, len))
82 return NULL;
83
84 return srh;
85}
86
87static struct ipv6_sr_hdr *get_and_validate_srh(struct sk_buff *skb)
88{
89 struct ipv6_sr_hdr *srh;
90
91 srh = get_srh(skb);
92 if (!srh)
93 return NULL;
94
95 if (srh->segments_left == 0)
96 return NULL;
97
98#ifdef CONFIG_IPV6_SEG6_HMAC
99 if (!seg6_hmac_validate_skb(skb))
100 return NULL;
101#endif
102
103 return srh;
104}
105
David Lebrund7a669d2017-08-25 09:56:47 +0200106static bool decap_and_validate(struct sk_buff *skb, int proto)
107{
108 struct ipv6_sr_hdr *srh;
109 unsigned int off = 0;
110
111 srh = get_srh(skb);
112 if (srh && srh->segments_left > 0)
113 return false;
114
115#ifdef CONFIG_IPV6_SEG6_HMAC
116 if (srh && !seg6_hmac_validate_skb(skb))
117 return false;
118#endif
119
120 if (ipv6_find_hdr(skb, &off, proto, NULL, NULL) < 0)
121 return false;
122
123 if (!pskb_pull(skb, off))
124 return false;
125
126 skb_postpull_rcsum(skb, skb_network_header(skb), off);
127
128 skb_reset_network_header(skb);
129 skb_reset_transport_header(skb);
130 skb->encapsulation = 0;
131
132 return true;
133}
134
135static void advance_nextseg(struct ipv6_sr_hdr *srh, struct in6_addr *daddr)
136{
137 struct in6_addr *addr;
138
139 srh->segments_left--;
140 addr = srh->segments + srh->segments_left;
141 *daddr = *addr;
142}
143
Mathieu Xhonneux1c1e7612018-05-20 14:58:13 +0100144int seg6_lookup_nexthop(struct sk_buff *skb, struct in6_addr *nhaddr,
145 u32 tbl_id)
David Lebrund7a669d2017-08-25 09:56:47 +0200146{
147 struct net *net = dev_net(skb->dev);
148 struct ipv6hdr *hdr = ipv6_hdr(skb);
149 int flags = RT6_LOOKUP_F_HAS_SADDR;
150 struct dst_entry *dst = NULL;
151 struct rt6_info *rt;
152 struct flowi6 fl6;
153
154 fl6.flowi6_iif = skb->dev->ifindex;
155 fl6.daddr = nhaddr ? *nhaddr : hdr->daddr;
156 fl6.saddr = hdr->saddr;
157 fl6.flowlabel = ip6_flowinfo(hdr);
158 fl6.flowi6_mark = skb->mark;
159 fl6.flowi6_proto = hdr->nexthdr;
160
161 if (nhaddr)
162 fl6.flowi6_flags = FLOWI_FLAG_KNOWN_NH;
163
164 if (!tbl_id) {
David Ahernb75cc8f2018-03-02 08:32:17 -0800165 dst = ip6_route_input_lookup(net, skb->dev, &fl6, skb, flags);
David Lebrund7a669d2017-08-25 09:56:47 +0200166 } else {
167 struct fib6_table *table;
168
169 table = fib6_get_table(net, tbl_id);
170 if (!table)
171 goto out;
172
David Ahernb75cc8f2018-03-02 08:32:17 -0800173 rt = ip6_pol_route(net, table, 0, &fl6, skb, flags);
David Lebrund7a669d2017-08-25 09:56:47 +0200174 dst = &rt->dst;
175 }
176
177 if (dst && dst->dev->flags & IFF_LOOPBACK && !dst->error) {
178 dst_release(dst);
179 dst = NULL;
180 }
181
182out:
183 if (!dst) {
184 rt = net->ipv6.ip6_blk_hole_entry;
185 dst = &rt->dst;
186 dst_hold(dst);
187 }
188
189 skb_dst_drop(skb);
190 skb_dst_set(skb, dst);
Mathieu Xhonneux1c1e7612018-05-20 14:58:13 +0100191 return dst->error;
David Lebrund7a669d2017-08-25 09:56:47 +0200192}
193
David Lebrun140f04c2017-08-05 12:39:48 +0200194/* regular endpoint function */
195static int input_action_end(struct sk_buff *skb, struct seg6_local_lwt *slwt)
196{
197 struct ipv6_sr_hdr *srh;
David Lebrun140f04c2017-08-05 12:39:48 +0200198
199 srh = get_and_validate_srh(skb);
200 if (!srh)
201 goto drop;
202
David Lebrund7a669d2017-08-25 09:56:47 +0200203 advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
David Lebrun140f04c2017-08-05 12:39:48 +0200204
Mathieu Xhonneux1c1e7612018-05-20 14:58:13 +0100205 seg6_lookup_nexthop(skb, NULL, 0);
David Lebrun140f04c2017-08-05 12:39:48 +0200206
207 return dst_input(skb);
208
209drop:
210 kfree_skb(skb);
211 return -EINVAL;
212}
213
214/* regular endpoint, and forward to specified nexthop */
215static int input_action_end_x(struct sk_buff *skb, struct seg6_local_lwt *slwt)
216{
David Lebrun140f04c2017-08-05 12:39:48 +0200217 struct ipv6_sr_hdr *srh;
David Lebrun140f04c2017-08-05 12:39:48 +0200218
219 srh = get_and_validate_srh(skb);
220 if (!srh)
221 goto drop;
222
David Lebrund7a669d2017-08-25 09:56:47 +0200223 advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
David Lebrun140f04c2017-08-05 12:39:48 +0200224
Mathieu Xhonneux1c1e7612018-05-20 14:58:13 +0100225 seg6_lookup_nexthop(skb, &slwt->nh6, 0);
David Lebrun140f04c2017-08-05 12:39:48 +0200226
227 return dst_input(skb);
228
229drop:
230 kfree_skb(skb);
231 return -EINVAL;
232}
233
David Lebrun891ef8d2017-08-25 09:58:17 +0200234static int input_action_end_t(struct sk_buff *skb, struct seg6_local_lwt *slwt)
235{
236 struct ipv6_sr_hdr *srh;
237
238 srh = get_and_validate_srh(skb);
239 if (!srh)
240 goto drop;
241
242 advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
243
Mathieu Xhonneux1c1e7612018-05-20 14:58:13 +0100244 seg6_lookup_nexthop(skb, NULL, slwt->table);
David Lebrun891ef8d2017-08-25 09:58:17 +0200245
246 return dst_input(skb);
247
248drop:
249 kfree_skb(skb);
250 return -EINVAL;
251}
252
253/* decapsulate and forward inner L2 frame on specified interface */
254static int input_action_end_dx2(struct sk_buff *skb,
255 struct seg6_local_lwt *slwt)
256{
257 struct net *net = dev_net(skb->dev);
258 struct net_device *odev;
259 struct ethhdr *eth;
260
261 if (!decap_and_validate(skb, NEXTHDR_NONE))
262 goto drop;
263
264 if (!pskb_may_pull(skb, ETH_HLEN))
265 goto drop;
266
267 skb_reset_mac_header(skb);
268 eth = (struct ethhdr *)skb->data;
269
270 /* To determine the frame's protocol, we assume it is 802.3. This avoids
271 * a call to eth_type_trans(), which is not really relevant for our
272 * use case.
273 */
274 if (!eth_proto_is_802_3(eth->h_proto))
275 goto drop;
276
277 odev = dev_get_by_index_rcu(net, slwt->oif);
278 if (!odev)
279 goto drop;
280
281 /* As we accept Ethernet frames, make sure the egress device is of
282 * the correct type.
283 */
284 if (odev->type != ARPHRD_ETHER)
285 goto drop;
286
287 if (!(odev->flags & IFF_UP) || !netif_carrier_ok(odev))
288 goto drop;
289
290 skb_orphan(skb);
291
292 if (skb_warn_if_lro(skb))
293 goto drop;
294
295 skb_forward_csum(skb);
296
297 if (skb->len - ETH_HLEN > odev->mtu)
298 goto drop;
299
300 skb->dev = odev;
301 skb->protocol = eth->h_proto;
302
303 return dev_queue_xmit(skb);
304
305drop:
306 kfree_skb(skb);
307 return -EINVAL;
308}
309
David Lebrun140f04c2017-08-05 12:39:48 +0200310/* decapsulate and forward to specified nexthop */
311static int input_action_end_dx6(struct sk_buff *skb,
312 struct seg6_local_lwt *slwt)
313{
David Lebrund7a669d2017-08-25 09:56:47 +0200314 struct in6_addr *nhaddr = NULL;
David Lebrun140f04c2017-08-05 12:39:48 +0200315
316 /* this function accepts IPv6 encapsulated packets, with either
317 * an SRH with SL=0, or no SRH.
318 */
319
David Lebrund7a669d2017-08-25 09:56:47 +0200320 if (!decap_and_validate(skb, IPPROTO_IPV6))
David Lebrun140f04c2017-08-05 12:39:48 +0200321 goto drop;
322
David Lebrund7a669d2017-08-25 09:56:47 +0200323 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
David Lebrun140f04c2017-08-05 12:39:48 +0200324 goto drop;
David Lebrun140f04c2017-08-05 12:39:48 +0200325
326 /* The inner packet is not associated to any local interface,
327 * so we do not call netif_rx().
328 *
329 * If slwt->nh6 is set to ::, then lookup the nexthop for the
330 * inner packet's DA. Otherwise, use the specified nexthop.
331 */
332
David Lebrund7a669d2017-08-25 09:56:47 +0200333 if (!ipv6_addr_any(&slwt->nh6))
334 nhaddr = &slwt->nh6;
David Lebrun140f04c2017-08-05 12:39:48 +0200335
Mathieu Xhonneux1c1e7612018-05-20 14:58:13 +0100336 seg6_lookup_nexthop(skb, nhaddr, 0);
David Lebrun140f04c2017-08-05 12:39:48 +0200337
338 return dst_input(skb);
339drop:
340 kfree_skb(skb);
341 return -EINVAL;
342}
343
David Lebrun891ef8d2017-08-25 09:58:17 +0200344static int input_action_end_dx4(struct sk_buff *skb,
345 struct seg6_local_lwt *slwt)
346{
347 struct iphdr *iph;
348 __be32 nhaddr;
349 int err;
350
351 if (!decap_and_validate(skb, IPPROTO_IPIP))
352 goto drop;
353
354 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
355 goto drop;
356
357 skb->protocol = htons(ETH_P_IP);
358
359 iph = ip_hdr(skb);
360
361 nhaddr = slwt->nh4.s_addr ?: iph->daddr;
362
363 skb_dst_drop(skb);
364
365 err = ip_route_input(skb, nhaddr, iph->saddr, 0, skb->dev);
366 if (err)
367 goto drop;
368
369 return dst_input(skb);
370
371drop:
372 kfree_skb(skb);
373 return -EINVAL;
374}
375
376static int input_action_end_dt6(struct sk_buff *skb,
377 struct seg6_local_lwt *slwt)
378{
379 if (!decap_and_validate(skb, IPPROTO_IPV6))
380 goto drop;
381
382 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
383 goto drop;
384
Mathieu Xhonneux1c1e7612018-05-20 14:58:13 +0100385 seg6_lookup_nexthop(skb, NULL, slwt->table);
David Lebrun891ef8d2017-08-25 09:58:17 +0200386
387 return dst_input(skb);
388
389drop:
390 kfree_skb(skb);
391 return -EINVAL;
392}
393
David Lebrun140f04c2017-08-05 12:39:48 +0200394/* push an SRH on top of the current one */
395static int input_action_end_b6(struct sk_buff *skb, struct seg6_local_lwt *slwt)
396{
397 struct ipv6_sr_hdr *srh;
398 int err = -EINVAL;
399
400 srh = get_and_validate_srh(skb);
401 if (!srh)
402 goto drop;
403
404 err = seg6_do_srh_inline(skb, slwt->srh);
405 if (err)
406 goto drop;
407
408 ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
409 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
410
Mathieu Xhonneux1c1e7612018-05-20 14:58:13 +0100411 seg6_lookup_nexthop(skb, NULL, 0);
David Lebrun140f04c2017-08-05 12:39:48 +0200412
413 return dst_input(skb);
414
415drop:
416 kfree_skb(skb);
417 return err;
418}
419
420/* encapsulate within an outer IPv6 header and a specified SRH */
421static int input_action_end_b6_encap(struct sk_buff *skb,
422 struct seg6_local_lwt *slwt)
423{
424 struct ipv6_sr_hdr *srh;
David Lebrun140f04c2017-08-05 12:39:48 +0200425 int err = -EINVAL;
426
427 srh = get_and_validate_srh(skb);
428 if (!srh)
429 goto drop;
430
David Lebrund7a669d2017-08-25 09:56:47 +0200431 advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
David Lebrun140f04c2017-08-05 12:39:48 +0200432
433 skb_reset_inner_headers(skb);
434 skb->encapsulation = 1;
435
David Lebrun32d99d02017-08-25 09:56:44 +0200436 err = seg6_do_srh_encap(skb, slwt->srh, IPPROTO_IPV6);
David Lebrun140f04c2017-08-05 12:39:48 +0200437 if (err)
438 goto drop;
439
440 ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
441 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
442
Mathieu Xhonneux1c1e7612018-05-20 14:58:13 +0100443 seg6_lookup_nexthop(skb, NULL, 0);
David Lebrun140f04c2017-08-05 12:39:48 +0200444
445 return dst_input(skb);
446
447drop:
448 kfree_skb(skb);
449 return err;
450}
451
David Lebrund1df6fd2017-08-05 12:38:26 +0200452static struct seg6_action_desc seg6_action_table[] = {
453 {
454 .action = SEG6_LOCAL_ACTION_END,
455 .attrs = 0,
David Lebrun140f04c2017-08-05 12:39:48 +0200456 .input = input_action_end,
David Lebrund1df6fd2017-08-05 12:38:26 +0200457 },
David Lebrun140f04c2017-08-05 12:39:48 +0200458 {
459 .action = SEG6_LOCAL_ACTION_END_X,
460 .attrs = (1 << SEG6_LOCAL_NH6),
461 .input = input_action_end_x,
462 },
463 {
David Lebrun891ef8d2017-08-25 09:58:17 +0200464 .action = SEG6_LOCAL_ACTION_END_T,
465 .attrs = (1 << SEG6_LOCAL_TABLE),
466 .input = input_action_end_t,
467 },
468 {
469 .action = SEG6_LOCAL_ACTION_END_DX2,
470 .attrs = (1 << SEG6_LOCAL_OIF),
471 .input = input_action_end_dx2,
472 },
473 {
David Lebrun140f04c2017-08-05 12:39:48 +0200474 .action = SEG6_LOCAL_ACTION_END_DX6,
475 .attrs = (1 << SEG6_LOCAL_NH6),
476 .input = input_action_end_dx6,
477 },
478 {
David Lebrun891ef8d2017-08-25 09:58:17 +0200479 .action = SEG6_LOCAL_ACTION_END_DX4,
480 .attrs = (1 << SEG6_LOCAL_NH4),
481 .input = input_action_end_dx4,
482 },
483 {
484 .action = SEG6_LOCAL_ACTION_END_DT6,
485 .attrs = (1 << SEG6_LOCAL_TABLE),
486 .input = input_action_end_dt6,
487 },
488 {
David Lebrun140f04c2017-08-05 12:39:48 +0200489 .action = SEG6_LOCAL_ACTION_END_B6,
490 .attrs = (1 << SEG6_LOCAL_SRH),
491 .input = input_action_end_b6,
492 },
493 {
494 .action = SEG6_LOCAL_ACTION_END_B6_ENCAP,
495 .attrs = (1 << SEG6_LOCAL_SRH),
496 .input = input_action_end_b6_encap,
497 .static_headroom = sizeof(struct ipv6hdr),
498 }
David Lebrund1df6fd2017-08-05 12:38:26 +0200499};
500
501static struct seg6_action_desc *__get_action_desc(int action)
502{
503 struct seg6_action_desc *desc;
504 int i, count;
505
Colin Ian King709af182018-01-07 23:50:26 +0000506 count = ARRAY_SIZE(seg6_action_table);
David Lebrund1df6fd2017-08-05 12:38:26 +0200507 for (i = 0; i < count; i++) {
508 desc = &seg6_action_table[i];
509 if (desc->action == action)
510 return desc;
511 }
512
513 return NULL;
514}
515
516static int seg6_local_input(struct sk_buff *skb)
517{
518 struct dst_entry *orig_dst = skb_dst(skb);
519 struct seg6_action_desc *desc;
520 struct seg6_local_lwt *slwt;
521
David Lebrun62852172017-08-25 09:56:46 +0200522 if (skb->protocol != htons(ETH_P_IPV6)) {
523 kfree_skb(skb);
524 return -EINVAL;
525 }
526
David Lebrund1df6fd2017-08-05 12:38:26 +0200527 slwt = seg6_local_lwtunnel(orig_dst->lwtstate);
528 desc = slwt->desc;
529
530 return desc->input(skb, slwt);
531}
532
533static const struct nla_policy seg6_local_policy[SEG6_LOCAL_MAX + 1] = {
534 [SEG6_LOCAL_ACTION] = { .type = NLA_U32 },
535 [SEG6_LOCAL_SRH] = { .type = NLA_BINARY },
536 [SEG6_LOCAL_TABLE] = { .type = NLA_U32 },
537 [SEG6_LOCAL_NH4] = { .type = NLA_BINARY,
538 .len = sizeof(struct in_addr) },
539 [SEG6_LOCAL_NH6] = { .type = NLA_BINARY,
540 .len = sizeof(struct in6_addr) },
541 [SEG6_LOCAL_IIF] = { .type = NLA_U32 },
542 [SEG6_LOCAL_OIF] = { .type = NLA_U32 },
543};
544
David Lebrun2d9cc602017-08-05 12:38:27 +0200545static int parse_nla_srh(struct nlattr **attrs, struct seg6_local_lwt *slwt)
546{
547 struct ipv6_sr_hdr *srh;
548 int len;
549
550 srh = nla_data(attrs[SEG6_LOCAL_SRH]);
551 len = nla_len(attrs[SEG6_LOCAL_SRH]);
552
553 /* SRH must contain at least one segment */
554 if (len < sizeof(*srh) + sizeof(struct in6_addr))
555 return -EINVAL;
556
557 if (!seg6_validate_srh(srh, len))
558 return -EINVAL;
559
560 slwt->srh = kmalloc(len, GFP_KERNEL);
561 if (!slwt->srh)
562 return -ENOMEM;
563
564 memcpy(slwt->srh, srh, len);
565
566 slwt->headroom += len;
567
568 return 0;
569}
570
571static int put_nla_srh(struct sk_buff *skb, struct seg6_local_lwt *slwt)
572{
573 struct ipv6_sr_hdr *srh;
574 struct nlattr *nla;
575 int len;
576
577 srh = slwt->srh;
578 len = (srh->hdrlen + 1) << 3;
579
580 nla = nla_reserve(skb, SEG6_LOCAL_SRH, len);
581 if (!nla)
582 return -EMSGSIZE;
583
584 memcpy(nla_data(nla), srh, len);
585
586 return 0;
587}
588
589static int cmp_nla_srh(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
590{
591 int len = (a->srh->hdrlen + 1) << 3;
592
593 if (len != ((b->srh->hdrlen + 1) << 3))
594 return 1;
595
596 return memcmp(a->srh, b->srh, len);
597}
598
599static int parse_nla_table(struct nlattr **attrs, struct seg6_local_lwt *slwt)
600{
601 slwt->table = nla_get_u32(attrs[SEG6_LOCAL_TABLE]);
602
603 return 0;
604}
605
606static int put_nla_table(struct sk_buff *skb, struct seg6_local_lwt *slwt)
607{
608 if (nla_put_u32(skb, SEG6_LOCAL_TABLE, slwt->table))
609 return -EMSGSIZE;
610
611 return 0;
612}
613
614static int cmp_nla_table(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
615{
616 if (a->table != b->table)
617 return 1;
618
619 return 0;
620}
621
622static int parse_nla_nh4(struct nlattr **attrs, struct seg6_local_lwt *slwt)
623{
624 memcpy(&slwt->nh4, nla_data(attrs[SEG6_LOCAL_NH4]),
625 sizeof(struct in_addr));
626
627 return 0;
628}
629
630static int put_nla_nh4(struct sk_buff *skb, struct seg6_local_lwt *slwt)
631{
632 struct nlattr *nla;
633
634 nla = nla_reserve(skb, SEG6_LOCAL_NH4, sizeof(struct in_addr));
635 if (!nla)
636 return -EMSGSIZE;
637
638 memcpy(nla_data(nla), &slwt->nh4, sizeof(struct in_addr));
639
640 return 0;
641}
642
643static int cmp_nla_nh4(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
644{
645 return memcmp(&a->nh4, &b->nh4, sizeof(struct in_addr));
646}
647
648static int parse_nla_nh6(struct nlattr **attrs, struct seg6_local_lwt *slwt)
649{
650 memcpy(&slwt->nh6, nla_data(attrs[SEG6_LOCAL_NH6]),
651 sizeof(struct in6_addr));
652
653 return 0;
654}
655
656static int put_nla_nh6(struct sk_buff *skb, struct seg6_local_lwt *slwt)
657{
658 struct nlattr *nla;
659
660 nla = nla_reserve(skb, SEG6_LOCAL_NH6, sizeof(struct in6_addr));
661 if (!nla)
662 return -EMSGSIZE;
663
664 memcpy(nla_data(nla), &slwt->nh6, sizeof(struct in6_addr));
665
666 return 0;
667}
668
669static int cmp_nla_nh6(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
670{
671 return memcmp(&a->nh6, &b->nh6, sizeof(struct in6_addr));
672}
673
674static int parse_nla_iif(struct nlattr **attrs, struct seg6_local_lwt *slwt)
675{
676 slwt->iif = nla_get_u32(attrs[SEG6_LOCAL_IIF]);
677
678 return 0;
679}
680
681static int put_nla_iif(struct sk_buff *skb, struct seg6_local_lwt *slwt)
682{
683 if (nla_put_u32(skb, SEG6_LOCAL_IIF, slwt->iif))
684 return -EMSGSIZE;
685
686 return 0;
687}
688
689static int cmp_nla_iif(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
690{
691 if (a->iif != b->iif)
692 return 1;
693
694 return 0;
695}
696
697static int parse_nla_oif(struct nlattr **attrs, struct seg6_local_lwt *slwt)
698{
699 slwt->oif = nla_get_u32(attrs[SEG6_LOCAL_OIF]);
700
701 return 0;
702}
703
704static int put_nla_oif(struct sk_buff *skb, struct seg6_local_lwt *slwt)
705{
706 if (nla_put_u32(skb, SEG6_LOCAL_OIF, slwt->oif))
707 return -EMSGSIZE;
708
709 return 0;
710}
711
712static int cmp_nla_oif(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
713{
714 if (a->oif != b->oif)
715 return 1;
716
717 return 0;
718}
719
David Lebrund1df6fd2017-08-05 12:38:26 +0200720struct seg6_action_param {
721 int (*parse)(struct nlattr **attrs, struct seg6_local_lwt *slwt);
722 int (*put)(struct sk_buff *skb, struct seg6_local_lwt *slwt);
723 int (*cmp)(struct seg6_local_lwt *a, struct seg6_local_lwt *b);
724};
725
726static struct seg6_action_param seg6_action_params[SEG6_LOCAL_MAX + 1] = {
David Lebrun2d9cc602017-08-05 12:38:27 +0200727 [SEG6_LOCAL_SRH] = { .parse = parse_nla_srh,
728 .put = put_nla_srh,
729 .cmp = cmp_nla_srh },
David Lebrund1df6fd2017-08-05 12:38:26 +0200730
David Lebrun2d9cc602017-08-05 12:38:27 +0200731 [SEG6_LOCAL_TABLE] = { .parse = parse_nla_table,
732 .put = put_nla_table,
733 .cmp = cmp_nla_table },
David Lebrund1df6fd2017-08-05 12:38:26 +0200734
David Lebrun2d9cc602017-08-05 12:38:27 +0200735 [SEG6_LOCAL_NH4] = { .parse = parse_nla_nh4,
736 .put = put_nla_nh4,
737 .cmp = cmp_nla_nh4 },
David Lebrund1df6fd2017-08-05 12:38:26 +0200738
David Lebrun2d9cc602017-08-05 12:38:27 +0200739 [SEG6_LOCAL_NH6] = { .parse = parse_nla_nh6,
740 .put = put_nla_nh6,
741 .cmp = cmp_nla_nh6 },
David Lebrund1df6fd2017-08-05 12:38:26 +0200742
David Lebrun2d9cc602017-08-05 12:38:27 +0200743 [SEG6_LOCAL_IIF] = { .parse = parse_nla_iif,
744 .put = put_nla_iif,
745 .cmp = cmp_nla_iif },
David Lebrund1df6fd2017-08-05 12:38:26 +0200746
David Lebrun2d9cc602017-08-05 12:38:27 +0200747 [SEG6_LOCAL_OIF] = { .parse = parse_nla_oif,
748 .put = put_nla_oif,
749 .cmp = cmp_nla_oif },
David Lebrund1df6fd2017-08-05 12:38:26 +0200750};
751
752static int parse_nla_action(struct nlattr **attrs, struct seg6_local_lwt *slwt)
753{
754 struct seg6_action_param *param;
755 struct seg6_action_desc *desc;
756 int i, err;
757
758 desc = __get_action_desc(slwt->action);
759 if (!desc)
760 return -EINVAL;
761
762 if (!desc->input)
763 return -EOPNOTSUPP;
764
765 slwt->desc = desc;
766 slwt->headroom += desc->static_headroom;
767
768 for (i = 0; i < SEG6_LOCAL_MAX + 1; i++) {
769 if (desc->attrs & (1 << i)) {
770 if (!attrs[i])
771 return -EINVAL;
772
773 param = &seg6_action_params[i];
774
775 err = param->parse(attrs, slwt);
776 if (err < 0)
777 return err;
778 }
779 }
780
781 return 0;
782}
783
784static int seg6_local_build_state(struct nlattr *nla, unsigned int family,
785 const void *cfg, struct lwtunnel_state **ts,
786 struct netlink_ext_ack *extack)
787{
788 struct nlattr *tb[SEG6_LOCAL_MAX + 1];
789 struct lwtunnel_state *newts;
790 struct seg6_local_lwt *slwt;
791 int err;
792
David Lebrun62852172017-08-25 09:56:46 +0200793 if (family != AF_INET6)
794 return -EINVAL;
795
David Lebrund1df6fd2017-08-05 12:38:26 +0200796 err = nla_parse_nested(tb, SEG6_LOCAL_MAX, nla, seg6_local_policy,
797 extack);
798
799 if (err < 0)
800 return err;
801
802 if (!tb[SEG6_LOCAL_ACTION])
803 return -EINVAL;
804
805 newts = lwtunnel_state_alloc(sizeof(*slwt));
806 if (!newts)
807 return -ENOMEM;
808
809 slwt = seg6_local_lwtunnel(newts);
810 slwt->action = nla_get_u32(tb[SEG6_LOCAL_ACTION]);
811
812 err = parse_nla_action(tb, slwt);
813 if (err < 0)
814 goto out_free;
815
816 newts->type = LWTUNNEL_ENCAP_SEG6_LOCAL;
817 newts->flags = LWTUNNEL_STATE_INPUT_REDIRECT;
818 newts->headroom = slwt->headroom;
819
820 *ts = newts;
821
822 return 0;
823
824out_free:
825 kfree(slwt->srh);
826 kfree(newts);
827 return err;
828}
829
830static void seg6_local_destroy_state(struct lwtunnel_state *lwt)
831{
832 struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
833
834 kfree(slwt->srh);
835}
836
837static int seg6_local_fill_encap(struct sk_buff *skb,
838 struct lwtunnel_state *lwt)
839{
840 struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
841 struct seg6_action_param *param;
842 int i, err;
843
844 if (nla_put_u32(skb, SEG6_LOCAL_ACTION, slwt->action))
845 return -EMSGSIZE;
846
847 for (i = 0; i < SEG6_LOCAL_MAX + 1; i++) {
848 if (slwt->desc->attrs & (1 << i)) {
849 param = &seg6_action_params[i];
850 err = param->put(skb, slwt);
851 if (err < 0)
852 return err;
853 }
854 }
855
856 return 0;
857}
858
859static int seg6_local_get_encap_size(struct lwtunnel_state *lwt)
860{
861 struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
862 unsigned long attrs;
863 int nlsize;
864
865 nlsize = nla_total_size(4); /* action */
866
867 attrs = slwt->desc->attrs;
868
869 if (attrs & (1 << SEG6_LOCAL_SRH))
870 nlsize += nla_total_size((slwt->srh->hdrlen + 1) << 3);
871
872 if (attrs & (1 << SEG6_LOCAL_TABLE))
873 nlsize += nla_total_size(4);
874
875 if (attrs & (1 << SEG6_LOCAL_NH4))
876 nlsize += nla_total_size(4);
877
878 if (attrs & (1 << SEG6_LOCAL_NH6))
879 nlsize += nla_total_size(16);
880
881 if (attrs & (1 << SEG6_LOCAL_IIF))
882 nlsize += nla_total_size(4);
883
884 if (attrs & (1 << SEG6_LOCAL_OIF))
885 nlsize += nla_total_size(4);
886
887 return nlsize;
888}
889
890static int seg6_local_cmp_encap(struct lwtunnel_state *a,
891 struct lwtunnel_state *b)
892{
893 struct seg6_local_lwt *slwt_a, *slwt_b;
894 struct seg6_action_param *param;
895 int i;
896
897 slwt_a = seg6_local_lwtunnel(a);
898 slwt_b = seg6_local_lwtunnel(b);
899
900 if (slwt_a->action != slwt_b->action)
901 return 1;
902
903 if (slwt_a->desc->attrs != slwt_b->desc->attrs)
904 return 1;
905
906 for (i = 0; i < SEG6_LOCAL_MAX + 1; i++) {
907 if (slwt_a->desc->attrs & (1 << i)) {
908 param = &seg6_action_params[i];
909 if (param->cmp(slwt_a, slwt_b))
910 return 1;
911 }
912 }
913
914 return 0;
915}
916
917static const struct lwtunnel_encap_ops seg6_local_ops = {
918 .build_state = seg6_local_build_state,
919 .destroy_state = seg6_local_destroy_state,
920 .input = seg6_local_input,
921 .fill_encap = seg6_local_fill_encap,
922 .get_encap_size = seg6_local_get_encap_size,
923 .cmp_encap = seg6_local_cmp_encap,
924 .owner = THIS_MODULE,
925};
926
927int __init seg6_local_init(void)
928{
929 return lwtunnel_encap_add_ops(&seg6_local_ops,
930 LWTUNNEL_ENCAP_SEG6_LOCAL);
931}
932
933void seg6_local_exit(void)
934{
935 lwtunnel_encap_del_ops(&seg6_local_ops, LWTUNNEL_ENCAP_SEG6_LOCAL);
936}