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