Add missing section number.
[dragonfly.git] / sys / netgraph7 / ng_tee.c
blobd2995d23711fec31e456b8ad9a2d04ee83488ecd
2 /*
3 * ng_tee.c
4 */
6 /*-
7 * Copyright (c) 1996-1999 Whistle Communications, Inc.
8 * All rights reserved.
9 *
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
37 * OF SUCH DAMAGE.
39 * Author: Julian Elischer <julian@freebsd.org>
41 * $FreeBSD: src/sys/netgraph/ng_tee.c,v 1.35 2008/02/24 10:13:32 mav Exp $
42 * $DragonFly: src/sys/netgraph7/ng_tee.c,v 1.2 2008/06/26 23:05:35 dillon Exp $
43 * $Whistle: ng_tee.c,v 1.18 1999/11/01 09:24:52 julian Exp $
47 * This node is like the tee(1) command and is useful for ``snooping.''
48 * It has 4 hooks: left, right, left2right, and right2left. Data
49 * entering from the right is passed to the left and duplicated on
50 * right2left, and data entering from the left is passed to the right
51 * and duplicated on left2right. Data entering from left2right is
52 * sent to left, and data from right2left to right.
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/errno.h>
58 #include <sys/kernel.h>
59 #include <sys/malloc.h>
60 #include <sys/mbuf.h>
61 #include "ng_message.h"
62 #include "netgraph.h"
63 #include "ng_parse.h"
64 #include "ng_tee.h"
66 /* Per hook info */
67 struct hookinfo {
68 hook_p hook;
69 struct hookinfo *dest, *dup;
70 struct ng_tee_hookstat stats;
72 typedef struct hookinfo *hi_p;
74 /* Per node info */
75 struct privdata {
76 struct hookinfo left;
77 struct hookinfo right;
78 struct hookinfo left2right;
79 struct hookinfo right2left;
81 typedef struct privdata *sc_p;
83 /* Netgraph methods */
84 static ng_constructor_t ng_tee_constructor;
85 static ng_rcvmsg_t ng_tee_rcvmsg;
86 static ng_close_t ng_tee_close;
87 static ng_shutdown_t ng_tee_shutdown;
88 static ng_newhook_t ng_tee_newhook;
89 static ng_rcvdata_t ng_tee_rcvdata;
90 static ng_disconnect_t ng_tee_disconnect;
92 /* Parse type for struct ng_tee_hookstat */
93 static const struct ng_parse_struct_field ng_tee_hookstat_type_fields[]
94 = NG_TEE_HOOKSTAT_INFO;
95 static const struct ng_parse_type ng_tee_hookstat_type = {
96 &ng_parse_struct_type,
97 &ng_tee_hookstat_type_fields
100 /* Parse type for struct ng_tee_stats */
101 static const struct ng_parse_struct_field ng_tee_stats_type_fields[]
102 = NG_TEE_STATS_INFO(&ng_tee_hookstat_type);
103 static const struct ng_parse_type ng_tee_stats_type = {
104 &ng_parse_struct_type,
105 &ng_tee_stats_type_fields
108 /* List of commands and how to convert arguments to/from ASCII */
109 static const struct ng_cmdlist ng_tee_cmds[] = {
111 NGM_TEE_COOKIE,
112 NGM_TEE_GET_STATS,
113 "getstats",
114 NULL,
115 &ng_tee_stats_type
118 NGM_TEE_COOKIE,
119 NGM_TEE_CLR_STATS,
120 "clrstats",
121 NULL,
122 NULL
125 NGM_TEE_COOKIE,
126 NGM_TEE_GETCLR_STATS,
127 "getclrstats",
128 NULL,
129 &ng_tee_stats_type
131 { 0 }
134 /* Netgraph type descriptor */
135 static struct ng_type ng_tee_typestruct = {
136 .version = NG_ABI_VERSION,
137 .name = NG_TEE_NODE_TYPE,
138 .constructor = ng_tee_constructor,
139 .rcvmsg = ng_tee_rcvmsg,
140 .close = ng_tee_close,
141 .shutdown = ng_tee_shutdown,
142 .newhook = ng_tee_newhook,
143 .rcvdata = ng_tee_rcvdata,
144 .disconnect = ng_tee_disconnect,
145 .cmdlist = ng_tee_cmds,
147 NETGRAPH_INIT(tee, &ng_tee_typestruct);
150 * Node constructor
152 static int
153 ng_tee_constructor(node_p node)
155 sc_p privdata;
157 MALLOC(privdata, sc_p, sizeof(*privdata), M_NETGRAPH, M_WAITOK | M_NULLOK | M_ZERO);
158 if (privdata == NULL)
159 return (ENOMEM);
161 NG_NODE_SET_PRIVATE(node, privdata);
162 return (0);
166 * Add a hook
168 static int
169 ng_tee_newhook(node_p node, hook_p hook, const char *name)
171 sc_p privdata = NG_NODE_PRIVATE(node);
172 hi_p hinfo;
174 /* Precalculate internal pathes. */
175 if (strcmp(name, NG_TEE_HOOK_RIGHT) == 0) {
176 hinfo = &privdata->right;
177 if (privdata->left.dest)
178 privdata->left.dup = privdata->left.dest;
179 privdata->left.dest = hinfo;
180 privdata->right2left.dest = hinfo;
181 } else if (strcmp(name, NG_TEE_HOOK_LEFT) == 0) {
182 hinfo = &privdata->left;
183 if (privdata->right.dest)
184 privdata->right.dup = privdata->right.dest;
185 privdata->right.dest = hinfo;
186 privdata->left2right.dest = hinfo;
187 } else if (strcmp(name, NG_TEE_HOOK_RIGHT2LEFT) == 0) {
188 hinfo = &privdata->right2left;
189 if (privdata->right.dest)
190 privdata->right.dup = hinfo;
191 else
192 privdata->right.dest = hinfo;
193 } else if (strcmp(name, NG_TEE_HOOK_LEFT2RIGHT) == 0) {
194 hinfo = &privdata->left2right;
195 if (privdata->left.dest)
196 privdata->left.dup = hinfo;
197 else
198 privdata->left.dest = hinfo;
199 } else
200 return (EINVAL);
201 hinfo->hook = hook;
202 bzero(&hinfo->stats, sizeof(hinfo->stats));
203 NG_HOOK_SET_PRIVATE(hook, hinfo);
204 return (0);
208 * Receive a control message
210 static int
211 ng_tee_rcvmsg(node_p node, item_p item, hook_p lasthook)
213 const sc_p sc = NG_NODE_PRIVATE(node);
214 struct ng_mesg *resp = NULL;
215 int error = 0;
216 struct ng_mesg *msg;
218 NGI_GET_MSG(item, msg);
219 switch (msg->header.typecookie) {
220 case NGM_TEE_COOKIE:
221 switch (msg->header.cmd) {
222 case NGM_TEE_GET_STATS:
223 case NGM_TEE_CLR_STATS:
224 case NGM_TEE_GETCLR_STATS:
226 struct ng_tee_stats *stats;
228 if (msg->header.cmd != NGM_TEE_CLR_STATS) {
229 NG_MKRESPONSE(resp, msg,
230 sizeof(*stats), M_WAITOK | M_NULLOK);
231 if (resp == NULL) {
232 error = ENOMEM;
233 goto done;
235 stats = (struct ng_tee_stats *)resp->data;
236 bcopy(&sc->right.stats, &stats->right,
237 sizeof(stats->right));
238 bcopy(&sc->left.stats, &stats->left,
239 sizeof(stats->left));
240 bcopy(&sc->right2left.stats, &stats->right2left,
241 sizeof(stats->right2left));
242 bcopy(&sc->left2right.stats, &stats->left2right,
243 sizeof(stats->left2right));
245 if (msg->header.cmd != NGM_TEE_GET_STATS) {
246 bzero(&sc->right.stats,
247 sizeof(sc->right.stats));
248 bzero(&sc->left.stats,
249 sizeof(sc->left.stats));
250 bzero(&sc->right2left.stats,
251 sizeof(sc->right2left.stats));
252 bzero(&sc->left2right.stats,
253 sizeof(sc->left2right.stats));
255 break;
257 default:
258 error = EINVAL;
259 break;
261 break;
262 case NGM_FLOW_COOKIE:
263 if (lasthook == sc->left.hook || lasthook == sc->right.hook) {
264 hi_p const hinfo = NG_HOOK_PRIVATE(lasthook);
265 if (hinfo && hinfo->dest) {
266 NGI_MSG(item) = msg;
267 NG_FWD_ITEM_HOOK(error, item, hinfo->dest->hook);
268 return (error);
271 break;
272 default:
273 error = EINVAL;
274 break;
276 done:
277 NG_RESPOND_MSG(error, node, item, resp);
278 NG_FREE_MSG(msg);
279 return (error);
283 * Receive data on a hook
285 * If data comes in the right link send a copy out right2left, and then
286 * send the original onwards out through the left link.
287 * Do the opposite for data coming in from the left link.
288 * Data coming in right2left or left2right is forwarded
289 * on through the appropriate destination hook as if it had come
290 * from the other side.
292 static int
293 ng_tee_rcvdata(hook_p hook, item_p item)
295 const hi_p hinfo = NG_HOOK_PRIVATE(hook);
296 hi_p h;
297 int error = 0;
298 struct mbuf *m;
300 m = NGI_M(item);
302 /* Update stats on incoming hook */
303 hinfo->stats.inOctets += m->m_pkthdr.len;
304 hinfo->stats.inFrames++;
306 /* Duplicate packet if requried */
307 if (hinfo->dup) {
308 struct mbuf *m2;
310 /* Copy packet (failure will not stop the original)*/
311 m2 = m_dup(m, MB_DONTWAIT);
312 if (m2) {
313 /* Deliver duplicate */
314 h = hinfo->dup;
315 NG_SEND_DATA_ONLY(error, h->hook, m2);
316 if (error == 0) {
317 h->stats.outOctets += m->m_pkthdr.len;
318 h->stats.outFrames++;
322 /* Deliver frame out destination hook */
323 if (hinfo->dest) {
324 h = hinfo->dest;
325 h->stats.outOctets += m->m_pkthdr.len;
326 h->stats.outFrames++;
327 NG_FWD_ITEM_HOOK(error, item, h->hook);
328 } else
329 NG_FREE_ITEM(item);
330 return (error);
334 * We are going to be shut down soon
336 * If we have both a left and right hook, then we probably want to extricate
337 * ourselves and leave the two peers still linked to each other. Otherwise we
338 * should just shut down as a normal node would.
340 static int
341 ng_tee_close(node_p node)
343 const sc_p privdata = NG_NODE_PRIVATE(node);
345 if (privdata->left.hook && privdata->right.hook)
346 ng_bypass(privdata->left.hook, privdata->right.hook);
348 return (0);
352 * Shutdown processing
354 static int
355 ng_tee_shutdown(node_p node)
357 const sc_p privdata = NG_NODE_PRIVATE(node);
359 NG_NODE_SET_PRIVATE(node, NULL);
360 FREE(privdata, M_NETGRAPH);
361 NG_NODE_UNREF(node);
362 return (0);
366 * Hook disconnection
368 static int
369 ng_tee_disconnect(hook_p hook)
371 sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
372 hi_p const hinfo = NG_HOOK_PRIVATE(hook);
374 KASSERT(hinfo != NULL, ("%s: null info", __func__));
375 hinfo->hook = NULL;
377 /* Recalculate internal pathes. */
378 if (sc->left.dest == hinfo) {
379 sc->left.dest = sc->left.dup;
380 sc->left.dup = NULL;
381 } else if (sc->left.dup == hinfo)
382 sc->left.dup = NULL;
383 if (sc->right.dest == hinfo) {
384 sc->right.dest = sc->right.dup;
385 sc->right.dup = NULL;
386 } else if (sc->right.dup == hinfo)
387 sc->right.dup = NULL;
388 if (sc->left2right.dest == hinfo)
389 sc->left2right.dest = NULL;
390 if (sc->right2left.dest == hinfo)
391 sc->right2left.dest = NULL;
393 /* Die when last hook disconnected. */
394 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0) &&
395 NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))
396 ng_rmnode_self(NG_HOOK_NODE(hook));
397 return (0);