#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>
#include <netgraph/ng_message.h>
#include <netgraph/netgraph.h>
#include <netgraph/ng_parse.h>
#include "ng_bpf.h"
#define ERROUT(x) do { error = (x); goto done; } while (0)
struct ng_bpf_hookinfo {
node_p node;
hook_p hook;
struct ng_bpf_hookprog *prog;
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_rmnode;
static ng_newhook_t ng_bpf_newhook;
static ng_rcvdata_t ng_bpf_rcvdata;
static ng_disconnect_t ng_bpf_disconnect;
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 = {
NG_VERSION,
NG_BPF_NODE_TYPE,
NULL,
ng_bpf_constructor,
ng_bpf_rcvmsg,
ng_bpf_rmnode,
ng_bpf_newhook,
NULL,
NULL,
ng_bpf_rcvdata,
ng_bpf_rcvdata,
ng_bpf_disconnect,
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 *nodep)
{
int error = 0;
if ((error = ng_make_node_common(&typestruct, nodep)))
return (error);
(*nodep)->private = NULL;
return (0);
}
static int
ng_bpf_newhook(node_p node, hook_p hook, const char *name)
{
hinfo_p hip;
int error;
hip = kmalloc(sizeof(*hip), M_NETGRAPH, M_NOWAIT | M_ZERO);
if (hip == NULL)
return (ENOMEM);
hip->hook = hook;
hook->private = hip;
hip->node = node;
if ((error = ng_bpf_setprog(hook, &ng_bpf_default_prog)) != 0) {
kfree(hip, M_NETGRAPH);
hook->private = NULL;
return (error);
}
strncpy(hip->prog->thisHook, name, sizeof(hip->prog->thisHook) - 1);
hip->prog->thisHook[sizeof(hip->prog->thisHook) - 1] = '\0';
return (0);
}
static int
ng_bpf_rcvmsg(node_p node, struct ng_mesg *msg, const char *retaddr,
struct ng_mesg **rptr)
{
struct ng_mesg *resp = NULL;
int error = 0;
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)hook->private)->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)hook->private)->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;
}
if (rptr)
*rptr = resp;
else if (resp)
kfree(resp, M_NETGRAPH);
done:
kfree(msg, M_NETGRAPH);
return (error);
}
static int
ng_bpf_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
{
const hinfo_p hip = hook->private;
int totlen = m->m_pkthdr.len;
int needfree = 0, error = 0;
u_char *data, buf[256];
hinfo_p dhip;
hook_p dest;
u_int len;
hip->stats.recvFrames++;
hip->stats.recvOctets += totlen;
if (m->m_next != NULL) {
if (totlen > sizeof(buf)) {
data = kmalloc(totlen, M_NETGRAPH, M_NOWAIT);
if (data == NULL) {
NG_FREE_DATA(m, meta);
return (ENOMEM);
}
needfree = 1;
} else
data = buf;
m_copydata(m, 0, totlen, data);
} else
data = mtod(m, u_char *);
len = bpf_filter(hip->prog->bpf_prog, data, totlen, totlen);
if (needfree)
kfree(data, M_NETGRAPH);
if (len > 0) {
hip->stats.recvMatchFrames++;
hip->stats.recvMatchOctets += totlen;
if (len < totlen) {
m_adj(m, -(totlen - len));
totlen -= len;
}
dest = ng_findhook(hip->node, hip->prog->ifMatch);
} else
dest = ng_findhook(hip->node, hip->prog->ifNotMatch);
if (dest == NULL) {
NG_FREE_DATA(m, meta);
return (0);
}
dhip = (hinfo_p)dest->private;
dhip->stats.xmitOctets += totlen;
dhip->stats.xmitFrames++;
NG_SEND_DATA(error, dest, m, meta);
return (error);
}
static int
ng_bpf_rmnode(node_p node)
{
node->flags |= NG_INVALID;
ng_cutlinks(node);
ng_unname(node);
ng_unref(node);
return (0);
}
static int
ng_bpf_disconnect(hook_p hook)
{
const hinfo_p hip = hook->private;
KASSERT(hip != NULL, ("%s: null info", __func__));
kfree(hip->prog, M_NETGRAPH);
bzero(hip, sizeof(*hip));
kfree(hip, M_NETGRAPH);
hook->private = NULL;
if (hook->node->numhooks == 0)
ng_rmnode(hook->node);
return (0);
}
static int
ng_bpf_setprog(hook_p hook, const struct ng_bpf_hookprog *hp0)
{
const hinfo_p hip = hook->private;
struct ng_bpf_hookprog *hp;
int size;
if (!bpf_validate(hp0->bpf_prog, hp0->bpf_prog_len))
return (EINVAL);
size = NG_BPF_HOOKPROG_SIZE(hp0->bpf_prog_len);
hp = kmalloc(size, M_NETGRAPH, M_NOWAIT);
if (hp == NULL)
return (ENOMEM);
bcopy(hp0, hp, size);
if (hip->prog != NULL)
kfree(hip->prog, M_NETGRAPH);
hip->prog = hp;
return (0);
}