Home | History | Annotate | Line # | Download | only in net
bpf_filter.c revision 1.7
      1 /*	$NetBSD: bpf_filter.c,v 1.7 1995/03/06 10:56:08 mycroft Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1990, 1991, 1992, 1993
      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/param.h>
     44 #include <sys/types.h>
     45 #include <sys/time.h>
     46 #include <net/bpf.h>
     47 
     48 #ifdef sun
     49 #include <netinet/in.h>
     50 #endif
     51 
     52 #if defined(sparc) || defined(mips) || defined(ibm032)
     53 #define BPF_ALIGN
     54 #endif
     55 
     56 #ifndef BPF_ALIGN
     57 #define EXTRACT_SHORT(p)	((u_short)ntohs(*(u_short *)p))
     58 #define EXTRACT_LONG(p)		(ntohl(*(u_int32_t *)p))
     59 #else
     60 #define EXTRACT_SHORT(p)\
     61 	((u_short)\
     62 		((u_short)*((u_char *)p+0)<<8|\
     63 		 (u_short)*((u_char *)p+1)<<0))
     64 #define EXTRACT_LONG(p)\
     65 		((u_int32_t)*((u_char *)p+0)<<24|\
     66 		 (u_int32_t)*((u_char *)p+1)<<16|\
     67 		 (u_int32_t)*((u_char *)p+2)<<8|\
     68 		 (u_int32_t)*((u_char *)p+3)<<0)
     69 #endif
     70 
     71 #ifdef KERNEL
     72 #include <sys/mbuf.h>
     73 #define MINDEX(m, k) \
     74 { \
     75 	register int len = m->m_len; \
     76  \
     77 	while (k >= len) { \
     78 		k -= len; \
     79 		m = m->m_next; \
     80 		if (m == 0) \
     81 			return 0; \
     82 		len = m->m_len; \
     83 	} \
     84 }
     85 
     86 static int
     87 m_xword(m, k, err)
     88 	register struct mbuf *m;
     89 	register int k, *err;
     90 {
     91 	register int len;
     92 	register u_char *cp, *np;
     93 	register struct mbuf *m0;
     94 
     95 	len = m->m_len;
     96 	while (k >= len) {
     97 		k -= len;
     98 		m = m->m_next;
     99 		if (m == 0)
    100 			goto bad;
    101 		len = m->m_len;
    102 	}
    103 	cp = mtod(m, u_char *) + k;
    104 	if (len - k >= 4) {
    105 		*err = 0;
    106 		return EXTRACT_LONG(cp);
    107 	}
    108 	m0 = m->m_next;
    109 	if (m0 == 0 || m0->m_len + len - k < 4)
    110 		goto bad;
    111 	*err = 0;
    112 	np = mtod(m0, u_char *);
    113 	switch (len - k) {
    114 
    115 	case 1:
    116 		return (cp[k] << 24) | (np[0] << 16) | (np[1] << 8) | np[2];
    117 
    118 	case 2:
    119 		return (cp[k] << 24) | (cp[k + 1] << 16) | (np[0] << 8) |
    120 			np[1];
    121 
    122 	default:
    123 		return (cp[k] << 24) | (cp[k + 1] << 16) | (cp[k + 2] << 8) |
    124 			np[0];
    125 	}
    126     bad:
    127 	*err = 1;
    128 	return 0;
    129 }
    130 
    131 static int
    132 m_xhalf(m, k, err)
    133 	register struct mbuf *m;
    134 	register int k, *err;
    135 {
    136 	register int len;
    137 	register u_char *cp;
    138 	register struct mbuf *m0;
    139 
    140 	len = m->m_len;
    141 	while (k >= len) {
    142 		k -= len;
    143 		m = m->m_next;
    144 		if (m == 0)
    145 			goto bad;
    146 		len = m->m_len;
    147 	}
    148 	cp = mtod(m, u_char *) + k;
    149 	if (len - k >= 2) {
    150 		*err = 0;
    151 		return EXTRACT_SHORT(cp);
    152 	}
    153 	m0 = m->m_next;
    154 	if (m0 == 0)
    155 		goto bad;
    156 	*err = 0;
    157 	return (cp[k] << 8) | mtod(m0, u_char *)[0];
    158  bad:
    159 	*err = 1;
    160 	return 0;
    161 }
    162 #endif
    163 
    164 /*
    165  * Execute the filter program starting at pc on the packet p
    166  * wirelen is the length of the original packet
    167  * buflen is the amount of data present
    168  */
    169 u_int
    170 bpf_filter(pc, p, wirelen, buflen)
    171 	register struct bpf_insn *pc;
    172 	register u_char *p;
    173 	u_int wirelen;
    174 	register u_int buflen;
    175 {
    176 	register u_int32_t A, X;
    177 	register int k;
    178 	int32_t mem[BPF_MEMWORDS];
    179 
    180 	if (pc == 0)
    181 		/*
    182 		 * No filter means accept all.
    183 		 */
    184 		return (u_int)-1;
    185 #ifdef lint
    186 	A = 0;
    187 	X = 0;
    188 #endif
    189 	--pc;
    190 	while (1) {
    191 		++pc;
    192 		switch (pc->code) {
    193 
    194 		default:
    195 #ifdef KERNEL
    196 			return 0;
    197 #else
    198 			abort();
    199 #endif
    200 		case BPF_RET|BPF_K:
    201 			return (u_int)pc->k;
    202 
    203 		case BPF_RET|BPF_A:
    204 			return (u_int)A;
    205 
    206 		case BPF_LD|BPF_W|BPF_ABS:
    207 			k = pc->k;
    208 			if (k + sizeof(int32_t) > buflen) {
    209 #ifdef KERNEL
    210 				int merr;
    211 
    212 				if (buflen != 0)
    213 					return 0;
    214 				A = m_xword((struct mbuf *)p, k, &merr);
    215 				if (merr != 0)
    216 					return 0;
    217 				continue;
    218 #else
    219 				return 0;
    220 #endif
    221 			}
    222 			A = EXTRACT_LONG(&p[k]);
    223 			continue;
    224 
    225 		case BPF_LD|BPF_H|BPF_ABS:
    226 			k = pc->k;
    227 			if (k + sizeof(short) > buflen) {
    228 #ifdef KERNEL
    229 				int merr;
    230 
    231 				if (buflen != 0)
    232 					return 0;
    233 				A = m_xhalf((struct mbuf *)p, k, &merr);
    234 				continue;
    235 #else
    236 				return 0;
    237 #endif
    238 			}
    239 			A = EXTRACT_SHORT(&p[k]);
    240 			continue;
    241 
    242 		case BPF_LD|BPF_B|BPF_ABS:
    243 			k = pc->k;
    244 			if (k >= buflen) {
    245 #ifdef KERNEL
    246 				register struct mbuf *m;
    247 
    248 				if (buflen != 0)
    249 					return 0;
    250 				m = (struct mbuf *)p;
    251 				MINDEX(m, k);
    252 				A = mtod(m, u_char *)[k];
    253 				continue;
    254 #else
    255 				return 0;
    256 #endif
    257 			}
    258 			A = p[k];
    259 			continue;
    260 
    261 		case BPF_LD|BPF_W|BPF_LEN:
    262 			A = wirelen;
    263 			continue;
    264 
    265 		case BPF_LDX|BPF_W|BPF_LEN:
    266 			X = wirelen;
    267 			continue;
    268 
    269 		case BPF_LD|BPF_W|BPF_IND:
    270 			k = X + pc->k;
    271 			if (k + sizeof(int32_t) > buflen) {
    272 #ifdef KERNEL
    273 				int merr;
    274 
    275 				if (buflen != 0)
    276 					return 0;
    277 				A = m_xword((struct mbuf *)p, k, &merr);
    278 				if (merr != 0)
    279 					return 0;
    280 				continue;
    281 #else
    282 				return 0;
    283 #endif
    284 			}
    285 			A = EXTRACT_LONG(&p[k]);
    286 			continue;
    287 
    288 		case BPF_LD|BPF_H|BPF_IND:
    289 			k = X + pc->k;
    290 			if (k + sizeof(short) > buflen) {
    291 #ifdef KERNEL
    292 				int merr;
    293 
    294 				if (buflen != 0)
    295 					return 0;
    296 				A = m_xhalf((struct mbuf *)p, k, &merr);
    297 				if (merr != 0)
    298 					return 0;
    299 				continue;
    300 #else
    301 				return 0;
    302 #endif
    303 			}
    304 			A = EXTRACT_SHORT(&p[k]);
    305 			continue;
    306 
    307 		case BPF_LD|BPF_B|BPF_IND:
    308 			k = X + pc->k;
    309 			if (k >= buflen) {
    310 #ifdef KERNEL
    311 				register struct mbuf *m;
    312 
    313 				if (buflen != 0)
    314 					return 0;
    315 				m = (struct mbuf *)p;
    316 				MINDEX(m, k);
    317 				A = mtod(m, char *)[k];
    318 				continue;
    319 #else
    320 				return 0;
    321 #endif
    322 			}
    323 			A = p[k];
    324 			continue;
    325 
    326 		case BPF_LDX|BPF_MSH|BPF_B:
    327 			k = pc->k;
    328 			if (k >= buflen) {
    329 #ifdef KERNEL
    330 				register struct mbuf *m;
    331 
    332 				if (buflen != 0)
    333 					return 0;
    334 				m = (struct mbuf *)p;
    335 				MINDEX(m, k);
    336 				X = (mtod(m, char *)[k] & 0xf) << 2;
    337 				continue;
    338 #else
    339 				return 0;
    340 #endif
    341 			}
    342 			X = (p[pc->k] & 0xf) << 2;
    343 			continue;
    344 
    345 		case BPF_LD|BPF_IMM:
    346 			A = pc->k;
    347 			continue;
    348 
    349 		case BPF_LDX|BPF_IMM:
    350 			X = pc->k;
    351 			continue;
    352 
    353 		case BPF_LD|BPF_MEM:
    354 			A = mem[pc->k];
    355 			continue;
    356 
    357 		case BPF_LDX|BPF_MEM:
    358 			X = mem[pc->k];
    359 			continue;
    360 
    361 		case BPF_ST:
    362 			mem[pc->k] = A;
    363 			continue;
    364 
    365 		case BPF_STX:
    366 			mem[pc->k] = X;
    367 			continue;
    368 
    369 		case BPF_JMP|BPF_JA:
    370 			pc += pc->k;
    371 			continue;
    372 
    373 		case BPF_JMP|BPF_JGT|BPF_K:
    374 			pc += (A > pc->k) ? pc->jt : pc->jf;
    375 			continue;
    376 
    377 		case BPF_JMP|BPF_JGE|BPF_K:
    378 			pc += (A >= pc->k) ? pc->jt : pc->jf;
    379 			continue;
    380 
    381 		case BPF_JMP|BPF_JEQ|BPF_K:
    382 			pc += (A == pc->k) ? pc->jt : pc->jf;
    383 			continue;
    384 
    385 		case BPF_JMP|BPF_JSET|BPF_K:
    386 			pc += (A & pc->k) ? pc->jt : pc->jf;
    387 			continue;
    388 
    389 		case BPF_JMP|BPF_JGT|BPF_X:
    390 			pc += (A > X) ? pc->jt : pc->jf;
    391 			continue;
    392 
    393 		case BPF_JMP|BPF_JGE|BPF_X:
    394 			pc += (A >= X) ? pc->jt : pc->jf;
    395 			continue;
    396 
    397 		case BPF_JMP|BPF_JEQ|BPF_X:
    398 			pc += (A == X) ? pc->jt : pc->jf;
    399 			continue;
    400 
    401 		case BPF_JMP|BPF_JSET|BPF_X:
    402 			pc += (A & X) ? pc->jt : pc->jf;
    403 			continue;
    404 
    405 		case BPF_ALU|BPF_ADD|BPF_X:
    406 			A += X;
    407 			continue;
    408 
    409 		case BPF_ALU|BPF_SUB|BPF_X:
    410 			A -= X;
    411 			continue;
    412 
    413 		case BPF_ALU|BPF_MUL|BPF_X:
    414 			A *= X;
    415 			continue;
    416 
    417 		case BPF_ALU|BPF_DIV|BPF_X:
    418 			if (X == 0)
    419 				return 0;
    420 			A /= X;
    421 			continue;
    422 
    423 		case BPF_ALU|BPF_AND|BPF_X:
    424 			A &= X;
    425 			continue;
    426 
    427 		case BPF_ALU|BPF_OR|BPF_X:
    428 			A |= X;
    429 			continue;
    430 
    431 		case BPF_ALU|BPF_LSH|BPF_X:
    432 			A <<= X;
    433 			continue;
    434 
    435 		case BPF_ALU|BPF_RSH|BPF_X:
    436 			A >>= X;
    437 			continue;
    438 
    439 		case BPF_ALU|BPF_ADD|BPF_K:
    440 			A += pc->k;
    441 			continue;
    442 
    443 		case BPF_ALU|BPF_SUB|BPF_K:
    444 			A -= pc->k;
    445 			continue;
    446 
    447 		case BPF_ALU|BPF_MUL|BPF_K:
    448 			A *= pc->k;
    449 			continue;
    450 
    451 		case BPF_ALU|BPF_DIV|BPF_K:
    452 			A /= pc->k;
    453 			continue;
    454 
    455 		case BPF_ALU|BPF_AND|BPF_K:
    456 			A &= pc->k;
    457 			continue;
    458 
    459 		case BPF_ALU|BPF_OR|BPF_K:
    460 			A |= pc->k;
    461 			continue;
    462 
    463 		case BPF_ALU|BPF_LSH|BPF_K:
    464 			A <<= pc->k;
    465 			continue;
    466 
    467 		case BPF_ALU|BPF_RSH|BPF_K:
    468 			A >>= pc->k;
    469 			continue;
    470 
    471 		case BPF_ALU|BPF_NEG:
    472 			A = -A;
    473 			continue;
    474 
    475 		case BPF_MISC|BPF_TAX:
    476 			X = A;
    477 			continue;
    478 
    479 		case BPF_MISC|BPF_TXA:
    480 			A = X;
    481 			continue;
    482 		}
    483 	}
    484 }
    485 
    486 #ifdef KERNEL
    487 /*
    488  * Return true if the 'fcode' is a valid filter program.
    489  * The constraints are that each jump be forward and to a valid
    490  * code.  The code must terminate with either an accept or reject.
    491  * 'valid' is an array for use by the routine (it must be at least
    492  * 'len' bytes long).
    493  *
    494  * The kernel needs to be able to verify an application's filter code.
    495  * Otherwise, a bogus program could easily crash the system.
    496  */
    497 int
    498 bpf_validate(f, len)
    499 	struct bpf_insn *f;
    500 	int len;
    501 {
    502 	register int i;
    503 	register struct bpf_insn *p;
    504 
    505 	for (i = 0; i < len; ++i) {
    506 		/*
    507 		 * Check that that jumps are forward, and within
    508 		 * the code block.
    509 		 */
    510 		p = &f[i];
    511 		if (BPF_CLASS(p->code) == BPF_JMP) {
    512 			register int from = i + 1;
    513 
    514 			if (BPF_OP(p->code) == BPF_JA) {
    515 				if (from + p->k >= len)
    516 					return 0;
    517 			}
    518 			else if (from + p->jt >= len || from + p->jf >= len)
    519 				return 0;
    520 		}
    521 		/*
    522 		 * Check that memory operations use valid addresses.
    523 		 */
    524 		if ((BPF_CLASS(p->code) == BPF_ST ||
    525 		     (BPF_CLASS(p->code) == BPF_LD &&
    526 		      (p->code & 0xe0) == BPF_MEM)) &&
    527 		    (p->k >= BPF_MEMWORDS || p->k < 0))
    528 			return 0;
    529 		/*
    530 		 * Check for constant division by 0.
    531 		 */
    532 		if (p->code == (BPF_ALU|BPF_DIV|BPF_K) && p->k == 0)
    533 			return 0;
    534 	}
    535 	return BPF_CLASS(f[len - 1].code) == BPF_RET;
    536 }
    537 #endif
    538