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