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 goto err; 260 261 skb_dst_drop(skb); 262 skb_dst_set(skb, dst); 263 264 err = dst_output(dev_net(skb_dst(skb)->dev), skb->sk, skb); 265 if (unlikely(err)) 266 return net_xmit_errno(err); 267 268 /* ip[6]_finish_output2 understand LWTUNNEL_XMIT_DONE */ 269 return LWTUNNEL_XMIT_DONE; 270 271 err: 272 kfree_skb(skb); 273 return err; 274 } 275 276 static int bpf_xmit(struct sk_buff *skb) 277 { 278 struct dst_entry *dst = skb_dst(skb); 279 struct bpf_lwt *bpf; 280 281 bpf = bpf_lwt_lwtunnel(dst->lwtstate); 282 if (bpf->xmit.prog) { 283 int hh_len = dst->dev->hard_header_len; 284 __be16 proto = skb->protocol; 285 int ret; 286 287 ret = run_lwt_bpf(skb, &bpf->xmit, dst, CAN_REDIRECT); 288 switch (ret) { 289 case BPF_OK: 290 /* If the header changed, e.g. via bpf_lwt_push_encap, 291 * BPF_LWT_REROUTE below should have been used if the 292 * protocol was also changed. 293 */ 294 if (skb->protocol != proto) { 295 kfree_skb(skb); 296 return -EINVAL; 297 } 298 /* If the header was expanded, headroom might be too 299 * small for L2 header to come, expand as needed. 300 */ 301 ret = xmit_check_hhlen(skb, hh_len); 302 if (unlikely(ret)) 303 return ret; 304 305 return LWTUNNEL_XMIT_CONTINUE; 306 case BPF_REDIRECT: 307 return LWTUNNEL_XMIT_DONE; 308 case BPF_LWT_REROUTE: 309 return bpf_lwt_xmit_reroute(skb); 310 default: 311 return ret; 312 } 313 } 314 315 return LWTUNNEL_XMIT_CONTINUE; 316 } 317 318 static void bpf_lwt_prog_destroy(struct bpf_lwt_prog *prog) 319 { 320 if (prog->prog) 321 bpf_prog_put(prog->prog); 322 323 kfree(prog->name); 324 } 325 326 static void bpf_destroy_state(struct lwtunnel_state *lwt) 327 { 328 struct bpf_lwt *bpf = bpf_lwt_lwtunnel(lwt); 329 330 bpf_lwt_prog_destroy(&bpf->in); 331 bpf_lwt_prog_destroy(&bpf->out); 332 bpf_lwt_prog_destroy(&bpf->xmit); 333 } 334 335 static const struct nla_policy bpf_prog_policy[LWT_BPF_PROG_MAX + 1] = { 336 [LWT_BPF_PROG_FD] = { .type = NLA_U32, }, 337 [LWT_BPF_PROG_NAME] = { .type = NLA_NUL_STRING, 338 .len = MAX_PROG_NAME }, 339 }; 340 341 static int bpf_parse_prog(struct nlattr *attr, struct bpf_lwt_prog *prog, 342 enum bpf_prog_type type) 343 { 344 struct nlattr *tb[LWT_BPF_PROG_MAX + 1]; 345 struct bpf_prog *p; 346 int ret; 347 u32 fd; 348 349 ret = nla_parse_nested_deprecated(tb, LWT_BPF_PROG_MAX, attr, 350 bpf_prog_policy, NULL); 351 if (ret < 0) 352 return ret; 353 354 if (!tb[LWT_BPF_PROG_FD] || !tb[LWT_BPF_PROG_NAME]) 355 return -EINVAL; 356 357 prog->name = nla_memdup(tb[LWT_BPF_PROG_NAME], GFP_ATOMIC); 358 if (!prog->name) 359 return -ENOMEM; 360 361 fd = nla_get_u32(tb[LWT_BPF_PROG_FD]); 362 p = bpf_prog_get_type(fd, type); 363 if (IS_ERR(p)) 364 return PTR_ERR(p); 365 366 prog->prog = p; 367 368 return 0; 369 } 370 371 static const struct nla_policy bpf_nl_policy[LWT_BPF_MAX + 1] = { 372 [LWT_BPF_IN] = { .type = NLA_NESTED, }, 373 [LWT_BPF_OUT] = { .type = NLA_NESTED, }, 374 [LWT_BPF_XMIT] = { .type = NLA_NESTED, }, 375 [LWT_BPF_XMIT_HEADROOM] = { .type = NLA_U32 }, 376 }; 377 378 static int bpf_build_state(struct net *net, struct nlattr *nla, 379 unsigned int family, const void *cfg, 380 struct lwtunnel_state **ts, 381 struct netlink_ext_ack *extack) 382 { 383 struct nlattr *tb[LWT_BPF_MAX + 1]; 384 struct lwtunnel_state *newts; 385 struct bpf_lwt *bpf; 386 int ret; 387 388 if (family != AF_INET && family != AF_INET6) 389 return -EAFNOSUPPORT; 390 391 ret = nla_parse_nested_deprecated(tb, LWT_BPF_MAX, nla, bpf_nl_policy, 392 extack); 393 if (ret < 0) 394 return ret; 395 396 if (!tb[LWT_BPF_IN] && !tb[LWT_BPF_OUT] && !tb[LWT_BPF_XMIT]) 397 return -EINVAL; 398 399 newts = lwtunnel_state_alloc(sizeof(*bpf)); 400 if (!newts) 401 return -ENOMEM; 402 403 newts->type = LWTUNNEL_ENCAP_BPF; 404 bpf = bpf_lwt_lwtunnel(newts); 405 406 if (tb[LWT_BPF_IN]) { 407 newts->flags |= LWTUNNEL_STATE_INPUT_REDIRECT; 408 ret = bpf_parse_prog(tb[LWT_BPF_IN], &bpf->in, 409 BPF_PROG_TYPE_LWT_IN); 410 if (ret < 0) 411 goto errout; 412 } 413 414 if (tb[LWT_BPF_OUT]) { 415 newts->flags |= LWTUNNEL_STATE_OUTPUT_REDIRECT; 416 ret = bpf_parse_prog(tb[LWT_BPF_OUT], &bpf->out, 417 BPF_PROG_TYPE_LWT_OUT); 418 if (ret < 0) 419 goto errout; 420 } 421 422 if (tb[LWT_BPF_XMIT]) { 423 newts->flags |= LWTUNNEL_STATE_XMIT_REDIRECT; 424 ret = bpf_parse_prog(tb[LWT_BPF_XMIT], &bpf->xmit, 425 BPF_PROG_TYPE_LWT_XMIT); 426 if (ret < 0) 427 goto errout; 428 } 429 430 if (tb[LWT_BPF_XMIT_HEADROOM]) { 431 u32 headroom = nla_get_u32(tb[LWT_BPF_XMIT_HEADROOM]); 432 433 if (headroom > LWT_BPF_MAX_HEADROOM) { 434 ret = -ERANGE; 435 goto errout; 436 } 437 438 newts->headroom = headroom; 439 } 440 441 bpf->family = family; 442 *ts = newts; 443 444 return 0; 445 446 errout: 447 bpf_destroy_state(newts); 448 kfree(newts); 449 return ret; 450 } 451 452 static int bpf_fill_lwt_prog(struct sk_buff *skb, int attr, 453 struct bpf_lwt_prog *prog) 454 { 455 struct nlattr *nest; 456 457 if (!prog->prog) 458 return 0; 459 460 nest = nla_nest_start_noflag(skb, attr); 461 if (!nest) 462 return -EMSGSIZE; 463 464 if (prog->name && 465 nla_put_string(skb, LWT_BPF_PROG_NAME, prog->name)) 466 return -EMSGSIZE; 467 468 return nla_nest_end(skb, nest); 469 } 470 471 static int bpf_fill_encap_info(struct sk_buff *skb, struct lwtunnel_state *lwt) 472 { 473 struct bpf_lwt *bpf = bpf_lwt_lwtunnel(lwt); 474 475 if (bpf_fill_lwt_prog(skb, LWT_BPF_IN, &bpf->in) < 0 || 476 bpf_fill_lwt_prog(skb, LWT_BPF_OUT, &bpf->out) < 0 || 477 bpf_fill_lwt_prog(skb, LWT_BPF_XMIT, &bpf->xmit) < 0) 478 return -EMSGSIZE; 479 480 return 0; 481 } 482 483 static int bpf_encap_nlsize(struct lwtunnel_state *lwtstate) 484 { 485 int nest_len = nla_total_size(sizeof(struct nlattr)) + 486 nla_total_size(MAX_PROG_NAME) + /* LWT_BPF_PROG_NAME */ 487 0; 488 489 return nest_len + /* LWT_BPF_IN */ 490 nest_len + /* LWT_BPF_OUT */ 491 nest_len + /* LWT_BPF_XMIT */ 492 0; 493 } 494 495 static int bpf_lwt_prog_cmp(struct bpf_lwt_prog *a, struct bpf_lwt_prog *b) 496 { 497 /* FIXME: 498 * The LWT state is currently rebuilt for delete requests which 499 * results in a new bpf_prog instance. Comparing names for now. 500 */ 501 if (!a->name && !b->name) 502 return 0; 503 504 if (!a->name || !b->name) 505 return 1; 506 507 return strcmp(a->name, b->name); 508 } 509 510 static int bpf_encap_cmp(struct lwtunnel_state *a, struct lwtunnel_state *b) 511 { 512 struct bpf_lwt *a_bpf = bpf_lwt_lwtunnel(a); 513 struct bpf_lwt *b_bpf = bpf_lwt_lwtunnel(b); 514 515 return bpf_lwt_prog_cmp(&a_bpf->in, &b_bpf->in) || 516 bpf_lwt_prog_cmp(&a_bpf->out, &b_bpf->out) || 517 bpf_lwt_prog_cmp(&a_bpf->xmit, &b_bpf->xmit); 518 } 519 520 static const struct lwtunnel_encap_ops bpf_encap_ops = { 521 .build_state = bpf_build_state, 522 .destroy_state = bpf_destroy_state, 523 .input = bpf_input, 524 .output = bpf_output, 525 .xmit = bpf_xmit, 526 .fill_encap = bpf_fill_encap_info, 527 .get_encap_size = bpf_encap_nlsize, 528 .cmp_encap = bpf_encap_cmp, 529 .owner = THIS_MODULE, 530 }; 531 532 static int handle_gso_type(struct sk_buff *skb, unsigned int gso_type, 533 int encap_len) 534 { 535 struct skb_shared_info *shinfo = skb_shinfo(skb); 536 537 gso_type |= SKB_GSO_DODGY; 538 shinfo->gso_type |= gso_type; 539 skb_decrease_gso_size(shinfo, encap_len); 540 shinfo->gso_segs = 0; 541 return 0; 542 } 543 544 static int handle_gso_encap(struct sk_buff *skb, bool ipv4, int encap_len) 545 { 546 int next_hdr_offset; 547 void *next_hdr; 548 __u8 protocol; 549 550 /* SCTP and UDP_L4 gso need more nuanced handling than what 551 * handle_gso_type() does above: skb_decrease_gso_size() is not enough. 552 * So at the moment only TCP GSO packets are let through. 553 */ 554 if (!(skb_shinfo(skb)->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) 555 return -ENOTSUPP; 556 557 if (ipv4) { 558 protocol = ip_hdr(skb)->protocol; 559 next_hdr_offset = sizeof(struct iphdr); 560 next_hdr = skb_network_header(skb) + next_hdr_offset; 561 } else { 562 protocol = ipv6_hdr(skb)->nexthdr; 563 next_hdr_offset = sizeof(struct ipv6hdr); 564 next_hdr = skb_network_header(skb) + next_hdr_offset; 565 } 566 567 switch (protocol) { 568 case IPPROTO_GRE: 569 next_hdr_offset += sizeof(struct gre_base_hdr); 570 if (next_hdr_offset > encap_len) 571 return -EINVAL; 572 573 if (((struct gre_base_hdr *)next_hdr)->flags & GRE_CSUM) 574 return handle_gso_type(skb, SKB_GSO_GRE_CSUM, 575 encap_len); 576 return handle_gso_type(skb, SKB_GSO_GRE, encap_len); 577 578 case IPPROTO_UDP: 579 next_hdr_offset += sizeof(struct udphdr); 580 if (next_hdr_offset > encap_len) 581 return -EINVAL; 582 583 if (((struct udphdr *)next_hdr)->check) 584 return handle_gso_type(skb, SKB_GSO_UDP_TUNNEL_CSUM, 585 encap_len); 586 return handle_gso_type(skb, SKB_GSO_UDP_TUNNEL, encap_len); 587 588 case IPPROTO_IP: 589 case IPPROTO_IPV6: 590 if (ipv4) 591 return handle_gso_type(skb, SKB_GSO_IPXIP4, encap_len); 592 else 593 return handle_gso_type(skb, SKB_GSO_IPXIP6, encap_len); 594 595 default: 596 return -EPROTONOSUPPORT; 597 } 598 } 599 600 int bpf_lwt_push_ip_encap(struct sk_buff *skb, void *hdr, u32 len, bool ingress) 601 { 602 struct iphdr *iph; 603 bool ipv4; 604 int err; 605 606 if (unlikely(len < sizeof(struct iphdr) || len > LWT_BPF_MAX_HEADROOM)) 607 return -EINVAL; 608 609 /* validate protocol and length */ 610 iph = (struct iphdr *)hdr; 611 if (iph->version == 4) { 612 ipv4 = true; 613 if (unlikely(len < iph->ihl * 4)) 614 return -EINVAL; 615 } else if (iph->version == 6) { 616 ipv4 = false; 617 if (unlikely(len < sizeof(struct ipv6hdr))) 618 return -EINVAL; 619 } else { 620 return -EINVAL; 621 } 622 623 if (ingress) 624 err = skb_cow_head(skb, len + skb->mac_len); 625 else 626 err = skb_cow_head(skb, 627 len + LL_RESERVED_SPACE(skb_dst(skb)->dev)); 628 if (unlikely(err)) 629 return err; 630 631 /* push the encap headers and fix pointers */ 632 skb_reset_inner_headers(skb); 633 skb_reset_inner_mac_header(skb); /* mac header is not yet set */ 634 skb_set_inner_protocol(skb, skb->protocol); 635 skb->encapsulation = 1; 636 skb_push(skb, len); 637 if (ingress) 638 skb_postpush_rcsum(skb, iph, len); 639 skb_reset_network_header(skb); 640 memcpy(skb_network_header(skb), hdr, len); 641 bpf_compute_data_pointers(skb); 642 skb_clear_hash(skb); 643 644 if (ipv4) { 645 skb->protocol = htons(ETH_P_IP); 646 iph = ip_hdr(skb); 647 648 if (!iph->check) 649 iph->check = ip_fast_csum((unsigned char *)iph, 650 iph->ihl); 651 } else { 652 skb->protocol = htons(ETH_P_IPV6); 653 } 654 655 if (skb_is_gso(skb)) 656 return handle_gso_encap(skb, ipv4, len); 657 658 return 0; 659 } 660 661 static int __init bpf_lwt_init(void) 662 { 663 return lwtunnel_encap_add_ops(&bpf_encap_ops, LWTUNNEL_ENCAP_BPF); 664 } 665 666 subsys_initcall(bpf_lwt_init) 667