2 * Linux Socket Filter - Kernel level socket filtering
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.
18 #include <linux/config.h>
19 #if defined(CONFIG_FILTER)
21 #include <linux/module.h>
22 #include <linux/types.h>
23 #include <linux/sched.h>
25 #include <linux/fcntl.h>
26 #include <linux/socket.h>
28 #include <linux/inet.h>
29 #include <linux/netdevice.h>
30 #include <linux/if_packet.h>
32 #include <net/protocol.h>
33 #include <linux/skbuff.h>
35 #include <linux/errno.h>
36 #include <linux/timer.h>
37 #include <asm/system.h>
38 #include <asm/uaccess.h>
39 #include <linux/filter.h>
41 /* No hurry in this branch */
43 static u8 *load_pointer(struct sk_buff *skb, int k)
48 ptr = skb->nh.raw + k - SKF_NET_OFF;
49 else if (k>=SKF_LL_OFF)
50 ptr = skb->mac.raw + k - SKF_LL_OFF;
52 if (ptr >= skb->head && ptr < skb->tail)
58 * sk_run_filter - run a filter on a socket
59 * @skb: buffer to run the filter on
60 * @filter: filter to apply
61 * @flen: length of filter
63 * Decode and apply filter instructions to the skb->data.
64 * Return length to keep, 0 for none. skb is the data we are
65 * filtering, filter is the array of filter instructions, and
66 * len is the number of filter blocks in the array.
69 int sk_run_filter(struct sk_buff *skb, struct sock_filter *filter, int flen)
71 unsigned char *data = skb->data;
72 /* len is UNSIGNED. Byte wide insns relies only on implicit
73 type casts to prevent reading arbitrary memory locations.
75 unsigned int len = skb->len-skb->data_len;
76 struct sock_filter *fentry; /* We walk down these */
77 u32 A = 0; /* Accumulator */
78 u32 X = 0; /* Index Register */
79 u32 mem[BPF_MEMWORDS]; /* Scratch Memory Store */
84 * Process array of filter instructions.
87 for(pc = 0; pc < flen; pc++)
93 case BPF_ALU|BPF_ADD|BPF_X:
97 case BPF_ALU|BPF_ADD|BPF_K:
101 case BPF_ALU|BPF_SUB|BPF_X:
105 case BPF_ALU|BPF_SUB|BPF_K:
109 case BPF_ALU|BPF_MUL|BPF_X:
113 case BPF_ALU|BPF_MUL|BPF_K:
117 case BPF_ALU|BPF_DIV|BPF_X:
123 case BPF_ALU|BPF_DIV|BPF_K:
129 case BPF_ALU|BPF_AND|BPF_X:
133 case BPF_ALU|BPF_AND|BPF_K:
137 case BPF_ALU|BPF_OR|BPF_X:
141 case BPF_ALU|BPF_OR|BPF_K:
145 case BPF_ALU|BPF_LSH|BPF_X:
149 case BPF_ALU|BPF_LSH|BPF_K:
153 case BPF_ALU|BPF_RSH|BPF_X:
157 case BPF_ALU|BPF_RSH|BPF_K:
161 case BPF_ALU|BPF_NEG:
169 case BPF_JMP|BPF_JGT|BPF_K:
170 pc += (A > fentry->k) ? fentry->jt : fentry->jf;
173 case BPF_JMP|BPF_JGE|BPF_K:
174 pc += (A >= fentry->k) ? fentry->jt : fentry->jf;
177 case BPF_JMP|BPF_JEQ|BPF_K:
178 pc += (A == fentry->k) ? fentry->jt : fentry->jf;
181 case BPF_JMP|BPF_JSET|BPF_K:
182 pc += (A & fentry->k) ? fentry->jt : fentry->jf;
185 case BPF_JMP|BPF_JGT|BPF_X:
186 pc += (A > X) ? fentry->jt : fentry->jf;
189 case BPF_JMP|BPF_JGE|BPF_X:
190 pc += (A >= X) ? fentry->jt : fentry->jf;
193 case BPF_JMP|BPF_JEQ|BPF_X:
194 pc += (A == X) ? fentry->jt : fentry->jf;
197 case BPF_JMP|BPF_JSET|BPF_X:
198 pc += (A & X) ? fentry->jt : fentry->jf;
201 case BPF_LD|BPF_W|BPF_ABS:
204 if(k >= 0 && (unsigned int)(k+sizeof(u32)) <= len) {
205 A = ntohl(*(u32*)&data[k]);
213 if ((ptr = load_pointer(skb, k)) != NULL) {
214 A = ntohl(*(u32*)ptr);
219 if (!skb_copy_bits(skb, k, &tmp, 4)) {
226 case BPF_LD|BPF_H|BPF_ABS:
229 if(k >= 0 && (unsigned int) (k + sizeof(u16)) <= len) {
230 A = ntohs(*(u16*)&data[k]);
238 if ((ptr = load_pointer(skb, k)) != NULL) {
239 A = ntohs(*(u16*)ptr);
244 if (!skb_copy_bits(skb, k, &tmp, 2)) {
251 case BPF_LD|BPF_B|BPF_ABS:
254 if(k >= 0 && (unsigned int)k < len) {
263 if ((ptr = load_pointer(skb, k)) != NULL) {
269 if (!skb_copy_bits(skb, k, &tmp, 1)) {
276 case BPF_LD|BPF_W|BPF_LEN:
280 case BPF_LDX|BPF_W|BPF_LEN:
284 case BPF_LD|BPF_W|BPF_IND:
288 case BPF_LD|BPF_H|BPF_IND:
292 case BPF_LD|BPF_B|BPF_IND:
296 case BPF_LDX|BPF_B|BPF_MSH:
299 X = (data[fentry->k] & 0xf) << 2;
306 case BPF_LDX|BPF_IMM:
314 case BPF_LDX|BPF_MEM:
318 case BPF_MISC|BPF_TAX:
322 case BPF_MISC|BPF_TXA:
327 return ((unsigned int)fentry->k);
330 return ((unsigned int)A);
341 /* Invalid instruction counts as RET */
345 /* Handle ancillary data, which are impossible
346 (or very difficult) to get parsing packet contents.
348 switch (k-SKF_AD_OFF) {
349 case SKF_AD_PROTOCOL:
350 A = htons(skb->protocol);
356 A = skb->dev->ifindex;
367 * sk_chk_filter - verify socket filter code
368 * @filter: filter to verify
369 * @flen: length of filter
371 * Check the user's filter code. If we let some ugly
372 * filter code slip through kaboom! The filter must contain
373 * no references or jumps that are out of range, no illegal instructions
374 * and no backward jumps. It must end with a RET instruction
376 * Returns 0 if the rule set is legal or a negative errno code if not.
379 int sk_chk_filter(struct sock_filter *filter, int flen)
381 struct sock_filter *ftest;
384 if ((unsigned int) flen >= (~0U / sizeof(struct sock_filter)))
388 * Check the filter code now.
390 for(pc = 0; pc < flen; pc++)
393 * All jumps are forward as they are not signed
397 if(BPF_CLASS(ftest->code) == BPF_JMP)
400 * But they mustn't jump off the end.
402 if(BPF_OP(ftest->code) == BPF_JA)
404 /* Note, the large ftest->k might cause
405 loops. Compare this with conditional
406 jumps below, where offsets are limited. --ANK (981016)
408 if (ftest->k >= (unsigned)(flen-pc-1))
414 * For conditionals both must be safe
416 if(pc + ftest->jt +1 >= flen || pc + ftest->jf +1 >= flen)
422 * Check that memory operations use valid addresses.
425 if (ftest->k >= BPF_MEMWORDS)
428 * But it might not be a memory operation...
430 switch (ftest->code) {
434 case BPF_LDX|BPF_MEM:
441 * The program must end with a return. We don't care where they
442 * jumped within the script (its always forwards) but in the
443 * end they _will_ hit this.
446 return (BPF_CLASS(filter[flen - 1].code) == BPF_RET)?0:-EINVAL;
450 * sk_attach_filter - attach a socket filter
451 * @fprog: the filter program
452 * @sk: the socket to use
454 * Attach the user's filter code. We first run some sanity checks on
455 * it to make sure it does not explode on us later. If an error
456 * occurs or there is insufficient memory for the filter a negative
457 * errno code is returned. On success the return is zero.
460 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
462 struct sk_filter *fp;
463 unsigned int fsize = sizeof(struct sock_filter) * fprog->len;
466 /* Make sure new filter is there and in the right amounts. */
467 if (fprog->filter == NULL || fprog->len > BPF_MAXINSNS)
470 fp = (struct sk_filter *)sock_kmalloc(sk, fsize+sizeof(*fp), GFP_KERNEL);
474 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
475 sock_kfree_s(sk, fp, fsize+sizeof(*fp));
479 atomic_set(&fp->refcnt, 1);
480 fp->len = fprog->len;
482 if ((err = sk_chk_filter(fp->insns, fp->len))==0) {
483 struct sk_filter *old_fp;
485 spin_lock_bh(&sk->lock.slock);
488 spin_unlock_bh(&sk->lock.slock);
493 sk_filter_release(sk, fp);
497 #endif /* CONFIG_FILTER */