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