]>
git.saurik.com Git - apple/xnu.git/blob - bsd/net/bpf_filter.c
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 * Copyright (c) 1990, 1991, 1993
25 * The Regents of the University of California. All rights reserved.
27 * This code is derived from the Stanford/CMU enet packet filter,
28 * (net/enet.c) distributed as part of 4.3BSD, and code contributed
29 * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
30 * Berkeley Laboratory.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)bpf_filter.c 8.1 (Berkeley) 6/10/93
62 * $FreeBSD: src/sys/net/bpf_filter.c,v 1.17 1999/12/29 04:38:31 peter Exp $
65 #include <sys/param.h>
68 #include <netinet/in.h>
71 #if defined(sparc) || defined(mips) || defined(ibm032) || defined(__alpha__)
76 #define EXTRACT_SHORT(p) ((u_int16_t)ntohs(*(u_int16_t *)p))
77 #define EXTRACT_LONG(p) (ntohl(*(u_int32_t *)p))
79 #define EXTRACT_SHORT(p)\
81 ((u_int16_t)*((u_char *)p+0)<<8|\
82 (u_int16_t)*((u_char *)p+1)<<0))
83 #define EXTRACT_LONG(p)\
84 ((u_int32_t)*((u_char *)p+0)<<24|\
85 (u_int32_t)*((u_char *)p+1)<<16|\
86 (u_int32_t)*((u_char *)p+2)<<8|\
87 (u_int32_t)*((u_char *)p+3)<<0)
95 #define MINDEX(m, k) \
97 register unsigned int len = m->m_len; \
108 static u_int16_t
m_xhalf(struct mbuf
*m
, bpf_u_int32 k
, int *err
);
109 static u_int32_t
m_xword(struct mbuf
*m
, bpf_u_int32 k
, int *err
);
112 m_xword(struct mbuf
*m
, bpf_u_int32 k
, int *err
)
115 register u_char
*cp
, *np
;
116 register struct mbuf
*m0
;
126 cp
= mtod(m
, u_char
*) + k
;
129 return EXTRACT_LONG(cp
);
132 if (m0
== 0 || m0
->m_len
+ len
- k
< 4)
135 np
= mtod(m0
, u_char
*);
140 ((u_int32_t
)cp
[0] << 24) |
141 ((u_int32_t
)np
[0] << 16) |
142 ((u_int32_t
)np
[1] << 8) |
147 ((u_int32_t
)cp
[0] << 24) |
148 ((u_int32_t
)cp
[1] << 16) |
149 ((u_int32_t
)np
[0] << 8) |
154 ((u_int32_t
)cp
[0] << 24) |
155 ((u_int32_t
)cp
[1] << 16) |
156 ((u_int32_t
)cp
[2] << 8) |
165 m_xhalf(struct mbuf
*m
, bpf_u_int32 k
, int *err
)
169 register struct mbuf
*m0
;
179 cp
= mtod(m
, u_char
*) + k
;
182 return EXTRACT_SHORT(cp
);
188 return (cp
[0] << 8) | mtod(m0
, u_char
*)[0];
196 * Execute the filter program starting at pc on the packet p
197 * wirelen is the length of the original packet
198 * buflen is the amount of data present
201 bpf_filter(const struct bpf_insn
*pc
, u_char
*p
, u_int wirelen
, u_int buflen
)
203 register u_int32_t A
= 0, X
= 0;
204 register bpf_u_int32 k
;
205 int32_t mem
[BPF_MEMWORDS
];
209 * No filter means accept all.
230 case BPF_LD
|BPF_W
|BPF_ABS
:
232 if (k
> buflen
|| sizeof(int32_t) > buflen
- k
) {
238 A
= m_xword((struct mbuf
*)p
, k
, &merr
);
247 if (((intptr_t)(p
+ k
) & 3) != 0)
248 A
= EXTRACT_LONG(&p
[k
]);
251 A
= ntohl(*(int32_t *)(p
+ k
));
254 case BPF_LD
|BPF_H
|BPF_ABS
:
256 if (k
> buflen
|| sizeof(int16_t) > buflen
- k
) {
262 A
= m_xhalf((struct mbuf
*)p
, k
, &merr
);
268 A
= EXTRACT_SHORT(&p
[k
]);
271 case BPF_LD
|BPF_B
|BPF_ABS
:
275 register struct mbuf
*m
;
279 m
= (struct mbuf
*)p
;
281 A
= mtod(m
, u_char
*)[k
];
290 case BPF_LD
|BPF_W
|BPF_LEN
:
294 case BPF_LDX
|BPF_W
|BPF_LEN
:
298 case BPF_LD
|BPF_W
|BPF_IND
:
300 if (pc
->k
> buflen
|| X
> buflen
- pc
->k
||
301 sizeof(int32_t) > buflen
- k
) {
307 A
= m_xword((struct mbuf
*)p
, k
, &merr
);
316 if (((intptr_t)(p
+ k
) & 3) != 0)
317 A
= EXTRACT_LONG(&p
[k
]);
320 A
= ntohl(*(int32_t *)(p
+ k
));
323 case BPF_LD
|BPF_H
|BPF_IND
:
325 if (X
> buflen
|| pc
->k
> buflen
- X
||
326 sizeof(int16_t) > buflen
- k
) {
332 A
= m_xhalf((struct mbuf
*)p
, k
, &merr
);
340 A
= EXTRACT_SHORT(&p
[k
]);
343 case BPF_LD
|BPF_B
|BPF_IND
:
345 if (pc
->k
>= buflen
|| X
>= buflen
- pc
->k
) {
347 register struct mbuf
*m
;
351 m
= (struct mbuf
*)p
;
353 A
= mtod(m
, char *)[k
];
362 case BPF_LDX
|BPF_MSH
|BPF_B
:
366 register struct mbuf
*m
;
370 m
= (struct mbuf
*)p
;
372 X
= (mtod(m
, char *)[k
] & 0xf) << 2;
378 X
= (p
[pc
->k
] & 0xf) << 2;
385 case BPF_LDX
|BPF_IMM
:
393 case BPF_LDX
|BPF_MEM
:
409 case BPF_JMP
|BPF_JGT
|BPF_K
:
410 pc
+= (A
> pc
->k
) ? pc
->jt
: pc
->jf
;
413 case BPF_JMP
|BPF_JGE
|BPF_K
:
414 pc
+= (A
>= pc
->k
) ? pc
->jt
: pc
->jf
;
417 case BPF_JMP
|BPF_JEQ
|BPF_K
:
418 pc
+= (A
== pc
->k
) ? pc
->jt
: pc
->jf
;
421 case BPF_JMP
|BPF_JSET
|BPF_K
:
422 pc
+= (A
& pc
->k
) ? pc
->jt
: pc
->jf
;
425 case BPF_JMP
|BPF_JGT
|BPF_X
:
426 pc
+= (A
> X
) ? pc
->jt
: pc
->jf
;
429 case BPF_JMP
|BPF_JGE
|BPF_X
:
430 pc
+= (A
>= X
) ? pc
->jt
: pc
->jf
;
433 case BPF_JMP
|BPF_JEQ
|BPF_X
:
434 pc
+= (A
== X
) ? pc
->jt
: pc
->jf
;
437 case BPF_JMP
|BPF_JSET
|BPF_X
:
438 pc
+= (A
& X
) ? pc
->jt
: pc
->jf
;
441 case BPF_ALU
|BPF_ADD
|BPF_X
:
445 case BPF_ALU
|BPF_SUB
|BPF_X
:
449 case BPF_ALU
|BPF_MUL
|BPF_X
:
453 case BPF_ALU
|BPF_DIV
|BPF_X
:
459 case BPF_ALU
|BPF_AND
|BPF_X
:
463 case BPF_ALU
|BPF_OR
|BPF_X
:
467 case BPF_ALU
|BPF_LSH
|BPF_X
:
471 case BPF_ALU
|BPF_RSH
|BPF_X
:
475 case BPF_ALU
|BPF_ADD
|BPF_K
:
479 case BPF_ALU
|BPF_SUB
|BPF_K
:
483 case BPF_ALU
|BPF_MUL
|BPF_K
:
487 case BPF_ALU
|BPF_DIV
|BPF_K
:
491 case BPF_ALU
|BPF_AND
|BPF_K
:
495 case BPF_ALU
|BPF_OR
|BPF_K
:
499 case BPF_ALU
|BPF_LSH
|BPF_K
:
503 case BPF_ALU
|BPF_RSH
|BPF_K
:
507 case BPF_ALU
|BPF_NEG
:
511 case BPF_MISC
|BPF_TAX
:
515 case BPF_MISC
|BPF_TXA
:
524 * Return true if the 'fcode' is a valid filter program.
525 * The constraints are that each jump be forward and to a valid
526 * code. The code must terminate with either an accept or reject.
527 * 'valid' is an array for use by the routine (it must be at least
530 * The kernel needs to be able to verify an application's filter code.
531 * Otherwise, a bogus program could easily crash the system.
534 bpf_validate(const struct bpf_insn
*f
, int len
)
537 const struct bpf_insn
*p
;
539 for (i
= 0; i
< len
; ++i
) {
541 * Check that that jumps are forward, and within
545 if (BPF_CLASS(p
->code
) == BPF_JMP
) {
546 register int from
= i
+ 1;
548 if (BPF_OP(p
->code
) == BPF_JA
) {
549 if (from
>= len
|| p
->k
>= (bpf_u_int32
)(len
- from
))
552 else if (from
>= len
|| p
->jt
>= len
- from
||
557 * Check that memory operations use valid addresses.
559 if ((BPF_CLASS(p
->code
) == BPF_ST
||
560 (BPF_CLASS(p
->code
) == BPF_LD
&&
561 (p
->code
& 0xe0) == BPF_MEM
)) &&
562 p
->k
>= BPF_MEMWORDS
)
565 * Check for constant division by 0.
567 if (p
->code
== (BPF_ALU
|BPF_DIV
|BPF_K
) && p
->k
== 0)
570 return BPF_CLASS(f
[len
- 1].code
) == BPF_RET
;