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 * $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>
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_rmnode
;
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_field ng_tee_hookstat_type_fields
[]
89 = 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_fields
95 /* Parse type for struct ng_tee_stats */
96 static const struct ng_parse_struct_field ng_tee_stats_type_fields
[]
97 = 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_fields
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
= {
145 NETGRAPH_INIT(tee
, &ng_tee_typestruct
);
151 ngt_constructor(node_p
*nodep
)
156 privdata
= kmalloc(sizeof(*privdata
), M_NETGRAPH
, M_NOWAIT
| M_ZERO
);
157 if (privdata
== NULL
)
160 if ((error
= ng_make_node_common(&ng_tee_typestruct
, nodep
))) {
161 kfree(privdata
, M_NETGRAPH
);
164 (*nodep
)->private = privdata
;
165 privdata
->node
= *nodep
;
173 ngt_newhook(node_p node
, hook_p hook
, const char *name
)
175 const sc_p sc
= node
->private;
177 if (strcmp(name
, NG_TEE_HOOK_RIGHT
) == 0) {
178 sc
->right
.hook
= hook
;
179 bzero(&sc
->right
.stats
, sizeof(sc
->right
.stats
));
180 hook
->private = &sc
->right
;
181 } else if (strcmp(name
, NG_TEE_HOOK_LEFT
) == 0) {
182 sc
->left
.hook
= hook
;
183 bzero(&sc
->left
.stats
, sizeof(sc
->left
.stats
));
184 hook
->private = &sc
->left
;
185 } else if (strcmp(name
, NG_TEE_HOOK_RIGHT2LEFT
) == 0) {
186 sc
->right2left
.hook
= hook
;
187 bzero(&sc
->right2left
.stats
, sizeof(sc
->right2left
.stats
));
188 hook
->private = &sc
->right2left
;
189 } else if (strcmp(name
, NG_TEE_HOOK_LEFT2RIGHT
) == 0) {
190 sc
->left2right
.hook
= hook
;
191 bzero(&sc
->left2right
.stats
, sizeof(sc
->left2right
.stats
));
192 hook
->private = &sc
->left2right
;
199 * Receive a control message
202 ngt_rcvmsg(node_p node
, struct ng_mesg
*msg
, const char *retaddr
,
203 struct ng_mesg
**rptr
)
205 const sc_p sc
= node
->private;
206 struct ng_mesg
*resp
= NULL
;
209 switch (msg
->header
.typecookie
) {
211 switch (msg
->header
.cmd
) {
212 case NGM_TEE_GET_STATS
:
213 case NGM_TEE_CLR_STATS
:
214 case NGM_TEE_GETCLR_STATS
:
216 struct ng_tee_stats
*stats
;
218 if (msg
->header
.cmd
!= NGM_TEE_CLR_STATS
) {
219 NG_MKRESPONSE(resp
, msg
,
220 sizeof(*stats
), M_NOWAIT
);
225 stats
= (struct ng_tee_stats
*)resp
->data
;
226 bcopy(&sc
->right
.stats
, &stats
->right
,
227 sizeof(stats
->right
));
228 bcopy(&sc
->left
.stats
, &stats
->left
,
229 sizeof(stats
->left
));
230 bcopy(&sc
->right2left
.stats
, &stats
->right2left
,
231 sizeof(stats
->right2left
));
232 bcopy(&sc
->left2right
.stats
, &stats
->left2right
,
233 sizeof(stats
->left2right
));
235 if (msg
->header
.cmd
!= NGM_TEE_GET_STATS
) {
236 bzero(&sc
->right
.stats
,
237 sizeof(sc
->right
.stats
));
238 bzero(&sc
->left
.stats
,
239 sizeof(sc
->left
.stats
));
240 bzero(&sc
->right2left
.stats
,
241 sizeof(sc
->right2left
.stats
));
242 bzero(&sc
->left2right
.stats
,
243 sizeof(sc
->left2right
.stats
));
259 kfree(resp
, M_NETGRAPH
);
262 kfree(msg
, M_NETGRAPH
);
267 * Receive data on a hook
269 * If data comes in the right link send a copy out right2left, and then
270 * send the original onwards out through the left link.
271 * Do the opposite for data coming in from the left link.
272 * Data coming in right2left or left2right is forwarded
273 * on through the appropriate destination hook as if it had come
274 * from the other side.
277 ngt_rcvdata(hook_p hook
, struct mbuf
*m
, meta_p meta
)
279 const sc_p sc
= hook
->node
->private;
280 struct hookinfo
*const hinfo
= (struct hookinfo
*) hook
->private;
281 struct hookinfo
*dest
;
282 struct hookinfo
*dup
;
286 if (hinfo
== &sc
->left
) {
287 dup
= &sc
->left2right
;
289 } else if (hinfo
== &sc
->right
) {
290 dup
= &sc
->right2left
;
292 } else if (hinfo
== &sc
->right2left
) {
295 } else if (hinfo
== &sc
->left2right
) {
299 panic("%s: no hook!", __func__
);
301 /* Update stats on incoming hook */
302 hinfo
->stats
.inOctets
+= m
->m_pkthdr
.len
;
303 hinfo
->stats
.inFrames
++;
305 /* Duplicate packet and meta info if requried */
311 m2
= m_dup(m
, M_NOWAIT
);
313 NG_FREE_DATA(m
, meta
);
319 meta2
= kmalloc(meta
->used_len
, M_NETGRAPH
, M_NOWAIT
);
322 NG_FREE_DATA(m
, meta
);
325 bcopy(meta
, meta2
, meta
->used_len
);
326 meta2
->allocated_len
= meta
->used_len
;
330 /* Deliver duplicate */
331 dup
->stats
.outOctets
+= m
->m_pkthdr
.len
;
332 dup
->stats
.outFrames
++;
333 NG_SEND_DATA(error
, dup
->hook
, m2
, meta2
);
336 /* Deliver frame out destination hook */
337 dest
->stats
.outOctets
+= m
->m_pkthdr
.len
;
338 dest
->stats
.outFrames
++;
339 NG_SEND_DATA(error
, dest
->hook
, m
, meta
);
344 * Shutdown processing
346 * This is tricky. If we have both a left and right hook, then we
347 * probably want to extricate ourselves and leave the two peers
348 * still linked to each other. Otherwise we should just shut down as
349 * a normal node would.
351 * To keep the scope of info correct the routine to "extract" a node
352 * from two links is in ng_base.c.
355 ngt_rmnode(node_p node
)
357 const sc_p privdata
= node
->private;
359 node
->flags
|= NG_INVALID
;
360 if (privdata
->left
.hook
&& privdata
->right
.hook
)
361 ng_bypass(privdata
->left
.hook
, privdata
->right
.hook
);
364 node
->private = NULL
;
365 ng_unref(privdata
->node
);
366 kfree(privdata
, M_NETGRAPH
);
374 ngt_disconnect(hook_p hook
)
376 struct hookinfo
*const hinfo
= (struct hookinfo
*) hook
->private;
378 KASSERT(hinfo
!= NULL
, ("%s: null info", __func__
));
380 if (hook
->node
->numhooks
== 0)
381 ng_rmnode(hook
->node
);