xref: /freebsd/sys/netgraph/ng_base.c (revision cc3bbd68c54fab5539ff1cfa5f7bfb454633239c)
14cf49a43SJulian Elischer 
24cf49a43SJulian Elischer /*
34cf49a43SJulian Elischer  * ng_base.c
44cf49a43SJulian Elischer  *
54cf49a43SJulian Elischer  * Copyright (c) 1996-1999 Whistle Communications, Inc.
64cf49a43SJulian Elischer  * All rights reserved.
74cf49a43SJulian Elischer  *
84cf49a43SJulian Elischer  * Subject to the following obligations and disclaimer of warranty, use and
94cf49a43SJulian Elischer  * redistribution of this software, in source or object code forms, with or
104cf49a43SJulian Elischer  * without modifications are expressly permitted by Whistle Communications;
114cf49a43SJulian Elischer  * provided, however, that:
124cf49a43SJulian Elischer  * 1. Any and all reproductions of the source or object code must include the
134cf49a43SJulian Elischer  *    copyright notice above and the following disclaimer of warranties; and
144cf49a43SJulian Elischer  * 2. No rights are granted, in any manner or form, to use Whistle
154cf49a43SJulian Elischer  *    Communications, Inc. trademarks, including the mark "WHISTLE
164cf49a43SJulian Elischer  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
174cf49a43SJulian Elischer  *    such appears in the above copyright notice or in the software.
184cf49a43SJulian Elischer  *
194cf49a43SJulian Elischer  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
204cf49a43SJulian Elischer  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
214cf49a43SJulian Elischer  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
224cf49a43SJulian Elischer  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
234cf49a43SJulian Elischer  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
244cf49a43SJulian Elischer  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
254cf49a43SJulian Elischer  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
264cf49a43SJulian Elischer  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
274cf49a43SJulian Elischer  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
284cf49a43SJulian Elischer  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
294cf49a43SJulian Elischer  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
304cf49a43SJulian Elischer  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
314cf49a43SJulian Elischer  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
324cf49a43SJulian Elischer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
334cf49a43SJulian Elischer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
344cf49a43SJulian Elischer  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
354cf49a43SJulian Elischer  * OF SUCH DAMAGE.
364cf49a43SJulian Elischer  *
37cc3bbd68SJulian Elischer  * Authors: Julian Elischer <julian@freebsd.org>
38cc3bbd68SJulian Elischer  *          Archie Cobbs <archie@freebsd.org>
394cf49a43SJulian Elischer  *
404cf49a43SJulian Elischer  * $FreeBSD$
414cf49a43SJulian Elischer  * $Whistle: ng_base.c,v 1.39 1999/01/28 23:54:53 julian Exp $
424cf49a43SJulian Elischer  */
434cf49a43SJulian Elischer 
444cf49a43SJulian Elischer /*
454cf49a43SJulian Elischer  * This file implements the base netgraph code.
464cf49a43SJulian Elischer  */
474cf49a43SJulian Elischer 
484cf49a43SJulian Elischer #include <sys/param.h>
494cf49a43SJulian Elischer #include <sys/systm.h>
504cf49a43SJulian Elischer #include <sys/errno.h>
514cf49a43SJulian Elischer #include <sys/kernel.h>
524cf49a43SJulian Elischer #include <sys/malloc.h>
534cf49a43SJulian Elischer #include <sys/syslog.h>
544cf49a43SJulian Elischer #include <sys/linker.h>
554cf49a43SJulian Elischer #include <sys/queue.h>
564cf49a43SJulian Elischer #include <sys/mbuf.h>
575b664c7cSPoul-Henning Kamp #include <sys/ctype.h>
582b70adcbSArchie Cobbs #include <machine/limits.h>
594cf49a43SJulian Elischer 
604cf49a43SJulian Elischer #include <net/netisr.h>
614cf49a43SJulian Elischer 
624cf49a43SJulian Elischer #include <netgraph/ng_message.h>
634cf49a43SJulian Elischer #include <netgraph/netgraph.h>
64f8307e12SArchie Cobbs #include <netgraph/ng_parse.h>
654cf49a43SJulian Elischer 
6699ff8176SPeter Wemm MODULE_VERSION(netgraph, 1);
6799ff8176SPeter Wemm 
684cf49a43SJulian Elischer /* List of all nodes */
69e3975643SJake Burkholder static LIST_HEAD(, ng_node) nodelist;
704cf49a43SJulian Elischer 
714cf49a43SJulian Elischer /* List of installed types */
72e3975643SJake Burkholder static LIST_HEAD(, ng_type) typelist;
734cf49a43SJulian Elischer 
74dc90cad9SJulian Elischer /* Hash releted definitions */
75dc90cad9SJulian Elischer #define ID_HASH_SIZE 32 /* most systems wont need even this many */
76e3975643SJake Burkholder static LIST_HEAD(, ng_node) ID_hash[ID_HASH_SIZE];
77dc90cad9SJulian Elischer /* Don't nead to initialise them because it's a LIST */
78dc90cad9SJulian Elischer 
794cf49a43SJulian Elischer /* Internal functions */
804cf49a43SJulian Elischer static int	ng_add_hook(node_p node, const char *name, hook_p * hookp);
814cf49a43SJulian Elischer static int	ng_connect(hook_p hook1, hook_p hook2);
824cf49a43SJulian Elischer static void	ng_disconnect_hook(hook_p hook);
834cf49a43SJulian Elischer static int	ng_generic_msg(node_p here, struct ng_mesg *msg,
84a4ec03cfSJulian Elischer 			const char *retaddr, struct ng_mesg ** resp,
85a4ec03cfSJulian Elischer 			hook_p hook);
86dc90cad9SJulian Elischer static ng_ID_t	ng_decodeidname(const char *name);
874cf49a43SJulian Elischer static int	ngb_mod_event(module_t mod, int event, void *data);
884cf49a43SJulian Elischer static void	ngintr(void);
894cf49a43SJulian Elischer 
904cf49a43SJulian Elischer /* Our own netgraph malloc type */
914cf49a43SJulian Elischer MALLOC_DEFINE(M_NETGRAPH, "netgraph", "netgraph structures and ctrl messages");
924cf49a43SJulian Elischer 
934cf49a43SJulian Elischer /* Set this to Debugger("X") to catch all errors as they occur */
944cf49a43SJulian Elischer #ifndef TRAP_ERROR
954cf49a43SJulian Elischer #define TRAP_ERROR
964cf49a43SJulian Elischer #endif
974cf49a43SJulian Elischer 
98dc90cad9SJulian Elischer static	ng_ID_t nextID = 1;
99dc90cad9SJulian Elischer 
100b2da83c2SArchie Cobbs #ifdef INVARIANTS
101b2da83c2SArchie Cobbs #define CHECK_DATA_MBUF(m)	do {					\
102b2da83c2SArchie Cobbs 		struct mbuf *n;						\
103b2da83c2SArchie Cobbs 		int total;						\
104b2da83c2SArchie Cobbs 									\
105b2da83c2SArchie Cobbs 		if (((m)->m_flags & M_PKTHDR) == 0)			\
106b2da83c2SArchie Cobbs 			panic("%s: !PKTHDR", __FUNCTION__);		\
107b2da83c2SArchie Cobbs 		for (total = 0, n = (m); n != NULL; n = n->m_next)	\
108b2da83c2SArchie Cobbs 			total += n->m_len;				\
109b2da83c2SArchie Cobbs 		if ((m)->m_pkthdr.len != total) {			\
110b2da83c2SArchie Cobbs 			panic("%s: %d != %d",				\
111b2da83c2SArchie Cobbs 			    __FUNCTION__, (m)->m_pkthdr.len, total);	\
112b2da83c2SArchie Cobbs 		}							\
113b2da83c2SArchie Cobbs 	} while (0)
114b2da83c2SArchie Cobbs #else
115b2da83c2SArchie Cobbs #define CHECK_DATA_MBUF(m)
116b2da83c2SArchie Cobbs #endif
117b2da83c2SArchie Cobbs 
118dc90cad9SJulian Elischer 
1194cf49a43SJulian Elischer /************************************************************************
120f8307e12SArchie Cobbs 	Parse type definitions for generic messages
121f8307e12SArchie Cobbs ************************************************************************/
122f8307e12SArchie Cobbs 
123f8307e12SArchie Cobbs /* Handy structure parse type defining macro */
124f8307e12SArchie Cobbs #define DEFINE_PARSE_STRUCT_TYPE(lo, up, args)				\
125f8307e12SArchie Cobbs static const struct ng_parse_struct_info				\
126f8307e12SArchie Cobbs 	ng_ ## lo ## _type_info = NG_GENERIC_ ## up ## _INFO args;	\
127f8307e12SArchie Cobbs static const struct ng_parse_type ng_generic_ ## lo ## _type = {	\
128f8307e12SArchie Cobbs 	&ng_parse_struct_type,						\
129f8307e12SArchie Cobbs 	&ng_ ## lo ## _type_info					\
130f8307e12SArchie Cobbs }
131f8307e12SArchie Cobbs 
132f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(mkpeer, MKPEER, ());
133f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(connect, CONNECT, ());
134f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(name, NAME, ());
135f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(rmhook, RMHOOK, ());
136f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(nodeinfo, NODEINFO, ());
137f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(typeinfo, TYPEINFO, ());
138f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(linkinfo, LINKINFO, (&ng_generic_nodeinfo_type));
139f8307e12SArchie Cobbs 
140f8307e12SArchie Cobbs /* Get length of an array when the length is stored as a 32 bit
141f8307e12SArchie Cobbs    value immediately preceeding the array -- as with struct namelist
142f8307e12SArchie Cobbs    and struct typelist. */
143f8307e12SArchie Cobbs static int
144f8307e12SArchie Cobbs ng_generic_list_getLength(const struct ng_parse_type *type,
145f8307e12SArchie Cobbs 	const u_char *start, const u_char *buf)
146f8307e12SArchie Cobbs {
147f8307e12SArchie Cobbs 	return *((const u_int32_t *)(buf - 4));
148f8307e12SArchie Cobbs }
149f8307e12SArchie Cobbs 
150f8307e12SArchie Cobbs /* Get length of the array of struct linkinfo inside a struct hooklist */
151f8307e12SArchie Cobbs static int
152f8307e12SArchie Cobbs ng_generic_linkinfo_getLength(const struct ng_parse_type *type,
153f8307e12SArchie Cobbs 	const u_char *start, const u_char *buf)
154f8307e12SArchie Cobbs {
155f8307e12SArchie Cobbs 	const struct hooklist *hl = (const struct hooklist *)start;
156f8307e12SArchie Cobbs 
157f8307e12SArchie Cobbs 	return hl->nodeinfo.hooks;
158f8307e12SArchie Cobbs }
159f8307e12SArchie Cobbs 
160f8307e12SArchie Cobbs /* Array type for a variable length array of struct namelist */
161f8307e12SArchie Cobbs static const struct ng_parse_array_info ng_nodeinfoarray_type_info = {
162f8307e12SArchie Cobbs 	&ng_generic_nodeinfo_type,
163f8307e12SArchie Cobbs 	&ng_generic_list_getLength
164f8307e12SArchie Cobbs };
165f8307e12SArchie Cobbs static const struct ng_parse_type ng_generic_nodeinfoarray_type = {
166f8307e12SArchie Cobbs 	&ng_parse_array_type,
167f8307e12SArchie Cobbs 	&ng_nodeinfoarray_type_info
168f8307e12SArchie Cobbs };
169f8307e12SArchie Cobbs 
170f8307e12SArchie Cobbs /* Array type for a variable length array of struct typelist */
171f8307e12SArchie Cobbs static const struct ng_parse_array_info ng_typeinfoarray_type_info = {
172f8307e12SArchie Cobbs 	&ng_generic_typeinfo_type,
173f8307e12SArchie Cobbs 	&ng_generic_list_getLength
174f8307e12SArchie Cobbs };
175f8307e12SArchie Cobbs static const struct ng_parse_type ng_generic_typeinfoarray_type = {
176f8307e12SArchie Cobbs 	&ng_parse_array_type,
177f8307e12SArchie Cobbs 	&ng_typeinfoarray_type_info
178f8307e12SArchie Cobbs };
179f8307e12SArchie Cobbs 
180f8307e12SArchie Cobbs /* Array type for array of struct linkinfo in struct hooklist */
181f8307e12SArchie Cobbs static const struct ng_parse_array_info ng_generic_linkinfo_array_type_info = {
182f8307e12SArchie Cobbs 	&ng_generic_linkinfo_type,
183f8307e12SArchie Cobbs 	&ng_generic_linkinfo_getLength
184f8307e12SArchie Cobbs };
185f8307e12SArchie Cobbs static const struct ng_parse_type ng_generic_linkinfo_array_type = {
186f8307e12SArchie Cobbs 	&ng_parse_array_type,
187f8307e12SArchie Cobbs 	&ng_generic_linkinfo_array_type_info
188f8307e12SArchie Cobbs };
189f8307e12SArchie Cobbs 
190f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(typelist, TYPELIST, (&ng_generic_nodeinfoarray_type));
191f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(hooklist, HOOKLIST,
192f8307e12SArchie Cobbs 	(&ng_generic_nodeinfo_type, &ng_generic_linkinfo_array_type));
193f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(listnodes, LISTNODES,
194f8307e12SArchie Cobbs 	(&ng_generic_nodeinfoarray_type));
195f8307e12SArchie Cobbs 
196f8307e12SArchie Cobbs /* List of commands and how to convert arguments to/from ASCII */
197f8307e12SArchie Cobbs static const struct ng_cmdlist ng_generic_cmds[] = {
198f8307e12SArchie Cobbs 	{
199f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
200f8307e12SArchie Cobbs 	  NGM_SHUTDOWN,
201f8307e12SArchie Cobbs 	  "shutdown",
202f8307e12SArchie Cobbs 	  NULL,
203f8307e12SArchie Cobbs 	  NULL
204f8307e12SArchie Cobbs 	},
205f8307e12SArchie Cobbs 	{
206f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
207f8307e12SArchie Cobbs 	  NGM_MKPEER,
208f8307e12SArchie Cobbs 	  "mkpeer",
209f8307e12SArchie Cobbs 	  &ng_generic_mkpeer_type,
210f8307e12SArchie Cobbs 	  NULL
211f8307e12SArchie Cobbs 	},
212f8307e12SArchie Cobbs 	{
213f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
214f8307e12SArchie Cobbs 	  NGM_CONNECT,
215f8307e12SArchie Cobbs 	  "connect",
216f8307e12SArchie Cobbs 	  &ng_generic_connect_type,
217f8307e12SArchie Cobbs 	  NULL
218f8307e12SArchie Cobbs 	},
219f8307e12SArchie Cobbs 	{
220f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
221f8307e12SArchie Cobbs 	  NGM_NAME,
222f8307e12SArchie Cobbs 	  "name",
223f8307e12SArchie Cobbs 	  &ng_generic_name_type,
224f8307e12SArchie Cobbs 	  NULL
225f8307e12SArchie Cobbs 	},
226f8307e12SArchie Cobbs 	{
227f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
228f8307e12SArchie Cobbs 	  NGM_RMHOOK,
229f8307e12SArchie Cobbs 	  "rmhook",
230f8307e12SArchie Cobbs 	  &ng_generic_rmhook_type,
231f8307e12SArchie Cobbs 	  NULL
232f8307e12SArchie Cobbs 	},
233f8307e12SArchie Cobbs 	{
234f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
235f8307e12SArchie Cobbs 	  NGM_NODEINFO,
236f8307e12SArchie Cobbs 	  "nodeinfo",
237f8307e12SArchie Cobbs 	  NULL,
238f8307e12SArchie Cobbs 	  &ng_generic_nodeinfo_type
239f8307e12SArchie Cobbs 	},
240f8307e12SArchie Cobbs 	{
241f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
242f8307e12SArchie Cobbs 	  NGM_LISTHOOKS,
243f8307e12SArchie Cobbs 	  "listhooks",
244f8307e12SArchie Cobbs 	  NULL,
245f8307e12SArchie Cobbs 	  &ng_generic_hooklist_type
246f8307e12SArchie Cobbs 	},
247f8307e12SArchie Cobbs 	{
248f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
249f8307e12SArchie Cobbs 	  NGM_LISTNAMES,
250f8307e12SArchie Cobbs 	  "listnames",
251f8307e12SArchie Cobbs 	  NULL,
252f8307e12SArchie Cobbs 	  &ng_generic_listnodes_type	/* same as NGM_LISTNODES */
253f8307e12SArchie Cobbs 	},
254f8307e12SArchie Cobbs 	{
255f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
256f8307e12SArchie Cobbs 	  NGM_LISTNODES,
257f8307e12SArchie Cobbs 	  "listnodes",
258f8307e12SArchie Cobbs 	  NULL,
259f8307e12SArchie Cobbs 	  &ng_generic_listnodes_type
260f8307e12SArchie Cobbs 	},
261f8307e12SArchie Cobbs 	{
262f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
263f8307e12SArchie Cobbs 	  NGM_LISTTYPES,
264f8307e12SArchie Cobbs 	  "listtypes",
265f8307e12SArchie Cobbs 	  NULL,
266f8307e12SArchie Cobbs 	  &ng_generic_typeinfo_type
267f8307e12SArchie Cobbs 	},
268f8307e12SArchie Cobbs 	{
269f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
2707095e097SPoul-Henning Kamp 	  NGM_TEXT_CONFIG,
2717095e097SPoul-Henning Kamp 	  "textconfig",
2727095e097SPoul-Henning Kamp 	  NULL,
2737095e097SPoul-Henning Kamp 	  &ng_parse_string_type
2747095e097SPoul-Henning Kamp 	},
2757095e097SPoul-Henning Kamp 	{
2767095e097SPoul-Henning Kamp 	  NGM_GENERIC_COOKIE,
277f8307e12SArchie Cobbs 	  NGM_TEXT_STATUS,
278f8307e12SArchie Cobbs 	  "textstatus",
279f8307e12SArchie Cobbs 	  NULL,
280f8307e12SArchie Cobbs 	  &ng_parse_string_type
281f8307e12SArchie Cobbs 	},
282f8307e12SArchie Cobbs 	{
283f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
284f8307e12SArchie Cobbs 	  NGM_ASCII2BINARY,
285f8307e12SArchie Cobbs 	  "ascii2binary",
286f8307e12SArchie Cobbs 	  &ng_parse_ng_mesg_type,
287f8307e12SArchie Cobbs 	  &ng_parse_ng_mesg_type
288f8307e12SArchie Cobbs 	},
289f8307e12SArchie Cobbs 	{
290f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
291f8307e12SArchie Cobbs 	  NGM_BINARY2ASCII,
292f8307e12SArchie Cobbs 	  "binary2ascii",
293f8307e12SArchie Cobbs 	  &ng_parse_ng_mesg_type,
294f8307e12SArchie Cobbs 	  &ng_parse_ng_mesg_type
295f8307e12SArchie Cobbs 	},
296f8307e12SArchie Cobbs 	{ 0 }
297f8307e12SArchie Cobbs };
298f8307e12SArchie Cobbs 
299f8307e12SArchie Cobbs /************************************************************************
3004cf49a43SJulian Elischer 			Node routines
3014cf49a43SJulian Elischer ************************************************************************/
3024cf49a43SJulian Elischer 
3034cf49a43SJulian Elischer /*
3044cf49a43SJulian Elischer  * Instantiate a node of the requested type
3054cf49a43SJulian Elischer  */
3064cf49a43SJulian Elischer int
3074cf49a43SJulian Elischer ng_make_node(const char *typename, node_p *nodepp)
3084cf49a43SJulian Elischer {
3094cf49a43SJulian Elischer 	struct ng_type *type;
3104cf49a43SJulian Elischer 
3114cf49a43SJulian Elischer 	/* Check that the type makes sense */
3124cf49a43SJulian Elischer 	if (typename == NULL) {
3134cf49a43SJulian Elischer 		TRAP_ERROR;
3144cf49a43SJulian Elischer 		return (EINVAL);
3154cf49a43SJulian Elischer 	}
3164cf49a43SJulian Elischer 
3174cf49a43SJulian Elischer 	/* Locate the node type */
3184cf49a43SJulian Elischer 	if ((type = ng_findtype(typename)) == NULL) {
319cc3daab5SPeter Wemm 		char filename[NG_TYPELEN + 4];
3204cf49a43SJulian Elischer 		linker_file_t lf;
3214cf49a43SJulian Elischer 		int error;
3224cf49a43SJulian Elischer 
3234cf49a43SJulian Elischer 		/* Not found, try to load it as a loadable module */
324cc3daab5SPeter Wemm 		snprintf(filename, sizeof(filename), "ng_%s", typename);
325cc3daab5SPeter Wemm 		error = linker_load_file(filename, &lf);
3264cf49a43SJulian Elischer 		if (error != 0)
3274cf49a43SJulian Elischer 			return (error);
3284cf49a43SJulian Elischer 		lf->userrefs++;		/* pretend loaded by the syscall */
3294cf49a43SJulian Elischer 
3304cf49a43SJulian Elischer 		/* Try again, as now the type should have linked itself in */
3314cf49a43SJulian Elischer 		if ((type = ng_findtype(typename)) == NULL)
3324cf49a43SJulian Elischer 			return (ENXIO);
3334cf49a43SJulian Elischer 	}
3344cf49a43SJulian Elischer 
3354cf49a43SJulian Elischer 	/* Call the constructor */
3364cf49a43SJulian Elischer 	if (type->constructor != NULL)
3374cf49a43SJulian Elischer 		return ((*type->constructor)(nodepp));
3384cf49a43SJulian Elischer 	else
3394cf49a43SJulian Elischer 		return (ng_make_node_common(type, nodepp));
3404cf49a43SJulian Elischer }
3414cf49a43SJulian Elischer 
3424cf49a43SJulian Elischer /*
3434cf49a43SJulian Elischer  * Generic node creation. Called by node constructors.
3444cf49a43SJulian Elischer  * The returned node has a reference count of 1.
3454cf49a43SJulian Elischer  */
3464cf49a43SJulian Elischer int
3474cf49a43SJulian Elischer ng_make_node_common(struct ng_type *type, node_p *nodepp)
3484cf49a43SJulian Elischer {
3494cf49a43SJulian Elischer 	node_p node;
3504cf49a43SJulian Elischer 
3514cf49a43SJulian Elischer 	/* Require the node type to have been already installed */
3524cf49a43SJulian Elischer 	if (ng_findtype(type->name) == NULL) {
3534cf49a43SJulian Elischer 		TRAP_ERROR;
3544cf49a43SJulian Elischer 		return (EINVAL);
3554cf49a43SJulian Elischer 	}
3564cf49a43SJulian Elischer 
3574cf49a43SJulian Elischer 	/* Make a node and try attach it to the type */
358d2ea40c2SArchie Cobbs 	MALLOC(node, node_p, sizeof(*node), M_NETGRAPH, M_NOWAIT);
3594cf49a43SJulian Elischer 	if (node == NULL) {
3604cf49a43SJulian Elischer 		TRAP_ERROR;
3614cf49a43SJulian Elischer 		return (ENOMEM);
3624cf49a43SJulian Elischer 	}
3634cf49a43SJulian Elischer 	bzero(node, sizeof(*node));
3644cf49a43SJulian Elischer 	node->type = type;
3654cf49a43SJulian Elischer 	node->refs++;				/* note reference */
3664cf49a43SJulian Elischer 	type->refs++;
3674cf49a43SJulian Elischer 
3684cf49a43SJulian Elischer 	/* Link us into the node linked list */
3694cf49a43SJulian Elischer 	LIST_INSERT_HEAD(&nodelist, node, nodes);
3704cf49a43SJulian Elischer 
3714cf49a43SJulian Elischer 	/* Initialize hook list for new node */
3724cf49a43SJulian Elischer 	LIST_INIT(&node->hooks);
3734cf49a43SJulian Elischer 
374dc90cad9SJulian Elischer 	/* get an ID and put us in the hash chain */
375dc90cad9SJulian Elischer 	node->ID = nextID++; /* 137 per second for 1 year before wrap */
376dc90cad9SJulian Elischer 	LIST_INSERT_HEAD(&ID_hash[node->ID % ID_HASH_SIZE], node, idnodes);
377dc90cad9SJulian Elischer 
3784cf49a43SJulian Elischer 	/* Done */
3794cf49a43SJulian Elischer 	*nodepp = node;
3804cf49a43SJulian Elischer 	return (0);
3814cf49a43SJulian Elischer }
3824cf49a43SJulian Elischer 
3834cf49a43SJulian Elischer /*
3844cf49a43SJulian Elischer  * Forceably start the shutdown process on a node. Either call
3854cf49a43SJulian Elischer  * it's shutdown method, or do the default shutdown if there is
3864cf49a43SJulian Elischer  * no type-specific method.
3874cf49a43SJulian Elischer  *
3884cf49a43SJulian Elischer  * Persistent nodes must have a type-specific method which
3894cf49a43SJulian Elischer  * resets the NG_INVALID flag.
3904cf49a43SJulian Elischer  */
3914cf49a43SJulian Elischer void
3924cf49a43SJulian Elischer ng_rmnode(node_p node)
3934cf49a43SJulian Elischer {
3944cf49a43SJulian Elischer 	/* Check if it's already shutting down */
3954cf49a43SJulian Elischer 	if ((node->flags & NG_INVALID) != 0)
3964cf49a43SJulian Elischer 		return;
3974cf49a43SJulian Elischer 
3984cf49a43SJulian Elischer 	/* Add an extra reference so it doesn't go away during this */
3994cf49a43SJulian Elischer 	node->refs++;
4004cf49a43SJulian Elischer 
4014cf49a43SJulian Elischer 	/* Mark it invalid so any newcomers know not to try use it */
4024cf49a43SJulian Elischer 	node->flags |= NG_INVALID;
4034cf49a43SJulian Elischer 
4044cf49a43SJulian Elischer 	/* Ask the type if it has anything to do in this case */
4054cf49a43SJulian Elischer 	if (node->type && node->type->shutdown)
4064cf49a43SJulian Elischer 		(*node->type->shutdown)(node);
4074cf49a43SJulian Elischer 	else {				/* do the default thing */
4084cf49a43SJulian Elischer 		ng_unname(node);
4094cf49a43SJulian Elischer 		ng_cutlinks(node);
4104cf49a43SJulian Elischer 		ng_unref(node);
4114cf49a43SJulian Elischer 	}
4124cf49a43SJulian Elischer 
4134cf49a43SJulian Elischer 	/* Remove extra reference, possibly the last */
4144cf49a43SJulian Elischer 	ng_unref(node);
4154cf49a43SJulian Elischer }
4164cf49a43SJulian Elischer 
4174cf49a43SJulian Elischer /*
4184cf49a43SJulian Elischer  * Called by the destructor to remove any STANDARD external references
4194cf49a43SJulian Elischer  */
4204cf49a43SJulian Elischer void
4214cf49a43SJulian Elischer ng_cutlinks(node_p node)
4224cf49a43SJulian Elischer {
4234cf49a43SJulian Elischer 	hook_p  hook;
4244cf49a43SJulian Elischer 
4254cf49a43SJulian Elischer 	/* Make sure that this is set to stop infinite loops */
4264cf49a43SJulian Elischer 	node->flags |= NG_INVALID;
4274cf49a43SJulian Elischer 
4284cf49a43SJulian Elischer 	/* If we have sleepers, wake them up; they'll see NG_INVALID */
4294cf49a43SJulian Elischer 	if (node->sleepers)
4304cf49a43SJulian Elischer 		wakeup(node);
4314cf49a43SJulian Elischer 
4324cf49a43SJulian Elischer 	/* Notify all remaining connected nodes to disconnect */
4334cf49a43SJulian Elischer 	while ((hook = LIST_FIRST(&node->hooks)) != NULL)
4344cf49a43SJulian Elischer 		ng_destroy_hook(hook);
4354cf49a43SJulian Elischer }
4364cf49a43SJulian Elischer 
4374cf49a43SJulian Elischer /*
4384cf49a43SJulian Elischer  * Remove a reference to the node, possibly the last
4394cf49a43SJulian Elischer  */
4404cf49a43SJulian Elischer void
4414cf49a43SJulian Elischer ng_unref(node_p node)
4424cf49a43SJulian Elischer {
4434cf49a43SJulian Elischer 	if (--node->refs <= 0) {
4444cf49a43SJulian Elischer 		node->type->refs--;
4454cf49a43SJulian Elischer 		LIST_REMOVE(node, nodes);
446dc90cad9SJulian Elischer 		LIST_REMOVE(node, idnodes);
4474cf49a43SJulian Elischer 		FREE(node, M_NETGRAPH);
4484cf49a43SJulian Elischer 	}
4494cf49a43SJulian Elischer }
4504cf49a43SJulian Elischer 
4514cf49a43SJulian Elischer /*
4524cf49a43SJulian Elischer  * Wait for a node to come ready. Returns a node with a reference count;
4534cf49a43SJulian Elischer  * don't forget to drop it when we are done with it using ng_release_node().
4544cf49a43SJulian Elischer  */
4554cf49a43SJulian Elischer int
4564cf49a43SJulian Elischer ng_wait_node(node_p node, char *msg)
4574cf49a43SJulian Elischer {
4584cf49a43SJulian Elischer 	int s, error = 0;
4594cf49a43SJulian Elischer 
4604cf49a43SJulian Elischer 	if (msg == NULL)
4614cf49a43SJulian Elischer 		msg = "netgraph";
4624cf49a43SJulian Elischer 	s = splnet();
4634cf49a43SJulian Elischer 	node->sleepers++;
4644cf49a43SJulian Elischer 	node->refs++;		/* the sleeping process counts as a reference */
4654cf49a43SJulian Elischer 	while ((node->flags & (NG_BUSY | NG_INVALID)) == NG_BUSY)
4664cf49a43SJulian Elischer 		error = tsleep(node, (PZERO + 1) | PCATCH, msg, 0);
4674cf49a43SJulian Elischer 	node->sleepers--;
4684cf49a43SJulian Elischer 	if (node->flags & NG_INVALID) {
4694cf49a43SJulian Elischer 		TRAP_ERROR;
4704cf49a43SJulian Elischer 		error = ENXIO;
4714cf49a43SJulian Elischer 	} else {
472b2da83c2SArchie Cobbs 		KASSERT(node->refs > 1,
473b2da83c2SArchie Cobbs 		    ("%s: refs=%d", __FUNCTION__, node->refs));
4744cf49a43SJulian Elischer 		node->flags |= NG_BUSY;
4754cf49a43SJulian Elischer 	}
4764cf49a43SJulian Elischer 	splx(s);
4774cf49a43SJulian Elischer 
4784cf49a43SJulian Elischer 	/* Release the reference we had on it */
4794cf49a43SJulian Elischer 	if (error != 0)
4804cf49a43SJulian Elischer 		ng_unref(node);
4814cf49a43SJulian Elischer 	return error;
4824cf49a43SJulian Elischer }
4834cf49a43SJulian Elischer 
4844cf49a43SJulian Elischer /*
4854cf49a43SJulian Elischer  * Release a node acquired via ng_wait_node()
4864cf49a43SJulian Elischer  */
4874cf49a43SJulian Elischer void
4884cf49a43SJulian Elischer ng_release_node(node_p node)
4894cf49a43SJulian Elischer {
4904cf49a43SJulian Elischer 	/* Declare that we don't want it */
4914cf49a43SJulian Elischer 	node->flags &= ~NG_BUSY;
4924cf49a43SJulian Elischer 
4934cf49a43SJulian Elischer 	/* If we have sleepers, then wake them up */
4944cf49a43SJulian Elischer 	if (node->sleepers)
4954cf49a43SJulian Elischer 		wakeup(node);
4964cf49a43SJulian Elischer 
4974cf49a43SJulian Elischer 	/* We also have a reference.. drop it too */
4984cf49a43SJulian Elischer 	ng_unref(node);
4994cf49a43SJulian Elischer }
5004cf49a43SJulian Elischer 
5014cf49a43SJulian Elischer /************************************************************************
502dc90cad9SJulian Elischer 			Node ID handling
503dc90cad9SJulian Elischer ************************************************************************/
504dc90cad9SJulian Elischer static node_p
505dc90cad9SJulian Elischer ng_ID2node(ng_ID_t ID)
506dc90cad9SJulian Elischer {
507dc90cad9SJulian Elischer 	node_p np;
508dc90cad9SJulian Elischer 	LIST_FOREACH(np, &ID_hash[ID % ID_HASH_SIZE], idnodes) {
509dc90cad9SJulian Elischer 		if (np->ID == ID)
510dc90cad9SJulian Elischer 			break;
511dc90cad9SJulian Elischer 	}
512dc90cad9SJulian Elischer 	return(np);
513dc90cad9SJulian Elischer }
514dc90cad9SJulian Elischer 
515dc90cad9SJulian Elischer ng_ID_t
516dc90cad9SJulian Elischer ng_node2ID(node_p node)
517dc90cad9SJulian Elischer {
518dc90cad9SJulian Elischer 	return (node->ID);
519dc90cad9SJulian Elischer }
520dc90cad9SJulian Elischer 
521dc90cad9SJulian Elischer /************************************************************************
5224cf49a43SJulian Elischer 			Node name handling
5234cf49a43SJulian Elischer ************************************************************************/
5244cf49a43SJulian Elischer 
5254cf49a43SJulian Elischer /*
5264cf49a43SJulian Elischer  * Assign a node a name. Once assigned, the name cannot be changed.
5274cf49a43SJulian Elischer  */
5284cf49a43SJulian Elischer int
5294cf49a43SJulian Elischer ng_name_node(node_p node, const char *name)
5304cf49a43SJulian Elischer {
5314cf49a43SJulian Elischer 	int i;
5324cf49a43SJulian Elischer 
5334cf49a43SJulian Elischer 	/* Check the name is valid */
5344cf49a43SJulian Elischer 	for (i = 0; i < NG_NODELEN + 1; i++) {
5354cf49a43SJulian Elischer 		if (name[i] == '\0' || name[i] == '.' || name[i] == ':')
5364cf49a43SJulian Elischer 			break;
5374cf49a43SJulian Elischer 	}
5384cf49a43SJulian Elischer 	if (i == 0 || name[i] != '\0') {
5394cf49a43SJulian Elischer 		TRAP_ERROR;
5404cf49a43SJulian Elischer 		return (EINVAL);
5414cf49a43SJulian Elischer 	}
542dc90cad9SJulian Elischer 	if (ng_decodeidname(name) != 0) { /* valid IDs not allowed here */
5434cf49a43SJulian Elischer 		TRAP_ERROR;
5444cf49a43SJulian Elischer 		return (EINVAL);
5454cf49a43SJulian Elischer 	}
5464cf49a43SJulian Elischer 
5474cf49a43SJulian Elischer 	/* Check the node isn't already named */
5484cf49a43SJulian Elischer 	if (node->name != NULL) {
5494cf49a43SJulian Elischer 		TRAP_ERROR;
5504cf49a43SJulian Elischer 		return (EISCONN);
5514cf49a43SJulian Elischer 	}
5524cf49a43SJulian Elischer 
5534cf49a43SJulian Elischer 	/* Check the name isn't already being used */
5544cf49a43SJulian Elischer 	if (ng_findname(node, name) != NULL) {
5554cf49a43SJulian Elischer 		TRAP_ERROR;
5564cf49a43SJulian Elischer 		return (EADDRINUSE);
5574cf49a43SJulian Elischer 	}
5584cf49a43SJulian Elischer 
5594cf49a43SJulian Elischer 	/* Allocate space and copy it */
560d2ea40c2SArchie Cobbs 	MALLOC(node->name, char *, strlen(name) + 1, M_NETGRAPH, M_NOWAIT);
5614cf49a43SJulian Elischer 	if (node->name == NULL) {
5624cf49a43SJulian Elischer 		TRAP_ERROR;
5634cf49a43SJulian Elischer 		return (ENOMEM);
5644cf49a43SJulian Elischer 	}
5654cf49a43SJulian Elischer 	strcpy(node->name, name);
5664cf49a43SJulian Elischer 
5674cf49a43SJulian Elischer 	/* The name counts as a reference */
5684cf49a43SJulian Elischer 	node->refs++;
5694cf49a43SJulian Elischer 	return (0);
5704cf49a43SJulian Elischer }
5714cf49a43SJulian Elischer 
5724cf49a43SJulian Elischer /*
5734cf49a43SJulian Elischer  * Find a node by absolute name. The name should NOT end with ':'
5744cf49a43SJulian Elischer  * The name "." means "this node" and "[xxx]" means "the node
5754cf49a43SJulian Elischer  * with ID (ie, at address) xxx".
5764cf49a43SJulian Elischer  *
5774cf49a43SJulian Elischer  * Returns the node if found, else NULL.
5784cf49a43SJulian Elischer  */
5794cf49a43SJulian Elischer node_p
5804cf49a43SJulian Elischer ng_findname(node_p this, const char *name)
5814cf49a43SJulian Elischer {
582dc90cad9SJulian Elischer 	node_p node;
583dc90cad9SJulian Elischer 	ng_ID_t temp;
5844cf49a43SJulian Elischer 
5854cf49a43SJulian Elischer 	/* "." means "this node" */
5864cf49a43SJulian Elischer 	if (strcmp(name, ".") == 0)
5874cf49a43SJulian Elischer 		return(this);
5884cf49a43SJulian Elischer 
5894cf49a43SJulian Elischer 	/* Check for name-by-ID */
590dc90cad9SJulian Elischer 	if ((temp = ng_decodeidname(name)) != 0) {
591dc90cad9SJulian Elischer 		return (ng_ID2node(temp));
5924cf49a43SJulian Elischer 	}
5934cf49a43SJulian Elischer 
5944cf49a43SJulian Elischer 	/* Find node by name */
5954cf49a43SJulian Elischer 	LIST_FOREACH(node, &nodelist, nodes) {
5964cf49a43SJulian Elischer 		if (node->name != NULL && strcmp(node->name, name) == 0)
5974cf49a43SJulian Elischer 			break;
5984cf49a43SJulian Elischer 	}
5994cf49a43SJulian Elischer 	return (node);
6004cf49a43SJulian Elischer }
6014cf49a43SJulian Elischer 
6024cf49a43SJulian Elischer /*
603dc90cad9SJulian Elischer  * Decode a ID name, eg. "[f03034de]". Returns 0 if the
604dc90cad9SJulian Elischer  * string is not valid, otherwise returns the value.
6054cf49a43SJulian Elischer  */
606dc90cad9SJulian Elischer static ng_ID_t
6074cf49a43SJulian Elischer ng_decodeidname(const char *name)
6084cf49a43SJulian Elischer {
6092b70adcbSArchie Cobbs 	const int len = strlen(name);
61025792ef3SArchie Cobbs 	char *eptr;
6112b70adcbSArchie Cobbs 	u_long val;
6124cf49a43SJulian Elischer 
6132b70adcbSArchie Cobbs 	/* Check for proper length, brackets, no leading junk */
6142b70adcbSArchie Cobbs 	if (len < 3 || name[0] != '[' || name[len - 1] != ']'
615ef050c81SJulian Elischer 	    || !isxdigit(name[1]))
6162b70adcbSArchie Cobbs 		return (0);
6174cf49a43SJulian Elischer 
6182b70adcbSArchie Cobbs 	/* Decode number */
6192b70adcbSArchie Cobbs 	val = strtoul(name + 1, &eptr, 16);
620ef050c81SJulian Elischer 	if (eptr - name != len - 1 || val == ULONG_MAX || val == 0)
62112f035e0SJulian Elischer 		return ((ng_ID_t)0);
6222b70adcbSArchie Cobbs 	return (ng_ID_t)val;
6234cf49a43SJulian Elischer }
6244cf49a43SJulian Elischer 
6254cf49a43SJulian Elischer /*
6264cf49a43SJulian Elischer  * Remove a name from a node. This should only be called
6274cf49a43SJulian Elischer  * when shutting down and removing the node.
6284cf49a43SJulian Elischer  */
6294cf49a43SJulian Elischer void
6304cf49a43SJulian Elischer ng_unname(node_p node)
6314cf49a43SJulian Elischer {
6324cf49a43SJulian Elischer 	if (node->name) {
6334cf49a43SJulian Elischer 		FREE(node->name, M_NETGRAPH);
6344cf49a43SJulian Elischer 		node->name = NULL;
6354cf49a43SJulian Elischer 		ng_unref(node);
6364cf49a43SJulian Elischer 	}
6374cf49a43SJulian Elischer }
6384cf49a43SJulian Elischer 
6394cf49a43SJulian Elischer /************************************************************************
6404cf49a43SJulian Elischer 			Hook routines
6414cf49a43SJulian Elischer 
6424cf49a43SJulian Elischer  Names are not optional. Hooks are always connected, except for a
6434cf49a43SJulian Elischer  brief moment within these routines.
6444cf49a43SJulian Elischer 
6454cf49a43SJulian Elischer ************************************************************************/
6464cf49a43SJulian Elischer 
6474cf49a43SJulian Elischer /*
6484cf49a43SJulian Elischer  * Remove a hook reference
6494cf49a43SJulian Elischer  */
6504cf49a43SJulian Elischer static void
6514cf49a43SJulian Elischer ng_unref_hook(hook_p hook)
6524cf49a43SJulian Elischer {
6534cf49a43SJulian Elischer 	if (--hook->refs == 0)
6544cf49a43SJulian Elischer 		FREE(hook, M_NETGRAPH);
6554cf49a43SJulian Elischer }
6564cf49a43SJulian Elischer 
6574cf49a43SJulian Elischer /*
6584cf49a43SJulian Elischer  * Add an unconnected hook to a node. Only used internally.
6594cf49a43SJulian Elischer  */
6604cf49a43SJulian Elischer static int
6614cf49a43SJulian Elischer ng_add_hook(node_p node, const char *name, hook_p *hookp)
6624cf49a43SJulian Elischer {
6634cf49a43SJulian Elischer 	hook_p hook;
6644cf49a43SJulian Elischer 	int error = 0;
6654cf49a43SJulian Elischer 
6664cf49a43SJulian Elischer 	/* Check that the given name is good */
6674cf49a43SJulian Elischer 	if (name == NULL) {
6684cf49a43SJulian Elischer 		TRAP_ERROR;
6694cf49a43SJulian Elischer 		return (EINVAL);
6704cf49a43SJulian Elischer 	}
671899e9c4eSArchie Cobbs 	if (ng_findhook(node, name) != NULL) {
6724cf49a43SJulian Elischer 		TRAP_ERROR;
6734cf49a43SJulian Elischer 		return (EEXIST);
6744cf49a43SJulian Elischer 	}
6754cf49a43SJulian Elischer 
6764cf49a43SJulian Elischer 	/* Allocate the hook and link it up */
677d2ea40c2SArchie Cobbs 	MALLOC(hook, hook_p, sizeof(*hook), M_NETGRAPH, M_NOWAIT);
6784cf49a43SJulian Elischer 	if (hook == NULL) {
6794cf49a43SJulian Elischer 		TRAP_ERROR;
6804cf49a43SJulian Elischer 		return (ENOMEM);
6814cf49a43SJulian Elischer 	}
6824cf49a43SJulian Elischer 	bzero(hook, sizeof(*hook));
6834cf49a43SJulian Elischer 	hook->refs = 1;
6844cf49a43SJulian Elischer 	hook->flags = HK_INVALID;
6854cf49a43SJulian Elischer 	hook->node = node;
6864cf49a43SJulian Elischer 	node->refs++;		/* each hook counts as a reference */
6874cf49a43SJulian Elischer 
6884cf49a43SJulian Elischer 	/* Check if the node type code has something to say about it */
6894cf49a43SJulian Elischer 	if (node->type->newhook != NULL)
6904cf49a43SJulian Elischer 		if ((error = (*node->type->newhook)(node, hook, name)) != 0)
6914cf49a43SJulian Elischer 			goto fail;
6924cf49a43SJulian Elischer 
6934cf49a43SJulian Elischer 	/*
6944cf49a43SJulian Elischer 	 * The 'type' agrees so far, so go ahead and link it in.
6954cf49a43SJulian Elischer 	 * We'll ask again later when we actually connect the hooks.
6964cf49a43SJulian Elischer 	 */
6974cf49a43SJulian Elischer 	LIST_INSERT_HEAD(&node->hooks, hook, hooks);
6984cf49a43SJulian Elischer 	node->numhooks++;
6994cf49a43SJulian Elischer 
7004cf49a43SJulian Elischer 	/* Set hook name */
701d2ea40c2SArchie Cobbs 	MALLOC(hook->name, char *, strlen(name) + 1, M_NETGRAPH, M_NOWAIT);
7024cf49a43SJulian Elischer 	if (hook->name == NULL) {
7034cf49a43SJulian Elischer 		error = ENOMEM;
7044cf49a43SJulian Elischer 		LIST_REMOVE(hook, hooks);
7054cf49a43SJulian Elischer 		node->numhooks--;
7064cf49a43SJulian Elischer fail:
7074cf49a43SJulian Elischer 		hook->node = NULL;
7084cf49a43SJulian Elischer 		ng_unref(node);
7094cf49a43SJulian Elischer 		ng_unref_hook(hook);	/* this frees the hook */
7104cf49a43SJulian Elischer 		return (error);
7114cf49a43SJulian Elischer 	}
7124cf49a43SJulian Elischer 	strcpy(hook->name, name);
7134cf49a43SJulian Elischer 	if (hookp)
7144cf49a43SJulian Elischer 		*hookp = hook;
7154cf49a43SJulian Elischer 	return (error);
7164cf49a43SJulian Elischer }
7174cf49a43SJulian Elischer 
7184cf49a43SJulian Elischer /*
7194cf49a43SJulian Elischer  * Connect a pair of hooks. Only used internally.
7204cf49a43SJulian Elischer  */
7214cf49a43SJulian Elischer static int
7224cf49a43SJulian Elischer ng_connect(hook_p hook1, hook_p hook2)
7234cf49a43SJulian Elischer {
7244cf49a43SJulian Elischer 	int     error;
7254cf49a43SJulian Elischer 
7264cf49a43SJulian Elischer 	hook1->peer = hook2;
7274cf49a43SJulian Elischer 	hook2->peer = hook1;
7284cf49a43SJulian Elischer 
7294cf49a43SJulian Elischer 	/* Give each node the opportunity to veto the impending connection */
7304cf49a43SJulian Elischer 	if (hook1->node->type->connect) {
7314cf49a43SJulian Elischer 		if ((error = (*hook1->node->type->connect) (hook1))) {
7324cf49a43SJulian Elischer 			ng_destroy_hook(hook1);	/* also zaps hook2 */
7334cf49a43SJulian Elischer 			return (error);
7344cf49a43SJulian Elischer 		}
7354cf49a43SJulian Elischer 	}
7364cf49a43SJulian Elischer 	if (hook2->node->type->connect) {
7374cf49a43SJulian Elischer 		if ((error = (*hook2->node->type->connect) (hook2))) {
7384cf49a43SJulian Elischer 			ng_destroy_hook(hook2);	/* also zaps hook1 */
7394cf49a43SJulian Elischer 			return (error);
7404cf49a43SJulian Elischer 		}
7414cf49a43SJulian Elischer 	}
7424cf49a43SJulian Elischer 	hook1->flags &= ~HK_INVALID;
7434cf49a43SJulian Elischer 	hook2->flags &= ~HK_INVALID;
7444cf49a43SJulian Elischer 	return (0);
7454cf49a43SJulian Elischer }
7464cf49a43SJulian Elischer 
7474cf49a43SJulian Elischer /*
748899e9c4eSArchie Cobbs  * Find a hook
749899e9c4eSArchie Cobbs  *
750899e9c4eSArchie Cobbs  * Node types may supply their own optimized routines for finding
751899e9c4eSArchie Cobbs  * hooks.  If none is supplied, we just do a linear search.
752899e9c4eSArchie Cobbs  */
753899e9c4eSArchie Cobbs hook_p
754899e9c4eSArchie Cobbs ng_findhook(node_p node, const char *name)
755899e9c4eSArchie Cobbs {
756899e9c4eSArchie Cobbs 	hook_p hook;
757899e9c4eSArchie Cobbs 
758899e9c4eSArchie Cobbs 	if (node->type->findhook != NULL)
759899e9c4eSArchie Cobbs 		return (*node->type->findhook)(node, name);
760899e9c4eSArchie Cobbs 	LIST_FOREACH(hook, &node->hooks, hooks) {
761899e9c4eSArchie Cobbs 		if (hook->name != NULL && strcmp(hook->name, name) == 0)
762899e9c4eSArchie Cobbs 			return (hook);
763899e9c4eSArchie Cobbs 	}
764899e9c4eSArchie Cobbs 	return (NULL);
765899e9c4eSArchie Cobbs }
766899e9c4eSArchie Cobbs 
767899e9c4eSArchie Cobbs /*
7684cf49a43SJulian Elischer  * Destroy a hook
7694cf49a43SJulian Elischer  *
7704cf49a43SJulian Elischer  * As hooks are always attached, this really destroys two hooks.
7714cf49a43SJulian Elischer  * The one given, and the one attached to it. Disconnect the hooks
7724cf49a43SJulian Elischer  * from each other first.
7734cf49a43SJulian Elischer  */
7744cf49a43SJulian Elischer void
7754cf49a43SJulian Elischer ng_destroy_hook(hook_p hook)
7764cf49a43SJulian Elischer {
7774cf49a43SJulian Elischer 	hook_p peer = hook->peer;
7784cf49a43SJulian Elischer 
7794cf49a43SJulian Elischer 	hook->flags |= HK_INVALID;		/* as soon as possible */
7804cf49a43SJulian Elischer 	if (peer) {
7814cf49a43SJulian Elischer 		peer->flags |= HK_INVALID;	/* as soon as possible */
7824cf49a43SJulian Elischer 		hook->peer = NULL;
7834cf49a43SJulian Elischer 		peer->peer = NULL;
7844cf49a43SJulian Elischer 		ng_disconnect_hook(peer);
7854cf49a43SJulian Elischer 	}
7864cf49a43SJulian Elischer 	ng_disconnect_hook(hook);
7874cf49a43SJulian Elischer }
7884cf49a43SJulian Elischer 
7894cf49a43SJulian Elischer /*
7904cf49a43SJulian Elischer  * Notify the node of the hook's demise. This may result in more actions
7914cf49a43SJulian Elischer  * (e.g. shutdown) but we don't do that ourselves and don't know what
7924cf49a43SJulian Elischer  * happens there. If there is no appropriate handler, then just remove it
7934cf49a43SJulian Elischer  * (and decrement the reference count of it's node which in turn might
7944cf49a43SJulian Elischer  * make something happen).
7954cf49a43SJulian Elischer  */
7964cf49a43SJulian Elischer static void
7974cf49a43SJulian Elischer ng_disconnect_hook(hook_p hook)
7984cf49a43SJulian Elischer {
7994cf49a43SJulian Elischer 	node_p node = hook->node;
8004cf49a43SJulian Elischer 
8014cf49a43SJulian Elischer 	/*
8024cf49a43SJulian Elischer 	 * Remove the hook from the node's list to avoid possible recursion
8034cf49a43SJulian Elischer 	 * in case the disconnection results in node shutdown.
8044cf49a43SJulian Elischer 	 */
8054cf49a43SJulian Elischer 	LIST_REMOVE(hook, hooks);
8064cf49a43SJulian Elischer 	node->numhooks--;
8074cf49a43SJulian Elischer 	if (node->type->disconnect) {
8084cf49a43SJulian Elischer 		/*
8094cf49a43SJulian Elischer 		 * The type handler may elect to destroy the peer so don't
8104cf49a43SJulian Elischer 		 * trust its existance after this point.
8114cf49a43SJulian Elischer 		 */
8124cf49a43SJulian Elischer 		(*node->type->disconnect) (hook);
8134cf49a43SJulian Elischer 	}
8144cf49a43SJulian Elischer 	ng_unref(node);		/* might be the last reference */
8154cf49a43SJulian Elischer 	if (hook->name)
8164cf49a43SJulian Elischer 		FREE(hook->name, M_NETGRAPH);
8174cf49a43SJulian Elischer 	hook->node = NULL;	/* may still be referenced elsewhere */
8184cf49a43SJulian Elischer 	ng_unref_hook(hook);
8194cf49a43SJulian Elischer }
8204cf49a43SJulian Elischer 
8214cf49a43SJulian Elischer /*
8224cf49a43SJulian Elischer  * Take two hooks on a node and merge the connection so that the given node
8234cf49a43SJulian Elischer  * is effectively bypassed.
8244cf49a43SJulian Elischer  */
8254cf49a43SJulian Elischer int
8264cf49a43SJulian Elischer ng_bypass(hook_p hook1, hook_p hook2)
8274cf49a43SJulian Elischer {
8284cf49a43SJulian Elischer 	if (hook1->node != hook2->node)
8294cf49a43SJulian Elischer 		return (EINVAL);
8304cf49a43SJulian Elischer 	hook1->peer->peer = hook2->peer;
8314cf49a43SJulian Elischer 	hook2->peer->peer = hook1->peer;
8324cf49a43SJulian Elischer 
8334cf49a43SJulian Elischer 	/* XXX If we ever cache methods on hooks update them as well */
8344cf49a43SJulian Elischer 	hook1->peer = NULL;
8354cf49a43SJulian Elischer 	hook2->peer = NULL;
8364cf49a43SJulian Elischer 	ng_destroy_hook(hook1);
8374cf49a43SJulian Elischer 	ng_destroy_hook(hook2);
8384cf49a43SJulian Elischer 	return (0);
8394cf49a43SJulian Elischer }
8404cf49a43SJulian Elischer 
8414cf49a43SJulian Elischer /*
8424cf49a43SJulian Elischer  * Install a new netgraph type
8434cf49a43SJulian Elischer  */
8444cf49a43SJulian Elischer int
8454cf49a43SJulian Elischer ng_newtype(struct ng_type *tp)
8464cf49a43SJulian Elischer {
8474cf49a43SJulian Elischer 	const size_t namelen = strlen(tp->name);
8484cf49a43SJulian Elischer 
8494cf49a43SJulian Elischer 	/* Check version and type name fields */
8504cf49a43SJulian Elischer 	if (tp->version != NG_VERSION || namelen == 0 || namelen > NG_TYPELEN) {
8514cf49a43SJulian Elischer 		TRAP_ERROR;
8524cf49a43SJulian Elischer 		return (EINVAL);
8534cf49a43SJulian Elischer 	}
8544cf49a43SJulian Elischer 
8554cf49a43SJulian Elischer 	/* Check for name collision */
8564cf49a43SJulian Elischer 	if (ng_findtype(tp->name) != NULL) {
8574cf49a43SJulian Elischer 		TRAP_ERROR;
8584cf49a43SJulian Elischer 		return (EEXIST);
8594cf49a43SJulian Elischer 	}
8604cf49a43SJulian Elischer 
8614cf49a43SJulian Elischer 	/* Link in new type */
8624cf49a43SJulian Elischer 	LIST_INSERT_HEAD(&typelist, tp, types);
8634cf49a43SJulian Elischer 	tp->refs = 0;
8644cf49a43SJulian Elischer 	return (0);
8654cf49a43SJulian Elischer }
8664cf49a43SJulian Elischer 
8674cf49a43SJulian Elischer /*
8684cf49a43SJulian Elischer  * Look for a type of the name given
8694cf49a43SJulian Elischer  */
8704cf49a43SJulian Elischer struct ng_type *
8714cf49a43SJulian Elischer ng_findtype(const char *typename)
8724cf49a43SJulian Elischer {
8734cf49a43SJulian Elischer 	struct ng_type *type;
8744cf49a43SJulian Elischer 
8754cf49a43SJulian Elischer 	LIST_FOREACH(type, &typelist, types) {
8764cf49a43SJulian Elischer 		if (strcmp(type->name, typename) == 0)
8774cf49a43SJulian Elischer 			break;
8784cf49a43SJulian Elischer 	}
8794cf49a43SJulian Elischer 	return (type);
8804cf49a43SJulian Elischer }
8814cf49a43SJulian Elischer 
8824cf49a43SJulian Elischer 
8834cf49a43SJulian Elischer /************************************************************************
8844cf49a43SJulian Elischer 			Composite routines
8854cf49a43SJulian Elischer ************************************************************************/
8864cf49a43SJulian Elischer 
8874cf49a43SJulian Elischer /*
8884cf49a43SJulian Elischer  * Make a peer and connect. The order is arranged to minimise
8894cf49a43SJulian Elischer  * the work needed to back out in case of error.
8904cf49a43SJulian Elischer  */
8914cf49a43SJulian Elischer int
8924cf49a43SJulian Elischer ng_mkpeer(node_p node, const char *name, const char *name2, char *type)
8934cf49a43SJulian Elischer {
8944cf49a43SJulian Elischer 	node_p  node2;
8954cf49a43SJulian Elischer 	hook_p  hook;
8964cf49a43SJulian Elischer 	hook_p  hook2;
8974cf49a43SJulian Elischer 	int     error;
8984cf49a43SJulian Elischer 
8994cf49a43SJulian Elischer 	if ((error = ng_add_hook(node, name, &hook)))
9004cf49a43SJulian Elischer 		return (error);
9014cf49a43SJulian Elischer 	if ((error = ng_make_node(type, &node2))) {
9024cf49a43SJulian Elischer 		ng_destroy_hook(hook);
9034cf49a43SJulian Elischer 		return (error);
9044cf49a43SJulian Elischer 	}
9054cf49a43SJulian Elischer 	if ((error = ng_add_hook(node2, name2, &hook2))) {
9064cf49a43SJulian Elischer 		ng_rmnode(node2);
9074cf49a43SJulian Elischer 		ng_destroy_hook(hook);
9084cf49a43SJulian Elischer 		return (error);
9094cf49a43SJulian Elischer 	}
9104cf49a43SJulian Elischer 
9114cf49a43SJulian Elischer 	/*
9124cf49a43SJulian Elischer 	 * Actually link the two hooks together.. on failure they are
9134cf49a43SJulian Elischer 	 * destroyed so we don't have to do that here.
9144cf49a43SJulian Elischer 	 */
9154cf49a43SJulian Elischer 	if ((error = ng_connect(hook, hook2)))
9164cf49a43SJulian Elischer 		ng_rmnode(node2);
9174cf49a43SJulian Elischer 	return (error);
9184cf49a43SJulian Elischer }
9194cf49a43SJulian Elischer 
9204cf49a43SJulian Elischer /*
9214cf49a43SJulian Elischer  * Connect two nodes using the specified hooks
9224cf49a43SJulian Elischer  */
9234cf49a43SJulian Elischer int
9244cf49a43SJulian Elischer ng_con_nodes(node_p node, const char *name, node_p node2, const char *name2)
9254cf49a43SJulian Elischer {
9264cf49a43SJulian Elischer 	int     error;
9274cf49a43SJulian Elischer 	hook_p  hook;
9284cf49a43SJulian Elischer 	hook_p  hook2;
9294cf49a43SJulian Elischer 
9304cf49a43SJulian Elischer 	if ((error = ng_add_hook(node, name, &hook)))
9314cf49a43SJulian Elischer 		return (error);
9324cf49a43SJulian Elischer 	if ((error = ng_add_hook(node2, name2, &hook2))) {
9334cf49a43SJulian Elischer 		ng_destroy_hook(hook);
9344cf49a43SJulian Elischer 		return (error);
9354cf49a43SJulian Elischer 	}
9364cf49a43SJulian Elischer 	return (ng_connect(hook, hook2));
9374cf49a43SJulian Elischer }
9384cf49a43SJulian Elischer 
9394cf49a43SJulian Elischer /*
9404cf49a43SJulian Elischer  * Parse and verify a string of the form:  <NODE:><PATH>
9414cf49a43SJulian Elischer  *
9424cf49a43SJulian Elischer  * Such a string can refer to a specific node or a specific hook
9434cf49a43SJulian Elischer  * on a specific node, depending on how you look at it. In the
9444cf49a43SJulian Elischer  * latter case, the PATH component must not end in a dot.
9454cf49a43SJulian Elischer  *
9464cf49a43SJulian Elischer  * Both <NODE:> and <PATH> are optional. The <PATH> is a string
9474cf49a43SJulian Elischer  * of hook names separated by dots. This breaks out the original
9484cf49a43SJulian Elischer  * string, setting *nodep to "NODE" (or NULL if none) and *pathp
9494cf49a43SJulian Elischer  * to "PATH" (or NULL if degenerate). Also, *hookp will point to
9504cf49a43SJulian Elischer  * the final hook component of <PATH>, if any, otherwise NULL.
9514cf49a43SJulian Elischer  *
9524cf49a43SJulian Elischer  * This returns -1 if the path is malformed. The char ** are optional.
9534cf49a43SJulian Elischer  */
9544cf49a43SJulian Elischer 
9554cf49a43SJulian Elischer int
9564cf49a43SJulian Elischer ng_path_parse(char *addr, char **nodep, char **pathp, char **hookp)
9574cf49a43SJulian Elischer {
9584cf49a43SJulian Elischer 	char   *node, *path, *hook;
9594cf49a43SJulian Elischer 	int     k;
9604cf49a43SJulian Elischer 
9614cf49a43SJulian Elischer 	/*
9624cf49a43SJulian Elischer 	 * Extract absolute NODE, if any
9634cf49a43SJulian Elischer 	 */
9644cf49a43SJulian Elischer 	for (path = addr; *path && *path != ':'; path++);
9654cf49a43SJulian Elischer 	if (*path) {
9664cf49a43SJulian Elischer 		node = addr;	/* Here's the NODE */
9674cf49a43SJulian Elischer 		*path++ = '\0';	/* Here's the PATH */
9684cf49a43SJulian Elischer 
9694cf49a43SJulian Elischer 		/* Node name must not be empty */
9704cf49a43SJulian Elischer 		if (!*node)
9714cf49a43SJulian Elischer 			return -1;
9724cf49a43SJulian Elischer 
9734cf49a43SJulian Elischer 		/* A name of "." is OK; otherwise '.' not allowed */
9744cf49a43SJulian Elischer 		if (strcmp(node, ".") != 0) {
9754cf49a43SJulian Elischer 			for (k = 0; node[k]; k++)
9764cf49a43SJulian Elischer 				if (node[k] == '.')
9774cf49a43SJulian Elischer 					return -1;
9784cf49a43SJulian Elischer 		}
9794cf49a43SJulian Elischer 	} else {
9804cf49a43SJulian Elischer 		node = NULL;	/* No absolute NODE */
9814cf49a43SJulian Elischer 		path = addr;	/* Here's the PATH */
9824cf49a43SJulian Elischer 	}
9834cf49a43SJulian Elischer 
9844cf49a43SJulian Elischer 	/* Snoop for illegal characters in PATH */
9854cf49a43SJulian Elischer 	for (k = 0; path[k]; k++)
9864cf49a43SJulian Elischer 		if (path[k] == ':')
9874cf49a43SJulian Elischer 			return -1;
9884cf49a43SJulian Elischer 
9894cf49a43SJulian Elischer 	/* Check for no repeated dots in PATH */
9904cf49a43SJulian Elischer 	for (k = 0; path[k]; k++)
9914cf49a43SJulian Elischer 		if (path[k] == '.' && path[k + 1] == '.')
9924cf49a43SJulian Elischer 			return -1;
9934cf49a43SJulian Elischer 
9944cf49a43SJulian Elischer 	/* Remove extra (degenerate) dots from beginning or end of PATH */
9954cf49a43SJulian Elischer 	if (path[0] == '.')
9964cf49a43SJulian Elischer 		path++;
9974cf49a43SJulian Elischer 	if (*path && path[strlen(path) - 1] == '.')
9984cf49a43SJulian Elischer 		path[strlen(path) - 1] = 0;
9994cf49a43SJulian Elischer 
10004cf49a43SJulian Elischer 	/* If PATH has a dot, then we're not talking about a hook */
10014cf49a43SJulian Elischer 	if (*path) {
10024cf49a43SJulian Elischer 		for (hook = path, k = 0; path[k]; k++)
10034cf49a43SJulian Elischer 			if (path[k] == '.') {
10044cf49a43SJulian Elischer 				hook = NULL;
10054cf49a43SJulian Elischer 				break;
10064cf49a43SJulian Elischer 			}
10074cf49a43SJulian Elischer 	} else
10084cf49a43SJulian Elischer 		path = hook = NULL;
10094cf49a43SJulian Elischer 
10104cf49a43SJulian Elischer 	/* Done */
10114cf49a43SJulian Elischer 	if (nodep)
10124cf49a43SJulian Elischer 		*nodep = node;
10134cf49a43SJulian Elischer 	if (pathp)
10144cf49a43SJulian Elischer 		*pathp = path;
10154cf49a43SJulian Elischer 	if (hookp)
10164cf49a43SJulian Elischer 		*hookp = hook;
10174cf49a43SJulian Elischer 	return (0);
10184cf49a43SJulian Elischer }
10194cf49a43SJulian Elischer 
10204cf49a43SJulian Elischer /*
10214cf49a43SJulian Elischer  * Given a path, which may be absolute or relative, and a starting node,
10224cf49a43SJulian Elischer  * return the destination node. Compute the "return address" if desired.
10234cf49a43SJulian Elischer  */
10244cf49a43SJulian Elischer int
1025a4ec03cfSJulian Elischer ng_path2node(node_p here, const char *address, node_p *destp, char **rtnp,
1026a4ec03cfSJulian Elischer 	hook_p *lasthook)
10274cf49a43SJulian Elischer {
10284cf49a43SJulian Elischer 	const	node_p start = here;
10294cf49a43SJulian Elischer 	char    fullpath[NG_PATHLEN + 1];
10304cf49a43SJulian Elischer 	char   *nodename, *path, pbuf[2];
10314cf49a43SJulian Elischer 	node_p  node;
10324cf49a43SJulian Elischer 	char   *cp;
1033a4ec03cfSJulian Elischer 	hook_p hook = NULL;
10344cf49a43SJulian Elischer 
10354cf49a43SJulian Elischer 	/* Initialize */
10364cf49a43SJulian Elischer 	if (rtnp)
10374cf49a43SJulian Elischer 		*rtnp = NULL;
10384cf49a43SJulian Elischer 	if (destp == NULL)
10394cf49a43SJulian Elischer 		return EINVAL;
10404cf49a43SJulian Elischer 	*destp = NULL;
10414cf49a43SJulian Elischer 
10424cf49a43SJulian Elischer 	/* Make a writable copy of address for ng_path_parse() */
10434cf49a43SJulian Elischer 	strncpy(fullpath, address, sizeof(fullpath) - 1);
10444cf49a43SJulian Elischer 	fullpath[sizeof(fullpath) - 1] = '\0';
10454cf49a43SJulian Elischer 
10464cf49a43SJulian Elischer 	/* Parse out node and sequence of hooks */
10474cf49a43SJulian Elischer 	if (ng_path_parse(fullpath, &nodename, &path, NULL) < 0) {
10484cf49a43SJulian Elischer 		TRAP_ERROR;
10494cf49a43SJulian Elischer 		return EINVAL;
10504cf49a43SJulian Elischer 	}
10514cf49a43SJulian Elischer 	if (path == NULL) {
10524cf49a43SJulian Elischer 		pbuf[0] = '.';	/* Needs to be writable */
10534cf49a43SJulian Elischer 		pbuf[1] = '\0';
10544cf49a43SJulian Elischer 		path = pbuf;
10554cf49a43SJulian Elischer 	}
10564cf49a43SJulian Elischer 
10574cf49a43SJulian Elischer 	/* For an absolute address, jump to the starting node */
10584cf49a43SJulian Elischer 	if (nodename) {
10594cf49a43SJulian Elischer 		node = ng_findname(here, nodename);
10604cf49a43SJulian Elischer 		if (node == NULL) {
10614cf49a43SJulian Elischer 			TRAP_ERROR;
10624cf49a43SJulian Elischer 			return (ENOENT);
10634cf49a43SJulian Elischer 		}
10644cf49a43SJulian Elischer 	} else
10654cf49a43SJulian Elischer 		node = here;
10664cf49a43SJulian Elischer 
10674cf49a43SJulian Elischer 	/* Now follow the sequence of hooks */
10684cf49a43SJulian Elischer 	for (cp = path; node != NULL && *cp != '\0'; ) {
10694cf49a43SJulian Elischer 		char *segment;
10704cf49a43SJulian Elischer 
10714cf49a43SJulian Elischer 		/*
10724cf49a43SJulian Elischer 		 * Break out the next path segment. Replace the dot we just
10734cf49a43SJulian Elischer 		 * found with a NUL; "cp" points to the next segment (or the
10744cf49a43SJulian Elischer 		 * NUL at the end).
10754cf49a43SJulian Elischer 		 */
10764cf49a43SJulian Elischer 		for (segment = cp; *cp != '\0'; cp++) {
10774cf49a43SJulian Elischer 			if (*cp == '.') {
10784cf49a43SJulian Elischer 				*cp++ = '\0';
10794cf49a43SJulian Elischer 				break;
10804cf49a43SJulian Elischer 			}
10814cf49a43SJulian Elischer 		}
10824cf49a43SJulian Elischer 
10834cf49a43SJulian Elischer 		/* Empty segment */
10844cf49a43SJulian Elischer 		if (*segment == '\0')
10854cf49a43SJulian Elischer 			continue;
10864cf49a43SJulian Elischer 
10874cf49a43SJulian Elischer 		/* We have a segment, so look for a hook by that name */
1088899e9c4eSArchie Cobbs 		hook = ng_findhook(node, segment);
10894cf49a43SJulian Elischer 
10904cf49a43SJulian Elischer 		/* Can't get there from here... */
10914cf49a43SJulian Elischer 		if (hook == NULL
10924cf49a43SJulian Elischer 		    || hook->peer == NULL
10934cf49a43SJulian Elischer 		    || (hook->flags & HK_INVALID) != 0) {
10944cf49a43SJulian Elischer 			TRAP_ERROR;
10954cf49a43SJulian Elischer 			return (ENOENT);
10964cf49a43SJulian Elischer 		}
10974cf49a43SJulian Elischer 
10984cf49a43SJulian Elischer 		/* Hop on over to the next node */
10994cf49a43SJulian Elischer 		node = hook->peer->node;
11004cf49a43SJulian Elischer 	}
11014cf49a43SJulian Elischer 
11024cf49a43SJulian Elischer 	/* If node somehow missing, fail here (probably this is not needed) */
11034cf49a43SJulian Elischer 	if (node == NULL) {
11044cf49a43SJulian Elischer 		TRAP_ERROR;
11054cf49a43SJulian Elischer 		return (ENXIO);
11064cf49a43SJulian Elischer 	}
11074cf49a43SJulian Elischer 
11084cf49a43SJulian Elischer 	/* Now compute return address, i.e., the path to the sender */
11094cf49a43SJulian Elischer 	if (rtnp != NULL) {
1110d2ea40c2SArchie Cobbs 		MALLOC(*rtnp, char *, NG_NODELEN + 2, M_NETGRAPH, M_NOWAIT);
11114cf49a43SJulian Elischer 		if (*rtnp == NULL) {
11124cf49a43SJulian Elischer 			TRAP_ERROR;
11134cf49a43SJulian Elischer 			return (ENOMEM);
11144cf49a43SJulian Elischer 		}
11154cf49a43SJulian Elischer 		if (start->name != NULL)
11164cf49a43SJulian Elischer 			sprintf(*rtnp, "%s:", start->name);
11174cf49a43SJulian Elischer 		else
1118dc90cad9SJulian Elischer 			sprintf(*rtnp, "[%x]:", ng_node2ID(start));
11194cf49a43SJulian Elischer 	}
11204cf49a43SJulian Elischer 
11214cf49a43SJulian Elischer 	/* Done */
11224cf49a43SJulian Elischer 	*destp = node;
11231bdebe4dSArchie Cobbs 	if (lasthook != NULL)
11241bdebe4dSArchie Cobbs 		*lasthook = hook ? hook->peer : NULL;
11254cf49a43SJulian Elischer 	return (0);
11264cf49a43SJulian Elischer }
11274cf49a43SJulian Elischer 
11284cf49a43SJulian Elischer /*
11294cf49a43SJulian Elischer  * Call the appropriate message handler for the object.
11304cf49a43SJulian Elischer  * It is up to the message handler to free the message.
11314cf49a43SJulian Elischer  * If it's a generic message, handle it generically, otherwise
11324cf49a43SJulian Elischer  * call the type's message handler (if it exists)
11334cf49a43SJulian Elischer  */
11344cf49a43SJulian Elischer 
1135a4ec03cfSJulian Elischer #define CALL_MSG_HANDLER(error, node, msg, retaddr, resp, hook)		\
11364cf49a43SJulian Elischer do {									\
11374cf49a43SJulian Elischer 	if((msg)->header.typecookie == NGM_GENERIC_COOKIE) {		\
11384cf49a43SJulian Elischer 		(error) = ng_generic_msg((node), (msg),			\
1139a4ec03cfSJulian Elischer 			(retaddr), (resp), (hook));			\
11404cf49a43SJulian Elischer 	} else {							\
11414cf49a43SJulian Elischer 		if ((node)->type->rcvmsg != NULL) {			\
11424cf49a43SJulian Elischer 			(error) = (*(node)->type->rcvmsg)((node),	\
1143a4ec03cfSJulian Elischer 				(msg), (retaddr), (resp), (hook));	\
11444cf49a43SJulian Elischer 		} else {						\
11454cf49a43SJulian Elischer 			TRAP_ERROR;					\
11464cf49a43SJulian Elischer 			FREE((msg), M_NETGRAPH);			\
11474cf49a43SJulian Elischer 			(error) = EINVAL;				\
11484cf49a43SJulian Elischer 		}							\
11494cf49a43SJulian Elischer 	}								\
11504cf49a43SJulian Elischer } while (0)
11514cf49a43SJulian Elischer 
11524cf49a43SJulian Elischer 
11534cf49a43SJulian Elischer /*
11544cf49a43SJulian Elischer  * Send a control message to a node
11554cf49a43SJulian Elischer  */
11564cf49a43SJulian Elischer int
11574cf49a43SJulian Elischer ng_send_msg(node_p here, struct ng_mesg *msg, const char *address,
11584cf49a43SJulian Elischer 	    struct ng_mesg **rptr)
11594cf49a43SJulian Elischer {
11604cf49a43SJulian Elischer 	node_p  dest = NULL;
11614cf49a43SJulian Elischer 	char   *retaddr = NULL;
11624cf49a43SJulian Elischer 	int     error;
1163a4ec03cfSJulian Elischer 	hook_p	lasthook;
11644cf49a43SJulian Elischer 
11654cf49a43SJulian Elischer 	/* Find the target node */
1166a4ec03cfSJulian Elischer 	error = ng_path2node(here, address, &dest, &retaddr, &lasthook);
11674cf49a43SJulian Elischer 	if (error) {
11684cf49a43SJulian Elischer 		FREE(msg, M_NETGRAPH);
11694cf49a43SJulian Elischer 		return error;
11704cf49a43SJulian Elischer 	}
11714cf49a43SJulian Elischer 
11724cf49a43SJulian Elischer 	/* Make sure the resp field is null before we start */
11734cf49a43SJulian Elischer 	if (rptr != NULL)
11744cf49a43SJulian Elischer 		*rptr = NULL;
11754cf49a43SJulian Elischer 
1176a4ec03cfSJulian Elischer 	CALL_MSG_HANDLER(error, dest, msg, retaddr, rptr, lasthook);
11774cf49a43SJulian Elischer 
11784cf49a43SJulian Elischer 	/* Make sure that if there is a response, it has the RESP bit set */
11794cf49a43SJulian Elischer 	if ((error == 0) && rptr && *rptr)
11804cf49a43SJulian Elischer 		(*rptr)->header.flags |= NGF_RESP;
11814cf49a43SJulian Elischer 
11824cf49a43SJulian Elischer 	/*
11834cf49a43SJulian Elischer 	 * If we had a return address it is up to us to free it. They should
11844cf49a43SJulian Elischer 	 * have taken a copy if they needed to make a delayed response.
11854cf49a43SJulian Elischer 	 */
11864cf49a43SJulian Elischer 	if (retaddr)
11874cf49a43SJulian Elischer 		FREE(retaddr, M_NETGRAPH);
11884cf49a43SJulian Elischer 	return (error);
11894cf49a43SJulian Elischer }
11904cf49a43SJulian Elischer 
11914cf49a43SJulian Elischer /*
11924cf49a43SJulian Elischer  * Implement the 'generic' control messages
11934cf49a43SJulian Elischer  */
11944cf49a43SJulian Elischer static int
11954cf49a43SJulian Elischer ng_generic_msg(node_p here, struct ng_mesg *msg, const char *retaddr,
1196a4ec03cfSJulian Elischer 	       struct ng_mesg **resp, hook_p lasthook)
11974cf49a43SJulian Elischer {
11984cf49a43SJulian Elischer 	int error = 0;
11994cf49a43SJulian Elischer 
12004cf49a43SJulian Elischer 	if (msg->header.typecookie != NGM_GENERIC_COOKIE) {
12014cf49a43SJulian Elischer 		TRAP_ERROR;
12024cf49a43SJulian Elischer 		FREE(msg, M_NETGRAPH);
12034cf49a43SJulian Elischer 		return (EINVAL);
12044cf49a43SJulian Elischer 	}
12054cf49a43SJulian Elischer 	switch (msg->header.cmd) {
12064cf49a43SJulian Elischer 	case NGM_SHUTDOWN:
12074cf49a43SJulian Elischer 		ng_rmnode(here);
12084cf49a43SJulian Elischer 		break;
12094cf49a43SJulian Elischer 	case NGM_MKPEER:
12104cf49a43SJulian Elischer 	    {
12114cf49a43SJulian Elischer 		struct ngm_mkpeer *const mkp = (struct ngm_mkpeer *) msg->data;
12124cf49a43SJulian Elischer 
12134cf49a43SJulian Elischer 		if (msg->header.arglen != sizeof(*mkp)) {
12144cf49a43SJulian Elischer 			TRAP_ERROR;
12154cf49a43SJulian Elischer 			return (EINVAL);
12164cf49a43SJulian Elischer 		}
12174cf49a43SJulian Elischer 		mkp->type[sizeof(mkp->type) - 1] = '\0';
12184cf49a43SJulian Elischer 		mkp->ourhook[sizeof(mkp->ourhook) - 1] = '\0';
12194cf49a43SJulian Elischer 		mkp->peerhook[sizeof(mkp->peerhook) - 1] = '\0';
12204cf49a43SJulian Elischer 		error = ng_mkpeer(here, mkp->ourhook, mkp->peerhook, mkp->type);
12214cf49a43SJulian Elischer 		break;
12224cf49a43SJulian Elischer 	    }
12234cf49a43SJulian Elischer 	case NGM_CONNECT:
12244cf49a43SJulian Elischer 	    {
12254cf49a43SJulian Elischer 		struct ngm_connect *const con =
12264cf49a43SJulian Elischer 			(struct ngm_connect *) msg->data;
12274cf49a43SJulian Elischer 		node_p node2;
12284cf49a43SJulian Elischer 
12294cf49a43SJulian Elischer 		if (msg->header.arglen != sizeof(*con)) {
12304cf49a43SJulian Elischer 			TRAP_ERROR;
12314cf49a43SJulian Elischer 			return (EINVAL);
12324cf49a43SJulian Elischer 		}
12334cf49a43SJulian Elischer 		con->path[sizeof(con->path) - 1] = '\0';
12344cf49a43SJulian Elischer 		con->ourhook[sizeof(con->ourhook) - 1] = '\0';
12354cf49a43SJulian Elischer 		con->peerhook[sizeof(con->peerhook) - 1] = '\0';
1236a4ec03cfSJulian Elischer 		error = ng_path2node(here, con->path, &node2, NULL, NULL);
12374cf49a43SJulian Elischer 		if (error)
12384cf49a43SJulian Elischer 			break;
12394cf49a43SJulian Elischer 		error = ng_con_nodes(here, con->ourhook, node2, con->peerhook);
12404cf49a43SJulian Elischer 		break;
12414cf49a43SJulian Elischer 	    }
12424cf49a43SJulian Elischer 	case NGM_NAME:
12434cf49a43SJulian Elischer 	    {
12444cf49a43SJulian Elischer 		struct ngm_name *const nam = (struct ngm_name *) msg->data;
12454cf49a43SJulian Elischer 
12464cf49a43SJulian Elischer 		if (msg->header.arglen != sizeof(*nam)) {
12474cf49a43SJulian Elischer 			TRAP_ERROR;
12484cf49a43SJulian Elischer 			return (EINVAL);
12494cf49a43SJulian Elischer 		}
12504cf49a43SJulian Elischer 		nam->name[sizeof(nam->name) - 1] = '\0';
12514cf49a43SJulian Elischer 		error = ng_name_node(here, nam->name);
12524cf49a43SJulian Elischer 		break;
12534cf49a43SJulian Elischer 	    }
12544cf49a43SJulian Elischer 	case NGM_RMHOOK:
12554cf49a43SJulian Elischer 	    {
12564cf49a43SJulian Elischer 		struct ngm_rmhook *const rmh = (struct ngm_rmhook *) msg->data;
12574cf49a43SJulian Elischer 		hook_p hook;
12584cf49a43SJulian Elischer 
12594cf49a43SJulian Elischer 		if (msg->header.arglen != sizeof(*rmh)) {
12604cf49a43SJulian Elischer 			TRAP_ERROR;
12614cf49a43SJulian Elischer 			return (EINVAL);
12624cf49a43SJulian Elischer 		}
12634cf49a43SJulian Elischer 		rmh->ourhook[sizeof(rmh->ourhook) - 1] = '\0';
1264899e9c4eSArchie Cobbs 		if ((hook = ng_findhook(here, rmh->ourhook)) != NULL)
12654cf49a43SJulian Elischer 			ng_destroy_hook(hook);
12664cf49a43SJulian Elischer 		break;
12674cf49a43SJulian Elischer 	    }
12684cf49a43SJulian Elischer 	case NGM_NODEINFO:
12694cf49a43SJulian Elischer 	    {
12704cf49a43SJulian Elischer 		struct nodeinfo *ni;
12714cf49a43SJulian Elischer 		struct ng_mesg *rp;
12724cf49a43SJulian Elischer 
12734cf49a43SJulian Elischer 		/* Get response struct */
12744cf49a43SJulian Elischer 		if (resp == NULL) {
12754cf49a43SJulian Elischer 			error = EINVAL;
12764cf49a43SJulian Elischer 			break;
12774cf49a43SJulian Elischer 		}
12784cf49a43SJulian Elischer 		NG_MKRESPONSE(rp, msg, sizeof(*ni), M_NOWAIT);
12794cf49a43SJulian Elischer 		if (rp == NULL) {
12804cf49a43SJulian Elischer 			error = ENOMEM;
12814cf49a43SJulian Elischer 			break;
12824cf49a43SJulian Elischer 		}
12834cf49a43SJulian Elischer 
12844cf49a43SJulian Elischer 		/* Fill in node info */
12854cf49a43SJulian Elischer 		ni = (struct nodeinfo *) rp->data;
12864cf49a43SJulian Elischer 		if (here->name != NULL)
12874cf49a43SJulian Elischer 			strncpy(ni->name, here->name, NG_NODELEN);
12884cf49a43SJulian Elischer 		strncpy(ni->type, here->type->name, NG_TYPELEN);
1289dc90cad9SJulian Elischer 		ni->id = ng_node2ID(here);
12904cf49a43SJulian Elischer 		ni->hooks = here->numhooks;
12914cf49a43SJulian Elischer 		*resp = rp;
12924cf49a43SJulian Elischer 		break;
12934cf49a43SJulian Elischer 	    }
12944cf49a43SJulian Elischer 	case NGM_LISTHOOKS:
12954cf49a43SJulian Elischer 	    {
12964cf49a43SJulian Elischer 		const int nhooks = here->numhooks;
12974cf49a43SJulian Elischer 		struct hooklist *hl;
12984cf49a43SJulian Elischer 		struct nodeinfo *ni;
12994cf49a43SJulian Elischer 		struct ng_mesg *rp;
13004cf49a43SJulian Elischer 		hook_p hook;
13014cf49a43SJulian Elischer 
13024cf49a43SJulian Elischer 		/* Get response struct */
13034cf49a43SJulian Elischer 		if (resp == NULL) {
13044cf49a43SJulian Elischer 			error = EINVAL;
13054cf49a43SJulian Elischer 			break;
13064cf49a43SJulian Elischer 		}
13074cf49a43SJulian Elischer 		NG_MKRESPONSE(rp, msg, sizeof(*hl)
13084cf49a43SJulian Elischer 		    + (nhooks * sizeof(struct linkinfo)), M_NOWAIT);
13094cf49a43SJulian Elischer 		if (rp == NULL) {
13104cf49a43SJulian Elischer 			error = ENOMEM;
13114cf49a43SJulian Elischer 			break;
13124cf49a43SJulian Elischer 		}
13134cf49a43SJulian Elischer 		hl = (struct hooklist *) rp->data;
13144cf49a43SJulian Elischer 		ni = &hl->nodeinfo;
13154cf49a43SJulian Elischer 
13164cf49a43SJulian Elischer 		/* Fill in node info */
13174cf49a43SJulian Elischer 		if (here->name)
13184cf49a43SJulian Elischer 			strncpy(ni->name, here->name, NG_NODELEN);
13194cf49a43SJulian Elischer 		strncpy(ni->type, here->type->name, NG_TYPELEN);
1320dc90cad9SJulian Elischer 		ni->id = ng_node2ID(here);
13214cf49a43SJulian Elischer 
13224cf49a43SJulian Elischer 		/* Cycle through the linked list of hooks */
13234cf49a43SJulian Elischer 		ni->hooks = 0;
13244cf49a43SJulian Elischer 		LIST_FOREACH(hook, &here->hooks, hooks) {
13254cf49a43SJulian Elischer 			struct linkinfo *const link = &hl->link[ni->hooks];
13264cf49a43SJulian Elischer 
13274cf49a43SJulian Elischer 			if (ni->hooks >= nhooks) {
13284cf49a43SJulian Elischer 				log(LOG_ERR, "%s: number of %s changed\n",
13294cf49a43SJulian Elischer 				    __FUNCTION__, "hooks");
13304cf49a43SJulian Elischer 				break;
13314cf49a43SJulian Elischer 			}
13324cf49a43SJulian Elischer 			if ((hook->flags & HK_INVALID) != 0)
13334cf49a43SJulian Elischer 				continue;
13344cf49a43SJulian Elischer 			strncpy(link->ourhook, hook->name, NG_HOOKLEN);
13354cf49a43SJulian Elischer 			strncpy(link->peerhook, hook->peer->name, NG_HOOKLEN);
13364cf49a43SJulian Elischer 			if (hook->peer->node->name != NULL)
13374cf49a43SJulian Elischer 				strncpy(link->nodeinfo.name,
13384cf49a43SJulian Elischer 				    hook->peer->node->name, NG_NODELEN);
13394cf49a43SJulian Elischer 			strncpy(link->nodeinfo.type,
13404cf49a43SJulian Elischer 			   hook->peer->node->type->name, NG_TYPELEN);
1341dc90cad9SJulian Elischer 			link->nodeinfo.id = ng_node2ID(hook->peer->node);
13424cf49a43SJulian Elischer 			link->nodeinfo.hooks = hook->peer->node->numhooks;
13434cf49a43SJulian Elischer 			ni->hooks++;
13444cf49a43SJulian Elischer 		}
13454cf49a43SJulian Elischer 		*resp = rp;
13464cf49a43SJulian Elischer 		break;
13474cf49a43SJulian Elischer 	    }
13484cf49a43SJulian Elischer 
13494cf49a43SJulian Elischer 	case NGM_LISTNAMES:
13504cf49a43SJulian Elischer 	case NGM_LISTNODES:
13514cf49a43SJulian Elischer 	    {
13524cf49a43SJulian Elischer 		const int unnamed = (msg->header.cmd == NGM_LISTNODES);
13534cf49a43SJulian Elischer 		struct namelist *nl;
13544cf49a43SJulian Elischer 		struct ng_mesg *rp;
13554cf49a43SJulian Elischer 		node_p node;
13564cf49a43SJulian Elischer 		int num = 0;
13574cf49a43SJulian Elischer 
13584cf49a43SJulian Elischer 		if (resp == NULL) {
13594cf49a43SJulian Elischer 			error = EINVAL;
13604cf49a43SJulian Elischer 			break;
13614cf49a43SJulian Elischer 		}
13624cf49a43SJulian Elischer 
13634cf49a43SJulian Elischer 		/* Count number of nodes */
13644cf49a43SJulian Elischer 		LIST_FOREACH(node, &nodelist, nodes) {
13654cf49a43SJulian Elischer 			if (unnamed || node->name != NULL)
13664cf49a43SJulian Elischer 				num++;
13674cf49a43SJulian Elischer 		}
13684cf49a43SJulian Elischer 
13694cf49a43SJulian Elischer 		/* Get response struct */
13704cf49a43SJulian Elischer 		if (resp == NULL) {
13714cf49a43SJulian Elischer 			error = EINVAL;
13724cf49a43SJulian Elischer 			break;
13734cf49a43SJulian Elischer 		}
13744cf49a43SJulian Elischer 		NG_MKRESPONSE(rp, msg, sizeof(*nl)
13754cf49a43SJulian Elischer 		    + (num * sizeof(struct nodeinfo)), M_NOWAIT);
13764cf49a43SJulian Elischer 		if (rp == NULL) {
13774cf49a43SJulian Elischer 			error = ENOMEM;
13784cf49a43SJulian Elischer 			break;
13794cf49a43SJulian Elischer 		}
13804cf49a43SJulian Elischer 		nl = (struct namelist *) rp->data;
13814cf49a43SJulian Elischer 
13824cf49a43SJulian Elischer 		/* Cycle through the linked list of nodes */
13834cf49a43SJulian Elischer 		nl->numnames = 0;
13844cf49a43SJulian Elischer 		LIST_FOREACH(node, &nodelist, nodes) {
13854cf49a43SJulian Elischer 			struct nodeinfo *const np = &nl->nodeinfo[nl->numnames];
13864cf49a43SJulian Elischer 
13874cf49a43SJulian Elischer 			if (nl->numnames >= num) {
13884cf49a43SJulian Elischer 				log(LOG_ERR, "%s: number of %s changed\n",
13894cf49a43SJulian Elischer 				    __FUNCTION__, "nodes");
13904cf49a43SJulian Elischer 				break;
13914cf49a43SJulian Elischer 			}
13924cf49a43SJulian Elischer 			if ((node->flags & NG_INVALID) != 0)
13934cf49a43SJulian Elischer 				continue;
13944cf49a43SJulian Elischer 			if (!unnamed && node->name == NULL)
13954cf49a43SJulian Elischer 				continue;
13964cf49a43SJulian Elischer 			if (node->name != NULL)
13974cf49a43SJulian Elischer 				strncpy(np->name, node->name, NG_NODELEN);
13984cf49a43SJulian Elischer 			strncpy(np->type, node->type->name, NG_TYPELEN);
1399dc90cad9SJulian Elischer 			np->id = ng_node2ID(node);
14004cf49a43SJulian Elischer 			np->hooks = node->numhooks;
14014cf49a43SJulian Elischer 			nl->numnames++;
14024cf49a43SJulian Elischer 		}
14034cf49a43SJulian Elischer 		*resp = rp;
14044cf49a43SJulian Elischer 		break;
14054cf49a43SJulian Elischer 	    }
14064cf49a43SJulian Elischer 
14074cf49a43SJulian Elischer 	case NGM_LISTTYPES:
14084cf49a43SJulian Elischer 	    {
14094cf49a43SJulian Elischer 		struct typelist *tl;
14104cf49a43SJulian Elischer 		struct ng_mesg *rp;
14114cf49a43SJulian Elischer 		struct ng_type *type;
14124cf49a43SJulian Elischer 		int num = 0;
14134cf49a43SJulian Elischer 
14144cf49a43SJulian Elischer 		if (resp == NULL) {
14154cf49a43SJulian Elischer 			error = EINVAL;
14164cf49a43SJulian Elischer 			break;
14174cf49a43SJulian Elischer 		}
14184cf49a43SJulian Elischer 
14194cf49a43SJulian Elischer 		/* Count number of types */
14204cf49a43SJulian Elischer 		LIST_FOREACH(type, &typelist, types)
14214cf49a43SJulian Elischer 			num++;
14224cf49a43SJulian Elischer 
14234cf49a43SJulian Elischer 		/* Get response struct */
14244cf49a43SJulian Elischer 		if (resp == NULL) {
14254cf49a43SJulian Elischer 			error = EINVAL;
14264cf49a43SJulian Elischer 			break;
14274cf49a43SJulian Elischer 		}
14284cf49a43SJulian Elischer 		NG_MKRESPONSE(rp, msg, sizeof(*tl)
14294cf49a43SJulian Elischer 		    + (num * sizeof(struct typeinfo)), M_NOWAIT);
14304cf49a43SJulian Elischer 		if (rp == NULL) {
14314cf49a43SJulian Elischer 			error = ENOMEM;
14324cf49a43SJulian Elischer 			break;
14334cf49a43SJulian Elischer 		}
14344cf49a43SJulian Elischer 		tl = (struct typelist *) rp->data;
14354cf49a43SJulian Elischer 
14364cf49a43SJulian Elischer 		/* Cycle through the linked list of types */
14374cf49a43SJulian Elischer 		tl->numtypes = 0;
14384cf49a43SJulian Elischer 		LIST_FOREACH(type, &typelist, types) {
14394cf49a43SJulian Elischer 			struct typeinfo *const tp = &tl->typeinfo[tl->numtypes];
14404cf49a43SJulian Elischer 
14414cf49a43SJulian Elischer 			if (tl->numtypes >= num) {
14424cf49a43SJulian Elischer 				log(LOG_ERR, "%s: number of %s changed\n",
14434cf49a43SJulian Elischer 				    __FUNCTION__, "types");
14444cf49a43SJulian Elischer 				break;
14454cf49a43SJulian Elischer 			}
1446a096e45aSArchie Cobbs 			strncpy(tp->type_name, type->name, NG_TYPELEN);
14474cf49a43SJulian Elischer 			tp->numnodes = type->refs;
14484cf49a43SJulian Elischer 			tl->numtypes++;
14494cf49a43SJulian Elischer 		}
14504cf49a43SJulian Elischer 		*resp = rp;
14514cf49a43SJulian Elischer 		break;
14524cf49a43SJulian Elischer 	    }
14534cf49a43SJulian Elischer 
1454f8307e12SArchie Cobbs 	case NGM_BINARY2ASCII:
1455f8307e12SArchie Cobbs 	    {
14567133ac27SArchie Cobbs 		int bufSize = 20 * 1024;	/* XXX hard coded constant */
1457f8307e12SArchie Cobbs 		const struct ng_parse_type *argstype;
1458f8307e12SArchie Cobbs 		const struct ng_cmdlist *c;
1459f8307e12SArchie Cobbs 		struct ng_mesg *rp, *binary, *ascii;
1460f8307e12SArchie Cobbs 
1461f8307e12SArchie Cobbs 		/* Data area must contain a valid netgraph message */
1462f8307e12SArchie Cobbs 		binary = (struct ng_mesg *)msg->data;
1463f8307e12SArchie Cobbs 		if (msg->header.arglen < sizeof(struct ng_mesg)
1464f8307e12SArchie Cobbs 		    || msg->header.arglen - sizeof(struct ng_mesg)
1465f8307e12SArchie Cobbs 		      < binary->header.arglen) {
1466f8307e12SArchie Cobbs 			error = EINVAL;
1467f8307e12SArchie Cobbs 			break;
1468f8307e12SArchie Cobbs 		}
1469f8307e12SArchie Cobbs 
1470f8307e12SArchie Cobbs 		/* Get a response message with lots of room */
1471f8307e12SArchie Cobbs 		NG_MKRESPONSE(rp, msg, sizeof(*ascii) + bufSize, M_NOWAIT);
1472f8307e12SArchie Cobbs 		if (rp == NULL) {
1473f8307e12SArchie Cobbs 			error = ENOMEM;
1474f8307e12SArchie Cobbs 			break;
1475f8307e12SArchie Cobbs 		}
1476f8307e12SArchie Cobbs 		ascii = (struct ng_mesg *)rp->data;
1477f8307e12SArchie Cobbs 
1478f8307e12SArchie Cobbs 		/* Copy binary message header to response message payload */
1479f8307e12SArchie Cobbs 		bcopy(binary, ascii, sizeof(*binary));
1480f8307e12SArchie Cobbs 
1481f8307e12SArchie Cobbs 		/* Find command by matching typecookie and command number */
1482f8307e12SArchie Cobbs 		for (c = here->type->cmdlist;
1483f8307e12SArchie Cobbs 		    c != NULL && c->name != NULL; c++) {
1484f8307e12SArchie Cobbs 			if (binary->header.typecookie == c->cookie
1485f8307e12SArchie Cobbs 			    && binary->header.cmd == c->cmd)
1486f8307e12SArchie Cobbs 				break;
1487f8307e12SArchie Cobbs 		}
1488f8307e12SArchie Cobbs 		if (c == NULL || c->name == NULL) {
1489f8307e12SArchie Cobbs 			for (c = ng_generic_cmds; c->name != NULL; c++) {
1490f8307e12SArchie Cobbs 				if (binary->header.typecookie == c->cookie
1491f8307e12SArchie Cobbs 				    && binary->header.cmd == c->cmd)
1492f8307e12SArchie Cobbs 					break;
1493f8307e12SArchie Cobbs 			}
1494f8307e12SArchie Cobbs 			if (c->name == NULL) {
1495f8307e12SArchie Cobbs 				FREE(rp, M_NETGRAPH);
1496f8307e12SArchie Cobbs 				error = ENOSYS;
1497f8307e12SArchie Cobbs 				break;
1498f8307e12SArchie Cobbs 			}
1499f8307e12SArchie Cobbs 		}
1500f8307e12SArchie Cobbs 
1501f8307e12SArchie Cobbs 		/* Convert command name to ASCII */
1502f8307e12SArchie Cobbs 		snprintf(ascii->header.cmdstr, sizeof(ascii->header.cmdstr),
1503f8307e12SArchie Cobbs 		    "%s", c->name);
1504f8307e12SArchie Cobbs 
1505f8307e12SArchie Cobbs 		/* Convert command arguments to ASCII */
1506f8307e12SArchie Cobbs 		argstype = (binary->header.flags & NGF_RESP) ?
1507f8307e12SArchie Cobbs 		    c->respType : c->mesgType;
1508f8307e12SArchie Cobbs 		if (argstype == NULL)
1509f8307e12SArchie Cobbs 			*ascii->data = '\0';
1510f8307e12SArchie Cobbs 		else {
1511f8307e12SArchie Cobbs 			if ((error = ng_unparse(argstype,
1512f8307e12SArchie Cobbs 			    (u_char *)binary->data,
1513f8307e12SArchie Cobbs 			    ascii->data, bufSize)) != 0) {
1514f8307e12SArchie Cobbs 				FREE(rp, M_NETGRAPH);
1515f8307e12SArchie Cobbs 				break;
1516f8307e12SArchie Cobbs 			}
1517f8307e12SArchie Cobbs 		}
1518f8307e12SArchie Cobbs 
1519f8307e12SArchie Cobbs 		/* Return the result as struct ng_mesg plus ASCII string */
1520f8307e12SArchie Cobbs 		bufSize = strlen(ascii->data) + 1;
1521f8307e12SArchie Cobbs 		ascii->header.arglen = bufSize;
1522f8307e12SArchie Cobbs 		rp->header.arglen = sizeof(*ascii) + bufSize;
1523f8307e12SArchie Cobbs 		*resp = rp;
1524f8307e12SArchie Cobbs 		break;
1525f8307e12SArchie Cobbs 	    }
1526f8307e12SArchie Cobbs 
1527f8307e12SArchie Cobbs 	case NGM_ASCII2BINARY:
1528f8307e12SArchie Cobbs 	    {
1529f8307e12SArchie Cobbs 		int bufSize = 2000;	/* XXX hard coded constant */
1530f8307e12SArchie Cobbs 		const struct ng_cmdlist *c;
1531f8307e12SArchie Cobbs 		const struct ng_parse_type *argstype;
1532f8307e12SArchie Cobbs 		struct ng_mesg *rp, *ascii, *binary;
153352ec4a03SArchie Cobbs 		int off = 0;
1534f8307e12SArchie Cobbs 
1535f8307e12SArchie Cobbs 		/* Data area must contain at least a struct ng_mesg + '\0' */
1536f8307e12SArchie Cobbs 		ascii = (struct ng_mesg *)msg->data;
1537f8307e12SArchie Cobbs 		if (msg->header.arglen < sizeof(*ascii) + 1
1538f8307e12SArchie Cobbs 		    || ascii->header.arglen < 1
1539f8307e12SArchie Cobbs 		    || msg->header.arglen
1540f8307e12SArchie Cobbs 		      < sizeof(*ascii) + ascii->header.arglen) {
1541f8307e12SArchie Cobbs 			error = EINVAL;
1542f8307e12SArchie Cobbs 			break;
1543f8307e12SArchie Cobbs 		}
1544f8307e12SArchie Cobbs 		ascii->data[ascii->header.arglen - 1] = '\0';
1545f8307e12SArchie Cobbs 
1546f8307e12SArchie Cobbs 		/* Get a response message with lots of room */
1547f8307e12SArchie Cobbs 		NG_MKRESPONSE(rp, msg, sizeof(*binary) + bufSize, M_NOWAIT);
1548f8307e12SArchie Cobbs 		if (rp == NULL) {
1549f8307e12SArchie Cobbs 			error = ENOMEM;
1550f8307e12SArchie Cobbs 			break;
1551f8307e12SArchie Cobbs 		}
1552f8307e12SArchie Cobbs 		binary = (struct ng_mesg *)rp->data;
1553f8307e12SArchie Cobbs 
1554f8307e12SArchie Cobbs 		/* Copy ASCII message header to response message payload */
1555f8307e12SArchie Cobbs 		bcopy(ascii, binary, sizeof(*ascii));
1556f8307e12SArchie Cobbs 
1557f8307e12SArchie Cobbs 		/* Find command by matching ASCII command string */
1558f8307e12SArchie Cobbs 		for (c = here->type->cmdlist;
1559f8307e12SArchie Cobbs 		    c != NULL && c->name != NULL; c++) {
1560f8307e12SArchie Cobbs 			if (strcmp(ascii->header.cmdstr, c->name) == 0)
1561f8307e12SArchie Cobbs 				break;
1562f8307e12SArchie Cobbs 		}
1563f8307e12SArchie Cobbs 		if (c == NULL || c->name == NULL) {
1564f8307e12SArchie Cobbs 			for (c = ng_generic_cmds; c->name != NULL; c++) {
1565f8307e12SArchie Cobbs 				if (strcmp(ascii->header.cmdstr, c->name) == 0)
1566f8307e12SArchie Cobbs 					break;
1567f8307e12SArchie Cobbs 			}
1568f8307e12SArchie Cobbs 			if (c->name == NULL) {
1569f8307e12SArchie Cobbs 				FREE(rp, M_NETGRAPH);
1570f8307e12SArchie Cobbs 				error = ENOSYS;
1571f8307e12SArchie Cobbs 				break;
1572f8307e12SArchie Cobbs 			}
1573f8307e12SArchie Cobbs 		}
1574f8307e12SArchie Cobbs 
1575f8307e12SArchie Cobbs 		/* Convert command name to binary */
1576f8307e12SArchie Cobbs 		binary->header.cmd = c->cmd;
1577f8307e12SArchie Cobbs 		binary->header.typecookie = c->cookie;
1578f8307e12SArchie Cobbs 
1579f8307e12SArchie Cobbs 		/* Convert command arguments to binary */
1580f8307e12SArchie Cobbs 		argstype = (binary->header.flags & NGF_RESP) ?
1581f8307e12SArchie Cobbs 		    c->respType : c->mesgType;
1582f8307e12SArchie Cobbs 		if (argstype == NULL)
1583f8307e12SArchie Cobbs 			bufSize = 0;
1584f8307e12SArchie Cobbs 		else {
1585f8307e12SArchie Cobbs 			if ((error = ng_parse(argstype, ascii->data,
1586f8307e12SArchie Cobbs 			    &off, (u_char *)binary->data, &bufSize)) != 0) {
1587f8307e12SArchie Cobbs 				FREE(rp, M_NETGRAPH);
1588f8307e12SArchie Cobbs 				break;
1589f8307e12SArchie Cobbs 			}
1590f8307e12SArchie Cobbs 		}
1591f8307e12SArchie Cobbs 
1592f8307e12SArchie Cobbs 		/* Return the result */
1593f8307e12SArchie Cobbs 		binary->header.arglen = bufSize;
1594f8307e12SArchie Cobbs 		rp->header.arglen = sizeof(*binary) + bufSize;
1595f8307e12SArchie Cobbs 		*resp = rp;
1596f8307e12SArchie Cobbs 		break;
1597f8307e12SArchie Cobbs 	    }
1598f8307e12SArchie Cobbs 
15997095e097SPoul-Henning Kamp 	case NGM_TEXT_CONFIG:
16004cf49a43SJulian Elischer 	case NGM_TEXT_STATUS:
16014cf49a43SJulian Elischer 		/*
16024cf49a43SJulian Elischer 		 * This one is tricky as it passes the command down to the
16034cf49a43SJulian Elischer 		 * actual node, even though it is a generic type command.
16044cf49a43SJulian Elischer 		 * This means we must assume that the msg is already freed
16054cf49a43SJulian Elischer 		 * when control passes back to us.
16064cf49a43SJulian Elischer 		 */
16074cf49a43SJulian Elischer 		if (resp == NULL) {
16084cf49a43SJulian Elischer 			error = EINVAL;
16094cf49a43SJulian Elischer 			break;
16104cf49a43SJulian Elischer 		}
16114cf49a43SJulian Elischer 		if (here->type->rcvmsg != NULL)
1612a4ec03cfSJulian Elischer 			return((*here->type->rcvmsg)(here, msg, retaddr,
1613a4ec03cfSJulian Elischer 			resp, lasthook));
16144cf49a43SJulian Elischer 		/* Fall through if rcvmsg not supported */
16154cf49a43SJulian Elischer 	default:
16164cf49a43SJulian Elischer 		TRAP_ERROR;
16174cf49a43SJulian Elischer 		error = EINVAL;
16184cf49a43SJulian Elischer 	}
16194cf49a43SJulian Elischer 	FREE(msg, M_NETGRAPH);
16204cf49a43SJulian Elischer 	return (error);
16214cf49a43SJulian Elischer }
16224cf49a43SJulian Elischer 
16234cf49a43SJulian Elischer /*
16244cf49a43SJulian Elischer  * Send a data packet to a node. If the recipient has no
16254cf49a43SJulian Elischer  * 'receive data' method, then silently discard the packet.
16264cf49a43SJulian Elischer  */
16274cf49a43SJulian Elischer int
1628a4ec03cfSJulian Elischer ng_send_data(hook_p hook, struct mbuf *m, meta_p meta,
1629a4ec03cfSJulian Elischer 	struct mbuf **ret_m, meta_p *ret_meta)
16304cf49a43SJulian Elischer {
1631a4ec03cfSJulian Elischer 	ng_rcvdata_t *rcvdata;
16324cf49a43SJulian Elischer 	int error;
16334cf49a43SJulian Elischer 
1634b2da83c2SArchie Cobbs 	CHECK_DATA_MBUF(m);
16354cf49a43SJulian Elischer 	if (hook && (hook->flags & HK_INVALID) == 0) {
16364cf49a43SJulian Elischer 		rcvdata = hook->peer->node->type->rcvdata;
16374cf49a43SJulian Elischer 		if (rcvdata != NULL)
1638a4ec03cfSJulian Elischer 			error = (*rcvdata)(hook->peer, m, meta,
1639a4ec03cfSJulian Elischer 					ret_m, ret_meta);
16404cf49a43SJulian Elischer 		else {
16414cf49a43SJulian Elischer 			error = 0;
16424cf49a43SJulian Elischer 			NG_FREE_DATA(m, meta);
16434cf49a43SJulian Elischer 		}
16444cf49a43SJulian Elischer 	} else {
16454cf49a43SJulian Elischer 		TRAP_ERROR;
16464cf49a43SJulian Elischer 		error = ENOTCONN;
16474cf49a43SJulian Elischer 		NG_FREE_DATA(m, meta);
16484cf49a43SJulian Elischer 	}
16494cf49a43SJulian Elischer 	return (error);
16504cf49a43SJulian Elischer }
16514cf49a43SJulian Elischer 
16524cf49a43SJulian Elischer /*
16534cf49a43SJulian Elischer  * Send a queued data packet to a node. If the recipient has no
16544cf49a43SJulian Elischer  * 'receive queued data' method, then try the 'receive data' method above.
16554cf49a43SJulian Elischer  */
16564cf49a43SJulian Elischer int
1657a4ec03cfSJulian Elischer ng_send_dataq(hook_p hook, struct mbuf *m, meta_p meta,
1658a4ec03cfSJulian Elischer 	struct mbuf **ret_m, meta_p *ret_meta)
16594cf49a43SJulian Elischer {
1660a4ec03cfSJulian Elischer 	ng_rcvdata_t *rcvdata;
16614cf49a43SJulian Elischer 	int error;
16624cf49a43SJulian Elischer 
1663b2da83c2SArchie Cobbs 	CHECK_DATA_MBUF(m);
16644cf49a43SJulian Elischer 	if (hook && (hook->flags & HK_INVALID) == 0) {
1665a4ec03cfSJulian Elischer 		rcvdata = hook->peer->node->type->rcvdataq;
1666a4ec03cfSJulian Elischer 		if (rcvdata != NULL)
1667a4ec03cfSJulian Elischer 			error = (*rcvdata)(hook->peer, m, meta,
1668a4ec03cfSJulian Elischer 					ret_m, ret_meta);
16694cf49a43SJulian Elischer 		else {
1670a4ec03cfSJulian Elischer 			rcvdata = hook->peer->node->type->rcvdata;
1671a4ec03cfSJulian Elischer 			if (rcvdata != NULL) {
1672a4ec03cfSJulian Elischer 				error = (*rcvdata)(hook->peer, m, meta,
1673a4ec03cfSJulian Elischer 					ret_m, ret_meta);
1674a4ec03cfSJulian Elischer 			} else {
1675a4ec03cfSJulian Elischer 				error = 0;
1676a4ec03cfSJulian Elischer 				NG_FREE_DATA(m, meta);
1677a4ec03cfSJulian Elischer 			}
16784cf49a43SJulian Elischer 		}
16794cf49a43SJulian Elischer 	} else {
16804cf49a43SJulian Elischer 		TRAP_ERROR;
16814cf49a43SJulian Elischer 		error = ENOTCONN;
16824cf49a43SJulian Elischer 		NG_FREE_DATA(m, meta);
16834cf49a43SJulian Elischer 	}
16844cf49a43SJulian Elischer 	return (error);
16854cf49a43SJulian Elischer }
16864cf49a43SJulian Elischer 
1687a096e45aSArchie Cobbs /*
1688a096e45aSArchie Cobbs  * Copy a 'meta'.
1689a096e45aSArchie Cobbs  *
1690a096e45aSArchie Cobbs  * Returns new meta, or NULL if original meta is NULL or ENOMEM.
1691a096e45aSArchie Cobbs  */
1692a096e45aSArchie Cobbs meta_p
1693a096e45aSArchie Cobbs ng_copy_meta(meta_p meta)
1694a096e45aSArchie Cobbs {
1695a096e45aSArchie Cobbs 	meta_p meta2;
1696a096e45aSArchie Cobbs 
1697a096e45aSArchie Cobbs 	if (meta == NULL)
1698a096e45aSArchie Cobbs 		return (NULL);
1699a096e45aSArchie Cobbs 	MALLOC(meta2, meta_p, meta->used_len, M_NETGRAPH, M_NOWAIT);
1700a096e45aSArchie Cobbs 	if (meta2 == NULL)
1701a096e45aSArchie Cobbs 		return (NULL);
1702a096e45aSArchie Cobbs 	meta2->allocated_len = meta->used_len;
1703a096e45aSArchie Cobbs 	bcopy(meta, meta2, meta->used_len);
1704a096e45aSArchie Cobbs 	return (meta2);
1705a096e45aSArchie Cobbs }
1706a096e45aSArchie Cobbs 
17074cf49a43SJulian Elischer /************************************************************************
17084cf49a43SJulian Elischer 			Module routines
17094cf49a43SJulian Elischer ************************************************************************/
17104cf49a43SJulian Elischer 
17114cf49a43SJulian Elischer /*
17124cf49a43SJulian Elischer  * Handle the loading/unloading of a netgraph node type module
17134cf49a43SJulian Elischer  */
17144cf49a43SJulian Elischer int
17154cf49a43SJulian Elischer ng_mod_event(module_t mod, int event, void *data)
17164cf49a43SJulian Elischer {
17174cf49a43SJulian Elischer 	struct ng_type *const type = data;
17184cf49a43SJulian Elischer 	int s, error = 0;
17194cf49a43SJulian Elischer 
17204cf49a43SJulian Elischer 	switch (event) {
17214cf49a43SJulian Elischer 	case MOD_LOAD:
17224cf49a43SJulian Elischer 
17234cf49a43SJulian Elischer 		/* Register new netgraph node type */
17244cf49a43SJulian Elischer 		s = splnet();
17254cf49a43SJulian Elischer 		if ((error = ng_newtype(type)) != 0) {
17264cf49a43SJulian Elischer 			splx(s);
17274cf49a43SJulian Elischer 			break;
17284cf49a43SJulian Elischer 		}
17294cf49a43SJulian Elischer 
17304cf49a43SJulian Elischer 		/* Call type specific code */
17314cf49a43SJulian Elischer 		if (type->mod_event != NULL)
17324cf49a43SJulian Elischer 			if ((error = (*type->mod_event)(mod, event, data)) != 0)
17334cf49a43SJulian Elischer 				LIST_REMOVE(type, types);
17344cf49a43SJulian Elischer 		splx(s);
17354cf49a43SJulian Elischer 		break;
17364cf49a43SJulian Elischer 
17374cf49a43SJulian Elischer 	case MOD_UNLOAD:
17384cf49a43SJulian Elischer 		s = splnet();
17394cf49a43SJulian Elischer 		if (type->refs != 0)		/* make sure no nodes exist! */
17404cf49a43SJulian Elischer 			error = EBUSY;
17414cf49a43SJulian Elischer 		else {
17424cf49a43SJulian Elischer 			if (type->mod_event != NULL) {	/* check with type */
17434cf49a43SJulian Elischer 				error = (*type->mod_event)(mod, event, data);
17444cf49a43SJulian Elischer 				if (error != 0) {	/* type refuses.. */
17454cf49a43SJulian Elischer 					splx(s);
17464cf49a43SJulian Elischer 					break;
17474cf49a43SJulian Elischer 				}
17484cf49a43SJulian Elischer 			}
17494cf49a43SJulian Elischer 			LIST_REMOVE(type, types);
17504cf49a43SJulian Elischer 		}
17514cf49a43SJulian Elischer 		splx(s);
17524cf49a43SJulian Elischer 		break;
17534cf49a43SJulian Elischer 
17544cf49a43SJulian Elischer 	default:
17554cf49a43SJulian Elischer 		if (type->mod_event != NULL)
17564cf49a43SJulian Elischer 			error = (*type->mod_event)(mod, event, data);
17574cf49a43SJulian Elischer 		else
17584cf49a43SJulian Elischer 			error = 0;		/* XXX ? */
17594cf49a43SJulian Elischer 		break;
17604cf49a43SJulian Elischer 	}
17614cf49a43SJulian Elischer 	return (error);
17624cf49a43SJulian Elischer }
17634cf49a43SJulian Elischer 
17644cf49a43SJulian Elischer /*
17654cf49a43SJulian Elischer  * Handle loading and unloading for this code.
17664cf49a43SJulian Elischer  * The only thing we need to link into is the NETISR strucure.
17674cf49a43SJulian Elischer  */
17684cf49a43SJulian Elischer static int
17694cf49a43SJulian Elischer ngb_mod_event(module_t mod, int event, void *data)
17704cf49a43SJulian Elischer {
17714cf49a43SJulian Elischer 	int s, error = 0;
17724cf49a43SJulian Elischer 
17734cf49a43SJulian Elischer 	switch (event) {
17744cf49a43SJulian Elischer 	case MOD_LOAD:
17754cf49a43SJulian Elischer 		/* Register line discipline */
17764cf49a43SJulian Elischer 		s = splimp();
17774cf49a43SJulian Elischer 		error = register_netisr(NETISR_NETGRAPH, ngintr);
17784cf49a43SJulian Elischer 		splx(s);
17794cf49a43SJulian Elischer 		break;
17804cf49a43SJulian Elischer 	case MOD_UNLOAD:
17814cf49a43SJulian Elischer 		/* You cant unload it because an interface may be using it.  */
17824cf49a43SJulian Elischer 		error = EBUSY;
17834cf49a43SJulian Elischer 		break;
17844cf49a43SJulian Elischer 	default:
17854cf49a43SJulian Elischer 		error = EOPNOTSUPP;
17864cf49a43SJulian Elischer 		break;
17874cf49a43SJulian Elischer 	}
17884cf49a43SJulian Elischer 	return (error);
17894cf49a43SJulian Elischer }
17904cf49a43SJulian Elischer 
17914cf49a43SJulian Elischer static moduledata_t netgraph_mod = {
17924cf49a43SJulian Elischer 	"netgraph",
17934cf49a43SJulian Elischer 	ngb_mod_event,
17944cf49a43SJulian Elischer 	(NULL)
17954cf49a43SJulian Elischer };
17964cf49a43SJulian Elischer DECLARE_MODULE(netgraph, netgraph_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
17974cf49a43SJulian Elischer 
17984cf49a43SJulian Elischer /************************************************************************
17994cf49a43SJulian Elischer 			Queueing routines
18004cf49a43SJulian Elischer ************************************************************************/
18014cf49a43SJulian Elischer 
18024cf49a43SJulian Elischer /* The structure for queueing across ISR switches */
18034cf49a43SJulian Elischer struct ng_queue_entry {
18044cf49a43SJulian Elischer 	u_long	flags;
18054cf49a43SJulian Elischer 	struct ng_queue_entry *next;
18064cf49a43SJulian Elischer 	union {
18074cf49a43SJulian Elischer 		struct {
18084cf49a43SJulian Elischer 			hook_p		da_hook;	/*  target hook */
18094cf49a43SJulian Elischer 			struct mbuf	*da_m;
18104cf49a43SJulian Elischer 			meta_p		da_meta;
18114cf49a43SJulian Elischer 		} data;
18124cf49a43SJulian Elischer 		struct {
18134cf49a43SJulian Elischer 			struct ng_mesg	*msg_msg;
18144cf49a43SJulian Elischer 			node_p		msg_node;
18154cf49a43SJulian Elischer 			void		*msg_retaddr;
1816a4ec03cfSJulian Elischer 			hook_p		msg_lasthook;
18174cf49a43SJulian Elischer 		} msg;
18184cf49a43SJulian Elischer 	} body;
18194cf49a43SJulian Elischer };
18204cf49a43SJulian Elischer #define NGQF_DATA	0x01		/* the queue element is data */
18214cf49a43SJulian Elischer #define NGQF_MESG	0x02		/* the queue element is a message */
18224cf49a43SJulian Elischer 
18234cf49a43SJulian Elischer static struct ng_queue_entry   *ngqbase;	/* items to be unqueued */
18244cf49a43SJulian Elischer static struct ng_queue_entry   *ngqlast;	/* last item queued */
18254cf49a43SJulian Elischer static const int		ngqroom = 64;	/* max items to queue */
18264cf49a43SJulian Elischer static int			ngqsize;	/* number of items in queue */
18274cf49a43SJulian Elischer 
18284cf49a43SJulian Elischer static struct ng_queue_entry   *ngqfree;	/* free ones */
18294cf49a43SJulian Elischer static const int		ngqfreemax = 16;/* cache at most this many */
18304cf49a43SJulian Elischer static int			ngqfreesize;	/* number of cached entries */
18314cf49a43SJulian Elischer 
18324cf49a43SJulian Elischer /*
18334cf49a43SJulian Elischer  * Get a queue entry
18344cf49a43SJulian Elischer  */
18354cf49a43SJulian Elischer static struct ng_queue_entry *
18364cf49a43SJulian Elischer ng_getqblk(void)
18374cf49a43SJulian Elischer {
18384cf49a43SJulian Elischer 	register struct ng_queue_entry *q;
18394cf49a43SJulian Elischer 	int s;
18404cf49a43SJulian Elischer 
18414cf49a43SJulian Elischer 	/* Could be guarding against tty ints or whatever */
18424cf49a43SJulian Elischer 	s = splhigh();
18434cf49a43SJulian Elischer 
18444cf49a43SJulian Elischer 	/* Try get a cached queue block, or else allocate a new one */
18454cf49a43SJulian Elischer 	if ((q = ngqfree) == NULL) {
18464cf49a43SJulian Elischer 		splx(s);
18474cf49a43SJulian Elischer 		if (ngqsize < ngqroom) {	/* don't worry about races */
18484cf49a43SJulian Elischer 			MALLOC(q, struct ng_queue_entry *,
18494cf49a43SJulian Elischer 			    sizeof(*q), M_NETGRAPH, M_NOWAIT);
18504cf49a43SJulian Elischer 		}
18514cf49a43SJulian Elischer 	} else {
18524cf49a43SJulian Elischer 		ngqfree = q->next;
18534cf49a43SJulian Elischer 		ngqfreesize--;
18544cf49a43SJulian Elischer 		splx(s);
18554cf49a43SJulian Elischer 	}
18564cf49a43SJulian Elischer 	return (q);
18574cf49a43SJulian Elischer }
18584cf49a43SJulian Elischer 
18594cf49a43SJulian Elischer /*
18604cf49a43SJulian Elischer  * Release a queue entry
18614cf49a43SJulian Elischer  */
18624cf49a43SJulian Elischer #define RETURN_QBLK(q)							\
18634cf49a43SJulian Elischer do {									\
18644cf49a43SJulian Elischer 	int s;								\
18654cf49a43SJulian Elischer 	if (ngqfreesize < ngqfreemax) { /* don't worry about races */ 	\
18664cf49a43SJulian Elischer 		s = splhigh();						\
18674cf49a43SJulian Elischer 		(q)->next = ngqfree;					\
18684cf49a43SJulian Elischer 		ngqfree = (q);						\
18694cf49a43SJulian Elischer 		ngqfreesize++;						\
18704cf49a43SJulian Elischer 		splx(s);						\
18714cf49a43SJulian Elischer 	} else {							\
18724cf49a43SJulian Elischer 		FREE((q), M_NETGRAPH);					\
18734cf49a43SJulian Elischer 	}								\
18744cf49a43SJulian Elischer } while (0)
18754cf49a43SJulian Elischer 
18764cf49a43SJulian Elischer /*
18774cf49a43SJulian Elischer  * Running at a raised (but we don't know which) processor priority level,
18784cf49a43SJulian Elischer  * put the data onto a queue to be picked up by another PPL (probably splnet)
18794cf49a43SJulian Elischer  */
18804cf49a43SJulian Elischer int
18814cf49a43SJulian Elischer ng_queue_data(hook_p hook, struct mbuf *m, meta_p meta)
18824cf49a43SJulian Elischer {
18834cf49a43SJulian Elischer 	struct ng_queue_entry *q;
18844cf49a43SJulian Elischer 	int s;
18854cf49a43SJulian Elischer 
18864cf49a43SJulian Elischer 	if (hook == NULL) {
18874cf49a43SJulian Elischer 		NG_FREE_DATA(m, meta);
18884cf49a43SJulian Elischer 		return (0);
18894cf49a43SJulian Elischer 	}
18904cf49a43SJulian Elischer 	if ((q = ng_getqblk()) == NULL) {
18914cf49a43SJulian Elischer 		NG_FREE_DATA(m, meta);
18924cf49a43SJulian Elischer 		return (ENOBUFS);
18934cf49a43SJulian Elischer 	}
18944cf49a43SJulian Elischer 
18954cf49a43SJulian Elischer 	/* Fill out the contents */
18964cf49a43SJulian Elischer 	q->flags = NGQF_DATA;
18974cf49a43SJulian Elischer 	q->next = NULL;
18984cf49a43SJulian Elischer 	q->body.data.da_hook = hook;
18994cf49a43SJulian Elischer 	q->body.data.da_m = m;
19004cf49a43SJulian Elischer 	q->body.data.da_meta = meta;
19014cf49a43SJulian Elischer 	hook->refs++;		/* don't let it go away while on the queue */
19024cf49a43SJulian Elischer 
19034cf49a43SJulian Elischer 	/* Put it on the queue */
19044cf49a43SJulian Elischer 	s = splhigh();
19054cf49a43SJulian Elischer 	if (ngqbase) {
19064cf49a43SJulian Elischer 		ngqlast->next = q;
19074cf49a43SJulian Elischer 	} else {
19084cf49a43SJulian Elischer 		ngqbase = q;
19094cf49a43SJulian Elischer 	}
19104cf49a43SJulian Elischer 	ngqlast = q;
19114cf49a43SJulian Elischer 	ngqsize++;
19124cf49a43SJulian Elischer 	splx(s);
19134cf49a43SJulian Elischer 
19144cf49a43SJulian Elischer 	/* Schedule software interrupt to handle it later */
19154cf49a43SJulian Elischer 	schednetisr(NETISR_NETGRAPH);
19164cf49a43SJulian Elischer 	return (0);
19174cf49a43SJulian Elischer }
19184cf49a43SJulian Elischer 
19194cf49a43SJulian Elischer /*
19204cf49a43SJulian Elischer  * Running at a raised (but we don't know which) processor priority level,
19214cf49a43SJulian Elischer  * put the msg onto a queue to be picked up by another PPL (probably splnet)
19224cf49a43SJulian Elischer  */
19234cf49a43SJulian Elischer int
19249a9a26fdSArchie Cobbs ng_queue_msg(node_p here, struct ng_mesg *msg, const char *address)
19254cf49a43SJulian Elischer {
19264cf49a43SJulian Elischer 	register struct ng_queue_entry *q;
19274cf49a43SJulian Elischer 	int     s;
19284cf49a43SJulian Elischer 	node_p  dest = NULL;
19294cf49a43SJulian Elischer 	char   *retaddr = NULL;
19304cf49a43SJulian Elischer 	int     error;
1931a4ec03cfSJulian Elischer 	hook_p	lasthook = NULL;
19324cf49a43SJulian Elischer 
1933dc90cad9SJulian Elischer 	/* Find the target node. */
1934a4ec03cfSJulian Elischer 	error = ng_path2node(here, address, &dest, &retaddr, &lasthook);
19354cf49a43SJulian Elischer 	if (error) {
19364cf49a43SJulian Elischer 		FREE(msg, M_NETGRAPH);
19374cf49a43SJulian Elischer 		return (error);
19384cf49a43SJulian Elischer 	}
19394cf49a43SJulian Elischer 	if ((q = ng_getqblk()) == NULL) {
19404cf49a43SJulian Elischer 		FREE(msg, M_NETGRAPH);
19414cf49a43SJulian Elischer 		if (retaddr)
19424cf49a43SJulian Elischer 			FREE(retaddr, M_NETGRAPH);
19434cf49a43SJulian Elischer 		return (ENOBUFS);
19444cf49a43SJulian Elischer 	}
19454cf49a43SJulian Elischer 
19464cf49a43SJulian Elischer 	/* Fill out the contents */
19474cf49a43SJulian Elischer 	q->flags = NGQF_MESG;
19484cf49a43SJulian Elischer 	q->next = NULL;
19494cf49a43SJulian Elischer 	q->body.msg.msg_node = dest;
19504cf49a43SJulian Elischer 	q->body.msg.msg_msg = msg;
19514cf49a43SJulian Elischer 	q->body.msg.msg_retaddr = retaddr;
1952a4ec03cfSJulian Elischer 	q->body.msg.msg_lasthook = lasthook;
19534cf49a43SJulian Elischer 	dest->refs++;		/* don't let it go away while on the queue */
1954a4ec03cfSJulian Elischer 	if (lasthook)
1955a4ec03cfSJulian Elischer 		lasthook->refs++; /* same for the hook */
19564cf49a43SJulian Elischer 
19574cf49a43SJulian Elischer 	/* Put it on the queue */
19584cf49a43SJulian Elischer 	s = splhigh();
19594cf49a43SJulian Elischer 	if (ngqbase) {
19604cf49a43SJulian Elischer 		ngqlast->next = q;
19614cf49a43SJulian Elischer 	} else {
19624cf49a43SJulian Elischer 		ngqbase = q;
19634cf49a43SJulian Elischer 	}
19644cf49a43SJulian Elischer 	ngqlast = q;
19654cf49a43SJulian Elischer 	ngqsize++;
19664cf49a43SJulian Elischer 	splx(s);
19674cf49a43SJulian Elischer 
19684cf49a43SJulian Elischer 	/* Schedule software interrupt to handle it later */
19694cf49a43SJulian Elischer 	schednetisr(NETISR_NETGRAPH);
19704cf49a43SJulian Elischer 	return (0);
19714cf49a43SJulian Elischer }
19724cf49a43SJulian Elischer 
19734cf49a43SJulian Elischer /*
19744cf49a43SJulian Elischer  * Pick an item off the queue, process it, and dispose of the queue entry.
19754cf49a43SJulian Elischer  * Should be running at splnet.
19764cf49a43SJulian Elischer  */
19774cf49a43SJulian Elischer static void
19784cf49a43SJulian Elischer ngintr(void)
19794cf49a43SJulian Elischer {
19804cf49a43SJulian Elischer 	hook_p  hook;
19814cf49a43SJulian Elischer 	struct ng_queue_entry *ngq;
19824cf49a43SJulian Elischer 	struct mbuf *m;
19834cf49a43SJulian Elischer 	meta_p  meta;
19844cf49a43SJulian Elischer 	void   *retaddr;
19854cf49a43SJulian Elischer 	struct ng_mesg *msg;
19864cf49a43SJulian Elischer 	node_p  node;
19874cf49a43SJulian Elischer 	int     error = 0;
19884cf49a43SJulian Elischer 	int     s;
19894cf49a43SJulian Elischer 
19904cf49a43SJulian Elischer 	while (1) {
19914cf49a43SJulian Elischer 		s = splhigh();
19924cf49a43SJulian Elischer 		if ((ngq = ngqbase)) {
19934cf49a43SJulian Elischer 			ngqbase = ngq->next;
19944cf49a43SJulian Elischer 			ngqsize--;
19954cf49a43SJulian Elischer 		}
19964cf49a43SJulian Elischer 		splx(s);
19974cf49a43SJulian Elischer 		if (ngq == NULL)
19984cf49a43SJulian Elischer 			return;
19994cf49a43SJulian Elischer 		switch (ngq->flags) {
20004cf49a43SJulian Elischer 		case NGQF_DATA:
20014cf49a43SJulian Elischer 			hook = ngq->body.data.da_hook;
20024cf49a43SJulian Elischer 			m = ngq->body.data.da_m;
20034cf49a43SJulian Elischer 			meta = ngq->body.data.da_meta;
20044cf49a43SJulian Elischer 			RETURN_QBLK(ngq);
20054cf49a43SJulian Elischer 			NG_SEND_DATAQ(error, hook, m, meta);
20064cf49a43SJulian Elischer 			ng_unref_hook(hook);
20074cf49a43SJulian Elischer 			break;
20084cf49a43SJulian Elischer 		case NGQF_MESG:
20094cf49a43SJulian Elischer 			node = ngq->body.msg.msg_node;
20104cf49a43SJulian Elischer 			msg = ngq->body.msg.msg_msg;
20114cf49a43SJulian Elischer 			retaddr = ngq->body.msg.msg_retaddr;
2012a4ec03cfSJulian Elischer 			hook = ngq->body.msg.msg_lasthook;
20134cf49a43SJulian Elischer 			RETURN_QBLK(ngq);
2014a4ec03cfSJulian Elischer 			if (hook) {
2015a4ec03cfSJulian Elischer 				if ((hook->refs == 1)
2016a4ec03cfSJulian Elischer 		    		|| (hook->flags & HK_INVALID) != 0) {
2017a4ec03cfSJulian Elischer 				/* If the hook only has one ref left
2018a4ec03cfSJulian Elischer 					then we can't use it */
2019a4ec03cfSJulian Elischer 					ng_unref_hook(hook);
2020a4ec03cfSJulian Elischer 					hook = NULL;
2021a4ec03cfSJulian Elischer 				} else {
2022a4ec03cfSJulian Elischer 					ng_unref_hook(hook);
2023a4ec03cfSJulian Elischer 				}
2024a4ec03cfSJulian Elischer 			}
2025a4ec03cfSJulian Elischer 			/* similarly, if the node is a zombie.. */
20264cf49a43SJulian Elischer 			if (node->flags & NG_INVALID) {
20274cf49a43SJulian Elischer 				FREE(msg, M_NETGRAPH);
20284cf49a43SJulian Elischer 			} else {
20294cf49a43SJulian Elischer 				CALL_MSG_HANDLER(error, node, msg,
2030a4ec03cfSJulian Elischer 						 retaddr, NULL, hook);
20314cf49a43SJulian Elischer 			}
20324cf49a43SJulian Elischer 			ng_unref(node);
20334cf49a43SJulian Elischer 			if (retaddr)
20344cf49a43SJulian Elischer 				FREE(retaddr, M_NETGRAPH);
20354cf49a43SJulian Elischer 			break;
20364cf49a43SJulian Elischer 		default:
20374cf49a43SJulian Elischer 			RETURN_QBLK(ngq);
20384cf49a43SJulian Elischer 		}
20394cf49a43SJulian Elischer 	}
20404cf49a43SJulian Elischer }
20414cf49a43SJulian Elischer 
20424cf49a43SJulian Elischer 
2043