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:
298 if(k >= 0 && (unsigned int)k >= len)
300 X = (data[k] & 0xf) << 2;
307 case BPF_LDX|BPF_IMM:
315 case BPF_LDX|BPF_MEM:
319 case BPF_MISC|BPF_TAX:
323 case BPF_MISC|BPF_TXA:
328 return ((unsigned int)fentry->k);
331 return ((unsigned int)A);
342 /* Invalid instruction counts as RET */
346 /* Handle ancillary data, which are impossible
347 (or very difficult) to get parsing packet contents.
349 switch (k-SKF_AD_OFF) {
350 case SKF_AD_PROTOCOL:
351 A = htons(skb->protocol);
357 A = skb->dev->ifindex;
368 * sk_chk_filter - verify socket filter code
369 * @filter: filter to verify
370 * @flen: length of filter
372 * Check the user's filter code. If we let some ugly
373 * filter code slip through kaboom! The filter must contain
374 * no references or jumps that are out of range, no illegal instructions
375 * and no backward jumps. It must end with a RET instruction
377 * Returns 0 if the rule set is legal or a negative errno code if not.
380 int sk_chk_filter(struct sock_filter *filter, int flen)
382 struct sock_filter *ftest;
385 if ((unsigned int) flen >= (~0U / sizeof(struct sock_filter)))
389 * Check the filter code now.
391 for(pc = 0; pc < flen; pc++)
394 * All jumps are forward as they are not signed
398 if(BPF_CLASS(ftest->code) == BPF_JMP)
401 * But they mustn't jump off the end.
403 if(BPF_OP(ftest->code) == BPF_JA)
405 /* Note, the large ftest->k might cause
406 loops. Compare this with conditional
407 jumps below, where offsets are limited. --ANK (981016)
409 if (ftest->k >= (unsigned)(flen-pc-1))
415 * For conditionals both must be safe
417 if(pc + ftest->jt +1 >= flen || pc + ftest->jf +1 >= flen)
423 * Check that memory operations use valid addresses.
426 if (ftest->k >= BPF_MEMWORDS)
429 * But it might not be a memory operation...
431 switch (ftest->code) {
435 case BPF_LDX|BPF_MEM:
442 * The program must end with a return. We don't care where they
443 * jumped within the script (its always forwards) but in the
444 * end they _will_ hit this.
447 return (BPF_CLASS(filter[flen - 1].code) == BPF_RET)?0:-EINVAL;
451 * sk_attach_filter - attach a socket filter
452 * @fprog: the filter program
453 * @sk: the socket to use
455 * Attach the user's filter code. We first run some sanity checks on
456 * it to make sure it does not explode on us later. If an error
457 * occurs or there is insufficient memory for the filter a negative
458 * errno code is returned. On success the return is zero.
461 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
463 struct sk_filter *fp;
464 unsigned int fsize = sizeof(struct sock_filter) * fprog->len;
467 /* Make sure new filter is there and in the right amounts. */
468 if (fprog->filter == NULL || fprog->len > BPF_MAXINSNS)
471 fp = (struct sk_filter *)sock_kmalloc(sk, fsize+sizeof(*fp), GFP_KERNEL);
475 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
476 sock_kfree_s(sk, fp, fsize+sizeof(*fp));
480 atomic_set(&fp->refcnt, 1);
481 fp->len = fprog->len;
483 if ((err = sk_chk_filter(fp->insns, fp->len))==0) {
484 struct sk_filter *old_fp;
486 spin_lock_bh(&sk->lock.slock);
489 spin_unlock_bh(&sk->lock.slock);
494 sk_filter_release(sk, fp);
498 #endif /* CONFIG_FILTER */