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