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>
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 left, and data from right2left to right.
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 hookinfo *dest, *dup;
67 struct ng_tee_hookstat stats;
69 typedef struct hookinfo *hi_p;
74 struct hookinfo right;
75 struct hookinfo left2right;
76 struct hookinfo right2left;
78 typedef struct privdata *sc_p;
80 /* Netgraph methods */
81 static ng_constructor_t ng_tee_constructor;
82 static ng_rcvmsg_t ng_tee_rcvmsg;
83 static ng_close_t ng_tee_close;
84 static ng_shutdown_t ng_tee_shutdown;
85 static ng_newhook_t ng_tee_newhook;
86 static ng_rcvdata_t ng_tee_rcvdata;
87 static ng_disconnect_t ng_tee_disconnect;
89 /* Parse type for struct ng_tee_hookstat */
90 static const struct ng_parse_struct_field ng_tee_hookstat_type_fields[]
91 = NG_TEE_HOOKSTAT_INFO;
92 static const struct ng_parse_type ng_tee_hookstat_type = {
93 &ng_parse_struct_type,
94 &ng_tee_hookstat_type_fields
97 /* Parse type for struct ng_tee_stats */
98 static const struct ng_parse_struct_field ng_tee_stats_type_fields[]
99 = NG_TEE_STATS_INFO(&ng_tee_hookstat_type);
100 static const struct ng_parse_type ng_tee_stats_type = {
101 &ng_parse_struct_type,
102 &ng_tee_stats_type_fields
105 /* List of commands and how to convert arguments to/from ASCII */
106 static const struct ng_cmdlist ng_tee_cmds[] = {
123 NGM_TEE_GETCLR_STATS,
131 /* Netgraph type descriptor */
132 static struct ng_type ng_tee_typestruct = {
133 .version = NG_ABI_VERSION,
134 .name = NG_TEE_NODE_TYPE,
135 .constructor = ng_tee_constructor,
136 .rcvmsg = ng_tee_rcvmsg,
137 .close = ng_tee_close,
138 .shutdown = ng_tee_shutdown,
139 .newhook = ng_tee_newhook,
140 .rcvdata = ng_tee_rcvdata,
141 .disconnect = ng_tee_disconnect,
142 .cmdlist = ng_tee_cmds,
144 NETGRAPH_INIT(tee, &ng_tee_typestruct);
150 ng_tee_constructor(node_p node)
154 privdata = malloc(sizeof(*privdata), M_NETGRAPH, M_WAITOK | M_ZERO);
156 NG_NODE_SET_PRIVATE(node, privdata);
164 ng_tee_newhook(node_p node, hook_p hook, const char *name)
166 sc_p privdata = NG_NODE_PRIVATE(node);
169 /* Precalculate internal paths. */
170 if (strcmp(name, NG_TEE_HOOK_RIGHT) == 0) {
171 hinfo = &privdata->right;
172 if (privdata->left.dest)
173 privdata->left.dup = privdata->left.dest;
174 privdata->left.dest = hinfo;
175 privdata->right2left.dest = hinfo;
176 } else if (strcmp(name, NG_TEE_HOOK_LEFT) == 0) {
177 hinfo = &privdata->left;
178 if (privdata->right.dest)
179 privdata->right.dup = privdata->right.dest;
180 privdata->right.dest = hinfo;
181 privdata->left2right.dest = hinfo;
182 } else if (strcmp(name, NG_TEE_HOOK_RIGHT2LEFT) == 0) {
183 hinfo = &privdata->right2left;
184 if (privdata->right.dest)
185 privdata->right.dup = hinfo;
187 privdata->right.dest = hinfo;
188 } else if (strcmp(name, NG_TEE_HOOK_LEFT2RIGHT) == 0) {
189 hinfo = &privdata->left2right;
190 if (privdata->left.dest)
191 privdata->left.dup = hinfo;
193 privdata->left.dest = hinfo;
197 bzero(&hinfo->stats, sizeof(hinfo->stats));
198 NG_HOOK_SET_PRIVATE(hook, hinfo);
203 * Receive a control message
206 ng_tee_rcvmsg(node_p node, item_p item, hook_p lasthook)
208 const sc_p sc = NG_NODE_PRIVATE(node);
209 struct ng_mesg *resp = NULL;
213 NGI_GET_MSG(item, msg);
214 switch (msg->header.typecookie) {
216 switch (msg->header.cmd) {
217 case NGM_TEE_GET_STATS:
218 case NGM_TEE_CLR_STATS:
219 case NGM_TEE_GETCLR_STATS:
221 struct ng_tee_stats *stats;
223 if (msg->header.cmd != NGM_TEE_CLR_STATS) {
224 NG_MKRESPONSE(resp, msg,
225 sizeof(*stats), M_NOWAIT);
230 stats = (struct ng_tee_stats *)resp->data;
231 bcopy(&sc->right.stats, &stats->right,
232 sizeof(stats->right));
233 bcopy(&sc->left.stats, &stats->left,
234 sizeof(stats->left));
235 bcopy(&sc->right2left.stats, &stats->right2left,
236 sizeof(stats->right2left));
237 bcopy(&sc->left2right.stats, &stats->left2right,
238 sizeof(stats->left2right));
240 if (msg->header.cmd != NGM_TEE_GET_STATS) {
241 bzero(&sc->right.stats,
242 sizeof(sc->right.stats));
243 bzero(&sc->left.stats,
244 sizeof(sc->left.stats));
245 bzero(&sc->right2left.stats,
246 sizeof(sc->right2left.stats));
247 bzero(&sc->left2right.stats,
248 sizeof(sc->left2right.stats));
257 case NGM_FLOW_COOKIE:
258 if (lasthook == sc->left.hook || lasthook == sc->right.hook) {
259 hi_p const hinfo = NG_HOOK_PRIVATE(lasthook);
260 if (hinfo && hinfo->dest) {
262 NG_FWD_ITEM_HOOK(error, item, hinfo->dest->hook);
272 NG_RESPOND_MSG(error, node, item, resp);
278 * Receive data on a hook
280 * If data comes in the right link send a copy out right2left, and then
281 * send the original onwards out through the left link.
282 * Do the opposite for data coming in from the left link.
283 * Data coming in right2left or left2right is forwarded
284 * on through the appropriate destination hook as if it had come
285 * from the other side.
288 ng_tee_rcvdata(hook_p hook, item_p item)
290 const hi_p hinfo = NG_HOOK_PRIVATE(hook);
297 /* Update stats on incoming hook */
298 hinfo->stats.inOctets += m->m_pkthdr.len;
299 hinfo->stats.inFrames++;
301 /* Duplicate packet if requried */
305 /* Copy packet (failure will not stop the original)*/
306 m2 = m_dup(m, M_NOWAIT);
308 /* Deliver duplicate */
310 NG_SEND_DATA_ONLY(error, h->hook, m2);
312 h->stats.outOctets += m->m_pkthdr.len;
313 h->stats.outFrames++;
317 /* Deliver frame out destination hook */
320 h->stats.outOctets += m->m_pkthdr.len;
321 h->stats.outFrames++;
322 NG_FWD_ITEM_HOOK(error, item, h->hook);
329 * We are going to be shut down soon
331 * If we have both a left and right hook, then we probably want to extricate
332 * ourselves and leave the two peers still linked to each other. Otherwise we
333 * should just shut down as a normal node would.
336 ng_tee_close(node_p node)
338 const sc_p privdata = NG_NODE_PRIVATE(node);
340 if (privdata->left.hook && privdata->right.hook)
341 ng_bypass(privdata->left.hook, privdata->right.hook);
347 * Shutdown processing
350 ng_tee_shutdown(node_p node)
352 const sc_p privdata = NG_NODE_PRIVATE(node);
354 NG_NODE_SET_PRIVATE(node, NULL);
355 free(privdata, M_NETGRAPH);
364 ng_tee_disconnect(hook_p hook)
366 sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
367 hi_p const hinfo = NG_HOOK_PRIVATE(hook);
369 KASSERT(hinfo != NULL, ("%s: null info", __func__));
372 /* Recalculate internal paths. */
373 if (sc->left.dest == hinfo) {
374 sc->left.dest = sc->left.dup;
376 } else if (sc->left.dup == hinfo)
378 if (sc->right.dest == hinfo) {
379 sc->right.dest = sc->right.dup;
380 sc->right.dup = NULL;
381 } else if (sc->right.dup == hinfo)
382 sc->right.dup = NULL;
383 if (sc->left2right.dest == hinfo)
384 sc->left2right.dest = NULL;
385 if (sc->right2left.dest == hinfo)
386 sc->right2left.dest = NULL;
388 /* Die when last hook disconnected. */
389 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0) &&
390 NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))
391 ng_rmnode_self(NG_HOOK_NODE(hook));