]>
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 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1990, 1991, 1993
27 * The Regents of the University of California. All rights reserved.
29 * This code is derived from the Stanford/CMU enet packet filter,
30 * (net/enet.c) distributed as part of 4.3BSD, and code contributed
31 * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
32 * Berkeley Laboratory.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 * This product includes software developed by the University of
45 * California, Berkeley and its contributors.
46 * 4. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * @(#)bpf_filter.c 8.1 (Berkeley) 6/10/93
64 * $FreeBSD: src/sys/net/bpf_filter.c,v 1.17 1999/12/29 04:38:31 peter Exp $
67 #include <sys/param.h>
70 #include <netinet/in.h>
73 #if defined(sparc) || defined(mips) || defined(ibm032) || defined(__alpha__)
78 #define EXTRACT_SHORT(p) ((u_int16_t)ntohs(*(u_int16_t *)p))
79 #define EXTRACT_LONG(p) (ntohl(*(u_int32_t *)p))
81 #define EXTRACT_SHORT(p)\
83 ((u_int16_t)*((u_char *)p+0)<<8|\
84 (u_int16_t)*((u_char *)p+1)<<0))
85 #define EXTRACT_LONG(p)\
86 ((u_int32_t)*((u_char *)p+0)<<24|\
87 (u_int32_t)*((u_char *)p+1)<<16|\
88 (u_int32_t)*((u_char *)p+2)<<8|\
89 (u_int32_t)*((u_char *)p+3)<<0)
97 #define MINDEX(m, k) \
99 register int len = m->m_len; \
110 static u_int16_t m_xhalf
__P((struct mbuf
*m
, bpf_u_int32 k
, int *err
));
111 static u_int32_t m_xword
__P((struct mbuf
*m
, bpf_u_int32 k
, int *err
));
115 register struct mbuf
*m
;
116 register bpf_u_int32 k
;
120 register u_char
*cp
, *np
;
121 register struct mbuf
*m0
;
131 cp
= mtod(m
, u_char
*) + k
;
134 return EXTRACT_LONG(cp
);
137 if (m0
== 0 || m0
->m_len
+ len
- k
< 4)
140 np
= mtod(m0
, u_char
*);
145 ((u_int32_t
)cp
[0] << 24) |
146 ((u_int32_t
)np
[0] << 16) |
147 ((u_int32_t
)np
[1] << 8) |
152 ((u_int32_t
)cp
[0] << 24) |
153 ((u_int32_t
)cp
[1] << 16) |
154 ((u_int32_t
)np
[0] << 8) |
159 ((u_int32_t
)cp
[0] << 24) |
160 ((u_int32_t
)cp
[1] << 16) |
161 ((u_int32_t
)cp
[2] << 8) |
171 register struct mbuf
*m
;
172 register bpf_u_int32 k
;
177 register struct mbuf
*m0
;
187 cp
= mtod(m
, u_char
*) + k
;
190 return EXTRACT_SHORT(cp
);
196 return (cp
[0] << 8) | mtod(m0
, u_char
*)[0];
204 * Execute the filter program starting at pc on the packet p
205 * wirelen is the length of the original packet
206 * buflen is the amount of data present
209 bpf_filter(pc
, p
, wirelen
, buflen
)
210 register const struct bpf_insn
*pc
;
213 register u_int buflen
;
215 register u_int32_t A
= 0, X
= 0;
216 register bpf_u_int32 k
;
217 int32_t mem
[BPF_MEMWORDS
];
221 * No filter means accept all.
242 case BPF_LD
|BPF_W
|BPF_ABS
:
244 if (k
> buflen
|| sizeof(int32_t) > buflen
- k
) {
250 A
= m_xword((struct mbuf
*)p
, k
, &merr
);
259 if (((intptr_t)(p
+ k
) & 3) != 0)
260 A
= EXTRACT_LONG(&p
[k
]);
263 A
= ntohl(*(int32_t *)(p
+ k
));
266 case BPF_LD
|BPF_H
|BPF_ABS
:
268 if (k
> buflen
|| sizeof(int16_t) > buflen
- k
) {
274 A
= m_xhalf((struct mbuf
*)p
, k
, &merr
);
280 A
= EXTRACT_SHORT(&p
[k
]);
283 case BPF_LD
|BPF_B
|BPF_ABS
:
287 register struct mbuf
*m
;
291 m
= (struct mbuf
*)p
;
293 A
= mtod(m
, u_char
*)[k
];
302 case BPF_LD
|BPF_W
|BPF_LEN
:
306 case BPF_LDX
|BPF_W
|BPF_LEN
:
310 case BPF_LD
|BPF_W
|BPF_IND
:
312 if (pc
->k
> buflen
|| X
> buflen
- pc
->k
||
313 sizeof(int32_t) > buflen
- k
) {
319 A
= m_xword((struct mbuf
*)p
, k
, &merr
);
328 if (((intptr_t)(p
+ k
) & 3) != 0)
329 A
= EXTRACT_LONG(&p
[k
]);
332 A
= ntohl(*(int32_t *)(p
+ k
));
335 case BPF_LD
|BPF_H
|BPF_IND
:
337 if (X
> buflen
|| pc
->k
> buflen
- X
||
338 sizeof(int16_t) > buflen
- k
) {
344 A
= m_xhalf((struct mbuf
*)p
, k
, &merr
);
352 A
= EXTRACT_SHORT(&p
[k
]);
355 case BPF_LD
|BPF_B
|BPF_IND
:
357 if (pc
->k
>= buflen
|| X
>= buflen
- pc
->k
) {
359 register struct mbuf
*m
;
363 m
= (struct mbuf
*)p
;
365 A
= mtod(m
, char *)[k
];
374 case BPF_LDX
|BPF_MSH
|BPF_B
:
378 register struct mbuf
*m
;
382 m
= (struct mbuf
*)p
;
384 X
= (mtod(m
, char *)[k
] & 0xf) << 2;
390 X
= (p
[pc
->k
] & 0xf) << 2;
397 case BPF_LDX
|BPF_IMM
:
405 case BPF_LDX
|BPF_MEM
:
421 case BPF_JMP
|BPF_JGT
|BPF_K
:
422 pc
+= (A
> pc
->k
) ? pc
->jt
: pc
->jf
;
425 case BPF_JMP
|BPF_JGE
|BPF_K
:
426 pc
+= (A
>= pc
->k
) ? pc
->jt
: pc
->jf
;
429 case BPF_JMP
|BPF_JEQ
|BPF_K
:
430 pc
+= (A
== pc
->k
) ? pc
->jt
: pc
->jf
;
433 case BPF_JMP
|BPF_JSET
|BPF_K
:
434 pc
+= (A
& pc
->k
) ? pc
->jt
: pc
->jf
;
437 case BPF_JMP
|BPF_JGT
|BPF_X
:
438 pc
+= (A
> X
) ? pc
->jt
: pc
->jf
;
441 case BPF_JMP
|BPF_JGE
|BPF_X
:
442 pc
+= (A
>= X
) ? pc
->jt
: pc
->jf
;
445 case BPF_JMP
|BPF_JEQ
|BPF_X
:
446 pc
+= (A
== X
) ? pc
->jt
: pc
->jf
;
449 case BPF_JMP
|BPF_JSET
|BPF_X
:
450 pc
+= (A
& X
) ? pc
->jt
: pc
->jf
;
453 case BPF_ALU
|BPF_ADD
|BPF_X
:
457 case BPF_ALU
|BPF_SUB
|BPF_X
:
461 case BPF_ALU
|BPF_MUL
|BPF_X
:
465 case BPF_ALU
|BPF_DIV
|BPF_X
:
471 case BPF_ALU
|BPF_AND
|BPF_X
:
475 case BPF_ALU
|BPF_OR
|BPF_X
:
479 case BPF_ALU
|BPF_LSH
|BPF_X
:
483 case BPF_ALU
|BPF_RSH
|BPF_X
:
487 case BPF_ALU
|BPF_ADD
|BPF_K
:
491 case BPF_ALU
|BPF_SUB
|BPF_K
:
495 case BPF_ALU
|BPF_MUL
|BPF_K
:
499 case BPF_ALU
|BPF_DIV
|BPF_K
:
503 case BPF_ALU
|BPF_AND
|BPF_K
:
507 case BPF_ALU
|BPF_OR
|BPF_K
:
511 case BPF_ALU
|BPF_LSH
|BPF_K
:
515 case BPF_ALU
|BPF_RSH
|BPF_K
:
519 case BPF_ALU
|BPF_NEG
:
523 case BPF_MISC
|BPF_TAX
:
527 case BPF_MISC
|BPF_TXA
:
536 * Return true if the 'fcode' is a valid filter program.
537 * The constraints are that each jump be forward and to a valid
538 * code. The code must terminate with either an accept or reject.
539 * 'valid' is an array for use by the routine (it must be at least
542 * The kernel needs to be able to verify an application's filter code.
543 * Otherwise, a bogus program could easily crash the system.
547 const struct bpf_insn
*f
;
551 const struct bpf_insn
*p
;
553 for (i
= 0; i
< len
; ++i
) {
555 * Check that that jumps are forward, and within
559 if (BPF_CLASS(p
->code
) == BPF_JMP
) {
560 register int from
= i
+ 1;
562 if (BPF_OP(p
->code
) == BPF_JA
) {
563 if (from
>= len
|| p
->k
>= len
- from
)
566 else if (from
>= len
|| p
->jt
>= len
- from
||
571 * Check that memory operations use valid addresses.
573 if ((BPF_CLASS(p
->code
) == BPF_ST
||
574 (BPF_CLASS(p
->code
) == BPF_LD
&&
575 (p
->code
& 0xe0) == BPF_MEM
)) &&
576 p
->k
>= BPF_MEMWORDS
)
579 * Check for constant division by 0.
581 if (p
->code
== (BPF_ALU
|BPF_DIV
|BPF_K
) && p
->k
== 0)
584 return BPF_CLASS(f
[len
- 1].code
) == BPF_RET
;