bpf_filter.c revision 139823
1279377Simp/*-
2279377Simp * Copyright (c) 1990, 1991, 1993
3279377Simp *	The Regents of the University of California.  All rights reserved.
4279377Simp *
5279377Simp * This code is derived from the Stanford/CMU enet packet filter,
6279377Simp * (net/enet.c) distributed as part of 4.3BSD, and code contributed
7279377Simp * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
8279377Simp * Berkeley Laboratory.
9279377Simp *
10279377Simp * Redistribution and use in source and binary forms, with or without
11279377Simp * modification, are permitted provided that the following conditions
12279377Simp * are met:
13279377Simp * 1. Redistributions of source code must retain the above copyright
14279377Simp *    notice, this list of conditions and the following disclaimer.
15279377Simp * 2. Redistributions in binary form must reproduce the above copyright
16279377Simp *    notice, this list of conditions and the following disclaimer in the
17279377Simp *    documentation and/or other materials provided with the distribution.
18279377Simp * 4. Neither the name of the University nor the names of its contributors
19279377Simp *    may be used to endorse or promote products derived from this software
20279377Simp *    without specific prior written permission.
21279377Simp *
22279377Simp * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23279377Simp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24279377Simp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25279377Simp * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26279377Simp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27279377Simp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28279377Simp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29279377Simp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30279377Simp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31279377Simp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32279377Simp * SUCH DAMAGE.
33279377Simp *
34279377Simp *      @(#)bpf_filter.c	8.1 (Berkeley) 6/10/93
35279377Simp *
36279377Simp * $FreeBSD: head/sys/net/bpf_filter.c 139823 2005-01-07 01:45:51Z imp $
37279377Simp */
38279377Simp
39279377Simp#include <sys/param.h>
40279377Simp
41279377Simp#ifdef sun
42279377Simp#include <netinet/in.h>
43279377Simp#endif
44279377Simp
45279377Simp#ifndef __i386__
46279377Simp#define BPF_ALIGN
47279377Simp#endif
48279377Simp
49279377Simp#ifndef BPF_ALIGN
50279377Simp#define EXTRACT_SHORT(p)	((u_int16_t)ntohs(*(u_int16_t *)p))
51279377Simp#define EXTRACT_LONG(p)		(ntohl(*(u_int32_t *)p))
52279377Simp#else
53279377Simp#define EXTRACT_SHORT(p)\
54279377Simp	((u_int16_t)\
55279377Simp		((u_int16_t)*((u_char *)p+0)<<8|\
56279377Simp		 (u_int16_t)*((u_char *)p+1)<<0))
57279377Simp#define EXTRACT_LONG(p)\
58279377Simp		((u_int32_t)*((u_char *)p+0)<<24|\
59279377Simp		 (u_int32_t)*((u_char *)p+1)<<16|\
60279377Simp		 (u_int32_t)*((u_char *)p+2)<<8|\
61279377Simp		 (u_int32_t)*((u_char *)p+3)<<0)
62279377Simp#endif
63279377Simp
64279377Simp#ifdef _KERNEL
65279377Simp#include <sys/mbuf.h>
66279377Simp#endif
67279377Simp#include <net/bpf.h>
68279377Simp#ifdef _KERNEL
69279377Simp#define MINDEX(m, k) \
70279377Simp{ \
71279377Simp	register int len = m->m_len; \
72279377Simp \
73279377Simp	while (k >= len) { \
74279377Simp		k -= len; \
75279377Simp		m = m->m_next; \
76279377Simp		if (m == 0) \
77279377Simp			return 0; \
78279377Simp		len = m->m_len; \
79279377Simp	} \
80279377Simp}
81279377Simp
82279377Simpstatic u_int16_t	m_xhalf(struct mbuf *m, bpf_u_int32 k, int *err);
83279377Simpstatic u_int32_t	m_xword(struct mbuf *m, bpf_u_int32 k, int *err);
84279377Simp
85279377Simpstatic u_int32_t
86279377Simpm_xword(m, k, err)
87279377Simp	register struct mbuf *m;
88279377Simp	register bpf_u_int32 k;
89279377Simp	register int *err;
90279377Simp{
91279377Simp	register size_t len;
92279377Simp	register u_char *cp, *np;
93279377Simp	register struct mbuf *m0;
94279377Simp
95279377Simp	len = m->m_len;
96279377Simp	while (k >= len) {
97279377Simp		k -= len;
98279377Simp		m = m->m_next;
99279377Simp		if (m == 0)
100279377Simp			goto bad;
101279377Simp		len = m->m_len;
102279377Simp	}
103279377Simp	cp = mtod(m, u_char *) + k;
104279377Simp	if (len - k >= 4) {
105279377Simp		*err = 0;
106279377Simp		return EXTRACT_LONG(cp);
107279377Simp	}
108279377Simp	m0 = m->m_next;
109279377Simp	if (m0 == 0 || m0->m_len + len - k < 4)
110279377Simp		goto bad;
111279377Simp	*err = 0;
112279377Simp	np = mtod(m0, u_char *);
113279377Simp	switch (len - k) {
114279377Simp
115279377Simp	case 1:
116279377Simp		return
117279377Simp		    ((u_int32_t)cp[0] << 24) |
118279377Simp		    ((u_int32_t)np[0] << 16) |
119279377Simp		    ((u_int32_t)np[1] << 8)  |
120279377Simp		    (u_int32_t)np[2];
121279377Simp
122279377Simp	case 2:
123279377Simp		return
124279377Simp		    ((u_int32_t)cp[0] << 24) |
125279377Simp		    ((u_int32_t)cp[1] << 16) |
126279377Simp		    ((u_int32_t)np[0] << 8) |
127279377Simp		    (u_int32_t)np[1];
128279377Simp
129279377Simp	default:
130279377Simp		return
131279377Simp		    ((u_int32_t)cp[0] << 24) |
132279377Simp		    ((u_int32_t)cp[1] << 16) |
133279377Simp		    ((u_int32_t)cp[2] << 8) |
134279377Simp		    (u_int32_t)np[0];
135279377Simp	}
136279377Simp    bad:
137279377Simp	*err = 1;
138279377Simp	return 0;
139279377Simp}
140279377Simp
141279377Simpstatic u_int16_t
142279377Simpm_xhalf(m, k, err)
143279377Simp	register struct mbuf *m;
144279377Simp	register bpf_u_int32 k;
145279377Simp	register int *err;
146279377Simp{
147279377Simp	register size_t len;
148279377Simp	register u_char *cp;
149279377Simp	register struct mbuf *m0;
150279377Simp
151279377Simp	len = m->m_len;
152279377Simp	while (k >= len) {
153279377Simp		k -= len;
154279377Simp		m = m->m_next;
155279377Simp		if (m == 0)
156279377Simp			goto bad;
157279377Simp		len = m->m_len;
158279377Simp	}
159279377Simp	cp = mtod(m, u_char *) + k;
160279377Simp	if (len - k >= 2) {
161279377Simp		*err = 0;
162279377Simp		return EXTRACT_SHORT(cp);
163279377Simp	}
164279377Simp	m0 = m->m_next;
165279377Simp	if (m0 == 0)
166279377Simp		goto bad;
167279377Simp	*err = 0;
168279377Simp	return (cp[0] << 8) | mtod(m0, u_char *)[0];
169279377Simp bad:
170279377Simp	*err = 1;
171279377Simp	return 0;
172279377Simp}
173#endif
174
175/*
176 * Execute the filter program starting at pc on the packet p
177 * wirelen is the length of the original packet
178 * buflen is the amount of data present
179 */
180u_int
181bpf_filter(pc, p, wirelen, buflen)
182	register const struct bpf_insn *pc;
183	register u_char *p;
184	u_int wirelen;
185	register u_int buflen;
186{
187	register u_int32_t A = 0, X = 0;
188	register bpf_u_int32 k;
189	int32_t mem[BPF_MEMWORDS];
190
191	if (pc == 0)
192		/*
193		 * No filter means accept all.
194		 */
195		return (u_int)-1;
196
197	--pc;
198	while (1) {
199		++pc;
200		switch (pc->code) {
201
202		default:
203#ifdef _KERNEL
204			return 0;
205#else
206			abort();
207#endif
208		case BPF_RET|BPF_K:
209			return (u_int)pc->k;
210
211		case BPF_RET|BPF_A:
212			return (u_int)A;
213
214		case BPF_LD|BPF_W|BPF_ABS:
215			k = pc->k;
216			if (k > buflen || sizeof(int32_t) > buflen - k) {
217#ifdef _KERNEL
218				int merr;
219
220				if (buflen != 0)
221					return 0;
222				A = m_xword((struct mbuf *)p, k, &merr);
223				if (merr != 0)
224					return 0;
225				continue;
226#else
227				return 0;
228#endif
229			}
230#ifdef BPF_ALIGN
231			if (((intptr_t)(p + k) & 3) != 0)
232				A = EXTRACT_LONG(&p[k]);
233			else
234#endif
235				A = ntohl(*(int32_t *)(p + k));
236			continue;
237
238		case BPF_LD|BPF_H|BPF_ABS:
239			k = pc->k;
240			if (k > buflen || sizeof(int16_t) > buflen - k) {
241#ifdef _KERNEL
242				int merr;
243
244				if (buflen != 0)
245					return 0;
246				A = m_xhalf((struct mbuf *)p, k, &merr);
247				continue;
248#else
249				return 0;
250#endif
251			}
252			A = EXTRACT_SHORT(&p[k]);
253			continue;
254
255		case BPF_LD|BPF_B|BPF_ABS:
256			k = pc->k;
257			if (k >= buflen) {
258#ifdef _KERNEL
259				register struct mbuf *m;
260
261				if (buflen != 0)
262					return 0;
263				m = (struct mbuf *)p;
264				MINDEX(m, k);
265				A = mtod(m, u_char *)[k];
266				continue;
267#else
268				return 0;
269#endif
270			}
271			A = p[k];
272			continue;
273
274		case BPF_LD|BPF_W|BPF_LEN:
275			A = wirelen;
276			continue;
277
278		case BPF_LDX|BPF_W|BPF_LEN:
279			X = wirelen;
280			continue;
281
282		case BPF_LD|BPF_W|BPF_IND:
283			k = X + pc->k;
284			if (pc->k > buflen || X > buflen - pc->k ||
285			    sizeof(int32_t) > buflen - k) {
286#ifdef _KERNEL
287				int merr;
288
289				if (buflen != 0)
290					return 0;
291				A = m_xword((struct mbuf *)p, k, &merr);
292				if (merr != 0)
293					return 0;
294				continue;
295#else
296				return 0;
297#endif
298			}
299#ifdef BPF_ALIGN
300			if (((intptr_t)(p + k) & 3) != 0)
301				A = EXTRACT_LONG(&p[k]);
302			else
303#endif
304				A = ntohl(*(int32_t *)(p + k));
305			continue;
306
307		case BPF_LD|BPF_H|BPF_IND:
308			k = X + pc->k;
309			if (X > buflen || pc->k > buflen - X ||
310			    sizeof(int16_t) > buflen - k) {
311#ifdef _KERNEL
312				int merr;
313
314				if (buflen != 0)
315					return 0;
316				A = m_xhalf((struct mbuf *)p, k, &merr);
317				if (merr != 0)
318					return 0;
319				continue;
320#else
321				return 0;
322#endif
323			}
324			A = EXTRACT_SHORT(&p[k]);
325			continue;
326
327		case BPF_LD|BPF_B|BPF_IND:
328			k = X + pc->k;
329			if (pc->k >= buflen || X >= buflen - pc->k) {
330#ifdef _KERNEL
331				register struct mbuf *m;
332
333				if (buflen != 0)
334					return 0;
335				m = (struct mbuf *)p;
336				MINDEX(m, k);
337				A = mtod(m, char *)[k];
338				continue;
339#else
340				return 0;
341#endif
342			}
343			A = p[k];
344			continue;
345
346		case BPF_LDX|BPF_MSH|BPF_B:
347			k = pc->k;
348			if (k >= buflen) {
349#ifdef _KERNEL
350				register struct mbuf *m;
351
352				if (buflen != 0)
353					return 0;
354				m = (struct mbuf *)p;
355				MINDEX(m, k);
356				X = (mtod(m, char *)[k] & 0xf) << 2;
357				continue;
358#else
359				return 0;
360#endif
361			}
362			X = (p[pc->k] & 0xf) << 2;
363			continue;
364
365		case BPF_LD|BPF_IMM:
366			A = pc->k;
367			continue;
368
369		case BPF_LDX|BPF_IMM:
370			X = pc->k;
371			continue;
372
373		case BPF_LD|BPF_MEM:
374			A = mem[pc->k];
375			continue;
376
377		case BPF_LDX|BPF_MEM:
378			X = mem[pc->k];
379			continue;
380
381		case BPF_ST:
382			mem[pc->k] = A;
383			continue;
384
385		case BPF_STX:
386			mem[pc->k] = X;
387			continue;
388
389		case BPF_JMP|BPF_JA:
390			pc += pc->k;
391			continue;
392
393		case BPF_JMP|BPF_JGT|BPF_K:
394			pc += (A > pc->k) ? pc->jt : pc->jf;
395			continue;
396
397		case BPF_JMP|BPF_JGE|BPF_K:
398			pc += (A >= pc->k) ? pc->jt : pc->jf;
399			continue;
400
401		case BPF_JMP|BPF_JEQ|BPF_K:
402			pc += (A == pc->k) ? pc->jt : pc->jf;
403			continue;
404
405		case BPF_JMP|BPF_JSET|BPF_K:
406			pc += (A & pc->k) ? pc->jt : pc->jf;
407			continue;
408
409		case BPF_JMP|BPF_JGT|BPF_X:
410			pc += (A > X) ? pc->jt : pc->jf;
411			continue;
412
413		case BPF_JMP|BPF_JGE|BPF_X:
414			pc += (A >= X) ? pc->jt : pc->jf;
415			continue;
416
417		case BPF_JMP|BPF_JEQ|BPF_X:
418			pc += (A == X) ? pc->jt : pc->jf;
419			continue;
420
421		case BPF_JMP|BPF_JSET|BPF_X:
422			pc += (A & X) ? pc->jt : pc->jf;
423			continue;
424
425		case BPF_ALU|BPF_ADD|BPF_X:
426			A += X;
427			continue;
428
429		case BPF_ALU|BPF_SUB|BPF_X:
430			A -= X;
431			continue;
432
433		case BPF_ALU|BPF_MUL|BPF_X:
434			A *= X;
435			continue;
436
437		case BPF_ALU|BPF_DIV|BPF_X:
438			if (X == 0)
439				return 0;
440			A /= X;
441			continue;
442
443		case BPF_ALU|BPF_AND|BPF_X:
444			A &= X;
445			continue;
446
447		case BPF_ALU|BPF_OR|BPF_X:
448			A |= X;
449			continue;
450
451		case BPF_ALU|BPF_LSH|BPF_X:
452			A <<= X;
453			continue;
454
455		case BPF_ALU|BPF_RSH|BPF_X:
456			A >>= X;
457			continue;
458
459		case BPF_ALU|BPF_ADD|BPF_K:
460			A += pc->k;
461			continue;
462
463		case BPF_ALU|BPF_SUB|BPF_K:
464			A -= pc->k;
465			continue;
466
467		case BPF_ALU|BPF_MUL|BPF_K:
468			A *= pc->k;
469			continue;
470
471		case BPF_ALU|BPF_DIV|BPF_K:
472			A /= pc->k;
473			continue;
474
475		case BPF_ALU|BPF_AND|BPF_K:
476			A &= pc->k;
477			continue;
478
479		case BPF_ALU|BPF_OR|BPF_K:
480			A |= pc->k;
481			continue;
482
483		case BPF_ALU|BPF_LSH|BPF_K:
484			A <<= pc->k;
485			continue;
486
487		case BPF_ALU|BPF_RSH|BPF_K:
488			A >>= pc->k;
489			continue;
490
491		case BPF_ALU|BPF_NEG:
492			A = -A;
493			continue;
494
495		case BPF_MISC|BPF_TAX:
496			X = A;
497			continue;
498
499		case BPF_MISC|BPF_TXA:
500			A = X;
501			continue;
502		}
503	}
504}
505
506#ifdef _KERNEL
507/*
508 * Return true if the 'fcode' is a valid filter program.
509 * The constraints are that each jump be forward and to a valid
510 * code.  The code must terminate with either an accept or reject.
511 *
512 * The kernel needs to be able to verify an application's filter code.
513 * Otherwise, a bogus program could easily crash the system.
514 */
515int
516bpf_validate(f, len)
517	const struct bpf_insn *f;
518	int len;
519{
520	register int i;
521	register const struct bpf_insn *p;
522
523	for (i = 0; i < len; ++i) {
524		/*
525		 * Check that that jumps are forward, and within
526		 * the code block.
527		 */
528		p = &f[i];
529		if (BPF_CLASS(p->code) == BPF_JMP) {
530			register int from = i + 1;
531
532			if (BPF_OP(p->code) == BPF_JA) {
533				if (from >= len || p->k >= len - from)
534					return 0;
535			}
536			else if (from >= len || p->jt >= len - from ||
537				 p->jf >= len - from)
538				return 0;
539		}
540		/*
541		 * Check that memory operations use valid addresses.
542		 */
543		if ((BPF_CLASS(p->code) == BPF_ST ||
544		     (BPF_CLASS(p->code) == BPF_LD &&
545		      (p->code & 0xe0) == BPF_MEM)) &&
546		    p->k >= BPF_MEMWORDS)
547			return 0;
548		/*
549		 * Check for constant division by 0.
550		 */
551		if (p->code == (BPF_ALU|BPF_DIV|BPF_K) && p->k == 0)
552			return 0;
553	}
554	return BPF_CLASS(f[len - 1].code) == BPF_RET;
555}
556#endif
557