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