1 /* $NetBSD: bpf_filter.c,v 1.6 2015/03/31 21:39:42 christos 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 #ifdef HAVE_CONFIG_H
40 #include "config.h"
41 #endif
42
43 #ifdef WIN32
44
45 #include <pcap-stdinc.h>
46
47 #else /* WIN32 */
48
49 #if HAVE_INTTYPES_H
50 #include <inttypes.h>
51 #elif HAVE_STDINT_H
52 #include <stdint.h>
53 #endif
54 #ifdef HAVE_SYS_BITYPES_H
55 #include <sys/bitypes.h>
56 #endif
57
58 #include <sys/param.h>
59 #include <sys/types.h>
60 #include <sys/time.h>
61
62 #define SOLARIS (defined(sun) && (defined(__SVR4) || defined(__svr4__)))
63 #if defined(__hpux) || SOLARIS
64 # include <sys/sysmacros.h>
65 # include <sys/stream.h>
66 # define mbuf msgb
67 # define m_next b_cont
68 # define MLEN(m) ((m)->b_wptr - (m)->b_rptr)
69 # define mtod(m,t) ((t)(m)->b_rptr)
70 #else /* defined(__hpux) || SOLARIS */
71 # define MLEN(m) ((m)->m_len)
72 #endif /* defined(__hpux) || SOLARIS */
73
74 #endif /* WIN32 */
75
76 #include <pcap/bpf.h>
77
78 #if !defined(KERNEL) && !defined(_KERNEL)
79 #include <stdlib.h>
80 #endif
81
82 #define int32 bpf_int32
83 #define u_int32 bpf_u_int32
84
85 #ifndef LBL_ALIGN
86 /*
87 * XXX - IA-64? If not, this probably won't work on Win64 IA-64
88 * systems, unless LBL_ALIGN is defined elsewhere for them.
89 * XXX - SuperH? If not, this probably won't work on WinCE SuperH
90 * systems, unless LBL_ALIGN is defined elsewhere for them.
91 */
92 #if defined(sparc) || defined(__sparc__) || defined(mips) || \
93 defined(ibm032) || defined(__alpha) || defined(__hpux) || \
94 defined(__arm__)
95 #define LBL_ALIGN
96 #endif
97 #endif
98
99 #ifndef LBL_ALIGN
100 #ifndef WIN32
101 #include <netinet/in.h>
102 #endif
103
104 #define EXTRACT_SHORT(p) ((u_short)ntohs(*(const u_short *)p))
105 #define EXTRACT_LONG(p) (ntohl(*(const u_int32 *)p))
106 #else
107 #define EXTRACT_SHORT(p)\
108 ((u_short)\
109 ((u_short)*((const u_char *)p+0)<<8|\
110 (u_short)*((const u_char *)p+1)<<0))
111 #define EXTRACT_LONG(p)\
112 ((u_int32)*((const u_char *)p+0)<<24|\
113 (u_int32)*((const u_char *)p+1)<<16|\
114 (u_int32)*((const u_char *)p+2)<<8|\
115 (u_int32)*((const u_char *)p+3)<<0)
116 #endif
117
118 #if defined(KERNEL) || defined(_KERNEL)
119 # if !defined(__hpux) && !SOLARIS
120 #include <sys/mbuf.h>
121 # endif
122 #define MINDEX(len, _m, _k) \
123 { \
124 len = MLEN(m); \
125 while ((_k) >= len) { \
126 (_k) -= len; \
127 (_m) = (_m)->m_next; \
128 if ((_m) == 0) \
129 return 0; \
130 len = MLEN(m); \
131 } \
132 }
133
134 static int m_xword (const struct mbuf *, uint32_t, int *);
135 static int m_xhalf (const struct mbuf *, uint32_t, int *);
136
137 static int
m_xword(const struct mbuf * m,uint32_t k,int * err)138 m_xword(const struct mbuf *m, uint32_t k, int *err)
139 {
140 int len;
141 u_char *cp, *np;
142 struct mbuf *m0;
143
144 *err = 1;
145 MINDEX(len, m, k);
146 cp = mtod(m, u_char *) + k;
147 if (len - k >= 4) {
148 *err = 0;
149 return EXTRACT_LONG(cp);
150 }
151 m0 = m->m_next;
152 if (m0 == 0 || MLEN(m0) + len - k < 4)
153 return 0;
154 *err = 0;
155 np = mtod(m0, u_char *);
156 switch (len - k) {
157
158 case 1:
159 return (cp[0] << 24) | (np[0] << 16) | (np[1] << 8) | np[2];
160
161 case 2:
162 return (cp[0] << 24) | (cp[1] << 16) | (np[0] << 8) | np[1];
163
164 default:
165 return (cp[0] << 24) | (cp[1] << 16) | (cp[2] << 8) | np[0];
166 }
167 }
168
169 static int
m_xhalf(const struct mbuf * m,uint32_t k,int * err)170 m_xhalf(const struct mbuf *m, uint32_t k, int *err)
171 {
172 int len;
173 u_char *cp;
174 struct mbuf *m0;
175
176 *err = 1;
177 MINDEX(len, m, k);
178 cp = mtod(m, u_char *) + k;
179 if (len - k >= 2) {
180 *err = 0;
181 return EXTRACT_SHORT(cp);
182 }
183 m0 = m->m_next;
184 if (m0 == 0)
185 return 0;
186 *err = 0;
187 return (cp[0] << 8) | mtod(m0, u_char *)[0];
188 }
189 #endif
190
191 #ifdef __linux__
192 #include <linux/types.h>
193 #include <linux/if_packet.h>
194 #include <linux/filter.h>
195 #endif
196
197 enum {
198 BPF_S_ANC_NONE,
199 BPF_S_ANC_VLAN_TAG,
200 BPF_S_ANC_VLAN_TAG_PRESENT,
201 };
202
203 /*
204 * Execute the filter program starting at pc on the packet p
205 * wirelen is the length of the original packet
206 * buflen is the amount of data present
207 * aux_data is auxiliary data, currently used only when interpreting
208 * filters intended for the Linux kernel in cases where the kernel
209 * rejects the filter; it contains VLAN tag information
210 * For the kernel, p is assumed to be a pointer to an mbuf if buflen is 0,
211 * in all other cases, p is a pointer to a buffer and buflen is its size.
212 */
213 u_int
bpf_filter(const struct bpf_insn * pc,const u_char * p,u_int wirelen,u_int buflen)214 bpf_filter(const struct bpf_insn *pc, const u_char *p, u_int wirelen,
215 u_int buflen)
216 {
217 register u_int32 A, X;
218 register bpf_u_int32 k;
219 u_int32 mem[BPF_MEMWORDS];
220 #if defined(KERNEL) || defined(_KERNEL)
221 struct mbuf *m, *n;
222 int merr, len;
223
224 if (buflen == 0) {
225 m = (struct mbuf *)p;
226 p = mtod(m, u_char *);
227 buflen = MLEN(m);
228 } else
229 m = NULL;
230 #endif
231
232 if (pc == 0)
233 /*
234 * No filter means accept all.
235 */
236 return (u_int)-1;
237 A = 0;
238 X = 0;
239 --pc;
240 /* CONSTCOND */
241 while (1) {
242 ++pc;
243 switch (pc->code) {
244
245 default:
246 #if defined(KERNEL) || defined(_KERNEL)
247 return 0;
248 #else
249 abort();
250 #endif
251 case BPF_RET|BPF_K:
252 return (u_int)pc->k;
253
254 case BPF_RET|BPF_A:
255 return (u_int)A;
256
257 case BPF_LD|BPF_W|BPF_ABS:
258 k = pc->k;
259 if (k > buflen || sizeof(int32_t) > buflen - k) {
260 #if defined(KERNEL) || defined(_KERNEL)
261 if (m == NULL)
262 return 0;
263 A = m_xword(m, 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_ABS:
275 k = pc->k;
276 if (k > buflen || sizeof(int16_t) > buflen - k) {
277 #if defined(KERNEL) || defined(_KERNEL)
278 if (m == NULL)
279 return 0;
280 A = m_xhalf(m, k, &merr);
281 if (merr != 0)
282 return 0;
283 continue;
284 #else
285 return 0;
286 #endif
287 }
288 A = EXTRACT_SHORT(&p[k]);
289 continue;
290
291 case BPF_LD|BPF_B|BPF_ABS:
292 {
293 #if defined(SKF_AD_VLAN_TAG) && defined(SKF_AD_VLAN_TAG_PRESENT)
294 int code = BPF_S_ANC_NONE;
295 #define ANCILLARY(CODE) case SKF_AD_OFF + SKF_AD_##CODE: \
296 code = BPF_S_ANC_##CODE; \
297 if (!aux_data) \
298 return 0; \
299 break;
300
301 switch (pc->k) {
302 ANCILLARY(VLAN_TAG);
303 ANCILLARY(VLAN_TAG_PRESENT);
304 default :
305 #endif
306 k = pc->k;
307 if (k >= (int)buflen) {
308 #if defined(KERNEL) || defined(_KERNEL)
309 if (m == NULL)
310 return 0;
311 n = m;
312 MINDEX(len, n, k);
313 A = mtod(n, u_char *)[k];
314 continue;
315 #else
316 return 0;
317 #endif
318 }
319 A = p[k];
320 #if defined(SKF_AD_VLAN_TAG) && defined(SKF_AD_VLAN_TAG_PRESENT)
321 }
322 switch (code) {
323 case BPF_S_ANC_VLAN_TAG:
324 if (aux_data)
325 A = aux_data->vlan_tag;
326 break;
327
328 case BPF_S_ANC_VLAN_TAG_PRESENT:
329 if (aux_data)
330 A = aux_data->vlan_tag_present;
331 break;
332 }
333 #endif
334 continue;
335 }
336 case BPF_LD|BPF_W|BPF_LEN:
337 A = wirelen;
338 continue;
339
340 case BPF_LDX|BPF_W|BPF_LEN:
341 X = wirelen;
342 continue;
343
344 case BPF_LD|BPF_W|BPF_IND:
345 k = X + pc->k;
346 if (pc->k > buflen || X > buflen - pc->k ||
347 sizeof(int32_t) > buflen - k) {
348 #if defined(KERNEL) || defined(_KERNEL)
349 if (m == NULL)
350 return 0;
351 A = m_xword(m, k, &merr);
352 if (merr != 0)
353 return 0;
354 continue;
355 #else
356 return 0;
357 #endif
358 }
359 A = EXTRACT_LONG(&p[k]);
360 continue;
361
362 case BPF_LD|BPF_H|BPF_IND:
363 k = X + pc->k;
364 if (X > buflen || pc->k > buflen - X ||
365 sizeof(int16_t) > buflen - k) {
366 #if defined(KERNEL) || defined(_KERNEL)
367 if (m == NULL)
368 return 0;
369 A = m_xhalf(m, k, &merr);
370 if (merr != 0)
371 return 0;
372 continue;
373 #else
374 return 0;
375 #endif
376 }
377 A = EXTRACT_SHORT(&p[k]);
378 continue;
379
380 case BPF_LD|BPF_B|BPF_IND:
381 k = X + pc->k;
382 if (pc->k >= (int)buflen || X >= buflen - pc->k) {
383 #if defined(KERNEL) || defined(_KERNEL)
384 if (m == NULL)
385 return 0;
386 n = m;
387 MINDEX(len, n, k);
388 A = mtod(n, u_char *)[k];
389 continue;
390 #else
391 return 0;
392 #endif
393 }
394 A = p[k];
395 continue;
396
397 case BPF_LDX|BPF_MSH|BPF_B:
398 k = pc->k;
399 if (k >= (int)buflen) {
400 #if defined(KERNEL) || defined(_KERNEL)
401 if (m == NULL)
402 return 0;
403 n = m;
404 MINDEX(len, n, k);
405 X = (mtod(n, char *)[k] & 0xf) << 2;
406 continue;
407 #else
408 return 0;
409 #endif
410 }
411 X = (p[pc->k] & 0xf) << 2;
412 continue;
413
414 case BPF_LD|BPF_IMM:
415 A = pc->k;
416 continue;
417
418 case BPF_LDX|BPF_IMM:
419 X = pc->k;
420 continue;
421
422 case BPF_LD|BPF_MEM:
423 A = mem[pc->k];
424 continue;
425
426 case BPF_LDX|BPF_MEM:
427 X = mem[pc->k];
428 continue;
429
430 case BPF_ST:
431 mem[pc->k] = A;
432 continue;
433
434 case BPF_STX:
435 mem[pc->k] = X;
436 continue;
437
438 case BPF_JMP|BPF_JA:
439 #if defined(KERNEL) || defined(_KERNEL)
440 /*
441 * No backward jumps allowed.
442 */
443 pc += pc->k;
444 #else
445 /*
446 * XXX - we currently implement "ip6 protochain"
447 * with backward jumps, so sign-extend pc->k.
448 */
449 pc += (bpf_int32)pc->k;
450 #endif
451 continue;
452
453 case BPF_JMP|BPF_JGT|BPF_K:
454 pc += (A > pc->k) ? pc->jt : pc->jf;
455 continue;
456
457 case BPF_JMP|BPF_JGE|BPF_K:
458 pc += (A >= pc->k) ? pc->jt : pc->jf;
459 continue;
460
461 case BPF_JMP|BPF_JEQ|BPF_K:
462 pc += (A == pc->k) ? pc->jt : pc->jf;
463 continue;
464
465 case BPF_JMP|BPF_JSET|BPF_K:
466 pc += (A & pc->k) ? pc->jt : pc->jf;
467 continue;
468
469 case BPF_JMP|BPF_JGT|BPF_X:
470 pc += (A > X) ? pc->jt : pc->jf;
471 continue;
472
473 case BPF_JMP|BPF_JGE|BPF_X:
474 pc += (A >= X) ? pc->jt : pc->jf;
475 continue;
476
477 case BPF_JMP|BPF_JEQ|BPF_X:
478 pc += (A == X) ? pc->jt : pc->jf;
479 continue;
480
481 case BPF_JMP|BPF_JSET|BPF_X:
482 pc += (A & X) ? pc->jt : pc->jf;
483 continue;
484
485 case BPF_ALU|BPF_ADD|BPF_X:
486 A += X;
487 continue;
488
489 case BPF_ALU|BPF_SUB|BPF_X:
490 A -= X;
491 continue;
492
493 case BPF_ALU|BPF_MUL|BPF_X:
494 A *= X;
495 continue;
496
497 case BPF_ALU|BPF_DIV|BPF_X:
498 if (X == 0)
499 return 0;
500 A /= X;
501 continue;
502
503 case BPF_ALU|BPF_MOD|BPF_X:
504 if (X == 0)
505 return 0;
506 A %= X;
507 continue;
508
509 case BPF_ALU|BPF_AND|BPF_X:
510 A &= X;
511 continue;
512
513 case BPF_ALU|BPF_OR|BPF_X:
514 A |= X;
515 continue;
516
517 case BPF_ALU|BPF_XOR|BPF_X:
518 A ^= X;
519 continue;
520
521 case BPF_ALU|BPF_LSH|BPF_X:
522 A <<= X;
523 continue;
524
525 case BPF_ALU|BPF_RSH|BPF_X:
526 A >>= X;
527 continue;
528
529 case BPF_ALU|BPF_ADD|BPF_K:
530 A += pc->k;
531 continue;
532
533 case BPF_ALU|BPF_SUB|BPF_K:
534 A -= pc->k;
535 continue;
536
537 case BPF_ALU|BPF_MUL|BPF_K:
538 A *= pc->k;
539 continue;
540
541 case BPF_ALU|BPF_DIV|BPF_K:
542 A /= pc->k;
543 continue;
544
545 case BPF_ALU|BPF_MOD|BPF_K:
546 A %= pc->k;
547 continue;
548
549 case BPF_ALU|BPF_AND|BPF_K:
550 A &= pc->k;
551 continue;
552
553 case BPF_ALU|BPF_OR|BPF_K:
554 A |= pc->k;
555 continue;
556
557 case BPF_ALU|BPF_XOR|BPF_K:
558 A ^= pc->k;
559 continue;
560
561 case BPF_ALU|BPF_LSH|BPF_K:
562 A <<= pc->k;
563 continue;
564
565 case BPF_ALU|BPF_RSH|BPF_K:
566 A >>= pc->k;
567 continue;
568
569 case BPF_ALU|BPF_NEG:
570 A = -A;
571 continue;
572
573 case BPF_MISC|BPF_TAX:
574 X = A;
575 continue;
576
577 case BPF_MISC|BPF_TXA:
578 A = X;
579 continue;
580 }
581 }
582 }
583
584 u_int
bpf_filter(pc,p,wirelen,buflen)585 bpf_filter(pc, p, wirelen, buflen)
586 register const struct bpf_insn *pc;
587 register const u_char *p;
588 u_int wirelen;
589 register u_int buflen;
590 {
591 return bpf_filter_with_aux_data(pc, p, wirelen, buflen, NULL);
592 }
593
594
595 /*
596 * Return true if the 'fcode' is a valid filter program.
597 * The constraints are that each jump be forward and to a valid
598 * code, that memory accesses are within valid ranges (to the
599 * extent that this can be checked statically; loads of packet
600 * data have to be, and are, also checked at run time), and that
601 * the code terminates with either an accept or reject.
602 *
603 * The kernel needs to be able to verify an application's filter code.
604 * Otherwise, a bogus program could easily crash the system.
605 */
606 int
bpf_validate(const struct bpf_insn * f,int signed_len)607 bpf_validate(const struct bpf_insn *f, int signed_len)
608 {
609 u_int i, from, len;
610 const struct bpf_insn *p;
611
612 len = (u_int)signed_len;
613 if (len < 1)
614 return 0;
615 /*
616 * There's no maximum program length in userland.
617 */
618 #if defined(KERNEL) || defined(_KERNEL)
619 if (len > BPF_MAXINSNS)
620 return 0;
621 #endif
622
623 for (i = 0; i < len; ++i) {
624 p = &f[i];
625 switch (BPF_CLASS(p->code)) {
626 /*
627 * Check that memory operations use valid addresses.
628 */
629 case BPF_LD:
630 case BPF_LDX:
631 switch (BPF_MODE(p->code)) {
632 case BPF_MEM:
633 /*
634 * There's no maximum packet data size
635 * in userland. The runtime packet length
636 * check suffices.
637 */
638 #if defined(KERNEL) || defined(_KERNEL)
639 /*
640 * More strict check with actual packet length
641 * is done runtime.
642 */
643 if (p->k >= BPF_MEMWORDS)
644 return 0;
645 #endif
646 break;
647 case BPF_ABS:
648 case BPF_IND:
649 case BPF_MSH:
650 case BPF_IMM:
651 case BPF_MEM:
652 if (p->k >= BPF_MEMWORDS)
653 return 0;
654 break;
655 case BPF_LEN:
656 break;
657 default:
658 return 0;
659 }
660 break;
661 case BPF_ST:
662 case BPF_STX:
663 if (p->k >= BPF_MEMWORDS)
664 return 0;
665 break;
666 case BPF_ALU:
667 switch (BPF_OP(p->code)) {
668 case BPF_ADD:
669 case BPF_SUB:
670 case BPF_MUL:
671 case BPF_OR:
672 case BPF_AND:
673 case BPF_XOR:
674 case BPF_LSH:
675 case BPF_RSH:
676 case BPF_NEG:
677 break;
678 case BPF_DIV:
679 case BPF_MOD:
680 /*
681 * Check for constant division or modulus
682 * by 0.
683 */
684 if (BPF_SRC(p->code) == BPF_K && p->k == 0)
685 return 0;
686 break;
687 default:
688 return 0;
689 }
690 break;
691 case BPF_JMP:
692 /*
693 * Check that jumps are within the code block,
694 * and that unconditional branches don't go
695 * backwards as a result of an overflow.
696 * Unconditional branches have a 32-bit offset,
697 * so they could overflow; we check to make
698 * sure they don't. Conditional branches have
699 * an 8-bit offset, and the from address is <=
700 * BPF_MAXINSNS, and we assume that BPF_MAXINSNS
701 * is sufficiently small that adding 255 to it
702 * won't overflow.
703 *
704 * We know that len is <= BPF_MAXINSNS, and we
705 * assume that BPF_MAXINSNS is < the maximum size
706 * of a u_int, so that i + 1 doesn't overflow.
707 *
708 * For userland, we don't know that the from
709 * or len are <= BPF_MAXINSNS, but we know that
710 * from <= len, and, except on a 64-bit system,
711 * it's unlikely that len, if it truly reflects
712 * the size of the program we've been handed,
713 * will be anywhere near the maximum size of
714 * a u_int. We also don't check for backward
715 * branches, as we currently support them in
716 * userland for the protochain operation.
717 */
718 from = i + 1;
719 switch (BPF_OP(p->code)) {
720 case BPF_JA:
721 #if defined(KERNEL) || defined(_KERNEL)
722 if (from + p->k < from || from + p->k >= len)
723 #else
724 if (from + p->k >= len)
725 #endif
726 return 0;
727 break;
728 case BPF_JEQ:
729 case BPF_JGT:
730 case BPF_JGE:
731 case BPF_JSET:
732 if (from + p->jt >= len || from + p->jf >= len)
733 return 0;
734 break;
735 default:
736 return 0;
737 }
738 break;
739 case BPF_RET:
740 break;
741 case BPF_MISC:
742 break;
743 default:
744 return 0;
745 }
746 }
747 return BPF_CLASS(f[len - 1].code) == BPF_RET;
748 }
749