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