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