blob: ae68c1ef8fb0b4fb1d507796123b6b858822cbaf [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
Mathieu Xhonneuxfe94cc22018-05-20 14:58:14 +0100452DEFINE_PER_CPU(struct seg6_bpf_srh_state, seg6_bpf_srh_states);
453
David Lebrund1df6fd2017-08-05 12:38:26 +0200454static struct seg6_action_desc seg6_action_table[] = {
455 {
456 .action = SEG6_LOCAL_ACTION_END,
457 .attrs = 0,
David Lebrun140f04c2017-08-05 12:39:48 +0200458 .input = input_action_end,
David Lebrund1df6fd2017-08-05 12:38:26 +0200459 },
David Lebrun140f04c2017-08-05 12:39:48 +0200460 {
461 .action = SEG6_LOCAL_ACTION_END_X,
462 .attrs = (1 << SEG6_LOCAL_NH6),
463 .input = input_action_end_x,
464 },
465 {
David Lebrun891ef8d2017-08-25 09:58:17 +0200466 .action = SEG6_LOCAL_ACTION_END_T,
467 .attrs = (1 << SEG6_LOCAL_TABLE),
468 .input = input_action_end_t,
469 },
470 {
471 .action = SEG6_LOCAL_ACTION_END_DX2,
472 .attrs = (1 << SEG6_LOCAL_OIF),
473 .input = input_action_end_dx2,
474 },
475 {
David Lebrun140f04c2017-08-05 12:39:48 +0200476 .action = SEG6_LOCAL_ACTION_END_DX6,
477 .attrs = (1 << SEG6_LOCAL_NH6),
478 .input = input_action_end_dx6,
479 },
480 {
David Lebrun891ef8d2017-08-25 09:58:17 +0200481 .action = SEG6_LOCAL_ACTION_END_DX4,
482 .attrs = (1 << SEG6_LOCAL_NH4),
483 .input = input_action_end_dx4,
484 },
485 {
486 .action = SEG6_LOCAL_ACTION_END_DT6,
487 .attrs = (1 << SEG6_LOCAL_TABLE),
488 .input = input_action_end_dt6,
489 },
490 {
David Lebrun140f04c2017-08-05 12:39:48 +0200491 .action = SEG6_LOCAL_ACTION_END_B6,
492 .attrs = (1 << SEG6_LOCAL_SRH),
493 .input = input_action_end_b6,
494 },
495 {
496 .action = SEG6_LOCAL_ACTION_END_B6_ENCAP,
497 .attrs = (1 << SEG6_LOCAL_SRH),
498 .input = input_action_end_b6_encap,
499 .static_headroom = sizeof(struct ipv6hdr),
500 }
David Lebrund1df6fd2017-08-05 12:38:26 +0200501};
502
503static struct seg6_action_desc *__get_action_desc(int action)
504{
505 struct seg6_action_desc *desc;
506 int i, count;
507
Colin Ian King709af182018-01-07 23:50:26 +0000508 count = ARRAY_SIZE(seg6_action_table);
David Lebrund1df6fd2017-08-05 12:38:26 +0200509 for (i = 0; i < count; i++) {
510 desc = &seg6_action_table[i];
511 if (desc->action == action)
512 return desc;
513 }
514
515 return NULL;
516}
517
518static int seg6_local_input(struct sk_buff *skb)
519{
520 struct dst_entry *orig_dst = skb_dst(skb);
521 struct seg6_action_desc *desc;
522 struct seg6_local_lwt *slwt;
523
David Lebrun62852172017-08-25 09:56:46 +0200524 if (skb->protocol != htons(ETH_P_IPV6)) {
525 kfree_skb(skb);
526 return -EINVAL;
527 }
528
David Lebrund1df6fd2017-08-05 12:38:26 +0200529 slwt = seg6_local_lwtunnel(orig_dst->lwtstate);
530 desc = slwt->desc;
531
532 return desc->input(skb, slwt);
533}
534
535static const struct nla_policy seg6_local_policy[SEG6_LOCAL_MAX + 1] = {
536 [SEG6_LOCAL_ACTION] = { .type = NLA_U32 },
537 [SEG6_LOCAL_SRH] = { .type = NLA_BINARY },
538 [SEG6_LOCAL_TABLE] = { .type = NLA_U32 },
539 [SEG6_LOCAL_NH4] = { .type = NLA_BINARY,
540 .len = sizeof(struct in_addr) },
541 [SEG6_LOCAL_NH6] = { .type = NLA_BINARY,
542 .len = sizeof(struct in6_addr) },
543 [SEG6_LOCAL_IIF] = { .type = NLA_U32 },
544 [SEG6_LOCAL_OIF] = { .type = NLA_U32 },
545};
546
David Lebrun2d9cc602017-08-05 12:38:27 +0200547static int parse_nla_srh(struct nlattr **attrs, struct seg6_local_lwt *slwt)
548{
549 struct ipv6_sr_hdr *srh;
550 int len;
551
552 srh = nla_data(attrs[SEG6_LOCAL_SRH]);
553 len = nla_len(attrs[SEG6_LOCAL_SRH]);
554
555 /* SRH must contain at least one segment */
556 if (len < sizeof(*srh) + sizeof(struct in6_addr))
557 return -EINVAL;
558
559 if (!seg6_validate_srh(srh, len))
560 return -EINVAL;
561
562 slwt->srh = kmalloc(len, GFP_KERNEL);
563 if (!slwt->srh)
564 return -ENOMEM;
565
566 memcpy(slwt->srh, srh, len);
567
568 slwt->headroom += len;
569
570 return 0;
571}
572
573static int put_nla_srh(struct sk_buff *skb, struct seg6_local_lwt *slwt)
574{
575 struct ipv6_sr_hdr *srh;
576 struct nlattr *nla;
577 int len;
578
579 srh = slwt->srh;
580 len = (srh->hdrlen + 1) << 3;
581
582 nla = nla_reserve(skb, SEG6_LOCAL_SRH, len);
583 if (!nla)
584 return -EMSGSIZE;
585
586 memcpy(nla_data(nla), srh, len);
587
588 return 0;
589}
590
591static int cmp_nla_srh(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
592{
593 int len = (a->srh->hdrlen + 1) << 3;
594
595 if (len != ((b->srh->hdrlen + 1) << 3))
596 return 1;
597
598 return memcmp(a->srh, b->srh, len);
599}
600
601static int parse_nla_table(struct nlattr **attrs, struct seg6_local_lwt *slwt)
602{
603 slwt->table = nla_get_u32(attrs[SEG6_LOCAL_TABLE]);
604
605 return 0;
606}
607
608static int put_nla_table(struct sk_buff *skb, struct seg6_local_lwt *slwt)
609{
610 if (nla_put_u32(skb, SEG6_LOCAL_TABLE, slwt->table))
611 return -EMSGSIZE;
612
613 return 0;
614}
615
616static int cmp_nla_table(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
617{
618 if (a->table != b->table)
619 return 1;
620
621 return 0;
622}
623
624static int parse_nla_nh4(struct nlattr **attrs, struct seg6_local_lwt *slwt)
625{
626 memcpy(&slwt->nh4, nla_data(attrs[SEG6_LOCAL_NH4]),
627 sizeof(struct in_addr));
628
629 return 0;
630}
631
632static int put_nla_nh4(struct sk_buff *skb, struct seg6_local_lwt *slwt)
633{
634 struct nlattr *nla;
635
636 nla = nla_reserve(skb, SEG6_LOCAL_NH4, sizeof(struct in_addr));
637 if (!nla)
638 return -EMSGSIZE;
639
640 memcpy(nla_data(nla), &slwt->nh4, sizeof(struct in_addr));
641
642 return 0;
643}
644
645static int cmp_nla_nh4(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
646{
647 return memcmp(&a->nh4, &b->nh4, sizeof(struct in_addr));
648}
649
650static int parse_nla_nh6(struct nlattr **attrs, struct seg6_local_lwt *slwt)
651{
652 memcpy(&slwt->nh6, nla_data(attrs[SEG6_LOCAL_NH6]),
653 sizeof(struct in6_addr));
654
655 return 0;
656}
657
658static int put_nla_nh6(struct sk_buff *skb, struct seg6_local_lwt *slwt)
659{
660 struct nlattr *nla;
661
662 nla = nla_reserve(skb, SEG6_LOCAL_NH6, sizeof(struct in6_addr));
663 if (!nla)
664 return -EMSGSIZE;
665
666 memcpy(nla_data(nla), &slwt->nh6, sizeof(struct in6_addr));
667
668 return 0;
669}
670
671static int cmp_nla_nh6(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
672{
673 return memcmp(&a->nh6, &b->nh6, sizeof(struct in6_addr));
674}
675
676static int parse_nla_iif(struct nlattr **attrs, struct seg6_local_lwt *slwt)
677{
678 slwt->iif = nla_get_u32(attrs[SEG6_LOCAL_IIF]);
679
680 return 0;
681}
682
683static int put_nla_iif(struct sk_buff *skb, struct seg6_local_lwt *slwt)
684{
685 if (nla_put_u32(skb, SEG6_LOCAL_IIF, slwt->iif))
686 return -EMSGSIZE;
687
688 return 0;
689}
690
691static int cmp_nla_iif(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
692{
693 if (a->iif != b->iif)
694 return 1;
695
696 return 0;
697}
698
699static int parse_nla_oif(struct nlattr **attrs, struct seg6_local_lwt *slwt)
700{
701 slwt->oif = nla_get_u32(attrs[SEG6_LOCAL_OIF]);
702
703 return 0;
704}
705
706static int put_nla_oif(struct sk_buff *skb, struct seg6_local_lwt *slwt)
707{
708 if (nla_put_u32(skb, SEG6_LOCAL_OIF, slwt->oif))
709 return -EMSGSIZE;
710
711 return 0;
712}
713
714static int cmp_nla_oif(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
715{
716 if (a->oif != b->oif)
717 return 1;
718
719 return 0;
720}
721
David Lebrund1df6fd2017-08-05 12:38:26 +0200722struct seg6_action_param {
723 int (*parse)(struct nlattr **attrs, struct seg6_local_lwt *slwt);
724 int (*put)(struct sk_buff *skb, struct seg6_local_lwt *slwt);
725 int (*cmp)(struct seg6_local_lwt *a, struct seg6_local_lwt *b);
726};
727
728static struct seg6_action_param seg6_action_params[SEG6_LOCAL_MAX + 1] = {
David Lebrun2d9cc602017-08-05 12:38:27 +0200729 [SEG6_LOCAL_SRH] = { .parse = parse_nla_srh,
730 .put = put_nla_srh,
731 .cmp = cmp_nla_srh },
David Lebrund1df6fd2017-08-05 12:38:26 +0200732
David Lebrun2d9cc602017-08-05 12:38:27 +0200733 [SEG6_LOCAL_TABLE] = { .parse = parse_nla_table,
734 .put = put_nla_table,
735 .cmp = cmp_nla_table },
David Lebrund1df6fd2017-08-05 12:38:26 +0200736
David Lebrun2d9cc602017-08-05 12:38:27 +0200737 [SEG6_LOCAL_NH4] = { .parse = parse_nla_nh4,
738 .put = put_nla_nh4,
739 .cmp = cmp_nla_nh4 },
David Lebrund1df6fd2017-08-05 12:38:26 +0200740
David Lebrun2d9cc602017-08-05 12:38:27 +0200741 [SEG6_LOCAL_NH6] = { .parse = parse_nla_nh6,
742 .put = put_nla_nh6,
743 .cmp = cmp_nla_nh6 },
David Lebrund1df6fd2017-08-05 12:38:26 +0200744
David Lebrun2d9cc602017-08-05 12:38:27 +0200745 [SEG6_LOCAL_IIF] = { .parse = parse_nla_iif,
746 .put = put_nla_iif,
747 .cmp = cmp_nla_iif },
David Lebrund1df6fd2017-08-05 12:38:26 +0200748
David Lebrun2d9cc602017-08-05 12:38:27 +0200749 [SEG6_LOCAL_OIF] = { .parse = parse_nla_oif,
750 .put = put_nla_oif,
751 .cmp = cmp_nla_oif },
David Lebrund1df6fd2017-08-05 12:38:26 +0200752};
753
754static int parse_nla_action(struct nlattr **attrs, struct seg6_local_lwt *slwt)
755{
756 struct seg6_action_param *param;
757 struct seg6_action_desc *desc;
758 int i, err;
759
760 desc = __get_action_desc(slwt->action);
761 if (!desc)
762 return -EINVAL;
763
764 if (!desc->input)
765 return -EOPNOTSUPP;
766
767 slwt->desc = desc;
768 slwt->headroom += desc->static_headroom;
769
770 for (i = 0; i < SEG6_LOCAL_MAX + 1; i++) {
771 if (desc->attrs & (1 << i)) {
772 if (!attrs[i])
773 return -EINVAL;
774
775 param = &seg6_action_params[i];
776
777 err = param->parse(attrs, slwt);
778 if (err < 0)
779 return err;
780 }
781 }
782
783 return 0;
784}
785
786static int seg6_local_build_state(struct nlattr *nla, unsigned int family,
787 const void *cfg, struct lwtunnel_state **ts,
788 struct netlink_ext_ack *extack)
789{
790 struct nlattr *tb[SEG6_LOCAL_MAX + 1];
791 struct lwtunnel_state *newts;
792 struct seg6_local_lwt *slwt;
793 int err;
794
David Lebrun62852172017-08-25 09:56:46 +0200795 if (family != AF_INET6)
796 return -EINVAL;
797
David Lebrund1df6fd2017-08-05 12:38:26 +0200798 err = nla_parse_nested(tb, SEG6_LOCAL_MAX, nla, seg6_local_policy,
799 extack);
800
801 if (err < 0)
802 return err;
803
804 if (!tb[SEG6_LOCAL_ACTION])
805 return -EINVAL;
806
807 newts = lwtunnel_state_alloc(sizeof(*slwt));
808 if (!newts)
809 return -ENOMEM;
810
811 slwt = seg6_local_lwtunnel(newts);
812 slwt->action = nla_get_u32(tb[SEG6_LOCAL_ACTION]);
813
814 err = parse_nla_action(tb, slwt);
815 if (err < 0)
816 goto out_free;
817
818 newts->type = LWTUNNEL_ENCAP_SEG6_LOCAL;
819 newts->flags = LWTUNNEL_STATE_INPUT_REDIRECT;
820 newts->headroom = slwt->headroom;
821
822 *ts = newts;
823
824 return 0;
825
826out_free:
827 kfree(slwt->srh);
828 kfree(newts);
829 return err;
830}
831
832static void seg6_local_destroy_state(struct lwtunnel_state *lwt)
833{
834 struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
835
836 kfree(slwt->srh);
837}
838
839static int seg6_local_fill_encap(struct sk_buff *skb,
840 struct lwtunnel_state *lwt)
841{
842 struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
843 struct seg6_action_param *param;
844 int i, err;
845
846 if (nla_put_u32(skb, SEG6_LOCAL_ACTION, slwt->action))
847 return -EMSGSIZE;
848
849 for (i = 0; i < SEG6_LOCAL_MAX + 1; i++) {
850 if (slwt->desc->attrs & (1 << i)) {
851 param = &seg6_action_params[i];
852 err = param->put(skb, slwt);
853 if (err < 0)
854 return err;
855 }
856 }
857
858 return 0;
859}
860
861static int seg6_local_get_encap_size(struct lwtunnel_state *lwt)
862{
863 struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
864 unsigned long attrs;
865 int nlsize;
866
867 nlsize = nla_total_size(4); /* action */
868
869 attrs = slwt->desc->attrs;
870
871 if (attrs & (1 << SEG6_LOCAL_SRH))
872 nlsize += nla_total_size((slwt->srh->hdrlen + 1) << 3);
873
874 if (attrs & (1 << SEG6_LOCAL_TABLE))
875 nlsize += nla_total_size(4);
876
877 if (attrs & (1 << SEG6_LOCAL_NH4))
878 nlsize += nla_total_size(4);
879
880 if (attrs & (1 << SEG6_LOCAL_NH6))
881 nlsize += nla_total_size(16);
882
883 if (attrs & (1 << SEG6_LOCAL_IIF))
884 nlsize += nla_total_size(4);
885
886 if (attrs & (1 << SEG6_LOCAL_OIF))
887 nlsize += nla_total_size(4);
888
889 return nlsize;
890}
891
892static int seg6_local_cmp_encap(struct lwtunnel_state *a,
893 struct lwtunnel_state *b)
894{
895 struct seg6_local_lwt *slwt_a, *slwt_b;
896 struct seg6_action_param *param;
897 int i;
898
899 slwt_a = seg6_local_lwtunnel(a);
900 slwt_b = seg6_local_lwtunnel(b);
901
902 if (slwt_a->action != slwt_b->action)
903 return 1;
904
905 if (slwt_a->desc->attrs != slwt_b->desc->attrs)
906 return 1;
907
908 for (i = 0; i < SEG6_LOCAL_MAX + 1; i++) {
909 if (slwt_a->desc->attrs & (1 << i)) {
910 param = &seg6_action_params[i];
911 if (param->cmp(slwt_a, slwt_b))
912 return 1;
913 }
914 }
915
916 return 0;
917}
918
919static const struct lwtunnel_encap_ops seg6_local_ops = {
920 .build_state = seg6_local_build_state,
921 .destroy_state = seg6_local_destroy_state,
922 .input = seg6_local_input,
923 .fill_encap = seg6_local_fill_encap,
924 .get_encap_size = seg6_local_get_encap_size,
925 .cmp_encap = seg6_local_cmp_encap,
926 .owner = THIS_MODULE,
927};
928
929int __init seg6_local_init(void)
930{
931 return lwtunnel_encap_add_ops(&seg6_local_ops,
932 LWTUNNEL_ENCAP_SEG6_LOCAL);
933}
934
935void seg6_local_exit(void)
936{
937 lwtunnel_encap_del_ops(&seg6_local_ops, LWTUNNEL_ENCAP_SEG6_LOCAL);
938}