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