GNU Linux-libre 4.14.290-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         skb_set_transport_header(skb, sizeof(struct ipv6hdr));
409
410         lookup_nexthop(skb, NULL, 0);
411
412         return dst_input(skb);
413
414 drop:
415         kfree_skb(skb);
416         return err;
417 }
418
419 /* encapsulate within an outer IPv6 header and a specified SRH */
420 static int input_action_end_b6_encap(struct sk_buff *skb,
421                                      struct seg6_local_lwt *slwt)
422 {
423         struct ipv6_sr_hdr *srh;
424         int err = -EINVAL;
425
426         srh = get_and_validate_srh(skb);
427         if (!srh)
428                 goto drop;
429
430         advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
431
432         skb_reset_inner_headers(skb);
433         skb->encapsulation = 1;
434
435         err = seg6_do_srh_encap(skb, slwt->srh, IPPROTO_IPV6);
436         if (err)
437                 goto drop;
438
439         skb_set_transport_header(skb, sizeof(struct ipv6hdr));
440
441         lookup_nexthop(skb, NULL, 0);
442
443         return dst_input(skb);
444
445 drop:
446         kfree_skb(skb);
447         return err;
448 }
449
450 static struct seg6_action_desc seg6_action_table[] = {
451         {
452                 .action         = SEG6_LOCAL_ACTION_END,
453                 .attrs          = 0,
454                 .input          = input_action_end,
455         },
456         {
457                 .action         = SEG6_LOCAL_ACTION_END_X,
458                 .attrs          = (1 << SEG6_LOCAL_NH6),
459                 .input          = input_action_end_x,
460         },
461         {
462                 .action         = SEG6_LOCAL_ACTION_END_T,
463                 .attrs          = (1 << SEG6_LOCAL_TABLE),
464                 .input          = input_action_end_t,
465         },
466         {
467                 .action         = SEG6_LOCAL_ACTION_END_DX2,
468                 .attrs          = (1 << SEG6_LOCAL_OIF),
469                 .input          = input_action_end_dx2,
470         },
471         {
472                 .action         = SEG6_LOCAL_ACTION_END_DX6,
473                 .attrs          = (1 << SEG6_LOCAL_NH6),
474                 .input          = input_action_end_dx6,
475         },
476         {
477                 .action         = SEG6_LOCAL_ACTION_END_DX4,
478                 .attrs          = (1 << SEG6_LOCAL_NH4),
479                 .input          = input_action_end_dx4,
480         },
481         {
482                 .action         = SEG6_LOCAL_ACTION_END_DT6,
483                 .attrs          = (1 << SEG6_LOCAL_TABLE),
484                 .input          = input_action_end_dt6,
485         },
486         {
487                 .action         = SEG6_LOCAL_ACTION_END_B6,
488                 .attrs          = (1 << SEG6_LOCAL_SRH),
489                 .input          = input_action_end_b6,
490         },
491         {
492                 .action         = SEG6_LOCAL_ACTION_END_B6_ENCAP,
493                 .attrs          = (1 << SEG6_LOCAL_SRH),
494                 .input          = input_action_end_b6_encap,
495                 .static_headroom        = sizeof(struct ipv6hdr),
496         }
497 };
498
499 static struct seg6_action_desc *__get_action_desc(int action)
500 {
501         struct seg6_action_desc *desc;
502         int i, count;
503
504         count = sizeof(seg6_action_table) / sizeof(struct seg6_action_desc);
505         for (i = 0; i < count; i++) {
506                 desc = &seg6_action_table[i];
507                 if (desc->action == action)
508                         return desc;
509         }
510
511         return NULL;
512 }
513
514 static int seg6_local_input(struct sk_buff *skb)
515 {
516         struct dst_entry *orig_dst = skb_dst(skb);
517         struct seg6_action_desc *desc;
518         struct seg6_local_lwt *slwt;
519
520         if (skb->protocol != htons(ETH_P_IPV6)) {
521                 kfree_skb(skb);
522                 return -EINVAL;
523         }
524
525         slwt = seg6_local_lwtunnel(orig_dst->lwtstate);
526         desc = slwt->desc;
527
528         return desc->input(skb, slwt);
529 }
530
531 static const struct nla_policy seg6_local_policy[SEG6_LOCAL_MAX + 1] = {
532         [SEG6_LOCAL_ACTION]     = { .type = NLA_U32 },
533         [SEG6_LOCAL_SRH]        = { .type = NLA_BINARY },
534         [SEG6_LOCAL_TABLE]      = { .type = NLA_U32 },
535         [SEG6_LOCAL_NH4]        = { .type = NLA_BINARY,
536                                     .len = sizeof(struct in_addr) },
537         [SEG6_LOCAL_NH6]        = { .type = NLA_BINARY,
538                                     .len = sizeof(struct in6_addr) },
539         [SEG6_LOCAL_IIF]        = { .type = NLA_U32 },
540         [SEG6_LOCAL_OIF]        = { .type = NLA_U32 },
541 };
542
543 static int parse_nla_srh(struct nlattr **attrs, struct seg6_local_lwt *slwt)
544 {
545         struct ipv6_sr_hdr *srh;
546         int len;
547
548         srh = nla_data(attrs[SEG6_LOCAL_SRH]);
549         len = nla_len(attrs[SEG6_LOCAL_SRH]);
550
551         /* SRH must contain at least one segment */
552         if (len < sizeof(*srh) + sizeof(struct in6_addr))
553                 return -EINVAL;
554
555         if (!seg6_validate_srh(srh, len))
556                 return -EINVAL;
557
558         slwt->srh = kmalloc(len, GFP_KERNEL);
559         if (!slwt->srh)
560                 return -ENOMEM;
561
562         memcpy(slwt->srh, srh, len);
563
564         slwt->headroom += len;
565
566         return 0;
567 }
568
569 static int put_nla_srh(struct sk_buff *skb, struct seg6_local_lwt *slwt)
570 {
571         struct ipv6_sr_hdr *srh;
572         struct nlattr *nla;
573         int len;
574
575         srh = slwt->srh;
576         len = (srh->hdrlen + 1) << 3;
577
578         nla = nla_reserve(skb, SEG6_LOCAL_SRH, len);
579         if (!nla)
580                 return -EMSGSIZE;
581
582         memcpy(nla_data(nla), srh, len);
583
584         return 0;
585 }
586
587 static int cmp_nla_srh(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
588 {
589         int len = (a->srh->hdrlen + 1) << 3;
590
591         if (len != ((b->srh->hdrlen + 1) << 3))
592                 return 1;
593
594         return memcmp(a->srh, b->srh, len);
595 }
596
597 static int parse_nla_table(struct nlattr **attrs, struct seg6_local_lwt *slwt)
598 {
599         slwt->table = nla_get_u32(attrs[SEG6_LOCAL_TABLE]);
600
601         return 0;
602 }
603
604 static int put_nla_table(struct sk_buff *skb, struct seg6_local_lwt *slwt)
605 {
606         if (nla_put_u32(skb, SEG6_LOCAL_TABLE, slwt->table))
607                 return -EMSGSIZE;
608
609         return 0;
610 }
611
612 static int cmp_nla_table(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
613 {
614         if (a->table != b->table)
615                 return 1;
616
617         return 0;
618 }
619
620 static int parse_nla_nh4(struct nlattr **attrs, struct seg6_local_lwt *slwt)
621 {
622         memcpy(&slwt->nh4, nla_data(attrs[SEG6_LOCAL_NH4]),
623                sizeof(struct in_addr));
624
625         return 0;
626 }
627
628 static int put_nla_nh4(struct sk_buff *skb, struct seg6_local_lwt *slwt)
629 {
630         struct nlattr *nla;
631
632         nla = nla_reserve(skb, SEG6_LOCAL_NH4, sizeof(struct in_addr));
633         if (!nla)
634                 return -EMSGSIZE;
635
636         memcpy(nla_data(nla), &slwt->nh4, sizeof(struct in_addr));
637
638         return 0;
639 }
640
641 static int cmp_nla_nh4(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
642 {
643         return memcmp(&a->nh4, &b->nh4, sizeof(struct in_addr));
644 }
645
646 static int parse_nla_nh6(struct nlattr **attrs, struct seg6_local_lwt *slwt)
647 {
648         memcpy(&slwt->nh6, nla_data(attrs[SEG6_LOCAL_NH6]),
649                sizeof(struct in6_addr));
650
651         return 0;
652 }
653
654 static int put_nla_nh6(struct sk_buff *skb, struct seg6_local_lwt *slwt)
655 {
656         struct nlattr *nla;
657
658         nla = nla_reserve(skb, SEG6_LOCAL_NH6, sizeof(struct in6_addr));
659         if (!nla)
660                 return -EMSGSIZE;
661
662         memcpy(nla_data(nla), &slwt->nh6, sizeof(struct in6_addr));
663
664         return 0;
665 }
666
667 static int cmp_nla_nh6(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
668 {
669         return memcmp(&a->nh6, &b->nh6, sizeof(struct in6_addr));
670 }
671
672 static int parse_nla_iif(struct nlattr **attrs, struct seg6_local_lwt *slwt)
673 {
674         slwt->iif = nla_get_u32(attrs[SEG6_LOCAL_IIF]);
675
676         return 0;
677 }
678
679 static int put_nla_iif(struct sk_buff *skb, struct seg6_local_lwt *slwt)
680 {
681         if (nla_put_u32(skb, SEG6_LOCAL_IIF, slwt->iif))
682                 return -EMSGSIZE;
683
684         return 0;
685 }
686
687 static int cmp_nla_iif(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
688 {
689         if (a->iif != b->iif)
690                 return 1;
691
692         return 0;
693 }
694
695 static int parse_nla_oif(struct nlattr **attrs, struct seg6_local_lwt *slwt)
696 {
697         slwt->oif = nla_get_u32(attrs[SEG6_LOCAL_OIF]);
698
699         return 0;
700 }
701
702 static int put_nla_oif(struct sk_buff *skb, struct seg6_local_lwt *slwt)
703 {
704         if (nla_put_u32(skb, SEG6_LOCAL_OIF, slwt->oif))
705                 return -EMSGSIZE;
706
707         return 0;
708 }
709
710 static int cmp_nla_oif(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
711 {
712         if (a->oif != b->oif)
713                 return 1;
714
715         return 0;
716 }
717
718 struct seg6_action_param {
719         int (*parse)(struct nlattr **attrs, struct seg6_local_lwt *slwt);
720         int (*put)(struct sk_buff *skb, struct seg6_local_lwt *slwt);
721         int (*cmp)(struct seg6_local_lwt *a, struct seg6_local_lwt *b);
722 };
723
724 static struct seg6_action_param seg6_action_params[SEG6_LOCAL_MAX + 1] = {
725         [SEG6_LOCAL_SRH]        = { .parse = parse_nla_srh,
726                                     .put = put_nla_srh,
727                                     .cmp = cmp_nla_srh },
728
729         [SEG6_LOCAL_TABLE]      = { .parse = parse_nla_table,
730                                     .put = put_nla_table,
731                                     .cmp = cmp_nla_table },
732
733         [SEG6_LOCAL_NH4]        = { .parse = parse_nla_nh4,
734                                     .put = put_nla_nh4,
735                                     .cmp = cmp_nla_nh4 },
736
737         [SEG6_LOCAL_NH6]        = { .parse = parse_nla_nh6,
738                                     .put = put_nla_nh6,
739                                     .cmp = cmp_nla_nh6 },
740
741         [SEG6_LOCAL_IIF]        = { .parse = parse_nla_iif,
742                                     .put = put_nla_iif,
743                                     .cmp = cmp_nla_iif },
744
745         [SEG6_LOCAL_OIF]        = { .parse = parse_nla_oif,
746                                     .put = put_nla_oif,
747                                     .cmp = cmp_nla_oif },
748 };
749
750 static int parse_nla_action(struct nlattr **attrs, struct seg6_local_lwt *slwt)
751 {
752         struct seg6_action_param *param;
753         struct seg6_action_desc *desc;
754         int i, err;
755
756         desc = __get_action_desc(slwt->action);
757         if (!desc)
758                 return -EINVAL;
759
760         if (!desc->input)
761                 return -EOPNOTSUPP;
762
763         slwt->desc = desc;
764         slwt->headroom += desc->static_headroom;
765
766         for (i = 0; i < SEG6_LOCAL_MAX + 1; i++) {
767                 if (desc->attrs & (1 << i)) {
768                         if (!attrs[i])
769                                 return -EINVAL;
770
771                         param = &seg6_action_params[i];
772
773                         err = param->parse(attrs, slwt);
774                         if (err < 0)
775                                 return err;
776                 }
777         }
778
779         return 0;
780 }
781
782 static int seg6_local_build_state(struct nlattr *nla, unsigned int family,
783                                   const void *cfg, struct lwtunnel_state **ts,
784                                   struct netlink_ext_ack *extack)
785 {
786         struct nlattr *tb[SEG6_LOCAL_MAX + 1];
787         struct lwtunnel_state *newts;
788         struct seg6_local_lwt *slwt;
789         int err;
790
791         if (family != AF_INET6)
792                 return -EINVAL;
793
794         err = nla_parse_nested(tb, SEG6_LOCAL_MAX, nla, seg6_local_policy,
795                                extack);
796
797         if (err < 0)
798                 return err;
799
800         if (!tb[SEG6_LOCAL_ACTION])
801                 return -EINVAL;
802
803         newts = lwtunnel_state_alloc(sizeof(*slwt));
804         if (!newts)
805                 return -ENOMEM;
806
807         slwt = seg6_local_lwtunnel(newts);
808         slwt->action = nla_get_u32(tb[SEG6_LOCAL_ACTION]);
809
810         err = parse_nla_action(tb, slwt);
811         if (err < 0)
812                 goto out_free;
813
814         newts->type = LWTUNNEL_ENCAP_SEG6_LOCAL;
815         newts->flags = LWTUNNEL_STATE_INPUT_REDIRECT;
816         newts->headroom = slwt->headroom;
817
818         *ts = newts;
819
820         return 0;
821
822 out_free:
823         kfree(slwt->srh);
824         kfree(newts);
825         return err;
826 }
827
828 static void seg6_local_destroy_state(struct lwtunnel_state *lwt)
829 {
830         struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
831
832         kfree(slwt->srh);
833 }
834
835 static int seg6_local_fill_encap(struct sk_buff *skb,
836                                  struct lwtunnel_state *lwt)
837 {
838         struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
839         struct seg6_action_param *param;
840         int i, err;
841
842         if (nla_put_u32(skb, SEG6_LOCAL_ACTION, slwt->action))
843                 return -EMSGSIZE;
844
845         for (i = 0; i < SEG6_LOCAL_MAX + 1; i++) {
846                 if (slwt->desc->attrs & (1 << i)) {
847                         param = &seg6_action_params[i];
848                         err = param->put(skb, slwt);
849                         if (err < 0)
850                                 return err;
851                 }
852         }
853
854         return 0;
855 }
856
857 static int seg6_local_get_encap_size(struct lwtunnel_state *lwt)
858 {
859         struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
860         unsigned long attrs;
861         int nlsize;
862
863         nlsize = nla_total_size(4); /* action */
864
865         attrs = slwt->desc->attrs;
866
867         if (attrs & (1 << SEG6_LOCAL_SRH))
868                 nlsize += nla_total_size((slwt->srh->hdrlen + 1) << 3);
869
870         if (attrs & (1 << SEG6_LOCAL_TABLE))
871                 nlsize += nla_total_size(4);
872
873         if (attrs & (1 << SEG6_LOCAL_NH4))
874                 nlsize += nla_total_size(4);
875
876         if (attrs & (1 << SEG6_LOCAL_NH6))
877                 nlsize += nla_total_size(16);
878
879         if (attrs & (1 << SEG6_LOCAL_IIF))
880                 nlsize += nla_total_size(4);
881
882         if (attrs & (1 << SEG6_LOCAL_OIF))
883                 nlsize += nla_total_size(4);
884
885         return nlsize;
886 }
887
888 static int seg6_local_cmp_encap(struct lwtunnel_state *a,
889                                 struct lwtunnel_state *b)
890 {
891         struct seg6_local_lwt *slwt_a, *slwt_b;
892         struct seg6_action_param *param;
893         int i;
894
895         slwt_a = seg6_local_lwtunnel(a);
896         slwt_b = seg6_local_lwtunnel(b);
897
898         if (slwt_a->action != slwt_b->action)
899                 return 1;
900
901         if (slwt_a->desc->attrs != slwt_b->desc->attrs)
902                 return 1;
903
904         for (i = 0; i < SEG6_LOCAL_MAX + 1; i++) {
905                 if (slwt_a->desc->attrs & (1 << i)) {
906                         param = &seg6_action_params[i];
907                         if (param->cmp(slwt_a, slwt_b))
908                                 return 1;
909                 }
910         }
911
912         return 0;
913 }
914
915 static const struct lwtunnel_encap_ops seg6_local_ops = {
916         .build_state    = seg6_local_build_state,
917         .destroy_state  = seg6_local_destroy_state,
918         .input          = seg6_local_input,
919         .fill_encap     = seg6_local_fill_encap,
920         .get_encap_size = seg6_local_get_encap_size,
921         .cmp_encap      = seg6_local_cmp_encap,
922         .owner          = THIS_MODULE,
923 };
924
925 int __init seg6_local_init(void)
926 {
927         return lwtunnel_encap_add_ops(&seg6_local_ops,
928                                       LWTUNNEL_ENCAP_SEG6_LOCAL);
929 }
930
931 void seg6_local_exit(void)
932 {
933         lwtunnel_encap_del_ops(&seg6_local_ops, LWTUNNEL_ENCAP_SEG6_LOCAL);
934 }