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