ng_base.c revision 54249
1
2/*
3 * ng_base.c
4 *
5 * Copyright (c) 1996-1999 Whistle Communications, Inc.
6 * All rights reserved.
7 *
8 * Subject to the following obligations and disclaimer of warranty, use and
9 * redistribution of this software, in source or object code forms, with or
10 * without modifications are expressly permitted by Whistle Communications;
11 * provided, however, that:
12 * 1. Any and all reproductions of the source or object code must include the
13 *    copyright notice above and the following disclaimer of warranties; and
14 * 2. No rights are granted, in any manner or form, to use Whistle
15 *    Communications, Inc. trademarks, including the mark "WHISTLE
16 *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
17 *    such appears in the above copyright notice or in the software.
18 *
19 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
20 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
21 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
22 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
23 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
24 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
25 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
26 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
27 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
28 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
29 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
30 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
35 * OF SUCH DAMAGE.
36 *
37 * Authors: Julian Elischer <julian@whistle.com>
38 *          Archie Cobbs <archie@whistle.com>
39 *
40 * $FreeBSD: head/sys/netgraph/ng_base.c 54249 1999-12-07 05:50:48Z julian $
41 * $Whistle: ng_base.c,v 1.39 1999/01/28 23:54:53 julian Exp $
42 */
43
44/*
45 * This file implements the base netgraph code.
46 */
47
48#include <sys/param.h>
49#include <sys/systm.h>
50#include <sys/errno.h>
51#include <sys/kernel.h>
52#include <sys/malloc.h>
53#include <sys/syslog.h>
54#include <sys/linker.h>
55#include <sys/queue.h>
56#include <sys/mbuf.h>
57#include <sys/ctype.h>
58#include <machine/limits.h>
59
60#include <net/netisr.h>
61
62#include <netgraph/ng_message.h>
63#include <netgraph/netgraph.h>
64#include <netgraph/ng_parse.h>
65
66/* List of all nodes */
67static LIST_HEAD(, ng_node) nodelist;
68
69/* List of installed types */
70static LIST_HEAD(, ng_type) typelist;
71
72/* Hash releted definitions */
73#define ID_HASH_SIZE 32 /* most systems wont need even this many */
74static LIST_HEAD(, ng_node) ID_hash[ID_HASH_SIZE];
75/* Don't nead to initialise them because it's a LIST */
76
77/* Internal functions */
78static int	ng_add_hook(node_p node, const char *name, hook_p * hookp);
79static int	ng_connect(hook_p hook1, hook_p hook2);
80static void	ng_disconnect_hook(hook_p hook);
81static int	ng_generic_msg(node_p here, struct ng_mesg *msg,
82			const char *retaddr, struct ng_mesg ** resp);
83static ng_ID_t	ng_decodeidname(const char *name);
84static int	ngb_mod_event(module_t mod, int event, void *data);
85static void	ngintr(void);
86
87/* Our own netgraph malloc type */
88MALLOC_DEFINE(M_NETGRAPH, "netgraph", "netgraph structures and ctrl messages");
89
90/* Set this to Debugger("X") to catch all errors as they occur */
91#ifndef TRAP_ERROR
92#define TRAP_ERROR
93#endif
94
95static	ng_ID_t nextID = 1;
96
97#ifdef INVARIANTS
98#define CHECK_DATA_MBUF(m)	do {					\
99		struct mbuf *n;						\
100		int total;						\
101									\
102		if (((m)->m_flags & M_PKTHDR) == 0)			\
103			panic("%s: !PKTHDR", __FUNCTION__);		\
104		for (total = 0, n = (m); n != NULL; n = n->m_next)	\
105			total += n->m_len;				\
106		if ((m)->m_pkthdr.len != total) {			\
107			panic("%s: %d != %d",				\
108			    __FUNCTION__, (m)->m_pkthdr.len, total);	\
109		}							\
110	} while (0)
111#else
112#define CHECK_DATA_MBUF(m)
113#endif
114
115
116/************************************************************************
117	Parse type definitions for generic messages
118************************************************************************/
119
120/* Handy structure parse type defining macro */
121#define DEFINE_PARSE_STRUCT_TYPE(lo, up, args)				\
122static const struct ng_parse_struct_info				\
123	ng_ ## lo ## _type_info = NG_GENERIC_ ## up ## _INFO args;	\
124static const struct ng_parse_type ng_generic_ ## lo ## _type = {	\
125	&ng_parse_struct_type,						\
126	&ng_ ## lo ## _type_info					\
127}
128
129DEFINE_PARSE_STRUCT_TYPE(mkpeer, MKPEER, ());
130DEFINE_PARSE_STRUCT_TYPE(connect, CONNECT, ());
131DEFINE_PARSE_STRUCT_TYPE(name, NAME, ());
132DEFINE_PARSE_STRUCT_TYPE(rmhook, RMHOOK, ());
133DEFINE_PARSE_STRUCT_TYPE(nodeinfo, NODEINFO, ());
134DEFINE_PARSE_STRUCT_TYPE(typeinfo, TYPEINFO, ());
135DEFINE_PARSE_STRUCT_TYPE(linkinfo, LINKINFO, (&ng_generic_nodeinfo_type));
136
137/* Get length of an array when the length is stored as a 32 bit
138   value immediately preceeding the array -- as with struct namelist
139   and struct typelist. */
140static int
141ng_generic_list_getLength(const struct ng_parse_type *type,
142	const u_char *start, const u_char *buf)
143{
144	return *((const u_int32_t *)(buf - 4));
145}
146
147/* Get length of the array of struct linkinfo inside a struct hooklist */
148static int
149ng_generic_linkinfo_getLength(const struct ng_parse_type *type,
150	const u_char *start, const u_char *buf)
151{
152	const struct hooklist *hl = (const struct hooklist *)start;
153
154	return hl->nodeinfo.hooks;
155}
156
157/* Array type for a variable length array of struct namelist */
158static const struct ng_parse_array_info ng_nodeinfoarray_type_info = {
159	&ng_generic_nodeinfo_type,
160	&ng_generic_list_getLength
161};
162static const struct ng_parse_type ng_generic_nodeinfoarray_type = {
163	&ng_parse_array_type,
164	&ng_nodeinfoarray_type_info
165};
166
167/* Array type for a variable length array of struct typelist */
168static const struct ng_parse_array_info ng_typeinfoarray_type_info = {
169	&ng_generic_typeinfo_type,
170	&ng_generic_list_getLength
171};
172static const struct ng_parse_type ng_generic_typeinfoarray_type = {
173	&ng_parse_array_type,
174	&ng_typeinfoarray_type_info
175};
176
177/* Array type for array of struct linkinfo in struct hooklist */
178static const struct ng_parse_array_info ng_generic_linkinfo_array_type_info = {
179	&ng_generic_linkinfo_type,
180	&ng_generic_linkinfo_getLength
181};
182static const struct ng_parse_type ng_generic_linkinfo_array_type = {
183	&ng_parse_array_type,
184	&ng_generic_linkinfo_array_type_info
185};
186
187DEFINE_PARSE_STRUCT_TYPE(typelist, TYPELIST, (&ng_generic_nodeinfoarray_type));
188DEFINE_PARSE_STRUCT_TYPE(hooklist, HOOKLIST,
189	(&ng_generic_nodeinfo_type, &ng_generic_linkinfo_array_type));
190DEFINE_PARSE_STRUCT_TYPE(listnodes, LISTNODES,
191	(&ng_generic_nodeinfoarray_type));
192
193/* List of commands and how to convert arguments to/from ASCII */
194static const struct ng_cmdlist ng_generic_cmds[] = {
195	{
196	  NGM_GENERIC_COOKIE,
197	  NGM_SHUTDOWN,
198	  "shutdown",
199	  NULL,
200	  NULL
201	},
202	{
203	  NGM_GENERIC_COOKIE,
204	  NGM_MKPEER,
205	  "mkpeer",
206	  &ng_generic_mkpeer_type,
207	  NULL
208	},
209	{
210	  NGM_GENERIC_COOKIE,
211	  NGM_CONNECT,
212	  "connect",
213	  &ng_generic_connect_type,
214	  NULL
215	},
216	{
217	  NGM_GENERIC_COOKIE,
218	  NGM_NAME,
219	  "name",
220	  &ng_generic_name_type,
221	  NULL
222	},
223	{
224	  NGM_GENERIC_COOKIE,
225	  NGM_RMHOOK,
226	  "rmhook",
227	  &ng_generic_rmhook_type,
228	  NULL
229	},
230	{
231	  NGM_GENERIC_COOKIE,
232	  NGM_NODEINFO,
233	  "nodeinfo",
234	  NULL,
235	  &ng_generic_nodeinfo_type
236	},
237	{
238	  NGM_GENERIC_COOKIE,
239	  NGM_LISTHOOKS,
240	  "listhooks",
241	  NULL,
242	  &ng_generic_hooklist_type
243	},
244	{
245	  NGM_GENERIC_COOKIE,
246	  NGM_LISTNAMES,
247	  "listnames",
248	  NULL,
249	  &ng_generic_listnodes_type	/* same as NGM_LISTNODES */
250	},
251	{
252	  NGM_GENERIC_COOKIE,
253	  NGM_LISTNODES,
254	  "listnodes",
255	  NULL,
256	  &ng_generic_listnodes_type
257	},
258	{
259	  NGM_GENERIC_COOKIE,
260	  NGM_LISTTYPES,
261	  "listtypes",
262	  NULL,
263	  &ng_generic_typeinfo_type
264	},
265	{
266	  NGM_GENERIC_COOKIE,
267	  NGM_TEXT_STATUS,
268	  "textstatus",
269	  NULL,
270	  &ng_parse_string_type
271	},
272	{
273	  NGM_GENERIC_COOKIE,
274	  NGM_ASCII2BINARY,
275	  "ascii2binary",
276	  &ng_parse_ng_mesg_type,
277	  &ng_parse_ng_mesg_type
278	},
279	{
280	  NGM_GENERIC_COOKIE,
281	  NGM_BINARY2ASCII,
282	  "binary2ascii",
283	  &ng_parse_ng_mesg_type,
284	  &ng_parse_ng_mesg_type
285	},
286	{ 0 }
287};
288
289/************************************************************************
290			Node routines
291************************************************************************/
292
293/*
294 * Instantiate a node of the requested type
295 */
296int
297ng_make_node(const char *typename, node_p *nodepp)
298{
299	struct ng_type *type;
300
301	/* Check that the type makes sense */
302	if (typename == NULL) {
303		TRAP_ERROR;
304		return (EINVAL);
305	}
306
307	/* Locate the node type */
308	if ((type = ng_findtype(typename)) == NULL) {
309		char *path, filename[NG_TYPELEN + 4];
310		linker_file_t lf;
311		int error;
312
313		/* Not found, try to load it as a loadable module */
314		snprintf(filename, sizeof(filename), "ng_%s.ko", typename);
315		if ((path = linker_search_path(filename)) == NULL)
316			return (ENXIO);
317		error = linker_load_file(path, &lf);
318		FREE(path, M_LINKER);
319		if (error != 0)
320			return (error);
321		lf->userrefs++;		/* pretend loaded by the syscall */
322
323		/* Try again, as now the type should have linked itself in */
324		if ((type = ng_findtype(typename)) == NULL)
325			return (ENXIO);
326	}
327
328	/* Call the constructor */
329	if (type->constructor != NULL)
330		return ((*type->constructor)(nodepp));
331	else
332		return (ng_make_node_common(type, nodepp));
333}
334
335/*
336 * Generic node creation. Called by node constructors.
337 * The returned node has a reference count of 1.
338 */
339int
340ng_make_node_common(struct ng_type *type, node_p *nodepp)
341{
342	node_p node;
343
344	/* Require the node type to have been already installed */
345	if (ng_findtype(type->name) == NULL) {
346		TRAP_ERROR;
347		return (EINVAL);
348	}
349
350	/* Make a node and try attach it to the type */
351	MALLOC(node, node_p, sizeof(*node), M_NETGRAPH, M_WAITOK);
352	if (node == NULL) {
353		TRAP_ERROR;
354		return (ENOMEM);
355	}
356	bzero(node, sizeof(*node));
357	node->type = type;
358	node->refs++;				/* note reference */
359	type->refs++;
360
361	/* Link us into the node linked list */
362	LIST_INSERT_HEAD(&nodelist, node, nodes);
363
364	/* Initialize hook list for new node */
365	LIST_INIT(&node->hooks);
366
367	/* get an ID and put us in the hash chain */
368	node->ID = nextID++; /* 137 per second for 1 year before wrap */
369	LIST_INSERT_HEAD(&ID_hash[node->ID % ID_HASH_SIZE], node, idnodes);
370
371	/* Done */
372	*nodepp = node;
373	return (0);
374}
375
376/*
377 * Forceably start the shutdown process on a node. Either call
378 * it's shutdown method, or do the default shutdown if there is
379 * no type-specific method.
380 *
381 * Persistent nodes must have a type-specific method which
382 * resets the NG_INVALID flag.
383 */
384void
385ng_rmnode(node_p node)
386{
387	/* Check if it's already shutting down */
388	if ((node->flags & NG_INVALID) != 0)
389		return;
390
391	/* Add an extra reference so it doesn't go away during this */
392	node->refs++;
393
394	/* Mark it invalid so any newcomers know not to try use it */
395	node->flags |= NG_INVALID;
396
397	/* Ask the type if it has anything to do in this case */
398	if (node->type && node->type->shutdown)
399		(*node->type->shutdown)(node);
400	else {				/* do the default thing */
401		ng_unname(node);
402		ng_cutlinks(node);
403		ng_unref(node);
404	}
405
406	/* Remove extra reference, possibly the last */
407	ng_unref(node);
408}
409
410/*
411 * Called by the destructor to remove any STANDARD external references
412 */
413void
414ng_cutlinks(node_p node)
415{
416	hook_p  hook;
417
418	/* Make sure that this is set to stop infinite loops */
419	node->flags |= NG_INVALID;
420
421	/* If we have sleepers, wake them up; they'll see NG_INVALID */
422	if (node->sleepers)
423		wakeup(node);
424
425	/* Notify all remaining connected nodes to disconnect */
426	while ((hook = LIST_FIRST(&node->hooks)) != NULL)
427		ng_destroy_hook(hook);
428}
429
430/*
431 * Remove a reference to the node, possibly the last
432 */
433void
434ng_unref(node_p node)
435{
436	if (--node->refs <= 0) {
437		node->type->refs--;
438		LIST_REMOVE(node, nodes);
439		LIST_REMOVE(node, idnodes);
440		FREE(node, M_NETGRAPH);
441	}
442}
443
444/*
445 * Wait for a node to come ready. Returns a node with a reference count;
446 * don't forget to drop it when we are done with it using ng_release_node().
447 */
448int
449ng_wait_node(node_p node, char *msg)
450{
451	int s, error = 0;
452
453	if (msg == NULL)
454		msg = "netgraph";
455	s = splnet();
456	node->sleepers++;
457	node->refs++;		/* the sleeping process counts as a reference */
458	while ((node->flags & (NG_BUSY | NG_INVALID)) == NG_BUSY)
459		error = tsleep(node, (PZERO + 1) | PCATCH, msg, 0);
460	node->sleepers--;
461	if (node->flags & NG_INVALID) {
462		TRAP_ERROR;
463		error = ENXIO;
464	} else {
465		KASSERT(node->refs > 1,
466		    ("%s: refs=%d", __FUNCTION__, node->refs));
467		node->flags |= NG_BUSY;
468	}
469	splx(s);
470
471	/* Release the reference we had on it */
472	if (error != 0)
473		ng_unref(node);
474	return error;
475}
476
477/*
478 * Release a node acquired via ng_wait_node()
479 */
480void
481ng_release_node(node_p node)
482{
483	/* Declare that we don't want it */
484	node->flags &= ~NG_BUSY;
485
486	/* If we have sleepers, then wake them up */
487	if (node->sleepers)
488		wakeup(node);
489
490	/* We also have a reference.. drop it too */
491	ng_unref(node);
492}
493
494/************************************************************************
495			Node ID handling
496************************************************************************/
497static node_p
498ng_ID2node(ng_ID_t ID)
499{
500	node_p np;
501	LIST_FOREACH(np, &ID_hash[ID % ID_HASH_SIZE], idnodes) {
502		if (np->ID == ID)
503			break;
504	}
505	return(np);
506}
507
508ng_ID_t
509ng_node2ID(node_p node)
510{
511	return (node->ID);
512}
513
514/************************************************************************
515			Node name handling
516************************************************************************/
517
518/*
519 * Assign a node a name. Once assigned, the name cannot be changed.
520 */
521int
522ng_name_node(node_p node, const char *name)
523{
524	int i;
525
526	/* Check the name is valid */
527	for (i = 0; i < NG_NODELEN + 1; i++) {
528		if (name[i] == '\0' || name[i] == '.' || name[i] == ':')
529			break;
530	}
531	if (i == 0 || name[i] != '\0') {
532		TRAP_ERROR;
533		return (EINVAL);
534	}
535	if (ng_decodeidname(name) != 0) { /* valid IDs not allowed here */
536		TRAP_ERROR;
537		return (EINVAL);
538	}
539
540	/* Check the node isn't already named */
541	if (node->name != NULL) {
542		TRAP_ERROR;
543		return (EISCONN);
544	}
545
546	/* Check the name isn't already being used */
547	if (ng_findname(node, name) != NULL) {
548		TRAP_ERROR;
549		return (EADDRINUSE);
550	}
551
552	/* Allocate space and copy it */
553	MALLOC(node->name, char *, strlen(name) + 1, M_NETGRAPH, M_WAITOK);
554	if (node->name == NULL) {
555		TRAP_ERROR;
556		return (ENOMEM);
557	}
558	strcpy(node->name, name);
559
560	/* The name counts as a reference */
561	node->refs++;
562	return (0);
563}
564
565/*
566 * Find a node by absolute name. The name should NOT end with ':'
567 * The name "." means "this node" and "[xxx]" means "the node
568 * with ID (ie, at address) xxx".
569 *
570 * Returns the node if found, else NULL.
571 */
572node_p
573ng_findname(node_p this, const char *name)
574{
575	node_p node;
576	ng_ID_t temp;
577
578	/* "." means "this node" */
579	if (strcmp(name, ".") == 0)
580		return(this);
581
582	/* Check for name-by-ID */
583	if ((temp = ng_decodeidname(name)) != 0) {
584		return (ng_ID2node(temp));
585	}
586
587	/* Find node by name */
588	LIST_FOREACH(node, &nodelist, nodes) {
589		if (node->name != NULL && strcmp(node->name, name) == 0)
590			break;
591	}
592	return (node);
593}
594
595/*
596 * Decode a ID name, eg. "[f03034de]". Returns 0 if the
597 * string is not valid, otherwise returns the value.
598 */
599static ng_ID_t
600ng_decodeidname(const char *name)
601{
602	const int len = strlen(name);
603	char *eptr;
604	u_long val;
605
606	/* Check for proper length, brackets, no leading junk */
607	if (len < 3 || name[0] != '[' || name[len - 1] != ']'
608	    || !isxdigit(name[1]))
609		return (0);
610
611	/* Decode number */
612	val = strtoul(name + 1, &eptr, 16);
613	if (eptr - name != len - 1 || val == ULONG_MAX || val == 0)
614		return ((ng_ID_t)0);
615	return (ng_ID_t)val;
616}
617
618/*
619 * Remove a name from a node. This should only be called
620 * when shutting down and removing the node.
621 */
622void
623ng_unname(node_p node)
624{
625	if (node->name) {
626		FREE(node->name, M_NETGRAPH);
627		node->name = NULL;
628		ng_unref(node);
629	}
630}
631
632/************************************************************************
633			Hook routines
634
635 Names are not optional. Hooks are always connected, except for a
636 brief moment within these routines.
637
638************************************************************************/
639
640/*
641 * Remove a hook reference
642 */
643static void
644ng_unref_hook(hook_p hook)
645{
646	if (--hook->refs == 0)
647		FREE(hook, M_NETGRAPH);
648}
649
650/*
651 * Add an unconnected hook to a node. Only used internally.
652 */
653static int
654ng_add_hook(node_p node, const char *name, hook_p *hookp)
655{
656	hook_p hook;
657	int error = 0;
658
659	/* Check that the given name is good */
660	if (name == NULL) {
661		TRAP_ERROR;
662		return (EINVAL);
663	}
664	if (ng_findhook(node, name) != NULL) {
665		TRAP_ERROR;
666		return (EEXIST);
667	}
668
669	/* Allocate the hook and link it up */
670	MALLOC(hook, hook_p, sizeof(*hook), M_NETGRAPH, M_WAITOK);
671	if (hook == NULL) {
672		TRAP_ERROR;
673		return (ENOMEM);
674	}
675	bzero(hook, sizeof(*hook));
676	hook->refs = 1;
677	hook->flags = HK_INVALID;
678	hook->node = node;
679	node->refs++;		/* each hook counts as a reference */
680
681	/* Check if the node type code has something to say about it */
682	if (node->type->newhook != NULL)
683		if ((error = (*node->type->newhook)(node, hook, name)) != 0)
684			goto fail;
685
686	/*
687	 * The 'type' agrees so far, so go ahead and link it in.
688	 * We'll ask again later when we actually connect the hooks.
689	 */
690	LIST_INSERT_HEAD(&node->hooks, hook, hooks);
691	node->numhooks++;
692
693	/* Set hook name */
694	MALLOC(hook->name, char *, strlen(name) + 1, M_NETGRAPH, M_WAITOK);
695	if (hook->name == NULL) {
696		error = ENOMEM;
697		LIST_REMOVE(hook, hooks);
698		node->numhooks--;
699fail:
700		hook->node = NULL;
701		ng_unref(node);
702		ng_unref_hook(hook);	/* this frees the hook */
703		return (error);
704	}
705	strcpy(hook->name, name);
706	if (hookp)
707		*hookp = hook;
708	return (error);
709}
710
711/*
712 * Connect a pair of hooks. Only used internally.
713 */
714static int
715ng_connect(hook_p hook1, hook_p hook2)
716{
717	int     error;
718
719	hook1->peer = hook2;
720	hook2->peer = hook1;
721
722	/* Give each node the opportunity to veto the impending connection */
723	if (hook1->node->type->connect) {
724		if ((error = (*hook1->node->type->connect) (hook1))) {
725			ng_destroy_hook(hook1);	/* also zaps hook2 */
726			return (error);
727		}
728	}
729	if (hook2->node->type->connect) {
730		if ((error = (*hook2->node->type->connect) (hook2))) {
731			ng_destroy_hook(hook2);	/* also zaps hook1 */
732			return (error);
733		}
734	}
735	hook1->flags &= ~HK_INVALID;
736	hook2->flags &= ~HK_INVALID;
737	return (0);
738}
739
740/*
741 * Find a hook
742 *
743 * Node types may supply their own optimized routines for finding
744 * hooks.  If none is supplied, we just do a linear search.
745 */
746hook_p
747ng_findhook(node_p node, const char *name)
748{
749	hook_p hook;
750
751	if (node->type->findhook != NULL)
752		return (*node->type->findhook)(node, name);
753	LIST_FOREACH(hook, &node->hooks, hooks) {
754		if (hook->name != NULL && strcmp(hook->name, name) == 0)
755			return (hook);
756	}
757	return (NULL);
758}
759
760/*
761 * Destroy a hook
762 *
763 * As hooks are always attached, this really destroys two hooks.
764 * The one given, and the one attached to it. Disconnect the hooks
765 * from each other first.
766 */
767void
768ng_destroy_hook(hook_p hook)
769{
770	hook_p peer = hook->peer;
771
772	hook->flags |= HK_INVALID;		/* as soon as possible */
773	if (peer) {
774		peer->flags |= HK_INVALID;	/* as soon as possible */
775		hook->peer = NULL;
776		peer->peer = NULL;
777		ng_disconnect_hook(peer);
778	}
779	ng_disconnect_hook(hook);
780}
781
782/*
783 * Notify the node of the hook's demise. This may result in more actions
784 * (e.g. shutdown) but we don't do that ourselves and don't know what
785 * happens there. If there is no appropriate handler, then just remove it
786 * (and decrement the reference count of it's node which in turn might
787 * make something happen).
788 */
789static void
790ng_disconnect_hook(hook_p hook)
791{
792	node_p node = hook->node;
793
794	/*
795	 * Remove the hook from the node's list to avoid possible recursion
796	 * in case the disconnection results in node shutdown.
797	 */
798	LIST_REMOVE(hook, hooks);
799	node->numhooks--;
800	if (node->type->disconnect) {
801		/*
802		 * The type handler may elect to destroy the peer so don't
803		 * trust its existance after this point.
804		 */
805		(*node->type->disconnect) (hook);
806	}
807	ng_unref(node);		/* might be the last reference */
808	if (hook->name)
809		FREE(hook->name, M_NETGRAPH);
810	hook->node = NULL;	/* may still be referenced elsewhere */
811	ng_unref_hook(hook);
812}
813
814/*
815 * Take two hooks on a node and merge the connection so that the given node
816 * is effectively bypassed.
817 */
818int
819ng_bypass(hook_p hook1, hook_p hook2)
820{
821	if (hook1->node != hook2->node)
822		return (EINVAL);
823	hook1->peer->peer = hook2->peer;
824	hook2->peer->peer = hook1->peer;
825
826	/* XXX If we ever cache methods on hooks update them as well */
827	hook1->peer = NULL;
828	hook2->peer = NULL;
829	ng_destroy_hook(hook1);
830	ng_destroy_hook(hook2);
831	return (0);
832}
833
834/*
835 * Install a new netgraph type
836 */
837int
838ng_newtype(struct ng_type *tp)
839{
840	const size_t namelen = strlen(tp->name);
841
842	/* Check version and type name fields */
843	if (tp->version != NG_VERSION || namelen == 0 || namelen > NG_TYPELEN) {
844		TRAP_ERROR;
845		return (EINVAL);
846	}
847
848	/* Check for name collision */
849	if (ng_findtype(tp->name) != NULL) {
850		TRAP_ERROR;
851		return (EEXIST);
852	}
853
854	/* Link in new type */
855	LIST_INSERT_HEAD(&typelist, tp, types);
856	tp->refs = 0;
857	return (0);
858}
859
860/*
861 * Look for a type of the name given
862 */
863struct ng_type *
864ng_findtype(const char *typename)
865{
866	struct ng_type *type;
867
868	LIST_FOREACH(type, &typelist, types) {
869		if (strcmp(type->name, typename) == 0)
870			break;
871	}
872	return (type);
873}
874
875
876/************************************************************************
877			Composite routines
878************************************************************************/
879
880/*
881 * Make a peer and connect. The order is arranged to minimise
882 * the work needed to back out in case of error.
883 */
884int
885ng_mkpeer(node_p node, const char *name, const char *name2, char *type)
886{
887	node_p  node2;
888	hook_p  hook;
889	hook_p  hook2;
890	int     error;
891
892	if ((error = ng_add_hook(node, name, &hook)))
893		return (error);
894	if ((error = ng_make_node(type, &node2))) {
895		ng_destroy_hook(hook);
896		return (error);
897	}
898	if ((error = ng_add_hook(node2, name2, &hook2))) {
899		ng_rmnode(node2);
900		ng_destroy_hook(hook);
901		return (error);
902	}
903
904	/*
905	 * Actually link the two hooks together.. on failure they are
906	 * destroyed so we don't have to do that here.
907	 */
908	if ((error = ng_connect(hook, hook2)))
909		ng_rmnode(node2);
910	return (error);
911}
912
913/*
914 * Connect two nodes using the specified hooks
915 */
916int
917ng_con_nodes(node_p node, const char *name, node_p node2, const char *name2)
918{
919	int     error;
920	hook_p  hook;
921	hook_p  hook2;
922
923	if ((error = ng_add_hook(node, name, &hook)))
924		return (error);
925	if ((error = ng_add_hook(node2, name2, &hook2))) {
926		ng_destroy_hook(hook);
927		return (error);
928	}
929	return (ng_connect(hook, hook2));
930}
931
932/*
933 * Parse and verify a string of the form:  <NODE:><PATH>
934 *
935 * Such a string can refer to a specific node or a specific hook
936 * on a specific node, depending on how you look at it. In the
937 * latter case, the PATH component must not end in a dot.
938 *
939 * Both <NODE:> and <PATH> are optional. The <PATH> is a string
940 * of hook names separated by dots. This breaks out the original
941 * string, setting *nodep to "NODE" (or NULL if none) and *pathp
942 * to "PATH" (or NULL if degenerate). Also, *hookp will point to
943 * the final hook component of <PATH>, if any, otherwise NULL.
944 *
945 * This returns -1 if the path is malformed. The char ** are optional.
946 */
947
948int
949ng_path_parse(char *addr, char **nodep, char **pathp, char **hookp)
950{
951	char   *node, *path, *hook;
952	int     k;
953
954	/*
955	 * Extract absolute NODE, if any
956	 */
957	for (path = addr; *path && *path != ':'; path++);
958	if (*path) {
959		node = addr;	/* Here's the NODE */
960		*path++ = '\0';	/* Here's the PATH */
961
962		/* Node name must not be empty */
963		if (!*node)
964			return -1;
965
966		/* A name of "." is OK; otherwise '.' not allowed */
967		if (strcmp(node, ".") != 0) {
968			for (k = 0; node[k]; k++)
969				if (node[k] == '.')
970					return -1;
971		}
972	} else {
973		node = NULL;	/* No absolute NODE */
974		path = addr;	/* Here's the PATH */
975	}
976
977	/* Snoop for illegal characters in PATH */
978	for (k = 0; path[k]; k++)
979		if (path[k] == ':')
980			return -1;
981
982	/* Check for no repeated dots in PATH */
983	for (k = 0; path[k]; k++)
984		if (path[k] == '.' && path[k + 1] == '.')
985			return -1;
986
987	/* Remove extra (degenerate) dots from beginning or end of PATH */
988	if (path[0] == '.')
989		path++;
990	if (*path && path[strlen(path) - 1] == '.')
991		path[strlen(path) - 1] = 0;
992
993	/* If PATH has a dot, then we're not talking about a hook */
994	if (*path) {
995		for (hook = path, k = 0; path[k]; k++)
996			if (path[k] == '.') {
997				hook = NULL;
998				break;
999			}
1000	} else
1001		path = hook = NULL;
1002
1003	/* Done */
1004	if (nodep)
1005		*nodep = node;
1006	if (pathp)
1007		*pathp = path;
1008	if (hookp)
1009		*hookp = hook;
1010	return (0);
1011}
1012
1013/*
1014 * Given a path, which may be absolute or relative, and a starting node,
1015 * return the destination node. Compute the "return address" if desired.
1016 */
1017int
1018ng_path2node(node_p here, const char *address, node_p *destp, char **rtnp)
1019{
1020	const	node_p start = here;
1021	char    fullpath[NG_PATHLEN + 1];
1022	char   *nodename, *path, pbuf[2];
1023	node_p  node;
1024	char   *cp;
1025
1026	/* Initialize */
1027	if (rtnp)
1028		*rtnp = NULL;
1029	if (destp == NULL)
1030		return EINVAL;
1031	*destp = NULL;
1032
1033	/* Make a writable copy of address for ng_path_parse() */
1034	strncpy(fullpath, address, sizeof(fullpath) - 1);
1035	fullpath[sizeof(fullpath) - 1] = '\0';
1036
1037	/* Parse out node and sequence of hooks */
1038	if (ng_path_parse(fullpath, &nodename, &path, NULL) < 0) {
1039		TRAP_ERROR;
1040		return EINVAL;
1041	}
1042	if (path == NULL) {
1043		pbuf[0] = '.';	/* Needs to be writable */
1044		pbuf[1] = '\0';
1045		path = pbuf;
1046	}
1047
1048	/* For an absolute address, jump to the starting node */
1049	if (nodename) {
1050		node = ng_findname(here, nodename);
1051		if (node == NULL) {
1052			TRAP_ERROR;
1053			return (ENOENT);
1054		}
1055	} else
1056		node = here;
1057
1058	/* Now follow the sequence of hooks */
1059	for (cp = path; node != NULL && *cp != '\0'; ) {
1060		hook_p hook;
1061		char *segment;
1062
1063		/*
1064		 * Break out the next path segment. Replace the dot we just
1065		 * found with a NUL; "cp" points to the next segment (or the
1066		 * NUL at the end).
1067		 */
1068		for (segment = cp; *cp != '\0'; cp++) {
1069			if (*cp == '.') {
1070				*cp++ = '\0';
1071				break;
1072			}
1073		}
1074
1075		/* Empty segment */
1076		if (*segment == '\0')
1077			continue;
1078
1079		/* We have a segment, so look for a hook by that name */
1080		hook = ng_findhook(node, segment);
1081
1082		/* Can't get there from here... */
1083		if (hook == NULL
1084		    || hook->peer == NULL
1085		    || (hook->flags & HK_INVALID) != 0) {
1086			TRAP_ERROR;
1087			return (ENOENT);
1088		}
1089
1090		/* Hop on over to the next node */
1091		node = hook->peer->node;
1092	}
1093
1094	/* If node somehow missing, fail here (probably this is not needed) */
1095	if (node == NULL) {
1096		TRAP_ERROR;
1097		return (ENXIO);
1098	}
1099
1100	/* Now compute return address, i.e., the path to the sender */
1101	if (rtnp != NULL) {
1102		MALLOC(*rtnp, char *, NG_NODELEN + 2, M_NETGRAPH, M_WAITOK);
1103		if (*rtnp == NULL) {
1104			TRAP_ERROR;
1105			return (ENOMEM);
1106		}
1107		if (start->name != NULL)
1108			sprintf(*rtnp, "%s:", start->name);
1109		else
1110			sprintf(*rtnp, "[%x]:", ng_node2ID(start));
1111	}
1112
1113	/* Done */
1114	*destp = node;
1115	return (0);
1116}
1117
1118/*
1119 * Call the appropriate message handler for the object.
1120 * It is up to the message handler to free the message.
1121 * If it's a generic message, handle it generically, otherwise
1122 * call the type's message handler (if it exists)
1123 */
1124
1125#define CALL_MSG_HANDLER(error, node, msg, retaddr, resp)		\
1126do {									\
1127	if((msg)->header.typecookie == NGM_GENERIC_COOKIE) {		\
1128		(error) = ng_generic_msg((node), (msg),			\
1129				(retaddr), (resp));			\
1130	} else {							\
1131		if ((node)->type->rcvmsg != NULL) {			\
1132			(error) = (*(node)->type->rcvmsg)((node),	\
1133					(msg), (retaddr), (resp));	\
1134		} else {						\
1135			TRAP_ERROR;					\
1136			FREE((msg), M_NETGRAPH);			\
1137			(error) = EINVAL;				\
1138		}							\
1139	}								\
1140} while (0)
1141
1142
1143/*
1144 * Send a control message to a node
1145 */
1146int
1147ng_send_msg(node_p here, struct ng_mesg *msg, const char *address,
1148	    struct ng_mesg **rptr)
1149{
1150	node_p  dest = NULL;
1151	char   *retaddr = NULL;
1152	int     error;
1153
1154	/* Find the target node */
1155	error = ng_path2node(here, address, &dest, &retaddr);
1156	if (error) {
1157		FREE(msg, M_NETGRAPH);
1158		return error;
1159	}
1160
1161	/* Make sure the resp field is null before we start */
1162	if (rptr != NULL)
1163		*rptr = NULL;
1164
1165	CALL_MSG_HANDLER(error, dest, msg, retaddr, rptr);
1166
1167	/* Make sure that if there is a response, it has the RESP bit set */
1168	if ((error == 0) && rptr && *rptr)
1169		(*rptr)->header.flags |= NGF_RESP;
1170
1171	/*
1172	 * If we had a return address it is up to us to free it. They should
1173	 * have taken a copy if they needed to make a delayed response.
1174	 */
1175	if (retaddr)
1176		FREE(retaddr, M_NETGRAPH);
1177	return (error);
1178}
1179
1180/*
1181 * Implement the 'generic' control messages
1182 */
1183static int
1184ng_generic_msg(node_p here, struct ng_mesg *msg, const char *retaddr,
1185	       struct ng_mesg **resp)
1186{
1187	int error = 0;
1188
1189	if (msg->header.typecookie != NGM_GENERIC_COOKIE) {
1190		TRAP_ERROR;
1191		FREE(msg, M_NETGRAPH);
1192		return (EINVAL);
1193	}
1194	switch (msg->header.cmd) {
1195	case NGM_SHUTDOWN:
1196		ng_rmnode(here);
1197		break;
1198	case NGM_MKPEER:
1199	    {
1200		struct ngm_mkpeer *const mkp = (struct ngm_mkpeer *) msg->data;
1201
1202		if (msg->header.arglen != sizeof(*mkp)) {
1203			TRAP_ERROR;
1204			return (EINVAL);
1205		}
1206		mkp->type[sizeof(mkp->type) - 1] = '\0';
1207		mkp->ourhook[sizeof(mkp->ourhook) - 1] = '\0';
1208		mkp->peerhook[sizeof(mkp->peerhook) - 1] = '\0';
1209		error = ng_mkpeer(here, mkp->ourhook, mkp->peerhook, mkp->type);
1210		break;
1211	    }
1212	case NGM_CONNECT:
1213	    {
1214		struct ngm_connect *const con =
1215			(struct ngm_connect *) msg->data;
1216		node_p node2;
1217
1218		if (msg->header.arglen != sizeof(*con)) {
1219			TRAP_ERROR;
1220			return (EINVAL);
1221		}
1222		con->path[sizeof(con->path) - 1] = '\0';
1223		con->ourhook[sizeof(con->ourhook) - 1] = '\0';
1224		con->peerhook[sizeof(con->peerhook) - 1] = '\0';
1225		error = ng_path2node(here, con->path, &node2, NULL);
1226		if (error)
1227			break;
1228		error = ng_con_nodes(here, con->ourhook, node2, con->peerhook);
1229		break;
1230	    }
1231	case NGM_NAME:
1232	    {
1233		struct ngm_name *const nam = (struct ngm_name *) msg->data;
1234
1235		if (msg->header.arglen != sizeof(*nam)) {
1236			TRAP_ERROR;
1237			return (EINVAL);
1238		}
1239		nam->name[sizeof(nam->name) - 1] = '\0';
1240		error = ng_name_node(here, nam->name);
1241		break;
1242	    }
1243	case NGM_RMHOOK:
1244	    {
1245		struct ngm_rmhook *const rmh = (struct ngm_rmhook *) msg->data;
1246		hook_p hook;
1247
1248		if (msg->header.arglen != sizeof(*rmh)) {
1249			TRAP_ERROR;
1250			return (EINVAL);
1251		}
1252		rmh->ourhook[sizeof(rmh->ourhook) - 1] = '\0';
1253		if ((hook = ng_findhook(here, rmh->ourhook)) != NULL)
1254			ng_destroy_hook(hook);
1255		break;
1256	    }
1257	case NGM_NODEINFO:
1258	    {
1259		struct nodeinfo *ni;
1260		struct ng_mesg *rp;
1261
1262		/* Get response struct */
1263		if (resp == NULL) {
1264			error = EINVAL;
1265			break;
1266		}
1267		NG_MKRESPONSE(rp, msg, sizeof(*ni), M_NOWAIT);
1268		if (rp == NULL) {
1269			error = ENOMEM;
1270			break;
1271		}
1272
1273		/* Fill in node info */
1274		ni = (struct nodeinfo *) rp->data;
1275		if (here->name != NULL)
1276			strncpy(ni->name, here->name, NG_NODELEN);
1277		strncpy(ni->type, here->type->name, NG_TYPELEN);
1278		ni->id = ng_node2ID(here);
1279		ni->hooks = here->numhooks;
1280		*resp = rp;
1281		break;
1282	    }
1283	case NGM_LISTHOOKS:
1284	    {
1285		const int nhooks = here->numhooks;
1286		struct hooklist *hl;
1287		struct nodeinfo *ni;
1288		struct ng_mesg *rp;
1289		hook_p hook;
1290
1291		/* Get response struct */
1292		if (resp == NULL) {
1293			error = EINVAL;
1294			break;
1295		}
1296		NG_MKRESPONSE(rp, msg, sizeof(*hl)
1297		    + (nhooks * sizeof(struct linkinfo)), M_NOWAIT);
1298		if (rp == NULL) {
1299			error = ENOMEM;
1300			break;
1301		}
1302		hl = (struct hooklist *) rp->data;
1303		ni = &hl->nodeinfo;
1304
1305		/* Fill in node info */
1306		if (here->name)
1307			strncpy(ni->name, here->name, NG_NODELEN);
1308		strncpy(ni->type, here->type->name, NG_TYPELEN);
1309		ni->id = ng_node2ID(here);
1310
1311		/* Cycle through the linked list of hooks */
1312		ni->hooks = 0;
1313		LIST_FOREACH(hook, &here->hooks, hooks) {
1314			struct linkinfo *const link = &hl->link[ni->hooks];
1315
1316			if (ni->hooks >= nhooks) {
1317				log(LOG_ERR, "%s: number of %s changed\n",
1318				    __FUNCTION__, "hooks");
1319				break;
1320			}
1321			if ((hook->flags & HK_INVALID) != 0)
1322				continue;
1323			strncpy(link->ourhook, hook->name, NG_HOOKLEN);
1324			strncpy(link->peerhook, hook->peer->name, NG_HOOKLEN);
1325			if (hook->peer->node->name != NULL)
1326				strncpy(link->nodeinfo.name,
1327				    hook->peer->node->name, NG_NODELEN);
1328			strncpy(link->nodeinfo.type,
1329			   hook->peer->node->type->name, NG_TYPELEN);
1330			link->nodeinfo.id = ng_node2ID(hook->peer->node);
1331			link->nodeinfo.hooks = hook->peer->node->numhooks;
1332			ni->hooks++;
1333		}
1334		*resp = rp;
1335		break;
1336	    }
1337
1338	case NGM_LISTNAMES:
1339	case NGM_LISTNODES:
1340	    {
1341		const int unnamed = (msg->header.cmd == NGM_LISTNODES);
1342		struct namelist *nl;
1343		struct ng_mesg *rp;
1344		node_p node;
1345		int num = 0;
1346
1347		if (resp == NULL) {
1348			error = EINVAL;
1349			break;
1350		}
1351
1352		/* Count number of nodes */
1353		LIST_FOREACH(node, &nodelist, nodes) {
1354			if (unnamed || node->name != NULL)
1355				num++;
1356		}
1357
1358		/* Get response struct */
1359		if (resp == NULL) {
1360			error = EINVAL;
1361			break;
1362		}
1363		NG_MKRESPONSE(rp, msg, sizeof(*nl)
1364		    + (num * sizeof(struct nodeinfo)), M_NOWAIT);
1365		if (rp == NULL) {
1366			error = ENOMEM;
1367			break;
1368		}
1369		nl = (struct namelist *) rp->data;
1370
1371		/* Cycle through the linked list of nodes */
1372		nl->numnames = 0;
1373		LIST_FOREACH(node, &nodelist, nodes) {
1374			struct nodeinfo *const np = &nl->nodeinfo[nl->numnames];
1375
1376			if (nl->numnames >= num) {
1377				log(LOG_ERR, "%s: number of %s changed\n",
1378				    __FUNCTION__, "nodes");
1379				break;
1380			}
1381			if ((node->flags & NG_INVALID) != 0)
1382				continue;
1383			if (!unnamed && node->name == NULL)
1384				continue;
1385			if (node->name != NULL)
1386				strncpy(np->name, node->name, NG_NODELEN);
1387			strncpy(np->type, node->type->name, NG_TYPELEN);
1388			np->id = ng_node2ID(node);
1389			np->hooks = node->numhooks;
1390			nl->numnames++;
1391		}
1392		*resp = rp;
1393		break;
1394	    }
1395
1396	case NGM_LISTTYPES:
1397	    {
1398		struct typelist *tl;
1399		struct ng_mesg *rp;
1400		struct ng_type *type;
1401		int num = 0;
1402
1403		if (resp == NULL) {
1404			error = EINVAL;
1405			break;
1406		}
1407
1408		/* Count number of types */
1409		LIST_FOREACH(type, &typelist, types)
1410			num++;
1411
1412		/* Get response struct */
1413		if (resp == NULL) {
1414			error = EINVAL;
1415			break;
1416		}
1417		NG_MKRESPONSE(rp, msg, sizeof(*tl)
1418		    + (num * sizeof(struct typeinfo)), M_NOWAIT);
1419		if (rp == NULL) {
1420			error = ENOMEM;
1421			break;
1422		}
1423		tl = (struct typelist *) rp->data;
1424
1425		/* Cycle through the linked list of types */
1426		tl->numtypes = 0;
1427		LIST_FOREACH(type, &typelist, types) {
1428			struct typeinfo *const tp = &tl->typeinfo[tl->numtypes];
1429
1430			if (tl->numtypes >= num) {
1431				log(LOG_ERR, "%s: number of %s changed\n",
1432				    __FUNCTION__, "types");
1433				break;
1434			}
1435			strncpy(tp->typename, type->name, NG_TYPELEN);
1436			tp->numnodes = type->refs;
1437			tl->numtypes++;
1438		}
1439		*resp = rp;
1440		break;
1441	    }
1442
1443	case NGM_BINARY2ASCII:
1444	    {
1445		int bufSize = 2000;	/* XXX hard coded constant */
1446		const struct ng_parse_type *argstype;
1447		const struct ng_cmdlist *c;
1448		struct ng_mesg *rp, *binary, *ascii;
1449
1450		/* Data area must contain a valid netgraph message */
1451		binary = (struct ng_mesg *)msg->data;
1452		if (msg->header.arglen < sizeof(struct ng_mesg)
1453		    || msg->header.arglen - sizeof(struct ng_mesg)
1454		      < binary->header.arglen) {
1455			error = EINVAL;
1456			break;
1457		}
1458
1459		/* Get a response message with lots of room */
1460		NG_MKRESPONSE(rp, msg, sizeof(*ascii) + bufSize, M_NOWAIT);
1461		if (rp == NULL) {
1462			error = ENOMEM;
1463			break;
1464		}
1465		ascii = (struct ng_mesg *)rp->data;
1466
1467		/* Copy binary message header to response message payload */
1468		bcopy(binary, ascii, sizeof(*binary));
1469
1470		/* Find command by matching typecookie and command number */
1471		for (c = here->type->cmdlist;
1472		    c != NULL && c->name != NULL; c++) {
1473			if (binary->header.typecookie == c->cookie
1474			    && binary->header.cmd == c->cmd)
1475				break;
1476		}
1477		if (c == NULL || c->name == NULL) {
1478			for (c = ng_generic_cmds; c->name != NULL; c++) {
1479				if (binary->header.typecookie == c->cookie
1480				    && binary->header.cmd == c->cmd)
1481					break;
1482			}
1483			if (c->name == NULL) {
1484				FREE(rp, M_NETGRAPH);
1485				error = ENOSYS;
1486				break;
1487			}
1488		}
1489
1490		/* Convert command name to ASCII */
1491		snprintf(ascii->header.cmdstr, sizeof(ascii->header.cmdstr),
1492		    "%s", c->name);
1493
1494		/* Convert command arguments to ASCII */
1495		argstype = (binary->header.flags & NGF_RESP) ?
1496		    c->respType : c->mesgType;
1497		if (argstype == NULL)
1498			*ascii->data = '\0';
1499		else {
1500			if ((error = ng_unparse(argstype,
1501			    (u_char *)binary->data,
1502			    ascii->data, bufSize)) != 0) {
1503				FREE(rp, M_NETGRAPH);
1504				break;
1505			}
1506		}
1507
1508		/* Return the result as struct ng_mesg plus ASCII string */
1509		bufSize = strlen(ascii->data) + 1;
1510		ascii->header.arglen = bufSize;
1511		rp->header.arglen = sizeof(*ascii) + bufSize;
1512		*resp = rp;
1513		break;
1514	    }
1515
1516	case NGM_ASCII2BINARY:
1517	    {
1518		int bufSize = 2000;	/* XXX hard coded constant */
1519		const struct ng_cmdlist *c;
1520		const struct ng_parse_type *argstype;
1521		struct ng_mesg *rp, *ascii, *binary;
1522		int off;
1523
1524		/* Data area must contain at least a struct ng_mesg + '\0' */
1525		ascii = (struct ng_mesg *)msg->data;
1526		if (msg->header.arglen < sizeof(*ascii) + 1
1527		    || ascii->header.arglen < 1
1528		    || msg->header.arglen
1529		      < sizeof(*ascii) + ascii->header.arglen) {
1530			error = EINVAL;
1531			break;
1532		}
1533		ascii->data[ascii->header.arglen - 1] = '\0';
1534
1535		/* Get a response message with lots of room */
1536		NG_MKRESPONSE(rp, msg, sizeof(*binary) + bufSize, M_NOWAIT);
1537		if (rp == NULL) {
1538			error = ENOMEM;
1539			break;
1540		}
1541		binary = (struct ng_mesg *)rp->data;
1542
1543		/* Copy ASCII message header to response message payload */
1544		bcopy(ascii, binary, sizeof(*ascii));
1545
1546		/* Find command by matching ASCII command string */
1547		for (c = here->type->cmdlist;
1548		    c != NULL && c->name != NULL; c++) {
1549			if (strcmp(ascii->header.cmdstr, c->name) == 0)
1550				break;
1551		}
1552		if (c == NULL || c->name == NULL) {
1553			for (c = ng_generic_cmds; c->name != NULL; c++) {
1554				if (strcmp(ascii->header.cmdstr, c->name) == 0)
1555					break;
1556			}
1557			if (c->name == NULL) {
1558				FREE(rp, M_NETGRAPH);
1559				error = ENOSYS;
1560				break;
1561			}
1562		}
1563
1564		/* Convert command name to binary */
1565		binary->header.cmd = c->cmd;
1566		binary->header.typecookie = c->cookie;
1567
1568		/* Convert command arguments to binary */
1569		argstype = (binary->header.flags & NGF_RESP) ?
1570		    c->respType : c->mesgType;
1571		if (argstype == NULL)
1572			bufSize = 0;
1573		else {
1574			if ((error = ng_parse(argstype, ascii->data,
1575			    &off, (u_char *)binary->data, &bufSize)) != 0) {
1576				FREE(rp, M_NETGRAPH);
1577				break;
1578			}
1579		}
1580
1581		/* Return the result */
1582		binary->header.arglen = bufSize;
1583		rp->header.arglen = sizeof(*binary) + bufSize;
1584		*resp = rp;
1585		break;
1586	    }
1587
1588	case NGM_TEXT_STATUS:
1589		/*
1590		 * This one is tricky as it passes the command down to the
1591		 * actual node, even though it is a generic type command.
1592		 * This means we must assume that the msg is already freed
1593		 * when control passes back to us.
1594		 */
1595		if (resp == NULL) {
1596			error = EINVAL;
1597			break;
1598		}
1599		if (here->type->rcvmsg != NULL)
1600			return((*here->type->rcvmsg)(here, msg, retaddr, resp));
1601		/* Fall through if rcvmsg not supported */
1602	default:
1603		TRAP_ERROR;
1604		error = EINVAL;
1605	}
1606	FREE(msg, M_NETGRAPH);
1607	return (error);
1608}
1609
1610/*
1611 * Send a data packet to a node. If the recipient has no
1612 * 'receive data' method, then silently discard the packet.
1613 */
1614int
1615ng_send_data(hook_p hook, struct mbuf *m, meta_p meta)
1616{
1617	int (*rcvdata)(hook_p, struct mbuf *, meta_p);
1618	int error;
1619
1620	CHECK_DATA_MBUF(m);
1621	if (hook && (hook->flags & HK_INVALID) == 0) {
1622		rcvdata = hook->peer->node->type->rcvdata;
1623		if (rcvdata != NULL)
1624			error = (*rcvdata)(hook->peer, m, meta);
1625		else {
1626			error = 0;
1627			NG_FREE_DATA(m, meta);
1628		}
1629	} else {
1630		TRAP_ERROR;
1631		error = ENOTCONN;
1632		NG_FREE_DATA(m, meta);
1633	}
1634	return (error);
1635}
1636
1637/*
1638 * Send a queued data packet to a node. If the recipient has no
1639 * 'receive queued data' method, then try the 'receive data' method above.
1640 */
1641int
1642ng_send_dataq(hook_p hook, struct mbuf *m, meta_p meta)
1643{
1644	int (*rcvdataq)(hook_p, struct mbuf *, meta_p);
1645	int error;
1646
1647	CHECK_DATA_MBUF(m);
1648	if (hook && (hook->flags & HK_INVALID) == 0) {
1649		rcvdataq = hook->peer->node->type->rcvdataq;
1650		if (rcvdataq != NULL)
1651			error = (*rcvdataq)(hook->peer, m, meta);
1652		else {
1653			error = ng_send_data(hook, m, meta);
1654		}
1655	} else {
1656		TRAP_ERROR;
1657		error = ENOTCONN;
1658		NG_FREE_DATA(m, meta);
1659	}
1660	return (error);
1661}
1662
1663/************************************************************************
1664			Module routines
1665************************************************************************/
1666
1667/*
1668 * Handle the loading/unloading of a netgraph node type module
1669 */
1670int
1671ng_mod_event(module_t mod, int event, void *data)
1672{
1673	struct ng_type *const type = data;
1674	int s, error = 0;
1675
1676	switch (event) {
1677	case MOD_LOAD:
1678
1679		/* Register new netgraph node type */
1680		s = splnet();
1681		if ((error = ng_newtype(type)) != 0) {
1682			splx(s);
1683			break;
1684		}
1685
1686		/* Call type specific code */
1687		if (type->mod_event != NULL)
1688			if ((error = (*type->mod_event)(mod, event, data)) != 0)
1689				LIST_REMOVE(type, types);
1690		splx(s);
1691		break;
1692
1693	case MOD_UNLOAD:
1694		s = splnet();
1695		if (type->refs != 0)		/* make sure no nodes exist! */
1696			error = EBUSY;
1697		else {
1698			if (type->mod_event != NULL) {	/* check with type */
1699				error = (*type->mod_event)(mod, event, data);
1700				if (error != 0) {	/* type refuses.. */
1701					splx(s);
1702					break;
1703				}
1704			}
1705			LIST_REMOVE(type, types);
1706		}
1707		splx(s);
1708		break;
1709
1710	default:
1711		if (type->mod_event != NULL)
1712			error = (*type->mod_event)(mod, event, data);
1713		else
1714			error = 0;		/* XXX ? */
1715		break;
1716	}
1717	return (error);
1718}
1719
1720/*
1721 * Handle loading and unloading for this code.
1722 * The only thing we need to link into is the NETISR strucure.
1723 */
1724static int
1725ngb_mod_event(module_t mod, int event, void *data)
1726{
1727	int s, error = 0;
1728
1729	switch (event) {
1730	case MOD_LOAD:
1731		/* Register line discipline */
1732		s = splimp();
1733		error = register_netisr(NETISR_NETGRAPH, ngintr);
1734		splx(s);
1735		break;
1736	case MOD_UNLOAD:
1737		/* You cant unload it because an interface may be using it.  */
1738		error = EBUSY;
1739		break;
1740	default:
1741		error = EOPNOTSUPP;
1742		break;
1743	}
1744	return (error);
1745}
1746
1747static moduledata_t netgraph_mod = {
1748	"netgraph",
1749	ngb_mod_event,
1750	(NULL)
1751};
1752DECLARE_MODULE(netgraph, netgraph_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
1753
1754/************************************************************************
1755			Queueing routines
1756************************************************************************/
1757
1758/* The structure for queueing across ISR switches */
1759struct ng_queue_entry {
1760	u_long	flags;
1761	struct ng_queue_entry *next;
1762	union {
1763		struct {
1764			hook_p		da_hook;	/*  target hook */
1765			struct mbuf	*da_m;
1766			meta_p		da_meta;
1767		} data;
1768		struct {
1769			struct ng_mesg	*msg_msg;
1770			node_p		msg_node;
1771			void		*msg_retaddr;
1772		} msg;
1773	} body;
1774};
1775#define NGQF_DATA	0x01		/* the queue element is data */
1776#define NGQF_MESG	0x02		/* the queue element is a message */
1777
1778static struct ng_queue_entry   *ngqbase;	/* items to be unqueued */
1779static struct ng_queue_entry   *ngqlast;	/* last item queued */
1780static const int		ngqroom = 64;	/* max items to queue */
1781static int			ngqsize;	/* number of items in queue */
1782
1783static struct ng_queue_entry   *ngqfree;	/* free ones */
1784static const int		ngqfreemax = 16;/* cache at most this many */
1785static int			ngqfreesize;	/* number of cached entries */
1786
1787/*
1788 * Get a queue entry
1789 */
1790static struct ng_queue_entry *
1791ng_getqblk(void)
1792{
1793	register struct ng_queue_entry *q;
1794	int s;
1795
1796	/* Could be guarding against tty ints or whatever */
1797	s = splhigh();
1798
1799	/* Try get a cached queue block, or else allocate a new one */
1800	if ((q = ngqfree) == NULL) {
1801		splx(s);
1802		if (ngqsize < ngqroom) {	/* don't worry about races */
1803			MALLOC(q, struct ng_queue_entry *,
1804			    sizeof(*q), M_NETGRAPH, M_NOWAIT);
1805		}
1806	} else {
1807		ngqfree = q->next;
1808		ngqfreesize--;
1809		splx(s);
1810	}
1811	return (q);
1812}
1813
1814/*
1815 * Release a queue entry
1816 */
1817#define RETURN_QBLK(q)							\
1818do {									\
1819	int s;								\
1820	if (ngqfreesize < ngqfreemax) { /* don't worry about races */ 	\
1821		s = splhigh();						\
1822		(q)->next = ngqfree;					\
1823		ngqfree = (q);						\
1824		ngqfreesize++;						\
1825		splx(s);						\
1826	} else {							\
1827		FREE((q), M_NETGRAPH);					\
1828	}								\
1829} while (0)
1830
1831/*
1832 * Running at a raised (but we don't know which) processor priority level,
1833 * put the data onto a queue to be picked up by another PPL (probably splnet)
1834 */
1835int
1836ng_queue_data(hook_p hook, struct mbuf *m, meta_p meta)
1837{
1838	struct ng_queue_entry *q;
1839	int s;
1840
1841	if (hook == NULL) {
1842		NG_FREE_DATA(m, meta);
1843		return (0);
1844	}
1845	if ((q = ng_getqblk()) == NULL) {
1846		NG_FREE_DATA(m, meta);
1847		return (ENOBUFS);
1848	}
1849
1850	/* Fill out the contents */
1851	q->flags = NGQF_DATA;
1852	q->next = NULL;
1853	q->body.data.da_hook = hook;
1854	q->body.data.da_m = m;
1855	q->body.data.da_meta = meta;
1856	hook->refs++;		/* don't let it go away while on the queue */
1857
1858	/* Put it on the queue */
1859	s = splhigh();
1860	if (ngqbase) {
1861		ngqlast->next = q;
1862	} else {
1863		ngqbase = q;
1864	}
1865	ngqlast = q;
1866	ngqsize++;
1867	splx(s);
1868
1869	/* Schedule software interrupt to handle it later */
1870	schednetisr(NETISR_NETGRAPH);
1871	return (0);
1872}
1873
1874/*
1875 * Running at a raised (but we don't know which) processor priority level,
1876 * put the msg onto a queue to be picked up by another PPL (probably splnet)
1877 */
1878int
1879ng_queue_msg(node_p here, struct ng_mesg * msg, int len, const char *address)
1880{
1881	register struct ng_queue_entry *q;
1882	int     s;
1883	node_p  dest = NULL;
1884	char   *retaddr = NULL;
1885	int     error;
1886
1887	/* Find the target node. */
1888	error = ng_path2node(here, address, &dest, &retaddr);
1889	if (error) {
1890		FREE(msg, M_NETGRAPH);
1891		return (error);
1892	}
1893	if ((q = ng_getqblk()) == NULL) {
1894		FREE(msg, M_NETGRAPH);
1895		if (retaddr)
1896			FREE(retaddr, M_NETGRAPH);
1897		return (ENOBUFS);
1898	}
1899
1900	/* Fill out the contents */
1901	q->flags = NGQF_MESG;
1902	q->next = NULL;
1903	q->body.msg.msg_node = dest;
1904	q->body.msg.msg_msg = msg;
1905	q->body.msg.msg_retaddr = retaddr;
1906	dest->refs++;		/* don't let it go away while on the queue */
1907
1908	/* Put it on the queue */
1909	s = splhigh();
1910	if (ngqbase) {
1911		ngqlast->next = q;
1912	} else {
1913		ngqbase = q;
1914	}
1915	ngqlast = q;
1916	ngqsize++;
1917	splx(s);
1918
1919	/* Schedule software interrupt to handle it later */
1920	schednetisr(NETISR_NETGRAPH);
1921	return (0);
1922}
1923
1924/*
1925 * Pick an item off the queue, process it, and dispose of the queue entry.
1926 * Should be running at splnet.
1927 */
1928static void
1929ngintr(void)
1930{
1931	hook_p  hook;
1932	struct ng_queue_entry *ngq;
1933	struct mbuf *m;
1934	meta_p  meta;
1935	void   *retaddr;
1936	struct ng_mesg *msg;
1937	node_p  node;
1938	int     error = 0;
1939	int     s;
1940
1941	while (1) {
1942		s = splhigh();
1943		if ((ngq = ngqbase)) {
1944			ngqbase = ngq->next;
1945			ngqsize--;
1946		}
1947		splx(s);
1948		if (ngq == NULL)
1949			return;
1950		switch (ngq->flags) {
1951		case NGQF_DATA:
1952			hook = ngq->body.data.da_hook;
1953			m = ngq->body.data.da_m;
1954			meta = ngq->body.data.da_meta;
1955			RETURN_QBLK(ngq);
1956			NG_SEND_DATAQ(error, hook, m, meta);
1957			ng_unref_hook(hook);
1958			break;
1959		case NGQF_MESG:
1960			node = ngq->body.msg.msg_node;
1961			msg = ngq->body.msg.msg_msg;
1962			retaddr = ngq->body.msg.msg_retaddr;
1963			RETURN_QBLK(ngq);
1964			if (node->flags & NG_INVALID) {
1965				FREE(msg, M_NETGRAPH);
1966			} else {
1967				CALL_MSG_HANDLER(error, node, msg,
1968						 retaddr, NULL);
1969			}
1970			ng_unref(node);
1971			if (retaddr)
1972				FREE(retaddr, M_NETGRAPH);
1973			break;
1974		default:
1975			RETURN_QBLK(ngq);
1976		}
1977	}
1978}
1979
1980
1981