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