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(struct ngpcb *pcbp, int type); 126 static void ng_socket_free_priv(struct ngsock *priv); 127 /*static int ng_internalize(struct mbuf *m, struct thread *p); */ 128 129 static int ng_connect_data(struct sockaddr *nam, struct ngpcb *pcbp); 130 static int ng_bind(struct sockaddr *nam, struct ngpcb *pcbp); 131 132 static int ngs_mod_event(module_t mod, int event, void *data); 133 static int ship_msg(struct ngpcb *pcbp, struct ng_mesg *msg, 134 struct sockaddr_ng *addr); 135 static void ng_socket_item_applied(void *context, int error); 136 137 /* Netgraph type descriptor */ 138 static struct ng_type typestruct = { 139 .version = NG_ABI_VERSION, 140 .name = NG_SOCKET_NODE_TYPE, 141 .mod_event = ngs_mod_event, 142 .constructor = ngs_constructor, 143 .rcvmsg = ngs_rcvmsg, 144 .shutdown = ngs_shutdown, 145 .newhook = ngs_newhook, 146 .connect = ngs_connect, 147 .rcvdata = ngs_rcvdata, 148 .disconnect = ngs_disconnect, 149 }; 150 NETGRAPH_INIT_ORDERED(socket, &typestruct, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY); 151 152 /* Buffer space */ 153 static u_long ngpdg_sendspace = 20 * 1024; /* really max datagram size */ 154 SYSCTL_INT(_net_graph, OID_AUTO, maxdgram, CTLFLAG_RW, 155 &ngpdg_sendspace , 0, "Maximum outgoing Netgraph datagram size"); 156 static u_long ngpdg_recvspace = 20 * 1024; 157 SYSCTL_INT(_net_graph, OID_AUTO, recvspace, CTLFLAG_RW, 158 &ngpdg_recvspace , 0, "Maximum space for incoming Netgraph datagrams"); 159 160 /* List of all sockets */ 161 static LIST_HEAD(, ngpcb) ngsocklist; 162 163 static struct mtx ngsocketlist_mtx; 164 165 #define sotongpcb(so) ((struct ngpcb *)(so)->so_pcb) 166 167 /* If getting unexplained errors returned, set this to "kdb_enter("X"); */ 168 #ifndef TRAP_ERROR 169 #define TRAP_ERROR 170 #endif 171 172 /*************************************************************** 173 Control sockets 174 ***************************************************************/ 175 176 static int 177 ngc_attach(struct socket *so, int proto, struct thread *td) 178 { 179 struct ngpcb *const pcbp = sotongpcb(so); 180 181 if (suser(td)) 182 return (EPERM); 183 if (pcbp != NULL) 184 return (EISCONN); 185 return (ng_attach_cntl(so)); 186 } 187 188 static void 189 ngc_detach(struct socket *so) 190 { 191 struct ngpcb *const pcbp = sotongpcb(so); 192 193 KASSERT(pcbp != NULL, ("ngc_detach: pcbp == NULL")); 194 ng_detach_common(pcbp, NG_CONTROL); 195 } 196 197 static int 198 ngc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, 199 struct mbuf *control, struct thread *td) 200 { 201 struct ngpcb *const pcbp = sotongpcb(so); 202 struct ngsock *const priv = NG_NODE_PRIVATE(pcbp->sockdata->node); 203 struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr; 204 struct ng_mesg *msg; 205 struct mbuf *m0; 206 item_p item; 207 char *path = NULL; 208 int len, error = 0; 209 210 if (pcbp == NULL) { 211 error = EINVAL; 212 goto release; 213 } 214 #ifdef NOTYET 215 if (control && (error = ng_internalize(control, td))) { 216 if (pcbp->sockdata == NULL) { 217 error = ENOTCONN; 218 goto release; 219 } 220 } 221 #else /* NOTYET */ 222 if (control) { 223 error = EINVAL; 224 goto release; 225 } 226 #endif /* NOTYET */ 227 228 /* Require destination as there may be >= 1 hooks on this node */ 229 if (addr == NULL) { 230 error = EDESTADDRREQ; 231 goto release; 232 } 233 234 /* Allocate an expendable buffer for the path, chop off 235 * the sockaddr header, and make sure it's NUL terminated */ 236 len = sap->sg_len - 2; 237 MALLOC(path, char *, len + 1, M_NETGRAPH_PATH, M_WAITOK); 238 if (path == NULL) { 239 error = ENOMEM; 240 goto release; 241 } 242 bcopy(sap->sg_data, path, len); 243 path[len] = '\0'; 244 245 /* Move the actual message out of mbufs into a linear buffer. 246 * Start by adding up the size of the data. (could use mh_len?) */ 247 for (len = 0, m0 = m; m0 != NULL; m0 = m0->m_next) 248 len += m0->m_len; 249 250 /* Move the data into a linear buffer as well. Messages are not 251 * delivered in mbufs. */ 252 MALLOC(msg, struct ng_mesg *, len + 1, M_NETGRAPH_MSG, M_WAITOK); 253 if (msg == NULL) { 254 error = ENOMEM; 255 goto release; 256 } 257 m_copydata(m, 0, len, (char *)msg); 258 259 if (msg->header.version != NG_VERSION) { 260 error = EINVAL; 261 goto release; 262 } 263 264 /* 265 * Hack alert! 266 * We look into the message and if it mkpeers a node of unknown type, we 267 * try to load it. We need to do this now, in syscall thread, because if 268 * message gets queued and applied later we will get panic. 269 */ 270 if (msg->header.typecookie == NGM_GENERIC_COOKIE && 271 msg->header.cmd == NGM_MKPEER) { 272 struct ngm_mkpeer *const mkp = (struct ngm_mkpeer *) msg->data; 273 struct ng_type *type; 274 275 if ((type = ng_findtype(mkp->type)) == NULL) { 276 char filename[NG_TYPESIZ + 3]; 277 linker_file_t lf; 278 279 /* Not found, try to load it as a loadable module. */ 280 snprintf(filename, sizeof(filename), "ng_%s", 281 mkp->type); 282 mtx_lock(&Giant); 283 error = linker_load_module(NULL, filename, NULL, NULL, 284 &lf); 285 mtx_unlock(&Giant); 286 if (error != 0) { 287 FREE(msg, M_NETGRAPH_MSG); 288 goto release; 289 } 290 lf->userrefs++; 291 292 /* See if type has been loaded successfully. */ 293 if ((type = ng_findtype(mkp->type)) == NULL) { 294 FREE(msg, M_NETGRAPH_MSG); 295 error = ENXIO; 296 goto release; 297 } 298 } 299 } 300 301 if ((item = ng_package_msg(msg, M_WAITOK)) == NULL) { 302 error = ENOMEM; 303 #ifdef TRACE_MESSAGES 304 printf("ng_package_msg: err=%d\n", error); 305 #endif 306 goto release; 307 } 308 if ((error = ng_address_path((pcbp->sockdata->node), item, 309 path, 0)) != 0) { 310 #ifdef TRACE_MESSAGES 311 printf("ng_address_path: errx=%d\n", error); 312 #endif 313 goto release; 314 } 315 316 #ifdef TRACE_MESSAGES 317 printf("[%x]:<---------[socket]: c=<%d>cmd=%x(%s) f=%x #%d (%s)\n", 318 item->el_dest->nd_ID, 319 msg->header.typecookie, 320 msg->header.cmd, 321 msg->header.cmdstr, 322 msg->header.flags, 323 msg->header.token, 324 item->el_dest->nd_type->name); 325 #endif 326 SAVE_LINE(item); 327 /* 328 * We do not want to return from syscall until the item 329 * is processed by destination node. We register callback 330 * on the item, which will update priv->error when item 331 * was applied. 332 * If ng_snd_item() has queued item, we sleep until 333 * callback wakes us up. 334 */ 335 item->apply = ng_socket_item_applied; 336 item->context = priv; 337 priv->error = -1; 338 339 error = ng_snd_item(item, NG_PROGRESS); 340 341 if (error == EINPROGRESS) { 342 mtx_lock(&priv->mtx); 343 if (priv->error == -1) 344 msleep(priv, &priv->mtx, 0, "ngsock", 0); 345 mtx_unlock(&priv->mtx); 346 KASSERT(priv->error != -1, 347 ("ng_socket: priv->error wasn't updated")); 348 error = priv->error; 349 } 350 351 release: 352 if (path != NULL) 353 FREE(path, M_NETGRAPH_PATH); 354 if (control != NULL) 355 m_freem(control); 356 if (m != NULL) 357 m_freem(m); 358 return (error); 359 } 360 361 static int 362 ngc_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 363 { 364 struct ngpcb *const pcbp = sotongpcb(so); 365 366 if (pcbp == 0) 367 return (EINVAL); 368 return (ng_bind(nam, pcbp)); 369 } 370 371 static int 372 ngc_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 373 { 374 printf(" program tried to connect control socket to remote node\n "); 375 /* 376 * At this time refuse to do this.. it used to 377 * do something but it was undocumented and not used. 378 */ 379 return (EINVAL); 380 } 381 382 /*************************************************************** 383 Data sockets 384 ***************************************************************/ 385 386 static int 387 ngd_attach(struct socket *so, int proto, struct thread *td) 388 { 389 struct ngpcb *const pcbp = sotongpcb(so); 390 391 if (pcbp != NULL) 392 return (EISCONN); 393 return (ng_attach_data(so)); 394 } 395 396 static void 397 ngd_detach(struct socket *so) 398 { 399 struct ngpcb *const pcbp = sotongpcb(so); 400 401 KASSERT(pcbp != NULL, ("ngd_detach: pcbp == NULL")); 402 ng_detach_common(pcbp, NG_DATA); 403 } 404 405 static int 406 ngd_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, 407 struct mbuf *control, struct thread *td) 408 { 409 struct ngpcb *const pcbp = sotongpcb(so); 410 struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr; 411 int len, error; 412 hook_p hook = NULL; 413 char hookname[NG_HOOKSIZ]; 414 415 if ((pcbp == NULL) || (control != NULL)) { 416 error = EINVAL; 417 goto release; 418 } 419 if (pcbp->sockdata == NULL) { 420 error = ENOTCONN; 421 goto release; 422 } 423 424 if (sap == NULL) 425 len = 0; /* Make compiler happy. */ 426 else 427 len = sap->sg_len - 2; 428 429 /* 430 * If the user used any of these ways to not specify an address 431 * then handle specially. 432 */ 433 if ((sap == NULL) || (len <= 0) || (*sap->sg_data == '\0')) { 434 if (NG_NODE_NUMHOOKS(pcbp->sockdata->node) != 1) { 435 error = EDESTADDRREQ; 436 goto release; 437 } 438 /* 439 * if exactly one hook exists, just use it. 440 * Special case to allow write(2) to work on an ng_socket. 441 */ 442 hook = LIST_FIRST(&pcbp->sockdata->node->nd_hooks); 443 } else { 444 if (len >= NG_HOOKSIZ) { 445 error = EINVAL; 446 goto release; 447 } 448 449 /* 450 * chop off the sockaddr header, and make sure it's NUL 451 * terminated 452 */ 453 bcopy(sap->sg_data, hookname, len); 454 hookname[len] = '\0'; 455 456 /* Find the correct hook from 'hookname' */ 457 LIST_FOREACH(hook, &pcbp->sockdata->node->nd_hooks, hk_hooks) { 458 if (strcmp(hookname, NG_HOOK_NAME(hook)) == 0) { 459 break; 460 } 461 } 462 if (hook == NULL) { 463 error = EHOSTUNREACH; 464 } 465 } 466 467 /* Send data (OK if hook is NULL) */ 468 NG_SEND_DATA_ONLY(error, hook, m); /* makes m NULL */ 469 470 release: 471 if (control != NULL) 472 m_freem(control); 473 if (m != NULL) 474 m_freem(m); 475 return (error); 476 } 477 478 static int 479 ngd_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 480 { 481 struct ngpcb *const pcbp = sotongpcb(so); 482 483 if (pcbp == 0) 484 return (EINVAL); 485 return (ng_connect_data(nam, pcbp)); 486 } 487 488 /* 489 * Used for both data and control sockets 490 */ 491 static int 492 ng_setsockaddr(struct socket *so, struct sockaddr **addr) 493 { 494 struct ngpcb *pcbp; 495 struct sockaddr_ng *sg; 496 int sg_len; 497 int error = 0; 498 499 /* Why isn't sg_data a `char[1]' ? :-( */ 500 sg_len = sizeof(struct sockaddr_ng) - sizeof(sg->sg_data) + 1; 501 502 pcbp = sotongpcb(so); 503 if ((pcbp == NULL) || (pcbp->sockdata == NULL)) 504 /* XXXGL: can this still happen? */ 505 return (EINVAL); 506 507 mtx_lock(&pcbp->sockdata->mtx); 508 if (pcbp->sockdata->node != NULL) { 509 node_p node = pcbp->sockdata->node; 510 int namelen = 0; /* silence compiler! */ 511 512 if (NG_NODE_HAS_NAME(node)) 513 sg_len += namelen = strlen(NG_NODE_NAME(node)); 514 515 sg = malloc(sg_len, M_SONAME, M_WAITOK | M_ZERO); 516 517 if (NG_NODE_HAS_NAME(node)) 518 bcopy(NG_NODE_NAME(node), sg->sg_data, namelen); 519 520 sg->sg_len = sg_len; 521 sg->sg_family = AF_NETGRAPH; 522 *addr = (struct sockaddr *)sg; 523 mtx_unlock(&pcbp->sockdata->mtx); 524 } else { 525 mtx_unlock(&pcbp->sockdata->mtx); 526 error = EINVAL; 527 } 528 529 return (error); 530 } 531 532 /* 533 * Attach a socket to it's protocol specific partner. 534 * For a control socket, actually create a netgraph node and attach 535 * to it as well. 536 */ 537 538 static int 539 ng_attach_cntl(struct socket *so) 540 { 541 struct ngsock *priv; 542 struct ngpcb *pcbp; 543 int error; 544 545 /* Allocate node private info */ 546 MALLOC(priv, struct ngsock *, 547 sizeof(*priv), M_NETGRAPH_SOCK, M_WAITOK | M_ZERO); 548 if (priv == NULL) 549 return (ENOMEM); 550 551 /* Setup protocol control block */ 552 if ((error = ng_attach_common(so, NG_CONTROL)) != 0) { 553 FREE(priv, M_NETGRAPH_SOCK); 554 return (error); 555 } 556 pcbp = sotongpcb(so); 557 558 /* Link the pcb the private data. */ 559 priv->ctlsock = pcbp; 560 pcbp->sockdata = priv; 561 priv->refs++; 562 563 /* Initialize mutex. */ 564 mtx_init(&priv->mtx, "ng_socket", NULL, MTX_DEF); 565 566 /* Make the generic node components */ 567 if ((error = ng_make_node_common(&typestruct, &priv->node)) != 0) { 568 FREE(priv, M_NETGRAPH_SOCK); 569 ng_detach_common(pcbp, NG_CONTROL); 570 return (error); 571 } 572 573 /* Link the node and the private data. */ 574 NG_NODE_SET_PRIVATE(priv->node, priv); 575 NG_NODE_REF(priv->node); 576 priv->refs++; 577 578 return (0); 579 } 580 581 static int 582 ng_attach_data(struct socket *so) 583 { 584 return(ng_attach_common(so, NG_DATA)); 585 } 586 587 /* 588 * Set up a socket protocol control block. 589 * This code is shared between control and data sockets. 590 */ 591 static int 592 ng_attach_common(struct socket *so, int type) 593 { 594 struct ngpcb *pcbp; 595 int error; 596 597 /* Standard socket setup stuff */ 598 error = soreserve(so, ngpdg_sendspace, ngpdg_recvspace); 599 if (error) 600 return (error); 601 602 /* Allocate the pcb */ 603 MALLOC(pcbp, struct ngpcb *, sizeof(*pcbp), M_PCB, M_WAITOK | M_ZERO); 604 if (pcbp == NULL) 605 return (ENOMEM); 606 pcbp->type = type; 607 608 /* Link the pcb and the socket */ 609 so->so_pcb = (caddr_t) pcbp; 610 pcbp->ng_socket = so; 611 612 /* Add the socket to linked list */ 613 mtx_lock(&ngsocketlist_mtx); 614 LIST_INSERT_HEAD(&ngsocklist, pcbp, socks); 615 mtx_unlock(&ngsocketlist_mtx); 616 return (0); 617 } 618 619 /* 620 * Disassociate the socket from it's protocol specific 621 * partner. If it's attached to a node's private data structure, 622 * then unlink from that too. If we were the last socket attached to it, 623 * then shut down the entire node. Shared code for control and data sockets. 624 */ 625 static void 626 ng_detach_common(struct ngpcb *pcbp, int which) 627 { 628 struct ngsock *priv = pcbp->sockdata; 629 630 if (priv != NULL) { 631 mtx_lock(&priv->mtx); 632 633 switch (which) { 634 case NG_CONTROL: 635 priv->ctlsock = NULL; 636 break; 637 case NG_DATA: 638 priv->datasock = NULL; 639 break; 640 default: 641 panic(__func__); 642 } 643 pcbp->sockdata = NULL; 644 645 ng_socket_free_priv(priv); 646 } 647 648 pcbp->ng_socket->so_pcb = NULL; 649 mtx_lock(&ngsocketlist_mtx); 650 LIST_REMOVE(pcbp, socks); 651 mtx_unlock(&ngsocketlist_mtx); 652 FREE(pcbp, M_PCB); 653 } 654 655 /* 656 * Remove a reference from node private data. 657 */ 658 static void 659 ng_socket_free_priv(struct ngsock *priv) 660 { 661 mtx_assert(&priv->mtx, MA_OWNED); 662 663 priv->refs--; 664 665 if (priv->refs == 0) { 666 mtx_destroy(&priv->mtx); 667 FREE(priv, M_NETGRAPH_SOCK); 668 return; 669 } 670 671 if ((priv->refs == 1) && (priv->node != NULL)) { 672 node_p node = priv->node; 673 674 priv->node = NULL; 675 mtx_unlock(&priv->mtx); 676 NG_NODE_UNREF(node); 677 ng_rmnode_self(node); 678 } else 679 mtx_unlock(&priv->mtx); 680 } 681 682 #ifdef NOTYET 683 /* 684 * File descriptors can be passed into an AF_NETGRAPH socket. 685 * Note, that file descriptors cannot be passed OUT. 686 * Only character device descriptors are accepted. 687 * Character devices are useful to connect a graph to a device, 688 * which after all is the purpose of this whole system. 689 */ 690 static int 691 ng_internalize(struct mbuf *control, struct thread *td) 692 { 693 const struct cmsghdr *cm = mtod(control, const struct cmsghdr *); 694 struct file *fp; 695 struct vnode *vn; 696 int oldfds; 697 int fd; 698 699 if (cm->cmsg_type != SCM_RIGHTS || cm->cmsg_level != SOL_SOCKET || 700 cm->cmsg_len != control->m_len) { 701 TRAP_ERROR; 702 return (EINVAL); 703 } 704 705 /* Check there is only one FD. XXX what would more than one signify? */ 706 oldfds = ((caddr_t)cm + cm->cmsg_len - (caddr_t)data) / sizeof (int); 707 if (oldfds != 1) { 708 TRAP_ERROR; 709 return (EINVAL); 710 } 711 712 /* Check that the FD given is legit. and change it to a pointer to a 713 * struct file. */ 714 fd = CMSG_DATA(cm); 715 if ((error = fget(td, fd, &fp)) != 0) 716 return (error); 717 718 /* Depending on what kind of resource it is, act differently. For 719 * devices, we treat it as a file. For an AF_NETGRAPH socket, 720 * shortcut straight to the node. */ 721 switch (fp->f_type) { 722 case DTYPE_VNODE: 723 vn = fp->f_data; 724 if (vn && (vn->v_type == VCHR)) { 725 /* for a VCHR, actually reference the FILE */ 726 fp->f_count++; 727 /* XXX then what :) */ 728 /* how to pass on to other modules? */ 729 } else { 730 fdrop(fp, td); 731 TRAP_ERROR; 732 return (EINVAL); 733 } 734 break; 735 default: 736 fdrop(fp, td); 737 TRAP_ERROR; 738 return (EINVAL); 739 } 740 fdrop(fp, td); 741 return (0); 742 } 743 #endif /* NOTYET */ 744 745 /* 746 * Connect the data socket to a named control socket node. 747 */ 748 static int 749 ng_connect_data(struct sockaddr *nam, struct ngpcb *pcbp) 750 { 751 struct sockaddr_ng *sap; 752 node_p farnode; 753 struct ngsock *priv; 754 int error; 755 item_p item; 756 757 /* If we are already connected, don't do it again */ 758 if (pcbp->sockdata != NULL) 759 return (EISCONN); 760 761 /* Find the target (victim) and check it doesn't already have a data 762 * socket. Also check it is a 'socket' type node. 763 * Use ng_package_data() and address_path() to do this. 764 */ 765 766 sap = (struct sockaddr_ng *) nam; 767 /* The item will hold the node reference */ 768 item = ng_package_data(NULL, NG_WAITOK); 769 if (item == NULL) { 770 return (ENOMEM); 771 } 772 if ((error = ng_address_path(NULL, item, sap->sg_data, 0))) 773 return (error); /* item is freed on failure */ 774 775 /* 776 * Extract node from item and free item. Remember we now have 777 * a reference on the node. The item holds it for us. 778 * when we free the item we release the reference. 779 */ 780 farnode = item->el_dest; /* shortcut */ 781 if (strcmp(farnode->nd_type->name, NG_SOCKET_NODE_TYPE) != 0) { 782 NG_FREE_ITEM(item); /* drop the reference to the node */ 783 return (EINVAL); 784 } 785 priv = NG_NODE_PRIVATE(farnode); 786 if (priv->datasock != NULL) { 787 NG_FREE_ITEM(item); /* drop the reference to the node */ 788 return (EADDRINUSE); 789 } 790 791 /* 792 * Link the PCB and the private data struct. and note the extra 793 * reference. Drop the extra reference on the node. 794 */ 795 mtx_lock(&priv->mtx); 796 priv->datasock = pcbp; 797 pcbp->sockdata = priv; 798 priv->refs++; 799 mtx_unlock(&priv->mtx); 800 NG_FREE_ITEM(item); /* drop the reference to the node */ 801 return (0); 802 } 803 804 /* 805 * Binding a socket means giving the corresponding node a name 806 */ 807 static int 808 ng_bind(struct sockaddr *nam, struct ngpcb *pcbp) 809 { 810 struct ngsock *const priv = pcbp->sockdata; 811 struct sockaddr_ng *const sap = (struct sockaddr_ng *) nam; 812 813 if (priv == NULL) { 814 TRAP_ERROR; 815 return (EINVAL); 816 } 817 if ((sap->sg_len < 4) 818 || (sap->sg_len > (NG_NODESIZ + 2)) 819 || (sap->sg_data[0] == '\0') 820 || (sap->sg_data[sap->sg_len - 3] != '\0')) { 821 TRAP_ERROR; 822 return (EINVAL); 823 } 824 return (ng_name_node(priv->node, sap->sg_data)); 825 } 826 827 /* 828 * Take a message and pass it up to the control socket associated 829 * with the node. 830 */ 831 static int 832 ship_msg(struct ngpcb *pcbp, struct ng_mesg *msg, struct sockaddr_ng *addr) 833 { 834 struct socket *const so = pcbp->ng_socket; 835 struct mbuf *mdata; 836 int msglen; 837 int error = 0; 838 839 /* Copy the message itself into an mbuf chain */ 840 msglen = sizeof(struct ng_mesg) + msg->header.arglen; 841 mdata = m_devget((caddr_t) msg, msglen, 0, NULL, NULL); 842 843 /* Here we free the message, as we are the end of the line. 844 * We need to do that regardless of whether we got mbufs. */ 845 NG_FREE_MSG(msg); 846 847 if (mdata == NULL) { 848 TRAP_ERROR; 849 return (ENOBUFS); 850 } 851 852 /* Send it up to the socket */ 853 if (sbappendaddr(&so->so_rcv, 854 (struct sockaddr *) addr, mdata, NULL) == 0) { 855 TRAP_ERROR; 856 m_freem(mdata); 857 error = so->so_error = ENOBUFS; 858 } 859 sorwakeup(so); 860 return (error); 861 } 862 863 /*************************************************************** 864 Netgraph node 865 ***************************************************************/ 866 867 /* 868 * You can only create new nodes from the socket end of things. 869 */ 870 static int 871 ngs_constructor(node_p nodep) 872 { 873 return (EINVAL); 874 } 875 876 /* 877 * We allow any hook to be connected to the node. 878 * There is no per-hook private information though. 879 */ 880 static int 881 ngs_newhook(node_p node, hook_p hook, const char *name) 882 { 883 NG_HOOK_SET_PRIVATE(hook, NG_NODE_PRIVATE(node)); 884 return (0); 885 } 886 887 /* 888 * if only one hook, allow read(2) and write(2) to work. 889 */ 890 static int 891 ngs_connect(hook_p hook) 892 { 893 node_p node = NG_HOOK_NODE(hook); 894 struct ngsock *priv = NG_NODE_PRIVATE(node); 895 896 if ((priv->datasock) 897 && (priv->datasock->ng_socket)) { 898 if (NG_NODE_NUMHOOKS(node) == 1) { 899 priv->datasock->ng_socket->so_state |= SS_ISCONNECTED; 900 } else { 901 priv->datasock->ng_socket->so_state &= ~SS_ISCONNECTED; 902 } 903 } 904 return (0); 905 } 906 907 /* 908 * Incoming messages get passed up to the control socket. 909 * Unless they are for us specifically (socket_type) 910 */ 911 static int 912 ngs_rcvmsg(node_p node, item_p item, hook_p lasthook) 913 { 914 struct ngsock *const priv = NG_NODE_PRIVATE(node); 915 struct ngpcb *const pcbp = priv->ctlsock; 916 struct sockaddr_ng *addr; 917 int addrlen; 918 int error = 0; 919 struct ng_mesg *msg; 920 ng_ID_t retaddr = NGI_RETADDR(item); 921 char retabuf[32]; 922 923 NGI_GET_MSG(item, msg); 924 NG_FREE_ITEM(item); /* we have all we need */ 925 926 /* Only allow mesgs to be passed if we have the control socket. 927 * Data sockets can only support the generic messages. */ 928 if (pcbp == NULL) { 929 TRAP_ERROR; 930 return (EINVAL); 931 } 932 933 #ifdef TRACE_MESSAGES 934 printf("[%x]:---------->[socket]: c=<%d>cmd=%x(%s) f=%x #%d\n", 935 retaddr, 936 msg->header.typecookie, 937 msg->header.cmd, 938 msg->header.cmdstr, 939 msg->header.flags, 940 msg->header.token); 941 #endif 942 943 if (msg->header.typecookie == NGM_SOCKET_COOKIE) { 944 switch (msg->header.cmd) { 945 case NGM_SOCK_CMD_NOLINGER: 946 priv->flags |= NGS_FLAG_NOLINGER; 947 break; 948 case NGM_SOCK_CMD_LINGER: 949 priv->flags &= ~NGS_FLAG_NOLINGER; 950 break; 951 default: 952 error = EINVAL; /* unknown command */ 953 } 954 /* Free the message and return */ 955 NG_FREE_MSG(msg); 956 return(error); 957 958 } 959 /* Get the return address into a sockaddr */ 960 sprintf(retabuf,"[%x]:", retaddr); 961 addrlen = strlen(retabuf); 962 MALLOC(addr, struct sockaddr_ng *, addrlen + 4, M_NETGRAPH_PATH, M_NOWAIT); 963 if (addr == NULL) { 964 TRAP_ERROR; 965 return (ENOMEM); 966 } 967 addr->sg_len = addrlen + 3; 968 addr->sg_family = AF_NETGRAPH; 969 bcopy(retabuf, addr->sg_data, addrlen); 970 addr->sg_data[addrlen] = '\0'; 971 972 /* Send it up */ 973 error = ship_msg(pcbp, msg, addr); 974 FREE(addr, M_NETGRAPH_PATH); 975 return (error); 976 } 977 978 /* 979 * Receive data on a hook 980 */ 981 static int 982 ngs_rcvdata(hook_p hook, item_p item) 983 { 984 struct ngsock *const priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 985 struct ngpcb *const pcbp = priv->datasock; 986 struct socket *so; 987 struct sockaddr_ng *addr; 988 char *addrbuf[NG_HOOKSIZ + 4]; 989 int addrlen; 990 struct mbuf *m; 991 992 NGI_GET_M(item, m); 993 NG_FREE_ITEM(item); 994 /* If there is no data socket, black-hole it */ 995 if (pcbp == NULL) { 996 NG_FREE_M(m); 997 return (0); 998 } 999 so = pcbp->ng_socket; 1000 1001 /* Get the return address into a sockaddr. */ 1002 addrlen = strlen(NG_HOOK_NAME(hook)); /* <= NG_HOOKSIZ - 1 */ 1003 addr = (struct sockaddr_ng *) addrbuf; 1004 addr->sg_len = addrlen + 3; 1005 addr->sg_family = AF_NETGRAPH; 1006 bcopy(NG_HOOK_NAME(hook), addr->sg_data, addrlen); 1007 addr->sg_data[addrlen] = '\0'; 1008 1009 /* Try to tell the socket which hook it came in on */ 1010 if (sbappendaddr(&so->so_rcv, (struct sockaddr *) addr, m, NULL) == 0) { 1011 m_freem(m); 1012 TRAP_ERROR; 1013 return (ENOBUFS); 1014 } 1015 sorwakeup(so); 1016 return (0); 1017 } 1018 1019 /* 1020 * Hook disconnection 1021 * 1022 * For this type, removal of the last link destroys the node 1023 * if the NOLINGER flag is set. 1024 */ 1025 static int 1026 ngs_disconnect(hook_p hook) 1027 { 1028 node_p node = NG_HOOK_NODE(hook); 1029 struct ngsock *const priv = NG_NODE_PRIVATE(node); 1030 1031 if ((priv->datasock) 1032 && (priv->datasock->ng_socket)) { 1033 if (NG_NODE_NUMHOOKS(node) == 1) { 1034 priv->datasock->ng_socket->so_state |= SS_ISCONNECTED; 1035 } else { 1036 priv->datasock->ng_socket->so_state &= ~SS_ISCONNECTED; 1037 } 1038 } 1039 1040 if ((priv->flags & NGS_FLAG_NOLINGER ) 1041 && (NG_NODE_NUMHOOKS(node) == 0) 1042 && (NG_NODE_IS_VALID(node))) { 1043 ng_rmnode_self(node); 1044 } 1045 return (0); 1046 } 1047 1048 /* 1049 * Do local shutdown processing. 1050 * In this case, that involves making sure the socket 1051 * knows we should be shutting down. 1052 */ 1053 static int 1054 ngs_shutdown(node_p node) 1055 { 1056 struct ngsock *const priv = NG_NODE_PRIVATE(node); 1057 struct ngpcb *const dpcbp = priv->datasock; 1058 struct ngpcb *const pcbp = priv->ctlsock; 1059 1060 if (dpcbp != NULL) 1061 soisdisconnected(dpcbp->ng_socket); 1062 1063 if (pcbp != NULL) 1064 soisdisconnected(pcbp->ng_socket); 1065 1066 mtx_lock(&priv->mtx); 1067 priv->node = NULL; 1068 NG_NODE_SET_PRIVATE(node, NULL); 1069 ng_socket_free_priv(priv); 1070 1071 NG_NODE_UNREF(node); 1072 return (0); 1073 } 1074 1075 static void 1076 ng_socket_item_applied(void *context, int error) 1077 { 1078 struct ngsock *const priv = (struct ngsock *)context; 1079 1080 mtx_lock(&priv->mtx); 1081 priv->error = error; 1082 wakeup(priv); 1083 mtx_unlock(&priv->mtx); 1084 1085 } 1086 1087 static int 1088 dummy_disconnect(struct socket *so) 1089 { 1090 return (0); 1091 } 1092 /* 1093 * Control and data socket type descriptors 1094 */ 1095 1096 static struct pr_usrreqs ngc_usrreqs = { 1097 .pru_abort = NULL, 1098 .pru_attach = ngc_attach, 1099 .pru_bind = ngc_bind, 1100 .pru_connect = ngc_connect, 1101 .pru_detach = ngc_detach, 1102 .pru_disconnect = dummy_disconnect, 1103 .pru_peeraddr = NULL, 1104 .pru_send = ngc_send, 1105 .pru_shutdown = NULL, 1106 .pru_sockaddr = ng_setsockaddr, 1107 }; 1108 1109 static struct pr_usrreqs ngd_usrreqs = { 1110 .pru_abort = NULL, 1111 .pru_attach = ngd_attach, 1112 .pru_bind = NULL, 1113 .pru_connect = ngd_connect, 1114 .pru_detach = ngd_detach, 1115 .pru_disconnect = dummy_disconnect, 1116 .pru_peeraddr = NULL, 1117 .pru_send = ngd_send, 1118 .pru_shutdown = NULL, 1119 .pru_sockaddr = ng_setsockaddr, 1120 }; 1121 1122 /* 1123 * Definitions of protocols supported in the NETGRAPH domain. 1124 */ 1125 1126 extern struct domain ngdomain; /* stop compiler warnings */ 1127 1128 static struct protosw ngsw[] = { 1129 { 1130 .pr_type = SOCK_DGRAM, 1131 .pr_domain = &ngdomain, 1132 .pr_protocol = NG_CONTROL, 1133 .pr_flags = PR_ATOMIC | PR_ADDR /* | PR_RIGHTS */, 1134 .pr_usrreqs = &ngc_usrreqs 1135 }, 1136 { 1137 .pr_type = SOCK_DGRAM, 1138 .pr_domain = &ngdomain, 1139 .pr_protocol = NG_DATA, 1140 .pr_flags = PR_ATOMIC | PR_ADDR, 1141 .pr_usrreqs = &ngd_usrreqs 1142 } 1143 }; 1144 1145 struct domain ngdomain = { 1146 .dom_family = AF_NETGRAPH, 1147 .dom_name = "netgraph", 1148 .dom_protosw = ngsw, 1149 .dom_protoswNPROTOSW = &ngsw[sizeof(ngsw) / sizeof(ngsw[0])] 1150 }; 1151 1152 /* 1153 * Handle loading and unloading for this node type 1154 * This is to handle auxiliary linkages (e.g protocol domain addition). 1155 */ 1156 static int 1157 ngs_mod_event(module_t mod, int event, void *data) 1158 { 1159 int error = 0; 1160 1161 switch (event) { 1162 case MOD_LOAD: 1163 mtx_init(&ngsocketlist_mtx, "ng_socketlist", NULL, MTX_DEF); 1164 /* Register protocol domain */ 1165 net_add_domain(&ngdomain); 1166 break; 1167 case MOD_UNLOAD: 1168 /* Insure there are no open netgraph sockets */ 1169 if (!LIST_EMPTY(&ngsocklist)) { 1170 error = EBUSY; 1171 break; 1172 } 1173 1174 #ifdef NOTYET 1175 if ((LIST_EMPTY(&ngsocklist)) && (typestruct.refs == 0)) { 1176 /* Unregister protocol domain XXX can't do this yet.. */ 1177 if ((error = net_rm_domain(&ngdomain)) != 0) 1178 break; 1179 mtx_destroy(&ngsocketlist_mtx); 1180 } else 1181 #endif 1182 error = EBUSY; 1183 break; 1184 default: 1185 error = EOPNOTSUPP; 1186 break; 1187 } 1188 return (error); 1189 } 1190 1191 SYSCTL_INT(_net_graph, OID_AUTO, family, CTLFLAG_RD, 0, AF_NETGRAPH, ""); 1192 SYSCTL_NODE(_net_graph, OID_AUTO, data, CTLFLAG_RW, 0, "DATA"); 1193 SYSCTL_INT(_net_graph_data, OID_AUTO, proto, CTLFLAG_RD, 0, NG_DATA, ""); 1194 SYSCTL_NODE(_net_graph, OID_AUTO, control, CTLFLAG_RW, 0, "CONTROL"); 1195 SYSCTL_INT(_net_graph_control, OID_AUTO, proto, CTLFLAG_RD, 0, NG_CONTROL, ""); 1196 1197