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 error = linker_load_module(NULL, filename, NULL, NULL, &lf); 304 if (error != 0) { 305 FREE(msg, M_NETGRAPH_MSG); 306 goto release; 307 } 308 lf->userrefs++; 309 310 /* Try again, as now the type should have linked itself in */ 311 if ((type = ng_findtype(mkp->type)) == NULL) { 312 FREE(msg, M_NETGRAPH_MSG); 313 error = ENXIO; 314 goto release; 315 } 316 } 317 } 318 319 /* The callee will free the msg when done. The path is our business. */ 320 NG_SEND_MSG_PATH(error, pcbp->sockdata->node, msg, path, 0); 321 #endif 322 release: 323 if (path != NULL) 324 FREE(path, M_NETGRAPH_PATH); 325 if (control != NULL) 326 m_freem(control); 327 if (m != NULL) 328 m_freem(m); 329 return (error); 330 } 331 332 static int 333 ngc_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 334 { 335 struct ngpcb *const pcbp = sotongpcb(so); 336 337 if (pcbp == 0) 338 return (EINVAL); 339 return (ng_bind(nam, pcbp)); 340 } 341 342 static int 343 ngc_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 344 { 345 printf(" program tried to connect control socket to remote node\n "); 346 /* 347 * At this time refuse to do this.. it used to 348 * do something but it was undocumented and not used. 349 */ 350 return (EINVAL); 351 } 352 353 /*************************************************************** 354 Data sockets 355 ***************************************************************/ 356 357 static int 358 ngd_attach(struct socket *so, int proto, struct thread *td) 359 { 360 struct ngpcb *const pcbp = sotongpcb(so); 361 362 if (pcbp != NULL) 363 return (EISCONN); 364 return (ng_attach_data(so)); 365 } 366 367 static int 368 ngd_detach(struct socket *so) 369 { 370 struct ngpcb *const pcbp = sotongpcb(so); 371 372 if (pcbp == NULL) 373 return (EINVAL); 374 ng_detach_common(pcbp, NG_DATA); 375 return (0); 376 } 377 378 static int 379 ngd_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, 380 struct mbuf *control, struct thread *td) 381 { 382 struct ngpcb *const pcbp = sotongpcb(so); 383 struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr; 384 int len, error; 385 hook_p hook = NULL; 386 char hookname[NG_HOOKSIZ]; 387 388 if ((pcbp == NULL) || (control != NULL)) { 389 error = EINVAL; 390 goto release; 391 } 392 if (pcbp->sockdata == NULL) { 393 error = ENOTCONN; 394 goto release; 395 } 396 /* 397 * If the user used any of these ways to not specify an address 398 * then handle specially. 399 */ 400 if ((sap == NULL) 401 || ((len = sap->sg_len - 2) <= 0) 402 || (*sap->sg_data == '\0')) { 403 if (NG_NODE_NUMHOOKS(pcbp->sockdata->node) != 1) { 404 error = EDESTADDRREQ; 405 goto release; 406 } 407 /* 408 * if exactly one hook exists, just use it. 409 * Special case to allow write(2) to work on an ng_socket. 410 */ 411 hook = LIST_FIRST(&pcbp->sockdata->node->nd_hooks); 412 } else { 413 if (len >= NG_HOOKSIZ) { 414 error = EINVAL; 415 goto release; 416 } 417 418 /* 419 * chop off the sockaddr header, and make sure it's NUL 420 * terminated 421 */ 422 bcopy(sap->sg_data, hookname, len); 423 hookname[len] = '\0'; 424 425 /* Find the correct hook from 'hookname' */ 426 LIST_FOREACH(hook, &pcbp->sockdata->node->nd_hooks, hk_hooks) { 427 if (strcmp(hookname, NG_HOOK_NAME(hook)) == 0) { 428 break; 429 } 430 } 431 if (hook == NULL) { 432 error = EHOSTUNREACH; 433 } 434 } 435 436 /* Send data (OK if hook is NULL) */ 437 NG_SEND_DATA_ONLY(error, hook, m); /* makes m NULL */ 438 439 release: 440 if (control != NULL) 441 m_freem(control); 442 if (m != NULL) 443 m_freem(m); 444 return (error); 445 } 446 447 static int 448 ngd_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 449 { 450 struct ngpcb *const pcbp = sotongpcb(so); 451 452 if (pcbp == 0) 453 return (EINVAL); 454 return (ng_connect_data(nam, pcbp)); 455 } 456 457 /* 458 * Used for both data and control sockets 459 */ 460 static int 461 ng_setsockaddr(struct socket *so, struct sockaddr **addr) 462 { 463 struct ngpcb *pcbp; 464 struct sockaddr_ng *sg; 465 int sg_len, namelen, s; 466 467 /* Why isn't sg_data a `char[1]' ? :-( */ 468 sg_len = sizeof(struct sockaddr_ng) - sizeof(sg->sg_data) + 1; 469 470 s = splnet(); 471 pcbp = sotongpcb(so); 472 if ((pcbp == NULL) || (pcbp->sockdata == NULL)) { 473 splx(s); 474 return (EINVAL); 475 } 476 477 namelen = 0; /* silence compiler ! */ 478 if ( NG_NODE_HAS_NAME(pcbp->sockdata->node)) 479 sg_len += namelen = strlen(NG_NODE_NAME(pcbp->sockdata->node)); 480 481 MALLOC(sg, struct sockaddr_ng *, sg_len, M_SONAME, M_WAITOK | M_ZERO); 482 483 if (NG_NODE_HAS_NAME(pcbp->sockdata->node)) 484 bcopy(NG_NODE_NAME(pcbp->sockdata->node), sg->sg_data, namelen); 485 splx(s); 486 487 sg->sg_len = sg_len; 488 sg->sg_family = AF_NETGRAPH; 489 *addr = (struct sockaddr *)sg; 490 491 return (0); 492 } 493 494 /* 495 * Attach a socket to it's protocol specific partner. 496 * For a control socket, actually create a netgraph node and attach 497 * to it as well. 498 */ 499 500 static int 501 ng_attach_cntl(struct socket *so) 502 { 503 struct ngsock *privdata; 504 struct ngpcb *pcbp; 505 int error; 506 507 /* Setup protocol control block */ 508 if ((error = ng_attach_common(so, NG_CONTROL)) != 0) 509 return (error); 510 pcbp = sotongpcb(so); 511 512 /* Allocate node private info */ 513 MALLOC(privdata, struct ngsock *, 514 sizeof(*privdata), M_NETGRAPH_SOCK, M_WAITOK | M_ZERO); 515 if (privdata == NULL) { 516 ng_detach_common(pcbp, NG_CONTROL); 517 return (ENOMEM); 518 } 519 520 /* Make the generic node components */ 521 if ((error = ng_make_node_common(&typestruct, &privdata->node)) != 0) { 522 FREE(privdata, M_NETGRAPH_SOCK); 523 ng_detach_common(pcbp, NG_CONTROL); 524 return (error); 525 } 526 NG_NODE_SET_PRIVATE(privdata->node, privdata); 527 528 /* Link the pcb and the node private data */ 529 privdata->ctlsock = pcbp; 530 pcbp->sockdata = privdata; 531 privdata->refs++; 532 return (0); 533 } 534 535 static int 536 ng_attach_data(struct socket *so) 537 { 538 return(ng_attach_common(so, NG_DATA)); 539 } 540 541 /* 542 * Set up a socket protocol control block. 543 * This code is shared between control and data sockets. 544 */ 545 static int 546 ng_attach_common(struct socket *so, int type) 547 { 548 struct ngpcb *pcbp; 549 int error; 550 551 /* Standard socket setup stuff */ 552 error = soreserve(so, ngpdg_sendspace, ngpdg_recvspace); 553 if (error) 554 return (error); 555 556 /* Allocate the pcb */ 557 MALLOC(pcbp, struct ngpcb *, sizeof(*pcbp), M_PCB, M_WAITOK | M_ZERO); 558 if (pcbp == NULL) 559 return (ENOMEM); 560 pcbp->type = type; 561 562 /* Link the pcb and the socket */ 563 so->so_pcb = (caddr_t) pcbp; 564 pcbp->ng_socket = so; 565 566 /* Add the socket to linked list */ 567 mtx_lock(&ngsocketlist_mtx); 568 LIST_INSERT_HEAD(&ngsocklist, pcbp, socks); 569 mtx_unlock(&ngsocketlist_mtx); 570 return (0); 571 } 572 573 /* 574 * Disassociate the socket from it's protocol specific 575 * partner. If it's attached to a node's private data structure, 576 * then unlink from that too. If we were the last socket attached to it, 577 * then shut down the entire node. Shared code for control and data sockets. 578 */ 579 static void 580 ng_detach_common(struct ngpcb *pcbp, int which) 581 { 582 struct ngsock *priv; 583 584 if (pcbp->sockdata) { 585 priv = pcbp->sockdata; 586 pcbp->sockdata = NULL; 587 switch (which) { 588 case NG_CONTROL: 589 priv->ctlsock = NULL; 590 break; 591 case NG_DATA: 592 priv->datasock = NULL; 593 break; 594 default: 595 panic(__func__); 596 } 597 if ((--priv->refs == 0) && (priv->node != NULL)) 598 ng_rmnode_self(priv->node); 599 } 600 pcbp->ng_socket->so_pcb = NULL; 601 pcbp->ng_socket = NULL; 602 mtx_lock(&ngsocketlist_mtx); 603 LIST_REMOVE(pcbp, socks); 604 mtx_unlock(&ngsocketlist_mtx); 605 FREE(pcbp, M_PCB); 606 } 607 608 #ifdef NOTYET 609 /* 610 * File descriptors can be passed into an AF_NETGRAPH socket. 611 * Note, that file descriptors cannot be passed OUT. 612 * Only character device descriptors are accepted. 613 * Character devices are useful to connect a graph to a device, 614 * which after all is the purpose of this whole system. 615 */ 616 static int 617 ng_internalize(struct mbuf *control, struct thread *td) 618 { 619 const struct cmsghdr *cm = mtod(control, const struct cmsghdr *); 620 struct file *fp; 621 struct vnode *vn; 622 int oldfds; 623 int fd; 624 625 if (cm->cmsg_type != SCM_RIGHTS || cm->cmsg_level != SOL_SOCKET || 626 cm->cmsg_len != control->m_len) { 627 TRAP_ERROR; 628 return (EINVAL); 629 } 630 631 /* Check there is only one FD. XXX what would more than one signify? */ 632 oldfds = ((caddr_t)cm + cm->cmsg_len - (caddr_t)data) / sizeof (int); 633 if (oldfds != 1) { 634 TRAP_ERROR; 635 return (EINVAL); 636 } 637 638 /* Check that the FD given is legit. and change it to a pointer to a 639 * struct file. */ 640 fd = CMSG_DATA(cm); 641 if ((error = fget(td, fd, &fp)) != 0) 642 return (error); 643 644 /* Depending on what kind of resource it is, act differently. For 645 * devices, we treat it as a file. For an AF_NETGRAPH socket, 646 * shortcut straight to the node. */ 647 switch (fp->f_type) { 648 case DTYPE_VNODE: 649 vn = fp->f_data; 650 if (vn && (vn->v_type == VCHR)) { 651 /* for a VCHR, actually reference the FILE */ 652 fp->f_count++; 653 /* XXX then what :) */ 654 /* how to pass on to other modules? */ 655 } else { 656 fdrop(fp, td); 657 TRAP_ERROR; 658 return (EINVAL); 659 } 660 break; 661 default: 662 fdrop(fp, td); 663 TRAP_ERROR; 664 return (EINVAL); 665 } 666 fdrop(fp, td); 667 return (0); 668 } 669 #endif /* NOTYET */ 670 671 /* 672 * Connect the data socket to a named control socket node. 673 */ 674 static int 675 ng_connect_data(struct sockaddr *nam, struct ngpcb *pcbp) 676 { 677 struct sockaddr_ng *sap; 678 node_p farnode; 679 struct ngsock *priv; 680 int error; 681 item_p item; 682 683 /* If we are already connected, don't do it again */ 684 if (pcbp->sockdata != NULL) 685 return (EISCONN); 686 687 /* Find the target (victim) and check it doesn't already have a data 688 * socket. Also check it is a 'socket' type node. 689 * Use ng_package_data() and address_path() to do this. 690 */ 691 692 sap = (struct sockaddr_ng *) nam; 693 /* The item will hold the node reference */ 694 item = ng_package_data(NULL, NULL); 695 if (item == NULL) { 696 return (ENOMEM); 697 } 698 if ((error = ng_address_path(NULL, item, sap->sg_data, 0))) 699 return (error); /* item is freed on failure */ 700 701 /* 702 * Extract node from item and free item. Remember we now have 703 * a reference on the node. The item holds it for us. 704 * when we free the item we release the reference. 705 */ 706 farnode = item->el_dest; /* shortcut */ 707 if (strcmp(farnode->nd_type->name, NG_SOCKET_NODE_TYPE) != 0) { 708 NG_FREE_ITEM(item); /* drop the reference to the node */ 709 return (EINVAL); 710 } 711 priv = NG_NODE_PRIVATE(farnode); 712 if (priv->datasock != NULL) { 713 NG_FREE_ITEM(item); /* drop the reference to the node */ 714 return (EADDRINUSE); 715 } 716 717 /* 718 * Link the PCB and the private data struct. and note the extra 719 * reference. Drop the extra reference on the node. 720 */ 721 priv->datasock = pcbp; 722 pcbp->sockdata = priv; 723 priv->refs++; /* XXX possible race if it's being freed */ 724 NG_FREE_ITEM(item); /* drop the reference to the node */ 725 return (0); 726 } 727 728 /* 729 * Binding a socket means giving the corresponding node a name 730 */ 731 static int 732 ng_bind(struct sockaddr *nam, struct ngpcb *pcbp) 733 { 734 struct ngsock *const priv = pcbp->sockdata; 735 struct sockaddr_ng *const sap = (struct sockaddr_ng *) nam; 736 737 if (priv == NULL) { 738 TRAP_ERROR; 739 return (EINVAL); 740 } 741 if ((sap->sg_len < 4) 742 || (sap->sg_len > (NG_NODESIZ + 2)) 743 || (sap->sg_data[0] == '\0') 744 || (sap->sg_data[sap->sg_len - 3] != '\0')) { 745 TRAP_ERROR; 746 return (EINVAL); 747 } 748 return (ng_name_node(priv->node, sap->sg_data)); 749 } 750 751 /* 752 * Take a message and pass it up to the control socket associated 753 * with the node. 754 */ 755 static int 756 ship_msg(struct ngpcb *pcbp, struct ng_mesg *msg, struct sockaddr_ng *addr) 757 { 758 struct socket *const so = pcbp->ng_socket; 759 struct mbuf *mdata; 760 int msglen; 761 762 /* Copy the message itself into an mbuf chain */ 763 msglen = sizeof(struct ng_mesg) + msg->header.arglen; 764 mdata = m_devget((caddr_t) msg, msglen, 0, NULL, NULL); 765 766 /* Here we free the message, as we are the end of the line. 767 * We need to do that regardless of whether we got mbufs. */ 768 NG_FREE_MSG(msg); 769 770 if (mdata == NULL) { 771 TRAP_ERROR; 772 return (ENOBUFS); 773 } 774 775 /* Send it up to the socket */ 776 if (sbappendaddr(&so->so_rcv, 777 (struct sockaddr *) addr, mdata, NULL) == 0) { 778 TRAP_ERROR; 779 m_freem(mdata); 780 return (ENOBUFS); 781 } 782 sorwakeup(so); 783 return (0); 784 } 785 786 /* 787 * You can only create new nodes from the socket end of things. 788 */ 789 static int 790 ngs_constructor(node_p nodep) 791 { 792 return (EINVAL); 793 } 794 795 /* 796 * We allow any hook to be connected to the node. 797 * There is no per-hook private information though. 798 */ 799 static int 800 ngs_newhook(node_p node, hook_p hook, const char *name) 801 { 802 NG_HOOK_SET_PRIVATE(hook, NG_NODE_PRIVATE(node)); 803 return (0); 804 } 805 806 /* 807 * if only one hook, allow read(2) and write(2) to work. 808 */ 809 static int 810 ngs_connect(hook_p hook) 811 { 812 node_p node = NG_HOOK_NODE(hook); 813 struct ngsock *priv = NG_NODE_PRIVATE(node); 814 815 if ((priv->datasock) 816 && (priv->datasock->ng_socket)) { 817 if (NG_NODE_NUMHOOKS(node) == 1) { 818 priv->datasock->ng_socket->so_state |= SS_ISCONNECTED; 819 } else { 820 priv->datasock->ng_socket->so_state &= ~SS_ISCONNECTED; 821 } 822 } 823 return (0); 824 } 825 826 /* 827 * Incoming messages get passed up to the control socket. 828 * Unless they are for us specifically (socket_type) 829 */ 830 static int 831 ngs_rcvmsg(node_p node, item_p item, hook_p lasthook) 832 { 833 struct ngsock *const priv = NG_NODE_PRIVATE(node); 834 struct ngpcb *const pcbp = priv->ctlsock; 835 struct sockaddr_ng *addr; 836 int addrlen; 837 int error = 0; 838 struct ng_mesg *msg; 839 ng_ID_t retaddr = NGI_RETADDR(item); 840 char retabuf[32]; 841 842 NGI_GET_MSG(item, msg); 843 NG_FREE_ITEM(item); /* we have all we need */ 844 845 /* Only allow mesgs to be passed if we have the control socket. 846 * Data sockets can only support the generic messages. */ 847 if (pcbp == NULL) { 848 TRAP_ERROR; 849 return (EINVAL); 850 } 851 #ifdef TRACE_MESSAGES 852 printf("[%x]:---------->[socket]: c=<%d>cmd=%x(%s) f=%x #%d\n", 853 retaddr, 854 msg->header.typecookie, 855 msg->header.cmd, 856 msg->header.cmdstr, 857 msg->header.flags, 858 msg->header.token); 859 860 #endif 861 862 if (msg->header.typecookie == NGM_SOCKET_COOKIE) { 863 switch (msg->header.cmd) { 864 case NGM_SOCK_CMD_NOLINGER: 865 priv->flags |= NGS_FLAG_NOLINGER; 866 break; 867 case NGM_SOCK_CMD_LINGER: 868 priv->flags &= ~NGS_FLAG_NOLINGER; 869 break; 870 default: 871 error = EINVAL; /* unknown command */ 872 } 873 /* Free the message and return */ 874 NG_FREE_MSG(msg); 875 return(error); 876 877 } 878 /* Get the return address into a sockaddr */ 879 sprintf(retabuf,"[%x]:", retaddr); 880 addrlen = strlen(retabuf); 881 MALLOC(addr, struct sockaddr_ng *, addrlen + 4, M_NETGRAPH_PATH, M_NOWAIT); 882 if (addr == NULL) { 883 TRAP_ERROR; 884 return (ENOMEM); 885 } 886 addr->sg_len = addrlen + 3; 887 addr->sg_family = AF_NETGRAPH; 888 bcopy(retabuf, addr->sg_data, addrlen); 889 addr->sg_data[addrlen] = '\0'; 890 891 /* Send it up */ 892 error = ship_msg(pcbp, msg, addr); 893 FREE(addr, M_NETGRAPH_PATH); 894 return (error); 895 } 896 897 /* 898 * Receive data on a hook 899 */ 900 static int 901 ngs_rcvdata(hook_p hook, item_p item) 902 { 903 struct ngsock *const priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 904 struct ngpcb *const pcbp = priv->datasock; 905 struct socket *so; 906 struct sockaddr_ng *addr; 907 char *addrbuf[NG_HOOKSIZ + 4]; 908 int addrlen; 909 struct mbuf *m; 910 911 NGI_GET_M(item, m); 912 NG_FREE_ITEM(item); 913 /* If there is no data socket, black-hole it */ 914 if (pcbp == NULL) { 915 NG_FREE_M(m); 916 return (0); 917 } 918 so = pcbp->ng_socket; 919 920 /* Get the return address into a sockaddr. */ 921 addrlen = strlen(NG_HOOK_NAME(hook)); /* <= NG_HOOKSIZ - 1 */ 922 addr = (struct sockaddr_ng *) addrbuf; 923 addr->sg_len = addrlen + 3; 924 addr->sg_family = AF_NETGRAPH; 925 bcopy(NG_HOOK_NAME(hook), addr->sg_data, addrlen); 926 addr->sg_data[addrlen] = '\0'; 927 928 /* Try to tell the socket which hook it came in on */ 929 if (sbappendaddr(&so->so_rcv, (struct sockaddr *) addr, m, NULL) == 0) { 930 m_freem(m); 931 TRAP_ERROR; 932 return (ENOBUFS); 933 } 934 sorwakeup(so); 935 return (0); 936 } 937 938 /* 939 * Hook disconnection 940 * 941 * For this type, removal of the last link destroys the node 942 * if the NOLINGER flag is set. 943 */ 944 static int 945 ngs_disconnect(hook_p hook) 946 { 947 node_p node = NG_HOOK_NODE(hook); 948 struct ngsock *const priv = NG_NODE_PRIVATE(node); 949 950 if ((priv->datasock) 951 && (priv->datasock->ng_socket)) { 952 if (NG_NODE_NUMHOOKS(node) == 1) { 953 priv->datasock->ng_socket->so_state |= SS_ISCONNECTED; 954 } else { 955 priv->datasock->ng_socket->so_state &= ~SS_ISCONNECTED; 956 } 957 } 958 959 if ((priv->flags & NGS_FLAG_NOLINGER ) 960 && (NG_NODE_NUMHOOKS(node) == 0) 961 && (NG_NODE_IS_VALID(node))) { 962 ng_rmnode_self(node); 963 } 964 return (0); 965 } 966 967 /* 968 * Do local shutdown processing. 969 * In this case, that involves making sure the socket 970 * knows we should be shutting down. 971 */ 972 static int 973 ngs_shutdown(node_p node) 974 { 975 struct ngsock *const priv = NG_NODE_PRIVATE(node); 976 struct ngpcb *const dpcbp = priv->datasock; 977 struct ngpcb *const pcbp = priv->ctlsock; 978 979 if (dpcbp != NULL) { 980 soisdisconnected(dpcbp->ng_socket); 981 dpcbp->sockdata = NULL; 982 priv->datasock = NULL; 983 priv->refs--; 984 } 985 if (pcbp != NULL) { 986 soisdisconnected(pcbp->ng_socket); 987 pcbp->sockdata = NULL; 988 priv->ctlsock = NULL; 989 priv->refs--; 990 } 991 NG_NODE_SET_PRIVATE(node, NULL); 992 NG_NODE_UNREF(node); 993 FREE(priv, M_NETGRAPH_SOCK); 994 return (0); 995 } 996 997 static int 998 dummy_disconnect(struct socket *so) 999 { 1000 return (0); 1001 } 1002 /* 1003 * Control and data socket type descriptors 1004 */ 1005 1006 static struct pr_usrreqs ngc_usrreqs = { 1007 NULL, /* abort */ 1008 pru_accept_notsupp, 1009 ngc_attach, 1010 ngc_bind, 1011 ngc_connect, 1012 pru_connect2_notsupp, 1013 pru_control_notsupp, 1014 ngc_detach, 1015 dummy_disconnect, /* disconnect */ 1016 pru_listen_notsupp, 1017 NULL, /* setpeeraddr */ 1018 pru_rcvd_notsupp, 1019 pru_rcvoob_notsupp, 1020 ngc_send, 1021 pru_sense_null, 1022 NULL, /* shutdown */ 1023 ng_setsockaddr, 1024 sosend, 1025 soreceive, 1026 sopoll, 1027 pru_sosetlabel_null 1028 }; 1029 1030 static struct pr_usrreqs ngd_usrreqs = { 1031 NULL, /* abort */ 1032 pru_accept_notsupp, 1033 ngd_attach, 1034 NULL, /* bind */ 1035 ngd_connect, 1036 pru_connect2_notsupp, 1037 pru_control_notsupp, 1038 ngd_detach, 1039 dummy_disconnect, /* disconnect */ 1040 pru_listen_notsupp, 1041 NULL, /* setpeeraddr */ 1042 pru_rcvd_notsupp, 1043 pru_rcvoob_notsupp, 1044 ngd_send, 1045 pru_sense_null, 1046 NULL, /* shutdown */ 1047 ng_setsockaddr, 1048 sosend, 1049 soreceive, 1050 sopoll, 1051 pru_sosetlabel_null 1052 }; 1053 1054 /* 1055 * Definitions of protocols supported in the NETGRAPH domain. 1056 */ 1057 1058 extern struct domain ngdomain; /* stop compiler warnings */ 1059 1060 static struct protosw ngsw[] = { 1061 { 1062 SOCK_DGRAM, /* protocol type */ 1063 &ngdomain, /* backpointer to domain */ 1064 NG_CONTROL, 1065 PR_ATOMIC | PR_ADDR /* | PR_RIGHTS */, /* flags */ 1066 0, 0, 0, 0, /* input, output, ctlinput, ctloutput */ 1067 NULL, /* ousrreq */ 1068 0, 0, 0, 0, /* init, fasttimeo, slowtimo, drain */ 1069 &ngc_usrreqs, /* usrreq table (above) */ 1070 /*{NULL}*/ /* pffh (protocol filter head?) */ 1071 }, 1072 { 1073 SOCK_DGRAM, /* protocol type */ 1074 &ngdomain, /* backpointer to domain */ 1075 NG_DATA, 1076 PR_ATOMIC | PR_ADDR, /* flags */ 1077 0, 0, 0, 0, /* input, output, ctlinput, ctloutput */ 1078 NULL, /* ousrreq() */ 1079 0, 0, 0, 0, /* init, fasttimeo, slowtimo, drain */ 1080 &ngd_usrreqs, /* usrreq table (above) */ 1081 /*{NULL}*/ /* pffh (protocol filter head?) */ 1082 } 1083 }; 1084 1085 struct domain ngdomain = { 1086 AF_NETGRAPH, 1087 "netgraph", 1088 NULL, /* init() */ 1089 NULL, /* externalise() */ 1090 NULL, /* dispose() */ 1091 ngsw, /* protosw entry */ 1092 &ngsw[sizeof(ngsw) / sizeof(ngsw[0])], /* Number of protosw entries */ 1093 NULL, /* next domain in list */ 1094 NULL, /* rtattach() */ 1095 0, /* arg to rtattach in bits */ 1096 0 /* maxrtkey */ 1097 }; 1098 1099 /* 1100 * Handle loading and unloading for this node type 1101 * This is to handle auxiliary linkages (e.g protocol domain addition). 1102 */ 1103 static int 1104 ngs_mod_event(module_t mod, int event, void *data) 1105 { 1106 int error = 0; 1107 1108 switch (event) { 1109 case MOD_LOAD: 1110 /* Register protocol domain */ 1111 net_add_domain(&ngdomain); 1112 break; 1113 case MOD_UNLOAD: 1114 /* Insure there are no open netgraph sockets */ 1115 if (!LIST_EMPTY(&ngsocklist)) { 1116 error = EBUSY; 1117 break; 1118 } 1119 1120 #ifdef NOTYET 1121 if ((LIST_EMPTY(&ngsocklist)) && (typestruct.refs == 0)) { 1122 /* Unregister protocol domain XXX can't do this yet.. */ 1123 if ((error = net_rm_domain(&ngdomain)) != 0) 1124 break; 1125 } else 1126 #endif 1127 error = EBUSY; 1128 break; 1129 default: 1130 error = EOPNOTSUPP; 1131 break; 1132 } 1133 return (error); 1134 } 1135 1136 SYSCTL_INT(_net_graph, OID_AUTO, family, CTLFLAG_RD, 0, AF_NETGRAPH, ""); 1137 SYSCTL_NODE(_net_graph, OID_AUTO, data, CTLFLAG_RW, 0, "DATA"); 1138 SYSCTL_INT(_net_graph_data, OID_AUTO, proto, CTLFLAG_RD, 0, NG_DATA, ""); 1139 SYSCTL_NODE(_net_graph, OID_AUTO, control, CTLFLAG_RW, 0, "CONTROL"); 1140 SYSCTL_INT(_net_graph_control, OID_AUTO, proto, CTLFLAG_RD, 0, NG_CONTROL, ""); 1141 1142