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, RET_A are remaped into 2 insns. */
534 case BPF_RET | BPF_A:
535 case BPF_RET | BPF_K:
536 *insn++ = BPF_MOV32_RAW(BPF_RVAL(fp->code) == BPF_K ?
537 BPF_K : BPF_X, BPF_REG_0,
539 *insn = BPF_EXIT_INSN();
542 /* Store to stack. */
545 *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) ==
546 BPF_ST ? BPF_REG_A : BPF_REG_X,
547 -(BPF_MEMWORDS - fp->k) * 4);
550 /* Load from stack. */
551 case BPF_LD | BPF_MEM:
552 case BPF_LDX | BPF_MEM:
553 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
554 BPF_REG_A : BPF_REG_X, BPF_REG_FP,
555 -(BPF_MEMWORDS - fp->k) * 4);
559 case BPF_LD | BPF_IMM:
560 case BPF_LDX | BPF_IMM:
561 *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ?
562 BPF_REG_A : BPF_REG_X, fp->k);
566 case BPF_MISC | BPF_TAX:
567 *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
571 case BPF_MISC | BPF_TXA:
572 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X);
575 /* A = skb->len or X = skb->len */
576 case BPF_LD | BPF_W | BPF_LEN:
577 case BPF_LDX | BPF_W | BPF_LEN:
578 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
579 BPF_REG_A : BPF_REG_X, BPF_REG_CTX,
580 offsetof(struct sk_buff, len));
583 /* Access seccomp_data fields. */
584 case BPF_LDX | BPF_ABS | BPF_W:
585 /* A = *(u32 *) (ctx + K) */
586 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k);
589 /* Unknown instruction. */
596 memcpy(new_insn, tmp_insns,
597 sizeof(*insn) * (insn - tmp_insns));
598 new_insn += insn - tmp_insns;
602 /* Only calculating new length. */
603 *new_len = new_insn - new_prog;
608 if (new_flen != new_insn - new_prog) {
609 new_flen = new_insn - new_prog;
616 BUG_ON(*new_len != new_flen);
625 * As we dont want to clear mem[] array for each packet going through
626 * __bpf_prog_run(), we check that filter loaded by user never try to read
627 * a cell if not previously written, and we check all branches to be sure
628 * a malicious user doesn't try to abuse us.
630 static int check_load_and_stores(const struct sock_filter *filter, int flen)
632 u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */
635 BUILD_BUG_ON(BPF_MEMWORDS > 16);
637 masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL);
641 memset(masks, 0xff, flen * sizeof(*masks));
643 for (pc = 0; pc < flen; pc++) {
644 memvalid &= masks[pc];
646 switch (filter[pc].code) {
649 memvalid |= (1 << filter[pc].k);
651 case BPF_LD | BPF_MEM:
652 case BPF_LDX | BPF_MEM:
653 if (!(memvalid & (1 << filter[pc].k))) {
658 case BPF_JMP | BPF_JA:
659 /* A jump must set masks on target */
660 masks[pc + 1 + filter[pc].k] &= memvalid;
663 case BPF_JMP | BPF_JEQ | BPF_K:
664 case BPF_JMP | BPF_JEQ | BPF_X:
665 case BPF_JMP | BPF_JGE | BPF_K:
666 case BPF_JMP | BPF_JGE | BPF_X:
667 case BPF_JMP | BPF_JGT | BPF_K:
668 case BPF_JMP | BPF_JGT | BPF_X:
669 case BPF_JMP | BPF_JSET | BPF_K:
670 case BPF_JMP | BPF_JSET | BPF_X:
671 /* A jump must set masks on targets */
672 masks[pc + 1 + filter[pc].jt] &= memvalid;
673 masks[pc + 1 + filter[pc].jf] &= memvalid;
683 static bool chk_code_allowed(u16 code_to_probe)
685 static const bool codes[] = {
686 /* 32 bit ALU operations */
687 [BPF_ALU | BPF_ADD | BPF_K] = true,
688 [BPF_ALU | BPF_ADD | BPF_X] = true,
689 [BPF_ALU | BPF_SUB | BPF_K] = true,
690 [BPF_ALU | BPF_SUB | BPF_X] = true,
691 [BPF_ALU | BPF_MUL | BPF_K] = true,
692 [BPF_ALU | BPF_MUL | BPF_X] = true,
693 [BPF_ALU | BPF_DIV | BPF_K] = true,
694 [BPF_ALU | BPF_DIV | BPF_X] = true,
695 [BPF_ALU | BPF_MOD | BPF_K] = true,
696 [BPF_ALU | BPF_MOD | BPF_X] = true,
697 [BPF_ALU | BPF_AND | BPF_K] = true,
698 [BPF_ALU | BPF_AND | BPF_X] = true,
699 [BPF_ALU | BPF_OR | BPF_K] = true,
700 [BPF_ALU | BPF_OR | BPF_X] = true,
701 [BPF_ALU | BPF_XOR | BPF_K] = true,
702 [BPF_ALU | BPF_XOR | BPF_X] = true,
703 [BPF_ALU | BPF_LSH | BPF_K] = true,
704 [BPF_ALU | BPF_LSH | BPF_X] = true,
705 [BPF_ALU | BPF_RSH | BPF_K] = true,
706 [BPF_ALU | BPF_RSH | BPF_X] = true,
707 [BPF_ALU | BPF_NEG] = true,
708 /* Load instructions */
709 [BPF_LD | BPF_W | BPF_ABS] = true,
710 [BPF_LD | BPF_H | BPF_ABS] = true,
711 [BPF_LD | BPF_B | BPF_ABS] = true,
712 [BPF_LD | BPF_W | BPF_LEN] = true,
713 [BPF_LD | BPF_W | BPF_IND] = true,
714 [BPF_LD | BPF_H | BPF_IND] = true,
715 [BPF_LD | BPF_B | BPF_IND] = true,
716 [BPF_LD | BPF_IMM] = true,
717 [BPF_LD | BPF_MEM] = true,
718 [BPF_LDX | BPF_W | BPF_LEN] = true,
719 [BPF_LDX | BPF_B | BPF_MSH] = true,
720 [BPF_LDX | BPF_IMM] = true,
721 [BPF_LDX | BPF_MEM] = true,
722 /* Store instructions */
725 /* Misc instructions */
726 [BPF_MISC | BPF_TAX] = true,
727 [BPF_MISC | BPF_TXA] = true,
728 /* Return instructions */
729 [BPF_RET | BPF_K] = true,
730 [BPF_RET | BPF_A] = true,
731 /* Jump instructions */
732 [BPF_JMP | BPF_JA] = true,
733 [BPF_JMP | BPF_JEQ | BPF_K] = true,
734 [BPF_JMP | BPF_JEQ | BPF_X] = true,
735 [BPF_JMP | BPF_JGE | BPF_K] = true,
736 [BPF_JMP | BPF_JGE | BPF_X] = true,
737 [BPF_JMP | BPF_JGT | BPF_K] = true,
738 [BPF_JMP | BPF_JGT | BPF_X] = true,
739 [BPF_JMP | BPF_JSET | BPF_K] = true,
740 [BPF_JMP | BPF_JSET | BPF_X] = true,
743 if (code_to_probe >= ARRAY_SIZE(codes))
746 return codes[code_to_probe];
750 * bpf_check_classic - verify socket filter code
751 * @filter: filter to verify
752 * @flen: length of filter
754 * Check the user's filter code. If we let some ugly
755 * filter code slip through kaboom! The filter must contain
756 * no references or jumps that are out of range, no illegal
757 * instructions, and must end with a RET instruction.
759 * All jumps are forward as they are not signed.
761 * Returns 0 if the rule set is legal or -EINVAL if not.
763 static int bpf_check_classic(const struct sock_filter *filter,
769 if (flen == 0 || flen > BPF_MAXINSNS)
772 /* Check the filter code now */
773 for (pc = 0; pc < flen; pc++) {
774 const struct sock_filter *ftest = &filter[pc];
776 /* May we actually operate on this code? */
777 if (!chk_code_allowed(ftest->code))
780 /* Some instructions need special checks */
781 switch (ftest->code) {
782 case BPF_ALU | BPF_DIV | BPF_K:
783 case BPF_ALU | BPF_MOD | BPF_K:
784 /* Check for division by zero */
788 case BPF_LD | BPF_MEM:
789 case BPF_LDX | BPF_MEM:
792 /* Check for invalid memory addresses */
793 if (ftest->k >= BPF_MEMWORDS)
796 case BPF_JMP | BPF_JA:
797 /* Note, the large ftest->k might cause loops.
798 * Compare this with conditional jumps below,
799 * where offsets are limited. --ANK (981016)
801 if (ftest->k >= (unsigned int)(flen - pc - 1))
804 case BPF_JMP | BPF_JEQ | BPF_K:
805 case BPF_JMP | BPF_JEQ | BPF_X:
806 case BPF_JMP | BPF_JGE | BPF_K:
807 case BPF_JMP | BPF_JGE | BPF_X:
808 case BPF_JMP | BPF_JGT | BPF_K:
809 case BPF_JMP | BPF_JGT | BPF_X:
810 case BPF_JMP | BPF_JSET | BPF_K:
811 case BPF_JMP | BPF_JSET | BPF_X:
812 /* Both conditionals must be safe */
813 if (pc + ftest->jt + 1 >= flen ||
814 pc + ftest->jf + 1 >= flen)
817 case BPF_LD | BPF_W | BPF_ABS:
818 case BPF_LD | BPF_H | BPF_ABS:
819 case BPF_LD | BPF_B | BPF_ABS:
821 if (bpf_anc_helper(ftest) & BPF_ANC)
823 /* Ancillary operation unknown or unsupported */
824 if (anc_found == false && ftest->k >= SKF_AD_OFF)
829 /* Last instruction must be a RET code */
830 switch (filter[flen - 1].code) {
831 case BPF_RET | BPF_K:
832 case BPF_RET | BPF_A:
833 return check_load_and_stores(filter, flen);
839 static int bpf_prog_store_orig_filter(struct bpf_prog *fp,
840 const struct sock_fprog *fprog)
842 unsigned int fsize = bpf_classic_proglen(fprog);
843 struct sock_fprog_kern *fkprog;
845 fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL);
849 fkprog = fp->orig_prog;
850 fkprog->len = fprog->len;
852 fkprog->filter = kmemdup(fp->insns, fsize,
853 GFP_KERNEL | __GFP_NOWARN);
854 if (!fkprog->filter) {
855 kfree(fp->orig_prog);
862 static void bpf_release_orig_filter(struct bpf_prog *fp)
864 struct sock_fprog_kern *fprog = fp->orig_prog;
867 kfree(fprog->filter);
872 static void __bpf_prog_release(struct bpf_prog *prog)
874 if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) {
877 bpf_release_orig_filter(prog);
882 static void __sk_filter_release(struct sk_filter *fp)
884 __bpf_prog_release(fp->prog);
889 * sk_filter_release_rcu - Release a socket filter by rcu_head
890 * @rcu: rcu_head that contains the sk_filter to free
892 static void sk_filter_release_rcu(struct rcu_head *rcu)
894 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
896 __sk_filter_release(fp);
900 * sk_filter_release - release a socket filter
901 * @fp: filter to remove
903 * Remove a filter from a socket and release its resources.
905 static void sk_filter_release(struct sk_filter *fp)
907 if (atomic_dec_and_test(&fp->refcnt))
908 call_rcu(&fp->rcu, sk_filter_release_rcu);
911 void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp)
913 u32 filter_size = bpf_prog_size(fp->prog->len);
915 atomic_sub(filter_size, &sk->sk_omem_alloc);
916 sk_filter_release(fp);
919 /* try to charge the socket memory if there is space available
920 * return true on success
922 bool sk_filter_charge(struct sock *sk, struct sk_filter *fp)
924 u32 filter_size = bpf_prog_size(fp->prog->len);
926 /* same check as in sock_kmalloc() */
927 if (filter_size <= sysctl_optmem_max &&
928 atomic_read(&sk->sk_omem_alloc) + filter_size < sysctl_optmem_max) {
929 atomic_inc(&fp->refcnt);
930 atomic_add(filter_size, &sk->sk_omem_alloc);
936 static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp)
938 struct sock_filter *old_prog;
939 struct bpf_prog *old_fp;
940 int err, new_len, old_len = fp->len;
942 /* We are free to overwrite insns et al right here as it
943 * won't be used at this point in time anymore internally
944 * after the migration to the internal BPF instruction
947 BUILD_BUG_ON(sizeof(struct sock_filter) !=
948 sizeof(struct bpf_insn));
950 /* Conversion cannot happen on overlapping memory areas,
951 * so we need to keep the user BPF around until the 2nd
952 * pass. At this time, the user BPF is stored in fp->insns.
954 old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter),
955 GFP_KERNEL | __GFP_NOWARN);
961 /* 1st pass: calculate the new program length. */
962 err = bpf_convert_filter(old_prog, old_len, NULL, &new_len);
966 /* Expand fp for appending the new filter representation. */
968 fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0);
970 /* The old_fp is still around in case we couldn't
971 * allocate new memory, so uncharge on that one.
980 /* 2nd pass: remap sock_filter insns into bpf_insn insns. */
981 err = bpf_convert_filter(old_prog, old_len, fp->insnsi, &new_len);
983 /* 2nd bpf_convert_filter() can fail only if it fails
984 * to allocate memory, remapping must succeed. Note,
985 * that at this time old_fp has already been released
990 bpf_prog_select_runtime(fp);
998 __bpf_prog_release(fp);
1002 static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp,
1003 bpf_aux_classic_check_t trans)
1007 fp->bpf_func = NULL;
1010 err = bpf_check_classic(fp->insns, fp->len);
1012 __bpf_prog_release(fp);
1013 return ERR_PTR(err);
1016 /* There might be additional checks and transformations
1017 * needed on classic filters, f.e. in case of seccomp.
1020 err = trans(fp->insns, fp->len);
1022 __bpf_prog_release(fp);
1023 return ERR_PTR(err);
1027 /* Probe if we can JIT compile the filter and if so, do
1028 * the compilation of the filter.
1030 bpf_jit_compile(fp);
1032 /* JIT compiler couldn't process this filter, so do the
1033 * internal BPF translation for the optimized interpreter.
1036 fp = bpf_migrate_filter(fp);
1042 * bpf_prog_create - create an unattached filter
1043 * @pfp: the unattached filter that is created
1044 * @fprog: the filter program
1046 * Create a filter independent of any socket. We first run some
1047 * sanity checks on it to make sure it does not explode on us later.
1048 * If an error occurs or there is insufficient memory for the filter
1049 * a negative errno code is returned. On success the return is zero.
1051 int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog)
1053 unsigned int fsize = bpf_classic_proglen(fprog);
1054 struct bpf_prog *fp;
1056 /* Make sure new filter is there and in the right amounts. */
1057 if (fprog->filter == NULL)
1060 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1064 memcpy(fp->insns, fprog->filter, fsize);
1066 fp->len = fprog->len;
1067 /* Since unattached filters are not copied back to user
1068 * space through sk_get_filter(), we do not need to hold
1069 * a copy here, and can spare us the work.
1071 fp->orig_prog = NULL;
1073 /* bpf_prepare_filter() already takes care of freeing
1074 * memory in case something goes wrong.
1076 fp = bpf_prepare_filter(fp, NULL);
1083 EXPORT_SYMBOL_GPL(bpf_prog_create);
1086 * bpf_prog_create_from_user - create an unattached filter from user buffer
1087 * @pfp: the unattached filter that is created
1088 * @fprog: the filter program
1089 * @trans: post-classic verifier transformation handler
1090 * @save_orig: save classic BPF program
1092 * This function effectively does the same as bpf_prog_create(), only
1093 * that it builds up its insns buffer from user space provided buffer.
1094 * It also allows for passing a bpf_aux_classic_check_t handler.
1096 int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
1097 bpf_aux_classic_check_t trans, bool save_orig)
1099 unsigned int fsize = bpf_classic_proglen(fprog);
1100 struct bpf_prog *fp;
1103 /* Make sure new filter is there and in the right amounts. */
1104 if (fprog->filter == NULL)
1107 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1111 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
1112 __bpf_prog_free(fp);
1116 fp->len = fprog->len;
1117 fp->orig_prog = NULL;
1120 err = bpf_prog_store_orig_filter(fp, fprog);
1122 __bpf_prog_free(fp);
1127 /* bpf_prepare_filter() already takes care of freeing
1128 * memory in case something goes wrong.
1130 fp = bpf_prepare_filter(fp, trans);
1137 EXPORT_SYMBOL_GPL(bpf_prog_create_from_user);
1139 void bpf_prog_destroy(struct bpf_prog *fp)
1141 __bpf_prog_release(fp);
1143 EXPORT_SYMBOL_GPL(bpf_prog_destroy);
1145 static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk)
1147 struct sk_filter *fp, *old_fp;
1149 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
1154 atomic_set(&fp->refcnt, 0);
1156 if (!sk_filter_charge(sk, fp)) {
1161 old_fp = rcu_dereference_protected(sk->sk_filter,
1162 sock_owned_by_user(sk));
1163 rcu_assign_pointer(sk->sk_filter, fp);
1166 sk_filter_uncharge(sk, old_fp);
1171 static int __reuseport_attach_prog(struct bpf_prog *prog, struct sock *sk)
1173 struct bpf_prog *old_prog;
1176 if (bpf_prog_size(prog->len) > sysctl_optmem_max)
1179 if (sk_unhashed(sk)) {
1180 err = reuseport_alloc(sk);
1183 } else if (!rcu_access_pointer(sk->sk_reuseport_cb)) {
1184 /* The socket wasn't bound with SO_REUSEPORT */
1188 old_prog = reuseport_attach_prog(sk, prog);
1190 bpf_prog_destroy(old_prog);
1196 struct bpf_prog *__get_filter(struct sock_fprog *fprog, struct sock *sk)
1198 unsigned int fsize = bpf_classic_proglen(fprog);
1199 unsigned int bpf_fsize = bpf_prog_size(fprog->len);
1200 struct bpf_prog *prog;
1203 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1204 return ERR_PTR(-EPERM);
1206 /* Make sure new filter is there and in the right amounts. */
1207 if (fprog->filter == NULL)
1208 return ERR_PTR(-EINVAL);
1210 prog = bpf_prog_alloc(bpf_fsize, 0);
1212 return ERR_PTR(-ENOMEM);
1214 if (copy_from_user(prog->insns, fprog->filter, fsize)) {
1215 __bpf_prog_free(prog);
1216 return ERR_PTR(-EFAULT);
1219 prog->len = fprog->len;
1221 err = bpf_prog_store_orig_filter(prog, fprog);
1223 __bpf_prog_free(prog);
1224 return ERR_PTR(-ENOMEM);
1227 /* bpf_prepare_filter() already takes care of freeing
1228 * memory in case something goes wrong.
1230 return bpf_prepare_filter(prog, NULL);
1234 * sk_attach_filter - attach a socket filter
1235 * @fprog: the filter program
1236 * @sk: the socket to use
1238 * Attach the user's filter code. We first run some sanity checks on
1239 * it to make sure it does not explode on us later. If an error
1240 * occurs or there is insufficient memory for the filter a negative
1241 * errno code is returned. On success the return is zero.
1243 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1245 struct bpf_prog *prog = __get_filter(fprog, sk);
1249 return PTR_ERR(prog);
1251 err = __sk_attach_prog(prog, sk);
1253 __bpf_prog_release(prog);
1259 EXPORT_SYMBOL_GPL(sk_attach_filter);
1261 int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1263 struct bpf_prog *prog = __get_filter(fprog, sk);
1267 return PTR_ERR(prog);
1269 err = __reuseport_attach_prog(prog, sk);
1271 __bpf_prog_release(prog);
1278 static struct bpf_prog *__get_bpf(u32 ufd, struct sock *sk)
1280 struct bpf_prog *prog;
1282 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1283 return ERR_PTR(-EPERM);
1285 prog = bpf_prog_get(ufd);
1289 if (prog->type != BPF_PROG_TYPE_SOCKET_FILTER) {
1291 return ERR_PTR(-EINVAL);
1297 int sk_attach_bpf(u32 ufd, struct sock *sk)
1299 struct bpf_prog *prog = __get_bpf(ufd, sk);
1303 return PTR_ERR(prog);
1305 err = __sk_attach_prog(prog, sk);
1314 int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk)
1316 struct bpf_prog *prog = __get_bpf(ufd, sk);
1320 return PTR_ERR(prog);
1322 err = __reuseport_attach_prog(prog, sk);
1331 #define BPF_RECOMPUTE_CSUM(flags) ((flags) & 1)
1332 #define BPF_LDST_LEN 16U
1334 static u64 bpf_skb_store_bytes(u64 r1, u64 r2, u64 r3, u64 r4, u64 flags)
1336 struct sk_buff *skb = (struct sk_buff *) (long) r1;
1337 int offset = (int) r2;
1338 void *from = (void *) (long) r3;
1339 unsigned int len = (unsigned int) r4;
1340 char buf[BPF_LDST_LEN];
1343 /* bpf verifier guarantees that:
1344 * 'from' pointer points to bpf program stack
1345 * 'len' bytes of it were initialized
1347 * 'skb' is a valid pointer to 'struct sk_buff'
1349 * so check for invalid 'offset' and too large 'len'
1351 if (unlikely((u32) offset > 0xffff || len > sizeof(buf)))
1354 if (unlikely(skb_cloned(skb) &&
1355 !skb_clone_writable(skb, offset + len)))
1358 ptr = skb_header_pointer(skb, offset, len, buf);
1362 if (BPF_RECOMPUTE_CSUM(flags))
1363 skb_postpull_rcsum(skb, ptr, len);
1365 memcpy(ptr, from, len);
1368 /* skb_store_bits cannot return -EFAULT here */
1369 skb_store_bits(skb, offset, ptr, len);
1371 if (BPF_RECOMPUTE_CSUM(flags) && skb->ip_summed == CHECKSUM_COMPLETE)
1372 skb->csum = csum_add(skb->csum, csum_partial(ptr, len, 0));
1376 const struct bpf_func_proto bpf_skb_store_bytes_proto = {
1377 .func = bpf_skb_store_bytes,
1379 .ret_type = RET_INTEGER,
1380 .arg1_type = ARG_PTR_TO_CTX,
1381 .arg2_type = ARG_ANYTHING,
1382 .arg3_type = ARG_PTR_TO_STACK,
1383 .arg4_type = ARG_CONST_STACK_SIZE,
1384 .arg5_type = ARG_ANYTHING,
1387 static u64 bpf_skb_load_bytes(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
1389 const struct sk_buff *skb = (const struct sk_buff *)(unsigned long) r1;
1390 int offset = (int) r2;
1391 void *to = (void *)(unsigned long) r3;
1392 unsigned int len = (unsigned int) r4;
1395 if (unlikely((u32) offset > 0xffff || len > BPF_LDST_LEN))
1398 ptr = skb_header_pointer(skb, offset, len, to);
1402 memcpy(to, ptr, len);
1407 const struct bpf_func_proto bpf_skb_load_bytes_proto = {
1408 .func = bpf_skb_load_bytes,
1410 .ret_type = RET_INTEGER,
1411 .arg1_type = ARG_PTR_TO_CTX,
1412 .arg2_type = ARG_ANYTHING,
1413 .arg3_type = ARG_PTR_TO_STACK,
1414 .arg4_type = ARG_CONST_STACK_SIZE,
1417 #define BPF_HEADER_FIELD_SIZE(flags) ((flags) & 0x0f)
1418 #define BPF_IS_PSEUDO_HEADER(flags) ((flags) & 0x10)
1420 static u64 bpf_l3_csum_replace(u64 r1, u64 r2, u64 from, u64 to, u64 flags)
1422 struct sk_buff *skb = (struct sk_buff *) (long) r1;
1423 int offset = (int) r2;
1426 if (unlikely((u32) offset > 0xffff))
1429 if (unlikely(skb_cloned(skb) &&
1430 !skb_clone_writable(skb, offset + sizeof(sum))))
1433 ptr = skb_header_pointer(skb, offset, sizeof(sum), &sum);
1437 switch (BPF_HEADER_FIELD_SIZE(flags)) {
1439 csum_replace2(ptr, from, to);
1442 csum_replace4(ptr, from, to);
1449 /* skb_store_bits guaranteed to not return -EFAULT here */
1450 skb_store_bits(skb, offset, ptr, sizeof(sum));
1455 const struct bpf_func_proto bpf_l3_csum_replace_proto = {
1456 .func = bpf_l3_csum_replace,
1458 .ret_type = RET_INTEGER,
1459 .arg1_type = ARG_PTR_TO_CTX,
1460 .arg2_type = ARG_ANYTHING,
1461 .arg3_type = ARG_ANYTHING,
1462 .arg4_type = ARG_ANYTHING,
1463 .arg5_type = ARG_ANYTHING,
1466 static u64 bpf_l4_csum_replace(u64 r1, u64 r2, u64 from, u64 to, u64 flags)
1468 struct sk_buff *skb = (struct sk_buff *) (long) r1;
1469 bool is_pseudo = !!BPF_IS_PSEUDO_HEADER(flags);
1470 int offset = (int) r2;
1473 if (unlikely((u32) offset > 0xffff))
1476 if (unlikely(skb_cloned(skb) &&
1477 !skb_clone_writable(skb, offset + sizeof(sum))))
1480 ptr = skb_header_pointer(skb, offset, sizeof(sum), &sum);
1484 switch (BPF_HEADER_FIELD_SIZE(flags)) {
1486 inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo);
1489 inet_proto_csum_replace4(ptr, skb, from, to, is_pseudo);
1496 /* skb_store_bits guaranteed to not return -EFAULT here */
1497 skb_store_bits(skb, offset, ptr, sizeof(sum));
1502 const struct bpf_func_proto bpf_l4_csum_replace_proto = {
1503 .func = bpf_l4_csum_replace,
1505 .ret_type = RET_INTEGER,
1506 .arg1_type = ARG_PTR_TO_CTX,
1507 .arg2_type = ARG_ANYTHING,
1508 .arg3_type = ARG_ANYTHING,
1509 .arg4_type = ARG_ANYTHING,
1510 .arg5_type = ARG_ANYTHING,
1513 #define BPF_IS_REDIRECT_INGRESS(flags) ((flags) & 1)
1515 static u64 bpf_clone_redirect(u64 r1, u64 ifindex, u64 flags, u64 r4, u64 r5)
1517 struct sk_buff *skb = (struct sk_buff *) (long) r1, *skb2;
1518 struct net_device *dev;
1520 dev = dev_get_by_index_rcu(dev_net(skb->dev), ifindex);
1524 skb2 = skb_clone(skb, GFP_ATOMIC);
1525 if (unlikely(!skb2))
1528 if (BPF_IS_REDIRECT_INGRESS(flags))
1529 return dev_forward_skb(dev, skb2);
1532 skb_sender_cpu_clear(skb2);
1533 return dev_queue_xmit(skb2);
1536 const struct bpf_func_proto bpf_clone_redirect_proto = {
1537 .func = bpf_clone_redirect,
1539 .ret_type = RET_INTEGER,
1540 .arg1_type = ARG_PTR_TO_CTX,
1541 .arg2_type = ARG_ANYTHING,
1542 .arg3_type = ARG_ANYTHING,
1545 struct redirect_info {
1550 static DEFINE_PER_CPU(struct redirect_info, redirect_info);
1551 static u64 bpf_redirect(u64 ifindex, u64 flags, u64 r3, u64 r4, u64 r5)
1553 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
1555 ri->ifindex = ifindex;
1557 return TC_ACT_REDIRECT;
1560 int skb_do_redirect(struct sk_buff *skb)
1562 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
1563 struct net_device *dev;
1565 dev = dev_get_by_index_rcu(dev_net(skb->dev), ri->ifindex);
1567 if (unlikely(!dev)) {
1572 if (BPF_IS_REDIRECT_INGRESS(ri->flags))
1573 return dev_forward_skb(dev, skb);
1576 skb_sender_cpu_clear(skb);
1577 return dev_queue_xmit(skb);
1580 const struct bpf_func_proto bpf_redirect_proto = {
1581 .func = bpf_redirect,
1583 .ret_type = RET_INTEGER,
1584 .arg1_type = ARG_ANYTHING,
1585 .arg2_type = ARG_ANYTHING,
1588 static u64 bpf_get_cgroup_classid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
1590 return task_get_classid((struct sk_buff *) (unsigned long) r1);
1593 static const struct bpf_func_proto bpf_get_cgroup_classid_proto = {
1594 .func = bpf_get_cgroup_classid,
1596 .ret_type = RET_INTEGER,
1597 .arg1_type = ARG_PTR_TO_CTX,
1600 static u64 bpf_get_route_realm(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
1602 #ifdef CONFIG_IP_ROUTE_CLASSID
1603 const struct dst_entry *dst;
1605 dst = skb_dst((struct sk_buff *) (unsigned long) r1);
1607 return dst->tclassid;
1612 static const struct bpf_func_proto bpf_get_route_realm_proto = {
1613 .func = bpf_get_route_realm,
1615 .ret_type = RET_INTEGER,
1616 .arg1_type = ARG_PTR_TO_CTX,
1619 static u64 bpf_skb_vlan_push(u64 r1, u64 r2, u64 vlan_tci, u64 r4, u64 r5)
1621 struct sk_buff *skb = (struct sk_buff *) (long) r1;
1622 __be16 vlan_proto = (__force __be16) r2;
1624 if (unlikely(vlan_proto != htons(ETH_P_8021Q) &&
1625 vlan_proto != htons(ETH_P_8021AD)))
1626 vlan_proto = htons(ETH_P_8021Q);
1628 return skb_vlan_push(skb, vlan_proto, vlan_tci);
1631 const struct bpf_func_proto bpf_skb_vlan_push_proto = {
1632 .func = bpf_skb_vlan_push,
1634 .ret_type = RET_INTEGER,
1635 .arg1_type = ARG_PTR_TO_CTX,
1636 .arg2_type = ARG_ANYTHING,
1637 .arg3_type = ARG_ANYTHING,
1639 EXPORT_SYMBOL_GPL(bpf_skb_vlan_push_proto);
1641 static u64 bpf_skb_vlan_pop(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
1643 struct sk_buff *skb = (struct sk_buff *) (long) r1;
1645 return skb_vlan_pop(skb);
1648 const struct bpf_func_proto bpf_skb_vlan_pop_proto = {
1649 .func = bpf_skb_vlan_pop,
1651 .ret_type = RET_INTEGER,
1652 .arg1_type = ARG_PTR_TO_CTX,
1654 EXPORT_SYMBOL_GPL(bpf_skb_vlan_pop_proto);
1656 bool bpf_helper_changes_skb_data(void *func)
1658 if (func == bpf_skb_vlan_push)
1660 if (func == bpf_skb_vlan_pop)
1665 static u64 bpf_skb_get_tunnel_key(u64 r1, u64 r2, u64 size, u64 flags, u64 r5)
1667 struct sk_buff *skb = (struct sk_buff *) (long) r1;
1668 struct bpf_tunnel_key *to = (struct bpf_tunnel_key *) (long) r2;
1669 struct ip_tunnel_info *info = skb_tunnel_info(skb);
1671 if (unlikely(size != sizeof(struct bpf_tunnel_key) || flags || !info))
1673 if (ip_tunnel_info_af(info) != AF_INET)
1676 to->tunnel_id = be64_to_cpu(info->key.tun_id);
1677 to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src);
1682 const struct bpf_func_proto bpf_skb_get_tunnel_key_proto = {
1683 .func = bpf_skb_get_tunnel_key,
1685 .ret_type = RET_INTEGER,
1686 .arg1_type = ARG_PTR_TO_CTX,
1687 .arg2_type = ARG_PTR_TO_STACK,
1688 .arg3_type = ARG_CONST_STACK_SIZE,
1689 .arg4_type = ARG_ANYTHING,
1692 static struct metadata_dst __percpu *md_dst;
1694 static u64 bpf_skb_set_tunnel_key(u64 r1, u64 r2, u64 size, u64 flags, u64 r5)
1696 struct sk_buff *skb = (struct sk_buff *) (long) r1;
1697 struct bpf_tunnel_key *from = (struct bpf_tunnel_key *) (long) r2;
1698 struct metadata_dst *md = this_cpu_ptr(md_dst);
1699 struct ip_tunnel_info *info;
1701 if (unlikely(size != sizeof(struct bpf_tunnel_key) || flags))
1705 dst_hold((struct dst_entry *) md);
1706 skb_dst_set(skb, (struct dst_entry *) md);
1708 info = &md->u.tun_info;
1709 info->mode = IP_TUNNEL_INFO_TX;
1710 info->key.tun_flags = TUNNEL_KEY;
1711 info->key.tun_id = cpu_to_be64(from->tunnel_id);
1712 info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4);
1717 const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = {
1718 .func = bpf_skb_set_tunnel_key,
1720 .ret_type = RET_INTEGER,
1721 .arg1_type = ARG_PTR_TO_CTX,
1722 .arg2_type = ARG_PTR_TO_STACK,
1723 .arg3_type = ARG_CONST_STACK_SIZE,
1724 .arg4_type = ARG_ANYTHING,
1727 static const struct bpf_func_proto *bpf_get_skb_set_tunnel_key_proto(void)
1730 /* race is not possible, since it's called from
1731 * verifier that is holding verifier mutex
1733 md_dst = metadata_dst_alloc_percpu(0, GFP_KERNEL);
1737 return &bpf_skb_set_tunnel_key_proto;
1740 static const struct bpf_func_proto *
1741 sk_filter_func_proto(enum bpf_func_id func_id)
1744 case BPF_FUNC_map_lookup_elem:
1745 return &bpf_map_lookup_elem_proto;
1746 case BPF_FUNC_map_update_elem:
1747 return &bpf_map_update_elem_proto;
1748 case BPF_FUNC_map_delete_elem:
1749 return &bpf_map_delete_elem_proto;
1750 case BPF_FUNC_get_prandom_u32:
1751 return &bpf_get_prandom_u32_proto;
1752 case BPF_FUNC_get_smp_processor_id:
1753 return &bpf_get_smp_processor_id_proto;
1754 case BPF_FUNC_tail_call:
1755 return &bpf_tail_call_proto;
1756 case BPF_FUNC_ktime_get_ns:
1757 return &bpf_ktime_get_ns_proto;
1758 case BPF_FUNC_trace_printk:
1759 if (capable(CAP_SYS_ADMIN))
1760 return bpf_get_trace_printk_proto();
1766 static const struct bpf_func_proto *
1767 tc_cls_act_func_proto(enum bpf_func_id func_id)
1770 case BPF_FUNC_skb_store_bytes:
1771 return &bpf_skb_store_bytes_proto;
1772 case BPF_FUNC_skb_load_bytes:
1773 return &bpf_skb_load_bytes_proto;
1774 case BPF_FUNC_l3_csum_replace:
1775 return &bpf_l3_csum_replace_proto;
1776 case BPF_FUNC_l4_csum_replace:
1777 return &bpf_l4_csum_replace_proto;
1778 case BPF_FUNC_clone_redirect:
1779 return &bpf_clone_redirect_proto;
1780 case BPF_FUNC_get_cgroup_classid:
1781 return &bpf_get_cgroup_classid_proto;
1782 case BPF_FUNC_skb_vlan_push:
1783 return &bpf_skb_vlan_push_proto;
1784 case BPF_FUNC_skb_vlan_pop:
1785 return &bpf_skb_vlan_pop_proto;
1786 case BPF_FUNC_skb_get_tunnel_key:
1787 return &bpf_skb_get_tunnel_key_proto;
1788 case BPF_FUNC_skb_set_tunnel_key:
1789 return bpf_get_skb_set_tunnel_key_proto();
1790 case BPF_FUNC_redirect:
1791 return &bpf_redirect_proto;
1792 case BPF_FUNC_get_route_realm:
1793 return &bpf_get_route_realm_proto;
1795 return sk_filter_func_proto(func_id);
1799 static bool __is_valid_access(int off, int size, enum bpf_access_type type)
1802 if (off < 0 || off >= sizeof(struct __sk_buff))
1805 /* disallow misaligned access */
1806 if (off % size != 0)
1809 /* all __sk_buff fields are __u32 */
1816 static bool sk_filter_is_valid_access(int off, int size,
1817 enum bpf_access_type type)
1819 if (off == offsetof(struct __sk_buff, tc_classid))
1822 if (type == BPF_WRITE) {
1824 case offsetof(struct __sk_buff, cb[0]) ...
1825 offsetof(struct __sk_buff, cb[4]):
1832 return __is_valid_access(off, size, type);
1835 static bool tc_cls_act_is_valid_access(int off, int size,
1836 enum bpf_access_type type)
1838 if (off == offsetof(struct __sk_buff, tc_classid))
1839 return type == BPF_WRITE ? true : false;
1841 if (type == BPF_WRITE) {
1843 case offsetof(struct __sk_buff, mark):
1844 case offsetof(struct __sk_buff, tc_index):
1845 case offsetof(struct __sk_buff, priority):
1846 case offsetof(struct __sk_buff, cb[0]) ...
1847 offsetof(struct __sk_buff, cb[4]):
1853 return __is_valid_access(off, size, type);
1856 static u32 bpf_net_convert_ctx_access(enum bpf_access_type type, int dst_reg,
1857 int src_reg, int ctx_off,
1858 struct bpf_insn *insn_buf,
1859 struct bpf_prog *prog)
1861 struct bpf_insn *insn = insn_buf;
1864 case offsetof(struct __sk_buff, len):
1865 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, len) != 4);
1867 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
1868 offsetof(struct sk_buff, len));
1871 case offsetof(struct __sk_buff, protocol):
1872 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
1874 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
1875 offsetof(struct sk_buff, protocol));
1878 case offsetof(struct __sk_buff, vlan_proto):
1879 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2);
1881 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
1882 offsetof(struct sk_buff, vlan_proto));
1885 case offsetof(struct __sk_buff, priority):
1886 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, priority) != 4);
1888 if (type == BPF_WRITE)
1889 *insn++ = BPF_STX_MEM(BPF_W, dst_reg, src_reg,
1890 offsetof(struct sk_buff, priority));
1892 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
1893 offsetof(struct sk_buff, priority));
1896 case offsetof(struct __sk_buff, ingress_ifindex):
1897 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, skb_iif) != 4);
1899 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
1900 offsetof(struct sk_buff, skb_iif));
1903 case offsetof(struct __sk_buff, ifindex):
1904 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
1906 *insn++ = BPF_LDX_MEM(bytes_to_bpf_size(FIELD_SIZEOF(struct sk_buff, dev)),
1908 offsetof(struct sk_buff, dev));
1909 *insn++ = BPF_JMP_IMM(BPF_JEQ, dst_reg, 0, 1);
1910 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, dst_reg,
1911 offsetof(struct net_device, ifindex));
1914 case offsetof(struct __sk_buff, hash):
1915 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
1917 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
1918 offsetof(struct sk_buff, hash));
1921 case offsetof(struct __sk_buff, mark):
1922 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
1924 if (type == BPF_WRITE)
1925 *insn++ = BPF_STX_MEM(BPF_W, dst_reg, src_reg,
1926 offsetof(struct sk_buff, mark));
1928 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
1929 offsetof(struct sk_buff, mark));
1932 case offsetof(struct __sk_buff, pkt_type):
1933 return convert_skb_access(SKF_AD_PKTTYPE, dst_reg, src_reg, insn);
1935 case offsetof(struct __sk_buff, queue_mapping):
1936 return convert_skb_access(SKF_AD_QUEUE, dst_reg, src_reg, insn);
1938 case offsetof(struct __sk_buff, vlan_present):
1939 return convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
1940 dst_reg, src_reg, insn);
1942 case offsetof(struct __sk_buff, vlan_tci):
1943 return convert_skb_access(SKF_AD_VLAN_TAG,
1944 dst_reg, src_reg, insn);
1946 case offsetof(struct __sk_buff, cb[0]) ...
1947 offsetof(struct __sk_buff, cb[4]):
1948 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, data) < 20);
1950 prog->cb_access = 1;
1951 ctx_off -= offsetof(struct __sk_buff, cb[0]);
1952 ctx_off += offsetof(struct sk_buff, cb);
1953 ctx_off += offsetof(struct qdisc_skb_cb, data);
1954 if (type == BPF_WRITE)
1955 *insn++ = BPF_STX_MEM(BPF_W, dst_reg, src_reg, ctx_off);
1957 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg, ctx_off);
1960 case offsetof(struct __sk_buff, tc_classid):
1961 ctx_off -= offsetof(struct __sk_buff, tc_classid);
1962 ctx_off += offsetof(struct sk_buff, cb);
1963 ctx_off += offsetof(struct qdisc_skb_cb, tc_classid);
1964 WARN_ON(type != BPF_WRITE);
1965 *insn++ = BPF_STX_MEM(BPF_H, dst_reg, src_reg, ctx_off);
1968 case offsetof(struct __sk_buff, tc_index):
1969 #ifdef CONFIG_NET_SCHED
1970 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, tc_index) != 2);
1972 if (type == BPF_WRITE)
1973 *insn++ = BPF_STX_MEM(BPF_H, dst_reg, src_reg,
1974 offsetof(struct sk_buff, tc_index));
1976 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
1977 offsetof(struct sk_buff, tc_index));
1980 if (type == BPF_WRITE)
1981 *insn++ = BPF_MOV64_REG(dst_reg, dst_reg);
1983 *insn++ = BPF_MOV64_IMM(dst_reg, 0);
1988 return insn - insn_buf;
1991 static const struct bpf_verifier_ops sk_filter_ops = {
1992 .get_func_proto = sk_filter_func_proto,
1993 .is_valid_access = sk_filter_is_valid_access,
1994 .convert_ctx_access = bpf_net_convert_ctx_access,
1997 static const struct bpf_verifier_ops tc_cls_act_ops = {
1998 .get_func_proto = tc_cls_act_func_proto,
1999 .is_valid_access = tc_cls_act_is_valid_access,
2000 .convert_ctx_access = bpf_net_convert_ctx_access,
2003 static struct bpf_prog_type_list sk_filter_type __read_mostly = {
2004 .ops = &sk_filter_ops,
2005 .type = BPF_PROG_TYPE_SOCKET_FILTER,
2008 static struct bpf_prog_type_list sched_cls_type __read_mostly = {
2009 .ops = &tc_cls_act_ops,
2010 .type = BPF_PROG_TYPE_SCHED_CLS,
2013 static struct bpf_prog_type_list sched_act_type __read_mostly = {
2014 .ops = &tc_cls_act_ops,
2015 .type = BPF_PROG_TYPE_SCHED_ACT,
2018 static int __init register_sk_filter_ops(void)
2020 bpf_register_prog_type(&sk_filter_type);
2021 bpf_register_prog_type(&sched_cls_type);
2022 bpf_register_prog_type(&sched_act_type);
2026 late_initcall(register_sk_filter_ops);
2028 int sk_detach_filter(struct sock *sk)
2031 struct sk_filter *filter;
2033 if (sock_flag(sk, SOCK_FILTER_LOCKED))
2036 filter = rcu_dereference_protected(sk->sk_filter,
2037 sock_owned_by_user(sk));
2039 RCU_INIT_POINTER(sk->sk_filter, NULL);
2040 sk_filter_uncharge(sk, filter);
2046 EXPORT_SYMBOL_GPL(sk_detach_filter);
2048 int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf,
2051 struct sock_fprog_kern *fprog;
2052 struct sk_filter *filter;
2056 filter = rcu_dereference_protected(sk->sk_filter,
2057 sock_owned_by_user(sk));
2061 /* We're copying the filter that has been originally attached,
2062 * so no conversion/decode needed anymore. eBPF programs that
2063 * have no original program cannot be dumped through this.
2066 fprog = filter->prog->orig_prog;
2072 /* User space only enquires number of filter blocks. */
2076 if (len < fprog->len)
2080 if (copy_to_user(ubuf, fprog->filter, bpf_classic_proglen(fprog)))
2083 /* Instead of bytes, the API requests to return the number