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