#include <sys/param.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/syslog.h>
#include <sys/linker.h>
#include <sys/queue.h>
#include <sys/mbuf.h>
#include <sys/ctype.h>
#include <sys/sysctl.h>
#include <sys/vnode.h>
#include <machine/limits.h>
#include <sys/thread2.h>
#include <sys/msgport2.h>
#include <sys/mplock2.h>
#include <net/netisr.h>
#include <netgraph/ng_message.h>
#include <netgraph/netgraph.h>
#include <netgraph/ng_parse.h>
MODULE_VERSION(netgraph, NG_ABI_VERSION);
union netmsg;
static LIST_HEAD(, ng_node) nodelist;
static LIST_HEAD(, ng_type) typelist;
#define ID_HASH_SIZE 32
static LIST_HEAD(, ng_node) ID_hash[ID_HASH_SIZE];
static int ng_add_hook(node_p node, const char *name, hook_p * hookp);
static int ng_connect(hook_p hook1, hook_p hook2);
static void ng_disconnect_hook(hook_p hook);
static int ng_generic_msg(node_p here, struct ng_mesg *msg,
const char *retaddr, struct ng_mesg ** resp);
static ng_ID_t ng_decodeidname(const char *name);
static int ngb_mod_event(module_t mod, int event, void *data);
static void ngintr(union netmsg *);
static int ng_load_module(const char *);
MALLOC_DEFINE(M_NETGRAPH, "netgraph", "netgraph structures and ctrl messages");
#ifndef TRAP_ERROR
#define TRAP_ERROR
#endif
static ng_ID_t nextID = 1;
#ifdef INVARIANTS
#define CHECK_DATA_MBUF(m) do { \
struct mbuf *n; \
int total; \
\
if (((m)->m_flags & M_PKTHDR) == 0) \
panic("%s: !PKTHDR", __func__); \
for (total = 0, n = (m); n != NULL; n = n->m_next) \
total += n->m_len; \
if ((m)->m_pkthdr.len != total) { \
panic("%s: %d != %d", \
__func__, (m)->m_pkthdr.len, total); \
} \
} while (0)
#else
#define CHECK_DATA_MBUF(m)
#endif
#define DEFINE_PARSE_STRUCT_TYPE(lo, up, args) \
static const struct ng_parse_struct_field \
ng_ ## lo ## _type_fields[] = NG_GENERIC_ ## up ## _INFO args; \
static const struct ng_parse_type ng_generic_ ## lo ## _type = { \
&ng_parse_struct_type, \
&ng_ ## lo ## _type_fields \
}
DEFINE_PARSE_STRUCT_TYPE(mkpeer, MKPEER, ());
DEFINE_PARSE_STRUCT_TYPE(connect, CONNECT, ());
DEFINE_PARSE_STRUCT_TYPE(name, NAME, ());
DEFINE_PARSE_STRUCT_TYPE(rmhook, RMHOOK, ());
DEFINE_PARSE_STRUCT_TYPE(nodeinfo, NODEINFO, ());
DEFINE_PARSE_STRUCT_TYPE(typeinfo, TYPEINFO, ());
DEFINE_PARSE_STRUCT_TYPE(linkinfo, LINKINFO, (&ng_generic_nodeinfo_type));
static int
ng_generic_list_getLength(const struct ng_parse_type *type,
const u_char *start, const u_char *buf)
{
return *((const u_int32_t *)(buf - 4));
}
static int
ng_generic_linkinfo_getLength(const struct ng_parse_type *type,
const u_char *start, const u_char *buf)
{
const struct hooklist *hl = (const struct hooklist *)start;
return hl->nodeinfo.hooks;
}
static const struct ng_parse_array_info ng_nodeinfoarray_type_info = {
&ng_generic_nodeinfo_type,
&ng_generic_list_getLength
};
static const struct ng_parse_type ng_generic_nodeinfoarray_type = {
&ng_parse_array_type,
&ng_nodeinfoarray_type_info
};
#if 0
static const struct ng_parse_array_info ng_typeinfoarray_type_info = {
&ng_generic_typeinfo_type,
&ng_generic_list_getLength
};
static const struct ng_parse_type ng_generic_typeinfoarray_type = {
&ng_parse_array_type,
&ng_typeinfoarray_type_info
};
#endif
static const struct ng_parse_array_info ng_generic_linkinfo_array_type_info = {
&ng_generic_linkinfo_type,
&ng_generic_linkinfo_getLength
};
static const struct ng_parse_type ng_generic_linkinfo_array_type = {
&ng_parse_array_type,
&ng_generic_linkinfo_array_type_info
};
#if 0
DEFINE_PARSE_STRUCT_TYPE(typelist, TYPELIST, (&ng_generic_nodeinfoarray_type));
#endif
DEFINE_PARSE_STRUCT_TYPE(hooklist, HOOKLIST,
(&ng_generic_nodeinfo_type, &ng_generic_linkinfo_array_type));
DEFINE_PARSE_STRUCT_TYPE(listnodes, LISTNODES,
(&ng_generic_nodeinfoarray_type));
static const struct ng_cmdlist ng_generic_cmds[] = {
{
NGM_GENERIC_COOKIE,
NGM_SHUTDOWN,
"shutdown",
NULL,
NULL
},
{
NGM_GENERIC_COOKIE,
NGM_MKPEER,
"mkpeer",
&ng_generic_mkpeer_type,
NULL
},
{
NGM_GENERIC_COOKIE,
NGM_CONNECT,
"connect",
&ng_generic_connect_type,
NULL
},
{
NGM_GENERIC_COOKIE,
NGM_NAME,
"name",
&ng_generic_name_type,
NULL
},
{
NGM_GENERIC_COOKIE,
NGM_RMHOOK,
"rmhook",
&ng_generic_rmhook_type,
NULL
},
{
NGM_GENERIC_COOKIE,
NGM_NODEINFO,
"nodeinfo",
NULL,
&ng_generic_nodeinfo_type
},
{
NGM_GENERIC_COOKIE,
NGM_LISTHOOKS,
"listhooks",
NULL,
&ng_generic_hooklist_type
},
{
NGM_GENERIC_COOKIE,
NGM_LISTNAMES,
"listnames",
NULL,
&ng_generic_listnodes_type
},
{
NGM_GENERIC_COOKIE,
NGM_LISTNODES,
"listnodes",
NULL,
&ng_generic_listnodes_type
},
{
NGM_GENERIC_COOKIE,
NGM_LISTTYPES,
"listtypes",
NULL,
&ng_generic_typeinfo_type
},
{
NGM_GENERIC_COOKIE,
NGM_TEXT_CONFIG,
"textconfig",
NULL,
&ng_parse_string_type
},
{
NGM_GENERIC_COOKIE,
NGM_TEXT_STATUS,
"textstatus",
NULL,
&ng_parse_string_type
},
{
NGM_GENERIC_COOKIE,
NGM_ASCII2BINARY,
"ascii2binary",
&ng_parse_ng_mesg_type,
&ng_parse_ng_mesg_type
},
{
NGM_GENERIC_COOKIE,
NGM_BINARY2ASCII,
"binary2ascii",
&ng_parse_ng_mesg_type,
&ng_parse_ng_mesg_type
},
{ 0 }
};
static int
linker_api_available(void)
{
if (curproc == NULL)
return 0;
if (namei_oc == NULL)
return 0;
return 1;
}
static int
ng_load_module(const char *name)
{
char *path, filename[NG_TYPESIZ + 3];
linker_file_t lf;
int error;
if (!linker_api_available())
return (ENXIO);
ksnprintf(filename, sizeof(filename), "ng_%s.ko", name);
if ((path = linker_search_path(filename)) == NULL)
return (ENXIO);
error = linker_load_file(path, &lf);
kfree(path, M_LINKER);
if (error == 0)
lf->userrefs++;
return (error);
}
int
ng_make_node(const char *typename, node_p *nodepp)
{
struct ng_type *type;
if (typename == NULL) {
TRAP_ERROR;
return (EINVAL);
}
if ((type = ng_findtype(typename)) == NULL)
return (ENXIO);
if (type->constructor != NULL)
return ((*type->constructor)(nodepp));
else
return (ng_make_node_common(type, nodepp));
}
int
ng_make_node_common(struct ng_type *type, node_p *nodepp)
{
node_p node;
if (ng_findtype(type->name) == NULL) {
TRAP_ERROR;
return (EINVAL);
}
node = kmalloc(sizeof(*node), M_NETGRAPH, M_NOWAIT | M_ZERO);
if (node == NULL) {
TRAP_ERROR;
return (ENOMEM);
}
node->type = type;
node->refs++;
type->refs++;
LIST_INSERT_HEAD(&nodelist, node, nodes);
LIST_INIT(&node->hooks);
node->ID = nextID++;
LIST_INSERT_HEAD(&ID_hash[node->ID % ID_HASH_SIZE], node, idnodes);
*nodepp = node;
return (0);
}
void
ng_rmnode(node_p node)
{
if ((node->flags & NG_INVALID) != 0)
return;
node->refs++;
node->flags |= NG_INVALID;
if (node->type && node->type->shutdown)
(*node->type->shutdown)(node);
else {
ng_unname(node);
ng_cutlinks(node);
ng_unref(node);
}
ng_unref(node);
}
void
ng_cutlinks(node_p node)
{
hook_p hook;
node->flags |= NG_INVALID;
if (node->sleepers)
wakeup(node);
while ((hook = LIST_FIRST(&node->hooks)) != NULL)
ng_destroy_hook(hook);
}
void
ng_unref(node_p node)
{
crit_enter();
if (--node->refs <= 0) {
node->type->refs--;
LIST_REMOVE(node, nodes);
LIST_REMOVE(node, idnodes);
kfree(node, M_NETGRAPH);
}
crit_exit();
}
int
ng_wait_node(node_p node, char *msg)
{
int error = 0;
if (msg == NULL)
msg = "netgraph";
crit_enter();
node->sleepers++;
node->refs++;
while ((node->flags & (NG_BUSY | NG_INVALID)) == NG_BUSY)
error = tsleep(node, PCATCH, msg, 0);
node->sleepers--;
if (node->flags & NG_INVALID) {
TRAP_ERROR;
error = ENXIO;
} else {
KASSERT(node->refs > 1,
("%s: refs=%d", __func__, node->refs));
node->flags |= NG_BUSY;
}
crit_exit();
if (error != 0)
ng_unref(node);
return error;
}
void
ng_release_node(node_p node)
{
node->flags &= ~NG_BUSY;
if (node->sleepers)
wakeup(node);
ng_unref(node);
}
static node_p
ng_ID2node(ng_ID_t ID)
{
node_p np;
LIST_FOREACH(np, &ID_hash[ID % ID_HASH_SIZE], idnodes) {
if ((np->flags & NG_INVALID) == 0 && np->ID == ID)
break;
}
return(np);
}
ng_ID_t
ng_node2ID(node_p node)
{
return (node->ID);
}
int
ng_name_node(node_p node, const char *name)
{
int i;
for (i = 0; i < NG_NODESIZ; i++) {
if (name[i] == '\0' || name[i] == '.' || name[i] == ':')
break;
}
if (i == 0 || name[i] != '\0') {
TRAP_ERROR;
return (EINVAL);
}
if (ng_decodeidname(name) != 0) {
TRAP_ERROR;
return (EINVAL);
}
if (node->name != NULL) {
TRAP_ERROR;
return (EISCONN);
}
if (ng_findname(node, name) != NULL) {
TRAP_ERROR;
return (EADDRINUSE);
}
node->name = kmalloc(strlen(name) + 1, M_NETGRAPH, M_NOWAIT);
if (node->name == NULL) {
TRAP_ERROR;
return (ENOMEM);
}
strcpy(node->name, name);
node->refs++;
return (0);
}
node_p
ng_findname(node_p this, const char *name)
{
node_p node;
ng_ID_t temp;
if (strcmp(name, ".") == 0)
return(this);
if ((temp = ng_decodeidname(name)) != 0) {
return (ng_ID2node(temp));
}
LIST_FOREACH(node, &nodelist, nodes) {
if ((node->name != NULL)
&& (strcmp(node->name, name) == 0)
&& ((node->flags & NG_INVALID) == 0))
break;
}
return (node);
}
static ng_ID_t
ng_decodeidname(const char *name)
{
const int len = strlen(name);
char *eptr;
u_long val;
if (len < 3 || name[0] != '[' || name[len - 1] != ']'
|| !isxdigit(name[1]))
return (0);
val = strtoul(name + 1, &eptr, 16);
if (eptr - name != len - 1 || val == ULONG_MAX || val == 0)
return ((ng_ID_t)0);
return (ng_ID_t)val;
}
void
ng_unname(node_p node)
{
if (node->name) {
kfree(node->name, M_NETGRAPH);
node->name = NULL;
ng_unref(node);
}
}
void
ng_unref_hook(hook_p hook)
{
crit_enter();
if (--hook->refs == 0)
kfree(hook, M_NETGRAPH);
crit_exit();
}
static int
ng_add_hook(node_p node, const char *name, hook_p *hookp)
{
hook_p hook;
int error = 0;
if (name == NULL) {
TRAP_ERROR;
return (EINVAL);
}
if (ng_findhook(node, name) != NULL) {
TRAP_ERROR;
return (EEXIST);
}
hook = kmalloc(sizeof(*hook), M_NETGRAPH, M_NOWAIT | M_ZERO);
if (hook == NULL) {
TRAP_ERROR;
return (ENOMEM);
}
hook->refs = 1;
hook->flags = HK_INVALID;
hook->node = node;
node->refs++;
if (node->type->newhook != NULL)
if ((error = (*node->type->newhook)(node, hook, name)) != 0)
goto fail;
LIST_INSERT_HEAD(&node->hooks, hook, hooks);
node->numhooks++;
hook->name = kmalloc(strlen(name) + 1, M_NETGRAPH, M_NOWAIT);
if (hook->name == NULL) {
error = ENOMEM;
LIST_REMOVE(hook, hooks);
node->numhooks--;
fail:
hook->node = NULL;
ng_unref(node);
ng_unref_hook(hook);
return (error);
}
strcpy(hook->name, name);
if (hookp)
*hookp = hook;
return (error);
}
static int
ng_connect(hook_p hook1, hook_p hook2)
{
int error;
hook1->peer = hook2;
hook2->peer = hook1;
if (hook1->node->type->connect) {
if ((error = (*hook1->node->type->connect) (hook1))) {
ng_destroy_hook(hook1);
return (error);
}
}
if (hook2->node->type->connect) {
if ((error = (*hook2->node->type->connect) (hook2))) {
ng_destroy_hook(hook2);
return (error);
}
}
hook1->flags &= ~HK_INVALID;
hook2->flags &= ~HK_INVALID;
return (0);
}
hook_p
ng_findhook(node_p node, const char *name)
{
hook_p hook;
if (node->type->findhook != NULL)
return (*node->type->findhook)(node, name);
LIST_FOREACH(hook, &node->hooks, hooks) {
if (hook->name != NULL
&& strcmp(hook->name, name) == 0
&& (hook->flags & HK_INVALID) == 0)
return (hook);
}
return (NULL);
}
void
ng_destroy_hook(hook_p hook)
{
hook_p peer = hook->peer;
hook->flags |= HK_INVALID;
if (peer) {
peer->flags |= HK_INVALID;
hook->peer = NULL;
peer->peer = NULL;
ng_disconnect_hook(peer);
}
ng_disconnect_hook(hook);
}
static void
ng_disconnect_hook(hook_p hook)
{
node_p node = hook->node;
LIST_REMOVE(hook, hooks);
node->numhooks--;
if (node->type->disconnect) {
(*node->type->disconnect) (hook);
}
ng_unref(node);
if (hook->name)
kfree(hook->name, M_NETGRAPH);
hook->node = NULL;
ng_unref_hook(hook);
}
int
ng_bypass(hook_p hook1, hook_p hook2)
{
if (hook1->node != hook2->node)
return (EINVAL);
hook1->peer->peer = hook2->peer;
hook2->peer->peer = hook1->peer;
hook1->peer = NULL;
hook2->peer = NULL;
ng_destroy_hook(hook1);
ng_destroy_hook(hook2);
return (0);
}
int
ng_newtype(struct ng_type *tp)
{
const size_t namelen = strlen(tp->name);
if (tp->version != NG_VERSION || namelen == 0 || namelen >= NG_TYPESIZ) {
TRAP_ERROR;
return (EINVAL);
}
if (ng_findtype(tp->name) != NULL) {
TRAP_ERROR;
return (EEXIST);
}
LIST_INSERT_HEAD(&typelist, tp, types);
tp->refs = 1;
return (0);
}
struct ng_type *
ng_findtype(const char *typename)
{
struct ng_type *type;
LIST_FOREACH(type, &typelist, types) {
if (strcmp(type->name, typename) == 0)
break;
}
return (type);
}
int
ng_mkpeer(node_p node, const char *name, const char *name2, char *type)
{
node_p node2;
hook_p hook;
hook_p hook2;
int error;
if ((error = ng_add_hook(node, name, &hook)))
return (error);
if (ng_findtype(type) == NULL) {
error = ng_load_module(type);
if (error != 0) {
kprintf("required netgraph module ng_%s not loaded\n",
type);
return (error);
}
}
if ((error = ng_make_node(type, &node2))) {
ng_destroy_hook(hook);
return (error);
}
if ((error = ng_add_hook(node2, name2, &hook2))) {
ng_rmnode(node2);
ng_destroy_hook(hook);
return (error);
}
if ((error = ng_connect(hook, hook2)))
ng_rmnode(node2);
return (error);
}
int
ng_con_nodes(node_p node, const char *name, node_p node2, const char *name2)
{
int error;
hook_p hook;
hook_p hook2;
if ((error = ng_add_hook(node, name, &hook)))
return (error);
if ((error = ng_add_hook(node2, name2, &hook2))) {
ng_destroy_hook(hook);
return (error);
}
return (ng_connect(hook, hook2));
}
int
ng_path_parse(char *addr, char **nodep, char **pathp, char **hookp)
{
char *node, *path, *hook;
int k;
for (path = addr; *path && *path != ':'; path++);
if (*path) {
node = addr;
*path++ = '\0';
if (!*node)
return -1;
if (strcmp(node, ".") != 0) {
for (k = 0; node[k]; k++)
if (node[k] == '.')
return -1;
}
} else {
node = NULL;
path = addr;
}
for (k = 0; path[k]; k++)
if (path[k] == ':')
return -1;
for (k = 0; path[k]; k++)
if (path[k] == '.' && path[k + 1] == '.')
return -1;
if (path[0] == '.')
path++;
if (*path && path[strlen(path) - 1] == '.')
path[strlen(path) - 1] = 0;
if (*path) {
for (hook = path, k = 0; path[k]; k++)
if (path[k] == '.') {
hook = NULL;
break;
}
} else
path = hook = NULL;
if (nodep)
*nodep = node;
if (pathp)
*pathp = path;
if (hookp)
*hookp = hook;
return (0);
}
int
ng_path2node(node_p here, const char *address, node_p *destp, char **rtnp)
{
const node_p start = here;
char fullpath[NG_PATHSIZ];
char *nodename, *path, pbuf[2];
node_p node;
char *cp;
if (rtnp)
*rtnp = NULL;
if (destp == NULL)
return EINVAL;
*destp = NULL;
strncpy(fullpath, address, sizeof(fullpath) - 1);
fullpath[sizeof(fullpath) - 1] = '\0';
if (ng_path_parse(fullpath, &nodename, &path, NULL) < 0) {
TRAP_ERROR;
return EINVAL;
}
if (path == NULL) {
pbuf[0] = '.';
pbuf[1] = '\0';
path = pbuf;
}
if (nodename) {
node = ng_findname(here, nodename);
if (node == NULL) {
TRAP_ERROR;
return (ENOENT);
}
} else
node = here;
for (cp = path; node != NULL && *cp != '\0'; ) {
hook_p hook;
char *segment;
for (segment = cp; *cp != '\0'; cp++) {
if (*cp == '.') {
*cp++ = '\0';
break;
}
}
if (*segment == '\0')
continue;
hook = ng_findhook(node, segment);
if (hook == NULL
|| hook->peer == NULL
|| (hook->flags & HK_INVALID) != 0) {
TRAP_ERROR;
return (ENOENT);
}
node = hook->peer->node;
}
if (node == NULL) {
TRAP_ERROR;
return (ENXIO);
}
if (rtnp != NULL) {
*rtnp = kmalloc(NG_NODESIZ + 1, M_NETGRAPH, M_NOWAIT);
if (*rtnp == NULL) {
TRAP_ERROR;
return (ENOMEM);
}
if (start->name != NULL)
ksprintf(*rtnp, "%s:", start->name);
else
ksprintf(*rtnp, "[%x]:", ng_node2ID(start));
}
*destp = node;
return (0);
}
#define CALL_MSG_HANDLER(error, node, msg, retaddr, resp) \
do { \
if((msg)->header.typecookie == NGM_GENERIC_COOKIE) { \
(error) = ng_generic_msg((node), (msg), \
(retaddr), (resp)); \
} else { \
if ((node)->type->rcvmsg != NULL) { \
(error) = (*(node)->type->rcvmsg)((node), \
(msg), (retaddr), (resp)); \
} else { \
TRAP_ERROR; \
kfree((msg), M_NETGRAPH); \
(error) = EINVAL; \
} \
} \
} while (0)
int
ng_send_msg(node_p here, struct ng_mesg *msg, const char *address,
struct ng_mesg **rptr)
{
node_p dest = NULL;
char *retaddr = NULL;
int error;
error = ng_path2node(here, address, &dest, &retaddr);
if (error) {
kfree(msg, M_NETGRAPH);
return error;
}
if (rptr != NULL)
*rptr = NULL;
CALL_MSG_HANDLER(error, dest, msg, retaddr, rptr);
if ((error == 0) && rptr && *rptr)
(*rptr)->header.flags |= NGF_RESP;
if (retaddr)
kfree(retaddr, M_NETGRAPH);
return (error);
}
static int
ng_generic_msg(node_p here, struct ng_mesg *msg, const char *retaddr,
struct ng_mesg **resp)
{
int error = 0;
if (msg->header.typecookie != NGM_GENERIC_COOKIE) {
TRAP_ERROR;
kfree(msg, M_NETGRAPH);
return (EINVAL);
}
switch (msg->header.cmd) {
case NGM_SHUTDOWN:
ng_rmnode(here);
break;
case NGM_MKPEER:
{
struct ngm_mkpeer *const mkp = (struct ngm_mkpeer *) msg->data;
if (msg->header.arglen != sizeof(*mkp)) {
TRAP_ERROR;
return (EINVAL);
}
mkp->type[sizeof(mkp->type) - 1] = '\0';
mkp->ourhook[sizeof(mkp->ourhook) - 1] = '\0';
mkp->peerhook[sizeof(mkp->peerhook) - 1] = '\0';
error = ng_mkpeer(here, mkp->ourhook, mkp->peerhook, mkp->type);
break;
}
case NGM_CONNECT:
{
struct ngm_connect *const con =
(struct ngm_connect *) msg->data;
node_p node2;
if (msg->header.arglen != sizeof(*con)) {
TRAP_ERROR;
return (EINVAL);
}
con->path[sizeof(con->path) - 1] = '\0';
con->ourhook[sizeof(con->ourhook) - 1] = '\0';
con->peerhook[sizeof(con->peerhook) - 1] = '\0';
error = ng_path2node(here, con->path, &node2, NULL);
if (error)
break;
error = ng_con_nodes(here, con->ourhook, node2, con->peerhook);
break;
}
case NGM_NAME:
{
struct ngm_name *const nam = (struct ngm_name *) msg->data;
if (msg->header.arglen != sizeof(*nam)) {
TRAP_ERROR;
return (EINVAL);
}
nam->name[sizeof(nam->name) - 1] = '\0';
error = ng_name_node(here, nam->name);
break;
}
case NGM_RMHOOK:
{
struct ngm_rmhook *const rmh = (struct ngm_rmhook *) msg->data;
hook_p hook;
if (msg->header.arglen != sizeof(*rmh)) {
TRAP_ERROR;
return (EINVAL);
}
rmh->ourhook[sizeof(rmh->ourhook) - 1] = '\0';
if ((hook = ng_findhook(here, rmh->ourhook)) != NULL)
ng_destroy_hook(hook);
break;
}
case NGM_NODEINFO:
{
struct nodeinfo *ni;
struct ng_mesg *rp;
if (resp == NULL) {
error = EINVAL;
break;
}
NG_MKRESPONSE(rp, msg, sizeof(*ni), M_NOWAIT);
if (rp == NULL) {
error = ENOMEM;
break;
}
ni = (struct nodeinfo *) rp->data;
if (here->name != NULL)
strlcpy(ni->name, here->name, NG_NODESIZ);
strlcpy(ni->type, here->type->name, NG_TYPESIZ);
ni->id = ng_node2ID(here);
ni->hooks = here->numhooks;
*resp = rp;
break;
}
case NGM_LISTHOOKS:
{
const int nhooks = here->numhooks;
struct hooklist *hl;
struct nodeinfo *ni;
struct ng_mesg *rp;
hook_p hook;
if (resp == NULL) {
error = EINVAL;
break;
}
NG_MKRESPONSE(rp, msg, sizeof(*hl)
+ (nhooks * sizeof(struct linkinfo)), M_NOWAIT);
if (rp == NULL) {
error = ENOMEM;
break;
}
hl = (struct hooklist *) rp->data;
ni = &hl->nodeinfo;
if (here->name)
strlcpy(ni->name, here->name, NG_NODESIZ);
strlcpy(ni->type, here->type->name, NG_TYPESIZ);
ni->id = ng_node2ID(here);
ni->hooks = 0;
LIST_FOREACH(hook, &here->hooks, hooks) {
struct linkinfo *const link = &hl->link[ni->hooks];
if (ni->hooks >= nhooks) {
log(LOG_ERR, "%s: number of %s changed\n",
__func__, "hooks");
break;
}
if ((hook->flags & HK_INVALID) != 0)
continue;
strlcpy(link->ourhook, hook->name, NG_HOOKSIZ);
strlcpy(link->peerhook, hook->peer->name, NG_HOOKSIZ);
if (hook->peer->node->name != NULL)
strlcpy(link->nodeinfo.name,
hook->peer->node->name, NG_NODESIZ);
strlcpy(link->nodeinfo.type,
hook->peer->node->type->name, NG_TYPESIZ);
link->nodeinfo.id = ng_node2ID(hook->peer->node);
link->nodeinfo.hooks = hook->peer->node->numhooks;
ni->hooks++;
}
*resp = rp;
break;
}
case NGM_LISTNAMES:
case NGM_LISTNODES:
{
const int unnamed = (msg->header.cmd == NGM_LISTNODES);
struct namelist *nl;
struct ng_mesg *rp;
node_p node;
int num = 0;
if (resp == NULL) {
error = EINVAL;
break;
}
LIST_FOREACH(node, &nodelist, nodes) {
if ((node->flags & NG_INVALID) == 0
&& (unnamed || node->name != NULL))
num++;
}
if (resp == NULL) {
error = EINVAL;
break;
}
NG_MKRESPONSE(rp, msg, sizeof(*nl)
+ (num * sizeof(struct nodeinfo)), M_NOWAIT);
if (rp == NULL) {
error = ENOMEM;
break;
}
nl = (struct namelist *) rp->data;
nl->numnames = 0;
LIST_FOREACH(node, &nodelist, nodes) {
struct nodeinfo *const np = &nl->nodeinfo[nl->numnames];
if (nl->numnames >= num) {
log(LOG_ERR, "%s: number of %s changed\n",
__func__, "nodes");
break;
}
if ((node->flags & NG_INVALID) != 0)
continue;
if (!unnamed && node->name == NULL)
continue;
if (node->name != NULL)
strlcpy(np->name, node->name, NG_NODESIZ);
strlcpy(np->type, node->type->name, NG_TYPESIZ);
np->id = ng_node2ID(node);
np->hooks = node->numhooks;
nl->numnames++;
}
*resp = rp;
break;
}
case NGM_LISTTYPES:
{
struct typelist *tl;
struct ng_mesg *rp;
struct ng_type *type;
int num = 0;
if (resp == NULL) {
error = EINVAL;
break;
}
LIST_FOREACH(type, &typelist, types)
num++;
if (resp == NULL) {
error = EINVAL;
break;
}
NG_MKRESPONSE(rp, msg, sizeof(*tl)
+ (num * sizeof(struct typeinfo)), M_NOWAIT);
if (rp == NULL) {
error = ENOMEM;
break;
}
tl = (struct typelist *) rp->data;
tl->numtypes = 0;
LIST_FOREACH(type, &typelist, types) {
struct typeinfo *const tp = &tl->typeinfo[tl->numtypes];
if (tl->numtypes >= num) {
log(LOG_ERR, "%s: number of %s changed\n",
__func__, "types");
break;
}
strlcpy(tp->type_name, type->name, NG_TYPESIZ);
tp->numnodes = type->refs - 1;
tl->numtypes++;
}
*resp = rp;
break;
}
case NGM_BINARY2ASCII:
{
int bufSize = 20 * 1024;
const struct ng_parse_type *argstype;
const struct ng_cmdlist *c;
struct ng_mesg *rp, *binary, *ascii;
binary = (struct ng_mesg *)msg->data;
if (msg->header.arglen < sizeof(struct ng_mesg)
|| msg->header.arglen - sizeof(struct ng_mesg)
< binary->header.arglen) {
error = EINVAL;
break;
}
NG_MKRESPONSE(rp, msg, sizeof(*ascii) + bufSize, M_NOWAIT);
if (rp == NULL) {
error = ENOMEM;
break;
}
ascii = (struct ng_mesg *)rp->data;
bcopy(binary, ascii, sizeof(*binary));
for (c = here->type->cmdlist;
c != NULL && c->name != NULL; c++) {
if (binary->header.typecookie == c->cookie
&& binary->header.cmd == c->cmd)
break;
}
if (c == NULL || c->name == NULL) {
for (c = ng_generic_cmds; c->name != NULL; c++) {
if (binary->header.typecookie == c->cookie
&& binary->header.cmd == c->cmd)
break;
}
if (c->name == NULL) {
kfree(rp, M_NETGRAPH);
error = ENOSYS;
break;
}
}
ksnprintf(ascii->header.cmdstr, sizeof(ascii->header.cmdstr),
"%s", c->name);
argstype = (binary->header.flags & NGF_RESP) ?
c->respType : c->mesgType;
if (argstype == NULL)
*ascii->data = '\0';
else {
if ((error = ng_unparse(argstype,
(u_char *)binary->data,
ascii->data, bufSize)) != 0) {
kfree(rp, M_NETGRAPH);
break;
}
}
bufSize = strlen(ascii->data) + 1;
ascii->header.arglen = bufSize;
rp->header.arglen = sizeof(*ascii) + bufSize;
*resp = rp;
break;
}
case NGM_ASCII2BINARY:
{
int bufSize = 2000;
const struct ng_cmdlist *c;
const struct ng_parse_type *argstype;
struct ng_mesg *rp, *ascii, *binary;
int off = 0;
ascii = (struct ng_mesg *)msg->data;
if (msg->header.arglen < sizeof(*ascii) + 1
|| ascii->header.arglen < 1
|| msg->header.arglen
< sizeof(*ascii) + ascii->header.arglen) {
error = EINVAL;
break;
}
ascii->data[ascii->header.arglen - 1] = '\0';
NG_MKRESPONSE(rp, msg, sizeof(*binary) + bufSize, M_NOWAIT);
if (rp == NULL) {
error = ENOMEM;
break;
}
binary = (struct ng_mesg *)rp->data;
bcopy(ascii, binary, sizeof(*ascii));
for (c = here->type->cmdlist;
c != NULL && c->name != NULL; c++) {
if (strcmp(ascii->header.cmdstr, c->name) == 0)
break;
}
if (c == NULL || c->name == NULL) {
for (c = ng_generic_cmds; c->name != NULL; c++) {
if (strcmp(ascii->header.cmdstr, c->name) == 0)
break;
}
if (c->name == NULL) {
kfree(rp, M_NETGRAPH);
error = ENOSYS;
break;
}
}
binary->header.cmd = c->cmd;
binary->header.typecookie = c->cookie;
argstype = (binary->header.flags & NGF_RESP) ?
c->respType : c->mesgType;
if (argstype == NULL)
bufSize = 0;
else {
if ((error = ng_parse(argstype, ascii->data,
&off, (u_char *)binary->data, &bufSize)) != 0) {
kfree(rp, M_NETGRAPH);
break;
}
}
binary->header.arglen = bufSize;
rp->header.arglen = sizeof(*binary) + bufSize;
*resp = rp;
break;
}
case NGM_TEXT_CONFIG:
case NGM_TEXT_STATUS:
if (resp == NULL) {
error = EINVAL;
break;
}
if (here->type->rcvmsg != NULL)
return((*here->type->rcvmsg)(here, msg, retaddr, resp));
default:
TRAP_ERROR;
error = EINVAL;
}
kfree(msg, M_NETGRAPH);
return (error);
}
int
ng_send_data(hook_p hook, struct mbuf *m, meta_p meta)
{
int (*rcvdata)(hook_p, struct mbuf *, meta_p);
int error;
CHECK_DATA_MBUF(m);
if (hook && (hook->flags & HK_INVALID) == 0) {
rcvdata = hook->peer->node->type->rcvdata;
if (rcvdata != NULL)
error = (*rcvdata)(hook->peer, m, meta);
else {
error = 0;
NG_FREE_DATA(m, meta);
}
} else {
TRAP_ERROR;
error = ENOTCONN;
NG_FREE_DATA(m, meta);
}
return (error);
}
int
ng_send_dataq(hook_p hook, struct mbuf *m, meta_p meta)
{
int (*rcvdataq)(hook_p, struct mbuf *, meta_p);
int error;
CHECK_DATA_MBUF(m);
if (hook && (hook->flags & HK_INVALID) == 0) {
rcvdataq = hook->peer->node->type->rcvdataq;
if (rcvdataq != NULL)
error = (*rcvdataq)(hook->peer, m, meta);
else {
error = ng_send_data(hook, m, meta);
}
} else {
TRAP_ERROR;
error = ENOTCONN;
NG_FREE_DATA(m, meta);
}
return (error);
}
meta_p
ng_copy_meta(meta_p meta)
{
meta_p meta2;
if (meta == NULL)
return (NULL);
meta2 = kmalloc(meta->used_len, M_NETGRAPH, M_NOWAIT);
if (meta2 == NULL)
return (NULL);
meta2->allocated_len = meta->used_len;
bcopy(meta, meta2, meta->used_len);
return (meta2);
}
int
ng_mod_event(module_t mod, int event, void *data)
{
struct ng_type *const type = data;
int error = 0;
switch (event) {
case MOD_LOAD:
crit_enter();
if ((error = ng_newtype(type)) != 0) {
crit_exit();
break;
}
if (type->mod_event != NULL)
if ((error = (*type->mod_event)(mod, event, data))) {
type->refs--;
LIST_REMOVE(type, types);
}
crit_exit();
break;
case MOD_UNLOAD:
crit_enter();
if (type->refs > 1) {
error = EBUSY;
} else {
if (type->refs == 0) {
crit_exit();
break;
}
if (type->mod_event != NULL) {
error = (*type->mod_event)(mod, event, data);
if (error != 0) {
crit_exit();
break;
}
}
LIST_REMOVE(type, types);
}
crit_exit();
break;
default:
if (type->mod_event != NULL)
error = (*type->mod_event)(mod, event, data);
else
error = 0;
break;
}
return (error);
}
static int
ngb_mod_event(module_t mod, int event, void *data)
{
int error = 0;
switch (event) {
case MOD_LOAD:
crit_enter();
netisr_register(NETISR_NETGRAPH, ngintr, NULL);
crit_exit();
break;
case MOD_UNLOAD:
error = EBUSY;
break;
default:
error = EOPNOTSUPP;
break;
}
return (error);
}
static moduledata_t netgraph_mod = {
"netgraph",
ngb_mod_event,
(NULL)
};
DECLARE_MODULE(netgraph, netgraph_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
SYSCTL_NODE(_net, OID_AUTO, graph, CTLFLAG_RW, 0, "netgraph Family");
SYSCTL_INT(_net_graph, OID_AUTO, abi_version, CTLFLAG_RD, 0, NG_ABI_VERSION,"");
SYSCTL_INT(_net_graph, OID_AUTO, msg_version, CTLFLAG_RD, 0, NG_VERSION, "");
struct ng_queue_entry {
u_long flags;
struct ng_queue_entry *next;
union {
struct {
hook_p da_hook;
struct mbuf *da_m;
meta_p da_meta;
} data;
struct {
struct ng_mesg *msg_msg;
node_p msg_node;
void *msg_retaddr;
} msg;
} body;
};
#define NGQF_DATA 0x01
#define NGQF_MESG 0x02
static struct ng_queue_entry *ngqbase;
static struct ng_queue_entry *ngqlast;
static const int ngqroom = 256;
static int ngqsize;
static struct ng_queue_entry *ngqfree;
static const int ngqfreemax = 256;
static int ngqfreesize;
static struct ng_queue_entry *
ng_getqblk(void)
{
struct ng_queue_entry *q;
crit_enter();
if ((q = ngqfree) == NULL) {
crit_exit();
if (ngqsize < ngqroom) {
q = kmalloc(sizeof(*q), M_NETGRAPH, M_NOWAIT);
}
} else {
ngqfree = q->next;
ngqfreesize--;
crit_exit();
}
return (q);
}
#define RETURN_QBLK(q) \
do { \
if (ngqfreesize < ngqfreemax) { \
crit_enter(); \
(q)->next = ngqfree; \
ngqfree = (q); \
ngqfreesize++; \
crit_exit(); \
} else { \
kfree((q), M_NETGRAPH); \
} \
} while (0)
int
ng_queue_data(hook_p hook, struct mbuf *m, meta_p meta)
{
struct ng_queue_entry *q;
if (hook == NULL) {
NG_FREE_DATA(m, meta);
return (0);
}
if ((q = ng_getqblk()) == NULL) {
NG_FREE_DATA(m, meta);
return (ENOBUFS);
}
q->flags = NGQF_DATA;
q->next = NULL;
q->body.data.da_hook = hook;
q->body.data.da_m = m;
q->body.data.da_meta = meta;
crit_enter();
hook->refs++;
if (ngqbase) {
ngqlast->next = q;
} else {
ngqbase = q;
}
ngqlast = q;
ngqsize++;
crit_exit();
schednetisr(NETISR_NETGRAPH);
return (0);
}
int
ng_queue_msg(node_p here, struct ng_mesg *msg, const char *address)
{
struct ng_queue_entry *q;
node_p dest = NULL;
char *retaddr = NULL;
int error;
error = ng_path2node(here, address, &dest, &retaddr);
if (error) {
kfree(msg, M_NETGRAPH);
return (error);
}
if ((q = ng_getqblk()) == NULL) {
kfree(msg, M_NETGRAPH);
if (retaddr)
kfree(retaddr, M_NETGRAPH);
return (ENOBUFS);
}
q->flags = NGQF_MESG;
q->next = NULL;
q->body.msg.msg_node = dest;
q->body.msg.msg_msg = msg;
q->body.msg.msg_retaddr = retaddr;
crit_enter();
dest->refs++;
if (ngqbase) {
ngqlast->next = q;
} else {
ngqbase = q;
}
ngqlast = q;
ngqsize++;
crit_exit();
schednetisr(NETISR_NETGRAPH);
return (0);
}
static void
ngintr(netmsg_t pmsg)
{
hook_p hook;
struct mbuf *m;
struct ng_queue_entry *ngq;
meta_p meta;
void *retaddr;
struct ng_mesg *msg;
node_p node;
int error = 0;
lwkt_replymsg(&pmsg->lmsg, 0);
get_mplock();
while (1) {
crit_enter();
if ((ngq = ngqbase)) {
ngqbase = ngq->next;
ngqsize--;
}
crit_exit();
if (ngq == NULL)
goto out;
switch (ngq->flags) {
case NGQF_DATA:
hook = ngq->body.data.da_hook;
m = ngq->body.data.da_m;
meta = ngq->body.data.da_meta;
RETURN_QBLK(ngq);
NG_SEND_DATAQ(error, hook, m, meta);
ng_unref_hook(hook);
break;
case NGQF_MESG:
node = ngq->body.msg.msg_node;
msg = ngq->body.msg.msg_msg;
retaddr = ngq->body.msg.msg_retaddr;
RETURN_QBLK(ngq);
if (node->flags & NG_INVALID) {
kfree(msg, M_NETGRAPH);
} else {
CALL_MSG_HANDLER(error, node, msg,
retaddr, NULL);
}
ng_unref(node);
if (retaddr)
kfree(retaddr, M_NETGRAPH);
break;
default:
RETURN_QBLK(ngq);
}
}
out:
rel_mplock();
}