ng_base.c revision 70791
1/*
2 * ng_base.c
3 *
4 * Copyright (c) 1996-1999 Whistle Communications, Inc.
5 * All rights reserved.
6 *
7 * Subject to the following obligations and disclaimer of warranty, use and
8 * redistribution of this software, in source or object code forms, with or
9 * without modifications are expressly permitted by Whistle Communications;
10 * provided, however, that:
11 * 1. Any and all reproductions of the source or object code must include the
12 *    copyright notice above and the following disclaimer of warranties; and
13 * 2. No rights are granted, in any manner or form, to use Whistle
14 *    Communications, Inc. trademarks, including the mark "WHISTLE
15 *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
16 *    such appears in the above copyright notice or in the software.
17 *
18 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
19 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
20 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
21 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
23 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
24 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
25 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
26 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
27 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
28 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
29 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
30 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
34 * OF SUCH DAMAGE.
35 *
36 * Authors: Julian Elischer <julian@freebsd.org>
37 *          Archie Cobbs <archie@freebsd.org>
38 *
39 * $FreeBSD: head/sys/netgraph/ng_base.c 70791 2001-01-08 06:28:30Z julian $
40 * $Whistle: ng_base.c,v 1.39 1999/01/28 23:54:53 julian Exp $
41 */
42
43/*
44 * This file implements the base netgraph code.
45 */
46
47#include <sys/param.h>
48#include <sys/systm.h>
49#include <sys/errno.h>
50#include <sys/kernel.h>
51#include <sys/malloc.h>
52#include <sys/syslog.h>
53#include <sys/sysctl.h>
54#include <sys/linker.h>
55#include <sys/queue.h>
56#include <sys/mbuf.h>
57#include <sys/ctype.h>
58#include <machine/limits.h>
59
60#include <net/netisr.h>
61
62#include <netgraph/ng_message.h>
63#include <netgraph/netgraph.h>
64#include <netgraph/ng_parse.h>
65
66MODULE_VERSION(netgraph, 1);
67
68/* List of all active nodes */
69static LIST_HEAD(, ng_node) ng_nodelist;
70static struct mtx	ng_nodelist_mtx;
71
72#ifdef	NETGRAPH_DEBUG
73
74static SLIST_HEAD(, ng_node) ng_allnodes;
75static LIST_HEAD(, ng_node) ng_freenodes; /* in debug, we never free() them */
76static SLIST_HEAD(, ng_hook) ng_allhooks;
77static LIST_HEAD(, ng_hook) ng_freehooks; /* in debug, we never free() them */
78
79static void ng_dumpitems(void);
80static void ng_dumpnodes(void);
81static void ng_dumphooks(void);
82
83#endif	/* NETGRAPH_DEBUG */
84
85/* List nodes with unallocated work */
86static TAILQ_HEAD(, ng_node) ng_worklist = TAILQ_HEAD_INITIALIZER(ng_worklist);
87static struct mtx	ng_worklist_mtx;
88
89/* List of installed types */
90static LIST_HEAD(, ng_type) ng_typelist;
91static struct mtx	ng_typelist_mtx;
92
93/* Hash related definitions */
94/* Don't nead to initialise them because it's a LIST */
95#define ID_HASH_SIZE 32 /* most systems wont need even this many */
96static LIST_HEAD(, ng_node) ng_ID_hash[ID_HASH_SIZE];
97static struct mtx	ng_idhash_mtx;
98
99/* Mutex that protects the free queue item list */
100static volatile item_p		ngqfree;	/* free ones */
101static struct mtx	ngq_mtx;
102
103/* Internal functions */
104static int	ng_add_hook(node_p node, const char *name, hook_p * hookp);
105static int	ng_connect(hook_p hook1, hook_p hook2);
106static void	ng_disconnect_hook(hook_p hook);
107static int	ng_generic_msg(node_p here, item_p item, hook_p lasthook);
108static ng_ID_t	ng_decodeidname(const char *name);
109static int	ngb_mod_event(module_t mod, int event, void *data);
110static void	ng_worklist_remove(node_p node);
111static void	ngintr(void);
112static int	ng_apply_item(node_p node, item_p item);
113static void	ng_flush_input_queue(struct ng_queue * ngq);
114static void	ng_setisr(node_p node);
115static node_p	ng_ID2noderef(ng_ID_t ID);
116
117/* imported , these used to be externally visible, some may go back */
118int	ng_bypass(hook_p hook1, hook_p hook2);
119void	ng_cutlinks(node_p node);
120int	ng_con_nodes(node_p node, const char *name, node_p node2,
121	const char *name2);
122void	ng_destroy_hook(hook_p hook);
123node_p	ng_name2noderef(node_p node, const char *name);
124int	ng_path2noderef(node_p here, const char *path,
125	node_p *dest, hook_p *lasthook);
126struct	ng_type *ng_findtype(const char *type);
127int	ng_make_node(const char *type, node_p *nodepp);
128int	ng_mkpeer(node_p node, const char *name, const char *name2, char *type);
129int	ng_path_parse(char *addr, char **node, char **path, char **hook);
130void	ng_rmnode(node_p node);
131void	ng_unname(node_p node);
132
133
134/* Our own netgraph malloc type */
135MALLOC_DEFINE(M_NETGRAPH, "netgraph", "netgraph structures and ctrl messages");
136MALLOC_DEFINE(M_NETGRAPH_HOOK, "netgraph_hook", "netgraph hook structures");
137MALLOC_DEFINE(M_NETGRAPH_NODE, "netgraph_node", "netgraph node structures");
138MALLOC_DEFINE(M_NETGRAPH_ITEM, "netgraph_item", "netgraph item structures");
139MALLOC_DEFINE(M_NETGRAPH_META, "netgraph_meta", "netgraph name storage");
140MALLOC_DEFINE(M_NETGRAPH_MSG, "netgraph_msg", "netgraph name storage");
141
142/* Should not be visible outside this file */
143
144#define _NG_ALLOC_HOOK(hook) \
145	MALLOC(hook, hook_p, sizeof(*hook), M_NETGRAPH_HOOK, M_NOWAIT | M_ZERO)
146#define _NG_ALLOC_NODE(node) \
147	MALLOC(node, node_p, sizeof(*node), M_NETGRAPH_NODE, M_NOWAIT | M_ZERO)
148
149#ifdef NETGRAPH_DEBUG /*----------------------------------------------*/
150/*
151 * In debug mode:
152 * In an attempt to help track reference count screwups
153 * we do not free objects back to the malloc system, but keep them
154 * in a local cache where we can examine them and keep information safely
155 * after they have been freed.
156 * We use this scheme for nodes and hooks, and to some extent for items.
157 */
158static __inline hook_p
159ng_alloc_hook(void)
160{
161	hook_p hook;
162	SLIST_ENTRY(ng_hook) temp;
163	mtx_enter(&ng_nodelist_mtx, MTX_DEF);
164	hook = LIST_FIRST(&ng_freehooks);
165	if (hook) {
166		LIST_REMOVE(hook, hk_hooks);
167		bcopy(&hook->hk_all, &temp, sizeof(temp));
168		bzero(hook, sizeof(struct ng_hook));
169		bcopy(&temp, &hook->hk_all, sizeof(temp));
170		mtx_exit(&ng_nodelist_mtx, MTX_DEF);
171		hook->hk_magic = HK_MAGIC;
172	} else {
173		mtx_exit(&ng_nodelist_mtx, MTX_DEF);
174		_NG_ALLOC_HOOK(hook);
175		if (hook) {
176			hook->hk_magic = HK_MAGIC;
177			mtx_enter(&ng_nodelist_mtx, MTX_DEF);
178			SLIST_INSERT_HEAD(&ng_allhooks, hook, hk_all);
179			mtx_exit(&ng_nodelist_mtx, MTX_DEF);
180		}
181	}
182	return (hook);
183}
184
185static __inline node_p
186ng_alloc_node(void)
187{
188	node_p node;
189	SLIST_ENTRY(ng_node) temp;
190	mtx_enter(&ng_nodelist_mtx, MTX_DEF);
191	node = LIST_FIRST(&ng_freenodes);
192	if (node) {
193		LIST_REMOVE(node, nd_nodes);
194		bcopy(&node->nd_all, &temp, sizeof(temp));
195		bzero(node, sizeof(struct ng_node));
196		bcopy(&temp, &node->nd_all, sizeof(temp));
197		mtx_exit(&ng_nodelist_mtx, MTX_DEF);
198		node->nd_magic = ND_MAGIC;
199	} else {
200		mtx_exit(&ng_nodelist_mtx, MTX_DEF);
201		_NG_ALLOC_NODE(node);
202		if (node) {
203			node->nd_magic = ND_MAGIC;
204			mtx_enter(&ng_nodelist_mtx, MTX_DEF);
205			SLIST_INSERT_HEAD(&ng_allnodes, node, nd_all);
206			mtx_exit(&ng_nodelist_mtx, MTX_DEF);
207		}
208	}
209	return (node);
210}
211
212#define NG_ALLOC_HOOK(hook) do { (hook) = ng_alloc_hook(); } while (0)
213#define NG_ALLOC_NODE(node) do { (node) = ng_alloc_node(); } while (0)
214
215
216#define NG_FREE_HOOK(hook)						\
217	do {								\
218		mtx_enter(&ng_nodelist_mtx, MTX_DEF);			\
219		LIST_INSERT_HEAD(&ng_freehooks, hook, hk_hooks);	\
220		hook->hk_magic = 0;					\
221		mtx_exit(&ng_nodelist_mtx, MTX_DEF);			\
222	} while (0)
223
224#define NG_FREE_NODE(node)						\
225	do {								\
226		mtx_enter(&ng_nodelist_mtx, MTX_DEF);			\
227		LIST_INSERT_HEAD(&ng_freenodes, node, nd_nodes);	\
228		node->nd_magic = 0;					\
229		mtx_exit(&ng_nodelist_mtx, MTX_DEF);			\
230	} while (0)
231
232#else /* NETGRAPH_DEBUG */ /*----------------------------------------------*/
233
234#define NG_ALLOC_HOOK(hook) _NG_ALLOC_HOOK(hook)
235#define NG_ALLOC_NODE(node) _NG_ALLOC_NODE(node)
236
237#define NG_FREE_HOOK(hook) do { FREE((hook), M_NETGRAPH_HOOK); } while (0)
238#define NG_FREE_NODE(node) do { FREE((node), M_NETGRAPH_NODE); } while (0)
239
240#endif /* NETGRAPH_DEBUG */ /*----------------------------------------------*/
241
242/* Warning: Generally use NG_FREE_ITEM() instead */
243#define NG_FREE_ITEM_REAL(item) do { FREE((item), M_NETGRAPH_ITEM); } while (0)
244
245
246/* Set this to Debugger("X") to catch all errors as they occur */
247#ifndef TRAP_ERROR
248#define TRAP_ERROR
249#endif
250
251static	ng_ID_t nextID = 1;
252
253#ifdef INVARIANTS
254#define CHECK_DATA_MBUF(m)	do {					\
255		struct mbuf *n;						\
256		int total;						\
257									\
258		if (((m)->m_flags & M_PKTHDR) == 0)			\
259			panic("%s: !PKTHDR", __FUNCTION__);		\
260		for (total = 0, n = (m); n != NULL; n = n->m_next)	\
261			total += n->m_len;				\
262		if ((m)->m_pkthdr.len != total) {			\
263			panic("%s: %d != %d",				\
264			    __FUNCTION__, (m)->m_pkthdr.len, total);	\
265		}							\
266	} while (0)
267#else
268#define CHECK_DATA_MBUF(m)
269#endif
270
271
272/************************************************************************
273	Parse type definitions for generic messages
274************************************************************************/
275
276/* Handy structure parse type defining macro */
277#define DEFINE_PARSE_STRUCT_TYPE(lo, up, args)				\
278static const struct ng_parse_struct_info				\
279	ng_ ## lo ## _type_info = NG_GENERIC_ ## up ## _INFO args;	\
280static const struct ng_parse_type ng_generic_ ## lo ## _type = {	\
281	&ng_parse_struct_type,						\
282	&ng_ ## lo ## _type_info					\
283}
284
285DEFINE_PARSE_STRUCT_TYPE(mkpeer, MKPEER, ());
286DEFINE_PARSE_STRUCT_TYPE(connect, CONNECT, ());
287DEFINE_PARSE_STRUCT_TYPE(name, NAME, ());
288DEFINE_PARSE_STRUCT_TYPE(rmhook, RMHOOK, ());
289DEFINE_PARSE_STRUCT_TYPE(nodeinfo, NODEINFO, ());
290DEFINE_PARSE_STRUCT_TYPE(typeinfo, TYPEINFO, ());
291DEFINE_PARSE_STRUCT_TYPE(linkinfo, LINKINFO, (&ng_generic_nodeinfo_type));
292
293/* Get length of an array when the length is stored as a 32 bit
294   value immediately preceeding the array -- as with struct namelist
295   and struct typelist. */
296static int
297ng_generic_list_getLength(const struct ng_parse_type *type,
298	const u_char *start, const u_char *buf)
299{
300	return *((const u_int32_t *)(buf - 4));
301}
302
303/* Get length of the array of struct linkinfo inside a struct hooklist */
304static int
305ng_generic_linkinfo_getLength(const struct ng_parse_type *type,
306	const u_char *start, const u_char *buf)
307{
308	const struct hooklist *hl = (const struct hooklist *)start;
309
310	return hl->nodeinfo.hooks;
311}
312
313/* Array type for a variable length array of struct namelist */
314static const struct ng_parse_array_info ng_nodeinfoarray_type_info = {
315	&ng_generic_nodeinfo_type,
316	&ng_generic_list_getLength
317};
318static const struct ng_parse_type ng_generic_nodeinfoarray_type = {
319	&ng_parse_array_type,
320	&ng_nodeinfoarray_type_info
321};
322
323/* Array type for a variable length array of struct typelist */
324static const struct ng_parse_array_info ng_typeinfoarray_type_info = {
325	&ng_generic_typeinfo_type,
326	&ng_generic_list_getLength
327};
328static const struct ng_parse_type ng_generic_typeinfoarray_type = {
329	&ng_parse_array_type,
330	&ng_typeinfoarray_type_info
331};
332
333/* Array type for array of struct linkinfo in struct hooklist */
334static const struct ng_parse_array_info ng_generic_linkinfo_array_type_info = {
335	&ng_generic_linkinfo_type,
336	&ng_generic_linkinfo_getLength
337};
338static const struct ng_parse_type ng_generic_linkinfo_array_type = {
339	&ng_parse_array_type,
340	&ng_generic_linkinfo_array_type_info
341};
342
343DEFINE_PARSE_STRUCT_TYPE(typelist, TYPELIST, (&ng_generic_nodeinfoarray_type));
344DEFINE_PARSE_STRUCT_TYPE(hooklist, HOOKLIST,
345	(&ng_generic_nodeinfo_type, &ng_generic_linkinfo_array_type));
346DEFINE_PARSE_STRUCT_TYPE(listnodes, LISTNODES,
347	(&ng_generic_nodeinfoarray_type));
348
349/* List of commands and how to convert arguments to/from ASCII */
350static const struct ng_cmdlist ng_generic_cmds[] = {
351	{
352	  NGM_GENERIC_COOKIE,
353	  NGM_SHUTDOWN,
354	  "shutdown",
355	  NULL,
356	  NULL
357	},
358	{
359	  NGM_GENERIC_COOKIE,
360	  NGM_MKPEER,
361	  "mkpeer",
362	  &ng_generic_mkpeer_type,
363	  NULL
364	},
365	{
366	  NGM_GENERIC_COOKIE,
367	  NGM_CONNECT,
368	  "connect",
369	  &ng_generic_connect_type,
370	  NULL
371	},
372	{
373	  NGM_GENERIC_COOKIE,
374	  NGM_NAME,
375	  "name",
376	  &ng_generic_name_type,
377	  NULL
378	},
379	{
380	  NGM_GENERIC_COOKIE,
381	  NGM_RMHOOK,
382	  "rmhook",
383	  &ng_generic_rmhook_type,
384	  NULL
385	},
386	{
387	  NGM_GENERIC_COOKIE,
388	  NGM_NODEINFO,
389	  "nodeinfo",
390	  NULL,
391	  &ng_generic_nodeinfo_type
392	},
393	{
394	  NGM_GENERIC_COOKIE,
395	  NGM_LISTHOOKS,
396	  "listhooks",
397	  NULL,
398	  &ng_generic_hooklist_type
399	},
400	{
401	  NGM_GENERIC_COOKIE,
402	  NGM_LISTNAMES,
403	  "listnames",
404	  NULL,
405	  &ng_generic_listnodes_type	/* same as NGM_LISTNODES */
406	},
407	{
408	  NGM_GENERIC_COOKIE,
409	  NGM_LISTNODES,
410	  "listnodes",
411	  NULL,
412	  &ng_generic_listnodes_type
413	},
414	{
415	  NGM_GENERIC_COOKIE,
416	  NGM_LISTTYPES,
417	  "listtypes",
418	  NULL,
419	  &ng_generic_typeinfo_type
420	},
421	{
422	  NGM_GENERIC_COOKIE,
423	  NGM_TEXT_CONFIG,
424	  "textconfig",
425	  NULL,
426	  &ng_parse_string_type
427	},
428	{
429	  NGM_GENERIC_COOKIE,
430	  NGM_TEXT_STATUS,
431	  "textstatus",
432	  NULL,
433	  &ng_parse_string_type
434	},
435	{
436	  NGM_GENERIC_COOKIE,
437	  NGM_ASCII2BINARY,
438	  "ascii2binary",
439	  &ng_parse_ng_mesg_type,
440	  &ng_parse_ng_mesg_type
441	},
442	{
443	  NGM_GENERIC_COOKIE,
444	  NGM_BINARY2ASCII,
445	  "binary2ascii",
446	  &ng_parse_ng_mesg_type,
447	  &ng_parse_ng_mesg_type
448	},
449	{ 0 }
450};
451
452/************************************************************************
453			Node routines
454************************************************************************/
455
456/*
457 * Instantiate a node of the requested type
458 */
459int
460ng_make_node(const char *typename, node_p *nodepp)
461{
462	struct ng_type *type;
463	int	error;
464
465	/* Check that the type makes sense */
466	if (typename == NULL) {
467		TRAP_ERROR;
468		return (EINVAL);
469	}
470
471	/* Locate the node type */
472	if ((type = ng_findtype(typename)) == NULL) {
473		char filename[NG_TYPELEN + 4];
474		linker_file_t lf;
475		int error;
476
477		/* Not found, try to load it as a loadable module */
478		snprintf(filename, sizeof(filename), "ng_%s", typename);
479		error = linker_load_file(filename, &lf);
480		if (error != 0)
481			return (error);
482		lf->userrefs++;		/* pretend loaded by the syscall */
483
484		/* Try again, as now the type should have linked itself in */
485		if ((type = ng_findtype(typename)) == NULL)
486			return (ENXIO);
487	}
488
489	/*
490	 * If we have a constructor, then make the node and
491	 * call the constructor to do type specific initialisation.
492	 */
493	if (type->constructor != NULL) {
494		if ((error = ng_make_node_common(type, nodepp)) == 0) {
495			if ((error = ((*type->constructor)(*nodepp)) != 0)) {
496				NG_NODE_UNREF(*nodepp);
497			}
498		}
499	} else {
500		/*
501		 * Node has no constructor. We cannot ask for one
502		 * to be made. It must be brought into existance by
503		 * some external agency. The external acency should
504		 * call ng_make_node_common() directly to get the
505		 * netgraph part initialised.
506		 */
507		TRAP_ERROR
508		error = EINVAL;
509	}
510	return (error);
511}
512
513/*
514 * Generic node creation. Called by node initialisation for externally
515 * instantiated nodes (e.g. hardware, sockets, etc ).
516 * The returned node has a reference count of 1.
517 */
518int
519ng_make_node_common(struct ng_type *type, node_p *nodepp)
520{
521	node_p node;
522
523	/* Require the node type to have been already installed */
524	if (ng_findtype(type->name) == NULL) {
525		TRAP_ERROR;
526		return (EINVAL);
527	}
528
529	/* Make a node and try attach it to the type */
530	NG_ALLOC_NODE(node);
531	if (node == NULL) {
532		TRAP_ERROR;
533		return (ENOMEM);
534	}
535	node->nd_type = type;
536	NG_NODE_REF(node);				/* note reference */
537	type->refs++;
538
539	mtx_init(&node->nd_input_queue.q_mtx, "netgraph node mutex", 0);
540	node->nd_input_queue.queue = NULL;
541	node->nd_input_queue.last = &node->nd_input_queue.queue;
542	node->nd_input_queue.q_flags = 0;
543	node->nd_input_queue.q_node = node;
544
545	/* Initialize hook list for new node */
546	LIST_INIT(&node->nd_hooks);
547
548	/* Link us into the node linked list */
549	mtx_enter(&ng_nodelist_mtx, MTX_DEF);
550	LIST_INSERT_HEAD(&ng_nodelist, node, nd_nodes);
551	mtx_exit(&ng_nodelist_mtx, MTX_DEF);
552
553
554	/* get an ID and put us in the hash chain */
555	mtx_enter(&ng_idhash_mtx, MTX_DEF);
556	for (;;) { /* wrap protection, even if silly */
557		node_p node2 = NULL;
558		node->nd_ID = nextID++; /* 137/second for 1 year before wrap */
559		/* Is there a problem with the new number? */
560		if ((node->nd_ID == 0)
561		|| (node2 = ng_ID2noderef(node->nd_ID))) {
562			if (node2) {
563				NG_NODE_UNREF(node2);
564				node2 = NULL;
565			}
566		} else {
567			break;
568		}
569	}
570	LIST_INSERT_HEAD(&ng_ID_hash[node->nd_ID % ID_HASH_SIZE],
571							node, nd_idnodes);
572	mtx_exit(&ng_idhash_mtx, MTX_DEF);
573
574	/* Done */
575	*nodepp = node;
576	return (0);
577}
578
579/*
580 * Forceably start the shutdown process on a node. Either call
581 * it's shutdown method, or do the default shutdown if there is
582 * no type-specific method.
583 *
584 * We can only be called form a shutdown message, so we know we have
585 * a writer lock, and therefore exclusive access.
586 *
587 * Persistent node types must have a type-specific method which
588 * Allocates a new node. This one is irretrievably going away.
589 */
590void
591ng_rmnode(node_p node)
592{
593	/* Check if it's already shutting down */
594	if ((node->nd_flags & NG_CLOSING) != 0)
595		return;
596
597	/* Add an extra reference so it doesn't go away during this */
598	NG_NODE_REF(node);
599
600	/*
601	 * Mark it invalid so any newcomers know not to try use it
602	 * Also add our own mark so we can't recurse
603	 * note that NG_INVALID does not do this as it's also set during
604	 * creation
605	 */
606	node->nd_flags |= NG_INVALID|NG_CLOSING;
607
608	ng_cutlinks(node);
609
610	/*
611	 * Drain the input queue forceably.
612	 * it has no hooks so what's it going to do, bleed on someone?
613	 * Theoretically we came here from a queue entry that was added
614	 * Just before the queue was closed, so it should be empty anyway.
615	 */
616	ng_flush_input_queue(&node->nd_input_queue);
617
618	/*
619	 * Take us off the work queue if we are there.
620	 * We definatly have no work to be done.
621	 */
622	ng_worklist_remove(node);
623
624	/* Ask the type if it has anything to do in this case */
625	if (node->nd_type && node->nd_type->shutdown) {
626		(*node->nd_type->shutdown)(node);
627	} else {				/* do the default thing */
628		NG_NODE_UNREF(node);
629	}
630	if (NG_NODE_IS_VALID(node)) {
631		/*
632		 * Well, blow me down if the node code hasn't declared
633		 * that it doesn't want to die.
634		 * Presumably it is a persistant node.
635		 * XXX we need a way to tell the node
636		 * "No, really.. the hardware's going away.. REALLY die"
637		 * We need a way
638		 */
639		return;
640	}
641
642	ng_unname(node); /* basically a NOP these days */
643
644	/*
645	 * Remove extra reference, possibly the last
646	 * Possible other holders of references may include
647	 * timeout callouts, but theoretically the node's supposed to
648	 * have cancelled them. Possibly hardware dependencies may
649	 * force a driver to 'linger' with a reference.
650	 */
651	NG_NODE_UNREF(node);
652}
653
654/*
655 * Called by the destructor to remove any STANDARD external references
656 * May one day have it's own message to call it..
657 */
658void
659ng_cutlinks(node_p node)
660{
661	hook_p  hook;
662
663	/* Make sure that this is set to stop infinite loops */
664	node->nd_flags |= NG_INVALID;
665
666	/*
667	 * Drain the input queue forceably.
668	 * We also do this in ng_rmnode
669	 * to make sure we get all code paths.
670	 */
671	ng_flush_input_queue(&node->nd_input_queue);
672
673	/* Notify all remaining connected nodes to disconnect */
674	while ((hook = LIST_FIRST(&node->nd_hooks)) != NULL)
675		ng_destroy_hook(hook);
676}
677
678/*
679 * Remove a reference to the node, possibly the last
680 */
681void
682ng_unref_node(node_p node)
683{
684	int     v;
685	do {
686		v = node->nd_refs;
687	} while (! atomic_cmpset_int(&node->nd_refs, v, v - 1));
688
689	if (v == 1) { /* we were the last */
690
691		mtx_enter(&ng_nodelist_mtx, MTX_DEF);
692		node->nd_type->refs--; /* XXX maybe should get types lock? */
693		LIST_REMOVE(node, nd_nodes);
694		mtx_exit(&ng_nodelist_mtx, MTX_DEF);
695
696		mtx_enter(&ng_idhash_mtx, MTX_DEF);
697		LIST_REMOVE(node, nd_idnodes);
698		mtx_exit(&ng_idhash_mtx, MTX_DEF);
699
700		mtx_destroy(&node->nd_input_queue.q_mtx);
701		NG_FREE_NODE(node);
702	}
703}
704
705/************************************************************************
706			Node ID handling
707************************************************************************/
708static node_p
709ng_ID2noderef(ng_ID_t ID)
710{
711	node_p node;
712	mtx_enter(&ng_idhash_mtx, MTX_DEF);
713	LIST_FOREACH(node, &ng_ID_hash[ID % ID_HASH_SIZE], nd_idnodes) {
714		if (node->nd_ID == ID)
715			break;
716	}
717	if(node)
718		NG_NODE_REF(node);
719	mtx_exit(&ng_idhash_mtx, MTX_DEF);
720	return(node);
721}
722
723ng_ID_t
724ng_node2ID(node_p node)
725{
726	return (node?node->nd_ID:0);
727}
728
729/************************************************************************
730			Node name handling
731************************************************************************/
732
733/*
734 * Assign a node a name. Once assigned, the name cannot be changed.
735 */
736int
737ng_name_node(node_p node, const char *name)
738{
739	int i;
740	node_p node2;
741
742	/* Check the name is valid */
743	for (i = 0; i < NG_NODELEN + 1; i++) {
744		if (name[i] == '\0' || name[i] == '.' || name[i] == ':')
745			break;
746	}
747	if (i == 0 || name[i] != '\0') {
748		TRAP_ERROR;
749		return (EINVAL);
750	}
751	if (ng_decodeidname(name) != 0) { /* valid IDs not allowed here */
752		TRAP_ERROR;
753		return (EINVAL);
754	}
755
756	/* Check the name isn't already being used */
757	if ((node2 = ng_name2noderef(node, name)) != NULL) {
758		NG_NODE_UNREF(node2);
759		TRAP_ERROR;
760		return (EADDRINUSE);
761	}
762
763	/* copy it */
764	strncpy(NG_NODE_NAME(node), name, NG_NODELEN);
765
766	return (0);
767}
768
769/*
770 * Find a node by absolute name. The name should NOT end with ':'
771 * The name "." means "this node" and "[xxx]" means "the node
772 * with ID (ie, at address) xxx".
773 *
774 * Returns the node if found, else NULL.
775 * Eventually should add something faster than a sequential search.
776 * Note it aquires a reference on the node so you can be sure it's still there.
777 */
778node_p
779ng_name2noderef(node_p here, const char *name)
780{
781	node_p node;
782	ng_ID_t temp;
783
784	/* "." means "this node" */
785	if (strcmp(name, ".") == 0) {
786		NG_NODE_REF(here);
787		return(here);
788	}
789
790	/* Check for name-by-ID */
791	if ((temp = ng_decodeidname(name)) != 0) {
792		return (ng_ID2noderef(temp));
793	}
794
795	/* Find node by name */
796	mtx_enter(&ng_nodelist_mtx, MTX_DEF);
797	LIST_FOREACH(node, &ng_nodelist, nd_nodes) {
798		if (NG_NODE_HAS_NAME(node)
799		&& (strcmp(NG_NODE_NAME(node), name) == 0))
800			break;
801	}
802	if (node)
803		NG_NODE_REF(node);
804	mtx_exit(&ng_nodelist_mtx, MTX_DEF);
805	return (node);
806}
807
808/*
809 * Decode a ID name, eg. "[f03034de]". Returns 0 if the
810 * string is not valid, otherwise returns the value.
811 */
812static ng_ID_t
813ng_decodeidname(const char *name)
814{
815	const int len = strlen(name);
816	char *eptr;
817	u_long val;
818
819	/* Check for proper length, brackets, no leading junk */
820	if (len < 3 || name[0] != '[' || name[len - 1] != ']'
821	    || !isxdigit(name[1]))
822		return (0);
823
824	/* Decode number */
825	val = strtoul(name + 1, &eptr, 16);
826	if (eptr - name != len - 1 || val == ULONG_MAX || val == 0)
827		return ((ng_ID_t)0);
828	return (ng_ID_t)val;
829}
830
831/*
832 * Remove a name from a node. This should only be called
833 * when shutting down and removing the node.
834 */
835void
836ng_unname(node_p node)
837{
838	bzero(NG_NODE_NAME(node), NG_NODELEN);
839}
840
841/************************************************************************
842			Hook routines
843 Names are not optional. Hooks are always connected, except for a
844 brief moment within these routines.
845************************************************************************/
846
847/*
848 * Remove a hook reference
849 */
850void
851ng_unref_hook(hook_p hook)
852{
853	int     v;
854	do {
855		v = hook->hk_refs;
856	} while (! atomic_cmpset_int(&hook->hk_refs, v, v - 1));
857
858	if (v == 1) { /* we were the last */
859		if (NG_HOOK_NODE(hook)) {
860			NG_NODE_UNREF((NG_HOOK_NODE(hook)));
861			hook->hk_node = NULL;
862		}
863		NG_FREE_HOOK(hook);
864	}
865}
866
867/*
868 * Add an unconnected hook to a node. Only used internally.
869 */
870static int
871ng_add_hook(node_p node, const char *name, hook_p *hookp)
872{
873	hook_p hook;
874	int error = 0;
875
876	/* Check that the given name is good */
877	if (name == NULL) {
878		TRAP_ERROR;
879		return (EINVAL);
880	}
881	if (ng_findhook(node, name) != NULL) {
882		TRAP_ERROR;
883		return (EEXIST);
884	}
885
886	/* Allocate the hook and link it up */
887	NG_ALLOC_HOOK(hook);
888	if (hook == NULL) {
889		TRAP_ERROR;
890		return (ENOMEM);
891	}
892	hook->hk_refs = 1;
893	hook->hk_flags = HK_INVALID;
894	hook->hk_node = node;
895	NG_NODE_REF(node);		/* each hook counts as a reference */
896
897	/* Check if the node type code has something to say about it */
898	if (node->nd_type->newhook != NULL)
899		if ((error = (*node->nd_type->newhook)(node, hook, name)) != 0) {
900			NG_HOOK_UNREF(hook);	/* this frees the hook */
901			return (error);
902		}
903
904	/*
905	 * The 'type' agrees so far, so go ahead and link it in.
906	 * We'll ask again later when we actually connect the hooks.
907	 * The reference we have is for this linkage.
908	 */
909	LIST_INSERT_HEAD(&node->nd_hooks, hook, hk_hooks);
910	node->nd_numhooks++;
911
912	/* Set hook name */
913	strncpy(NG_HOOK_NAME(hook), name, NG_HOOKLEN);
914	if (hookp)
915		*hookp = hook;
916	return (error);
917}
918
919/*
920 * Connect a pair of hooks. Only used internally.
921 */
922static int
923ng_connect(hook_p hook1, hook_p hook2)
924{
925	int     error;
926
927	hook1->hk_peer = hook2;
928	hook2->hk_peer = hook1;
929
930	/* Give each node the opportunity to veto the impending connection */
931	if (hook1->hk_node->nd_type->connect) {
932		if ((error = (*hook1->hk_node->nd_type->connect) (hook1))) {
933			ng_destroy_hook(hook1);	/* also zaps hook2 */
934			return (error);
935		}
936	}
937	if (hook2->hk_node->nd_type->connect) {
938		if ((error = (*hook2->hk_node->nd_type->connect) (hook2))) {
939			ng_destroy_hook(hook2);	/* also zaps hook1 */
940			return (error);
941		}
942	}
943	hook1->hk_flags &= ~HK_INVALID;
944	hook2->hk_flags &= ~HK_INVALID;
945	return (0);
946}
947
948/*
949 * Find a hook
950 *
951 * Node types may supply their own optimized routines for finding
952 * hooks.  If none is supplied, we just do a linear search.
953 */
954hook_p
955ng_findhook(node_p node, const char *name)
956{
957	hook_p hook;
958
959	if (node->nd_type->findhook != NULL)
960		return (*node->nd_type->findhook)(node, name);
961	LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
962		if (strcmp(NG_HOOK_NAME(hook), name) == 0)
963			return (hook);
964	}
965	return (NULL);
966}
967
968/*
969 * Destroy a hook
970 *
971 * As hooks are always attached, this really destroys two hooks.
972 * The one given, and the one attached to it. Disconnect the hooks
973 * from each other first.
974 */
975void
976ng_destroy_hook(hook_p hook)
977{
978	hook_p peer = NG_HOOK_PEER(hook);
979
980	hook->hk_flags |= HK_INVALID;		/* as soon as possible */
981	if (peer) {
982		peer->hk_flags |= HK_INVALID;	/* as soon as possible */
983		hook->hk_peer = NULL;
984		peer->hk_peer = NULL;
985		ng_disconnect_hook(peer);
986	}
987	ng_disconnect_hook(hook);
988}
989
990/*
991 * Notify the node of the hook's demise. This may result in more actions
992 * (e.g. shutdown) but we don't do that ourselves and don't know what
993 * happens there. If there is no appropriate handler, then just remove it
994 * (and decrement the reference count of it's node which in turn might
995 * make something happen).
996 */
997static void
998ng_disconnect_hook(hook_p hook)
999{
1000	node_p node = NG_HOOK_NODE(hook);
1001
1002	/*
1003	 * Remove the hook from the node's list to avoid possible recursion
1004	 * in case the disconnection results in node shutdown.
1005	 */
1006	LIST_REMOVE(hook, hk_hooks);
1007	node->nd_numhooks--;
1008	if (node->nd_type->disconnect) {
1009		/*
1010		 * The type handler may elect to destroy the peer so don't
1011		 * trust its existance after this point.
1012		 */
1013		(*node->nd_type->disconnect) (hook);
1014	}
1015	NG_HOOK_UNREF(hook);
1016}
1017
1018/*
1019 * Take two hooks on a node and merge the connection so that the given node
1020 * is effectively bypassed.
1021 */
1022int
1023ng_bypass(hook_p hook1, hook_p hook2)
1024{
1025	if (hook1->hk_node != hook2->hk_node) {
1026		TRAP_ERROR
1027		return (EINVAL);
1028	}
1029	hook1->hk_peer->hk_peer = hook2->hk_peer;
1030	hook2->hk_peer->hk_peer = hook1->hk_peer;
1031
1032	/* XXX If we ever cache methods on hooks update them as well */
1033	hook1->hk_peer = NULL;
1034	hook2->hk_peer = NULL;
1035	ng_destroy_hook(hook1);
1036	ng_destroy_hook(hook2);
1037	return (0);
1038}
1039
1040/*
1041 * Install a new netgraph type
1042 */
1043int
1044ng_newtype(struct ng_type *tp)
1045{
1046	const size_t namelen = strlen(tp->name);
1047
1048	/* Check version and type name fields */
1049	if ((tp->version != NG_ABI_VERSION)
1050	|| (namelen == 0)
1051	|| (namelen > NG_TYPELEN)) {
1052		TRAP_ERROR;
1053		return (EINVAL);
1054	}
1055
1056	/* Check for name collision */
1057	if (ng_findtype(tp->name) != NULL) {
1058		TRAP_ERROR;
1059		return (EEXIST);
1060	}
1061
1062	tp->refs = 0;
1063
1064	/* Link in new type */
1065	mtx_enter(&ng_typelist_mtx, MTX_DEF);
1066	LIST_INSERT_HEAD(&ng_typelist, tp, types);
1067	mtx_exit(&ng_typelist_mtx, MTX_DEF);
1068	return (0);
1069}
1070
1071/*
1072 * Look for a type of the name given
1073 */
1074struct ng_type *
1075ng_findtype(const char *typename)
1076{
1077	struct ng_type *type;
1078
1079	mtx_enter(&ng_typelist_mtx, MTX_DEF);
1080	LIST_FOREACH(type, &ng_typelist, types) {
1081		if (strcmp(type->name, typename) == 0)
1082			break;
1083	}
1084	mtx_exit(&ng_typelist_mtx, MTX_DEF);
1085	return (type);
1086}
1087
1088/************************************************************************
1089			Composite routines
1090************************************************************************/
1091
1092/*
1093 * Make a peer and connect. The order is arranged to minimise
1094 * the work needed to back out in case of error.
1095 */
1096int
1097ng_mkpeer(node_p node, const char *name, const char *name2, char *type)
1098{
1099	node_p  node2;
1100	hook_p  hook;
1101	hook_p  hook2;
1102	int     error;
1103
1104	if ((error = ng_add_hook(node, name, &hook)))
1105		return (error);
1106	if ((error = ng_make_node(type, &node2))) {
1107		ng_destroy_hook(hook);
1108		return (error);
1109	}
1110	if ((error = ng_add_hook(node2, name2, &hook2))) {
1111		ng_rmnode(node2);
1112		ng_destroy_hook(hook);
1113		return (error);
1114	}
1115
1116	/*
1117	 * Actually link the two hooks together.. on failure they are
1118	 * destroyed so we don't have to do that here.
1119	 */
1120	if ((error = ng_connect(hook, hook2)))
1121		ng_rmnode(node2);
1122	return (error);
1123}
1124
1125/*
1126 * Connect two nodes using the specified hooks
1127 */
1128int
1129ng_con_nodes(node_p node, const char *name, node_p node2, const char *name2)
1130{
1131	int     error;
1132	hook_p  hook;
1133	hook_p  hook2;
1134
1135	if ((error = ng_add_hook(node, name, &hook)))
1136		return (error);
1137	if ((error = ng_add_hook(node2, name2, &hook2))) {
1138		ng_destroy_hook(hook);
1139		return (error);
1140	}
1141	return (ng_connect(hook, hook2));
1142}
1143/************************************************************************
1144		Utility routines to send self messages
1145************************************************************************/
1146/*
1147 * Static version of shutdown message. we don't want to need resources
1148 * to shut down (we may be doing it to release resources because we ran out.
1149 */
1150static struct	ng_mesg  ng_msg_shutdown = {
1151	{NG_VERSION,		/* u_char */
1152	0,			/* u_char spare */
1153	0,			/* u_int16_t arglen */
1154	NGF_STATIC,		/* u_int32_t flags */
1155	0,			/* u_int32_t token */
1156	NGM_GENERIC_COOKIE,	/* u_int32_t */
1157	NGM_SHUTDOWN,		/* u_int32_t */
1158	"shutdown"}		/* u_char[16] */
1159};
1160
1161int
1162ng_rmnode_self(node_p here)
1163{
1164	item_p	item;
1165	struct	ng_mesg	*msg;
1166
1167	/*
1168	 * Use the static version to avoid needing
1169	 * memory allocation to succeed.
1170	 * The message is never written to and always the same.
1171	 */
1172	msg = &ng_msg_shutdown;
1173
1174	/*
1175	 * Try get a queue item to send it with.
1176	 * Hopefully since it has a reserve, we can get one.
1177	 * If we can't we are screwed anyhow.
1178	 * Increase the chances by flushing our queue first.
1179	 * We may free an item, (if we were the hog).
1180	 * Work in progress is allowed to complete.
1181	 * We also pretty much ensure that we come straight
1182	 * back in to do the shutdown. It may be a good idea
1183	 * to hold a reference actually to stop it from all
1184	 * going up in smoke.
1185	 */
1186/*	ng_flush_input_queue(&here->nd_input_queue); will mask problem  */
1187	item = ng_package_msg_self(here, NULL, msg);
1188	if (item == NULL) { /* it would have freed the msg except static */
1189		/* try again after flushing our queue */
1190		ng_flush_input_queue(&here->nd_input_queue);
1191		item = ng_package_msg_self(here, NULL, msg);
1192		if (item == NULL) {
1193			printf("failed to free node 0x%x\n", ng_node2ID(here));
1194			return (ENOMEM);
1195		}
1196	}
1197	return (ng_snd_item(item, 0));
1198}
1199
1200/***********************************************************************
1201 * Parse and verify a string of the form:  <NODE:><PATH>
1202 *
1203 * Such a string can refer to a specific node or a specific hook
1204 * on a specific node, depending on how you look at it. In the
1205 * latter case, the PATH component must not end in a dot.
1206 *
1207 * Both <NODE:> and <PATH> are optional. The <PATH> is a string
1208 * of hook names separated by dots. This breaks out the original
1209 * string, setting *nodep to "NODE" (or NULL if none) and *pathp
1210 * to "PATH" (or NULL if degenerate). Also, *hookp will point to
1211 * the final hook component of <PATH>, if any, otherwise NULL.
1212 *
1213 * This returns -1 if the path is malformed. The char ** are optional.
1214 ***********************************************************************/
1215int
1216ng_path_parse(char *addr, char **nodep, char **pathp, char **hookp)
1217{
1218	char   *node, *path, *hook;
1219	int     k;
1220
1221	/*
1222	 * Extract absolute NODE, if any
1223	 */
1224	for (path = addr; *path && *path != ':'; path++);
1225	if (*path) {
1226		node = addr;	/* Here's the NODE */
1227		*path++ = '\0';	/* Here's the PATH */
1228
1229		/* Node name must not be empty */
1230		if (!*node)
1231			return -1;
1232
1233		/* A name of "." is OK; otherwise '.' not allowed */
1234		if (strcmp(node, ".") != 0) {
1235			for (k = 0; node[k]; k++)
1236				if (node[k] == '.')
1237					return -1;
1238		}
1239	} else {
1240		node = NULL;	/* No absolute NODE */
1241		path = addr;	/* Here's the PATH */
1242	}
1243
1244	/* Snoop for illegal characters in PATH */
1245	for (k = 0; path[k]; k++)
1246		if (path[k] == ':')
1247			return -1;
1248
1249	/* Check for no repeated dots in PATH */
1250	for (k = 0; path[k]; k++)
1251		if (path[k] == '.' && path[k + 1] == '.')
1252			return -1;
1253
1254	/* Remove extra (degenerate) dots from beginning or end of PATH */
1255	if (path[0] == '.')
1256		path++;
1257	if (*path && path[strlen(path) - 1] == '.')
1258		path[strlen(path) - 1] = 0;
1259
1260	/* If PATH has a dot, then we're not talking about a hook */
1261	if (*path) {
1262		for (hook = path, k = 0; path[k]; k++)
1263			if (path[k] == '.') {
1264				hook = NULL;
1265				break;
1266			}
1267	} else
1268		path = hook = NULL;
1269
1270	/* Done */
1271	if (nodep)
1272		*nodep = node;
1273	if (pathp)
1274		*pathp = path;
1275	if (hookp)
1276		*hookp = hook;
1277	return (0);
1278}
1279
1280/*
1281 * Given a path, which may be absolute or relative, and a starting node,
1282 * return the destination node.
1283 */
1284int
1285ng_path2noderef(node_p here, const char *address,
1286				node_p *destp, hook_p *lasthook)
1287{
1288	char    fullpath[NG_PATHLEN + 1];
1289	char   *nodename, *path, pbuf[2];
1290	node_p  node, oldnode;
1291	char   *cp;
1292	hook_p hook = NULL;
1293
1294	/* Initialize */
1295	if (destp == NULL) {
1296		TRAP_ERROR
1297		return EINVAL;
1298	}
1299	*destp = NULL;
1300
1301	/* Make a writable copy of address for ng_path_parse() */
1302	strncpy(fullpath, address, sizeof(fullpath) - 1);
1303	fullpath[sizeof(fullpath) - 1] = '\0';
1304
1305	/* Parse out node and sequence of hooks */
1306	if (ng_path_parse(fullpath, &nodename, &path, NULL) < 0) {
1307		TRAP_ERROR;
1308		return EINVAL;
1309	}
1310	if (path == NULL) {
1311		pbuf[0] = '.';	/* Needs to be writable */
1312		pbuf[1] = '\0';
1313		path = pbuf;
1314	}
1315
1316	/*
1317	 * For an absolute address, jump to the starting node.
1318	 * Note that this holds a reference on the node for us.
1319	 * Don't forget to drop the reference if we don't need it.
1320	 */
1321	if (nodename) {
1322		node = ng_name2noderef(here, nodename);
1323		if (node == NULL) {
1324			TRAP_ERROR
1325			return (ENOENT);
1326		}
1327	} else {
1328		if (here == NULL) {
1329			TRAP_ERROR
1330			return (EINVAL);
1331		}
1332		node = here;
1333		NG_NODE_REF(node);
1334	}
1335
1336	/*
1337	 * Now follow the sequence of hooks
1338	 * XXX
1339	 * We actually cannot guarantee that the sequence
1340	 * is not being demolished as we crawl along it
1341	 * without extra-ordinary locking etc.
1342	 * So this is a bit dodgy to say the least.
1343	 * We can probably hold up some things by holding
1344	 * the nodelist mutex for the time of this
1345	 * crawl if we wanted.. At least that way we wouldn't have to
1346	 * worry about the nodes dissappearing, but the hooks would still
1347	 * be a problem.
1348	 */
1349	for (cp = path; node != NULL && *cp != '\0'; ) {
1350		char *segment;
1351
1352		/*
1353		 * Break out the next path segment. Replace the dot we just
1354		 * found with a NUL; "cp" points to the next segment (or the
1355		 * NUL at the end).
1356		 */
1357		for (segment = cp; *cp != '\0'; cp++) {
1358			if (*cp == '.') {
1359				*cp++ = '\0';
1360				break;
1361			}
1362		}
1363
1364		/* Empty segment */
1365		if (*segment == '\0')
1366			continue;
1367
1368		/* We have a segment, so look for a hook by that name */
1369		hook = ng_findhook(node, segment);
1370
1371		/* Can't get there from here... */
1372		if (hook == NULL
1373		    || NG_HOOK_PEER(hook) == NULL
1374		    || NG_HOOK_NOT_VALID(hook)
1375		    || NG_HOOK_NOT_VALID(NG_HOOK_PEER(hook))) {
1376			TRAP_ERROR;
1377			NG_NODE_UNREF(node);
1378#if 0
1379			printf("hooknotvalid %s %s %d %d %d %d ",
1380					path,
1381					segment,
1382					hook == NULL,
1383		     			NG_HOOK_PEER(hook) == NULL,
1384		     			NG_HOOK_NOT_VALID(hook),
1385		     			NG_HOOK_NOT_VALID(NG_HOOK_PEER(hook)));
1386#endif
1387			return (ENOENT);
1388		}
1389
1390		/*
1391		 * Hop on over to the next node
1392		 * XXX
1393		 * Big race conditions here as hooks and nodes go away
1394		 * *** Idea.. store an ng_ID_t in each hook and use that
1395		 * instead of the direct hook in this crawl?
1396		 */
1397		oldnode = node;
1398		if ((node = NG_PEER_NODE(hook)))
1399			NG_NODE_REF(node);	/* XXX RACE */
1400		NG_NODE_UNREF(oldnode);	/* XXX another race */
1401		if (NG_NODE_NOT_VALID(node)) {
1402			NG_NODE_UNREF(node);	/* XXX more races */
1403			node = NULL;
1404		}
1405	}
1406
1407	/* If node somehow missing, fail here (probably this is not needed) */
1408	if (node == NULL) {
1409		TRAP_ERROR;
1410		return (ENXIO);
1411	}
1412
1413	/* Done */
1414	*destp = node;
1415	if (lasthook != NULL)
1416		*lasthook = (hook ? NG_HOOK_PEER(hook) : NULL);
1417	return (0);
1418}
1419
1420/***************************************************************\
1421* Input queue handling.
1422* All activities are submitted to the node via the input queue
1423* which implements a multiple-reader/single-writer gate.
1424* Items which cannot be handled immeditly are queued.
1425*
1426* read-write queue locking inline functions			*
1427\***************************************************************/
1428
1429static __inline item_p ng_dequeue(struct ng_queue * ngq);
1430static __inline item_p ng_acquire_read(struct ng_queue * ngq,
1431					item_p  item);
1432static __inline item_p ng_acquire_write(struct ng_queue * ngq,
1433					item_p  item);
1434static __inline void	ng_leave_read(struct ng_queue * ngq);
1435static __inline void	ng_leave_write(struct ng_queue * ngq);
1436static __inline void	ng_queue_rw(struct ng_queue * ngq,
1437					item_p  item, int rw);
1438
1439/*
1440 * Definition of the bits fields in the ng_queue flag word.
1441 * Defined here rather than in netgraph.h because no-one should fiddle
1442 * with them.
1443 *
1444 * The ordering here is important! don't shuffle these. If you add
1445 * READ_PENDING to the word when it has READ_PENDING already set, you
1446 * generate a carry into the reader count, this you atomically add a reader,
1447 * and remove the pending reader count! Similarly for the pending writer
1448 * flag, adding WRITE_PENDING generates a carry and sets the WRITER_ACTIVE
1449 * flag, while clearing WRITE_PENDING. When 'SINGLE_THREAD_ONLY' is set, then
1450 * it is only permitted to do WRITER operations. Reader operations will
1451 * result in errors.
1452 * But that "hack" is unnecessary: "cpp" can do the math for us!
1453 */
1454/*-
1455 Safety Barrier--------+ (adjustable to suit taste) (not used yet)
1456                       |
1457                       V
1458+-------+-------+-------+-------+-------+-------+-------+-------+
1459| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
1460|A|c|t|i|v|e| |R|e|a|d|e|r| |C|o|u|n|t| | | | | | | | | |R|A|W|S|
1461| | | | | | | | | | | | | | | | | | | | | | | | | | | | |P|W|P|T|
1462+-------+-------+-------+-------+-------+-------+-------+-------+
1463\_________________________ ____________________________/ | | | |
1464                          V                              | | | |
1465                [active reader count]                    | | | |
1466                                                         | | | |
1467        Read Pending ------------------------------------+ | | |
1468                                                           | | |
1469        Active Writer -------------------------------------+ | |
1470                                                             | |
1471        Write Pending ---------------------------------------+ |
1472                                                               |
1473        Single Threading Only ---------------------------------+
1474*/
1475#define	SINGLE_THREAD_ONLY 0x00000001	/* if set, even reads single thread */
1476#define WRITE_PENDING	0x00000002
1477#define	WRITER_ACTIVE	0x00000004
1478#define READ_PENDING	0x00000008
1479#define	READER_INCREMENT 0x00000010
1480#define	READER_MASK	0xfffffff0	/* Not valid if WRITER_ACTIVE is set */
1481#define SAFETY_BARRIER	0x00100000	/* 64K items queued should be enough */
1482/*
1483 * Taking into account the current state of the queue and node, possibly take
1484 * the next entry off the queue and return it. Return NULL if there was
1485 * nothing we could return, either because there really was nothing there, or
1486 * because the node was in a state where it cannot yet process the next item
1487 * on the queue.
1488 *
1489 * This MUST MUST MUST be called with the mutex held.
1490 */
1491static __inline item_p
1492ng_dequeue(struct ng_queue *ngq)
1493{
1494	item_p item;
1495	u_int		add_arg;
1496	/*
1497	 * If there is a writer, then the answer is "no". Everything else
1498	 * stops when there is a WRITER.
1499	 */
1500	if (ngq->q_flags & WRITER_ACTIVE) {
1501		return (NULL);
1502	}
1503	/* Now take a look at what's on the queue and what's running */
1504	if ((ngq->q_flags & ~(READER_MASK | SINGLE_THREAD_ONLY)) == READ_PENDING) {
1505		/*
1506		 * It was a reader and we have no write active. We don't care
1507		 * how many readers are already active. Adjust the count for
1508		 * the item we are about to dequeue. Adding READ_PENDING to
1509		 * the exisiting READ_PENDING clears it and generates a carry
1510		 * into the reader count.
1511		 */
1512		add_arg = READ_PENDING;
1513	} else if ((ngq->q_flags & ~SINGLE_THREAD_ONLY) == WRITE_PENDING) {
1514		/*
1515		 * There is a pending write, no readers and no active writer.
1516		 * This means we can go ahead with the pending writer. Note
1517		 * the fact that we now have a writer, ready for when we take
1518		 * it off the queue.
1519		 *
1520		 * We don't need to worry about a possible collision with the
1521		 * fasttrack reader.
1522		 *
1523		 * The fasttrack thread may take a long time to discover that we
1524		 * are running so we would have an inconsistent state in the
1525		 * flags for a while. Since we ignore the reader count
1526		 * entirely when the WRITER_ACTIVE flag is set, this should
1527		 * not matter (in fact it is defined that way). If it tests
1528		 * the flag before this operation, the WRITE_PENDING flag
1529		 * will make it fail, and if it tests it later, the
1530		 * ACTIVE_WRITER flag will do the same. If it is SO slow that
1531		 * we have actually completed the operation, and neither flag
1532		 * is set (nor the READ_PENDING) by the time that it tests
1533		 * the flags, then it is actually ok for it to continue. If
1534		 * it completes and we've finished and the read pending is
1535		 * set it still fails.
1536		 *
1537		 * So we can just ignore it,  as long as we can ensure that the
1538		 * transition from WRITE_PENDING state to the WRITER_ACTIVE
1539		 * state is atomic.
1540		 *
1541		 * After failing, first it will be held back by the mutex, then
1542		 * when it can proceed, it will queue its request, then it
1543		 * would arrive at this function. Usually it will have to
1544		 * leave empty handed because the ACTIVE WRITER bit wil be
1545		 * set.
1546		 */
1547		/*
1548		 * Adjust the flags for the item we are about to dequeue.
1549		 * Adding WRITE_PENDING to the exisiting WRITE_PENDING clears
1550		 * it and generates a carry into the WRITER_ACTIVE flag, all
1551		 * atomically.
1552		 */
1553		add_arg = WRITE_PENDING;
1554		/*
1555		 * We want to write "active writer, no readers " Now go make
1556		 * it true. In fact there may be a number in the readers
1557		 * count but we know it is not true and will be fixed soon.
1558		 * We will fix the flags for the next pending entry in a
1559		 * moment.
1560		 */
1561	} else {
1562		/*
1563		 * We can't dequeue anything.. return and say so. Probably we
1564		 * have a write pending and the readers count is non zero. If
1565		 * we got here because a reader hit us just at the wrong
1566		 * moment with the fasttrack code, and put us in a strange
1567		 * state, then it will be through in just a moment, (as soon
1568		 * as we release the mutex) and keep things moving.
1569		 */
1570		return (0);
1571	}
1572
1573	/*
1574	 * Now we dequeue the request (whatever it may be) and correct the
1575	 * pending flags and the next and last pointers.
1576	 */
1577	item = ngq->queue;
1578	ngq->queue = item->el_next;
1579	if (ngq->last == &(item->el_next)) {
1580		/*
1581		 * that was the last entry in the queue so set the 'last
1582		 * pointer up correctly and make sure the pending flags are
1583		 * clear.
1584		 */
1585		ngq->last = &(ngq->queue);
1586		/*
1587		 * Whatever flag was set is cleared and the carry sets the
1588		 * correct new active state/count. So we don't need to change
1589		 * add_arg.
1590		 */
1591	} else {
1592		if ((ngq->queue->el_flags & NGQF_TYPE) == NGQF_READER) {
1593			/*
1594			 * If we had a READ_PENDING and have another one, we
1595			 * just want to add READ_PENDING twice (the same as
1596			 * adding READER_INCREMENT). If we had WRITE_PENDING,
1597			 * we want to add READ_PENDING + WRITE_PENDING to
1598			 * clear the old WRITE_PENDING, set ACTIVE_WRITER,
1599			 * and set READ_PENDING. Either way we just add
1600			 * READ_PENDING to whatever we already had.
1601			 */
1602			add_arg += READ_PENDING;
1603		} else {
1604			/*
1605			 * If we had a WRITE_PENDING and have another one, we
1606			 * just want to add WRITE_PENDING twice (the same as
1607			 * adding ACTIVE_WRITER). If we had READ_PENDING, we
1608			 * want to add READ_PENDING + WRITE_PENDING to clear
1609			 * the old READ_PENDING, increment the readers, and
1610			 * set WRITE_PENDING. Either way we just add
1611			 * WRITE_PENDING to whatever we already had.
1612			 */
1613			add_arg += WRITE_PENDING;
1614		}
1615	}
1616	atomic_add_long(&ngq->q_flags, add_arg);
1617	/*
1618	 * We have successfully cleared the old pending flag, set the new one
1619	 * if it is needed, and incremented the appropriate active field.
1620	 * (all in one atomic addition.. wow)
1621	 */
1622	return (item);
1623}
1624
1625/*
1626 * Queue a packet to be picked up by someone else.
1627 * We really don't care who, but we can't or don't want to hang around
1628 * to process it ourselves. We are probably an interrupt routine..
1629 * 1 = writer, 0 = reader
1630 * We should set something to indicate NETISR requested
1631 * If it's the first item queued.
1632 */
1633#define NGQRW_R 0
1634#define NGQRW_W 1
1635static __inline void
1636ng_queue_rw(struct ng_queue * ngq, item_p  item, int rw)
1637{
1638	item->el_next = NULL;	/* maybe not needed */
1639	*ngq->last = item;
1640	/*
1641	 * If it was the first item in the queue then we need to
1642	 * set the last pointer and the type flags.
1643	 */
1644	if (ngq->last == &(ngq->queue)) {
1645		/*
1646		 * When called with constants for rw, the optimiser will
1647		 * remove the unneeded branch below.
1648		 */
1649		if (rw == NGQRW_W) {
1650			atomic_add_long(&ngq->q_flags, WRITE_PENDING);
1651		} else {
1652			atomic_add_long(&ngq->q_flags, READ_PENDING);
1653		}
1654	}
1655	ngq->last = &(item->el_next);
1656}
1657
1658
1659/*
1660 * This function 'cheats' in that it first tries to 'grab' the use of the
1661 * node, without going through the mutex. We can do this becasue of the
1662 * semantics of the lock. The semantics include a clause that says that the
1663 * value of the readers count is invalid if the WRITER_ACTIVE flag is set. It
1664 * also says that the WRITER_ACTIVE flag cannot be set if the readers count
1665 * is not zero. Note that this talks about what is valid to SET the
1666 * WRITER_ACTIVE flag, because from the moment it is set, the value if the
1667 * reader count is immaterial, and not valid. The two 'pending' flags have a
1668 * similar effect, in that If they are orthogonal to the two active fields in
1669 * how they are set, but if either is set, the attempted 'grab' need to be
1670 * backed out because there is earlier work, and we maintain ordering in the
1671 * queue. The result of this is that the reader request can try obtain use of
1672 * the node with only a single atomic addition, and without any of the mutex
1673 * overhead. If this fails the operation degenerates to the same as for other
1674 * cases.
1675 *
1676 */
1677static __inline item_p
1678ng_acquire_read(struct ng_queue *ngq, item_p item)
1679{
1680
1681	/* ######### Hack alert ######### */
1682	atomic_add_long(&ngq->q_flags, READER_INCREMENT);
1683	if ((ngq->q_flags & (~READER_MASK)) == 0) {
1684		/* Successfully grabbed node */
1685		return (item);
1686	}
1687	/* undo the damage if we didn't succeed */
1688	atomic_subtract_long(&ngq->q_flags, READER_INCREMENT);
1689
1690	/* ######### End Hack alert ######### */
1691	mtx_enter((&ngq->q_mtx), MTX_SPIN);
1692	/*
1693	 * Try again. Another processor (or interrupt for that matter) may
1694	 * have removed the last queued item that was stopping us from
1695	 * running, between the previous test, and the moment that we took
1696	 * the mutex. (Or maybe a writer completed.)
1697	 */
1698	if ((ngq->q_flags & (~READER_MASK)) == 0) {
1699		atomic_add_long(&ngq->q_flags, READER_INCREMENT);
1700		mtx_exit((&ngq->q_mtx), MTX_SPIN);
1701		return (item);
1702	}
1703
1704	/*
1705	 * Quick check that we are doing things right.
1706	 */
1707	if (ngq->q_flags & SINGLE_THREAD_ONLY) {
1708		panic("Calling single treaded queue incorrectly");
1709	}
1710
1711	/*
1712	 * and queue the request for later.
1713	 */
1714	item->el_flags |= NGQF_TYPE;
1715	ng_queue_rw(ngq, item, NGQRW_R);
1716
1717	/*
1718	 * Ok, so that's the item successfully queued for later. So now we
1719	 * see if we can dequeue something to run instead.
1720	 */
1721	item = ng_dequeue(ngq);
1722	mtx_exit(&(ngq->q_mtx), MTX_SPIN);
1723	return (item);
1724}
1725
1726static __inline item_p
1727ng_acquire_write(struct ng_queue *ngq, item_p item)
1728{
1729restart:
1730	mtx_enter(&(ngq->q_mtx), MTX_SPIN);
1731	/*
1732	 * If there are no readers, no writer, and no pending packets, then
1733	 * we can just go ahead. In all other situations we need to queue the
1734	 * request
1735	 */
1736	if ((ngq->q_flags & (~SINGLE_THREAD_ONLY)) == 0) {
1737		atomic_add_long(&ngq->q_flags, WRITER_ACTIVE);
1738		mtx_exit((&ngq->q_mtx), MTX_SPIN);
1739		if (ngq->q_flags & READER_MASK) {
1740			/* Collision with fast-track reader */
1741			atomic_add_long(&ngq->q_flags, -WRITER_ACTIVE);
1742			goto restart;
1743		}
1744
1745		return (item);
1746	}
1747
1748	/*
1749	 * and queue the request for later.
1750	 */
1751	item->el_flags &= ~NGQF_TYPE;
1752	ng_queue_rw(ngq, item, NGQRW_W);
1753
1754	/*
1755	 * Ok, so that's the item successfully queued for later. So now we
1756	 * see if we can dequeue something to run instead.
1757	 */
1758	item = ng_dequeue(ngq);
1759	mtx_exit(&(ngq->q_mtx), MTX_SPIN);
1760	return (item);
1761}
1762
1763static __inline void
1764ng_leave_read(struct ng_queue *ngq)
1765{
1766	atomic_subtract_long(&ngq->q_flags, READER_INCREMENT);
1767}
1768
1769static __inline void
1770ng_leave_write(struct ng_queue *ngq)
1771{
1772	atomic_subtract_long(&ngq->q_flags, WRITER_ACTIVE);
1773}
1774
1775static void
1776ng_flush_input_queue(struct ng_queue * ngq)
1777{
1778	item_p item;
1779	u_int		add_arg;
1780	mtx_enter(&ngq->q_mtx, MTX_SPIN);
1781	for (;;) {
1782		/* Now take a look at what's on the queue */
1783		if (ngq->q_flags & READ_PENDING) {
1784			add_arg = -READ_PENDING;
1785		} else if (ngq->q_flags & WRITE_PENDING) {
1786			add_arg = -WRITE_PENDING;
1787		} else {
1788			break;
1789		}
1790
1791		item = ngq->queue;
1792		ngq->queue = item->el_next;
1793		if (ngq->last == &(item->el_next)) {
1794			ngq->last = &(ngq->queue);
1795		} else {
1796			if ((ngq->queue->el_flags & NGQF_TYPE) == NGQF_READER) {
1797				add_arg += READ_PENDING;
1798			} else {
1799				add_arg += WRITE_PENDING;
1800			}
1801		}
1802		atomic_add_long(&ngq->q_flags, add_arg);
1803
1804		mtx_exit(&ngq->q_mtx, MTX_SPIN);
1805		NG_FREE_ITEM(item);
1806		mtx_enter(&ngq->q_mtx, MTX_SPIN);
1807	}
1808	mtx_exit(&ngq->q_mtx, MTX_SPIN);
1809}
1810
1811/***********************************************************************
1812* Externally visible method for sending or queueing messages or data.
1813***********************************************************************/
1814
1815/*
1816 * MACRO WILL DO THE JOB OF CALLING ng_package_msg IN CALLER
1817 * before we are called. The user code should have filled out the item
1818 * correctly by this stage:
1819 * Common:
1820 *    reference to destination node.
1821 *    Reference to destination rcv hook if relevant.
1822 * Data:
1823 *    pointer to mbuf
1824 *    pointer to metadata
1825 * Control_Message:
1826 *    pointer to msg.
1827 *    ID of original sender node. (return address)
1828 *
1829 * The nodes have several routines and macros to help with this task:
1830 * ng_package_msg()
1831 * ng_package_data() do much of the work.
1832 * ng_retarget_msg
1833 * ng_retarget_data
1834 */
1835
1836int
1837ng_snd_item(item_p item, int queue)
1838{
1839	hook_p hook = item->el_hook;
1840	node_p dest = item->el_dest;
1841	int rw;
1842	int error = 0, ierror;
1843	item_p	oitem;
1844	struct ng_queue * ngq = &dest->nd_input_queue;
1845
1846#ifdef	NETGRAPH_DEBUG
1847        _ngi_check(item, __FILE__, __LINE__);
1848#endif
1849
1850	if (item == NULL) {
1851		TRAP_ERROR;
1852		return (EINVAL);	/* failed to get queue element */
1853	}
1854	if (dest == NULL) {
1855		NG_FREE_ITEM(item);
1856		TRAP_ERROR;
1857		return (EINVAL);	/* No address */
1858	}
1859	if ((item->el_flags & NGQF_D_M) == NGQF_DATA) {
1860		/*
1861		 * DATA MESSAGE
1862		 * Delivered to a node via a non-optional hook.
1863		 * Both should be present in the item even though
1864		 * the node is derivable from the hook.
1865		 * References are held on both by the item.
1866		 */
1867#ifdef	NETGRAPH_DEBUG
1868        _ngi_check(item, __FILE__, __LINE__);
1869#endif
1870		CHECK_DATA_MBUF(NGI_M(item));
1871		if (hook == NULL) {
1872			NG_FREE_ITEM(item);
1873			TRAP_ERROR;
1874			return(EINVAL);
1875		}
1876		if ((NG_HOOK_NOT_VALID(hook))
1877		|| (NG_NODE_NOT_VALID(NG_HOOK_NODE(hook)))) {
1878			NG_FREE_ITEM(item);
1879			return (ENOTCONN);
1880		}
1881		if ((hook->hk_flags & HK_QUEUE)) {
1882			queue = 1;
1883		}
1884		/* By default data is a reader in the locking scheme */
1885		item->el_flags |= NGQF_READER;
1886		rw = NGQRW_R;
1887	} else {
1888		/*
1889		 * CONTROL MESSAGE
1890		 * Delivered to a node.
1891		 * Hook is optional.
1892		 * References are held by the item on the node and
1893		 * the hook if it is present.
1894		 */
1895		if (hook && (hook->hk_flags & HK_QUEUE)) {
1896			queue = 1;
1897		}
1898		/* Data messages count as writers unles explicitly exempted */
1899		if (NGI_MSG(item)->header.cmd & NGM_READONLY) {
1900			item->el_flags |= NGQF_READER;
1901			rw = NGQRW_R;
1902		} else {
1903			item->el_flags &= ~NGQF_TYPE;
1904			rw = NGQRW_W;
1905		}
1906	}
1907	/*
1908	 * If the node specifies single threading, force writer semantics
1909	 * Similarly the node may say one hook always produces writers.
1910	 * These are over-rides.
1911	 */
1912	if ((ngq->q_flags & SINGLE_THREAD_ONLY)
1913	|| (dest->nd_flags & NG_FORCE_WRITER)
1914	|| (hook && (hook->hk_flags & HK_FORCE_WRITER))) {
1915			rw = NGQRW_W;
1916			item->el_flags &= ~NGQF_TYPE;
1917	}
1918	if (queue) {
1919		/* Put it on the queue for that node*/
1920#ifdef	NETGRAPH_DEBUG
1921        _ngi_check(item, __FILE__, __LINE__);
1922#endif
1923		mtx_enter(&(ngq->q_mtx), MTX_SPIN);
1924		ng_queue_rw(ngq, item, rw);
1925		mtx_exit(&(ngq->q_mtx), MTX_SPIN);
1926		/*
1927		 * If there are active elements then we can rely on
1928		 * them. if not we should not rely on another packet
1929		 * coming here by another path,
1930		 * so it is best to put us in the netisr list.
1931		 */
1932		if ((ngq->q_flags & (READER_MASK|WRITER_ACTIVE)) == 0) {
1933			ng_setisr(ngq->q_node);
1934		}
1935		return (0);
1936	}
1937	/*
1938	 * Take a queue item and a node and see if we can apply the item to
1939	 * the node. We may end up getting a different item to apply instead.
1940	 * Will allow for a piggyback reply only in the case where
1941	 * there is no queueing.
1942	 */
1943
1944	oitem = item;
1945	/*
1946	 * We already decided how we will be queueud or treated.
1947	 * Try get the appropriate operating permission.
1948	 */
1949 	if (rw == NGQRW_R) {
1950		item = ng_acquire_read(ngq, item);
1951	} else {
1952		item = ng_acquire_write(ngq, item);
1953	}
1954
1955	/*
1956	 * May have come back with a different item.
1957	 * or maybe none at all. The one we started with will
1958	 * have been queued in thises cases.
1959	 */
1960	if (item == NULL) {
1961		return (0);
1962	}
1963
1964#ifdef	NETGRAPH_DEBUG
1965        _ngi_check(item, __FILE__, __LINE__);
1966#endif
1967	ierror = ng_apply_item(dest, item); /* drops r/w lock when done */
1968
1969	/* only return an error if it was our initial item.. (compat hack) */
1970	if (oitem == item) {
1971		error = ierror;
1972	}
1973
1974	/*
1975	 * Now we've handled the packet we brought, (or a friend of it) let's
1976	 * look for any other packets that may have been queued up. We hold
1977	 * no locks, so if someone puts something in the queue after
1978	 * we check that it is empty, it is their problem
1979	 * to ensure it is processed. If we have the netisr thread cme in here
1980	 * while we still say we have stuff to do, we may get a boost
1981	 * in SMP systems. :-)
1982	 */
1983	for (;;) {
1984		/* quick hack to save all that mutex stuff */
1985		if ((ngq->q_flags & (WRITE_PENDING | READ_PENDING)) == 0) {
1986			if (dest->nd_flags & NG_WORKQ)
1987				ng_worklist_remove(dest);
1988			return (0);
1989		}
1990		/*
1991		 * dequeue acquires and adjusts the input_queue as it dequeues
1992		 * packets. It acquires the rw lock as needed.
1993		 */
1994		mtx_enter(&ngq->q_mtx, MTX_SPIN);
1995		item = ng_dequeue(ngq);
1996		mtx_exit(&ngq->q_mtx, MTX_SPIN);
1997		if (!item) {
1998			/*
1999			 * If we have no work to do
2000			 * then we certainly don't need to be
2001			 * on the worklist.
2002			 */
2003			if (dest->nd_flags & NG_WORKQ)
2004				ng_worklist_remove(dest);
2005			return (0);
2006		}
2007#ifdef	NETGRAPH_DEBUG
2008        _ngi_check(item, __FILE__, __LINE__);
2009#endif
2010
2011		/*
2012		 * We have the appropriate lock, so run the item.
2013		 * When finished it will drop the lock accordingly
2014		 */
2015
2016		ierror = ng_apply_item(dest, item);
2017		/*
2018		 * only return an error if it was our initial
2019		 * item.. (compat hack)
2020		 */
2021		if (oitem == item) {
2022			error = ierror;
2023		}
2024	}
2025	return (0);
2026}
2027
2028/*
2029 * We have an item that was possibly queued somewhere.
2030 * It should contain all the information needed
2031 * to run it on the appropriate node/hook.
2032 */
2033static int
2034ng_apply_item(node_p node, item_p item)
2035{
2036	hook_p  hook;
2037	int was_reader = ((item->el_flags & NGQF_TYPE));
2038	int error = 0;
2039	ng_rcvdata_t *rcvdata;
2040
2041	hook = item->el_hook;
2042	item->el_hook = NULL;	/* so NG_FREE_ITEM doesn't NG_HOOK_UNREF() */
2043	/* We already have the node.. assume responsibility */
2044	/* And the reference */
2045	/* node = item->el_dest; */
2046	item->el_dest = NULL;	/* same as for the hook above */
2047#ifdef	NETGRAPH_DEBUG
2048        _ngi_check(item, __FILE__, __LINE__);
2049#endif
2050
2051	switch (item->el_flags & NGQF_D_M) {
2052	case NGQF_DATA:
2053		/*
2054		 * Check things are still ok as when we were queued.
2055		 */
2056
2057		if ((hook == NULL)
2058		|| NG_HOOK_NOT_VALID(hook)
2059		|| NG_NODE_NOT_VALID(NG_HOOK_NODE(hook))
2060		|| ((rcvdata = NG_HOOK_NODE(hook)->nd_type->rcvdata) == NULL)) {
2061			error = EIO;
2062			NG_FREE_ITEM(item);
2063		} else {
2064			error = (*rcvdata)(hook, item);
2065		}
2066		break;
2067	case NGQF_MESG:
2068
2069		if (hook) {
2070			item->el_hook = NULL;
2071			if (NG_HOOK_NOT_VALID(hook)) {
2072			/*
2073			 * If the hook has been zapped then we can't use it.
2074			 * Immediatly drop its reference.
2075			 * The message may not need it.
2076			 */
2077				NG_HOOK_UNREF(hook);
2078				hook = NULL;
2079			}
2080		}
2081		/*
2082		 * Similarly, if the node is a zombie there is
2083		 * nothing we can do with it, drop everything.
2084		 */
2085		if (NG_NODE_NOT_VALID(node)) {
2086			TRAP_ERROR;
2087			error = EINVAL;
2088			NG_FREE_ITEM(item);
2089		} else {
2090			/*
2091			 * Call the appropriate message handler for the object.
2092			 * It is up to the message handler to free the message.
2093			 * If it's a generic message, handle it generically,
2094			 * otherwise call the type's message handler
2095			 * (if it exists)
2096			 * XXX (race). Remember that a queued message may
2097			 * reference a node or hook that has just been
2098			 * invalidated. It will exist as the queue code
2099			 * is holding a reference, but..
2100			 */
2101
2102			struct ng_mesg *msg = NGI_MSG(item);
2103
2104			if ((msg->header.typecookie == NGM_GENERIC_COOKIE)
2105			&& ((msg->header.flags & NGF_RESP) == 0)) {
2106				error = ng_generic_msg(node, item, hook);
2107			} else {
2108				if ((node)->nd_type->rcvmsg != NULL) {
2109					error = (*(node)->nd_type->rcvmsg)((node),
2110						(item), (hook));
2111				} else {
2112					TRAP_ERROR;
2113					error = EINVAL; /* XXX */
2114					NG_FREE_ITEM(item);
2115				}
2116			}
2117			/* item is now invalid */
2118		}
2119		break;
2120	}
2121	/*
2122	 * We held references on some of the resources
2123	 * that we took from the item. Now that we have
2124	 * finished doing everything, drop those references.
2125	 */
2126	if (hook) {
2127		NG_HOOK_UNREF(hook);
2128	}
2129
2130	if (was_reader) {
2131		ng_leave_read(&node->nd_input_queue);
2132	} else {
2133		ng_leave_write(&node->nd_input_queue);
2134	}
2135	NG_NODE_UNREF(node);
2136	return (error);
2137}
2138
2139/***********************************************************************
2140 * Implement the 'generic' control messages
2141 ***********************************************************************/
2142static int
2143ng_generic_msg(node_p here, item_p item, hook_p lasthook)
2144{
2145	int error = 0;
2146	struct ng_mesg *msg;
2147	struct ng_mesg *resp = NULL;
2148
2149	NGI_GET_MSG(item, msg);
2150	if (msg->header.typecookie != NGM_GENERIC_COOKIE) {
2151		TRAP_ERROR
2152		error = EINVAL;
2153		goto out;
2154	}
2155	switch (msg->header.cmd) {
2156	case NGM_SHUTDOWN:
2157		ng_rmnode(here);
2158		break;
2159	case NGM_MKPEER:
2160	    {
2161		struct ngm_mkpeer *const mkp = (struct ngm_mkpeer *) msg->data;
2162
2163		if (msg->header.arglen != sizeof(*mkp)) {
2164			TRAP_ERROR
2165			error = EINVAL;
2166			break;
2167		}
2168		mkp->type[sizeof(mkp->type) - 1] = '\0';
2169		mkp->ourhook[sizeof(mkp->ourhook) - 1] = '\0';
2170		mkp->peerhook[sizeof(mkp->peerhook) - 1] = '\0';
2171		error = ng_mkpeer(here, mkp->ourhook, mkp->peerhook, mkp->type);
2172		break;
2173	    }
2174	case NGM_CONNECT:
2175	    {
2176		struct ngm_connect *const con =
2177			(struct ngm_connect *) msg->data;
2178		node_p node2;
2179
2180		if (msg->header.arglen != sizeof(*con)) {
2181			TRAP_ERROR
2182			error = EINVAL;
2183			break;
2184		}
2185		con->path[sizeof(con->path) - 1] = '\0';
2186		con->ourhook[sizeof(con->ourhook) - 1] = '\0';
2187		con->peerhook[sizeof(con->peerhook) - 1] = '\0';
2188		/* Don't forget we get a reference.. */
2189		error = ng_path2noderef(here, con->path, &node2, NULL);
2190		if (error)
2191			break;
2192		error = ng_con_nodes(here, con->ourhook, node2, con->peerhook);
2193		NG_NODE_UNREF(node2);
2194		break;
2195	    }
2196	case NGM_NAME:
2197	    {
2198		struct ngm_name *const nam = (struct ngm_name *) msg->data;
2199
2200		if (msg->header.arglen != sizeof(*nam)) {
2201			TRAP_ERROR
2202			error = EINVAL;
2203			break;
2204		}
2205		nam->name[sizeof(nam->name) - 1] = '\0';
2206		error = ng_name_node(here, nam->name);
2207		break;
2208	    }
2209	case NGM_RMHOOK:
2210	    {
2211		struct ngm_rmhook *const rmh = (struct ngm_rmhook *) msg->data;
2212		hook_p hook;
2213
2214		if (msg->header.arglen != sizeof(*rmh)) {
2215			TRAP_ERROR
2216			error = EINVAL;
2217			break;
2218		}
2219		rmh->ourhook[sizeof(rmh->ourhook) - 1] = '\0';
2220		if ((hook = ng_findhook(here, rmh->ourhook)) != NULL)
2221			ng_destroy_hook(hook);
2222		break;
2223	    }
2224	case NGM_NODEINFO:
2225	    {
2226		struct nodeinfo *ni;
2227
2228		NG_MKRESPONSE(resp, msg, sizeof(*ni), M_NOWAIT);
2229		if (resp == NULL) {
2230			error = ENOMEM;
2231			break;
2232		}
2233
2234		/* Fill in node info */
2235		ni = (struct nodeinfo *) resp->data;
2236		if (NG_NODE_HAS_NAME(here))
2237			strncpy(ni->name, NG_NODE_NAME(here), NG_NODELEN);
2238		strncpy(ni->type, here->nd_type->name, NG_TYPELEN);
2239		ni->id = ng_node2ID(here);
2240		ni->hooks = here->nd_numhooks;
2241		break;
2242	    }
2243	case NGM_LISTHOOKS:
2244	    {
2245		const int nhooks = here->nd_numhooks;
2246		struct hooklist *hl;
2247		struct nodeinfo *ni;
2248		hook_p hook;
2249
2250		/* Get response struct */
2251		NG_MKRESPONSE(resp, msg, sizeof(*hl)
2252		    + (nhooks * sizeof(struct linkinfo)), M_NOWAIT);
2253		if (resp == NULL) {
2254			error = ENOMEM;
2255			break;
2256		}
2257		hl = (struct hooklist *) resp->data;
2258		ni = &hl->nodeinfo;
2259
2260		/* Fill in node info */
2261		if (NG_NODE_HAS_NAME(here))
2262			strncpy(ni->name, NG_NODE_NAME(here), NG_NODELEN);
2263		strncpy(ni->type, here->nd_type->name, NG_TYPELEN);
2264		ni->id = ng_node2ID(here);
2265
2266		/* Cycle through the linked list of hooks */
2267		ni->hooks = 0;
2268		LIST_FOREACH(hook, &here->nd_hooks, hk_hooks) {
2269			struct linkinfo *const link = &hl->link[ni->hooks];
2270
2271			if (ni->hooks >= nhooks) {
2272				log(LOG_ERR, "%s: number of %s changed\n",
2273				    __FUNCTION__, "hooks");
2274				break;
2275			}
2276			if (NG_HOOK_NOT_VALID(hook))
2277				continue;
2278			strncpy(link->ourhook, NG_HOOK_NAME(hook), NG_HOOKLEN);
2279			strncpy(link->peerhook,
2280				NG_PEER_HOOK_NAME(hook), NG_HOOKLEN);
2281			if (NG_PEER_NODE_NAME(hook)[0] != '\0')
2282				strncpy(link->nodeinfo.name,
2283				    NG_PEER_NODE_NAME(hook), NG_NODELEN);
2284			strncpy(link->nodeinfo.type,
2285			   NG_PEER_NODE(hook)->nd_type->name, NG_TYPELEN);
2286			link->nodeinfo.id = ng_node2ID(NG_PEER_NODE(hook));
2287			link->nodeinfo.hooks = NG_PEER_NODE(hook)->nd_numhooks;
2288			ni->hooks++;
2289		}
2290		break;
2291	    }
2292
2293	case NGM_LISTNAMES:
2294	case NGM_LISTNODES:
2295	    {
2296		const int unnamed = (msg->header.cmd == NGM_LISTNODES);
2297		struct namelist *nl;
2298		node_p node;
2299		int num = 0;
2300
2301		mtx_enter(&ng_nodelist_mtx, MTX_DEF);
2302		/* Count number of nodes */
2303		LIST_FOREACH(node, &ng_nodelist, nd_nodes) {
2304			if (unnamed || NG_NODE_HAS_NAME(node))
2305				num++;
2306		}
2307		mtx_exit(&ng_nodelist_mtx, MTX_DEF);
2308
2309		/* Get response struct */
2310		NG_MKRESPONSE(resp, msg, sizeof(*nl)
2311		    + (num * sizeof(struct nodeinfo)), M_NOWAIT);
2312		if (resp == NULL) {
2313			error = ENOMEM;
2314			break;
2315		}
2316		nl = (struct namelist *) resp->data;
2317
2318		/* Cycle through the linked list of nodes */
2319		nl->numnames = 0;
2320		mtx_enter(&ng_nodelist_mtx, MTX_DEF);
2321		LIST_FOREACH(node, &ng_nodelist, nd_nodes) {
2322			struct nodeinfo *const np = &nl->nodeinfo[nl->numnames];
2323
2324			if (nl->numnames >= num) {
2325				log(LOG_ERR, "%s: number of %s changed\n",
2326				    __FUNCTION__, "nodes");
2327				break;
2328			}
2329			if (NG_NODE_NOT_VALID(node))
2330				continue;
2331			if (!unnamed && (! NG_NODE_HAS_NAME(node)))
2332				continue;
2333			if (NG_NODE_HAS_NAME(node))
2334				strncpy(np->name, NG_NODE_NAME(node), NG_NODELEN);
2335			strncpy(np->type, node->nd_type->name, NG_TYPELEN);
2336			np->id = ng_node2ID(node);
2337			np->hooks = node->nd_numhooks;
2338			nl->numnames++;
2339		}
2340		mtx_exit(&ng_nodelist_mtx, MTX_DEF);
2341		break;
2342	    }
2343
2344	case NGM_LISTTYPES:
2345	    {
2346		struct typelist *tl;
2347		struct ng_type *type;
2348		int num = 0;
2349
2350		mtx_enter(&ng_typelist_mtx, MTX_DEF);
2351		/* Count number of types */
2352		LIST_FOREACH(type, &ng_typelist, types)
2353			num++;
2354		mtx_exit(&ng_typelist_mtx, MTX_DEF);
2355
2356		/* Get response struct */
2357		NG_MKRESPONSE(resp, msg, sizeof(*tl)
2358		    + (num * sizeof(struct typeinfo)), M_NOWAIT);
2359		if (resp == NULL) {
2360			error = ENOMEM;
2361			break;
2362		}
2363		tl = (struct typelist *) resp->data;
2364
2365		/* Cycle through the linked list of types */
2366		tl->numtypes = 0;
2367		mtx_enter(&ng_typelist_mtx, MTX_DEF);
2368		LIST_FOREACH(type, &ng_typelist, types) {
2369			struct typeinfo *const tp = &tl->typeinfo[tl->numtypes];
2370
2371			if (tl->numtypes >= num) {
2372				log(LOG_ERR, "%s: number of %s changed\n",
2373				    __FUNCTION__, "types");
2374				break;
2375			}
2376			strncpy(tp->type_name, type->name, NG_TYPELEN);
2377			tp->numnodes = type->refs;
2378			tl->numtypes++;
2379		}
2380		mtx_exit(&ng_typelist_mtx, MTX_DEF);
2381		break;
2382	    }
2383
2384	case NGM_BINARY2ASCII:
2385	    {
2386		int bufSize = 20 * 1024;	/* XXX hard coded constant */
2387		const struct ng_parse_type *argstype;
2388		const struct ng_cmdlist *c;
2389		struct ng_mesg *binary, *ascii;
2390
2391		/* Data area must contain a valid netgraph message */
2392		binary = (struct ng_mesg *)msg->data;
2393		if (msg->header.arglen < sizeof(struct ng_mesg)
2394		    || msg->header.arglen - sizeof(struct ng_mesg)
2395		      < binary->header.arglen) {
2396			TRAP_ERROR
2397			error = EINVAL;
2398			break;
2399		}
2400
2401		/* Get a response message with lots of room */
2402		NG_MKRESPONSE(resp, msg, sizeof(*ascii) + bufSize, M_NOWAIT);
2403		if (resp == NULL) {
2404			error = ENOMEM;
2405			break;
2406		}
2407		ascii = (struct ng_mesg *)resp->data;
2408
2409		/* Copy binary message header to response message payload */
2410		bcopy(binary, ascii, sizeof(*binary));
2411
2412		/* Find command by matching typecookie and command number */
2413		for (c = here->nd_type->cmdlist;
2414		    c != NULL && c->name != NULL; c++) {
2415			if (binary->header.typecookie == c->cookie
2416			    && binary->header.cmd == c->cmd)
2417				break;
2418		}
2419		if (c == NULL || c->name == NULL) {
2420			for (c = ng_generic_cmds; c->name != NULL; c++) {
2421				if (binary->header.typecookie == c->cookie
2422				    && binary->header.cmd == c->cmd)
2423					break;
2424			}
2425			if (c->name == NULL) {
2426				NG_FREE_MSG(resp);
2427				error = ENOSYS;
2428				break;
2429			}
2430		}
2431
2432		/* Convert command name to ASCII */
2433		snprintf(ascii->header.cmdstr, sizeof(ascii->header.cmdstr),
2434		    "%s", c->name);
2435
2436		/* Convert command arguments to ASCII */
2437		argstype = (binary->header.flags & NGF_RESP) ?
2438		    c->respType : c->mesgType;
2439		if (argstype == NULL)
2440			*ascii->data = '\0';
2441		else {
2442			if ((error = ng_unparse(argstype,
2443			    (u_char *)binary->data,
2444			    ascii->data, bufSize)) != 0) {
2445				NG_FREE_MSG(resp);
2446				break;
2447			}
2448		}
2449
2450		/* Return the result as struct ng_mesg plus ASCII string */
2451		bufSize = strlen(ascii->data) + 1;
2452		ascii->header.arglen = bufSize;
2453		resp->header.arglen = sizeof(*ascii) + bufSize;
2454		break;
2455	    }
2456
2457	case NGM_ASCII2BINARY:
2458	    {
2459		int bufSize = 2000;	/* XXX hard coded constant */
2460		const struct ng_cmdlist *c;
2461		const struct ng_parse_type *argstype;
2462		struct ng_mesg *ascii, *binary;
2463		int off = 0;
2464
2465		/* Data area must contain at least a struct ng_mesg + '\0' */
2466		ascii = (struct ng_mesg *)msg->data;
2467		if (msg->header.arglen < sizeof(*ascii) + 1
2468		    || ascii->header.arglen < 1
2469		    || msg->header.arglen
2470		      < sizeof(*ascii) + ascii->header.arglen) {
2471			TRAP_ERROR
2472			error = EINVAL;
2473			break;
2474		}
2475		ascii->data[ascii->header.arglen - 1] = '\0';
2476
2477		/* Get a response message with lots of room */
2478		NG_MKRESPONSE(resp, msg, sizeof(*binary) + bufSize, M_NOWAIT);
2479		if (resp == NULL) {
2480			error = ENOMEM;
2481			break;
2482		}
2483		binary = (struct ng_mesg *)resp->data;
2484
2485		/* Copy ASCII message header to response message payload */
2486		bcopy(ascii, binary, sizeof(*ascii));
2487
2488		/* Find command by matching ASCII command string */
2489		for (c = here->nd_type->cmdlist;
2490		    c != NULL && c->name != NULL; c++) {
2491			if (strcmp(ascii->header.cmdstr, c->name) == 0)
2492				break;
2493		}
2494		if (c == NULL || c->name == NULL) {
2495			for (c = ng_generic_cmds; c->name != NULL; c++) {
2496				if (strcmp(ascii->header.cmdstr, c->name) == 0)
2497					break;
2498			}
2499			if (c->name == NULL) {
2500				NG_FREE_MSG(resp);
2501				error = ENOSYS;
2502				break;
2503			}
2504		}
2505
2506		/* Convert command name to binary */
2507		binary->header.cmd = c->cmd;
2508		binary->header.typecookie = c->cookie;
2509
2510		/* Convert command arguments to binary */
2511		argstype = (binary->header.flags & NGF_RESP) ?
2512		    c->respType : c->mesgType;
2513		if (argstype == NULL)
2514			bufSize = 0;
2515		else {
2516			if ((error = ng_parse(argstype, ascii->data,
2517			    &off, (u_char *)binary->data, &bufSize)) != 0) {
2518				NG_FREE_MSG(resp);
2519				break;
2520			}
2521		}
2522
2523		/* Return the result */
2524		binary->header.arglen = bufSize;
2525		resp->header.arglen = sizeof(*binary) + bufSize;
2526		break;
2527	    }
2528
2529	case NGM_TEXT_CONFIG:
2530	case NGM_TEXT_STATUS:
2531		/*
2532		 * This one is tricky as it passes the command down to the
2533		 * actual node, even though it is a generic type command.
2534		 * This means we must assume that the item/msg is already freed
2535		 * when control passes back to us.
2536		 */
2537		if (here->nd_type->rcvmsg != NULL) {
2538			NGI_MSG(item) = msg; /* put it back as we found it */
2539			return((*here->nd_type->rcvmsg)(here, item, lasthook));
2540		}
2541		/* Fall through if rcvmsg not supported */
2542	default:
2543		TRAP_ERROR;
2544		error = EINVAL;
2545	}
2546	/*
2547	 * Sometimes a generic message may be statically allocated
2548	 * to avoid problems with allocating when in tight memeory situations.
2549	 * Don't free it if it is so.
2550	 * I break them appart here, because erros may cause a free if the item
2551	 * in which case we'd be doing it twice.
2552	 * they are kept together above, to simplify freeing.
2553	 */
2554out:
2555	NG_RESPOND_MSG(error, here, item, resp);
2556	if ( msg && ((msg->header.flags & NGF_STATIC) == 0))
2557		NG_FREE_MSG(msg);
2558	return (error);
2559}
2560
2561/*
2562 * Copy a 'meta'.
2563 *
2564 * Returns new meta, or NULL if original meta is NULL or ENOMEM.
2565 */
2566meta_p
2567ng_copy_meta(meta_p meta)
2568{
2569	meta_p meta2;
2570
2571	if (meta == NULL)
2572		return (NULL);
2573	MALLOC(meta2, meta_p, meta->used_len, M_NETGRAPH_META, M_NOWAIT);
2574	if (meta2 == NULL)
2575		return (NULL);
2576	meta2->allocated_len = meta->used_len;
2577	bcopy(meta, meta2, meta->used_len);
2578	return (meta2);
2579}
2580
2581/************************************************************************
2582			Module routines
2583************************************************************************/
2584
2585/*
2586 * Handle the loading/unloading of a netgraph node type module
2587 */
2588int
2589ng_mod_event(module_t mod, int event, void *data)
2590{
2591	struct ng_type *const type = data;
2592	int s, error = 0;
2593
2594	switch (event) {
2595	case MOD_LOAD:
2596
2597		/* Register new netgraph node type */
2598		s = splnet();
2599		if ((error = ng_newtype(type)) != 0) {
2600			splx(s);
2601			break;
2602		}
2603
2604		/* Call type specific code */
2605		if (type->mod_event != NULL)
2606			if ((error = (*type->mod_event)(mod, event, data))) {
2607				mtx_enter(&ng_typelist_mtx, MTX_DEF);
2608				LIST_REMOVE(type, types);
2609				mtx_exit(&ng_typelist_mtx, MTX_DEF);
2610			}
2611		splx(s);
2612		break;
2613
2614	case MOD_UNLOAD:
2615		s = splnet();
2616		if (type->refs != 0)		/* make sure no nodes exist! */
2617			error = EBUSY;
2618		else {
2619			if (type->mod_event != NULL) {	/* check with type */
2620				error = (*type->mod_event)(mod, event, data);
2621				if (error != 0) {	/* type refuses.. */
2622					splx(s);
2623					break;
2624				}
2625			}
2626			mtx_enter(&ng_typelist_mtx, MTX_DEF);
2627			LIST_REMOVE(type, types);
2628			mtx_exit(&ng_typelist_mtx, MTX_DEF);
2629		}
2630		splx(s);
2631		break;
2632
2633	default:
2634		if (type->mod_event != NULL)
2635			error = (*type->mod_event)(mod, event, data);
2636		else
2637			error = 0;		/* XXX ? */
2638		break;
2639	}
2640	return (error);
2641}
2642
2643/*
2644 * Handle loading and unloading for this code.
2645 * The only thing we need to link into is the NETISR strucure.
2646 */
2647static int
2648ngb_mod_event(module_t mod, int event, void *data)
2649{
2650	int s, error = 0;
2651
2652	switch (event) {
2653	case MOD_LOAD:
2654		/* Register line discipline */
2655		mtx_init(&ng_worklist_mtx, "netgraph worklist mutex", 0);
2656		mtx_init(&ng_typelist_mtx, "netgraph types mutex", 0);
2657		mtx_init(&ng_nodelist_mtx, "netgraph nodelist mutex", 0);
2658		mtx_init(&ng_idhash_mtx, "netgraph idhash mutex", 0);
2659		mtx_init(&ngq_mtx, "netgraph netisr mutex", 0);
2660		s = splimp();
2661		error = register_netisr(NETISR_NETGRAPH, ngintr);
2662		splx(s);
2663		break;
2664	case MOD_UNLOAD:
2665		/* You cant unload it because an interface may be using it.  */
2666		error = EBUSY;
2667		break;
2668	default:
2669		error = EOPNOTSUPP;
2670		break;
2671	}
2672	return (error);
2673}
2674
2675static moduledata_t netgraph_mod = {
2676	"netgraph",
2677	ngb_mod_event,
2678	(NULL)
2679};
2680DECLARE_MODULE(netgraph, netgraph_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
2681
2682/************************************************************************
2683			Queue element get/free routines
2684************************************************************************/
2685
2686
2687static int			allocated;	/* number of items malloc'd */
2688static int			maxalloc = 128;	/* limit the damage of a leak */
2689static const int		ngqfreemax = 64;/* cache at most this many */
2690static const int		ngqfreelow = 4; /* try malloc if free < this */
2691static volatile int		ngqfreesize;	/* number of cached entries */
2692#ifdef	NETGRAPH_DEBUG
2693static TAILQ_HEAD(, ng_item) ng_itemlist = TAILQ_HEAD_INITIALIZER(ng_itemlist);
2694#endif
2695/*
2696 * Get a queue entry
2697 * This is usually called when a packet first enters netgraph.
2698 * By definition, this is usually from an interrupt, or from a user.
2699 * Users are not so important, but try be quick for the times that it's
2700 * an interrupt. Use atomic operations to cope with collisions
2701 * with interrupts and other processors. Assumes MALLOC is SMP safe.
2702 * XXX If reserve is low, we should try to get 2 from malloc as this
2703 * would indicate it often fails.
2704 */
2705static item_p
2706ng_getqblk(void)
2707{
2708	item_p item = NULL;
2709
2710	/*
2711	 * Try get a cached queue block, or else allocate a new one
2712	 * If we are less than our reserve, try malloc. If malloc
2713	 * fails, then that's what the reserve is for...
2714	 * Don't completely trust ngqfreesize, as it is subject
2715	 * to races.. (it'll eventually catch up but may be out by one or two
2716	 * for brief moments(under SMP or interrupts).
2717	 * ngqfree is the final arbiter. We have our little reserve
2718	 * because we use M_NOWAIT for malloc. This just helps us
2719	 * avoid dropping packets while not increasing the time
2720	 * we take to service the interrupt (on average) (we hope).
2721	 */
2722	for (;;) {
2723		if ((ngqfreesize < ngqfreelow) || (ngqfree == NULL)) {
2724			if (allocated < maxalloc) {  /* don't leak forever */
2725				MALLOC(item, item_p ,
2726				    sizeof(*item), M_NETGRAPH_ITEM,
2727				    (M_NOWAIT | M_ZERO));
2728				if (item) {
2729#ifdef	NETGRAPH_DEBUG
2730					TAILQ_INSERT_TAIL(&ng_itemlist,
2731								item, all);
2732#endif	/* NETGRAPH_DEBUG */
2733					atomic_add_int(&allocated, 1);
2734					break;
2735				}
2736			}
2737		}
2738
2739		/*
2740		 * We didn't or couldn't malloc.
2741		 * try get one from our cache.
2742		 * item must be NULL to get here.
2743		 */
2744		if ((item = ngqfree) != NULL) {
2745			/*
2746			 * Atomically try grab the first item
2747			 * and put it's successor in its place.
2748			 * If we fail, just try again.. someone else
2749			 * beat us to this one or freed one.
2750			 * Don't worry about races with ngqfreesize.
2751			 * Close enough is good enough..
2752			 */
2753			if (atomic_cmpset_ptr(&ngqfree, item, item->el_next)) {
2754				atomic_subtract_int(&ngqfreesize, 1);
2755				break;
2756			}
2757			item = NULL;
2758		} else {
2759			/* We really ran out */
2760			break;
2761		}
2762	}
2763	item->el_flags &= ~NGQF_FREE;
2764	return (item);
2765}
2766
2767/*
2768 * Release a queue entry
2769 */
2770void
2771ng_free_item(item_p item)
2772{
2773
2774	/*
2775	 * The item may hold resources on it's own. We need to free
2776	 * these before we can free the item. What they are depends upon
2777	 * what kind of item it is. it is important that nodes zero
2778	 * out pointers to resources that they remove from the item
2779	 * or we release them again here.
2780	 */
2781	if (item->el_flags & NGQF_FREE) {
2782		panic(" Freeing free queue item");
2783	}
2784	switch (item->el_flags & NGQF_D_M) {
2785	case NGQF_DATA:
2786		/* If we have an mbuf and metadata still attached.. */
2787		NG_FREE_M(_NGI_M(item));
2788		NG_FREE_META(_NGI_META(item));
2789		break;
2790	case NGQF_MESG:
2791		_NGI_RETADDR(item) = NULL;
2792		NG_FREE_MSG(_NGI_MSG(item));
2793		break;
2794	}
2795		/* If we still have a node or hook referenced... */
2796	if (item->el_dest) {
2797		NG_NODE_UNREF(item->el_dest);
2798		item->el_dest = NULL;
2799	}
2800	if (item->el_hook) {
2801		NG_HOOK_UNREF(item->el_hook);
2802		item->el_hook = NULL;
2803	}
2804	item->el_flags |= NGQF_FREE;
2805
2806	/*
2807	 * We have freed any resources held by the item.
2808	 * now we can free the item itself.
2809	 */
2810	if (ngqfreesize < ngqfreemax) { /* don't worry about races */
2811		for (;;) {
2812			item->el_next = ngqfree;
2813			if (atomic_cmpset_ptr(&ngqfree, item->el_next, item)) {
2814				break;
2815			}
2816		}
2817		atomic_add_int(&ngqfreesize, 1);
2818	} else {
2819		/* This is the only place that should use this Macro */
2820#ifdef	NETGRAPH_DEBUG
2821		TAILQ_REMOVE(&ng_itemlist, item, all);
2822#endif	/* NETGRAPH_DEBUG */
2823		NG_FREE_ITEM_REAL(item);
2824		atomic_subtract_int(&allocated, 1);
2825	}
2826}
2827
2828#ifdef	NETGRAPH_DEBUG
2829void
2830dumphook (hook_p hook, char *file, int line)
2831{
2832	printf("hook: name %s, %d refs, Last touched:\n",
2833		_NG_HOOK_NAME(hook), hook->hk_refs);
2834	printf("	Last active @ %s, line %d\n",
2835		hook->lastfile, hook->lastline);
2836	if (line) {
2837		printf(" problem discovered at file %s, line %d\n", file, line);
2838	}
2839}
2840
2841void
2842dumpnode(node_p node, char *file, int line)
2843{
2844	printf("node: ID [%x]: type '%s', %d hooks, flags 0x%x, %d refs, %s:\n",
2845		ng_node2ID(node), node->nd_type->name,
2846		node->nd_numhooks, node->nd_flags,
2847		node->nd_refs, node->nd_name);
2848	printf("	Last active @ %s, line %d\n",
2849		node->lastfile, node->lastline);
2850	if (line) {
2851		printf(" problem discovered at file %s, line %d\n", file, line);
2852	}
2853}
2854
2855void
2856dumpitem(item_p item, char *file, int line)
2857{
2858	if (item->el_flags & NGQF_FREE) {
2859		printf(" Free item, freed at %s, line %d\n",
2860			item->lastfile, item->lastline);
2861	} else {
2862		printf(" ACTIVE item, last used at %s, line %d",
2863			item->lastfile, item->lastline);
2864		if ((item->el_flags & NGQF_D_M) == NGQF_MESG) {
2865			printf(" - retaddr[%d]:\n", _NGI_RETADDR(item));
2866		} else {
2867			printf(" - [data]\n");
2868		}
2869	}
2870	if (line) {
2871		printf(" problem discovered at file %s, line %d\n", file, line);
2872		if (item->el_dest) {
2873			printf("node %p ([%x])\n",
2874				item->el_dest, ng_node2ID(item->el_dest));
2875		}
2876	}
2877}
2878
2879static void
2880ng_dumpitems(void)
2881{
2882	item_p item;
2883	int i = 1;
2884	TAILQ_FOREACH(item, &ng_itemlist, all) {
2885		printf("[%d] ", i++);
2886		dumpitem(item, NULL, 0);
2887	}
2888}
2889
2890static void
2891ng_dumpnodes(void)
2892{
2893	node_p node;
2894	int i = 1;
2895	SLIST_FOREACH(node, &ng_allnodes, nd_all) {
2896		printf("[%d] ", i++);
2897		dumpnode(node, NULL, 0);
2898	}
2899}
2900
2901static void
2902ng_dumphooks(void)
2903{
2904	hook_p hook;
2905	int i = 1;
2906	SLIST_FOREACH(hook, &ng_allhooks, hk_all) {
2907		printf("[%d] ", i++);
2908		dumphook(hook, NULL, 0);
2909	}
2910}
2911
2912static int
2913sysctl_debug_ng_dump_items(SYSCTL_HANDLER_ARGS)
2914{
2915	static int count;
2916	int error;
2917	int val;
2918	int i;
2919
2920	val = allocated;
2921	i = 1;
2922	error = sysctl_handle_int(oidp, &val, sizeof(int), req);
2923	if(count++  & 1) { /* for some reason sysctl calls it twice */
2924		ng_dumpitems();
2925		ng_dumpnodes();
2926		ng_dumphooks();
2927	}
2928	return error;
2929}
2930
2931SYSCTL_PROC(_debug, OID_AUTO, ng_dump_items, CTLTYPE_INT | CTLFLAG_RD,
2932    0, 0, sysctl_debug_ng_dump_items, "I", "Number of allocated items");
2933#endif	/* NETGRAPH_DEBUG */
2934
2935
2936/***********************************************************************
2937* Worklist routines
2938**********************************************************************/
2939/* NETISR thread enters here */
2940/*
2941 * Pick a node off the list of nodes with work,
2942 * try get an item to process off it.
2943 * If there are no more, remove the node from the list.
2944 */
2945static void
2946ngintr(void)
2947{
2948	item_p item;
2949	node_p  node = NULL;
2950
2951	for (;;) {
2952		mtx_enter(&ng_worklist_mtx, MTX_SPIN);
2953		node = TAILQ_FIRST(&ng_worklist);
2954		if (!node) {
2955			mtx_exit(&ng_worklist_mtx, MTX_SPIN);
2956			break;
2957		}
2958		TAILQ_REMOVE(&ng_worklist, node, nd_work);
2959		mtx_exit(&ng_worklist_mtx, MTX_SPIN);
2960		/*
2961		 * We have the node. We also take over the reference
2962		 * that the list had on it.
2963		 * Now process as much as you can, until it won't
2964		 * let you have another item off the queue.
2965		 * All this time, keep the reference
2966		 * that lets us be sure that the node still exists.
2967		 * Let the reference go at the last minute.
2968		 */
2969		for (;;) {
2970			mtx_enter(&node->nd_input_queue.q_mtx, MTX_SPIN);
2971			item = ng_dequeue(&node->nd_input_queue);
2972			if (item == NULL) {
2973				/*
2974				 * Say we are on the queue as long as
2975				 * we are processing it here.
2976				 * it probably wouldn't come here while we
2977				 * are processing anyhow.
2978				 */
2979				node->nd_flags &= ~NG_WORKQ;
2980				mtx_exit(&node->nd_input_queue.q_mtx, MTX_SPIN);
2981				NG_NODE_UNREF(node);
2982				break; /* go look for another node */
2983			} else {
2984				mtx_exit(&node->nd_input_queue.q_mtx, MTX_SPIN);
2985#ifdef	NETGRAPH_DEBUG
2986        _ngi_check(item, __FILE__, __LINE__);
2987#endif
2988				ng_apply_item(node, item);
2989			}
2990		}
2991	}
2992}
2993
2994static void
2995ng_worklist_remove(node_p node)
2996{
2997	mtx_enter(&ng_worklist_mtx, MTX_SPIN);
2998	if (node->nd_flags & NG_WORKQ) {
2999		TAILQ_REMOVE(&ng_worklist, node, nd_work);
3000		NG_NODE_UNREF(node);
3001	}
3002	node->nd_flags &= ~NG_WORKQ;
3003	mtx_exit(&ng_worklist_mtx, MTX_SPIN);
3004}
3005
3006static void
3007ng_setisr(node_p node)
3008{
3009	mtx_enter(&ng_worklist_mtx, MTX_SPIN);
3010	if ((node->nd_flags & NG_WORKQ) == 0) {
3011		/*
3012		 * If we are not already on the work queue,
3013		 * then put us on.
3014		 */
3015		node->nd_flags |= NG_WORKQ;
3016		TAILQ_INSERT_TAIL(&ng_worklist, node, nd_work);
3017		NG_NODE_REF(node);
3018	}
3019	mtx_exit(&ng_worklist_mtx, MTX_SPIN);
3020	schednetisr(NETISR_NETGRAPH);
3021}
3022
3023
3024/***********************************************************************
3025* Externally useable functions to set up a queue item ready for sending
3026***********************************************************************/
3027
3028#ifdef	NETGRAPH_DEBUG
3029#define	ITEM_DEBUG_CHECKS						\
3030	do {								\
3031		if (item->el_dest ) {					\
3032			printf("item already has node");		\
3033			Debugger("has node");				\
3034			NG_NODE_UNREF(item->el_dest);			\
3035			item->el_dest = NULL;				\
3036		}							\
3037		if (item->el_hook ) {					\
3038			printf("item already has hook");		\
3039			Debugger("has hook");				\
3040			NG_HOOK_UNREF(item->el_hook);			\
3041			item->el_hook = NULL;				\
3042		}							\
3043	} while (0)
3044#else
3045#define ITEM_DEBUG_CHECKS
3046#endif
3047
3048/*
3049 * Put elements into the item.
3050 * Hook and node references will be removed when the item is dequeued.
3051 * (or equivalent)
3052 * (XXX) Unsafe because no reference held by peer on remote node.
3053 * remote node might go away in this timescale.
3054 * We know the hooks can't go away because that would require getting
3055 * a writer item on both nodes and we must have at least a  reader
3056 * here to eb able to do this.
3057 * Note that the hook loaded is the REMOTE hook.
3058 *
3059 * This is possibly in the critical path for new data.
3060 */
3061item_p
3062ng_package_data(struct mbuf *m, meta_p meta)
3063{
3064	item_p item;
3065
3066	if ((item = ng_getqblk()) == NULL) {
3067		NG_FREE_M(m);
3068		NG_FREE_META(meta);
3069		return (NULL);
3070	}
3071	ITEM_DEBUG_CHECKS;
3072	item->el_flags = NGQF_DATA;
3073	item->el_next = NULL;
3074	NGI_M(item) = m;
3075	NGI_META(item) = meta;
3076	return (item);
3077}
3078
3079/*
3080 * Allocate a queue item and put items into it..
3081 * Evaluate the address as this will be needed to queue it and
3082 * to work out what some of the fields should be.
3083 * Hook and node references will be removed when the item is dequeued.
3084 * (or equivalent)
3085 */
3086item_p
3087ng_package_msg(struct ng_mesg *msg)
3088{
3089	item_p item;
3090
3091	if ((item = ng_getqblk()) == NULL) {
3092		if ((msg->header.flags & NGF_STATIC) == 0) {
3093			NG_FREE_MSG(msg);
3094		}
3095		return (NULL);
3096	}
3097	ITEM_DEBUG_CHECKS;
3098	item->el_flags = NGQF_MESG;
3099	item->el_next = NULL;
3100	/*
3101	 * Set the current lasthook into the queue item
3102	 */
3103	NGI_MSG(item) = msg;
3104	NGI_RETADDR(item) = NULL;
3105	return (item);
3106}
3107
3108
3109
3110#define SET_RETADDR							\
3111	do {	/* Data items don't have retaddrs */			\
3112		if ((item->el_flags & NGQF_D_M) == NGQF_MESG) {		\
3113			if (retaddr) {					\
3114				NGI_RETADDR(item) = retaddr;		\
3115			} else {					\
3116				/*					\
3117				 * The old return address should be ok.	\
3118				 * If there isn't one, use the address	\
3119				 * here.				\
3120				 */					\
3121				if (NGI_RETADDR(item) == 0) {		\
3122					NGI_RETADDR(item)		\
3123						= ng_node2ID(here);	\
3124				}					\
3125			}						\
3126		}							\
3127	} while (0)
3128
3129int
3130ng_address_hook(node_p here, item_p item, hook_p hook, ng_ID_t retaddr)
3131{
3132	ITEM_DEBUG_CHECKS;
3133	/*
3134	 * Quick sanity check..
3135	 * Since a hook holds a reference on it's node, once we know
3136	 * that the peer is still connected (even if invalid,) we know
3137	 * that the peer node is present, though maybe invalid.
3138	 */
3139	if ((hook == NULL)
3140	|| NG_HOOK_NOT_VALID(hook)
3141	|| (NG_HOOK_PEER(hook) == NULL)
3142	|| NG_HOOK_NOT_VALID(NG_HOOK_PEER(hook))
3143	|| NG_NODE_NOT_VALID(NG_PEER_NODE(hook))) {
3144		NG_FREE_ITEM(item);
3145		TRAP_ERROR
3146		return (EINVAL);
3147	}
3148
3149	/*
3150	 * Transfer our interest to the other (peer) end.
3151	 */
3152	item->el_hook = NG_HOOK_PEER(hook);
3153	NG_HOOK_REF(item->el_hook); /* Don't let it go while on the queue */
3154	item->el_dest = NG_PEER_NODE(hook);
3155	NG_NODE_REF(item->el_dest); /* Nor this */
3156	SET_RETADDR;
3157	return (0);
3158}
3159
3160int
3161ng_address_path(node_p here, item_p item, char *address, ng_ID_t retaddr)
3162{
3163	node_p  dest = NULL;
3164	hook_p	hook = NULL;
3165	int     error;
3166
3167	ITEM_DEBUG_CHECKS;
3168	/*
3169	 * Note that ng_path2noderef increments the reference count
3170	 * on the node for us if it finds one. So we don't have to.
3171	 */
3172	error = ng_path2noderef(here, address, &dest, &hook);
3173	if (error) {
3174		NG_FREE_ITEM(item);
3175		return (error);
3176	}
3177	item->el_dest = dest;
3178	if (( item->el_hook = hook))
3179		NG_HOOK_REF(hook);	/* don't let it go while on the queue */
3180	SET_RETADDR;
3181	return (0);
3182}
3183
3184int
3185ng_address_ID(node_p here, item_p item, ng_ID_t ID, ng_ID_t retaddr)
3186{
3187	node_p dest;
3188
3189	ITEM_DEBUG_CHECKS;
3190	/*
3191	 * Find the target node.
3192	 */
3193	dest = ng_ID2noderef(ID); /* GETS REFERENCE! */
3194	if (dest == NULL) {
3195		NG_FREE_ITEM(item);
3196		TRAP_ERROR
3197		return(EINVAL);
3198	}
3199	/* Fill out the contents */
3200	item->el_flags = NGQF_MESG;
3201	item->el_next = NULL;
3202	item->el_dest = dest;
3203	item->el_hook = NULL;
3204	SET_RETADDR;
3205	return (0);
3206}
3207
3208/*
3209 * special case to send a message to self (e.g. destroy node)
3210 * Possibly indicate an arrival hook too.
3211 * Useful for removing that hook :-)
3212 */
3213item_p
3214ng_package_msg_self(node_p here, hook_p hook, struct ng_mesg *msg)
3215{
3216	item_p item;
3217
3218	/*
3219	 * Find the target node.
3220	 * If there is a HOOK argument, then use that in preference
3221	 * to the address.
3222	 */
3223	if ((item = ng_getqblk()) == NULL) {
3224		if ((msg->header.flags & NGF_STATIC) == 0) {
3225			NG_FREE_MSG(msg);
3226		}
3227		return (NULL);
3228	}
3229
3230	/* Fill out the contents */
3231	item->el_flags = NGQF_MESG;
3232	item->el_next = NULL;
3233	item->el_dest = here;
3234	NG_NODE_REF(here);
3235	item->el_hook = hook;
3236	if (hook)
3237		NG_HOOK_REF(hook);
3238	NGI_MSG(item) = msg;
3239	NGI_RETADDR(item) = ng_node2ID(here);
3240	return (item);
3241}
3242
3243/*
3244 * Set the address, if none given, give the node here.
3245 */
3246void
3247ng_replace_retaddr(node_p here, item_p item, ng_ID_t retaddr)
3248{
3249	if (retaddr) {
3250		NGI_RETADDR(item) = retaddr;
3251	} else {
3252		/*
3253		 * The old return address should be ok.
3254		 * If there isn't one, use the address here.
3255		 */
3256		NGI_RETADDR(item) = ng_node2ID(here);
3257	}
3258}
3259
3260#define TESTING
3261#ifdef TESTING
3262/* just test all the macros */
3263void
3264ng_macro_test(item_p item);
3265void
3266ng_macro_test(item_p item)
3267{
3268	node_p node = NULL;
3269	hook_p hook = NULL;
3270	struct mbuf *m;
3271	meta_p meta;
3272	struct ng_mesg *msg;
3273	ng_ID_t retaddr;
3274	int	error;
3275
3276	NGI_GET_M(item, m);
3277	NGI_GET_META(item, meta);
3278	NGI_GET_MSG(item, msg);
3279	retaddr = NGI_RETADDR(item);
3280	NG_SEND_DATA(error, hook, m, meta);
3281	NG_SEND_DATA_ONLY(error, hook, m);
3282	NG_FWD_NEW_DATA(error, item, hook, m);
3283	NG_FWD_ITEM_HOOK(error, item, hook);
3284	NG_SEND_MSG_HOOK(error, node, msg, hook, retaddr);
3285	NG_SEND_MSG_ID(error, node, msg, retaddr, retaddr);
3286	NG_SEND_MSG_PATH(error, node, msg, ".:", retaddr);
3287	NG_QUEUE_MSG(error, node, msg, ".:", retaddr);
3288	NG_FWD_MSG_HOOK(error, node, item, hook, retaddr);
3289}
3290#endif /* TESTING */
3291
3292