1 /* 2 * ng_socket.c 3 */ 4 5 /*- 6 * Copyright (c) 1996-1999 Whistle Communications, Inc. 7 * All rights reserved. 8 * 9 * Subject to the following obligations and disclaimer of warranty, use and 10 * redistribution of this software, in source or object code forms, with or 11 * without modifications are expressly permitted by Whistle Communications; 12 * provided, however, that: 13 * 1. Any and all reproductions of the source or object code must include the 14 * copyright notice above and the following disclaimer of warranties; and 15 * 2. No rights are granted, in any manner or form, to use Whistle 16 * Communications, Inc. trademarks, including the mark "WHISTLE 17 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as 18 * such appears in the above copyright notice or in the software. 19 * 20 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND 21 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO 22 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, 23 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF 24 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. 25 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY 26 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS 27 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. 28 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES 29 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING 30 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 31 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR 32 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY 33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 35 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY 36 * OF SUCH DAMAGE. 37 * 38 * Author: Julian Elischer <julian@freebsd.org> 39 * 40 * $FreeBSD$ 41 * $Whistle: ng_socket.c,v 1.28 1999/11/01 09:24:52 julian Exp $ 42 */ 43 44 /* 45 * Netgraph socket nodes 46 * 47 * There are two types of netgraph sockets, control and data. 48 * Control sockets have a netgraph node, but data sockets are 49 * parasitic on control sockets, and have no node of their own. 50 */ 51 52 #include <sys/param.h> 53 #include <sys/domain.h> 54 #include <sys/errno.h> 55 #include <sys/kdb.h> 56 #include <sys/kernel.h> 57 #include <sys/linker.h> 58 #include <sys/lock.h> 59 #include <sys/malloc.h> 60 #include <sys/mbuf.h> 61 #include <sys/mutex.h> 62 #include <sys/protosw.h> 63 #include <sys/queue.h> 64 #include <sys/signalvar.h> 65 #include <sys/socket.h> 66 #include <sys/socketvar.h> 67 #include <sys/sx.h> 68 #include <sys/sysctl.h> 69 #include <sys/systm.h> 70 #ifdef NOTYET 71 #include <sys/vnode.h> 72 #endif 73 #include <netgraph/ng_message.h> 74 #include <netgraph/netgraph.h> 75 #include <netgraph/ng_socketvar.h> 76 #include <netgraph/ng_socket.h> 77 78 #ifdef NG_SEPARATE_MALLOC 79 MALLOC_DEFINE(M_NETGRAPH_PATH, "netgraph_path", "netgraph path info "); 80 MALLOC_DEFINE(M_NETGRAPH_SOCK, "netgraph_sock", "netgraph socket info "); 81 #else 82 #define M_NETGRAPH_PATH M_NETGRAPH 83 #define M_NETGRAPH_SOCK M_NETGRAPH 84 #endif 85 86 /* 87 * It's Ascii-art time! 88 * +-------------+ +-------------+ 89 * |socket (ctl)| |socket (data)| 90 * +-------------+ +-------------+ 91 * ^ ^ 92 * | | 93 * v v 94 * +-----------+ +-----------+ 95 * |pcb (ctl)| |pcb (data)| 96 * +-----------+ +-----------+ 97 * ^ ^ 98 * | | 99 * v v 100 * +--------------------------+ 101 * | Socket type private | 102 * | data | 103 * +--------------------------+ 104 * ^ 105 * | 106 * v 107 * +----------------+ 108 * | struct ng_node | 109 * +----------------+ 110 */ 111 112 /* Netgraph node methods */ 113 static ng_constructor_t ngs_constructor; 114 static ng_rcvmsg_t ngs_rcvmsg; 115 static ng_shutdown_t ngs_shutdown; 116 static ng_newhook_t ngs_newhook; 117 static ng_connect_t ngs_connect; 118 static ng_rcvdata_t ngs_rcvdata; 119 static ng_disconnect_t ngs_disconnect; 120 121 /* Internal methods */ 122 static int ng_attach_data(struct socket *so); 123 static int ng_attach_cntl(struct socket *so); 124 static int ng_attach_common(struct socket *so, int type); 125 static void ng_detach_common(node_p node, hook_p hook, void *arg1, int which); 126 /*static int ng_internalize(struct mbuf *m, struct thread *p); */ 127 128 static int ng_connect_data(struct sockaddr *nam, struct ngpcb *pcbp); 129 static int ng_bind(struct sockaddr *nam, struct ngpcb *pcbp); 130 131 static int ngs_mod_event(module_t mod, int event, void *data); 132 static int ship_msg(struct ngpcb *pcbp, struct ng_mesg *msg, 133 struct sockaddr_ng *addr); 134 135 /* Netgraph type descriptor */ 136 static struct ng_type typestruct = { 137 .version = NG_ABI_VERSION, 138 .name = NG_SOCKET_NODE_TYPE, 139 .mod_event = ngs_mod_event, 140 .constructor = ngs_constructor, 141 .rcvmsg = ngs_rcvmsg, 142 .shutdown = ngs_shutdown, 143 .newhook = ngs_newhook, 144 .connect = ngs_connect, 145 .rcvdata = ngs_rcvdata, 146 .disconnect = ngs_disconnect, 147 }; 148 NETGRAPH_INIT_ORDERED(socket, &typestruct, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY); 149 150 /* Buffer space */ 151 static u_long ngpdg_sendspace = 20 * 1024; /* really max datagram size */ 152 SYSCTL_INT(_net_graph, OID_AUTO, maxdgram, CTLFLAG_RW, 153 &ngpdg_sendspace , 0, "Maximum outgoing Netgraph datagram size"); 154 static u_long ngpdg_recvspace = 20 * 1024; 155 SYSCTL_INT(_net_graph, OID_AUTO, recvspace, CTLFLAG_RW, 156 &ngpdg_recvspace , 0, "Maximum space for incoming Netgraph datagrams"); 157 158 /* List of all sockets */ 159 static LIST_HEAD(, ngpcb) ngsocklist; 160 161 static struct mtx ngsocketlist_mtx; 162 163 #define sotongpcb(so) ((struct ngpcb *)(so)->so_pcb) 164 165 /* If getting unexplained errors returned, set this to "kdb_enter("X"); */ 166 #ifndef TRAP_ERROR 167 #define TRAP_ERROR 168 #endif 169 170 /*************************************************************** 171 Control sockets 172 ***************************************************************/ 173 174 static int 175 ngc_attach(struct socket *so, int proto, struct thread *td) 176 { 177 struct ngpcb *const pcbp = sotongpcb(so); 178 179 if (suser(td)) 180 return (EPERM); 181 if (pcbp != NULL) 182 return (EISCONN); 183 return (ng_attach_cntl(so)); 184 } 185 186 static int 187 ngc_detach(struct socket *so) 188 { 189 struct ngpcb *const pcbp = sotongpcb(so); 190 191 if (pcbp == NULL) 192 return (EINVAL); 193 194 /* 195 * If there is a node, then obtain netgraph locking first. 196 */ 197 if (pcbp->sockdata != NULL) 198 ng_send_fn1(pcbp->sockdata->node, NULL, &ng_detach_common, 199 pcbp, NG_CONTROL, NG_WAITOK); 200 else 201 ng_detach_common(NULL, NULL, pcbp, NG_CONTROL); 202 203 return (0); 204 } 205 206 static int 207 ngc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, 208 struct mbuf *control, struct thread *td) 209 { 210 struct ngpcb *const pcbp = sotongpcb(so); 211 struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr; 212 struct ng_mesg *msg; 213 struct mbuf *m0; 214 item_p item; 215 char *path = NULL; 216 int len, error = 0; 217 218 if (pcbp == NULL) { 219 error = EINVAL; 220 goto release; 221 } 222 #ifdef NOTYET 223 if (control && (error = ng_internalize(control, td))) { 224 if (pcbp->sockdata == NULL) { 225 error = ENOTCONN; 226 goto release; 227 } 228 } 229 #else /* NOTYET */ 230 if (control) { 231 error = EINVAL; 232 goto release; 233 } 234 #endif /* NOTYET */ 235 236 /* Require destination as there may be >= 1 hooks on this node */ 237 if (addr == NULL) { 238 error = EDESTADDRREQ; 239 goto release; 240 } 241 242 /* Allocate an expendable buffer for the path, chop off 243 * the sockaddr header, and make sure it's NUL terminated */ 244 len = sap->sg_len - 2; 245 MALLOC(path, char *, len + 1, M_NETGRAPH_PATH, M_WAITOK); 246 if (path == NULL) { 247 error = ENOMEM; 248 goto release; 249 } 250 bcopy(sap->sg_data, path, len); 251 path[len] = '\0'; 252 253 /* Move the actual message out of mbufs into a linear buffer. 254 * Start by adding up the size of the data. (could use mh_len?) */ 255 for (len = 0, m0 = m; m0 != NULL; m0 = m0->m_next) 256 len += m0->m_len; 257 258 /* Move the data into a linear buffer as well. Messages are not 259 * delivered in mbufs. */ 260 MALLOC(msg, struct ng_mesg *, len + 1, M_NETGRAPH_MSG, M_WAITOK); 261 if (msg == NULL) { 262 error = ENOMEM; 263 goto release; 264 } 265 m_copydata(m, 0, len, (char *)msg); 266 267 if (msg->header.version != NG_VERSION) { 268 error = EINVAL; 269 goto release; 270 } 271 272 /* 273 * Hack alert! 274 * We look into the message and if it mkpeers a node of unknown type, we 275 * try to load it. We need to do this now, in syscall thread, because if 276 * message gets queued and applied later we will get panic. 277 */ 278 if (msg->header.typecookie == NGM_GENERIC_COOKIE && 279 msg->header.cmd == NGM_MKPEER) { 280 struct ngm_mkpeer *const mkp = (struct ngm_mkpeer *) msg->data; 281 struct ng_type *type; 282 283 if ((type = ng_findtype(mkp->type)) == NULL) { 284 char filename[NG_TYPESIZ + 3]; 285 linker_file_t lf; 286 287 /* Not found, try to load it as a loadable module. */ 288 snprintf(filename, sizeof(filename), "ng_%s", 289 mkp->type); 290 mtx_lock(&Giant); 291 error = linker_load_module(NULL, filename, NULL, NULL, 292 &lf); 293 mtx_unlock(&Giant); 294 if (error != 0) { 295 FREE(msg, M_NETGRAPH_MSG); 296 goto release; 297 } 298 lf->userrefs++; 299 300 /* See if type has been loaded successfully. */ 301 if ((type = ng_findtype(mkp->type)) == NULL) { 302 FREE(msg, M_NETGRAPH_MSG); 303 error = ENXIO; 304 goto release; 305 } 306 } 307 } 308 309 if ((item = ng_package_msg(msg, M_WAITOK)) == NULL) { 310 error = ENOMEM; 311 #ifdef TRACE_MESSAGES 312 printf("ng_package_msg: err=%d\n", error); 313 #endif 314 goto release; 315 } 316 if ((error = ng_address_path((pcbp->sockdata->node), item, 317 path, 0)) != 0) { 318 #ifdef TRACE_MESSAGES 319 printf("ng_address_path: errx=%d\n", error); 320 #endif 321 goto release; 322 } 323 324 #ifdef TRACE_MESSAGES 325 printf("[%x]:<---------[socket]: c=<%d>cmd=%x(%s) f=%x #%d (%s)\n", 326 item->el_dest->nd_ID, 327 msg->header.typecookie, 328 msg->header.cmd, 329 msg->header.cmdstr, 330 msg->header.flags, 331 msg->header.token, 332 item->el_dest->nd_type->name); 333 #endif 334 SAVE_LINE(item); 335 error = ng_snd_item(item, NG_NOFLAGS); 336 337 release: 338 if (path != NULL) 339 FREE(path, M_NETGRAPH_PATH); 340 if (control != NULL) 341 m_freem(control); 342 if (m != NULL) 343 m_freem(m); 344 return (error); 345 } 346 347 static int 348 ngc_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 349 { 350 struct ngpcb *const pcbp = sotongpcb(so); 351 352 if (pcbp == 0) 353 return (EINVAL); 354 return (ng_bind(nam, pcbp)); 355 } 356 357 static int 358 ngc_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 359 { 360 printf(" program tried to connect control socket to remote node\n "); 361 /* 362 * At this time refuse to do this.. it used to 363 * do something but it was undocumented and not used. 364 */ 365 return (EINVAL); 366 } 367 368 /*************************************************************** 369 Data sockets 370 ***************************************************************/ 371 372 static int 373 ngd_attach(struct socket *so, int proto, struct thread *td) 374 { 375 struct ngpcb *const pcbp = sotongpcb(so); 376 377 if (pcbp != NULL) 378 return (EISCONN); 379 return (ng_attach_data(so)); 380 } 381 382 static int 383 ngd_detach(struct socket *so) 384 { 385 struct ngpcb *const pcbp = sotongpcb(so); 386 387 if (pcbp == NULL) 388 return (EINVAL); 389 390 /* 391 * If there is a node, then obtain netgraph locking first. 392 */ 393 if (pcbp->sockdata != NULL) 394 ng_send_fn1(pcbp->sockdata->node, NULL, &ng_detach_common, 395 pcbp, NG_DATA, NG_WAITOK); 396 else 397 ng_detach_common(NULL, NULL, pcbp, NG_DATA); 398 399 return (0); 400 } 401 402 static int 403 ngd_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, 404 struct mbuf *control, struct thread *td) 405 { 406 struct ngpcb *const pcbp = sotongpcb(so); 407 struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr; 408 int len, error; 409 hook_p hook = NULL; 410 char hookname[NG_HOOKSIZ]; 411 412 if ((pcbp == NULL) || (control != NULL)) { 413 error = EINVAL; 414 goto release; 415 } 416 if (pcbp->sockdata == NULL) { 417 error = ENOTCONN; 418 goto release; 419 } 420 /* 421 * If the user used any of these ways to not specify an address 422 * then handle specially. 423 */ 424 if ((sap == NULL) 425 || ((len = sap->sg_len - 2) <= 0) 426 || (*sap->sg_data == '\0')) { 427 if (NG_NODE_NUMHOOKS(pcbp->sockdata->node) != 1) { 428 error = EDESTADDRREQ; 429 goto release; 430 } 431 /* 432 * if exactly one hook exists, just use it. 433 * Special case to allow write(2) to work on an ng_socket. 434 */ 435 hook = LIST_FIRST(&pcbp->sockdata->node->nd_hooks); 436 } else { 437 if (len >= NG_HOOKSIZ) { 438 error = EINVAL; 439 goto release; 440 } 441 442 /* 443 * chop off the sockaddr header, and make sure it's NUL 444 * terminated 445 */ 446 bcopy(sap->sg_data, hookname, len); 447 hookname[len] = '\0'; 448 449 /* Find the correct hook from 'hookname' */ 450 LIST_FOREACH(hook, &pcbp->sockdata->node->nd_hooks, hk_hooks) { 451 if (strcmp(hookname, NG_HOOK_NAME(hook)) == 0) { 452 break; 453 } 454 } 455 if (hook == NULL) { 456 error = EHOSTUNREACH; 457 } 458 } 459 460 /* Send data (OK if hook is NULL) */ 461 NG_SEND_DATA_ONLY(error, hook, m); /* makes m NULL */ 462 463 release: 464 if (control != NULL) 465 m_freem(control); 466 if (m != NULL) 467 m_freem(m); 468 return (error); 469 } 470 471 static int 472 ngd_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 473 { 474 struct ngpcb *const pcbp = sotongpcb(so); 475 476 if (pcbp == 0) 477 return (EINVAL); 478 return (ng_connect_data(nam, pcbp)); 479 } 480 481 /* 482 * Used for both data and control sockets 483 */ 484 static int 485 ng_setsockaddr(struct socket *so, struct sockaddr **addr) 486 { 487 struct ngpcb *pcbp; 488 struct sockaddr_ng *sg; 489 int sg_len, namelen, s; 490 491 /* Why isn't sg_data a `char[1]' ? :-( */ 492 sg_len = sizeof(struct sockaddr_ng) - sizeof(sg->sg_data) + 1; 493 494 s = splnet(); 495 pcbp = sotongpcb(so); 496 if ((pcbp == NULL) || (pcbp->sockdata == NULL)) { 497 splx(s); 498 return (EINVAL); 499 } 500 501 namelen = 0; /* silence compiler ! */ 502 if ( NG_NODE_HAS_NAME(pcbp->sockdata->node)) 503 sg_len += namelen = strlen(NG_NODE_NAME(pcbp->sockdata->node)); 504 505 MALLOC(sg, struct sockaddr_ng *, sg_len, M_SONAME, M_WAITOK | M_ZERO); 506 507 if (NG_NODE_HAS_NAME(pcbp->sockdata->node)) 508 bcopy(NG_NODE_NAME(pcbp->sockdata->node), sg->sg_data, namelen); 509 splx(s); 510 511 sg->sg_len = sg_len; 512 sg->sg_family = AF_NETGRAPH; 513 *addr = (struct sockaddr *)sg; 514 515 return (0); 516 } 517 518 /* 519 * Attach a socket to it's protocol specific partner. 520 * For a control socket, actually create a netgraph node and attach 521 * to it as well. 522 */ 523 524 static int 525 ng_attach_cntl(struct socket *so) 526 { 527 struct ngsock *privdata; 528 struct ngpcb *pcbp; 529 int error; 530 531 /* Setup protocol control block */ 532 if ((error = ng_attach_common(so, NG_CONTROL)) != 0) 533 return (error); 534 pcbp = sotongpcb(so); 535 536 /* Allocate node private info */ 537 MALLOC(privdata, struct ngsock *, 538 sizeof(*privdata), M_NETGRAPH_SOCK, M_WAITOK | M_ZERO); 539 if (privdata == NULL) { 540 ng_detach_common(NULL, NULL, pcbp, NG_CONTROL); 541 return (ENOMEM); 542 } 543 544 /* Make the generic node components */ 545 if ((error = ng_make_node_common(&typestruct, &privdata->node)) != 0) { 546 FREE(privdata, M_NETGRAPH_SOCK); 547 ng_detach_common(NULL, NULL, pcbp, NG_CONTROL); 548 return (error); 549 } 550 NG_NODE_SET_PRIVATE(privdata->node, privdata); 551 552 /* Link the pcb and the node private data */ 553 privdata->ctlsock = pcbp; 554 pcbp->sockdata = privdata; 555 privdata->refs++; 556 return (0); 557 } 558 559 static int 560 ng_attach_data(struct socket *so) 561 { 562 return(ng_attach_common(so, NG_DATA)); 563 } 564 565 /* 566 * Set up a socket protocol control block. 567 * This code is shared between control and data sockets. 568 */ 569 static int 570 ng_attach_common(struct socket *so, int type) 571 { 572 struct ngpcb *pcbp; 573 int error; 574 575 /* Standard socket setup stuff */ 576 error = soreserve(so, ngpdg_sendspace, ngpdg_recvspace); 577 if (error) 578 return (error); 579 580 /* Allocate the pcb */ 581 MALLOC(pcbp, struct ngpcb *, sizeof(*pcbp), M_PCB, M_WAITOK | M_ZERO); 582 if (pcbp == NULL) 583 return (ENOMEM); 584 pcbp->type = type; 585 586 /* Link the pcb and the socket */ 587 so->so_pcb = (caddr_t) pcbp; 588 pcbp->ng_socket = so; 589 590 /* Add the socket to linked list */ 591 mtx_lock(&ngsocketlist_mtx); 592 LIST_INSERT_HEAD(&ngsocklist, pcbp, socks); 593 mtx_unlock(&ngsocketlist_mtx); 594 return (0); 595 } 596 597 /* 598 * Disassociate the socket from it's protocol specific 599 * partner. If it's attached to a node's private data structure, 600 * then unlink from that too. If we were the last socket attached to it, 601 * then shut down the entire node. Shared code for control and data sockets. 602 */ 603 static void 604 ng_detach_common(node_p node, hook_p hook, void *arg1, int which) 605 { 606 struct ngpcb *pcbp = arg1; 607 608 if (pcbp->sockdata) { 609 struct ngsock *priv; 610 611 priv = pcbp->sockdata; 612 pcbp->sockdata = NULL; 613 switch (which) { 614 case NG_CONTROL: 615 priv->ctlsock = NULL; 616 break; 617 case NG_DATA: 618 priv->datasock = NULL; 619 break; 620 default: 621 panic(__func__); 622 } 623 if ((--priv->refs == 0) && (priv->node != NULL)) 624 ng_rmnode_self(priv->node); 625 } 626 pcbp->ng_socket->so_pcb = NULL; 627 pcbp->ng_socket = NULL; 628 mtx_lock(&ngsocketlist_mtx); 629 LIST_REMOVE(pcbp, socks); 630 mtx_unlock(&ngsocketlist_mtx); 631 FREE(pcbp, M_PCB); 632 } 633 634 #ifdef NOTYET 635 /* 636 * File descriptors can be passed into an AF_NETGRAPH socket. 637 * Note, that file descriptors cannot be passed OUT. 638 * Only character device descriptors are accepted. 639 * Character devices are useful to connect a graph to a device, 640 * which after all is the purpose of this whole system. 641 */ 642 static int 643 ng_internalize(struct mbuf *control, struct thread *td) 644 { 645 const struct cmsghdr *cm = mtod(control, const struct cmsghdr *); 646 struct file *fp; 647 struct vnode *vn; 648 int oldfds; 649 int fd; 650 651 if (cm->cmsg_type != SCM_RIGHTS || cm->cmsg_level != SOL_SOCKET || 652 cm->cmsg_len != control->m_len) { 653 TRAP_ERROR; 654 return (EINVAL); 655 } 656 657 /* Check there is only one FD. XXX what would more than one signify? */ 658 oldfds = ((caddr_t)cm + cm->cmsg_len - (caddr_t)data) / sizeof (int); 659 if (oldfds != 1) { 660 TRAP_ERROR; 661 return (EINVAL); 662 } 663 664 /* Check that the FD given is legit. and change it to a pointer to a 665 * struct file. */ 666 fd = CMSG_DATA(cm); 667 if ((error = fget(td, fd, &fp)) != 0) 668 return (error); 669 670 /* Depending on what kind of resource it is, act differently. For 671 * devices, we treat it as a file. For an AF_NETGRAPH socket, 672 * shortcut straight to the node. */ 673 switch (fp->f_type) { 674 case DTYPE_VNODE: 675 vn = fp->f_data; 676 if (vn && (vn->v_type == VCHR)) { 677 /* for a VCHR, actually reference the FILE */ 678 fp->f_count++; 679 /* XXX then what :) */ 680 /* how to pass on to other modules? */ 681 } else { 682 fdrop(fp, td); 683 TRAP_ERROR; 684 return (EINVAL); 685 } 686 break; 687 default: 688 fdrop(fp, td); 689 TRAP_ERROR; 690 return (EINVAL); 691 } 692 fdrop(fp, td); 693 return (0); 694 } 695 #endif /* NOTYET */ 696 697 /* 698 * Connect the data socket to a named control socket node. 699 */ 700 static int 701 ng_connect_data(struct sockaddr *nam, struct ngpcb *pcbp) 702 { 703 struct sockaddr_ng *sap; 704 node_p farnode; 705 struct ngsock *priv; 706 int error; 707 item_p item; 708 709 /* If we are already connected, don't do it again */ 710 if (pcbp->sockdata != NULL) 711 return (EISCONN); 712 713 /* Find the target (victim) and check it doesn't already have a data 714 * socket. Also check it is a 'socket' type node. 715 * Use ng_package_data() and address_path() to do this. 716 */ 717 718 sap = (struct sockaddr_ng *) nam; 719 /* The item will hold the node reference */ 720 item = ng_package_data(NULL, NG_WAITOK); 721 if (item == NULL) { 722 return (ENOMEM); 723 } 724 if ((error = ng_address_path(NULL, item, sap->sg_data, 0))) 725 return (error); /* item is freed on failure */ 726 727 /* 728 * Extract node from item and free item. Remember we now have 729 * a reference on the node. The item holds it for us. 730 * when we free the item we release the reference. 731 */ 732 farnode = item->el_dest; /* shortcut */ 733 if (strcmp(farnode->nd_type->name, NG_SOCKET_NODE_TYPE) != 0) { 734 NG_FREE_ITEM(item); /* drop the reference to the node */ 735 return (EINVAL); 736 } 737 priv = NG_NODE_PRIVATE(farnode); 738 if (priv->datasock != NULL) { 739 NG_FREE_ITEM(item); /* drop the reference to the node */ 740 return (EADDRINUSE); 741 } 742 743 /* 744 * Link the PCB and the private data struct. and note the extra 745 * reference. Drop the extra reference on the node. 746 */ 747 priv->datasock = pcbp; 748 pcbp->sockdata = priv; 749 priv->refs++; /* XXX possible race if it's being freed */ 750 NG_FREE_ITEM(item); /* drop the reference to the node */ 751 return (0); 752 } 753 754 /* 755 * Binding a socket means giving the corresponding node a name 756 */ 757 static int 758 ng_bind(struct sockaddr *nam, struct ngpcb *pcbp) 759 { 760 struct ngsock *const priv = pcbp->sockdata; 761 struct sockaddr_ng *const sap = (struct sockaddr_ng *) nam; 762 763 if (priv == NULL) { 764 TRAP_ERROR; 765 return (EINVAL); 766 } 767 if ((sap->sg_len < 4) 768 || (sap->sg_len > (NG_NODESIZ + 2)) 769 || (sap->sg_data[0] == '\0') 770 || (sap->sg_data[sap->sg_len - 3] != '\0')) { 771 TRAP_ERROR; 772 return (EINVAL); 773 } 774 return (ng_name_node(priv->node, sap->sg_data)); 775 } 776 777 /* 778 * Take a message and pass it up to the control socket associated 779 * with the node. 780 */ 781 static int 782 ship_msg(struct ngpcb *pcbp, struct ng_mesg *msg, struct sockaddr_ng *addr) 783 { 784 struct socket *const so = pcbp->ng_socket; 785 struct mbuf *mdata; 786 int msglen; 787 788 /* Copy the message itself into an mbuf chain */ 789 msglen = sizeof(struct ng_mesg) + msg->header.arglen; 790 mdata = m_devget((caddr_t) msg, msglen, 0, NULL, NULL); 791 792 /* Here we free the message, as we are the end of the line. 793 * We need to do that regardless of whether we got mbufs. */ 794 NG_FREE_MSG(msg); 795 796 if (mdata == NULL) { 797 TRAP_ERROR; 798 return (ENOBUFS); 799 } 800 801 /* Send it up to the socket */ 802 if (sbappendaddr(&so->so_rcv, 803 (struct sockaddr *) addr, mdata, NULL) == 0) { 804 TRAP_ERROR; 805 m_freem(mdata); 806 return (ENOBUFS); 807 } 808 sorwakeup(so); 809 return (0); 810 } 811 812 /* 813 * You can only create new nodes from the socket end of things. 814 */ 815 static int 816 ngs_constructor(node_p nodep) 817 { 818 return (EINVAL); 819 } 820 821 /* 822 * We allow any hook to be connected to the node. 823 * There is no per-hook private information though. 824 */ 825 static int 826 ngs_newhook(node_p node, hook_p hook, const char *name) 827 { 828 NG_HOOK_SET_PRIVATE(hook, NG_NODE_PRIVATE(node)); 829 return (0); 830 } 831 832 /* 833 * if only one hook, allow read(2) and write(2) to work. 834 */ 835 static int 836 ngs_connect(hook_p hook) 837 { 838 node_p node = NG_HOOK_NODE(hook); 839 struct ngsock *priv = NG_NODE_PRIVATE(node); 840 841 if ((priv->datasock) 842 && (priv->datasock->ng_socket)) { 843 if (NG_NODE_NUMHOOKS(node) == 1) { 844 priv->datasock->ng_socket->so_state |= SS_ISCONNECTED; 845 } else { 846 priv->datasock->ng_socket->so_state &= ~SS_ISCONNECTED; 847 } 848 } 849 return (0); 850 } 851 852 /* 853 * Incoming messages get passed up to the control socket. 854 * Unless they are for us specifically (socket_type) 855 */ 856 static int 857 ngs_rcvmsg(node_p node, item_p item, hook_p lasthook) 858 { 859 struct ngsock *const priv = NG_NODE_PRIVATE(node); 860 struct ngpcb *const pcbp = priv->ctlsock; 861 struct sockaddr_ng *addr; 862 int addrlen; 863 int error = 0; 864 struct ng_mesg *msg; 865 ng_ID_t retaddr = NGI_RETADDR(item); 866 char retabuf[32]; 867 868 NGI_GET_MSG(item, msg); 869 NG_FREE_ITEM(item); /* we have all we need */ 870 871 /* Only allow mesgs to be passed if we have the control socket. 872 * Data sockets can only support the generic messages. */ 873 if (pcbp == NULL) { 874 TRAP_ERROR; 875 return (EINVAL); 876 } 877 878 #ifdef TRACE_MESSAGES 879 printf("[%x]:---------->[socket]: c=<%d>cmd=%x(%s) f=%x #%d\n", 880 retaddr, 881 msg->header.typecookie, 882 msg->header.cmd, 883 msg->header.cmdstr, 884 msg->header.flags, 885 msg->header.token); 886 #endif 887 888 if (msg->header.typecookie == NGM_SOCKET_COOKIE) { 889 switch (msg->header.cmd) { 890 case NGM_SOCK_CMD_NOLINGER: 891 priv->flags |= NGS_FLAG_NOLINGER; 892 break; 893 case NGM_SOCK_CMD_LINGER: 894 priv->flags &= ~NGS_FLAG_NOLINGER; 895 break; 896 default: 897 error = EINVAL; /* unknown command */ 898 } 899 /* Free the message and return */ 900 NG_FREE_MSG(msg); 901 return(error); 902 903 } 904 /* Get the return address into a sockaddr */ 905 sprintf(retabuf,"[%x]:", retaddr); 906 addrlen = strlen(retabuf); 907 MALLOC(addr, struct sockaddr_ng *, addrlen + 4, M_NETGRAPH_PATH, M_NOWAIT); 908 if (addr == NULL) { 909 TRAP_ERROR; 910 return (ENOMEM); 911 } 912 addr->sg_len = addrlen + 3; 913 addr->sg_family = AF_NETGRAPH; 914 bcopy(retabuf, addr->sg_data, addrlen); 915 addr->sg_data[addrlen] = '\0'; 916 917 /* Send it up */ 918 error = ship_msg(pcbp, msg, addr); 919 FREE(addr, M_NETGRAPH_PATH); 920 return (error); 921 } 922 923 /* 924 * Receive data on a hook 925 */ 926 static int 927 ngs_rcvdata(hook_p hook, item_p item) 928 { 929 struct ngsock *const priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 930 struct ngpcb *const pcbp = priv->datasock; 931 struct socket *so; 932 struct sockaddr_ng *addr; 933 char *addrbuf[NG_HOOKSIZ + 4]; 934 int addrlen; 935 struct mbuf *m; 936 937 NGI_GET_M(item, m); 938 NG_FREE_ITEM(item); 939 /* If there is no data socket, black-hole it */ 940 if (pcbp == NULL) { 941 NG_FREE_M(m); 942 return (0); 943 } 944 so = pcbp->ng_socket; 945 946 /* Get the return address into a sockaddr. */ 947 addrlen = strlen(NG_HOOK_NAME(hook)); /* <= NG_HOOKSIZ - 1 */ 948 addr = (struct sockaddr_ng *) addrbuf; 949 addr->sg_len = addrlen + 3; 950 addr->sg_family = AF_NETGRAPH; 951 bcopy(NG_HOOK_NAME(hook), addr->sg_data, addrlen); 952 addr->sg_data[addrlen] = '\0'; 953 954 /* Try to tell the socket which hook it came in on */ 955 if (sbappendaddr(&so->so_rcv, (struct sockaddr *) addr, m, NULL) == 0) { 956 m_freem(m); 957 TRAP_ERROR; 958 return (ENOBUFS); 959 } 960 sorwakeup(so); 961 return (0); 962 } 963 964 /* 965 * Hook disconnection 966 * 967 * For this type, removal of the last link destroys the node 968 * if the NOLINGER flag is set. 969 */ 970 static int 971 ngs_disconnect(hook_p hook) 972 { 973 node_p node = NG_HOOK_NODE(hook); 974 struct ngsock *const priv = NG_NODE_PRIVATE(node); 975 976 if ((priv->datasock) 977 && (priv->datasock->ng_socket)) { 978 if (NG_NODE_NUMHOOKS(node) == 1) { 979 priv->datasock->ng_socket->so_state |= SS_ISCONNECTED; 980 } else { 981 priv->datasock->ng_socket->so_state &= ~SS_ISCONNECTED; 982 } 983 } 984 985 if ((priv->flags & NGS_FLAG_NOLINGER ) 986 && (NG_NODE_NUMHOOKS(node) == 0) 987 && (NG_NODE_IS_VALID(node))) { 988 ng_rmnode_self(node); 989 } 990 return (0); 991 } 992 993 /* 994 * Do local shutdown processing. 995 * In this case, that involves making sure the socket 996 * knows we should be shutting down. 997 */ 998 static int 999 ngs_shutdown(node_p node) 1000 { 1001 struct ngsock *const priv = NG_NODE_PRIVATE(node); 1002 struct ngpcb *const dpcbp = priv->datasock; 1003 struct ngpcb *const pcbp = priv->ctlsock; 1004 1005 if (dpcbp != NULL) { 1006 soisdisconnected(dpcbp->ng_socket); 1007 dpcbp->sockdata = NULL; 1008 priv->datasock = NULL; 1009 priv->refs--; 1010 } 1011 if (pcbp != NULL) { 1012 soisdisconnected(pcbp->ng_socket); 1013 pcbp->sockdata = NULL; 1014 priv->ctlsock = NULL; 1015 priv->refs--; 1016 } 1017 NG_NODE_SET_PRIVATE(node, NULL); 1018 NG_NODE_UNREF(node); 1019 FREE(priv, M_NETGRAPH_SOCK); 1020 return (0); 1021 } 1022 1023 static int 1024 dummy_disconnect(struct socket *so) 1025 { 1026 return (0); 1027 } 1028 /* 1029 * Control and data socket type descriptors 1030 */ 1031 1032 static struct pr_usrreqs ngc_usrreqs = { 1033 .pru_abort = NULL, 1034 .pru_attach = ngc_attach, 1035 .pru_bind = ngc_bind, 1036 .pru_connect = ngc_connect, 1037 .pru_detach = ngc_detach, 1038 .pru_disconnect = dummy_disconnect, 1039 .pru_peeraddr = NULL, 1040 .pru_send = ngc_send, 1041 .pru_shutdown = NULL, 1042 .pru_sockaddr = ng_setsockaddr, 1043 }; 1044 1045 static struct pr_usrreqs ngd_usrreqs = { 1046 .pru_abort = NULL, 1047 .pru_attach = ngd_attach, 1048 .pru_bind = NULL, 1049 .pru_connect = ngd_connect, 1050 .pru_detach = ngd_detach, 1051 .pru_disconnect = dummy_disconnect, 1052 .pru_peeraddr = NULL, 1053 .pru_send = ngd_send, 1054 .pru_shutdown = NULL, 1055 .pru_sockaddr ng_setsockaddr, 1056 }; 1057 1058 /* 1059 * Definitions of protocols supported in the NETGRAPH domain. 1060 */ 1061 1062 extern struct domain ngdomain; /* stop compiler warnings */ 1063 1064 static struct protosw ngsw[] = { 1065 { 1066 SOCK_DGRAM, /* protocol type */ 1067 &ngdomain, /* backpointer to domain */ 1068 NG_CONTROL, 1069 PR_ATOMIC | PR_ADDR /* | PR_RIGHTS */, /* flags */ 1070 0, 0, 0, 0, /* input, output, ctlinput, ctloutput */ 1071 NULL, /* ousrreq */ 1072 0, 0, 0, 0, /* init, fasttimeo, slowtimo, drain */ 1073 &ngc_usrreqs, /* usrreq table (above) */ 1074 /*{NULL}*/ /* pffh (protocol filter head?) */ 1075 }, 1076 { 1077 SOCK_DGRAM, /* protocol type */ 1078 &ngdomain, /* backpointer to domain */ 1079 NG_DATA, 1080 PR_ATOMIC | PR_ADDR, /* flags */ 1081 0, 0, 0, 0, /* input, output, ctlinput, ctloutput */ 1082 NULL, /* ousrreq() */ 1083 0, 0, 0, 0, /* init, fasttimeo, slowtimo, drain */ 1084 &ngd_usrreqs, /* usrreq table (above) */ 1085 /*{NULL}*/ /* pffh (protocol filter head?) */ 1086 } 1087 }; 1088 1089 struct domain ngdomain = { 1090 AF_NETGRAPH, 1091 "netgraph", 1092 NULL, /* init() */ 1093 NULL, /* externalise() */ 1094 NULL, /* dispose() */ 1095 ngsw, /* protosw entry */ 1096 &ngsw[sizeof(ngsw) / sizeof(ngsw[0])], /* Number of protosw entries */ 1097 NULL, /* next domain in list */ 1098 NULL, /* rtattach() */ 1099 0, /* arg to rtattach in bits */ 1100 0 /* maxrtkey */ 1101 }; 1102 1103 /* 1104 * Handle loading and unloading for this node type 1105 * This is to handle auxiliary linkages (e.g protocol domain addition). 1106 */ 1107 static int 1108 ngs_mod_event(module_t mod, int event, void *data) 1109 { 1110 int error = 0; 1111 1112 switch (event) { 1113 case MOD_LOAD: 1114 mtx_init(&ngsocketlist_mtx, "ng_socketlist", NULL, MTX_DEF); 1115 /* Register protocol domain */ 1116 net_add_domain(&ngdomain); 1117 break; 1118 case MOD_UNLOAD: 1119 /* Insure there are no open netgraph sockets */ 1120 if (!LIST_EMPTY(&ngsocklist)) { 1121 error = EBUSY; 1122 break; 1123 } 1124 1125 #ifdef NOTYET 1126 if ((LIST_EMPTY(&ngsocklist)) && (typestruct.refs == 0)) { 1127 /* Unregister protocol domain XXX can't do this yet.. */ 1128 if ((error = net_rm_domain(&ngdomain)) != 0) 1129 break; 1130 mtx_destroy(&ngsocketlist_mtx); 1131 } else 1132 #endif 1133 error = EBUSY; 1134 break; 1135 default: 1136 error = EOPNOTSUPP; 1137 break; 1138 } 1139 return (error); 1140 } 1141 1142 SYSCTL_INT(_net_graph, OID_AUTO, family, CTLFLAG_RD, 0, AF_NETGRAPH, ""); 1143 SYSCTL_NODE(_net_graph, OID_AUTO, data, CTLFLAG_RW, 0, "DATA"); 1144 SYSCTL_INT(_net_graph_data, OID_AUTO, proto, CTLFLAG_RD, 0, NG_DATA, ""); 1145 SYSCTL_NODE(_net_graph, OID_AUTO, control, CTLFLAG_RW, 0, "CONTROL"); 1146 SYSCTL_INT(_net_graph_control, OID_AUTO, proto, CTLFLAG_RD, 0, NG_CONTROL, ""); 1147 1148