]> git.saurik.com Git - apple/xnu.git/blame - bsd/net/bpf_filter.c
xnu-792.12.6.tar.gz
[apple/xnu.git] / bsd / net / bpf_filter.c
CommitLineData
1c79356b 1/*
8ad349bb 2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
1c79356b 3 *
8ad349bb 4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
1c79356b 5 *
8ad349bb
A
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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
1c79356b
A
29 */
30/*
31 * Copyright (c) 1990, 1991, 1993
32 * The Regents of the University of California. All rights reserved.
33 *
34 * This code is derived from the Stanford/CMU enet packet filter,
35 * (net/enet.c) distributed as part of 4.3BSD, and code contributed
36 * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
37 * Berkeley Laboratory.
38 *
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
41 * are met:
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. All advertising materials mentioning features or use of this software
48 * must display the following acknowledgement:
49 * This product includes software developed by the University of
50 * California, Berkeley and its contributors.
51 * 4. Neither the name of the University nor the names of its contributors
52 * may be used to endorse or promote products derived from this software
53 * without specific prior written permission.
54 *
55 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * SUCH DAMAGE.
66 *
67 * @(#)bpf_filter.c 8.1 (Berkeley) 6/10/93
68 *
9bccf70c 69 * $FreeBSD: src/sys/net/bpf_filter.c,v 1.17 1999/12/29 04:38:31 peter Exp $
1c79356b
A
70 */
71
72#include <sys/param.h>
73
74#ifdef sun
75#include <netinet/in.h>
76#endif
77
78#if defined(sparc) || defined(mips) || defined(ibm032) || defined(__alpha__)
79#define BPF_ALIGN
80#endif
81
82#ifndef BPF_ALIGN
83#define EXTRACT_SHORT(p) ((u_int16_t)ntohs(*(u_int16_t *)p))
84#define EXTRACT_LONG(p) (ntohl(*(u_int32_t *)p))
85#else
86#define EXTRACT_SHORT(p)\
87 ((u_int16_t)\
88 ((u_int16_t)*((u_char *)p+0)<<8|\
89 (u_int16_t)*((u_char *)p+1)<<0))
90#define EXTRACT_LONG(p)\
91 ((u_int32_t)*((u_char *)p+0)<<24|\
92 (u_int32_t)*((u_char *)p+1)<<16|\
93 (u_int32_t)*((u_char *)p+2)<<8|\
94 (u_int32_t)*((u_char *)p+3)<<0)
95#endif
96
97#ifdef KERNEL
98#include <sys/mbuf.h>
99#endif
100#include <net/bpf.h>
101#ifdef KERNEL
102#define MINDEX(m, k) \
103{ \
91447636 104 register unsigned int len = m->m_len; \
1c79356b
A
105 \
106 while (k >= len) { \
107 k -= len; \
108 m = m->m_next; \
109 if (m == 0) \
110 return 0; \
111 len = m->m_len; \
112 } \
113}
114
91447636
A
115static u_int16_t m_xhalf(struct mbuf *m, bpf_u_int32 k, int *err);
116static u_int32_t m_xword(struct mbuf *m, bpf_u_int32 k, int *err);
1c79356b
A
117
118static u_int32_t
91447636 119m_xword(struct mbuf *m, bpf_u_int32 k, int *err)
1c79356b
A
120{
121 register size_t len;
122 register u_char *cp, *np;
123 register struct mbuf *m0;
124
125 len = m->m_len;
126 while (k >= len) {
127 k -= len;
128 m = m->m_next;
129 if (m == 0)
130 goto bad;
131 len = m->m_len;
132 }
133 cp = mtod(m, u_char *) + k;
134 if (len - k >= 4) {
135 *err = 0;
136 return EXTRACT_LONG(cp);
137 }
138 m0 = m->m_next;
139 if (m0 == 0 || m0->m_len + len - k < 4)
140 goto bad;
141 *err = 0;
142 np = mtod(m0, u_char *);
143 switch (len - k) {
144
145 case 1:
146 return
147 ((u_int32_t)cp[0] << 24) |
148 ((u_int32_t)np[0] << 16) |
149 ((u_int32_t)np[1] << 8) |
150 (u_int32_t)np[2];
151
152 case 2:
153 return
154 ((u_int32_t)cp[0] << 24) |
155 ((u_int32_t)cp[1] << 16) |
156 ((u_int32_t)np[0] << 8) |
157 (u_int32_t)np[1];
158
159 default:
160 return
161 ((u_int32_t)cp[0] << 24) |
162 ((u_int32_t)cp[1] << 16) |
163 ((u_int32_t)cp[2] << 8) |
164 (u_int32_t)np[0];
165 }
166 bad:
167 *err = 1;
168 return 0;
169}
170
171static u_int16_t
91447636 172m_xhalf(struct mbuf *m, bpf_u_int32 k, int *err)
1c79356b
A
173{
174 register size_t len;
175 register u_char *cp;
176 register struct mbuf *m0;
177
178 len = m->m_len;
179 while (k >= len) {
180 k -= len;
181 m = m->m_next;
182 if (m == 0)
183 goto bad;
184 len = m->m_len;
185 }
186 cp = mtod(m, u_char *) + k;
187 if (len - k >= 2) {
188 *err = 0;
189 return EXTRACT_SHORT(cp);
190 }
191 m0 = m->m_next;
192 if (m0 == 0)
193 goto bad;
194 *err = 0;
195 return (cp[0] << 8) | mtod(m0, u_char *)[0];
196 bad:
197 *err = 1;
198 return 0;
199}
200#endif
201
202/*
203 * Execute the filter program starting at pc on the packet p
204 * wirelen is the length of the original packet
205 * buflen is the amount of data present
206 */
207u_int
91447636 208bpf_filter(const struct bpf_insn *pc, u_char *p, u_int wirelen, u_int buflen)
1c79356b
A
209{
210 register u_int32_t A = 0, X = 0;
211 register bpf_u_int32 k;
212 int32_t mem[BPF_MEMWORDS];
213
214 if (pc == 0)
215 /*
216 * No filter means accept all.
217 */
218 return (u_int)-1;
219
220 --pc;
221 while (1) {
222 ++pc;
223 switch (pc->code) {
224
225 default:
226#ifdef KERNEL
227 return 0;
228#else
229 abort();
230#endif
231 case BPF_RET|BPF_K:
232 return (u_int)pc->k;
233
234 case BPF_RET|BPF_A:
235 return (u_int)A;
236
237 case BPF_LD|BPF_W|BPF_ABS:
238 k = pc->k;
239 if (k > buflen || sizeof(int32_t) > buflen - k) {
240#ifdef KERNEL
241 int merr;
242
243 if (buflen != 0)
244 return 0;
245 A = m_xword((struct mbuf *)p, k, &merr);
246 if (merr != 0)
247 return 0;
248 continue;
249#else
250 return 0;
251#endif
252 }
253#if BPF_ALIGN
254 if (((intptr_t)(p + k) & 3) != 0)
255 A = EXTRACT_LONG(&p[k]);
256 else
257#endif
258 A = ntohl(*(int32_t *)(p + k));
259 continue;
260
261 case BPF_LD|BPF_H|BPF_ABS:
262 k = pc->k;
263 if (k > buflen || sizeof(int16_t) > buflen - k) {
264#ifdef KERNEL
265 int merr;
266
267 if (buflen != 0)
268 return 0;
269 A = m_xhalf((struct mbuf *)p, k, &merr);
270 continue;
271#else
272 return 0;
273#endif
274 }
275 A = EXTRACT_SHORT(&p[k]);
276 continue;
277
278 case BPF_LD|BPF_B|BPF_ABS:
279 k = pc->k;
280 if (k >= buflen) {
281#ifdef KERNEL
282 register struct mbuf *m;
283
284 if (buflen != 0)
285 return 0;
286 m = (struct mbuf *)p;
287 MINDEX(m, k);
288 A = mtod(m, u_char *)[k];
289 continue;
290#else
291 return 0;
292#endif
293 }
294 A = p[k];
295 continue;
296
297 case BPF_LD|BPF_W|BPF_LEN:
298 A = wirelen;
299 continue;
300
301 case BPF_LDX|BPF_W|BPF_LEN:
302 X = wirelen;
303 continue;
304
305 case BPF_LD|BPF_W|BPF_IND:
306 k = X + pc->k;
9bccf70c
A
307 if (pc->k > buflen || X > buflen - pc->k ||
308 sizeof(int32_t) > buflen - k) {
1c79356b
A
309#ifdef KERNEL
310 int merr;
311
312 if (buflen != 0)
313 return 0;
314 A = m_xword((struct mbuf *)p, k, &merr);
315 if (merr != 0)
316 return 0;
317 continue;
318#else
319 return 0;
320#endif
321 }
322#if BPF_ALIGN
323 if (((intptr_t)(p + k) & 3) != 0)
324 A = EXTRACT_LONG(&p[k]);
325 else
326#endif
327 A = ntohl(*(int32_t *)(p + k));
328 continue;
329
330 case BPF_LD|BPF_H|BPF_IND:
331 k = X + pc->k;
9bccf70c
A
332 if (X > buflen || pc->k > buflen - X ||
333 sizeof(int16_t) > buflen - k) {
1c79356b
A
334#ifdef KERNEL
335 int merr;
336
337 if (buflen != 0)
338 return 0;
339 A = m_xhalf((struct mbuf *)p, k, &merr);
340 if (merr != 0)
341 return 0;
342 continue;
343#else
344 return 0;
345#endif
346 }
347 A = EXTRACT_SHORT(&p[k]);
348 continue;
349
350 case BPF_LD|BPF_B|BPF_IND:
351 k = X + pc->k;
352 if (pc->k >= buflen || X >= buflen - pc->k) {
353#ifdef KERNEL
354 register struct mbuf *m;
355
356 if (buflen != 0)
357 return 0;
358 m = (struct mbuf *)p;
359 MINDEX(m, k);
360 A = mtod(m, char *)[k];
361 continue;
362#else
363 return 0;
364#endif
365 }
366 A = p[k];
367 continue;
368
369 case BPF_LDX|BPF_MSH|BPF_B:
370 k = pc->k;
371 if (k >= buflen) {
372#ifdef KERNEL
373 register struct mbuf *m;
374
375 if (buflen != 0)
376 return 0;
377 m = (struct mbuf *)p;
378 MINDEX(m, k);
379 X = (mtod(m, char *)[k] & 0xf) << 2;
380 continue;
381#else
382 return 0;
383#endif
384 }
385 X = (p[pc->k] & 0xf) << 2;
386 continue;
387
388 case BPF_LD|BPF_IMM:
389 A = pc->k;
390 continue;
391
392 case BPF_LDX|BPF_IMM:
393 X = pc->k;
394 continue;
395
396 case BPF_LD|BPF_MEM:
397 A = mem[pc->k];
398 continue;
399
400 case BPF_LDX|BPF_MEM:
401 X = mem[pc->k];
402 continue;
403
404 case BPF_ST:
405 mem[pc->k] = A;
406 continue;
407
408 case BPF_STX:
409 mem[pc->k] = X;
410 continue;
411
412 case BPF_JMP|BPF_JA:
413 pc += pc->k;
414 continue;
415
416 case BPF_JMP|BPF_JGT|BPF_K:
417 pc += (A > pc->k) ? pc->jt : pc->jf;
418 continue;
419
420 case BPF_JMP|BPF_JGE|BPF_K:
421 pc += (A >= pc->k) ? pc->jt : pc->jf;
422 continue;
423
424 case BPF_JMP|BPF_JEQ|BPF_K:
425 pc += (A == pc->k) ? pc->jt : pc->jf;
426 continue;
427
428 case BPF_JMP|BPF_JSET|BPF_K:
429 pc += (A & pc->k) ? pc->jt : pc->jf;
430 continue;
431
432 case BPF_JMP|BPF_JGT|BPF_X:
433 pc += (A > X) ? pc->jt : pc->jf;
434 continue;
435
436 case BPF_JMP|BPF_JGE|BPF_X:
437 pc += (A >= X) ? pc->jt : pc->jf;
438 continue;
439
440 case BPF_JMP|BPF_JEQ|BPF_X:
441 pc += (A == X) ? pc->jt : pc->jf;
442 continue;
443
444 case BPF_JMP|BPF_JSET|BPF_X:
445 pc += (A & X) ? pc->jt : pc->jf;
446 continue;
447
448 case BPF_ALU|BPF_ADD|BPF_X:
449 A += X;
450 continue;
451
452 case BPF_ALU|BPF_SUB|BPF_X:
453 A -= X;
454 continue;
455
456 case BPF_ALU|BPF_MUL|BPF_X:
457 A *= X;
458 continue;
459
460 case BPF_ALU|BPF_DIV|BPF_X:
461 if (X == 0)
462 return 0;
463 A /= X;
464 continue;
465
466 case BPF_ALU|BPF_AND|BPF_X:
467 A &= X;
468 continue;
469
470 case BPF_ALU|BPF_OR|BPF_X:
471 A |= X;
472 continue;
473
474 case BPF_ALU|BPF_LSH|BPF_X:
475 A <<= X;
476 continue;
477
478 case BPF_ALU|BPF_RSH|BPF_X:
479 A >>= X;
480 continue;
481
482 case BPF_ALU|BPF_ADD|BPF_K:
483 A += pc->k;
484 continue;
485
486 case BPF_ALU|BPF_SUB|BPF_K:
487 A -= pc->k;
488 continue;
489
490 case BPF_ALU|BPF_MUL|BPF_K:
491 A *= pc->k;
492 continue;
493
494 case BPF_ALU|BPF_DIV|BPF_K:
495 A /= pc->k;
496 continue;
497
498 case BPF_ALU|BPF_AND|BPF_K:
499 A &= pc->k;
500 continue;
501
502 case BPF_ALU|BPF_OR|BPF_K:
503 A |= pc->k;
504 continue;
505
506 case BPF_ALU|BPF_LSH|BPF_K:
507 A <<= pc->k;
508 continue;
509
510 case BPF_ALU|BPF_RSH|BPF_K:
511 A >>= pc->k;
512 continue;
513
514 case BPF_ALU|BPF_NEG:
515 A = -A;
516 continue;
517
518 case BPF_MISC|BPF_TAX:
519 X = A;
520 continue;
521
522 case BPF_MISC|BPF_TXA:
523 A = X;
524 continue;
525 }
526 }
527}
528
529#ifdef KERNEL
530/*
531 * Return true if the 'fcode' is a valid filter program.
532 * The constraints are that each jump be forward and to a valid
533 * code. The code must terminate with either an accept or reject.
534 * 'valid' is an array for use by the routine (it must be at least
535 * 'len' bytes long).
536 *
537 * The kernel needs to be able to verify an application's filter code.
538 * Otherwise, a bogus program could easily crash the system.
539 */
540int
91447636 541bpf_validate(const struct bpf_insn *f, int len)
1c79356b
A
542{
543 register int i;
9bccf70c 544 const struct bpf_insn *p;
1c79356b
A
545
546 for (i = 0; i < len; ++i) {
547 /*
548 * Check that that jumps are forward, and within
549 * the code block.
550 */
551 p = &f[i];
552 if (BPF_CLASS(p->code) == BPF_JMP) {
553 register int from = i + 1;
554
555 if (BPF_OP(p->code) == BPF_JA) {
91447636 556 if (from >= len || p->k >= (bpf_u_int32)(len - from))
1c79356b
A
557 return 0;
558 }
9bccf70c
A
559 else if (from >= len || p->jt >= len - from ||
560 p->jf >= len - from)
1c79356b
A
561 return 0;
562 }
563 /*
564 * Check that memory operations use valid addresses.
565 */
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)
570 return 0;
571 /*
572 * Check for constant division by 0.
573 */
574 if (p->code == (BPF_ALU|BPF_DIV|BPF_K) && p->k == 0)
575 return 0;
576 }
577 return BPF_CLASS(f[len - 1].code) == BPF_RET;
578}
579#endif