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/module.h>
19 #include <linux/types.h>
20 #include <linux/sched.h>
22 #include <linux/fcntl.h>
23 #include <linux/socket.h>
25 #include <linux/inet.h>
26 #include <linux/netdevice.h>
27 #include <linux/if_packet.h>
29 #include <net/protocol.h>
30 #include <linux/skbuff.h>
32 #include <linux/errno.h>
33 #include <linux/timer.h>
34 #include <asm/system.h>
35 #include <asm/uaccess.h>
36 #include <linux/filter.h>
38 /* No hurry in this branch */
39 static void *__load_pointer(struct sk_buff
*skb
, int k
)
44 ptr
= skb
->nh
.raw
+ k
- SKF_NET_OFF
;
45 else if (k
>= SKF_LL_OFF
)
46 ptr
= skb
->mac
.raw
+ k
- SKF_LL_OFF
;
48 if (ptr
>= skb
->head
&& ptr
< skb
->tail
)
53 static inline void *load_pointer(struct sk_buff
*skb
, int k
,
54 unsigned int size
, void *buffer
)
57 return skb_header_pointer(skb
, k
, size
, buffer
);
61 return __load_pointer(skb
, k
);
66 * sk_run_filter - run a filter on a socket
67 * @skb: buffer to run the filter on
68 * @filter: filter to apply
69 * @flen: length of filter
71 * Decode and apply filter instructions to the skb->data.
72 * Return length to keep, 0 for none. skb is the data we are
73 * filtering, filter is the array of filter instructions, and
74 * len is the number of filter blocks in the array.
77 int sk_run_filter(struct sk_buff
*skb
, struct sock_filter
*filter
, int flen
)
79 struct sock_filter
*fentry
; /* We walk down these */
81 u32 A
= 0; /* Accumulator */
82 u32 X
= 0; /* Index Register */
83 u32 mem
[BPF_MEMWORDS
]; /* Scratch Memory Store */
89 * Process array of filter instructions.
91 for (pc
= 0; pc
< flen
; pc
++) {
94 switch (fentry
->code
) {
95 case BPF_ALU
|BPF_ADD
|BPF_X
:
98 case BPF_ALU
|BPF_ADD
|BPF_K
:
101 case BPF_ALU
|BPF_SUB
|BPF_X
:
104 case BPF_ALU
|BPF_SUB
|BPF_K
:
107 case BPF_ALU
|BPF_MUL
|BPF_X
:
110 case BPF_ALU
|BPF_MUL
|BPF_K
:
113 case BPF_ALU
|BPF_DIV
|BPF_X
:
118 case BPF_ALU
|BPF_DIV
|BPF_K
:
123 case BPF_ALU
|BPF_AND
|BPF_X
:
126 case BPF_ALU
|BPF_AND
|BPF_K
:
129 case BPF_ALU
|BPF_OR
|BPF_X
:
132 case BPF_ALU
|BPF_OR
|BPF_K
:
135 case BPF_ALU
|BPF_LSH
|BPF_X
:
138 case BPF_ALU
|BPF_LSH
|BPF_K
:
141 case BPF_ALU
|BPF_RSH
|BPF_X
:
144 case BPF_ALU
|BPF_RSH
|BPF_K
:
147 case BPF_ALU
|BPF_NEG
:
153 case BPF_JMP
|BPF_JGT
|BPF_K
:
154 pc
+= (A
> fentry
->k
) ? fentry
->jt
: fentry
->jf
;
156 case BPF_JMP
|BPF_JGE
|BPF_K
:
157 pc
+= (A
>= fentry
->k
) ? fentry
->jt
: fentry
->jf
;
159 case BPF_JMP
|BPF_JEQ
|BPF_K
:
160 pc
+= (A
== fentry
->k
) ? fentry
->jt
: fentry
->jf
;
162 case BPF_JMP
|BPF_JSET
|BPF_K
:
163 pc
+= (A
& fentry
->k
) ? fentry
->jt
: fentry
->jf
;
165 case BPF_JMP
|BPF_JGT
|BPF_X
:
166 pc
+= (A
> X
) ? fentry
->jt
: fentry
->jf
;
168 case BPF_JMP
|BPF_JGE
|BPF_X
:
169 pc
+= (A
>= X
) ? fentry
->jt
: fentry
->jf
;
171 case BPF_JMP
|BPF_JEQ
|BPF_X
:
172 pc
+= (A
== X
) ? fentry
->jt
: fentry
->jf
;
174 case BPF_JMP
|BPF_JSET
|BPF_X
:
175 pc
+= (A
& X
) ? fentry
->jt
: fentry
->jf
;
177 case BPF_LD
|BPF_W
|BPF_ABS
:
180 ptr
= load_pointer(skb
, k
, 4, &tmp
);
182 A
= ntohl(*(u32
*)ptr
);
186 case BPF_LD
|BPF_H
|BPF_ABS
:
189 ptr
= load_pointer(skb
, k
, 2, &tmp
);
191 A
= ntohs(*(u16
*)ptr
);
195 case BPF_LD
|BPF_B
|BPF_ABS
:
198 ptr
= load_pointer(skb
, k
, 1, &tmp
);
204 case BPF_LD
|BPF_W
|BPF_LEN
:
207 case BPF_LDX
|BPF_W
|BPF_LEN
:
210 case BPF_LD
|BPF_W
|BPF_IND
:
213 case BPF_LD
|BPF_H
|BPF_IND
:
216 case BPF_LD
|BPF_B
|BPF_IND
:
219 case BPF_LDX
|BPF_B
|BPF_MSH
:
220 ptr
= load_pointer(skb
, fentry
->k
, 1, &tmp
);
222 X
= (*(u8
*)ptr
& 0xf) << 2;
229 case BPF_LDX
|BPF_IMM
:
235 case BPF_LDX
|BPF_MEM
:
238 case BPF_MISC
|BPF_TAX
:
241 case BPF_MISC
|BPF_TXA
:
245 return ((unsigned int)fentry
->k
);
247 return ((unsigned int)A
);
255 /* Invalid instruction counts as RET */
260 * Handle ancillary data, which are impossible
261 * (or very difficult) to get parsing packet contents.
263 switch (k
-SKF_AD_OFF
) {
264 case SKF_AD_PROTOCOL
:
265 A
= htons(skb
->protocol
);
271 A
= skb
->dev
->ifindex
;
282 * sk_chk_filter - verify socket filter code
283 * @filter: filter to verify
284 * @flen: length of filter
286 * Check the user's filter code. If we let some ugly
287 * filter code slip through kaboom! The filter must contain
288 * no references or jumps that are out of range, no illegal instructions
289 * and no backward jumps. It must end with a RET instruction
291 * Returns 0 if the rule set is legal or a negative errno code if not.
293 int sk_chk_filter(struct sock_filter
*filter
, int flen
)
295 struct sock_filter
*ftest
;
298 if (((unsigned int)flen
>= (~0U / sizeof(struct sock_filter
))) || flen
== 0)
301 /* check the filter code now */
302 for (pc
= 0; pc
< flen
; pc
++) {
303 /* all jumps are forward as they are not signed */
305 if (BPF_CLASS(ftest
->code
) == BPF_JMP
) {
306 /* but they mustn't jump off the end */
307 if (BPF_OP(ftest
->code
) == BPF_JA
) {
309 * Note, the large ftest->k might cause loops.
310 * Compare this with conditional jumps below,
311 * where offsets are limited. --ANK (981016)
313 if (ftest
->k
>= (unsigned)(flen
-pc
-1))
316 /* for conditionals both must be safe */
317 if (pc
+ ftest
->jt
+1 >= flen
||
318 pc
+ ftest
->jf
+1 >= flen
)
323 /* check that memory operations use valid addresses. */
324 if (ftest
->k
>= BPF_MEMWORDS
) {
325 /* but it might not be a memory operation... */
326 switch (ftest
->code
) {
330 case BPF_LDX
|BPF_MEM
:
337 * The program must end with a return. We don't care where they
338 * jumped within the script (its always forwards) but in the end
339 * they _will_ hit this.
341 return (BPF_CLASS(filter
[flen
- 1].code
) == BPF_RET
) ? 0 : -EINVAL
;
345 * sk_attach_filter - attach a socket filter
346 * @fprog: the filter program
347 * @sk: the socket to use
349 * Attach the user's filter code. We first run some sanity checks on
350 * it to make sure it does not explode on us later. If an error
351 * occurs or there is insufficient memory for the filter a negative
352 * errno code is returned. On success the return is zero.
354 int sk_attach_filter(struct sock_fprog
*fprog
, struct sock
*sk
)
356 struct sk_filter
*fp
;
357 unsigned int fsize
= sizeof(struct sock_filter
) * fprog
->len
;
360 /* Make sure new filter is there and in the right amounts. */
361 if (fprog
->filter
== NULL
|| fprog
->len
> BPF_MAXINSNS
)
364 fp
= sock_kmalloc(sk
, fsize
+sizeof(*fp
), GFP_KERNEL
);
367 if (copy_from_user(fp
->insns
, fprog
->filter
, fsize
)) {
368 sock_kfree_s(sk
, fp
, fsize
+sizeof(*fp
));
372 atomic_set(&fp
->refcnt
, 1);
373 fp
->len
= fprog
->len
;
375 err
= sk_chk_filter(fp
->insns
, fp
->len
);
377 struct sk_filter
*old_fp
;
379 spin_lock_bh(&sk
->sk_lock
.slock
);
380 old_fp
= sk
->sk_filter
;
382 spin_unlock_bh(&sk
->sk_lock
.slock
);
387 sk_filter_release(sk
, fp
);
391 EXPORT_SYMBOL(sk_chk_filter
);
392 EXPORT_SYMBOL(sk_run_filter
);