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