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_tee.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 /*
    3  * ng_tee.c
    4  *
    5  * Copyright (c) 1996-1999 Whistle Communications, Inc.
    6  * All rights reserved.
    7  * 
    8  * Subject to the following obligations and disclaimer of warranty, use and
    9  * redistribution of this software, in source or object code forms, with or
   10  * without modifications are expressly permitted by Whistle Communications;
   11  * provided, however, that:
   12  * 1. Any and all reproductions of the source or object code must include the
   13  *    copyright notice above and the following disclaimer of warranties; and
   14  * 2. No rights are granted, in any manner or form, to use Whistle
   15  *    Communications, Inc. trademarks, including the mark "WHISTLE
   16  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
   17  *    such appears in the above copyright notice or in the software.
   18  * 
   19  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
   20  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
   21  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
   22  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
   23  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
   24  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
   25  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
   26  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
   27  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
   28  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
   29  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
   30  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
   31  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
   32  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   33  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   34  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
   35  * OF SUCH DAMAGE.
   36  *
   37  * Author: Julian Elischer <julian@freebsd.org>
   38  *
   39  * $FreeBSD$
   40  * $Whistle: ng_tee.c,v 1.18 1999/11/01 09:24:52 julian Exp $
   41  */
   42 
   43 /*
   44  * This node is like the tee(1) command and is useful for ``snooping.''
   45  * It has 4 hooks: left, right, left2right, and right2left. Data
   46  * entering from the right is passed to the left and duplicated on
   47  * right2left, and data entering from the left is passed to the right
   48  * and duplicated on left2right. Data entering from left2right is
   49  * sent to left, and data from right2left to right.
   50  */
   51 
   52 #include <sys/param.h>
   53 #include <sys/systm.h>
   54 #include <sys/errno.h>
   55 #include <sys/kernel.h>
   56 #include <sys/malloc.h>
   57 #include <sys/mbuf.h>
   58 #include <netgraph/ng_message.h>
   59 #include <netgraph/netgraph.h>
   60 #include <netgraph/ng_parse.h>
   61 #include <netgraph/ng_tee.h>
   62 
   63 /* Per hook info */
   64 struct hookinfo {
   65         hook_p                  hook;
   66         struct ng_tee_hookstat  stats;
   67 };
   68 
   69 /* Per node info */
   70 struct privdata {
   71         node_p                  node;
   72         struct hookinfo         left;
   73         struct hookinfo         right;
   74         struct hookinfo         left2right;
   75         struct hookinfo         right2left;
   76 };
   77 typedef struct privdata *sc_p;
   78 
   79 /* Netgraph methods */
   80 static ng_constructor_t ngt_constructor;
   81 static ng_rcvmsg_t      ngt_rcvmsg;
   82 static ng_shutdown_t    ngt_rmnode;
   83 static ng_newhook_t     ngt_newhook;
   84 static ng_rcvdata_t     ngt_rcvdata;
   85 static ng_disconnect_t  ngt_disconnect;
   86 
   87 /* Parse type for struct ng_tee_hookstat */
   88 static const struct ng_parse_struct_field ng_tee_hookstat_type_fields[]
   89         = NG_TEE_HOOKSTAT_INFO;
   90 static const struct ng_parse_type ng_tee_hookstat_type = {
   91         &ng_parse_struct_type,
   92         &ng_tee_hookstat_type_fields
   93 };
   94 
   95 /* Parse type for struct ng_tee_stats */
   96 static const struct ng_parse_struct_field ng_tee_stats_type_fields[]
   97         = NG_TEE_STATS_INFO(&ng_tee_hookstat_type);
   98 static const struct ng_parse_type ng_tee_stats_type = {
   99         &ng_parse_struct_type,
  100         &ng_tee_stats_type_fields
  101 };
  102 
  103 /* List of commands and how to convert arguments to/from ASCII */
  104 static const struct ng_cmdlist ng_tee_cmds[] = {
  105         {
  106           NGM_TEE_COOKIE,
  107           NGM_TEE_GET_STATS,
  108           "getstats",
  109           NULL,
  110           &ng_tee_stats_type
  111         },
  112         {
  113           NGM_TEE_COOKIE,
  114           NGM_TEE_CLR_STATS,
  115           "clrstats",
  116           NULL,
  117           NULL
  118         },
  119         {
  120           NGM_TEE_COOKIE,
  121           NGM_TEE_GETCLR_STATS,
  122           "getclrstats",
  123           NULL,
  124           &ng_tee_stats_type
  125         },
  126         { 0 }
  127 };
  128 
  129 /* Netgraph type descriptor */
  130 static struct ng_type ng_tee_typestruct = {
  131         NG_VERSION,
  132         NG_TEE_NODE_TYPE,
  133         NULL,
  134         ngt_constructor,
  135         ngt_rcvmsg,
  136         ngt_rmnode,
  137         ngt_newhook,
  138         NULL,
  139         NULL,
  140         ngt_rcvdata,
  141         ngt_rcvdata,
  142         ngt_disconnect,
  143         ng_tee_cmds
  144 };
  145 NETGRAPH_INIT(tee, &ng_tee_typestruct);
  146 
  147 /*
  148  * Node constructor
  149  */
  150 static int
  151 ngt_constructor(node_p *nodep)
  152 {
  153         sc_p privdata;
  154         int error = 0;
  155 
  156         MALLOC(privdata, sc_p, sizeof(*privdata), M_NETGRAPH, M_NOWAIT);
  157         if (privdata == NULL)
  158                 return (ENOMEM);
  159         bzero(privdata, sizeof(*privdata));
  160 
  161         if ((error = ng_make_node_common(&ng_tee_typestruct, nodep))) {
  162                 FREE(privdata, M_NETGRAPH);
  163                 return (error);
  164         }
  165         (*nodep)->private = privdata;
  166         privdata->node = *nodep;
  167         return (0);
  168 }
  169 
  170 /*
  171  * Add a hook
  172  */
  173 static int
  174 ngt_newhook(node_p node, hook_p hook, const char *name)
  175 {
  176         const sc_p sc = node->private;
  177 
  178         if (strcmp(name, NG_TEE_HOOK_RIGHT) == 0) {
  179                 sc->right.hook = hook;
  180                 bzero(&sc->right.stats, sizeof(sc->right.stats));
  181                 hook->private = &sc->right;
  182         } else if (strcmp(name, NG_TEE_HOOK_LEFT) == 0) {
  183                 sc->left.hook = hook;
  184                 bzero(&sc->left.stats, sizeof(sc->left.stats));
  185                 hook->private = &sc->left;
  186         } else if (strcmp(name, NG_TEE_HOOK_RIGHT2LEFT) == 0) {
  187                 sc->right2left.hook = hook;
  188                 bzero(&sc->right2left.stats, sizeof(sc->right2left.stats));
  189                 hook->private = &sc->right2left;
  190         } else if (strcmp(name, NG_TEE_HOOK_LEFT2RIGHT) == 0) {
  191                 sc->left2right.hook = hook;
  192                 bzero(&sc->left2right.stats, sizeof(sc->left2right.stats));
  193                 hook->private = &sc->left2right;
  194         } else
  195                 return (EINVAL);
  196         return (0);
  197 }
  198 
  199 /*
  200  * Receive a control message
  201  */
  202 static int
  203 ngt_rcvmsg(node_p node, struct ng_mesg *msg, const char *retaddr,
  204            struct ng_mesg **rptr)
  205 {
  206         const sc_p sc = node->private;
  207         struct ng_mesg *resp = NULL;
  208         int error = 0;
  209 
  210         switch (msg->header.typecookie) {
  211         case NGM_TEE_COOKIE:
  212                 switch (msg->header.cmd) {
  213                 case NGM_TEE_GET_STATS:
  214                 case NGM_TEE_CLR_STATS:
  215                 case NGM_TEE_GETCLR_STATS:
  216                     {
  217                         struct ng_tee_stats *stats;
  218 
  219                         if (msg->header.cmd != NGM_TEE_CLR_STATS) {
  220                                 NG_MKRESPONSE(resp, msg,
  221                                     sizeof(*stats), M_NOWAIT);
  222                                 if (resp == NULL) {
  223                                         error = ENOMEM;
  224                                         goto done;
  225                                 }
  226                                 stats = (struct ng_tee_stats *)resp->data;
  227                                 bcopy(&sc->right.stats, &stats->right,
  228                                     sizeof(stats->right));
  229                                 bcopy(&sc->left.stats, &stats->left,
  230                                     sizeof(stats->left));
  231                                 bcopy(&sc->right2left.stats, &stats->right2left,
  232                                     sizeof(stats->right2left));
  233                                 bcopy(&sc->left2right.stats, &stats->left2right,
  234                                     sizeof(stats->left2right));
  235                         }
  236                         if (msg->header.cmd != NGM_TEE_GET_STATS) {
  237                                 bzero(&sc->right.stats,
  238                                     sizeof(sc->right.stats));
  239                                 bzero(&sc->left.stats,
  240                                     sizeof(sc->left.stats));
  241                                 bzero(&sc->right2left.stats,
  242                                     sizeof(sc->right2left.stats));
  243                                 bzero(&sc->left2right.stats,
  244                                     sizeof(sc->left2right.stats));
  245                         }
  246                         break;
  247                     }
  248                 default:
  249                         error = EINVAL;
  250                         break;
  251                 }
  252                 break;
  253         default:
  254                 error = EINVAL;
  255                 break;
  256         }
  257         if (rptr)
  258                 *rptr = resp;
  259         else if (resp)
  260                 FREE(resp, M_NETGRAPH);
  261 
  262 done:
  263         FREE(msg, M_NETGRAPH);
  264         return (error);
  265 }
  266 
  267 /*
  268  * Receive data on a hook
  269  *
  270  * If data comes in the right link send a copy out right2left, and then
  271  * send the original onwards out through the left link.
  272  * Do the opposite for data coming in from the left link.
  273  * Data coming in right2left or left2right is forwarded
  274  * on through the appropriate destination hook as if it had come
  275  * from the other side.
  276  */
  277 static int
  278 ngt_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
  279 {
  280         const sc_p sc = hook->node->private;
  281         struct hookinfo *const hinfo = (struct hookinfo *) hook->private;
  282         struct hookinfo *dest;
  283         struct hookinfo *dup;
  284         int error = 0;
  285 
  286         /* Which hook? */
  287         if (hinfo == &sc->left) {
  288                 dup = &sc->left2right;
  289                 dest = &sc->right;
  290         } else if (hinfo == &sc->right) {
  291                 dup = &sc->right2left;
  292                 dest = &sc->left;
  293         } else if (hinfo == &sc->right2left) {
  294                 dup = NULL;
  295                 dest = &sc->right;
  296         } else if (hinfo == &sc->left2right) {
  297                 dup = NULL;
  298                 dest = &sc->left;
  299         } else
  300                 panic("%s: no hook!", __FUNCTION__);
  301 
  302         /* Update stats on incoming hook */
  303         hinfo->stats.inOctets += m->m_pkthdr.len;
  304         hinfo->stats.inFrames++;
  305 
  306         /*
  307          * Don't make a copy if only the dup hook exists.
  308          */
  309         if ((dup && dup->hook) && (dest->hook == NULL)) {
  310                 dest = dup;
  311                 dup = NULL;
  312         }
  313 
  314         /* Duplicate packet and meta info if requried */
  315         if (dup && dup->hook) {
  316                 struct mbuf *m2;
  317                 meta_p meta2;
  318 
  319                 /* Copy packet */
  320                 m2 = m_dup(m, M_NOWAIT);
  321                 if (m2 == NULL) {
  322                         NG_FREE_DATA(m, meta);
  323                         return (ENOBUFS);
  324                 }
  325 
  326                 /* Copy meta info */
  327                 if (meta != NULL) {
  328                         MALLOC(meta2, meta_p,
  329                             meta->used_len, M_NETGRAPH, M_NOWAIT);
  330                         if (meta2 == NULL) {
  331                                 m_freem(m2);
  332                                 NG_FREE_DATA(m, meta);
  333                                 return (ENOMEM);
  334                         }
  335                         bcopy(meta, meta2, meta->used_len);
  336                         meta2->allocated_len = meta->used_len;
  337                 } else
  338                         meta2 = NULL;
  339 
  340                 /* Deliver duplicate */
  341                 dup->stats.outOctets += m->m_pkthdr.len;
  342                 dup->stats.outFrames++;
  343                 NG_SEND_DATA(error, dup->hook, m2, meta2);
  344         }
  345 
  346         /* Deliver frame out destination hook */
  347         if (dest->hook != NULL) {
  348                 dest->stats.outOctets += m->m_pkthdr.len;
  349                 dest->stats.outFrames++;
  350         }
  351         NG_SEND_DATA(error, dest->hook, m, meta);
  352         return (error);
  353 }
  354 
  355 /*
  356  * Shutdown processing
  357  *
  358  * This is tricky. If we have both a left and right hook, then we
  359  * probably want to extricate ourselves and leave the two peers
  360  * still linked to each other. Otherwise we should just shut down as
  361  * a normal node would.
  362  *
  363  * To keep the scope of info correct the routine to "extract" a node
  364  * from two links is in ng_base.c.
  365  */
  366 static int
  367 ngt_rmnode(node_p node)
  368 {
  369         const sc_p privdata = node->private;
  370 
  371         node->flags |= NG_INVALID;
  372         if (privdata->left.hook && privdata->right.hook)
  373                 ng_bypass(privdata->left.hook, privdata->right.hook);
  374         ng_cutlinks(node);
  375         ng_unname(node);
  376         node->private = NULL;
  377         ng_unref(privdata->node);
  378         FREE(privdata, M_NETGRAPH);
  379         return (0);
  380 }
  381 
  382 /*
  383  * Hook disconnection
  384  */
  385 static int
  386 ngt_disconnect(hook_p hook)
  387 {
  388         struct hookinfo *const hinfo = (struct hookinfo *) hook->private;
  389 
  390         KASSERT(hinfo != NULL, ("%s: null info", __FUNCTION__));
  391         hinfo->hook = NULL;
  392         if (hook->node->numhooks == 0)
  393                 ng_rmnode(hook->node);
  394         return (0);
  395 }
  396 

Cache object: 708aee13c3764a4d1fd0c1d5a02e1f11


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