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