#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/mbuf.h>
#include <sys/malloc.h>
#include <sys/ctype.h>
#include <sys/errno.h>
#include <sys/queue.h>
#include <sys/syslog.h>
#include <net/ethernet.h>
#include <netgraph/ng_message.h>
#include <netgraph/ng_parse.h>
#include <netgraph/ng_etf.h>
#include <netgraph/netgraph.h>
#ifdef NG_SEPARATE_MALLOC
static MALLOC_DEFINE(M_NETGRAPH_ETF, "netgraph_etf", "netgraph etf node ");
#else
#define M_NETGRAPH_ETF M_NETGRAPH
#endif
static ng_constructor_t ng_etf_constructor;
static ng_rcvmsg_t ng_etf_rcvmsg;
static ng_shutdown_t ng_etf_shutdown;
static ng_newhook_t ng_etf_newhook;
static ng_rcvdata_t ng_etf_rcvdata;
static ng_disconnect_t ng_etf_disconnect;
static const struct ng_parse_struct_field ng_etf_stat_type_fields[]
= NG_ETF_STATS_TYPE_INFO;
static const struct ng_parse_type ng_etf_stat_type = {
&ng_parse_struct_type,
&ng_etf_stat_type_fields
};
static const struct ng_parse_struct_field ng_etf_filter_type_fields[]
= NG_ETF_FILTER_TYPE_INFO;
static const struct ng_parse_type ng_etf_filter_type = {
&ng_parse_struct_type,
&ng_etf_filter_type_fields
};
static const struct ng_cmdlist ng_etf_cmdlist[] = {
{
NGM_ETF_COOKIE,
NGM_ETF_GET_STATUS,
"getstatus",
NULL,
&ng_etf_stat_type,
},
{
NGM_ETF_COOKIE,
NGM_ETF_SET_FLAG,
"setflag",
&ng_parse_int32_type,
NULL
},
{
NGM_ETF_COOKIE,
NGM_ETF_SET_FILTER,
"setfilter",
&ng_etf_filter_type,
NULL
},
{ 0 }
};
static struct ng_type typestruct = {
.version = NG_ABI_VERSION,
.name = NG_ETF_NODE_TYPE,
.constructor = ng_etf_constructor,
.rcvmsg = ng_etf_rcvmsg,
.shutdown = ng_etf_shutdown,
.newhook = ng_etf_newhook,
.rcvdata = ng_etf_rcvdata,
.disconnect = ng_etf_disconnect,
.cmdlist = ng_etf_cmdlist,
};
NETGRAPH_INIT(etf, &typestruct);
struct ETF_hookinfo {
hook_p hook;
};
struct filter {
LIST_ENTRY(filter) next;
u_int16_t ethertype;
hook_p match_hook;
};
#define HASHSIZE 16
#define HASH(et) ((((et)>>12)+((et)>>8)+((et)>>4)+(et)) & 0x0f)
LIST_HEAD(filterhead, filter);
struct ETF {
struct ETF_hookinfo downstream_hook;
struct ETF_hookinfo nomatch_hook;
node_p node;
u_int packets_in;
u_int packets_out;
u_int32_t flags;
struct filterhead hashtable[HASHSIZE];
};
typedef struct ETF *etf_p;
static struct filter *
ng_etf_findentry(etf_p etfp, u_int16_t ethertype)
{
struct filterhead *chain = etfp->hashtable + HASH(ethertype);
struct filter *fil;
LIST_FOREACH(fil, chain, next) {
if (fil->ethertype == ethertype) {
return (fil);
}
}
return (NULL);
}
static int
ng_etf_constructor(node_p node)
{
etf_p privdata;
int i;
privdata = malloc(sizeof(*privdata), M_NETGRAPH_ETF, M_WAITOK | M_ZERO);
for (i = 0; i < HASHSIZE; i++) {
LIST_INIT((privdata->hashtable + i));
}
NG_NODE_SET_PRIVATE(node, privdata);
privdata->node = node;
return (0);
}
static int
ng_etf_newhook(node_p node, hook_p hook, const char *name)
{
const etf_p etfp = NG_NODE_PRIVATE(node);
struct ETF_hookinfo *hpriv;
if (strcmp(name, NG_ETF_HOOK_DOWNSTREAM) == 0) {
etfp->downstream_hook.hook = hook;
NG_HOOK_SET_PRIVATE(hook, &etfp->downstream_hook);
etfp->packets_in = 0;
etfp->packets_out = 0;
} else if (strcmp(name, NG_ETF_HOOK_NOMATCH) == 0) {
etfp->nomatch_hook.hook = hook;
NG_HOOK_SET_PRIVATE(hook, &etfp->nomatch_hook);
} else {
hpriv = malloc(sizeof(*hpriv),
M_NETGRAPH_ETF, M_NOWAIT | M_ZERO);
if (hpriv == NULL) {
return (ENOMEM);
}
NG_HOOK_SET_PRIVATE(hook, hpriv);
hpriv->hook = hook;
}
return(0);
}
static int
ng_etf_rcvmsg(node_p node, item_p item, hook_p lasthook)
{
const etf_p etfp = NG_NODE_PRIVATE(node);
struct ng_mesg *resp = NULL;
int error = 0;
struct ng_mesg *msg;
NGI_GET_MSG(item, msg);
switch (msg->header.typecookie) {
case NGM_ETF_COOKIE:
switch (msg->header.cmd) {
case NGM_ETF_GET_STATUS:
{
struct ng_etfstat *stats;
NG_MKRESPONSE(resp, msg, sizeof(*stats), M_NOWAIT);
if (!resp) {
error = ENOMEM;
break;
}
stats = (struct ng_etfstat *) resp->data;
stats->packets_in = etfp->packets_in;
stats->packets_out = etfp->packets_out;
break;
}
case NGM_ETF_SET_FLAG:
if (msg->header.arglen != sizeof(u_int32_t)) {
error = EINVAL;
break;
}
etfp->flags = *((u_int32_t *) msg->data);
break;
case NGM_ETF_SET_FILTER:
{
struct ng_etffilter *f;
struct filter *fil;
hook_p hook;
if (msg->header.arglen != sizeof(*f)) {
error = EINVAL;
break;
}
f = (struct ng_etffilter *)msg->data;
hook = ng_findhook(node, f->matchhook);
if (hook == NULL) {
error = ENOENT;
break;
}
if (hook == etfp->downstream_hook.hook) {
error = EINVAL;
break;
}
if (ng_etf_findentry(etfp,
htons(f->ethertype))) {
error = EEXIST;
break;
}
fil = malloc(sizeof(*fil),
M_NETGRAPH_ETF, M_NOWAIT | M_ZERO);
if (fil == NULL) {
error = ENOMEM;
break;
}
fil->match_hook = hook;
fil->ethertype = htons(f->ethertype);
LIST_INSERT_HEAD( etfp->hashtable
+ HASH(fil->ethertype),
fil, next);
}
break;
default:
error = EINVAL;
break;
}
break;
default:
error = EINVAL;
break;
}
NG_RESPOND_MSG(error, node, item, resp);
NG_FREE_MSG(msg);
return(error);
}
static int
ng_etf_rcvdata(hook_p hook, item_p item )
{
const etf_p etfp = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
struct ether_header *eh;
int error = 0;
struct mbuf *m;
u_int16_t ethertype;
struct filter *fil;
if (NG_HOOK_PRIVATE(hook) == NULL) {
NG_FREE_ITEM(item);
}
NGI_GET_M(item, m);
if (m->m_len < sizeof(*eh) ) {
m = m_pullup(m, sizeof(*eh));
if (m == NULL) {
NG_FREE_ITEM(item);
return(EINVAL);
}
}
eh = mtod(m, struct ether_header *);
ethertype = eh->ether_type;
fil = ng_etf_findentry(etfp, ethertype);
if (hook == etfp->downstream_hook.hook) {
etfp->packets_in++;
if (fil && fil->match_hook) {
NG_FWD_NEW_DATA(error, item, fil->match_hook, m);
} else {
NG_FWD_NEW_DATA(error, item,etfp->nomatch_hook.hook, m);
}
} else {
if ((fil && (fil->match_hook != hook))
|| ((fil == NULL) && (hook != etfp->nomatch_hook.hook))) {
NG_FREE_ITEM(item);
NG_FREE_M(m);
return (EPROTOTYPE);
}
NG_FWD_NEW_DATA( error, item, etfp->downstream_hook.hook, m);
if (error == 0) {
etfp->packets_out++;
}
}
return (error);
}
static int
ng_etf_shutdown(node_p node)
{
const etf_p privdata = NG_NODE_PRIVATE(node);
NG_NODE_SET_PRIVATE(node, NULL);
NG_NODE_UNREF(privdata->node);
free(privdata, M_NETGRAPH_ETF);
return (0);
}
static int
ng_etf_disconnect(hook_p hook)
{
const etf_p etfp = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
int i;
struct filter *fil1, *fil2;
for (i = 0; i < HASHSIZE; i++) {
fil1 = LIST_FIRST(&etfp->hashtable[i]);
while (fil1 != NULL) {
fil2 = LIST_NEXT(fil1, next);
if (fil1->match_hook == hook) {
LIST_REMOVE(fil1, next);
free(fil1, M_NETGRAPH_ETF);
}
fil1 = fil2;
}
}
if (hook == etfp->downstream_hook.hook) {
etfp->downstream_hook.hook = NULL;
} else if (hook == etfp->nomatch_hook.hook) {
etfp->nomatch_hook.hook = NULL;
} else {
if (NG_HOOK_PRIVATE(hook))
free(NG_HOOK_PRIVATE(hook), M_NETGRAPH_ETF);
}
NG_HOOK_SET_PRIVATE(hook, NULL);
if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
&& (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
ng_rmnode_self(NG_HOOK_NODE(hook));
return (0);
}