]>
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
61 * $FreeBSD: src/sys/net/bpf_filter.c,v 1.17 1999/12/29 04:38:31 peter Exp $
64 #include <sys/param.h>
67 #include <netinet/in.h>
70 #if defined(sparc) || defined(mips) || defined(ibm032) || defined(__alpha__)
75 #define EXTRACT_SHORT(p) ((u_int16_t)ntohs(*(u_int16_t *)p))
76 #define EXTRACT_LONG(p) (ntohl(*(u_int32_t *)p))
78 #define EXTRACT_SHORT(p)\
80 ((u_int16_t)*((u_char *)p+0)<<8|\
81 (u_int16_t)*((u_char *)p+1)<<0))
82 #define EXTRACT_LONG(p)\
83 ((u_int32_t)*((u_char *)p+0)<<24|\
84 (u_int32_t)*((u_char *)p+1)<<16|\
85 (u_int32_t)*((u_char *)p+2)<<8|\
86 (u_int32_t)*((u_char *)p+3)<<0)
94 #define MINDEX(m, k) \
96 register unsigned int len = m->m_len; \
107 static u_int16_t
m_xhalf(struct mbuf
*m
, bpf_u_int32 k
, int *err
);
108 static u_int32_t
m_xword(struct mbuf
*m
, bpf_u_int32 k
, int *err
);
111 m_xword(struct mbuf
*m
, bpf_u_int32 k
, int *err
)
114 register u_char
*cp
, *np
;
115 register struct mbuf
*m0
;
125 cp
= mtod(m
, u_char
*) + k
;
128 return EXTRACT_LONG(cp
);
131 if (m0
== 0 || m0
->m_len
+ len
- k
< 4)
134 np
= mtod(m0
, u_char
*);
139 ((u_int32_t
)cp
[0] << 24) |
140 ((u_int32_t
)np
[0] << 16) |
141 ((u_int32_t
)np
[1] << 8) |
146 ((u_int32_t
)cp
[0] << 24) |
147 ((u_int32_t
)cp
[1] << 16) |
148 ((u_int32_t
)np
[0] << 8) |
153 ((u_int32_t
)cp
[0] << 24) |
154 ((u_int32_t
)cp
[1] << 16) |
155 ((u_int32_t
)cp
[2] << 8) |
164 m_xhalf(struct mbuf
*m
, bpf_u_int32 k
, int *err
)
168 register struct mbuf
*m0
;
178 cp
= mtod(m
, u_char
*) + k
;
181 return EXTRACT_SHORT(cp
);
187 return (cp
[0] << 8) | mtod(m0
, u_char
*)[0];
195 * Execute the filter program starting at pc on the packet p
196 * wirelen is the length of the original packet
197 * buflen is the amount of data present
200 bpf_filter(const struct bpf_insn
*pc
, u_char
*p
, u_int wirelen
, u_int buflen
)
202 register u_int32_t A
= 0, X
= 0;
203 register bpf_u_int32 k
;
204 int32_t mem
[BPF_MEMWORDS
];
208 * No filter means accept all.
229 case BPF_LD
|BPF_W
|BPF_ABS
:
231 if (k
> buflen
|| sizeof(int32_t) > buflen
- k
) {
237 A
= m_xword((struct mbuf
*)p
, k
, &merr
);
246 if (((intptr_t)(p
+ k
) & 3) != 0)
247 A
= EXTRACT_LONG(&p
[k
]);
250 A
= ntohl(*(int32_t *)(p
+ k
));
253 case BPF_LD
|BPF_H
|BPF_ABS
:
255 if (k
> buflen
|| sizeof(int16_t) > buflen
- k
) {
261 A
= m_xhalf((struct mbuf
*)p
, k
, &merr
);
267 A
= EXTRACT_SHORT(&p
[k
]);
270 case BPF_LD
|BPF_B
|BPF_ABS
:
274 register struct mbuf
*m
;
278 m
= (struct mbuf
*)p
;
280 A
= mtod(m
, u_char
*)[k
];
289 case BPF_LD
|BPF_W
|BPF_LEN
:
293 case BPF_LDX
|BPF_W
|BPF_LEN
:
297 case BPF_LD
|BPF_W
|BPF_IND
:
299 if (pc
->k
> buflen
|| X
> buflen
- pc
->k
||
300 sizeof(int32_t) > buflen
- k
) {
306 A
= m_xword((struct mbuf
*)p
, k
, &merr
);
315 if (((intptr_t)(p
+ k
) & 3) != 0)
316 A
= EXTRACT_LONG(&p
[k
]);
319 A
= ntohl(*(int32_t *)(p
+ k
));
322 case BPF_LD
|BPF_H
|BPF_IND
:
324 if (X
> buflen
|| pc
->k
> buflen
- X
||
325 sizeof(int16_t) > buflen
- k
) {
331 A
= m_xhalf((struct mbuf
*)p
, k
, &merr
);
339 A
= EXTRACT_SHORT(&p
[k
]);
342 case BPF_LD
|BPF_B
|BPF_IND
:
344 if (pc
->k
>= buflen
|| X
>= buflen
- pc
->k
) {
346 register struct mbuf
*m
;
350 m
= (struct mbuf
*)p
;
352 A
= mtod(m
, char *)[k
];
361 case BPF_LDX
|BPF_MSH
|BPF_B
:
365 register struct mbuf
*m
;
369 m
= (struct mbuf
*)p
;
371 X
= (mtod(m
, char *)[k
] & 0xf) << 2;
377 X
= (p
[pc
->k
] & 0xf) << 2;
384 case BPF_LDX
|BPF_IMM
:
392 case BPF_LDX
|BPF_MEM
:
408 case BPF_JMP
|BPF_JGT
|BPF_K
:
409 pc
+= (A
> pc
->k
) ? pc
->jt
: pc
->jf
;
412 case BPF_JMP
|BPF_JGE
|BPF_K
:
413 pc
+= (A
>= pc
->k
) ? pc
->jt
: pc
->jf
;
416 case BPF_JMP
|BPF_JEQ
|BPF_K
:
417 pc
+= (A
== pc
->k
) ? pc
->jt
: pc
->jf
;
420 case BPF_JMP
|BPF_JSET
|BPF_K
:
421 pc
+= (A
& pc
->k
) ? pc
->jt
: pc
->jf
;
424 case BPF_JMP
|BPF_JGT
|BPF_X
:
425 pc
+= (A
> X
) ? pc
->jt
: pc
->jf
;
428 case BPF_JMP
|BPF_JGE
|BPF_X
:
429 pc
+= (A
>= X
) ? pc
->jt
: pc
->jf
;
432 case BPF_JMP
|BPF_JEQ
|BPF_X
:
433 pc
+= (A
== X
) ? pc
->jt
: pc
->jf
;
436 case BPF_JMP
|BPF_JSET
|BPF_X
:
437 pc
+= (A
& X
) ? pc
->jt
: pc
->jf
;
440 case BPF_ALU
|BPF_ADD
|BPF_X
:
444 case BPF_ALU
|BPF_SUB
|BPF_X
:
448 case BPF_ALU
|BPF_MUL
|BPF_X
:
452 case BPF_ALU
|BPF_DIV
|BPF_X
:
458 case BPF_ALU
|BPF_AND
|BPF_X
:
462 case BPF_ALU
|BPF_OR
|BPF_X
:
466 case BPF_ALU
|BPF_LSH
|BPF_X
:
470 case BPF_ALU
|BPF_RSH
|BPF_X
:
474 case BPF_ALU
|BPF_ADD
|BPF_K
:
478 case BPF_ALU
|BPF_SUB
|BPF_K
:
482 case BPF_ALU
|BPF_MUL
|BPF_K
:
486 case BPF_ALU
|BPF_DIV
|BPF_K
:
490 case BPF_ALU
|BPF_AND
|BPF_K
:
494 case BPF_ALU
|BPF_OR
|BPF_K
:
498 case BPF_ALU
|BPF_LSH
|BPF_K
:
502 case BPF_ALU
|BPF_RSH
|BPF_K
:
506 case BPF_ALU
|BPF_NEG
:
510 case BPF_MISC
|BPF_TAX
:
514 case BPF_MISC
|BPF_TXA
:
523 * Return true if the 'fcode' is a valid filter program.
524 * The constraints are that each jump be forward and to a valid
525 * code. The code must terminate with either an accept or reject.
526 * 'valid' is an array for use by the routine (it must be at least
529 * The kernel needs to be able to verify an application's filter code.
530 * Otherwise, a bogus program could easily crash the system.
533 bpf_validate(const struct bpf_insn
*f
, int len
)
536 const struct bpf_insn
*p
;
538 for (i
= 0; i
< len
; ++i
) {
540 * Check that that jumps are forward, and within
544 if (BPF_CLASS(p
->code
) == BPF_JMP
) {
545 register int from
= i
+ 1;
547 if (BPF_OP(p
->code
) == BPF_JA
) {
548 if (from
>= len
|| p
->k
>= (bpf_u_int32
)(len
- from
))
551 else if (from
>= len
|| p
->jt
>= len
- from
||
556 * Check that memory operations use valid addresses.
558 if ((BPF_CLASS(p
->code
) == BPF_ST
||
559 (BPF_CLASS(p
->code
) == BPF_LD
&&
560 (p
->code
& 0xe0) == BPF_MEM
)) &&
561 p
->k
>= BPF_MEMWORDS
)
564 * Check for constant division by 0.
566 if (p
->code
== (BPF_ALU
|BPF_DIV
|BPF_K
) && p
->k
== 0)
569 return BPF_CLASS(f
[len
- 1].code
) == BPF_RET
;