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