5 * Copyright (c) 1996-1999 Whistle Communications, Inc.
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.
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
37 * Author: Julian Elischer <julian@freebsd.org>
40 * $Whistle: ng_tee.c,v 1.18 1999/11/01 09:24:52 julian Exp $
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.
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>
58 #include <netgraph/ng_message.h>
59 #include <netgraph/netgraph.h>
60 #include <netgraph/ng_parse.h>
61 #include <netgraph/ng_tee.h>
66 struct ng_tee_hookstat stats;
73 struct hookinfo right;
74 struct hookinfo left2right;
75 struct hookinfo right2left;
77 typedef struct privdata *sc_p;
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;
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,
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,
103 /* List of commands and how to convert arguments to/from ASCII */
104 static const struct ng_cmdlist ng_tee_cmds[] = {
121 NGM_TEE_GETCLR_STATS,
129 /* Netgraph type descriptor */
130 static struct ng_type ng_tee_typestruct = {
144 NETGRAPH_INIT(tee, &ng_tee_typestruct);
150 ngt_constructor(node_p node)
154 MALLOC(privdata, sc_p, sizeof(*privdata), M_NETGRAPH, M_NOWAIT|M_ZERO);
155 if (privdata == NULL)
158 NG_NODE_SET_PRIVATE(node, privdata);
159 privdata->node = node;
167 ngt_newhook(node_p node, hook_p hook, const char *name)
169 const sc_p sc = NG_NODE_PRIVATE(node);
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);
193 * Receive a control message
196 ngt_rcvmsg(node_p node, item_p item, hook_p lasthook)
198 const sc_p sc = NG_NODE_PRIVATE(node);
199 struct ng_mesg *resp = NULL;
203 NGI_GET_MSG(item, msg);
204 switch (msg->header.typecookie) {
206 switch (msg->header.cmd) {
207 case NGM_TEE_GET_STATS:
208 case NGM_TEE_CLR_STATS:
209 case NGM_TEE_GETCLR_STATS:
211 struct ng_tee_stats *stats;
213 if (msg->header.cmd != NGM_TEE_CLR_STATS) {
214 NG_MKRESPONSE(resp, msg,
215 sizeof(*stats), M_NOWAIT);
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));
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));
247 case NGM_FLOW_COOKIE:
249 if (lasthook == sc->left.hook) {
250 if (sc->right.hook) {
252 NG_FWD_ITEM_HOOK(error, item,
259 NG_FWD_ITEM_HOOK(error, item,
271 NG_RESPOND_MSG(error, node, item, resp);
277 * Receive data on a hook
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.
287 ngt_rcvdata(hook_p hook, item_p item)
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;
298 meta = NGI_META(item); /* leave these owned by the item */
300 if (hinfo == &sc->left) {
301 dup = &sc->left2right;
303 } else if (hinfo == &sc->right) {
304 dup = &sc->right2left;
306 } else if (hinfo == &sc->right2left) {
309 } else if (hinfo == &sc->left2right) {
313 panic("%s: no hook!", __func__);
314 #ifdef RESTARTABLE_PANICS
319 /* Update stats on incoming hook */
320 hinfo->stats.inOctets += m->m_pkthdr.len;
321 hinfo->stats.inFrames++;
324 * Don't make a copy if only the dup hook exists.
326 if ((dup && dup->hook) && (dest->hook == NULL)) {
331 /* Duplicate packet and meta info if requried */
336 /* Copy packet (failure will not stop the original)*/
337 m2 = m_dup(m, M_NOWAIT);
341 /* If we can't get a copy, tough.. */
343 meta2 = ng_copy_meta(meta);
347 /* Deliver duplicate */
348 dup->stats.outOctets += m->m_pkthdr.len;
349 dup->stats.outFrames++;
350 NG_SEND_DATA(error, dup->hook, m2, meta2);
353 /* Deliver frame out destination hook */
354 dest->stats.outOctets += m->m_pkthdr.len;
355 dest->stats.outFrames++;
357 NG_FWD_ITEM_HOOK(error, item, dest->hook);
364 * Shutdown processing
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.
371 * To keep the scope of info correct the routine to "extract" a node
372 * from two links is in ng_base.c.
375 ngt_shutdown(node_p node)
377 const sc_p privdata = NG_NODE_PRIVATE(node);
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);
383 NG_NODE_SET_PRIVATE(node, NULL);
384 NG_NODE_UNREF(privdata->node);
385 FREE(privdata, M_NETGRAPH);
393 ngt_disconnect(hook_p hook)
395 struct hookinfo *const hinfo = NG_HOOK_PRIVATE(hook);
397 KASSERT(hinfo != NULL, ("%s: null info", __func__));
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));