]>
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 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1990, 1991, 1993
24 * The Regents of the University of California. All rights reserved.
26 * This code is derived from the Stanford/CMU enet packet filter,
27 * (net/enet.c) distributed as part of 4.3BSD, and code contributed
28 * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
29 * Berkeley Laboratory.
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
39 * 3. All advertising materials mentioning features or use of this software
40 * must display the following acknowledgement:
41 * This product includes software developed by the University of
42 * California, Berkeley and its contributors.
43 * 4. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * @(#)bpf_filter.c 8.1 (Berkeley) 6/10/93
63 #include <sys/param.h>
66 #include <netinet/in.h>
69 #if defined(sparc) || defined(mips) || defined(ibm032) || defined(__alpha__)
74 #define EXTRACT_SHORT(p) ((u_int16_t)ntohs(*(u_int16_t *)p))
75 #define EXTRACT_LONG(p) (ntohl(*(u_int32_t *)p))
77 #define EXTRACT_SHORT(p)\
79 ((u_int16_t)*((u_char *)p+0)<<8|\
80 (u_int16_t)*((u_char *)p+1)<<0))
81 #define EXTRACT_LONG(p)\
82 ((u_int32_t)*((u_char *)p+0)<<24|\
83 (u_int32_t)*((u_char *)p+1)<<16|\
84 (u_int32_t)*((u_char *)p+2)<<8|\
85 (u_int32_t)*((u_char *)p+3)<<0)
93 #define MINDEX(m, k) \
95 register int len = m->m_len; \
106 static u_int16_t m_xhalf
__P((struct mbuf
*m
, bpf_u_int32 k
, int *err
));
107 static u_int32_t m_xword
__P((struct mbuf
*m
, bpf_u_int32 k
, int *err
));
111 register struct mbuf
*m
;
112 register bpf_u_int32 k
;
116 register u_char
*cp
, *np
;
117 register struct mbuf
*m0
;
127 cp
= mtod(m
, u_char
*) + k
;
130 return EXTRACT_LONG(cp
);
133 if (m0
== 0 || m0
->m_len
+ len
- k
< 4)
136 np
= mtod(m0
, u_char
*);
141 ((u_int32_t
)cp
[0] << 24) |
142 ((u_int32_t
)np
[0] << 16) |
143 ((u_int32_t
)np
[1] << 8) |
148 ((u_int32_t
)cp
[0] << 24) |
149 ((u_int32_t
)cp
[1] << 16) |
150 ((u_int32_t
)np
[0] << 8) |
155 ((u_int32_t
)cp
[0] << 24) |
156 ((u_int32_t
)cp
[1] << 16) |
157 ((u_int32_t
)cp
[2] << 8) |
167 register struct mbuf
*m
;
168 register bpf_u_int32 k
;
173 register struct mbuf
*m0
;
183 cp
= mtod(m
, u_char
*) + k
;
186 return EXTRACT_SHORT(cp
);
192 return (cp
[0] << 8) | mtod(m0
, u_char
*)[0];
200 * Execute the filter program starting at pc on the packet p
201 * wirelen is the length of the original packet
202 * buflen is the amount of data present
205 bpf_filter(pc
, p
, wirelen
, buflen
)
206 register struct bpf_insn
*pc
;
209 register u_int buflen
;
211 register u_int32_t A
= 0, X
= 0;
212 register bpf_u_int32 k
;
213 int32_t mem
[BPF_MEMWORDS
];
217 * No filter means accept all.
238 case BPF_LD
|BPF_W
|BPF_ABS
:
240 if (k
> buflen
|| sizeof(int32_t) > buflen
- k
) {
246 A
= m_xword((struct mbuf
*)p
, k
, &merr
);
255 if (((intptr_t)(p
+ k
) & 3) != 0)
256 A
= EXTRACT_LONG(&p
[k
]);
259 A
= ntohl(*(int32_t *)(p
+ k
));
262 case BPF_LD
|BPF_H
|BPF_ABS
:
264 if (k
> buflen
|| sizeof(int16_t) > buflen
- k
) {
270 A
= m_xhalf((struct mbuf
*)p
, k
, &merr
);
276 A
= EXTRACT_SHORT(&p
[k
]);
279 case BPF_LD
|BPF_B
|BPF_ABS
:
283 register struct mbuf
*m
;
287 m
= (struct mbuf
*)p
;
289 A
= mtod(m
, u_char
*)[k
];
298 case BPF_LD
|BPF_W
|BPF_LEN
:
302 case BPF_LDX
|BPF_W
|BPF_LEN
:
306 case BPF_LD
|BPF_W
|BPF_IND
:
308 if (pc
->k
> buflen
|| X
> buflen
- pc
->k
|| sizeof(int32_t) > buflen
- k
) {
314 A
= m_xword((struct mbuf
*)p
, k
, &merr
);
323 if (((intptr_t)(p
+ k
) & 3) != 0)
324 A
= EXTRACT_LONG(&p
[k
]);
327 A
= ntohl(*(int32_t *)(p
+ k
));
330 case BPF_LD
|BPF_H
|BPF_IND
:
332 if (X
> buflen
|| pc
->k
> buflen
- X
|| sizeof(int16_t) > buflen
- pc
->k
) {
338 A
= m_xhalf((struct mbuf
*)p
, k
, &merr
);
346 A
= EXTRACT_SHORT(&p
[k
]);
349 case BPF_LD
|BPF_B
|BPF_IND
:
351 if (pc
->k
>= buflen
|| X
>= buflen
- pc
->k
) {
353 register struct mbuf
*m
;
357 m
= (struct mbuf
*)p
;
359 A
= mtod(m
, char *)[k
];
368 case BPF_LDX
|BPF_MSH
|BPF_B
:
372 register struct mbuf
*m
;
376 m
= (struct mbuf
*)p
;
378 X
= (mtod(m
, char *)[k
] & 0xf) << 2;
384 X
= (p
[pc
->k
] & 0xf) << 2;
391 case BPF_LDX
|BPF_IMM
:
399 case BPF_LDX
|BPF_MEM
:
415 case BPF_JMP
|BPF_JGT
|BPF_K
:
416 pc
+= (A
> pc
->k
) ? pc
->jt
: pc
->jf
;
419 case BPF_JMP
|BPF_JGE
|BPF_K
:
420 pc
+= (A
>= pc
->k
) ? pc
->jt
: pc
->jf
;
423 case BPF_JMP
|BPF_JEQ
|BPF_K
:
424 pc
+= (A
== pc
->k
) ? pc
->jt
: pc
->jf
;
427 case BPF_JMP
|BPF_JSET
|BPF_K
:
428 pc
+= (A
& pc
->k
) ? pc
->jt
: pc
->jf
;
431 case BPF_JMP
|BPF_JGT
|BPF_X
:
432 pc
+= (A
> X
) ? pc
->jt
: pc
->jf
;
435 case BPF_JMP
|BPF_JGE
|BPF_X
:
436 pc
+= (A
>= X
) ? pc
->jt
: pc
->jf
;
439 case BPF_JMP
|BPF_JEQ
|BPF_X
:
440 pc
+= (A
== X
) ? pc
->jt
: pc
->jf
;
443 case BPF_JMP
|BPF_JSET
|BPF_X
:
444 pc
+= (A
& X
) ? pc
->jt
: pc
->jf
;
447 case BPF_ALU
|BPF_ADD
|BPF_X
:
451 case BPF_ALU
|BPF_SUB
|BPF_X
:
455 case BPF_ALU
|BPF_MUL
|BPF_X
:
459 case BPF_ALU
|BPF_DIV
|BPF_X
:
465 case BPF_ALU
|BPF_AND
|BPF_X
:
469 case BPF_ALU
|BPF_OR
|BPF_X
:
473 case BPF_ALU
|BPF_LSH
|BPF_X
:
477 case BPF_ALU
|BPF_RSH
|BPF_X
:
481 case BPF_ALU
|BPF_ADD
|BPF_K
:
485 case BPF_ALU
|BPF_SUB
|BPF_K
:
489 case BPF_ALU
|BPF_MUL
|BPF_K
:
493 case BPF_ALU
|BPF_DIV
|BPF_K
:
497 case BPF_ALU
|BPF_AND
|BPF_K
:
501 case BPF_ALU
|BPF_OR
|BPF_K
:
505 case BPF_ALU
|BPF_LSH
|BPF_K
:
509 case BPF_ALU
|BPF_RSH
|BPF_K
:
513 case BPF_ALU
|BPF_NEG
:
517 case BPF_MISC
|BPF_TAX
:
521 case BPF_MISC
|BPF_TXA
:
530 * Return true if the 'fcode' is a valid filter program.
531 * The constraints are that each jump be forward and to a valid
532 * code. The code must terminate with either an accept or reject.
533 * 'valid' is an array for use by the routine (it must be at least
536 * The kernel needs to be able to verify an application's filter code.
537 * Otherwise, a bogus program could easily crash the system.
545 register struct bpf_insn
*p
;
547 for (i
= 0; i
< len
; ++i
) {
549 * Check that that jumps are forward, and within
553 if (BPF_CLASS(p
->code
) == BPF_JMP
) {
554 register int from
= i
+ 1;
556 if (BPF_OP(p
->code
) == BPF_JA
) {
557 if (from
>= len
|| p
->k
>= len
- from
)
560 else if (from
>= len
|| p
->jt
>= len
- from
|| p
->jf
>= len
- from
)
564 * Check that memory operations use valid addresses.
566 if ((BPF_CLASS(p
->code
) == BPF_ST
||
567 (BPF_CLASS(p
->code
) == BPF_LD
&&
568 (p
->code
& 0xe0) == BPF_MEM
)) &&
569 p
->k
>= BPF_MEMWORDS
)
572 * Check for constant division by 0.
574 if (p
->code
== (BPF_ALU
|BPF_DIV
|BPF_K
) && p
->k
== 0)
577 return BPF_CLASS(f
[len
- 1].code
) == BPF_RET
;