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