Thomas Gleixner | 25763b3 | 2019-05-28 10:10:09 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 2 | /* Copyright (c) 2017 Facebook |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 3 | */ |
| 4 | #include <linux/bpf.h> |
| 5 | #include <linux/slab.h> |
| 6 | #include <linux/vmalloc.h> |
| 7 | #include <linux/etherdevice.h> |
| 8 | #include <linux/filter.h> |
| 9 | #include <linux/sched/signal.h> |
Martin KaFai Lau | 6ac99e8 | 2019-04-26 16:39:39 -0700 | [diff] [blame] | 10 | #include <net/bpf_sk_storage.h> |
Song Liu | 2cb494a | 2018-10-19 09:57:58 -0700 | [diff] [blame] | 11 | #include <net/sock.h> |
| 12 | #include <net/tcp.h> |
KP Singh | 3d08b6f | 2020-03-04 20:18:53 +0100 | [diff] [blame] | 13 | #include <linux/error-injection.h> |
Song Liu | 1b4d60e | 2020-09-25 13:54:29 -0700 | [diff] [blame] | 14 | #include <linux/smp.h> |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 15 | |
Matt Mullins | e950e84 | 2019-04-26 11:49:51 -0700 | [diff] [blame] | 16 | #define CREATE_TRACE_POINTS |
| 17 | #include <trace/events/bpf_test_run.h> |
| 18 | |
Stanislav Fomichev | df1a2cb | 2019-02-12 15:42:38 -0800 | [diff] [blame] | 19 | static int bpf_test_run(struct bpf_prog *prog, void *ctx, u32 repeat, |
Björn Töpel | f23c4b3 | 2019-12-13 18:51:10 +0100 | [diff] [blame] | 20 | u32 *retval, u32 *time, bool xdp) |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 21 | { |
Bo YU | 71b91a5 | 2019-03-08 01:45:51 -0500 | [diff] [blame] | 22 | struct bpf_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE] = { NULL }; |
Roman Gushchin | 8bad74f | 2018-09-28 14:45:36 +0000 | [diff] [blame] | 23 | enum bpf_cgroup_storage_type stype; |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 24 | u64 time_start, time_spent = 0; |
Stanislav Fomichev | df1a2cb | 2019-02-12 15:42:38 -0800 | [diff] [blame] | 25 | int ret = 0; |
Roman Gushchin | dcb4059 | 2018-12-01 10:39:44 -0800 | [diff] [blame] | 26 | u32 i; |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 27 | |
Roman Gushchin | 8bad74f | 2018-09-28 14:45:36 +0000 | [diff] [blame] | 28 | for_each_cgroup_storage_type(stype) { |
| 29 | storage[stype] = bpf_cgroup_storage_alloc(prog, stype); |
| 30 | if (IS_ERR(storage[stype])) { |
| 31 | storage[stype] = NULL; |
| 32 | for_each_cgroup_storage_type(stype) |
| 33 | bpf_cgroup_storage_free(storage[stype]); |
| 34 | return -ENOMEM; |
| 35 | } |
| 36 | } |
Roman Gushchin | f42ee09 | 2018-08-02 14:27:27 -0700 | [diff] [blame] | 37 | |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 38 | if (!repeat) |
| 39 | repeat = 1; |
Stanislav Fomichev | df1a2cb | 2019-02-12 15:42:38 -0800 | [diff] [blame] | 40 | |
| 41 | rcu_read_lock(); |
David Miller | 6eac779 | 2020-02-24 15:01:44 +0100 | [diff] [blame] | 42 | migrate_disable(); |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 43 | time_start = ktime_get_ns(); |
| 44 | for (i = 0; i < repeat; i++) { |
Stanislav Fomichev | df1a2cb | 2019-02-12 15:42:38 -0800 | [diff] [blame] | 45 | bpf_cgroup_storage_set(storage); |
Björn Töpel | f23c4b3 | 2019-12-13 18:51:10 +0100 | [diff] [blame] | 46 | |
| 47 | if (xdp) |
| 48 | *retval = bpf_prog_run_xdp(prog, ctx); |
| 49 | else |
| 50 | *retval = BPF_PROG_RUN(prog, ctx); |
Stanislav Fomichev | df1a2cb | 2019-02-12 15:42:38 -0800 | [diff] [blame] | 51 | |
| 52 | if (signal_pending(current)) { |
| 53 | ret = -EINTR; |
| 54 | break; |
| 55 | } |
| 56 | |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 57 | if (need_resched()) { |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 58 | time_spent += ktime_get_ns() - time_start; |
David Miller | 6eac779 | 2020-02-24 15:01:44 +0100 | [diff] [blame] | 59 | migrate_enable(); |
Stanislav Fomichev | df1a2cb | 2019-02-12 15:42:38 -0800 | [diff] [blame] | 60 | rcu_read_unlock(); |
| 61 | |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 62 | cond_resched(); |
Stanislav Fomichev | df1a2cb | 2019-02-12 15:42:38 -0800 | [diff] [blame] | 63 | |
| 64 | rcu_read_lock(); |
David Miller | 6eac779 | 2020-02-24 15:01:44 +0100 | [diff] [blame] | 65 | migrate_disable(); |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 66 | time_start = ktime_get_ns(); |
| 67 | } |
| 68 | } |
| 69 | time_spent += ktime_get_ns() - time_start; |
David Miller | 6eac779 | 2020-02-24 15:01:44 +0100 | [diff] [blame] | 70 | migrate_enable(); |
Stanislav Fomichev | df1a2cb | 2019-02-12 15:42:38 -0800 | [diff] [blame] | 71 | rcu_read_unlock(); |
| 72 | |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 73 | do_div(time_spent, repeat); |
| 74 | *time = time_spent > U32_MAX ? U32_MAX : (u32)time_spent; |
| 75 | |
Roman Gushchin | 8bad74f | 2018-09-28 14:45:36 +0000 | [diff] [blame] | 76 | for_each_cgroup_storage_type(stype) |
| 77 | bpf_cgroup_storage_free(storage[stype]); |
Roman Gushchin | f42ee09 | 2018-08-02 14:27:27 -0700 | [diff] [blame] | 78 | |
Stanislav Fomichev | df1a2cb | 2019-02-12 15:42:38 -0800 | [diff] [blame] | 79 | return ret; |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 80 | } |
| 81 | |
David Miller | 78e5227 | 2017-05-02 11:36:33 -0400 | [diff] [blame] | 82 | static int bpf_test_finish(const union bpf_attr *kattr, |
| 83 | union bpf_attr __user *uattr, const void *data, |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 84 | u32 size, u32 retval, u32 duration) |
| 85 | { |
David Miller | 78e5227 | 2017-05-02 11:36:33 -0400 | [diff] [blame] | 86 | void __user *data_out = u64_to_user_ptr(kattr->test.data_out); |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 87 | int err = -EFAULT; |
Lorenz Bauer | b5a36b1 | 2018-12-03 11:31:23 +0000 | [diff] [blame] | 88 | u32 copy_size = size; |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 89 | |
Lorenz Bauer | b5a36b1 | 2018-12-03 11:31:23 +0000 | [diff] [blame] | 90 | /* Clamp copy if the user has provided a size hint, but copy the full |
| 91 | * buffer if not to retain old behaviour. |
| 92 | */ |
| 93 | if (kattr->test.data_size_out && |
| 94 | copy_size > kattr->test.data_size_out) { |
| 95 | copy_size = kattr->test.data_size_out; |
| 96 | err = -ENOSPC; |
| 97 | } |
| 98 | |
| 99 | if (data_out && copy_to_user(data_out, data, copy_size)) |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 100 | goto out; |
| 101 | if (copy_to_user(&uattr->test.data_size_out, &size, sizeof(size))) |
| 102 | goto out; |
| 103 | if (copy_to_user(&uattr->test.retval, &retval, sizeof(retval))) |
| 104 | goto out; |
| 105 | if (copy_to_user(&uattr->test.duration, &duration, sizeof(duration))) |
| 106 | goto out; |
Lorenz Bauer | b5a36b1 | 2018-12-03 11:31:23 +0000 | [diff] [blame] | 107 | if (err != -ENOSPC) |
| 108 | err = 0; |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 109 | out: |
Matt Mullins | e950e84 | 2019-04-26 11:49:51 -0700 | [diff] [blame] | 110 | trace_bpf_test_finish(&err); |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 111 | return err; |
| 112 | } |
| 113 | |
Alexei Starovoitov | faeb2dc | 2019-11-14 10:57:08 -0800 | [diff] [blame] | 114 | /* Integer types of various sizes and pointer combinations cover variety of |
| 115 | * architecture dependent calling conventions. 7+ can be supported in the |
| 116 | * future. |
| 117 | */ |
Jean-Philippe Menil | e9ff9d5 | 2020-03-27 21:47:13 +0100 | [diff] [blame] | 118 | __diag_push(); |
| 119 | __diag_ignore(GCC, 8, "-Wmissing-prototypes", |
| 120 | "Global functions as their definitions will be in vmlinux BTF"); |
Alexei Starovoitov | faeb2dc | 2019-11-14 10:57:08 -0800 | [diff] [blame] | 121 | int noinline bpf_fentry_test1(int a) |
| 122 | { |
| 123 | return a + 1; |
| 124 | } |
| 125 | |
| 126 | int noinline bpf_fentry_test2(int a, u64 b) |
| 127 | { |
| 128 | return a + b; |
| 129 | } |
| 130 | |
| 131 | int noinline bpf_fentry_test3(char a, int b, u64 c) |
| 132 | { |
| 133 | return a + b + c; |
| 134 | } |
| 135 | |
| 136 | int noinline bpf_fentry_test4(void *a, char b, int c, u64 d) |
| 137 | { |
| 138 | return (long)a + b + c + d; |
| 139 | } |
| 140 | |
| 141 | int noinline bpf_fentry_test5(u64 a, void *b, short c, int d, u64 e) |
| 142 | { |
| 143 | return a + (long)b + c + d + e; |
| 144 | } |
| 145 | |
| 146 | int noinline bpf_fentry_test6(u64 a, void *b, short c, int d, void *e, u64 f) |
| 147 | { |
| 148 | return a + (long)b + c + d + (long)e + f; |
| 149 | } |
| 150 | |
Yonghong Song | d923021 | 2020-06-30 10:12:41 -0700 | [diff] [blame] | 151 | struct bpf_fentry_test_t { |
| 152 | struct bpf_fentry_test_t *a; |
| 153 | }; |
| 154 | |
| 155 | int noinline bpf_fentry_test7(struct bpf_fentry_test_t *arg) |
| 156 | { |
| 157 | return (long)arg; |
| 158 | } |
| 159 | |
| 160 | int noinline bpf_fentry_test8(struct bpf_fentry_test_t *arg) |
| 161 | { |
| 162 | return (long)arg->a; |
| 163 | } |
| 164 | |
KP Singh | 3d08b6f | 2020-03-04 20:18:53 +0100 | [diff] [blame] | 165 | int noinline bpf_modify_return_test(int a, int *b) |
| 166 | { |
| 167 | *b += 1; |
| 168 | return a + *b; |
| 169 | } |
Jean-Philippe Menil | e9ff9d5 | 2020-03-27 21:47:13 +0100 | [diff] [blame] | 170 | __diag_pop(); |
KP Singh | 3d08b6f | 2020-03-04 20:18:53 +0100 | [diff] [blame] | 171 | |
| 172 | ALLOW_ERROR_INJECTION(bpf_modify_return_test, ERRNO); |
| 173 | |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 174 | static void *bpf_test_init(const union bpf_attr *kattr, u32 size, |
| 175 | u32 headroom, u32 tailroom) |
| 176 | { |
| 177 | void __user *data_in = u64_to_user_ptr(kattr->test.data_in); |
Jesper Dangaard Brouer | d800bad | 2020-05-18 15:05:27 +0200 | [diff] [blame] | 178 | u32 user_size = kattr->test.data_size_in; |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 179 | void *data; |
| 180 | |
| 181 | if (size < ETH_HLEN || size > PAGE_SIZE - headroom - tailroom) |
| 182 | return ERR_PTR(-EINVAL); |
| 183 | |
Jesper Dangaard Brouer | d800bad | 2020-05-18 15:05:27 +0200 | [diff] [blame] | 184 | if (user_size > size) |
| 185 | return ERR_PTR(-EMSGSIZE); |
| 186 | |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 187 | data = kzalloc(size + headroom + tailroom, GFP_USER); |
| 188 | if (!data) |
| 189 | return ERR_PTR(-ENOMEM); |
| 190 | |
Jesper Dangaard Brouer | d800bad | 2020-05-18 15:05:27 +0200 | [diff] [blame] | 191 | if (copy_from_user(data + headroom, data_in, user_size)) { |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 192 | kfree(data); |
| 193 | return ERR_PTR(-EFAULT); |
| 194 | } |
KP Singh | da00d2f | 2020-03-04 20:18:52 +0100 | [diff] [blame] | 195 | |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 196 | return data; |
| 197 | } |
| 198 | |
KP Singh | da00d2f | 2020-03-04 20:18:52 +0100 | [diff] [blame] | 199 | int bpf_prog_test_run_tracing(struct bpf_prog *prog, |
| 200 | const union bpf_attr *kattr, |
| 201 | union bpf_attr __user *uattr) |
| 202 | { |
Yonghong Song | d923021 | 2020-06-30 10:12:41 -0700 | [diff] [blame] | 203 | struct bpf_fentry_test_t arg = {}; |
KP Singh | 3d08b6f | 2020-03-04 20:18:53 +0100 | [diff] [blame] | 204 | u16 side_effect = 0, ret = 0; |
| 205 | int b = 2, err = -EFAULT; |
| 206 | u32 retval = 0; |
KP Singh | da00d2f | 2020-03-04 20:18:52 +0100 | [diff] [blame] | 207 | |
Song Liu | 1b4d60e | 2020-09-25 13:54:29 -0700 | [diff] [blame] | 208 | if (kattr->test.flags || kattr->test.cpu) |
| 209 | return -EINVAL; |
| 210 | |
KP Singh | da00d2f | 2020-03-04 20:18:52 +0100 | [diff] [blame] | 211 | switch (prog->expected_attach_type) { |
| 212 | case BPF_TRACE_FENTRY: |
| 213 | case BPF_TRACE_FEXIT: |
| 214 | if (bpf_fentry_test1(1) != 2 || |
| 215 | bpf_fentry_test2(2, 3) != 5 || |
| 216 | bpf_fentry_test3(4, 5, 6) != 15 || |
| 217 | bpf_fentry_test4((void *)7, 8, 9, 10) != 34 || |
| 218 | bpf_fentry_test5(11, (void *)12, 13, 14, 15) != 65 || |
Yonghong Song | d923021 | 2020-06-30 10:12:41 -0700 | [diff] [blame] | 219 | bpf_fentry_test6(16, (void *)17, 18, 19, (void *)20, 21) != 111 || |
| 220 | bpf_fentry_test7((struct bpf_fentry_test_t *)0) != 0 || |
| 221 | bpf_fentry_test8(&arg) != 0) |
KP Singh | da00d2f | 2020-03-04 20:18:52 +0100 | [diff] [blame] | 222 | goto out; |
| 223 | break; |
KP Singh | 3d08b6f | 2020-03-04 20:18:53 +0100 | [diff] [blame] | 224 | case BPF_MODIFY_RETURN: |
| 225 | ret = bpf_modify_return_test(1, &b); |
| 226 | if (b != 2) |
| 227 | side_effect = 1; |
| 228 | break; |
KP Singh | da00d2f | 2020-03-04 20:18:52 +0100 | [diff] [blame] | 229 | default: |
| 230 | goto out; |
| 231 | } |
| 232 | |
KP Singh | 3d08b6f | 2020-03-04 20:18:53 +0100 | [diff] [blame] | 233 | retval = ((u32)side_effect << 16) | ret; |
| 234 | if (copy_to_user(&uattr->test.retval, &retval, sizeof(retval))) |
| 235 | goto out; |
| 236 | |
KP Singh | da00d2f | 2020-03-04 20:18:52 +0100 | [diff] [blame] | 237 | err = 0; |
| 238 | out: |
| 239 | trace_bpf_test_finish(&err); |
| 240 | return err; |
| 241 | } |
| 242 | |
Song Liu | 1b4d60e | 2020-09-25 13:54:29 -0700 | [diff] [blame] | 243 | struct bpf_raw_tp_test_run_info { |
| 244 | struct bpf_prog *prog; |
| 245 | void *ctx; |
| 246 | u32 retval; |
| 247 | }; |
| 248 | |
| 249 | static void |
| 250 | __bpf_prog_test_run_raw_tp(void *data) |
| 251 | { |
| 252 | struct bpf_raw_tp_test_run_info *info = data; |
| 253 | |
| 254 | rcu_read_lock(); |
Song Liu | 1b4d60e | 2020-09-25 13:54:29 -0700 | [diff] [blame] | 255 | info->retval = BPF_PROG_RUN(info->prog, info->ctx); |
Song Liu | 1b4d60e | 2020-09-25 13:54:29 -0700 | [diff] [blame] | 256 | rcu_read_unlock(); |
| 257 | } |
| 258 | |
| 259 | int bpf_prog_test_run_raw_tp(struct bpf_prog *prog, |
| 260 | const union bpf_attr *kattr, |
| 261 | union bpf_attr __user *uattr) |
| 262 | { |
| 263 | void __user *ctx_in = u64_to_user_ptr(kattr->test.ctx_in); |
| 264 | __u32 ctx_size_in = kattr->test.ctx_size_in; |
| 265 | struct bpf_raw_tp_test_run_info info; |
| 266 | int cpu = kattr->test.cpu, err = 0; |
Song Liu | 963ec27 | 2020-09-29 15:29:49 -0700 | [diff] [blame] | 267 | int current_cpu; |
Song Liu | 1b4d60e | 2020-09-25 13:54:29 -0700 | [diff] [blame] | 268 | |
| 269 | /* doesn't support data_in/out, ctx_out, duration, or repeat */ |
| 270 | if (kattr->test.data_in || kattr->test.data_out || |
| 271 | kattr->test.ctx_out || kattr->test.duration || |
| 272 | kattr->test.repeat) |
| 273 | return -EINVAL; |
| 274 | |
| 275 | if (ctx_size_in < prog->aux->max_ctx_offset) |
| 276 | return -EINVAL; |
| 277 | |
| 278 | if ((kattr->test.flags & BPF_F_TEST_RUN_ON_CPU) == 0 && cpu != 0) |
| 279 | return -EINVAL; |
| 280 | |
| 281 | if (ctx_size_in) { |
| 282 | info.ctx = kzalloc(ctx_size_in, GFP_USER); |
| 283 | if (!info.ctx) |
| 284 | return -ENOMEM; |
| 285 | if (copy_from_user(info.ctx, ctx_in, ctx_size_in)) { |
| 286 | err = -EFAULT; |
| 287 | goto out; |
| 288 | } |
| 289 | } else { |
| 290 | info.ctx = NULL; |
| 291 | } |
| 292 | |
| 293 | info.prog = prog; |
| 294 | |
Song Liu | 963ec27 | 2020-09-29 15:29:49 -0700 | [diff] [blame] | 295 | current_cpu = get_cpu(); |
Song Liu | 1b4d60e | 2020-09-25 13:54:29 -0700 | [diff] [blame] | 296 | if ((kattr->test.flags & BPF_F_TEST_RUN_ON_CPU) == 0 || |
Song Liu | 963ec27 | 2020-09-29 15:29:49 -0700 | [diff] [blame] | 297 | cpu == current_cpu) { |
Song Liu | 1b4d60e | 2020-09-25 13:54:29 -0700 | [diff] [blame] | 298 | __bpf_prog_test_run_raw_tp(&info); |
Song Liu | 963ec27 | 2020-09-29 15:29:49 -0700 | [diff] [blame] | 299 | } else if (cpu >= nr_cpu_ids || !cpu_online(cpu)) { |
Song Liu | 1b4d60e | 2020-09-25 13:54:29 -0700 | [diff] [blame] | 300 | /* smp_call_function_single() also checks cpu_online() |
| 301 | * after csd_lock(). However, since cpu is from user |
| 302 | * space, let's do an extra quick check to filter out |
| 303 | * invalid value before smp_call_function_single(). |
| 304 | */ |
Song Liu | 963ec27 | 2020-09-29 15:29:49 -0700 | [diff] [blame] | 305 | err = -ENXIO; |
| 306 | } else { |
Song Liu | 1b4d60e | 2020-09-25 13:54:29 -0700 | [diff] [blame] | 307 | err = smp_call_function_single(cpu, __bpf_prog_test_run_raw_tp, |
| 308 | &info, 1); |
Song Liu | 1b4d60e | 2020-09-25 13:54:29 -0700 | [diff] [blame] | 309 | } |
Song Liu | 963ec27 | 2020-09-29 15:29:49 -0700 | [diff] [blame] | 310 | put_cpu(); |
Song Liu | 1b4d60e | 2020-09-25 13:54:29 -0700 | [diff] [blame] | 311 | |
Song Liu | 963ec27 | 2020-09-29 15:29:49 -0700 | [diff] [blame] | 312 | if (!err && |
| 313 | copy_to_user(&uattr->test.retval, &info.retval, sizeof(u32))) |
Song Liu | 1b4d60e | 2020-09-25 13:54:29 -0700 | [diff] [blame] | 314 | err = -EFAULT; |
| 315 | |
| 316 | out: |
| 317 | kfree(info.ctx); |
| 318 | return err; |
| 319 | } |
| 320 | |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame] | 321 | static void *bpf_ctx_init(const union bpf_attr *kattr, u32 max_size) |
| 322 | { |
| 323 | void __user *data_in = u64_to_user_ptr(kattr->test.ctx_in); |
| 324 | void __user *data_out = u64_to_user_ptr(kattr->test.ctx_out); |
| 325 | u32 size = kattr->test.ctx_size_in; |
| 326 | void *data; |
| 327 | int err; |
| 328 | |
| 329 | if (!data_in && !data_out) |
| 330 | return NULL; |
| 331 | |
| 332 | data = kzalloc(max_size, GFP_USER); |
| 333 | if (!data) |
| 334 | return ERR_PTR(-ENOMEM); |
| 335 | |
| 336 | if (data_in) { |
| 337 | err = bpf_check_uarg_tail_zero(data_in, max_size, size); |
| 338 | if (err) { |
| 339 | kfree(data); |
| 340 | return ERR_PTR(err); |
| 341 | } |
| 342 | |
| 343 | size = min_t(u32, max_size, size); |
| 344 | if (copy_from_user(data, data_in, size)) { |
| 345 | kfree(data); |
| 346 | return ERR_PTR(-EFAULT); |
| 347 | } |
| 348 | } |
| 349 | return data; |
| 350 | } |
| 351 | |
| 352 | static int bpf_ctx_finish(const union bpf_attr *kattr, |
| 353 | union bpf_attr __user *uattr, const void *data, |
| 354 | u32 size) |
| 355 | { |
| 356 | void __user *data_out = u64_to_user_ptr(kattr->test.ctx_out); |
| 357 | int err = -EFAULT; |
| 358 | u32 copy_size = size; |
| 359 | |
| 360 | if (!data || !data_out) |
| 361 | return 0; |
| 362 | |
| 363 | if (copy_size > kattr->test.ctx_size_out) { |
| 364 | copy_size = kattr->test.ctx_size_out; |
| 365 | err = -ENOSPC; |
| 366 | } |
| 367 | |
| 368 | if (copy_to_user(data_out, data, copy_size)) |
| 369 | goto out; |
| 370 | if (copy_to_user(&uattr->test.ctx_size_out, &size, sizeof(size))) |
| 371 | goto out; |
| 372 | if (err != -ENOSPC) |
| 373 | err = 0; |
| 374 | out: |
| 375 | return err; |
| 376 | } |
| 377 | |
| 378 | /** |
| 379 | * range_is_zero - test whether buffer is initialized |
| 380 | * @buf: buffer to check |
| 381 | * @from: check from this position |
| 382 | * @to: check up until (excluding) this position |
| 383 | * |
| 384 | * This function returns true if the there is a non-zero byte |
| 385 | * in the buf in the range [from,to). |
| 386 | */ |
| 387 | static inline bool range_is_zero(void *buf, size_t from, size_t to) |
| 388 | { |
| 389 | return !memchr_inv((u8 *)buf + from, 0, to - from); |
| 390 | } |
| 391 | |
| 392 | static int convert___skb_to_skb(struct sk_buff *skb, struct __sk_buff *__skb) |
| 393 | { |
| 394 | struct qdisc_skb_cb *cb = (struct qdisc_skb_cb *)skb->cb; |
| 395 | |
| 396 | if (!__skb) |
| 397 | return 0; |
| 398 | |
| 399 | /* make sure the fields we don't use are zeroed */ |
Nikita V. Shirokov | 6de6c1f | 2019-12-18 12:57:47 -0800 | [diff] [blame] | 400 | if (!range_is_zero(__skb, 0, offsetof(struct __sk_buff, mark))) |
| 401 | return -EINVAL; |
| 402 | |
| 403 | /* mark is allowed */ |
| 404 | |
| 405 | if (!range_is_zero(__skb, offsetofend(struct __sk_buff, mark), |
| 406 | offsetof(struct __sk_buff, priority))) |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame] | 407 | return -EINVAL; |
| 408 | |
| 409 | /* priority is allowed */ |
| 410 | |
Stanislav Fomichev | b590cb5 | 2019-12-10 11:19:33 -0800 | [diff] [blame] | 411 | if (!range_is_zero(__skb, offsetofend(struct __sk_buff, priority), |
Dmitry Yakunin | 21594c4 | 2020-08-03 12:05:45 +0300 | [diff] [blame] | 412 | offsetof(struct __sk_buff, ifindex))) |
| 413 | return -EINVAL; |
| 414 | |
| 415 | /* ifindex is allowed */ |
| 416 | |
| 417 | if (!range_is_zero(__skb, offsetofend(struct __sk_buff, ifindex), |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame] | 418 | offsetof(struct __sk_buff, cb))) |
| 419 | return -EINVAL; |
| 420 | |
| 421 | /* cb is allowed */ |
| 422 | |
Stanislav Fomichev | b590cb5 | 2019-12-10 11:19:33 -0800 | [diff] [blame] | 423 | if (!range_is_zero(__skb, offsetofend(struct __sk_buff, cb), |
Stanislav Fomichev | ba94094 | 2019-10-15 11:31:24 -0700 | [diff] [blame] | 424 | offsetof(struct __sk_buff, tstamp))) |
| 425 | return -EINVAL; |
| 426 | |
| 427 | /* tstamp is allowed */ |
Stanislav Fomichev | 850a88c | 2019-12-13 14:30:27 -0800 | [diff] [blame] | 428 | /* wire_len is allowed */ |
| 429 | /* gso_segs is allowed */ |
Stanislav Fomichev | ba94094 | 2019-10-15 11:31:24 -0700 | [diff] [blame] | 430 | |
Stanislav Fomichev | 850a88c | 2019-12-13 14:30:27 -0800 | [diff] [blame] | 431 | if (!range_is_zero(__skb, offsetofend(struct __sk_buff, gso_segs), |
Willem de Bruijn | cf62089 | 2020-03-03 15:05:01 -0500 | [diff] [blame] | 432 | offsetof(struct __sk_buff, gso_size))) |
| 433 | return -EINVAL; |
| 434 | |
| 435 | /* gso_size is allowed */ |
| 436 | |
| 437 | if (!range_is_zero(__skb, offsetofend(struct __sk_buff, gso_size), |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame] | 438 | sizeof(struct __sk_buff))) |
| 439 | return -EINVAL; |
| 440 | |
Nikita V. Shirokov | 6de6c1f | 2019-12-18 12:57:47 -0800 | [diff] [blame] | 441 | skb->mark = __skb->mark; |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame] | 442 | skb->priority = __skb->priority; |
Stanislav Fomichev | ba94094 | 2019-10-15 11:31:24 -0700 | [diff] [blame] | 443 | skb->tstamp = __skb->tstamp; |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame] | 444 | memcpy(&cb->data, __skb->cb, QDISC_CB_PRIV_LEN); |
| 445 | |
Stanislav Fomichev | 850a88c | 2019-12-13 14:30:27 -0800 | [diff] [blame] | 446 | if (__skb->wire_len == 0) { |
| 447 | cb->pkt_len = skb->len; |
| 448 | } else { |
| 449 | if (__skb->wire_len < skb->len || |
| 450 | __skb->wire_len > GSO_MAX_SIZE) |
| 451 | return -EINVAL; |
| 452 | cb->pkt_len = __skb->wire_len; |
| 453 | } |
| 454 | |
| 455 | if (__skb->gso_segs > GSO_MAX_SEGS) |
| 456 | return -EINVAL; |
| 457 | skb_shinfo(skb)->gso_segs = __skb->gso_segs; |
Willem de Bruijn | cf62089 | 2020-03-03 15:05:01 -0500 | [diff] [blame] | 458 | skb_shinfo(skb)->gso_size = __skb->gso_size; |
Stanislav Fomichev | 850a88c | 2019-12-13 14:30:27 -0800 | [diff] [blame] | 459 | |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame] | 460 | return 0; |
| 461 | } |
| 462 | |
| 463 | static void convert_skb_to___skb(struct sk_buff *skb, struct __sk_buff *__skb) |
| 464 | { |
| 465 | struct qdisc_skb_cb *cb = (struct qdisc_skb_cb *)skb->cb; |
| 466 | |
| 467 | if (!__skb) |
| 468 | return; |
| 469 | |
Nikita V. Shirokov | 6de6c1f | 2019-12-18 12:57:47 -0800 | [diff] [blame] | 470 | __skb->mark = skb->mark; |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame] | 471 | __skb->priority = skb->priority; |
Dmitry Yakunin | 21594c4 | 2020-08-03 12:05:45 +0300 | [diff] [blame] | 472 | __skb->ifindex = skb->dev->ifindex; |
Stanislav Fomichev | ba94094 | 2019-10-15 11:31:24 -0700 | [diff] [blame] | 473 | __skb->tstamp = skb->tstamp; |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame] | 474 | memcpy(__skb->cb, &cb->data, QDISC_CB_PRIV_LEN); |
Stanislav Fomichev | 850a88c | 2019-12-13 14:30:27 -0800 | [diff] [blame] | 475 | __skb->wire_len = cb->pkt_len; |
| 476 | __skb->gso_segs = skb_shinfo(skb)->gso_segs; |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame] | 477 | } |
| 478 | |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 479 | int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr, |
| 480 | union bpf_attr __user *uattr) |
| 481 | { |
| 482 | bool is_l2 = false, is_direct_pkt_access = false; |
Dmitry Yakunin | 21594c4 | 2020-08-03 12:05:45 +0300 | [diff] [blame] | 483 | struct net *net = current->nsproxy->net_ns; |
| 484 | struct net_device *dev = net->loopback_dev; |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 485 | u32 size = kattr->test.data_size_in; |
| 486 | u32 repeat = kattr->test.repeat; |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame] | 487 | struct __sk_buff *ctx = NULL; |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 488 | u32 retval, duration; |
Daniel Borkmann | 6e6fddc | 2018-07-11 15:30:14 +0200 | [diff] [blame] | 489 | int hh_len = ETH_HLEN; |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 490 | struct sk_buff *skb; |
Song Liu | 2cb494a | 2018-10-19 09:57:58 -0700 | [diff] [blame] | 491 | struct sock *sk; |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 492 | void *data; |
| 493 | int ret; |
| 494 | |
Song Liu | 1b4d60e | 2020-09-25 13:54:29 -0700 | [diff] [blame] | 495 | if (kattr->test.flags || kattr->test.cpu) |
| 496 | return -EINVAL; |
| 497 | |
David Miller | 586f852 | 2017-05-02 11:36:45 -0400 | [diff] [blame] | 498 | data = bpf_test_init(kattr, size, NET_SKB_PAD + NET_IP_ALIGN, |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 499 | SKB_DATA_ALIGN(sizeof(struct skb_shared_info))); |
| 500 | if (IS_ERR(data)) |
| 501 | return PTR_ERR(data); |
| 502 | |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame] | 503 | ctx = bpf_ctx_init(kattr, sizeof(struct __sk_buff)); |
| 504 | if (IS_ERR(ctx)) { |
| 505 | kfree(data); |
| 506 | return PTR_ERR(ctx); |
| 507 | } |
| 508 | |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 509 | switch (prog->type) { |
| 510 | case BPF_PROG_TYPE_SCHED_CLS: |
| 511 | case BPF_PROG_TYPE_SCHED_ACT: |
| 512 | is_l2 = true; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 513 | fallthrough; |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 514 | case BPF_PROG_TYPE_LWT_IN: |
| 515 | case BPF_PROG_TYPE_LWT_OUT: |
| 516 | case BPF_PROG_TYPE_LWT_XMIT: |
| 517 | is_direct_pkt_access = true; |
| 518 | break; |
| 519 | default: |
| 520 | break; |
| 521 | } |
| 522 | |
Song Liu | 2cb494a | 2018-10-19 09:57:58 -0700 | [diff] [blame] | 523 | sk = kzalloc(sizeof(struct sock), GFP_USER); |
| 524 | if (!sk) { |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 525 | kfree(data); |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame] | 526 | kfree(ctx); |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 527 | return -ENOMEM; |
| 528 | } |
Dmitry Yakunin | 21594c4 | 2020-08-03 12:05:45 +0300 | [diff] [blame] | 529 | sock_net_set(sk, net); |
Song Liu | 2cb494a | 2018-10-19 09:57:58 -0700 | [diff] [blame] | 530 | sock_init_data(NULL, sk); |
| 531 | |
| 532 | skb = build_skb(data, 0); |
| 533 | if (!skb) { |
| 534 | kfree(data); |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame] | 535 | kfree(ctx); |
Song Liu | 2cb494a | 2018-10-19 09:57:58 -0700 | [diff] [blame] | 536 | kfree(sk); |
| 537 | return -ENOMEM; |
| 538 | } |
| 539 | skb->sk = sk; |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 540 | |
David Miller | 586f852 | 2017-05-02 11:36:45 -0400 | [diff] [blame] | 541 | skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN); |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 542 | __skb_put(skb, size); |
Dmitry Yakunin | 21594c4 | 2020-08-03 12:05:45 +0300 | [diff] [blame] | 543 | if (ctx && ctx->ifindex > 1) { |
| 544 | dev = dev_get_by_index(net, ctx->ifindex); |
| 545 | if (!dev) { |
| 546 | ret = -ENODEV; |
| 547 | goto out; |
| 548 | } |
| 549 | } |
| 550 | skb->protocol = eth_type_trans(skb, dev); |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 551 | skb_reset_network_header(skb); |
| 552 | |
Dmitry Yakunin | fa5cb54 | 2020-08-03 12:05:44 +0300 | [diff] [blame] | 553 | switch (skb->protocol) { |
| 554 | case htons(ETH_P_IP): |
| 555 | sk->sk_family = AF_INET; |
| 556 | if (sizeof(struct iphdr) <= skb_headlen(skb)) { |
| 557 | sk->sk_rcv_saddr = ip_hdr(skb)->saddr; |
| 558 | sk->sk_daddr = ip_hdr(skb)->daddr; |
| 559 | } |
| 560 | break; |
| 561 | #if IS_ENABLED(CONFIG_IPV6) |
| 562 | case htons(ETH_P_IPV6): |
| 563 | sk->sk_family = AF_INET6; |
| 564 | if (sizeof(struct ipv6hdr) <= skb_headlen(skb)) { |
| 565 | sk->sk_v6_rcv_saddr = ipv6_hdr(skb)->saddr; |
| 566 | sk->sk_v6_daddr = ipv6_hdr(skb)->daddr; |
| 567 | } |
| 568 | break; |
| 569 | #endif |
| 570 | default: |
| 571 | break; |
| 572 | } |
| 573 | |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 574 | if (is_l2) |
Daniel Borkmann | 6e6fddc | 2018-07-11 15:30:14 +0200 | [diff] [blame] | 575 | __skb_push(skb, hh_len); |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 576 | if (is_direct_pkt_access) |
Daniel Borkmann | 6aaae2b | 2017-09-25 02:25:50 +0200 | [diff] [blame] | 577 | bpf_compute_data_pointers(skb); |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame] | 578 | ret = convert___skb_to_skb(skb, ctx); |
| 579 | if (ret) |
| 580 | goto out; |
Björn Töpel | f23c4b3 | 2019-12-13 18:51:10 +0100 | [diff] [blame] | 581 | ret = bpf_test_run(prog, skb, repeat, &retval, &duration, false); |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame] | 582 | if (ret) |
| 583 | goto out; |
Daniel Borkmann | 6e6fddc | 2018-07-11 15:30:14 +0200 | [diff] [blame] | 584 | if (!is_l2) { |
| 585 | if (skb_headroom(skb) < hh_len) { |
| 586 | int nhead = HH_DATA_ALIGN(hh_len - skb_headroom(skb)); |
| 587 | |
| 588 | if (pskb_expand_head(skb, nhead, 0, GFP_USER)) { |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame] | 589 | ret = -ENOMEM; |
| 590 | goto out; |
Daniel Borkmann | 6e6fddc | 2018-07-11 15:30:14 +0200 | [diff] [blame] | 591 | } |
| 592 | } |
| 593 | memset(__skb_push(skb, hh_len), 0, hh_len); |
| 594 | } |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame] | 595 | convert_skb_to___skb(skb, ctx); |
Daniel Borkmann | 6e6fddc | 2018-07-11 15:30:14 +0200 | [diff] [blame] | 596 | |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 597 | size = skb->len; |
| 598 | /* bpf program can never convert linear skb to non-linear */ |
| 599 | if (WARN_ON_ONCE(skb_is_nonlinear(skb))) |
| 600 | size = skb_headlen(skb); |
David Miller | 78e5227 | 2017-05-02 11:36:33 -0400 | [diff] [blame] | 601 | ret = bpf_test_finish(kattr, uattr, skb->data, size, retval, duration); |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame] | 602 | if (!ret) |
| 603 | ret = bpf_ctx_finish(kattr, uattr, ctx, |
| 604 | sizeof(struct __sk_buff)); |
| 605 | out: |
Dmitry Yakunin | 21594c4 | 2020-08-03 12:05:45 +0300 | [diff] [blame] | 606 | if (dev && dev != net->loopback_dev) |
| 607 | dev_put(dev); |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 608 | kfree_skb(skb); |
Martin KaFai Lau | 6ac99e8 | 2019-04-26 16:39:39 -0700 | [diff] [blame] | 609 | bpf_sk_storage_free(sk); |
Song Liu | 2cb494a | 2018-10-19 09:57:58 -0700 | [diff] [blame] | 610 | kfree(sk); |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame] | 611 | kfree(ctx); |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 612 | return ret; |
| 613 | } |
| 614 | |
| 615 | int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr, |
| 616 | union bpf_attr __user *uattr) |
| 617 | { |
Jesper Dangaard Brouer | bc56c91 | 2020-05-14 12:51:35 +0200 | [diff] [blame] | 618 | u32 tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); |
| 619 | u32 headroom = XDP_PACKET_HEADROOM; |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 620 | u32 size = kattr->test.data_size_in; |
| 621 | u32 repeat = kattr->test.repeat; |
Daniel Borkmann | 65073a6 | 2018-01-31 12:58:56 +0100 | [diff] [blame] | 622 | struct netdev_rx_queue *rxqueue; |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 623 | struct xdp_buff xdp = {}; |
| 624 | u32 retval, duration; |
Jesper Dangaard Brouer | bc56c91 | 2020-05-14 12:51:35 +0200 | [diff] [blame] | 625 | u32 max_data_sz; |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 626 | void *data; |
| 627 | int ret; |
| 628 | |
Stanislav Fomichev | 947e8b5 | 2019-04-11 15:47:07 -0700 | [diff] [blame] | 629 | if (kattr->test.ctx_in || kattr->test.ctx_out) |
| 630 | return -EINVAL; |
| 631 | |
Jesper Dangaard Brouer | bc56c91 | 2020-05-14 12:51:35 +0200 | [diff] [blame] | 632 | /* XDP have extra tailroom as (most) drivers use full page */ |
| 633 | max_data_sz = 4096 - headroom - tailroom; |
Jesper Dangaard Brouer | bc56c91 | 2020-05-14 12:51:35 +0200 | [diff] [blame] | 634 | |
| 635 | data = bpf_test_init(kattr, max_data_sz, headroom, tailroom); |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 636 | if (IS_ERR(data)) |
| 637 | return PTR_ERR(data); |
| 638 | |
Daniel Borkmann | 65073a6 | 2018-01-31 12:58:56 +0100 | [diff] [blame] | 639 | rxqueue = __netif_get_rx_queue(current->nsproxy->net_ns->loopback_dev, 0); |
Lorenzo Bianconi | 43b5169 | 2020-12-22 22:09:28 +0100 | [diff] [blame] | 640 | xdp_init_buff(&xdp, headroom + max_data_sz + tailroom, |
| 641 | &rxqueue->xdp_rxq); |
Lorenzo Bianconi | be9df4a | 2020-12-22 22:09:29 +0100 | [diff] [blame^] | 642 | xdp_prepare_buff(&xdp, data, headroom, size, true); |
| 643 | |
Björn Töpel | f23c4b3 | 2019-12-13 18:51:10 +0100 | [diff] [blame] | 644 | bpf_prog_change_xdp(NULL, prog); |
| 645 | ret = bpf_test_run(prog, &xdp, repeat, &retval, &duration, true); |
Roman Gushchin | dcb4059 | 2018-12-01 10:39:44 -0800 | [diff] [blame] | 646 | if (ret) |
| 647 | goto out; |
Jesper Dangaard Brouer | bc56c91 | 2020-05-14 12:51:35 +0200 | [diff] [blame] | 648 | if (xdp.data != data + headroom || xdp.data_end != xdp.data + size) |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 649 | size = xdp.data_end - xdp.data; |
David Miller | 78e5227 | 2017-05-02 11:36:33 -0400 | [diff] [blame] | 650 | ret = bpf_test_finish(kattr, uattr, xdp.data, size, retval, duration); |
Roman Gushchin | dcb4059 | 2018-12-01 10:39:44 -0800 | [diff] [blame] | 651 | out: |
Björn Töpel | f23c4b3 | 2019-12-13 18:51:10 +0100 | [diff] [blame] | 652 | bpf_prog_change_xdp(prog, NULL); |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 653 | kfree(data); |
| 654 | return ret; |
| 655 | } |
Stanislav Fomichev | b7a1848 | 2019-01-28 08:53:54 -0800 | [diff] [blame] | 656 | |
Stanislav Fomichev | b2ca4e1 | 2019-07-25 15:52:27 -0700 | [diff] [blame] | 657 | static int verify_user_bpf_flow_keys(struct bpf_flow_keys *ctx) |
| 658 | { |
| 659 | /* make sure the fields we don't use are zeroed */ |
| 660 | if (!range_is_zero(ctx, 0, offsetof(struct bpf_flow_keys, flags))) |
| 661 | return -EINVAL; |
| 662 | |
| 663 | /* flags is allowed */ |
| 664 | |
Stanislav Fomichev | b590cb5 | 2019-12-10 11:19:33 -0800 | [diff] [blame] | 665 | if (!range_is_zero(ctx, offsetofend(struct bpf_flow_keys, flags), |
Stanislav Fomichev | b2ca4e1 | 2019-07-25 15:52:27 -0700 | [diff] [blame] | 666 | sizeof(struct bpf_flow_keys))) |
| 667 | return -EINVAL; |
| 668 | |
| 669 | return 0; |
| 670 | } |
| 671 | |
Stanislav Fomichev | b7a1848 | 2019-01-28 08:53:54 -0800 | [diff] [blame] | 672 | int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog, |
| 673 | const union bpf_attr *kattr, |
| 674 | union bpf_attr __user *uattr) |
| 675 | { |
| 676 | u32 size = kattr->test.data_size_in; |
Stanislav Fomichev | 7b8a130 | 2019-04-22 08:55:45 -0700 | [diff] [blame] | 677 | struct bpf_flow_dissector ctx = {}; |
Stanislav Fomichev | b7a1848 | 2019-01-28 08:53:54 -0800 | [diff] [blame] | 678 | u32 repeat = kattr->test.repeat; |
Stanislav Fomichev | b2ca4e1 | 2019-07-25 15:52:27 -0700 | [diff] [blame] | 679 | struct bpf_flow_keys *user_ctx; |
Stanislav Fomichev | b7a1848 | 2019-01-28 08:53:54 -0800 | [diff] [blame] | 680 | struct bpf_flow_keys flow_keys; |
| 681 | u64 time_start, time_spent = 0; |
Stanislav Fomichev | 7b8a130 | 2019-04-22 08:55:45 -0700 | [diff] [blame] | 682 | const struct ethhdr *eth; |
Stanislav Fomichev | b2ca4e1 | 2019-07-25 15:52:27 -0700 | [diff] [blame] | 683 | unsigned int flags = 0; |
Stanislav Fomichev | b7a1848 | 2019-01-28 08:53:54 -0800 | [diff] [blame] | 684 | u32 retval, duration; |
Stanislav Fomichev | b7a1848 | 2019-01-28 08:53:54 -0800 | [diff] [blame] | 685 | void *data; |
| 686 | int ret; |
| 687 | u32 i; |
| 688 | |
| 689 | if (prog->type != BPF_PROG_TYPE_FLOW_DISSECTOR) |
| 690 | return -EINVAL; |
| 691 | |
Song Liu | 1b4d60e | 2020-09-25 13:54:29 -0700 | [diff] [blame] | 692 | if (kattr->test.flags || kattr->test.cpu) |
| 693 | return -EINVAL; |
| 694 | |
Stanislav Fomichev | 7b8a130 | 2019-04-22 08:55:45 -0700 | [diff] [blame] | 695 | if (size < ETH_HLEN) |
| 696 | return -EINVAL; |
| 697 | |
| 698 | data = bpf_test_init(kattr, size, 0, 0); |
Stanislav Fomichev | b7a1848 | 2019-01-28 08:53:54 -0800 | [diff] [blame] | 699 | if (IS_ERR(data)) |
| 700 | return PTR_ERR(data); |
| 701 | |
Stanislav Fomichev | 7b8a130 | 2019-04-22 08:55:45 -0700 | [diff] [blame] | 702 | eth = (struct ethhdr *)data; |
Stanislav Fomichev | b7a1848 | 2019-01-28 08:53:54 -0800 | [diff] [blame] | 703 | |
Stanislav Fomichev | b7a1848 | 2019-01-28 08:53:54 -0800 | [diff] [blame] | 704 | if (!repeat) |
| 705 | repeat = 1; |
| 706 | |
Stanislav Fomichev | b2ca4e1 | 2019-07-25 15:52:27 -0700 | [diff] [blame] | 707 | user_ctx = bpf_ctx_init(kattr, sizeof(struct bpf_flow_keys)); |
| 708 | if (IS_ERR(user_ctx)) { |
| 709 | kfree(data); |
| 710 | return PTR_ERR(user_ctx); |
| 711 | } |
| 712 | if (user_ctx) { |
| 713 | ret = verify_user_bpf_flow_keys(user_ctx); |
| 714 | if (ret) |
| 715 | goto out; |
| 716 | flags = user_ctx->flags; |
| 717 | } |
| 718 | |
Stanislav Fomichev | 7b8a130 | 2019-04-22 08:55:45 -0700 | [diff] [blame] | 719 | ctx.flow_keys = &flow_keys; |
| 720 | ctx.data = data; |
| 721 | ctx.data_end = (__u8 *)data + size; |
| 722 | |
Stanislav Fomichev | a439184 | 2019-02-19 10:54:17 -0800 | [diff] [blame] | 723 | rcu_read_lock(); |
| 724 | preempt_disable(); |
Stanislav Fomichev | b7a1848 | 2019-01-28 08:53:54 -0800 | [diff] [blame] | 725 | time_start = ktime_get_ns(); |
| 726 | for (i = 0; i < repeat; i++) { |
Stanislav Fomichev | 7b8a130 | 2019-04-22 08:55:45 -0700 | [diff] [blame] | 727 | retval = bpf_flow_dissect(prog, &ctx, eth->h_proto, ETH_HLEN, |
Stanislav Fomichev | b2ca4e1 | 2019-07-25 15:52:27 -0700 | [diff] [blame] | 728 | size, flags); |
Stanislav Fomichev | 7b8a130 | 2019-04-22 08:55:45 -0700 | [diff] [blame] | 729 | |
Stanislav Fomichev | a439184 | 2019-02-19 10:54:17 -0800 | [diff] [blame] | 730 | if (signal_pending(current)) { |
| 731 | preempt_enable(); |
| 732 | rcu_read_unlock(); |
| 733 | |
| 734 | ret = -EINTR; |
| 735 | goto out; |
| 736 | } |
Stanislav Fomichev | b7a1848 | 2019-01-28 08:53:54 -0800 | [diff] [blame] | 737 | |
| 738 | if (need_resched()) { |
Stanislav Fomichev | b7a1848 | 2019-01-28 08:53:54 -0800 | [diff] [blame] | 739 | time_spent += ktime_get_ns() - time_start; |
Stanislav Fomichev | a439184 | 2019-02-19 10:54:17 -0800 | [diff] [blame] | 740 | preempt_enable(); |
| 741 | rcu_read_unlock(); |
| 742 | |
Stanislav Fomichev | b7a1848 | 2019-01-28 08:53:54 -0800 | [diff] [blame] | 743 | cond_resched(); |
Stanislav Fomichev | a439184 | 2019-02-19 10:54:17 -0800 | [diff] [blame] | 744 | |
| 745 | rcu_read_lock(); |
| 746 | preempt_disable(); |
Stanislav Fomichev | b7a1848 | 2019-01-28 08:53:54 -0800 | [diff] [blame] | 747 | time_start = ktime_get_ns(); |
| 748 | } |
| 749 | } |
| 750 | time_spent += ktime_get_ns() - time_start; |
Stanislav Fomichev | a439184 | 2019-02-19 10:54:17 -0800 | [diff] [blame] | 751 | preempt_enable(); |
| 752 | rcu_read_unlock(); |
| 753 | |
Stanislav Fomichev | b7a1848 | 2019-01-28 08:53:54 -0800 | [diff] [blame] | 754 | do_div(time_spent, repeat); |
| 755 | duration = time_spent > U32_MAX ? U32_MAX : (u32)time_spent; |
| 756 | |
| 757 | ret = bpf_test_finish(kattr, uattr, &flow_keys, sizeof(flow_keys), |
| 758 | retval, duration); |
Stanislav Fomichev | b2ca4e1 | 2019-07-25 15:52:27 -0700 | [diff] [blame] | 759 | if (!ret) |
| 760 | ret = bpf_ctx_finish(kattr, uattr, user_ctx, |
| 761 | sizeof(struct bpf_flow_keys)); |
Stanislav Fomichev | b7a1848 | 2019-01-28 08:53:54 -0800 | [diff] [blame] | 762 | |
Stanislav Fomichev | a439184 | 2019-02-19 10:54:17 -0800 | [diff] [blame] | 763 | out: |
Stanislav Fomichev | b2ca4e1 | 2019-07-25 15:52:27 -0700 | [diff] [blame] | 764 | kfree(user_ctx); |
Stanislav Fomichev | 7b8a130 | 2019-04-22 08:55:45 -0700 | [diff] [blame] | 765 | kfree(data); |
Stanislav Fomichev | b7a1848 | 2019-01-28 08:53:54 -0800 | [diff] [blame] | 766 | return ret; |
| 767 | } |