Home | History | Annotate | Line # | Download | only in net
      1 /*	$NetBSD: bpf_filter.c,v 1.74 2026/09/07 15:47:16 tls 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.74 2026/09/07 15:47:16 tls 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/atomic.h>
     51 
     52 #include <sys/time.h>
     53 #include <sys/kmem.h>
     54 #include <sys/endian.h>
     55 
     56 #ifdef _KERNEL
     57 #include <sys/module.h>
     58 #endif
     59 
     60 #define	__BPF_PRIVATE
     61 #include <net/bpf.h>
     62 
     63 #ifdef _KERNEL
     64 
     65 bpf_ctx_t *
     66 bpf_create(void)
     67 {
     68 	return kmem_zalloc(sizeof(bpf_ctx_t), KM_SLEEP);
     69 }
     70 
     71 void
     72 bpf_destroy(bpf_ctx_t *bc)
     73 {
     74 	kmem_free(bc, sizeof(bpf_ctx_t));
     75 }
     76 
     77 int
     78 bpf_set_cop(bpf_ctx_t *bc, const bpf_copfunc_t *funcs, size_t n)
     79 {
     80 	bc->copfuncs = funcs;
     81 	bc->nfuncs = n;
     82 	return 0;
     83 }
     84 
     85 int
     86 bpf_set_extmem(bpf_ctx_t *bc, size_t nwords, bpf_memword_init_t preinited)
     87 {
     88 	if (nwords > BPF_MAX_MEMWORDS || (preinited >> nwords) != 0) {
     89 		return EINVAL;
     90 	}
     91 	bc->extwords = nwords;
     92 	bc->preinited = preinited;
     93 	return 0;
     94 }
     95 
     96 #endif
     97 
     98 #define EXTRACT_SHORT(p)	be16dec(p)
     99 #define EXTRACT_LONG(p)		be32dec(p)
    100 
    101 #ifdef _KERNEL
    102 #include <sys/mbuf.h>
    103 #define MINDEX(len, m, k) 		\
    104 {					\
    105 	len = m->m_len; 		\
    106 	while (k >= len) { 		\
    107 		k -= len; 		\
    108 		m = m->m_next; 		\
    109 		if (m == 0) 		\
    110 			return 0; 	\
    111 		len = m->m_len; 	\
    112 	}				\
    113 }
    114 
    115 uint32_t m_xword(const struct mbuf *, uint32_t, int *);
    116 uint32_t m_xhalf(const struct mbuf *, uint32_t, int *);
    117 uint32_t m_xbyte(const struct mbuf *, uint32_t, int *);
    118 
    119 #define xword(p, k, err) m_xword((const struct mbuf *)(p), (k), (err))
    120 #define xhalf(p, k, err) m_xhalf((const struct mbuf *)(p), (k), (err))
    121 #define xbyte(p, k, err) m_xbyte((const struct mbuf *)(p), (k), (err))
    122 
    123 uint32_t
    124 m_xword(const struct mbuf *m, uint32_t k, int *err)
    125 {
    126 	int len;
    127 	u_char *cp, *np;
    128 	struct mbuf *m0;
    129 
    130 	*err = 1;
    131 	MINDEX(len, m, k);
    132 	cp = mtod(m, u_char *) + k;
    133 	if (len - k >= 4) {
    134 		*err = 0;
    135 		return EXTRACT_LONG(cp);
    136 	}
    137 	m0 = m->m_next;
    138 	if (m0 == 0 || (len - k) + m0->m_len < 4)
    139 		return 0;
    140 	*err = 0;
    141 	np = mtod(m0, u_char *);
    142 
    143 	switch (len - k) {
    144 	case 1:
    145 		return (cp[0] << 24) | (np[0] << 16) | (np[1] << 8) | np[2];
    146 	case 2:
    147 		return (cp[0] << 24) | (cp[1] << 16) | (np[0] << 8) | np[1];
    148 	default:
    149 		return (cp[0] << 24) | (cp[1] << 16) | (cp[2] << 8) | np[0];
    150 	}
    151 }
    152 
    153 uint32_t
    154 m_xhalf(const struct mbuf *m, uint32_t k, int *err)
    155 {
    156 	int len;
    157 	u_char *cp;
    158 	struct mbuf *m0;
    159 
    160 	*err = 1;
    161 	MINDEX(len, m, k);
    162 	cp = mtod(m, u_char *) + k;
    163 	if (len - k >= 2) {
    164 		*err = 0;
    165 		return EXTRACT_SHORT(cp);
    166 	}
    167 	m0 = m->m_next;
    168 	if (m0 == 0)
    169 		return 0;
    170 	*err = 0;
    171 	return (cp[0] << 8) | mtod(m0, u_char *)[0];
    172 }
    173 
    174 uint32_t
    175 m_xbyte(const struct mbuf *m, uint32_t k, int *err)
    176 {
    177 	int len;
    178 
    179 	*err = 1;
    180 	MINDEX(len, m, k);
    181 	*err = 0;
    182 	return mtod(m, u_char *)[k];
    183 }
    184 #else /* _KERNEL */
    185 #include <stdlib.h>
    186 #endif /* !_KERNEL */
    187 
    188 #include <net/bpf.h>
    189 
    190 /*
    191  * Execute the filter program starting at pc on the packet p
    192  * wirelen is the length of the original packet
    193  * buflen is the amount of data present
    194  */
    195 #ifdef _KERNEL
    196 
    197 u_int
    198 bpf_filter(const struct bpf_insn *pc, const u_char *p, u_int wirelen,
    199     u_int buflen)
    200 {
    201 	uint32_t mem[BPF_MEMWORDS];
    202 	bpf_args_t args = {
    203 		.pkt = p,
    204 		.wirelen = wirelen,
    205 		.buflen = buflen,
    206 		.mem = mem,
    207 		.arg = NULL
    208 	};
    209 
    210 	return bpf_filter_ext(NULL, pc, &args);
    211 }
    212 
    213 u_int
    214 bpf_filter_ext(const bpf_ctx_t *bc, const struct bpf_insn *pc, bpf_args_t *args)
    215 #else
    216 __strong_alias(pcapint_filter, bpf_filter)
    217 u_int
    218 bpf_filter(const struct bpf_insn *pc, const u_char *p, u_int wirelen,
    219     u_int buflen)
    220 #endif
    221 {
    222 	uint32_t A, X, k;
    223 #ifndef _KERNEL
    224 	uint32_t mem[BPF_MEMWORDS];
    225 	bpf_args_t args_store = {
    226 		.pkt = p,
    227 		.wirelen = wirelen,
    228 		.buflen = buflen,
    229 		.mem = mem,
    230 		.arg = NULL
    231 	};
    232 	bpf_args_t * const args = &args_store;
    233 #else
    234 	const uint8_t * const p = args->pkt;
    235 #endif
    236 	if (pc == 0) {
    237 		/*
    238 		 * No filter means accept all.
    239 		 */
    240 		return (u_int)-1;
    241 	}
    242 
    243 	/*
    244 	 * Note: safe to leave memwords uninitialised, as the validation
    245 	 * step ensures that it will not be read, if it was not written.
    246 	 */
    247 	A = 0;
    248 	X = 0;
    249 	--pc;
    250 
    251 	for (;;) {
    252 		++pc;
    253 		switch (pc->code) {
    254 
    255 		default:
    256 #ifdef _KERNEL
    257 			return 0;
    258 #else
    259 			abort();
    260 			/*NOTREACHED*/
    261 #endif
    262 		case BPF_RET|BPF_K:
    263 			return (u_int)pc->k;
    264 
    265 		case BPF_RET|BPF_A:
    266 			return (u_int)A;
    267 
    268 		case BPF_LD|BPF_W|BPF_ABS:
    269 			k = pc->k;
    270 			if (k > args->buflen ||
    271 			    sizeof(int32_t) > args->buflen - k) {
    272 #ifdef _KERNEL
    273 				int merr;
    274 
    275 				if (args->buflen != 0)
    276 					return 0;
    277 				A = xword(args->pkt, 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_ABS:
    289 			k = pc->k;
    290 			if (k > args->buflen ||
    291 			    sizeof(int16_t) > args->buflen - k) {
    292 #ifdef _KERNEL
    293 				int merr;
    294 
    295 				if (args->buflen != 0)
    296 					return 0;
    297 				A = xhalf(args->pkt, 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_ABS:
    309 			k = pc->k;
    310 			if (k >= args->buflen) {
    311 #ifdef _KERNEL
    312 				int merr;
    313 
    314 				if (args->buflen != 0)
    315 					return 0;
    316 				A = xbyte(args->pkt, k, &merr);
    317 				if (merr != 0)
    318 					return 0;
    319 				continue;
    320 #else
    321 				return 0;
    322 #endif
    323 			}
    324 			A = p[k];
    325 			continue;
    326 
    327 		case BPF_LD|BPF_W|BPF_LEN:
    328 			A = args->wirelen;
    329 			continue;
    330 
    331 		case BPF_LDX|BPF_W|BPF_LEN:
    332 			X = args->wirelen;
    333 			continue;
    334 
    335 		case BPF_LD|BPF_W|BPF_IND:
    336 			k = X + pc->k;
    337 			if (k < X || k >= args->buflen ||
    338 			    sizeof(int32_t) > args->buflen - k) {
    339 #ifdef _KERNEL
    340 				int merr;
    341 
    342 				if (k < X || args->buflen != 0)
    343 					return 0;
    344 				A = xword(args->pkt, k, &merr);
    345 				if (merr != 0)
    346 					return 0;
    347 				continue;
    348 #else
    349 				return 0;
    350 #endif
    351 			}
    352 			A = EXTRACT_LONG(&p[k]);
    353 			continue;
    354 
    355 		case BPF_LD|BPF_H|BPF_IND:
    356 			k = X + pc->k;
    357 			if (k < X || k >= args->buflen ||
    358 			    sizeof(int16_t) > args->buflen - k) {
    359 #ifdef _KERNEL
    360 				int merr;
    361 
    362 				if (k < X || args->buflen != 0)
    363 					return 0;
    364 				A = xhalf(args->pkt, k, &merr);
    365 				if (merr != 0)
    366 					return 0;
    367 				continue;
    368 #else
    369 				return 0;
    370 #endif
    371 			}
    372 			A = EXTRACT_SHORT(&p[k]);
    373 			continue;
    374 
    375 		case BPF_LD|BPF_B|BPF_IND:
    376 			k = X + pc->k;
    377 			if (k < X || k >= args->buflen) {
    378 #ifdef _KERNEL
    379 				int merr;
    380 
    381 				if (k < X || args->buflen != 0)
    382 					return 0;
    383 				A = xbyte(args->pkt, k, &merr);
    384 				if (merr != 0)
    385 					return 0;
    386 				continue;
    387 #else
    388 				return 0;
    389 #endif
    390 			}
    391 			A = p[k];
    392 			continue;
    393 
    394 		case BPF_LDX|BPF_MSH|BPF_B:
    395 			k = pc->k;
    396 			if (k >= args->buflen) {
    397 #ifdef _KERNEL
    398 				int merr;
    399 
    400 				if (args->buflen != 0)
    401 					return 0;
    402 				X = (xbyte(args->pkt, k, &merr) & 0xf) << 2;
    403 				if (merr != 0)
    404 					return 0;
    405 				continue;
    406 #else
    407 				return 0;
    408 #endif
    409 			}
    410 			X = (p[pc->k] & 0xf) << 2;
    411 			continue;
    412 
    413 		case BPF_LD|BPF_IMM:
    414 			A = pc->k;
    415 			continue;
    416 
    417 		case BPF_LDX|BPF_IMM:
    418 			X = pc->k;
    419 			continue;
    420 
    421 		case BPF_LD|BPF_MEM:
    422 			A = args->mem[pc->k];
    423 			continue;
    424 
    425 		case BPF_LDX|BPF_MEM:
    426 			X = args->mem[pc->k];
    427 			continue;
    428 
    429 		case BPF_ST:
    430 			args->mem[pc->k] = A;
    431 			continue;
    432 
    433 		case BPF_STX:
    434 			args->mem[pc->k] = X;
    435 			continue;
    436 
    437 		case BPF_JMP|BPF_JA:
    438 			pc += pc->k;
    439 			continue;
    440 
    441 		case BPF_JMP|BPF_JGT|BPF_K:
    442 			pc += (A > pc->k) ? pc->jt : pc->jf;
    443 			continue;
    444 
    445 		case BPF_JMP|BPF_JGE|BPF_K:
    446 			pc += (A >= pc->k) ? pc->jt : pc->jf;
    447 			continue;
    448 
    449 		case BPF_JMP|BPF_JEQ|BPF_K:
    450 			pc += (A == pc->k) ? pc->jt : pc->jf;
    451 			continue;
    452 
    453 		case BPF_JMP|BPF_JSET|BPF_K:
    454 			pc += (A & pc->k) ? pc->jt : pc->jf;
    455 			continue;
    456 
    457 		case BPF_JMP|BPF_JGT|BPF_X:
    458 			pc += (A > X) ? pc->jt : pc->jf;
    459 			continue;
    460 
    461 		case BPF_JMP|BPF_JGE|BPF_X:
    462 			pc += (A >= X) ? pc->jt : pc->jf;
    463 			continue;
    464 
    465 		case BPF_JMP|BPF_JEQ|BPF_X:
    466 			pc += (A == X) ? pc->jt : pc->jf;
    467 			continue;
    468 
    469 		case BPF_JMP|BPF_JSET|BPF_X:
    470 			pc += (A & X) ? pc->jt : pc->jf;
    471 			continue;
    472 
    473 		case BPF_ALU|BPF_ADD|BPF_X:
    474 			A += X;
    475 			continue;
    476 
    477 		case BPF_ALU|BPF_SUB|BPF_X:
    478 			A -= X;
    479 			continue;
    480 
    481 		case BPF_ALU|BPF_MUL|BPF_X:
    482 			A *= X;
    483 			continue;
    484 
    485 		case BPF_ALU|BPF_DIV|BPF_X:
    486 			if (X == 0)
    487 				return 0;
    488 			A /= X;
    489 			continue;
    490 
    491 		case BPF_ALU|BPF_MOD|BPF_X:
    492 			if (X == 0)
    493 				return 0;
    494 			A %= X;
    495 			continue;
    496 
    497 		case BPF_ALU|BPF_AND|BPF_X:
    498 			A &= X;
    499 			continue;
    500 
    501 		case BPF_ALU|BPF_OR|BPF_X:
    502 			A |= X;
    503 			continue;
    504 
    505 		case BPF_ALU|BPF_XOR|BPF_X:
    506 			A ^= X;
    507 			continue;
    508 
    509 		case BPF_ALU|BPF_LSH|BPF_X:
    510 			A <<= X;
    511 			continue;
    512 
    513 		case BPF_ALU|BPF_RSH|BPF_X:
    514 			A >>= X;
    515 			continue;
    516 
    517 		case BPF_ALU|BPF_ADD|BPF_K:
    518 			A += pc->k;
    519 			continue;
    520 
    521 		case BPF_ALU|BPF_SUB|BPF_K:
    522 			A -= pc->k;
    523 			continue;
    524 
    525 		case BPF_ALU|BPF_MUL|BPF_K:
    526 			A *= pc->k;
    527 			continue;
    528 
    529 		case BPF_ALU|BPF_DIV|BPF_K:
    530 			A /= pc->k;
    531 			continue;
    532 
    533 		case BPF_ALU|BPF_MOD|BPF_K:
    534 			A %= pc->k;
    535 			continue;
    536 
    537 		case BPF_ALU|BPF_AND|BPF_K:
    538 			A &= pc->k;
    539 			continue;
    540 
    541 		case BPF_ALU|BPF_OR|BPF_K:
    542 			A |= pc->k;
    543 			continue;
    544 
    545 		case BPF_ALU|BPF_XOR|BPF_K:
    546 			A ^= pc->k;
    547 			continue;
    548 
    549 		case BPF_ALU|BPF_LSH|BPF_K:
    550 			A <<= pc->k;
    551 			continue;
    552 
    553 		case BPF_ALU|BPF_RSH|BPF_K:
    554 			A >>= pc->k;
    555 			continue;
    556 
    557 		case BPF_ALU|BPF_NEG:
    558 			A = -A;
    559 			continue;
    560 
    561 		case BPF_MISC|BPF_TAX:
    562 			X = A;
    563 			continue;
    564 
    565 		case BPF_MISC|BPF_TXA:
    566 			A = X;
    567 			continue;
    568 
    569 		case BPF_MISC|BPF_COP:
    570 #ifdef _KERNEL
    571 			if (pc->k < bc->nfuncs) {
    572 				const bpf_copfunc_t fn = bc->copfuncs[pc->k];
    573 				A = fn(bc, args, A);
    574 				continue;
    575 			}
    576 #endif
    577 			return 0;
    578 
    579 		case BPF_MISC|BPF_COPX:
    580 #ifdef _KERNEL
    581 			if (X < bc->nfuncs) {
    582 				const bpf_copfunc_t fn = bc->copfuncs[X];
    583 				A = fn(bc, args, A);
    584 				continue;
    585 			}
    586 #endif
    587 			return 0;
    588 		}
    589 	}
    590 }
    591 
    592 /*
    593  * Return true if the 'fcode' is a valid filter program.
    594  * The constraints are that each jump be forward and to a valid
    595  * code, that memory accesses are within valid ranges (to the
    596  * extent that this can be checked statically; loads of packet
    597  * data have to be, and are, also checked at run time), and that
    598  * the code terminates with either an accept or reject.
    599  *
    600  * The kernel needs to be able to verify an application's filter code.
    601  * Otherwise, a bogus program could easily crash the system.
    602  */
    603 
    604 #if defined(KERNEL) || defined(_KERNEL)
    605 
    606 int
    607 bpf_validate(const struct bpf_insn *f, int signed_len)
    608 {
    609 	return bpf_validate_ext(NULL, f, signed_len);
    610 }
    611 
    612 int
    613 bpf_validate_ext(const bpf_ctx_t *bc, const struct bpf_insn *f, int signed_len)
    614 #else
    615 __strong_alias(pcapint_validate_filter, bpf_validate)
    616 int
    617 bpf_validate(const struct bpf_insn *f, int signed_len)
    618 #endif
    619 {
    620 	u_int i, from, len, ok = 0;
    621 	const struct bpf_insn *p;
    622 #if defined(KERNEL) || defined(_KERNEL)
    623 	bpf_memword_init_t *mem, invalid;
    624 	size_t size;
    625 	const size_t extwords = bc ? bc->extwords : 0;
    626 	const size_t memwords = extwords ? extwords : BPF_MEMWORDS;
    627 	const bpf_memword_init_t preinited = extwords ? bc->preinited : 0;
    628 #else
    629 	const size_t memwords = BPF_MEMWORDS;
    630 #endif
    631 
    632 	len = (u_int)signed_len;
    633 	if (len < 1)
    634 		return 0;
    635 #if defined(KERNEL) || defined(_KERNEL)
    636 	if (len > BPF_MAXINSNS)
    637 		return 0;
    638 #endif
    639 	if (f[len - 1].code != (BPF_RET|BPF_K) &&
    640 	    f[len - 1].code != (BPF_RET|BPF_A)) {
    641 		return 0;
    642 	}
    643 
    644 #if defined(KERNEL) || defined(_KERNEL)
    645 	/* Note: only the pre-initialised is valid on startup */
    646 	mem = kmem_zalloc(size = sizeof(*mem) * len, KM_SLEEP);
    647 	invalid = ~preinited;
    648 #endif
    649 
    650 	for (i = 0; i < len; ++i) {
    651 #if defined(KERNEL) || defined(_KERNEL)
    652 		/* blend in any invalid bits for current pc */
    653 		invalid |= mem[i];
    654 #endif
    655 		p = &f[i];
    656 		switch (BPF_CLASS(p->code)) {
    657 		/*
    658 		 * Check that memory operations use valid addresses.
    659 		 */
    660 		case BPF_LD:
    661 		case BPF_LDX:
    662 			switch (BPF_MODE(p->code)) {
    663 			case BPF_MEM:
    664 				/*
    665 				 * There's no maximum packet data size
    666 				 * in userland.  The runtime packet length
    667 				 * check suffices.
    668 				 */
    669 #if defined(KERNEL) || defined(_KERNEL)
    670 				/*
    671 				 * More strict check with actual packet length
    672 				 * is done runtime.
    673 				 */
    674 				if (p->k >= memwords)
    675 					goto out;
    676 				/* check for current memory invalid */
    677 				if (invalid & BPF_MEMWORD_INIT(p->k))
    678 					goto out;
    679 #endif
    680 				break;
    681 			case BPF_ABS:
    682 			case BPF_IND:
    683 			case BPF_MSH:
    684 			case BPF_IMM:
    685 			case BPF_LEN:
    686 				break;
    687 			default:
    688 				goto out;
    689 			}
    690 			break;
    691 		case BPF_ST:
    692 		case BPF_STX:
    693 			if (p->k >= memwords)
    694 				goto out;
    695 #if defined(KERNEL) || defined(_KERNEL)
    696 			/* validate the memory word */
    697 			invalid &= ~BPF_MEMWORD_INIT(p->k);
    698 #endif
    699 			break;
    700 		case BPF_ALU:
    701 			switch (BPF_OP(p->code)) {
    702 			case BPF_ADD:
    703 			case BPF_SUB:
    704 			case BPF_MUL:
    705 			case BPF_OR:
    706 			case BPF_XOR:
    707 			case BPF_AND:
    708 			case BPF_LSH:
    709 			case BPF_RSH:
    710 			case BPF_NEG:
    711 				break;
    712 			case BPF_DIV:
    713 			case BPF_MOD:
    714 				/*
    715 				 * Check for constant division by 0.
    716 				 */
    717 				if (BPF_SRC(p->code) == BPF_K && p->k == 0)
    718 					goto out;
    719 				break;
    720 			default:
    721 				goto out;
    722 			}
    723 			break;
    724 		case BPF_JMP:
    725 			/*
    726 			 * Check that jumps are within the code block,
    727 			 * and that unconditional branches don't go
    728 			 * backwards as a result of an overflow.
    729 			 * Unconditional branches have a 32-bit offset,
    730 			 * so they could overflow; we check to make
    731 			 * sure they don't.  Conditional branches have
    732 			 * an 8-bit offset, and the from address is <=
    733 			 * BPF_MAXINSNS, and we assume that BPF_MAXINSNS
    734 			 * is sufficiently small that adding 255 to it
    735 			 * won't overflow.
    736 			 *
    737 			 * We know that len is <= BPF_MAXINSNS, and we
    738 			 * assume that BPF_MAXINSNS is < the maximum size
    739 			 * of a u_int, so that i + 1 doesn't overflow.
    740 			 *
    741 			 * For userland, we don't know that the from
    742 			 * or len are <= BPF_MAXINSNS, but we know that
    743 			 * from <= len, and, except on a 64-bit system,
    744 			 * it's unlikely that len, if it truly reflects
    745 			 * the size of the program we've been handed,
    746 			 * will be anywhere near the maximum size of
    747 			 * a u_int.  We also don't check for backward
    748 			 * branches, as we currently support them in
    749 			 * userland for the protochain operation.
    750 			 */
    751 			from = i + 1;
    752 			switch (BPF_OP(p->code)) {
    753 			case BPF_JA:
    754 				if (from + p->k >= len)
    755 					goto out;
    756 #if defined(KERNEL) || defined(_KERNEL)
    757 				if (from + p->k < from)
    758 					goto out;
    759 				/*
    760 				 * mark the currently invalid bits for the
    761 				 * destination
    762 				 */
    763 				mem[from + p->k] |= invalid;
    764 				invalid = 0;
    765 #endif
    766 				break;
    767 			case BPF_JEQ:
    768 			case BPF_JGT:
    769 			case BPF_JGE:
    770 			case BPF_JSET:
    771 				if (from + p->jt >= len || from + p->jf >= len)
    772 					goto out;
    773 #if defined(KERNEL) || defined(_KERNEL)
    774 				/*
    775 				 * mark the currently invalid bits for both
    776 				 * possible jump destinations
    777 				 */
    778 				mem[from + p->jt] |= invalid;
    779 				mem[from + p->jf] |= invalid;
    780 				invalid = 0;
    781 #endif
    782 				break;
    783 			default:
    784 				goto out;
    785 			}
    786 			break;
    787 		case BPF_RET:
    788 			break;
    789 		case BPF_MISC:
    790 			switch (BPF_MISCOP(p->code)) {
    791 			case BPF_COP:
    792 			case BPF_COPX:
    793 				/* In-kernel COP use only. */
    794 #if defined(KERNEL) || defined(_KERNEL)
    795 				if (bc == NULL || bc->copfuncs == NULL)
    796 					goto out;
    797 				if (BPF_MISCOP(p->code) == BPF_COP &&
    798 				    p->k >= bc->nfuncs) {
    799 					goto out;
    800 				}
    801 				break;
    802 #else
    803 				goto out;
    804 #endif
    805 			default:
    806 				break;
    807 			}
    808 			break;
    809 		default:
    810 			goto out;
    811 		}
    812 	}
    813 	ok = 1;
    814 out:
    815 #if defined(KERNEL) || defined(_KERNEL)
    816 	kmem_free(mem, size);
    817 #endif
    818 	return ok;
    819 }
    820 
    821 #ifdef _KERNEL
    822 /*
    823  * bpfjit is used by other in-kernel callers, such as npf, so define
    824  * its hooks and accessors here, not in the bpf.c device node interface.
    825  */
    826 struct bpfjit_ops bpfjit_module_ops = {
    827 	.bj_generate_code = NULL,
    828 	.bj_free_code = NULL
    829 };
    830 
    831 bpfjit_func_t
    832 bpf_jit_generate(bpf_ctx_t *bc, void *code, size_t size)
    833 {
    834 	struct bpfjit_ops *ops = &bpfjit_module_ops;
    835 	bpfjit_func_t (*generate_code)(const bpf_ctx_t *,
    836 	    const struct bpf_insn *, size_t);
    837 
    838 	generate_code = atomic_load_acquire(&ops->bj_generate_code);
    839 	if (generate_code != NULL) {
    840 		return generate_code(bc, code, size);
    841 	}
    842 	return NULL;
    843 }
    844 
    845 void
    846 bpf_jit_freecode(bpfjit_func_t jcode)
    847 {
    848 	KASSERT(bpfjit_module_ops.bj_free_code != NULL);
    849 	bpfjit_module_ops.bj_free_code(jcode);
    850 }
    851 #endif
    852 
    853 /* Kernel module interface */
    854 
    855 #ifdef _KERNEL
    856 MODULE(MODULE_CLASS_MISC, bpf_filter, NULL);
    857 
    858 static int
    859 bpf_filter_modcmd(modcmd_t cmd, void *opaque)
    860 {
    861 
    862 	switch (cmd) {
    863 	case MODULE_CMD_INIT:
    864 	case MODULE_CMD_FINI:
    865 		return 0;
    866 	default:
    867 		return ENOTTY;
    868 	}
    869 }
    870 #endif
    871