7 * Copyright (c) 1996-1999 Whistle Communications, Inc.
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.
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
39 * Author: Julian Elischer <julian@freebsd.org>
42 * $Whistle: ng_tee.c,v 1.18 1999/11/01 09:24:52 julian Exp $
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.
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>
60 #include <netgraph/ng_message.h>
61 #include <netgraph/netgraph.h>
62 #include <netgraph/ng_parse.h>
63 #include <netgraph/ng_tee.h>
68 struct hookinfo *dest, *dup;
69 struct ng_tee_hookstat stats;
71 typedef struct hookinfo *hi_p;
76 struct hookinfo right;
77 struct hookinfo left2right;
78 struct hookinfo right2left;
80 typedef struct privdata *sc_p;
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;
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
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
107 /* List of commands and how to convert arguments to/from ASCII */
108 static const struct ng_cmdlist ng_tee_cmds[] = {
125 NGM_TEE_GETCLR_STATS,
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,
146 NETGRAPH_INIT(tee, &ng_tee_typestruct);
152 ng_tee_constructor(node_p node)
156 privdata = malloc(sizeof(*privdata), M_NETGRAPH, M_WAITOK | M_ZERO);
158 NG_NODE_SET_PRIVATE(node, privdata);
166 ng_tee_newhook(node_p node, hook_p hook, const char *name)
168 sc_p privdata = NG_NODE_PRIVATE(node);
171 /* Precalculate internal pathes. */
172 if (strcmp(name, NG_TEE_HOOK_RIGHT) == 0) {
173 hinfo = &privdata->right;
174 if (privdata->left.dest)
175 privdata->left.dup = privdata->left.dest;
176 privdata->left.dest = hinfo;
177 privdata->right2left.dest = hinfo;
178 } else if (strcmp(name, NG_TEE_HOOK_LEFT) == 0) {
179 hinfo = &privdata->left;
180 if (privdata->right.dest)
181 privdata->right.dup = privdata->right.dest;
182 privdata->right.dest = hinfo;
183 privdata->left2right.dest = hinfo;
184 } else if (strcmp(name, NG_TEE_HOOK_RIGHT2LEFT) == 0) {
185 hinfo = &privdata->right2left;
186 if (privdata->right.dest)
187 privdata->right.dup = hinfo;
189 privdata->right.dest = hinfo;
190 } else if (strcmp(name, NG_TEE_HOOK_LEFT2RIGHT) == 0) {
191 hinfo = &privdata->left2right;
192 if (privdata->left.dest)
193 privdata->left.dup = hinfo;
195 privdata->left.dest = hinfo;
199 bzero(&hinfo->stats, sizeof(hinfo->stats));
200 NG_HOOK_SET_PRIVATE(hook, hinfo);
205 * Receive a control message
208 ng_tee_rcvmsg(node_p node, item_p item, hook_p lasthook)
210 const sc_p sc = NG_NODE_PRIVATE(node);
211 struct ng_mesg *resp = NULL;
215 NGI_GET_MSG(item, msg);
216 switch (msg->header.typecookie) {
218 switch (msg->header.cmd) {
219 case NGM_TEE_GET_STATS:
220 case NGM_TEE_CLR_STATS:
221 case NGM_TEE_GETCLR_STATS:
223 struct ng_tee_stats *stats;
225 if (msg->header.cmd != NGM_TEE_CLR_STATS) {
226 NG_MKRESPONSE(resp, msg,
227 sizeof(*stats), M_NOWAIT);
232 stats = (struct ng_tee_stats *)resp->data;
233 bcopy(&sc->right.stats, &stats->right,
234 sizeof(stats->right));
235 bcopy(&sc->left.stats, &stats->left,
236 sizeof(stats->left));
237 bcopy(&sc->right2left.stats, &stats->right2left,
238 sizeof(stats->right2left));
239 bcopy(&sc->left2right.stats, &stats->left2right,
240 sizeof(stats->left2right));
242 if (msg->header.cmd != NGM_TEE_GET_STATS) {
243 bzero(&sc->right.stats,
244 sizeof(sc->right.stats));
245 bzero(&sc->left.stats,
246 sizeof(sc->left.stats));
247 bzero(&sc->right2left.stats,
248 sizeof(sc->right2left.stats));
249 bzero(&sc->left2right.stats,
250 sizeof(sc->left2right.stats));
259 case NGM_FLOW_COOKIE:
260 if (lasthook == sc->left.hook || lasthook == sc->right.hook) {
261 hi_p const hinfo = NG_HOOK_PRIVATE(lasthook);
262 if (hinfo && hinfo->dest) {
264 NG_FWD_ITEM_HOOK(error, item, hinfo->dest->hook);
274 NG_RESPOND_MSG(error, node, item, resp);
280 * Receive data on a hook
282 * If data comes in the right link send a copy out right2left, and then
283 * send the original onwards out through the left link.
284 * Do the opposite for data coming in from the left link.
285 * Data coming in right2left or left2right is forwarded
286 * on through the appropriate destination hook as if it had come
287 * from the other side.
290 ng_tee_rcvdata(hook_p hook, item_p item)
292 const hi_p hinfo = NG_HOOK_PRIVATE(hook);
299 /* Update stats on incoming hook */
300 hinfo->stats.inOctets += m->m_pkthdr.len;
301 hinfo->stats.inFrames++;
303 /* Duplicate packet if requried */
307 /* Copy packet (failure will not stop the original)*/
308 m2 = m_dup(m, M_DONTWAIT);
310 /* Deliver duplicate */
312 NG_SEND_DATA_ONLY(error, h->hook, m2);
314 h->stats.outOctets += m->m_pkthdr.len;
315 h->stats.outFrames++;
319 /* Deliver frame out destination hook */
322 h->stats.outOctets += m->m_pkthdr.len;
323 h->stats.outFrames++;
324 NG_FWD_ITEM_HOOK(error, item, h->hook);
331 * We are going to be shut down soon
333 * If we have both a left and right hook, then we probably want to extricate
334 * ourselves and leave the two peers still linked to each other. Otherwise we
335 * should just shut down as a normal node would.
338 ng_tee_close(node_p node)
340 const sc_p privdata = NG_NODE_PRIVATE(node);
342 if (privdata->left.hook && privdata->right.hook)
343 ng_bypass(privdata->left.hook, privdata->right.hook);
349 * Shutdown processing
352 ng_tee_shutdown(node_p node)
354 const sc_p privdata = NG_NODE_PRIVATE(node);
356 NG_NODE_SET_PRIVATE(node, NULL);
357 free(privdata, M_NETGRAPH);
366 ng_tee_disconnect(hook_p hook)
368 sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
369 hi_p const hinfo = NG_HOOK_PRIVATE(hook);
371 KASSERT(hinfo != NULL, ("%s: null info", __func__));
374 /* Recalculate internal pathes. */
375 if (sc->left.dest == hinfo) {
376 sc->left.dest = sc->left.dup;
378 } else if (sc->left.dup == hinfo)
380 if (sc->right.dest == hinfo) {
381 sc->right.dest = sc->right.dup;
382 sc->right.dup = NULL;
383 } else if (sc->right.dup == hinfo)
384 sc->right.dup = NULL;
385 if (sc->left2right.dest == hinfo)
386 sc->left2right.dest = NULL;
387 if (sc->right2left.dest == hinfo)
388 sc->right2left.dest = NULL;
390 /* Die when last hook disconnected. */
391 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0) &&
392 NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))
393 ng_rmnode_self(NG_HOOK_NODE(hook));