Home | History | Annotate | Line # | Download | only in net
bpf_filter.c revision 1.24
      1 /*	$NetBSD: bpf_filter.c,v 1.24 2005/11/30 23:14:38 rpaulo 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.24 2005/11/30 23:14:38 rpaulo 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 	while (1) {
    172 		++pc;
    173 		switch (pc->code) {
    174 
    175 		default:
    176 #ifdef _KERNEL
    177 			return 0;
    178 #else
    179 			abort();
    180 #endif
    181 		case BPF_RET|BPF_K:
    182 			return (u_int)pc->k;
    183 
    184 		case BPF_RET|BPF_A:
    185 			return (u_int)A;
    186 
    187 		case BPF_LD|BPF_W|BPF_ABS:
    188 			k = pc->k;
    189 			if (k + sizeof(int32_t) > buflen) {
    190 #ifdef _KERNEL
    191 				int merr;
    192 
    193 				if (buflen != 0)
    194 					return 0;
    195 				A = m_xword((struct mbuf *)p, k, &merr);
    196 				if (merr != 0)
    197 					return 0;
    198 				continue;
    199 #else
    200 				return 0;
    201 #endif
    202 			}
    203 			A = EXTRACT_LONG(&p[k]);
    204 			continue;
    205 
    206 		case BPF_LD|BPF_H|BPF_ABS:
    207 			k = pc->k;
    208 			if (k + sizeof(int16_t) > buflen) {
    209 #ifdef _KERNEL
    210 				int merr;
    211 
    212 				if (buflen != 0)
    213 					return 0;
    214 				A = m_xhalf((struct mbuf *)p, k, &merr);
    215 				continue;
    216 #else
    217 				return 0;
    218 #endif
    219 			}
    220 			A = EXTRACT_SHORT(&p[k]);
    221 			continue;
    222 
    223 		case BPF_LD|BPF_B|BPF_ABS:
    224 			k = pc->k;
    225 			if (k >= buflen) {
    226 #ifdef _KERNEL
    227 				struct mbuf *m;
    228 				int len;
    229 
    230 				if (buflen != 0)
    231 					return 0;
    232 				m = (struct mbuf *)p;
    233 				MINDEX(len, m, k);
    234 				A = mtod(m, u_char *)[k];
    235 				continue;
    236 #else
    237 				return 0;
    238 #endif
    239 			}
    240 			A = p[k];
    241 			continue;
    242 
    243 		case BPF_LD|BPF_W|BPF_LEN:
    244 			A = wirelen;
    245 			continue;
    246 
    247 		case BPF_LDX|BPF_W|BPF_LEN:
    248 			X = wirelen;
    249 			continue;
    250 
    251 		case BPF_LD|BPF_W|BPF_IND:
    252 			k = X + pc->k;
    253 			if (k + sizeof(int32_t) > buflen) {
    254 #ifdef _KERNEL
    255 				int merr;
    256 
    257 				if (buflen != 0)
    258 					return 0;
    259 				A = m_xword((struct mbuf *)p, k, &merr);
    260 				if (merr != 0)
    261 					return 0;
    262 				continue;
    263 #else
    264 				return 0;
    265 #endif
    266 			}
    267 			A = EXTRACT_LONG(&p[k]);
    268 			continue;
    269 
    270 		case BPF_LD|BPF_H|BPF_IND:
    271 			k = X + pc->k;
    272 			if (k + sizeof(int16_t) > buflen) {
    273 #ifdef _KERNEL
    274 				int merr;
    275 
    276 				if (buflen != 0)
    277 					return 0;
    278 				A = m_xhalf((struct mbuf *)p, k, &merr);
    279 				if (merr != 0)
    280 					return 0;
    281 				continue;
    282 #else
    283 				return 0;
    284 #endif
    285 			}
    286 			A = EXTRACT_SHORT(&p[k]);
    287 			continue;
    288 
    289 		case BPF_LD|BPF_B|BPF_IND:
    290 			k = X + pc->k;
    291 			if (k >= buflen) {
    292 #ifdef _KERNEL
    293 				struct mbuf *m;
    294 				int len;
    295 
    296 				if (buflen != 0)
    297 					return 0;
    298 				m = (struct mbuf *)p;
    299 				MINDEX(len, m, k);
    300 				A = mtod(m, u_char *)[k];
    301 				continue;
    302 #else
    303 				return 0;
    304 #endif
    305 			}
    306 			A = p[k];
    307 			continue;
    308 
    309 		case BPF_LDX|BPF_MSH|BPF_B:
    310 			k = pc->k;
    311 			if (k >= buflen) {
    312 #ifdef _KERNEL
    313 				struct mbuf *m;
    314 				int len;
    315 
    316 				if (buflen != 0)
    317 					return 0;
    318 				m = (struct mbuf *)p;
    319 				MINDEX(len, m, k);
    320 				X = (mtod(m, char *)[k] & 0xf) << 2;
    321 				continue;
    322 #else
    323 				return 0;
    324 #endif
    325 			}
    326 			X = (p[pc->k] & 0xf) << 2;
    327 			continue;
    328 
    329 		case BPF_LD|BPF_IMM:
    330 			A = pc->k;
    331 			continue;
    332 
    333 		case BPF_LDX|BPF_IMM:
    334 			X = pc->k;
    335 			continue;
    336 
    337 		case BPF_LD|BPF_MEM:
    338 			A = mem[pc->k];
    339 			continue;
    340 
    341 		case BPF_LDX|BPF_MEM:
    342 			X = mem[pc->k];
    343 			continue;
    344 
    345 		case BPF_ST:
    346 			mem[pc->k] = A;
    347 			continue;
    348 
    349 		case BPF_STX:
    350 			mem[pc->k] = X;
    351 			continue;
    352 
    353 		case BPF_JMP|BPF_JA:
    354 			pc += pc->k;
    355 			continue;
    356 
    357 		case BPF_JMP|BPF_JGT|BPF_K:
    358 			pc += (A > pc->k) ? pc->jt : pc->jf;
    359 			continue;
    360 
    361 		case BPF_JMP|BPF_JGE|BPF_K:
    362 			pc += (A >= pc->k) ? pc->jt : pc->jf;
    363 			continue;
    364 
    365 		case BPF_JMP|BPF_JEQ|BPF_K:
    366 			pc += (A == pc->k) ? pc->jt : pc->jf;
    367 			continue;
    368 
    369 		case BPF_JMP|BPF_JSET|BPF_K:
    370 			pc += (A & pc->k) ? pc->jt : pc->jf;
    371 			continue;
    372 
    373 		case BPF_JMP|BPF_JGT|BPF_X:
    374 			pc += (A > X) ? pc->jt : pc->jf;
    375 			continue;
    376 
    377 		case BPF_JMP|BPF_JGE|BPF_X:
    378 			pc += (A >= X) ? pc->jt : pc->jf;
    379 			continue;
    380 
    381 		case BPF_JMP|BPF_JEQ|BPF_X:
    382 			pc += (A == X) ? pc->jt : pc->jf;
    383 			continue;
    384 
    385 		case BPF_JMP|BPF_JSET|BPF_X:
    386 			pc += (A & X) ? pc->jt : pc->jf;
    387 			continue;
    388 
    389 		case BPF_ALU|BPF_ADD|BPF_X:
    390 			A += X;
    391 			continue;
    392 
    393 		case BPF_ALU|BPF_SUB|BPF_X:
    394 			A -= X;
    395 			continue;
    396 
    397 		case BPF_ALU|BPF_MUL|BPF_X:
    398 			A *= X;
    399 			continue;
    400 
    401 		case BPF_ALU|BPF_DIV|BPF_X:
    402 			if (X == 0)
    403 				return 0;
    404 			A /= X;
    405 			continue;
    406 
    407 		case BPF_ALU|BPF_AND|BPF_X:
    408 			A &= X;
    409 			continue;
    410 
    411 		case BPF_ALU|BPF_OR|BPF_X:
    412 			A |= X;
    413 			continue;
    414 
    415 		case BPF_ALU|BPF_LSH|BPF_X:
    416 			A <<= X;
    417 			continue;
    418 
    419 		case BPF_ALU|BPF_RSH|BPF_X:
    420 			A >>= X;
    421 			continue;
    422 
    423 		case BPF_ALU|BPF_ADD|BPF_K:
    424 			A += pc->k;
    425 			continue;
    426 
    427 		case BPF_ALU|BPF_SUB|BPF_K:
    428 			A -= pc->k;
    429 			continue;
    430 
    431 		case BPF_ALU|BPF_MUL|BPF_K:
    432 			A *= pc->k;
    433 			continue;
    434 
    435 		case BPF_ALU|BPF_DIV|BPF_K:
    436 			A /= pc->k;
    437 			continue;
    438 
    439 		case BPF_ALU|BPF_AND|BPF_K:
    440 			A &= pc->k;
    441 			continue;
    442 
    443 		case BPF_ALU|BPF_OR|BPF_K:
    444 			A |= pc->k;
    445 			continue;
    446 
    447 		case BPF_ALU|BPF_LSH|BPF_K:
    448 			A <<= pc->k;
    449 			continue;
    450 
    451 		case BPF_ALU|BPF_RSH|BPF_K:
    452 			A >>= pc->k;
    453 			continue;
    454 
    455 		case BPF_ALU|BPF_NEG:
    456 			A = -A;
    457 			continue;
    458 
    459 		case BPF_MISC|BPF_TAX:
    460 			X = A;
    461 			continue;
    462 
    463 		case BPF_MISC|BPF_TXA:
    464 			A = X;
    465 			continue;
    466 		}
    467 	}
    468 }
    469 
    470 #ifdef _KERNEL
    471 /*
    472  * Return true if the 'fcode' is a valid filter program.
    473  * The constraints are that each jump be forward and to a valid
    474  * code.  The code must terminate with either an accept or reject.
    475  * 'valid' is an array for use by the routine (it must be at least
    476  * 'len' bytes long).
    477  *
    478  * The kernel needs to be able to verify an application's filter code.
    479  * Otherwise, a bogus program could easily crash the system.
    480  */
    481 int
    482 bpf_validate(struct bpf_insn *f, int len)
    483 {
    484 	u_int i, from;
    485 	struct bpf_insn *p;
    486 
    487 
    488 	if (len < 1 || len > BPF_MAXINSNS)
    489 		return 0;
    490 
    491 	for (i = 0; i < len; ++i) {
    492 		p = &f[i];
    493 		switch (BPF_CLASS(p->code)) {
    494 		/*
    495 		 * Check that memory operations use valid addresses.
    496 		 */
    497 		case BPF_LD:
    498 		case BPF_LDX:
    499 			switch (BPF_MODE(p->code)) {
    500 			case BPF_ABS:
    501 			case BPF_IND:
    502 			case BPF_MSH:
    503 				/*
    504 				 * More strict check with actual packet length
    505 				 * is done runtime.
    506 				 */
    507 				if (p->k >= bpf_maxbufsize)
    508 					return 0;
    509 				break;
    510 			case BPF_MEM:
    511 				if (p->k >= BPF_MEMWORDS)
    512 					return 0;
    513 				break;
    514 			case BPF_IMM:
    515 			case BPF_LEN:
    516 				break;
    517 			default:
    518 				return 0;
    519 			}
    520 			break;
    521 		case BPF_ST:
    522 		case BPF_STX:
    523 			if (p->k >= BPF_MEMWORDS)
    524 				return 0;
    525 			break;
    526 		case BPF_ALU:
    527 			switch (BPF_OP(p->code)) {
    528 			case BPF_ADD:
    529 			case BPF_SUB:
    530 			case BPF_OR:
    531 			case BPF_AND:
    532 			case BPF_LSH:
    533 			case BPF_RSH:
    534 			case BPF_NEG:
    535 				break;
    536 			case BPF_DIV:
    537 				/*
    538 				 * Check for constant division by 0.
    539 				 */
    540 				if (BPF_RVAL(p->code) == BPF_K && p->k == 0)
    541 					return 0;
    542 			default:
    543 				return 0;
    544 			}
    545 			break;
    546 		case BPF_JMP:
    547 			/*
    548 			 * Check that jumps are within the code block,
    549 			 * and that unconditional branches don't go
    550 			 * backwards as a result of an overflow.
    551 			 * Unconditional branches have a 32-bit offset,
    552 			 * so they could overflow; we check to make
    553 			 * sure they don't.  Conditional branches have
    554 			 * an 8-bit offset, and the from address is <=
    555 			 * BPF_MAXINSNS, and we assume that BPF_MAXINSNS
    556 			 * is sufficiently small that adding 255 to it
    557 			 * won't overflow.
    558 			 *
    559 			 * We know that len is <= BPF_MAXINSNS, and we
    560 			 * assume that BPF_MAXINSNS is < the maximum size
    561 			 * of a u_int, so that i + 1 doesn't overflow.
    562 			 */
    563 			from = i + 1;
    564 			switch (BPF_OP(p->code)) {
    565 			case BPF_JA:
    566 				if (from + p->k < from || from + p->k >= len)
    567 					return 0;
    568 				break;
    569 			case BPF_JEQ:
    570 			case BPF_JGT:
    571 			case BPF_JGE:
    572 			case BPF_JSET:
    573 				if (from + p->jt >= len || from + p->jf >= len)
    574 					return 0;
    575 				break;
    576 			default:
    577 				return 0;
    578 			}
    579 			break;
    580 		case BPF_RET:
    581 			break;
    582 		case BPF_MISC:
    583 			break;
    584 		default:
    585 			return 0;
    586 		}
    587 	}
    588 
    589 	return BPF_CLASS(f[len - 1].code) == BPF_RET;
    590 }
    591 #endif
    592