1 // SPDX-License-Identifier: GPL-2.0-only
5 #include <linux/filter.h>
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/skbuff.h>
9 #include <linux/types.h>
10 #include <linux/bpf.h>
11 #include <net/lwtunnel.h>
13 #include <net/ip6_route.h>
14 #include <net/ipv6_stubs.h>
17 struct bpf_prog *prog;
22 struct bpf_lwt_prog in;
23 struct bpf_lwt_prog out;
24 struct bpf_lwt_prog xmit;
28 #define MAX_PROG_NAME 256
30 static inline struct bpf_lwt *bpf_lwt_lwtunnel(struct lwtunnel_state *lwt)
32 return (struct bpf_lwt *)lwt->data;
35 #define NO_REDIRECT false
36 #define CAN_REDIRECT true
38 static int run_lwt_bpf(struct sk_buff *skb, struct bpf_lwt_prog *lwt,
39 struct dst_entry *dst, bool can_redirect)
43 /* Migration disable and BH disable are needed to protect per-cpu
44 * redirect_info between BPF prog and skb_do_redirect().
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);
74 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 net_device *dev = skb_dst(skb)->dev;
92 struct iphdr *iph = ip_hdr(skb);
96 err = ip_route_input_noref(skb, iph->daddr, iph->saddr,
99 } else if (skb->protocol == htons(ETH_P_IPV6)) {
101 err = ipv6_stub->ipv6_route_input(skb);
108 return dst_input(skb);
115 static int bpf_input(struct sk_buff *skb)
117 struct dst_entry *dst = skb_dst(skb);
121 bpf = bpf_lwt_lwtunnel(dst->lwtstate);
123 ret = run_lwt_bpf(skb, &bpf->in, dst, NO_REDIRECT);
126 if (ret == BPF_LWT_REROUTE)
127 return bpf_lwt_input_reroute(skb);
130 if (unlikely(!dst->lwtstate->orig_input)) {
135 return dst->lwtstate->orig_input(skb);
138 static int bpf_output(struct net *net, struct sock *sk, struct sk_buff *skb)
140 struct dst_entry *dst = skb_dst(skb);
144 bpf = bpf_lwt_lwtunnel(dst->lwtstate);
146 ret = run_lwt_bpf(skb, &bpf->out, dst, NO_REDIRECT);
151 if (unlikely(!dst->lwtstate->orig_output)) {
152 pr_warn_once("orig_output not set on dst for prog %s\n",
158 return dst->lwtstate->orig_output(net, sk, skb);
161 static int xmit_check_hhlen(struct sk_buff *skb, int hh_len)
163 if (skb_headroom(skb) < hh_len) {
164 int nhead = HH_DATA_ALIGN(hh_len - skb_headroom(skb));
166 if (pskb_expand_head(skb, nhead, 0, GFP_ATOMIC))
173 static int bpf_lwt_xmit_reroute(struct sk_buff *skb)
175 struct net_device *l3mdev = l3mdev_master_dev_rcu(skb_dst(skb)->dev);
176 int oif = l3mdev ? l3mdev->ifindex : 0;
177 struct dst_entry *dst = NULL;
178 int err = -EAFNOSUPPORT;
183 if (skb->protocol == htons(ETH_P_IP))
185 else if (skb->protocol == htons(ETH_P_IPV6))
190 sk = sk_to_full_sk(skb->sk);
192 if (sk->sk_bound_dev_if)
193 oif = sk->sk_bound_dev_if;
196 net = dev_net(skb_dst(skb)->dev);
200 struct iphdr *iph = ip_hdr(skb);
201 struct flowi4 fl4 = {};
204 fl4.flowi4_oif = oif;
205 fl4.flowi4_mark = skb->mark;
206 fl4.flowi4_uid = sock_net_uid(net, sk);
207 fl4.flowi4_tos = RT_TOS(iph->tos);
208 fl4.flowi4_flags = FLOWI_FLAG_ANYSRC;
209 fl4.flowi4_proto = iph->protocol;
210 fl4.daddr = iph->daddr;
211 fl4.saddr = iph->saddr;
213 rt = ip_route_output_key(net, &fl4);
220 struct ipv6hdr *iph6 = ipv6_hdr(skb);
221 struct flowi6 fl6 = {};
223 fl6.flowi6_oif = oif;
224 fl6.flowi6_mark = skb->mark;
225 fl6.flowi6_uid = sock_net_uid(net, sk);
226 fl6.flowlabel = ip6_flowinfo(iph6);
227 fl6.flowi6_proto = iph6->nexthdr;
228 fl6.daddr = iph6->daddr;
229 fl6.saddr = iph6->saddr;
231 dst = ipv6_stub->ipv6_dst_lookup_flow(net, skb->sk, &fl6, NULL);
237 if (unlikely(dst->error)) {
243 /* Although skb header was reserved in bpf_lwt_push_ip_encap(), it
244 * was done for the previous dst, so we are doing it here again, in
245 * case the new dst needs much more space. The call below is a noop
246 * if there is enough header space in skb.
248 err = skb_cow_head(skb, LL_RESERVED_SPACE(dst->dev));
253 skb_dst_set(skb, dst);
255 err = dst_output(dev_net(skb_dst(skb)->dev), skb->sk, skb);
257 return net_xmit_errno(err);
259 /* ip[6]_finish_output2 understand LWTUNNEL_XMIT_DONE */
260 return LWTUNNEL_XMIT_DONE;
267 static int bpf_xmit(struct sk_buff *skb)
269 struct dst_entry *dst = skb_dst(skb);
272 bpf = bpf_lwt_lwtunnel(dst->lwtstate);
273 if (bpf->xmit.prog) {
274 int hh_len = dst->dev->hard_header_len;
275 __be16 proto = skb->protocol;
278 ret = run_lwt_bpf(skb, &bpf->xmit, dst, CAN_REDIRECT);
281 /* If the header changed, e.g. via bpf_lwt_push_encap,
282 * BPF_LWT_REROUTE below should have been used if the
283 * protocol was also changed.
285 if (skb->protocol != proto) {
289 /* If the header was expanded, headroom might be too
290 * small for L2 header to come, expand as needed.
292 ret = xmit_check_hhlen(skb, hh_len);
296 return LWTUNNEL_XMIT_CONTINUE;
298 return LWTUNNEL_XMIT_DONE;
299 case BPF_LWT_REROUTE:
300 return bpf_lwt_xmit_reroute(skb);
306 return LWTUNNEL_XMIT_CONTINUE;
309 static void bpf_lwt_prog_destroy(struct bpf_lwt_prog *prog)
312 bpf_prog_put(prog->prog);
317 static void bpf_destroy_state(struct lwtunnel_state *lwt)
319 struct bpf_lwt *bpf = bpf_lwt_lwtunnel(lwt);
321 bpf_lwt_prog_destroy(&bpf->in);
322 bpf_lwt_prog_destroy(&bpf->out);
323 bpf_lwt_prog_destroy(&bpf->xmit);
326 static const struct nla_policy bpf_prog_policy[LWT_BPF_PROG_MAX + 1] = {
327 [LWT_BPF_PROG_FD] = { .type = NLA_U32, },
328 [LWT_BPF_PROG_NAME] = { .type = NLA_NUL_STRING,
329 .len = MAX_PROG_NAME },
332 static int bpf_parse_prog(struct nlattr *attr, struct bpf_lwt_prog *prog,
333 enum bpf_prog_type type)
335 struct nlattr *tb[LWT_BPF_PROG_MAX + 1];
340 ret = nla_parse_nested_deprecated(tb, LWT_BPF_PROG_MAX, attr,
341 bpf_prog_policy, NULL);
345 if (!tb[LWT_BPF_PROG_FD] || !tb[LWT_BPF_PROG_NAME])
348 prog->name = nla_memdup(tb[LWT_BPF_PROG_NAME], GFP_ATOMIC);
352 fd = nla_get_u32(tb[LWT_BPF_PROG_FD]);
353 p = bpf_prog_get_type(fd, type);
362 static const struct nla_policy bpf_nl_policy[LWT_BPF_MAX + 1] = {
363 [LWT_BPF_IN] = { .type = NLA_NESTED, },
364 [LWT_BPF_OUT] = { .type = NLA_NESTED, },
365 [LWT_BPF_XMIT] = { .type = NLA_NESTED, },
366 [LWT_BPF_XMIT_HEADROOM] = { .type = NLA_U32 },
369 static int bpf_build_state(struct net *net, struct nlattr *nla,
370 unsigned int family, const void *cfg,
371 struct lwtunnel_state **ts,
372 struct netlink_ext_ack *extack)
374 struct nlattr *tb[LWT_BPF_MAX + 1];
375 struct lwtunnel_state *newts;
379 if (family != AF_INET && family != AF_INET6)
380 return -EAFNOSUPPORT;
382 ret = nla_parse_nested_deprecated(tb, LWT_BPF_MAX, nla, bpf_nl_policy,
387 if (!tb[LWT_BPF_IN] && !tb[LWT_BPF_OUT] && !tb[LWT_BPF_XMIT])
390 newts = lwtunnel_state_alloc(sizeof(*bpf));
394 newts->type = LWTUNNEL_ENCAP_BPF;
395 bpf = bpf_lwt_lwtunnel(newts);
397 if (tb[LWT_BPF_IN]) {
398 newts->flags |= LWTUNNEL_STATE_INPUT_REDIRECT;
399 ret = bpf_parse_prog(tb[LWT_BPF_IN], &bpf->in,
400 BPF_PROG_TYPE_LWT_IN);
405 if (tb[LWT_BPF_OUT]) {
406 newts->flags |= LWTUNNEL_STATE_OUTPUT_REDIRECT;
407 ret = bpf_parse_prog(tb[LWT_BPF_OUT], &bpf->out,
408 BPF_PROG_TYPE_LWT_OUT);
413 if (tb[LWT_BPF_XMIT]) {
414 newts->flags |= LWTUNNEL_STATE_XMIT_REDIRECT;
415 ret = bpf_parse_prog(tb[LWT_BPF_XMIT], &bpf->xmit,
416 BPF_PROG_TYPE_LWT_XMIT);
421 if (tb[LWT_BPF_XMIT_HEADROOM]) {
422 u32 headroom = nla_get_u32(tb[LWT_BPF_XMIT_HEADROOM]);
424 if (headroom > LWT_BPF_MAX_HEADROOM) {
429 newts->headroom = headroom;
432 bpf->family = family;
438 bpf_destroy_state(newts);
443 static int bpf_fill_lwt_prog(struct sk_buff *skb, int attr,
444 struct bpf_lwt_prog *prog)
451 nest = nla_nest_start_noflag(skb, attr);
456 nla_put_string(skb, LWT_BPF_PROG_NAME, prog->name))
459 return nla_nest_end(skb, nest);
462 static int bpf_fill_encap_info(struct sk_buff *skb, struct lwtunnel_state *lwt)
464 struct bpf_lwt *bpf = bpf_lwt_lwtunnel(lwt);
466 if (bpf_fill_lwt_prog(skb, LWT_BPF_IN, &bpf->in) < 0 ||
467 bpf_fill_lwt_prog(skb, LWT_BPF_OUT, &bpf->out) < 0 ||
468 bpf_fill_lwt_prog(skb, LWT_BPF_XMIT, &bpf->xmit) < 0)
474 static int bpf_encap_nlsize(struct lwtunnel_state *lwtstate)
476 int nest_len = nla_total_size(sizeof(struct nlattr)) +
477 nla_total_size(MAX_PROG_NAME) + /* LWT_BPF_PROG_NAME */
480 return nest_len + /* LWT_BPF_IN */
481 nest_len + /* LWT_BPF_OUT */
482 nest_len + /* LWT_BPF_XMIT */
486 static int bpf_lwt_prog_cmp(struct bpf_lwt_prog *a, struct bpf_lwt_prog *b)
489 * The LWT state is currently rebuilt for delete requests which
490 * results in a new bpf_prog instance. Comparing names for now.
492 if (!a->name && !b->name)
495 if (!a->name || !b->name)
498 return strcmp(a->name, b->name);
501 static int bpf_encap_cmp(struct lwtunnel_state *a, struct lwtunnel_state *b)
503 struct bpf_lwt *a_bpf = bpf_lwt_lwtunnel(a);
504 struct bpf_lwt *b_bpf = bpf_lwt_lwtunnel(b);
506 return bpf_lwt_prog_cmp(&a_bpf->in, &b_bpf->in) ||
507 bpf_lwt_prog_cmp(&a_bpf->out, &b_bpf->out) ||
508 bpf_lwt_prog_cmp(&a_bpf->xmit, &b_bpf->xmit);
511 static const struct lwtunnel_encap_ops bpf_encap_ops = {
512 .build_state = bpf_build_state,
513 .destroy_state = bpf_destroy_state,
515 .output = bpf_output,
517 .fill_encap = bpf_fill_encap_info,
518 .get_encap_size = bpf_encap_nlsize,
519 .cmp_encap = bpf_encap_cmp,
520 .owner = THIS_MODULE,
523 static int handle_gso_type(struct sk_buff *skb, unsigned int gso_type,
526 struct skb_shared_info *shinfo = skb_shinfo(skb);
528 gso_type |= SKB_GSO_DODGY;
529 shinfo->gso_type |= gso_type;
530 skb_decrease_gso_size(shinfo, encap_len);
531 shinfo->gso_segs = 0;
535 static int handle_gso_encap(struct sk_buff *skb, bool ipv4, int encap_len)
541 /* SCTP and UDP_L4 gso need more nuanced handling than what
542 * handle_gso_type() does above: skb_decrease_gso_size() is not enough.
543 * So at the moment only TCP GSO packets are let through.
545 if (!(skb_shinfo(skb)->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
549 protocol = ip_hdr(skb)->protocol;
550 next_hdr_offset = sizeof(struct iphdr);
551 next_hdr = skb_network_header(skb) + next_hdr_offset;
553 protocol = ipv6_hdr(skb)->nexthdr;
554 next_hdr_offset = sizeof(struct ipv6hdr);
555 next_hdr = skb_network_header(skb) + next_hdr_offset;
560 next_hdr_offset += sizeof(struct gre_base_hdr);
561 if (next_hdr_offset > encap_len)
564 if (((struct gre_base_hdr *)next_hdr)->flags & GRE_CSUM)
565 return handle_gso_type(skb, SKB_GSO_GRE_CSUM,
567 return handle_gso_type(skb, SKB_GSO_GRE, encap_len);
570 next_hdr_offset += sizeof(struct udphdr);
571 if (next_hdr_offset > encap_len)
574 if (((struct udphdr *)next_hdr)->check)
575 return handle_gso_type(skb, SKB_GSO_UDP_TUNNEL_CSUM,
577 return handle_gso_type(skb, SKB_GSO_UDP_TUNNEL, encap_len);
582 return handle_gso_type(skb, SKB_GSO_IPXIP4, encap_len);
584 return handle_gso_type(skb, SKB_GSO_IPXIP6, encap_len);
587 return -EPROTONOSUPPORT;
591 int bpf_lwt_push_ip_encap(struct sk_buff *skb, void *hdr, u32 len, bool ingress)
597 if (unlikely(len < sizeof(struct iphdr) || len > LWT_BPF_MAX_HEADROOM))
600 /* validate protocol and length */
601 iph = (struct iphdr *)hdr;
602 if (iph->version == 4) {
604 if (unlikely(len < iph->ihl * 4))
606 } else if (iph->version == 6) {
608 if (unlikely(len < sizeof(struct ipv6hdr)))
615 err = skb_cow_head(skb, len + skb->mac_len);
617 err = skb_cow_head(skb,
618 len + LL_RESERVED_SPACE(skb_dst(skb)->dev));
622 /* push the encap headers and fix pointers */
623 skb_reset_inner_headers(skb);
624 skb_reset_inner_mac_header(skb); /* mac header is not yet set */
625 skb_set_inner_protocol(skb, skb->protocol);
626 skb->encapsulation = 1;
629 skb_postpush_rcsum(skb, iph, len);
630 skb_reset_network_header(skb);
631 memcpy(skb_network_header(skb), hdr, len);
632 bpf_compute_data_pointers(skb);
636 skb->protocol = htons(ETH_P_IP);
640 iph->check = ip_fast_csum((unsigned char *)iph,
643 skb->protocol = htons(ETH_P_IPV6);
647 return handle_gso_encap(skb, ipv4, len);
652 static int __init bpf_lwt_init(void)
654 return lwtunnel_encap_add_ops(&bpf_encap_ops, LWTUNNEL_ENCAP_BPF);
657 subsys_initcall(bpf_lwt_init)