125763b3cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21cf1cae9SAlexei Starovoitov /* Copyright (c) 2017 Facebook 31cf1cae9SAlexei Starovoitov */ 41cf1cae9SAlexei Starovoitov #include <linux/bpf.h> 5c48e51c8SKumar Kartikeya Dwivedi #include <linux/btf.h> 67bd1590dSMartin KaFai Lau #include <linux/btf_ids.h> 71cf1cae9SAlexei Starovoitov #include <linux/slab.h> 8b202d844SKumar Kartikeya Dwivedi #include <linux/init.h> 91cf1cae9SAlexei Starovoitov #include <linux/vmalloc.h> 101cf1cae9SAlexei Starovoitov #include <linux/etherdevice.h> 111cf1cae9SAlexei Starovoitov #include <linux/filter.h> 1287b7b533SYonghong Song #include <linux/rcupdate_trace.h> 131cf1cae9SAlexei Starovoitov #include <linux/sched/signal.h> 146ac99e8fSMartin KaFai Lau #include <net/bpf_sk_storage.h> 152cb494a3SSong Liu #include <net/sock.h> 162cb494a3SSong Liu #include <net/tcp.h> 177c32e8f8SLorenz Bauer #include <net/net_namespace.h> 183d08b6f2SKP Singh #include <linux/error-injection.h> 191b4d60ecSSong Liu #include <linux/smp.h> 207c32e8f8SLorenz Bauer #include <linux/sock_diag.h> 2147316f4aSZvi Effron #include <net/xdp.h> 221cf1cae9SAlexei Starovoitov 23e950e843SMatt Mullins #define CREATE_TRACE_POINTS 24e950e843SMatt Mullins #include <trace/events/bpf_test_run.h> 25e950e843SMatt Mullins 26607b9cc9SLorenz Bauer struct bpf_test_timer { 27607b9cc9SLorenz Bauer enum { NO_PREEMPT, NO_MIGRATE } mode; 28607b9cc9SLorenz Bauer u32 i; 29607b9cc9SLorenz Bauer u64 time_start, time_spent; 30607b9cc9SLorenz Bauer }; 31607b9cc9SLorenz Bauer 32607b9cc9SLorenz Bauer static void bpf_test_timer_enter(struct bpf_test_timer *t) 33607b9cc9SLorenz Bauer __acquires(rcu) 34607b9cc9SLorenz Bauer { 35607b9cc9SLorenz Bauer rcu_read_lock(); 36607b9cc9SLorenz Bauer if (t->mode == NO_PREEMPT) 37607b9cc9SLorenz Bauer preempt_disable(); 38607b9cc9SLorenz Bauer else 39607b9cc9SLorenz Bauer migrate_disable(); 40607b9cc9SLorenz Bauer 41607b9cc9SLorenz Bauer t->time_start = ktime_get_ns(); 42607b9cc9SLorenz Bauer } 43607b9cc9SLorenz Bauer 44607b9cc9SLorenz Bauer static void bpf_test_timer_leave(struct bpf_test_timer *t) 45607b9cc9SLorenz Bauer __releases(rcu) 46607b9cc9SLorenz Bauer { 47607b9cc9SLorenz Bauer t->time_start = 0; 48607b9cc9SLorenz Bauer 49607b9cc9SLorenz Bauer if (t->mode == NO_PREEMPT) 50607b9cc9SLorenz Bauer preempt_enable(); 51607b9cc9SLorenz Bauer else 52607b9cc9SLorenz Bauer migrate_enable(); 53607b9cc9SLorenz Bauer rcu_read_unlock(); 54607b9cc9SLorenz Bauer } 55607b9cc9SLorenz Bauer 56607b9cc9SLorenz Bauer static bool bpf_test_timer_continue(struct bpf_test_timer *t, u32 repeat, int *err, u32 *duration) 57607b9cc9SLorenz Bauer __must_hold(rcu) 58607b9cc9SLorenz Bauer { 59607b9cc9SLorenz Bauer t->i++; 60607b9cc9SLorenz Bauer if (t->i >= repeat) { 61607b9cc9SLorenz Bauer /* We're done. */ 62607b9cc9SLorenz Bauer t->time_spent += ktime_get_ns() - t->time_start; 63607b9cc9SLorenz Bauer do_div(t->time_spent, t->i); 64607b9cc9SLorenz Bauer *duration = t->time_spent > U32_MAX ? U32_MAX : (u32)t->time_spent; 65607b9cc9SLorenz Bauer *err = 0; 66607b9cc9SLorenz Bauer goto reset; 67607b9cc9SLorenz Bauer } 68607b9cc9SLorenz Bauer 69607b9cc9SLorenz Bauer if (signal_pending(current)) { 70607b9cc9SLorenz Bauer /* During iteration: we've been cancelled, abort. */ 71607b9cc9SLorenz Bauer *err = -EINTR; 72607b9cc9SLorenz Bauer goto reset; 73607b9cc9SLorenz Bauer } 74607b9cc9SLorenz Bauer 75607b9cc9SLorenz Bauer if (need_resched()) { 76607b9cc9SLorenz Bauer /* During iteration: we need to reschedule between runs. */ 77607b9cc9SLorenz Bauer t->time_spent += ktime_get_ns() - t->time_start; 78607b9cc9SLorenz Bauer bpf_test_timer_leave(t); 79607b9cc9SLorenz Bauer cond_resched(); 80607b9cc9SLorenz Bauer bpf_test_timer_enter(t); 81607b9cc9SLorenz Bauer } 82607b9cc9SLorenz Bauer 83607b9cc9SLorenz Bauer /* Do another round. */ 84607b9cc9SLorenz Bauer return true; 85607b9cc9SLorenz Bauer 86607b9cc9SLorenz Bauer reset: 87607b9cc9SLorenz Bauer t->i = 0; 88607b9cc9SLorenz Bauer return false; 89607b9cc9SLorenz Bauer } 90607b9cc9SLorenz Bauer 91df1a2cb7SStanislav Fomichev static int bpf_test_run(struct bpf_prog *prog, void *ctx, u32 repeat, 92f23c4b39SBjörn Töpel u32 *retval, u32 *time, bool xdp) 931cf1cae9SAlexei Starovoitov { 94c7603cfaSAndrii Nakryiko struct bpf_prog_array_item item = {.prog = prog}; 95c7603cfaSAndrii Nakryiko struct bpf_run_ctx *old_ctx; 96c7603cfaSAndrii Nakryiko struct bpf_cg_run_ctx run_ctx; 97607b9cc9SLorenz Bauer struct bpf_test_timer t = { NO_MIGRATE }; 988bad74f9SRoman Gushchin enum bpf_cgroup_storage_type stype; 99607b9cc9SLorenz Bauer int ret; 1001cf1cae9SAlexei Starovoitov 1018bad74f9SRoman Gushchin for_each_cgroup_storage_type(stype) { 102c7603cfaSAndrii Nakryiko item.cgroup_storage[stype] = bpf_cgroup_storage_alloc(prog, stype); 103c7603cfaSAndrii Nakryiko if (IS_ERR(item.cgroup_storage[stype])) { 104c7603cfaSAndrii Nakryiko item.cgroup_storage[stype] = NULL; 1058bad74f9SRoman Gushchin for_each_cgroup_storage_type(stype) 106c7603cfaSAndrii Nakryiko bpf_cgroup_storage_free(item.cgroup_storage[stype]); 1078bad74f9SRoman Gushchin return -ENOMEM; 1088bad74f9SRoman Gushchin } 1098bad74f9SRoman Gushchin } 110f42ee093SRoman Gushchin 1111cf1cae9SAlexei Starovoitov if (!repeat) 1121cf1cae9SAlexei Starovoitov repeat = 1; 113df1a2cb7SStanislav Fomichev 114607b9cc9SLorenz Bauer bpf_test_timer_enter(&t); 115c7603cfaSAndrii Nakryiko old_ctx = bpf_set_run_ctx(&run_ctx.run_ctx); 116607b9cc9SLorenz Bauer do { 117c7603cfaSAndrii Nakryiko run_ctx.prog_item = &item; 118f23c4b39SBjörn Töpel if (xdp) 119f23c4b39SBjörn Töpel *retval = bpf_prog_run_xdp(prog, ctx); 120f23c4b39SBjörn Töpel else 121fb7dd8bcSAndrii Nakryiko *retval = bpf_prog_run(prog, ctx); 122607b9cc9SLorenz Bauer } while (bpf_test_timer_continue(&t, repeat, &ret, time)); 123c7603cfaSAndrii Nakryiko bpf_reset_run_ctx(old_ctx); 124607b9cc9SLorenz Bauer bpf_test_timer_leave(&t); 1251cf1cae9SAlexei Starovoitov 1268bad74f9SRoman Gushchin for_each_cgroup_storage_type(stype) 127c7603cfaSAndrii Nakryiko bpf_cgroup_storage_free(item.cgroup_storage[stype]); 128f42ee093SRoman Gushchin 129df1a2cb7SStanislav Fomichev return ret; 1301cf1cae9SAlexei Starovoitov } 1311cf1cae9SAlexei Starovoitov 13278e52272SDavid Miller static int bpf_test_finish(const union bpf_attr *kattr, 13378e52272SDavid Miller union bpf_attr __user *uattr, const void *data, 1341cf1cae9SAlexei Starovoitov u32 size, u32 retval, u32 duration) 1351cf1cae9SAlexei Starovoitov { 13678e52272SDavid Miller void __user *data_out = u64_to_user_ptr(kattr->test.data_out); 1371cf1cae9SAlexei Starovoitov int err = -EFAULT; 138b5a36b1eSLorenz Bauer u32 copy_size = size; 1391cf1cae9SAlexei Starovoitov 140b5a36b1eSLorenz Bauer /* Clamp copy if the user has provided a size hint, but copy the full 141b5a36b1eSLorenz Bauer * buffer if not to retain old behaviour. 142b5a36b1eSLorenz Bauer */ 143b5a36b1eSLorenz Bauer if (kattr->test.data_size_out && 144b5a36b1eSLorenz Bauer copy_size > kattr->test.data_size_out) { 145b5a36b1eSLorenz Bauer copy_size = kattr->test.data_size_out; 146b5a36b1eSLorenz Bauer err = -ENOSPC; 147b5a36b1eSLorenz Bauer } 148b5a36b1eSLorenz Bauer 149b5a36b1eSLorenz Bauer if (data_out && copy_to_user(data_out, data, copy_size)) 1501cf1cae9SAlexei Starovoitov goto out; 1511cf1cae9SAlexei Starovoitov if (copy_to_user(&uattr->test.data_size_out, &size, sizeof(size))) 1521cf1cae9SAlexei Starovoitov goto out; 1531cf1cae9SAlexei Starovoitov if (copy_to_user(&uattr->test.retval, &retval, sizeof(retval))) 1541cf1cae9SAlexei Starovoitov goto out; 1551cf1cae9SAlexei Starovoitov if (copy_to_user(&uattr->test.duration, &duration, sizeof(duration))) 1561cf1cae9SAlexei Starovoitov goto out; 157b5a36b1eSLorenz Bauer if (err != -ENOSPC) 1581cf1cae9SAlexei Starovoitov err = 0; 1591cf1cae9SAlexei Starovoitov out: 160e950e843SMatt Mullins trace_bpf_test_finish(&err); 1611cf1cae9SAlexei Starovoitov return err; 1621cf1cae9SAlexei Starovoitov } 1631cf1cae9SAlexei Starovoitov 164faeb2dceSAlexei Starovoitov /* Integer types of various sizes and pointer combinations cover variety of 165faeb2dceSAlexei Starovoitov * architecture dependent calling conventions. 7+ can be supported in the 166faeb2dceSAlexei Starovoitov * future. 167faeb2dceSAlexei Starovoitov */ 168e9ff9d52SJean-Philippe Menil __diag_push(); 169e9ff9d52SJean-Philippe Menil __diag_ignore(GCC, 8, "-Wmissing-prototypes", 170e9ff9d52SJean-Philippe Menil "Global functions as their definitions will be in vmlinux BTF"); 171faeb2dceSAlexei Starovoitov int noinline bpf_fentry_test1(int a) 172faeb2dceSAlexei Starovoitov { 173faeb2dceSAlexei Starovoitov return a + 1; 174faeb2dceSAlexei Starovoitov } 17546565696SKumar Kartikeya Dwivedi EXPORT_SYMBOL_GPL(bpf_fentry_test1); 17646565696SKumar Kartikeya Dwivedi ALLOW_ERROR_INJECTION(bpf_fentry_test1, ERRNO); 177faeb2dceSAlexei Starovoitov 178faeb2dceSAlexei Starovoitov int noinline bpf_fentry_test2(int a, u64 b) 179faeb2dceSAlexei Starovoitov { 180faeb2dceSAlexei Starovoitov return a + b; 181faeb2dceSAlexei Starovoitov } 182faeb2dceSAlexei Starovoitov 183faeb2dceSAlexei Starovoitov int noinline bpf_fentry_test3(char a, int b, u64 c) 184faeb2dceSAlexei Starovoitov { 185faeb2dceSAlexei Starovoitov return a + b + c; 186faeb2dceSAlexei Starovoitov } 187faeb2dceSAlexei Starovoitov 188faeb2dceSAlexei Starovoitov int noinline bpf_fentry_test4(void *a, char b, int c, u64 d) 189faeb2dceSAlexei Starovoitov { 190faeb2dceSAlexei Starovoitov return (long)a + b + c + d; 191faeb2dceSAlexei Starovoitov } 192faeb2dceSAlexei Starovoitov 193faeb2dceSAlexei Starovoitov int noinline bpf_fentry_test5(u64 a, void *b, short c, int d, u64 e) 194faeb2dceSAlexei Starovoitov { 195faeb2dceSAlexei Starovoitov return a + (long)b + c + d + e; 196faeb2dceSAlexei Starovoitov } 197faeb2dceSAlexei Starovoitov 198faeb2dceSAlexei Starovoitov int noinline bpf_fentry_test6(u64 a, void *b, short c, int d, void *e, u64 f) 199faeb2dceSAlexei Starovoitov { 200faeb2dceSAlexei Starovoitov return a + (long)b + c + d + (long)e + f; 201faeb2dceSAlexei Starovoitov } 202faeb2dceSAlexei Starovoitov 203d923021cSYonghong Song struct bpf_fentry_test_t { 204d923021cSYonghong Song struct bpf_fentry_test_t *a; 205d923021cSYonghong Song }; 206d923021cSYonghong Song 207d923021cSYonghong Song int noinline bpf_fentry_test7(struct bpf_fentry_test_t *arg) 208d923021cSYonghong Song { 209d923021cSYonghong Song return (long)arg; 210d923021cSYonghong Song } 211d923021cSYonghong Song 212d923021cSYonghong Song int noinline bpf_fentry_test8(struct bpf_fentry_test_t *arg) 213d923021cSYonghong Song { 214d923021cSYonghong Song return (long)arg->a; 215d923021cSYonghong Song } 216d923021cSYonghong Song 2173d08b6f2SKP Singh int noinline bpf_modify_return_test(int a, int *b) 2183d08b6f2SKP Singh { 2193d08b6f2SKP Singh *b += 1; 2203d08b6f2SKP Singh return a + *b; 2213d08b6f2SKP Singh } 2227bd1590dSMartin KaFai Lau 2237bd1590dSMartin KaFai Lau u64 noinline bpf_kfunc_call_test1(struct sock *sk, u32 a, u64 b, u32 c, u64 d) 2247bd1590dSMartin KaFai Lau { 2257bd1590dSMartin KaFai Lau return a + b + c + d; 2267bd1590dSMartin KaFai Lau } 2277bd1590dSMartin KaFai Lau 2287bd1590dSMartin KaFai Lau int noinline bpf_kfunc_call_test2(struct sock *sk, u32 a, u32 b) 2297bd1590dSMartin KaFai Lau { 2307bd1590dSMartin KaFai Lau return a + b; 2317bd1590dSMartin KaFai Lau } 2327bd1590dSMartin KaFai Lau 2337bd1590dSMartin KaFai Lau struct sock * noinline bpf_kfunc_call_test3(struct sock *sk) 2347bd1590dSMartin KaFai Lau { 2357bd1590dSMartin KaFai Lau return sk; 2367bd1590dSMartin KaFai Lau } 2377bd1590dSMartin KaFai Lau 238c1ff181fSKumar Kartikeya Dwivedi struct prog_test_ref_kfunc { 239c1ff181fSKumar Kartikeya Dwivedi int a; 240c1ff181fSKumar Kartikeya Dwivedi int b; 241c1ff181fSKumar Kartikeya Dwivedi struct prog_test_ref_kfunc *next; 242c1ff181fSKumar Kartikeya Dwivedi }; 243c1ff181fSKumar Kartikeya Dwivedi 244c1ff181fSKumar Kartikeya Dwivedi static struct prog_test_ref_kfunc prog_test_struct = { 245c1ff181fSKumar Kartikeya Dwivedi .a = 42, 246c1ff181fSKumar Kartikeya Dwivedi .b = 108, 247c1ff181fSKumar Kartikeya Dwivedi .next = &prog_test_struct, 248c1ff181fSKumar Kartikeya Dwivedi }; 249c1ff181fSKumar Kartikeya Dwivedi 250c1ff181fSKumar Kartikeya Dwivedi noinline struct prog_test_ref_kfunc * 251c1ff181fSKumar Kartikeya Dwivedi bpf_kfunc_call_test_acquire(unsigned long *scalar_ptr) 252c1ff181fSKumar Kartikeya Dwivedi { 253c1ff181fSKumar Kartikeya Dwivedi /* randomly return NULL */ 254c1ff181fSKumar Kartikeya Dwivedi if (get_jiffies_64() % 2) 255c1ff181fSKumar Kartikeya Dwivedi return NULL; 256c1ff181fSKumar Kartikeya Dwivedi return &prog_test_struct; 257c1ff181fSKumar Kartikeya Dwivedi } 258c1ff181fSKumar Kartikeya Dwivedi 259c1ff181fSKumar Kartikeya Dwivedi noinline void bpf_kfunc_call_test_release(struct prog_test_ref_kfunc *p) 260c1ff181fSKumar Kartikeya Dwivedi { 261c1ff181fSKumar Kartikeya Dwivedi } 262c1ff181fSKumar Kartikeya Dwivedi 263c1ff181fSKumar Kartikeya Dwivedi struct prog_test_pass1 { 264c1ff181fSKumar Kartikeya Dwivedi int x0; 265c1ff181fSKumar Kartikeya Dwivedi struct { 266c1ff181fSKumar Kartikeya Dwivedi int x1; 267c1ff181fSKumar Kartikeya Dwivedi struct { 268c1ff181fSKumar Kartikeya Dwivedi int x2; 269c1ff181fSKumar Kartikeya Dwivedi struct { 270c1ff181fSKumar Kartikeya Dwivedi int x3; 271c1ff181fSKumar Kartikeya Dwivedi }; 272c1ff181fSKumar Kartikeya Dwivedi }; 273c1ff181fSKumar Kartikeya Dwivedi }; 274c1ff181fSKumar Kartikeya Dwivedi }; 275c1ff181fSKumar Kartikeya Dwivedi 276c1ff181fSKumar Kartikeya Dwivedi struct prog_test_pass2 { 277c1ff181fSKumar Kartikeya Dwivedi int len; 278c1ff181fSKumar Kartikeya Dwivedi short arr1[4]; 279c1ff181fSKumar Kartikeya Dwivedi struct { 280c1ff181fSKumar Kartikeya Dwivedi char arr2[4]; 281c1ff181fSKumar Kartikeya Dwivedi unsigned long arr3[8]; 282c1ff181fSKumar Kartikeya Dwivedi } x; 283c1ff181fSKumar Kartikeya Dwivedi }; 284c1ff181fSKumar Kartikeya Dwivedi 285c1ff181fSKumar Kartikeya Dwivedi struct prog_test_fail1 { 286c1ff181fSKumar Kartikeya Dwivedi void *p; 287c1ff181fSKumar Kartikeya Dwivedi int x; 288c1ff181fSKumar Kartikeya Dwivedi }; 289c1ff181fSKumar Kartikeya Dwivedi 290c1ff181fSKumar Kartikeya Dwivedi struct prog_test_fail2 { 291c1ff181fSKumar Kartikeya Dwivedi int x8; 292c1ff181fSKumar Kartikeya Dwivedi struct prog_test_pass1 x; 293c1ff181fSKumar Kartikeya Dwivedi }; 294c1ff181fSKumar Kartikeya Dwivedi 295c1ff181fSKumar Kartikeya Dwivedi struct prog_test_fail3 { 296c1ff181fSKumar Kartikeya Dwivedi int len; 297c1ff181fSKumar Kartikeya Dwivedi char arr1[2]; 298c1ff181fSKumar Kartikeya Dwivedi char arr2[0]; 299c1ff181fSKumar Kartikeya Dwivedi }; 300c1ff181fSKumar Kartikeya Dwivedi 301c1ff181fSKumar Kartikeya Dwivedi noinline void bpf_kfunc_call_test_pass_ctx(struct __sk_buff *skb) 302c1ff181fSKumar Kartikeya Dwivedi { 303c1ff181fSKumar Kartikeya Dwivedi } 304c1ff181fSKumar Kartikeya Dwivedi 305c1ff181fSKumar Kartikeya Dwivedi noinline void bpf_kfunc_call_test_pass1(struct prog_test_pass1 *p) 306c1ff181fSKumar Kartikeya Dwivedi { 307c1ff181fSKumar Kartikeya Dwivedi } 308c1ff181fSKumar Kartikeya Dwivedi 309c1ff181fSKumar Kartikeya Dwivedi noinline void bpf_kfunc_call_test_pass2(struct prog_test_pass2 *p) 310c1ff181fSKumar Kartikeya Dwivedi { 311c1ff181fSKumar Kartikeya Dwivedi } 312c1ff181fSKumar Kartikeya Dwivedi 313c1ff181fSKumar Kartikeya Dwivedi noinline void bpf_kfunc_call_test_fail1(struct prog_test_fail1 *p) 314c1ff181fSKumar Kartikeya Dwivedi { 315c1ff181fSKumar Kartikeya Dwivedi } 316c1ff181fSKumar Kartikeya Dwivedi 317c1ff181fSKumar Kartikeya Dwivedi noinline void bpf_kfunc_call_test_fail2(struct prog_test_fail2 *p) 318c1ff181fSKumar Kartikeya Dwivedi { 319c1ff181fSKumar Kartikeya Dwivedi } 320c1ff181fSKumar Kartikeya Dwivedi 321c1ff181fSKumar Kartikeya Dwivedi noinline void bpf_kfunc_call_test_fail3(struct prog_test_fail3 *p) 322c1ff181fSKumar Kartikeya Dwivedi { 323c1ff181fSKumar Kartikeya Dwivedi } 324c1ff181fSKumar Kartikeya Dwivedi 325c1ff181fSKumar Kartikeya Dwivedi noinline void bpf_kfunc_call_test_mem_len_pass1(void *mem, int mem__sz) 326c1ff181fSKumar Kartikeya Dwivedi { 327c1ff181fSKumar Kartikeya Dwivedi } 328c1ff181fSKumar Kartikeya Dwivedi 329c1ff181fSKumar Kartikeya Dwivedi noinline void bpf_kfunc_call_test_mem_len_fail1(void *mem, int len) 330c1ff181fSKumar Kartikeya Dwivedi { 331c1ff181fSKumar Kartikeya Dwivedi } 332c1ff181fSKumar Kartikeya Dwivedi 333c1ff181fSKumar Kartikeya Dwivedi noinline void bpf_kfunc_call_test_mem_len_fail2(u64 *mem, int len) 334c1ff181fSKumar Kartikeya Dwivedi { 335c1ff181fSKumar Kartikeya Dwivedi } 336c1ff181fSKumar Kartikeya Dwivedi 337e9ff9d52SJean-Philippe Menil __diag_pop(); 3383d08b6f2SKP Singh 3393d08b6f2SKP Singh ALLOW_ERROR_INJECTION(bpf_modify_return_test, ERRNO); 3403d08b6f2SKP Singh 341b202d844SKumar Kartikeya Dwivedi BTF_SET_START(test_sk_check_kfunc_ids) 3427bd1590dSMartin KaFai Lau BTF_ID(func, bpf_kfunc_call_test1) 3437bd1590dSMartin KaFai Lau BTF_ID(func, bpf_kfunc_call_test2) 3447bd1590dSMartin KaFai Lau BTF_ID(func, bpf_kfunc_call_test3) 345c1ff181fSKumar Kartikeya Dwivedi BTF_ID(func, bpf_kfunc_call_test_acquire) 346c1ff181fSKumar Kartikeya Dwivedi BTF_ID(func, bpf_kfunc_call_test_release) 347c1ff181fSKumar Kartikeya Dwivedi BTF_ID(func, bpf_kfunc_call_test_pass_ctx) 348c1ff181fSKumar Kartikeya Dwivedi BTF_ID(func, bpf_kfunc_call_test_pass1) 349c1ff181fSKumar Kartikeya Dwivedi BTF_ID(func, bpf_kfunc_call_test_pass2) 350c1ff181fSKumar Kartikeya Dwivedi BTF_ID(func, bpf_kfunc_call_test_fail1) 351c1ff181fSKumar Kartikeya Dwivedi BTF_ID(func, bpf_kfunc_call_test_fail2) 352c1ff181fSKumar Kartikeya Dwivedi BTF_ID(func, bpf_kfunc_call_test_fail3) 353c1ff181fSKumar Kartikeya Dwivedi BTF_ID(func, bpf_kfunc_call_test_mem_len_pass1) 354c1ff181fSKumar Kartikeya Dwivedi BTF_ID(func, bpf_kfunc_call_test_mem_len_fail1) 355c1ff181fSKumar Kartikeya Dwivedi BTF_ID(func, bpf_kfunc_call_test_mem_len_fail2) 356b202d844SKumar Kartikeya Dwivedi BTF_SET_END(test_sk_check_kfunc_ids) 3577bd1590dSMartin KaFai Lau 358c1ff181fSKumar Kartikeya Dwivedi BTF_SET_START(test_sk_acquire_kfunc_ids) 359c1ff181fSKumar Kartikeya Dwivedi BTF_ID(func, bpf_kfunc_call_test_acquire) 360c1ff181fSKumar Kartikeya Dwivedi BTF_SET_END(test_sk_acquire_kfunc_ids) 361c1ff181fSKumar Kartikeya Dwivedi 362c1ff181fSKumar Kartikeya Dwivedi BTF_SET_START(test_sk_release_kfunc_ids) 363c1ff181fSKumar Kartikeya Dwivedi BTF_ID(func, bpf_kfunc_call_test_release) 364c1ff181fSKumar Kartikeya Dwivedi BTF_SET_END(test_sk_release_kfunc_ids) 365c1ff181fSKumar Kartikeya Dwivedi 366c1ff181fSKumar Kartikeya Dwivedi BTF_SET_START(test_sk_ret_null_kfunc_ids) 367c1ff181fSKumar Kartikeya Dwivedi BTF_ID(func, bpf_kfunc_call_test_acquire) 368c1ff181fSKumar Kartikeya Dwivedi BTF_SET_END(test_sk_ret_null_kfunc_ids) 369c1ff181fSKumar Kartikeya Dwivedi 370be3d72a2SLorenzo Bianconi static void *bpf_test_init(const union bpf_attr *kattr, u32 user_size, 371be3d72a2SLorenzo Bianconi u32 size, u32 headroom, u32 tailroom) 3721cf1cae9SAlexei Starovoitov { 3731cf1cae9SAlexei Starovoitov void __user *data_in = u64_to_user_ptr(kattr->test.data_in); 3741cf1cae9SAlexei Starovoitov void *data; 3751cf1cae9SAlexei Starovoitov 3761cf1cae9SAlexei Starovoitov if (size < ETH_HLEN || size > PAGE_SIZE - headroom - tailroom) 3771cf1cae9SAlexei Starovoitov return ERR_PTR(-EINVAL); 3781cf1cae9SAlexei Starovoitov 379d800bad6SJesper Dangaard Brouer if (user_size > size) 380d800bad6SJesper Dangaard Brouer return ERR_PTR(-EMSGSIZE); 381d800bad6SJesper Dangaard Brouer 3821cf1cae9SAlexei Starovoitov data = kzalloc(size + headroom + tailroom, GFP_USER); 3831cf1cae9SAlexei Starovoitov if (!data) 3841cf1cae9SAlexei Starovoitov return ERR_PTR(-ENOMEM); 3851cf1cae9SAlexei Starovoitov 386d800bad6SJesper Dangaard Brouer if (copy_from_user(data + headroom, data_in, user_size)) { 3871cf1cae9SAlexei Starovoitov kfree(data); 3881cf1cae9SAlexei Starovoitov return ERR_PTR(-EFAULT); 3891cf1cae9SAlexei Starovoitov } 390da00d2f1SKP Singh 391da00d2f1SKP Singh return data; 392da00d2f1SKP Singh } 393da00d2f1SKP Singh 394da00d2f1SKP Singh int bpf_prog_test_run_tracing(struct bpf_prog *prog, 395da00d2f1SKP Singh const union bpf_attr *kattr, 396da00d2f1SKP Singh union bpf_attr __user *uattr) 397da00d2f1SKP Singh { 398d923021cSYonghong Song struct bpf_fentry_test_t arg = {}; 3993d08b6f2SKP Singh u16 side_effect = 0, ret = 0; 4003d08b6f2SKP Singh int b = 2, err = -EFAULT; 4013d08b6f2SKP Singh u32 retval = 0; 402da00d2f1SKP Singh 4031b4d60ecSSong Liu if (kattr->test.flags || kattr->test.cpu) 4041b4d60ecSSong Liu return -EINVAL; 4051b4d60ecSSong Liu 406da00d2f1SKP Singh switch (prog->expected_attach_type) { 407da00d2f1SKP Singh case BPF_TRACE_FENTRY: 408da00d2f1SKP Singh case BPF_TRACE_FEXIT: 409faeb2dceSAlexei Starovoitov if (bpf_fentry_test1(1) != 2 || 410faeb2dceSAlexei Starovoitov bpf_fentry_test2(2, 3) != 5 || 411faeb2dceSAlexei Starovoitov bpf_fentry_test3(4, 5, 6) != 15 || 412faeb2dceSAlexei Starovoitov bpf_fentry_test4((void *)7, 8, 9, 10) != 34 || 413faeb2dceSAlexei Starovoitov bpf_fentry_test5(11, (void *)12, 13, 14, 15) != 65 || 414d923021cSYonghong Song bpf_fentry_test6(16, (void *)17, 18, 19, (void *)20, 21) != 111 || 415d923021cSYonghong Song bpf_fentry_test7((struct bpf_fentry_test_t *)0) != 0 || 416d923021cSYonghong Song bpf_fentry_test8(&arg) != 0) 417da00d2f1SKP Singh goto out; 418da00d2f1SKP Singh break; 4193d08b6f2SKP Singh case BPF_MODIFY_RETURN: 4203d08b6f2SKP Singh ret = bpf_modify_return_test(1, &b); 4213d08b6f2SKP Singh if (b != 2) 4223d08b6f2SKP Singh side_effect = 1; 4233d08b6f2SKP Singh break; 424da00d2f1SKP Singh default: 425da00d2f1SKP Singh goto out; 426a25ecd9dSColin Ian King } 427da00d2f1SKP Singh 4283d08b6f2SKP Singh retval = ((u32)side_effect << 16) | ret; 4293d08b6f2SKP Singh if (copy_to_user(&uattr->test.retval, &retval, sizeof(retval))) 4303d08b6f2SKP Singh goto out; 4313d08b6f2SKP Singh 432da00d2f1SKP Singh err = 0; 433da00d2f1SKP Singh out: 434da00d2f1SKP Singh trace_bpf_test_finish(&err); 435da00d2f1SKP Singh return err; 4361cf1cae9SAlexei Starovoitov } 4371cf1cae9SAlexei Starovoitov 4381b4d60ecSSong Liu struct bpf_raw_tp_test_run_info { 4391b4d60ecSSong Liu struct bpf_prog *prog; 4401b4d60ecSSong Liu void *ctx; 4411b4d60ecSSong Liu u32 retval; 4421b4d60ecSSong Liu }; 4431b4d60ecSSong Liu 4441b4d60ecSSong Liu static void 4451b4d60ecSSong Liu __bpf_prog_test_run_raw_tp(void *data) 4461b4d60ecSSong Liu { 4471b4d60ecSSong Liu struct bpf_raw_tp_test_run_info *info = data; 4481b4d60ecSSong Liu 4491b4d60ecSSong Liu rcu_read_lock(); 450fb7dd8bcSAndrii Nakryiko info->retval = bpf_prog_run(info->prog, info->ctx); 4511b4d60ecSSong Liu rcu_read_unlock(); 4521b4d60ecSSong Liu } 4531b4d60ecSSong Liu 4541b4d60ecSSong Liu int bpf_prog_test_run_raw_tp(struct bpf_prog *prog, 4551b4d60ecSSong Liu const union bpf_attr *kattr, 4561b4d60ecSSong Liu union bpf_attr __user *uattr) 4571b4d60ecSSong Liu { 4581b4d60ecSSong Liu void __user *ctx_in = u64_to_user_ptr(kattr->test.ctx_in); 4591b4d60ecSSong Liu __u32 ctx_size_in = kattr->test.ctx_size_in; 4601b4d60ecSSong Liu struct bpf_raw_tp_test_run_info info; 4611b4d60ecSSong Liu int cpu = kattr->test.cpu, err = 0; 462963ec27aSSong Liu int current_cpu; 4631b4d60ecSSong Liu 4641b4d60ecSSong Liu /* doesn't support data_in/out, ctx_out, duration, or repeat */ 4651b4d60ecSSong Liu if (kattr->test.data_in || kattr->test.data_out || 4661b4d60ecSSong Liu kattr->test.ctx_out || kattr->test.duration || 4671b4d60ecSSong Liu kattr->test.repeat) 4681b4d60ecSSong Liu return -EINVAL; 4691b4d60ecSSong Liu 4707ac6ad05SSong Liu if (ctx_size_in < prog->aux->max_ctx_offset || 4717ac6ad05SSong Liu ctx_size_in > MAX_BPF_FUNC_ARGS * sizeof(u64)) 4721b4d60ecSSong Liu return -EINVAL; 4731b4d60ecSSong Liu 4741b4d60ecSSong Liu if ((kattr->test.flags & BPF_F_TEST_RUN_ON_CPU) == 0 && cpu != 0) 4751b4d60ecSSong Liu return -EINVAL; 4761b4d60ecSSong Liu 4771b4d60ecSSong Liu if (ctx_size_in) { 478db5b6a46SQing Wang info.ctx = memdup_user(ctx_in, ctx_size_in); 479db5b6a46SQing Wang if (IS_ERR(info.ctx)) 480db5b6a46SQing Wang return PTR_ERR(info.ctx); 4811b4d60ecSSong Liu } else { 4821b4d60ecSSong Liu info.ctx = NULL; 4831b4d60ecSSong Liu } 4841b4d60ecSSong Liu 4851b4d60ecSSong Liu info.prog = prog; 4861b4d60ecSSong Liu 487963ec27aSSong Liu current_cpu = get_cpu(); 4881b4d60ecSSong Liu if ((kattr->test.flags & BPF_F_TEST_RUN_ON_CPU) == 0 || 489963ec27aSSong Liu cpu == current_cpu) { 4901b4d60ecSSong Liu __bpf_prog_test_run_raw_tp(&info); 491963ec27aSSong Liu } else if (cpu >= nr_cpu_ids || !cpu_online(cpu)) { 4921b4d60ecSSong Liu /* smp_call_function_single() also checks cpu_online() 4931b4d60ecSSong Liu * after csd_lock(). However, since cpu is from user 4941b4d60ecSSong Liu * space, let's do an extra quick check to filter out 4951b4d60ecSSong Liu * invalid value before smp_call_function_single(). 4961b4d60ecSSong Liu */ 4971b4d60ecSSong Liu err = -ENXIO; 498963ec27aSSong Liu } else { 4991b4d60ecSSong Liu err = smp_call_function_single(cpu, __bpf_prog_test_run_raw_tp, 5001b4d60ecSSong Liu &info, 1); 5011b4d60ecSSong Liu } 502963ec27aSSong Liu put_cpu(); 5031b4d60ecSSong Liu 504963ec27aSSong Liu if (!err && 505963ec27aSSong Liu copy_to_user(&uattr->test.retval, &info.retval, sizeof(u32))) 5061b4d60ecSSong Liu err = -EFAULT; 5071b4d60ecSSong Liu 5081b4d60ecSSong Liu kfree(info.ctx); 5091b4d60ecSSong Liu return err; 5101b4d60ecSSong Liu } 5111b4d60ecSSong Liu 512b0b9395dSStanislav Fomichev static void *bpf_ctx_init(const union bpf_attr *kattr, u32 max_size) 513b0b9395dSStanislav Fomichev { 514b0b9395dSStanislav Fomichev void __user *data_in = u64_to_user_ptr(kattr->test.ctx_in); 515b0b9395dSStanislav Fomichev void __user *data_out = u64_to_user_ptr(kattr->test.ctx_out); 516b0b9395dSStanislav Fomichev u32 size = kattr->test.ctx_size_in; 517b0b9395dSStanislav Fomichev void *data; 518b0b9395dSStanislav Fomichev int err; 519b0b9395dSStanislav Fomichev 520b0b9395dSStanislav Fomichev if (!data_in && !data_out) 521b0b9395dSStanislav Fomichev return NULL; 522b0b9395dSStanislav Fomichev 523b0b9395dSStanislav Fomichev data = kzalloc(max_size, GFP_USER); 524b0b9395dSStanislav Fomichev if (!data) 525b0b9395dSStanislav Fomichev return ERR_PTR(-ENOMEM); 526b0b9395dSStanislav Fomichev 527b0b9395dSStanislav Fomichev if (data_in) { 528af2ac3e1SAlexei Starovoitov err = bpf_check_uarg_tail_zero(USER_BPFPTR(data_in), max_size, size); 529b0b9395dSStanislav Fomichev if (err) { 530b0b9395dSStanislav Fomichev kfree(data); 531b0b9395dSStanislav Fomichev return ERR_PTR(err); 532b0b9395dSStanislav Fomichev } 533b0b9395dSStanislav Fomichev 534b0b9395dSStanislav Fomichev size = min_t(u32, max_size, size); 535b0b9395dSStanislav Fomichev if (copy_from_user(data, data_in, size)) { 536b0b9395dSStanislav Fomichev kfree(data); 537b0b9395dSStanislav Fomichev return ERR_PTR(-EFAULT); 538b0b9395dSStanislav Fomichev } 539b0b9395dSStanislav Fomichev } 540b0b9395dSStanislav Fomichev return data; 541b0b9395dSStanislav Fomichev } 542b0b9395dSStanislav Fomichev 543b0b9395dSStanislav Fomichev static int bpf_ctx_finish(const union bpf_attr *kattr, 544b0b9395dSStanislav Fomichev union bpf_attr __user *uattr, const void *data, 545b0b9395dSStanislav Fomichev u32 size) 546b0b9395dSStanislav Fomichev { 547b0b9395dSStanislav Fomichev void __user *data_out = u64_to_user_ptr(kattr->test.ctx_out); 548b0b9395dSStanislav Fomichev int err = -EFAULT; 549b0b9395dSStanislav Fomichev u32 copy_size = size; 550b0b9395dSStanislav Fomichev 551b0b9395dSStanislav Fomichev if (!data || !data_out) 552b0b9395dSStanislav Fomichev return 0; 553b0b9395dSStanislav Fomichev 554b0b9395dSStanislav Fomichev if (copy_size > kattr->test.ctx_size_out) { 555b0b9395dSStanislav Fomichev copy_size = kattr->test.ctx_size_out; 556b0b9395dSStanislav Fomichev err = -ENOSPC; 557b0b9395dSStanislav Fomichev } 558b0b9395dSStanislav Fomichev 559b0b9395dSStanislav Fomichev if (copy_to_user(data_out, data, copy_size)) 560b0b9395dSStanislav Fomichev goto out; 561b0b9395dSStanislav Fomichev if (copy_to_user(&uattr->test.ctx_size_out, &size, sizeof(size))) 562b0b9395dSStanislav Fomichev goto out; 563b0b9395dSStanislav Fomichev if (err != -ENOSPC) 564b0b9395dSStanislav Fomichev err = 0; 565b0b9395dSStanislav Fomichev out: 566b0b9395dSStanislav Fomichev return err; 567b0b9395dSStanislav Fomichev } 568b0b9395dSStanislav Fomichev 569b0b9395dSStanislav Fomichev /** 570b0b9395dSStanislav Fomichev * range_is_zero - test whether buffer is initialized 571b0b9395dSStanislav Fomichev * @buf: buffer to check 572b0b9395dSStanislav Fomichev * @from: check from this position 573b0b9395dSStanislav Fomichev * @to: check up until (excluding) this position 574b0b9395dSStanislav Fomichev * 575b0b9395dSStanislav Fomichev * This function returns true if the there is a non-zero byte 576b0b9395dSStanislav Fomichev * in the buf in the range [from,to). 577b0b9395dSStanislav Fomichev */ 578b0b9395dSStanislav Fomichev static inline bool range_is_zero(void *buf, size_t from, size_t to) 579b0b9395dSStanislav Fomichev { 580b0b9395dSStanislav Fomichev return !memchr_inv((u8 *)buf + from, 0, to - from); 581b0b9395dSStanislav Fomichev } 582b0b9395dSStanislav Fomichev 583b0b9395dSStanislav Fomichev static int convert___skb_to_skb(struct sk_buff *skb, struct __sk_buff *__skb) 584b0b9395dSStanislav Fomichev { 585b0b9395dSStanislav Fomichev struct qdisc_skb_cb *cb = (struct qdisc_skb_cb *)skb->cb; 586b0b9395dSStanislav Fomichev 587b0b9395dSStanislav Fomichev if (!__skb) 588b0b9395dSStanislav Fomichev return 0; 589b0b9395dSStanislav Fomichev 590b0b9395dSStanislav Fomichev /* make sure the fields we don't use are zeroed */ 5916de6c1f8SNikita V. Shirokov if (!range_is_zero(__skb, 0, offsetof(struct __sk_buff, mark))) 5926de6c1f8SNikita V. Shirokov return -EINVAL; 5936de6c1f8SNikita V. Shirokov 5946de6c1f8SNikita V. Shirokov /* mark is allowed */ 5956de6c1f8SNikita V. Shirokov 5966de6c1f8SNikita V. Shirokov if (!range_is_zero(__skb, offsetofend(struct __sk_buff, mark), 5976de6c1f8SNikita V. Shirokov offsetof(struct __sk_buff, priority))) 598b0b9395dSStanislav Fomichev return -EINVAL; 599b0b9395dSStanislav Fomichev 600b0b9395dSStanislav Fomichev /* priority is allowed */ 601b238290bSNeil Spring /* ingress_ifindex is allowed */ 60221594c44SDmitry Yakunin /* ifindex is allowed */ 60321594c44SDmitry Yakunin 60421594c44SDmitry Yakunin if (!range_is_zero(__skb, offsetofend(struct __sk_buff, ifindex), 605b0b9395dSStanislav Fomichev offsetof(struct __sk_buff, cb))) 606b0b9395dSStanislav Fomichev return -EINVAL; 607b0b9395dSStanislav Fomichev 608b0b9395dSStanislav Fomichev /* cb is allowed */ 609b0b9395dSStanislav Fomichev 610b590cb5fSStanislav Fomichev if (!range_is_zero(__skb, offsetofend(struct __sk_buff, cb), 611ba940948SStanislav Fomichev offsetof(struct __sk_buff, tstamp))) 612ba940948SStanislav Fomichev return -EINVAL; 613ba940948SStanislav Fomichev 614ba940948SStanislav Fomichev /* tstamp is allowed */ 615850a88ccSStanislav Fomichev /* wire_len is allowed */ 616850a88ccSStanislav Fomichev /* gso_segs is allowed */ 617ba940948SStanislav Fomichev 618850a88ccSStanislav Fomichev if (!range_is_zero(__skb, offsetofend(struct __sk_buff, gso_segs), 619cf62089bSWillem de Bruijn offsetof(struct __sk_buff, gso_size))) 620cf62089bSWillem de Bruijn return -EINVAL; 621cf62089bSWillem de Bruijn 622cf62089bSWillem de Bruijn /* gso_size is allowed */ 623cf62089bSWillem de Bruijn 624cf62089bSWillem de Bruijn if (!range_is_zero(__skb, offsetofend(struct __sk_buff, gso_size), 6253384c7c7SVadim Fedorenko offsetof(struct __sk_buff, hwtstamp))) 6263384c7c7SVadim Fedorenko return -EINVAL; 6273384c7c7SVadim Fedorenko 6283384c7c7SVadim Fedorenko /* hwtstamp is allowed */ 6293384c7c7SVadim Fedorenko 6303384c7c7SVadim Fedorenko if (!range_is_zero(__skb, offsetofend(struct __sk_buff, hwtstamp), 631b0b9395dSStanislav Fomichev sizeof(struct __sk_buff))) 632b0b9395dSStanislav Fomichev return -EINVAL; 633b0b9395dSStanislav Fomichev 6346de6c1f8SNikita V. Shirokov skb->mark = __skb->mark; 635b0b9395dSStanislav Fomichev skb->priority = __skb->priority; 636b238290bSNeil Spring skb->skb_iif = __skb->ingress_ifindex; 637ba940948SStanislav Fomichev skb->tstamp = __skb->tstamp; 638b0b9395dSStanislav Fomichev memcpy(&cb->data, __skb->cb, QDISC_CB_PRIV_LEN); 639b0b9395dSStanislav Fomichev 640850a88ccSStanislav Fomichev if (__skb->wire_len == 0) { 641850a88ccSStanislav Fomichev cb->pkt_len = skb->len; 642850a88ccSStanislav Fomichev } else { 643850a88ccSStanislav Fomichev if (__skb->wire_len < skb->len || 644850a88ccSStanislav Fomichev __skb->wire_len > GSO_MAX_SIZE) 645850a88ccSStanislav Fomichev return -EINVAL; 646850a88ccSStanislav Fomichev cb->pkt_len = __skb->wire_len; 647850a88ccSStanislav Fomichev } 648850a88ccSStanislav Fomichev 649850a88ccSStanislav Fomichev if (__skb->gso_segs > GSO_MAX_SEGS) 650850a88ccSStanislav Fomichev return -EINVAL; 651850a88ccSStanislav Fomichev skb_shinfo(skb)->gso_segs = __skb->gso_segs; 652cf62089bSWillem de Bruijn skb_shinfo(skb)->gso_size = __skb->gso_size; 6533384c7c7SVadim Fedorenko skb_shinfo(skb)->hwtstamps.hwtstamp = __skb->hwtstamp; 654850a88ccSStanislav Fomichev 655b0b9395dSStanislav Fomichev return 0; 656b0b9395dSStanislav Fomichev } 657b0b9395dSStanislav Fomichev 658b0b9395dSStanislav Fomichev static void convert_skb_to___skb(struct sk_buff *skb, struct __sk_buff *__skb) 659b0b9395dSStanislav Fomichev { 660b0b9395dSStanislav Fomichev struct qdisc_skb_cb *cb = (struct qdisc_skb_cb *)skb->cb; 661b0b9395dSStanislav Fomichev 662b0b9395dSStanislav Fomichev if (!__skb) 663b0b9395dSStanislav Fomichev return; 664b0b9395dSStanislav Fomichev 6656de6c1f8SNikita V. Shirokov __skb->mark = skb->mark; 666b0b9395dSStanislav Fomichev __skb->priority = skb->priority; 667b238290bSNeil Spring __skb->ingress_ifindex = skb->skb_iif; 66821594c44SDmitry Yakunin __skb->ifindex = skb->dev->ifindex; 669ba940948SStanislav Fomichev __skb->tstamp = skb->tstamp; 670b0b9395dSStanislav Fomichev memcpy(__skb->cb, &cb->data, QDISC_CB_PRIV_LEN); 671850a88ccSStanislav Fomichev __skb->wire_len = cb->pkt_len; 672850a88ccSStanislav Fomichev __skb->gso_segs = skb_shinfo(skb)->gso_segs; 6733384c7c7SVadim Fedorenko __skb->hwtstamp = skb_shinfo(skb)->hwtstamps.hwtstamp; 674b0b9395dSStanislav Fomichev } 675b0b9395dSStanislav Fomichev 676435b08ecSDaniel Borkmann static struct proto bpf_dummy_proto = { 677435b08ecSDaniel Borkmann .name = "bpf_dummy", 678435b08ecSDaniel Borkmann .owner = THIS_MODULE, 679435b08ecSDaniel Borkmann .obj_size = sizeof(struct sock), 680435b08ecSDaniel Borkmann }; 681435b08ecSDaniel Borkmann 6821cf1cae9SAlexei Starovoitov int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr, 6831cf1cae9SAlexei Starovoitov union bpf_attr __user *uattr) 6841cf1cae9SAlexei Starovoitov { 6851cf1cae9SAlexei Starovoitov bool is_l2 = false, is_direct_pkt_access = false; 68621594c44SDmitry Yakunin struct net *net = current->nsproxy->net_ns; 68721594c44SDmitry Yakunin struct net_device *dev = net->loopback_dev; 6881cf1cae9SAlexei Starovoitov u32 size = kattr->test.data_size_in; 6891cf1cae9SAlexei Starovoitov u32 repeat = kattr->test.repeat; 690b0b9395dSStanislav Fomichev struct __sk_buff *ctx = NULL; 6911cf1cae9SAlexei Starovoitov u32 retval, duration; 6926e6fddc7SDaniel Borkmann int hh_len = ETH_HLEN; 6931cf1cae9SAlexei Starovoitov struct sk_buff *skb; 6942cb494a3SSong Liu struct sock *sk; 6951cf1cae9SAlexei Starovoitov void *data; 6961cf1cae9SAlexei Starovoitov int ret; 6971cf1cae9SAlexei Starovoitov 6981b4d60ecSSong Liu if (kattr->test.flags || kattr->test.cpu) 6991b4d60ecSSong Liu return -EINVAL; 7001b4d60ecSSong Liu 701be3d72a2SLorenzo Bianconi data = bpf_test_init(kattr, kattr->test.data_size_in, 702be3d72a2SLorenzo Bianconi size, NET_SKB_PAD + NET_IP_ALIGN, 7031cf1cae9SAlexei Starovoitov SKB_DATA_ALIGN(sizeof(struct skb_shared_info))); 7041cf1cae9SAlexei Starovoitov if (IS_ERR(data)) 7051cf1cae9SAlexei Starovoitov return PTR_ERR(data); 7061cf1cae9SAlexei Starovoitov 707b0b9395dSStanislav Fomichev ctx = bpf_ctx_init(kattr, sizeof(struct __sk_buff)); 708b0b9395dSStanislav Fomichev if (IS_ERR(ctx)) { 709b0b9395dSStanislav Fomichev kfree(data); 710b0b9395dSStanislav Fomichev return PTR_ERR(ctx); 711b0b9395dSStanislav Fomichev } 712b0b9395dSStanislav Fomichev 7131cf1cae9SAlexei Starovoitov switch (prog->type) { 7141cf1cae9SAlexei Starovoitov case BPF_PROG_TYPE_SCHED_CLS: 7151cf1cae9SAlexei Starovoitov case BPF_PROG_TYPE_SCHED_ACT: 7161cf1cae9SAlexei Starovoitov is_l2 = true; 717df561f66SGustavo A. R. Silva fallthrough; 7181cf1cae9SAlexei Starovoitov case BPF_PROG_TYPE_LWT_IN: 7191cf1cae9SAlexei Starovoitov case BPF_PROG_TYPE_LWT_OUT: 7201cf1cae9SAlexei Starovoitov case BPF_PROG_TYPE_LWT_XMIT: 7211cf1cae9SAlexei Starovoitov is_direct_pkt_access = true; 7221cf1cae9SAlexei Starovoitov break; 7231cf1cae9SAlexei Starovoitov default: 7241cf1cae9SAlexei Starovoitov break; 7251cf1cae9SAlexei Starovoitov } 7261cf1cae9SAlexei Starovoitov 727435b08ecSDaniel Borkmann sk = sk_alloc(net, AF_UNSPEC, GFP_USER, &bpf_dummy_proto, 1); 7282cb494a3SSong Liu if (!sk) { 7291cf1cae9SAlexei Starovoitov kfree(data); 730b0b9395dSStanislav Fomichev kfree(ctx); 7311cf1cae9SAlexei Starovoitov return -ENOMEM; 7321cf1cae9SAlexei Starovoitov } 7332cb494a3SSong Liu sock_init_data(NULL, sk); 7342cb494a3SSong Liu 7352cb494a3SSong Liu skb = build_skb(data, 0); 7362cb494a3SSong Liu if (!skb) { 7372cb494a3SSong Liu kfree(data); 738b0b9395dSStanislav Fomichev kfree(ctx); 739435b08ecSDaniel Borkmann sk_free(sk); 7402cb494a3SSong Liu return -ENOMEM; 7412cb494a3SSong Liu } 7422cb494a3SSong Liu skb->sk = sk; 7431cf1cae9SAlexei Starovoitov 744586f8525SDavid Miller skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN); 7451cf1cae9SAlexei Starovoitov __skb_put(skb, size); 74621594c44SDmitry Yakunin if (ctx && ctx->ifindex > 1) { 74721594c44SDmitry Yakunin dev = dev_get_by_index(net, ctx->ifindex); 74821594c44SDmitry Yakunin if (!dev) { 74921594c44SDmitry Yakunin ret = -ENODEV; 75021594c44SDmitry Yakunin goto out; 75121594c44SDmitry Yakunin } 75221594c44SDmitry Yakunin } 75321594c44SDmitry Yakunin skb->protocol = eth_type_trans(skb, dev); 7541cf1cae9SAlexei Starovoitov skb_reset_network_header(skb); 7551cf1cae9SAlexei Starovoitov 756fa5cb548SDmitry Yakunin switch (skb->protocol) { 757fa5cb548SDmitry Yakunin case htons(ETH_P_IP): 758fa5cb548SDmitry Yakunin sk->sk_family = AF_INET; 759fa5cb548SDmitry Yakunin if (sizeof(struct iphdr) <= skb_headlen(skb)) { 760fa5cb548SDmitry Yakunin sk->sk_rcv_saddr = ip_hdr(skb)->saddr; 761fa5cb548SDmitry Yakunin sk->sk_daddr = ip_hdr(skb)->daddr; 762fa5cb548SDmitry Yakunin } 763fa5cb548SDmitry Yakunin break; 764fa5cb548SDmitry Yakunin #if IS_ENABLED(CONFIG_IPV6) 765fa5cb548SDmitry Yakunin case htons(ETH_P_IPV6): 766fa5cb548SDmitry Yakunin sk->sk_family = AF_INET6; 767fa5cb548SDmitry Yakunin if (sizeof(struct ipv6hdr) <= skb_headlen(skb)) { 768fa5cb548SDmitry Yakunin sk->sk_v6_rcv_saddr = ipv6_hdr(skb)->saddr; 769fa5cb548SDmitry Yakunin sk->sk_v6_daddr = ipv6_hdr(skb)->daddr; 770fa5cb548SDmitry Yakunin } 771fa5cb548SDmitry Yakunin break; 772fa5cb548SDmitry Yakunin #endif 773fa5cb548SDmitry Yakunin default: 774fa5cb548SDmitry Yakunin break; 775fa5cb548SDmitry Yakunin } 776fa5cb548SDmitry Yakunin 7771cf1cae9SAlexei Starovoitov if (is_l2) 7786e6fddc7SDaniel Borkmann __skb_push(skb, hh_len); 7791cf1cae9SAlexei Starovoitov if (is_direct_pkt_access) 7806aaae2b6SDaniel Borkmann bpf_compute_data_pointers(skb); 781b0b9395dSStanislav Fomichev ret = convert___skb_to_skb(skb, ctx); 782b0b9395dSStanislav Fomichev if (ret) 783b0b9395dSStanislav Fomichev goto out; 784f23c4b39SBjörn Töpel ret = bpf_test_run(prog, skb, repeat, &retval, &duration, false); 785b0b9395dSStanislav Fomichev if (ret) 786b0b9395dSStanislav Fomichev goto out; 7876e6fddc7SDaniel Borkmann if (!is_l2) { 7886e6fddc7SDaniel Borkmann if (skb_headroom(skb) < hh_len) { 7896e6fddc7SDaniel Borkmann int nhead = HH_DATA_ALIGN(hh_len - skb_headroom(skb)); 7906e6fddc7SDaniel Borkmann 7916e6fddc7SDaniel Borkmann if (pskb_expand_head(skb, nhead, 0, GFP_USER)) { 792b0b9395dSStanislav Fomichev ret = -ENOMEM; 793b0b9395dSStanislav Fomichev goto out; 7946e6fddc7SDaniel Borkmann } 7956e6fddc7SDaniel Borkmann } 7966e6fddc7SDaniel Borkmann memset(__skb_push(skb, hh_len), 0, hh_len); 7976e6fddc7SDaniel Borkmann } 798b0b9395dSStanislav Fomichev convert_skb_to___skb(skb, ctx); 7996e6fddc7SDaniel Borkmann 8001cf1cae9SAlexei Starovoitov size = skb->len; 8011cf1cae9SAlexei Starovoitov /* bpf program can never convert linear skb to non-linear */ 8021cf1cae9SAlexei Starovoitov if (WARN_ON_ONCE(skb_is_nonlinear(skb))) 8031cf1cae9SAlexei Starovoitov size = skb_headlen(skb); 80478e52272SDavid Miller ret = bpf_test_finish(kattr, uattr, skb->data, size, retval, duration); 805b0b9395dSStanislav Fomichev if (!ret) 806b0b9395dSStanislav Fomichev ret = bpf_ctx_finish(kattr, uattr, ctx, 807b0b9395dSStanislav Fomichev sizeof(struct __sk_buff)); 808b0b9395dSStanislav Fomichev out: 80921594c44SDmitry Yakunin if (dev && dev != net->loopback_dev) 81021594c44SDmitry Yakunin dev_put(dev); 8111cf1cae9SAlexei Starovoitov kfree_skb(skb); 812435b08ecSDaniel Borkmann sk_free(sk); 813b0b9395dSStanislav Fomichev kfree(ctx); 8141cf1cae9SAlexei Starovoitov return ret; 8151cf1cae9SAlexei Starovoitov } 8161cf1cae9SAlexei Starovoitov 81747316f4aSZvi Effron static int xdp_convert_md_to_buff(struct xdp_md *xdp_md, struct xdp_buff *xdp) 81847316f4aSZvi Effron { 819ec94670fSZvi Effron unsigned int ingress_ifindex, rx_queue_index; 820ec94670fSZvi Effron struct netdev_rx_queue *rxqueue; 821ec94670fSZvi Effron struct net_device *device; 822ec94670fSZvi Effron 82347316f4aSZvi Effron if (!xdp_md) 82447316f4aSZvi Effron return 0; 82547316f4aSZvi Effron 82647316f4aSZvi Effron if (xdp_md->egress_ifindex != 0) 82747316f4aSZvi Effron return -EINVAL; 82847316f4aSZvi Effron 829ec94670fSZvi Effron ingress_ifindex = xdp_md->ingress_ifindex; 830ec94670fSZvi Effron rx_queue_index = xdp_md->rx_queue_index; 831ec94670fSZvi Effron 832ec94670fSZvi Effron if (!ingress_ifindex && rx_queue_index) 83347316f4aSZvi Effron return -EINVAL; 83447316f4aSZvi Effron 835ec94670fSZvi Effron if (ingress_ifindex) { 836ec94670fSZvi Effron device = dev_get_by_index(current->nsproxy->net_ns, 837ec94670fSZvi Effron ingress_ifindex); 838ec94670fSZvi Effron if (!device) 839ec94670fSZvi Effron return -ENODEV; 84047316f4aSZvi Effron 841ec94670fSZvi Effron if (rx_queue_index >= device->real_num_rx_queues) 842ec94670fSZvi Effron goto free_dev; 843ec94670fSZvi Effron 844ec94670fSZvi Effron rxqueue = __netif_get_rx_queue(device, rx_queue_index); 845ec94670fSZvi Effron 846ec94670fSZvi Effron if (!xdp_rxq_info_is_reg(&rxqueue->xdp_rxq)) 847ec94670fSZvi Effron goto free_dev; 848ec94670fSZvi Effron 849ec94670fSZvi Effron xdp->rxq = &rxqueue->xdp_rxq; 850ec94670fSZvi Effron /* The device is now tracked in the xdp->rxq for later 851ec94670fSZvi Effron * dev_put() 852ec94670fSZvi Effron */ 853ec94670fSZvi Effron } 854ec94670fSZvi Effron 855ec94670fSZvi Effron xdp->data = xdp->data_meta + xdp_md->data; 85647316f4aSZvi Effron return 0; 857ec94670fSZvi Effron 858ec94670fSZvi Effron free_dev: 859ec94670fSZvi Effron dev_put(device); 860ec94670fSZvi Effron return -EINVAL; 861ec94670fSZvi Effron } 862ec94670fSZvi Effron 863ec94670fSZvi Effron static void xdp_convert_buff_to_md(struct xdp_buff *xdp, struct xdp_md *xdp_md) 864ec94670fSZvi Effron { 865ec94670fSZvi Effron if (!xdp_md) 866ec94670fSZvi Effron return; 867ec94670fSZvi Effron 868ec94670fSZvi Effron xdp_md->data = xdp->data - xdp->data_meta; 869ec94670fSZvi Effron xdp_md->data_end = xdp->data_end - xdp->data_meta; 870ec94670fSZvi Effron 871ec94670fSZvi Effron if (xdp_md->ingress_ifindex) 872ec94670fSZvi Effron dev_put(xdp->rxq->dev); 87347316f4aSZvi Effron } 87447316f4aSZvi Effron 8751cf1cae9SAlexei Starovoitov int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr, 8761cf1cae9SAlexei Starovoitov union bpf_attr __user *uattr) 8771cf1cae9SAlexei Starovoitov { 878bc56c919SJesper Dangaard Brouer u32 tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 8791cf1cae9SAlexei Starovoitov u32 size = kattr->test.data_size_in; 880*1c194998SLorenzo Bianconi u32 headroom = XDP_PACKET_HEADROOM; 881*1c194998SLorenzo Bianconi u32 retval, duration, max_data_sz; 8821cf1cae9SAlexei Starovoitov u32 repeat = kattr->test.repeat; 88365073a67SDaniel Borkmann struct netdev_rx_queue *rxqueue; 884*1c194998SLorenzo Bianconi struct skb_shared_info *sinfo; 8851cf1cae9SAlexei Starovoitov struct xdp_buff xdp = {}; 886*1c194998SLorenzo Bianconi int i, ret = -EINVAL; 88747316f4aSZvi Effron struct xdp_md *ctx; 8881cf1cae9SAlexei Starovoitov void *data; 8891cf1cae9SAlexei Starovoitov 8905e21bb4eSXuan Zhuo if (prog->expected_attach_type == BPF_XDP_DEVMAP || 8915e21bb4eSXuan Zhuo prog->expected_attach_type == BPF_XDP_CPUMAP) 8925e21bb4eSXuan Zhuo return -EINVAL; 8936d4eb36dSAndrii Nakryiko 89447316f4aSZvi Effron ctx = bpf_ctx_init(kattr, sizeof(struct xdp_md)); 89547316f4aSZvi Effron if (IS_ERR(ctx)) 89647316f4aSZvi Effron return PTR_ERR(ctx); 89747316f4aSZvi Effron 89847316f4aSZvi Effron if (ctx) { 89947316f4aSZvi Effron /* There can't be user provided data before the meta data */ 90047316f4aSZvi Effron if (ctx->data_meta || ctx->data_end != size || 90147316f4aSZvi Effron ctx->data > ctx->data_end || 90247316f4aSZvi Effron unlikely(xdp_metalen_invalid(ctx->data))) 90347316f4aSZvi Effron goto free_ctx; 90447316f4aSZvi Effron /* Meta data is allocated from the headroom */ 90547316f4aSZvi Effron headroom -= ctx->data; 90647316f4aSZvi Effron } 907947e8b59SStanislav Fomichev 908bc56c919SJesper Dangaard Brouer max_data_sz = 4096 - headroom - tailroom; 909*1c194998SLorenzo Bianconi size = min_t(u32, size, max_data_sz); 910bc56c919SJesper Dangaard Brouer 911*1c194998SLorenzo Bianconi data = bpf_test_init(kattr, size, max_data_sz, headroom, tailroom); 91247316f4aSZvi Effron if (IS_ERR(data)) { 91347316f4aSZvi Effron ret = PTR_ERR(data); 91447316f4aSZvi Effron goto free_ctx; 91547316f4aSZvi Effron } 9161cf1cae9SAlexei Starovoitov 91765073a67SDaniel Borkmann rxqueue = __netif_get_rx_queue(current->nsproxy->net_ns->loopback_dev, 0); 918*1c194998SLorenzo Bianconi rxqueue->xdp_rxq.frag_size = headroom + max_data_sz + tailroom; 919*1c194998SLorenzo Bianconi xdp_init_buff(&xdp, rxqueue->xdp_rxq.frag_size, &rxqueue->xdp_rxq); 920be9df4afSLorenzo Bianconi xdp_prepare_buff(&xdp, data, headroom, size, true); 921*1c194998SLorenzo Bianconi sinfo = xdp_get_shared_info_from_buff(&xdp); 922be9df4afSLorenzo Bianconi 92347316f4aSZvi Effron ret = xdp_convert_md_to_buff(ctx, &xdp); 92447316f4aSZvi Effron if (ret) 92547316f4aSZvi Effron goto free_data; 92647316f4aSZvi Effron 927*1c194998SLorenzo Bianconi if (unlikely(kattr->test.data_size_in > size)) { 928*1c194998SLorenzo Bianconi void __user *data_in = u64_to_user_ptr(kattr->test.data_in); 929*1c194998SLorenzo Bianconi 930*1c194998SLorenzo Bianconi while (size < kattr->test.data_size_in) { 931*1c194998SLorenzo Bianconi struct page *page; 932*1c194998SLorenzo Bianconi skb_frag_t *frag; 933*1c194998SLorenzo Bianconi int data_len; 934*1c194998SLorenzo Bianconi 935*1c194998SLorenzo Bianconi page = alloc_page(GFP_KERNEL); 936*1c194998SLorenzo Bianconi if (!page) { 937*1c194998SLorenzo Bianconi ret = -ENOMEM; 938*1c194998SLorenzo Bianconi goto out; 939*1c194998SLorenzo Bianconi } 940*1c194998SLorenzo Bianconi 941*1c194998SLorenzo Bianconi frag = &sinfo->frags[sinfo->nr_frags++]; 942*1c194998SLorenzo Bianconi __skb_frag_set_page(frag, page); 943*1c194998SLorenzo Bianconi 944*1c194998SLorenzo Bianconi data_len = min_t(int, kattr->test.data_size_in - size, 945*1c194998SLorenzo Bianconi PAGE_SIZE); 946*1c194998SLorenzo Bianconi skb_frag_size_set(frag, data_len); 947*1c194998SLorenzo Bianconi 948*1c194998SLorenzo Bianconi if (copy_from_user(page_address(page), data_in + size, 949*1c194998SLorenzo Bianconi data_len)) { 950*1c194998SLorenzo Bianconi ret = -EFAULT; 951*1c194998SLorenzo Bianconi goto out; 952*1c194998SLorenzo Bianconi } 953*1c194998SLorenzo Bianconi sinfo->xdp_frags_size += data_len; 954*1c194998SLorenzo Bianconi size += data_len; 955*1c194998SLorenzo Bianconi } 956*1c194998SLorenzo Bianconi xdp_buff_set_frags_flag(&xdp); 957*1c194998SLorenzo Bianconi } 958*1c194998SLorenzo Bianconi 959de21d8bfSLorenz Bauer if (repeat > 1) 960f23c4b39SBjörn Töpel bpf_prog_change_xdp(NULL, prog); 961*1c194998SLorenzo Bianconi 962f23c4b39SBjörn Töpel ret = bpf_test_run(prog, &xdp, repeat, &retval, &duration, true); 963ec94670fSZvi Effron /* We convert the xdp_buff back to an xdp_md before checking the return 964ec94670fSZvi Effron * code so the reference count of any held netdevice will be decremented 965ec94670fSZvi Effron * even if the test run failed. 966ec94670fSZvi Effron */ 967ec94670fSZvi Effron xdp_convert_buff_to_md(&xdp, ctx); 968dcb40590SRoman Gushchin if (ret) 969dcb40590SRoman Gushchin goto out; 97047316f4aSZvi Effron 971*1c194998SLorenzo Bianconi size = xdp.data_end - xdp.data_meta + sinfo->xdp_frags_size; 97247316f4aSZvi Effron ret = bpf_test_finish(kattr, uattr, xdp.data_meta, size, retval, 97347316f4aSZvi Effron duration); 97447316f4aSZvi Effron if (!ret) 97547316f4aSZvi Effron ret = bpf_ctx_finish(kattr, uattr, ctx, 97647316f4aSZvi Effron sizeof(struct xdp_md)); 97747316f4aSZvi Effron 978dcb40590SRoman Gushchin out: 979de21d8bfSLorenz Bauer if (repeat > 1) 980f23c4b39SBjörn Töpel bpf_prog_change_xdp(prog, NULL); 98147316f4aSZvi Effron free_data: 982*1c194998SLorenzo Bianconi for (i = 0; i < sinfo->nr_frags; i++) 983*1c194998SLorenzo Bianconi __free_page(skb_frag_page(&sinfo->frags[i])); 9841cf1cae9SAlexei Starovoitov kfree(data); 98547316f4aSZvi Effron free_ctx: 98647316f4aSZvi Effron kfree(ctx); 9871cf1cae9SAlexei Starovoitov return ret; 9881cf1cae9SAlexei Starovoitov } 989b7a1848eSStanislav Fomichev 990b2ca4e1cSStanislav Fomichev static int verify_user_bpf_flow_keys(struct bpf_flow_keys *ctx) 991b2ca4e1cSStanislav Fomichev { 992b2ca4e1cSStanislav Fomichev /* make sure the fields we don't use are zeroed */ 993b2ca4e1cSStanislav Fomichev if (!range_is_zero(ctx, 0, offsetof(struct bpf_flow_keys, flags))) 994b2ca4e1cSStanislav Fomichev return -EINVAL; 995b2ca4e1cSStanislav Fomichev 996b2ca4e1cSStanislav Fomichev /* flags is allowed */ 997b2ca4e1cSStanislav Fomichev 998b590cb5fSStanislav Fomichev if (!range_is_zero(ctx, offsetofend(struct bpf_flow_keys, flags), 999b2ca4e1cSStanislav Fomichev sizeof(struct bpf_flow_keys))) 1000b2ca4e1cSStanislav Fomichev return -EINVAL; 1001b2ca4e1cSStanislav Fomichev 1002b2ca4e1cSStanislav Fomichev return 0; 1003b2ca4e1cSStanislav Fomichev } 1004b2ca4e1cSStanislav Fomichev 1005b7a1848eSStanislav Fomichev int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog, 1006b7a1848eSStanislav Fomichev const union bpf_attr *kattr, 1007b7a1848eSStanislav Fomichev union bpf_attr __user *uattr) 1008b7a1848eSStanislav Fomichev { 1009607b9cc9SLorenz Bauer struct bpf_test_timer t = { NO_PREEMPT }; 1010b7a1848eSStanislav Fomichev u32 size = kattr->test.data_size_in; 10117b8a1304SStanislav Fomichev struct bpf_flow_dissector ctx = {}; 1012b7a1848eSStanislav Fomichev u32 repeat = kattr->test.repeat; 1013b2ca4e1cSStanislav Fomichev struct bpf_flow_keys *user_ctx; 1014b7a1848eSStanislav Fomichev struct bpf_flow_keys flow_keys; 10157b8a1304SStanislav Fomichev const struct ethhdr *eth; 1016b2ca4e1cSStanislav Fomichev unsigned int flags = 0; 1017b7a1848eSStanislav Fomichev u32 retval, duration; 1018b7a1848eSStanislav Fomichev void *data; 1019b7a1848eSStanislav Fomichev int ret; 1020b7a1848eSStanislav Fomichev 1021b7a1848eSStanislav Fomichev if (prog->type != BPF_PROG_TYPE_FLOW_DISSECTOR) 1022b7a1848eSStanislav Fomichev return -EINVAL; 1023b7a1848eSStanislav Fomichev 10241b4d60ecSSong Liu if (kattr->test.flags || kattr->test.cpu) 10251b4d60ecSSong Liu return -EINVAL; 10261b4d60ecSSong Liu 10277b8a1304SStanislav Fomichev if (size < ETH_HLEN) 10287b8a1304SStanislav Fomichev return -EINVAL; 10297b8a1304SStanislav Fomichev 1030be3d72a2SLorenzo Bianconi data = bpf_test_init(kattr, kattr->test.data_size_in, size, 0, 0); 1031b7a1848eSStanislav Fomichev if (IS_ERR(data)) 1032b7a1848eSStanislav Fomichev return PTR_ERR(data); 1033b7a1848eSStanislav Fomichev 10347b8a1304SStanislav Fomichev eth = (struct ethhdr *)data; 1035b7a1848eSStanislav Fomichev 1036b7a1848eSStanislav Fomichev if (!repeat) 1037b7a1848eSStanislav Fomichev repeat = 1; 1038b7a1848eSStanislav Fomichev 1039b2ca4e1cSStanislav Fomichev user_ctx = bpf_ctx_init(kattr, sizeof(struct bpf_flow_keys)); 1040b2ca4e1cSStanislav Fomichev if (IS_ERR(user_ctx)) { 1041b2ca4e1cSStanislav Fomichev kfree(data); 1042b2ca4e1cSStanislav Fomichev return PTR_ERR(user_ctx); 1043b2ca4e1cSStanislav Fomichev } 1044b2ca4e1cSStanislav Fomichev if (user_ctx) { 1045b2ca4e1cSStanislav Fomichev ret = verify_user_bpf_flow_keys(user_ctx); 1046b2ca4e1cSStanislav Fomichev if (ret) 1047b2ca4e1cSStanislav Fomichev goto out; 1048b2ca4e1cSStanislav Fomichev flags = user_ctx->flags; 1049b2ca4e1cSStanislav Fomichev } 1050b2ca4e1cSStanislav Fomichev 10517b8a1304SStanislav Fomichev ctx.flow_keys = &flow_keys; 10527b8a1304SStanislav Fomichev ctx.data = data; 10537b8a1304SStanislav Fomichev ctx.data_end = (__u8 *)data + size; 10547b8a1304SStanislav Fomichev 1055607b9cc9SLorenz Bauer bpf_test_timer_enter(&t); 1056607b9cc9SLorenz Bauer do { 10577b8a1304SStanislav Fomichev retval = bpf_flow_dissect(prog, &ctx, eth->h_proto, ETH_HLEN, 1058b2ca4e1cSStanislav Fomichev size, flags); 1059607b9cc9SLorenz Bauer } while (bpf_test_timer_continue(&t, repeat, &ret, &duration)); 1060607b9cc9SLorenz Bauer bpf_test_timer_leave(&t); 10617b8a1304SStanislav Fomichev 1062607b9cc9SLorenz Bauer if (ret < 0) 1063a439184dSStanislav Fomichev goto out; 1064b7a1848eSStanislav Fomichev 1065b7a1848eSStanislav Fomichev ret = bpf_test_finish(kattr, uattr, &flow_keys, sizeof(flow_keys), 1066b7a1848eSStanislav Fomichev retval, duration); 1067b2ca4e1cSStanislav Fomichev if (!ret) 1068b2ca4e1cSStanislav Fomichev ret = bpf_ctx_finish(kattr, uattr, user_ctx, 1069b2ca4e1cSStanislav Fomichev sizeof(struct bpf_flow_keys)); 1070b7a1848eSStanislav Fomichev 1071a439184dSStanislav Fomichev out: 1072b2ca4e1cSStanislav Fomichev kfree(user_ctx); 10737b8a1304SStanislav Fomichev kfree(data); 1074b7a1848eSStanislav Fomichev return ret; 1075b7a1848eSStanislav Fomichev } 10767c32e8f8SLorenz Bauer 10777c32e8f8SLorenz Bauer int bpf_prog_test_run_sk_lookup(struct bpf_prog *prog, const union bpf_attr *kattr, 10787c32e8f8SLorenz Bauer union bpf_attr __user *uattr) 10797c32e8f8SLorenz Bauer { 10807c32e8f8SLorenz Bauer struct bpf_test_timer t = { NO_PREEMPT }; 10817c32e8f8SLorenz Bauer struct bpf_prog_array *progs = NULL; 10827c32e8f8SLorenz Bauer struct bpf_sk_lookup_kern ctx = {}; 10837c32e8f8SLorenz Bauer u32 repeat = kattr->test.repeat; 10847c32e8f8SLorenz Bauer struct bpf_sk_lookup *user_ctx; 10857c32e8f8SLorenz Bauer u32 retval, duration; 10867c32e8f8SLorenz Bauer int ret = -EINVAL; 10877c32e8f8SLorenz Bauer 10887c32e8f8SLorenz Bauer if (prog->type != BPF_PROG_TYPE_SK_LOOKUP) 10897c32e8f8SLorenz Bauer return -EINVAL; 10907c32e8f8SLorenz Bauer 10917c32e8f8SLorenz Bauer if (kattr->test.flags || kattr->test.cpu) 10927c32e8f8SLorenz Bauer return -EINVAL; 10937c32e8f8SLorenz Bauer 10947c32e8f8SLorenz Bauer if (kattr->test.data_in || kattr->test.data_size_in || kattr->test.data_out || 10957c32e8f8SLorenz Bauer kattr->test.data_size_out) 10967c32e8f8SLorenz Bauer return -EINVAL; 10977c32e8f8SLorenz Bauer 10987c32e8f8SLorenz Bauer if (!repeat) 10997c32e8f8SLorenz Bauer repeat = 1; 11007c32e8f8SLorenz Bauer 11017c32e8f8SLorenz Bauer user_ctx = bpf_ctx_init(kattr, sizeof(*user_ctx)); 11027c32e8f8SLorenz Bauer if (IS_ERR(user_ctx)) 11037c32e8f8SLorenz Bauer return PTR_ERR(user_ctx); 11047c32e8f8SLorenz Bauer 11057c32e8f8SLorenz Bauer if (!user_ctx) 11067c32e8f8SLorenz Bauer return -EINVAL; 11077c32e8f8SLorenz Bauer 11087c32e8f8SLorenz Bauer if (user_ctx->sk) 11097c32e8f8SLorenz Bauer goto out; 11107c32e8f8SLorenz Bauer 11117c32e8f8SLorenz Bauer if (!range_is_zero(user_ctx, offsetofend(typeof(*user_ctx), local_port), sizeof(*user_ctx))) 11127c32e8f8SLorenz Bauer goto out; 11137c32e8f8SLorenz Bauer 11147c32e8f8SLorenz Bauer if (user_ctx->local_port > U16_MAX || user_ctx->remote_port > U16_MAX) { 11157c32e8f8SLorenz Bauer ret = -ERANGE; 11167c32e8f8SLorenz Bauer goto out; 11177c32e8f8SLorenz Bauer } 11187c32e8f8SLorenz Bauer 11197c32e8f8SLorenz Bauer ctx.family = (u16)user_ctx->family; 11207c32e8f8SLorenz Bauer ctx.protocol = (u16)user_ctx->protocol; 11217c32e8f8SLorenz Bauer ctx.dport = (u16)user_ctx->local_port; 11227c32e8f8SLorenz Bauer ctx.sport = (__force __be16)user_ctx->remote_port; 11237c32e8f8SLorenz Bauer 11247c32e8f8SLorenz Bauer switch (ctx.family) { 11257c32e8f8SLorenz Bauer case AF_INET: 11267c32e8f8SLorenz Bauer ctx.v4.daddr = (__force __be32)user_ctx->local_ip4; 11277c32e8f8SLorenz Bauer ctx.v4.saddr = (__force __be32)user_ctx->remote_ip4; 11287c32e8f8SLorenz Bauer break; 11297c32e8f8SLorenz Bauer 11307c32e8f8SLorenz Bauer #if IS_ENABLED(CONFIG_IPV6) 11317c32e8f8SLorenz Bauer case AF_INET6: 11327c32e8f8SLorenz Bauer ctx.v6.daddr = (struct in6_addr *)user_ctx->local_ip6; 11337c32e8f8SLorenz Bauer ctx.v6.saddr = (struct in6_addr *)user_ctx->remote_ip6; 11347c32e8f8SLorenz Bauer break; 11357c32e8f8SLorenz Bauer #endif 11367c32e8f8SLorenz Bauer 11377c32e8f8SLorenz Bauer default: 11387c32e8f8SLorenz Bauer ret = -EAFNOSUPPORT; 11397c32e8f8SLorenz Bauer goto out; 11407c32e8f8SLorenz Bauer } 11417c32e8f8SLorenz Bauer 11427c32e8f8SLorenz Bauer progs = bpf_prog_array_alloc(1, GFP_KERNEL); 11437c32e8f8SLorenz Bauer if (!progs) { 11447c32e8f8SLorenz Bauer ret = -ENOMEM; 11457c32e8f8SLorenz Bauer goto out; 11467c32e8f8SLorenz Bauer } 11477c32e8f8SLorenz Bauer 11487c32e8f8SLorenz Bauer progs->items[0].prog = prog; 11497c32e8f8SLorenz Bauer 11507c32e8f8SLorenz Bauer bpf_test_timer_enter(&t); 11517c32e8f8SLorenz Bauer do { 11527c32e8f8SLorenz Bauer ctx.selected_sk = NULL; 1153fb7dd8bcSAndrii Nakryiko retval = BPF_PROG_SK_LOOKUP_RUN_ARRAY(progs, ctx, bpf_prog_run); 11547c32e8f8SLorenz Bauer } while (bpf_test_timer_continue(&t, repeat, &ret, &duration)); 11557c32e8f8SLorenz Bauer bpf_test_timer_leave(&t); 11567c32e8f8SLorenz Bauer 11577c32e8f8SLorenz Bauer if (ret < 0) 11587c32e8f8SLorenz Bauer goto out; 11597c32e8f8SLorenz Bauer 11607c32e8f8SLorenz Bauer user_ctx->cookie = 0; 11617c32e8f8SLorenz Bauer if (ctx.selected_sk) { 11627c32e8f8SLorenz Bauer if (ctx.selected_sk->sk_reuseport && !ctx.no_reuseport) { 11637c32e8f8SLorenz Bauer ret = -EOPNOTSUPP; 11647c32e8f8SLorenz Bauer goto out; 11657c32e8f8SLorenz Bauer } 11667c32e8f8SLorenz Bauer 11677c32e8f8SLorenz Bauer user_ctx->cookie = sock_gen_cookie(ctx.selected_sk); 11687c32e8f8SLorenz Bauer } 11697c32e8f8SLorenz Bauer 11707c32e8f8SLorenz Bauer ret = bpf_test_finish(kattr, uattr, NULL, 0, retval, duration); 11717c32e8f8SLorenz Bauer if (!ret) 11727c32e8f8SLorenz Bauer ret = bpf_ctx_finish(kattr, uattr, user_ctx, sizeof(*user_ctx)); 11737c32e8f8SLorenz Bauer 11747c32e8f8SLorenz Bauer out: 11757c32e8f8SLorenz Bauer bpf_prog_array_free(progs); 11767c32e8f8SLorenz Bauer kfree(user_ctx); 11777c32e8f8SLorenz Bauer return ret; 11787c32e8f8SLorenz Bauer } 117979a7f8bdSAlexei Starovoitov 118079a7f8bdSAlexei Starovoitov int bpf_prog_test_run_syscall(struct bpf_prog *prog, 118179a7f8bdSAlexei Starovoitov const union bpf_attr *kattr, 118279a7f8bdSAlexei Starovoitov union bpf_attr __user *uattr) 118379a7f8bdSAlexei Starovoitov { 118479a7f8bdSAlexei Starovoitov void __user *ctx_in = u64_to_user_ptr(kattr->test.ctx_in); 118579a7f8bdSAlexei Starovoitov __u32 ctx_size_in = kattr->test.ctx_size_in; 118679a7f8bdSAlexei Starovoitov void *ctx = NULL; 118779a7f8bdSAlexei Starovoitov u32 retval; 118879a7f8bdSAlexei Starovoitov int err = 0; 118979a7f8bdSAlexei Starovoitov 119079a7f8bdSAlexei Starovoitov /* doesn't support data_in/out, ctx_out, duration, or repeat or flags */ 119179a7f8bdSAlexei Starovoitov if (kattr->test.data_in || kattr->test.data_out || 119279a7f8bdSAlexei Starovoitov kattr->test.ctx_out || kattr->test.duration || 119379a7f8bdSAlexei Starovoitov kattr->test.repeat || kattr->test.flags) 119479a7f8bdSAlexei Starovoitov return -EINVAL; 119579a7f8bdSAlexei Starovoitov 119679a7f8bdSAlexei Starovoitov if (ctx_size_in < prog->aux->max_ctx_offset || 119779a7f8bdSAlexei Starovoitov ctx_size_in > U16_MAX) 119879a7f8bdSAlexei Starovoitov return -EINVAL; 119979a7f8bdSAlexei Starovoitov 120079a7f8bdSAlexei Starovoitov if (ctx_size_in) { 1201db5b6a46SQing Wang ctx = memdup_user(ctx_in, ctx_size_in); 1202db5b6a46SQing Wang if (IS_ERR(ctx)) 1203db5b6a46SQing Wang return PTR_ERR(ctx); 120479a7f8bdSAlexei Starovoitov } 120587b7b533SYonghong Song 120687b7b533SYonghong Song rcu_read_lock_trace(); 120779a7f8bdSAlexei Starovoitov retval = bpf_prog_run_pin_on_cpu(prog, ctx); 120887b7b533SYonghong Song rcu_read_unlock_trace(); 120979a7f8bdSAlexei Starovoitov 121079a7f8bdSAlexei Starovoitov if (copy_to_user(&uattr->test.retval, &retval, sizeof(u32))) { 121179a7f8bdSAlexei Starovoitov err = -EFAULT; 121279a7f8bdSAlexei Starovoitov goto out; 121379a7f8bdSAlexei Starovoitov } 121479a7f8bdSAlexei Starovoitov if (ctx_size_in) 121579a7f8bdSAlexei Starovoitov if (copy_to_user(ctx_in, ctx, ctx_size_in)) 121679a7f8bdSAlexei Starovoitov err = -EFAULT; 121779a7f8bdSAlexei Starovoitov out: 121879a7f8bdSAlexei Starovoitov kfree(ctx); 121979a7f8bdSAlexei Starovoitov return err; 122079a7f8bdSAlexei Starovoitov } 1221b202d844SKumar Kartikeya Dwivedi 1222b202d844SKumar Kartikeya Dwivedi static const struct btf_kfunc_id_set bpf_prog_test_kfunc_set = { 1223b202d844SKumar Kartikeya Dwivedi .owner = THIS_MODULE, 1224b202d844SKumar Kartikeya Dwivedi .check_set = &test_sk_check_kfunc_ids, 1225c1ff181fSKumar Kartikeya Dwivedi .acquire_set = &test_sk_acquire_kfunc_ids, 1226c1ff181fSKumar Kartikeya Dwivedi .release_set = &test_sk_release_kfunc_ids, 1227c1ff181fSKumar Kartikeya Dwivedi .ret_null_set = &test_sk_ret_null_kfunc_ids, 1228b202d844SKumar Kartikeya Dwivedi }; 1229b202d844SKumar Kartikeya Dwivedi 1230b202d844SKumar Kartikeya Dwivedi static int __init bpf_prog_test_run_init(void) 1231b202d844SKumar Kartikeya Dwivedi { 1232b202d844SKumar Kartikeya Dwivedi return register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, &bpf_prog_test_kfunc_set); 1233b202d844SKumar Kartikeya Dwivedi } 1234b202d844SKumar Kartikeya Dwivedi late_initcall(bpf_prog_test_run_init); 1235