Linux 4.19.133
[linux/fpc-iii.git] / net / ipv6 / seg6_local.c
blob607709a8847cb112aac5410570202164cb4e6a53
1 /*
2 * SR-IPv6 implementation
4 * Authors:
5 * David Lebrun <david.lebrun@uclouvain.be>
6 * eBPF support: Mathieu Xhonneux <m.xhonneux@gmail.com>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
15 #include <linux/types.h>
16 #include <linux/skbuff.h>
17 #include <linux/net.h>
18 #include <linux/module.h>
19 #include <net/ip.h>
20 #include <net/lwtunnel.h>
21 #include <net/netevent.h>
22 #include <net/netns/generic.h>
23 #include <net/ip6_fib.h>
24 #include <net/route.h>
25 #include <net/seg6.h>
26 #include <linux/seg6.h>
27 #include <linux/seg6_local.h>
28 #include <net/addrconf.h>
29 #include <net/ip6_route.h>
30 #include <net/dst_cache.h>
31 #include <net/ip_tunnels.h>
32 #ifdef CONFIG_IPV6_SEG6_HMAC
33 #include <net/seg6_hmac.h>
34 #endif
35 #include <net/seg6_local.h>
36 #include <linux/etherdevice.h>
37 #include <linux/bpf.h>
39 struct seg6_local_lwt;
41 struct seg6_action_desc {
42 int action;
43 unsigned long attrs;
44 int (*input)(struct sk_buff *skb, struct seg6_local_lwt *slwt);
45 int static_headroom;
48 struct bpf_lwt_prog {
49 struct bpf_prog *prog;
50 char *name;
53 struct seg6_local_lwt {
54 int action;
55 struct ipv6_sr_hdr *srh;
56 int table;
57 struct in_addr nh4;
58 struct in6_addr nh6;
59 int iif;
60 int oif;
61 struct bpf_lwt_prog bpf;
63 int headroom;
64 struct seg6_action_desc *desc;
67 static struct seg6_local_lwt *seg6_local_lwtunnel(struct lwtunnel_state *lwt)
69 return (struct seg6_local_lwt *)lwt->data;
72 static struct ipv6_sr_hdr *get_srh(struct sk_buff *skb)
74 struct ipv6_sr_hdr *srh;
75 int len, srhoff = 0;
77 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
78 return NULL;
80 if (!pskb_may_pull(skb, srhoff + sizeof(*srh)))
81 return NULL;
83 srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
85 len = (srh->hdrlen + 1) << 3;
87 if (!pskb_may_pull(skb, srhoff + len))
88 return NULL;
90 if (!seg6_validate_srh(srh, len))
91 return NULL;
93 return srh;
96 static struct ipv6_sr_hdr *get_and_validate_srh(struct sk_buff *skb)
98 struct ipv6_sr_hdr *srh;
100 srh = get_srh(skb);
101 if (!srh)
102 return NULL;
104 if (srh->segments_left == 0)
105 return NULL;
107 #ifdef CONFIG_IPV6_SEG6_HMAC
108 if (!seg6_hmac_validate_skb(skb))
109 return NULL;
110 #endif
112 return srh;
115 static bool decap_and_validate(struct sk_buff *skb, int proto)
117 struct ipv6_sr_hdr *srh;
118 unsigned int off = 0;
120 srh = get_srh(skb);
121 if (srh && srh->segments_left > 0)
122 return false;
124 #ifdef CONFIG_IPV6_SEG6_HMAC
125 if (srh && !seg6_hmac_validate_skb(skb))
126 return false;
127 #endif
129 if (ipv6_find_hdr(skb, &off, proto, NULL, NULL) < 0)
130 return false;
132 if (!pskb_pull(skb, off))
133 return false;
135 skb_postpull_rcsum(skb, skb_network_header(skb), off);
137 skb_reset_network_header(skb);
138 skb_reset_transport_header(skb);
139 if (iptunnel_pull_offloads(skb))
140 return false;
142 return true;
145 static void advance_nextseg(struct ipv6_sr_hdr *srh, struct in6_addr *daddr)
147 struct in6_addr *addr;
149 srh->segments_left--;
150 addr = srh->segments + srh->segments_left;
151 *daddr = *addr;
154 int seg6_lookup_nexthop(struct sk_buff *skb, struct in6_addr *nhaddr,
155 u32 tbl_id)
157 struct net *net = dev_net(skb->dev);
158 struct ipv6hdr *hdr = ipv6_hdr(skb);
159 int flags = RT6_LOOKUP_F_HAS_SADDR;
160 struct dst_entry *dst = NULL;
161 struct rt6_info *rt;
162 struct flowi6 fl6;
164 fl6.flowi6_iif = skb->dev->ifindex;
165 fl6.daddr = nhaddr ? *nhaddr : hdr->daddr;
166 fl6.saddr = hdr->saddr;
167 fl6.flowlabel = ip6_flowinfo(hdr);
168 fl6.flowi6_mark = skb->mark;
169 fl6.flowi6_proto = hdr->nexthdr;
171 if (nhaddr)
172 fl6.flowi6_flags = FLOWI_FLAG_KNOWN_NH;
174 if (!tbl_id) {
175 dst = ip6_route_input_lookup(net, skb->dev, &fl6, skb, flags);
176 } else {
177 struct fib6_table *table;
179 table = fib6_get_table(net, tbl_id);
180 if (!table)
181 goto out;
183 rt = ip6_pol_route(net, table, 0, &fl6, skb, flags);
184 dst = &rt->dst;
187 if (dst && dst->dev->flags & IFF_LOOPBACK && !dst->error) {
188 dst_release(dst);
189 dst = NULL;
192 out:
193 if (!dst) {
194 rt = net->ipv6.ip6_blk_hole_entry;
195 dst = &rt->dst;
196 dst_hold(dst);
199 skb_dst_drop(skb);
200 skb_dst_set(skb, dst);
201 return dst->error;
204 /* regular endpoint function */
205 static int input_action_end(struct sk_buff *skb, struct seg6_local_lwt *slwt)
207 struct ipv6_sr_hdr *srh;
209 srh = get_and_validate_srh(skb);
210 if (!srh)
211 goto drop;
213 advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
215 seg6_lookup_nexthop(skb, NULL, 0);
217 return dst_input(skb);
219 drop:
220 kfree_skb(skb);
221 return -EINVAL;
224 /* regular endpoint, and forward to specified nexthop */
225 static int input_action_end_x(struct sk_buff *skb, struct seg6_local_lwt *slwt)
227 struct ipv6_sr_hdr *srh;
229 srh = get_and_validate_srh(skb);
230 if (!srh)
231 goto drop;
233 advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
235 seg6_lookup_nexthop(skb, &slwt->nh6, 0);
237 return dst_input(skb);
239 drop:
240 kfree_skb(skb);
241 return -EINVAL;
244 static int input_action_end_t(struct sk_buff *skb, struct seg6_local_lwt *slwt)
246 struct ipv6_sr_hdr *srh;
248 srh = get_and_validate_srh(skb);
249 if (!srh)
250 goto drop;
252 advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
254 seg6_lookup_nexthop(skb, NULL, slwt->table);
256 return dst_input(skb);
258 drop:
259 kfree_skb(skb);
260 return -EINVAL;
263 /* decapsulate and forward inner L2 frame on specified interface */
264 static int input_action_end_dx2(struct sk_buff *skb,
265 struct seg6_local_lwt *slwt)
267 struct net *net = dev_net(skb->dev);
268 struct net_device *odev;
269 struct ethhdr *eth;
271 if (!decap_and_validate(skb, NEXTHDR_NONE))
272 goto drop;
274 if (!pskb_may_pull(skb, ETH_HLEN))
275 goto drop;
277 skb_reset_mac_header(skb);
278 eth = (struct ethhdr *)skb->data;
280 /* To determine the frame's protocol, we assume it is 802.3. This avoids
281 * a call to eth_type_trans(), which is not really relevant for our
282 * use case.
284 if (!eth_proto_is_802_3(eth->h_proto))
285 goto drop;
287 odev = dev_get_by_index_rcu(net, slwt->oif);
288 if (!odev)
289 goto drop;
291 /* As we accept Ethernet frames, make sure the egress device is of
292 * the correct type.
294 if (odev->type != ARPHRD_ETHER)
295 goto drop;
297 if (!(odev->flags & IFF_UP) || !netif_carrier_ok(odev))
298 goto drop;
300 skb_orphan(skb);
302 if (skb_warn_if_lro(skb))
303 goto drop;
305 skb_forward_csum(skb);
307 if (skb->len - ETH_HLEN > odev->mtu)
308 goto drop;
310 skb->dev = odev;
311 skb->protocol = eth->h_proto;
313 return dev_queue_xmit(skb);
315 drop:
316 kfree_skb(skb);
317 return -EINVAL;
320 /* decapsulate and forward to specified nexthop */
321 static int input_action_end_dx6(struct sk_buff *skb,
322 struct seg6_local_lwt *slwt)
324 struct in6_addr *nhaddr = NULL;
326 /* this function accepts IPv6 encapsulated packets, with either
327 * an SRH with SL=0, or no SRH.
330 if (!decap_and_validate(skb, IPPROTO_IPV6))
331 goto drop;
333 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
334 goto drop;
336 /* The inner packet is not associated to any local interface,
337 * so we do not call netif_rx().
339 * If slwt->nh6 is set to ::, then lookup the nexthop for the
340 * inner packet's DA. Otherwise, use the specified nexthop.
343 if (!ipv6_addr_any(&slwt->nh6))
344 nhaddr = &slwt->nh6;
346 seg6_lookup_nexthop(skb, nhaddr, 0);
348 return dst_input(skb);
349 drop:
350 kfree_skb(skb);
351 return -EINVAL;
354 static int input_action_end_dx4(struct sk_buff *skb,
355 struct seg6_local_lwt *slwt)
357 struct iphdr *iph;
358 __be32 nhaddr;
359 int err;
361 if (!decap_and_validate(skb, IPPROTO_IPIP))
362 goto drop;
364 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
365 goto drop;
367 skb->protocol = htons(ETH_P_IP);
369 iph = ip_hdr(skb);
371 nhaddr = slwt->nh4.s_addr ?: iph->daddr;
373 skb_dst_drop(skb);
375 err = ip_route_input(skb, nhaddr, iph->saddr, 0, skb->dev);
376 if (err)
377 goto drop;
379 return dst_input(skb);
381 drop:
382 kfree_skb(skb);
383 return -EINVAL;
386 static int input_action_end_dt6(struct sk_buff *skb,
387 struct seg6_local_lwt *slwt)
389 if (!decap_and_validate(skb, IPPROTO_IPV6))
390 goto drop;
392 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
393 goto drop;
395 seg6_lookup_nexthop(skb, NULL, slwt->table);
397 return dst_input(skb);
399 drop:
400 kfree_skb(skb);
401 return -EINVAL;
404 /* push an SRH on top of the current one */
405 static int input_action_end_b6(struct sk_buff *skb, struct seg6_local_lwt *slwt)
407 struct ipv6_sr_hdr *srh;
408 int err = -EINVAL;
410 srh = get_and_validate_srh(skb);
411 if (!srh)
412 goto drop;
414 err = seg6_do_srh_inline(skb, slwt->srh);
415 if (err)
416 goto drop;
418 ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
419 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
421 seg6_lookup_nexthop(skb, NULL, 0);
423 return dst_input(skb);
425 drop:
426 kfree_skb(skb);
427 return err;
430 /* encapsulate within an outer IPv6 header and a specified SRH */
431 static int input_action_end_b6_encap(struct sk_buff *skb,
432 struct seg6_local_lwt *slwt)
434 struct ipv6_sr_hdr *srh;
435 int err = -EINVAL;
437 srh = get_and_validate_srh(skb);
438 if (!srh)
439 goto drop;
441 advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
443 skb_reset_inner_headers(skb);
444 skb->encapsulation = 1;
446 err = seg6_do_srh_encap(skb, slwt->srh, IPPROTO_IPV6);
447 if (err)
448 goto drop;
450 ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
451 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
453 seg6_lookup_nexthop(skb, NULL, 0);
455 return dst_input(skb);
457 drop:
458 kfree_skb(skb);
459 return err;
462 DEFINE_PER_CPU(struct seg6_bpf_srh_state, seg6_bpf_srh_states);
464 bool seg6_bpf_has_valid_srh(struct sk_buff *skb)
466 struct seg6_bpf_srh_state *srh_state =
467 this_cpu_ptr(&seg6_bpf_srh_states);
468 struct ipv6_sr_hdr *srh = srh_state->srh;
470 if (unlikely(srh == NULL))
471 return false;
473 if (unlikely(!srh_state->valid)) {
474 if ((srh_state->hdrlen & 7) != 0)
475 return false;
477 srh->hdrlen = (u8)(srh_state->hdrlen >> 3);
478 if (!seg6_validate_srh(srh, (srh->hdrlen + 1) << 3))
479 return false;
481 srh_state->valid = true;
484 return true;
487 static int input_action_end_bpf(struct sk_buff *skb,
488 struct seg6_local_lwt *slwt)
490 struct seg6_bpf_srh_state *srh_state =
491 this_cpu_ptr(&seg6_bpf_srh_states);
492 struct ipv6_sr_hdr *srh;
493 int ret;
495 srh = get_and_validate_srh(skb);
496 if (!srh) {
497 kfree_skb(skb);
498 return -EINVAL;
500 advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
502 /* preempt_disable is needed to protect the per-CPU buffer srh_state,
503 * which is also accessed by the bpf_lwt_seg6_* helpers
505 preempt_disable();
506 srh_state->srh = srh;
507 srh_state->hdrlen = srh->hdrlen << 3;
508 srh_state->valid = true;
510 rcu_read_lock();
511 bpf_compute_data_pointers(skb);
512 ret = bpf_prog_run_save_cb(slwt->bpf.prog, skb);
513 rcu_read_unlock();
515 switch (ret) {
516 case BPF_OK:
517 case BPF_REDIRECT:
518 break;
519 case BPF_DROP:
520 goto drop;
521 default:
522 pr_warn_once("bpf-seg6local: Illegal return value %u\n", ret);
523 goto drop;
526 if (srh_state->srh && !seg6_bpf_has_valid_srh(skb))
527 goto drop;
529 preempt_enable();
530 if (ret != BPF_REDIRECT)
531 seg6_lookup_nexthop(skb, NULL, 0);
533 return dst_input(skb);
535 drop:
536 preempt_enable();
537 kfree_skb(skb);
538 return -EINVAL;
541 static struct seg6_action_desc seg6_action_table[] = {
543 .action = SEG6_LOCAL_ACTION_END,
544 .attrs = 0,
545 .input = input_action_end,
548 .action = SEG6_LOCAL_ACTION_END_X,
549 .attrs = (1 << SEG6_LOCAL_NH6),
550 .input = input_action_end_x,
553 .action = SEG6_LOCAL_ACTION_END_T,
554 .attrs = (1 << SEG6_LOCAL_TABLE),
555 .input = input_action_end_t,
558 .action = SEG6_LOCAL_ACTION_END_DX2,
559 .attrs = (1 << SEG6_LOCAL_OIF),
560 .input = input_action_end_dx2,
563 .action = SEG6_LOCAL_ACTION_END_DX6,
564 .attrs = (1 << SEG6_LOCAL_NH6),
565 .input = input_action_end_dx6,
568 .action = SEG6_LOCAL_ACTION_END_DX4,
569 .attrs = (1 << SEG6_LOCAL_NH4),
570 .input = input_action_end_dx4,
573 .action = SEG6_LOCAL_ACTION_END_DT6,
574 .attrs = (1 << SEG6_LOCAL_TABLE),
575 .input = input_action_end_dt6,
578 .action = SEG6_LOCAL_ACTION_END_B6,
579 .attrs = (1 << SEG6_LOCAL_SRH),
580 .input = input_action_end_b6,
583 .action = SEG6_LOCAL_ACTION_END_B6_ENCAP,
584 .attrs = (1 << SEG6_LOCAL_SRH),
585 .input = input_action_end_b6_encap,
586 .static_headroom = sizeof(struct ipv6hdr),
589 .action = SEG6_LOCAL_ACTION_END_BPF,
590 .attrs = (1 << SEG6_LOCAL_BPF),
591 .input = input_action_end_bpf,
596 static struct seg6_action_desc *__get_action_desc(int action)
598 struct seg6_action_desc *desc;
599 int i, count;
601 count = ARRAY_SIZE(seg6_action_table);
602 for (i = 0; i < count; i++) {
603 desc = &seg6_action_table[i];
604 if (desc->action == action)
605 return desc;
608 return NULL;
611 static int seg6_local_input(struct sk_buff *skb)
613 struct dst_entry *orig_dst = skb_dst(skb);
614 struct seg6_action_desc *desc;
615 struct seg6_local_lwt *slwt;
617 if (skb->protocol != htons(ETH_P_IPV6)) {
618 kfree_skb(skb);
619 return -EINVAL;
622 slwt = seg6_local_lwtunnel(orig_dst->lwtstate);
623 desc = slwt->desc;
625 return desc->input(skb, slwt);
628 static const struct nla_policy seg6_local_policy[SEG6_LOCAL_MAX + 1] = {
629 [SEG6_LOCAL_ACTION] = { .type = NLA_U32 },
630 [SEG6_LOCAL_SRH] = { .type = NLA_BINARY },
631 [SEG6_LOCAL_TABLE] = { .type = NLA_U32 },
632 [SEG6_LOCAL_NH4] = { .type = NLA_BINARY,
633 .len = sizeof(struct in_addr) },
634 [SEG6_LOCAL_NH6] = { .type = NLA_BINARY,
635 .len = sizeof(struct in6_addr) },
636 [SEG6_LOCAL_IIF] = { .type = NLA_U32 },
637 [SEG6_LOCAL_OIF] = { .type = NLA_U32 },
638 [SEG6_LOCAL_BPF] = { .type = NLA_NESTED },
641 static int parse_nla_srh(struct nlattr **attrs, struct seg6_local_lwt *slwt)
643 struct ipv6_sr_hdr *srh;
644 int len;
646 srh = nla_data(attrs[SEG6_LOCAL_SRH]);
647 len = nla_len(attrs[SEG6_LOCAL_SRH]);
649 /* SRH must contain at least one segment */
650 if (len < sizeof(*srh) + sizeof(struct in6_addr))
651 return -EINVAL;
653 if (!seg6_validate_srh(srh, len))
654 return -EINVAL;
656 slwt->srh = kmemdup(srh, len, GFP_KERNEL);
657 if (!slwt->srh)
658 return -ENOMEM;
660 slwt->headroom += len;
662 return 0;
665 static int put_nla_srh(struct sk_buff *skb, struct seg6_local_lwt *slwt)
667 struct ipv6_sr_hdr *srh;
668 struct nlattr *nla;
669 int len;
671 srh = slwt->srh;
672 len = (srh->hdrlen + 1) << 3;
674 nla = nla_reserve(skb, SEG6_LOCAL_SRH, len);
675 if (!nla)
676 return -EMSGSIZE;
678 memcpy(nla_data(nla), srh, len);
680 return 0;
683 static int cmp_nla_srh(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
685 int len = (a->srh->hdrlen + 1) << 3;
687 if (len != ((b->srh->hdrlen + 1) << 3))
688 return 1;
690 return memcmp(a->srh, b->srh, len);
693 static int parse_nla_table(struct nlattr **attrs, struct seg6_local_lwt *slwt)
695 slwt->table = nla_get_u32(attrs[SEG6_LOCAL_TABLE]);
697 return 0;
700 static int put_nla_table(struct sk_buff *skb, struct seg6_local_lwt *slwt)
702 if (nla_put_u32(skb, SEG6_LOCAL_TABLE, slwt->table))
703 return -EMSGSIZE;
705 return 0;
708 static int cmp_nla_table(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
710 if (a->table != b->table)
711 return 1;
713 return 0;
716 static int parse_nla_nh4(struct nlattr **attrs, struct seg6_local_lwt *slwt)
718 memcpy(&slwt->nh4, nla_data(attrs[SEG6_LOCAL_NH4]),
719 sizeof(struct in_addr));
721 return 0;
724 static int put_nla_nh4(struct sk_buff *skb, struct seg6_local_lwt *slwt)
726 struct nlattr *nla;
728 nla = nla_reserve(skb, SEG6_LOCAL_NH4, sizeof(struct in_addr));
729 if (!nla)
730 return -EMSGSIZE;
732 memcpy(nla_data(nla), &slwt->nh4, sizeof(struct in_addr));
734 return 0;
737 static int cmp_nla_nh4(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
739 return memcmp(&a->nh4, &b->nh4, sizeof(struct in_addr));
742 static int parse_nla_nh6(struct nlattr **attrs, struct seg6_local_lwt *slwt)
744 memcpy(&slwt->nh6, nla_data(attrs[SEG6_LOCAL_NH6]),
745 sizeof(struct in6_addr));
747 return 0;
750 static int put_nla_nh6(struct sk_buff *skb, struct seg6_local_lwt *slwt)
752 struct nlattr *nla;
754 nla = nla_reserve(skb, SEG6_LOCAL_NH6, sizeof(struct in6_addr));
755 if (!nla)
756 return -EMSGSIZE;
758 memcpy(nla_data(nla), &slwt->nh6, sizeof(struct in6_addr));
760 return 0;
763 static int cmp_nla_nh6(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
765 return memcmp(&a->nh6, &b->nh6, sizeof(struct in6_addr));
768 static int parse_nla_iif(struct nlattr **attrs, struct seg6_local_lwt *slwt)
770 slwt->iif = nla_get_u32(attrs[SEG6_LOCAL_IIF]);
772 return 0;
775 static int put_nla_iif(struct sk_buff *skb, struct seg6_local_lwt *slwt)
777 if (nla_put_u32(skb, SEG6_LOCAL_IIF, slwt->iif))
778 return -EMSGSIZE;
780 return 0;
783 static int cmp_nla_iif(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
785 if (a->iif != b->iif)
786 return 1;
788 return 0;
791 static int parse_nla_oif(struct nlattr **attrs, struct seg6_local_lwt *slwt)
793 slwt->oif = nla_get_u32(attrs[SEG6_LOCAL_OIF]);
795 return 0;
798 static int put_nla_oif(struct sk_buff *skb, struct seg6_local_lwt *slwt)
800 if (nla_put_u32(skb, SEG6_LOCAL_OIF, slwt->oif))
801 return -EMSGSIZE;
803 return 0;
806 static int cmp_nla_oif(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
808 if (a->oif != b->oif)
809 return 1;
811 return 0;
814 #define MAX_PROG_NAME 256
815 static const struct nla_policy bpf_prog_policy[SEG6_LOCAL_BPF_PROG_MAX + 1] = {
816 [SEG6_LOCAL_BPF_PROG] = { .type = NLA_U32, },
817 [SEG6_LOCAL_BPF_PROG_NAME] = { .type = NLA_NUL_STRING,
818 .len = MAX_PROG_NAME },
821 static int parse_nla_bpf(struct nlattr **attrs, struct seg6_local_lwt *slwt)
823 struct nlattr *tb[SEG6_LOCAL_BPF_PROG_MAX + 1];
824 struct bpf_prog *p;
825 int ret;
826 u32 fd;
828 ret = nla_parse_nested(tb, SEG6_LOCAL_BPF_PROG_MAX,
829 attrs[SEG6_LOCAL_BPF], bpf_prog_policy, NULL);
830 if (ret < 0)
831 return ret;
833 if (!tb[SEG6_LOCAL_BPF_PROG] || !tb[SEG6_LOCAL_BPF_PROG_NAME])
834 return -EINVAL;
836 slwt->bpf.name = nla_memdup(tb[SEG6_LOCAL_BPF_PROG_NAME], GFP_KERNEL);
837 if (!slwt->bpf.name)
838 return -ENOMEM;
840 fd = nla_get_u32(tb[SEG6_LOCAL_BPF_PROG]);
841 p = bpf_prog_get_type(fd, BPF_PROG_TYPE_LWT_SEG6LOCAL);
842 if (IS_ERR(p)) {
843 kfree(slwt->bpf.name);
844 return PTR_ERR(p);
847 slwt->bpf.prog = p;
848 return 0;
851 static int put_nla_bpf(struct sk_buff *skb, struct seg6_local_lwt *slwt)
853 struct nlattr *nest;
855 if (!slwt->bpf.prog)
856 return 0;
858 nest = nla_nest_start(skb, SEG6_LOCAL_BPF);
859 if (!nest)
860 return -EMSGSIZE;
862 if (nla_put_u32(skb, SEG6_LOCAL_BPF_PROG, slwt->bpf.prog->aux->id))
863 return -EMSGSIZE;
865 if (slwt->bpf.name &&
866 nla_put_string(skb, SEG6_LOCAL_BPF_PROG_NAME, slwt->bpf.name))
867 return -EMSGSIZE;
869 return nla_nest_end(skb, nest);
872 static int cmp_nla_bpf(struct seg6_local_lwt *a, struct seg6_local_lwt *b)
874 if (!a->bpf.name && !b->bpf.name)
875 return 0;
877 if (!a->bpf.name || !b->bpf.name)
878 return 1;
880 return strcmp(a->bpf.name, b->bpf.name);
883 struct seg6_action_param {
884 int (*parse)(struct nlattr **attrs, struct seg6_local_lwt *slwt);
885 int (*put)(struct sk_buff *skb, struct seg6_local_lwt *slwt);
886 int (*cmp)(struct seg6_local_lwt *a, struct seg6_local_lwt *b);
889 static struct seg6_action_param seg6_action_params[SEG6_LOCAL_MAX + 1] = {
890 [SEG6_LOCAL_SRH] = { .parse = parse_nla_srh,
891 .put = put_nla_srh,
892 .cmp = cmp_nla_srh },
894 [SEG6_LOCAL_TABLE] = { .parse = parse_nla_table,
895 .put = put_nla_table,
896 .cmp = cmp_nla_table },
898 [SEG6_LOCAL_NH4] = { .parse = parse_nla_nh4,
899 .put = put_nla_nh4,
900 .cmp = cmp_nla_nh4 },
902 [SEG6_LOCAL_NH6] = { .parse = parse_nla_nh6,
903 .put = put_nla_nh6,
904 .cmp = cmp_nla_nh6 },
906 [SEG6_LOCAL_IIF] = { .parse = parse_nla_iif,
907 .put = put_nla_iif,
908 .cmp = cmp_nla_iif },
910 [SEG6_LOCAL_OIF] = { .parse = parse_nla_oif,
911 .put = put_nla_oif,
912 .cmp = cmp_nla_oif },
914 [SEG6_LOCAL_BPF] = { .parse = parse_nla_bpf,
915 .put = put_nla_bpf,
916 .cmp = cmp_nla_bpf },
920 static int parse_nla_action(struct nlattr **attrs, struct seg6_local_lwt *slwt)
922 struct seg6_action_param *param;
923 struct seg6_action_desc *desc;
924 int i, err;
926 desc = __get_action_desc(slwt->action);
927 if (!desc)
928 return -EINVAL;
930 if (!desc->input)
931 return -EOPNOTSUPP;
933 slwt->desc = desc;
934 slwt->headroom += desc->static_headroom;
936 for (i = 0; i < SEG6_LOCAL_MAX + 1; i++) {
937 if (desc->attrs & (1 << i)) {
938 if (!attrs[i])
939 return -EINVAL;
941 param = &seg6_action_params[i];
943 err = param->parse(attrs, slwt);
944 if (err < 0)
945 return err;
949 return 0;
952 static int seg6_local_build_state(struct nlattr *nla, unsigned int family,
953 const void *cfg, struct lwtunnel_state **ts,
954 struct netlink_ext_ack *extack)
956 struct nlattr *tb[SEG6_LOCAL_MAX + 1];
957 struct lwtunnel_state *newts;
958 struct seg6_local_lwt *slwt;
959 int err;
961 if (family != AF_INET6)
962 return -EINVAL;
964 err = nla_parse_nested(tb, SEG6_LOCAL_MAX, nla, seg6_local_policy,
965 extack);
967 if (err < 0)
968 return err;
970 if (!tb[SEG6_LOCAL_ACTION])
971 return -EINVAL;
973 newts = lwtunnel_state_alloc(sizeof(*slwt));
974 if (!newts)
975 return -ENOMEM;
977 slwt = seg6_local_lwtunnel(newts);
978 slwt->action = nla_get_u32(tb[SEG6_LOCAL_ACTION]);
980 err = parse_nla_action(tb, slwt);
981 if (err < 0)
982 goto out_free;
984 newts->type = LWTUNNEL_ENCAP_SEG6_LOCAL;
985 newts->flags = LWTUNNEL_STATE_INPUT_REDIRECT;
986 newts->headroom = slwt->headroom;
988 *ts = newts;
990 return 0;
992 out_free:
993 kfree(slwt->srh);
994 kfree(newts);
995 return err;
998 static void seg6_local_destroy_state(struct lwtunnel_state *lwt)
1000 struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
1002 kfree(slwt->srh);
1004 if (slwt->desc->attrs & (1 << SEG6_LOCAL_BPF)) {
1005 kfree(slwt->bpf.name);
1006 bpf_prog_put(slwt->bpf.prog);
1009 return;
1012 static int seg6_local_fill_encap(struct sk_buff *skb,
1013 struct lwtunnel_state *lwt)
1015 struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
1016 struct seg6_action_param *param;
1017 int i, err;
1019 if (nla_put_u32(skb, SEG6_LOCAL_ACTION, slwt->action))
1020 return -EMSGSIZE;
1022 for (i = 0; i < SEG6_LOCAL_MAX + 1; i++) {
1023 if (slwt->desc->attrs & (1 << i)) {
1024 param = &seg6_action_params[i];
1025 err = param->put(skb, slwt);
1026 if (err < 0)
1027 return err;
1031 return 0;
1034 static int seg6_local_get_encap_size(struct lwtunnel_state *lwt)
1036 struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt);
1037 unsigned long attrs;
1038 int nlsize;
1040 nlsize = nla_total_size(4); /* action */
1042 attrs = slwt->desc->attrs;
1044 if (attrs & (1 << SEG6_LOCAL_SRH))
1045 nlsize += nla_total_size((slwt->srh->hdrlen + 1) << 3);
1047 if (attrs & (1 << SEG6_LOCAL_TABLE))
1048 nlsize += nla_total_size(4);
1050 if (attrs & (1 << SEG6_LOCAL_NH4))
1051 nlsize += nla_total_size(4);
1053 if (attrs & (1 << SEG6_LOCAL_NH6))
1054 nlsize += nla_total_size(16);
1056 if (attrs & (1 << SEG6_LOCAL_IIF))
1057 nlsize += nla_total_size(4);
1059 if (attrs & (1 << SEG6_LOCAL_OIF))
1060 nlsize += nla_total_size(4);
1062 if (attrs & (1 << SEG6_LOCAL_BPF))
1063 nlsize += nla_total_size(sizeof(struct nlattr)) +
1064 nla_total_size(MAX_PROG_NAME) +
1065 nla_total_size(4);
1067 return nlsize;
1070 static int seg6_local_cmp_encap(struct lwtunnel_state *a,
1071 struct lwtunnel_state *b)
1073 struct seg6_local_lwt *slwt_a, *slwt_b;
1074 struct seg6_action_param *param;
1075 int i;
1077 slwt_a = seg6_local_lwtunnel(a);
1078 slwt_b = seg6_local_lwtunnel(b);
1080 if (slwt_a->action != slwt_b->action)
1081 return 1;
1083 if (slwt_a->desc->attrs != slwt_b->desc->attrs)
1084 return 1;
1086 for (i = 0; i < SEG6_LOCAL_MAX + 1; i++) {
1087 if (slwt_a->desc->attrs & (1 << i)) {
1088 param = &seg6_action_params[i];
1089 if (param->cmp(slwt_a, slwt_b))
1090 return 1;
1094 return 0;
1097 static const struct lwtunnel_encap_ops seg6_local_ops = {
1098 .build_state = seg6_local_build_state,
1099 .destroy_state = seg6_local_destroy_state,
1100 .input = seg6_local_input,
1101 .fill_encap = seg6_local_fill_encap,
1102 .get_encap_size = seg6_local_get_encap_size,
1103 .cmp_encap = seg6_local_cmp_encap,
1104 .owner = THIS_MODULE,
1107 int __init seg6_local_init(void)
1109 return lwtunnel_encap_add_ops(&seg6_local_ops,
1110 LWTUNNEL_ENCAP_SEG6_LOCAL);
1113 void seg6_local_exit(void)
1115 lwtunnel_encap_del_ops(&seg6_local_ops, LWTUNNEL_ENCAP_SEG6_LOCAL);