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