#include "opt_bpf.h"
#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>
#ifdef BPF_JITTER
#include <net/bpf_jitter.h>
#endif
#include <netgraph/ng_message.h>
#include <netgraph/netgraph.h>
#include <netgraph/ng_parse.h>
#include <netgraph/ng_bpf.h>
#ifdef NG_SEPARATE_MALLOC
static MALLOC_DEFINE(M_NETGRAPH_BPF, "netgraph_bpf", "netgraph bpf node");
#else
#define M_NETGRAPH_BPF M_NETGRAPH
#endif
#define OFFSETOF(s, e) ((char *)&((s *)0)->e - (char *)((s *)0))
#define ERROUT(x) do { error = (x); goto done; } while (0)
struct ng_bpf_hookinfo {
hook_p hook;
hook_p match;
hook_p nomatch;
struct ng_bpf_hookprog *prog;
#ifdef BPF_JITTER
bpf_jit_filter *jit_prog;
#endif
struct ng_bpf_hookstat stats;
};
typedef struct ng_bpf_hookinfo *hinfo_p;
static ng_constructor_t ng_bpf_constructor;
static ng_rcvmsg_t ng_bpf_rcvmsg;
static ng_shutdown_t ng_bpf_shutdown;
static ng_newhook_t ng_bpf_newhook;
static ng_rcvdata_t ng_bpf_rcvdata;
static ng_disconnect_t ng_bpf_disconnect;
extern int bpf_maxinsns;
static int ng_bpf_setprog(hook_p hook, const struct ng_bpf_hookprog *hp);
static const struct ng_parse_struct_field ng_bpf_insn_type_fields[] = {
{ "code", &ng_parse_hint16_type },
{ "jt", &ng_parse_uint8_type },
{ "jf", &ng_parse_uint8_type },
{ "k", &ng_parse_uint32_type },
{ NULL }
};
static const struct ng_parse_type ng_bpf_insn_type = {
&ng_parse_struct_type,
&ng_bpf_insn_type_fields
};
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
};
static const struct ng_parse_struct_field ng_bpf_hookprog_type_fields[]
= NG_BPF_HOOKPROG_TYPE_INFO(&ng_bpf_hookprogary_type);
static const struct ng_parse_type ng_bpf_hookprog_type = {
&ng_parse_struct_type,
&ng_bpf_hookprog_type_fields
};
static const struct ng_parse_struct_field ng_bpf_hookstat_type_fields[]
= NG_BPF_HOOKSTAT_TYPE_INFO;
static const struct ng_parse_type ng_bpf_hookstat_type = {
&ng_parse_struct_type,
&ng_bpf_hookstat_type_fields
};
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 }
};
static struct ng_type typestruct = {
.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,
};
NETGRAPH_INIT(bpf, &typestruct);
static const struct ng_bpf_hookprog ng_bpf_default_prog = {
{ '\0' },
{ '\0' },
{ '\0' },
1,
{ BPF_STMT(BPF_RET+BPF_K, 0) }
};
static int
ng_bpf_constructor(node_p node)
{
NG_NODE_SET_PRIVATE(node, NULL);
return (0);
}
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);
}
static int
ng_bpf_newhook(node_p node, hook_p hook, const char *name)
{
hinfo_p hip;
int error;
hip = malloc(sizeof(*hip), M_NETGRAPH_BPF, M_NOWAIT | M_ZERO);
if (hip == NULL)
return (ENOMEM);
hip->hook = hook;
NG_HOOK_SET_PRIVATE(hook, hip);
NG_NODE_FOREACH_HOOK(node, ng_bpf_addrefs, hook);
if ((error = ng_bpf_setprog(hook, &ng_bpf_default_prog)) != 0) {
free(hip, M_NETGRAPH_BPF);
NG_HOOK_SET_PRIVATE(hook, NULL);
return (error);
}
strlcpy(hip->prog->thisHook, name, sizeof(hip->prog->thisHook));
return (0);
}
static int
ng_bpf_rcvmsg(node_p node, item_p item, hook_p lasthook)
{
struct ng_mesg *msg;
struct ng_mesg *resp = NULL;
int error = 0;
NGI_GET_MSG(item, msg);
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;
if (msg->header.arglen < sizeof(*hp)
|| msg->header.arglen
!= NG_BPF_HOOKPROG_SIZE(hp->bpf_prog_len))
ERROUT(EINVAL);
if ((hook = ng_findhook(node, hp->thisHook)) == NULL)
ERROUT(ENOENT);
if ((error = ng_bpf_setprog(hook, hp)) != 0)
ERROUT(error);
break;
}
case NGM_BPF_GET_PROGRAM:
{
struct ng_bpf_hookprog *hp;
hook_p hook;
if (msg->header.arglen == 0)
ERROUT(EINVAL);
msg->data[msg->header.arglen - 1] = '\0';
if ((hook = ng_findhook(node, msg->data)) == NULL)
ERROUT(ENOENT);
hp = ((hinfo_p)NG_HOOK_PRIVATE(hook))->prog;
NG_MKRESPONSE(resp, msg,
NG_BPF_HOOKPROG_SIZE(hp->bpf_prog_len), M_NOWAIT);
if (resp == NULL)
ERROUT(ENOMEM);
bcopy(hp, resp->data,
NG_BPF_HOOKPROG_SIZE(hp->bpf_prog_len));
break;
}
case NGM_BPF_GET_STATS:
case NGM_BPF_CLR_STATS:
case NGM_BPF_GETCLR_STATS:
{
struct ng_bpf_hookstat *stats;
hook_p hook;
if (msg->header.arglen == 0)
ERROUT(EINVAL);
msg->data[msg->header.arglen - 1] = '\0';
if ((hook = ng_findhook(node, msg->data)) == NULL)
ERROUT(ENOENT);
stats = &((hinfo_p)NG_HOOK_PRIVATE(hook))->stats;
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));
}
if (msg->header.cmd != NGM_BPF_GET_STATS)
bzero(stats, sizeof(*stats));
break;
}
default:
error = EINVAL;
break;
}
break;
default:
error = EINVAL;
break;
}
done:
NG_RESPOND_MSG(error, node, item, resp);
NG_FREE_MSG(msg);
return (error);
}
static int
ng_bpf_rcvdata(hook_p hook, item_p item)
{
const hinfo_p hip = NG_HOOK_PRIVATE(hook);
int totlen;
int needfree = 0, error = 0, usejit = 0;
u_char *data = NULL;
hinfo_p dhip;
hook_p dest;
u_int len;
struct mbuf *m;
m = NGI_M(item);
totlen = m->m_pkthdr.len;
hip->stats.recvFrames++;
hip->stats.recvOctets += totlen;
if (totlen == 0) {
len = 0;
goto ready;
}
#ifdef BPF_JITTER
if (bpf_jitter_enable != 0 && hip->jit_prog != NULL)
usejit = 1;
#endif
if (m->m_next != NULL && totlen > MHLEN) {
if (usejit) {
data = malloc(totlen, M_NETGRAPH_BPF, M_NOWAIT);
if (data == NULL) {
NG_FREE_ITEM(item);
return (ENOMEM);
}
needfree = 1;
m_copydata(m, 0, totlen, (caddr_t)data);
}
} else {
if (m->m_next != NULL) {
NGI_M(item) = m = m_pullup(m, totlen);
if (m == NULL) {
NG_FREE_ITEM(item);
return (ENOBUFS);
}
}
data = mtod(m, u_char *);
}
#ifdef BPF_JITTER
if (usejit)
len = (*(hip->jit_prog->func))(data, totlen, totlen);
else
#endif
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);
if (needfree)
free(data, M_NETGRAPH_BPF);
ready:
if (len > 0) {
hip->stats.recvMatchFrames++;
hip->stats.recvMatchOctets += totlen;
if (len < totlen) {
m_adj(m, -(totlen - len));
totlen = len;
}
dest = hip->match;
} else
dest = hip->nomatch;
if (dest == NULL) {
NG_FREE_ITEM(item);
return (0);
}
dhip = NG_HOOK_PRIVATE(dest);
dhip->stats.xmitOctets += totlen;
dhip->stats.xmitFrames++;
NG_FWD_ITEM_HOOK(error, item, dest);
return (error);
}
static int
ng_bpf_shutdown(node_p node)
{
NG_NODE_UNREF(node);
return (0);
}
static int
ng_bpf_disconnect(hook_p hook)
{
const node_p node = NG_HOOK_NODE(hook);
const hinfo_p hip = NG_HOOK_PRIVATE(hook);
KASSERT(hip != NULL, ("%s: null info", __func__));
NG_NODE_FOREACH_HOOK(node, ng_bpf_remrefs, hook);
free(hip->prog, M_NETGRAPH_BPF);
#ifdef BPF_JITTER
if (hip->jit_prog != NULL)
bpf_destroy_jit_filter(hip->jit_prog);
#endif
free(hip, M_NETGRAPH_BPF);
if ((NG_NODE_NUMHOOKS(node) == 0) &&
(NG_NODE_IS_VALID(node))) {
ng_rmnode_self(node);
}
return (0);
}
static int
ng_bpf_setprog(hook_p hook, const struct ng_bpf_hookprog *hp0)
{
const hinfo_p hip = NG_HOOK_PRIVATE(hook);
struct ng_bpf_hookprog *hp;
#ifdef BPF_JITTER
bpf_jit_filter *jit_prog;
#endif
int size;
if (hp0->bpf_prog_len > bpf_maxinsns ||
!bpf_validate(hp0->bpf_prog, hp0->bpf_prog_len))
return (EINVAL);
size = NG_BPF_HOOKPROG_SIZE(hp0->bpf_prog_len);
hp = malloc(size, M_NETGRAPH_BPF, M_NOWAIT);
if (hp == NULL)
return (ENOMEM);
bcopy(hp0, hp, size);
#ifdef BPF_JITTER
jit_prog = bpf_jitter(hp->bpf_prog, hp->bpf_prog_len);
#endif
if (hip->prog != NULL)
free(hip->prog, M_NETGRAPH_BPF);
hip->prog = hp;
#ifdef BPF_JITTER
if (hip->jit_prog != NULL)
bpf_destroy_jit_filter(hip->jit_prog);
hip->jit_prog = jit_prog;
#endif
hip->match = ng_findhook(NG_HOOK_NODE(hook), hip->prog->ifMatch);
hip->nomatch = ng_findhook(NG_HOOK_NODE(hook), hip->prog->ifNotMatch);
return (0);
}