2 * Copyright (c) 1990, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from the Stanford/CMU enet packet filter,
6 * (net/enet.c) distributed as part of 4.3BSD, and code contributed
7 * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * @(#)bpf_filter.c 8.1 (Berkeley) 6/10/93
40 * $FreeBSD: src/sys/net/bpf_filter.c,v 1.17 1999/12/29 04:38:31 peter Exp $
41 * $DragonFly: src/sys/net/bpf_filter.c,v 1.10 2008/01/02 12:30:34 sephe Exp $
44 #include <sys/param.h>
46 #if defined(sparc) || defined(mips) || defined(ibm032)
51 #define EXTRACT_SHORT(p) ((u_int16_t)ntohs(*(u_int16_t *)p))
52 #define EXTRACT_LONG(p) (ntohl(*(u_int32_t *)p))
54 #define EXTRACT_SHORT(p)\
56 ((u_int16_t)*((u_char *)p+0)<<8|\
57 (u_int16_t)*((u_char *)p+1)<<0))
58 #define EXTRACT_LONG(p)\
59 ((u_int32_t)*((u_char *)p+0)<<24|\
60 (u_int32_t)*((u_char *)p+1)<<16|\
61 (u_int32_t)*((u_char *)p+2)<<8|\
62 (u_int32_t)*((u_char *)p+3)<<0)
70 #define MINDEX(m, k) \
83 extern int bpf_maxbufsize
;
85 static u_int16_t
m_xhalf (struct mbuf
*m
, bpf_u_int32 k
, int *err
);
86 static u_int32_t
m_xword (struct mbuf
*m
, bpf_u_int32 k
, int *err
);
89 m_xword(struct mbuf
*m
, bpf_u_int32 k
, int *err
)
103 cp
= mtod(m
, u_char
*) + k
;
106 return EXTRACT_LONG(cp
);
109 if (m0
== 0 || m0
->m_len
+ len
- k
< 4)
112 np
= mtod(m0
, u_char
*);
117 ((u_int32_t
)cp
[0] << 24) |
118 ((u_int32_t
)np
[0] << 16) |
119 ((u_int32_t
)np
[1] << 8) |
124 ((u_int32_t
)cp
[0] << 24) |
125 ((u_int32_t
)cp
[1] << 16) |
126 ((u_int32_t
)np
[0] << 8) |
131 ((u_int32_t
)cp
[0] << 24) |
132 ((u_int32_t
)cp
[1] << 16) |
133 ((u_int32_t
)cp
[2] << 8) |
142 m_xhalf(struct mbuf
*m
, bpf_u_int32 k
, int *err
)
156 cp
= mtod(m
, u_char
*) + k
;
159 return EXTRACT_SHORT(cp
);
165 return (cp
[0] << 8) | mtod(m0
, u_char
*)[0];
173 * Execute the filter program starting at pc on the packet p
174 * wirelen is the length of the original packet
175 * buflen is the amount of data present
178 bpf_filter(const struct bpf_insn
*pc
, u_char
*p
, u_int wirelen
, u_int buflen
)
180 u_int32_t A
= 0, X
= 0;
182 int32_t mem
[BPF_MEMWORDS
];
186 * No filter means accept all.
208 case BPF_LD
|BPF_W
|BPF_ABS
:
210 if (k
> buflen
|| sizeof(int32_t) > buflen
- k
) {
216 A
= m_xword((struct mbuf
*)p
, k
, &merr
);
225 if (((intptr_t)(p
+ k
) & 3) != 0)
226 A
= EXTRACT_LONG(&p
[k
]);
229 A
= ntohl(*(int32_t *)(p
+ k
));
232 case BPF_LD
|BPF_H
|BPF_ABS
:
234 if (k
> buflen
|| sizeof(int16_t) > buflen
- k
) {
240 A
= m_xhalf((struct mbuf
*)p
, k
, &merr
);
246 A
= EXTRACT_SHORT(&p
[k
]);
249 case BPF_LD
|BPF_B
|BPF_ABS
:
257 m
= (struct mbuf
*)p
;
259 A
= mtod(m
, u_char
*)[k
];
268 case BPF_LD
|BPF_W
|BPF_LEN
:
272 case BPF_LDX
|BPF_W
|BPF_LEN
:
276 case BPF_LD
|BPF_W
|BPF_IND
:
278 if (pc
->k
> buflen
|| X
> buflen
- pc
->k
||
279 sizeof(int32_t) > buflen
- k
) {
285 A
= m_xword((struct mbuf
*)p
, k
, &merr
);
294 if (((intptr_t)(p
+ k
) & 3) != 0)
295 A
= EXTRACT_LONG(&p
[k
]);
298 A
= ntohl(*(int32_t *)(p
+ k
));
301 case BPF_LD
|BPF_H
|BPF_IND
:
303 if (X
> buflen
|| pc
->k
> buflen
- X
||
304 sizeof(int16_t) > buflen
- k
) {
310 A
= m_xhalf((struct mbuf
*)p
, k
, &merr
);
318 A
= EXTRACT_SHORT(&p
[k
]);
321 case BPF_LD
|BPF_B
|BPF_IND
:
323 if (pc
->k
>= buflen
|| X
>= buflen
- pc
->k
) {
329 m
= (struct mbuf
*)p
;
331 A
= mtod(m
, u_char
*)[k
];
340 case BPF_LDX
|BPF_MSH
|BPF_B
:
348 m
= (struct mbuf
*)p
;
350 X
= (mtod(m
, char *)[k
] & 0xf) << 2;
356 X
= (p
[pc
->k
] & 0xf) << 2;
363 case BPF_LDX
|BPF_IMM
:
371 case BPF_LDX
|BPF_MEM
:
387 case BPF_JMP
|BPF_JGT
|BPF_K
:
388 pc
+= (A
> pc
->k
) ? pc
->jt
: pc
->jf
;
391 case BPF_JMP
|BPF_JGE
|BPF_K
:
392 pc
+= (A
>= pc
->k
) ? pc
->jt
: pc
->jf
;
395 case BPF_JMP
|BPF_JEQ
|BPF_K
:
396 pc
+= (A
== pc
->k
) ? pc
->jt
: pc
->jf
;
399 case BPF_JMP
|BPF_JSET
|BPF_K
:
400 pc
+= (A
& pc
->k
) ? pc
->jt
: pc
->jf
;
403 case BPF_JMP
|BPF_JGT
|BPF_X
:
404 pc
+= (A
> X
) ? pc
->jt
: pc
->jf
;
407 case BPF_JMP
|BPF_JGE
|BPF_X
:
408 pc
+= (A
>= X
) ? pc
->jt
: pc
->jf
;
411 case BPF_JMP
|BPF_JEQ
|BPF_X
:
412 pc
+= (A
== X
) ? pc
->jt
: pc
->jf
;
415 case BPF_JMP
|BPF_JSET
|BPF_X
:
416 pc
+= (A
& X
) ? pc
->jt
: pc
->jf
;
419 case BPF_ALU
|BPF_ADD
|BPF_X
:
423 case BPF_ALU
|BPF_SUB
|BPF_X
:
427 case BPF_ALU
|BPF_MUL
|BPF_X
:
431 case BPF_ALU
|BPF_DIV
|BPF_X
:
437 case BPF_ALU
|BPF_AND
|BPF_X
:
441 case BPF_ALU
|BPF_OR
|BPF_X
:
445 case BPF_ALU
|BPF_LSH
|BPF_X
:
449 case BPF_ALU
|BPF_RSH
|BPF_X
:
453 case BPF_ALU
|BPF_ADD
|BPF_K
:
457 case BPF_ALU
|BPF_SUB
|BPF_K
:
461 case BPF_ALU
|BPF_MUL
|BPF_K
:
465 case BPF_ALU
|BPF_DIV
|BPF_K
:
469 case BPF_ALU
|BPF_AND
|BPF_K
:
473 case BPF_ALU
|BPF_OR
|BPF_K
:
477 case BPF_ALU
|BPF_LSH
|BPF_K
:
481 case BPF_ALU
|BPF_RSH
|BPF_K
:
485 case BPF_ALU
|BPF_NEG
:
489 case BPF_MISC
|BPF_TAX
:
493 case BPF_MISC
|BPF_TXA
:
502 * Return true if the 'fcode' is a valid filter program.
503 * The constraints are that each jump be forward and to a valid
504 * code, that memory accesses are within valid ranges (to the
505 * extent that this can be checked statically; loads of packet
506 * data have to be, and are, also checked at run time), and that
507 * the code terminates with either an accept or reject.
509 * The kernel needs to be able to verify an application's filter code.
510 * Otherwise, a bogus program could easily crash the system.
513 bpf_validate(const struct bpf_insn
*f
, int len
)
516 const struct bpf_insn
*p
;
518 if (len
< 1 || len
> BPF_MAXINSNS
)
521 for (i
= 0; i
< len
; ++i
) {
523 switch (BPF_CLASS(p
->code
)) {
525 * Check that memory operations use valid addresses.
529 switch (BPF_MODE(p
->code
)) {
536 * More strict check with actual packet length
539 if (p
->k
>= bpf_maxbufsize
)
543 if (p
->k
>= BPF_MEMWORDS
)
554 if (p
->k
>= BPF_MEMWORDS
)
558 switch (BPF_OP(p
->code
)) {
570 * Check for constant division by 0.
572 if (BPF_RVAL(p
->code
) == BPF_K
&& p
->k
== 0)
581 * Check that jumps are within the code block,
582 * and that unconditional branches don't go
583 * backwards as a result of an overflow.
584 * Unconditional branches have a 32-bit offset,
585 * so they could overflow; we check to make
586 * sure they don't. Conditional branches have
587 * an 8-bit offset, and the from address is <=
588 * BPF_MAXINSNS, and we assume that BPF_MAXINSNS
589 * is sufficiently small that adding 255 to it
592 * We know that len is <= BPF_MAXINSNS, and we
593 * assume that BPF_MAXINSNS is < the maximum size
594 * of a u_int, so that i + 1 doesn't overflow.
597 switch (BPF_OP(p
->code
)) {
599 if (from
+ p
->k
< from
|| from
+ p
->k
>= len
)
606 if (from
+ p
->jt
>= len
|| from
+ p
->jf
>= len
)
621 return BPF_CLASS(f
[len
- 1].code
) == BPF_RET
;