]>
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_OSREFERENCE_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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1990, 1991, 1993
30 * The Regents of the University of California. All rights reserved.
32 * This code is derived from the Stanford/CMU enet packet filter,
33 * (net/enet.c) distributed as part of 4.3BSD, and code contributed
34 * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
35 * Berkeley Laboratory.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the University of
48 * California, Berkeley and its contributors.
49 * 4. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * @(#)bpf_filter.c 8.1 (Berkeley) 6/10/93
67 * $FreeBSD: src/sys/net/bpf_filter.c,v 1.17 1999/12/29 04:38:31 peter Exp $
70 #include <sys/param.h>
73 #include <netinet/in.h>
76 #if defined(sparc) || defined(mips) || defined(ibm032) || defined(__alpha__)
81 #define EXTRACT_SHORT(p) ((u_int16_t)ntohs(*(u_int16_t *)p))
82 #define EXTRACT_LONG(p) (ntohl(*(u_int32_t *)p))
84 #define EXTRACT_SHORT(p)\
86 ((u_int16_t)*((u_char *)p+0)<<8|\
87 (u_int16_t)*((u_char *)p+1)<<0))
88 #define EXTRACT_LONG(p)\
89 ((u_int32_t)*((u_char *)p+0)<<24|\
90 (u_int32_t)*((u_char *)p+1)<<16|\
91 (u_int32_t)*((u_char *)p+2)<<8|\
92 (u_int32_t)*((u_char *)p+3)<<0)
100 #define MINDEX(m, k) \
102 register unsigned int len = m->m_len; \
113 static u_int16_t
m_xhalf(struct mbuf
*m
, bpf_u_int32 k
, int *err
);
114 static u_int32_t
m_xword(struct mbuf
*m
, bpf_u_int32 k
, int *err
);
117 m_xword(struct mbuf
*m
, bpf_u_int32 k
, int *err
)
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) |
170 m_xhalf(struct mbuf
*m
, bpf_u_int32 k
, int *err
)
174 register struct mbuf
*m0
;
184 cp
= mtod(m
, u_char
*) + k
;
187 return EXTRACT_SHORT(cp
);
193 return (cp
[0] << 8) | mtod(m0
, u_char
*)[0];
201 * Execute the filter program starting at pc on the packet p
202 * wirelen is the length of the original packet
203 * buflen is the amount of data present
206 bpf_filter(const struct bpf_insn
*pc
, u_char
*p
, u_int wirelen
, u_int buflen
)
208 register u_int32_t A
= 0, X
= 0;
209 register bpf_u_int32 k
;
210 int32_t mem
[BPF_MEMWORDS
];
214 * No filter means accept all.
235 case BPF_LD
|BPF_W
|BPF_ABS
:
237 if (k
> buflen
|| sizeof(int32_t) > buflen
- k
) {
243 A
= m_xword((struct mbuf
*)p
, k
, &merr
);
252 if (((intptr_t)(p
+ k
) & 3) != 0)
253 A
= EXTRACT_LONG(&p
[k
]);
256 A
= ntohl(*(int32_t *)(p
+ k
));
259 case BPF_LD
|BPF_H
|BPF_ABS
:
261 if (k
> buflen
|| sizeof(int16_t) > buflen
- k
) {
267 A
= m_xhalf((struct mbuf
*)p
, k
, &merr
);
273 A
= EXTRACT_SHORT(&p
[k
]);
276 case BPF_LD
|BPF_B
|BPF_ABS
:
280 register struct mbuf
*m
;
284 m
= (struct mbuf
*)p
;
286 A
= mtod(m
, u_char
*)[k
];
295 case BPF_LD
|BPF_W
|BPF_LEN
:
299 case BPF_LDX
|BPF_W
|BPF_LEN
:
303 case BPF_LD
|BPF_W
|BPF_IND
:
305 if (pc
->k
> buflen
|| X
> buflen
- pc
->k
||
306 sizeof(int32_t) > buflen
- k
) {
312 A
= m_xword((struct mbuf
*)p
, k
, &merr
);
321 if (((intptr_t)(p
+ k
) & 3) != 0)
322 A
= EXTRACT_LONG(&p
[k
]);
325 A
= ntohl(*(int32_t *)(p
+ k
));
328 case BPF_LD
|BPF_H
|BPF_IND
:
330 if (X
> buflen
|| pc
->k
> buflen
- X
||
331 sizeof(int16_t) > buflen
- k
) {
337 A
= m_xhalf((struct mbuf
*)p
, k
, &merr
);
345 A
= EXTRACT_SHORT(&p
[k
]);
348 case BPF_LD
|BPF_B
|BPF_IND
:
350 if (pc
->k
>= buflen
|| X
>= buflen
- pc
->k
) {
352 register struct mbuf
*m
;
356 m
= (struct mbuf
*)p
;
358 A
= mtod(m
, char *)[k
];
367 case BPF_LDX
|BPF_MSH
|BPF_B
:
371 register struct mbuf
*m
;
375 m
= (struct mbuf
*)p
;
377 X
= (mtod(m
, char *)[k
] & 0xf) << 2;
383 X
= (p
[pc
->k
] & 0xf) << 2;
390 case BPF_LDX
|BPF_IMM
:
398 case BPF_LDX
|BPF_MEM
:
414 case BPF_JMP
|BPF_JGT
|BPF_K
:
415 pc
+= (A
> pc
->k
) ? pc
->jt
: pc
->jf
;
418 case BPF_JMP
|BPF_JGE
|BPF_K
:
419 pc
+= (A
>= pc
->k
) ? pc
->jt
: pc
->jf
;
422 case BPF_JMP
|BPF_JEQ
|BPF_K
:
423 pc
+= (A
== pc
->k
) ? pc
->jt
: pc
->jf
;
426 case BPF_JMP
|BPF_JSET
|BPF_K
:
427 pc
+= (A
& pc
->k
) ? pc
->jt
: pc
->jf
;
430 case BPF_JMP
|BPF_JGT
|BPF_X
:
431 pc
+= (A
> X
) ? pc
->jt
: pc
->jf
;
434 case BPF_JMP
|BPF_JGE
|BPF_X
:
435 pc
+= (A
>= X
) ? pc
->jt
: pc
->jf
;
438 case BPF_JMP
|BPF_JEQ
|BPF_X
:
439 pc
+= (A
== X
) ? pc
->jt
: pc
->jf
;
442 case BPF_JMP
|BPF_JSET
|BPF_X
:
443 pc
+= (A
& X
) ? pc
->jt
: pc
->jf
;
446 case BPF_ALU
|BPF_ADD
|BPF_X
:
450 case BPF_ALU
|BPF_SUB
|BPF_X
:
454 case BPF_ALU
|BPF_MUL
|BPF_X
:
458 case BPF_ALU
|BPF_DIV
|BPF_X
:
464 case BPF_ALU
|BPF_AND
|BPF_X
:
468 case BPF_ALU
|BPF_OR
|BPF_X
:
472 case BPF_ALU
|BPF_LSH
|BPF_X
:
476 case BPF_ALU
|BPF_RSH
|BPF_X
:
480 case BPF_ALU
|BPF_ADD
|BPF_K
:
484 case BPF_ALU
|BPF_SUB
|BPF_K
:
488 case BPF_ALU
|BPF_MUL
|BPF_K
:
492 case BPF_ALU
|BPF_DIV
|BPF_K
:
496 case BPF_ALU
|BPF_AND
|BPF_K
:
500 case BPF_ALU
|BPF_OR
|BPF_K
:
504 case BPF_ALU
|BPF_LSH
|BPF_K
:
508 case BPF_ALU
|BPF_RSH
|BPF_K
:
512 case BPF_ALU
|BPF_NEG
:
516 case BPF_MISC
|BPF_TAX
:
520 case BPF_MISC
|BPF_TXA
:
529 * Return true if the 'fcode' is a valid filter program.
530 * The constraints are that each jump be forward and to a valid
531 * code. The code must terminate with either an accept or reject.
532 * 'valid' is an array for use by the routine (it must be at least
535 * The kernel needs to be able to verify an application's filter code.
536 * Otherwise, a bogus program could easily crash the system.
539 bpf_validate(const struct bpf_insn
*f
, int len
)
542 const struct bpf_insn
*p
;
544 for (i
= 0; i
< len
; ++i
) {
546 * Check that that jumps are forward, and within
550 if (BPF_CLASS(p
->code
) == BPF_JMP
) {
551 register int from
= i
+ 1;
553 if (BPF_OP(p
->code
) == BPF_JA
) {
554 if (from
>= len
|| p
->k
>= (bpf_u_int32
)(len
- from
))
557 else if (from
>= len
|| p
->jt
>= len
- from
||
562 * Check that memory operations use valid addresses.
564 if ((BPF_CLASS(p
->code
) == BPF_ST
||
565 (BPF_CLASS(p
->code
) == BPF_LD
&&
566 (p
->code
& 0xe0) == BPF_MEM
)) &&
567 p
->k
>= BPF_MEMWORDS
)
570 * Check for constant division by 0.
572 if (p
->code
== (BPF_ALU
|BPF_DIV
|BPF_K
) && p
->k
== 0)
575 return BPF_CLASS(f
[len
- 1].code
) == BPF_RET
;