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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)bpf_filter.c 8.1 (Berkeley) 6/10/93
36 * $FreeBSD: src/sys/net/bpf_filter.c,v 1.17 1999/12/29 04:38:31 peter Exp $
39 #include <sys/systm.h>
40 #include <sys/param.h>
42 #if defined(sparc) || defined(mips) || defined(ibm032)
47 #define EXTRACT_SHORT(p) ((u_int16_t)ntohs(*(u_int16_t *)p))
48 #define EXTRACT_LONG(p) (ntohl(*(u_int32_t *)p))
50 #define EXTRACT_SHORT(p)\
52 ((u_int16_t)*((u_char *)p+0)<<8|\
53 (u_int16_t)*((u_char *)p+1)<<0))
54 #define EXTRACT_LONG(p)\
55 ((u_int32_t)*((u_char *)p+0)<<24|\
56 (u_int32_t)*((u_char *)p+1)<<16|\
57 (u_int32_t)*((u_char *)p+2)<<8|\
58 (u_int32_t)*((u_char *)p+3)<<0)
66 #define MINDEX(m, k) \
79 extern int bpf_maxbufsize
;
81 static u_int16_t
m_xhalf (struct mbuf
*m
, bpf_u_int32 k
, int *err
);
82 static u_int32_t
m_xword (struct mbuf
*m
, bpf_u_int32 k
, int *err
);
85 m_xword(struct mbuf
*m
, bpf_u_int32 k
, int *err
)
99 cp
= mtod(m
, u_char
*) + k
;
102 return EXTRACT_LONG(cp
);
105 if (m0
== NULL
|| m0
->m_len
+ len
- k
< 4)
108 np
= mtod(m0
, u_char
*);
113 ((u_int32_t
)cp
[0] << 24) |
114 ((u_int32_t
)np
[0] << 16) |
115 ((u_int32_t
)np
[1] << 8) |
120 ((u_int32_t
)cp
[0] << 24) |
121 ((u_int32_t
)cp
[1] << 16) |
122 ((u_int32_t
)np
[0] << 8) |
127 ((u_int32_t
)cp
[0] << 24) |
128 ((u_int32_t
)cp
[1] << 16) |
129 ((u_int32_t
)cp
[2] << 8) |
138 m_xhalf(struct mbuf
*m
, bpf_u_int32 k
, int *err
)
152 cp
= mtod(m
, u_char
*) + k
;
155 return EXTRACT_SHORT(cp
);
161 return (cp
[0] << 8) | mtod(m0
, u_char
*)[0];
169 * Execute the filter program starting at pc on the packet p
170 * wirelen is the length of the original packet
171 * buflen is the amount of data present
174 bpf_filter(const struct bpf_insn
*pc
, u_char
*p
, u_int wirelen
, u_int buflen
)
176 u_int32_t A
= 0, X
= 0;
178 u_int32_t mem
[BPF_MEMWORDS
];
180 bzero(mem
, sizeof(mem
));
184 * No filter means accept all.
206 case BPF_LD
|BPF_W
|BPF_ABS
:
208 if (k
> buflen
|| sizeof(int32_t) > buflen
- k
) {
214 A
= m_xword((struct mbuf
*)p
, k
, &merr
);
223 if (((intptr_t)(p
+ k
) & 3) != 0)
224 A
= EXTRACT_LONG(&p
[k
]);
227 A
= ntohl(*(int32_t *)(p
+ k
));
230 case BPF_LD
|BPF_H
|BPF_ABS
:
232 if (k
> buflen
|| sizeof(int16_t) > buflen
- k
) {
238 A
= m_xhalf((struct mbuf
*)p
, k
, &merr
);
244 A
= EXTRACT_SHORT(&p
[k
]);
247 case BPF_LD
|BPF_B
|BPF_ABS
:
255 m
= (struct mbuf
*)p
;
257 A
= mtod(m
, u_char
*)[k
];
266 case BPF_LD
|BPF_W
|BPF_LEN
:
270 case BPF_LDX
|BPF_W
|BPF_LEN
:
274 case BPF_LD
|BPF_W
|BPF_IND
:
276 if (pc
->k
> buflen
|| X
> buflen
- pc
->k
||
277 sizeof(int32_t) > buflen
- k
) {
283 A
= m_xword((struct mbuf
*)p
, k
, &merr
);
292 if (((intptr_t)(p
+ k
) & 3) != 0)
293 A
= EXTRACT_LONG(&p
[k
]);
296 A
= ntohl(*(int32_t *)(p
+ k
));
299 case BPF_LD
|BPF_H
|BPF_IND
:
301 if (X
> buflen
|| pc
->k
> buflen
- X
||
302 sizeof(int16_t) > buflen
- k
) {
308 A
= m_xhalf((struct mbuf
*)p
, k
, &merr
);
316 A
= EXTRACT_SHORT(&p
[k
]);
319 case BPF_LD
|BPF_B
|BPF_IND
:
321 if (pc
->k
>= buflen
|| X
>= buflen
- pc
->k
) {
327 m
= (struct mbuf
*)p
;
329 A
= mtod(m
, u_char
*)[k
];
338 case BPF_LDX
|BPF_MSH
|BPF_B
:
346 m
= (struct mbuf
*)p
;
348 X
= (mtod(m
, char *)[k
] & 0xf) << 2;
354 X
= (p
[pc
->k
] & 0xf) << 2;
361 case BPF_LDX
|BPF_IMM
:
369 case BPF_LDX
|BPF_MEM
:
385 case BPF_JMP
|BPF_JGT
|BPF_K
:
386 pc
+= (A
> pc
->k
) ? pc
->jt
: pc
->jf
;
389 case BPF_JMP
|BPF_JGE
|BPF_K
:
390 pc
+= (A
>= pc
->k
) ? pc
->jt
: pc
->jf
;
393 case BPF_JMP
|BPF_JEQ
|BPF_K
:
394 pc
+= (A
== pc
->k
) ? pc
->jt
: pc
->jf
;
397 case BPF_JMP
|BPF_JSET
|BPF_K
:
398 pc
+= (A
& pc
->k
) ? pc
->jt
: pc
->jf
;
401 case BPF_JMP
|BPF_JGT
|BPF_X
:
402 pc
+= (A
> X
) ? pc
->jt
: pc
->jf
;
405 case BPF_JMP
|BPF_JGE
|BPF_X
:
406 pc
+= (A
>= X
) ? pc
->jt
: pc
->jf
;
409 case BPF_JMP
|BPF_JEQ
|BPF_X
:
410 pc
+= (A
== X
) ? pc
->jt
: pc
->jf
;
413 case BPF_JMP
|BPF_JSET
|BPF_X
:
414 pc
+= (A
& X
) ? pc
->jt
: pc
->jf
;
417 case BPF_ALU
|BPF_ADD
|BPF_X
:
421 case BPF_ALU
|BPF_SUB
|BPF_X
:
425 case BPF_ALU
|BPF_MUL
|BPF_X
:
429 case BPF_ALU
|BPF_DIV
|BPF_X
:
435 case BPF_ALU
|BPF_AND
|BPF_X
:
439 case BPF_ALU
|BPF_OR
|BPF_X
:
443 case BPF_ALU
|BPF_LSH
|BPF_X
:
447 case BPF_ALU
|BPF_RSH
|BPF_X
:
451 case BPF_ALU
|BPF_ADD
|BPF_K
:
455 case BPF_ALU
|BPF_SUB
|BPF_K
:
459 case BPF_ALU
|BPF_MUL
|BPF_K
:
463 case BPF_ALU
|BPF_DIV
|BPF_K
:
467 case BPF_ALU
|BPF_AND
|BPF_K
:
471 case BPF_ALU
|BPF_OR
|BPF_K
:
475 case BPF_ALU
|BPF_LSH
|BPF_K
:
479 case BPF_ALU
|BPF_RSH
|BPF_K
:
483 case BPF_ALU
|BPF_NEG
:
487 case BPF_MISC
|BPF_TAX
:
491 case BPF_MISC
|BPF_TXA
:
500 * Return true if the 'fcode' is a valid filter program.
501 * The constraints are that each jump be forward and to a valid
502 * code, that memory accesses are within valid ranges (to the
503 * extent that this can be checked statically; loads of packet
504 * data have to be, and are, also checked at run time), and that
505 * the code terminates with either an accept or reject.
507 * The kernel needs to be able to verify an application's filter code.
508 * Otherwise, a bogus program could easily crash the system.
511 bpf_validate(const struct bpf_insn
*f
, int len
)
514 const struct bpf_insn
*p
;
516 if (len
< 1 || len
> BPF_MAXINSNS
)
519 for (i
= 0; i
< len
; ++i
) {
521 switch (BPF_CLASS(p
->code
)) {
523 * Check that memory operations use valid addresses.
527 switch (BPF_MODE(p
->code
)) {
534 * More strict check with actual packet length
537 if (p
->k
>= bpf_maxbufsize
)
541 if (p
->k
>= BPF_MEMWORDS
)
552 if (p
->k
>= BPF_MEMWORDS
)
556 switch (BPF_OP(p
->code
)) {
568 * Check for constant division by 0.
570 if (BPF_SRC(p
->code
) == BPF_K
&& p
->k
== 0)
579 * Check that jumps are within the code block,
580 * and that unconditional branches don't go
581 * backwards as a result of an overflow.
582 * Unconditional branches have a 32-bit offset,
583 * so they could overflow; we check to make
584 * sure they don't. Conditional branches have
585 * an 8-bit offset, and the from address is <=
586 * BPF_MAXINSNS, and we assume that BPF_MAXINSNS
587 * is sufficiently small that adding 255 to it
590 * We know that len is <= BPF_MAXINSNS, and we
591 * assume that BPF_MAXINSNS is < the maximum size
592 * of a u_int, so that i + 1 doesn't overflow.
595 switch (BPF_OP(p
->code
)) {
597 if (from
+ p
->k
< from
|| from
+ p
->k
>= len
)
604 if (from
+ p
->jt
>= len
|| from
+ p
->jf
>= len
)
619 return BPF_CLASS(f
[len
- 1].code
) == BPF_RET
;