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