xref: /netbsd-src/sys/net/bpf_filter.c (revision d9158b13b5dfe46201430699a3f7a235ecf28df3)
1 /*
2  * Copyright (c) 1990, 1991, 1993
3  *	The Regents of the University of California.  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  *
38  *	from: @(#)bpf_filter.c	8.1 (Berkeley) 6/10/93
39  *	$Id: bpf_filter.c,v 1.5 1994/05/13 06:02:22 mycroft Exp $
40  */
41 
42 #include <sys/param.h>
43 #include <sys/types.h>
44 #include <sys/time.h>
45 
46 #ifdef sun
47 #include <netinet/in.h>
48 #endif
49 
50 #if defined(sparc) || defined(mips) || defined(ibm032)
51 #define BPF_ALIGN
52 #endif
53 
54 #ifndef BPF_ALIGN
55 #define EXTRACT_SHORT(p)	((u_short)ntohs(*(u_short *)p))
56 #define EXTRACT_LONG(p)		(ntohl(*(u_long *)p))
57 #else
58 #define EXTRACT_SHORT(p)\
59 	((u_short)\
60 		((u_short)*((u_char *)p+0)<<8|\
61 		 (u_short)*((u_char *)p+1)<<0))
62 #define EXTRACT_LONG(p)\
63 		((u_long)*((u_char *)p+0)<<24|\
64 		 (u_long)*((u_char *)p+1)<<16|\
65 		 (u_long)*((u_char *)p+2)<<8|\
66 		 (u_long)*((u_char *)p+3)<<0)
67 #endif
68 
69 #ifdef KERNEL
70 #include <sys/mbuf.h>
71 #define MINDEX(m, k) \
72 { \
73 	register int len = m->m_len; \
74  \
75 	while (k >= len) { \
76 		k -= len; \
77 		m = m->m_next; \
78 		if (m == 0) \
79 			return 0; \
80 		len = m->m_len; \
81 	} \
82 }
83 
84 static int
85 m_xword(m, k, err)
86 	register struct mbuf *m;
87 	register int k, *err;
88 {
89 	register int len;
90 	register u_char *cp, *np;
91 	register struct mbuf *m0;
92 
93 	len = m->m_len;
94 	while (k >= len) {
95 		k -= len;
96 		m = m->m_next;
97 		if (m == 0)
98 			goto bad;
99 		len = m->m_len;
100 	}
101 	cp = mtod(m, u_char *) + k;
102 	if (len - k >= 4) {
103 		*err = 0;
104 		return EXTRACT_LONG(cp);
105 	}
106 	m0 = m->m_next;
107 	if (m0 == 0 || m0->m_len + len - k < 4)
108 		goto bad;
109 	*err = 0;
110 	np = mtod(m0, u_char *);
111 	switch (len - k) {
112 
113 	case 1:
114 		return (cp[k] << 24) | (np[0] << 16) | (np[1] << 8) | np[2];
115 
116 	case 2:
117 		return (cp[k] << 24) | (cp[k + 1] << 16) | (np[0] << 8) |
118 			np[1];
119 
120 	default:
121 		return (cp[k] << 24) | (cp[k + 1] << 16) | (cp[k + 2] << 8) |
122 			np[0];
123 	}
124     bad:
125 	*err = 1;
126 	return 0;
127 }
128 
129 static int
130 m_xhalf(m, k, err)
131 	register struct mbuf *m;
132 	register int k, *err;
133 {
134 	register int len;
135 	register u_char *cp;
136 	register struct mbuf *m0;
137 
138 	len = m->m_len;
139 	while (k >= len) {
140 		k -= len;
141 		m = m->m_next;
142 		if (m == 0)
143 			goto bad;
144 		len = m->m_len;
145 	}
146 	cp = mtod(m, u_char *) + k;
147 	if (len - k >= 2) {
148 		*err = 0;
149 		return EXTRACT_SHORT(cp);
150 	}
151 	m0 = m->m_next;
152 	if (m0 == 0)
153 		goto bad;
154 	*err = 0;
155 	return (cp[k] << 8) | mtod(m0, u_char *)[0];
156  bad:
157 	*err = 1;
158 	return 0;
159 }
160 #endif
161 
162 #include <net/bpf.h>
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  */
168 u_int
169 bpf_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 {
175 	register u_long A, X;
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  */
506 int
507 bpf_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
547