1 /*
2 * Linux Socket Filter - Kernel level socket filtering
3 *
4 * Based on the design of the Berkeley Packet Filter. The new
5 * internal format has been designed by PLUMgrid:
6 *
7 * Copyright (c) 2011 - 2014 PLUMgrid, http://plumgrid.com
8 *
9 * Authors:
10 *
11 * Jay Schulist <jschlst@samba.org>
12 * Alexei Starovoitov <ast@plumgrid.com>
13 * Daniel Borkmann <dborkman@redhat.com>
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version
18 * 2 of the License, or (at your option) any later version.
19 *
20 * Andi Kleen - Fix a few bad bugs and races.
21 * Kris Katterjohn - Added many additional checks in bpf_check_classic()
22 */
23
24 #include <linux/module.h>
25 #include <linux/types.h>
26 #include <linux/mm.h>
27 #include <linux/fcntl.h>
28 #include <linux/socket.h>
29 #include <linux/sock_diag.h>
30 #include <linux/in.h>
31 #include <linux/inet.h>
32 #include <linux/netdevice.h>
33 #include <linux/if_packet.h>
34 #include <linux/if_arp.h>
35 #include <linux/gfp.h>
36 #include <net/ip.h>
37 #include <net/protocol.h>
38 #include <net/netlink.h>
39 #include <linux/skbuff.h>
40 #include <net/sock.h>
41 #include <net/flow_dissector.h>
42 #include <linux/errno.h>
43 #include <linux/timer.h>
44 #include <linux/uaccess.h>
45 #include <asm/unaligned.h>
46 #include <linux/filter.h>
47 #include <linux/ratelimit.h>
48 #include <linux/seccomp.h>
49 #include <linux/if_vlan.h>
50 #include <linux/bpf.h>
51 #include <net/sch_generic.h>
52 #include <net/cls_cgroup.h>
53 #include <net/dst_metadata.h>
54 #include <net/dst.h>
55 #include <net/sock_reuseport.h>
56 #include <net/busy_poll.h>
57 #include <net/tcp.h>
58 #include <linux/bpf_trace.h>
59
60 /**
61 * sk_filter_trim_cap - run a packet through a socket filter
62 * @sk: sock associated with &sk_buff
63 * @skb: buffer to filter
64 * @cap: limit on how short the eBPF program may trim the packet
65 *
66 * Run the eBPF program and then cut skb->data to correct size returned by
67 * the program. If pkt_len is 0 we toss packet. If skb->len is smaller
68 * than pkt_len we keep whole skb->data. This is the socket level
69 * wrapper to BPF_PROG_RUN. It returns 0 if the packet should
70 * be accepted or -EPERM if the packet should be tossed.
71 *
72 */
sk_filter_trim_cap(struct sock * sk,struct sk_buff * skb,unsigned int cap)73 int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap)
74 {
75 int err;
76 struct sk_filter *filter;
77
78 /*
79 * If the skb was allocated from pfmemalloc reserves, only
80 * allow SOCK_MEMALLOC sockets to use it as this socket is
81 * helping free memory
82 */
83 if (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC)) {
84 NET_INC_STATS(sock_net(sk), LINUX_MIB_PFMEMALLOCDROP);
85 return -ENOMEM;
86 }
87 err = BPF_CGROUP_RUN_PROG_INET_INGRESS(sk, skb);
88 if (err)
89 return err;
90
91 err = security_sock_rcv_skb(sk, skb);
92 if (err)
93 return err;
94
95 rcu_read_lock();
96 filter = rcu_dereference(sk->sk_filter);
97 if (filter) {
98 struct sock *save_sk = skb->sk;
99 unsigned int pkt_len;
100
101 skb->sk = sk;
102 pkt_len = bpf_prog_run_save_cb(filter->prog, skb);
103 skb->sk = save_sk;
104 err = pkt_len ? pskb_trim(skb, max(cap, pkt_len)) : -EPERM;
105 }
106 rcu_read_unlock();
107
108 return err;
109 }
110 EXPORT_SYMBOL(sk_filter_trim_cap);
111
BPF_CALL_1(__skb_get_pay_offset,struct sk_buff *,skb)112 BPF_CALL_1(__skb_get_pay_offset, struct sk_buff *, skb)
113 {
114 return skb_get_poff(skb);
115 }
116
BPF_CALL_3(__skb_get_nlattr,struct sk_buff *,skb,u32,a,u32,x)117 BPF_CALL_3(__skb_get_nlattr, struct sk_buff *, skb, u32, a, u32, x)
118 {
119 struct nlattr *nla;
120
121 if (skb_is_nonlinear(skb))
122 return 0;
123
124 if (skb->len < sizeof(struct nlattr))
125 return 0;
126
127 if (a > skb->len - sizeof(struct nlattr))
128 return 0;
129
130 nla = nla_find((struct nlattr *) &skb->data[a], skb->len - a, x);
131 if (nla)
132 return (void *) nla - (void *) skb->data;
133
134 return 0;
135 }
136
BPF_CALL_3(__skb_get_nlattr_nest,struct sk_buff *,skb,u32,a,u32,x)137 BPF_CALL_3(__skb_get_nlattr_nest, struct sk_buff *, skb, u32, a, u32, x)
138 {
139 struct nlattr *nla;
140
141 if (skb_is_nonlinear(skb))
142 return 0;
143
144 if (skb->len < sizeof(struct nlattr))
145 return 0;
146
147 if (a > skb->len - sizeof(struct nlattr))
148 return 0;
149
150 nla = (struct nlattr *) &skb->data[a];
151 if (nla->nla_len > skb->len - a)
152 return 0;
153
154 nla = nla_find_nested(nla, x);
155 if (nla)
156 return (void *) nla - (void *) skb->data;
157
158 return 0;
159 }
160
BPF_CALL_0(__get_raw_cpu_id)161 BPF_CALL_0(__get_raw_cpu_id)
162 {
163 return raw_smp_processor_id();
164 }
165
166 static const struct bpf_func_proto bpf_get_raw_smp_processor_id_proto = {
167 .func = __get_raw_cpu_id,
168 .gpl_only = false,
169 .ret_type = RET_INTEGER,
170 };
171
convert_skb_access(int skb_field,int dst_reg,int src_reg,struct bpf_insn * insn_buf)172 static u32 convert_skb_access(int skb_field, int dst_reg, int src_reg,
173 struct bpf_insn *insn_buf)
174 {
175 struct bpf_insn *insn = insn_buf;
176
177 switch (skb_field) {
178 case SKF_AD_MARK:
179 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
180
181 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
182 offsetof(struct sk_buff, mark));
183 break;
184
185 case SKF_AD_PKTTYPE:
186 *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_TYPE_OFFSET());
187 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, PKT_TYPE_MAX);
188 #ifdef __BIG_ENDIAN_BITFIELD
189 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 5);
190 #endif
191 break;
192
193 case SKF_AD_QUEUE:
194 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, queue_mapping) != 2);
195
196 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
197 offsetof(struct sk_buff, queue_mapping));
198 break;
199
200 case SKF_AD_VLAN_TAG:
201 case SKF_AD_VLAN_TAG_PRESENT:
202 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_tci) != 2);
203 BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000);
204
205 /* dst_reg = *(u16 *) (src_reg + offsetof(vlan_tci)) */
206 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
207 offsetof(struct sk_buff, vlan_tci));
208 if (skb_field == SKF_AD_VLAN_TAG) {
209 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg,
210 ~VLAN_TAG_PRESENT);
211 } else {
212 /* dst_reg >>= 12 */
213 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 12);
214 /* dst_reg &= 1 */
215 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, 1);
216 }
217 break;
218 }
219
220 return insn - insn_buf;
221 }
222
convert_bpf_extensions(struct sock_filter * fp,struct bpf_insn ** insnp)223 static bool convert_bpf_extensions(struct sock_filter *fp,
224 struct bpf_insn **insnp)
225 {
226 struct bpf_insn *insn = *insnp;
227 u32 cnt;
228
229 switch (fp->k) {
230 case SKF_AD_OFF + SKF_AD_PROTOCOL:
231 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
232
233 /* A = *(u16 *) (CTX + offsetof(protocol)) */
234 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
235 offsetof(struct sk_buff, protocol));
236 /* A = ntohs(A) [emitting a nop or swap16] */
237 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
238 break;
239
240 case SKF_AD_OFF + SKF_AD_PKTTYPE:
241 cnt = convert_skb_access(SKF_AD_PKTTYPE, BPF_REG_A, BPF_REG_CTX, insn);
242 insn += cnt - 1;
243 break;
244
245 case SKF_AD_OFF + SKF_AD_IFINDEX:
246 case SKF_AD_OFF + SKF_AD_HATYPE:
247 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
248 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, type) != 2);
249
250 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
251 BPF_REG_TMP, BPF_REG_CTX,
252 offsetof(struct sk_buff, dev));
253 /* if (tmp != 0) goto pc + 1 */
254 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_TMP, 0, 1);
255 *insn++ = BPF_EXIT_INSN();
256 if (fp->k == SKF_AD_OFF + SKF_AD_IFINDEX)
257 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_TMP,
258 offsetof(struct net_device, ifindex));
259 else
260 *insn = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_TMP,
261 offsetof(struct net_device, type));
262 break;
263
264 case SKF_AD_OFF + SKF_AD_MARK:
265 cnt = convert_skb_access(SKF_AD_MARK, BPF_REG_A, BPF_REG_CTX, insn);
266 insn += cnt - 1;
267 break;
268
269 case SKF_AD_OFF + SKF_AD_RXHASH:
270 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
271
272 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX,
273 offsetof(struct sk_buff, hash));
274 break;
275
276 case SKF_AD_OFF + SKF_AD_QUEUE:
277 cnt = convert_skb_access(SKF_AD_QUEUE, BPF_REG_A, BPF_REG_CTX, insn);
278 insn += cnt - 1;
279 break;
280
281 case SKF_AD_OFF + SKF_AD_VLAN_TAG:
282 cnt = convert_skb_access(SKF_AD_VLAN_TAG,
283 BPF_REG_A, BPF_REG_CTX, insn);
284 insn += cnt - 1;
285 break;
286
287 case SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT:
288 cnt = convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
289 BPF_REG_A, BPF_REG_CTX, insn);
290 insn += cnt - 1;
291 break;
292
293 case SKF_AD_OFF + SKF_AD_VLAN_TPID:
294 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2);
295
296 /* A = *(u16 *) (CTX + offsetof(vlan_proto)) */
297 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
298 offsetof(struct sk_buff, vlan_proto));
299 /* A = ntohs(A) [emitting a nop or swap16] */
300 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
301 break;
302
303 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
304 case SKF_AD_OFF + SKF_AD_NLATTR:
305 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
306 case SKF_AD_OFF + SKF_AD_CPU:
307 case SKF_AD_OFF + SKF_AD_RANDOM:
308 /* arg1 = CTX */
309 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
310 /* arg2 = A */
311 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_A);
312 /* arg3 = X */
313 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_X);
314 /* Emit call(arg1=CTX, arg2=A, arg3=X) */
315 switch (fp->k) {
316 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
317 *insn = BPF_EMIT_CALL(__skb_get_pay_offset);
318 break;
319 case SKF_AD_OFF + SKF_AD_NLATTR:
320 *insn = BPF_EMIT_CALL(__skb_get_nlattr);
321 break;
322 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
323 *insn = BPF_EMIT_CALL(__skb_get_nlattr_nest);
324 break;
325 case SKF_AD_OFF + SKF_AD_CPU:
326 *insn = BPF_EMIT_CALL(__get_raw_cpu_id);
327 break;
328 case SKF_AD_OFF + SKF_AD_RANDOM:
329 *insn = BPF_EMIT_CALL(bpf_user_rnd_u32);
330 bpf_user_rnd_init_once();
331 break;
332 }
333 break;
334
335 case SKF_AD_OFF + SKF_AD_ALU_XOR_X:
336 /* A ^= X */
337 *insn = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_X);
338 break;
339
340 default:
341 /* This is just a dummy call to avoid letting the compiler
342 * evict __bpf_call_base() as an optimization. Placed here
343 * where no-one bothers.
344 */
345 BUG_ON(__bpf_call_base(0, 0, 0, 0, 0) != 0);
346 return false;
347 }
348
349 *insnp = insn;
350 return true;
351 }
352
353 /**
354 * bpf_convert_filter - convert filter program
355 * @prog: the user passed filter program
356 * @len: the length of the user passed filter program
357 * @new_prog: allocated 'struct bpf_prog' or NULL
358 * @new_len: pointer to store length of converted program
359 *
360 * Remap 'sock_filter' style classic BPF (cBPF) instruction set to 'bpf_insn'
361 * style extended BPF (eBPF).
362 * Conversion workflow:
363 *
364 * 1) First pass for calculating the new program length:
365 * bpf_convert_filter(old_prog, old_len, NULL, &new_len)
366 *
367 * 2) 2nd pass to remap in two passes: 1st pass finds new
368 * jump offsets, 2nd pass remapping:
369 * bpf_convert_filter(old_prog, old_len, new_prog, &new_len);
370 */
bpf_convert_filter(struct sock_filter * prog,int len,struct bpf_prog * new_prog,int * new_len)371 static int bpf_convert_filter(struct sock_filter *prog, int len,
372 struct bpf_prog *new_prog, int *new_len)
373 {
374 int new_flen = 0, pass = 0, target, i, stack_off;
375 struct bpf_insn *new_insn, *first_insn = NULL;
376 struct sock_filter *fp;
377 int *addrs = NULL;
378 u8 bpf_src;
379
380 BUILD_BUG_ON(BPF_MEMWORDS * sizeof(u32) > MAX_BPF_STACK);
381 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
382
383 if (len <= 0 || len > BPF_MAXINSNS)
384 return -EINVAL;
385
386 if (new_prog) {
387 first_insn = new_prog->insnsi;
388 addrs = kcalloc(len, sizeof(*addrs),
389 GFP_KERNEL | __GFP_NOWARN);
390 if (!addrs)
391 return -ENOMEM;
392 }
393
394 do_pass:
395 new_insn = first_insn;
396 fp = prog;
397
398 /* Classic BPF related prologue emission. */
399 if (new_prog) {
400 /* Classic BPF expects A and X to be reset first. These need
401 * to be guaranteed to be the first two instructions.
402 */
403 *new_insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
404 *new_insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_X, BPF_REG_X);
405
406 /* All programs must keep CTX in callee saved BPF_REG_CTX.
407 * In eBPF case it's done by the compiler, here we need to
408 * do this ourself. Initial CTX is present in BPF_REG_ARG1.
409 */
410 *new_insn++ = BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1);
411 } else {
412 new_insn += 3;
413 }
414
415 for (i = 0; i < len; fp++, i++) {
416 struct bpf_insn tmp_insns[6] = { };
417 struct bpf_insn *insn = tmp_insns;
418
419 if (addrs)
420 addrs[i] = new_insn - first_insn;
421
422 switch (fp->code) {
423 /* All arithmetic insns and skb loads map as-is. */
424 case BPF_ALU | BPF_ADD | BPF_X:
425 case BPF_ALU | BPF_ADD | BPF_K:
426 case BPF_ALU | BPF_SUB | BPF_X:
427 case BPF_ALU | BPF_SUB | BPF_K:
428 case BPF_ALU | BPF_AND | BPF_X:
429 case BPF_ALU | BPF_AND | BPF_K:
430 case BPF_ALU | BPF_OR | BPF_X:
431 case BPF_ALU | BPF_OR | BPF_K:
432 case BPF_ALU | BPF_LSH | BPF_X:
433 case BPF_ALU | BPF_LSH | BPF_K:
434 case BPF_ALU | BPF_RSH | BPF_X:
435 case BPF_ALU | BPF_RSH | BPF_K:
436 case BPF_ALU | BPF_XOR | BPF_X:
437 case BPF_ALU | BPF_XOR | BPF_K:
438 case BPF_ALU | BPF_MUL | BPF_X:
439 case BPF_ALU | BPF_MUL | BPF_K:
440 case BPF_ALU | BPF_DIV | BPF_X:
441 case BPF_ALU | BPF_DIV | BPF_K:
442 case BPF_ALU | BPF_MOD | BPF_X:
443 case BPF_ALU | BPF_MOD | BPF_K:
444 case BPF_ALU | BPF_NEG:
445 case BPF_LD | BPF_ABS | BPF_W:
446 case BPF_LD | BPF_ABS | BPF_H:
447 case BPF_LD | BPF_ABS | BPF_B:
448 case BPF_LD | BPF_IND | BPF_W:
449 case BPF_LD | BPF_IND | BPF_H:
450 case BPF_LD | BPF_IND | BPF_B:
451 /* Check for overloaded BPF extension and
452 * directly convert it if found, otherwise
453 * just move on with mapping.
454 */
455 if (BPF_CLASS(fp->code) == BPF_LD &&
456 BPF_MODE(fp->code) == BPF_ABS &&
457 convert_bpf_extensions(fp, &insn))
458 break;
459
460 if (fp->code == (BPF_ALU | BPF_DIV | BPF_X) ||
461 fp->code == (BPF_ALU | BPF_MOD | BPF_X))
462 *insn++ = BPF_MOV32_REG(BPF_REG_X, BPF_REG_X);
463
464 *insn = BPF_RAW_INSN(fp->code, BPF_REG_A, BPF_REG_X, 0, fp->k);
465 break;
466
467 /* Jump transformation cannot use BPF block macros
468 * everywhere as offset calculation and target updates
469 * require a bit more work than the rest, i.e. jump
470 * opcodes map as-is, but offsets need adjustment.
471 */
472
473 #define BPF_EMIT_JMP \
474 do { \
475 if (target >= len || target < 0) \
476 goto err; \
477 insn->off = addrs ? addrs[target] - addrs[i] - 1 : 0; \
478 /* Adjust pc relative offset for 2nd or 3rd insn. */ \
479 insn->off -= insn - tmp_insns; \
480 } while (0)
481
482 case BPF_JMP | BPF_JA:
483 target = i + fp->k + 1;
484 insn->code = fp->code;
485 BPF_EMIT_JMP;
486 break;
487
488 case BPF_JMP | BPF_JEQ | BPF_K:
489 case BPF_JMP | BPF_JEQ | BPF_X:
490 case BPF_JMP | BPF_JSET | BPF_K:
491 case BPF_JMP | BPF_JSET | BPF_X:
492 case BPF_JMP | BPF_JGT | BPF_K:
493 case BPF_JMP | BPF_JGT | BPF_X:
494 case BPF_JMP | BPF_JGE | BPF_K:
495 case BPF_JMP | BPF_JGE | BPF_X:
496 if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) {
497 /* BPF immediates are signed, zero extend
498 * immediate into tmp register and use it
499 * in compare insn.
500 */
501 *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k);
502
503 insn->dst_reg = BPF_REG_A;
504 insn->src_reg = BPF_REG_TMP;
505 bpf_src = BPF_X;
506 } else {
507 insn->dst_reg = BPF_REG_A;
508 insn->imm = fp->k;
509 bpf_src = BPF_SRC(fp->code);
510 insn->src_reg = bpf_src == BPF_X ? BPF_REG_X : 0;
511 }
512
513 /* Common case where 'jump_false' is next insn. */
514 if (fp->jf == 0) {
515 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
516 target = i + fp->jt + 1;
517 BPF_EMIT_JMP;
518 break;
519 }
520
521 /* Convert some jumps when 'jump_true' is next insn. */
522 if (fp->jt == 0) {
523 switch (BPF_OP(fp->code)) {
524 case BPF_JEQ:
525 insn->code = BPF_JMP | BPF_JNE | bpf_src;
526 break;
527 case BPF_JGT:
528 insn->code = BPF_JMP | BPF_JLE | bpf_src;
529 break;
530 case BPF_JGE:
531 insn->code = BPF_JMP | BPF_JLT | bpf_src;
532 break;
533 default:
534 goto jmp_rest;
535 }
536
537 target = i + fp->jf + 1;
538 BPF_EMIT_JMP;
539 break;
540 }
541 jmp_rest:
542 /* Other jumps are mapped into two insns: Jxx and JA. */
543 target = i + fp->jt + 1;
544 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
545 BPF_EMIT_JMP;
546 insn++;
547
548 insn->code = BPF_JMP | BPF_JA;
549 target = i + fp->jf + 1;
550 BPF_EMIT_JMP;
551 break;
552
553 /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */
554 case BPF_LDX | BPF_MSH | BPF_B:
555 /* tmp = A */
556 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_A);
557 /* A = BPF_R0 = *(u8 *) (skb->data + K) */
558 *insn++ = BPF_LD_ABS(BPF_B, fp->k);
559 /* A &= 0xf */
560 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf);
561 /* A <<= 2 */
562 *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2);
563 /* X = A */
564 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
565 /* A = tmp */
566 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP);
567 break;
568
569 /* RET_K is remaped into 2 insns. RET_A case doesn't need an
570 * extra mov as BPF_REG_0 is already mapped into BPF_REG_A.
571 */
572 case BPF_RET | BPF_A:
573 case BPF_RET | BPF_K:
574 if (BPF_RVAL(fp->code) == BPF_K)
575 *insn++ = BPF_MOV32_RAW(BPF_K, BPF_REG_0,
576 0, fp->k);
577 *insn = BPF_EXIT_INSN();
578 break;
579
580 /* Store to stack. */
581 case BPF_ST:
582 case BPF_STX:
583 stack_off = fp->k * 4 + 4;
584 *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) ==
585 BPF_ST ? BPF_REG_A : BPF_REG_X,
586 -stack_off);
587 /* check_load_and_stores() verifies that classic BPF can
588 * load from stack only after write, so tracking
589 * stack_depth for ST|STX insns is enough
590 */
591 if (new_prog && new_prog->aux->stack_depth < stack_off)
592 new_prog->aux->stack_depth = stack_off;
593 break;
594
595 /* Load from stack. */
596 case BPF_LD | BPF_MEM:
597 case BPF_LDX | BPF_MEM:
598 stack_off = fp->k * 4 + 4;
599 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
600 BPF_REG_A : BPF_REG_X, BPF_REG_FP,
601 -stack_off);
602 break;
603
604 /* A = K or X = K */
605 case BPF_LD | BPF_IMM:
606 case BPF_LDX | BPF_IMM:
607 *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ?
608 BPF_REG_A : BPF_REG_X, fp->k);
609 break;
610
611 /* X = A */
612 case BPF_MISC | BPF_TAX:
613 *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
614 break;
615
616 /* A = X */
617 case BPF_MISC | BPF_TXA:
618 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X);
619 break;
620
621 /* A = skb->len or X = skb->len */
622 case BPF_LD | BPF_W | BPF_LEN:
623 case BPF_LDX | BPF_W | BPF_LEN:
624 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
625 BPF_REG_A : BPF_REG_X, BPF_REG_CTX,
626 offsetof(struct sk_buff, len));
627 break;
628
629 /* Access seccomp_data fields. */
630 case BPF_LDX | BPF_ABS | BPF_W:
631 /* A = *(u32 *) (ctx + K) */
632 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k);
633 break;
634
635 /* Unknown instruction. */
636 default:
637 goto err;
638 }
639
640 insn++;
641 if (new_prog)
642 memcpy(new_insn, tmp_insns,
643 sizeof(*insn) * (insn - tmp_insns));
644 new_insn += insn - tmp_insns;
645 }
646
647 if (!new_prog) {
648 /* Only calculating new length. */
649 *new_len = new_insn - first_insn;
650 return 0;
651 }
652
653 pass++;
654 if (new_flen != new_insn - first_insn) {
655 new_flen = new_insn - first_insn;
656 if (pass > 2)
657 goto err;
658 goto do_pass;
659 }
660
661 kfree(addrs);
662 BUG_ON(*new_len != new_flen);
663 return 0;
664 err:
665 kfree(addrs);
666 return -EINVAL;
667 }
668
669 /* Security:
670 *
671 * As we dont want to clear mem[] array for each packet going through
672 * __bpf_prog_run(), we check that filter loaded by user never try to read
673 * a cell if not previously written, and we check all branches to be sure
674 * a malicious user doesn't try to abuse us.
675 */
check_load_and_stores(const struct sock_filter * filter,int flen)676 static int check_load_and_stores(const struct sock_filter *filter, int flen)
677 {
678 u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */
679 int pc, ret = 0;
680
681 BUILD_BUG_ON(BPF_MEMWORDS > 16);
682
683 masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL);
684 if (!masks)
685 return -ENOMEM;
686
687 memset(masks, 0xff, flen * sizeof(*masks));
688
689 for (pc = 0; pc < flen; pc++) {
690 memvalid &= masks[pc];
691
692 switch (filter[pc].code) {
693 case BPF_ST:
694 case BPF_STX:
695 memvalid |= (1 << filter[pc].k);
696 break;
697 case BPF_LD | BPF_MEM:
698 case BPF_LDX | BPF_MEM:
699 if (!(memvalid & (1 << filter[pc].k))) {
700 ret = -EINVAL;
701 goto error;
702 }
703 break;
704 case BPF_JMP | BPF_JA:
705 /* A jump must set masks on target */
706 masks[pc + 1 + filter[pc].k] &= memvalid;
707 memvalid = ~0;
708 break;
709 case BPF_JMP | BPF_JEQ | BPF_K:
710 case BPF_JMP | BPF_JEQ | BPF_X:
711 case BPF_JMP | BPF_JGE | BPF_K:
712 case BPF_JMP | BPF_JGE | BPF_X:
713 case BPF_JMP | BPF_JGT | BPF_K:
714 case BPF_JMP | BPF_JGT | BPF_X:
715 case BPF_JMP | BPF_JSET | BPF_K:
716 case BPF_JMP | BPF_JSET | BPF_X:
717 /* A jump must set masks on targets */
718 masks[pc + 1 + filter[pc].jt] &= memvalid;
719 masks[pc + 1 + filter[pc].jf] &= memvalid;
720 memvalid = ~0;
721 break;
722 }
723 }
724 error:
725 kfree(masks);
726 return ret;
727 }
728
chk_code_allowed(u16 code_to_probe)729 static bool chk_code_allowed(u16 code_to_probe)
730 {
731 static const bool codes[] = {
732 /* 32 bit ALU operations */
733 [BPF_ALU | BPF_ADD | BPF_K] = true,
734 [BPF_ALU | BPF_ADD | BPF_X] = true,
735 [BPF_ALU | BPF_SUB | BPF_K] = true,
736 [BPF_ALU | BPF_SUB | BPF_X] = true,
737 [BPF_ALU | BPF_MUL | BPF_K] = true,
738 [BPF_ALU | BPF_MUL | BPF_X] = true,
739 [BPF_ALU | BPF_DIV | BPF_K] = true,
740 [BPF_ALU | BPF_DIV | BPF_X] = true,
741 [BPF_ALU | BPF_MOD | BPF_K] = true,
742 [BPF_ALU | BPF_MOD | BPF_X] = true,
743 [BPF_ALU | BPF_AND | BPF_K] = true,
744 [BPF_ALU | BPF_AND | BPF_X] = true,
745 [BPF_ALU | BPF_OR | BPF_K] = true,
746 [BPF_ALU | BPF_OR | BPF_X] = true,
747 [BPF_ALU | BPF_XOR | BPF_K] = true,
748 [BPF_ALU | BPF_XOR | BPF_X] = true,
749 [BPF_ALU | BPF_LSH | BPF_K] = true,
750 [BPF_ALU | BPF_LSH | BPF_X] = true,
751 [BPF_ALU | BPF_RSH | BPF_K] = true,
752 [BPF_ALU | BPF_RSH | BPF_X] = true,
753 [BPF_ALU | BPF_NEG] = true,
754 /* Load instructions */
755 [BPF_LD | BPF_W | BPF_ABS] = true,
756 [BPF_LD | BPF_H | BPF_ABS] = true,
757 [BPF_LD | BPF_B | BPF_ABS] = true,
758 [BPF_LD | BPF_W | BPF_LEN] = true,
759 [BPF_LD | BPF_W | BPF_IND] = true,
760 [BPF_LD | BPF_H | BPF_IND] = true,
761 [BPF_LD | BPF_B | BPF_IND] = true,
762 [BPF_LD | BPF_IMM] = true,
763 [BPF_LD | BPF_MEM] = true,
764 [BPF_LDX | BPF_W | BPF_LEN] = true,
765 [BPF_LDX | BPF_B | BPF_MSH] = true,
766 [BPF_LDX | BPF_IMM] = true,
767 [BPF_LDX | BPF_MEM] = true,
768 /* Store instructions */
769 [BPF_ST] = true,
770 [BPF_STX] = true,
771 /* Misc instructions */
772 [BPF_MISC | BPF_TAX] = true,
773 [BPF_MISC | BPF_TXA] = true,
774 /* Return instructions */
775 [BPF_RET | BPF_K] = true,
776 [BPF_RET | BPF_A] = true,
777 /* Jump instructions */
778 [BPF_JMP | BPF_JA] = true,
779 [BPF_JMP | BPF_JEQ | BPF_K] = true,
780 [BPF_JMP | BPF_JEQ | BPF_X] = true,
781 [BPF_JMP | BPF_JGE | BPF_K] = true,
782 [BPF_JMP | BPF_JGE | BPF_X] = true,
783 [BPF_JMP | BPF_JGT | BPF_K] = true,
784 [BPF_JMP | BPF_JGT | BPF_X] = true,
785 [BPF_JMP | BPF_JSET | BPF_K] = true,
786 [BPF_JMP | BPF_JSET | BPF_X] = true,
787 };
788
789 if (code_to_probe >= ARRAY_SIZE(codes))
790 return false;
791
792 return codes[code_to_probe];
793 }
794
bpf_check_basics_ok(const struct sock_filter * filter,unsigned int flen)795 static bool bpf_check_basics_ok(const struct sock_filter *filter,
796 unsigned int flen)
797 {
798 if (filter == NULL)
799 return false;
800 if (flen == 0 || flen > BPF_MAXINSNS)
801 return false;
802
803 return true;
804 }
805
806 /**
807 * bpf_check_classic - verify socket filter code
808 * @filter: filter to verify
809 * @flen: length of filter
810 *
811 * Check the user's filter code. If we let some ugly
812 * filter code slip through kaboom! The filter must contain
813 * no references or jumps that are out of range, no illegal
814 * instructions, and must end with a RET instruction.
815 *
816 * All jumps are forward as they are not signed.
817 *
818 * Returns 0 if the rule set is legal or -EINVAL if not.
819 */
bpf_check_classic(const struct sock_filter * filter,unsigned int flen)820 static int bpf_check_classic(const struct sock_filter *filter,
821 unsigned int flen)
822 {
823 bool anc_found;
824 int pc;
825
826 /* Check the filter code now */
827 for (pc = 0; pc < flen; pc++) {
828 const struct sock_filter *ftest = &filter[pc];
829
830 /* May we actually operate on this code? */
831 if (!chk_code_allowed(ftest->code))
832 return -EINVAL;
833
834 /* Some instructions need special checks */
835 switch (ftest->code) {
836 case BPF_ALU | BPF_DIV | BPF_K:
837 case BPF_ALU | BPF_MOD | BPF_K:
838 /* Check for division by zero */
839 if (ftest->k == 0)
840 return -EINVAL;
841 break;
842 case BPF_ALU | BPF_LSH | BPF_K:
843 case BPF_ALU | BPF_RSH | BPF_K:
844 if (ftest->k >= 32)
845 return -EINVAL;
846 break;
847 case BPF_LD | BPF_MEM:
848 case BPF_LDX | BPF_MEM:
849 case BPF_ST:
850 case BPF_STX:
851 /* Check for invalid memory addresses */
852 if (ftest->k >= BPF_MEMWORDS)
853 return -EINVAL;
854 break;
855 case BPF_JMP | BPF_JA:
856 /* Note, the large ftest->k might cause loops.
857 * Compare this with conditional jumps below,
858 * where offsets are limited. --ANK (981016)
859 */
860 if (ftest->k >= (unsigned int)(flen - pc - 1))
861 return -EINVAL;
862 break;
863 case BPF_JMP | BPF_JEQ | BPF_K:
864 case BPF_JMP | BPF_JEQ | BPF_X:
865 case BPF_JMP | BPF_JGE | BPF_K:
866 case BPF_JMP | BPF_JGE | BPF_X:
867 case BPF_JMP | BPF_JGT | BPF_K:
868 case BPF_JMP | BPF_JGT | BPF_X:
869 case BPF_JMP | BPF_JSET | BPF_K:
870 case BPF_JMP | BPF_JSET | BPF_X:
871 /* Both conditionals must be safe */
872 if (pc + ftest->jt + 1 >= flen ||
873 pc + ftest->jf + 1 >= flen)
874 return -EINVAL;
875 break;
876 case BPF_LD | BPF_W | BPF_ABS:
877 case BPF_LD | BPF_H | BPF_ABS:
878 case BPF_LD | BPF_B | BPF_ABS:
879 anc_found = false;
880 if (bpf_anc_helper(ftest) & BPF_ANC)
881 anc_found = true;
882 /* Ancillary operation unknown or unsupported */
883 if (anc_found == false && ftest->k >= SKF_AD_OFF)
884 return -EINVAL;
885 }
886 }
887
888 /* Last instruction must be a RET code */
889 switch (filter[flen - 1].code) {
890 case BPF_RET | BPF_K:
891 case BPF_RET | BPF_A:
892 return check_load_and_stores(filter, flen);
893 }
894
895 return -EINVAL;
896 }
897
bpf_prog_store_orig_filter(struct bpf_prog * fp,const struct sock_fprog * fprog)898 static int bpf_prog_store_orig_filter(struct bpf_prog *fp,
899 const struct sock_fprog *fprog)
900 {
901 unsigned int fsize = bpf_classic_proglen(fprog);
902 struct sock_fprog_kern *fkprog;
903
904 fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL);
905 if (!fp->orig_prog)
906 return -ENOMEM;
907
908 fkprog = fp->orig_prog;
909 fkprog->len = fprog->len;
910
911 fkprog->filter = kmemdup(fp->insns, fsize,
912 GFP_KERNEL | __GFP_NOWARN);
913 if (!fkprog->filter) {
914 kfree(fp->orig_prog);
915 return -ENOMEM;
916 }
917
918 return 0;
919 }
920
bpf_release_orig_filter(struct bpf_prog * fp)921 static void bpf_release_orig_filter(struct bpf_prog *fp)
922 {
923 struct sock_fprog_kern *fprog = fp->orig_prog;
924
925 if (fprog) {
926 kfree(fprog->filter);
927 kfree(fprog);
928 }
929 }
930
__bpf_prog_release(struct bpf_prog * prog)931 static void __bpf_prog_release(struct bpf_prog *prog)
932 {
933 if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) {
934 bpf_prog_put(prog);
935 } else {
936 bpf_release_orig_filter(prog);
937 bpf_prog_free(prog);
938 }
939 }
940
__sk_filter_release(struct sk_filter * fp)941 static void __sk_filter_release(struct sk_filter *fp)
942 {
943 __bpf_prog_release(fp->prog);
944 kfree(fp);
945 }
946
947 /**
948 * sk_filter_release_rcu - Release a socket filter by rcu_head
949 * @rcu: rcu_head that contains the sk_filter to free
950 */
sk_filter_release_rcu(struct rcu_head * rcu)951 static void sk_filter_release_rcu(struct rcu_head *rcu)
952 {
953 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
954
955 __sk_filter_release(fp);
956 }
957
958 /**
959 * sk_filter_release - release a socket filter
960 * @fp: filter to remove
961 *
962 * Remove a filter from a socket and release its resources.
963 */
sk_filter_release(struct sk_filter * fp)964 static void sk_filter_release(struct sk_filter *fp)
965 {
966 if (refcount_dec_and_test(&fp->refcnt))
967 call_rcu(&fp->rcu, sk_filter_release_rcu);
968 }
969
sk_filter_uncharge(struct sock * sk,struct sk_filter * fp)970 void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp)
971 {
972 u32 filter_size = bpf_prog_size(fp->prog->len);
973
974 atomic_sub(filter_size, &sk->sk_omem_alloc);
975 sk_filter_release(fp);
976 }
977
978 /* try to charge the socket memory if there is space available
979 * return true on success
980 */
__sk_filter_charge(struct sock * sk,struct sk_filter * fp)981 static bool __sk_filter_charge(struct sock *sk, struct sk_filter *fp)
982 {
983 u32 filter_size = bpf_prog_size(fp->prog->len);
984
985 /* same check as in sock_kmalloc() */
986 if (filter_size <= sysctl_optmem_max &&
987 atomic_read(&sk->sk_omem_alloc) + filter_size < sysctl_optmem_max) {
988 atomic_add(filter_size, &sk->sk_omem_alloc);
989 return true;
990 }
991 return false;
992 }
993
sk_filter_charge(struct sock * sk,struct sk_filter * fp)994 bool sk_filter_charge(struct sock *sk, struct sk_filter *fp)
995 {
996 if (!refcount_inc_not_zero(&fp->refcnt))
997 return false;
998
999 if (!__sk_filter_charge(sk, fp)) {
1000 sk_filter_release(fp);
1001 return false;
1002 }
1003 return true;
1004 }
1005
bpf_migrate_filter(struct bpf_prog * fp)1006 static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp)
1007 {
1008 struct sock_filter *old_prog;
1009 struct bpf_prog *old_fp;
1010 int err, new_len, old_len = fp->len;
1011
1012 /* We are free to overwrite insns et al right here as it
1013 * won't be used at this point in time anymore internally
1014 * after the migration to the internal BPF instruction
1015 * representation.
1016 */
1017 BUILD_BUG_ON(sizeof(struct sock_filter) !=
1018 sizeof(struct bpf_insn));
1019
1020 /* Conversion cannot happen on overlapping memory areas,
1021 * so we need to keep the user BPF around until the 2nd
1022 * pass. At this time, the user BPF is stored in fp->insns.
1023 */
1024 old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter),
1025 GFP_KERNEL | __GFP_NOWARN);
1026 if (!old_prog) {
1027 err = -ENOMEM;
1028 goto out_err;
1029 }
1030
1031 /* 1st pass: calculate the new program length. */
1032 err = bpf_convert_filter(old_prog, old_len, NULL, &new_len);
1033 if (err)
1034 goto out_err_free;
1035
1036 /* Expand fp for appending the new filter representation. */
1037 old_fp = fp;
1038 fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0);
1039 if (!fp) {
1040 /* The old_fp is still around in case we couldn't
1041 * allocate new memory, so uncharge on that one.
1042 */
1043 fp = old_fp;
1044 err = -ENOMEM;
1045 goto out_err_free;
1046 }
1047
1048 fp->len = new_len;
1049
1050 /* 2nd pass: remap sock_filter insns into bpf_insn insns. */
1051 err = bpf_convert_filter(old_prog, old_len, fp, &new_len);
1052 if (err)
1053 /* 2nd bpf_convert_filter() can fail only if it fails
1054 * to allocate memory, remapping must succeed. Note,
1055 * that at this time old_fp has already been released
1056 * by krealloc().
1057 */
1058 goto out_err_free;
1059
1060 fp = bpf_prog_select_runtime(fp, &err);
1061 if (err)
1062 goto out_err_free;
1063
1064 kfree(old_prog);
1065 return fp;
1066
1067 out_err_free:
1068 kfree(old_prog);
1069 out_err:
1070 __bpf_prog_release(fp);
1071 return ERR_PTR(err);
1072 }
1073
bpf_prepare_filter(struct bpf_prog * fp,bpf_aux_classic_check_t trans)1074 static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp,
1075 bpf_aux_classic_check_t trans)
1076 {
1077 int err;
1078
1079 fp->bpf_func = NULL;
1080 fp->jited = 0;
1081
1082 err = bpf_check_classic(fp->insns, fp->len);
1083 if (err) {
1084 __bpf_prog_release(fp);
1085 return ERR_PTR(err);
1086 }
1087
1088 /* There might be additional checks and transformations
1089 * needed on classic filters, f.e. in case of seccomp.
1090 */
1091 if (trans) {
1092 err = trans(fp->insns, fp->len);
1093 if (err) {
1094 __bpf_prog_release(fp);
1095 return ERR_PTR(err);
1096 }
1097 }
1098
1099 /* Probe if we can JIT compile the filter and if so, do
1100 * the compilation of the filter.
1101 */
1102 bpf_jit_compile(fp);
1103
1104 /* JIT compiler couldn't process this filter, so do the
1105 * internal BPF translation for the optimized interpreter.
1106 */
1107 if (!fp->jited)
1108 fp = bpf_migrate_filter(fp);
1109
1110 return fp;
1111 }
1112
1113 /**
1114 * bpf_prog_create - create an unattached filter
1115 * @pfp: the unattached filter that is created
1116 * @fprog: the filter program
1117 *
1118 * Create a filter independent of any socket. We first run some
1119 * sanity checks on it to make sure it does not explode on us later.
1120 * If an error occurs or there is insufficient memory for the filter
1121 * a negative errno code is returned. On success the return is zero.
1122 */
bpf_prog_create(struct bpf_prog ** pfp,struct sock_fprog_kern * fprog)1123 int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog)
1124 {
1125 unsigned int fsize = bpf_classic_proglen(fprog);
1126 struct bpf_prog *fp;
1127
1128 /* Make sure new filter is there and in the right amounts. */
1129 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1130 return -EINVAL;
1131
1132 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1133 if (!fp)
1134 return -ENOMEM;
1135
1136 memcpy(fp->insns, fprog->filter, fsize);
1137
1138 fp->len = fprog->len;
1139 /* Since unattached filters are not copied back to user
1140 * space through sk_get_filter(), we do not need to hold
1141 * a copy here, and can spare us the work.
1142 */
1143 fp->orig_prog = NULL;
1144
1145 /* bpf_prepare_filter() already takes care of freeing
1146 * memory in case something goes wrong.
1147 */
1148 fp = bpf_prepare_filter(fp, NULL);
1149 if (IS_ERR(fp))
1150 return PTR_ERR(fp);
1151
1152 *pfp = fp;
1153 return 0;
1154 }
1155 EXPORT_SYMBOL_GPL(bpf_prog_create);
1156
1157 /**
1158 * bpf_prog_create_from_user - create an unattached filter from user buffer
1159 * @pfp: the unattached filter that is created
1160 * @fprog: the filter program
1161 * @trans: post-classic verifier transformation handler
1162 * @save_orig: save classic BPF program
1163 *
1164 * This function effectively does the same as bpf_prog_create(), only
1165 * that it builds up its insns buffer from user space provided buffer.
1166 * It also allows for passing a bpf_aux_classic_check_t handler.
1167 */
bpf_prog_create_from_user(struct bpf_prog ** pfp,struct sock_fprog * fprog,bpf_aux_classic_check_t trans,bool save_orig)1168 int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
1169 bpf_aux_classic_check_t trans, bool save_orig)
1170 {
1171 unsigned int fsize = bpf_classic_proglen(fprog);
1172 struct bpf_prog *fp;
1173 int err;
1174
1175 /* Make sure new filter is there and in the right amounts. */
1176 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1177 return -EINVAL;
1178
1179 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1180 if (!fp)
1181 return -ENOMEM;
1182
1183 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
1184 __bpf_prog_free(fp);
1185 return -EFAULT;
1186 }
1187
1188 fp->len = fprog->len;
1189 fp->orig_prog = NULL;
1190
1191 if (save_orig) {
1192 err = bpf_prog_store_orig_filter(fp, fprog);
1193 if (err) {
1194 __bpf_prog_free(fp);
1195 return -ENOMEM;
1196 }
1197 }
1198
1199 /* bpf_prepare_filter() already takes care of freeing
1200 * memory in case something goes wrong.
1201 */
1202 fp = bpf_prepare_filter(fp, trans);
1203 if (IS_ERR(fp))
1204 return PTR_ERR(fp);
1205
1206 *pfp = fp;
1207 return 0;
1208 }
1209 EXPORT_SYMBOL_GPL(bpf_prog_create_from_user);
1210
bpf_prog_destroy(struct bpf_prog * fp)1211 void bpf_prog_destroy(struct bpf_prog *fp)
1212 {
1213 __bpf_prog_release(fp);
1214 }
1215 EXPORT_SYMBOL_GPL(bpf_prog_destroy);
1216
__sk_attach_prog(struct bpf_prog * prog,struct sock * sk)1217 static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk)
1218 {
1219 struct sk_filter *fp, *old_fp;
1220
1221 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
1222 if (!fp)
1223 return -ENOMEM;
1224
1225 fp->prog = prog;
1226
1227 if (!__sk_filter_charge(sk, fp)) {
1228 kfree(fp);
1229 return -ENOMEM;
1230 }
1231 refcount_set(&fp->refcnt, 1);
1232
1233 old_fp = rcu_dereference_protected(sk->sk_filter,
1234 lockdep_sock_is_held(sk));
1235 rcu_assign_pointer(sk->sk_filter, fp);
1236
1237 if (old_fp)
1238 sk_filter_uncharge(sk, old_fp);
1239
1240 return 0;
1241 }
1242
__reuseport_attach_prog(struct bpf_prog * prog,struct sock * sk)1243 static int __reuseport_attach_prog(struct bpf_prog *prog, struct sock *sk)
1244 {
1245 struct bpf_prog *old_prog;
1246 int err;
1247
1248 if (bpf_prog_size(prog->len) > sysctl_optmem_max)
1249 return -ENOMEM;
1250
1251 if (sk_unhashed(sk) && sk->sk_reuseport) {
1252 err = reuseport_alloc(sk);
1253 if (err)
1254 return err;
1255 } else if (!rcu_access_pointer(sk->sk_reuseport_cb)) {
1256 /* The socket wasn't bound with SO_REUSEPORT */
1257 return -EINVAL;
1258 }
1259
1260 old_prog = reuseport_attach_prog(sk, prog);
1261 if (old_prog)
1262 bpf_prog_destroy(old_prog);
1263
1264 return 0;
1265 }
1266
1267 static
__get_filter(struct sock_fprog * fprog,struct sock * sk)1268 struct bpf_prog *__get_filter(struct sock_fprog *fprog, struct sock *sk)
1269 {
1270 unsigned int fsize = bpf_classic_proglen(fprog);
1271 struct bpf_prog *prog;
1272 int err;
1273
1274 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1275 return ERR_PTR(-EPERM);
1276
1277 /* Make sure new filter is there and in the right amounts. */
1278 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1279 return ERR_PTR(-EINVAL);
1280
1281 prog = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1282 if (!prog)
1283 return ERR_PTR(-ENOMEM);
1284
1285 if (copy_from_user(prog->insns, fprog->filter, fsize)) {
1286 __bpf_prog_free(prog);
1287 return ERR_PTR(-EFAULT);
1288 }
1289
1290 prog->len = fprog->len;
1291
1292 err = bpf_prog_store_orig_filter(prog, fprog);
1293 if (err) {
1294 __bpf_prog_free(prog);
1295 return ERR_PTR(-ENOMEM);
1296 }
1297
1298 /* bpf_prepare_filter() already takes care of freeing
1299 * memory in case something goes wrong.
1300 */
1301 return bpf_prepare_filter(prog, NULL);
1302 }
1303
1304 /**
1305 * sk_attach_filter - attach a socket filter
1306 * @fprog: the filter program
1307 * @sk: the socket to use
1308 *
1309 * Attach the user's filter code. We first run some sanity checks on
1310 * it to make sure it does not explode on us later. If an error
1311 * occurs or there is insufficient memory for the filter a negative
1312 * errno code is returned. On success the return is zero.
1313 */
sk_attach_filter(struct sock_fprog * fprog,struct sock * sk)1314 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1315 {
1316 struct bpf_prog *prog = __get_filter(fprog, sk);
1317 int err;
1318
1319 if (IS_ERR(prog))
1320 return PTR_ERR(prog);
1321
1322 err = __sk_attach_prog(prog, sk);
1323 if (err < 0) {
1324 __bpf_prog_release(prog);
1325 return err;
1326 }
1327
1328 return 0;
1329 }
1330 EXPORT_SYMBOL_GPL(sk_attach_filter);
1331
sk_reuseport_attach_filter(struct sock_fprog * fprog,struct sock * sk)1332 int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1333 {
1334 struct bpf_prog *prog = __get_filter(fprog, sk);
1335 int err;
1336
1337 if (IS_ERR(prog))
1338 return PTR_ERR(prog);
1339
1340 err = __reuseport_attach_prog(prog, sk);
1341 if (err < 0) {
1342 __bpf_prog_release(prog);
1343 return err;
1344 }
1345
1346 return 0;
1347 }
1348
__get_bpf(u32 ufd,struct sock * sk)1349 static struct bpf_prog *__get_bpf(u32 ufd, struct sock *sk)
1350 {
1351 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1352 return ERR_PTR(-EPERM);
1353
1354 return bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER);
1355 }
1356
sk_attach_bpf(u32 ufd,struct sock * sk)1357 int sk_attach_bpf(u32 ufd, struct sock *sk)
1358 {
1359 struct bpf_prog *prog = __get_bpf(ufd, sk);
1360 int err;
1361
1362 if (IS_ERR(prog))
1363 return PTR_ERR(prog);
1364
1365 err = __sk_attach_prog(prog, sk);
1366 if (err < 0) {
1367 bpf_prog_put(prog);
1368 return err;
1369 }
1370
1371 return 0;
1372 }
1373
sk_reuseport_attach_bpf(u32 ufd,struct sock * sk)1374 int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk)
1375 {
1376 struct bpf_prog *prog = __get_bpf(ufd, sk);
1377 int err;
1378
1379 if (IS_ERR(prog))
1380 return PTR_ERR(prog);
1381
1382 err = __reuseport_attach_prog(prog, sk);
1383 if (err < 0) {
1384 bpf_prog_put(prog);
1385 return err;
1386 }
1387
1388 return 0;
1389 }
1390
1391 struct bpf_scratchpad {
1392 union {
1393 __be32 diff[MAX_BPF_STACK / sizeof(__be32)];
1394 u8 buff[MAX_BPF_STACK];
1395 };
1396 };
1397
1398 static DEFINE_PER_CPU(struct bpf_scratchpad, bpf_sp);
1399
__bpf_try_make_writable(struct sk_buff * skb,unsigned int write_len)1400 static inline int __bpf_try_make_writable(struct sk_buff *skb,
1401 unsigned int write_len)
1402 {
1403 return skb_ensure_writable(skb, write_len);
1404 }
1405
bpf_try_make_writable(struct sk_buff * skb,unsigned int write_len)1406 static inline int bpf_try_make_writable(struct sk_buff *skb,
1407 unsigned int write_len)
1408 {
1409 int err = __bpf_try_make_writable(skb, write_len);
1410
1411 bpf_compute_data_end(skb);
1412 return err;
1413 }
1414
bpf_try_make_head_writable(struct sk_buff * skb)1415 static int bpf_try_make_head_writable(struct sk_buff *skb)
1416 {
1417 return bpf_try_make_writable(skb, skb_headlen(skb));
1418 }
1419
bpf_push_mac_rcsum(struct sk_buff * skb)1420 static inline void bpf_push_mac_rcsum(struct sk_buff *skb)
1421 {
1422 if (skb_at_tc_ingress(skb))
1423 skb_postpush_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1424 }
1425
bpf_pull_mac_rcsum(struct sk_buff * skb)1426 static inline void bpf_pull_mac_rcsum(struct sk_buff *skb)
1427 {
1428 if (skb_at_tc_ingress(skb))
1429 skb_postpull_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1430 }
1431
BPF_CALL_5(bpf_skb_store_bytes,struct sk_buff *,skb,u32,offset,const void *,from,u32,len,u64,flags)1432 BPF_CALL_5(bpf_skb_store_bytes, struct sk_buff *, skb, u32, offset,
1433 const void *, from, u32, len, u64, flags)
1434 {
1435 void *ptr;
1436
1437 if (unlikely(flags & ~(BPF_F_RECOMPUTE_CSUM | BPF_F_INVALIDATE_HASH)))
1438 return -EINVAL;
1439 if (unlikely(offset > 0xffff))
1440 return -EFAULT;
1441 if (unlikely(bpf_try_make_writable(skb, offset + len)))
1442 return -EFAULT;
1443
1444 ptr = skb->data + offset;
1445 if (flags & BPF_F_RECOMPUTE_CSUM)
1446 __skb_postpull_rcsum(skb, ptr, len, offset);
1447
1448 memcpy(ptr, from, len);
1449
1450 if (flags & BPF_F_RECOMPUTE_CSUM)
1451 __skb_postpush_rcsum(skb, ptr, len, offset);
1452 if (flags & BPF_F_INVALIDATE_HASH)
1453 skb_clear_hash(skb);
1454
1455 return 0;
1456 }
1457
1458 static const struct bpf_func_proto bpf_skb_store_bytes_proto = {
1459 .func = bpf_skb_store_bytes,
1460 .gpl_only = false,
1461 .ret_type = RET_INTEGER,
1462 .arg1_type = ARG_PTR_TO_CTX,
1463 .arg2_type = ARG_ANYTHING,
1464 .arg3_type = ARG_PTR_TO_MEM,
1465 .arg4_type = ARG_CONST_SIZE,
1466 .arg5_type = ARG_ANYTHING,
1467 };
1468
BPF_CALL_4(bpf_skb_load_bytes,const struct sk_buff *,skb,u32,offset,void *,to,u32,len)1469 BPF_CALL_4(bpf_skb_load_bytes, const struct sk_buff *, skb, u32, offset,
1470 void *, to, u32, len)
1471 {
1472 void *ptr;
1473
1474 if (unlikely(offset > 0xffff))
1475 goto err_clear;
1476
1477 ptr = skb_header_pointer(skb, offset, len, to);
1478 if (unlikely(!ptr))
1479 goto err_clear;
1480 if (ptr != to)
1481 memcpy(to, ptr, len);
1482
1483 return 0;
1484 err_clear:
1485 memset(to, 0, len);
1486 return -EFAULT;
1487 }
1488
1489 static const struct bpf_func_proto bpf_skb_load_bytes_proto = {
1490 .func = bpf_skb_load_bytes,
1491 .gpl_only = false,
1492 .ret_type = RET_INTEGER,
1493 .arg1_type = ARG_PTR_TO_CTX,
1494 .arg2_type = ARG_ANYTHING,
1495 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1496 .arg4_type = ARG_CONST_SIZE,
1497 };
1498
BPF_CALL_2(bpf_skb_pull_data,struct sk_buff *,skb,u32,len)1499 BPF_CALL_2(bpf_skb_pull_data, struct sk_buff *, skb, u32, len)
1500 {
1501 /* Idea is the following: should the needed direct read/write
1502 * test fail during runtime, we can pull in more data and redo
1503 * again, since implicitly, we invalidate previous checks here.
1504 *
1505 * Or, since we know how much we need to make read/writeable,
1506 * this can be done once at the program beginning for direct
1507 * access case. By this we overcome limitations of only current
1508 * headroom being accessible.
1509 */
1510 return bpf_try_make_writable(skb, len ? : skb_headlen(skb));
1511 }
1512
1513 static const struct bpf_func_proto bpf_skb_pull_data_proto = {
1514 .func = bpf_skb_pull_data,
1515 .gpl_only = false,
1516 .ret_type = RET_INTEGER,
1517 .arg1_type = ARG_PTR_TO_CTX,
1518 .arg2_type = ARG_ANYTHING,
1519 };
1520
BPF_CALL_5(bpf_l3_csum_replace,struct sk_buff *,skb,u32,offset,u64,from,u64,to,u64,flags)1521 BPF_CALL_5(bpf_l3_csum_replace, struct sk_buff *, skb, u32, offset,
1522 u64, from, u64, to, u64, flags)
1523 {
1524 __sum16 *ptr;
1525
1526 if (unlikely(flags & ~(BPF_F_HDR_FIELD_MASK)))
1527 return -EINVAL;
1528 if (unlikely(offset > 0xffff || offset & 1))
1529 return -EFAULT;
1530 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1531 return -EFAULT;
1532
1533 ptr = (__sum16 *)(skb->data + offset);
1534 switch (flags & BPF_F_HDR_FIELD_MASK) {
1535 case 0:
1536 if (unlikely(from != 0))
1537 return -EINVAL;
1538
1539 csum_replace_by_diff(ptr, to);
1540 break;
1541 case 2:
1542 csum_replace2(ptr, from, to);
1543 break;
1544 case 4:
1545 csum_replace4(ptr, from, to);
1546 break;
1547 default:
1548 return -EINVAL;
1549 }
1550
1551 return 0;
1552 }
1553
1554 static const struct bpf_func_proto bpf_l3_csum_replace_proto = {
1555 .func = bpf_l3_csum_replace,
1556 .gpl_only = false,
1557 .ret_type = RET_INTEGER,
1558 .arg1_type = ARG_PTR_TO_CTX,
1559 .arg2_type = ARG_ANYTHING,
1560 .arg3_type = ARG_ANYTHING,
1561 .arg4_type = ARG_ANYTHING,
1562 .arg5_type = ARG_ANYTHING,
1563 };
1564
BPF_CALL_5(bpf_l4_csum_replace,struct sk_buff *,skb,u32,offset,u64,from,u64,to,u64,flags)1565 BPF_CALL_5(bpf_l4_csum_replace, struct sk_buff *, skb, u32, offset,
1566 u64, from, u64, to, u64, flags)
1567 {
1568 bool is_pseudo = flags & BPF_F_PSEUDO_HDR;
1569 bool is_mmzero = flags & BPF_F_MARK_MANGLED_0;
1570 bool do_mforce = flags & BPF_F_MARK_ENFORCE;
1571 __sum16 *ptr;
1572
1573 if (unlikely(flags & ~(BPF_F_MARK_MANGLED_0 | BPF_F_MARK_ENFORCE |
1574 BPF_F_PSEUDO_HDR | BPF_F_HDR_FIELD_MASK)))
1575 return -EINVAL;
1576 if (unlikely(offset > 0xffff || offset & 1))
1577 return -EFAULT;
1578 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1579 return -EFAULT;
1580
1581 ptr = (__sum16 *)(skb->data + offset);
1582 if (is_mmzero && !do_mforce && !*ptr)
1583 return 0;
1584
1585 switch (flags & BPF_F_HDR_FIELD_MASK) {
1586 case 0:
1587 if (unlikely(from != 0))
1588 return -EINVAL;
1589
1590 inet_proto_csum_replace_by_diff(ptr, skb, to, is_pseudo);
1591 break;
1592 case 2:
1593 inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo);
1594 break;
1595 case 4:
1596 inet_proto_csum_replace4(ptr, skb, from, to, is_pseudo);
1597 break;
1598 default:
1599 return -EINVAL;
1600 }
1601
1602 if (is_mmzero && !*ptr)
1603 *ptr = CSUM_MANGLED_0;
1604 return 0;
1605 }
1606
1607 static const struct bpf_func_proto bpf_l4_csum_replace_proto = {
1608 .func = bpf_l4_csum_replace,
1609 .gpl_only = false,
1610 .ret_type = RET_INTEGER,
1611 .arg1_type = ARG_PTR_TO_CTX,
1612 .arg2_type = ARG_ANYTHING,
1613 .arg3_type = ARG_ANYTHING,
1614 .arg4_type = ARG_ANYTHING,
1615 .arg5_type = ARG_ANYTHING,
1616 };
1617
BPF_CALL_5(bpf_csum_diff,__be32 *,from,u32,from_size,__be32 *,to,u32,to_size,__wsum,seed)1618 BPF_CALL_5(bpf_csum_diff, __be32 *, from, u32, from_size,
1619 __be32 *, to, u32, to_size, __wsum, seed)
1620 {
1621 struct bpf_scratchpad *sp = this_cpu_ptr(&bpf_sp);
1622 u32 diff_size = from_size + to_size;
1623 int i, j = 0;
1624
1625 /* This is quite flexible, some examples:
1626 *
1627 * from_size == 0, to_size > 0, seed := csum --> pushing data
1628 * from_size > 0, to_size == 0, seed := csum --> pulling data
1629 * from_size > 0, to_size > 0, seed := 0 --> diffing data
1630 *
1631 * Even for diffing, from_size and to_size don't need to be equal.
1632 */
1633 if (unlikely(((from_size | to_size) & (sizeof(__be32) - 1)) ||
1634 diff_size > sizeof(sp->diff)))
1635 return -EINVAL;
1636
1637 for (i = 0; i < from_size / sizeof(__be32); i++, j++)
1638 sp->diff[j] = ~from[i];
1639 for (i = 0; i < to_size / sizeof(__be32); i++, j++)
1640 sp->diff[j] = to[i];
1641
1642 return csum_partial(sp->diff, diff_size, seed);
1643 }
1644
1645 static const struct bpf_func_proto bpf_csum_diff_proto = {
1646 .func = bpf_csum_diff,
1647 .gpl_only = false,
1648 .pkt_access = true,
1649 .ret_type = RET_INTEGER,
1650 .arg1_type = ARG_PTR_TO_MEM,
1651 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
1652 .arg3_type = ARG_PTR_TO_MEM,
1653 .arg4_type = ARG_CONST_SIZE_OR_ZERO,
1654 .arg5_type = ARG_ANYTHING,
1655 };
1656
BPF_CALL_2(bpf_csum_update,struct sk_buff *,skb,__wsum,csum)1657 BPF_CALL_2(bpf_csum_update, struct sk_buff *, skb, __wsum, csum)
1658 {
1659 /* The interface is to be used in combination with bpf_csum_diff()
1660 * for direct packet writes. csum rotation for alignment as well
1661 * as emulating csum_sub() can be done from the eBPF program.
1662 */
1663 if (skb->ip_summed == CHECKSUM_COMPLETE)
1664 return (skb->csum = csum_add(skb->csum, csum));
1665
1666 return -ENOTSUPP;
1667 }
1668
1669 static const struct bpf_func_proto bpf_csum_update_proto = {
1670 .func = bpf_csum_update,
1671 .gpl_only = false,
1672 .ret_type = RET_INTEGER,
1673 .arg1_type = ARG_PTR_TO_CTX,
1674 .arg2_type = ARG_ANYTHING,
1675 };
1676
__bpf_rx_skb(struct net_device * dev,struct sk_buff * skb)1677 static inline int __bpf_rx_skb(struct net_device *dev, struct sk_buff *skb)
1678 {
1679 return dev_forward_skb(dev, skb);
1680 }
1681
__bpf_rx_skb_no_mac(struct net_device * dev,struct sk_buff * skb)1682 static inline int __bpf_rx_skb_no_mac(struct net_device *dev,
1683 struct sk_buff *skb)
1684 {
1685 int ret = ____dev_forward_skb(dev, skb);
1686
1687 if (likely(!ret)) {
1688 skb->dev = dev;
1689 ret = netif_rx(skb);
1690 }
1691
1692 return ret;
1693 }
1694
__bpf_tx_skb(struct net_device * dev,struct sk_buff * skb)1695 static inline int __bpf_tx_skb(struct net_device *dev, struct sk_buff *skb)
1696 {
1697 int ret;
1698
1699 if (unlikely(__this_cpu_read(xmit_recursion) > XMIT_RECURSION_LIMIT)) {
1700 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n");
1701 kfree_skb(skb);
1702 return -ENETDOWN;
1703 }
1704
1705 skb->dev = dev;
1706
1707 __this_cpu_inc(xmit_recursion);
1708 ret = dev_queue_xmit(skb);
1709 __this_cpu_dec(xmit_recursion);
1710
1711 return ret;
1712 }
1713
__bpf_redirect_no_mac(struct sk_buff * skb,struct net_device * dev,u32 flags)1714 static int __bpf_redirect_no_mac(struct sk_buff *skb, struct net_device *dev,
1715 u32 flags)
1716 {
1717 unsigned int mlen = skb_network_offset(skb);
1718
1719 if (mlen) {
1720 __skb_pull(skb, mlen);
1721
1722 /* At ingress, the mac header has already been pulled once.
1723 * At egress, skb_pospull_rcsum has to be done in case that
1724 * the skb is originated from ingress (i.e. a forwarded skb)
1725 * to ensure that rcsum starts at net header.
1726 */
1727 if (!skb_at_tc_ingress(skb))
1728 skb_postpull_rcsum(skb, skb_mac_header(skb), mlen);
1729 }
1730 skb_pop_mac_header(skb);
1731 skb_reset_mac_len(skb);
1732 return flags & BPF_F_INGRESS ?
1733 __bpf_rx_skb_no_mac(dev, skb) : __bpf_tx_skb(dev, skb);
1734 }
1735
__bpf_redirect_common(struct sk_buff * skb,struct net_device * dev,u32 flags)1736 static int __bpf_redirect_common(struct sk_buff *skb, struct net_device *dev,
1737 u32 flags)
1738 {
1739 /* Verify that a link layer header is carried */
1740 if (unlikely(skb->mac_header >= skb->network_header)) {
1741 kfree_skb(skb);
1742 return -ERANGE;
1743 }
1744
1745 bpf_push_mac_rcsum(skb);
1746 return flags & BPF_F_INGRESS ?
1747 __bpf_rx_skb(dev, skb) : __bpf_tx_skb(dev, skb);
1748 }
1749
__bpf_redirect(struct sk_buff * skb,struct net_device * dev,u32 flags)1750 static int __bpf_redirect(struct sk_buff *skb, struct net_device *dev,
1751 u32 flags)
1752 {
1753 if (dev_is_mac_header_xmit(dev))
1754 return __bpf_redirect_common(skb, dev, flags);
1755 else
1756 return __bpf_redirect_no_mac(skb, dev, flags);
1757 }
1758
BPF_CALL_3(bpf_clone_redirect,struct sk_buff *,skb,u32,ifindex,u64,flags)1759 BPF_CALL_3(bpf_clone_redirect, struct sk_buff *, skb, u32, ifindex, u64, flags)
1760 {
1761 struct net_device *dev;
1762 struct sk_buff *clone;
1763 int ret;
1764
1765 if (unlikely(flags & ~(BPF_F_INGRESS)))
1766 return -EINVAL;
1767
1768 dev = dev_get_by_index_rcu(dev_net(skb->dev), ifindex);
1769 if (unlikely(!dev))
1770 return -EINVAL;
1771
1772 clone = skb_clone(skb, GFP_ATOMIC);
1773 if (unlikely(!clone))
1774 return -ENOMEM;
1775
1776 /* For direct write, we need to keep the invariant that the skbs
1777 * we're dealing with need to be uncloned. Should uncloning fail
1778 * here, we need to free the just generated clone to unclone once
1779 * again.
1780 */
1781 ret = bpf_try_make_head_writable(skb);
1782 if (unlikely(ret)) {
1783 kfree_skb(clone);
1784 return -ENOMEM;
1785 }
1786
1787 return __bpf_redirect(clone, dev, flags);
1788 }
1789
1790 static const struct bpf_func_proto bpf_clone_redirect_proto = {
1791 .func = bpf_clone_redirect,
1792 .gpl_only = false,
1793 .ret_type = RET_INTEGER,
1794 .arg1_type = ARG_PTR_TO_CTX,
1795 .arg2_type = ARG_ANYTHING,
1796 .arg3_type = ARG_ANYTHING,
1797 };
1798
1799 struct redirect_info {
1800 u32 ifindex;
1801 u32 flags;
1802 struct bpf_map *map;
1803 struct bpf_map *map_to_flush;
1804 unsigned long map_owner;
1805 };
1806
1807 static DEFINE_PER_CPU(struct redirect_info, redirect_info);
1808
BPF_CALL_2(bpf_redirect,u32,ifindex,u64,flags)1809 BPF_CALL_2(bpf_redirect, u32, ifindex, u64, flags)
1810 {
1811 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
1812
1813 if (unlikely(flags & ~(BPF_F_INGRESS)))
1814 return TC_ACT_SHOT;
1815
1816 ri->ifindex = ifindex;
1817 ri->flags = flags;
1818
1819 return TC_ACT_REDIRECT;
1820 }
1821
skb_do_redirect(struct sk_buff * skb)1822 int skb_do_redirect(struct sk_buff *skb)
1823 {
1824 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
1825 struct net_device *dev;
1826
1827 dev = dev_get_by_index_rcu(dev_net(skb->dev), ri->ifindex);
1828 ri->ifindex = 0;
1829 if (unlikely(!dev)) {
1830 kfree_skb(skb);
1831 return -EINVAL;
1832 }
1833
1834 return __bpf_redirect(skb, dev, ri->flags);
1835 }
1836
1837 static const struct bpf_func_proto bpf_redirect_proto = {
1838 .func = bpf_redirect,
1839 .gpl_only = false,
1840 .ret_type = RET_INTEGER,
1841 .arg1_type = ARG_ANYTHING,
1842 .arg2_type = ARG_ANYTHING,
1843 };
1844
BPF_CALL_4(bpf_sk_redirect_map,struct sk_buff *,skb,struct bpf_map *,map,u32,key,u64,flags)1845 BPF_CALL_4(bpf_sk_redirect_map, struct sk_buff *, skb,
1846 struct bpf_map *, map, u32, key, u64, flags)
1847 {
1848 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
1849
1850 /* If user passes invalid input drop the packet. */
1851 if (unlikely(flags))
1852 return SK_DROP;
1853
1854 tcb->bpf.key = key;
1855 tcb->bpf.flags = flags;
1856 tcb->bpf.map = map;
1857
1858 return SK_PASS;
1859 }
1860
do_sk_redirect_map(struct sk_buff * skb)1861 struct sock *do_sk_redirect_map(struct sk_buff *skb)
1862 {
1863 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
1864 struct sock *sk = NULL;
1865
1866 if (tcb->bpf.map) {
1867 sk = __sock_map_lookup_elem(tcb->bpf.map, tcb->bpf.key);
1868
1869 tcb->bpf.key = 0;
1870 tcb->bpf.map = NULL;
1871 }
1872
1873 return sk;
1874 }
1875
1876 static const struct bpf_func_proto bpf_sk_redirect_map_proto = {
1877 .func = bpf_sk_redirect_map,
1878 .gpl_only = false,
1879 .ret_type = RET_INTEGER,
1880 .arg1_type = ARG_PTR_TO_CTX,
1881 .arg2_type = ARG_CONST_MAP_PTR,
1882 .arg3_type = ARG_ANYTHING,
1883 .arg4_type = ARG_ANYTHING,
1884 };
1885
BPF_CALL_1(bpf_get_cgroup_classid,const struct sk_buff *,skb)1886 BPF_CALL_1(bpf_get_cgroup_classid, const struct sk_buff *, skb)
1887 {
1888 return task_get_classid(skb);
1889 }
1890
1891 static const struct bpf_func_proto bpf_get_cgroup_classid_proto = {
1892 .func = bpf_get_cgroup_classid,
1893 .gpl_only = false,
1894 .ret_type = RET_INTEGER,
1895 .arg1_type = ARG_PTR_TO_CTX,
1896 };
1897
BPF_CALL_1(bpf_get_route_realm,const struct sk_buff *,skb)1898 BPF_CALL_1(bpf_get_route_realm, const struct sk_buff *, skb)
1899 {
1900 return dst_tclassid(skb);
1901 }
1902
1903 static const struct bpf_func_proto bpf_get_route_realm_proto = {
1904 .func = bpf_get_route_realm,
1905 .gpl_only = false,
1906 .ret_type = RET_INTEGER,
1907 .arg1_type = ARG_PTR_TO_CTX,
1908 };
1909
BPF_CALL_1(bpf_get_hash_recalc,struct sk_buff *,skb)1910 BPF_CALL_1(bpf_get_hash_recalc, struct sk_buff *, skb)
1911 {
1912 /* If skb_clear_hash() was called due to mangling, we can
1913 * trigger SW recalculation here. Later access to hash
1914 * can then use the inline skb->hash via context directly
1915 * instead of calling this helper again.
1916 */
1917 return skb_get_hash(skb);
1918 }
1919
1920 static const struct bpf_func_proto bpf_get_hash_recalc_proto = {
1921 .func = bpf_get_hash_recalc,
1922 .gpl_only = false,
1923 .ret_type = RET_INTEGER,
1924 .arg1_type = ARG_PTR_TO_CTX,
1925 };
1926
BPF_CALL_1(bpf_set_hash_invalid,struct sk_buff *,skb)1927 BPF_CALL_1(bpf_set_hash_invalid, struct sk_buff *, skb)
1928 {
1929 /* After all direct packet write, this can be used once for
1930 * triggering a lazy recalc on next skb_get_hash() invocation.
1931 */
1932 skb_clear_hash(skb);
1933 return 0;
1934 }
1935
1936 static const struct bpf_func_proto bpf_set_hash_invalid_proto = {
1937 .func = bpf_set_hash_invalid,
1938 .gpl_only = false,
1939 .ret_type = RET_INTEGER,
1940 .arg1_type = ARG_PTR_TO_CTX,
1941 };
1942
BPF_CALL_2(bpf_set_hash,struct sk_buff *,skb,u32,hash)1943 BPF_CALL_2(bpf_set_hash, struct sk_buff *, skb, u32, hash)
1944 {
1945 /* Set user specified hash as L4(+), so that it gets returned
1946 * on skb_get_hash() call unless BPF prog later on triggers a
1947 * skb_clear_hash().
1948 */
1949 __skb_set_sw_hash(skb, hash, true);
1950 return 0;
1951 }
1952
1953 static const struct bpf_func_proto bpf_set_hash_proto = {
1954 .func = bpf_set_hash,
1955 .gpl_only = false,
1956 .ret_type = RET_INTEGER,
1957 .arg1_type = ARG_PTR_TO_CTX,
1958 .arg2_type = ARG_ANYTHING,
1959 };
1960
BPF_CALL_3(bpf_skb_vlan_push,struct sk_buff *,skb,__be16,vlan_proto,u16,vlan_tci)1961 BPF_CALL_3(bpf_skb_vlan_push, struct sk_buff *, skb, __be16, vlan_proto,
1962 u16, vlan_tci)
1963 {
1964 int ret;
1965
1966 if (unlikely(vlan_proto != htons(ETH_P_8021Q) &&
1967 vlan_proto != htons(ETH_P_8021AD)))
1968 vlan_proto = htons(ETH_P_8021Q);
1969
1970 bpf_push_mac_rcsum(skb);
1971 ret = skb_vlan_push(skb, vlan_proto, vlan_tci);
1972 bpf_pull_mac_rcsum(skb);
1973
1974 bpf_compute_data_end(skb);
1975 return ret;
1976 }
1977
1978 const struct bpf_func_proto bpf_skb_vlan_push_proto = {
1979 .func = bpf_skb_vlan_push,
1980 .gpl_only = false,
1981 .ret_type = RET_INTEGER,
1982 .arg1_type = ARG_PTR_TO_CTX,
1983 .arg2_type = ARG_ANYTHING,
1984 .arg3_type = ARG_ANYTHING,
1985 };
1986 EXPORT_SYMBOL_GPL(bpf_skb_vlan_push_proto);
1987
BPF_CALL_1(bpf_skb_vlan_pop,struct sk_buff *,skb)1988 BPF_CALL_1(bpf_skb_vlan_pop, struct sk_buff *, skb)
1989 {
1990 int ret;
1991
1992 bpf_push_mac_rcsum(skb);
1993 ret = skb_vlan_pop(skb);
1994 bpf_pull_mac_rcsum(skb);
1995
1996 bpf_compute_data_end(skb);
1997 return ret;
1998 }
1999
2000 const struct bpf_func_proto bpf_skb_vlan_pop_proto = {
2001 .func = bpf_skb_vlan_pop,
2002 .gpl_only = false,
2003 .ret_type = RET_INTEGER,
2004 .arg1_type = ARG_PTR_TO_CTX,
2005 };
2006 EXPORT_SYMBOL_GPL(bpf_skb_vlan_pop_proto);
2007
bpf_skb_generic_push(struct sk_buff * skb,u32 off,u32 len)2008 static int bpf_skb_generic_push(struct sk_buff *skb, u32 off, u32 len)
2009 {
2010 /* Caller already did skb_cow() with len as headroom,
2011 * so no need to do it here.
2012 */
2013 skb_push(skb, len);
2014 memmove(skb->data, skb->data + len, off);
2015 memset(skb->data + off, 0, len);
2016
2017 /* No skb_postpush_rcsum(skb, skb->data + off, len)
2018 * needed here as it does not change the skb->csum
2019 * result for checksum complete when summing over
2020 * zeroed blocks.
2021 */
2022 return 0;
2023 }
2024
bpf_skb_generic_pop(struct sk_buff * skb,u32 off,u32 len)2025 static int bpf_skb_generic_pop(struct sk_buff *skb, u32 off, u32 len)
2026 {
2027 /* skb_ensure_writable() is not needed here, as we're
2028 * already working on an uncloned skb.
2029 */
2030 if (unlikely(!pskb_may_pull(skb, off + len)))
2031 return -ENOMEM;
2032
2033 skb_postpull_rcsum(skb, skb->data + off, len);
2034 memmove(skb->data + len, skb->data, off);
2035 __skb_pull(skb, len);
2036
2037 return 0;
2038 }
2039
bpf_skb_net_hdr_push(struct sk_buff * skb,u32 off,u32 len)2040 static int bpf_skb_net_hdr_push(struct sk_buff *skb, u32 off, u32 len)
2041 {
2042 bool trans_same = skb->transport_header == skb->network_header;
2043 int ret;
2044
2045 /* There's no need for __skb_push()/__skb_pull() pair to
2046 * get to the start of the mac header as we're guaranteed
2047 * to always start from here under eBPF.
2048 */
2049 ret = bpf_skb_generic_push(skb, off, len);
2050 if (likely(!ret)) {
2051 skb->mac_header -= len;
2052 skb->network_header -= len;
2053 if (trans_same)
2054 skb->transport_header = skb->network_header;
2055 }
2056
2057 return ret;
2058 }
2059
bpf_skb_net_hdr_pop(struct sk_buff * skb,u32 off,u32 len)2060 static int bpf_skb_net_hdr_pop(struct sk_buff *skb, u32 off, u32 len)
2061 {
2062 bool trans_same = skb->transport_header == skb->network_header;
2063 int ret;
2064
2065 /* Same here, __skb_push()/__skb_pull() pair not needed. */
2066 ret = bpf_skb_generic_pop(skb, off, len);
2067 if (likely(!ret)) {
2068 skb->mac_header += len;
2069 skb->network_header += len;
2070 if (trans_same)
2071 skb->transport_header = skb->network_header;
2072 }
2073
2074 return ret;
2075 }
2076
bpf_skb_proto_4_to_6(struct sk_buff * skb)2077 static int bpf_skb_proto_4_to_6(struct sk_buff *skb)
2078 {
2079 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
2080 u32 off = skb_mac_header_len(skb);
2081 int ret;
2082
2083 ret = skb_cow(skb, len_diff);
2084 if (unlikely(ret < 0))
2085 return ret;
2086
2087 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
2088 if (unlikely(ret < 0))
2089 return ret;
2090
2091 if (skb_is_gso(skb)) {
2092 /* SKB_GSO_TCPV4 needs to be changed into
2093 * SKB_GSO_TCPV6.
2094 */
2095 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
2096 skb_shinfo(skb)->gso_type &= ~SKB_GSO_TCPV4;
2097 skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV6;
2098 }
2099
2100 /* Due to IPv6 header, MSS needs to be downgraded. */
2101 skb_shinfo(skb)->gso_size -= len_diff;
2102 /* Header must be checked, and gso_segs recomputed. */
2103 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
2104 skb_shinfo(skb)->gso_segs = 0;
2105 }
2106
2107 skb->protocol = htons(ETH_P_IPV6);
2108 skb_clear_hash(skb);
2109
2110 return 0;
2111 }
2112
bpf_skb_proto_6_to_4(struct sk_buff * skb)2113 static int bpf_skb_proto_6_to_4(struct sk_buff *skb)
2114 {
2115 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
2116 u32 off = skb_mac_header_len(skb);
2117 int ret;
2118
2119 ret = skb_unclone(skb, GFP_ATOMIC);
2120 if (unlikely(ret < 0))
2121 return ret;
2122
2123 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
2124 if (unlikely(ret < 0))
2125 return ret;
2126
2127 if (skb_is_gso(skb)) {
2128 /* SKB_GSO_TCPV6 needs to be changed into
2129 * SKB_GSO_TCPV4.
2130 */
2131 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) {
2132 skb_shinfo(skb)->gso_type &= ~SKB_GSO_TCPV6;
2133 skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV4;
2134 }
2135
2136 /* Due to IPv4 header, MSS can be upgraded. */
2137 skb_shinfo(skb)->gso_size += len_diff;
2138 /* Header must be checked, and gso_segs recomputed. */
2139 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
2140 skb_shinfo(skb)->gso_segs = 0;
2141 }
2142
2143 skb->protocol = htons(ETH_P_IP);
2144 skb_clear_hash(skb);
2145
2146 return 0;
2147 }
2148
bpf_skb_proto_xlat(struct sk_buff * skb,__be16 to_proto)2149 static int bpf_skb_proto_xlat(struct sk_buff *skb, __be16 to_proto)
2150 {
2151 __be16 from_proto = skb->protocol;
2152
2153 if (from_proto == htons(ETH_P_IP) &&
2154 to_proto == htons(ETH_P_IPV6))
2155 return bpf_skb_proto_4_to_6(skb);
2156
2157 if (from_proto == htons(ETH_P_IPV6) &&
2158 to_proto == htons(ETH_P_IP))
2159 return bpf_skb_proto_6_to_4(skb);
2160
2161 return -ENOTSUPP;
2162 }
2163
BPF_CALL_3(bpf_skb_change_proto,struct sk_buff *,skb,__be16,proto,u64,flags)2164 BPF_CALL_3(bpf_skb_change_proto, struct sk_buff *, skb, __be16, proto,
2165 u64, flags)
2166 {
2167 int ret;
2168
2169 if (unlikely(flags))
2170 return -EINVAL;
2171
2172 /* General idea is that this helper does the basic groundwork
2173 * needed for changing the protocol, and eBPF program fills the
2174 * rest through bpf_skb_store_bytes(), bpf_lX_csum_replace()
2175 * and other helpers, rather than passing a raw buffer here.
2176 *
2177 * The rationale is to keep this minimal and without a need to
2178 * deal with raw packet data. F.e. even if we would pass buffers
2179 * here, the program still needs to call the bpf_lX_csum_replace()
2180 * helpers anyway. Plus, this way we keep also separation of
2181 * concerns, since f.e. bpf_skb_store_bytes() should only take
2182 * care of stores.
2183 *
2184 * Currently, additional options and extension header space are
2185 * not supported, but flags register is reserved so we can adapt
2186 * that. For offloads, we mark packet as dodgy, so that headers
2187 * need to be verified first.
2188 */
2189 ret = bpf_skb_proto_xlat(skb, proto);
2190 bpf_compute_data_end(skb);
2191 return ret;
2192 }
2193
2194 static const struct bpf_func_proto bpf_skb_change_proto_proto = {
2195 .func = bpf_skb_change_proto,
2196 .gpl_only = false,
2197 .ret_type = RET_INTEGER,
2198 .arg1_type = ARG_PTR_TO_CTX,
2199 .arg2_type = ARG_ANYTHING,
2200 .arg3_type = ARG_ANYTHING,
2201 };
2202
BPF_CALL_2(bpf_skb_change_type,struct sk_buff *,skb,u32,pkt_type)2203 BPF_CALL_2(bpf_skb_change_type, struct sk_buff *, skb, u32, pkt_type)
2204 {
2205 /* We only allow a restricted subset to be changed for now. */
2206 if (unlikely(!skb_pkt_type_ok(skb->pkt_type) ||
2207 !skb_pkt_type_ok(pkt_type)))
2208 return -EINVAL;
2209
2210 skb->pkt_type = pkt_type;
2211 return 0;
2212 }
2213
2214 static const struct bpf_func_proto bpf_skb_change_type_proto = {
2215 .func = bpf_skb_change_type,
2216 .gpl_only = false,
2217 .ret_type = RET_INTEGER,
2218 .arg1_type = ARG_PTR_TO_CTX,
2219 .arg2_type = ARG_ANYTHING,
2220 };
2221
bpf_skb_net_base_len(const struct sk_buff * skb)2222 static u32 bpf_skb_net_base_len(const struct sk_buff *skb)
2223 {
2224 switch (skb->protocol) {
2225 case htons(ETH_P_IP):
2226 return sizeof(struct iphdr);
2227 case htons(ETH_P_IPV6):
2228 return sizeof(struct ipv6hdr);
2229 default:
2230 return ~0U;
2231 }
2232 }
2233
bpf_skb_net_grow(struct sk_buff * skb,u32 len_diff)2234 static int bpf_skb_net_grow(struct sk_buff *skb, u32 len_diff)
2235 {
2236 u32 off = skb_mac_header_len(skb) + bpf_skb_net_base_len(skb);
2237 int ret;
2238
2239 ret = skb_cow(skb, len_diff);
2240 if (unlikely(ret < 0))
2241 return ret;
2242
2243 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
2244 if (unlikely(ret < 0))
2245 return ret;
2246
2247 if (skb_is_gso(skb)) {
2248 /* Due to header grow, MSS needs to be downgraded. */
2249 skb_shinfo(skb)->gso_size -= len_diff;
2250 /* Header must be checked, and gso_segs recomputed. */
2251 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
2252 skb_shinfo(skb)->gso_segs = 0;
2253 }
2254
2255 return 0;
2256 }
2257
bpf_skb_net_shrink(struct sk_buff * skb,u32 len_diff)2258 static int bpf_skb_net_shrink(struct sk_buff *skb, u32 len_diff)
2259 {
2260 u32 off = skb_mac_header_len(skb) + bpf_skb_net_base_len(skb);
2261 int ret;
2262
2263 ret = skb_unclone(skb, GFP_ATOMIC);
2264 if (unlikely(ret < 0))
2265 return ret;
2266
2267 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
2268 if (unlikely(ret < 0))
2269 return ret;
2270
2271 if (skb_is_gso(skb)) {
2272 /* Due to header shrink, MSS can be upgraded. */
2273 skb_shinfo(skb)->gso_size += len_diff;
2274 /* Header must be checked, and gso_segs recomputed. */
2275 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
2276 skb_shinfo(skb)->gso_segs = 0;
2277 }
2278
2279 return 0;
2280 }
2281
__bpf_skb_max_len(const struct sk_buff * skb)2282 static u32 __bpf_skb_max_len(const struct sk_buff *skb)
2283 {
2284 return skb->dev->mtu + skb->dev->hard_header_len;
2285 }
2286
bpf_skb_adjust_net(struct sk_buff * skb,s32 len_diff)2287 static int bpf_skb_adjust_net(struct sk_buff *skb, s32 len_diff)
2288 {
2289 bool trans_same = skb->transport_header == skb->network_header;
2290 u32 len_cur, len_diff_abs = abs(len_diff);
2291 u32 len_min = bpf_skb_net_base_len(skb);
2292 u32 len_max = __bpf_skb_max_len(skb);
2293 __be16 proto = skb->protocol;
2294 bool shrink = len_diff < 0;
2295 int ret;
2296
2297 if (unlikely(len_diff_abs > 0xfffU))
2298 return -EFAULT;
2299 if (unlikely(proto != htons(ETH_P_IP) &&
2300 proto != htons(ETH_P_IPV6)))
2301 return -ENOTSUPP;
2302
2303 len_cur = skb->len - skb_network_offset(skb);
2304 if (skb_transport_header_was_set(skb) && !trans_same)
2305 len_cur = skb_network_header_len(skb);
2306 if ((shrink && (len_diff_abs >= len_cur ||
2307 len_cur - len_diff_abs < len_min)) ||
2308 (!shrink && (skb->len + len_diff_abs > len_max &&
2309 !skb_is_gso(skb))))
2310 return -ENOTSUPP;
2311
2312 ret = shrink ? bpf_skb_net_shrink(skb, len_diff_abs) :
2313 bpf_skb_net_grow(skb, len_diff_abs);
2314
2315 bpf_compute_data_end(skb);
2316 return ret;
2317 }
2318
BPF_CALL_4(bpf_skb_adjust_room,struct sk_buff *,skb,s32,len_diff,u32,mode,u64,flags)2319 BPF_CALL_4(bpf_skb_adjust_room, struct sk_buff *, skb, s32, len_diff,
2320 u32, mode, u64, flags)
2321 {
2322 if (unlikely(flags))
2323 return -EINVAL;
2324 if (likely(mode == BPF_ADJ_ROOM_NET))
2325 return bpf_skb_adjust_net(skb, len_diff);
2326
2327 return -ENOTSUPP;
2328 }
2329
2330 static const struct bpf_func_proto bpf_skb_adjust_room_proto = {
2331 .func = bpf_skb_adjust_room,
2332 .gpl_only = false,
2333 .ret_type = RET_INTEGER,
2334 .arg1_type = ARG_PTR_TO_CTX,
2335 .arg2_type = ARG_ANYTHING,
2336 .arg3_type = ARG_ANYTHING,
2337 .arg4_type = ARG_ANYTHING,
2338 };
2339
__bpf_skb_min_len(const struct sk_buff * skb)2340 static u32 __bpf_skb_min_len(const struct sk_buff *skb)
2341 {
2342 u32 min_len = skb_network_offset(skb);
2343
2344 if (skb_transport_header_was_set(skb))
2345 min_len = skb_transport_offset(skb);
2346 if (skb->ip_summed == CHECKSUM_PARTIAL)
2347 min_len = skb_checksum_start_offset(skb) +
2348 skb->csum_offset + sizeof(__sum16);
2349 return min_len;
2350 }
2351
bpf_skb_grow_rcsum(struct sk_buff * skb,unsigned int new_len)2352 static int bpf_skb_grow_rcsum(struct sk_buff *skb, unsigned int new_len)
2353 {
2354 unsigned int old_len = skb->len;
2355 int ret;
2356
2357 ret = __skb_grow_rcsum(skb, new_len);
2358 if (!ret)
2359 memset(skb->data + old_len, 0, new_len - old_len);
2360 return ret;
2361 }
2362
bpf_skb_trim_rcsum(struct sk_buff * skb,unsigned int new_len)2363 static int bpf_skb_trim_rcsum(struct sk_buff *skb, unsigned int new_len)
2364 {
2365 return __skb_trim_rcsum(skb, new_len);
2366 }
2367
BPF_CALL_3(bpf_skb_change_tail,struct sk_buff *,skb,u32,new_len,u64,flags)2368 BPF_CALL_3(bpf_skb_change_tail, struct sk_buff *, skb, u32, new_len,
2369 u64, flags)
2370 {
2371 u32 max_len = __bpf_skb_max_len(skb);
2372 u32 min_len = __bpf_skb_min_len(skb);
2373 int ret;
2374
2375 if (unlikely(flags || new_len > max_len || new_len < min_len))
2376 return -EINVAL;
2377 if (skb->encapsulation)
2378 return -ENOTSUPP;
2379
2380 /* The basic idea of this helper is that it's performing the
2381 * needed work to either grow or trim an skb, and eBPF program
2382 * rewrites the rest via helpers like bpf_skb_store_bytes(),
2383 * bpf_lX_csum_replace() and others rather than passing a raw
2384 * buffer here. This one is a slow path helper and intended
2385 * for replies with control messages.
2386 *
2387 * Like in bpf_skb_change_proto(), we want to keep this rather
2388 * minimal and without protocol specifics so that we are able
2389 * to separate concerns as in bpf_skb_store_bytes() should only
2390 * be the one responsible for writing buffers.
2391 *
2392 * It's really expected to be a slow path operation here for
2393 * control message replies, so we're implicitly linearizing,
2394 * uncloning and drop offloads from the skb by this.
2395 */
2396 ret = __bpf_try_make_writable(skb, skb->len);
2397 if (!ret) {
2398 if (new_len > skb->len)
2399 ret = bpf_skb_grow_rcsum(skb, new_len);
2400 else if (new_len < skb->len)
2401 ret = bpf_skb_trim_rcsum(skb, new_len);
2402 if (!ret && skb_is_gso(skb))
2403 skb_gso_reset(skb);
2404 }
2405
2406 bpf_compute_data_end(skb);
2407 return ret;
2408 }
2409
2410 static const struct bpf_func_proto bpf_skb_change_tail_proto = {
2411 .func = bpf_skb_change_tail,
2412 .gpl_only = false,
2413 .ret_type = RET_INTEGER,
2414 .arg1_type = ARG_PTR_TO_CTX,
2415 .arg2_type = ARG_ANYTHING,
2416 .arg3_type = ARG_ANYTHING,
2417 };
2418
BPF_CALL_3(bpf_skb_change_head,struct sk_buff *,skb,u32,head_room,u64,flags)2419 BPF_CALL_3(bpf_skb_change_head, struct sk_buff *, skb, u32, head_room,
2420 u64, flags)
2421 {
2422 u32 max_len = __bpf_skb_max_len(skb);
2423 u32 new_len = skb->len + head_room;
2424 int ret;
2425
2426 if (unlikely(flags || (!skb_is_gso(skb) && new_len > max_len) ||
2427 new_len < skb->len))
2428 return -EINVAL;
2429
2430 ret = skb_cow(skb, head_room);
2431 if (likely(!ret)) {
2432 /* Idea for this helper is that we currently only
2433 * allow to expand on mac header. This means that
2434 * skb->protocol network header, etc, stay as is.
2435 * Compared to bpf_skb_change_tail(), we're more
2436 * flexible due to not needing to linearize or
2437 * reset GSO. Intention for this helper is to be
2438 * used by an L3 skb that needs to push mac header
2439 * for redirection into L2 device.
2440 */
2441 __skb_push(skb, head_room);
2442 memset(skb->data, 0, head_room);
2443 skb_reset_mac_header(skb);
2444 }
2445
2446 bpf_compute_data_end(skb);
2447 return 0;
2448 }
2449
2450 static const struct bpf_func_proto bpf_skb_change_head_proto = {
2451 .func = bpf_skb_change_head,
2452 .gpl_only = false,
2453 .ret_type = RET_INTEGER,
2454 .arg1_type = ARG_PTR_TO_CTX,
2455 .arg2_type = ARG_ANYTHING,
2456 .arg3_type = ARG_ANYTHING,
2457 };
2458
BPF_CALL_2(bpf_xdp_adjust_head,struct xdp_buff *,xdp,int,offset)2459 BPF_CALL_2(bpf_xdp_adjust_head, struct xdp_buff *, xdp, int, offset)
2460 {
2461 void *data = xdp->data + offset;
2462
2463 if (unlikely(data < xdp->data_hard_start ||
2464 data > xdp->data_end - ETH_HLEN))
2465 return -EINVAL;
2466
2467 xdp->data = data;
2468
2469 return 0;
2470 }
2471
2472 static const struct bpf_func_proto bpf_xdp_adjust_head_proto = {
2473 .func = bpf_xdp_adjust_head,
2474 .gpl_only = false,
2475 .ret_type = RET_INTEGER,
2476 .arg1_type = ARG_PTR_TO_CTX,
2477 .arg2_type = ARG_ANYTHING,
2478 };
2479
__bpf_tx_xdp(struct net_device * dev,struct bpf_map * map,struct xdp_buff * xdp,u32 index)2480 static int __bpf_tx_xdp(struct net_device *dev,
2481 struct bpf_map *map,
2482 struct xdp_buff *xdp,
2483 u32 index)
2484 {
2485 int err;
2486
2487 if (!dev->netdev_ops->ndo_xdp_xmit) {
2488 return -EOPNOTSUPP;
2489 }
2490
2491 err = dev->netdev_ops->ndo_xdp_xmit(dev, xdp);
2492 if (err)
2493 return err;
2494 if (map)
2495 __dev_map_insert_ctx(map, index);
2496 else
2497 dev->netdev_ops->ndo_xdp_flush(dev);
2498 return 0;
2499 }
2500
xdp_do_flush_map(void)2501 void xdp_do_flush_map(void)
2502 {
2503 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
2504 struct bpf_map *map = ri->map_to_flush;
2505
2506 ri->map_to_flush = NULL;
2507 if (map)
2508 __dev_map_flush(map);
2509 }
2510 EXPORT_SYMBOL_GPL(xdp_do_flush_map);
2511
xdp_map_invalid(const struct bpf_prog * xdp_prog,unsigned long aux)2512 static inline bool xdp_map_invalid(const struct bpf_prog *xdp_prog,
2513 unsigned long aux)
2514 {
2515 return (unsigned long)xdp_prog->aux != aux;
2516 }
2517
xdp_do_redirect_map(struct net_device * dev,struct xdp_buff * xdp,struct bpf_prog * xdp_prog)2518 static int xdp_do_redirect_map(struct net_device *dev, struct xdp_buff *xdp,
2519 struct bpf_prog *xdp_prog)
2520 {
2521 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
2522 unsigned long map_owner = ri->map_owner;
2523 struct bpf_map *map = ri->map;
2524 struct net_device *fwd = NULL;
2525 u32 index = ri->ifindex;
2526 int err;
2527
2528 ri->ifindex = 0;
2529 ri->map = NULL;
2530 ri->map_owner = 0;
2531
2532 if (unlikely(xdp_map_invalid(xdp_prog, map_owner))) {
2533 err = -EFAULT;
2534 map = NULL;
2535 goto err;
2536 }
2537
2538 fwd = __dev_map_lookup_elem(map, index);
2539 if (!fwd) {
2540 err = -EINVAL;
2541 goto err;
2542 }
2543 if (ri->map_to_flush && ri->map_to_flush != map)
2544 xdp_do_flush_map();
2545
2546 err = __bpf_tx_xdp(fwd, map, xdp, index);
2547 if (unlikely(err))
2548 goto err;
2549
2550 ri->map_to_flush = map;
2551 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map, index);
2552 return 0;
2553 err:
2554 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map, index, err);
2555 return err;
2556 }
2557
xdp_do_redirect(struct net_device * dev,struct xdp_buff * xdp,struct bpf_prog * xdp_prog)2558 int xdp_do_redirect(struct net_device *dev, struct xdp_buff *xdp,
2559 struct bpf_prog *xdp_prog)
2560 {
2561 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
2562 struct net_device *fwd;
2563 u32 index = ri->ifindex;
2564 int err;
2565
2566 if (ri->map)
2567 return xdp_do_redirect_map(dev, xdp, xdp_prog);
2568
2569 fwd = dev_get_by_index_rcu(dev_net(dev), index);
2570 ri->ifindex = 0;
2571 if (unlikely(!fwd)) {
2572 err = -EINVAL;
2573 goto err;
2574 }
2575
2576 err = __bpf_tx_xdp(fwd, NULL, xdp, 0);
2577 if (unlikely(err))
2578 goto err;
2579
2580 _trace_xdp_redirect(dev, xdp_prog, index);
2581 return 0;
2582 err:
2583 _trace_xdp_redirect_err(dev, xdp_prog, index, err);
2584 return err;
2585 }
2586 EXPORT_SYMBOL_GPL(xdp_do_redirect);
2587
xdp_do_generic_redirect(struct net_device * dev,struct sk_buff * skb,struct bpf_prog * xdp_prog)2588 int xdp_do_generic_redirect(struct net_device *dev, struct sk_buff *skb,
2589 struct bpf_prog *xdp_prog)
2590 {
2591 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
2592 unsigned long map_owner = ri->map_owner;
2593 struct bpf_map *map = ri->map;
2594 struct net_device *fwd = NULL;
2595 u32 index = ri->ifindex;
2596 unsigned int len;
2597 int err = 0;
2598
2599 ri->ifindex = 0;
2600 ri->map = NULL;
2601 ri->map_owner = 0;
2602
2603 if (map) {
2604 if (unlikely(xdp_map_invalid(xdp_prog, map_owner))) {
2605 err = -EFAULT;
2606 map = NULL;
2607 goto err;
2608 }
2609 fwd = __dev_map_lookup_elem(map, index);
2610 } else {
2611 fwd = dev_get_by_index_rcu(dev_net(dev), index);
2612 }
2613 if (unlikely(!fwd)) {
2614 err = -EINVAL;
2615 goto err;
2616 }
2617
2618 if (unlikely(!(fwd->flags & IFF_UP))) {
2619 err = -ENETDOWN;
2620 goto err;
2621 }
2622
2623 len = fwd->mtu + fwd->hard_header_len + VLAN_HLEN;
2624 if (skb->len > len) {
2625 err = -EMSGSIZE;
2626 goto err;
2627 }
2628
2629 skb->dev = fwd;
2630 map ? _trace_xdp_redirect_map(dev, xdp_prog, fwd, map, index)
2631 : _trace_xdp_redirect(dev, xdp_prog, index);
2632 return 0;
2633 err:
2634 map ? _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map, index, err)
2635 : _trace_xdp_redirect_err(dev, xdp_prog, index, err);
2636 return err;
2637 }
2638 EXPORT_SYMBOL_GPL(xdp_do_generic_redirect);
2639
BPF_CALL_2(bpf_xdp_redirect,u32,ifindex,u64,flags)2640 BPF_CALL_2(bpf_xdp_redirect, u32, ifindex, u64, flags)
2641 {
2642 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
2643
2644 if (unlikely(flags))
2645 return XDP_ABORTED;
2646
2647 ri->ifindex = ifindex;
2648 ri->flags = flags;
2649 ri->map = NULL;
2650 ri->map_owner = 0;
2651
2652 return XDP_REDIRECT;
2653 }
2654
2655 static const struct bpf_func_proto bpf_xdp_redirect_proto = {
2656 .func = bpf_xdp_redirect,
2657 .gpl_only = false,
2658 .ret_type = RET_INTEGER,
2659 .arg1_type = ARG_ANYTHING,
2660 .arg2_type = ARG_ANYTHING,
2661 };
2662
BPF_CALL_4(bpf_xdp_redirect_map,struct bpf_map *,map,u32,ifindex,u64,flags,unsigned long,map_owner)2663 BPF_CALL_4(bpf_xdp_redirect_map, struct bpf_map *, map, u32, ifindex, u64, flags,
2664 unsigned long, map_owner)
2665 {
2666 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
2667
2668 if (unlikely(flags))
2669 return XDP_ABORTED;
2670
2671 ri->ifindex = ifindex;
2672 ri->flags = flags;
2673 ri->map = map;
2674 ri->map_owner = map_owner;
2675
2676 return XDP_REDIRECT;
2677 }
2678
2679 /* Note, arg4 is hidden from users and populated by the verifier
2680 * with the right pointer.
2681 */
2682 static const struct bpf_func_proto bpf_xdp_redirect_map_proto = {
2683 .func = bpf_xdp_redirect_map,
2684 .gpl_only = false,
2685 .ret_type = RET_INTEGER,
2686 .arg1_type = ARG_CONST_MAP_PTR,
2687 .arg2_type = ARG_ANYTHING,
2688 .arg3_type = ARG_ANYTHING,
2689 };
2690
bpf_helper_changes_pkt_data(void * func)2691 bool bpf_helper_changes_pkt_data(void *func)
2692 {
2693 if (func == bpf_skb_vlan_push ||
2694 func == bpf_skb_vlan_pop ||
2695 func == bpf_skb_store_bytes ||
2696 func == bpf_skb_change_proto ||
2697 func == bpf_skb_change_head ||
2698 func == bpf_skb_change_tail ||
2699 func == bpf_skb_adjust_room ||
2700 func == bpf_skb_pull_data ||
2701 func == bpf_clone_redirect ||
2702 func == bpf_l3_csum_replace ||
2703 func == bpf_l4_csum_replace ||
2704 func == bpf_xdp_adjust_head)
2705 return true;
2706
2707 return false;
2708 }
2709
bpf_skb_copy(void * dst_buff,const void * skb,unsigned long off,unsigned long len)2710 static unsigned long bpf_skb_copy(void *dst_buff, const void *skb,
2711 unsigned long off, unsigned long len)
2712 {
2713 void *ptr = skb_header_pointer(skb, off, len, dst_buff);
2714
2715 if (unlikely(!ptr))
2716 return len;
2717 if (ptr != dst_buff)
2718 memcpy(dst_buff, ptr, len);
2719
2720 return 0;
2721 }
2722
BPF_CALL_5(bpf_skb_event_output,struct sk_buff *,skb,struct bpf_map *,map,u64,flags,void *,meta,u64,meta_size)2723 BPF_CALL_5(bpf_skb_event_output, struct sk_buff *, skb, struct bpf_map *, map,
2724 u64, flags, void *, meta, u64, meta_size)
2725 {
2726 u64 skb_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
2727
2728 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
2729 return -EINVAL;
2730 if (unlikely(skb_size > skb->len))
2731 return -EFAULT;
2732
2733 return bpf_event_output(map, flags, meta, meta_size, skb, skb_size,
2734 bpf_skb_copy);
2735 }
2736
2737 static const struct bpf_func_proto bpf_skb_event_output_proto = {
2738 .func = bpf_skb_event_output,
2739 .gpl_only = true,
2740 .ret_type = RET_INTEGER,
2741 .arg1_type = ARG_PTR_TO_CTX,
2742 .arg2_type = ARG_CONST_MAP_PTR,
2743 .arg3_type = ARG_ANYTHING,
2744 .arg4_type = ARG_PTR_TO_MEM,
2745 .arg5_type = ARG_CONST_SIZE,
2746 };
2747
bpf_tunnel_key_af(u64 flags)2748 static unsigned short bpf_tunnel_key_af(u64 flags)
2749 {
2750 return flags & BPF_F_TUNINFO_IPV6 ? AF_INET6 : AF_INET;
2751 }
2752
BPF_CALL_4(bpf_skb_get_tunnel_key,struct sk_buff *,skb,struct bpf_tunnel_key *,to,u32,size,u64,flags)2753 BPF_CALL_4(bpf_skb_get_tunnel_key, struct sk_buff *, skb, struct bpf_tunnel_key *, to,
2754 u32, size, u64, flags)
2755 {
2756 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
2757 u8 compat[sizeof(struct bpf_tunnel_key)];
2758 void *to_orig = to;
2759 int err;
2760
2761 if (unlikely(!info || (flags & ~(BPF_F_TUNINFO_IPV6)))) {
2762 err = -EINVAL;
2763 goto err_clear;
2764 }
2765 if (ip_tunnel_info_af(info) != bpf_tunnel_key_af(flags)) {
2766 err = -EPROTO;
2767 goto err_clear;
2768 }
2769 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
2770 err = -EINVAL;
2771 switch (size) {
2772 case offsetof(struct bpf_tunnel_key, tunnel_label):
2773 case offsetof(struct bpf_tunnel_key, tunnel_ext):
2774 goto set_compat;
2775 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
2776 /* Fixup deprecated structure layouts here, so we have
2777 * a common path later on.
2778 */
2779 if (ip_tunnel_info_af(info) != AF_INET)
2780 goto err_clear;
2781 set_compat:
2782 to = (struct bpf_tunnel_key *)compat;
2783 break;
2784 default:
2785 goto err_clear;
2786 }
2787 }
2788
2789 to->tunnel_id = be64_to_cpu(info->key.tun_id);
2790 to->tunnel_tos = info->key.tos;
2791 to->tunnel_ttl = info->key.ttl;
2792
2793 if (flags & BPF_F_TUNINFO_IPV6) {
2794 memcpy(to->remote_ipv6, &info->key.u.ipv6.src,
2795 sizeof(to->remote_ipv6));
2796 to->tunnel_label = be32_to_cpu(info->key.label);
2797 } else {
2798 to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src);
2799 }
2800
2801 if (unlikely(size != sizeof(struct bpf_tunnel_key)))
2802 memcpy(to_orig, to, size);
2803
2804 return 0;
2805 err_clear:
2806 memset(to_orig, 0, size);
2807 return err;
2808 }
2809
2810 static const struct bpf_func_proto bpf_skb_get_tunnel_key_proto = {
2811 .func = bpf_skb_get_tunnel_key,
2812 .gpl_only = false,
2813 .ret_type = RET_INTEGER,
2814 .arg1_type = ARG_PTR_TO_CTX,
2815 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
2816 .arg3_type = ARG_CONST_SIZE,
2817 .arg4_type = ARG_ANYTHING,
2818 };
2819
BPF_CALL_3(bpf_skb_get_tunnel_opt,struct sk_buff *,skb,u8 *,to,u32,size)2820 BPF_CALL_3(bpf_skb_get_tunnel_opt, struct sk_buff *, skb, u8 *, to, u32, size)
2821 {
2822 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
2823 int err;
2824
2825 if (unlikely(!info ||
2826 !(info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))) {
2827 err = -ENOENT;
2828 goto err_clear;
2829 }
2830 if (unlikely(size < info->options_len)) {
2831 err = -ENOMEM;
2832 goto err_clear;
2833 }
2834
2835 ip_tunnel_info_opts_get(to, info);
2836 if (size > info->options_len)
2837 memset(to + info->options_len, 0, size - info->options_len);
2838
2839 return info->options_len;
2840 err_clear:
2841 memset(to, 0, size);
2842 return err;
2843 }
2844
2845 static const struct bpf_func_proto bpf_skb_get_tunnel_opt_proto = {
2846 .func = bpf_skb_get_tunnel_opt,
2847 .gpl_only = false,
2848 .ret_type = RET_INTEGER,
2849 .arg1_type = ARG_PTR_TO_CTX,
2850 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
2851 .arg3_type = ARG_CONST_SIZE,
2852 };
2853
2854 static struct metadata_dst __percpu *md_dst;
2855
BPF_CALL_4(bpf_skb_set_tunnel_key,struct sk_buff *,skb,const struct bpf_tunnel_key *,from,u32,size,u64,flags)2856 BPF_CALL_4(bpf_skb_set_tunnel_key, struct sk_buff *, skb,
2857 const struct bpf_tunnel_key *, from, u32, size, u64, flags)
2858 {
2859 struct metadata_dst *md = this_cpu_ptr(md_dst);
2860 u8 compat[sizeof(struct bpf_tunnel_key)];
2861 struct ip_tunnel_info *info;
2862
2863 if (unlikely(flags & ~(BPF_F_TUNINFO_IPV6 | BPF_F_ZERO_CSUM_TX |
2864 BPF_F_DONT_FRAGMENT)))
2865 return -EINVAL;
2866 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
2867 switch (size) {
2868 case offsetof(struct bpf_tunnel_key, tunnel_label):
2869 case offsetof(struct bpf_tunnel_key, tunnel_ext):
2870 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
2871 /* Fixup deprecated structure layouts here, so we have
2872 * a common path later on.
2873 */
2874 memcpy(compat, from, size);
2875 memset(compat + size, 0, sizeof(compat) - size);
2876 from = (const struct bpf_tunnel_key *) compat;
2877 break;
2878 default:
2879 return -EINVAL;
2880 }
2881 }
2882 if (unlikely((!(flags & BPF_F_TUNINFO_IPV6) && from->tunnel_label) ||
2883 from->tunnel_ext))
2884 return -EINVAL;
2885
2886 skb_dst_drop(skb);
2887 dst_hold((struct dst_entry *) md);
2888 skb_dst_set(skb, (struct dst_entry *) md);
2889
2890 info = &md->u.tun_info;
2891 info->mode = IP_TUNNEL_INFO_TX;
2892
2893 info->key.tun_flags = TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_NOCACHE;
2894 if (flags & BPF_F_DONT_FRAGMENT)
2895 info->key.tun_flags |= TUNNEL_DONT_FRAGMENT;
2896
2897 info->key.tun_id = cpu_to_be64(from->tunnel_id);
2898 info->key.tos = from->tunnel_tos;
2899 info->key.ttl = from->tunnel_ttl;
2900
2901 if (flags & BPF_F_TUNINFO_IPV6) {
2902 info->mode |= IP_TUNNEL_INFO_IPV6;
2903 memcpy(&info->key.u.ipv6.dst, from->remote_ipv6,
2904 sizeof(from->remote_ipv6));
2905 info->key.label = cpu_to_be32(from->tunnel_label) &
2906 IPV6_FLOWLABEL_MASK;
2907 } else {
2908 info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4);
2909 if (flags & BPF_F_ZERO_CSUM_TX)
2910 info->key.tun_flags &= ~TUNNEL_CSUM;
2911 }
2912
2913 return 0;
2914 }
2915
2916 static const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = {
2917 .func = bpf_skb_set_tunnel_key,
2918 .gpl_only = false,
2919 .ret_type = RET_INTEGER,
2920 .arg1_type = ARG_PTR_TO_CTX,
2921 .arg2_type = ARG_PTR_TO_MEM,
2922 .arg3_type = ARG_CONST_SIZE,
2923 .arg4_type = ARG_ANYTHING,
2924 };
2925
BPF_CALL_3(bpf_skb_set_tunnel_opt,struct sk_buff *,skb,const u8 *,from,u32,size)2926 BPF_CALL_3(bpf_skb_set_tunnel_opt, struct sk_buff *, skb,
2927 const u8 *, from, u32, size)
2928 {
2929 struct ip_tunnel_info *info = skb_tunnel_info(skb);
2930 const struct metadata_dst *md = this_cpu_ptr(md_dst);
2931
2932 if (unlikely(info != &md->u.tun_info || (size & (sizeof(u32) - 1))))
2933 return -EINVAL;
2934 if (unlikely(size > IP_TUNNEL_OPTS_MAX))
2935 return -ENOMEM;
2936
2937 ip_tunnel_info_opts_set(info, from, size);
2938
2939 return 0;
2940 }
2941
2942 static const struct bpf_func_proto bpf_skb_set_tunnel_opt_proto = {
2943 .func = bpf_skb_set_tunnel_opt,
2944 .gpl_only = false,
2945 .ret_type = RET_INTEGER,
2946 .arg1_type = ARG_PTR_TO_CTX,
2947 .arg2_type = ARG_PTR_TO_MEM,
2948 .arg3_type = ARG_CONST_SIZE,
2949 };
2950
2951 static const struct bpf_func_proto *
bpf_get_skb_set_tunnel_proto(enum bpf_func_id which)2952 bpf_get_skb_set_tunnel_proto(enum bpf_func_id which)
2953 {
2954 if (!md_dst) {
2955 /* Race is not possible, since it's called from verifier
2956 * that is holding verifier mutex.
2957 */
2958 md_dst = metadata_dst_alloc_percpu(IP_TUNNEL_OPTS_MAX,
2959 METADATA_IP_TUNNEL,
2960 GFP_KERNEL);
2961 if (!md_dst)
2962 return NULL;
2963 }
2964
2965 switch (which) {
2966 case BPF_FUNC_skb_set_tunnel_key:
2967 return &bpf_skb_set_tunnel_key_proto;
2968 case BPF_FUNC_skb_set_tunnel_opt:
2969 return &bpf_skb_set_tunnel_opt_proto;
2970 default:
2971 return NULL;
2972 }
2973 }
2974
BPF_CALL_3(bpf_skb_under_cgroup,struct sk_buff *,skb,struct bpf_map *,map,u32,idx)2975 BPF_CALL_3(bpf_skb_under_cgroup, struct sk_buff *, skb, struct bpf_map *, map,
2976 u32, idx)
2977 {
2978 struct bpf_array *array = container_of(map, struct bpf_array, map);
2979 struct cgroup *cgrp;
2980 struct sock *sk;
2981
2982 sk = skb_to_full_sk(skb);
2983 if (!sk || !sk_fullsock(sk))
2984 return -ENOENT;
2985 if (unlikely(idx >= array->map.max_entries))
2986 return -E2BIG;
2987
2988 cgrp = READ_ONCE(array->ptrs[idx]);
2989 if (unlikely(!cgrp))
2990 return -EAGAIN;
2991
2992 return sk_under_cgroup_hierarchy(sk, cgrp);
2993 }
2994
2995 static const struct bpf_func_proto bpf_skb_under_cgroup_proto = {
2996 .func = bpf_skb_under_cgroup,
2997 .gpl_only = false,
2998 .ret_type = RET_INTEGER,
2999 .arg1_type = ARG_PTR_TO_CTX,
3000 .arg2_type = ARG_CONST_MAP_PTR,
3001 .arg3_type = ARG_ANYTHING,
3002 };
3003
bpf_xdp_copy(void * dst_buff,const void * src_buff,unsigned long off,unsigned long len)3004 static unsigned long bpf_xdp_copy(void *dst_buff, const void *src_buff,
3005 unsigned long off, unsigned long len)
3006 {
3007 memcpy(dst_buff, src_buff + off, len);
3008 return 0;
3009 }
3010
BPF_CALL_5(bpf_xdp_event_output,struct xdp_buff *,xdp,struct bpf_map *,map,u64,flags,void *,meta,u64,meta_size)3011 BPF_CALL_5(bpf_xdp_event_output, struct xdp_buff *, xdp, struct bpf_map *, map,
3012 u64, flags, void *, meta, u64, meta_size)
3013 {
3014 u64 xdp_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
3015
3016 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
3017 return -EINVAL;
3018 if (unlikely(xdp_size > (unsigned long)(xdp->data_end - xdp->data)))
3019 return -EFAULT;
3020
3021 return bpf_event_output(map, flags, meta, meta_size, xdp->data,
3022 xdp_size, bpf_xdp_copy);
3023 }
3024
3025 static const struct bpf_func_proto bpf_xdp_event_output_proto = {
3026 .func = bpf_xdp_event_output,
3027 .gpl_only = true,
3028 .ret_type = RET_INTEGER,
3029 .arg1_type = ARG_PTR_TO_CTX,
3030 .arg2_type = ARG_CONST_MAP_PTR,
3031 .arg3_type = ARG_ANYTHING,
3032 .arg4_type = ARG_PTR_TO_MEM,
3033 .arg5_type = ARG_CONST_SIZE,
3034 };
3035
BPF_CALL_1(bpf_get_socket_cookie,struct sk_buff *,skb)3036 BPF_CALL_1(bpf_get_socket_cookie, struct sk_buff *, skb)
3037 {
3038 return skb->sk ? sock_gen_cookie(skb->sk) : 0;
3039 }
3040
3041 static const struct bpf_func_proto bpf_get_socket_cookie_proto = {
3042 .func = bpf_get_socket_cookie,
3043 .gpl_only = false,
3044 .ret_type = RET_INTEGER,
3045 .arg1_type = ARG_PTR_TO_CTX,
3046 };
3047
BPF_CALL_1(bpf_get_socket_uid,struct sk_buff *,skb)3048 BPF_CALL_1(bpf_get_socket_uid, struct sk_buff *, skb)
3049 {
3050 struct sock *sk = sk_to_full_sk(skb->sk);
3051 kuid_t kuid;
3052
3053 if (!sk || !sk_fullsock(sk))
3054 return overflowuid;
3055 kuid = sock_net_uid(sock_net(sk), sk);
3056 return from_kuid_munged(sock_net(sk)->user_ns, kuid);
3057 }
3058
3059 static const struct bpf_func_proto bpf_get_socket_uid_proto = {
3060 .func = bpf_get_socket_uid,
3061 .gpl_only = false,
3062 .ret_type = RET_INTEGER,
3063 .arg1_type = ARG_PTR_TO_CTX,
3064 };
3065
BPF_CALL_5(bpf_setsockopt,struct bpf_sock_ops_kern *,bpf_sock,int,level,int,optname,char *,optval,int,optlen)3066 BPF_CALL_5(bpf_setsockopt, struct bpf_sock_ops_kern *, bpf_sock,
3067 int, level, int, optname, char *, optval, int, optlen)
3068 {
3069 struct sock *sk = bpf_sock->sk;
3070 int ret = 0;
3071 int val;
3072
3073 if (!sk_fullsock(sk))
3074 return -EINVAL;
3075
3076 if (level == SOL_SOCKET) {
3077 if (optlen != sizeof(int))
3078 return -EINVAL;
3079 val = *((int *)optval);
3080
3081 /* Only some socketops are supported */
3082 switch (optname) {
3083 case SO_RCVBUF:
3084 val = min_t(u32, val, sysctl_rmem_max);
3085 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
3086 sk->sk_rcvbuf = max_t(int, val * 2, SOCK_MIN_RCVBUF);
3087 break;
3088 case SO_SNDBUF:
3089 val = min_t(u32, val, sysctl_wmem_max);
3090 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
3091 sk->sk_sndbuf = max_t(int, val * 2, SOCK_MIN_SNDBUF);
3092 break;
3093 case SO_MAX_PACING_RATE:
3094 sk->sk_max_pacing_rate = val;
3095 sk->sk_pacing_rate = min(sk->sk_pacing_rate,
3096 sk->sk_max_pacing_rate);
3097 break;
3098 case SO_PRIORITY:
3099 sk->sk_priority = val;
3100 break;
3101 case SO_RCVLOWAT:
3102 if (val < 0)
3103 val = INT_MAX;
3104 sk->sk_rcvlowat = val ? : 1;
3105 break;
3106 case SO_MARK:
3107 if (sk->sk_mark != val) {
3108 sk->sk_mark = val;
3109 sk_dst_reset(sk);
3110 }
3111 break;
3112 default:
3113 ret = -EINVAL;
3114 }
3115 #ifdef CONFIG_INET
3116 } else if (level == SOL_TCP &&
3117 sk->sk_prot->setsockopt == tcp_setsockopt) {
3118 if (optname == TCP_CONGESTION) {
3119 char name[TCP_CA_NAME_MAX];
3120 bool reinit = bpf_sock->op > BPF_SOCK_OPS_NEEDS_ECN;
3121
3122 strncpy(name, optval, min_t(long, optlen,
3123 TCP_CA_NAME_MAX-1));
3124 name[TCP_CA_NAME_MAX-1] = 0;
3125 ret = tcp_set_congestion_control(sk, name, false,
3126 reinit, true);
3127 } else {
3128 struct tcp_sock *tp = tcp_sk(sk);
3129
3130 if (optlen != sizeof(int))
3131 return -EINVAL;
3132
3133 val = *((int *)optval);
3134 /* Only some options are supported */
3135 switch (optname) {
3136 case TCP_BPF_IW:
3137 if (val <= 0 || tp->data_segs_out > tp->syn_data)
3138 ret = -EINVAL;
3139 else
3140 tp->snd_cwnd = val;
3141 break;
3142 case TCP_BPF_SNDCWND_CLAMP:
3143 if (val <= 0) {
3144 ret = -EINVAL;
3145 } else {
3146 tp->snd_cwnd_clamp = val;
3147 tp->snd_ssthresh = val;
3148 }
3149 break;
3150 default:
3151 ret = -EINVAL;
3152 }
3153 }
3154 #endif
3155 } else {
3156 ret = -EINVAL;
3157 }
3158 return ret;
3159 }
3160
3161 static const struct bpf_func_proto bpf_setsockopt_proto = {
3162 .func = bpf_setsockopt,
3163 .gpl_only = true,
3164 .ret_type = RET_INTEGER,
3165 .arg1_type = ARG_PTR_TO_CTX,
3166 .arg2_type = ARG_ANYTHING,
3167 .arg3_type = ARG_ANYTHING,
3168 .arg4_type = ARG_PTR_TO_MEM,
3169 .arg5_type = ARG_CONST_SIZE,
3170 };
3171
3172 static const struct bpf_func_proto *
bpf_base_func_proto(enum bpf_func_id func_id)3173 bpf_base_func_proto(enum bpf_func_id func_id)
3174 {
3175 switch (func_id) {
3176 case BPF_FUNC_map_lookup_elem:
3177 return &bpf_map_lookup_elem_proto;
3178 case BPF_FUNC_map_update_elem:
3179 return &bpf_map_update_elem_proto;
3180 case BPF_FUNC_map_delete_elem:
3181 return &bpf_map_delete_elem_proto;
3182 case BPF_FUNC_get_prandom_u32:
3183 return &bpf_get_prandom_u32_proto;
3184 case BPF_FUNC_get_smp_processor_id:
3185 return &bpf_get_raw_smp_processor_id_proto;
3186 case BPF_FUNC_get_numa_node_id:
3187 return &bpf_get_numa_node_id_proto;
3188 case BPF_FUNC_tail_call:
3189 return &bpf_tail_call_proto;
3190 case BPF_FUNC_ktime_get_ns:
3191 return &bpf_ktime_get_ns_proto;
3192 case BPF_FUNC_trace_printk:
3193 if (capable(CAP_SYS_ADMIN))
3194 return bpf_get_trace_printk_proto();
3195 default:
3196 return NULL;
3197 }
3198 }
3199
3200 static const struct bpf_func_proto *
sock_filter_func_proto(enum bpf_func_id func_id)3201 sock_filter_func_proto(enum bpf_func_id func_id)
3202 {
3203 switch (func_id) {
3204 /* inet and inet6 sockets are created in a process
3205 * context so there is always a valid uid/gid
3206 */
3207 case BPF_FUNC_get_current_uid_gid:
3208 return &bpf_get_current_uid_gid_proto;
3209 default:
3210 return bpf_base_func_proto(func_id);
3211 }
3212 }
3213
3214 static const struct bpf_func_proto *
sk_filter_func_proto(enum bpf_func_id func_id)3215 sk_filter_func_proto(enum bpf_func_id func_id)
3216 {
3217 switch (func_id) {
3218 case BPF_FUNC_skb_load_bytes:
3219 return &bpf_skb_load_bytes_proto;
3220 case BPF_FUNC_get_socket_cookie:
3221 return &bpf_get_socket_cookie_proto;
3222 case BPF_FUNC_get_socket_uid:
3223 return &bpf_get_socket_uid_proto;
3224 default:
3225 return bpf_base_func_proto(func_id);
3226 }
3227 }
3228
3229 static const struct bpf_func_proto *
tc_cls_act_func_proto(enum bpf_func_id func_id)3230 tc_cls_act_func_proto(enum bpf_func_id func_id)
3231 {
3232 switch (func_id) {
3233 case BPF_FUNC_skb_store_bytes:
3234 return &bpf_skb_store_bytes_proto;
3235 case BPF_FUNC_skb_load_bytes:
3236 return &bpf_skb_load_bytes_proto;
3237 case BPF_FUNC_skb_pull_data:
3238 return &bpf_skb_pull_data_proto;
3239 case BPF_FUNC_csum_diff:
3240 return &bpf_csum_diff_proto;
3241 case BPF_FUNC_csum_update:
3242 return &bpf_csum_update_proto;
3243 case BPF_FUNC_l3_csum_replace:
3244 return &bpf_l3_csum_replace_proto;
3245 case BPF_FUNC_l4_csum_replace:
3246 return &bpf_l4_csum_replace_proto;
3247 case BPF_FUNC_clone_redirect:
3248 return &bpf_clone_redirect_proto;
3249 case BPF_FUNC_get_cgroup_classid:
3250 return &bpf_get_cgroup_classid_proto;
3251 case BPF_FUNC_skb_vlan_push:
3252 return &bpf_skb_vlan_push_proto;
3253 case BPF_FUNC_skb_vlan_pop:
3254 return &bpf_skb_vlan_pop_proto;
3255 case BPF_FUNC_skb_change_proto:
3256 return &bpf_skb_change_proto_proto;
3257 case BPF_FUNC_skb_change_type:
3258 return &bpf_skb_change_type_proto;
3259 case BPF_FUNC_skb_adjust_room:
3260 return &bpf_skb_adjust_room_proto;
3261 case BPF_FUNC_skb_change_tail:
3262 return &bpf_skb_change_tail_proto;
3263 case BPF_FUNC_skb_change_head:
3264 return &bpf_skb_change_head_proto;
3265 case BPF_FUNC_skb_get_tunnel_key:
3266 return &bpf_skb_get_tunnel_key_proto;
3267 case BPF_FUNC_skb_set_tunnel_key:
3268 return bpf_get_skb_set_tunnel_proto(func_id);
3269 case BPF_FUNC_skb_get_tunnel_opt:
3270 return &bpf_skb_get_tunnel_opt_proto;
3271 case BPF_FUNC_skb_set_tunnel_opt:
3272 return bpf_get_skb_set_tunnel_proto(func_id);
3273 case BPF_FUNC_redirect:
3274 return &bpf_redirect_proto;
3275 case BPF_FUNC_get_route_realm:
3276 return &bpf_get_route_realm_proto;
3277 case BPF_FUNC_get_hash_recalc:
3278 return &bpf_get_hash_recalc_proto;
3279 case BPF_FUNC_set_hash_invalid:
3280 return &bpf_set_hash_invalid_proto;
3281 case BPF_FUNC_set_hash:
3282 return &bpf_set_hash_proto;
3283 case BPF_FUNC_perf_event_output:
3284 return &bpf_skb_event_output_proto;
3285 case BPF_FUNC_get_smp_processor_id:
3286 return &bpf_get_smp_processor_id_proto;
3287 case BPF_FUNC_skb_under_cgroup:
3288 return &bpf_skb_under_cgroup_proto;
3289 case BPF_FUNC_get_socket_cookie:
3290 return &bpf_get_socket_cookie_proto;
3291 case BPF_FUNC_get_socket_uid:
3292 return &bpf_get_socket_uid_proto;
3293 default:
3294 return bpf_base_func_proto(func_id);
3295 }
3296 }
3297
3298 static const struct bpf_func_proto *
xdp_func_proto(enum bpf_func_id func_id)3299 xdp_func_proto(enum bpf_func_id func_id)
3300 {
3301 switch (func_id) {
3302 case BPF_FUNC_perf_event_output:
3303 return &bpf_xdp_event_output_proto;
3304 case BPF_FUNC_get_smp_processor_id:
3305 return &bpf_get_smp_processor_id_proto;
3306 case BPF_FUNC_xdp_adjust_head:
3307 return &bpf_xdp_adjust_head_proto;
3308 case BPF_FUNC_redirect:
3309 return &bpf_xdp_redirect_proto;
3310 case BPF_FUNC_redirect_map:
3311 return &bpf_xdp_redirect_map_proto;
3312 default:
3313 return bpf_base_func_proto(func_id);
3314 }
3315 }
3316
3317 static const struct bpf_func_proto *
lwt_inout_func_proto(enum bpf_func_id func_id)3318 lwt_inout_func_proto(enum bpf_func_id func_id)
3319 {
3320 switch (func_id) {
3321 case BPF_FUNC_skb_load_bytes:
3322 return &bpf_skb_load_bytes_proto;
3323 case BPF_FUNC_skb_pull_data:
3324 return &bpf_skb_pull_data_proto;
3325 case BPF_FUNC_csum_diff:
3326 return &bpf_csum_diff_proto;
3327 case BPF_FUNC_get_cgroup_classid:
3328 return &bpf_get_cgroup_classid_proto;
3329 case BPF_FUNC_get_route_realm:
3330 return &bpf_get_route_realm_proto;
3331 case BPF_FUNC_get_hash_recalc:
3332 return &bpf_get_hash_recalc_proto;
3333 case BPF_FUNC_perf_event_output:
3334 return &bpf_skb_event_output_proto;
3335 case BPF_FUNC_get_smp_processor_id:
3336 return &bpf_get_smp_processor_id_proto;
3337 case BPF_FUNC_skb_under_cgroup:
3338 return &bpf_skb_under_cgroup_proto;
3339 default:
3340 return bpf_base_func_proto(func_id);
3341 }
3342 }
3343
3344 static const struct bpf_func_proto *
sock_ops_func_proto(enum bpf_func_id func_id)3345 sock_ops_func_proto(enum bpf_func_id func_id)
3346 {
3347 switch (func_id) {
3348 case BPF_FUNC_setsockopt:
3349 return &bpf_setsockopt_proto;
3350 case BPF_FUNC_sock_map_update:
3351 return &bpf_sock_map_update_proto;
3352 default:
3353 return bpf_base_func_proto(func_id);
3354 }
3355 }
3356
sk_skb_func_proto(enum bpf_func_id func_id)3357 static const struct bpf_func_proto *sk_skb_func_proto(enum bpf_func_id func_id)
3358 {
3359 switch (func_id) {
3360 case BPF_FUNC_skb_store_bytes:
3361 return &bpf_skb_store_bytes_proto;
3362 case BPF_FUNC_skb_load_bytes:
3363 return &bpf_skb_load_bytes_proto;
3364 case BPF_FUNC_skb_pull_data:
3365 return &bpf_skb_pull_data_proto;
3366 case BPF_FUNC_skb_change_tail:
3367 return &bpf_skb_change_tail_proto;
3368 case BPF_FUNC_skb_change_head:
3369 return &bpf_skb_change_head_proto;
3370 case BPF_FUNC_get_socket_cookie:
3371 return &bpf_get_socket_cookie_proto;
3372 case BPF_FUNC_get_socket_uid:
3373 return &bpf_get_socket_uid_proto;
3374 case BPF_FUNC_sk_redirect_map:
3375 return &bpf_sk_redirect_map_proto;
3376 default:
3377 return bpf_base_func_proto(func_id);
3378 }
3379 }
3380
3381 static const struct bpf_func_proto *
lwt_xmit_func_proto(enum bpf_func_id func_id)3382 lwt_xmit_func_proto(enum bpf_func_id func_id)
3383 {
3384 switch (func_id) {
3385 case BPF_FUNC_skb_get_tunnel_key:
3386 return &bpf_skb_get_tunnel_key_proto;
3387 case BPF_FUNC_skb_set_tunnel_key:
3388 return bpf_get_skb_set_tunnel_proto(func_id);
3389 case BPF_FUNC_skb_get_tunnel_opt:
3390 return &bpf_skb_get_tunnel_opt_proto;
3391 case BPF_FUNC_skb_set_tunnel_opt:
3392 return bpf_get_skb_set_tunnel_proto(func_id);
3393 case BPF_FUNC_redirect:
3394 return &bpf_redirect_proto;
3395 case BPF_FUNC_clone_redirect:
3396 return &bpf_clone_redirect_proto;
3397 case BPF_FUNC_skb_change_tail:
3398 return &bpf_skb_change_tail_proto;
3399 case BPF_FUNC_skb_change_head:
3400 return &bpf_skb_change_head_proto;
3401 case BPF_FUNC_skb_store_bytes:
3402 return &bpf_skb_store_bytes_proto;
3403 case BPF_FUNC_csum_update:
3404 return &bpf_csum_update_proto;
3405 case BPF_FUNC_l3_csum_replace:
3406 return &bpf_l3_csum_replace_proto;
3407 case BPF_FUNC_l4_csum_replace:
3408 return &bpf_l4_csum_replace_proto;
3409 case BPF_FUNC_set_hash_invalid:
3410 return &bpf_set_hash_invalid_proto;
3411 default:
3412 return lwt_inout_func_proto(func_id);
3413 }
3414 }
3415
bpf_skb_is_valid_access(int off,int size,enum bpf_access_type type,struct bpf_insn_access_aux * info)3416 static bool bpf_skb_is_valid_access(int off, int size, enum bpf_access_type type,
3417 struct bpf_insn_access_aux *info)
3418 {
3419 const int size_default = sizeof(__u32);
3420
3421 if (off < 0 || off >= sizeof(struct __sk_buff))
3422 return false;
3423
3424 /* The verifier guarantees that size > 0. */
3425 if (off % size != 0)
3426 return false;
3427
3428 switch (off) {
3429 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
3430 if (off + size > offsetofend(struct __sk_buff, cb[4]))
3431 return false;
3432 break;
3433 case bpf_ctx_range_till(struct __sk_buff, remote_ip6[0], remote_ip6[3]):
3434 case bpf_ctx_range_till(struct __sk_buff, local_ip6[0], local_ip6[3]):
3435 case bpf_ctx_range_till(struct __sk_buff, remote_ip4, remote_ip4):
3436 case bpf_ctx_range_till(struct __sk_buff, local_ip4, local_ip4):
3437 case bpf_ctx_range(struct __sk_buff, data):
3438 case bpf_ctx_range(struct __sk_buff, data_end):
3439 if (size != size_default)
3440 return false;
3441 break;
3442 default:
3443 /* Only narrow read access allowed for now. */
3444 if (type == BPF_WRITE) {
3445 if (size != size_default)
3446 return false;
3447 } else {
3448 bpf_ctx_record_field_size(info, size_default);
3449 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
3450 return false;
3451 }
3452 }
3453
3454 return true;
3455 }
3456
sk_filter_is_valid_access(int off,int size,enum bpf_access_type type,struct bpf_insn_access_aux * info)3457 static bool sk_filter_is_valid_access(int off, int size,
3458 enum bpf_access_type type,
3459 struct bpf_insn_access_aux *info)
3460 {
3461 switch (off) {
3462 case bpf_ctx_range(struct __sk_buff, tc_classid):
3463 case bpf_ctx_range(struct __sk_buff, data):
3464 case bpf_ctx_range(struct __sk_buff, data_end):
3465 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
3466 return false;
3467 }
3468
3469 if (type == BPF_WRITE) {
3470 switch (off) {
3471 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
3472 break;
3473 default:
3474 return false;
3475 }
3476 }
3477
3478 return bpf_skb_is_valid_access(off, size, type, info);
3479 }
3480
lwt_is_valid_access(int off,int size,enum bpf_access_type type,struct bpf_insn_access_aux * info)3481 static bool lwt_is_valid_access(int off, int size,
3482 enum bpf_access_type type,
3483 struct bpf_insn_access_aux *info)
3484 {
3485 switch (off) {
3486 case bpf_ctx_range(struct __sk_buff, tc_classid):
3487 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
3488 return false;
3489 }
3490
3491 if (type == BPF_WRITE) {
3492 switch (off) {
3493 case bpf_ctx_range(struct __sk_buff, mark):
3494 case bpf_ctx_range(struct __sk_buff, priority):
3495 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
3496 break;
3497 default:
3498 return false;
3499 }
3500 }
3501
3502 switch (off) {
3503 case bpf_ctx_range(struct __sk_buff, data):
3504 info->reg_type = PTR_TO_PACKET;
3505 break;
3506 case bpf_ctx_range(struct __sk_buff, data_end):
3507 info->reg_type = PTR_TO_PACKET_END;
3508 break;
3509 }
3510
3511 return bpf_skb_is_valid_access(off, size, type, info);
3512 }
3513
sock_filter_is_valid_access(int off,int size,enum bpf_access_type type,struct bpf_insn_access_aux * info)3514 static bool sock_filter_is_valid_access(int off, int size,
3515 enum bpf_access_type type,
3516 struct bpf_insn_access_aux *info)
3517 {
3518 if (type == BPF_WRITE) {
3519 switch (off) {
3520 case offsetof(struct bpf_sock, bound_dev_if):
3521 case offsetof(struct bpf_sock, mark):
3522 case offsetof(struct bpf_sock, priority):
3523 break;
3524 default:
3525 return false;
3526 }
3527 }
3528
3529 if (off < 0 || off + size > sizeof(struct bpf_sock))
3530 return false;
3531 /* The verifier guarantees that size > 0. */
3532 if (off % size != 0)
3533 return false;
3534 if (size != sizeof(__u32))
3535 return false;
3536
3537 return true;
3538 }
3539
bpf_unclone_prologue(struct bpf_insn * insn_buf,bool direct_write,const struct bpf_prog * prog,int drop_verdict)3540 static int bpf_unclone_prologue(struct bpf_insn *insn_buf, bool direct_write,
3541 const struct bpf_prog *prog, int drop_verdict)
3542 {
3543 struct bpf_insn *insn = insn_buf;
3544
3545 if (!direct_write)
3546 return 0;
3547
3548 /* if (!skb->cloned)
3549 * goto start;
3550 *
3551 * (Fast-path, otherwise approximation that we might be
3552 * a clone, do the rest in helper.)
3553 */
3554 *insn++ = BPF_LDX_MEM(BPF_B, BPF_REG_6, BPF_REG_1, CLONED_OFFSET());
3555 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_6, CLONED_MASK);
3556 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 7);
3557
3558 /* ret = bpf_skb_pull_data(skb, 0); */
3559 *insn++ = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);
3560 *insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_2, BPF_REG_2);
3561 *insn++ = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
3562 BPF_FUNC_skb_pull_data);
3563 /* if (!ret)
3564 * goto restore;
3565 * return TC_ACT_SHOT;
3566 */
3567 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2);
3568 *insn++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_0, drop_verdict);
3569 *insn++ = BPF_EXIT_INSN();
3570
3571 /* restore: */
3572 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6);
3573 /* start: */
3574 *insn++ = prog->insnsi[0];
3575
3576 return insn - insn_buf;
3577 }
3578
tc_cls_act_prologue(struct bpf_insn * insn_buf,bool direct_write,const struct bpf_prog * prog)3579 static int tc_cls_act_prologue(struct bpf_insn *insn_buf, bool direct_write,
3580 const struct bpf_prog *prog)
3581 {
3582 return bpf_unclone_prologue(insn_buf, direct_write, prog, TC_ACT_SHOT);
3583 }
3584
tc_cls_act_is_valid_access(int off,int size,enum bpf_access_type type,struct bpf_insn_access_aux * info)3585 static bool tc_cls_act_is_valid_access(int off, int size,
3586 enum bpf_access_type type,
3587 struct bpf_insn_access_aux *info)
3588 {
3589 if (type == BPF_WRITE) {
3590 switch (off) {
3591 case bpf_ctx_range(struct __sk_buff, mark):
3592 case bpf_ctx_range(struct __sk_buff, tc_index):
3593 case bpf_ctx_range(struct __sk_buff, priority):
3594 case bpf_ctx_range(struct __sk_buff, tc_classid):
3595 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
3596 break;
3597 default:
3598 return false;
3599 }
3600 }
3601
3602 switch (off) {
3603 case bpf_ctx_range(struct __sk_buff, data):
3604 info->reg_type = PTR_TO_PACKET;
3605 break;
3606 case bpf_ctx_range(struct __sk_buff, data_end):
3607 info->reg_type = PTR_TO_PACKET_END;
3608 break;
3609 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
3610 return false;
3611 }
3612
3613 return bpf_skb_is_valid_access(off, size, type, info);
3614 }
3615
__is_valid_xdp_access(int off,int size)3616 static bool __is_valid_xdp_access(int off, int size)
3617 {
3618 if (off < 0 || off >= sizeof(struct xdp_md))
3619 return false;
3620 if (off % size != 0)
3621 return false;
3622 if (size != sizeof(__u32))
3623 return false;
3624
3625 return true;
3626 }
3627
xdp_is_valid_access(int off,int size,enum bpf_access_type type,struct bpf_insn_access_aux * info)3628 static bool xdp_is_valid_access(int off, int size,
3629 enum bpf_access_type type,
3630 struct bpf_insn_access_aux *info)
3631 {
3632 if (type == BPF_WRITE)
3633 return false;
3634
3635 switch (off) {
3636 case offsetof(struct xdp_md, data):
3637 info->reg_type = PTR_TO_PACKET;
3638 break;
3639 case offsetof(struct xdp_md, data_end):
3640 info->reg_type = PTR_TO_PACKET_END;
3641 break;
3642 }
3643
3644 return __is_valid_xdp_access(off, size);
3645 }
3646
bpf_warn_invalid_xdp_action(u32 act)3647 void bpf_warn_invalid_xdp_action(u32 act)
3648 {
3649 const u32 act_max = XDP_REDIRECT;
3650
3651 WARN_ONCE(1, "%s XDP return value %u, expect packet loss!\n",
3652 act > act_max ? "Illegal" : "Driver unsupported",
3653 act);
3654 }
3655 EXPORT_SYMBOL_GPL(bpf_warn_invalid_xdp_action);
3656
__is_valid_sock_ops_access(int off,int size)3657 static bool __is_valid_sock_ops_access(int off, int size)
3658 {
3659 if (off < 0 || off >= sizeof(struct bpf_sock_ops))
3660 return false;
3661 /* The verifier guarantees that size > 0. */
3662 if (off % size != 0)
3663 return false;
3664 if (size != sizeof(__u32))
3665 return false;
3666
3667 return true;
3668 }
3669
sock_ops_is_valid_access(int off,int size,enum bpf_access_type type,struct bpf_insn_access_aux * info)3670 static bool sock_ops_is_valid_access(int off, int size,
3671 enum bpf_access_type type,
3672 struct bpf_insn_access_aux *info)
3673 {
3674 if (type == BPF_WRITE) {
3675 switch (off) {
3676 case offsetof(struct bpf_sock_ops, op) ...
3677 offsetof(struct bpf_sock_ops, replylong[3]):
3678 break;
3679 default:
3680 return false;
3681 }
3682 }
3683
3684 return __is_valid_sock_ops_access(off, size);
3685 }
3686
sk_skb_prologue(struct bpf_insn * insn_buf,bool direct_write,const struct bpf_prog * prog)3687 static int sk_skb_prologue(struct bpf_insn *insn_buf, bool direct_write,
3688 const struct bpf_prog *prog)
3689 {
3690 return bpf_unclone_prologue(insn_buf, direct_write, prog, SK_DROP);
3691 }
3692
sk_skb_is_valid_access(int off,int size,enum bpf_access_type type,struct bpf_insn_access_aux * info)3693 static bool sk_skb_is_valid_access(int off, int size,
3694 enum bpf_access_type type,
3695 struct bpf_insn_access_aux *info)
3696 {
3697 if (type == BPF_WRITE) {
3698 switch (off) {
3699 case bpf_ctx_range(struct __sk_buff, tc_index):
3700 case bpf_ctx_range(struct __sk_buff, priority):
3701 break;
3702 default:
3703 return false;
3704 }
3705 }
3706
3707 switch (off) {
3708 case bpf_ctx_range(struct __sk_buff, mark):
3709 case bpf_ctx_range(struct __sk_buff, tc_classid):
3710 return false;
3711 case bpf_ctx_range(struct __sk_buff, data):
3712 info->reg_type = PTR_TO_PACKET;
3713 break;
3714 case bpf_ctx_range(struct __sk_buff, data_end):
3715 info->reg_type = PTR_TO_PACKET_END;
3716 break;
3717 }
3718
3719 return bpf_skb_is_valid_access(off, size, type, info);
3720 }
3721
bpf_convert_ctx_access(enum bpf_access_type type,const struct bpf_insn * si,struct bpf_insn * insn_buf,struct bpf_prog * prog,u32 * target_size)3722 static u32 bpf_convert_ctx_access(enum bpf_access_type type,
3723 const struct bpf_insn *si,
3724 struct bpf_insn *insn_buf,
3725 struct bpf_prog *prog, u32 *target_size)
3726 {
3727 struct bpf_insn *insn = insn_buf;
3728 int off;
3729
3730 switch (si->off) {
3731 case offsetof(struct __sk_buff, len):
3732 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
3733 bpf_target_off(struct sk_buff, len, 4,
3734 target_size));
3735 break;
3736
3737 case offsetof(struct __sk_buff, protocol):
3738 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
3739 bpf_target_off(struct sk_buff, protocol, 2,
3740 target_size));
3741 break;
3742
3743 case offsetof(struct __sk_buff, vlan_proto):
3744 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
3745 bpf_target_off(struct sk_buff, vlan_proto, 2,
3746 target_size));
3747 break;
3748
3749 case offsetof(struct __sk_buff, priority):
3750 if (type == BPF_WRITE)
3751 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
3752 bpf_target_off(struct sk_buff, priority, 4,
3753 target_size));
3754 else
3755 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
3756 bpf_target_off(struct sk_buff, priority, 4,
3757 target_size));
3758 break;
3759
3760 case offsetof(struct __sk_buff, ingress_ifindex):
3761 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
3762 bpf_target_off(struct sk_buff, skb_iif, 4,
3763 target_size));
3764 break;
3765
3766 case offsetof(struct __sk_buff, ifindex):
3767 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
3768 si->dst_reg, si->src_reg,
3769 offsetof(struct sk_buff, dev));
3770 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
3771 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
3772 bpf_target_off(struct net_device, ifindex, 4,
3773 target_size));
3774 break;
3775
3776 case offsetof(struct __sk_buff, hash):
3777 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
3778 bpf_target_off(struct sk_buff, hash, 4,
3779 target_size));
3780 break;
3781
3782 case offsetof(struct __sk_buff, mark):
3783 if (type == BPF_WRITE)
3784 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
3785 bpf_target_off(struct sk_buff, mark, 4,
3786 target_size));
3787 else
3788 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
3789 bpf_target_off(struct sk_buff, mark, 4,
3790 target_size));
3791 break;
3792
3793 case offsetof(struct __sk_buff, pkt_type):
3794 *target_size = 1;
3795 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->src_reg,
3796 PKT_TYPE_OFFSET());
3797 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, PKT_TYPE_MAX);
3798 #ifdef __BIG_ENDIAN_BITFIELD
3799 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 5);
3800 #endif
3801 break;
3802
3803 case offsetof(struct __sk_buff, queue_mapping):
3804 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
3805 bpf_target_off(struct sk_buff, queue_mapping, 2,
3806 target_size));
3807 break;
3808
3809 case offsetof(struct __sk_buff, vlan_present):
3810 case offsetof(struct __sk_buff, vlan_tci):
3811 BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000);
3812
3813 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
3814 bpf_target_off(struct sk_buff, vlan_tci, 2,
3815 target_size));
3816 if (si->off == offsetof(struct __sk_buff, vlan_tci)) {
3817 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg,
3818 ~VLAN_TAG_PRESENT);
3819 } else {
3820 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 12);
3821 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, 1);
3822 }
3823 break;
3824
3825 case offsetof(struct __sk_buff, cb[0]) ...
3826 offsetofend(struct __sk_buff, cb[4]) - 1:
3827 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, data) < 20);
3828 BUILD_BUG_ON((offsetof(struct sk_buff, cb) +
3829 offsetof(struct qdisc_skb_cb, data)) %
3830 sizeof(__u64));
3831
3832 prog->cb_access = 1;
3833 off = si->off;
3834 off -= offsetof(struct __sk_buff, cb[0]);
3835 off += offsetof(struct sk_buff, cb);
3836 off += offsetof(struct qdisc_skb_cb, data);
3837 if (type == BPF_WRITE)
3838 *insn++ = BPF_STX_MEM(BPF_SIZE(si->code), si->dst_reg,
3839 si->src_reg, off);
3840 else
3841 *insn++ = BPF_LDX_MEM(BPF_SIZE(si->code), si->dst_reg,
3842 si->src_reg, off);
3843 break;
3844
3845 case offsetof(struct __sk_buff, tc_classid):
3846 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, tc_classid) != 2);
3847
3848 off = si->off;
3849 off -= offsetof(struct __sk_buff, tc_classid);
3850 off += offsetof(struct sk_buff, cb);
3851 off += offsetof(struct qdisc_skb_cb, tc_classid);
3852 *target_size = 2;
3853 if (type == BPF_WRITE)
3854 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg,
3855 si->src_reg, off);
3856 else
3857 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg,
3858 si->src_reg, off);
3859 break;
3860
3861 case offsetof(struct __sk_buff, data):
3862 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
3863 si->dst_reg, si->src_reg,
3864 offsetof(struct sk_buff, data));
3865 break;
3866
3867 case offsetof(struct __sk_buff, data_end):
3868 off = si->off;
3869 off -= offsetof(struct __sk_buff, data_end);
3870 off += offsetof(struct sk_buff, cb);
3871 off += offsetof(struct bpf_skb_data_end, data_end);
3872 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
3873 si->src_reg, off);
3874 break;
3875
3876 case offsetof(struct __sk_buff, tc_index):
3877 #ifdef CONFIG_NET_SCHED
3878 if (type == BPF_WRITE)
3879 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg, si->src_reg,
3880 bpf_target_off(struct sk_buff, tc_index, 2,
3881 target_size));
3882 else
3883 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
3884 bpf_target_off(struct sk_buff, tc_index, 2,
3885 target_size));
3886 #else
3887 *target_size = 2;
3888 if (type == BPF_WRITE)
3889 *insn++ = BPF_MOV64_REG(si->dst_reg, si->dst_reg);
3890 else
3891 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
3892 #endif
3893 break;
3894
3895 case offsetof(struct __sk_buff, napi_id):
3896 #if defined(CONFIG_NET_RX_BUSY_POLL)
3897 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
3898 bpf_target_off(struct sk_buff, napi_id, 4,
3899 target_size));
3900 *insn++ = BPF_JMP_IMM(BPF_JGE, si->dst_reg, MIN_NAPI_ID, 1);
3901 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
3902 #else
3903 *target_size = 4;
3904 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
3905 #endif
3906 break;
3907 case offsetof(struct __sk_buff, family):
3908 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
3909
3910 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
3911 si->dst_reg, si->src_reg,
3912 offsetof(struct sk_buff, sk));
3913 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
3914 bpf_target_off(struct sock_common,
3915 skc_family,
3916 2, target_size));
3917 break;
3918 case offsetof(struct __sk_buff, remote_ip4):
3919 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
3920
3921 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
3922 si->dst_reg, si->src_reg,
3923 offsetof(struct sk_buff, sk));
3924 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
3925 bpf_target_off(struct sock_common,
3926 skc_daddr,
3927 4, target_size));
3928 break;
3929 case offsetof(struct __sk_buff, local_ip4):
3930 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
3931 skc_rcv_saddr) != 4);
3932
3933 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
3934 si->dst_reg, si->src_reg,
3935 offsetof(struct sk_buff, sk));
3936 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
3937 bpf_target_off(struct sock_common,
3938 skc_rcv_saddr,
3939 4, target_size));
3940 break;
3941 case offsetof(struct __sk_buff, remote_ip6[0]) ...
3942 offsetof(struct __sk_buff, remote_ip6[3]):
3943 #if IS_ENABLED(CONFIG_IPV6)
3944 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
3945 skc_v6_daddr.s6_addr32[0]) != 4);
3946
3947 off = si->off;
3948 off -= offsetof(struct __sk_buff, remote_ip6[0]);
3949
3950 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
3951 si->dst_reg, si->src_reg,
3952 offsetof(struct sk_buff, sk));
3953 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
3954 offsetof(struct sock_common,
3955 skc_v6_daddr.s6_addr32[0]) +
3956 off);
3957 #else
3958 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
3959 #endif
3960 break;
3961 case offsetof(struct __sk_buff, local_ip6[0]) ...
3962 offsetof(struct __sk_buff, local_ip6[3]):
3963 #if IS_ENABLED(CONFIG_IPV6)
3964 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
3965 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
3966
3967 off = si->off;
3968 off -= offsetof(struct __sk_buff, local_ip6[0]);
3969
3970 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
3971 si->dst_reg, si->src_reg,
3972 offsetof(struct sk_buff, sk));
3973 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
3974 offsetof(struct sock_common,
3975 skc_v6_rcv_saddr.s6_addr32[0]) +
3976 off);
3977 #else
3978 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
3979 #endif
3980 break;
3981
3982 case offsetof(struct __sk_buff, remote_port):
3983 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
3984
3985 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
3986 si->dst_reg, si->src_reg,
3987 offsetof(struct sk_buff, sk));
3988 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
3989 bpf_target_off(struct sock_common,
3990 skc_dport,
3991 2, target_size));
3992 #ifndef __BIG_ENDIAN_BITFIELD
3993 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
3994 #endif
3995 break;
3996
3997 case offsetof(struct __sk_buff, local_port):
3998 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
3999
4000 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
4001 si->dst_reg, si->src_reg,
4002 offsetof(struct sk_buff, sk));
4003 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
4004 bpf_target_off(struct sock_common,
4005 skc_num, 2, target_size));
4006 break;
4007 }
4008
4009 return insn - insn_buf;
4010 }
4011
sock_filter_convert_ctx_access(enum bpf_access_type type,const struct bpf_insn * si,struct bpf_insn * insn_buf,struct bpf_prog * prog,u32 * target_size)4012 static u32 sock_filter_convert_ctx_access(enum bpf_access_type type,
4013 const struct bpf_insn *si,
4014 struct bpf_insn *insn_buf,
4015 struct bpf_prog *prog, u32 *target_size)
4016 {
4017 struct bpf_insn *insn = insn_buf;
4018
4019 switch (si->off) {
4020 case offsetof(struct bpf_sock, bound_dev_if):
4021 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_bound_dev_if) != 4);
4022
4023 if (type == BPF_WRITE)
4024 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
4025 offsetof(struct sock, sk_bound_dev_if));
4026 else
4027 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
4028 offsetof(struct sock, sk_bound_dev_if));
4029 break;
4030
4031 case offsetof(struct bpf_sock, mark):
4032 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_mark) != 4);
4033
4034 if (type == BPF_WRITE)
4035 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
4036 offsetof(struct sock, sk_mark));
4037 else
4038 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
4039 offsetof(struct sock, sk_mark));
4040 break;
4041
4042 case offsetof(struct bpf_sock, priority):
4043 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_priority) != 4);
4044
4045 if (type == BPF_WRITE)
4046 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
4047 offsetof(struct sock, sk_priority));
4048 else
4049 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
4050 offsetof(struct sock, sk_priority));
4051 break;
4052
4053 case offsetof(struct bpf_sock, family):
4054 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_family) != 2);
4055
4056 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
4057 offsetof(struct sock, sk_family));
4058 break;
4059
4060 case offsetof(struct bpf_sock, type):
4061 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
4062 offsetof(struct sock, __sk_flags_offset));
4063 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_TYPE_MASK);
4064 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_TYPE_SHIFT);
4065 break;
4066
4067 case offsetof(struct bpf_sock, protocol):
4068 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
4069 offsetof(struct sock, __sk_flags_offset));
4070 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_PROTO_MASK);
4071 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_PROTO_SHIFT);
4072 break;
4073 }
4074
4075 return insn - insn_buf;
4076 }
4077
tc_cls_act_convert_ctx_access(enum bpf_access_type type,const struct bpf_insn * si,struct bpf_insn * insn_buf,struct bpf_prog * prog,u32 * target_size)4078 static u32 tc_cls_act_convert_ctx_access(enum bpf_access_type type,
4079 const struct bpf_insn *si,
4080 struct bpf_insn *insn_buf,
4081 struct bpf_prog *prog, u32 *target_size)
4082 {
4083 struct bpf_insn *insn = insn_buf;
4084
4085 switch (si->off) {
4086 case offsetof(struct __sk_buff, ifindex):
4087 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
4088 si->dst_reg, si->src_reg,
4089 offsetof(struct sk_buff, dev));
4090 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
4091 bpf_target_off(struct net_device, ifindex, 4,
4092 target_size));
4093 break;
4094 default:
4095 return bpf_convert_ctx_access(type, si, insn_buf, prog,
4096 target_size);
4097 }
4098
4099 return insn - insn_buf;
4100 }
4101
xdp_convert_ctx_access(enum bpf_access_type type,const struct bpf_insn * si,struct bpf_insn * insn_buf,struct bpf_prog * prog,u32 * target_size)4102 static u32 xdp_convert_ctx_access(enum bpf_access_type type,
4103 const struct bpf_insn *si,
4104 struct bpf_insn *insn_buf,
4105 struct bpf_prog *prog, u32 *target_size)
4106 {
4107 struct bpf_insn *insn = insn_buf;
4108
4109 switch (si->off) {
4110 case offsetof(struct xdp_md, data):
4111 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data),
4112 si->dst_reg, si->src_reg,
4113 offsetof(struct xdp_buff, data));
4114 break;
4115 case offsetof(struct xdp_md, data_end):
4116 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_end),
4117 si->dst_reg, si->src_reg,
4118 offsetof(struct xdp_buff, data_end));
4119 break;
4120 }
4121
4122 return insn - insn_buf;
4123 }
4124
sock_ops_convert_ctx_access(enum bpf_access_type type,const struct bpf_insn * si,struct bpf_insn * insn_buf,struct bpf_prog * prog,u32 * target_size)4125 static u32 sock_ops_convert_ctx_access(enum bpf_access_type type,
4126 const struct bpf_insn *si,
4127 struct bpf_insn *insn_buf,
4128 struct bpf_prog *prog,
4129 u32 *target_size)
4130 {
4131 struct bpf_insn *insn = insn_buf;
4132 int off;
4133
4134 switch (si->off) {
4135 case offsetof(struct bpf_sock_ops, op) ...
4136 offsetof(struct bpf_sock_ops, replylong[3]):
4137 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, op) !=
4138 FIELD_SIZEOF(struct bpf_sock_ops_kern, op));
4139 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, reply) !=
4140 FIELD_SIZEOF(struct bpf_sock_ops_kern, reply));
4141 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, replylong) !=
4142 FIELD_SIZEOF(struct bpf_sock_ops_kern, replylong));
4143 off = si->off;
4144 off -= offsetof(struct bpf_sock_ops, op);
4145 off += offsetof(struct bpf_sock_ops_kern, op);
4146 if (type == BPF_WRITE)
4147 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
4148 off);
4149 else
4150 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
4151 off);
4152 break;
4153
4154 case offsetof(struct bpf_sock_ops, family):
4155 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
4156
4157 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
4158 struct bpf_sock_ops_kern, sk),
4159 si->dst_reg, si->src_reg,
4160 offsetof(struct bpf_sock_ops_kern, sk));
4161 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
4162 offsetof(struct sock_common, skc_family));
4163 break;
4164
4165 case offsetof(struct bpf_sock_ops, remote_ip4):
4166 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
4167
4168 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
4169 struct bpf_sock_ops_kern, sk),
4170 si->dst_reg, si->src_reg,
4171 offsetof(struct bpf_sock_ops_kern, sk));
4172 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
4173 offsetof(struct sock_common, skc_daddr));
4174 break;
4175
4176 case offsetof(struct bpf_sock_ops, local_ip4):
4177 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_rcv_saddr) != 4);
4178
4179 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
4180 struct bpf_sock_ops_kern, sk),
4181 si->dst_reg, si->src_reg,
4182 offsetof(struct bpf_sock_ops_kern, sk));
4183 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
4184 offsetof(struct sock_common,
4185 skc_rcv_saddr));
4186 break;
4187
4188 case offsetof(struct bpf_sock_ops, remote_ip6[0]) ...
4189 offsetof(struct bpf_sock_ops, remote_ip6[3]):
4190 #if IS_ENABLED(CONFIG_IPV6)
4191 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
4192 skc_v6_daddr.s6_addr32[0]) != 4);
4193
4194 off = si->off;
4195 off -= offsetof(struct bpf_sock_ops, remote_ip6[0]);
4196 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
4197 struct bpf_sock_ops_kern, sk),
4198 si->dst_reg, si->src_reg,
4199 offsetof(struct bpf_sock_ops_kern, sk));
4200 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
4201 offsetof(struct sock_common,
4202 skc_v6_daddr.s6_addr32[0]) +
4203 off);
4204 #else
4205 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
4206 #endif
4207 break;
4208
4209 case offsetof(struct bpf_sock_ops, local_ip6[0]) ...
4210 offsetof(struct bpf_sock_ops, local_ip6[3]):
4211 #if IS_ENABLED(CONFIG_IPV6)
4212 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
4213 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
4214
4215 off = si->off;
4216 off -= offsetof(struct bpf_sock_ops, local_ip6[0]);
4217 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
4218 struct bpf_sock_ops_kern, sk),
4219 si->dst_reg, si->src_reg,
4220 offsetof(struct bpf_sock_ops_kern, sk));
4221 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
4222 offsetof(struct sock_common,
4223 skc_v6_rcv_saddr.s6_addr32[0]) +
4224 off);
4225 #else
4226 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
4227 #endif
4228 break;
4229
4230 case offsetof(struct bpf_sock_ops, remote_port):
4231 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
4232
4233 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
4234 struct bpf_sock_ops_kern, sk),
4235 si->dst_reg, si->src_reg,
4236 offsetof(struct bpf_sock_ops_kern, sk));
4237 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
4238 offsetof(struct sock_common, skc_dport));
4239 #ifndef __BIG_ENDIAN_BITFIELD
4240 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
4241 #endif
4242 break;
4243
4244 case offsetof(struct bpf_sock_ops, local_port):
4245 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
4246
4247 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
4248 struct bpf_sock_ops_kern, sk),
4249 si->dst_reg, si->src_reg,
4250 offsetof(struct bpf_sock_ops_kern, sk));
4251 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
4252 offsetof(struct sock_common, skc_num));
4253 break;
4254 }
4255 return insn - insn_buf;
4256 }
4257
sk_skb_convert_ctx_access(enum bpf_access_type type,const struct bpf_insn * si,struct bpf_insn * insn_buf,struct bpf_prog * prog,u32 * target_size)4258 static u32 sk_skb_convert_ctx_access(enum bpf_access_type type,
4259 const struct bpf_insn *si,
4260 struct bpf_insn *insn_buf,
4261 struct bpf_prog *prog, u32 *target_size)
4262 {
4263 struct bpf_insn *insn = insn_buf;
4264 int off;
4265
4266 switch (si->off) {
4267 case offsetof(struct __sk_buff, data_end):
4268 off = si->off;
4269 off -= offsetof(struct __sk_buff, data_end);
4270 off += offsetof(struct sk_buff, cb);
4271 off += offsetof(struct tcp_skb_cb, bpf.data_end);
4272 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
4273 si->src_reg, off);
4274 break;
4275 default:
4276 return bpf_convert_ctx_access(type, si, insn_buf, prog,
4277 target_size);
4278 }
4279
4280 return insn - insn_buf;
4281 }
4282
4283 const struct bpf_verifier_ops sk_filter_prog_ops = {
4284 .get_func_proto = sk_filter_func_proto,
4285 .is_valid_access = sk_filter_is_valid_access,
4286 .convert_ctx_access = bpf_convert_ctx_access,
4287 };
4288
4289 const struct bpf_verifier_ops tc_cls_act_prog_ops = {
4290 .get_func_proto = tc_cls_act_func_proto,
4291 .is_valid_access = tc_cls_act_is_valid_access,
4292 .convert_ctx_access = tc_cls_act_convert_ctx_access,
4293 .gen_prologue = tc_cls_act_prologue,
4294 .test_run = bpf_prog_test_run_skb,
4295 };
4296
4297 const struct bpf_verifier_ops xdp_prog_ops = {
4298 .get_func_proto = xdp_func_proto,
4299 .is_valid_access = xdp_is_valid_access,
4300 .convert_ctx_access = xdp_convert_ctx_access,
4301 .test_run = bpf_prog_test_run_xdp,
4302 };
4303
4304 const struct bpf_verifier_ops cg_skb_prog_ops = {
4305 .get_func_proto = sk_filter_func_proto,
4306 .is_valid_access = sk_filter_is_valid_access,
4307 .convert_ctx_access = bpf_convert_ctx_access,
4308 .test_run = bpf_prog_test_run_skb,
4309 };
4310
4311 const struct bpf_verifier_ops lwt_inout_prog_ops = {
4312 .get_func_proto = lwt_inout_func_proto,
4313 .is_valid_access = lwt_is_valid_access,
4314 .convert_ctx_access = bpf_convert_ctx_access,
4315 .test_run = bpf_prog_test_run_skb,
4316 };
4317
4318 const struct bpf_verifier_ops lwt_xmit_prog_ops = {
4319 .get_func_proto = lwt_xmit_func_proto,
4320 .is_valid_access = lwt_is_valid_access,
4321 .convert_ctx_access = bpf_convert_ctx_access,
4322 .gen_prologue = tc_cls_act_prologue,
4323 .test_run = bpf_prog_test_run_skb,
4324 };
4325
4326 const struct bpf_verifier_ops cg_sock_prog_ops = {
4327 .get_func_proto = sock_filter_func_proto,
4328 .is_valid_access = sock_filter_is_valid_access,
4329 .convert_ctx_access = sock_filter_convert_ctx_access,
4330 };
4331
4332 const struct bpf_verifier_ops sock_ops_prog_ops = {
4333 .get_func_proto = sock_ops_func_proto,
4334 .is_valid_access = sock_ops_is_valid_access,
4335 .convert_ctx_access = sock_ops_convert_ctx_access,
4336 };
4337
4338 const struct bpf_verifier_ops sk_skb_prog_ops = {
4339 .get_func_proto = sk_skb_func_proto,
4340 .is_valid_access = sk_skb_is_valid_access,
4341 .convert_ctx_access = sk_skb_convert_ctx_access,
4342 .gen_prologue = sk_skb_prologue,
4343 };
4344
sk_detach_filter(struct sock * sk)4345 int sk_detach_filter(struct sock *sk)
4346 {
4347 int ret = -ENOENT;
4348 struct sk_filter *filter;
4349
4350 if (sock_flag(sk, SOCK_FILTER_LOCKED))
4351 return -EPERM;
4352
4353 filter = rcu_dereference_protected(sk->sk_filter,
4354 lockdep_sock_is_held(sk));
4355 if (filter) {
4356 RCU_INIT_POINTER(sk->sk_filter, NULL);
4357 sk_filter_uncharge(sk, filter);
4358 ret = 0;
4359 }
4360
4361 return ret;
4362 }
4363 EXPORT_SYMBOL_GPL(sk_detach_filter);
4364
sk_get_filter(struct sock * sk,struct sock_filter __user * ubuf,unsigned int len)4365 int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf,
4366 unsigned int len)
4367 {
4368 struct sock_fprog_kern *fprog;
4369 struct sk_filter *filter;
4370 int ret = 0;
4371
4372 lock_sock(sk);
4373 filter = rcu_dereference_protected(sk->sk_filter,
4374 lockdep_sock_is_held(sk));
4375 if (!filter)
4376 goto out;
4377
4378 /* We're copying the filter that has been originally attached,
4379 * so no conversion/decode needed anymore. eBPF programs that
4380 * have no original program cannot be dumped through this.
4381 */
4382 ret = -EACCES;
4383 fprog = filter->prog->orig_prog;
4384 if (!fprog)
4385 goto out;
4386
4387 ret = fprog->len;
4388 if (!len)
4389 /* User space only enquires number of filter blocks. */
4390 goto out;
4391
4392 ret = -EINVAL;
4393 if (len < fprog->len)
4394 goto out;
4395
4396 ret = -EFAULT;
4397 if (copy_to_user(ubuf, fprog->filter, bpf_classic_proglen(fprog)))
4398 goto out;
4399
4400 /* Instead of bytes, the API requests to return the number
4401 * of filter blocks.
4402 */
4403 ret = fprog->len;
4404 out:
4405 release_sock(sk);
4406 return ret;
4407 }
4408