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

Cache object: 1acaeddff2c2b767f73e4aaeea44a0f5


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