1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2016 Thomas Graf <tgraf@tgraf.ch>
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 /* Migration disable and BH disable are needed to protect per-cpu
43 * redirect_info between BPF prog and skb_do_redirect().
47 bpf_compute_data_pointers(skb);
48 ret = bpf_prog_run_save_cb(lwt->prog, skb);
56 if (unlikely(!can_redirect)) {
57 pr_warn_once("Illegal redirect return code in prog %s\n",
58 lwt->name ? : "<unknown>");
61 skb_reset_mac_header(skb);
73 pr_warn_once("bpf-lwt: Illegal return value %u, expect packet loss\n", ret);
85 static int bpf_lwt_input_reroute(struct sk_buff *skb)
89 if (skb->protocol == htons(ETH_P_IP)) {
90 struct net_device *dev = skb_dst(skb)->dev;
91 struct iphdr *iph = ip_hdr(skb);
95 err = ip_route_input_noref(skb, iph->daddr, iph->saddr,
98 } else if (skb->protocol == htons(ETH_P_IPV6)) {
100 err = ipv6_stub->ipv6_route_input(skb);
107 return dst_input(skb);
114 static int bpf_input(struct sk_buff *skb)
116 struct dst_entry *dst = skb_dst(skb);
120 bpf = bpf_lwt_lwtunnel(dst->lwtstate);
122 ret = run_lwt_bpf(skb, &bpf->in, dst, NO_REDIRECT);
125 if (ret == BPF_LWT_REROUTE)
126 return bpf_lwt_input_reroute(skb);
129 if (unlikely(!dst->lwtstate->orig_input)) {
134 return dst->lwtstate->orig_input(skb);
137 static int bpf_output(struct net *net, struct sock *sk, struct sk_buff *skb)
139 struct dst_entry *dst = skb_dst(skb);
143 bpf = bpf_lwt_lwtunnel(dst->lwtstate);
145 ret = run_lwt_bpf(skb, &bpf->out, dst, NO_REDIRECT);
150 if (unlikely(!dst->lwtstate->orig_output)) {
151 pr_warn_once("orig_output not set on dst for prog %s\n",
157 return dst->lwtstate->orig_output(net, sk, skb);
160 static int xmit_check_hhlen(struct sk_buff *skb, int hh_len)
162 if (skb_headroom(skb) < hh_len) {
163 int nhead = HH_DATA_ALIGN(hh_len - skb_headroom(skb));
165 if (pskb_expand_head(skb, nhead, 0, GFP_ATOMIC))
172 static int bpf_lwt_xmit_reroute(struct sk_buff *skb)
174 struct net_device *l3mdev = l3mdev_master_dev_rcu(skb_dst(skb)->dev);
175 int oif = l3mdev ? l3mdev->ifindex : 0;
176 struct dst_entry *dst = NULL;
177 int err = -EAFNOSUPPORT;
182 if (skb->protocol == htons(ETH_P_IP))
184 else if (skb->protocol == htons(ETH_P_IPV6))
189 sk = sk_to_full_sk(skb->sk);
191 if (sk->sk_bound_dev_if)
192 oif = sk->sk_bound_dev_if;
195 net = dev_net(skb_dst(skb)->dev);
199 struct iphdr *iph = ip_hdr(skb);
200 struct flowi4 fl4 = {};
203 fl4.flowi4_oif = oif;
204 fl4.flowi4_mark = skb->mark;
205 fl4.flowi4_uid = sock_net_uid(net, sk);
206 fl4.flowi4_tos = RT_TOS(iph->tos);
207 fl4.flowi4_flags = FLOWI_FLAG_ANYSRC;
208 fl4.flowi4_proto = iph->protocol;
209 fl4.daddr = iph->daddr;
210 fl4.saddr = iph->saddr;
212 rt = ip_route_output_key(net, &fl4);
219 struct ipv6hdr *iph6 = ipv6_hdr(skb);
220 struct flowi6 fl6 = {};
222 fl6.flowi6_oif = oif;
223 fl6.flowi6_mark = skb->mark;
224 fl6.flowi6_uid = sock_net_uid(net, sk);
225 fl6.flowlabel = ip6_flowinfo(iph6);
226 fl6.flowi6_proto = iph6->nexthdr;
227 fl6.daddr = iph6->daddr;
228 fl6.saddr = iph6->saddr;
230 dst = ipv6_stub->ipv6_dst_lookup_flow(net, skb->sk, &fl6, NULL);
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);
256 return net_xmit_errno(err);
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 int hh_len = dst->dev->hard_header_len;
274 __be16 proto = skb->protocol;
277 ret = run_lwt_bpf(skb, &bpf->xmit, dst, CAN_REDIRECT);
280 /* If the header changed, e.g. via bpf_lwt_push_encap,
281 * BPF_LWT_REROUTE below should have been used if the
282 * protocol was also changed.
284 if (skb->protocol != proto) {
288 /* If the header was expanded, headroom might be too
289 * small for L2 header to come, expand as needed.
291 ret = xmit_check_hhlen(skb, hh_len);
295 return LWTUNNEL_XMIT_CONTINUE;
297 return LWTUNNEL_XMIT_DONE;
298 case BPF_LWT_REROUTE:
299 return bpf_lwt_xmit_reroute(skb);
305 return LWTUNNEL_XMIT_CONTINUE;
308 static void bpf_lwt_prog_destroy(struct bpf_lwt_prog *prog)
311 bpf_prog_put(prog->prog);
316 static void bpf_destroy_state(struct lwtunnel_state *lwt)
318 struct bpf_lwt *bpf = bpf_lwt_lwtunnel(lwt);
320 bpf_lwt_prog_destroy(&bpf->in);
321 bpf_lwt_prog_destroy(&bpf->out);
322 bpf_lwt_prog_destroy(&bpf->xmit);
325 static const struct nla_policy bpf_prog_policy[LWT_BPF_PROG_MAX + 1] = {
326 [LWT_BPF_PROG_FD] = { .type = NLA_U32, },
327 [LWT_BPF_PROG_NAME] = { .type = NLA_NUL_STRING,
328 .len = MAX_PROG_NAME },
331 static int bpf_parse_prog(struct nlattr *attr, struct bpf_lwt_prog *prog,
332 enum bpf_prog_type type)
334 struct nlattr *tb[LWT_BPF_PROG_MAX + 1];
339 ret = nla_parse_nested_deprecated(tb, LWT_BPF_PROG_MAX, attr,
340 bpf_prog_policy, NULL);
344 if (!tb[LWT_BPF_PROG_FD] || !tb[LWT_BPF_PROG_NAME])
347 prog->name = nla_memdup(tb[LWT_BPF_PROG_NAME], GFP_ATOMIC);
351 fd = nla_get_u32(tb[LWT_BPF_PROG_FD]);
352 p = bpf_prog_get_type(fd, type);
361 static const struct nla_policy bpf_nl_policy[LWT_BPF_MAX + 1] = {
362 [LWT_BPF_IN] = { .type = NLA_NESTED, },
363 [LWT_BPF_OUT] = { .type = NLA_NESTED, },
364 [LWT_BPF_XMIT] = { .type = NLA_NESTED, },
365 [LWT_BPF_XMIT_HEADROOM] = { .type = NLA_U32 },
368 static int bpf_build_state(struct net *net, struct nlattr *nla,
369 unsigned int family, const void *cfg,
370 struct lwtunnel_state **ts,
371 struct netlink_ext_ack *extack)
373 struct nlattr *tb[LWT_BPF_MAX + 1];
374 struct lwtunnel_state *newts;
378 if (family != AF_INET && family != AF_INET6)
379 return -EAFNOSUPPORT;
381 ret = nla_parse_nested_deprecated(tb, LWT_BPF_MAX, nla, bpf_nl_policy,
386 if (!tb[LWT_BPF_IN] && !tb[LWT_BPF_OUT] && !tb[LWT_BPF_XMIT])
389 newts = lwtunnel_state_alloc(sizeof(*bpf));
393 newts->type = LWTUNNEL_ENCAP_BPF;
394 bpf = bpf_lwt_lwtunnel(newts);
396 if (tb[LWT_BPF_IN]) {
397 newts->flags |= LWTUNNEL_STATE_INPUT_REDIRECT;
398 ret = bpf_parse_prog(tb[LWT_BPF_IN], &bpf->in,
399 BPF_PROG_TYPE_LWT_IN);
404 if (tb[LWT_BPF_OUT]) {
405 newts->flags |= LWTUNNEL_STATE_OUTPUT_REDIRECT;
406 ret = bpf_parse_prog(tb[LWT_BPF_OUT], &bpf->out,
407 BPF_PROG_TYPE_LWT_OUT);
412 if (tb[LWT_BPF_XMIT]) {
413 newts->flags |= LWTUNNEL_STATE_XMIT_REDIRECT;
414 ret = bpf_parse_prog(tb[LWT_BPF_XMIT], &bpf->xmit,
415 BPF_PROG_TYPE_LWT_XMIT);
420 if (tb[LWT_BPF_XMIT_HEADROOM]) {
421 u32 headroom = nla_get_u32(tb[LWT_BPF_XMIT_HEADROOM]);
423 if (headroom > LWT_BPF_MAX_HEADROOM) {
428 newts->headroom = headroom;
431 bpf->family = family;
437 bpf_destroy_state(newts);
442 static int bpf_fill_lwt_prog(struct sk_buff *skb, int attr,
443 struct bpf_lwt_prog *prog)
450 nest = nla_nest_start_noflag(skb, attr);
455 nla_put_string(skb, LWT_BPF_PROG_NAME, prog->name))
458 return nla_nest_end(skb, nest);
461 static int bpf_fill_encap_info(struct sk_buff *skb, struct lwtunnel_state *lwt)
463 struct bpf_lwt *bpf = bpf_lwt_lwtunnel(lwt);
465 if (bpf_fill_lwt_prog(skb, LWT_BPF_IN, &bpf->in) < 0 ||
466 bpf_fill_lwt_prog(skb, LWT_BPF_OUT, &bpf->out) < 0 ||
467 bpf_fill_lwt_prog(skb, LWT_BPF_XMIT, &bpf->xmit) < 0)
473 static int bpf_encap_nlsize(struct lwtunnel_state *lwtstate)
475 int nest_len = nla_total_size(sizeof(struct nlattr)) +
476 nla_total_size(MAX_PROG_NAME) + /* LWT_BPF_PROG_NAME */
479 return nest_len + /* LWT_BPF_IN */
480 nest_len + /* LWT_BPF_OUT */
481 nest_len + /* LWT_BPF_XMIT */
485 static int bpf_lwt_prog_cmp(struct bpf_lwt_prog *a, struct bpf_lwt_prog *b)
488 * The LWT state is currently rebuilt for delete requests which
489 * results in a new bpf_prog instance. Comparing names for now.
491 if (!a->name && !b->name)
494 if (!a->name || !b->name)
497 return strcmp(a->name, b->name);
500 static int bpf_encap_cmp(struct lwtunnel_state *a, struct lwtunnel_state *b)
502 struct bpf_lwt *a_bpf = bpf_lwt_lwtunnel(a);
503 struct bpf_lwt *b_bpf = bpf_lwt_lwtunnel(b);
505 return bpf_lwt_prog_cmp(&a_bpf->in, &b_bpf->in) ||
506 bpf_lwt_prog_cmp(&a_bpf->out, &b_bpf->out) ||
507 bpf_lwt_prog_cmp(&a_bpf->xmit, &b_bpf->xmit);
510 static const struct lwtunnel_encap_ops bpf_encap_ops = {
511 .build_state = bpf_build_state,
512 .destroy_state = bpf_destroy_state,
514 .output = bpf_output,
516 .fill_encap = bpf_fill_encap_info,
517 .get_encap_size = bpf_encap_nlsize,
518 .cmp_encap = bpf_encap_cmp,
519 .owner = THIS_MODULE,
522 static int handle_gso_type(struct sk_buff *skb, unsigned int gso_type,
525 struct skb_shared_info *shinfo = skb_shinfo(skb);
527 gso_type |= SKB_GSO_DODGY;
528 shinfo->gso_type |= gso_type;
529 skb_decrease_gso_size(shinfo, encap_len);
530 shinfo->gso_segs = 0;
534 static int handle_gso_encap(struct sk_buff *skb, bool ipv4, int encap_len)
540 /* SCTP and UDP_L4 gso need more nuanced handling than what
541 * handle_gso_type() does above: skb_decrease_gso_size() is not enough.
542 * So at the moment only TCP GSO packets are let through.
544 if (!(skb_shinfo(skb)->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
548 protocol = ip_hdr(skb)->protocol;
549 next_hdr_offset = sizeof(struct iphdr);
550 next_hdr = skb_network_header(skb) + next_hdr_offset;
552 protocol = ipv6_hdr(skb)->nexthdr;
553 next_hdr_offset = sizeof(struct ipv6hdr);
554 next_hdr = skb_network_header(skb) + next_hdr_offset;
559 next_hdr_offset += sizeof(struct gre_base_hdr);
560 if (next_hdr_offset > encap_len)
563 if (((struct gre_base_hdr *)next_hdr)->flags & GRE_CSUM)
564 return handle_gso_type(skb, SKB_GSO_GRE_CSUM,
566 return handle_gso_type(skb, SKB_GSO_GRE, encap_len);
569 next_hdr_offset += sizeof(struct udphdr);
570 if (next_hdr_offset > encap_len)
573 if (((struct udphdr *)next_hdr)->check)
574 return handle_gso_type(skb, SKB_GSO_UDP_TUNNEL_CSUM,
576 return handle_gso_type(skb, SKB_GSO_UDP_TUNNEL, encap_len);
581 return handle_gso_type(skb, SKB_GSO_IPXIP4, encap_len);
583 return handle_gso_type(skb, SKB_GSO_IPXIP6, encap_len);
586 return -EPROTONOSUPPORT;
590 int bpf_lwt_push_ip_encap(struct sk_buff *skb, void *hdr, u32 len, bool ingress)
596 if (unlikely(len < sizeof(struct iphdr) || len > LWT_BPF_MAX_HEADROOM))
599 /* validate protocol and length */
600 iph = (struct iphdr *)hdr;
601 if (iph->version == 4) {
603 if (unlikely(len < iph->ihl * 4))
605 } else if (iph->version == 6) {
607 if (unlikely(len < sizeof(struct ipv6hdr)))
614 err = skb_cow_head(skb, len + skb->mac_len);
616 err = skb_cow_head(skb,
617 len + LL_RESERVED_SPACE(skb_dst(skb)->dev));
621 /* push the encap headers and fix pointers */
622 skb_reset_inner_headers(skb);
623 skb_reset_inner_mac_header(skb); /* mac header is not yet set */
624 skb_set_inner_protocol(skb, skb->protocol);
625 skb->encapsulation = 1;
628 skb_postpush_rcsum(skb, iph, len);
629 skb_reset_network_header(skb);
630 memcpy(skb_network_header(skb), hdr, len);
631 bpf_compute_data_pointers(skb);
635 skb->protocol = htons(ETH_P_IP);
639 iph->check = ip_fast_csum((unsigned char *)iph,
642 skb->protocol = htons(ETH_P_IPV6);
646 return handle_gso_encap(skb, ipv4, len);
651 static int __init bpf_lwt_init(void)
653 return lwtunnel_encap_add_ops(&bpf_encap_ops, LWTUNNEL_ENCAP_BPF);
656 subsys_initcall(bpf_lwt_init)