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>
39 * $FreeBSD: src/sys/netgraph/ng_tee.c,v 1.7.2.5 2002/07/02 23:44:03 archie Exp $
40 * $DragonFly: src/sys/netgraph/tee/ng_tee.c,v 1.6 2008/01/05 14:02:40 swildner Exp $
41 * $Whistle: ng_tee.c,v 1.18 1999/11/01 09:24:52 julian Exp $
45 * This node is like the tee(1) command and is useful for ``snooping.''
46 * It has 4 hooks: left, right, left2right, and right2left. Data
47 * entering from the right is passed to the left and duplicated on
48 * right2left, and data entering from the left is passed to the right
49 * and duplicated on left2right. Data entering from left2right is
50 * sent to left, and data from right2left to right.
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/errno.h>
56 #include <sys/kernel.h>
57 #include <sys/malloc.h>
59 #include <netgraph/ng_message.h>
60 #include <netgraph/netgraph.h>
61 #include <netgraph/ng_parse.h>
67 struct ng_tee_hookstat stats
;
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 ngt_constructor
;
82 static ng_rcvmsg_t ngt_rcvmsg
;
83 static ng_shutdown_t ngt_rmnode
;
84 static ng_newhook_t ngt_newhook
;
85 static ng_rcvdata_t ngt_rcvdata
;
86 static ng_disconnect_t ngt_disconnect
;
88 /* Parse type for struct ng_tee_hookstat */
89 static const struct ng_parse_struct_field ng_tee_hookstat_type_fields
[]
90 = NG_TEE_HOOKSTAT_INFO
;
91 static const struct ng_parse_type ng_tee_hookstat_type
= {
92 &ng_parse_struct_type
,
93 &ng_tee_hookstat_type_fields
96 /* Parse type for struct ng_tee_stats */
97 static const struct ng_parse_struct_field ng_tee_stats_type_fields
[]
98 = NG_TEE_STATS_INFO(&ng_tee_hookstat_type
);
99 static const struct ng_parse_type ng_tee_stats_type
= {
100 &ng_parse_struct_type
,
101 &ng_tee_stats_type_fields
104 /* List of commands and how to convert arguments to/from ASCII */
105 static const struct ng_cmdlist ng_tee_cmds
[] = {
122 NGM_TEE_GETCLR_STATS
,
130 /* Netgraph type descriptor */
131 static struct ng_type ng_tee_typestruct
= {
146 NETGRAPH_INIT(tee
, &ng_tee_typestruct
);
152 ngt_constructor(node_p
*nodep
)
157 MALLOC(privdata
, sc_p
, sizeof(*privdata
), M_NETGRAPH
, M_NOWAIT
| M_ZERO
);
158 if (privdata
== NULL
)
161 if ((error
= ng_make_node_common(&ng_tee_typestruct
, nodep
))) {
162 FREE(privdata
, M_NETGRAPH
);
165 (*nodep
)->private = privdata
;
166 privdata
->node
= *nodep
;
174 ngt_newhook(node_p node
, hook_p hook
, const char *name
)
176 const sc_p sc
= node
->private;
178 if (strcmp(name
, NG_TEE_HOOK_RIGHT
) == 0) {
179 sc
->right
.hook
= hook
;
180 bzero(&sc
->right
.stats
, sizeof(sc
->right
.stats
));
181 hook
->private = &sc
->right
;
182 } else if (strcmp(name
, NG_TEE_HOOK_LEFT
) == 0) {
183 sc
->left
.hook
= hook
;
184 bzero(&sc
->left
.stats
, sizeof(sc
->left
.stats
));
185 hook
->private = &sc
->left
;
186 } else if (strcmp(name
, NG_TEE_HOOK_RIGHT2LEFT
) == 0) {
187 sc
->right2left
.hook
= hook
;
188 bzero(&sc
->right2left
.stats
, sizeof(sc
->right2left
.stats
));
189 hook
->private = &sc
->right2left
;
190 } else if (strcmp(name
, NG_TEE_HOOK_LEFT2RIGHT
) == 0) {
191 sc
->left2right
.hook
= hook
;
192 bzero(&sc
->left2right
.stats
, sizeof(sc
->left2right
.stats
));
193 hook
->private = &sc
->left2right
;
200 * Receive a control message
203 ngt_rcvmsg(node_p node
, struct ng_mesg
*msg
, const char *retaddr
,
204 struct ng_mesg
**rptr
)
206 const sc_p sc
= node
->private;
207 struct ng_mesg
*resp
= NULL
;
210 switch (msg
->header
.typecookie
) {
212 switch (msg
->header
.cmd
) {
213 case NGM_TEE_GET_STATS
:
214 case NGM_TEE_CLR_STATS
:
215 case NGM_TEE_GETCLR_STATS
:
217 struct ng_tee_stats
*stats
;
219 if (msg
->header
.cmd
!= NGM_TEE_CLR_STATS
) {
220 NG_MKRESPONSE(resp
, msg
,
221 sizeof(*stats
), M_NOWAIT
);
226 stats
= (struct ng_tee_stats
*)resp
->data
;
227 bcopy(&sc
->right
.stats
, &stats
->right
,
228 sizeof(stats
->right
));
229 bcopy(&sc
->left
.stats
, &stats
->left
,
230 sizeof(stats
->left
));
231 bcopy(&sc
->right2left
.stats
, &stats
->right2left
,
232 sizeof(stats
->right2left
));
233 bcopy(&sc
->left2right
.stats
, &stats
->left2right
,
234 sizeof(stats
->left2right
));
236 if (msg
->header
.cmd
!= NGM_TEE_GET_STATS
) {
237 bzero(&sc
->right
.stats
,
238 sizeof(sc
->right
.stats
));
239 bzero(&sc
->left
.stats
,
240 sizeof(sc
->left
.stats
));
241 bzero(&sc
->right2left
.stats
,
242 sizeof(sc
->right2left
.stats
));
243 bzero(&sc
->left2right
.stats
,
244 sizeof(sc
->left2right
.stats
));
260 FREE(resp
, M_NETGRAPH
);
263 FREE(msg
, M_NETGRAPH
);
268 * Receive data on a hook
270 * If data comes in the right link send a copy out right2left, and then
271 * send the original onwards out through the left link.
272 * Do the opposite for data coming in from the left link.
273 * Data coming in right2left or left2right is forwarded
274 * on through the appropriate destination hook as if it had come
275 * from the other side.
278 ngt_rcvdata(hook_p hook
, struct mbuf
*m
, meta_p meta
)
280 const sc_p sc
= hook
->node
->private;
281 struct hookinfo
*const hinfo
= (struct hookinfo
*) hook
->private;
282 struct hookinfo
*dest
;
283 struct hookinfo
*dup
;
287 if (hinfo
== &sc
->left
) {
288 dup
= &sc
->left2right
;
290 } else if (hinfo
== &sc
->right
) {
291 dup
= &sc
->right2left
;
293 } else if (hinfo
== &sc
->right2left
) {
296 } else if (hinfo
== &sc
->left2right
) {
300 panic("%s: no hook!", __func__
);
302 /* Update stats on incoming hook */
303 hinfo
->stats
.inOctets
+= m
->m_pkthdr
.len
;
304 hinfo
->stats
.inFrames
++;
306 /* Duplicate packet and meta info if requried */
312 m2
= m_dup(m
, M_NOWAIT
);
314 NG_FREE_DATA(m
, meta
);
320 MALLOC(meta2
, meta_p
,
321 meta
->used_len
, M_NETGRAPH
, M_NOWAIT
);
324 NG_FREE_DATA(m
, meta
);
327 bcopy(meta
, meta2
, meta
->used_len
);
328 meta2
->allocated_len
= meta
->used_len
;
332 /* Deliver duplicate */
333 dup
->stats
.outOctets
+= m
->m_pkthdr
.len
;
334 dup
->stats
.outFrames
++;
335 NG_SEND_DATA(error
, dup
->hook
, m2
, meta2
);
338 /* Deliver frame out destination hook */
339 dest
->stats
.outOctets
+= m
->m_pkthdr
.len
;
340 dest
->stats
.outFrames
++;
341 NG_SEND_DATA(error
, dest
->hook
, m
, meta
);
346 * Shutdown processing
348 * This is tricky. If we have both a left and right hook, then we
349 * probably want to extricate ourselves and leave the two peers
350 * still linked to each other. Otherwise we should just shut down as
351 * a normal node would.
353 * To keep the scope of info correct the routine to "extract" a node
354 * from two links is in ng_base.c.
357 ngt_rmnode(node_p node
)
359 const sc_p privdata
= node
->private;
361 node
->flags
|= NG_INVALID
;
362 if (privdata
->left
.hook
&& privdata
->right
.hook
)
363 ng_bypass(privdata
->left
.hook
, privdata
->right
.hook
);
366 node
->private = NULL
;
367 ng_unref(privdata
->node
);
368 FREE(privdata
, M_NETGRAPH
);
376 ngt_disconnect(hook_p hook
)
378 struct hookinfo
*const hinfo
= (struct hookinfo
*) hook
->private;
380 KASSERT(hinfo
!= NULL
, ("%s: null info", __func__
));
382 if (hook
->node
->numhooks
== 0)
383 ng_rmnode(hook
->node
);