1 // SPDX-License-Identifier: GPL-2.0-only
5 #include <linux/kernel.h>
6 #include <linux/module.h>
7 #include <linux/skbuff.h>
8 #include <linux/types.h>
10 #include <net/lwtunnel.h>
12 #include <net/ip6_route.h>
13 #include <net/ipv6_stubs.h>
16 struct bpf_prog *prog;
21 struct bpf_lwt_prog in;
22 struct bpf_lwt_prog out;
23 struct bpf_lwt_prog xmit;
27 #define MAX_PROG_NAME 256
29 static inline struct bpf_lwt *bpf_lwt_lwtunnel(struct lwtunnel_state *lwt)
31 return (struct bpf_lwt *)lwt->data;
34 #define NO_REDIRECT false
35 #define CAN_REDIRECT true
37 static int run_lwt_bpf(struct sk_buff *skb, struct bpf_lwt_prog *lwt,
38 struct dst_entry *dst, bool can_redirect)
42 /* Preempt disable is needed to protect per-cpu redirect_info between
43 * BPF prog and skb_do_redirect(). The call_rcu in bpf_prog_put() and
44 * access to maps strictly require a rcu_read_lock() for protection,
45 * mixing with BH RCU lock doesn't work.
48 bpf_compute_data_pointers(skb);
49 ret = bpf_prog_run_save_cb(lwt->prog, skb);
57 if (unlikely(!can_redirect)) {
58 pr_warn_once("Illegal redirect return code in prog %s\n",
59 lwt->name ? : "<unknown>");
62 skb_reset_mac_header(skb);
63 ret = skb_do_redirect(skb);
75 pr_warn_once("bpf-lwt: Illegal return value %u, expect packet loss\n", ret);
86 static int bpf_lwt_input_reroute(struct sk_buff *skb)
90 if (skb->protocol == htons(ETH_P_IP)) {
91 struct iphdr *iph = ip_hdr(skb);
93 err = ip_route_input_noref(skb, iph->daddr, iph->saddr,
94 iph->tos, skb_dst(skb)->dev);
95 } else if (skb->protocol == htons(ETH_P_IPV6)) {
96 err = ipv6_stub->ipv6_route_input(skb);
103 return dst_input(skb);
110 static int bpf_input(struct sk_buff *skb)
112 struct dst_entry *dst = skb_dst(skb);
116 bpf = bpf_lwt_lwtunnel(dst->lwtstate);
118 ret = run_lwt_bpf(skb, &bpf->in, dst, NO_REDIRECT);
121 if (ret == BPF_LWT_REROUTE)
122 return bpf_lwt_input_reroute(skb);
125 if (unlikely(!dst->lwtstate->orig_input)) {
130 return dst->lwtstate->orig_input(skb);
133 static int bpf_output(struct net *net, struct sock *sk, struct sk_buff *skb)
135 struct dst_entry *dst = skb_dst(skb);
139 bpf = bpf_lwt_lwtunnel(dst->lwtstate);
141 ret = run_lwt_bpf(skb, &bpf->out, dst, NO_REDIRECT);
146 if (unlikely(!dst->lwtstate->orig_output)) {
147 pr_warn_once("orig_output not set on dst for prog %s\n",
153 return dst->lwtstate->orig_output(net, sk, skb);
156 static int xmit_check_hhlen(struct sk_buff *skb)
158 int hh_len = skb_dst(skb)->dev->hard_header_len;
160 if (skb_headroom(skb) < hh_len) {
161 int nhead = HH_DATA_ALIGN(hh_len - skb_headroom(skb));
163 if (pskb_expand_head(skb, nhead, 0, GFP_ATOMIC))
170 static int bpf_lwt_xmit_reroute(struct sk_buff *skb)
172 struct net_device *l3mdev = l3mdev_master_dev_rcu(skb_dst(skb)->dev);
173 int oif = l3mdev ? l3mdev->ifindex : 0;
174 struct dst_entry *dst = NULL;
175 int err = -EAFNOSUPPORT;
180 if (skb->protocol == htons(ETH_P_IP))
182 else if (skb->protocol == htons(ETH_P_IPV6))
187 sk = sk_to_full_sk(skb->sk);
189 if (sk->sk_bound_dev_if)
190 oif = sk->sk_bound_dev_if;
193 net = dev_net(skb_dst(skb)->dev);
197 struct iphdr *iph = ip_hdr(skb);
198 struct flowi4 fl4 = {};
201 fl4.flowi4_oif = oif;
202 fl4.flowi4_mark = skb->mark;
203 fl4.flowi4_uid = sock_net_uid(net, sk);
204 fl4.flowi4_tos = RT_TOS(iph->tos);
205 fl4.flowi4_flags = FLOWI_FLAG_ANYSRC;
206 fl4.flowi4_proto = iph->protocol;
207 fl4.daddr = iph->daddr;
208 fl4.saddr = iph->saddr;
210 rt = ip_route_output_key(net, &fl4);
217 struct ipv6hdr *iph6 = ipv6_hdr(skb);
218 struct flowi6 fl6 = {};
220 fl6.flowi6_oif = oif;
221 fl6.flowi6_mark = skb->mark;
222 fl6.flowi6_uid = sock_net_uid(net, sk);
223 fl6.flowlabel = ip6_flowinfo(iph6);
224 fl6.flowi6_proto = iph6->nexthdr;
225 fl6.daddr = iph6->daddr;
226 fl6.saddr = iph6->saddr;
228 err = ipv6_stub->ipv6_dst_lookup(net, skb->sk, &dst, &fl6);
236 if (unlikely(dst->error)) {
242 /* Although skb header was reserved in bpf_lwt_push_ip_encap(), it
243 * was done for the previous dst, so we are doing it here again, in
244 * case the new dst needs much more space. The call below is a noop
245 * if there is enough header space in skb.
247 err = skb_cow_head(skb, LL_RESERVED_SPACE(dst->dev));
252 skb_dst_set(skb, dst);
254 err = dst_output(dev_net(skb_dst(skb)->dev), skb->sk, skb);
258 /* ip[6]_finish_output2 understand LWTUNNEL_XMIT_DONE */
259 return LWTUNNEL_XMIT_DONE;
266 static int bpf_xmit(struct sk_buff *skb)
268 struct dst_entry *dst = skb_dst(skb);
271 bpf = bpf_lwt_lwtunnel(dst->lwtstate);
272 if (bpf->xmit.prog) {
273 __be16 proto = skb->protocol;
276 ret = run_lwt_bpf(skb, &bpf->xmit, dst, CAN_REDIRECT);
279 /* If the header changed, e.g. via bpf_lwt_push_encap,
280 * BPF_LWT_REROUTE below should have been used if the
281 * protocol was also changed.
283 if (skb->protocol != proto) {
287 /* If the header was expanded, headroom might be too
288 * small for L2 header to come, expand as needed.
290 ret = xmit_check_hhlen(skb);
294 return LWTUNNEL_XMIT_CONTINUE;
296 return LWTUNNEL_XMIT_DONE;
297 case BPF_LWT_REROUTE:
298 return bpf_lwt_xmit_reroute(skb);
304 return LWTUNNEL_XMIT_CONTINUE;
307 static void bpf_lwt_prog_destroy(struct bpf_lwt_prog *prog)
310 bpf_prog_put(prog->prog);
315 static void bpf_destroy_state(struct lwtunnel_state *lwt)
317 struct bpf_lwt *bpf = bpf_lwt_lwtunnel(lwt);
319 bpf_lwt_prog_destroy(&bpf->in);
320 bpf_lwt_prog_destroy(&bpf->out);
321 bpf_lwt_prog_destroy(&bpf->xmit);
324 static const struct nla_policy bpf_prog_policy[LWT_BPF_PROG_MAX + 1] = {
325 [LWT_BPF_PROG_FD] = { .type = NLA_U32, },
326 [LWT_BPF_PROG_NAME] = { .type = NLA_NUL_STRING,
327 .len = MAX_PROG_NAME },
330 static int bpf_parse_prog(struct nlattr *attr, struct bpf_lwt_prog *prog,
331 enum bpf_prog_type type)
333 struct nlattr *tb[LWT_BPF_PROG_MAX + 1];
338 ret = nla_parse_nested_deprecated(tb, LWT_BPF_PROG_MAX, attr,
339 bpf_prog_policy, NULL);
343 if (!tb[LWT_BPF_PROG_FD] || !tb[LWT_BPF_PROG_NAME])
346 prog->name = nla_memdup(tb[LWT_BPF_PROG_NAME], GFP_ATOMIC);
350 fd = nla_get_u32(tb[LWT_BPF_PROG_FD]);
351 p = bpf_prog_get_type(fd, type);
360 static const struct nla_policy bpf_nl_policy[LWT_BPF_MAX + 1] = {
361 [LWT_BPF_IN] = { .type = NLA_NESTED, },
362 [LWT_BPF_OUT] = { .type = NLA_NESTED, },
363 [LWT_BPF_XMIT] = { .type = NLA_NESTED, },
364 [LWT_BPF_XMIT_HEADROOM] = { .type = NLA_U32 },
367 static int bpf_build_state(struct nlattr *nla,
368 unsigned int family, const void *cfg,
369 struct lwtunnel_state **ts,
370 struct netlink_ext_ack *extack)
372 struct nlattr *tb[LWT_BPF_MAX + 1];
373 struct lwtunnel_state *newts;
377 if (family != AF_INET && family != AF_INET6)
378 return -EAFNOSUPPORT;
380 ret = nla_parse_nested_deprecated(tb, LWT_BPF_MAX, nla, bpf_nl_policy,
385 if (!tb[LWT_BPF_IN] && !tb[LWT_BPF_OUT] && !tb[LWT_BPF_XMIT])
388 newts = lwtunnel_state_alloc(sizeof(*bpf));
392 newts->type = LWTUNNEL_ENCAP_BPF;
393 bpf = bpf_lwt_lwtunnel(newts);
395 if (tb[LWT_BPF_IN]) {
396 newts->flags |= LWTUNNEL_STATE_INPUT_REDIRECT;
397 ret = bpf_parse_prog(tb[LWT_BPF_IN], &bpf->in,
398 BPF_PROG_TYPE_LWT_IN);
403 if (tb[LWT_BPF_OUT]) {
404 newts->flags |= LWTUNNEL_STATE_OUTPUT_REDIRECT;
405 ret = bpf_parse_prog(tb[LWT_BPF_OUT], &bpf->out,
406 BPF_PROG_TYPE_LWT_OUT);
411 if (tb[LWT_BPF_XMIT]) {
412 newts->flags |= LWTUNNEL_STATE_XMIT_REDIRECT;
413 ret = bpf_parse_prog(tb[LWT_BPF_XMIT], &bpf->xmit,
414 BPF_PROG_TYPE_LWT_XMIT);
419 if (tb[LWT_BPF_XMIT_HEADROOM]) {
420 u32 headroom = nla_get_u32(tb[LWT_BPF_XMIT_HEADROOM]);
422 if (headroom > LWT_BPF_MAX_HEADROOM) {
427 newts->headroom = headroom;
430 bpf->family = family;
436 bpf_destroy_state(newts);
441 static int bpf_fill_lwt_prog(struct sk_buff *skb, int attr,
442 struct bpf_lwt_prog *prog)
449 nest = nla_nest_start_noflag(skb, attr);
454 nla_put_string(skb, LWT_BPF_PROG_NAME, prog->name))
457 return nla_nest_end(skb, nest);
460 static int bpf_fill_encap_info(struct sk_buff *skb, struct lwtunnel_state *lwt)
462 struct bpf_lwt *bpf = bpf_lwt_lwtunnel(lwt);
464 if (bpf_fill_lwt_prog(skb, LWT_BPF_IN, &bpf->in) < 0 ||
465 bpf_fill_lwt_prog(skb, LWT_BPF_OUT, &bpf->out) < 0 ||
466 bpf_fill_lwt_prog(skb, LWT_BPF_XMIT, &bpf->xmit) < 0)
472 static int bpf_encap_nlsize(struct lwtunnel_state *lwtstate)
474 int nest_len = nla_total_size(sizeof(struct nlattr)) +
475 nla_total_size(MAX_PROG_NAME) + /* LWT_BPF_PROG_NAME */
478 return nest_len + /* LWT_BPF_IN */
479 nest_len + /* LWT_BPF_OUT */
480 nest_len + /* LWT_BPF_XMIT */
484 static int bpf_lwt_prog_cmp(struct bpf_lwt_prog *a, struct bpf_lwt_prog *b)
487 * The LWT state is currently rebuilt for delete requests which
488 * results in a new bpf_prog instance. Comparing names for now.
490 if (!a->name && !b->name)
493 if (!a->name || !b->name)
496 return strcmp(a->name, b->name);
499 static int bpf_encap_cmp(struct lwtunnel_state *a, struct lwtunnel_state *b)
501 struct bpf_lwt *a_bpf = bpf_lwt_lwtunnel(a);
502 struct bpf_lwt *b_bpf = bpf_lwt_lwtunnel(b);
504 return bpf_lwt_prog_cmp(&a_bpf->in, &b_bpf->in) ||
505 bpf_lwt_prog_cmp(&a_bpf->out, &b_bpf->out) ||
506 bpf_lwt_prog_cmp(&a_bpf->xmit, &b_bpf->xmit);
509 static const struct lwtunnel_encap_ops bpf_encap_ops = {
510 .build_state = bpf_build_state,
511 .destroy_state = bpf_destroy_state,
513 .output = bpf_output,
515 .fill_encap = bpf_fill_encap_info,
516 .get_encap_size = bpf_encap_nlsize,
517 .cmp_encap = bpf_encap_cmp,
518 .owner = THIS_MODULE,
521 static int handle_gso_type(struct sk_buff *skb, unsigned int gso_type,
524 struct skb_shared_info *shinfo = skb_shinfo(skb);
526 gso_type |= SKB_GSO_DODGY;
527 shinfo->gso_type |= gso_type;
528 skb_decrease_gso_size(shinfo, encap_len);
529 shinfo->gso_segs = 0;
533 static int handle_gso_encap(struct sk_buff *skb, bool ipv4, int encap_len)
539 /* SCTP and UDP_L4 gso need more nuanced handling than what
540 * handle_gso_type() does above: skb_decrease_gso_size() is not enough.
541 * So at the moment only TCP GSO packets are let through.
543 if (!(skb_shinfo(skb)->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
547 protocol = ip_hdr(skb)->protocol;
548 next_hdr_offset = sizeof(struct iphdr);
549 next_hdr = skb_network_header(skb) + next_hdr_offset;
551 protocol = ipv6_hdr(skb)->nexthdr;
552 next_hdr_offset = sizeof(struct ipv6hdr);
553 next_hdr = skb_network_header(skb) + next_hdr_offset;
558 next_hdr_offset += sizeof(struct gre_base_hdr);
559 if (next_hdr_offset > encap_len)
562 if (((struct gre_base_hdr *)next_hdr)->flags & GRE_CSUM)
563 return handle_gso_type(skb, SKB_GSO_GRE_CSUM,
565 return handle_gso_type(skb, SKB_GSO_GRE, encap_len);
568 next_hdr_offset += sizeof(struct udphdr);
569 if (next_hdr_offset > encap_len)
572 if (((struct udphdr *)next_hdr)->check)
573 return handle_gso_type(skb, SKB_GSO_UDP_TUNNEL_CSUM,
575 return handle_gso_type(skb, SKB_GSO_UDP_TUNNEL, encap_len);
580 return handle_gso_type(skb, SKB_GSO_IPXIP4, encap_len);
582 return handle_gso_type(skb, SKB_GSO_IPXIP6, encap_len);
585 return -EPROTONOSUPPORT;
589 int bpf_lwt_push_ip_encap(struct sk_buff *skb, void *hdr, u32 len, bool ingress)
595 if (unlikely(len < sizeof(struct iphdr) || len > LWT_BPF_MAX_HEADROOM))
598 /* validate protocol and length */
599 iph = (struct iphdr *)hdr;
600 if (iph->version == 4) {
602 if (unlikely(len < iph->ihl * 4))
604 } else if (iph->version == 6) {
606 if (unlikely(len < sizeof(struct ipv6hdr)))
613 err = skb_cow_head(skb, len + skb->mac_len);
615 err = skb_cow_head(skb,
616 len + LL_RESERVED_SPACE(skb_dst(skb)->dev));
620 /* push the encap headers and fix pointers */
621 skb_reset_inner_headers(skb);
622 skb_reset_inner_mac_header(skb); /* mac header is not yet set */
623 skb_set_inner_protocol(skb, skb->protocol);
624 skb->encapsulation = 1;
627 skb_postpush_rcsum(skb, iph, len);
628 skb_reset_network_header(skb);
629 memcpy(skb_network_header(skb), hdr, len);
630 bpf_compute_data_pointers(skb);
634 skb->protocol = htons(ETH_P_IP);
638 iph->check = ip_fast_csum((unsigned char *)iph,
641 skb->protocol = htons(ETH_P_IPV6);
645 return handle_gso_encap(skb, ipv4, len);
650 static int __init bpf_lwt_init(void)
652 return lwtunnel_encap_add_ops(&bpf_encap_ops, LWTUNNEL_ENCAP_BPF);
655 subsys_initcall(bpf_lwt_init)