batman-adv: remove old bridge loop avoidance code
[linux-2.6.git] / net / core / filter.c
blob95d05a6012d18bf664466fa5feb613f6e9b651d6
1 /*
2 * Linux Socket Filter - Kernel level socket filtering
4 * Author:
5 * Jay Schulist <jschlst@samba.org>
7 * Based on the design of:
8 * - The Berkeley Packet Filter
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
15 * Andi Kleen - Fix a few bad bugs and races.
16 * Kris Katterjohn - Added many additional checks in sk_chk_filter()
19 #include <linux/module.h>
20 #include <linux/types.h>
21 #include <linux/mm.h>
22 #include <linux/fcntl.h>
23 #include <linux/socket.h>
24 #include <linux/in.h>
25 #include <linux/inet.h>
26 #include <linux/netdevice.h>
27 #include <linux/if_packet.h>
28 #include <linux/gfp.h>
29 #include <net/ip.h>
30 #include <net/protocol.h>
31 #include <net/netlink.h>
32 #include <linux/skbuff.h>
33 #include <net/sock.h>
34 #include <linux/errno.h>
35 #include <linux/timer.h>
36 #include <asm/uaccess.h>
37 #include <asm/unaligned.h>
38 #include <linux/filter.h>
39 #include <linux/reciprocal_div.h>
40 #include <linux/ratelimit.h>
42 /* No hurry in this branch
44 * Exported for the bpf jit load helper.
46 void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb, int k, unsigned int size)
48 u8 *ptr = NULL;
50 if (k >= SKF_NET_OFF)
51 ptr = skb_network_header(skb) + k - SKF_NET_OFF;
52 else if (k >= SKF_LL_OFF)
53 ptr = skb_mac_header(skb) + k - SKF_LL_OFF;
55 if (ptr >= skb->head && ptr + size <= skb_tail_pointer(skb))
56 return ptr;
57 return NULL;
60 static inline void *load_pointer(const struct sk_buff *skb, int k,
61 unsigned int size, void *buffer)
63 if (k >= 0)
64 return skb_header_pointer(skb, k, size, buffer);
65 return bpf_internal_load_pointer_neg_helper(skb, k, size);
68 /**
69 * sk_filter - run a packet through a socket filter
70 * @sk: sock associated with &sk_buff
71 * @skb: buffer to filter
73 * Run the filter code and then cut skb->data to correct size returned by
74 * sk_run_filter. If pkt_len is 0 we toss packet. If skb->len is smaller
75 * than pkt_len we keep whole skb->data. This is the socket level
76 * wrapper to sk_run_filter. It returns 0 if the packet should
77 * be accepted or -EPERM if the packet should be tossed.
80 int sk_filter(struct sock *sk, struct sk_buff *skb)
82 int err;
83 struct sk_filter *filter;
85 err = security_sock_rcv_skb(sk, skb);
86 if (err)
87 return err;
89 rcu_read_lock();
90 filter = rcu_dereference(sk->sk_filter);
91 if (filter) {
92 unsigned int pkt_len = SK_RUN_FILTER(filter, skb);
94 err = pkt_len ? pskb_trim(skb, pkt_len) : -EPERM;
96 rcu_read_unlock();
98 return err;
100 EXPORT_SYMBOL(sk_filter);
103 * sk_run_filter - run a filter on a socket
104 * @skb: buffer to run the filter on
105 * @fentry: filter to apply
107 * Decode and apply filter instructions to the skb->data.
108 * Return length to keep, 0 for none. @skb is the data we are
109 * filtering, @filter is the array of filter instructions.
110 * Because all jumps are guaranteed to be before last instruction,
111 * and last instruction guaranteed to be a RET, we dont need to check
112 * flen. (We used to pass to this function the length of filter)
114 unsigned int sk_run_filter(const struct sk_buff *skb,
115 const struct sock_filter *fentry)
117 void *ptr;
118 u32 A = 0; /* Accumulator */
119 u32 X = 0; /* Index Register */
120 u32 mem[BPF_MEMWORDS]; /* Scratch Memory Store */
121 u32 tmp;
122 int k;
125 * Process array of filter instructions.
127 for (;; fentry++) {
128 #if defined(CONFIG_X86_32)
129 #define K (fentry->k)
130 #else
131 const u32 K = fentry->k;
132 #endif
134 switch (fentry->code) {
135 case BPF_S_ALU_ADD_X:
136 A += X;
137 continue;
138 case BPF_S_ALU_ADD_K:
139 A += K;
140 continue;
141 case BPF_S_ALU_SUB_X:
142 A -= X;
143 continue;
144 case BPF_S_ALU_SUB_K:
145 A -= K;
146 continue;
147 case BPF_S_ALU_MUL_X:
148 A *= X;
149 continue;
150 case BPF_S_ALU_MUL_K:
151 A *= K;
152 continue;
153 case BPF_S_ALU_DIV_X:
154 if (X == 0)
155 return 0;
156 A /= X;
157 continue;
158 case BPF_S_ALU_DIV_K:
159 A = reciprocal_divide(A, K);
160 continue;
161 case BPF_S_ALU_AND_X:
162 A &= X;
163 continue;
164 case BPF_S_ALU_AND_K:
165 A &= K;
166 continue;
167 case BPF_S_ALU_OR_X:
168 A |= X;
169 continue;
170 case BPF_S_ALU_OR_K:
171 A |= K;
172 continue;
173 case BPF_S_ALU_LSH_X:
174 A <<= X;
175 continue;
176 case BPF_S_ALU_LSH_K:
177 A <<= K;
178 continue;
179 case BPF_S_ALU_RSH_X:
180 A >>= X;
181 continue;
182 case BPF_S_ALU_RSH_K:
183 A >>= K;
184 continue;
185 case BPF_S_ALU_NEG:
186 A = -A;
187 continue;
188 case BPF_S_JMP_JA:
189 fentry += K;
190 continue;
191 case BPF_S_JMP_JGT_K:
192 fentry += (A > K) ? fentry->jt : fentry->jf;
193 continue;
194 case BPF_S_JMP_JGE_K:
195 fentry += (A >= K) ? fentry->jt : fentry->jf;
196 continue;
197 case BPF_S_JMP_JEQ_K:
198 fentry += (A == K) ? fentry->jt : fentry->jf;
199 continue;
200 case BPF_S_JMP_JSET_K:
201 fentry += (A & K) ? fentry->jt : fentry->jf;
202 continue;
203 case BPF_S_JMP_JGT_X:
204 fentry += (A > X) ? fentry->jt : fentry->jf;
205 continue;
206 case BPF_S_JMP_JGE_X:
207 fentry += (A >= X) ? fentry->jt : fentry->jf;
208 continue;
209 case BPF_S_JMP_JEQ_X:
210 fentry += (A == X) ? fentry->jt : fentry->jf;
211 continue;
212 case BPF_S_JMP_JSET_X:
213 fentry += (A & X) ? fentry->jt : fentry->jf;
214 continue;
215 case BPF_S_LD_W_ABS:
216 k = K;
217 load_w:
218 ptr = load_pointer(skb, k, 4, &tmp);
219 if (ptr != NULL) {
220 A = get_unaligned_be32(ptr);
221 continue;
223 return 0;
224 case BPF_S_LD_H_ABS:
225 k = K;
226 load_h:
227 ptr = load_pointer(skb, k, 2, &tmp);
228 if (ptr != NULL) {
229 A = get_unaligned_be16(ptr);
230 continue;
232 return 0;
233 case BPF_S_LD_B_ABS:
234 k = K;
235 load_b:
236 ptr = load_pointer(skb, k, 1, &tmp);
237 if (ptr != NULL) {
238 A = *(u8 *)ptr;
239 continue;
241 return 0;
242 case BPF_S_LD_W_LEN:
243 A = skb->len;
244 continue;
245 case BPF_S_LDX_W_LEN:
246 X = skb->len;
247 continue;
248 case BPF_S_LD_W_IND:
249 k = X + K;
250 goto load_w;
251 case BPF_S_LD_H_IND:
252 k = X + K;
253 goto load_h;
254 case BPF_S_LD_B_IND:
255 k = X + K;
256 goto load_b;
257 case BPF_S_LDX_B_MSH:
258 ptr = load_pointer(skb, K, 1, &tmp);
259 if (ptr != NULL) {
260 X = (*(u8 *)ptr & 0xf) << 2;
261 continue;
263 return 0;
264 case BPF_S_LD_IMM:
265 A = K;
266 continue;
267 case BPF_S_LDX_IMM:
268 X = K;
269 continue;
270 case BPF_S_LD_MEM:
271 A = mem[K];
272 continue;
273 case BPF_S_LDX_MEM:
274 X = mem[K];
275 continue;
276 case BPF_S_MISC_TAX:
277 X = A;
278 continue;
279 case BPF_S_MISC_TXA:
280 A = X;
281 continue;
282 case BPF_S_RET_K:
283 return K;
284 case BPF_S_RET_A:
285 return A;
286 case BPF_S_ST:
287 mem[K] = A;
288 continue;
289 case BPF_S_STX:
290 mem[K] = X;
291 continue;
292 case BPF_S_ANC_PROTOCOL:
293 A = ntohs(skb->protocol);
294 continue;
295 case BPF_S_ANC_PKTTYPE:
296 A = skb->pkt_type;
297 continue;
298 case BPF_S_ANC_IFINDEX:
299 if (!skb->dev)
300 return 0;
301 A = skb->dev->ifindex;
302 continue;
303 case BPF_S_ANC_MARK:
304 A = skb->mark;
305 continue;
306 case BPF_S_ANC_QUEUE:
307 A = skb->queue_mapping;
308 continue;
309 case BPF_S_ANC_HATYPE:
310 if (!skb->dev)
311 return 0;
312 A = skb->dev->type;
313 continue;
314 case BPF_S_ANC_RXHASH:
315 A = skb->rxhash;
316 continue;
317 case BPF_S_ANC_CPU:
318 A = raw_smp_processor_id();
319 continue;
320 case BPF_S_ANC_ALU_XOR_X:
321 A ^= X;
322 continue;
323 case BPF_S_ANC_NLATTR: {
324 struct nlattr *nla;
326 if (skb_is_nonlinear(skb))
327 return 0;
328 if (A > skb->len - sizeof(struct nlattr))
329 return 0;
331 nla = nla_find((struct nlattr *)&skb->data[A],
332 skb->len - A, X);
333 if (nla)
334 A = (void *)nla - (void *)skb->data;
335 else
336 A = 0;
337 continue;
339 case BPF_S_ANC_NLATTR_NEST: {
340 struct nlattr *nla;
342 if (skb_is_nonlinear(skb))
343 return 0;
344 if (A > skb->len - sizeof(struct nlattr))
345 return 0;
347 nla = (struct nlattr *)&skb->data[A];
348 if (nla->nla_len > A - skb->len)
349 return 0;
351 nla = nla_find_nested(nla, X);
352 if (nla)
353 A = (void *)nla - (void *)skb->data;
354 else
355 A = 0;
356 continue;
358 default:
359 WARN_RATELIMIT(1, "Unknown code:%u jt:%u tf:%u k:%u\n",
360 fentry->code, fentry->jt,
361 fentry->jf, fentry->k);
362 return 0;
366 return 0;
368 EXPORT_SYMBOL(sk_run_filter);
371 * Security :
372 * A BPF program is able to use 16 cells of memory to store intermediate
373 * values (check u32 mem[BPF_MEMWORDS] in sk_run_filter())
374 * As we dont want to clear mem[] array for each packet going through
375 * sk_run_filter(), we check that filter loaded by user never try to read
376 * a cell if not previously written, and we check all branches to be sure
377 * a malicious user doesn't try to abuse us.
379 static int check_load_and_stores(struct sock_filter *filter, int flen)
381 u16 *masks, memvalid = 0; /* one bit per cell, 16 cells */
382 int pc, ret = 0;
384 BUILD_BUG_ON(BPF_MEMWORDS > 16);
385 masks = kmalloc(flen * sizeof(*masks), GFP_KERNEL);
386 if (!masks)
387 return -ENOMEM;
388 memset(masks, 0xff, flen * sizeof(*masks));
390 for (pc = 0; pc < flen; pc++) {
391 memvalid &= masks[pc];
393 switch (filter[pc].code) {
394 case BPF_S_ST:
395 case BPF_S_STX:
396 memvalid |= (1 << filter[pc].k);
397 break;
398 case BPF_S_LD_MEM:
399 case BPF_S_LDX_MEM:
400 if (!(memvalid & (1 << filter[pc].k))) {
401 ret = -EINVAL;
402 goto error;
404 break;
405 case BPF_S_JMP_JA:
406 /* a jump must set masks on target */
407 masks[pc + 1 + filter[pc].k] &= memvalid;
408 memvalid = ~0;
409 break;
410 case BPF_S_JMP_JEQ_K:
411 case BPF_S_JMP_JEQ_X:
412 case BPF_S_JMP_JGE_K:
413 case BPF_S_JMP_JGE_X:
414 case BPF_S_JMP_JGT_K:
415 case BPF_S_JMP_JGT_X:
416 case BPF_S_JMP_JSET_X:
417 case BPF_S_JMP_JSET_K:
418 /* a jump must set masks on targets */
419 masks[pc + 1 + filter[pc].jt] &= memvalid;
420 masks[pc + 1 + filter[pc].jf] &= memvalid;
421 memvalid = ~0;
422 break;
425 error:
426 kfree(masks);
427 return ret;
431 * sk_chk_filter - verify socket filter code
432 * @filter: filter to verify
433 * @flen: length of filter
435 * Check the user's filter code. If we let some ugly
436 * filter code slip through kaboom! The filter must contain
437 * no references or jumps that are out of range, no illegal
438 * instructions, and must end with a RET instruction.
440 * All jumps are forward as they are not signed.
442 * Returns 0 if the rule set is legal or -EINVAL if not.
444 int sk_chk_filter(struct sock_filter *filter, unsigned int flen)
447 * Valid instructions are initialized to non-0.
448 * Invalid instructions are initialized to 0.
450 static const u8 codes[] = {
451 [BPF_ALU|BPF_ADD|BPF_K] = BPF_S_ALU_ADD_K,
452 [BPF_ALU|BPF_ADD|BPF_X] = BPF_S_ALU_ADD_X,
453 [BPF_ALU|BPF_SUB|BPF_K] = BPF_S_ALU_SUB_K,
454 [BPF_ALU|BPF_SUB|BPF_X] = BPF_S_ALU_SUB_X,
455 [BPF_ALU|BPF_MUL|BPF_K] = BPF_S_ALU_MUL_K,
456 [BPF_ALU|BPF_MUL|BPF_X] = BPF_S_ALU_MUL_X,
457 [BPF_ALU|BPF_DIV|BPF_X] = BPF_S_ALU_DIV_X,
458 [BPF_ALU|BPF_AND|BPF_K] = BPF_S_ALU_AND_K,
459 [BPF_ALU|BPF_AND|BPF_X] = BPF_S_ALU_AND_X,
460 [BPF_ALU|BPF_OR|BPF_K] = BPF_S_ALU_OR_K,
461 [BPF_ALU|BPF_OR|BPF_X] = BPF_S_ALU_OR_X,
462 [BPF_ALU|BPF_LSH|BPF_K] = BPF_S_ALU_LSH_K,
463 [BPF_ALU|BPF_LSH|BPF_X] = BPF_S_ALU_LSH_X,
464 [BPF_ALU|BPF_RSH|BPF_K] = BPF_S_ALU_RSH_K,
465 [BPF_ALU|BPF_RSH|BPF_X] = BPF_S_ALU_RSH_X,
466 [BPF_ALU|BPF_NEG] = BPF_S_ALU_NEG,
467 [BPF_LD|BPF_W|BPF_ABS] = BPF_S_LD_W_ABS,
468 [BPF_LD|BPF_H|BPF_ABS] = BPF_S_LD_H_ABS,
469 [BPF_LD|BPF_B|BPF_ABS] = BPF_S_LD_B_ABS,
470 [BPF_LD|BPF_W|BPF_LEN] = BPF_S_LD_W_LEN,
471 [BPF_LD|BPF_W|BPF_IND] = BPF_S_LD_W_IND,
472 [BPF_LD|BPF_H|BPF_IND] = BPF_S_LD_H_IND,
473 [BPF_LD|BPF_B|BPF_IND] = BPF_S_LD_B_IND,
474 [BPF_LD|BPF_IMM] = BPF_S_LD_IMM,
475 [BPF_LDX|BPF_W|BPF_LEN] = BPF_S_LDX_W_LEN,
476 [BPF_LDX|BPF_B|BPF_MSH] = BPF_S_LDX_B_MSH,
477 [BPF_LDX|BPF_IMM] = BPF_S_LDX_IMM,
478 [BPF_MISC|BPF_TAX] = BPF_S_MISC_TAX,
479 [BPF_MISC|BPF_TXA] = BPF_S_MISC_TXA,
480 [BPF_RET|BPF_K] = BPF_S_RET_K,
481 [BPF_RET|BPF_A] = BPF_S_RET_A,
482 [BPF_ALU|BPF_DIV|BPF_K] = BPF_S_ALU_DIV_K,
483 [BPF_LD|BPF_MEM] = BPF_S_LD_MEM,
484 [BPF_LDX|BPF_MEM] = BPF_S_LDX_MEM,
485 [BPF_ST] = BPF_S_ST,
486 [BPF_STX] = BPF_S_STX,
487 [BPF_JMP|BPF_JA] = BPF_S_JMP_JA,
488 [BPF_JMP|BPF_JEQ|BPF_K] = BPF_S_JMP_JEQ_K,
489 [BPF_JMP|BPF_JEQ|BPF_X] = BPF_S_JMP_JEQ_X,
490 [BPF_JMP|BPF_JGE|BPF_K] = BPF_S_JMP_JGE_K,
491 [BPF_JMP|BPF_JGE|BPF_X] = BPF_S_JMP_JGE_X,
492 [BPF_JMP|BPF_JGT|BPF_K] = BPF_S_JMP_JGT_K,
493 [BPF_JMP|BPF_JGT|BPF_X] = BPF_S_JMP_JGT_X,
494 [BPF_JMP|BPF_JSET|BPF_K] = BPF_S_JMP_JSET_K,
495 [BPF_JMP|BPF_JSET|BPF_X] = BPF_S_JMP_JSET_X,
497 int pc;
499 if (flen == 0 || flen > BPF_MAXINSNS)
500 return -EINVAL;
502 /* check the filter code now */
503 for (pc = 0; pc < flen; pc++) {
504 struct sock_filter *ftest = &filter[pc];
505 u16 code = ftest->code;
507 if (code >= ARRAY_SIZE(codes))
508 return -EINVAL;
509 code = codes[code];
510 if (!code)
511 return -EINVAL;
512 /* Some instructions need special checks */
513 switch (code) {
514 case BPF_S_ALU_DIV_K:
515 /* check for division by zero */
516 if (ftest->k == 0)
517 return -EINVAL;
518 ftest->k = reciprocal_value(ftest->k);
519 break;
520 case BPF_S_LD_MEM:
521 case BPF_S_LDX_MEM:
522 case BPF_S_ST:
523 case BPF_S_STX:
524 /* check for invalid memory addresses */
525 if (ftest->k >= BPF_MEMWORDS)
526 return -EINVAL;
527 break;
528 case BPF_S_JMP_JA:
530 * Note, the large ftest->k might cause loops.
531 * Compare this with conditional jumps below,
532 * where offsets are limited. --ANK (981016)
534 if (ftest->k >= (unsigned)(flen-pc-1))
535 return -EINVAL;
536 break;
537 case BPF_S_JMP_JEQ_K:
538 case BPF_S_JMP_JEQ_X:
539 case BPF_S_JMP_JGE_K:
540 case BPF_S_JMP_JGE_X:
541 case BPF_S_JMP_JGT_K:
542 case BPF_S_JMP_JGT_X:
543 case BPF_S_JMP_JSET_X:
544 case BPF_S_JMP_JSET_K:
545 /* for conditionals both must be safe */
546 if (pc + ftest->jt + 1 >= flen ||
547 pc + ftest->jf + 1 >= flen)
548 return -EINVAL;
549 break;
550 case BPF_S_LD_W_ABS:
551 case BPF_S_LD_H_ABS:
552 case BPF_S_LD_B_ABS:
553 #define ANCILLARY(CODE) case SKF_AD_OFF + SKF_AD_##CODE: \
554 code = BPF_S_ANC_##CODE; \
555 break
556 switch (ftest->k) {
557 ANCILLARY(PROTOCOL);
558 ANCILLARY(PKTTYPE);
559 ANCILLARY(IFINDEX);
560 ANCILLARY(NLATTR);
561 ANCILLARY(NLATTR_NEST);
562 ANCILLARY(MARK);
563 ANCILLARY(QUEUE);
564 ANCILLARY(HATYPE);
565 ANCILLARY(RXHASH);
566 ANCILLARY(CPU);
567 ANCILLARY(ALU_XOR_X);
570 ftest->code = code;
573 /* last instruction must be a RET code */
574 switch (filter[flen - 1].code) {
575 case BPF_S_RET_K:
576 case BPF_S_RET_A:
577 return check_load_and_stores(filter, flen);
579 return -EINVAL;
581 EXPORT_SYMBOL(sk_chk_filter);
584 * sk_filter_release_rcu - Release a socket filter by rcu_head
585 * @rcu: rcu_head that contains the sk_filter to free
587 void sk_filter_release_rcu(struct rcu_head *rcu)
589 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
591 bpf_jit_free(fp);
592 kfree(fp);
594 EXPORT_SYMBOL(sk_filter_release_rcu);
596 static int __sk_prepare_filter(struct sk_filter *fp)
598 int err;
600 fp->bpf_func = sk_run_filter;
602 err = sk_chk_filter(fp->insns, fp->len);
603 if (err)
604 return err;
606 bpf_jit_compile(fp);
607 return 0;
611 * sk_unattached_filter_create - create an unattached filter
612 * @fprog: the filter program
613 * @sk: the socket to use
615 * Create a filter independent ofr any socket. We first run some
616 * sanity checks on it to make sure it does not explode on us later.
617 * If an error occurs or there is insufficient memory for the filter
618 * a negative errno code is returned. On success the return is zero.
620 int sk_unattached_filter_create(struct sk_filter **pfp,
621 struct sock_fprog *fprog)
623 struct sk_filter *fp;
624 unsigned int fsize = sizeof(struct sock_filter) * fprog->len;
625 int err;
627 /* Make sure new filter is there and in the right amounts. */
628 if (fprog->filter == NULL)
629 return -EINVAL;
631 fp = kmalloc(fsize + sizeof(*fp), GFP_KERNEL);
632 if (!fp)
633 return -ENOMEM;
634 memcpy(fp->insns, fprog->filter, fsize);
636 atomic_set(&fp->refcnt, 1);
637 fp->len = fprog->len;
639 err = __sk_prepare_filter(fp);
640 if (err)
641 goto free_mem;
643 *pfp = fp;
644 return 0;
645 free_mem:
646 kfree(fp);
647 return err;
649 EXPORT_SYMBOL_GPL(sk_unattached_filter_create);
651 void sk_unattached_filter_destroy(struct sk_filter *fp)
653 sk_filter_release(fp);
655 EXPORT_SYMBOL_GPL(sk_unattached_filter_destroy);
658 * sk_attach_filter - attach a socket filter
659 * @fprog: the filter program
660 * @sk: the socket to use
662 * Attach the user's filter code. We first run some sanity checks on
663 * it to make sure it does not explode on us later. If an error
664 * occurs or there is insufficient memory for the filter a negative
665 * errno code is returned. On success the return is zero.
667 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
669 struct sk_filter *fp, *old_fp;
670 unsigned int fsize = sizeof(struct sock_filter) * fprog->len;
671 int err;
673 /* Make sure new filter is there and in the right amounts. */
674 if (fprog->filter == NULL)
675 return -EINVAL;
677 fp = sock_kmalloc(sk, fsize+sizeof(*fp), GFP_KERNEL);
678 if (!fp)
679 return -ENOMEM;
680 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
681 sock_kfree_s(sk, fp, fsize+sizeof(*fp));
682 return -EFAULT;
685 atomic_set(&fp->refcnt, 1);
686 fp->len = fprog->len;
688 err = __sk_prepare_filter(fp);
689 if (err) {
690 sk_filter_uncharge(sk, fp);
691 return err;
694 old_fp = rcu_dereference_protected(sk->sk_filter,
695 sock_owned_by_user(sk));
696 rcu_assign_pointer(sk->sk_filter, fp);
698 if (old_fp)
699 sk_filter_uncharge(sk, old_fp);
700 return 0;
702 EXPORT_SYMBOL_GPL(sk_attach_filter);
704 int sk_detach_filter(struct sock *sk)
706 int ret = -ENOENT;
707 struct sk_filter *filter;
709 filter = rcu_dereference_protected(sk->sk_filter,
710 sock_owned_by_user(sk));
711 if (filter) {
712 RCU_INIT_POINTER(sk->sk_filter, NULL);
713 sk_filter_uncharge(sk, filter);
714 ret = 0;
716 return ret;
718 EXPORT_SYMBOL_GPL(sk_detach_filter);