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

Cache object: 82825ae55a7bb60291cdd035a9209d12


[ 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.