xref: /freebsd/sys/netgraph/ng_base.c (revision ba5b359aef106eee2d7ccdb0feefadfe8a8156f5)
14cf49a43SJulian Elischer /*
24cf49a43SJulian Elischer  * ng_base.c
3c398230bSWarner Losh  */
4c398230bSWarner Losh 
5c398230bSWarner Losh /*-
64cf49a43SJulian Elischer  * Copyright (c) 1996-1999 Whistle Communications, Inc.
74cf49a43SJulian Elischer  * All rights reserved.
84cf49a43SJulian Elischer  *
94cf49a43SJulian Elischer  * Subject to the following obligations and disclaimer of warranty, use and
104cf49a43SJulian Elischer  * redistribution of this software, in source or object code forms, with or
114cf49a43SJulian Elischer  * without modifications are expressly permitted by Whistle Communications;
124cf49a43SJulian Elischer  * provided, however, that:
134cf49a43SJulian Elischer  * 1. Any and all reproductions of the source or object code must include the
144cf49a43SJulian Elischer  *    copyright notice above and the following disclaimer of warranties; and
154cf49a43SJulian Elischer  * 2. No rights are granted, in any manner or form, to use Whistle
164cf49a43SJulian Elischer  *    Communications, Inc. trademarks, including the mark "WHISTLE
174cf49a43SJulian Elischer  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
184cf49a43SJulian Elischer  *    such appears in the above copyright notice or in the software.
194cf49a43SJulian Elischer  *
204cf49a43SJulian Elischer  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
214cf49a43SJulian Elischer  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
224cf49a43SJulian Elischer  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
234cf49a43SJulian Elischer  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
244cf49a43SJulian Elischer  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
254cf49a43SJulian Elischer  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
264cf49a43SJulian Elischer  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
274cf49a43SJulian Elischer  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
284cf49a43SJulian Elischer  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
294cf49a43SJulian Elischer  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
304cf49a43SJulian Elischer  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
314cf49a43SJulian Elischer  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
324cf49a43SJulian Elischer  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
334cf49a43SJulian Elischer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
344cf49a43SJulian Elischer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
354cf49a43SJulian Elischer  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
364cf49a43SJulian Elischer  * OF SUCH DAMAGE.
374cf49a43SJulian Elischer  *
38cc3bbd68SJulian Elischer  * Authors: Julian Elischer <julian@freebsd.org>
39cc3bbd68SJulian Elischer  *          Archie Cobbs <archie@freebsd.org>
404cf49a43SJulian Elischer  *
414cf49a43SJulian Elischer  * $FreeBSD$
424cf49a43SJulian Elischer  * $Whistle: ng_base.c,v 1.39 1999/01/28 23:54:53 julian Exp $
434cf49a43SJulian Elischer  */
444cf49a43SJulian Elischer 
454cf49a43SJulian Elischer /*
464cf49a43SJulian Elischer  * This file implements the base netgraph code.
474cf49a43SJulian Elischer  */
484cf49a43SJulian Elischer 
494cf49a43SJulian Elischer #include <sys/param.h>
508e853a2cSGleb Smirnoff #include <sys/systm.h>
5177a58296SGleb Smirnoff #include <sys/ctype.h>
524cf49a43SJulian Elischer #include <sys/errno.h>
53f33ca0c9SMarcel Moolenaar #include <sys/kdb.h>
544cf49a43SJulian Elischer #include <sys/kernel.h>
55104a9b7eSAlexander Kabaev #include <sys/limits.h>
564cf49a43SJulian Elischer #include <sys/malloc.h>
574cf49a43SJulian Elischer #include <sys/mbuf.h>
5877a58296SGleb Smirnoff #include <sys/queue.h>
59bfa7e882SJulian Elischer #include <sys/sysctl.h>
6077a58296SGleb Smirnoff #include <sys/syslog.h>
614cf49a43SJulian Elischer 
624cf49a43SJulian Elischer #include <net/netisr.h>
634cf49a43SJulian Elischer 
644cf49a43SJulian Elischer #include <netgraph/ng_message.h>
654cf49a43SJulian Elischer #include <netgraph/netgraph.h>
66f8307e12SArchie Cobbs #include <netgraph/ng_parse.h>
674cf49a43SJulian Elischer 
689d72a7a3SJulian Elischer MODULE_VERSION(netgraph, NG_ABI_VERSION);
6999ff8176SPeter Wemm 
7030400f03SJulian Elischer /* List of all active nodes */
71069154d5SJulian Elischer static LIST_HEAD(, ng_node) ng_nodelist;
72069154d5SJulian Elischer static struct mtx	ng_nodelist_mtx;
73069154d5SJulian Elischer 
7430400f03SJulian Elischer #ifdef	NETGRAPH_DEBUG
751489164fSGleb Smirnoff static struct mtx		ngq_mtx;	/* protects the queue item list */
7630400f03SJulian Elischer 
7730400f03SJulian Elischer static SLIST_HEAD(, ng_node) ng_allnodes;
7830400f03SJulian Elischer static LIST_HEAD(, ng_node) ng_freenodes; /* in debug, we never free() them */
7930400f03SJulian Elischer static SLIST_HEAD(, ng_hook) ng_allhooks;
8030400f03SJulian Elischer static LIST_HEAD(, ng_hook) ng_freehooks; /* in debug, we never free() them */
8130400f03SJulian Elischer 
8230400f03SJulian Elischer static void ng_dumpitems(void);
8330400f03SJulian Elischer static void ng_dumpnodes(void);
8430400f03SJulian Elischer static void ng_dumphooks(void);
8530400f03SJulian Elischer 
8630400f03SJulian Elischer #endif	/* NETGRAPH_DEBUG */
87954c4772SJulian Elischer /*
88954c4772SJulian Elischer  * DEAD versions of the structures.
89954c4772SJulian Elischer  * In order to avoid races, it is sometimes neccesary to point
90954c4772SJulian Elischer  * at SOMETHING even though theoretically, the current entity is
91954c4772SJulian Elischer  * INVALID. Use these to avoid these races.
92954c4772SJulian Elischer  */
93954c4772SJulian Elischer struct ng_type ng_deadtype = {
94954c4772SJulian Elischer 	NG_ABI_VERSION,
95954c4772SJulian Elischer 	"dead",
96954c4772SJulian Elischer 	NULL,	/* modevent */
97954c4772SJulian Elischer 	NULL,	/* constructor */
98954c4772SJulian Elischer 	NULL,	/* rcvmsg */
99954c4772SJulian Elischer 	NULL,	/* shutdown */
100954c4772SJulian Elischer 	NULL,	/* newhook */
101954c4772SJulian Elischer 	NULL,	/* findhook */
102954c4772SJulian Elischer 	NULL,	/* connect */
103954c4772SJulian Elischer 	NULL,	/* rcvdata */
104954c4772SJulian Elischer 	NULL,	/* disconnect */
105954c4772SJulian Elischer 	NULL, 	/* cmdlist */
106954c4772SJulian Elischer };
10730400f03SJulian Elischer 
108954c4772SJulian Elischer struct ng_node ng_deadnode = {
109954c4772SJulian Elischer 	"dead",
110954c4772SJulian Elischer 	&ng_deadtype,
111be4252b3SJulian Elischer 	NGF_INVALID,
112954c4772SJulian Elischer 	1,	/* refs */
113954c4772SJulian Elischer 	0,	/* numhooks */
114954c4772SJulian Elischer 	NULL,	/* private */
115954c4772SJulian Elischer 	0,	/* ID */
116954c4772SJulian Elischer 	LIST_HEAD_INITIALIZER(ng_deadnode.hooks),
117954c4772SJulian Elischer 	{},	/* all_nodes list entry */
118954c4772SJulian Elischer 	{},	/* id hashtable list entry */
119954c4772SJulian Elischer 	{},	/* workqueue entry */
120954c4772SJulian Elischer 	{	0,
121954c4772SJulian Elischer 		{}, /* should never use! (should hang) */
122954c4772SJulian Elischer 		NULL,
123954c4772SJulian Elischer 		&ng_deadnode.nd_input_queue.queue,
124954c4772SJulian Elischer 		&ng_deadnode
125954c4772SJulian Elischer 	},
126954c4772SJulian Elischer #ifdef	NETGRAPH_DEBUG
127954c4772SJulian Elischer 	ND_MAGIC,
128954c4772SJulian Elischer 	__FILE__,
129954c4772SJulian Elischer 	__LINE__,
130954c4772SJulian Elischer 	{NULL}
131954c4772SJulian Elischer #endif	/* NETGRAPH_DEBUG */
132954c4772SJulian Elischer };
133954c4772SJulian Elischer 
134954c4772SJulian Elischer struct ng_hook ng_deadhook = {
135954c4772SJulian Elischer 	"dead",
136954c4772SJulian Elischer 	NULL,		/* private */
137954c4772SJulian Elischer 	HK_INVALID | HK_DEAD,
138954c4772SJulian Elischer 	1,		/* refs always >= 1 */
139e58d779dSGleb Smirnoff 	0,		/* undefined data link type */
140954c4772SJulian Elischer 	&ng_deadhook,	/* Peer is self */
141954c4772SJulian Elischer 	&ng_deadnode,	/* attached to deadnode */
142954c4772SJulian Elischer 	{},		/* hooks list */
143c4b5eea4SJulian Elischer 	NULL,		/* override rcvmsg() */
144c4b5eea4SJulian Elischer 	NULL,		/* override rcvdata() */
145954c4772SJulian Elischer #ifdef	NETGRAPH_DEBUG
146954c4772SJulian Elischer 	HK_MAGIC,
147954c4772SJulian Elischer 	__FILE__,
148954c4772SJulian Elischer 	__LINE__,
149954c4772SJulian Elischer 	{NULL}
150954c4772SJulian Elischer #endif	/* NETGRAPH_DEBUG */
151954c4772SJulian Elischer };
152954c4772SJulian Elischer 
153954c4772SJulian Elischer /*
154954c4772SJulian Elischer  * END DEAD STRUCTURES
155954c4772SJulian Elischer  */
156069154d5SJulian Elischer /* List nodes with unallocated work */
157069154d5SJulian Elischer static TAILQ_HEAD(, ng_node) ng_worklist = TAILQ_HEAD_INITIALIZER(ng_worklist);
158b57a7965SJulian Elischer static struct mtx	ng_worklist_mtx;   /* MUST LOCK NODE FIRST */
1594cf49a43SJulian Elischer 
1604cf49a43SJulian Elischer /* List of installed types */
161069154d5SJulian Elischer static LIST_HEAD(, ng_type) ng_typelist;
162069154d5SJulian Elischer static struct mtx	ng_typelist_mtx;
1634cf49a43SJulian Elischer 
164069154d5SJulian Elischer /* Hash related definitions */
1650f150d04SJulian Elischer /* XXX Don't need to initialise them because it's a LIST */
1660f150d04SJulian Elischer #define NG_ID_HASH_SIZE 32 /* most systems wont need even this many */
1670f150d04SJulian Elischer static LIST_HEAD(, ng_node) ng_ID_hash[NG_ID_HASH_SIZE];
168069154d5SJulian Elischer static struct mtx	ng_idhash_mtx;
1690f150d04SJulian Elischer /* Method to find a node.. used twice so do it here */
1700f150d04SJulian Elischer #define NG_IDHASH_FN(ID) ((ID) % (NG_ID_HASH_SIZE))
1710f150d04SJulian Elischer #define NG_IDHASH_FIND(ID, node)					\
1720f150d04SJulian Elischer 	do { 								\
17353f9c5e9SRobert Watson 		mtx_assert(&ng_idhash_mtx, MA_OWNED);			\
1740f150d04SJulian Elischer 		LIST_FOREACH(node, &ng_ID_hash[NG_IDHASH_FN(ID)],	\
1750f150d04SJulian Elischer 						nd_idnodes) {		\
1760f150d04SJulian Elischer 			if (NG_NODE_IS_VALID(node)			\
1770f150d04SJulian Elischer 			&& (NG_NODE_ID(node) == ID)) {			\
1780f150d04SJulian Elischer 				break;					\
1790f150d04SJulian Elischer 			}						\
1800f150d04SJulian Elischer 		}							\
1810f150d04SJulian Elischer 	} while (0)
182069154d5SJulian Elischer 
183dc90cad9SJulian Elischer 
1844cf49a43SJulian Elischer /* Internal functions */
1854cf49a43SJulian Elischer static int	ng_add_hook(node_p node, const char *name, hook_p * hookp);
186069154d5SJulian Elischer static int	ng_generic_msg(node_p here, item_p item, hook_p lasthook);
187dc90cad9SJulian Elischer static ng_ID_t	ng_decodeidname(const char *name);
1884cf49a43SJulian Elischer static int	ngb_mod_event(module_t mod, int event, void *data);
189069154d5SJulian Elischer static void	ng_worklist_remove(node_p node);
1904cf49a43SJulian Elischer static void	ngintr(void);
1915951069aSJulian Elischer static int	ng_apply_item(node_p node, item_p item);
192069154d5SJulian Elischer static void	ng_flush_input_queue(struct ng_queue * ngq);
193069154d5SJulian Elischer static void	ng_setisr(node_p node);
194069154d5SJulian Elischer static node_p	ng_ID2noderef(ng_ID_t ID);
1956b795970SJulian Elischer static int	ng_con_nodes(node_p node, const char *name, node_p node2,
1966b795970SJulian Elischer 							const char *name2);
1971acb27c6SJulian Elischer static void	ng_con_part2(node_p node, hook_p hook, void *arg1, int arg2);
1981acb27c6SJulian Elischer static void	ng_con_part3(node_p node, hook_p hook, void *arg1, int arg2);
1996b795970SJulian Elischer static int	ng_mkpeer(node_p node, const char *name,
2006b795970SJulian Elischer 						const char *name2, char *type);
201069154d5SJulian Elischer 
20230400f03SJulian Elischer /* imported , these used to be externally visible, some may go back */
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 int	ng_make_node(const char *type, node_p *nodepp);
208069154d5SJulian Elischer int	ng_path_parse(char *addr, char **node, char **path, char **hook);
2091acb27c6SJulian Elischer void	ng_rmnode(node_p node, hook_p dummy1, void *dummy2, int dummy3);
21030400f03SJulian Elischer void	ng_unname(node_p node);
211069154d5SJulian Elischer 
2124cf49a43SJulian Elischer 
2134cf49a43SJulian Elischer /* Our own netgraph malloc type */
2144cf49a43SJulian Elischer MALLOC_DEFINE(M_NETGRAPH, "netgraph", "netgraph structures and ctrl messages");
215069154d5SJulian Elischer MALLOC_DEFINE(M_NETGRAPH_HOOK, "netgraph_hook", "netgraph hook structures");
216069154d5SJulian Elischer MALLOC_DEFINE(M_NETGRAPH_NODE, "netgraph_node", "netgraph node structures");
217069154d5SJulian Elischer MALLOC_DEFINE(M_NETGRAPH_ITEM, "netgraph_item", "netgraph item structures");
218069154d5SJulian Elischer MALLOC_DEFINE(M_NETGRAPH_MSG, "netgraph_msg", "netgraph name storage");
219069154d5SJulian Elischer 
220069154d5SJulian Elischer /* Should not be visible outside this file */
22130400f03SJulian Elischer 
22230400f03SJulian Elischer #define _NG_ALLOC_HOOK(hook) \
22330400f03SJulian Elischer 	MALLOC(hook, hook_p, sizeof(*hook), M_NETGRAPH_HOOK, M_NOWAIT | M_ZERO)
22430400f03SJulian Elischer #define _NG_ALLOC_NODE(node) \
22530400f03SJulian Elischer 	MALLOC(node, node_p, sizeof(*node), M_NETGRAPH_NODE, M_NOWAIT | M_ZERO)
22630400f03SJulian Elischer 
22730400f03SJulian Elischer #ifdef NETGRAPH_DEBUG /*----------------------------------------------*/
22830400f03SJulian Elischer /*
22930400f03SJulian Elischer  * In debug mode:
23030400f03SJulian Elischer  * In an attempt to help track reference count screwups
23130400f03SJulian Elischer  * we do not free objects back to the malloc system, but keep them
23230400f03SJulian Elischer  * in a local cache where we can examine them and keep information safely
23330400f03SJulian Elischer  * after they have been freed.
23430400f03SJulian Elischer  * We use this scheme for nodes and hooks, and to some extent for items.
23530400f03SJulian Elischer  */
23630400f03SJulian Elischer static __inline hook_p
23730400f03SJulian Elischer ng_alloc_hook(void)
23830400f03SJulian Elischer {
23930400f03SJulian Elischer 	hook_p hook;
24030400f03SJulian Elischer 	SLIST_ENTRY(ng_hook) temp;
2419ed346baSBosko Milekic 	mtx_lock(&ng_nodelist_mtx);
24230400f03SJulian Elischer 	hook = LIST_FIRST(&ng_freehooks);
24330400f03SJulian Elischer 	if (hook) {
24430400f03SJulian Elischer 		LIST_REMOVE(hook, hk_hooks);
24530400f03SJulian Elischer 		bcopy(&hook->hk_all, &temp, sizeof(temp));
24630400f03SJulian Elischer 		bzero(hook, sizeof(struct ng_hook));
24730400f03SJulian Elischer 		bcopy(&temp, &hook->hk_all, sizeof(temp));
2489ed346baSBosko Milekic 		mtx_unlock(&ng_nodelist_mtx);
24930400f03SJulian Elischer 		hook->hk_magic = HK_MAGIC;
25030400f03SJulian Elischer 	} else {
2519ed346baSBosko Milekic 		mtx_unlock(&ng_nodelist_mtx);
25230400f03SJulian Elischer 		_NG_ALLOC_HOOK(hook);
25330400f03SJulian Elischer 		if (hook) {
25430400f03SJulian Elischer 			hook->hk_magic = HK_MAGIC;
2559ed346baSBosko Milekic 			mtx_lock(&ng_nodelist_mtx);
25630400f03SJulian Elischer 			SLIST_INSERT_HEAD(&ng_allhooks, hook, hk_all);
2579ed346baSBosko Milekic 			mtx_unlock(&ng_nodelist_mtx);
25830400f03SJulian Elischer 		}
25930400f03SJulian Elischer 	}
26030400f03SJulian Elischer 	return (hook);
26130400f03SJulian Elischer }
26230400f03SJulian Elischer 
26330400f03SJulian Elischer static __inline node_p
26430400f03SJulian Elischer ng_alloc_node(void)
26530400f03SJulian Elischer {
26630400f03SJulian Elischer 	node_p node;
26730400f03SJulian Elischer 	SLIST_ENTRY(ng_node) temp;
2689ed346baSBosko Milekic 	mtx_lock(&ng_nodelist_mtx);
26930400f03SJulian Elischer 	node = LIST_FIRST(&ng_freenodes);
27030400f03SJulian Elischer 	if (node) {
27130400f03SJulian Elischer 		LIST_REMOVE(node, nd_nodes);
27230400f03SJulian Elischer 		bcopy(&node->nd_all, &temp, sizeof(temp));
27330400f03SJulian Elischer 		bzero(node, sizeof(struct ng_node));
27430400f03SJulian Elischer 		bcopy(&temp, &node->nd_all, sizeof(temp));
2759ed346baSBosko Milekic 		mtx_unlock(&ng_nodelist_mtx);
27630400f03SJulian Elischer 		node->nd_magic = ND_MAGIC;
27730400f03SJulian Elischer 	} else {
2789ed346baSBosko Milekic 		mtx_unlock(&ng_nodelist_mtx);
27930400f03SJulian Elischer 		_NG_ALLOC_NODE(node);
28030400f03SJulian Elischer 		if (node) {
28130400f03SJulian Elischer 			node->nd_magic = ND_MAGIC;
2829ed346baSBosko Milekic 			mtx_lock(&ng_nodelist_mtx);
28330400f03SJulian Elischer 			SLIST_INSERT_HEAD(&ng_allnodes, node, nd_all);
2849ed346baSBosko Milekic 			mtx_unlock(&ng_nodelist_mtx);
28530400f03SJulian Elischer 		}
28630400f03SJulian Elischer 	}
28730400f03SJulian Elischer 	return (node);
28830400f03SJulian Elischer }
28930400f03SJulian Elischer 
29030400f03SJulian Elischer #define NG_ALLOC_HOOK(hook) do { (hook) = ng_alloc_hook(); } while (0)
29130400f03SJulian Elischer #define NG_ALLOC_NODE(node) do { (node) = ng_alloc_node(); } while (0)
29230400f03SJulian Elischer 
29330400f03SJulian Elischer 
29430400f03SJulian Elischer #define NG_FREE_HOOK(hook)						\
29530400f03SJulian Elischer 	do {								\
2969ed346baSBosko Milekic 		mtx_lock(&ng_nodelist_mtx);			\
29730400f03SJulian Elischer 		LIST_INSERT_HEAD(&ng_freehooks, hook, hk_hooks);	\
29830400f03SJulian Elischer 		hook->hk_magic = 0;					\
2999ed346baSBosko Milekic 		mtx_unlock(&ng_nodelist_mtx);			\
30030400f03SJulian Elischer 	} while (0)
30130400f03SJulian Elischer 
30230400f03SJulian Elischer #define NG_FREE_NODE(node)						\
30330400f03SJulian Elischer 	do {								\
3049ed346baSBosko Milekic 		mtx_lock(&ng_nodelist_mtx);			\
30530400f03SJulian Elischer 		LIST_INSERT_HEAD(&ng_freenodes, node, nd_nodes);	\
30630400f03SJulian Elischer 		node->nd_magic = 0;					\
3079ed346baSBosko Milekic 		mtx_unlock(&ng_nodelist_mtx);			\
30830400f03SJulian Elischer 	} while (0)
30930400f03SJulian Elischer 
31030400f03SJulian Elischer #else /* NETGRAPH_DEBUG */ /*----------------------------------------------*/
31130400f03SJulian Elischer 
31230400f03SJulian Elischer #define NG_ALLOC_HOOK(hook) _NG_ALLOC_HOOK(hook)
31330400f03SJulian Elischer #define NG_ALLOC_NODE(node) _NG_ALLOC_NODE(node)
31430400f03SJulian Elischer 
315069154d5SJulian Elischer #define NG_FREE_HOOK(hook) do { FREE((hook), M_NETGRAPH_HOOK); } while (0)
316069154d5SJulian Elischer #define NG_FREE_NODE(node) do { FREE((node), M_NETGRAPH_NODE); } while (0)
31730400f03SJulian Elischer 
31830400f03SJulian Elischer #endif /* NETGRAPH_DEBUG */ /*----------------------------------------------*/
31930400f03SJulian Elischer 
320f33ca0c9SMarcel Moolenaar /* Set this to kdb_enter("X") to catch all errors as they occur */
3214cf49a43SJulian Elischer #ifndef TRAP_ERROR
3226b795970SJulian Elischer #define TRAP_ERROR()
3234cf49a43SJulian Elischer #endif
3244cf49a43SJulian Elischer 
325dc90cad9SJulian Elischer static	ng_ID_t nextID = 1;
326dc90cad9SJulian Elischer 
327b2da83c2SArchie Cobbs #ifdef INVARIANTS
328b2da83c2SArchie Cobbs #define CHECK_DATA_MBUF(m)	do {					\
329b2da83c2SArchie Cobbs 		struct mbuf *n;						\
330b2da83c2SArchie Cobbs 		int total;						\
331b2da83c2SArchie Cobbs 									\
332fe584538SDag-Erling Smørgrav 		M_ASSERTPKTHDR(m);					\
333b32cfb32SGleb Smirnoff 		for (total = 0, n = (m); n != NULL; n = n->m_next) {	\
334b2da83c2SArchie Cobbs 			total += n->m_len;				\
335b32cfb32SGleb Smirnoff 			if (n->m_nextpkt != NULL)			\
336b32cfb32SGleb Smirnoff 				panic("%s: m_nextpkt", __func__);	\
337b32cfb32SGleb Smirnoff 		}							\
338ba5b359aSGleb Smirnoff 									\
339b2da83c2SArchie Cobbs 		if ((m)->m_pkthdr.len != total) {			\
340b2da83c2SArchie Cobbs 			panic("%s: %d != %d",				\
3416e551fb6SDavid E. O'Brien 			    __func__, (m)->m_pkthdr.len, total);	\
342b2da83c2SArchie Cobbs 		}							\
343b2da83c2SArchie Cobbs 	} while (0)
344b2da83c2SArchie Cobbs #else
345b2da83c2SArchie Cobbs #define CHECK_DATA_MBUF(m)
346b2da83c2SArchie Cobbs #endif
347b2da83c2SArchie Cobbs 
348dc90cad9SJulian Elischer 
3494cf49a43SJulian Elischer /************************************************************************
350f8307e12SArchie Cobbs 	Parse type definitions for generic messages
351f8307e12SArchie Cobbs ************************************************************************/
352f8307e12SArchie Cobbs 
353f8307e12SArchie Cobbs /* Handy structure parse type defining macro */
354f8307e12SArchie Cobbs #define DEFINE_PARSE_STRUCT_TYPE(lo, up, args)				\
355f0184ff8SArchie Cobbs static const struct ng_parse_struct_field				\
356f0184ff8SArchie Cobbs 	ng_ ## lo ## _type_fields[] = NG_GENERIC_ ## up ## _INFO args;	\
357f8307e12SArchie Cobbs static const struct ng_parse_type ng_generic_ ## lo ## _type = {	\
358f8307e12SArchie Cobbs 	&ng_parse_struct_type,						\
359f0184ff8SArchie Cobbs 	&ng_ ## lo ## _type_fields					\
360f8307e12SArchie Cobbs }
361f8307e12SArchie Cobbs 
362f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(mkpeer, MKPEER, ());
363f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(connect, CONNECT, ());
364f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(name, NAME, ());
365f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(rmhook, RMHOOK, ());
366f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(nodeinfo, NODEINFO, ());
367f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(typeinfo, TYPEINFO, ());
368f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(linkinfo, LINKINFO, (&ng_generic_nodeinfo_type));
369f8307e12SArchie Cobbs 
370f8307e12SArchie Cobbs /* Get length of an array when the length is stored as a 32 bit
371d7d97eb0SJeroen Ruigrok van der Werven    value immediately preceding the array -- as with struct namelist
372f8307e12SArchie Cobbs    and struct typelist. */
373f8307e12SArchie Cobbs static int
374f8307e12SArchie Cobbs ng_generic_list_getLength(const struct ng_parse_type *type,
375f8307e12SArchie Cobbs 	const u_char *start, const u_char *buf)
376f8307e12SArchie Cobbs {
377f8307e12SArchie Cobbs 	return *((const u_int32_t *)(buf - 4));
378f8307e12SArchie Cobbs }
379f8307e12SArchie Cobbs 
380f8307e12SArchie Cobbs /* Get length of the array of struct linkinfo inside a struct hooklist */
381f8307e12SArchie Cobbs static int
382f8307e12SArchie Cobbs ng_generic_linkinfo_getLength(const struct ng_parse_type *type,
383f8307e12SArchie Cobbs 	const u_char *start, const u_char *buf)
384f8307e12SArchie Cobbs {
385f8307e12SArchie Cobbs 	const struct hooklist *hl = (const struct hooklist *)start;
386f8307e12SArchie Cobbs 
387f8307e12SArchie Cobbs 	return hl->nodeinfo.hooks;
388f8307e12SArchie Cobbs }
389f8307e12SArchie Cobbs 
390f8307e12SArchie Cobbs /* Array type for a variable length array of struct namelist */
391f8307e12SArchie Cobbs static const struct ng_parse_array_info ng_nodeinfoarray_type_info = {
392f8307e12SArchie Cobbs 	&ng_generic_nodeinfo_type,
393f8307e12SArchie Cobbs 	&ng_generic_list_getLength
394f8307e12SArchie Cobbs };
395f8307e12SArchie Cobbs static const struct ng_parse_type ng_generic_nodeinfoarray_type = {
396f8307e12SArchie Cobbs 	&ng_parse_array_type,
397f8307e12SArchie Cobbs 	&ng_nodeinfoarray_type_info
398f8307e12SArchie Cobbs };
399f8307e12SArchie Cobbs 
400f8307e12SArchie Cobbs /* Array type for a variable length array of struct typelist */
401f8307e12SArchie Cobbs static const struct ng_parse_array_info ng_typeinfoarray_type_info = {
402f8307e12SArchie Cobbs 	&ng_generic_typeinfo_type,
403f8307e12SArchie Cobbs 	&ng_generic_list_getLength
404f8307e12SArchie Cobbs };
405f8307e12SArchie Cobbs static const struct ng_parse_type ng_generic_typeinfoarray_type = {
406f8307e12SArchie Cobbs 	&ng_parse_array_type,
407f8307e12SArchie Cobbs 	&ng_typeinfoarray_type_info
408f8307e12SArchie Cobbs };
409f8307e12SArchie Cobbs 
410f8307e12SArchie Cobbs /* Array type for array of struct linkinfo in struct hooklist */
411f8307e12SArchie Cobbs static const struct ng_parse_array_info ng_generic_linkinfo_array_type_info = {
412f8307e12SArchie Cobbs 	&ng_generic_linkinfo_type,
413f8307e12SArchie Cobbs 	&ng_generic_linkinfo_getLength
414f8307e12SArchie Cobbs };
415f8307e12SArchie Cobbs static const struct ng_parse_type ng_generic_linkinfo_array_type = {
416f8307e12SArchie Cobbs 	&ng_parse_array_type,
417f8307e12SArchie Cobbs 	&ng_generic_linkinfo_array_type_info
418f8307e12SArchie Cobbs };
419f8307e12SArchie Cobbs 
420f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(typelist, TYPELIST, (&ng_generic_nodeinfoarray_type));
421f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(hooklist, HOOKLIST,
422f8307e12SArchie Cobbs 	(&ng_generic_nodeinfo_type, &ng_generic_linkinfo_array_type));
423f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(listnodes, LISTNODES,
424f8307e12SArchie Cobbs 	(&ng_generic_nodeinfoarray_type));
425f8307e12SArchie Cobbs 
426f8307e12SArchie Cobbs /* List of commands and how to convert arguments to/from ASCII */
427f8307e12SArchie Cobbs static const struct ng_cmdlist ng_generic_cmds[] = {
428f8307e12SArchie Cobbs 	{
429f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
430f8307e12SArchie Cobbs 	  NGM_SHUTDOWN,
431f8307e12SArchie Cobbs 	  "shutdown",
432f8307e12SArchie Cobbs 	  NULL,
433f8307e12SArchie Cobbs 	  NULL
434f8307e12SArchie Cobbs 	},
435f8307e12SArchie Cobbs 	{
436f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
437f8307e12SArchie Cobbs 	  NGM_MKPEER,
438f8307e12SArchie Cobbs 	  "mkpeer",
439f8307e12SArchie Cobbs 	  &ng_generic_mkpeer_type,
440f8307e12SArchie Cobbs 	  NULL
441f8307e12SArchie Cobbs 	},
442f8307e12SArchie Cobbs 	{
443f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
444f8307e12SArchie Cobbs 	  NGM_CONNECT,
445f8307e12SArchie Cobbs 	  "connect",
446f8307e12SArchie Cobbs 	  &ng_generic_connect_type,
447f8307e12SArchie Cobbs 	  NULL
448f8307e12SArchie Cobbs 	},
449f8307e12SArchie Cobbs 	{
450f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
451f8307e12SArchie Cobbs 	  NGM_NAME,
452f8307e12SArchie Cobbs 	  "name",
453f8307e12SArchie Cobbs 	  &ng_generic_name_type,
454f8307e12SArchie Cobbs 	  NULL
455f8307e12SArchie Cobbs 	},
456f8307e12SArchie Cobbs 	{
457f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
458f8307e12SArchie Cobbs 	  NGM_RMHOOK,
459f8307e12SArchie Cobbs 	  "rmhook",
460f8307e12SArchie Cobbs 	  &ng_generic_rmhook_type,
461f8307e12SArchie Cobbs 	  NULL
462f8307e12SArchie Cobbs 	},
463f8307e12SArchie Cobbs 	{
464f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
465f8307e12SArchie Cobbs 	  NGM_NODEINFO,
466f8307e12SArchie Cobbs 	  "nodeinfo",
467f8307e12SArchie Cobbs 	  NULL,
468f8307e12SArchie Cobbs 	  &ng_generic_nodeinfo_type
469f8307e12SArchie Cobbs 	},
470f8307e12SArchie Cobbs 	{
471f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
472f8307e12SArchie Cobbs 	  NGM_LISTHOOKS,
473f8307e12SArchie Cobbs 	  "listhooks",
474f8307e12SArchie Cobbs 	  NULL,
475f8307e12SArchie Cobbs 	  &ng_generic_hooklist_type
476f8307e12SArchie Cobbs 	},
477f8307e12SArchie Cobbs 	{
478f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
479f8307e12SArchie Cobbs 	  NGM_LISTNAMES,
480f8307e12SArchie Cobbs 	  "listnames",
481f8307e12SArchie Cobbs 	  NULL,
482f8307e12SArchie Cobbs 	  &ng_generic_listnodes_type	/* same as NGM_LISTNODES */
483f8307e12SArchie Cobbs 	},
484f8307e12SArchie Cobbs 	{
485f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
486f8307e12SArchie Cobbs 	  NGM_LISTNODES,
487f8307e12SArchie Cobbs 	  "listnodes",
488f8307e12SArchie Cobbs 	  NULL,
489f8307e12SArchie Cobbs 	  &ng_generic_listnodes_type
490f8307e12SArchie Cobbs 	},
491f8307e12SArchie Cobbs 	{
492f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
493f8307e12SArchie Cobbs 	  NGM_LISTTYPES,
494f8307e12SArchie Cobbs 	  "listtypes",
495f8307e12SArchie Cobbs 	  NULL,
496f8307e12SArchie Cobbs 	  &ng_generic_typeinfo_type
497f8307e12SArchie Cobbs 	},
498f8307e12SArchie Cobbs 	{
499f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
5007095e097SPoul-Henning Kamp 	  NGM_TEXT_CONFIG,
5017095e097SPoul-Henning Kamp 	  "textconfig",
5027095e097SPoul-Henning Kamp 	  NULL,
5037095e097SPoul-Henning Kamp 	  &ng_parse_string_type
5047095e097SPoul-Henning Kamp 	},
5057095e097SPoul-Henning Kamp 	{
5067095e097SPoul-Henning Kamp 	  NGM_GENERIC_COOKIE,
507f8307e12SArchie Cobbs 	  NGM_TEXT_STATUS,
508f8307e12SArchie Cobbs 	  "textstatus",
509f8307e12SArchie Cobbs 	  NULL,
510f8307e12SArchie Cobbs 	  &ng_parse_string_type
511f8307e12SArchie Cobbs 	},
512f8307e12SArchie Cobbs 	{
513f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
514f8307e12SArchie Cobbs 	  NGM_ASCII2BINARY,
515f8307e12SArchie Cobbs 	  "ascii2binary",
516f8307e12SArchie Cobbs 	  &ng_parse_ng_mesg_type,
517f8307e12SArchie Cobbs 	  &ng_parse_ng_mesg_type
518f8307e12SArchie Cobbs 	},
519f8307e12SArchie Cobbs 	{
520f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
521f8307e12SArchie Cobbs 	  NGM_BINARY2ASCII,
522f8307e12SArchie Cobbs 	  "binary2ascii",
523f8307e12SArchie Cobbs 	  &ng_parse_ng_mesg_type,
524f8307e12SArchie Cobbs 	  &ng_parse_ng_mesg_type
525f8307e12SArchie Cobbs 	},
526f8307e12SArchie Cobbs 	{ 0 }
527f8307e12SArchie Cobbs };
528f8307e12SArchie Cobbs 
529f8307e12SArchie Cobbs /************************************************************************
5304cf49a43SJulian Elischer 			Node routines
5314cf49a43SJulian Elischer ************************************************************************/
5324cf49a43SJulian Elischer 
5334cf49a43SJulian Elischer /*
5344cf49a43SJulian Elischer  * Instantiate a node of the requested type
5354cf49a43SJulian Elischer  */
5364cf49a43SJulian Elischer int
5374cf49a43SJulian Elischer ng_make_node(const char *typename, node_p *nodepp)
5384cf49a43SJulian Elischer {
5394cf49a43SJulian Elischer 	struct ng_type *type;
540069154d5SJulian Elischer 	int	error;
5414cf49a43SJulian Elischer 
5424cf49a43SJulian Elischer 	/* Check that the type makes sense */
5434cf49a43SJulian Elischer 	if (typename == NULL) {
5446b795970SJulian Elischer 		TRAP_ERROR();
5454cf49a43SJulian Elischer 		return (EINVAL);
5464cf49a43SJulian Elischer 	}
5474cf49a43SJulian Elischer 
5487610f574SGleb Smirnoff 	/* Locate the node type. If we fail we return. Do not try to load
5497610f574SGleb Smirnoff 	 * module.
5507610f574SGleb Smirnoff 	 */
5514cf49a43SJulian Elischer 	if ((type = ng_findtype(typename)) == NULL)
5524cf49a43SJulian Elischer 		return (ENXIO);
5534cf49a43SJulian Elischer 
554069154d5SJulian Elischer 	/*
555069154d5SJulian Elischer 	 * If we have a constructor, then make the node and
556069154d5SJulian Elischer 	 * call the constructor to do type specific initialisation.
557069154d5SJulian Elischer 	 */
558069154d5SJulian Elischer 	if (type->constructor != NULL) {
559069154d5SJulian Elischer 		if ((error = ng_make_node_common(type, nodepp)) == 0) {
560069154d5SJulian Elischer 			if ((error = ((*type->constructor)(*nodepp)) != 0)) {
56130400f03SJulian Elischer 				NG_NODE_UNREF(*nodepp);
562069154d5SJulian Elischer 			}
563069154d5SJulian Elischer 		}
564069154d5SJulian Elischer 	} else {
565069154d5SJulian Elischer 		/*
566069154d5SJulian Elischer 		 * Node has no constructor. We cannot ask for one
567069154d5SJulian Elischer 		 * to be made. It must be brought into existance by
568954c4772SJulian Elischer 		 * some external agency. The external agency should
569069154d5SJulian Elischer 		 * call ng_make_node_common() directly to get the
570069154d5SJulian Elischer 		 * netgraph part initialised.
571069154d5SJulian Elischer 		 */
5726b795970SJulian Elischer 		TRAP_ERROR();
573069154d5SJulian Elischer 		error = EINVAL;
574069154d5SJulian Elischer 	}
575069154d5SJulian Elischer 	return (error);
5764cf49a43SJulian Elischer }
5774cf49a43SJulian Elischer 
5784cf49a43SJulian Elischer /*
579069154d5SJulian Elischer  * Generic node creation. Called by node initialisation for externally
580069154d5SJulian Elischer  * instantiated nodes (e.g. hardware, sockets, etc ).
5814cf49a43SJulian Elischer  * The returned node has a reference count of 1.
5824cf49a43SJulian Elischer  */
5834cf49a43SJulian Elischer int
5844cf49a43SJulian Elischer ng_make_node_common(struct ng_type *type, node_p *nodepp)
5854cf49a43SJulian Elischer {
5864cf49a43SJulian Elischer 	node_p node;
5874cf49a43SJulian Elischer 
5884cf49a43SJulian Elischer 	/* Require the node type to have been already installed */
5894cf49a43SJulian Elischer 	if (ng_findtype(type->name) == NULL) {
5906b795970SJulian Elischer 		TRAP_ERROR();
5914cf49a43SJulian Elischer 		return (EINVAL);
5924cf49a43SJulian Elischer 	}
5934cf49a43SJulian Elischer 
5944cf49a43SJulian Elischer 	/* Make a node and try attach it to the type */
59530400f03SJulian Elischer 	NG_ALLOC_NODE(node);
5964cf49a43SJulian Elischer 	if (node == NULL) {
5976b795970SJulian Elischer 		TRAP_ERROR();
5984cf49a43SJulian Elischer 		return (ENOMEM);
5994cf49a43SJulian Elischer 	}
60030400f03SJulian Elischer 	node->nd_type = type;
60130400f03SJulian Elischer 	NG_NODE_REF(node);				/* note reference */
6024cf49a43SJulian Elischer 	type->refs++;
6034cf49a43SJulian Elischer 
6046008862bSJohn Baldwin 	mtx_init(&node->nd_input_queue.q_mtx, "ng_node", NULL, MTX_SPIN);
60530400f03SJulian Elischer 	node->nd_input_queue.queue = NULL;
60630400f03SJulian Elischer 	node->nd_input_queue.last = &node->nd_input_queue.queue;
60730400f03SJulian Elischer 	node->nd_input_queue.q_flags = 0;
60830400f03SJulian Elischer 	node->nd_input_queue.q_node = node;
6094cf49a43SJulian Elischer 
6104cf49a43SJulian Elischer 	/* Initialize hook list for new node */
61130400f03SJulian Elischer 	LIST_INIT(&node->nd_hooks);
6124cf49a43SJulian Elischer 
613069154d5SJulian Elischer 	/* Link us into the node linked list */
6149ed346baSBosko Milekic 	mtx_lock(&ng_nodelist_mtx);
61530400f03SJulian Elischer 	LIST_INSERT_HEAD(&ng_nodelist, node, nd_nodes);
6169ed346baSBosko Milekic 	mtx_unlock(&ng_nodelist_mtx);
617069154d5SJulian Elischer 
618069154d5SJulian Elischer 
619dc90cad9SJulian Elischer 	/* get an ID and put us in the hash chain */
6209ed346baSBosko Milekic 	mtx_lock(&ng_idhash_mtx);
62130400f03SJulian Elischer 	for (;;) { /* wrap protection, even if silly */
622069154d5SJulian Elischer 		node_p node2 = NULL;
62330400f03SJulian Elischer 		node->nd_ID = nextID++; /* 137/second for 1 year before wrap */
6240f150d04SJulian Elischer 
62530400f03SJulian Elischer 		/* Is there a problem with the new number? */
6260f150d04SJulian Elischer 		NG_IDHASH_FIND(node->nd_ID, node2); /* already taken? */
6270f150d04SJulian Elischer 		if ((node->nd_ID != 0) && (node2 == NULL)) {
62830400f03SJulian Elischer 			break;
629069154d5SJulian Elischer 		}
63030400f03SJulian Elischer 	}
6310f150d04SJulian Elischer 	LIST_INSERT_HEAD(&ng_ID_hash[NG_IDHASH_FN(node->nd_ID)],
63230400f03SJulian Elischer 							node, nd_idnodes);
6339ed346baSBosko Milekic 	mtx_unlock(&ng_idhash_mtx);
634dc90cad9SJulian Elischer 
6354cf49a43SJulian Elischer 	/* Done */
6364cf49a43SJulian Elischer 	*nodepp = node;
6374cf49a43SJulian Elischer 	return (0);
6384cf49a43SJulian Elischer }
6394cf49a43SJulian Elischer 
6404cf49a43SJulian Elischer /*
6414cf49a43SJulian Elischer  * Forceably start the shutdown process on a node. Either call
6424cf49a43SJulian Elischer  * it's shutdown method, or do the default shutdown if there is
6434cf49a43SJulian Elischer  * no type-specific method.
6444cf49a43SJulian Elischer  *
645069154d5SJulian Elischer  * We can only be called form a shutdown message, so we know we have
6463e4084c8SJulian Elischer  * a writer lock, and therefore exclusive access. It also means
6473e4084c8SJulian Elischer  * that we should not be on the work queue, but we check anyhow.
648069154d5SJulian Elischer  *
649069154d5SJulian Elischer  * Persistent node types must have a type-specific method which
6503e4084c8SJulian Elischer  * Allocates a new node in which case, this one is irretrievably going away,
6513e4084c8SJulian Elischer  * or cleans up anything it needs, and just makes the node valid again,
6523e4084c8SJulian Elischer  * in which case we allow the node to survive.
6533e4084c8SJulian Elischer  *
6543e4084c8SJulian Elischer  * XXX We need to think of how to tell a persistant node that we
6553e4084c8SJulian Elischer  * REALLY need to go away because the hardware has gone or we
6563e4084c8SJulian Elischer  * are rebooting.... etc.
6574cf49a43SJulian Elischer  */
6584cf49a43SJulian Elischer void
6591acb27c6SJulian Elischer ng_rmnode(node_p node, hook_p dummy1, void *dummy2, int dummy3)
6604cf49a43SJulian Elischer {
6613e4084c8SJulian Elischer 	hook_p hook;
6623e4084c8SJulian Elischer 
6634cf49a43SJulian Elischer 	/* Check if it's already shutting down */
664be4252b3SJulian Elischer 	if ((node->nd_flags & NGF_CLOSING) != 0)
6654cf49a43SJulian Elischer 		return;
6664cf49a43SJulian Elischer 
6671acb27c6SJulian Elischer 	if (node == &ng_deadnode) {
6681acb27c6SJulian Elischer 		printf ("shutdown called on deadnode\n");
6691acb27c6SJulian Elischer 		return;
6701acb27c6SJulian Elischer 	}
6711acb27c6SJulian Elischer 
6724cf49a43SJulian Elischer 	/* Add an extra reference so it doesn't go away during this */
67330400f03SJulian Elischer 	NG_NODE_REF(node);
6744cf49a43SJulian Elischer 
67530400f03SJulian Elischer 	/*
67630400f03SJulian Elischer 	 * Mark it invalid so any newcomers know not to try use it
67730400f03SJulian Elischer 	 * Also add our own mark so we can't recurse
678be4252b3SJulian Elischer 	 * note that NGF_INVALID does not do this as it's also set during
67930400f03SJulian Elischer 	 * creation
68030400f03SJulian Elischer 	 */
681be4252b3SJulian Elischer 	node->nd_flags |= NGF_INVALID|NGF_CLOSING;
6824cf49a43SJulian Elischer 
683991fc65aSJulian Elischer 	/* If node has its pre-shutdown method, then call it first*/
684991fc65aSJulian Elischer 	if (node->nd_type && node->nd_type->close)
685991fc65aSJulian Elischer 		(*node->nd_type->close)(node);
686991fc65aSJulian Elischer 
6873e4084c8SJulian Elischer 	/* Notify all remaining connected nodes to disconnect */
6883e4084c8SJulian Elischer 	while ((hook = LIST_FIRST(&node->nd_hooks)) != NULL)
6893e4084c8SJulian Elischer 		ng_destroy_hook(hook);
69030400f03SJulian Elischer 
691069154d5SJulian Elischer 	/*
692069154d5SJulian Elischer 	 * Drain the input queue forceably.
69330400f03SJulian Elischer 	 * it has no hooks so what's it going to do, bleed on someone?
69430400f03SJulian Elischer 	 * Theoretically we came here from a queue entry that was added
69530400f03SJulian Elischer 	 * Just before the queue was closed, so it should be empty anyway.
696b57a7965SJulian Elischer 	 * Also removes us from worklist if needed.
697069154d5SJulian Elischer 	 */
69830400f03SJulian Elischer 	ng_flush_input_queue(&node->nd_input_queue);
699069154d5SJulian Elischer 
700069154d5SJulian Elischer 	/* Ask the type if it has anything to do in this case */
70130400f03SJulian Elischer 	if (node->nd_type && node->nd_type->shutdown) {
70230400f03SJulian Elischer 		(*node->nd_type->shutdown)(node);
70330400f03SJulian Elischer 		if (NG_NODE_IS_VALID(node)) {
70430400f03SJulian Elischer 			/*
70530400f03SJulian Elischer 			 * Well, blow me down if the node code hasn't declared
70630400f03SJulian Elischer 			 * that it doesn't want to die.
70730400f03SJulian Elischer 			 * Presumably it is a persistant node.
7081acb27c6SJulian Elischer 			 * If we REALLY want it to go away,
7091acb27c6SJulian Elischer 			 *  e.g. hardware going away,
710be4252b3SJulian Elischer 			 * Our caller should set NGF_REALLY_DIE in nd_flags.
71130400f03SJulian Elischer 			 */
712be4252b3SJulian Elischer 			node->nd_flags &= ~(NGF_INVALID|NGF_CLOSING);
7131acb27c6SJulian Elischer 			NG_NODE_UNREF(node); /* Assume they still have theirs */
71430400f03SJulian Elischer 			return;
7154cf49a43SJulian Elischer 		}
7161acb27c6SJulian Elischer 	} else {				/* do the default thing */
7171acb27c6SJulian Elischer 		NG_NODE_UNREF(node);
7181acb27c6SJulian Elischer 	}
7194cf49a43SJulian Elischer 
72030400f03SJulian Elischer 	ng_unname(node); /* basically a NOP these days */
72130400f03SJulian Elischer 
72230400f03SJulian Elischer 	/*
72330400f03SJulian Elischer 	 * Remove extra reference, possibly the last
72430400f03SJulian Elischer 	 * Possible other holders of references may include
72530400f03SJulian Elischer 	 * timeout callouts, but theoretically the node's supposed to
72630400f03SJulian Elischer 	 * have cancelled them. Possibly hardware dependencies may
72730400f03SJulian Elischer 	 * force a driver to 'linger' with a reference.
72830400f03SJulian Elischer 	 */
72930400f03SJulian Elischer 	NG_NODE_UNREF(node);
7304cf49a43SJulian Elischer }
7314cf49a43SJulian Elischer 
7325951069aSJulian Elischer #ifdef	NETGRAPH_DEBUG
7334cf49a43SJulian Elischer void
7345951069aSJulian Elischer ng_ref_node(node_p node)
7355951069aSJulian Elischer {
7365951069aSJulian Elischer 	_NG_NODE_REF(node);
7375951069aSJulian Elischer }
7385951069aSJulian Elischer #endif
7395951069aSJulian Elischer 
7405951069aSJulian Elischer /*
7415951069aSJulian Elischer  * Remove a reference to the node, possibly the last.
7425951069aSJulian Elischer  * deadnode always acts as it it were the last.
7435951069aSJulian Elischer  */
7445951069aSJulian Elischer int
74530400f03SJulian Elischer ng_unref_node(node_p node)
7464cf49a43SJulian Elischer {
74730400f03SJulian Elischer 	int     v;
7486b795970SJulian Elischer 
7496b795970SJulian Elischer 	if (node == &ng_deadnode) {
7505951069aSJulian Elischer 		return (0);
7516b795970SJulian Elischer 	}
7526b795970SJulian Elischer 
75330400f03SJulian Elischer 	do {
7545951069aSJulian Elischer 		v = node->nd_refs - 1;
7555951069aSJulian Elischer 	} while (! atomic_cmpset_int(&node->nd_refs, v + 1, v));
756e8a49db2SJulian Elischer 
7575951069aSJulian Elischer 	if (v == 0) { /* we were the last */
758069154d5SJulian Elischer 
7599ed346baSBosko Milekic 		mtx_lock(&ng_nodelist_mtx);
76030400f03SJulian Elischer 		node->nd_type->refs--; /* XXX maybe should get types lock? */
76130400f03SJulian Elischer 		LIST_REMOVE(node, nd_nodes);
7629ed346baSBosko Milekic 		mtx_unlock(&ng_nodelist_mtx);
763069154d5SJulian Elischer 
7649ed346baSBosko Milekic 		mtx_lock(&ng_idhash_mtx);
76530400f03SJulian Elischer 		LIST_REMOVE(node, nd_idnodes);
7669ed346baSBosko Milekic 		mtx_unlock(&ng_idhash_mtx);
767069154d5SJulian Elischer 
76812574a02SJulian Elischer 		mtx_destroy(&node->nd_input_queue.q_mtx);
769069154d5SJulian Elischer 		NG_FREE_NODE(node);
7704cf49a43SJulian Elischer 	}
7715951069aSJulian Elischer 	return (v);
7724cf49a43SJulian Elischer }
7734cf49a43SJulian Elischer 
7744cf49a43SJulian Elischer /************************************************************************
775dc90cad9SJulian Elischer 			Node ID handling
776dc90cad9SJulian Elischer ************************************************************************/
777dc90cad9SJulian Elischer static node_p
778069154d5SJulian Elischer ng_ID2noderef(ng_ID_t ID)
779dc90cad9SJulian Elischer {
78030400f03SJulian Elischer 	node_p node;
7819ed346baSBosko Milekic 	mtx_lock(&ng_idhash_mtx);
7820f150d04SJulian Elischer 	NG_IDHASH_FIND(ID, node);
78330400f03SJulian Elischer 	if(node)
78430400f03SJulian Elischer 		NG_NODE_REF(node);
7859ed346baSBosko Milekic 	mtx_unlock(&ng_idhash_mtx);
78630400f03SJulian Elischer 	return(node);
787dc90cad9SJulian Elischer }
788dc90cad9SJulian Elischer 
789dc90cad9SJulian Elischer ng_ID_t
790dc90cad9SJulian Elischer ng_node2ID(node_p node)
791dc90cad9SJulian Elischer {
79270de87f2SJulian Elischer 	return (node ? NG_NODE_ID(node) : 0);
793dc90cad9SJulian Elischer }
794dc90cad9SJulian Elischer 
795dc90cad9SJulian Elischer /************************************************************************
7964cf49a43SJulian Elischer 			Node name handling
7974cf49a43SJulian Elischer ************************************************************************/
7984cf49a43SJulian Elischer 
7994cf49a43SJulian Elischer /*
8004cf49a43SJulian Elischer  * Assign a node a name. Once assigned, the name cannot be changed.
8014cf49a43SJulian Elischer  */
8024cf49a43SJulian Elischer int
8034cf49a43SJulian Elischer ng_name_node(node_p node, const char *name)
8044cf49a43SJulian Elischer {
8054cf49a43SJulian Elischer 	int i;
806069154d5SJulian Elischer 	node_p node2;
8074cf49a43SJulian Elischer 
8084cf49a43SJulian Elischer 	/* Check the name is valid */
80987e2c66aSHartmut Brandt 	for (i = 0; i < NG_NODESIZ; i++) {
8104cf49a43SJulian Elischer 		if (name[i] == '\0' || name[i] == '.' || name[i] == ':')
8114cf49a43SJulian Elischer 			break;
8124cf49a43SJulian Elischer 	}
8134cf49a43SJulian Elischer 	if (i == 0 || name[i] != '\0') {
8146b795970SJulian Elischer 		TRAP_ERROR();
8154cf49a43SJulian Elischer 		return (EINVAL);
8164cf49a43SJulian Elischer 	}
817dc90cad9SJulian Elischer 	if (ng_decodeidname(name) != 0) { /* valid IDs not allowed here */
8186b795970SJulian Elischer 		TRAP_ERROR();
8194cf49a43SJulian Elischer 		return (EINVAL);
8204cf49a43SJulian Elischer 	}
8214cf49a43SJulian Elischer 
8224cf49a43SJulian Elischer 	/* Check the name isn't already being used */
823069154d5SJulian Elischer 	if ((node2 = ng_name2noderef(node, name)) != NULL) {
82430400f03SJulian Elischer 		NG_NODE_UNREF(node2);
8256b795970SJulian Elischer 		TRAP_ERROR();
8264cf49a43SJulian Elischer 		return (EADDRINUSE);
8274cf49a43SJulian Elischer 	}
8284cf49a43SJulian Elischer 
829069154d5SJulian Elischer 	/* copy it */
83087e2c66aSHartmut Brandt 	strlcpy(NG_NODE_NAME(node), name, NG_NODESIZ);
8314cf49a43SJulian Elischer 
8324cf49a43SJulian Elischer 	return (0);
8334cf49a43SJulian Elischer }
8344cf49a43SJulian Elischer 
8354cf49a43SJulian Elischer /*
8364cf49a43SJulian Elischer  * Find a node by absolute name. The name should NOT end with ':'
8374cf49a43SJulian Elischer  * The name "." means "this node" and "[xxx]" means "the node
8384cf49a43SJulian Elischer  * with ID (ie, at address) xxx".
8394cf49a43SJulian Elischer  *
8404cf49a43SJulian Elischer  * Returns the node if found, else NULL.
841069154d5SJulian Elischer  * Eventually should add something faster than a sequential search.
84230400f03SJulian Elischer  * Note it aquires a reference on the node so you can be sure it's still there.
8434cf49a43SJulian Elischer  */
8444cf49a43SJulian Elischer node_p
845069154d5SJulian Elischer ng_name2noderef(node_p here, const char *name)
8464cf49a43SJulian Elischer {
847dc90cad9SJulian Elischer 	node_p node;
848dc90cad9SJulian Elischer 	ng_ID_t temp;
8494cf49a43SJulian Elischer 
8504cf49a43SJulian Elischer 	/* "." means "this node" */
851069154d5SJulian Elischer 	if (strcmp(name, ".") == 0) {
85230400f03SJulian Elischer 		NG_NODE_REF(here);
853069154d5SJulian Elischer 		return(here);
854069154d5SJulian Elischer 	}
8554cf49a43SJulian Elischer 
8564cf49a43SJulian Elischer 	/* Check for name-by-ID */
857dc90cad9SJulian Elischer 	if ((temp = ng_decodeidname(name)) != 0) {
858069154d5SJulian Elischer 		return (ng_ID2noderef(temp));
8594cf49a43SJulian Elischer 	}
8604cf49a43SJulian Elischer 
8614cf49a43SJulian Elischer 	/* Find node by name */
8629ed346baSBosko Milekic 	mtx_lock(&ng_nodelist_mtx);
86330400f03SJulian Elischer 	LIST_FOREACH(node, &ng_nodelist, nd_nodes) {
86470de87f2SJulian Elischer 		if (NG_NODE_IS_VALID(node)
86570de87f2SJulian Elischer 		&& NG_NODE_HAS_NAME(node)
86670de87f2SJulian Elischer 		&& (strcmp(NG_NODE_NAME(node), name) == 0)) {
8674cf49a43SJulian Elischer 			break;
8684cf49a43SJulian Elischer 		}
86970de87f2SJulian Elischer 	}
870069154d5SJulian Elischer 	if (node)
87130400f03SJulian Elischer 		NG_NODE_REF(node);
8729ed346baSBosko Milekic 	mtx_unlock(&ng_nodelist_mtx);
8734cf49a43SJulian Elischer 	return (node);
8744cf49a43SJulian Elischer }
8754cf49a43SJulian Elischer 
8764cf49a43SJulian Elischer /*
8779d5abbddSJens Schweikhardt  * Decode an ID name, eg. "[f03034de]". Returns 0 if the
878dc90cad9SJulian Elischer  * string is not valid, otherwise returns the value.
8794cf49a43SJulian Elischer  */
880dc90cad9SJulian Elischer static ng_ID_t
8814cf49a43SJulian Elischer ng_decodeidname(const char *name)
8824cf49a43SJulian Elischer {
8832b70adcbSArchie Cobbs 	const int len = strlen(name);
88425792ef3SArchie Cobbs 	char *eptr;
8852b70adcbSArchie Cobbs 	u_long val;
8864cf49a43SJulian Elischer 
8872b70adcbSArchie Cobbs 	/* Check for proper length, brackets, no leading junk */
88870de87f2SJulian Elischer 	if ((len < 3)
88970de87f2SJulian Elischer 	|| (name[0] != '[')
89070de87f2SJulian Elischer 	|| (name[len - 1] != ']')
89170de87f2SJulian Elischer 	|| (!isxdigit(name[1]))) {
89270de87f2SJulian Elischer 		return ((ng_ID_t)0);
89370de87f2SJulian Elischer 	}
8944cf49a43SJulian Elischer 
8952b70adcbSArchie Cobbs 	/* Decode number */
8962b70adcbSArchie Cobbs 	val = strtoul(name + 1, &eptr, 16);
89770de87f2SJulian Elischer 	if ((eptr - name != len - 1)
89870de87f2SJulian Elischer 	|| (val == ULONG_MAX)
89970de87f2SJulian Elischer 	|| (val == 0)) {
90012f035e0SJulian Elischer 		return ((ng_ID_t)0);
90170de87f2SJulian Elischer 	}
9022b70adcbSArchie Cobbs 	return (ng_ID_t)val;
9034cf49a43SJulian Elischer }
9044cf49a43SJulian Elischer 
9054cf49a43SJulian Elischer /*
9064cf49a43SJulian Elischer  * Remove a name from a node. This should only be called
9074cf49a43SJulian Elischer  * when shutting down and removing the node.
9081acb27c6SJulian Elischer  * IF we allow name changing this may be more resurected.
9094cf49a43SJulian Elischer  */
9104cf49a43SJulian Elischer void
9114cf49a43SJulian Elischer ng_unname(node_p node)
9124cf49a43SJulian Elischer {
9134cf49a43SJulian Elischer }
9144cf49a43SJulian Elischer 
9154cf49a43SJulian Elischer /************************************************************************
9164cf49a43SJulian Elischer 			Hook routines
9174cf49a43SJulian Elischer  Names are not optional. Hooks are always connected, except for a
9183e4084c8SJulian Elischer  brief moment within these routines. On invalidation or during creation
9193e4084c8SJulian Elischer  they are connected to the 'dead' hook.
9204cf49a43SJulian Elischer ************************************************************************/
9214cf49a43SJulian Elischer 
9224cf49a43SJulian Elischer /*
9234cf49a43SJulian Elischer  * Remove a hook reference
9244cf49a43SJulian Elischer  */
92530400f03SJulian Elischer void
9264cf49a43SJulian Elischer ng_unref_hook(hook_p hook)
9274cf49a43SJulian Elischer {
92830400f03SJulian Elischer 	int     v;
9296b795970SJulian Elischer 
9306b795970SJulian Elischer 	if (hook == &ng_deadhook) {
9316b795970SJulian Elischer 		return;
9326b795970SJulian Elischer 	}
93330400f03SJulian Elischer 	do {
93430400f03SJulian Elischer 		v = hook->hk_refs;
93530400f03SJulian Elischer 	} while (! atomic_cmpset_int(&hook->hk_refs, v, v - 1));
936e8a49db2SJulian Elischer 
93730400f03SJulian Elischer 	if (v == 1) { /* we were the last */
9386b795970SJulian Elischer 		if (_NG_HOOK_NODE(hook)) { /* it'll probably be ng_deadnode */
9396b795970SJulian Elischer 			_NG_NODE_UNREF((_NG_HOOK_NODE(hook)));
94030400f03SJulian Elischer 			hook->hk_node = NULL;
941069154d5SJulian Elischer 		}
942069154d5SJulian Elischer 		NG_FREE_HOOK(hook);
943069154d5SJulian Elischer 	}
9444cf49a43SJulian Elischer }
9454cf49a43SJulian Elischer 
9464cf49a43SJulian Elischer /*
9474cf49a43SJulian Elischer  * Add an unconnected hook to a node. Only used internally.
9483e4084c8SJulian Elischer  * Assumes node is locked. (XXX not yet true )
9494cf49a43SJulian Elischer  */
9504cf49a43SJulian Elischer static int
9514cf49a43SJulian Elischer ng_add_hook(node_p node, const char *name, hook_p *hookp)
9524cf49a43SJulian Elischer {
9534cf49a43SJulian Elischer 	hook_p hook;
9544cf49a43SJulian Elischer 	int error = 0;
9554cf49a43SJulian Elischer 
9564cf49a43SJulian Elischer 	/* Check that the given name is good */
9574cf49a43SJulian Elischer 	if (name == NULL) {
9586b795970SJulian Elischer 		TRAP_ERROR();
9594cf49a43SJulian Elischer 		return (EINVAL);
9604cf49a43SJulian Elischer 	}
961899e9c4eSArchie Cobbs 	if (ng_findhook(node, name) != NULL) {
9626b795970SJulian Elischer 		TRAP_ERROR();
9634cf49a43SJulian Elischer 		return (EEXIST);
9644cf49a43SJulian Elischer 	}
9654cf49a43SJulian Elischer 
9664cf49a43SJulian Elischer 	/* Allocate the hook and link it up */
96730400f03SJulian Elischer 	NG_ALLOC_HOOK(hook);
9684cf49a43SJulian Elischer 	if (hook == NULL) {
9696b795970SJulian Elischer 		TRAP_ERROR();
9704cf49a43SJulian Elischer 		return (ENOMEM);
9714cf49a43SJulian Elischer 	}
9723e4084c8SJulian Elischer 	hook->hk_refs = 1;		/* add a reference for us to return */
97330400f03SJulian Elischer 	hook->hk_flags = HK_INVALID;
9743e4084c8SJulian Elischer 	hook->hk_peer = &ng_deadhook;	/* start off this way */
97530400f03SJulian Elischer 	hook->hk_node = node;
97630400f03SJulian Elischer 	NG_NODE_REF(node);		/* each hook counts as a reference */
9774cf49a43SJulian Elischer 
9783e4084c8SJulian Elischer 	/* Set hook name */
97987e2c66aSHartmut Brandt 	strlcpy(NG_HOOK_NAME(hook), name, NG_HOOKSIZ);
9803e4084c8SJulian Elischer 
9813e4084c8SJulian Elischer 	/*
9823e4084c8SJulian Elischer 	 * Check if the node type code has something to say about it
9833e4084c8SJulian Elischer 	 * If it fails, the unref of the hook will also unref the node.
9843e4084c8SJulian Elischer 	 */
985954c4772SJulian Elischer 	if (node->nd_type->newhook != NULL) {
986954c4772SJulian Elischer 		if ((error = (*node->nd_type->newhook)(node, hook, name))) {
98730400f03SJulian Elischer 			NG_HOOK_UNREF(hook);	/* this frees the hook */
988069154d5SJulian Elischer 			return (error);
989069154d5SJulian Elischer 		}
990954c4772SJulian Elischer 	}
9914cf49a43SJulian Elischer 	/*
9924cf49a43SJulian Elischer 	 * The 'type' agrees so far, so go ahead and link it in.
9934cf49a43SJulian Elischer 	 * We'll ask again later when we actually connect the hooks.
9944cf49a43SJulian Elischer 	 */
99530400f03SJulian Elischer 	LIST_INSERT_HEAD(&node->nd_hooks, hook, hk_hooks);
99630400f03SJulian Elischer 	node->nd_numhooks++;
9973e4084c8SJulian Elischer 	NG_HOOK_REF(hook);	/* one for the node */
9984cf49a43SJulian Elischer 
9994cf49a43SJulian Elischer 	if (hookp)
10004cf49a43SJulian Elischer 		*hookp = hook;
10013e4084c8SJulian Elischer 	return (0);
10024cf49a43SJulian Elischer }
10034cf49a43SJulian Elischer 
10044cf49a43SJulian Elischer /*
1005899e9c4eSArchie Cobbs  * Find a hook
1006899e9c4eSArchie Cobbs  *
1007899e9c4eSArchie Cobbs  * Node types may supply their own optimized routines for finding
1008899e9c4eSArchie Cobbs  * hooks.  If none is supplied, we just do a linear search.
10093e4084c8SJulian Elischer  * XXX Possibly we should add a reference to the hook?
1010899e9c4eSArchie Cobbs  */
1011899e9c4eSArchie Cobbs hook_p
1012899e9c4eSArchie Cobbs ng_findhook(node_p node, const char *name)
1013899e9c4eSArchie Cobbs {
1014899e9c4eSArchie Cobbs 	hook_p hook;
1015899e9c4eSArchie Cobbs 
101630400f03SJulian Elischer 	if (node->nd_type->findhook != NULL)
101730400f03SJulian Elischer 		return (*node->nd_type->findhook)(node, name);
101830400f03SJulian Elischer 	LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
101970de87f2SJulian Elischer 		if (NG_HOOK_IS_VALID(hook)
1020ce5e5f99SArchie Cobbs 		&& (strcmp(NG_HOOK_NAME(hook), name) == 0))
1021899e9c4eSArchie Cobbs 			return (hook);
1022899e9c4eSArchie Cobbs 	}
1023899e9c4eSArchie Cobbs 	return (NULL);
1024899e9c4eSArchie Cobbs }
1025899e9c4eSArchie Cobbs 
1026899e9c4eSArchie Cobbs /*
10274cf49a43SJulian Elischer  * Destroy a hook
10284cf49a43SJulian Elischer  *
10294cf49a43SJulian Elischer  * As hooks are always attached, this really destroys two hooks.
10304cf49a43SJulian Elischer  * The one given, and the one attached to it. Disconnect the hooks
10313e4084c8SJulian Elischer  * from each other first. We reconnect the peer hook to the 'dead'
10323e4084c8SJulian Elischer  * hook so that it can still exist after we depart. We then
10333e4084c8SJulian Elischer  * send the peer its own destroy message. This ensures that we only
10343e4084c8SJulian Elischer  * interact with the peer's structures when it is locked processing that
10353e4084c8SJulian Elischer  * message. We hold a reference to the peer hook so we are guaranteed that
10363e4084c8SJulian Elischer  * the peer hook and node are still going to exist until
10373e4084c8SJulian Elischer  * we are finished there as the hook holds a ref on the node.
10383e4084c8SJulian Elischer  * We run this same code again on the peer hook, but that time it is already
10393e4084c8SJulian Elischer  * attached to the 'dead' hook.
10406b795970SJulian Elischer  *
10416b795970SJulian Elischer  * This routine is called at all stages of hook creation
10426b795970SJulian Elischer  * on error detection and must be able to handle any such stage.
10434cf49a43SJulian Elischer  */
10444cf49a43SJulian Elischer void
10454cf49a43SJulian Elischer ng_destroy_hook(hook_p hook)
10464cf49a43SJulian Elischer {
104730400f03SJulian Elischer 	hook_p peer = NG_HOOK_PEER(hook);
10486b795970SJulian Elischer 	node_p node = NG_HOOK_NODE(hook);
10494cf49a43SJulian Elischer 
10506b795970SJulian Elischer 	if (hook == &ng_deadhook) {	/* better safe than sorry */
10516b795970SJulian Elischer 		printf("ng_destroy_hook called on deadhook\n");
10526b795970SJulian Elischer 		return;
10536b795970SJulian Elischer 	}
105430400f03SJulian Elischer 	hook->hk_flags |= HK_INVALID;		/* as soon as possible */
10553e4084c8SJulian Elischer 	if (peer && (peer != &ng_deadhook)) {
10563e4084c8SJulian Elischer 		/*
10573e4084c8SJulian Elischer 		 * Set the peer to point to ng_deadhook
10583e4084c8SJulian Elischer 		 * from this moment on we are effectively independent it.
10593e4084c8SJulian Elischer 		 * send it an rmhook message of it's own.
10603e4084c8SJulian Elischer 		 */
10613e4084c8SJulian Elischer 		peer->hk_peer = &ng_deadhook;	/* They no longer know us */
10623e4084c8SJulian Elischer 		hook->hk_peer = &ng_deadhook;	/* Nor us, them */
10636b795970SJulian Elischer 		if (NG_HOOK_NODE(peer) == &ng_deadnode) {
10646b795970SJulian Elischer 			/*
10656b795970SJulian Elischer 			 * If it's already divorced from a node,
10666b795970SJulian Elischer 			 * just free it.
10676b795970SJulian Elischer 			 */
10686b795970SJulian Elischer 			/* nothing */
10696b795970SJulian Elischer 		} else {
10706b795970SJulian Elischer 			ng_rmhook_self(peer); 	/* Send it a surprise */
10716b795970SJulian Elischer 		}
107252fa3556SJulian Elischer 		NG_HOOK_UNREF(peer);		/* account for peer link */
107352fa3556SJulian Elischer 		NG_HOOK_UNREF(hook);		/* account for peer link */
10744cf49a43SJulian Elischer 	}
10754cf49a43SJulian Elischer 
10764cf49a43SJulian Elischer 	/*
10774cf49a43SJulian Elischer 	 * Remove the hook from the node's list to avoid possible recursion
10784cf49a43SJulian Elischer 	 * in case the disconnection results in node shutdown.
10794cf49a43SJulian Elischer 	 */
10806b795970SJulian Elischer 	if (node == &ng_deadnode) { /* happens if called from ng_con_nodes() */
10816b795970SJulian Elischer 		return;
10826b795970SJulian Elischer 	}
108330400f03SJulian Elischer 	LIST_REMOVE(hook, hk_hooks);
108430400f03SJulian Elischer 	node->nd_numhooks--;
108530400f03SJulian Elischer 	if (node->nd_type->disconnect) {
10864cf49a43SJulian Elischer 		/*
10876b795970SJulian Elischer 		 * The type handler may elect to destroy the node so don't
10886b795970SJulian Elischer 		 * trust its existance after this point. (except
10896b795970SJulian Elischer 		 * that we still hold a reference on it. (which we
10906b795970SJulian Elischer 		 * inherrited from the hook we are destroying)
10914cf49a43SJulian Elischer 		 */
109230400f03SJulian Elischer 		(*node->nd_type->disconnect) (hook);
10934cf49a43SJulian Elischer 	}
10946b795970SJulian Elischer 
10956b795970SJulian Elischer 	/*
10966b795970SJulian Elischer 	 * Note that because we will point to ng_deadnode, the original node
10976b795970SJulian Elischer 	 * is not decremented automatically so we do that manually.
10986b795970SJulian Elischer 	 */
10996b795970SJulian Elischer 	_NG_HOOK_NODE(hook) = &ng_deadnode;
11006b795970SJulian Elischer 	NG_NODE_UNREF(node);	/* We no longer point to it so adjust count */
11016b795970SJulian Elischer 	NG_HOOK_UNREF(hook);	/* Account for linkage (in list) to node */
11024cf49a43SJulian Elischer }
11034cf49a43SJulian Elischer 
11044cf49a43SJulian Elischer /*
11054cf49a43SJulian Elischer  * Take two hooks on a node and merge the connection so that the given node
11064cf49a43SJulian Elischer  * is effectively bypassed.
11074cf49a43SJulian Elischer  */
11084cf49a43SJulian Elischer int
11094cf49a43SJulian Elischer ng_bypass(hook_p hook1, hook_p hook2)
11104cf49a43SJulian Elischer {
111130400f03SJulian Elischer 	if (hook1->hk_node != hook2->hk_node) {
11126b795970SJulian Elischer 		TRAP_ERROR();
11134cf49a43SJulian Elischer 		return (EINVAL);
111430400f03SJulian Elischer 	}
111530400f03SJulian Elischer 	hook1->hk_peer->hk_peer = hook2->hk_peer;
111630400f03SJulian Elischer 	hook2->hk_peer->hk_peer = hook1->hk_peer;
11174cf49a43SJulian Elischer 
11183e4084c8SJulian Elischer 	hook1->hk_peer = &ng_deadhook;
11193e4084c8SJulian Elischer 	hook2->hk_peer = &ng_deadhook;
11203e4084c8SJulian Elischer 
11214cf49a43SJulian Elischer 	/* XXX If we ever cache methods on hooks update them as well */
11224cf49a43SJulian Elischer 	ng_destroy_hook(hook1);
11234cf49a43SJulian Elischer 	ng_destroy_hook(hook2);
11244cf49a43SJulian Elischer 	return (0);
11254cf49a43SJulian Elischer }
11264cf49a43SJulian Elischer 
11274cf49a43SJulian Elischer /*
11284cf49a43SJulian Elischer  * Install a new netgraph type
11294cf49a43SJulian Elischer  */
11304cf49a43SJulian Elischer int
11314cf49a43SJulian Elischer ng_newtype(struct ng_type *tp)
11324cf49a43SJulian Elischer {
11334cf49a43SJulian Elischer 	const size_t namelen = strlen(tp->name);
11344cf49a43SJulian Elischer 
11354cf49a43SJulian Elischer 	/* Check version and type name fields */
1136589f6ed8SJulian Elischer 	if ((tp->version != NG_ABI_VERSION)
1137589f6ed8SJulian Elischer 	|| (namelen == 0)
113887e2c66aSHartmut Brandt 	|| (namelen >= NG_TYPESIZ)) {
11396b795970SJulian Elischer 		TRAP_ERROR();
11408ed370fdSJulian Elischer 		if (tp->version != NG_ABI_VERSION) {
11418ed370fdSJulian Elischer 			printf("Netgraph: Node type rejected. ABI mismatch. Suggest recompile\n");
11428ed370fdSJulian Elischer 		}
11434cf49a43SJulian Elischer 		return (EINVAL);
11444cf49a43SJulian Elischer 	}
11454cf49a43SJulian Elischer 
11464cf49a43SJulian Elischer 	/* Check for name collision */
11474cf49a43SJulian Elischer 	if (ng_findtype(tp->name) != NULL) {
11486b795970SJulian Elischer 		TRAP_ERROR();
11494cf49a43SJulian Elischer 		return (EEXIST);
11504cf49a43SJulian Elischer 	}
11514cf49a43SJulian Elischer 
1152069154d5SJulian Elischer 
1153069154d5SJulian Elischer 	/* Link in new type */
11549ed346baSBosko Milekic 	mtx_lock(&ng_typelist_mtx);
1155069154d5SJulian Elischer 	LIST_INSERT_HEAD(&ng_typelist, tp, types);
1156c73b94a2SJulian Elischer 	tp->refs = 1;	/* first ref is linked list */
11579ed346baSBosko Milekic 	mtx_unlock(&ng_typelist_mtx);
11584cf49a43SJulian Elischer 	return (0);
11594cf49a43SJulian Elischer }
11604cf49a43SJulian Elischer 
11614cf49a43SJulian Elischer /*
1162c31b4a53SJulian Elischer  * unlink a netgraph type
1163c31b4a53SJulian Elischer  * If no examples exist
1164c31b4a53SJulian Elischer  */
1165c31b4a53SJulian Elischer int
1166c31b4a53SJulian Elischer ng_rmtype(struct ng_type *tp)
1167c31b4a53SJulian Elischer {
1168c31b4a53SJulian Elischer 	/* Check for name collision */
1169c31b4a53SJulian Elischer 	if (tp->refs != 1) {
1170c31b4a53SJulian Elischer 		TRAP_ERROR();
1171c31b4a53SJulian Elischer 		return (EBUSY);
1172c31b4a53SJulian Elischer 	}
1173c31b4a53SJulian Elischer 
1174c31b4a53SJulian Elischer 	/* Unlink type */
1175c31b4a53SJulian Elischer 	mtx_lock(&ng_typelist_mtx);
1176c31b4a53SJulian Elischer 	LIST_REMOVE(tp, types);
1177c31b4a53SJulian Elischer 	mtx_unlock(&ng_typelist_mtx);
1178c31b4a53SJulian Elischer 	return (0);
1179c31b4a53SJulian Elischer }
1180c31b4a53SJulian Elischer 
1181c31b4a53SJulian Elischer /*
11824cf49a43SJulian Elischer  * Look for a type of the name given
11834cf49a43SJulian Elischer  */
11844cf49a43SJulian Elischer struct ng_type *
11854cf49a43SJulian Elischer ng_findtype(const char *typename)
11864cf49a43SJulian Elischer {
11874cf49a43SJulian Elischer 	struct ng_type *type;
11884cf49a43SJulian Elischer 
11899ed346baSBosko Milekic 	mtx_lock(&ng_typelist_mtx);
1190069154d5SJulian Elischer 	LIST_FOREACH(type, &ng_typelist, types) {
11914cf49a43SJulian Elischer 		if (strcmp(type->name, typename) == 0)
11924cf49a43SJulian Elischer 			break;
11934cf49a43SJulian Elischer 	}
11949ed346baSBosko Milekic 	mtx_unlock(&ng_typelist_mtx);
11954cf49a43SJulian Elischer 	return (type);
11964cf49a43SJulian Elischer }
11974cf49a43SJulian Elischer 
11984cf49a43SJulian Elischer /************************************************************************
11994cf49a43SJulian Elischer 			Composite routines
12004cf49a43SJulian Elischer ************************************************************************/
12014cf49a43SJulian Elischer /*
12026b795970SJulian Elischer  * Connect two nodes using the specified hooks, using queued functions.
12034cf49a43SJulian Elischer  */
12041acb27c6SJulian Elischer static void
12056b795970SJulian Elischer ng_con_part3(node_p node, hook_p hook, void *arg1, int arg2)
12064cf49a43SJulian Elischer {
12074cf49a43SJulian Elischer 
12086b795970SJulian Elischer 	/*
12096b795970SJulian Elischer 	 * When we run, we know that the node 'node' is locked for us.
12106b795970SJulian Elischer 	 * Our caller has a reference on the hook.
12116b795970SJulian Elischer 	 * Our caller has a reference on the node.
12126b795970SJulian Elischer 	 * (In this case our caller is ng_apply_item() ).
12136b795970SJulian Elischer 	 * The peer hook has a reference on the hook.
12141acb27c6SJulian Elischer 	 * We are all set up except for the final call to the node, and
12151acb27c6SJulian Elischer 	 * the clearing of the INVALID flag.
12166b795970SJulian Elischer 	 */
12176b795970SJulian Elischer 	if (NG_HOOK_NODE(hook) == &ng_deadnode) {
12186b795970SJulian Elischer 		/*
12196b795970SJulian Elischer 		 * The node must have been freed again since we last visited
12206b795970SJulian Elischer 		 * here. ng_destry_hook() has this effect but nothing else does.
12216b795970SJulian Elischer 		 * We should just release our references and
12226b795970SJulian Elischer 		 * free anything we can think of.
12236b795970SJulian Elischer 		 * Since we know it's been destroyed, and it's our caller
12246b795970SJulian Elischer 		 * that holds the references, just return.
12256b795970SJulian Elischer 		 */
12261acb27c6SJulian Elischer 		return ;
12276b795970SJulian Elischer 	}
12286b795970SJulian Elischer 	if (hook->hk_node->nd_type->connect) {
12291acb27c6SJulian Elischer 		if ((*hook->hk_node->nd_type->connect) (hook)) {
12306b795970SJulian Elischer 			ng_destroy_hook(hook);	/* also zaps peer */
12311acb27c6SJulian Elischer 			printf("failed in ng_con_part3()\n");
12321acb27c6SJulian Elischer 			return ;
12334cf49a43SJulian Elischer 		}
12346b795970SJulian Elischer 	}
12356b795970SJulian Elischer 	/*
12366b795970SJulian Elischer 	 *  XXX this is wrong for SMP. Possibly we need
12376b795970SJulian Elischer 	 * to separate out 'create' and 'invalid' flags.
12386b795970SJulian Elischer 	 * should only set flags on hooks we have locked under our node.
12396b795970SJulian Elischer 	 */
12406b795970SJulian Elischer 	hook->hk_flags &= ~HK_INVALID;
12411acb27c6SJulian Elischer 	return ;
12426b795970SJulian Elischer }
12436b795970SJulian Elischer 
12441acb27c6SJulian Elischer static void
12456b795970SJulian Elischer ng_con_part2(node_p node, hook_p hook, void *arg1, int arg2)
12466b795970SJulian Elischer {
12476b795970SJulian Elischer 
12486b795970SJulian Elischer 	/*
12496b795970SJulian Elischer 	 * When we run, we know that the node 'node' is locked for us.
12506b795970SJulian Elischer 	 * Our caller has a reference on the hook.
12516b795970SJulian Elischer 	 * Our caller has a reference on the node.
12526b795970SJulian Elischer 	 * (In this case our caller is ng_apply_item() ).
12536b795970SJulian Elischer 	 * The peer hook has a reference on the hook.
12546b795970SJulian Elischer 	 * our node pointer points to the 'dead' node.
12556b795970SJulian Elischer 	 * First check the hook name is unique.
12561acb27c6SJulian Elischer 	 * Should not happen because we checked before queueing this.
12576b795970SJulian Elischer 	 */
12586b795970SJulian Elischer 	if (ng_findhook(node, NG_HOOK_NAME(hook)) != NULL) {
12596b795970SJulian Elischer 		TRAP_ERROR();
12606b795970SJulian Elischer 		ng_destroy_hook(hook); /* should destroy peer too */
12611acb27c6SJulian Elischer 		printf("failed in ng_con_part2()\n");
12621acb27c6SJulian Elischer 		return ;
12636b795970SJulian Elischer 	}
12646b795970SJulian Elischer 	/*
12656b795970SJulian Elischer 	 * Check if the node type code has something to say about it
12666b795970SJulian Elischer 	 * If it fails, the unref of the hook will also unref the attached node,
12676b795970SJulian Elischer 	 * however since that node is 'ng_deadnode' this will do nothing.
12686b795970SJulian Elischer 	 * The peer hook will also be destroyed.
12696b795970SJulian Elischer 	 */
12706b795970SJulian Elischer 	if (node->nd_type->newhook != NULL) {
12711acb27c6SJulian Elischer 		if ((*node->nd_type->newhook)(node, hook, hook->hk_name)) {
12726b795970SJulian Elischer 			ng_destroy_hook(hook); /* should destroy peer too */
12731acb27c6SJulian Elischer 			printf("failed in ng_con_part2()\n");
12741acb27c6SJulian Elischer 			return ;
12756b795970SJulian Elischer 		}
12766b795970SJulian Elischer 	}
12776b795970SJulian Elischer 
12786b795970SJulian Elischer 	/*
12796b795970SJulian Elischer 	 * The 'type' agrees so far, so go ahead and link it in.
12806b795970SJulian Elischer 	 * We'll ask again later when we actually connect the hooks.
12816b795970SJulian Elischer 	 */
12826b795970SJulian Elischer 	hook->hk_node = node;		/* just overwrite ng_deadnode */
12836b795970SJulian Elischer 	NG_NODE_REF(node);		/* each hook counts as a reference */
12846b795970SJulian Elischer 	LIST_INSERT_HEAD(&node->nd_hooks, hook, hk_hooks);
12856b795970SJulian Elischer 	node->nd_numhooks++;
12866b795970SJulian Elischer 	NG_HOOK_REF(hook);	/* one for the node */
12876b795970SJulian Elischer 
12886b795970SJulian Elischer 	/*
12896b795970SJulian Elischer 	 * We now have a symetrical situation, where both hooks have been
12905951069aSJulian Elischer 	 * linked to their nodes, the newhook methods have been called
12916b795970SJulian Elischer 	 * And the references are all correct. The hooks are still marked
12926b795970SJulian Elischer 	 * as invalid, as we have not called the 'connect' methods
12936b795970SJulian Elischer 	 * yet.
12946b795970SJulian Elischer 	 * We can call the local one immediatly as we have the
12956b795970SJulian Elischer 	 * node locked, but we need to queue the remote one.
12966b795970SJulian Elischer 	 */
12976b795970SJulian Elischer 	if (hook->hk_node->nd_type->connect) {
12981acb27c6SJulian Elischer 		if ((*hook->hk_node->nd_type->connect) (hook)) {
12996b795970SJulian Elischer 			ng_destroy_hook(hook);	/* also zaps peer */
13001acb27c6SJulian Elischer 			printf("failed in ng_con_part2(A)\n");
13011acb27c6SJulian Elischer 			return ;
13026b795970SJulian Elischer 		}
13036b795970SJulian Elischer 	}
13041acb27c6SJulian Elischer 	if (ng_send_fn(hook->hk_peer->hk_node, hook->hk_peer,
13051acb27c6SJulian Elischer 			&ng_con_part3, arg1, arg2)) {
13061acb27c6SJulian Elischer 		printf("failed in ng_con_part2(B)");
13071acb27c6SJulian Elischer 		ng_destroy_hook(hook);	/* also zaps peer */
13081acb27c6SJulian Elischer 		return ;
13091acb27c6SJulian Elischer 	}
13106b795970SJulian Elischer 	hook->hk_flags &= ~HK_INVALID; /* need both to be able to work */
13111acb27c6SJulian Elischer 	return ;
13124cf49a43SJulian Elischer }
13134cf49a43SJulian Elischer 
13144cf49a43SJulian Elischer /*
13156b795970SJulian Elischer  * Connect this node with another node. We assume that this node is
13166b795970SJulian Elischer  * currently locked, as we are only called from an NGM_CONNECT message.
13174cf49a43SJulian Elischer  */
13186b795970SJulian Elischer static int
13194cf49a43SJulian Elischer ng_con_nodes(node_p node, const char *name, node_p node2, const char *name2)
13204cf49a43SJulian Elischer {
13214cf49a43SJulian Elischer 	int     error;
13224cf49a43SJulian Elischer 	hook_p  hook;
13234cf49a43SJulian Elischer 	hook_p  hook2;
13244cf49a43SJulian Elischer 
13251acb27c6SJulian Elischer 	if (ng_findhook(node2, name2) != NULL) {
13261acb27c6SJulian Elischer 		return(EEXIST);
13271acb27c6SJulian Elischer 	}
13283e4084c8SJulian Elischer 	if ((error = ng_add_hook(node, name, &hook)))  /* gives us a ref */
13294cf49a43SJulian Elischer 		return (error);
13306b795970SJulian Elischer 	/* Allocate the other hook and link it up */
13316b795970SJulian Elischer 	NG_ALLOC_HOOK(hook2);
133219724144SGleb Smirnoff 	if (hook2 == NULL) {
13336b795970SJulian Elischer 		TRAP_ERROR();
13346b795970SJulian Elischer 		ng_destroy_hook(hook);	/* XXX check ref counts so far */
13356b795970SJulian Elischer 		NG_HOOK_UNREF(hook);	/* including our ref */
13366b795970SJulian Elischer 		return (ENOMEM);
13376b795970SJulian Elischer 	}
13386b795970SJulian Elischer 	hook2->hk_refs = 1;		/* start with a reference for us. */
13396b795970SJulian Elischer 	hook2->hk_flags = HK_INVALID;
13406b795970SJulian Elischer 	hook2->hk_peer = hook;		/* Link the two together */
13416b795970SJulian Elischer 	hook->hk_peer = hook2;
13426b795970SJulian Elischer 	NG_HOOK_REF(hook);		/* Add a ref for the peer to each*/
13436b795970SJulian Elischer 	NG_HOOK_REF(hook2);
13446b795970SJulian Elischer 	hook2->hk_node = &ng_deadnode;
134587e2c66aSHartmut Brandt 	strlcpy(NG_HOOK_NAME(hook2), name2, NG_HOOKSIZ);
13466b795970SJulian Elischer 
13476b795970SJulian Elischer 	/*
13486b795970SJulian Elischer 	 * Queue the function above.
13496b795970SJulian Elischer 	 * Procesing continues in that function in the lock context of
13506b795970SJulian Elischer 	 * the other node.
13516b795970SJulian Elischer 	 */
13521acb27c6SJulian Elischer 	ng_send_fn(node2, hook2, &ng_con_part2, NULL, 0);
13536b795970SJulian Elischer 
13546b795970SJulian Elischer 	NG_HOOK_UNREF(hook);		/* Let each hook go if it wants to */
13556b795970SJulian Elischer 	NG_HOOK_UNREF(hook2);
13561acb27c6SJulian Elischer 	return (0);
13574cf49a43SJulian Elischer }
13586b795970SJulian Elischer 
13596b795970SJulian Elischer /*
13606b795970SJulian Elischer  * Make a peer and connect.
13616b795970SJulian Elischer  * We assume that the local node is locked.
13626b795970SJulian Elischer  * The new node probably doesn't need a lock until
13636b795970SJulian Elischer  * it has a hook, because it cannot really have any work until then,
13646b795970SJulian Elischer  * but we should think about it a bit more.
13656b795970SJulian Elischer  *
13666b795970SJulian Elischer  * The problem may come if the other node also fires up
13676b795970SJulian Elischer  * some hardware or a timer or some other source of activation,
13686b795970SJulian Elischer  * also it may already get a command msg via it's ID.
13696b795970SJulian Elischer  *
13706b795970SJulian Elischer  * We could use the same method as ng_con_nodes() but we'd have
13716b795970SJulian Elischer  * to add ability to remove the node when failing. (Not hard, just
13726b795970SJulian Elischer  * make arg1 point to the node to remove).
13736b795970SJulian Elischer  * Unless of course we just ignore failure to connect and leave
13746b795970SJulian Elischer  * an unconnected node?
13756b795970SJulian Elischer  */
13766b795970SJulian Elischer static int
13776b795970SJulian Elischer ng_mkpeer(node_p node, const char *name, const char *name2, char *type)
13786b795970SJulian Elischer {
13796b795970SJulian Elischer 	node_p  node2;
13806b795970SJulian Elischer 	hook_p  hook1;
13816b795970SJulian Elischer 	hook_p  hook2;
13826b795970SJulian Elischer 	int     error;
13836b795970SJulian Elischer 
13846b795970SJulian Elischer 	if ((error = ng_make_node(type, &node2))) {
13856b795970SJulian Elischer 		return (error);
13866b795970SJulian Elischer 	}
13876b795970SJulian Elischer 
13886b795970SJulian Elischer 	if ((error = ng_add_hook(node, name, &hook1))) { /* gives us a ref */
13891acb27c6SJulian Elischer 		ng_rmnode(node2, NULL, NULL, 0);
13906b795970SJulian Elischer 		return (error);
13916b795970SJulian Elischer 	}
13926b795970SJulian Elischer 
13936b795970SJulian Elischer 	if ((error = ng_add_hook(node2, name2, &hook2))) {
13941acb27c6SJulian Elischer 		ng_rmnode(node2, NULL, NULL, 0);
13956b795970SJulian Elischer 		ng_destroy_hook(hook1);
13966b795970SJulian Elischer 		NG_HOOK_UNREF(hook1);
13976b795970SJulian Elischer 		return (error);
13986b795970SJulian Elischer 	}
13996b795970SJulian Elischer 
14006b795970SJulian Elischer 	/*
14016b795970SJulian Elischer 	 * Actually link the two hooks together.
14026b795970SJulian Elischer 	 */
14036b795970SJulian Elischer 	hook1->hk_peer = hook2;
14046b795970SJulian Elischer 	hook2->hk_peer = hook1;
14056b795970SJulian Elischer 
14066b795970SJulian Elischer 	/* Each hook is referenced by the other */
14076b795970SJulian Elischer 	NG_HOOK_REF(hook1);
14086b795970SJulian Elischer 	NG_HOOK_REF(hook2);
14096b795970SJulian Elischer 
14106b795970SJulian Elischer 	/* Give each node the opportunity to veto the pending connection */
14116b795970SJulian Elischer 	if (hook1->hk_node->nd_type->connect) {
14126b795970SJulian Elischer 		error = (*hook1->hk_node->nd_type->connect) (hook1);
14136b795970SJulian Elischer 	}
14146b795970SJulian Elischer 
14156b795970SJulian Elischer 	if ((error == 0) && hook2->hk_node->nd_type->connect) {
14166b795970SJulian Elischer 		error = (*hook2->hk_node->nd_type->connect) (hook2);
14176b795970SJulian Elischer 
14186b795970SJulian Elischer 	}
14193e4084c8SJulian Elischer 
14203e4084c8SJulian Elischer 	/*
14213e4084c8SJulian Elischer 	 * drop the references we were holding on the two hooks.
14223e4084c8SJulian Elischer 	 */
14236b795970SJulian Elischer 	if (error) {
14246b795970SJulian Elischer 		ng_destroy_hook(hook2);	/* also zaps hook1 */
14251acb27c6SJulian Elischer 		ng_rmnode(node2, NULL, NULL, 0);
14266b795970SJulian Elischer 	} else {
14276b795970SJulian Elischer 		/* As a last act, allow the hooks to be used */
14286b795970SJulian Elischer 		hook1->hk_flags &= ~HK_INVALID;
14296b795970SJulian Elischer 		hook2->hk_flags &= ~HK_INVALID;
14306b795970SJulian Elischer 	}
14316b795970SJulian Elischer 	NG_HOOK_UNREF(hook1);
14323e4084c8SJulian Elischer 	NG_HOOK_UNREF(hook2);
14333e4084c8SJulian Elischer 	return (error);
14344cf49a43SJulian Elischer }
14356b795970SJulian Elischer 
1436069154d5SJulian Elischer /************************************************************************
1437069154d5SJulian Elischer 		Utility routines to send self messages
1438069154d5SJulian Elischer ************************************************************************/
1439069154d5SJulian Elischer 
14401acb27c6SJulian Elischer /* Shut this node down as soon as everyone is clear of it */
14411acb27c6SJulian Elischer /* Should add arg "immediatly" to jump the queue */
1442069154d5SJulian Elischer int
14431acb27c6SJulian Elischer ng_rmnode_self(node_p node)
1444069154d5SJulian Elischer {
14451acb27c6SJulian Elischer 	int		error;
14464cf49a43SJulian Elischer 
14471acb27c6SJulian Elischer 	if (node == &ng_deadnode)
14481acb27c6SJulian Elischer 		return (0);
1449be4252b3SJulian Elischer 	node->nd_flags |= NGF_INVALID;
1450be4252b3SJulian Elischer 	if (node->nd_flags & NGF_CLOSING)
14511acb27c6SJulian Elischer 		return (0);
1452069154d5SJulian Elischer 
14531acb27c6SJulian Elischer 	error = ng_send_fn(node, NULL, &ng_rmnode, NULL, 0);
14541acb27c6SJulian Elischer 	return (error);
1455069154d5SJulian Elischer }
1456069154d5SJulian Elischer 
14571acb27c6SJulian Elischer static void
14586b795970SJulian Elischer ng_rmhook_part2(node_p node, hook_p hook, void *arg1, int arg2)
14596b795970SJulian Elischer {
14606b795970SJulian Elischer 	ng_destroy_hook(hook);
14611acb27c6SJulian Elischer 	return ;
14626b795970SJulian Elischer }
14636b795970SJulian Elischer 
1464954c4772SJulian Elischer int
1465954c4772SJulian Elischer ng_rmhook_self(hook_p hook)
1466954c4772SJulian Elischer {
14676b795970SJulian Elischer 	int		error;
1468954c4772SJulian Elischer 	node_p node = NG_HOOK_NODE(hook);
1469954c4772SJulian Elischer 
14706b795970SJulian Elischer 	if (node == &ng_deadnode)
14716b795970SJulian Elischer 		return (0);
14726b795970SJulian Elischer 
14736b795970SJulian Elischer 	error = ng_send_fn(node, hook, &ng_rmhook_part2, NULL, 0);
14746b795970SJulian Elischer 	return (error);
1475954c4772SJulian Elischer }
1476954c4772SJulian Elischer 
1477069154d5SJulian Elischer /***********************************************************************
14784cf49a43SJulian Elischer  * Parse and verify a string of the form:  <NODE:><PATH>
14794cf49a43SJulian Elischer  *
14804cf49a43SJulian Elischer  * Such a string can refer to a specific node or a specific hook
14814cf49a43SJulian Elischer  * on a specific node, depending on how you look at it. In the
14824cf49a43SJulian Elischer  * latter case, the PATH component must not end in a dot.
14834cf49a43SJulian Elischer  *
14844cf49a43SJulian Elischer  * Both <NODE:> and <PATH> are optional. The <PATH> is a string
14854cf49a43SJulian Elischer  * of hook names separated by dots. This breaks out the original
14864cf49a43SJulian Elischer  * string, setting *nodep to "NODE" (or NULL if none) and *pathp
14874cf49a43SJulian Elischer  * to "PATH" (or NULL if degenerate). Also, *hookp will point to
14884cf49a43SJulian Elischer  * the final hook component of <PATH>, if any, otherwise NULL.
14894cf49a43SJulian Elischer  *
14904cf49a43SJulian Elischer  * This returns -1 if the path is malformed. The char ** are optional.
1491069154d5SJulian Elischer  ***********************************************************************/
14924cf49a43SJulian Elischer int
14934cf49a43SJulian Elischer ng_path_parse(char *addr, char **nodep, char **pathp, char **hookp)
14944cf49a43SJulian Elischer {
14954cf49a43SJulian Elischer 	char   *node, *path, *hook;
14964cf49a43SJulian Elischer 	int     k;
14974cf49a43SJulian Elischer 
14984cf49a43SJulian Elischer 	/*
14994cf49a43SJulian Elischer 	 * Extract absolute NODE, if any
15004cf49a43SJulian Elischer 	 */
15014cf49a43SJulian Elischer 	for (path = addr; *path && *path != ':'; path++);
15024cf49a43SJulian Elischer 	if (*path) {
15034cf49a43SJulian Elischer 		node = addr;	/* Here's the NODE */
15044cf49a43SJulian Elischer 		*path++ = '\0';	/* Here's the PATH */
15054cf49a43SJulian Elischer 
15064cf49a43SJulian Elischer 		/* Node name must not be empty */
15074cf49a43SJulian Elischer 		if (!*node)
15084cf49a43SJulian Elischer 			return -1;
15094cf49a43SJulian Elischer 
15104cf49a43SJulian Elischer 		/* A name of "." is OK; otherwise '.' not allowed */
15114cf49a43SJulian Elischer 		if (strcmp(node, ".") != 0) {
15124cf49a43SJulian Elischer 			for (k = 0; node[k]; k++)
15134cf49a43SJulian Elischer 				if (node[k] == '.')
15144cf49a43SJulian Elischer 					return -1;
15154cf49a43SJulian Elischer 		}
15164cf49a43SJulian Elischer 	} else {
15174cf49a43SJulian Elischer 		node = NULL;	/* No absolute NODE */
15184cf49a43SJulian Elischer 		path = addr;	/* Here's the PATH */
15194cf49a43SJulian Elischer 	}
15204cf49a43SJulian Elischer 
15214cf49a43SJulian Elischer 	/* Snoop for illegal characters in PATH */
15224cf49a43SJulian Elischer 	for (k = 0; path[k]; k++)
15234cf49a43SJulian Elischer 		if (path[k] == ':')
15244cf49a43SJulian Elischer 			return -1;
15254cf49a43SJulian Elischer 
15264cf49a43SJulian Elischer 	/* Check for no repeated dots in PATH */
15274cf49a43SJulian Elischer 	for (k = 0; path[k]; k++)
15284cf49a43SJulian Elischer 		if (path[k] == '.' && path[k + 1] == '.')
15294cf49a43SJulian Elischer 			return -1;
15304cf49a43SJulian Elischer 
15314cf49a43SJulian Elischer 	/* Remove extra (degenerate) dots from beginning or end of PATH */
15324cf49a43SJulian Elischer 	if (path[0] == '.')
15334cf49a43SJulian Elischer 		path++;
15344cf49a43SJulian Elischer 	if (*path && path[strlen(path) - 1] == '.')
15354cf49a43SJulian Elischer 		path[strlen(path) - 1] = 0;
15364cf49a43SJulian Elischer 
15374cf49a43SJulian Elischer 	/* If PATH has a dot, then we're not talking about a hook */
15384cf49a43SJulian Elischer 	if (*path) {
15394cf49a43SJulian Elischer 		for (hook = path, k = 0; path[k]; k++)
15404cf49a43SJulian Elischer 			if (path[k] == '.') {
15414cf49a43SJulian Elischer 				hook = NULL;
15424cf49a43SJulian Elischer 				break;
15434cf49a43SJulian Elischer 			}
15444cf49a43SJulian Elischer 	} else
15454cf49a43SJulian Elischer 		path = hook = NULL;
15464cf49a43SJulian Elischer 
15474cf49a43SJulian Elischer 	/* Done */
15484cf49a43SJulian Elischer 	if (nodep)
15494cf49a43SJulian Elischer 		*nodep = node;
15504cf49a43SJulian Elischer 	if (pathp)
15514cf49a43SJulian Elischer 		*pathp = path;
15524cf49a43SJulian Elischer 	if (hookp)
15534cf49a43SJulian Elischer 		*hookp = hook;
15544cf49a43SJulian Elischer 	return (0);
15554cf49a43SJulian Elischer }
15564cf49a43SJulian Elischer 
15574cf49a43SJulian Elischer /*
15584cf49a43SJulian Elischer  * Given a path, which may be absolute or relative, and a starting node,
1559069154d5SJulian Elischer  * return the destination node.
15604cf49a43SJulian Elischer  */
15614cf49a43SJulian Elischer int
1562069154d5SJulian Elischer ng_path2noderef(node_p here, const char *address,
1563069154d5SJulian Elischer 				node_p *destp, hook_p *lasthook)
15644cf49a43SJulian Elischer {
156587e2c66aSHartmut Brandt 	char    fullpath[NG_PATHSIZ];
15664cf49a43SJulian Elischer 	char   *nodename, *path, pbuf[2];
1567069154d5SJulian Elischer 	node_p  node, oldnode;
15684cf49a43SJulian Elischer 	char   *cp;
1569a4ec03cfSJulian Elischer 	hook_p hook = NULL;
15704cf49a43SJulian Elischer 
15714cf49a43SJulian Elischer 	/* Initialize */
157230400f03SJulian Elischer 	if (destp == NULL) {
15736b795970SJulian Elischer 		TRAP_ERROR();
15744cf49a43SJulian Elischer 		return EINVAL;
157530400f03SJulian Elischer 	}
15764cf49a43SJulian Elischer 	*destp = NULL;
15774cf49a43SJulian Elischer 
15784cf49a43SJulian Elischer 	/* Make a writable copy of address for ng_path_parse() */
15794cf49a43SJulian Elischer 	strncpy(fullpath, address, sizeof(fullpath) - 1);
15804cf49a43SJulian Elischer 	fullpath[sizeof(fullpath) - 1] = '\0';
15814cf49a43SJulian Elischer 
15824cf49a43SJulian Elischer 	/* Parse out node and sequence of hooks */
15834cf49a43SJulian Elischer 	if (ng_path_parse(fullpath, &nodename, &path, NULL) < 0) {
15846b795970SJulian Elischer 		TRAP_ERROR();
15854cf49a43SJulian Elischer 		return EINVAL;
15864cf49a43SJulian Elischer 	}
15874cf49a43SJulian Elischer 	if (path == NULL) {
15884cf49a43SJulian Elischer 		pbuf[0] = '.';	/* Needs to be writable */
15894cf49a43SJulian Elischer 		pbuf[1] = '\0';
15904cf49a43SJulian Elischer 		path = pbuf;
15914cf49a43SJulian Elischer 	}
15924cf49a43SJulian Elischer 
1593069154d5SJulian Elischer 	/*
1594069154d5SJulian Elischer 	 * For an absolute address, jump to the starting node.
1595069154d5SJulian Elischer 	 * Note that this holds a reference on the node for us.
1596069154d5SJulian Elischer 	 * Don't forget to drop the reference if we don't need it.
1597069154d5SJulian Elischer 	 */
15984cf49a43SJulian Elischer 	if (nodename) {
1599069154d5SJulian Elischer 		node = ng_name2noderef(here, nodename);
16004cf49a43SJulian Elischer 		if (node == NULL) {
16016b795970SJulian Elischer 			TRAP_ERROR();
16024cf49a43SJulian Elischer 			return (ENOENT);
16034cf49a43SJulian Elischer 		}
1604069154d5SJulian Elischer 	} else {
1605069154d5SJulian Elischer 		if (here == NULL) {
16066b795970SJulian Elischer 			TRAP_ERROR();
1607069154d5SJulian Elischer 			return (EINVAL);
1608069154d5SJulian Elischer 		}
16094cf49a43SJulian Elischer 		node = here;
161030400f03SJulian Elischer 		NG_NODE_REF(node);
1611069154d5SJulian Elischer 	}
16124cf49a43SJulian Elischer 
1613069154d5SJulian Elischer 	/*
1614069154d5SJulian Elischer 	 * Now follow the sequence of hooks
1615069154d5SJulian Elischer 	 * XXX
1616069154d5SJulian Elischer 	 * We actually cannot guarantee that the sequence
1617069154d5SJulian Elischer 	 * is not being demolished as we crawl along it
1618069154d5SJulian Elischer 	 * without extra-ordinary locking etc.
1619069154d5SJulian Elischer 	 * So this is a bit dodgy to say the least.
1620069154d5SJulian Elischer 	 * We can probably hold up some things by holding
1621069154d5SJulian Elischer 	 * the nodelist mutex for the time of this
1622069154d5SJulian Elischer 	 * crawl if we wanted.. At least that way we wouldn't have to
1623069154d5SJulian Elischer 	 * worry about the nodes dissappearing, but the hooks would still
1624069154d5SJulian Elischer 	 * be a problem.
1625069154d5SJulian Elischer 	 */
16264cf49a43SJulian Elischer 	for (cp = path; node != NULL && *cp != '\0'; ) {
16274cf49a43SJulian Elischer 		char *segment;
16284cf49a43SJulian Elischer 
16294cf49a43SJulian Elischer 		/*
16304cf49a43SJulian Elischer 		 * Break out the next path segment. Replace the dot we just
16314cf49a43SJulian Elischer 		 * found with a NUL; "cp" points to the next segment (or the
16324cf49a43SJulian Elischer 		 * NUL at the end).
16334cf49a43SJulian Elischer 		 */
16344cf49a43SJulian Elischer 		for (segment = cp; *cp != '\0'; cp++) {
16354cf49a43SJulian Elischer 			if (*cp == '.') {
16364cf49a43SJulian Elischer 				*cp++ = '\0';
16374cf49a43SJulian Elischer 				break;
16384cf49a43SJulian Elischer 			}
16394cf49a43SJulian Elischer 		}
16404cf49a43SJulian Elischer 
16414cf49a43SJulian Elischer 		/* Empty segment */
16424cf49a43SJulian Elischer 		if (*segment == '\0')
16434cf49a43SJulian Elischer 			continue;
16444cf49a43SJulian Elischer 
16454cf49a43SJulian Elischer 		/* We have a segment, so look for a hook by that name */
1646899e9c4eSArchie Cobbs 		hook = ng_findhook(node, segment);
16474cf49a43SJulian Elischer 
16484cf49a43SJulian Elischer 		/* Can't get there from here... */
16494cf49a43SJulian Elischer 		if (hook == NULL
165030400f03SJulian Elischer 		    || NG_HOOK_PEER(hook) == NULL
165130400f03SJulian Elischer 		    || NG_HOOK_NOT_VALID(hook)
165230400f03SJulian Elischer 		    || NG_HOOK_NOT_VALID(NG_HOOK_PEER(hook))) {
16536b795970SJulian Elischer 			TRAP_ERROR();
165430400f03SJulian Elischer 			NG_NODE_UNREF(node);
165530400f03SJulian Elischer #if 0
165630400f03SJulian Elischer 			printf("hooknotvalid %s %s %d %d %d %d ",
165730400f03SJulian Elischer 					path,
165830400f03SJulian Elischer 					segment,
165930400f03SJulian Elischer 					hook == NULL,
166030400f03SJulian Elischer 		     			NG_HOOK_PEER(hook) == NULL,
166130400f03SJulian Elischer 		     			NG_HOOK_NOT_VALID(hook),
166230400f03SJulian Elischer 		     			NG_HOOK_NOT_VALID(NG_HOOK_PEER(hook)));
166330400f03SJulian Elischer #endif
16644cf49a43SJulian Elischer 			return (ENOENT);
16654cf49a43SJulian Elischer 		}
16664cf49a43SJulian Elischer 
1667069154d5SJulian Elischer 		/*
1668069154d5SJulian Elischer 		 * Hop on over to the next node
1669069154d5SJulian Elischer 		 * XXX
1670069154d5SJulian Elischer 		 * Big race conditions here as hooks and nodes go away
1671069154d5SJulian Elischer 		 * *** Idea.. store an ng_ID_t in each hook and use that
1672069154d5SJulian Elischer 		 * instead of the direct hook in this crawl?
1673069154d5SJulian Elischer 		 */
1674069154d5SJulian Elischer 		oldnode = node;
167530400f03SJulian Elischer 		if ((node = NG_PEER_NODE(hook)))
167630400f03SJulian Elischer 			NG_NODE_REF(node);	/* XXX RACE */
167730400f03SJulian Elischer 		NG_NODE_UNREF(oldnode);	/* XXX another race */
167830400f03SJulian Elischer 		if (NG_NODE_NOT_VALID(node)) {
167930400f03SJulian Elischer 			NG_NODE_UNREF(node);	/* XXX more races */
1680069154d5SJulian Elischer 			node = NULL;
1681069154d5SJulian Elischer 		}
16824cf49a43SJulian Elischer 	}
16834cf49a43SJulian Elischer 
16844cf49a43SJulian Elischer 	/* If node somehow missing, fail here (probably this is not needed) */
16854cf49a43SJulian Elischer 	if (node == NULL) {
16866b795970SJulian Elischer 		TRAP_ERROR();
16874cf49a43SJulian Elischer 		return (ENXIO);
16884cf49a43SJulian Elischer 	}
16894cf49a43SJulian Elischer 
16904cf49a43SJulian Elischer 	/* Done */
16914cf49a43SJulian Elischer 	*destp = node;
16921bdebe4dSArchie Cobbs 	if (lasthook != NULL)
169330400f03SJulian Elischer 		*lasthook = (hook ? NG_HOOK_PEER(hook) : NULL);
1694069154d5SJulian Elischer 	return (0);
1695069154d5SJulian Elischer }
1696069154d5SJulian Elischer 
1697069154d5SJulian Elischer /***************************************************************\
1698069154d5SJulian Elischer * Input queue handling.
1699069154d5SJulian Elischer * All activities are submitted to the node via the input queue
1700069154d5SJulian Elischer * which implements a multiple-reader/single-writer gate.
1701069154d5SJulian Elischer * Items which cannot be handled immeditly are queued.
1702069154d5SJulian Elischer *
1703069154d5SJulian Elischer * read-write queue locking inline functions			*
1704069154d5SJulian Elischer \***************************************************************/
1705069154d5SJulian Elischer 
1706069154d5SJulian Elischer static __inline item_p ng_dequeue(struct ng_queue * ngq);
1707069154d5SJulian Elischer static __inline item_p ng_acquire_read(struct ng_queue * ngq,
1708069154d5SJulian Elischer 					item_p  item);
1709069154d5SJulian Elischer static __inline item_p ng_acquire_write(struct ng_queue * ngq,
1710069154d5SJulian Elischer 					item_p  item);
1711069154d5SJulian Elischer static __inline void	ng_leave_read(struct ng_queue * ngq);
1712069154d5SJulian Elischer static __inline void	ng_leave_write(struct ng_queue * ngq);
1713069154d5SJulian Elischer static __inline void	ng_queue_rw(struct ng_queue * ngq,
1714069154d5SJulian Elischer 					item_p  item, int rw);
1715069154d5SJulian Elischer 
1716069154d5SJulian Elischer /*
1717069154d5SJulian Elischer  * Definition of the bits fields in the ng_queue flag word.
1718069154d5SJulian Elischer  * Defined here rather than in netgraph.h because no-one should fiddle
1719069154d5SJulian Elischer  * with them.
1720069154d5SJulian Elischer  *
1721b57a7965SJulian Elischer  * The ordering here may be important! don't shuffle these.
1722069154d5SJulian Elischer  */
1723069154d5SJulian Elischer /*-
1724069154d5SJulian Elischer  Safety Barrier--------+ (adjustable to suit taste) (not used yet)
1725069154d5SJulian Elischer                        |
1726069154d5SJulian Elischer                        V
1727069154d5SJulian Elischer +-------+-------+-------+-------+-------+-------+-------+-------+
1728069154d5SJulian Elischer | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
1729b57a7965SJulian Elischer | |A|c|t|i|v|e| |R|e|a|d|e|r| |C|o|u|n|t| | | | | | | | | |R|A|W|
1730b57a7965SJulian Elischer | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |P|W|P|
1731069154d5SJulian Elischer +-------+-------+-------+-------+-------+-------+-------+-------+
1732b57a7965SJulian Elischer \___________________________ ____________________________/ | | |
1733b57a7965SJulian Elischer                             V                              | | |
1734b57a7965SJulian Elischer                   [active reader count]                    | | |
1735069154d5SJulian Elischer                                                            | | |
1736b57a7965SJulian Elischer           Read Pending ------------------------------------+ | |
1737069154d5SJulian Elischer                                                              | |
1738b57a7965SJulian Elischer           Active Writer -------------------------------------+ |
1739069154d5SJulian Elischer                                                                |
1740b57a7965SJulian Elischer           Write Pending ---------------------------------------+
1741b57a7965SJulian Elischer 
1742b57a7965SJulian Elischer 
1743069154d5SJulian Elischer */
1744b57a7965SJulian Elischer #define WRITE_PENDING	0x00000001
1745b57a7965SJulian Elischer #define WRITER_ACTIVE	0x00000002
1746b57a7965SJulian Elischer #define READ_PENDING	0x00000004
1747b57a7965SJulian Elischer #define READER_INCREMENT 0x00000008
1748c6118fccSGleb Smirnoff #define READER_MASK	0xfffffff8	/* Not valid if WRITER_ACTIVE is set */
1749069154d5SJulian Elischer #define SAFETY_BARRIER	0x00100000	/* 64K items queued should be enough */
1750b57a7965SJulian Elischer 
1751b57a7965SJulian Elischer /* Defines of more elaborate states on the queue */
1752b57a7965SJulian Elischer /* Mask of bits a read cares about */
1753b57a7965SJulian Elischer #define NGQ_RMASK	(WRITE_PENDING|WRITER_ACTIVE|READ_PENDING)
1754b57a7965SJulian Elischer 
1755b57a7965SJulian Elischer /* Mask of bits a write cares about */
1756b57a7965SJulian Elischer #define NGQ_WMASK	(NGQ_RMASK|READER_MASK)
1757b57a7965SJulian Elischer 
1758b57a7965SJulian Elischer /* tests to decide if we could get a read or write off the queue */
1759b57a7965SJulian Elischer #define CAN_GET_READ(flag)	((flag & NGQ_RMASK) == READ_PENDING)
1760b57a7965SJulian Elischer #define CAN_GET_WRITE(flag)	((flag & NGQ_WMASK) == WRITE_PENDING)
1761b57a7965SJulian Elischer 
1762b57a7965SJulian Elischer /* Is there a chance of getting ANY work off the queue? */
1763b57a7965SJulian Elischer #define CAN_GET_WORK(flag)	(CAN_GET_READ(flag) || CAN_GET_WRITE(flag))
1764b57a7965SJulian Elischer 
1765069154d5SJulian Elischer /*
1766069154d5SJulian Elischer  * Taking into account the current state of the queue and node, possibly take
1767069154d5SJulian Elischer  * the next entry off the queue and return it. Return NULL if there was
1768069154d5SJulian Elischer  * nothing we could return, either because there really was nothing there, or
1769069154d5SJulian Elischer  * because the node was in a state where it cannot yet process the next item
1770069154d5SJulian Elischer  * on the queue.
1771069154d5SJulian Elischer  *
1772069154d5SJulian Elischer  * This MUST MUST MUST be called with the mutex held.
1773069154d5SJulian Elischer  */
1774069154d5SJulian Elischer static __inline item_p
1775069154d5SJulian Elischer ng_dequeue(struct ng_queue *ngq)
1776069154d5SJulian Elischer {
1777069154d5SJulian Elischer 	item_p item;
1778069154d5SJulian Elischer 	u_int		add_arg;
1779b57a7965SJulian Elischer 
1780d58e678fSGleb Smirnoff 	mtx_assert(&ngq->q_mtx, MA_OWNED);
1781d58e678fSGleb Smirnoff 
1782b57a7965SJulian Elischer 	if (CAN_GET_READ(ngq->q_flags)) {
1783069154d5SJulian Elischer 		/*
1784b57a7965SJulian Elischer 		 * Head of queue is a reader and we have no write active.
1785b57a7965SJulian Elischer 		 * We don't care how many readers are already active.
1786b57a7965SJulian Elischer 		 * Adjust the flags for the item we are about to dequeue.
1787b57a7965SJulian Elischer 		 * Add the correct increment for the reader count as well.
1788069154d5SJulian Elischer 		 */
1789b57a7965SJulian Elischer 		add_arg = (READER_INCREMENT - READ_PENDING);
1790b57a7965SJulian Elischer 	} else if (CAN_GET_WRITE(ngq->q_flags)) {
1791069154d5SJulian Elischer 		/*
1792069154d5SJulian Elischer 		 * There is a pending write, no readers and no active writer.
1793069154d5SJulian Elischer 		 * This means we can go ahead with the pending writer. Note
1794069154d5SJulian Elischer 		 * the fact that we now have a writer, ready for when we take
1795069154d5SJulian Elischer 		 * it off the queue.
1796069154d5SJulian Elischer 		 *
1797069154d5SJulian Elischer 		 * We don't need to worry about a possible collision with the
1798069154d5SJulian Elischer 		 * fasttrack reader.
1799069154d5SJulian Elischer 		 *
1800069154d5SJulian Elischer 		 * The fasttrack thread may take a long time to discover that we
1801069154d5SJulian Elischer 		 * are running so we would have an inconsistent state in the
1802069154d5SJulian Elischer 		 * flags for a while. Since we ignore the reader count
1803069154d5SJulian Elischer 		 * entirely when the WRITER_ACTIVE flag is set, this should
1804069154d5SJulian Elischer 		 * not matter (in fact it is defined that way). If it tests
1805069154d5SJulian Elischer 		 * the flag before this operation, the WRITE_PENDING flag
1806069154d5SJulian Elischer 		 * will make it fail, and if it tests it later, the
1807b57a7965SJulian Elischer 		 * WRITER_ACTIVE flag will do the same. If it is SO slow that
1808069154d5SJulian Elischer 		 * we have actually completed the operation, and neither flag
1809069154d5SJulian Elischer 		 * is set (nor the READ_PENDING) by the time that it tests
1810069154d5SJulian Elischer 		 * the flags, then it is actually ok for it to continue. If
1811069154d5SJulian Elischer 		 * it completes and we've finished and the read pending is
1812069154d5SJulian Elischer 		 * set it still fails.
1813069154d5SJulian Elischer 		 *
1814069154d5SJulian Elischer 		 * So we can just ignore it,  as long as we can ensure that the
1815069154d5SJulian Elischer 		 * transition from WRITE_PENDING state to the WRITER_ACTIVE
1816069154d5SJulian Elischer 		 * state is atomic.
1817069154d5SJulian Elischer 		 *
1818069154d5SJulian Elischer 		 * After failing, first it will be held back by the mutex, then
1819069154d5SJulian Elischer 		 * when it can proceed, it will queue its request, then it
1820069154d5SJulian Elischer 		 * would arrive at this function. Usually it will have to
1821b57a7965SJulian Elischer 		 * leave empty handed because the ACTIVE WRITER bit will be
1822069154d5SJulian Elischer 		 * set.
1823b57a7965SJulian Elischer 		 *
1824b57a7965SJulian Elischer 		 * Adjust the flags for the item we are about to dequeue
1825b57a7965SJulian Elischer 		 * and for the new active writer.
1826069154d5SJulian Elischer 		 */
1827b57a7965SJulian Elischer 		add_arg = (WRITER_ACTIVE - WRITE_PENDING);
1828069154d5SJulian Elischer 		/*
1829069154d5SJulian Elischer 		 * We want to write "active writer, no readers " Now go make
1830069154d5SJulian Elischer 		 * it true. In fact there may be a number in the readers
1831069154d5SJulian Elischer 		 * count but we know it is not true and will be fixed soon.
1832069154d5SJulian Elischer 		 * We will fix the flags for the next pending entry in a
1833069154d5SJulian Elischer 		 * moment.
1834069154d5SJulian Elischer 		 */
1835069154d5SJulian Elischer 	} else {
1836069154d5SJulian Elischer 		/*
1837069154d5SJulian Elischer 		 * We can't dequeue anything.. return and say so. Probably we
1838069154d5SJulian Elischer 		 * have a write pending and the readers count is non zero. If
1839069154d5SJulian Elischer 		 * we got here because a reader hit us just at the wrong
1840069154d5SJulian Elischer 		 * moment with the fasttrack code, and put us in a strange
1841069154d5SJulian Elischer 		 * state, then it will be through in just a moment, (as soon
1842069154d5SJulian Elischer 		 * as we release the mutex) and keep things moving.
1843b57a7965SJulian Elischer 		 * Make sure we remove ourselves from the work queue.
1844069154d5SJulian Elischer 		 */
1845b57a7965SJulian Elischer 		ng_worklist_remove(ngq->q_node);
18464cf49a43SJulian Elischer 		return (0);
18474cf49a43SJulian Elischer 	}
18484cf49a43SJulian Elischer 
18494cf49a43SJulian Elischer 	/*
1850069154d5SJulian Elischer 	 * Now we dequeue the request (whatever it may be) and correct the
1851069154d5SJulian Elischer 	 * pending flags and the next and last pointers.
18524cf49a43SJulian Elischer 	 */
1853069154d5SJulian Elischer 	item = ngq->queue;
1854069154d5SJulian Elischer 	ngq->queue = item->el_next;
1855069154d5SJulian Elischer 	if (ngq->last == &(item->el_next)) {
18564cf49a43SJulian Elischer 		/*
1857069154d5SJulian Elischer 		 * that was the last entry in the queue so set the 'last
1858069154d5SJulian Elischer 		 * pointer up correctly and make sure the pending flags are
1859069154d5SJulian Elischer 		 * clear.
18604cf49a43SJulian Elischer 		 */
1861069154d5SJulian Elischer 		ngq->last = &(ngq->queue);
1862859a4d16SJulian Elischer 		/*
1863b57a7965SJulian Elischer 		 * Whatever flag was set will be cleared and
1864b57a7965SJulian Elischer 		 * the new acive field will be set by the add as well,
1865b57a7965SJulian Elischer 		 * so we don't need to change add_arg.
1866b57a7965SJulian Elischer 		 * But we know we don't need to be on the work list.
1867859a4d16SJulian Elischer 		 */
1868b57a7965SJulian Elischer 		atomic_add_long(&ngq->q_flags, add_arg);
1869b57a7965SJulian Elischer 		ng_worklist_remove(ngq->q_node);
1870859a4d16SJulian Elischer 	} else {
1871069154d5SJulian Elischer 		/*
1872b57a7965SJulian Elischer 		 * Since there is something on the queue, note what it is
1873b57a7965SJulian Elischer 		 * in the flags word.
1874069154d5SJulian Elischer 		 */
1875b57a7965SJulian Elischer 		if ((ngq->queue->el_flags & NGQF_RW) == NGQF_READER) {
1876069154d5SJulian Elischer 			add_arg += READ_PENDING;
1877069154d5SJulian Elischer 		} else {
1878069154d5SJulian Elischer 			add_arg += WRITE_PENDING;
1879859a4d16SJulian Elischer 		}
1880069154d5SJulian Elischer 		atomic_add_long(&ngq->q_flags, add_arg);
1881069154d5SJulian Elischer 		/*
1882b57a7965SJulian Elischer 		 * If we see more doable work, make sure we are
1883b57a7965SJulian Elischer 		 * on the work queue.
1884b57a7965SJulian Elischer 		 */
1885b57a7965SJulian Elischer 		if (CAN_GET_WORK(ngq->q_flags)) {
1886b57a7965SJulian Elischer 			ng_setisr(ngq->q_node);
1887b57a7965SJulian Elischer 		}
1888b57a7965SJulian Elischer 	}
1889b57a7965SJulian Elischer 	/*
1890069154d5SJulian Elischer 	 * We have successfully cleared the old pending flag, set the new one
1891069154d5SJulian Elischer 	 * if it is needed, and incremented the appropriate active field.
1892b57a7965SJulian Elischer 	 * (all in one atomic addition.. )
1893069154d5SJulian Elischer 	 */
1894069154d5SJulian Elischer 	return (item);
1895069154d5SJulian Elischer }
1896859a4d16SJulian Elischer 
1897859a4d16SJulian Elischer /*
1898069154d5SJulian Elischer  * Queue a packet to be picked up by someone else.
1899069154d5SJulian Elischer  * We really don't care who, but we can't or don't want to hang around
1900069154d5SJulian Elischer  * to process it ourselves. We are probably an interrupt routine..
1901069154d5SJulian Elischer  * 1 = writer, 0 = reader
1902859a4d16SJulian Elischer  */
1903069154d5SJulian Elischer #define NGQRW_R 0
1904069154d5SJulian Elischer #define NGQRW_W 1
1905069154d5SJulian Elischer static __inline void
1906069154d5SJulian Elischer ng_queue_rw(struct ng_queue * ngq, item_p  item, int rw)
1907069154d5SJulian Elischer {
1908d58e678fSGleb Smirnoff 	mtx_assert(&ngq->q_mtx, MA_OWNED);
1909d58e678fSGleb Smirnoff 
1910069154d5SJulian Elischer 	item->el_next = NULL;	/* maybe not needed */
1911069154d5SJulian Elischer 	*ngq->last = item;
1912069154d5SJulian Elischer 	/*
1913069154d5SJulian Elischer 	 * If it was the first item in the queue then we need to
1914069154d5SJulian Elischer 	 * set the last pointer and the type flags.
1915069154d5SJulian Elischer 	 */
1916069154d5SJulian Elischer 	if (ngq->last == &(ngq->queue)) {
1917069154d5SJulian Elischer 		/*
1918069154d5SJulian Elischer 		 * When called with constants for rw, the optimiser will
1919069154d5SJulian Elischer 		 * remove the unneeded branch below.
1920069154d5SJulian Elischer 		 */
1921069154d5SJulian Elischer 		if (rw == NGQRW_W) {
1922069154d5SJulian Elischer 			atomic_add_long(&ngq->q_flags, WRITE_PENDING);
1923069154d5SJulian Elischer 		} else {
1924069154d5SJulian Elischer 			atomic_add_long(&ngq->q_flags, READ_PENDING);
1925069154d5SJulian Elischer 		}
1926069154d5SJulian Elischer 	}
1927069154d5SJulian Elischer 	ngq->last = &(item->el_next);
1928069154d5SJulian Elischer }
1929069154d5SJulian Elischer 
1930069154d5SJulian Elischer 
1931069154d5SJulian Elischer /*
1932069154d5SJulian Elischer  * This function 'cheats' in that it first tries to 'grab' the use of the
1933069154d5SJulian Elischer  * node, without going through the mutex. We can do this becasue of the
1934069154d5SJulian Elischer  * semantics of the lock. The semantics include a clause that says that the
1935069154d5SJulian Elischer  * value of the readers count is invalid if the WRITER_ACTIVE flag is set. It
1936069154d5SJulian Elischer  * also says that the WRITER_ACTIVE flag cannot be set if the readers count
1937069154d5SJulian Elischer  * is not zero. Note that this talks about what is valid to SET the
1938069154d5SJulian Elischer  * WRITER_ACTIVE flag, because from the moment it is set, the value if the
1939069154d5SJulian Elischer  * reader count is immaterial, and not valid. The two 'pending' flags have a
1940069154d5SJulian Elischer  * similar effect, in that If they are orthogonal to the two active fields in
1941069154d5SJulian Elischer  * how they are set, but if either is set, the attempted 'grab' need to be
1942069154d5SJulian Elischer  * backed out because there is earlier work, and we maintain ordering in the
1943069154d5SJulian Elischer  * queue. The result of this is that the reader request can try obtain use of
1944069154d5SJulian Elischer  * the node with only a single atomic addition, and without any of the mutex
1945069154d5SJulian Elischer  * overhead. If this fails the operation degenerates to the same as for other
1946069154d5SJulian Elischer  * cases.
1947069154d5SJulian Elischer  *
1948069154d5SJulian Elischer  */
1949069154d5SJulian Elischer static __inline item_p
1950069154d5SJulian Elischer ng_acquire_read(struct ng_queue *ngq, item_p item)
1951069154d5SJulian Elischer {
1952069154d5SJulian Elischer 
1953069154d5SJulian Elischer 	/* ######### Hack alert ######### */
1954069154d5SJulian Elischer 	atomic_add_long(&ngq->q_flags, READER_INCREMENT);
1955b57a7965SJulian Elischer 	if ((ngq->q_flags & NGQ_RMASK) == 0) {
1956069154d5SJulian Elischer 		/* Successfully grabbed node */
1957069154d5SJulian Elischer 		return (item);
1958069154d5SJulian Elischer 	}
1959069154d5SJulian Elischer 	/* undo the damage if we didn't succeed */
1960069154d5SJulian Elischer 	atomic_subtract_long(&ngq->q_flags, READER_INCREMENT);
1961069154d5SJulian Elischer 
1962069154d5SJulian Elischer 	/* ######### End Hack alert ######### */
19639ed346baSBosko Milekic 	mtx_lock_spin((&ngq->q_mtx));
1964069154d5SJulian Elischer 	/*
1965069154d5SJulian Elischer 	 * Try again. Another processor (or interrupt for that matter) may
1966069154d5SJulian Elischer 	 * have removed the last queued item that was stopping us from
1967069154d5SJulian Elischer 	 * running, between the previous test, and the moment that we took
1968069154d5SJulian Elischer 	 * the mutex. (Or maybe a writer completed.)
1969069154d5SJulian Elischer 	 */
1970b57a7965SJulian Elischer 	if ((ngq->q_flags & NGQ_RMASK) == 0) {
1971069154d5SJulian Elischer 		atomic_add_long(&ngq->q_flags, READER_INCREMENT);
19729ed346baSBosko Milekic 		mtx_unlock_spin((&ngq->q_mtx));
1973069154d5SJulian Elischer 		return (item);
1974069154d5SJulian Elischer 	}
1975069154d5SJulian Elischer 
1976069154d5SJulian Elischer 	/*
1977069154d5SJulian Elischer 	 * and queue the request for later.
1978069154d5SJulian Elischer 	 */
1979069154d5SJulian Elischer 	ng_queue_rw(ngq, item, NGQRW_R);
1980f912c0f7SGleb Smirnoff 	if (CAN_GET_WORK(ngq->q_flags))
1981f912c0f7SGleb Smirnoff 		ng_setisr(ngq->q_node);
19829ed346baSBosko Milekic 	mtx_unlock_spin(&(ngq->q_mtx));
1983f912c0f7SGleb Smirnoff 
1984f912c0f7SGleb Smirnoff 	return (NULL);
1985069154d5SJulian Elischer }
1986069154d5SJulian Elischer 
1987069154d5SJulian Elischer static __inline item_p
1988069154d5SJulian Elischer ng_acquire_write(struct ng_queue *ngq, item_p item)
1989069154d5SJulian Elischer {
1990069154d5SJulian Elischer restart:
19919ed346baSBosko Milekic 	mtx_lock_spin(&(ngq->q_mtx));
1992069154d5SJulian Elischer 	/*
1993069154d5SJulian Elischer 	 * If there are no readers, no writer, and no pending packets, then
1994069154d5SJulian Elischer 	 * we can just go ahead. In all other situations we need to queue the
1995069154d5SJulian Elischer 	 * request
1996069154d5SJulian Elischer 	 */
1997b57a7965SJulian Elischer 	if ((ngq->q_flags & NGQ_WMASK) == 0) {
1998069154d5SJulian Elischer 		atomic_add_long(&ngq->q_flags, WRITER_ACTIVE);
19999ed346baSBosko Milekic 		mtx_unlock_spin((&ngq->q_mtx));
2000069154d5SJulian Elischer 		if (ngq->q_flags & READER_MASK) {
2001069154d5SJulian Elischer 			/* Collision with fast-track reader */
2002b57a7965SJulian Elischer 			atomic_subtract_long(&ngq->q_flags, WRITER_ACTIVE);
2003069154d5SJulian Elischer 			goto restart;
2004069154d5SJulian Elischer 		}
2005069154d5SJulian Elischer 		return (item);
2006069154d5SJulian Elischer 	}
2007069154d5SJulian Elischer 
2008069154d5SJulian Elischer 	/*
2009069154d5SJulian Elischer 	 * and queue the request for later.
2010069154d5SJulian Elischer 	 */
2011069154d5SJulian Elischer 	ng_queue_rw(ngq, item, NGQRW_W);
2012f912c0f7SGleb Smirnoff 	if (CAN_GET_WORK(ngq->q_flags))
2013f912c0f7SGleb Smirnoff 		ng_setisr(ngq->q_node);
20149ed346baSBosko Milekic 	mtx_unlock_spin(&(ngq->q_mtx));
2015f912c0f7SGleb Smirnoff 
2016f912c0f7SGleb Smirnoff 	return (NULL);
2017069154d5SJulian Elischer }
2018069154d5SJulian Elischer 
2019069154d5SJulian Elischer static __inline void
2020069154d5SJulian Elischer ng_leave_read(struct ng_queue *ngq)
2021069154d5SJulian Elischer {
2022069154d5SJulian Elischer 	atomic_subtract_long(&ngq->q_flags, READER_INCREMENT);
2023069154d5SJulian Elischer }
2024069154d5SJulian Elischer 
2025069154d5SJulian Elischer static __inline void
2026069154d5SJulian Elischer ng_leave_write(struct ng_queue *ngq)
2027069154d5SJulian Elischer {
2028069154d5SJulian Elischer 	atomic_subtract_long(&ngq->q_flags, WRITER_ACTIVE);
2029069154d5SJulian Elischer }
2030069154d5SJulian Elischer 
2031069154d5SJulian Elischer static void
2032069154d5SJulian Elischer ng_flush_input_queue(struct ng_queue * ngq)
2033069154d5SJulian Elischer {
2034069154d5SJulian Elischer 	item_p item;
2035069154d5SJulian Elischer 	u_int		add_arg;
20369ed346baSBosko Milekic 	mtx_lock_spin(&ngq->q_mtx);
2037069154d5SJulian Elischer 	for (;;) {
2038069154d5SJulian Elischer 		/* Now take a look at what's on the queue */
2039069154d5SJulian Elischer 		if (ngq->q_flags & READ_PENDING) {
2040069154d5SJulian Elischer 			add_arg = -READ_PENDING;
2041069154d5SJulian Elischer 		} else if (ngq->q_flags & WRITE_PENDING) {
2042069154d5SJulian Elischer 			add_arg = -WRITE_PENDING;
2043069154d5SJulian Elischer 		} else {
2044069154d5SJulian Elischer 			break;
2045069154d5SJulian Elischer 		}
2046069154d5SJulian Elischer 
2047069154d5SJulian Elischer 		item = ngq->queue;
2048069154d5SJulian Elischer 		ngq->queue = item->el_next;
2049069154d5SJulian Elischer 		if (ngq->last == &(item->el_next)) {
2050069154d5SJulian Elischer 			ngq->last = &(ngq->queue);
2051069154d5SJulian Elischer 		} else {
20526b795970SJulian Elischer 			if ((ngq->queue->el_flags & NGQF_RW) == NGQF_READER) {
2053069154d5SJulian Elischer 				add_arg += READ_PENDING;
2054069154d5SJulian Elischer 			} else {
2055069154d5SJulian Elischer 				add_arg += WRITE_PENDING;
2056069154d5SJulian Elischer 			}
2057069154d5SJulian Elischer 		}
2058069154d5SJulian Elischer 		atomic_add_long(&ngq->q_flags, add_arg);
2059069154d5SJulian Elischer 
20609ed346baSBosko Milekic 		mtx_unlock_spin(&ngq->q_mtx);
2061505fad52SJulian Elischer 		NG_FREE_ITEM(item);
2062505fad52SJulian Elischer 		mtx_lock_spin(&ngq->q_mtx);
2063069154d5SJulian Elischer 	}
2064b57a7965SJulian Elischer 	/*
2065b57a7965SJulian Elischer 	 * Take us off the work queue if we are there.
2066b57a7965SJulian Elischer 	 * We definatly have no work to be done.
2067b57a7965SJulian Elischer 	 */
2068b57a7965SJulian Elischer 	ng_worklist_remove(ngq->q_node);
20699ed346baSBosko Milekic 	mtx_unlock_spin(&ngq->q_mtx);
2070069154d5SJulian Elischer }
2071069154d5SJulian Elischer 
2072069154d5SJulian Elischer /***********************************************************************
2073069154d5SJulian Elischer * Externally visible method for sending or queueing messages or data.
2074069154d5SJulian Elischer ***********************************************************************/
2075069154d5SJulian Elischer 
2076069154d5SJulian Elischer /*
20771acb27c6SJulian Elischer  * The module code should have filled out the item correctly by this stage:
2078069154d5SJulian Elischer  * Common:
2079069154d5SJulian Elischer  *    reference to destination node.
2080069154d5SJulian Elischer  *    Reference to destination rcv hook if relevant.
2081069154d5SJulian Elischer  * Data:
2082069154d5SJulian Elischer  *    pointer to mbuf
2083069154d5SJulian Elischer  * Control_Message:
2084069154d5SJulian Elischer  *    pointer to msg.
2085069154d5SJulian Elischer  *    ID of original sender node. (return address)
20861acb27c6SJulian Elischer  * Function:
20871acb27c6SJulian Elischer  *    Function pointer
20881acb27c6SJulian Elischer  *    void * argument
20891acb27c6SJulian Elischer  *    integer argument
2090069154d5SJulian Elischer  *
2091069154d5SJulian Elischer  * The nodes have several routines and macros to help with this task:
2092069154d5SJulian Elischer  */
2093069154d5SJulian Elischer 
2094069154d5SJulian Elischer int
209542282202SGleb Smirnoff ng_snd_item(item_p item, int flags)
2096069154d5SJulian Elischer {
20971acb27c6SJulian Elischer 	hook_p hook = NGI_HOOK(item);
2098b57a7965SJulian Elischer 	node_p node = NGI_NODE(item);
209942282202SGleb Smirnoff 	int queue, rw;
2100069154d5SJulian Elischer 	int error = 0, ierror;
2101069154d5SJulian Elischer 	item_p	oitem;
2102b57a7965SJulian Elischer 	struct ng_queue * ngq = &node->nd_input_queue;
2103069154d5SJulian Elischer 
210430400f03SJulian Elischer #ifdef	NETGRAPH_DEBUG
2105069154d5SJulian Elischer         _ngi_check(item, __FILE__, __LINE__);
2106069154d5SJulian Elischer #endif
2107069154d5SJulian Elischer 
210842282202SGleb Smirnoff 	queue = (flags & NG_QUEUE) ? 1 : 0;
210942282202SGleb Smirnoff 
2110069154d5SJulian Elischer 	if (item == NULL) {
21116b795970SJulian Elischer 		TRAP_ERROR();
2112069154d5SJulian Elischer 		return (EINVAL);	/* failed to get queue element */
2113069154d5SJulian Elischer 	}
2114b57a7965SJulian Elischer 	if (node == NULL) {
2115069154d5SJulian Elischer 		NG_FREE_ITEM(item);
21166b795970SJulian Elischer 		TRAP_ERROR();
2117069154d5SJulian Elischer 		return (EINVAL);	/* No address */
2118069154d5SJulian Elischer 	}
21196b795970SJulian Elischer 	switch(item->el_flags & NGQF_TYPE) {
21206b795970SJulian Elischer 	case NGQF_DATA:
2121069154d5SJulian Elischer 		/*
2122069154d5SJulian Elischer 		 * DATA MESSAGE
2123069154d5SJulian Elischer 		 * Delivered to a node via a non-optional hook.
2124069154d5SJulian Elischer 		 * Both should be present in the item even though
2125069154d5SJulian Elischer 		 * the node is derivable from the hook.
2126069154d5SJulian Elischer 		 * References are held on both by the item.
2127069154d5SJulian Elischer 		 */
21281cf3fa79SJulian Elischer 
21291cf3fa79SJulian Elischer 		/* Protect nodes from sending NULL pointers
21301cf3fa79SJulian Elischer 		 * to each other
21311cf3fa79SJulian Elischer 		 */
213210204449SJulian Elischer 		if (NGI_M(item) == NULL)
21331cf3fa79SJulian Elischer 			return (EINVAL);
21341cf3fa79SJulian Elischer 
2135069154d5SJulian Elischer 		CHECK_DATA_MBUF(NGI_M(item));
2136069154d5SJulian Elischer 		if (hook == NULL) {
2137069154d5SJulian Elischer 			NG_FREE_ITEM(item);
21386b795970SJulian Elischer 			TRAP_ERROR();
2139069154d5SJulian Elischer 			return(EINVAL);
2140069154d5SJulian Elischer 		}
214130400f03SJulian Elischer 		if ((NG_HOOK_NOT_VALID(hook))
214230400f03SJulian Elischer 		|| (NG_NODE_NOT_VALID(NG_HOOK_NODE(hook)))) {
2143069154d5SJulian Elischer 			NG_FREE_ITEM(item);
2144069154d5SJulian Elischer 			return (ENOTCONN);
2145859a4d16SJulian Elischer 		}
214630400f03SJulian Elischer 		if ((hook->hk_flags & HK_QUEUE)) {
2147069154d5SJulian Elischer 			queue = 1;
21484cf49a43SJulian Elischer 		}
21496b795970SJulian Elischer 		break;
21506b795970SJulian Elischer 	case NGQF_MESG:
21514cf49a43SJulian Elischer 		/*
2152069154d5SJulian Elischer 		 * CONTROL MESSAGE
2153069154d5SJulian Elischer 		 * Delivered to a node.
2154069154d5SJulian Elischer 		 * Hook is optional.
2155069154d5SJulian Elischer 		 * References are held by the item on the node and
2156069154d5SJulian Elischer 		 * the hook if it is present.
21574cf49a43SJulian Elischer 		 */
215830400f03SJulian Elischer 		if (hook && (hook->hk_flags & HK_QUEUE)) {
2159069154d5SJulian Elischer 			queue = 1;
2160069154d5SJulian Elischer 		}
21616b795970SJulian Elischer 		break;
21626b795970SJulian Elischer 	case NGQF_FN:
21636b795970SJulian Elischer 		break;
21646b795970SJulian Elischer 	default:
21656b795970SJulian Elischer 		NG_FREE_ITEM(item);
21666b795970SJulian Elischer 		TRAP_ERROR();
21676b795970SJulian Elischer 		return (EINVAL);
2168069154d5SJulian Elischer 	}
21696f683eeeSGleb Smirnoff 	switch(item->el_flags & NGQF_RW) {
21706f683eeeSGleb Smirnoff 	case NGQF_READER:
21716f683eeeSGleb Smirnoff 		rw = NGQRW_R;
21726f683eeeSGleb Smirnoff 		break;
21736f683eeeSGleb Smirnoff 	case NGQF_WRITER:
21746f683eeeSGleb Smirnoff 		rw = NGQRW_W;
21756f683eeeSGleb Smirnoff 		break;
21766f683eeeSGleb Smirnoff 	default:
21776f683eeeSGleb Smirnoff 		panic("%s: invalid item flags %lx", __func__, item->el_flags);
21786f683eeeSGleb Smirnoff 	}
21796f683eeeSGleb Smirnoff 
2180069154d5SJulian Elischer 	/*
21816f683eeeSGleb Smirnoff 	 * If the node specifies single threading, force writer semantics.
21826f683eeeSGleb Smirnoff 	 * Similarly, the node may say one hook always produces writers.
21836f683eeeSGleb Smirnoff 	 * These are overrides. Modify the item itself, so that if it
21846f683eeeSGleb Smirnoff 	 * is queued now, it would be dequeued later with corrected
21856f683eeeSGleb Smirnoff 	 * read/write flags.
2186069154d5SJulian Elischer 	 */
2187be4252b3SJulian Elischer 	if ((node->nd_flags & NGF_FORCE_WRITER)
218830400f03SJulian Elischer 	    || (hook && (hook->hk_flags & HK_FORCE_WRITER))) {
2189069154d5SJulian Elischer 			rw = NGQRW_W;
21906b795970SJulian Elischer 			item->el_flags &= ~NGQF_READER;
2191069154d5SJulian Elischer 	}
21926f683eeeSGleb Smirnoff 
2193069154d5SJulian Elischer 	if (queue) {
2194069154d5SJulian Elischer 		/* Put it on the queue for that node*/
219530400f03SJulian Elischer #ifdef	NETGRAPH_DEBUG
2196069154d5SJulian Elischer         _ngi_check(item, __FILE__, __LINE__);
2197069154d5SJulian Elischer #endif
21989ed346baSBosko Milekic 		mtx_lock_spin(&(ngq->q_mtx));
2199069154d5SJulian Elischer 		ng_queue_rw(ngq, item, rw);
2200069154d5SJulian Elischer 		/*
2201069154d5SJulian Elischer 		 * If there are active elements then we can rely on
2202069154d5SJulian Elischer 		 * them. if not we should not rely on another packet
2203069154d5SJulian Elischer 		 * coming here by another path,
2204069154d5SJulian Elischer 		 * so it is best to put us in the netisr list.
2205b57a7965SJulian Elischer 		 * We can take the worklist lock with the node locked
2206b57a7965SJulian Elischer 		 * BUT NOT THE REVERSE!
2207069154d5SJulian Elischer 		 */
2208b57a7965SJulian Elischer 		if (CAN_GET_WORK(ngq->q_flags)) {
2209b57a7965SJulian Elischer 			ng_setisr(node);
2210069154d5SJulian Elischer 		}
22119ed346baSBosko Milekic 		mtx_unlock_spin(&(ngq->q_mtx));
22128afe16d5SGleb Smirnoff 
22138afe16d5SGleb Smirnoff 		if (flags & NG_PROGRESS)
22148afe16d5SGleb Smirnoff 			return (EINPROGRESS);
22158afe16d5SGleb Smirnoff 		else
2216069154d5SJulian Elischer 			return (0);
2217069154d5SJulian Elischer 	}
2218069154d5SJulian Elischer 	/*
2219069154d5SJulian Elischer 	 * Take a queue item and a node and see if we can apply the item to
2220069154d5SJulian Elischer 	 * the node. We may end up getting a different item to apply instead.
2221069154d5SJulian Elischer 	 * Will allow for a piggyback reply only in the case where
2222069154d5SJulian Elischer 	 * there is no queueing.
2223069154d5SJulian Elischer 	 */
2224069154d5SJulian Elischer 
2225069154d5SJulian Elischer 	oitem = item;
2226069154d5SJulian Elischer 	/*
2227069154d5SJulian Elischer 	 * We already decided how we will be queueud or treated.
2228069154d5SJulian Elischer 	 * Try get the appropriate operating permission.
2229069154d5SJulian Elischer 	 */
2230069154d5SJulian Elischer  	if (rw == NGQRW_R) {
2231069154d5SJulian Elischer 		item = ng_acquire_read(ngq, item);
2232069154d5SJulian Elischer 	} else {
2233069154d5SJulian Elischer 		item = ng_acquire_write(ngq, item);
22344cf49a43SJulian Elischer 	}
22354cf49a43SJulian Elischer 
22364cf49a43SJulian Elischer 	/*
2237069154d5SJulian Elischer 	 * May have come back with a different item.
2238069154d5SJulian Elischer 	 * or maybe none at all. The one we started with will
2239069154d5SJulian Elischer 	 * have been queued in thises cases.
2240069154d5SJulian Elischer 	 */
2241069154d5SJulian Elischer 	if (item == NULL) {
22428afe16d5SGleb Smirnoff 		if (flags & NG_PROGRESS)
22438afe16d5SGleb Smirnoff 			return (EINPROGRESS);
22448afe16d5SGleb Smirnoff 		else
2245069154d5SJulian Elischer 			return (0);
2246069154d5SJulian Elischer 	}
2247069154d5SJulian Elischer 
224830400f03SJulian Elischer #ifdef	NETGRAPH_DEBUG
2249069154d5SJulian Elischer         _ngi_check(item, __FILE__, __LINE__);
2250069154d5SJulian Elischer #endif
22515951069aSJulian Elischer 	/*
22525951069aSJulian Elischer 	 * Take over the reference frm the item.
22535951069aSJulian Elischer 	 * Hold it until the called function returns.
22545951069aSJulian Elischer 	 */
22555951069aSJulian Elischer 	NGI_GET_NODE(item, node); /* zaps stored node */
22565951069aSJulian Elischer 
22575951069aSJulian Elischer 	ierror = ng_apply_item(node, item); /* drops r/w lock when done */
2258069154d5SJulian Elischer 
2259069154d5SJulian Elischer 	/* only return an error if it was our initial item.. (compat hack) */
2260069154d5SJulian Elischer 	if (oitem == item) {
2261069154d5SJulian Elischer 		error = ierror;
2262069154d5SJulian Elischer 	}
2263069154d5SJulian Elischer 
2264069154d5SJulian Elischer 	/*
22655951069aSJulian Elischer 	 * If the node goes away when we remove the reference,
2266376958b4SBrian Somers 	 * whatever we just did caused it.. whatever we do, DO NOT
22675951069aSJulian Elischer 	 * access the node again!
22685951069aSJulian Elischer 	 */
22695951069aSJulian Elischer 	if (NG_NODE_UNREF(node) == 0) {
22705951069aSJulian Elischer 		return (error);
22715951069aSJulian Elischer 	}
22725951069aSJulian Elischer 
2273f912c0f7SGleb Smirnoff 	mtx_lock_spin(&(ngq->q_mtx));
2274f912c0f7SGleb Smirnoff 	if (CAN_GET_WORK(ngq->q_flags))
2275f912c0f7SGleb Smirnoff 		ng_setisr(ngq->q_node);
2276f912c0f7SGleb Smirnoff 	mtx_unlock_spin(&(ngq->q_mtx));
2277069154d5SJulian Elischer 
22781acb27c6SJulian Elischer 	return (error);
2279069154d5SJulian Elischer }
2280069154d5SJulian Elischer 
2281069154d5SJulian Elischer /*
2282069154d5SJulian Elischer  * We have an item that was possibly queued somewhere.
2283069154d5SJulian Elischer  * It should contain all the information needed
2284069154d5SJulian Elischer  * to run it on the appropriate node/hook.
22854cf49a43SJulian Elischer  */
22864cf49a43SJulian Elischer static int
22875951069aSJulian Elischer ng_apply_item(node_p node, item_p item)
2288069154d5SJulian Elischer {
2289069154d5SJulian Elischer 	hook_p  hook;
22906b795970SJulian Elischer 	int	was_reader = ((item->el_flags & NGQF_RW));
2291069154d5SJulian Elischer 	int	error = 0;
2292069154d5SJulian Elischer 	ng_rcvdata_t *rcvdata;
2293c4b5eea4SJulian Elischer 	ng_rcvmsg_t *rcvmsg;
22948afe16d5SGleb Smirnoff 	ng_apply_t *apply = NULL;
22958afe16d5SGleb Smirnoff 	void	*context = NULL;
2296069154d5SJulian Elischer 
22971acb27c6SJulian Elischer 	NGI_GET_HOOK(item, hook); /* clears stored hook */
229830400f03SJulian Elischer #ifdef	NETGRAPH_DEBUG
2299069154d5SJulian Elischer         _ngi_check(item, __FILE__, __LINE__);
2300069154d5SJulian Elischer #endif
23018afe16d5SGleb Smirnoff 
23028afe16d5SGleb Smirnoff 	/*
23038afe16d5SGleb Smirnoff 	 * If item has apply callback, store it. Clear callback
23048afe16d5SGleb Smirnoff 	 * immediately, two avoid another call in case if
23058afe16d5SGleb Smirnoff 	 * item would be reused by destination node.
23068afe16d5SGleb Smirnoff 	 */
23078afe16d5SGleb Smirnoff 	if (item->apply != NULL) {
23088afe16d5SGleb Smirnoff 		apply = item->apply;
23098afe16d5SGleb Smirnoff 		context = item->context;
23108afe16d5SGleb Smirnoff 		item->apply = NULL;
23118afe16d5SGleb Smirnoff 	}
23128afe16d5SGleb Smirnoff 
23136b795970SJulian Elischer 	switch (item->el_flags & NGQF_TYPE) {
2314069154d5SJulian Elischer 	case NGQF_DATA:
2315069154d5SJulian Elischer 		/*
2316069154d5SJulian Elischer 		 * Check things are still ok as when we were queued.
2317069154d5SJulian Elischer 		 */
2318069154d5SJulian Elischer 		if ((hook == NULL)
231930400f03SJulian Elischer 		|| NG_HOOK_NOT_VALID(hook)
2320c4b5eea4SJulian Elischer 		|| NG_NODE_NOT_VALID(node) ) {
2321069154d5SJulian Elischer 			error = EIO;
2322069154d5SJulian Elischer 			NG_FREE_ITEM(item);
2323c4b5eea4SJulian Elischer 			break;
2324069154d5SJulian Elischer 		}
2325c4b5eea4SJulian Elischer 		/*
2326c4b5eea4SJulian Elischer 		 * If no receive method, just silently drop it.
2327c4b5eea4SJulian Elischer 		 * Give preference to the hook over-ride method
2328c4b5eea4SJulian Elischer 		 */
2329c4b5eea4SJulian Elischer 		if ((!(rcvdata = hook->hk_rcvdata))
2330c4b5eea4SJulian Elischer 		&& (!(rcvdata = NG_HOOK_NODE(hook)->nd_type->rcvdata))) {
2331c4b5eea4SJulian Elischer 			error = 0;
2332c4b5eea4SJulian Elischer 			NG_FREE_ITEM(item);
2333c4b5eea4SJulian Elischer 			break;
2334c4b5eea4SJulian Elischer 		}
2335c4b5eea4SJulian Elischer 		error = (*rcvdata)(hook, item);
2336069154d5SJulian Elischer 		break;
2337069154d5SJulian Elischer 	case NGQF_MESG:
2338069154d5SJulian Elischer 		if (hook) {
233930400f03SJulian Elischer 			if (NG_HOOK_NOT_VALID(hook)) {
2340069154d5SJulian Elischer 				/*
23411acb27c6SJulian Elischer 				 * The hook has been zapped then we can't
23421acb27c6SJulian Elischer 				 * use it. Immediatly drop its reference.
2343069154d5SJulian Elischer 				 * The message may not need it.
2344069154d5SJulian Elischer 				 */
234530400f03SJulian Elischer 				NG_HOOK_UNREF(hook);
2346069154d5SJulian Elischer 				hook = NULL;
2347069154d5SJulian Elischer 			}
2348069154d5SJulian Elischer 		}
2349069154d5SJulian Elischer 		/*
2350069154d5SJulian Elischer 		 * Similarly, if the node is a zombie there is
2351069154d5SJulian Elischer 		 * nothing we can do with it, drop everything.
2352069154d5SJulian Elischer 		 */
235330400f03SJulian Elischer 		if (NG_NODE_NOT_VALID(node)) {
23546b795970SJulian Elischer 			TRAP_ERROR();
2355069154d5SJulian Elischer 			error = EINVAL;
2356069154d5SJulian Elischer 			NG_FREE_ITEM(item);
2357069154d5SJulian Elischer 		} else {
2358069154d5SJulian Elischer 			/*
2359069154d5SJulian Elischer 			 * Call the appropriate message handler for the object.
2360069154d5SJulian Elischer 			 * It is up to the message handler to free the message.
2361069154d5SJulian Elischer 			 * If it's a generic message, handle it generically,
2362069154d5SJulian Elischer 			 * otherwise call the type's message handler
2363069154d5SJulian Elischer 			 * (if it exists)
2364069154d5SJulian Elischer 			 * XXX (race). Remember that a queued message may
2365069154d5SJulian Elischer 			 * reference a node or hook that has just been
2366069154d5SJulian Elischer 			 * invalidated. It will exist as the queue code
2367069154d5SJulian Elischer 			 * is holding a reference, but..
2368069154d5SJulian Elischer 			 */
2369069154d5SJulian Elischer 
2370069154d5SJulian Elischer 			struct ng_mesg *msg = NGI_MSG(item);
2371069154d5SJulian Elischer 
2372c4b5eea4SJulian Elischer 			/*
2373c4b5eea4SJulian Elischer 			 * check if the generic handler owns it.
2374c4b5eea4SJulian Elischer 			 */
2375069154d5SJulian Elischer 			if ((msg->header.typecookie == NGM_GENERIC_COOKIE)
2376069154d5SJulian Elischer 			&& ((msg->header.flags & NGF_RESP) == 0)) {
2377069154d5SJulian Elischer 				error = ng_generic_msg(node, item, hook);
2378c4b5eea4SJulian Elischer 				break;
2379c4b5eea4SJulian Elischer 			}
2380c4b5eea4SJulian Elischer 			/*
2381c4b5eea4SJulian Elischer 			 * Now see if there is a handler (hook or node specific)
2382c4b5eea4SJulian Elischer 			 * in the target node. If none, silently discard.
2383c4b5eea4SJulian Elischer 			 */
2384c4b5eea4SJulian Elischer 			if (((!hook) || (!(rcvmsg = hook->hk_rcvmsg)))
2385c4b5eea4SJulian Elischer 			&& (!(rcvmsg = node->nd_type->rcvmsg))) {
23866b795970SJulian Elischer 				TRAP_ERROR();
2387c4b5eea4SJulian Elischer 				error = 0;
2388069154d5SJulian Elischer 				NG_FREE_ITEM(item);
2389c4b5eea4SJulian Elischer 				break;
2390069154d5SJulian Elischer 			}
2391c4b5eea4SJulian Elischer 			error = (*rcvmsg)(node, item, hook);
2392069154d5SJulian Elischer 		}
2393069154d5SJulian Elischer 		break;
23946b795970SJulian Elischer 	case NGQF_FN:
23956b795970SJulian Elischer 		/*
23966b795970SJulian Elischer 		 *  We have to implicitly trust the hook,
23976b795970SJulian Elischer 		 * as some of these are used for system purposes
23981acb27c6SJulian Elischer 		 * where the hook is invalid. In the case of
23991acb27c6SJulian Elischer 		 * the shutdown message we allow it to hit
24001acb27c6SJulian Elischer 		 * even if the node is invalid.
24016b795970SJulian Elischer 		 */
24021acb27c6SJulian Elischer 		if ((NG_NODE_NOT_VALID(node))
24031acb27c6SJulian Elischer 		&& (NGI_FN(item) != &ng_rmnode)) {
24046b795970SJulian Elischer 			TRAP_ERROR();
24056b795970SJulian Elischer 			error = EINVAL;
24068bb55179SGleb Smirnoff 			NG_FREE_ITEM(item);
24076b795970SJulian Elischer 			break;
24086b795970SJulian Elischer 		}
24096b795970SJulian Elischer 		(*NGI_FN(item))(node, hook, NGI_ARG1(item), NGI_ARG2(item));
24106b795970SJulian Elischer 		NG_FREE_ITEM(item);
24116b795970SJulian Elischer 		break;
24126b795970SJulian Elischer 
2413069154d5SJulian Elischer 	}
2414069154d5SJulian Elischer 	/*
2415069154d5SJulian Elischer 	 * We held references on some of the resources
2416069154d5SJulian Elischer 	 * that we took from the item. Now that we have
2417069154d5SJulian Elischer 	 * finished doing everything, drop those references.
2418069154d5SJulian Elischer 	 */
2419069154d5SJulian Elischer 	if (hook) {
242030400f03SJulian Elischer 		NG_HOOK_UNREF(hook);
2421069154d5SJulian Elischer 	}
2422069154d5SJulian Elischer 
2423069154d5SJulian Elischer 	if (was_reader) {
242430400f03SJulian Elischer 		ng_leave_read(&node->nd_input_queue);
2425069154d5SJulian Elischer 	} else {
242630400f03SJulian Elischer 		ng_leave_write(&node->nd_input_queue);
2427069154d5SJulian Elischer 	}
24288afe16d5SGleb Smirnoff 
24298afe16d5SGleb Smirnoff 	/* Apply callback. */
24308afe16d5SGleb Smirnoff 	if (apply != NULL)
24318afe16d5SGleb Smirnoff 		(*apply)(context, error);
24328afe16d5SGleb Smirnoff 
2433069154d5SJulian Elischer 	return (error);
2434069154d5SJulian Elischer }
2435069154d5SJulian Elischer 
2436069154d5SJulian Elischer /***********************************************************************
2437069154d5SJulian Elischer  * Implement the 'generic' control messages
2438069154d5SJulian Elischer  ***********************************************************************/
2439069154d5SJulian Elischer static int
2440069154d5SJulian Elischer ng_generic_msg(node_p here, item_p item, hook_p lasthook)
24414cf49a43SJulian Elischer {
24424cf49a43SJulian Elischer 	int error = 0;
2443069154d5SJulian Elischer 	struct ng_mesg *msg;
2444069154d5SJulian Elischer 	struct ng_mesg *resp = NULL;
24454cf49a43SJulian Elischer 
2446069154d5SJulian Elischer 	NGI_GET_MSG(item, msg);
24474cf49a43SJulian Elischer 	if (msg->header.typecookie != NGM_GENERIC_COOKIE) {
24486b795970SJulian Elischer 		TRAP_ERROR();
2449069154d5SJulian Elischer 		error = EINVAL;
2450069154d5SJulian Elischer 		goto out;
24514cf49a43SJulian Elischer 	}
24524cf49a43SJulian Elischer 	switch (msg->header.cmd) {
24534cf49a43SJulian Elischer 	case NGM_SHUTDOWN:
24541acb27c6SJulian Elischer 		ng_rmnode(here, NULL, NULL, 0);
24554cf49a43SJulian Elischer 		break;
24564cf49a43SJulian Elischer 	case NGM_MKPEER:
24574cf49a43SJulian Elischer 	    {
24584cf49a43SJulian Elischer 		struct ngm_mkpeer *const mkp = (struct ngm_mkpeer *) msg->data;
24594cf49a43SJulian Elischer 
24604cf49a43SJulian Elischer 		if (msg->header.arglen != sizeof(*mkp)) {
24616b795970SJulian Elischer 			TRAP_ERROR();
2462069154d5SJulian Elischer 			error = EINVAL;
2463069154d5SJulian Elischer 			break;
24644cf49a43SJulian Elischer 		}
24654cf49a43SJulian Elischer 		mkp->type[sizeof(mkp->type) - 1] = '\0';
24664cf49a43SJulian Elischer 		mkp->ourhook[sizeof(mkp->ourhook) - 1] = '\0';
24674cf49a43SJulian Elischer 		mkp->peerhook[sizeof(mkp->peerhook) - 1] = '\0';
24684cf49a43SJulian Elischer 		error = ng_mkpeer(here, mkp->ourhook, mkp->peerhook, mkp->type);
24694cf49a43SJulian Elischer 		break;
24704cf49a43SJulian Elischer 	    }
24714cf49a43SJulian Elischer 	case NGM_CONNECT:
24724cf49a43SJulian Elischer 	    {
24734cf49a43SJulian Elischer 		struct ngm_connect *const con =
24744cf49a43SJulian Elischer 			(struct ngm_connect *) msg->data;
24754cf49a43SJulian Elischer 		node_p node2;
24764cf49a43SJulian Elischer 
24774cf49a43SJulian Elischer 		if (msg->header.arglen != sizeof(*con)) {
24786b795970SJulian Elischer 			TRAP_ERROR();
2479069154d5SJulian Elischer 			error = EINVAL;
2480069154d5SJulian Elischer 			break;
24814cf49a43SJulian Elischer 		}
24824cf49a43SJulian Elischer 		con->path[sizeof(con->path) - 1] = '\0';
24834cf49a43SJulian Elischer 		con->ourhook[sizeof(con->ourhook) - 1] = '\0';
24844cf49a43SJulian Elischer 		con->peerhook[sizeof(con->peerhook) - 1] = '\0';
2485069154d5SJulian Elischer 		/* Don't forget we get a reference.. */
2486069154d5SJulian Elischer 		error = ng_path2noderef(here, con->path, &node2, NULL);
24874cf49a43SJulian Elischer 		if (error)
24884cf49a43SJulian Elischer 			break;
24894cf49a43SJulian Elischer 		error = ng_con_nodes(here, con->ourhook, node2, con->peerhook);
249030400f03SJulian Elischer 		NG_NODE_UNREF(node2);
24914cf49a43SJulian Elischer 		break;
24924cf49a43SJulian Elischer 	    }
24934cf49a43SJulian Elischer 	case NGM_NAME:
24944cf49a43SJulian Elischer 	    {
24954cf49a43SJulian Elischer 		struct ngm_name *const nam = (struct ngm_name *) msg->data;
24964cf49a43SJulian Elischer 
24974cf49a43SJulian Elischer 		if (msg->header.arglen != sizeof(*nam)) {
24986b795970SJulian Elischer 			TRAP_ERROR();
2499069154d5SJulian Elischer 			error = EINVAL;
2500069154d5SJulian Elischer 			break;
25014cf49a43SJulian Elischer 		}
25024cf49a43SJulian Elischer 		nam->name[sizeof(nam->name) - 1] = '\0';
25034cf49a43SJulian Elischer 		error = ng_name_node(here, nam->name);
25044cf49a43SJulian Elischer 		break;
25054cf49a43SJulian Elischer 	    }
25064cf49a43SJulian Elischer 	case NGM_RMHOOK:
25074cf49a43SJulian Elischer 	    {
25084cf49a43SJulian Elischer 		struct ngm_rmhook *const rmh = (struct ngm_rmhook *) msg->data;
25094cf49a43SJulian Elischer 		hook_p hook;
25104cf49a43SJulian Elischer 
25114cf49a43SJulian Elischer 		if (msg->header.arglen != sizeof(*rmh)) {
25126b795970SJulian Elischer 			TRAP_ERROR();
2513069154d5SJulian Elischer 			error = EINVAL;
2514069154d5SJulian Elischer 			break;
25154cf49a43SJulian Elischer 		}
25164cf49a43SJulian Elischer 		rmh->ourhook[sizeof(rmh->ourhook) - 1] = '\0';
2517899e9c4eSArchie Cobbs 		if ((hook = ng_findhook(here, rmh->ourhook)) != NULL)
25184cf49a43SJulian Elischer 			ng_destroy_hook(hook);
25194cf49a43SJulian Elischer 		break;
25204cf49a43SJulian Elischer 	    }
25214cf49a43SJulian Elischer 	case NGM_NODEINFO:
25224cf49a43SJulian Elischer 	    {
25234cf49a43SJulian Elischer 		struct nodeinfo *ni;
25244cf49a43SJulian Elischer 
2525069154d5SJulian Elischer 		NG_MKRESPONSE(resp, msg, sizeof(*ni), M_NOWAIT);
25264cf49a43SJulian Elischer 		if (resp == NULL) {
25274cf49a43SJulian Elischer 			error = ENOMEM;
25284cf49a43SJulian Elischer 			break;
25294cf49a43SJulian Elischer 		}
25304cf49a43SJulian Elischer 
25314cf49a43SJulian Elischer 		/* Fill in node info */
2532069154d5SJulian Elischer 		ni = (struct nodeinfo *) resp->data;
253330400f03SJulian Elischer 		if (NG_NODE_HAS_NAME(here))
253487e2c66aSHartmut Brandt 			strcpy(ni->name, NG_NODE_NAME(here));
253587e2c66aSHartmut Brandt 		strcpy(ni->type, here->nd_type->name);
2536dc90cad9SJulian Elischer 		ni->id = ng_node2ID(here);
253730400f03SJulian Elischer 		ni->hooks = here->nd_numhooks;
25384cf49a43SJulian Elischer 		break;
25394cf49a43SJulian Elischer 	    }
25404cf49a43SJulian Elischer 	case NGM_LISTHOOKS:
25414cf49a43SJulian Elischer 	    {
254230400f03SJulian Elischer 		const int nhooks = here->nd_numhooks;
25434cf49a43SJulian Elischer 		struct hooklist *hl;
25444cf49a43SJulian Elischer 		struct nodeinfo *ni;
25454cf49a43SJulian Elischer 		hook_p hook;
25464cf49a43SJulian Elischer 
25474cf49a43SJulian Elischer 		/* Get response struct */
2548069154d5SJulian Elischer 		NG_MKRESPONSE(resp, msg, sizeof(*hl)
25494cf49a43SJulian Elischer 		    + (nhooks * sizeof(struct linkinfo)), M_NOWAIT);
2550069154d5SJulian Elischer 		if (resp == NULL) {
25514cf49a43SJulian Elischer 			error = ENOMEM;
25524cf49a43SJulian Elischer 			break;
25534cf49a43SJulian Elischer 		}
2554069154d5SJulian Elischer 		hl = (struct hooklist *) resp->data;
25554cf49a43SJulian Elischer 		ni = &hl->nodeinfo;
25564cf49a43SJulian Elischer 
25574cf49a43SJulian Elischer 		/* Fill in node info */
255830400f03SJulian Elischer 		if (NG_NODE_HAS_NAME(here))
255987e2c66aSHartmut Brandt 			strcpy(ni->name, NG_NODE_NAME(here));
256087e2c66aSHartmut Brandt 		strcpy(ni->type, here->nd_type->name);
2561dc90cad9SJulian Elischer 		ni->id = ng_node2ID(here);
25624cf49a43SJulian Elischer 
25634cf49a43SJulian Elischer 		/* Cycle through the linked list of hooks */
25644cf49a43SJulian Elischer 		ni->hooks = 0;
256530400f03SJulian Elischer 		LIST_FOREACH(hook, &here->nd_hooks, hk_hooks) {
25664cf49a43SJulian Elischer 			struct linkinfo *const link = &hl->link[ni->hooks];
25674cf49a43SJulian Elischer 
25684cf49a43SJulian Elischer 			if (ni->hooks >= nhooks) {
25694cf49a43SJulian Elischer 				log(LOG_ERR, "%s: number of %s changed\n",
25706e551fb6SDavid E. O'Brien 				    __func__, "hooks");
25714cf49a43SJulian Elischer 				break;
25724cf49a43SJulian Elischer 			}
257330400f03SJulian Elischer 			if (NG_HOOK_NOT_VALID(hook))
25744cf49a43SJulian Elischer 				continue;
257587e2c66aSHartmut Brandt 			strcpy(link->ourhook, NG_HOOK_NAME(hook));
257687e2c66aSHartmut Brandt 			strcpy(link->peerhook, NG_PEER_HOOK_NAME(hook));
257730400f03SJulian Elischer 			if (NG_PEER_NODE_NAME(hook)[0] != '\0')
257887e2c66aSHartmut Brandt 				strcpy(link->nodeinfo.name,
257987e2c66aSHartmut Brandt 				    NG_PEER_NODE_NAME(hook));
258087e2c66aSHartmut Brandt 			strcpy(link->nodeinfo.type,
258187e2c66aSHartmut Brandt 			   NG_PEER_NODE(hook)->nd_type->name);
258230400f03SJulian Elischer 			link->nodeinfo.id = ng_node2ID(NG_PEER_NODE(hook));
258330400f03SJulian Elischer 			link->nodeinfo.hooks = NG_PEER_NODE(hook)->nd_numhooks;
25844cf49a43SJulian Elischer 			ni->hooks++;
25854cf49a43SJulian Elischer 		}
25864cf49a43SJulian Elischer 		break;
25874cf49a43SJulian Elischer 	    }
25884cf49a43SJulian Elischer 
25894cf49a43SJulian Elischer 	case NGM_LISTNAMES:
25904cf49a43SJulian Elischer 	case NGM_LISTNODES:
25914cf49a43SJulian Elischer 	    {
25924cf49a43SJulian Elischer 		const int unnamed = (msg->header.cmd == NGM_LISTNODES);
25934cf49a43SJulian Elischer 		struct namelist *nl;
25944cf49a43SJulian Elischer 		node_p node;
25954cf49a43SJulian Elischer 		int num = 0;
25964cf49a43SJulian Elischer 
25979ed346baSBosko Milekic 		mtx_lock(&ng_nodelist_mtx);
25984cf49a43SJulian Elischer 		/* Count number of nodes */
259930400f03SJulian Elischer 		LIST_FOREACH(node, &ng_nodelist, nd_nodes) {
260070de87f2SJulian Elischer 			if (NG_NODE_IS_VALID(node)
260170de87f2SJulian Elischer 			&& (unnamed || NG_NODE_HAS_NAME(node))) {
26024cf49a43SJulian Elischer 				num++;
26034cf49a43SJulian Elischer 			}
260470de87f2SJulian Elischer 		}
26059ed346baSBosko Milekic 		mtx_unlock(&ng_nodelist_mtx);
26064cf49a43SJulian Elischer 
26074cf49a43SJulian Elischer 		/* Get response struct */
2608069154d5SJulian Elischer 		NG_MKRESPONSE(resp, msg, sizeof(*nl)
26094cf49a43SJulian Elischer 		    + (num * sizeof(struct nodeinfo)), M_NOWAIT);
2610069154d5SJulian Elischer 		if (resp == NULL) {
26114cf49a43SJulian Elischer 			error = ENOMEM;
26124cf49a43SJulian Elischer 			break;
26134cf49a43SJulian Elischer 		}
2614069154d5SJulian Elischer 		nl = (struct namelist *) resp->data;
26154cf49a43SJulian Elischer 
26164cf49a43SJulian Elischer 		/* Cycle through the linked list of nodes */
26174cf49a43SJulian Elischer 		nl->numnames = 0;
26189ed346baSBosko Milekic 		mtx_lock(&ng_nodelist_mtx);
261930400f03SJulian Elischer 		LIST_FOREACH(node, &ng_nodelist, nd_nodes) {
26204cf49a43SJulian Elischer 			struct nodeinfo *const np = &nl->nodeinfo[nl->numnames];
26214cf49a43SJulian Elischer 
26224cf49a43SJulian Elischer 			if (nl->numnames >= num) {
26234cf49a43SJulian Elischer 				log(LOG_ERR, "%s: number of %s changed\n",
26246e551fb6SDavid E. O'Brien 				    __func__, "nodes");
26254cf49a43SJulian Elischer 				break;
26264cf49a43SJulian Elischer 			}
262730400f03SJulian Elischer 			if (NG_NODE_NOT_VALID(node))
26284cf49a43SJulian Elischer 				continue;
262930400f03SJulian Elischer 			if (!unnamed && (! NG_NODE_HAS_NAME(node)))
26304cf49a43SJulian Elischer 				continue;
263130400f03SJulian Elischer 			if (NG_NODE_HAS_NAME(node))
263287e2c66aSHartmut Brandt 				strcpy(np->name, NG_NODE_NAME(node));
263387e2c66aSHartmut Brandt 			strcpy(np->type, node->nd_type->name);
2634dc90cad9SJulian Elischer 			np->id = ng_node2ID(node);
263530400f03SJulian Elischer 			np->hooks = node->nd_numhooks;
26364cf49a43SJulian Elischer 			nl->numnames++;
26374cf49a43SJulian Elischer 		}
26389ed346baSBosko Milekic 		mtx_unlock(&ng_nodelist_mtx);
26394cf49a43SJulian Elischer 		break;
26404cf49a43SJulian Elischer 	    }
26414cf49a43SJulian Elischer 
26424cf49a43SJulian Elischer 	case NGM_LISTTYPES:
26434cf49a43SJulian Elischer 	    {
26444cf49a43SJulian Elischer 		struct typelist *tl;
26454cf49a43SJulian Elischer 		struct ng_type *type;
26464cf49a43SJulian Elischer 		int num = 0;
26474cf49a43SJulian Elischer 
26489ed346baSBosko Milekic 		mtx_lock(&ng_typelist_mtx);
26494cf49a43SJulian Elischer 		/* Count number of types */
265070de87f2SJulian Elischer 		LIST_FOREACH(type, &ng_typelist, types) {
26514cf49a43SJulian Elischer 			num++;
265270de87f2SJulian Elischer 		}
26539ed346baSBosko Milekic 		mtx_unlock(&ng_typelist_mtx);
26544cf49a43SJulian Elischer 
26554cf49a43SJulian Elischer 		/* Get response struct */
2656069154d5SJulian Elischer 		NG_MKRESPONSE(resp, msg, sizeof(*tl)
26574cf49a43SJulian Elischer 		    + (num * sizeof(struct typeinfo)), M_NOWAIT);
2658069154d5SJulian Elischer 		if (resp == NULL) {
26594cf49a43SJulian Elischer 			error = ENOMEM;
26604cf49a43SJulian Elischer 			break;
26614cf49a43SJulian Elischer 		}
2662069154d5SJulian Elischer 		tl = (struct typelist *) resp->data;
26634cf49a43SJulian Elischer 
26644cf49a43SJulian Elischer 		/* Cycle through the linked list of types */
26654cf49a43SJulian Elischer 		tl->numtypes = 0;
26669ed346baSBosko Milekic 		mtx_lock(&ng_typelist_mtx);
2667069154d5SJulian Elischer 		LIST_FOREACH(type, &ng_typelist, types) {
26684cf49a43SJulian Elischer 			struct typeinfo *const tp = &tl->typeinfo[tl->numtypes];
26694cf49a43SJulian Elischer 
26704cf49a43SJulian Elischer 			if (tl->numtypes >= num) {
26714cf49a43SJulian Elischer 				log(LOG_ERR, "%s: number of %s changed\n",
26726e551fb6SDavid E. O'Brien 				    __func__, "types");
26734cf49a43SJulian Elischer 				break;
26744cf49a43SJulian Elischer 			}
267587e2c66aSHartmut Brandt 			strcpy(tp->type_name, type->name);
2676c73b94a2SJulian Elischer 			tp->numnodes = type->refs - 1; /* don't count list */
26774cf49a43SJulian Elischer 			tl->numtypes++;
26784cf49a43SJulian Elischer 		}
26799ed346baSBosko Milekic 		mtx_unlock(&ng_typelist_mtx);
26804cf49a43SJulian Elischer 		break;
26814cf49a43SJulian Elischer 	    }
26824cf49a43SJulian Elischer 
2683f8307e12SArchie Cobbs 	case NGM_BINARY2ASCII:
2684f8307e12SArchie Cobbs 	    {
26857133ac27SArchie Cobbs 		int bufSize = 20 * 1024;	/* XXX hard coded constant */
2686f8307e12SArchie Cobbs 		const struct ng_parse_type *argstype;
2687f8307e12SArchie Cobbs 		const struct ng_cmdlist *c;
2688069154d5SJulian Elischer 		struct ng_mesg *binary, *ascii;
2689f8307e12SArchie Cobbs 
2690f8307e12SArchie Cobbs 		/* Data area must contain a valid netgraph message */
2691f8307e12SArchie Cobbs 		binary = (struct ng_mesg *)msg->data;
2692f8307e12SArchie Cobbs 		if (msg->header.arglen < sizeof(struct ng_mesg)
269370de87f2SJulian Elischer 		    || (msg->header.arglen - sizeof(struct ng_mesg)
269470de87f2SJulian Elischer 		      < binary->header.arglen)) {
26956b795970SJulian Elischer 			TRAP_ERROR();
2696f8307e12SArchie Cobbs 			error = EINVAL;
2697f8307e12SArchie Cobbs 			break;
2698f8307e12SArchie Cobbs 		}
2699f8307e12SArchie Cobbs 
2700f8307e12SArchie Cobbs 		/* Get a response message with lots of room */
2701069154d5SJulian Elischer 		NG_MKRESPONSE(resp, msg, sizeof(*ascii) + bufSize, M_NOWAIT);
2702069154d5SJulian Elischer 		if (resp == NULL) {
2703f8307e12SArchie Cobbs 			error = ENOMEM;
2704f8307e12SArchie Cobbs 			break;
2705f8307e12SArchie Cobbs 		}
2706069154d5SJulian Elischer 		ascii = (struct ng_mesg *)resp->data;
2707f8307e12SArchie Cobbs 
2708f8307e12SArchie Cobbs 		/* Copy binary message header to response message payload */
2709f8307e12SArchie Cobbs 		bcopy(binary, ascii, sizeof(*binary));
2710f8307e12SArchie Cobbs 
2711f8307e12SArchie Cobbs 		/* Find command by matching typecookie and command number */
271230400f03SJulian Elischer 		for (c = here->nd_type->cmdlist;
2713f8307e12SArchie Cobbs 		    c != NULL && c->name != NULL; c++) {
2714f8307e12SArchie Cobbs 			if (binary->header.typecookie == c->cookie
2715f8307e12SArchie Cobbs 			    && binary->header.cmd == c->cmd)
2716f8307e12SArchie Cobbs 				break;
2717f8307e12SArchie Cobbs 		}
2718f8307e12SArchie Cobbs 		if (c == NULL || c->name == NULL) {
2719f8307e12SArchie Cobbs 			for (c = ng_generic_cmds; c->name != NULL; c++) {
2720f8307e12SArchie Cobbs 				if (binary->header.typecookie == c->cookie
2721f8307e12SArchie Cobbs 				    && binary->header.cmd == c->cmd)
2722f8307e12SArchie Cobbs 					break;
2723f8307e12SArchie Cobbs 			}
2724f8307e12SArchie Cobbs 			if (c->name == NULL) {
2725069154d5SJulian Elischer 				NG_FREE_MSG(resp);
2726f8307e12SArchie Cobbs 				error = ENOSYS;
2727f8307e12SArchie Cobbs 				break;
2728f8307e12SArchie Cobbs 			}
2729f8307e12SArchie Cobbs 		}
2730f8307e12SArchie Cobbs 
2731f8307e12SArchie Cobbs 		/* Convert command name to ASCII */
2732f8307e12SArchie Cobbs 		snprintf(ascii->header.cmdstr, sizeof(ascii->header.cmdstr),
2733f8307e12SArchie Cobbs 		    "%s", c->name);
2734f8307e12SArchie Cobbs 
2735f8307e12SArchie Cobbs 		/* Convert command arguments to ASCII */
2736f8307e12SArchie Cobbs 		argstype = (binary->header.flags & NGF_RESP) ?
2737f8307e12SArchie Cobbs 		    c->respType : c->mesgType;
273870de87f2SJulian Elischer 		if (argstype == NULL) {
2739f8307e12SArchie Cobbs 			*ascii->data = '\0';
274070de87f2SJulian Elischer 		} else {
2741f8307e12SArchie Cobbs 			if ((error = ng_unparse(argstype,
2742f8307e12SArchie Cobbs 			    (u_char *)binary->data,
2743f8307e12SArchie Cobbs 			    ascii->data, bufSize)) != 0) {
2744069154d5SJulian Elischer 				NG_FREE_MSG(resp);
2745f8307e12SArchie Cobbs 				break;
2746f8307e12SArchie Cobbs 			}
2747f8307e12SArchie Cobbs 		}
2748f8307e12SArchie Cobbs 
2749f8307e12SArchie Cobbs 		/* Return the result as struct ng_mesg plus ASCII string */
2750f8307e12SArchie Cobbs 		bufSize = strlen(ascii->data) + 1;
2751f8307e12SArchie Cobbs 		ascii->header.arglen = bufSize;
2752069154d5SJulian Elischer 		resp->header.arglen = sizeof(*ascii) + bufSize;
2753f8307e12SArchie Cobbs 		break;
2754f8307e12SArchie Cobbs 	    }
2755f8307e12SArchie Cobbs 
2756f8307e12SArchie Cobbs 	case NGM_ASCII2BINARY:
2757f8307e12SArchie Cobbs 	    {
2758f8307e12SArchie Cobbs 		int bufSize = 2000;	/* XXX hard coded constant */
2759f8307e12SArchie Cobbs 		const struct ng_cmdlist *c;
2760f8307e12SArchie Cobbs 		const struct ng_parse_type *argstype;
2761069154d5SJulian Elischer 		struct ng_mesg *ascii, *binary;
276252ec4a03SArchie Cobbs 		int off = 0;
2763f8307e12SArchie Cobbs 
2764f8307e12SArchie Cobbs 		/* Data area must contain at least a struct ng_mesg + '\0' */
2765f8307e12SArchie Cobbs 		ascii = (struct ng_mesg *)msg->data;
276670de87f2SJulian Elischer 		if ((msg->header.arglen < sizeof(*ascii) + 1)
276770de87f2SJulian Elischer 		    || (ascii->header.arglen < 1)
276870de87f2SJulian Elischer 		    || (msg->header.arglen
276970de87f2SJulian Elischer 		      < sizeof(*ascii) + ascii->header.arglen)) {
27706b795970SJulian Elischer 			TRAP_ERROR();
2771f8307e12SArchie Cobbs 			error = EINVAL;
2772f8307e12SArchie Cobbs 			break;
2773f8307e12SArchie Cobbs 		}
2774f8307e12SArchie Cobbs 		ascii->data[ascii->header.arglen - 1] = '\0';
2775f8307e12SArchie Cobbs 
2776f8307e12SArchie Cobbs 		/* Get a response message with lots of room */
2777069154d5SJulian Elischer 		NG_MKRESPONSE(resp, msg, sizeof(*binary) + bufSize, M_NOWAIT);
2778069154d5SJulian Elischer 		if (resp == NULL) {
2779f8307e12SArchie Cobbs 			error = ENOMEM;
2780f8307e12SArchie Cobbs 			break;
2781f8307e12SArchie Cobbs 		}
2782069154d5SJulian Elischer 		binary = (struct ng_mesg *)resp->data;
2783f8307e12SArchie Cobbs 
2784f8307e12SArchie Cobbs 		/* Copy ASCII message header to response message payload */
2785f8307e12SArchie Cobbs 		bcopy(ascii, binary, sizeof(*ascii));
2786f8307e12SArchie Cobbs 
2787f8307e12SArchie Cobbs 		/* Find command by matching ASCII command string */
278830400f03SJulian Elischer 		for (c = here->nd_type->cmdlist;
2789f8307e12SArchie Cobbs 		    c != NULL && c->name != NULL; c++) {
2790f8307e12SArchie Cobbs 			if (strcmp(ascii->header.cmdstr, c->name) == 0)
2791f8307e12SArchie Cobbs 				break;
2792f8307e12SArchie Cobbs 		}
2793f8307e12SArchie Cobbs 		if (c == NULL || c->name == NULL) {
2794f8307e12SArchie Cobbs 			for (c = ng_generic_cmds; c->name != NULL; c++) {
2795f8307e12SArchie Cobbs 				if (strcmp(ascii->header.cmdstr, c->name) == 0)
2796f8307e12SArchie Cobbs 					break;
2797f8307e12SArchie Cobbs 			}
2798f8307e12SArchie Cobbs 			if (c->name == NULL) {
2799069154d5SJulian Elischer 				NG_FREE_MSG(resp);
2800f8307e12SArchie Cobbs 				error = ENOSYS;
2801f8307e12SArchie Cobbs 				break;
2802f8307e12SArchie Cobbs 			}
2803f8307e12SArchie Cobbs 		}
2804f8307e12SArchie Cobbs 
2805f8307e12SArchie Cobbs 		/* Convert command name to binary */
2806f8307e12SArchie Cobbs 		binary->header.cmd = c->cmd;
2807f8307e12SArchie Cobbs 		binary->header.typecookie = c->cookie;
2808f8307e12SArchie Cobbs 
2809f8307e12SArchie Cobbs 		/* Convert command arguments to binary */
2810f8307e12SArchie Cobbs 		argstype = (binary->header.flags & NGF_RESP) ?
2811f8307e12SArchie Cobbs 		    c->respType : c->mesgType;
281270de87f2SJulian Elischer 		if (argstype == NULL) {
2813f8307e12SArchie Cobbs 			bufSize = 0;
281470de87f2SJulian Elischer 		} else {
2815f8307e12SArchie Cobbs 			if ((error = ng_parse(argstype, ascii->data,
2816f8307e12SArchie Cobbs 			    &off, (u_char *)binary->data, &bufSize)) != 0) {
2817069154d5SJulian Elischer 				NG_FREE_MSG(resp);
2818f8307e12SArchie Cobbs 				break;
2819f8307e12SArchie Cobbs 			}
2820f8307e12SArchie Cobbs 		}
2821f8307e12SArchie Cobbs 
2822f8307e12SArchie Cobbs 		/* Return the result */
2823f8307e12SArchie Cobbs 		binary->header.arglen = bufSize;
2824069154d5SJulian Elischer 		resp->header.arglen = sizeof(*binary) + bufSize;
2825f8307e12SArchie Cobbs 		break;
2826f8307e12SArchie Cobbs 	    }
2827f8307e12SArchie Cobbs 
28287095e097SPoul-Henning Kamp 	case NGM_TEXT_CONFIG:
28294cf49a43SJulian Elischer 	case NGM_TEXT_STATUS:
28304cf49a43SJulian Elischer 		/*
28314cf49a43SJulian Elischer 		 * This one is tricky as it passes the command down to the
28324cf49a43SJulian Elischer 		 * actual node, even though it is a generic type command.
2833069154d5SJulian Elischer 		 * This means we must assume that the item/msg is already freed
28344cf49a43SJulian Elischer 		 * when control passes back to us.
28354cf49a43SJulian Elischer 		 */
283630400f03SJulian Elischer 		if (here->nd_type->rcvmsg != NULL) {
2837069154d5SJulian Elischer 			NGI_MSG(item) = msg; /* put it back as we found it */
283830400f03SJulian Elischer 			return((*here->nd_type->rcvmsg)(here, item, lasthook));
28394cf49a43SJulian Elischer 		}
28404cf49a43SJulian Elischer 		/* Fall through if rcvmsg not supported */
28414cf49a43SJulian Elischer 	default:
28426b795970SJulian Elischer 		TRAP_ERROR();
28434cf49a43SJulian Elischer 		error = EINVAL;
28444cf49a43SJulian Elischer 	}
2845069154d5SJulian Elischer 	/*
2846069154d5SJulian Elischer 	 * Sometimes a generic message may be statically allocated
2847069154d5SJulian Elischer 	 * to avoid problems with allocating when in tight memeory situations.
2848069154d5SJulian Elischer 	 * Don't free it if it is so.
2849069154d5SJulian Elischer 	 * I break them appart here, because erros may cause a free if the item
2850069154d5SJulian Elischer 	 * in which case we'd be doing it twice.
2851069154d5SJulian Elischer 	 * they are kept together above, to simplify freeing.
2852069154d5SJulian Elischer 	 */
2853069154d5SJulian Elischer out:
2854069154d5SJulian Elischer 	NG_RESPOND_MSG(error, here, item, resp);
28551acb27c6SJulian Elischer 	if (msg)
2856069154d5SJulian Elischer 		NG_FREE_MSG(msg);
28574cf49a43SJulian Elischer 	return (error);
28584cf49a43SJulian Elischer }
28594cf49a43SJulian Elischer 
28604cf49a43SJulian Elischer /************************************************************************
28618253c060SGleb Smirnoff 			Queue element get/free routines
28628253c060SGleb Smirnoff ************************************************************************/
28638253c060SGleb Smirnoff 
28648253c060SGleb Smirnoff uma_zone_t			ng_qzone;
28658253c060SGleb Smirnoff static int			maxalloc = 512;	/* limit the damage of a leak */
28668253c060SGleb Smirnoff 
28678253c060SGleb Smirnoff TUNABLE_INT("net.graph.maxalloc", &maxalloc);
28688253c060SGleb Smirnoff SYSCTL_INT(_net_graph, OID_AUTO, maxalloc, CTLFLAG_RDTUN, &maxalloc,
28698253c060SGleb Smirnoff     0, "Maximum number of queue items to allocate");
28708253c060SGleb Smirnoff 
28718253c060SGleb Smirnoff #ifdef	NETGRAPH_DEBUG
28728253c060SGleb Smirnoff static TAILQ_HEAD(, ng_item) ng_itemlist = TAILQ_HEAD_INITIALIZER(ng_itemlist);
28738253c060SGleb Smirnoff static int			allocated;	/* number of items malloc'd */
28748253c060SGleb Smirnoff #endif
28758253c060SGleb Smirnoff 
28768253c060SGleb Smirnoff /*
28778253c060SGleb Smirnoff  * Get a queue entry.
28788253c060SGleb Smirnoff  * This is usually called when a packet first enters netgraph.
28798253c060SGleb Smirnoff  * By definition, this is usually from an interrupt, or from a user.
28808253c060SGleb Smirnoff  * Users are not so important, but try be quick for the times that it's
28818253c060SGleb Smirnoff  * an interrupt.
28828253c060SGleb Smirnoff  */
28838253c060SGleb Smirnoff static __inline item_p
288442282202SGleb Smirnoff ng_getqblk(int flags)
28858253c060SGleb Smirnoff {
28868253c060SGleb Smirnoff 	item_p item = NULL;
288742282202SGleb Smirnoff 	int wait;
28888253c060SGleb Smirnoff 
288942282202SGleb Smirnoff 	wait = (flags & NG_WAITOK) ? M_WAITOK : M_NOWAIT;
289042282202SGleb Smirnoff 
289142282202SGleb Smirnoff 	item = uma_zalloc(ng_qzone, wait | M_ZERO);
28928253c060SGleb Smirnoff 
28938253c060SGleb Smirnoff #ifdef	NETGRAPH_DEBUG
28948253c060SGleb Smirnoff 	if (item) {
28958253c060SGleb Smirnoff 			mtx_lock(&ngq_mtx);
28968253c060SGleb Smirnoff 			TAILQ_INSERT_TAIL(&ng_itemlist, item, all);
28978253c060SGleb Smirnoff 			allocated++;
28988253c060SGleb Smirnoff 			mtx_unlock(&ngq_mtx);
28998253c060SGleb Smirnoff 	}
29008253c060SGleb Smirnoff #endif
29018253c060SGleb Smirnoff 
29028253c060SGleb Smirnoff 	return (item);
29038253c060SGleb Smirnoff }
29048253c060SGleb Smirnoff 
29058253c060SGleb Smirnoff /*
29068253c060SGleb Smirnoff  * Release a queue entry
29078253c060SGleb Smirnoff  */
29088253c060SGleb Smirnoff void
29098253c060SGleb Smirnoff ng_free_item(item_p item)
29108253c060SGleb Smirnoff {
29118253c060SGleb Smirnoff 	/*
29128253c060SGleb Smirnoff 	 * The item may hold resources on it's own. We need to free
29138253c060SGleb Smirnoff 	 * these before we can free the item. What they are depends upon
29148253c060SGleb Smirnoff 	 * what kind of item it is. it is important that nodes zero
29158253c060SGleb Smirnoff 	 * out pointers to resources that they remove from the item
29168253c060SGleb Smirnoff 	 * or we release them again here.
29178253c060SGleb Smirnoff 	 */
29188253c060SGleb Smirnoff 	switch (item->el_flags & NGQF_TYPE) {
29198253c060SGleb Smirnoff 	case NGQF_DATA:
29208253c060SGleb Smirnoff 		/* If we have an mbuf still attached.. */
29218253c060SGleb Smirnoff 		NG_FREE_M(_NGI_M(item));
29228253c060SGleb Smirnoff 		break;
29238253c060SGleb Smirnoff 	case NGQF_MESG:
29248253c060SGleb Smirnoff 		_NGI_RETADDR(item) = 0;
29258253c060SGleb Smirnoff 		NG_FREE_MSG(_NGI_MSG(item));
29268253c060SGleb Smirnoff 		break;
29278253c060SGleb Smirnoff 	case NGQF_FN:
29288253c060SGleb Smirnoff 		/* nothing to free really, */
29298253c060SGleb Smirnoff 		_NGI_FN(item) = NULL;
29308253c060SGleb Smirnoff 		_NGI_ARG1(item) = NULL;
29318253c060SGleb Smirnoff 		_NGI_ARG2(item) = 0;
29328253c060SGleb Smirnoff 	case NGQF_UNDEF:
29338253c060SGleb Smirnoff 		break;
29348253c060SGleb Smirnoff 	}
29358253c060SGleb Smirnoff 	/* If we still have a node or hook referenced... */
29368253c060SGleb Smirnoff 	_NGI_CLR_NODE(item);
29378253c060SGleb Smirnoff 	_NGI_CLR_HOOK(item);
29388253c060SGleb Smirnoff 
29398253c060SGleb Smirnoff #ifdef	NETGRAPH_DEBUG
29408253c060SGleb Smirnoff 	mtx_lock(&ngq_mtx);
29418253c060SGleb Smirnoff 	TAILQ_REMOVE(&ng_itemlist, item, all);
29428253c060SGleb Smirnoff 	allocated--;
29438253c060SGleb Smirnoff 	mtx_unlock(&ngq_mtx);
29448253c060SGleb Smirnoff #endif
29458253c060SGleb Smirnoff 	uma_zfree(ng_qzone, item);
29468253c060SGleb Smirnoff }
29478253c060SGleb Smirnoff 
29488253c060SGleb Smirnoff /************************************************************************
29494cf49a43SJulian Elischer 			Module routines
29504cf49a43SJulian Elischer ************************************************************************/
29514cf49a43SJulian Elischer 
29524cf49a43SJulian Elischer /*
29534cf49a43SJulian Elischer  * Handle the loading/unloading of a netgraph node type module
29544cf49a43SJulian Elischer  */
29554cf49a43SJulian Elischer int
29564cf49a43SJulian Elischer ng_mod_event(module_t mod, int event, void *data)
29574cf49a43SJulian Elischer {
29584cf49a43SJulian Elischer 	struct ng_type *const type = data;
29594cf49a43SJulian Elischer 	int s, error = 0;
29604cf49a43SJulian Elischer 
29614cf49a43SJulian Elischer 	switch (event) {
29624cf49a43SJulian Elischer 	case MOD_LOAD:
29634cf49a43SJulian Elischer 
29644cf49a43SJulian Elischer 		/* Register new netgraph node type */
29654cf49a43SJulian Elischer 		s = splnet();
29664cf49a43SJulian Elischer 		if ((error = ng_newtype(type)) != 0) {
29674cf49a43SJulian Elischer 			splx(s);
29684cf49a43SJulian Elischer 			break;
29694cf49a43SJulian Elischer 		}
29704cf49a43SJulian Elischer 
29714cf49a43SJulian Elischer 		/* Call type specific code */
29724cf49a43SJulian Elischer 		if (type->mod_event != NULL)
2973069154d5SJulian Elischer 			if ((error = (*type->mod_event)(mod, event, data))) {
29749ed346baSBosko Milekic 				mtx_lock(&ng_typelist_mtx);
2975c73b94a2SJulian Elischer 				type->refs--;	/* undo it */
29764cf49a43SJulian Elischer 				LIST_REMOVE(type, types);
29779ed346baSBosko Milekic 				mtx_unlock(&ng_typelist_mtx);
2978069154d5SJulian Elischer 			}
29794cf49a43SJulian Elischer 		splx(s);
29804cf49a43SJulian Elischer 		break;
29814cf49a43SJulian Elischer 
29824cf49a43SJulian Elischer 	case MOD_UNLOAD:
29834cf49a43SJulian Elischer 		s = splnet();
2984c73b94a2SJulian Elischer 		if (type->refs > 1) {		/* make sure no nodes exist! */
29854cf49a43SJulian Elischer 			error = EBUSY;
2986c73b94a2SJulian Elischer 		} else {
2987c73b94a2SJulian Elischer 			if (type->refs == 0) {
2988c73b94a2SJulian Elischer 				/* failed load, nothing to undo */
2989c73b94a2SJulian Elischer 				splx(s);
2990c73b94a2SJulian Elischer 				break;
2991c73b94a2SJulian Elischer 			}
29924cf49a43SJulian Elischer 			if (type->mod_event != NULL) {	/* check with type */
29934cf49a43SJulian Elischer 				error = (*type->mod_event)(mod, event, data);
29944cf49a43SJulian Elischer 				if (error != 0) {	/* type refuses.. */
29954cf49a43SJulian Elischer 					splx(s);
29964cf49a43SJulian Elischer 					break;
29974cf49a43SJulian Elischer 				}
29984cf49a43SJulian Elischer 			}
29999ed346baSBosko Milekic 			mtx_lock(&ng_typelist_mtx);
30004cf49a43SJulian Elischer 			LIST_REMOVE(type, types);
30019ed346baSBosko Milekic 			mtx_unlock(&ng_typelist_mtx);
30024cf49a43SJulian Elischer 		}
30034cf49a43SJulian Elischer 		splx(s);
30044cf49a43SJulian Elischer 		break;
30054cf49a43SJulian Elischer 
30064cf49a43SJulian Elischer 	default:
30074cf49a43SJulian Elischer 		if (type->mod_event != NULL)
30084cf49a43SJulian Elischer 			error = (*type->mod_event)(mod, event, data);
30094cf49a43SJulian Elischer 		else
30103e019deaSPoul-Henning Kamp 			error = EOPNOTSUPP;		/* XXX ? */
30114cf49a43SJulian Elischer 		break;
30124cf49a43SJulian Elischer 	}
30134cf49a43SJulian Elischer 	return (error);
30144cf49a43SJulian Elischer }
30154cf49a43SJulian Elischer 
30164cf49a43SJulian Elischer /*
30174cf49a43SJulian Elischer  * Handle loading and unloading for this code.
30184cf49a43SJulian Elischer  * The only thing we need to link into is the NETISR strucure.
30194cf49a43SJulian Elischer  */
30204cf49a43SJulian Elischer static int
30214cf49a43SJulian Elischer ngb_mod_event(module_t mod, int event, void *data)
30224cf49a43SJulian Elischer {
30231489164fSGleb Smirnoff 	int error = 0;
30244cf49a43SJulian Elischer 
30254cf49a43SJulian Elischer 	switch (event) {
30264cf49a43SJulian Elischer 	case MOD_LOAD:
30271489164fSGleb Smirnoff 		/* Initialize everything. */
30286008862bSJohn Baldwin 		mtx_init(&ng_worklist_mtx, "ng_worklist", NULL, MTX_SPIN);
3029efd8e7c9SDon Lewis 		mtx_init(&ng_typelist_mtx, "netgraph types mutex", NULL,
3030efd8e7c9SDon Lewis 		    MTX_DEF);
3031efd8e7c9SDon Lewis 		mtx_init(&ng_nodelist_mtx, "netgraph nodelist mutex", NULL,
3032efd8e7c9SDon Lewis 		    MTX_DEF);
3033efd8e7c9SDon Lewis 		mtx_init(&ng_idhash_mtx, "netgraph idhash mutex", NULL,
3034efd8e7c9SDon Lewis 		    MTX_DEF);
30351489164fSGleb Smirnoff #ifdef	NETGRAPH_DEBUG
30361489164fSGleb Smirnoff 		mtx_init(&ngq_mtx, "netgraph item list mutex", NULL,
3037efd8e7c9SDon Lewis 		    MTX_DEF);
30381489164fSGleb Smirnoff #endif
30391489164fSGleb Smirnoff 		ng_qzone = uma_zcreate("NetGraph items", sizeof(struct ng_item),
30401489164fSGleb Smirnoff 		    NULL, NULL, NULL, NULL, UMA_ALIGN_CACHE, 0);
30411489164fSGleb Smirnoff 		uma_zone_set_max(ng_qzone, maxalloc);
304268780975SGleb Smirnoff 		netisr_register(NETISR_NETGRAPH, (netisr_t *)ngintr, NULL,
304368780975SGleb Smirnoff 		    NETISR_MPSAFE);
30444cf49a43SJulian Elischer 		break;
30454cf49a43SJulian Elischer 	case MOD_UNLOAD:
30464cf49a43SJulian Elischer 		/* You cant unload it because an interface may be using it.  */
30474cf49a43SJulian Elischer 		error = EBUSY;
30484cf49a43SJulian Elischer 		break;
30494cf49a43SJulian Elischer 	default:
30504cf49a43SJulian Elischer 		error = EOPNOTSUPP;
30514cf49a43SJulian Elischer 		break;
30524cf49a43SJulian Elischer 	}
30534cf49a43SJulian Elischer 	return (error);
30544cf49a43SJulian Elischer }
30554cf49a43SJulian Elischer 
30564cf49a43SJulian Elischer static moduledata_t netgraph_mod = {
30574cf49a43SJulian Elischer 	"netgraph",
30584cf49a43SJulian Elischer 	ngb_mod_event,
30594cf49a43SJulian Elischer 	(NULL)
30604cf49a43SJulian Elischer };
3061aa38f8f9SMaksim Yevmenkin DECLARE_MODULE(netgraph, netgraph_mod, SI_SUB_NETGRAPH, SI_ORDER_MIDDLE);
3062bfa7e882SJulian Elischer SYSCTL_NODE(_net, OID_AUTO, graph, CTLFLAG_RW, 0, "netgraph Family");
3063bfa7e882SJulian Elischer SYSCTL_INT(_net_graph, OID_AUTO, abi_version, CTLFLAG_RD, 0, NG_ABI_VERSION,"");
3064bfa7e882SJulian Elischer SYSCTL_INT(_net_graph, OID_AUTO, msg_version, CTLFLAG_RD, 0, NG_VERSION, "");
30654cf49a43SJulian Elischer 
306630400f03SJulian Elischer #ifdef	NETGRAPH_DEBUG
306730400f03SJulian Elischer void
306830400f03SJulian Elischer dumphook (hook_p hook, char *file, int line)
306930400f03SJulian Elischer {
307030400f03SJulian Elischer 	printf("hook: name %s, %d refs, Last touched:\n",
307130400f03SJulian Elischer 		_NG_HOOK_NAME(hook), hook->hk_refs);
307230400f03SJulian Elischer 	printf("	Last active @ %s, line %d\n",
307330400f03SJulian Elischer 		hook->lastfile, hook->lastline);
307430400f03SJulian Elischer 	if (line) {
307530400f03SJulian Elischer 		printf(" problem discovered at file %s, line %d\n", file, line);
307630400f03SJulian Elischer 	}
307730400f03SJulian Elischer }
307830400f03SJulian Elischer 
307930400f03SJulian Elischer void
308030400f03SJulian Elischer dumpnode(node_p node, char *file, int line)
308130400f03SJulian Elischer {
308230400f03SJulian Elischer 	printf("node: ID [%x]: type '%s', %d hooks, flags 0x%x, %d refs, %s:\n",
30836b795970SJulian Elischer 		_NG_NODE_ID(node), node->nd_type->name,
308430400f03SJulian Elischer 		node->nd_numhooks, node->nd_flags,
308530400f03SJulian Elischer 		node->nd_refs, node->nd_name);
308630400f03SJulian Elischer 	printf("	Last active @ %s, line %d\n",
308730400f03SJulian Elischer 		node->lastfile, node->lastline);
308830400f03SJulian Elischer 	if (line) {
308930400f03SJulian Elischer 		printf(" problem discovered at file %s, line %d\n", file, line);
309030400f03SJulian Elischer 	}
309130400f03SJulian Elischer }
309230400f03SJulian Elischer 
3093069154d5SJulian Elischer void
3094069154d5SJulian Elischer dumpitem(item_p item, char *file, int line)
3095069154d5SJulian Elischer {
3096069154d5SJulian Elischer 	printf(" ACTIVE item, last used at %s, line %d",
3097069154d5SJulian Elischer 		item->lastfile, item->lastline);
30986b795970SJulian Elischer 	switch(item->el_flags & NGQF_TYPE) {
30996b795970SJulian Elischer 	case NGQF_DATA:
3100069154d5SJulian Elischer 		printf(" - [data]\n");
31016b795970SJulian Elischer 		break;
31026b795970SJulian Elischer 	case NGQF_MESG:
31036b795970SJulian Elischer 		printf(" - retaddr[%d]:\n", _NGI_RETADDR(item));
31046b795970SJulian Elischer 		break;
31056b795970SJulian Elischer 	case NGQF_FN:
31066b795970SJulian Elischer 		printf(" - fn@%p (%p, %p, %p, %d (%x))\n",
31076b795970SJulian Elischer 			item->body.fn.fn_fn,
31086064e568SGleb Smirnoff 			_NGI_NODE(item),
31096064e568SGleb Smirnoff 			_NGI_HOOK(item),
31106b795970SJulian Elischer 			item->body.fn.fn_arg1,
31116b795970SJulian Elischer 			item->body.fn.fn_arg2,
31126b795970SJulian Elischer 			item->body.fn.fn_arg2);
31136b795970SJulian Elischer 		break;
31146b795970SJulian Elischer 	case NGQF_UNDEF:
31156b795970SJulian Elischer 		printf(" - UNDEFINED!\n");
3116069154d5SJulian Elischer 	}
311730400f03SJulian Elischer 	if (line) {
3118069154d5SJulian Elischer 		printf(" problem discovered at file %s, line %d\n", file, line);
31196064e568SGleb Smirnoff 		if (_NGI_NODE(item)) {
312030400f03SJulian Elischer 			printf("node %p ([%x])\n",
31216064e568SGleb Smirnoff 				_NGI_NODE(item), ng_node2ID(_NGI_NODE(item)));
3122069154d5SJulian Elischer 		}
312330400f03SJulian Elischer 	}
312430400f03SJulian Elischer }
312530400f03SJulian Elischer 
312630400f03SJulian Elischer static void
312730400f03SJulian Elischer ng_dumpitems(void)
312830400f03SJulian Elischer {
312930400f03SJulian Elischer 	item_p item;
313030400f03SJulian Elischer 	int i = 1;
313130400f03SJulian Elischer 	TAILQ_FOREACH(item, &ng_itemlist, all) {
313230400f03SJulian Elischer 		printf("[%d] ", i++);
313330400f03SJulian Elischer 		dumpitem(item, NULL, 0);
313430400f03SJulian Elischer 	}
313530400f03SJulian Elischer }
313630400f03SJulian Elischer 
313730400f03SJulian Elischer static void
313830400f03SJulian Elischer ng_dumpnodes(void)
313930400f03SJulian Elischer {
314030400f03SJulian Elischer 	node_p node;
314130400f03SJulian Elischer 	int i = 1;
314253f9c5e9SRobert Watson 	mtx_lock(&ng_nodelist_mtx);
314330400f03SJulian Elischer 	SLIST_FOREACH(node, &ng_allnodes, nd_all) {
314430400f03SJulian Elischer 		printf("[%d] ", i++);
314530400f03SJulian Elischer 		dumpnode(node, NULL, 0);
314630400f03SJulian Elischer 	}
314753f9c5e9SRobert Watson 	mtx_unlock(&ng_nodelist_mtx);
314830400f03SJulian Elischer }
314930400f03SJulian Elischer 
315030400f03SJulian Elischer static void
315130400f03SJulian Elischer ng_dumphooks(void)
315230400f03SJulian Elischer {
315330400f03SJulian Elischer 	hook_p hook;
315430400f03SJulian Elischer 	int i = 1;
315553f9c5e9SRobert Watson 	mtx_lock(&ng_nodelist_mtx);
315630400f03SJulian Elischer 	SLIST_FOREACH(hook, &ng_allhooks, hk_all) {
315730400f03SJulian Elischer 		printf("[%d] ", i++);
315830400f03SJulian Elischer 		dumphook(hook, NULL, 0);
315930400f03SJulian Elischer 	}
316053f9c5e9SRobert Watson 	mtx_unlock(&ng_nodelist_mtx);
316130400f03SJulian Elischer }
3162069154d5SJulian Elischer 
3163069154d5SJulian Elischer static int
3164069154d5SJulian Elischer sysctl_debug_ng_dump_items(SYSCTL_HANDLER_ARGS)
3165069154d5SJulian Elischer {
3166069154d5SJulian Elischer 	int error;
3167069154d5SJulian Elischer 	int val;
3168069154d5SJulian Elischer 	int i;
3169069154d5SJulian Elischer 
3170069154d5SJulian Elischer 	val = allocated;
3171069154d5SJulian Elischer 	i = 1;
3172069154d5SJulian Elischer 	error = sysctl_handle_int(oidp, &val, sizeof(int), req);
31736b795970SJulian Elischer 	if (error != 0 || req->newptr == NULL)
31746b795970SJulian Elischer 		return (error);
31756b795970SJulian Elischer 	if (val == 42) {
317630400f03SJulian Elischer 		ng_dumpitems();
317730400f03SJulian Elischer 		ng_dumpnodes();
317830400f03SJulian Elischer 		ng_dumphooks();
3179069154d5SJulian Elischer 	}
31806b795970SJulian Elischer 	return (0);
3181069154d5SJulian Elischer }
3182069154d5SJulian Elischer 
31836b795970SJulian Elischer SYSCTL_PROC(_debug, OID_AUTO, ng_dump_items, CTLTYPE_INT | CTLFLAG_RW,
31846b795970SJulian Elischer     0, sizeof(int), sysctl_debug_ng_dump_items, "I", "Number of allocated items");
318530400f03SJulian Elischer #endif	/* NETGRAPH_DEBUG */
3186069154d5SJulian Elischer 
3187069154d5SJulian Elischer 
3188069154d5SJulian Elischer /***********************************************************************
3189069154d5SJulian Elischer * Worklist routines
3190069154d5SJulian Elischer **********************************************************************/
3191069154d5SJulian Elischer /* NETISR thread enters here */
3192069154d5SJulian Elischer /*
3193069154d5SJulian Elischer  * Pick a node off the list of nodes with work,
3194069154d5SJulian Elischer  * try get an item to process off it.
3195069154d5SJulian Elischer  * If there are no more, remove the node from the list.
3196069154d5SJulian Elischer  */
3197069154d5SJulian Elischer static void
3198069154d5SJulian Elischer ngintr(void)
3199069154d5SJulian Elischer {
3200069154d5SJulian Elischer 	item_p item;
3201069154d5SJulian Elischer 	node_p  node = NULL;
3202069154d5SJulian Elischer 
3203069154d5SJulian Elischer 	for (;;) {
32049ed346baSBosko Milekic 		mtx_lock_spin(&ng_worklist_mtx);
3205069154d5SJulian Elischer 		node = TAILQ_FIRST(&ng_worklist);
3206069154d5SJulian Elischer 		if (!node) {
32079ed346baSBosko Milekic 			mtx_unlock_spin(&ng_worklist_mtx);
3208069154d5SJulian Elischer 			break;
3209069154d5SJulian Elischer 		}
3210be4252b3SJulian Elischer 		node->nd_flags &= ~NGF_WORKQ;
321130400f03SJulian Elischer 		TAILQ_REMOVE(&ng_worklist, node, nd_work);
32129ed346baSBosko Milekic 		mtx_unlock_spin(&ng_worklist_mtx);
3213069154d5SJulian Elischer 		/*
3214069154d5SJulian Elischer 		 * We have the node. We also take over the reference
3215069154d5SJulian Elischer 		 * that the list had on it.
3216069154d5SJulian Elischer 		 * Now process as much as you can, until it won't
3217069154d5SJulian Elischer 		 * let you have another item off the queue.
3218069154d5SJulian Elischer 		 * All this time, keep the reference
3219069154d5SJulian Elischer 		 * that lets us be sure that the node still exists.
3220069154d5SJulian Elischer 		 * Let the reference go at the last minute.
3221b57a7965SJulian Elischer 		 * ng_dequeue will put us back on the worklist
3222b57a7965SJulian Elischer 		 * if there is more too do. This may be of use if there
3223b57a7965SJulian Elischer 		 * are Multiple Processors and multiple Net threads in the
3224b57a7965SJulian Elischer 		 * future.
3225069154d5SJulian Elischer 		 */
3226069154d5SJulian Elischer 		for (;;) {
32279ed346baSBosko Milekic 			mtx_lock_spin(&node->nd_input_queue.q_mtx);
322830400f03SJulian Elischer 			item = ng_dequeue(&node->nd_input_queue);
3229069154d5SJulian Elischer 			if (item == NULL) {
32309ed346baSBosko Milekic 				mtx_unlock_spin(&node->nd_input_queue.q_mtx);
3231069154d5SJulian Elischer 				break; /* go look for another node */
3232069154d5SJulian Elischer 			} else {
32339ed346baSBosko Milekic 				mtx_unlock_spin(&node->nd_input_queue.q_mtx);
32345951069aSJulian Elischer 				NGI_GET_NODE(item, node); /* zaps stored node */
32355951069aSJulian Elischer 				ng_apply_item(node, item);
32365951069aSJulian Elischer 				NG_NODE_UNREF(node);
3237069154d5SJulian Elischer 			}
3238069154d5SJulian Elischer 		}
3239a96dcd84SJulian Elischer 		NG_NODE_UNREF(node);
3240069154d5SJulian Elischer 	}
3241069154d5SJulian Elischer }
3242069154d5SJulian Elischer 
3243069154d5SJulian Elischer static void
3244069154d5SJulian Elischer ng_worklist_remove(node_p node)
3245069154d5SJulian Elischer {
32469ed346baSBosko Milekic 	mtx_lock_spin(&ng_worklist_mtx);
3247be4252b3SJulian Elischer 	if (node->nd_flags & NGF_WORKQ) {
3248be4252b3SJulian Elischer 		node->nd_flags &= ~NGF_WORKQ;
324933338e73SJulian Elischer 		TAILQ_REMOVE(&ng_worklist, node, nd_work);
325033338e73SJulian Elischer 		mtx_unlock_spin(&ng_worklist_mtx);
325133338e73SJulian Elischer 		NG_NODE_UNREF(node);
325233338e73SJulian Elischer 	} else {
32539ed346baSBosko Milekic 		mtx_unlock_spin(&ng_worklist_mtx);
3254069154d5SJulian Elischer 	}
325533338e73SJulian Elischer }
3256069154d5SJulian Elischer 
325733338e73SJulian Elischer /*
325833338e73SJulian Elischer  * XXX
325933338e73SJulian Elischer  * It's posible that a debugging NG_NODE_REF may need
326033338e73SJulian Elischer  * to be outside the mutex zone
326133338e73SJulian Elischer  */
3262069154d5SJulian Elischer static void
3263069154d5SJulian Elischer ng_setisr(node_p node)
3264069154d5SJulian Elischer {
3265f912c0f7SGleb Smirnoff 
32665bc15201SGleb Smirnoff 	mtx_assert(&node->nd_input_queue.q_mtx, MA_OWNED);
3267f912c0f7SGleb Smirnoff 
3268be4252b3SJulian Elischer 	if ((node->nd_flags & NGF_WORKQ) == 0) {
3269069154d5SJulian Elischer 		/*
3270069154d5SJulian Elischer 		 * If we are not already on the work queue,
3271069154d5SJulian Elischer 		 * then put us on.
3272069154d5SJulian Elischer 		 */
3273be4252b3SJulian Elischer 		node->nd_flags |= NGF_WORKQ;
3274f912c0f7SGleb Smirnoff 		mtx_lock_spin(&ng_worklist_mtx);
327530400f03SJulian Elischer 		TAILQ_INSERT_TAIL(&ng_worklist, node, nd_work);
3276f912c0f7SGleb Smirnoff 		mtx_unlock_spin(&ng_worklist_mtx);
327733338e73SJulian Elischer 		NG_NODE_REF(node); /* XXX fafe in mutex? */
3278069154d5SJulian Elischer 	}
3279069154d5SJulian Elischer 	schednetisr(NETISR_NETGRAPH);
3280069154d5SJulian Elischer }
3281069154d5SJulian Elischer 
3282069154d5SJulian Elischer 
3283069154d5SJulian Elischer /***********************************************************************
3284069154d5SJulian Elischer * Externally useable functions to set up a queue item ready for sending
3285069154d5SJulian Elischer ***********************************************************************/
3286069154d5SJulian Elischer 
328730400f03SJulian Elischer #ifdef	NETGRAPH_DEBUG
328830400f03SJulian Elischer #define	ITEM_DEBUG_CHECKS						\
32894cf49a43SJulian Elischer 	do {								\
32901acb27c6SJulian Elischer 		if (NGI_NODE(item) ) {					\
3291069154d5SJulian Elischer 			printf("item already has node");		\
3292f33ca0c9SMarcel Moolenaar 			kdb_enter("has node");				\
32931acb27c6SJulian Elischer 			NGI_CLR_NODE(item);				\
3294069154d5SJulian Elischer 		}							\
32951acb27c6SJulian Elischer 		if (NGI_HOOK(item) ) {					\
3296069154d5SJulian Elischer 			printf("item already has hook");		\
3297f33ca0c9SMarcel Moolenaar 			kdb_enter("has hook");				\
32981acb27c6SJulian Elischer 			NGI_CLR_HOOK(item);				\
3299069154d5SJulian Elischer 		}							\
3300069154d5SJulian Elischer 	} while (0)
3301069154d5SJulian Elischer #else
330230400f03SJulian Elischer #define ITEM_DEBUG_CHECKS
3303069154d5SJulian Elischer #endif
3304069154d5SJulian Elischer 
3305069154d5SJulian Elischer /*
33068ed370fdSJulian Elischer  * Put mbuf into the item.
3307069154d5SJulian Elischer  * Hook and node references will be removed when the item is dequeued.
3308069154d5SJulian Elischer  * (or equivalent)
3309069154d5SJulian Elischer  * (XXX) Unsafe because no reference held by peer on remote node.
3310069154d5SJulian Elischer  * remote node might go away in this timescale.
3311069154d5SJulian Elischer  * We know the hooks can't go away because that would require getting
3312069154d5SJulian Elischer  * a writer item on both nodes and we must have at least a  reader
3313069154d5SJulian Elischer  * here to eb able to do this.
3314069154d5SJulian Elischer  * Note that the hook loaded is the REMOTE hook.
3315069154d5SJulian Elischer  *
3316069154d5SJulian Elischer  * This is possibly in the critical path for new data.
3317069154d5SJulian Elischer  */
3318069154d5SJulian Elischer item_p
331942282202SGleb Smirnoff ng_package_data(struct mbuf *m, int flags)
3320069154d5SJulian Elischer {
3321069154d5SJulian Elischer 	item_p item;
3322069154d5SJulian Elischer 
332342282202SGleb Smirnoff 	if ((item = ng_getqblk(flags)) == NULL) {
3324069154d5SJulian Elischer 		NG_FREE_M(m);
3325069154d5SJulian Elischer 		return (NULL);
3326069154d5SJulian Elischer 	}
332730400f03SJulian Elischer 	ITEM_DEBUG_CHECKS;
33286f683eeeSGleb Smirnoff 	item->el_flags = NGQF_DATA | NGQF_READER;
3329069154d5SJulian Elischer 	item->el_next = NULL;
3330069154d5SJulian Elischer 	NGI_M(item) = m;
3331069154d5SJulian Elischer 	return (item);
3332069154d5SJulian Elischer }
3333069154d5SJulian Elischer 
3334069154d5SJulian Elischer /*
3335069154d5SJulian Elischer  * Allocate a queue item and put items into it..
3336069154d5SJulian Elischer  * Evaluate the address as this will be needed to queue it and
3337069154d5SJulian Elischer  * to work out what some of the fields should be.
3338069154d5SJulian Elischer  * Hook and node references will be removed when the item is dequeued.
3339069154d5SJulian Elischer  * (or equivalent)
3340069154d5SJulian Elischer  */
3341069154d5SJulian Elischer item_p
334242282202SGleb Smirnoff ng_package_msg(struct ng_mesg *msg, int flags)
3343069154d5SJulian Elischer {
3344069154d5SJulian Elischer 	item_p item;
3345069154d5SJulian Elischer 
334642282202SGleb Smirnoff 	if ((item = ng_getqblk(flags)) == NULL) {
3347069154d5SJulian Elischer 		NG_FREE_MSG(msg);
3348069154d5SJulian Elischer 		return (NULL);
3349069154d5SJulian Elischer 	}
335030400f03SJulian Elischer 	ITEM_DEBUG_CHECKS;
33516f683eeeSGleb Smirnoff 	/* Messages items count as writers unless explicitly exempted. */
33526f683eeeSGleb Smirnoff 	if (msg->header.cmd & NGM_READONLY)
33536f683eeeSGleb Smirnoff 		item->el_flags = NGQF_MESG | NGQF_READER;
33546f683eeeSGleb Smirnoff 	else
33556f683eeeSGleb Smirnoff 		item->el_flags = NGQF_MESG | NGQF_WRITER;
3356069154d5SJulian Elischer 	item->el_next = NULL;
3357069154d5SJulian Elischer 	/*
3358069154d5SJulian Elischer 	 * Set the current lasthook into the queue item
3359069154d5SJulian Elischer 	 */
3360069154d5SJulian Elischer 	NGI_MSG(item) = msg;
3361facfd889SArchie Cobbs 	NGI_RETADDR(item) = 0;
3362069154d5SJulian Elischer 	return (item);
3363069154d5SJulian Elischer }
3364069154d5SJulian Elischer 
3365069154d5SJulian Elischer 
3366069154d5SJulian Elischer 
33671acb27c6SJulian Elischer #define SET_RETADDR(item, here, retaddr)				\
33686b795970SJulian Elischer 	do {	/* Data or fn items don't have retaddrs */		\
33696b795970SJulian Elischer 		if ((item->el_flags & NGQF_TYPE) == NGQF_MESG) {	\
3370069154d5SJulian Elischer 			if (retaddr) {					\
3371069154d5SJulian Elischer 				NGI_RETADDR(item) = retaddr;		\
33724cf49a43SJulian Elischer 			} else {					\
3373069154d5SJulian Elischer 				/*					\
3374069154d5SJulian Elischer 				 * The old return address should be ok.	\
3375069154d5SJulian Elischer 				 * If there isn't one, use the address	\
3376069154d5SJulian Elischer 				 * here.				\
3377069154d5SJulian Elischer 				 */					\
3378069154d5SJulian Elischer 				if (NGI_RETADDR(item) == 0) {		\
3379069154d5SJulian Elischer 					NGI_RETADDR(item)		\
3380069154d5SJulian Elischer 						= ng_node2ID(here);	\
3381069154d5SJulian Elischer 				}					\
3382069154d5SJulian Elischer 			}						\
33834cf49a43SJulian Elischer 		}							\
33844cf49a43SJulian Elischer 	} while (0)
33854cf49a43SJulian Elischer 
33864cf49a43SJulian Elischer int
3387069154d5SJulian Elischer ng_address_hook(node_p here, item_p item, hook_p hook, ng_ID_t retaddr)
33884cf49a43SJulian Elischer {
33891acb27c6SJulian Elischer 	hook_p peer;
33901acb27c6SJulian Elischer 	node_p peernode;
339130400f03SJulian Elischer 	ITEM_DEBUG_CHECKS;
3392069154d5SJulian Elischer 	/*
3393069154d5SJulian Elischer 	 * Quick sanity check..
339430400f03SJulian Elischer 	 * Since a hook holds a reference on it's node, once we know
339530400f03SJulian Elischer 	 * that the peer is still connected (even if invalid,) we know
339630400f03SJulian Elischer 	 * that the peer node is present, though maybe invalid.
3397069154d5SJulian Elischer 	 */
3398069154d5SJulian Elischer 	if ((hook == NULL)
339930400f03SJulian Elischer 	|| NG_HOOK_NOT_VALID(hook)
340030400f03SJulian Elischer 	|| (NG_HOOK_PEER(hook) == NULL)
340130400f03SJulian Elischer 	|| NG_HOOK_NOT_VALID(NG_HOOK_PEER(hook))
340230400f03SJulian Elischer 	|| NG_NODE_NOT_VALID(NG_PEER_NODE(hook))) {
3403069154d5SJulian Elischer 		NG_FREE_ITEM(item);
34046b795970SJulian Elischer 		TRAP_ERROR();
3405e08d3e3cSJulian Elischer 		return (ENETDOWN);
34064cf49a43SJulian Elischer 	}
34074cf49a43SJulian Elischer 
34084cf49a43SJulian Elischer 	/*
3409069154d5SJulian Elischer 	 * Transfer our interest to the other (peer) end.
34104cf49a43SJulian Elischer 	 */
34111acb27c6SJulian Elischer 	peer = NG_HOOK_PEER(hook);
34121acb27c6SJulian Elischer 	NG_HOOK_REF(peer);
34131acb27c6SJulian Elischer 	NGI_SET_HOOK(item, peer);
34141acb27c6SJulian Elischer 	peernode = NG_PEER_NODE(hook);
34151acb27c6SJulian Elischer 	NG_NODE_REF(peernode);
34161acb27c6SJulian Elischer 	NGI_SET_NODE(item, peernode);
34178b68f82fSJulian Elischer 	SET_RETADDR(item, here, retaddr);
3418069154d5SJulian Elischer 	return (0);
3419069154d5SJulian Elischer }
3420069154d5SJulian Elischer 
34214cf49a43SJulian Elischer int
3422069154d5SJulian Elischer ng_address_path(node_p here, item_p item, char *address, ng_ID_t retaddr)
34234cf49a43SJulian Elischer {
34244cf49a43SJulian Elischer 	node_p  dest = NULL;
3425069154d5SJulian Elischer 	hook_p	hook = NULL;
34264cf49a43SJulian Elischer 	int     error;
3427069154d5SJulian Elischer 
342830400f03SJulian Elischer 	ITEM_DEBUG_CHECKS;
3429069154d5SJulian Elischer 	/*
3430069154d5SJulian Elischer 	 * Note that ng_path2noderef increments the reference count
3431069154d5SJulian Elischer 	 * on the node for us if it finds one. So we don't have to.
3432069154d5SJulian Elischer 	 */
3433069154d5SJulian Elischer 	error = ng_path2noderef(here, address, &dest, &hook);
3434069154d5SJulian Elischer 	if (error) {
3435069154d5SJulian Elischer 		NG_FREE_ITEM(item);
343630400f03SJulian Elischer 		return (error);
3437069154d5SJulian Elischer 	}
34381acb27c6SJulian Elischer 	NGI_SET_NODE(item, dest);
34391acb27c6SJulian Elischer 	if ( hook) {
344030400f03SJulian Elischer 		NG_HOOK_REF(hook);	/* don't let it go while on the queue */
34411acb27c6SJulian Elischer 		NGI_SET_HOOK(item, hook);
34421acb27c6SJulian Elischer 	}
34431acb27c6SJulian Elischer 	SET_RETADDR(item, here, retaddr);
3444069154d5SJulian Elischer 	return (0);
3445069154d5SJulian Elischer }
3446069154d5SJulian Elischer 
3447069154d5SJulian Elischer int
3448069154d5SJulian Elischer ng_address_ID(node_p here, item_p item, ng_ID_t ID, ng_ID_t retaddr)
3449069154d5SJulian Elischer {
3450069154d5SJulian Elischer 	node_p dest;
3451069154d5SJulian Elischer 
345230400f03SJulian Elischer 	ITEM_DEBUG_CHECKS;
3453069154d5SJulian Elischer 	/*
3454069154d5SJulian Elischer 	 * Find the target node.
3455069154d5SJulian Elischer 	 */
3456069154d5SJulian Elischer 	dest = ng_ID2noderef(ID); /* GETS REFERENCE! */
3457069154d5SJulian Elischer 	if (dest == NULL) {
3458069154d5SJulian Elischer 		NG_FREE_ITEM(item);
34596b795970SJulian Elischer 		TRAP_ERROR();
3460069154d5SJulian Elischer 		return(EINVAL);
3461069154d5SJulian Elischer 	}
3462069154d5SJulian Elischer 	/* Fill out the contents */
34631acb27c6SJulian Elischer 	NGI_SET_NODE(item, dest);
34641acb27c6SJulian Elischer 	NGI_CLR_HOOK(item);
34651acb27c6SJulian Elischer 	SET_RETADDR(item, here, retaddr);
3466069154d5SJulian Elischer 	return (0);
3467069154d5SJulian Elischer }
3468069154d5SJulian Elischer 
3469069154d5SJulian Elischer /*
3470069154d5SJulian Elischer  * special case to send a message to self (e.g. destroy node)
3471069154d5SJulian Elischer  * Possibly indicate an arrival hook too.
3472069154d5SJulian Elischer  * Useful for removing that hook :-)
3473069154d5SJulian Elischer  */
3474069154d5SJulian Elischer item_p
3475069154d5SJulian Elischer ng_package_msg_self(node_p here, hook_p hook, struct ng_mesg *msg)
3476069154d5SJulian Elischer {
3477069154d5SJulian Elischer 	item_p item;
34784cf49a43SJulian Elischer 
3479859a4d16SJulian Elischer 	/*
3480859a4d16SJulian Elischer 	 * Find the target node.
3481859a4d16SJulian Elischer 	 * If there is a HOOK argument, then use that in preference
3482859a4d16SJulian Elischer 	 * to the address.
3483859a4d16SJulian Elischer 	 */
348442282202SGleb Smirnoff 	if ((item = ng_getqblk(NG_NOFLAGS)) == NULL) {
3485069154d5SJulian Elischer 		NG_FREE_MSG(msg);
3486069154d5SJulian Elischer 		return (NULL);
34874cf49a43SJulian Elischer 	}
34884cf49a43SJulian Elischer 
34894cf49a43SJulian Elischer 	/* Fill out the contents */
34906f683eeeSGleb Smirnoff 	item->el_flags = NGQF_MESG | NGQF_WRITER;
3491069154d5SJulian Elischer 	item->el_next = NULL;
349230400f03SJulian Elischer 	NG_NODE_REF(here);
34931acb27c6SJulian Elischer 	NGI_SET_NODE(item, here);
34941acb27c6SJulian Elischer 	if (hook) {
349530400f03SJulian Elischer 		NG_HOOK_REF(hook);
34961acb27c6SJulian Elischer 		NGI_SET_HOOK(item, hook);
34971acb27c6SJulian Elischer 	}
3498069154d5SJulian Elischer 	NGI_MSG(item) = msg;
3499069154d5SJulian Elischer 	NGI_RETADDR(item) = ng_node2ID(here);
3500069154d5SJulian Elischer 	return (item);
35014cf49a43SJulian Elischer }
35024cf49a43SJulian Elischer 
350342282202SGleb Smirnoff int
3504aacdb114SGleb Smirnoff ng_send_fn1(node_p node, hook_p hook, ng_item_fn *fn, void * arg1, int arg2,
350542282202SGleb Smirnoff 	int flags)
35066b795970SJulian Elischer {
35076b795970SJulian Elischer 	item_p item;
35086b795970SJulian Elischer 
350942282202SGleb Smirnoff 	if ((item = ng_getqblk(flags)) == NULL) {
35106b795970SJulian Elischer 		return (ENOMEM);
35116b795970SJulian Elischer 	}
35126b795970SJulian Elischer 	item->el_flags = NGQF_FN | NGQF_WRITER;
3513a96dcd84SJulian Elischer 	NG_NODE_REF(node); /* and one for the item */
35141acb27c6SJulian Elischer 	NGI_SET_NODE(item, node);
35151acb27c6SJulian Elischer 	if (hook) {
35166b795970SJulian Elischer 		NG_HOOK_REF(hook);
35171acb27c6SJulian Elischer 		NGI_SET_HOOK(item, hook);
35186b795970SJulian Elischer 	}
35196b795970SJulian Elischer 	NGI_FN(item) = fn;
35206b795970SJulian Elischer 	NGI_ARG1(item) = arg1;
35216b795970SJulian Elischer 	NGI_ARG2(item) = arg2;
352242282202SGleb Smirnoff 	return(ng_snd_item(item, flags));
35236b795970SJulian Elischer }
35246b795970SJulian Elischer 
35254cf49a43SJulian Elischer /*
3526d2ca21a9SJulian Elischer  * Official timeout routines for Netgraph nodes.
3527d2ca21a9SJulian Elischer  */
3528d2ca21a9SJulian Elischer static void
35291fbb36ffSGleb Smirnoff ng_callout_trampoline(void *arg)
3530d2ca21a9SJulian Elischer {
3531d2ca21a9SJulian Elischer 	item_p item = arg;
3532d2ca21a9SJulian Elischer 
3533d2ca21a9SJulian Elischer 	ng_snd_item(item, 0);
3534d2ca21a9SJulian Elischer }
3535d2ca21a9SJulian Elischer 
3536d2ca21a9SJulian Elischer 
353730bef41bSGleb Smirnoff int
3538f9d9e1b4SGleb Smirnoff ng_callout(struct callout *c, node_p node, hook_p hook, int ticks,
3539d2ca21a9SJulian Elischer     ng_item_fn *fn, void * arg1, int arg2)
3540d2ca21a9SJulian Elischer {
3541d2ca21a9SJulian Elischer 	item_p item;
3542d2ca21a9SJulian Elischer 
354342282202SGleb Smirnoff 	if ((item = ng_getqblk(NG_NOFLAGS)) == NULL)
354430bef41bSGleb Smirnoff 		return (ENOMEM);
354530bef41bSGleb Smirnoff 
3546d2ca21a9SJulian Elischer 	item->el_flags = NGQF_FN | NGQF_WRITER;
3547d2ca21a9SJulian Elischer 	NG_NODE_REF(node);		/* and one for the item */
3548d2ca21a9SJulian Elischer 	NGI_SET_NODE(item, node);
3549d2ca21a9SJulian Elischer 	if (hook) {
3550d2ca21a9SJulian Elischer 		NG_HOOK_REF(hook);
3551d2ca21a9SJulian Elischer 		NGI_SET_HOOK(item, hook);
3552d2ca21a9SJulian Elischer 	}
3553d2ca21a9SJulian Elischer 	NGI_FN(item) = fn;
3554d2ca21a9SJulian Elischer 	NGI_ARG1(item) = arg1;
3555d2ca21a9SJulian Elischer 	NGI_ARG2(item) = arg2;
35561fbb36ffSGleb Smirnoff 	callout_reset(c, ticks, &ng_callout_trampoline, item);
355730bef41bSGleb Smirnoff 	return (0);
3558d2ca21a9SJulian Elischer }
3559d2ca21a9SJulian Elischer 
3560d2ca21a9SJulian Elischer /* A special modified version of untimeout() */
3561d2ca21a9SJulian Elischer int
3562f9d9e1b4SGleb Smirnoff ng_uncallout(struct callout *c, node_p node)
3563d2ca21a9SJulian Elischer {
3564d2ca21a9SJulian Elischer 	item_p item;
356530bef41bSGleb Smirnoff 	int rval;
3566d2ca21a9SJulian Elischer 
356703b25f5dSGleb Smirnoff 	KASSERT(c != NULL, ("ng_uncallout: NULL callout"));
356803b25f5dSGleb Smirnoff 	KASSERT(node != NULL, ("ng_uncallout: NULL node"));
356903b25f5dSGleb Smirnoff 
35703eadb26dSGleb Smirnoff 	rval = callout_stop(c);
357130bef41bSGleb Smirnoff 	item = c->c_arg;
357230bef41bSGleb Smirnoff 	/* Do an extra check */
35731fbb36ffSGleb Smirnoff 	if ((rval > 0) && (c->c_func == &ng_callout_trampoline) &&
357430bef41bSGleb Smirnoff 	    (NGI_NODE(item) == node)) {
3575d2ca21a9SJulian Elischer 		/*
3576d2ca21a9SJulian Elischer 		 * We successfully removed it from the queue before it ran
3577d2ca21a9SJulian Elischer 		 * So now we need to unreference everything that was
3578d2ca21a9SJulian Elischer 		 * given extra references. (NG_FREE_ITEM does this).
3579d2ca21a9SJulian Elischer 		 */
3580d2ca21a9SJulian Elischer 		NG_FREE_ITEM(item);
3581d2ca21a9SJulian Elischer 	}
358230bef41bSGleb Smirnoff 
358330bef41bSGleb Smirnoff 	return (rval);
3584d2ca21a9SJulian Elischer }
3585d2ca21a9SJulian Elischer 
3586d2ca21a9SJulian Elischer /*
3587069154d5SJulian Elischer  * Set the address, if none given, give the node here.
35884cf49a43SJulian Elischer  */
3589069154d5SJulian Elischer void
3590069154d5SJulian Elischer ng_replace_retaddr(node_p here, item_p item, ng_ID_t retaddr)
35914cf49a43SJulian Elischer {
3592069154d5SJulian Elischer 	if (retaddr) {
3593069154d5SJulian Elischer 		NGI_RETADDR(item) = retaddr;
3594069154d5SJulian Elischer 	} else {
3595069154d5SJulian Elischer 		/*
3596069154d5SJulian Elischer 		 * The old return address should be ok.
3597069154d5SJulian Elischer 		 * If there isn't one, use the address here.
3598069154d5SJulian Elischer 		 */
3599069154d5SJulian Elischer 		NGI_RETADDR(item) = ng_node2ID(here);
3600069154d5SJulian Elischer 	}
3601069154d5SJulian Elischer }
3602069154d5SJulian Elischer 
3603069154d5SJulian Elischer #define TESTING
3604069154d5SJulian Elischer #ifdef TESTING
3605069154d5SJulian Elischer /* just test all the macros */
3606069154d5SJulian Elischer void
3607069154d5SJulian Elischer ng_macro_test(item_p item);
3608069154d5SJulian Elischer void
3609069154d5SJulian Elischer ng_macro_test(item_p item)
3610069154d5SJulian Elischer {
3611069154d5SJulian Elischer 	node_p node = NULL;
3612069154d5SJulian Elischer 	hook_p hook = NULL;
36134cf49a43SJulian Elischer 	struct mbuf *m;
36144cf49a43SJulian Elischer 	struct ng_mesg *msg;
3615069154d5SJulian Elischer 	ng_ID_t retaddr;
3616069154d5SJulian Elischer 	int	error;
36174cf49a43SJulian Elischer 
3618069154d5SJulian Elischer 	NGI_GET_M(item, m);
3619069154d5SJulian Elischer 	NGI_GET_MSG(item, msg);
3620069154d5SJulian Elischer 	retaddr = NGI_RETADDR(item);
36218ed370fdSJulian Elischer 	NG_SEND_DATA(error, hook, m, NULL);
3622069154d5SJulian Elischer 	NG_SEND_DATA_ONLY(error, hook, m);
3623069154d5SJulian Elischer 	NG_FWD_NEW_DATA(error, item, hook, m);
362430400f03SJulian Elischer 	NG_FWD_ITEM_HOOK(error, item, hook);
3625069154d5SJulian Elischer 	NG_SEND_MSG_HOOK(error, node, msg, hook, retaddr);
3626069154d5SJulian Elischer 	NG_SEND_MSG_ID(error, node, msg, retaddr, retaddr);
3627069154d5SJulian Elischer 	NG_SEND_MSG_PATH(error, node, msg, ".:", retaddr);
3628069154d5SJulian Elischer 	NG_FWD_MSG_HOOK(error, node, item, hook, retaddr);
36294cf49a43SJulian Elischer }
3630069154d5SJulian Elischer #endif /* TESTING */
36314cf49a43SJulian Elischer 
3632