2 * Linux Socket Filter - Kernel level socket filtering
4 * Based on the design of the Berkeley Packet Filter. The new
5 * internal format has been designed by PLUMgrid:
7 * Copyright (c) 2011 - 2014 PLUMgrid, http://plumgrid.com
11 * Jay Schulist <jschlst@samba.org>
12 * Alexei Starovoitov <ast@plumgrid.com>
13 * Daniel Borkmann <dborkman@redhat.com>
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.
20 * Andi Kleen - Fix a few bad bugs and races.
21 * Kris Katterjohn - Added many additional checks in bpf_check_classic()
24 #include <linux/module.h>
25 #include <linux/types.h>
27 #include <linux/fcntl.h>
28 #include <linux/socket.h>
30 #include <linux/inet.h>
31 #include <linux/netdevice.h>
32 #include <linux/if_packet.h>
33 #include <linux/gfp.h>
35 #include <net/protocol.h>
36 #include <net/netlink.h>
37 #include <linux/skbuff.h>
39 #include <net/flow_dissector.h>
40 #include <linux/errno.h>
41 #include <linux/timer.h>
42 #include <asm/uaccess.h>
43 #include <asm/unaligned.h>
44 #include <linux/filter.h>
45 #include <linux/ratelimit.h>
46 #include <linux/seccomp.h>
47 #include <linux/if_vlan.h>
48 #include <linux/bpf.h>
49 #include <net/sch_generic.h>
50 #include <net/cls_cgroup.h>
51 #include <net/dst_metadata.h>
53 #include <net/sock_reuseport.h>
56 * sk_filter - run a packet through a socket filter
57 * @sk: sock associated with &sk_buff
58 * @skb: buffer to filter
60 * Run the eBPF program and then cut skb->data to correct size returned by
61 * the program. If pkt_len is 0 we toss packet. If skb->len is smaller
62 * than pkt_len we keep whole skb->data. This is the socket level
63 * wrapper to BPF_PROG_RUN. It returns 0 if the packet should
64 * be accepted or -EPERM if the packet should be tossed.
67 int sk_filter(struct sock *sk, struct sk_buff *skb)
70 struct sk_filter *filter;
73 * If the skb was allocated from pfmemalloc reserves, only
74 * allow SOCK_MEMALLOC sockets to use it as this socket is
77 if (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC))
80 err = security_sock_rcv_skb(sk, skb);
85 filter = rcu_dereference(sk->sk_filter);
87 unsigned int pkt_len = bpf_prog_run_save_cb(filter->prog, skb);
89 err = pkt_len ? pskb_trim(skb, pkt_len) : -EPERM;
95 EXPORT_SYMBOL(sk_filter);
97 static u64 __skb_get_pay_offset(u64 ctx, u64 a, u64 x, u64 r4, u64 r5)
99 return skb_get_poff((struct sk_buff *)(unsigned long) ctx);
102 static u64 __skb_get_nlattr(u64 ctx, u64 a, u64 x, u64 r4, u64 r5)
104 struct sk_buff *skb = (struct sk_buff *)(unsigned long) ctx;
107 if (skb_is_nonlinear(skb))
110 if (skb->len < sizeof(struct nlattr))
113 if (a > skb->len - sizeof(struct nlattr))
116 nla = nla_find((struct nlattr *) &skb->data[a], skb->len - a, x);
118 return (void *) nla - (void *) skb->data;
123 static u64 __skb_get_nlattr_nest(u64 ctx, u64 a, u64 x, u64 r4, u64 r5)
125 struct sk_buff *skb = (struct sk_buff *)(unsigned long) ctx;
128 if (skb_is_nonlinear(skb))
131 if (skb->len < sizeof(struct nlattr))
134 if (a > skb->len - sizeof(struct nlattr))
137 nla = (struct nlattr *) &skb->data[a];
138 if (nla->nla_len > skb->len - a)
141 nla = nla_find_nested(nla, x);
143 return (void *) nla - (void *) skb->data;
148 static u64 __get_raw_cpu_id(u64 ctx, u64 a, u64 x, u64 r4, u64 r5)
150 return raw_smp_processor_id();
153 static u32 convert_skb_access(int skb_field, int dst_reg, int src_reg,
154 struct bpf_insn *insn_buf)
156 struct bpf_insn *insn = insn_buf;
160 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
162 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
163 offsetof(struct sk_buff, mark));
167 *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_TYPE_OFFSET());
168 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, PKT_TYPE_MAX);
169 #ifdef __BIG_ENDIAN_BITFIELD
170 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 5);
175 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, queue_mapping) != 2);
177 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
178 offsetof(struct sk_buff, queue_mapping));
181 case SKF_AD_VLAN_TAG:
182 case SKF_AD_VLAN_TAG_PRESENT:
183 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_tci) != 2);
184 BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000);
186 /* dst_reg = *(u16 *) (src_reg + offsetof(vlan_tci)) */
187 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
188 offsetof(struct sk_buff, vlan_tci));
189 if (skb_field == SKF_AD_VLAN_TAG) {
190 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg,
194 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 12);
196 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, 1);
201 return insn - insn_buf;
204 static bool convert_bpf_extensions(struct sock_filter *fp,
205 struct bpf_insn **insnp)
207 struct bpf_insn *insn = *insnp;
211 case SKF_AD_OFF + SKF_AD_PROTOCOL:
212 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
214 /* A = *(u16 *) (CTX + offsetof(protocol)) */
215 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
216 offsetof(struct sk_buff, protocol));
217 /* A = ntohs(A) [emitting a nop or swap16] */
218 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
221 case SKF_AD_OFF + SKF_AD_PKTTYPE:
222 cnt = convert_skb_access(SKF_AD_PKTTYPE, BPF_REG_A, BPF_REG_CTX, insn);
226 case SKF_AD_OFF + SKF_AD_IFINDEX:
227 case SKF_AD_OFF + SKF_AD_HATYPE:
228 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
229 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, type) != 2);
230 BUILD_BUG_ON(bytes_to_bpf_size(FIELD_SIZEOF(struct sk_buff, dev)) < 0);
232 *insn++ = BPF_LDX_MEM(bytes_to_bpf_size(FIELD_SIZEOF(struct sk_buff, dev)),
233 BPF_REG_TMP, BPF_REG_CTX,
234 offsetof(struct sk_buff, dev));
235 /* if (tmp != 0) goto pc + 1 */
236 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_TMP, 0, 1);
237 *insn++ = BPF_EXIT_INSN();
238 if (fp->k == SKF_AD_OFF + SKF_AD_IFINDEX)
239 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_TMP,
240 offsetof(struct net_device, ifindex));
242 *insn = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_TMP,
243 offsetof(struct net_device, type));
246 case SKF_AD_OFF + SKF_AD_MARK:
247 cnt = convert_skb_access(SKF_AD_MARK, BPF_REG_A, BPF_REG_CTX, insn);
251 case SKF_AD_OFF + SKF_AD_RXHASH:
252 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
254 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX,
255 offsetof(struct sk_buff, hash));
258 case SKF_AD_OFF + SKF_AD_QUEUE:
259 cnt = convert_skb_access(SKF_AD_QUEUE, BPF_REG_A, BPF_REG_CTX, insn);
263 case SKF_AD_OFF + SKF_AD_VLAN_TAG:
264 cnt = convert_skb_access(SKF_AD_VLAN_TAG,
265 BPF_REG_A, BPF_REG_CTX, insn);
269 case SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT:
270 cnt = convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
271 BPF_REG_A, BPF_REG_CTX, insn);
275 case SKF_AD_OFF + SKF_AD_VLAN_TPID:
276 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2);
278 /* A = *(u16 *) (CTX + offsetof(vlan_proto)) */
279 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
280 offsetof(struct sk_buff, vlan_proto));
281 /* A = ntohs(A) [emitting a nop or swap16] */
282 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
285 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
286 case SKF_AD_OFF + SKF_AD_NLATTR:
287 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
288 case SKF_AD_OFF + SKF_AD_CPU:
289 case SKF_AD_OFF + SKF_AD_RANDOM:
291 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
293 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_A);
295 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_X);
296 /* Emit call(arg1=CTX, arg2=A, arg3=X) */
298 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
299 *insn = BPF_EMIT_CALL(__skb_get_pay_offset);
301 case SKF_AD_OFF + SKF_AD_NLATTR:
302 *insn = BPF_EMIT_CALL(__skb_get_nlattr);
304 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
305 *insn = BPF_EMIT_CALL(__skb_get_nlattr_nest);
307 case SKF_AD_OFF + SKF_AD_CPU:
308 *insn = BPF_EMIT_CALL(__get_raw_cpu_id);
310 case SKF_AD_OFF + SKF_AD_RANDOM:
311 *insn = BPF_EMIT_CALL(bpf_user_rnd_u32);
312 bpf_user_rnd_init_once();
317 case SKF_AD_OFF + SKF_AD_ALU_XOR_X:
319 *insn = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_X);
323 /* This is just a dummy call to avoid letting the compiler
324 * evict __bpf_call_base() as an optimization. Placed here
325 * where no-one bothers.
327 BUG_ON(__bpf_call_base(0, 0, 0, 0, 0) != 0);
336 * bpf_convert_filter - convert filter program
337 * @prog: the user passed filter program
338 * @len: the length of the user passed filter program
339 * @new_prog: buffer where converted program will be stored
340 * @new_len: pointer to store length of converted program
342 * Remap 'sock_filter' style BPF instruction set to 'sock_filter_ext' style.
343 * Conversion workflow:
345 * 1) First pass for calculating the new program length:
346 * bpf_convert_filter(old_prog, old_len, NULL, &new_len)
348 * 2) 2nd pass to remap in two passes: 1st pass finds new
349 * jump offsets, 2nd pass remapping:
350 * new_prog = kmalloc(sizeof(struct bpf_insn) * new_len);
351 * bpf_convert_filter(old_prog, old_len, new_prog, &new_len);
353 static int bpf_convert_filter(struct sock_filter *prog, int len,
354 struct bpf_insn *new_prog, int *new_len)
356 int new_flen = 0, pass = 0, target, i;
357 struct bpf_insn *new_insn;
358 struct sock_filter *fp;
362 BUILD_BUG_ON(BPF_MEMWORDS * sizeof(u32) > MAX_BPF_STACK);
363 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
365 if (len <= 0 || len > BPF_MAXINSNS)
369 addrs = kcalloc(len, sizeof(*addrs),
370 GFP_KERNEL | __GFP_NOWARN);
379 /* Classic BPF related prologue emission. */
381 /* Classic BPF expects A and X to be reset first. These need
382 * to be guaranteed to be the first two instructions.
384 *new_insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
385 *new_insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_X, BPF_REG_X);
387 /* All programs must keep CTX in callee saved BPF_REG_CTX.
388 * In eBPF case it's done by the compiler, here we need to
389 * do this ourself. Initial CTX is present in BPF_REG_ARG1.
391 *new_insn++ = BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1);
396 for (i = 0; i < len; fp++, i++) {
397 struct bpf_insn tmp_insns[6] = { };
398 struct bpf_insn *insn = tmp_insns;
401 addrs[i] = new_insn - new_prog;
404 /* All arithmetic insns and skb loads map as-is. */
405 case BPF_ALU | BPF_ADD | BPF_X:
406 case BPF_ALU | BPF_ADD | BPF_K:
407 case BPF_ALU | BPF_SUB | BPF_X:
408 case BPF_ALU | BPF_SUB | BPF_K:
409 case BPF_ALU | BPF_AND | BPF_X:
410 case BPF_ALU | BPF_AND | BPF_K:
411 case BPF_ALU | BPF_OR | BPF_X:
412 case BPF_ALU | BPF_OR | BPF_K:
413 case BPF_ALU | BPF_LSH | BPF_X:
414 case BPF_ALU | BPF_LSH | BPF_K:
415 case BPF_ALU | BPF_RSH | BPF_X:
416 case BPF_ALU | BPF_RSH | BPF_K:
417 case BPF_ALU | BPF_XOR | BPF_X:
418 case BPF_ALU | BPF_XOR | BPF_K:
419 case BPF_ALU | BPF_MUL | BPF_X:
420 case BPF_ALU | BPF_MUL | BPF_K:
421 case BPF_ALU | BPF_DIV | BPF_X:
422 case BPF_ALU | BPF_DIV | BPF_K:
423 case BPF_ALU | BPF_MOD | BPF_X:
424 case BPF_ALU | BPF_MOD | BPF_K:
425 case BPF_ALU | BPF_NEG:
426 case BPF_LD | BPF_ABS | BPF_W:
427 case BPF_LD | BPF_ABS | BPF_H:
428 case BPF_LD | BPF_ABS | BPF_B:
429 case BPF_LD | BPF_IND | BPF_W:
430 case BPF_LD | BPF_IND | BPF_H:
431 case BPF_LD | BPF_IND | BPF_B:
432 /* Check for overloaded BPF extension and
433 * directly convert it if found, otherwise
434 * just move on with mapping.
436 if (BPF_CLASS(fp->code) == BPF_LD &&
437 BPF_MODE(fp->code) == BPF_ABS &&
438 convert_bpf_extensions(fp, &insn))
441 *insn = BPF_RAW_INSN(fp->code, BPF_REG_A, BPF_REG_X, 0, fp->k);
444 /* Jump transformation cannot use BPF block macros
445 * everywhere as offset calculation and target updates
446 * require a bit more work than the rest, i.e. jump
447 * opcodes map as-is, but offsets need adjustment.
450 #define BPF_EMIT_JMP \
452 if (target >= len || target < 0) \
454 insn->off = addrs ? addrs[target] - addrs[i] - 1 : 0; \
455 /* Adjust pc relative offset for 2nd or 3rd insn. */ \
456 insn->off -= insn - tmp_insns; \
459 case BPF_JMP | BPF_JA:
460 target = i + fp->k + 1;
461 insn->code = fp->code;
465 case BPF_JMP | BPF_JEQ | BPF_K:
466 case BPF_JMP | BPF_JEQ | BPF_X:
467 case BPF_JMP | BPF_JSET | BPF_K:
468 case BPF_JMP | BPF_JSET | BPF_X:
469 case BPF_JMP | BPF_JGT | BPF_K:
470 case BPF_JMP | BPF_JGT | BPF_X:
471 case BPF_JMP | BPF_JGE | BPF_K:
472 case BPF_JMP | BPF_JGE | BPF_X:
473 if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) {
474 /* BPF immediates are signed, zero extend
475 * immediate into tmp register and use it
478 *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k);
480 insn->dst_reg = BPF_REG_A;
481 insn->src_reg = BPF_REG_TMP;
484 insn->dst_reg = BPF_REG_A;
486 bpf_src = BPF_SRC(fp->code);
487 insn->src_reg = bpf_src == BPF_X ? BPF_REG_X : 0;
490 /* Common case where 'jump_false' is next insn. */
492 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
493 target = i + fp->jt + 1;
498 /* Convert JEQ into JNE when 'jump_true' is next insn. */
499 if (fp->jt == 0 && BPF_OP(fp->code) == BPF_JEQ) {
500 insn->code = BPF_JMP | BPF_JNE | bpf_src;
501 target = i + fp->jf + 1;
506 /* Other jumps are mapped into two insns: Jxx and JA. */
507 target = i + fp->jt + 1;
508 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
512 insn->code = BPF_JMP | BPF_JA;
513 target = i + fp->jf + 1;
517 /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */
518 case BPF_LDX | BPF_MSH | BPF_B:
520 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_A);
521 /* A = BPF_R0 = *(u8 *) (skb->data + K) */
522 *insn++ = BPF_LD_ABS(BPF_B, fp->k);
524 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf);
526 *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2);
528 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
530 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP);
533 /* RET_K is remaped into 2 insns. RET_A case doesn't need an
534 * extra mov as BPF_REG_0 is already mapped into BPF_REG_A.
536 case BPF_RET | BPF_A:
537 case BPF_RET | BPF_K:
538 if (BPF_RVAL(fp->code) == BPF_K)
539 *insn++ = BPF_MOV32_RAW(BPF_K, BPF_REG_0,
541 *insn = BPF_EXIT_INSN();
544 /* Store to stack. */
547 *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) ==
548 BPF_ST ? BPF_REG_A : BPF_REG_X,
549 -(BPF_MEMWORDS - fp->k) * 4);
552 /* Load from stack. */
553 case BPF_LD | BPF_MEM:
554 case BPF_LDX | BPF_MEM:
555 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
556 BPF_REG_A : BPF_REG_X, BPF_REG_FP,
557 -(BPF_MEMWORDS - fp->k) * 4);
561 case BPF_LD | BPF_IMM:
562 case BPF_LDX | BPF_IMM:
563 *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ?
564 BPF_REG_A : BPF_REG_X, fp->k);
568 case BPF_MISC | BPF_TAX:
569 *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
573 case BPF_MISC | BPF_TXA:
574 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X);
577 /* A = skb->len or X = skb->len */
578 case BPF_LD | BPF_W | BPF_LEN:
579 case BPF_LDX | BPF_W | BPF_LEN:
580 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
581 BPF_REG_A : BPF_REG_X, BPF_REG_CTX,
582 offsetof(struct sk_buff, len));
585 /* Access seccomp_data fields. */
586 case BPF_LDX | BPF_ABS | BPF_W:
587 /* A = *(u32 *) (ctx + K) */
588 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k);
591 /* Unknown instruction. */
598 memcpy(new_insn, tmp_insns,
599 sizeof(*insn) * (insn - tmp_insns));
600 new_insn += insn - tmp_insns;
604 /* Only calculating new length. */
605 *new_len = new_insn - new_prog;
610 if (new_flen != new_insn - new_prog) {
611 new_flen = new_insn - new_prog;
618 BUG_ON(*new_len != new_flen);
627 * As we dont want to clear mem[] array for each packet going through
628 * __bpf_prog_run(), we check that filter loaded by user never try to read
629 * a cell if not previously written, and we check all branches to be sure
630 * a malicious user doesn't try to abuse us.
632 static int check_load_and_stores(const struct sock_filter *filter, int flen)
634 u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */
637 BUILD_BUG_ON(BPF_MEMWORDS > 16);
639 masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL);
643 memset(masks, 0xff, flen * sizeof(*masks));
645 for (pc = 0; pc < flen; pc++) {
646 memvalid &= masks[pc];
648 switch (filter[pc].code) {
651 memvalid |= (1 << filter[pc].k);
653 case BPF_LD | BPF_MEM:
654 case BPF_LDX | BPF_MEM:
655 if (!(memvalid & (1 << filter[pc].k))) {
660 case BPF_JMP | BPF_JA:
661 /* A jump must set masks on target */
662 masks[pc + 1 + filter[pc].k] &= memvalid;
665 case BPF_JMP | BPF_JEQ | BPF_K:
666 case BPF_JMP | BPF_JEQ | BPF_X:
667 case BPF_JMP | BPF_JGE | BPF_K:
668 case BPF_JMP | BPF_JGE | BPF_X:
669 case BPF_JMP | BPF_JGT | BPF_K:
670 case BPF_JMP | BPF_JGT | BPF_X:
671 case BPF_JMP | BPF_JSET | BPF_K:
672 case BPF_JMP | BPF_JSET | BPF_X:
673 /* A jump must set masks on targets */
674 masks[pc + 1 + filter[pc].jt] &= memvalid;
675 masks[pc + 1 + filter[pc].jf] &= memvalid;
685 static bool chk_code_allowed(u16 code_to_probe)
687 static const bool codes[] = {
688 /* 32 bit ALU operations */
689 [BPF_ALU | BPF_ADD | BPF_K] = true,
690 [BPF_ALU | BPF_ADD | BPF_X] = true,
691 [BPF_ALU | BPF_SUB | BPF_K] = true,
692 [BPF_ALU | BPF_SUB | BPF_X] = true,
693 [BPF_ALU | BPF_MUL | BPF_K] = true,
694 [BPF_ALU | BPF_MUL | BPF_X] = true,
695 [BPF_ALU | BPF_DIV | BPF_K] = true,
696 [BPF_ALU | BPF_DIV | BPF_X] = true,
697 [BPF_ALU | BPF_MOD | BPF_K] = true,
698 [BPF_ALU | BPF_MOD | BPF_X] = true,
699 [BPF_ALU | BPF_AND | BPF_K] = true,
700 [BPF_ALU | BPF_AND | BPF_X] = true,
701 [BPF_ALU | BPF_OR | BPF_K] = true,
702 [BPF_ALU | BPF_OR | BPF_X] = true,
703 [BPF_ALU | BPF_XOR | BPF_K] = true,
704 [BPF_ALU | BPF_XOR | BPF_X] = true,
705 [BPF_ALU | BPF_LSH | BPF_K] = true,
706 [BPF_ALU | BPF_LSH | BPF_X] = true,
707 [BPF_ALU | BPF_RSH | BPF_K] = true,
708 [BPF_ALU | BPF_RSH | BPF_X] = true,
709 [BPF_ALU | BPF_NEG] = true,
710 /* Load instructions */
711 [BPF_LD | BPF_W | BPF_ABS] = true,
712 [BPF_LD | BPF_H | BPF_ABS] = true,
713 [BPF_LD | BPF_B | BPF_ABS] = true,
714 [BPF_LD | BPF_W | BPF_LEN] = true,
715 [BPF_LD | BPF_W | BPF_IND] = true,
716 [BPF_LD | BPF_H | BPF_IND] = true,
717 [BPF_LD | BPF_B | BPF_IND] = true,
718 [BPF_LD | BPF_IMM] = true,
719 [BPF_LD | BPF_MEM] = true,
720 [BPF_LDX | BPF_W | BPF_LEN] = true,
721 [BPF_LDX | BPF_B | BPF_MSH] = true,
722 [BPF_LDX | BPF_IMM] = true,
723 [BPF_LDX | BPF_MEM] = true,
724 /* Store instructions */
727 /* Misc instructions */
728 [BPF_MISC | BPF_TAX] = true,
729 [BPF_MISC | BPF_TXA] = true,
730 /* Return instructions */
731 [BPF_RET | BPF_K] = true,
732 [BPF_RET | BPF_A] = true,
733 /* Jump instructions */
734 [BPF_JMP | BPF_JA] = true,
735 [BPF_JMP | BPF_JEQ | BPF_K] = true,
736 [BPF_JMP | BPF_JEQ | BPF_X] = true,
737 [BPF_JMP | BPF_JGE | BPF_K] = true,
738 [BPF_JMP | BPF_JGE | BPF_X] = true,
739 [BPF_JMP | BPF_JGT | BPF_K] = true,
740 [BPF_JMP | BPF_JGT | BPF_X] = true,
741 [BPF_JMP | BPF_JSET | BPF_K] = true,
742 [BPF_JMP | BPF_JSET | BPF_X] = true,
745 if (code_to_probe >= ARRAY_SIZE(codes))
748 return codes[code_to_probe];
752 * bpf_check_classic - verify socket filter code
753 * @filter: filter to verify
754 * @flen: length of filter
756 * Check the user's filter code. If we let some ugly
757 * filter code slip through kaboom! The filter must contain
758 * no references or jumps that are out of range, no illegal
759 * instructions, and must end with a RET instruction.
761 * All jumps are forward as they are not signed.
763 * Returns 0 if the rule set is legal or -EINVAL if not.
765 static int bpf_check_classic(const struct sock_filter *filter,
771 if (flen == 0 || flen > BPF_MAXINSNS)
774 /* Check the filter code now */
775 for (pc = 0; pc < flen; pc++) {
776 const struct sock_filter *ftest = &filter[pc];
778 /* May we actually operate on this code? */
779 if (!chk_code_allowed(ftest->code))
782 /* Some instructions need special checks */
783 switch (ftest->code) {
784 case BPF_ALU | BPF_DIV | BPF_K:
785 case BPF_ALU | BPF_MOD | BPF_K:
786 /* Check for division by zero */
790 case BPF_ALU | BPF_LSH | BPF_K:
791 case BPF_ALU | BPF_RSH | BPF_K:
795 case BPF_LD | BPF_MEM:
796 case BPF_LDX | BPF_MEM:
799 /* Check for invalid memory addresses */
800 if (ftest->k >= BPF_MEMWORDS)
803 case BPF_JMP | BPF_JA:
804 /* Note, the large ftest->k might cause loops.
805 * Compare this with conditional jumps below,
806 * where offsets are limited. --ANK (981016)
808 if (ftest->k >= (unsigned int)(flen - pc - 1))
811 case BPF_JMP | BPF_JEQ | BPF_K:
812 case BPF_JMP | BPF_JEQ | BPF_X:
813 case BPF_JMP | BPF_JGE | BPF_K:
814 case BPF_JMP | BPF_JGE | BPF_X:
815 case BPF_JMP | BPF_JGT | BPF_K:
816 case BPF_JMP | BPF_JGT | BPF_X:
817 case BPF_JMP | BPF_JSET | BPF_K:
818 case BPF_JMP | BPF_JSET | BPF_X:
819 /* Both conditionals must be safe */
820 if (pc + ftest->jt + 1 >= flen ||
821 pc + ftest->jf + 1 >= flen)
824 case BPF_LD | BPF_W | BPF_ABS:
825 case BPF_LD | BPF_H | BPF_ABS:
826 case BPF_LD | BPF_B | BPF_ABS:
828 if (bpf_anc_helper(ftest) & BPF_ANC)
830 /* Ancillary operation unknown or unsupported */
831 if (anc_found == false && ftest->k >= SKF_AD_OFF)
836 /* Last instruction must be a RET code */
837 switch (filter[flen - 1].code) {
838 case BPF_RET | BPF_K:
839 case BPF_RET | BPF_A:
840 return check_load_and_stores(filter, flen);
846 static int bpf_prog_store_orig_filter(struct bpf_prog *fp,
847 const struct sock_fprog *fprog)
849 unsigned int fsize = bpf_classic_proglen(fprog);
850 struct sock_fprog_kern *fkprog;
852 fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL);
856 fkprog = fp->orig_prog;
857 fkprog->len = fprog->len;
859 fkprog->filter = kmemdup(fp->insns, fsize,
860 GFP_KERNEL | __GFP_NOWARN);
861 if (!fkprog->filter) {
862 kfree(fp->orig_prog);
869 static void bpf_release_orig_filter(struct bpf_prog *fp)
871 struct sock_fprog_kern *fprog = fp->orig_prog;
874 kfree(fprog->filter);
879 static void __bpf_prog_release(struct bpf_prog *prog)
881 if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) {
884 bpf_release_orig_filter(prog);
889 static void __sk_filter_release(struct sk_filter *fp)
891 __bpf_prog_release(fp->prog);
896 * sk_filter_release_rcu - Release a socket filter by rcu_head
897 * @rcu: rcu_head that contains the sk_filter to free
899 static void sk_filter_release_rcu(struct rcu_head *rcu)
901 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
903 __sk_filter_release(fp);
907 * sk_filter_release - release a socket filter
908 * @fp: filter to remove
910 * Remove a filter from a socket and release its resources.
912 static void sk_filter_release(struct sk_filter *fp)
914 if (atomic_dec_and_test(&fp->refcnt))
915 call_rcu(&fp->rcu, sk_filter_release_rcu);
918 void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp)
920 u32 filter_size = bpf_prog_size(fp->prog->len);
922 atomic_sub(filter_size, &sk->sk_omem_alloc);
923 sk_filter_release(fp);
926 /* try to charge the socket memory if there is space available
927 * return true on success
929 bool sk_filter_charge(struct sock *sk, struct sk_filter *fp)
931 u32 filter_size = bpf_prog_size(fp->prog->len);
933 /* same check as in sock_kmalloc() */
934 if (filter_size <= sysctl_optmem_max &&
935 atomic_read(&sk->sk_omem_alloc) + filter_size < sysctl_optmem_max) {
936 atomic_inc(&fp->refcnt);
937 atomic_add(filter_size, &sk->sk_omem_alloc);
943 static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp)
945 struct sock_filter *old_prog;
946 struct bpf_prog *old_fp;
947 int err, new_len, old_len = fp->len;
949 /* We are free to overwrite insns et al right here as it
950 * won't be used at this point in time anymore internally
951 * after the migration to the internal BPF instruction
954 BUILD_BUG_ON(sizeof(struct sock_filter) !=
955 sizeof(struct bpf_insn));
957 /* Conversion cannot happen on overlapping memory areas,
958 * so we need to keep the user BPF around until the 2nd
959 * pass. At this time, the user BPF is stored in fp->insns.
961 old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter),
962 GFP_KERNEL | __GFP_NOWARN);
968 /* 1st pass: calculate the new program length. */
969 err = bpf_convert_filter(old_prog, old_len, NULL, &new_len);
973 /* Expand fp for appending the new filter representation. */
975 fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0);
977 /* The old_fp is still around in case we couldn't
978 * allocate new memory, so uncharge on that one.
987 /* 2nd pass: remap sock_filter insns into bpf_insn insns. */
988 err = bpf_convert_filter(old_prog, old_len, fp->insnsi, &new_len);
990 /* 2nd bpf_convert_filter() can fail only if it fails
991 * to allocate memory, remapping must succeed. Note,
992 * that at this time old_fp has already been released
997 bpf_prog_select_runtime(fp);
1005 __bpf_prog_release(fp);
1006 return ERR_PTR(err);
1009 static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp,
1010 bpf_aux_classic_check_t trans)
1014 fp->bpf_func = NULL;
1017 err = bpf_check_classic(fp->insns, fp->len);
1019 __bpf_prog_release(fp);
1020 return ERR_PTR(err);
1023 /* There might be additional checks and transformations
1024 * needed on classic filters, f.e. in case of seccomp.
1027 err = trans(fp->insns, fp->len);
1029 __bpf_prog_release(fp);
1030 return ERR_PTR(err);
1034 /* Probe if we can JIT compile the filter and if so, do
1035 * the compilation of the filter.
1037 bpf_jit_compile(fp);
1039 /* JIT compiler couldn't process this filter, so do the
1040 * internal BPF translation for the optimized interpreter.
1043 fp = bpf_migrate_filter(fp);
1049 * bpf_prog_create - create an unattached filter
1050 * @pfp: the unattached filter that is created
1051 * @fprog: the filter program
1053 * Create a filter independent of any socket. We first run some
1054 * sanity checks on it to make sure it does not explode on us later.
1055 * If an error occurs or there is insufficient memory for the filter
1056 * a negative errno code is returned. On success the return is zero.
1058 int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog)
1060 unsigned int fsize = bpf_classic_proglen(fprog);
1061 struct bpf_prog *fp;
1063 /* Make sure new filter is there and in the right amounts. */
1064 if (fprog->filter == NULL)
1067 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1071 memcpy(fp->insns, fprog->filter, fsize);
1073 fp->len = fprog->len;
1074 /* Since unattached filters are not copied back to user
1075 * space through sk_get_filter(), we do not need to hold
1076 * a copy here, and can spare us the work.
1078 fp->orig_prog = NULL;
1080 /* bpf_prepare_filter() already takes care of freeing
1081 * memory in case something goes wrong.
1083 fp = bpf_prepare_filter(fp, NULL);
1090 EXPORT_SYMBOL_GPL(bpf_prog_create);
1093 * bpf_prog_create_from_user - create an unattached filter from user buffer
1094 * @pfp: the unattached filter that is created
1095 * @fprog: the filter program
1096 * @trans: post-classic verifier transformation handler
1097 * @save_orig: save classic BPF program
1099 * This function effectively does the same as bpf_prog_create(), only
1100 * that it builds up its insns buffer from user space provided buffer.
1101 * It also allows for passing a bpf_aux_classic_check_t handler.
1103 int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
1104 bpf_aux_classic_check_t trans, bool save_orig)
1106 unsigned int fsize = bpf_classic_proglen(fprog);
1107 struct bpf_prog *fp;
1110 /* Make sure new filter is there and in the right amounts. */
1111 if (fprog->filter == NULL)
1114 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1118 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
1119 __bpf_prog_free(fp);
1123 fp->len = fprog->len;
1124 fp->orig_prog = NULL;
1127 err = bpf_prog_store_orig_filter(fp, fprog);
1129 __bpf_prog_free(fp);
1134 /* bpf_prepare_filter() already takes care of freeing
1135 * memory in case something goes wrong.
1137 fp = bpf_prepare_filter(fp, trans);
1144 EXPORT_SYMBOL_GPL(bpf_prog_create_from_user);
1146 void bpf_prog_destroy(struct bpf_prog *fp)
1148 __bpf_prog_release(fp);
1150 EXPORT_SYMBOL_GPL(bpf_prog_destroy);
1152 static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk)
1154 struct sk_filter *fp, *old_fp;
1156 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
1161 atomic_set(&fp->refcnt, 0);
1163 if (!sk_filter_charge(sk, fp)) {
1168 old_fp = rcu_dereference_protected(sk->sk_filter,
1169 lockdep_sock_is_held(sk));
1170 rcu_assign_pointer(sk->sk_filter, fp);
1173 sk_filter_uncharge(sk, old_fp);
1178 static int __reuseport_attach_prog(struct bpf_prog *prog, struct sock *sk)
1180 struct bpf_prog *old_prog;
1183 if (bpf_prog_size(prog->len) > sysctl_optmem_max)
1186 if (sk_unhashed(sk) && sk->sk_reuseport) {
1187 err = reuseport_alloc(sk);
1190 } else if (!rcu_access_pointer(sk->sk_reuseport_cb)) {
1191 /* The socket wasn't bound with SO_REUSEPORT */
1195 old_prog = reuseport_attach_prog(sk, prog);
1197 bpf_prog_destroy(old_prog);
1203 struct bpf_prog *__get_filter(struct sock_fprog *fprog, struct sock *sk)
1205 unsigned int fsize = bpf_classic_proglen(fprog);
1206 unsigned int bpf_fsize = bpf_prog_size(fprog->len);
1207 struct bpf_prog *prog;
1210 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1211 return ERR_PTR(-EPERM);
1213 /* Make sure new filter is there and in the right amounts. */
1214 if (fprog->filter == NULL)
1215 return ERR_PTR(-EINVAL);
1217 prog = bpf_prog_alloc(bpf_fsize, 0);
1219 return ERR_PTR(-ENOMEM);
1221 if (copy_from_user(prog->insns, fprog->filter, fsize)) {
1222 __bpf_prog_free(prog);
1223 return ERR_PTR(-EFAULT);
1226 prog->len = fprog->len;
1228 err = bpf_prog_store_orig_filter(prog, fprog);
1230 __bpf_prog_free(prog);
1231 return ERR_PTR(-ENOMEM);
1234 /* bpf_prepare_filter() already takes care of freeing
1235 * memory in case something goes wrong.
1237 return bpf_prepare_filter(prog, NULL);
1241 * sk_attach_filter - attach a socket filter
1242 * @fprog: the filter program
1243 * @sk: the socket to use
1245 * Attach the user's filter code. We first run some sanity checks on
1246 * it to make sure it does not explode on us later. If an error
1247 * occurs or there is insufficient memory for the filter a negative
1248 * errno code is returned. On success the return is zero.
1250 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1252 struct bpf_prog *prog = __get_filter(fprog, sk);
1256 return PTR_ERR(prog);
1258 err = __sk_attach_prog(prog, sk);
1260 __bpf_prog_release(prog);
1266 EXPORT_SYMBOL_GPL(sk_attach_filter);
1268 int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1270 struct bpf_prog *prog = __get_filter(fprog, sk);
1274 return PTR_ERR(prog);
1276 err = __reuseport_attach_prog(prog, sk);
1278 __bpf_prog_release(prog);
1285 static struct bpf_prog *__get_bpf(u32 ufd, struct sock *sk)
1287 struct bpf_prog *prog;
1289 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1290 return ERR_PTR(-EPERM);
1292 prog = bpf_prog_get(ufd);
1296 if (prog->type != BPF_PROG_TYPE_SOCKET_FILTER) {
1298 return ERR_PTR(-EINVAL);
1304 int sk_attach_bpf(u32 ufd, struct sock *sk)
1306 struct bpf_prog *prog = __get_bpf(ufd, sk);
1310 return PTR_ERR(prog);
1312 err = __sk_attach_prog(prog, sk);
1321 int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk)
1323 struct bpf_prog *prog = __get_bpf(ufd, sk);
1327 return PTR_ERR(prog);
1329 err = __reuseport_attach_prog(prog, sk);
1338 struct bpf_scratchpad {
1340 __be32 diff[MAX_BPF_STACK / sizeof(__be32)];
1341 u8 buff[MAX_BPF_STACK];
1345 static DEFINE_PER_CPU(struct bpf_scratchpad, bpf_sp);
1347 static inline int bpf_try_make_writable(struct sk_buff *skb,
1348 unsigned int write_len)
1352 if (!skb_cloned(skb))
1354 if (skb_clone_writable(skb, write_len))
1356 err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
1358 bpf_compute_data_end(skb);
1362 static u64 bpf_skb_store_bytes(u64 r1, u64 r2, u64 r3, u64 r4, u64 flags)
1364 struct bpf_scratchpad *sp = this_cpu_ptr(&bpf_sp);
1365 struct sk_buff *skb = (struct sk_buff *) (long) r1;
1366 int offset = (int) r2;
1367 void *from = (void *) (long) r3;
1368 unsigned int len = (unsigned int) r4;
1371 if (unlikely(flags & ~(BPF_F_RECOMPUTE_CSUM | BPF_F_INVALIDATE_HASH)))
1374 /* bpf verifier guarantees that:
1375 * 'from' pointer points to bpf program stack
1376 * 'len' bytes of it were initialized
1378 * 'skb' is a valid pointer to 'struct sk_buff'
1380 * so check for invalid 'offset' and too large 'len'
1382 if (unlikely((u32) offset > 0xffff || len > sizeof(sp->buff)))
1384 if (unlikely(bpf_try_make_writable(skb, offset + len)))
1387 ptr = skb_header_pointer(skb, offset, len, sp->buff);
1391 if (flags & BPF_F_RECOMPUTE_CSUM)
1392 skb_postpull_rcsum(skb, ptr, len);
1394 memcpy(ptr, from, len);
1396 if (ptr == sp->buff)
1397 /* skb_store_bits cannot return -EFAULT here */
1398 skb_store_bits(skb, offset, ptr, len);
1400 if (flags & BPF_F_RECOMPUTE_CSUM)
1401 skb_postpush_rcsum(skb, ptr, len);
1402 if (flags & BPF_F_INVALIDATE_HASH)
1403 skb_clear_hash(skb);
1408 static const struct bpf_func_proto bpf_skb_store_bytes_proto = {
1409 .func = bpf_skb_store_bytes,
1411 .ret_type = RET_INTEGER,
1412 .arg1_type = ARG_PTR_TO_CTX,
1413 .arg2_type = ARG_ANYTHING,
1414 .arg3_type = ARG_PTR_TO_STACK,
1415 .arg4_type = ARG_CONST_STACK_SIZE,
1416 .arg5_type = ARG_ANYTHING,
1419 static u64 bpf_skb_load_bytes(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
1421 const struct sk_buff *skb = (const struct sk_buff *)(unsigned long) r1;
1422 int offset = (int) r2;
1423 void *to = (void *)(unsigned long) r3;
1424 unsigned int len = (unsigned int) r4;
1427 if (unlikely((u32) offset > 0xffff))
1430 ptr = skb_header_pointer(skb, offset, len, to);
1434 memcpy(to, ptr, len);
1442 static const struct bpf_func_proto bpf_skb_load_bytes_proto = {
1443 .func = bpf_skb_load_bytes,
1445 .ret_type = RET_INTEGER,
1446 .arg1_type = ARG_PTR_TO_CTX,
1447 .arg2_type = ARG_ANYTHING,
1448 .arg3_type = ARG_PTR_TO_RAW_STACK,
1449 .arg4_type = ARG_CONST_STACK_SIZE,
1452 static u64 bpf_l3_csum_replace(u64 r1, u64 r2, u64 from, u64 to, u64 flags)
1454 struct sk_buff *skb = (struct sk_buff *) (long) r1;
1455 int offset = (int) r2;
1458 if (unlikely(flags & ~(BPF_F_HDR_FIELD_MASK)))
1460 if (unlikely((u32) offset > 0xffff))
1462 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(sum))))
1465 ptr = skb_header_pointer(skb, offset, sizeof(sum), &sum);
1469 switch (flags & BPF_F_HDR_FIELD_MASK) {
1471 if (unlikely(from != 0))
1474 csum_replace_by_diff(ptr, to);
1477 csum_replace2(ptr, from, to);
1480 csum_replace4(ptr, from, to);
1487 /* skb_store_bits guaranteed to not return -EFAULT here */
1488 skb_store_bits(skb, offset, ptr, sizeof(sum));
1493 static const struct bpf_func_proto bpf_l3_csum_replace_proto = {
1494 .func = bpf_l3_csum_replace,
1496 .ret_type = RET_INTEGER,
1497 .arg1_type = ARG_PTR_TO_CTX,
1498 .arg2_type = ARG_ANYTHING,
1499 .arg3_type = ARG_ANYTHING,
1500 .arg4_type = ARG_ANYTHING,
1501 .arg5_type = ARG_ANYTHING,
1504 static u64 bpf_l4_csum_replace(u64 r1, u64 r2, u64 from, u64 to, u64 flags)
1506 struct sk_buff *skb = (struct sk_buff *) (long) r1;
1507 bool is_pseudo = flags & BPF_F_PSEUDO_HDR;
1508 bool is_mmzero = flags & BPF_F_MARK_MANGLED_0;
1509 int offset = (int) r2;
1512 if (unlikely(flags & ~(BPF_F_MARK_MANGLED_0 | BPF_F_PSEUDO_HDR |
1513 BPF_F_HDR_FIELD_MASK)))
1515 if (unlikely((u32) offset > 0xffff))
1517 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(sum))))
1520 ptr = skb_header_pointer(skb, offset, sizeof(sum), &sum);
1523 if (is_mmzero && !*ptr)
1526 switch (flags & BPF_F_HDR_FIELD_MASK) {
1528 if (unlikely(from != 0))
1531 inet_proto_csum_replace_by_diff(ptr, skb, to, is_pseudo);
1534 inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo);
1537 inet_proto_csum_replace4(ptr, skb, from, to, is_pseudo);
1543 if (is_mmzero && !*ptr)
1544 *ptr = CSUM_MANGLED_0;
1546 /* skb_store_bits guaranteed to not return -EFAULT here */
1547 skb_store_bits(skb, offset, ptr, sizeof(sum));
1552 static const struct bpf_func_proto bpf_l4_csum_replace_proto = {
1553 .func = bpf_l4_csum_replace,
1555 .ret_type = RET_INTEGER,
1556 .arg1_type = ARG_PTR_TO_CTX,
1557 .arg2_type = ARG_ANYTHING,
1558 .arg3_type = ARG_ANYTHING,
1559 .arg4_type = ARG_ANYTHING,
1560 .arg5_type = ARG_ANYTHING,
1563 static u64 bpf_csum_diff(u64 r1, u64 from_size, u64 r3, u64 to_size, u64 seed)
1565 struct bpf_scratchpad *sp = this_cpu_ptr(&bpf_sp);
1566 u64 diff_size = from_size + to_size;
1567 __be32 *from = (__be32 *) (long) r1;
1568 __be32 *to = (__be32 *) (long) r3;
1571 /* This is quite flexible, some examples:
1573 * from_size == 0, to_size > 0, seed := csum --> pushing data
1574 * from_size > 0, to_size == 0, seed := csum --> pulling data
1575 * from_size > 0, to_size > 0, seed := 0 --> diffing data
1577 * Even for diffing, from_size and to_size don't need to be equal.
1579 if (unlikely(((from_size | to_size) & (sizeof(__be32) - 1)) ||
1580 diff_size > sizeof(sp->diff)))
1583 for (i = 0; i < from_size / sizeof(__be32); i++, j++)
1584 sp->diff[j] = ~from[i];
1585 for (i = 0; i < to_size / sizeof(__be32); i++, j++)
1586 sp->diff[j] = to[i];
1588 return csum_partial(sp->diff, diff_size, seed);
1591 static const struct bpf_func_proto bpf_csum_diff_proto = {
1592 .func = bpf_csum_diff,
1594 .ret_type = RET_INTEGER,
1595 .arg1_type = ARG_PTR_TO_STACK,
1596 .arg2_type = ARG_CONST_STACK_SIZE_OR_ZERO,
1597 .arg3_type = ARG_PTR_TO_STACK,
1598 .arg4_type = ARG_CONST_STACK_SIZE_OR_ZERO,
1599 .arg5_type = ARG_ANYTHING,
1602 static u64 bpf_clone_redirect(u64 r1, u64 ifindex, u64 flags, u64 r4, u64 r5)
1604 struct sk_buff *skb = (struct sk_buff *) (long) r1, *skb2;
1605 struct net_device *dev;
1607 if (unlikely(flags & ~(BPF_F_INGRESS)))
1610 dev = dev_get_by_index_rcu(dev_net(skb->dev), ifindex);
1614 skb2 = skb_clone(skb, GFP_ATOMIC);
1615 if (unlikely(!skb2))
1618 if (flags & BPF_F_INGRESS) {
1619 if (skb_at_tc_ingress(skb2))
1620 skb_postpush_rcsum(skb2, skb_mac_header(skb2),
1622 return dev_forward_skb(dev, skb2);
1626 return dev_queue_xmit(skb2);
1629 static const struct bpf_func_proto bpf_clone_redirect_proto = {
1630 .func = bpf_clone_redirect,
1632 .ret_type = RET_INTEGER,
1633 .arg1_type = ARG_PTR_TO_CTX,
1634 .arg2_type = ARG_ANYTHING,
1635 .arg3_type = ARG_ANYTHING,
1638 struct redirect_info {
1643 static DEFINE_PER_CPU(struct redirect_info, redirect_info);
1645 static u64 bpf_redirect(u64 ifindex, u64 flags, u64 r3, u64 r4, u64 r5)
1647 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
1649 if (unlikely(flags & ~(BPF_F_INGRESS)))
1652 ri->ifindex = ifindex;
1655 return TC_ACT_REDIRECT;
1658 int skb_do_redirect(struct sk_buff *skb)
1660 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
1661 struct net_device *dev;
1663 dev = dev_get_by_index_rcu(dev_net(skb->dev), ri->ifindex);
1665 if (unlikely(!dev)) {
1670 if (ri->flags & BPF_F_INGRESS) {
1671 if (skb_at_tc_ingress(skb))
1672 skb_postpush_rcsum(skb, skb_mac_header(skb),
1674 return dev_forward_skb(dev, skb);
1678 return dev_queue_xmit(skb);
1681 static const struct bpf_func_proto bpf_redirect_proto = {
1682 .func = bpf_redirect,
1684 .ret_type = RET_INTEGER,
1685 .arg1_type = ARG_ANYTHING,
1686 .arg2_type = ARG_ANYTHING,
1689 static u64 bpf_get_cgroup_classid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
1691 return task_get_classid((struct sk_buff *) (unsigned long) r1);
1694 static const struct bpf_func_proto bpf_get_cgroup_classid_proto = {
1695 .func = bpf_get_cgroup_classid,
1697 .ret_type = RET_INTEGER,
1698 .arg1_type = ARG_PTR_TO_CTX,
1701 static u64 bpf_get_route_realm(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
1703 return dst_tclassid((struct sk_buff *) (unsigned long) r1);
1706 static const struct bpf_func_proto bpf_get_route_realm_proto = {
1707 .func = bpf_get_route_realm,
1709 .ret_type = RET_INTEGER,
1710 .arg1_type = ARG_PTR_TO_CTX,
1713 static u64 bpf_skb_vlan_push(u64 r1, u64 r2, u64 vlan_tci, u64 r4, u64 r5)
1715 struct sk_buff *skb = (struct sk_buff *) (long) r1;
1716 __be16 vlan_proto = (__force __be16) r2;
1719 if (unlikely(vlan_proto != htons(ETH_P_8021Q) &&
1720 vlan_proto != htons(ETH_P_8021AD)))
1721 vlan_proto = htons(ETH_P_8021Q);
1723 ret = skb_vlan_push(skb, vlan_proto, vlan_tci);
1724 bpf_compute_data_end(skb);
1728 const struct bpf_func_proto bpf_skb_vlan_push_proto = {
1729 .func = bpf_skb_vlan_push,
1731 .ret_type = RET_INTEGER,
1732 .arg1_type = ARG_PTR_TO_CTX,
1733 .arg2_type = ARG_ANYTHING,
1734 .arg3_type = ARG_ANYTHING,
1736 EXPORT_SYMBOL_GPL(bpf_skb_vlan_push_proto);
1738 static u64 bpf_skb_vlan_pop(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
1740 struct sk_buff *skb = (struct sk_buff *) (long) r1;
1743 ret = skb_vlan_pop(skb);
1744 bpf_compute_data_end(skb);
1748 const struct bpf_func_proto bpf_skb_vlan_pop_proto = {
1749 .func = bpf_skb_vlan_pop,
1751 .ret_type = RET_INTEGER,
1752 .arg1_type = ARG_PTR_TO_CTX,
1754 EXPORT_SYMBOL_GPL(bpf_skb_vlan_pop_proto);
1756 bool bpf_helper_changes_skb_data(void *func)
1758 if (func == bpf_skb_vlan_push)
1760 if (func == bpf_skb_vlan_pop)
1762 if (func == bpf_skb_store_bytes)
1764 if (func == bpf_l3_csum_replace)
1766 if (func == bpf_l4_csum_replace)
1772 static unsigned short bpf_tunnel_key_af(u64 flags)
1774 return flags & BPF_F_TUNINFO_IPV6 ? AF_INET6 : AF_INET;
1777 static u64 bpf_skb_get_tunnel_key(u64 r1, u64 r2, u64 size, u64 flags, u64 r5)
1779 struct sk_buff *skb = (struct sk_buff *) (long) r1;
1780 struct bpf_tunnel_key *to = (struct bpf_tunnel_key *) (long) r2;
1781 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
1782 u8 compat[sizeof(struct bpf_tunnel_key)];
1786 if (unlikely(!info || (flags & ~(BPF_F_TUNINFO_IPV6)))) {
1790 if (ip_tunnel_info_af(info) != bpf_tunnel_key_af(flags)) {
1794 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
1797 case offsetof(struct bpf_tunnel_key, tunnel_label):
1798 case offsetof(struct bpf_tunnel_key, tunnel_ext):
1800 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
1801 /* Fixup deprecated structure layouts here, so we have
1802 * a common path later on.
1804 if (ip_tunnel_info_af(info) != AF_INET)
1807 to = (struct bpf_tunnel_key *)compat;
1814 to->tunnel_id = be64_to_cpu(info->key.tun_id);
1815 to->tunnel_tos = info->key.tos;
1816 to->tunnel_ttl = info->key.ttl;
1818 if (flags & BPF_F_TUNINFO_IPV6) {
1819 memcpy(to->remote_ipv6, &info->key.u.ipv6.src,
1820 sizeof(to->remote_ipv6));
1821 to->tunnel_label = be32_to_cpu(info->key.label);
1823 to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src);
1826 if (unlikely(size != sizeof(struct bpf_tunnel_key)))
1827 memcpy(to_orig, to, size);
1831 memset(to_orig, 0, size);
1835 static const struct bpf_func_proto bpf_skb_get_tunnel_key_proto = {
1836 .func = bpf_skb_get_tunnel_key,
1838 .ret_type = RET_INTEGER,
1839 .arg1_type = ARG_PTR_TO_CTX,
1840 .arg2_type = ARG_PTR_TO_RAW_STACK,
1841 .arg3_type = ARG_CONST_STACK_SIZE,
1842 .arg4_type = ARG_ANYTHING,
1845 static u64 bpf_skb_get_tunnel_opt(u64 r1, u64 r2, u64 size, u64 r4, u64 r5)
1847 struct sk_buff *skb = (struct sk_buff *) (long) r1;
1848 u8 *to = (u8 *) (long) r2;
1849 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
1852 if (unlikely(!info ||
1853 !(info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))) {
1857 if (unlikely(size < info->options_len)) {
1862 ip_tunnel_info_opts_get(to, info);
1863 if (size > info->options_len)
1864 memset(to + info->options_len, 0, size - info->options_len);
1866 return info->options_len;
1868 memset(to, 0, size);
1872 static const struct bpf_func_proto bpf_skb_get_tunnel_opt_proto = {
1873 .func = bpf_skb_get_tunnel_opt,
1875 .ret_type = RET_INTEGER,
1876 .arg1_type = ARG_PTR_TO_CTX,
1877 .arg2_type = ARG_PTR_TO_RAW_STACK,
1878 .arg3_type = ARG_CONST_STACK_SIZE,
1881 static struct metadata_dst __percpu *md_dst;
1883 static u64 bpf_skb_set_tunnel_key(u64 r1, u64 r2, u64 size, u64 flags, u64 r5)
1885 struct sk_buff *skb = (struct sk_buff *) (long) r1;
1886 struct bpf_tunnel_key *from = (struct bpf_tunnel_key *) (long) r2;
1887 struct metadata_dst *md = this_cpu_ptr(md_dst);
1888 u8 compat[sizeof(struct bpf_tunnel_key)];
1889 struct ip_tunnel_info *info;
1891 if (unlikely(flags & ~(BPF_F_TUNINFO_IPV6 | BPF_F_ZERO_CSUM_TX |
1892 BPF_F_DONT_FRAGMENT)))
1894 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
1896 case offsetof(struct bpf_tunnel_key, tunnel_label):
1897 case offsetof(struct bpf_tunnel_key, tunnel_ext):
1898 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
1899 /* Fixup deprecated structure layouts here, so we have
1900 * a common path later on.
1902 memcpy(compat, from, size);
1903 memset(compat + size, 0, sizeof(compat) - size);
1904 from = (struct bpf_tunnel_key *)compat;
1910 if (unlikely((!(flags & BPF_F_TUNINFO_IPV6) && from->tunnel_label) ||
1915 dst_hold((struct dst_entry *) md);
1916 skb_dst_set(skb, (struct dst_entry *) md);
1918 info = &md->u.tun_info;
1919 info->mode = IP_TUNNEL_INFO_TX;
1921 info->key.tun_flags = TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_NOCACHE;
1922 if (flags & BPF_F_DONT_FRAGMENT)
1923 info->key.tun_flags |= TUNNEL_DONT_FRAGMENT;
1925 info->key.tun_id = cpu_to_be64(from->tunnel_id);
1926 info->key.tos = from->tunnel_tos;
1927 info->key.ttl = from->tunnel_ttl;
1929 if (flags & BPF_F_TUNINFO_IPV6) {
1930 info->mode |= IP_TUNNEL_INFO_IPV6;
1931 memcpy(&info->key.u.ipv6.dst, from->remote_ipv6,
1932 sizeof(from->remote_ipv6));
1933 info->key.label = cpu_to_be32(from->tunnel_label) &
1934 IPV6_FLOWLABEL_MASK;
1936 info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4);
1937 if (flags & BPF_F_ZERO_CSUM_TX)
1938 info->key.tun_flags &= ~TUNNEL_CSUM;
1944 static const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = {
1945 .func = bpf_skb_set_tunnel_key,
1947 .ret_type = RET_INTEGER,
1948 .arg1_type = ARG_PTR_TO_CTX,
1949 .arg2_type = ARG_PTR_TO_STACK,
1950 .arg3_type = ARG_CONST_STACK_SIZE,
1951 .arg4_type = ARG_ANYTHING,
1954 static u64 bpf_skb_set_tunnel_opt(u64 r1, u64 r2, u64 size, u64 r4, u64 r5)
1956 struct sk_buff *skb = (struct sk_buff *) (long) r1;
1957 u8 *from = (u8 *) (long) r2;
1958 struct ip_tunnel_info *info = skb_tunnel_info(skb);
1959 const struct metadata_dst *md = this_cpu_ptr(md_dst);
1961 if (unlikely(info != &md->u.tun_info || (size & (sizeof(u32) - 1))))
1963 if (unlikely(size > IP_TUNNEL_OPTS_MAX))
1966 ip_tunnel_info_opts_set(info, from, size);
1971 static const struct bpf_func_proto bpf_skb_set_tunnel_opt_proto = {
1972 .func = bpf_skb_set_tunnel_opt,
1974 .ret_type = RET_INTEGER,
1975 .arg1_type = ARG_PTR_TO_CTX,
1976 .arg2_type = ARG_PTR_TO_STACK,
1977 .arg3_type = ARG_CONST_STACK_SIZE,
1980 static const struct bpf_func_proto *
1981 bpf_get_skb_set_tunnel_proto(enum bpf_func_id which)
1984 /* Race is not possible, since it's called from verifier
1985 * that is holding verifier mutex.
1987 md_dst = metadata_dst_alloc_percpu(IP_TUNNEL_OPTS_MAX,
1994 case BPF_FUNC_skb_set_tunnel_key:
1995 return &bpf_skb_set_tunnel_key_proto;
1996 case BPF_FUNC_skb_set_tunnel_opt:
1997 return &bpf_skb_set_tunnel_opt_proto;
2003 static const struct bpf_func_proto *
2004 sk_filter_func_proto(enum bpf_func_id func_id)
2007 case BPF_FUNC_map_lookup_elem:
2008 return &bpf_map_lookup_elem_proto;
2009 case BPF_FUNC_map_update_elem:
2010 return &bpf_map_update_elem_proto;
2011 case BPF_FUNC_map_delete_elem:
2012 return &bpf_map_delete_elem_proto;
2013 case BPF_FUNC_get_prandom_u32:
2014 return &bpf_get_prandom_u32_proto;
2015 case BPF_FUNC_get_smp_processor_id:
2016 return &bpf_get_smp_processor_id_proto;
2017 case BPF_FUNC_tail_call:
2018 return &bpf_tail_call_proto;
2019 case BPF_FUNC_ktime_get_ns:
2020 return &bpf_ktime_get_ns_proto;
2021 case BPF_FUNC_trace_printk:
2022 if (capable(CAP_SYS_ADMIN))
2023 return bpf_get_trace_printk_proto();
2029 static const struct bpf_func_proto *
2030 tc_cls_act_func_proto(enum bpf_func_id func_id)
2033 case BPF_FUNC_skb_store_bytes:
2034 return &bpf_skb_store_bytes_proto;
2035 case BPF_FUNC_skb_load_bytes:
2036 return &bpf_skb_load_bytes_proto;
2037 case BPF_FUNC_csum_diff:
2038 return &bpf_csum_diff_proto;
2039 case BPF_FUNC_l3_csum_replace:
2040 return &bpf_l3_csum_replace_proto;
2041 case BPF_FUNC_l4_csum_replace:
2042 return &bpf_l4_csum_replace_proto;
2043 case BPF_FUNC_clone_redirect:
2044 return &bpf_clone_redirect_proto;
2045 case BPF_FUNC_get_cgroup_classid:
2046 return &bpf_get_cgroup_classid_proto;
2047 case BPF_FUNC_skb_vlan_push:
2048 return &bpf_skb_vlan_push_proto;
2049 case BPF_FUNC_skb_vlan_pop:
2050 return &bpf_skb_vlan_pop_proto;
2051 case BPF_FUNC_skb_get_tunnel_key:
2052 return &bpf_skb_get_tunnel_key_proto;
2053 case BPF_FUNC_skb_set_tunnel_key:
2054 return bpf_get_skb_set_tunnel_proto(func_id);
2055 case BPF_FUNC_skb_get_tunnel_opt:
2056 return &bpf_skb_get_tunnel_opt_proto;
2057 case BPF_FUNC_skb_set_tunnel_opt:
2058 return bpf_get_skb_set_tunnel_proto(func_id);
2059 case BPF_FUNC_redirect:
2060 return &bpf_redirect_proto;
2061 case BPF_FUNC_get_route_realm:
2062 return &bpf_get_route_realm_proto;
2063 case BPF_FUNC_perf_event_output:
2064 return bpf_get_event_output_proto();
2066 return sk_filter_func_proto(func_id);
2070 static bool __is_valid_access(int off, int size, enum bpf_access_type type)
2073 if (off < 0 || off >= sizeof(struct __sk_buff))
2076 /* disallow misaligned access */
2077 if (off % size != 0)
2080 /* all __sk_buff fields are __u32 */
2087 static bool sk_filter_is_valid_access(int off, int size,
2088 enum bpf_access_type type)
2091 case offsetof(struct __sk_buff, tc_classid):
2092 case offsetof(struct __sk_buff, data):
2093 case offsetof(struct __sk_buff, data_end):
2097 if (type == BPF_WRITE) {
2099 case offsetof(struct __sk_buff, cb[0]) ...
2100 offsetof(struct __sk_buff, cb[4]):
2107 return __is_valid_access(off, size, type);
2110 static bool tc_cls_act_is_valid_access(int off, int size,
2111 enum bpf_access_type type)
2113 if (type == BPF_WRITE) {
2115 case offsetof(struct __sk_buff, mark):
2116 case offsetof(struct __sk_buff, tc_index):
2117 case offsetof(struct __sk_buff, priority):
2118 case offsetof(struct __sk_buff, cb[0]) ...
2119 offsetof(struct __sk_buff, cb[4]):
2120 case offsetof(struct __sk_buff, tc_classid):
2126 return __is_valid_access(off, size, type);
2129 static u32 bpf_net_convert_ctx_access(enum bpf_access_type type, int dst_reg,
2130 int src_reg, int ctx_off,
2131 struct bpf_insn *insn_buf,
2132 struct bpf_prog *prog)
2134 struct bpf_insn *insn = insn_buf;
2137 case offsetof(struct __sk_buff, len):
2138 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, len) != 4);
2140 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
2141 offsetof(struct sk_buff, len));
2144 case offsetof(struct __sk_buff, protocol):
2145 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
2147 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
2148 offsetof(struct sk_buff, protocol));
2151 case offsetof(struct __sk_buff, vlan_proto):
2152 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2);
2154 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
2155 offsetof(struct sk_buff, vlan_proto));
2158 case offsetof(struct __sk_buff, priority):
2159 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, priority) != 4);
2161 if (type == BPF_WRITE)
2162 *insn++ = BPF_STX_MEM(BPF_W, dst_reg, src_reg,
2163 offsetof(struct sk_buff, priority));
2165 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
2166 offsetof(struct sk_buff, priority));
2169 case offsetof(struct __sk_buff, ingress_ifindex):
2170 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, skb_iif) != 4);
2172 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
2173 offsetof(struct sk_buff, skb_iif));
2176 case offsetof(struct __sk_buff, ifindex):
2177 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
2179 *insn++ = BPF_LDX_MEM(bytes_to_bpf_size(FIELD_SIZEOF(struct sk_buff, dev)),
2181 offsetof(struct sk_buff, dev));
2182 *insn++ = BPF_JMP_IMM(BPF_JEQ, dst_reg, 0, 1);
2183 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, dst_reg,
2184 offsetof(struct net_device, ifindex));
2187 case offsetof(struct __sk_buff, hash):
2188 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
2190 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
2191 offsetof(struct sk_buff, hash));
2194 case offsetof(struct __sk_buff, mark):
2195 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
2197 if (type == BPF_WRITE)
2198 *insn++ = BPF_STX_MEM(BPF_W, dst_reg, src_reg,
2199 offsetof(struct sk_buff, mark));
2201 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
2202 offsetof(struct sk_buff, mark));
2205 case offsetof(struct __sk_buff, pkt_type):
2206 return convert_skb_access(SKF_AD_PKTTYPE, dst_reg, src_reg, insn);
2208 case offsetof(struct __sk_buff, queue_mapping):
2209 return convert_skb_access(SKF_AD_QUEUE, dst_reg, src_reg, insn);
2211 case offsetof(struct __sk_buff, vlan_present):
2212 return convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
2213 dst_reg, src_reg, insn);
2215 case offsetof(struct __sk_buff, vlan_tci):
2216 return convert_skb_access(SKF_AD_VLAN_TAG,
2217 dst_reg, src_reg, insn);
2219 case offsetof(struct __sk_buff, cb[0]) ...
2220 offsetof(struct __sk_buff, cb[4]):
2221 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, data) < 20);
2223 prog->cb_access = 1;
2224 ctx_off -= offsetof(struct __sk_buff, cb[0]);
2225 ctx_off += offsetof(struct sk_buff, cb);
2226 ctx_off += offsetof(struct qdisc_skb_cb, data);
2227 if (type == BPF_WRITE)
2228 *insn++ = BPF_STX_MEM(BPF_W, dst_reg, src_reg, ctx_off);
2230 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg, ctx_off);
2233 case offsetof(struct __sk_buff, tc_classid):
2234 ctx_off -= offsetof(struct __sk_buff, tc_classid);
2235 ctx_off += offsetof(struct sk_buff, cb);
2236 ctx_off += offsetof(struct qdisc_skb_cb, tc_classid);
2237 if (type == BPF_WRITE)
2238 *insn++ = BPF_STX_MEM(BPF_H, dst_reg, src_reg, ctx_off);
2240 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg, ctx_off);
2243 case offsetof(struct __sk_buff, data):
2244 *insn++ = BPF_LDX_MEM(bytes_to_bpf_size(FIELD_SIZEOF(struct sk_buff, data)),
2246 offsetof(struct sk_buff, data));
2249 case offsetof(struct __sk_buff, data_end):
2250 ctx_off -= offsetof(struct __sk_buff, data_end);
2251 ctx_off += offsetof(struct sk_buff, cb);
2252 ctx_off += offsetof(struct bpf_skb_data_end, data_end);
2253 *insn++ = BPF_LDX_MEM(bytes_to_bpf_size(sizeof(void *)),
2254 dst_reg, src_reg, ctx_off);
2257 case offsetof(struct __sk_buff, tc_index):
2258 #ifdef CONFIG_NET_SCHED
2259 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, tc_index) != 2);
2261 if (type == BPF_WRITE)
2262 *insn++ = BPF_STX_MEM(BPF_H, dst_reg, src_reg,
2263 offsetof(struct sk_buff, tc_index));
2265 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
2266 offsetof(struct sk_buff, tc_index));
2269 if (type == BPF_WRITE)
2270 *insn++ = BPF_MOV64_REG(dst_reg, dst_reg);
2272 *insn++ = BPF_MOV64_IMM(dst_reg, 0);
2277 return insn - insn_buf;
2280 static const struct bpf_verifier_ops sk_filter_ops = {
2281 .get_func_proto = sk_filter_func_proto,
2282 .is_valid_access = sk_filter_is_valid_access,
2283 .convert_ctx_access = bpf_net_convert_ctx_access,
2286 static const struct bpf_verifier_ops tc_cls_act_ops = {
2287 .get_func_proto = tc_cls_act_func_proto,
2288 .is_valid_access = tc_cls_act_is_valid_access,
2289 .convert_ctx_access = bpf_net_convert_ctx_access,
2292 static struct bpf_prog_type_list sk_filter_type __read_mostly = {
2293 .ops = &sk_filter_ops,
2294 .type = BPF_PROG_TYPE_SOCKET_FILTER,
2297 static struct bpf_prog_type_list sched_cls_type __read_mostly = {
2298 .ops = &tc_cls_act_ops,
2299 .type = BPF_PROG_TYPE_SCHED_CLS,
2302 static struct bpf_prog_type_list sched_act_type __read_mostly = {
2303 .ops = &tc_cls_act_ops,
2304 .type = BPF_PROG_TYPE_SCHED_ACT,
2307 static int __init register_sk_filter_ops(void)
2309 bpf_register_prog_type(&sk_filter_type);
2310 bpf_register_prog_type(&sched_cls_type);
2311 bpf_register_prog_type(&sched_act_type);
2315 late_initcall(register_sk_filter_ops);
2317 int sk_detach_filter(struct sock *sk)
2320 struct sk_filter *filter;
2322 if (sock_flag(sk, SOCK_FILTER_LOCKED))
2325 filter = rcu_dereference_protected(sk->sk_filter,
2326 lockdep_sock_is_held(sk));
2328 RCU_INIT_POINTER(sk->sk_filter, NULL);
2329 sk_filter_uncharge(sk, filter);
2335 EXPORT_SYMBOL_GPL(sk_detach_filter);
2337 int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf,
2340 struct sock_fprog_kern *fprog;
2341 struct sk_filter *filter;
2345 filter = rcu_dereference_protected(sk->sk_filter,
2346 lockdep_sock_is_held(sk));
2350 /* We're copying the filter that has been originally attached,
2351 * so no conversion/decode needed anymore. eBPF programs that
2352 * have no original program cannot be dumped through this.
2355 fprog = filter->prog->orig_prog;
2361 /* User space only enquires number of filter blocks. */
2365 if (len < fprog->len)
2369 if (copy_to_user(ubuf, fprog->filter, bpf_classic_proglen(fprog)))
2372 /* Instead of bytes, the API requests to return the number