GNU Linux-libre 4.14.251-gnu1
[releases.git] / net / ipv6 / seg6_local.c
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 #include <net/ip_tunnels.h>
31 #ifdef CONFIG_IPV6_SEG6_HMAC
32 #include <net/seg6_hmac.h>
33 #endif
34 #include <linux/etherdevice.h>
35
36 struct seg6_local_lwt;
37
38 struct 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
45 struct 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
58 static struct seg6_local_lwt *seg6_local_lwtunnel(struct lwtunnel_state *lwt)
59 {
60         return (struct seg6_local_lwt *)lwt->data;
61 }
62
63 static struct ipv6_sr_hdr *get_srh(struct sk_buff *skb)
64 {
65         struct ipv6_sr_hdr *srh;
66         int len, srhoff = 0;
67
68         if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
69                 return NULL;
70
71         if (!pskb_may_pull(skb, srhoff + sizeof(*srh)))
72                 return NULL;
73
74         srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
75
76         len = (srh->hdrlen + 1) << 3;
77
78         if (!pskb_may_pull(skb, srhoff + len))
79                 return NULL;
80
81         if (!seg6_validate_srh(srh, len))
82                 return NULL;
83
84         return srh;
85 }
86
87 static 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
106 static 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         if (iptunnel_pull_offloads(skb))
131                 return false;
132
133         return true;
134 }
135
136 static void advance_nextseg(struct ipv6_sr_hdr *srh, struct in6_addr *daddr)
137 {
138         struct in6_addr *addr;
139
140         srh->segments_left--;
141         addr = srh->segments + srh->segments_left;
142         *daddr = *addr;
143 }
144
145 static void lookup_nexthop(struct sk_buff *skb, struct in6_addr *nhaddr,
146                            u32 tbl_id)
147 {
148         struct net *net = dev_net(skb->dev);
149         struct ipv6hdr *hdr = ipv6_hdr(skb);
150         int flags = RT6_LOOKUP_F_HAS_SADDR;
151         struct dst_entry *dst = NULL;
152         struct rt6_info *rt;
153         struct flowi6 fl6;
154
155         fl6.flowi6_iif = skb->dev->ifindex;
156         fl6.daddr = nhaddr ? *nhaddr : hdr->daddr;
157         fl6.saddr = hdr->saddr;
158         fl6.flowlabel = ip6_flowinfo(hdr);
159         fl6.flowi6_mark = skb->mark;
160         fl6.flowi6_proto = hdr->nexthdr;
161
162         if (nhaddr)
163                 fl6.flowi6_flags = FLOWI_FLAG_KNOWN_NH;
164
165         if (!tbl_id) {
166                 dst = ip6_route_input_lookup(net, skb->dev, &fl6, flags);
167         } else {
168                 struct fib6_table *table;
169
170                 table = fib6_get_table(net, tbl_id);
171                 if (!table)
172                         goto out;
173
174                 rt = ip6_pol_route(net, table, 0, &fl6, flags);
175                 dst = &rt->dst;
176         }
177
178         if (dst && dst->dev->flags & IFF_LOOPBACK && !dst->error) {
179                 dst_release(dst);
180                 dst = NULL;
181         }
182
183 out:
184         if (!dst) {
185                 rt = net->ipv6.ip6_blk_hole_entry;
186                 dst = &rt->dst;
187                 dst_hold(dst);
188         }
189
190         skb_dst_drop(skb);
191         skb_dst_set(skb, dst);
192 }
193
194 /* regular endpoint function */
195 static int input_action_end(struct sk_buff *skb, struct seg6_local_lwt *slwt)
196 {
197         struct ipv6_sr_hdr *srh;
198
199         srh = get_and_validate_srh(skb);
200         if (!srh)
201                 goto drop;
202
203         advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
204
205         lookup_nexthop(skb, NULL, 0);
206
207         return dst_input(skb);
208
209 drop:
210         kfree_skb(skb);
211         return -EINVAL;
212 }
213
214 /* regular endpoint, and forward to specified nexthop */
215 static int input_action_end_x(struct sk_buff *skb, struct seg6_local_lwt *slwt)
216 {
217         struct ipv6_sr_hdr *srh;
218
219         srh = get_and_validate_srh(skb);
220         if (!srh)
221                 goto drop;
222
223         advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
224
225         lookup_nexthop(skb, &slwt->nh6, 0);
226
227         return dst_input(skb);
228
229 drop:
230         kfree_skb(skb);
231         return -EINVAL;
232 }
233
234 static 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
244         lookup_nexthop(skb, NULL, slwt->table);
245
246         return dst_input(skb);
247
248 drop:
249         kfree_skb(skb);
250         return -EINVAL;
251 }
252
253 /* decapsulate and forward inner L2 frame on specified interface */
254 static 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
305 drop:
306         kfree_skb(skb);
307         return -EINVAL;
308 }
309
310 /* decapsulate and forward to specified nexthop */
311 static int input_action_end_dx6(struct sk_buff *skb,
312                                 struct seg6_local_lwt *slwt)
313 {
314         struct in6_addr *nhaddr = NULL;
315
316         /* this function accepts IPv6 encapsulated packets, with either
317          * an SRH with SL=0, or no SRH.
318          */
319
320         if (!decap_and_validate(skb, IPPROTO_IPV6))
321                 goto drop;
322
323         if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
324                 goto drop;
325
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
333         if (!ipv6_addr_any(&slwt->nh6))
334                 nhaddr = &slwt->nh6;
335
336         lookup_nexthop(skb, nhaddr, 0);
337
338         return dst_input(skb);
339 drop:
340         kfree_skb(skb);
341         return -EINVAL;
342 }
343
344 static 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
371 drop:
372         kfree_skb(skb);
373         return -EINVAL;
374 }
375
376 static 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
385         lookup_nexthop(skb, NULL, slwt->table);
386
387         return dst_input(skb);
388
389 drop:
390         kfree_skb(skb);
391         return -EINVAL;
392 }
393
394 /* push an SRH on top of the current one */
395 static 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
411         lookup_nexthop(skb, NULL, 0);
412
413         return dst_input(skb);
414
415 drop:
416         kfree_skb(skb);
417         return err;
418 }
419
420 /* encapsulate within an outer IPv6 header and a specified SRH */
421 static int input_action_end_b6_encap(struct sk_buff *skb,
422                                      struct seg6_local_lwt *slwt)
423 {
424         struct ipv6_sr_hdr *srh;
425         int err = -EINVAL;
426
427         srh = get_and_validate_srh(skb);
428         if (!srh)
429                 goto drop;
430
431         advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
432
433         skb_reset_inner_headers(skb);
434         skb->encapsulation = 1;
435
436         err = seg6_do_srh_encap(skb, slwt->srh, IPPROTO_IPV6);
437         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
443         lookup_nexthop(skb, NULL, 0);
444
445         return dst_input(skb);
446
447 drop:
448         kfree_skb(skb);
449         return err;
450 }
451
452 static struct seg6_action_desc seg6_action_table[] = {
453         {
454                 .action         = SEG6_LOCAL_ACTION_END,
455                 .attrs          = 0,
456                 .input          = input_action_end,
457         },
458         {
459                 .action         = SEG6_LOCAL_ACTION_END_X,
460                 .attrs          = (1 << SEG6_LOCAL_NH6),
461                 .input          = input_action_end_x,
462         },
463         {
464                 .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         {
474                 .action         = SEG6_LOCAL_ACTION_END_DX6,
475                 .attrs          = (1 << SEG6_LOCAL_NH6),
476                 .input          = input_action_end_dx6,
477         },
478         {
479                 .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         {
489                 .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         }
499 };
500
501 static struct seg6_action_desc *__get_action_desc(int action)
502 {
503         struct seg6_action_desc *desc;
504         int i, count;
505
506         count = sizeof(seg6_action_table) / sizeof(struct seg6_action_desc);
507         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
516 static 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
522         if (skb->protocol != htons(ETH_P_IPV6)) {
523                 kfree_skb(skb);
524                 return -EINVAL;
525         }
526
527         slwt = seg6_local_lwtunnel(orig_dst->lwtstate);
528         desc = slwt->desc;
529
530         return desc->input(skb, slwt);
531 }
532
533 static 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
545 static 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
571 static 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
589 static 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
599 static 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
606 static 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
614 static 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
622 static 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
630 static 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
643 static 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
648 static 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
656 static 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
669 static 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
674 static 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
681 static 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
689 static 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
697 static 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
704 static 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
712 static 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
720 struct 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
726 static struct seg6_action_param seg6_action_params[SEG6_LOCAL_MAX + 1] = {
727         [SEG6_LOCAL_SRH]        = { .parse = parse_nla_srh,
728                                     .put = put_nla_srh,
729                                     .cmp = cmp_nla_srh },
730
731         [SEG6_LOCAL_TABLE]      = { .parse = parse_nla_table,
732                                     .put = put_nla_table,
733                                     .cmp = cmp_nla_table },
734
735         [SEG6_LOCAL_NH4]        = { .parse = parse_nla_nh4,
736                                     .put = put_nla_nh4,
737                                     .cmp = cmp_nla_nh4 },
738
739         [SEG6_LOCAL_NH6]        = { .parse = parse_nla_nh6,
740                                     .put = put_nla_nh6,
741                                     .cmp = cmp_nla_nh6 },
742
743         [SEG6_LOCAL_IIF]        = { .parse = parse_nla_iif,
744                                     .put = put_nla_iif,
745                                     .cmp = cmp_nla_iif },
746
747         [SEG6_LOCAL_OIF]        = { .parse = parse_nla_oif,
748                                     .put = put_nla_oif,
749                                     .cmp = cmp_nla_oif },
750 };
751
752 static 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
784 static 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
793         if (family != AF_INET6)
794                 return -EINVAL;
795
796         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
824 out_free:
825         kfree(slwt->srh);
826         kfree(newts);
827         return err;
828 }
829
830 static 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
837 static 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
859 static 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
890 static 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
917 static 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
927 int __init seg6_local_init(void)
928 {
929         return lwtunnel_encap_add_ops(&seg6_local_ops,
930                                       LWTUNNEL_ENCAP_SEG6_LOCAL);
931 }
932
933 void seg6_local_exit(void)
934 {
935         lwtunnel_encap_del_ops(&seg6_local_ops, LWTUNNEL_ENCAP_SEG6_LOCAL);
936 }