1999-12-03 21:21:49 +00:00
|
|
|
/*
|
|
|
|
* ng_bpf.c
|
2005-01-07 01:45:51 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*-
|
1999-12-03 21:21:49 +00:00
|
|
|
* Copyright (c) 1999 Whistle Communications, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Subject to the following obligations and disclaimer of warranty, use and
|
|
|
|
* redistribution of this software, in source or object code forms, with or
|
|
|
|
* without modifications are expressly permitted by Whistle Communications;
|
|
|
|
* provided, however, that:
|
|
|
|
* 1. Any and all reproductions of the source or object code must include the
|
|
|
|
* copyright notice above and the following disclaimer of warranties; and
|
|
|
|
* 2. No rights are granted, in any manner or form, to use Whistle
|
|
|
|
* Communications, Inc. trademarks, including the mark "WHISTLE
|
|
|
|
* COMMUNICATIONS" on advertising, endorsements, or otherwise except as
|
|
|
|
* such appears in the above copyright notice or in the software.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
|
|
|
|
* TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
|
|
|
|
* REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
|
|
|
|
* INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
|
2001-01-30 07:58:30 +00:00
|
|
|
* WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
|
1999-12-03 21:21:49 +00:00
|
|
|
* REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
|
|
|
|
* SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
|
|
|
|
* IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
|
|
|
|
* RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
|
|
|
|
* WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
|
|
|
* PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER 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 WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
|
|
|
|
* OF SUCH DAMAGE.
|
|
|
|
*
|
2000-10-24 17:32:45 +00:00
|
|
|
* Author: Archie Cobbs <archie@freebsd.org>
|
1999-12-03 21:21:49 +00:00
|
|
|
*
|
|
|
|
* $FreeBSD$
|
|
|
|
* $Whistle: ng_bpf.c,v 1.3 1999/12/03 20:30:23 archie Exp $
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* BPF NETGRAPH NODE TYPE
|
|
|
|
*
|
|
|
|
* This node type accepts any number of hook connections. With each hook
|
|
|
|
* is associated a bpf(4) filter program, and two hook names (each possibly
|
|
|
|
* the empty string). Incoming packets are compared against the filter;
|
|
|
|
* matching packets are delivered out the first named hook (or dropped if
|
|
|
|
* the empty string), and non-matching packets are delivered out the second
|
|
|
|
* named hook (or dropped if the empty string).
|
|
|
|
*
|
|
|
|
* Each hook also keeps statistics about how many packets have matched, etc.
|
|
|
|
*/
|
|
|
|
|
2005-12-07 21:30:47 +00:00
|
|
|
#include "opt_bpf.h"
|
|
|
|
|
1999-12-03 21:21:49 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/mbuf.h>
|
|
|
|
|
|
|
|
#include <net/bpf.h>
|
2005-12-07 21:30:47 +00:00
|
|
|
#ifdef BPF_JITTER
|
|
|
|
#include <net/bpf_jitter.h>
|
|
|
|
#endif
|
1999-12-03 21:21:49 +00:00
|
|
|
|
|
|
|
#include <netgraph/ng_message.h>
|
|
|
|
#include <netgraph/netgraph.h>
|
|
|
|
#include <netgraph/ng_parse.h>
|
|
|
|
#include <netgraph/ng_bpf.h>
|
|
|
|
|
2001-01-10 07:13:58 +00:00
|
|
|
#ifdef NG_SEPARATE_MALLOC
|
|
|
|
MALLOC_DEFINE(M_NETGRAPH_BPF, "netgraph_bpf", "netgraph bpf node ");
|
|
|
|
#else
|
|
|
|
#define M_NETGRAPH_BPF M_NETGRAPH
|
|
|
|
#endif
|
|
|
|
|
1999-12-03 21:21:49 +00:00
|
|
|
#define OFFSETOF(s, e) ((char *)&((s *)0)->e - (char *)((s *)0))
|
|
|
|
|
|
|
|
#define ERROUT(x) do { error = (x); goto done; } while (0)
|
|
|
|
|
|
|
|
/* Per hook private info */
|
|
|
|
struct ng_bpf_hookinfo {
|
|
|
|
hook_p hook;
|
2008-02-04 19:26:53 +00:00
|
|
|
hook_p match;
|
|
|
|
hook_p nomatch;
|
1999-12-03 21:21:49 +00:00
|
|
|
struct ng_bpf_hookprog *prog;
|
2005-12-07 21:30:47 +00:00
|
|
|
#ifdef BPF_JITTER
|
|
|
|
bpf_jit_filter *jit_prog;
|
|
|
|
#endif
|
1999-12-03 21:21:49 +00:00
|
|
|
struct ng_bpf_hookstat stats;
|
|
|
|
};
|
|
|
|
typedef struct ng_bpf_hookinfo *hinfo_p;
|
|
|
|
|
|
|
|
/* Netgraph methods */
|
|
|
|
static ng_constructor_t ng_bpf_constructor;
|
|
|
|
static ng_rcvmsg_t ng_bpf_rcvmsg;
|
2001-01-06 00:46:47 +00:00
|
|
|
static ng_shutdown_t ng_bpf_shutdown;
|
1999-12-03 21:21:49 +00:00
|
|
|
static ng_newhook_t ng_bpf_newhook;
|
|
|
|
static ng_rcvdata_t ng_bpf_rcvdata;
|
|
|
|
static ng_disconnect_t ng_bpf_disconnect;
|
|
|
|
|
2008-08-29 15:49:40 +00:00
|
|
|
/* Maximum bpf program instructions */
|
|
|
|
extern int bpf_maxinsns;
|
|
|
|
|
1999-12-03 21:21:49 +00:00
|
|
|
/* Internal helper functions */
|
|
|
|
static int ng_bpf_setprog(hook_p hook, const struct ng_bpf_hookprog *hp);
|
|
|
|
|
|
|
|
/* Parse type for one struct bfp_insn */
|
2002-05-31 23:48:03 +00:00
|
|
|
static const struct ng_parse_struct_field ng_bpf_insn_type_fields[] = {
|
2000-08-10 22:45:54 +00:00
|
|
|
{ "code", &ng_parse_hint16_type },
|
|
|
|
{ "jt", &ng_parse_uint8_type },
|
|
|
|
{ "jf", &ng_parse_uint8_type },
|
|
|
|
{ "k", &ng_parse_uint32_type },
|
1999-12-03 21:21:49 +00:00
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
static const struct ng_parse_type ng_bpf_insn_type = {
|
|
|
|
&ng_parse_struct_type,
|
2002-05-31 23:48:03 +00:00
|
|
|
&ng_bpf_insn_type_fields
|
1999-12-03 21:21:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Parse type for the field 'bpf_prog' in struct ng_bpf_hookprog */
|
|
|
|
static int
|
|
|
|
ng_bpf_hookprogary_getLength(const struct ng_parse_type *type,
|
|
|
|
const u_char *start, const u_char *buf)
|
|
|
|
{
|
|
|
|
const struct ng_bpf_hookprog *hp;
|
|
|
|
|
|
|
|
hp = (const struct ng_bpf_hookprog *)
|
|
|
|
(buf - OFFSETOF(struct ng_bpf_hookprog, bpf_prog));
|
|
|
|
return hp->bpf_prog_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct ng_parse_array_info ng_bpf_hookprogary_info = {
|
|
|
|
&ng_bpf_insn_type,
|
|
|
|
&ng_bpf_hookprogary_getLength,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
static const struct ng_parse_type ng_bpf_hookprogary_type = {
|
|
|
|
&ng_parse_array_type,
|
|
|
|
&ng_bpf_hookprogary_info
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Parse type for struct ng_bpf_hookprog */
|
2002-05-31 23:48:03 +00:00
|
|
|
static const struct ng_parse_struct_field ng_bpf_hookprog_type_fields[]
|
1999-12-03 21:21:49 +00:00
|
|
|
= NG_BPF_HOOKPROG_TYPE_INFO(&ng_bpf_hookprogary_type);
|
|
|
|
static const struct ng_parse_type ng_bpf_hookprog_type = {
|
|
|
|
&ng_parse_struct_type,
|
2002-05-31 23:48:03 +00:00
|
|
|
&ng_bpf_hookprog_type_fields
|
1999-12-03 21:21:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Parse type for struct ng_bpf_hookstat */
|
2002-05-31 23:48:03 +00:00
|
|
|
static const struct ng_parse_struct_field ng_bpf_hookstat_type_fields[]
|
|
|
|
= NG_BPF_HOOKSTAT_TYPE_INFO;
|
1999-12-03 21:21:49 +00:00
|
|
|
static const struct ng_parse_type ng_bpf_hookstat_type = {
|
|
|
|
&ng_parse_struct_type,
|
2002-05-31 23:48:03 +00:00
|
|
|
&ng_bpf_hookstat_type_fields
|
1999-12-03 21:21:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* List of commands and how to convert arguments to/from ASCII */
|
|
|
|
static const struct ng_cmdlist ng_bpf_cmdlist[] = {
|
|
|
|
{
|
|
|
|
NGM_BPF_COOKIE,
|
|
|
|
NGM_BPF_SET_PROGRAM,
|
|
|
|
"setprogram",
|
|
|
|
&ng_bpf_hookprog_type,
|
|
|
|
NULL
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NGM_BPF_COOKIE,
|
|
|
|
NGM_BPF_GET_PROGRAM,
|
|
|
|
"getprogram",
|
|
|
|
&ng_parse_hookbuf_type,
|
|
|
|
&ng_bpf_hookprog_type
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NGM_BPF_COOKIE,
|
|
|
|
NGM_BPF_GET_STATS,
|
|
|
|
"getstats",
|
|
|
|
&ng_parse_hookbuf_type,
|
|
|
|
&ng_bpf_hookstat_type
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NGM_BPF_COOKIE,
|
|
|
|
NGM_BPF_CLR_STATS,
|
|
|
|
"clrstats",
|
|
|
|
&ng_parse_hookbuf_type,
|
|
|
|
NULL
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NGM_BPF_COOKIE,
|
|
|
|
NGM_BPF_GETCLR_STATS,
|
|
|
|
"getclrstats",
|
|
|
|
&ng_parse_hookbuf_type,
|
|
|
|
&ng_bpf_hookstat_type
|
|
|
|
},
|
|
|
|
{ 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Netgraph type descriptor */
|
|
|
|
static struct ng_type typestruct = {
|
2004-05-29 00:51:19 +00:00
|
|
|
.version = NG_ABI_VERSION,
|
|
|
|
.name = NG_BPF_NODE_TYPE,
|
|
|
|
.constructor = ng_bpf_constructor,
|
|
|
|
.rcvmsg = ng_bpf_rcvmsg,
|
|
|
|
.shutdown = ng_bpf_shutdown,
|
|
|
|
.newhook = ng_bpf_newhook,
|
|
|
|
.rcvdata = ng_bpf_rcvdata,
|
|
|
|
.disconnect = ng_bpf_disconnect,
|
|
|
|
.cmdlist = ng_bpf_cmdlist,
|
1999-12-03 21:21:49 +00:00
|
|
|
};
|
|
|
|
NETGRAPH_INIT(bpf, &typestruct);
|
|
|
|
|
|
|
|
/* Default BPF program for a hook that matches nothing */
|
|
|
|
static const struct ng_bpf_hookprog ng_bpf_default_prog = {
|
|
|
|
{ '\0' }, /* to be filled in at hook creation time */
|
|
|
|
{ '\0' },
|
|
|
|
{ '\0' },
|
|
|
|
1,
|
|
|
|
{ BPF_STMT(BPF_RET+BPF_K, 0) }
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Node constructor
|
|
|
|
*
|
|
|
|
* We don't keep any per-node private data
|
2001-01-06 00:46:47 +00:00
|
|
|
* We go via the hooks.
|
1999-12-03 21:21:49 +00:00
|
|
|
*/
|
|
|
|
static int
|
2001-01-06 00:46:47 +00:00
|
|
|
ng_bpf_constructor(node_p node)
|
1999-12-03 21:21:49 +00:00
|
|
|
{
|
2001-01-08 05:34:06 +00:00
|
|
|
NG_NODE_SET_PRIVATE(node, NULL);
|
1999-12-03 21:21:49 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2008-02-04 19:26:53 +00:00
|
|
|
/*
|
|
|
|
* Callback functions to be used by NG_NODE_FOREACH_HOOK() macro.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ng_bpf_addrefs(hook_p hook, void* arg)
|
|
|
|
{
|
|
|
|
hinfo_p hip = NG_HOOK_PRIVATE(hook);
|
|
|
|
hook_p h = (hook_p)arg;
|
|
|
|
|
|
|
|
if (strcmp(hip->prog->ifMatch, NG_HOOK_NAME(h)) == 0)
|
|
|
|
hip->match = h;
|
|
|
|
if (strcmp(hip->prog->ifNotMatch, NG_HOOK_NAME(h)) == 0)
|
|
|
|
hip->nomatch = h;
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ng_bpf_remrefs(hook_p hook, void* arg)
|
|
|
|
{
|
|
|
|
hinfo_p hip = NG_HOOK_PRIVATE(hook);
|
|
|
|
hook_p h = (hook_p)arg;
|
|
|
|
|
|
|
|
if (hip->match == h)
|
|
|
|
hip->match = NULL;
|
|
|
|
if (hip->nomatch == h)
|
|
|
|
hip->nomatch = NULL;
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
1999-12-03 21:21:49 +00:00
|
|
|
/*
|
|
|
|
* Add a hook
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ng_bpf_newhook(node_p node, hook_p hook, const char *name)
|
|
|
|
{
|
|
|
|
hinfo_p hip;
|
2008-02-04 19:26:53 +00:00
|
|
|
hook_p tmp;
|
1999-12-03 21:21:49 +00:00
|
|
|
int error;
|
|
|
|
|
|
|
|
/* Create hook private structure */
|
2001-01-10 07:13:58 +00:00
|
|
|
MALLOC(hip, hinfo_p, sizeof(*hip), M_NETGRAPH_BPF, M_NOWAIT | M_ZERO);
|
1999-12-03 21:21:49 +00:00
|
|
|
if (hip == NULL)
|
|
|
|
return (ENOMEM);
|
|
|
|
hip->hook = hook;
|
2001-01-08 05:34:06 +00:00
|
|
|
NG_HOOK_SET_PRIVATE(hook, hip);
|
2008-02-04 19:26:53 +00:00
|
|
|
|
|
|
|
/* Add our reference into other hooks data. */
|
|
|
|
NG_NODE_FOREACH_HOOK(node, ng_bpf_addrefs, hook, tmp);
|
1999-12-03 21:21:49 +00:00
|
|
|
|
|
|
|
/* Attach the default BPF program */
|
|
|
|
if ((error = ng_bpf_setprog(hook, &ng_bpf_default_prog)) != 0) {
|
2001-01-10 07:13:58 +00:00
|
|
|
FREE(hip, M_NETGRAPH_BPF);
|
2001-01-08 05:34:06 +00:00
|
|
|
NG_HOOK_SET_PRIVATE(hook, NULL);
|
1999-12-03 21:21:49 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set hook name */
|
2008-02-04 19:26:53 +00:00
|
|
|
strlcpy(hip->prog->thisHook, name, sizeof(hip->prog->thisHook));
|
1999-12-03 21:21:49 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Receive a control message
|
|
|
|
*/
|
|
|
|
static int
|
2001-01-06 00:46:47 +00:00
|
|
|
ng_bpf_rcvmsg(node_p node, item_p item, hook_p lasthook)
|
1999-12-03 21:21:49 +00:00
|
|
|
{
|
2001-01-06 00:46:47 +00:00
|
|
|
struct ng_mesg *msg;
|
1999-12-03 21:21:49 +00:00
|
|
|
struct ng_mesg *resp = NULL;
|
|
|
|
int error = 0;
|
|
|
|
|
2001-01-06 00:46:47 +00:00
|
|
|
NGI_GET_MSG(item, msg);
|
1999-12-03 21:21:49 +00:00
|
|
|
switch (msg->header.typecookie) {
|
|
|
|
case NGM_BPF_COOKIE:
|
|
|
|
switch (msg->header.cmd) {
|
|
|
|
case NGM_BPF_SET_PROGRAM:
|
|
|
|
{
|
|
|
|
struct ng_bpf_hookprog *const
|
|
|
|
hp = (struct ng_bpf_hookprog *)msg->data;
|
|
|
|
hook_p hook;
|
|
|
|
|
|
|
|
/* Sanity check */
|
|
|
|
if (msg->header.arglen < sizeof(*hp)
|
1999-12-06 18:43:32 +00:00
|
|
|
|| msg->header.arglen
|
|
|
|
!= NG_BPF_HOOKPROG_SIZE(hp->bpf_prog_len))
|
1999-12-03 21:21:49 +00:00
|
|
|
ERROUT(EINVAL);
|
|
|
|
|
|
|
|
/* Find hook */
|
|
|
|
if ((hook = ng_findhook(node, hp->thisHook)) == NULL)
|
|
|
|
ERROUT(ENOENT);
|
|
|
|
|
|
|
|
/* Set new program */
|
|
|
|
if ((error = ng_bpf_setprog(hook, hp)) != 0)
|
|
|
|
ERROUT(error);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case NGM_BPF_GET_PROGRAM:
|
|
|
|
{
|
1999-12-06 18:43:32 +00:00
|
|
|
struct ng_bpf_hookprog *hp;
|
1999-12-03 21:21:49 +00:00
|
|
|
hook_p hook;
|
|
|
|
|
|
|
|
/* Sanity check */
|
|
|
|
if (msg->header.arglen == 0)
|
|
|
|
ERROUT(EINVAL);
|
|
|
|
msg->data[msg->header.arglen - 1] = '\0';
|
|
|
|
|
|
|
|
/* Find hook */
|
|
|
|
if ((hook = ng_findhook(node, msg->data)) == NULL)
|
|
|
|
ERROUT(ENOENT);
|
|
|
|
|
|
|
|
/* Build response */
|
2001-01-08 05:34:06 +00:00
|
|
|
hp = ((hinfo_p)NG_HOOK_PRIVATE(hook))->prog;
|
1999-12-03 21:21:49 +00:00
|
|
|
NG_MKRESPONSE(resp, msg,
|
1999-12-06 18:43:32 +00:00
|
|
|
NG_BPF_HOOKPROG_SIZE(hp->bpf_prog_len), M_NOWAIT);
|
1999-12-03 21:21:49 +00:00
|
|
|
if (resp == NULL)
|
|
|
|
ERROUT(ENOMEM);
|
1999-12-06 18:43:32 +00:00
|
|
|
bcopy(hp, resp->data,
|
|
|
|
NG_BPF_HOOKPROG_SIZE(hp->bpf_prog_len));
|
1999-12-03 21:21:49 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case NGM_BPF_GET_STATS:
|
|
|
|
case NGM_BPF_CLR_STATS:
|
|
|
|
case NGM_BPF_GETCLR_STATS:
|
|
|
|
{
|
|
|
|
struct ng_bpf_hookstat *stats;
|
|
|
|
hook_p hook;
|
|
|
|
|
|
|
|
/* Sanity check */
|
|
|
|
if (msg->header.arglen == 0)
|
|
|
|
ERROUT(EINVAL);
|
|
|
|
msg->data[msg->header.arglen - 1] = '\0';
|
|
|
|
|
|
|
|
/* Find hook */
|
|
|
|
if ((hook = ng_findhook(node, msg->data)) == NULL)
|
|
|
|
ERROUT(ENOENT);
|
2001-01-08 05:34:06 +00:00
|
|
|
stats = &((hinfo_p)NG_HOOK_PRIVATE(hook))->stats;
|
1999-12-03 21:21:49 +00:00
|
|
|
|
|
|
|
/* Build response (if desired) */
|
|
|
|
if (msg->header.cmd != NGM_BPF_CLR_STATS) {
|
|
|
|
NG_MKRESPONSE(resp,
|
|
|
|
msg, sizeof(*stats), M_NOWAIT);
|
|
|
|
if (resp == NULL)
|
|
|
|
ERROUT(ENOMEM);
|
|
|
|
bcopy(stats, resp->data, sizeof(*stats));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Clear stats (if desired) */
|
|
|
|
if (msg->header.cmd != NGM_BPF_GET_STATS)
|
|
|
|
bzero(stats, sizeof(*stats));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
error = EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error = EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
done:
|
2001-01-11 15:44:41 +00:00
|
|
|
NG_RESPOND_MSG(error, node, item, resp);
|
2001-01-06 00:46:47 +00:00
|
|
|
NG_FREE_MSG(msg);
|
1999-12-03 21:21:49 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Receive data on a hook
|
|
|
|
*
|
|
|
|
* Apply the filter, and then drop or forward packet as appropriate.
|
|
|
|
*/
|
|
|
|
static int
|
2001-01-06 00:46:47 +00:00
|
|
|
ng_bpf_rcvdata(hook_p hook, item_p item)
|
1999-12-03 21:21:49 +00:00
|
|
|
{
|
2001-01-08 05:34:06 +00:00
|
|
|
const hinfo_p hip = NG_HOOK_PRIVATE(hook);
|
2001-01-06 00:46:47 +00:00
|
|
|
int totlen;
|
2008-01-28 22:37:17 +00:00
|
|
|
int needfree = 0, error = 0, usejit = 0;
|
|
|
|
u_char *data = NULL;
|
1999-12-03 21:21:49 +00:00
|
|
|
hinfo_p dhip;
|
|
|
|
hook_p dest;
|
|
|
|
u_int len;
|
2001-01-06 00:46:47 +00:00
|
|
|
struct mbuf *m;
|
|
|
|
|
|
|
|
m = NGI_M(item); /* 'item' still owns it.. we are peeking */
|
|
|
|
totlen = m->m_pkthdr.len;
|
|
|
|
/* Update stats on incoming hook. XXX Can we do 64 bits atomically? */
|
|
|
|
/* atomic_add_int64(&hip->stats.recvFrames, 1); */
|
|
|
|
/* atomic_add_int64(&hip->stats.recvOctets, totlen); */
|
|
|
|
hip->stats.recvFrames++;
|
1999-12-03 21:21:49 +00:00
|
|
|
hip->stats.recvOctets += totlen;
|
|
|
|
|
2008-02-04 19:26:53 +00:00
|
|
|
/* Don't call bpf_filter() with totlen == 0! */
|
|
|
|
if (totlen == 0) {
|
|
|
|
len = 0;
|
|
|
|
goto ready;
|
|
|
|
}
|
|
|
|
|
2008-01-28 22:37:17 +00:00
|
|
|
#ifdef BPF_JITTER
|
|
|
|
if (bpf_jitter_enable != 0 && hip->jit_prog != NULL)
|
|
|
|
usejit = 1;
|
|
|
|
#endif
|
|
|
|
|
1999-12-03 21:21:49 +00:00
|
|
|
/* Need to put packet in contiguous memory for bpf */
|
2008-01-28 22:37:17 +00:00
|
|
|
if (m->m_next != NULL && totlen > MHLEN) {
|
|
|
|
if (usejit) {
|
2001-01-10 07:13:58 +00:00
|
|
|
MALLOC(data, u_char *, totlen, M_NETGRAPH_BPF, M_NOWAIT);
|
1999-12-03 21:21:49 +00:00
|
|
|
if (data == NULL) {
|
2001-01-06 00:46:47 +00:00
|
|
|
NG_FREE_ITEM(item);
|
1999-12-03 21:21:49 +00:00
|
|
|
return (ENOMEM);
|
|
|
|
}
|
|
|
|
needfree = 1;
|
2007-07-26 18:15:02 +00:00
|
|
|
m_copydata(m, 0, totlen, (caddr_t)data);
|
2008-01-28 22:37:17 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (m->m_next != NULL) {
|
2007-07-26 18:15:02 +00:00
|
|
|
NGI_M(item) = m = m_pullup(m, totlen);
|
|
|
|
if (m == NULL) {
|
|
|
|
NG_FREE_ITEM(item);
|
|
|
|
return (ENOBUFS);
|
|
|
|
}
|
|
|
|
}
|
1999-12-03 21:21:49 +00:00
|
|
|
data = mtod(m, u_char *);
|
2008-01-28 22:37:17 +00:00
|
|
|
}
|
1999-12-03 21:21:49 +00:00
|
|
|
|
|
|
|
/* Run packet through filter */
|
2005-12-07 21:30:47 +00:00
|
|
|
#ifdef BPF_JITTER
|
2008-02-04 19:26:53 +00:00
|
|
|
if (usejit)
|
|
|
|
len = (*(hip->jit_prog->func))(data, totlen, totlen);
|
|
|
|
else
|
2005-12-07 21:30:47 +00:00
|
|
|
#endif
|
2008-02-04 19:26:53 +00:00
|
|
|
if (data)
|
|
|
|
len = bpf_filter(hip->prog->bpf_prog, data, totlen, totlen);
|
|
|
|
else
|
|
|
|
len = bpf_filter(hip->prog->bpf_prog, (u_char *)m, totlen, 0);
|
1999-12-03 21:21:49 +00:00
|
|
|
if (needfree)
|
2001-01-10 07:13:58 +00:00
|
|
|
FREE(data, M_NETGRAPH_BPF);
|
2008-02-04 19:26:53 +00:00
|
|
|
ready:
|
1999-12-03 21:21:49 +00:00
|
|
|
/* See if we got a match and find destination hook */
|
|
|
|
if (len > 0) {
|
|
|
|
|
|
|
|
/* Update stats */
|
2001-01-06 00:46:47 +00:00
|
|
|
/* XXX atomically? */
|
1999-12-03 21:21:49 +00:00
|
|
|
hip->stats.recvMatchFrames++;
|
|
|
|
hip->stats.recvMatchOctets += totlen;
|
|
|
|
|
|
|
|
/* Truncate packet length if required by the filter */
|
2001-01-06 00:46:47 +00:00
|
|
|
/* Assume this never changes m */
|
1999-12-03 21:21:49 +00:00
|
|
|
if (len < totlen) {
|
|
|
|
m_adj(m, -(totlen - len));
|
2008-02-04 19:26:53 +00:00
|
|
|
totlen = len;
|
1999-12-03 21:21:49 +00:00
|
|
|
}
|
2008-02-04 19:26:53 +00:00
|
|
|
dest = hip->match;
|
1999-12-03 21:21:49 +00:00
|
|
|
} else
|
2008-02-04 19:26:53 +00:00
|
|
|
dest = hip->nomatch;
|
1999-12-03 21:21:49 +00:00
|
|
|
if (dest == NULL) {
|
2001-01-06 00:46:47 +00:00
|
|
|
NG_FREE_ITEM(item);
|
1999-12-03 21:21:49 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Deliver frame out destination hook */
|
2001-01-08 05:34:06 +00:00
|
|
|
dhip = NG_HOOK_PRIVATE(dest);
|
1999-12-03 21:21:49 +00:00
|
|
|
dhip->stats.xmitOctets += totlen;
|
|
|
|
dhip->stats.xmitFrames++;
|
2001-01-08 05:34:06 +00:00
|
|
|
NG_FWD_ITEM_HOOK(error, item, dest);
|
1999-12-03 21:21:49 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Shutdown processing
|
|
|
|
*/
|
|
|
|
static int
|
2001-01-06 00:46:47 +00:00
|
|
|
ng_bpf_shutdown(node_p node)
|
1999-12-03 21:21:49 +00:00
|
|
|
{
|
2001-01-08 05:34:06 +00:00
|
|
|
NG_NODE_UNREF(node);
|
1999-12-03 21:21:49 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Hook disconnection
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ng_bpf_disconnect(hook_p hook)
|
|
|
|
{
|
2008-02-04 19:26:53 +00:00
|
|
|
const node_p node = NG_HOOK_NODE(hook);
|
2001-01-08 05:34:06 +00:00
|
|
|
const hinfo_p hip = NG_HOOK_PRIVATE(hook);
|
2008-02-04 19:26:53 +00:00
|
|
|
hook_p tmp;
|
1999-12-03 21:21:49 +00:00
|
|
|
|
2001-12-10 08:09:49 +00:00
|
|
|
KASSERT(hip != NULL, ("%s: null info", __func__));
|
2008-02-04 19:26:53 +00:00
|
|
|
|
|
|
|
/* Remove our reference from other hooks data. */
|
|
|
|
NG_NODE_FOREACH_HOOK(node, ng_bpf_remrefs, hook, tmp);
|
|
|
|
|
2001-01-10 07:13:58 +00:00
|
|
|
FREE(hip->prog, M_NETGRAPH_BPF);
|
2005-12-07 21:30:47 +00:00
|
|
|
#ifdef BPF_JITTER
|
|
|
|
if (hip->jit_prog != NULL)
|
|
|
|
bpf_destroy_jit_filter(hip->jit_prog);
|
|
|
|
#endif
|
2001-01-10 07:13:58 +00:00
|
|
|
FREE(hip, M_NETGRAPH_BPF);
|
2008-02-04 19:26:53 +00:00
|
|
|
if ((NG_NODE_NUMHOOKS(node) == 0) &&
|
|
|
|
(NG_NODE_IS_VALID(node))) {
|
|
|
|
ng_rmnode_self(node);
|
2001-01-06 00:46:47 +00:00
|
|
|
}
|
1999-12-03 21:21:49 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/************************************************************************
|
|
|
|
HELPER STUFF
|
|
|
|
************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the BPF program associated with a hook
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ng_bpf_setprog(hook_p hook, const struct ng_bpf_hookprog *hp0)
|
|
|
|
{
|
2001-01-08 05:34:06 +00:00
|
|
|
const hinfo_p hip = NG_HOOK_PRIVATE(hook);
|
1999-12-03 21:21:49 +00:00
|
|
|
struct ng_bpf_hookprog *hp;
|
2005-12-07 21:41:45 +00:00
|
|
|
#ifdef BPF_JITTER
|
2005-12-07 21:30:47 +00:00
|
|
|
bpf_jit_filter *jit_prog;
|
2005-12-07 21:41:45 +00:00
|
|
|
#endif
|
1999-12-03 21:21:49 +00:00
|
|
|
int size;
|
|
|
|
|
|
|
|
/* Check program for validity */
|
2008-08-29 15:49:40 +00:00
|
|
|
if (hp0->bpf_prog_len > bpf_maxinsns ||
|
|
|
|
!bpf_validate(hp0->bpf_prog, hp0->bpf_prog_len))
|
1999-12-03 21:21:49 +00:00
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
/* Make a copy of the program */
|
1999-12-06 18:43:32 +00:00
|
|
|
size = NG_BPF_HOOKPROG_SIZE(hp0->bpf_prog_len);
|
2001-01-10 07:13:58 +00:00
|
|
|
MALLOC(hp, struct ng_bpf_hookprog *, size, M_NETGRAPH_BPF, M_NOWAIT);
|
1999-12-03 21:21:49 +00:00
|
|
|
if (hp == NULL)
|
|
|
|
return (ENOMEM);
|
|
|
|
bcopy(hp0, hp, size);
|
2005-12-07 21:30:47 +00:00
|
|
|
#ifdef BPF_JITTER
|
|
|
|
jit_prog = bpf_jitter(hp->bpf_prog, hp->bpf_prog_len);
|
|
|
|
#endif
|
1999-12-03 21:21:49 +00:00
|
|
|
|
|
|
|
/* Free previous program, if any, and assign new one */
|
|
|
|
if (hip->prog != NULL)
|
2001-01-10 07:13:58 +00:00
|
|
|
FREE(hip->prog, M_NETGRAPH_BPF);
|
1999-12-03 21:21:49 +00:00
|
|
|
hip->prog = hp;
|
2005-12-07 21:30:47 +00:00
|
|
|
#ifdef BPF_JITTER
|
|
|
|
if (hip->jit_prog != NULL)
|
|
|
|
bpf_destroy_jit_filter(hip->jit_prog);
|
|
|
|
hip->jit_prog = jit_prog;
|
2005-12-07 21:41:45 +00:00
|
|
|
#endif
|
2008-02-04 19:26:53 +00:00
|
|
|
|
|
|
|
/* Prepare direct references on target hooks. */
|
|
|
|
hip->match = ng_findhook(NG_HOOK_NODE(hook), hip->prog->ifMatch);
|
|
|
|
hip->nomatch = ng_findhook(NG_HOOK_NODE(hook), hip->prog->ifNotMatch);
|
1999-12-03 21:21:49 +00:00
|
|
|
return (0);
|
|
|
|
}
|