ng_base.c revision 54096
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 54096 1999-12-03 21:17:30Z 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> 5752419Sjulian#include <sys/socketvar.h> 5852843Sphk#include <sys/ctype.h> 5952816Sarchie#include <machine/limits.h> 6052419Sjulian 6152419Sjulian#include <net/netisr.h> 6252419Sjulian 6352419Sjulian#include <netgraph/ng_message.h> 6452419Sjulian#include <netgraph/netgraph.h> 6553913Sarchie#include <netgraph/ng_parse.h> 6652419Sjulian 6752419Sjulian/* List of all nodes */ 6852419Sjulianstatic LIST_HEAD(, ng_node) nodelist; 6952419Sjulian 7052419Sjulian/* List of installed types */ 7152419Sjulianstatic LIST_HEAD(, ng_type) typelist; 7252419Sjulian 7352722Sjulian/* Hash releted definitions */ 7452722Sjulian#define ID_HASH_SIZE 32 /* most systems wont need even this many */ 7552722Sjulianstatic LIST_HEAD(, ng_node) ID_hash[ID_HASH_SIZE]; 7652722Sjulian/* Don't nead to initialise them because it's a LIST */ 7752722Sjulian 7852419Sjulian/* Internal functions */ 7952419Sjulianstatic int ng_add_hook(node_p node, const char *name, hook_p * hookp); 8052419Sjulianstatic int ng_connect(hook_p hook1, hook_p hook2); 8152419Sjulianstatic void ng_disconnect_hook(hook_p hook); 8252419Sjulianstatic int ng_generic_msg(node_p here, struct ng_mesg *msg, 8352419Sjulian const char *retaddr, struct ng_mesg ** resp); 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 101952419Sjulianng_path2node(node_p here, const char *address, node_p *destp, char **rtnp) 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; 102652419Sjulian 102752419Sjulian /* Initialize */ 102852419Sjulian if (rtnp) 102952419Sjulian *rtnp = NULL; 103052419Sjulian if (destp == NULL) 103152419Sjulian return EINVAL; 103252419Sjulian *destp = NULL; 103352419Sjulian 103452419Sjulian /* Make a writable copy of address for ng_path_parse() */ 103552419Sjulian strncpy(fullpath, address, sizeof(fullpath) - 1); 103652419Sjulian fullpath[sizeof(fullpath) - 1] = '\0'; 103752419Sjulian 103852419Sjulian /* Parse out node and sequence of hooks */ 103952419Sjulian if (ng_path_parse(fullpath, &nodename, &path, NULL) < 0) { 104052419Sjulian TRAP_ERROR; 104152419Sjulian return EINVAL; 104252419Sjulian } 104352419Sjulian if (path == NULL) { 104452419Sjulian pbuf[0] = '.'; /* Needs to be writable */ 104552419Sjulian pbuf[1] = '\0'; 104652419Sjulian path = pbuf; 104752419Sjulian } 104852419Sjulian 104952419Sjulian /* For an absolute address, jump to the starting node */ 105052419Sjulian if (nodename) { 105152419Sjulian node = ng_findname(here, nodename); 105252419Sjulian if (node == NULL) { 105352419Sjulian TRAP_ERROR; 105452419Sjulian return (ENOENT); 105552419Sjulian } 105652419Sjulian } else 105752419Sjulian node = here; 105852419Sjulian 105952419Sjulian /* Now follow the sequence of hooks */ 106052419Sjulian for (cp = path; node != NULL && *cp != '\0'; ) { 106152419Sjulian hook_p hook; 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; 111652419Sjulian return (0); 111752419Sjulian} 111852419Sjulian 111952419Sjulian/* 112052419Sjulian * Call the appropriate message handler for the object. 112152419Sjulian * It is up to the message handler to free the message. 112252419Sjulian * If it's a generic message, handle it generically, otherwise 112352419Sjulian * call the type's message handler (if it exists) 112452419Sjulian */ 112552419Sjulian 112652419Sjulian#define CALL_MSG_HANDLER(error, node, msg, retaddr, resp) \ 112752419Sjuliando { \ 112852419Sjulian if((msg)->header.typecookie == NGM_GENERIC_COOKIE) { \ 112952419Sjulian (error) = ng_generic_msg((node), (msg), \ 113052419Sjulian (retaddr), (resp)); \ 113152419Sjulian } else { \ 113252419Sjulian if ((node)->type->rcvmsg != NULL) { \ 113352419Sjulian (error) = (*(node)->type->rcvmsg)((node), \ 113452419Sjulian (msg), (retaddr), (resp)); \ 113552419Sjulian } else { \ 113652419Sjulian TRAP_ERROR; \ 113752419Sjulian FREE((msg), M_NETGRAPH); \ 113852419Sjulian (error) = EINVAL; \ 113952419Sjulian } \ 114052419Sjulian } \ 114152419Sjulian} while (0) 114252419Sjulian 114352419Sjulian 114452419Sjulian/* 114552419Sjulian * Send a control message to a node 114652419Sjulian */ 114752419Sjulianint 114852419Sjulianng_send_msg(node_p here, struct ng_mesg *msg, const char *address, 114952419Sjulian struct ng_mesg **rptr) 115052419Sjulian{ 115152419Sjulian node_p dest = NULL; 115252419Sjulian char *retaddr = NULL; 115352419Sjulian int error; 115452419Sjulian 115552419Sjulian /* Find the target node */ 115652419Sjulian error = ng_path2node(here, address, &dest, &retaddr); 115752419Sjulian if (error) { 115852419Sjulian FREE(msg, M_NETGRAPH); 115952419Sjulian return error; 116052419Sjulian } 116152419Sjulian 116252419Sjulian /* Make sure the resp field is null before we start */ 116352419Sjulian if (rptr != NULL) 116452419Sjulian *rptr = NULL; 116552419Sjulian 116652419Sjulian CALL_MSG_HANDLER(error, dest, msg, retaddr, rptr); 116752419Sjulian 116852419Sjulian /* Make sure that if there is a response, it has the RESP bit set */ 116952419Sjulian if ((error == 0) && rptr && *rptr) 117052419Sjulian (*rptr)->header.flags |= NGF_RESP; 117152419Sjulian 117252419Sjulian /* 117352419Sjulian * If we had a return address it is up to us to free it. They should 117452419Sjulian * have taken a copy if they needed to make a delayed response. 117552419Sjulian */ 117652419Sjulian if (retaddr) 117752419Sjulian FREE(retaddr, M_NETGRAPH); 117852419Sjulian return (error); 117952419Sjulian} 118052419Sjulian 118152419Sjulian/* 118252419Sjulian * Implement the 'generic' control messages 118352419Sjulian */ 118452419Sjulianstatic int 118552419Sjulianng_generic_msg(node_p here, struct ng_mesg *msg, const char *retaddr, 118652419Sjulian struct ng_mesg **resp) 118752419Sjulian{ 118852419Sjulian int error = 0; 118952419Sjulian 119052419Sjulian if (msg->header.typecookie != NGM_GENERIC_COOKIE) { 119152419Sjulian TRAP_ERROR; 119252419Sjulian FREE(msg, M_NETGRAPH); 119352419Sjulian return (EINVAL); 119452419Sjulian } 119552419Sjulian switch (msg->header.cmd) { 119652419Sjulian case NGM_SHUTDOWN: 119752419Sjulian ng_rmnode(here); 119852419Sjulian break; 119952419Sjulian case NGM_MKPEER: 120052419Sjulian { 120152419Sjulian struct ngm_mkpeer *const mkp = (struct ngm_mkpeer *) msg->data; 120252419Sjulian 120352419Sjulian if (msg->header.arglen != sizeof(*mkp)) { 120452419Sjulian TRAP_ERROR; 120552419Sjulian return (EINVAL); 120652419Sjulian } 120752419Sjulian mkp->type[sizeof(mkp->type) - 1] = '\0'; 120852419Sjulian mkp->ourhook[sizeof(mkp->ourhook) - 1] = '\0'; 120952419Sjulian mkp->peerhook[sizeof(mkp->peerhook) - 1] = '\0'; 121052419Sjulian error = ng_mkpeer(here, mkp->ourhook, mkp->peerhook, mkp->type); 121152419Sjulian break; 121252419Sjulian } 121352419Sjulian case NGM_CONNECT: 121452419Sjulian { 121552419Sjulian struct ngm_connect *const con = 121652419Sjulian (struct ngm_connect *) msg->data; 121752419Sjulian node_p node2; 121852419Sjulian 121952419Sjulian if (msg->header.arglen != sizeof(*con)) { 122052419Sjulian TRAP_ERROR; 122152419Sjulian return (EINVAL); 122252419Sjulian } 122352419Sjulian con->path[sizeof(con->path) - 1] = '\0'; 122452419Sjulian con->ourhook[sizeof(con->ourhook) - 1] = '\0'; 122552419Sjulian con->peerhook[sizeof(con->peerhook) - 1] = '\0'; 122652419Sjulian error = ng_path2node(here, con->path, &node2, NULL); 122752419Sjulian if (error) 122852419Sjulian break; 122952419Sjulian error = ng_con_nodes(here, con->ourhook, node2, con->peerhook); 123052419Sjulian break; 123152419Sjulian } 123252419Sjulian case NGM_NAME: 123352419Sjulian { 123452419Sjulian struct ngm_name *const nam = (struct ngm_name *) msg->data; 123552419Sjulian 123652419Sjulian if (msg->header.arglen != sizeof(*nam)) { 123752419Sjulian TRAP_ERROR; 123852419Sjulian return (EINVAL); 123952419Sjulian } 124052419Sjulian nam->name[sizeof(nam->name) - 1] = '\0'; 124152419Sjulian error = ng_name_node(here, nam->name); 124252419Sjulian break; 124352419Sjulian } 124452419Sjulian case NGM_RMHOOK: 124552419Sjulian { 124652419Sjulian struct ngm_rmhook *const rmh = (struct ngm_rmhook *) msg->data; 124752419Sjulian hook_p hook; 124852419Sjulian 124952419Sjulian if (msg->header.arglen != sizeof(*rmh)) { 125052419Sjulian TRAP_ERROR; 125152419Sjulian return (EINVAL); 125252419Sjulian } 125352419Sjulian rmh->ourhook[sizeof(rmh->ourhook) - 1] = '\0'; 125454096Sarchie if ((hook = ng_findhook(here, rmh->ourhook)) != NULL) 125552419Sjulian ng_destroy_hook(hook); 125652419Sjulian break; 125752419Sjulian } 125852419Sjulian case NGM_NODEINFO: 125952419Sjulian { 126052419Sjulian struct nodeinfo *ni; 126152419Sjulian struct ng_mesg *rp; 126252419Sjulian 126352419Sjulian /* Get response struct */ 126452419Sjulian if (resp == NULL) { 126552419Sjulian error = EINVAL; 126652419Sjulian break; 126752419Sjulian } 126852419Sjulian NG_MKRESPONSE(rp, msg, sizeof(*ni), M_NOWAIT); 126952419Sjulian if (rp == NULL) { 127052419Sjulian error = ENOMEM; 127152419Sjulian break; 127252419Sjulian } 127352419Sjulian 127452419Sjulian /* Fill in node info */ 127552419Sjulian ni = (struct nodeinfo *) rp->data; 127652419Sjulian if (here->name != NULL) 127752419Sjulian strncpy(ni->name, here->name, NG_NODELEN); 127852419Sjulian strncpy(ni->type, here->type->name, NG_TYPELEN); 127952722Sjulian ni->id = ng_node2ID(here); 128052419Sjulian ni->hooks = here->numhooks; 128152419Sjulian *resp = rp; 128252419Sjulian break; 128352419Sjulian } 128452419Sjulian case NGM_LISTHOOKS: 128552419Sjulian { 128652419Sjulian const int nhooks = here->numhooks; 128752419Sjulian struct hooklist *hl; 128852419Sjulian struct nodeinfo *ni; 128952419Sjulian struct ng_mesg *rp; 129052419Sjulian hook_p hook; 129152419Sjulian 129252419Sjulian /* Get response struct */ 129352419Sjulian if (resp == NULL) { 129452419Sjulian error = EINVAL; 129552419Sjulian break; 129652419Sjulian } 129752419Sjulian NG_MKRESPONSE(rp, msg, sizeof(*hl) 129852419Sjulian + (nhooks * sizeof(struct linkinfo)), M_NOWAIT); 129952419Sjulian if (rp == NULL) { 130052419Sjulian error = ENOMEM; 130152419Sjulian break; 130252419Sjulian } 130352419Sjulian hl = (struct hooklist *) rp->data; 130452419Sjulian ni = &hl->nodeinfo; 130552419Sjulian 130652419Sjulian /* Fill in node info */ 130752419Sjulian if (here->name) 130852419Sjulian strncpy(ni->name, here->name, NG_NODELEN); 130952419Sjulian strncpy(ni->type, here->type->name, NG_TYPELEN); 131052722Sjulian ni->id = ng_node2ID(here); 131152419Sjulian 131252419Sjulian /* Cycle through the linked list of hooks */ 131352419Sjulian ni->hooks = 0; 131452419Sjulian LIST_FOREACH(hook, &here->hooks, hooks) { 131552419Sjulian struct linkinfo *const link = &hl->link[ni->hooks]; 131652419Sjulian 131752419Sjulian if (ni->hooks >= nhooks) { 131852419Sjulian log(LOG_ERR, "%s: number of %s changed\n", 131952419Sjulian __FUNCTION__, "hooks"); 132052419Sjulian break; 132152419Sjulian } 132252419Sjulian if ((hook->flags & HK_INVALID) != 0) 132352419Sjulian continue; 132452419Sjulian strncpy(link->ourhook, hook->name, NG_HOOKLEN); 132552419Sjulian strncpy(link->peerhook, hook->peer->name, NG_HOOKLEN); 132652419Sjulian if (hook->peer->node->name != NULL) 132752419Sjulian strncpy(link->nodeinfo.name, 132852419Sjulian hook->peer->node->name, NG_NODELEN); 132952419Sjulian strncpy(link->nodeinfo.type, 133052419Sjulian hook->peer->node->type->name, NG_TYPELEN); 133152722Sjulian link->nodeinfo.id = ng_node2ID(hook->peer->node); 133252419Sjulian link->nodeinfo.hooks = hook->peer->node->numhooks; 133352419Sjulian ni->hooks++; 133452419Sjulian } 133552419Sjulian *resp = rp; 133652419Sjulian break; 133752419Sjulian } 133852419Sjulian 133952419Sjulian case NGM_LISTNAMES: 134052419Sjulian case NGM_LISTNODES: 134152419Sjulian { 134252419Sjulian const int unnamed = (msg->header.cmd == NGM_LISTNODES); 134352419Sjulian struct namelist *nl; 134452419Sjulian struct ng_mesg *rp; 134552419Sjulian node_p node; 134652419Sjulian int num = 0; 134752419Sjulian 134852419Sjulian if (resp == NULL) { 134952419Sjulian error = EINVAL; 135052419Sjulian break; 135152419Sjulian } 135252419Sjulian 135352419Sjulian /* Count number of nodes */ 135452419Sjulian LIST_FOREACH(node, &nodelist, nodes) { 135552419Sjulian if (unnamed || node->name != NULL) 135652419Sjulian num++; 135752419Sjulian } 135852419Sjulian 135952419Sjulian /* Get response struct */ 136052419Sjulian if (resp == NULL) { 136152419Sjulian error = EINVAL; 136252419Sjulian break; 136352419Sjulian } 136452419Sjulian NG_MKRESPONSE(rp, msg, sizeof(*nl) 136552419Sjulian + (num * sizeof(struct nodeinfo)), M_NOWAIT); 136652419Sjulian if (rp == NULL) { 136752419Sjulian error = ENOMEM; 136852419Sjulian break; 136952419Sjulian } 137052419Sjulian nl = (struct namelist *) rp->data; 137152419Sjulian 137252419Sjulian /* Cycle through the linked list of nodes */ 137352419Sjulian nl->numnames = 0; 137452419Sjulian LIST_FOREACH(node, &nodelist, nodes) { 137552419Sjulian struct nodeinfo *const np = &nl->nodeinfo[nl->numnames]; 137652419Sjulian 137752419Sjulian if (nl->numnames >= num) { 137852419Sjulian log(LOG_ERR, "%s: number of %s changed\n", 137952419Sjulian __FUNCTION__, "nodes"); 138052419Sjulian break; 138152419Sjulian } 138252419Sjulian if ((node->flags & NG_INVALID) != 0) 138352419Sjulian continue; 138452419Sjulian if (!unnamed && node->name == NULL) 138552419Sjulian continue; 138652419Sjulian if (node->name != NULL) 138752419Sjulian strncpy(np->name, node->name, NG_NODELEN); 138852419Sjulian strncpy(np->type, node->type->name, NG_TYPELEN); 138952722Sjulian np->id = ng_node2ID(node); 139052419Sjulian np->hooks = node->numhooks; 139152419Sjulian nl->numnames++; 139252419Sjulian } 139352419Sjulian *resp = rp; 139452419Sjulian break; 139552419Sjulian } 139652419Sjulian 139752419Sjulian case NGM_LISTTYPES: 139852419Sjulian { 139952419Sjulian struct typelist *tl; 140052419Sjulian struct ng_mesg *rp; 140152419Sjulian struct ng_type *type; 140252419Sjulian int num = 0; 140352419Sjulian 140452419Sjulian if (resp == NULL) { 140552419Sjulian error = EINVAL; 140652419Sjulian break; 140752419Sjulian } 140852419Sjulian 140952419Sjulian /* Count number of types */ 141052419Sjulian LIST_FOREACH(type, &typelist, types) 141152419Sjulian num++; 141252419Sjulian 141352419Sjulian /* Get response struct */ 141452419Sjulian if (resp == NULL) { 141552419Sjulian error = EINVAL; 141652419Sjulian break; 141752419Sjulian } 141852419Sjulian NG_MKRESPONSE(rp, msg, sizeof(*tl) 141952419Sjulian + (num * sizeof(struct typeinfo)), M_NOWAIT); 142052419Sjulian if (rp == NULL) { 142152419Sjulian error = ENOMEM; 142252419Sjulian break; 142352419Sjulian } 142452419Sjulian tl = (struct typelist *) rp->data; 142552419Sjulian 142652419Sjulian /* Cycle through the linked list of types */ 142752419Sjulian tl->numtypes = 0; 142852419Sjulian LIST_FOREACH(type, &typelist, types) { 142952419Sjulian struct typeinfo *const tp = &tl->typeinfo[tl->numtypes]; 143052419Sjulian 143152419Sjulian if (tl->numtypes >= num) { 143252419Sjulian log(LOG_ERR, "%s: number of %s changed\n", 143352419Sjulian __FUNCTION__, "types"); 143452419Sjulian break; 143552419Sjulian } 143652419Sjulian strncpy(tp->typename, type->name, NG_TYPELEN); 143752419Sjulian tp->numnodes = type->refs; 143852419Sjulian tl->numtypes++; 143952419Sjulian } 144052419Sjulian *resp = rp; 144152419Sjulian break; 144252419Sjulian } 144352419Sjulian 144453913Sarchie case NGM_BINARY2ASCII: 144553913Sarchie { 144653913Sarchie int bufSize = 2000; /* XXX hard coded constant */ 144753913Sarchie const struct ng_parse_type *argstype; 144853913Sarchie const struct ng_cmdlist *c; 144953913Sarchie struct ng_mesg *rp, *binary, *ascii; 145053913Sarchie 145153913Sarchie /* Data area must contain a valid netgraph message */ 145253913Sarchie binary = (struct ng_mesg *)msg->data; 145353913Sarchie if (msg->header.arglen < sizeof(struct ng_mesg) 145453913Sarchie || msg->header.arglen - sizeof(struct ng_mesg) 145553913Sarchie < binary->header.arglen) { 145653913Sarchie error = EINVAL; 145753913Sarchie break; 145853913Sarchie } 145953913Sarchie 146053913Sarchie /* Get a response message with lots of room */ 146153913Sarchie NG_MKRESPONSE(rp, msg, sizeof(*ascii) + bufSize, M_NOWAIT); 146253913Sarchie if (rp == NULL) { 146353913Sarchie error = ENOMEM; 146453913Sarchie break; 146553913Sarchie } 146653913Sarchie ascii = (struct ng_mesg *)rp->data; 146753913Sarchie 146853913Sarchie /* Copy binary message header to response message payload */ 146953913Sarchie bcopy(binary, ascii, sizeof(*binary)); 147053913Sarchie 147153913Sarchie /* Find command by matching typecookie and command number */ 147253913Sarchie for (c = here->type->cmdlist; 147353913Sarchie c != NULL && c->name != NULL; c++) { 147453913Sarchie if (binary->header.typecookie == c->cookie 147553913Sarchie && binary->header.cmd == c->cmd) 147653913Sarchie break; 147753913Sarchie } 147853913Sarchie if (c == NULL || c->name == NULL) { 147953913Sarchie for (c = ng_generic_cmds; c->name != NULL; c++) { 148053913Sarchie if (binary->header.typecookie == c->cookie 148153913Sarchie && binary->header.cmd == c->cmd) 148253913Sarchie break; 148353913Sarchie } 148453913Sarchie if (c->name == NULL) { 148553913Sarchie FREE(rp, M_NETGRAPH); 148653913Sarchie error = ENOSYS; 148753913Sarchie break; 148853913Sarchie } 148953913Sarchie } 149053913Sarchie 149153913Sarchie /* Convert command name to ASCII */ 149253913Sarchie snprintf(ascii->header.cmdstr, sizeof(ascii->header.cmdstr), 149353913Sarchie "%s", c->name); 149453913Sarchie 149553913Sarchie /* Convert command arguments to ASCII */ 149653913Sarchie argstype = (binary->header.flags & NGF_RESP) ? 149753913Sarchie c->respType : c->mesgType; 149853913Sarchie if (argstype == NULL) 149953913Sarchie *ascii->data = '\0'; 150053913Sarchie else { 150153913Sarchie if ((error = ng_unparse(argstype, 150253913Sarchie (u_char *)binary->data, 150353913Sarchie ascii->data, bufSize)) != 0) { 150453913Sarchie FREE(rp, M_NETGRAPH); 150553913Sarchie break; 150653913Sarchie } 150753913Sarchie } 150853913Sarchie 150953913Sarchie /* Return the result as struct ng_mesg plus ASCII string */ 151053913Sarchie bufSize = strlen(ascii->data) + 1; 151153913Sarchie ascii->header.arglen = bufSize; 151253913Sarchie rp->header.arglen = sizeof(*ascii) + bufSize; 151353913Sarchie *resp = rp; 151453913Sarchie break; 151553913Sarchie } 151653913Sarchie 151753913Sarchie case NGM_ASCII2BINARY: 151853913Sarchie { 151953913Sarchie int bufSize = 2000; /* XXX hard coded constant */ 152053913Sarchie const struct ng_cmdlist *c; 152153913Sarchie const struct ng_parse_type *argstype; 152253913Sarchie struct ng_mesg *rp, *ascii, *binary; 152353913Sarchie int off; 152453913Sarchie 152553913Sarchie /* Data area must contain at least a struct ng_mesg + '\0' */ 152653913Sarchie ascii = (struct ng_mesg *)msg->data; 152753913Sarchie if (msg->header.arglen < sizeof(*ascii) + 1 152853913Sarchie || ascii->header.arglen < 1 152953913Sarchie || msg->header.arglen 153053913Sarchie < sizeof(*ascii) + ascii->header.arglen) { 153153913Sarchie error = EINVAL; 153253913Sarchie break; 153353913Sarchie } 153453913Sarchie ascii->data[ascii->header.arglen - 1] = '\0'; 153553913Sarchie 153653913Sarchie /* Get a response message with lots of room */ 153753913Sarchie NG_MKRESPONSE(rp, msg, sizeof(*binary) + bufSize, M_NOWAIT); 153853913Sarchie if (rp == NULL) { 153953913Sarchie error = ENOMEM; 154053913Sarchie break; 154153913Sarchie } 154253913Sarchie binary = (struct ng_mesg *)rp->data; 154353913Sarchie 154453913Sarchie /* Copy ASCII message header to response message payload */ 154553913Sarchie bcopy(ascii, binary, sizeof(*ascii)); 154653913Sarchie 154753913Sarchie /* Find command by matching ASCII command string */ 154853913Sarchie for (c = here->type->cmdlist; 154953913Sarchie c != NULL && c->name != NULL; c++) { 155053913Sarchie if (strcmp(ascii->header.cmdstr, c->name) == 0) 155153913Sarchie break; 155253913Sarchie } 155353913Sarchie if (c == NULL || c->name == NULL) { 155453913Sarchie for (c = ng_generic_cmds; c->name != NULL; c++) { 155553913Sarchie if (strcmp(ascii->header.cmdstr, c->name) == 0) 155653913Sarchie break; 155753913Sarchie } 155853913Sarchie if (c->name == NULL) { 155953913Sarchie FREE(rp, M_NETGRAPH); 156053913Sarchie error = ENOSYS; 156153913Sarchie break; 156253913Sarchie } 156353913Sarchie } 156453913Sarchie 156553913Sarchie /* Convert command name to binary */ 156653913Sarchie binary->header.cmd = c->cmd; 156753913Sarchie binary->header.typecookie = c->cookie; 156853913Sarchie 156953913Sarchie /* Convert command arguments to binary */ 157053913Sarchie argstype = (binary->header.flags & NGF_RESP) ? 157153913Sarchie c->respType : c->mesgType; 157253913Sarchie if (argstype == NULL) 157353913Sarchie bufSize = 0; 157453913Sarchie else { 157553913Sarchie if ((error = ng_parse(argstype, ascii->data, 157653913Sarchie &off, (u_char *)binary->data, &bufSize)) != 0) { 157753913Sarchie FREE(rp, M_NETGRAPH); 157853913Sarchie break; 157953913Sarchie } 158053913Sarchie } 158153913Sarchie 158253913Sarchie /* Return the result */ 158353913Sarchie binary->header.arglen = bufSize; 158453913Sarchie rp->header.arglen = sizeof(*binary) + bufSize; 158553913Sarchie *resp = rp; 158653913Sarchie break; 158753913Sarchie } 158853913Sarchie 158952419Sjulian case NGM_TEXT_STATUS: 159052419Sjulian /* 159152419Sjulian * This one is tricky as it passes the command down to the 159252419Sjulian * actual node, even though it is a generic type command. 159352419Sjulian * This means we must assume that the msg is already freed 159452419Sjulian * when control passes back to us. 159552419Sjulian */ 159652419Sjulian if (resp == NULL) { 159752419Sjulian error = EINVAL; 159852419Sjulian break; 159952419Sjulian } 160052419Sjulian if (here->type->rcvmsg != NULL) 160152419Sjulian return((*here->type->rcvmsg)(here, msg, retaddr, resp)); 160252419Sjulian /* Fall through if rcvmsg not supported */ 160352419Sjulian default: 160452419Sjulian TRAP_ERROR; 160552419Sjulian error = EINVAL; 160652419Sjulian } 160752419Sjulian FREE(msg, M_NETGRAPH); 160852419Sjulian return (error); 160952419Sjulian} 161052419Sjulian 161152419Sjulian/* 161252419Sjulian * Send a data packet to a node. If the recipient has no 161352419Sjulian * 'receive data' method, then silently discard the packet. 161452419Sjulian */ 161552419Sjulianint 161652419Sjulianng_send_data(hook_p hook, struct mbuf *m, meta_p meta) 161752419Sjulian{ 161852419Sjulian int (*rcvdata)(hook_p, struct mbuf *, meta_p); 161952419Sjulian int error; 162052419Sjulian 162153403Sarchie CHECK_DATA_MBUF(m); 162252419Sjulian if (hook && (hook->flags & HK_INVALID) == 0) { 162352419Sjulian rcvdata = hook->peer->node->type->rcvdata; 162452419Sjulian if (rcvdata != NULL) 162552419Sjulian error = (*rcvdata)(hook->peer, m, meta); 162652419Sjulian else { 162752419Sjulian error = 0; 162852419Sjulian NG_FREE_DATA(m, meta); 162952419Sjulian } 163052419Sjulian } else { 163152419Sjulian TRAP_ERROR; 163252419Sjulian error = ENOTCONN; 163352419Sjulian NG_FREE_DATA(m, meta); 163452419Sjulian } 163552419Sjulian return (error); 163652419Sjulian} 163752419Sjulian 163852419Sjulian/* 163952419Sjulian * Send a queued data packet to a node. If the recipient has no 164052419Sjulian * 'receive queued data' method, then try the 'receive data' method above. 164152419Sjulian */ 164252419Sjulianint 164352419Sjulianng_send_dataq(hook_p hook, struct mbuf *m, meta_p meta) 164452419Sjulian{ 164552419Sjulian int (*rcvdataq)(hook_p, struct mbuf *, meta_p); 164652419Sjulian int error; 164752419Sjulian 164853403Sarchie CHECK_DATA_MBUF(m); 164952419Sjulian if (hook && (hook->flags & HK_INVALID) == 0) { 165052419Sjulian rcvdataq = hook->peer->node->type->rcvdataq; 165152419Sjulian if (rcvdataq != NULL) 165252419Sjulian error = (*rcvdataq)(hook->peer, m, meta); 165352419Sjulian else { 165452419Sjulian error = ng_send_data(hook, m, meta); 165552419Sjulian } 165652419Sjulian } else { 165752419Sjulian TRAP_ERROR; 165852419Sjulian error = ENOTCONN; 165952419Sjulian NG_FREE_DATA(m, meta); 166052419Sjulian } 166152419Sjulian return (error); 166252419Sjulian} 166352419Sjulian 166452419Sjulian/************************************************************************ 166552419Sjulian Module routines 166652419Sjulian************************************************************************/ 166752419Sjulian 166852419Sjulian/* 166952419Sjulian * Handle the loading/unloading of a netgraph node type module 167052419Sjulian */ 167152419Sjulianint 167252419Sjulianng_mod_event(module_t mod, int event, void *data) 167352419Sjulian{ 167452419Sjulian struct ng_type *const type = data; 167552419Sjulian int s, error = 0; 167652419Sjulian 167752419Sjulian switch (event) { 167852419Sjulian case MOD_LOAD: 167952419Sjulian 168052419Sjulian /* Register new netgraph node type */ 168152419Sjulian s = splnet(); 168252419Sjulian if ((error = ng_newtype(type)) != 0) { 168352419Sjulian splx(s); 168452419Sjulian break; 168552419Sjulian } 168652419Sjulian 168752419Sjulian /* Call type specific code */ 168852419Sjulian if (type->mod_event != NULL) 168952419Sjulian if ((error = (*type->mod_event)(mod, event, data)) != 0) 169052419Sjulian LIST_REMOVE(type, types); 169152419Sjulian splx(s); 169252419Sjulian break; 169352419Sjulian 169452419Sjulian case MOD_UNLOAD: 169552419Sjulian s = splnet(); 169652419Sjulian if (type->refs != 0) /* make sure no nodes exist! */ 169752419Sjulian error = EBUSY; 169852419Sjulian else { 169952419Sjulian if (type->mod_event != NULL) { /* check with type */ 170052419Sjulian error = (*type->mod_event)(mod, event, data); 170152419Sjulian if (error != 0) { /* type refuses.. */ 170252419Sjulian splx(s); 170352419Sjulian break; 170452419Sjulian } 170552419Sjulian } 170652419Sjulian LIST_REMOVE(type, types); 170752419Sjulian } 170852419Sjulian splx(s); 170952419Sjulian break; 171052419Sjulian 171152419Sjulian default: 171252419Sjulian if (type->mod_event != NULL) 171352419Sjulian error = (*type->mod_event)(mod, event, data); 171452419Sjulian else 171552419Sjulian error = 0; /* XXX ? */ 171652419Sjulian break; 171752419Sjulian } 171852419Sjulian return (error); 171952419Sjulian} 172052419Sjulian 172152419Sjulian/* 172252419Sjulian * Handle loading and unloading for this code. 172352419Sjulian * The only thing we need to link into is the NETISR strucure. 172452419Sjulian */ 172552419Sjulianstatic int 172652419Sjulianngb_mod_event(module_t mod, int event, void *data) 172752419Sjulian{ 172852419Sjulian int s, error = 0; 172952419Sjulian 173052419Sjulian switch (event) { 173152419Sjulian case MOD_LOAD: 173252419Sjulian /* Register line discipline */ 173352419Sjulian s = splimp(); 173452419Sjulian error = register_netisr(NETISR_NETGRAPH, ngintr); 173552419Sjulian splx(s); 173652419Sjulian break; 173752419Sjulian case MOD_UNLOAD: 173852419Sjulian /* You cant unload it because an interface may be using it. */ 173952419Sjulian error = EBUSY; 174052419Sjulian break; 174152419Sjulian default: 174252419Sjulian error = EOPNOTSUPP; 174352419Sjulian break; 174452419Sjulian } 174552419Sjulian return (error); 174652419Sjulian} 174752419Sjulian 174852419Sjulianstatic moduledata_t netgraph_mod = { 174952419Sjulian "netgraph", 175052419Sjulian ngb_mod_event, 175152419Sjulian (NULL) 175252419Sjulian}; 175352419SjulianDECLARE_MODULE(netgraph, netgraph_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE); 175452419Sjulian 175552419Sjulian/************************************************************************ 175652419Sjulian Queueing routines 175752419Sjulian************************************************************************/ 175852419Sjulian 175952419Sjulian/* The structure for queueing across ISR switches */ 176052419Sjulianstruct ng_queue_entry { 176152419Sjulian u_long flags; 176252419Sjulian struct ng_queue_entry *next; 176352419Sjulian union { 176452419Sjulian struct { 176552419Sjulian hook_p da_hook; /* target hook */ 176652419Sjulian struct mbuf *da_m; 176752419Sjulian meta_p da_meta; 176852419Sjulian } data; 176952419Sjulian struct { 177052419Sjulian struct ng_mesg *msg_msg; 177152419Sjulian node_p msg_node; 177252419Sjulian void *msg_retaddr; 177352419Sjulian } msg; 177452419Sjulian } body; 177552419Sjulian}; 177652419Sjulian#define NGQF_DATA 0x01 /* the queue element is data */ 177752419Sjulian#define NGQF_MESG 0x02 /* the queue element is a message */ 177852419Sjulian 177952419Sjulianstatic struct ng_queue_entry *ngqbase; /* items to be unqueued */ 178052419Sjulianstatic struct ng_queue_entry *ngqlast; /* last item queued */ 178152419Sjulianstatic const int ngqroom = 64; /* max items to queue */ 178252419Sjulianstatic int ngqsize; /* number of items in queue */ 178352419Sjulian 178452419Sjulianstatic struct ng_queue_entry *ngqfree; /* free ones */ 178552419Sjulianstatic const int ngqfreemax = 16;/* cache at most this many */ 178652419Sjulianstatic int ngqfreesize; /* number of cached entries */ 178752419Sjulian 178852419Sjulian/* 178952419Sjulian * Get a queue entry 179052419Sjulian */ 179152419Sjulianstatic struct ng_queue_entry * 179252419Sjulianng_getqblk(void) 179352419Sjulian{ 179452419Sjulian register struct ng_queue_entry *q; 179552419Sjulian int s; 179652419Sjulian 179752419Sjulian /* Could be guarding against tty ints or whatever */ 179852419Sjulian s = splhigh(); 179952419Sjulian 180052419Sjulian /* Try get a cached queue block, or else allocate a new one */ 180152419Sjulian if ((q = ngqfree) == NULL) { 180252419Sjulian splx(s); 180352419Sjulian if (ngqsize < ngqroom) { /* don't worry about races */ 180452419Sjulian MALLOC(q, struct ng_queue_entry *, 180552419Sjulian sizeof(*q), M_NETGRAPH, M_NOWAIT); 180652419Sjulian } 180752419Sjulian } else { 180852419Sjulian ngqfree = q->next; 180952419Sjulian ngqfreesize--; 181052419Sjulian splx(s); 181152419Sjulian } 181252419Sjulian return (q); 181352419Sjulian} 181452419Sjulian 181552419Sjulian/* 181652419Sjulian * Release a queue entry 181752419Sjulian */ 181852419Sjulian#define RETURN_QBLK(q) \ 181952419Sjuliando { \ 182052419Sjulian int s; \ 182152419Sjulian if (ngqfreesize < ngqfreemax) { /* don't worry about races */ \ 182252419Sjulian s = splhigh(); \ 182352419Sjulian (q)->next = ngqfree; \ 182452419Sjulian ngqfree = (q); \ 182552419Sjulian ngqfreesize++; \ 182652419Sjulian splx(s); \ 182752419Sjulian } else { \ 182852419Sjulian FREE((q), M_NETGRAPH); \ 182952419Sjulian } \ 183052419Sjulian} while (0) 183152419Sjulian 183252419Sjulian/* 183352419Sjulian * Running at a raised (but we don't know which) processor priority level, 183452419Sjulian * put the data onto a queue to be picked up by another PPL (probably splnet) 183552419Sjulian */ 183652419Sjulianint 183752419Sjulianng_queue_data(hook_p hook, struct mbuf *m, meta_p meta) 183852419Sjulian{ 183952419Sjulian struct ng_queue_entry *q; 184052419Sjulian int s; 184152419Sjulian 184252419Sjulian if (hook == NULL) { 184352419Sjulian NG_FREE_DATA(m, meta); 184452419Sjulian return (0); 184552419Sjulian } 184652419Sjulian if ((q = ng_getqblk()) == NULL) { 184752419Sjulian NG_FREE_DATA(m, meta); 184852419Sjulian return (ENOBUFS); 184952419Sjulian } 185052419Sjulian 185152419Sjulian /* Fill out the contents */ 185252419Sjulian q->flags = NGQF_DATA; 185352419Sjulian q->next = NULL; 185452419Sjulian q->body.data.da_hook = hook; 185552419Sjulian q->body.data.da_m = m; 185652419Sjulian q->body.data.da_meta = meta; 185752419Sjulian hook->refs++; /* don't let it go away while on the queue */ 185852419Sjulian 185952419Sjulian /* Put it on the queue */ 186052419Sjulian s = splhigh(); 186152419Sjulian if (ngqbase) { 186252419Sjulian ngqlast->next = q; 186352419Sjulian } else { 186452419Sjulian ngqbase = q; 186552419Sjulian } 186652419Sjulian ngqlast = q; 186752419Sjulian ngqsize++; 186852419Sjulian splx(s); 186952419Sjulian 187052419Sjulian /* Schedule software interrupt to handle it later */ 187152419Sjulian schednetisr(NETISR_NETGRAPH); 187252419Sjulian return (0); 187352419Sjulian} 187452419Sjulian 187552419Sjulian/* 187652419Sjulian * Running at a raised (but we don't know which) processor priority level, 187752419Sjulian * put the msg onto a queue to be picked up by another PPL (probably splnet) 187852419Sjulian */ 187952419Sjulianint 188052419Sjulianng_queue_msg(node_p here, struct ng_mesg * msg, int len, const char *address) 188152419Sjulian{ 188252419Sjulian register struct ng_queue_entry *q; 188352419Sjulian int s; 188452419Sjulian node_p dest = NULL; 188552419Sjulian char *retaddr = NULL; 188652419Sjulian int error; 188752419Sjulian 188852722Sjulian /* Find the target node. */ 188952419Sjulian error = ng_path2node(here, address, &dest, &retaddr); 189052419Sjulian if (error) { 189152419Sjulian FREE(msg, M_NETGRAPH); 189252419Sjulian return (error); 189352419Sjulian } 189452419Sjulian if ((q = ng_getqblk()) == NULL) { 189552419Sjulian FREE(msg, M_NETGRAPH); 189652419Sjulian if (retaddr) 189752419Sjulian FREE(retaddr, M_NETGRAPH); 189852419Sjulian return (ENOBUFS); 189952419Sjulian } 190052419Sjulian 190152419Sjulian /* Fill out the contents */ 190252419Sjulian q->flags = NGQF_MESG; 190352419Sjulian q->next = NULL; 190452419Sjulian q->body.msg.msg_node = dest; 190552419Sjulian q->body.msg.msg_msg = msg; 190652419Sjulian q->body.msg.msg_retaddr = retaddr; 190752419Sjulian dest->refs++; /* don't let it go away while on the queue */ 190852419Sjulian 190952419Sjulian /* Put it on the queue */ 191052419Sjulian s = splhigh(); 191152419Sjulian if (ngqbase) { 191252419Sjulian ngqlast->next = q; 191352419Sjulian } else { 191452419Sjulian ngqbase = q; 191552419Sjulian } 191652419Sjulian ngqlast = q; 191752419Sjulian ngqsize++; 191852419Sjulian splx(s); 191952419Sjulian 192052419Sjulian /* Schedule software interrupt to handle it later */ 192152419Sjulian schednetisr(NETISR_NETGRAPH); 192252419Sjulian return (0); 192352419Sjulian} 192452419Sjulian 192552419Sjulian/* 192652419Sjulian * Pick an item off the queue, process it, and dispose of the queue entry. 192752419Sjulian * Should be running at splnet. 192852419Sjulian */ 192952419Sjulianstatic void 193052419Sjulianngintr(void) 193152419Sjulian{ 193252419Sjulian hook_p hook; 193352419Sjulian struct ng_queue_entry *ngq; 193452419Sjulian struct mbuf *m; 193552419Sjulian meta_p meta; 193652419Sjulian void *retaddr; 193752419Sjulian struct ng_mesg *msg; 193852419Sjulian node_p node; 193952419Sjulian int error = 0; 194052419Sjulian int s; 194152419Sjulian 194252419Sjulian while (1) { 194352419Sjulian s = splhigh(); 194452419Sjulian if ((ngq = ngqbase)) { 194552419Sjulian ngqbase = ngq->next; 194652419Sjulian ngqsize--; 194752419Sjulian } 194852419Sjulian splx(s); 194952419Sjulian if (ngq == NULL) 195052419Sjulian return; 195152419Sjulian switch (ngq->flags) { 195252419Sjulian case NGQF_DATA: 195352419Sjulian hook = ngq->body.data.da_hook; 195452419Sjulian m = ngq->body.data.da_m; 195552419Sjulian meta = ngq->body.data.da_meta; 195652419Sjulian RETURN_QBLK(ngq); 195752419Sjulian NG_SEND_DATAQ(error, hook, m, meta); 195852419Sjulian ng_unref_hook(hook); 195952419Sjulian break; 196052419Sjulian case NGQF_MESG: 196152419Sjulian node = ngq->body.msg.msg_node; 196252419Sjulian msg = ngq->body.msg.msg_msg; 196352419Sjulian retaddr = ngq->body.msg.msg_retaddr; 196452419Sjulian RETURN_QBLK(ngq); 196552419Sjulian if (node->flags & NG_INVALID) { 196652419Sjulian FREE(msg, M_NETGRAPH); 196752419Sjulian } else { 196852419Sjulian CALL_MSG_HANDLER(error, node, msg, 196952419Sjulian retaddr, NULL); 197052419Sjulian } 197152419Sjulian ng_unref(node); 197252419Sjulian if (retaddr) 197352419Sjulian FREE(retaddr, M_NETGRAPH); 197452419Sjulian break; 197552419Sjulian default: 197652419Sjulian RETURN_QBLK(ngq); 197752419Sjulian } 197852419Sjulian } 197952419Sjulian} 198052419Sjulian 198152419Sjulian 1982