xref: /freebsd/sys/netgraph/ng_base.c (revision c73b94a276b787e6d55e7492bdc0785ca58af32a)
14cf49a43SJulian Elischer /*
24cf49a43SJulian Elischer  * ng_base.c
34cf49a43SJulian Elischer  *
44cf49a43SJulian Elischer  * Copyright (c) 1996-1999 Whistle Communications, Inc.
54cf49a43SJulian Elischer  * All rights reserved.
64cf49a43SJulian Elischer  *
74cf49a43SJulian Elischer  * Subject to the following obligations and disclaimer of warranty, use and
84cf49a43SJulian Elischer  * redistribution of this software, in source or object code forms, with or
94cf49a43SJulian Elischer  * without modifications are expressly permitted by Whistle Communications;
104cf49a43SJulian Elischer  * provided, however, that:
114cf49a43SJulian Elischer  * 1. Any and all reproductions of the source or object code must include the
124cf49a43SJulian Elischer  *    copyright notice above and the following disclaimer of warranties; and
134cf49a43SJulian Elischer  * 2. No rights are granted, in any manner or form, to use Whistle
144cf49a43SJulian Elischer  *    Communications, Inc. trademarks, including the mark "WHISTLE
154cf49a43SJulian Elischer  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
164cf49a43SJulian Elischer  *    such appears in the above copyright notice or in the software.
174cf49a43SJulian Elischer  *
184cf49a43SJulian Elischer  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
194cf49a43SJulian Elischer  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
204cf49a43SJulian Elischer  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
214cf49a43SJulian Elischer  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
224cf49a43SJulian Elischer  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
234cf49a43SJulian Elischer  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
244cf49a43SJulian Elischer  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
254cf49a43SJulian Elischer  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
264cf49a43SJulian Elischer  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
274cf49a43SJulian Elischer  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
284cf49a43SJulian Elischer  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
294cf49a43SJulian Elischer  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
304cf49a43SJulian Elischer  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
314cf49a43SJulian Elischer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
324cf49a43SJulian Elischer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
334cf49a43SJulian Elischer  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
344cf49a43SJulian Elischer  * OF SUCH DAMAGE.
354cf49a43SJulian Elischer  *
36cc3bbd68SJulian Elischer  * Authors: Julian Elischer <julian@freebsd.org>
37cc3bbd68SJulian Elischer  *          Archie Cobbs <archie@freebsd.org>
384cf49a43SJulian Elischer  *
394cf49a43SJulian Elischer  * $FreeBSD$
404cf49a43SJulian Elischer  * $Whistle: ng_base.c,v 1.39 1999/01/28 23:54:53 julian Exp $
414cf49a43SJulian Elischer  */
424cf49a43SJulian Elischer 
434cf49a43SJulian Elischer /*
444cf49a43SJulian Elischer  * This file implements the base netgraph code.
454cf49a43SJulian Elischer  */
464cf49a43SJulian Elischer 
474cf49a43SJulian Elischer #include <sys/param.h>
484cf49a43SJulian Elischer #include <sys/systm.h>
494cf49a43SJulian Elischer #include <sys/errno.h>
504cf49a43SJulian Elischer #include <sys/kernel.h>
514cf49a43SJulian Elischer #include <sys/malloc.h>
524cf49a43SJulian Elischer #include <sys/syslog.h>
53069154d5SJulian Elischer #include <sys/sysctl.h>
544cf49a43SJulian Elischer #include <sys/linker.h>
554cf49a43SJulian Elischer #include <sys/queue.h>
564cf49a43SJulian Elischer #include <sys/mbuf.h>
575b664c7cSPoul-Henning Kamp #include <sys/ctype.h>
582b70adcbSArchie Cobbs #include <machine/limits.h>
594cf49a43SJulian Elischer 
604cf49a43SJulian Elischer #include <net/netisr.h>
614cf49a43SJulian Elischer 
624cf49a43SJulian Elischer #include <netgraph/ng_message.h>
634cf49a43SJulian Elischer #include <netgraph/netgraph.h>
64f8307e12SArchie Cobbs #include <netgraph/ng_parse.h>
654cf49a43SJulian Elischer 
6699ff8176SPeter Wemm MODULE_VERSION(netgraph, 1);
6799ff8176SPeter Wemm 
6830400f03SJulian Elischer /* List of all active nodes */
69069154d5SJulian Elischer static LIST_HEAD(, ng_node) ng_nodelist;
70069154d5SJulian Elischer static struct mtx	ng_nodelist_mtx;
71069154d5SJulian Elischer 
7230400f03SJulian Elischer #ifdef	NETGRAPH_DEBUG
7330400f03SJulian Elischer 
7430400f03SJulian Elischer static SLIST_HEAD(, ng_node) ng_allnodes;
7530400f03SJulian Elischer static LIST_HEAD(, ng_node) ng_freenodes; /* in debug, we never free() them */
7630400f03SJulian Elischer static SLIST_HEAD(, ng_hook) ng_allhooks;
7730400f03SJulian Elischer static LIST_HEAD(, ng_hook) ng_freehooks; /* in debug, we never free() them */
7830400f03SJulian Elischer 
7930400f03SJulian Elischer static void ng_dumpitems(void);
8030400f03SJulian Elischer static void ng_dumpnodes(void);
8130400f03SJulian Elischer static void ng_dumphooks(void);
8230400f03SJulian Elischer 
8330400f03SJulian Elischer #endif	/* NETGRAPH_DEBUG */
84954c4772SJulian Elischer /*
85954c4772SJulian Elischer  * DEAD versions of the structures.
86954c4772SJulian Elischer  * In order to avoid races, it is sometimes neccesary to point
87954c4772SJulian Elischer  * at SOMETHING even though theoretically, the current entity is
88954c4772SJulian Elischer  * INVALID. Use these to avoid these races.
89954c4772SJulian Elischer  */
90954c4772SJulian Elischer struct ng_type ng_deadtype = {
91954c4772SJulian Elischer 	NG_ABI_VERSION,
92954c4772SJulian Elischer 	"dead",
93954c4772SJulian Elischer 	NULL,	/* modevent */
94954c4772SJulian Elischer 	NULL,	/* constructor */
95954c4772SJulian Elischer 	NULL,	/* rcvmsg */
96954c4772SJulian Elischer 	NULL,	/* shutdown */
97954c4772SJulian Elischer 	NULL,	/* newhook */
98954c4772SJulian Elischer 	NULL,	/* findhook */
99954c4772SJulian Elischer 	NULL,	/* connect */
100954c4772SJulian Elischer 	NULL,	/* rcvdata */
101954c4772SJulian Elischer 	NULL,	/* disconnect */
102954c4772SJulian Elischer 	NULL, 	/* cmdlist */
103954c4772SJulian Elischer };
10430400f03SJulian Elischer 
105954c4772SJulian Elischer struct ng_node ng_deadnode = {
106954c4772SJulian Elischer 	"dead",
107954c4772SJulian Elischer 	&ng_deadtype,
108954c4772SJulian Elischer 	NG_INVALID,
109954c4772SJulian Elischer 	1,	/* refs */
110954c4772SJulian Elischer 	0,	/* numhooks */
111954c4772SJulian Elischer 	NULL,	/* private */
112954c4772SJulian Elischer 	0,	/* ID */
113954c4772SJulian Elischer 	LIST_HEAD_INITIALIZER(ng_deadnode.hooks),
114954c4772SJulian Elischer 	{},	/* all_nodes list entry */
115954c4772SJulian Elischer 	{},	/* id hashtable list entry */
116954c4772SJulian Elischer 	{},	/* workqueue entry */
117954c4772SJulian Elischer 	{	0,
118954c4772SJulian Elischer 		{}, /* should never use! (should hang) */
119954c4772SJulian Elischer 		NULL,
120954c4772SJulian Elischer 		&ng_deadnode.nd_input_queue.queue,
121954c4772SJulian Elischer 		&ng_deadnode
122954c4772SJulian Elischer 	},
123954c4772SJulian Elischer #ifdef	NETGRAPH_DEBUG
124954c4772SJulian Elischer 	ND_MAGIC,
125954c4772SJulian Elischer 	__FILE__,
126954c4772SJulian Elischer 	__LINE__,
127954c4772SJulian Elischer 	{NULL}
128954c4772SJulian Elischer #endif	/* NETGRAPH_DEBUG */
129954c4772SJulian Elischer };
130954c4772SJulian Elischer 
131954c4772SJulian Elischer struct ng_hook ng_deadhook = {
132954c4772SJulian Elischer 	"dead",
133954c4772SJulian Elischer 	NULL,		/* private */
134954c4772SJulian Elischer 	HK_INVALID | HK_DEAD,
135954c4772SJulian Elischer 	1,		/* refs always >= 1 */
136954c4772SJulian Elischer 	&ng_deadhook,	/* Peer is self */
137954c4772SJulian Elischer 	&ng_deadnode,	/* attached to deadnode */
138954c4772SJulian Elischer 	{},		/* hooks list */
139954c4772SJulian Elischer #ifdef	NETGRAPH_DEBUG
140954c4772SJulian Elischer 	HK_MAGIC,
141954c4772SJulian Elischer 	__FILE__,
142954c4772SJulian Elischer 	__LINE__,
143954c4772SJulian Elischer 	{NULL}
144954c4772SJulian Elischer #endif	/* NETGRAPH_DEBUG */
145954c4772SJulian Elischer };
146954c4772SJulian Elischer 
147954c4772SJulian Elischer /*
148954c4772SJulian Elischer  * END DEAD STRUCTURES
149954c4772SJulian Elischer  */
150069154d5SJulian Elischer /* List nodes with unallocated work */
151069154d5SJulian Elischer static TAILQ_HEAD(, ng_node) ng_worklist = TAILQ_HEAD_INITIALIZER(ng_worklist);
152069154d5SJulian Elischer static struct mtx	ng_worklist_mtx;
1534cf49a43SJulian Elischer 
1544cf49a43SJulian Elischer /* List of installed types */
155069154d5SJulian Elischer static LIST_HEAD(, ng_type) ng_typelist;
156069154d5SJulian Elischer static struct mtx	ng_typelist_mtx;
1574cf49a43SJulian Elischer 
158069154d5SJulian Elischer /* Hash related definitions */
1590f150d04SJulian Elischer /* XXX Don't need to initialise them because it's a LIST */
1600f150d04SJulian Elischer #define NG_ID_HASH_SIZE 32 /* most systems wont need even this many */
1610f150d04SJulian Elischer static LIST_HEAD(, ng_node) ng_ID_hash[NG_ID_HASH_SIZE];
162069154d5SJulian Elischer static struct mtx	ng_idhash_mtx;
1630f150d04SJulian Elischer /* Method to find a node.. used twice so do it here */
1640f150d04SJulian Elischer #define NG_IDHASH_FN(ID) ((ID) % (NG_ID_HASH_SIZE))
1650f150d04SJulian Elischer #define NG_IDHASH_FIND(ID, node)					\
1660f150d04SJulian Elischer 	do { 								\
1670f150d04SJulian Elischer 		LIST_FOREACH(node, &ng_ID_hash[NG_IDHASH_FN(ID)],	\
1680f150d04SJulian Elischer 						nd_idnodes) {		\
1690f150d04SJulian Elischer 			if (NG_NODE_IS_VALID(node)			\
1700f150d04SJulian Elischer 			&& (NG_NODE_ID(node) == ID)) {			\
1710f150d04SJulian Elischer 				break;					\
1720f150d04SJulian Elischer 			}						\
1730f150d04SJulian Elischer 		}							\
1740f150d04SJulian Elischer 	} while (0)
175069154d5SJulian Elischer 
176069154d5SJulian Elischer /* Mutex that protects the free queue item list */
177069154d5SJulian Elischer static volatile item_p		ngqfree;	/* free ones */
178069154d5SJulian Elischer static struct mtx	ngq_mtx;
179dc90cad9SJulian Elischer 
1804cf49a43SJulian Elischer /* Internal functions */
1814cf49a43SJulian Elischer static int	ng_add_hook(node_p node, const char *name, hook_p * hookp);
182069154d5SJulian Elischer static int	ng_generic_msg(node_p here, item_p item, hook_p lasthook);
183dc90cad9SJulian Elischer static ng_ID_t	ng_decodeidname(const char *name);
1844cf49a43SJulian Elischer static int	ngb_mod_event(module_t mod, int event, void *data);
185069154d5SJulian Elischer static void	ng_worklist_remove(node_p node);
1864cf49a43SJulian Elischer static void	ngintr(void);
187069154d5SJulian Elischer static int	ng_apply_item(node_p node, item_p item);
188069154d5SJulian Elischer static void	ng_flush_input_queue(struct ng_queue * ngq);
189069154d5SJulian Elischer static void	ng_setisr(node_p node);
190069154d5SJulian Elischer static node_p	ng_ID2noderef(ng_ID_t ID);
1916b795970SJulian Elischer static int	ng_con_nodes(node_p node, const char *name, node_p node2,
1926b795970SJulian Elischer 							const char *name2);
1936b795970SJulian Elischer static int	ng_con_part2(node_p node, hook_p hook, void *arg1, int arg2);
1946b795970SJulian Elischer static int	ng_con_part3(node_p node, hook_p hook, void *arg1, int arg2);
1956b795970SJulian Elischer static int	ng_mkpeer(node_p node, const char *name,
1966b795970SJulian Elischer 						const char *name2, char *type);
197069154d5SJulian Elischer 
19830400f03SJulian Elischer /* imported , these used to be externally visible, some may go back */
199069154d5SJulian Elischer int	ng_bypass(hook_p hook1, hook_p hook2);
200069154d5SJulian Elischer void	ng_destroy_hook(hook_p hook);
201069154d5SJulian Elischer node_p	ng_name2noderef(node_p node, const char *name);
202069154d5SJulian Elischer int	ng_path2noderef(node_p here, const char *path,
203069154d5SJulian Elischer 	node_p *dest, hook_p *lasthook);
204069154d5SJulian Elischer struct	ng_type *ng_findtype(const char *type);
205069154d5SJulian Elischer int	ng_make_node(const char *type, node_p *nodepp);
206069154d5SJulian Elischer int	ng_path_parse(char *addr, char **node, char **path, char **hook);
207069154d5SJulian Elischer void	ng_rmnode(node_p node);
20830400f03SJulian Elischer void	ng_unname(node_p node);
209069154d5SJulian Elischer 
2104cf49a43SJulian Elischer 
2114cf49a43SJulian Elischer /* Our own netgraph malloc type */
2124cf49a43SJulian Elischer MALLOC_DEFINE(M_NETGRAPH, "netgraph", "netgraph structures and ctrl messages");
213069154d5SJulian Elischer MALLOC_DEFINE(M_NETGRAPH_HOOK, "netgraph_hook", "netgraph hook structures");
214069154d5SJulian Elischer MALLOC_DEFINE(M_NETGRAPH_NODE, "netgraph_node", "netgraph node structures");
215069154d5SJulian Elischer MALLOC_DEFINE(M_NETGRAPH_ITEM, "netgraph_item", "netgraph item structures");
216069154d5SJulian Elischer MALLOC_DEFINE(M_NETGRAPH_META, "netgraph_meta", "netgraph name storage");
217069154d5SJulian Elischer MALLOC_DEFINE(M_NETGRAPH_MSG, "netgraph_msg", "netgraph name storage");
218069154d5SJulian Elischer 
219069154d5SJulian Elischer /* Should not be visible outside this file */
22030400f03SJulian Elischer 
22130400f03SJulian Elischer #define _NG_ALLOC_HOOK(hook) \
22230400f03SJulian Elischer 	MALLOC(hook, hook_p, sizeof(*hook), M_NETGRAPH_HOOK, M_NOWAIT | M_ZERO)
22330400f03SJulian Elischer #define _NG_ALLOC_NODE(node) \
22430400f03SJulian Elischer 	MALLOC(node, node_p, sizeof(*node), M_NETGRAPH_NODE, M_NOWAIT | M_ZERO)
22530400f03SJulian Elischer 
22630400f03SJulian Elischer #ifdef NETGRAPH_DEBUG /*----------------------------------------------*/
22730400f03SJulian Elischer /*
22830400f03SJulian Elischer  * In debug mode:
22930400f03SJulian Elischer  * In an attempt to help track reference count screwups
23030400f03SJulian Elischer  * we do not free objects back to the malloc system, but keep them
23130400f03SJulian Elischer  * in a local cache where we can examine them and keep information safely
23230400f03SJulian Elischer  * after they have been freed.
23330400f03SJulian Elischer  * We use this scheme for nodes and hooks, and to some extent for items.
23430400f03SJulian Elischer  */
23530400f03SJulian Elischer static __inline hook_p
23630400f03SJulian Elischer ng_alloc_hook(void)
23730400f03SJulian Elischer {
23830400f03SJulian Elischer 	hook_p hook;
23930400f03SJulian Elischer 	SLIST_ENTRY(ng_hook) temp;
24030400f03SJulian Elischer 	mtx_enter(&ng_nodelist_mtx, MTX_DEF);
24130400f03SJulian Elischer 	hook = LIST_FIRST(&ng_freehooks);
24230400f03SJulian Elischer 	if (hook) {
24330400f03SJulian Elischer 		LIST_REMOVE(hook, hk_hooks);
24430400f03SJulian Elischer 		bcopy(&hook->hk_all, &temp, sizeof(temp));
24530400f03SJulian Elischer 		bzero(hook, sizeof(struct ng_hook));
24630400f03SJulian Elischer 		bcopy(&temp, &hook->hk_all, sizeof(temp));
24730400f03SJulian Elischer 		mtx_exit(&ng_nodelist_mtx, MTX_DEF);
24830400f03SJulian Elischer 		hook->hk_magic = HK_MAGIC;
24930400f03SJulian Elischer 	} else {
25030400f03SJulian Elischer 		mtx_exit(&ng_nodelist_mtx, MTX_DEF);
25130400f03SJulian Elischer 		_NG_ALLOC_HOOK(hook);
25230400f03SJulian Elischer 		if (hook) {
25330400f03SJulian Elischer 			hook->hk_magic = HK_MAGIC;
25430400f03SJulian Elischer 			mtx_enter(&ng_nodelist_mtx, MTX_DEF);
25530400f03SJulian Elischer 			SLIST_INSERT_HEAD(&ng_allhooks, hook, hk_all);
25630400f03SJulian Elischer 			mtx_exit(&ng_nodelist_mtx, MTX_DEF);
25730400f03SJulian Elischer 		}
25830400f03SJulian Elischer 	}
25930400f03SJulian Elischer 	return (hook);
26030400f03SJulian Elischer }
26130400f03SJulian Elischer 
26230400f03SJulian Elischer static __inline node_p
26330400f03SJulian Elischer ng_alloc_node(void)
26430400f03SJulian Elischer {
26530400f03SJulian Elischer 	node_p node;
26630400f03SJulian Elischer 	SLIST_ENTRY(ng_node) temp;
26730400f03SJulian Elischer 	mtx_enter(&ng_nodelist_mtx, MTX_DEF);
26830400f03SJulian Elischer 	node = LIST_FIRST(&ng_freenodes);
26930400f03SJulian Elischer 	if (node) {
27030400f03SJulian Elischer 		LIST_REMOVE(node, nd_nodes);
27130400f03SJulian Elischer 		bcopy(&node->nd_all, &temp, sizeof(temp));
27230400f03SJulian Elischer 		bzero(node, sizeof(struct ng_node));
27330400f03SJulian Elischer 		bcopy(&temp, &node->nd_all, sizeof(temp));
27430400f03SJulian Elischer 		mtx_exit(&ng_nodelist_mtx, MTX_DEF);
27530400f03SJulian Elischer 		node->nd_magic = ND_MAGIC;
27630400f03SJulian Elischer 	} else {
27730400f03SJulian Elischer 		mtx_exit(&ng_nodelist_mtx, MTX_DEF);
27830400f03SJulian Elischer 		_NG_ALLOC_NODE(node);
27930400f03SJulian Elischer 		if (node) {
28030400f03SJulian Elischer 			node->nd_magic = ND_MAGIC;
28130400f03SJulian Elischer 			mtx_enter(&ng_nodelist_mtx, MTX_DEF);
28230400f03SJulian Elischer 			SLIST_INSERT_HEAD(&ng_allnodes, node, nd_all);
28330400f03SJulian Elischer 			mtx_exit(&ng_nodelist_mtx, MTX_DEF);
28430400f03SJulian Elischer 		}
28530400f03SJulian Elischer 	}
28630400f03SJulian Elischer 	return (node);
28730400f03SJulian Elischer }
28830400f03SJulian Elischer 
28930400f03SJulian Elischer #define NG_ALLOC_HOOK(hook) do { (hook) = ng_alloc_hook(); } while (0)
29030400f03SJulian Elischer #define NG_ALLOC_NODE(node) do { (node) = ng_alloc_node(); } while (0)
29130400f03SJulian Elischer 
29230400f03SJulian Elischer 
29330400f03SJulian Elischer #define NG_FREE_HOOK(hook)						\
29430400f03SJulian Elischer 	do {								\
29530400f03SJulian Elischer 		mtx_enter(&ng_nodelist_mtx, MTX_DEF);			\
29630400f03SJulian Elischer 		LIST_INSERT_HEAD(&ng_freehooks, hook, hk_hooks);	\
29730400f03SJulian Elischer 		hook->hk_magic = 0;					\
29830400f03SJulian Elischer 		mtx_exit(&ng_nodelist_mtx, MTX_DEF);			\
29930400f03SJulian Elischer 	} while (0)
30030400f03SJulian Elischer 
30130400f03SJulian Elischer #define NG_FREE_NODE(node)						\
30230400f03SJulian Elischer 	do {								\
30330400f03SJulian Elischer 		mtx_enter(&ng_nodelist_mtx, MTX_DEF);			\
30430400f03SJulian Elischer 		LIST_INSERT_HEAD(&ng_freenodes, node, nd_nodes);	\
30530400f03SJulian Elischer 		node->nd_magic = 0;					\
30630400f03SJulian Elischer 		mtx_exit(&ng_nodelist_mtx, MTX_DEF);			\
30730400f03SJulian Elischer 	} while (0)
30830400f03SJulian Elischer 
30930400f03SJulian Elischer #else /* NETGRAPH_DEBUG */ /*----------------------------------------------*/
31030400f03SJulian Elischer 
31130400f03SJulian Elischer #define NG_ALLOC_HOOK(hook) _NG_ALLOC_HOOK(hook)
31230400f03SJulian Elischer #define NG_ALLOC_NODE(node) _NG_ALLOC_NODE(node)
31330400f03SJulian Elischer 
314069154d5SJulian Elischer #define NG_FREE_HOOK(hook) do { FREE((hook), M_NETGRAPH_HOOK); } while (0)
315069154d5SJulian Elischer #define NG_FREE_NODE(node) do { FREE((node), M_NETGRAPH_NODE); } while (0)
31630400f03SJulian Elischer 
31730400f03SJulian Elischer #endif /* NETGRAPH_DEBUG */ /*----------------------------------------------*/
31830400f03SJulian Elischer 
319069154d5SJulian Elischer /* Warning: Generally use NG_FREE_ITEM() instead */
320069154d5SJulian Elischer #define NG_FREE_ITEM_REAL(item) do { FREE((item), M_NETGRAPH_ITEM); } while (0)
321069154d5SJulian Elischer 
3224cf49a43SJulian Elischer 
3234cf49a43SJulian Elischer /* Set this to Debugger("X") to catch all errors as they occur */
3244cf49a43SJulian Elischer #ifndef TRAP_ERROR
3256b795970SJulian Elischer #define TRAP_ERROR()
3264cf49a43SJulian Elischer #endif
3274cf49a43SJulian Elischer 
328dc90cad9SJulian Elischer static	ng_ID_t nextID = 1;
329dc90cad9SJulian Elischer 
330b2da83c2SArchie Cobbs #ifdef INVARIANTS
331b2da83c2SArchie Cobbs #define CHECK_DATA_MBUF(m)	do {					\
332b2da83c2SArchie Cobbs 		struct mbuf *n;						\
333b2da83c2SArchie Cobbs 		int total;						\
334b2da83c2SArchie Cobbs 									\
335b2da83c2SArchie Cobbs 		if (((m)->m_flags & M_PKTHDR) == 0)			\
336b2da83c2SArchie Cobbs 			panic("%s: !PKTHDR", __FUNCTION__);		\
337b2da83c2SArchie Cobbs 		for (total = 0, n = (m); n != NULL; n = n->m_next)	\
338b2da83c2SArchie Cobbs 			total += n->m_len;				\
339b2da83c2SArchie Cobbs 		if ((m)->m_pkthdr.len != total) {			\
340b2da83c2SArchie Cobbs 			panic("%s: %d != %d",				\
341b2da83c2SArchie Cobbs 			    __FUNCTION__, (m)->m_pkthdr.len, total);	\
342b2da83c2SArchie Cobbs 		}							\
343b2da83c2SArchie Cobbs 	} while (0)
344b2da83c2SArchie Cobbs #else
345b2da83c2SArchie Cobbs #define CHECK_DATA_MBUF(m)
346b2da83c2SArchie Cobbs #endif
347b2da83c2SArchie Cobbs 
348dc90cad9SJulian Elischer 
3494cf49a43SJulian Elischer /************************************************************************
350f8307e12SArchie Cobbs 	Parse type definitions for generic messages
351f8307e12SArchie Cobbs ************************************************************************/
352f8307e12SArchie Cobbs 
353f8307e12SArchie Cobbs /* Handy structure parse type defining macro */
354f8307e12SArchie Cobbs #define DEFINE_PARSE_STRUCT_TYPE(lo, up, args)				\
355f8307e12SArchie Cobbs static const struct ng_parse_struct_info				\
356f8307e12SArchie Cobbs 	ng_ ## lo ## _type_info = NG_GENERIC_ ## up ## _INFO args;	\
357f8307e12SArchie Cobbs static const struct ng_parse_type ng_generic_ ## lo ## _type = {	\
358f8307e12SArchie Cobbs 	&ng_parse_struct_type,						\
359f8307e12SArchie Cobbs 	&ng_ ## lo ## _type_info					\
360f8307e12SArchie Cobbs }
361f8307e12SArchie Cobbs 
362f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(mkpeer, MKPEER, ());
363f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(connect, CONNECT, ());
364f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(name, NAME, ());
365f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(rmhook, RMHOOK, ());
366f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(nodeinfo, NODEINFO, ());
367f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(typeinfo, TYPEINFO, ());
368f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(linkinfo, LINKINFO, (&ng_generic_nodeinfo_type));
369f8307e12SArchie Cobbs 
370f8307e12SArchie Cobbs /* Get length of an array when the length is stored as a 32 bit
371f8307e12SArchie Cobbs    value immediately preceeding the array -- as with struct namelist
372f8307e12SArchie Cobbs    and struct typelist. */
373f8307e12SArchie Cobbs static int
374f8307e12SArchie Cobbs ng_generic_list_getLength(const struct ng_parse_type *type,
375f8307e12SArchie Cobbs 	const u_char *start, const u_char *buf)
376f8307e12SArchie Cobbs {
377f8307e12SArchie Cobbs 	return *((const u_int32_t *)(buf - 4));
378f8307e12SArchie Cobbs }
379f8307e12SArchie Cobbs 
380f8307e12SArchie Cobbs /* Get length of the array of struct linkinfo inside a struct hooklist */
381f8307e12SArchie Cobbs static int
382f8307e12SArchie Cobbs ng_generic_linkinfo_getLength(const struct ng_parse_type *type,
383f8307e12SArchie Cobbs 	const u_char *start, const u_char *buf)
384f8307e12SArchie Cobbs {
385f8307e12SArchie Cobbs 	const struct hooklist *hl = (const struct hooklist *)start;
386f8307e12SArchie Cobbs 
387f8307e12SArchie Cobbs 	return hl->nodeinfo.hooks;
388f8307e12SArchie Cobbs }
389f8307e12SArchie Cobbs 
390f8307e12SArchie Cobbs /* Array type for a variable length array of struct namelist */
391f8307e12SArchie Cobbs static const struct ng_parse_array_info ng_nodeinfoarray_type_info = {
392f8307e12SArchie Cobbs 	&ng_generic_nodeinfo_type,
393f8307e12SArchie Cobbs 	&ng_generic_list_getLength
394f8307e12SArchie Cobbs };
395f8307e12SArchie Cobbs static const struct ng_parse_type ng_generic_nodeinfoarray_type = {
396f8307e12SArchie Cobbs 	&ng_parse_array_type,
397f8307e12SArchie Cobbs 	&ng_nodeinfoarray_type_info
398f8307e12SArchie Cobbs };
399f8307e12SArchie Cobbs 
400f8307e12SArchie Cobbs /* Array type for a variable length array of struct typelist */
401f8307e12SArchie Cobbs static const struct ng_parse_array_info ng_typeinfoarray_type_info = {
402f8307e12SArchie Cobbs 	&ng_generic_typeinfo_type,
403f8307e12SArchie Cobbs 	&ng_generic_list_getLength
404f8307e12SArchie Cobbs };
405f8307e12SArchie Cobbs static const struct ng_parse_type ng_generic_typeinfoarray_type = {
406f8307e12SArchie Cobbs 	&ng_parse_array_type,
407f8307e12SArchie Cobbs 	&ng_typeinfoarray_type_info
408f8307e12SArchie Cobbs };
409f8307e12SArchie Cobbs 
410f8307e12SArchie Cobbs /* Array type for array of struct linkinfo in struct hooklist */
411f8307e12SArchie Cobbs static const struct ng_parse_array_info ng_generic_linkinfo_array_type_info = {
412f8307e12SArchie Cobbs 	&ng_generic_linkinfo_type,
413f8307e12SArchie Cobbs 	&ng_generic_linkinfo_getLength
414f8307e12SArchie Cobbs };
415f8307e12SArchie Cobbs static const struct ng_parse_type ng_generic_linkinfo_array_type = {
416f8307e12SArchie Cobbs 	&ng_parse_array_type,
417f8307e12SArchie Cobbs 	&ng_generic_linkinfo_array_type_info
418f8307e12SArchie Cobbs };
419f8307e12SArchie Cobbs 
420f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(typelist, TYPELIST, (&ng_generic_nodeinfoarray_type));
421f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(hooklist, HOOKLIST,
422f8307e12SArchie Cobbs 	(&ng_generic_nodeinfo_type, &ng_generic_linkinfo_array_type));
423f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(listnodes, LISTNODES,
424f8307e12SArchie Cobbs 	(&ng_generic_nodeinfoarray_type));
425f8307e12SArchie Cobbs 
426f8307e12SArchie Cobbs /* List of commands and how to convert arguments to/from ASCII */
427f8307e12SArchie Cobbs static const struct ng_cmdlist ng_generic_cmds[] = {
428f8307e12SArchie Cobbs 	{
429f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
430f8307e12SArchie Cobbs 	  NGM_SHUTDOWN,
431f8307e12SArchie Cobbs 	  "shutdown",
432f8307e12SArchie Cobbs 	  NULL,
433f8307e12SArchie Cobbs 	  NULL
434f8307e12SArchie Cobbs 	},
435f8307e12SArchie Cobbs 	{
436f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
437f8307e12SArchie Cobbs 	  NGM_MKPEER,
438f8307e12SArchie Cobbs 	  "mkpeer",
439f8307e12SArchie Cobbs 	  &ng_generic_mkpeer_type,
440f8307e12SArchie Cobbs 	  NULL
441f8307e12SArchie Cobbs 	},
442f8307e12SArchie Cobbs 	{
443f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
444f8307e12SArchie Cobbs 	  NGM_CONNECT,
445f8307e12SArchie Cobbs 	  "connect",
446f8307e12SArchie Cobbs 	  &ng_generic_connect_type,
447f8307e12SArchie Cobbs 	  NULL
448f8307e12SArchie Cobbs 	},
449f8307e12SArchie Cobbs 	{
450f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
451f8307e12SArchie Cobbs 	  NGM_NAME,
452f8307e12SArchie Cobbs 	  "name",
453f8307e12SArchie Cobbs 	  &ng_generic_name_type,
454f8307e12SArchie Cobbs 	  NULL
455f8307e12SArchie Cobbs 	},
456f8307e12SArchie Cobbs 	{
457f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
458f8307e12SArchie Cobbs 	  NGM_RMHOOK,
459f8307e12SArchie Cobbs 	  "rmhook",
460f8307e12SArchie Cobbs 	  &ng_generic_rmhook_type,
461f8307e12SArchie Cobbs 	  NULL
462f8307e12SArchie Cobbs 	},
463f8307e12SArchie Cobbs 	{
464f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
465f8307e12SArchie Cobbs 	  NGM_NODEINFO,
466f8307e12SArchie Cobbs 	  "nodeinfo",
467f8307e12SArchie Cobbs 	  NULL,
468f8307e12SArchie Cobbs 	  &ng_generic_nodeinfo_type
469f8307e12SArchie Cobbs 	},
470f8307e12SArchie Cobbs 	{
471f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
472f8307e12SArchie Cobbs 	  NGM_LISTHOOKS,
473f8307e12SArchie Cobbs 	  "listhooks",
474f8307e12SArchie Cobbs 	  NULL,
475f8307e12SArchie Cobbs 	  &ng_generic_hooklist_type
476f8307e12SArchie Cobbs 	},
477f8307e12SArchie Cobbs 	{
478f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
479f8307e12SArchie Cobbs 	  NGM_LISTNAMES,
480f8307e12SArchie Cobbs 	  "listnames",
481f8307e12SArchie Cobbs 	  NULL,
482f8307e12SArchie Cobbs 	  &ng_generic_listnodes_type	/* same as NGM_LISTNODES */
483f8307e12SArchie Cobbs 	},
484f8307e12SArchie Cobbs 	{
485f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
486f8307e12SArchie Cobbs 	  NGM_LISTNODES,
487f8307e12SArchie Cobbs 	  "listnodes",
488f8307e12SArchie Cobbs 	  NULL,
489f8307e12SArchie Cobbs 	  &ng_generic_listnodes_type
490f8307e12SArchie Cobbs 	},
491f8307e12SArchie Cobbs 	{
492f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
493f8307e12SArchie Cobbs 	  NGM_LISTTYPES,
494f8307e12SArchie Cobbs 	  "listtypes",
495f8307e12SArchie Cobbs 	  NULL,
496f8307e12SArchie Cobbs 	  &ng_generic_typeinfo_type
497f8307e12SArchie Cobbs 	},
498f8307e12SArchie Cobbs 	{
499f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
5007095e097SPoul-Henning Kamp 	  NGM_TEXT_CONFIG,
5017095e097SPoul-Henning Kamp 	  "textconfig",
5027095e097SPoul-Henning Kamp 	  NULL,
5037095e097SPoul-Henning Kamp 	  &ng_parse_string_type
5047095e097SPoul-Henning Kamp 	},
5057095e097SPoul-Henning Kamp 	{
5067095e097SPoul-Henning Kamp 	  NGM_GENERIC_COOKIE,
507f8307e12SArchie Cobbs 	  NGM_TEXT_STATUS,
508f8307e12SArchie Cobbs 	  "textstatus",
509f8307e12SArchie Cobbs 	  NULL,
510f8307e12SArchie Cobbs 	  &ng_parse_string_type
511f8307e12SArchie Cobbs 	},
512f8307e12SArchie Cobbs 	{
513f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
514f8307e12SArchie Cobbs 	  NGM_ASCII2BINARY,
515f8307e12SArchie Cobbs 	  "ascii2binary",
516f8307e12SArchie Cobbs 	  &ng_parse_ng_mesg_type,
517f8307e12SArchie Cobbs 	  &ng_parse_ng_mesg_type
518f8307e12SArchie Cobbs 	},
519f8307e12SArchie Cobbs 	{
520f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
521f8307e12SArchie Cobbs 	  NGM_BINARY2ASCII,
522f8307e12SArchie Cobbs 	  "binary2ascii",
523f8307e12SArchie Cobbs 	  &ng_parse_ng_mesg_type,
524f8307e12SArchie Cobbs 	  &ng_parse_ng_mesg_type
525f8307e12SArchie Cobbs 	},
526f8307e12SArchie Cobbs 	{ 0 }
527f8307e12SArchie Cobbs };
528f8307e12SArchie Cobbs 
529f8307e12SArchie Cobbs /************************************************************************
5304cf49a43SJulian Elischer 			Node routines
5314cf49a43SJulian Elischer ************************************************************************/
5324cf49a43SJulian Elischer 
5334cf49a43SJulian Elischer /*
5344cf49a43SJulian Elischer  * Instantiate a node of the requested type
5354cf49a43SJulian Elischer  */
5364cf49a43SJulian Elischer int
5374cf49a43SJulian Elischer ng_make_node(const char *typename, node_p *nodepp)
5384cf49a43SJulian Elischer {
5394cf49a43SJulian Elischer 	struct ng_type *type;
540069154d5SJulian Elischer 	int	error;
5414cf49a43SJulian Elischer 
5424cf49a43SJulian Elischer 	/* Check that the type makes sense */
5434cf49a43SJulian Elischer 	if (typename == NULL) {
5446b795970SJulian Elischer 		TRAP_ERROR();
5454cf49a43SJulian Elischer 		return (EINVAL);
5464cf49a43SJulian Elischer 	}
5474cf49a43SJulian Elischer 
5484cf49a43SJulian Elischer 	/* Locate the node type */
5494cf49a43SJulian Elischer 	if ((type = ng_findtype(typename)) == NULL) {
550cc3daab5SPeter Wemm 		char filename[NG_TYPELEN + 4];
5514cf49a43SJulian Elischer 		linker_file_t lf;
5524cf49a43SJulian Elischer 		int error;
5534cf49a43SJulian Elischer 
5544cf49a43SJulian Elischer 		/* Not found, try to load it as a loadable module */
555453b5565SJulian Elischer 		snprintf(filename, sizeof(filename), "ng_%s", typename);
556cc3daab5SPeter Wemm 		error = linker_load_file(filename, &lf);
5574cf49a43SJulian Elischer 		if (error != 0)
5584cf49a43SJulian Elischer 			return (error);
5594cf49a43SJulian Elischer 		lf->userrefs++;		/* pretend loaded by the syscall */
5604cf49a43SJulian Elischer 
5614cf49a43SJulian Elischer 		/* Try again, as now the type should have linked itself in */
5624cf49a43SJulian Elischer 		if ((type = ng_findtype(typename)) == NULL)
5634cf49a43SJulian Elischer 			return (ENXIO);
5644cf49a43SJulian Elischer 	}
5654cf49a43SJulian Elischer 
566069154d5SJulian Elischer 	/*
567069154d5SJulian Elischer 	 * If we have a constructor, then make the node and
568069154d5SJulian Elischer 	 * call the constructor to do type specific initialisation.
569069154d5SJulian Elischer 	 */
570069154d5SJulian Elischer 	if (type->constructor != NULL) {
571069154d5SJulian Elischer 		if ((error = ng_make_node_common(type, nodepp)) == 0) {
572069154d5SJulian Elischer 			if ((error = ((*type->constructor)(*nodepp)) != 0)) {
57330400f03SJulian Elischer 				NG_NODE_UNREF(*nodepp);
574069154d5SJulian Elischer 			}
575069154d5SJulian Elischer 		}
576069154d5SJulian Elischer 	} else {
577069154d5SJulian Elischer 		/*
578069154d5SJulian Elischer 		 * Node has no constructor. We cannot ask for one
579069154d5SJulian Elischer 		 * to be made. It must be brought into existance by
580954c4772SJulian Elischer 		 * some external agency. The external agency should
581069154d5SJulian Elischer 		 * call ng_make_node_common() directly to get the
582069154d5SJulian Elischer 		 * netgraph part initialised.
583069154d5SJulian Elischer 		 */
5846b795970SJulian Elischer 		TRAP_ERROR();
585069154d5SJulian Elischer 		error = EINVAL;
586069154d5SJulian Elischer 	}
587069154d5SJulian Elischer 	return (error);
5884cf49a43SJulian Elischer }
5894cf49a43SJulian Elischer 
5904cf49a43SJulian Elischer /*
591069154d5SJulian Elischer  * Generic node creation. Called by node initialisation for externally
592069154d5SJulian Elischer  * instantiated nodes (e.g. hardware, sockets, etc ).
5934cf49a43SJulian Elischer  * The returned node has a reference count of 1.
5944cf49a43SJulian Elischer  */
5954cf49a43SJulian Elischer int
5964cf49a43SJulian Elischer ng_make_node_common(struct ng_type *type, node_p *nodepp)
5974cf49a43SJulian Elischer {
5984cf49a43SJulian Elischer 	node_p node;
5994cf49a43SJulian Elischer 
6004cf49a43SJulian Elischer 	/* Require the node type to have been already installed */
6014cf49a43SJulian Elischer 	if (ng_findtype(type->name) == NULL) {
6026b795970SJulian Elischer 		TRAP_ERROR();
6034cf49a43SJulian Elischer 		return (EINVAL);
6044cf49a43SJulian Elischer 	}
6054cf49a43SJulian Elischer 
6064cf49a43SJulian Elischer 	/* Make a node and try attach it to the type */
60730400f03SJulian Elischer 	NG_ALLOC_NODE(node);
6084cf49a43SJulian Elischer 	if (node == NULL) {
6096b795970SJulian Elischer 		TRAP_ERROR();
6104cf49a43SJulian Elischer 		return (ENOMEM);
6114cf49a43SJulian Elischer 	}
61230400f03SJulian Elischer 	node->nd_type = type;
61330400f03SJulian Elischer 	NG_NODE_REF(node);				/* note reference */
6144cf49a43SJulian Elischer 	type->refs++;
6154cf49a43SJulian Elischer 
61695012d38SJulian Elischer 	mtx_init(&node->nd_input_queue.q_mtx, "netgraph node mutex", MTX_SPIN);
61730400f03SJulian Elischer 	node->nd_input_queue.queue = NULL;
61830400f03SJulian Elischer 	node->nd_input_queue.last = &node->nd_input_queue.queue;
61930400f03SJulian Elischer 	node->nd_input_queue.q_flags = 0;
62030400f03SJulian Elischer 	node->nd_input_queue.q_node = node;
6214cf49a43SJulian Elischer 
6224cf49a43SJulian Elischer 	/* Initialize hook list for new node */
62330400f03SJulian Elischer 	LIST_INIT(&node->nd_hooks);
6244cf49a43SJulian Elischer 
625069154d5SJulian Elischer 	/* Link us into the node linked list */
626069154d5SJulian Elischer 	mtx_enter(&ng_nodelist_mtx, MTX_DEF);
62730400f03SJulian Elischer 	LIST_INSERT_HEAD(&ng_nodelist, node, nd_nodes);
628069154d5SJulian Elischer 	mtx_exit(&ng_nodelist_mtx, MTX_DEF);
629069154d5SJulian Elischer 
630069154d5SJulian Elischer 
631dc90cad9SJulian Elischer 	/* get an ID and put us in the hash chain */
632069154d5SJulian Elischer 	mtx_enter(&ng_idhash_mtx, MTX_DEF);
63330400f03SJulian Elischer 	for (;;) { /* wrap protection, even if silly */
634069154d5SJulian Elischer 		node_p node2 = NULL;
63530400f03SJulian Elischer 		node->nd_ID = nextID++; /* 137/second for 1 year before wrap */
6360f150d04SJulian Elischer 
63730400f03SJulian Elischer 		/* Is there a problem with the new number? */
6380f150d04SJulian Elischer 		NG_IDHASH_FIND(node->nd_ID, node2); /* already taken? */
6390f150d04SJulian Elischer 		if ((node->nd_ID != 0) && (node2 == NULL)) {
64030400f03SJulian Elischer 			break;
641069154d5SJulian Elischer 		}
64230400f03SJulian Elischer 	}
6430f150d04SJulian Elischer 	LIST_INSERT_HEAD(&ng_ID_hash[NG_IDHASH_FN(node->nd_ID)],
64430400f03SJulian Elischer 							node, nd_idnodes);
645069154d5SJulian Elischer 	mtx_exit(&ng_idhash_mtx, MTX_DEF);
646dc90cad9SJulian Elischer 
6474cf49a43SJulian Elischer 	/* Done */
6484cf49a43SJulian Elischer 	*nodepp = node;
6494cf49a43SJulian Elischer 	return (0);
6504cf49a43SJulian Elischer }
6514cf49a43SJulian Elischer 
6524cf49a43SJulian Elischer /*
6534cf49a43SJulian Elischer  * Forceably start the shutdown process on a node. Either call
6544cf49a43SJulian Elischer  * it's shutdown method, or do the default shutdown if there is
6554cf49a43SJulian Elischer  * no type-specific method.
6564cf49a43SJulian Elischer  *
657069154d5SJulian Elischer  * We can only be called form a shutdown message, so we know we have
6583e4084c8SJulian Elischer  * a writer lock, and therefore exclusive access. It also means
6593e4084c8SJulian Elischer  * that we should not be on the work queue, but we check anyhow.
660069154d5SJulian Elischer  *
661069154d5SJulian Elischer  * Persistent node types must have a type-specific method which
6623e4084c8SJulian Elischer  * Allocates a new node in which case, this one is irretrievably going away,
6633e4084c8SJulian Elischer  * or cleans up anything it needs, and just makes the node valid again,
6643e4084c8SJulian Elischer  * in which case we allow the node to survive.
6653e4084c8SJulian Elischer  *
6663e4084c8SJulian Elischer  * XXX We need to think of how to tell a persistant node that we
6673e4084c8SJulian Elischer  * REALLY need to go away because the hardware has gone or we
6683e4084c8SJulian Elischer  * are rebooting.... etc.
6694cf49a43SJulian Elischer  */
6704cf49a43SJulian Elischer void
6714cf49a43SJulian Elischer ng_rmnode(node_p node)
6724cf49a43SJulian Elischer {
6733e4084c8SJulian Elischer 	hook_p hook;
6743e4084c8SJulian Elischer 
6754cf49a43SJulian Elischer 	/* Check if it's already shutting down */
67630400f03SJulian Elischer 	if ((node->nd_flags & NG_CLOSING) != 0)
6774cf49a43SJulian Elischer 		return;
6784cf49a43SJulian Elischer 
6794cf49a43SJulian Elischer 	/* Add an extra reference so it doesn't go away during this */
68030400f03SJulian Elischer 	NG_NODE_REF(node);
6814cf49a43SJulian Elischer 
68230400f03SJulian Elischer 	/*
68330400f03SJulian Elischer 	 * Mark it invalid so any newcomers know not to try use it
68430400f03SJulian Elischer 	 * Also add our own mark so we can't recurse
68530400f03SJulian Elischer 	 * note that NG_INVALID does not do this as it's also set during
68630400f03SJulian Elischer 	 * creation
68730400f03SJulian Elischer 	 */
68830400f03SJulian Elischer 	node->nd_flags |= NG_INVALID|NG_CLOSING;
6894cf49a43SJulian Elischer 
6903e4084c8SJulian Elischer 	/* Notify all remaining connected nodes to disconnect */
6913e4084c8SJulian Elischer 	while ((hook = LIST_FIRST(&node->nd_hooks)) != NULL)
6923e4084c8SJulian Elischer 		ng_destroy_hook(hook);
69330400f03SJulian Elischer 
694069154d5SJulian Elischer 	/*
695069154d5SJulian Elischer 	 * Drain the input queue forceably.
69630400f03SJulian Elischer 	 * it has no hooks so what's it going to do, bleed on someone?
69730400f03SJulian Elischer 	 * Theoretically we came here from a queue entry that was added
69830400f03SJulian Elischer 	 * Just before the queue was closed, so it should be empty anyway.
699069154d5SJulian Elischer 	 */
70030400f03SJulian Elischer 	ng_flush_input_queue(&node->nd_input_queue);
701069154d5SJulian Elischer 
702069154d5SJulian Elischer 	/*
703069154d5SJulian Elischer 	 * Take us off the work queue if we are there.
70430400f03SJulian Elischer 	 * We definatly have no work to be done.
705069154d5SJulian Elischer 	 */
706069154d5SJulian Elischer 	ng_worklist_remove(node);
707069154d5SJulian Elischer 
708069154d5SJulian Elischer 	/* Ask the type if it has anything to do in this case */
70930400f03SJulian Elischer 	if (node->nd_type && node->nd_type->shutdown) {
71030400f03SJulian Elischer 		(*node->nd_type->shutdown)(node);
711069154d5SJulian Elischer 	} else {				/* do the default thing */
71230400f03SJulian Elischer 		NG_NODE_UNREF(node);
71330400f03SJulian Elischer 	}
71430400f03SJulian Elischer 	if (NG_NODE_IS_VALID(node)) {
71530400f03SJulian Elischer 		/*
71630400f03SJulian Elischer 		 * Well, blow me down if the node code hasn't declared
71730400f03SJulian Elischer 		 * that it doesn't want to die.
71830400f03SJulian Elischer 		 * Presumably it is a persistant node.
71930400f03SJulian Elischer 		 * XXX we need a way to tell the node
72030400f03SJulian Elischer 		 * "No, really.. the hardware's going away.. REALLY die"
72130400f03SJulian Elischer 		 * We need a way
72230400f03SJulian Elischer 		 */
72330400f03SJulian Elischer 		return;
7244cf49a43SJulian Elischer 	}
7254cf49a43SJulian Elischer 
72630400f03SJulian Elischer 	ng_unname(node); /* basically a NOP these days */
72730400f03SJulian Elischer 
72830400f03SJulian Elischer 	/*
72930400f03SJulian Elischer 	 * Remove extra reference, possibly the last
73030400f03SJulian Elischer 	 * Possible other holders of references may include
73130400f03SJulian Elischer 	 * timeout callouts, but theoretically the node's supposed to
73230400f03SJulian Elischer 	 * have cancelled them. Possibly hardware dependencies may
73330400f03SJulian Elischer 	 * force a driver to 'linger' with a reference.
73430400f03SJulian Elischer 	 */
73530400f03SJulian Elischer 	NG_NODE_UNREF(node);
7364cf49a43SJulian Elischer }
7374cf49a43SJulian Elischer 
7384cf49a43SJulian Elischer /*
7394cf49a43SJulian Elischer  * Remove a reference to the node, possibly the last
7404cf49a43SJulian Elischer  */
7414cf49a43SJulian Elischer void
74230400f03SJulian Elischer ng_unref_node(node_p node)
7434cf49a43SJulian Elischer {
74430400f03SJulian Elischer 	int     v;
7456b795970SJulian Elischer 
7466b795970SJulian Elischer 	if (node == &ng_deadnode) {
7476b795970SJulian Elischer 		return;
7486b795970SJulian Elischer 	}
7496b795970SJulian Elischer 
75030400f03SJulian Elischer 	do {
75130400f03SJulian Elischer 		v = node->nd_refs;
75230400f03SJulian Elischer 	} while (! atomic_cmpset_int(&node->nd_refs, v, v - 1));
753e8a49db2SJulian Elischer 
75430400f03SJulian Elischer 	if (v == 1) { /* we were the last */
755069154d5SJulian Elischer 
756069154d5SJulian Elischer 		mtx_enter(&ng_nodelist_mtx, MTX_DEF);
75730400f03SJulian Elischer 		node->nd_type->refs--; /* XXX maybe should get types lock? */
75830400f03SJulian Elischer 		LIST_REMOVE(node, nd_nodes);
759069154d5SJulian Elischer 		mtx_exit(&ng_nodelist_mtx, MTX_DEF);
760069154d5SJulian Elischer 
761069154d5SJulian Elischer 		mtx_enter(&ng_idhash_mtx, MTX_DEF);
76230400f03SJulian Elischer 		LIST_REMOVE(node, nd_idnodes);
763069154d5SJulian Elischer 		mtx_exit(&ng_idhash_mtx, MTX_DEF);
764069154d5SJulian Elischer 
76512574a02SJulian Elischer 		mtx_destroy(&node->nd_input_queue.q_mtx);
766069154d5SJulian Elischer 		NG_FREE_NODE(node);
7674cf49a43SJulian Elischer 	}
7684cf49a43SJulian Elischer }
7694cf49a43SJulian Elischer 
7704cf49a43SJulian Elischer /************************************************************************
771dc90cad9SJulian Elischer 			Node ID handling
772dc90cad9SJulian Elischer ************************************************************************/
773dc90cad9SJulian Elischer static node_p
774069154d5SJulian Elischer ng_ID2noderef(ng_ID_t ID)
775dc90cad9SJulian Elischer {
77630400f03SJulian Elischer 	node_p node;
777069154d5SJulian Elischer 	mtx_enter(&ng_idhash_mtx, MTX_DEF);
7780f150d04SJulian Elischer 	NG_IDHASH_FIND(ID, node);
77930400f03SJulian Elischer 	if(node)
78030400f03SJulian Elischer 		NG_NODE_REF(node);
781069154d5SJulian Elischer 	mtx_exit(&ng_idhash_mtx, MTX_DEF);
78230400f03SJulian Elischer 	return(node);
783dc90cad9SJulian Elischer }
784dc90cad9SJulian Elischer 
785dc90cad9SJulian Elischer ng_ID_t
786dc90cad9SJulian Elischer ng_node2ID(node_p node)
787dc90cad9SJulian Elischer {
78870de87f2SJulian Elischer 	return (node ? NG_NODE_ID(node) : 0);
789dc90cad9SJulian Elischer }
790dc90cad9SJulian Elischer 
791dc90cad9SJulian Elischer /************************************************************************
7924cf49a43SJulian Elischer 			Node name handling
7934cf49a43SJulian Elischer ************************************************************************/
7944cf49a43SJulian Elischer 
7954cf49a43SJulian Elischer /*
7964cf49a43SJulian Elischer  * Assign a node a name. Once assigned, the name cannot be changed.
7974cf49a43SJulian Elischer  */
7984cf49a43SJulian Elischer int
7994cf49a43SJulian Elischer ng_name_node(node_p node, const char *name)
8004cf49a43SJulian Elischer {
8014cf49a43SJulian Elischer 	int i;
802069154d5SJulian Elischer 	node_p node2;
8034cf49a43SJulian Elischer 
8044cf49a43SJulian Elischer 	/* Check the name is valid */
8054cf49a43SJulian Elischer 	for (i = 0; i < NG_NODELEN + 1; i++) {
8064cf49a43SJulian Elischer 		if (name[i] == '\0' || name[i] == '.' || name[i] == ':')
8074cf49a43SJulian Elischer 			break;
8084cf49a43SJulian Elischer 	}
8094cf49a43SJulian Elischer 	if (i == 0 || name[i] != '\0') {
8106b795970SJulian Elischer 		TRAP_ERROR();
8114cf49a43SJulian Elischer 		return (EINVAL);
8124cf49a43SJulian Elischer 	}
813dc90cad9SJulian Elischer 	if (ng_decodeidname(name) != 0) { /* valid IDs not allowed here */
8146b795970SJulian Elischer 		TRAP_ERROR();
8154cf49a43SJulian Elischer 		return (EINVAL);
8164cf49a43SJulian Elischer 	}
8174cf49a43SJulian Elischer 
8184cf49a43SJulian Elischer 	/* Check the name isn't already being used */
819069154d5SJulian Elischer 	if ((node2 = ng_name2noderef(node, name)) != NULL) {
82030400f03SJulian Elischer 		NG_NODE_UNREF(node2);
8216b795970SJulian Elischer 		TRAP_ERROR();
8224cf49a43SJulian Elischer 		return (EADDRINUSE);
8234cf49a43SJulian Elischer 	}
8244cf49a43SJulian Elischer 
825069154d5SJulian Elischer 	/* copy it */
82630400f03SJulian Elischer 	strncpy(NG_NODE_NAME(node), name, NG_NODELEN);
8274cf49a43SJulian Elischer 
8284cf49a43SJulian Elischer 	return (0);
8294cf49a43SJulian Elischer }
8304cf49a43SJulian Elischer 
8314cf49a43SJulian Elischer /*
8324cf49a43SJulian Elischer  * Find a node by absolute name. The name should NOT end with ':'
8334cf49a43SJulian Elischer  * The name "." means "this node" and "[xxx]" means "the node
8344cf49a43SJulian Elischer  * with ID (ie, at address) xxx".
8354cf49a43SJulian Elischer  *
8364cf49a43SJulian Elischer  * Returns the node if found, else NULL.
837069154d5SJulian Elischer  * Eventually should add something faster than a sequential search.
83830400f03SJulian Elischer  * Note it aquires a reference on the node so you can be sure it's still there.
8394cf49a43SJulian Elischer  */
8404cf49a43SJulian Elischer node_p
841069154d5SJulian Elischer ng_name2noderef(node_p here, const char *name)
8424cf49a43SJulian Elischer {
843dc90cad9SJulian Elischer 	node_p node;
844dc90cad9SJulian Elischer 	ng_ID_t temp;
8454cf49a43SJulian Elischer 
8464cf49a43SJulian Elischer 	/* "." means "this node" */
847069154d5SJulian Elischer 	if (strcmp(name, ".") == 0) {
84830400f03SJulian Elischer 		NG_NODE_REF(here);
849069154d5SJulian Elischer 		return(here);
850069154d5SJulian Elischer 	}
8514cf49a43SJulian Elischer 
8524cf49a43SJulian Elischer 	/* Check for name-by-ID */
853dc90cad9SJulian Elischer 	if ((temp = ng_decodeidname(name)) != 0) {
854069154d5SJulian Elischer 		return (ng_ID2noderef(temp));
8554cf49a43SJulian Elischer 	}
8564cf49a43SJulian Elischer 
8574cf49a43SJulian Elischer 	/* Find node by name */
858069154d5SJulian Elischer 	mtx_enter(&ng_nodelist_mtx, MTX_DEF);
85930400f03SJulian Elischer 	LIST_FOREACH(node, &ng_nodelist, nd_nodes) {
86070de87f2SJulian Elischer 		if (NG_NODE_IS_VALID(node)
86170de87f2SJulian Elischer 		&& NG_NODE_HAS_NAME(node)
86270de87f2SJulian Elischer 		&& (strcmp(NG_NODE_NAME(node), name) == 0)) {
8634cf49a43SJulian Elischer 			break;
8644cf49a43SJulian Elischer 		}
86570de87f2SJulian Elischer 	}
866069154d5SJulian Elischer 	if (node)
86730400f03SJulian Elischer 		NG_NODE_REF(node);
868069154d5SJulian Elischer 	mtx_exit(&ng_nodelist_mtx, MTX_DEF);
8694cf49a43SJulian Elischer 	return (node);
8704cf49a43SJulian Elischer }
8714cf49a43SJulian Elischer 
8724cf49a43SJulian Elischer /*
873dc90cad9SJulian Elischer  * Decode a ID name, eg. "[f03034de]". Returns 0 if the
874dc90cad9SJulian Elischer  * string is not valid, otherwise returns the value.
8754cf49a43SJulian Elischer  */
876dc90cad9SJulian Elischer static ng_ID_t
8774cf49a43SJulian Elischer ng_decodeidname(const char *name)
8784cf49a43SJulian Elischer {
8792b70adcbSArchie Cobbs 	const int len = strlen(name);
88025792ef3SArchie Cobbs 	char *eptr;
8812b70adcbSArchie Cobbs 	u_long val;
8824cf49a43SJulian Elischer 
8832b70adcbSArchie Cobbs 	/* Check for proper length, brackets, no leading junk */
88470de87f2SJulian Elischer 	if ((len < 3)
88570de87f2SJulian Elischer 	|| (name[0] != '[')
88670de87f2SJulian Elischer 	|| (name[len - 1] != ']')
88770de87f2SJulian Elischer 	|| (!isxdigit(name[1]))) {
88870de87f2SJulian Elischer 		return ((ng_ID_t)0);
88970de87f2SJulian Elischer 	}
8904cf49a43SJulian Elischer 
8912b70adcbSArchie Cobbs 	/* Decode number */
8922b70adcbSArchie Cobbs 	val = strtoul(name + 1, &eptr, 16);
89370de87f2SJulian Elischer 	if ((eptr - name != len - 1)
89470de87f2SJulian Elischer 	|| (val == ULONG_MAX)
89570de87f2SJulian Elischer 	|| (val == 0)) {
89612f035e0SJulian Elischer 		return ((ng_ID_t)0);
89770de87f2SJulian Elischer 	}
8982b70adcbSArchie Cobbs 	return (ng_ID_t)val;
8994cf49a43SJulian Elischer }
9004cf49a43SJulian Elischer 
9014cf49a43SJulian Elischer /*
9024cf49a43SJulian Elischer  * Remove a name from a node. This should only be called
9034cf49a43SJulian Elischer  * when shutting down and removing the node.
9044cf49a43SJulian Elischer  */
9054cf49a43SJulian Elischer void
9064cf49a43SJulian Elischer ng_unname(node_p node)
9074cf49a43SJulian Elischer {
90830400f03SJulian Elischer 	bzero(NG_NODE_NAME(node), NG_NODELEN);
9094cf49a43SJulian Elischer }
9104cf49a43SJulian Elischer 
9114cf49a43SJulian Elischer /************************************************************************
9124cf49a43SJulian Elischer 			Hook routines
9134cf49a43SJulian Elischer  Names are not optional. Hooks are always connected, except for a
9143e4084c8SJulian Elischer  brief moment within these routines. On invalidation or during creation
9153e4084c8SJulian Elischer  they are connected to the 'dead' hook.
9164cf49a43SJulian Elischer ************************************************************************/
9174cf49a43SJulian Elischer 
9184cf49a43SJulian Elischer /*
9194cf49a43SJulian Elischer  * Remove a hook reference
9204cf49a43SJulian Elischer  */
92130400f03SJulian Elischer void
9224cf49a43SJulian Elischer ng_unref_hook(hook_p hook)
9234cf49a43SJulian Elischer {
92430400f03SJulian Elischer 	int     v;
9256b795970SJulian Elischer 
9266b795970SJulian Elischer 	if (hook == &ng_deadhook) {
9276b795970SJulian Elischer 		return;
9286b795970SJulian Elischer 	}
92930400f03SJulian Elischer 	do {
93030400f03SJulian Elischer 		v = hook->hk_refs;
93130400f03SJulian Elischer 	} while (! atomic_cmpset_int(&hook->hk_refs, v, v - 1));
932e8a49db2SJulian Elischer 
93330400f03SJulian Elischer 	if (v == 1) { /* we were the last */
9346b795970SJulian Elischer 		if (_NG_HOOK_NODE(hook)) { /* it'll probably be ng_deadnode */
9356b795970SJulian Elischer 			_NG_NODE_UNREF((_NG_HOOK_NODE(hook)));
93630400f03SJulian Elischer 			hook->hk_node = NULL;
937069154d5SJulian Elischer 		}
938069154d5SJulian Elischer 		NG_FREE_HOOK(hook);
939069154d5SJulian Elischer 	}
9404cf49a43SJulian Elischer }
9414cf49a43SJulian Elischer 
9424cf49a43SJulian Elischer /*
9434cf49a43SJulian Elischer  * Add an unconnected hook to a node. Only used internally.
9443e4084c8SJulian Elischer  * Assumes node is locked. (XXX not yet true )
9454cf49a43SJulian Elischer  */
9464cf49a43SJulian Elischer static int
9474cf49a43SJulian Elischer ng_add_hook(node_p node, const char *name, hook_p *hookp)
9484cf49a43SJulian Elischer {
9494cf49a43SJulian Elischer 	hook_p hook;
9504cf49a43SJulian Elischer 	int error = 0;
9514cf49a43SJulian Elischer 
9524cf49a43SJulian Elischer 	/* Check that the given name is good */
9534cf49a43SJulian Elischer 	if (name == NULL) {
9546b795970SJulian Elischer 		TRAP_ERROR();
9554cf49a43SJulian Elischer 		return (EINVAL);
9564cf49a43SJulian Elischer 	}
957899e9c4eSArchie Cobbs 	if (ng_findhook(node, name) != NULL) {
9586b795970SJulian Elischer 		TRAP_ERROR();
9594cf49a43SJulian Elischer 		return (EEXIST);
9604cf49a43SJulian Elischer 	}
9614cf49a43SJulian Elischer 
9624cf49a43SJulian Elischer 	/* Allocate the hook and link it up */
96330400f03SJulian Elischer 	NG_ALLOC_HOOK(hook);
9644cf49a43SJulian Elischer 	if (hook == NULL) {
9656b795970SJulian Elischer 		TRAP_ERROR();
9664cf49a43SJulian Elischer 		return (ENOMEM);
9674cf49a43SJulian Elischer 	}
9683e4084c8SJulian Elischer 	hook->hk_refs = 1;		/* add a reference for us to return */
96930400f03SJulian Elischer 	hook->hk_flags = HK_INVALID;
9703e4084c8SJulian Elischer 	hook->hk_peer = &ng_deadhook;	/* start off this way */
97130400f03SJulian Elischer 	hook->hk_node = node;
97230400f03SJulian Elischer 	NG_NODE_REF(node);		/* each hook counts as a reference */
9734cf49a43SJulian Elischer 
9743e4084c8SJulian Elischer 	/* Set hook name */
9753e4084c8SJulian Elischer 	strncpy(NG_HOOK_NAME(hook), name, NG_HOOKLEN);
9763e4084c8SJulian Elischer 
9773e4084c8SJulian Elischer 	/*
9783e4084c8SJulian Elischer 	 * Check if the node type code has something to say about it
9793e4084c8SJulian Elischer 	 * If it fails, the unref of the hook will also unref the node.
9803e4084c8SJulian Elischer 	 */
981954c4772SJulian Elischer 	if (node->nd_type->newhook != NULL) {
982954c4772SJulian Elischer 		if ((error = (*node->nd_type->newhook)(node, hook, name))) {
98330400f03SJulian Elischer 			NG_HOOK_UNREF(hook);	/* this frees the hook */
984069154d5SJulian Elischer 			return (error);
985069154d5SJulian Elischer 		}
986954c4772SJulian Elischer 	}
9874cf49a43SJulian Elischer 	/*
9884cf49a43SJulian Elischer 	 * The 'type' agrees so far, so go ahead and link it in.
9894cf49a43SJulian Elischer 	 * We'll ask again later when we actually connect the hooks.
9904cf49a43SJulian Elischer 	 */
99130400f03SJulian Elischer 	LIST_INSERT_HEAD(&node->nd_hooks, hook, hk_hooks);
99230400f03SJulian Elischer 	node->nd_numhooks++;
9933e4084c8SJulian Elischer 	NG_HOOK_REF(hook);	/* one for the node */
9944cf49a43SJulian Elischer 
9954cf49a43SJulian Elischer 	if (hookp)
9964cf49a43SJulian Elischer 		*hookp = hook;
9973e4084c8SJulian Elischer 	return (0);
9984cf49a43SJulian Elischer }
9994cf49a43SJulian Elischer 
10004cf49a43SJulian Elischer /*
1001899e9c4eSArchie Cobbs  * Find a hook
1002899e9c4eSArchie Cobbs  *
1003899e9c4eSArchie Cobbs  * Node types may supply their own optimized routines for finding
1004899e9c4eSArchie Cobbs  * hooks.  If none is supplied, we just do a linear search.
10053e4084c8SJulian Elischer  * XXX Possibly we should add a reference to the hook?
1006899e9c4eSArchie Cobbs  */
1007899e9c4eSArchie Cobbs hook_p
1008899e9c4eSArchie Cobbs ng_findhook(node_p node, const char *name)
1009899e9c4eSArchie Cobbs {
1010899e9c4eSArchie Cobbs 	hook_p hook;
1011899e9c4eSArchie Cobbs 
101230400f03SJulian Elischer 	if (node->nd_type->findhook != NULL)
101330400f03SJulian Elischer 		return (*node->nd_type->findhook)(node, name);
101430400f03SJulian Elischer 	LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
101570de87f2SJulian Elischer 		if (NG_HOOK_IS_VALID(hook)
1016ce5e5f99SArchie Cobbs 		&& (strcmp(NG_HOOK_NAME(hook), name) == 0))
1017899e9c4eSArchie Cobbs 			return (hook);
1018899e9c4eSArchie Cobbs 	}
1019899e9c4eSArchie Cobbs 	return (NULL);
1020899e9c4eSArchie Cobbs }
1021899e9c4eSArchie Cobbs 
1022899e9c4eSArchie Cobbs /*
10234cf49a43SJulian Elischer  * Destroy a hook
10244cf49a43SJulian Elischer  *
10254cf49a43SJulian Elischer  * As hooks are always attached, this really destroys two hooks.
10264cf49a43SJulian Elischer  * The one given, and the one attached to it. Disconnect the hooks
10273e4084c8SJulian Elischer  * from each other first. We reconnect the peer hook to the 'dead'
10283e4084c8SJulian Elischer  * hook so that it can still exist after we depart. We then
10293e4084c8SJulian Elischer  * send the peer its own destroy message. This ensures that we only
10303e4084c8SJulian Elischer  * interact with the peer's structures when it is locked processing that
10313e4084c8SJulian Elischer  * message. We hold a reference to the peer hook so we are guaranteed that
10323e4084c8SJulian Elischer  * the peer hook and node are still going to exist until
10333e4084c8SJulian Elischer  * we are finished there as the hook holds a ref on the node.
10343e4084c8SJulian Elischer  * We run this same code again on the peer hook, but that time it is already
10353e4084c8SJulian Elischer  * attached to the 'dead' hook.
10366b795970SJulian Elischer  *
10376b795970SJulian Elischer  * This routine is called at all stages of hook creation
10386b795970SJulian Elischer  * on error detection and must be able to handle any such stage.
10394cf49a43SJulian Elischer  */
10404cf49a43SJulian Elischer void
10414cf49a43SJulian Elischer ng_destroy_hook(hook_p hook)
10424cf49a43SJulian Elischer {
104330400f03SJulian Elischer 	hook_p peer = NG_HOOK_PEER(hook);
10446b795970SJulian Elischer 	node_p node = NG_HOOK_NODE(hook);
10454cf49a43SJulian Elischer 
10466b795970SJulian Elischer 	if (hook == &ng_deadhook) {	/* better safe than sorry */
10476b795970SJulian Elischer 		printf("ng_destroy_hook called on deadhook\n");
10486b795970SJulian Elischer 		return;
10496b795970SJulian Elischer 	}
105030400f03SJulian Elischer 	hook->hk_flags |= HK_INVALID;		/* as soon as possible */
10513e4084c8SJulian Elischer 	if (peer && (peer != &ng_deadhook)) {
10523e4084c8SJulian Elischer 		/*
10533e4084c8SJulian Elischer 		 * Set the peer to point to ng_deadhook
10543e4084c8SJulian Elischer 		 * from this moment on we are effectively independent it.
10553e4084c8SJulian Elischer 		 * send it an rmhook message of it's own.
10563e4084c8SJulian Elischer 		 */
10573e4084c8SJulian Elischer 		peer->hk_peer = &ng_deadhook;	/* They no longer know us */
10583e4084c8SJulian Elischer 		hook->hk_peer = &ng_deadhook;	/* Nor us, them */
10596b795970SJulian Elischer 		if (NG_HOOK_NODE(peer) == &ng_deadnode) {
10606b795970SJulian Elischer 			/*
10616b795970SJulian Elischer 			 * If it's already divorced from a node,
10626b795970SJulian Elischer 			 * just free it.
10636b795970SJulian Elischer 			 */
10646b795970SJulian Elischer 			/* nothing */
10656b795970SJulian Elischer 		} else {
10666b795970SJulian Elischer 			ng_rmhook_self(peer); 	/* Send it a surprise */
10676b795970SJulian Elischer 		}
106852fa3556SJulian Elischer 		NG_HOOK_UNREF(peer);		/* account for peer link */
106952fa3556SJulian Elischer 		NG_HOOK_UNREF(hook);		/* account for peer link */
10704cf49a43SJulian Elischer 	}
10714cf49a43SJulian Elischer 
10724cf49a43SJulian Elischer 	/*
10734cf49a43SJulian Elischer 	 * Remove the hook from the node's list to avoid possible recursion
10744cf49a43SJulian Elischer 	 * in case the disconnection results in node shutdown.
10754cf49a43SJulian Elischer 	 */
10766b795970SJulian Elischer 	if (node == &ng_deadnode) { /* happens if called from ng_con_nodes() */
10776b795970SJulian Elischer 		return;
10786b795970SJulian Elischer 	}
107930400f03SJulian Elischer 	LIST_REMOVE(hook, hk_hooks);
108030400f03SJulian Elischer 	node->nd_numhooks--;
108130400f03SJulian Elischer 	if (node->nd_type->disconnect) {
10824cf49a43SJulian Elischer 		/*
10836b795970SJulian Elischer 		 * The type handler may elect to destroy the node so don't
10846b795970SJulian Elischer 		 * trust its existance after this point. (except
10856b795970SJulian Elischer 		 * that we still hold a reference on it. (which we
10866b795970SJulian Elischer 		 * inherrited from the hook we are destroying)
10874cf49a43SJulian Elischer 		 */
108830400f03SJulian Elischer 		(*node->nd_type->disconnect) (hook);
10894cf49a43SJulian Elischer 	}
10906b795970SJulian Elischer 
10916b795970SJulian Elischer 	/*
10926b795970SJulian Elischer 	 * Note that because we will point to ng_deadnode, the original node
10936b795970SJulian Elischer 	 * is not decremented automatically so we do that manually.
10946b795970SJulian Elischer 	 */
10956b795970SJulian Elischer 	_NG_HOOK_NODE(hook) = &ng_deadnode;
10966b795970SJulian Elischer 	NG_NODE_UNREF(node);	/* We no longer point to it so adjust count */
10976b795970SJulian Elischer 	NG_HOOK_UNREF(hook);	/* Account for linkage (in list) to node */
10984cf49a43SJulian Elischer }
10994cf49a43SJulian Elischer 
11004cf49a43SJulian Elischer /*
11014cf49a43SJulian Elischer  * Take two hooks on a node and merge the connection so that the given node
11024cf49a43SJulian Elischer  * is effectively bypassed.
11034cf49a43SJulian Elischer  */
11044cf49a43SJulian Elischer int
11054cf49a43SJulian Elischer ng_bypass(hook_p hook1, hook_p hook2)
11064cf49a43SJulian Elischer {
110730400f03SJulian Elischer 	if (hook1->hk_node != hook2->hk_node) {
11086b795970SJulian Elischer 		TRAP_ERROR();
11094cf49a43SJulian Elischer 		return (EINVAL);
111030400f03SJulian Elischer 	}
111130400f03SJulian Elischer 	hook1->hk_peer->hk_peer = hook2->hk_peer;
111230400f03SJulian Elischer 	hook2->hk_peer->hk_peer = hook1->hk_peer;
11134cf49a43SJulian Elischer 
11143e4084c8SJulian Elischer 	hook1->hk_peer = &ng_deadhook;
11153e4084c8SJulian Elischer 	hook2->hk_peer = &ng_deadhook;
11163e4084c8SJulian Elischer 
11174cf49a43SJulian Elischer 	/* XXX If we ever cache methods on hooks update them as well */
11184cf49a43SJulian Elischer 	ng_destroy_hook(hook1);
11194cf49a43SJulian Elischer 	ng_destroy_hook(hook2);
11204cf49a43SJulian Elischer 	return (0);
11214cf49a43SJulian Elischer }
11224cf49a43SJulian Elischer 
11234cf49a43SJulian Elischer /*
11244cf49a43SJulian Elischer  * Install a new netgraph type
11254cf49a43SJulian Elischer  */
11264cf49a43SJulian Elischer int
11274cf49a43SJulian Elischer ng_newtype(struct ng_type *tp)
11284cf49a43SJulian Elischer {
11294cf49a43SJulian Elischer 	const size_t namelen = strlen(tp->name);
11304cf49a43SJulian Elischer 
11314cf49a43SJulian Elischer 	/* Check version and type name fields */
1132589f6ed8SJulian Elischer 	if ((tp->version != NG_ABI_VERSION)
1133589f6ed8SJulian Elischer 	|| (namelen == 0)
1134589f6ed8SJulian Elischer 	|| (namelen > NG_TYPELEN)) {
11356b795970SJulian Elischer 		TRAP_ERROR();
11364cf49a43SJulian Elischer 		return (EINVAL);
11374cf49a43SJulian Elischer 	}
11384cf49a43SJulian Elischer 
11394cf49a43SJulian Elischer 	/* Check for name collision */
11404cf49a43SJulian Elischer 	if (ng_findtype(tp->name) != NULL) {
11416b795970SJulian Elischer 		TRAP_ERROR();
11424cf49a43SJulian Elischer 		return (EEXIST);
11434cf49a43SJulian Elischer 	}
11444cf49a43SJulian Elischer 
1145069154d5SJulian Elischer 
1146069154d5SJulian Elischer 	/* Link in new type */
1147069154d5SJulian Elischer 	mtx_enter(&ng_typelist_mtx, MTX_DEF);
1148069154d5SJulian Elischer 	LIST_INSERT_HEAD(&ng_typelist, tp, types);
1149c73b94a2SJulian Elischer 	tp->refs = 1;	/* first ref is linked list */
1150069154d5SJulian Elischer 	mtx_exit(&ng_typelist_mtx, MTX_DEF);
11514cf49a43SJulian Elischer 	return (0);
11524cf49a43SJulian Elischer }
11534cf49a43SJulian Elischer 
11544cf49a43SJulian Elischer /*
11554cf49a43SJulian Elischer  * Look for a type of the name given
11564cf49a43SJulian Elischer  */
11574cf49a43SJulian Elischer struct ng_type *
11584cf49a43SJulian Elischer ng_findtype(const char *typename)
11594cf49a43SJulian Elischer {
11604cf49a43SJulian Elischer 	struct ng_type *type;
11614cf49a43SJulian Elischer 
1162069154d5SJulian Elischer 	mtx_enter(&ng_typelist_mtx, MTX_DEF);
1163069154d5SJulian Elischer 	LIST_FOREACH(type, &ng_typelist, types) {
11644cf49a43SJulian Elischer 		if (strcmp(type->name, typename) == 0)
11654cf49a43SJulian Elischer 			break;
11664cf49a43SJulian Elischer 	}
1167069154d5SJulian Elischer 	mtx_exit(&ng_typelist_mtx, MTX_DEF);
11684cf49a43SJulian Elischer 	return (type);
11694cf49a43SJulian Elischer }
11704cf49a43SJulian Elischer 
11714cf49a43SJulian Elischer /************************************************************************
11724cf49a43SJulian Elischer 			Composite routines
11734cf49a43SJulian Elischer ************************************************************************/
11744cf49a43SJulian Elischer /*
11756b795970SJulian Elischer  * Connect two nodes using the specified hooks, using queued functions.
11764cf49a43SJulian Elischer  */
11776b795970SJulian Elischer static int
11786b795970SJulian Elischer ng_con_part3(node_p node, hook_p hook, void *arg1, int arg2)
11794cf49a43SJulian Elischer {
11806b795970SJulian Elischer 	int error = 0;
11814cf49a43SJulian Elischer 
11826b795970SJulian Elischer 	/*
11836b795970SJulian Elischer 	 * When we run, we know that the node 'node' is locked for us.
11846b795970SJulian Elischer 	 * Our caller has a reference on the hook.
11856b795970SJulian Elischer 	 * Our caller has a reference on the node.
11866b795970SJulian Elischer 	 * (In this case our caller is ng_apply_item() ).
11876b795970SJulian Elischer 	 * The peer hook has a reference on the hook.
11886b795970SJulian Elischer 	 */
11896b795970SJulian Elischer 	if (NG_HOOK_NODE(hook) == &ng_deadnode) {
11906b795970SJulian Elischer 		/*
11916b795970SJulian Elischer 		 * The node must have been freed again since we last visited
11926b795970SJulian Elischer 		 * here. ng_destry_hook() has this effect but nothing else does.
11936b795970SJulian Elischer 		 * We should just release our references and
11946b795970SJulian Elischer 		 * free anything we can think of.
11956b795970SJulian Elischer 		 * Since we know it's been destroyed, and it's our caller
11966b795970SJulian Elischer 		 * that holds the references, just return.
11976b795970SJulian Elischer 		 */
11986b795970SJulian Elischer 		return (0);
11996b795970SJulian Elischer 	}
12006b795970SJulian Elischer 	if (hook->hk_node->nd_type->connect) {
12016b795970SJulian Elischer 		if ((error = (*hook->hk_node->nd_type->connect) (hook))) {
12026b795970SJulian Elischer 			ng_destroy_hook(hook);	/* also zaps peer */
12034cf49a43SJulian Elischer 			return (error);
12044cf49a43SJulian Elischer 		}
12056b795970SJulian Elischer 	}
12066b795970SJulian Elischer 	/*
12076b795970SJulian Elischer 	 *  XXX this is wrong for SMP. Possibly we need
12086b795970SJulian Elischer 	 * to separate out 'create' and 'invalid' flags.
12096b795970SJulian Elischer 	 * should only set flags on hooks we have locked under our node.
12106b795970SJulian Elischer 	 */
12116b795970SJulian Elischer 	hook->hk_flags &= ~HK_INVALID;
12126b795970SJulian Elischer 	return (error);
12136b795970SJulian Elischer }
12146b795970SJulian Elischer 
12156b795970SJulian Elischer static int
12166b795970SJulian Elischer ng_con_part2(node_p node, hook_p hook, void *arg1, int arg2)
12176b795970SJulian Elischer {
12186b795970SJulian Elischer 	int error = 0;
12196b795970SJulian Elischer 
12206b795970SJulian Elischer 	/*
12216b795970SJulian Elischer 	 * When we run, we know that the node 'node' is locked for us.
12226b795970SJulian Elischer 	 * Our caller has a reference on the hook.
12236b795970SJulian Elischer 	 * Our caller has a reference on the node.
12246b795970SJulian Elischer 	 * (In this case our caller is ng_apply_item() ).
12256b795970SJulian Elischer 	 * The peer hook has a reference on the hook.
12266b795970SJulian Elischer 	 * our node pointer points to the 'dead' node.
12276b795970SJulian Elischer 	 * First check the hook name is unique.
12286b795970SJulian Elischer 	 */
12296b795970SJulian Elischer 	if (ng_findhook(node, NG_HOOK_NAME(hook)) != NULL) {
12306b795970SJulian Elischer 		TRAP_ERROR();
12316b795970SJulian Elischer 		ng_destroy_hook(hook); /* should destroy peer too */
12326b795970SJulian Elischer 		return (EEXIST);
12336b795970SJulian Elischer 	}
12346b795970SJulian Elischer 	/*
12356b795970SJulian Elischer 	 * Check if the node type code has something to say about it
12366b795970SJulian Elischer 	 * If it fails, the unref of the hook will also unref the attached node,
12376b795970SJulian Elischer 	 * however since that node is 'ng_deadnode' this will do nothing.
12386b795970SJulian Elischer 	 * The peer hook will also be destroyed.
12396b795970SJulian Elischer 	 */
12406b795970SJulian Elischer 	if (node->nd_type->newhook != NULL) {
12416b795970SJulian Elischer 		if ((error =
12426b795970SJulian Elischer 		    (*node->nd_type->newhook)(node, hook, hook->hk_name))) {
12436b795970SJulian Elischer 			ng_destroy_hook(hook); /* should destroy peer too */
12446b795970SJulian Elischer 			return (error);
12456b795970SJulian Elischer 		}
12466b795970SJulian Elischer 	}
12476b795970SJulian Elischer 
12486b795970SJulian Elischer 	/*
12496b795970SJulian Elischer 	 * The 'type' agrees so far, so go ahead and link it in.
12506b795970SJulian Elischer 	 * We'll ask again later when we actually connect the hooks.
12516b795970SJulian Elischer 	 */
12526b795970SJulian Elischer 	hook->hk_node = node;		/* just overwrite ng_deadnode */
12536b795970SJulian Elischer 	NG_NODE_REF(node);		/* each hook counts as a reference */
12546b795970SJulian Elischer 	LIST_INSERT_HEAD(&node->nd_hooks, hook, hk_hooks);
12556b795970SJulian Elischer 	node->nd_numhooks++;
12566b795970SJulian Elischer 	NG_HOOK_REF(hook);	/* one for the node */
12576b795970SJulian Elischer 
12586b795970SJulian Elischer 	/*
12596b795970SJulian Elischer 	 * We now have a symetrical situation, where both hooks have been
12606b795970SJulian Elischer 	 * linked to theur nodes, the newhook methods have been called
12616b795970SJulian Elischer 	 * And the references are all correct. The hooks are still marked
12626b795970SJulian Elischer 	 * as invalid, as we have not called the 'connect' methods
12636b795970SJulian Elischer 	 * yet.
12646b795970SJulian Elischer 	 * We can call the local one immediatly as we have the
12656b795970SJulian Elischer 	 * node locked, but we need to queue the remote one.
12666b795970SJulian Elischer 	 */
12676b795970SJulian Elischer 	if (hook->hk_node->nd_type->connect) {
12686b795970SJulian Elischer 		if ((error = (*hook->hk_node->nd_type->connect) (hook))) {
12696b795970SJulian Elischer 			ng_destroy_hook(hook);	/* also zaps peer */
12706b795970SJulian Elischer 			return (error);
12716b795970SJulian Elischer 		}
12726b795970SJulian Elischer 	}
12736b795970SJulian Elischer 	error = ng_send_fn(hook->hk_peer->hk_node, hook->hk_peer,
12746b795970SJulian Elischer 			&ng_con_part3, arg1, arg2);
12756b795970SJulian Elischer 	hook->hk_flags &= ~HK_INVALID; /* need both to be able to work */
12764cf49a43SJulian Elischer 	return (error);
12774cf49a43SJulian Elischer }
12784cf49a43SJulian Elischer 
12794cf49a43SJulian Elischer /*
12806b795970SJulian Elischer  * Connect this node with another node. We assume that this node is
12816b795970SJulian Elischer  * currently locked, as we are only called from an NGM_CONNECT message.
12824cf49a43SJulian Elischer  */
12836b795970SJulian Elischer static int
12844cf49a43SJulian Elischer ng_con_nodes(node_p node, const char *name, node_p node2, const char *name2)
12854cf49a43SJulian Elischer {
12864cf49a43SJulian Elischer 	int     error;
12874cf49a43SJulian Elischer 	hook_p  hook;
12884cf49a43SJulian Elischer 	hook_p  hook2;
12894cf49a43SJulian Elischer 
12903e4084c8SJulian Elischer 	if ((error = ng_add_hook(node, name, &hook)))  /* gives us a ref */
12914cf49a43SJulian Elischer 		return (error);
12926b795970SJulian Elischer 	/* Allocate the other hook and link it up */
12936b795970SJulian Elischer 	NG_ALLOC_HOOK(hook2);
12946b795970SJulian Elischer 	if (hook == NULL) {
12956b795970SJulian Elischer 		TRAP_ERROR();
12966b795970SJulian Elischer 		ng_destroy_hook(hook);	/* XXX check ref counts so far */
12976b795970SJulian Elischer 		NG_HOOK_UNREF(hook);	/* including our ref */
12986b795970SJulian Elischer 		return (ENOMEM);
12996b795970SJulian Elischer 	}
13006b795970SJulian Elischer 	hook2->hk_refs = 1;		/* start with a reference for us. */
13016b795970SJulian Elischer 	hook2->hk_flags = HK_INVALID;
13026b795970SJulian Elischer 	hook2->hk_peer = hook;		/* Link the two together */
13036b795970SJulian Elischer 	hook->hk_peer = hook2;
13046b795970SJulian Elischer 	NG_HOOK_REF(hook);		/* Add a ref for the peer to each*/
13056b795970SJulian Elischer 	NG_HOOK_REF(hook2);
13066b795970SJulian Elischer 	hook2->hk_node = &ng_deadnode;
13076b795970SJulian Elischer 	strncpy(NG_HOOK_NAME(hook2), name2, NG_HOOKLEN);
13086b795970SJulian Elischer 
13096b795970SJulian Elischer 	/*
13106b795970SJulian Elischer 	 * Queue the function above.
13116b795970SJulian Elischer 	 * Procesing continues in that function in the lock context of
13126b795970SJulian Elischer 	 * the other node.
13136b795970SJulian Elischer 	 */
13146b795970SJulian Elischer 	error = ng_send_fn(node2, hook2, &ng_con_part2, NULL, 0);
13156b795970SJulian Elischer 
13166b795970SJulian Elischer 	NG_HOOK_UNREF(hook);		/* Let each hook go if it wants to */
13176b795970SJulian Elischer 	NG_HOOK_UNREF(hook2);
13184cf49a43SJulian Elischer 	return (error);
13194cf49a43SJulian Elischer }
13206b795970SJulian Elischer 
13216b795970SJulian Elischer /*
13226b795970SJulian Elischer  * Make a peer and connect.
13236b795970SJulian Elischer  * We assume that the local node is locked.
13246b795970SJulian Elischer  * The new node probably doesn't need a lock until
13256b795970SJulian Elischer  * it has a hook, because it cannot really have any work until then,
13266b795970SJulian Elischer  * but we should think about it a bit more.
13276b795970SJulian Elischer  *
13286b795970SJulian Elischer  * The problem may come if the other node also fires up
13296b795970SJulian Elischer  * some hardware or a timer or some other source of activation,
13306b795970SJulian Elischer  * also it may already get a command msg via it's ID.
13316b795970SJulian Elischer  *
13326b795970SJulian Elischer  * We could use the same method as ng_con_nodes() but we'd have
13336b795970SJulian Elischer  * to add ability to remove the node when failing. (Not hard, just
13346b795970SJulian Elischer  * make arg1 point to the node to remove).
13356b795970SJulian Elischer  * Unless of course we just ignore failure to connect and leave
13366b795970SJulian Elischer  * an unconnected node?
13376b795970SJulian Elischer  */
13386b795970SJulian Elischer static int
13396b795970SJulian Elischer ng_mkpeer(node_p node, const char *name, const char *name2, char *type)
13406b795970SJulian Elischer {
13416b795970SJulian Elischer 	node_p  node2;
13426b795970SJulian Elischer 	hook_p  hook1;
13436b795970SJulian Elischer 	hook_p  hook2;
13446b795970SJulian Elischer 	int     error;
13456b795970SJulian Elischer 
13466b795970SJulian Elischer 	if ((error = ng_make_node(type, &node2))) {
13476b795970SJulian Elischer 		return (error);
13486b795970SJulian Elischer 	}
13496b795970SJulian Elischer 
13506b795970SJulian Elischer 	if ((error = ng_add_hook(node, name, &hook1))) { /* gives us a ref */
13516b795970SJulian Elischer 		ng_rmnode(node2);
13526b795970SJulian Elischer 		return (error);
13536b795970SJulian Elischer 	}
13546b795970SJulian Elischer 
13556b795970SJulian Elischer 	if ((error = ng_add_hook(node2, name2, &hook2))) {
13566b795970SJulian Elischer 		ng_rmnode(node2);
13576b795970SJulian Elischer 		ng_destroy_hook(hook1);
13586b795970SJulian Elischer 		NG_HOOK_UNREF(hook1);
13596b795970SJulian Elischer 		return (error);
13606b795970SJulian Elischer 	}
13616b795970SJulian Elischer 
13626b795970SJulian Elischer 	/*
13636b795970SJulian Elischer 	 * Actually link the two hooks together.
13646b795970SJulian Elischer 	 */
13656b795970SJulian Elischer 	hook1->hk_peer = hook2;
13666b795970SJulian Elischer 	hook2->hk_peer = hook1;
13676b795970SJulian Elischer 
13686b795970SJulian Elischer 	/* Each hook is referenced by the other */
13696b795970SJulian Elischer 	NG_HOOK_REF(hook1);
13706b795970SJulian Elischer 	NG_HOOK_REF(hook2);
13716b795970SJulian Elischer 
13726b795970SJulian Elischer 	/* Give each node the opportunity to veto the pending connection */
13736b795970SJulian Elischer 	if (hook1->hk_node->nd_type->connect) {
13746b795970SJulian Elischer 		error = (*hook1->hk_node->nd_type->connect) (hook1);
13756b795970SJulian Elischer 	}
13766b795970SJulian Elischer 
13776b795970SJulian Elischer 	if ((error == 0) && hook2->hk_node->nd_type->connect) {
13786b795970SJulian Elischer 		error = (*hook2->hk_node->nd_type->connect) (hook2);
13796b795970SJulian Elischer 
13806b795970SJulian Elischer 	}
13813e4084c8SJulian Elischer 
13823e4084c8SJulian Elischer 	/*
13833e4084c8SJulian Elischer 	 * drop the references we were holding on the two hooks.
13843e4084c8SJulian Elischer 	 */
13856b795970SJulian Elischer 	if (error) {
13866b795970SJulian Elischer 		ng_destroy_hook(hook2);	/* also zaps hook1 */
13876b795970SJulian Elischer 		ng_rmnode(node2);
13886b795970SJulian Elischer 	} else {
13896b795970SJulian Elischer 		/* As a last act, allow the hooks to be used */
13906b795970SJulian Elischer 		hook1->hk_flags &= ~HK_INVALID;
13916b795970SJulian Elischer 		hook2->hk_flags &= ~HK_INVALID;
13926b795970SJulian Elischer 	}
13936b795970SJulian Elischer 	NG_HOOK_UNREF(hook1);
13943e4084c8SJulian Elischer 	NG_HOOK_UNREF(hook2);
13953e4084c8SJulian Elischer 	return (error);
13964cf49a43SJulian Elischer }
13976b795970SJulian Elischer 
1398069154d5SJulian Elischer /************************************************************************
1399069154d5SJulian Elischer 		Utility routines to send self messages
1400069154d5SJulian Elischer ************************************************************************/
1401069154d5SJulian Elischer /*
1402069154d5SJulian Elischer  * Static version of shutdown message. we don't want to need resources
1403069154d5SJulian Elischer  * to shut down (we may be doing it to release resources because we ran out.
1404069154d5SJulian Elischer  */
1405069154d5SJulian Elischer static struct	ng_mesg  ng_msg_shutdown = {
1406069154d5SJulian Elischer 	{NG_VERSION,		/* u_char */
1407069154d5SJulian Elischer 	0,			/* u_char spare */
1408069154d5SJulian Elischer 	0,			/* u_int16_t arglen */
1409069154d5SJulian Elischer 	NGF_STATIC,		/* u_int32_t flags */
1410069154d5SJulian Elischer 	0,			/* u_int32_t token */
1411069154d5SJulian Elischer 	NGM_GENERIC_COOKIE,	/* u_int32_t */
1412069154d5SJulian Elischer 	NGM_SHUTDOWN,		/* u_int32_t */
1413069154d5SJulian Elischer 	"shutdown"}		/* u_char[16] */
1414069154d5SJulian Elischer };
1415069154d5SJulian Elischer 
1416069154d5SJulian Elischer int
1417069154d5SJulian Elischer ng_rmnode_self(node_p here)
1418069154d5SJulian Elischer {
1419069154d5SJulian Elischer 	item_p	item;
1420069154d5SJulian Elischer 	struct	ng_mesg	*msg;
14214cf49a43SJulian Elischer 
14224cf49a43SJulian Elischer 	/*
1423069154d5SJulian Elischer 	 * Use the static version to avoid needing
1424069154d5SJulian Elischer 	 * memory allocation to succeed.
1425069154d5SJulian Elischer 	 * The message is never written to and always the same.
1426069154d5SJulian Elischer 	 */
1427069154d5SJulian Elischer 	msg = &ng_msg_shutdown;
1428069154d5SJulian Elischer 
1429069154d5SJulian Elischer 	/*
1430069154d5SJulian Elischer 	 * Try get a queue item to send it with.
1431069154d5SJulian Elischer 	 * Hopefully since it has a reserve, we can get one.
1432069154d5SJulian Elischer 	 * If we can't we are screwed anyhow.
1433069154d5SJulian Elischer 	 * Increase the chances by flushing our queue first.
1434069154d5SJulian Elischer 	 * We may free an item, (if we were the hog).
1435069154d5SJulian Elischer 	 * Work in progress is allowed to complete.
1436069154d5SJulian Elischer 	 * We also pretty much ensure that we come straight
1437069154d5SJulian Elischer 	 * back in to do the shutdown. It may be a good idea
1438069154d5SJulian Elischer 	 * to hold a reference actually to stop it from all
1439069154d5SJulian Elischer 	 * going up in smoke.
1440069154d5SJulian Elischer 	 */
144130400f03SJulian Elischer /*	ng_flush_input_queue(&here->nd_input_queue); will mask problem  */
1442069154d5SJulian Elischer 	item = ng_package_msg_self(here, NULL, msg);
1443069154d5SJulian Elischer 	if (item == NULL) { /* it would have freed the msg except static */
1444069154d5SJulian Elischer 		/* try again after flushing our queue */
144530400f03SJulian Elischer 		ng_flush_input_queue(&here->nd_input_queue);
1446069154d5SJulian Elischer 		item = ng_package_msg_self(here, NULL, msg);
1447069154d5SJulian Elischer 		if (item == NULL) {
1448069154d5SJulian Elischer 			printf("failed to free node 0x%x\n", ng_node2ID(here));
1449069154d5SJulian Elischer 			return (ENOMEM);
1450069154d5SJulian Elischer 		}
1451069154d5SJulian Elischer 	}
1452069154d5SJulian Elischer 	return (ng_snd_item(item, 0));
1453069154d5SJulian Elischer }
1454069154d5SJulian Elischer 
14556b795970SJulian Elischer static int
14566b795970SJulian Elischer ng_rmhook_part2(node_p node, hook_p hook, void *arg1, int arg2)
14576b795970SJulian Elischer {
14586b795970SJulian Elischer 	ng_destroy_hook(hook);
14596b795970SJulian Elischer 	return (0);
14606b795970SJulian Elischer }
14616b795970SJulian Elischer 
1462954c4772SJulian Elischer int
1463954c4772SJulian Elischer ng_rmhook_self(hook_p hook)
1464954c4772SJulian Elischer {
14656b795970SJulian Elischer 	int		error;
1466954c4772SJulian Elischer 	node_p node = NG_HOOK_NODE(hook);
1467954c4772SJulian Elischer 
14686b795970SJulian Elischer 	if (node == &ng_deadnode)
14696b795970SJulian Elischer 		return (0);
14706b795970SJulian Elischer 
14716b795970SJulian Elischer 	error = ng_send_fn(node, hook, &ng_rmhook_part2, NULL, 0);
14726b795970SJulian Elischer 	return (error);
1473954c4772SJulian Elischer }
1474954c4772SJulian Elischer 
1475069154d5SJulian Elischer /***********************************************************************
14764cf49a43SJulian Elischer  * Parse and verify a string of the form:  <NODE:><PATH>
14774cf49a43SJulian Elischer  *
14784cf49a43SJulian Elischer  * Such a string can refer to a specific node or a specific hook
14794cf49a43SJulian Elischer  * on a specific node, depending on how you look at it. In the
14804cf49a43SJulian Elischer  * latter case, the PATH component must not end in a dot.
14814cf49a43SJulian Elischer  *
14824cf49a43SJulian Elischer  * Both <NODE:> and <PATH> are optional. The <PATH> is a string
14834cf49a43SJulian Elischer  * of hook names separated by dots. This breaks out the original
14844cf49a43SJulian Elischer  * string, setting *nodep to "NODE" (or NULL if none) and *pathp
14854cf49a43SJulian Elischer  * to "PATH" (or NULL if degenerate). Also, *hookp will point to
14864cf49a43SJulian Elischer  * the final hook component of <PATH>, if any, otherwise NULL.
14874cf49a43SJulian Elischer  *
14884cf49a43SJulian Elischer  * This returns -1 if the path is malformed. The char ** are optional.
1489069154d5SJulian Elischer  ***********************************************************************/
14904cf49a43SJulian Elischer int
14914cf49a43SJulian Elischer ng_path_parse(char *addr, char **nodep, char **pathp, char **hookp)
14924cf49a43SJulian Elischer {
14934cf49a43SJulian Elischer 	char   *node, *path, *hook;
14944cf49a43SJulian Elischer 	int     k;
14954cf49a43SJulian Elischer 
14964cf49a43SJulian Elischer 	/*
14974cf49a43SJulian Elischer 	 * Extract absolute NODE, if any
14984cf49a43SJulian Elischer 	 */
14994cf49a43SJulian Elischer 	for (path = addr; *path && *path != ':'; path++);
15004cf49a43SJulian Elischer 	if (*path) {
15014cf49a43SJulian Elischer 		node = addr;	/* Here's the NODE */
15024cf49a43SJulian Elischer 		*path++ = '\0';	/* Here's the PATH */
15034cf49a43SJulian Elischer 
15044cf49a43SJulian Elischer 		/* Node name must not be empty */
15054cf49a43SJulian Elischer 		if (!*node)
15064cf49a43SJulian Elischer 			return -1;
15074cf49a43SJulian Elischer 
15084cf49a43SJulian Elischer 		/* A name of "." is OK; otherwise '.' not allowed */
15094cf49a43SJulian Elischer 		if (strcmp(node, ".") != 0) {
15104cf49a43SJulian Elischer 			for (k = 0; node[k]; k++)
15114cf49a43SJulian Elischer 				if (node[k] == '.')
15124cf49a43SJulian Elischer 					return -1;
15134cf49a43SJulian Elischer 		}
15144cf49a43SJulian Elischer 	} else {
15154cf49a43SJulian Elischer 		node = NULL;	/* No absolute NODE */
15164cf49a43SJulian Elischer 		path = addr;	/* Here's the PATH */
15174cf49a43SJulian Elischer 	}
15184cf49a43SJulian Elischer 
15194cf49a43SJulian Elischer 	/* Snoop for illegal characters in PATH */
15204cf49a43SJulian Elischer 	for (k = 0; path[k]; k++)
15214cf49a43SJulian Elischer 		if (path[k] == ':')
15224cf49a43SJulian Elischer 			return -1;
15234cf49a43SJulian Elischer 
15244cf49a43SJulian Elischer 	/* Check for no repeated dots in PATH */
15254cf49a43SJulian Elischer 	for (k = 0; path[k]; k++)
15264cf49a43SJulian Elischer 		if (path[k] == '.' && path[k + 1] == '.')
15274cf49a43SJulian Elischer 			return -1;
15284cf49a43SJulian Elischer 
15294cf49a43SJulian Elischer 	/* Remove extra (degenerate) dots from beginning or end of PATH */
15304cf49a43SJulian Elischer 	if (path[0] == '.')
15314cf49a43SJulian Elischer 		path++;
15324cf49a43SJulian Elischer 	if (*path && path[strlen(path) - 1] == '.')
15334cf49a43SJulian Elischer 		path[strlen(path) - 1] = 0;
15344cf49a43SJulian Elischer 
15354cf49a43SJulian Elischer 	/* If PATH has a dot, then we're not talking about a hook */
15364cf49a43SJulian Elischer 	if (*path) {
15374cf49a43SJulian Elischer 		for (hook = path, k = 0; path[k]; k++)
15384cf49a43SJulian Elischer 			if (path[k] == '.') {
15394cf49a43SJulian Elischer 				hook = NULL;
15404cf49a43SJulian Elischer 				break;
15414cf49a43SJulian Elischer 			}
15424cf49a43SJulian Elischer 	} else
15434cf49a43SJulian Elischer 		path = hook = NULL;
15444cf49a43SJulian Elischer 
15454cf49a43SJulian Elischer 	/* Done */
15464cf49a43SJulian Elischer 	if (nodep)
15474cf49a43SJulian Elischer 		*nodep = node;
15484cf49a43SJulian Elischer 	if (pathp)
15494cf49a43SJulian Elischer 		*pathp = path;
15504cf49a43SJulian Elischer 	if (hookp)
15514cf49a43SJulian Elischer 		*hookp = hook;
15524cf49a43SJulian Elischer 	return (0);
15534cf49a43SJulian Elischer }
15544cf49a43SJulian Elischer 
15554cf49a43SJulian Elischer /*
15564cf49a43SJulian Elischer  * Given a path, which may be absolute or relative, and a starting node,
1557069154d5SJulian Elischer  * return the destination node.
15584cf49a43SJulian Elischer  */
15594cf49a43SJulian Elischer int
1560069154d5SJulian Elischer ng_path2noderef(node_p here, const char *address,
1561069154d5SJulian Elischer 				node_p *destp, hook_p *lasthook)
15624cf49a43SJulian Elischer {
15634cf49a43SJulian Elischer 	char    fullpath[NG_PATHLEN + 1];
15644cf49a43SJulian Elischer 	char   *nodename, *path, pbuf[2];
1565069154d5SJulian Elischer 	node_p  node, oldnode;
15664cf49a43SJulian Elischer 	char   *cp;
1567a4ec03cfSJulian Elischer 	hook_p hook = NULL;
15684cf49a43SJulian Elischer 
15694cf49a43SJulian Elischer 	/* Initialize */
157030400f03SJulian Elischer 	if (destp == NULL) {
15716b795970SJulian Elischer 		TRAP_ERROR();
15724cf49a43SJulian Elischer 		return EINVAL;
157330400f03SJulian Elischer 	}
15744cf49a43SJulian Elischer 	*destp = NULL;
15754cf49a43SJulian Elischer 
15764cf49a43SJulian Elischer 	/* Make a writable copy of address for ng_path_parse() */
15774cf49a43SJulian Elischer 	strncpy(fullpath, address, sizeof(fullpath) - 1);
15784cf49a43SJulian Elischer 	fullpath[sizeof(fullpath) - 1] = '\0';
15794cf49a43SJulian Elischer 
15804cf49a43SJulian Elischer 	/* Parse out node and sequence of hooks */
15814cf49a43SJulian Elischer 	if (ng_path_parse(fullpath, &nodename, &path, NULL) < 0) {
15826b795970SJulian Elischer 		TRAP_ERROR();
15834cf49a43SJulian Elischer 		return EINVAL;
15844cf49a43SJulian Elischer 	}
15854cf49a43SJulian Elischer 	if (path == NULL) {
15864cf49a43SJulian Elischer 		pbuf[0] = '.';	/* Needs to be writable */
15874cf49a43SJulian Elischer 		pbuf[1] = '\0';
15884cf49a43SJulian Elischer 		path = pbuf;
15894cf49a43SJulian Elischer 	}
15904cf49a43SJulian Elischer 
1591069154d5SJulian Elischer 	/*
1592069154d5SJulian Elischer 	 * For an absolute address, jump to the starting node.
1593069154d5SJulian Elischer 	 * Note that this holds a reference on the node for us.
1594069154d5SJulian Elischer 	 * Don't forget to drop the reference if we don't need it.
1595069154d5SJulian Elischer 	 */
15964cf49a43SJulian Elischer 	if (nodename) {
1597069154d5SJulian Elischer 		node = ng_name2noderef(here, nodename);
15984cf49a43SJulian Elischer 		if (node == NULL) {
15996b795970SJulian Elischer 			TRAP_ERROR();
16004cf49a43SJulian Elischer 			return (ENOENT);
16014cf49a43SJulian Elischer 		}
1602069154d5SJulian Elischer 	} else {
1603069154d5SJulian Elischer 		if (here == NULL) {
16046b795970SJulian Elischer 			TRAP_ERROR();
1605069154d5SJulian Elischer 			return (EINVAL);
1606069154d5SJulian Elischer 		}
16074cf49a43SJulian Elischer 		node = here;
160830400f03SJulian Elischer 		NG_NODE_REF(node);
1609069154d5SJulian Elischer 	}
16104cf49a43SJulian Elischer 
1611069154d5SJulian Elischer 	/*
1612069154d5SJulian Elischer 	 * Now follow the sequence of hooks
1613069154d5SJulian Elischer 	 * XXX
1614069154d5SJulian Elischer 	 * We actually cannot guarantee that the sequence
1615069154d5SJulian Elischer 	 * is not being demolished as we crawl along it
1616069154d5SJulian Elischer 	 * without extra-ordinary locking etc.
1617069154d5SJulian Elischer 	 * So this is a bit dodgy to say the least.
1618069154d5SJulian Elischer 	 * We can probably hold up some things by holding
1619069154d5SJulian Elischer 	 * the nodelist mutex for the time of this
1620069154d5SJulian Elischer 	 * crawl if we wanted.. At least that way we wouldn't have to
1621069154d5SJulian Elischer 	 * worry about the nodes dissappearing, but the hooks would still
1622069154d5SJulian Elischer 	 * be a problem.
1623069154d5SJulian Elischer 	 */
16244cf49a43SJulian Elischer 	for (cp = path; node != NULL && *cp != '\0'; ) {
16254cf49a43SJulian Elischer 		char *segment;
16264cf49a43SJulian Elischer 
16274cf49a43SJulian Elischer 		/*
16284cf49a43SJulian Elischer 		 * Break out the next path segment. Replace the dot we just
16294cf49a43SJulian Elischer 		 * found with a NUL; "cp" points to the next segment (or the
16304cf49a43SJulian Elischer 		 * NUL at the end).
16314cf49a43SJulian Elischer 		 */
16324cf49a43SJulian Elischer 		for (segment = cp; *cp != '\0'; cp++) {
16334cf49a43SJulian Elischer 			if (*cp == '.') {
16344cf49a43SJulian Elischer 				*cp++ = '\0';
16354cf49a43SJulian Elischer 				break;
16364cf49a43SJulian Elischer 			}
16374cf49a43SJulian Elischer 		}
16384cf49a43SJulian Elischer 
16394cf49a43SJulian Elischer 		/* Empty segment */
16404cf49a43SJulian Elischer 		if (*segment == '\0')
16414cf49a43SJulian Elischer 			continue;
16424cf49a43SJulian Elischer 
16434cf49a43SJulian Elischer 		/* We have a segment, so look for a hook by that name */
1644899e9c4eSArchie Cobbs 		hook = ng_findhook(node, segment);
16454cf49a43SJulian Elischer 
16464cf49a43SJulian Elischer 		/* Can't get there from here... */
16474cf49a43SJulian Elischer 		if (hook == NULL
164830400f03SJulian Elischer 		    || NG_HOOK_PEER(hook) == NULL
164930400f03SJulian Elischer 		    || NG_HOOK_NOT_VALID(hook)
165030400f03SJulian Elischer 		    || NG_HOOK_NOT_VALID(NG_HOOK_PEER(hook))) {
16516b795970SJulian Elischer 			TRAP_ERROR();
165230400f03SJulian Elischer 			NG_NODE_UNREF(node);
165330400f03SJulian Elischer #if 0
165430400f03SJulian Elischer 			printf("hooknotvalid %s %s %d %d %d %d ",
165530400f03SJulian Elischer 					path,
165630400f03SJulian Elischer 					segment,
165730400f03SJulian Elischer 					hook == NULL,
165830400f03SJulian Elischer 		     			NG_HOOK_PEER(hook) == NULL,
165930400f03SJulian Elischer 		     			NG_HOOK_NOT_VALID(hook),
166030400f03SJulian Elischer 		     			NG_HOOK_NOT_VALID(NG_HOOK_PEER(hook)));
166130400f03SJulian Elischer #endif
16624cf49a43SJulian Elischer 			return (ENOENT);
16634cf49a43SJulian Elischer 		}
16644cf49a43SJulian Elischer 
1665069154d5SJulian Elischer 		/*
1666069154d5SJulian Elischer 		 * Hop on over to the next node
1667069154d5SJulian Elischer 		 * XXX
1668069154d5SJulian Elischer 		 * Big race conditions here as hooks and nodes go away
1669069154d5SJulian Elischer 		 * *** Idea.. store an ng_ID_t in each hook and use that
1670069154d5SJulian Elischer 		 * instead of the direct hook in this crawl?
1671069154d5SJulian Elischer 		 */
1672069154d5SJulian Elischer 		oldnode = node;
167330400f03SJulian Elischer 		if ((node = NG_PEER_NODE(hook)))
167430400f03SJulian Elischer 			NG_NODE_REF(node);	/* XXX RACE */
167530400f03SJulian Elischer 		NG_NODE_UNREF(oldnode);	/* XXX another race */
167630400f03SJulian Elischer 		if (NG_NODE_NOT_VALID(node)) {
167730400f03SJulian Elischer 			NG_NODE_UNREF(node);	/* XXX more races */
1678069154d5SJulian Elischer 			node = NULL;
1679069154d5SJulian Elischer 		}
16804cf49a43SJulian Elischer 	}
16814cf49a43SJulian Elischer 
16824cf49a43SJulian Elischer 	/* If node somehow missing, fail here (probably this is not needed) */
16834cf49a43SJulian Elischer 	if (node == NULL) {
16846b795970SJulian Elischer 		TRAP_ERROR();
16854cf49a43SJulian Elischer 		return (ENXIO);
16864cf49a43SJulian Elischer 	}
16874cf49a43SJulian Elischer 
16884cf49a43SJulian Elischer 	/* Done */
16894cf49a43SJulian Elischer 	*destp = node;
16901bdebe4dSArchie Cobbs 	if (lasthook != NULL)
169130400f03SJulian Elischer 		*lasthook = (hook ? NG_HOOK_PEER(hook) : NULL);
1692069154d5SJulian Elischer 	return (0);
1693069154d5SJulian Elischer }
1694069154d5SJulian Elischer 
1695069154d5SJulian Elischer /***************************************************************\
1696069154d5SJulian Elischer * Input queue handling.
1697069154d5SJulian Elischer * All activities are submitted to the node via the input queue
1698069154d5SJulian Elischer * which implements a multiple-reader/single-writer gate.
1699069154d5SJulian Elischer * Items which cannot be handled immeditly are queued.
1700069154d5SJulian Elischer *
1701069154d5SJulian Elischer * read-write queue locking inline functions			*
1702069154d5SJulian Elischer \***************************************************************/
1703069154d5SJulian Elischer 
1704069154d5SJulian Elischer static __inline item_p ng_dequeue(struct ng_queue * ngq);
1705069154d5SJulian Elischer static __inline item_p ng_acquire_read(struct ng_queue * ngq,
1706069154d5SJulian Elischer 					item_p  item);
1707069154d5SJulian Elischer static __inline item_p ng_acquire_write(struct ng_queue * ngq,
1708069154d5SJulian Elischer 					item_p  item);
1709069154d5SJulian Elischer static __inline void	ng_leave_read(struct ng_queue * ngq);
1710069154d5SJulian Elischer static __inline void	ng_leave_write(struct ng_queue * ngq);
1711069154d5SJulian Elischer static __inline void	ng_queue_rw(struct ng_queue * ngq,
1712069154d5SJulian Elischer 					item_p  item, int rw);
1713069154d5SJulian Elischer 
1714069154d5SJulian Elischer /*
1715069154d5SJulian Elischer  * Definition of the bits fields in the ng_queue flag word.
1716069154d5SJulian Elischer  * Defined here rather than in netgraph.h because no-one should fiddle
1717069154d5SJulian Elischer  * with them.
1718069154d5SJulian Elischer  *
1719069154d5SJulian Elischer  * The ordering here is important! don't shuffle these. If you add
1720069154d5SJulian Elischer  * READ_PENDING to the word when it has READ_PENDING already set, you
1721069154d5SJulian Elischer  * generate a carry into the reader count, this you atomically add a reader,
1722069154d5SJulian Elischer  * and remove the pending reader count! Similarly for the pending writer
1723069154d5SJulian Elischer  * flag, adding WRITE_PENDING generates a carry and sets the WRITER_ACTIVE
1724069154d5SJulian Elischer  * flag, while clearing WRITE_PENDING. When 'SINGLE_THREAD_ONLY' is set, then
1725069154d5SJulian Elischer  * it is only permitted to do WRITER operations. Reader operations will
1726069154d5SJulian Elischer  * result in errors.
1727069154d5SJulian Elischer  * But that "hack" is unnecessary: "cpp" can do the math for us!
1728069154d5SJulian Elischer  */
1729069154d5SJulian Elischer /*-
1730069154d5SJulian Elischer  Safety Barrier--------+ (adjustable to suit taste) (not used yet)
1731069154d5SJulian Elischer                        |
1732069154d5SJulian Elischer                        V
1733069154d5SJulian Elischer +-------+-------+-------+-------+-------+-------+-------+-------+
1734069154d5SJulian Elischer | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
1735069154d5SJulian Elischer |A|c|t|i|v|e| |R|e|a|d|e|r| |C|o|u|n|t| | | | | | | | | |R|A|W|S|
1736069154d5SJulian Elischer | | | | | | | | | | | | | | | | | | | | | | | | | | | | |P|W|P|T|
1737069154d5SJulian Elischer +-------+-------+-------+-------+-------+-------+-------+-------+
1738069154d5SJulian Elischer \_________________________ ____________________________/ | | | |
1739069154d5SJulian Elischer                           V                              | | | |
1740069154d5SJulian Elischer                 [active reader count]                    | | | |
1741069154d5SJulian Elischer                                                          | | | |
1742069154d5SJulian Elischer         Read Pending ------------------------------------+ | | |
1743069154d5SJulian Elischer                                                            | | |
1744069154d5SJulian Elischer         Active Writer -------------------------------------+ | |
1745069154d5SJulian Elischer                                                              | |
1746069154d5SJulian Elischer         Write Pending ---------------------------------------+ |
1747069154d5SJulian Elischer                                                                |
1748069154d5SJulian Elischer         Single Threading Only ---------------------------------+
1749069154d5SJulian Elischer */
1750069154d5SJulian Elischer #define	SINGLE_THREAD_ONLY 0x00000001	/* if set, even reads single thread */
1751069154d5SJulian Elischer #define WRITE_PENDING	0x00000002
1752069154d5SJulian Elischer #define	WRITER_ACTIVE	0x00000004
1753069154d5SJulian Elischer #define READ_PENDING	0x00000008
1754069154d5SJulian Elischer #define	READER_INCREMENT 0x00000010
1755069154d5SJulian Elischer #define	READER_MASK	0xfffffff0	/* Not valid if WRITER_ACTIVE is set */
1756069154d5SJulian Elischer #define SAFETY_BARRIER	0x00100000	/* 64K items queued should be enough */
1757069154d5SJulian Elischer /*
1758069154d5SJulian Elischer  * Taking into account the current state of the queue and node, possibly take
1759069154d5SJulian Elischer  * the next entry off the queue and return it. Return NULL if there was
1760069154d5SJulian Elischer  * nothing we could return, either because there really was nothing there, or
1761069154d5SJulian Elischer  * because the node was in a state where it cannot yet process the next item
1762069154d5SJulian Elischer  * on the queue.
1763069154d5SJulian Elischer  *
1764069154d5SJulian Elischer  * This MUST MUST MUST be called with the mutex held.
1765069154d5SJulian Elischer  */
1766069154d5SJulian Elischer static __inline item_p
1767069154d5SJulian Elischer ng_dequeue(struct ng_queue *ngq)
1768069154d5SJulian Elischer {
1769069154d5SJulian Elischer 	item_p item;
1770069154d5SJulian Elischer 	u_int		add_arg;
1771069154d5SJulian Elischer 	/*
1772069154d5SJulian Elischer 	 * If there is a writer, then the answer is "no". Everything else
1773069154d5SJulian Elischer 	 * stops when there is a WRITER.
1774069154d5SJulian Elischer 	 */
1775069154d5SJulian Elischer 	if (ngq->q_flags & WRITER_ACTIVE) {
1776069154d5SJulian Elischer 		return (NULL);
1777069154d5SJulian Elischer 	}
1778069154d5SJulian Elischer 	/* Now take a look at what's on the queue and what's running */
1779069154d5SJulian Elischer 	if ((ngq->q_flags & ~(READER_MASK | SINGLE_THREAD_ONLY)) == READ_PENDING) {
1780069154d5SJulian Elischer 		/*
1781069154d5SJulian Elischer 		 * It was a reader and we have no write active. We don't care
1782069154d5SJulian Elischer 		 * how many readers are already active. Adjust the count for
1783069154d5SJulian Elischer 		 * the item we are about to dequeue. Adding READ_PENDING to
1784069154d5SJulian Elischer 		 * the exisiting READ_PENDING clears it and generates a carry
1785069154d5SJulian Elischer 		 * into the reader count.
1786069154d5SJulian Elischer 		 */
1787069154d5SJulian Elischer 		add_arg = READ_PENDING;
1788069154d5SJulian Elischer 	} else if ((ngq->q_flags & ~SINGLE_THREAD_ONLY) == WRITE_PENDING) {
1789069154d5SJulian Elischer 		/*
1790069154d5SJulian Elischer 		 * There is a pending write, no readers and no active writer.
1791069154d5SJulian Elischer 		 * This means we can go ahead with the pending writer. Note
1792069154d5SJulian Elischer 		 * the fact that we now have a writer, ready for when we take
1793069154d5SJulian Elischer 		 * it off the queue.
1794069154d5SJulian Elischer 		 *
1795069154d5SJulian Elischer 		 * We don't need to worry about a possible collision with the
1796069154d5SJulian Elischer 		 * fasttrack reader.
1797069154d5SJulian Elischer 		 *
1798069154d5SJulian Elischer 		 * The fasttrack thread may take a long time to discover that we
1799069154d5SJulian Elischer 		 * are running so we would have an inconsistent state in the
1800069154d5SJulian Elischer 		 * flags for a while. Since we ignore the reader count
1801069154d5SJulian Elischer 		 * entirely when the WRITER_ACTIVE flag is set, this should
1802069154d5SJulian Elischer 		 * not matter (in fact it is defined that way). If it tests
1803069154d5SJulian Elischer 		 * the flag before this operation, the WRITE_PENDING flag
1804069154d5SJulian Elischer 		 * will make it fail, and if it tests it later, the
1805069154d5SJulian Elischer 		 * ACTIVE_WRITER flag will do the same. If it is SO slow that
1806069154d5SJulian Elischer 		 * we have actually completed the operation, and neither flag
1807069154d5SJulian Elischer 		 * is set (nor the READ_PENDING) by the time that it tests
1808069154d5SJulian Elischer 		 * the flags, then it is actually ok for it to continue. If
1809069154d5SJulian Elischer 		 * it completes and we've finished and the read pending is
1810069154d5SJulian Elischer 		 * set it still fails.
1811069154d5SJulian Elischer 		 *
1812069154d5SJulian Elischer 		 * So we can just ignore it,  as long as we can ensure that the
1813069154d5SJulian Elischer 		 * transition from WRITE_PENDING state to the WRITER_ACTIVE
1814069154d5SJulian Elischer 		 * state is atomic.
1815069154d5SJulian Elischer 		 *
1816069154d5SJulian Elischer 		 * After failing, first it will be held back by the mutex, then
1817069154d5SJulian Elischer 		 * when it can proceed, it will queue its request, then it
1818069154d5SJulian Elischer 		 * would arrive at this function. Usually it will have to
1819069154d5SJulian Elischer 		 * leave empty handed because the ACTIVE WRITER bit wil be
1820069154d5SJulian Elischer 		 * set.
1821069154d5SJulian Elischer 		 */
1822069154d5SJulian Elischer 		/*
1823069154d5SJulian Elischer 		 * Adjust the flags for the item we are about to dequeue.
1824069154d5SJulian Elischer 		 * Adding WRITE_PENDING to the exisiting WRITE_PENDING clears
1825069154d5SJulian Elischer 		 * it and generates a carry into the WRITER_ACTIVE flag, all
1826069154d5SJulian Elischer 		 * atomically.
1827069154d5SJulian Elischer 		 */
1828069154d5SJulian Elischer 		add_arg = WRITE_PENDING;
1829069154d5SJulian Elischer 		/*
1830069154d5SJulian Elischer 		 * We want to write "active writer, no readers " Now go make
1831069154d5SJulian Elischer 		 * it true. In fact there may be a number in the readers
1832069154d5SJulian Elischer 		 * count but we know it is not true and will be fixed soon.
1833069154d5SJulian Elischer 		 * We will fix the flags for the next pending entry in a
1834069154d5SJulian Elischer 		 * moment.
1835069154d5SJulian Elischer 		 */
1836069154d5SJulian Elischer 	} else {
1837069154d5SJulian Elischer 		/*
1838069154d5SJulian Elischer 		 * We can't dequeue anything.. return and say so. Probably we
1839069154d5SJulian Elischer 		 * have a write pending and the readers count is non zero. If
1840069154d5SJulian Elischer 		 * we got here because a reader hit us just at the wrong
1841069154d5SJulian Elischer 		 * moment with the fasttrack code, and put us in a strange
1842069154d5SJulian Elischer 		 * state, then it will be through in just a moment, (as soon
1843069154d5SJulian Elischer 		 * as we release the mutex) and keep things moving.
1844069154d5SJulian Elischer 		 */
18454cf49a43SJulian Elischer 		return (0);
18464cf49a43SJulian Elischer 	}
18474cf49a43SJulian Elischer 
18484cf49a43SJulian Elischer 	/*
1849069154d5SJulian Elischer 	 * Now we dequeue the request (whatever it may be) and correct the
1850069154d5SJulian Elischer 	 * pending flags and the next and last pointers.
18514cf49a43SJulian Elischer 	 */
1852069154d5SJulian Elischer 	item = ngq->queue;
1853069154d5SJulian Elischer 	ngq->queue = item->el_next;
1854069154d5SJulian Elischer 	if (ngq->last == &(item->el_next)) {
18554cf49a43SJulian Elischer 		/*
1856069154d5SJulian Elischer 		 * that was the last entry in the queue so set the 'last
1857069154d5SJulian Elischer 		 * pointer up correctly and make sure the pending flags are
1858069154d5SJulian Elischer 		 * clear.
18594cf49a43SJulian Elischer 		 */
1860069154d5SJulian Elischer 		ngq->last = &(ngq->queue);
1861859a4d16SJulian Elischer 		/*
1862069154d5SJulian Elischer 		 * Whatever flag was set is cleared and the carry sets the
1863069154d5SJulian Elischer 		 * correct new active state/count. So we don't need to change
1864069154d5SJulian Elischer 		 * add_arg.
1865859a4d16SJulian Elischer 		 */
1866859a4d16SJulian Elischer 	} else {
18676b795970SJulian Elischer 		if ((ngq->queue->el_flags & NGQF_RW) == NGQF_READER) {
1868069154d5SJulian Elischer 			/*
1869069154d5SJulian Elischer 			 * If we had a READ_PENDING and have another one, we
1870069154d5SJulian Elischer 			 * just want to add READ_PENDING twice (the same as
1871069154d5SJulian Elischer 			 * adding READER_INCREMENT). If we had WRITE_PENDING,
1872069154d5SJulian Elischer 			 * we want to add READ_PENDING + WRITE_PENDING to
1873069154d5SJulian Elischer 			 * clear the old WRITE_PENDING, set ACTIVE_WRITER,
1874069154d5SJulian Elischer 			 * and set READ_PENDING. Either way we just add
1875069154d5SJulian Elischer 			 * READ_PENDING to whatever we already had.
1876069154d5SJulian Elischer 			 */
1877069154d5SJulian Elischer 			add_arg += READ_PENDING;
1878069154d5SJulian Elischer 		} else {
1879069154d5SJulian Elischer 			/*
1880069154d5SJulian Elischer 			 * If we had a WRITE_PENDING and have another one, we
1881069154d5SJulian Elischer 			 * just want to add WRITE_PENDING twice (the same as
1882069154d5SJulian Elischer 			 * adding ACTIVE_WRITER). If we had READ_PENDING, we
1883069154d5SJulian Elischer 			 * want to add READ_PENDING + WRITE_PENDING to clear
1884069154d5SJulian Elischer 			 * the old READ_PENDING, increment the readers, and
1885069154d5SJulian Elischer 			 * set WRITE_PENDING. Either way we just add
1886069154d5SJulian Elischer 			 * WRITE_PENDING to whatever we already had.
1887069154d5SJulian Elischer 			 */
1888069154d5SJulian Elischer 			add_arg += WRITE_PENDING;
1889859a4d16SJulian Elischer 		}
1890859a4d16SJulian Elischer 	}
1891069154d5SJulian Elischer 	atomic_add_long(&ngq->q_flags, add_arg);
1892069154d5SJulian Elischer 	/*
1893069154d5SJulian Elischer 	 * We have successfully cleared the old pending flag, set the new one
1894069154d5SJulian Elischer 	 * if it is needed, and incremented the appropriate active field.
1895069154d5SJulian Elischer 	 * (all in one atomic addition.. wow)
1896069154d5SJulian Elischer 	 */
1897069154d5SJulian Elischer 	return (item);
1898069154d5SJulian Elischer }
1899859a4d16SJulian Elischer 
1900859a4d16SJulian Elischer /*
1901069154d5SJulian Elischer  * Queue a packet to be picked up by someone else.
1902069154d5SJulian Elischer  * We really don't care who, but we can't or don't want to hang around
1903069154d5SJulian Elischer  * to process it ourselves. We are probably an interrupt routine..
1904069154d5SJulian Elischer  * 1 = writer, 0 = reader
1905069154d5SJulian Elischer  * We should set something to indicate NETISR requested
1906069154d5SJulian Elischer  * If it's the first item queued.
1907859a4d16SJulian Elischer  */
1908069154d5SJulian Elischer #define NGQRW_R 0
1909069154d5SJulian Elischer #define NGQRW_W 1
1910069154d5SJulian Elischer static __inline void
1911069154d5SJulian Elischer ng_queue_rw(struct ng_queue * ngq, item_p  item, int rw)
1912069154d5SJulian Elischer {
1913069154d5SJulian Elischer 	item->el_next = NULL;	/* maybe not needed */
1914069154d5SJulian Elischer 	*ngq->last = item;
1915069154d5SJulian Elischer 	/*
1916069154d5SJulian Elischer 	 * If it was the first item in the queue then we need to
1917069154d5SJulian Elischer 	 * set the last pointer and the type flags.
1918069154d5SJulian Elischer 	 */
1919069154d5SJulian Elischer 	if (ngq->last == &(ngq->queue)) {
1920069154d5SJulian Elischer 		/*
1921069154d5SJulian Elischer 		 * When called with constants for rw, the optimiser will
1922069154d5SJulian Elischer 		 * remove the unneeded branch below.
1923069154d5SJulian Elischer 		 */
1924069154d5SJulian Elischer 		if (rw == NGQRW_W) {
1925069154d5SJulian Elischer 			atomic_add_long(&ngq->q_flags, WRITE_PENDING);
1926069154d5SJulian Elischer 		} else {
1927069154d5SJulian Elischer 			atomic_add_long(&ngq->q_flags, READ_PENDING);
1928069154d5SJulian Elischer 		}
1929069154d5SJulian Elischer 	}
1930069154d5SJulian Elischer 	ngq->last = &(item->el_next);
1931069154d5SJulian Elischer }
1932069154d5SJulian Elischer 
1933069154d5SJulian Elischer 
1934069154d5SJulian Elischer /*
1935069154d5SJulian Elischer  * This function 'cheats' in that it first tries to 'grab' the use of the
1936069154d5SJulian Elischer  * node, without going through the mutex. We can do this becasue of the
1937069154d5SJulian Elischer  * semantics of the lock. The semantics include a clause that says that the
1938069154d5SJulian Elischer  * value of the readers count is invalid if the WRITER_ACTIVE flag is set. It
1939069154d5SJulian Elischer  * also says that the WRITER_ACTIVE flag cannot be set if the readers count
1940069154d5SJulian Elischer  * is not zero. Note that this talks about what is valid to SET the
1941069154d5SJulian Elischer  * WRITER_ACTIVE flag, because from the moment it is set, the value if the
1942069154d5SJulian Elischer  * reader count is immaterial, and not valid. The two 'pending' flags have a
1943069154d5SJulian Elischer  * similar effect, in that If they are orthogonal to the two active fields in
1944069154d5SJulian Elischer  * how they are set, but if either is set, the attempted 'grab' need to be
1945069154d5SJulian Elischer  * backed out because there is earlier work, and we maintain ordering in the
1946069154d5SJulian Elischer  * queue. The result of this is that the reader request can try obtain use of
1947069154d5SJulian Elischer  * the node with only a single atomic addition, and without any of the mutex
1948069154d5SJulian Elischer  * overhead. If this fails the operation degenerates to the same as for other
1949069154d5SJulian Elischer  * cases.
1950069154d5SJulian Elischer  *
1951069154d5SJulian Elischer  */
1952069154d5SJulian Elischer static __inline item_p
1953069154d5SJulian Elischer ng_acquire_read(struct ng_queue *ngq, item_p item)
1954069154d5SJulian Elischer {
1955069154d5SJulian Elischer 
1956069154d5SJulian Elischer 	/* ######### Hack alert ######### */
1957069154d5SJulian Elischer 	atomic_add_long(&ngq->q_flags, READER_INCREMENT);
1958069154d5SJulian Elischer 	if ((ngq->q_flags & (~READER_MASK)) == 0) {
1959069154d5SJulian Elischer 		/* Successfully grabbed node */
1960069154d5SJulian Elischer 		return (item);
1961069154d5SJulian Elischer 	}
1962069154d5SJulian Elischer 	/* undo the damage if we didn't succeed */
1963069154d5SJulian Elischer 	atomic_subtract_long(&ngq->q_flags, READER_INCREMENT);
1964069154d5SJulian Elischer 
1965069154d5SJulian Elischer 	/* ######### End Hack alert ######### */
1966069154d5SJulian Elischer 	mtx_enter((&ngq->q_mtx), MTX_SPIN);
1967069154d5SJulian Elischer 	/*
1968069154d5SJulian Elischer 	 * Try again. Another processor (or interrupt for that matter) may
1969069154d5SJulian Elischer 	 * have removed the last queued item that was stopping us from
1970069154d5SJulian Elischer 	 * running, between the previous test, and the moment that we took
1971069154d5SJulian Elischer 	 * the mutex. (Or maybe a writer completed.)
1972069154d5SJulian Elischer 	 */
1973069154d5SJulian Elischer 	if ((ngq->q_flags & (~READER_MASK)) == 0) {
1974069154d5SJulian Elischer 		atomic_add_long(&ngq->q_flags, READER_INCREMENT);
1975069154d5SJulian Elischer 		mtx_exit((&ngq->q_mtx), MTX_SPIN);
1976069154d5SJulian Elischer 		return (item);
1977069154d5SJulian Elischer 	}
1978069154d5SJulian Elischer 
1979069154d5SJulian Elischer 	/*
1980069154d5SJulian Elischer 	 * Quick check that we are doing things right.
1981069154d5SJulian Elischer 	 */
1982069154d5SJulian Elischer 	if (ngq->q_flags & SINGLE_THREAD_ONLY) {
1983069154d5SJulian Elischer 		panic("Calling single treaded queue incorrectly");
1984069154d5SJulian Elischer 	}
1985069154d5SJulian Elischer 
1986069154d5SJulian Elischer 	/*
1987069154d5SJulian Elischer 	 * and queue the request for later.
1988069154d5SJulian Elischer 	 */
19896b795970SJulian Elischer 	item->el_flags |= NGQF_READER;
1990069154d5SJulian Elischer 	ng_queue_rw(ngq, item, NGQRW_R);
1991069154d5SJulian Elischer 
1992069154d5SJulian Elischer 	/*
1993069154d5SJulian Elischer 	 * Ok, so that's the item successfully queued for later. So now we
1994069154d5SJulian Elischer 	 * see if we can dequeue something to run instead.
1995069154d5SJulian Elischer 	 */
1996069154d5SJulian Elischer 	item = ng_dequeue(ngq);
1997069154d5SJulian Elischer 	mtx_exit(&(ngq->q_mtx), MTX_SPIN);
1998069154d5SJulian Elischer 	return (item);
1999069154d5SJulian Elischer }
2000069154d5SJulian Elischer 
2001069154d5SJulian Elischer static __inline item_p
2002069154d5SJulian Elischer ng_acquire_write(struct ng_queue *ngq, item_p item)
2003069154d5SJulian Elischer {
2004069154d5SJulian Elischer restart:
2005069154d5SJulian Elischer 	mtx_enter(&(ngq->q_mtx), MTX_SPIN);
2006069154d5SJulian Elischer 	/*
2007069154d5SJulian Elischer 	 * If there are no readers, no writer, and no pending packets, then
2008069154d5SJulian Elischer 	 * we can just go ahead. In all other situations we need to queue the
2009069154d5SJulian Elischer 	 * request
2010069154d5SJulian Elischer 	 */
2011069154d5SJulian Elischer 	if ((ngq->q_flags & (~SINGLE_THREAD_ONLY)) == 0) {
2012069154d5SJulian Elischer 		atomic_add_long(&ngq->q_flags, WRITER_ACTIVE);
2013069154d5SJulian Elischer 		mtx_exit((&ngq->q_mtx), MTX_SPIN);
2014069154d5SJulian Elischer 		if (ngq->q_flags & READER_MASK) {
2015069154d5SJulian Elischer 			/* Collision with fast-track reader */
2016069154d5SJulian Elischer 			atomic_add_long(&ngq->q_flags, -WRITER_ACTIVE);
2017069154d5SJulian Elischer 			goto restart;
2018069154d5SJulian Elischer 		}
2019069154d5SJulian Elischer 
2020069154d5SJulian Elischer 		return (item);
2021069154d5SJulian Elischer 	}
2022069154d5SJulian Elischer 
2023069154d5SJulian Elischer 	/*
2024069154d5SJulian Elischer 	 * and queue the request for later.
2025069154d5SJulian Elischer 	 */
20266b795970SJulian Elischer 	item->el_flags &= ~NGQF_RW;
2027069154d5SJulian Elischer 	ng_queue_rw(ngq, item, NGQRW_W);
2028069154d5SJulian Elischer 
2029069154d5SJulian Elischer 	/*
2030069154d5SJulian Elischer 	 * Ok, so that's the item successfully queued for later. So now we
2031069154d5SJulian Elischer 	 * see if we can dequeue something to run instead.
2032069154d5SJulian Elischer 	 */
2033069154d5SJulian Elischer 	item = ng_dequeue(ngq);
2034069154d5SJulian Elischer 	mtx_exit(&(ngq->q_mtx), MTX_SPIN);
2035069154d5SJulian Elischer 	return (item);
2036069154d5SJulian Elischer }
2037069154d5SJulian Elischer 
2038069154d5SJulian Elischer static __inline void
2039069154d5SJulian Elischer ng_leave_read(struct ng_queue *ngq)
2040069154d5SJulian Elischer {
2041069154d5SJulian Elischer 	atomic_subtract_long(&ngq->q_flags, READER_INCREMENT);
2042069154d5SJulian Elischer }
2043069154d5SJulian Elischer 
2044069154d5SJulian Elischer static __inline void
2045069154d5SJulian Elischer ng_leave_write(struct ng_queue *ngq)
2046069154d5SJulian Elischer {
2047069154d5SJulian Elischer 	atomic_subtract_long(&ngq->q_flags, WRITER_ACTIVE);
2048069154d5SJulian Elischer }
2049069154d5SJulian Elischer 
2050069154d5SJulian Elischer static void
2051069154d5SJulian Elischer ng_flush_input_queue(struct ng_queue * ngq)
2052069154d5SJulian Elischer {
2053069154d5SJulian Elischer 	item_p item;
2054069154d5SJulian Elischer 	u_int		add_arg;
2055069154d5SJulian Elischer 	mtx_enter(&ngq->q_mtx, MTX_SPIN);
2056069154d5SJulian Elischer 	for (;;) {
2057069154d5SJulian Elischer 		/* Now take a look at what's on the queue */
2058069154d5SJulian Elischer 		if (ngq->q_flags & READ_PENDING) {
2059069154d5SJulian Elischer 			add_arg = -READ_PENDING;
2060069154d5SJulian Elischer 		} else if (ngq->q_flags & WRITE_PENDING) {
2061069154d5SJulian Elischer 			add_arg = -WRITE_PENDING;
2062069154d5SJulian Elischer 		} else {
2063069154d5SJulian Elischer 			break;
2064069154d5SJulian Elischer 		}
2065069154d5SJulian Elischer 
2066069154d5SJulian Elischer 		item = ngq->queue;
2067069154d5SJulian Elischer 		ngq->queue = item->el_next;
2068069154d5SJulian Elischer 		if (ngq->last == &(item->el_next)) {
2069069154d5SJulian Elischer 			ngq->last = &(ngq->queue);
2070069154d5SJulian Elischer 		} else {
20716b795970SJulian Elischer 			if ((ngq->queue->el_flags & NGQF_RW) == NGQF_READER) {
2072069154d5SJulian Elischer 				add_arg += READ_PENDING;
2073069154d5SJulian Elischer 			} else {
2074069154d5SJulian Elischer 				add_arg += WRITE_PENDING;
2075069154d5SJulian Elischer 			}
2076069154d5SJulian Elischer 		}
2077069154d5SJulian Elischer 		atomic_add_long(&ngq->q_flags, add_arg);
2078069154d5SJulian Elischer 
2079069154d5SJulian Elischer 		mtx_exit(&ngq->q_mtx, MTX_SPIN);
2080069154d5SJulian Elischer 		NG_FREE_ITEM(item);
2081069154d5SJulian Elischer 		mtx_enter(&ngq->q_mtx, MTX_SPIN);
2082069154d5SJulian Elischer 	}
2083069154d5SJulian Elischer 	mtx_exit(&ngq->q_mtx, MTX_SPIN);
2084069154d5SJulian Elischer }
2085069154d5SJulian Elischer 
2086069154d5SJulian Elischer /***********************************************************************
2087069154d5SJulian Elischer * Externally visible method for sending or queueing messages or data.
2088069154d5SJulian Elischer ***********************************************************************/
2089069154d5SJulian Elischer 
2090069154d5SJulian Elischer /*
2091069154d5SJulian Elischer  * MACRO WILL DO THE JOB OF CALLING ng_package_msg IN CALLER
2092069154d5SJulian Elischer  * before we are called. The user code should have filled out the item
2093069154d5SJulian Elischer  * correctly by this stage:
2094069154d5SJulian Elischer  * Common:
2095069154d5SJulian Elischer  *    reference to destination node.
2096069154d5SJulian Elischer  *    Reference to destination rcv hook if relevant.
2097069154d5SJulian Elischer  * Data:
2098069154d5SJulian Elischer  *    pointer to mbuf
2099069154d5SJulian Elischer  *    pointer to metadata
2100069154d5SJulian Elischer  * Control_Message:
2101069154d5SJulian Elischer  *    pointer to msg.
2102069154d5SJulian Elischer  *    ID of original sender node. (return address)
2103069154d5SJulian Elischer  *
2104069154d5SJulian Elischer  * The nodes have several routines and macros to help with this task:
2105069154d5SJulian Elischer  * ng_package_msg()
2106069154d5SJulian Elischer  * ng_package_data() do much of the work.
2107069154d5SJulian Elischer  * ng_retarget_msg
2108069154d5SJulian Elischer  * ng_retarget_data
2109069154d5SJulian Elischer  */
2110069154d5SJulian Elischer 
2111069154d5SJulian Elischer int
2112069154d5SJulian Elischer ng_snd_item(item_p item, int queue)
2113069154d5SJulian Elischer {
2114069154d5SJulian Elischer 	hook_p hook = item->el_hook;
2115069154d5SJulian Elischer 	node_p dest = item->el_dest;
2116069154d5SJulian Elischer 	int rw;
2117069154d5SJulian Elischer 	int error = 0, ierror;
2118069154d5SJulian Elischer 	item_p	oitem;
211930400f03SJulian Elischer 	struct ng_queue * ngq = &dest->nd_input_queue;
2120069154d5SJulian Elischer 
212130400f03SJulian Elischer #ifdef	NETGRAPH_DEBUG
2122069154d5SJulian Elischer         _ngi_check(item, __FILE__, __LINE__);
2123069154d5SJulian Elischer #endif
2124069154d5SJulian Elischer 
2125069154d5SJulian Elischer 	if (item == NULL) {
21266b795970SJulian Elischer 		TRAP_ERROR();
2127069154d5SJulian Elischer 		return (EINVAL);	/* failed to get queue element */
2128069154d5SJulian Elischer 	}
2129069154d5SJulian Elischer 	if (dest == NULL) {
2130069154d5SJulian Elischer 		NG_FREE_ITEM(item);
21316b795970SJulian Elischer 		TRAP_ERROR();
2132069154d5SJulian Elischer 		return (EINVAL);	/* No address */
2133069154d5SJulian Elischer 	}
21346b795970SJulian Elischer 	switch(item->el_flags & NGQF_TYPE) {
21356b795970SJulian Elischer 	case NGQF_DATA:
2136069154d5SJulian Elischer 		/*
2137069154d5SJulian Elischer 		 * DATA MESSAGE
2138069154d5SJulian Elischer 		 * Delivered to a node via a non-optional hook.
2139069154d5SJulian Elischer 		 * Both should be present in the item even though
2140069154d5SJulian Elischer 		 * the node is derivable from the hook.
2141069154d5SJulian Elischer 		 * References are held on both by the item.
2142069154d5SJulian Elischer 		 */
214330400f03SJulian Elischer #ifdef	NETGRAPH_DEBUG
2144069154d5SJulian Elischer         _ngi_check(item, __FILE__, __LINE__);
2145069154d5SJulian Elischer #endif
2146069154d5SJulian Elischer 		CHECK_DATA_MBUF(NGI_M(item));
2147069154d5SJulian Elischer 		if (hook == NULL) {
2148069154d5SJulian Elischer 			NG_FREE_ITEM(item);
21496b795970SJulian Elischer 			TRAP_ERROR();
2150069154d5SJulian Elischer 			return(EINVAL);
2151069154d5SJulian Elischer 		}
215230400f03SJulian Elischer 		if ((NG_HOOK_NOT_VALID(hook))
215330400f03SJulian Elischer 		|| (NG_NODE_NOT_VALID(NG_HOOK_NODE(hook)))) {
2154069154d5SJulian Elischer 			NG_FREE_ITEM(item);
2155069154d5SJulian Elischer 			return (ENOTCONN);
2156859a4d16SJulian Elischer 		}
215730400f03SJulian Elischer 		if ((hook->hk_flags & HK_QUEUE)) {
2158069154d5SJulian Elischer 			queue = 1;
21594cf49a43SJulian Elischer 		}
2160069154d5SJulian Elischer 		/* By default data is a reader in the locking scheme */
2161069154d5SJulian Elischer 		item->el_flags |= NGQF_READER;
2162069154d5SJulian Elischer 		rw = NGQRW_R;
21636b795970SJulian Elischer 		break;
21646b795970SJulian Elischer 	case NGQF_MESG:
21654cf49a43SJulian Elischer 		/*
2166069154d5SJulian Elischer 		 * CONTROL MESSAGE
2167069154d5SJulian Elischer 		 * Delivered to a node.
2168069154d5SJulian Elischer 		 * Hook is optional.
2169069154d5SJulian Elischer 		 * References are held by the item on the node and
2170069154d5SJulian Elischer 		 * the hook if it is present.
21714cf49a43SJulian Elischer 		 */
217230400f03SJulian Elischer 		if (hook && (hook->hk_flags & HK_QUEUE)) {
2173069154d5SJulian Elischer 			queue = 1;
2174069154d5SJulian Elischer 		}
2175069154d5SJulian Elischer 		/* Data messages count as writers unles explicitly exempted */
2176069154d5SJulian Elischer 		if (NGI_MSG(item)->header.cmd & NGM_READONLY) {
2177069154d5SJulian Elischer 			item->el_flags |= NGQF_READER;
2178069154d5SJulian Elischer 			rw = NGQRW_R;
2179069154d5SJulian Elischer 		} else {
21806b795970SJulian Elischer 			item->el_flags &= ~NGQF_RW;
2181069154d5SJulian Elischer 			rw = NGQRW_W;
2182069154d5SJulian Elischer 		}
21836b795970SJulian Elischer 		break;
21846b795970SJulian Elischer 	case NGQF_FN:
21856b795970SJulian Elischer 		item->el_flags &= ~NGQF_RW;
21866b795970SJulian Elischer 		rw = NGQRW_W;
21876b795970SJulian Elischer 		break;
21886b795970SJulian Elischer 	default:
21896b795970SJulian Elischer 		NG_FREE_ITEM(item);
21906b795970SJulian Elischer 		TRAP_ERROR();
21916b795970SJulian Elischer 		return (EINVAL);
2192069154d5SJulian Elischer 	}
2193069154d5SJulian Elischer 	/*
2194069154d5SJulian Elischer 	 * If the node specifies single threading, force writer semantics
2195069154d5SJulian Elischer 	 * Similarly the node may say one hook always produces writers.
2196069154d5SJulian Elischer 	 * These are over-rides.
2197069154d5SJulian Elischer 	 */
2198069154d5SJulian Elischer 	if ((ngq->q_flags & SINGLE_THREAD_ONLY)
219930400f03SJulian Elischer 	|| (dest->nd_flags & NG_FORCE_WRITER)
220030400f03SJulian Elischer 	|| (hook && (hook->hk_flags & HK_FORCE_WRITER))) {
2201069154d5SJulian Elischer 			rw = NGQRW_W;
22026b795970SJulian Elischer 			item->el_flags &= ~NGQF_READER;
2203069154d5SJulian Elischer 	}
2204069154d5SJulian Elischer 	if (queue) {
2205069154d5SJulian Elischer 		/* Put it on the queue for that node*/
220630400f03SJulian Elischer #ifdef	NETGRAPH_DEBUG
2207069154d5SJulian Elischer         _ngi_check(item, __FILE__, __LINE__);
2208069154d5SJulian Elischer #endif
2209069154d5SJulian Elischer 		mtx_enter(&(ngq->q_mtx), MTX_SPIN);
2210069154d5SJulian Elischer 		ng_queue_rw(ngq, item, rw);
2211069154d5SJulian Elischer 		mtx_exit(&(ngq->q_mtx), MTX_SPIN);
2212069154d5SJulian Elischer 		/*
2213069154d5SJulian Elischer 		 * If there are active elements then we can rely on
2214069154d5SJulian Elischer 		 * them. if not we should not rely on another packet
2215069154d5SJulian Elischer 		 * coming here by another path,
2216069154d5SJulian Elischer 		 * so it is best to put us in the netisr list.
2217069154d5SJulian Elischer 		 */
2218069154d5SJulian Elischer 		if ((ngq->q_flags & (READER_MASK|WRITER_ACTIVE)) == 0) {
2219069154d5SJulian Elischer 			ng_setisr(ngq->q_node);
2220069154d5SJulian Elischer 		}
2221069154d5SJulian Elischer 		return (0);
2222069154d5SJulian Elischer 	}
2223069154d5SJulian Elischer 	/*
2224069154d5SJulian Elischer 	 * Take a queue item and a node and see if we can apply the item to
2225069154d5SJulian Elischer 	 * the node. We may end up getting a different item to apply instead.
2226069154d5SJulian Elischer 	 * Will allow for a piggyback reply only in the case where
2227069154d5SJulian Elischer 	 * there is no queueing.
2228069154d5SJulian Elischer 	 */
2229069154d5SJulian Elischer 
2230069154d5SJulian Elischer 	oitem = item;
2231069154d5SJulian Elischer 	/*
2232069154d5SJulian Elischer 	 * We already decided how we will be queueud or treated.
2233069154d5SJulian Elischer 	 * Try get the appropriate operating permission.
2234069154d5SJulian Elischer 	 */
2235069154d5SJulian Elischer  	if (rw == NGQRW_R) {
2236069154d5SJulian Elischer 		item = ng_acquire_read(ngq, item);
2237069154d5SJulian Elischer 	} else {
2238069154d5SJulian Elischer 		item = ng_acquire_write(ngq, item);
22394cf49a43SJulian Elischer 	}
22404cf49a43SJulian Elischer 
22414cf49a43SJulian Elischer 	/*
2242069154d5SJulian Elischer 	 * May have come back with a different item.
2243069154d5SJulian Elischer 	 * or maybe none at all. The one we started with will
2244069154d5SJulian Elischer 	 * have been queued in thises cases.
2245069154d5SJulian Elischer 	 */
2246069154d5SJulian Elischer 	if (item == NULL) {
2247069154d5SJulian Elischer 		return (0);
2248069154d5SJulian Elischer 	}
2249069154d5SJulian Elischer 
225030400f03SJulian Elischer #ifdef	NETGRAPH_DEBUG
2251069154d5SJulian Elischer         _ngi_check(item, __FILE__, __LINE__);
2252069154d5SJulian Elischer #endif
2253069154d5SJulian Elischer 	ierror = ng_apply_item(dest, item); /* drops r/w lock when done */
2254069154d5SJulian Elischer 
2255069154d5SJulian Elischer 	/* only return an error if it was our initial item.. (compat hack) */
2256069154d5SJulian Elischer 	if (oitem == item) {
2257069154d5SJulian Elischer 		error = ierror;
2258069154d5SJulian Elischer 	}
2259069154d5SJulian Elischer 
2260069154d5SJulian Elischer 	/*
2261069154d5SJulian Elischer 	 * Now we've handled the packet we brought, (or a friend of it) let's
2262069154d5SJulian Elischer 	 * look for any other packets that may have been queued up. We hold
2263069154d5SJulian Elischer 	 * no locks, so if someone puts something in the queue after
2264069154d5SJulian Elischer 	 * we check that it is empty, it is their problem
2265069154d5SJulian Elischer 	 * to ensure it is processed. If we have the netisr thread cme in here
2266069154d5SJulian Elischer 	 * while we still say we have stuff to do, we may get a boost
2267069154d5SJulian Elischer 	 * in SMP systems. :-)
2268069154d5SJulian Elischer 	 */
2269069154d5SJulian Elischer 	for (;;) {
2270069154d5SJulian Elischer 		/* quick hack to save all that mutex stuff */
2271069154d5SJulian Elischer 		if ((ngq->q_flags & (WRITE_PENDING | READ_PENDING)) == 0) {
227230400f03SJulian Elischer 			if (dest->nd_flags & NG_WORKQ)
2273069154d5SJulian Elischer 				ng_worklist_remove(dest);
2274069154d5SJulian Elischer 			return (0);
2275069154d5SJulian Elischer 		}
2276069154d5SJulian Elischer 		/*
2277069154d5SJulian Elischer 		 * dequeue acquires and adjusts the input_queue as it dequeues
2278069154d5SJulian Elischer 		 * packets. It acquires the rw lock as needed.
2279069154d5SJulian Elischer 		 */
2280069154d5SJulian Elischer 		mtx_enter(&ngq->q_mtx, MTX_SPIN);
2281069154d5SJulian Elischer 		item = ng_dequeue(ngq);
2282069154d5SJulian Elischer 		mtx_exit(&ngq->q_mtx, MTX_SPIN);
2283069154d5SJulian Elischer 		if (!item) {
2284069154d5SJulian Elischer 			/*
2285069154d5SJulian Elischer 			 * If we have no work to do
2286069154d5SJulian Elischer 			 * then we certainly don't need to be
2287069154d5SJulian Elischer 			 * on the worklist.
2288069154d5SJulian Elischer 			 */
228930400f03SJulian Elischer 			if (dest->nd_flags & NG_WORKQ)
2290069154d5SJulian Elischer 				ng_worklist_remove(dest);
2291069154d5SJulian Elischer 			return (0);
2292069154d5SJulian Elischer 		}
229330400f03SJulian Elischer #ifdef	NETGRAPH_DEBUG
2294069154d5SJulian Elischer         _ngi_check(item, __FILE__, __LINE__);
2295069154d5SJulian Elischer #endif
2296069154d5SJulian Elischer 
2297069154d5SJulian Elischer 		/*
2298069154d5SJulian Elischer 		 * We have the appropriate lock, so run the item.
2299069154d5SJulian Elischer 		 * When finished it will drop the lock accordingly
2300069154d5SJulian Elischer 		 */
2301069154d5SJulian Elischer 
2302069154d5SJulian Elischer 		ierror = ng_apply_item(dest, item);
2303069154d5SJulian Elischer 		/*
2304069154d5SJulian Elischer 		 * only return an error if it was our initial
2305069154d5SJulian Elischer 		 * item.. (compat hack)
2306069154d5SJulian Elischer 		 */
2307069154d5SJulian Elischer 		if (oitem == item) {
2308069154d5SJulian Elischer 			error = ierror;
2309069154d5SJulian Elischer 		}
2310069154d5SJulian Elischer 	}
2311069154d5SJulian Elischer 	return (0);
2312069154d5SJulian Elischer }
2313069154d5SJulian Elischer 
2314069154d5SJulian Elischer /*
2315069154d5SJulian Elischer  * We have an item that was possibly queued somewhere.
2316069154d5SJulian Elischer  * It should contain all the information needed
2317069154d5SJulian Elischer  * to run it on the appropriate node/hook.
23184cf49a43SJulian Elischer  */
23194cf49a43SJulian Elischer static int
2320069154d5SJulian Elischer ng_apply_item(node_p node, item_p item)
2321069154d5SJulian Elischer {
2322069154d5SJulian Elischer 	hook_p  hook;
23236b795970SJulian Elischer 	int was_reader = ((item->el_flags & NGQF_RW));
2324069154d5SJulian Elischer 	int error = 0;
2325069154d5SJulian Elischer 	ng_rcvdata_t *rcvdata;
2326069154d5SJulian Elischer 
2327069154d5SJulian Elischer 	hook = item->el_hook;
232830400f03SJulian Elischer 	item->el_hook = NULL;	/* so NG_FREE_ITEM doesn't NG_HOOK_UNREF() */
2329069154d5SJulian Elischer 	/* We already have the node.. assume responsibility */
2330069154d5SJulian Elischer 	/* And the reference */
2331069154d5SJulian Elischer 	/* node = item->el_dest; */
2332069154d5SJulian Elischer 	item->el_dest = NULL;	/* same as for the hook above */
233330400f03SJulian Elischer #ifdef	NETGRAPH_DEBUG
2334069154d5SJulian Elischer         _ngi_check(item, __FILE__, __LINE__);
2335069154d5SJulian Elischer #endif
23366b795970SJulian Elischer 	switch (item->el_flags & NGQF_TYPE) {
2337069154d5SJulian Elischer 	case NGQF_DATA:
2338069154d5SJulian Elischer 		/*
2339069154d5SJulian Elischer 		 * Check things are still ok as when we were queued.
2340069154d5SJulian Elischer 		 */
2341069154d5SJulian Elischer 
2342069154d5SJulian Elischer 		if ((hook == NULL)
234330400f03SJulian Elischer 		|| NG_HOOK_NOT_VALID(hook)
23446b795970SJulian Elischer 		|| NG_NODE_NOT_VALID(node)
234530400f03SJulian Elischer 		|| ((rcvdata = NG_HOOK_NODE(hook)->nd_type->rcvdata) == NULL)) {
2346069154d5SJulian Elischer 			error = EIO;
2347069154d5SJulian Elischer 			NG_FREE_ITEM(item);
2348069154d5SJulian Elischer 		} else {
2349069154d5SJulian Elischer 			error = (*rcvdata)(hook, item);
2350069154d5SJulian Elischer 		}
2351069154d5SJulian Elischer 		break;
2352069154d5SJulian Elischer 	case NGQF_MESG:
2353069154d5SJulian Elischer 		if (hook) {
2354069154d5SJulian Elischer 			item->el_hook = NULL;
235530400f03SJulian Elischer 			if (NG_HOOK_NOT_VALID(hook)) {
2356069154d5SJulian Elischer 			/*
2357069154d5SJulian Elischer 			 * If the hook has been zapped then we can't use it.
2358069154d5SJulian Elischer 			 * Immediatly drop its reference.
2359069154d5SJulian Elischer 			 * The message may not need it.
2360069154d5SJulian Elischer 			 */
236130400f03SJulian Elischer 				NG_HOOK_UNREF(hook);
2362069154d5SJulian Elischer 				hook = NULL;
2363069154d5SJulian Elischer 			}
2364069154d5SJulian Elischer 		}
2365069154d5SJulian Elischer 		/*
2366069154d5SJulian Elischer 		 * Similarly, if the node is a zombie there is
2367069154d5SJulian Elischer 		 * nothing we can do with it, drop everything.
2368069154d5SJulian Elischer 		 */
236930400f03SJulian Elischer 		if (NG_NODE_NOT_VALID(node)) {
23706b795970SJulian Elischer 			TRAP_ERROR();
2371069154d5SJulian Elischer 			error = EINVAL;
2372069154d5SJulian Elischer 			NG_FREE_ITEM(item);
2373069154d5SJulian Elischer 		} else {
2374069154d5SJulian Elischer 			/*
2375069154d5SJulian Elischer 			 * Call the appropriate message handler for the object.
2376069154d5SJulian Elischer 			 * It is up to the message handler to free the message.
2377069154d5SJulian Elischer 			 * If it's a generic message, handle it generically,
2378069154d5SJulian Elischer 			 * otherwise call the type's message handler
2379069154d5SJulian Elischer 			 * (if it exists)
2380069154d5SJulian Elischer 			 * XXX (race). Remember that a queued message may
2381069154d5SJulian Elischer 			 * reference a node or hook that has just been
2382069154d5SJulian Elischer 			 * invalidated. It will exist as the queue code
2383069154d5SJulian Elischer 			 * is holding a reference, but..
2384069154d5SJulian Elischer 			 */
2385069154d5SJulian Elischer 
2386069154d5SJulian Elischer 			struct ng_mesg *msg = NGI_MSG(item);
2387069154d5SJulian Elischer 
2388069154d5SJulian Elischer 			if ((msg->header.typecookie == NGM_GENERIC_COOKIE)
2389069154d5SJulian Elischer 			&& ((msg->header.flags & NGF_RESP) == 0)) {
2390069154d5SJulian Elischer 				error = ng_generic_msg(node, item, hook);
2391069154d5SJulian Elischer 			} else {
239230400f03SJulian Elischer 				if ((node)->nd_type->rcvmsg != NULL) {
239330400f03SJulian Elischer 					error = (*(node)->nd_type->rcvmsg)((node),
2394069154d5SJulian Elischer 						(item), (hook));
2395069154d5SJulian Elischer 				} else {
23966b795970SJulian Elischer 					TRAP_ERROR();
2397069154d5SJulian Elischer 					error = EINVAL; /* XXX */
2398069154d5SJulian Elischer 					NG_FREE_ITEM(item);
2399069154d5SJulian Elischer 				}
2400069154d5SJulian Elischer 			}
2401069154d5SJulian Elischer 			/* item is now invalid */
2402069154d5SJulian Elischer 		}
2403069154d5SJulian Elischer 		break;
24046b795970SJulian Elischer 	case NGQF_FN:
24056b795970SJulian Elischer 		/*
24066b795970SJulian Elischer 		 *  We have to implicitly trust the hook,
24076b795970SJulian Elischer 		 * as some of these are used for system purposes
24086b795970SJulian Elischer 		 * where the hook is invalid.
24096b795970SJulian Elischer 		 */
24106b795970SJulian Elischer 		if (NG_NODE_NOT_VALID(node)) {
24116b795970SJulian Elischer 			TRAP_ERROR();
24126b795970SJulian Elischer 			error = EINVAL;
24136b795970SJulian Elischer 			break;
24146b795970SJulian Elischer 		}
24156b795970SJulian Elischer 		error =
24166b795970SJulian Elischer 		    (*NGI_FN(item))(node, hook, NGI_ARG1(item), NGI_ARG2(item));
24176b795970SJulian Elischer 
24186b795970SJulian Elischer 		NG_FREE_ITEM(item);
24196b795970SJulian Elischer 		break;
24206b795970SJulian Elischer 
2421069154d5SJulian Elischer 	}
2422069154d5SJulian Elischer 	/*
2423069154d5SJulian Elischer 	 * We held references on some of the resources
2424069154d5SJulian Elischer 	 * that we took from the item. Now that we have
2425069154d5SJulian Elischer 	 * finished doing everything, drop those references.
2426069154d5SJulian Elischer 	 */
2427069154d5SJulian Elischer 	if (hook) {
242830400f03SJulian Elischer 		NG_HOOK_UNREF(hook);
2429069154d5SJulian Elischer 	}
2430069154d5SJulian Elischer 
2431069154d5SJulian Elischer 	if (was_reader) {
243230400f03SJulian Elischer 		ng_leave_read(&node->nd_input_queue);
2433069154d5SJulian Elischer 	} else {
243430400f03SJulian Elischer 		ng_leave_write(&node->nd_input_queue);
2435069154d5SJulian Elischer 	}
243630400f03SJulian Elischer 	NG_NODE_UNREF(node);
2437069154d5SJulian Elischer 	return (error);
2438069154d5SJulian Elischer }
2439069154d5SJulian Elischer 
2440069154d5SJulian Elischer /***********************************************************************
2441069154d5SJulian Elischer  * Implement the 'generic' control messages
2442069154d5SJulian Elischer  ***********************************************************************/
2443069154d5SJulian Elischer static int
2444069154d5SJulian Elischer ng_generic_msg(node_p here, item_p item, hook_p lasthook)
24454cf49a43SJulian Elischer {
24464cf49a43SJulian Elischer 	int error = 0;
2447069154d5SJulian Elischer 	struct ng_mesg *msg;
2448069154d5SJulian Elischer 	struct ng_mesg *resp = NULL;
24494cf49a43SJulian Elischer 
2450069154d5SJulian Elischer 	NGI_GET_MSG(item, msg);
24514cf49a43SJulian Elischer 	if (msg->header.typecookie != NGM_GENERIC_COOKIE) {
24526b795970SJulian Elischer 		TRAP_ERROR();
2453069154d5SJulian Elischer 		error = EINVAL;
2454069154d5SJulian Elischer 		goto out;
24554cf49a43SJulian Elischer 	}
24564cf49a43SJulian Elischer 	switch (msg->header.cmd) {
24574cf49a43SJulian Elischer 	case NGM_SHUTDOWN:
24584cf49a43SJulian Elischer 		ng_rmnode(here);
24594cf49a43SJulian Elischer 		break;
24604cf49a43SJulian Elischer 	case NGM_MKPEER:
24614cf49a43SJulian Elischer 	    {
24624cf49a43SJulian Elischer 		struct ngm_mkpeer *const mkp = (struct ngm_mkpeer *) msg->data;
24634cf49a43SJulian Elischer 
24644cf49a43SJulian Elischer 		if (msg->header.arglen != sizeof(*mkp)) {
24656b795970SJulian Elischer 			TRAP_ERROR();
2466069154d5SJulian Elischer 			error = EINVAL;
2467069154d5SJulian Elischer 			break;
24684cf49a43SJulian Elischer 		}
24694cf49a43SJulian Elischer 		mkp->type[sizeof(mkp->type) - 1] = '\0';
24704cf49a43SJulian Elischer 		mkp->ourhook[sizeof(mkp->ourhook) - 1] = '\0';
24714cf49a43SJulian Elischer 		mkp->peerhook[sizeof(mkp->peerhook) - 1] = '\0';
24724cf49a43SJulian Elischer 		error = ng_mkpeer(here, mkp->ourhook, mkp->peerhook, mkp->type);
24734cf49a43SJulian Elischer 		break;
24744cf49a43SJulian Elischer 	    }
24754cf49a43SJulian Elischer 	case NGM_CONNECT:
24764cf49a43SJulian Elischer 	    {
24774cf49a43SJulian Elischer 		struct ngm_connect *const con =
24784cf49a43SJulian Elischer 			(struct ngm_connect *) msg->data;
24794cf49a43SJulian Elischer 		node_p node2;
24804cf49a43SJulian Elischer 
24814cf49a43SJulian Elischer 		if (msg->header.arglen != sizeof(*con)) {
24826b795970SJulian Elischer 			TRAP_ERROR();
2483069154d5SJulian Elischer 			error = EINVAL;
2484069154d5SJulian Elischer 			break;
24854cf49a43SJulian Elischer 		}
24864cf49a43SJulian Elischer 		con->path[sizeof(con->path) - 1] = '\0';
24874cf49a43SJulian Elischer 		con->ourhook[sizeof(con->ourhook) - 1] = '\0';
24884cf49a43SJulian Elischer 		con->peerhook[sizeof(con->peerhook) - 1] = '\0';
2489069154d5SJulian Elischer 		/* Don't forget we get a reference.. */
2490069154d5SJulian Elischer 		error = ng_path2noderef(here, con->path, &node2, NULL);
24914cf49a43SJulian Elischer 		if (error)
24924cf49a43SJulian Elischer 			break;
24934cf49a43SJulian Elischer 		error = ng_con_nodes(here, con->ourhook, node2, con->peerhook);
249430400f03SJulian Elischer 		NG_NODE_UNREF(node2);
24954cf49a43SJulian Elischer 		break;
24964cf49a43SJulian Elischer 	    }
24974cf49a43SJulian Elischer 	case NGM_NAME:
24984cf49a43SJulian Elischer 	    {
24994cf49a43SJulian Elischer 		struct ngm_name *const nam = (struct ngm_name *) msg->data;
25004cf49a43SJulian Elischer 
25014cf49a43SJulian Elischer 		if (msg->header.arglen != sizeof(*nam)) {
25026b795970SJulian Elischer 			TRAP_ERROR();
2503069154d5SJulian Elischer 			error = EINVAL;
2504069154d5SJulian Elischer 			break;
25054cf49a43SJulian Elischer 		}
25064cf49a43SJulian Elischer 		nam->name[sizeof(nam->name) - 1] = '\0';
25074cf49a43SJulian Elischer 		error = ng_name_node(here, nam->name);
25084cf49a43SJulian Elischer 		break;
25094cf49a43SJulian Elischer 	    }
25104cf49a43SJulian Elischer 	case NGM_RMHOOK:
25114cf49a43SJulian Elischer 	    {
25124cf49a43SJulian Elischer 		struct ngm_rmhook *const rmh = (struct ngm_rmhook *) msg->data;
25134cf49a43SJulian Elischer 		hook_p hook;
25144cf49a43SJulian Elischer 
25154cf49a43SJulian Elischer 		if (msg->header.arglen != sizeof(*rmh)) {
25166b795970SJulian Elischer 			TRAP_ERROR();
2517069154d5SJulian Elischer 			error = EINVAL;
2518069154d5SJulian Elischer 			break;
25194cf49a43SJulian Elischer 		}
25204cf49a43SJulian Elischer 		rmh->ourhook[sizeof(rmh->ourhook) - 1] = '\0';
2521899e9c4eSArchie Cobbs 		if ((hook = ng_findhook(here, rmh->ourhook)) != NULL)
25224cf49a43SJulian Elischer 			ng_destroy_hook(hook);
25234cf49a43SJulian Elischer 		break;
25244cf49a43SJulian Elischer 	    }
25254cf49a43SJulian Elischer 	case NGM_NODEINFO:
25264cf49a43SJulian Elischer 	    {
25274cf49a43SJulian Elischer 		struct nodeinfo *ni;
25284cf49a43SJulian Elischer 
2529069154d5SJulian Elischer 		NG_MKRESPONSE(resp, msg, sizeof(*ni), M_NOWAIT);
25304cf49a43SJulian Elischer 		if (resp == NULL) {
25314cf49a43SJulian Elischer 			error = ENOMEM;
25324cf49a43SJulian Elischer 			break;
25334cf49a43SJulian Elischer 		}
25344cf49a43SJulian Elischer 
25354cf49a43SJulian Elischer 		/* Fill in node info */
2536069154d5SJulian Elischer 		ni = (struct nodeinfo *) resp->data;
253730400f03SJulian Elischer 		if (NG_NODE_HAS_NAME(here))
253830400f03SJulian Elischer 			strncpy(ni->name, NG_NODE_NAME(here), NG_NODELEN);
253930400f03SJulian Elischer 		strncpy(ni->type, here->nd_type->name, NG_TYPELEN);
2540dc90cad9SJulian Elischer 		ni->id = ng_node2ID(here);
254130400f03SJulian Elischer 		ni->hooks = here->nd_numhooks;
25424cf49a43SJulian Elischer 		break;
25434cf49a43SJulian Elischer 	    }
25444cf49a43SJulian Elischer 	case NGM_LISTHOOKS:
25454cf49a43SJulian Elischer 	    {
254630400f03SJulian Elischer 		const int nhooks = here->nd_numhooks;
25474cf49a43SJulian Elischer 		struct hooklist *hl;
25484cf49a43SJulian Elischer 		struct nodeinfo *ni;
25494cf49a43SJulian Elischer 		hook_p hook;
25504cf49a43SJulian Elischer 
25514cf49a43SJulian Elischer 		/* Get response struct */
2552069154d5SJulian Elischer 		NG_MKRESPONSE(resp, msg, sizeof(*hl)
25534cf49a43SJulian Elischer 		    + (nhooks * sizeof(struct linkinfo)), M_NOWAIT);
2554069154d5SJulian Elischer 		if (resp == NULL) {
25554cf49a43SJulian Elischer 			error = ENOMEM;
25564cf49a43SJulian Elischer 			break;
25574cf49a43SJulian Elischer 		}
2558069154d5SJulian Elischer 		hl = (struct hooklist *) resp->data;
25594cf49a43SJulian Elischer 		ni = &hl->nodeinfo;
25604cf49a43SJulian Elischer 
25614cf49a43SJulian Elischer 		/* Fill in node info */
256230400f03SJulian Elischer 		if (NG_NODE_HAS_NAME(here))
256330400f03SJulian Elischer 			strncpy(ni->name, NG_NODE_NAME(here), NG_NODELEN);
256430400f03SJulian Elischer 		strncpy(ni->type, here->nd_type->name, NG_TYPELEN);
2565dc90cad9SJulian Elischer 		ni->id = ng_node2ID(here);
25664cf49a43SJulian Elischer 
25674cf49a43SJulian Elischer 		/* Cycle through the linked list of hooks */
25684cf49a43SJulian Elischer 		ni->hooks = 0;
256930400f03SJulian Elischer 		LIST_FOREACH(hook, &here->nd_hooks, hk_hooks) {
25704cf49a43SJulian Elischer 			struct linkinfo *const link = &hl->link[ni->hooks];
25714cf49a43SJulian Elischer 
25724cf49a43SJulian Elischer 			if (ni->hooks >= nhooks) {
25734cf49a43SJulian Elischer 				log(LOG_ERR, "%s: number of %s changed\n",
25744cf49a43SJulian Elischer 				    __FUNCTION__, "hooks");
25754cf49a43SJulian Elischer 				break;
25764cf49a43SJulian Elischer 			}
257730400f03SJulian Elischer 			if (NG_HOOK_NOT_VALID(hook))
25784cf49a43SJulian Elischer 				continue;
257930400f03SJulian Elischer 			strncpy(link->ourhook, NG_HOOK_NAME(hook), NG_HOOKLEN);
258030400f03SJulian Elischer 			strncpy(link->peerhook,
258130400f03SJulian Elischer 				NG_PEER_HOOK_NAME(hook), NG_HOOKLEN);
258230400f03SJulian Elischer 			if (NG_PEER_NODE_NAME(hook)[0] != '\0')
25834cf49a43SJulian Elischer 				strncpy(link->nodeinfo.name,
258430400f03SJulian Elischer 				    NG_PEER_NODE_NAME(hook), NG_NODELEN);
25854cf49a43SJulian Elischer 			strncpy(link->nodeinfo.type,
258630400f03SJulian Elischer 			   NG_PEER_NODE(hook)->nd_type->name, NG_TYPELEN);
258730400f03SJulian Elischer 			link->nodeinfo.id = ng_node2ID(NG_PEER_NODE(hook));
258830400f03SJulian Elischer 			link->nodeinfo.hooks = NG_PEER_NODE(hook)->nd_numhooks;
25894cf49a43SJulian Elischer 			ni->hooks++;
25904cf49a43SJulian Elischer 		}
25914cf49a43SJulian Elischer 		break;
25924cf49a43SJulian Elischer 	    }
25934cf49a43SJulian Elischer 
25944cf49a43SJulian Elischer 	case NGM_LISTNAMES:
25954cf49a43SJulian Elischer 	case NGM_LISTNODES:
25964cf49a43SJulian Elischer 	    {
25974cf49a43SJulian Elischer 		const int unnamed = (msg->header.cmd == NGM_LISTNODES);
25984cf49a43SJulian Elischer 		struct namelist *nl;
25994cf49a43SJulian Elischer 		node_p node;
26004cf49a43SJulian Elischer 		int num = 0;
26014cf49a43SJulian Elischer 
2602069154d5SJulian Elischer 		mtx_enter(&ng_nodelist_mtx, MTX_DEF);
26034cf49a43SJulian Elischer 		/* Count number of nodes */
260430400f03SJulian Elischer 		LIST_FOREACH(node, &ng_nodelist, nd_nodes) {
260570de87f2SJulian Elischer 			if (NG_NODE_IS_VALID(node)
260670de87f2SJulian Elischer 			&& (unnamed || NG_NODE_HAS_NAME(node))) {
26074cf49a43SJulian Elischer 				num++;
26084cf49a43SJulian Elischer 			}
260970de87f2SJulian Elischer 		}
2610069154d5SJulian Elischer 		mtx_exit(&ng_nodelist_mtx, MTX_DEF);
26114cf49a43SJulian Elischer 
26124cf49a43SJulian Elischer 		/* Get response struct */
2613069154d5SJulian Elischer 		NG_MKRESPONSE(resp, msg, sizeof(*nl)
26144cf49a43SJulian Elischer 		    + (num * sizeof(struct nodeinfo)), M_NOWAIT);
2615069154d5SJulian Elischer 		if (resp == NULL) {
26164cf49a43SJulian Elischer 			error = ENOMEM;
26174cf49a43SJulian Elischer 			break;
26184cf49a43SJulian Elischer 		}
2619069154d5SJulian Elischer 		nl = (struct namelist *) resp->data;
26204cf49a43SJulian Elischer 
26214cf49a43SJulian Elischer 		/* Cycle through the linked list of nodes */
26224cf49a43SJulian Elischer 		nl->numnames = 0;
2623069154d5SJulian Elischer 		mtx_enter(&ng_nodelist_mtx, MTX_DEF);
262430400f03SJulian Elischer 		LIST_FOREACH(node, &ng_nodelist, nd_nodes) {
26254cf49a43SJulian Elischer 			struct nodeinfo *const np = &nl->nodeinfo[nl->numnames];
26264cf49a43SJulian Elischer 
26274cf49a43SJulian Elischer 			if (nl->numnames >= num) {
26284cf49a43SJulian Elischer 				log(LOG_ERR, "%s: number of %s changed\n",
26294cf49a43SJulian Elischer 				    __FUNCTION__, "nodes");
26304cf49a43SJulian Elischer 				break;
26314cf49a43SJulian Elischer 			}
263230400f03SJulian Elischer 			if (NG_NODE_NOT_VALID(node))
26334cf49a43SJulian Elischer 				continue;
263430400f03SJulian Elischer 			if (!unnamed && (! NG_NODE_HAS_NAME(node)))
26354cf49a43SJulian Elischer 				continue;
263630400f03SJulian Elischer 			if (NG_NODE_HAS_NAME(node))
263730400f03SJulian Elischer 				strncpy(np->name, NG_NODE_NAME(node), NG_NODELEN);
263830400f03SJulian Elischer 			strncpy(np->type, node->nd_type->name, NG_TYPELEN);
2639dc90cad9SJulian Elischer 			np->id = ng_node2ID(node);
264030400f03SJulian Elischer 			np->hooks = node->nd_numhooks;
26414cf49a43SJulian Elischer 			nl->numnames++;
26424cf49a43SJulian Elischer 		}
2643069154d5SJulian Elischer 		mtx_exit(&ng_nodelist_mtx, MTX_DEF);
26444cf49a43SJulian Elischer 		break;
26454cf49a43SJulian Elischer 	    }
26464cf49a43SJulian Elischer 
26474cf49a43SJulian Elischer 	case NGM_LISTTYPES:
26484cf49a43SJulian Elischer 	    {
26494cf49a43SJulian Elischer 		struct typelist *tl;
26504cf49a43SJulian Elischer 		struct ng_type *type;
26514cf49a43SJulian Elischer 		int num = 0;
26524cf49a43SJulian Elischer 
2653069154d5SJulian Elischer 		mtx_enter(&ng_typelist_mtx, MTX_DEF);
26544cf49a43SJulian Elischer 		/* Count number of types */
265570de87f2SJulian Elischer 		LIST_FOREACH(type, &ng_typelist, types) {
26564cf49a43SJulian Elischer 			num++;
265770de87f2SJulian Elischer 		}
2658069154d5SJulian Elischer 		mtx_exit(&ng_typelist_mtx, MTX_DEF);
26594cf49a43SJulian Elischer 
26604cf49a43SJulian Elischer 		/* Get response struct */
2661069154d5SJulian Elischer 		NG_MKRESPONSE(resp, msg, sizeof(*tl)
26624cf49a43SJulian Elischer 		    + (num * sizeof(struct typeinfo)), M_NOWAIT);
2663069154d5SJulian Elischer 		if (resp == NULL) {
26644cf49a43SJulian Elischer 			error = ENOMEM;
26654cf49a43SJulian Elischer 			break;
26664cf49a43SJulian Elischer 		}
2667069154d5SJulian Elischer 		tl = (struct typelist *) resp->data;
26684cf49a43SJulian Elischer 
26694cf49a43SJulian Elischer 		/* Cycle through the linked list of types */
26704cf49a43SJulian Elischer 		tl->numtypes = 0;
2671069154d5SJulian Elischer 		mtx_enter(&ng_typelist_mtx, MTX_DEF);
2672069154d5SJulian Elischer 		LIST_FOREACH(type, &ng_typelist, types) {
26734cf49a43SJulian Elischer 			struct typeinfo *const tp = &tl->typeinfo[tl->numtypes];
26744cf49a43SJulian Elischer 
26754cf49a43SJulian Elischer 			if (tl->numtypes >= num) {
26764cf49a43SJulian Elischer 				log(LOG_ERR, "%s: number of %s changed\n",
26774cf49a43SJulian Elischer 				    __FUNCTION__, "types");
26784cf49a43SJulian Elischer 				break;
26794cf49a43SJulian Elischer 			}
2680a096e45aSArchie Cobbs 			strncpy(tp->type_name, type->name, NG_TYPELEN);
2681c73b94a2SJulian Elischer 			tp->numnodes = type->refs - 1; /* don't count list */
26824cf49a43SJulian Elischer 			tl->numtypes++;
26834cf49a43SJulian Elischer 		}
2684069154d5SJulian Elischer 		mtx_exit(&ng_typelist_mtx, MTX_DEF);
26854cf49a43SJulian Elischer 		break;
26864cf49a43SJulian Elischer 	    }
26874cf49a43SJulian Elischer 
2688f8307e12SArchie Cobbs 	case NGM_BINARY2ASCII:
2689f8307e12SArchie Cobbs 	    {
26907133ac27SArchie Cobbs 		int bufSize = 20 * 1024;	/* XXX hard coded constant */
2691f8307e12SArchie Cobbs 		const struct ng_parse_type *argstype;
2692f8307e12SArchie Cobbs 		const struct ng_cmdlist *c;
2693069154d5SJulian Elischer 		struct ng_mesg *binary, *ascii;
2694f8307e12SArchie Cobbs 
2695f8307e12SArchie Cobbs 		/* Data area must contain a valid netgraph message */
2696f8307e12SArchie Cobbs 		binary = (struct ng_mesg *)msg->data;
2697f8307e12SArchie Cobbs 		if (msg->header.arglen < sizeof(struct ng_mesg)
269870de87f2SJulian Elischer 		    || (msg->header.arglen - sizeof(struct ng_mesg)
269970de87f2SJulian Elischer 		      < binary->header.arglen)) {
27006b795970SJulian Elischer 			TRAP_ERROR();
2701f8307e12SArchie Cobbs 			error = EINVAL;
2702f8307e12SArchie Cobbs 			break;
2703f8307e12SArchie Cobbs 		}
2704f8307e12SArchie Cobbs 
2705f8307e12SArchie Cobbs 		/* Get a response message with lots of room */
2706069154d5SJulian Elischer 		NG_MKRESPONSE(resp, msg, sizeof(*ascii) + bufSize, M_NOWAIT);
2707069154d5SJulian Elischer 		if (resp == NULL) {
2708f8307e12SArchie Cobbs 			error = ENOMEM;
2709f8307e12SArchie Cobbs 			break;
2710f8307e12SArchie Cobbs 		}
2711069154d5SJulian Elischer 		ascii = (struct ng_mesg *)resp->data;
2712f8307e12SArchie Cobbs 
2713f8307e12SArchie Cobbs 		/* Copy binary message header to response message payload */
2714f8307e12SArchie Cobbs 		bcopy(binary, ascii, sizeof(*binary));
2715f8307e12SArchie Cobbs 
2716f8307e12SArchie Cobbs 		/* Find command by matching typecookie and command number */
271730400f03SJulian Elischer 		for (c = here->nd_type->cmdlist;
2718f8307e12SArchie Cobbs 		    c != NULL && c->name != NULL; c++) {
2719f8307e12SArchie Cobbs 			if (binary->header.typecookie == c->cookie
2720f8307e12SArchie Cobbs 			    && binary->header.cmd == c->cmd)
2721f8307e12SArchie Cobbs 				break;
2722f8307e12SArchie Cobbs 		}
2723f8307e12SArchie Cobbs 		if (c == NULL || c->name == NULL) {
2724f8307e12SArchie Cobbs 			for (c = ng_generic_cmds; c->name != NULL; c++) {
2725f8307e12SArchie Cobbs 				if (binary->header.typecookie == c->cookie
2726f8307e12SArchie Cobbs 				    && binary->header.cmd == c->cmd)
2727f8307e12SArchie Cobbs 					break;
2728f8307e12SArchie Cobbs 			}
2729f8307e12SArchie Cobbs 			if (c->name == NULL) {
2730069154d5SJulian Elischer 				NG_FREE_MSG(resp);
2731f8307e12SArchie Cobbs 				error = ENOSYS;
2732f8307e12SArchie Cobbs 				break;
2733f8307e12SArchie Cobbs 			}
2734f8307e12SArchie Cobbs 		}
2735f8307e12SArchie Cobbs 
2736f8307e12SArchie Cobbs 		/* Convert command name to ASCII */
2737f8307e12SArchie Cobbs 		snprintf(ascii->header.cmdstr, sizeof(ascii->header.cmdstr),
2738f8307e12SArchie Cobbs 		    "%s", c->name);
2739f8307e12SArchie Cobbs 
2740f8307e12SArchie Cobbs 		/* Convert command arguments to ASCII */
2741f8307e12SArchie Cobbs 		argstype = (binary->header.flags & NGF_RESP) ?
2742f8307e12SArchie Cobbs 		    c->respType : c->mesgType;
274370de87f2SJulian Elischer 		if (argstype == NULL) {
2744f8307e12SArchie Cobbs 			*ascii->data = '\0';
274570de87f2SJulian Elischer 		} else {
2746f8307e12SArchie Cobbs 			if ((error = ng_unparse(argstype,
2747f8307e12SArchie Cobbs 			    (u_char *)binary->data,
2748f8307e12SArchie Cobbs 			    ascii->data, bufSize)) != 0) {
2749069154d5SJulian Elischer 				NG_FREE_MSG(resp);
2750f8307e12SArchie Cobbs 				break;
2751f8307e12SArchie Cobbs 			}
2752f8307e12SArchie Cobbs 		}
2753f8307e12SArchie Cobbs 
2754f8307e12SArchie Cobbs 		/* Return the result as struct ng_mesg plus ASCII string */
2755f8307e12SArchie Cobbs 		bufSize = strlen(ascii->data) + 1;
2756f8307e12SArchie Cobbs 		ascii->header.arglen = bufSize;
2757069154d5SJulian Elischer 		resp->header.arglen = sizeof(*ascii) + bufSize;
2758f8307e12SArchie Cobbs 		break;
2759f8307e12SArchie Cobbs 	    }
2760f8307e12SArchie Cobbs 
2761f8307e12SArchie Cobbs 	case NGM_ASCII2BINARY:
2762f8307e12SArchie Cobbs 	    {
2763f8307e12SArchie Cobbs 		int bufSize = 2000;	/* XXX hard coded constant */
2764f8307e12SArchie Cobbs 		const struct ng_cmdlist *c;
2765f8307e12SArchie Cobbs 		const struct ng_parse_type *argstype;
2766069154d5SJulian Elischer 		struct ng_mesg *ascii, *binary;
276752ec4a03SArchie Cobbs 		int off = 0;
2768f8307e12SArchie Cobbs 
2769f8307e12SArchie Cobbs 		/* Data area must contain at least a struct ng_mesg + '\0' */
2770f8307e12SArchie Cobbs 		ascii = (struct ng_mesg *)msg->data;
277170de87f2SJulian Elischer 		if ((msg->header.arglen < sizeof(*ascii) + 1)
277270de87f2SJulian Elischer 		    || (ascii->header.arglen < 1)
277370de87f2SJulian Elischer 		    || (msg->header.arglen
277470de87f2SJulian Elischer 		      < sizeof(*ascii) + ascii->header.arglen)) {
27756b795970SJulian Elischer 			TRAP_ERROR();
2776f8307e12SArchie Cobbs 			error = EINVAL;
2777f8307e12SArchie Cobbs 			break;
2778f8307e12SArchie Cobbs 		}
2779f8307e12SArchie Cobbs 		ascii->data[ascii->header.arglen - 1] = '\0';
2780f8307e12SArchie Cobbs 
2781f8307e12SArchie Cobbs 		/* Get a response message with lots of room */
2782069154d5SJulian Elischer 		NG_MKRESPONSE(resp, msg, sizeof(*binary) + bufSize, M_NOWAIT);
2783069154d5SJulian Elischer 		if (resp == NULL) {
2784f8307e12SArchie Cobbs 			error = ENOMEM;
2785f8307e12SArchie Cobbs 			break;
2786f8307e12SArchie Cobbs 		}
2787069154d5SJulian Elischer 		binary = (struct ng_mesg *)resp->data;
2788f8307e12SArchie Cobbs 
2789f8307e12SArchie Cobbs 		/* Copy ASCII message header to response message payload */
2790f8307e12SArchie Cobbs 		bcopy(ascii, binary, sizeof(*ascii));
2791f8307e12SArchie Cobbs 
2792f8307e12SArchie Cobbs 		/* Find command by matching ASCII command string */
279330400f03SJulian Elischer 		for (c = here->nd_type->cmdlist;
2794f8307e12SArchie Cobbs 		    c != NULL && c->name != NULL; c++) {
2795f8307e12SArchie Cobbs 			if (strcmp(ascii->header.cmdstr, c->name) == 0)
2796f8307e12SArchie Cobbs 				break;
2797f8307e12SArchie Cobbs 		}
2798f8307e12SArchie Cobbs 		if (c == NULL || c->name == NULL) {
2799f8307e12SArchie Cobbs 			for (c = ng_generic_cmds; c->name != NULL; c++) {
2800f8307e12SArchie Cobbs 				if (strcmp(ascii->header.cmdstr, c->name) == 0)
2801f8307e12SArchie Cobbs 					break;
2802f8307e12SArchie Cobbs 			}
2803f8307e12SArchie Cobbs 			if (c->name == NULL) {
2804069154d5SJulian Elischer 				NG_FREE_MSG(resp);
2805f8307e12SArchie Cobbs 				error = ENOSYS;
2806f8307e12SArchie Cobbs 				break;
2807f8307e12SArchie Cobbs 			}
2808f8307e12SArchie Cobbs 		}
2809f8307e12SArchie Cobbs 
2810f8307e12SArchie Cobbs 		/* Convert command name to binary */
2811f8307e12SArchie Cobbs 		binary->header.cmd = c->cmd;
2812f8307e12SArchie Cobbs 		binary->header.typecookie = c->cookie;
2813f8307e12SArchie Cobbs 
2814f8307e12SArchie Cobbs 		/* Convert command arguments to binary */
2815f8307e12SArchie Cobbs 		argstype = (binary->header.flags & NGF_RESP) ?
2816f8307e12SArchie Cobbs 		    c->respType : c->mesgType;
281770de87f2SJulian Elischer 		if (argstype == NULL) {
2818f8307e12SArchie Cobbs 			bufSize = 0;
281970de87f2SJulian Elischer 		} else {
2820f8307e12SArchie Cobbs 			if ((error = ng_parse(argstype, ascii->data,
2821f8307e12SArchie Cobbs 			    &off, (u_char *)binary->data, &bufSize)) != 0) {
2822069154d5SJulian Elischer 				NG_FREE_MSG(resp);
2823f8307e12SArchie Cobbs 				break;
2824f8307e12SArchie Cobbs 			}
2825f8307e12SArchie Cobbs 		}
2826f8307e12SArchie Cobbs 
2827f8307e12SArchie Cobbs 		/* Return the result */
2828f8307e12SArchie Cobbs 		binary->header.arglen = bufSize;
2829069154d5SJulian Elischer 		resp->header.arglen = sizeof(*binary) + bufSize;
2830f8307e12SArchie Cobbs 		break;
2831f8307e12SArchie Cobbs 	    }
2832f8307e12SArchie Cobbs 
28337095e097SPoul-Henning Kamp 	case NGM_TEXT_CONFIG:
28344cf49a43SJulian Elischer 	case NGM_TEXT_STATUS:
28354cf49a43SJulian Elischer 		/*
28364cf49a43SJulian Elischer 		 * This one is tricky as it passes the command down to the
28374cf49a43SJulian Elischer 		 * actual node, even though it is a generic type command.
2838069154d5SJulian Elischer 		 * This means we must assume that the item/msg is already freed
28394cf49a43SJulian Elischer 		 * when control passes back to us.
28404cf49a43SJulian Elischer 		 */
284130400f03SJulian Elischer 		if (here->nd_type->rcvmsg != NULL) {
2842069154d5SJulian Elischer 			NGI_MSG(item) = msg; /* put it back as we found it */
284330400f03SJulian Elischer 			return((*here->nd_type->rcvmsg)(here, item, lasthook));
28444cf49a43SJulian Elischer 		}
28454cf49a43SJulian Elischer 		/* Fall through if rcvmsg not supported */
28464cf49a43SJulian Elischer 	default:
28476b795970SJulian Elischer 		TRAP_ERROR();
28484cf49a43SJulian Elischer 		error = EINVAL;
28494cf49a43SJulian Elischer 	}
2850069154d5SJulian Elischer 	/*
2851069154d5SJulian Elischer 	 * Sometimes a generic message may be statically allocated
2852069154d5SJulian Elischer 	 * to avoid problems with allocating when in tight memeory situations.
2853069154d5SJulian Elischer 	 * Don't free it if it is so.
2854069154d5SJulian Elischer 	 * I break them appart here, because erros may cause a free if the item
2855069154d5SJulian Elischer 	 * in which case we'd be doing it twice.
2856069154d5SJulian Elischer 	 * they are kept together above, to simplify freeing.
2857069154d5SJulian Elischer 	 */
2858069154d5SJulian Elischer out:
2859069154d5SJulian Elischer 	NG_RESPOND_MSG(error, here, item, resp);
2860069154d5SJulian Elischer 	if ( msg && ((msg->header.flags & NGF_STATIC) == 0))
2861069154d5SJulian Elischer 		NG_FREE_MSG(msg);
28624cf49a43SJulian Elischer 	return (error);
28634cf49a43SJulian Elischer }
28644cf49a43SJulian Elischer 
28654cf49a43SJulian Elischer /*
2866a096e45aSArchie Cobbs  * Copy a 'meta'.
2867a096e45aSArchie Cobbs  *
2868a096e45aSArchie Cobbs  * Returns new meta, or NULL if original meta is NULL or ENOMEM.
2869a096e45aSArchie Cobbs  */
2870a096e45aSArchie Cobbs meta_p
2871a096e45aSArchie Cobbs ng_copy_meta(meta_p meta)
2872a096e45aSArchie Cobbs {
2873a096e45aSArchie Cobbs 	meta_p meta2;
2874a096e45aSArchie Cobbs 
2875a096e45aSArchie Cobbs 	if (meta == NULL)
2876a096e45aSArchie Cobbs 		return (NULL);
2877069154d5SJulian Elischer 	MALLOC(meta2, meta_p, meta->used_len, M_NETGRAPH_META, M_NOWAIT);
2878a096e45aSArchie Cobbs 	if (meta2 == NULL)
2879a096e45aSArchie Cobbs 		return (NULL);
2880a096e45aSArchie Cobbs 	meta2->allocated_len = meta->used_len;
2881a096e45aSArchie Cobbs 	bcopy(meta, meta2, meta->used_len);
2882a096e45aSArchie Cobbs 	return (meta2);
2883a096e45aSArchie Cobbs }
2884a096e45aSArchie Cobbs 
28854cf49a43SJulian Elischer /************************************************************************
28864cf49a43SJulian Elischer 			Module routines
28874cf49a43SJulian Elischer ************************************************************************/
28884cf49a43SJulian Elischer 
28894cf49a43SJulian Elischer /*
28904cf49a43SJulian Elischer  * Handle the loading/unloading of a netgraph node type module
28914cf49a43SJulian Elischer  */
28924cf49a43SJulian Elischer int
28934cf49a43SJulian Elischer ng_mod_event(module_t mod, int event, void *data)
28944cf49a43SJulian Elischer {
28954cf49a43SJulian Elischer 	struct ng_type *const type = data;
28964cf49a43SJulian Elischer 	int s, error = 0;
28974cf49a43SJulian Elischer 
28984cf49a43SJulian Elischer 	switch (event) {
28994cf49a43SJulian Elischer 	case MOD_LOAD:
29004cf49a43SJulian Elischer 
29014cf49a43SJulian Elischer 		/* Register new netgraph node type */
29024cf49a43SJulian Elischer 		s = splnet();
29034cf49a43SJulian Elischer 		if ((error = ng_newtype(type)) != 0) {
29044cf49a43SJulian Elischer 			splx(s);
29054cf49a43SJulian Elischer 			break;
29064cf49a43SJulian Elischer 		}
29074cf49a43SJulian Elischer 
29084cf49a43SJulian Elischer 		/* Call type specific code */
29094cf49a43SJulian Elischer 		if (type->mod_event != NULL)
2910069154d5SJulian Elischer 			if ((error = (*type->mod_event)(mod, event, data))) {
2911069154d5SJulian Elischer 				mtx_enter(&ng_typelist_mtx, MTX_DEF);
2912c73b94a2SJulian Elischer 				type->refs--;	/* undo it */
29134cf49a43SJulian Elischer 				LIST_REMOVE(type, types);
2914069154d5SJulian Elischer 				mtx_exit(&ng_typelist_mtx, MTX_DEF);
2915069154d5SJulian Elischer 			}
29164cf49a43SJulian Elischer 		splx(s);
29174cf49a43SJulian Elischer 		break;
29184cf49a43SJulian Elischer 
29194cf49a43SJulian Elischer 	case MOD_UNLOAD:
29204cf49a43SJulian Elischer 		s = splnet();
2921c73b94a2SJulian Elischer 		if (type->refs > 1) {		/* make sure no nodes exist! */
29224cf49a43SJulian Elischer 			error = EBUSY;
2923c73b94a2SJulian Elischer 		} else {
2924c73b94a2SJulian Elischer 			if (type->refs == 0) {
2925c73b94a2SJulian Elischer 				/* failed load, nothing to undo */
2926c73b94a2SJulian Elischer 				splx(s);
2927c73b94a2SJulian Elischer 				break;
2928c73b94a2SJulian Elischer 			}
29294cf49a43SJulian Elischer 			if (type->mod_event != NULL) {	/* check with type */
29304cf49a43SJulian Elischer 				error = (*type->mod_event)(mod, event, data);
29314cf49a43SJulian Elischer 				if (error != 0) {	/* type refuses.. */
29324cf49a43SJulian Elischer 					splx(s);
29334cf49a43SJulian Elischer 					break;
29344cf49a43SJulian Elischer 				}
29354cf49a43SJulian Elischer 			}
2936069154d5SJulian Elischer 			mtx_enter(&ng_typelist_mtx, MTX_DEF);
29374cf49a43SJulian Elischer 			LIST_REMOVE(type, types);
2938069154d5SJulian Elischer 			mtx_exit(&ng_typelist_mtx, MTX_DEF);
29394cf49a43SJulian Elischer 		}
29404cf49a43SJulian Elischer 		splx(s);
29414cf49a43SJulian Elischer 		break;
29424cf49a43SJulian Elischer 
29434cf49a43SJulian Elischer 	default:
29444cf49a43SJulian Elischer 		if (type->mod_event != NULL)
29454cf49a43SJulian Elischer 			error = (*type->mod_event)(mod, event, data);
29464cf49a43SJulian Elischer 		else
29474cf49a43SJulian Elischer 			error = 0;		/* XXX ? */
29484cf49a43SJulian Elischer 		break;
29494cf49a43SJulian Elischer 	}
29504cf49a43SJulian Elischer 	return (error);
29514cf49a43SJulian Elischer }
29524cf49a43SJulian Elischer 
29534cf49a43SJulian Elischer /*
29544cf49a43SJulian Elischer  * Handle loading and unloading for this code.
29554cf49a43SJulian Elischer  * The only thing we need to link into is the NETISR strucure.
29564cf49a43SJulian Elischer  */
29574cf49a43SJulian Elischer static int
29584cf49a43SJulian Elischer ngb_mod_event(module_t mod, int event, void *data)
29594cf49a43SJulian Elischer {
29604cf49a43SJulian Elischer 	int s, error = 0;
29614cf49a43SJulian Elischer 
29624cf49a43SJulian Elischer 	switch (event) {
29634cf49a43SJulian Elischer 	case MOD_LOAD:
29644cf49a43SJulian Elischer 		/* Register line discipline */
2965069154d5SJulian Elischer 		mtx_init(&ng_worklist_mtx, "netgraph worklist mutex", 0);
2966069154d5SJulian Elischer 		mtx_init(&ng_typelist_mtx, "netgraph types mutex", 0);
2967069154d5SJulian Elischer 		mtx_init(&ng_nodelist_mtx, "netgraph nodelist mutex", 0);
2968069154d5SJulian Elischer 		mtx_init(&ng_idhash_mtx, "netgraph idhash mutex", 0);
2969069154d5SJulian Elischer 		mtx_init(&ngq_mtx, "netgraph netisr mutex", 0);
29704cf49a43SJulian Elischer 		s = splimp();
29714cf49a43SJulian Elischer 		error = register_netisr(NETISR_NETGRAPH, ngintr);
29724cf49a43SJulian Elischer 		splx(s);
29734cf49a43SJulian Elischer 		break;
29744cf49a43SJulian Elischer 	case MOD_UNLOAD:
29754cf49a43SJulian Elischer 		/* You cant unload it because an interface may be using it.  */
29764cf49a43SJulian Elischer 		error = EBUSY;
29774cf49a43SJulian Elischer 		break;
29784cf49a43SJulian Elischer 	default:
29794cf49a43SJulian Elischer 		error = EOPNOTSUPP;
29804cf49a43SJulian Elischer 		break;
29814cf49a43SJulian Elischer 	}
29824cf49a43SJulian Elischer 	return (error);
29834cf49a43SJulian Elischer }
29844cf49a43SJulian Elischer 
29854cf49a43SJulian Elischer static moduledata_t netgraph_mod = {
29864cf49a43SJulian Elischer 	"netgraph",
29874cf49a43SJulian Elischer 	ngb_mod_event,
29884cf49a43SJulian Elischer 	(NULL)
29894cf49a43SJulian Elischer };
29904cf49a43SJulian Elischer DECLARE_MODULE(netgraph, netgraph_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
29914cf49a43SJulian Elischer 
29924cf49a43SJulian Elischer /************************************************************************
2993069154d5SJulian Elischer 			Queue element get/free routines
29944cf49a43SJulian Elischer ************************************************************************/
29954cf49a43SJulian Elischer 
29964cf49a43SJulian Elischer 
2997069154d5SJulian Elischer static int			allocated;	/* number of items malloc'd */
2998069154d5SJulian Elischer static int			maxalloc = 128;	/* limit the damage of a leak */
2999069154d5SJulian Elischer static const int		ngqfreemax = 64;/* cache at most this many */
3000069154d5SJulian Elischer static const int		ngqfreelow = 4; /* try malloc if free < this */
3001069154d5SJulian Elischer static volatile int		ngqfreesize;	/* number of cached entries */
300230400f03SJulian Elischer #ifdef	NETGRAPH_DEBUG
3003069154d5SJulian Elischer static TAILQ_HEAD(, ng_item) ng_itemlist = TAILQ_HEAD_INITIALIZER(ng_itemlist);
3004069154d5SJulian Elischer #endif
30054cf49a43SJulian Elischer /*
30064cf49a43SJulian Elischer  * Get a queue entry
3007069154d5SJulian Elischer  * This is usually called when a packet first enters netgraph.
3008069154d5SJulian Elischer  * By definition, this is usually from an interrupt, or from a user.
3009069154d5SJulian Elischer  * Users are not so important, but try be quick for the times that it's
3010069154d5SJulian Elischer  * an interrupt. Use atomic operations to cope with collisions
3011069154d5SJulian Elischer  * with interrupts and other processors. Assumes MALLOC is SMP safe.
3012069154d5SJulian Elischer  * XXX If reserve is low, we should try to get 2 from malloc as this
3013069154d5SJulian Elischer  * would indicate it often fails.
30144cf49a43SJulian Elischer  */
3015069154d5SJulian Elischer static item_p
30164cf49a43SJulian Elischer ng_getqblk(void)
30174cf49a43SJulian Elischer {
3018069154d5SJulian Elischer 	item_p item = NULL;
30194cf49a43SJulian Elischer 
3020069154d5SJulian Elischer 	/*
3021069154d5SJulian Elischer 	 * Try get a cached queue block, or else allocate a new one
3022069154d5SJulian Elischer 	 * If we are less than our reserve, try malloc. If malloc
3023069154d5SJulian Elischer 	 * fails, then that's what the reserve is for...
3024069154d5SJulian Elischer 	 * Don't completely trust ngqfreesize, as it is subject
3025069154d5SJulian Elischer 	 * to races.. (it'll eventually catch up but may be out by one or two
3026069154d5SJulian Elischer 	 * for brief moments(under SMP or interrupts).
3027069154d5SJulian Elischer 	 * ngqfree is the final arbiter. We have our little reserve
3028069154d5SJulian Elischer 	 * because we use M_NOWAIT for malloc. This just helps us
3029069154d5SJulian Elischer 	 * avoid dropping packets while not increasing the time
3030069154d5SJulian Elischer 	 * we take to service the interrupt (on average) (we hope).
3031069154d5SJulian Elischer 	 */
3032069154d5SJulian Elischer 	for (;;) {
3033069154d5SJulian Elischer 		if ((ngqfreesize < ngqfreelow) || (ngqfree == NULL)) {
3034069154d5SJulian Elischer 			if (allocated < maxalloc) {  /* don't leak forever */
3035069154d5SJulian Elischer 				MALLOC(item, item_p ,
3036069154d5SJulian Elischer 				    sizeof(*item), M_NETGRAPH_ITEM,
3037069154d5SJulian Elischer 				    (M_NOWAIT | M_ZERO));
3038069154d5SJulian Elischer 				if (item) {
303930400f03SJulian Elischer #ifdef	NETGRAPH_DEBUG
3040069154d5SJulian Elischer 					TAILQ_INSERT_TAIL(&ng_itemlist,
3041069154d5SJulian Elischer 								item, all);
304230400f03SJulian Elischer #endif	/* NETGRAPH_DEBUG */
3043069154d5SJulian Elischer 					atomic_add_int(&allocated, 1);
3044069154d5SJulian Elischer 					break;
30454cf49a43SJulian Elischer 				}
3046069154d5SJulian Elischer 			}
3047069154d5SJulian Elischer 		}
3048069154d5SJulian Elischer 
3049069154d5SJulian Elischer 		/*
3050069154d5SJulian Elischer 		 * We didn't or couldn't malloc.
3051069154d5SJulian Elischer 		 * try get one from our cache.
3052069154d5SJulian Elischer 		 * item must be NULL to get here.
3053069154d5SJulian Elischer 		 */
3054069154d5SJulian Elischer 		if ((item = ngqfree) != NULL) {
3055069154d5SJulian Elischer 			/*
3056069154d5SJulian Elischer 			 * Atomically try grab the first item
3057069154d5SJulian Elischer 			 * and put it's successor in its place.
3058069154d5SJulian Elischer 			 * If we fail, just try again.. someone else
3059069154d5SJulian Elischer 			 * beat us to this one or freed one.
3060069154d5SJulian Elischer 			 * Don't worry about races with ngqfreesize.
3061069154d5SJulian Elischer 			 * Close enough is good enough..
3062069154d5SJulian Elischer 			 */
3063069154d5SJulian Elischer 			if (atomic_cmpset_ptr(&ngqfree, item, item->el_next)) {
3064069154d5SJulian Elischer 				atomic_subtract_int(&ngqfreesize, 1);
3065069154d5SJulian Elischer 				break;
3066069154d5SJulian Elischer 			}
3067069154d5SJulian Elischer 			item = NULL;
30684cf49a43SJulian Elischer 		} else {
3069069154d5SJulian Elischer 			/* We really ran out */
3070069154d5SJulian Elischer 			break;
30714cf49a43SJulian Elischer 		}
3072069154d5SJulian Elischer 	}
3073069154d5SJulian Elischer 	item->el_flags &= ~NGQF_FREE;
3074069154d5SJulian Elischer 	return (item);
30754cf49a43SJulian Elischer }
30764cf49a43SJulian Elischer 
30774cf49a43SJulian Elischer /*
30784cf49a43SJulian Elischer  * Release a queue entry
30794cf49a43SJulian Elischer  */
3080069154d5SJulian Elischer void
3081069154d5SJulian Elischer ng_free_item(item_p item)
3082069154d5SJulian Elischer {
3083069154d5SJulian Elischer 
3084069154d5SJulian Elischer 	/*
3085069154d5SJulian Elischer 	 * The item may hold resources on it's own. We need to free
3086069154d5SJulian Elischer 	 * these before we can free the item. What they are depends upon
3087069154d5SJulian Elischer 	 * what kind of item it is. it is important that nodes zero
3088069154d5SJulian Elischer 	 * out pointers to resources that they remove from the item
3089069154d5SJulian Elischer 	 * or we release them again here.
3090069154d5SJulian Elischer 	 */
3091069154d5SJulian Elischer 	if (item->el_flags & NGQF_FREE) {
3092069154d5SJulian Elischer 		panic(" Freeing free queue item");
3093069154d5SJulian Elischer 	}
30946b795970SJulian Elischer 	switch (item->el_flags & NGQF_TYPE) {
3095069154d5SJulian Elischer 	case NGQF_DATA:
3096069154d5SJulian Elischer 		/* If we have an mbuf and metadata still attached.. */
3097069154d5SJulian Elischer 		NG_FREE_M(_NGI_M(item));
3098069154d5SJulian Elischer 		NG_FREE_META(_NGI_META(item));
3099069154d5SJulian Elischer 		break;
3100069154d5SJulian Elischer 	case NGQF_MESG:
3101069154d5SJulian Elischer 		_NGI_RETADDR(item) = NULL;
3102069154d5SJulian Elischer 		NG_FREE_MSG(_NGI_MSG(item));
3103069154d5SJulian Elischer 		break;
31046b795970SJulian Elischer 	case NGQF_FN:
31056b795970SJulian Elischer 		/* nothing to free really, */
31066b795970SJulian Elischer 		_NGI_FN(item) = NULL;
31076b795970SJulian Elischer 		_NGI_ARG1(item) = NULL;
31086b795970SJulian Elischer 		_NGI_ARG2(item) = 0;
31096b795970SJulian Elischer 	case NGQF_UNDEF:
3110069154d5SJulian Elischer 	}
3111069154d5SJulian Elischer 		/* If we still have a node or hook referenced... */
3112069154d5SJulian Elischer 	if (item->el_dest) {
311330400f03SJulian Elischer 		NG_NODE_UNREF(item->el_dest);
3114069154d5SJulian Elischer 		item->el_dest = NULL;
3115069154d5SJulian Elischer 	}
3116069154d5SJulian Elischer 	if (item->el_hook) {
311730400f03SJulian Elischer 		NG_HOOK_UNREF(item->el_hook);
3118069154d5SJulian Elischer 		item->el_hook = NULL;
3119069154d5SJulian Elischer 	}
3120069154d5SJulian Elischer 	item->el_flags |= NGQF_FREE;
3121069154d5SJulian Elischer 
3122069154d5SJulian Elischer 	/*
3123069154d5SJulian Elischer 	 * We have freed any resources held by the item.
3124069154d5SJulian Elischer 	 * now we can free the item itself.
3125069154d5SJulian Elischer 	 */
3126069154d5SJulian Elischer 	if (ngqfreesize < ngqfreemax) { /* don't worry about races */
3127069154d5SJulian Elischer 		for (;;) {
3128069154d5SJulian Elischer 			item->el_next = ngqfree;
3129069154d5SJulian Elischer 			if (atomic_cmpset_ptr(&ngqfree, item->el_next, item)) {
3130069154d5SJulian Elischer 				break;
3131069154d5SJulian Elischer 			}
3132069154d5SJulian Elischer 		}
3133069154d5SJulian Elischer 		atomic_add_int(&ngqfreesize, 1);
3134069154d5SJulian Elischer 	} else {
3135069154d5SJulian Elischer 		/* This is the only place that should use this Macro */
313630400f03SJulian Elischer #ifdef	NETGRAPH_DEBUG
3137069154d5SJulian Elischer 		TAILQ_REMOVE(&ng_itemlist, item, all);
313830400f03SJulian Elischer #endif	/* NETGRAPH_DEBUG */
3139069154d5SJulian Elischer 		NG_FREE_ITEM_REAL(item);
3140069154d5SJulian Elischer 		atomic_subtract_int(&allocated, 1);
3141069154d5SJulian Elischer 	}
3142069154d5SJulian Elischer }
3143069154d5SJulian Elischer 
314430400f03SJulian Elischer #ifdef	NETGRAPH_DEBUG
314530400f03SJulian Elischer void
314630400f03SJulian Elischer dumphook (hook_p hook, char *file, int line)
314730400f03SJulian Elischer {
314830400f03SJulian Elischer 	printf("hook: name %s, %d refs, Last touched:\n",
314930400f03SJulian Elischer 		_NG_HOOK_NAME(hook), hook->hk_refs);
315030400f03SJulian Elischer 	printf("	Last active @ %s, line %d\n",
315130400f03SJulian Elischer 		hook->lastfile, hook->lastline);
315230400f03SJulian Elischer 	if (line) {
315330400f03SJulian Elischer 		printf(" problem discovered at file %s, line %d\n", file, line);
315430400f03SJulian Elischer 	}
315530400f03SJulian Elischer }
315630400f03SJulian Elischer 
315730400f03SJulian Elischer void
315830400f03SJulian Elischer dumpnode(node_p node, char *file, int line)
315930400f03SJulian Elischer {
316030400f03SJulian Elischer 	printf("node: ID [%x]: type '%s', %d hooks, flags 0x%x, %d refs, %s:\n",
31616b795970SJulian Elischer 		_NG_NODE_ID(node), node->nd_type->name,
316230400f03SJulian Elischer 		node->nd_numhooks, node->nd_flags,
316330400f03SJulian Elischer 		node->nd_refs, node->nd_name);
316430400f03SJulian Elischer 	printf("	Last active @ %s, line %d\n",
316530400f03SJulian Elischer 		node->lastfile, node->lastline);
316630400f03SJulian Elischer 	if (line) {
316730400f03SJulian Elischer 		printf(" problem discovered at file %s, line %d\n", file, line);
316830400f03SJulian Elischer 	}
316930400f03SJulian Elischer }
317030400f03SJulian Elischer 
3171069154d5SJulian Elischer void
3172069154d5SJulian Elischer dumpitem(item_p item, char *file, int line)
3173069154d5SJulian Elischer {
3174069154d5SJulian Elischer 	if (item->el_flags & NGQF_FREE) {
3175069154d5SJulian Elischer 		printf(" Free item, freed at %s, line %d\n",
3176069154d5SJulian Elischer 			item->lastfile, item->lastline);
3177069154d5SJulian Elischer 	} else {
3178069154d5SJulian Elischer 		printf(" ACTIVE item, last used at %s, line %d",
3179069154d5SJulian Elischer 			item->lastfile, item->lastline);
31806b795970SJulian Elischer 		switch(item->el_flags & NGQF_TYPE) {
31816b795970SJulian Elischer 		case NGQF_DATA:
3182069154d5SJulian Elischer 			printf(" - [data]\n");
31836b795970SJulian Elischer 			break;
31846b795970SJulian Elischer 		case NGQF_MESG:
31856b795970SJulian Elischer 			printf(" - retaddr[%d]:\n", _NGI_RETADDR(item));
31866b795970SJulian Elischer 			break;
31876b795970SJulian Elischer 		case NGQF_FN:
31886b795970SJulian Elischer 			printf(" - fn@%p (%p, %p, %p, %d (%x))\n",
31896b795970SJulian Elischer 				item->body.fn.fn_fn,
31906b795970SJulian Elischer 				item->el_dest,
31916b795970SJulian Elischer 				item->el_hook,
31926b795970SJulian Elischer 				item->body.fn.fn_arg1,
31936b795970SJulian Elischer 				item->body.fn.fn_arg2,
31946b795970SJulian Elischer 				item->body.fn.fn_arg2);
31956b795970SJulian Elischer 			break;
31966b795970SJulian Elischer 		case NGQF_UNDEF:
31976b795970SJulian Elischer 			printf(" - UNDEFINED!\n");
3198069154d5SJulian Elischer 		}
3199069154d5SJulian Elischer 	}
320030400f03SJulian Elischer 	if (line) {
3201069154d5SJulian Elischer 		printf(" problem discovered at file %s, line %d\n", file, line);
320230400f03SJulian Elischer 		if (item->el_dest) {
320330400f03SJulian Elischer 			printf("node %p ([%x])\n",
3204069154d5SJulian Elischer 				item->el_dest, ng_node2ID(item->el_dest));
3205069154d5SJulian Elischer 		}
320630400f03SJulian Elischer 	}
320730400f03SJulian Elischer }
320830400f03SJulian Elischer 
320930400f03SJulian Elischer static void
321030400f03SJulian Elischer ng_dumpitems(void)
321130400f03SJulian Elischer {
321230400f03SJulian Elischer 	item_p item;
321330400f03SJulian Elischer 	int i = 1;
321430400f03SJulian Elischer 	TAILQ_FOREACH(item, &ng_itemlist, all) {
321530400f03SJulian Elischer 		printf("[%d] ", i++);
321630400f03SJulian Elischer 		dumpitem(item, NULL, 0);
321730400f03SJulian Elischer 	}
321830400f03SJulian Elischer }
321930400f03SJulian Elischer 
322030400f03SJulian Elischer static void
322130400f03SJulian Elischer ng_dumpnodes(void)
322230400f03SJulian Elischer {
322330400f03SJulian Elischer 	node_p node;
322430400f03SJulian Elischer 	int i = 1;
322530400f03SJulian Elischer 	SLIST_FOREACH(node, &ng_allnodes, nd_all) {
322630400f03SJulian Elischer 		printf("[%d] ", i++);
322730400f03SJulian Elischer 		dumpnode(node, NULL, 0);
322830400f03SJulian Elischer 	}
322930400f03SJulian Elischer }
323030400f03SJulian Elischer 
323130400f03SJulian Elischer static void
323230400f03SJulian Elischer ng_dumphooks(void)
323330400f03SJulian Elischer {
323430400f03SJulian Elischer 	hook_p hook;
323530400f03SJulian Elischer 	int i = 1;
323630400f03SJulian Elischer 	SLIST_FOREACH(hook, &ng_allhooks, hk_all) {
323730400f03SJulian Elischer 		printf("[%d] ", i++);
323830400f03SJulian Elischer 		dumphook(hook, NULL, 0);
323930400f03SJulian Elischer 	}
324030400f03SJulian Elischer }
3241069154d5SJulian Elischer 
3242069154d5SJulian Elischer static int
3243069154d5SJulian Elischer sysctl_debug_ng_dump_items(SYSCTL_HANDLER_ARGS)
3244069154d5SJulian Elischer {
3245069154d5SJulian Elischer 	int error;
3246069154d5SJulian Elischer 	int val;
3247069154d5SJulian Elischer 	int i;
3248069154d5SJulian Elischer 
3249069154d5SJulian Elischer 	val = allocated;
3250069154d5SJulian Elischer 	i = 1;
3251069154d5SJulian Elischer 	error = sysctl_handle_int(oidp, &val, sizeof(int), req);
32526b795970SJulian Elischer 	if (error != 0 || req->newptr == NULL)
32536b795970SJulian Elischer 		return (error);
32546b795970SJulian Elischer 	if (val == 42) {
325530400f03SJulian Elischer 		ng_dumpitems();
325630400f03SJulian Elischer 		ng_dumpnodes();
325730400f03SJulian Elischer 		ng_dumphooks();
3258069154d5SJulian Elischer 	}
32596b795970SJulian Elischer 	return (0);
3260069154d5SJulian Elischer }
3261069154d5SJulian Elischer 
32626b795970SJulian Elischer SYSCTL_PROC(_debug, OID_AUTO, ng_dump_items, CTLTYPE_INT | CTLFLAG_RW,
32636b795970SJulian Elischer     0, sizeof(int), sysctl_debug_ng_dump_items, "I", "Number of allocated items");
326430400f03SJulian Elischer #endif	/* NETGRAPH_DEBUG */
3265069154d5SJulian Elischer 
3266069154d5SJulian Elischer 
3267069154d5SJulian Elischer /***********************************************************************
3268069154d5SJulian Elischer * Worklist routines
3269069154d5SJulian Elischer **********************************************************************/
3270069154d5SJulian Elischer /* NETISR thread enters here */
3271069154d5SJulian Elischer /*
3272069154d5SJulian Elischer  * Pick a node off the list of nodes with work,
3273069154d5SJulian Elischer  * try get an item to process off it.
3274069154d5SJulian Elischer  * If there are no more, remove the node from the list.
3275069154d5SJulian Elischer  */
3276069154d5SJulian Elischer static void
3277069154d5SJulian Elischer ngintr(void)
3278069154d5SJulian Elischer {
3279069154d5SJulian Elischer 	item_p item;
3280069154d5SJulian Elischer 	node_p  node = NULL;
3281069154d5SJulian Elischer 
3282069154d5SJulian Elischer 	for (;;) {
3283069154d5SJulian Elischer 		mtx_enter(&ng_worklist_mtx, MTX_SPIN);
3284069154d5SJulian Elischer 		node = TAILQ_FIRST(&ng_worklist);
3285069154d5SJulian Elischer 		if (!node) {
3286069154d5SJulian Elischer 			mtx_exit(&ng_worklist_mtx, MTX_SPIN);
3287069154d5SJulian Elischer 			break;
3288069154d5SJulian Elischer 		}
328930400f03SJulian Elischer 		TAILQ_REMOVE(&ng_worklist, node, nd_work);
3290069154d5SJulian Elischer 		mtx_exit(&ng_worklist_mtx, MTX_SPIN);
3291069154d5SJulian Elischer 		/*
3292069154d5SJulian Elischer 		 * We have the node. We also take over the reference
3293069154d5SJulian Elischer 		 * that the list had on it.
3294069154d5SJulian Elischer 		 * Now process as much as you can, until it won't
3295069154d5SJulian Elischer 		 * let you have another item off the queue.
3296069154d5SJulian Elischer 		 * All this time, keep the reference
3297069154d5SJulian Elischer 		 * that lets us be sure that the node still exists.
3298069154d5SJulian Elischer 		 * Let the reference go at the last minute.
3299069154d5SJulian Elischer 		 */
3300069154d5SJulian Elischer 		for (;;) {
330130400f03SJulian Elischer 			mtx_enter(&node->nd_input_queue.q_mtx, MTX_SPIN);
330230400f03SJulian Elischer 			item = ng_dequeue(&node->nd_input_queue);
3303069154d5SJulian Elischer 			if (item == NULL) {
3304069154d5SJulian Elischer 				/*
3305069154d5SJulian Elischer 				 * Say we are on the queue as long as
3306069154d5SJulian Elischer 				 * we are processing it here.
3307069154d5SJulian Elischer 				 * it probably wouldn't come here while we
3308069154d5SJulian Elischer 				 * are processing anyhow.
3309069154d5SJulian Elischer 				 */
331030400f03SJulian Elischer 				node->nd_flags &= ~NG_WORKQ;
331130400f03SJulian Elischer 				mtx_exit(&node->nd_input_queue.q_mtx, MTX_SPIN);
331230400f03SJulian Elischer 				NG_NODE_UNREF(node);
3313069154d5SJulian Elischer 				break; /* go look for another node */
3314069154d5SJulian Elischer 			} else {
331530400f03SJulian Elischer 				mtx_exit(&node->nd_input_queue.q_mtx, MTX_SPIN);
331630400f03SJulian Elischer #ifdef	NETGRAPH_DEBUG
3317069154d5SJulian Elischer         _ngi_check(item, __FILE__, __LINE__);
3318069154d5SJulian Elischer #endif
3319069154d5SJulian Elischer 				ng_apply_item(node, item);
3320069154d5SJulian Elischer 			}
3321069154d5SJulian Elischer 		}
3322069154d5SJulian Elischer 	}
3323069154d5SJulian Elischer }
3324069154d5SJulian Elischer 
3325069154d5SJulian Elischer static void
3326069154d5SJulian Elischer ng_worklist_remove(node_p node)
3327069154d5SJulian Elischer {
3328069154d5SJulian Elischer 	mtx_enter(&ng_worklist_mtx, MTX_SPIN);
332930400f03SJulian Elischer 	if (node->nd_flags & NG_WORKQ) {
333030400f03SJulian Elischer 		TAILQ_REMOVE(&ng_worklist, node, nd_work);
333130400f03SJulian Elischer 		NG_NODE_UNREF(node);
3332069154d5SJulian Elischer 	}
333330400f03SJulian Elischer 	node->nd_flags &= ~NG_WORKQ;
3334069154d5SJulian Elischer 	mtx_exit(&ng_worklist_mtx, MTX_SPIN);
3335069154d5SJulian Elischer }
3336069154d5SJulian Elischer 
3337069154d5SJulian Elischer static void
3338069154d5SJulian Elischer ng_setisr(node_p node)
3339069154d5SJulian Elischer {
3340069154d5SJulian Elischer 	mtx_enter(&ng_worklist_mtx, MTX_SPIN);
334130400f03SJulian Elischer 	if ((node->nd_flags & NG_WORKQ) == 0) {
3342069154d5SJulian Elischer 		/*
3343069154d5SJulian Elischer 		 * If we are not already on the work queue,
3344069154d5SJulian Elischer 		 * then put us on.
3345069154d5SJulian Elischer 		 */
334630400f03SJulian Elischer 		node->nd_flags |= NG_WORKQ;
334730400f03SJulian Elischer 		TAILQ_INSERT_TAIL(&ng_worklist, node, nd_work);
334830400f03SJulian Elischer 		NG_NODE_REF(node);
3349069154d5SJulian Elischer 	}
3350069154d5SJulian Elischer 	mtx_exit(&ng_worklist_mtx, MTX_SPIN);
3351069154d5SJulian Elischer 	schednetisr(NETISR_NETGRAPH);
3352069154d5SJulian Elischer }
3353069154d5SJulian Elischer 
3354069154d5SJulian Elischer 
3355069154d5SJulian Elischer /***********************************************************************
3356069154d5SJulian Elischer * Externally useable functions to set up a queue item ready for sending
3357069154d5SJulian Elischer ***********************************************************************/
3358069154d5SJulian Elischer 
335930400f03SJulian Elischer #ifdef	NETGRAPH_DEBUG
336030400f03SJulian Elischer #define	ITEM_DEBUG_CHECKS						\
33614cf49a43SJulian Elischer 	do {								\
3362069154d5SJulian Elischer 		if (item->el_dest ) {					\
3363069154d5SJulian Elischer 			printf("item already has node");		\
3364069154d5SJulian Elischer 			Debugger("has node");				\
336530400f03SJulian Elischer 			NG_NODE_UNREF(item->el_dest);			\
3366069154d5SJulian Elischer 			item->el_dest = NULL;				\
3367069154d5SJulian Elischer 		}							\
3368069154d5SJulian Elischer 		if (item->el_hook ) {					\
3369069154d5SJulian Elischer 			printf("item already has hook");		\
3370069154d5SJulian Elischer 			Debugger("has hook");				\
337130400f03SJulian Elischer 			NG_HOOK_UNREF(item->el_hook);			\
3372069154d5SJulian Elischer 			item->el_hook = NULL;				\
3373069154d5SJulian Elischer 		}							\
3374069154d5SJulian Elischer 	} while (0)
3375069154d5SJulian Elischer #else
337630400f03SJulian Elischer #define ITEM_DEBUG_CHECKS
3377069154d5SJulian Elischer #endif
3378069154d5SJulian Elischer 
3379069154d5SJulian Elischer /*
3380069154d5SJulian Elischer  * Put elements into the item.
3381069154d5SJulian Elischer  * Hook and node references will be removed when the item is dequeued.
3382069154d5SJulian Elischer  * (or equivalent)
3383069154d5SJulian Elischer  * (XXX) Unsafe because no reference held by peer on remote node.
3384069154d5SJulian Elischer  * remote node might go away in this timescale.
3385069154d5SJulian Elischer  * We know the hooks can't go away because that would require getting
3386069154d5SJulian Elischer  * a writer item on both nodes and we must have at least a  reader
3387069154d5SJulian Elischer  * here to eb able to do this.
3388069154d5SJulian Elischer  * Note that the hook loaded is the REMOTE hook.
3389069154d5SJulian Elischer  *
3390069154d5SJulian Elischer  * This is possibly in the critical path for new data.
3391069154d5SJulian Elischer  */
3392069154d5SJulian Elischer item_p
3393069154d5SJulian Elischer ng_package_data(struct mbuf *m, meta_p meta)
3394069154d5SJulian Elischer {
3395069154d5SJulian Elischer 	item_p item;
3396069154d5SJulian Elischer 
3397069154d5SJulian Elischer 	if ((item = ng_getqblk()) == NULL) {
3398069154d5SJulian Elischer 		NG_FREE_M(m);
3399069154d5SJulian Elischer 		NG_FREE_META(meta);
3400069154d5SJulian Elischer 		return (NULL);
3401069154d5SJulian Elischer 	}
340230400f03SJulian Elischer 	ITEM_DEBUG_CHECKS;
3403069154d5SJulian Elischer 	item->el_flags = NGQF_DATA;
3404069154d5SJulian Elischer 	item->el_next = NULL;
3405069154d5SJulian Elischer 	NGI_M(item) = m;
3406069154d5SJulian Elischer 	NGI_META(item) = meta;
3407069154d5SJulian Elischer 	return (item);
3408069154d5SJulian Elischer }
3409069154d5SJulian Elischer 
3410069154d5SJulian Elischer /*
3411069154d5SJulian Elischer  * Allocate a queue item and put items into it..
3412069154d5SJulian Elischer  * Evaluate the address as this will be needed to queue it and
3413069154d5SJulian Elischer  * to work out what some of the fields should be.
3414069154d5SJulian Elischer  * Hook and node references will be removed when the item is dequeued.
3415069154d5SJulian Elischer  * (or equivalent)
3416069154d5SJulian Elischer  */
3417069154d5SJulian Elischer item_p
3418069154d5SJulian Elischer ng_package_msg(struct ng_mesg *msg)
3419069154d5SJulian Elischer {
3420069154d5SJulian Elischer 	item_p item;
3421069154d5SJulian Elischer 
3422069154d5SJulian Elischer 	if ((item = ng_getqblk()) == NULL) {
3423069154d5SJulian Elischer 		if ((msg->header.flags & NGF_STATIC) == 0) {
3424069154d5SJulian Elischer 			NG_FREE_MSG(msg);
3425069154d5SJulian Elischer 		}
3426069154d5SJulian Elischer 		return (NULL);
3427069154d5SJulian Elischer 	}
342830400f03SJulian Elischer 	ITEM_DEBUG_CHECKS;
3429069154d5SJulian Elischer 	item->el_flags = NGQF_MESG;
3430069154d5SJulian Elischer 	item->el_next = NULL;
3431069154d5SJulian Elischer 	/*
3432069154d5SJulian Elischer 	 * Set the current lasthook into the queue item
3433069154d5SJulian Elischer 	 */
3434069154d5SJulian Elischer 	NGI_MSG(item) = msg;
3435069154d5SJulian Elischer 	NGI_RETADDR(item) = NULL;
3436069154d5SJulian Elischer 	return (item);
3437069154d5SJulian Elischer }
3438069154d5SJulian Elischer 
3439069154d5SJulian Elischer 
3440069154d5SJulian Elischer 
3441069154d5SJulian Elischer #define SET_RETADDR							\
34426b795970SJulian Elischer 	do {	/* Data or fn items don't have retaddrs */		\
34436b795970SJulian Elischer 		if ((item->el_flags & NGQF_TYPE) == NGQF_MESG) {	\
3444069154d5SJulian Elischer 			if (retaddr) {					\
3445069154d5SJulian Elischer 				NGI_RETADDR(item) = retaddr;		\
34464cf49a43SJulian Elischer 			} else {					\
3447069154d5SJulian Elischer 				/*					\
3448069154d5SJulian Elischer 				 * The old return address should be ok.	\
3449069154d5SJulian Elischer 				 * If there isn't one, use the address	\
3450069154d5SJulian Elischer 				 * here.				\
3451069154d5SJulian Elischer 				 */					\
3452069154d5SJulian Elischer 				if (NGI_RETADDR(item) == 0) {		\
3453069154d5SJulian Elischer 					NGI_RETADDR(item)		\
3454069154d5SJulian Elischer 						= ng_node2ID(here);	\
3455069154d5SJulian Elischer 				}					\
3456069154d5SJulian Elischer 			}						\
34574cf49a43SJulian Elischer 		}							\
34584cf49a43SJulian Elischer 	} while (0)
34594cf49a43SJulian Elischer 
34604cf49a43SJulian Elischer int
3461069154d5SJulian Elischer ng_address_hook(node_p here, item_p item, hook_p hook, ng_ID_t retaddr)
34624cf49a43SJulian Elischer {
346330400f03SJulian Elischer 	ITEM_DEBUG_CHECKS;
3464069154d5SJulian Elischer 	/*
3465069154d5SJulian Elischer 	 * Quick sanity check..
346630400f03SJulian Elischer 	 * Since a hook holds a reference on it's node, once we know
346730400f03SJulian Elischer 	 * that the peer is still connected (even if invalid,) we know
346830400f03SJulian Elischer 	 * that the peer node is present, though maybe invalid.
3469069154d5SJulian Elischer 	 */
3470069154d5SJulian Elischer 	if ((hook == NULL)
347130400f03SJulian Elischer 	|| NG_HOOK_NOT_VALID(hook)
347230400f03SJulian Elischer 	|| (NG_HOOK_PEER(hook) == NULL)
347330400f03SJulian Elischer 	|| NG_HOOK_NOT_VALID(NG_HOOK_PEER(hook))
347430400f03SJulian Elischer 	|| NG_NODE_NOT_VALID(NG_PEER_NODE(hook))) {
3475069154d5SJulian Elischer 		NG_FREE_ITEM(item);
34766b795970SJulian Elischer 		TRAP_ERROR();
3477069154d5SJulian Elischer 		return (EINVAL);
34784cf49a43SJulian Elischer 	}
34794cf49a43SJulian Elischer 
34804cf49a43SJulian Elischer 	/*
3481069154d5SJulian Elischer 	 * Transfer our interest to the other (peer) end.
34824cf49a43SJulian Elischer 	 */
348330400f03SJulian Elischer 	item->el_hook = NG_HOOK_PEER(hook);
348430400f03SJulian Elischer 	NG_HOOK_REF(item->el_hook); /* Don't let it go while on the queue */
348530400f03SJulian Elischer 	item->el_dest = NG_PEER_NODE(hook);
348630400f03SJulian Elischer 	NG_NODE_REF(item->el_dest); /* Nor this */
3487069154d5SJulian Elischer 	SET_RETADDR;
3488069154d5SJulian Elischer 	return (0);
3489069154d5SJulian Elischer }
3490069154d5SJulian Elischer 
34914cf49a43SJulian Elischer int
3492069154d5SJulian Elischer ng_address_path(node_p here, item_p item, char *address, ng_ID_t retaddr)
34934cf49a43SJulian Elischer {
34944cf49a43SJulian Elischer 	node_p  dest = NULL;
3495069154d5SJulian Elischer 	hook_p	hook = NULL;
34964cf49a43SJulian Elischer 	int     error;
3497069154d5SJulian Elischer 
349830400f03SJulian Elischer 	ITEM_DEBUG_CHECKS;
3499069154d5SJulian Elischer 	/*
3500069154d5SJulian Elischer 	 * Note that ng_path2noderef increments the reference count
3501069154d5SJulian Elischer 	 * on the node for us if it finds one. So we don't have to.
3502069154d5SJulian Elischer 	 */
3503069154d5SJulian Elischer 	error = ng_path2noderef(here, address, &dest, &hook);
3504069154d5SJulian Elischer 	if (error) {
3505069154d5SJulian Elischer 		NG_FREE_ITEM(item);
350630400f03SJulian Elischer 		return (error);
3507069154d5SJulian Elischer 	}
3508069154d5SJulian Elischer 	item->el_dest = dest;
3509069154d5SJulian Elischer 	if (( item->el_hook = hook))
351030400f03SJulian Elischer 		NG_HOOK_REF(hook);	/* don't let it go while on the queue */
3511069154d5SJulian Elischer 	SET_RETADDR;
3512069154d5SJulian Elischer 	return (0);
3513069154d5SJulian Elischer }
3514069154d5SJulian Elischer 
3515069154d5SJulian Elischer int
3516069154d5SJulian Elischer ng_address_ID(node_p here, item_p item, ng_ID_t ID, ng_ID_t retaddr)
3517069154d5SJulian Elischer {
3518069154d5SJulian Elischer 	node_p dest;
3519069154d5SJulian Elischer 
352030400f03SJulian Elischer 	ITEM_DEBUG_CHECKS;
3521069154d5SJulian Elischer 	/*
3522069154d5SJulian Elischer 	 * Find the target node.
3523069154d5SJulian Elischer 	 */
3524069154d5SJulian Elischer 	dest = ng_ID2noderef(ID); /* GETS REFERENCE! */
3525069154d5SJulian Elischer 	if (dest == NULL) {
3526069154d5SJulian Elischer 		NG_FREE_ITEM(item);
35276b795970SJulian Elischer 		TRAP_ERROR();
3528069154d5SJulian Elischer 		return(EINVAL);
3529069154d5SJulian Elischer 	}
3530069154d5SJulian Elischer 	/* Fill out the contents */
3531069154d5SJulian Elischer 	item->el_flags = NGQF_MESG;
3532069154d5SJulian Elischer 	item->el_next = NULL;
3533069154d5SJulian Elischer 	item->el_dest = dest;
3534069154d5SJulian Elischer 	item->el_hook = NULL;
3535069154d5SJulian Elischer 	SET_RETADDR;
3536069154d5SJulian Elischer 	return (0);
3537069154d5SJulian Elischer }
3538069154d5SJulian Elischer 
3539069154d5SJulian Elischer /*
3540069154d5SJulian Elischer  * special case to send a message to self (e.g. destroy node)
3541069154d5SJulian Elischer  * Possibly indicate an arrival hook too.
3542069154d5SJulian Elischer  * Useful for removing that hook :-)
3543069154d5SJulian Elischer  */
3544069154d5SJulian Elischer item_p
3545069154d5SJulian Elischer ng_package_msg_self(node_p here, hook_p hook, struct ng_mesg *msg)
3546069154d5SJulian Elischer {
3547069154d5SJulian Elischer 	item_p item;
35484cf49a43SJulian Elischer 
3549859a4d16SJulian Elischer 	/*
3550859a4d16SJulian Elischer 	 * Find the target node.
3551859a4d16SJulian Elischer 	 * If there is a HOOK argument, then use that in preference
3552859a4d16SJulian Elischer 	 * to the address.
3553859a4d16SJulian Elischer 	 */
3554069154d5SJulian Elischer 	if ((item = ng_getqblk()) == NULL) {
3555069154d5SJulian Elischer 		if ((msg->header.flags & NGF_STATIC) == 0) {
3556069154d5SJulian Elischer 			NG_FREE_MSG(msg);
35574cf49a43SJulian Elischer 		}
3558069154d5SJulian Elischer 		return (NULL);
35594cf49a43SJulian Elischer 	}
35604cf49a43SJulian Elischer 
35614cf49a43SJulian Elischer 	/* Fill out the contents */
3562069154d5SJulian Elischer 	item->el_flags = NGQF_MESG;
3563069154d5SJulian Elischer 	item->el_next = NULL;
3564069154d5SJulian Elischer 	item->el_dest = here;
356530400f03SJulian Elischer 	NG_NODE_REF(here);
3566069154d5SJulian Elischer 	item->el_hook = hook;
3567069154d5SJulian Elischer 	if (hook)
356830400f03SJulian Elischer 		NG_HOOK_REF(hook);
3569069154d5SJulian Elischer 	NGI_MSG(item) = msg;
3570069154d5SJulian Elischer 	NGI_RETADDR(item) = ng_node2ID(here);
3571069154d5SJulian Elischer 	return (item);
35724cf49a43SJulian Elischer }
35734cf49a43SJulian Elischer 
35746b795970SJulian Elischer int
35756b795970SJulian Elischer ng_send_fn(node_p node, hook_p hook, ng_item_fn *fn, void * arg1, int arg2)
35766b795970SJulian Elischer {
35776b795970SJulian Elischer 	item_p item;
35786b795970SJulian Elischer 
35796b795970SJulian Elischer 	if ((item = ng_getqblk()) == NULL) {
35806b795970SJulian Elischer 		return (ENOMEM);
35816b795970SJulian Elischer 	}
35826b795970SJulian Elischer 	item->el_flags = NGQF_FN | NGQF_WRITER;
35836b795970SJulian Elischer 	item->el_dest = node;
35846b795970SJulian Elischer 	NG_NODE_REF(node);
35856b795970SJulian Elischer 	if ((item->el_hook = hook)) {
35866b795970SJulian Elischer 		NG_HOOK_REF(hook);
35876b795970SJulian Elischer 	}
35886b795970SJulian Elischer 	NGI_FN(item) = fn;
35896b795970SJulian Elischer 	NGI_ARG1(item) = arg1;
35906b795970SJulian Elischer 	NGI_ARG2(item) = arg2;
35916b795970SJulian Elischer 	return (ng_snd_item(item, 0));
35926b795970SJulian Elischer }
35936b795970SJulian Elischer 
35944cf49a43SJulian Elischer /*
3595069154d5SJulian Elischer  * Set the address, if none given, give the node here.
35964cf49a43SJulian Elischer  */
3597069154d5SJulian Elischer void
3598069154d5SJulian Elischer ng_replace_retaddr(node_p here, item_p item, ng_ID_t retaddr)
35994cf49a43SJulian Elischer {
3600069154d5SJulian Elischer 	if (retaddr) {
3601069154d5SJulian Elischer 		NGI_RETADDR(item) = retaddr;
3602069154d5SJulian Elischer 	} else {
3603069154d5SJulian Elischer 		/*
3604069154d5SJulian Elischer 		 * The old return address should be ok.
3605069154d5SJulian Elischer 		 * If there isn't one, use the address here.
3606069154d5SJulian Elischer 		 */
3607069154d5SJulian Elischer 		NGI_RETADDR(item) = ng_node2ID(here);
3608069154d5SJulian Elischer 	}
3609069154d5SJulian Elischer }
3610069154d5SJulian Elischer 
3611069154d5SJulian Elischer #define TESTING
3612069154d5SJulian Elischer #ifdef TESTING
3613069154d5SJulian Elischer /* just test all the macros */
3614069154d5SJulian Elischer void
3615069154d5SJulian Elischer ng_macro_test(item_p item);
3616069154d5SJulian Elischer void
3617069154d5SJulian Elischer ng_macro_test(item_p item)
3618069154d5SJulian Elischer {
3619069154d5SJulian Elischer 	node_p node = NULL;
3620069154d5SJulian Elischer 	hook_p hook = NULL;
36214cf49a43SJulian Elischer 	struct mbuf *m;
36224cf49a43SJulian Elischer 	meta_p meta;
36234cf49a43SJulian Elischer 	struct ng_mesg *msg;
3624069154d5SJulian Elischer 	ng_ID_t retaddr;
3625069154d5SJulian Elischer 	int	error;
36264cf49a43SJulian Elischer 
3627069154d5SJulian Elischer 	NGI_GET_M(item, m);
3628069154d5SJulian Elischer 	NGI_GET_META(item, meta);
3629069154d5SJulian Elischer 	NGI_GET_MSG(item, msg);
3630069154d5SJulian Elischer 	retaddr = NGI_RETADDR(item);
3631069154d5SJulian Elischer 	NG_SEND_DATA(error, hook, m, meta);
3632069154d5SJulian Elischer 	NG_SEND_DATA_ONLY(error, hook, m);
3633069154d5SJulian Elischer 	NG_FWD_NEW_DATA(error, item, hook, m);
363430400f03SJulian Elischer 	NG_FWD_ITEM_HOOK(error, item, hook);
3635069154d5SJulian Elischer 	NG_SEND_MSG_HOOK(error, node, msg, hook, retaddr);
3636069154d5SJulian Elischer 	NG_SEND_MSG_ID(error, node, msg, retaddr, retaddr);
3637069154d5SJulian Elischer 	NG_SEND_MSG_PATH(error, node, msg, ".:", retaddr);
3638069154d5SJulian Elischer 	NG_QUEUE_MSG(error, node, msg, ".:", retaddr);
3639069154d5SJulian Elischer 	NG_FWD_MSG_HOOK(error, node, item, hook, retaddr);
36404cf49a43SJulian Elischer }
3641069154d5SJulian Elischer #endif /* TESTING */
36424cf49a43SJulian Elischer 
3643