1 // SPDX-License-Identifier: GPL-2.0-only 2 /* Copyright (c) 2017 Facebook 3 */ 4 #include <linux/bpf.h> 5 #include <linux/btf.h> 6 #include <linux/btf_ids.h> 7 #include <linux/slab.h> 8 #include <linux/init.h> 9 #include <linux/vmalloc.h> 10 #include <linux/etherdevice.h> 11 #include <linux/filter.h> 12 #include <linux/rcupdate_trace.h> 13 #include <linux/sched/signal.h> 14 #include <net/bpf_sk_storage.h> 15 #include <net/hotdata.h> 16 #include <net/sock.h> 17 #include <net/tcp.h> 18 #include <net/net_namespace.h> 19 #include <net/page_pool/helpers.h> 20 #include <linux/error-injection.h> 21 #include <linux/smp.h> 22 #include <linux/sock_diag.h> 23 #include <linux/netfilter.h> 24 #include <net/netdev_rx_queue.h> 25 #include <net/xdp.h> 26 #include <net/netfilter/nf_bpf_link.h> 27 28 #define CREATE_TRACE_POINTS 29 #include <trace/events/bpf_test_run.h> 30 31 struct bpf_test_timer { 32 u32 i; 33 u64 time_start, time_spent; 34 }; 35 36 static void bpf_test_timer_enter(struct bpf_test_timer *t) 37 __acquires(rcu) 38 { 39 rcu_read_lock_dont_migrate(); 40 t->time_start = ktime_get_ns(); 41 } 42 43 static void bpf_test_timer_leave(struct bpf_test_timer *t) 44 __releases(rcu) 45 { 46 t->time_start = 0; 47 rcu_read_unlock_migrate(); 48 } 49 50 static bool bpf_test_timer_continue(struct bpf_test_timer *t, int iterations, 51 u32 repeat, int *err, u32 *duration) 52 __must_hold(rcu) 53 { 54 t->i += iterations; 55 if (t->i >= repeat) { 56 /* We're done. */ 57 t->time_spent += ktime_get_ns() - t->time_start; 58 do_div(t->time_spent, t->i); 59 *duration = t->time_spent > U32_MAX ? U32_MAX : (u32)t->time_spent; 60 *err = 0; 61 goto reset; 62 } 63 64 if (signal_pending(current)) { 65 /* During iteration: we've been cancelled, abort. */ 66 *err = -EINTR; 67 goto reset; 68 } 69 70 if (need_resched()) { 71 /* During iteration: we need to reschedule between runs. */ 72 t->time_spent += ktime_get_ns() - t->time_start; 73 bpf_test_timer_leave(t); 74 cond_resched(); 75 bpf_test_timer_enter(t); 76 } 77 78 /* Do another round. */ 79 return true; 80 81 reset: 82 t->i = 0; 83 return false; 84 } 85 86 /* We put this struct at the head of each page with a context and frame 87 * initialised when the page is allocated, so we don't have to do this on each 88 * repetition of the test run. 89 */ 90 struct xdp_page_head { 91 struct xdp_buff orig_ctx; 92 struct xdp_buff ctx; 93 union { 94 /* ::data_hard_start starts here */ 95 DECLARE_FLEX_ARRAY(struct xdp_frame, frame); 96 DECLARE_FLEX_ARRAY(u8, data); 97 }; 98 }; 99 100 struct xdp_test_data { 101 struct xdp_buff *orig_ctx; 102 struct xdp_rxq_info rxq; 103 struct net_device *dev; 104 struct page_pool *pp; 105 struct xdp_frame **frames; 106 struct sk_buff **skbs; 107 struct xdp_mem_info mem; 108 u32 batch_size; 109 u32 frame_cnt; 110 }; 111 112 /* tools/testing/selftests/bpf/prog_tests/xdp_do_redirect.c:%MAX_PKT_SIZE 113 * must be updated accordingly this gets changed, otherwise BPF selftests 114 * will fail. 115 */ 116 #define TEST_XDP_FRAME_SIZE (PAGE_SIZE - sizeof(struct xdp_page_head)) 117 #define TEST_XDP_MAX_BATCH 256 118 119 static void xdp_test_run_init_page(netmem_ref netmem, void *arg) 120 { 121 struct xdp_page_head *head = 122 phys_to_virt(page_to_phys(netmem_to_page(netmem))); 123 struct xdp_buff *new_ctx, *orig_ctx; 124 u32 headroom = XDP_PACKET_HEADROOM; 125 struct xdp_test_data *xdp = arg; 126 size_t frm_len, meta_len; 127 struct xdp_frame *frm; 128 void *data; 129 130 orig_ctx = xdp->orig_ctx; 131 frm_len = orig_ctx->data_end - orig_ctx->data_meta; 132 meta_len = orig_ctx->data - orig_ctx->data_meta; 133 headroom -= meta_len; 134 135 new_ctx = &head->ctx; 136 frm = head->frame; 137 data = head->data; 138 memcpy(data + headroom, orig_ctx->data_meta, frm_len); 139 140 xdp_init_buff(new_ctx, TEST_XDP_FRAME_SIZE, &xdp->rxq); 141 xdp_prepare_buff(new_ctx, data, headroom, frm_len, true); 142 new_ctx->data = new_ctx->data_meta + meta_len; 143 144 xdp_update_frame_from_buff(new_ctx, frm); 145 frm->mem_type = new_ctx->rxq->mem.type; 146 147 memcpy(&head->orig_ctx, new_ctx, sizeof(head->orig_ctx)); 148 } 149 150 static int xdp_test_run_setup(struct xdp_test_data *xdp, struct xdp_buff *orig_ctx) 151 { 152 struct page_pool *pp; 153 int err = -ENOMEM; 154 struct page_pool_params pp_params = { 155 .order = 0, 156 .flags = 0, 157 .pool_size = xdp->batch_size, 158 .nid = NUMA_NO_NODE, 159 .init_callback = xdp_test_run_init_page, 160 .init_arg = xdp, 161 }; 162 163 xdp->frames = kvmalloc_array(xdp->batch_size, sizeof(void *), GFP_KERNEL); 164 if (!xdp->frames) 165 return -ENOMEM; 166 167 xdp->skbs = kvmalloc_array(xdp->batch_size, sizeof(void *), GFP_KERNEL); 168 if (!xdp->skbs) 169 goto err_skbs; 170 171 pp = page_pool_create(&pp_params); 172 if (IS_ERR(pp)) { 173 err = PTR_ERR(pp); 174 goto err_pp; 175 } 176 177 /* will copy 'mem.id' into pp->xdp_mem_id */ 178 err = xdp_reg_mem_model(&xdp->mem, MEM_TYPE_PAGE_POOL, pp); 179 if (err) 180 goto err_mmodel; 181 182 xdp->pp = pp; 183 184 /* We create a 'fake' RXQ referencing the original dev, but with an 185 * xdp_mem_info pointing to our page_pool 186 */ 187 xdp_rxq_info_reg(&xdp->rxq, orig_ctx->rxq->dev, 0, 0); 188 xdp->rxq.mem.type = MEM_TYPE_PAGE_POOL; 189 xdp->rxq.mem.id = pp->xdp_mem_id; 190 xdp->dev = orig_ctx->rxq->dev; 191 xdp->orig_ctx = orig_ctx; 192 193 return 0; 194 195 err_mmodel: 196 page_pool_destroy(pp); 197 err_pp: 198 kvfree(xdp->skbs); 199 err_skbs: 200 kvfree(xdp->frames); 201 return err; 202 } 203 204 static void xdp_test_run_teardown(struct xdp_test_data *xdp) 205 { 206 xdp_unreg_mem_model(&xdp->mem); 207 page_pool_destroy(xdp->pp); 208 kfree(xdp->frames); 209 kfree(xdp->skbs); 210 } 211 212 static bool frame_was_changed(const struct xdp_page_head *head) 213 { 214 /* xdp_scrub_frame() zeroes the data pointer, flags is the last field, 215 * i.e. has the highest chances to be overwritten. If those two are 216 * untouched, it's most likely safe to skip the context reset. 217 */ 218 return head->frame->data != head->orig_ctx.data || 219 head->frame->flags != head->orig_ctx.flags; 220 } 221 222 static bool ctx_was_changed(struct xdp_page_head *head) 223 { 224 return head->orig_ctx.data != head->ctx.data || 225 head->orig_ctx.data_meta != head->ctx.data_meta || 226 head->orig_ctx.data_end != head->ctx.data_end; 227 } 228 229 static void reset_ctx(struct xdp_page_head *head) 230 { 231 if (likely(!frame_was_changed(head) && !ctx_was_changed(head))) 232 return; 233 234 head->ctx.data = head->orig_ctx.data; 235 head->ctx.data_meta = head->orig_ctx.data_meta; 236 head->ctx.data_end = head->orig_ctx.data_end; 237 xdp_update_frame_from_buff(&head->ctx, head->frame); 238 head->frame->mem_type = head->orig_ctx.rxq->mem.type; 239 } 240 241 static int xdp_recv_frames(struct xdp_frame **frames, int nframes, 242 struct sk_buff **skbs, 243 struct net_device *dev) 244 { 245 gfp_t gfp = __GFP_ZERO | GFP_ATOMIC; 246 int i; 247 LIST_HEAD(list); 248 249 if (unlikely(!kmem_cache_alloc_bulk(net_hotdata.skbuff_cache, gfp, 250 nframes, (void **)skbs))) { 251 for (i = 0; i < nframes; i++) 252 xdp_return_frame(frames[i]); 253 return -ENOMEM; 254 } 255 256 for (i = 0; i < nframes; i++) { 257 struct xdp_frame *xdpf = frames[i]; 258 struct sk_buff *skb = skbs[i]; 259 260 skb = __xdp_build_skb_from_frame(xdpf, skb, dev); 261 if (!skb) { 262 xdp_return_frame(xdpf); 263 continue; 264 } 265 266 list_add_tail(&skb->list, &list); 267 } 268 netif_receive_skb_list(&list); 269 270 return 0; 271 } 272 273 static int xdp_test_run_batch(struct xdp_test_data *xdp, struct bpf_prog *prog, 274 u32 repeat) 275 { 276 struct bpf_net_context __bpf_net_ctx, *bpf_net_ctx; 277 int err = 0, act, ret, i, nframes = 0, batch_sz; 278 struct xdp_frame **frames = xdp->frames; 279 struct bpf_redirect_info *ri; 280 struct xdp_page_head *head; 281 struct xdp_frame *frm; 282 bool redirect = false; 283 struct xdp_buff *ctx; 284 struct page *page; 285 286 batch_sz = min_t(u32, repeat, xdp->batch_size); 287 288 local_bh_disable(); 289 bpf_net_ctx = bpf_net_ctx_set(&__bpf_net_ctx); 290 ri = bpf_net_ctx_get_ri(); 291 xdp_set_return_frame_no_direct(); 292 293 for (i = 0; i < batch_sz; i++) { 294 page = page_pool_dev_alloc_pages(xdp->pp); 295 if (!page) { 296 err = -ENOMEM; 297 goto out; 298 } 299 300 head = phys_to_virt(page_to_phys(page)); 301 reset_ctx(head); 302 ctx = &head->ctx; 303 frm = head->frame; 304 xdp->frame_cnt++; 305 306 act = bpf_prog_run_xdp(prog, ctx); 307 308 /* if program changed pkt bounds we need to update the xdp_frame */ 309 if (unlikely(ctx_was_changed(head))) { 310 ret = xdp_update_frame_from_buff(ctx, frm); 311 if (ret) { 312 xdp_return_buff(ctx); 313 continue; 314 } 315 } 316 317 switch (act) { 318 case XDP_TX: 319 /* we can't do a real XDP_TX since we're not in the 320 * driver, so turn it into a REDIRECT back to the same 321 * index 322 */ 323 ri->tgt_index = xdp->dev->ifindex; 324 ri->map_id = INT_MAX; 325 ri->map_type = BPF_MAP_TYPE_UNSPEC; 326 fallthrough; 327 case XDP_REDIRECT: 328 redirect = true; 329 ret = xdp_do_redirect_frame(xdp->dev, ctx, frm, prog); 330 if (ret) 331 xdp_return_buff(ctx); 332 break; 333 case XDP_PASS: 334 frames[nframes++] = frm; 335 break; 336 default: 337 bpf_warn_invalid_xdp_action(NULL, prog, act); 338 fallthrough; 339 case XDP_DROP: 340 xdp_return_buff(ctx); 341 break; 342 } 343 } 344 345 out: 346 if (redirect) 347 xdp_do_flush(); 348 if (nframes) { 349 ret = xdp_recv_frames(frames, nframes, xdp->skbs, xdp->dev); 350 if (ret) 351 err = ret; 352 } 353 354 xdp_clear_return_frame_no_direct(); 355 bpf_net_ctx_clear(bpf_net_ctx); 356 local_bh_enable(); 357 return err; 358 } 359 360 static int bpf_test_run_xdp_live(struct bpf_prog *prog, struct xdp_buff *ctx, 361 u32 repeat, u32 batch_size, u32 *time) 362 363 { 364 struct xdp_test_data xdp = { .batch_size = batch_size }; 365 struct bpf_test_timer t = {}; 366 int ret; 367 368 if (!repeat) 369 repeat = 1; 370 371 ret = xdp_test_run_setup(&xdp, ctx); 372 if (ret) 373 return ret; 374 375 bpf_test_timer_enter(&t); 376 do { 377 xdp.frame_cnt = 0; 378 ret = xdp_test_run_batch(&xdp, prog, repeat - t.i); 379 if (unlikely(ret < 0)) 380 break; 381 } while (bpf_test_timer_continue(&t, xdp.frame_cnt, repeat, &ret, time)); 382 bpf_test_timer_leave(&t); 383 384 xdp_test_run_teardown(&xdp); 385 return ret; 386 } 387 388 static int bpf_test_run(struct bpf_prog *prog, void *ctx, u32 repeat, 389 u32 *retval, u32 *time, bool xdp) 390 { 391 struct bpf_net_context __bpf_net_ctx, *bpf_net_ctx; 392 struct bpf_prog_array_item item = {.prog = prog}; 393 struct bpf_run_ctx *old_ctx; 394 struct bpf_cg_run_ctx run_ctx; 395 struct bpf_test_timer t = {}; 396 enum bpf_cgroup_storage_type stype; 397 int ret; 398 399 for_each_cgroup_storage_type(stype) { 400 item.cgroup_storage[stype] = bpf_cgroup_storage_alloc(prog, stype); 401 if (IS_ERR(item.cgroup_storage[stype])) { 402 item.cgroup_storage[stype] = NULL; 403 for_each_cgroup_storage_type(stype) 404 bpf_cgroup_storage_free(item.cgroup_storage[stype]); 405 return -ENOMEM; 406 } 407 } 408 409 if (!repeat) 410 repeat = 1; 411 412 bpf_test_timer_enter(&t); 413 old_ctx = bpf_set_run_ctx(&run_ctx.run_ctx); 414 do { 415 run_ctx.prog_item = &item; 416 local_bh_disable(); 417 bpf_net_ctx = bpf_net_ctx_set(&__bpf_net_ctx); 418 419 if (xdp) 420 *retval = bpf_prog_run_xdp(prog, ctx); 421 else 422 *retval = bpf_prog_run(prog, ctx); 423 424 bpf_net_ctx_clear(bpf_net_ctx); 425 local_bh_enable(); 426 } while (bpf_test_timer_continue(&t, 1, repeat, &ret, time)); 427 bpf_reset_run_ctx(old_ctx); 428 bpf_test_timer_leave(&t); 429 430 for_each_cgroup_storage_type(stype) 431 bpf_cgroup_storage_free(item.cgroup_storage[stype]); 432 433 return ret; 434 } 435 436 static int bpf_test_finish(const union bpf_attr *kattr, 437 union bpf_attr __user *uattr, const void *data, 438 struct skb_shared_info *sinfo, u32 size, u32 frag_size, 439 u32 retval, u32 duration) 440 { 441 void __user *data_out = u64_to_user_ptr(kattr->test.data_out); 442 int err = -EFAULT; 443 u32 copy_size = size; 444 445 /* Clamp copy if the user has provided a size hint, but copy the full 446 * buffer if not to retain old behaviour. 447 */ 448 if (kattr->test.data_size_out && 449 copy_size > kattr->test.data_size_out) { 450 copy_size = kattr->test.data_size_out; 451 err = -ENOSPC; 452 } 453 454 if (data_out) { 455 int len = sinfo ? copy_size - frag_size : copy_size; 456 457 if (len < 0) { 458 err = -ENOSPC; 459 goto out; 460 } 461 462 if (copy_to_user(data_out, data, len)) 463 goto out; 464 465 if (sinfo) { 466 int i, offset = len; 467 u32 data_len; 468 469 for (i = 0; i < sinfo->nr_frags; i++) { 470 skb_frag_t *frag = &sinfo->frags[i]; 471 472 if (offset >= copy_size) { 473 err = -ENOSPC; 474 break; 475 } 476 477 data_len = min_t(u32, copy_size - offset, 478 skb_frag_size(frag)); 479 480 if (copy_to_user(data_out + offset, 481 skb_frag_address(frag), 482 data_len)) 483 goto out; 484 485 offset += data_len; 486 } 487 } 488 } 489 490 if (copy_to_user(&uattr->test.data_size_out, &size, sizeof(size))) 491 goto out; 492 if (copy_to_user(&uattr->test.retval, &retval, sizeof(retval))) 493 goto out; 494 if (copy_to_user(&uattr->test.duration, &duration, sizeof(duration))) 495 goto out; 496 if (err != -ENOSPC) 497 err = 0; 498 out: 499 trace_bpf_test_finish(&err); 500 return err; 501 } 502 503 /* Integer types of various sizes and pointer combinations cover variety of 504 * architecture dependent calling conventions. 7+ can be supported in the 505 * future. 506 */ 507 __bpf_kfunc_start_defs(); 508 509 __bpf_kfunc int bpf_fentry_test1(int a) 510 { 511 return a + 1; 512 } 513 EXPORT_SYMBOL_GPL(bpf_fentry_test1); 514 515 noinline int bpf_fentry_test2(int a, u64 b) 516 { 517 return a + b; 518 } 519 520 noinline int bpf_fentry_test3(char a, int b, u64 c) 521 { 522 return a + b + c; 523 } 524 525 noinline int bpf_fentry_test4(void *a, char b, int c, u64 d) 526 { 527 return (long)a + b + c + d; 528 } 529 530 noinline int bpf_fentry_test5(u64 a, void *b, short c, int d, u64 e) 531 { 532 return a + (long)b + c + d + e; 533 } 534 535 noinline int bpf_fentry_test6(u64 a, void *b, short c, int d, void *e, u64 f) 536 { 537 return a + (long)b + c + d + (long)e + f; 538 } 539 540 struct bpf_fentry_test_t { 541 struct bpf_fentry_test_t *a; 542 }; 543 544 noinline int bpf_fentry_test7(struct bpf_fentry_test_t *arg) 545 { 546 asm volatile ("" : "+r"(arg)); 547 return (long)arg; 548 } 549 550 noinline int bpf_fentry_test8(struct bpf_fentry_test_t *arg) 551 { 552 return (long)arg->a; 553 } 554 555 __bpf_kfunc u32 bpf_fentry_test9(u32 *a) 556 { 557 return *a; 558 } 559 560 noinline int bpf_fentry_test10(const void *a) 561 { 562 return (long)a; 563 } 564 565 noinline void bpf_fentry_test_sinfo(struct skb_shared_info *sinfo) 566 { 567 } 568 569 noinline void bpf_fentry_test_ppvoid(void **pp) 570 { 571 } 572 573 noinline void bpf_fentry_test_pppvoid(void ***ppp) 574 { 575 } 576 577 noinline void bpf_fentry_test_ppfile(struct file **ppf) 578 { 579 } 580 581 noinline struct file **bpf_fexit_test_ret_ppfile(void) 582 { 583 return (struct file **)NULL; 584 } 585 586 __bpf_kfunc int bpf_modify_return_test(int a, int *b) 587 { 588 *b += 1; 589 return a + *b; 590 } 591 592 __bpf_kfunc int bpf_modify_return_test2(int a, int *b, short c, int d, 593 void *e, char f, int g) 594 { 595 *b += 1; 596 return a + *b + c + d + (long)e + f + g; 597 } 598 599 __bpf_kfunc int bpf_modify_return_test_tp(int nonce) 600 { 601 trace_bpf_trigger_tp(nonce); 602 603 return nonce; 604 } 605 606 noinline int bpf_fentry_shadow_test(int a) 607 { 608 return a + 1; 609 } 610 611 struct prog_test_member1 { 612 int a; 613 }; 614 615 struct prog_test_member { 616 struct prog_test_member1 m; 617 int c; 618 }; 619 620 struct prog_test_ref_kfunc { 621 int a; 622 int b; 623 struct prog_test_member memb; 624 struct prog_test_ref_kfunc *next; 625 refcount_t cnt; 626 }; 627 628 __bpf_kfunc void bpf_kfunc_call_test_release(struct prog_test_ref_kfunc *p) 629 { 630 refcount_dec(&p->cnt); 631 } 632 633 __bpf_kfunc void bpf_kfunc_call_test_release_dtor(void *p) 634 { 635 bpf_kfunc_call_test_release(p); 636 } 637 CFI_NOSEAL(bpf_kfunc_call_test_release_dtor); 638 639 __bpf_kfunc void bpf_kfunc_call_memb_release(struct prog_test_member *p) 640 { 641 } 642 643 __bpf_kfunc void bpf_kfunc_call_memb_release_dtor(void *p) 644 { 645 } 646 CFI_NOSEAL(bpf_kfunc_call_memb_release_dtor); 647 648 __bpf_kfunc_end_defs(); 649 650 BTF_KFUNCS_START(bpf_test_modify_return_ids) 651 BTF_ID_FLAGS(func, bpf_modify_return_test) 652 BTF_ID_FLAGS(func, bpf_modify_return_test2) 653 BTF_ID_FLAGS(func, bpf_modify_return_test_tp) 654 BTF_ID_FLAGS(func, bpf_fentry_test1, KF_SLEEPABLE) 655 BTF_KFUNCS_END(bpf_test_modify_return_ids) 656 657 static const struct btf_kfunc_id_set bpf_test_modify_return_set = { 658 .owner = THIS_MODULE, 659 .set = &bpf_test_modify_return_ids, 660 }; 661 662 BTF_KFUNCS_START(test_sk_check_kfunc_ids) 663 BTF_ID_FLAGS(func, bpf_kfunc_call_test_release, KF_RELEASE) 664 BTF_ID_FLAGS(func, bpf_kfunc_call_memb_release, KF_RELEASE) 665 BTF_KFUNCS_END(test_sk_check_kfunc_ids) 666 667 static void *bpf_test_init(const union bpf_attr *kattr, u32 user_size, 668 u32 size, u32 headroom, u32 tailroom) 669 { 670 void __user *data_in = u64_to_user_ptr(kattr->test.data_in); 671 void *data; 672 673 if (user_size > PAGE_SIZE - headroom - tailroom) 674 return ERR_PTR(-EINVAL); 675 676 size = SKB_DATA_ALIGN(size); 677 data = kzalloc(size + headroom + tailroom, GFP_USER); 678 if (!data) 679 return ERR_PTR(-ENOMEM); 680 681 if (copy_from_user(data + headroom, data_in, user_size)) { 682 kfree(data); 683 return ERR_PTR(-EFAULT); 684 } 685 686 return data; 687 } 688 689 int bpf_prog_test_run_tracing(struct bpf_prog *prog, 690 const union bpf_attr *kattr, 691 union bpf_attr __user *uattr) 692 { 693 struct bpf_fentry_test_t arg = {}; 694 u16 side_effect = 0, ret = 0; 695 int b = 2, err = -EFAULT; 696 u32 retval = 0; 697 698 if (kattr->test.flags || kattr->test.cpu || kattr->test.batch_size) 699 return -EINVAL; 700 701 switch (prog->expected_attach_type) { 702 case BPF_TRACE_FENTRY: 703 case BPF_TRACE_FEXIT: 704 case BPF_TRACE_FSESSION: 705 if (bpf_fentry_test1(1) != 2 || 706 bpf_fentry_test2(2, 3) != 5 || 707 bpf_fentry_test3(4, 5, 6) != 15 || 708 bpf_fentry_test4((void *)7, 8, 9, 10) != 34 || 709 bpf_fentry_test5(11, (void *)12, 13, 14, 15) != 65 || 710 bpf_fentry_test6(16, (void *)17, 18, 19, (void *)20, 21) != 111 || 711 bpf_fentry_test7((struct bpf_fentry_test_t *)0) != 0 || 712 bpf_fentry_test8(&arg) != 0 || 713 bpf_fentry_test9(&retval) != 0 || 714 bpf_fentry_test10((void *)0) != 0) 715 goto out; 716 break; 717 case BPF_MODIFY_RETURN: 718 ret = bpf_modify_return_test(1, &b); 719 if (b != 2) 720 side_effect++; 721 b = 2; 722 ret += bpf_modify_return_test2(1, &b, 3, 4, (void *)5, 6, 7); 723 if (b != 2) 724 side_effect++; 725 break; 726 default: 727 goto out; 728 } 729 730 retval = ((u32)side_effect << 16) | ret; 731 if (copy_to_user(&uattr->test.retval, &retval, sizeof(retval))) 732 goto out; 733 734 err = 0; 735 out: 736 trace_bpf_test_finish(&err); 737 return err; 738 } 739 740 struct bpf_raw_tp_test_run_info { 741 struct bpf_prog *prog; 742 void *ctx; 743 u32 retval; 744 }; 745 746 static void 747 __bpf_prog_test_run_raw_tp(void *data) 748 { 749 struct bpf_raw_tp_test_run_info *info = data; 750 struct bpf_trace_run_ctx run_ctx = {}; 751 struct bpf_run_ctx *old_run_ctx; 752 753 old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx); 754 755 rcu_read_lock(); 756 info->retval = bpf_prog_run(info->prog, info->ctx); 757 rcu_read_unlock(); 758 759 bpf_reset_run_ctx(old_run_ctx); 760 } 761 762 int bpf_prog_test_run_raw_tp(struct bpf_prog *prog, 763 const union bpf_attr *kattr, 764 union bpf_attr __user *uattr) 765 { 766 void __user *ctx_in = u64_to_user_ptr(kattr->test.ctx_in); 767 __u32 ctx_size_in = kattr->test.ctx_size_in; 768 struct bpf_raw_tp_test_run_info info; 769 int cpu = kattr->test.cpu, err = 0; 770 int current_cpu; 771 772 /* doesn't support data_in/out, ctx_out, duration, or repeat */ 773 if (kattr->test.data_in || kattr->test.data_out || 774 kattr->test.ctx_out || kattr->test.duration || 775 kattr->test.repeat || kattr->test.batch_size) 776 return -EINVAL; 777 778 if (ctx_size_in < prog->aux->max_ctx_offset || 779 ctx_size_in > MAX_BPF_FUNC_ARGS * sizeof(u64)) 780 return -EINVAL; 781 782 if ((kattr->test.flags & BPF_F_TEST_RUN_ON_CPU) == 0 && cpu != 0) 783 return -EINVAL; 784 785 if (ctx_size_in) { 786 info.ctx = memdup_user(ctx_in, ctx_size_in); 787 if (IS_ERR(info.ctx)) 788 return PTR_ERR(info.ctx); 789 } else { 790 info.ctx = NULL; 791 } 792 793 info.prog = prog; 794 795 current_cpu = get_cpu(); 796 if ((kattr->test.flags & BPF_F_TEST_RUN_ON_CPU) == 0 || 797 cpu == current_cpu) { 798 __bpf_prog_test_run_raw_tp(&info); 799 } else if (cpu >= nr_cpu_ids || !cpu_online(cpu)) { 800 /* smp_call_function_single() also checks cpu_online() 801 * after csd_lock(). However, since cpu is from user 802 * space, let's do an extra quick check to filter out 803 * invalid value before smp_call_function_single(). 804 */ 805 err = -ENXIO; 806 } else { 807 err = smp_call_function_single(cpu, __bpf_prog_test_run_raw_tp, 808 &info, 1); 809 } 810 put_cpu(); 811 812 if (!err && 813 copy_to_user(&uattr->test.retval, &info.retval, sizeof(u32))) 814 err = -EFAULT; 815 816 kfree(info.ctx); 817 return err; 818 } 819 820 static void *bpf_ctx_init(const union bpf_attr *kattr, u32 max_size) 821 { 822 void __user *data_in = u64_to_user_ptr(kattr->test.ctx_in); 823 void __user *data_out = u64_to_user_ptr(kattr->test.ctx_out); 824 u32 size = kattr->test.ctx_size_in; 825 void *data; 826 int err; 827 828 if (!data_in && !data_out) 829 return NULL; 830 831 data = kzalloc(max_size, GFP_USER); 832 if (!data) 833 return ERR_PTR(-ENOMEM); 834 835 if (data_in) { 836 err = bpf_check_uarg_tail_zero(USER_BPFPTR(data_in), max_size, size); 837 if (err) { 838 kfree(data); 839 return ERR_PTR(err); 840 } 841 842 size = min_t(u32, max_size, size); 843 if (copy_from_user(data, data_in, size)) { 844 kfree(data); 845 return ERR_PTR(-EFAULT); 846 } 847 } 848 return data; 849 } 850 851 static int bpf_ctx_finish(const union bpf_attr *kattr, 852 union bpf_attr __user *uattr, const void *data, 853 u32 size) 854 { 855 void __user *data_out = u64_to_user_ptr(kattr->test.ctx_out); 856 int err = -EFAULT; 857 u32 copy_size = size; 858 859 if (!data || !data_out) 860 return 0; 861 862 if (copy_size > kattr->test.ctx_size_out) { 863 copy_size = kattr->test.ctx_size_out; 864 err = -ENOSPC; 865 } 866 867 if (copy_to_user(data_out, data, copy_size)) 868 goto out; 869 if (copy_to_user(&uattr->test.ctx_size_out, &size, sizeof(size))) 870 goto out; 871 if (err != -ENOSPC) 872 err = 0; 873 out: 874 return err; 875 } 876 877 /** 878 * range_is_zero - test whether buffer is initialized 879 * @buf: buffer to check 880 * @from: check from this position 881 * @to: check up until (excluding) this position 882 * 883 * This function returns true if the there is a non-zero byte 884 * in the buf in the range [from,to). 885 */ 886 static inline bool range_is_zero(void *buf, size_t from, size_t to) 887 { 888 return !memchr_inv((u8 *)buf + from, 0, to - from); 889 } 890 891 static int convert___skb_to_skb(struct sk_buff *skb, struct __sk_buff *__skb) 892 { 893 struct qdisc_skb_cb *cb = (struct qdisc_skb_cb *)skb->cb; 894 895 if (!__skb) 896 return 0; 897 898 /* make sure the fields we don't use are zeroed */ 899 if (!range_is_zero(__skb, 0, offsetof(struct __sk_buff, mark))) 900 return -EINVAL; 901 902 /* mark is allowed */ 903 904 if (!range_is_zero(__skb, offsetofend(struct __sk_buff, mark), 905 offsetof(struct __sk_buff, priority))) 906 return -EINVAL; 907 908 /* priority is allowed */ 909 /* ingress_ifindex is allowed */ 910 /* ifindex is allowed */ 911 912 if (!range_is_zero(__skb, offsetofend(struct __sk_buff, ifindex), 913 offsetof(struct __sk_buff, cb))) 914 return -EINVAL; 915 916 /* cb is allowed */ 917 918 if (!range_is_zero(__skb, offsetofend(struct __sk_buff, cb), 919 offsetof(struct __sk_buff, data_end))) 920 return -EINVAL; 921 922 /* data_end is allowed, but not copied to skb */ 923 924 if (!range_is_zero(__skb, offsetofend(struct __sk_buff, data_end), 925 offsetof(struct __sk_buff, tstamp))) 926 return -EINVAL; 927 928 /* tstamp is allowed */ 929 /* wire_len is allowed */ 930 /* gso_segs is allowed */ 931 932 if (!range_is_zero(__skb, offsetofend(struct __sk_buff, gso_segs), 933 offsetof(struct __sk_buff, gso_size))) 934 return -EINVAL; 935 936 /* gso_size is allowed */ 937 938 if (!range_is_zero(__skb, offsetofend(struct __sk_buff, gso_size), 939 offsetof(struct __sk_buff, hwtstamp))) 940 return -EINVAL; 941 942 /* hwtstamp is allowed */ 943 944 if (!range_is_zero(__skb, offsetofend(struct __sk_buff, hwtstamp), 945 sizeof(struct __sk_buff))) 946 return -EINVAL; 947 948 skb->mark = __skb->mark; 949 skb->priority = __skb->priority; 950 skb->skb_iif = __skb->ingress_ifindex; 951 skb->tstamp = __skb->tstamp; 952 memcpy(&cb->data, __skb->cb, QDISC_CB_PRIV_LEN); 953 954 if (__skb->wire_len == 0) { 955 cb->pkt_len = skb->len; 956 } else { 957 if (__skb->wire_len < skb->len || 958 __skb->wire_len > GSO_LEGACY_MAX_SIZE) 959 return -EINVAL; 960 cb->pkt_len = __skb->wire_len; 961 } 962 963 if (__skb->gso_segs > GSO_MAX_SEGS) 964 return -EINVAL; 965 966 /* Currently GSO type is zero/unset. If this gets extended with 967 * a small list of accepted GSO types in future, the filter for 968 * an unset GSO type in bpf_clone_redirect() can be lifted. 969 */ 970 skb_shinfo(skb)->gso_segs = __skb->gso_segs; 971 skb_shinfo(skb)->gso_size = __skb->gso_size; 972 skb_shinfo(skb)->hwtstamps.hwtstamp = __skb->hwtstamp; 973 974 return 0; 975 } 976 977 static void convert_skb_to___skb(struct sk_buff *skb, struct __sk_buff *__skb) 978 { 979 struct qdisc_skb_cb *cb = (struct qdisc_skb_cb *)skb->cb; 980 981 if (!__skb) 982 return; 983 984 __skb->mark = skb->mark; 985 __skb->priority = skb->priority; 986 __skb->ingress_ifindex = skb->skb_iif; 987 __skb->ifindex = skb->dev->ifindex; 988 __skb->tstamp = skb->tstamp; 989 memcpy(__skb->cb, &cb->data, QDISC_CB_PRIV_LEN); 990 __skb->wire_len = cb->pkt_len; 991 __skb->gso_segs = skb_shinfo(skb)->gso_segs; 992 __skb->hwtstamp = skb_shinfo(skb)->hwtstamps.hwtstamp; 993 } 994 995 static struct proto bpf_dummy_proto = { 996 .name = "bpf_dummy", 997 .owner = THIS_MODULE, 998 .obj_size = sizeof(struct sock), 999 }; 1000 1001 int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr, 1002 union bpf_attr __user *uattr) 1003 { 1004 bool is_l2 = false, is_direct_pkt_access = false, is_lwt = false; 1005 u32 tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 1006 struct net *net = current->nsproxy->net_ns; 1007 struct net_device *dev = net->loopback_dev; 1008 u32 headroom = NET_SKB_PAD + NET_IP_ALIGN; 1009 u32 linear_sz = kattr->test.data_size_in; 1010 u32 repeat = kattr->test.repeat; 1011 struct __sk_buff *ctx = NULL; 1012 struct sk_buff *skb = NULL; 1013 struct sock *sk = NULL; 1014 u32 retval, duration; 1015 int hh_len = ETH_HLEN; 1016 void *data = NULL; 1017 int ret; 1018 1019 if ((kattr->test.flags & ~BPF_F_TEST_SKB_CHECKSUM_COMPLETE) || 1020 kattr->test.cpu || kattr->test.batch_size) 1021 return -EINVAL; 1022 1023 if (kattr->test.data_size_in < ETH_HLEN) 1024 return -EINVAL; 1025 1026 switch (prog->type) { 1027 case BPF_PROG_TYPE_SCHED_CLS: 1028 case BPF_PROG_TYPE_SCHED_ACT: 1029 is_direct_pkt_access = true; 1030 is_l2 = true; 1031 break; 1032 case BPF_PROG_TYPE_LWT_IN: 1033 case BPF_PROG_TYPE_LWT_OUT: 1034 case BPF_PROG_TYPE_LWT_XMIT: 1035 is_lwt = true; 1036 fallthrough; 1037 case BPF_PROG_TYPE_CGROUP_SKB: 1038 is_direct_pkt_access = true; 1039 break; 1040 default: 1041 break; 1042 } 1043 1044 ctx = bpf_ctx_init(kattr, sizeof(struct __sk_buff)); 1045 if (IS_ERR(ctx)) 1046 return PTR_ERR(ctx); 1047 1048 if (ctx) { 1049 if (ctx->data_end > kattr->test.data_size_in || ctx->data || ctx->data_meta) { 1050 ret = -EINVAL; 1051 goto out; 1052 } 1053 if (ctx->data_end) { 1054 /* Non-linear LWT test_run is unsupported for now. */ 1055 if (is_lwt) { 1056 ret = -EINVAL; 1057 goto out; 1058 } 1059 linear_sz = max(ETH_HLEN, ctx->data_end); 1060 } 1061 } 1062 1063 linear_sz = min_t(u32, linear_sz, PAGE_SIZE - headroom - tailroom); 1064 1065 data = bpf_test_init(kattr, linear_sz, linear_sz, headroom, tailroom); 1066 if (IS_ERR(data)) { 1067 ret = PTR_ERR(data); 1068 data = NULL; 1069 goto out; 1070 } 1071 1072 sk = sk_alloc(net, AF_UNSPEC, GFP_USER, &bpf_dummy_proto, 1); 1073 if (!sk) { 1074 ret = -ENOMEM; 1075 goto out; 1076 } 1077 sock_init_data(NULL, sk); 1078 1079 skb = slab_build_skb(data); 1080 if (!skb) { 1081 ret = -ENOMEM; 1082 goto out; 1083 } 1084 skb->sk = sk; 1085 1086 data = NULL; /* data released via kfree_skb */ 1087 1088 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN); 1089 __skb_put(skb, linear_sz); 1090 1091 if (unlikely(kattr->test.data_size_in > linear_sz)) { 1092 void __user *data_in = u64_to_user_ptr(kattr->test.data_in); 1093 struct skb_shared_info *sinfo = skb_shinfo(skb); 1094 u32 copied = linear_sz; 1095 1096 while (copied < kattr->test.data_size_in) { 1097 struct page *page; 1098 u32 data_len; 1099 1100 if (sinfo->nr_frags == MAX_SKB_FRAGS) { 1101 ret = -ENOMEM; 1102 goto out; 1103 } 1104 1105 page = alloc_page(GFP_KERNEL); 1106 if (!page) { 1107 ret = -ENOMEM; 1108 goto out; 1109 } 1110 1111 data_len = min_t(u32, kattr->test.data_size_in - copied, 1112 PAGE_SIZE); 1113 skb_fill_page_desc(skb, sinfo->nr_frags, page, 0, data_len); 1114 1115 if (copy_from_user(page_address(page), data_in + copied, 1116 data_len)) { 1117 ret = -EFAULT; 1118 goto out; 1119 } 1120 skb->data_len += data_len; 1121 skb->truesize += PAGE_SIZE; 1122 skb->len += data_len; 1123 copied += data_len; 1124 } 1125 } 1126 1127 if (ctx && ctx->ifindex > 1) { 1128 dev = dev_get_by_index(net, ctx->ifindex); 1129 if (!dev) { 1130 ret = -ENODEV; 1131 goto out; 1132 } 1133 } 1134 skb->protocol = eth_type_trans(skb, dev); 1135 skb_reset_network_header(skb); 1136 1137 switch (skb->protocol) { 1138 case htons(ETH_P_IP): 1139 if (skb_headlen(skb) < sizeof(struct iphdr)) { 1140 ret = -EINVAL; 1141 goto out; 1142 } 1143 sk->sk_family = AF_INET; 1144 sk->sk_rcv_saddr = ip_hdr(skb)->saddr; 1145 sk->sk_daddr = ip_hdr(skb)->daddr; 1146 break; 1147 #if IS_ENABLED(CONFIG_IPV6) 1148 case htons(ETH_P_IPV6): 1149 if (skb_headlen(skb) < sizeof(struct ipv6hdr)) { 1150 ret = -EINVAL; 1151 goto out; 1152 } 1153 sk->sk_family = AF_INET6; 1154 sk->sk_v6_rcv_saddr = ipv6_hdr(skb)->saddr; 1155 sk->sk_v6_daddr = ipv6_hdr(skb)->daddr; 1156 break; 1157 #endif 1158 default: 1159 break; 1160 } 1161 1162 if (is_l2) 1163 __skb_push(skb, hh_len); 1164 if (is_direct_pkt_access) 1165 bpf_compute_data_pointers(skb); 1166 1167 ret = convert___skb_to_skb(skb, ctx); 1168 if (ret) 1169 goto out; 1170 1171 if (kattr->test.flags & BPF_F_TEST_SKB_CHECKSUM_COMPLETE) { 1172 const int off = skb_network_offset(skb); 1173 int len = skb->len - off; 1174 1175 skb->csum = skb_checksum(skb, off, len, 0); 1176 skb->ip_summed = CHECKSUM_COMPLETE; 1177 } 1178 1179 if (prog->type == BPF_PROG_TYPE_LWT_XMIT) { 1180 if (!ipv6_mod_enabled()) { 1181 pr_warn_once("Please test this program with IPv6 enabled kernel\n"); 1182 ret = -EOPNOTSUPP; 1183 goto out; 1184 } 1185 #if IS_ENABLED(CONFIG_IPV6) 1186 dst_hold(&net->ipv6.ip6_null_entry->dst); 1187 skb_dst_set(skb, &net->ipv6.ip6_null_entry->dst); 1188 #endif 1189 } 1190 1191 ret = bpf_test_run(prog, skb, repeat, &retval, &duration, false); 1192 if (ret) 1193 goto out; 1194 if (!is_l2) { 1195 if (skb_headroom(skb) < hh_len) { 1196 int nhead = HH_DATA_ALIGN(hh_len - skb_headroom(skb)); 1197 1198 if (pskb_expand_head(skb, nhead, 0, GFP_USER)) { 1199 ret = -ENOMEM; 1200 goto out; 1201 } 1202 } 1203 memset(__skb_push(skb, hh_len), 0, hh_len); 1204 } 1205 1206 if (kattr->test.flags & BPF_F_TEST_SKB_CHECKSUM_COMPLETE) { 1207 const int off = skb_network_offset(skb); 1208 int len = skb->len - off; 1209 __wsum csum; 1210 1211 csum = skb_checksum(skb, off, len, 0); 1212 1213 if (csum_fold(skb->csum) != csum_fold(csum)) { 1214 ret = -EBADMSG; 1215 goto out; 1216 } 1217 } 1218 1219 convert_skb_to___skb(skb, ctx); 1220 1221 if (skb_is_nonlinear(skb)) 1222 /* bpf program can never convert linear skb to non-linear */ 1223 WARN_ON_ONCE(linear_sz == kattr->test.data_size_in); 1224 ret = bpf_test_finish(kattr, uattr, skb->data, skb_shinfo(skb), skb->len, 1225 skb->data_len, retval, duration); 1226 if (!ret) 1227 ret = bpf_ctx_finish(kattr, uattr, ctx, 1228 sizeof(struct __sk_buff)); 1229 out: 1230 if (dev && dev != net->loopback_dev) 1231 dev_put(dev); 1232 kfree_skb(skb); 1233 kfree(data); 1234 if (sk) 1235 sk_free(sk); 1236 kfree(ctx); 1237 return ret; 1238 } 1239 1240 static int xdp_convert_md_to_buff(struct xdp_md *xdp_md, struct xdp_buff *xdp) 1241 { 1242 unsigned int ingress_ifindex, rx_queue_index; 1243 struct netdev_rx_queue *rxqueue; 1244 struct net_device *device; 1245 1246 if (!xdp_md) 1247 return 0; 1248 1249 if (xdp_md->egress_ifindex != 0) 1250 return -EINVAL; 1251 1252 ingress_ifindex = xdp_md->ingress_ifindex; 1253 rx_queue_index = xdp_md->rx_queue_index; 1254 1255 if (!ingress_ifindex && rx_queue_index) 1256 return -EINVAL; 1257 1258 if (ingress_ifindex) { 1259 device = dev_get_by_index(current->nsproxy->net_ns, 1260 ingress_ifindex); 1261 if (!device) 1262 return -ENODEV; 1263 1264 if (rx_queue_index >= device->real_num_rx_queues) 1265 goto free_dev; 1266 1267 rxqueue = __netif_get_rx_queue(device, rx_queue_index); 1268 1269 if (!xdp_rxq_info_is_reg(&rxqueue->xdp_rxq)) 1270 goto free_dev; 1271 1272 xdp->rxq = &rxqueue->xdp_rxq; 1273 /* The device is now tracked in the xdp->rxq for later 1274 * dev_put() 1275 */ 1276 } 1277 1278 xdp->data = xdp->data_meta + xdp_md->data; 1279 return 0; 1280 1281 free_dev: 1282 dev_put(device); 1283 return -EINVAL; 1284 } 1285 1286 static void xdp_convert_buff_to_md(struct xdp_buff *xdp, struct xdp_md *xdp_md) 1287 { 1288 if (!xdp_md) 1289 return; 1290 1291 xdp_md->data = xdp->data - xdp->data_meta; 1292 xdp_md->data_end = xdp->data_end - xdp->data_meta; 1293 1294 if (xdp_md->ingress_ifindex) 1295 dev_put(xdp->rxq->dev); 1296 } 1297 1298 int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr, 1299 union bpf_attr __user *uattr) 1300 { 1301 bool do_live = (kattr->test.flags & BPF_F_TEST_XDP_LIVE_FRAMES); 1302 u32 tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 1303 u32 retval = 0, meta_sz = 0, duration, max_linear_sz, size; 1304 u32 linear_sz = kattr->test.data_size_in; 1305 u32 batch_size = kattr->test.batch_size; 1306 u32 headroom = XDP_PACKET_HEADROOM; 1307 u32 repeat = kattr->test.repeat; 1308 struct netdev_rx_queue *rxqueue; 1309 struct skb_shared_info *sinfo; 1310 struct xdp_buff xdp = {}; 1311 int i, ret = -EINVAL; 1312 struct xdp_md *ctx; 1313 void *data; 1314 1315 if (prog->expected_attach_type == BPF_XDP_DEVMAP || 1316 prog->expected_attach_type == BPF_XDP_CPUMAP) 1317 return -EINVAL; 1318 1319 if (kattr->test.flags & ~BPF_F_TEST_XDP_LIVE_FRAMES) 1320 return -EINVAL; 1321 1322 if (bpf_prog_is_dev_bound(prog->aux)) 1323 return -EINVAL; 1324 1325 if (do_live) { 1326 if (!batch_size) 1327 batch_size = NAPI_POLL_WEIGHT; 1328 else if (batch_size > TEST_XDP_MAX_BATCH) 1329 return -E2BIG; 1330 } else if (batch_size) { 1331 return -EINVAL; 1332 } 1333 1334 ctx = bpf_ctx_init(kattr, sizeof(struct xdp_md)); 1335 if (IS_ERR(ctx)) 1336 return PTR_ERR(ctx); 1337 1338 if (ctx) { 1339 /* There can't be user provided data before the meta data */ 1340 if (ctx->data_meta || ctx->data_end > kattr->test.data_size_in || 1341 ctx->data > ctx->data_end || 1342 (do_live && (kattr->test.data_out || kattr->test.ctx_out))) 1343 goto free_ctx; 1344 1345 meta_sz = ctx->data; 1346 if (xdp_metalen_invalid(meta_sz) || meta_sz > headroom - sizeof(struct xdp_frame)) 1347 goto free_ctx; 1348 1349 /* Meta data is allocated from the headroom */ 1350 headroom -= meta_sz; 1351 linear_sz = ctx->data_end; 1352 } 1353 1354 /* The xdp_page_head structure takes up space in each page, limiting the 1355 * size of the packet data; add the extra size to headroom here to make 1356 * sure it's accounted in the length checks below, but not in the 1357 * metadata size check above. 1358 */ 1359 if (do_live) 1360 headroom += sizeof(struct xdp_page_head); 1361 1362 max_linear_sz = PAGE_SIZE - headroom - tailroom; 1363 linear_sz = min_t(u32, linear_sz, max_linear_sz); 1364 1365 /* disallow live data mode for jumbo frames */ 1366 if (do_live && kattr->test.data_size_in > linear_sz) 1367 goto free_ctx; 1368 1369 if (kattr->test.data_size_in - meta_sz < ETH_HLEN) 1370 goto free_ctx; 1371 1372 data = bpf_test_init(kattr, linear_sz, max_linear_sz, headroom, tailroom); 1373 if (IS_ERR(data)) { 1374 ret = PTR_ERR(data); 1375 goto free_ctx; 1376 } 1377 1378 rxqueue = __netif_get_rx_queue(current->nsproxy->net_ns->loopback_dev, 0); 1379 rxqueue->xdp_rxq.frag_size = PAGE_SIZE; 1380 xdp_init_buff(&xdp, rxqueue->xdp_rxq.frag_size, &rxqueue->xdp_rxq); 1381 xdp_prepare_buff(&xdp, data, headroom, linear_sz, true); 1382 sinfo = xdp_get_shared_info_from_buff(&xdp); 1383 1384 ret = xdp_convert_md_to_buff(ctx, &xdp); 1385 if (ret) 1386 goto free_data; 1387 1388 size = linear_sz; 1389 if (unlikely(kattr->test.data_size_in > size)) { 1390 void __user *data_in = u64_to_user_ptr(kattr->test.data_in); 1391 1392 while (size < kattr->test.data_size_in) { 1393 struct page *page; 1394 skb_frag_t *frag; 1395 u32 data_len; 1396 1397 if (sinfo->nr_frags == MAX_SKB_FRAGS) { 1398 ret = -ENOMEM; 1399 goto out_put_dev; 1400 } 1401 1402 page = alloc_page(GFP_KERNEL); 1403 if (!page) { 1404 ret = -ENOMEM; 1405 goto out_put_dev; 1406 } 1407 1408 frag = &sinfo->frags[sinfo->nr_frags++]; 1409 1410 data_len = min_t(u32, kattr->test.data_size_in - size, 1411 PAGE_SIZE); 1412 skb_frag_fill_page_desc(frag, page, 0, data_len); 1413 1414 if (copy_from_user(page_address(page), data_in + size, 1415 data_len)) { 1416 ret = -EFAULT; 1417 goto out_put_dev; 1418 } 1419 sinfo->xdp_frags_size += data_len; 1420 size += data_len; 1421 } 1422 xdp_buff_set_frags_flag(&xdp); 1423 } 1424 1425 if (repeat > 1) 1426 bpf_prog_change_xdp(NULL, prog); 1427 1428 if (do_live) 1429 ret = bpf_test_run_xdp_live(prog, &xdp, repeat, batch_size, &duration); 1430 else 1431 ret = bpf_test_run(prog, &xdp, repeat, &retval, &duration, true); 1432 out_put_dev: 1433 /* We convert the xdp_buff back to an xdp_md before checking the return 1434 * code so the reference count of any held netdevice will be decremented 1435 * even if the test run failed. 1436 */ 1437 xdp_convert_buff_to_md(&xdp, ctx); 1438 if (ret) 1439 goto out; 1440 1441 size = xdp.data_end - xdp.data_meta + sinfo->xdp_frags_size; 1442 ret = bpf_test_finish(kattr, uattr, xdp.data_meta, sinfo, size, sinfo->xdp_frags_size, 1443 retval, duration); 1444 if (!ret) 1445 ret = bpf_ctx_finish(kattr, uattr, ctx, 1446 sizeof(struct xdp_md)); 1447 1448 out: 1449 if (repeat > 1) 1450 bpf_prog_change_xdp(prog, NULL); 1451 free_data: 1452 for (i = 0; i < sinfo->nr_frags; i++) 1453 __free_page(skb_frag_page(&sinfo->frags[i])); 1454 kfree(data); 1455 free_ctx: 1456 kfree(ctx); 1457 return ret; 1458 } 1459 1460 static int verify_user_bpf_flow_keys(struct bpf_flow_keys *ctx) 1461 { 1462 /* make sure the fields we don't use are zeroed */ 1463 if (!range_is_zero(ctx, 0, offsetof(struct bpf_flow_keys, flags))) 1464 return -EINVAL; 1465 1466 /* flags is allowed */ 1467 1468 if (!range_is_zero(ctx, offsetofend(struct bpf_flow_keys, flags), 1469 sizeof(struct bpf_flow_keys))) 1470 return -EINVAL; 1471 1472 return 0; 1473 } 1474 1475 int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog, 1476 const union bpf_attr *kattr, 1477 union bpf_attr __user *uattr) 1478 { 1479 struct bpf_test_timer t = {}; 1480 u32 size = kattr->test.data_size_in; 1481 struct bpf_flow_dissector ctx = {}; 1482 u32 repeat = kattr->test.repeat; 1483 struct bpf_flow_keys *user_ctx; 1484 struct bpf_flow_keys flow_keys; 1485 const struct ethhdr *eth; 1486 unsigned int flags = 0; 1487 u32 retval, duration; 1488 void *data; 1489 int ret; 1490 1491 if (kattr->test.flags || kattr->test.cpu || kattr->test.batch_size) 1492 return -EINVAL; 1493 1494 if (size < ETH_HLEN) 1495 return -EINVAL; 1496 1497 data = bpf_test_init(kattr, kattr->test.data_size_in, size, 0, 0); 1498 if (IS_ERR(data)) 1499 return PTR_ERR(data); 1500 1501 eth = (struct ethhdr *)data; 1502 1503 if (!repeat) 1504 repeat = 1; 1505 1506 user_ctx = bpf_ctx_init(kattr, sizeof(struct bpf_flow_keys)); 1507 if (IS_ERR(user_ctx)) { 1508 kfree(data); 1509 return PTR_ERR(user_ctx); 1510 } 1511 if (user_ctx) { 1512 ret = verify_user_bpf_flow_keys(user_ctx); 1513 if (ret) 1514 goto out; 1515 flags = user_ctx->flags; 1516 } 1517 1518 ctx.flow_keys = &flow_keys; 1519 ctx.data = data; 1520 ctx.data_end = (__u8 *)data + size; 1521 1522 bpf_test_timer_enter(&t); 1523 do { 1524 retval = bpf_flow_dissect(prog, &ctx, eth->h_proto, ETH_HLEN, 1525 size, flags); 1526 } while (bpf_test_timer_continue(&t, 1, repeat, &ret, &duration)); 1527 bpf_test_timer_leave(&t); 1528 1529 if (ret < 0) 1530 goto out; 1531 1532 ret = bpf_test_finish(kattr, uattr, &flow_keys, NULL, 1533 sizeof(flow_keys), 0, retval, duration); 1534 if (!ret) 1535 ret = bpf_ctx_finish(kattr, uattr, user_ctx, 1536 sizeof(struct bpf_flow_keys)); 1537 1538 out: 1539 kfree(user_ctx); 1540 kfree(data); 1541 return ret; 1542 } 1543 1544 int bpf_prog_test_run_sk_lookup(struct bpf_prog *prog, const union bpf_attr *kattr, 1545 union bpf_attr __user *uattr) 1546 { 1547 struct bpf_test_timer t = {}; 1548 struct bpf_prog_array *progs = NULL; 1549 struct bpf_sk_lookup_kern ctx = {}; 1550 u32 repeat = kattr->test.repeat; 1551 struct bpf_sk_lookup *user_ctx; 1552 u32 retval, duration; 1553 int ret = -EINVAL; 1554 1555 if (kattr->test.flags || kattr->test.cpu || kattr->test.batch_size) 1556 return -EINVAL; 1557 1558 if (kattr->test.data_in || kattr->test.data_size_in || kattr->test.data_out || 1559 kattr->test.data_size_out) 1560 return -EINVAL; 1561 1562 if (!repeat) 1563 repeat = 1; 1564 1565 user_ctx = bpf_ctx_init(kattr, sizeof(*user_ctx)); 1566 if (IS_ERR(user_ctx)) 1567 return PTR_ERR(user_ctx); 1568 1569 if (!user_ctx) 1570 return -EINVAL; 1571 1572 if (user_ctx->sk) 1573 goto out; 1574 1575 if (!range_is_zero(user_ctx, offsetofend(typeof(*user_ctx), local_port), sizeof(*user_ctx))) 1576 goto out; 1577 1578 if (user_ctx->local_port > U16_MAX) { 1579 ret = -ERANGE; 1580 goto out; 1581 } 1582 1583 ctx.family = (u16)user_ctx->family; 1584 ctx.protocol = (u16)user_ctx->protocol; 1585 ctx.dport = (u16)user_ctx->local_port; 1586 ctx.sport = user_ctx->remote_port; 1587 1588 switch (ctx.family) { 1589 case AF_INET: 1590 ctx.v4.daddr = (__force __be32)user_ctx->local_ip4; 1591 ctx.v4.saddr = (__force __be32)user_ctx->remote_ip4; 1592 break; 1593 1594 #if IS_ENABLED(CONFIG_IPV6) 1595 case AF_INET6: 1596 ctx.v6.daddr = (struct in6_addr *)user_ctx->local_ip6; 1597 ctx.v6.saddr = (struct in6_addr *)user_ctx->remote_ip6; 1598 break; 1599 #endif 1600 1601 default: 1602 ret = -EAFNOSUPPORT; 1603 goto out; 1604 } 1605 1606 progs = bpf_prog_array_alloc(1, GFP_KERNEL); 1607 if (!progs) { 1608 ret = -ENOMEM; 1609 goto out; 1610 } 1611 1612 progs->items[0].prog = prog; 1613 1614 bpf_test_timer_enter(&t); 1615 do { 1616 ctx.selected_sk = NULL; 1617 retval = BPF_PROG_SK_LOOKUP_RUN_ARRAY(progs, ctx, bpf_prog_run); 1618 } while (bpf_test_timer_continue(&t, 1, repeat, &ret, &duration)); 1619 bpf_test_timer_leave(&t); 1620 1621 if (ret < 0) 1622 goto out; 1623 1624 user_ctx->cookie = 0; 1625 if (ctx.selected_sk) { 1626 if (ctx.selected_sk->sk_reuseport && !ctx.no_reuseport) { 1627 ret = -EOPNOTSUPP; 1628 goto out; 1629 } 1630 1631 user_ctx->cookie = sock_gen_cookie(ctx.selected_sk); 1632 } 1633 1634 ret = bpf_test_finish(kattr, uattr, NULL, NULL, 0, 0, retval, duration); 1635 if (!ret) 1636 ret = bpf_ctx_finish(kattr, uattr, user_ctx, sizeof(*user_ctx)); 1637 1638 out: 1639 bpf_prog_array_free(progs); 1640 kfree(user_ctx); 1641 return ret; 1642 } 1643 1644 int bpf_prog_test_run_syscall(struct bpf_prog *prog, 1645 const union bpf_attr *kattr, 1646 union bpf_attr __user *uattr) 1647 { 1648 void __user *ctx_in = u64_to_user_ptr(kattr->test.ctx_in); 1649 __u32 ctx_size_in = kattr->test.ctx_size_in; 1650 void *ctx = NULL; 1651 u32 retval; 1652 int err = 0; 1653 1654 /* doesn't support data_in/out, ctx_out, duration, or repeat or flags */ 1655 if (kattr->test.data_in || kattr->test.data_out || 1656 kattr->test.ctx_out || kattr->test.duration || 1657 kattr->test.repeat || kattr->test.flags || 1658 kattr->test.batch_size) 1659 return -EINVAL; 1660 1661 if (ctx_size_in < prog->aux->max_ctx_offset || 1662 ctx_size_in > U16_MAX) 1663 return -EINVAL; 1664 1665 if (ctx_size_in) { 1666 ctx = memdup_user(ctx_in, ctx_size_in); 1667 if (IS_ERR(ctx)) 1668 return PTR_ERR(ctx); 1669 } 1670 1671 rcu_read_lock_trace(); 1672 retval = bpf_prog_run_pin_on_cpu(prog, ctx); 1673 rcu_read_unlock_trace(); 1674 1675 if (copy_to_user(&uattr->test.retval, &retval, sizeof(u32))) { 1676 err = -EFAULT; 1677 goto out; 1678 } 1679 if (ctx_size_in) 1680 if (copy_to_user(ctx_in, ctx, ctx_size_in)) 1681 err = -EFAULT; 1682 out: 1683 kfree(ctx); 1684 return err; 1685 } 1686 1687 static int verify_and_copy_hook_state(struct nf_hook_state *state, 1688 const struct nf_hook_state *user, 1689 struct net_device *dev) 1690 { 1691 if (user->in || user->out) 1692 return -EINVAL; 1693 1694 if (user->net || user->sk || user->okfn) 1695 return -EINVAL; 1696 1697 switch (user->pf) { 1698 case NFPROTO_IPV4: 1699 case NFPROTO_IPV6: 1700 switch (state->hook) { 1701 case NF_INET_PRE_ROUTING: 1702 state->in = dev; 1703 break; 1704 case NF_INET_LOCAL_IN: 1705 state->in = dev; 1706 break; 1707 case NF_INET_FORWARD: 1708 state->in = dev; 1709 state->out = dev; 1710 break; 1711 case NF_INET_LOCAL_OUT: 1712 state->out = dev; 1713 break; 1714 case NF_INET_POST_ROUTING: 1715 state->out = dev; 1716 break; 1717 } 1718 1719 break; 1720 default: 1721 return -EINVAL; 1722 } 1723 1724 state->pf = user->pf; 1725 state->hook = user->hook; 1726 1727 return 0; 1728 } 1729 1730 static __be16 nfproto_eth(int nfproto) 1731 { 1732 switch (nfproto) { 1733 case NFPROTO_IPV4: 1734 return htons(ETH_P_IP); 1735 case NFPROTO_IPV6: 1736 break; 1737 } 1738 1739 return htons(ETH_P_IPV6); 1740 } 1741 1742 int bpf_prog_test_run_nf(struct bpf_prog *prog, 1743 const union bpf_attr *kattr, 1744 union bpf_attr __user *uattr) 1745 { 1746 struct net *net = current->nsproxy->net_ns; 1747 struct net_device *dev = net->loopback_dev; 1748 struct nf_hook_state *user_ctx, hook_state = { 1749 .pf = NFPROTO_IPV4, 1750 .hook = NF_INET_LOCAL_OUT, 1751 }; 1752 u32 size = kattr->test.data_size_in; 1753 u32 repeat = kattr->test.repeat; 1754 struct bpf_nf_ctx ctx = { 1755 .state = &hook_state, 1756 }; 1757 struct sk_buff *skb = NULL; 1758 u32 retval, duration; 1759 void *data; 1760 int ret; 1761 1762 if (kattr->test.flags || kattr->test.cpu || kattr->test.batch_size) 1763 return -EINVAL; 1764 1765 if (size < sizeof(struct iphdr)) 1766 return -EINVAL; 1767 1768 data = bpf_test_init(kattr, kattr->test.data_size_in, size, 1769 NET_SKB_PAD + NET_IP_ALIGN, 1770 SKB_DATA_ALIGN(sizeof(struct skb_shared_info))); 1771 if (IS_ERR(data)) 1772 return PTR_ERR(data); 1773 1774 if (!repeat) 1775 repeat = 1; 1776 1777 user_ctx = bpf_ctx_init(kattr, sizeof(struct nf_hook_state)); 1778 if (IS_ERR(user_ctx)) { 1779 kfree(data); 1780 return PTR_ERR(user_ctx); 1781 } 1782 1783 if (user_ctx) { 1784 ret = verify_and_copy_hook_state(&hook_state, user_ctx, dev); 1785 if (ret) 1786 goto out; 1787 } 1788 1789 skb = slab_build_skb(data); 1790 if (!skb) { 1791 ret = -ENOMEM; 1792 goto out; 1793 } 1794 1795 data = NULL; /* data released via kfree_skb */ 1796 1797 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN); 1798 __skb_put(skb, size); 1799 1800 ret = -EINVAL; 1801 1802 if (hook_state.hook != NF_INET_LOCAL_OUT) { 1803 if (size < ETH_HLEN + sizeof(struct iphdr)) 1804 goto out; 1805 1806 skb->protocol = eth_type_trans(skb, dev); 1807 switch (skb->protocol) { 1808 case htons(ETH_P_IP): 1809 if (hook_state.pf == NFPROTO_IPV4) 1810 break; 1811 goto out; 1812 case htons(ETH_P_IPV6): 1813 if (size < ETH_HLEN + sizeof(struct ipv6hdr)) 1814 goto out; 1815 if (hook_state.pf == NFPROTO_IPV6) 1816 break; 1817 goto out; 1818 default: 1819 ret = -EPROTO; 1820 goto out; 1821 } 1822 1823 skb_reset_network_header(skb); 1824 } else { 1825 skb->protocol = nfproto_eth(hook_state.pf); 1826 } 1827 1828 ctx.skb = skb; 1829 1830 ret = bpf_test_run(prog, &ctx, repeat, &retval, &duration, false); 1831 if (ret) 1832 goto out; 1833 1834 ret = bpf_test_finish(kattr, uattr, NULL, NULL, 0, 0, retval, duration); 1835 1836 out: 1837 kfree(user_ctx); 1838 kfree_skb(skb); 1839 kfree(data); 1840 return ret; 1841 } 1842 1843 static const struct btf_kfunc_id_set bpf_prog_test_kfunc_set = { 1844 .owner = THIS_MODULE, 1845 .set = &test_sk_check_kfunc_ids, 1846 }; 1847 1848 BTF_ID_LIST(bpf_prog_test_dtor_kfunc_ids) 1849 BTF_ID(struct, prog_test_ref_kfunc) 1850 BTF_ID(func, bpf_kfunc_call_test_release_dtor) 1851 BTF_ID(struct, prog_test_member) 1852 BTF_ID(func, bpf_kfunc_call_memb_release_dtor) 1853 1854 static int __init bpf_prog_test_run_init(void) 1855 { 1856 const struct btf_id_dtor_kfunc bpf_prog_test_dtor_kfunc[] = { 1857 { 1858 .btf_id = bpf_prog_test_dtor_kfunc_ids[0], 1859 .kfunc_btf_id = bpf_prog_test_dtor_kfunc_ids[1] 1860 }, 1861 { 1862 .btf_id = bpf_prog_test_dtor_kfunc_ids[2], 1863 .kfunc_btf_id = bpf_prog_test_dtor_kfunc_ids[3], 1864 }, 1865 }; 1866 int ret; 1867 1868 ret = register_btf_fmodret_id_set(&bpf_test_modify_return_set); 1869 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, &bpf_prog_test_kfunc_set); 1870 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &bpf_prog_test_kfunc_set); 1871 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, &bpf_prog_test_kfunc_set); 1872 return ret ?: register_btf_id_dtor_kfuncs(bpf_prog_test_dtor_kfunc, 1873 ARRAY_SIZE(bpf_prog_test_dtor_kfunc), 1874 THIS_MODULE); 1875 } 1876 late_initcall(bpf_prog_test_run_init); 1877