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