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