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