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

Cache object: f02021a343102e8f44e63d95f07c3eff


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