#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 <sys/stddef.h>
#include <netgraph/ng_message.h>
#include <netgraph/netgraph.h>
#include <netgraph/ng_parse.h>
#include <netgraph/ng_tag.h>
#ifdef NG_SEPARATE_MALLOC
static MALLOC_DEFINE(M_NETGRAPH_TAG, "netgraph_tag", "netgraph tag node");
#else
#define M_NETGRAPH_TAG M_NETGRAPH
#endif
#define ERROUT(x) do { error = (x); goto done; } while (0)
struct ng_tag_hookinfo {
hook_p hi_match;
hook_p hi_nonmatch;
uint32_t in_tag_cookie;
uint32_t out_tag_cookie;
uint16_t in_tag_id;
uint16_t in_tag_len;
uint16_t out_tag_id;
uint16_t out_tag_len;
uint8_t strip;
void *in_tag_data;
void *out_tag_data;
struct ng_tag_hookin *in;
struct ng_tag_hookout *out;
#ifdef NG_TAG_DEBUG
struct ng_tag_hookstat stats;
#endif
};
typedef struct ng_tag_hookinfo *hinfo_p;
static ng_constructor_t ng_tag_constructor;
static ng_rcvmsg_t ng_tag_rcvmsg;
static ng_shutdown_t ng_tag_shutdown;
static ng_newhook_t ng_tag_newhook;
static ng_rcvdata_t ng_tag_rcvdata;
static ng_disconnect_t ng_tag_disconnect;
static int ng_tag_setdata_in(hook_p hook, const struct ng_tag_hookin *hp);
static int ng_tag_setdata_out(hook_p hook, const struct ng_tag_hookout *hp);
static int
ng_tag_hookinary_getLength(const struct ng_parse_type *type,
const u_char *start, const u_char *buf)
{
const struct ng_tag_hookin *hp;
hp = (const struct ng_tag_hookin *)
(buf - offsetof(struct ng_tag_hookin, tag_data));
return (hp->tag_len);
}
static int
ng_tag_hookoutary_getLength(const struct ng_parse_type *type,
const u_char *start, const u_char *buf)
{
const struct ng_tag_hookout *hp;
hp = (const struct ng_tag_hookout *)
(buf - offsetof(struct ng_tag_hookout, tag_data));
return (hp->tag_len);
}
static const struct ng_parse_type ng_tag_hookinary_type = {
&ng_parse_bytearray_type,
&ng_tag_hookinary_getLength
};
static const struct ng_parse_type ng_tag_hookoutary_type = {
&ng_parse_bytearray_type,
&ng_tag_hookoutary_getLength
};
static const struct ng_parse_struct_field ng_tag_hookin_type_fields[]
= NG_TAG_HOOKIN_TYPE_INFO(&ng_tag_hookinary_type);
static const struct ng_parse_type ng_tag_hookin_type = {
&ng_parse_struct_type,
&ng_tag_hookin_type_fields
};
static const struct ng_parse_struct_field ng_tag_hookout_type_fields[]
= NG_TAG_HOOKOUT_TYPE_INFO(&ng_tag_hookoutary_type);
static const struct ng_parse_type ng_tag_hookout_type = {
&ng_parse_struct_type,
&ng_tag_hookout_type_fields
};
#ifdef NG_TAG_DEBUG
static const struct ng_parse_struct_field ng_tag_hookstat_type_fields[]
= NG_TAG_HOOKSTAT_TYPE_INFO;
static const struct ng_parse_type ng_tag_hookstat_type = {
&ng_parse_struct_type,
&ng_tag_hookstat_type_fields
};
#endif
static const struct ng_cmdlist ng_tag_cmdlist[] = {
{
NGM_TAG_COOKIE,
NGM_TAG_SET_HOOKIN,
"sethookin",
&ng_tag_hookin_type,
NULL
},
{
NGM_TAG_COOKIE,
NGM_TAG_GET_HOOKIN,
"gethookin",
&ng_parse_hookbuf_type,
&ng_tag_hookin_type
},
{
NGM_TAG_COOKIE,
NGM_TAG_SET_HOOKOUT,
"sethookout",
&ng_tag_hookout_type,
NULL
},
{
NGM_TAG_COOKIE,
NGM_TAG_GET_HOOKOUT,
"gethookout",
&ng_parse_hookbuf_type,
&ng_tag_hookout_type
},
#ifdef NG_TAG_DEBUG
{
NGM_TAG_COOKIE,
NGM_TAG_GET_STATS,
"getstats",
&ng_parse_hookbuf_type,
&ng_tag_hookstat_type
},
{
NGM_TAG_COOKIE,
NGM_TAG_CLR_STATS,
"clrstats",
&ng_parse_hookbuf_type,
NULL
},
{
NGM_TAG_COOKIE,
NGM_TAG_GETCLR_STATS,
"getclrstats",
&ng_parse_hookbuf_type,
&ng_tag_hookstat_type
},
#endif
{ 0 }
};
static struct ng_type typestruct = {
.version = NG_ABI_VERSION,
.name = NG_TAG_NODE_TYPE,
.constructor = ng_tag_constructor,
.rcvmsg = ng_tag_rcvmsg,
.shutdown = ng_tag_shutdown,
.newhook = ng_tag_newhook,
.rcvdata = ng_tag_rcvdata,
.disconnect = ng_tag_disconnect,
.cmdlist = ng_tag_cmdlist,
};
NETGRAPH_INIT(tag, &typestruct);
static const struct ng_tag_hookin ng_tag_default_in = {
{ '\0' },
{ '\0' },
{ '\0' },
0,
0,
0,
0
};
static const struct ng_tag_hookout ng_tag_default_out = {
{ '\0' },
0,
0,
0
};
static int
ng_tag_constructor(node_p node)
{
return (0);
}
static int
ng_tag_newhook(node_p node, hook_p hook, const char *name)
{
hinfo_p hip;
int error;
hip = malloc(sizeof(*hip), M_NETGRAPH_TAG, M_NOWAIT | M_ZERO);
if (hip == NULL)
return (ENOMEM);
NG_HOOK_SET_PRIVATE(hook, hip);
if ((error = ng_tag_setdata_in(hook, &ng_tag_default_in)) != 0) {
free(hip, M_NETGRAPH_TAG);
return (error);
}
if ((error = ng_tag_setdata_out(hook, &ng_tag_default_out)) != 0) {
free(hip, M_NETGRAPH_TAG);
return (error);
}
strncpy(hip->in->thisHook, name, sizeof(hip->in->thisHook) - 1);
hip->in->thisHook[sizeof(hip->in->thisHook) - 1] = '\0';
strncpy(hip->out->thisHook, name, sizeof(hip->out->thisHook) - 1);
hip->out->thisHook[sizeof(hip->out->thisHook) - 1] = '\0';
return (0);
}
static int
ng_tag_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_TAG_COOKIE:
switch (msg->header.cmd) {
case NGM_TAG_SET_HOOKIN:
{
struct ng_tag_hookin *const
hp = (struct ng_tag_hookin *)msg->data;
hook_p hook;
if (msg->header.arglen < sizeof(*hp) ||
msg->header.arglen < NG_TAG_HOOKIN_SIZE(hp->tag_len))
ERROUT(EINVAL);
if ((hook = ng_findhook(node, hp->thisHook)) == NULL)
ERROUT(ENOENT);
if ((error = ng_tag_setdata_in(hook, hp)) != 0)
ERROUT(error);
break;
}
case NGM_TAG_SET_HOOKOUT:
{
struct ng_tag_hookout *const
hp = (struct ng_tag_hookout *)msg->data;
hook_p hook;
if (msg->header.arglen < sizeof(*hp) ||
msg->header.arglen < NG_TAG_HOOKOUT_SIZE(hp->tag_len))
ERROUT(EINVAL);
if ((hook = ng_findhook(node, hp->thisHook)) == NULL)
ERROUT(ENOENT);
if ((error = ng_tag_setdata_out(hook, hp)) != 0)
ERROUT(error);
break;
}
case NGM_TAG_GET_HOOKIN:
{
struct ng_tag_hookin *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))->in;
NG_MKRESPONSE(resp, msg,
NG_TAG_HOOKIN_SIZE(hp->tag_len), M_WAITOK);
bcopy(hp, resp->data,
NG_TAG_HOOKIN_SIZE(hp->tag_len));
break;
}
case NGM_TAG_GET_HOOKOUT:
{
struct ng_tag_hookout *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))->out;
NG_MKRESPONSE(resp, msg,
NG_TAG_HOOKOUT_SIZE(hp->tag_len), M_WAITOK);
bcopy(hp, resp->data,
NG_TAG_HOOKOUT_SIZE(hp->tag_len));
break;
}
#ifdef NG_TAG_DEBUG
case NGM_TAG_GET_STATS:
case NGM_TAG_CLR_STATS:
case NGM_TAG_GETCLR_STATS:
{
struct ng_tag_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_TAG_CLR_STATS) {
NG_MKRESPONSE(resp,
msg, sizeof(*stats), M_WAITOK);
bcopy(stats, resp->data, sizeof(*stats));
}
if (msg->header.cmd != NGM_TAG_GET_STATS)
bzero(stats, sizeof(*stats));
break;
}
#endif
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_tag_rcvdata(hook_p hook, item_p item)
{
struct mbuf *m;
struct m_tag *tag = NULL;
const hinfo_p hip = NG_HOOK_PRIVATE(hook);
uint16_t type, tag_len;
uint32_t cookie;
hinfo_p dhip;
hook_p dest;
#ifdef NG_TAG_DEBUG
int totlen;
#endif
int found = 0, error = 0;
m = NGI_M(item);
#ifdef NG_TAG_DEBUG
totlen = m->m_pkthdr.len;
hip->stats.recvFrames++;
hip->stats.recvOctets += totlen;
#endif
cookie = hip->in_tag_cookie;
type = hip->in_tag_id;
tag_len = hip->in_tag_len;
if ((cookie != 0) || (type != 0)) {
tag = m_tag_locate(m, cookie, type, NULL);
while (tag != NULL) {
if (memcmp((void *)(tag + 1),
hip->in_tag_data, tag_len) == 0) {
found = 1;
break;
}
tag = m_tag_locate(m, cookie, type, tag);
}
}
if (found) {
#ifdef NG_TAG_DEBUG
hip->stats.recvMatchFrames++;
hip->stats.recvMatchOctets += totlen;
#endif
if (hip->strip)
m_tag_delete(m, tag);
dest = hip->hi_match;
} else
dest = hip->hi_nonmatch;
if (dest == NULL) {
NG_FREE_ITEM(item);
return (0);
}
dhip = NG_HOOK_PRIVATE(dest);
#ifdef NG_TAG_DEBUG
dhip->stats.xmitOctets += totlen;
dhip->stats.xmitFrames++;
#endif
cookie = dhip->out_tag_cookie;
type = dhip->out_tag_id;
tag_len = dhip->out_tag_len;
if ((cookie != 0) || (type != 0)) {
tag = m_tag_alloc(cookie, type, tag_len, M_NOWAIT);
if (tag != NULL) {
if (tag_len != 0) {
memcpy((void *)(tag + 1),
dhip->out_tag_data, tag_len);
}
m_tag_prepend(m, tag);
}
}
NG_FWD_ITEM_HOOK(error, item, dest);
return (error);
}
static int
ng_tag_shutdown(node_p node)
{
NG_NODE_UNREF(node);
return (0);
}
static int
ng_tag_disconnect(hook_p hook)
{
const hinfo_p hip = NG_HOOK_PRIVATE(hook);
node_p node = NG_HOOK_NODE(hook);
hook_p hook2;
KASSERT(hip != NULL, ("%s: null info", __func__));
LIST_FOREACH(hook2, &node->nd_hooks, hk_hooks) {
hinfo_p priv = NG_HOOK_PRIVATE(hook2);
if (priv->hi_match == hook)
priv->hi_match = NULL;
if (priv->hi_nonmatch == hook)
priv->hi_nonmatch = NULL;
}
free(hip->in, M_NETGRAPH_TAG);
free(hip->out, M_NETGRAPH_TAG);
free(hip, M_NETGRAPH_TAG);
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);
}
static int
ng_tag_setdata_in(hook_p hook, const struct ng_tag_hookin *hp0)
{
const hinfo_p hip = NG_HOOK_PRIVATE(hook);
struct ng_tag_hookin *hp;
int size;
size = NG_TAG_HOOKIN_SIZE(hp0->tag_len);
hp = malloc(size, M_NETGRAPH_TAG, M_WAITOK);
bcopy(hp0, hp, size);
if (hip->in != NULL)
free(hip->in, M_NETGRAPH_TAG);
hip->in = hp;
hip->hi_match = ng_findhook(NG_HOOK_NODE(hook), hip->in->ifMatch);
hip->hi_nonmatch = ng_findhook(NG_HOOK_NODE(hook), hip->in->ifNotMatch);
hip->in_tag_cookie = hip->in->tag_cookie;
hip->in_tag_id = hip->in->tag_id;
hip->in_tag_len = hip->in->tag_len;
hip->strip = hip->in->strip;
hip->in_tag_data = (void*)(hip->in->tag_data);
return (0);
}
static int
ng_tag_setdata_out(hook_p hook, const struct ng_tag_hookout *hp0)
{
const hinfo_p hip = NG_HOOK_PRIVATE(hook);
struct ng_tag_hookout *hp;
int size;
size = NG_TAG_HOOKOUT_SIZE(hp0->tag_len);
hp = malloc(size, M_NETGRAPH_TAG, M_WAITOK);
bcopy(hp0, hp, size);
if (hip->out != NULL)
free(hip->out, M_NETGRAPH_TAG);
hip->out = hp;
hip->out_tag_cookie = hip->out->tag_cookie;
hip->out_tag_id = hip->out->tag_id;
hip->out_tag_len = hip->out->tag_len;
hip->out_tag_data = (void*)(hip->out->tag_data);
return (0);
}