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>
51 * sk_filter - run a packet through a socket filter
52 * @sk: sock associated with &sk_buff
53 * @skb: buffer to filter
55 * Run the filter code and then cut skb->data to correct size returned by
56 * SK_RUN_FILTER. If pkt_len is 0 we toss packet. If skb->len is smaller
57 * than pkt_len we keep whole skb->data. This is the socket level
58 * wrapper to SK_RUN_FILTER. It returns 0 if the packet should
59 * be accepted or -EPERM if the packet should be tossed.
62 int sk_filter(struct sock *sk, struct sk_buff *skb)
65 struct sk_filter *filter;
68 * If the skb was allocated from pfmemalloc reserves, only
69 * allow SOCK_MEMALLOC sockets to use it as this socket is
72 if (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC))
75 err = security_sock_rcv_skb(sk, skb);
80 filter = rcu_dereference(sk->sk_filter);
82 unsigned int pkt_len = SK_RUN_FILTER(filter, skb);
84 err = pkt_len ? pskb_trim(skb, pkt_len) : -EPERM;
90 EXPORT_SYMBOL(sk_filter);
92 static u64 __skb_get_pay_offset(u64 ctx, u64 a, u64 x, u64 r4, u64 r5)
94 return skb_get_poff((struct sk_buff *)(unsigned long) ctx);
97 static u64 __skb_get_nlattr(u64 ctx, u64 a, u64 x, u64 r4, u64 r5)
99 struct sk_buff *skb = (struct sk_buff *)(unsigned long) ctx;
102 if (skb_is_nonlinear(skb))
105 if (skb->len < sizeof(struct nlattr))
108 if (a > skb->len - sizeof(struct nlattr))
111 nla = nla_find((struct nlattr *) &skb->data[a], skb->len - a, x);
113 return (void *) nla - (void *) skb->data;
118 static u64 __skb_get_nlattr_nest(u64 ctx, u64 a, u64 x, u64 r4, u64 r5)
120 struct sk_buff *skb = (struct sk_buff *)(unsigned long) ctx;
123 if (skb_is_nonlinear(skb))
126 if (skb->len < sizeof(struct nlattr))
129 if (a > skb->len - sizeof(struct nlattr))
132 nla = (struct nlattr *) &skb->data[a];
133 if (nla->nla_len > skb->len - a)
136 nla = nla_find_nested(nla, x);
138 return (void *) nla - (void *) skb->data;
143 static u64 __get_raw_cpu_id(u64 ctx, u64 a, u64 x, u64 r4, u64 r5)
145 return raw_smp_processor_id();
148 /* note that this only generates 32-bit random numbers */
149 static u64 __get_random_u32(u64 ctx, u64 a, u64 x, u64 r4, u64 r5)
151 return prandom_u32();
154 static u32 convert_skb_access(int skb_field, int dst_reg, int src_reg,
155 struct bpf_insn *insn_buf)
157 struct bpf_insn *insn = insn_buf;
161 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
163 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
164 offsetof(struct sk_buff, mark));
168 *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_TYPE_OFFSET());
169 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, PKT_TYPE_MAX);
170 #ifdef __BIG_ENDIAN_BITFIELD
171 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 5);
176 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, queue_mapping) != 2);
178 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
179 offsetof(struct sk_buff, queue_mapping));
182 case SKF_AD_VLAN_TAG:
183 case SKF_AD_VLAN_TAG_PRESENT:
184 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_tci) != 2);
185 BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000);
187 /* dst_reg = *(u16 *) (src_reg + offsetof(vlan_tci)) */
188 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
189 offsetof(struct sk_buff, vlan_tci));
190 if (skb_field == SKF_AD_VLAN_TAG) {
191 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg,
195 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 12);
197 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, 1);
202 return insn - insn_buf;
205 static bool convert_bpf_extensions(struct sock_filter *fp,
206 struct bpf_insn **insnp)
208 struct bpf_insn *insn = *insnp;
212 case SKF_AD_OFF + SKF_AD_PROTOCOL:
213 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
215 /* A = *(u16 *) (CTX + offsetof(protocol)) */
216 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
217 offsetof(struct sk_buff, protocol));
218 /* A = ntohs(A) [emitting a nop or swap16] */
219 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
222 case SKF_AD_OFF + SKF_AD_PKTTYPE:
223 cnt = convert_skb_access(SKF_AD_PKTTYPE, BPF_REG_A, BPF_REG_CTX, insn);
227 case SKF_AD_OFF + SKF_AD_IFINDEX:
228 case SKF_AD_OFF + SKF_AD_HATYPE:
229 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
230 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, type) != 2);
231 BUILD_BUG_ON(bytes_to_bpf_size(FIELD_SIZEOF(struct sk_buff, dev)) < 0);
233 *insn++ = BPF_LDX_MEM(bytes_to_bpf_size(FIELD_SIZEOF(struct sk_buff, dev)),
234 BPF_REG_TMP, BPF_REG_CTX,
235 offsetof(struct sk_buff, dev));
236 /* if (tmp != 0) goto pc + 1 */
237 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_TMP, 0, 1);
238 *insn++ = BPF_EXIT_INSN();
239 if (fp->k == SKF_AD_OFF + SKF_AD_IFINDEX)
240 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_TMP,
241 offsetof(struct net_device, ifindex));
243 *insn = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_TMP,
244 offsetof(struct net_device, type));
247 case SKF_AD_OFF + SKF_AD_MARK:
248 cnt = convert_skb_access(SKF_AD_MARK, BPF_REG_A, BPF_REG_CTX, insn);
252 case SKF_AD_OFF + SKF_AD_RXHASH:
253 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
255 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX,
256 offsetof(struct sk_buff, hash));
259 case SKF_AD_OFF + SKF_AD_QUEUE:
260 cnt = convert_skb_access(SKF_AD_QUEUE, BPF_REG_A, BPF_REG_CTX, insn);
264 case SKF_AD_OFF + SKF_AD_VLAN_TAG:
265 cnt = convert_skb_access(SKF_AD_VLAN_TAG,
266 BPF_REG_A, BPF_REG_CTX, insn);
270 case SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT:
271 cnt = convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
272 BPF_REG_A, BPF_REG_CTX, insn);
276 case SKF_AD_OFF + SKF_AD_VLAN_TPID:
277 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2);
279 /* A = *(u16 *) (CTX + offsetof(vlan_proto)) */
280 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
281 offsetof(struct sk_buff, vlan_proto));
282 /* A = ntohs(A) [emitting a nop or swap16] */
283 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
286 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
287 case SKF_AD_OFF + SKF_AD_NLATTR:
288 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
289 case SKF_AD_OFF + SKF_AD_CPU:
290 case SKF_AD_OFF + SKF_AD_RANDOM:
292 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
294 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_A);
296 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_X);
297 /* Emit call(arg1=CTX, arg2=A, arg3=X) */
299 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
300 *insn = BPF_EMIT_CALL(__skb_get_pay_offset);
302 case SKF_AD_OFF + SKF_AD_NLATTR:
303 *insn = BPF_EMIT_CALL(__skb_get_nlattr);
305 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
306 *insn = BPF_EMIT_CALL(__skb_get_nlattr_nest);
308 case SKF_AD_OFF + SKF_AD_CPU:
309 *insn = BPF_EMIT_CALL(__get_raw_cpu_id);
311 case SKF_AD_OFF + SKF_AD_RANDOM:
312 *insn = BPF_EMIT_CALL(__get_random_u32);
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 * User BPF's register A is mapped to our BPF register 6, user BPF
354 * register X is mapped to BPF register 7; frame pointer is always
355 * register 10; Context 'void *ctx' is stored in register 1, that is,
356 * for socket filters: ctx == 'struct sk_buff *', for seccomp:
357 * ctx == 'struct seccomp_data *'.
359 static int bpf_convert_filter(struct sock_filter *prog, int len,
360 struct bpf_insn *new_prog, int *new_len)
362 int new_flen = 0, pass = 0, target, i;
363 struct bpf_insn *new_insn;
364 struct sock_filter *fp;
368 BUILD_BUG_ON(BPF_MEMWORDS * sizeof(u32) > MAX_BPF_STACK);
369 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
371 if (len <= 0 || len > BPF_MAXINSNS)
375 addrs = kcalloc(len, sizeof(*addrs),
376 GFP_KERNEL | __GFP_NOWARN);
386 *new_insn = BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1);
389 for (i = 0; i < len; fp++, i++) {
390 struct bpf_insn tmp_insns[6] = { };
391 struct bpf_insn *insn = tmp_insns;
394 addrs[i] = new_insn - new_prog;
397 /* All arithmetic insns and skb loads map as-is. */
398 case BPF_ALU | BPF_ADD | BPF_X:
399 case BPF_ALU | BPF_ADD | BPF_K:
400 case BPF_ALU | BPF_SUB | BPF_X:
401 case BPF_ALU | BPF_SUB | BPF_K:
402 case BPF_ALU | BPF_AND | BPF_X:
403 case BPF_ALU | BPF_AND | BPF_K:
404 case BPF_ALU | BPF_OR | BPF_X:
405 case BPF_ALU | BPF_OR | BPF_K:
406 case BPF_ALU | BPF_LSH | BPF_X:
407 case BPF_ALU | BPF_LSH | BPF_K:
408 case BPF_ALU | BPF_RSH | BPF_X:
409 case BPF_ALU | BPF_RSH | BPF_K:
410 case BPF_ALU | BPF_XOR | BPF_X:
411 case BPF_ALU | BPF_XOR | BPF_K:
412 case BPF_ALU | BPF_MUL | BPF_X:
413 case BPF_ALU | BPF_MUL | BPF_K:
414 case BPF_ALU | BPF_DIV | BPF_X:
415 case BPF_ALU | BPF_DIV | BPF_K:
416 case BPF_ALU | BPF_MOD | BPF_X:
417 case BPF_ALU | BPF_MOD | BPF_K:
418 case BPF_ALU | BPF_NEG:
419 case BPF_LD | BPF_ABS | BPF_W:
420 case BPF_LD | BPF_ABS | BPF_H:
421 case BPF_LD | BPF_ABS | BPF_B:
422 case BPF_LD | BPF_IND | BPF_W:
423 case BPF_LD | BPF_IND | BPF_H:
424 case BPF_LD | BPF_IND | BPF_B:
425 /* Check for overloaded BPF extension and
426 * directly convert it if found, otherwise
427 * just move on with mapping.
429 if (BPF_CLASS(fp->code) == BPF_LD &&
430 BPF_MODE(fp->code) == BPF_ABS &&
431 convert_bpf_extensions(fp, &insn))
434 *insn = BPF_RAW_INSN(fp->code, BPF_REG_A, BPF_REG_X, 0, fp->k);
437 /* Jump transformation cannot use BPF block macros
438 * everywhere as offset calculation and target updates
439 * require a bit more work than the rest, i.e. jump
440 * opcodes map as-is, but offsets need adjustment.
443 #define BPF_EMIT_JMP \
445 if (target >= len || target < 0) \
447 insn->off = addrs ? addrs[target] - addrs[i] - 1 : 0; \
448 /* Adjust pc relative offset for 2nd or 3rd insn. */ \
449 insn->off -= insn - tmp_insns; \
452 case BPF_JMP | BPF_JA:
453 target = i + fp->k + 1;
454 insn->code = fp->code;
458 case BPF_JMP | BPF_JEQ | BPF_K:
459 case BPF_JMP | BPF_JEQ | BPF_X:
460 case BPF_JMP | BPF_JSET | BPF_K:
461 case BPF_JMP | BPF_JSET | BPF_X:
462 case BPF_JMP | BPF_JGT | BPF_K:
463 case BPF_JMP | BPF_JGT | BPF_X:
464 case BPF_JMP | BPF_JGE | BPF_K:
465 case BPF_JMP | BPF_JGE | BPF_X:
466 if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) {
467 /* BPF immediates are signed, zero extend
468 * immediate into tmp register and use it
471 *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k);
473 insn->dst_reg = BPF_REG_A;
474 insn->src_reg = BPF_REG_TMP;
477 insn->dst_reg = BPF_REG_A;
478 insn->src_reg = BPF_REG_X;
480 bpf_src = BPF_SRC(fp->code);
483 /* Common case where 'jump_false' is next insn. */
485 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
486 target = i + fp->jt + 1;
491 /* Convert JEQ into JNE when 'jump_true' is next insn. */
492 if (fp->jt == 0 && BPF_OP(fp->code) == BPF_JEQ) {
493 insn->code = BPF_JMP | BPF_JNE | bpf_src;
494 target = i + fp->jf + 1;
499 /* Other jumps are mapped into two insns: Jxx and JA. */
500 target = i + fp->jt + 1;
501 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
505 insn->code = BPF_JMP | BPF_JA;
506 target = i + fp->jf + 1;
510 /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */
511 case BPF_LDX | BPF_MSH | BPF_B:
513 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_A);
514 /* A = BPF_R0 = *(u8 *) (skb->data + K) */
515 *insn++ = BPF_LD_ABS(BPF_B, fp->k);
517 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf);
519 *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2);
521 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
523 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP);
526 /* RET_K, RET_A are remaped into 2 insns. */
527 case BPF_RET | BPF_A:
528 case BPF_RET | BPF_K:
529 *insn++ = BPF_MOV32_RAW(BPF_RVAL(fp->code) == BPF_K ?
530 BPF_K : BPF_X, BPF_REG_0,
532 *insn = BPF_EXIT_INSN();
535 /* Store to stack. */
538 *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) ==
539 BPF_ST ? BPF_REG_A : BPF_REG_X,
540 -(BPF_MEMWORDS - fp->k) * 4);
543 /* Load from stack. */
544 case BPF_LD | BPF_MEM:
545 case BPF_LDX | BPF_MEM:
546 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
547 BPF_REG_A : BPF_REG_X, BPF_REG_FP,
548 -(BPF_MEMWORDS - fp->k) * 4);
552 case BPF_LD | BPF_IMM:
553 case BPF_LDX | BPF_IMM:
554 *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ?
555 BPF_REG_A : BPF_REG_X, fp->k);
559 case BPF_MISC | BPF_TAX:
560 *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
564 case BPF_MISC | BPF_TXA:
565 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X);
568 /* A = skb->len or X = skb->len */
569 case BPF_LD | BPF_W | BPF_LEN:
570 case BPF_LDX | BPF_W | BPF_LEN:
571 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
572 BPF_REG_A : BPF_REG_X, BPF_REG_CTX,
573 offsetof(struct sk_buff, len));
576 /* Access seccomp_data fields. */
577 case BPF_LDX | BPF_ABS | BPF_W:
578 /* A = *(u32 *) (ctx + K) */
579 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k);
582 /* Unknown instruction. */
589 memcpy(new_insn, tmp_insns,
590 sizeof(*insn) * (insn - tmp_insns));
591 new_insn += insn - tmp_insns;
595 /* Only calculating new length. */
596 *new_len = new_insn - new_prog;
601 if (new_flen != new_insn - new_prog) {
602 new_flen = new_insn - new_prog;
609 BUG_ON(*new_len != new_flen);
618 * As we dont want to clear mem[] array for each packet going through
619 * __bpf_prog_run(), we check that filter loaded by user never try to read
620 * a cell if not previously written, and we check all branches to be sure
621 * a malicious user doesn't try to abuse us.
623 static int check_load_and_stores(const struct sock_filter *filter, int flen)
625 u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */
628 BUILD_BUG_ON(BPF_MEMWORDS > 16);
630 masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL);
634 memset(masks, 0xff, flen * sizeof(*masks));
636 for (pc = 0; pc < flen; pc++) {
637 memvalid &= masks[pc];
639 switch (filter[pc].code) {
642 memvalid |= (1 << filter[pc].k);
644 case BPF_LD | BPF_MEM:
645 case BPF_LDX | BPF_MEM:
646 if (!(memvalid & (1 << filter[pc].k))) {
651 case BPF_JMP | BPF_JA:
652 /* A jump must set masks on target */
653 masks[pc + 1 + filter[pc].k] &= memvalid;
656 case BPF_JMP | BPF_JEQ | BPF_K:
657 case BPF_JMP | BPF_JEQ | BPF_X:
658 case BPF_JMP | BPF_JGE | BPF_K:
659 case BPF_JMP | BPF_JGE | BPF_X:
660 case BPF_JMP | BPF_JGT | BPF_K:
661 case BPF_JMP | BPF_JGT | BPF_X:
662 case BPF_JMP | BPF_JSET | BPF_K:
663 case BPF_JMP | BPF_JSET | BPF_X:
664 /* A jump must set masks on targets */
665 masks[pc + 1 + filter[pc].jt] &= memvalid;
666 masks[pc + 1 + filter[pc].jf] &= memvalid;
676 static bool chk_code_allowed(u16 code_to_probe)
678 static const bool codes[] = {
679 /* 32 bit ALU operations */
680 [BPF_ALU | BPF_ADD | BPF_K] = true,
681 [BPF_ALU | BPF_ADD | BPF_X] = true,
682 [BPF_ALU | BPF_SUB | BPF_K] = true,
683 [BPF_ALU | BPF_SUB | BPF_X] = true,
684 [BPF_ALU | BPF_MUL | BPF_K] = true,
685 [BPF_ALU | BPF_MUL | BPF_X] = true,
686 [BPF_ALU | BPF_DIV | BPF_K] = true,
687 [BPF_ALU | BPF_DIV | BPF_X] = true,
688 [BPF_ALU | BPF_MOD | BPF_K] = true,
689 [BPF_ALU | BPF_MOD | BPF_X] = true,
690 [BPF_ALU | BPF_AND | BPF_K] = true,
691 [BPF_ALU | BPF_AND | BPF_X] = true,
692 [BPF_ALU | BPF_OR | BPF_K] = true,
693 [BPF_ALU | BPF_OR | BPF_X] = true,
694 [BPF_ALU | BPF_XOR | BPF_K] = true,
695 [BPF_ALU | BPF_XOR | BPF_X] = true,
696 [BPF_ALU | BPF_LSH | BPF_K] = true,
697 [BPF_ALU | BPF_LSH | BPF_X] = true,
698 [BPF_ALU | BPF_RSH | BPF_K] = true,
699 [BPF_ALU | BPF_RSH | BPF_X] = true,
700 [BPF_ALU | BPF_NEG] = true,
701 /* Load instructions */
702 [BPF_LD | BPF_W | BPF_ABS] = true,
703 [BPF_LD | BPF_H | BPF_ABS] = true,
704 [BPF_LD | BPF_B | BPF_ABS] = true,
705 [BPF_LD | BPF_W | BPF_LEN] = true,
706 [BPF_LD | BPF_W | BPF_IND] = true,
707 [BPF_LD | BPF_H | BPF_IND] = true,
708 [BPF_LD | BPF_B | BPF_IND] = true,
709 [BPF_LD | BPF_IMM] = true,
710 [BPF_LD | BPF_MEM] = true,
711 [BPF_LDX | BPF_W | BPF_LEN] = true,
712 [BPF_LDX | BPF_B | BPF_MSH] = true,
713 [BPF_LDX | BPF_IMM] = true,
714 [BPF_LDX | BPF_MEM] = true,
715 /* Store instructions */
718 /* Misc instructions */
719 [BPF_MISC | BPF_TAX] = true,
720 [BPF_MISC | BPF_TXA] = true,
721 /* Return instructions */
722 [BPF_RET | BPF_K] = true,
723 [BPF_RET | BPF_A] = true,
724 /* Jump instructions */
725 [BPF_JMP | BPF_JA] = true,
726 [BPF_JMP | BPF_JEQ | BPF_K] = true,
727 [BPF_JMP | BPF_JEQ | BPF_X] = true,
728 [BPF_JMP | BPF_JGE | BPF_K] = true,
729 [BPF_JMP | BPF_JGE | BPF_X] = true,
730 [BPF_JMP | BPF_JGT | BPF_K] = true,
731 [BPF_JMP | BPF_JGT | BPF_X] = true,
732 [BPF_JMP | BPF_JSET | BPF_K] = true,
733 [BPF_JMP | BPF_JSET | BPF_X] = true,
736 if (code_to_probe >= ARRAY_SIZE(codes))
739 return codes[code_to_probe];
743 * bpf_check_classic - verify socket filter code
744 * @filter: filter to verify
745 * @flen: length of filter
747 * Check the user's filter code. If we let some ugly
748 * filter code slip through kaboom! The filter must contain
749 * no references or jumps that are out of range, no illegal
750 * instructions, and must end with a RET instruction.
752 * All jumps are forward as they are not signed.
754 * Returns 0 if the rule set is legal or -EINVAL if not.
756 static int bpf_check_classic(const struct sock_filter *filter,
762 if (flen == 0 || flen > BPF_MAXINSNS)
765 /* Check the filter code now */
766 for (pc = 0; pc < flen; pc++) {
767 const struct sock_filter *ftest = &filter[pc];
769 /* May we actually operate on this code? */
770 if (!chk_code_allowed(ftest->code))
773 /* Some instructions need special checks */
774 switch (ftest->code) {
775 case BPF_ALU | BPF_DIV | BPF_K:
776 case BPF_ALU | BPF_MOD | BPF_K:
777 /* Check for division by zero */
781 case BPF_LD | BPF_MEM:
782 case BPF_LDX | BPF_MEM:
785 /* Check for invalid memory addresses */
786 if (ftest->k >= BPF_MEMWORDS)
789 case BPF_JMP | BPF_JA:
790 /* Note, the large ftest->k might cause loops.
791 * Compare this with conditional jumps below,
792 * where offsets are limited. --ANK (981016)
794 if (ftest->k >= (unsigned int)(flen - pc - 1))
797 case BPF_JMP | BPF_JEQ | BPF_K:
798 case BPF_JMP | BPF_JEQ | BPF_X:
799 case BPF_JMP | BPF_JGE | BPF_K:
800 case BPF_JMP | BPF_JGE | BPF_X:
801 case BPF_JMP | BPF_JGT | BPF_K:
802 case BPF_JMP | BPF_JGT | BPF_X:
803 case BPF_JMP | BPF_JSET | BPF_K:
804 case BPF_JMP | BPF_JSET | BPF_X:
805 /* Both conditionals must be safe */
806 if (pc + ftest->jt + 1 >= flen ||
807 pc + ftest->jf + 1 >= flen)
810 case BPF_LD | BPF_W | BPF_ABS:
811 case BPF_LD | BPF_H | BPF_ABS:
812 case BPF_LD | BPF_B | BPF_ABS:
814 if (bpf_anc_helper(ftest) & BPF_ANC)
816 /* Ancillary operation unknown or unsupported */
817 if (anc_found == false && ftest->k >= SKF_AD_OFF)
822 /* Last instruction must be a RET code */
823 switch (filter[flen - 1].code) {
824 case BPF_RET | BPF_K:
825 case BPF_RET | BPF_A:
826 return check_load_and_stores(filter, flen);
832 static int bpf_prog_store_orig_filter(struct bpf_prog *fp,
833 const struct sock_fprog *fprog)
835 unsigned int fsize = bpf_classic_proglen(fprog);
836 struct sock_fprog_kern *fkprog;
838 fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL);
842 fkprog = fp->orig_prog;
843 fkprog->len = fprog->len;
845 fkprog->filter = kmemdup(fp->insns, fsize,
846 GFP_KERNEL | __GFP_NOWARN);
847 if (!fkprog->filter) {
848 kfree(fp->orig_prog);
855 static void bpf_release_orig_filter(struct bpf_prog *fp)
857 struct sock_fprog_kern *fprog = fp->orig_prog;
860 kfree(fprog->filter);
865 static void __bpf_prog_release(struct bpf_prog *prog)
867 if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) {
870 bpf_release_orig_filter(prog);
875 static void __sk_filter_release(struct sk_filter *fp)
877 __bpf_prog_release(fp->prog);
882 * sk_filter_release_rcu - Release a socket filter by rcu_head
883 * @rcu: rcu_head that contains the sk_filter to free
885 static void sk_filter_release_rcu(struct rcu_head *rcu)
887 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
889 __sk_filter_release(fp);
893 * sk_filter_release - release a socket filter
894 * @fp: filter to remove
896 * Remove a filter from a socket and release its resources.
898 static void sk_filter_release(struct sk_filter *fp)
900 if (atomic_dec_and_test(&fp->refcnt))
901 call_rcu(&fp->rcu, sk_filter_release_rcu);
904 void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp)
906 u32 filter_size = bpf_prog_size(fp->prog->len);
908 atomic_sub(filter_size, &sk->sk_omem_alloc);
909 sk_filter_release(fp);
912 /* try to charge the socket memory if there is space available
913 * return true on success
915 bool sk_filter_charge(struct sock *sk, struct sk_filter *fp)
917 u32 filter_size = bpf_prog_size(fp->prog->len);
919 /* same check as in sock_kmalloc() */
920 if (filter_size <= sysctl_optmem_max &&
921 atomic_read(&sk->sk_omem_alloc) + filter_size < sysctl_optmem_max) {
922 atomic_inc(&fp->refcnt);
923 atomic_add(filter_size, &sk->sk_omem_alloc);
929 static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp)
931 struct sock_filter *old_prog;
932 struct bpf_prog *old_fp;
933 int err, new_len, old_len = fp->len;
935 /* We are free to overwrite insns et al right here as it
936 * won't be used at this point in time anymore internally
937 * after the migration to the internal BPF instruction
940 BUILD_BUG_ON(sizeof(struct sock_filter) !=
941 sizeof(struct bpf_insn));
943 /* Conversion cannot happen on overlapping memory areas,
944 * so we need to keep the user BPF around until the 2nd
945 * pass. At this time, the user BPF is stored in fp->insns.
947 old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter),
948 GFP_KERNEL | __GFP_NOWARN);
954 /* 1st pass: calculate the new program length. */
955 err = bpf_convert_filter(old_prog, old_len, NULL, &new_len);
959 /* Expand fp for appending the new filter representation. */
961 fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0);
963 /* The old_fp is still around in case we couldn't
964 * allocate new memory, so uncharge on that one.
973 /* 2nd pass: remap sock_filter insns into bpf_insn insns. */
974 err = bpf_convert_filter(old_prog, old_len, fp->insnsi, &new_len);
976 /* 2nd bpf_convert_filter() can fail only if it fails
977 * to allocate memory, remapping must succeed. Note,
978 * that at this time old_fp has already been released
983 bpf_prog_select_runtime(fp);
991 __bpf_prog_release(fp);
995 static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp,
996 bpf_aux_classic_check_t trans)
1000 fp->bpf_func = NULL;
1003 err = bpf_check_classic(fp->insns, fp->len);
1005 __bpf_prog_release(fp);
1006 return ERR_PTR(err);
1009 /* There might be additional checks and transformations
1010 * needed on classic filters, f.e. in case of seccomp.
1013 err = trans(fp->insns, fp->len);
1015 __bpf_prog_release(fp);
1016 return ERR_PTR(err);
1020 /* Probe if we can JIT compile the filter and if so, do
1021 * the compilation of the filter.
1023 bpf_jit_compile(fp);
1025 /* JIT compiler couldn't process this filter, so do the
1026 * internal BPF translation for the optimized interpreter.
1029 fp = bpf_migrate_filter(fp);
1035 * bpf_prog_create - create an unattached filter
1036 * @pfp: the unattached filter that is created
1037 * @fprog: the filter program
1039 * Create a filter independent of any socket. We first run some
1040 * sanity checks on it to make sure it does not explode on us later.
1041 * If an error occurs or there is insufficient memory for the filter
1042 * a negative errno code is returned. On success the return is zero.
1044 int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog)
1046 unsigned int fsize = bpf_classic_proglen(fprog);
1047 struct bpf_prog *fp;
1049 /* Make sure new filter is there and in the right amounts. */
1050 if (fprog->filter == NULL)
1053 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1057 memcpy(fp->insns, fprog->filter, fsize);
1059 fp->len = fprog->len;
1060 /* Since unattached filters are not copied back to user
1061 * space through sk_get_filter(), we do not need to hold
1062 * a copy here, and can spare us the work.
1064 fp->orig_prog = NULL;
1066 /* bpf_prepare_filter() already takes care of freeing
1067 * memory in case something goes wrong.
1069 fp = bpf_prepare_filter(fp, NULL);
1076 EXPORT_SYMBOL_GPL(bpf_prog_create);
1079 * bpf_prog_create_from_user - create an unattached filter from user buffer
1080 * @pfp: the unattached filter that is created
1081 * @fprog: the filter program
1082 * @trans: post-classic verifier transformation handler
1084 * This function effectively does the same as bpf_prog_create(), only
1085 * that it builds up its insns buffer from user space provided buffer.
1086 * It also allows for passing a bpf_aux_classic_check_t handler.
1088 int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
1089 bpf_aux_classic_check_t trans)
1091 unsigned int fsize = bpf_classic_proglen(fprog);
1092 struct bpf_prog *fp;
1094 /* Make sure new filter is there and in the right amounts. */
1095 if (fprog->filter == NULL)
1098 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1102 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
1103 __bpf_prog_free(fp);
1107 fp->len = fprog->len;
1108 /* Since unattached filters are not copied back to user
1109 * space through sk_get_filter(), we do not need to hold
1110 * a copy here, and can spare us the work.
1112 fp->orig_prog = NULL;
1114 /* bpf_prepare_filter() already takes care of freeing
1115 * memory in case something goes wrong.
1117 fp = bpf_prepare_filter(fp, trans);
1125 void bpf_prog_destroy(struct bpf_prog *fp)
1127 __bpf_prog_release(fp);
1129 EXPORT_SYMBOL_GPL(bpf_prog_destroy);
1131 static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk)
1133 struct sk_filter *fp, *old_fp;
1135 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
1140 atomic_set(&fp->refcnt, 0);
1142 if (!sk_filter_charge(sk, fp)) {
1147 old_fp = rcu_dereference_protected(sk->sk_filter,
1148 sock_owned_by_user(sk));
1149 rcu_assign_pointer(sk->sk_filter, fp);
1152 sk_filter_uncharge(sk, old_fp);
1158 * sk_attach_filter - attach a socket filter
1159 * @fprog: the filter program
1160 * @sk: the socket to use
1162 * Attach the user's filter code. We first run some sanity checks on
1163 * it to make sure it does not explode on us later. If an error
1164 * occurs or there is insufficient memory for the filter a negative
1165 * errno code is returned. On success the return is zero.
1167 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1169 unsigned int fsize = bpf_classic_proglen(fprog);
1170 unsigned int bpf_fsize = bpf_prog_size(fprog->len);
1171 struct bpf_prog *prog;
1174 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1177 /* Make sure new filter is there and in the right amounts. */
1178 if (fprog->filter == NULL)
1181 prog = bpf_prog_alloc(bpf_fsize, 0);
1185 if (copy_from_user(prog->insns, fprog->filter, fsize)) {
1186 __bpf_prog_free(prog);
1190 prog->len = fprog->len;
1192 err = bpf_prog_store_orig_filter(prog, fprog);
1194 __bpf_prog_free(prog);
1198 /* bpf_prepare_filter() already takes care of freeing
1199 * memory in case something goes wrong.
1201 prog = bpf_prepare_filter(prog, NULL);
1203 return PTR_ERR(prog);
1205 err = __sk_attach_prog(prog, sk);
1207 __bpf_prog_release(prog);
1213 EXPORT_SYMBOL_GPL(sk_attach_filter);
1215 int sk_attach_bpf(u32 ufd, struct sock *sk)
1217 struct bpf_prog *prog;
1220 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1223 prog = bpf_prog_get(ufd);
1225 return PTR_ERR(prog);
1227 if (prog->type != BPF_PROG_TYPE_SOCKET_FILTER) {
1232 err = __sk_attach_prog(prog, sk);
1242 * bpf_skb_clone_not_writable - is the header of a clone not writable
1243 * @skb: buffer to check
1244 * @len: length up to which to write, can be negative
1246 * Returns true if modifying the header part of the cloned buffer
1247 * does require the data to be copied. I.e. this version works with
1248 * negative lengths needed for eBPF case!
1250 static bool bpf_skb_clone_unwritable(const struct sk_buff *skb, int len)
1252 return skb_header_cloned(skb) ||
1253 (int) skb_headroom(skb) + len > skb->hdr_len;
1256 #define BPF_RECOMPUTE_CSUM(flags) ((flags) & 1)
1258 static u64 bpf_skb_store_bytes(u64 r1, u64 r2, u64 r3, u64 r4, u64 flags)
1260 struct sk_buff *skb = (struct sk_buff *) (long) r1;
1261 int offset = (int) r2;
1262 void *from = (void *) (long) r3;
1263 unsigned int len = (unsigned int) r4;
1267 /* bpf verifier guarantees that:
1268 * 'from' pointer points to bpf program stack
1269 * 'len' bytes of it were initialized
1271 * 'skb' is a valid pointer to 'struct sk_buff'
1273 * so check for invalid 'offset' and too large 'len'
1275 if (unlikely((u32) offset > 0xffff || len > sizeof(buf)))
1278 offset -= skb->data - skb_mac_header(skb);
1279 if (unlikely(skb_cloned(skb) &&
1280 bpf_skb_clone_unwritable(skb, offset + len)))
1283 ptr = skb_header_pointer(skb, offset, len, buf);
1287 if (BPF_RECOMPUTE_CSUM(flags))
1288 skb_postpull_rcsum(skb, ptr, len);
1290 memcpy(ptr, from, len);
1293 /* skb_store_bits cannot return -EFAULT here */
1294 skb_store_bits(skb, offset, ptr, len);
1296 if (BPF_RECOMPUTE_CSUM(flags) && skb->ip_summed == CHECKSUM_COMPLETE)
1297 skb->csum = csum_add(skb->csum, csum_partial(ptr, len, 0));
1301 const struct bpf_func_proto bpf_skb_store_bytes_proto = {
1302 .func = bpf_skb_store_bytes,
1304 .ret_type = RET_INTEGER,
1305 .arg1_type = ARG_PTR_TO_CTX,
1306 .arg2_type = ARG_ANYTHING,
1307 .arg3_type = ARG_PTR_TO_STACK,
1308 .arg4_type = ARG_CONST_STACK_SIZE,
1309 .arg5_type = ARG_ANYTHING,
1312 #define BPF_HEADER_FIELD_SIZE(flags) ((flags) & 0x0f)
1313 #define BPF_IS_PSEUDO_HEADER(flags) ((flags) & 0x10)
1315 static u64 bpf_l3_csum_replace(u64 r1, u64 r2, u64 from, u64 to, u64 flags)
1317 struct sk_buff *skb = (struct sk_buff *) (long) r1;
1318 int offset = (int) r2;
1321 if (unlikely((u32) offset > 0xffff))
1324 offset -= skb->data - skb_mac_header(skb);
1325 if (unlikely(skb_cloned(skb) &&
1326 bpf_skb_clone_unwritable(skb, offset + sizeof(sum))))
1329 ptr = skb_header_pointer(skb, offset, sizeof(sum), &sum);
1333 switch (BPF_HEADER_FIELD_SIZE(flags)) {
1335 csum_replace2(ptr, from, to);
1338 csum_replace4(ptr, from, to);
1345 /* skb_store_bits guaranteed to not return -EFAULT here */
1346 skb_store_bits(skb, offset, ptr, sizeof(sum));
1351 const struct bpf_func_proto bpf_l3_csum_replace_proto = {
1352 .func = bpf_l3_csum_replace,
1354 .ret_type = RET_INTEGER,
1355 .arg1_type = ARG_PTR_TO_CTX,
1356 .arg2_type = ARG_ANYTHING,
1357 .arg3_type = ARG_ANYTHING,
1358 .arg4_type = ARG_ANYTHING,
1359 .arg5_type = ARG_ANYTHING,
1362 static u64 bpf_l4_csum_replace(u64 r1, u64 r2, u64 from, u64 to, u64 flags)
1364 struct sk_buff *skb = (struct sk_buff *) (long) r1;
1365 u32 is_pseudo = BPF_IS_PSEUDO_HEADER(flags);
1366 int offset = (int) r2;
1369 if (unlikely((u32) offset > 0xffff))
1372 offset -= skb->data - skb_mac_header(skb);
1373 if (unlikely(skb_cloned(skb) &&
1374 bpf_skb_clone_unwritable(skb, offset + sizeof(sum))))
1377 ptr = skb_header_pointer(skb, offset, sizeof(sum), &sum);
1381 switch (BPF_HEADER_FIELD_SIZE(flags)) {
1383 inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo);
1386 inet_proto_csum_replace4(ptr, skb, from, to, is_pseudo);
1393 /* skb_store_bits guaranteed to not return -EFAULT here */
1394 skb_store_bits(skb, offset, ptr, sizeof(sum));
1399 const struct bpf_func_proto bpf_l4_csum_replace_proto = {
1400 .func = bpf_l4_csum_replace,
1402 .ret_type = RET_INTEGER,
1403 .arg1_type = ARG_PTR_TO_CTX,
1404 .arg2_type = ARG_ANYTHING,
1405 .arg3_type = ARG_ANYTHING,
1406 .arg4_type = ARG_ANYTHING,
1407 .arg5_type = ARG_ANYTHING,
1410 #define BPF_IS_REDIRECT_INGRESS(flags) ((flags) & 1)
1412 static u64 bpf_clone_redirect(u64 r1, u64 ifindex, u64 flags, u64 r4, u64 r5)
1414 struct sk_buff *skb = (struct sk_buff *) (long) r1, *skb2;
1415 struct net_device *dev;
1417 dev = dev_get_by_index_rcu(dev_net(skb->dev), ifindex);
1421 if (unlikely(!(dev->flags & IFF_UP)))
1424 skb2 = skb_clone(skb, GFP_ATOMIC);
1425 if (unlikely(!skb2))
1428 skb_push(skb2, skb2->data - skb_mac_header(skb2));
1430 if (BPF_IS_REDIRECT_INGRESS(flags))
1431 return dev_forward_skb(dev, skb2);
1434 return dev_queue_xmit(skb2);
1437 const struct bpf_func_proto bpf_clone_redirect_proto = {
1438 .func = bpf_clone_redirect,
1440 .ret_type = RET_INTEGER,
1441 .arg1_type = ARG_PTR_TO_CTX,
1442 .arg2_type = ARG_ANYTHING,
1443 .arg3_type = ARG_ANYTHING,
1446 static const struct bpf_func_proto *
1447 sk_filter_func_proto(enum bpf_func_id func_id)
1450 case BPF_FUNC_map_lookup_elem:
1451 return &bpf_map_lookup_elem_proto;
1452 case BPF_FUNC_map_update_elem:
1453 return &bpf_map_update_elem_proto;
1454 case BPF_FUNC_map_delete_elem:
1455 return &bpf_map_delete_elem_proto;
1456 case BPF_FUNC_get_prandom_u32:
1457 return &bpf_get_prandom_u32_proto;
1458 case BPF_FUNC_get_smp_processor_id:
1459 return &bpf_get_smp_processor_id_proto;
1460 case BPF_FUNC_tail_call:
1461 return &bpf_tail_call_proto;
1462 case BPF_FUNC_ktime_get_ns:
1463 return &bpf_ktime_get_ns_proto;
1469 static const struct bpf_func_proto *
1470 tc_cls_act_func_proto(enum bpf_func_id func_id)
1473 case BPF_FUNC_skb_store_bytes:
1474 return &bpf_skb_store_bytes_proto;
1475 case BPF_FUNC_l3_csum_replace:
1476 return &bpf_l3_csum_replace_proto;
1477 case BPF_FUNC_l4_csum_replace:
1478 return &bpf_l4_csum_replace_proto;
1479 case BPF_FUNC_clone_redirect:
1480 return &bpf_clone_redirect_proto;
1482 return sk_filter_func_proto(func_id);
1486 static bool sk_filter_is_valid_access(int off, int size,
1487 enum bpf_access_type type)
1489 /* only read is allowed */
1490 if (type != BPF_READ)
1494 if (off < 0 || off >= sizeof(struct __sk_buff))
1497 /* disallow misaligned access */
1498 if (off % size != 0)
1501 /* all __sk_buff fields are __u32 */
1508 static u32 sk_filter_convert_ctx_access(int dst_reg, int src_reg, int ctx_off,
1509 struct bpf_insn *insn_buf)
1511 struct bpf_insn *insn = insn_buf;
1514 case offsetof(struct __sk_buff, len):
1515 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, len) != 4);
1517 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
1518 offsetof(struct sk_buff, len));
1521 case offsetof(struct __sk_buff, protocol):
1522 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
1524 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
1525 offsetof(struct sk_buff, protocol));
1528 case offsetof(struct __sk_buff, vlan_proto):
1529 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2);
1531 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
1532 offsetof(struct sk_buff, vlan_proto));
1535 case offsetof(struct __sk_buff, priority):
1536 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, priority) != 4);
1538 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
1539 offsetof(struct sk_buff, priority));
1542 case offsetof(struct __sk_buff, ingress_ifindex):
1543 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, skb_iif) != 4);
1545 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
1546 offsetof(struct sk_buff, skb_iif));
1549 case offsetof(struct __sk_buff, ifindex):
1550 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
1552 *insn++ = BPF_LDX_MEM(bytes_to_bpf_size(FIELD_SIZEOF(struct sk_buff, dev)),
1554 offsetof(struct sk_buff, dev));
1555 *insn++ = BPF_JMP_IMM(BPF_JEQ, dst_reg, 0, 1);
1556 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, dst_reg,
1557 offsetof(struct net_device, ifindex));
1560 case offsetof(struct __sk_buff, mark):
1561 return convert_skb_access(SKF_AD_MARK, dst_reg, src_reg, insn);
1563 case offsetof(struct __sk_buff, pkt_type):
1564 return convert_skb_access(SKF_AD_PKTTYPE, dst_reg, src_reg, insn);
1566 case offsetof(struct __sk_buff, queue_mapping):
1567 return convert_skb_access(SKF_AD_QUEUE, dst_reg, src_reg, insn);
1569 case offsetof(struct __sk_buff, vlan_present):
1570 return convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
1571 dst_reg, src_reg, insn);
1573 case offsetof(struct __sk_buff, vlan_tci):
1574 return convert_skb_access(SKF_AD_VLAN_TAG,
1575 dst_reg, src_reg, insn);
1578 return insn - insn_buf;
1581 static const struct bpf_verifier_ops sk_filter_ops = {
1582 .get_func_proto = sk_filter_func_proto,
1583 .is_valid_access = sk_filter_is_valid_access,
1584 .convert_ctx_access = sk_filter_convert_ctx_access,
1587 static const struct bpf_verifier_ops tc_cls_act_ops = {
1588 .get_func_proto = tc_cls_act_func_proto,
1589 .is_valid_access = sk_filter_is_valid_access,
1590 .convert_ctx_access = sk_filter_convert_ctx_access,
1593 static struct bpf_prog_type_list sk_filter_type __read_mostly = {
1594 .ops = &sk_filter_ops,
1595 .type = BPF_PROG_TYPE_SOCKET_FILTER,
1598 static struct bpf_prog_type_list sched_cls_type __read_mostly = {
1599 .ops = &tc_cls_act_ops,
1600 .type = BPF_PROG_TYPE_SCHED_CLS,
1603 static struct bpf_prog_type_list sched_act_type __read_mostly = {
1604 .ops = &tc_cls_act_ops,
1605 .type = BPF_PROG_TYPE_SCHED_ACT,
1608 static int __init register_sk_filter_ops(void)
1610 bpf_register_prog_type(&sk_filter_type);
1611 bpf_register_prog_type(&sched_cls_type);
1612 bpf_register_prog_type(&sched_act_type);
1616 late_initcall(register_sk_filter_ops);
1618 int sk_detach_filter(struct sock *sk)
1621 struct sk_filter *filter;
1623 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1626 filter = rcu_dereference_protected(sk->sk_filter,
1627 sock_owned_by_user(sk));
1629 RCU_INIT_POINTER(sk->sk_filter, NULL);
1630 sk_filter_uncharge(sk, filter);
1636 EXPORT_SYMBOL_GPL(sk_detach_filter);
1638 int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf,
1641 struct sock_fprog_kern *fprog;
1642 struct sk_filter *filter;
1646 filter = rcu_dereference_protected(sk->sk_filter,
1647 sock_owned_by_user(sk));
1651 /* We're copying the filter that has been originally attached,
1652 * so no conversion/decode needed anymore.
1654 fprog = filter->prog->orig_prog;
1658 /* User space only enquires number of filter blocks. */
1662 if (len < fprog->len)
1666 if (copy_to_user(ubuf, fprog->filter, bpf_classic_proglen(fprog)))
1669 /* Instead of bytes, the API requests to return the number