xref: /freebsd/sys/netgraph/ng_base.c (revision 104a9b7e3edbd88cdda0698c5c77a2ad6dafcc16)
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>
51104a9b7eSAlexander Kabaev #include <sys/limits.h>
524cf49a43SJulian Elischer #include <sys/malloc.h>
534cf49a43SJulian Elischer #include <sys/syslog.h>
54069154d5SJulian Elischer #include <sys/sysctl.h>
554cf49a43SJulian Elischer #include <sys/linker.h>
564cf49a43SJulian Elischer #include <sys/queue.h>
574cf49a43SJulian Elischer #include <sys/mbuf.h>
585b664c7cSPoul-Henning Kamp #include <sys/ctype.h>
59bfa7e882SJulian Elischer #include <sys/sysctl.h>
604cf49a43SJulian Elischer 
614cf49a43SJulian Elischer #include <net/netisr.h>
624cf49a43SJulian Elischer 
634cf49a43SJulian Elischer #include <netgraph/ng_message.h>
644cf49a43SJulian Elischer #include <netgraph/netgraph.h>
65f8307e12SArchie Cobbs #include <netgraph/ng_parse.h>
664cf49a43SJulian Elischer 
679d72a7a3SJulian Elischer MODULE_VERSION(netgraph, NG_ABI_VERSION);
6899ff8176SPeter Wemm 
6930400f03SJulian Elischer /* List of all active nodes */
70069154d5SJulian Elischer static LIST_HEAD(, ng_node) ng_nodelist;
71069154d5SJulian Elischer static struct mtx	ng_nodelist_mtx;
72069154d5SJulian Elischer 
7330400f03SJulian Elischer #ifdef	NETGRAPH_DEBUG
7430400f03SJulian Elischer 
7530400f03SJulian Elischer static SLIST_HEAD(, ng_node) ng_allnodes;
7630400f03SJulian Elischer static LIST_HEAD(, ng_node) ng_freenodes; /* in debug, we never free() them */
7730400f03SJulian Elischer static SLIST_HEAD(, ng_hook) ng_allhooks;
7830400f03SJulian Elischer static LIST_HEAD(, ng_hook) ng_freehooks; /* in debug, we never free() them */
7930400f03SJulian Elischer 
8030400f03SJulian Elischer static void ng_dumpitems(void);
8130400f03SJulian Elischer static void ng_dumpnodes(void);
8230400f03SJulian Elischer static void ng_dumphooks(void);
8330400f03SJulian Elischer 
8430400f03SJulian Elischer #endif	/* NETGRAPH_DEBUG */
85954c4772SJulian Elischer /*
86954c4772SJulian Elischer  * DEAD versions of the structures.
87954c4772SJulian Elischer  * In order to avoid races, it is sometimes neccesary to point
88954c4772SJulian Elischer  * at SOMETHING even though theoretically, the current entity is
89954c4772SJulian Elischer  * INVALID. Use these to avoid these races.
90954c4772SJulian Elischer  */
91954c4772SJulian Elischer struct ng_type ng_deadtype = {
92954c4772SJulian Elischer 	NG_ABI_VERSION,
93954c4772SJulian Elischer 	"dead",
94954c4772SJulian Elischer 	NULL,	/* modevent */
95954c4772SJulian Elischer 	NULL,	/* constructor */
96954c4772SJulian Elischer 	NULL,	/* rcvmsg */
97954c4772SJulian Elischer 	NULL,	/* shutdown */
98954c4772SJulian Elischer 	NULL,	/* newhook */
99954c4772SJulian Elischer 	NULL,	/* findhook */
100954c4772SJulian Elischer 	NULL,	/* connect */
101954c4772SJulian Elischer 	NULL,	/* rcvdata */
102954c4772SJulian Elischer 	NULL,	/* disconnect */
103954c4772SJulian Elischer 	NULL, 	/* cmdlist */
104954c4772SJulian Elischer };
10530400f03SJulian Elischer 
106954c4772SJulian Elischer struct ng_node ng_deadnode = {
107954c4772SJulian Elischer 	"dead",
108954c4772SJulian Elischer 	&ng_deadtype,
109954c4772SJulian Elischer 	NG_INVALID,
110954c4772SJulian Elischer 	1,	/* refs */
111954c4772SJulian Elischer 	0,	/* numhooks */
112954c4772SJulian Elischer 	NULL,	/* private */
113954c4772SJulian Elischer 	0,	/* ID */
114954c4772SJulian Elischer 	LIST_HEAD_INITIALIZER(ng_deadnode.hooks),
115954c4772SJulian Elischer 	{},	/* all_nodes list entry */
116954c4772SJulian Elischer 	{},	/* id hashtable list entry */
117954c4772SJulian Elischer 	{},	/* workqueue entry */
118954c4772SJulian Elischer 	{	0,
119954c4772SJulian Elischer 		{}, /* should never use! (should hang) */
120954c4772SJulian Elischer 		NULL,
121954c4772SJulian Elischer 		&ng_deadnode.nd_input_queue.queue,
122954c4772SJulian Elischer 		&ng_deadnode
123954c4772SJulian Elischer 	},
124954c4772SJulian Elischer #ifdef	NETGRAPH_DEBUG
125954c4772SJulian Elischer 	ND_MAGIC,
126954c4772SJulian Elischer 	__FILE__,
127954c4772SJulian Elischer 	__LINE__,
128954c4772SJulian Elischer 	{NULL}
129954c4772SJulian Elischer #endif	/* NETGRAPH_DEBUG */
130954c4772SJulian Elischer };
131954c4772SJulian Elischer 
132954c4772SJulian Elischer struct ng_hook ng_deadhook = {
133954c4772SJulian Elischer 	"dead",
134954c4772SJulian Elischer 	NULL,		/* private */
135954c4772SJulian Elischer 	HK_INVALID | HK_DEAD,
136954c4772SJulian Elischer 	1,		/* refs always >= 1 */
137954c4772SJulian Elischer 	&ng_deadhook,	/* Peer is self */
138954c4772SJulian Elischer 	&ng_deadnode,	/* attached to deadnode */
139954c4772SJulian Elischer 	{},		/* hooks list */
140c4b5eea4SJulian Elischer 	NULL,		/* override rcvmsg() */
141c4b5eea4SJulian Elischer 	NULL,		/* override rcvdata() */
142954c4772SJulian Elischer #ifdef	NETGRAPH_DEBUG
143954c4772SJulian Elischer 	HK_MAGIC,
144954c4772SJulian Elischer 	__FILE__,
145954c4772SJulian Elischer 	__LINE__,
146954c4772SJulian Elischer 	{NULL}
147954c4772SJulian Elischer #endif	/* NETGRAPH_DEBUG */
148954c4772SJulian Elischer };
149954c4772SJulian Elischer 
150954c4772SJulian Elischer /*
151954c4772SJulian Elischer  * END DEAD STRUCTURES
152954c4772SJulian Elischer  */
153069154d5SJulian Elischer /* List nodes with unallocated work */
154069154d5SJulian Elischer static TAILQ_HEAD(, ng_node) ng_worklist = TAILQ_HEAD_INITIALIZER(ng_worklist);
155b57a7965SJulian Elischer static struct mtx	ng_worklist_mtx;   /* MUST LOCK NODE FIRST */
1564cf49a43SJulian Elischer 
1574cf49a43SJulian Elischer /* List of installed types */
158069154d5SJulian Elischer static LIST_HEAD(, ng_type) ng_typelist;
159069154d5SJulian Elischer static struct mtx	ng_typelist_mtx;
1604cf49a43SJulian Elischer 
161069154d5SJulian Elischer /* Hash related definitions */
1620f150d04SJulian Elischer /* XXX Don't need to initialise them because it's a LIST */
1630f150d04SJulian Elischer #define NG_ID_HASH_SIZE 32 /* most systems wont need even this many */
1640f150d04SJulian Elischer static LIST_HEAD(, ng_node) ng_ID_hash[NG_ID_HASH_SIZE];
165069154d5SJulian Elischer static struct mtx	ng_idhash_mtx;
1660f150d04SJulian Elischer /* Method to find a node.. used twice so do it here */
1670f150d04SJulian Elischer #define NG_IDHASH_FN(ID) ((ID) % (NG_ID_HASH_SIZE))
1680f150d04SJulian Elischer #define NG_IDHASH_FIND(ID, node)					\
1690f150d04SJulian Elischer 	do { 								\
1700f150d04SJulian Elischer 		LIST_FOREACH(node, &ng_ID_hash[NG_IDHASH_FN(ID)],	\
1710f150d04SJulian Elischer 						nd_idnodes) {		\
1720f150d04SJulian Elischer 			if (NG_NODE_IS_VALID(node)			\
1730f150d04SJulian Elischer 			&& (NG_NODE_ID(node) == ID)) {			\
1740f150d04SJulian Elischer 				break;					\
1750f150d04SJulian Elischer 			}						\
1760f150d04SJulian Elischer 		}							\
1770f150d04SJulian Elischer 	} while (0)
178069154d5SJulian Elischer 
179069154d5SJulian Elischer /* Mutex that protects the free queue item list */
180069154d5SJulian Elischer static volatile item_p		ngqfree;	/* free ones */
181069154d5SJulian Elischer static struct mtx	ngq_mtx;
182dc90cad9SJulian Elischer 
1834cf49a43SJulian Elischer /* Internal functions */
1844cf49a43SJulian Elischer static int	ng_add_hook(node_p node, const char *name, hook_p * hookp);
185069154d5SJulian Elischer static int	ng_generic_msg(node_p here, item_p item, hook_p lasthook);
186dc90cad9SJulian Elischer static ng_ID_t	ng_decodeidname(const char *name);
1874cf49a43SJulian Elischer static int	ngb_mod_event(module_t mod, int event, void *data);
188069154d5SJulian Elischer static void	ng_worklist_remove(node_p node);
1894cf49a43SJulian Elischer static void	ngintr(void);
1905951069aSJulian Elischer static int	ng_apply_item(node_p node, item_p item);
191069154d5SJulian Elischer static void	ng_flush_input_queue(struct ng_queue * ngq);
192069154d5SJulian Elischer static void	ng_setisr(node_p node);
193069154d5SJulian Elischer static node_p	ng_ID2noderef(ng_ID_t ID);
1946b795970SJulian Elischer static int	ng_con_nodes(node_p node, const char *name, node_p node2,
1956b795970SJulian Elischer 							const char *name2);
1961acb27c6SJulian Elischer static void	ng_con_part2(node_p node, hook_p hook, void *arg1, int arg2);
1971acb27c6SJulian Elischer static void	ng_con_part3(node_p node, hook_p hook, void *arg1, int arg2);
1986b795970SJulian Elischer static int	ng_mkpeer(node_p node, const char *name,
1996b795970SJulian Elischer 						const char *name2, char *type);
200069154d5SJulian Elischer 
20130400f03SJulian Elischer /* imported , these used to be externally visible, some may go back */
202069154d5SJulian Elischer int	ng_bypass(hook_p hook1, hook_p hook2);
203069154d5SJulian Elischer void	ng_destroy_hook(hook_p hook);
204069154d5SJulian Elischer node_p	ng_name2noderef(node_p node, const char *name);
205069154d5SJulian Elischer int	ng_path2noderef(node_p here, const char *path,
206069154d5SJulian Elischer 	node_p *dest, hook_p *lasthook);
207069154d5SJulian Elischer struct	ng_type *ng_findtype(const char *type);
208069154d5SJulian Elischer int	ng_make_node(const char *type, node_p *nodepp);
209069154d5SJulian Elischer int	ng_path_parse(char *addr, char **node, char **path, char **hook);
2101acb27c6SJulian Elischer void	ng_rmnode(node_p node, hook_p dummy1, void *dummy2, int dummy3);
21130400f03SJulian Elischer void	ng_unname(node_p node);
212069154d5SJulian Elischer 
2134cf49a43SJulian Elischer 
2144cf49a43SJulian Elischer /* Our own netgraph malloc type */
2154cf49a43SJulian Elischer MALLOC_DEFINE(M_NETGRAPH, "netgraph", "netgraph structures and ctrl messages");
216069154d5SJulian Elischer MALLOC_DEFINE(M_NETGRAPH_HOOK, "netgraph_hook", "netgraph hook structures");
217069154d5SJulian Elischer MALLOC_DEFINE(M_NETGRAPH_NODE, "netgraph_node", "netgraph node structures");
218069154d5SJulian Elischer MALLOC_DEFINE(M_NETGRAPH_ITEM, "netgraph_item", "netgraph item structures");
219069154d5SJulian Elischer MALLOC_DEFINE(M_NETGRAPH_META, "netgraph_meta", "netgraph name storage");
220069154d5SJulian Elischer MALLOC_DEFINE(M_NETGRAPH_MSG, "netgraph_msg", "netgraph name storage");
221069154d5SJulian Elischer 
222069154d5SJulian Elischer /* Should not be visible outside this file */
22330400f03SJulian Elischer 
22430400f03SJulian Elischer #define _NG_ALLOC_HOOK(hook) \
22530400f03SJulian Elischer 	MALLOC(hook, hook_p, sizeof(*hook), M_NETGRAPH_HOOK, M_NOWAIT | M_ZERO)
22630400f03SJulian Elischer #define _NG_ALLOC_NODE(node) \
22730400f03SJulian Elischer 	MALLOC(node, node_p, sizeof(*node), M_NETGRAPH_NODE, M_NOWAIT | M_ZERO)
22830400f03SJulian Elischer 
22930400f03SJulian Elischer #ifdef NETGRAPH_DEBUG /*----------------------------------------------*/
23030400f03SJulian Elischer /*
23130400f03SJulian Elischer  * In debug mode:
23230400f03SJulian Elischer  * In an attempt to help track reference count screwups
23330400f03SJulian Elischer  * we do not free objects back to the malloc system, but keep them
23430400f03SJulian Elischer  * in a local cache where we can examine them and keep information safely
23530400f03SJulian Elischer  * after they have been freed.
23630400f03SJulian Elischer  * We use this scheme for nodes and hooks, and to some extent for items.
23730400f03SJulian Elischer  */
23830400f03SJulian Elischer static __inline hook_p
23930400f03SJulian Elischer ng_alloc_hook(void)
24030400f03SJulian Elischer {
24130400f03SJulian Elischer 	hook_p hook;
24230400f03SJulian Elischer 	SLIST_ENTRY(ng_hook) temp;
2439ed346baSBosko Milekic 	mtx_lock(&ng_nodelist_mtx);
24430400f03SJulian Elischer 	hook = LIST_FIRST(&ng_freehooks);
24530400f03SJulian Elischer 	if (hook) {
24630400f03SJulian Elischer 		LIST_REMOVE(hook, hk_hooks);
24730400f03SJulian Elischer 		bcopy(&hook->hk_all, &temp, sizeof(temp));
24830400f03SJulian Elischer 		bzero(hook, sizeof(struct ng_hook));
24930400f03SJulian Elischer 		bcopy(&temp, &hook->hk_all, sizeof(temp));
2509ed346baSBosko Milekic 		mtx_unlock(&ng_nodelist_mtx);
25130400f03SJulian Elischer 		hook->hk_magic = HK_MAGIC;
25230400f03SJulian Elischer 	} else {
2539ed346baSBosko Milekic 		mtx_unlock(&ng_nodelist_mtx);
25430400f03SJulian Elischer 		_NG_ALLOC_HOOK(hook);
25530400f03SJulian Elischer 		if (hook) {
25630400f03SJulian Elischer 			hook->hk_magic = HK_MAGIC;
2579ed346baSBosko Milekic 			mtx_lock(&ng_nodelist_mtx);
25830400f03SJulian Elischer 			SLIST_INSERT_HEAD(&ng_allhooks, hook, hk_all);
2599ed346baSBosko Milekic 			mtx_unlock(&ng_nodelist_mtx);
26030400f03SJulian Elischer 		}
26130400f03SJulian Elischer 	}
26230400f03SJulian Elischer 	return (hook);
26330400f03SJulian Elischer }
26430400f03SJulian Elischer 
26530400f03SJulian Elischer static __inline node_p
26630400f03SJulian Elischer ng_alloc_node(void)
26730400f03SJulian Elischer {
26830400f03SJulian Elischer 	node_p node;
26930400f03SJulian Elischer 	SLIST_ENTRY(ng_node) temp;
2709ed346baSBosko Milekic 	mtx_lock(&ng_nodelist_mtx);
27130400f03SJulian Elischer 	node = LIST_FIRST(&ng_freenodes);
27230400f03SJulian Elischer 	if (node) {
27330400f03SJulian Elischer 		LIST_REMOVE(node, nd_nodes);
27430400f03SJulian Elischer 		bcopy(&node->nd_all, &temp, sizeof(temp));
27530400f03SJulian Elischer 		bzero(node, sizeof(struct ng_node));
27630400f03SJulian Elischer 		bcopy(&temp, &node->nd_all, sizeof(temp));
2779ed346baSBosko Milekic 		mtx_unlock(&ng_nodelist_mtx);
27830400f03SJulian Elischer 		node->nd_magic = ND_MAGIC;
27930400f03SJulian Elischer 	} else {
2809ed346baSBosko Milekic 		mtx_unlock(&ng_nodelist_mtx);
28130400f03SJulian Elischer 		_NG_ALLOC_NODE(node);
28230400f03SJulian Elischer 		if (node) {
28330400f03SJulian Elischer 			node->nd_magic = ND_MAGIC;
2849ed346baSBosko Milekic 			mtx_lock(&ng_nodelist_mtx);
28530400f03SJulian Elischer 			SLIST_INSERT_HEAD(&ng_allnodes, node, nd_all);
2869ed346baSBosko Milekic 			mtx_unlock(&ng_nodelist_mtx);
28730400f03SJulian Elischer 		}
28830400f03SJulian Elischer 	}
28930400f03SJulian Elischer 	return (node);
29030400f03SJulian Elischer }
29130400f03SJulian Elischer 
29230400f03SJulian Elischer #define NG_ALLOC_HOOK(hook) do { (hook) = ng_alloc_hook(); } while (0)
29330400f03SJulian Elischer #define NG_ALLOC_NODE(node) do { (node) = ng_alloc_node(); } while (0)
29430400f03SJulian Elischer 
29530400f03SJulian Elischer 
29630400f03SJulian Elischer #define NG_FREE_HOOK(hook)						\
29730400f03SJulian Elischer 	do {								\
2989ed346baSBosko Milekic 		mtx_lock(&ng_nodelist_mtx);			\
29930400f03SJulian Elischer 		LIST_INSERT_HEAD(&ng_freehooks, hook, hk_hooks);	\
30030400f03SJulian Elischer 		hook->hk_magic = 0;					\
3019ed346baSBosko Milekic 		mtx_unlock(&ng_nodelist_mtx);			\
30230400f03SJulian Elischer 	} while (0)
30330400f03SJulian Elischer 
30430400f03SJulian Elischer #define NG_FREE_NODE(node)						\
30530400f03SJulian Elischer 	do {								\
3069ed346baSBosko Milekic 		mtx_lock(&ng_nodelist_mtx);			\
30730400f03SJulian Elischer 		LIST_INSERT_HEAD(&ng_freenodes, node, nd_nodes);	\
30830400f03SJulian Elischer 		node->nd_magic = 0;					\
3099ed346baSBosko Milekic 		mtx_unlock(&ng_nodelist_mtx);			\
31030400f03SJulian Elischer 	} while (0)
31130400f03SJulian Elischer 
31230400f03SJulian Elischer #else /* NETGRAPH_DEBUG */ /*----------------------------------------------*/
31330400f03SJulian Elischer 
31430400f03SJulian Elischer #define NG_ALLOC_HOOK(hook) _NG_ALLOC_HOOK(hook)
31530400f03SJulian Elischer #define NG_ALLOC_NODE(node) _NG_ALLOC_NODE(node)
31630400f03SJulian Elischer 
317069154d5SJulian Elischer #define NG_FREE_HOOK(hook) do { FREE((hook), M_NETGRAPH_HOOK); } while (0)
318069154d5SJulian Elischer #define NG_FREE_NODE(node) do { FREE((node), M_NETGRAPH_NODE); } while (0)
31930400f03SJulian Elischer 
32030400f03SJulian Elischer #endif /* NETGRAPH_DEBUG */ /*----------------------------------------------*/
32130400f03SJulian Elischer 
322069154d5SJulian Elischer /* Warning: Generally use NG_FREE_ITEM() instead */
323069154d5SJulian Elischer #define NG_FREE_ITEM_REAL(item) do { FREE((item), M_NETGRAPH_ITEM); } while (0)
324069154d5SJulian Elischer 
3254cf49a43SJulian Elischer 
3264cf49a43SJulian Elischer /* Set this to Debugger("X") to catch all errors as they occur */
3274cf49a43SJulian Elischer #ifndef TRAP_ERROR
3286b795970SJulian Elischer #define TRAP_ERROR()
3294cf49a43SJulian Elischer #endif
3304cf49a43SJulian Elischer 
331dc90cad9SJulian Elischer static	ng_ID_t nextID = 1;
332dc90cad9SJulian Elischer 
333b2da83c2SArchie Cobbs #ifdef INVARIANTS
334b2da83c2SArchie Cobbs #define CHECK_DATA_MBUF(m)	do {					\
335b2da83c2SArchie Cobbs 		struct mbuf *n;						\
336b2da83c2SArchie Cobbs 		int total;						\
337b2da83c2SArchie Cobbs 									\
338fe584538SDag-Erling Smørgrav 		M_ASSERTPKTHDR(m);					\
339b2da83c2SArchie Cobbs 		for (total = 0, n = (m); n != NULL; n = n->m_next)	\
340b2da83c2SArchie Cobbs 			total += n->m_len;				\
341b2da83c2SArchie Cobbs 		if ((m)->m_pkthdr.len != total) {			\
342b2da83c2SArchie Cobbs 			panic("%s: %d != %d",				\
3436e551fb6SDavid E. O'Brien 			    __func__, (m)->m_pkthdr.len, total);	\
344b2da83c2SArchie Cobbs 		}							\
345b2da83c2SArchie Cobbs 	} while (0)
346b2da83c2SArchie Cobbs #else
347b2da83c2SArchie Cobbs #define CHECK_DATA_MBUF(m)
348b2da83c2SArchie Cobbs #endif
349b2da83c2SArchie Cobbs 
350dc90cad9SJulian Elischer 
3514cf49a43SJulian Elischer /************************************************************************
352f8307e12SArchie Cobbs 	Parse type definitions for generic messages
353f8307e12SArchie Cobbs ************************************************************************/
354f8307e12SArchie Cobbs 
355f8307e12SArchie Cobbs /* Handy structure parse type defining macro */
356f8307e12SArchie Cobbs #define DEFINE_PARSE_STRUCT_TYPE(lo, up, args)				\
357f0184ff8SArchie Cobbs static const struct ng_parse_struct_field				\
358f0184ff8SArchie Cobbs 	ng_ ## lo ## _type_fields[] = NG_GENERIC_ ## up ## _INFO args;	\
359f8307e12SArchie Cobbs static const struct ng_parse_type ng_generic_ ## lo ## _type = {	\
360f8307e12SArchie Cobbs 	&ng_parse_struct_type,						\
361f0184ff8SArchie Cobbs 	&ng_ ## lo ## _type_fields					\
362f8307e12SArchie Cobbs }
363f8307e12SArchie Cobbs 
364f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(mkpeer, MKPEER, ());
365f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(connect, CONNECT, ());
366f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(name, NAME, ());
367f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(rmhook, RMHOOK, ());
368f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(nodeinfo, NODEINFO, ());
369f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(typeinfo, TYPEINFO, ());
370f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(linkinfo, LINKINFO, (&ng_generic_nodeinfo_type));
371f8307e12SArchie Cobbs 
372f8307e12SArchie Cobbs /* Get length of an array when the length is stored as a 32 bit
373d7d97eb0SJeroen Ruigrok van der Werven    value immediately preceding the array -- as with struct namelist
374f8307e12SArchie Cobbs    and struct typelist. */
375f8307e12SArchie Cobbs static int
376f8307e12SArchie Cobbs ng_generic_list_getLength(const struct ng_parse_type *type,
377f8307e12SArchie Cobbs 	const u_char *start, const u_char *buf)
378f8307e12SArchie Cobbs {
379f8307e12SArchie Cobbs 	return *((const u_int32_t *)(buf - 4));
380f8307e12SArchie Cobbs }
381f8307e12SArchie Cobbs 
382f8307e12SArchie Cobbs /* Get length of the array of struct linkinfo inside a struct hooklist */
383f8307e12SArchie Cobbs static int
384f8307e12SArchie Cobbs ng_generic_linkinfo_getLength(const struct ng_parse_type *type,
385f8307e12SArchie Cobbs 	const u_char *start, const u_char *buf)
386f8307e12SArchie Cobbs {
387f8307e12SArchie Cobbs 	const struct hooklist *hl = (const struct hooklist *)start;
388f8307e12SArchie Cobbs 
389f8307e12SArchie Cobbs 	return hl->nodeinfo.hooks;
390f8307e12SArchie Cobbs }
391f8307e12SArchie Cobbs 
392f8307e12SArchie Cobbs /* Array type for a variable length array of struct namelist */
393f8307e12SArchie Cobbs static const struct ng_parse_array_info ng_nodeinfoarray_type_info = {
394f8307e12SArchie Cobbs 	&ng_generic_nodeinfo_type,
395f8307e12SArchie Cobbs 	&ng_generic_list_getLength
396f8307e12SArchie Cobbs };
397f8307e12SArchie Cobbs static const struct ng_parse_type ng_generic_nodeinfoarray_type = {
398f8307e12SArchie Cobbs 	&ng_parse_array_type,
399f8307e12SArchie Cobbs 	&ng_nodeinfoarray_type_info
400f8307e12SArchie Cobbs };
401f8307e12SArchie Cobbs 
402f8307e12SArchie Cobbs /* Array type for a variable length array of struct typelist */
403f8307e12SArchie Cobbs static const struct ng_parse_array_info ng_typeinfoarray_type_info = {
404f8307e12SArchie Cobbs 	&ng_generic_typeinfo_type,
405f8307e12SArchie Cobbs 	&ng_generic_list_getLength
406f8307e12SArchie Cobbs };
407f8307e12SArchie Cobbs static const struct ng_parse_type ng_generic_typeinfoarray_type = {
408f8307e12SArchie Cobbs 	&ng_parse_array_type,
409f8307e12SArchie Cobbs 	&ng_typeinfoarray_type_info
410f8307e12SArchie Cobbs };
411f8307e12SArchie Cobbs 
412f8307e12SArchie Cobbs /* Array type for array of struct linkinfo in struct hooklist */
413f8307e12SArchie Cobbs static const struct ng_parse_array_info ng_generic_linkinfo_array_type_info = {
414f8307e12SArchie Cobbs 	&ng_generic_linkinfo_type,
415f8307e12SArchie Cobbs 	&ng_generic_linkinfo_getLength
416f8307e12SArchie Cobbs };
417f8307e12SArchie Cobbs static const struct ng_parse_type ng_generic_linkinfo_array_type = {
418f8307e12SArchie Cobbs 	&ng_parse_array_type,
419f8307e12SArchie Cobbs 	&ng_generic_linkinfo_array_type_info
420f8307e12SArchie Cobbs };
421f8307e12SArchie Cobbs 
422f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(typelist, TYPELIST, (&ng_generic_nodeinfoarray_type));
423f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(hooklist, HOOKLIST,
424f8307e12SArchie Cobbs 	(&ng_generic_nodeinfo_type, &ng_generic_linkinfo_array_type));
425f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(listnodes, LISTNODES,
426f8307e12SArchie Cobbs 	(&ng_generic_nodeinfoarray_type));
427f8307e12SArchie Cobbs 
428f8307e12SArchie Cobbs /* List of commands and how to convert arguments to/from ASCII */
429f8307e12SArchie Cobbs static const struct ng_cmdlist ng_generic_cmds[] = {
430f8307e12SArchie Cobbs 	{
431f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
432f8307e12SArchie Cobbs 	  NGM_SHUTDOWN,
433f8307e12SArchie Cobbs 	  "shutdown",
434f8307e12SArchie Cobbs 	  NULL,
435f8307e12SArchie Cobbs 	  NULL
436f8307e12SArchie Cobbs 	},
437f8307e12SArchie Cobbs 	{
438f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
439f8307e12SArchie Cobbs 	  NGM_MKPEER,
440f8307e12SArchie Cobbs 	  "mkpeer",
441f8307e12SArchie Cobbs 	  &ng_generic_mkpeer_type,
442f8307e12SArchie Cobbs 	  NULL
443f8307e12SArchie Cobbs 	},
444f8307e12SArchie Cobbs 	{
445f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
446f8307e12SArchie Cobbs 	  NGM_CONNECT,
447f8307e12SArchie Cobbs 	  "connect",
448f8307e12SArchie Cobbs 	  &ng_generic_connect_type,
449f8307e12SArchie Cobbs 	  NULL
450f8307e12SArchie Cobbs 	},
451f8307e12SArchie Cobbs 	{
452f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
453f8307e12SArchie Cobbs 	  NGM_NAME,
454f8307e12SArchie Cobbs 	  "name",
455f8307e12SArchie Cobbs 	  &ng_generic_name_type,
456f8307e12SArchie Cobbs 	  NULL
457f8307e12SArchie Cobbs 	},
458f8307e12SArchie Cobbs 	{
459f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
460f8307e12SArchie Cobbs 	  NGM_RMHOOK,
461f8307e12SArchie Cobbs 	  "rmhook",
462f8307e12SArchie Cobbs 	  &ng_generic_rmhook_type,
463f8307e12SArchie Cobbs 	  NULL
464f8307e12SArchie Cobbs 	},
465f8307e12SArchie Cobbs 	{
466f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
467f8307e12SArchie Cobbs 	  NGM_NODEINFO,
468f8307e12SArchie Cobbs 	  "nodeinfo",
469f8307e12SArchie Cobbs 	  NULL,
470f8307e12SArchie Cobbs 	  &ng_generic_nodeinfo_type
471f8307e12SArchie Cobbs 	},
472f8307e12SArchie Cobbs 	{
473f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
474f8307e12SArchie Cobbs 	  NGM_LISTHOOKS,
475f8307e12SArchie Cobbs 	  "listhooks",
476f8307e12SArchie Cobbs 	  NULL,
477f8307e12SArchie Cobbs 	  &ng_generic_hooklist_type
478f8307e12SArchie Cobbs 	},
479f8307e12SArchie Cobbs 	{
480f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
481f8307e12SArchie Cobbs 	  NGM_LISTNAMES,
482f8307e12SArchie Cobbs 	  "listnames",
483f8307e12SArchie Cobbs 	  NULL,
484f8307e12SArchie Cobbs 	  &ng_generic_listnodes_type	/* same as NGM_LISTNODES */
485f8307e12SArchie Cobbs 	},
486f8307e12SArchie Cobbs 	{
487f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
488f8307e12SArchie Cobbs 	  NGM_LISTNODES,
489f8307e12SArchie Cobbs 	  "listnodes",
490f8307e12SArchie Cobbs 	  NULL,
491f8307e12SArchie Cobbs 	  &ng_generic_listnodes_type
492f8307e12SArchie Cobbs 	},
493f8307e12SArchie Cobbs 	{
494f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
495f8307e12SArchie Cobbs 	  NGM_LISTTYPES,
496f8307e12SArchie Cobbs 	  "listtypes",
497f8307e12SArchie Cobbs 	  NULL,
498f8307e12SArchie Cobbs 	  &ng_generic_typeinfo_type
499f8307e12SArchie Cobbs 	},
500f8307e12SArchie Cobbs 	{
501f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
5027095e097SPoul-Henning Kamp 	  NGM_TEXT_CONFIG,
5037095e097SPoul-Henning Kamp 	  "textconfig",
5047095e097SPoul-Henning Kamp 	  NULL,
5057095e097SPoul-Henning Kamp 	  &ng_parse_string_type
5067095e097SPoul-Henning Kamp 	},
5077095e097SPoul-Henning Kamp 	{
5087095e097SPoul-Henning Kamp 	  NGM_GENERIC_COOKIE,
509f8307e12SArchie Cobbs 	  NGM_TEXT_STATUS,
510f8307e12SArchie Cobbs 	  "textstatus",
511f8307e12SArchie Cobbs 	  NULL,
512f8307e12SArchie Cobbs 	  &ng_parse_string_type
513f8307e12SArchie Cobbs 	},
514f8307e12SArchie Cobbs 	{
515f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
516f8307e12SArchie Cobbs 	  NGM_ASCII2BINARY,
517f8307e12SArchie Cobbs 	  "ascii2binary",
518f8307e12SArchie Cobbs 	  &ng_parse_ng_mesg_type,
519f8307e12SArchie Cobbs 	  &ng_parse_ng_mesg_type
520f8307e12SArchie Cobbs 	},
521f8307e12SArchie Cobbs 	{
522f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
523f8307e12SArchie Cobbs 	  NGM_BINARY2ASCII,
524f8307e12SArchie Cobbs 	  "binary2ascii",
525f8307e12SArchie Cobbs 	  &ng_parse_ng_mesg_type,
526f8307e12SArchie Cobbs 	  &ng_parse_ng_mesg_type
527f8307e12SArchie Cobbs 	},
528f8307e12SArchie Cobbs 	{ 0 }
529f8307e12SArchie Cobbs };
530f8307e12SArchie Cobbs 
531f8307e12SArchie Cobbs /************************************************************************
5324cf49a43SJulian Elischer 			Node routines
5334cf49a43SJulian Elischer ************************************************************************/
5344cf49a43SJulian Elischer 
5354cf49a43SJulian Elischer /*
5364cf49a43SJulian Elischer  * Instantiate a node of the requested type
5374cf49a43SJulian Elischer  */
5384cf49a43SJulian Elischer int
5394cf49a43SJulian Elischer ng_make_node(const char *typename, node_p *nodepp)
5404cf49a43SJulian Elischer {
5414cf49a43SJulian Elischer 	struct ng_type *type;
542069154d5SJulian Elischer 	int	error;
5434cf49a43SJulian Elischer 
5444cf49a43SJulian Elischer 	/* Check that the type makes sense */
5454cf49a43SJulian Elischer 	if (typename == NULL) {
5466b795970SJulian Elischer 		TRAP_ERROR();
5474cf49a43SJulian Elischer 		return (EINVAL);
5484cf49a43SJulian Elischer 	}
5494cf49a43SJulian Elischer 
5504cf49a43SJulian Elischer 	/* Locate the node type */
5514cf49a43SJulian Elischer 	if ((type = ng_findtype(typename)) == NULL) {
552cc3daab5SPeter Wemm 		char filename[NG_TYPELEN + 4];
5534cf49a43SJulian Elischer 		linker_file_t lf;
5544cf49a43SJulian Elischer 		int error;
5554cf49a43SJulian Elischer 
5564cf49a43SJulian Elischer 		/* Not found, try to load it as a loadable module */
557453b5565SJulian Elischer 		snprintf(filename, sizeof(filename), "ng_%s", typename);
558f2b17113SMaxime Henrion 		error = linker_load_module(NULL, filename, NULL, NULL, &lf);
5594cf49a43SJulian Elischer 		if (error != 0)
5604cf49a43SJulian Elischer 			return (error);
5614cf49a43SJulian Elischer 		lf->userrefs++;		/* pretend loaded by the syscall */
5624cf49a43SJulian Elischer 
5634cf49a43SJulian Elischer 		/* Try again, as now the type should have linked itself in */
5644cf49a43SJulian Elischer 		if ((type = ng_findtype(typename)) == NULL)
5654cf49a43SJulian Elischer 			return (ENXIO);
5664cf49a43SJulian Elischer 	}
5674cf49a43SJulian Elischer 
568069154d5SJulian Elischer 	/*
569069154d5SJulian Elischer 	 * If we have a constructor, then make the node and
570069154d5SJulian Elischer 	 * call the constructor to do type specific initialisation.
571069154d5SJulian Elischer 	 */
572069154d5SJulian Elischer 	if (type->constructor != NULL) {
573069154d5SJulian Elischer 		if ((error = ng_make_node_common(type, nodepp)) == 0) {
574069154d5SJulian Elischer 			if ((error = ((*type->constructor)(*nodepp)) != 0)) {
57530400f03SJulian Elischer 				NG_NODE_UNREF(*nodepp);
576069154d5SJulian Elischer 			}
577069154d5SJulian Elischer 		}
578069154d5SJulian Elischer 	} else {
579069154d5SJulian Elischer 		/*
580069154d5SJulian Elischer 		 * Node has no constructor. We cannot ask for one
581069154d5SJulian Elischer 		 * to be made. It must be brought into existance by
582954c4772SJulian Elischer 		 * some external agency. The external agency should
583069154d5SJulian Elischer 		 * call ng_make_node_common() directly to get the
584069154d5SJulian Elischer 		 * netgraph part initialised.
585069154d5SJulian Elischer 		 */
5866b795970SJulian Elischer 		TRAP_ERROR();
587069154d5SJulian Elischer 		error = EINVAL;
588069154d5SJulian Elischer 	}
589069154d5SJulian Elischer 	return (error);
5904cf49a43SJulian Elischer }
5914cf49a43SJulian Elischer 
5924cf49a43SJulian Elischer /*
593069154d5SJulian Elischer  * Generic node creation. Called by node initialisation for externally
594069154d5SJulian Elischer  * instantiated nodes (e.g. hardware, sockets, etc ).
5954cf49a43SJulian Elischer  * The returned node has a reference count of 1.
5964cf49a43SJulian Elischer  */
5974cf49a43SJulian Elischer int
5984cf49a43SJulian Elischer ng_make_node_common(struct ng_type *type, node_p *nodepp)
5994cf49a43SJulian Elischer {
6004cf49a43SJulian Elischer 	node_p node;
6014cf49a43SJulian Elischer 
6024cf49a43SJulian Elischer 	/* Require the node type to have been already installed */
6034cf49a43SJulian Elischer 	if (ng_findtype(type->name) == NULL) {
6046b795970SJulian Elischer 		TRAP_ERROR();
6054cf49a43SJulian Elischer 		return (EINVAL);
6064cf49a43SJulian Elischer 	}
6074cf49a43SJulian Elischer 
6084cf49a43SJulian Elischer 	/* Make a node and try attach it to the type */
60930400f03SJulian Elischer 	NG_ALLOC_NODE(node);
6104cf49a43SJulian Elischer 	if (node == NULL) {
6116b795970SJulian Elischer 		TRAP_ERROR();
6124cf49a43SJulian Elischer 		return (ENOMEM);
6134cf49a43SJulian Elischer 	}
61430400f03SJulian Elischer 	node->nd_type = type;
61530400f03SJulian Elischer 	NG_NODE_REF(node);				/* note reference */
6164cf49a43SJulian Elischer 	type->refs++;
6174cf49a43SJulian Elischer 
6186008862bSJohn Baldwin 	mtx_init(&node->nd_input_queue.q_mtx, "ng_node", NULL, MTX_SPIN);
61930400f03SJulian Elischer 	node->nd_input_queue.queue = NULL;
62030400f03SJulian Elischer 	node->nd_input_queue.last = &node->nd_input_queue.queue;
62130400f03SJulian Elischer 	node->nd_input_queue.q_flags = 0;
62230400f03SJulian Elischer 	node->nd_input_queue.q_node = node;
6234cf49a43SJulian Elischer 
6244cf49a43SJulian Elischer 	/* Initialize hook list for new node */
62530400f03SJulian Elischer 	LIST_INIT(&node->nd_hooks);
6264cf49a43SJulian Elischer 
627069154d5SJulian Elischer 	/* Link us into the node linked list */
6289ed346baSBosko Milekic 	mtx_lock(&ng_nodelist_mtx);
62930400f03SJulian Elischer 	LIST_INSERT_HEAD(&ng_nodelist, node, nd_nodes);
6309ed346baSBosko Milekic 	mtx_unlock(&ng_nodelist_mtx);
631069154d5SJulian Elischer 
632069154d5SJulian Elischer 
633dc90cad9SJulian Elischer 	/* get an ID and put us in the hash chain */
6349ed346baSBosko Milekic 	mtx_lock(&ng_idhash_mtx);
63530400f03SJulian Elischer 	for (;;) { /* wrap protection, even if silly */
636069154d5SJulian Elischer 		node_p node2 = NULL;
63730400f03SJulian Elischer 		node->nd_ID = nextID++; /* 137/second for 1 year before wrap */
6380f150d04SJulian Elischer 
63930400f03SJulian Elischer 		/* Is there a problem with the new number? */
6400f150d04SJulian Elischer 		NG_IDHASH_FIND(node->nd_ID, node2); /* already taken? */
6410f150d04SJulian Elischer 		if ((node->nd_ID != 0) && (node2 == NULL)) {
64230400f03SJulian Elischer 			break;
643069154d5SJulian Elischer 		}
64430400f03SJulian Elischer 	}
6450f150d04SJulian Elischer 	LIST_INSERT_HEAD(&ng_ID_hash[NG_IDHASH_FN(node->nd_ID)],
64630400f03SJulian Elischer 							node, nd_idnodes);
6479ed346baSBosko Milekic 	mtx_unlock(&ng_idhash_mtx);
648dc90cad9SJulian Elischer 
6494cf49a43SJulian Elischer 	/* Done */
6504cf49a43SJulian Elischer 	*nodepp = node;
6514cf49a43SJulian Elischer 	return (0);
6524cf49a43SJulian Elischer }
6534cf49a43SJulian Elischer 
6544cf49a43SJulian Elischer /*
6554cf49a43SJulian Elischer  * Forceably start the shutdown process on a node. Either call
6564cf49a43SJulian Elischer  * it's shutdown method, or do the default shutdown if there is
6574cf49a43SJulian Elischer  * no type-specific method.
6584cf49a43SJulian Elischer  *
659069154d5SJulian Elischer  * We can only be called form a shutdown message, so we know we have
6603e4084c8SJulian Elischer  * a writer lock, and therefore exclusive access. It also means
6613e4084c8SJulian Elischer  * that we should not be on the work queue, but we check anyhow.
662069154d5SJulian Elischer  *
663069154d5SJulian Elischer  * Persistent node types must have a type-specific method which
6643e4084c8SJulian Elischer  * Allocates a new node in which case, this one is irretrievably going away,
6653e4084c8SJulian Elischer  * or cleans up anything it needs, and just makes the node valid again,
6663e4084c8SJulian Elischer  * in which case we allow the node to survive.
6673e4084c8SJulian Elischer  *
6683e4084c8SJulian Elischer  * XXX We need to think of how to tell a persistant node that we
6693e4084c8SJulian Elischer  * REALLY need to go away because the hardware has gone or we
6703e4084c8SJulian Elischer  * are rebooting.... etc.
6714cf49a43SJulian Elischer  */
6724cf49a43SJulian Elischer void
6731acb27c6SJulian Elischer ng_rmnode(node_p node, hook_p dummy1, void *dummy2, int dummy3)
6744cf49a43SJulian Elischer {
6753e4084c8SJulian Elischer 	hook_p hook;
6763e4084c8SJulian Elischer 
6774cf49a43SJulian Elischer 	/* Check if it's already shutting down */
67830400f03SJulian Elischer 	if ((node->nd_flags & NG_CLOSING) != 0)
6794cf49a43SJulian Elischer 		return;
6804cf49a43SJulian Elischer 
6811acb27c6SJulian Elischer 	if (node == &ng_deadnode) {
6821acb27c6SJulian Elischer 		printf ("shutdown called on deadnode\n");
6831acb27c6SJulian Elischer 		return;
6841acb27c6SJulian Elischer 	}
6851acb27c6SJulian Elischer 
6864cf49a43SJulian Elischer 	/* Add an extra reference so it doesn't go away during this */
68730400f03SJulian Elischer 	NG_NODE_REF(node);
6884cf49a43SJulian Elischer 
68930400f03SJulian Elischer 	/*
69030400f03SJulian Elischer 	 * Mark it invalid so any newcomers know not to try use it
69130400f03SJulian Elischer 	 * Also add our own mark so we can't recurse
69230400f03SJulian Elischer 	 * note that NG_INVALID does not do this as it's also set during
69330400f03SJulian Elischer 	 * creation
69430400f03SJulian Elischer 	 */
69530400f03SJulian Elischer 	node->nd_flags |= NG_INVALID|NG_CLOSING;
6964cf49a43SJulian Elischer 
6973e4084c8SJulian Elischer 	/* Notify all remaining connected nodes to disconnect */
6983e4084c8SJulian Elischer 	while ((hook = LIST_FIRST(&node->nd_hooks)) != NULL)
6993e4084c8SJulian Elischer 		ng_destroy_hook(hook);
70030400f03SJulian Elischer 
701069154d5SJulian Elischer 	/*
702069154d5SJulian Elischer 	 * Drain the input queue forceably.
70330400f03SJulian Elischer 	 * it has no hooks so what's it going to do, bleed on someone?
70430400f03SJulian Elischer 	 * Theoretically we came here from a queue entry that was added
70530400f03SJulian Elischer 	 * Just before the queue was closed, so it should be empty anyway.
706b57a7965SJulian Elischer 	 * Also removes us from worklist if needed.
707069154d5SJulian Elischer 	 */
70830400f03SJulian Elischer 	ng_flush_input_queue(&node->nd_input_queue);
709069154d5SJulian Elischer 
710069154d5SJulian Elischer 	/* Ask the type if it has anything to do in this case */
71130400f03SJulian Elischer 	if (node->nd_type && node->nd_type->shutdown) {
71230400f03SJulian Elischer 		(*node->nd_type->shutdown)(node);
71330400f03SJulian Elischer 		if (NG_NODE_IS_VALID(node)) {
71430400f03SJulian Elischer 			/*
71530400f03SJulian Elischer 			 * Well, blow me down if the node code hasn't declared
71630400f03SJulian Elischer 			 * that it doesn't want to die.
71730400f03SJulian Elischer 			 * Presumably it is a persistant node.
7181acb27c6SJulian Elischer 			 * If we REALLY want it to go away,
7191acb27c6SJulian Elischer 			 *  e.g. hardware going away,
7201acb27c6SJulian Elischer 			 * Our caller should set NG_REALLY_DIE in nd_flags.
72130400f03SJulian Elischer 			 */
7221acb27c6SJulian Elischer 			node->nd_flags &= ~(NG_INVALID|NG_CLOSING);
7231acb27c6SJulian Elischer 			NG_NODE_UNREF(node); /* Assume they still have theirs */
72430400f03SJulian Elischer 			return;
7254cf49a43SJulian Elischer 		}
7261acb27c6SJulian Elischer 	} else {				/* do the default thing */
7271acb27c6SJulian Elischer 		NG_NODE_UNREF(node);
7281acb27c6SJulian Elischer 	}
7294cf49a43SJulian Elischer 
73030400f03SJulian Elischer 	ng_unname(node); /* basically a NOP these days */
73130400f03SJulian Elischer 
73230400f03SJulian Elischer 	/*
73330400f03SJulian Elischer 	 * Remove extra reference, possibly the last
73430400f03SJulian Elischer 	 * Possible other holders of references may include
73530400f03SJulian Elischer 	 * timeout callouts, but theoretically the node's supposed to
73630400f03SJulian Elischer 	 * have cancelled them. Possibly hardware dependencies may
73730400f03SJulian Elischer 	 * force a driver to 'linger' with a reference.
73830400f03SJulian Elischer 	 */
73930400f03SJulian Elischer 	NG_NODE_UNREF(node);
7404cf49a43SJulian Elischer }
7414cf49a43SJulian Elischer 
7425951069aSJulian Elischer #ifdef	NETGRAPH_DEBUG
7434cf49a43SJulian Elischer void
7445951069aSJulian Elischer ng_ref_node(node_p node)
7455951069aSJulian Elischer {
7465951069aSJulian Elischer 	_NG_NODE_REF(node);
7475951069aSJulian Elischer }
7485951069aSJulian Elischer #endif
7495951069aSJulian Elischer 
7505951069aSJulian Elischer /*
7515951069aSJulian Elischer  * Remove a reference to the node, possibly the last.
7525951069aSJulian Elischer  * deadnode always acts as it it were the last.
7535951069aSJulian Elischer  */
7545951069aSJulian Elischer int
75530400f03SJulian Elischer ng_unref_node(node_p node)
7564cf49a43SJulian Elischer {
75730400f03SJulian Elischer 	int     v;
7586b795970SJulian Elischer 
7596b795970SJulian Elischer 	if (node == &ng_deadnode) {
7605951069aSJulian Elischer 		return (0);
7616b795970SJulian Elischer 	}
7626b795970SJulian Elischer 
76330400f03SJulian Elischer 	do {
7645951069aSJulian Elischer 		v = node->nd_refs - 1;
7655951069aSJulian Elischer 	} while (! atomic_cmpset_int(&node->nd_refs, v + 1, v));
766e8a49db2SJulian Elischer 
7675951069aSJulian Elischer 	if (v == 0) { /* we were the last */
768069154d5SJulian Elischer 
7699ed346baSBosko Milekic 		mtx_lock(&ng_nodelist_mtx);
77030400f03SJulian Elischer 		node->nd_type->refs--; /* XXX maybe should get types lock? */
77130400f03SJulian Elischer 		LIST_REMOVE(node, nd_nodes);
7729ed346baSBosko Milekic 		mtx_unlock(&ng_nodelist_mtx);
773069154d5SJulian Elischer 
7749ed346baSBosko Milekic 		mtx_lock(&ng_idhash_mtx);
77530400f03SJulian Elischer 		LIST_REMOVE(node, nd_idnodes);
7769ed346baSBosko Milekic 		mtx_unlock(&ng_idhash_mtx);
777069154d5SJulian Elischer 
77812574a02SJulian Elischer 		mtx_destroy(&node->nd_input_queue.q_mtx);
779069154d5SJulian Elischer 		NG_FREE_NODE(node);
7804cf49a43SJulian Elischer 	}
7815951069aSJulian Elischer 	return (v);
7824cf49a43SJulian Elischer }
7834cf49a43SJulian Elischer 
7844cf49a43SJulian Elischer /************************************************************************
785dc90cad9SJulian Elischer 			Node ID handling
786dc90cad9SJulian Elischer ************************************************************************/
787dc90cad9SJulian Elischer static node_p
788069154d5SJulian Elischer ng_ID2noderef(ng_ID_t ID)
789dc90cad9SJulian Elischer {
79030400f03SJulian Elischer 	node_p node;
7919ed346baSBosko Milekic 	mtx_lock(&ng_idhash_mtx);
7920f150d04SJulian Elischer 	NG_IDHASH_FIND(ID, node);
79330400f03SJulian Elischer 	if(node)
79430400f03SJulian Elischer 		NG_NODE_REF(node);
7959ed346baSBosko Milekic 	mtx_unlock(&ng_idhash_mtx);
79630400f03SJulian Elischer 	return(node);
797dc90cad9SJulian Elischer }
798dc90cad9SJulian Elischer 
799dc90cad9SJulian Elischer ng_ID_t
800dc90cad9SJulian Elischer ng_node2ID(node_p node)
801dc90cad9SJulian Elischer {
80270de87f2SJulian Elischer 	return (node ? NG_NODE_ID(node) : 0);
803dc90cad9SJulian Elischer }
804dc90cad9SJulian Elischer 
805dc90cad9SJulian Elischer /************************************************************************
8064cf49a43SJulian Elischer 			Node name handling
8074cf49a43SJulian Elischer ************************************************************************/
8084cf49a43SJulian Elischer 
8094cf49a43SJulian Elischer /*
8104cf49a43SJulian Elischer  * Assign a node a name. Once assigned, the name cannot be changed.
8114cf49a43SJulian Elischer  */
8124cf49a43SJulian Elischer int
8134cf49a43SJulian Elischer ng_name_node(node_p node, const char *name)
8144cf49a43SJulian Elischer {
8154cf49a43SJulian Elischer 	int i;
816069154d5SJulian Elischer 	node_p node2;
8174cf49a43SJulian Elischer 
8184cf49a43SJulian Elischer 	/* Check the name is valid */
8194cf49a43SJulian Elischer 	for (i = 0; i < NG_NODELEN + 1; i++) {
8204cf49a43SJulian Elischer 		if (name[i] == '\0' || name[i] == '.' || name[i] == ':')
8214cf49a43SJulian Elischer 			break;
8224cf49a43SJulian Elischer 	}
8234cf49a43SJulian Elischer 	if (i == 0 || name[i] != '\0') {
8246b795970SJulian Elischer 		TRAP_ERROR();
8254cf49a43SJulian Elischer 		return (EINVAL);
8264cf49a43SJulian Elischer 	}
827dc90cad9SJulian Elischer 	if (ng_decodeidname(name) != 0) { /* valid IDs not allowed here */
8286b795970SJulian Elischer 		TRAP_ERROR();
8294cf49a43SJulian Elischer 		return (EINVAL);
8304cf49a43SJulian Elischer 	}
8314cf49a43SJulian Elischer 
8324cf49a43SJulian Elischer 	/* Check the name isn't already being used */
833069154d5SJulian Elischer 	if ((node2 = ng_name2noderef(node, name)) != NULL) {
83430400f03SJulian Elischer 		NG_NODE_UNREF(node2);
8356b795970SJulian Elischer 		TRAP_ERROR();
8364cf49a43SJulian Elischer 		return (EADDRINUSE);
8374cf49a43SJulian Elischer 	}
8384cf49a43SJulian Elischer 
839069154d5SJulian Elischer 	/* copy it */
84030400f03SJulian Elischer 	strncpy(NG_NODE_NAME(node), name, NG_NODELEN);
8414cf49a43SJulian Elischer 
8424cf49a43SJulian Elischer 	return (0);
8434cf49a43SJulian Elischer }
8444cf49a43SJulian Elischer 
8454cf49a43SJulian Elischer /*
8464cf49a43SJulian Elischer  * Find a node by absolute name. The name should NOT end with ':'
8474cf49a43SJulian Elischer  * The name "." means "this node" and "[xxx]" means "the node
8484cf49a43SJulian Elischer  * with ID (ie, at address) xxx".
8494cf49a43SJulian Elischer  *
8504cf49a43SJulian Elischer  * Returns the node if found, else NULL.
851069154d5SJulian Elischer  * Eventually should add something faster than a sequential search.
85230400f03SJulian Elischer  * Note it aquires a reference on the node so you can be sure it's still there.
8534cf49a43SJulian Elischer  */
8544cf49a43SJulian Elischer node_p
855069154d5SJulian Elischer ng_name2noderef(node_p here, const char *name)
8564cf49a43SJulian Elischer {
857dc90cad9SJulian Elischer 	node_p node;
858dc90cad9SJulian Elischer 	ng_ID_t temp;
8594cf49a43SJulian Elischer 
8604cf49a43SJulian Elischer 	/* "." means "this node" */
861069154d5SJulian Elischer 	if (strcmp(name, ".") == 0) {
86230400f03SJulian Elischer 		NG_NODE_REF(here);
863069154d5SJulian Elischer 		return(here);
864069154d5SJulian Elischer 	}
8654cf49a43SJulian Elischer 
8664cf49a43SJulian Elischer 	/* Check for name-by-ID */
867dc90cad9SJulian Elischer 	if ((temp = ng_decodeidname(name)) != 0) {
868069154d5SJulian Elischer 		return (ng_ID2noderef(temp));
8694cf49a43SJulian Elischer 	}
8704cf49a43SJulian Elischer 
8714cf49a43SJulian Elischer 	/* Find node by name */
8729ed346baSBosko Milekic 	mtx_lock(&ng_nodelist_mtx);
87330400f03SJulian Elischer 	LIST_FOREACH(node, &ng_nodelist, nd_nodes) {
87470de87f2SJulian Elischer 		if (NG_NODE_IS_VALID(node)
87570de87f2SJulian Elischer 		&& NG_NODE_HAS_NAME(node)
87670de87f2SJulian Elischer 		&& (strcmp(NG_NODE_NAME(node), name) == 0)) {
8774cf49a43SJulian Elischer 			break;
8784cf49a43SJulian Elischer 		}
87970de87f2SJulian Elischer 	}
880069154d5SJulian Elischer 	if (node)
88130400f03SJulian Elischer 		NG_NODE_REF(node);
8829ed346baSBosko Milekic 	mtx_unlock(&ng_nodelist_mtx);
8834cf49a43SJulian Elischer 	return (node);
8844cf49a43SJulian Elischer }
8854cf49a43SJulian Elischer 
8864cf49a43SJulian Elischer /*
8879d5abbddSJens Schweikhardt  * Decode an ID name, eg. "[f03034de]". Returns 0 if the
888dc90cad9SJulian Elischer  * string is not valid, otherwise returns the value.
8894cf49a43SJulian Elischer  */
890dc90cad9SJulian Elischer static ng_ID_t
8914cf49a43SJulian Elischer ng_decodeidname(const char *name)
8924cf49a43SJulian Elischer {
8932b70adcbSArchie Cobbs 	const int len = strlen(name);
89425792ef3SArchie Cobbs 	char *eptr;
8952b70adcbSArchie Cobbs 	u_long val;
8964cf49a43SJulian Elischer 
8972b70adcbSArchie Cobbs 	/* Check for proper length, brackets, no leading junk */
89870de87f2SJulian Elischer 	if ((len < 3)
89970de87f2SJulian Elischer 	|| (name[0] != '[')
90070de87f2SJulian Elischer 	|| (name[len - 1] != ']')
90170de87f2SJulian Elischer 	|| (!isxdigit(name[1]))) {
90270de87f2SJulian Elischer 		return ((ng_ID_t)0);
90370de87f2SJulian Elischer 	}
9044cf49a43SJulian Elischer 
9052b70adcbSArchie Cobbs 	/* Decode number */
9062b70adcbSArchie Cobbs 	val = strtoul(name + 1, &eptr, 16);
90770de87f2SJulian Elischer 	if ((eptr - name != len - 1)
90870de87f2SJulian Elischer 	|| (val == ULONG_MAX)
90970de87f2SJulian Elischer 	|| (val == 0)) {
91012f035e0SJulian Elischer 		return ((ng_ID_t)0);
91170de87f2SJulian Elischer 	}
9122b70adcbSArchie Cobbs 	return (ng_ID_t)val;
9134cf49a43SJulian Elischer }
9144cf49a43SJulian Elischer 
9154cf49a43SJulian Elischer /*
9164cf49a43SJulian Elischer  * Remove a name from a node. This should only be called
9174cf49a43SJulian Elischer  * when shutting down and removing the node.
9181acb27c6SJulian Elischer  * IF we allow name changing this may be more resurected.
9194cf49a43SJulian Elischer  */
9204cf49a43SJulian Elischer void
9214cf49a43SJulian Elischer ng_unname(node_p node)
9224cf49a43SJulian Elischer {
9234cf49a43SJulian Elischer }
9244cf49a43SJulian Elischer 
9254cf49a43SJulian Elischer /************************************************************************
9264cf49a43SJulian Elischer 			Hook routines
9274cf49a43SJulian Elischer  Names are not optional. Hooks are always connected, except for a
9283e4084c8SJulian Elischer  brief moment within these routines. On invalidation or during creation
9293e4084c8SJulian Elischer  they are connected to the 'dead' hook.
9304cf49a43SJulian Elischer ************************************************************************/
9314cf49a43SJulian Elischer 
9324cf49a43SJulian Elischer /*
9334cf49a43SJulian Elischer  * Remove a hook reference
9344cf49a43SJulian Elischer  */
93530400f03SJulian Elischer void
9364cf49a43SJulian Elischer ng_unref_hook(hook_p hook)
9374cf49a43SJulian Elischer {
93830400f03SJulian Elischer 	int     v;
9396b795970SJulian Elischer 
9406b795970SJulian Elischer 	if (hook == &ng_deadhook) {
9416b795970SJulian Elischer 		return;
9426b795970SJulian Elischer 	}
94330400f03SJulian Elischer 	do {
94430400f03SJulian Elischer 		v = hook->hk_refs;
94530400f03SJulian Elischer 	} while (! atomic_cmpset_int(&hook->hk_refs, v, v - 1));
946e8a49db2SJulian Elischer 
94730400f03SJulian Elischer 	if (v == 1) { /* we were the last */
9486b795970SJulian Elischer 		if (_NG_HOOK_NODE(hook)) { /* it'll probably be ng_deadnode */
9496b795970SJulian Elischer 			_NG_NODE_UNREF((_NG_HOOK_NODE(hook)));
95030400f03SJulian Elischer 			hook->hk_node = NULL;
951069154d5SJulian Elischer 		}
952069154d5SJulian Elischer 		NG_FREE_HOOK(hook);
953069154d5SJulian Elischer 	}
9544cf49a43SJulian Elischer }
9554cf49a43SJulian Elischer 
9564cf49a43SJulian Elischer /*
9574cf49a43SJulian Elischer  * Add an unconnected hook to a node. Only used internally.
9583e4084c8SJulian Elischer  * Assumes node is locked. (XXX not yet true )
9594cf49a43SJulian Elischer  */
9604cf49a43SJulian Elischer static int
9614cf49a43SJulian Elischer ng_add_hook(node_p node, const char *name, hook_p *hookp)
9624cf49a43SJulian Elischer {
9634cf49a43SJulian Elischer 	hook_p hook;
9644cf49a43SJulian Elischer 	int error = 0;
9654cf49a43SJulian Elischer 
9664cf49a43SJulian Elischer 	/* Check that the given name is good */
9674cf49a43SJulian Elischer 	if (name == NULL) {
9686b795970SJulian Elischer 		TRAP_ERROR();
9694cf49a43SJulian Elischer 		return (EINVAL);
9704cf49a43SJulian Elischer 	}
971899e9c4eSArchie Cobbs 	if (ng_findhook(node, name) != NULL) {
9726b795970SJulian Elischer 		TRAP_ERROR();
9734cf49a43SJulian Elischer 		return (EEXIST);
9744cf49a43SJulian Elischer 	}
9754cf49a43SJulian Elischer 
9764cf49a43SJulian Elischer 	/* Allocate the hook and link it up */
97730400f03SJulian Elischer 	NG_ALLOC_HOOK(hook);
9784cf49a43SJulian Elischer 	if (hook == NULL) {
9796b795970SJulian Elischer 		TRAP_ERROR();
9804cf49a43SJulian Elischer 		return (ENOMEM);
9814cf49a43SJulian Elischer 	}
9823e4084c8SJulian Elischer 	hook->hk_refs = 1;		/* add a reference for us to return */
98330400f03SJulian Elischer 	hook->hk_flags = HK_INVALID;
9843e4084c8SJulian Elischer 	hook->hk_peer = &ng_deadhook;	/* start off this way */
98530400f03SJulian Elischer 	hook->hk_node = node;
98630400f03SJulian Elischer 	NG_NODE_REF(node);		/* each hook counts as a reference */
9874cf49a43SJulian Elischer 
9883e4084c8SJulian Elischer 	/* Set hook name */
9893e4084c8SJulian Elischer 	strncpy(NG_HOOK_NAME(hook), name, NG_HOOKLEN);
9903e4084c8SJulian Elischer 
9913e4084c8SJulian Elischer 	/*
9923e4084c8SJulian Elischer 	 * Check if the node type code has something to say about it
9933e4084c8SJulian Elischer 	 * If it fails, the unref of the hook will also unref the node.
9943e4084c8SJulian Elischer 	 */
995954c4772SJulian Elischer 	if (node->nd_type->newhook != NULL) {
996954c4772SJulian Elischer 		if ((error = (*node->nd_type->newhook)(node, hook, name))) {
99730400f03SJulian Elischer 			NG_HOOK_UNREF(hook);	/* this frees the hook */
998069154d5SJulian Elischer 			return (error);
999069154d5SJulian Elischer 		}
1000954c4772SJulian Elischer 	}
10014cf49a43SJulian Elischer 	/*
10024cf49a43SJulian Elischer 	 * The 'type' agrees so far, so go ahead and link it in.
10034cf49a43SJulian Elischer 	 * We'll ask again later when we actually connect the hooks.
10044cf49a43SJulian Elischer 	 */
100530400f03SJulian Elischer 	LIST_INSERT_HEAD(&node->nd_hooks, hook, hk_hooks);
100630400f03SJulian Elischer 	node->nd_numhooks++;
10073e4084c8SJulian Elischer 	NG_HOOK_REF(hook);	/* one for the node */
10084cf49a43SJulian Elischer 
10094cf49a43SJulian Elischer 	if (hookp)
10104cf49a43SJulian Elischer 		*hookp = hook;
10113e4084c8SJulian Elischer 	return (0);
10124cf49a43SJulian Elischer }
10134cf49a43SJulian Elischer 
10144cf49a43SJulian Elischer /*
1015899e9c4eSArchie Cobbs  * Find a hook
1016899e9c4eSArchie Cobbs  *
1017899e9c4eSArchie Cobbs  * Node types may supply their own optimized routines for finding
1018899e9c4eSArchie Cobbs  * hooks.  If none is supplied, we just do a linear search.
10193e4084c8SJulian Elischer  * XXX Possibly we should add a reference to the hook?
1020899e9c4eSArchie Cobbs  */
1021899e9c4eSArchie Cobbs hook_p
1022899e9c4eSArchie Cobbs ng_findhook(node_p node, const char *name)
1023899e9c4eSArchie Cobbs {
1024899e9c4eSArchie Cobbs 	hook_p hook;
1025899e9c4eSArchie Cobbs 
102630400f03SJulian Elischer 	if (node->nd_type->findhook != NULL)
102730400f03SJulian Elischer 		return (*node->nd_type->findhook)(node, name);
102830400f03SJulian Elischer 	LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
102970de87f2SJulian Elischer 		if (NG_HOOK_IS_VALID(hook)
1030ce5e5f99SArchie Cobbs 		&& (strcmp(NG_HOOK_NAME(hook), name) == 0))
1031899e9c4eSArchie Cobbs 			return (hook);
1032899e9c4eSArchie Cobbs 	}
1033899e9c4eSArchie Cobbs 	return (NULL);
1034899e9c4eSArchie Cobbs }
1035899e9c4eSArchie Cobbs 
1036899e9c4eSArchie Cobbs /*
10374cf49a43SJulian Elischer  * Destroy a hook
10384cf49a43SJulian Elischer  *
10394cf49a43SJulian Elischer  * As hooks are always attached, this really destroys two hooks.
10404cf49a43SJulian Elischer  * The one given, and the one attached to it. Disconnect the hooks
10413e4084c8SJulian Elischer  * from each other first. We reconnect the peer hook to the 'dead'
10423e4084c8SJulian Elischer  * hook so that it can still exist after we depart. We then
10433e4084c8SJulian Elischer  * send the peer its own destroy message. This ensures that we only
10443e4084c8SJulian Elischer  * interact with the peer's structures when it is locked processing that
10453e4084c8SJulian Elischer  * message. We hold a reference to the peer hook so we are guaranteed that
10463e4084c8SJulian Elischer  * the peer hook and node are still going to exist until
10473e4084c8SJulian Elischer  * we are finished there as the hook holds a ref on the node.
10483e4084c8SJulian Elischer  * We run this same code again on the peer hook, but that time it is already
10493e4084c8SJulian Elischer  * attached to the 'dead' hook.
10506b795970SJulian Elischer  *
10516b795970SJulian Elischer  * This routine is called at all stages of hook creation
10526b795970SJulian Elischer  * on error detection and must be able to handle any such stage.
10534cf49a43SJulian Elischer  */
10544cf49a43SJulian Elischer void
10554cf49a43SJulian Elischer ng_destroy_hook(hook_p hook)
10564cf49a43SJulian Elischer {
105730400f03SJulian Elischer 	hook_p peer = NG_HOOK_PEER(hook);
10586b795970SJulian Elischer 	node_p node = NG_HOOK_NODE(hook);
10594cf49a43SJulian Elischer 
10606b795970SJulian Elischer 	if (hook == &ng_deadhook) {	/* better safe than sorry */
10616b795970SJulian Elischer 		printf("ng_destroy_hook called on deadhook\n");
10626b795970SJulian Elischer 		return;
10636b795970SJulian Elischer 	}
106430400f03SJulian Elischer 	hook->hk_flags |= HK_INVALID;		/* as soon as possible */
10653e4084c8SJulian Elischer 	if (peer && (peer != &ng_deadhook)) {
10663e4084c8SJulian Elischer 		/*
10673e4084c8SJulian Elischer 		 * Set the peer to point to ng_deadhook
10683e4084c8SJulian Elischer 		 * from this moment on we are effectively independent it.
10693e4084c8SJulian Elischer 		 * send it an rmhook message of it's own.
10703e4084c8SJulian Elischer 		 */
10713e4084c8SJulian Elischer 		peer->hk_peer = &ng_deadhook;	/* They no longer know us */
10723e4084c8SJulian Elischer 		hook->hk_peer = &ng_deadhook;	/* Nor us, them */
10736b795970SJulian Elischer 		if (NG_HOOK_NODE(peer) == &ng_deadnode) {
10746b795970SJulian Elischer 			/*
10756b795970SJulian Elischer 			 * If it's already divorced from a node,
10766b795970SJulian Elischer 			 * just free it.
10776b795970SJulian Elischer 			 */
10786b795970SJulian Elischer 			/* nothing */
10796b795970SJulian Elischer 		} else {
10806b795970SJulian Elischer 			ng_rmhook_self(peer); 	/* Send it a surprise */
10816b795970SJulian Elischer 		}
108252fa3556SJulian Elischer 		NG_HOOK_UNREF(peer);		/* account for peer link */
108352fa3556SJulian Elischer 		NG_HOOK_UNREF(hook);		/* account for peer link */
10844cf49a43SJulian Elischer 	}
10854cf49a43SJulian Elischer 
10864cf49a43SJulian Elischer 	/*
10874cf49a43SJulian Elischer 	 * Remove the hook from the node's list to avoid possible recursion
10884cf49a43SJulian Elischer 	 * in case the disconnection results in node shutdown.
10894cf49a43SJulian Elischer 	 */
10906b795970SJulian Elischer 	if (node == &ng_deadnode) { /* happens if called from ng_con_nodes() */
10916b795970SJulian Elischer 		return;
10926b795970SJulian Elischer 	}
109330400f03SJulian Elischer 	LIST_REMOVE(hook, hk_hooks);
109430400f03SJulian Elischer 	node->nd_numhooks--;
109530400f03SJulian Elischer 	if (node->nd_type->disconnect) {
10964cf49a43SJulian Elischer 		/*
10976b795970SJulian Elischer 		 * The type handler may elect to destroy the node so don't
10986b795970SJulian Elischer 		 * trust its existance after this point. (except
10996b795970SJulian Elischer 		 * that we still hold a reference on it. (which we
11006b795970SJulian Elischer 		 * inherrited from the hook we are destroying)
11014cf49a43SJulian Elischer 		 */
110230400f03SJulian Elischer 		(*node->nd_type->disconnect) (hook);
11034cf49a43SJulian Elischer 	}
11046b795970SJulian Elischer 
11056b795970SJulian Elischer 	/*
11066b795970SJulian Elischer 	 * Note that because we will point to ng_deadnode, the original node
11076b795970SJulian Elischer 	 * is not decremented automatically so we do that manually.
11086b795970SJulian Elischer 	 */
11096b795970SJulian Elischer 	_NG_HOOK_NODE(hook) = &ng_deadnode;
11106b795970SJulian Elischer 	NG_NODE_UNREF(node);	/* We no longer point to it so adjust count */
11116b795970SJulian Elischer 	NG_HOOK_UNREF(hook);	/* Account for linkage (in list) to node */
11124cf49a43SJulian Elischer }
11134cf49a43SJulian Elischer 
11144cf49a43SJulian Elischer /*
11154cf49a43SJulian Elischer  * Take two hooks on a node and merge the connection so that the given node
11164cf49a43SJulian Elischer  * is effectively bypassed.
11174cf49a43SJulian Elischer  */
11184cf49a43SJulian Elischer int
11194cf49a43SJulian Elischer ng_bypass(hook_p hook1, hook_p hook2)
11204cf49a43SJulian Elischer {
112130400f03SJulian Elischer 	if (hook1->hk_node != hook2->hk_node) {
11226b795970SJulian Elischer 		TRAP_ERROR();
11234cf49a43SJulian Elischer 		return (EINVAL);
112430400f03SJulian Elischer 	}
112530400f03SJulian Elischer 	hook1->hk_peer->hk_peer = hook2->hk_peer;
112630400f03SJulian Elischer 	hook2->hk_peer->hk_peer = hook1->hk_peer;
11274cf49a43SJulian Elischer 
11283e4084c8SJulian Elischer 	hook1->hk_peer = &ng_deadhook;
11293e4084c8SJulian Elischer 	hook2->hk_peer = &ng_deadhook;
11303e4084c8SJulian Elischer 
11314cf49a43SJulian Elischer 	/* XXX If we ever cache methods on hooks update them as well */
11324cf49a43SJulian Elischer 	ng_destroy_hook(hook1);
11334cf49a43SJulian Elischer 	ng_destroy_hook(hook2);
11344cf49a43SJulian Elischer 	return (0);
11354cf49a43SJulian Elischer }
11364cf49a43SJulian Elischer 
11374cf49a43SJulian Elischer /*
11384cf49a43SJulian Elischer  * Install a new netgraph type
11394cf49a43SJulian Elischer  */
11404cf49a43SJulian Elischer int
11414cf49a43SJulian Elischer ng_newtype(struct ng_type *tp)
11424cf49a43SJulian Elischer {
11434cf49a43SJulian Elischer 	const size_t namelen = strlen(tp->name);
11444cf49a43SJulian Elischer 
11454cf49a43SJulian Elischer 	/* Check version and type name fields */
1146589f6ed8SJulian Elischer 	if ((tp->version != NG_ABI_VERSION)
1147589f6ed8SJulian Elischer 	|| (namelen == 0)
1148589f6ed8SJulian Elischer 	|| (namelen > NG_TYPELEN)) {
11496b795970SJulian Elischer 		TRAP_ERROR();
11504cf49a43SJulian Elischer 		return (EINVAL);
11514cf49a43SJulian Elischer 	}
11524cf49a43SJulian Elischer 
11534cf49a43SJulian Elischer 	/* Check for name collision */
11544cf49a43SJulian Elischer 	if (ng_findtype(tp->name) != NULL) {
11556b795970SJulian Elischer 		TRAP_ERROR();
11564cf49a43SJulian Elischer 		return (EEXIST);
11574cf49a43SJulian Elischer 	}
11584cf49a43SJulian Elischer 
1159069154d5SJulian Elischer 
1160069154d5SJulian Elischer 	/* Link in new type */
11619ed346baSBosko Milekic 	mtx_lock(&ng_typelist_mtx);
1162069154d5SJulian Elischer 	LIST_INSERT_HEAD(&ng_typelist, tp, types);
1163c73b94a2SJulian Elischer 	tp->refs = 1;	/* first ref is linked list */
11649ed346baSBosko Milekic 	mtx_unlock(&ng_typelist_mtx);
11654cf49a43SJulian Elischer 	return (0);
11664cf49a43SJulian Elischer }
11674cf49a43SJulian Elischer 
11684cf49a43SJulian Elischer /*
1169c31b4a53SJulian Elischer  * unlink a netgraph type
1170c31b4a53SJulian Elischer  * If no examples exist
1171c31b4a53SJulian Elischer  */
1172c31b4a53SJulian Elischer int
1173c31b4a53SJulian Elischer ng_rmtype(struct ng_type *tp)
1174c31b4a53SJulian Elischer {
1175c31b4a53SJulian Elischer 	/* Check for name collision */
1176c31b4a53SJulian Elischer 	if (tp->refs != 1) {
1177c31b4a53SJulian Elischer 		TRAP_ERROR();
1178c31b4a53SJulian Elischer 		return (EBUSY);
1179c31b4a53SJulian Elischer 	}
1180c31b4a53SJulian Elischer 
1181c31b4a53SJulian Elischer 	/* Unlink type */
1182c31b4a53SJulian Elischer 	mtx_lock(&ng_typelist_mtx);
1183c31b4a53SJulian Elischer 	LIST_REMOVE(tp, types);
1184c31b4a53SJulian Elischer 	mtx_unlock(&ng_typelist_mtx);
1185c31b4a53SJulian Elischer 	return (0);
1186c31b4a53SJulian Elischer }
1187c31b4a53SJulian Elischer 
1188c31b4a53SJulian Elischer /*
11894cf49a43SJulian Elischer  * Look for a type of the name given
11904cf49a43SJulian Elischer  */
11914cf49a43SJulian Elischer struct ng_type *
11924cf49a43SJulian Elischer ng_findtype(const char *typename)
11934cf49a43SJulian Elischer {
11944cf49a43SJulian Elischer 	struct ng_type *type;
11954cf49a43SJulian Elischer 
11969ed346baSBosko Milekic 	mtx_lock(&ng_typelist_mtx);
1197069154d5SJulian Elischer 	LIST_FOREACH(type, &ng_typelist, types) {
11984cf49a43SJulian Elischer 		if (strcmp(type->name, typename) == 0)
11994cf49a43SJulian Elischer 			break;
12004cf49a43SJulian Elischer 	}
12019ed346baSBosko Milekic 	mtx_unlock(&ng_typelist_mtx);
12024cf49a43SJulian Elischer 	return (type);
12034cf49a43SJulian Elischer }
12044cf49a43SJulian Elischer 
12054cf49a43SJulian Elischer /************************************************************************
12064cf49a43SJulian Elischer 			Composite routines
12074cf49a43SJulian Elischer ************************************************************************/
12084cf49a43SJulian Elischer /*
12096b795970SJulian Elischer  * Connect two nodes using the specified hooks, using queued functions.
12104cf49a43SJulian Elischer  */
12111acb27c6SJulian Elischer static void
12126b795970SJulian Elischer ng_con_part3(node_p node, hook_p hook, void *arg1, int arg2)
12134cf49a43SJulian Elischer {
12144cf49a43SJulian Elischer 
12156b795970SJulian Elischer 	/*
12166b795970SJulian Elischer 	 * When we run, we know that the node 'node' is locked for us.
12176b795970SJulian Elischer 	 * Our caller has a reference on the hook.
12186b795970SJulian Elischer 	 * Our caller has a reference on the node.
12196b795970SJulian Elischer 	 * (In this case our caller is ng_apply_item() ).
12206b795970SJulian Elischer 	 * The peer hook has a reference on the hook.
12211acb27c6SJulian Elischer 	 * We are all set up except for the final call to the node, and
12221acb27c6SJulian Elischer 	 * the clearing of the INVALID flag.
12236b795970SJulian Elischer 	 */
12246b795970SJulian Elischer 	if (NG_HOOK_NODE(hook) == &ng_deadnode) {
12256b795970SJulian Elischer 		/*
12266b795970SJulian Elischer 		 * The node must have been freed again since we last visited
12276b795970SJulian Elischer 		 * here. ng_destry_hook() has this effect but nothing else does.
12286b795970SJulian Elischer 		 * We should just release our references and
12296b795970SJulian Elischer 		 * free anything we can think of.
12306b795970SJulian Elischer 		 * Since we know it's been destroyed, and it's our caller
12316b795970SJulian Elischer 		 * that holds the references, just return.
12326b795970SJulian Elischer 		 */
12331acb27c6SJulian Elischer 		return ;
12346b795970SJulian Elischer 	}
12356b795970SJulian Elischer 	if (hook->hk_node->nd_type->connect) {
12361acb27c6SJulian Elischer 		if ((*hook->hk_node->nd_type->connect) (hook)) {
12376b795970SJulian Elischer 			ng_destroy_hook(hook);	/* also zaps peer */
12381acb27c6SJulian Elischer 			printf("failed in ng_con_part3()\n");
12391acb27c6SJulian Elischer 			return ;
12404cf49a43SJulian Elischer 		}
12416b795970SJulian Elischer 	}
12426b795970SJulian Elischer 	/*
12436b795970SJulian Elischer 	 *  XXX this is wrong for SMP. Possibly we need
12446b795970SJulian Elischer 	 * to separate out 'create' and 'invalid' flags.
12456b795970SJulian Elischer 	 * should only set flags on hooks we have locked under our node.
12466b795970SJulian Elischer 	 */
12476b795970SJulian Elischer 	hook->hk_flags &= ~HK_INVALID;
12481acb27c6SJulian Elischer 	return ;
12496b795970SJulian Elischer }
12506b795970SJulian Elischer 
12511acb27c6SJulian Elischer static void
12526b795970SJulian Elischer ng_con_part2(node_p node, hook_p hook, void *arg1, int arg2)
12536b795970SJulian Elischer {
12546b795970SJulian Elischer 
12556b795970SJulian Elischer 	/*
12566b795970SJulian Elischer 	 * When we run, we know that the node 'node' is locked for us.
12576b795970SJulian Elischer 	 * Our caller has a reference on the hook.
12586b795970SJulian Elischer 	 * Our caller has a reference on the node.
12596b795970SJulian Elischer 	 * (In this case our caller is ng_apply_item() ).
12606b795970SJulian Elischer 	 * The peer hook has a reference on the hook.
12616b795970SJulian Elischer 	 * our node pointer points to the 'dead' node.
12626b795970SJulian Elischer 	 * First check the hook name is unique.
12631acb27c6SJulian Elischer 	 * Should not happen because we checked before queueing this.
12646b795970SJulian Elischer 	 */
12656b795970SJulian Elischer 	if (ng_findhook(node, NG_HOOK_NAME(hook)) != NULL) {
12666b795970SJulian Elischer 		TRAP_ERROR();
12676b795970SJulian Elischer 		ng_destroy_hook(hook); /* should destroy peer too */
12681acb27c6SJulian Elischer 		printf("failed in ng_con_part2()\n");
12691acb27c6SJulian Elischer 		return ;
12706b795970SJulian Elischer 	}
12716b795970SJulian Elischer 	/*
12726b795970SJulian Elischer 	 * Check if the node type code has something to say about it
12736b795970SJulian Elischer 	 * If it fails, the unref of the hook will also unref the attached node,
12746b795970SJulian Elischer 	 * however since that node is 'ng_deadnode' this will do nothing.
12756b795970SJulian Elischer 	 * The peer hook will also be destroyed.
12766b795970SJulian Elischer 	 */
12776b795970SJulian Elischer 	if (node->nd_type->newhook != NULL) {
12781acb27c6SJulian Elischer 		if ((*node->nd_type->newhook)(node, hook, hook->hk_name)) {
12796b795970SJulian Elischer 			ng_destroy_hook(hook); /* should destroy peer too */
12801acb27c6SJulian Elischer 			printf("failed in ng_con_part2()\n");
12811acb27c6SJulian Elischer 			return ;
12826b795970SJulian Elischer 		}
12836b795970SJulian Elischer 	}
12846b795970SJulian Elischer 
12856b795970SJulian Elischer 	/*
12866b795970SJulian Elischer 	 * The 'type' agrees so far, so go ahead and link it in.
12876b795970SJulian Elischer 	 * We'll ask again later when we actually connect the hooks.
12886b795970SJulian Elischer 	 */
12896b795970SJulian Elischer 	hook->hk_node = node;		/* just overwrite ng_deadnode */
12906b795970SJulian Elischer 	NG_NODE_REF(node);		/* each hook counts as a reference */
12916b795970SJulian Elischer 	LIST_INSERT_HEAD(&node->nd_hooks, hook, hk_hooks);
12926b795970SJulian Elischer 	node->nd_numhooks++;
12936b795970SJulian Elischer 	NG_HOOK_REF(hook);	/* one for the node */
12946b795970SJulian Elischer 
12956b795970SJulian Elischer 	/*
12966b795970SJulian Elischer 	 * We now have a symetrical situation, where both hooks have been
12975951069aSJulian Elischer 	 * linked to their nodes, the newhook methods have been called
12986b795970SJulian Elischer 	 * And the references are all correct. The hooks are still marked
12996b795970SJulian Elischer 	 * as invalid, as we have not called the 'connect' methods
13006b795970SJulian Elischer 	 * yet.
13016b795970SJulian Elischer 	 * We can call the local one immediatly as we have the
13026b795970SJulian Elischer 	 * node locked, but we need to queue the remote one.
13036b795970SJulian Elischer 	 */
13046b795970SJulian Elischer 	if (hook->hk_node->nd_type->connect) {
13051acb27c6SJulian Elischer 		if ((*hook->hk_node->nd_type->connect) (hook)) {
13066b795970SJulian Elischer 			ng_destroy_hook(hook);	/* also zaps peer */
13071acb27c6SJulian Elischer 			printf("failed in ng_con_part2(A)\n");
13081acb27c6SJulian Elischer 			return ;
13096b795970SJulian Elischer 		}
13106b795970SJulian Elischer 	}
13111acb27c6SJulian Elischer 	if (ng_send_fn(hook->hk_peer->hk_node, hook->hk_peer,
13121acb27c6SJulian Elischer 			&ng_con_part3, arg1, arg2)) {
13131acb27c6SJulian Elischer 		printf("failed in ng_con_part2(B)");
13141acb27c6SJulian Elischer 		ng_destroy_hook(hook);	/* also zaps peer */
13151acb27c6SJulian Elischer 		return ;
13161acb27c6SJulian Elischer 	}
13176b795970SJulian Elischer 	hook->hk_flags &= ~HK_INVALID; /* need both to be able to work */
13181acb27c6SJulian Elischer 	return ;
13194cf49a43SJulian Elischer }
13204cf49a43SJulian Elischer 
13214cf49a43SJulian Elischer /*
13226b795970SJulian Elischer  * Connect this node with another node. We assume that this node is
13236b795970SJulian Elischer  * currently locked, as we are only called from an NGM_CONNECT message.
13244cf49a43SJulian Elischer  */
13256b795970SJulian Elischer static int
13264cf49a43SJulian Elischer ng_con_nodes(node_p node, const char *name, node_p node2, const char *name2)
13274cf49a43SJulian Elischer {
13284cf49a43SJulian Elischer 	int     error;
13294cf49a43SJulian Elischer 	hook_p  hook;
13304cf49a43SJulian Elischer 	hook_p  hook2;
13314cf49a43SJulian Elischer 
13321acb27c6SJulian Elischer 	if (ng_findhook(node2, name2) != NULL) {
13331acb27c6SJulian Elischer 		return(EEXIST);
13341acb27c6SJulian Elischer 	}
13353e4084c8SJulian Elischer 	if ((error = ng_add_hook(node, name, &hook)))  /* gives us a ref */
13364cf49a43SJulian Elischer 		return (error);
13376b795970SJulian Elischer 	/* Allocate the other hook and link it up */
13386b795970SJulian Elischer 	NG_ALLOC_HOOK(hook2);
13396b795970SJulian Elischer 	if (hook == NULL) {
13406b795970SJulian Elischer 		TRAP_ERROR();
13416b795970SJulian Elischer 		ng_destroy_hook(hook);	/* XXX check ref counts so far */
13426b795970SJulian Elischer 		NG_HOOK_UNREF(hook);	/* including our ref */
13436b795970SJulian Elischer 		return (ENOMEM);
13446b795970SJulian Elischer 	}
13456b795970SJulian Elischer 	hook2->hk_refs = 1;		/* start with a reference for us. */
13466b795970SJulian Elischer 	hook2->hk_flags = HK_INVALID;
13476b795970SJulian Elischer 	hook2->hk_peer = hook;		/* Link the two together */
13486b795970SJulian Elischer 	hook->hk_peer = hook2;
13496b795970SJulian Elischer 	NG_HOOK_REF(hook);		/* Add a ref for the peer to each*/
13506b795970SJulian Elischer 	NG_HOOK_REF(hook2);
13516b795970SJulian Elischer 	hook2->hk_node = &ng_deadnode;
13526b795970SJulian Elischer 	strncpy(NG_HOOK_NAME(hook2), name2, NG_HOOKLEN);
13536b795970SJulian Elischer 
13546b795970SJulian Elischer 	/*
13556b795970SJulian Elischer 	 * Queue the function above.
13566b795970SJulian Elischer 	 * Procesing continues in that function in the lock context of
13576b795970SJulian Elischer 	 * the other node.
13586b795970SJulian Elischer 	 */
13591acb27c6SJulian Elischer 	ng_send_fn(node2, hook2, &ng_con_part2, NULL, 0);
13606b795970SJulian Elischer 
13616b795970SJulian Elischer 	NG_HOOK_UNREF(hook);		/* Let each hook go if it wants to */
13626b795970SJulian Elischer 	NG_HOOK_UNREF(hook2);
13631acb27c6SJulian Elischer 	return (0);
13644cf49a43SJulian Elischer }
13656b795970SJulian Elischer 
13666b795970SJulian Elischer /*
13676b795970SJulian Elischer  * Make a peer and connect.
13686b795970SJulian Elischer  * We assume that the local node is locked.
13696b795970SJulian Elischer  * The new node probably doesn't need a lock until
13706b795970SJulian Elischer  * it has a hook, because it cannot really have any work until then,
13716b795970SJulian Elischer  * but we should think about it a bit more.
13726b795970SJulian Elischer  *
13736b795970SJulian Elischer  * The problem may come if the other node also fires up
13746b795970SJulian Elischer  * some hardware or a timer or some other source of activation,
13756b795970SJulian Elischer  * also it may already get a command msg via it's ID.
13766b795970SJulian Elischer  *
13776b795970SJulian Elischer  * We could use the same method as ng_con_nodes() but we'd have
13786b795970SJulian Elischer  * to add ability to remove the node when failing. (Not hard, just
13796b795970SJulian Elischer  * make arg1 point to the node to remove).
13806b795970SJulian Elischer  * Unless of course we just ignore failure to connect and leave
13816b795970SJulian Elischer  * an unconnected node?
13826b795970SJulian Elischer  */
13836b795970SJulian Elischer static int
13846b795970SJulian Elischer ng_mkpeer(node_p node, const char *name, const char *name2, char *type)
13856b795970SJulian Elischer {
13866b795970SJulian Elischer 	node_p  node2;
13876b795970SJulian Elischer 	hook_p  hook1;
13886b795970SJulian Elischer 	hook_p  hook2;
13896b795970SJulian Elischer 	int     error;
13906b795970SJulian Elischer 
13916b795970SJulian Elischer 	if ((error = ng_make_node(type, &node2))) {
13926b795970SJulian Elischer 		return (error);
13936b795970SJulian Elischer 	}
13946b795970SJulian Elischer 
13956b795970SJulian Elischer 	if ((error = ng_add_hook(node, name, &hook1))) { /* gives us a ref */
13961acb27c6SJulian Elischer 		ng_rmnode(node2, NULL, NULL, 0);
13976b795970SJulian Elischer 		return (error);
13986b795970SJulian Elischer 	}
13996b795970SJulian Elischer 
14006b795970SJulian Elischer 	if ((error = ng_add_hook(node2, name2, &hook2))) {
14011acb27c6SJulian Elischer 		ng_rmnode(node2, NULL, NULL, 0);
14026b795970SJulian Elischer 		ng_destroy_hook(hook1);
14036b795970SJulian Elischer 		NG_HOOK_UNREF(hook1);
14046b795970SJulian Elischer 		return (error);
14056b795970SJulian Elischer 	}
14066b795970SJulian Elischer 
14076b795970SJulian Elischer 	/*
14086b795970SJulian Elischer 	 * Actually link the two hooks together.
14096b795970SJulian Elischer 	 */
14106b795970SJulian Elischer 	hook1->hk_peer = hook2;
14116b795970SJulian Elischer 	hook2->hk_peer = hook1;
14126b795970SJulian Elischer 
14136b795970SJulian Elischer 	/* Each hook is referenced by the other */
14146b795970SJulian Elischer 	NG_HOOK_REF(hook1);
14156b795970SJulian Elischer 	NG_HOOK_REF(hook2);
14166b795970SJulian Elischer 
14176b795970SJulian Elischer 	/* Give each node the opportunity to veto the pending connection */
14186b795970SJulian Elischer 	if (hook1->hk_node->nd_type->connect) {
14196b795970SJulian Elischer 		error = (*hook1->hk_node->nd_type->connect) (hook1);
14206b795970SJulian Elischer 	}
14216b795970SJulian Elischer 
14226b795970SJulian Elischer 	if ((error == 0) && hook2->hk_node->nd_type->connect) {
14236b795970SJulian Elischer 		error = (*hook2->hk_node->nd_type->connect) (hook2);
14246b795970SJulian Elischer 
14256b795970SJulian Elischer 	}
14263e4084c8SJulian Elischer 
14273e4084c8SJulian Elischer 	/*
14283e4084c8SJulian Elischer 	 * drop the references we were holding on the two hooks.
14293e4084c8SJulian Elischer 	 */
14306b795970SJulian Elischer 	if (error) {
14316b795970SJulian Elischer 		ng_destroy_hook(hook2);	/* also zaps hook1 */
14321acb27c6SJulian Elischer 		ng_rmnode(node2, NULL, NULL, 0);
14336b795970SJulian Elischer 	} else {
14346b795970SJulian Elischer 		/* As a last act, allow the hooks to be used */
14356b795970SJulian Elischer 		hook1->hk_flags &= ~HK_INVALID;
14366b795970SJulian Elischer 		hook2->hk_flags &= ~HK_INVALID;
14376b795970SJulian Elischer 	}
14386b795970SJulian Elischer 	NG_HOOK_UNREF(hook1);
14393e4084c8SJulian Elischer 	NG_HOOK_UNREF(hook2);
14403e4084c8SJulian Elischer 	return (error);
14414cf49a43SJulian Elischer }
14426b795970SJulian Elischer 
1443069154d5SJulian Elischer /************************************************************************
1444069154d5SJulian Elischer 		Utility routines to send self messages
1445069154d5SJulian Elischer ************************************************************************/
1446069154d5SJulian Elischer 
14471acb27c6SJulian Elischer /* Shut this node down as soon as everyone is clear of it */
14481acb27c6SJulian Elischer /* Should add arg "immediatly" to jump the queue */
1449069154d5SJulian Elischer int
14501acb27c6SJulian Elischer ng_rmnode_self(node_p node)
1451069154d5SJulian Elischer {
14521acb27c6SJulian Elischer 	int		error;
14534cf49a43SJulian Elischer 
14541acb27c6SJulian Elischer 	if (node == &ng_deadnode)
14551acb27c6SJulian Elischer 		return (0);
14569d72a7a3SJulian Elischer 	node->nd_flags |= NG_INVALID;
14571acb27c6SJulian Elischer 	if (node->nd_flags & NG_CLOSING)
14581acb27c6SJulian Elischer 		return (0);
1459069154d5SJulian Elischer 
14601acb27c6SJulian Elischer 	error = ng_send_fn(node, NULL, &ng_rmnode, NULL, 0);
14611acb27c6SJulian Elischer 	return (error);
1462069154d5SJulian Elischer }
1463069154d5SJulian Elischer 
14641acb27c6SJulian Elischer static void
14656b795970SJulian Elischer ng_rmhook_part2(node_p node, hook_p hook, void *arg1, int arg2)
14666b795970SJulian Elischer {
14676b795970SJulian Elischer 	ng_destroy_hook(hook);
14681acb27c6SJulian Elischer 	return ;
14696b795970SJulian Elischer }
14706b795970SJulian Elischer 
1471954c4772SJulian Elischer int
1472954c4772SJulian Elischer ng_rmhook_self(hook_p hook)
1473954c4772SJulian Elischer {
14746b795970SJulian Elischer 	int		error;
1475954c4772SJulian Elischer 	node_p node = NG_HOOK_NODE(hook);
1476954c4772SJulian Elischer 
14776b795970SJulian Elischer 	if (node == &ng_deadnode)
14786b795970SJulian Elischer 		return (0);
14796b795970SJulian Elischer 
14806b795970SJulian Elischer 	error = ng_send_fn(node, hook, &ng_rmhook_part2, NULL, 0);
14816b795970SJulian Elischer 	return (error);
1482954c4772SJulian Elischer }
1483954c4772SJulian Elischer 
1484069154d5SJulian Elischer /***********************************************************************
14854cf49a43SJulian Elischer  * Parse and verify a string of the form:  <NODE:><PATH>
14864cf49a43SJulian Elischer  *
14874cf49a43SJulian Elischer  * Such a string can refer to a specific node or a specific hook
14884cf49a43SJulian Elischer  * on a specific node, depending on how you look at it. In the
14894cf49a43SJulian Elischer  * latter case, the PATH component must not end in a dot.
14904cf49a43SJulian Elischer  *
14914cf49a43SJulian Elischer  * Both <NODE:> and <PATH> are optional. The <PATH> is a string
14924cf49a43SJulian Elischer  * of hook names separated by dots. This breaks out the original
14934cf49a43SJulian Elischer  * string, setting *nodep to "NODE" (or NULL if none) and *pathp
14944cf49a43SJulian Elischer  * to "PATH" (or NULL if degenerate). Also, *hookp will point to
14954cf49a43SJulian Elischer  * the final hook component of <PATH>, if any, otherwise NULL.
14964cf49a43SJulian Elischer  *
14974cf49a43SJulian Elischer  * This returns -1 if the path is malformed. The char ** are optional.
1498069154d5SJulian Elischer  ***********************************************************************/
14994cf49a43SJulian Elischer int
15004cf49a43SJulian Elischer ng_path_parse(char *addr, char **nodep, char **pathp, char **hookp)
15014cf49a43SJulian Elischer {
15024cf49a43SJulian Elischer 	char   *node, *path, *hook;
15034cf49a43SJulian Elischer 	int     k;
15044cf49a43SJulian Elischer 
15054cf49a43SJulian Elischer 	/*
15064cf49a43SJulian Elischer 	 * Extract absolute NODE, if any
15074cf49a43SJulian Elischer 	 */
15084cf49a43SJulian Elischer 	for (path = addr; *path && *path != ':'; path++);
15094cf49a43SJulian Elischer 	if (*path) {
15104cf49a43SJulian Elischer 		node = addr;	/* Here's the NODE */
15114cf49a43SJulian Elischer 		*path++ = '\0';	/* Here's the PATH */
15124cf49a43SJulian Elischer 
15134cf49a43SJulian Elischer 		/* Node name must not be empty */
15144cf49a43SJulian Elischer 		if (!*node)
15154cf49a43SJulian Elischer 			return -1;
15164cf49a43SJulian Elischer 
15174cf49a43SJulian Elischer 		/* A name of "." is OK; otherwise '.' not allowed */
15184cf49a43SJulian Elischer 		if (strcmp(node, ".") != 0) {
15194cf49a43SJulian Elischer 			for (k = 0; node[k]; k++)
15204cf49a43SJulian Elischer 				if (node[k] == '.')
15214cf49a43SJulian Elischer 					return -1;
15224cf49a43SJulian Elischer 		}
15234cf49a43SJulian Elischer 	} else {
15244cf49a43SJulian Elischer 		node = NULL;	/* No absolute NODE */
15254cf49a43SJulian Elischer 		path = addr;	/* Here's the PATH */
15264cf49a43SJulian Elischer 	}
15274cf49a43SJulian Elischer 
15284cf49a43SJulian Elischer 	/* Snoop for illegal characters in PATH */
15294cf49a43SJulian Elischer 	for (k = 0; path[k]; k++)
15304cf49a43SJulian Elischer 		if (path[k] == ':')
15314cf49a43SJulian Elischer 			return -1;
15324cf49a43SJulian Elischer 
15334cf49a43SJulian Elischer 	/* Check for no repeated dots in PATH */
15344cf49a43SJulian Elischer 	for (k = 0; path[k]; k++)
15354cf49a43SJulian Elischer 		if (path[k] == '.' && path[k + 1] == '.')
15364cf49a43SJulian Elischer 			return -1;
15374cf49a43SJulian Elischer 
15384cf49a43SJulian Elischer 	/* Remove extra (degenerate) dots from beginning or end of PATH */
15394cf49a43SJulian Elischer 	if (path[0] == '.')
15404cf49a43SJulian Elischer 		path++;
15414cf49a43SJulian Elischer 	if (*path && path[strlen(path) - 1] == '.')
15424cf49a43SJulian Elischer 		path[strlen(path) - 1] = 0;
15434cf49a43SJulian Elischer 
15444cf49a43SJulian Elischer 	/* If PATH has a dot, then we're not talking about a hook */
15454cf49a43SJulian Elischer 	if (*path) {
15464cf49a43SJulian Elischer 		for (hook = path, k = 0; path[k]; k++)
15474cf49a43SJulian Elischer 			if (path[k] == '.') {
15484cf49a43SJulian Elischer 				hook = NULL;
15494cf49a43SJulian Elischer 				break;
15504cf49a43SJulian Elischer 			}
15514cf49a43SJulian Elischer 	} else
15524cf49a43SJulian Elischer 		path = hook = NULL;
15534cf49a43SJulian Elischer 
15544cf49a43SJulian Elischer 	/* Done */
15554cf49a43SJulian Elischer 	if (nodep)
15564cf49a43SJulian Elischer 		*nodep = node;
15574cf49a43SJulian Elischer 	if (pathp)
15584cf49a43SJulian Elischer 		*pathp = path;
15594cf49a43SJulian Elischer 	if (hookp)
15604cf49a43SJulian Elischer 		*hookp = hook;
15614cf49a43SJulian Elischer 	return (0);
15624cf49a43SJulian Elischer }
15634cf49a43SJulian Elischer 
15644cf49a43SJulian Elischer /*
15654cf49a43SJulian Elischer  * Given a path, which may be absolute or relative, and a starting node,
1566069154d5SJulian Elischer  * return the destination node.
15674cf49a43SJulian Elischer  */
15684cf49a43SJulian Elischer int
1569069154d5SJulian Elischer ng_path2noderef(node_p here, const char *address,
1570069154d5SJulian Elischer 				node_p *destp, hook_p *lasthook)
15714cf49a43SJulian Elischer {
15724cf49a43SJulian Elischer 	char    fullpath[NG_PATHLEN + 1];
15734cf49a43SJulian Elischer 	char   *nodename, *path, pbuf[2];
1574069154d5SJulian Elischer 	node_p  node, oldnode;
15754cf49a43SJulian Elischer 	char   *cp;
1576a4ec03cfSJulian Elischer 	hook_p hook = NULL;
15774cf49a43SJulian Elischer 
15784cf49a43SJulian Elischer 	/* Initialize */
157930400f03SJulian Elischer 	if (destp == NULL) {
15806b795970SJulian Elischer 		TRAP_ERROR();
15814cf49a43SJulian Elischer 		return EINVAL;
158230400f03SJulian Elischer 	}
15834cf49a43SJulian Elischer 	*destp = NULL;
15844cf49a43SJulian Elischer 
15854cf49a43SJulian Elischer 	/* Make a writable copy of address for ng_path_parse() */
15864cf49a43SJulian Elischer 	strncpy(fullpath, address, sizeof(fullpath) - 1);
15874cf49a43SJulian Elischer 	fullpath[sizeof(fullpath) - 1] = '\0';
15884cf49a43SJulian Elischer 
15894cf49a43SJulian Elischer 	/* Parse out node and sequence of hooks */
15904cf49a43SJulian Elischer 	if (ng_path_parse(fullpath, &nodename, &path, NULL) < 0) {
15916b795970SJulian Elischer 		TRAP_ERROR();
15924cf49a43SJulian Elischer 		return EINVAL;
15934cf49a43SJulian Elischer 	}
15944cf49a43SJulian Elischer 	if (path == NULL) {
15954cf49a43SJulian Elischer 		pbuf[0] = '.';	/* Needs to be writable */
15964cf49a43SJulian Elischer 		pbuf[1] = '\0';
15974cf49a43SJulian Elischer 		path = pbuf;
15984cf49a43SJulian Elischer 	}
15994cf49a43SJulian Elischer 
1600069154d5SJulian Elischer 	/*
1601069154d5SJulian Elischer 	 * For an absolute address, jump to the starting node.
1602069154d5SJulian Elischer 	 * Note that this holds a reference on the node for us.
1603069154d5SJulian Elischer 	 * Don't forget to drop the reference if we don't need it.
1604069154d5SJulian Elischer 	 */
16054cf49a43SJulian Elischer 	if (nodename) {
1606069154d5SJulian Elischer 		node = ng_name2noderef(here, nodename);
16074cf49a43SJulian Elischer 		if (node == NULL) {
16086b795970SJulian Elischer 			TRAP_ERROR();
16094cf49a43SJulian Elischer 			return (ENOENT);
16104cf49a43SJulian Elischer 		}
1611069154d5SJulian Elischer 	} else {
1612069154d5SJulian Elischer 		if (here == NULL) {
16136b795970SJulian Elischer 			TRAP_ERROR();
1614069154d5SJulian Elischer 			return (EINVAL);
1615069154d5SJulian Elischer 		}
16164cf49a43SJulian Elischer 		node = here;
161730400f03SJulian Elischer 		NG_NODE_REF(node);
1618069154d5SJulian Elischer 	}
16194cf49a43SJulian Elischer 
1620069154d5SJulian Elischer 	/*
1621069154d5SJulian Elischer 	 * Now follow the sequence of hooks
1622069154d5SJulian Elischer 	 * XXX
1623069154d5SJulian Elischer 	 * We actually cannot guarantee that the sequence
1624069154d5SJulian Elischer 	 * is not being demolished as we crawl along it
1625069154d5SJulian Elischer 	 * without extra-ordinary locking etc.
1626069154d5SJulian Elischer 	 * So this is a bit dodgy to say the least.
1627069154d5SJulian Elischer 	 * We can probably hold up some things by holding
1628069154d5SJulian Elischer 	 * the nodelist mutex for the time of this
1629069154d5SJulian Elischer 	 * crawl if we wanted.. At least that way we wouldn't have to
1630069154d5SJulian Elischer 	 * worry about the nodes dissappearing, but the hooks would still
1631069154d5SJulian Elischer 	 * be a problem.
1632069154d5SJulian Elischer 	 */
16334cf49a43SJulian Elischer 	for (cp = path; node != NULL && *cp != '\0'; ) {
16344cf49a43SJulian Elischer 		char *segment;
16354cf49a43SJulian Elischer 
16364cf49a43SJulian Elischer 		/*
16374cf49a43SJulian Elischer 		 * Break out the next path segment. Replace the dot we just
16384cf49a43SJulian Elischer 		 * found with a NUL; "cp" points to the next segment (or the
16394cf49a43SJulian Elischer 		 * NUL at the end).
16404cf49a43SJulian Elischer 		 */
16414cf49a43SJulian Elischer 		for (segment = cp; *cp != '\0'; cp++) {
16424cf49a43SJulian Elischer 			if (*cp == '.') {
16434cf49a43SJulian Elischer 				*cp++ = '\0';
16444cf49a43SJulian Elischer 				break;
16454cf49a43SJulian Elischer 			}
16464cf49a43SJulian Elischer 		}
16474cf49a43SJulian Elischer 
16484cf49a43SJulian Elischer 		/* Empty segment */
16494cf49a43SJulian Elischer 		if (*segment == '\0')
16504cf49a43SJulian Elischer 			continue;
16514cf49a43SJulian Elischer 
16524cf49a43SJulian Elischer 		/* We have a segment, so look for a hook by that name */
1653899e9c4eSArchie Cobbs 		hook = ng_findhook(node, segment);
16544cf49a43SJulian Elischer 
16554cf49a43SJulian Elischer 		/* Can't get there from here... */
16564cf49a43SJulian Elischer 		if (hook == NULL
165730400f03SJulian Elischer 		    || NG_HOOK_PEER(hook) == NULL
165830400f03SJulian Elischer 		    || NG_HOOK_NOT_VALID(hook)
165930400f03SJulian Elischer 		    || NG_HOOK_NOT_VALID(NG_HOOK_PEER(hook))) {
16606b795970SJulian Elischer 			TRAP_ERROR();
166130400f03SJulian Elischer 			NG_NODE_UNREF(node);
166230400f03SJulian Elischer #if 0
166330400f03SJulian Elischer 			printf("hooknotvalid %s %s %d %d %d %d ",
166430400f03SJulian Elischer 					path,
166530400f03SJulian Elischer 					segment,
166630400f03SJulian Elischer 					hook == NULL,
166730400f03SJulian Elischer 		     			NG_HOOK_PEER(hook) == NULL,
166830400f03SJulian Elischer 		     			NG_HOOK_NOT_VALID(hook),
166930400f03SJulian Elischer 		     			NG_HOOK_NOT_VALID(NG_HOOK_PEER(hook)));
167030400f03SJulian Elischer #endif
16714cf49a43SJulian Elischer 			return (ENOENT);
16724cf49a43SJulian Elischer 		}
16734cf49a43SJulian Elischer 
1674069154d5SJulian Elischer 		/*
1675069154d5SJulian Elischer 		 * Hop on over to the next node
1676069154d5SJulian Elischer 		 * XXX
1677069154d5SJulian Elischer 		 * Big race conditions here as hooks and nodes go away
1678069154d5SJulian Elischer 		 * *** Idea.. store an ng_ID_t in each hook and use that
1679069154d5SJulian Elischer 		 * instead of the direct hook in this crawl?
1680069154d5SJulian Elischer 		 */
1681069154d5SJulian Elischer 		oldnode = node;
168230400f03SJulian Elischer 		if ((node = NG_PEER_NODE(hook)))
168330400f03SJulian Elischer 			NG_NODE_REF(node);	/* XXX RACE */
168430400f03SJulian Elischer 		NG_NODE_UNREF(oldnode);	/* XXX another race */
168530400f03SJulian Elischer 		if (NG_NODE_NOT_VALID(node)) {
168630400f03SJulian Elischer 			NG_NODE_UNREF(node);	/* XXX more races */
1687069154d5SJulian Elischer 			node = NULL;
1688069154d5SJulian Elischer 		}
16894cf49a43SJulian Elischer 	}
16904cf49a43SJulian Elischer 
16914cf49a43SJulian Elischer 	/* If node somehow missing, fail here (probably this is not needed) */
16924cf49a43SJulian Elischer 	if (node == NULL) {
16936b795970SJulian Elischer 		TRAP_ERROR();
16944cf49a43SJulian Elischer 		return (ENXIO);
16954cf49a43SJulian Elischer 	}
16964cf49a43SJulian Elischer 
16974cf49a43SJulian Elischer 	/* Done */
16984cf49a43SJulian Elischer 	*destp = node;
16991bdebe4dSArchie Cobbs 	if (lasthook != NULL)
170030400f03SJulian Elischer 		*lasthook = (hook ? NG_HOOK_PEER(hook) : NULL);
1701069154d5SJulian Elischer 	return (0);
1702069154d5SJulian Elischer }
1703069154d5SJulian Elischer 
1704069154d5SJulian Elischer /***************************************************************\
1705069154d5SJulian Elischer * Input queue handling.
1706069154d5SJulian Elischer * All activities are submitted to the node via the input queue
1707069154d5SJulian Elischer * which implements a multiple-reader/single-writer gate.
1708069154d5SJulian Elischer * Items which cannot be handled immeditly are queued.
1709069154d5SJulian Elischer *
1710069154d5SJulian Elischer * read-write queue locking inline functions			*
1711069154d5SJulian Elischer \***************************************************************/
1712069154d5SJulian Elischer 
1713069154d5SJulian Elischer static __inline item_p ng_dequeue(struct ng_queue * ngq);
1714069154d5SJulian Elischer static __inline item_p ng_acquire_read(struct ng_queue * ngq,
1715069154d5SJulian Elischer 					item_p  item);
1716069154d5SJulian Elischer static __inline item_p ng_acquire_write(struct ng_queue * ngq,
1717069154d5SJulian Elischer 					item_p  item);
1718069154d5SJulian Elischer static __inline void	ng_leave_read(struct ng_queue * ngq);
1719069154d5SJulian Elischer static __inline void	ng_leave_write(struct ng_queue * ngq);
1720069154d5SJulian Elischer static __inline void	ng_queue_rw(struct ng_queue * ngq,
1721069154d5SJulian Elischer 					item_p  item, int rw);
1722069154d5SJulian Elischer 
1723069154d5SJulian Elischer /*
1724069154d5SJulian Elischer  * Definition of the bits fields in the ng_queue flag word.
1725069154d5SJulian Elischer  * Defined here rather than in netgraph.h because no-one should fiddle
1726069154d5SJulian Elischer  * with them.
1727069154d5SJulian Elischer  *
1728b57a7965SJulian Elischer  * The ordering here may be important! don't shuffle these.
1729069154d5SJulian Elischer  */
1730069154d5SJulian Elischer /*-
1731069154d5SJulian Elischer  Safety Barrier--------+ (adjustable to suit taste) (not used yet)
1732069154d5SJulian Elischer                        |
1733069154d5SJulian Elischer                        V
1734069154d5SJulian Elischer +-------+-------+-------+-------+-------+-------+-------+-------+
1735069154d5SJulian Elischer | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
1736b57a7965SJulian Elischer | |A|c|t|i|v|e| |R|e|a|d|e|r| |C|o|u|n|t| | | | | | | | | |R|A|W|
1737b57a7965SJulian Elischer | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |P|W|P|
1738069154d5SJulian Elischer +-------+-------+-------+-------+-------+-------+-------+-------+
1739b57a7965SJulian Elischer \___________________________ ____________________________/ | | |
1740b57a7965SJulian Elischer                             V                              | | |
1741b57a7965SJulian Elischer                   [active reader count]                    | | |
1742069154d5SJulian Elischer                                                            | | |
1743b57a7965SJulian Elischer           Read Pending ------------------------------------+ | |
1744069154d5SJulian Elischer                                                              | |
1745b57a7965SJulian Elischer           Active Writer -------------------------------------+ |
1746069154d5SJulian Elischer                                                                |
1747b57a7965SJulian Elischer           Write Pending ---------------------------------------+
1748b57a7965SJulian Elischer 
1749b57a7965SJulian Elischer 
1750069154d5SJulian Elischer */
1751b57a7965SJulian Elischer #define WRITE_PENDING	0x00000001
1752b57a7965SJulian Elischer #define WRITER_ACTIVE	0x00000002
1753b57a7965SJulian Elischer #define READ_PENDING	0x00000004
1754b57a7965SJulian Elischer #define READER_INCREMENT 0x00000008
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 */
1757b57a7965SJulian Elischer 
1758b57a7965SJulian Elischer /* Defines of more elaborate states on the queue */
1759b57a7965SJulian Elischer /* Mask of bits a read cares about */
1760b57a7965SJulian Elischer #define NGQ_RMASK	(WRITE_PENDING|WRITER_ACTIVE|READ_PENDING)
1761b57a7965SJulian Elischer 
1762b57a7965SJulian Elischer /* Mask of bits a write cares about */
1763b57a7965SJulian Elischer #define NGQ_WMASK	(NGQ_RMASK|READER_MASK)
1764b57a7965SJulian Elischer 
1765b57a7965SJulian Elischer /* tests to decide if we could get a read or write off the queue */
1766b57a7965SJulian Elischer #define CAN_GET_READ(flag)	((flag & NGQ_RMASK) == READ_PENDING)
1767b57a7965SJulian Elischer #define CAN_GET_WRITE(flag)	((flag & NGQ_WMASK) == WRITE_PENDING)
1768b57a7965SJulian Elischer 
1769b57a7965SJulian Elischer /* Is there a chance of getting ANY work off the queue? */
1770b57a7965SJulian Elischer #define CAN_GET_WORK(flag)	(CAN_GET_READ(flag) || CAN_GET_WRITE(flag))
1771b57a7965SJulian Elischer 
1772069154d5SJulian Elischer /*
1773069154d5SJulian Elischer  * Taking into account the current state of the queue and node, possibly take
1774069154d5SJulian Elischer  * the next entry off the queue and return it. Return NULL if there was
1775069154d5SJulian Elischer  * nothing we could return, either because there really was nothing there, or
1776069154d5SJulian Elischer  * because the node was in a state where it cannot yet process the next item
1777069154d5SJulian Elischer  * on the queue.
1778069154d5SJulian Elischer  *
1779069154d5SJulian Elischer  * This MUST MUST MUST be called with the mutex held.
1780069154d5SJulian Elischer  */
1781069154d5SJulian Elischer static __inline item_p
1782069154d5SJulian Elischer ng_dequeue(struct ng_queue *ngq)
1783069154d5SJulian Elischer {
1784069154d5SJulian Elischer 	item_p item;
1785069154d5SJulian Elischer 	u_int		add_arg;
1786b57a7965SJulian Elischer 
1787b57a7965SJulian Elischer 	if (CAN_GET_READ(ngq->q_flags)) {
1788069154d5SJulian Elischer 		/*
1789b57a7965SJulian Elischer 		 * Head of queue is a reader and we have no write active.
1790b57a7965SJulian Elischer 		 * We don't care how many readers are already active.
1791b57a7965SJulian Elischer 		 * Adjust the flags for the item we are about to dequeue.
1792b57a7965SJulian Elischer 		 * Add the correct increment for the reader count as well.
1793069154d5SJulian Elischer 		 */
1794b57a7965SJulian Elischer 		add_arg = (READER_INCREMENT - READ_PENDING);
1795b57a7965SJulian Elischer 	} else if (CAN_GET_WRITE(ngq->q_flags)) {
1796069154d5SJulian Elischer 		/*
1797069154d5SJulian Elischer 		 * There is a pending write, no readers and no active writer.
1798069154d5SJulian Elischer 		 * This means we can go ahead with the pending writer. Note
1799069154d5SJulian Elischer 		 * the fact that we now have a writer, ready for when we take
1800069154d5SJulian Elischer 		 * it off the queue.
1801069154d5SJulian Elischer 		 *
1802069154d5SJulian Elischer 		 * We don't need to worry about a possible collision with the
1803069154d5SJulian Elischer 		 * fasttrack reader.
1804069154d5SJulian Elischer 		 *
1805069154d5SJulian Elischer 		 * The fasttrack thread may take a long time to discover that we
1806069154d5SJulian Elischer 		 * are running so we would have an inconsistent state in the
1807069154d5SJulian Elischer 		 * flags for a while. Since we ignore the reader count
1808069154d5SJulian Elischer 		 * entirely when the WRITER_ACTIVE flag is set, this should
1809069154d5SJulian Elischer 		 * not matter (in fact it is defined that way). If it tests
1810069154d5SJulian Elischer 		 * the flag before this operation, the WRITE_PENDING flag
1811069154d5SJulian Elischer 		 * will make it fail, and if it tests it later, the
1812b57a7965SJulian Elischer 		 * WRITER_ACTIVE flag will do the same. If it is SO slow that
1813069154d5SJulian Elischer 		 * we have actually completed the operation, and neither flag
1814069154d5SJulian Elischer 		 * is set (nor the READ_PENDING) by the time that it tests
1815069154d5SJulian Elischer 		 * the flags, then it is actually ok for it to continue. If
1816069154d5SJulian Elischer 		 * it completes and we've finished and the read pending is
1817069154d5SJulian Elischer 		 * set it still fails.
1818069154d5SJulian Elischer 		 *
1819069154d5SJulian Elischer 		 * So we can just ignore it,  as long as we can ensure that the
1820069154d5SJulian Elischer 		 * transition from WRITE_PENDING state to the WRITER_ACTIVE
1821069154d5SJulian Elischer 		 * state is atomic.
1822069154d5SJulian Elischer 		 *
1823069154d5SJulian Elischer 		 * After failing, first it will be held back by the mutex, then
1824069154d5SJulian Elischer 		 * when it can proceed, it will queue its request, then it
1825069154d5SJulian Elischer 		 * would arrive at this function. Usually it will have to
1826b57a7965SJulian Elischer 		 * leave empty handed because the ACTIVE WRITER bit will be
1827069154d5SJulian Elischer 		 * set.
1828b57a7965SJulian Elischer 		 *
1829b57a7965SJulian Elischer 		 * Adjust the flags for the item we are about to dequeue
1830b57a7965SJulian Elischer 		 * and for the new active writer.
1831069154d5SJulian Elischer 		 */
1832b57a7965SJulian Elischer 		add_arg = (WRITER_ACTIVE - WRITE_PENDING);
1833069154d5SJulian Elischer 		/*
1834069154d5SJulian Elischer 		 * We want to write "active writer, no readers " Now go make
1835069154d5SJulian Elischer 		 * it true. In fact there may be a number in the readers
1836069154d5SJulian Elischer 		 * count but we know it is not true and will be fixed soon.
1837069154d5SJulian Elischer 		 * We will fix the flags for the next pending entry in a
1838069154d5SJulian Elischer 		 * moment.
1839069154d5SJulian Elischer 		 */
1840069154d5SJulian Elischer 	} else {
1841069154d5SJulian Elischer 		/*
1842069154d5SJulian Elischer 		 * We can't dequeue anything.. return and say so. Probably we
1843069154d5SJulian Elischer 		 * have a write pending and the readers count is non zero. If
1844069154d5SJulian Elischer 		 * we got here because a reader hit us just at the wrong
1845069154d5SJulian Elischer 		 * moment with the fasttrack code, and put us in a strange
1846069154d5SJulian Elischer 		 * state, then it will be through in just a moment, (as soon
1847069154d5SJulian Elischer 		 * as we release the mutex) and keep things moving.
1848b57a7965SJulian Elischer 		 * Make sure we remove ourselves from the work queue.
1849069154d5SJulian Elischer 		 */
1850b57a7965SJulian Elischer 		ng_worklist_remove(ngq->q_node);
18514cf49a43SJulian Elischer 		return (0);
18524cf49a43SJulian Elischer 	}
18534cf49a43SJulian Elischer 
18544cf49a43SJulian Elischer 	/*
1855069154d5SJulian Elischer 	 * Now we dequeue the request (whatever it may be) and correct the
1856069154d5SJulian Elischer 	 * pending flags and the next and last pointers.
18574cf49a43SJulian Elischer 	 */
1858069154d5SJulian Elischer 	item = ngq->queue;
1859069154d5SJulian Elischer 	ngq->queue = item->el_next;
1860069154d5SJulian Elischer 	if (ngq->last == &(item->el_next)) {
18614cf49a43SJulian Elischer 		/*
1862069154d5SJulian Elischer 		 * that was the last entry in the queue so set the 'last
1863069154d5SJulian Elischer 		 * pointer up correctly and make sure the pending flags are
1864069154d5SJulian Elischer 		 * clear.
18654cf49a43SJulian Elischer 		 */
1866069154d5SJulian Elischer 		ngq->last = &(ngq->queue);
1867859a4d16SJulian Elischer 		/*
1868b57a7965SJulian Elischer 		 * Whatever flag was set will be cleared and
1869b57a7965SJulian Elischer 		 * the new acive field will be set by the add as well,
1870b57a7965SJulian Elischer 		 * so we don't need to change add_arg.
1871b57a7965SJulian Elischer 		 * But we know we don't need to be on the work list.
1872859a4d16SJulian Elischer 		 */
1873b57a7965SJulian Elischer 		atomic_add_long(&ngq->q_flags, add_arg);
1874b57a7965SJulian Elischer 		ng_worklist_remove(ngq->q_node);
1875859a4d16SJulian Elischer 	} else {
1876069154d5SJulian Elischer 		/*
1877b57a7965SJulian Elischer 		 * Since there is something on the queue, note what it is
1878b57a7965SJulian Elischer 		 * in the flags word.
1879069154d5SJulian Elischer 		 */
1880b57a7965SJulian Elischer 		if ((ngq->queue->el_flags & NGQF_RW) == NGQF_READER) {
1881069154d5SJulian Elischer 			add_arg += READ_PENDING;
1882069154d5SJulian Elischer 		} else {
1883069154d5SJulian Elischer 			add_arg += WRITE_PENDING;
1884859a4d16SJulian Elischer 		}
1885069154d5SJulian Elischer 		atomic_add_long(&ngq->q_flags, add_arg);
1886069154d5SJulian Elischer 		/*
1887b57a7965SJulian Elischer 		 * If we see more doable work, make sure we are
1888b57a7965SJulian Elischer 		 * on the work queue.
1889b57a7965SJulian Elischer 		 */
1890b57a7965SJulian Elischer 		if (CAN_GET_WORK(ngq->q_flags)) {
1891b57a7965SJulian Elischer 			ng_setisr(ngq->q_node);
1892b57a7965SJulian Elischer 		}
1893b57a7965SJulian Elischer 	}
1894b57a7965SJulian Elischer 	/*
1895069154d5SJulian Elischer 	 * We have successfully cleared the old pending flag, set the new one
1896069154d5SJulian Elischer 	 * if it is needed, and incremented the appropriate active field.
1897b57a7965SJulian Elischer 	 * (all in one atomic addition.. )
1898069154d5SJulian Elischer 	 */
1899069154d5SJulian Elischer 	return (item);
1900069154d5SJulian Elischer }
1901859a4d16SJulian Elischer 
1902859a4d16SJulian Elischer /*
1903069154d5SJulian Elischer  * Queue a packet to be picked up by someone else.
1904069154d5SJulian Elischer  * We really don't care who, but we can't or don't want to hang around
1905069154d5SJulian Elischer  * to process it ourselves. We are probably an interrupt routine..
1906069154d5SJulian Elischer  * 1 = writer, 0 = reader
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);
1958b57a7965SJulian Elischer 	if ((ngq->q_flags & NGQ_RMASK) == 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 ######### */
19669ed346baSBosko Milekic 	mtx_lock_spin((&ngq->q_mtx));
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 	 */
1973b57a7965SJulian Elischer 	if ((ngq->q_flags & NGQ_RMASK) == 0) {
1974069154d5SJulian Elischer 		atomic_add_long(&ngq->q_flags, READER_INCREMENT);
19759ed346baSBosko Milekic 		mtx_unlock_spin((&ngq->q_mtx));
1976069154d5SJulian Elischer 		return (item);
1977069154d5SJulian Elischer 	}
1978069154d5SJulian Elischer 
1979069154d5SJulian Elischer 	/*
1980069154d5SJulian Elischer 	 * and queue the request for later.
1981069154d5SJulian Elischer 	 */
19826b795970SJulian Elischer 	item->el_flags |= NGQF_READER;
1983069154d5SJulian Elischer 	ng_queue_rw(ngq, item, NGQRW_R);
1984069154d5SJulian Elischer 
1985069154d5SJulian Elischer 	/*
1986069154d5SJulian Elischer 	 * Ok, so that's the item successfully queued for later. So now we
1987069154d5SJulian Elischer 	 * see if we can dequeue something to run instead.
1988069154d5SJulian Elischer 	 */
1989069154d5SJulian Elischer 	item = ng_dequeue(ngq);
19909ed346baSBosko Milekic 	mtx_unlock_spin(&(ngq->q_mtx));
1991069154d5SJulian Elischer 	return (item);
1992069154d5SJulian Elischer }
1993069154d5SJulian Elischer 
1994069154d5SJulian Elischer static __inline item_p
1995069154d5SJulian Elischer ng_acquire_write(struct ng_queue *ngq, item_p item)
1996069154d5SJulian Elischer {
1997069154d5SJulian Elischer restart:
19989ed346baSBosko Milekic 	mtx_lock_spin(&(ngq->q_mtx));
1999069154d5SJulian Elischer 	/*
2000069154d5SJulian Elischer 	 * If there are no readers, no writer, and no pending packets, then
2001069154d5SJulian Elischer 	 * we can just go ahead. In all other situations we need to queue the
2002069154d5SJulian Elischer 	 * request
2003069154d5SJulian Elischer 	 */
2004b57a7965SJulian Elischer 	if ((ngq->q_flags & NGQ_WMASK) == 0) {
2005069154d5SJulian Elischer 		atomic_add_long(&ngq->q_flags, WRITER_ACTIVE);
20069ed346baSBosko Milekic 		mtx_unlock_spin((&ngq->q_mtx));
2007069154d5SJulian Elischer 		if (ngq->q_flags & READER_MASK) {
2008069154d5SJulian Elischer 			/* Collision with fast-track reader */
2009b57a7965SJulian Elischer 			atomic_subtract_long(&ngq->q_flags, WRITER_ACTIVE);
2010069154d5SJulian Elischer 			goto restart;
2011069154d5SJulian Elischer 		}
2012069154d5SJulian Elischer 		return (item);
2013069154d5SJulian Elischer 	}
2014069154d5SJulian Elischer 
2015069154d5SJulian Elischer 	/*
2016069154d5SJulian Elischer 	 * and queue the request for later.
2017069154d5SJulian Elischer 	 */
20186b795970SJulian Elischer 	item->el_flags &= ~NGQF_RW;
2019069154d5SJulian Elischer 	ng_queue_rw(ngq, item, NGQRW_W);
2020069154d5SJulian Elischer 
2021069154d5SJulian Elischer 	/*
2022069154d5SJulian Elischer 	 * Ok, so that's the item successfully queued for later. So now we
2023069154d5SJulian Elischer 	 * see if we can dequeue something to run instead.
2024069154d5SJulian Elischer 	 */
2025069154d5SJulian Elischer 	item = ng_dequeue(ngq);
20269ed346baSBosko Milekic 	mtx_unlock_spin(&(ngq->q_mtx));
2027069154d5SJulian Elischer 	return (item);
2028069154d5SJulian Elischer }
2029069154d5SJulian Elischer 
2030069154d5SJulian Elischer static __inline void
2031069154d5SJulian Elischer ng_leave_read(struct ng_queue *ngq)
2032069154d5SJulian Elischer {
2033069154d5SJulian Elischer 	atomic_subtract_long(&ngq->q_flags, READER_INCREMENT);
2034069154d5SJulian Elischer }
2035069154d5SJulian Elischer 
2036069154d5SJulian Elischer static __inline void
2037069154d5SJulian Elischer ng_leave_write(struct ng_queue *ngq)
2038069154d5SJulian Elischer {
2039069154d5SJulian Elischer 	atomic_subtract_long(&ngq->q_flags, WRITER_ACTIVE);
2040069154d5SJulian Elischer }
2041069154d5SJulian Elischer 
2042069154d5SJulian Elischer static void
2043069154d5SJulian Elischer ng_flush_input_queue(struct ng_queue * ngq)
2044069154d5SJulian Elischer {
2045069154d5SJulian Elischer 	item_p item;
2046069154d5SJulian Elischer 	u_int		add_arg;
20479ed346baSBosko Milekic 	mtx_lock_spin(&ngq->q_mtx);
2048069154d5SJulian Elischer 	for (;;) {
2049069154d5SJulian Elischer 		/* Now take a look at what's on the queue */
2050069154d5SJulian Elischer 		if (ngq->q_flags & READ_PENDING) {
2051069154d5SJulian Elischer 			add_arg = -READ_PENDING;
2052069154d5SJulian Elischer 		} else if (ngq->q_flags & WRITE_PENDING) {
2053069154d5SJulian Elischer 			add_arg = -WRITE_PENDING;
2054069154d5SJulian Elischer 		} else {
2055069154d5SJulian Elischer 			break;
2056069154d5SJulian Elischer 		}
2057069154d5SJulian Elischer 
2058069154d5SJulian Elischer 		item = ngq->queue;
2059069154d5SJulian Elischer 		ngq->queue = item->el_next;
2060069154d5SJulian Elischer 		if (ngq->last == &(item->el_next)) {
2061069154d5SJulian Elischer 			ngq->last = &(ngq->queue);
2062069154d5SJulian Elischer 		} else {
20636b795970SJulian Elischer 			if ((ngq->queue->el_flags & NGQF_RW) == NGQF_READER) {
2064069154d5SJulian Elischer 				add_arg += READ_PENDING;
2065069154d5SJulian Elischer 			} else {
2066069154d5SJulian Elischer 				add_arg += WRITE_PENDING;
2067069154d5SJulian Elischer 			}
2068069154d5SJulian Elischer 		}
2069069154d5SJulian Elischer 		atomic_add_long(&ngq->q_flags, add_arg);
2070069154d5SJulian Elischer 
20719ed346baSBosko Milekic 		mtx_lock_spin(&ngq->q_mtx);
2072069154d5SJulian Elischer 		NG_FREE_ITEM(item);
20739ed346baSBosko Milekic 		mtx_unlock_spin(&ngq->q_mtx);
2074069154d5SJulian Elischer 	}
2075b57a7965SJulian Elischer 	/*
2076b57a7965SJulian Elischer 	 * Take us off the work queue if we are there.
2077b57a7965SJulian Elischer 	 * We definatly have no work to be done.
2078b57a7965SJulian Elischer 	 */
2079b57a7965SJulian Elischer 	ng_worklist_remove(ngq->q_node);
20809ed346baSBosko Milekic 	mtx_unlock_spin(&ngq->q_mtx);
2081069154d5SJulian Elischer }
2082069154d5SJulian Elischer 
2083069154d5SJulian Elischer /***********************************************************************
2084069154d5SJulian Elischer * Externally visible method for sending or queueing messages or data.
2085069154d5SJulian Elischer ***********************************************************************/
2086069154d5SJulian Elischer 
2087069154d5SJulian Elischer /*
20881acb27c6SJulian Elischer  * The module code should have filled out the item correctly by this stage:
2089069154d5SJulian Elischer  * Common:
2090069154d5SJulian Elischer  *    reference to destination node.
2091069154d5SJulian Elischer  *    Reference to destination rcv hook if relevant.
2092069154d5SJulian Elischer  * Data:
2093069154d5SJulian Elischer  *    pointer to mbuf
2094069154d5SJulian Elischer  *    pointer to metadata
2095069154d5SJulian Elischer  * Control_Message:
2096069154d5SJulian Elischer  *    pointer to msg.
2097069154d5SJulian Elischer  *    ID of original sender node. (return address)
20981acb27c6SJulian Elischer  * Function:
20991acb27c6SJulian Elischer  *    Function pointer
21001acb27c6SJulian Elischer  *    void * argument
21011acb27c6SJulian Elischer  *    integer argument
2102069154d5SJulian Elischer  *
2103069154d5SJulian Elischer  * The nodes have several routines and macros to help with this task:
2104069154d5SJulian Elischer  */
2105069154d5SJulian Elischer 
2106069154d5SJulian Elischer int
2107069154d5SJulian Elischer ng_snd_item(item_p item, int queue)
2108069154d5SJulian Elischer {
21091acb27c6SJulian Elischer 	hook_p hook = NGI_HOOK(item);
2110b57a7965SJulian Elischer 	node_p node = NGI_NODE(item);
2111069154d5SJulian Elischer 	int rw;
2112069154d5SJulian Elischer 	int error = 0, ierror;
2113069154d5SJulian Elischer 	item_p	oitem;
2114b57a7965SJulian Elischer 	struct ng_queue * ngq = &node->nd_input_queue;
2115069154d5SJulian Elischer 
211630400f03SJulian Elischer #ifdef	NETGRAPH_DEBUG
2117069154d5SJulian Elischer         _ngi_check(item, __FILE__, __LINE__);
2118069154d5SJulian Elischer #endif
2119069154d5SJulian Elischer 
2120069154d5SJulian Elischer 	if (item == NULL) {
21216b795970SJulian Elischer 		TRAP_ERROR();
2122069154d5SJulian Elischer 		return (EINVAL);	/* failed to get queue element */
2123069154d5SJulian Elischer 	}
2124b57a7965SJulian Elischer 	if (node == NULL) {
2125069154d5SJulian Elischer 		NG_FREE_ITEM(item);
21266b795970SJulian Elischer 		TRAP_ERROR();
2127069154d5SJulian Elischer 		return (EINVAL);	/* No address */
2128069154d5SJulian Elischer 	}
21296b795970SJulian Elischer 	switch(item->el_flags & NGQF_TYPE) {
21306b795970SJulian Elischer 	case NGQF_DATA:
2131069154d5SJulian Elischer 		/*
2132069154d5SJulian Elischer 		 * DATA MESSAGE
2133069154d5SJulian Elischer 		 * Delivered to a node via a non-optional hook.
2134069154d5SJulian Elischer 		 * Both should be present in the item even though
2135069154d5SJulian Elischer 		 * the node is derivable from the hook.
2136069154d5SJulian Elischer 		 * References are held on both by the item.
2137069154d5SJulian Elischer 		 */
2138069154d5SJulian Elischer 		CHECK_DATA_MBUF(NGI_M(item));
2139069154d5SJulian Elischer 		if (hook == NULL) {
2140069154d5SJulian Elischer 			NG_FREE_ITEM(item);
21416b795970SJulian Elischer 			TRAP_ERROR();
2142069154d5SJulian Elischer 			return(EINVAL);
2143069154d5SJulian Elischer 		}
214430400f03SJulian Elischer 		if ((NG_HOOK_NOT_VALID(hook))
214530400f03SJulian Elischer 		|| (NG_NODE_NOT_VALID(NG_HOOK_NODE(hook)))) {
2146069154d5SJulian Elischer 			NG_FREE_ITEM(item);
2147069154d5SJulian Elischer 			return (ENOTCONN);
2148859a4d16SJulian Elischer 		}
214930400f03SJulian Elischer 		if ((hook->hk_flags & HK_QUEUE)) {
2150069154d5SJulian Elischer 			queue = 1;
21514cf49a43SJulian Elischer 		}
2152069154d5SJulian Elischer 		/* By default data is a reader in the locking scheme */
2153069154d5SJulian Elischer 		item->el_flags |= NGQF_READER;
2154069154d5SJulian Elischer 		rw = NGQRW_R;
21556b795970SJulian Elischer 		break;
21566b795970SJulian Elischer 	case NGQF_MESG:
21574cf49a43SJulian Elischer 		/*
2158069154d5SJulian Elischer 		 * CONTROL MESSAGE
2159069154d5SJulian Elischer 		 * Delivered to a node.
2160069154d5SJulian Elischer 		 * Hook is optional.
2161069154d5SJulian Elischer 		 * References are held by the item on the node and
2162069154d5SJulian Elischer 		 * the hook if it is present.
21634cf49a43SJulian Elischer 		 */
216430400f03SJulian Elischer 		if (hook && (hook->hk_flags & HK_QUEUE)) {
2165069154d5SJulian Elischer 			queue = 1;
2166069154d5SJulian Elischer 		}
2167069154d5SJulian Elischer 		/* Data messages count as writers unles explicitly exempted */
2168069154d5SJulian Elischer 		if (NGI_MSG(item)->header.cmd & NGM_READONLY) {
2169069154d5SJulian Elischer 			item->el_flags |= NGQF_READER;
2170069154d5SJulian Elischer 			rw = NGQRW_R;
2171069154d5SJulian Elischer 		} else {
21726b795970SJulian Elischer 			item->el_flags &= ~NGQF_RW;
2173069154d5SJulian Elischer 			rw = NGQRW_W;
2174069154d5SJulian Elischer 		}
21756b795970SJulian Elischer 		break;
21766b795970SJulian Elischer 	case NGQF_FN:
21776b795970SJulian Elischer 		item->el_flags &= ~NGQF_RW;
21786b795970SJulian Elischer 		rw = NGQRW_W;
21796b795970SJulian Elischer 		break;
21806b795970SJulian Elischer 	default:
21816b795970SJulian Elischer 		NG_FREE_ITEM(item);
21826b795970SJulian Elischer 		TRAP_ERROR();
21836b795970SJulian Elischer 		return (EINVAL);
2184069154d5SJulian Elischer 	}
2185069154d5SJulian Elischer 	/*
2186069154d5SJulian Elischer 	 * If the node specifies single threading, force writer semantics
2187069154d5SJulian Elischer 	 * Similarly the node may say one hook always produces writers.
2188069154d5SJulian Elischer 	 * These are over-rides.
2189069154d5SJulian Elischer 	 */
2190b57a7965SJulian Elischer 	if ((node->nd_flags & NG_FORCE_WRITER)
219130400f03SJulian Elischer 	|| (hook && (hook->hk_flags & HK_FORCE_WRITER))) {
2192069154d5SJulian Elischer 			rw = NGQRW_W;
21936b795970SJulian Elischer 			item->el_flags &= ~NGQF_READER;
2194069154d5SJulian Elischer 	}
2195069154d5SJulian Elischer 	if (queue) {
2196069154d5SJulian Elischer 		/* Put it on the queue for that node*/
219730400f03SJulian Elischer #ifdef	NETGRAPH_DEBUG
2198069154d5SJulian Elischer         _ngi_check(item, __FILE__, __LINE__);
2199069154d5SJulian Elischer #endif
22009ed346baSBosko Milekic 		mtx_lock_spin(&(ngq->q_mtx));
2201069154d5SJulian Elischer 		ng_queue_rw(ngq, item, rw);
2202069154d5SJulian Elischer 		/*
2203069154d5SJulian Elischer 		 * If there are active elements then we can rely on
2204069154d5SJulian Elischer 		 * them. if not we should not rely on another packet
2205069154d5SJulian Elischer 		 * coming here by another path,
2206069154d5SJulian Elischer 		 * so it is best to put us in the netisr list.
2207b57a7965SJulian Elischer 		 * We can take the worklist lock with the node locked
2208b57a7965SJulian Elischer 		 * BUT NOT THE REVERSE!
2209069154d5SJulian Elischer 		 */
2210b57a7965SJulian Elischer 		if (CAN_GET_WORK(ngq->q_flags)) {
2211b57a7965SJulian Elischer 			ng_setisr(node);
2212069154d5SJulian Elischer 		}
22139ed346baSBosko Milekic 		mtx_unlock_spin(&(ngq->q_mtx));
2214069154d5SJulian Elischer 		return (0);
2215069154d5SJulian Elischer 	}
2216069154d5SJulian Elischer 	/*
2217069154d5SJulian Elischer 	 * Take a queue item and a node and see if we can apply the item to
2218069154d5SJulian Elischer 	 * the node. We may end up getting a different item to apply instead.
2219069154d5SJulian Elischer 	 * Will allow for a piggyback reply only in the case where
2220069154d5SJulian Elischer 	 * there is no queueing.
2221069154d5SJulian Elischer 	 */
2222069154d5SJulian Elischer 
2223069154d5SJulian Elischer 	oitem = item;
2224069154d5SJulian Elischer 	/*
2225069154d5SJulian Elischer 	 * We already decided how we will be queueud or treated.
2226069154d5SJulian Elischer 	 * Try get the appropriate operating permission.
2227069154d5SJulian Elischer 	 */
2228069154d5SJulian Elischer  	if (rw == NGQRW_R) {
2229069154d5SJulian Elischer 		item = ng_acquire_read(ngq, item);
2230069154d5SJulian Elischer 	} else {
2231069154d5SJulian Elischer 		item = ng_acquire_write(ngq, item);
22324cf49a43SJulian Elischer 	}
22334cf49a43SJulian Elischer 
22344cf49a43SJulian Elischer 	/*
2235069154d5SJulian Elischer 	 * May have come back with a different item.
2236069154d5SJulian Elischer 	 * or maybe none at all. The one we started with will
2237069154d5SJulian Elischer 	 * have been queued in thises cases.
2238069154d5SJulian Elischer 	 */
2239069154d5SJulian Elischer 	if (item == NULL) {
2240069154d5SJulian Elischer 		return (0);
2241069154d5SJulian Elischer 	}
2242069154d5SJulian Elischer 
224330400f03SJulian Elischer #ifdef	NETGRAPH_DEBUG
2244069154d5SJulian Elischer         _ngi_check(item, __FILE__, __LINE__);
2245069154d5SJulian Elischer #endif
22465951069aSJulian Elischer 	/*
22475951069aSJulian Elischer 	 * Take over the reference frm the item.
22485951069aSJulian Elischer 	 * Hold it until the called function returns.
22495951069aSJulian Elischer 	 */
22505951069aSJulian Elischer 	NGI_GET_NODE(item, node); /* zaps stored node */
22515951069aSJulian Elischer 
22525951069aSJulian Elischer 	ierror = ng_apply_item(node, item); /* drops r/w lock when done */
2253069154d5SJulian Elischer 
2254069154d5SJulian Elischer 	/* only return an error if it was our initial item.. (compat hack) */
2255069154d5SJulian Elischer 	if (oitem == item) {
2256069154d5SJulian Elischer 		error = ierror;
2257069154d5SJulian Elischer 	}
2258069154d5SJulian Elischer 
2259069154d5SJulian Elischer 	/*
22605951069aSJulian Elischer 	 * If the node goes away when we remove the reference,
2261376958b4SBrian Somers 	 * whatever we just did caused it.. whatever we do, DO NOT
22625951069aSJulian Elischer 	 * access the node again!
22635951069aSJulian Elischer 	 */
22645951069aSJulian Elischer 	if (NG_NODE_UNREF(node) == 0) {
22655951069aSJulian Elischer 		return (error);
22665951069aSJulian Elischer 	}
22675951069aSJulian Elischer 
22685951069aSJulian Elischer 	/*
2269069154d5SJulian Elischer 	 * Now we've handled the packet we brought, (or a friend of it) let's
2270069154d5SJulian Elischer 	 * look for any other packets that may have been queued up. We hold
2271069154d5SJulian Elischer 	 * no locks, so if someone puts something in the queue after
2272069154d5SJulian Elischer 	 * we check that it is empty, it is their problem
2273069154d5SJulian Elischer 	 * to ensure it is processed. If we have the netisr thread cme in here
2274069154d5SJulian Elischer 	 * while we still say we have stuff to do, we may get a boost
2275069154d5SJulian Elischer 	 * in SMP systems. :-)
2276069154d5SJulian Elischer 	 */
2277069154d5SJulian Elischer 	for (;;) {
2278069154d5SJulian Elischer 		/*
2279069154d5SJulian Elischer 		 * dequeue acquires and adjusts the input_queue as it dequeues
2280069154d5SJulian Elischer 		 * packets. It acquires the rw lock as needed.
2281069154d5SJulian Elischer 		 */
22829ed346baSBosko Milekic 		mtx_lock_spin(&ngq->q_mtx);
2283b57a7965SJulian Elischer 		item = ng_dequeue(ngq); /* fixes worklist too*/
2284069154d5SJulian Elischer 		if (!item) {
22859ed346baSBosko Milekic 			mtx_unlock_spin(&ngq->q_mtx);
22861acb27c6SJulian Elischer 			return (error);
2287069154d5SJulian Elischer 		}
22889ed346baSBosko Milekic 		mtx_unlock_spin(&ngq->q_mtx);
2289069154d5SJulian Elischer 
2290069154d5SJulian Elischer 		/*
22915951069aSJulian Elischer 		 * Take over the reference frm the item.
22925951069aSJulian Elischer 		 * Hold it until the called function returns.
22935951069aSJulian Elischer 		 */
22945951069aSJulian Elischer 
22955951069aSJulian Elischer 		NGI_GET_NODE(item, node); /* zaps stored node */
22965951069aSJulian Elischer 
22975951069aSJulian Elischer 		/*
2298069154d5SJulian Elischer 		 * We have the appropriate lock, so run the item.
2299069154d5SJulian Elischer 		 * When finished it will drop the lock accordingly
2300069154d5SJulian Elischer 		 */
23015951069aSJulian Elischer 		ierror = ng_apply_item(node, item);
2302069154d5SJulian Elischer 
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 		}
23105951069aSJulian Elischer 
23115951069aSJulian Elischer 		/*
23125951069aSJulian Elischer 		 * If the node goes away when we remove the reference,
2313376958b4SBrian Somers 		 * whatever we just did caused it.. whatever we do, DO NOT
23145951069aSJulian Elischer 		 * access the node again!
23155951069aSJulian Elischer 		 */
23165951069aSJulian Elischer 		if (NG_NODE_UNREF(node) == 0) {
23175951069aSJulian Elischer 			break;
23185951069aSJulian Elischer 		}
2319069154d5SJulian Elischer 	}
23201acb27c6SJulian Elischer 	return (error);
2321069154d5SJulian Elischer }
2322069154d5SJulian Elischer 
2323069154d5SJulian Elischer /*
2324069154d5SJulian Elischer  * We have an item that was possibly queued somewhere.
2325069154d5SJulian Elischer  * It should contain all the information needed
2326069154d5SJulian Elischer  * to run it on the appropriate node/hook.
23274cf49a43SJulian Elischer  */
23284cf49a43SJulian Elischer static int
23295951069aSJulian Elischer ng_apply_item(node_p node, item_p item)
2330069154d5SJulian Elischer {
2331069154d5SJulian Elischer 	hook_p  hook;
23326b795970SJulian Elischer 	int	was_reader = ((item->el_flags & NGQF_RW));
2333069154d5SJulian Elischer 	int	error = 0;
2334069154d5SJulian Elischer 	ng_rcvdata_t *rcvdata;
2335c4b5eea4SJulian Elischer 	ng_rcvmsg_t *rcvmsg;
2336069154d5SJulian Elischer 
23371acb27c6SJulian Elischer 	NGI_GET_HOOK(item, hook); /* clears stored hook */
233830400f03SJulian Elischer #ifdef	NETGRAPH_DEBUG
2339069154d5SJulian Elischer         _ngi_check(item, __FILE__, __LINE__);
2340069154d5SJulian Elischer #endif
23416b795970SJulian Elischer 	switch (item->el_flags & NGQF_TYPE) {
2342069154d5SJulian Elischer 	case NGQF_DATA:
2343069154d5SJulian Elischer 		/*
2344069154d5SJulian Elischer 		 * Check things are still ok as when we were queued.
2345069154d5SJulian Elischer 		 */
2346069154d5SJulian Elischer 		if ((hook == NULL)
234730400f03SJulian Elischer 		|| NG_HOOK_NOT_VALID(hook)
2348c4b5eea4SJulian Elischer 		|| NG_NODE_NOT_VALID(node) ) {
2349069154d5SJulian Elischer 			error = EIO;
2350069154d5SJulian Elischer 			NG_FREE_ITEM(item);
2351c4b5eea4SJulian Elischer 			break;
2352069154d5SJulian Elischer 		}
2353c4b5eea4SJulian Elischer 		/*
2354c4b5eea4SJulian Elischer 		 * If no receive method, just silently drop it.
2355c4b5eea4SJulian Elischer 		 * Give preference to the hook over-ride method
2356c4b5eea4SJulian Elischer 		 */
2357c4b5eea4SJulian Elischer 		if ((!(rcvdata = hook->hk_rcvdata))
2358c4b5eea4SJulian Elischer 		&& (!(rcvdata = NG_HOOK_NODE(hook)->nd_type->rcvdata))) {
2359c4b5eea4SJulian Elischer 			error = 0;
2360c4b5eea4SJulian Elischer 			NG_FREE_ITEM(item);
2361c4b5eea4SJulian Elischer 			break;
2362c4b5eea4SJulian Elischer 		}
2363c4b5eea4SJulian Elischer 		error = (*rcvdata)(hook, item);
2364069154d5SJulian Elischer 		break;
2365069154d5SJulian Elischer 	case NGQF_MESG:
2366069154d5SJulian Elischer 		if (hook) {
236730400f03SJulian Elischer 			if (NG_HOOK_NOT_VALID(hook)) {
2368069154d5SJulian Elischer 				/*
23691acb27c6SJulian Elischer 				 * The hook has been zapped then we can't
23701acb27c6SJulian Elischer 				 * use it. Immediatly drop its reference.
2371069154d5SJulian Elischer 				 * The message may not need it.
2372069154d5SJulian Elischer 				 */
237330400f03SJulian Elischer 				NG_HOOK_UNREF(hook);
2374069154d5SJulian Elischer 				hook = NULL;
2375069154d5SJulian Elischer 			}
2376069154d5SJulian Elischer 		}
2377069154d5SJulian Elischer 		/*
2378069154d5SJulian Elischer 		 * Similarly, if the node is a zombie there is
2379069154d5SJulian Elischer 		 * nothing we can do with it, drop everything.
2380069154d5SJulian Elischer 		 */
238130400f03SJulian Elischer 		if (NG_NODE_NOT_VALID(node)) {
23826b795970SJulian Elischer 			TRAP_ERROR();
2383069154d5SJulian Elischer 			error = EINVAL;
2384069154d5SJulian Elischer 			NG_FREE_ITEM(item);
2385069154d5SJulian Elischer 		} else {
2386069154d5SJulian Elischer 			/*
2387069154d5SJulian Elischer 			 * Call the appropriate message handler for the object.
2388069154d5SJulian Elischer 			 * It is up to the message handler to free the message.
2389069154d5SJulian Elischer 			 * If it's a generic message, handle it generically,
2390069154d5SJulian Elischer 			 * otherwise call the type's message handler
2391069154d5SJulian Elischer 			 * (if it exists)
2392069154d5SJulian Elischer 			 * XXX (race). Remember that a queued message may
2393069154d5SJulian Elischer 			 * reference a node or hook that has just been
2394069154d5SJulian Elischer 			 * invalidated. It will exist as the queue code
2395069154d5SJulian Elischer 			 * is holding a reference, but..
2396069154d5SJulian Elischer 			 */
2397069154d5SJulian Elischer 
2398069154d5SJulian Elischer 			struct ng_mesg *msg = NGI_MSG(item);
2399069154d5SJulian Elischer 
2400c4b5eea4SJulian Elischer 			/*
2401c4b5eea4SJulian Elischer 			 * check if the generic handler owns it.
2402c4b5eea4SJulian Elischer 			 */
2403069154d5SJulian Elischer 			if ((msg->header.typecookie == NGM_GENERIC_COOKIE)
2404069154d5SJulian Elischer 			&& ((msg->header.flags & NGF_RESP) == 0)) {
2405069154d5SJulian Elischer 				error = ng_generic_msg(node, item, hook);
2406c4b5eea4SJulian Elischer 				break;
2407c4b5eea4SJulian Elischer 			}
2408c4b5eea4SJulian Elischer 			/*
2409c4b5eea4SJulian Elischer 			 * Now see if there is a handler (hook or node specific)
2410c4b5eea4SJulian Elischer 			 * in the target node. If none, silently discard.
2411c4b5eea4SJulian Elischer 			 */
2412c4b5eea4SJulian Elischer 			if (((!hook) || (!(rcvmsg = hook->hk_rcvmsg)))
2413c4b5eea4SJulian Elischer 			&& (!(rcvmsg = node->nd_type->rcvmsg))) {
24146b795970SJulian Elischer 				TRAP_ERROR();
2415c4b5eea4SJulian Elischer 				error = 0;
2416069154d5SJulian Elischer 				NG_FREE_ITEM(item);
2417c4b5eea4SJulian Elischer 				break;
2418069154d5SJulian Elischer 			}
2419c4b5eea4SJulian Elischer 			error = (*rcvmsg)(node, item, hook);
2420069154d5SJulian Elischer 		}
2421069154d5SJulian Elischer 		break;
24226b795970SJulian Elischer 	case NGQF_FN:
24236b795970SJulian Elischer 		/*
24246b795970SJulian Elischer 		 *  We have to implicitly trust the hook,
24256b795970SJulian Elischer 		 * as some of these are used for system purposes
24261acb27c6SJulian Elischer 		 * where the hook is invalid. In the case of
24271acb27c6SJulian Elischer 		 * the shutdown message we allow it to hit
24281acb27c6SJulian Elischer 		 * even if the node is invalid.
24296b795970SJulian Elischer 		 */
24301acb27c6SJulian Elischer 		if ((NG_NODE_NOT_VALID(node))
24311acb27c6SJulian Elischer 		&& (NGI_FN(item) != &ng_rmnode)) {
24326b795970SJulian Elischer 			TRAP_ERROR();
24336b795970SJulian Elischer 			error = EINVAL;
24346b795970SJulian Elischer 			break;
24356b795970SJulian Elischer 		}
24366b795970SJulian Elischer 		(*NGI_FN(item))(node, hook, NGI_ARG1(item), NGI_ARG2(item));
24376b795970SJulian Elischer 		NG_FREE_ITEM(item);
24386b795970SJulian Elischer 		break;
24396b795970SJulian Elischer 
2440069154d5SJulian Elischer 	}
2441069154d5SJulian Elischer 	/*
2442069154d5SJulian Elischer 	 * We held references on some of the resources
2443069154d5SJulian Elischer 	 * that we took from the item. Now that we have
2444069154d5SJulian Elischer 	 * finished doing everything, drop those references.
2445069154d5SJulian Elischer 	 */
2446069154d5SJulian Elischer 	if (hook) {
244730400f03SJulian Elischer 		NG_HOOK_UNREF(hook);
2448069154d5SJulian Elischer 	}
2449069154d5SJulian Elischer 
2450069154d5SJulian Elischer 	if (was_reader) {
245130400f03SJulian Elischer 		ng_leave_read(&node->nd_input_queue);
2452069154d5SJulian Elischer 	} else {
245330400f03SJulian Elischer 		ng_leave_write(&node->nd_input_queue);
2454069154d5SJulian Elischer 	}
2455069154d5SJulian Elischer 	return (error);
2456069154d5SJulian Elischer }
2457069154d5SJulian Elischer 
2458069154d5SJulian Elischer /***********************************************************************
2459069154d5SJulian Elischer  * Implement the 'generic' control messages
2460069154d5SJulian Elischer  ***********************************************************************/
2461069154d5SJulian Elischer static int
2462069154d5SJulian Elischer ng_generic_msg(node_p here, item_p item, hook_p lasthook)
24634cf49a43SJulian Elischer {
24644cf49a43SJulian Elischer 	int error = 0;
2465069154d5SJulian Elischer 	struct ng_mesg *msg;
2466069154d5SJulian Elischer 	struct ng_mesg *resp = NULL;
24674cf49a43SJulian Elischer 
2468069154d5SJulian Elischer 	NGI_GET_MSG(item, msg);
24694cf49a43SJulian Elischer 	if (msg->header.typecookie != NGM_GENERIC_COOKIE) {
24706b795970SJulian Elischer 		TRAP_ERROR();
2471069154d5SJulian Elischer 		error = EINVAL;
2472069154d5SJulian Elischer 		goto out;
24734cf49a43SJulian Elischer 	}
24744cf49a43SJulian Elischer 	switch (msg->header.cmd) {
24754cf49a43SJulian Elischer 	case NGM_SHUTDOWN:
24761acb27c6SJulian Elischer 		ng_rmnode(here, NULL, NULL, 0);
24774cf49a43SJulian Elischer 		break;
24784cf49a43SJulian Elischer 	case NGM_MKPEER:
24794cf49a43SJulian Elischer 	    {
24804cf49a43SJulian Elischer 		struct ngm_mkpeer *const mkp = (struct ngm_mkpeer *) msg->data;
24814cf49a43SJulian Elischer 
24824cf49a43SJulian Elischer 		if (msg->header.arglen != sizeof(*mkp)) {
24836b795970SJulian Elischer 			TRAP_ERROR();
2484069154d5SJulian Elischer 			error = EINVAL;
2485069154d5SJulian Elischer 			break;
24864cf49a43SJulian Elischer 		}
24874cf49a43SJulian Elischer 		mkp->type[sizeof(mkp->type) - 1] = '\0';
24884cf49a43SJulian Elischer 		mkp->ourhook[sizeof(mkp->ourhook) - 1] = '\0';
24894cf49a43SJulian Elischer 		mkp->peerhook[sizeof(mkp->peerhook) - 1] = '\0';
24904cf49a43SJulian Elischer 		error = ng_mkpeer(here, mkp->ourhook, mkp->peerhook, mkp->type);
24914cf49a43SJulian Elischer 		break;
24924cf49a43SJulian Elischer 	    }
24934cf49a43SJulian Elischer 	case NGM_CONNECT:
24944cf49a43SJulian Elischer 	    {
24954cf49a43SJulian Elischer 		struct ngm_connect *const con =
24964cf49a43SJulian Elischer 			(struct ngm_connect *) msg->data;
24974cf49a43SJulian Elischer 		node_p node2;
24984cf49a43SJulian Elischer 
24994cf49a43SJulian Elischer 		if (msg->header.arglen != sizeof(*con)) {
25006b795970SJulian Elischer 			TRAP_ERROR();
2501069154d5SJulian Elischer 			error = EINVAL;
2502069154d5SJulian Elischer 			break;
25034cf49a43SJulian Elischer 		}
25044cf49a43SJulian Elischer 		con->path[sizeof(con->path) - 1] = '\0';
25054cf49a43SJulian Elischer 		con->ourhook[sizeof(con->ourhook) - 1] = '\0';
25064cf49a43SJulian Elischer 		con->peerhook[sizeof(con->peerhook) - 1] = '\0';
2507069154d5SJulian Elischer 		/* Don't forget we get a reference.. */
2508069154d5SJulian Elischer 		error = ng_path2noderef(here, con->path, &node2, NULL);
25094cf49a43SJulian Elischer 		if (error)
25104cf49a43SJulian Elischer 			break;
25114cf49a43SJulian Elischer 		error = ng_con_nodes(here, con->ourhook, node2, con->peerhook);
251230400f03SJulian Elischer 		NG_NODE_UNREF(node2);
25134cf49a43SJulian Elischer 		break;
25144cf49a43SJulian Elischer 	    }
25154cf49a43SJulian Elischer 	case NGM_NAME:
25164cf49a43SJulian Elischer 	    {
25174cf49a43SJulian Elischer 		struct ngm_name *const nam = (struct ngm_name *) msg->data;
25184cf49a43SJulian Elischer 
25194cf49a43SJulian Elischer 		if (msg->header.arglen != sizeof(*nam)) {
25206b795970SJulian Elischer 			TRAP_ERROR();
2521069154d5SJulian Elischer 			error = EINVAL;
2522069154d5SJulian Elischer 			break;
25234cf49a43SJulian Elischer 		}
25244cf49a43SJulian Elischer 		nam->name[sizeof(nam->name) - 1] = '\0';
25254cf49a43SJulian Elischer 		error = ng_name_node(here, nam->name);
25264cf49a43SJulian Elischer 		break;
25274cf49a43SJulian Elischer 	    }
25284cf49a43SJulian Elischer 	case NGM_RMHOOK:
25294cf49a43SJulian Elischer 	    {
25304cf49a43SJulian Elischer 		struct ngm_rmhook *const rmh = (struct ngm_rmhook *) msg->data;
25314cf49a43SJulian Elischer 		hook_p hook;
25324cf49a43SJulian Elischer 
25334cf49a43SJulian Elischer 		if (msg->header.arglen != sizeof(*rmh)) {
25346b795970SJulian Elischer 			TRAP_ERROR();
2535069154d5SJulian Elischer 			error = EINVAL;
2536069154d5SJulian Elischer 			break;
25374cf49a43SJulian Elischer 		}
25384cf49a43SJulian Elischer 		rmh->ourhook[sizeof(rmh->ourhook) - 1] = '\0';
2539899e9c4eSArchie Cobbs 		if ((hook = ng_findhook(here, rmh->ourhook)) != NULL)
25404cf49a43SJulian Elischer 			ng_destroy_hook(hook);
25414cf49a43SJulian Elischer 		break;
25424cf49a43SJulian Elischer 	    }
25434cf49a43SJulian Elischer 	case NGM_NODEINFO:
25444cf49a43SJulian Elischer 	    {
25454cf49a43SJulian Elischer 		struct nodeinfo *ni;
25464cf49a43SJulian Elischer 
2547069154d5SJulian Elischer 		NG_MKRESPONSE(resp, msg, sizeof(*ni), M_NOWAIT);
25484cf49a43SJulian Elischer 		if (resp == NULL) {
25494cf49a43SJulian Elischer 			error = ENOMEM;
25504cf49a43SJulian Elischer 			break;
25514cf49a43SJulian Elischer 		}
25524cf49a43SJulian Elischer 
25534cf49a43SJulian Elischer 		/* Fill in node info */
2554069154d5SJulian Elischer 		ni = (struct nodeinfo *) resp->data;
255530400f03SJulian Elischer 		if (NG_NODE_HAS_NAME(here))
255630400f03SJulian Elischer 			strncpy(ni->name, NG_NODE_NAME(here), NG_NODELEN);
255730400f03SJulian Elischer 		strncpy(ni->type, here->nd_type->name, NG_TYPELEN);
2558dc90cad9SJulian Elischer 		ni->id = ng_node2ID(here);
255930400f03SJulian Elischer 		ni->hooks = here->nd_numhooks;
25604cf49a43SJulian Elischer 		break;
25614cf49a43SJulian Elischer 	    }
25624cf49a43SJulian Elischer 	case NGM_LISTHOOKS:
25634cf49a43SJulian Elischer 	    {
256430400f03SJulian Elischer 		const int nhooks = here->nd_numhooks;
25654cf49a43SJulian Elischer 		struct hooklist *hl;
25664cf49a43SJulian Elischer 		struct nodeinfo *ni;
25674cf49a43SJulian Elischer 		hook_p hook;
25684cf49a43SJulian Elischer 
25694cf49a43SJulian Elischer 		/* Get response struct */
2570069154d5SJulian Elischer 		NG_MKRESPONSE(resp, msg, sizeof(*hl)
25714cf49a43SJulian Elischer 		    + (nhooks * sizeof(struct linkinfo)), M_NOWAIT);
2572069154d5SJulian Elischer 		if (resp == NULL) {
25734cf49a43SJulian Elischer 			error = ENOMEM;
25744cf49a43SJulian Elischer 			break;
25754cf49a43SJulian Elischer 		}
2576069154d5SJulian Elischer 		hl = (struct hooklist *) resp->data;
25774cf49a43SJulian Elischer 		ni = &hl->nodeinfo;
25784cf49a43SJulian Elischer 
25794cf49a43SJulian Elischer 		/* Fill in node info */
258030400f03SJulian Elischer 		if (NG_NODE_HAS_NAME(here))
258130400f03SJulian Elischer 			strncpy(ni->name, NG_NODE_NAME(here), NG_NODELEN);
258230400f03SJulian Elischer 		strncpy(ni->type, here->nd_type->name, NG_TYPELEN);
2583dc90cad9SJulian Elischer 		ni->id = ng_node2ID(here);
25844cf49a43SJulian Elischer 
25854cf49a43SJulian Elischer 		/* Cycle through the linked list of hooks */
25864cf49a43SJulian Elischer 		ni->hooks = 0;
258730400f03SJulian Elischer 		LIST_FOREACH(hook, &here->nd_hooks, hk_hooks) {
25884cf49a43SJulian Elischer 			struct linkinfo *const link = &hl->link[ni->hooks];
25894cf49a43SJulian Elischer 
25904cf49a43SJulian Elischer 			if (ni->hooks >= nhooks) {
25914cf49a43SJulian Elischer 				log(LOG_ERR, "%s: number of %s changed\n",
25926e551fb6SDavid E. O'Brien 				    __func__, "hooks");
25934cf49a43SJulian Elischer 				break;
25944cf49a43SJulian Elischer 			}
259530400f03SJulian Elischer 			if (NG_HOOK_NOT_VALID(hook))
25964cf49a43SJulian Elischer 				continue;
259730400f03SJulian Elischer 			strncpy(link->ourhook, NG_HOOK_NAME(hook), NG_HOOKLEN);
259830400f03SJulian Elischer 			strncpy(link->peerhook,
259930400f03SJulian Elischer 				NG_PEER_HOOK_NAME(hook), NG_HOOKLEN);
260030400f03SJulian Elischer 			if (NG_PEER_NODE_NAME(hook)[0] != '\0')
26014cf49a43SJulian Elischer 				strncpy(link->nodeinfo.name,
260230400f03SJulian Elischer 				    NG_PEER_NODE_NAME(hook), NG_NODELEN);
26034cf49a43SJulian Elischer 			strncpy(link->nodeinfo.type,
260430400f03SJulian Elischer 			   NG_PEER_NODE(hook)->nd_type->name, NG_TYPELEN);
260530400f03SJulian Elischer 			link->nodeinfo.id = ng_node2ID(NG_PEER_NODE(hook));
260630400f03SJulian Elischer 			link->nodeinfo.hooks = NG_PEER_NODE(hook)->nd_numhooks;
26074cf49a43SJulian Elischer 			ni->hooks++;
26084cf49a43SJulian Elischer 		}
26094cf49a43SJulian Elischer 		break;
26104cf49a43SJulian Elischer 	    }
26114cf49a43SJulian Elischer 
26124cf49a43SJulian Elischer 	case NGM_LISTNAMES:
26134cf49a43SJulian Elischer 	case NGM_LISTNODES:
26144cf49a43SJulian Elischer 	    {
26154cf49a43SJulian Elischer 		const int unnamed = (msg->header.cmd == NGM_LISTNODES);
26164cf49a43SJulian Elischer 		struct namelist *nl;
26174cf49a43SJulian Elischer 		node_p node;
26184cf49a43SJulian Elischer 		int num = 0;
26194cf49a43SJulian Elischer 
26209ed346baSBosko Milekic 		mtx_lock(&ng_nodelist_mtx);
26214cf49a43SJulian Elischer 		/* Count number of nodes */
262230400f03SJulian Elischer 		LIST_FOREACH(node, &ng_nodelist, nd_nodes) {
262370de87f2SJulian Elischer 			if (NG_NODE_IS_VALID(node)
262470de87f2SJulian Elischer 			&& (unnamed || NG_NODE_HAS_NAME(node))) {
26254cf49a43SJulian Elischer 				num++;
26264cf49a43SJulian Elischer 			}
262770de87f2SJulian Elischer 		}
26289ed346baSBosko Milekic 		mtx_unlock(&ng_nodelist_mtx);
26294cf49a43SJulian Elischer 
26304cf49a43SJulian Elischer 		/* Get response struct */
2631069154d5SJulian Elischer 		NG_MKRESPONSE(resp, msg, sizeof(*nl)
26324cf49a43SJulian Elischer 		    + (num * sizeof(struct nodeinfo)), M_NOWAIT);
2633069154d5SJulian Elischer 		if (resp == NULL) {
26344cf49a43SJulian Elischer 			error = ENOMEM;
26354cf49a43SJulian Elischer 			break;
26364cf49a43SJulian Elischer 		}
2637069154d5SJulian Elischer 		nl = (struct namelist *) resp->data;
26384cf49a43SJulian Elischer 
26394cf49a43SJulian Elischer 		/* Cycle through the linked list of nodes */
26404cf49a43SJulian Elischer 		nl->numnames = 0;
26419ed346baSBosko Milekic 		mtx_lock(&ng_nodelist_mtx);
264230400f03SJulian Elischer 		LIST_FOREACH(node, &ng_nodelist, nd_nodes) {
26434cf49a43SJulian Elischer 			struct nodeinfo *const np = &nl->nodeinfo[nl->numnames];
26444cf49a43SJulian Elischer 
26454cf49a43SJulian Elischer 			if (nl->numnames >= num) {
26464cf49a43SJulian Elischer 				log(LOG_ERR, "%s: number of %s changed\n",
26476e551fb6SDavid E. O'Brien 				    __func__, "nodes");
26484cf49a43SJulian Elischer 				break;
26494cf49a43SJulian Elischer 			}
265030400f03SJulian Elischer 			if (NG_NODE_NOT_VALID(node))
26514cf49a43SJulian Elischer 				continue;
265230400f03SJulian Elischer 			if (!unnamed && (! NG_NODE_HAS_NAME(node)))
26534cf49a43SJulian Elischer 				continue;
265430400f03SJulian Elischer 			if (NG_NODE_HAS_NAME(node))
265530400f03SJulian Elischer 				strncpy(np->name, NG_NODE_NAME(node), NG_NODELEN);
265630400f03SJulian Elischer 			strncpy(np->type, node->nd_type->name, NG_TYPELEN);
2657dc90cad9SJulian Elischer 			np->id = ng_node2ID(node);
265830400f03SJulian Elischer 			np->hooks = node->nd_numhooks;
26594cf49a43SJulian Elischer 			nl->numnames++;
26604cf49a43SJulian Elischer 		}
26619ed346baSBosko Milekic 		mtx_unlock(&ng_nodelist_mtx);
26624cf49a43SJulian Elischer 		break;
26634cf49a43SJulian Elischer 	    }
26644cf49a43SJulian Elischer 
26654cf49a43SJulian Elischer 	case NGM_LISTTYPES:
26664cf49a43SJulian Elischer 	    {
26674cf49a43SJulian Elischer 		struct typelist *tl;
26684cf49a43SJulian Elischer 		struct ng_type *type;
26694cf49a43SJulian Elischer 		int num = 0;
26704cf49a43SJulian Elischer 
26719ed346baSBosko Milekic 		mtx_lock(&ng_typelist_mtx);
26724cf49a43SJulian Elischer 		/* Count number of types */
267370de87f2SJulian Elischer 		LIST_FOREACH(type, &ng_typelist, types) {
26744cf49a43SJulian Elischer 			num++;
267570de87f2SJulian Elischer 		}
26769ed346baSBosko Milekic 		mtx_unlock(&ng_typelist_mtx);
26774cf49a43SJulian Elischer 
26784cf49a43SJulian Elischer 		/* Get response struct */
2679069154d5SJulian Elischer 		NG_MKRESPONSE(resp, msg, sizeof(*tl)
26804cf49a43SJulian Elischer 		    + (num * sizeof(struct typeinfo)), M_NOWAIT);
2681069154d5SJulian Elischer 		if (resp == NULL) {
26824cf49a43SJulian Elischer 			error = ENOMEM;
26834cf49a43SJulian Elischer 			break;
26844cf49a43SJulian Elischer 		}
2685069154d5SJulian Elischer 		tl = (struct typelist *) resp->data;
26864cf49a43SJulian Elischer 
26874cf49a43SJulian Elischer 		/* Cycle through the linked list of types */
26884cf49a43SJulian Elischer 		tl->numtypes = 0;
26899ed346baSBosko Milekic 		mtx_lock(&ng_typelist_mtx);
2690069154d5SJulian Elischer 		LIST_FOREACH(type, &ng_typelist, types) {
26914cf49a43SJulian Elischer 			struct typeinfo *const tp = &tl->typeinfo[tl->numtypes];
26924cf49a43SJulian Elischer 
26934cf49a43SJulian Elischer 			if (tl->numtypes >= num) {
26944cf49a43SJulian Elischer 				log(LOG_ERR, "%s: number of %s changed\n",
26956e551fb6SDavid E. O'Brien 				    __func__, "types");
26964cf49a43SJulian Elischer 				break;
26974cf49a43SJulian Elischer 			}
2698a096e45aSArchie Cobbs 			strncpy(tp->type_name, type->name, NG_TYPELEN);
2699c73b94a2SJulian Elischer 			tp->numnodes = type->refs - 1; /* don't count list */
27004cf49a43SJulian Elischer 			tl->numtypes++;
27014cf49a43SJulian Elischer 		}
27029ed346baSBosko Milekic 		mtx_unlock(&ng_typelist_mtx);
27034cf49a43SJulian Elischer 		break;
27044cf49a43SJulian Elischer 	    }
27054cf49a43SJulian Elischer 
2706f8307e12SArchie Cobbs 	case NGM_BINARY2ASCII:
2707f8307e12SArchie Cobbs 	    {
27087133ac27SArchie Cobbs 		int bufSize = 20 * 1024;	/* XXX hard coded constant */
2709f8307e12SArchie Cobbs 		const struct ng_parse_type *argstype;
2710f8307e12SArchie Cobbs 		const struct ng_cmdlist *c;
2711069154d5SJulian Elischer 		struct ng_mesg *binary, *ascii;
2712f8307e12SArchie Cobbs 
2713f8307e12SArchie Cobbs 		/* Data area must contain a valid netgraph message */
2714f8307e12SArchie Cobbs 		binary = (struct ng_mesg *)msg->data;
2715f8307e12SArchie Cobbs 		if (msg->header.arglen < sizeof(struct ng_mesg)
271670de87f2SJulian Elischer 		    || (msg->header.arglen - sizeof(struct ng_mesg)
271770de87f2SJulian Elischer 		      < binary->header.arglen)) {
27186b795970SJulian Elischer 			TRAP_ERROR();
2719f8307e12SArchie Cobbs 			error = EINVAL;
2720f8307e12SArchie Cobbs 			break;
2721f8307e12SArchie Cobbs 		}
2722f8307e12SArchie Cobbs 
2723f8307e12SArchie Cobbs 		/* Get a response message with lots of room */
2724069154d5SJulian Elischer 		NG_MKRESPONSE(resp, msg, sizeof(*ascii) + bufSize, M_NOWAIT);
2725069154d5SJulian Elischer 		if (resp == NULL) {
2726f8307e12SArchie Cobbs 			error = ENOMEM;
2727f8307e12SArchie Cobbs 			break;
2728f8307e12SArchie Cobbs 		}
2729069154d5SJulian Elischer 		ascii = (struct ng_mesg *)resp->data;
2730f8307e12SArchie Cobbs 
2731f8307e12SArchie Cobbs 		/* Copy binary message header to response message payload */
2732f8307e12SArchie Cobbs 		bcopy(binary, ascii, sizeof(*binary));
2733f8307e12SArchie Cobbs 
2734f8307e12SArchie Cobbs 		/* Find command by matching typecookie and command number */
273530400f03SJulian Elischer 		for (c = here->nd_type->cmdlist;
2736f8307e12SArchie Cobbs 		    c != NULL && c->name != NULL; c++) {
2737f8307e12SArchie Cobbs 			if (binary->header.typecookie == c->cookie
2738f8307e12SArchie Cobbs 			    && binary->header.cmd == c->cmd)
2739f8307e12SArchie Cobbs 				break;
2740f8307e12SArchie Cobbs 		}
2741f8307e12SArchie Cobbs 		if (c == NULL || c->name == NULL) {
2742f8307e12SArchie Cobbs 			for (c = ng_generic_cmds; c->name != NULL; c++) {
2743f8307e12SArchie Cobbs 				if (binary->header.typecookie == c->cookie
2744f8307e12SArchie Cobbs 				    && binary->header.cmd == c->cmd)
2745f8307e12SArchie Cobbs 					break;
2746f8307e12SArchie Cobbs 			}
2747f8307e12SArchie Cobbs 			if (c->name == NULL) {
2748069154d5SJulian Elischer 				NG_FREE_MSG(resp);
2749f8307e12SArchie Cobbs 				error = ENOSYS;
2750f8307e12SArchie Cobbs 				break;
2751f8307e12SArchie Cobbs 			}
2752f8307e12SArchie Cobbs 		}
2753f8307e12SArchie Cobbs 
2754f8307e12SArchie Cobbs 		/* Convert command name to ASCII */
2755f8307e12SArchie Cobbs 		snprintf(ascii->header.cmdstr, sizeof(ascii->header.cmdstr),
2756f8307e12SArchie Cobbs 		    "%s", c->name);
2757f8307e12SArchie Cobbs 
2758f8307e12SArchie Cobbs 		/* Convert command arguments to ASCII */
2759f8307e12SArchie Cobbs 		argstype = (binary->header.flags & NGF_RESP) ?
2760f8307e12SArchie Cobbs 		    c->respType : c->mesgType;
276170de87f2SJulian Elischer 		if (argstype == NULL) {
2762f8307e12SArchie Cobbs 			*ascii->data = '\0';
276370de87f2SJulian Elischer 		} else {
2764f8307e12SArchie Cobbs 			if ((error = ng_unparse(argstype,
2765f8307e12SArchie Cobbs 			    (u_char *)binary->data,
2766f8307e12SArchie Cobbs 			    ascii->data, bufSize)) != 0) {
2767069154d5SJulian Elischer 				NG_FREE_MSG(resp);
2768f8307e12SArchie Cobbs 				break;
2769f8307e12SArchie Cobbs 			}
2770f8307e12SArchie Cobbs 		}
2771f8307e12SArchie Cobbs 
2772f8307e12SArchie Cobbs 		/* Return the result as struct ng_mesg plus ASCII string */
2773f8307e12SArchie Cobbs 		bufSize = strlen(ascii->data) + 1;
2774f8307e12SArchie Cobbs 		ascii->header.arglen = bufSize;
2775069154d5SJulian Elischer 		resp->header.arglen = sizeof(*ascii) + bufSize;
2776f8307e12SArchie Cobbs 		break;
2777f8307e12SArchie Cobbs 	    }
2778f8307e12SArchie Cobbs 
2779f8307e12SArchie Cobbs 	case NGM_ASCII2BINARY:
2780f8307e12SArchie Cobbs 	    {
2781f8307e12SArchie Cobbs 		int bufSize = 2000;	/* XXX hard coded constant */
2782f8307e12SArchie Cobbs 		const struct ng_cmdlist *c;
2783f8307e12SArchie Cobbs 		const struct ng_parse_type *argstype;
2784069154d5SJulian Elischer 		struct ng_mesg *ascii, *binary;
278552ec4a03SArchie Cobbs 		int off = 0;
2786f8307e12SArchie Cobbs 
2787f8307e12SArchie Cobbs 		/* Data area must contain at least a struct ng_mesg + '\0' */
2788f8307e12SArchie Cobbs 		ascii = (struct ng_mesg *)msg->data;
278970de87f2SJulian Elischer 		if ((msg->header.arglen < sizeof(*ascii) + 1)
279070de87f2SJulian Elischer 		    || (ascii->header.arglen < 1)
279170de87f2SJulian Elischer 		    || (msg->header.arglen
279270de87f2SJulian Elischer 		      < sizeof(*ascii) + ascii->header.arglen)) {
27936b795970SJulian Elischer 			TRAP_ERROR();
2794f8307e12SArchie Cobbs 			error = EINVAL;
2795f8307e12SArchie Cobbs 			break;
2796f8307e12SArchie Cobbs 		}
2797f8307e12SArchie Cobbs 		ascii->data[ascii->header.arglen - 1] = '\0';
2798f8307e12SArchie Cobbs 
2799f8307e12SArchie Cobbs 		/* Get a response message with lots of room */
2800069154d5SJulian Elischer 		NG_MKRESPONSE(resp, msg, sizeof(*binary) + bufSize, M_NOWAIT);
2801069154d5SJulian Elischer 		if (resp == NULL) {
2802f8307e12SArchie Cobbs 			error = ENOMEM;
2803f8307e12SArchie Cobbs 			break;
2804f8307e12SArchie Cobbs 		}
2805069154d5SJulian Elischer 		binary = (struct ng_mesg *)resp->data;
2806f8307e12SArchie Cobbs 
2807f8307e12SArchie Cobbs 		/* Copy ASCII message header to response message payload */
2808f8307e12SArchie Cobbs 		bcopy(ascii, binary, sizeof(*ascii));
2809f8307e12SArchie Cobbs 
2810f8307e12SArchie Cobbs 		/* Find command by matching ASCII command string */
281130400f03SJulian Elischer 		for (c = here->nd_type->cmdlist;
2812f8307e12SArchie Cobbs 		    c != NULL && c->name != NULL; c++) {
2813f8307e12SArchie Cobbs 			if (strcmp(ascii->header.cmdstr, c->name) == 0)
2814f8307e12SArchie Cobbs 				break;
2815f8307e12SArchie Cobbs 		}
2816f8307e12SArchie Cobbs 		if (c == NULL || c->name == NULL) {
2817f8307e12SArchie Cobbs 			for (c = ng_generic_cmds; c->name != NULL; c++) {
2818f8307e12SArchie Cobbs 				if (strcmp(ascii->header.cmdstr, c->name) == 0)
2819f8307e12SArchie Cobbs 					break;
2820f8307e12SArchie Cobbs 			}
2821f8307e12SArchie Cobbs 			if (c->name == NULL) {
2822069154d5SJulian Elischer 				NG_FREE_MSG(resp);
2823f8307e12SArchie Cobbs 				error = ENOSYS;
2824f8307e12SArchie Cobbs 				break;
2825f8307e12SArchie Cobbs 			}
2826f8307e12SArchie Cobbs 		}
2827f8307e12SArchie Cobbs 
2828f8307e12SArchie Cobbs 		/* Convert command name to binary */
2829f8307e12SArchie Cobbs 		binary->header.cmd = c->cmd;
2830f8307e12SArchie Cobbs 		binary->header.typecookie = c->cookie;
2831f8307e12SArchie Cobbs 
2832f8307e12SArchie Cobbs 		/* Convert command arguments to binary */
2833f8307e12SArchie Cobbs 		argstype = (binary->header.flags & NGF_RESP) ?
2834f8307e12SArchie Cobbs 		    c->respType : c->mesgType;
283570de87f2SJulian Elischer 		if (argstype == NULL) {
2836f8307e12SArchie Cobbs 			bufSize = 0;
283770de87f2SJulian Elischer 		} else {
2838f8307e12SArchie Cobbs 			if ((error = ng_parse(argstype, ascii->data,
2839f8307e12SArchie Cobbs 			    &off, (u_char *)binary->data, &bufSize)) != 0) {
2840069154d5SJulian Elischer 				NG_FREE_MSG(resp);
2841f8307e12SArchie Cobbs 				break;
2842f8307e12SArchie Cobbs 			}
2843f8307e12SArchie Cobbs 		}
2844f8307e12SArchie Cobbs 
2845f8307e12SArchie Cobbs 		/* Return the result */
2846f8307e12SArchie Cobbs 		binary->header.arglen = bufSize;
2847069154d5SJulian Elischer 		resp->header.arglen = sizeof(*binary) + bufSize;
2848f8307e12SArchie Cobbs 		break;
2849f8307e12SArchie Cobbs 	    }
2850f8307e12SArchie Cobbs 
28517095e097SPoul-Henning Kamp 	case NGM_TEXT_CONFIG:
28524cf49a43SJulian Elischer 	case NGM_TEXT_STATUS:
28534cf49a43SJulian Elischer 		/*
28544cf49a43SJulian Elischer 		 * This one is tricky as it passes the command down to the
28554cf49a43SJulian Elischer 		 * actual node, even though it is a generic type command.
2856069154d5SJulian Elischer 		 * This means we must assume that the item/msg is already freed
28574cf49a43SJulian Elischer 		 * when control passes back to us.
28584cf49a43SJulian Elischer 		 */
285930400f03SJulian Elischer 		if (here->nd_type->rcvmsg != NULL) {
2860069154d5SJulian Elischer 			NGI_MSG(item) = msg; /* put it back as we found it */
286130400f03SJulian Elischer 			return((*here->nd_type->rcvmsg)(here, item, lasthook));
28624cf49a43SJulian Elischer 		}
28634cf49a43SJulian Elischer 		/* Fall through if rcvmsg not supported */
28644cf49a43SJulian Elischer 	default:
28656b795970SJulian Elischer 		TRAP_ERROR();
28664cf49a43SJulian Elischer 		error = EINVAL;
28674cf49a43SJulian Elischer 	}
2868069154d5SJulian Elischer 	/*
2869069154d5SJulian Elischer 	 * Sometimes a generic message may be statically allocated
2870069154d5SJulian Elischer 	 * to avoid problems with allocating when in tight memeory situations.
2871069154d5SJulian Elischer 	 * Don't free it if it is so.
2872069154d5SJulian Elischer 	 * I break them appart here, because erros may cause a free if the item
2873069154d5SJulian Elischer 	 * in which case we'd be doing it twice.
2874069154d5SJulian Elischer 	 * they are kept together above, to simplify freeing.
2875069154d5SJulian Elischer 	 */
2876069154d5SJulian Elischer out:
2877069154d5SJulian Elischer 	NG_RESPOND_MSG(error, here, item, resp);
28781acb27c6SJulian Elischer 	if (msg)
2879069154d5SJulian Elischer 		NG_FREE_MSG(msg);
28804cf49a43SJulian Elischer 	return (error);
28814cf49a43SJulian Elischer }
28824cf49a43SJulian Elischer 
28834cf49a43SJulian Elischer /*
2884a096e45aSArchie Cobbs  * Copy a 'meta'.
2885a096e45aSArchie Cobbs  *
2886a096e45aSArchie Cobbs  * Returns new meta, or NULL if original meta is NULL or ENOMEM.
2887a096e45aSArchie Cobbs  */
2888a096e45aSArchie Cobbs meta_p
2889a096e45aSArchie Cobbs ng_copy_meta(meta_p meta)
2890a096e45aSArchie Cobbs {
2891a096e45aSArchie Cobbs 	meta_p meta2;
2892a096e45aSArchie Cobbs 
2893a096e45aSArchie Cobbs 	if (meta == NULL)
2894a096e45aSArchie Cobbs 		return (NULL);
2895069154d5SJulian Elischer 	MALLOC(meta2, meta_p, meta->used_len, M_NETGRAPH_META, M_NOWAIT);
2896a096e45aSArchie Cobbs 	if (meta2 == NULL)
2897a096e45aSArchie Cobbs 		return (NULL);
2898a096e45aSArchie Cobbs 	meta2->allocated_len = meta->used_len;
2899a096e45aSArchie Cobbs 	bcopy(meta, meta2, meta->used_len);
2900a096e45aSArchie Cobbs 	return (meta2);
2901a096e45aSArchie Cobbs }
2902a096e45aSArchie Cobbs 
29034cf49a43SJulian Elischer /************************************************************************
29044cf49a43SJulian Elischer 			Module routines
29054cf49a43SJulian Elischer ************************************************************************/
29064cf49a43SJulian Elischer 
29074cf49a43SJulian Elischer /*
29084cf49a43SJulian Elischer  * Handle the loading/unloading of a netgraph node type module
29094cf49a43SJulian Elischer  */
29104cf49a43SJulian Elischer int
29114cf49a43SJulian Elischer ng_mod_event(module_t mod, int event, void *data)
29124cf49a43SJulian Elischer {
29134cf49a43SJulian Elischer 	struct ng_type *const type = data;
29144cf49a43SJulian Elischer 	int s, error = 0;
29154cf49a43SJulian Elischer 
29164cf49a43SJulian Elischer 	switch (event) {
29174cf49a43SJulian Elischer 	case MOD_LOAD:
29184cf49a43SJulian Elischer 
29194cf49a43SJulian Elischer 		/* Register new netgraph node type */
29204cf49a43SJulian Elischer 		s = splnet();
29214cf49a43SJulian Elischer 		if ((error = ng_newtype(type)) != 0) {
29224cf49a43SJulian Elischer 			splx(s);
29234cf49a43SJulian Elischer 			break;
29244cf49a43SJulian Elischer 		}
29254cf49a43SJulian Elischer 
29264cf49a43SJulian Elischer 		/* Call type specific code */
29274cf49a43SJulian Elischer 		if (type->mod_event != NULL)
2928069154d5SJulian Elischer 			if ((error = (*type->mod_event)(mod, event, data))) {
29299ed346baSBosko Milekic 				mtx_lock(&ng_typelist_mtx);
2930c73b94a2SJulian Elischer 				type->refs--;	/* undo it */
29314cf49a43SJulian Elischer 				LIST_REMOVE(type, types);
29329ed346baSBosko Milekic 				mtx_unlock(&ng_typelist_mtx);
2933069154d5SJulian Elischer 			}
29344cf49a43SJulian Elischer 		splx(s);
29354cf49a43SJulian Elischer 		break;
29364cf49a43SJulian Elischer 
29374cf49a43SJulian Elischer 	case MOD_UNLOAD:
29384cf49a43SJulian Elischer 		s = splnet();
2939c73b94a2SJulian Elischer 		if (type->refs > 1) {		/* make sure no nodes exist! */
29404cf49a43SJulian Elischer 			error = EBUSY;
2941c73b94a2SJulian Elischer 		} else {
2942c73b94a2SJulian Elischer 			if (type->refs == 0) {
2943c73b94a2SJulian Elischer 				/* failed load, nothing to undo */
2944c73b94a2SJulian Elischer 				splx(s);
2945c73b94a2SJulian Elischer 				break;
2946c73b94a2SJulian Elischer 			}
29474cf49a43SJulian Elischer 			if (type->mod_event != NULL) {	/* check with type */
29484cf49a43SJulian Elischer 				error = (*type->mod_event)(mod, event, data);
29494cf49a43SJulian Elischer 				if (error != 0) {	/* type refuses.. */
29504cf49a43SJulian Elischer 					splx(s);
29514cf49a43SJulian Elischer 					break;
29524cf49a43SJulian Elischer 				}
29534cf49a43SJulian Elischer 			}
29549ed346baSBosko Milekic 			mtx_lock(&ng_typelist_mtx);
29554cf49a43SJulian Elischer 			LIST_REMOVE(type, types);
29569ed346baSBosko Milekic 			mtx_unlock(&ng_typelist_mtx);
29574cf49a43SJulian Elischer 		}
29584cf49a43SJulian Elischer 		splx(s);
29594cf49a43SJulian Elischer 		break;
29604cf49a43SJulian Elischer 
29614cf49a43SJulian Elischer 	default:
29624cf49a43SJulian Elischer 		if (type->mod_event != NULL)
29634cf49a43SJulian Elischer 			error = (*type->mod_event)(mod, event, data);
29644cf49a43SJulian Elischer 		else
29654cf49a43SJulian Elischer 			error = 0;		/* XXX ? */
29664cf49a43SJulian Elischer 		break;
29674cf49a43SJulian Elischer 	}
29684cf49a43SJulian Elischer 	return (error);
29694cf49a43SJulian Elischer }
29704cf49a43SJulian Elischer 
29714cf49a43SJulian Elischer /*
29724cf49a43SJulian Elischer  * Handle loading and unloading for this code.
29734cf49a43SJulian Elischer  * The only thing we need to link into is the NETISR strucure.
29744cf49a43SJulian Elischer  */
29754cf49a43SJulian Elischer static int
29764cf49a43SJulian Elischer ngb_mod_event(module_t mod, int event, void *data)
29774cf49a43SJulian Elischer {
29784cf49a43SJulian Elischer 	int s, error = 0;
29794cf49a43SJulian Elischer 
29804cf49a43SJulian Elischer 	switch (event) {
29814cf49a43SJulian Elischer 	case MOD_LOAD:
29824cf49a43SJulian Elischer 		/* Register line discipline */
29836008862bSJohn Baldwin 		mtx_init(&ng_worklist_mtx, "ng_worklist", NULL, MTX_SPIN);
29846008862bSJohn Baldwin 		mtx_init(&ng_typelist_mtx, "netgraph types mutex", NULL, 0);
29856008862bSJohn Baldwin 		mtx_init(&ng_nodelist_mtx, "netgraph nodelist mutex", NULL, 0);
29866008862bSJohn Baldwin 		mtx_init(&ng_idhash_mtx, "netgraph idhash mutex", NULL, 0);
29876008862bSJohn Baldwin 		mtx_init(&ngq_mtx, "netgraph netisr mutex", NULL, 0);
29884cf49a43SJulian Elischer 		s = splimp();
29891cafed39SJonathan Lemon 		netisr_register(NETISR_NETGRAPH, (netisr_t *)ngintr, NULL);
29904cf49a43SJulian Elischer 		splx(s);
29914cf49a43SJulian Elischer 		break;
29924cf49a43SJulian Elischer 	case MOD_UNLOAD:
29934cf49a43SJulian Elischer 		/* You cant unload it because an interface may be using it.  */
29944cf49a43SJulian Elischer 		error = EBUSY;
29954cf49a43SJulian Elischer 		break;
29964cf49a43SJulian Elischer 	default:
29974cf49a43SJulian Elischer 		error = EOPNOTSUPP;
29984cf49a43SJulian Elischer 		break;
29994cf49a43SJulian Elischer 	}
30004cf49a43SJulian Elischer 	return (error);
30014cf49a43SJulian Elischer }
30024cf49a43SJulian Elischer 
30034cf49a43SJulian Elischer static moduledata_t netgraph_mod = {
30044cf49a43SJulian Elischer 	"netgraph",
30054cf49a43SJulian Elischer 	ngb_mod_event,
30064cf49a43SJulian Elischer 	(NULL)
30074cf49a43SJulian Elischer };
30084cf49a43SJulian Elischer DECLARE_MODULE(netgraph, netgraph_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
3009bfa7e882SJulian Elischer SYSCTL_NODE(_net, OID_AUTO, graph, CTLFLAG_RW, 0, "netgraph Family");
3010bfa7e882SJulian Elischer SYSCTL_INT(_net_graph, OID_AUTO, abi_version, CTLFLAG_RD, 0, NG_ABI_VERSION,"");
3011bfa7e882SJulian Elischer SYSCTL_INT(_net_graph, OID_AUTO, msg_version, CTLFLAG_RD, 0, NG_VERSION, "");
30124cf49a43SJulian Elischer 
30134cf49a43SJulian Elischer /************************************************************************
3014069154d5SJulian Elischer 			Queue element get/free routines
30154cf49a43SJulian Elischer ************************************************************************/
30164cf49a43SJulian Elischer 
30174cf49a43SJulian Elischer 
3018069154d5SJulian Elischer static int			allocated;	/* number of items malloc'd */
30195dfe609dSHartmut Brandt 
3020069154d5SJulian Elischer static int			maxalloc = 128;	/* limit the damage of a leak */
30215dfe609dSHartmut Brandt static int			ngqfreemax = 64;/* cache at most this many */
30225dfe609dSHartmut Brandt 
30235dfe609dSHartmut Brandt TUNABLE_INT("net.graph.maxalloc", &maxalloc);
30245dfe609dSHartmut Brandt SYSCTL_INT(_net_graph, OID_AUTO, maxalloc, CTLFLAG_RD, &maxalloc,
30255dfe609dSHartmut Brandt     0, "Maximum number of queue items to allocate");
30265dfe609dSHartmut Brandt 
30275dfe609dSHartmut Brandt TUNABLE_INT("net.graph.ngqfreemax", &ngqfreemax);
30285dfe609dSHartmut Brandt SYSCTL_INT(_net_graph, OID_AUTO, ngqfreemax, CTLFLAG_RD, &ngqfreemax,
30295dfe609dSHartmut Brandt     0, "Maximum number of free queue items to cache");
30305dfe609dSHartmut Brandt 
3031069154d5SJulian Elischer static const int		ngqfreelow = 4; /* try malloc if free < this */
3032069154d5SJulian Elischer static volatile int		ngqfreesize;	/* number of cached entries */
303330400f03SJulian Elischer #ifdef	NETGRAPH_DEBUG
3034069154d5SJulian Elischer static TAILQ_HEAD(, ng_item) ng_itemlist = TAILQ_HEAD_INITIALIZER(ng_itemlist);
3035069154d5SJulian Elischer #endif
30364cf49a43SJulian Elischer /*
30374cf49a43SJulian Elischer  * Get a queue entry
3038069154d5SJulian Elischer  * This is usually called when a packet first enters netgraph.
3039069154d5SJulian Elischer  * By definition, this is usually from an interrupt, or from a user.
3040069154d5SJulian Elischer  * Users are not so important, but try be quick for the times that it's
3041069154d5SJulian Elischer  * an interrupt. Use atomic operations to cope with collisions
3042069154d5SJulian Elischer  * with interrupts and other processors. Assumes MALLOC is SMP safe.
3043069154d5SJulian Elischer  * XXX If reserve is low, we should try to get 2 from malloc as this
3044069154d5SJulian Elischer  * would indicate it often fails.
30454cf49a43SJulian Elischer  */
3046069154d5SJulian Elischer static item_p
30474cf49a43SJulian Elischer ng_getqblk(void)
30484cf49a43SJulian Elischer {
3049069154d5SJulian Elischer 	item_p item = NULL;
30504cf49a43SJulian Elischer 
3051069154d5SJulian Elischer 	/*
3052069154d5SJulian Elischer 	 * Try get a cached queue block, or else allocate a new one
3053069154d5SJulian Elischer 	 * If we are less than our reserve, try malloc. If malloc
3054069154d5SJulian Elischer 	 * fails, then that's what the reserve is for...
3055069154d5SJulian Elischer 	 * Don't completely trust ngqfreesize, as it is subject
3056069154d5SJulian Elischer 	 * to races.. (it'll eventually catch up but may be out by one or two
3057069154d5SJulian Elischer 	 * for brief moments(under SMP or interrupts).
3058069154d5SJulian Elischer 	 * ngqfree is the final arbiter. We have our little reserve
3059069154d5SJulian Elischer 	 * because we use M_NOWAIT for malloc. This just helps us
3060069154d5SJulian Elischer 	 * avoid dropping packets while not increasing the time
306134f9ca09SJulian Elischer 	 * we take to service the interrupt (on average) (I hope).
3062069154d5SJulian Elischer 	 */
3063069154d5SJulian Elischer 	for (;;) {
3064069154d5SJulian Elischer 		if ((ngqfreesize < ngqfreelow) || (ngqfree == NULL)) {
3065069154d5SJulian Elischer 			if (allocated < maxalloc) {  /* don't leak forever */
3066069154d5SJulian Elischer 				MALLOC(item, item_p ,
3067069154d5SJulian Elischer 				    sizeof(*item), M_NETGRAPH_ITEM,
3068069154d5SJulian Elischer 				    (M_NOWAIT | M_ZERO));
3069069154d5SJulian Elischer 				if (item) {
307030400f03SJulian Elischer #ifdef	NETGRAPH_DEBUG
3071069154d5SJulian Elischer 					TAILQ_INSERT_TAIL(&ng_itemlist,
3072069154d5SJulian Elischer 								item, all);
307330400f03SJulian Elischer #endif	/* NETGRAPH_DEBUG */
3074069154d5SJulian Elischer 					atomic_add_int(&allocated, 1);
3075069154d5SJulian Elischer 					break;
30764cf49a43SJulian Elischer 				}
3077069154d5SJulian Elischer 			}
3078069154d5SJulian Elischer 		}
3079069154d5SJulian Elischer 
3080069154d5SJulian Elischer 		/*
3081069154d5SJulian Elischer 		 * We didn't or couldn't malloc.
3082069154d5SJulian Elischer 		 * try get one from our cache.
3083069154d5SJulian Elischer 		 * item must be NULL to get here.
3084069154d5SJulian Elischer 		 */
3085069154d5SJulian Elischer 		if ((item = ngqfree) != NULL) {
3086069154d5SJulian Elischer 			/*
3087069154d5SJulian Elischer 			 * Atomically try grab the first item
3088069154d5SJulian Elischer 			 * and put it's successor in its place.
3089069154d5SJulian Elischer 			 * If we fail, just try again.. someone else
3090069154d5SJulian Elischer 			 * beat us to this one or freed one.
3091069154d5SJulian Elischer 			 * Don't worry about races with ngqfreesize.
3092069154d5SJulian Elischer 			 * Close enough is good enough..
3093069154d5SJulian Elischer 			 */
3094069154d5SJulian Elischer 			if (atomic_cmpset_ptr(&ngqfree, item, item->el_next)) {
3095069154d5SJulian Elischer 				atomic_subtract_int(&ngqfreesize, 1);
309634f9ca09SJulian Elischer 				item->el_flags &= ~NGQF_FREE;
3097069154d5SJulian Elischer 				break;
3098069154d5SJulian Elischer 			}
309934f9ca09SJulian Elischer 			/*
310034f9ca09SJulian Elischer 			 * something got there before we did.. try again
310134f9ca09SJulian Elischer 			 * (go around the loop again)
310234f9ca09SJulian Elischer 			 */
3103069154d5SJulian Elischer 			item = NULL;
31044cf49a43SJulian Elischer 		} else {
3105069154d5SJulian Elischer 			/* We really ran out */
3106069154d5SJulian Elischer 			break;
31074cf49a43SJulian Elischer 		}
3108069154d5SJulian Elischer 	}
3109069154d5SJulian Elischer 	return (item);
31104cf49a43SJulian Elischer }
31114cf49a43SJulian Elischer 
31124cf49a43SJulian Elischer /*
31134cf49a43SJulian Elischer  * Release a queue entry
31144cf49a43SJulian Elischer  */
3115069154d5SJulian Elischer void
3116069154d5SJulian Elischer ng_free_item(item_p item)
3117069154d5SJulian Elischer {
3118069154d5SJulian Elischer 
3119069154d5SJulian Elischer 	/*
3120069154d5SJulian Elischer 	 * The item may hold resources on it's own. We need to free
3121069154d5SJulian Elischer 	 * these before we can free the item. What they are depends upon
3122069154d5SJulian Elischer 	 * what kind of item it is. it is important that nodes zero
3123069154d5SJulian Elischer 	 * out pointers to resources that they remove from the item
3124069154d5SJulian Elischer 	 * or we release them again here.
3125069154d5SJulian Elischer 	 */
3126069154d5SJulian Elischer 	if (item->el_flags & NGQF_FREE) {
3127069154d5SJulian Elischer 		panic(" Freeing free queue item");
3128069154d5SJulian Elischer 	}
31296b795970SJulian Elischer 	switch (item->el_flags & NGQF_TYPE) {
3130069154d5SJulian Elischer 	case NGQF_DATA:
3131069154d5SJulian Elischer 		/* If we have an mbuf and metadata still attached.. */
3132069154d5SJulian Elischer 		NG_FREE_M(_NGI_M(item));
3133069154d5SJulian Elischer 		NG_FREE_META(_NGI_META(item));
3134069154d5SJulian Elischer 		break;
3135069154d5SJulian Elischer 	case NGQF_MESG:
3136facfd889SArchie Cobbs 		_NGI_RETADDR(item) = 0;
3137069154d5SJulian Elischer 		NG_FREE_MSG(_NGI_MSG(item));
3138069154d5SJulian Elischer 		break;
31396b795970SJulian Elischer 	case NGQF_FN:
31406b795970SJulian Elischer 		/* nothing to free really, */
31416b795970SJulian Elischer 		_NGI_FN(item) = NULL;
31426b795970SJulian Elischer 		_NGI_ARG1(item) = NULL;
31436b795970SJulian Elischer 		_NGI_ARG2(item) = 0;
31446b795970SJulian Elischer 	case NGQF_UNDEF:
314555be04abSPeter Wemm 		break;
3146069154d5SJulian Elischer 	}
3147069154d5SJulian Elischer 	/* If we still have a node or hook referenced... */
31481acb27c6SJulian Elischer 	_NGI_CLR_NODE(item);
31491acb27c6SJulian Elischer 	_NGI_CLR_HOOK(item);
3150069154d5SJulian Elischer 	item->el_flags |= NGQF_FREE;
3151069154d5SJulian Elischer 
3152069154d5SJulian Elischer 	/*
3153069154d5SJulian Elischer 	 * We have freed any resources held by the item.
3154069154d5SJulian Elischer 	 * now we can free the item itself.
3155069154d5SJulian Elischer 	 */
3156069154d5SJulian Elischer 	if (ngqfreesize < ngqfreemax) { /* don't worry about races */
3157069154d5SJulian Elischer 		for (;;) {
3158069154d5SJulian Elischer 			item->el_next = ngqfree;
3159069154d5SJulian Elischer 			if (atomic_cmpset_ptr(&ngqfree, item->el_next, item)) {
3160069154d5SJulian Elischer 				break;
3161069154d5SJulian Elischer 			}
3162069154d5SJulian Elischer 		}
3163069154d5SJulian Elischer 		atomic_add_int(&ngqfreesize, 1);
3164069154d5SJulian Elischer 	} else {
3165069154d5SJulian Elischer 		/* This is the only place that should use this Macro */
316630400f03SJulian Elischer #ifdef	NETGRAPH_DEBUG
3167069154d5SJulian Elischer 		TAILQ_REMOVE(&ng_itemlist, item, all);
316830400f03SJulian Elischer #endif	/* NETGRAPH_DEBUG */
3169069154d5SJulian Elischer 		NG_FREE_ITEM_REAL(item);
3170069154d5SJulian Elischer 		atomic_subtract_int(&allocated, 1);
3171069154d5SJulian Elischer 	}
3172069154d5SJulian Elischer }
3173069154d5SJulian Elischer 
317430400f03SJulian Elischer #ifdef	NETGRAPH_DEBUG
317530400f03SJulian Elischer void
317630400f03SJulian Elischer dumphook (hook_p hook, char *file, int line)
317730400f03SJulian Elischer {
317830400f03SJulian Elischer 	printf("hook: name %s, %d refs, Last touched:\n",
317930400f03SJulian Elischer 		_NG_HOOK_NAME(hook), hook->hk_refs);
318030400f03SJulian Elischer 	printf("	Last active @ %s, line %d\n",
318130400f03SJulian Elischer 		hook->lastfile, hook->lastline);
318230400f03SJulian Elischer 	if (line) {
318330400f03SJulian Elischer 		printf(" problem discovered at file %s, line %d\n", file, line);
318430400f03SJulian Elischer 	}
318530400f03SJulian Elischer }
318630400f03SJulian Elischer 
318730400f03SJulian Elischer void
318830400f03SJulian Elischer dumpnode(node_p node, char *file, int line)
318930400f03SJulian Elischer {
319030400f03SJulian Elischer 	printf("node: ID [%x]: type '%s', %d hooks, flags 0x%x, %d refs, %s:\n",
31916b795970SJulian Elischer 		_NG_NODE_ID(node), node->nd_type->name,
319230400f03SJulian Elischer 		node->nd_numhooks, node->nd_flags,
319330400f03SJulian Elischer 		node->nd_refs, node->nd_name);
319430400f03SJulian Elischer 	printf("	Last active @ %s, line %d\n",
319530400f03SJulian Elischer 		node->lastfile, node->lastline);
319630400f03SJulian Elischer 	if (line) {
319730400f03SJulian Elischer 		printf(" problem discovered at file %s, line %d\n", file, line);
319830400f03SJulian Elischer 	}
319930400f03SJulian Elischer }
320030400f03SJulian Elischer 
3201069154d5SJulian Elischer void
3202069154d5SJulian Elischer dumpitem(item_p item, char *file, int line)
3203069154d5SJulian Elischer {
3204069154d5SJulian Elischer 	if (item->el_flags & NGQF_FREE) {
3205069154d5SJulian Elischer 		printf(" Free item, freed at %s, line %d\n",
3206069154d5SJulian Elischer 			item->lastfile, item->lastline);
3207069154d5SJulian Elischer 	} else {
3208069154d5SJulian Elischer 		printf(" ACTIVE item, last used at %s, line %d",
3209069154d5SJulian Elischer 			item->lastfile, item->lastline);
32106b795970SJulian Elischer 		switch(item->el_flags & NGQF_TYPE) {
32116b795970SJulian Elischer 		case NGQF_DATA:
3212069154d5SJulian Elischer 			printf(" - [data]\n");
32136b795970SJulian Elischer 			break;
32146b795970SJulian Elischer 		case NGQF_MESG:
32156b795970SJulian Elischer 			printf(" - retaddr[%d]:\n", _NGI_RETADDR(item));
32166b795970SJulian Elischer 			break;
32176b795970SJulian Elischer 		case NGQF_FN:
32186b795970SJulian Elischer 			printf(" - fn@%p (%p, %p, %p, %d (%x))\n",
32196b795970SJulian Elischer 				item->body.fn.fn_fn,
32201acb27c6SJulian Elischer 				NGI_NODE(item),
32211acb27c6SJulian Elischer 				NGI_HOOK(item),
32226b795970SJulian Elischer 				item->body.fn.fn_arg1,
32236b795970SJulian Elischer 				item->body.fn.fn_arg2,
32246b795970SJulian Elischer 				item->body.fn.fn_arg2);
32256b795970SJulian Elischer 			break;
32266b795970SJulian Elischer 		case NGQF_UNDEF:
32276b795970SJulian Elischer 			printf(" - UNDEFINED!\n");
3228069154d5SJulian Elischer 		}
3229069154d5SJulian Elischer 	}
323030400f03SJulian Elischer 	if (line) {
3231069154d5SJulian Elischer 		printf(" problem discovered at file %s, line %d\n", file, line);
32321acb27c6SJulian Elischer 		if (NGI_NODE(item)) {
323330400f03SJulian Elischer 			printf("node %p ([%x])\n",
32341acb27c6SJulian Elischer 				NGI_NODE(item), ng_node2ID(NGI_NODE(item)));
3235069154d5SJulian Elischer 		}
323630400f03SJulian Elischer 	}
323730400f03SJulian Elischer }
323830400f03SJulian Elischer 
323930400f03SJulian Elischer static void
324030400f03SJulian Elischer ng_dumpitems(void)
324130400f03SJulian Elischer {
324230400f03SJulian Elischer 	item_p item;
324330400f03SJulian Elischer 	int i = 1;
324430400f03SJulian Elischer 	TAILQ_FOREACH(item, &ng_itemlist, all) {
324530400f03SJulian Elischer 		printf("[%d] ", i++);
324630400f03SJulian Elischer 		dumpitem(item, NULL, 0);
324730400f03SJulian Elischer 	}
324830400f03SJulian Elischer }
324930400f03SJulian Elischer 
325030400f03SJulian Elischer static void
325130400f03SJulian Elischer ng_dumpnodes(void)
325230400f03SJulian Elischer {
325330400f03SJulian Elischer 	node_p node;
325430400f03SJulian Elischer 	int i = 1;
325530400f03SJulian Elischer 	SLIST_FOREACH(node, &ng_allnodes, nd_all) {
325630400f03SJulian Elischer 		printf("[%d] ", i++);
325730400f03SJulian Elischer 		dumpnode(node, NULL, 0);
325830400f03SJulian Elischer 	}
325930400f03SJulian Elischer }
326030400f03SJulian Elischer 
326130400f03SJulian Elischer static void
326230400f03SJulian Elischer ng_dumphooks(void)
326330400f03SJulian Elischer {
326430400f03SJulian Elischer 	hook_p hook;
326530400f03SJulian Elischer 	int i = 1;
326630400f03SJulian Elischer 	SLIST_FOREACH(hook, &ng_allhooks, hk_all) {
326730400f03SJulian Elischer 		printf("[%d] ", i++);
326830400f03SJulian Elischer 		dumphook(hook, NULL, 0);
326930400f03SJulian Elischer 	}
327030400f03SJulian Elischer }
3271069154d5SJulian Elischer 
3272069154d5SJulian Elischer static int
3273069154d5SJulian Elischer sysctl_debug_ng_dump_items(SYSCTL_HANDLER_ARGS)
3274069154d5SJulian Elischer {
3275069154d5SJulian Elischer 	int error;
3276069154d5SJulian Elischer 	int val;
3277069154d5SJulian Elischer 	int i;
3278069154d5SJulian Elischer 
3279069154d5SJulian Elischer 	val = allocated;
3280069154d5SJulian Elischer 	i = 1;
3281069154d5SJulian Elischer 	error = sysctl_handle_int(oidp, &val, sizeof(int), req);
32826b795970SJulian Elischer 	if (error != 0 || req->newptr == NULL)
32836b795970SJulian Elischer 		return (error);
32846b795970SJulian Elischer 	if (val == 42) {
328530400f03SJulian Elischer 		ng_dumpitems();
328630400f03SJulian Elischer 		ng_dumpnodes();
328730400f03SJulian Elischer 		ng_dumphooks();
3288069154d5SJulian Elischer 	}
32896b795970SJulian Elischer 	return (0);
3290069154d5SJulian Elischer }
3291069154d5SJulian Elischer 
32926b795970SJulian Elischer SYSCTL_PROC(_debug, OID_AUTO, ng_dump_items, CTLTYPE_INT | CTLFLAG_RW,
32936b795970SJulian Elischer     0, sizeof(int), sysctl_debug_ng_dump_items, "I", "Number of allocated items");
329430400f03SJulian Elischer #endif	/* NETGRAPH_DEBUG */
3295069154d5SJulian Elischer 
3296069154d5SJulian Elischer 
3297069154d5SJulian Elischer /***********************************************************************
3298069154d5SJulian Elischer * Worklist routines
3299069154d5SJulian Elischer **********************************************************************/
3300069154d5SJulian Elischer /* NETISR thread enters here */
3301069154d5SJulian Elischer /*
3302069154d5SJulian Elischer  * Pick a node off the list of nodes with work,
3303069154d5SJulian Elischer  * try get an item to process off it.
3304069154d5SJulian Elischer  * If there are no more, remove the node from the list.
3305069154d5SJulian Elischer  */
3306069154d5SJulian Elischer static void
3307069154d5SJulian Elischer ngintr(void)
3308069154d5SJulian Elischer {
3309069154d5SJulian Elischer 	item_p item;
3310069154d5SJulian Elischer 	node_p  node = NULL;
3311069154d5SJulian Elischer 
3312069154d5SJulian Elischer 	for (;;) {
33139ed346baSBosko Milekic 		mtx_lock_spin(&ng_worklist_mtx);
3314069154d5SJulian Elischer 		node = TAILQ_FIRST(&ng_worklist);
3315069154d5SJulian Elischer 		if (!node) {
33169ed346baSBosko Milekic 			mtx_unlock_spin(&ng_worklist_mtx);
3317069154d5SJulian Elischer 			break;
3318069154d5SJulian Elischer 		}
3319b57a7965SJulian Elischer 		node->nd_flags &= ~NG_WORKQ;
332030400f03SJulian Elischer 		TAILQ_REMOVE(&ng_worklist, node, nd_work);
33219ed346baSBosko Milekic 		mtx_unlock_spin(&ng_worklist_mtx);
3322069154d5SJulian Elischer 		/*
3323069154d5SJulian Elischer 		 * We have the node. We also take over the reference
3324069154d5SJulian Elischer 		 * that the list had on it.
3325069154d5SJulian Elischer 		 * Now process as much as you can, until it won't
3326069154d5SJulian Elischer 		 * let you have another item off the queue.
3327069154d5SJulian Elischer 		 * All this time, keep the reference
3328069154d5SJulian Elischer 		 * that lets us be sure that the node still exists.
3329069154d5SJulian Elischer 		 * Let the reference go at the last minute.
3330b57a7965SJulian Elischer 		 * ng_dequeue will put us back on the worklist
3331b57a7965SJulian Elischer 		 * if there is more too do. This may be of use if there
3332b57a7965SJulian Elischer 		 * are Multiple Processors and multiple Net threads in the
3333b57a7965SJulian Elischer 		 * future.
3334069154d5SJulian Elischer 		 */
3335069154d5SJulian Elischer 		for (;;) {
33369ed346baSBosko Milekic 			mtx_lock_spin(&node->nd_input_queue.q_mtx);
333730400f03SJulian Elischer 			item = ng_dequeue(&node->nd_input_queue);
3338069154d5SJulian Elischer 			if (item == NULL) {
33399ed346baSBosko Milekic 				mtx_unlock_spin(&node->nd_input_queue.q_mtx);
3340069154d5SJulian Elischer 				break; /* go look for another node */
3341069154d5SJulian Elischer 			} else {
33429ed346baSBosko Milekic 				mtx_unlock_spin(&node->nd_input_queue.q_mtx);
33435951069aSJulian Elischer 				NGI_GET_NODE(item, node); /* zaps stored node */
33445951069aSJulian Elischer 				ng_apply_item(node, item);
33455951069aSJulian Elischer 				NG_NODE_UNREF(node);
3346069154d5SJulian Elischer 			}
3347069154d5SJulian Elischer 		}
3348a96dcd84SJulian Elischer 		NG_NODE_UNREF(node);
3349069154d5SJulian Elischer 	}
3350069154d5SJulian Elischer }
3351069154d5SJulian Elischer 
3352069154d5SJulian Elischer static void
3353069154d5SJulian Elischer ng_worklist_remove(node_p node)
3354069154d5SJulian Elischer {
33559ed346baSBosko Milekic 	mtx_lock_spin(&ng_worklist_mtx);
335630400f03SJulian Elischer 	if (node->nd_flags & NG_WORKQ) {
335730400f03SJulian Elischer 		node->nd_flags &= ~NG_WORKQ;
335833338e73SJulian Elischer 		TAILQ_REMOVE(&ng_worklist, node, nd_work);
335933338e73SJulian Elischer 		mtx_unlock_spin(&ng_worklist_mtx);
336033338e73SJulian Elischer 		NG_NODE_UNREF(node);
336133338e73SJulian Elischer 	} else {
33629ed346baSBosko Milekic 		mtx_unlock_spin(&ng_worklist_mtx);
3363069154d5SJulian Elischer 	}
336433338e73SJulian Elischer }
3365069154d5SJulian Elischer 
336633338e73SJulian Elischer /*
336733338e73SJulian Elischer  * XXX
336833338e73SJulian Elischer  * It's posible that a debugging NG_NODE_REF may need
336933338e73SJulian Elischer  * to be outside the mutex zone
337033338e73SJulian Elischer  */
3371069154d5SJulian Elischer static void
3372069154d5SJulian Elischer ng_setisr(node_p node)
3373069154d5SJulian Elischer {
33749ed346baSBosko Milekic 	mtx_lock_spin(&ng_worklist_mtx);
337530400f03SJulian Elischer 	if ((node->nd_flags & NG_WORKQ) == 0) {
3376069154d5SJulian Elischer 		/*
3377069154d5SJulian Elischer 		 * If we are not already on the work queue,
3378069154d5SJulian Elischer 		 * then put us on.
3379069154d5SJulian Elischer 		 */
338030400f03SJulian Elischer 		node->nd_flags |= NG_WORKQ;
338130400f03SJulian Elischer 		TAILQ_INSERT_TAIL(&ng_worklist, node, nd_work);
338233338e73SJulian Elischer 		NG_NODE_REF(node); /* XXX fafe in mutex? */
3383069154d5SJulian Elischer 	}
33849ed346baSBosko Milekic 	mtx_unlock_spin(&ng_worklist_mtx);
3385069154d5SJulian Elischer 	schednetisr(NETISR_NETGRAPH);
3386069154d5SJulian Elischer }
3387069154d5SJulian Elischer 
3388069154d5SJulian Elischer 
3389069154d5SJulian Elischer /***********************************************************************
3390069154d5SJulian Elischer * Externally useable functions to set up a queue item ready for sending
3391069154d5SJulian Elischer ***********************************************************************/
3392069154d5SJulian Elischer 
339330400f03SJulian Elischer #ifdef	NETGRAPH_DEBUG
339430400f03SJulian Elischer #define	ITEM_DEBUG_CHECKS						\
33954cf49a43SJulian Elischer 	do {								\
33961acb27c6SJulian Elischer 		if (NGI_NODE(item) ) {					\
3397069154d5SJulian Elischer 			printf("item already has node");		\
3398069154d5SJulian Elischer 			Debugger("has node");				\
33991acb27c6SJulian Elischer 			NGI_CLR_NODE(item);				\
3400069154d5SJulian Elischer 		}							\
34011acb27c6SJulian Elischer 		if (NGI_HOOK(item) ) {					\
3402069154d5SJulian Elischer 			printf("item already has hook");		\
3403069154d5SJulian Elischer 			Debugger("has hook");				\
34041acb27c6SJulian Elischer 			NGI_CLR_HOOK(item);				\
3405069154d5SJulian Elischer 		}							\
3406069154d5SJulian Elischer 	} while (0)
3407069154d5SJulian Elischer #else
340830400f03SJulian Elischer #define ITEM_DEBUG_CHECKS
3409069154d5SJulian Elischer #endif
3410069154d5SJulian Elischer 
3411069154d5SJulian Elischer /*
3412069154d5SJulian Elischer  * Put elements into the item.
3413069154d5SJulian Elischer  * Hook and node references will be removed when the item is dequeued.
3414069154d5SJulian Elischer  * (or equivalent)
3415069154d5SJulian Elischer  * (XXX) Unsafe because no reference held by peer on remote node.
3416069154d5SJulian Elischer  * remote node might go away in this timescale.
3417069154d5SJulian Elischer  * We know the hooks can't go away because that would require getting
3418069154d5SJulian Elischer  * a writer item on both nodes and we must have at least a  reader
3419069154d5SJulian Elischer  * here to eb able to do this.
3420069154d5SJulian Elischer  * Note that the hook loaded is the REMOTE hook.
3421069154d5SJulian Elischer  *
3422069154d5SJulian Elischer  * This is possibly in the critical path for new data.
3423069154d5SJulian Elischer  */
3424069154d5SJulian Elischer item_p
3425069154d5SJulian Elischer ng_package_data(struct mbuf *m, meta_p meta)
3426069154d5SJulian Elischer {
3427069154d5SJulian Elischer 	item_p item;
3428069154d5SJulian Elischer 
3429069154d5SJulian Elischer 	if ((item = ng_getqblk()) == NULL) {
3430069154d5SJulian Elischer 		NG_FREE_M(m);
3431069154d5SJulian Elischer 		NG_FREE_META(meta);
3432069154d5SJulian Elischer 		return (NULL);
3433069154d5SJulian Elischer 	}
343430400f03SJulian Elischer 	ITEM_DEBUG_CHECKS;
3435069154d5SJulian Elischer 	item->el_flags = NGQF_DATA;
3436069154d5SJulian Elischer 	item->el_next = NULL;
3437069154d5SJulian Elischer 	NGI_M(item) = m;
3438069154d5SJulian Elischer 	NGI_META(item) = meta;
3439069154d5SJulian Elischer 	return (item);
3440069154d5SJulian Elischer }
3441069154d5SJulian Elischer 
3442069154d5SJulian Elischer /*
3443069154d5SJulian Elischer  * Allocate a queue item and put items into it..
3444069154d5SJulian Elischer  * Evaluate the address as this will be needed to queue it and
3445069154d5SJulian Elischer  * to work out what some of the fields should be.
3446069154d5SJulian Elischer  * Hook and node references will be removed when the item is dequeued.
3447069154d5SJulian Elischer  * (or equivalent)
3448069154d5SJulian Elischer  */
3449069154d5SJulian Elischer item_p
3450069154d5SJulian Elischer ng_package_msg(struct ng_mesg *msg)
3451069154d5SJulian Elischer {
3452069154d5SJulian Elischer 	item_p item;
3453069154d5SJulian Elischer 
3454069154d5SJulian Elischer 	if ((item = ng_getqblk()) == NULL) {
3455069154d5SJulian Elischer 		NG_FREE_MSG(msg);
3456069154d5SJulian Elischer 		return (NULL);
3457069154d5SJulian Elischer 	}
345830400f03SJulian Elischer 	ITEM_DEBUG_CHECKS;
3459069154d5SJulian Elischer 	item->el_flags = NGQF_MESG;
3460069154d5SJulian Elischer 	item->el_next = NULL;
3461069154d5SJulian Elischer 	/*
3462069154d5SJulian Elischer 	 * Set the current lasthook into the queue item
3463069154d5SJulian Elischer 	 */
3464069154d5SJulian Elischer 	NGI_MSG(item) = msg;
3465facfd889SArchie Cobbs 	NGI_RETADDR(item) = 0;
3466069154d5SJulian Elischer 	return (item);
3467069154d5SJulian Elischer }
3468069154d5SJulian Elischer 
3469069154d5SJulian Elischer 
3470069154d5SJulian Elischer 
34711acb27c6SJulian Elischer #define SET_RETADDR(item, here, retaddr)				\
34726b795970SJulian Elischer 	do {	/* Data or fn items don't have retaddrs */		\
34736b795970SJulian Elischer 		if ((item->el_flags & NGQF_TYPE) == NGQF_MESG) {	\
3474069154d5SJulian Elischer 			if (retaddr) {					\
3475069154d5SJulian Elischer 				NGI_RETADDR(item) = retaddr;		\
34764cf49a43SJulian Elischer 			} else {					\
3477069154d5SJulian Elischer 				/*					\
3478069154d5SJulian Elischer 				 * The old return address should be ok.	\
3479069154d5SJulian Elischer 				 * If there isn't one, use the address	\
3480069154d5SJulian Elischer 				 * here.				\
3481069154d5SJulian Elischer 				 */					\
3482069154d5SJulian Elischer 				if (NGI_RETADDR(item) == 0) {		\
3483069154d5SJulian Elischer 					NGI_RETADDR(item)		\
3484069154d5SJulian Elischer 						= ng_node2ID(here);	\
3485069154d5SJulian Elischer 				}					\
3486069154d5SJulian Elischer 			}						\
34874cf49a43SJulian Elischer 		}							\
34884cf49a43SJulian Elischer 	} while (0)
34894cf49a43SJulian Elischer 
34904cf49a43SJulian Elischer int
3491069154d5SJulian Elischer ng_address_hook(node_p here, item_p item, hook_p hook, ng_ID_t retaddr)
34924cf49a43SJulian Elischer {
34931acb27c6SJulian Elischer 	hook_p peer;
34941acb27c6SJulian Elischer 	node_p peernode;
349530400f03SJulian Elischer 	ITEM_DEBUG_CHECKS;
3496069154d5SJulian Elischer 	/*
3497069154d5SJulian Elischer 	 * Quick sanity check..
349830400f03SJulian Elischer 	 * Since a hook holds a reference on it's node, once we know
349930400f03SJulian Elischer 	 * that the peer is still connected (even if invalid,) we know
350030400f03SJulian Elischer 	 * that the peer node is present, though maybe invalid.
3501069154d5SJulian Elischer 	 */
3502069154d5SJulian Elischer 	if ((hook == NULL)
350330400f03SJulian Elischer 	|| NG_HOOK_NOT_VALID(hook)
350430400f03SJulian Elischer 	|| (NG_HOOK_PEER(hook) == NULL)
350530400f03SJulian Elischer 	|| NG_HOOK_NOT_VALID(NG_HOOK_PEER(hook))
350630400f03SJulian Elischer 	|| NG_NODE_NOT_VALID(NG_PEER_NODE(hook))) {
3507069154d5SJulian Elischer 		NG_FREE_ITEM(item);
35086b795970SJulian Elischer 		TRAP_ERROR();
3509e08d3e3cSJulian Elischer 		return (ENETDOWN);
35104cf49a43SJulian Elischer 	}
35114cf49a43SJulian Elischer 
35124cf49a43SJulian Elischer 	/*
3513069154d5SJulian Elischer 	 * Transfer our interest to the other (peer) end.
35144cf49a43SJulian Elischer 	 */
35151acb27c6SJulian Elischer 	peer = NG_HOOK_PEER(hook);
35161acb27c6SJulian Elischer 	NG_HOOK_REF(peer);
35171acb27c6SJulian Elischer 	NGI_SET_HOOK(item, peer);
35181acb27c6SJulian Elischer 	peernode = NG_PEER_NODE(hook);
35191acb27c6SJulian Elischer 	NG_NODE_REF(peernode);
35201acb27c6SJulian Elischer 	NGI_SET_NODE(item, peernode);
35218b68f82fSJulian Elischer 	SET_RETADDR(item, here, retaddr);
3522069154d5SJulian Elischer 	return (0);
3523069154d5SJulian Elischer }
3524069154d5SJulian Elischer 
35254cf49a43SJulian Elischer int
3526069154d5SJulian Elischer ng_address_path(node_p here, item_p item, char *address, ng_ID_t retaddr)
35274cf49a43SJulian Elischer {
35284cf49a43SJulian Elischer 	node_p  dest = NULL;
3529069154d5SJulian Elischer 	hook_p	hook = NULL;
35304cf49a43SJulian Elischer 	int     error;
3531069154d5SJulian Elischer 
353230400f03SJulian Elischer 	ITEM_DEBUG_CHECKS;
3533069154d5SJulian Elischer 	/*
3534069154d5SJulian Elischer 	 * Note that ng_path2noderef increments the reference count
3535069154d5SJulian Elischer 	 * on the node for us if it finds one. So we don't have to.
3536069154d5SJulian Elischer 	 */
3537069154d5SJulian Elischer 	error = ng_path2noderef(here, address, &dest, &hook);
3538069154d5SJulian Elischer 	if (error) {
3539069154d5SJulian Elischer 		NG_FREE_ITEM(item);
354030400f03SJulian Elischer 		return (error);
3541069154d5SJulian Elischer 	}
35421acb27c6SJulian Elischer 	NGI_SET_NODE(item, dest);
35431acb27c6SJulian Elischer 	if ( hook) {
354430400f03SJulian Elischer 		NG_HOOK_REF(hook);	/* don't let it go while on the queue */
35451acb27c6SJulian Elischer 		NGI_SET_HOOK(item, hook);
35461acb27c6SJulian Elischer 	}
35471acb27c6SJulian Elischer 	SET_RETADDR(item, here, retaddr);
3548069154d5SJulian Elischer 	return (0);
3549069154d5SJulian Elischer }
3550069154d5SJulian Elischer 
3551069154d5SJulian Elischer int
3552069154d5SJulian Elischer ng_address_ID(node_p here, item_p item, ng_ID_t ID, ng_ID_t retaddr)
3553069154d5SJulian Elischer {
3554069154d5SJulian Elischer 	node_p dest;
3555069154d5SJulian Elischer 
355630400f03SJulian Elischer 	ITEM_DEBUG_CHECKS;
3557069154d5SJulian Elischer 	/*
3558069154d5SJulian Elischer 	 * Find the target node.
3559069154d5SJulian Elischer 	 */
3560069154d5SJulian Elischer 	dest = ng_ID2noderef(ID); /* GETS REFERENCE! */
3561069154d5SJulian Elischer 	if (dest == NULL) {
3562069154d5SJulian Elischer 		NG_FREE_ITEM(item);
35636b795970SJulian Elischer 		TRAP_ERROR();
3564069154d5SJulian Elischer 		return(EINVAL);
3565069154d5SJulian Elischer 	}
3566069154d5SJulian Elischer 	/* Fill out the contents */
3567069154d5SJulian Elischer 	item->el_flags = NGQF_MESG;
3568069154d5SJulian Elischer 	item->el_next = NULL;
35691acb27c6SJulian Elischer 	NGI_SET_NODE(item, dest);
35701acb27c6SJulian Elischer 	NGI_CLR_HOOK(item);
35711acb27c6SJulian Elischer 	SET_RETADDR(item, here, retaddr);
3572069154d5SJulian Elischer 	return (0);
3573069154d5SJulian Elischer }
3574069154d5SJulian Elischer 
3575069154d5SJulian Elischer /*
3576069154d5SJulian Elischer  * special case to send a message to self (e.g. destroy node)
3577069154d5SJulian Elischer  * Possibly indicate an arrival hook too.
3578069154d5SJulian Elischer  * Useful for removing that hook :-)
3579069154d5SJulian Elischer  */
3580069154d5SJulian Elischer item_p
3581069154d5SJulian Elischer ng_package_msg_self(node_p here, hook_p hook, struct ng_mesg *msg)
3582069154d5SJulian Elischer {
3583069154d5SJulian Elischer 	item_p item;
35844cf49a43SJulian Elischer 
3585859a4d16SJulian Elischer 	/*
3586859a4d16SJulian Elischer 	 * Find the target node.
3587859a4d16SJulian Elischer 	 * If there is a HOOK argument, then use that in preference
3588859a4d16SJulian Elischer 	 * to the address.
3589859a4d16SJulian Elischer 	 */
3590069154d5SJulian Elischer 	if ((item = ng_getqblk()) == NULL) {
3591069154d5SJulian Elischer 		NG_FREE_MSG(msg);
3592069154d5SJulian Elischer 		return (NULL);
35934cf49a43SJulian Elischer 	}
35944cf49a43SJulian Elischer 
35954cf49a43SJulian Elischer 	/* Fill out the contents */
3596069154d5SJulian Elischer 	item->el_flags = NGQF_MESG;
3597069154d5SJulian Elischer 	item->el_next = NULL;
359830400f03SJulian Elischer 	NG_NODE_REF(here);
35991acb27c6SJulian Elischer 	NGI_SET_NODE(item, here);
36001acb27c6SJulian Elischer 	if (hook) {
360130400f03SJulian Elischer 		NG_HOOK_REF(hook);
36021acb27c6SJulian Elischer 		NGI_SET_HOOK(item, hook);
36031acb27c6SJulian Elischer 	}
3604069154d5SJulian Elischer 	NGI_MSG(item) = msg;
3605069154d5SJulian Elischer 	NGI_RETADDR(item) = ng_node2ID(here);
3606069154d5SJulian Elischer 	return (item);
36074cf49a43SJulian Elischer }
36084cf49a43SJulian Elischer 
36096b795970SJulian Elischer int
36106b795970SJulian Elischer ng_send_fn(node_p node, hook_p hook, ng_item_fn *fn, void * arg1, int arg2)
36116b795970SJulian Elischer {
36126b795970SJulian Elischer 	item_p item;
36136b795970SJulian Elischer 
36146b795970SJulian Elischer 	if ((item = ng_getqblk()) == NULL) {
36156b795970SJulian Elischer 		return (ENOMEM);
36166b795970SJulian Elischer 	}
36176b795970SJulian Elischer 	item->el_flags = NGQF_FN | NGQF_WRITER;
3618a96dcd84SJulian Elischer 	NG_NODE_REF(node); /* and one for the item */
36191acb27c6SJulian Elischer 	NGI_SET_NODE(item, node);
36201acb27c6SJulian Elischer 	if (hook) {
36216b795970SJulian Elischer 		NG_HOOK_REF(hook);
36221acb27c6SJulian Elischer 		NGI_SET_HOOK(item, hook);
36236b795970SJulian Elischer 	}
36246b795970SJulian Elischer 	NGI_FN(item) = fn;
36256b795970SJulian Elischer 	NGI_ARG1(item) = arg1;
36266b795970SJulian Elischer 	NGI_ARG2(item) = arg2;
36276b795970SJulian Elischer 	return(ng_snd_item(item, 0));
36286b795970SJulian Elischer }
36296b795970SJulian Elischer 
36304cf49a43SJulian Elischer /*
3631d2ca21a9SJulian Elischer  * Official timeout routines for Netgraph nodes.
3632d2ca21a9SJulian Elischer  */
3633d2ca21a9SJulian Elischer static void
3634d2ca21a9SJulian Elischer ng_timeout_trapoline(void *arg)
3635d2ca21a9SJulian Elischer {
3636d2ca21a9SJulian Elischer 	item_p item = arg;
3637d2ca21a9SJulian Elischer 
3638d2ca21a9SJulian Elischer 	ng_snd_item(item, 0);
3639d2ca21a9SJulian Elischer }
3640d2ca21a9SJulian Elischer 
3641d2ca21a9SJulian Elischer 
3642d2ca21a9SJulian Elischer struct callout_handle
3643d2ca21a9SJulian Elischer ng_timeout(node_p node, hook_p hook, int ticks,
3644d2ca21a9SJulian Elischer     ng_item_fn *fn, void * arg1, int arg2)
3645d2ca21a9SJulian Elischer {
3646d2ca21a9SJulian Elischer 	item_p item;
3647d2ca21a9SJulian Elischer 
3648d2ca21a9SJulian Elischer 	if ((item = ng_getqblk()) == NULL) {
3649d2ca21a9SJulian Elischer 		struct callout_handle handle;
3650d2ca21a9SJulian Elischer 		handle.callout = NULL;
3651d2ca21a9SJulian Elischer 		return (handle);
3652d2ca21a9SJulian Elischer 	}
3653d2ca21a9SJulian Elischer 	item->el_flags = NGQF_FN | NGQF_WRITER;
3654d2ca21a9SJulian Elischer 	NG_NODE_REF(node);		/* and one for the item */
3655d2ca21a9SJulian Elischer 	NGI_SET_NODE(item, node);
3656d2ca21a9SJulian Elischer 	if (hook) {
3657d2ca21a9SJulian Elischer 		NG_HOOK_REF(hook);
3658d2ca21a9SJulian Elischer 		NGI_SET_HOOK(item, hook);
3659d2ca21a9SJulian Elischer 	}
3660d2ca21a9SJulian Elischer 	NGI_FN(item) = fn;
3661d2ca21a9SJulian Elischer 	NGI_ARG1(item) = arg1;
3662d2ca21a9SJulian Elischer 	NGI_ARG2(item) = arg2;
3663d2ca21a9SJulian Elischer 	return (timeout(&ng_timeout_trapoline, item, ticks));
3664d2ca21a9SJulian Elischer }
3665d2ca21a9SJulian Elischer 
3666d2ca21a9SJulian Elischer /* A special modified version of untimeout() */
3667d2ca21a9SJulian Elischer int
3668d2ca21a9SJulian Elischer ng_untimeout(struct callout_handle handle, node_p node)
3669d2ca21a9SJulian Elischer {
3670d2ca21a9SJulian Elischer 	item_p item;
3671d2ca21a9SJulian Elischer 
3672d2ca21a9SJulian Elischer 	if (handle.callout == NULL)
3673d2ca21a9SJulian Elischer 		return (0);
3674d2ca21a9SJulian Elischer 	mtx_lock_spin(&callout_lock);
3675d2ca21a9SJulian Elischer 	item = handle.callout->c_arg; /* should be an official way to do this */
3676d2ca21a9SJulian Elischer 	if ((handle.callout->c_func == &ng_timeout_trapoline) &&
3677d2ca21a9SJulian Elischer 	    (NGI_NODE(item) == node) &&
3678d2ca21a9SJulian Elischer 	    (callout_stop(handle.callout))) {
3679d2ca21a9SJulian Elischer 		/*
3680d2ca21a9SJulian Elischer 		 * We successfully removed it from the queue before it ran
3681d2ca21a9SJulian Elischer 		 * So now we need to unreference everything that was
3682d2ca21a9SJulian Elischer 		 * given extra references. (NG_FREE_ITEM does this).
3683d2ca21a9SJulian Elischer 		 */
3684d2ca21a9SJulian Elischer 		mtx_unlock_spin(&callout_lock);
3685d2ca21a9SJulian Elischer 		NG_FREE_ITEM(item);
3686d2ca21a9SJulian Elischer 		return (1);
3687d2ca21a9SJulian Elischer 	}
3688d2ca21a9SJulian Elischer 	mtx_unlock_spin(&callout_lock);
3689d2ca21a9SJulian Elischer 	return (0);
3690d2ca21a9SJulian Elischer }
3691d2ca21a9SJulian Elischer 
3692d2ca21a9SJulian Elischer /*
3693069154d5SJulian Elischer  * Set the address, if none given, give the node here.
36944cf49a43SJulian Elischer  */
3695069154d5SJulian Elischer void
3696069154d5SJulian Elischer ng_replace_retaddr(node_p here, item_p item, ng_ID_t retaddr)
36974cf49a43SJulian Elischer {
3698069154d5SJulian Elischer 	if (retaddr) {
3699069154d5SJulian Elischer 		NGI_RETADDR(item) = retaddr;
3700069154d5SJulian Elischer 	} else {
3701069154d5SJulian Elischer 		/*
3702069154d5SJulian Elischer 		 * The old return address should be ok.
3703069154d5SJulian Elischer 		 * If there isn't one, use the address here.
3704069154d5SJulian Elischer 		 */
3705069154d5SJulian Elischer 		NGI_RETADDR(item) = ng_node2ID(here);
3706069154d5SJulian Elischer 	}
3707069154d5SJulian Elischer }
3708069154d5SJulian Elischer 
3709069154d5SJulian Elischer #define TESTING
3710069154d5SJulian Elischer #ifdef TESTING
3711069154d5SJulian Elischer /* just test all the macros */
3712069154d5SJulian Elischer void
3713069154d5SJulian Elischer ng_macro_test(item_p item);
3714069154d5SJulian Elischer void
3715069154d5SJulian Elischer ng_macro_test(item_p item)
3716069154d5SJulian Elischer {
3717069154d5SJulian Elischer 	node_p node = NULL;
3718069154d5SJulian Elischer 	hook_p hook = NULL;
37194cf49a43SJulian Elischer 	struct mbuf *m;
37204cf49a43SJulian Elischer 	meta_p meta;
37214cf49a43SJulian Elischer 	struct ng_mesg *msg;
3722069154d5SJulian Elischer 	ng_ID_t retaddr;
3723069154d5SJulian Elischer 	int	error;
37244cf49a43SJulian Elischer 
3725069154d5SJulian Elischer 	NGI_GET_M(item, m);
3726069154d5SJulian Elischer 	NGI_GET_META(item, meta);
3727069154d5SJulian Elischer 	NGI_GET_MSG(item, msg);
3728069154d5SJulian Elischer 	retaddr = NGI_RETADDR(item);
3729069154d5SJulian Elischer 	NG_SEND_DATA(error, hook, m, meta);
3730069154d5SJulian Elischer 	NG_SEND_DATA_ONLY(error, hook, m);
3731069154d5SJulian Elischer 	NG_FWD_NEW_DATA(error, item, hook, m);
373230400f03SJulian Elischer 	NG_FWD_ITEM_HOOK(error, item, hook);
3733069154d5SJulian Elischer 	NG_SEND_MSG_HOOK(error, node, msg, hook, retaddr);
3734069154d5SJulian Elischer 	NG_SEND_MSG_ID(error, node, msg, retaddr, retaddr);
3735069154d5SJulian Elischer 	NG_SEND_MSG_PATH(error, node, msg, ".:", retaddr);
3736069154d5SJulian Elischer 	NG_FWD_MSG_HOOK(error, node, item, hook, retaddr);
37374cf49a43SJulian Elischer }
3738069154d5SJulian Elischer #endif /* TESTING */
37394cf49a43SJulian Elischer 
3740