The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/netgraph/ng_socket.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*
    2  * ng_socket.c
    3  */
    4 
    5 /*-
    6  * Copyright (c) 1996-1999 Whistle Communications, Inc.
    7  * All rights reserved.
    8  *
    9  * Subject to the following obligations and disclaimer of warranty, use and
   10  * redistribution of this software, in source or object code forms, with or
   11  * without modifications are expressly permitted by Whistle Communications;
   12  * provided, however, that:
   13  * 1. Any and all reproductions of the source or object code must include the
   14  *    copyright notice above and the following disclaimer of warranties; and
   15  * 2. No rights are granted, in any manner or form, to use Whistle
   16  *    Communications, Inc. trademarks, including the mark "WHISTLE
   17  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
   18  *    such appears in the above copyright notice or in the software.
   19  *
   20  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
   21  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
   22  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
   23  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
   24  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
   25  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
   26  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
   27  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
   28  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
   29  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
   30  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
   31  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
   32  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
   33  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   35  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
   36  * OF SUCH DAMAGE.
   37  *
   38  * Author: Julian Elischer <julian@freebsd.org>
   39  *
   40  * $FreeBSD: releng/9.0/sys/netgraph/ng_socket.c 224778 2011-08-11 12:30:23Z rwatson $
   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/domain.h>
   54 #include <sys/kernel.h>
   55 #include <sys/linker.h>
   56 #include <sys/lock.h>
   57 #include <sys/malloc.h>
   58 #include <sys/mbuf.h>
   59 #include <sys/mutex.h>
   60 #include <sys/priv.h>
   61 #include <sys/protosw.h>
   62 #include <sys/queue.h>
   63 #include <sys/socket.h>
   64 #include <sys/socketvar.h>
   65 #include <sys/syscallsubr.h>
   66 #include <sys/sysctl.h>
   67 #ifdef NOTYET
   68 #include <sys/vnode.h>
   69 #endif
   70 
   71 #include <net/vnet.h>
   72 
   73 #include <netgraph/ng_message.h>
   74 #include <netgraph/netgraph.h>
   75 #include <netgraph/ng_socketvar.h>
   76 #include <netgraph/ng_socket.h>
   77 
   78 #ifdef NG_SEPARATE_MALLOC
   79 MALLOC_DEFINE(M_NETGRAPH_PATH, "netgraph_path", "netgraph path info ");
   80 MALLOC_DEFINE(M_NETGRAPH_SOCK, "netgraph_sock", "netgraph socket info ");
   81 #else
   82 #define M_NETGRAPH_PATH M_NETGRAPH
   83 #define M_NETGRAPH_SOCK M_NETGRAPH
   84 #endif
   85 
   86 /*
   87  * It's Ascii-art time!
   88  *   +-------------+   +-------------+
   89  *   |socket  (ctl)|   |socket (data)|
   90  *   +-------------+   +-------------+
   91  *          ^                 ^
   92  *          |                 |
   93  *          v                 v
   94  *    +-----------+     +-----------+
   95  *    |pcb   (ctl)|     |pcb  (data)|
   96  *    +-----------+     +-----------+
   97  *          ^                 ^
   98  *          |                 |
   99  *          v                 v
  100  *      +--------------------------+
  101  *      |   Socket type private    |
  102  *      |       data               |
  103  *      +--------------------------+
  104  *                   ^
  105  *                   |
  106  *                   v
  107  *           +----------------+
  108  *           | struct ng_node |
  109  *           +----------------+
  110  */
  111 
  112 /* Netgraph node methods */
  113 static ng_constructor_t ngs_constructor;
  114 static ng_rcvmsg_t      ngs_rcvmsg;
  115 static ng_shutdown_t    ngs_shutdown;
  116 static ng_newhook_t     ngs_newhook;
  117 static ng_connect_t     ngs_connect;
  118 static ng_rcvdata_t     ngs_rcvdata;
  119 static ng_disconnect_t  ngs_disconnect;
  120 
  121 /* Internal methods */
  122 static int      ng_attach_data(struct socket *so);
  123 static int      ng_attach_cntl(struct socket *so);
  124 static int      ng_attach_common(struct socket *so, int type);
  125 static void     ng_detach_common(struct ngpcb *pcbp, int type);
  126 static void     ng_socket_free_priv(struct ngsock *priv);
  127 #ifdef NOTYET
  128 static int      ng_internalize(struct mbuf *m, struct thread *p);
  129 #endif
  130 static int      ng_connect_data(struct sockaddr *nam, struct ngpcb *pcbp);
  131 static int      ng_bind(struct sockaddr *nam, struct ngpcb *pcbp);
  132 
  133 static int      ngs_mod_event(module_t mod, int event, void *data);
  134 static void     ng_socket_item_applied(void *context, int error);
  135 
  136 /* Netgraph type descriptor */
  137 static struct ng_type typestruct = {
  138         .version =      NG_ABI_VERSION,
  139         .name =         NG_SOCKET_NODE_TYPE,
  140         .mod_event =    ngs_mod_event,
  141         .constructor =  ngs_constructor,
  142         .rcvmsg =       ngs_rcvmsg,
  143         .shutdown =     ngs_shutdown,
  144         .newhook =      ngs_newhook,
  145         .connect =      ngs_connect,
  146         .rcvdata =      ngs_rcvdata,
  147         .disconnect =   ngs_disconnect,
  148 };
  149 NETGRAPH_INIT_ORDERED(socket, &typestruct, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);
  150 
  151 /* Buffer space */
  152 static u_long ngpdg_sendspace = 20 * 1024;      /* really max datagram size */
  153 SYSCTL_ULONG(_net_graph, OID_AUTO, maxdgram, CTLFLAG_RW,
  154     &ngpdg_sendspace , 0, "Maximum outgoing Netgraph datagram size");
  155 static u_long ngpdg_recvspace = 20 * 1024;
  156 SYSCTL_ULONG(_net_graph, OID_AUTO, recvspace, CTLFLAG_RW,
  157     &ngpdg_recvspace , 0, "Maximum space for incoming Netgraph datagrams");
  158 
  159 /* List of all sockets (for netstat -f netgraph) */
  160 static LIST_HEAD(, ngpcb) ngsocklist;
  161 
  162 static struct mtx       ngsocketlist_mtx;
  163 
  164 #define sotongpcb(so) ((struct ngpcb *)(so)->so_pcb)
  165 
  166 /* If getting unexplained errors returned, set this to "kdb_enter("X"); */
  167 #ifndef TRAP_ERROR
  168 #define TRAP_ERROR
  169 #endif
  170 
  171 /***************************************************************
  172         Control sockets
  173 ***************************************************************/
  174 
  175 static int
  176 ngc_attach(struct socket *so, int proto, struct thread *td)
  177 {
  178         struct ngpcb *const pcbp = sotongpcb(so);
  179         int error;
  180 
  181         error = priv_check(td, PRIV_NETGRAPH_CONTROL);
  182         if (error)
  183                 return (error);
  184         if (pcbp != NULL)
  185                 return (EISCONN);
  186         return (ng_attach_cntl(so));
  187 }
  188 
  189 static void
  190 ngc_detach(struct socket *so)
  191 {
  192         struct ngpcb *const pcbp = sotongpcb(so);
  193 
  194         KASSERT(pcbp != NULL, ("ngc_detach: pcbp == NULL"));
  195         ng_detach_common(pcbp, NG_CONTROL);
  196 }
  197 
  198 static int
  199 ngc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
  200          struct mbuf *control, struct thread *td)
  201 {
  202         struct ngpcb *const pcbp = sotongpcb(so);
  203         struct ngsock *const priv = NG_NODE_PRIVATE(pcbp->sockdata->node);
  204         struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr;
  205         struct ng_mesg *msg;
  206         struct mbuf *m0;
  207         item_p item;
  208         char *path = NULL;
  209         int len, error = 0;
  210         struct ng_apply_info apply;
  211 
  212 #ifdef  NOTYET
  213         if (control && (error = ng_internalize(control, td))) {
  214                 if (pcbp->sockdata == NULL) {
  215                         error = ENOTCONN;
  216                         goto release;
  217                 }
  218         }
  219 #else   /* NOTYET */
  220         if (control) {
  221                 error = EINVAL;
  222                 goto release;
  223         }
  224 #endif  /* NOTYET */
  225 
  226         /* Require destination as there may be >= 1 hooks on this node. */
  227         if (addr == NULL) {
  228                 error = EDESTADDRREQ;
  229                 goto release;
  230         }
  231 
  232         /*
  233          * Allocate an expendable buffer for the path, chop off
  234          * the sockaddr header, and make sure it's NUL terminated.
  235          */
  236         len = sap->sg_len - 2;
  237         path = malloc(len + 1, M_NETGRAPH_PATH, M_WAITOK);
  238         bcopy(sap->sg_data, path, len);
  239         path[len] = '\0';
  240 
  241         /*
  242          * Move the actual message out of mbufs into a linear buffer.
  243          * Start by adding up the size of the data. (could use mh_len?)
  244          */
  245         for (len = 0, m0 = m; m0 != NULL; m0 = m0->m_next)
  246                 len += m0->m_len;
  247 
  248         /*
  249          * Move the data into a linear buffer as well.
  250          * Messages are not delivered in mbufs.
  251          */
  252         msg = malloc(len + 1, M_NETGRAPH_MSG, M_WAITOK);
  253         m_copydata(m, 0, len, (char *)msg);
  254 
  255         if (msg->header.version != NG_VERSION) {
  256                 free(msg, M_NETGRAPH_MSG);
  257                 error = EINVAL;
  258                 goto release;
  259         }
  260 
  261         /*
  262          * Hack alert!
  263          * We look into the message and if it mkpeers a node of unknown type, we
  264          * try to load it. We need to do this now, in syscall thread, because if
  265          * message gets queued and applied later we will get panic.
  266          */
  267         if (msg->header.typecookie == NGM_GENERIC_COOKIE &&
  268             msg->header.cmd == NGM_MKPEER) {
  269                 struct ngm_mkpeer *const mkp = (struct ngm_mkpeer *) msg->data;
  270 
  271                 if (ng_findtype(mkp->type) == NULL) {
  272                         char filename[NG_TYPESIZ + 3];
  273                         int fileid;
  274 
  275                         /* Not found, try to load it as a loadable module. */
  276                         snprintf(filename, sizeof(filename), "ng_%s",
  277                             mkp->type);
  278                         error = kern_kldload(curthread, filename, &fileid);
  279                         if (error != 0) {
  280                                 free(msg, M_NETGRAPH_MSG);
  281                                 goto release;
  282                         }
  283 
  284                         /* See if type has been loaded successfully. */
  285                         if (ng_findtype(mkp->type) == NULL) {
  286                                 free(msg, M_NETGRAPH_MSG);
  287                                 (void)kern_kldunload(curthread, fileid,
  288                                     LINKER_UNLOAD_NORMAL);
  289                                 error =  ENXIO;
  290                                 goto release;
  291                         }
  292                 }
  293         }
  294 
  295         item = ng_package_msg(msg, M_WAITOK);
  296         if ((error = ng_address_path((pcbp->sockdata->node), item, path, 0))
  297             != 0) {
  298 #ifdef TRACE_MESSAGES
  299                 printf("ng_address_path: errx=%d\n", error);
  300 #endif
  301                 goto release;
  302         }
  303 
  304 #ifdef TRACE_MESSAGES
  305         printf("[%x]:<---------[socket]: c=<%d>cmd=%x(%s) f=%x #%d (%s)\n",
  306                 item->el_dest->nd_ID,
  307                 msg->header.typecookie,
  308                 msg->header.cmd,
  309                 msg->header.cmdstr,
  310                 msg->header.flags,
  311                 msg->header.token,
  312                 item->el_dest->nd_type->name);
  313 #endif
  314         SAVE_LINE(item);
  315         /*
  316          * We do not want to return from syscall until the item
  317          * is processed by destination node. We register callback
  318          * on the item, which will update priv->error when item
  319          * was applied.
  320          * If ng_snd_item() has queued item, we sleep until
  321          * callback wakes us up.
  322          */
  323         bzero(&apply, sizeof(apply));
  324         apply.apply = ng_socket_item_applied;
  325         apply.context = priv;
  326         item->apply = &apply;
  327         priv->error = -1;
  328 
  329         error = ng_snd_item(item, 0);
  330 
  331         mtx_lock(&priv->mtx);
  332         if (priv->error == -1)
  333                 msleep(priv, &priv->mtx, 0, "ngsock", 0);
  334         mtx_unlock(&priv->mtx);
  335         KASSERT(priv->error != -1,
  336             ("ng_socket: priv->error wasn't updated"));
  337         error = priv->error;
  338 
  339 release:
  340         if (path != NULL)
  341                 free(path, M_NETGRAPH_PATH);
  342         if (control != NULL)
  343                 m_freem(control);
  344         if (m != NULL)
  345                 m_freem(m);
  346         return (error);
  347 }
  348 
  349 static int
  350 ngc_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
  351 {
  352         struct ngpcb *const pcbp = sotongpcb(so);
  353 
  354         if (pcbp == 0)
  355                 return (EINVAL);
  356         return (ng_bind(nam, pcbp));
  357 }
  358 
  359 static int
  360 ngc_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
  361 {
  362         /*
  363          * At this time refuse to do this.. it used to
  364          * do something but it was undocumented and not used.
  365          */
  366         printf("program tried to connect control socket to remote node\n");
  367         return (EINVAL);
  368 }
  369 
  370 /***************************************************************
  371         Data sockets
  372 ***************************************************************/
  373 
  374 static int
  375 ngd_attach(struct socket *so, int proto, struct thread *td)
  376 {
  377         struct ngpcb *const pcbp = sotongpcb(so);
  378 
  379         if (pcbp != NULL)
  380                 return (EISCONN);
  381         return (ng_attach_data(so));
  382 }
  383 
  384 static void
  385 ngd_detach(struct socket *so)
  386 {
  387         struct ngpcb *const pcbp = sotongpcb(so);
  388 
  389         KASSERT(pcbp != NULL, ("ngd_detach: pcbp == NULL"));
  390         ng_detach_common(pcbp, NG_DATA);
  391 }
  392 
  393 static int
  394 ngd_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
  395          struct mbuf *control, struct thread *td)
  396 {
  397         struct ngpcb *const pcbp = sotongpcb(so);
  398         struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr;
  399         int     len, error;
  400         hook_p  hook = NULL;
  401         char    hookname[NG_HOOKSIZ];
  402 
  403         if ((pcbp == NULL) || (control != NULL)) {
  404                 error = EINVAL;
  405                 goto release;
  406         }
  407         if (pcbp->sockdata == NULL) {
  408                 error = ENOTCONN;
  409                 goto release;
  410         }
  411 
  412         if (sap == NULL)
  413                 len = 0;                /* Make compiler happy. */
  414         else
  415                 len = sap->sg_len - 2;
  416 
  417         /*
  418          * If the user used any of these ways to not specify an address
  419          * then handle specially.
  420          */
  421         if ((sap == NULL) || (len <= 0) || (*sap->sg_data == '\0')) {
  422                 if (NG_NODE_NUMHOOKS(pcbp->sockdata->node) != 1) {
  423                         error = EDESTADDRREQ;
  424                         goto release;
  425                 }
  426                 /*
  427                  * If exactly one hook exists, just use it.
  428                  * Special case to allow write(2) to work on an ng_socket.
  429                  */
  430                 hook = LIST_FIRST(&pcbp->sockdata->node->nd_hooks);
  431         } else {
  432                 if (len >= NG_HOOKSIZ) {
  433                         error = EINVAL;
  434                         goto release;
  435                 }
  436 
  437                 /*
  438                  * chop off the sockaddr header, and make sure it's NUL
  439                  * terminated
  440                  */
  441                 bcopy(sap->sg_data, hookname, len);
  442                 hookname[len] = '\0';
  443 
  444                 /* Find the correct hook from 'hookname' */
  445                 hook = ng_findhook(pcbp->sockdata->node, hookname);
  446                 if (hook == NULL) {
  447                         error = EHOSTUNREACH;
  448                         goto release;
  449                 }
  450         }
  451 
  452         /* Send data. */
  453         NG_SEND_DATA_FLAGS(error, hook, m, NG_WAITOK);
  454 
  455 release:
  456         if (control != NULL)
  457                 m_freem(control);
  458         if (m != NULL)
  459                 m_freem(m);
  460         return (error);
  461 }
  462 
  463 static int
  464 ngd_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
  465 {
  466         struct ngpcb *const pcbp = sotongpcb(so);
  467 
  468         if (pcbp == 0)
  469                 return (EINVAL);
  470         return (ng_connect_data(nam, pcbp));
  471 }
  472 
  473 /*
  474  * Used for both data and control sockets
  475  */
  476 static int
  477 ng_getsockaddr(struct socket *so, struct sockaddr **addr)
  478 {
  479         struct ngpcb *pcbp;
  480         struct sockaddr_ng *sg;
  481         int sg_len;
  482         int error = 0;
  483 
  484         /* Why isn't sg_data a `char[1]' ? :-( */
  485         sg_len = sizeof(struct sockaddr_ng) - sizeof(sg->sg_data) + 1;
  486 
  487         pcbp = sotongpcb(so);
  488         if ((pcbp == NULL) || (pcbp->sockdata == NULL))
  489                 /* XXXGL: can this still happen? */
  490                 return (EINVAL);
  491 
  492         mtx_lock(&pcbp->sockdata->mtx);
  493         if (pcbp->sockdata->node != NULL) {
  494                 node_p node = pcbp->sockdata->node;
  495                 int namelen = 0;        /* silence compiler! */
  496 
  497                 if (NG_NODE_HAS_NAME(node))
  498                         sg_len += namelen = strlen(NG_NODE_NAME(node));
  499 
  500                 sg = malloc(sg_len, M_SONAME, M_WAITOK | M_ZERO);
  501 
  502                 if (NG_NODE_HAS_NAME(node))
  503                         bcopy(NG_NODE_NAME(node), sg->sg_data, namelen);
  504 
  505                 sg->sg_len = sg_len;
  506                 sg->sg_family = AF_NETGRAPH;
  507                 *addr = (struct sockaddr *)sg;
  508                 mtx_unlock(&pcbp->sockdata->mtx);
  509         } else {
  510                 mtx_unlock(&pcbp->sockdata->mtx);
  511                 error = EINVAL;
  512         }
  513 
  514         return (error);
  515 }
  516 
  517 /*
  518  * Attach a socket to it's protocol specific partner.
  519  * For a control socket, actually create a netgraph node and attach
  520  * to it as well.
  521  */
  522 
  523 static int
  524 ng_attach_cntl(struct socket *so)
  525 {
  526         struct ngsock *priv;
  527         struct ngpcb *pcbp;
  528         node_p node;
  529         int error;
  530 
  531         /* Setup protocol control block */
  532         if ((error = ng_attach_common(so, NG_CONTROL)) != 0)
  533                 return (error);
  534         pcbp = sotongpcb(so);
  535 
  536         /* Make the generic node components */
  537         if ((error = ng_make_node_common(&typestruct, &node)) != 0) {
  538                 ng_detach_common(pcbp, NG_CONTROL);
  539                 return (error);
  540         }
  541 
  542         /* Allocate node private info */
  543         priv = malloc(sizeof(*priv), M_NETGRAPH_SOCK, M_WAITOK | M_ZERO);
  544 
  545         /* Initialize mutex. */
  546         mtx_init(&priv->mtx, "ng_socket", NULL, MTX_DEF);
  547 
  548         /* Link the pcb the private data. */
  549         priv->ctlsock = pcbp;
  550         pcbp->sockdata = priv;
  551         priv->refs++;
  552         priv->node = node;
  553 
  554         /* Store a hint for netstat(1). */
  555         priv->node_id = priv->node->nd_ID;
  556 
  557         /* Link the node and the private data. */
  558         NG_NODE_SET_PRIVATE(priv->node, priv);
  559         NG_NODE_REF(priv->node);
  560         priv->refs++;
  561 
  562         return (0);
  563 }
  564 
  565 static int
  566 ng_attach_data(struct socket *so)
  567 {
  568         return (ng_attach_common(so, NG_DATA));
  569 }
  570 
  571 /*
  572  * Set up a socket protocol control block.
  573  * This code is shared between control and data sockets.
  574  */
  575 static int
  576 ng_attach_common(struct socket *so, int type)
  577 {
  578         struct ngpcb *pcbp;
  579         int error;
  580 
  581         /* Standard socket setup stuff. */
  582         error = soreserve(so, ngpdg_sendspace, ngpdg_recvspace);
  583         if (error)
  584                 return (error);
  585 
  586         /* Allocate the pcb. */
  587         pcbp = malloc(sizeof(struct ngpcb), M_PCB, M_WAITOK | M_ZERO);
  588         pcbp->type = type;
  589 
  590         /* Link the pcb and the socket. */
  591         so->so_pcb = (caddr_t)pcbp;
  592         pcbp->ng_socket = so;
  593 
  594         /* Add the socket to linked list */
  595         mtx_lock(&ngsocketlist_mtx);
  596         LIST_INSERT_HEAD(&ngsocklist, pcbp, socks);
  597         mtx_unlock(&ngsocketlist_mtx);
  598         return (0);
  599 }
  600 
  601 /*
  602  * Disassociate the socket from it's protocol specific
  603  * partner. If it's attached to a node's private data structure,
  604  * then unlink from that too. If we were the last socket attached to it,
  605  * then shut down the entire node. Shared code for control and data sockets.
  606  */
  607 static void
  608 ng_detach_common(struct ngpcb *pcbp, int which)
  609 {
  610         struct ngsock *priv = pcbp->sockdata;
  611 
  612         if (priv != NULL) {
  613                 mtx_lock(&priv->mtx);
  614 
  615                 switch (which) {
  616                 case NG_CONTROL:
  617                         priv->ctlsock = NULL;
  618                         break;
  619                 case NG_DATA:
  620                         priv->datasock = NULL;
  621                         break;
  622                 default:
  623                         panic("%s", __func__);
  624                 }
  625                 pcbp->sockdata = NULL;
  626 
  627                 ng_socket_free_priv(priv);
  628         }
  629 
  630         pcbp->ng_socket->so_pcb = NULL;
  631         mtx_lock(&ngsocketlist_mtx);
  632         LIST_REMOVE(pcbp, socks);
  633         mtx_unlock(&ngsocketlist_mtx);
  634         free(pcbp, M_PCB);
  635 }
  636 
  637 /*
  638  * Remove a reference from node private data.
  639  */
  640 static void
  641 ng_socket_free_priv(struct ngsock *priv)
  642 {
  643         mtx_assert(&priv->mtx, MA_OWNED);
  644 
  645         priv->refs--;
  646 
  647         if (priv->refs == 0) {
  648                 mtx_destroy(&priv->mtx);
  649                 free(priv, M_NETGRAPH_SOCK);
  650                 return;
  651         }
  652 
  653         if ((priv->refs == 1) && (priv->node != NULL)) {
  654                 node_p node = priv->node;
  655 
  656                 priv->node = NULL;
  657                 mtx_unlock(&priv->mtx);
  658                 NG_NODE_UNREF(node);
  659                 ng_rmnode_self(node);
  660         } else
  661                 mtx_unlock(&priv->mtx);
  662 }
  663 
  664 #ifdef NOTYET
  665 /*
  666  * File descriptors can be passed into an AF_NETGRAPH socket.
  667  * Note, that file descriptors cannot be passed OUT.
  668  * Only character device descriptors are accepted.
  669  * Character devices are useful to connect a graph to a device,
  670  * which after all is the purpose of this whole system.
  671  */
  672 static int
  673 ng_internalize(struct mbuf *control, struct thread *td)
  674 {
  675         const struct cmsghdr *cm = mtod(control, const struct cmsghdr *);
  676         struct file *fp;
  677         struct vnode *vn;
  678         int oldfds;
  679         int fd;
  680 
  681         if (cm->cmsg_type != SCM_RIGHTS || cm->cmsg_level != SOL_SOCKET ||
  682             cm->cmsg_len != control->m_len) {
  683                 TRAP_ERROR;
  684                 return (EINVAL);
  685         }
  686 
  687         /* Check there is only one FD. XXX what would more than one signify? */
  688         oldfds = ((caddr_t)cm + cm->cmsg_len - (caddr_t)data) / sizeof (int);
  689         if (oldfds != 1) {
  690                 TRAP_ERROR;
  691                 return (EINVAL);
  692         }
  693 
  694         /* Check that the FD given is legit. and change it to a pointer to a
  695          * struct file. */
  696         fd = CMSG_DATA(cm);
  697         if ((error = fget(td, fd, 0, &fp)) != 0)
  698                 return (error);
  699 
  700         /* Depending on what kind of resource it is, act differently. For
  701          * devices, we treat it as a file. For an AF_NETGRAPH socket,
  702          * shortcut straight to the node. */
  703         switch (fp->f_type) {
  704         case DTYPE_VNODE:
  705                 vn = fp->f_data;
  706                 if (vn && (vn->v_type == VCHR)) {
  707                         /* for a VCHR, actually reference the FILE */
  708                         fhold(fp);
  709                         /* XXX then what :) */
  710                         /* how to pass on to other modules? */
  711                 } else {
  712                         fdrop(fp, td);
  713                         TRAP_ERROR;
  714                         return (EINVAL);
  715                 }
  716                 break;
  717         default:
  718                 fdrop(fp, td);
  719                 TRAP_ERROR;
  720                 return (EINVAL);
  721         }
  722         fdrop(fp, td);
  723         return (0);
  724 }
  725 #endif  /* NOTYET */
  726 
  727 /*
  728  * Connect the data socket to a named control socket node.
  729  */
  730 static int
  731 ng_connect_data(struct sockaddr *nam, struct ngpcb *pcbp)
  732 {
  733         struct sockaddr_ng *sap;
  734         node_p farnode;
  735         struct ngsock *priv;
  736         int error;
  737         item_p item;
  738 
  739         /* If we are already connected, don't do it again. */
  740         if (pcbp->sockdata != NULL)
  741                 return (EISCONN);
  742 
  743         /*
  744          * Find the target (victim) and check it doesn't already have
  745          * a data socket. Also check it is a 'socket' type node.
  746          * Use ng_package_data() and ng_address_path() to do this.
  747          */
  748 
  749         sap = (struct sockaddr_ng *) nam;
  750         /* The item will hold the node reference. */
  751         item = ng_package_data(NULL, NG_WAITOK);
  752 
  753         if ((error = ng_address_path(NULL, item,  sap->sg_data, 0)))
  754                 return (error); /* item is freed on failure */
  755 
  756         /*
  757          * Extract node from item and free item. Remember we now have
  758          * a reference on the node. The item holds it for us.
  759          * when we free the item we release the reference.
  760          */
  761         farnode = item->el_dest; /* shortcut */
  762         if (strcmp(farnode->nd_type->name, NG_SOCKET_NODE_TYPE) != 0) {
  763                 NG_FREE_ITEM(item); /* drop the reference to the node */
  764                 return (EINVAL);
  765         }
  766         priv = NG_NODE_PRIVATE(farnode);
  767         if (priv->datasock != NULL) {
  768                 NG_FREE_ITEM(item);     /* drop the reference to the node */
  769                 return (EADDRINUSE);
  770         }
  771 
  772         /*
  773          * Link the PCB and the private data struct. and note the extra
  774          * reference. Drop the extra reference on the node.
  775          */
  776         mtx_lock(&priv->mtx);
  777         priv->datasock = pcbp;
  778         pcbp->sockdata = priv;
  779         priv->refs++;
  780         mtx_unlock(&priv->mtx);
  781         NG_FREE_ITEM(item);     /* drop the reference to the node */
  782         return (0);
  783 }
  784 
  785 /*
  786  * Binding a socket means giving the corresponding node a name
  787  */
  788 static int
  789 ng_bind(struct sockaddr *nam, struct ngpcb *pcbp)
  790 {
  791         struct ngsock *const priv = pcbp->sockdata;
  792         struct sockaddr_ng *const sap = (struct sockaddr_ng *) nam;
  793 
  794         if (priv == NULL) {
  795                 TRAP_ERROR;
  796                 return (EINVAL);
  797         }
  798         if ((sap->sg_len < 4) || (sap->sg_len > (NG_NODESIZ + 2)) ||
  799             (sap->sg_data[0] == '\0') ||
  800             (sap->sg_data[sap->sg_len - 3] != '\0')) {
  801                 TRAP_ERROR;
  802                 return (EINVAL);
  803         }
  804         return (ng_name_node(priv->node, sap->sg_data));
  805 }
  806 
  807 /***************************************************************
  808         Netgraph node
  809 ***************************************************************/
  810 
  811 /*
  812  * You can only create new nodes from the socket end of things.
  813  */
  814 static int
  815 ngs_constructor(node_p nodep)
  816 {
  817         return (EINVAL);
  818 }
  819 
  820 /*
  821  * We allow any hook to be connected to the node.
  822  * There is no per-hook private information though.
  823  */
  824 static int
  825 ngs_newhook(node_p node, hook_p hook, const char *name)
  826 {
  827         NG_HOOK_SET_PRIVATE(hook, NG_NODE_PRIVATE(node));
  828         return (0);
  829 }
  830 
  831 /*
  832  * If only one hook, allow read(2) and write(2) to work.
  833  */
  834 static int
  835 ngs_connect(hook_p hook)
  836 {
  837         node_p node = NG_HOOK_NODE(hook);
  838         struct ngsock *priv = NG_NODE_PRIVATE(node);
  839 
  840         if ((priv->datasock) && (priv->datasock->ng_socket)) {
  841                 if (NG_NODE_NUMHOOKS(node) == 1)
  842                         priv->datasock->ng_socket->so_state |= SS_ISCONNECTED;
  843                 else
  844                         priv->datasock->ng_socket->so_state &= ~SS_ISCONNECTED;
  845         }
  846         return (0);
  847 }
  848 
  849 /*
  850  * Incoming messages get passed up to the control socket.
  851  * Unless they are for us specifically (socket_type)
  852  */
  853 static int
  854 ngs_rcvmsg(node_p node, item_p item, hook_p lasthook)
  855 {
  856         struct ngsock *const priv = NG_NODE_PRIVATE(node);
  857         struct ngpcb *pcbp;
  858         struct socket *so;
  859         struct sockaddr_ng addr;
  860         struct ng_mesg *msg;
  861         struct mbuf *m;
  862         ng_ID_t retaddr = NGI_RETADDR(item);
  863         int addrlen;
  864         int error = 0;
  865 
  866         NGI_GET_MSG(item, msg);
  867         NG_FREE_ITEM(item);
  868 
  869         /*
  870          * Grab priv->mtx here to prevent destroying of control socket
  871          * after checking that priv->ctlsock is not NULL.
  872          */
  873         mtx_lock(&priv->mtx);
  874         pcbp = priv->ctlsock;
  875 
  876         /*
  877          * Only allow mesgs to be passed if we have the control socket.
  878          * Data sockets can only support the generic messages.
  879          */
  880         if (pcbp == NULL) {
  881                 mtx_unlock(&priv->mtx);
  882                 TRAP_ERROR;
  883                 NG_FREE_MSG(msg);
  884                 return (EINVAL);
  885         }
  886         so = pcbp->ng_socket;
  887         SOCKBUF_LOCK(&so->so_rcv);
  888 
  889         /* As long as the race is handled, priv->mtx may be unlocked now. */
  890         mtx_unlock(&priv->mtx);
  891 
  892 #ifdef TRACE_MESSAGES
  893         printf("[%x]:---------->[socket]: c=<%d>cmd=%x(%s) f=%x #%d\n",
  894                 retaddr,
  895                 msg->header.typecookie,
  896                 msg->header.cmd,
  897                 msg->header.cmdstr,
  898                 msg->header.flags,
  899                 msg->header.token);
  900 #endif
  901 
  902         if (msg->header.typecookie == NGM_SOCKET_COOKIE) {
  903                 switch (msg->header.cmd) {
  904                 case NGM_SOCK_CMD_NOLINGER:
  905                         priv->flags |= NGS_FLAG_NOLINGER;
  906                         break;
  907                 case NGM_SOCK_CMD_LINGER:
  908                         priv->flags &= ~NGS_FLAG_NOLINGER;
  909                         break;
  910                 default:
  911                         error = EINVAL;         /* unknown command */
  912                 }
  913                 SOCKBUF_UNLOCK(&so->so_rcv);
  914 
  915                 /* Free the message and return. */
  916                 NG_FREE_MSG(msg);
  917                 return (error);
  918         }
  919 
  920         /* Get the return address into a sockaddr. */
  921         bzero(&addr, sizeof(addr));
  922         addr.sg_len = sizeof(addr);
  923         addr.sg_family = AF_NETGRAPH;
  924         addrlen = snprintf((char *)&addr.sg_data, sizeof(addr.sg_data),
  925             "[%x]:", retaddr);
  926         if (addrlen < 0 || addrlen > sizeof(addr.sg_data)) {
  927                 SOCKBUF_UNLOCK(&so->so_rcv);
  928                 printf("%s: snprintf([%x]) failed - %d\n", __func__, retaddr,
  929                     addrlen);
  930                 NG_FREE_MSG(msg);
  931                 return (EINVAL);
  932         }
  933 
  934         /* Copy the message itself into an mbuf chain. */
  935         m = m_devget((caddr_t)msg, sizeof(struct ng_mesg) + msg->header.arglen,
  936             0, NULL, NULL);
  937 
  938         /*
  939          * Here we free the message. We need to do that
  940          * regardless of whether we got mbufs.
  941          */
  942         NG_FREE_MSG(msg);
  943 
  944         if (m == NULL) {
  945                 SOCKBUF_UNLOCK(&so->so_rcv);
  946                 TRAP_ERROR;
  947                 return (ENOBUFS);
  948         }
  949 
  950         /* Send it up to the socket. */
  951         if (sbappendaddr_locked(&so->so_rcv, (struct sockaddr *)&addr, m,
  952             NULL) == 0) {
  953                 SOCKBUF_UNLOCK(&so->so_rcv);
  954                 TRAP_ERROR;
  955                 m_freem(m);
  956                 return (ENOBUFS);
  957         }
  958         sorwakeup_locked(so);
  959         
  960         return (error);
  961 }
  962 
  963 /*
  964  * Receive data on a hook
  965  */
  966 static int
  967 ngs_rcvdata(hook_p hook, item_p item)
  968 {
  969         struct ngsock *const priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
  970         struct ngpcb *const pcbp = priv->datasock;
  971         struct socket *so;
  972         struct sockaddr_ng *addr;
  973         char *addrbuf[NG_HOOKSIZ + 4];
  974         int addrlen;
  975         struct mbuf *m;
  976 
  977         NGI_GET_M(item, m);
  978         NG_FREE_ITEM(item);
  979 
  980         /* If there is no data socket, black-hole it. */
  981         if (pcbp == NULL) {
  982                 NG_FREE_M(m);
  983                 return (0);
  984         }
  985         so = pcbp->ng_socket;
  986 
  987         /* Get the return address into a sockaddr. */
  988         addrlen = strlen(NG_HOOK_NAME(hook));   /* <= NG_HOOKSIZ - 1 */
  989         addr = (struct sockaddr_ng *) addrbuf;
  990         addr->sg_len = addrlen + 3;
  991         addr->sg_family = AF_NETGRAPH;
  992         bcopy(NG_HOOK_NAME(hook), addr->sg_data, addrlen);
  993         addr->sg_data[addrlen] = '\0';
  994 
  995         /* Try to tell the socket which hook it came in on. */
  996         if (sbappendaddr(&so->so_rcv, (struct sockaddr *)addr, m, NULL) == 0) {
  997                 m_freem(m);
  998                 TRAP_ERROR;
  999                 return (ENOBUFS);
 1000         }
 1001         sorwakeup(so);
 1002         return (0);
 1003 }
 1004 
 1005 /*
 1006  * Hook disconnection
 1007  *
 1008  * For this type, removal of the last link destroys the node
 1009  * if the NOLINGER flag is set.
 1010  */
 1011 static int
 1012 ngs_disconnect(hook_p hook)
 1013 {
 1014         node_p node = NG_HOOK_NODE(hook);
 1015         struct ngsock *const priv = NG_NODE_PRIVATE(node);
 1016 
 1017         if ((priv->datasock) && (priv->datasock->ng_socket)) {
 1018                 if (NG_NODE_NUMHOOKS(node) == 1)
 1019                         priv->datasock->ng_socket->so_state |= SS_ISCONNECTED;
 1020                 else
 1021                         priv->datasock->ng_socket->so_state &= ~SS_ISCONNECTED;
 1022         }
 1023 
 1024         if ((priv->flags & NGS_FLAG_NOLINGER) &&
 1025             (NG_NODE_NUMHOOKS(node) == 0) && (NG_NODE_IS_VALID(node)))
 1026                 ng_rmnode_self(node);
 1027 
 1028         return (0);
 1029 }
 1030 
 1031 /*
 1032  * Do local shutdown processing.
 1033  * In this case, that involves making sure the socket
 1034  * knows we should be shutting down.
 1035  */
 1036 static int
 1037 ngs_shutdown(node_p node)
 1038 {
 1039         struct ngsock *const priv = NG_NODE_PRIVATE(node);
 1040         struct ngpcb *dpcbp, *pcbp;
 1041 
 1042         mtx_lock(&priv->mtx);
 1043         dpcbp = priv->datasock;
 1044         pcbp = priv->ctlsock;
 1045 
 1046         if (dpcbp != NULL)
 1047                 soisdisconnected(dpcbp->ng_socket);
 1048 
 1049         if (pcbp != NULL)
 1050                 soisdisconnected(pcbp->ng_socket);
 1051 
 1052         priv->node = NULL;
 1053         NG_NODE_SET_PRIVATE(node, NULL);
 1054         ng_socket_free_priv(priv);
 1055 
 1056         NG_NODE_UNREF(node);
 1057         return (0);
 1058 }
 1059 
 1060 static void
 1061 ng_socket_item_applied(void *context, int error)
 1062 {
 1063         struct ngsock *const priv = (struct ngsock *)context;
 1064 
 1065         mtx_lock(&priv->mtx);
 1066         priv->error = error;
 1067         wakeup(priv);
 1068         mtx_unlock(&priv->mtx);
 1069 
 1070 }
 1071 
 1072 static  int
 1073 dummy_disconnect(struct socket *so)
 1074 {
 1075         return (0);
 1076 }
 1077 /*
 1078  * Control and data socket type descriptors
 1079  *
 1080  * XXXRW: Perhaps _close should do something?
 1081  */
 1082 
 1083 static struct pr_usrreqs ngc_usrreqs = {
 1084         .pru_abort =            NULL,
 1085         .pru_attach =           ngc_attach,
 1086         .pru_bind =             ngc_bind,
 1087         .pru_connect =          ngc_connect,
 1088         .pru_detach =           ngc_detach,
 1089         .pru_disconnect =       dummy_disconnect,
 1090         .pru_peeraddr =         NULL,
 1091         .pru_send =             ngc_send,
 1092         .pru_shutdown =         NULL,
 1093         .pru_sockaddr =         ng_getsockaddr,
 1094         .pru_close =            NULL,
 1095 };
 1096 
 1097 static struct pr_usrreqs ngd_usrreqs = {
 1098         .pru_abort =            NULL,
 1099         .pru_attach =           ngd_attach,
 1100         .pru_bind =             NULL,
 1101         .pru_connect =          ngd_connect,
 1102         .pru_detach =           ngd_detach,
 1103         .pru_disconnect =       dummy_disconnect,
 1104         .pru_peeraddr =         NULL,
 1105         .pru_send =             ngd_send,
 1106         .pru_shutdown =         NULL,
 1107         .pru_sockaddr =         ng_getsockaddr,
 1108         .pru_close =            NULL,
 1109 };
 1110 
 1111 /*
 1112  * Definitions of protocols supported in the NETGRAPH domain.
 1113  */
 1114 
 1115 extern struct domain ngdomain;          /* stop compiler warnings */
 1116 
 1117 static struct protosw ngsw[] = {
 1118 {
 1119         .pr_type =              SOCK_DGRAM,
 1120         .pr_domain =            &ngdomain,
 1121         .pr_protocol =          NG_CONTROL,
 1122         .pr_flags =             PR_ATOMIC | PR_ADDR /* | PR_RIGHTS */,
 1123         .pr_usrreqs =           &ngc_usrreqs
 1124 },
 1125 {
 1126         .pr_type =              SOCK_DGRAM,
 1127         .pr_domain =            &ngdomain,
 1128         .pr_protocol =          NG_DATA,
 1129         .pr_flags =             PR_ATOMIC | PR_ADDR,
 1130         .pr_usrreqs =           &ngd_usrreqs
 1131 }
 1132 };
 1133 
 1134 struct domain ngdomain = {
 1135         .dom_family =           AF_NETGRAPH,
 1136         .dom_name =             "netgraph",
 1137         .dom_protosw =          ngsw,
 1138         .dom_protoswNPROTOSW =  &ngsw[sizeof(ngsw) / sizeof(ngsw[0])]
 1139 };
 1140 
 1141 /*
 1142  * Handle loading and unloading for this node type.
 1143  * This is to handle auxiliary linkages (e.g protocol domain addition).
 1144  */
 1145 static int
 1146 ngs_mod_event(module_t mod, int event, void *data)
 1147 {
 1148         int error = 0;
 1149 
 1150         switch (event) {
 1151         case MOD_LOAD:
 1152                 mtx_init(&ngsocketlist_mtx, "ng_socketlist", NULL, MTX_DEF);
 1153                 break;
 1154         case MOD_UNLOAD:
 1155                 /* Ensure there are no open netgraph sockets. */
 1156                 if (!LIST_EMPTY(&ngsocklist)) {
 1157                         error = EBUSY;
 1158                         break;
 1159                 }
 1160 #ifdef NOTYET
 1161                 /* Unregister protocol domain XXX can't do this yet.. */
 1162 #endif
 1163                 error = EBUSY;
 1164                 break;
 1165         default:
 1166                 error = EOPNOTSUPP;
 1167                 break;
 1168         }
 1169         return (error);
 1170 }
 1171 
 1172 VNET_DOMAIN_SET(ng);
 1173 
 1174 SYSCTL_INT(_net_graph, OID_AUTO, family, CTLFLAG_RD, 0, AF_NETGRAPH, "");
 1175 SYSCTL_NODE(_net_graph, OID_AUTO, data, CTLFLAG_RW, 0, "DATA");
 1176 SYSCTL_INT(_net_graph_data, OID_AUTO, proto, CTLFLAG_RD, 0, NG_DATA, "");
 1177 SYSCTL_NODE(_net_graph, OID_AUTO, control, CTLFLAG_RW, 0, "CONTROL");
 1178 SYSCTL_INT(_net_graph_control, OID_AUTO, proto, CTLFLAG_RD, 0, NG_CONTROL, "");
 1179 

Cache object: 25dbd3d2480cb90bdb544fa6a8662f9d


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.