1 // SPDX-License-Identifier: GPL-2.0-only 2 /* Copyright (c) 2016 Thomas Graf <tgraf@tgraf.ch> 3 */ 4 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/flow.h> 12 #include <net/lwtunnel.h> 13 #include <net/gre.h> 14 #include <net/ip.h> 15 #include <net/ip6_route.h> 16 17 struct bpf_lwt_prog { 18 struct bpf_prog *prog; 19 char *name; 20 }; 21 22 struct bpf_lwt { 23 struct bpf_lwt_prog in; 24 struct bpf_lwt_prog out; 25 struct bpf_lwt_prog xmit; 26 int family; 27 }; 28 29 #define MAX_PROG_NAME 256 30 31 static inline struct bpf_lwt *bpf_lwt_lwtunnel(struct lwtunnel_state *lwt) 32 { 33 return (struct bpf_lwt *)lwt->data; 34 } 35 36 #define NO_REDIRECT false 37 #define CAN_REDIRECT true 38 39 static int run_lwt_bpf(struct sk_buff *skb, struct bpf_lwt_prog *lwt, 40 struct dst_entry *dst, bool can_redirect) 41 { 42 struct bpf_net_context __bpf_net_ctx, *bpf_net_ctx; 43 int ret; 44 45 /* Disabling BH is needed to protect per-CPU bpf_redirect_info between 46 * BPF prog and skb_do_redirect(). 47 */ 48 local_bh_disable(); 49 bpf_net_ctx = bpf_net_ctx_set(&__bpf_net_ctx); 50 bpf_compute_data_pointers(skb); 51 ret = bpf_prog_run_save_cb(lwt->prog, skb); 52 53 switch (ret) { 54 case BPF_OK: 55 case BPF_LWT_REROUTE: 56 break; 57 58 case BPF_REDIRECT: 59 if (unlikely(!can_redirect)) { 60 pr_warn_once("Illegal redirect return code in prog %s\n", 61 lwt->name ? : "<unknown>"); 62 ret = BPF_OK; 63 } else { 64 skb_reset_mac_header(skb); 65 skb_do_redirect(skb); 66 ret = BPF_REDIRECT; 67 } 68 break; 69 70 case BPF_DROP: 71 kfree_skb(skb); 72 ret = -EPERM; 73 break; 74 75 default: 76 pr_warn_once("bpf-lwt: Illegal return value %u, expect packet loss\n", ret); 77 kfree_skb(skb); 78 ret = -EINVAL; 79 break; 80 } 81 82 bpf_net_ctx_clear(bpf_net_ctx); 83 local_bh_enable(); 84 85 return ret; 86 } 87 88 static int bpf_lwt_input_reroute(struct sk_buff *skb) 89 { 90 enum skb_drop_reason reason; 91 int err = -EINVAL; 92 93 if (skb->protocol == htons(ETH_P_IP)) { 94 struct net_device *dev = skb_dst(skb)->dev; 95 const struct iphdr *iph = ip_hdr(skb); 96 97 dev_hold(dev); 98 skb_dst_drop(skb); 99 reason = ip_route_input_noref(skb, iph->daddr, iph->saddr, 100 ip4h_dscp(iph), dev); 101 err = reason ? -EINVAL : 0; 102 dev_put(dev); 103 } else if (skb->protocol == htons(ETH_P_IPV6)) { 104 skb_dst_drop(skb); 105 if (IS_ENABLED(CONFIG_IPV6)) { 106 ip6_route_input(skb); 107 err = skb_dst(skb)->error; 108 } else { 109 err = -EAFNOSUPPORT; 110 } 111 } else { 112 err = -EAFNOSUPPORT; 113 } 114 115 if (err) 116 goto err; 117 return dst_input(skb); 118 119 err: 120 kfree_skb(skb); 121 return err; 122 } 123 124 static int bpf_input(struct sk_buff *skb) 125 { 126 struct dst_entry *dst = skb_dst(skb); 127 struct bpf_lwt *bpf; 128 int ret; 129 130 bpf = bpf_lwt_lwtunnel(dst->lwtstate); 131 if (bpf->in.prog) { 132 ret = run_lwt_bpf(skb, &bpf->in, dst, NO_REDIRECT); 133 if (ret < 0) 134 return ret; 135 if (ret == BPF_LWT_REROUTE) 136 return bpf_lwt_input_reroute(skb); 137 } 138 139 if (unlikely(!dst->lwtstate->orig_input)) { 140 kfree_skb(skb); 141 return -EINVAL; 142 } 143 144 return dst->lwtstate->orig_input(skb); 145 } 146 147 static int bpf_output(struct net *net, struct sock *sk, struct sk_buff *skb) 148 { 149 struct dst_entry *dst = skb_dst(skb); 150 struct bpf_lwt *bpf; 151 int ret; 152 153 bpf = bpf_lwt_lwtunnel(dst->lwtstate); 154 if (bpf->out.prog) { 155 ret = run_lwt_bpf(skb, &bpf->out, dst, NO_REDIRECT); 156 if (ret < 0) 157 return ret; 158 } 159 160 if (unlikely(!dst->lwtstate->orig_output)) { 161 pr_warn_once("orig_output not set on dst for prog %s\n", 162 bpf->out.name); 163 kfree_skb(skb); 164 return -EINVAL; 165 } 166 167 return dst->lwtstate->orig_output(net, sk, skb); 168 } 169 170 static int xmit_check_hhlen(struct sk_buff *skb, int hh_len) 171 { 172 if (skb_headroom(skb) < hh_len) { 173 int nhead = HH_DATA_ALIGN(hh_len - skb_headroom(skb)); 174 175 if (pskb_expand_head(skb, nhead, 0, GFP_ATOMIC)) 176 return -ENOMEM; 177 } 178 179 return 0; 180 } 181 182 static int bpf_lwt_xmit_reroute(struct sk_buff *skb) 183 { 184 struct net_device *l3mdev = l3mdev_master_dev_rcu(skb_dst(skb)->dev); 185 int oif = l3mdev ? l3mdev->ifindex : 0; 186 struct dst_entry *dst = NULL; 187 int err = -EAFNOSUPPORT; 188 struct sock *sk; 189 struct net *net; 190 bool ipv4; 191 192 if (skb->protocol == htons(ETH_P_IP)) 193 ipv4 = true; 194 else if (skb->protocol == htons(ETH_P_IPV6)) 195 ipv4 = false; 196 else 197 goto err; 198 199 sk = sk_to_full_sk(skb->sk); 200 if (sk) { 201 if (sk->sk_bound_dev_if) 202 oif = sk->sk_bound_dev_if; 203 net = sock_net(sk); 204 } else { 205 net = dev_net(skb_dst(skb)->dev); 206 } 207 208 if (ipv4) { 209 struct iphdr *iph = ip_hdr(skb); 210 struct flowi4 fl4 = {}; 211 struct rtable *rt; 212 213 fl4.flowi4_oif = oif; 214 fl4.flowi4_mark = skb->mark; 215 fl4.flowi4_uid = sock_net_uid(net, sk); 216 fl4.flowi4_dscp = ip4h_dscp(iph); 217 fl4.flowi4_flags = FLOWI_FLAG_ANYSRC; 218 fl4.flowi4_proto = iph->protocol; 219 fl4.daddr = iph->daddr; 220 fl4.saddr = iph->saddr; 221 222 rt = ip_route_output_key(net, &fl4); 223 if (IS_ERR(rt)) { 224 err = PTR_ERR(rt); 225 goto err; 226 } 227 dst = &rt->dst; 228 } else { 229 struct ipv6hdr *iph6 = ipv6_hdr(skb); 230 struct flowi6 fl6 = {}; 231 232 fl6.flowi6_oif = oif; 233 fl6.flowi6_mark = skb->mark; 234 fl6.flowi6_uid = sock_net_uid(net, sk); 235 fl6.flowlabel = ip6_flowinfo(iph6); 236 fl6.flowi6_proto = iph6->nexthdr; 237 fl6.daddr = iph6->daddr; 238 fl6.saddr = iph6->saddr; 239 240 dst = ip6_dst_lookup_flow(net, skb->sk, &fl6, NULL); 241 if (IS_ERR(dst)) { 242 err = PTR_ERR(dst); 243 goto err; 244 } 245 } 246 if (unlikely(dst->error)) { 247 err = dst->error; 248 dst_release(dst); 249 goto err; 250 } 251 252 /* Although skb header was reserved in bpf_lwt_push_ip_encap(), it 253 * was done for the previous dst, so we are doing it here again, in 254 * case the new dst needs much more space. The call below is a noop 255 * if there is enough header space in skb. 256 */ 257 err = skb_cow_head(skb, LL_RESERVED_SPACE(dst->dev)); 258 if (unlikely(err)) { 259 dst_release(dst); 260 goto err; 261 } 262 263 skb_dst_drop(skb); 264 skb_dst_set(skb, dst); 265 266 err = dst_output(dev_net(skb_dst(skb)->dev), skb->sk, skb); 267 if (unlikely(err)) 268 return net_xmit_errno(err); 269 270 /* ip[6]_finish_output2 understand LWTUNNEL_XMIT_DONE */ 271 return LWTUNNEL_XMIT_DONE; 272 273 err: 274 kfree_skb(skb); 275 return err; 276 } 277 278 static int bpf_xmit(struct sk_buff *skb) 279 { 280 struct dst_entry *dst = skb_dst(skb); 281 struct bpf_lwt *bpf; 282 283 bpf = bpf_lwt_lwtunnel(dst->lwtstate); 284 if (bpf->xmit.prog) { 285 int hh_len = dst->dev->hard_header_len; 286 __be16 proto = skb->protocol; 287 int ret; 288 289 ret = run_lwt_bpf(skb, &bpf->xmit, dst, CAN_REDIRECT); 290 switch (ret) { 291 case BPF_OK: 292 /* If the header changed, e.g. via bpf_lwt_push_encap, 293 * BPF_LWT_REROUTE below should have been used if the 294 * protocol was also changed. 295 */ 296 if (skb->protocol != proto) { 297 kfree_skb(skb); 298 return -EINVAL; 299 } 300 /* If the header was expanded, headroom might be too 301 * small for L2 header to come, expand as needed. 302 */ 303 ret = xmit_check_hhlen(skb, hh_len); 304 if (unlikely(ret)) 305 return ret; 306 307 return LWTUNNEL_XMIT_CONTINUE; 308 case BPF_REDIRECT: 309 return LWTUNNEL_XMIT_DONE; 310 case BPF_LWT_REROUTE: 311 return bpf_lwt_xmit_reroute(skb); 312 default: 313 return ret; 314 } 315 } 316 317 return LWTUNNEL_XMIT_CONTINUE; 318 } 319 320 static void bpf_lwt_prog_destroy(struct bpf_lwt_prog *prog) 321 { 322 if (prog->prog) 323 bpf_prog_put(prog->prog); 324 325 kfree(prog->name); 326 } 327 328 static void bpf_destroy_state(struct lwtunnel_state *lwt) 329 { 330 struct bpf_lwt *bpf = bpf_lwt_lwtunnel(lwt); 331 332 bpf_lwt_prog_destroy(&bpf->in); 333 bpf_lwt_prog_destroy(&bpf->out); 334 bpf_lwt_prog_destroy(&bpf->xmit); 335 } 336 337 static const struct nla_policy bpf_prog_policy[LWT_BPF_PROG_MAX + 1] = { 338 [LWT_BPF_PROG_FD] = { .type = NLA_U32, }, 339 [LWT_BPF_PROG_NAME] = { .type = NLA_NUL_STRING, 340 .len = MAX_PROG_NAME }, 341 }; 342 343 static int bpf_parse_prog(struct nlattr *attr, struct bpf_lwt_prog *prog, 344 enum bpf_prog_type type) 345 { 346 struct nlattr *tb[LWT_BPF_PROG_MAX + 1]; 347 struct bpf_prog *p; 348 int ret; 349 u32 fd; 350 351 ret = nla_parse_nested_deprecated(tb, LWT_BPF_PROG_MAX, attr, 352 bpf_prog_policy, NULL); 353 if (ret < 0) 354 return ret; 355 356 if (!tb[LWT_BPF_PROG_FD] || !tb[LWT_BPF_PROG_NAME]) 357 return -EINVAL; 358 359 prog->name = nla_memdup(tb[LWT_BPF_PROG_NAME], GFP_ATOMIC); 360 if (!prog->name) 361 return -ENOMEM; 362 363 fd = nla_get_u32(tb[LWT_BPF_PROG_FD]); 364 p = bpf_prog_get_type(fd, type); 365 if (IS_ERR(p)) 366 return PTR_ERR(p); 367 368 prog->prog = p; 369 370 return 0; 371 } 372 373 static const struct nla_policy bpf_nl_policy[LWT_BPF_MAX + 1] = { 374 [LWT_BPF_IN] = { .type = NLA_NESTED, }, 375 [LWT_BPF_OUT] = { .type = NLA_NESTED, }, 376 [LWT_BPF_XMIT] = { .type = NLA_NESTED, }, 377 [LWT_BPF_XMIT_HEADROOM] = { .type = NLA_U32 }, 378 }; 379 380 static int bpf_build_state(struct net *net, struct nlattr *nla, 381 unsigned int family, const void *cfg, 382 struct lwtunnel_state **ts, 383 struct netlink_ext_ack *extack) 384 { 385 struct nlattr *tb[LWT_BPF_MAX + 1]; 386 struct lwtunnel_state *newts; 387 struct bpf_lwt *bpf; 388 int ret; 389 390 if (family != AF_INET && family != AF_INET6) 391 return -EAFNOSUPPORT; 392 393 ret = nla_parse_nested_deprecated(tb, LWT_BPF_MAX, nla, bpf_nl_policy, 394 extack); 395 if (ret < 0) 396 return ret; 397 398 if (!tb[LWT_BPF_IN] && !tb[LWT_BPF_OUT] && !tb[LWT_BPF_XMIT]) 399 return -EINVAL; 400 401 newts = lwtunnel_state_alloc(sizeof(*bpf)); 402 if (!newts) 403 return -ENOMEM; 404 405 newts->type = LWTUNNEL_ENCAP_BPF; 406 bpf = bpf_lwt_lwtunnel(newts); 407 408 if (tb[LWT_BPF_IN]) { 409 newts->flags |= LWTUNNEL_STATE_INPUT_REDIRECT; 410 ret = bpf_parse_prog(tb[LWT_BPF_IN], &bpf->in, 411 BPF_PROG_TYPE_LWT_IN); 412 if (ret < 0) 413 goto errout; 414 } 415 416 if (tb[LWT_BPF_OUT]) { 417 newts->flags |= LWTUNNEL_STATE_OUTPUT_REDIRECT; 418 ret = bpf_parse_prog(tb[LWT_BPF_OUT], &bpf->out, 419 BPF_PROG_TYPE_LWT_OUT); 420 if (ret < 0) 421 goto errout; 422 } 423 424 if (tb[LWT_BPF_XMIT]) { 425 newts->flags |= LWTUNNEL_STATE_XMIT_REDIRECT; 426 ret = bpf_parse_prog(tb[LWT_BPF_XMIT], &bpf->xmit, 427 BPF_PROG_TYPE_LWT_XMIT); 428 if (ret < 0) 429 goto errout; 430 } 431 432 if (tb[LWT_BPF_XMIT_HEADROOM]) { 433 u32 headroom = nla_get_u32(tb[LWT_BPF_XMIT_HEADROOM]); 434 435 if (headroom > LWT_BPF_MAX_HEADROOM) { 436 ret = -ERANGE; 437 goto errout; 438 } 439 440 newts->headroom = headroom; 441 } 442 443 bpf->family = family; 444 *ts = newts; 445 446 return 0; 447 448 errout: 449 bpf_destroy_state(newts); 450 kfree(newts); 451 return ret; 452 } 453 454 static int bpf_fill_lwt_prog(struct sk_buff *skb, int attr, 455 struct bpf_lwt_prog *prog) 456 { 457 struct nlattr *nest; 458 459 if (!prog->prog) 460 return 0; 461 462 nest = nla_nest_start_noflag(skb, attr); 463 if (!nest) 464 return -EMSGSIZE; 465 466 if (prog->name && 467 nla_put_string(skb, LWT_BPF_PROG_NAME, prog->name)) 468 return -EMSGSIZE; 469 470 return nla_nest_end(skb, nest); 471 } 472 473 static int bpf_fill_encap_info(struct sk_buff *skb, struct lwtunnel_state *lwt) 474 { 475 struct bpf_lwt *bpf = bpf_lwt_lwtunnel(lwt); 476 477 if (bpf_fill_lwt_prog(skb, LWT_BPF_IN, &bpf->in) < 0 || 478 bpf_fill_lwt_prog(skb, LWT_BPF_OUT, &bpf->out) < 0 || 479 bpf_fill_lwt_prog(skb, LWT_BPF_XMIT, &bpf->xmit) < 0) 480 return -EMSGSIZE; 481 482 return 0; 483 } 484 485 static int bpf_encap_nlsize(struct lwtunnel_state *lwtstate) 486 { 487 int nest_len = nla_total_size(sizeof(struct nlattr)) + 488 nla_total_size(MAX_PROG_NAME) + /* LWT_BPF_PROG_NAME */ 489 0; 490 491 return nest_len + /* LWT_BPF_IN */ 492 nest_len + /* LWT_BPF_OUT */ 493 nest_len + /* LWT_BPF_XMIT */ 494 0; 495 } 496 497 static int bpf_lwt_prog_cmp(struct bpf_lwt_prog *a, struct bpf_lwt_prog *b) 498 { 499 /* FIXME: 500 * The LWT state is currently rebuilt for delete requests which 501 * results in a new bpf_prog instance. Comparing names for now. 502 */ 503 if (!a->name && !b->name) 504 return 0; 505 506 if (!a->name || !b->name) 507 return 1; 508 509 return strcmp(a->name, b->name); 510 } 511 512 static int bpf_encap_cmp(struct lwtunnel_state *a, struct lwtunnel_state *b) 513 { 514 struct bpf_lwt *a_bpf = bpf_lwt_lwtunnel(a); 515 struct bpf_lwt *b_bpf = bpf_lwt_lwtunnel(b); 516 517 return bpf_lwt_prog_cmp(&a_bpf->in, &b_bpf->in) || 518 bpf_lwt_prog_cmp(&a_bpf->out, &b_bpf->out) || 519 bpf_lwt_prog_cmp(&a_bpf->xmit, &b_bpf->xmit); 520 } 521 522 static const struct lwtunnel_encap_ops bpf_encap_ops = { 523 .build_state = bpf_build_state, 524 .destroy_state = bpf_destroy_state, 525 .input = bpf_input, 526 .output = bpf_output, 527 .xmit = bpf_xmit, 528 .fill_encap = bpf_fill_encap_info, 529 .get_encap_size = bpf_encap_nlsize, 530 .cmp_encap = bpf_encap_cmp, 531 .owner = THIS_MODULE, 532 }; 533 534 static int handle_gso_type(struct sk_buff *skb, unsigned int gso_type, 535 int encap_len) 536 { 537 struct skb_shared_info *shinfo = skb_shinfo(skb); 538 539 gso_type |= SKB_GSO_DODGY; 540 shinfo->gso_type |= gso_type; 541 skb_decrease_gso_size(shinfo, encap_len); 542 shinfo->gso_segs = 0; 543 return 0; 544 } 545 546 static int handle_gso_encap(struct sk_buff *skb, bool ipv4, int encap_len) 547 { 548 int next_hdr_offset; 549 void *next_hdr; 550 __u8 protocol; 551 552 /* SCTP and UDP_L4 gso need more nuanced handling than what 553 * handle_gso_type() does above: skb_decrease_gso_size() is not enough. 554 * So at the moment only TCP GSO packets are let through. 555 */ 556 if (!(skb_shinfo(skb)->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) 557 return -ENOTSUPP; 558 559 if (ipv4) { 560 protocol = ip_hdr(skb)->protocol; 561 next_hdr_offset = sizeof(struct iphdr); 562 next_hdr = skb_network_header(skb) + next_hdr_offset; 563 } else { 564 protocol = ipv6_hdr(skb)->nexthdr; 565 next_hdr_offset = sizeof(struct ipv6hdr); 566 next_hdr = skb_network_header(skb) + next_hdr_offset; 567 } 568 569 switch (protocol) { 570 case IPPROTO_GRE: 571 next_hdr_offset += sizeof(struct gre_base_hdr); 572 if (next_hdr_offset > encap_len) 573 return -EINVAL; 574 575 if (((struct gre_base_hdr *)next_hdr)->flags & GRE_CSUM) 576 return handle_gso_type(skb, SKB_GSO_GRE_CSUM, 577 encap_len); 578 return handle_gso_type(skb, SKB_GSO_GRE, encap_len); 579 580 case IPPROTO_UDP: 581 next_hdr_offset += sizeof(struct udphdr); 582 if (next_hdr_offset > encap_len) 583 return -EINVAL; 584 585 if (((struct udphdr *)next_hdr)->check) 586 return handle_gso_type(skb, SKB_GSO_UDP_TUNNEL_CSUM, 587 encap_len); 588 return handle_gso_type(skb, SKB_GSO_UDP_TUNNEL, encap_len); 589 590 case IPPROTO_IP: 591 case IPPROTO_IPV6: 592 if (ipv4) 593 return handle_gso_type(skb, SKB_GSO_IPXIP4, encap_len); 594 else 595 return handle_gso_type(skb, SKB_GSO_IPXIP6, encap_len); 596 597 default: 598 return -EPROTONOSUPPORT; 599 } 600 } 601 602 int bpf_lwt_push_ip_encap(struct sk_buff *skb, void *hdr, u32 len, bool ingress) 603 { 604 bool is_udp_tunnel; 605 struct iphdr *iph; 606 bool ipv4; 607 int err; 608 609 if (unlikely(len < sizeof(struct iphdr) || len > LWT_BPF_MAX_HEADROOM)) 610 return -EINVAL; 611 612 /* validate protocol and length */ 613 iph = (struct iphdr *)hdr; 614 if (iph->version == 4) { 615 ipv4 = true; 616 if (unlikely(len < iph->ihl * 4)) 617 return -EINVAL; 618 is_udp_tunnel = iph->protocol == IPPROTO_UDP; 619 if (unlikely(is_udp_tunnel && len < iph->ihl * 4 + sizeof(struct udphdr))) 620 return -EINVAL; 621 } else if (iph->version == 6) { 622 ipv4 = false; 623 if (unlikely(len < sizeof(struct ipv6hdr))) 624 return -EINVAL; 625 is_udp_tunnel = ((struct ipv6hdr *)iph)->nexthdr == NEXTHDR_UDP; 626 if (unlikely(is_udp_tunnel && len < sizeof(struct ipv6hdr) + sizeof(struct udphdr))) 627 return -EINVAL; 628 } else { 629 return -EINVAL; 630 } 631 632 if (ingress) 633 err = skb_cow_head(skb, len + skb->mac_len); 634 else 635 err = skb_cow_head(skb, 636 len + LL_RESERVED_SPACE(skb_dst(skb)->dev)); 637 if (unlikely(err)) 638 return err; 639 640 /* push the encap headers and fix pointers */ 641 skb_reset_inner_headers(skb); 642 skb_reset_inner_mac_header(skb); /* mac header is not yet set */ 643 skb_set_inner_protocol(skb, skb->protocol); 644 skb->encapsulation = 1; 645 skb_push(skb, len); 646 if (ingress) 647 skb_postpush_rcsum(skb, iph, len); 648 skb_reset_network_header(skb); 649 if (is_udp_tunnel) { 650 size_t iph_sz = ipv4 ? iph->ihl * 4 : sizeof(struct ipv6hdr); 651 652 skb_set_transport_header(skb, skb_network_offset(skb) + iph_sz); 653 } 654 memcpy(skb_network_header(skb), hdr, len); 655 bpf_compute_data_pointers(skb); 656 skb_clear_hash(skb); 657 658 if (ipv4) { 659 skb->protocol = htons(ETH_P_IP); 660 iph = ip_hdr(skb); 661 662 if (!iph->check) 663 iph->check = ip_fast_csum((unsigned char *)iph, 664 iph->ihl); 665 } else { 666 skb->protocol = htons(ETH_P_IPV6); 667 } 668 669 if (skb_is_gso(skb)) 670 return handle_gso_encap(skb, ipv4, len); 671 672 return 0; 673 } 674 675 static int __init bpf_lwt_init(void) 676 { 677 return lwtunnel_encap_add_ops(&bpf_encap_ops, LWTUNNEL_ENCAP_BPF); 678 } 679 680 subsys_initcall(bpf_lwt_init) 681