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