2005-01-07 01:45:51 +00:00
|
|
|
/*-
|
1994-05-24 10:09:53 +00:00
|
|
|
* Copyright (c) 1990, 1991, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from the Stanford/CMU enet packet filter,
|
|
|
|
* (net/enet.c) distributed as part of 4.3BSD, and code contributed
|
|
|
|
* to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
|
|
|
|
* Berkeley Laboratory.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2017-02-28 23:42:47 +00:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1994-05-24 10:09:53 +00:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* @(#)bpf_filter.c 8.1 (Berkeley) 6/10/93
|
|
|
|
*/
|
|
|
|
|
2007-12-25 13:24:02 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
|
2014-03-19 13:10:25 +00:00
|
|
|
#if !defined(_KERNEL)
|
|
|
|
#include <strings.h>
|
|
|
|
#endif
|
2008-08-26 00:09:26 +00:00
|
|
|
#if !defined(_KERNEL) || defined(sun)
|
1994-05-24 10:09:53 +00:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#endif
|
|
|
|
|
2002-07-05 00:06:08 +00:00
|
|
|
#ifndef __i386__
|
1994-05-24 10:09:53 +00:00
|
|
|
#define BPF_ALIGN
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef BPF_ALIGN
|
1998-10-31 10:45:03 +00:00
|
|
|
#define EXTRACT_SHORT(p) ((u_int16_t)ntohs(*(u_int16_t *)p))
|
|
|
|
#define EXTRACT_LONG(p) (ntohl(*(u_int32_t *)p))
|
1994-05-24 10:09:53 +00:00
|
|
|
#else
|
|
|
|
#define EXTRACT_SHORT(p)\
|
1998-10-31 10:45:03 +00:00
|
|
|
((u_int16_t)\
|
|
|
|
((u_int16_t)*((u_char *)p+0)<<8|\
|
|
|
|
(u_int16_t)*((u_char *)p+1)<<0))
|
1994-05-24 10:09:53 +00:00
|
|
|
#define EXTRACT_LONG(p)\
|
1998-10-31 10:45:03 +00:00
|
|
|
((u_int32_t)*((u_char *)p+0)<<24|\
|
|
|
|
(u_int32_t)*((u_char *)p+1)<<16|\
|
|
|
|
(u_int32_t)*((u_char *)p+2)<<8|\
|
|
|
|
(u_int32_t)*((u_char *)p+3)<<0)
|
1994-05-24 10:09:53 +00:00
|
|
|
#endif
|
|
|
|
|
1999-12-29 04:46:21 +00:00
|
|
|
#ifdef _KERNEL
|
1994-05-24 10:09:53 +00:00
|
|
|
#include <sys/mbuf.h>
|
2008-08-26 00:09:26 +00:00
|
|
|
#else
|
|
|
|
#include <stdlib.h>
|
1998-12-07 16:31:15 +00:00
|
|
|
#endif
|
|
|
|
#include <net/bpf.h>
|
1999-12-29 04:46:21 +00:00
|
|
|
#ifdef _KERNEL
|
1994-05-24 10:09:53 +00:00
|
|
|
#define MINDEX(m, k) \
|
|
|
|
{ \
|
2017-05-17 00:34:34 +00:00
|
|
|
int len = m->m_len; \
|
1994-05-24 10:09:53 +00:00
|
|
|
\
|
|
|
|
while (k >= len) { \
|
|
|
|
k -= len; \
|
|
|
|
m = m->m_next; \
|
|
|
|
if (m == 0) \
|
2008-08-29 19:32:04 +00:00
|
|
|
return (0); \
|
1994-05-24 10:09:53 +00:00
|
|
|
len = m->m_len; \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
2002-03-19 21:54:18 +00:00
|
|
|
static u_int16_t m_xhalf(struct mbuf *m, bpf_u_int32 k, int *err);
|
|
|
|
static u_int32_t m_xword(struct mbuf *m, bpf_u_int32 k, int *err);
|
1995-12-02 19:38:06 +00:00
|
|
|
|
1998-12-07 16:31:15 +00:00
|
|
|
static u_int32_t
|
2008-03-09 21:13:43 +00:00
|
|
|
m_xword(struct mbuf *m, bpf_u_int32 k, int *err)
|
1994-05-24 10:09:53 +00:00
|
|
|
{
|
2008-03-09 21:13:43 +00:00
|
|
|
size_t len;
|
|
|
|
u_char *cp, *np;
|
|
|
|
struct mbuf *m0;
|
1994-05-24 10:09:53 +00:00
|
|
|
|
|
|
|
len = m->m_len;
|
|
|
|
while (k >= len) {
|
|
|
|
k -= len;
|
|
|
|
m = m->m_next;
|
2016-04-15 17:30:33 +00:00
|
|
|
if (m == NULL)
|
1994-05-24 10:09:53 +00:00
|
|
|
goto bad;
|
|
|
|
len = m->m_len;
|
|
|
|
}
|
|
|
|
cp = mtod(m, u_char *) + k;
|
|
|
|
if (len - k >= 4) {
|
|
|
|
*err = 0;
|
2008-08-29 20:00:55 +00:00
|
|
|
return (EXTRACT_LONG(cp));
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
m0 = m->m_next;
|
2016-04-15 17:30:33 +00:00
|
|
|
if (m0 == NULL || m0->m_len + len - k < 4)
|
1994-05-24 10:09:53 +00:00
|
|
|
goto bad;
|
|
|
|
*err = 0;
|
|
|
|
np = mtod(m0, u_char *);
|
|
|
|
switch (len - k) {
|
|
|
|
case 1:
|
2008-08-29 19:32:04 +00:00
|
|
|
return (((u_int32_t)cp[0] << 24) |
|
1998-12-07 16:31:15 +00:00
|
|
|
((u_int32_t)np[0] << 16) |
|
|
|
|
((u_int32_t)np[1] << 8) |
|
2008-08-29 19:32:04 +00:00
|
|
|
(u_int32_t)np[2]);
|
1994-05-24 10:09:53 +00:00
|
|
|
|
|
|
|
case 2:
|
2008-08-29 19:32:04 +00:00
|
|
|
return (((u_int32_t)cp[0] << 24) |
|
1998-12-07 16:31:15 +00:00
|
|
|
((u_int32_t)cp[1] << 16) |
|
|
|
|
((u_int32_t)np[0] << 8) |
|
2008-08-29 19:32:04 +00:00
|
|
|
(u_int32_t)np[1]);
|
1994-05-24 10:09:53 +00:00
|
|
|
|
|
|
|
default:
|
2008-08-29 19:32:04 +00:00
|
|
|
return (((u_int32_t)cp[0] << 24) |
|
1998-12-07 16:31:15 +00:00
|
|
|
((u_int32_t)cp[1] << 16) |
|
|
|
|
((u_int32_t)cp[2] << 8) |
|
2008-08-29 19:32:04 +00:00
|
|
|
(u_int32_t)np[0]);
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
bad:
|
|
|
|
*err = 1;
|
2008-03-09 21:13:43 +00:00
|
|
|
return (0);
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
|
1998-12-07 16:31:15 +00:00
|
|
|
static u_int16_t
|
2008-03-09 21:13:43 +00:00
|
|
|
m_xhalf(struct mbuf *m, bpf_u_int32 k, int *err)
|
1994-05-24 10:09:53 +00:00
|
|
|
{
|
2008-03-09 21:13:43 +00:00
|
|
|
size_t len;
|
|
|
|
u_char *cp;
|
|
|
|
struct mbuf *m0;
|
1994-05-24 10:09:53 +00:00
|
|
|
|
|
|
|
len = m->m_len;
|
|
|
|
while (k >= len) {
|
|
|
|
k -= len;
|
|
|
|
m = m->m_next;
|
2016-04-15 17:30:33 +00:00
|
|
|
if (m == NULL)
|
1994-05-24 10:09:53 +00:00
|
|
|
goto bad;
|
|
|
|
len = m->m_len;
|
|
|
|
}
|
|
|
|
cp = mtod(m, u_char *) + k;
|
|
|
|
if (len - k >= 2) {
|
|
|
|
*err = 0;
|
2008-03-09 21:13:43 +00:00
|
|
|
return (EXTRACT_SHORT(cp));
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
m0 = m->m_next;
|
2016-04-15 17:30:33 +00:00
|
|
|
if (m0 == NULL)
|
1994-05-24 10:09:53 +00:00
|
|
|
goto bad;
|
|
|
|
*err = 0;
|
2008-03-09 21:13:43 +00:00
|
|
|
return ((cp[0] << 8) | mtod(m0, u_char *)[0]);
|
1994-05-24 10:09:53 +00:00
|
|
|
bad:
|
|
|
|
*err = 1;
|
2008-03-09 21:13:43 +00:00
|
|
|
return (0);
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Execute the filter program starting at pc on the packet p
|
|
|
|
* wirelen is the length of the original packet
|
|
|
|
* buflen is the amount of data present
|
|
|
|
*/
|
|
|
|
u_int
|
2008-03-09 21:13:43 +00:00
|
|
|
bpf_filter(const struct bpf_insn *pc, u_char *p, u_int wirelen, u_int buflen)
|
1994-05-24 10:09:53 +00:00
|
|
|
{
|
2008-03-09 21:13:43 +00:00
|
|
|
u_int32_t A = 0, X = 0;
|
|
|
|
bpf_u_int32 k;
|
2007-09-13 09:00:32 +00:00
|
|
|
u_int32_t mem[BPF_MEMWORDS];
|
1994-05-24 10:09:53 +00:00
|
|
|
|
2011-07-14 21:06:22 +00:00
|
|
|
bzero(mem, sizeof(mem));
|
|
|
|
|
2008-03-09 21:13:43 +00:00
|
|
|
if (pc == NULL)
|
1994-05-24 10:09:53 +00:00
|
|
|
/*
|
|
|
|
* No filter means accept all.
|
|
|
|
*/
|
2008-03-09 21:13:43 +00:00
|
|
|
return ((u_int)-1);
|
1994-05-25 09:21:21 +00:00
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
--pc;
|
|
|
|
while (1) {
|
|
|
|
++pc;
|
|
|
|
switch (pc->code) {
|
|
|
|
default:
|
1999-12-29 04:46:21 +00:00
|
|
|
#ifdef _KERNEL
|
2008-08-29 19:32:04 +00:00
|
|
|
return (0);
|
1994-05-24 10:09:53 +00:00
|
|
|
#else
|
|
|
|
abort();
|
1995-05-30 08:16:23 +00:00
|
|
|
#endif
|
2008-03-09 21:13:43 +00:00
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
case BPF_RET|BPF_K:
|
2008-03-09 21:13:43 +00:00
|
|
|
return ((u_int)pc->k);
|
1994-05-24 10:09:53 +00:00
|
|
|
|
|
|
|
case BPF_RET|BPF_A:
|
2008-03-09 21:13:43 +00:00
|
|
|
return ((u_int)A);
|
1994-05-24 10:09:53 +00:00
|
|
|
|
|
|
|
case BPF_LD|BPF_W|BPF_ABS:
|
|
|
|
k = pc->k;
|
1998-12-07 16:31:15 +00:00
|
|
|
if (k > buflen || sizeof(int32_t) > buflen - k) {
|
1999-12-29 04:46:21 +00:00
|
|
|
#ifdef _KERNEL
|
1994-05-24 10:09:53 +00:00
|
|
|
int merr;
|
|
|
|
|
|
|
|
if (buflen != 0)
|
2008-08-29 19:32:04 +00:00
|
|
|
return (0);
|
1994-05-24 10:09:53 +00:00
|
|
|
A = m_xword((struct mbuf *)p, k, &merr);
|
|
|
|
if (merr != 0)
|
2008-08-29 19:32:04 +00:00
|
|
|
return (0);
|
1994-05-24 10:09:53 +00:00
|
|
|
continue;
|
|
|
|
#else
|
2008-03-09 21:13:43 +00:00
|
|
|
return (0);
|
1994-05-24 10:09:53 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#ifdef BPF_ALIGN
|
1998-10-31 10:45:03 +00:00
|
|
|
if (((intptr_t)(p + k) & 3) != 0)
|
1994-05-24 10:09:53 +00:00
|
|
|
A = EXTRACT_LONG(&p[k]);
|
|
|
|
else
|
|
|
|
#endif
|
1998-10-31 10:45:03 +00:00
|
|
|
A = ntohl(*(int32_t *)(p + k));
|
1994-05-24 10:09:53 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
case BPF_LD|BPF_H|BPF_ABS:
|
|
|
|
k = pc->k;
|
1998-12-07 16:31:15 +00:00
|
|
|
if (k > buflen || sizeof(int16_t) > buflen - k) {
|
1999-12-29 04:46:21 +00:00
|
|
|
#ifdef _KERNEL
|
1994-05-24 10:09:53 +00:00
|
|
|
int merr;
|
|
|
|
|
|
|
|
if (buflen != 0)
|
2008-08-29 19:32:04 +00:00
|
|
|
return (0);
|
1994-05-24 10:09:53 +00:00
|
|
|
A = m_xhalf((struct mbuf *)p, k, &merr);
|
|
|
|
continue;
|
|
|
|
#else
|
2008-08-29 19:32:04 +00:00
|
|
|
return (0);
|
1994-05-24 10:09:53 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
A = EXTRACT_SHORT(&p[k]);
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case BPF_LD|BPF_B|BPF_ABS:
|
|
|
|
k = pc->k;
|
|
|
|
if (k >= buflen) {
|
1999-12-29 04:46:21 +00:00
|
|
|
#ifdef _KERNEL
|
2008-03-09 21:13:43 +00:00
|
|
|
struct mbuf *m;
|
1994-05-24 10:09:53 +00:00
|
|
|
|
|
|
|
if (buflen != 0)
|
2008-08-29 19:32:04 +00:00
|
|
|
return (0);
|
1994-05-24 10:09:53 +00:00
|
|
|
m = (struct mbuf *)p;
|
|
|
|
MINDEX(m, k);
|
|
|
|
A = mtod(m, u_char *)[k];
|
|
|
|
continue;
|
|
|
|
#else
|
2008-08-29 19:32:04 +00:00
|
|
|
return (0);
|
1994-05-24 10:09:53 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
A = p[k];
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case BPF_LD|BPF_W|BPF_LEN:
|
|
|
|
A = wirelen;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case BPF_LDX|BPF_W|BPF_LEN:
|
|
|
|
X = wirelen;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case BPF_LD|BPF_W|BPF_IND:
|
|
|
|
k = X + pc->k;
|
1999-04-11 02:52:31 +00:00
|
|
|
if (pc->k > buflen || X > buflen - pc->k ||
|
|
|
|
sizeof(int32_t) > buflen - k) {
|
1999-12-29 04:46:21 +00:00
|
|
|
#ifdef _KERNEL
|
1994-05-24 10:09:53 +00:00
|
|
|
int merr;
|
|
|
|
|
|
|
|
if (buflen != 0)
|
2008-03-09 21:13:43 +00:00
|
|
|
return (0);
|
1994-05-24 10:09:53 +00:00
|
|
|
A = m_xword((struct mbuf *)p, k, &merr);
|
|
|
|
if (merr != 0)
|
2008-03-09 21:13:43 +00:00
|
|
|
return (0);
|
1994-05-24 10:09:53 +00:00
|
|
|
continue;
|
|
|
|
#else
|
2008-03-09 21:13:43 +00:00
|
|
|
return (0);
|
1994-05-24 10:09:53 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#ifdef BPF_ALIGN
|
1998-10-31 10:45:03 +00:00
|
|
|
if (((intptr_t)(p + k) & 3) != 0)
|
1994-05-24 10:09:53 +00:00
|
|
|
A = EXTRACT_LONG(&p[k]);
|
|
|
|
else
|
|
|
|
#endif
|
1998-10-31 10:45:03 +00:00
|
|
|
A = ntohl(*(int32_t *)(p + k));
|
1994-05-24 10:09:53 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
case BPF_LD|BPF_H|BPF_IND:
|
|
|
|
k = X + pc->k;
|
1999-04-11 02:52:31 +00:00
|
|
|
if (X > buflen || pc->k > buflen - X ||
|
|
|
|
sizeof(int16_t) > buflen - k) {
|
1999-12-29 04:46:21 +00:00
|
|
|
#ifdef _KERNEL
|
1994-05-24 10:09:53 +00:00
|
|
|
int merr;
|
|
|
|
|
|
|
|
if (buflen != 0)
|
2008-08-29 19:32:04 +00:00
|
|
|
return (0);
|
1994-05-24 10:09:53 +00:00
|
|
|
A = m_xhalf((struct mbuf *)p, k, &merr);
|
|
|
|
if (merr != 0)
|
2008-03-09 21:13:43 +00:00
|
|
|
return (0);
|
1994-05-24 10:09:53 +00:00
|
|
|
continue;
|
|
|
|
#else
|
2008-03-09 21:13:43 +00:00
|
|
|
return (0);
|
1994-05-24 10:09:53 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
A = EXTRACT_SHORT(&p[k]);
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case BPF_LD|BPF_B|BPF_IND:
|
|
|
|
k = X + pc->k;
|
1999-07-04 08:07:35 +00:00
|
|
|
if (pc->k >= buflen || X >= buflen - pc->k) {
|
1999-12-29 04:46:21 +00:00
|
|
|
#ifdef _KERNEL
|
2008-03-09 21:13:43 +00:00
|
|
|
struct mbuf *m;
|
1994-05-24 10:09:53 +00:00
|
|
|
|
|
|
|
if (buflen != 0)
|
2008-08-29 19:32:04 +00:00
|
|
|
return (0);
|
1994-05-24 10:09:53 +00:00
|
|
|
m = (struct mbuf *)p;
|
|
|
|
MINDEX(m, k);
|
2006-05-28 20:00:02 +00:00
|
|
|
A = mtod(m, u_char *)[k];
|
1994-05-24 10:09:53 +00:00
|
|
|
continue;
|
|
|
|
#else
|
2008-03-09 21:13:43 +00:00
|
|
|
return (0);
|
1994-05-24 10:09:53 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
A = p[k];
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case BPF_LDX|BPF_MSH|BPF_B:
|
|
|
|
k = pc->k;
|
|
|
|
if (k >= buflen) {
|
1999-12-29 04:46:21 +00:00
|
|
|
#ifdef _KERNEL
|
2017-05-17 00:34:34 +00:00
|
|
|
struct mbuf *m;
|
1994-05-24 10:09:53 +00:00
|
|
|
|
|
|
|
if (buflen != 0)
|
2008-08-29 19:32:04 +00:00
|
|
|
return (0);
|
1994-05-24 10:09:53 +00:00
|
|
|
m = (struct mbuf *)p;
|
|
|
|
MINDEX(m, k);
|
2006-05-28 20:00:02 +00:00
|
|
|
X = (mtod(m, u_char *)[k] & 0xf) << 2;
|
1994-05-24 10:09:53 +00:00
|
|
|
continue;
|
|
|
|
#else
|
2008-08-29 19:32:04 +00:00
|
|
|
return (0);
|
1994-05-24 10:09:53 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
X = (p[pc->k] & 0xf) << 2;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case BPF_LD|BPF_IMM:
|
|
|
|
A = pc->k;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case BPF_LDX|BPF_IMM:
|
|
|
|
X = pc->k;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case BPF_LD|BPF_MEM:
|
|
|
|
A = mem[pc->k];
|
|
|
|
continue;
|
1995-05-30 08:16:23 +00:00
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
case BPF_LDX|BPF_MEM:
|
|
|
|
X = mem[pc->k];
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case BPF_ST:
|
|
|
|
mem[pc->k] = A;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case BPF_STX:
|
|
|
|
mem[pc->k] = X;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case BPF_JMP|BPF_JA:
|
|
|
|
pc += pc->k;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case BPF_JMP|BPF_JGT|BPF_K:
|
|
|
|
pc += (A > pc->k) ? pc->jt : pc->jf;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case BPF_JMP|BPF_JGE|BPF_K:
|
|
|
|
pc += (A >= pc->k) ? pc->jt : pc->jf;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case BPF_JMP|BPF_JEQ|BPF_K:
|
|
|
|
pc += (A == pc->k) ? pc->jt : pc->jf;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case BPF_JMP|BPF_JSET|BPF_K:
|
|
|
|
pc += (A & pc->k) ? pc->jt : pc->jf;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case BPF_JMP|BPF_JGT|BPF_X:
|
|
|
|
pc += (A > X) ? pc->jt : pc->jf;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case BPF_JMP|BPF_JGE|BPF_X:
|
|
|
|
pc += (A >= X) ? pc->jt : pc->jf;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case BPF_JMP|BPF_JEQ|BPF_X:
|
|
|
|
pc += (A == X) ? pc->jt : pc->jf;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case BPF_JMP|BPF_JSET|BPF_X:
|
|
|
|
pc += (A & X) ? pc->jt : pc->jf;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case BPF_ALU|BPF_ADD|BPF_X:
|
|
|
|
A += X;
|
|
|
|
continue;
|
1995-05-30 08:16:23 +00:00
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
case BPF_ALU|BPF_SUB|BPF_X:
|
|
|
|
A -= X;
|
|
|
|
continue;
|
1995-05-30 08:16:23 +00:00
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
case BPF_ALU|BPF_MUL|BPF_X:
|
|
|
|
A *= X;
|
|
|
|
continue;
|
1995-05-30 08:16:23 +00:00
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
case BPF_ALU|BPF_DIV|BPF_X:
|
|
|
|
if (X == 0)
|
2008-08-29 19:32:04 +00:00
|
|
|
return (0);
|
1994-05-24 10:09:53 +00:00
|
|
|
A /= X;
|
|
|
|
continue;
|
1995-05-30 08:16:23 +00:00
|
|
|
|
2016-10-21 06:55:07 +00:00
|
|
|
case BPF_ALU|BPF_MOD|BPF_X:
|
|
|
|
if (X == 0)
|
|
|
|
return (0);
|
|
|
|
A %= X;
|
|
|
|
continue;
|
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
case BPF_ALU|BPF_AND|BPF_X:
|
|
|
|
A &= X;
|
|
|
|
continue;
|
1995-05-30 08:16:23 +00:00
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
case BPF_ALU|BPF_OR|BPF_X:
|
|
|
|
A |= X;
|
|
|
|
continue;
|
|
|
|
|
2016-10-21 06:55:07 +00:00
|
|
|
case BPF_ALU|BPF_XOR|BPF_X:
|
|
|
|
A ^= X;
|
|
|
|
continue;
|
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
case BPF_ALU|BPF_LSH|BPF_X:
|
|
|
|
A <<= X;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case BPF_ALU|BPF_RSH|BPF_X:
|
|
|
|
A >>= X;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case BPF_ALU|BPF_ADD|BPF_K:
|
|
|
|
A += pc->k;
|
|
|
|
continue;
|
1995-05-30 08:16:23 +00:00
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
case BPF_ALU|BPF_SUB|BPF_K:
|
|
|
|
A -= pc->k;
|
|
|
|
continue;
|
1995-05-30 08:16:23 +00:00
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
case BPF_ALU|BPF_MUL|BPF_K:
|
|
|
|
A *= pc->k;
|
|
|
|
continue;
|
1995-05-30 08:16:23 +00:00
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
case BPF_ALU|BPF_DIV|BPF_K:
|
|
|
|
A /= pc->k;
|
|
|
|
continue;
|
1995-05-30 08:16:23 +00:00
|
|
|
|
2016-10-21 06:55:07 +00:00
|
|
|
case BPF_ALU|BPF_MOD|BPF_K:
|
|
|
|
A %= pc->k;
|
|
|
|
continue;
|
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
case BPF_ALU|BPF_AND|BPF_K:
|
|
|
|
A &= pc->k;
|
|
|
|
continue;
|
1995-05-30 08:16:23 +00:00
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
case BPF_ALU|BPF_OR|BPF_K:
|
|
|
|
A |= pc->k;
|
|
|
|
continue;
|
|
|
|
|
2016-10-21 06:55:07 +00:00
|
|
|
case BPF_ALU|BPF_XOR|BPF_K:
|
|
|
|
A ^= pc->k;
|
|
|
|
continue;
|
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
case BPF_ALU|BPF_LSH|BPF_K:
|
|
|
|
A <<= pc->k;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case BPF_ALU|BPF_RSH|BPF_K:
|
|
|
|
A >>= pc->k;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case BPF_ALU|BPF_NEG:
|
|
|
|
A = -A;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case BPF_MISC|BPF_TAX:
|
|
|
|
X = A;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case BPF_MISC|BPF_TXA:
|
|
|
|
A = X;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-12-29 04:46:21 +00:00
|
|
|
#ifdef _KERNEL
|
2008-08-29 19:10:51 +00:00
|
|
|
static const u_short bpf_code_map[] = {
|
2008-08-28 22:00:21 +00:00
|
|
|
0x10ff, /* 0x00-0x0f: 1111111100001000 */
|
|
|
|
0x3070, /* 0x10-0x1f: 0000111000001100 */
|
|
|
|
0x3131, /* 0x20-0x2f: 1000110010001100 */
|
|
|
|
0x3031, /* 0x30-0x3f: 1000110000001100 */
|
|
|
|
0x3131, /* 0x40-0x4f: 1000110010001100 */
|
|
|
|
0x1011, /* 0x50-0x5f: 1000100000001000 */
|
|
|
|
0x1013, /* 0x60-0x6f: 1100100000001000 */
|
|
|
|
0x1010, /* 0x70-0x7f: 0000100000001000 */
|
|
|
|
0x0093, /* 0x80-0x8f: 1100100100000000 */
|
2016-10-21 06:55:07 +00:00
|
|
|
0x1010, /* 0x90-0x9f: 0000100000001000 */
|
|
|
|
0x1010, /* 0xa0-0xaf: 0000100000001000 */
|
2008-08-28 22:00:21 +00:00
|
|
|
0x0002, /* 0xb0-0xbf: 0100000000000000 */
|
|
|
|
0x0000, /* 0xc0-0xcf: 0000000000000000 */
|
|
|
|
0x0000, /* 0xd0-0xdf: 0000000000000000 */
|
|
|
|
0x0000, /* 0xe0-0xef: 0000000000000000 */
|
|
|
|
0x0000 /* 0xf0-0xff: 0000000000000000 */
|
|
|
|
};
|
|
|
|
|
2008-08-29 19:10:51 +00:00
|
|
|
#define BPF_VALIDATE_CODE(c) \
|
|
|
|
((c) <= 0xff && (bpf_code_map[(c) >> 4] & (1 << ((c) & 0xf))) != 0)
|
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
/*
|
|
|
|
* Return true if the 'fcode' is a valid filter program.
|
|
|
|
* The constraints are that each jump be forward and to a valid
|
1995-05-30 08:16:23 +00:00
|
|
|
* code. The code must terminate with either an accept or reject.
|
1994-05-24 10:09:53 +00:00
|
|
|
*
|
|
|
|
* The kernel needs to be able to verify an application's filter code.
|
|
|
|
* Otherwise, a bogus program could easily crash the system.
|
|
|
|
*/
|
|
|
|
int
|
2008-08-29 19:32:04 +00:00
|
|
|
bpf_validate(const struct bpf_insn *f, int len)
|
1994-05-24 10:09:53 +00:00
|
|
|
{
|
2017-05-17 00:34:34 +00:00
|
|
|
int i;
|
|
|
|
const struct bpf_insn *p;
|
1994-05-24 10:09:53 +00:00
|
|
|
|
2006-01-03 20:34:41 +00:00
|
|
|
/* Do not accept negative length filter. */
|
|
|
|
if (len < 0)
|
2008-08-29 19:32:04 +00:00
|
|
|
return (0);
|
2006-01-03 20:34:41 +00:00
|
|
|
|
|
|
|
/* An empty filter means accept all. */
|
|
|
|
if (len == 0)
|
2008-08-29 19:32:04 +00:00
|
|
|
return (1);
|
2005-12-08 00:05:03 +00:00
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
for (i = 0; i < len; ++i) {
|
2008-08-28 22:00:21 +00:00
|
|
|
p = &f[i];
|
|
|
|
/*
|
|
|
|
* Check that the code is valid.
|
|
|
|
*/
|
2008-08-29 19:10:51 +00:00
|
|
|
if (!BPF_VALIDATE_CODE(p->code))
|
2008-08-29 19:32:04 +00:00
|
|
|
return (0);
|
1994-05-24 10:09:53 +00:00
|
|
|
/*
|
1995-05-30 08:16:23 +00:00
|
|
|
* Check that that jumps are forward, and within
|
1994-05-24 10:09:53 +00:00
|
|
|
* the code block.
|
|
|
|
*/
|
|
|
|
if (BPF_CLASS(p->code) == BPF_JMP) {
|
2017-05-17 00:34:34 +00:00
|
|
|
u_int offset;
|
1994-05-24 10:09:53 +00:00
|
|
|
|
2008-08-29 19:10:51 +00:00
|
|
|
if (p->code == (BPF_JMP|BPF_JA))
|
2008-08-29 01:47:45 +00:00
|
|
|
offset = p->k;
|
|
|
|
else
|
|
|
|
offset = p->jt > p->jf ? p->jt : p->jf;
|
|
|
|
if (offset >= (u_int)(len - i) - 1)
|
2008-08-29 19:32:04 +00:00
|
|
|
return (0);
|
2008-08-29 19:10:51 +00:00
|
|
|
continue;
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Check that memory operations use valid addresses.
|
|
|
|
*/
|
2008-08-29 19:10:51 +00:00
|
|
|
if (p->code == BPF_ST || p->code == BPF_STX ||
|
|
|
|
p->code == (BPF_LD|BPF_MEM) ||
|
|
|
|
p->code == (BPF_LDX|BPF_MEM)) {
|
|
|
|
if (p->k >= BPF_MEMWORDS)
|
2008-08-29 19:32:04 +00:00
|
|
|
return (0);
|
2008-08-29 19:10:51 +00:00
|
|
|
continue;
|
|
|
|
}
|
1994-05-24 10:09:53 +00:00
|
|
|
/*
|
|
|
|
* Check for constant division by 0.
|
|
|
|
*/
|
2016-10-21 06:55:07 +00:00
|
|
|
if ((p->code == (BPF_ALU|BPF_DIV|BPF_K) ||
|
|
|
|
p->code == (BPF_ALU|BPF_MOD|BPF_K)) && p->k == 0)
|
2008-08-29 19:32:04 +00:00
|
|
|
return (0);
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
2008-08-29 19:32:04 +00:00
|
|
|
return (BPF_CLASS(f[len - 1].code) == BPF_RET);
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
#endif
|