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