1 /* $OpenBSD: bpf_filter.c,v 1.7 2000/06/19 03:00:54 jason Exp $ */ 2 /* $NetBSD: bpf_filter.c,v 1.12 1996/02/13 22:00:00 christos Exp $ */ 3 4 /* 5 * Copyright (c) 1990, 1991, 1992, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from the Stanford/CMU enet packet filter, 9 * (net/enet.c) distributed as part of 4.3BSD, and code contributed 10 * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence 11 * Berkeley Laboratory. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. All advertising materials mentioning features or use of this software 22 * must display the following acknowledgement: 23 * This product includes software developed by the University of 24 * California, Berkeley and its contributors. 25 * 4. Neither the name of the University nor the names of its contributors 26 * may be used to endorse or promote products derived from this software 27 * without specific prior written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 39 * SUCH DAMAGE. 40 * 41 * @(#)bpf_filter.c 8.1 (Berkeley) 6/10/93 42 */ 43 44 #include <sys/param.h> 45 #include <sys/types.h> 46 #include <sys/time.h> 47 48 #ifndef UNALIGNED_ACCESS 49 #define BPF_ALIGN 50 #endif 51 52 #ifndef BPF_ALIGN 53 #define EXTRACT_SHORT(p) ((u_int16_t)ntohs(*(u_int16_t *)p)) 54 #define EXTRACT_LONG(p) (ntohl(*(u_int32_t *)p)) 55 #else 56 #define EXTRACT_SHORT(p)\ 57 ((u_int16_t)\ 58 ((u_int16_t)*((u_char *)p+0)<<8|\ 59 (u_int16_t)*((u_char *)p+1)<<0)) 60 #define EXTRACT_LONG(p)\ 61 ((u_int32_t)*((u_char *)p+0)<<24|\ 62 (u_int32_t)*((u_char *)p+1)<<16|\ 63 (u_int32_t)*((u_char *)p+2)<<8|\ 64 (u_int32_t)*((u_char *)p+3)<<0) 65 #endif 66 67 #ifdef _KERNEL 68 #include <sys/mbuf.h> 69 #define MINDEX(len, m, k) \ 70 { \ 71 len = m->m_len; \ 72 while (k >= len) { \ 73 k -= len; \ 74 m = m->m_next; \ 75 if (m == 0) \ 76 return 0; \ 77 len = m->m_len; \ 78 } \ 79 } 80 81 int bpf_m_xword __P((struct mbuf *, int, int *)); 82 int bpf_m_xhalf __P((struct mbuf *, int, int *)); 83 84 int 85 bpf_m_xword(m, k, err) 86 register struct mbuf *m; 87 register int k, *err; 88 { 89 register int len; 90 register u_char *cp, *np; 91 register struct mbuf *m0; 92 93 MINDEX(len, m, k); 94 cp = mtod(m, u_char *) + k; 95 if (len - k >= 4) { 96 *err = 0; 97 return EXTRACT_LONG(cp); 98 } 99 m0 = m->m_next; 100 if (m0 == 0 || m0->m_len + len - k < 4) 101 goto bad; 102 *err = 0; 103 np = mtod(m0, u_char *); 104 switch (len - k) { 105 106 case 1: 107 return (cp[0] << 24) | (np[0] << 16) | (np[1] << 8) | np[2]; 108 109 case 2: 110 return (cp[0] << 24) | (cp[1] << 16) | (np[0] << 8) | np[1]; 111 112 default: 113 return (cp[0] << 24) | (cp[1] << 16) | (cp[2] << 8) | np[0]; 114 } 115 bad: 116 *err = 1; 117 return 0; 118 } 119 120 int 121 bpf_m_xhalf(m, k, err) 122 register struct mbuf *m; 123 register int k, *err; 124 { 125 register int len; 126 register u_char *cp; 127 register struct mbuf *m0; 128 129 MINDEX(len, m, k); 130 cp = mtod(m, u_char *) + k; 131 if (len - k >= 2) { 132 *err = 0; 133 return EXTRACT_SHORT(cp); 134 } 135 m0 = m->m_next; 136 if (m0 == 0) 137 goto bad; 138 *err = 0; 139 return (cp[0] << 8) | mtod(m0, u_char *)[0]; 140 bad: 141 *err = 1; 142 return 0; 143 } 144 #endif 145 146 #include <net/bpf.h> 147 148 /* 149 * Execute the filter program starting at pc on the packet p 150 * wirelen is the length of the original packet 151 * buflen is the amount of data present 152 */ 153 u_int 154 bpf_filter(pc, p, wirelen, buflen) 155 register struct bpf_insn *pc; 156 register u_char *p; 157 u_int wirelen; 158 register u_int buflen; 159 { 160 register u_int32_t A = 0, X = 0; 161 register int k; 162 int32_t mem[BPF_MEMWORDS]; 163 164 if (pc == 0) 165 /* 166 * No filter means accept all. 167 */ 168 return (u_int)-1; 169 --pc; 170 while (1) { 171 ++pc; 172 switch (pc->code) { 173 174 default: 175 #ifdef _KERNEL 176 return 0; 177 #else 178 abort(); 179 #endif 180 case BPF_RET|BPF_K: 181 return (u_int)pc->k; 182 183 case BPF_RET|BPF_A: 184 return (u_int)A; 185 186 case BPF_LD|BPF_W|BPF_ABS: 187 k = pc->k; 188 if (k + sizeof(int32_t) > buflen) { 189 #ifdef _KERNEL 190 int merr; 191 192 if (buflen != 0) 193 return 0; 194 A = bpf_m_xword((struct mbuf *)p, k, &merr); 195 if (merr != 0) 196 return 0; 197 continue; 198 #else 199 return 0; 200 #endif 201 } 202 A = EXTRACT_LONG(&p[k]); 203 continue; 204 205 case BPF_LD|BPF_H|BPF_ABS: 206 k = pc->k; 207 if (k + sizeof(int16_t) > buflen) { 208 #ifdef _KERNEL 209 int merr; 210 211 if (buflen != 0) 212 return 0; 213 A = bpf_m_xhalf((struct mbuf *)p, k, &merr); 214 continue; 215 #else 216 return 0; 217 #endif 218 } 219 A = EXTRACT_SHORT(&p[k]); 220 continue; 221 222 case BPF_LD|BPF_B|BPF_ABS: 223 k = pc->k; 224 if (k >= buflen) { 225 #ifdef _KERNEL 226 register struct mbuf *m; 227 register int len; 228 229 if (buflen != 0) 230 return 0; 231 m = (struct mbuf *)p; 232 MINDEX(len, m, k); 233 A = mtod(m, u_char *)[k]; 234 continue; 235 #else 236 return 0; 237 #endif 238 } 239 A = p[k]; 240 continue; 241 242 case BPF_LD|BPF_W|BPF_LEN: 243 A = wirelen; 244 continue; 245 246 case BPF_LDX|BPF_W|BPF_LEN: 247 X = wirelen; 248 continue; 249 250 case BPF_LD|BPF_W|BPF_IND: 251 k = X + pc->k; 252 if (k + sizeof(int32_t) > buflen) { 253 #ifdef _KERNEL 254 int merr; 255 256 if (buflen != 0) 257 return 0; 258 A = bpf_m_xword((struct mbuf *)p, k, &merr); 259 if (merr != 0) 260 return 0; 261 continue; 262 #else 263 return 0; 264 #endif 265 } 266 A = EXTRACT_LONG(&p[k]); 267 continue; 268 269 case BPF_LD|BPF_H|BPF_IND: 270 k = X + pc->k; 271 if (k + sizeof(int16_t) > buflen) { 272 #ifdef _KERNEL 273 int merr; 274 275 if (buflen != 0) 276 return 0; 277 A = bpf_m_xhalf((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_SHORT(&p[k]); 286 continue; 287 288 case BPF_LD|BPF_B|BPF_IND: 289 k = X + pc->k; 290 if (k >= buflen) { 291 #ifdef _KERNEL 292 register struct mbuf *m; 293 register int len; 294 295 if (buflen != 0) 296 return 0; 297 m = (struct mbuf *)p; 298 MINDEX(len, m, k); 299 A = mtod(m, char *)[k]; 300 continue; 301 #else 302 return 0; 303 #endif 304 } 305 A = p[k]; 306 continue; 307 308 case BPF_LDX|BPF_MSH|BPF_B: 309 k = pc->k; 310 if (k >= buflen) { 311 #ifdef _KERNEL 312 register struct mbuf *m; 313 register int len; 314 315 if (buflen != 0) 316 return 0; 317 m = (struct mbuf *)p; 318 MINDEX(len, m, k); 319 X = (mtod(m, char *)[k] & 0xf) << 2; 320 continue; 321 #else 322 return 0; 323 #endif 324 } 325 X = (p[pc->k] & 0xf) << 2; 326 continue; 327 328 case BPF_LD|BPF_IMM: 329 A = pc->k; 330 continue; 331 332 case BPF_LDX|BPF_IMM: 333 X = pc->k; 334 continue; 335 336 case BPF_LD|BPF_MEM: 337 A = mem[pc->k]; 338 continue; 339 340 case BPF_LDX|BPF_MEM: 341 X = mem[pc->k]; 342 continue; 343 344 case BPF_ST: 345 mem[pc->k] = A; 346 continue; 347 348 case BPF_STX: 349 mem[pc->k] = X; 350 continue; 351 352 case BPF_JMP|BPF_JA: 353 pc += pc->k; 354 continue; 355 356 case BPF_JMP|BPF_JGT|BPF_K: 357 pc += (A > pc->k) ? pc->jt : pc->jf; 358 continue; 359 360 case BPF_JMP|BPF_JGE|BPF_K: 361 pc += (A >= pc->k) ? pc->jt : pc->jf; 362 continue; 363 364 case BPF_JMP|BPF_JEQ|BPF_K: 365 pc += (A == pc->k) ? pc->jt : pc->jf; 366 continue; 367 368 case BPF_JMP|BPF_JSET|BPF_K: 369 pc += (A & pc->k) ? pc->jt : pc->jf; 370 continue; 371 372 case BPF_JMP|BPF_JGT|BPF_X: 373 pc += (A > X) ? pc->jt : pc->jf; 374 continue; 375 376 case BPF_JMP|BPF_JGE|BPF_X: 377 pc += (A >= X) ? pc->jt : pc->jf; 378 continue; 379 380 case BPF_JMP|BPF_JEQ|BPF_X: 381 pc += (A == X) ? pc->jt : pc->jf; 382 continue; 383 384 case BPF_JMP|BPF_JSET|BPF_X: 385 pc += (A & X) ? pc->jt : pc->jf; 386 continue; 387 388 case BPF_ALU|BPF_ADD|BPF_X: 389 A += X; 390 continue; 391 392 case BPF_ALU|BPF_SUB|BPF_X: 393 A -= X; 394 continue; 395 396 case BPF_ALU|BPF_MUL|BPF_X: 397 A *= X; 398 continue; 399 400 case BPF_ALU|BPF_DIV|BPF_X: 401 if (X == 0) 402 return 0; 403 A /= X; 404 continue; 405 406 case BPF_ALU|BPF_AND|BPF_X: 407 A &= X; 408 continue; 409 410 case BPF_ALU|BPF_OR|BPF_X: 411 A |= X; 412 continue; 413 414 case BPF_ALU|BPF_LSH|BPF_X: 415 A <<= X; 416 continue; 417 418 case BPF_ALU|BPF_RSH|BPF_X: 419 A >>= X; 420 continue; 421 422 case BPF_ALU|BPF_ADD|BPF_K: 423 A += pc->k; 424 continue; 425 426 case BPF_ALU|BPF_SUB|BPF_K: 427 A -= pc->k; 428 continue; 429 430 case BPF_ALU|BPF_MUL|BPF_K: 431 A *= pc->k; 432 continue; 433 434 case BPF_ALU|BPF_DIV|BPF_K: 435 A /= pc->k; 436 continue; 437 438 case BPF_ALU|BPF_AND|BPF_K: 439 A &= pc->k; 440 continue; 441 442 case BPF_ALU|BPF_OR|BPF_K: 443 A |= pc->k; 444 continue; 445 446 case BPF_ALU|BPF_LSH|BPF_K: 447 A <<= pc->k; 448 continue; 449 450 case BPF_ALU|BPF_RSH|BPF_K: 451 A >>= pc->k; 452 continue; 453 454 case BPF_ALU|BPF_NEG: 455 A = -A; 456 continue; 457 458 case BPF_MISC|BPF_TAX: 459 X = A; 460 continue; 461 462 case BPF_MISC|BPF_TXA: 463 A = X; 464 continue; 465 } 466 } 467 } 468 469 #ifdef _KERNEL 470 /* 471 * Return true if the 'fcode' is a valid filter program. 472 * The constraints are that each jump be forward and to a valid 473 * code. The code must terminate with either an accept or reject. 474 * 'valid' is an array for use by the routine (it must be at least 475 * 'len' bytes long). 476 * 477 * The kernel needs to be able to verify an application's filter code. 478 * Otherwise, a bogus program could easily crash the system. 479 */ 480 int 481 bpf_validate(f, len) 482 struct bpf_insn *f; 483 int len; 484 { 485 register int i; 486 register struct bpf_insn *p; 487 488 for (i = 0; i < len; ++i) { 489 /* 490 * Check that that jumps are forward, and within 491 * the code block. 492 */ 493 p = &f[i]; 494 if (BPF_CLASS(p->code) == BPF_JMP) { 495 register int from = i + 1; 496 497 if (BPF_OP(p->code) == BPF_JA) { 498 if (from + p->k >= len) 499 return 0; 500 } 501 else if (from + p->jt >= len || from + p->jf >= len) 502 return 0; 503 } 504 /* 505 * Check that memory operations use valid addresses. 506 */ 507 if ((BPF_CLASS(p->code) == BPF_ST || 508 (BPF_CLASS(p->code) == BPF_LD && 509 (p->code & 0xe0) == BPF_MEM)) && 510 (p->k >= BPF_MEMWORDS || p->k < 0)) 511 return 0; 512 /* 513 * Check for constant division by 0. 514 */ 515 if (p->code == (BPF_ALU|BPF_DIV|BPF_K) && p->k == 0) 516 return 0; 517 } 518 return BPF_CLASS(f[len - 1].code) == BPF_RET; 519 } 520 #endif 521