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 <linux/errno.h>
40 #include <linux/timer.h>
41 #include <asm/uaccess.h>
42 #include <asm/unaligned.h>
43 #include <linux/filter.h>
44 #include <linux/ratelimit.h>
45 #include <linux/seccomp.h>
46 #include <linux/if_vlan.h>
47 #include <linux/bpf.h>
50 * sk_filter - run a packet through a socket filter
51 * @sk: sock associated with &sk_buff
52 * @skb: buffer to filter
54 * Run the filter code and then cut skb->data to correct size returned by
55 * SK_RUN_FILTER. If pkt_len is 0 we toss packet. If skb->len is smaller
56 * than pkt_len we keep whole skb->data. This is the socket level
57 * wrapper to SK_RUN_FILTER. It returns 0 if the packet should
58 * be accepted or -EPERM if the packet should be tossed.
61 int sk_filter(struct sock *sk, struct sk_buff *skb)
64 struct sk_filter *filter;
67 * If the skb was allocated from pfmemalloc reserves, only
68 * allow SOCK_MEMALLOC sockets to use it as this socket is
71 if (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC))
74 err = security_sock_rcv_skb(sk, skb);
79 filter = rcu_dereference(sk->sk_filter);
81 unsigned int pkt_len = SK_RUN_FILTER(filter, skb);
83 err = pkt_len ? pskb_trim(skb, pkt_len) : -EPERM;
89 EXPORT_SYMBOL(sk_filter);
91 static u64 __skb_get_pay_offset(u64 ctx, u64 a, u64 x, u64 r4, u64 r5)
93 return skb_get_poff((struct sk_buff *)(unsigned long) ctx);
96 static u64 __skb_get_nlattr(u64 ctx, u64 a, u64 x, u64 r4, u64 r5)
98 struct sk_buff *skb = (struct sk_buff *)(unsigned long) ctx;
101 if (skb_is_nonlinear(skb))
104 if (skb->len < sizeof(struct nlattr))
107 if (a > skb->len - sizeof(struct nlattr))
110 nla = nla_find((struct nlattr *) &skb->data[a], skb->len - a, x);
112 return (void *) nla - (void *) skb->data;
117 static u64 __skb_get_nlattr_nest(u64 ctx, u64 a, u64 x, u64 r4, u64 r5)
119 struct sk_buff *skb = (struct sk_buff *)(unsigned long) ctx;
122 if (skb_is_nonlinear(skb))
125 if (skb->len < sizeof(struct nlattr))
128 if (a > skb->len - sizeof(struct nlattr))
131 nla = (struct nlattr *) &skb->data[a];
132 if (nla->nla_len > skb->len - a)
135 nla = nla_find_nested(nla, x);
137 return (void *) nla - (void *) skb->data;
142 static u64 __get_raw_cpu_id(u64 ctx, u64 a, u64 x, u64 r4, u64 r5)
144 return raw_smp_processor_id();
147 /* note that this only generates 32-bit random numbers */
148 static u64 __get_random_u32(u64 ctx, u64 a, u64 x, u64 r4, u64 r5)
150 return prandom_u32();
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(__get_random_u32);
316 case SKF_AD_OFF + SKF_AD_ALU_XOR_X:
318 *insn = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_X);
322 /* This is just a dummy call to avoid letting the compiler
323 * evict __bpf_call_base() as an optimization. Placed here
324 * where no-one bothers.
326 BUG_ON(__bpf_call_base(0, 0, 0, 0, 0) != 0);
335 * bpf_convert_filter - convert filter program
336 * @prog: the user passed filter program
337 * @len: the length of the user passed filter program
338 * @new_prog: buffer where converted program will be stored
339 * @new_len: pointer to store length of converted program
341 * Remap 'sock_filter' style BPF instruction set to 'sock_filter_ext' style.
342 * Conversion workflow:
344 * 1) First pass for calculating the new program length:
345 * bpf_convert_filter(old_prog, old_len, NULL, &new_len)
347 * 2) 2nd pass to remap in two passes: 1st pass finds new
348 * jump offsets, 2nd pass remapping:
349 * new_prog = kmalloc(sizeof(struct bpf_insn) * new_len);
350 * bpf_convert_filter(old_prog, old_len, new_prog, &new_len);
352 * User BPF's register A is mapped to our BPF register 6, user BPF
353 * register X is mapped to BPF register 7; frame pointer is always
354 * register 10; Context 'void *ctx' is stored in register 1, that is,
355 * for socket filters: ctx == 'struct sk_buff *', for seccomp:
356 * ctx == 'struct seccomp_data *'.
358 static int bpf_convert_filter(struct sock_filter *prog, int len,
359 struct bpf_insn *new_prog, int *new_len)
361 int new_flen = 0, pass = 0, target, i;
362 struct bpf_insn *new_insn;
363 struct sock_filter *fp;
367 BUILD_BUG_ON(BPF_MEMWORDS * sizeof(u32) > MAX_BPF_STACK);
368 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
370 if (len <= 0 || len > BPF_MAXINSNS)
374 addrs = kcalloc(len, sizeof(*addrs),
375 GFP_KERNEL | __GFP_NOWARN);
385 *new_insn = BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1);
388 for (i = 0; i < len; fp++, i++) {
389 struct bpf_insn tmp_insns[6] = { };
390 struct bpf_insn *insn = tmp_insns;
393 addrs[i] = new_insn - new_prog;
396 /* All arithmetic insns and skb loads map as-is. */
397 case BPF_ALU | BPF_ADD | BPF_X:
398 case BPF_ALU | BPF_ADD | BPF_K:
399 case BPF_ALU | BPF_SUB | BPF_X:
400 case BPF_ALU | BPF_SUB | BPF_K:
401 case BPF_ALU | BPF_AND | BPF_X:
402 case BPF_ALU | BPF_AND | BPF_K:
403 case BPF_ALU | BPF_OR | BPF_X:
404 case BPF_ALU | BPF_OR | BPF_K:
405 case BPF_ALU | BPF_LSH | BPF_X:
406 case BPF_ALU | BPF_LSH | BPF_K:
407 case BPF_ALU | BPF_RSH | BPF_X:
408 case BPF_ALU | BPF_RSH | BPF_K:
409 case BPF_ALU | BPF_XOR | BPF_X:
410 case BPF_ALU | BPF_XOR | BPF_K:
411 case BPF_ALU | BPF_MUL | BPF_X:
412 case BPF_ALU | BPF_MUL | BPF_K:
413 case BPF_ALU | BPF_DIV | BPF_X:
414 case BPF_ALU | BPF_DIV | BPF_K:
415 case BPF_ALU | BPF_MOD | BPF_X:
416 case BPF_ALU | BPF_MOD | BPF_K:
417 case BPF_ALU | BPF_NEG:
418 case BPF_LD | BPF_ABS | BPF_W:
419 case BPF_LD | BPF_ABS | BPF_H:
420 case BPF_LD | BPF_ABS | BPF_B:
421 case BPF_LD | BPF_IND | BPF_W:
422 case BPF_LD | BPF_IND | BPF_H:
423 case BPF_LD | BPF_IND | BPF_B:
424 /* Check for overloaded BPF extension and
425 * directly convert it if found, otherwise
426 * just move on with mapping.
428 if (BPF_CLASS(fp->code) == BPF_LD &&
429 BPF_MODE(fp->code) == BPF_ABS &&
430 convert_bpf_extensions(fp, &insn))
433 *insn = BPF_RAW_INSN(fp->code, BPF_REG_A, BPF_REG_X, 0, fp->k);
436 /* Jump transformation cannot use BPF block macros
437 * everywhere as offset calculation and target updates
438 * require a bit more work than the rest, i.e. jump
439 * opcodes map as-is, but offsets need adjustment.
442 #define BPF_EMIT_JMP \
444 if (target >= len || target < 0) \
446 insn->off = addrs ? addrs[target] - addrs[i] - 1 : 0; \
447 /* Adjust pc relative offset for 2nd or 3rd insn. */ \
448 insn->off -= insn - tmp_insns; \
451 case BPF_JMP | BPF_JA:
452 target = i + fp->k + 1;
453 insn->code = fp->code;
457 case BPF_JMP | BPF_JEQ | BPF_K:
458 case BPF_JMP | BPF_JEQ | BPF_X:
459 case BPF_JMP | BPF_JSET | BPF_K:
460 case BPF_JMP | BPF_JSET | BPF_X:
461 case BPF_JMP | BPF_JGT | BPF_K:
462 case BPF_JMP | BPF_JGT | BPF_X:
463 case BPF_JMP | BPF_JGE | BPF_K:
464 case BPF_JMP | BPF_JGE | BPF_X:
465 if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) {
466 /* BPF immediates are signed, zero extend
467 * immediate into tmp register and use it
470 *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k);
472 insn->dst_reg = BPF_REG_A;
473 insn->src_reg = BPF_REG_TMP;
476 insn->dst_reg = BPF_REG_A;
477 insn->src_reg = BPF_REG_X;
479 bpf_src = BPF_SRC(fp->code);
482 /* Common case where 'jump_false' is next insn. */
484 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
485 target = i + fp->jt + 1;
490 /* Convert JEQ into JNE when 'jump_true' is next insn. */
491 if (fp->jt == 0 && BPF_OP(fp->code) == BPF_JEQ) {
492 insn->code = BPF_JMP | BPF_JNE | bpf_src;
493 target = i + fp->jf + 1;
498 /* Other jumps are mapped into two insns: Jxx and JA. */
499 target = i + fp->jt + 1;
500 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
504 insn->code = BPF_JMP | BPF_JA;
505 target = i + fp->jf + 1;
509 /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */
510 case BPF_LDX | BPF_MSH | BPF_B:
512 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_A);
513 /* A = BPF_R0 = *(u8 *) (skb->data + K) */
514 *insn++ = BPF_LD_ABS(BPF_B, fp->k);
516 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf);
518 *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2);
520 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
522 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP);
525 /* RET_K, RET_A are remaped into 2 insns. */
526 case BPF_RET | BPF_A:
527 case BPF_RET | BPF_K:
528 *insn++ = BPF_MOV32_RAW(BPF_RVAL(fp->code) == BPF_K ?
529 BPF_K : BPF_X, BPF_REG_0,
531 *insn = BPF_EXIT_INSN();
534 /* Store to stack. */
537 *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) ==
538 BPF_ST ? BPF_REG_A : BPF_REG_X,
539 -(BPF_MEMWORDS - fp->k) * 4);
542 /* Load from stack. */
543 case BPF_LD | BPF_MEM:
544 case BPF_LDX | BPF_MEM:
545 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
546 BPF_REG_A : BPF_REG_X, BPF_REG_FP,
547 -(BPF_MEMWORDS - fp->k) * 4);
551 case BPF_LD | BPF_IMM:
552 case BPF_LDX | BPF_IMM:
553 *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ?
554 BPF_REG_A : BPF_REG_X, fp->k);
558 case BPF_MISC | BPF_TAX:
559 *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
563 case BPF_MISC | BPF_TXA:
564 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X);
567 /* A = skb->len or X = skb->len */
568 case BPF_LD | BPF_W | BPF_LEN:
569 case BPF_LDX | BPF_W | BPF_LEN:
570 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
571 BPF_REG_A : BPF_REG_X, BPF_REG_CTX,
572 offsetof(struct sk_buff, len));
575 /* Access seccomp_data fields. */
576 case BPF_LDX | BPF_ABS | BPF_W:
577 /* A = *(u32 *) (ctx + K) */
578 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k);
581 /* Unknown instruction. */
588 memcpy(new_insn, tmp_insns,
589 sizeof(*insn) * (insn - tmp_insns));
590 new_insn += insn - tmp_insns;
594 /* Only calculating new length. */
595 *new_len = new_insn - new_prog;
600 if (new_flen != new_insn - new_prog) {
601 new_flen = new_insn - new_prog;
608 BUG_ON(*new_len != new_flen);
617 * As we dont want to clear mem[] array for each packet going through
618 * __bpf_prog_run(), we check that filter loaded by user never try to read
619 * a cell if not previously written, and we check all branches to be sure
620 * a malicious user doesn't try to abuse us.
622 static int check_load_and_stores(const struct sock_filter *filter, int flen)
624 u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */
627 BUILD_BUG_ON(BPF_MEMWORDS > 16);
629 masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL);
633 memset(masks, 0xff, flen * sizeof(*masks));
635 for (pc = 0; pc < flen; pc++) {
636 memvalid &= masks[pc];
638 switch (filter[pc].code) {
641 memvalid |= (1 << filter[pc].k);
643 case BPF_LD | BPF_MEM:
644 case BPF_LDX | BPF_MEM:
645 if (!(memvalid & (1 << filter[pc].k))) {
650 case BPF_JMP | BPF_JA:
651 /* A jump must set masks on target */
652 masks[pc + 1 + filter[pc].k] &= memvalid;
655 case BPF_JMP | BPF_JEQ | BPF_K:
656 case BPF_JMP | BPF_JEQ | BPF_X:
657 case BPF_JMP | BPF_JGE | BPF_K:
658 case BPF_JMP | BPF_JGE | BPF_X:
659 case BPF_JMP | BPF_JGT | BPF_K:
660 case BPF_JMP | BPF_JGT | BPF_X:
661 case BPF_JMP | BPF_JSET | BPF_K:
662 case BPF_JMP | BPF_JSET | BPF_X:
663 /* A jump must set masks on targets */
664 masks[pc + 1 + filter[pc].jt] &= memvalid;
665 masks[pc + 1 + filter[pc].jf] &= memvalid;
675 static bool chk_code_allowed(u16 code_to_probe)
677 static const bool codes[] = {
678 /* 32 bit ALU operations */
679 [BPF_ALU | BPF_ADD | BPF_K] = true,
680 [BPF_ALU | BPF_ADD | BPF_X] = true,
681 [BPF_ALU | BPF_SUB | BPF_K] = true,
682 [BPF_ALU | BPF_SUB | BPF_X] = true,
683 [BPF_ALU | BPF_MUL | BPF_K] = true,
684 [BPF_ALU | BPF_MUL | BPF_X] = true,
685 [BPF_ALU | BPF_DIV | BPF_K] = true,
686 [BPF_ALU | BPF_DIV | BPF_X] = true,
687 [BPF_ALU | BPF_MOD | BPF_K] = true,
688 [BPF_ALU | BPF_MOD | BPF_X] = true,
689 [BPF_ALU | BPF_AND | BPF_K] = true,
690 [BPF_ALU | BPF_AND | BPF_X] = true,
691 [BPF_ALU | BPF_OR | BPF_K] = true,
692 [BPF_ALU | BPF_OR | BPF_X] = true,
693 [BPF_ALU | BPF_XOR | BPF_K] = true,
694 [BPF_ALU | BPF_XOR | BPF_X] = true,
695 [BPF_ALU | BPF_LSH | BPF_K] = true,
696 [BPF_ALU | BPF_LSH | BPF_X] = true,
697 [BPF_ALU | BPF_RSH | BPF_K] = true,
698 [BPF_ALU | BPF_RSH | BPF_X] = true,
699 [BPF_ALU | BPF_NEG] = true,
700 /* Load instructions */
701 [BPF_LD | BPF_W | BPF_ABS] = true,
702 [BPF_LD | BPF_H | BPF_ABS] = true,
703 [BPF_LD | BPF_B | BPF_ABS] = true,
704 [BPF_LD | BPF_W | BPF_LEN] = true,
705 [BPF_LD | BPF_W | BPF_IND] = true,
706 [BPF_LD | BPF_H | BPF_IND] = true,
707 [BPF_LD | BPF_B | BPF_IND] = true,
708 [BPF_LD | BPF_IMM] = true,
709 [BPF_LD | BPF_MEM] = true,
710 [BPF_LDX | BPF_W | BPF_LEN] = true,
711 [BPF_LDX | BPF_B | BPF_MSH] = true,
712 [BPF_LDX | BPF_IMM] = true,
713 [BPF_LDX | BPF_MEM] = true,
714 /* Store instructions */
717 /* Misc instructions */
718 [BPF_MISC | BPF_TAX] = true,
719 [BPF_MISC | BPF_TXA] = true,
720 /* Return instructions */
721 [BPF_RET | BPF_K] = true,
722 [BPF_RET | BPF_A] = true,
723 /* Jump instructions */
724 [BPF_JMP | BPF_JA] = true,
725 [BPF_JMP | BPF_JEQ | BPF_K] = true,
726 [BPF_JMP | BPF_JEQ | BPF_X] = true,
727 [BPF_JMP | BPF_JGE | BPF_K] = true,
728 [BPF_JMP | BPF_JGE | BPF_X] = true,
729 [BPF_JMP | BPF_JGT | BPF_K] = true,
730 [BPF_JMP | BPF_JGT | BPF_X] = true,
731 [BPF_JMP | BPF_JSET | BPF_K] = true,
732 [BPF_JMP | BPF_JSET | BPF_X] = true,
735 if (code_to_probe >= ARRAY_SIZE(codes))
738 return codes[code_to_probe];
742 * bpf_check_classic - verify socket filter code
743 * @filter: filter to verify
744 * @flen: length of filter
746 * Check the user's filter code. If we let some ugly
747 * filter code slip through kaboom! The filter must contain
748 * no references or jumps that are out of range, no illegal
749 * instructions, and must end with a RET instruction.
751 * All jumps are forward as they are not signed.
753 * Returns 0 if the rule set is legal or -EINVAL if not.
755 static int bpf_check_classic(const struct sock_filter *filter,
761 if (flen == 0 || flen > BPF_MAXINSNS)
764 /* Check the filter code now */
765 for (pc = 0; pc < flen; pc++) {
766 const struct sock_filter *ftest = &filter[pc];
768 /* May we actually operate on this code? */
769 if (!chk_code_allowed(ftest->code))
772 /* Some instructions need special checks */
773 switch (ftest->code) {
774 case BPF_ALU | BPF_DIV | BPF_K:
775 case BPF_ALU | BPF_MOD | BPF_K:
776 /* Check for division by zero */
780 case BPF_LD | BPF_MEM:
781 case BPF_LDX | BPF_MEM:
784 /* Check for invalid memory addresses */
785 if (ftest->k >= BPF_MEMWORDS)
788 case BPF_JMP | BPF_JA:
789 /* Note, the large ftest->k might cause loops.
790 * Compare this with conditional jumps below,
791 * where offsets are limited. --ANK (981016)
793 if (ftest->k >= (unsigned int)(flen - pc - 1))
796 case BPF_JMP | BPF_JEQ | BPF_K:
797 case BPF_JMP | BPF_JEQ | BPF_X:
798 case BPF_JMP | BPF_JGE | BPF_K:
799 case BPF_JMP | BPF_JGE | BPF_X:
800 case BPF_JMP | BPF_JGT | BPF_K:
801 case BPF_JMP | BPF_JGT | BPF_X:
802 case BPF_JMP | BPF_JSET | BPF_K:
803 case BPF_JMP | BPF_JSET | BPF_X:
804 /* Both conditionals must be safe */
805 if (pc + ftest->jt + 1 >= flen ||
806 pc + ftest->jf + 1 >= flen)
809 case BPF_LD | BPF_W | BPF_ABS:
810 case BPF_LD | BPF_H | BPF_ABS:
811 case BPF_LD | BPF_B | BPF_ABS:
813 if (bpf_anc_helper(ftest) & BPF_ANC)
815 /* Ancillary operation unknown or unsupported */
816 if (anc_found == false && ftest->k >= SKF_AD_OFF)
821 /* Last instruction must be a RET code */
822 switch (filter[flen - 1].code) {
823 case BPF_RET | BPF_K:
824 case BPF_RET | BPF_A:
825 return check_load_and_stores(filter, flen);
831 static int bpf_prog_store_orig_filter(struct bpf_prog *fp,
832 const struct sock_fprog *fprog)
834 unsigned int fsize = bpf_classic_proglen(fprog);
835 struct sock_fprog_kern *fkprog;
837 fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL);
841 fkprog = fp->orig_prog;
842 fkprog->len = fprog->len;
844 fkprog->filter = kmemdup(fp->insns, fsize,
845 GFP_KERNEL | __GFP_NOWARN);
846 if (!fkprog->filter) {
847 kfree(fp->orig_prog);
854 static void bpf_release_orig_filter(struct bpf_prog *fp)
856 struct sock_fprog_kern *fprog = fp->orig_prog;
859 kfree(fprog->filter);
864 static void __bpf_prog_release(struct bpf_prog *prog)
866 if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) {
869 bpf_release_orig_filter(prog);
874 static void __sk_filter_release(struct sk_filter *fp)
876 __bpf_prog_release(fp->prog);
881 * sk_filter_release_rcu - Release a socket filter by rcu_head
882 * @rcu: rcu_head that contains the sk_filter to free
884 static void sk_filter_release_rcu(struct rcu_head *rcu)
886 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
888 __sk_filter_release(fp);
892 * sk_filter_release - release a socket filter
893 * @fp: filter to remove
895 * Remove a filter from a socket and release its resources.
897 static void sk_filter_release(struct sk_filter *fp)
899 if (atomic_dec_and_test(&fp->refcnt))
900 call_rcu(&fp->rcu, sk_filter_release_rcu);
903 void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp)
905 u32 filter_size = bpf_prog_size(fp->prog->len);
907 atomic_sub(filter_size, &sk->sk_omem_alloc);
908 sk_filter_release(fp);
911 /* try to charge the socket memory if there is space available
912 * return true on success
914 bool sk_filter_charge(struct sock *sk, struct sk_filter *fp)
916 u32 filter_size = bpf_prog_size(fp->prog->len);
918 /* same check as in sock_kmalloc() */
919 if (filter_size <= sysctl_optmem_max &&
920 atomic_read(&sk->sk_omem_alloc) + filter_size < sysctl_optmem_max) {
921 atomic_inc(&fp->refcnt);
922 atomic_add(filter_size, &sk->sk_omem_alloc);
928 static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp)
930 struct sock_filter *old_prog;
931 struct bpf_prog *old_fp;
932 int err, new_len, old_len = fp->len;
934 /* We are free to overwrite insns et al right here as it
935 * won't be used at this point in time anymore internally
936 * after the migration to the internal BPF instruction
939 BUILD_BUG_ON(sizeof(struct sock_filter) !=
940 sizeof(struct bpf_insn));
942 /* Conversion cannot happen on overlapping memory areas,
943 * so we need to keep the user BPF around until the 2nd
944 * pass. At this time, the user BPF is stored in fp->insns.
946 old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter),
947 GFP_KERNEL | __GFP_NOWARN);
953 /* 1st pass: calculate the new program length. */
954 err = bpf_convert_filter(old_prog, old_len, NULL, &new_len);
958 /* Expand fp for appending the new filter representation. */
960 fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0);
962 /* The old_fp is still around in case we couldn't
963 * allocate new memory, so uncharge on that one.
972 /* 2nd pass: remap sock_filter insns into bpf_insn insns. */
973 err = bpf_convert_filter(old_prog, old_len, fp->insnsi, &new_len);
975 /* 2nd bpf_convert_filter() can fail only if it fails
976 * to allocate memory, remapping must succeed. Note,
977 * that at this time old_fp has already been released
982 bpf_prog_select_runtime(fp);
990 __bpf_prog_release(fp);
994 static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp,
995 bpf_aux_classic_check_t trans)
1002 err = bpf_check_classic(fp->insns, fp->len);
1004 __bpf_prog_release(fp);
1005 return ERR_PTR(err);
1008 /* There might be additional checks and transformations
1009 * needed on classic filters, f.e. in case of seccomp.
1012 err = trans(fp->insns, fp->len);
1014 __bpf_prog_release(fp);
1015 return ERR_PTR(err);
1019 /* Probe if we can JIT compile the filter and if so, do
1020 * the compilation of the filter.
1022 bpf_jit_compile(fp);
1024 /* JIT compiler couldn't process this filter, so do the
1025 * internal BPF translation for the optimized interpreter.
1028 fp = bpf_migrate_filter(fp);
1034 * bpf_prog_create - create an unattached filter
1035 * @pfp: the unattached filter that is created
1036 * @fprog: the filter program
1038 * Create a filter independent of any socket. We first run some
1039 * sanity checks on it to make sure it does not explode on us later.
1040 * If an error occurs or there is insufficient memory for the filter
1041 * a negative errno code is returned. On success the return is zero.
1043 int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog)
1045 unsigned int fsize = bpf_classic_proglen(fprog);
1046 struct bpf_prog *fp;
1048 /* Make sure new filter is there and in the right amounts. */
1049 if (fprog->filter == NULL)
1052 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1056 memcpy(fp->insns, fprog->filter, fsize);
1058 fp->len = fprog->len;
1059 /* Since unattached filters are not copied back to user
1060 * space through sk_get_filter(), we do not need to hold
1061 * a copy here, and can spare us the work.
1063 fp->orig_prog = NULL;
1065 /* bpf_prepare_filter() already takes care of freeing
1066 * memory in case something goes wrong.
1068 fp = bpf_prepare_filter(fp, NULL);
1075 EXPORT_SYMBOL_GPL(bpf_prog_create);
1078 * bpf_prog_create_from_user - create an unattached filter from user buffer
1079 * @pfp: the unattached filter that is created
1080 * @fprog: the filter program
1081 * @trans: post-classic verifier transformation handler
1083 * This function effectively does the same as bpf_prog_create(), only
1084 * that it builds up its insns buffer from user space provided buffer.
1085 * It also allows for passing a bpf_aux_classic_check_t handler.
1087 int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
1088 bpf_aux_classic_check_t trans)
1090 unsigned int fsize = bpf_classic_proglen(fprog);
1091 struct bpf_prog *fp;
1093 /* Make sure new filter is there and in the right amounts. */
1094 if (fprog->filter == NULL)
1097 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1101 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
1102 __bpf_prog_free(fp);
1106 fp->len = fprog->len;
1107 /* Since unattached filters are not copied back to user
1108 * space through sk_get_filter(), we do not need to hold
1109 * a copy here, and can spare us the work.
1111 fp->orig_prog = NULL;
1113 /* bpf_prepare_filter() already takes care of freeing
1114 * memory in case something goes wrong.
1116 fp = bpf_prepare_filter(fp, trans);
1124 void bpf_prog_destroy(struct bpf_prog *fp)
1126 __bpf_prog_release(fp);
1128 EXPORT_SYMBOL_GPL(bpf_prog_destroy);
1130 static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk)
1132 struct sk_filter *fp, *old_fp;
1134 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
1139 atomic_set(&fp->refcnt, 0);
1141 if (!sk_filter_charge(sk, fp)) {
1146 old_fp = rcu_dereference_protected(sk->sk_filter,
1147 sock_owned_by_user(sk));
1148 rcu_assign_pointer(sk->sk_filter, fp);
1151 sk_filter_uncharge(sk, old_fp);
1157 * sk_attach_filter - attach a socket filter
1158 * @fprog: the filter program
1159 * @sk: the socket to use
1161 * Attach the user's filter code. We first run some sanity checks on
1162 * it to make sure it does not explode on us later. If an error
1163 * occurs or there is insufficient memory for the filter a negative
1164 * errno code is returned. On success the return is zero.
1166 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1168 unsigned int fsize = bpf_classic_proglen(fprog);
1169 unsigned int bpf_fsize = bpf_prog_size(fprog->len);
1170 struct bpf_prog *prog;
1173 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1176 /* Make sure new filter is there and in the right amounts. */
1177 if (fprog->filter == NULL)
1180 prog = bpf_prog_alloc(bpf_fsize, 0);
1184 if (copy_from_user(prog->insns, fprog->filter, fsize)) {
1185 __bpf_prog_free(prog);
1189 prog->len = fprog->len;
1191 err = bpf_prog_store_orig_filter(prog, fprog);
1193 __bpf_prog_free(prog);
1197 /* bpf_prepare_filter() already takes care of freeing
1198 * memory in case something goes wrong.
1200 prog = bpf_prepare_filter(prog, NULL);
1202 return PTR_ERR(prog);
1204 err = __sk_attach_prog(prog, sk);
1206 __bpf_prog_release(prog);
1212 EXPORT_SYMBOL_GPL(sk_attach_filter);
1214 int sk_attach_bpf(u32 ufd, struct sock *sk)
1216 struct bpf_prog *prog;
1219 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1222 prog = bpf_prog_get(ufd);
1224 return PTR_ERR(prog);
1226 if (prog->type != BPF_PROG_TYPE_SOCKET_FILTER) {
1231 err = __sk_attach_prog(prog, sk);
1241 * bpf_skb_clone_not_writable - is the header of a clone not writable
1242 * @skb: buffer to check
1243 * @len: length up to which to write, can be negative
1245 * Returns true if modifying the header part of the cloned buffer
1246 * does require the data to be copied. I.e. this version works with
1247 * negative lengths needed for eBPF case!
1249 static bool bpf_skb_clone_unwritable(const struct sk_buff *skb, int len)
1251 return skb_header_cloned(skb) ||
1252 (int) skb_headroom(skb) + len > skb->hdr_len;
1255 #define BPF_RECOMPUTE_CSUM(flags) ((flags) & 1)
1257 static u64 bpf_skb_store_bytes(u64 r1, u64 r2, u64 r3, u64 r4, u64 flags)
1259 struct sk_buff *skb = (struct sk_buff *) (long) r1;
1260 int offset = (int) r2;
1261 void *from = (void *) (long) r3;
1262 unsigned int len = (unsigned int) r4;
1266 /* bpf verifier guarantees that:
1267 * 'from' pointer points to bpf program stack
1268 * 'len' bytes of it were initialized
1270 * 'skb' is a valid pointer to 'struct sk_buff'
1272 * so check for invalid 'offset' and too large 'len'
1274 if (unlikely((u32) offset > 0xffff || len > sizeof(buf)))
1277 offset -= skb->data - skb_mac_header(skb);
1278 if (unlikely(skb_cloned(skb) &&
1279 bpf_skb_clone_unwritable(skb, offset + len)))
1282 ptr = skb_header_pointer(skb, offset, len, buf);
1286 if (BPF_RECOMPUTE_CSUM(flags))
1287 skb_postpull_rcsum(skb, ptr, len);
1289 memcpy(ptr, from, len);
1292 /* skb_store_bits cannot return -EFAULT here */
1293 skb_store_bits(skb, offset, ptr, len);
1295 if (BPF_RECOMPUTE_CSUM(flags) && skb->ip_summed == CHECKSUM_COMPLETE)
1296 skb->csum = csum_add(skb->csum, csum_partial(ptr, len, 0));
1300 const struct bpf_func_proto bpf_skb_store_bytes_proto = {
1301 .func = bpf_skb_store_bytes,
1303 .ret_type = RET_INTEGER,
1304 .arg1_type = ARG_PTR_TO_CTX,
1305 .arg2_type = ARG_ANYTHING,
1306 .arg3_type = ARG_PTR_TO_STACK,
1307 .arg4_type = ARG_CONST_STACK_SIZE,
1308 .arg5_type = ARG_ANYTHING,
1311 #define BPF_HEADER_FIELD_SIZE(flags) ((flags) & 0x0f)
1312 #define BPF_IS_PSEUDO_HEADER(flags) ((flags) & 0x10)
1314 static u64 bpf_l3_csum_replace(u64 r1, u64 r2, u64 from, u64 to, u64 flags)
1316 struct sk_buff *skb = (struct sk_buff *) (long) r1;
1317 int offset = (int) r2;
1320 if (unlikely((u32) offset > 0xffff))
1323 offset -= skb->data - skb_mac_header(skb);
1324 if (unlikely(skb_cloned(skb) &&
1325 bpf_skb_clone_unwritable(skb, offset + sizeof(sum))))
1328 ptr = skb_header_pointer(skb, offset, sizeof(sum), &sum);
1332 switch (BPF_HEADER_FIELD_SIZE(flags)) {
1334 csum_replace2(ptr, from, to);
1337 csum_replace4(ptr, from, to);
1344 /* skb_store_bits guaranteed to not return -EFAULT here */
1345 skb_store_bits(skb, offset, ptr, sizeof(sum));
1350 const struct bpf_func_proto bpf_l3_csum_replace_proto = {
1351 .func = bpf_l3_csum_replace,
1353 .ret_type = RET_INTEGER,
1354 .arg1_type = ARG_PTR_TO_CTX,
1355 .arg2_type = ARG_ANYTHING,
1356 .arg3_type = ARG_ANYTHING,
1357 .arg4_type = ARG_ANYTHING,
1358 .arg5_type = ARG_ANYTHING,
1361 static u64 bpf_l4_csum_replace(u64 r1, u64 r2, u64 from, u64 to, u64 flags)
1363 struct sk_buff *skb = (struct sk_buff *) (long) r1;
1364 u32 is_pseudo = BPF_IS_PSEUDO_HEADER(flags);
1365 int offset = (int) r2;
1368 if (unlikely((u32) offset > 0xffff))
1371 offset -= skb->data - skb_mac_header(skb);
1372 if (unlikely(skb_cloned(skb) &&
1373 bpf_skb_clone_unwritable(skb, offset + sizeof(sum))))
1376 ptr = skb_header_pointer(skb, offset, sizeof(sum), &sum);
1380 switch (BPF_HEADER_FIELD_SIZE(flags)) {
1382 inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo);
1385 inet_proto_csum_replace4(ptr, skb, from, to, is_pseudo);
1392 /* skb_store_bits guaranteed to not return -EFAULT here */
1393 skb_store_bits(skb, offset, ptr, sizeof(sum));
1398 const struct bpf_func_proto bpf_l4_csum_replace_proto = {
1399 .func = bpf_l4_csum_replace,
1401 .ret_type = RET_INTEGER,
1402 .arg1_type = ARG_PTR_TO_CTX,
1403 .arg2_type = ARG_ANYTHING,
1404 .arg3_type = ARG_ANYTHING,
1405 .arg4_type = ARG_ANYTHING,
1406 .arg5_type = ARG_ANYTHING,
1409 static const struct bpf_func_proto *
1410 sk_filter_func_proto(enum bpf_func_id func_id)
1413 case BPF_FUNC_map_lookup_elem:
1414 return &bpf_map_lookup_elem_proto;
1415 case BPF_FUNC_map_update_elem:
1416 return &bpf_map_update_elem_proto;
1417 case BPF_FUNC_map_delete_elem:
1418 return &bpf_map_delete_elem_proto;
1419 case BPF_FUNC_get_prandom_u32:
1420 return &bpf_get_prandom_u32_proto;
1421 case BPF_FUNC_get_smp_processor_id:
1422 return &bpf_get_smp_processor_id_proto;
1428 static const struct bpf_func_proto *
1429 tc_cls_act_func_proto(enum bpf_func_id func_id)
1432 case BPF_FUNC_skb_store_bytes:
1433 return &bpf_skb_store_bytes_proto;
1434 case BPF_FUNC_l3_csum_replace:
1435 return &bpf_l3_csum_replace_proto;
1436 case BPF_FUNC_l4_csum_replace:
1437 return &bpf_l4_csum_replace_proto;
1439 return sk_filter_func_proto(func_id);
1443 static bool sk_filter_is_valid_access(int off, int size,
1444 enum bpf_access_type type)
1446 /* only read is allowed */
1447 if (type != BPF_READ)
1451 if (off < 0 || off >= sizeof(struct __sk_buff))
1454 /* disallow misaligned access */
1455 if (off % size != 0)
1458 /* all __sk_buff fields are __u32 */
1465 static u32 sk_filter_convert_ctx_access(int dst_reg, int src_reg, int ctx_off,
1466 struct bpf_insn *insn_buf)
1468 struct bpf_insn *insn = insn_buf;
1471 case offsetof(struct __sk_buff, len):
1472 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, len) != 4);
1474 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
1475 offsetof(struct sk_buff, len));
1478 case offsetof(struct __sk_buff, protocol):
1479 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
1481 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
1482 offsetof(struct sk_buff, protocol));
1485 case offsetof(struct __sk_buff, vlan_proto):
1486 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2);
1488 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
1489 offsetof(struct sk_buff, vlan_proto));
1492 case offsetof(struct __sk_buff, priority):
1493 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, priority) != 4);
1495 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
1496 offsetof(struct sk_buff, priority));
1499 case offsetof(struct __sk_buff, mark):
1500 return convert_skb_access(SKF_AD_MARK, dst_reg, src_reg, insn);
1502 case offsetof(struct __sk_buff, pkt_type):
1503 return convert_skb_access(SKF_AD_PKTTYPE, dst_reg, src_reg, insn);
1505 case offsetof(struct __sk_buff, queue_mapping):
1506 return convert_skb_access(SKF_AD_QUEUE, dst_reg, src_reg, insn);
1508 case offsetof(struct __sk_buff, vlan_present):
1509 return convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
1510 dst_reg, src_reg, insn);
1512 case offsetof(struct __sk_buff, vlan_tci):
1513 return convert_skb_access(SKF_AD_VLAN_TAG,
1514 dst_reg, src_reg, insn);
1517 return insn - insn_buf;
1520 static const struct bpf_verifier_ops sk_filter_ops = {
1521 .get_func_proto = sk_filter_func_proto,
1522 .is_valid_access = sk_filter_is_valid_access,
1523 .convert_ctx_access = sk_filter_convert_ctx_access,
1526 static const struct bpf_verifier_ops tc_cls_act_ops = {
1527 .get_func_proto = tc_cls_act_func_proto,
1528 .is_valid_access = sk_filter_is_valid_access,
1529 .convert_ctx_access = sk_filter_convert_ctx_access,
1532 static struct bpf_prog_type_list sk_filter_type __read_mostly = {
1533 .ops = &sk_filter_ops,
1534 .type = BPF_PROG_TYPE_SOCKET_FILTER,
1537 static struct bpf_prog_type_list sched_cls_type __read_mostly = {
1538 .ops = &tc_cls_act_ops,
1539 .type = BPF_PROG_TYPE_SCHED_CLS,
1542 static struct bpf_prog_type_list sched_act_type __read_mostly = {
1543 .ops = &tc_cls_act_ops,
1544 .type = BPF_PROG_TYPE_SCHED_ACT,
1547 static int __init register_sk_filter_ops(void)
1549 bpf_register_prog_type(&sk_filter_type);
1550 bpf_register_prog_type(&sched_cls_type);
1551 bpf_register_prog_type(&sched_act_type);
1555 late_initcall(register_sk_filter_ops);
1557 int sk_detach_filter(struct sock *sk)
1560 struct sk_filter *filter;
1562 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1565 filter = rcu_dereference_protected(sk->sk_filter,
1566 sock_owned_by_user(sk));
1568 RCU_INIT_POINTER(sk->sk_filter, NULL);
1569 sk_filter_uncharge(sk, filter);
1575 EXPORT_SYMBOL_GPL(sk_detach_filter);
1577 int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf,
1580 struct sock_fprog_kern *fprog;
1581 struct sk_filter *filter;
1585 filter = rcu_dereference_protected(sk->sk_filter,
1586 sock_owned_by_user(sk));
1590 /* We're copying the filter that has been originally attached,
1591 * so no conversion/decode needed anymore.
1593 fprog = filter->prog->orig_prog;
1597 /* User space only enquires number of filter blocks. */
1601 if (len < fprog->len)
1605 if (copy_to_user(ubuf, fprog->filter, bpf_classic_proglen(fprog)))
1608 /* Instead of bytes, the API requests to return the number