The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/netgraph/ng_base.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 1996-1999 Whistle Communications, Inc.
    3  * All rights reserved.
    4  *
    5  * Subject to the following obligations and disclaimer of warranty, use and
    6  * redistribution of this software, in source or object code forms, with or
    7  * without modifications are expressly permitted by Whistle Communications;
    8  * provided, however, that:
    9  * 1. Any and all reproductions of the source or object code must include the
   10  *    copyright notice above and the following disclaimer of warranties; and
   11  * 2. No rights are granted, in any manner or form, to use Whistle
   12  *    Communications, Inc. trademarks, including the mark "WHISTLE
   13  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
   14  *    such appears in the above copyright notice or in the software.
   15  *
   16  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
   17  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
   18  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
   19  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
   20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
   21  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
   22  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
   23  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
   24  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
   25  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
   26  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
   27  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
   28  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
   29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   31  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
   32  * OF SUCH DAMAGE.
   33  *
   34  * Authors: Julian Elischer <julian@freebsd.org>
   35  *          Archie Cobbs <archie@freebsd.org>
   36  *
   37  * $FreeBSD$
   38  * $Whistle: ng_base.c,v 1.39 1999/01/28 23:54:53 julian Exp $
   39  */
   40 
   41 /*
   42  * This file implements the base netgraph code.
   43  */
   44 
   45 #include <sys/param.h>
   46 #include <sys/systm.h>
   47 #include <sys/ctype.h>
   48 #include <sys/hash.h>
   49 #include <sys/kdb.h>
   50 #include <sys/kernel.h>
   51 #include <sys/kthread.h>
   52 #include <sys/ktr.h>
   53 #include <sys/limits.h>
   54 #include <sys/lock.h>
   55 #include <sys/malloc.h>
   56 #include <sys/mbuf.h>
   57 #include <sys/proc.h>
   58 #include <sys/queue.h>
   59 #include <sys/refcount.h>
   60 #include <sys/rwlock.h>
   61 #include <sys/smp.h>
   62 #include <sys/sysctl.h>
   63 #include <sys/syslog.h>
   64 #include <sys/unistd.h>
   65 #include <machine/cpu.h>
   66 #include <vm/uma.h>
   67 
   68 #include <net/netisr.h>
   69 #include <net/vnet.h>
   70 
   71 #include <netgraph/ng_message.h>
   72 #include <netgraph/netgraph.h>
   73 #include <netgraph/ng_parse.h>
   74 
   75 MODULE_VERSION(netgraph, NG_ABI_VERSION);
   76 
   77 /* Mutex to protect topology events. */
   78 static struct rwlock    ng_topo_lock;
   79 #define TOPOLOGY_RLOCK()        rw_rlock(&ng_topo_lock)
   80 #define TOPOLOGY_RUNLOCK()      rw_runlock(&ng_topo_lock)
   81 #define TOPOLOGY_WLOCK()        rw_wlock(&ng_topo_lock)
   82 #define TOPOLOGY_WUNLOCK()      rw_wunlock(&ng_topo_lock)
   83 #define TOPOLOGY_NOTOWNED()     rw_assert(&ng_topo_lock, RA_UNLOCKED)
   84 
   85 #ifdef  NETGRAPH_DEBUG
   86 static struct mtx       ng_nodelist_mtx; /* protects global node/hook lists */
   87 static struct mtx       ngq_mtx;        /* protects the queue item list */
   88 
   89 static SLIST_HEAD(, ng_node) ng_allnodes;
   90 static LIST_HEAD(, ng_node) ng_freenodes; /* in debug, we never free() them */
   91 static SLIST_HEAD(, ng_hook) ng_allhooks;
   92 static LIST_HEAD(, ng_hook) ng_freehooks; /* in debug, we never free() them */
   93 
   94 static void ng_dumpitems(void);
   95 static void ng_dumpnodes(void);
   96 static void ng_dumphooks(void);
   97 
   98 #endif  /* NETGRAPH_DEBUG */
   99 /*
  100  * DEAD versions of the structures.
  101  * In order to avoid races, it is sometimes necessary to point
  102  * at SOMETHING even though theoretically, the current entity is
  103  * INVALID. Use these to avoid these races.
  104  */
  105 struct ng_type ng_deadtype = {
  106         NG_ABI_VERSION,
  107         "dead",
  108         NULL,   /* modevent */
  109         NULL,   /* constructor */
  110         NULL,   /* rcvmsg */
  111         NULL,   /* shutdown */
  112         NULL,   /* newhook */
  113         NULL,   /* findhook */
  114         NULL,   /* connect */
  115         NULL,   /* rcvdata */
  116         NULL,   /* disconnect */
  117         NULL,   /* cmdlist */
  118 };
  119 
  120 struct ng_node ng_deadnode = {
  121         "dead",
  122         &ng_deadtype,   
  123         NGF_INVALID,
  124         0,      /* numhooks */
  125         NULL,   /* private */
  126         0,      /* ID */
  127         LIST_HEAD_INITIALIZER(ng_deadnode.nd_hooks),
  128         {},     /* all_nodes list entry */
  129         {},     /* id hashtable list entry */
  130         {       0,
  131                 0,
  132                 {}, /* should never use! (should hang) */
  133                 {}, /* workqueue entry */
  134                 STAILQ_HEAD_INITIALIZER(ng_deadnode.nd_input_queue.queue),
  135         },
  136         1,      /* refs */
  137         NULL,   /* vnet */
  138 #ifdef  NETGRAPH_DEBUG
  139         ND_MAGIC,
  140         __FILE__,
  141         __LINE__,
  142         {NULL}
  143 #endif  /* NETGRAPH_DEBUG */
  144 };
  145 
  146 struct ng_hook ng_deadhook = {
  147         "dead",
  148         NULL,           /* private */
  149         HK_INVALID | HK_DEAD,
  150         0,              /* undefined data link type */
  151         &ng_deadhook,   /* Peer is self */
  152         &ng_deadnode,   /* attached to deadnode */
  153         {},             /* hooks list */
  154         NULL,           /* override rcvmsg() */
  155         NULL,           /* override rcvdata() */
  156         1,              /* refs always >= 1 */
  157 #ifdef  NETGRAPH_DEBUG
  158         HK_MAGIC,
  159         __FILE__,
  160         __LINE__,
  161         {NULL}
  162 #endif  /* NETGRAPH_DEBUG */
  163 };
  164 
  165 /*
  166  * END DEAD STRUCTURES
  167  */
  168 /* List nodes with unallocated work */
  169 static STAILQ_HEAD(, ng_node) ng_worklist = STAILQ_HEAD_INITIALIZER(ng_worklist);
  170 static struct mtx       ng_worklist_mtx;   /* MUST LOCK NODE FIRST */
  171 
  172 /* List of installed types */
  173 static LIST_HEAD(, ng_type) ng_typelist;
  174 static struct rwlock    ng_typelist_lock;
  175 #define TYPELIST_RLOCK()        rw_rlock(&ng_typelist_lock)
  176 #define TYPELIST_RUNLOCK()      rw_runlock(&ng_typelist_lock)
  177 #define TYPELIST_WLOCK()        rw_wlock(&ng_typelist_lock)
  178 #define TYPELIST_WUNLOCK()      rw_wunlock(&ng_typelist_lock)
  179 
  180 /* Hash related definitions. */
  181 LIST_HEAD(nodehash, ng_node);
  182 VNET_DEFINE_STATIC(struct nodehash *, ng_ID_hash);
  183 VNET_DEFINE_STATIC(u_long, ng_ID_hmask);
  184 VNET_DEFINE_STATIC(u_long, ng_nodes);
  185 VNET_DEFINE_STATIC(struct nodehash *, ng_name_hash);
  186 VNET_DEFINE_STATIC(u_long, ng_name_hmask);
  187 VNET_DEFINE_STATIC(u_long, ng_named_nodes);
  188 #define V_ng_ID_hash            VNET(ng_ID_hash)
  189 #define V_ng_ID_hmask           VNET(ng_ID_hmask)
  190 #define V_ng_nodes              VNET(ng_nodes)
  191 #define V_ng_name_hash          VNET(ng_name_hash)
  192 #define V_ng_name_hmask         VNET(ng_name_hmask)
  193 #define V_ng_named_nodes        VNET(ng_named_nodes)
  194 
  195 static struct rwlock    ng_idhash_lock;
  196 #define IDHASH_RLOCK()          rw_rlock(&ng_idhash_lock)
  197 #define IDHASH_RUNLOCK()        rw_runlock(&ng_idhash_lock)
  198 #define IDHASH_WLOCK()          rw_wlock(&ng_idhash_lock)
  199 #define IDHASH_WUNLOCK()        rw_wunlock(&ng_idhash_lock)
  200 
  201 /* Method to find a node.. used twice so do it here */
  202 #define NG_IDHASH_FN(ID) ((ID) % (V_ng_ID_hmask + 1))
  203 #define NG_IDHASH_FIND(ID, node)                                        \
  204         do {                                                            \
  205                 rw_assert(&ng_idhash_lock, RA_LOCKED);                  \
  206                 LIST_FOREACH(node, &V_ng_ID_hash[NG_IDHASH_FN(ID)],     \
  207                                                 nd_idnodes) {           \
  208                         if (NG_NODE_IS_VALID(node)                      \
  209                         && (NG_NODE_ID(node) == ID)) {                  \
  210                                 break;                                  \
  211                         }                                               \
  212                 }                                                       \
  213         } while (0)
  214 
  215 static struct rwlock    ng_namehash_lock;
  216 #define NAMEHASH_RLOCK()        rw_rlock(&ng_namehash_lock)
  217 #define NAMEHASH_RUNLOCK()      rw_runlock(&ng_namehash_lock)
  218 #define NAMEHASH_WLOCK()        rw_wlock(&ng_namehash_lock)
  219 #define NAMEHASH_WUNLOCK()      rw_wunlock(&ng_namehash_lock)
  220 
  221 /* Internal functions */
  222 static int      ng_add_hook(node_p node, const char *name, hook_p * hookp);
  223 static int      ng_generic_msg(node_p here, item_p item, hook_p lasthook);
  224 static ng_ID_t  ng_decodeidname(const char *name);
  225 static int      ngb_mod_event(module_t mod, int event, void *data);
  226 static void     ng_worklist_add(node_p node);
  227 static void     ngthread(void *);
  228 static int      ng_apply_item(node_p node, item_p item, int rw);
  229 static void     ng_flush_input_queue(node_p node);
  230 static node_p   ng_ID2noderef(ng_ID_t ID);
  231 static int      ng_con_nodes(item_p item, node_p node, const char *name,
  232                     node_p node2, const char *name2);
  233 static int      ng_con_part2(node_p node, item_p item, hook_p hook);
  234 static int      ng_con_part3(node_p node, item_p item, hook_p hook);
  235 static int      ng_mkpeer(node_p node, const char *name, const char *name2,
  236                     char *type);
  237 static void     ng_name_rehash(void);
  238 static void     ng_ID_rehash(void);
  239 
  240 /* Imported, these used to be externally visible, some may go back. */
  241 void    ng_destroy_hook(hook_p hook);
  242 int     ng_path2noderef(node_p here, const char *path,
  243         node_p *dest, hook_p *lasthook);
  244 int     ng_make_node(const char *type, node_p *nodepp);
  245 int     ng_path_parse(char *addr, char **node, char **path, char **hook);
  246 void    ng_rmnode(node_p node, hook_p dummy1, void *dummy2, int dummy3);
  247 void    ng_unname(node_p node);
  248 
  249 /* Our own netgraph malloc type */
  250 MALLOC_DEFINE(M_NETGRAPH, "netgraph", "netgraph structures and ctrl messages");
  251 MALLOC_DEFINE(M_NETGRAPH_MSG, "netgraph_msg", "netgraph name storage");
  252 static MALLOC_DEFINE(M_NETGRAPH_HOOK, "netgraph_hook",
  253     "netgraph hook structures");
  254 static MALLOC_DEFINE(M_NETGRAPH_NODE, "netgraph_node",
  255     "netgraph node structures");
  256 static MALLOC_DEFINE(M_NETGRAPH_ITEM, "netgraph_item",
  257     "netgraph item structures");
  258 
  259 /* Should not be visible outside this file */
  260 
  261 #define _NG_ALLOC_HOOK(hook) \
  262         hook = malloc(sizeof(*hook), M_NETGRAPH_HOOK, M_NOWAIT | M_ZERO)
  263 #define _NG_ALLOC_NODE(node) \
  264         node = malloc(sizeof(*node), M_NETGRAPH_NODE, M_NOWAIT | M_ZERO)
  265 
  266 #define NG_QUEUE_LOCK_INIT(n)                   \
  267         mtx_init(&(n)->q_mtx, "ng_node", NULL, MTX_DEF)
  268 #define NG_QUEUE_LOCK(n)                        \
  269         mtx_lock(&(n)->q_mtx)
  270 #define NG_QUEUE_UNLOCK(n)                      \
  271         mtx_unlock(&(n)->q_mtx)
  272 #define NG_WORKLIST_LOCK_INIT()                 \
  273         mtx_init(&ng_worklist_mtx, "ng_worklist", NULL, MTX_DEF)
  274 #define NG_WORKLIST_LOCK()                      \
  275         mtx_lock(&ng_worklist_mtx)
  276 #define NG_WORKLIST_UNLOCK()                    \
  277         mtx_unlock(&ng_worklist_mtx)
  278 #define NG_WORKLIST_SLEEP()                     \
  279         mtx_sleep(&ng_worklist, &ng_worklist_mtx, PI_NET, "sleep", 0)
  280 #define NG_WORKLIST_WAKEUP()                    \
  281         wakeup_one(&ng_worklist)
  282 
  283 #ifdef NETGRAPH_DEBUG /*----------------------------------------------*/
  284 /*
  285  * In debug mode:
  286  * In an attempt to help track reference count screwups
  287  * we do not free objects back to the malloc system, but keep them
  288  * in a local cache where we can examine them and keep information safely
  289  * after they have been freed.
  290  * We use this scheme for nodes and hooks, and to some extent for items.
  291  */
  292 static __inline hook_p
  293 ng_alloc_hook(void)
  294 {
  295         hook_p hook;
  296         SLIST_ENTRY(ng_hook) temp;
  297         mtx_lock(&ng_nodelist_mtx);
  298         hook = LIST_FIRST(&ng_freehooks);
  299         if (hook) {
  300                 LIST_REMOVE(hook, hk_hooks);
  301                 bcopy(&hook->hk_all, &temp, sizeof(temp));
  302                 bzero(hook, sizeof(struct ng_hook));
  303                 bcopy(&temp, &hook->hk_all, sizeof(temp));
  304                 mtx_unlock(&ng_nodelist_mtx);
  305                 hook->hk_magic = HK_MAGIC;
  306         } else {
  307                 mtx_unlock(&ng_nodelist_mtx);
  308                 _NG_ALLOC_HOOK(hook);
  309                 if (hook) {
  310                         hook->hk_magic = HK_MAGIC;
  311                         mtx_lock(&ng_nodelist_mtx);
  312                         SLIST_INSERT_HEAD(&ng_allhooks, hook, hk_all);
  313                         mtx_unlock(&ng_nodelist_mtx);
  314                 }
  315         }
  316         return (hook);
  317 }
  318 
  319 static __inline node_p
  320 ng_alloc_node(void)
  321 {
  322         node_p node;
  323         SLIST_ENTRY(ng_node) temp;
  324         mtx_lock(&ng_nodelist_mtx);
  325         node = LIST_FIRST(&ng_freenodes);
  326         if (node) {
  327                 LIST_REMOVE(node, nd_nodes);
  328                 bcopy(&node->nd_all, &temp, sizeof(temp));
  329                 bzero(node, sizeof(struct ng_node));
  330                 bcopy(&temp, &node->nd_all, sizeof(temp));
  331                 mtx_unlock(&ng_nodelist_mtx);
  332                 node->nd_magic = ND_MAGIC;
  333         } else {
  334                 mtx_unlock(&ng_nodelist_mtx);
  335                 _NG_ALLOC_NODE(node);
  336                 if (node) {
  337                         node->nd_magic = ND_MAGIC;
  338                         mtx_lock(&ng_nodelist_mtx);
  339                         SLIST_INSERT_HEAD(&ng_allnodes, node, nd_all);
  340                         mtx_unlock(&ng_nodelist_mtx);
  341                 }
  342         }
  343         return (node);
  344 }
  345 
  346 #define NG_ALLOC_HOOK(hook) do { (hook) = ng_alloc_hook(); } while (0)
  347 #define NG_ALLOC_NODE(node) do { (node) = ng_alloc_node(); } while (0)
  348 
  349 #define NG_FREE_HOOK(hook)                                              \
  350         do {                                                            \
  351                 mtx_lock(&ng_nodelist_mtx);                             \
  352                 LIST_INSERT_HEAD(&ng_freehooks, hook, hk_hooks);        \
  353                 hook->hk_magic = 0;                                     \
  354                 mtx_unlock(&ng_nodelist_mtx);                           \
  355         } while (0)
  356 
  357 #define NG_FREE_NODE(node)                                              \
  358         do {                                                            \
  359                 mtx_lock(&ng_nodelist_mtx);                             \
  360                 LIST_INSERT_HEAD(&ng_freenodes, node, nd_nodes);        \
  361                 node->nd_magic = 0;                                     \
  362                 mtx_unlock(&ng_nodelist_mtx);                           \
  363         } while (0)
  364 
  365 #else /* NETGRAPH_DEBUG */ /*----------------------------------------------*/
  366 
  367 #define NG_ALLOC_HOOK(hook) _NG_ALLOC_HOOK(hook)
  368 #define NG_ALLOC_NODE(node) _NG_ALLOC_NODE(node)
  369 
  370 #define NG_FREE_HOOK(hook) do { free((hook), M_NETGRAPH_HOOK); } while (0)
  371 #define NG_FREE_NODE(node) do { free((node), M_NETGRAPH_NODE); } while (0)
  372 
  373 #endif /* NETGRAPH_DEBUG */ /*----------------------------------------------*/
  374 
  375 /* Set this to kdb_enter("X") to catch all errors as they occur */
  376 #ifndef TRAP_ERROR
  377 #define TRAP_ERROR()
  378 #endif
  379 
  380 VNET_DEFINE_STATIC(ng_ID_t, nextID) = 1;
  381 #define V_nextID                        VNET(nextID)
  382 
  383 #ifdef INVARIANTS
  384 #define CHECK_DATA_MBUF(m)      do {                                    \
  385                 struct mbuf *n;                                         \
  386                 int total;                                              \
  387                                                                         \
  388                 M_ASSERTPKTHDR(m);                                      \
  389                 for (total = 0, n = (m); n != NULL; n = n->m_next) {    \
  390                         total += n->m_len;                              \
  391                         if (n->m_nextpkt != NULL)                       \
  392                                 panic("%s: m_nextpkt", __func__);       \
  393                 }                                                       \
  394                                                                         \
  395                 if ((m)->m_pkthdr.len != total) {                       \
  396                         panic("%s: %d != %d",                           \
  397                             __func__, (m)->m_pkthdr.len, total);        \
  398                 }                                                       \
  399         } while (0)
  400 #else
  401 #define CHECK_DATA_MBUF(m)
  402 #endif
  403 
  404 #define ERROUT(x)       do { error = (x); goto done; } while (0)
  405 
  406 /************************************************************************
  407         Parse type definitions for generic messages
  408 ************************************************************************/
  409 
  410 /* Handy structure parse type defining macro */
  411 #define DEFINE_PARSE_STRUCT_TYPE(lo, up, args)                          \
  412 static const struct ng_parse_struct_field                               \
  413         ng_ ## lo ## _type_fields[] = NG_GENERIC_ ## up ## _INFO args;  \
  414 static const struct ng_parse_type ng_generic_ ## lo ## _type = {        \
  415         &ng_parse_struct_type,                                          \
  416         &ng_ ## lo ## _type_fields                                      \
  417 }
  418 
  419 DEFINE_PARSE_STRUCT_TYPE(mkpeer, MKPEER, ());
  420 DEFINE_PARSE_STRUCT_TYPE(connect, CONNECT, ());
  421 DEFINE_PARSE_STRUCT_TYPE(name, NAME, ());
  422 DEFINE_PARSE_STRUCT_TYPE(rmhook, RMHOOK, ());
  423 DEFINE_PARSE_STRUCT_TYPE(nodeinfo, NODEINFO, ());
  424 DEFINE_PARSE_STRUCT_TYPE(typeinfo, TYPEINFO, ());
  425 DEFINE_PARSE_STRUCT_TYPE(linkinfo, LINKINFO, (&ng_generic_nodeinfo_type));
  426 
  427 /* Get length of an array when the length is stored as a 32 bit
  428    value immediately preceding the array -- as with struct namelist
  429    and struct typelist. */
  430 static int
  431 ng_generic_list_getLength(const struct ng_parse_type *type,
  432         const u_char *start, const u_char *buf)
  433 {
  434         return *((const u_int32_t *)(buf - 4));
  435 }
  436 
  437 /* Get length of the array of struct linkinfo inside a struct hooklist */
  438 static int
  439 ng_generic_linkinfo_getLength(const struct ng_parse_type *type,
  440         const u_char *start, const u_char *buf)
  441 {
  442         const struct hooklist *hl = (const struct hooklist *)start;
  443 
  444         return hl->nodeinfo.hooks;
  445 }
  446 
  447 /* Array type for a variable length array of struct namelist */
  448 static const struct ng_parse_array_info ng_nodeinfoarray_type_info = {
  449         &ng_generic_nodeinfo_type,
  450         &ng_generic_list_getLength
  451 };
  452 static const struct ng_parse_type ng_generic_nodeinfoarray_type = {
  453         &ng_parse_array_type,
  454         &ng_nodeinfoarray_type_info
  455 };
  456 
  457 /* Array type for a variable length array of struct typelist */
  458 static const struct ng_parse_array_info ng_typeinfoarray_type_info = {
  459         &ng_generic_typeinfo_type,
  460         &ng_generic_list_getLength
  461 };
  462 static const struct ng_parse_type ng_generic_typeinfoarray_type = {
  463         &ng_parse_array_type,
  464         &ng_typeinfoarray_type_info
  465 };
  466 
  467 /* Array type for array of struct linkinfo in struct hooklist */
  468 static const struct ng_parse_array_info ng_generic_linkinfo_array_type_info = {
  469         &ng_generic_linkinfo_type,
  470         &ng_generic_linkinfo_getLength
  471 };
  472 static const struct ng_parse_type ng_generic_linkinfo_array_type = {
  473         &ng_parse_array_type,
  474         &ng_generic_linkinfo_array_type_info
  475 };
  476 
  477 DEFINE_PARSE_STRUCT_TYPE(typelist, TYPELIST, (&ng_generic_typeinfoarray_type));
  478 DEFINE_PARSE_STRUCT_TYPE(hooklist, HOOKLIST,
  479         (&ng_generic_nodeinfo_type, &ng_generic_linkinfo_array_type));
  480 DEFINE_PARSE_STRUCT_TYPE(listnodes, LISTNODES,
  481         (&ng_generic_nodeinfoarray_type));
  482 
  483 /* List of commands and how to convert arguments to/from ASCII */
  484 static const struct ng_cmdlist ng_generic_cmds[] = {
  485         {
  486           NGM_GENERIC_COOKIE,
  487           NGM_SHUTDOWN,
  488           "shutdown",
  489           NULL,
  490           NULL
  491         },
  492         {
  493           NGM_GENERIC_COOKIE,
  494           NGM_MKPEER,
  495           "mkpeer",
  496           &ng_generic_mkpeer_type,
  497           NULL
  498         },
  499         {
  500           NGM_GENERIC_COOKIE,
  501           NGM_CONNECT,
  502           "connect",
  503           &ng_generic_connect_type,
  504           NULL
  505         },
  506         {
  507           NGM_GENERIC_COOKIE,
  508           NGM_NAME,
  509           "name",
  510           &ng_generic_name_type,
  511           NULL
  512         },
  513         {
  514           NGM_GENERIC_COOKIE,
  515           NGM_RMHOOK,
  516           "rmhook",
  517           &ng_generic_rmhook_type,
  518           NULL
  519         },
  520         {
  521           NGM_GENERIC_COOKIE,
  522           NGM_NODEINFO,
  523           "nodeinfo",
  524           NULL,
  525           &ng_generic_nodeinfo_type
  526         },
  527         {
  528           NGM_GENERIC_COOKIE,
  529           NGM_LISTHOOKS,
  530           "listhooks",
  531           NULL,
  532           &ng_generic_hooklist_type
  533         },
  534         {
  535           NGM_GENERIC_COOKIE,
  536           NGM_LISTNAMES,
  537           "listnames",
  538           NULL,
  539           &ng_generic_listnodes_type    /* same as NGM_LISTNODES */
  540         },
  541         {
  542           NGM_GENERIC_COOKIE,
  543           NGM_LISTNODES,
  544           "listnodes",
  545           NULL,
  546           &ng_generic_listnodes_type
  547         },
  548         {
  549           NGM_GENERIC_COOKIE,
  550           NGM_LISTTYPES,
  551           "listtypes",
  552           NULL,
  553           &ng_generic_typelist_type
  554         },
  555         {
  556           NGM_GENERIC_COOKIE,
  557           NGM_TEXT_CONFIG,
  558           "textconfig",
  559           NULL,
  560           &ng_parse_string_type
  561         },
  562         {
  563           NGM_GENERIC_COOKIE,
  564           NGM_TEXT_STATUS,
  565           "textstatus",
  566           NULL,
  567           &ng_parse_string_type
  568         },
  569         {
  570           NGM_GENERIC_COOKIE,
  571           NGM_ASCII2BINARY,
  572           "ascii2binary",
  573           &ng_parse_ng_mesg_type,
  574           &ng_parse_ng_mesg_type
  575         },
  576         {
  577           NGM_GENERIC_COOKIE,
  578           NGM_BINARY2ASCII,
  579           "binary2ascii",
  580           &ng_parse_ng_mesg_type,
  581           &ng_parse_ng_mesg_type
  582         },
  583         { 0 }
  584 };
  585 
  586 /************************************************************************
  587                         Node routines
  588 ************************************************************************/
  589 
  590 /*
  591  * Instantiate a node of the requested type
  592  */
  593 int
  594 ng_make_node(const char *typename, node_p *nodepp)
  595 {
  596         struct ng_type *type;
  597         int     error;
  598 
  599         /* Check that the type makes sense */
  600         if (typename == NULL) {
  601                 TRAP_ERROR();
  602                 return (EINVAL);
  603         }
  604 
  605         /* Locate the node type. If we fail we return. Do not try to load
  606          * module.
  607          */
  608         if ((type = ng_findtype(typename)) == NULL)
  609                 return (ENXIO);
  610 
  611         /*
  612          * If we have a constructor, then make the node and
  613          * call the constructor to do type specific initialisation.
  614          */
  615         if (type->constructor != NULL) {
  616                 if ((error = ng_make_node_common(type, nodepp)) == 0) {
  617                         if ((error = ((*type->constructor)(*nodepp))) != 0) {
  618                                 NG_NODE_UNREF(*nodepp);
  619                         }
  620                 }
  621         } else {
  622                 /*
  623                  * Node has no constructor. We cannot ask for one
  624                  * to be made. It must be brought into existence by
  625                  * some external agency. The external agency should
  626                  * call ng_make_node_common() directly to get the
  627                  * netgraph part initialised.
  628                  */
  629                 TRAP_ERROR();
  630                 error = EINVAL;
  631         }
  632         return (error);
  633 }
  634 
  635 /*
  636  * Generic node creation. Called by node initialisation for externally
  637  * instantiated nodes (e.g. hardware, sockets, etc ).
  638  * The returned node has a reference count of 1.
  639  */
  640 int
  641 ng_make_node_common(struct ng_type *type, node_p *nodepp)
  642 {
  643         node_p node;
  644 
  645         /* Require the node type to have been already installed */
  646         if (ng_findtype(type->name) == NULL) {
  647                 TRAP_ERROR();
  648                 return (EINVAL);
  649         }
  650 
  651         /* Make a node and try attach it to the type */
  652         NG_ALLOC_NODE(node);
  653         if (node == NULL) {
  654                 TRAP_ERROR();
  655                 return (ENOMEM);
  656         }
  657         node->nd_type = type;
  658 #ifdef VIMAGE
  659         node->nd_vnet = curvnet;
  660 #endif
  661         NG_NODE_REF(node);                              /* note reference */
  662         type->refs++;
  663 
  664         NG_QUEUE_LOCK_INIT(&node->nd_input_queue);
  665         STAILQ_INIT(&node->nd_input_queue.queue);
  666         node->nd_input_queue.q_flags = 0;
  667 
  668         /* Initialize hook list for new node */
  669         LIST_INIT(&node->nd_hooks);
  670 
  671         /* Get an ID and put us in the hash chain. */
  672         IDHASH_WLOCK();
  673         for (;;) { /* wrap protection, even if silly */
  674                 node_p node2 = NULL;
  675                 node->nd_ID = V_nextID++; /* 137/sec for 1 year before wrap */
  676 
  677                 /* Is there a problem with the new number? */
  678                 NG_IDHASH_FIND(node->nd_ID, node2); /* already taken? */
  679                 if ((node->nd_ID != 0) && (node2 == NULL)) {
  680                         break;
  681                 }
  682         }
  683         V_ng_nodes++;
  684         if (V_ng_nodes * 2 > V_ng_ID_hmask)
  685                 ng_ID_rehash();
  686         LIST_INSERT_HEAD(&V_ng_ID_hash[NG_IDHASH_FN(node->nd_ID)], node,
  687             nd_idnodes);
  688         IDHASH_WUNLOCK();
  689 
  690         /* Done */
  691         *nodepp = node;
  692         return (0);
  693 }
  694 
  695 /*
  696  * Forceably start the shutdown process on a node. Either call
  697  * its shutdown method, or do the default shutdown if there is
  698  * no type-specific method.
  699  *
  700  * We can only be called from a shutdown message, so we know we have
  701  * a writer lock, and therefore exclusive access. It also means
  702  * that we should not be on the work queue, but we check anyhow.
  703  *
  704  * Persistent node types must have a type-specific method which
  705  * allocates a new node in which case, this one is irretrievably going away,
  706  * or cleans up anything it needs, and just makes the node valid again,
  707  * in which case we allow the node to survive.
  708  *
  709  * XXX We need to think of how to tell a persistent node that we
  710  * REALLY need to go away because the hardware has gone or we
  711  * are rebooting.... etc.
  712  */
  713 void
  714 ng_rmnode(node_p node, hook_p dummy1, void *dummy2, int dummy3)
  715 {
  716         hook_p hook;
  717 
  718         /* Check if it's already shutting down */
  719         if ((node->nd_flags & NGF_CLOSING) != 0)
  720                 return;
  721 
  722         if (node == &ng_deadnode) {
  723                 printf ("shutdown called on deadnode\n");
  724                 return;
  725         }
  726 
  727         /* Add an extra reference so it doesn't go away during this */
  728         NG_NODE_REF(node);
  729 
  730         /*
  731          * Mark it invalid so any newcomers know not to try use it
  732          * Also add our own mark so we can't recurse
  733          * note that NGF_INVALID does not do this as it's also set during
  734          * creation
  735          */
  736         node->nd_flags |= NGF_INVALID|NGF_CLOSING;
  737 
  738         /* If node has its pre-shutdown method, then call it first*/
  739         if (node->nd_type && node->nd_type->close)
  740                 (*node->nd_type->close)(node);
  741 
  742         /* Notify all remaining connected nodes to disconnect */
  743         while ((hook = LIST_FIRST(&node->nd_hooks)) != NULL)
  744                 ng_destroy_hook(hook);
  745 
  746         /*
  747          * Drain the input queue forceably.
  748          * it has no hooks so what's it going to do, bleed on someone?
  749          * Theoretically we came here from a queue entry that was added
  750          * Just before the queue was closed, so it should be empty anyway.
  751          * Also removes us from worklist if needed.
  752          */
  753         ng_flush_input_queue(node);
  754 
  755         /* Ask the type if it has anything to do in this case */
  756         if (node->nd_type && node->nd_type->shutdown) {
  757                 (*node->nd_type->shutdown)(node);
  758                 if (NG_NODE_IS_VALID(node)) {
  759                         /*
  760                          * Well, blow me down if the node code hasn't declared
  761                          * that it doesn't want to die.
  762                          * Presumably it is a persistent node.
  763                          * If we REALLY want it to go away,
  764                          *  e.g. hardware going away,
  765                          * Our caller should set NGF_REALLY_DIE in nd_flags.
  766                          */
  767                         node->nd_flags &= ~(NGF_INVALID|NGF_CLOSING);
  768                         NG_NODE_UNREF(node); /* Assume they still have theirs */
  769                         return;
  770                 }
  771         } else {                                /* do the default thing */
  772                 NG_NODE_UNREF(node);
  773         }
  774 
  775         ng_unname(node); /* basically a NOP these days */
  776 
  777         /*
  778          * Remove extra reference, possibly the last
  779          * Possible other holders of references may include
  780          * timeout callouts, but theoretically the node's supposed to
  781          * have cancelled them. Possibly hardware dependencies may
  782          * force a driver to 'linger' with a reference.
  783          */
  784         NG_NODE_UNREF(node);
  785 }
  786 
  787 /*
  788  * Remove a reference to the node, possibly the last.
  789  * deadnode always acts as it were the last.
  790  */
  791 void
  792 ng_unref_node(node_p node)
  793 {
  794 
  795         if (node == &ng_deadnode)
  796                 return;
  797 
  798         CURVNET_SET(node->nd_vnet);
  799 
  800         if (refcount_release(&node->nd_refs)) { /* we were the last */
  801 
  802                 node->nd_type->refs--; /* XXX maybe should get types lock? */
  803                 NAMEHASH_WLOCK();
  804                 if (NG_NODE_HAS_NAME(node)) {
  805                         V_ng_named_nodes--;
  806                         LIST_REMOVE(node, nd_nodes);
  807                 }
  808                 NAMEHASH_WUNLOCK();
  809 
  810                 IDHASH_WLOCK();
  811                 V_ng_nodes--;
  812                 LIST_REMOVE(node, nd_idnodes);
  813                 IDHASH_WUNLOCK();
  814 
  815                 mtx_destroy(&node->nd_input_queue.q_mtx);
  816                 NG_FREE_NODE(node);
  817         }
  818         CURVNET_RESTORE();
  819 }
  820 
  821 /************************************************************************
  822                         Node ID handling
  823 ************************************************************************/
  824 static node_p
  825 ng_ID2noderef(ng_ID_t ID)
  826 {
  827         node_p node;
  828 
  829         IDHASH_RLOCK();
  830         NG_IDHASH_FIND(ID, node);
  831         if (node)
  832                 NG_NODE_REF(node);
  833         IDHASH_RUNLOCK();
  834         return(node);
  835 }
  836 
  837 ng_ID_t
  838 ng_node2ID(node_cp node)
  839 {
  840         return (node ? NG_NODE_ID(node) : 0);
  841 }
  842 
  843 /************************************************************************
  844                         Node name handling
  845 ************************************************************************/
  846 
  847 /*
  848  * Assign a node a name.
  849  */
  850 int
  851 ng_name_node(node_p node, const char *name)
  852 {
  853         uint32_t hash;
  854         node_p node2;
  855         int i;
  856 
  857         /* Rename without change is a noop */
  858         if (strcmp(NG_NODE_NAME(node), name) == 0)
  859                 return (0);
  860 
  861         /* Check the name is valid */
  862         for (i = 0; i < NG_NODESIZ; i++) {
  863                 if (name[i] == '\0' || name[i] == '.' || name[i] == ':')
  864                         break;
  865         }
  866         if (i == 0 || name[i] != '\0') {
  867                 TRAP_ERROR();
  868                 return (EINVAL);
  869         }
  870         if (ng_decodeidname(name) != 0) { /* valid IDs not allowed here */
  871                 TRAP_ERROR();
  872                 return (EINVAL);
  873         }
  874 
  875         NAMEHASH_WLOCK();
  876         if (V_ng_named_nodes * 2 > V_ng_name_hmask)
  877                 ng_name_rehash();
  878 
  879         hash = hash32_str(name, HASHINIT) & V_ng_name_hmask;
  880         /* Check the name isn't already being used. */
  881         LIST_FOREACH(node2, &V_ng_name_hash[hash], nd_nodes)
  882                 if (NG_NODE_IS_VALID(node2) &&
  883                     (strcmp(NG_NODE_NAME(node2), name) == 0)) {
  884                         NAMEHASH_WUNLOCK();
  885                         return (EADDRINUSE);
  886                 }
  887 
  888         if (NG_NODE_HAS_NAME(node))
  889                 LIST_REMOVE(node, nd_nodes);
  890         else
  891                 V_ng_named_nodes++;
  892         /* Copy it. */
  893         strlcpy(NG_NODE_NAME(node), name, NG_NODESIZ);
  894         /* Update name hash. */
  895         LIST_INSERT_HEAD(&V_ng_name_hash[hash], node, nd_nodes);
  896         NAMEHASH_WUNLOCK();
  897 
  898         return (0);
  899 }
  900 
  901 /*
  902  * Find a node by absolute name. The name should NOT end with ':'
  903  * The name "." means "this node" and "[xxx]" means "the node
  904  * with ID (ie, at address) xxx".
  905  *
  906  * Returns the node if found, else NULL.
  907  * Eventually should add something faster than a sequential search.
  908  * Note it acquires a reference on the node so you can be sure it's still
  909  * there.
  910  */
  911 node_p
  912 ng_name2noderef(node_p here, const char *name)
  913 {
  914         node_p node;
  915         ng_ID_t temp;
  916         int     hash;
  917 
  918         /* "." means "this node" */
  919         if (strcmp(name, ".") == 0) {
  920                 NG_NODE_REF(here);
  921                 return(here);
  922         }
  923 
  924         /* Check for name-by-ID */
  925         if ((temp = ng_decodeidname(name)) != 0) {
  926                 return (ng_ID2noderef(temp));
  927         }
  928 
  929         /* Find node by name. */
  930         hash = hash32_str(name, HASHINIT) & V_ng_name_hmask;
  931         NAMEHASH_RLOCK();
  932         LIST_FOREACH(node, &V_ng_name_hash[hash], nd_nodes)
  933                 if (NG_NODE_IS_VALID(node) &&
  934                     (strcmp(NG_NODE_NAME(node), name) == 0)) {
  935                         NG_NODE_REF(node);
  936                         break;
  937                 }
  938         NAMEHASH_RUNLOCK();
  939 
  940         return (node);
  941 }
  942 
  943 /*
  944  * Decode an ID name, eg. "[f03034de]". Returns 0 if the
  945  * string is not valid, otherwise returns the value.
  946  */
  947 static ng_ID_t
  948 ng_decodeidname(const char *name)
  949 {
  950         const int len = strlen(name);
  951         char *eptr;
  952         u_long val;
  953 
  954         /* Check for proper length, brackets, no leading junk */
  955         if ((len < 3) || (name[0] != '[') || (name[len - 1] != ']') ||
  956             (!isxdigit(name[1])))
  957                 return ((ng_ID_t)0);
  958 
  959         /* Decode number */
  960         val = strtoul(name + 1, &eptr, 16);
  961         if ((eptr - name != len - 1) || (val == ULONG_MAX) || (val == 0))
  962                 return ((ng_ID_t)0);
  963 
  964         return ((ng_ID_t)val);
  965 }
  966 
  967 /*
  968  * Remove a name from a node. This should only be called
  969  * when shutting down and removing the node.
  970  */
  971 void
  972 ng_unname(node_p node)
  973 {
  974 }
  975 
  976 /*
  977  * Allocate a bigger name hash.
  978  */
  979 static void
  980 ng_name_rehash(void)
  981 {
  982         struct nodehash *new;
  983         uint32_t hash;
  984         u_long hmask;
  985         node_p node, node2;
  986         int i;
  987 
  988         new = hashinit_flags((V_ng_name_hmask + 1) * 2, M_NETGRAPH_NODE, &hmask,
  989             HASH_NOWAIT);
  990         if (new == NULL)
  991                 return;
  992 
  993         for (i = 0; i <= V_ng_name_hmask; i++)
  994                 LIST_FOREACH_SAFE(node, &V_ng_name_hash[i], nd_nodes, node2) {
  995 #ifdef INVARIANTS
  996                         LIST_REMOVE(node, nd_nodes);
  997 #endif
  998                         hash = hash32_str(NG_NODE_NAME(node), HASHINIT) & hmask;
  999                         LIST_INSERT_HEAD(&new[hash], node, nd_nodes);
 1000                 }
 1001 
 1002         hashdestroy(V_ng_name_hash, M_NETGRAPH_NODE, V_ng_name_hmask);
 1003         V_ng_name_hash = new;
 1004         V_ng_name_hmask = hmask;
 1005 }
 1006 
 1007 /*
 1008  * Allocate a bigger ID hash.
 1009  */
 1010 static void
 1011 ng_ID_rehash(void)
 1012 {
 1013         struct nodehash *new;
 1014         uint32_t hash;
 1015         u_long hmask;
 1016         node_p node, node2;
 1017         int i;
 1018 
 1019         new = hashinit_flags((V_ng_ID_hmask + 1) * 2, M_NETGRAPH_NODE, &hmask,
 1020             HASH_NOWAIT);
 1021         if (new == NULL)
 1022                 return;
 1023 
 1024         for (i = 0; i <= V_ng_ID_hmask; i++)
 1025                 LIST_FOREACH_SAFE(node, &V_ng_ID_hash[i], nd_idnodes, node2) {
 1026 #ifdef INVARIANTS
 1027                         LIST_REMOVE(node, nd_idnodes);
 1028 #endif
 1029                         hash = (node->nd_ID % (hmask + 1));
 1030                         LIST_INSERT_HEAD(&new[hash], node, nd_idnodes);
 1031                 }
 1032 
 1033         hashdestroy(V_ng_ID_hash, M_NETGRAPH_NODE, V_ng_name_hmask);
 1034         V_ng_ID_hash = new;
 1035         V_ng_ID_hmask = hmask;
 1036 }
 1037 
 1038 /************************************************************************
 1039                         Hook routines
 1040  Names are not optional. Hooks are always connected, except for a
 1041  brief moment within these routines. On invalidation or during creation
 1042  they are connected to the 'dead' hook.
 1043 ************************************************************************/
 1044 
 1045 /*
 1046  * Remove a hook reference
 1047  */
 1048 void
 1049 ng_unref_hook(hook_p hook)
 1050 {
 1051 
 1052         if (hook == &ng_deadhook)
 1053                 return;
 1054 
 1055         if (refcount_release(&hook->hk_refs)) { /* we were the last */
 1056                 if (_NG_HOOK_NODE(hook)) /* it'll probably be ng_deadnode */
 1057                         _NG_NODE_UNREF((_NG_HOOK_NODE(hook)));
 1058                 NG_FREE_HOOK(hook);
 1059         }
 1060 }
 1061 
 1062 /*
 1063  * Add an unconnected hook to a node. Only used internally.
 1064  * Assumes node is locked. (XXX not yet true )
 1065  */
 1066 static int
 1067 ng_add_hook(node_p node, const char *name, hook_p *hookp)
 1068 {
 1069         hook_p hook;
 1070         int error = 0;
 1071 
 1072         /* Check that the given name is good */
 1073         if (name == NULL) {
 1074                 TRAP_ERROR();
 1075                 return (EINVAL);
 1076         }
 1077         if (ng_findhook(node, name) != NULL) {
 1078                 TRAP_ERROR();
 1079                 return (EEXIST);
 1080         }
 1081 
 1082         /* Allocate the hook and link it up */
 1083         NG_ALLOC_HOOK(hook);
 1084         if (hook == NULL) {
 1085                 TRAP_ERROR();
 1086                 return (ENOMEM);
 1087         }
 1088         hook->hk_refs = 1;              /* add a reference for us to return */
 1089         hook->hk_flags = HK_INVALID;
 1090         hook->hk_peer = &ng_deadhook;   /* start off this way */
 1091         hook->hk_node = node;
 1092         NG_NODE_REF(node);              /* each hook counts as a reference */
 1093 
 1094         /* Set hook name */
 1095         strlcpy(NG_HOOK_NAME(hook), name, NG_HOOKSIZ);
 1096 
 1097         /*
 1098          * Check if the node type code has something to say about it
 1099          * If it fails, the unref of the hook will also unref the node.
 1100          */
 1101         if (node->nd_type->newhook != NULL) {
 1102                 if ((error = (*node->nd_type->newhook)(node, hook, name))) {
 1103                         NG_HOOK_UNREF(hook);    /* this frees the hook */
 1104                         return (error);
 1105                 }
 1106         }
 1107         /*
 1108          * The 'type' agrees so far, so go ahead and link it in.
 1109          * We'll ask again later when we actually connect the hooks.
 1110          */
 1111         LIST_INSERT_HEAD(&node->nd_hooks, hook, hk_hooks);
 1112         node->nd_numhooks++;
 1113         NG_HOOK_REF(hook);      /* one for the node */
 1114 
 1115         if (hookp)
 1116                 *hookp = hook;
 1117         return (0);
 1118 }
 1119 
 1120 /*
 1121  * Find a hook
 1122  *
 1123  * Node types may supply their own optimized routines for finding
 1124  * hooks.  If none is supplied, we just do a linear search.
 1125  * XXX Possibly we should add a reference to the hook?
 1126  */
 1127 hook_p
 1128 ng_findhook(node_p node, const char *name)
 1129 {
 1130         hook_p hook;
 1131 
 1132         if (node->nd_type->findhook != NULL)
 1133                 return (*node->nd_type->findhook)(node, name);
 1134         LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
 1135                 if (NG_HOOK_IS_VALID(hook) &&
 1136                     (strcmp(NG_HOOK_NAME(hook), name) == 0))
 1137                         return (hook);
 1138         }
 1139         return (NULL);
 1140 }
 1141 
 1142 /*
 1143  * Destroy a hook
 1144  *
 1145  * As hooks are always attached, this really destroys two hooks.
 1146  * The one given, and the one attached to it. Disconnect the hooks
 1147  * from each other first. We reconnect the peer hook to the 'dead'
 1148  * hook so that it can still exist after we depart. We then
 1149  * send the peer its own destroy message. This ensures that we only
 1150  * interact with the peer's structures when it is locked processing that
 1151  * message. We hold a reference to the peer hook so we are guaranteed that
 1152  * the peer hook and node are still going to exist until
 1153  * we are finished there as the hook holds a ref on the node.
 1154  * We run this same code again on the peer hook, but that time it is already
 1155  * attached to the 'dead' hook.
 1156  *
 1157  * This routine is called at all stages of hook creation
 1158  * on error detection and must be able to handle any such stage.
 1159  */
 1160 void
 1161 ng_destroy_hook(hook_p hook)
 1162 {
 1163         hook_p peer;
 1164         node_p node;
 1165 
 1166         if (hook == &ng_deadhook) {     /* better safe than sorry */
 1167                 printf("ng_destroy_hook called on deadhook\n");
 1168                 return;
 1169         }
 1170 
 1171         /*
 1172          * Protect divorce process with mutex, to avoid races on
 1173          * simultaneous disconnect.
 1174          */
 1175         TOPOLOGY_WLOCK();
 1176 
 1177         hook->hk_flags |= HK_INVALID;
 1178 
 1179         peer = NG_HOOK_PEER(hook);
 1180         node = NG_HOOK_NODE(hook);
 1181 
 1182         if (peer && (peer != &ng_deadhook)) {
 1183                 /*
 1184                  * Set the peer to point to ng_deadhook
 1185                  * from this moment on we are effectively independent it.
 1186                  * send it an rmhook message of its own.
 1187                  */
 1188                 peer->hk_peer = &ng_deadhook;   /* They no longer know us */
 1189                 hook->hk_peer = &ng_deadhook;   /* Nor us, them */
 1190                 if (NG_HOOK_NODE(peer) == &ng_deadnode) {
 1191                         /*
 1192                          * If it's already divorced from a node,
 1193                          * just free it.
 1194                          */
 1195                         TOPOLOGY_WUNLOCK();
 1196                 } else {
 1197                         TOPOLOGY_WUNLOCK();
 1198                         ng_rmhook_self(peer);   /* Send it a surprise */
 1199                 }
 1200                 NG_HOOK_UNREF(peer);            /* account for peer link */
 1201                 NG_HOOK_UNREF(hook);            /* account for peer link */
 1202         } else
 1203                 TOPOLOGY_WUNLOCK();
 1204 
 1205         TOPOLOGY_NOTOWNED();
 1206 
 1207         /*
 1208          * Remove the hook from the node's list to avoid possible recursion
 1209          * in case the disconnection results in node shutdown.
 1210          */
 1211         if (node == &ng_deadnode) { /* happens if called from ng_con_nodes() */
 1212                 return;
 1213         }
 1214         LIST_REMOVE(hook, hk_hooks);
 1215         node->nd_numhooks--;
 1216         if (node->nd_type->disconnect) {
 1217                 /*
 1218                  * The type handler may elect to destroy the node so don't
 1219                  * trust its existence after this point. (except
 1220                  * that we still hold a reference on it. (which we
 1221                  * inherrited from the hook we are destroying)
 1222                  */
 1223                 (*node->nd_type->disconnect) (hook);
 1224         }
 1225 
 1226         /*
 1227          * Note that because we will point to ng_deadnode, the original node
 1228          * is not decremented automatically so we do that manually.
 1229          */
 1230         _NG_HOOK_NODE(hook) = &ng_deadnode;
 1231         NG_NODE_UNREF(node);    /* We no longer point to it so adjust count */
 1232         NG_HOOK_UNREF(hook);    /* Account for linkage (in list) to node */
 1233 }
 1234 
 1235 /*
 1236  * Take two hooks on a node and merge the connection so that the given node
 1237  * is effectively bypassed.
 1238  */
 1239 int
 1240 ng_bypass(hook_p hook1, hook_p hook2)
 1241 {
 1242         if (hook1->hk_node != hook2->hk_node) {
 1243                 TRAP_ERROR();
 1244                 return (EINVAL);
 1245         }
 1246         TOPOLOGY_WLOCK();
 1247         if (NG_HOOK_NOT_VALID(hook1) || NG_HOOK_NOT_VALID(hook2)) {
 1248                 TOPOLOGY_WUNLOCK();
 1249                 return (EINVAL);
 1250         }
 1251         hook1->hk_peer->hk_peer = hook2->hk_peer;
 1252         hook2->hk_peer->hk_peer = hook1->hk_peer;
 1253 
 1254         hook1->hk_peer = &ng_deadhook;
 1255         hook2->hk_peer = &ng_deadhook;
 1256         TOPOLOGY_WUNLOCK();
 1257 
 1258         NG_HOOK_UNREF(hook1);
 1259         NG_HOOK_UNREF(hook2);
 1260 
 1261         /* XXX If we ever cache methods on hooks update them as well */
 1262         ng_destroy_hook(hook1);
 1263         ng_destroy_hook(hook2);
 1264         return (0);
 1265 }
 1266 
 1267 /*
 1268  * Install a new netgraph type
 1269  */
 1270 int
 1271 ng_newtype(struct ng_type *tp)
 1272 {
 1273         const size_t namelen = strlen(tp->name);
 1274 
 1275         /* Check version and type name fields */
 1276         if ((tp->version != NG_ABI_VERSION) || (namelen == 0) ||
 1277             (namelen >= NG_TYPESIZ)) {
 1278                 TRAP_ERROR();
 1279                 if (tp->version != NG_ABI_VERSION) {
 1280                         printf("Netgraph: Node type rejected. ABI mismatch. "
 1281                             "Suggest recompile\n");
 1282                 }
 1283                 return (EINVAL);
 1284         }
 1285 
 1286         /* Check for name collision */
 1287         if (ng_findtype(tp->name) != NULL) {
 1288                 TRAP_ERROR();
 1289                 return (EEXIST);
 1290         }
 1291 
 1292         /* Link in new type */
 1293         TYPELIST_WLOCK();
 1294         LIST_INSERT_HEAD(&ng_typelist, tp, types);
 1295         tp->refs = 1;   /* first ref is linked list */
 1296         TYPELIST_WUNLOCK();
 1297         return (0);
 1298 }
 1299 
 1300 /*
 1301  * unlink a netgraph type
 1302  * If no examples exist
 1303  */
 1304 int
 1305 ng_rmtype(struct ng_type *tp)
 1306 {
 1307         /* Check for name collision */
 1308         if (tp->refs != 1) {
 1309                 TRAP_ERROR();
 1310                 return (EBUSY);
 1311         }
 1312 
 1313         /* Unlink type */
 1314         TYPELIST_WLOCK();
 1315         LIST_REMOVE(tp, types);
 1316         TYPELIST_WUNLOCK();
 1317         return (0);
 1318 }
 1319 
 1320 /*
 1321  * Look for a type of the name given
 1322  */
 1323 struct ng_type *
 1324 ng_findtype(const char *typename)
 1325 {
 1326         struct ng_type *type;
 1327 
 1328         TYPELIST_RLOCK();
 1329         LIST_FOREACH(type, &ng_typelist, types) {
 1330                 if (strcmp(type->name, typename) == 0)
 1331                         break;
 1332         }
 1333         TYPELIST_RUNLOCK();
 1334         return (type);
 1335 }
 1336 
 1337 /************************************************************************
 1338                         Composite routines
 1339 ************************************************************************/
 1340 /*
 1341  * Connect two nodes using the specified hooks, using queued functions.
 1342  */
 1343 static int
 1344 ng_con_part3(node_p node, item_p item, hook_p hook)
 1345 {
 1346         int     error = 0;
 1347 
 1348         /*
 1349          * When we run, we know that the node 'node' is locked for us.
 1350          * Our caller has a reference on the hook.
 1351          * Our caller has a reference on the node.
 1352          * (In this case our caller is ng_apply_item() ).
 1353          * The peer hook has a reference on the hook.
 1354          * We are all set up except for the final call to the node, and
 1355          * the clearing of the INVALID flag.
 1356          */
 1357         if (NG_HOOK_NODE(hook) == &ng_deadnode) {
 1358                 /*
 1359                  * The node must have been freed again since we last visited
 1360                  * here. ng_destry_hook() has this effect but nothing else does.
 1361                  * We should just release our references and
 1362                  * free anything we can think of.
 1363                  * Since we know it's been destroyed, and it's our caller
 1364                  * that holds the references, just return.
 1365                  */
 1366                 ERROUT(ENOENT);
 1367         }
 1368         if (hook->hk_node->nd_type->connect) {
 1369                 if ((error = (*hook->hk_node->nd_type->connect) (hook))) {
 1370                         ng_destroy_hook(hook);  /* also zaps peer */
 1371                         printf("failed in ng_con_part3()\n");
 1372                         ERROUT(error);
 1373                 }
 1374         }
 1375         /*
 1376          *  XXX this is wrong for SMP. Possibly we need
 1377          * to separate out 'create' and 'invalid' flags.
 1378          * should only set flags on hooks we have locked under our node.
 1379          */
 1380         hook->hk_flags &= ~HK_INVALID;
 1381 done:
 1382         NG_FREE_ITEM(item);
 1383         return (error);
 1384 }
 1385 
 1386 static int
 1387 ng_con_part2(node_p node, item_p item, hook_p hook)
 1388 {
 1389         hook_p  peer;
 1390         int     error = 0;
 1391 
 1392         /*
 1393          * When we run, we know that the node 'node' is locked for us.
 1394          * Our caller has a reference on the hook.
 1395          * Our caller has a reference on the node.
 1396          * (In this case our caller is ng_apply_item() ).
 1397          * The peer hook has a reference on the hook.
 1398          * our node pointer points to the 'dead' node.
 1399          * First check the hook name is unique.
 1400          * Should not happen because we checked before queueing this.
 1401          */
 1402         if (ng_findhook(node, NG_HOOK_NAME(hook)) != NULL) {
 1403                 TRAP_ERROR();
 1404                 ng_destroy_hook(hook); /* should destroy peer too */
 1405                 printf("failed in ng_con_part2()\n");
 1406                 ERROUT(EEXIST);
 1407         }
 1408         /*
 1409          * Check if the node type code has something to say about it
 1410          * If it fails, the unref of the hook will also unref the attached node,
 1411          * however since that node is 'ng_deadnode' this will do nothing.
 1412          * The peer hook will also be destroyed.
 1413          */
 1414         if (node->nd_type->newhook != NULL) {
 1415                 if ((error = (*node->nd_type->newhook)(node, hook,
 1416                     hook->hk_name))) {
 1417                         ng_destroy_hook(hook); /* should destroy peer too */
 1418                         printf("failed in ng_con_part2()\n");
 1419                         ERROUT(error);
 1420                 }
 1421         }
 1422 
 1423         /*
 1424          * The 'type' agrees so far, so go ahead and link it in.
 1425          * We'll ask again later when we actually connect the hooks.
 1426          */
 1427         hook->hk_node = node;           /* just overwrite ng_deadnode */
 1428         NG_NODE_REF(node);              /* each hook counts as a reference */
 1429         LIST_INSERT_HEAD(&node->nd_hooks, hook, hk_hooks);
 1430         node->nd_numhooks++;
 1431         NG_HOOK_REF(hook);      /* one for the node */
 1432         
 1433         /*
 1434          * We now have a symmetrical situation, where both hooks have been
 1435          * linked to their nodes, the newhook methods have been called
 1436          * And the references are all correct. The hooks are still marked
 1437          * as invalid, as we have not called the 'connect' methods
 1438          * yet.
 1439          * We can call the local one immediately as we have the
 1440          * node locked, but we need to queue the remote one.
 1441          */
 1442         if (hook->hk_node->nd_type->connect) {
 1443                 if ((error = (*hook->hk_node->nd_type->connect) (hook))) {
 1444                         ng_destroy_hook(hook);  /* also zaps peer */
 1445                         printf("failed in ng_con_part2(A)\n");
 1446                         ERROUT(error);
 1447                 }
 1448         }
 1449 
 1450         /*
 1451          * Acquire topo mutex to avoid race with ng_destroy_hook().
 1452          */
 1453         TOPOLOGY_RLOCK();
 1454         peer = hook->hk_peer;
 1455         if (peer == &ng_deadhook) {
 1456                 TOPOLOGY_RUNLOCK();
 1457                 printf("failed in ng_con_part2(B)\n");
 1458                 ng_destroy_hook(hook);
 1459                 ERROUT(ENOENT);
 1460         }
 1461         TOPOLOGY_RUNLOCK();
 1462 
 1463         if ((error = ng_send_fn2(peer->hk_node, peer, item, &ng_con_part3,
 1464             NULL, 0, NG_REUSE_ITEM))) {
 1465                 printf("failed in ng_con_part2(C)\n");
 1466                 ng_destroy_hook(hook);  /* also zaps peer */
 1467                 return (error);         /* item was consumed. */
 1468         }
 1469         hook->hk_flags &= ~HK_INVALID; /* need both to be able to work */
 1470         return (0);                     /* item was consumed. */
 1471 done:
 1472         NG_FREE_ITEM(item);
 1473         return (error);
 1474 }
 1475 
 1476 /*
 1477  * Connect this node with another node. We assume that this node is
 1478  * currently locked, as we are only called from an NGM_CONNECT message.
 1479  */
 1480 static int
 1481 ng_con_nodes(item_p item, node_p node, const char *name,
 1482     node_p node2, const char *name2)
 1483 {
 1484         int     error;
 1485         hook_p  hook;
 1486         hook_p  hook2;
 1487 
 1488         if (ng_findhook(node2, name2) != NULL) {
 1489                 return(EEXIST);
 1490         }
 1491         if ((error = ng_add_hook(node, name, &hook)))  /* gives us a ref */
 1492                 return (error);
 1493         /* Allocate the other hook and link it up */
 1494         NG_ALLOC_HOOK(hook2);
 1495         if (hook2 == NULL) {
 1496                 TRAP_ERROR();
 1497                 ng_destroy_hook(hook);  /* XXX check ref counts so far */
 1498                 NG_HOOK_UNREF(hook);    /* including our ref */
 1499                 return (ENOMEM);
 1500         }
 1501         hook2->hk_refs = 1;             /* start with a reference for us. */
 1502         hook2->hk_flags = HK_INVALID;
 1503         hook2->hk_peer = hook;          /* Link the two together */
 1504         hook->hk_peer = hook2;  
 1505         NG_HOOK_REF(hook);              /* Add a ref for the peer to each*/
 1506         NG_HOOK_REF(hook2);
 1507         hook2->hk_node = &ng_deadnode;
 1508         strlcpy(NG_HOOK_NAME(hook2), name2, NG_HOOKSIZ);
 1509 
 1510         /*
 1511          * Queue the function above.
 1512          * Procesing continues in that function in the lock context of
 1513          * the other node.
 1514          */
 1515         if ((error = ng_send_fn2(node2, hook2, item, &ng_con_part2, NULL, 0,
 1516             NG_NOFLAGS))) {
 1517                 printf("failed in ng_con_nodes(): %d\n", error);
 1518                 ng_destroy_hook(hook);  /* also zaps peer */
 1519         }
 1520 
 1521         NG_HOOK_UNREF(hook);            /* Let each hook go if it wants to */
 1522         NG_HOOK_UNREF(hook2);
 1523         return (error);
 1524 }
 1525 
 1526 /*
 1527  * Make a peer and connect.
 1528  * We assume that the local node is locked.
 1529  * The new node probably doesn't need a lock until
 1530  * it has a hook, because it cannot really have any work until then,
 1531  * but we should think about it a bit more.
 1532  *
 1533  * The problem may come if the other node also fires up
 1534  * some hardware or a timer or some other source of activation,
 1535  * also it may already get a command msg via it's ID.
 1536  *
 1537  * We could use the same method as ng_con_nodes() but we'd have
 1538  * to add ability to remove the node when failing. (Not hard, just
 1539  * make arg1 point to the node to remove).
 1540  * Unless of course we just ignore failure to connect and leave
 1541  * an unconnected node?
 1542  */
 1543 static int
 1544 ng_mkpeer(node_p node, const char *name, const char *name2, char *type)
 1545 {
 1546         node_p  node2;
 1547         hook_p  hook1, hook2;
 1548         int     error;
 1549 
 1550         if ((error = ng_make_node(type, &node2))) {
 1551                 return (error);
 1552         }
 1553 
 1554         if ((error = ng_add_hook(node, name, &hook1))) { /* gives us a ref */
 1555                 ng_rmnode(node2, NULL, NULL, 0);
 1556                 return (error);
 1557         }
 1558 
 1559         if ((error = ng_add_hook(node2, name2, &hook2))) {
 1560                 ng_rmnode(node2, NULL, NULL, 0);
 1561                 ng_destroy_hook(hook1);
 1562                 NG_HOOK_UNREF(hook1);
 1563                 return (error);
 1564         }
 1565 
 1566         /*
 1567          * Actually link the two hooks together.
 1568          */
 1569         hook1->hk_peer = hook2;
 1570         hook2->hk_peer = hook1;
 1571 
 1572         /* Each hook is referenced by the other */
 1573         NG_HOOK_REF(hook1);
 1574         NG_HOOK_REF(hook2);
 1575 
 1576         /* Give each node the opportunity to veto the pending connection */
 1577         if (hook1->hk_node->nd_type->connect) {
 1578                 error = (*hook1->hk_node->nd_type->connect) (hook1);
 1579         }
 1580 
 1581         if ((error == 0) && hook2->hk_node->nd_type->connect) {
 1582                 error = (*hook2->hk_node->nd_type->connect) (hook2);
 1583 
 1584         }
 1585 
 1586         /*
 1587          * drop the references we were holding on the two hooks.
 1588          */
 1589         if (error) {
 1590                 ng_destroy_hook(hook2); /* also zaps hook1 */
 1591                 ng_rmnode(node2, NULL, NULL, 0);
 1592         } else {
 1593                 /* As a last act, allow the hooks to be used */
 1594                 hook1->hk_flags &= ~HK_INVALID;
 1595                 hook2->hk_flags &= ~HK_INVALID;
 1596         }
 1597         NG_HOOK_UNREF(hook1);
 1598         NG_HOOK_UNREF(hook2);
 1599         return (error);
 1600 }
 1601 
 1602 /************************************************************************
 1603                 Utility routines to send self messages
 1604 ************************************************************************/
 1605         
 1606 /* Shut this node down as soon as everyone is clear of it */
 1607 /* Should add arg "immediately" to jump the queue */
 1608 int
 1609 ng_rmnode_self(node_p node)
 1610 {
 1611         int             error;
 1612 
 1613         if (node == &ng_deadnode)
 1614                 return (0);
 1615         node->nd_flags |= NGF_INVALID;
 1616         if (node->nd_flags & NGF_CLOSING)
 1617                 return (0);
 1618 
 1619         error = ng_send_fn(node, NULL, &ng_rmnode, NULL, 0);
 1620         return (error);
 1621 }
 1622 
 1623 static void
 1624 ng_rmhook_part2(node_p node, hook_p hook, void *arg1, int arg2)
 1625 {
 1626         ng_destroy_hook(hook);
 1627         return ;
 1628 }
 1629 
 1630 int
 1631 ng_rmhook_self(hook_p hook)
 1632 {
 1633         int             error;
 1634         node_p node = NG_HOOK_NODE(hook);
 1635 
 1636         if (node == &ng_deadnode)
 1637                 return (0);
 1638 
 1639         error = ng_send_fn(node, hook, &ng_rmhook_part2, NULL, 0);
 1640         return (error);
 1641 }
 1642 
 1643 /***********************************************************************
 1644  * Parse and verify a string of the form:  <NODE:><PATH>
 1645  *
 1646  * Such a string can refer to a specific node or a specific hook
 1647  * on a specific node, depending on how you look at it. In the
 1648  * latter case, the PATH component must not end in a dot.
 1649  *
 1650  * Both <NODE:> and <PATH> are optional. The <PATH> is a string
 1651  * of hook names separated by dots. This breaks out the original
 1652  * string, setting *nodep to "NODE" (or NULL if none) and *pathp
 1653  * to "PATH" (or NULL if degenerate). Also, *hookp will point to
 1654  * the final hook component of <PATH>, if any, otherwise NULL.
 1655  *
 1656  * This returns -1 if the path is malformed. The char ** are optional.
 1657  ***********************************************************************/
 1658 int
 1659 ng_path_parse(char *addr, char **nodep, char **pathp, char **hookp)
 1660 {
 1661         char    *node, *path, *hook;
 1662         int     k;
 1663 
 1664         /*
 1665          * Extract absolute NODE, if any
 1666          */
 1667         for (path = addr; *path && *path != ':'; path++);
 1668         if (*path) {
 1669                 node = addr;    /* Here's the NODE */
 1670                 *path++ = '\0'; /* Here's the PATH */
 1671 
 1672                 /* Node name must not be empty */
 1673                 if (!*node)
 1674                         return -1;
 1675 
 1676                 /* A name of "." is OK; otherwise '.' not allowed */
 1677                 if (strcmp(node, ".") != 0) {
 1678                         for (k = 0; node[k]; k++)
 1679                                 if (node[k] == '.')
 1680                                         return -1;
 1681                 }
 1682         } else {
 1683                 node = NULL;    /* No absolute NODE */
 1684                 path = addr;    /* Here's the PATH */
 1685         }
 1686 
 1687         /* Snoop for illegal characters in PATH */
 1688         for (k = 0; path[k]; k++)
 1689                 if (path[k] == ':')
 1690                         return -1;
 1691 
 1692         /* Check for no repeated dots in PATH */
 1693         for (k = 0; path[k]; k++)
 1694                 if (path[k] == '.' && path[k + 1] == '.')
 1695                         return -1;
 1696 
 1697         /* Remove extra (degenerate) dots from beginning or end of PATH */
 1698         if (path[0] == '.')
 1699                 path++;
 1700         if (*path && path[strlen(path) - 1] == '.')
 1701                 path[strlen(path) - 1] = 0;
 1702 
 1703         /* If PATH has a dot, then we're not talking about a hook */
 1704         if (*path) {
 1705                 for (hook = path, k = 0; path[k]; k++)
 1706                         if (path[k] == '.') {
 1707                                 hook = NULL;
 1708                                 break;
 1709                         }
 1710         } else
 1711                 path = hook = NULL;
 1712 
 1713         /* Done */
 1714         if (nodep)
 1715                 *nodep = node;
 1716         if (pathp)
 1717                 *pathp = path;
 1718         if (hookp)
 1719                 *hookp = hook;
 1720         return (0);
 1721 }
 1722 
 1723 /*
 1724  * Given a path, which may be absolute or relative, and a starting node,
 1725  * return the destination node.
 1726  */
 1727 int
 1728 ng_path2noderef(node_p here, const char *address, node_p *destp,
 1729     hook_p *lasthook)
 1730 {
 1731         char    fullpath[NG_PATHSIZ];
 1732         char   *nodename, *path;
 1733         node_p  node, oldnode;
 1734 
 1735         /* Initialize */
 1736         if (destp == NULL) {
 1737                 TRAP_ERROR();
 1738                 return EINVAL;
 1739         }
 1740         *destp = NULL;
 1741 
 1742         /* Make a writable copy of address for ng_path_parse() */
 1743         strncpy(fullpath, address, sizeof(fullpath) - 1);
 1744         fullpath[sizeof(fullpath) - 1] = '\0';
 1745 
 1746         /* Parse out node and sequence of hooks */
 1747         if (ng_path_parse(fullpath, &nodename, &path, NULL) < 0) {
 1748                 TRAP_ERROR();
 1749                 return EINVAL;
 1750         }
 1751 
 1752         /*
 1753          * For an absolute address, jump to the starting node.
 1754          * Note that this holds a reference on the node for us.
 1755          * Don't forget to drop the reference if we don't need it.
 1756          */
 1757         if (nodename) {
 1758                 node = ng_name2noderef(here, nodename);
 1759                 if (node == NULL) {
 1760                         TRAP_ERROR();
 1761                         return (ENOENT);
 1762                 }
 1763         } else {
 1764                 if (here == NULL) {
 1765                         TRAP_ERROR();
 1766                         return (EINVAL);
 1767                 }
 1768                 node = here;
 1769                 NG_NODE_REF(node);
 1770         }
 1771 
 1772         if (path == NULL) {
 1773                 if (lasthook != NULL)
 1774                         *lasthook = NULL;
 1775                 *destp = node;
 1776                 return (0);
 1777         }
 1778 
 1779         /*
 1780          * Now follow the sequence of hooks
 1781          *
 1782          * XXXGL: The path may demolish as we go the sequence, but if
 1783          * we hold the topology mutex at critical places, then, I hope,
 1784          * we would always have valid pointers in hand, although the
 1785          * path behind us may no longer exist.
 1786          */
 1787         for (;;) {
 1788                 hook_p hook;
 1789                 char *segment;
 1790 
 1791                 /*
 1792                  * Break out the next path segment. Replace the dot we just
 1793                  * found with a NUL; "path" points to the next segment (or the
 1794                  * NUL at the end).
 1795                  */
 1796                 for (segment = path; *path != '\0'; path++) {
 1797                         if (*path == '.') {
 1798                                 *path++ = '\0';
 1799                                 break;
 1800                         }
 1801                 }
 1802 
 1803                 /* We have a segment, so look for a hook by that name */
 1804                 hook = ng_findhook(node, segment);
 1805 
 1806                 TOPOLOGY_WLOCK();
 1807                 /* Can't get there from here... */
 1808                 if (hook == NULL || NG_HOOK_PEER(hook) == NULL ||
 1809                     NG_HOOK_NOT_VALID(hook) ||
 1810                     NG_HOOK_NOT_VALID(NG_HOOK_PEER(hook))) {
 1811                         TRAP_ERROR();
 1812                         NG_NODE_UNREF(node);
 1813                         TOPOLOGY_WUNLOCK();
 1814                         return (ENOENT);
 1815                 }
 1816 
 1817                 /*
 1818                  * Hop on over to the next node
 1819                  * XXX
 1820                  * Big race conditions here as hooks and nodes go away
 1821                  * *** Idea.. store an ng_ID_t in each hook and use that
 1822                  * instead of the direct hook in this crawl?
 1823                  */
 1824                 oldnode = node;
 1825                 if ((node = NG_PEER_NODE(hook)))
 1826                         NG_NODE_REF(node);      /* XXX RACE */
 1827                 NG_NODE_UNREF(oldnode); /* XXX another race */
 1828                 if (NG_NODE_NOT_VALID(node)) {
 1829                         NG_NODE_UNREF(node);    /* XXX more races */
 1830                         TOPOLOGY_WUNLOCK();
 1831                         TRAP_ERROR();
 1832                         return (ENXIO);
 1833                 }
 1834 
 1835                 if (*path == '\0') {
 1836                         if (lasthook != NULL) {
 1837                                 if (hook != NULL) {
 1838                                         *lasthook = NG_HOOK_PEER(hook);
 1839                                         NG_HOOK_REF(*lasthook);
 1840                                 } else
 1841                                         *lasthook = NULL;
 1842                         }
 1843                         TOPOLOGY_WUNLOCK();
 1844                         *destp = node;
 1845                         return (0);
 1846                 }
 1847                 TOPOLOGY_WUNLOCK();
 1848         }
 1849 }
 1850 
 1851 /***************************************************************\
 1852 * Input queue handling.
 1853 * All activities are submitted to the node via the input queue
 1854 * which implements a multiple-reader/single-writer gate.
 1855 * Items which cannot be handled immediately are queued.
 1856 *
 1857 * read-write queue locking inline functions                     *
 1858 \***************************************************************/
 1859 
 1860 static __inline void    ng_queue_rw(node_p node, item_p  item, int rw);
 1861 static __inline item_p  ng_dequeue(node_p node, int *rw);
 1862 static __inline item_p  ng_acquire_read(node_p node, item_p  item);
 1863 static __inline item_p  ng_acquire_write(node_p node, item_p  item);
 1864 static __inline void    ng_leave_read(node_p node);
 1865 static __inline void    ng_leave_write(node_p node);
 1866 
 1867 /*
 1868  * Definition of the bits fields in the ng_queue flag word.
 1869  * Defined here rather than in netgraph.h because no-one should fiddle
 1870  * with them.
 1871  *
 1872  * The ordering here may be important! don't shuffle these.
 1873  */
 1874 /*-
 1875  Safety Barrier--------+ (adjustable to suit taste) (not used yet)
 1876                        |
 1877                        V
 1878 +-------+-------+-------+-------+-------+-------+-------+-------+
 1879   | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
 1880   | |A|c|t|i|v|e| |R|e|a|d|e|r| |C|o|u|n|t| | | | | | | | | |P|A|
 1881   | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |O|W|
 1882 +-------+-------+-------+-------+-------+-------+-------+-------+
 1883   \___________________________ ____________________________/ | |
 1884                             V                                | |
 1885                   [active reader count]                      | |
 1886                                                              | |
 1887             Operation Pending -------------------------------+ |
 1888                                                                |
 1889           Active Writer ---------------------------------------+
 1890 
 1891 Node queue has such semantics:
 1892 - All flags modifications are atomic.
 1893 - Reader count can be incremented only if there is no writer or pending flags.
 1894   As soon as this can't be done with single operation, it is implemented with
 1895   spin loop and atomic_cmpset().
 1896 - Writer flag can be set only if there is no any bits set.
 1897   It is implemented with atomic_cmpset().
 1898 - Pending flag can be set any time, but to avoid collision on queue processing
 1899   all queue fields are protected by the mutex.
 1900 - Queue processing thread reads queue holding the mutex, but releases it while
 1901   processing. When queue is empty pending flag is removed.
 1902 */
 1903 
 1904 #define WRITER_ACTIVE   0x00000001
 1905 #define OP_PENDING      0x00000002
 1906 #define READER_INCREMENT 0x00000004
 1907 #define READER_MASK     0xfffffffc      /* Not valid if WRITER_ACTIVE is set */
 1908 #define SAFETY_BARRIER  0x00100000      /* 128K items queued should be enough */
 1909 
 1910 /* Defines of more elaborate states on the queue */
 1911 /* Mask of bits a new read cares about */
 1912 #define NGQ_RMASK       (WRITER_ACTIVE|OP_PENDING)
 1913 
 1914 /* Mask of bits a new write cares about */
 1915 #define NGQ_WMASK       (NGQ_RMASK|READER_MASK)
 1916 
 1917 /* Test to decide if there is something on the queue. */
 1918 #define QUEUE_ACTIVE(QP) ((QP)->q_flags & OP_PENDING)
 1919 
 1920 /* How to decide what the next queued item is. */
 1921 #define HEAD_IS_READER(QP)  NGI_QUEUED_READER(STAILQ_FIRST(&(QP)->queue))
 1922 #define HEAD_IS_WRITER(QP)  NGI_QUEUED_WRITER(STAILQ_FIRST(&(QP)->queue)) /* notused */
 1923 
 1924 /* Read the status to decide if the next item on the queue can now run. */
 1925 #define QUEUED_READER_CAN_PROCEED(QP)                   \
 1926                 (((QP)->q_flags & (NGQ_RMASK & ~OP_PENDING)) == 0)
 1927 #define QUEUED_WRITER_CAN_PROCEED(QP)                   \
 1928                 (((QP)->q_flags & (NGQ_WMASK & ~OP_PENDING)) == 0)
 1929 
 1930 /* Is there a chance of getting ANY work off the queue? */
 1931 #define NEXT_QUEUED_ITEM_CAN_PROCEED(QP)                                \
 1932         ((HEAD_IS_READER(QP)) ? QUEUED_READER_CAN_PROCEED(QP) :         \
 1933                                 QUEUED_WRITER_CAN_PROCEED(QP))
 1934 
 1935 #define NGQRW_R 0
 1936 #define NGQRW_W 1
 1937 
 1938 #define NGQ2_WORKQ      0x00000001
 1939 
 1940 /*
 1941  * Taking into account the current state of the queue and node, possibly take
 1942  * the next entry off the queue and return it. Return NULL if there was
 1943  * nothing we could return, either because there really was nothing there, or
 1944  * because the node was in a state where it cannot yet process the next item
 1945  * on the queue.
 1946  */
 1947 static __inline item_p
 1948 ng_dequeue(node_p node, int *rw)
 1949 {
 1950         item_p item;
 1951         struct ng_queue *ngq = &node->nd_input_queue;
 1952 
 1953         /* This MUST be called with the mutex held. */
 1954         mtx_assert(&ngq->q_mtx, MA_OWNED);
 1955 
 1956         /* If there is nothing queued, then just return. */
 1957         if (!QUEUE_ACTIVE(ngq)) {
 1958                 CTR4(KTR_NET, "%20s: node [%x] (%p) queue empty; "
 1959                     "queue flags 0x%lx", __func__,
 1960                     node->nd_ID, node, ngq->q_flags);
 1961                 return (NULL);
 1962         }
 1963 
 1964         /*
 1965          * From here, we can assume there is a head item.
 1966          * We need to find out what it is and if it can be dequeued, given
 1967          * the current state of the node.
 1968          */
 1969         if (HEAD_IS_READER(ngq)) {
 1970                 while (1) {
 1971                         long t = ngq->q_flags;
 1972                         if (t & WRITER_ACTIVE) {
 1973                                 /* There is writer, reader can't proceed. */
 1974                                 CTR4(KTR_NET, "%20s: node [%x] (%p) queued "
 1975                                     "reader can't proceed; queue flags 0x%lx",
 1976                                     __func__, node->nd_ID, node, t);
 1977                                 return (NULL);
 1978                         }
 1979                         if (atomic_cmpset_acq_int(&ngq->q_flags, t,
 1980                             t + READER_INCREMENT))
 1981                                 break;
 1982                         cpu_spinwait();
 1983                 }
 1984                 /* We have got reader lock for the node. */
 1985                 *rw = NGQRW_R;
 1986         } else if (atomic_cmpset_acq_int(&ngq->q_flags, OP_PENDING,
 1987             OP_PENDING + WRITER_ACTIVE)) {
 1988                 /* We have got writer lock for the node. */
 1989                 *rw = NGQRW_W;
 1990         } else {
 1991                 /* There is somebody other, writer can't proceed. */
 1992                 CTR4(KTR_NET, "%20s: node [%x] (%p) queued writer can't "
 1993                     "proceed; queue flags 0x%lx", __func__, node->nd_ID, node,
 1994                     ngq->q_flags);
 1995                 return (NULL);
 1996         }
 1997 
 1998         /*
 1999          * Now we dequeue the request (whatever it may be) and correct the
 2000          * pending flags and the next and last pointers.
 2001          */
 2002         item = STAILQ_FIRST(&ngq->queue);
 2003         STAILQ_REMOVE_HEAD(&ngq->queue, el_next);
 2004         if (STAILQ_EMPTY(&ngq->queue))
 2005                 atomic_clear_int(&ngq->q_flags, OP_PENDING);
 2006         CTR6(KTR_NET, "%20s: node [%x] (%p) returning item %p as %s; queue "
 2007             "flags 0x%lx", __func__, node->nd_ID, node, item, *rw ? "WRITER" :
 2008             "READER", ngq->q_flags);
 2009         return (item);
 2010 }
 2011 
 2012 /*
 2013  * Queue a packet to be picked up later by someone else.
 2014  * If the queue could be run now, add node to the queue handler's worklist.
 2015  */
 2016 static __inline void
 2017 ng_queue_rw(node_p node, item_p  item, int rw)
 2018 {
 2019         struct ng_queue *ngq = &node->nd_input_queue;
 2020         if (rw == NGQRW_W)
 2021                 NGI_SET_WRITER(item);
 2022         else
 2023                 NGI_SET_READER(item);
 2024         item->depth = 1;
 2025 
 2026         NG_QUEUE_LOCK(ngq);
 2027         /* Set OP_PENDING flag and enqueue the item. */
 2028         atomic_set_int(&ngq->q_flags, OP_PENDING);
 2029         STAILQ_INSERT_TAIL(&ngq->queue, item, el_next);
 2030 
 2031         CTR5(KTR_NET, "%20s: node [%x] (%p) queued item %p as %s", __func__,
 2032             node->nd_ID, node, item, rw ? "WRITER" : "READER" );
 2033 
 2034         /*
 2035          * We can take the worklist lock with the node locked
 2036          * BUT NOT THE REVERSE!
 2037          */
 2038         if (NEXT_QUEUED_ITEM_CAN_PROCEED(ngq))
 2039                 ng_worklist_add(node);
 2040         NG_QUEUE_UNLOCK(ngq);
 2041 }
 2042 
 2043 /* Acquire reader lock on node. If node is busy, queue the packet. */
 2044 static __inline item_p
 2045 ng_acquire_read(node_p node, item_p item)
 2046 {
 2047         KASSERT(node != &ng_deadnode,
 2048             ("%s: working on deadnode", __func__));
 2049 
 2050         /* Reader needs node without writer and pending items. */
 2051         for (;;) {
 2052                 long t = node->nd_input_queue.q_flags;
 2053                 if (t & NGQ_RMASK)
 2054                         break; /* Node is not ready for reader. */
 2055                 if (atomic_cmpset_acq_int(&node->nd_input_queue.q_flags, t,
 2056                     t + READER_INCREMENT)) {
 2057                         /* Successfully grabbed node */
 2058                         CTR4(KTR_NET, "%20s: node [%x] (%p) acquired item %p",
 2059                             __func__, node->nd_ID, node, item);
 2060                         return (item);
 2061                 }
 2062                 cpu_spinwait();
 2063         }
 2064 
 2065         /* Queue the request for later. */
 2066         ng_queue_rw(node, item, NGQRW_R);
 2067 
 2068         return (NULL);
 2069 }
 2070 
 2071 /* Acquire writer lock on node. If node is busy, queue the packet. */
 2072 static __inline item_p
 2073 ng_acquire_write(node_p node, item_p item)
 2074 {
 2075         KASSERT(node != &ng_deadnode,
 2076             ("%s: working on deadnode", __func__));
 2077 
 2078         /* Writer needs completely idle node. */
 2079         if (atomic_cmpset_acq_int(&node->nd_input_queue.q_flags, 0,
 2080             WRITER_ACTIVE)) {
 2081                 /* Successfully grabbed node */
 2082                 CTR4(KTR_NET, "%20s: node [%x] (%p) acquired item %p",
 2083                     __func__, node->nd_ID, node, item);
 2084                 return (item);
 2085         }
 2086 
 2087         /* Queue the request for later. */
 2088         ng_queue_rw(node, item, NGQRW_W);
 2089 
 2090         return (NULL);
 2091 }
 2092 
 2093 #if 0
 2094 static __inline item_p
 2095 ng_upgrade_write(node_p node, item_p item)
 2096 {
 2097         struct ng_queue *ngq = &node->nd_input_queue;
 2098         KASSERT(node != &ng_deadnode,
 2099             ("%s: working on deadnode", __func__));
 2100 
 2101         NGI_SET_WRITER(item);
 2102 
 2103         NG_QUEUE_LOCK(ngq);
 2104 
 2105         /*
 2106          * There will never be no readers as we are there ourselves.
 2107          * Set the WRITER_ACTIVE flags ASAP to block out fast track readers.
 2108          * The caller we are running from will call ng_leave_read()
 2109          * soon, so we must account for that. We must leave again with the
 2110          * READER lock. If we find other readers, then
 2111          * queue the request for later. However "later" may be rignt now
 2112          * if there are no readers. We don't really care if there are queued
 2113          * items as we will bypass them anyhow.
 2114          */
 2115         atomic_add_int(&ngq->q_flags, WRITER_ACTIVE - READER_INCREMENT);
 2116         if ((ngq->q_flags & (NGQ_WMASK & ~OP_PENDING)) == WRITER_ACTIVE) {
 2117                 NG_QUEUE_UNLOCK(ngq);
 2118                 
 2119                 /* It's just us, act on the item. */
 2120                 /* will NOT drop writer lock when done */
 2121                 ng_apply_item(node, item, 0);
 2122 
 2123                 /*
 2124                  * Having acted on the item, atomically
 2125                  * downgrade back to READER and finish up.
 2126                  */
 2127                 atomic_add_int(&ngq->q_flags, READER_INCREMENT - WRITER_ACTIVE);
 2128 
 2129                 /* Our caller will call ng_leave_read() */
 2130                 return;
 2131         }
 2132         /*
 2133          * It's not just us active, so queue us AT THE HEAD.
 2134          * "Why?" I hear you ask.
 2135          * Put us at the head of the queue as we've already been
 2136          * through it once. If there is nothing else waiting,
 2137          * set the correct flags.
 2138          */
 2139         if (STAILQ_EMPTY(&ngq->queue)) {
 2140                 /* We've gone from, 0 to 1 item in the queue */
 2141                 atomic_set_int(&ngq->q_flags, OP_PENDING);
 2142 
 2143                 CTR3(KTR_NET, "%20s: node [%x] (%p) set OP_PENDING", __func__,
 2144                     node->nd_ID, node);
 2145         };
 2146         STAILQ_INSERT_HEAD(&ngq->queue, item, el_next);
 2147         CTR4(KTR_NET, "%20s: node [%x] (%p) requeued item %p as WRITER",
 2148             __func__, node->nd_ID, node, item );
 2149 
 2150         /* Reverse what we did above. That downgrades us back to reader */
 2151         atomic_add_int(&ngq->q_flags, READER_INCREMENT - WRITER_ACTIVE);
 2152         if (QUEUE_ACTIVE(ngq) && NEXT_QUEUED_ITEM_CAN_PROCEED(ngq))
 2153                 ng_worklist_add(node);
 2154         NG_QUEUE_UNLOCK(ngq);
 2155 
 2156         return;
 2157 }
 2158 #endif
 2159 
 2160 /* Release reader lock. */
 2161 static __inline void
 2162 ng_leave_read(node_p node)
 2163 {
 2164         atomic_subtract_rel_int(&node->nd_input_queue.q_flags, READER_INCREMENT);
 2165 }
 2166 
 2167 /* Release writer lock. */
 2168 static __inline void
 2169 ng_leave_write(node_p node)
 2170 {
 2171         atomic_clear_rel_int(&node->nd_input_queue.q_flags, WRITER_ACTIVE);
 2172 }
 2173 
 2174 /* Purge node queue. Called on node shutdown. */
 2175 static void
 2176 ng_flush_input_queue(node_p node)
 2177 {
 2178         struct ng_queue *ngq = &node->nd_input_queue;
 2179         item_p item;
 2180 
 2181         NG_QUEUE_LOCK(ngq);
 2182         while ((item = STAILQ_FIRST(&ngq->queue)) != NULL) {
 2183                 STAILQ_REMOVE_HEAD(&ngq->queue, el_next);
 2184                 if (STAILQ_EMPTY(&ngq->queue))
 2185                         atomic_clear_int(&ngq->q_flags, OP_PENDING);
 2186                 NG_QUEUE_UNLOCK(ngq);
 2187 
 2188                 /* If the item is supplying a callback, call it with an error */
 2189                 if (item->apply != NULL) {
 2190                         if (item->depth == 1)
 2191                                 item->apply->error = ENOENT;
 2192                         if (refcount_release(&item->apply->refs)) {
 2193                                 (*item->apply->apply)(item->apply->context,
 2194                                     item->apply->error);
 2195                         }
 2196                 }
 2197                 NG_FREE_ITEM(item);
 2198                 NG_QUEUE_LOCK(ngq);
 2199         }
 2200         NG_QUEUE_UNLOCK(ngq);
 2201 }
 2202 
 2203 /***********************************************************************
 2204 * Externally visible method for sending or queueing messages or data.
 2205 ***********************************************************************/
 2206 
 2207 /*
 2208  * The module code should have filled out the item correctly by this stage:
 2209  * Common:
 2210  *    reference to destination node.
 2211  *    Reference to destination rcv hook if relevant.
 2212  *    apply pointer must be or NULL or reference valid struct ng_apply_info.
 2213  * Data:
 2214  *    pointer to mbuf
 2215  * Control_Message:
 2216  *    pointer to msg.
 2217  *    ID of original sender node. (return address)
 2218  * Function:
 2219  *    Function pointer
 2220  *    void * argument
 2221  *    integer argument
 2222  *
 2223  * The nodes have several routines and macros to help with this task:
 2224  */
 2225 
 2226 int
 2227 ng_snd_item(item_p item, int flags)
 2228 {
 2229         hook_p hook;
 2230         node_p node;
 2231         int queue, rw;
 2232         struct ng_queue *ngq;
 2233         int error = 0;
 2234 
 2235         /* We are sending item, so it must be present! */
 2236         KASSERT(item != NULL, ("ng_snd_item: item is NULL"));
 2237 
 2238 #ifdef  NETGRAPH_DEBUG
 2239         _ngi_check(item, __FILE__, __LINE__);
 2240 #endif
 2241 
 2242         /* Item was sent once more, postpone apply() call. */
 2243         if (item->apply)
 2244                 refcount_acquire(&item->apply->refs);
 2245 
 2246         node = NGI_NODE(item);
 2247         /* Node is never optional. */
 2248         KASSERT(node != NULL, ("ng_snd_item: node is NULL"));
 2249 
 2250         hook = NGI_HOOK(item);
 2251         /* Valid hook and mbuf are mandatory for data. */
 2252         if ((item->el_flags & NGQF_TYPE) == NGQF_DATA) {
 2253                 KASSERT(hook != NULL, ("ng_snd_item: hook for data is NULL"));
 2254                 if (NGI_M(item) == NULL)
 2255                         ERROUT(EINVAL);
 2256                 CHECK_DATA_MBUF(NGI_M(item));
 2257         }
 2258 
 2259         /*
 2260          * If the item or the node specifies single threading, force
 2261          * writer semantics. Similarly, the node may say one hook always
 2262          * produces writers. These are overrides.
 2263          */
 2264         if (((item->el_flags & NGQF_RW) == NGQF_WRITER) ||
 2265             (node->nd_flags & NGF_FORCE_WRITER) ||
 2266             (hook && (hook->hk_flags & HK_FORCE_WRITER))) {
 2267                 rw = NGQRW_W;
 2268         } else {
 2269                 rw = NGQRW_R;
 2270         }
 2271 
 2272         /*
 2273          * If sender or receiver requests queued delivery, or call graph
 2274          * loops back from outbound to inbound path, or stack usage
 2275          * level is dangerous - enqueue message.
 2276          */
 2277         if ((flags & NG_QUEUE) || (hook && (hook->hk_flags & HK_QUEUE))) {
 2278                 queue = 1;
 2279         } else if (hook && (hook->hk_flags & HK_TO_INBOUND) &&
 2280             curthread->td_ng_outbound) {
 2281                 queue = 1;
 2282         } else {
 2283                 queue = 0;
 2284 
 2285                 /*
 2286                  * Most of netgraph nodes have small stack consumption and
 2287                  * for them 25% of free stack space is more than enough.
 2288                  * Nodes/hooks with higher stack usage should be marked as
 2289                  * HI_STACK. For them 50% of stack will be guaranteed then.
 2290                  * XXX: Values 25% and 50% are completely empirical.
 2291                  */
 2292                 size_t  st, su, sl;
 2293                 GET_STACK_USAGE(st, su);
 2294                 sl = st - su;
 2295                 if ((sl * 4 < st) || ((sl * 2 < st) &&
 2296                     ((node->nd_flags & NGF_HI_STACK) || (hook &&
 2297                     (hook->hk_flags & HK_HI_STACK)))))
 2298                         queue = 1;
 2299         }
 2300 
 2301         if (queue) {
 2302                 /* Put it on the queue for that node*/
 2303                 ng_queue_rw(node, item, rw);
 2304                 return ((flags & NG_PROGRESS) ? EINPROGRESS : 0);
 2305         }
 2306 
 2307         /*
 2308          * We already decided how we will be queueud or treated.
 2309          * Try get the appropriate operating permission.
 2310          */
 2311         if (rw == NGQRW_R)
 2312                 item = ng_acquire_read(node, item);
 2313         else
 2314                 item = ng_acquire_write(node, item);
 2315 
 2316         /* Item was queued while trying to get permission. */
 2317         if (item == NULL)
 2318                 return ((flags & NG_PROGRESS) ? EINPROGRESS : 0);
 2319 
 2320         NGI_GET_NODE(item, node); /* zaps stored node */
 2321 
 2322         item->depth++;
 2323         error = ng_apply_item(node, item, rw); /* drops r/w lock when done */
 2324 
 2325         /* If something is waiting on queue and ready, schedule it. */
 2326         ngq = &node->nd_input_queue;
 2327         if (QUEUE_ACTIVE(ngq)) {
 2328                 NG_QUEUE_LOCK(ngq);
 2329                 if (QUEUE_ACTIVE(ngq) && NEXT_QUEUED_ITEM_CAN_PROCEED(ngq))
 2330                         ng_worklist_add(node);
 2331                 NG_QUEUE_UNLOCK(ngq);
 2332         }
 2333 
 2334         /*
 2335          * Node may go away as soon as we remove the reference.
 2336          * Whatever we do, DO NOT access the node again!
 2337          */
 2338         NG_NODE_UNREF(node);
 2339 
 2340         return (error);
 2341 
 2342 done:
 2343         /* If was not sent, apply callback here. */
 2344         if (item->apply != NULL) {
 2345                 if (item->depth == 0 && error != 0)
 2346                         item->apply->error = error;
 2347                 if (refcount_release(&item->apply->refs)) {
 2348                         (*item->apply->apply)(item->apply->context,
 2349                             item->apply->error);
 2350                 }
 2351         }
 2352 
 2353         NG_FREE_ITEM(item);
 2354         return (error);
 2355 }
 2356 
 2357 /*
 2358  * We have an item that was possibly queued somewhere.
 2359  * It should contain all the information needed
 2360  * to run it on the appropriate node/hook.
 2361  * If there is apply pointer and we own the last reference, call apply().
 2362  */
 2363 static int
 2364 ng_apply_item(node_p node, item_p item, int rw)
 2365 {
 2366         hook_p  hook;
 2367         ng_rcvdata_t *rcvdata;
 2368         ng_rcvmsg_t *rcvmsg;
 2369         struct ng_apply_info *apply;
 2370         int     error = 0, depth;
 2371 
 2372         /* Node and item are never optional. */
 2373         KASSERT(node != NULL, ("ng_apply_item: node is NULL"));
 2374         KASSERT(item != NULL, ("ng_apply_item: item is NULL"));
 2375 
 2376         NGI_GET_HOOK(item, hook); /* clears stored hook */
 2377 #ifdef  NETGRAPH_DEBUG
 2378         _ngi_check(item, __FILE__, __LINE__);
 2379 #endif
 2380 
 2381         apply = item->apply;
 2382         depth = item->depth;
 2383 
 2384         switch (item->el_flags & NGQF_TYPE) {
 2385         case NGQF_DATA:
 2386                 /*
 2387                  * Check things are still ok as when we were queued.
 2388                  */
 2389                 KASSERT(hook != NULL, ("ng_apply_item: hook for data is NULL"));
 2390                 if (NG_HOOK_NOT_VALID(hook) ||
 2391                     NG_NODE_NOT_VALID(node)) {
 2392                         error = EIO;
 2393                         NG_FREE_ITEM(item);
 2394                         break;
 2395                 }
 2396                 /*
 2397                  * If no receive method, just silently drop it.
 2398                  * Give preference to the hook over-ride method.
 2399                  */
 2400                 if ((!(rcvdata = hook->hk_rcvdata)) &&
 2401                     (!(rcvdata = NG_HOOK_NODE(hook)->nd_type->rcvdata))) {
 2402                         error = 0;
 2403                         NG_FREE_ITEM(item);
 2404                         break;
 2405                 }
 2406                 error = (*rcvdata)(hook, item);
 2407                 break;
 2408         case NGQF_MESG:
 2409                 if (hook && NG_HOOK_NOT_VALID(hook)) {
 2410                         /*
 2411                          * The hook has been zapped then we can't use it.
 2412                          * Immediately drop its reference.
 2413                          * The message may not need it.
 2414                          */
 2415                         NG_HOOK_UNREF(hook);
 2416                         hook = NULL;
 2417                 }
 2418                 /*
 2419                  * Similarly, if the node is a zombie there is
 2420                  * nothing we can do with it, drop everything.
 2421                  */
 2422                 if (NG_NODE_NOT_VALID(node)) {
 2423                         TRAP_ERROR();
 2424                         error = EINVAL;
 2425                         NG_FREE_ITEM(item);
 2426                         break;
 2427                 }
 2428                 /*
 2429                  * Call the appropriate message handler for the object.
 2430                  * It is up to the message handler to free the message.
 2431                  * If it's a generic message, handle it generically,
 2432                  * otherwise call the type's message handler (if it exists).
 2433                  * XXX (race). Remember that a queued message may
 2434                  * reference a node or hook that has just been
 2435                  * invalidated. It will exist as the queue code
 2436                  * is holding a reference, but..
 2437                  */
 2438                 if ((NGI_MSG(item)->header.typecookie == NGM_GENERIC_COOKIE) &&
 2439                     ((NGI_MSG(item)->header.flags & NGF_RESP) == 0)) {
 2440                         error = ng_generic_msg(node, item, hook);
 2441                         break;
 2442                 }
 2443                 if (((!hook) || (!(rcvmsg = hook->hk_rcvmsg))) &&
 2444                     (!(rcvmsg = node->nd_type->rcvmsg))) {
 2445                         TRAP_ERROR();
 2446                         error = 0;
 2447                         NG_FREE_ITEM(item);
 2448                         break;
 2449                 }
 2450                 error = (*rcvmsg)(node, item, hook);
 2451                 break;
 2452         case NGQF_FN:
 2453         case NGQF_FN2:
 2454                 /*
 2455                  * In the case of the shutdown message we allow it to hit
 2456                  * even if the node is invalid.
 2457                  */
 2458                 if (NG_NODE_NOT_VALID(node) &&
 2459                     NGI_FN(item) != &ng_rmnode) {
 2460                         TRAP_ERROR();
 2461                         error = EINVAL;
 2462                         NG_FREE_ITEM(item);
 2463                         break;
 2464                 }
 2465                 /* Same is about some internal functions and invalid hook. */
 2466                 if (hook && NG_HOOK_NOT_VALID(hook) &&
 2467                     NGI_FN2(item) != &ng_con_part2 &&
 2468                     NGI_FN2(item) != &ng_con_part3 &&
 2469                     NGI_FN(item) != &ng_rmhook_part2) {
 2470                         TRAP_ERROR();
 2471                         error = EINVAL;
 2472                         NG_FREE_ITEM(item);
 2473                         break;
 2474                 }
 2475                 
 2476                 if ((item->el_flags & NGQF_TYPE) == NGQF_FN) {
 2477                         (*NGI_FN(item))(node, hook, NGI_ARG1(item),
 2478                             NGI_ARG2(item));
 2479                         NG_FREE_ITEM(item);
 2480                 } else  /* it is NGQF_FN2 */
 2481                         error = (*NGI_FN2(item))(node, item, hook);
 2482                 break;
 2483         }
 2484         /*
 2485          * We held references on some of the resources
 2486          * that we took from the item. Now that we have
 2487          * finished doing everything, drop those references.
 2488          */
 2489         if (hook)
 2490                 NG_HOOK_UNREF(hook);
 2491 
 2492         if (rw == NGQRW_R)
 2493                 ng_leave_read(node);
 2494         else
 2495                 ng_leave_write(node);
 2496 
 2497         /* Apply callback. */
 2498         if (apply != NULL) {
 2499                 if (depth == 1 && error != 0)
 2500                         apply->error = error;
 2501                 if (refcount_release(&apply->refs))
 2502                         (*apply->apply)(apply->context, apply->error);
 2503         }
 2504 
 2505         return (error);
 2506 }
 2507 
 2508 /***********************************************************************
 2509  * Implement the 'generic' control messages
 2510  ***********************************************************************/
 2511 static int
 2512 ng_generic_msg(node_p here, item_p item, hook_p lasthook)
 2513 {
 2514         int error = 0;
 2515         struct ng_mesg *msg;
 2516         struct ng_mesg *resp = NULL;
 2517 
 2518         NGI_GET_MSG(item, msg);
 2519         if (msg->header.typecookie != NGM_GENERIC_COOKIE) {
 2520                 TRAP_ERROR();
 2521                 error = EINVAL;
 2522                 goto out;
 2523         }
 2524         switch (msg->header.cmd) {
 2525         case NGM_SHUTDOWN:
 2526                 ng_rmnode(here, NULL, NULL, 0);
 2527                 break;
 2528         case NGM_MKPEER:
 2529             {
 2530                 struct ngm_mkpeer *const mkp = (struct ngm_mkpeer *) msg->data;
 2531 
 2532                 if (msg->header.arglen != sizeof(*mkp)) {
 2533                         TRAP_ERROR();
 2534                         error = EINVAL;
 2535                         break;
 2536                 }
 2537                 mkp->type[sizeof(mkp->type) - 1] = '\0';
 2538                 mkp->ourhook[sizeof(mkp->ourhook) - 1] = '\0';
 2539                 mkp->peerhook[sizeof(mkp->peerhook) - 1] = '\0';
 2540                 error = ng_mkpeer(here, mkp->ourhook, mkp->peerhook, mkp->type);
 2541                 break;
 2542             }
 2543         case NGM_CONNECT:
 2544             {
 2545                 struct ngm_connect *const con =
 2546                         (struct ngm_connect *) msg->data;
 2547                 node_p node2;
 2548 
 2549                 if (msg->header.arglen != sizeof(*con)) {
 2550                         TRAP_ERROR();
 2551                         error = EINVAL;
 2552                         break;
 2553                 }
 2554                 con->path[sizeof(con->path) - 1] = '\0';
 2555                 con->ourhook[sizeof(con->ourhook) - 1] = '\0';
 2556                 con->peerhook[sizeof(con->peerhook) - 1] = '\0';
 2557                 /* Don't forget we get a reference.. */
 2558                 error = ng_path2noderef(here, con->path, &node2, NULL);
 2559                 if (error)
 2560                         break;
 2561                 error = ng_con_nodes(item, here, con->ourhook,
 2562                     node2, con->peerhook);
 2563                 NG_NODE_UNREF(node2);
 2564                 break;
 2565             }
 2566         case NGM_NAME:
 2567             {
 2568                 struct ngm_name *const nam = (struct ngm_name *) msg->data;
 2569 
 2570                 if (msg->header.arglen != sizeof(*nam)) {
 2571                         TRAP_ERROR();
 2572                         error = EINVAL;
 2573                         break;
 2574                 }
 2575                 nam->name[sizeof(nam->name) - 1] = '\0';
 2576                 error = ng_name_node(here, nam->name);
 2577                 break;
 2578             }
 2579         case NGM_RMHOOK:
 2580             {
 2581                 struct ngm_rmhook *const rmh = (struct ngm_rmhook *) msg->data;
 2582                 hook_p hook;
 2583 
 2584                 if (msg->header.arglen != sizeof(*rmh)) {
 2585                         TRAP_ERROR();
 2586                         error = EINVAL;
 2587                         break;
 2588                 }
 2589                 rmh->ourhook[sizeof(rmh->ourhook) - 1] = '\0';
 2590                 if ((hook = ng_findhook(here, rmh->ourhook)) != NULL)
 2591                         ng_destroy_hook(hook);
 2592                 break;
 2593             }
 2594         case NGM_NODEINFO:
 2595             {
 2596                 struct nodeinfo *ni;
 2597 
 2598                 NG_MKRESPONSE(resp, msg, sizeof(*ni), M_NOWAIT);
 2599                 if (resp == NULL) {
 2600                         error = ENOMEM;
 2601                         break;
 2602                 }
 2603 
 2604                 /* Fill in node info */
 2605                 ni = (struct nodeinfo *) resp->data;
 2606                 if (NG_NODE_HAS_NAME(here))
 2607                         strcpy(ni->name, NG_NODE_NAME(here));
 2608                 strcpy(ni->type, here->nd_type->name);
 2609                 ni->id = ng_node2ID(here);
 2610                 ni->hooks = here->nd_numhooks;
 2611                 break;
 2612             }
 2613         case NGM_LISTHOOKS:
 2614             {
 2615                 const int nhooks = here->nd_numhooks;
 2616                 struct hooklist *hl;
 2617                 struct nodeinfo *ni;
 2618                 hook_p hook;
 2619 
 2620                 /* Get response struct */
 2621                 NG_MKRESPONSE(resp, msg, sizeof(*hl) +
 2622                     (nhooks * sizeof(struct linkinfo)), M_NOWAIT);
 2623                 if (resp == NULL) {
 2624                         error = ENOMEM;
 2625                         break;
 2626                 }
 2627                 hl = (struct hooklist *) resp->data;
 2628                 ni = &hl->nodeinfo;
 2629 
 2630                 /* Fill in node info */
 2631                 if (NG_NODE_HAS_NAME(here))
 2632                         strcpy(ni->name, NG_NODE_NAME(here));
 2633                 strcpy(ni->type, here->nd_type->name);
 2634                 ni->id = ng_node2ID(here);
 2635 
 2636                 /* Cycle through the linked list of hooks */
 2637                 ni->hooks = 0;
 2638                 LIST_FOREACH(hook, &here->nd_hooks, hk_hooks) {
 2639                         struct linkinfo *const link = &hl->link[ni->hooks];
 2640 
 2641                         if (ni->hooks >= nhooks) {
 2642                                 log(LOG_ERR, "%s: number of %s changed\n",
 2643                                     __func__, "hooks");
 2644                                 break;
 2645                         }
 2646                         if (NG_HOOK_NOT_VALID(hook))
 2647                                 continue;
 2648                         strcpy(link->ourhook, NG_HOOK_NAME(hook));
 2649                         strcpy(link->peerhook, NG_PEER_HOOK_NAME(hook));
 2650                         if (NG_PEER_NODE_NAME(hook)[0] != '\0')
 2651                                 strcpy(link->nodeinfo.name,
 2652                                     NG_PEER_NODE_NAME(hook));
 2653                         strcpy(link->nodeinfo.type,
 2654                            NG_PEER_NODE(hook)->nd_type->name);
 2655                         link->nodeinfo.id = ng_node2ID(NG_PEER_NODE(hook));
 2656                         link->nodeinfo.hooks = NG_PEER_NODE(hook)->nd_numhooks;
 2657                         ni->hooks++;
 2658                 }
 2659                 break;
 2660             }
 2661 
 2662         case NGM_LISTNODES:
 2663             {
 2664                 struct namelist *nl;
 2665                 node_p node;
 2666                 int i;
 2667 
 2668                 IDHASH_RLOCK();
 2669                 /* Get response struct. */
 2670                 NG_MKRESPONSE(resp, msg, sizeof(*nl) +
 2671                     (V_ng_nodes * sizeof(struct nodeinfo)), M_NOWAIT);
 2672                 if (resp == NULL) {
 2673                         IDHASH_RUNLOCK();
 2674                         error = ENOMEM;
 2675                         break;
 2676                 }
 2677                 nl = (struct namelist *) resp->data;
 2678 
 2679                 /* Cycle through the lists of nodes. */
 2680                 nl->numnames = 0;
 2681                 for (i = 0; i <= V_ng_ID_hmask; i++) {
 2682                         LIST_FOREACH(node, &V_ng_ID_hash[i], nd_idnodes) {
 2683                                 struct nodeinfo *const np =
 2684                                     &nl->nodeinfo[nl->numnames];
 2685 
 2686                                 if (NG_NODE_NOT_VALID(node))
 2687                                         continue;
 2688                                 if (NG_NODE_HAS_NAME(node))
 2689                                         strcpy(np->name, NG_NODE_NAME(node));
 2690                                 strcpy(np->type, node->nd_type->name);
 2691                                 np->id = ng_node2ID(node);
 2692                                 np->hooks = node->nd_numhooks;
 2693                                 KASSERT(nl->numnames < V_ng_nodes,
 2694                                     ("%s: no space", __func__));
 2695                                 nl->numnames++;
 2696                         }
 2697                 }
 2698                 IDHASH_RUNLOCK();
 2699                 break;
 2700             }
 2701         case NGM_LISTNAMES:
 2702             {
 2703                 struct namelist *nl;
 2704                 node_p node;
 2705                 int i;
 2706 
 2707                 NAMEHASH_RLOCK();
 2708                 /* Get response struct. */
 2709                 NG_MKRESPONSE(resp, msg, sizeof(*nl) +
 2710                     (V_ng_named_nodes * sizeof(struct nodeinfo)), M_NOWAIT);
 2711                 if (resp == NULL) {
 2712                         NAMEHASH_RUNLOCK();
 2713                         error = ENOMEM;
 2714                         break;
 2715                 }
 2716                 nl = (struct namelist *) resp->data;
 2717 
 2718                 /* Cycle through the lists of nodes. */
 2719                 nl->numnames = 0;
 2720                 for (i = 0; i <= V_ng_name_hmask; i++) {
 2721                         LIST_FOREACH(node, &V_ng_name_hash[i], nd_nodes) {
 2722                                 struct nodeinfo *const np =
 2723                                     &nl->nodeinfo[nl->numnames];
 2724 
 2725                                 if (NG_NODE_NOT_VALID(node))
 2726                                         continue;
 2727                                 strcpy(np->name, NG_NODE_NAME(node));
 2728                                 strcpy(np->type, node->nd_type->name);
 2729                                 np->id = ng_node2ID(node);
 2730                                 np->hooks = node->nd_numhooks;
 2731                                 KASSERT(nl->numnames < V_ng_named_nodes,
 2732                                     ("%s: no space", __func__));
 2733                                 nl->numnames++;
 2734                         }
 2735                 }
 2736                 NAMEHASH_RUNLOCK();
 2737                 break;
 2738             }
 2739 
 2740         case NGM_LISTTYPES:
 2741             {
 2742                 struct typelist *tl;
 2743                 struct ng_type *type;
 2744                 int num = 0;
 2745 
 2746                 TYPELIST_RLOCK();
 2747                 /* Count number of types */
 2748                 LIST_FOREACH(type, &ng_typelist, types)
 2749                         num++;
 2750 
 2751                 /* Get response struct */
 2752                 NG_MKRESPONSE(resp, msg, sizeof(*tl) +
 2753                     (num * sizeof(struct typeinfo)), M_NOWAIT);
 2754                 if (resp == NULL) {
 2755                         TYPELIST_RUNLOCK();
 2756                         error = ENOMEM;
 2757                         break;
 2758                 }
 2759                 tl = (struct typelist *) resp->data;
 2760 
 2761                 /* Cycle through the linked list of types */
 2762                 tl->numtypes = 0;
 2763                 LIST_FOREACH(type, &ng_typelist, types) {
 2764                         struct typeinfo *const tp = &tl->typeinfo[tl->numtypes];
 2765 
 2766                         strcpy(tp->type_name, type->name);
 2767                         tp->numnodes = type->refs - 1; /* don't count list */
 2768                         KASSERT(tl->numtypes < num, ("%s: no space", __func__));
 2769                         tl->numtypes++;
 2770                 }
 2771                 TYPELIST_RUNLOCK();
 2772                 break;
 2773             }
 2774 
 2775         case NGM_BINARY2ASCII:
 2776             {
 2777                 int bufSize = 1024;
 2778                 const struct ng_parse_type *argstype;
 2779                 const struct ng_cmdlist *c;
 2780                 struct ng_mesg *binary, *ascii;
 2781 
 2782                 /* Data area must contain a valid netgraph message */
 2783                 binary = (struct ng_mesg *)msg->data;
 2784                 if (msg->header.arglen < sizeof(struct ng_mesg) ||
 2785                     (msg->header.arglen - sizeof(struct ng_mesg) <
 2786                     binary->header.arglen)) {
 2787                         TRAP_ERROR();
 2788                         error = EINVAL;
 2789                         break;
 2790                 }
 2791 retry_b2a:
 2792                 /* Get a response message with lots of room */
 2793                 NG_MKRESPONSE(resp, msg, sizeof(*ascii) + bufSize, M_NOWAIT);
 2794                 if (resp == NULL) {
 2795                         error = ENOMEM;
 2796                         break;
 2797                 }
 2798                 ascii = (struct ng_mesg *)resp->data;
 2799 
 2800                 /* Copy binary message header to response message payload */
 2801                 bcopy(binary, ascii, sizeof(*binary));
 2802 
 2803                 /* Find command by matching typecookie and command number */
 2804                 for (c = here->nd_type->cmdlist; c != NULL && c->name != NULL;
 2805                     c++) {
 2806                         if (binary->header.typecookie == c->cookie &&
 2807                             binary->header.cmd == c->cmd)
 2808                                 break;
 2809                 }
 2810                 if (c == NULL || c->name == NULL) {
 2811                         for (c = ng_generic_cmds; c->name != NULL; c++) {
 2812                                 if (binary->header.typecookie == c->cookie &&
 2813                                     binary->header.cmd == c->cmd)
 2814                                         break;
 2815                         }
 2816                         if (c->name == NULL) {
 2817                                 NG_FREE_MSG(resp);
 2818                                 error = ENOSYS;
 2819                                 break;
 2820                         }
 2821                 }
 2822 
 2823                 /* Convert command name to ASCII */
 2824                 snprintf(ascii->header.cmdstr, sizeof(ascii->header.cmdstr),
 2825                     "%s", c->name);
 2826 
 2827                 /* Convert command arguments to ASCII */
 2828                 argstype = (binary->header.flags & NGF_RESP) ?
 2829                     c->respType : c->mesgType;
 2830                 if (argstype == NULL) {
 2831                         *ascii->data = '\0';
 2832                 } else {
 2833                         error = ng_unparse(argstype, (u_char *)binary->data,
 2834                             ascii->data, bufSize);
 2835                         if (error == ERANGE) {
 2836                                 NG_FREE_MSG(resp);
 2837                                 bufSize *= 2;
 2838                                 goto retry_b2a;
 2839                         } else if (error) {
 2840                                 NG_FREE_MSG(resp);
 2841                                 break;
 2842                         }
 2843                 }
 2844 
 2845                 /* Return the result as struct ng_mesg plus ASCII string */
 2846                 bufSize = strlen(ascii->data) + 1;
 2847                 ascii->header.arglen = bufSize;
 2848                 resp->header.arglen = sizeof(*ascii) + bufSize;
 2849                 break;
 2850             }
 2851 
 2852         case NGM_ASCII2BINARY:
 2853             {
 2854                 int bufSize = 20 * 1024;        /* XXX hard coded constant */
 2855                 const struct ng_cmdlist *c;
 2856                 const struct ng_parse_type *argstype;
 2857                 struct ng_mesg *ascii, *binary;
 2858                 int off = 0;
 2859 
 2860                 /* Data area must contain at least a struct ng_mesg + '\0' */
 2861                 ascii = (struct ng_mesg *)msg->data;
 2862                 if ((msg->header.arglen < sizeof(*ascii) + 1) ||
 2863                     (ascii->header.arglen < 1) ||
 2864                     (msg->header.arglen < sizeof(*ascii) +
 2865                     ascii->header.arglen)) {
 2866                         TRAP_ERROR();
 2867                         error = EINVAL;
 2868                         break;
 2869                 }
 2870                 ascii->data[ascii->header.arglen - 1] = '\0';
 2871 
 2872                 /* Get a response message with lots of room */
 2873                 NG_MKRESPONSE(resp, msg, sizeof(*binary) + bufSize, M_NOWAIT);
 2874                 if (resp == NULL) {
 2875                         error = ENOMEM;
 2876                         break;
 2877                 }
 2878                 binary = (struct ng_mesg *)resp->data;
 2879 
 2880                 /* Copy ASCII message header to response message payload */
 2881                 bcopy(ascii, binary, sizeof(*ascii));
 2882 
 2883                 /* Find command by matching ASCII command string */
 2884                 for (c = here->nd_type->cmdlist;
 2885                     c != NULL && c->name != NULL; c++) {
 2886                         if (strcmp(ascii->header.cmdstr, c->name) == 0)
 2887                                 break;
 2888                 }
 2889                 if (c == NULL || c->name == NULL) {
 2890                         for (c = ng_generic_cmds; c->name != NULL; c++) {
 2891                                 if (strcmp(ascii->header.cmdstr, c->name) == 0)
 2892                                         break;
 2893                         }
 2894                         if (c->name == NULL) {
 2895                                 NG_FREE_MSG(resp);
 2896                                 error = ENOSYS;
 2897                                 break;
 2898                         }
 2899                 }
 2900 
 2901                 /* Convert command name to binary */
 2902                 binary->header.cmd = c->cmd;
 2903                 binary->header.typecookie = c->cookie;
 2904 
 2905                 /* Convert command arguments to binary */
 2906                 argstype = (binary->header.flags & NGF_RESP) ?
 2907                     c->respType : c->mesgType;
 2908                 if (argstype == NULL) {
 2909                         bufSize = 0;
 2910                 } else {
 2911                         if ((error = ng_parse(argstype, ascii->data, &off,
 2912                             (u_char *)binary->data, &bufSize)) != 0) {
 2913                                 NG_FREE_MSG(resp);
 2914                                 break;
 2915                         }
 2916                 }
 2917 
 2918                 /* Return the result */
 2919                 binary->header.arglen = bufSize;
 2920                 resp->header.arglen = sizeof(*binary) + bufSize;
 2921                 break;
 2922             }
 2923 
 2924         case NGM_TEXT_CONFIG:
 2925         case NGM_TEXT_STATUS:
 2926                 /*
 2927                  * This one is tricky as it passes the command down to the
 2928                  * actual node, even though it is a generic type command.
 2929                  * This means we must assume that the item/msg is already freed
 2930                  * when control passes back to us.
 2931                  */
 2932                 if (here->nd_type->rcvmsg != NULL) {
 2933                         NGI_MSG(item) = msg; /* put it back as we found it */
 2934                         return((*here->nd_type->rcvmsg)(here, item, lasthook));
 2935                 }
 2936                 /* Fall through if rcvmsg not supported */
 2937         default:
 2938                 TRAP_ERROR();
 2939                 error = EINVAL;
 2940         }
 2941         /*
 2942          * Sometimes a generic message may be statically allocated
 2943          * to avoid problems with allocating when in tight memory situations.
 2944          * Don't free it if it is so.
 2945          * I break them apart here, because erros may cause a free if the item
 2946          * in which case we'd be doing it twice.
 2947          * they are kept together above, to simplify freeing.
 2948          */
 2949 out:
 2950         NG_RESPOND_MSG(error, here, item, resp);
 2951         NG_FREE_MSG(msg);
 2952         return (error);
 2953 }
 2954 
 2955 /************************************************************************
 2956                         Queue element get/free routines
 2957 ************************************************************************/
 2958 
 2959 uma_zone_t                      ng_qzone;
 2960 uma_zone_t                      ng_qdzone;
 2961 static int                      numthreads = 0; /* number of queue threads */
 2962 static int                      maxalloc = 4096;/* limit the damage of a leak */
 2963 static int                      maxdata = 4096; /* limit the damage of a DoS */
 2964 
 2965 SYSCTL_INT(_net_graph, OID_AUTO, threads, CTLFLAG_RDTUN, &numthreads,
 2966     0, "Number of queue processing threads");
 2967 SYSCTL_INT(_net_graph, OID_AUTO, maxalloc, CTLFLAG_RDTUN, &maxalloc,
 2968     0, "Maximum number of non-data queue items to allocate");
 2969 SYSCTL_INT(_net_graph, OID_AUTO, maxdata, CTLFLAG_RDTUN, &maxdata,
 2970     0, "Maximum number of data queue items to allocate");
 2971 
 2972 #ifdef  NETGRAPH_DEBUG
 2973 static TAILQ_HEAD(, ng_item) ng_itemlist = TAILQ_HEAD_INITIALIZER(ng_itemlist);
 2974 static int allocated;   /* number of items malloc'd */
 2975 #endif
 2976 
 2977 /*
 2978  * Get a queue entry.
 2979  * This is usually called when a packet first enters netgraph.
 2980  * By definition, this is usually from an interrupt, or from a user.
 2981  * Users are not so important, but try be quick for the times that it's
 2982  * an interrupt.
 2983  */
 2984 static __inline item_p
 2985 ng_alloc_item(int type, int flags)
 2986 {
 2987         item_p item;
 2988 
 2989         KASSERT(((type & ~NGQF_TYPE) == 0),
 2990             ("%s: incorrect item type: %d", __func__, type));
 2991 
 2992         item = uma_zalloc((type == NGQF_DATA) ? ng_qdzone : ng_qzone,
 2993             ((flags & NG_WAITOK) ? M_WAITOK : M_NOWAIT) | M_ZERO);
 2994 
 2995         if (item) {
 2996                 item->el_flags = type;
 2997 #ifdef  NETGRAPH_DEBUG
 2998                 mtx_lock(&ngq_mtx);
 2999                 TAILQ_INSERT_TAIL(&ng_itemlist, item, all);
 3000                 allocated++;
 3001                 mtx_unlock(&ngq_mtx);
 3002 #endif
 3003         }
 3004 
 3005         return (item);
 3006 }
 3007 
 3008 /*
 3009  * Release a queue entry
 3010  */
 3011 void
 3012 ng_free_item(item_p item)
 3013 {
 3014         /*
 3015          * The item may hold resources on its own. We need to free
 3016          * these before we can free the item. What they are depends upon
 3017          * what kind of item it is. it is important that nodes zero
 3018          * out pointers to resources that they remove from the item
 3019          * or we release them again here.
 3020          */
 3021         switch (item->el_flags & NGQF_TYPE) {
 3022         case NGQF_DATA:
 3023                 /* If we have an mbuf still attached.. */
 3024                 NG_FREE_M(_NGI_M(item));
 3025                 break;
 3026         case NGQF_MESG:
 3027                 _NGI_RETADDR(item) = 0;
 3028                 NG_FREE_MSG(_NGI_MSG(item));
 3029                 break;
 3030         case NGQF_FN:
 3031         case NGQF_FN2:
 3032                 /* nothing to free really, */
 3033                 _NGI_FN(item) = NULL;
 3034                 _NGI_ARG1(item) = NULL;
 3035                 _NGI_ARG2(item) = 0;
 3036                 break;
 3037         }
 3038         /* If we still have a node or hook referenced... */
 3039         _NGI_CLR_NODE(item);
 3040         _NGI_CLR_HOOK(item);
 3041 
 3042 #ifdef  NETGRAPH_DEBUG
 3043         mtx_lock(&ngq_mtx);
 3044         TAILQ_REMOVE(&ng_itemlist, item, all);
 3045         allocated--;
 3046         mtx_unlock(&ngq_mtx);
 3047 #endif
 3048         uma_zfree(((item->el_flags & NGQF_TYPE) == NGQF_DATA) ?
 3049             ng_qdzone : ng_qzone, item);
 3050 }
 3051 
 3052 /*
 3053  * Change type of the queue entry.
 3054  * Possibly reallocates it from another UMA zone.
 3055  */
 3056 static __inline item_p
 3057 ng_realloc_item(item_p pitem, int type, int flags)
 3058 {
 3059         item_p item;
 3060         int from, to;
 3061 
 3062         KASSERT((pitem != NULL), ("%s: can't reallocate NULL", __func__));
 3063         KASSERT(((type & ~NGQF_TYPE) == 0),
 3064             ("%s: incorrect item type: %d", __func__, type));
 3065 
 3066         from = ((pitem->el_flags & NGQF_TYPE) == NGQF_DATA);
 3067         to = (type == NGQF_DATA);
 3068         if (from != to) {
 3069                 /* If reallocation is required do it and copy item. */
 3070                 if ((item = ng_alloc_item(type, flags)) == NULL) {
 3071                         ng_free_item(pitem);
 3072                         return (NULL);
 3073                 }
 3074                 *item = *pitem;
 3075                 ng_free_item(pitem);
 3076         } else
 3077                 item = pitem;
 3078         item->el_flags = (item->el_flags & ~NGQF_TYPE) | type;
 3079 
 3080         return (item);
 3081 }
 3082 
 3083 /************************************************************************
 3084                         Module routines
 3085 ************************************************************************/
 3086 
 3087 /*
 3088  * Handle the loading/unloading of a netgraph node type module
 3089  */
 3090 int
 3091 ng_mod_event(module_t mod, int event, void *data)
 3092 {
 3093         struct ng_type *const type = data;
 3094         int error = 0;
 3095 
 3096         switch (event) {
 3097         case MOD_LOAD:
 3098 
 3099                 /* Register new netgraph node type */
 3100                 if ((error = ng_newtype(type)) != 0)
 3101                         break;
 3102 
 3103                 /* Call type specific code */
 3104                 if (type->mod_event != NULL)
 3105                         if ((error = (*type->mod_event)(mod, event, data))) {
 3106                                 TYPELIST_WLOCK();
 3107                                 type->refs--;   /* undo it */
 3108                                 LIST_REMOVE(type, types);
 3109                                 TYPELIST_WUNLOCK();
 3110                         }
 3111                 break;
 3112 
 3113         case MOD_UNLOAD:
 3114                 if (type->refs > 1) {           /* make sure no nodes exist! */
 3115                         error = EBUSY;
 3116                 } else {
 3117                         if (type->refs == 0) /* failed load, nothing to undo */
 3118                                 break;
 3119                         if (type->mod_event != NULL) {  /* check with type */
 3120                                 error = (*type->mod_event)(mod, event, data);
 3121                                 if (error != 0) /* type refuses.. */
 3122                                         break;
 3123                         }
 3124                         TYPELIST_WLOCK();
 3125                         LIST_REMOVE(type, types);
 3126                         TYPELIST_WUNLOCK();
 3127                 }
 3128                 break;
 3129 
 3130         default:
 3131                 if (type->mod_event != NULL)
 3132                         error = (*type->mod_event)(mod, event, data);
 3133                 else
 3134                         error = EOPNOTSUPP;             /* XXX ? */
 3135                 break;
 3136         }
 3137         return (error);
 3138 }
 3139 
 3140 static void
 3141 vnet_netgraph_init(const void *unused __unused)
 3142 {
 3143 
 3144         /* We start with small hashes, but they can grow. */
 3145         V_ng_ID_hash = hashinit(16, M_NETGRAPH_NODE, &V_ng_ID_hmask);
 3146         V_ng_name_hash = hashinit(16, M_NETGRAPH_NODE, &V_ng_name_hmask);
 3147 }
 3148 VNET_SYSINIT(vnet_netgraph_init, SI_SUB_NETGRAPH, SI_ORDER_FIRST,
 3149     vnet_netgraph_init, NULL);
 3150 
 3151 #ifdef VIMAGE
 3152 static void
 3153 vnet_netgraph_uninit(const void *unused __unused)
 3154 {
 3155         node_p node = NULL, last_killed = NULL;
 3156         int i;
 3157 
 3158         do {
 3159                 /* Find a node to kill */
 3160                 IDHASH_RLOCK();
 3161                 for (i = 0; i <= V_ng_ID_hmask; i++) {
 3162                         LIST_FOREACH(node, &V_ng_ID_hash[i], nd_idnodes) {
 3163                                 if (node != &ng_deadnode) {
 3164                                         NG_NODE_REF(node);
 3165                                         break;
 3166                                 }
 3167                         }
 3168                         if (node != NULL)
 3169                                 break;
 3170                 }
 3171                 IDHASH_RUNLOCK();
 3172 
 3173                 /* Attempt to kill it only if it is a regular node */
 3174                 if (node != NULL) {
 3175                         if (node == last_killed) {
 3176                                 if (node->nd_flags & NGF_REALLY_DIE)
 3177                                         panic("ng node %s won't die",
 3178                                             node->nd_name);
 3179                                 /* The node persisted itself.  Try again. */
 3180                                 node->nd_flags |= NGF_REALLY_DIE;
 3181                         }
 3182                         ng_rmnode(node, NULL, NULL, 0);
 3183                         NG_NODE_UNREF(node);
 3184                         last_killed = node;
 3185                 }
 3186         } while (node != NULL);
 3187 
 3188         hashdestroy(V_ng_name_hash, M_NETGRAPH_NODE, V_ng_name_hmask);
 3189         hashdestroy(V_ng_ID_hash, M_NETGRAPH_NODE, V_ng_ID_hmask);
 3190 }
 3191 VNET_SYSUNINIT(vnet_netgraph_uninit, SI_SUB_NETGRAPH, SI_ORDER_FIRST,
 3192     vnet_netgraph_uninit, NULL);
 3193 #endif /* VIMAGE */
 3194 
 3195 /*
 3196  * Handle loading and unloading for this code.
 3197  * The only thing we need to link into is the NETISR strucure.
 3198  */
 3199 static int
 3200 ngb_mod_event(module_t mod, int event, void *data)
 3201 {
 3202         struct proc *p;
 3203         struct thread *td;
 3204         int i, error = 0;
 3205 
 3206         switch (event) {
 3207         case MOD_LOAD:
 3208                 /* Initialize everything. */
 3209                 NG_WORKLIST_LOCK_INIT();
 3210                 rw_init(&ng_typelist_lock, "netgraph types");
 3211                 rw_init(&ng_idhash_lock, "netgraph idhash");
 3212                 rw_init(&ng_namehash_lock, "netgraph namehash");
 3213                 rw_init(&ng_topo_lock, "netgraph topology mutex");
 3214 #ifdef  NETGRAPH_DEBUG
 3215                 mtx_init(&ng_nodelist_mtx, "netgraph nodelist mutex", NULL,
 3216                     MTX_DEF);
 3217                 mtx_init(&ngq_mtx, "netgraph item list mutex", NULL,
 3218                     MTX_DEF);
 3219 #endif
 3220                 ng_qzone = uma_zcreate("NetGraph items", sizeof(struct ng_item),
 3221                     NULL, NULL, NULL, NULL, UMA_ALIGN_CACHE, 0);
 3222                 uma_zone_set_max(ng_qzone, maxalloc);
 3223                 ng_qdzone = uma_zcreate("NetGraph data items",
 3224                     sizeof(struct ng_item), NULL, NULL, NULL, NULL,
 3225                     UMA_ALIGN_CACHE, 0);
 3226                 uma_zone_set_max(ng_qdzone, maxdata);
 3227                 /* Autoconfigure number of threads. */
 3228                 if (numthreads <= 0)
 3229                         numthreads = mp_ncpus;
 3230                 /* Create threads. */
 3231                 p = NULL; /* start with no process */
 3232                 for (i = 0; i < numthreads; i++) {
 3233                         if (kproc_kthread_add(ngthread, NULL, &p, &td,
 3234                             RFHIGHPID, 0, "ng_queue", "ng_queue%d", i)) {
 3235                                 numthreads = i;
 3236                                 break;
 3237                         }
 3238                 }
 3239                 break;
 3240         case MOD_UNLOAD:
 3241                 /* You can't unload it because an interface may be using it. */
 3242                 error = EBUSY;
 3243                 break;
 3244         default:
 3245                 error = EOPNOTSUPP;
 3246                 break;
 3247         }
 3248         return (error);
 3249 }
 3250 
 3251 static moduledata_t netgraph_mod = {
 3252         "netgraph",
 3253         ngb_mod_event,
 3254         (NULL)
 3255 };
 3256 DECLARE_MODULE(netgraph, netgraph_mod, SI_SUB_NETGRAPH, SI_ORDER_FIRST);
 3257 SYSCTL_NODE(_net, OID_AUTO, graph, CTLFLAG_RW, 0, "netgraph Family");
 3258 SYSCTL_INT(_net_graph, OID_AUTO, abi_version, CTLFLAG_RD, SYSCTL_NULL_INT_PTR, NG_ABI_VERSION,"");
 3259 SYSCTL_INT(_net_graph, OID_AUTO, msg_version, CTLFLAG_RD, SYSCTL_NULL_INT_PTR, NG_VERSION, "");
 3260 
 3261 #ifdef  NETGRAPH_DEBUG
 3262 void
 3263 dumphook (hook_p hook, char *file, int line)
 3264 {
 3265         printf("hook: name %s, %d refs, Last touched:\n",
 3266                 _NG_HOOK_NAME(hook), hook->hk_refs);
 3267         printf("        Last active @ %s, line %d\n",
 3268                 hook->lastfile, hook->lastline);
 3269         if (line) {
 3270                 printf(" problem discovered at file %s, line %d\n", file, line);
 3271 #ifdef KDB
 3272                 kdb_backtrace();
 3273 #endif
 3274         }
 3275 }
 3276 
 3277 void
 3278 dumpnode(node_p node, char *file, int line)
 3279 {
 3280         printf("node: ID [%x]: type '%s', %d hooks, flags 0x%x, %d refs, %s:\n",
 3281                 _NG_NODE_ID(node), node->nd_type->name,
 3282                 node->nd_numhooks, node->nd_flags,
 3283                 node->nd_refs, node->nd_name);
 3284         printf("        Last active @ %s, line %d\n",
 3285                 node->lastfile, node->lastline);
 3286         if (line) {
 3287                 printf(" problem discovered at file %s, line %d\n", file, line);
 3288 #ifdef KDB
 3289                 kdb_backtrace();
 3290 #endif
 3291         }
 3292 }
 3293 
 3294 void
 3295 dumpitem(item_p item, char *file, int line)
 3296 {
 3297         printf(" ACTIVE item, last used at %s, line %d",
 3298                 item->lastfile, item->lastline);
 3299         switch(item->el_flags & NGQF_TYPE) {
 3300         case NGQF_DATA:
 3301                 printf(" - [data]\n");
 3302                 break;
 3303         case NGQF_MESG:
 3304                 printf(" - retaddr[%d]:\n", _NGI_RETADDR(item));
 3305                 break;
 3306         case NGQF_FN:
 3307                 printf(" - fn@%p (%p, %p, %p, %d (%x))\n",
 3308                         _NGI_FN(item),
 3309                         _NGI_NODE(item),
 3310                         _NGI_HOOK(item),
 3311                         item->body.fn.fn_arg1,
 3312                         item->body.fn.fn_arg2,
 3313                         item->body.fn.fn_arg2);
 3314                 break;
 3315         case NGQF_FN2:
 3316                 printf(" - fn2@%p (%p, %p, %p, %d (%x))\n",
 3317                         _NGI_FN2(item),
 3318                         _NGI_NODE(item),
 3319                         _NGI_HOOK(item),
 3320                         item->body.fn.fn_arg1,
 3321                         item->body.fn.fn_arg2,
 3322                         item->body.fn.fn_arg2);
 3323                 break;
 3324         }
 3325         if (line) {
 3326                 printf(" problem discovered at file %s, line %d\n", file, line);
 3327                 if (_NGI_NODE(item)) {
 3328                         printf("node %p ([%x])\n",
 3329                                 _NGI_NODE(item), ng_node2ID(_NGI_NODE(item)));
 3330                 }
 3331         }
 3332 }
 3333 
 3334 static void
 3335 ng_dumpitems(void)
 3336 {
 3337         item_p item;
 3338         int i = 1;
 3339         TAILQ_FOREACH(item, &ng_itemlist, all) {
 3340                 printf("[%d] ", i++);
 3341                 dumpitem(item, NULL, 0);
 3342         }
 3343 }
 3344 
 3345 static void
 3346 ng_dumpnodes(void)
 3347 {
 3348         node_p node;
 3349         int i = 1;
 3350         mtx_lock(&ng_nodelist_mtx);
 3351         SLIST_FOREACH(node, &ng_allnodes, nd_all) {
 3352                 printf("[%d] ", i++);
 3353                 dumpnode(node, NULL, 0);
 3354         }
 3355         mtx_unlock(&ng_nodelist_mtx);
 3356 }
 3357 
 3358 static void
 3359 ng_dumphooks(void)
 3360 {
 3361         hook_p hook;
 3362         int i = 1;
 3363         mtx_lock(&ng_nodelist_mtx);
 3364         SLIST_FOREACH(hook, &ng_allhooks, hk_all) {
 3365                 printf("[%d] ", i++);
 3366                 dumphook(hook, NULL, 0);
 3367         }
 3368         mtx_unlock(&ng_nodelist_mtx);
 3369 }
 3370 
 3371 static int
 3372 sysctl_debug_ng_dump_items(SYSCTL_HANDLER_ARGS)
 3373 {
 3374         int error;
 3375         int val;
 3376         int i;
 3377 
 3378         val = allocated;
 3379         i = 1;
 3380         error = sysctl_handle_int(oidp, &val, 0, req);
 3381         if (error != 0 || req->newptr == NULL)
 3382                 return (error);
 3383         if (val == 42) {
 3384                 ng_dumpitems();
 3385                 ng_dumpnodes();
 3386                 ng_dumphooks();
 3387         }
 3388         return (0);
 3389 }
 3390 
 3391 SYSCTL_PROC(_debug, OID_AUTO, ng_dump_items, CTLTYPE_INT | CTLFLAG_RW,
 3392     0, sizeof(int), sysctl_debug_ng_dump_items, "I", "Number of allocated items");
 3393 #endif  /* NETGRAPH_DEBUG */
 3394 
 3395 /***********************************************************************
 3396 * Worklist routines
 3397 **********************************************************************/
 3398 /*
 3399  * Pick a node off the list of nodes with work,
 3400  * try get an item to process off it. Remove the node from the list.
 3401  */
 3402 static void
 3403 ngthread(void *arg)
 3404 {
 3405         for (;;) {
 3406                 node_p  node;
 3407 
 3408                 /* Get node from the worklist. */
 3409                 NG_WORKLIST_LOCK();
 3410                 while ((node = STAILQ_FIRST(&ng_worklist)) == NULL)
 3411                         NG_WORKLIST_SLEEP();
 3412                 STAILQ_REMOVE_HEAD(&ng_worklist, nd_input_queue.q_work);
 3413                 NG_WORKLIST_UNLOCK();
 3414                 CURVNET_SET(node->nd_vnet);
 3415                 CTR3(KTR_NET, "%20s: node [%x] (%p) taken off worklist",
 3416                     __func__, node->nd_ID, node);
 3417                 /*
 3418                  * We have the node. We also take over the reference
 3419                  * that the list had on it.
 3420                  * Now process as much as you can, until it won't
 3421                  * let you have another item off the queue.
 3422                  * All this time, keep the reference
 3423                  * that lets us be sure that the node still exists.
 3424                  * Let the reference go at the last minute.
 3425                  */
 3426                 for (;;) {
 3427                         item_p item;
 3428                         int rw;
 3429 
 3430                         NG_QUEUE_LOCK(&node->nd_input_queue);
 3431                         item = ng_dequeue(node, &rw);
 3432                         if (item == NULL) {
 3433                                 node->nd_input_queue.q_flags2 &= ~NGQ2_WORKQ;
 3434                                 NG_QUEUE_UNLOCK(&node->nd_input_queue);
 3435                                 break; /* go look for another node */
 3436                         } else {
 3437                                 NG_QUEUE_UNLOCK(&node->nd_input_queue);
 3438                                 NGI_GET_NODE(item, node); /* zaps stored node */
 3439                                 ng_apply_item(node, item, rw);
 3440                                 NG_NODE_UNREF(node);
 3441                         }
 3442                 }
 3443                 NG_NODE_UNREF(node);
 3444                 CURVNET_RESTORE();
 3445         }
 3446 }
 3447 
 3448 /*
 3449  * XXX
 3450  * It's possible that a debugging NG_NODE_REF may need
 3451  * to be outside the mutex zone
 3452  */
 3453 static void
 3454 ng_worklist_add(node_p node)
 3455 {
 3456 
 3457         mtx_assert(&node->nd_input_queue.q_mtx, MA_OWNED);
 3458 
 3459         if ((node->nd_input_queue.q_flags2 & NGQ2_WORKQ) == 0) {
 3460                 /*
 3461                  * If we are not already on the work queue,
 3462                  * then put us on.
 3463                  */
 3464                 node->nd_input_queue.q_flags2 |= NGQ2_WORKQ;
 3465                 NG_NODE_REF(node); /* XXX safe in mutex? */
 3466                 NG_WORKLIST_LOCK();
 3467                 STAILQ_INSERT_TAIL(&ng_worklist, node, nd_input_queue.q_work);
 3468                 NG_WORKLIST_UNLOCK();
 3469                 CTR3(KTR_NET, "%20s: node [%x] (%p) put on worklist", __func__,
 3470                     node->nd_ID, node);
 3471                 NG_WORKLIST_WAKEUP();
 3472         } else {
 3473                 CTR3(KTR_NET, "%20s: node [%x] (%p) already on worklist",
 3474                     __func__, node->nd_ID, node);
 3475         }
 3476 }
 3477 
 3478 /***********************************************************************
 3479 * Externally useable functions to set up a queue item ready for sending
 3480 ***********************************************************************/
 3481 
 3482 #ifdef  NETGRAPH_DEBUG
 3483 #define ITEM_DEBUG_CHECKS                                               \
 3484         do {                                                            \
 3485                 if (NGI_NODE(item) ) {                                  \
 3486                         printf("item already has node");                \
 3487                         kdb_enter(KDB_WHY_NETGRAPH, "has node");        \
 3488                         NGI_CLR_NODE(item);                             \
 3489                 }                                                       \
 3490                 if (NGI_HOOK(item) ) {                                  \
 3491                         printf("item already has hook");                \
 3492                         kdb_enter(KDB_WHY_NETGRAPH, "has hook");        \
 3493                         NGI_CLR_HOOK(item);                             \
 3494                 }                                                       \
 3495         } while (0)
 3496 #else
 3497 #define ITEM_DEBUG_CHECKS
 3498 #endif
 3499 
 3500 /*
 3501  * Put mbuf into the item.
 3502  * Hook and node references will be removed when the item is dequeued.
 3503  * (or equivalent)
 3504  * (XXX) Unsafe because no reference held by peer on remote node.
 3505  * remote node might go away in this timescale.
 3506  * We know the hooks can't go away because that would require getting
 3507  * a writer item on both nodes and we must have at least a  reader
 3508  * here to be able to do this.
 3509  * Note that the hook loaded is the REMOTE hook.
 3510  *
 3511  * This is possibly in the critical path for new data.
 3512  */
 3513 item_p
 3514 ng_package_data(struct mbuf *m, int flags)
 3515 {
 3516         item_p item;
 3517 
 3518         if ((item = ng_alloc_item(NGQF_DATA, flags)) == NULL) {
 3519                 NG_FREE_M(m);
 3520                 return (NULL);
 3521         }
 3522         ITEM_DEBUG_CHECKS;
 3523         item->el_flags |= NGQF_READER;
 3524         NGI_M(item) = m;
 3525         return (item);
 3526 }
 3527 
 3528 /*
 3529  * Allocate a queue item and put items into it..
 3530  * Evaluate the address as this will be needed to queue it and
 3531  * to work out what some of the fields should be.
 3532  * Hook and node references will be removed when the item is dequeued.
 3533  * (or equivalent)
 3534  */
 3535 item_p
 3536 ng_package_msg(struct ng_mesg *msg, int flags)
 3537 {
 3538         item_p item;
 3539 
 3540         if ((item = ng_alloc_item(NGQF_MESG, flags)) == NULL) {
 3541                 NG_FREE_MSG(msg);
 3542                 return (NULL);
 3543         }
 3544         ITEM_DEBUG_CHECKS;
 3545         /* Messages items count as writers unless explicitly exempted. */
 3546         if (msg->header.cmd & NGM_READONLY)
 3547                 item->el_flags |= NGQF_READER;
 3548         else
 3549                 item->el_flags |= NGQF_WRITER;
 3550         /*
 3551          * Set the current lasthook into the queue item
 3552          */
 3553         NGI_MSG(item) = msg;
 3554         NGI_RETADDR(item) = 0;
 3555         return (item);
 3556 }
 3557 
 3558 #define SET_RETADDR(item, here, retaddr)                                \
 3559         do {    /* Data or fn items don't have retaddrs */              \
 3560                 if ((item->el_flags & NGQF_TYPE) == NGQF_MESG) {        \
 3561                         if (retaddr) {                                  \
 3562                                 NGI_RETADDR(item) = retaddr;            \
 3563                         } else {                                        \
 3564                                 /*                                      \
 3565                                  * The old return address should be ok. \
 3566                                  * If there isn't one, use the address  \
 3567                                  * here.                                \
 3568                                  */                                     \
 3569                                 if (NGI_RETADDR(item) == 0) {           \
 3570                                         NGI_RETADDR(item)               \
 3571                                                 = ng_node2ID(here);     \
 3572                                 }                                       \
 3573                         }                                               \
 3574                 }                                                       \
 3575         } while (0)
 3576 
 3577 int
 3578 ng_address_hook(node_p here, item_p item, hook_p hook, ng_ID_t retaddr)
 3579 {
 3580         hook_p peer;
 3581         node_p peernode;
 3582         ITEM_DEBUG_CHECKS;
 3583         /*
 3584          * Quick sanity check..
 3585          * Since a hook holds a reference on its node, once we know
 3586          * that the peer is still connected (even if invalid,) we know
 3587          * that the peer node is present, though maybe invalid.
 3588          */
 3589         TOPOLOGY_RLOCK();
 3590         if ((hook == NULL) || NG_HOOK_NOT_VALID(hook) ||
 3591             NG_HOOK_NOT_VALID(peer = NG_HOOK_PEER(hook)) ||
 3592             NG_NODE_NOT_VALID(peernode = NG_PEER_NODE(hook))) {
 3593                 NG_FREE_ITEM(item);
 3594                 TRAP_ERROR();
 3595                 TOPOLOGY_RUNLOCK();
 3596                 return (ENETDOWN);
 3597         }
 3598 
 3599         /*
 3600          * Transfer our interest to the other (peer) end.
 3601          */
 3602         NG_HOOK_REF(peer);
 3603         NG_NODE_REF(peernode);
 3604         NGI_SET_HOOK(item, peer);
 3605         NGI_SET_NODE(item, peernode);
 3606         SET_RETADDR(item, here, retaddr);
 3607 
 3608         TOPOLOGY_RUNLOCK();
 3609 
 3610         return (0);
 3611 }
 3612 
 3613 int
 3614 ng_address_path(node_p here, item_p item, const char *address, ng_ID_t retaddr)
 3615 {
 3616         node_p  dest = NULL;
 3617         hook_p  hook = NULL;
 3618         int     error;
 3619 
 3620         ITEM_DEBUG_CHECKS;
 3621         /*
 3622          * Note that ng_path2noderef increments the reference count
 3623          * on the node for us if it finds one. So we don't have to.
 3624          */
 3625         error = ng_path2noderef(here, address, &dest, &hook);
 3626         if (error) {
 3627                 NG_FREE_ITEM(item);
 3628                 return (error);
 3629         }
 3630         NGI_SET_NODE(item, dest);
 3631         if (hook)
 3632                 NGI_SET_HOOK(item, hook);
 3633 
 3634         SET_RETADDR(item, here, retaddr);
 3635         return (0);
 3636 }
 3637 
 3638 int
 3639 ng_address_ID(node_p here, item_p item, ng_ID_t ID, ng_ID_t retaddr)
 3640 {
 3641         node_p dest;
 3642 
 3643         ITEM_DEBUG_CHECKS;
 3644         /*
 3645          * Find the target node.
 3646          */
 3647         dest = ng_ID2noderef(ID); /* GETS REFERENCE! */
 3648         if (dest == NULL) {
 3649                 NG_FREE_ITEM(item);
 3650                 TRAP_ERROR();
 3651                 return(EINVAL);
 3652         }
 3653         /* Fill out the contents */
 3654         NGI_SET_NODE(item, dest);
 3655         NGI_CLR_HOOK(item);
 3656         SET_RETADDR(item, here, retaddr);
 3657         return (0);
 3658 }
 3659 
 3660 /*
 3661  * special case to send a message to self (e.g. destroy node)
 3662  * Possibly indicate an arrival hook too.
 3663  * Useful for removing that hook :-)
 3664  */
 3665 item_p
 3666 ng_package_msg_self(node_p here, hook_p hook, struct ng_mesg *msg)
 3667 {
 3668         item_p item;
 3669 
 3670         /*
 3671          * Find the target node.
 3672          * If there is a HOOK argument, then use that in preference
 3673          * to the address.
 3674          */
 3675         if ((item = ng_alloc_item(NGQF_MESG, NG_NOFLAGS)) == NULL) {
 3676                 NG_FREE_MSG(msg);
 3677                 return (NULL);
 3678         }
 3679 
 3680         /* Fill out the contents */
 3681         item->el_flags |= NGQF_WRITER;
 3682         NG_NODE_REF(here);
 3683         NGI_SET_NODE(item, here);
 3684         if (hook) {
 3685                 NG_HOOK_REF(hook);
 3686                 NGI_SET_HOOK(item, hook);
 3687         }
 3688         NGI_MSG(item) = msg;
 3689         NGI_RETADDR(item) = ng_node2ID(here);
 3690         return (item);
 3691 }
 3692 
 3693 /*
 3694  * Send ng_item_fn function call to the specified node.
 3695  */
 3696 
 3697 int
 3698 ng_send_fn(node_p node, hook_p hook, ng_item_fn *fn, void * arg1, int arg2)
 3699 {
 3700 
 3701         return ng_send_fn1(node, hook, fn, arg1, arg2, NG_NOFLAGS);
 3702 }
 3703 
 3704 int
 3705 ng_send_fn1(node_p node, hook_p hook, ng_item_fn *fn, void * arg1, int arg2,
 3706         int flags)
 3707 {
 3708         item_p item;
 3709 
 3710         if ((item = ng_alloc_item(NGQF_FN, flags)) == NULL) {
 3711                 return (ENOMEM);
 3712         }
 3713         item->el_flags |= NGQF_WRITER;
 3714         NG_NODE_REF(node); /* and one for the item */
 3715         NGI_SET_NODE(item, node);
 3716         if (hook) {
 3717                 NG_HOOK_REF(hook);
 3718                 NGI_SET_HOOK(item, hook);
 3719         }
 3720         NGI_FN(item) = fn;
 3721         NGI_ARG1(item) = arg1;
 3722         NGI_ARG2(item) = arg2;
 3723         return(ng_snd_item(item, flags));
 3724 }
 3725 
 3726 /*
 3727  * Send ng_item_fn2 function call to the specified node.
 3728  *
 3729  * If an optional pitem parameter is supplied, its apply
 3730  * callback will be copied to the new item. If also NG_REUSE_ITEM
 3731  * flag is set, no new item will be allocated, but pitem will
 3732  * be used.
 3733  */
 3734 int
 3735 ng_send_fn2(node_p node, hook_p hook, item_p pitem, ng_item_fn2 *fn, void *arg1,
 3736         int arg2, int flags)
 3737 {
 3738         item_p item;
 3739 
 3740         KASSERT((pitem != NULL || (flags & NG_REUSE_ITEM) == 0),
 3741             ("%s: NG_REUSE_ITEM but no pitem", __func__));
 3742 
 3743         /*
 3744          * Allocate a new item if no supplied or
 3745          * if we can't use supplied one.
 3746          */
 3747         if (pitem == NULL || (flags & NG_REUSE_ITEM) == 0) {
 3748                 if ((item = ng_alloc_item(NGQF_FN2, flags)) == NULL)
 3749                         return (ENOMEM);
 3750                 if (pitem != NULL)
 3751                         item->apply = pitem->apply;
 3752         } else {
 3753                 if ((item = ng_realloc_item(pitem, NGQF_FN2, flags)) == NULL)
 3754                         return (ENOMEM);
 3755         }
 3756 
 3757         item->el_flags = (item->el_flags & ~NGQF_RW) | NGQF_WRITER;
 3758         NG_NODE_REF(node); /* and one for the item */
 3759         NGI_SET_NODE(item, node);
 3760         if (hook) {
 3761                 NG_HOOK_REF(hook);
 3762                 NGI_SET_HOOK(item, hook);
 3763         }
 3764         NGI_FN2(item) = fn;
 3765         NGI_ARG1(item) = arg1;
 3766         NGI_ARG2(item) = arg2;
 3767         return(ng_snd_item(item, flags));
 3768 }
 3769 
 3770 /*
 3771  * Official timeout routines for Netgraph nodes.
 3772  */
 3773 static void
 3774 ng_callout_trampoline(void *arg)
 3775 {
 3776         item_p item = arg;
 3777 
 3778         CURVNET_SET(NGI_NODE(item)->nd_vnet);
 3779         ng_snd_item(item, 0);
 3780         CURVNET_RESTORE();
 3781 }
 3782 
 3783 int
 3784 ng_callout(struct callout *c, node_p node, hook_p hook, int ticks,
 3785     ng_item_fn *fn, void * arg1, int arg2)
 3786 {
 3787         item_p item, oitem;
 3788 
 3789         if ((item = ng_alloc_item(NGQF_FN, NG_NOFLAGS)) == NULL)
 3790                 return (ENOMEM);
 3791 
 3792         item->el_flags |= NGQF_WRITER;
 3793         NG_NODE_REF(node);              /* and one for the item */
 3794         NGI_SET_NODE(item, node);
 3795         if (hook) {
 3796                 NG_HOOK_REF(hook);
 3797                 NGI_SET_HOOK(item, hook);
 3798         }
 3799         NGI_FN(item) = fn;
 3800         NGI_ARG1(item) = arg1;
 3801         NGI_ARG2(item) = arg2;
 3802         oitem = c->c_arg;
 3803         if (callout_reset(c, ticks, &ng_callout_trampoline, item) == 1 &&
 3804             oitem != NULL)
 3805                 NG_FREE_ITEM(oitem);
 3806         return (0);
 3807 }
 3808 
 3809 /* A special modified version of untimeout() */
 3810 int
 3811 ng_uncallout(struct callout *c, node_p node)
 3812 {
 3813         item_p item;
 3814         int rval;
 3815 
 3816         KASSERT(c != NULL, ("ng_uncallout: NULL callout"));
 3817         KASSERT(node != NULL, ("ng_uncallout: NULL node"));
 3818 
 3819         rval = callout_stop(c);
 3820         item = c->c_arg;
 3821         /* Do an extra check */
 3822         if ((rval > 0) && (c->c_func == &ng_callout_trampoline) &&
 3823             (item != NULL) && (NGI_NODE(item) == node)) {
 3824                 /*
 3825                  * We successfully removed it from the queue before it ran
 3826                  * So now we need to unreference everything that was
 3827                  * given extra references. (NG_FREE_ITEM does this).
 3828                  */
 3829                 NG_FREE_ITEM(item);
 3830         }
 3831         c->c_arg = NULL;
 3832 
 3833         /*
 3834          * Callers only want to know if the callout was cancelled and
 3835          * not draining or stopped.
 3836          */
 3837         return (rval > 0);
 3838 }
 3839 
 3840 /*
 3841  * Set the address, if none given, give the node here.
 3842  */
 3843 void
 3844 ng_replace_retaddr(node_p here, item_p item, ng_ID_t retaddr)
 3845 {
 3846         if (retaddr) {
 3847                 NGI_RETADDR(item) = retaddr;
 3848         } else {
 3849                 /*
 3850                  * The old return address should be ok.
 3851                  * If there isn't one, use the address here.
 3852                  */
 3853                 NGI_RETADDR(item) = ng_node2ID(here);
 3854         }
 3855 }

Cache object: 406f4a6c77fc9386eaa9af65c0161735


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.