ng_base.c revision 59879
152419Sjulian
252419Sjulian/*
352419Sjulian * ng_base.c
452419Sjulian *
552419Sjulian * Copyright (c) 1996-1999 Whistle Communications, Inc.
652419Sjulian * All rights reserved.
752419Sjulian *
852419Sjulian * Subject to the following obligations and disclaimer of warranty, use and
952419Sjulian * redistribution of this software, in source or object code forms, with or
1052419Sjulian * without modifications are expressly permitted by Whistle Communications;
1152419Sjulian * provided, however, that:
1252419Sjulian * 1. Any and all reproductions of the source or object code must include the
1352419Sjulian *    copyright notice above and the following disclaimer of warranties; and
1452419Sjulian * 2. No rights are granted, in any manner or form, to use Whistle
1552419Sjulian *    Communications, Inc. trademarks, including the mark "WHISTLE
1652419Sjulian *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
1752419Sjulian *    such appears in the above copyright notice or in the software.
1852419Sjulian *
1952419Sjulian * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
2052419Sjulian * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
2152419Sjulian * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
2252419Sjulian * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
2352419Sjulian * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
2452419Sjulian * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
2552419Sjulian * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
2652419Sjulian * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
2752419Sjulian * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
2852419Sjulian * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
2952419Sjulian * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
3052419Sjulian * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
3152419Sjulian * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
3252419Sjulian * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3352419Sjulian * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
3452419Sjulian * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
3552419Sjulian * OF SUCH DAMAGE.
3652419Sjulian *
3752419Sjulian * Authors: Julian Elischer <julian@whistle.com>
3852419Sjulian *          Archie Cobbs <archie@whistle.com>
3952419Sjulian *
4052419Sjulian * $FreeBSD: head/sys/netgraph/ng_base.c 59879 2000-05-01 23:29:19Z archie $
4152419Sjulian * $Whistle: ng_base.c,v 1.39 1999/01/28 23:54:53 julian Exp $
4252419Sjulian */
4352419Sjulian
4452419Sjulian/*
4552419Sjulian * This file implements the base netgraph code.
4652419Sjulian */
4752419Sjulian
4852419Sjulian#include <sys/param.h>
4952419Sjulian#include <sys/systm.h>
5052419Sjulian#include <sys/errno.h>
5152419Sjulian#include <sys/kernel.h>
5252419Sjulian#include <sys/malloc.h>
5352419Sjulian#include <sys/syslog.h>
5452419Sjulian#include <sys/linker.h>
5552419Sjulian#include <sys/queue.h>
5652419Sjulian#include <sys/mbuf.h>
5752843Sphk#include <sys/ctype.h>
5852816Sarchie#include <machine/limits.h>
5952419Sjulian
6052419Sjulian#include <net/netisr.h>
6152419Sjulian
6252419Sjulian#include <netgraph/ng_message.h>
6352419Sjulian#include <netgraph/netgraph.h>
6453913Sarchie#include <netgraph/ng_parse.h>
6552419Sjulian
6659756SpeterMODULE_VERSION(netgraph, 1);
6759756Speter
6852419Sjulian/* List of all nodes */
6952419Sjulianstatic LIST_HEAD(, ng_node) nodelist;
7052419Sjulian
7152419Sjulian/* List of installed types */
7252419Sjulianstatic LIST_HEAD(, ng_type) typelist;
7352419Sjulian
7452722Sjulian/* Hash releted definitions */
7552722Sjulian#define ID_HASH_SIZE 32 /* most systems wont need even this many */
7652722Sjulianstatic LIST_HEAD(, ng_node) ID_hash[ID_HASH_SIZE];
7752722Sjulian/* Don't nead to initialise them because it's a LIST */
7852722Sjulian
7952419Sjulian/* Internal functions */
8052419Sjulianstatic int	ng_add_hook(node_p node, const char *name, hook_p * hookp);
8152419Sjulianstatic int	ng_connect(hook_p hook1, hook_p hook2);
8252419Sjulianstatic void	ng_disconnect_hook(hook_p hook);
8352419Sjulianstatic int	ng_generic_msg(node_p here, struct ng_mesg *msg,
8459728Sjulian			const char *retaddr, struct ng_mesg ** resp,
8559728Sjulian			hook_p hook);
8652722Sjulianstatic ng_ID_t	ng_decodeidname(const char *name);
8752419Sjulianstatic int	ngb_mod_event(module_t mod, int event, void *data);
8852419Sjulianstatic void	ngintr(void);
8952419Sjulian
9052419Sjulian/* Our own netgraph malloc type */
9152419SjulianMALLOC_DEFINE(M_NETGRAPH, "netgraph", "netgraph structures and ctrl messages");
9252419Sjulian
9352419Sjulian/* Set this to Debugger("X") to catch all errors as they occur */
9452419Sjulian#ifndef TRAP_ERROR
9552419Sjulian#define TRAP_ERROR
9652419Sjulian#endif
9752419Sjulian
9852722Sjulianstatic	ng_ID_t nextID = 1;
9952722Sjulian
10053403Sarchie#ifdef INVARIANTS
10153403Sarchie#define CHECK_DATA_MBUF(m)	do {					\
10253403Sarchie		struct mbuf *n;						\
10353403Sarchie		int total;						\
10453403Sarchie									\
10553403Sarchie		if (((m)->m_flags & M_PKTHDR) == 0)			\
10653403Sarchie			panic("%s: !PKTHDR", __FUNCTION__);		\
10753403Sarchie		for (total = 0, n = (m); n != NULL; n = n->m_next)	\
10853403Sarchie			total += n->m_len;				\
10953403Sarchie		if ((m)->m_pkthdr.len != total) {			\
11053403Sarchie			panic("%s: %d != %d",				\
11153403Sarchie			    __FUNCTION__, (m)->m_pkthdr.len, total);	\
11253403Sarchie		}							\
11353403Sarchie	} while (0)
11453403Sarchie#else
11553403Sarchie#define CHECK_DATA_MBUF(m)
11653403Sarchie#endif
11752722Sjulian
11853403Sarchie
11952419Sjulian/************************************************************************
12053913Sarchie	Parse type definitions for generic messages
12153913Sarchie************************************************************************/
12253913Sarchie
12353913Sarchie/* Handy structure parse type defining macro */
12453913Sarchie#define DEFINE_PARSE_STRUCT_TYPE(lo, up, args)				\
12553913Sarchiestatic const struct ng_parse_struct_info				\
12653913Sarchie	ng_ ## lo ## _type_info = NG_GENERIC_ ## up ## _INFO args;	\
12753913Sarchiestatic const struct ng_parse_type ng_generic_ ## lo ## _type = {	\
12853913Sarchie	&ng_parse_struct_type,						\
12953913Sarchie	&ng_ ## lo ## _type_info					\
13053913Sarchie}
13153913Sarchie
13253913SarchieDEFINE_PARSE_STRUCT_TYPE(mkpeer, MKPEER, ());
13353913SarchieDEFINE_PARSE_STRUCT_TYPE(connect, CONNECT, ());
13453913SarchieDEFINE_PARSE_STRUCT_TYPE(name, NAME, ());
13553913SarchieDEFINE_PARSE_STRUCT_TYPE(rmhook, RMHOOK, ());
13653913SarchieDEFINE_PARSE_STRUCT_TYPE(nodeinfo, NODEINFO, ());
13753913SarchieDEFINE_PARSE_STRUCT_TYPE(typeinfo, TYPEINFO, ());
13853913SarchieDEFINE_PARSE_STRUCT_TYPE(linkinfo, LINKINFO, (&ng_generic_nodeinfo_type));
13953913Sarchie
14053913Sarchie/* Get length of an array when the length is stored as a 32 bit
14153913Sarchie   value immediately preceeding the array -- as with struct namelist
14253913Sarchie   and struct typelist. */
14353913Sarchiestatic int
14453913Sarchieng_generic_list_getLength(const struct ng_parse_type *type,
14553913Sarchie	const u_char *start, const u_char *buf)
14653913Sarchie{
14753913Sarchie	return *((const u_int32_t *)(buf - 4));
14853913Sarchie}
14953913Sarchie
15053913Sarchie/* Get length of the array of struct linkinfo inside a struct hooklist */
15153913Sarchiestatic int
15253913Sarchieng_generic_linkinfo_getLength(const struct ng_parse_type *type,
15353913Sarchie	const u_char *start, const u_char *buf)
15453913Sarchie{
15553913Sarchie	const struct hooklist *hl = (const struct hooklist *)start;
15653913Sarchie
15753913Sarchie	return hl->nodeinfo.hooks;
15853913Sarchie}
15953913Sarchie
16053913Sarchie/* Array type for a variable length array of struct namelist */
16153913Sarchiestatic const struct ng_parse_array_info ng_nodeinfoarray_type_info = {
16253913Sarchie	&ng_generic_nodeinfo_type,
16353913Sarchie	&ng_generic_list_getLength
16453913Sarchie};
16553913Sarchiestatic const struct ng_parse_type ng_generic_nodeinfoarray_type = {
16653913Sarchie	&ng_parse_array_type,
16753913Sarchie	&ng_nodeinfoarray_type_info
16853913Sarchie};
16953913Sarchie
17053913Sarchie/* Array type for a variable length array of struct typelist */
17153913Sarchiestatic const struct ng_parse_array_info ng_typeinfoarray_type_info = {
17253913Sarchie	&ng_generic_typeinfo_type,
17353913Sarchie	&ng_generic_list_getLength
17453913Sarchie};
17553913Sarchiestatic const struct ng_parse_type ng_generic_typeinfoarray_type = {
17653913Sarchie	&ng_parse_array_type,
17753913Sarchie	&ng_typeinfoarray_type_info
17853913Sarchie};
17953913Sarchie
18053913Sarchie/* Array type for array of struct linkinfo in struct hooklist */
18153913Sarchiestatic const struct ng_parse_array_info ng_generic_linkinfo_array_type_info = {
18253913Sarchie	&ng_generic_linkinfo_type,
18353913Sarchie	&ng_generic_linkinfo_getLength
18453913Sarchie};
18553913Sarchiestatic const struct ng_parse_type ng_generic_linkinfo_array_type = {
18653913Sarchie	&ng_parse_array_type,
18753913Sarchie	&ng_generic_linkinfo_array_type_info
18853913Sarchie};
18953913Sarchie
19053913SarchieDEFINE_PARSE_STRUCT_TYPE(typelist, TYPELIST, (&ng_generic_nodeinfoarray_type));
19153913SarchieDEFINE_PARSE_STRUCT_TYPE(hooklist, HOOKLIST,
19253913Sarchie	(&ng_generic_nodeinfo_type, &ng_generic_linkinfo_array_type));
19353913SarchieDEFINE_PARSE_STRUCT_TYPE(listnodes, LISTNODES,
19453913Sarchie	(&ng_generic_nodeinfoarray_type));
19553913Sarchie
19653913Sarchie/* List of commands and how to convert arguments to/from ASCII */
19753913Sarchiestatic const struct ng_cmdlist ng_generic_cmds[] = {
19853913Sarchie	{
19953913Sarchie	  NGM_GENERIC_COOKIE,
20053913Sarchie	  NGM_SHUTDOWN,
20153913Sarchie	  "shutdown",
20253913Sarchie	  NULL,
20353913Sarchie	  NULL
20453913Sarchie	},
20553913Sarchie	{
20653913Sarchie	  NGM_GENERIC_COOKIE,
20753913Sarchie	  NGM_MKPEER,
20853913Sarchie	  "mkpeer",
20953913Sarchie	  &ng_generic_mkpeer_type,
21053913Sarchie	  NULL
21153913Sarchie	},
21253913Sarchie	{
21353913Sarchie	  NGM_GENERIC_COOKIE,
21453913Sarchie	  NGM_CONNECT,
21553913Sarchie	  "connect",
21653913Sarchie	  &ng_generic_connect_type,
21753913Sarchie	  NULL
21853913Sarchie	},
21953913Sarchie	{
22053913Sarchie	  NGM_GENERIC_COOKIE,
22153913Sarchie	  NGM_NAME,
22253913Sarchie	  "name",
22353913Sarchie	  &ng_generic_name_type,
22453913Sarchie	  NULL
22553913Sarchie	},
22653913Sarchie	{
22753913Sarchie	  NGM_GENERIC_COOKIE,
22853913Sarchie	  NGM_RMHOOK,
22953913Sarchie	  "rmhook",
23053913Sarchie	  &ng_generic_rmhook_type,
23153913Sarchie	  NULL
23253913Sarchie	},
23353913Sarchie	{
23453913Sarchie	  NGM_GENERIC_COOKIE,
23553913Sarchie	  NGM_NODEINFO,
23653913Sarchie	  "nodeinfo",
23753913Sarchie	  NULL,
23853913Sarchie	  &ng_generic_nodeinfo_type
23953913Sarchie	},
24053913Sarchie	{
24153913Sarchie	  NGM_GENERIC_COOKIE,
24253913Sarchie	  NGM_LISTHOOKS,
24353913Sarchie	  "listhooks",
24453913Sarchie	  NULL,
24553913Sarchie	  &ng_generic_hooklist_type
24653913Sarchie	},
24753913Sarchie	{
24853913Sarchie	  NGM_GENERIC_COOKIE,
24953913Sarchie	  NGM_LISTNAMES,
25053913Sarchie	  "listnames",
25153913Sarchie	  NULL,
25253913Sarchie	  &ng_generic_listnodes_type	/* same as NGM_LISTNODES */
25353913Sarchie	},
25453913Sarchie	{
25553913Sarchie	  NGM_GENERIC_COOKIE,
25653913Sarchie	  NGM_LISTNODES,
25753913Sarchie	  "listnodes",
25853913Sarchie	  NULL,
25953913Sarchie	  &ng_generic_listnodes_type
26053913Sarchie	},
26153913Sarchie	{
26253913Sarchie	  NGM_GENERIC_COOKIE,
26353913Sarchie	  NGM_LISTTYPES,
26453913Sarchie	  "listtypes",
26553913Sarchie	  NULL,
26653913Sarchie	  &ng_generic_typeinfo_type
26753913Sarchie	},
26853913Sarchie	{
26953913Sarchie	  NGM_GENERIC_COOKIE,
27053913Sarchie	  NGM_TEXT_STATUS,
27153913Sarchie	  "textstatus",
27253913Sarchie	  NULL,
27353913Sarchie	  &ng_parse_string_type
27453913Sarchie	},
27553913Sarchie	{
27653913Sarchie	  NGM_GENERIC_COOKIE,
27753913Sarchie	  NGM_ASCII2BINARY,
27853913Sarchie	  "ascii2binary",
27953913Sarchie	  &ng_parse_ng_mesg_type,
28053913Sarchie	  &ng_parse_ng_mesg_type
28153913Sarchie	},
28253913Sarchie	{
28353913Sarchie	  NGM_GENERIC_COOKIE,
28453913Sarchie	  NGM_BINARY2ASCII,
28553913Sarchie	  "binary2ascii",
28653913Sarchie	  &ng_parse_ng_mesg_type,
28753913Sarchie	  &ng_parse_ng_mesg_type
28853913Sarchie	},
28953913Sarchie	{ 0 }
29053913Sarchie};
29153913Sarchie
29253913Sarchie/************************************************************************
29352419Sjulian			Node routines
29452419Sjulian************************************************************************/
29552419Sjulian
29652419Sjulian/*
29752419Sjulian * Instantiate a node of the requested type
29852419Sjulian */
29952419Sjulianint
30052419Sjulianng_make_node(const char *typename, node_p *nodepp)
30152419Sjulian{
30252419Sjulian	struct ng_type *type;
30352419Sjulian
30452419Sjulian	/* Check that the type makes sense */
30552419Sjulian	if (typename == NULL) {
30652419Sjulian		TRAP_ERROR;
30752419Sjulian		return (EINVAL);
30852419Sjulian	}
30952419Sjulian
31052419Sjulian	/* Locate the node type */
31152419Sjulian	if ((type = ng_findtype(typename)) == NULL) {
31259875Speter		char filename[NG_TYPELEN + 4];
31352419Sjulian		linker_file_t lf;
31452419Sjulian		int error;
31552419Sjulian
31652419Sjulian		/* Not found, try to load it as a loadable module */
31759875Speter		snprintf(filename, sizeof(filename), "ng_%s", typename);
31859875Speter		error = linker_load_file(filename, &lf);
31952419Sjulian		if (error != 0)
32052419Sjulian			return (error);
32152419Sjulian		lf->userrefs++;		/* pretend loaded by the syscall */
32252419Sjulian
32352419Sjulian		/* Try again, as now the type should have linked itself in */
32452419Sjulian		if ((type = ng_findtype(typename)) == NULL)
32552419Sjulian			return (ENXIO);
32652419Sjulian	}
32752419Sjulian
32852419Sjulian	/* Call the constructor */
32952419Sjulian	if (type->constructor != NULL)
33052419Sjulian		return ((*type->constructor)(nodepp));
33152419Sjulian	else
33252419Sjulian		return (ng_make_node_common(type, nodepp));
33352419Sjulian}
33452419Sjulian
33552419Sjulian/*
33652419Sjulian * Generic node creation. Called by node constructors.
33752419Sjulian * The returned node has a reference count of 1.
33852419Sjulian */
33952419Sjulianint
34052419Sjulianng_make_node_common(struct ng_type *type, node_p *nodepp)
34152419Sjulian{
34252419Sjulian	node_p node;
34352419Sjulian
34452419Sjulian	/* Require the node type to have been already installed */
34552419Sjulian	if (ng_findtype(type->name) == NULL) {
34652419Sjulian		TRAP_ERROR;
34752419Sjulian		return (EINVAL);
34852419Sjulian	}
34952419Sjulian
35052419Sjulian	/* Make a node and try attach it to the type */
35152419Sjulian	MALLOC(node, node_p, sizeof(*node), M_NETGRAPH, M_WAITOK);
35252419Sjulian	if (node == NULL) {
35352419Sjulian		TRAP_ERROR;
35452419Sjulian		return (ENOMEM);
35552419Sjulian	}
35652419Sjulian	bzero(node, sizeof(*node));
35752419Sjulian	node->type = type;
35852419Sjulian	node->refs++;				/* note reference */
35952419Sjulian	type->refs++;
36052419Sjulian
36152419Sjulian	/* Link us into the node linked list */
36252419Sjulian	LIST_INSERT_HEAD(&nodelist, node, nodes);
36352419Sjulian
36452419Sjulian	/* Initialize hook list for new node */
36552419Sjulian	LIST_INIT(&node->hooks);
36652419Sjulian
36752722Sjulian	/* get an ID and put us in the hash chain */
36852722Sjulian	node->ID = nextID++; /* 137 per second for 1 year before wrap */
36952722Sjulian	LIST_INSERT_HEAD(&ID_hash[node->ID % ID_HASH_SIZE], node, idnodes);
37052722Sjulian
37152419Sjulian	/* Done */
37252419Sjulian	*nodepp = node;
37352419Sjulian	return (0);
37452419Sjulian}
37552419Sjulian
37652419Sjulian/*
37752419Sjulian * Forceably start the shutdown process on a node. Either call
37852419Sjulian * it's shutdown method, or do the default shutdown if there is
37952419Sjulian * no type-specific method.
38052419Sjulian *
38152419Sjulian * Persistent nodes must have a type-specific method which
38252419Sjulian * resets the NG_INVALID flag.
38352419Sjulian */
38452419Sjulianvoid
38552419Sjulianng_rmnode(node_p node)
38652419Sjulian{
38752419Sjulian	/* Check if it's already shutting down */
38852419Sjulian	if ((node->flags & NG_INVALID) != 0)
38952419Sjulian		return;
39052419Sjulian
39152419Sjulian	/* Add an extra reference so it doesn't go away during this */
39252419Sjulian	node->refs++;
39352419Sjulian
39452419Sjulian	/* Mark it invalid so any newcomers know not to try use it */
39552419Sjulian	node->flags |= NG_INVALID;
39652419Sjulian
39752419Sjulian	/* Ask the type if it has anything to do in this case */
39852419Sjulian	if (node->type && node->type->shutdown)
39952419Sjulian		(*node->type->shutdown)(node);
40052419Sjulian	else {				/* do the default thing */
40152419Sjulian		ng_unname(node);
40252419Sjulian		ng_cutlinks(node);
40352419Sjulian		ng_unref(node);
40452419Sjulian	}
40552419Sjulian
40652419Sjulian	/* Remove extra reference, possibly the last */
40752419Sjulian	ng_unref(node);
40852419Sjulian}
40952419Sjulian
41052419Sjulian/*
41152419Sjulian * Called by the destructor to remove any STANDARD external references
41252419Sjulian */
41352419Sjulianvoid
41452419Sjulianng_cutlinks(node_p node)
41552419Sjulian{
41652419Sjulian	hook_p  hook;
41752419Sjulian
41852419Sjulian	/* Make sure that this is set to stop infinite loops */
41952419Sjulian	node->flags |= NG_INVALID;
42052419Sjulian
42152419Sjulian	/* If we have sleepers, wake them up; they'll see NG_INVALID */
42252419Sjulian	if (node->sleepers)
42352419Sjulian		wakeup(node);
42452419Sjulian
42552419Sjulian	/* Notify all remaining connected nodes to disconnect */
42652419Sjulian	while ((hook = LIST_FIRST(&node->hooks)) != NULL)
42752419Sjulian		ng_destroy_hook(hook);
42852419Sjulian}
42952419Sjulian
43052419Sjulian/*
43152419Sjulian * Remove a reference to the node, possibly the last
43252419Sjulian */
43352419Sjulianvoid
43452419Sjulianng_unref(node_p node)
43552419Sjulian{
43652419Sjulian	if (--node->refs <= 0) {
43752419Sjulian		node->type->refs--;
43852419Sjulian		LIST_REMOVE(node, nodes);
43952722Sjulian		LIST_REMOVE(node, idnodes);
44052419Sjulian		FREE(node, M_NETGRAPH);
44152419Sjulian	}
44252419Sjulian}
44352419Sjulian
44452419Sjulian/*
44552419Sjulian * Wait for a node to come ready. Returns a node with a reference count;
44652419Sjulian * don't forget to drop it when we are done with it using ng_release_node().
44752419Sjulian */
44852419Sjulianint
44952419Sjulianng_wait_node(node_p node, char *msg)
45052419Sjulian{
45152419Sjulian	int s, error = 0;
45252419Sjulian
45352419Sjulian	if (msg == NULL)
45452419Sjulian		msg = "netgraph";
45552419Sjulian	s = splnet();
45652419Sjulian	node->sleepers++;
45752419Sjulian	node->refs++;		/* the sleeping process counts as a reference */
45852419Sjulian	while ((node->flags & (NG_BUSY | NG_INVALID)) == NG_BUSY)
45952419Sjulian		error = tsleep(node, (PZERO + 1) | PCATCH, msg, 0);
46052419Sjulian	node->sleepers--;
46152419Sjulian	if (node->flags & NG_INVALID) {
46252419Sjulian		TRAP_ERROR;
46352419Sjulian		error = ENXIO;
46452419Sjulian	} else {
46553403Sarchie		KASSERT(node->refs > 1,
46653403Sarchie		    ("%s: refs=%d", __FUNCTION__, node->refs));
46752419Sjulian		node->flags |= NG_BUSY;
46852419Sjulian	}
46952419Sjulian	splx(s);
47052419Sjulian
47152419Sjulian	/* Release the reference we had on it */
47252419Sjulian	if (error != 0)
47352419Sjulian		ng_unref(node);
47452419Sjulian	return error;
47552419Sjulian}
47652419Sjulian
47752419Sjulian/*
47852419Sjulian * Release a node acquired via ng_wait_node()
47952419Sjulian */
48052419Sjulianvoid
48152419Sjulianng_release_node(node_p node)
48252419Sjulian{
48352419Sjulian	/* Declare that we don't want it */
48452419Sjulian	node->flags &= ~NG_BUSY;
48552419Sjulian
48652419Sjulian	/* If we have sleepers, then wake them up */
48752419Sjulian	if (node->sleepers)
48852419Sjulian		wakeup(node);
48952419Sjulian
49052419Sjulian	/* We also have a reference.. drop it too */
49152419Sjulian	ng_unref(node);
49252419Sjulian}
49352419Sjulian
49452419Sjulian/************************************************************************
49552722Sjulian			Node ID handling
49652722Sjulian************************************************************************/
49752722Sjulianstatic node_p
49852722Sjulianng_ID2node(ng_ID_t ID)
49952722Sjulian{
50052722Sjulian	node_p np;
50152722Sjulian	LIST_FOREACH(np, &ID_hash[ID % ID_HASH_SIZE], idnodes) {
50252722Sjulian		if (np->ID == ID)
50352722Sjulian			break;
50452722Sjulian	}
50552722Sjulian	return(np);
50652722Sjulian}
50752722Sjulian
50852722Sjulianng_ID_t
50952722Sjulianng_node2ID(node_p node)
51052722Sjulian{
51152722Sjulian	return (node->ID);
51252722Sjulian}
51352722Sjulian
51452722Sjulian/************************************************************************
51552419Sjulian			Node name handling
51652419Sjulian************************************************************************/
51752419Sjulian
51852419Sjulian/*
51952419Sjulian * Assign a node a name. Once assigned, the name cannot be changed.
52052419Sjulian */
52152419Sjulianint
52252419Sjulianng_name_node(node_p node, const char *name)
52352419Sjulian{
52452419Sjulian	int i;
52552419Sjulian
52652419Sjulian	/* Check the name is valid */
52752419Sjulian	for (i = 0; i < NG_NODELEN + 1; i++) {
52852419Sjulian		if (name[i] == '\0' || name[i] == '.' || name[i] == ':')
52952419Sjulian			break;
53052419Sjulian	}
53152419Sjulian	if (i == 0 || name[i] != '\0') {
53252419Sjulian		TRAP_ERROR;
53352419Sjulian		return (EINVAL);
53452419Sjulian	}
53552722Sjulian	if (ng_decodeidname(name) != 0) { /* valid IDs not allowed here */
53652419Sjulian		TRAP_ERROR;
53752419Sjulian		return (EINVAL);
53852419Sjulian	}
53952419Sjulian
54052419Sjulian	/* Check the node isn't already named */
54152419Sjulian	if (node->name != NULL) {
54252419Sjulian		TRAP_ERROR;
54352419Sjulian		return (EISCONN);
54452419Sjulian	}
54552419Sjulian
54652419Sjulian	/* Check the name isn't already being used */
54752419Sjulian	if (ng_findname(node, name) != NULL) {
54852419Sjulian		TRAP_ERROR;
54952419Sjulian		return (EADDRINUSE);
55052419Sjulian	}
55152419Sjulian
55252419Sjulian	/* Allocate space and copy it */
55352419Sjulian	MALLOC(node->name, char *, strlen(name) + 1, M_NETGRAPH, M_WAITOK);
55452419Sjulian	if (node->name == NULL) {
55552419Sjulian		TRAP_ERROR;
55652419Sjulian		return (ENOMEM);
55752419Sjulian	}
55852419Sjulian	strcpy(node->name, name);
55952419Sjulian
56052419Sjulian	/* The name counts as a reference */
56152419Sjulian	node->refs++;
56252419Sjulian	return (0);
56352419Sjulian}
56452419Sjulian
56552419Sjulian/*
56652419Sjulian * Find a node by absolute name. The name should NOT end with ':'
56752419Sjulian * The name "." means "this node" and "[xxx]" means "the node
56852419Sjulian * with ID (ie, at address) xxx".
56952419Sjulian *
57052419Sjulian * Returns the node if found, else NULL.
57152419Sjulian */
57252419Sjuliannode_p
57352419Sjulianng_findname(node_p this, const char *name)
57452419Sjulian{
57552722Sjulian	node_p node;
57652722Sjulian	ng_ID_t temp;
57752419Sjulian
57852419Sjulian	/* "." means "this node" */
57952419Sjulian	if (strcmp(name, ".") == 0)
58052419Sjulian		return(this);
58152419Sjulian
58252419Sjulian	/* Check for name-by-ID */
58352722Sjulian	if ((temp = ng_decodeidname(name)) != 0) {
58452722Sjulian		return (ng_ID2node(temp));
58552419Sjulian	}
58652419Sjulian
58752419Sjulian	/* Find node by name */
58852419Sjulian	LIST_FOREACH(node, &nodelist, nodes) {
58952419Sjulian		if (node->name != NULL && strcmp(node->name, name) == 0)
59052419Sjulian			break;
59152419Sjulian	}
59252419Sjulian	return (node);
59352419Sjulian}
59452419Sjulian
59552419Sjulian/*
59652722Sjulian * Decode a ID name, eg. "[f03034de]". Returns 0 if the
59752722Sjulian * string is not valid, otherwise returns the value.
59852419Sjulian */
59952722Sjulianstatic ng_ID_t
60052419Sjulianng_decodeidname(const char *name)
60152419Sjulian{
60252816Sarchie	const int len = strlen(name);
60353648Sarchie	char *eptr;
60452816Sarchie	u_long val;
60552419Sjulian
60652816Sarchie	/* Check for proper length, brackets, no leading junk */
60752816Sarchie	if (len < 3 || name[0] != '[' || name[len - 1] != ']'
60852951Sjulian	    || !isxdigit(name[1]))
60952816Sarchie		return (0);
61052419Sjulian
61152816Sarchie	/* Decode number */
61252816Sarchie	val = strtoul(name + 1, &eptr, 16);
61352951Sjulian	if (eptr - name != len - 1 || val == ULONG_MAX || val == 0)
61453042Sjulian		return ((ng_ID_t)0);
61552816Sarchie	return (ng_ID_t)val;
61652419Sjulian}
61752419Sjulian
61852419Sjulian/*
61952419Sjulian * Remove a name from a node. This should only be called
62052419Sjulian * when shutting down and removing the node.
62152419Sjulian */
62252419Sjulianvoid
62352419Sjulianng_unname(node_p node)
62452419Sjulian{
62552419Sjulian	if (node->name) {
62652419Sjulian		FREE(node->name, M_NETGRAPH);
62752419Sjulian		node->name = NULL;
62852419Sjulian		ng_unref(node);
62952419Sjulian	}
63052419Sjulian}
63152419Sjulian
63252419Sjulian/************************************************************************
63352419Sjulian			Hook routines
63452419Sjulian
63552419Sjulian Names are not optional. Hooks are always connected, except for a
63652419Sjulian brief moment within these routines.
63752419Sjulian
63852419Sjulian************************************************************************/
63952419Sjulian
64052419Sjulian/*
64152419Sjulian * Remove a hook reference
64252419Sjulian */
64352419Sjulianstatic void
64452419Sjulianng_unref_hook(hook_p hook)
64552419Sjulian{
64652419Sjulian	if (--hook->refs == 0)
64752419Sjulian		FREE(hook, M_NETGRAPH);
64852419Sjulian}
64952419Sjulian
65052419Sjulian/*
65152419Sjulian * Add an unconnected hook to a node. Only used internally.
65252419Sjulian */
65352419Sjulianstatic int
65452419Sjulianng_add_hook(node_p node, const char *name, hook_p *hookp)
65552419Sjulian{
65652419Sjulian	hook_p hook;
65752419Sjulian	int error = 0;
65852419Sjulian
65952419Sjulian	/* Check that the given name is good */
66052419Sjulian	if (name == NULL) {
66152419Sjulian		TRAP_ERROR;
66252419Sjulian		return (EINVAL);
66352419Sjulian	}
66454096Sarchie	if (ng_findhook(node, name) != NULL) {
66554096Sarchie		TRAP_ERROR;
66654096Sarchie		return (EEXIST);
66752419Sjulian	}
66852419Sjulian
66952419Sjulian	/* Allocate the hook and link it up */
67052419Sjulian	MALLOC(hook, hook_p, sizeof(*hook), M_NETGRAPH, M_WAITOK);
67152419Sjulian	if (hook == NULL) {
67252419Sjulian		TRAP_ERROR;
67352419Sjulian		return (ENOMEM);
67452419Sjulian	}
67552419Sjulian	bzero(hook, sizeof(*hook));
67652419Sjulian	hook->refs = 1;
67752419Sjulian	hook->flags = HK_INVALID;
67852419Sjulian	hook->node = node;
67952419Sjulian	node->refs++;		/* each hook counts as a reference */
68052419Sjulian
68152419Sjulian	/* Check if the node type code has something to say about it */
68252419Sjulian	if (node->type->newhook != NULL)
68352419Sjulian		if ((error = (*node->type->newhook)(node, hook, name)) != 0)
68452419Sjulian			goto fail;
68552419Sjulian
68652419Sjulian	/*
68752419Sjulian	 * The 'type' agrees so far, so go ahead and link it in.
68852419Sjulian	 * We'll ask again later when we actually connect the hooks.
68952419Sjulian	 */
69052419Sjulian	LIST_INSERT_HEAD(&node->hooks, hook, hooks);
69152419Sjulian	node->numhooks++;
69252419Sjulian
69352419Sjulian	/* Set hook name */
69452419Sjulian	MALLOC(hook->name, char *, strlen(name) + 1, M_NETGRAPH, M_WAITOK);
69552419Sjulian	if (hook->name == NULL) {
69652419Sjulian		error = ENOMEM;
69752419Sjulian		LIST_REMOVE(hook, hooks);
69852419Sjulian		node->numhooks--;
69952419Sjulianfail:
70052419Sjulian		hook->node = NULL;
70152419Sjulian		ng_unref(node);
70252419Sjulian		ng_unref_hook(hook);	/* this frees the hook */
70352419Sjulian		return (error);
70452419Sjulian	}
70552419Sjulian	strcpy(hook->name, name);
70652419Sjulian	if (hookp)
70752419Sjulian		*hookp = hook;
70852419Sjulian	return (error);
70952419Sjulian}
71052419Sjulian
71152419Sjulian/*
71252419Sjulian * Connect a pair of hooks. Only used internally.
71352419Sjulian */
71452419Sjulianstatic int
71552419Sjulianng_connect(hook_p hook1, hook_p hook2)
71652419Sjulian{
71752419Sjulian	int     error;
71852419Sjulian
71952419Sjulian	hook1->peer = hook2;
72052419Sjulian	hook2->peer = hook1;
72152419Sjulian
72252419Sjulian	/* Give each node the opportunity to veto the impending connection */
72352419Sjulian	if (hook1->node->type->connect) {
72452419Sjulian		if ((error = (*hook1->node->type->connect) (hook1))) {
72552419Sjulian			ng_destroy_hook(hook1);	/* also zaps hook2 */
72652419Sjulian			return (error);
72752419Sjulian		}
72852419Sjulian	}
72952419Sjulian	if (hook2->node->type->connect) {
73052419Sjulian		if ((error = (*hook2->node->type->connect) (hook2))) {
73152419Sjulian			ng_destroy_hook(hook2);	/* also zaps hook1 */
73252419Sjulian			return (error);
73352419Sjulian		}
73452419Sjulian	}
73552419Sjulian	hook1->flags &= ~HK_INVALID;
73652419Sjulian	hook2->flags &= ~HK_INVALID;
73752419Sjulian	return (0);
73852419Sjulian}
73952419Sjulian
74052419Sjulian/*
74154096Sarchie * Find a hook
74254096Sarchie *
74354096Sarchie * Node types may supply their own optimized routines for finding
74454096Sarchie * hooks.  If none is supplied, we just do a linear search.
74554096Sarchie */
74654096Sarchiehook_p
74754096Sarchieng_findhook(node_p node, const char *name)
74854096Sarchie{
74954096Sarchie	hook_p hook;
75054096Sarchie
75154096Sarchie	if (node->type->findhook != NULL)
75254096Sarchie		return (*node->type->findhook)(node, name);
75354096Sarchie	LIST_FOREACH(hook, &node->hooks, hooks) {
75454096Sarchie		if (hook->name != NULL && strcmp(hook->name, name) == 0)
75554096Sarchie			return (hook);
75654096Sarchie	}
75754096Sarchie	return (NULL);
75854096Sarchie}
75954096Sarchie
76054096Sarchie/*
76152419Sjulian * Destroy a hook
76252419Sjulian *
76352419Sjulian * As hooks are always attached, this really destroys two hooks.
76452419Sjulian * The one given, and the one attached to it. Disconnect the hooks
76552419Sjulian * from each other first.
76652419Sjulian */
76752419Sjulianvoid
76852419Sjulianng_destroy_hook(hook_p hook)
76952419Sjulian{
77052419Sjulian	hook_p peer = hook->peer;
77152419Sjulian
77252419Sjulian	hook->flags |= HK_INVALID;		/* as soon as possible */
77352419Sjulian	if (peer) {
77452419Sjulian		peer->flags |= HK_INVALID;	/* as soon as possible */
77552419Sjulian		hook->peer = NULL;
77652419Sjulian		peer->peer = NULL;
77752419Sjulian		ng_disconnect_hook(peer);
77852419Sjulian	}
77952419Sjulian	ng_disconnect_hook(hook);
78052419Sjulian}
78152419Sjulian
78252419Sjulian/*
78352419Sjulian * Notify the node of the hook's demise. This may result in more actions
78452419Sjulian * (e.g. shutdown) but we don't do that ourselves and don't know what
78552419Sjulian * happens there. If there is no appropriate handler, then just remove it
78652419Sjulian * (and decrement the reference count of it's node which in turn might
78752419Sjulian * make something happen).
78852419Sjulian */
78952419Sjulianstatic void
79052419Sjulianng_disconnect_hook(hook_p hook)
79152419Sjulian{
79252419Sjulian	node_p node = hook->node;
79352419Sjulian
79452419Sjulian	/*
79552419Sjulian	 * Remove the hook from the node's list to avoid possible recursion
79652419Sjulian	 * in case the disconnection results in node shutdown.
79752419Sjulian	 */
79852419Sjulian	LIST_REMOVE(hook, hooks);
79952419Sjulian	node->numhooks--;
80052419Sjulian	if (node->type->disconnect) {
80152419Sjulian		/*
80252419Sjulian		 * The type handler may elect to destroy the peer so don't
80352419Sjulian		 * trust its existance after this point.
80452419Sjulian		 */
80552419Sjulian		(*node->type->disconnect) (hook);
80652419Sjulian	}
80752419Sjulian	ng_unref(node);		/* might be the last reference */
80852419Sjulian	if (hook->name)
80952419Sjulian		FREE(hook->name, M_NETGRAPH);
81052419Sjulian	hook->node = NULL;	/* may still be referenced elsewhere */
81152419Sjulian	ng_unref_hook(hook);
81252419Sjulian}
81352419Sjulian
81452419Sjulian/*
81552419Sjulian * Take two hooks on a node and merge the connection so that the given node
81652419Sjulian * is effectively bypassed.
81752419Sjulian */
81852419Sjulianint
81952419Sjulianng_bypass(hook_p hook1, hook_p hook2)
82052419Sjulian{
82152419Sjulian	if (hook1->node != hook2->node)
82252419Sjulian		return (EINVAL);
82352419Sjulian	hook1->peer->peer = hook2->peer;
82452419Sjulian	hook2->peer->peer = hook1->peer;
82552419Sjulian
82652419Sjulian	/* XXX If we ever cache methods on hooks update them as well */
82752419Sjulian	hook1->peer = NULL;
82852419Sjulian	hook2->peer = NULL;
82952419Sjulian	ng_destroy_hook(hook1);
83052419Sjulian	ng_destroy_hook(hook2);
83152419Sjulian	return (0);
83252419Sjulian}
83352419Sjulian
83452419Sjulian/*
83552419Sjulian * Install a new netgraph type
83652419Sjulian */
83752419Sjulianint
83852419Sjulianng_newtype(struct ng_type *tp)
83952419Sjulian{
84052419Sjulian	const size_t namelen = strlen(tp->name);
84152419Sjulian
84252419Sjulian	/* Check version and type name fields */
84352419Sjulian	if (tp->version != NG_VERSION || namelen == 0 || namelen > NG_TYPELEN) {
84452419Sjulian		TRAP_ERROR;
84552419Sjulian		return (EINVAL);
84652419Sjulian	}
84752419Sjulian
84852419Sjulian	/* Check for name collision */
84952419Sjulian	if (ng_findtype(tp->name) != NULL) {
85052419Sjulian		TRAP_ERROR;
85152419Sjulian		return (EEXIST);
85252419Sjulian	}
85352419Sjulian
85452419Sjulian	/* Link in new type */
85552419Sjulian	LIST_INSERT_HEAD(&typelist, tp, types);
85652419Sjulian	tp->refs = 0;
85752419Sjulian	return (0);
85852419Sjulian}
85952419Sjulian
86052419Sjulian/*
86152419Sjulian * Look for a type of the name given
86252419Sjulian */
86352419Sjulianstruct ng_type *
86452419Sjulianng_findtype(const char *typename)
86552419Sjulian{
86652419Sjulian	struct ng_type *type;
86752419Sjulian
86852419Sjulian	LIST_FOREACH(type, &typelist, types) {
86952419Sjulian		if (strcmp(type->name, typename) == 0)
87052419Sjulian			break;
87152419Sjulian	}
87252419Sjulian	return (type);
87352419Sjulian}
87452419Sjulian
87552419Sjulian
87652419Sjulian/************************************************************************
87752419Sjulian			Composite routines
87852419Sjulian************************************************************************/
87952419Sjulian
88052419Sjulian/*
88152419Sjulian * Make a peer and connect. The order is arranged to minimise
88252419Sjulian * the work needed to back out in case of error.
88352419Sjulian */
88452419Sjulianint
88552419Sjulianng_mkpeer(node_p node, const char *name, const char *name2, char *type)
88652419Sjulian{
88752419Sjulian	node_p  node2;
88852419Sjulian	hook_p  hook;
88952419Sjulian	hook_p  hook2;
89052419Sjulian	int     error;
89152419Sjulian
89252419Sjulian	if ((error = ng_add_hook(node, name, &hook)))
89352419Sjulian		return (error);
89452419Sjulian	if ((error = ng_make_node(type, &node2))) {
89552419Sjulian		ng_destroy_hook(hook);
89652419Sjulian		return (error);
89752419Sjulian	}
89852419Sjulian	if ((error = ng_add_hook(node2, name2, &hook2))) {
89952419Sjulian		ng_rmnode(node2);
90052419Sjulian		ng_destroy_hook(hook);
90152419Sjulian		return (error);
90252419Sjulian	}
90352419Sjulian
90452419Sjulian	/*
90552419Sjulian	 * Actually link the two hooks together.. on failure they are
90652419Sjulian	 * destroyed so we don't have to do that here.
90752419Sjulian	 */
90852419Sjulian	if ((error = ng_connect(hook, hook2)))
90952419Sjulian		ng_rmnode(node2);
91052419Sjulian	return (error);
91152419Sjulian}
91252419Sjulian
91352419Sjulian/*
91452419Sjulian * Connect two nodes using the specified hooks
91552419Sjulian */
91652419Sjulianint
91752419Sjulianng_con_nodes(node_p node, const char *name, node_p node2, const char *name2)
91852419Sjulian{
91952419Sjulian	int     error;
92052419Sjulian	hook_p  hook;
92152419Sjulian	hook_p  hook2;
92252419Sjulian
92352419Sjulian	if ((error = ng_add_hook(node, name, &hook)))
92452419Sjulian		return (error);
92552419Sjulian	if ((error = ng_add_hook(node2, name2, &hook2))) {
92652419Sjulian		ng_destroy_hook(hook);
92752419Sjulian		return (error);
92852419Sjulian	}
92952419Sjulian	return (ng_connect(hook, hook2));
93052419Sjulian}
93152419Sjulian
93252419Sjulian/*
93352419Sjulian * Parse and verify a string of the form:  <NODE:><PATH>
93452419Sjulian *
93552419Sjulian * Such a string can refer to a specific node or a specific hook
93652419Sjulian * on a specific node, depending on how you look at it. In the
93752419Sjulian * latter case, the PATH component must not end in a dot.
93852419Sjulian *
93952419Sjulian * Both <NODE:> and <PATH> are optional. The <PATH> is a string
94052419Sjulian * of hook names separated by dots. This breaks out the original
94152419Sjulian * string, setting *nodep to "NODE" (or NULL if none) and *pathp
94252419Sjulian * to "PATH" (or NULL if degenerate). Also, *hookp will point to
94352419Sjulian * the final hook component of <PATH>, if any, otherwise NULL.
94452419Sjulian *
94552419Sjulian * This returns -1 if the path is malformed. The char ** are optional.
94652419Sjulian */
94752419Sjulian
94852419Sjulianint
94952419Sjulianng_path_parse(char *addr, char **nodep, char **pathp, char **hookp)
95052419Sjulian{
95152419Sjulian	char   *node, *path, *hook;
95252419Sjulian	int     k;
95352419Sjulian
95452419Sjulian	/*
95552419Sjulian	 * Extract absolute NODE, if any
95652419Sjulian	 */
95752419Sjulian	for (path = addr; *path && *path != ':'; path++);
95852419Sjulian	if (*path) {
95952419Sjulian		node = addr;	/* Here's the NODE */
96052419Sjulian		*path++ = '\0';	/* Here's the PATH */
96152419Sjulian
96252419Sjulian		/* Node name must not be empty */
96352419Sjulian		if (!*node)
96452419Sjulian			return -1;
96552419Sjulian
96652419Sjulian		/* A name of "." is OK; otherwise '.' not allowed */
96752419Sjulian		if (strcmp(node, ".") != 0) {
96852419Sjulian			for (k = 0; node[k]; k++)
96952419Sjulian				if (node[k] == '.')
97052419Sjulian					return -1;
97152419Sjulian		}
97252419Sjulian	} else {
97352419Sjulian		node = NULL;	/* No absolute NODE */
97452419Sjulian		path = addr;	/* Here's the PATH */
97552419Sjulian	}
97652419Sjulian
97752419Sjulian	/* Snoop for illegal characters in PATH */
97852419Sjulian	for (k = 0; path[k]; k++)
97952419Sjulian		if (path[k] == ':')
98052419Sjulian			return -1;
98152419Sjulian
98252419Sjulian	/* Check for no repeated dots in PATH */
98352419Sjulian	for (k = 0; path[k]; k++)
98452419Sjulian		if (path[k] == '.' && path[k + 1] == '.')
98552419Sjulian			return -1;
98652419Sjulian
98752419Sjulian	/* Remove extra (degenerate) dots from beginning or end of PATH */
98852419Sjulian	if (path[0] == '.')
98952419Sjulian		path++;
99052419Sjulian	if (*path && path[strlen(path) - 1] == '.')
99152419Sjulian		path[strlen(path) - 1] = 0;
99252419Sjulian
99352419Sjulian	/* If PATH has a dot, then we're not talking about a hook */
99452419Sjulian	if (*path) {
99552419Sjulian		for (hook = path, k = 0; path[k]; k++)
99652419Sjulian			if (path[k] == '.') {
99752419Sjulian				hook = NULL;
99852419Sjulian				break;
99952419Sjulian			}
100052419Sjulian	} else
100152419Sjulian		path = hook = NULL;
100252419Sjulian
100352419Sjulian	/* Done */
100452419Sjulian	if (nodep)
100552419Sjulian		*nodep = node;
100652419Sjulian	if (pathp)
100752419Sjulian		*pathp = path;
100852419Sjulian	if (hookp)
100952419Sjulian		*hookp = hook;
101052419Sjulian	return (0);
101152419Sjulian}
101252419Sjulian
101352419Sjulian/*
101452419Sjulian * Given a path, which may be absolute or relative, and a starting node,
101552419Sjulian * return the destination node. Compute the "return address" if desired.
101652419Sjulian */
101752419Sjulianint
101859728Sjulianng_path2node(node_p here, const char *address, node_p *destp, char **rtnp,
101959728Sjulian	hook_p *lasthook)
102052419Sjulian{
102152419Sjulian	const	node_p start = here;
102252419Sjulian	char    fullpath[NG_PATHLEN + 1];
102352419Sjulian	char   *nodename, *path, pbuf[2];
102452419Sjulian	node_p  node;
102552419Sjulian	char   *cp;
102659728Sjulian	hook_p hook = NULL;
102752419Sjulian
102852419Sjulian	/* Initialize */
102952419Sjulian	if (rtnp)
103052419Sjulian		*rtnp = NULL;
103152419Sjulian	if (destp == NULL)
103252419Sjulian		return EINVAL;
103352419Sjulian	*destp = NULL;
103452419Sjulian
103552419Sjulian	/* Make a writable copy of address for ng_path_parse() */
103652419Sjulian	strncpy(fullpath, address, sizeof(fullpath) - 1);
103752419Sjulian	fullpath[sizeof(fullpath) - 1] = '\0';
103852419Sjulian
103952419Sjulian	/* Parse out node and sequence of hooks */
104052419Sjulian	if (ng_path_parse(fullpath, &nodename, &path, NULL) < 0) {
104152419Sjulian		TRAP_ERROR;
104252419Sjulian		return EINVAL;
104352419Sjulian	}
104452419Sjulian	if (path == NULL) {
104552419Sjulian		pbuf[0] = '.';	/* Needs to be writable */
104652419Sjulian		pbuf[1] = '\0';
104752419Sjulian		path = pbuf;
104852419Sjulian	}
104952419Sjulian
105052419Sjulian	/* For an absolute address, jump to the starting node */
105152419Sjulian	if (nodename) {
105252419Sjulian		node = ng_findname(here, nodename);
105352419Sjulian		if (node == NULL) {
105452419Sjulian			TRAP_ERROR;
105552419Sjulian			return (ENOENT);
105652419Sjulian		}
105752419Sjulian	} else
105852419Sjulian		node = here;
105952419Sjulian
106052419Sjulian	/* Now follow the sequence of hooks */
106152419Sjulian	for (cp = path; node != NULL && *cp != '\0'; ) {
106252419Sjulian		char *segment;
106352419Sjulian
106452419Sjulian		/*
106552419Sjulian		 * Break out the next path segment. Replace the dot we just
106652419Sjulian		 * found with a NUL; "cp" points to the next segment (or the
106752419Sjulian		 * NUL at the end).
106852419Sjulian		 */
106952419Sjulian		for (segment = cp; *cp != '\0'; cp++) {
107052419Sjulian			if (*cp == '.') {
107152419Sjulian				*cp++ = '\0';
107252419Sjulian				break;
107352419Sjulian			}
107452419Sjulian		}
107552419Sjulian
107652419Sjulian		/* Empty segment */
107752419Sjulian		if (*segment == '\0')
107852419Sjulian			continue;
107952419Sjulian
108052419Sjulian		/* We have a segment, so look for a hook by that name */
108154096Sarchie		hook = ng_findhook(node, segment);
108252419Sjulian
108352419Sjulian		/* Can't get there from here... */
108452419Sjulian		if (hook == NULL
108552419Sjulian		    || hook->peer == NULL
108652419Sjulian		    || (hook->flags & HK_INVALID) != 0) {
108752419Sjulian			TRAP_ERROR;
108852419Sjulian			return (ENOENT);
108952419Sjulian		}
109052419Sjulian
109152419Sjulian		/* Hop on over to the next node */
109252419Sjulian		node = hook->peer->node;
109352419Sjulian	}
109452419Sjulian
109552419Sjulian	/* If node somehow missing, fail here (probably this is not needed) */
109652419Sjulian	if (node == NULL) {
109752419Sjulian		TRAP_ERROR;
109852419Sjulian		return (ENXIO);
109952419Sjulian	}
110052419Sjulian
110152419Sjulian	/* Now compute return address, i.e., the path to the sender */
110252419Sjulian	if (rtnp != NULL) {
110352419Sjulian		MALLOC(*rtnp, char *, NG_NODELEN + 2, M_NETGRAPH, M_WAITOK);
110452419Sjulian		if (*rtnp == NULL) {
110552419Sjulian			TRAP_ERROR;
110652419Sjulian			return (ENOMEM);
110752419Sjulian		}
110852419Sjulian		if (start->name != NULL)
110952419Sjulian			sprintf(*rtnp, "%s:", start->name);
111052419Sjulian		else
111152722Sjulian			sprintf(*rtnp, "[%x]:", ng_node2ID(start));
111252419Sjulian	}
111352419Sjulian
111452419Sjulian	/* Done */
111552419Sjulian	*destp = node;
111659728Sjulian	if (lasthook && hook)
111759728Sjulian		*lasthook = hook->peer;
111852419Sjulian	return (0);
111952419Sjulian}
112052419Sjulian
112152419Sjulian/*
112252419Sjulian * Call the appropriate message handler for the object.
112352419Sjulian * It is up to the message handler to free the message.
112452419Sjulian * If it's a generic message, handle it generically, otherwise
112552419Sjulian * call the type's message handler (if it exists)
112652419Sjulian */
112752419Sjulian
112859728Sjulian#define CALL_MSG_HANDLER(error, node, msg, retaddr, resp, hook)		\
112952419Sjuliando {									\
113052419Sjulian	if((msg)->header.typecookie == NGM_GENERIC_COOKIE) {		\
113152419Sjulian		(error) = ng_generic_msg((node), (msg),			\
113259728Sjulian			(retaddr), (resp), (hook));			\
113352419Sjulian	} else {							\
113452419Sjulian		if ((node)->type->rcvmsg != NULL) {			\
113552419Sjulian			(error) = (*(node)->type->rcvmsg)((node),	\
113659728Sjulian				(msg), (retaddr), (resp), (hook));	\
113752419Sjulian		} else {						\
113852419Sjulian			TRAP_ERROR;					\
113952419Sjulian			FREE((msg), M_NETGRAPH);			\
114052419Sjulian			(error) = EINVAL;				\
114152419Sjulian		}							\
114252419Sjulian	}								\
114352419Sjulian} while (0)
114452419Sjulian
114552419Sjulian
114652419Sjulian/*
114752419Sjulian * Send a control message to a node
114852419Sjulian */
114952419Sjulianint
115052419Sjulianng_send_msg(node_p here, struct ng_mesg *msg, const char *address,
115152419Sjulian	    struct ng_mesg **rptr)
115252419Sjulian{
115352419Sjulian	node_p  dest = NULL;
115452419Sjulian	char   *retaddr = NULL;
115552419Sjulian	int     error;
115659728Sjulian	hook_p	lasthook;
115752419Sjulian
115852419Sjulian	/* Find the target node */
115959728Sjulian	error = ng_path2node(here, address, &dest, &retaddr, &lasthook);
116052419Sjulian	if (error) {
116152419Sjulian		FREE(msg, M_NETGRAPH);
116252419Sjulian		return error;
116352419Sjulian	}
116452419Sjulian
116552419Sjulian	/* Make sure the resp field is null before we start */
116652419Sjulian	if (rptr != NULL)
116752419Sjulian		*rptr = NULL;
116852419Sjulian
116959728Sjulian	CALL_MSG_HANDLER(error, dest, msg, retaddr, rptr, lasthook);
117052419Sjulian
117152419Sjulian	/* Make sure that if there is a response, it has the RESP bit set */
117252419Sjulian	if ((error == 0) && rptr && *rptr)
117352419Sjulian		(*rptr)->header.flags |= NGF_RESP;
117452419Sjulian
117552419Sjulian	/*
117652419Sjulian	 * If we had a return address it is up to us to free it. They should
117752419Sjulian	 * have taken a copy if they needed to make a delayed response.
117852419Sjulian	 */
117952419Sjulian	if (retaddr)
118052419Sjulian		FREE(retaddr, M_NETGRAPH);
118152419Sjulian	return (error);
118252419Sjulian}
118352419Sjulian
118452419Sjulian/*
118552419Sjulian * Implement the 'generic' control messages
118652419Sjulian */
118752419Sjulianstatic int
118852419Sjulianng_generic_msg(node_p here, struct ng_mesg *msg, const char *retaddr,
118959728Sjulian	       struct ng_mesg **resp, hook_p lasthook)
119052419Sjulian{
119152419Sjulian	int error = 0;
119252419Sjulian
119352419Sjulian	if (msg->header.typecookie != NGM_GENERIC_COOKIE) {
119452419Sjulian		TRAP_ERROR;
119552419Sjulian		FREE(msg, M_NETGRAPH);
119652419Sjulian		return (EINVAL);
119752419Sjulian	}
119852419Sjulian	switch (msg->header.cmd) {
119952419Sjulian	case NGM_SHUTDOWN:
120052419Sjulian		ng_rmnode(here);
120152419Sjulian		break;
120252419Sjulian	case NGM_MKPEER:
120352419Sjulian	    {
120452419Sjulian		struct ngm_mkpeer *const mkp = (struct ngm_mkpeer *) msg->data;
120552419Sjulian
120652419Sjulian		if (msg->header.arglen != sizeof(*mkp)) {
120752419Sjulian			TRAP_ERROR;
120852419Sjulian			return (EINVAL);
120952419Sjulian		}
121052419Sjulian		mkp->type[sizeof(mkp->type) - 1] = '\0';
121152419Sjulian		mkp->ourhook[sizeof(mkp->ourhook) - 1] = '\0';
121252419Sjulian		mkp->peerhook[sizeof(mkp->peerhook) - 1] = '\0';
121352419Sjulian		error = ng_mkpeer(here, mkp->ourhook, mkp->peerhook, mkp->type);
121452419Sjulian		break;
121552419Sjulian	    }
121652419Sjulian	case NGM_CONNECT:
121752419Sjulian	    {
121852419Sjulian		struct ngm_connect *const con =
121952419Sjulian			(struct ngm_connect *) msg->data;
122052419Sjulian		node_p node2;
122152419Sjulian
122252419Sjulian		if (msg->header.arglen != sizeof(*con)) {
122352419Sjulian			TRAP_ERROR;
122452419Sjulian			return (EINVAL);
122552419Sjulian		}
122652419Sjulian		con->path[sizeof(con->path) - 1] = '\0';
122752419Sjulian		con->ourhook[sizeof(con->ourhook) - 1] = '\0';
122852419Sjulian		con->peerhook[sizeof(con->peerhook) - 1] = '\0';
122959728Sjulian		error = ng_path2node(here, con->path, &node2, NULL, NULL);
123052419Sjulian		if (error)
123152419Sjulian			break;
123252419Sjulian		error = ng_con_nodes(here, con->ourhook, node2, con->peerhook);
123352419Sjulian		break;
123452419Sjulian	    }
123552419Sjulian	case NGM_NAME:
123652419Sjulian	    {
123752419Sjulian		struct ngm_name *const nam = (struct ngm_name *) msg->data;
123852419Sjulian
123952419Sjulian		if (msg->header.arglen != sizeof(*nam)) {
124052419Sjulian			TRAP_ERROR;
124152419Sjulian			return (EINVAL);
124252419Sjulian		}
124352419Sjulian		nam->name[sizeof(nam->name) - 1] = '\0';
124452419Sjulian		error = ng_name_node(here, nam->name);
124552419Sjulian		break;
124652419Sjulian	    }
124752419Sjulian	case NGM_RMHOOK:
124852419Sjulian	    {
124952419Sjulian		struct ngm_rmhook *const rmh = (struct ngm_rmhook *) msg->data;
125052419Sjulian		hook_p hook;
125152419Sjulian
125252419Sjulian		if (msg->header.arglen != sizeof(*rmh)) {
125352419Sjulian			TRAP_ERROR;
125452419Sjulian			return (EINVAL);
125552419Sjulian		}
125652419Sjulian		rmh->ourhook[sizeof(rmh->ourhook) - 1] = '\0';
125754096Sarchie		if ((hook = ng_findhook(here, rmh->ourhook)) != NULL)
125852419Sjulian			ng_destroy_hook(hook);
125952419Sjulian		break;
126052419Sjulian	    }
126152419Sjulian	case NGM_NODEINFO:
126252419Sjulian	    {
126352419Sjulian		struct nodeinfo *ni;
126452419Sjulian		struct ng_mesg *rp;
126552419Sjulian
126652419Sjulian		/* Get response struct */
126752419Sjulian		if (resp == NULL) {
126852419Sjulian			error = EINVAL;
126952419Sjulian			break;
127052419Sjulian		}
127152419Sjulian		NG_MKRESPONSE(rp, msg, sizeof(*ni), M_NOWAIT);
127252419Sjulian		if (rp == NULL) {
127352419Sjulian			error = ENOMEM;
127452419Sjulian			break;
127552419Sjulian		}
127652419Sjulian
127752419Sjulian		/* Fill in node info */
127852419Sjulian		ni = (struct nodeinfo *) rp->data;
127952419Sjulian		if (here->name != NULL)
128052419Sjulian			strncpy(ni->name, here->name, NG_NODELEN);
128152419Sjulian		strncpy(ni->type, here->type->name, NG_TYPELEN);
128252722Sjulian		ni->id = ng_node2ID(here);
128352419Sjulian		ni->hooks = here->numhooks;
128452419Sjulian		*resp = rp;
128552419Sjulian		break;
128652419Sjulian	    }
128752419Sjulian	case NGM_LISTHOOKS:
128852419Sjulian	    {
128952419Sjulian		const int nhooks = here->numhooks;
129052419Sjulian		struct hooklist *hl;
129152419Sjulian		struct nodeinfo *ni;
129252419Sjulian		struct ng_mesg *rp;
129352419Sjulian		hook_p hook;
129452419Sjulian
129552419Sjulian		/* Get response struct */
129652419Sjulian		if (resp == NULL) {
129752419Sjulian			error = EINVAL;
129852419Sjulian			break;
129952419Sjulian		}
130052419Sjulian		NG_MKRESPONSE(rp, msg, sizeof(*hl)
130152419Sjulian		    + (nhooks * sizeof(struct linkinfo)), M_NOWAIT);
130252419Sjulian		if (rp == NULL) {
130352419Sjulian			error = ENOMEM;
130452419Sjulian			break;
130552419Sjulian		}
130652419Sjulian		hl = (struct hooklist *) rp->data;
130752419Sjulian		ni = &hl->nodeinfo;
130852419Sjulian
130952419Sjulian		/* Fill in node info */
131052419Sjulian		if (here->name)
131152419Sjulian			strncpy(ni->name, here->name, NG_NODELEN);
131252419Sjulian		strncpy(ni->type, here->type->name, NG_TYPELEN);
131352722Sjulian		ni->id = ng_node2ID(here);
131452419Sjulian
131552419Sjulian		/* Cycle through the linked list of hooks */
131652419Sjulian		ni->hooks = 0;
131752419Sjulian		LIST_FOREACH(hook, &here->hooks, hooks) {
131852419Sjulian			struct linkinfo *const link = &hl->link[ni->hooks];
131952419Sjulian
132052419Sjulian			if (ni->hooks >= nhooks) {
132152419Sjulian				log(LOG_ERR, "%s: number of %s changed\n",
132252419Sjulian				    __FUNCTION__, "hooks");
132352419Sjulian				break;
132452419Sjulian			}
132552419Sjulian			if ((hook->flags & HK_INVALID) != 0)
132652419Sjulian				continue;
132752419Sjulian			strncpy(link->ourhook, hook->name, NG_HOOKLEN);
132852419Sjulian			strncpy(link->peerhook, hook->peer->name, NG_HOOKLEN);
132952419Sjulian			if (hook->peer->node->name != NULL)
133052419Sjulian				strncpy(link->nodeinfo.name,
133152419Sjulian				    hook->peer->node->name, NG_NODELEN);
133252419Sjulian			strncpy(link->nodeinfo.type,
133352419Sjulian			   hook->peer->node->type->name, NG_TYPELEN);
133452722Sjulian			link->nodeinfo.id = ng_node2ID(hook->peer->node);
133552419Sjulian			link->nodeinfo.hooks = hook->peer->node->numhooks;
133652419Sjulian			ni->hooks++;
133752419Sjulian		}
133852419Sjulian		*resp = rp;
133952419Sjulian		break;
134052419Sjulian	    }
134152419Sjulian
134252419Sjulian	case NGM_LISTNAMES:
134352419Sjulian	case NGM_LISTNODES:
134452419Sjulian	    {
134552419Sjulian		const int unnamed = (msg->header.cmd == NGM_LISTNODES);
134652419Sjulian		struct namelist *nl;
134752419Sjulian		struct ng_mesg *rp;
134852419Sjulian		node_p node;
134952419Sjulian		int num = 0;
135052419Sjulian
135152419Sjulian		if (resp == NULL) {
135252419Sjulian			error = EINVAL;
135352419Sjulian			break;
135452419Sjulian		}
135552419Sjulian
135652419Sjulian		/* Count number of nodes */
135752419Sjulian		LIST_FOREACH(node, &nodelist, nodes) {
135852419Sjulian			if (unnamed || node->name != NULL)
135952419Sjulian				num++;
136052419Sjulian		}
136152419Sjulian
136252419Sjulian		/* Get response struct */
136352419Sjulian		if (resp == NULL) {
136452419Sjulian			error = EINVAL;
136552419Sjulian			break;
136652419Sjulian		}
136752419Sjulian		NG_MKRESPONSE(rp, msg, sizeof(*nl)
136852419Sjulian		    + (num * sizeof(struct nodeinfo)), M_NOWAIT);
136952419Sjulian		if (rp == NULL) {
137052419Sjulian			error = ENOMEM;
137152419Sjulian			break;
137252419Sjulian		}
137352419Sjulian		nl = (struct namelist *) rp->data;
137452419Sjulian
137552419Sjulian		/* Cycle through the linked list of nodes */
137652419Sjulian		nl->numnames = 0;
137752419Sjulian		LIST_FOREACH(node, &nodelist, nodes) {
137852419Sjulian			struct nodeinfo *const np = &nl->nodeinfo[nl->numnames];
137952419Sjulian
138052419Sjulian			if (nl->numnames >= num) {
138152419Sjulian				log(LOG_ERR, "%s: number of %s changed\n",
138252419Sjulian				    __FUNCTION__, "nodes");
138352419Sjulian				break;
138452419Sjulian			}
138552419Sjulian			if ((node->flags & NG_INVALID) != 0)
138652419Sjulian				continue;
138752419Sjulian			if (!unnamed && node->name == NULL)
138852419Sjulian				continue;
138952419Sjulian			if (node->name != NULL)
139052419Sjulian				strncpy(np->name, node->name, NG_NODELEN);
139152419Sjulian			strncpy(np->type, node->type->name, NG_TYPELEN);
139252722Sjulian			np->id = ng_node2ID(node);
139352419Sjulian			np->hooks = node->numhooks;
139452419Sjulian			nl->numnames++;
139552419Sjulian		}
139652419Sjulian		*resp = rp;
139752419Sjulian		break;
139852419Sjulian	    }
139952419Sjulian
140052419Sjulian	case NGM_LISTTYPES:
140152419Sjulian	    {
140252419Sjulian		struct typelist *tl;
140352419Sjulian		struct ng_mesg *rp;
140452419Sjulian		struct ng_type *type;
140552419Sjulian		int num = 0;
140652419Sjulian
140752419Sjulian		if (resp == NULL) {
140852419Sjulian			error = EINVAL;
140952419Sjulian			break;
141052419Sjulian		}
141152419Sjulian
141252419Sjulian		/* Count number of types */
141352419Sjulian		LIST_FOREACH(type, &typelist, types)
141452419Sjulian			num++;
141552419Sjulian
141652419Sjulian		/* Get response struct */
141752419Sjulian		if (resp == NULL) {
141852419Sjulian			error = EINVAL;
141952419Sjulian			break;
142052419Sjulian		}
142152419Sjulian		NG_MKRESPONSE(rp, msg, sizeof(*tl)
142252419Sjulian		    + (num * sizeof(struct typeinfo)), M_NOWAIT);
142352419Sjulian		if (rp == NULL) {
142452419Sjulian			error = ENOMEM;
142552419Sjulian			break;
142652419Sjulian		}
142752419Sjulian		tl = (struct typelist *) rp->data;
142852419Sjulian
142952419Sjulian		/* Cycle through the linked list of types */
143052419Sjulian		tl->numtypes = 0;
143152419Sjulian		LIST_FOREACH(type, &typelist, types) {
143252419Sjulian			struct typeinfo *const tp = &tl->typeinfo[tl->numtypes];
143352419Sjulian
143452419Sjulian			if (tl->numtypes >= num) {
143552419Sjulian				log(LOG_ERR, "%s: number of %s changed\n",
143652419Sjulian				    __FUNCTION__, "types");
143752419Sjulian				break;
143852419Sjulian			}
143959879Sarchie			strncpy(tp->type_name, type->name, NG_TYPELEN);
144052419Sjulian			tp->numnodes = type->refs;
144152419Sjulian			tl->numtypes++;
144252419Sjulian		}
144352419Sjulian		*resp = rp;
144452419Sjulian		break;
144552419Sjulian	    }
144652419Sjulian
144753913Sarchie	case NGM_BINARY2ASCII:
144853913Sarchie	    {
144953913Sarchie		int bufSize = 2000;	/* XXX hard coded constant */
145053913Sarchie		const struct ng_parse_type *argstype;
145153913Sarchie		const struct ng_cmdlist *c;
145253913Sarchie		struct ng_mesg *rp, *binary, *ascii;
145353913Sarchie
145453913Sarchie		/* Data area must contain a valid netgraph message */
145553913Sarchie		binary = (struct ng_mesg *)msg->data;
145653913Sarchie		if (msg->header.arglen < sizeof(struct ng_mesg)
145753913Sarchie		    || msg->header.arglen - sizeof(struct ng_mesg)
145853913Sarchie		      < binary->header.arglen) {
145953913Sarchie			error = EINVAL;
146053913Sarchie			break;
146153913Sarchie		}
146253913Sarchie
146353913Sarchie		/* Get a response message with lots of room */
146453913Sarchie		NG_MKRESPONSE(rp, msg, sizeof(*ascii) + bufSize, M_NOWAIT);
146553913Sarchie		if (rp == NULL) {
146653913Sarchie			error = ENOMEM;
146753913Sarchie			break;
146853913Sarchie		}
146953913Sarchie		ascii = (struct ng_mesg *)rp->data;
147053913Sarchie
147153913Sarchie		/* Copy binary message header to response message payload */
147253913Sarchie		bcopy(binary, ascii, sizeof(*binary));
147353913Sarchie
147453913Sarchie		/* Find command by matching typecookie and command number */
147553913Sarchie		for (c = here->type->cmdlist;
147653913Sarchie		    c != NULL && c->name != NULL; c++) {
147753913Sarchie			if (binary->header.typecookie == c->cookie
147853913Sarchie			    && binary->header.cmd == c->cmd)
147953913Sarchie				break;
148053913Sarchie		}
148153913Sarchie		if (c == NULL || c->name == NULL) {
148253913Sarchie			for (c = ng_generic_cmds; c->name != NULL; c++) {
148353913Sarchie				if (binary->header.typecookie == c->cookie
148453913Sarchie				    && binary->header.cmd == c->cmd)
148553913Sarchie					break;
148653913Sarchie			}
148753913Sarchie			if (c->name == NULL) {
148853913Sarchie				FREE(rp, M_NETGRAPH);
148953913Sarchie				error = ENOSYS;
149053913Sarchie				break;
149153913Sarchie			}
149253913Sarchie		}
149353913Sarchie
149453913Sarchie		/* Convert command name to ASCII */
149553913Sarchie		snprintf(ascii->header.cmdstr, sizeof(ascii->header.cmdstr),
149653913Sarchie		    "%s", c->name);
149753913Sarchie
149853913Sarchie		/* Convert command arguments to ASCII */
149953913Sarchie		argstype = (binary->header.flags & NGF_RESP) ?
150053913Sarchie		    c->respType : c->mesgType;
150153913Sarchie		if (argstype == NULL)
150253913Sarchie			*ascii->data = '\0';
150353913Sarchie		else {
150453913Sarchie			if ((error = ng_unparse(argstype,
150553913Sarchie			    (u_char *)binary->data,
150653913Sarchie			    ascii->data, bufSize)) != 0) {
150753913Sarchie				FREE(rp, M_NETGRAPH);
150853913Sarchie				break;
150953913Sarchie			}
151053913Sarchie		}
151153913Sarchie
151253913Sarchie		/* Return the result as struct ng_mesg plus ASCII string */
151353913Sarchie		bufSize = strlen(ascii->data) + 1;
151453913Sarchie		ascii->header.arglen = bufSize;
151553913Sarchie		rp->header.arglen = sizeof(*ascii) + bufSize;
151653913Sarchie		*resp = rp;
151753913Sarchie		break;
151853913Sarchie	    }
151953913Sarchie
152053913Sarchie	case NGM_ASCII2BINARY:
152153913Sarchie	    {
152253913Sarchie		int bufSize = 2000;	/* XXX hard coded constant */
152353913Sarchie		const struct ng_cmdlist *c;
152453913Sarchie		const struct ng_parse_type *argstype;
152553913Sarchie		struct ng_mesg *rp, *ascii, *binary;
152659178Sarchie		int off = 0;
152753913Sarchie
152853913Sarchie		/* Data area must contain at least a struct ng_mesg + '\0' */
152953913Sarchie		ascii = (struct ng_mesg *)msg->data;
153053913Sarchie		if (msg->header.arglen < sizeof(*ascii) + 1
153153913Sarchie		    || ascii->header.arglen < 1
153253913Sarchie		    || msg->header.arglen
153353913Sarchie		      < sizeof(*ascii) + ascii->header.arglen) {
153453913Sarchie			error = EINVAL;
153553913Sarchie			break;
153653913Sarchie		}
153753913Sarchie		ascii->data[ascii->header.arglen - 1] = '\0';
153853913Sarchie
153953913Sarchie		/* Get a response message with lots of room */
154053913Sarchie		NG_MKRESPONSE(rp, msg, sizeof(*binary) + bufSize, M_NOWAIT);
154153913Sarchie		if (rp == NULL) {
154253913Sarchie			error = ENOMEM;
154353913Sarchie			break;
154453913Sarchie		}
154553913Sarchie		binary = (struct ng_mesg *)rp->data;
154653913Sarchie
154753913Sarchie		/* Copy ASCII message header to response message payload */
154853913Sarchie		bcopy(ascii, binary, sizeof(*ascii));
154953913Sarchie
155053913Sarchie		/* Find command by matching ASCII command string */
155153913Sarchie		for (c = here->type->cmdlist;
155253913Sarchie		    c != NULL && c->name != NULL; c++) {
155353913Sarchie			if (strcmp(ascii->header.cmdstr, c->name) == 0)
155453913Sarchie				break;
155553913Sarchie		}
155653913Sarchie		if (c == NULL || c->name == NULL) {
155753913Sarchie			for (c = ng_generic_cmds; c->name != NULL; c++) {
155853913Sarchie				if (strcmp(ascii->header.cmdstr, c->name) == 0)
155953913Sarchie					break;
156053913Sarchie			}
156153913Sarchie			if (c->name == NULL) {
156253913Sarchie				FREE(rp, M_NETGRAPH);
156353913Sarchie				error = ENOSYS;
156453913Sarchie				break;
156553913Sarchie			}
156653913Sarchie		}
156753913Sarchie
156853913Sarchie		/* Convert command name to binary */
156953913Sarchie		binary->header.cmd = c->cmd;
157053913Sarchie		binary->header.typecookie = c->cookie;
157153913Sarchie
157253913Sarchie		/* Convert command arguments to binary */
157353913Sarchie		argstype = (binary->header.flags & NGF_RESP) ?
157453913Sarchie		    c->respType : c->mesgType;
157553913Sarchie		if (argstype == NULL)
157653913Sarchie			bufSize = 0;
157753913Sarchie		else {
157853913Sarchie			if ((error = ng_parse(argstype, ascii->data,
157953913Sarchie			    &off, (u_char *)binary->data, &bufSize)) != 0) {
158053913Sarchie				FREE(rp, M_NETGRAPH);
158153913Sarchie				break;
158253913Sarchie			}
158353913Sarchie		}
158453913Sarchie
158553913Sarchie		/* Return the result */
158653913Sarchie		binary->header.arglen = bufSize;
158753913Sarchie		rp->header.arglen = sizeof(*binary) + bufSize;
158853913Sarchie		*resp = rp;
158953913Sarchie		break;
159053913Sarchie	    }
159153913Sarchie
159252419Sjulian	case NGM_TEXT_STATUS:
159352419Sjulian		/*
159452419Sjulian		 * This one is tricky as it passes the command down to the
159552419Sjulian		 * actual node, even though it is a generic type command.
159652419Sjulian		 * This means we must assume that the msg is already freed
159752419Sjulian		 * when control passes back to us.
159852419Sjulian		 */
159952419Sjulian		if (resp == NULL) {
160052419Sjulian			error = EINVAL;
160152419Sjulian			break;
160252419Sjulian		}
160352419Sjulian		if (here->type->rcvmsg != NULL)
160459728Sjulian			return((*here->type->rcvmsg)(here, msg, retaddr,
160559728Sjulian			resp, lasthook));
160652419Sjulian		/* Fall through if rcvmsg not supported */
160752419Sjulian	default:
160852419Sjulian		TRAP_ERROR;
160952419Sjulian		error = EINVAL;
161052419Sjulian	}
161152419Sjulian	FREE(msg, M_NETGRAPH);
161252419Sjulian	return (error);
161352419Sjulian}
161452419Sjulian
161552419Sjulian/*
161652419Sjulian * Send a data packet to a node. If the recipient has no
161752419Sjulian * 'receive data' method, then silently discard the packet.
161852419Sjulian */
161952419Sjulianint
162059728Sjulianng_send_data(hook_p hook, struct mbuf *m, meta_p meta,
162159728Sjulian	struct mbuf **ret_m, meta_p *ret_meta)
162252419Sjulian{
162359728Sjulian	ng_rcvdata_t *rcvdata;
162452419Sjulian	int error;
162552419Sjulian
162653403Sarchie	CHECK_DATA_MBUF(m);
162752419Sjulian	if (hook && (hook->flags & HK_INVALID) == 0) {
162852419Sjulian		rcvdata = hook->peer->node->type->rcvdata;
162952419Sjulian		if (rcvdata != NULL)
163059728Sjulian			error = (*rcvdata)(hook->peer, m, meta,
163159728Sjulian					ret_m, ret_meta);
163252419Sjulian		else {
163352419Sjulian			error = 0;
163452419Sjulian			NG_FREE_DATA(m, meta);
163552419Sjulian		}
163652419Sjulian	} else {
163752419Sjulian		TRAP_ERROR;
163852419Sjulian		error = ENOTCONN;
163952419Sjulian		NG_FREE_DATA(m, meta);
164052419Sjulian	}
164152419Sjulian	return (error);
164252419Sjulian}
164352419Sjulian
164452419Sjulian/*
164552419Sjulian * Send a queued data packet to a node. If the recipient has no
164652419Sjulian * 'receive queued data' method, then try the 'receive data' method above.
164752419Sjulian */
164852419Sjulianint
164959728Sjulianng_send_dataq(hook_p hook, struct mbuf *m, meta_p meta,
165059728Sjulian	struct mbuf **ret_m, meta_p *ret_meta)
165152419Sjulian{
165259728Sjulian	ng_rcvdata_t *rcvdata;
165352419Sjulian	int error;
165452419Sjulian
165553403Sarchie	CHECK_DATA_MBUF(m);
165652419Sjulian	if (hook && (hook->flags & HK_INVALID) == 0) {
165759728Sjulian		rcvdata = hook->peer->node->type->rcvdataq;
165859728Sjulian		if (rcvdata != NULL)
165959728Sjulian			error = (*rcvdata)(hook->peer, m, meta,
166059728Sjulian					ret_m, ret_meta);
166152419Sjulian		else {
166259728Sjulian			rcvdata = hook->peer->node->type->rcvdata;
166359728Sjulian			if (rcvdata != NULL) {
166459728Sjulian				error = (*rcvdata)(hook->peer, m, meta,
166559728Sjulian					ret_m, ret_meta);
166659728Sjulian			} else {
166759728Sjulian				error = 0;
166859728Sjulian				NG_FREE_DATA(m, meta);
166959728Sjulian			}
167052419Sjulian		}
167152419Sjulian	} else {
167252419Sjulian		TRAP_ERROR;
167352419Sjulian		error = ENOTCONN;
167452419Sjulian		NG_FREE_DATA(m, meta);
167552419Sjulian	}
167652419Sjulian	return (error);
167752419Sjulian}
167852419Sjulian
167959879Sarchie/*
168059879Sarchie * Copy a 'meta'.
168159879Sarchie *
168259879Sarchie * Returns new meta, or NULL if original meta is NULL or ENOMEM.
168359879Sarchie */
168459879Sarchiemeta_p
168559879Sarchieng_copy_meta(meta_p meta)
168659879Sarchie{
168759879Sarchie	meta_p meta2;
168859879Sarchie
168959879Sarchie	if (meta == NULL)
169059879Sarchie		return (NULL);
169159879Sarchie	MALLOC(meta2, meta_p, meta->used_len, M_NETGRAPH, M_NOWAIT);
169259879Sarchie	if (meta2 == NULL)
169359879Sarchie		return (NULL);
169459879Sarchie	meta2->allocated_len = meta->used_len;
169559879Sarchie	bcopy(meta, meta2, meta->used_len);
169659879Sarchie	return (meta2);
169759879Sarchie}
169859879Sarchie
169952419Sjulian/************************************************************************
170052419Sjulian			Module routines
170152419Sjulian************************************************************************/
170252419Sjulian
170352419Sjulian/*
170452419Sjulian * Handle the loading/unloading of a netgraph node type module
170552419Sjulian */
170652419Sjulianint
170752419Sjulianng_mod_event(module_t mod, int event, void *data)
170852419Sjulian{
170952419Sjulian	struct ng_type *const type = data;
171052419Sjulian	int s, error = 0;
171152419Sjulian
171252419Sjulian	switch (event) {
171352419Sjulian	case MOD_LOAD:
171452419Sjulian
171552419Sjulian		/* Register new netgraph node type */
171652419Sjulian		s = splnet();
171752419Sjulian		if ((error = ng_newtype(type)) != 0) {
171852419Sjulian			splx(s);
171952419Sjulian			break;
172052419Sjulian		}
172152419Sjulian
172252419Sjulian		/* Call type specific code */
172352419Sjulian		if (type->mod_event != NULL)
172452419Sjulian			if ((error = (*type->mod_event)(mod, event, data)) != 0)
172552419Sjulian				LIST_REMOVE(type, types);
172652419Sjulian		splx(s);
172752419Sjulian		break;
172852419Sjulian
172952419Sjulian	case MOD_UNLOAD:
173052419Sjulian		s = splnet();
173152419Sjulian		if (type->refs != 0)		/* make sure no nodes exist! */
173252419Sjulian			error = EBUSY;
173352419Sjulian		else {
173452419Sjulian			if (type->mod_event != NULL) {	/* check with type */
173552419Sjulian				error = (*type->mod_event)(mod, event, data);
173652419Sjulian				if (error != 0) {	/* type refuses.. */
173752419Sjulian					splx(s);
173852419Sjulian					break;
173952419Sjulian				}
174052419Sjulian			}
174152419Sjulian			LIST_REMOVE(type, types);
174252419Sjulian		}
174352419Sjulian		splx(s);
174452419Sjulian		break;
174552419Sjulian
174652419Sjulian	default:
174752419Sjulian		if (type->mod_event != NULL)
174852419Sjulian			error = (*type->mod_event)(mod, event, data);
174952419Sjulian		else
175052419Sjulian			error = 0;		/* XXX ? */
175152419Sjulian		break;
175252419Sjulian	}
175352419Sjulian	return (error);
175452419Sjulian}
175552419Sjulian
175652419Sjulian/*
175752419Sjulian * Handle loading and unloading for this code.
175852419Sjulian * The only thing we need to link into is the NETISR strucure.
175952419Sjulian */
176052419Sjulianstatic int
176152419Sjulianngb_mod_event(module_t mod, int event, void *data)
176252419Sjulian{
176352419Sjulian	int s, error = 0;
176452419Sjulian
176552419Sjulian	switch (event) {
176652419Sjulian	case MOD_LOAD:
176752419Sjulian		/* Register line discipline */
176852419Sjulian		s = splimp();
176952419Sjulian		error = register_netisr(NETISR_NETGRAPH, ngintr);
177052419Sjulian		splx(s);
177152419Sjulian		break;
177252419Sjulian	case MOD_UNLOAD:
177352419Sjulian		/* You cant unload it because an interface may be using it.  */
177452419Sjulian		error = EBUSY;
177552419Sjulian		break;
177652419Sjulian	default:
177752419Sjulian		error = EOPNOTSUPP;
177852419Sjulian		break;
177952419Sjulian	}
178052419Sjulian	return (error);
178152419Sjulian}
178252419Sjulian
178352419Sjulianstatic moduledata_t netgraph_mod = {
178452419Sjulian	"netgraph",
178552419Sjulian	ngb_mod_event,
178652419Sjulian	(NULL)
178752419Sjulian};
178852419SjulianDECLARE_MODULE(netgraph, netgraph_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
178952419Sjulian
179052419Sjulian/************************************************************************
179152419Sjulian			Queueing routines
179252419Sjulian************************************************************************/
179352419Sjulian
179452419Sjulian/* The structure for queueing across ISR switches */
179552419Sjulianstruct ng_queue_entry {
179652419Sjulian	u_long	flags;
179752419Sjulian	struct ng_queue_entry *next;
179852419Sjulian	union {
179952419Sjulian		struct {
180052419Sjulian			hook_p		da_hook;	/*  target hook */
180152419Sjulian			struct mbuf	*da_m;
180252419Sjulian			meta_p		da_meta;
180352419Sjulian		} data;
180452419Sjulian		struct {
180552419Sjulian			struct ng_mesg	*msg_msg;
180652419Sjulian			node_p		msg_node;
180752419Sjulian			void		*msg_retaddr;
180859728Sjulian			hook_p		msg_lasthook;
180952419Sjulian		} msg;
181052419Sjulian	} body;
181152419Sjulian};
181252419Sjulian#define NGQF_DATA	0x01		/* the queue element is data */
181352419Sjulian#define NGQF_MESG	0x02		/* the queue element is a message */
181452419Sjulian
181552419Sjulianstatic struct ng_queue_entry   *ngqbase;	/* items to be unqueued */
181652419Sjulianstatic struct ng_queue_entry   *ngqlast;	/* last item queued */
181752419Sjulianstatic const int		ngqroom = 64;	/* max items to queue */
181852419Sjulianstatic int			ngqsize;	/* number of items in queue */
181952419Sjulian
182052419Sjulianstatic struct ng_queue_entry   *ngqfree;	/* free ones */
182152419Sjulianstatic const int		ngqfreemax = 16;/* cache at most this many */
182252419Sjulianstatic int			ngqfreesize;	/* number of cached entries */
182352419Sjulian
182452419Sjulian/*
182552419Sjulian * Get a queue entry
182652419Sjulian */
182752419Sjulianstatic struct ng_queue_entry *
182852419Sjulianng_getqblk(void)
182952419Sjulian{
183052419Sjulian	register struct ng_queue_entry *q;
183152419Sjulian	int s;
183252419Sjulian
183352419Sjulian	/* Could be guarding against tty ints or whatever */
183452419Sjulian	s = splhigh();
183552419Sjulian
183652419Sjulian	/* Try get a cached queue block, or else allocate a new one */
183752419Sjulian	if ((q = ngqfree) == NULL) {
183852419Sjulian		splx(s);
183952419Sjulian		if (ngqsize < ngqroom) {	/* don't worry about races */
184052419Sjulian			MALLOC(q, struct ng_queue_entry *,
184152419Sjulian			    sizeof(*q), M_NETGRAPH, M_NOWAIT);
184252419Sjulian		}
184352419Sjulian	} else {
184452419Sjulian		ngqfree = q->next;
184552419Sjulian		ngqfreesize--;
184652419Sjulian		splx(s);
184752419Sjulian	}
184852419Sjulian	return (q);
184952419Sjulian}
185052419Sjulian
185152419Sjulian/*
185252419Sjulian * Release a queue entry
185352419Sjulian */
185452419Sjulian#define RETURN_QBLK(q)							\
185552419Sjuliando {									\
185652419Sjulian	int s;								\
185752419Sjulian	if (ngqfreesize < ngqfreemax) { /* don't worry about races */ 	\
185852419Sjulian		s = splhigh();						\
185952419Sjulian		(q)->next = ngqfree;					\
186052419Sjulian		ngqfree = (q);						\
186152419Sjulian		ngqfreesize++;						\
186252419Sjulian		splx(s);						\
186352419Sjulian	} else {							\
186452419Sjulian		FREE((q), M_NETGRAPH);					\
186552419Sjulian	}								\
186652419Sjulian} while (0)
186752419Sjulian
186852419Sjulian/*
186952419Sjulian * Running at a raised (but we don't know which) processor priority level,
187052419Sjulian * put the data onto a queue to be picked up by another PPL (probably splnet)
187152419Sjulian */
187252419Sjulianint
187352419Sjulianng_queue_data(hook_p hook, struct mbuf *m, meta_p meta)
187452419Sjulian{
187552419Sjulian	struct ng_queue_entry *q;
187652419Sjulian	int s;
187752419Sjulian
187852419Sjulian	if (hook == NULL) {
187952419Sjulian		NG_FREE_DATA(m, meta);
188052419Sjulian		return (0);
188152419Sjulian	}
188252419Sjulian	if ((q = ng_getqblk()) == NULL) {
188352419Sjulian		NG_FREE_DATA(m, meta);
188452419Sjulian		return (ENOBUFS);
188552419Sjulian	}
188652419Sjulian
188752419Sjulian	/* Fill out the contents */
188852419Sjulian	q->flags = NGQF_DATA;
188952419Sjulian	q->next = NULL;
189052419Sjulian	q->body.data.da_hook = hook;
189152419Sjulian	q->body.data.da_m = m;
189252419Sjulian	q->body.data.da_meta = meta;
189352419Sjulian	hook->refs++;		/* don't let it go away while on the queue */
189452419Sjulian
189552419Sjulian	/* Put it on the queue */
189652419Sjulian	s = splhigh();
189752419Sjulian	if (ngqbase) {
189852419Sjulian		ngqlast->next = q;
189952419Sjulian	} else {
190052419Sjulian		ngqbase = q;
190152419Sjulian	}
190252419Sjulian	ngqlast = q;
190352419Sjulian	ngqsize++;
190452419Sjulian	splx(s);
190552419Sjulian
190652419Sjulian	/* Schedule software interrupt to handle it later */
190752419Sjulian	schednetisr(NETISR_NETGRAPH);
190852419Sjulian	return (0);
190952419Sjulian}
191052419Sjulian
191152419Sjulian/*
191252419Sjulian * Running at a raised (but we don't know which) processor priority level,
191352419Sjulian * put the msg onto a queue to be picked up by another PPL (probably splnet)
191452419Sjulian */
191552419Sjulianint
191658013Sarchieng_queue_msg(node_p here, struct ng_mesg *msg, const char *address)
191752419Sjulian{
191852419Sjulian	register struct ng_queue_entry *q;
191952419Sjulian	int     s;
192052419Sjulian	node_p  dest = NULL;
192152419Sjulian	char   *retaddr = NULL;
192252419Sjulian	int     error;
192359728Sjulian	hook_p	lasthook = NULL;
192452419Sjulian
192552722Sjulian	/* Find the target node. */
192659728Sjulian	error = ng_path2node(here, address, &dest, &retaddr, &lasthook);
192752419Sjulian	if (error) {
192852419Sjulian		FREE(msg, M_NETGRAPH);
192952419Sjulian		return (error);
193052419Sjulian	}
193152419Sjulian	if ((q = ng_getqblk()) == NULL) {
193252419Sjulian		FREE(msg, M_NETGRAPH);
193352419Sjulian		if (retaddr)
193452419Sjulian			FREE(retaddr, M_NETGRAPH);
193552419Sjulian		return (ENOBUFS);
193652419Sjulian	}
193752419Sjulian
193852419Sjulian	/* Fill out the contents */
193952419Sjulian	q->flags = NGQF_MESG;
194052419Sjulian	q->next = NULL;
194152419Sjulian	q->body.msg.msg_node = dest;
194252419Sjulian	q->body.msg.msg_msg = msg;
194352419Sjulian	q->body.msg.msg_retaddr = retaddr;
194459728Sjulian	q->body.msg.msg_lasthook = lasthook;
194552419Sjulian	dest->refs++;		/* don't let it go away while on the queue */
194659728Sjulian	if (lasthook)
194759728Sjulian		lasthook->refs++; /* same for the hook */
194852419Sjulian
194952419Sjulian	/* Put it on the queue */
195052419Sjulian	s = splhigh();
195152419Sjulian	if (ngqbase) {
195252419Sjulian		ngqlast->next = q;
195352419Sjulian	} else {
195452419Sjulian		ngqbase = q;
195552419Sjulian	}
195652419Sjulian	ngqlast = q;
195752419Sjulian	ngqsize++;
195852419Sjulian	splx(s);
195952419Sjulian
196052419Sjulian	/* Schedule software interrupt to handle it later */
196152419Sjulian	schednetisr(NETISR_NETGRAPH);
196252419Sjulian	return (0);
196352419Sjulian}
196452419Sjulian
196552419Sjulian/*
196652419Sjulian * Pick an item off the queue, process it, and dispose of the queue entry.
196752419Sjulian * Should be running at splnet.
196852419Sjulian */
196952419Sjulianstatic void
197052419Sjulianngintr(void)
197152419Sjulian{
197252419Sjulian	hook_p  hook;
197352419Sjulian	struct ng_queue_entry *ngq;
197452419Sjulian	struct mbuf *m;
197552419Sjulian	meta_p  meta;
197652419Sjulian	void   *retaddr;
197752419Sjulian	struct ng_mesg *msg;
197852419Sjulian	node_p  node;
197952419Sjulian	int     error = 0;
198052419Sjulian	int     s;
198152419Sjulian
198252419Sjulian	while (1) {
198352419Sjulian		s = splhigh();
198452419Sjulian		if ((ngq = ngqbase)) {
198552419Sjulian			ngqbase = ngq->next;
198652419Sjulian			ngqsize--;
198752419Sjulian		}
198852419Sjulian		splx(s);
198952419Sjulian		if (ngq == NULL)
199052419Sjulian			return;
199152419Sjulian		switch (ngq->flags) {
199252419Sjulian		case NGQF_DATA:
199352419Sjulian			hook = ngq->body.data.da_hook;
199452419Sjulian			m = ngq->body.data.da_m;
199552419Sjulian			meta = ngq->body.data.da_meta;
199652419Sjulian			RETURN_QBLK(ngq);
199752419Sjulian			NG_SEND_DATAQ(error, hook, m, meta);
199852419Sjulian			ng_unref_hook(hook);
199952419Sjulian			break;
200052419Sjulian		case NGQF_MESG:
200152419Sjulian			node = ngq->body.msg.msg_node;
200252419Sjulian			msg = ngq->body.msg.msg_msg;
200352419Sjulian			retaddr = ngq->body.msg.msg_retaddr;
200459728Sjulian			hook = ngq->body.msg.msg_lasthook;
200552419Sjulian			RETURN_QBLK(ngq);
200659728Sjulian			if (hook) {
200759728Sjulian				if ((hook->refs == 1)
200859728Sjulian		    		|| (hook->flags & HK_INVALID) != 0) {
200959728Sjulian				/* If the hook only has one ref left
201059728Sjulian					then we can't use it */
201159728Sjulian					ng_unref_hook(hook);
201259728Sjulian					hook = NULL;
201359728Sjulian				} else {
201459728Sjulian					ng_unref_hook(hook);
201559728Sjulian				}
201659728Sjulian			}
201759728Sjulian			/* similarly, if the node is a zombie.. */
201852419Sjulian			if (node->flags & NG_INVALID) {
201952419Sjulian				FREE(msg, M_NETGRAPH);
202052419Sjulian			} else {
202152419Sjulian				CALL_MSG_HANDLER(error, node, msg,
202259728Sjulian						 retaddr, NULL, hook);
202352419Sjulian			}
202452419Sjulian			ng_unref(node);
202552419Sjulian			if (retaddr)
202652419Sjulian				FREE(retaddr, M_NETGRAPH);
202752419Sjulian			break;
202852419Sjulian		default:
202952419Sjulian			RETURN_QBLK(ngq);
203052419Sjulian		}
203152419Sjulian	}
203252419Sjulian}
203352419Sjulian
203452419Sjulian
2035