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), GFP_KERNEL);
384 *new_insn = BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1);
387 for (i = 0; i < len; fp++, i++) {
388 struct bpf_insn tmp_insns[6] = { };
389 struct bpf_insn *insn = tmp_insns;
392 addrs[i] = new_insn - new_prog;
395 /* All arithmetic insns and skb loads map as-is. */
396 case BPF_ALU | BPF_ADD | BPF_X:
397 case BPF_ALU | BPF_ADD | BPF_K:
398 case BPF_ALU | BPF_SUB | BPF_X:
399 case BPF_ALU | BPF_SUB | BPF_K:
400 case BPF_ALU | BPF_AND | BPF_X:
401 case BPF_ALU | BPF_AND | BPF_K:
402 case BPF_ALU | BPF_OR | BPF_X:
403 case BPF_ALU | BPF_OR | BPF_K:
404 case BPF_ALU | BPF_LSH | BPF_X:
405 case BPF_ALU | BPF_LSH | BPF_K:
406 case BPF_ALU | BPF_RSH | BPF_X:
407 case BPF_ALU | BPF_RSH | BPF_K:
408 case BPF_ALU | BPF_XOR | BPF_X:
409 case BPF_ALU | BPF_XOR | BPF_K:
410 case BPF_ALU | BPF_MUL | BPF_X:
411 case BPF_ALU | BPF_MUL | BPF_K:
412 case BPF_ALU | BPF_DIV | BPF_X:
413 case BPF_ALU | BPF_DIV | BPF_K:
414 case BPF_ALU | BPF_MOD | BPF_X:
415 case BPF_ALU | BPF_MOD | BPF_K:
416 case BPF_ALU | BPF_NEG:
417 case BPF_LD | BPF_ABS | BPF_W:
418 case BPF_LD | BPF_ABS | BPF_H:
419 case BPF_LD | BPF_ABS | BPF_B:
420 case BPF_LD | BPF_IND | BPF_W:
421 case BPF_LD | BPF_IND | BPF_H:
422 case BPF_LD | BPF_IND | BPF_B:
423 /* Check for overloaded BPF extension and
424 * directly convert it if found, otherwise
425 * just move on with mapping.
427 if (BPF_CLASS(fp->code) == BPF_LD &&
428 BPF_MODE(fp->code) == BPF_ABS &&
429 convert_bpf_extensions(fp, &insn))
432 *insn = BPF_RAW_INSN(fp->code, BPF_REG_A, BPF_REG_X, 0, fp->k);
435 /* Jump transformation cannot use BPF block macros
436 * everywhere as offset calculation and target updates
437 * require a bit more work than the rest, i.e. jump
438 * opcodes map as-is, but offsets need adjustment.
441 #define BPF_EMIT_JMP \
443 if (target >= len || target < 0) \
445 insn->off = addrs ? addrs[target] - addrs[i] - 1 : 0; \
446 /* Adjust pc relative offset for 2nd or 3rd insn. */ \
447 insn->off -= insn - tmp_insns; \
450 case BPF_JMP | BPF_JA:
451 target = i + fp->k + 1;
452 insn->code = fp->code;
456 case BPF_JMP | BPF_JEQ | BPF_K:
457 case BPF_JMP | BPF_JEQ | BPF_X:
458 case BPF_JMP | BPF_JSET | BPF_K:
459 case BPF_JMP | BPF_JSET | BPF_X:
460 case BPF_JMP | BPF_JGT | BPF_K:
461 case BPF_JMP | BPF_JGT | BPF_X:
462 case BPF_JMP | BPF_JGE | BPF_K:
463 case BPF_JMP | BPF_JGE | BPF_X:
464 if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) {
465 /* BPF immediates are signed, zero extend
466 * immediate into tmp register and use it
469 *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k);
471 insn->dst_reg = BPF_REG_A;
472 insn->src_reg = BPF_REG_TMP;
475 insn->dst_reg = BPF_REG_A;
476 insn->src_reg = BPF_REG_X;
478 bpf_src = BPF_SRC(fp->code);
481 /* Common case where 'jump_false' is next insn. */
483 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
484 target = i + fp->jt + 1;
489 /* Convert JEQ into JNE when 'jump_true' is next insn. */
490 if (fp->jt == 0 && BPF_OP(fp->code) == BPF_JEQ) {
491 insn->code = BPF_JMP | BPF_JNE | bpf_src;
492 target = i + fp->jf + 1;
497 /* Other jumps are mapped into two insns: Jxx and JA. */
498 target = i + fp->jt + 1;
499 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
503 insn->code = BPF_JMP | BPF_JA;
504 target = i + fp->jf + 1;
508 /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */
509 case BPF_LDX | BPF_MSH | BPF_B:
511 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_A);
512 /* A = BPF_R0 = *(u8 *) (skb->data + K) */
513 *insn++ = BPF_LD_ABS(BPF_B, fp->k);
515 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf);
517 *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2);
519 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
521 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP);
524 /* RET_K, RET_A are remaped into 2 insns. */
525 case BPF_RET | BPF_A:
526 case BPF_RET | BPF_K:
527 *insn++ = BPF_MOV32_RAW(BPF_RVAL(fp->code) == BPF_K ?
528 BPF_K : BPF_X, BPF_REG_0,
530 *insn = BPF_EXIT_INSN();
533 /* Store to stack. */
536 *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) ==
537 BPF_ST ? BPF_REG_A : BPF_REG_X,
538 -(BPF_MEMWORDS - fp->k) * 4);
541 /* Load from stack. */
542 case BPF_LD | BPF_MEM:
543 case BPF_LDX | BPF_MEM:
544 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
545 BPF_REG_A : BPF_REG_X, BPF_REG_FP,
546 -(BPF_MEMWORDS - fp->k) * 4);
550 case BPF_LD | BPF_IMM:
551 case BPF_LDX | BPF_IMM:
552 *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ?
553 BPF_REG_A : BPF_REG_X, fp->k);
557 case BPF_MISC | BPF_TAX:
558 *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
562 case BPF_MISC | BPF_TXA:
563 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X);
566 /* A = skb->len or X = skb->len */
567 case BPF_LD | BPF_W | BPF_LEN:
568 case BPF_LDX | BPF_W | BPF_LEN:
569 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
570 BPF_REG_A : BPF_REG_X, BPF_REG_CTX,
571 offsetof(struct sk_buff, len));
574 /* Access seccomp_data fields. */
575 case BPF_LDX | BPF_ABS | BPF_W:
576 /* A = *(u32 *) (ctx + K) */
577 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k);
580 /* Unknown instruction. */
587 memcpy(new_insn, tmp_insns,
588 sizeof(*insn) * (insn - tmp_insns));
589 new_insn += insn - tmp_insns;
593 /* Only calculating new length. */
594 *new_len = new_insn - new_prog;
599 if (new_flen != new_insn - new_prog) {
600 new_flen = new_insn - new_prog;
607 BUG_ON(*new_len != new_flen);
616 * As we dont want to clear mem[] array for each packet going through
617 * __bpf_prog_run(), we check that filter loaded by user never try to read
618 * a cell if not previously written, and we check all branches to be sure
619 * a malicious user doesn't try to abuse us.
621 static int check_load_and_stores(const struct sock_filter *filter, int flen)
623 u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */
626 BUILD_BUG_ON(BPF_MEMWORDS > 16);
628 masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL);
632 memset(masks, 0xff, flen * sizeof(*masks));
634 for (pc = 0; pc < flen; pc++) {
635 memvalid &= masks[pc];
637 switch (filter[pc].code) {
640 memvalid |= (1 << filter[pc].k);
642 case BPF_LD | BPF_MEM:
643 case BPF_LDX | BPF_MEM:
644 if (!(memvalid & (1 << filter[pc].k))) {
649 case BPF_JMP | BPF_JA:
650 /* A jump must set masks on target */
651 masks[pc + 1 + filter[pc].k] &= memvalid;
654 case BPF_JMP | BPF_JEQ | BPF_K:
655 case BPF_JMP | BPF_JEQ | BPF_X:
656 case BPF_JMP | BPF_JGE | BPF_K:
657 case BPF_JMP | BPF_JGE | BPF_X:
658 case BPF_JMP | BPF_JGT | BPF_K:
659 case BPF_JMP | BPF_JGT | BPF_X:
660 case BPF_JMP | BPF_JSET | BPF_K:
661 case BPF_JMP | BPF_JSET | BPF_X:
662 /* A jump must set masks on targets */
663 masks[pc + 1 + filter[pc].jt] &= memvalid;
664 masks[pc + 1 + filter[pc].jf] &= memvalid;
674 static bool chk_code_allowed(u16 code_to_probe)
676 static const bool codes[] = {
677 /* 32 bit ALU operations */
678 [BPF_ALU | BPF_ADD | BPF_K] = true,
679 [BPF_ALU | BPF_ADD | BPF_X] = true,
680 [BPF_ALU | BPF_SUB | BPF_K] = true,
681 [BPF_ALU | BPF_SUB | BPF_X] = true,
682 [BPF_ALU | BPF_MUL | BPF_K] = true,
683 [BPF_ALU | BPF_MUL | BPF_X] = true,
684 [BPF_ALU | BPF_DIV | BPF_K] = true,
685 [BPF_ALU | BPF_DIV | BPF_X] = true,
686 [BPF_ALU | BPF_MOD | BPF_K] = true,
687 [BPF_ALU | BPF_MOD | BPF_X] = true,
688 [BPF_ALU | BPF_AND | BPF_K] = true,
689 [BPF_ALU | BPF_AND | BPF_X] = true,
690 [BPF_ALU | BPF_OR | BPF_K] = true,
691 [BPF_ALU | BPF_OR | BPF_X] = true,
692 [BPF_ALU | BPF_XOR | BPF_K] = true,
693 [BPF_ALU | BPF_XOR | BPF_X] = true,
694 [BPF_ALU | BPF_LSH | BPF_K] = true,
695 [BPF_ALU | BPF_LSH | BPF_X] = true,
696 [BPF_ALU | BPF_RSH | BPF_K] = true,
697 [BPF_ALU | BPF_RSH | BPF_X] = true,
698 [BPF_ALU | BPF_NEG] = true,
699 /* Load instructions */
700 [BPF_LD | BPF_W | BPF_ABS] = true,
701 [BPF_LD | BPF_H | BPF_ABS] = true,
702 [BPF_LD | BPF_B | BPF_ABS] = true,
703 [BPF_LD | BPF_W | BPF_LEN] = true,
704 [BPF_LD | BPF_W | BPF_IND] = true,
705 [BPF_LD | BPF_H | BPF_IND] = true,
706 [BPF_LD | BPF_B | BPF_IND] = true,
707 [BPF_LD | BPF_IMM] = true,
708 [BPF_LD | BPF_MEM] = true,
709 [BPF_LDX | BPF_W | BPF_LEN] = true,
710 [BPF_LDX | BPF_B | BPF_MSH] = true,
711 [BPF_LDX | BPF_IMM] = true,
712 [BPF_LDX | BPF_MEM] = true,
713 /* Store instructions */
716 /* Misc instructions */
717 [BPF_MISC | BPF_TAX] = true,
718 [BPF_MISC | BPF_TXA] = true,
719 /* Return instructions */
720 [BPF_RET | BPF_K] = true,
721 [BPF_RET | BPF_A] = true,
722 /* Jump instructions */
723 [BPF_JMP | BPF_JA] = true,
724 [BPF_JMP | BPF_JEQ | BPF_K] = true,
725 [BPF_JMP | BPF_JEQ | BPF_X] = true,
726 [BPF_JMP | BPF_JGE | BPF_K] = true,
727 [BPF_JMP | BPF_JGE | BPF_X] = true,
728 [BPF_JMP | BPF_JGT | BPF_K] = true,
729 [BPF_JMP | BPF_JGT | BPF_X] = true,
730 [BPF_JMP | BPF_JSET | BPF_K] = true,
731 [BPF_JMP | BPF_JSET | BPF_X] = true,
734 if (code_to_probe >= ARRAY_SIZE(codes))
737 return codes[code_to_probe];
741 * bpf_check_classic - verify socket filter code
742 * @filter: filter to verify
743 * @flen: length of filter
745 * Check the user's filter code. If we let some ugly
746 * filter code slip through kaboom! The filter must contain
747 * no references or jumps that are out of range, no illegal
748 * instructions, and must end with a RET instruction.
750 * All jumps are forward as they are not signed.
752 * Returns 0 if the rule set is legal or -EINVAL if not.
754 static int bpf_check_classic(const struct sock_filter *filter,
760 if (flen == 0 || flen > BPF_MAXINSNS)
763 /* Check the filter code now */
764 for (pc = 0; pc < flen; pc++) {
765 const struct sock_filter *ftest = &filter[pc];
767 /* May we actually operate on this code? */
768 if (!chk_code_allowed(ftest->code))
771 /* Some instructions need special checks */
772 switch (ftest->code) {
773 case BPF_ALU | BPF_DIV | BPF_K:
774 case BPF_ALU | BPF_MOD | BPF_K:
775 /* Check for division by zero */
779 case BPF_LD | BPF_MEM:
780 case BPF_LDX | BPF_MEM:
783 /* Check for invalid memory addresses */
784 if (ftest->k >= BPF_MEMWORDS)
787 case BPF_JMP | BPF_JA:
788 /* Note, the large ftest->k might cause loops.
789 * Compare this with conditional jumps below,
790 * where offsets are limited. --ANK (981016)
792 if (ftest->k >= (unsigned int)(flen - pc - 1))
795 case BPF_JMP | BPF_JEQ | BPF_K:
796 case BPF_JMP | BPF_JEQ | BPF_X:
797 case BPF_JMP | BPF_JGE | BPF_K:
798 case BPF_JMP | BPF_JGE | BPF_X:
799 case BPF_JMP | BPF_JGT | BPF_K:
800 case BPF_JMP | BPF_JGT | BPF_X:
801 case BPF_JMP | BPF_JSET | BPF_K:
802 case BPF_JMP | BPF_JSET | BPF_X:
803 /* Both conditionals must be safe */
804 if (pc + ftest->jt + 1 >= flen ||
805 pc + ftest->jf + 1 >= flen)
808 case BPF_LD | BPF_W | BPF_ABS:
809 case BPF_LD | BPF_H | BPF_ABS:
810 case BPF_LD | BPF_B | BPF_ABS:
812 if (bpf_anc_helper(ftest) & BPF_ANC)
814 /* Ancillary operation unknown or unsupported */
815 if (anc_found == false && ftest->k >= SKF_AD_OFF)
820 /* Last instruction must be a RET code */
821 switch (filter[flen - 1].code) {
822 case BPF_RET | BPF_K:
823 case BPF_RET | BPF_A:
824 return check_load_and_stores(filter, flen);
830 static int bpf_prog_store_orig_filter(struct bpf_prog *fp,
831 const struct sock_fprog *fprog)
833 unsigned int fsize = bpf_classic_proglen(fprog);
834 struct sock_fprog_kern *fkprog;
836 fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL);
840 fkprog = fp->orig_prog;
841 fkprog->len = fprog->len;
842 fkprog->filter = kmemdup(fp->insns, fsize, GFP_KERNEL);
843 if (!fkprog->filter) {
844 kfree(fp->orig_prog);
851 static void bpf_release_orig_filter(struct bpf_prog *fp)
853 struct sock_fprog_kern *fprog = fp->orig_prog;
856 kfree(fprog->filter);
861 static void __bpf_prog_release(struct bpf_prog *prog)
863 if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) {
866 bpf_release_orig_filter(prog);
871 static void __sk_filter_release(struct sk_filter *fp)
873 __bpf_prog_release(fp->prog);
878 * sk_filter_release_rcu - Release a socket filter by rcu_head
879 * @rcu: rcu_head that contains the sk_filter to free
881 static void sk_filter_release_rcu(struct rcu_head *rcu)
883 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
885 __sk_filter_release(fp);
889 * sk_filter_release - release a socket filter
890 * @fp: filter to remove
892 * Remove a filter from a socket and release its resources.
894 static void sk_filter_release(struct sk_filter *fp)
896 if (atomic_dec_and_test(&fp->refcnt))
897 call_rcu(&fp->rcu, sk_filter_release_rcu);
900 void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp)
902 u32 filter_size = bpf_prog_size(fp->prog->len);
904 atomic_sub(filter_size, &sk->sk_omem_alloc);
905 sk_filter_release(fp);
908 /* try to charge the socket memory if there is space available
909 * return true on success
911 bool sk_filter_charge(struct sock *sk, struct sk_filter *fp)
913 u32 filter_size = bpf_prog_size(fp->prog->len);
915 /* same check as in sock_kmalloc() */
916 if (filter_size <= sysctl_optmem_max &&
917 atomic_read(&sk->sk_omem_alloc) + filter_size < sysctl_optmem_max) {
918 atomic_inc(&fp->refcnt);
919 atomic_add(filter_size, &sk->sk_omem_alloc);
925 static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp)
927 struct sock_filter *old_prog;
928 struct bpf_prog *old_fp;
929 int err, new_len, old_len = fp->len;
931 /* We are free to overwrite insns et al right here as it
932 * won't be used at this point in time anymore internally
933 * after the migration to the internal BPF instruction
936 BUILD_BUG_ON(sizeof(struct sock_filter) !=
937 sizeof(struct bpf_insn));
939 /* Conversion cannot happen on overlapping memory areas,
940 * so we need to keep the user BPF around until the 2nd
941 * pass. At this time, the user BPF is stored in fp->insns.
943 old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter),
950 /* 1st pass: calculate the new program length. */
951 err = bpf_convert_filter(old_prog, old_len, NULL, &new_len);
955 /* Expand fp for appending the new filter representation. */
957 fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0);
959 /* The old_fp is still around in case we couldn't
960 * allocate new memory, so uncharge on that one.
969 /* 2nd pass: remap sock_filter insns into bpf_insn insns. */
970 err = bpf_convert_filter(old_prog, old_len, fp->insnsi, &new_len);
972 /* 2nd bpf_convert_filter() can fail only if it fails
973 * to allocate memory, remapping must succeed. Note,
974 * that at this time old_fp has already been released
979 bpf_prog_select_runtime(fp);
987 __bpf_prog_release(fp);
991 struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp,
992 bpf_aux_classic_check_t trans)
999 err = bpf_check_classic(fp->insns, fp->len);
1001 __bpf_prog_release(fp);
1002 return ERR_PTR(err);
1005 /* There might be additional checks and transformations
1006 * needed on classic filters, f.e. in case of seccomp.
1009 err = trans(fp->insns, fp->len);
1011 __bpf_prog_release(fp);
1012 return ERR_PTR(err);
1016 /* Probe if we can JIT compile the filter and if so, do
1017 * the compilation of the filter.
1019 bpf_jit_compile(fp);
1021 /* JIT compiler couldn't process this filter, so do the
1022 * internal BPF translation for the optimized interpreter.
1025 fp = bpf_migrate_filter(fp);
1031 * bpf_prog_create - create an unattached filter
1032 * @pfp: the unattached filter that is created
1033 * @fprog: the filter program
1035 * Create a filter independent of any socket. We first run some
1036 * sanity checks on it to make sure it does not explode on us later.
1037 * If an error occurs or there is insufficient memory for the filter
1038 * a negative errno code is returned. On success the return is zero.
1040 int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog)
1042 unsigned int fsize = bpf_classic_proglen(fprog);
1043 struct bpf_prog *fp;
1045 /* Make sure new filter is there and in the right amounts. */
1046 if (fprog->filter == NULL)
1049 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1053 memcpy(fp->insns, fprog->filter, fsize);
1055 fp->len = fprog->len;
1056 /* Since unattached filters are not copied back to user
1057 * space through sk_get_filter(), we do not need to hold
1058 * a copy here, and can spare us the work.
1060 fp->orig_prog = NULL;
1062 /* bpf_prepare_filter() already takes care of freeing
1063 * memory in case something goes wrong.
1065 fp = bpf_prepare_filter(fp, NULL);
1072 EXPORT_SYMBOL_GPL(bpf_prog_create);
1074 void bpf_prog_destroy(struct bpf_prog *fp)
1076 __bpf_prog_release(fp);
1078 EXPORT_SYMBOL_GPL(bpf_prog_destroy);
1080 static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk)
1082 struct sk_filter *fp, *old_fp;
1084 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
1089 atomic_set(&fp->refcnt, 0);
1091 if (!sk_filter_charge(sk, fp)) {
1096 old_fp = rcu_dereference_protected(sk->sk_filter,
1097 sock_owned_by_user(sk));
1098 rcu_assign_pointer(sk->sk_filter, fp);
1101 sk_filter_uncharge(sk, old_fp);
1107 * sk_attach_filter - attach a socket filter
1108 * @fprog: the filter program
1109 * @sk: the socket to use
1111 * Attach the user's filter code. We first run some sanity checks on
1112 * it to make sure it does not explode on us later. If an error
1113 * occurs or there is insufficient memory for the filter a negative
1114 * errno code is returned. On success the return is zero.
1116 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1118 unsigned int fsize = bpf_classic_proglen(fprog);
1119 unsigned int bpf_fsize = bpf_prog_size(fprog->len);
1120 struct bpf_prog *prog;
1123 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1126 /* Make sure new filter is there and in the right amounts. */
1127 if (fprog->filter == NULL)
1130 prog = bpf_prog_alloc(bpf_fsize, 0);
1134 if (copy_from_user(prog->insns, fprog->filter, fsize)) {
1135 __bpf_prog_free(prog);
1139 prog->len = fprog->len;
1141 err = bpf_prog_store_orig_filter(prog, fprog);
1143 __bpf_prog_free(prog);
1147 /* bpf_prepare_filter() already takes care of freeing
1148 * memory in case something goes wrong.
1150 prog = bpf_prepare_filter(prog, NULL);
1152 return PTR_ERR(prog);
1154 err = __sk_attach_prog(prog, sk);
1156 __bpf_prog_release(prog);
1162 EXPORT_SYMBOL_GPL(sk_attach_filter);
1164 int sk_attach_bpf(u32 ufd, struct sock *sk)
1166 struct bpf_prog *prog;
1169 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1172 prog = bpf_prog_get(ufd);
1174 return PTR_ERR(prog);
1176 if (prog->type != BPF_PROG_TYPE_SOCKET_FILTER) {
1181 err = __sk_attach_prog(prog, sk);
1191 * bpf_skb_clone_not_writable - is the header of a clone not writable
1192 * @skb: buffer to check
1193 * @len: length up to which to write, can be negative
1195 * Returns true if modifying the header part of the cloned buffer
1196 * does require the data to be copied. I.e. this version works with
1197 * negative lengths needed for eBPF case!
1199 static bool bpf_skb_clone_unwritable(const struct sk_buff *skb, int len)
1201 return skb_header_cloned(skb) ||
1202 (int) skb_headroom(skb) + len > skb->hdr_len;
1205 #define BPF_RECOMPUTE_CSUM(flags) ((flags) & 1)
1207 static u64 bpf_skb_store_bytes(u64 r1, u64 r2, u64 r3, u64 r4, u64 flags)
1209 struct sk_buff *skb = (struct sk_buff *) (long) r1;
1210 int offset = (int) r2;
1211 void *from = (void *) (long) r3;
1212 unsigned int len = (unsigned int) r4;
1216 /* bpf verifier guarantees that:
1217 * 'from' pointer points to bpf program stack
1218 * 'len' bytes of it were initialized
1220 * 'skb' is a valid pointer to 'struct sk_buff'
1222 * so check for invalid 'offset' and too large 'len'
1224 if (unlikely((u32) offset > 0xffff || len > sizeof(buf)))
1227 offset -= skb->data - skb_mac_header(skb);
1228 if (unlikely(skb_cloned(skb) &&
1229 bpf_skb_clone_unwritable(skb, offset + len)))
1232 ptr = skb_header_pointer(skb, offset, len, buf);
1236 if (BPF_RECOMPUTE_CSUM(flags))
1237 skb_postpull_rcsum(skb, ptr, len);
1239 memcpy(ptr, from, len);
1242 /* skb_store_bits cannot return -EFAULT here */
1243 skb_store_bits(skb, offset, ptr, len);
1245 if (BPF_RECOMPUTE_CSUM(flags) && skb->ip_summed == CHECKSUM_COMPLETE)
1246 skb->csum = csum_add(skb->csum, csum_partial(ptr, len, 0));
1250 const struct bpf_func_proto bpf_skb_store_bytes_proto = {
1251 .func = bpf_skb_store_bytes,
1253 .ret_type = RET_INTEGER,
1254 .arg1_type = ARG_PTR_TO_CTX,
1255 .arg2_type = ARG_ANYTHING,
1256 .arg3_type = ARG_PTR_TO_STACK,
1257 .arg4_type = ARG_CONST_STACK_SIZE,
1258 .arg5_type = ARG_ANYTHING,
1261 #define BPF_HEADER_FIELD_SIZE(flags) ((flags) & 0x0f)
1262 #define BPF_IS_PSEUDO_HEADER(flags) ((flags) & 0x10)
1264 static u64 bpf_l3_csum_replace(u64 r1, u64 r2, u64 from, u64 to, u64 flags)
1266 struct sk_buff *skb = (struct sk_buff *) (long) r1;
1267 int offset = (int) r2;
1270 if (unlikely((u32) offset > 0xffff))
1273 offset -= skb->data - skb_mac_header(skb);
1274 if (unlikely(skb_cloned(skb) &&
1275 bpf_skb_clone_unwritable(skb, offset + sizeof(sum))))
1278 ptr = skb_header_pointer(skb, offset, sizeof(sum), &sum);
1282 switch (BPF_HEADER_FIELD_SIZE(flags)) {
1284 csum_replace2(ptr, from, to);
1287 csum_replace4(ptr, from, to);
1294 /* skb_store_bits guaranteed to not return -EFAULT here */
1295 skb_store_bits(skb, offset, ptr, sizeof(sum));
1300 const struct bpf_func_proto bpf_l3_csum_replace_proto = {
1301 .func = bpf_l3_csum_replace,
1303 .ret_type = RET_INTEGER,
1304 .arg1_type = ARG_PTR_TO_CTX,
1305 .arg2_type = ARG_ANYTHING,
1306 .arg3_type = ARG_ANYTHING,
1307 .arg4_type = ARG_ANYTHING,
1308 .arg5_type = ARG_ANYTHING,
1311 static u64 bpf_l4_csum_replace(u64 r1, u64 r2, u64 from, u64 to, u64 flags)
1313 struct sk_buff *skb = (struct sk_buff *) (long) r1;
1314 u32 is_pseudo = BPF_IS_PSEUDO_HEADER(flags);
1315 int offset = (int) r2;
1318 if (unlikely((u32) offset > 0xffff))
1321 offset -= skb->data - skb_mac_header(skb);
1322 if (unlikely(skb_cloned(skb) &&
1323 bpf_skb_clone_unwritable(skb, offset + sizeof(sum))))
1326 ptr = skb_header_pointer(skb, offset, sizeof(sum), &sum);
1330 switch (BPF_HEADER_FIELD_SIZE(flags)) {
1332 inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo);
1335 inet_proto_csum_replace4(ptr, skb, from, to, is_pseudo);
1342 /* skb_store_bits guaranteed to not return -EFAULT here */
1343 skb_store_bits(skb, offset, ptr, sizeof(sum));
1348 const struct bpf_func_proto bpf_l4_csum_replace_proto = {
1349 .func = bpf_l4_csum_replace,
1351 .ret_type = RET_INTEGER,
1352 .arg1_type = ARG_PTR_TO_CTX,
1353 .arg2_type = ARG_ANYTHING,
1354 .arg3_type = ARG_ANYTHING,
1355 .arg4_type = ARG_ANYTHING,
1356 .arg5_type = ARG_ANYTHING,
1359 static const struct bpf_func_proto *
1360 sk_filter_func_proto(enum bpf_func_id func_id)
1363 case BPF_FUNC_map_lookup_elem:
1364 return &bpf_map_lookup_elem_proto;
1365 case BPF_FUNC_map_update_elem:
1366 return &bpf_map_update_elem_proto;
1367 case BPF_FUNC_map_delete_elem:
1368 return &bpf_map_delete_elem_proto;
1369 case BPF_FUNC_get_prandom_u32:
1370 return &bpf_get_prandom_u32_proto;
1371 case BPF_FUNC_get_smp_processor_id:
1372 return &bpf_get_smp_processor_id_proto;
1378 static const struct bpf_func_proto *
1379 tc_cls_act_func_proto(enum bpf_func_id func_id)
1382 case BPF_FUNC_skb_store_bytes:
1383 return &bpf_skb_store_bytes_proto;
1384 case BPF_FUNC_l3_csum_replace:
1385 return &bpf_l3_csum_replace_proto;
1386 case BPF_FUNC_l4_csum_replace:
1387 return &bpf_l4_csum_replace_proto;
1389 return sk_filter_func_proto(func_id);
1393 static bool sk_filter_is_valid_access(int off, int size,
1394 enum bpf_access_type type)
1396 /* only read is allowed */
1397 if (type != BPF_READ)
1401 if (off < 0 || off >= sizeof(struct __sk_buff))
1404 /* disallow misaligned access */
1405 if (off % size != 0)
1408 /* all __sk_buff fields are __u32 */
1415 static u32 sk_filter_convert_ctx_access(int dst_reg, int src_reg, int ctx_off,
1416 struct bpf_insn *insn_buf)
1418 struct bpf_insn *insn = insn_buf;
1421 case offsetof(struct __sk_buff, len):
1422 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, len) != 4);
1424 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
1425 offsetof(struct sk_buff, len));
1428 case offsetof(struct __sk_buff, protocol):
1429 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
1431 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
1432 offsetof(struct sk_buff, protocol));
1435 case offsetof(struct __sk_buff, vlan_proto):
1436 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2);
1438 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
1439 offsetof(struct sk_buff, vlan_proto));
1442 case offsetof(struct __sk_buff, priority):
1443 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, priority) != 4);
1445 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
1446 offsetof(struct sk_buff, priority));
1449 case offsetof(struct __sk_buff, mark):
1450 return convert_skb_access(SKF_AD_MARK, dst_reg, src_reg, insn);
1452 case offsetof(struct __sk_buff, pkt_type):
1453 return convert_skb_access(SKF_AD_PKTTYPE, dst_reg, src_reg, insn);
1455 case offsetof(struct __sk_buff, queue_mapping):
1456 return convert_skb_access(SKF_AD_QUEUE, dst_reg, src_reg, insn);
1458 case offsetof(struct __sk_buff, vlan_present):
1459 return convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
1460 dst_reg, src_reg, insn);
1462 case offsetof(struct __sk_buff, vlan_tci):
1463 return convert_skb_access(SKF_AD_VLAN_TAG,
1464 dst_reg, src_reg, insn);
1467 return insn - insn_buf;
1470 static const struct bpf_verifier_ops sk_filter_ops = {
1471 .get_func_proto = sk_filter_func_proto,
1472 .is_valid_access = sk_filter_is_valid_access,
1473 .convert_ctx_access = sk_filter_convert_ctx_access,
1476 static const struct bpf_verifier_ops tc_cls_act_ops = {
1477 .get_func_proto = tc_cls_act_func_proto,
1478 .is_valid_access = sk_filter_is_valid_access,
1479 .convert_ctx_access = sk_filter_convert_ctx_access,
1482 static struct bpf_prog_type_list sk_filter_type __read_mostly = {
1483 .ops = &sk_filter_ops,
1484 .type = BPF_PROG_TYPE_SOCKET_FILTER,
1487 static struct bpf_prog_type_list sched_cls_type __read_mostly = {
1488 .ops = &tc_cls_act_ops,
1489 .type = BPF_PROG_TYPE_SCHED_CLS,
1492 static struct bpf_prog_type_list sched_act_type __read_mostly = {
1493 .ops = &tc_cls_act_ops,
1494 .type = BPF_PROG_TYPE_SCHED_ACT,
1497 static int __init register_sk_filter_ops(void)
1499 bpf_register_prog_type(&sk_filter_type);
1500 bpf_register_prog_type(&sched_cls_type);
1501 bpf_register_prog_type(&sched_act_type);
1505 late_initcall(register_sk_filter_ops);
1507 int sk_detach_filter(struct sock *sk)
1510 struct sk_filter *filter;
1512 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1515 filter = rcu_dereference_protected(sk->sk_filter,
1516 sock_owned_by_user(sk));
1518 RCU_INIT_POINTER(sk->sk_filter, NULL);
1519 sk_filter_uncharge(sk, filter);
1525 EXPORT_SYMBOL_GPL(sk_detach_filter);
1527 int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf,
1530 struct sock_fprog_kern *fprog;
1531 struct sk_filter *filter;
1535 filter = rcu_dereference_protected(sk->sk_filter,
1536 sock_owned_by_user(sk));
1540 /* We're copying the filter that has been originally attached,
1541 * so no conversion/decode needed anymore.
1543 fprog = filter->prog->orig_prog;
1547 /* User space only enquires number of filter blocks. */
1551 if (len < fprog->len)
1555 if (copy_to_user(ubuf, fprog->filter, bpf_classic_proglen(fprog)))
1558 /* Instead of bytes, the API requests to return the number