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-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 /*
    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: releng/5.1/sys/netgraph/ng_tee.c 111119 2003-02-19 05:47:46Z imp $
   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 right, and data from right2left to left.
   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_shutdown;
   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_ABI_VERSION,
  132         NG_TEE_NODE_TYPE,
  133         NULL,
  134         ngt_constructor,
  135         ngt_rcvmsg,
  136         ngt_shutdown,
  137         ngt_newhook,
  138         NULL,
  139         NULL,
  140         ngt_rcvdata,
  141         ngt_disconnect,
  142         ng_tee_cmds
  143 };
  144 NETGRAPH_INIT(tee, &ng_tee_typestruct);
  145 
  146 /*
  147  * Node constructor
  148  */
  149 static int
  150 ngt_constructor(node_p node)
  151 {
  152         sc_p privdata;
  153 
  154         MALLOC(privdata, sc_p, sizeof(*privdata), M_NETGRAPH, M_NOWAIT|M_ZERO);
  155         if (privdata == NULL)
  156                 return (ENOMEM);
  157 
  158         NG_NODE_SET_PRIVATE(node, privdata);
  159         privdata->node = node;
  160         return (0);
  161 }
  162 
  163 /*
  164  * Add a hook
  165  */
  166 static int
  167 ngt_newhook(node_p node, hook_p hook, const char *name)
  168 {
  169         const sc_p sc = NG_NODE_PRIVATE(node);
  170 
  171         if (strcmp(name, NG_TEE_HOOK_RIGHT) == 0) {
  172                 sc->right.hook = hook;
  173                 bzero(&sc->right.stats, sizeof(sc->right.stats));
  174                 NG_HOOK_SET_PRIVATE(hook, &sc->right);
  175         } else if (strcmp(name, NG_TEE_HOOK_LEFT) == 0) {
  176                 sc->left.hook = hook;
  177                 bzero(&sc->left.stats, sizeof(sc->left.stats));
  178                 NG_HOOK_SET_PRIVATE(hook, &sc->left);
  179         } else if (strcmp(name, NG_TEE_HOOK_RIGHT2LEFT) == 0) {
  180                 sc->right2left.hook = hook;
  181                 bzero(&sc->right2left.stats, sizeof(sc->right2left.stats));
  182                 NG_HOOK_SET_PRIVATE(hook, &sc->right2left);
  183         } else if (strcmp(name, NG_TEE_HOOK_LEFT2RIGHT) == 0) {
  184                 sc->left2right.hook = hook;
  185                 bzero(&sc->left2right.stats, sizeof(sc->left2right.stats));
  186                 NG_HOOK_SET_PRIVATE(hook, &sc->left2right);
  187         } else
  188                 return (EINVAL);
  189         return (0);
  190 }
  191 
  192 /*
  193  * Receive a control message
  194  */
  195 static int
  196 ngt_rcvmsg(node_p node, item_p item, hook_p lasthook)
  197 {
  198         const sc_p sc = NG_NODE_PRIVATE(node);
  199         struct ng_mesg *resp = NULL;
  200         int error = 0;
  201         struct ng_mesg *msg;
  202 
  203         NGI_GET_MSG(item, msg);
  204         switch (msg->header.typecookie) {
  205         case NGM_TEE_COOKIE:
  206                 switch (msg->header.cmd) {
  207                 case NGM_TEE_GET_STATS:
  208                 case NGM_TEE_CLR_STATS:
  209                 case NGM_TEE_GETCLR_STATS:
  210                     {
  211                         struct ng_tee_stats *stats;
  212 
  213                         if (msg->header.cmd != NGM_TEE_CLR_STATS) {
  214                                 NG_MKRESPONSE(resp, msg,
  215                                     sizeof(*stats), M_NOWAIT);
  216                                 if (resp == NULL) {
  217                                         error = ENOMEM;
  218                                         goto done;
  219                                 }
  220                                 stats = (struct ng_tee_stats *)resp->data;
  221                                 bcopy(&sc->right.stats, &stats->right,
  222                                     sizeof(stats->right));
  223                                 bcopy(&sc->left.stats, &stats->left,
  224                                     sizeof(stats->left));
  225                                 bcopy(&sc->right2left.stats, &stats->right2left,
  226                                     sizeof(stats->right2left));
  227                                 bcopy(&sc->left2right.stats, &stats->left2right,
  228                                     sizeof(stats->left2right));
  229                         }
  230                         if (msg->header.cmd != NGM_TEE_GET_STATS) {
  231                                 bzero(&sc->right.stats,
  232                                     sizeof(sc->right.stats));
  233                                 bzero(&sc->left.stats,
  234                                     sizeof(sc->left.stats));
  235                                 bzero(&sc->right2left.stats,
  236                                     sizeof(sc->right2left.stats));
  237                                 bzero(&sc->left2right.stats,
  238                                     sizeof(sc->left2right.stats));
  239                         }
  240                         break;
  241                     }
  242                 default:
  243                         error = EINVAL;
  244                         break;
  245                 }
  246                 break;
  247         case NGM_FLOW_COOKIE:
  248                 if (lasthook)  {
  249                         if (lasthook == sc->left.hook) {
  250                                 if (sc->right.hook) {
  251                                         NGI_MSG(item) = msg;
  252                                         NG_FWD_ITEM_HOOK(error, item,
  253                                                         sc->right.hook);
  254                                         return (error);
  255                                 }
  256                         } else {
  257                                 if (sc->left.hook) {
  258                                         NGI_MSG(item) = msg;
  259                                         NG_FWD_ITEM_HOOK(error, item, 
  260                                                         sc->left.hook);
  261                                         return (error);
  262                                 }
  263                         }
  264                 }
  265                 break;
  266         default:
  267                 error = EINVAL;
  268                 break;
  269         }
  270 done:
  271         NG_RESPOND_MSG(error, node, item, resp);
  272         NG_FREE_MSG(msg);
  273         return (error);
  274 }
  275 
  276 /*
  277  * Receive data on a hook
  278  *
  279  * If data comes in the right link send a copy out right2left, and then
  280  * send the original onwards out through the left link.
  281  * Do the opposite for data coming in from the left link.
  282  * Data coming in right2left or left2right is forwarded
  283  * on through the appropriate destination hook as if it had come
  284  * from the other side.
  285  */
  286 static int
  287 ngt_rcvdata(hook_p hook, item_p item)
  288 {
  289         const sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
  290         struct hookinfo *const hinfo = NG_HOOK_PRIVATE(hook);
  291         struct hookinfo *dest;
  292         struct hookinfo *dup;
  293         int error = 0;
  294         struct mbuf *m;
  295         meta_p meta;
  296 
  297         m = NGI_M(item);
  298         meta = NGI_META(item); /* leave these owned by the item */
  299         /* Which hook? */
  300         if (hinfo == &sc->left) {
  301                 dup = &sc->left2right;
  302                 dest = &sc->right;
  303         } else if (hinfo == &sc->right) {
  304                 dup = &sc->right2left;
  305                 dest = &sc->left;
  306         } else if (hinfo == &sc->right2left) {
  307                 dup = NULL;
  308                 dest = &sc->right;
  309         } else if (hinfo == &sc->left2right) {
  310                 dup = NULL;
  311                 dest = &sc->left;
  312         } else {
  313                 panic("%s: no hook!", __func__);
  314 #ifdef  RESTARTABLE_PANICS
  315                 return(EINVAL);
  316 #endif
  317         }
  318 
  319         /* Update stats on incoming hook */
  320         hinfo->stats.inOctets += m->m_pkthdr.len;
  321         hinfo->stats.inFrames++;
  322 
  323         /*
  324          * Don't make a copy if only the dup hook exists.
  325          */
  326         if ((dup && dup->hook) && (dest->hook == NULL)) {
  327                 dest = dup;
  328                 dup = NULL;
  329         }
  330 
  331         /* Duplicate packet and meta info if requried */
  332         if (dup != NULL) {
  333                 struct mbuf *m2;
  334                 meta_p meta2;
  335 
  336                 /* Copy packet (failure will not stop the original)*/
  337                 m2 = m_dup(m, M_DONTWAIT);
  338                 if (m2) {
  339 
  340                         /* Copy meta info */
  341                         /* If we can't get a copy, tough.. */
  342                         if (meta != NULL) {
  343                                 meta2 = ng_copy_meta(meta);
  344                         } else
  345                                 meta2 = NULL;
  346 
  347                         /* Deliver duplicate */
  348                         dup->stats.outOctets += m->m_pkthdr.len;
  349                         dup->stats.outFrames++;
  350                         NG_SEND_DATA(error, dup->hook, m2, meta2);
  351                 }
  352         }
  353         /* Deliver frame out destination hook */
  354         dest->stats.outOctets += m->m_pkthdr.len;
  355         dest->stats.outFrames++;
  356         if (dest->hook)
  357                 NG_FWD_ITEM_HOOK(error, item, dest->hook);
  358         else
  359                 NG_FREE_ITEM(item);
  360         return (0);
  361 }
  362 
  363 /*
  364  * Shutdown processing
  365  *
  366  * This is tricky. If we have both a left and right hook, then we
  367  * probably want to extricate ourselves and leave the two peers
  368  * still linked to each other. Otherwise we should just shut down as
  369  * a normal node would.
  370  *
  371  * To keep the scope of info correct the routine to "extract" a node
  372  * from two links is in ng_base.c.
  373  */
  374 static int
  375 ngt_shutdown(node_p node)
  376 {
  377         const sc_p privdata = NG_NODE_PRIVATE(node);
  378 
  379 #if 0 /* can never happen as cutlinks is already called */
  380         if (privdata->left.hook && privdata->right.hook)
  381                 ng_bypass(privdata->left.hook, privdata->right.hook);
  382 #endif
  383         NG_NODE_SET_PRIVATE(node, NULL);
  384         NG_NODE_UNREF(privdata->node);
  385         FREE(privdata, M_NETGRAPH);
  386         return (0);
  387 }
  388 
  389 /*
  390  * Hook disconnection
  391  */
  392 static int
  393 ngt_disconnect(hook_p hook)
  394 {
  395         struct hookinfo *const hinfo = NG_HOOK_PRIVATE(hook);
  396 
  397         KASSERT(hinfo != NULL, ("%s: null info", __func__));
  398         hinfo->hook = NULL;
  399         if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
  400         && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
  401                 ng_rmnode_self(NG_HOOK_NODE(hook));
  402         return (0);
  403 }
  404 

Cache object: 282192ed525169a16456ab6d457f5b82


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