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