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