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