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: stable/3/sys/netgraph/ng_tee.c 67532 2000-10-24 22:45:12Z julian $
   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_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_info
   89         ng_tee_hookstat_type_info = 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_info,
   93 };
   94 
   95 /* Parse type for struct ng_tee_stats */
   96 static const struct ng_parse_struct_info
   97         ng_tee_stats_type_info = 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_info,
  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         { 0 }
  120 };
  121 
  122 /* Netgraph type descriptor */
  123 static struct ng_type ng_tee_typestruct = {
  124         NG_VERSION,
  125         NG_TEE_NODE_TYPE,
  126         NULL,
  127         ngt_constructor,
  128         ngt_rcvmsg,
  129         ngt_rmnode,
  130         ngt_newhook,
  131         NULL,
  132         NULL,
  133         ngt_rcvdata,
  134         ngt_rcvdata,
  135         ngt_disconnect,
  136         ng_tee_cmds
  137 };
  138 NETGRAPH_INIT(tee, &ng_tee_typestruct);
  139 
  140 /*
  141  * Node constructor
  142  */
  143 static int
  144 ngt_constructor(node_p *nodep)
  145 {
  146         sc_p privdata;
  147         int error = 0;
  148 
  149         MALLOC(privdata, sc_p, sizeof(*privdata), M_NETGRAPH, M_WAITOK);
  150         if (privdata == NULL)
  151                 return (ENOMEM);
  152         bzero(privdata, sizeof(*privdata));
  153 
  154         if ((error = ng_make_node_common(&ng_tee_typestruct, nodep))) {
  155                 FREE(privdata, M_NETGRAPH);
  156                 return (error);
  157         }
  158         (*nodep)->private = privdata;
  159         privdata->node = *nodep;
  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 = node->private;
  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                 hook->private = &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                 hook->private = &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                 hook->private = &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                 hook->private = &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, struct ng_mesg *msg, const char *retaddr,
  197            struct ng_mesg **rptr)
  198 {
  199         const sc_p sc = node->private;
  200         struct ng_mesg *resp = NULL;
  201         int error = 0;
  202 
  203         switch (msg->header.typecookie) {
  204         case NGM_TEE_COOKIE:
  205                 switch (msg->header.cmd) {
  206                 case NGM_TEE_GET_STATS:
  207                     {
  208                         struct ng_tee_stats *stats;
  209 
  210                         NG_MKRESPONSE(resp, msg,
  211                             sizeof(struct ng_tee_stats), M_NOWAIT);
  212                         if (resp == NULL) {
  213                                 error = ENOMEM;
  214                                 goto done;
  215                         }
  216                         stats = (struct ng_tee_stats *) resp->data;
  217                         bcopy(&sc->right.stats,
  218                             &stats->right, sizeof(stats->right));
  219                         bcopy(&sc->left.stats,
  220                             &stats->left, sizeof(stats->left));
  221                         bcopy(&sc->right2left.stats,
  222                             &stats->right2left, sizeof(stats->right2left));
  223                         bcopy(&sc->left2right.stats,
  224                             &stats->left2right, sizeof(stats->left2right));
  225                         break;
  226                     }
  227                 case NGM_TEE_CLR_STATS:
  228                         bzero(&sc->right.stats, sizeof(sc->right.stats));
  229                         bzero(&sc->left.stats, sizeof(sc->left.stats));
  230                         bzero(&sc->right2left.stats,
  231                             sizeof(sc->right2left.stats));
  232                         bzero(&sc->left2right.stats,
  233                             sizeof(sc->left2right.stats));
  234                         break;
  235                 default:
  236                         error = EINVAL;
  237                         break;
  238                 }
  239                 break;
  240         default:
  241                 error = EINVAL;
  242                 break;
  243         }
  244         if (rptr)
  245                 *rptr = resp;
  246         else if (resp)
  247                 FREE(resp, M_NETGRAPH);
  248 
  249 done:
  250         FREE(msg, M_NETGRAPH);
  251         return (error);
  252 }
  253 
  254 /*
  255  * Receive data on a hook
  256  *
  257  * If data comes in the right link send a copy out right2left, and then
  258  * send the original onwards out through the left link.
  259  * Do the opposite for data coming in from the left link.
  260  * Data coming in right2left or left2right is forwarded
  261  * on through the appropriate destination hook as if it had come
  262  * from the other side.
  263  */
  264 static int
  265 ngt_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
  266 {
  267         const sc_p sc = hook->node->private;
  268         struct hookinfo *const hinfo = (struct hookinfo *) hook->private;
  269         struct hookinfo *dest;
  270         struct hookinfo *dup;
  271         int error = 0;
  272 
  273         /* Which hook? */
  274         if (hinfo == &sc->left) {
  275                 dup = &sc->left2right;
  276                 dest = &sc->right;
  277         } else if (hinfo == &sc->right) {
  278                 dup = &sc->right2left;
  279                 dest = &sc->left;
  280         } else if (hinfo == &sc->right2left) {
  281                 dup = NULL;
  282                 dest = &sc->left;
  283         } else if (hinfo == &sc->left2right) {
  284                 dup = NULL;
  285                 dest = &sc->right;
  286         } else
  287                 panic("%s: no hook!", __FUNCTION__);
  288 
  289         /* Update stats on incoming hook */
  290         hinfo->stats.inOctets += m->m_pkthdr.len;
  291         hinfo->stats.inFrames++;
  292 
  293         /* Duplicate packet and meta info if requried */
  294         if (dup != NULL) {
  295                 struct mbuf *m2;
  296                 meta_p meta2;
  297 
  298                 /* Copy packet */
  299                 m2 = m_dup(m, M_NOWAIT);
  300                 if (m2 == NULL) {
  301                         NG_FREE_DATA(m, meta);
  302                         return (ENOBUFS);
  303                 }
  304 
  305                 /* Copy meta info */
  306                 if (meta != NULL) {
  307                         MALLOC(meta2, meta_p,
  308                             meta->used_len, M_NETGRAPH, M_NOWAIT);
  309                         if (meta2 == NULL) {
  310                                 m_freem(m2);
  311                                 NG_FREE_DATA(m, meta);
  312                                 return (ENOMEM);
  313                         }
  314                         bcopy(meta, meta2, meta->used_len);
  315                         meta2->allocated_len = meta->used_len;
  316                 } else
  317                         meta2 = NULL;
  318 
  319                 /* Deliver duplicate */
  320                 dup->stats.outOctets += m->m_pkthdr.len;
  321                 dup->stats.outFrames++;
  322                 NG_SEND_DATA(error, dup->hook, m2, meta2);
  323         }
  324 
  325         /* Deliver frame out destination hook */
  326         dest->stats.outOctets += m->m_pkthdr.len;
  327         dest->stats.outFrames++;
  328         NG_SEND_DATA(error, dest->hook, m, meta);
  329         return (0);
  330 }
  331 
  332 /*
  333  * Shutdown processing
  334  *
  335  * This is tricky. If we have both a left and right hook, then we
  336  * probably want to extricate ourselves and leave the two peers
  337  * still linked to each other. Otherwise we should just shut down as
  338  * a normal node would.
  339  *
  340  * To keep the scope of info correct the routine to "extract" a node
  341  * from two links is in ng_base.c.
  342  */
  343 static int
  344 ngt_rmnode(node_p node)
  345 {
  346         const sc_p privdata = node->private;
  347 
  348         node->flags |= NG_INVALID;
  349         if (privdata->left.hook && privdata->right.hook)
  350                 ng_bypass(privdata->left.hook, privdata->right.hook);
  351         ng_cutlinks(node);
  352         ng_unname(node);
  353         node->private = NULL;
  354         ng_unref(privdata->node);
  355         FREE(privdata, M_NETGRAPH);
  356         return (0);
  357 }
  358 
  359 /*
  360  * Hook disconnection
  361  */
  362 static int
  363 ngt_disconnect(hook_p hook)
  364 {
  365         struct hookinfo *const hinfo = (struct hookinfo *) hook->private;
  366 
  367         KASSERT(hinfo != NULL, ("%s: null info", __FUNCTION__));
  368         hinfo->hook = NULL;
  369         if (hook->node->numhooks == 0)
  370                 ng_rmnode(hook->node);
  371         return (0);
  372 }
  373 

Cache object: be05e9cc1d6a78c6e420b778d5f0ce8f


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