2 * SR-IPv6 implementation
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>
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>
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>
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
{
44 int (*input
)(struct sk_buff
*skb
, struct seg6_local_lwt
*slwt
);
49 struct bpf_prog
*prog
;
53 struct seg6_local_lwt
{
55 struct ipv6_sr_hdr
*srh
;
61 struct bpf_lwt_prog bpf
;
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
;
77 if (ipv6_find_hdr(skb
, &srhoff
, IPPROTO_ROUTING
, NULL
, NULL
) < 0)
80 if (!pskb_may_pull(skb
, srhoff
+ sizeof(*srh
)))
83 srh
= (struct ipv6_sr_hdr
*)(skb
->data
+ srhoff
);
85 len
= (srh
->hdrlen
+ 1) << 3;
87 if (!pskb_may_pull(skb
, srhoff
+ len
))
90 if (!seg6_validate_srh(srh
, len
))
96 static struct ipv6_sr_hdr
*get_and_validate_srh(struct sk_buff
*skb
)
98 struct ipv6_sr_hdr
*srh
;
104 if (srh
->segments_left
== 0)
107 #ifdef CONFIG_IPV6_SEG6_HMAC
108 if (!seg6_hmac_validate_skb(skb
))
115 static bool decap_and_validate(struct sk_buff
*skb
, int proto
)
117 struct ipv6_sr_hdr
*srh
;
118 unsigned int off
= 0;
121 if (srh
&& srh
->segments_left
> 0)
124 #ifdef CONFIG_IPV6_SEG6_HMAC
125 if (srh
&& !seg6_hmac_validate_skb(skb
))
129 if (ipv6_find_hdr(skb
, &off
, proto
, NULL
, NULL
) < 0)
132 if (!pskb_pull(skb
, off
))
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
))
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
;
154 int seg6_lookup_nexthop(struct sk_buff
*skb
, struct in6_addr
*nhaddr
,
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
;
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
;
172 fl6
.flowi6_flags
= FLOWI_FLAG_KNOWN_NH
;
175 dst
= ip6_route_input_lookup(net
, skb
->dev
, &fl6
, skb
, flags
);
177 struct fib6_table
*table
;
179 table
= fib6_get_table(net
, tbl_id
);
183 rt
= ip6_pol_route(net
, table
, 0, &fl6
, skb
, flags
);
187 if (dst
&& dst
->dev
->flags
& IFF_LOOPBACK
&& !dst
->error
) {
194 rt
= net
->ipv6
.ip6_blk_hole_entry
;
200 skb_dst_set(skb
, dst
);
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
);
213 advance_nextseg(srh
, &ipv6_hdr(skb
)->daddr
);
215 seg6_lookup_nexthop(skb
, NULL
, 0);
217 return dst_input(skb
);
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
);
233 advance_nextseg(srh
, &ipv6_hdr(skb
)->daddr
);
235 seg6_lookup_nexthop(skb
, &slwt
->nh6
, 0);
237 return dst_input(skb
);
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
);
252 advance_nextseg(srh
, &ipv6_hdr(skb
)->daddr
);
254 seg6_lookup_nexthop(skb
, NULL
, slwt
->table
);
256 return dst_input(skb
);
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
;
271 if (!decap_and_validate(skb
, NEXTHDR_NONE
))
274 if (!pskb_may_pull(skb
, ETH_HLEN
))
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
284 if (!eth_proto_is_802_3(eth
->h_proto
))
287 odev
= dev_get_by_index_rcu(net
, slwt
->oif
);
291 /* As we accept Ethernet frames, make sure the egress device is of
294 if (odev
->type
!= ARPHRD_ETHER
)
297 if (!(odev
->flags
& IFF_UP
) || !netif_carrier_ok(odev
))
302 if (skb_warn_if_lro(skb
))
305 skb_forward_csum(skb
);
307 if (skb
->len
- ETH_HLEN
> odev
->mtu
)
311 skb
->protocol
= eth
->h_proto
;
313 return dev_queue_xmit(skb
);
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
))
333 if (!pskb_may_pull(skb
, sizeof(struct ipv6hdr
)))
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
))
346 seg6_lookup_nexthop(skb
, nhaddr
, 0);
348 return dst_input(skb
);
354 static int input_action_end_dx4(struct sk_buff
*skb
,
355 struct seg6_local_lwt
*slwt
)
361 if (!decap_and_validate(skb
, IPPROTO_IPIP
))
364 if (!pskb_may_pull(skb
, sizeof(struct iphdr
)))
367 skb
->protocol
= htons(ETH_P_IP
);
371 nhaddr
= slwt
->nh4
.s_addr
?: iph
->daddr
;
375 err
= ip_route_input(skb
, nhaddr
, iph
->saddr
, 0, skb
->dev
);
379 return dst_input(skb
);
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
))
392 if (!pskb_may_pull(skb
, sizeof(struct ipv6hdr
)))
395 seg6_lookup_nexthop(skb
, NULL
, slwt
->table
);
397 return dst_input(skb
);
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
;
410 srh
= get_and_validate_srh(skb
);
414 err
= seg6_do_srh_inline(skb
, slwt
->srh
);
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
);
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
;
437 srh
= get_and_validate_srh(skb
);
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
);
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
);
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
))
473 if (unlikely(!srh_state
->valid
)) {
474 if ((srh_state
->hdrlen
& 7) != 0)
477 srh
->hdrlen
= (u8
)(srh_state
->hdrlen
>> 3);
478 if (!seg6_validate_srh(srh
, (srh
->hdrlen
+ 1) << 3))
481 srh_state
->valid
= 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
;
495 srh
= get_and_validate_srh(skb
);
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
506 srh_state
->srh
= srh
;
507 srh_state
->hdrlen
= srh
->hdrlen
<< 3;
508 srh_state
->valid
= true;
511 bpf_compute_data_pointers(skb
);
512 ret
= bpf_prog_run_save_cb(slwt
->bpf
.prog
, skb
);
522 pr_warn_once("bpf-seg6local: Illegal return value %u\n", ret
);
526 if (srh_state
->srh
&& !seg6_bpf_has_valid_srh(skb
))
530 if (ret
!= BPF_REDIRECT
)
531 seg6_lookup_nexthop(skb
, NULL
, 0);
533 return dst_input(skb
);
541 static struct seg6_action_desc seg6_action_table
[] = {
543 .action
= SEG6_LOCAL_ACTION_END
,
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
;
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
)
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
)) {
622 slwt
= seg6_local_lwtunnel(orig_dst
->lwtstate
);
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
;
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
))
653 if (!seg6_validate_srh(srh
, len
))
656 slwt
->srh
= kmemdup(srh
, len
, GFP_KERNEL
);
660 slwt
->headroom
+= len
;
665 static int put_nla_srh(struct sk_buff
*skb
, struct seg6_local_lwt
*slwt
)
667 struct ipv6_sr_hdr
*srh
;
672 len
= (srh
->hdrlen
+ 1) << 3;
674 nla
= nla_reserve(skb
, SEG6_LOCAL_SRH
, len
);
678 memcpy(nla_data(nla
), srh
, len
);
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))
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
]);
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
))
708 static int cmp_nla_table(struct seg6_local_lwt
*a
, struct seg6_local_lwt
*b
)
710 if (a
->table
!= b
->table
)
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
));
724 static int put_nla_nh4(struct sk_buff
*skb
, struct seg6_local_lwt
*slwt
)
728 nla
= nla_reserve(skb
, SEG6_LOCAL_NH4
, sizeof(struct in_addr
));
732 memcpy(nla_data(nla
), &slwt
->nh4
, sizeof(struct in_addr
));
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
));
750 static int put_nla_nh6(struct sk_buff
*skb
, struct seg6_local_lwt
*slwt
)
754 nla
= nla_reserve(skb
, SEG6_LOCAL_NH6
, sizeof(struct in6_addr
));
758 memcpy(nla_data(nla
), &slwt
->nh6
, sizeof(struct in6_addr
));
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
]);
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
))
783 static int cmp_nla_iif(struct seg6_local_lwt
*a
, struct seg6_local_lwt
*b
)
785 if (a
->iif
!= b
->iif
)
791 static int parse_nla_oif(struct nlattr
**attrs
, struct seg6_local_lwt
*slwt
)
793 slwt
->oif
= nla_get_u32(attrs
[SEG6_LOCAL_OIF
]);
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
))
806 static int cmp_nla_oif(struct seg6_local_lwt
*a
, struct seg6_local_lwt
*b
)
808 if (a
->oif
!= b
->oif
)
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];
828 ret
= nla_parse_nested(tb
, SEG6_LOCAL_BPF_PROG_MAX
,
829 attrs
[SEG6_LOCAL_BPF
], bpf_prog_policy
, NULL
);
833 if (!tb
[SEG6_LOCAL_BPF_PROG
] || !tb
[SEG6_LOCAL_BPF_PROG_NAME
])
836 slwt
->bpf
.name
= nla_memdup(tb
[SEG6_LOCAL_BPF_PROG_NAME
], GFP_KERNEL
);
840 fd
= nla_get_u32(tb
[SEG6_LOCAL_BPF_PROG
]);
841 p
= bpf_prog_get_type(fd
, BPF_PROG_TYPE_LWT_SEG6LOCAL
);
843 kfree(slwt
->bpf
.name
);
851 static int put_nla_bpf(struct sk_buff
*skb
, struct seg6_local_lwt
*slwt
)
858 nest
= nla_nest_start(skb
, SEG6_LOCAL_BPF
);
862 if (nla_put_u32(skb
, SEG6_LOCAL_BPF_PROG
, slwt
->bpf
.prog
->aux
->id
))
865 if (slwt
->bpf
.name
&&
866 nla_put_string(skb
, SEG6_LOCAL_BPF_PROG_NAME
, slwt
->bpf
.name
))
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
)
877 if (!a
->bpf
.name
|| !b
->bpf
.name
)
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
,
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
,
900 .cmp
= cmp_nla_nh4
},
902 [SEG6_LOCAL_NH6
] = { .parse
= parse_nla_nh6
,
904 .cmp
= cmp_nla_nh6
},
906 [SEG6_LOCAL_IIF
] = { .parse
= parse_nla_iif
,
908 .cmp
= cmp_nla_iif
},
910 [SEG6_LOCAL_OIF
] = { .parse
= parse_nla_oif
,
912 .cmp
= cmp_nla_oif
},
914 [SEG6_LOCAL_BPF
] = { .parse
= parse_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
;
926 desc
= __get_action_desc(slwt
->action
);
934 slwt
->headroom
+= desc
->static_headroom
;
936 for (i
= 0; i
< SEG6_LOCAL_MAX
+ 1; i
++) {
937 if (desc
->attrs
& (1 << i
)) {
941 param
= &seg6_action_params
[i
];
943 err
= param
->parse(attrs
, slwt
);
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
;
961 if (family
!= AF_INET6
)
964 err
= nla_parse_nested(tb
, SEG6_LOCAL_MAX
, nla
, seg6_local_policy
,
970 if (!tb
[SEG6_LOCAL_ACTION
])
973 newts
= lwtunnel_state_alloc(sizeof(*slwt
));
977 slwt
= seg6_local_lwtunnel(newts
);
978 slwt
->action
= nla_get_u32(tb
[SEG6_LOCAL_ACTION
]);
980 err
= parse_nla_action(tb
, slwt
);
984 newts
->type
= LWTUNNEL_ENCAP_SEG6_LOCAL
;
985 newts
->flags
= LWTUNNEL_STATE_INPUT_REDIRECT
;
986 newts
->headroom
= slwt
->headroom
;
998 static void seg6_local_destroy_state(struct lwtunnel_state
*lwt
)
1000 struct seg6_local_lwt
*slwt
= seg6_local_lwtunnel(lwt
);
1004 if (slwt
->desc
->attrs
& (1 << SEG6_LOCAL_BPF
)) {
1005 kfree(slwt
->bpf
.name
);
1006 bpf_prog_put(slwt
->bpf
.prog
);
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
;
1019 if (nla_put_u32(skb
, SEG6_LOCAL_ACTION
, slwt
->action
))
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
);
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
;
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
) +
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
;
1077 slwt_a
= seg6_local_lwtunnel(a
);
1078 slwt_b
= seg6_local_lwtunnel(b
);
1080 if (slwt_a
->action
!= slwt_b
->action
)
1083 if (slwt_a
->desc
->attrs
!= slwt_b
->desc
->attrs
)
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
))
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
);