]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netgraph/ng_tee.c
ng_bridge: allow to automatically assign numbers to new hooks
[FreeBSD/FreeBSD.git] / sys / netgraph / ng_tee.c
1
2 /*
3  * ng_tee.c
4  */
5
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.
20  * 
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.
38  *
39  * Author: Julian Elischer <julian@freebsd.org>
40  * $Whistle: ng_tee.c,v 1.18 1999/11/01 09:24:52 julian Exp $
41  */
42
43 /*
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.
50  */
51
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>
57 #include <sys/mbuf.h>
58 #include <netgraph/ng_message.h>
59 #include <netgraph/netgraph.h>
60 #include <netgraph/ng_parse.h>
61 #include <netgraph/ng_tee.h>
62
63 /* Per hook info */
64 struct hookinfo {
65         hook_p                  hook;
66         struct hookinfo         *dest, *dup;
67         struct ng_tee_hookstat  stats;
68 };
69 typedef struct hookinfo *hi_p;
70
71 /* Per node info */
72 struct privdata {
73         struct hookinfo         left;
74         struct hookinfo         right;
75         struct hookinfo         left2right;
76         struct hookinfo         right2left;
77 };
78 typedef struct privdata *sc_p;
79
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;
88
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
95 };
96
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
103 };
104
105 /* List of commands and how to convert arguments to/from ASCII */
106 static const struct ng_cmdlist ng_tee_cmds[] = {
107         {
108           NGM_TEE_COOKIE,
109           NGM_TEE_GET_STATS,
110           "getstats",
111           NULL,
112           &ng_tee_stats_type
113         },
114         {
115           NGM_TEE_COOKIE,
116           NGM_TEE_CLR_STATS,
117           "clrstats",
118           NULL,
119           NULL
120         },
121         {
122           NGM_TEE_COOKIE,
123           NGM_TEE_GETCLR_STATS,
124           "getclrstats",
125           NULL,
126           &ng_tee_stats_type
127         },
128         { 0 }
129 };
130
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,
143 };
144 NETGRAPH_INIT(tee, &ng_tee_typestruct);
145
146 /*
147  * Node constructor
148  */
149 static int
150 ng_tee_constructor(node_p node)
151 {
152         sc_p privdata;
153
154         privdata = malloc(sizeof(*privdata), M_NETGRAPH, M_WAITOK | M_ZERO);
155
156         NG_NODE_SET_PRIVATE(node, privdata);
157         return (0);
158 }
159
160 /*
161  * Add a hook
162  */
163 static int
164 ng_tee_newhook(node_p node, hook_p hook, const char *name)
165 {
166         sc_p    privdata = NG_NODE_PRIVATE(node);
167         hi_p    hinfo;
168
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;
186                 else    
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;
192                 else
193                         privdata->left.dest = hinfo;
194         } else
195                 return (EINVAL);
196         hinfo->hook = hook;
197         bzero(&hinfo->stats, sizeof(hinfo->stats));
198         NG_HOOK_SET_PRIVATE(hook, hinfo);
199         return (0);
200 }
201
202 /*
203  * Receive a control message
204  */
205 static int
206 ng_tee_rcvmsg(node_p node, item_p item, hook_p lasthook)
207 {
208         const sc_p sc = NG_NODE_PRIVATE(node);
209         struct ng_mesg *resp = NULL;
210         int error = 0;
211         struct ng_mesg *msg;
212
213         NGI_GET_MSG(item, msg);
214         switch (msg->header.typecookie) {
215         case NGM_TEE_COOKIE:
216                 switch (msg->header.cmd) {
217                 case NGM_TEE_GET_STATS:
218                 case NGM_TEE_CLR_STATS:
219                 case NGM_TEE_GETCLR_STATS:
220                     {
221                         struct ng_tee_stats *stats;
222
223                         if (msg->header.cmd != NGM_TEE_CLR_STATS) {
224                                 NG_MKRESPONSE(resp, msg,
225                                     sizeof(*stats), M_NOWAIT);
226                                 if (resp == NULL) {
227                                         error = ENOMEM;
228                                         goto done;
229                                 }
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));
239                         }
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));
249                         }
250                         break;
251                     }
252                 default:
253                         error = EINVAL;
254                         break;
255                 }
256                 break;
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) {
261                                 NGI_MSG(item) = msg;
262                                 NG_FWD_ITEM_HOOK(error, item, hinfo->dest->hook);
263                                 return (error);
264                         }
265                 }
266                 break;
267         default:
268                 error = EINVAL;
269                 break;
270         }
271 done:
272         NG_RESPOND_MSG(error, node, item, resp);
273         NG_FREE_MSG(msg);
274         return (error);
275 }
276
277 /*
278  * Receive data on a hook
279  *
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.
286  */
287 static int
288 ng_tee_rcvdata(hook_p hook, item_p item)
289 {
290         const hi_p hinfo = NG_HOOK_PRIVATE(hook);
291         hi_p    h;
292         int     error = 0;
293         struct mbuf *m;
294
295         m = NGI_M(item);
296
297         /* Update stats on incoming hook */
298         hinfo->stats.inOctets += m->m_pkthdr.len;
299         hinfo->stats.inFrames++;
300
301         /* Duplicate packet if requried */
302         if (hinfo->dup) {
303                 struct mbuf *m2;
304
305                 /* Copy packet (failure will not stop the original)*/
306                 m2 = m_dup(m, M_NOWAIT);
307                 if (m2) {
308                         /* Deliver duplicate */
309                         h = hinfo->dup;
310                         NG_SEND_DATA_ONLY(error, h->hook, m2);
311                         if (error == 0) {
312                                 h->stats.outOctets += m->m_pkthdr.len;
313                                 h->stats.outFrames++;
314                         }
315                 }
316         }
317         /* Deliver frame out destination hook */
318         if (hinfo->dest) {
319                 h = hinfo->dest;
320                 h->stats.outOctets += m->m_pkthdr.len;
321                 h->stats.outFrames++;
322                 NG_FWD_ITEM_HOOK(error, item, h->hook);
323         } else
324                 NG_FREE_ITEM(item);
325         return (error);
326 }
327
328 /*
329  * We are going to be shut down soon
330  *
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.
334  */
335 static int
336 ng_tee_close(node_p node)
337 {
338         const sc_p privdata = NG_NODE_PRIVATE(node);
339
340         if (privdata->left.hook && privdata->right.hook)
341                 ng_bypass(privdata->left.hook, privdata->right.hook);
342
343         return (0);
344 }
345
346 /*
347  * Shutdown processing
348  */
349 static int
350 ng_tee_shutdown(node_p node)
351 {
352         const sc_p privdata = NG_NODE_PRIVATE(node);
353
354         NG_NODE_SET_PRIVATE(node, NULL);
355         free(privdata, M_NETGRAPH);
356         NG_NODE_UNREF(node);
357         return (0);
358 }
359
360 /*
361  * Hook disconnection
362  */
363 static int
364 ng_tee_disconnect(hook_p hook)
365 {
366         sc_p    sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
367         hi_p const hinfo = NG_HOOK_PRIVATE(hook);
368
369         KASSERT(hinfo != NULL, ("%s: null info", __func__));
370         hinfo->hook = NULL;
371
372         /* Recalculate internal paths. */
373         if (sc->left.dest == hinfo) {
374                 sc->left.dest = sc->left.dup;
375                 sc->left.dup = NULL;
376         } else if (sc->left.dup == hinfo)
377                 sc->left.dup = NULL;
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;
387
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));
392         return (0);
393 }