1 2 /* 3 * ng_socket.c 4 * 5 * Copyright (c) 1996-1999 Whistle Communications, Inc. 6 * All rights reserved. 7 * 8 * Subject to the following obligations and disclaimer of warranty, use and 9 * redistribution of this software, in source or object code forms, with or 10 * without modifications are expressly permitted by Whistle Communications; 11 * provided, however, that: 12 * 1. Any and all reproductions of the source or object code must include the 13 * copyright notice above and the following disclaimer of warranties; and 14 * 2. No rights are granted, in any manner or form, to use Whistle 15 * Communications, Inc. trademarks, including the mark "WHISTLE 16 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as 17 * such appears in the above copyright notice or in the software. 18 * 19 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND 20 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO 21 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, 22 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF 23 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. 24 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY 25 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS 26 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. 27 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES 28 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING 29 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 30 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR 31 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY 32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 34 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY 35 * OF SUCH DAMAGE. 36 * 37 * Author: Julian Elischer <julian@freebsd.org> 38 * 39 * $FreeBSD: src/sys/netgraph/ng_socket.c,v 1.11.2.6 2002/07/02 22:17:18 archie Exp $ 40 * $DragonFly: src/sys/netgraph/socket/ng_socket.c,v 1.17 2008/11/01 04:22:15 sephe 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/systm.h> 54 #include <sys/proc.h> 55 #include <sys/priv.h> 56 #include <sys/domain.h> 57 #include <sys/errno.h> 58 #include <sys/kernel.h> 59 #include <sys/filedesc.h> 60 #include <sys/malloc.h> 61 #include <sys/queue.h> 62 #include <sys/mbuf.h> 63 #include <sys/protosw.h> 64 #include <sys/socket.h> 65 #include <sys/socketvar.h> 66 #include <sys/sysctl.h> 67 #ifdef NOTYET 68 #include <sys/vnode.h> 69 #endif 70 71 #include <sys/thread2.h> 72 #include <sys/socketvar2.h> 73 #include <sys/msgport2.h> 74 75 #include <netgraph/ng_message.h> 76 #include <netgraph/netgraph.h> 77 #include "ng_socketvar.h" 78 #include "ng_socket.h" 79 80 /* 81 * It's Ascii-art time! 82 * +-------------+ +-------------+ 83 * |socket (ctl)| |socket (data)| 84 * +-------------+ +-------------+ 85 * ^ ^ 86 * | | 87 * v v 88 * +-----------+ +-----------+ 89 * |pcb (ctl)| |pcb (data)| 90 * +-----------+ +-----------+ 91 * ^ ^ 92 * | | 93 * v v 94 * +--------------------------+ 95 * | Socket type private | 96 * | data | 97 * +--------------------------+ 98 * ^ 99 * | 100 * v 101 * +----------------+ 102 * | struct ng_node | 103 * +----------------+ 104 */ 105 106 /* Netgraph node methods */ 107 static ng_constructor_t ngs_constructor; 108 static ng_rcvmsg_t ngs_rcvmsg; 109 static ng_shutdown_t ngs_rmnode; 110 static ng_newhook_t ngs_newhook; 111 static ng_rcvdata_t ngs_rcvdata; 112 static ng_disconnect_t ngs_disconnect; 113 114 /* Internal methods */ 115 static int ng_attach_data(struct socket *so); 116 static int ng_attach_cntl(struct socket *so); 117 static int ng_attach_common(struct socket *so, int type); 118 static void ng_detach_common(struct ngpcb *pcbp, int type); 119 /*static int ng_internalize(struct mbuf *m, struct thread *td); */ 120 121 static int ng_connect_data(struct sockaddr *nam, struct ngpcb *pcbp); 122 static int ng_connect_cntl(struct sockaddr *nam, struct ngpcb *pcbp); 123 static int ng_bind(struct sockaddr *nam, struct ngpcb *pcbp); 124 125 static int ngs_mod_event(module_t mod, int event, void *data); 126 static int ship_msg(struct ngpcb *pcbp, struct ng_mesg *msg, 127 struct sockaddr_ng *addr); 128 129 /* Netgraph type descriptor */ 130 static struct ng_type typestruct = { 131 NG_VERSION, 132 NG_SOCKET_NODE_TYPE, 133 ngs_mod_event, 134 ngs_constructor, 135 ngs_rcvmsg, 136 ngs_rmnode, 137 ngs_newhook, 138 NULL, 139 NULL, 140 ngs_rcvdata, 141 ngs_rcvdata, 142 ngs_disconnect, 143 NULL 144 }; 145 NETGRAPH_INIT(socket, &typestruct); 146 147 /* Buffer space */ 148 static u_long ngpdg_sendspace = 20 * 1024; /* really max datagram size */ 149 static u_long ngpdg_recvspace = 20 * 1024; 150 151 /* List of all sockets */ 152 LIST_HEAD(, ngpcb) ngsocklist; 153 154 #define sotongpcb(so) ((struct ngpcb *)(so)->so_pcb) 155 156 /* If getting unexplained errors returned, set this to "Debugger("X"); */ 157 #ifndef TRAP_ERROR 158 #define TRAP_ERROR 159 #endif 160 161 /*************************************************************** 162 Control sockets 163 ***************************************************************/ 164 165 static void 166 ngc_attach(netmsg_t msg) 167 { 168 struct socket *so = msg->attach.base.nm_so; 169 struct pru_attach_info *ai = msg->attach.nm_ai; 170 struct ngpcb *const pcbp = sotongpcb(so); 171 int error; 172 173 if (priv_check_cred(ai->p_ucred, PRIV_ROOT, NULL_CRED_OKAY) != 0) 174 error = EPERM; 175 else if (pcbp != NULL) 176 error = EISCONN; 177 else 178 error = ng_attach_cntl(so); 179 lwkt_replymsg(&msg->attach.base.lmsg, error); 180 } 181 182 static void 183 ngc_detach(netmsg_t msg) 184 { 185 struct socket *so = msg->detach.base.nm_so; 186 struct ngpcb *const pcbp = sotongpcb(so); 187 int error; 188 189 if (pcbp) { 190 ng_detach_common(pcbp, NG_CONTROL); 191 error = 0; 192 } else { 193 error = EINVAL; 194 } 195 lwkt_replymsg(&msg->detach.base.lmsg, error); 196 } 197 198 static void 199 ngc_send(netmsg_t msg) 200 { 201 struct socket *so = msg->send.base.nm_so; 202 struct mbuf *m = msg->send.nm_m; 203 struct sockaddr *addr = msg->send.nm_addr; 204 struct mbuf *control = msg->send.nm_control; 205 struct ngpcb *const pcbp = sotongpcb(so); 206 struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr; 207 struct ng_mesg *resp; 208 struct mbuf *m0; 209 char *xmsg, *path = NULL; 210 int len; 211 int error = 0; 212 213 if (pcbp == NULL) { 214 error = EINVAL; 215 goto release; 216 } 217 #ifdef NOTYET 218 if (control && (error = ng_internalize(control, p))) { 219 if (pcbp->sockdata == NULL) { 220 error = ENOTCONN; 221 goto release; 222 } 223 } 224 #else /* NOTYET */ 225 if (control) { 226 error = EINVAL; 227 goto release; 228 } 229 #endif /* NOTYET */ 230 231 /* Require destination as there may be >= 1 hooks on this node */ 232 if (addr == NULL) { 233 error = EDESTADDRREQ; 234 goto release; 235 } 236 237 /* Allocate an expendable buffer for the path, chop off 238 * the sockaddr header, and make sure it's NUL terminated */ 239 len = sap->sg_len - 2; 240 MALLOC(path, char *, len + 1, M_NETGRAPH, M_WAITOK); 241 bcopy(sap->sg_data, path, len); 242 path[len] = '\0'; 243 244 /* Move the actual message out of mbufs into a linear buffer. 245 * Start by adding up the size of the data. (could use mh_len?) */ 246 for (len = 0, m0 = m; m0 != NULL; m0 = m0->m_next) 247 len += m0->m_len; 248 249 /* Move the data into a linear buffer as well. Messages are not 250 * delivered in mbufs. */ 251 MALLOC(xmsg, char *, len + 1, M_NETGRAPH, M_WAITOK); 252 m_copydata(m, 0, len, xmsg); 253 254 /* The callee will free the xmsg when done. The addr is our business. */ 255 error = ng_send_msg(pcbp->sockdata->node, 256 (struct ng_mesg *) xmsg, path, &resp); 257 258 /* If the callee responded with a synchronous response, then put it 259 * back on the receive side of the socket; sap is source address. */ 260 if (error == 0 && resp != NULL) 261 error = ship_msg(pcbp, resp, sap); 262 263 release: 264 if (path != NULL) 265 FREE(path, M_NETGRAPH); 266 if (control != NULL) 267 m_freem(control); 268 if (m != NULL) 269 m_freem(m); 270 lwkt_replymsg(&msg->send.base.lmsg, error); 271 } 272 273 static void 274 ngc_bind(netmsg_t msg) 275 { 276 struct socket *so = msg->bind.base.nm_so; 277 struct sockaddr *nam = msg->bind.nm_nam; 278 struct ngpcb *const pcbp = sotongpcb(so); 279 int error; 280 281 if (pcbp) 282 error = ng_bind(nam, pcbp); 283 else 284 error = EINVAL; 285 lwkt_replymsg(&msg->bind.base.lmsg, error); 286 } 287 288 static void 289 ngc_connect(netmsg_t msg) 290 { 291 struct socket *so = msg->connect.base.nm_so; 292 struct sockaddr *nam = msg->connect.nm_nam; 293 struct ngpcb *const pcbp = sotongpcb(so); 294 int error; 295 296 if (pcbp) 297 error = ng_connect_cntl(nam, pcbp); 298 else 299 error = EINVAL; 300 lwkt_replymsg(&msg->connect.base.lmsg, error); 301 } 302 303 /*************************************************************** 304 Data sockets 305 ***************************************************************/ 306 307 static void 308 ngd_attach(netmsg_t msg) 309 { 310 struct socket *so = msg->attach.base.nm_so; 311 struct ngpcb *const pcbp = sotongpcb(so); 312 int error; 313 314 if (pcbp) 315 error = EISCONN; 316 else 317 error = ng_attach_data(so); 318 lwkt_replymsg(&msg->connect.base.lmsg, error); 319 } 320 321 static void 322 ngd_detach(netmsg_t msg) 323 { 324 struct socket *so = msg->detach.base.nm_so; 325 struct ngpcb *const pcbp = sotongpcb(so); 326 int error; 327 328 if (pcbp) { 329 ng_detach_common(pcbp, NG_DATA); 330 error = 0; 331 } else { 332 error = EINVAL; 333 } 334 lwkt_replymsg(&msg->detach.base.lmsg, error); 335 } 336 337 static void 338 ngd_send(netmsg_t msg) 339 { 340 struct socket *so = msg->send.base.nm_so; 341 struct mbuf *m = msg->send.nm_m; 342 struct sockaddr *addr = msg->send.nm_addr; 343 struct mbuf *control = msg->send.nm_control; 344 struct ngpcb *const pcbp = sotongpcb(so); 345 struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr; 346 meta_p mp = NULL; 347 int len, error; 348 hook_p hook = NULL; 349 char hookname[NG_HOOKSIZ]; 350 351 if ((pcbp == NULL) || (control != NULL)) { 352 error = EINVAL; 353 goto release; 354 } 355 if (pcbp->sockdata == NULL) { 356 error = ENOTCONN; 357 goto release; 358 } 359 /* 360 * If the user used any of these ways to not specify an address 361 * then handle specially. 362 */ 363 if ((sap == NULL) 364 || ((len = sap->sg_len - 2) <= 0) 365 || (*sap->sg_data == '\0')) { 366 if (pcbp->sockdata->node->numhooks != 1) { 367 error = EDESTADDRREQ; 368 goto release; 369 } 370 /* 371 * if exactly one hook exists, just use it. 372 * Special case to allow write(2) to work on an ng_socket. 373 */ 374 hook = LIST_FIRST(&pcbp->sockdata->node->hooks); 375 } else { 376 if (len >= NG_HOOKSIZ) { 377 error = EINVAL; 378 goto release; 379 } 380 381 /* 382 * chop off the sockaddr header, and make sure it's NUL 383 * terminated 384 */ 385 bcopy(sap->sg_data, hookname, len); 386 hookname[len] = '\0'; 387 388 /* Find the correct hook from 'hookname' */ 389 LIST_FOREACH(hook, &pcbp->sockdata->node->hooks, hooks) { 390 if (strcmp(hookname, hook->name) == 0) 391 break; 392 } 393 if (hook == NULL) 394 error = EHOSTUNREACH; 395 } 396 397 /* Send data (OK if hook is NULL) */ 398 NG_SEND_DATA(error, hook, m, mp); /* makes m NULL */ 399 400 release: 401 if (control != NULL) 402 m_freem(control); 403 if (m != NULL) 404 m_freem(m); 405 lwkt_replymsg(&msg->send.base.lmsg, error); 406 } 407 408 static void 409 ngd_connect(netmsg_t msg) 410 { 411 struct socket *so = msg->connect.base.nm_so; 412 struct sockaddr *nam = msg->connect.nm_nam; 413 struct ngpcb *const pcbp = sotongpcb(so); 414 int error; 415 416 if (pcbp) { 417 error = ng_connect_data(nam, pcbp); 418 } else { 419 error = EINVAL; 420 } 421 lwkt_replymsg(&msg->connect.base.lmsg, error); 422 } 423 424 /* 425 * Used for both data and control sockets 426 */ 427 static void 428 ng_setsockaddr(netmsg_t msg) 429 { 430 struct socket *so = msg->sockaddr.base.nm_so; 431 struct sockaddr **addr = msg->sockaddr.nm_nam; 432 struct ngpcb *pcbp; 433 struct sockaddr_ng *sg; 434 int sg_len, namelen; 435 int error; 436 437 /* Why isn't sg_data a `char[1]' ? :-( */ 438 sg_len = sizeof(struct sockaddr_ng) - sizeof(sg->sg_data) + 1; 439 440 crit_enter(); 441 pcbp = sotongpcb(so); 442 if ((pcbp == 0) || (pcbp->sockdata == NULL)) { 443 crit_exit(); 444 error = EINVAL; 445 goto out; 446 } 447 448 namelen = 0; /* silence compiler ! */ 449 450 if (pcbp->sockdata->node->name != NULL) 451 sg_len += namelen = strlen(pcbp->sockdata->node->name); 452 453 MALLOC(sg, struct sockaddr_ng *, sg_len, M_SONAME, M_WAITOK | M_ZERO); 454 455 if (pcbp->sockdata->node->name != NULL) 456 bcopy(pcbp->sockdata->node->name, sg->sg_data, namelen); 457 crit_exit(); 458 459 sg->sg_len = sg_len; 460 sg->sg_family = AF_NETGRAPH; 461 *addr = (struct sockaddr *)sg; 462 error = 0; 463 out: 464 lwkt_replymsg(&msg->sockaddr.base.lmsg, error); 465 } 466 467 /* 468 * Attach a socket to it's protocol specific partner. 469 * For a control socket, actually create a netgraph node and attach 470 * to it as well. 471 */ 472 473 static int 474 ng_attach_cntl(struct socket *so) 475 { 476 struct ngsock *privdata; 477 struct ngpcb *pcbp; 478 int error; 479 480 /* Setup protocol control block */ 481 if ((error = ng_attach_common(so, NG_CONTROL)) != 0) 482 return (error); 483 pcbp = sotongpcb(so); 484 485 /* Allocate node private info */ 486 MALLOC(privdata, struct ngsock *, 487 sizeof(*privdata), M_NETGRAPH, M_WAITOK | M_ZERO); 488 489 /* Make the generic node components */ 490 if ((error = ng_make_node_common(&typestruct, &privdata->node)) != 0) { 491 FREE(privdata, M_NETGRAPH); 492 ng_detach_common(pcbp, NG_CONTROL); 493 return (error); 494 } 495 privdata->node->private = privdata; 496 497 /* Link the pcb and the node private data */ 498 privdata->ctlsock = pcbp; 499 pcbp->sockdata = privdata; 500 privdata->refs++; 501 return (0); 502 } 503 504 static int 505 ng_attach_data(struct socket *so) 506 { 507 return(ng_attach_common(so, NG_DATA)); 508 } 509 510 /* 511 * Set up a socket protocol control block. 512 * This code is shared between control and data sockets. 513 */ 514 static int 515 ng_attach_common(struct socket *so, int type) 516 { 517 struct ngpcb *pcbp; 518 int error; 519 520 /* Standard socket setup stuff */ 521 error = soreserve(so, ngpdg_sendspace, ngpdg_recvspace, NULL); 522 if (error) 523 return (error); 524 525 /* Allocate the pcb */ 526 MALLOC(pcbp, struct ngpcb *, sizeof(*pcbp), M_PCB, M_WAITOK | M_ZERO); 527 pcbp->type = type; 528 529 /* Link the pcb and the socket */ 530 soreference(so); 531 so->so_pcb = (caddr_t) pcbp; 532 pcbp->ng_socket = so; 533 534 /* Add the socket to linked list */ 535 LIST_INSERT_HEAD(&ngsocklist, pcbp, socks); 536 return (0); 537 } 538 539 /* 540 * Disassociate the socket from it's protocol specific 541 * partner. If it's attached to a node's private data structure, 542 * then unlink from that too. If we were the last socket attached to it, 543 * then shut down the entire node. Shared code for control and data sockets. 544 */ 545 static void 546 ng_detach_common(struct ngpcb *pcbp, int which) 547 { 548 struct ngsock *sockdata; 549 struct socket *so; 550 551 if (pcbp->sockdata) { 552 sockdata = pcbp->sockdata; 553 pcbp->sockdata = NULL; 554 switch (which) { 555 case NG_CONTROL: 556 sockdata->ctlsock = NULL; 557 break; 558 case NG_DATA: 559 sockdata->datasock = NULL; 560 break; 561 default: 562 panic(__func__); 563 } 564 if ((--sockdata->refs == 0) && (sockdata->node != NULL)) 565 ng_rmnode(sockdata->node); 566 } 567 so = pcbp->ng_socket; 568 so->so_pcb = NULL; 569 pcbp->ng_socket = NULL; 570 sofree(so); /* remove pcb ref */ 571 572 LIST_REMOVE(pcbp, socks); 573 FREE(pcbp, M_PCB); 574 } 575 576 #ifdef NOTYET 577 /* 578 * File descriptors can be passed into a AF_NETGRAPH socket. 579 * Note, that file descriptors cannot be passed OUT. 580 * Only character device descriptors are accepted. 581 * Character devices are useful to connect a graph to a device, 582 * which after all is the purpose of this whole system. 583 */ 584 static int 585 ng_internalize(struct mbuf *control, struct thread *td) 586 { 587 struct filedesc *fdp = p->p_fd; 588 const struct cmsghdr *cm = mtod(control, const struct cmsghdr *); 589 struct file *fp; 590 struct vnode *vn; 591 int oldfds; 592 int fd; 593 594 if (cm->cmsg_type != SCM_RIGHTS || cm->cmsg_level != SOL_SOCKET || 595 cm->cmsg_len != control->m_len) { 596 TRAP_ERROR; 597 return (EINVAL); 598 } 599 600 /* Check there is only one FD. XXX what would more than one signify? */ 601 oldfds = (cm->cmsg_len - sizeof(*cm)) / sizeof(int); 602 if (oldfds != 1) { 603 TRAP_ERROR; 604 return (EINVAL); 605 } 606 607 /* Check that the FD given is legit. and change it to a pointer to a 608 * struct file. */ 609 fd = *(int *) (cm + 1); 610 if ((unsigned) fd >= fdp->fd_nfiles 611 || (fp = fdp->fd_files[fd].fp) == NULL) { 612 return (EBADF); 613 } 614 615 /* Depending on what kind of resource it is, act differently. For 616 * devices, we treat it as a file. For a AF_NETGRAPH socket, 617 * shortcut straight to the node. */ 618 switch (fp->f_type) { 619 case DTYPE_VNODE: 620 vn = (struct vnode *) fp->f_data; 621 if (vn && (vn->v_type == VCHR)) { 622 /* for a VCHR, actually reference the FILE */ 623 fp->f_count++; 624 /* XXX then what :) */ 625 /* how to pass on to other modules? */ 626 } else { 627 TRAP_ERROR; 628 return (EINVAL); 629 } 630 break; 631 default: 632 TRAP_ERROR; 633 return (EINVAL); 634 } 635 return (0); 636 } 637 #endif /* NOTYET */ 638 639 /* 640 * Connect the data socket to a named control socket node. 641 */ 642 static int 643 ng_connect_data(struct sockaddr *nam, struct ngpcb *pcbp) 644 { 645 struct sockaddr_ng *sap; 646 node_p farnode; 647 struct ngsock *sockdata; 648 int error; 649 650 /* If we are already connected, don't do it again */ 651 if (pcbp->sockdata != NULL) 652 return (EISCONN); 653 654 /* Find the target (victim) and check it doesn't already have a data 655 * socket. Also check it is a 'socket' type node. */ 656 sap = (struct sockaddr_ng *) nam; 657 if ((error = ng_path2node(NULL, sap->sg_data, &farnode, NULL))) 658 return (error); 659 660 if (strcmp(farnode->type->name, NG_SOCKET_NODE_TYPE) != 0) 661 return (EINVAL); 662 sockdata = farnode->private; 663 if (sockdata->datasock != NULL) 664 return (EADDRINUSE); 665 666 /* Link the PCB and the private data struct. and note the extra 667 * reference */ 668 sockdata->datasock = pcbp; 669 pcbp->sockdata = sockdata; 670 sockdata->refs++; 671 return (0); 672 } 673 674 /* 675 * Connect the existing control socket node to a named node:hook. 676 * The hook we use on this end is the same name as the remote node name. 677 */ 678 static int 679 ng_connect_cntl(struct sockaddr *nam, struct ngpcb *pcbp) 680 { 681 struct ngsock *const sockdata = pcbp->sockdata; 682 struct sockaddr_ng *sap; 683 char *node, *hook; 684 node_p farnode; 685 int rtn, error; 686 687 sap = (struct sockaddr_ng *) nam; 688 rtn = ng_path_parse(sap->sg_data, &node, NULL, &hook); 689 if (rtn < 0 || node == NULL || hook == NULL) { 690 TRAP_ERROR; 691 return (EINVAL); 692 } 693 farnode = ng_findname(sockdata->node, node); 694 if (farnode == NULL) { 695 TRAP_ERROR; 696 return (EADDRNOTAVAIL); 697 } 698 699 /* Connect, using a hook name the same as the far node name. */ 700 error = ng_con_nodes(sockdata->node, node, farnode, hook); 701 return error; 702 } 703 704 /* 705 * Binding a socket means giving the corresponding node a name 706 */ 707 static int 708 ng_bind(struct sockaddr *nam, struct ngpcb *pcbp) 709 { 710 struct ngsock *const sockdata = pcbp->sockdata; 711 struct sockaddr_ng *const sap = (struct sockaddr_ng *) nam; 712 713 if (sockdata == NULL) { 714 TRAP_ERROR; 715 return (EINVAL); 716 } 717 if (sap->sg_len < 3 || sap->sg_data[sap->sg_len - 3] != '\0') { 718 TRAP_ERROR; 719 return (EINVAL); 720 } 721 return (ng_name_node(sockdata->node, sap->sg_data)); 722 } 723 724 /* 725 * Take a message and pass it up to the control socket associated 726 * with the node. 727 */ 728 static int 729 ship_msg(struct ngpcb *pcbp, struct ng_mesg *msg, struct sockaddr_ng *addr) 730 { 731 struct socket *const so = pcbp->ng_socket; 732 struct mbuf *mdata; 733 int msglen; 734 735 /* Copy the message itself into an mbuf chain */ 736 msglen = sizeof(struct ng_mesg) + msg->header.arglen; 737 mdata = m_devget((caddr_t) msg, msglen, 0, NULL, NULL); 738 739 /* Here we free the message, as we are the end of the line. 740 * We need to do that regardless of whether we got mbufs. */ 741 FREE(msg, M_NETGRAPH); 742 743 if (mdata == NULL) { 744 TRAP_ERROR; 745 return (ENOBUFS); 746 } 747 748 /* Send it up to the socket */ 749 if (ssb_appendaddr(&so->so_rcv, 750 (struct sockaddr *) addr, mdata, NULL) == 0) { 751 TRAP_ERROR; 752 m_freem(mdata); 753 return (ENOBUFS); 754 } 755 sorwakeup(so); 756 return (0); 757 } 758 759 /* 760 * You can only create new nodes from the socket end of things. 761 */ 762 static int 763 ngs_constructor(node_p *nodep) 764 { 765 return (EINVAL); 766 } 767 768 /* 769 * We allow any hook to be connected to the node. 770 * There is no per-hook private information though. 771 */ 772 static int 773 ngs_newhook(node_p node, hook_p hook, const char *name) 774 { 775 hook->private = node->private; 776 return (0); 777 } 778 779 /* 780 * Incoming messages get passed up to the control socket. 781 * Unless they are for us specifically (socket_type) 782 */ 783 static int 784 ngs_rcvmsg(node_p node, struct ng_mesg *msg, const char *retaddr, 785 struct ng_mesg **resp) 786 { 787 struct ngsock *const sockdata = node->private; 788 struct ngpcb *const pcbp = sockdata->ctlsock; 789 struct sockaddr_ng *addr; 790 int addrlen; 791 int error = 0; 792 793 /* Only allow mesgs to be passed if we have the control socket. 794 * Data sockets can only support the generic messages. */ 795 if (pcbp == NULL) { 796 TRAP_ERROR; 797 return (EINVAL); 798 } 799 800 if (msg->header.typecookie == NGM_SOCKET_COOKIE) { 801 switch (msg->header.cmd) { 802 case NGM_SOCK_CMD_NOLINGER: 803 sockdata->flags |= NGS_FLAG_NOLINGER; 804 break; 805 case NGM_SOCK_CMD_LINGER: 806 sockdata->flags &= ~NGS_FLAG_NOLINGER; 807 break; 808 default: 809 error = EINVAL; /* unknown command */ 810 } 811 /* Free the message and return */ 812 FREE(msg, M_NETGRAPH); 813 return(error); 814 815 } 816 /* Get the return address into a sockaddr */ 817 if ((retaddr == NULL) || (*retaddr == '\0')) 818 retaddr = ""; 819 addrlen = strlen(retaddr); 820 MALLOC(addr, struct sockaddr_ng *, addrlen + 4, M_NETGRAPH, M_NOWAIT); 821 if (addr == NULL) { 822 TRAP_ERROR; 823 return (ENOMEM); 824 } 825 addr->sg_len = addrlen + 3; 826 addr->sg_family = AF_NETGRAPH; 827 bcopy(retaddr, addr->sg_data, addrlen); 828 addr->sg_data[addrlen] = '\0'; 829 830 /* Send it up */ 831 error = ship_msg(pcbp, msg, addr); 832 FREE(addr, M_NETGRAPH); 833 return (error); 834 } 835 836 /* 837 * Receive data on a hook 838 */ 839 static int 840 ngs_rcvdata(hook_p hook, struct mbuf *m, meta_p meta) 841 { 842 struct ngsock *const sockdata = hook->node->private; 843 struct ngpcb *const pcbp = sockdata->datasock; 844 struct socket *so; 845 struct sockaddr_ng *addr; 846 char *addrbuf[NG_HOOKSIZ + 4]; 847 int addrlen; 848 849 /* If there is no data socket, black-hole it */ 850 if (pcbp == NULL) { 851 NG_FREE_DATA(m, meta); 852 return (0); 853 } 854 so = pcbp->ng_socket; 855 856 /* Get the return address into a sockaddr. */ 857 addrlen = strlen(hook->name); /* <= NG_HOOKSIZ - 1 */ 858 addr = (struct sockaddr_ng *) addrbuf; 859 addr->sg_len = addrlen + 3; 860 addr->sg_family = AF_NETGRAPH; 861 bcopy(hook->name, addr->sg_data, addrlen); 862 addr->sg_data[addrlen] = '\0'; 863 864 /* We have no use for the meta data, free/clear it now. */ 865 NG_FREE_META(meta); 866 867 /* Try to tell the socket which hook it came in on */ 868 if (ssb_appendaddr(&so->so_rcv, (struct sockaddr *) addr, m, NULL) == 0) { 869 m_freem(m); 870 TRAP_ERROR; 871 return (ENOBUFS); 872 } 873 sorwakeup(so); 874 return (0); 875 } 876 877 /* 878 * Hook disconnection 879 * 880 * For this type, removal of the last link destroys the node 881 * if the NOLINGER flag is set. 882 */ 883 static int 884 ngs_disconnect(hook_p hook) 885 { 886 struct ngsock *const sockdata = hook->node->private; 887 888 if ((sockdata->flags & NGS_FLAG_NOLINGER ) 889 && (hook->node->numhooks == 0)) { 890 ng_rmnode(hook->node); 891 } 892 return (0); 893 } 894 895 /* 896 * Do local shutdown processing. 897 * In this case, that involves making sure the socket 898 * knows we should be shutting down. 899 */ 900 static int 901 ngs_rmnode(node_p node) 902 { 903 struct ngsock *const sockdata = node->private; 904 struct ngpcb *const dpcbp = sockdata->datasock; 905 struct ngpcb *const pcbp = sockdata->ctlsock; 906 907 ng_cutlinks(node); 908 ng_unname(node); 909 910 if (dpcbp != NULL) { 911 soisdisconnected(dpcbp->ng_socket); 912 dpcbp->sockdata = NULL; 913 sockdata->datasock = NULL; 914 sockdata->refs--; 915 } 916 if (pcbp != NULL) { 917 soisdisconnected(pcbp->ng_socket); 918 pcbp->sockdata = NULL; 919 sockdata->ctlsock = NULL; 920 sockdata->refs--; 921 } 922 node->private = NULL; 923 ng_unref(node); 924 FREE(sockdata, M_NETGRAPH); 925 return (0); 926 } 927 928 /* 929 * Control and data socket type descriptors 930 */ 931 932 static struct pr_usrreqs ngc_usrreqs = { 933 .pru_abort = NULL, 934 .pru_accept = pr_generic_notsupp, 935 .pru_attach = ngc_attach, 936 .pru_bind = ngc_bind, 937 .pru_connect = ngc_connect, 938 .pru_connect2 = pr_generic_notsupp, 939 .pru_control = pr_generic_notsupp, 940 .pru_detach = ngc_detach, 941 .pru_disconnect = NULL, 942 .pru_listen = pr_generic_notsupp, 943 .pru_peeraddr = NULL, 944 .pru_rcvd = pr_generic_notsupp, 945 .pru_rcvoob = pr_generic_notsupp, 946 .pru_send = ngc_send, 947 .pru_sense = pru_sense_null, 948 .pru_shutdown = NULL, 949 .pru_sockaddr = ng_setsockaddr, 950 .pru_sosend = sosend, 951 .pru_soreceive = soreceive 952 }; 953 954 static struct pr_usrreqs ngd_usrreqs = { 955 .pru_abort = NULL, 956 .pru_accept = pr_generic_notsupp, 957 .pru_attach = ngd_attach, 958 .pru_bind = NULL, 959 .pru_connect = ngd_connect, 960 .pru_connect2 = pr_generic_notsupp, 961 .pru_control = pr_generic_notsupp, 962 .pru_detach = ngd_detach, 963 .pru_disconnect = NULL, 964 .pru_listen = pr_generic_notsupp, 965 .pru_peeraddr = NULL, 966 .pru_rcvd = pr_generic_notsupp, 967 .pru_rcvoob = pr_generic_notsupp, 968 .pru_send = ngd_send, 969 .pru_sense = pru_sense_null, 970 .pru_shutdown = NULL, 971 .pru_sockaddr = ng_setsockaddr, 972 .pru_sosend = sosend, 973 .pru_soreceive = soreceive 974 }; 975 976 /* 977 * Definitions of protocols supported in the NETGRAPH domain. 978 */ 979 980 extern struct domain ngdomain; /* stop compiler warnings */ 981 982 static struct protosw ngsw[] = { 983 { 984 .pr_type = SOCK_DGRAM, 985 .pr_domain = &ngdomain, 986 .pr_protocol = NG_CONTROL, 987 .pr_flags = PR_ATOMIC | PR_ADDR /* | PR_RIGHTS */, 988 .pr_usrreqs = &ngc_usrreqs 989 }, 990 { 991 .pr_type = SOCK_DGRAM, 992 .pr_domain = &ngdomain, 993 .pr_protocol = NG_DATA, 994 .pr_flags = PR_ATOMIC | PR_ADDR, 995 .pr_usrreqs = &ngd_usrreqs 996 } 997 }; 998 999 struct domain ngdomain = { 1000 AF_NETGRAPH, "netgraph", NULL, NULL, NULL, 1001 ngsw, &ngsw[sizeof(ngsw) / sizeof(ngsw[0])], 1002 }; 1003 1004 /* 1005 * Handle loading and unloading for this node type 1006 * This is to handle auxiliary linkages (e.g protocol domain addition). 1007 */ 1008 static int 1009 ngs_mod_event(module_t mod, int event, void *data) 1010 { 1011 int error = 0; 1012 1013 switch (event) { 1014 case MOD_LOAD: 1015 /* Register protocol domain */ 1016 net_add_domain(&ngdomain); 1017 break; 1018 case MOD_UNLOAD: 1019 /* Insure there are no open netgraph sockets */ 1020 if (!LIST_EMPTY(&ngsocklist)) { 1021 error = EBUSY; 1022 break; 1023 } 1024 1025 #ifdef NOTYET 1026 /* Unregister protocol domain XXX can't do this yet.. */ 1027 if ((error = net_rm_domain(&ngdomain)) != 0) 1028 break; 1029 #else 1030 error = EBUSY; 1031 #endif 1032 break; 1033 default: 1034 error = EOPNOTSUPP; 1035 break; 1036 } 1037 return (error); 1038 } 1039 1040 SYSCTL_INT(_net_graph, OID_AUTO, family, CTLFLAG_RD, 0, AF_NETGRAPH, ""); 1041 SYSCTL_NODE(_net_graph, OID_AUTO, data, CTLFLAG_RW, 0, "DATA"); 1042 SYSCTL_INT(_net_graph_data, OID_AUTO, proto, CTLFLAG_RD, 0, NG_DATA, ""); 1043 SYSCTL_NODE(_net_graph, OID_AUTO, control, CTLFLAG_RW, 0, "CONTROL"); 1044 SYSCTL_INT(_net_graph_control, OID_AUTO, proto, CTLFLAG_RD, 0, NG_CONTROL, ""); 1045 1046