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 ng_tee_hookstat stats;
75 struct hookinfo right;
76 struct hookinfo left2right;
77 struct hookinfo right2left;
79 typedef struct privdata *sc_p;
81 /* Netgraph methods */
82 static ng_constructor_t ngt_constructor;
83 static ng_rcvmsg_t ngt_rcvmsg;
84 static ng_close_t ngt_close;
85 static ng_shutdown_t ngt_shutdown;
86 static ng_newhook_t ngt_newhook;
87 static ng_rcvdata_t ngt_rcvdata;
88 static ng_disconnect_t ngt_disconnect;
90 /* Parse type for struct ng_tee_hookstat */
91 static const struct ng_parse_struct_field ng_tee_hookstat_type_fields[]
92 = NG_TEE_HOOKSTAT_INFO;
93 static const struct ng_parse_type ng_tee_hookstat_type = {
94 &ng_parse_struct_type,
95 &ng_tee_hookstat_type_fields
98 /* Parse type for struct ng_tee_stats */
99 static const struct ng_parse_struct_field ng_tee_stats_type_fields[]
100 = NG_TEE_STATS_INFO(&ng_tee_hookstat_type);
101 static const struct ng_parse_type ng_tee_stats_type = {
102 &ng_parse_struct_type,
103 &ng_tee_stats_type_fields
106 /* List of commands and how to convert arguments to/from ASCII */
107 static const struct ng_cmdlist ng_tee_cmds[] = {
124 NGM_TEE_GETCLR_STATS,
132 /* Netgraph type descriptor */
133 static struct ng_type ng_tee_typestruct = {
134 .version = NG_ABI_VERSION,
135 .name = NG_TEE_NODE_TYPE,
136 .constructor = ngt_constructor,
137 .rcvmsg = ngt_rcvmsg,
139 .shutdown = ngt_shutdown,
140 .newhook = ngt_newhook,
141 .rcvdata = ngt_rcvdata,
142 .disconnect = ngt_disconnect,
143 .cmdlist = ng_tee_cmds,
145 NETGRAPH_INIT(tee, &ng_tee_typestruct);
151 ngt_constructor(node_p node)
155 MALLOC(privdata, sc_p, sizeof(*privdata), M_NETGRAPH, M_NOWAIT|M_ZERO);
156 if (privdata == NULL)
159 NG_NODE_SET_PRIVATE(node, privdata);
160 privdata->node = node;
168 ngt_newhook(node_p node, hook_p hook, const char *name)
170 const sc_p sc = NG_NODE_PRIVATE(node);
172 if (strcmp(name, NG_TEE_HOOK_RIGHT) == 0) {
173 sc->right.hook = hook;
174 bzero(&sc->right.stats, sizeof(sc->right.stats));
175 NG_HOOK_SET_PRIVATE(hook, &sc->right);
176 } else if (strcmp(name, NG_TEE_HOOK_LEFT) == 0) {
177 sc->left.hook = hook;
178 bzero(&sc->left.stats, sizeof(sc->left.stats));
179 NG_HOOK_SET_PRIVATE(hook, &sc->left);
180 } else if (strcmp(name, NG_TEE_HOOK_RIGHT2LEFT) == 0) {
181 sc->right2left.hook = hook;
182 bzero(&sc->right2left.stats, sizeof(sc->right2left.stats));
183 NG_HOOK_SET_PRIVATE(hook, &sc->right2left);
184 } else if (strcmp(name, NG_TEE_HOOK_LEFT2RIGHT) == 0) {
185 sc->left2right.hook = hook;
186 bzero(&sc->left2right.stats, sizeof(sc->left2right.stats));
187 NG_HOOK_SET_PRIVATE(hook, &sc->left2right);
194 * Receive a control message
197 ngt_rcvmsg(node_p node, item_p item, hook_p lasthook)
199 const sc_p sc = NG_NODE_PRIVATE(node);
200 struct ng_mesg *resp = NULL;
204 NGI_GET_MSG(item, msg);
205 switch (msg->header.typecookie) {
207 switch (msg->header.cmd) {
208 case NGM_TEE_GET_STATS:
209 case NGM_TEE_CLR_STATS:
210 case NGM_TEE_GETCLR_STATS:
212 struct ng_tee_stats *stats;
214 if (msg->header.cmd != NGM_TEE_CLR_STATS) {
215 NG_MKRESPONSE(resp, msg,
216 sizeof(*stats), M_NOWAIT);
221 stats = (struct ng_tee_stats *)resp->data;
222 bcopy(&sc->right.stats, &stats->right,
223 sizeof(stats->right));
224 bcopy(&sc->left.stats, &stats->left,
225 sizeof(stats->left));
226 bcopy(&sc->right2left.stats, &stats->right2left,
227 sizeof(stats->right2left));
228 bcopy(&sc->left2right.stats, &stats->left2right,
229 sizeof(stats->left2right));
231 if (msg->header.cmd != NGM_TEE_GET_STATS) {
232 bzero(&sc->right.stats,
233 sizeof(sc->right.stats));
234 bzero(&sc->left.stats,
235 sizeof(sc->left.stats));
236 bzero(&sc->right2left.stats,
237 sizeof(sc->right2left.stats));
238 bzero(&sc->left2right.stats,
239 sizeof(sc->left2right.stats));
248 case NGM_FLOW_COOKIE:
250 if (lasthook == sc->left.hook) {
251 if (sc->right.hook) {
253 NG_FWD_ITEM_HOOK(error, item,
260 NG_FWD_ITEM_HOOK(error, item,
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 ngt_rcvdata(hook_p hook, item_p item)
290 const sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
291 struct hookinfo *const hinfo = NG_HOOK_PRIVATE(hook);
292 struct hookinfo *dest;
293 struct hookinfo *dup;
299 if (hinfo == &sc->left) {
300 dup = &sc->left2right;
302 } else if (hinfo == &sc->right) {
303 dup = &sc->right2left;
305 } else if (hinfo == &sc->right2left) {
308 } else if (hinfo == &sc->left2right) {
312 panic("%s: no hook!", __func__);
313 #ifdef RESTARTABLE_PANICS
318 /* Update stats on incoming hook */
319 hinfo->stats.inOctets += m->m_pkthdr.len;
320 hinfo->stats.inFrames++;
323 * Don't make a copy if only the dup hook exists.
325 if ((dup && dup->hook) && (dest->hook == NULL)) {
330 /* Duplicate packet if requried */
331 if (dup && dup->hook) {
334 /* Copy packet (failure will not stop the original)*/
335 m2 = m_dup(m, M_DONTWAIT);
337 /* Deliver duplicate */
338 NG_SEND_DATA_ONLY(error, dup->hook, m2);
340 dup->stats.outOctets += m->m_pkthdr.len;
341 dup->stats.outFrames++;
345 /* Deliver frame out destination hook */
347 dest->stats.outOctets += m->m_pkthdr.len;
348 dest->stats.outFrames++;
349 NG_FWD_ITEM_HOOK(error, item, dest->hook);
356 * We are going to be shut down soon
358 * If we have both a left and right hook, then we probably want to extricate
359 * ourselves and leave the two peers still linked to each other. Otherwise we
360 * should just shut down as a normal node would.
363 ngt_close(node_p node)
365 const sc_p privdata = NG_NODE_PRIVATE(node);
367 if (privdata->left.hook && privdata->right.hook)
368 ng_bypass(privdata->left.hook, privdata->right.hook);
374 * Shutdown processing
377 ngt_shutdown(node_p node)
379 const sc_p privdata = NG_NODE_PRIVATE(node);
381 NG_NODE_SET_PRIVATE(node, NULL);
382 NG_NODE_UNREF(privdata->node);
383 FREE(privdata, M_NETGRAPH);
391 ngt_disconnect(hook_p hook)
393 struct hookinfo *const hinfo = NG_HOOK_PRIVATE(hook);
395 KASSERT(hinfo != NULL, ("%s: null info", __func__));
397 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
398 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
399 ng_rmnode_self(NG_HOOK_NODE(hook));