xref: /freebsd/sys/netgraph/ng_tee.c (revision a14a0223ae1b172e96dd2a1d849e22026a98b692)
1 
2 /*
3  * ng_tee.c
4  *
5  * Copyright (c) 1996-1999 Whistle Communications, Inc.
6  * All rights reserved.
7  *
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.
18  *
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
35  * OF SUCH DAMAGE.
36  *
37  * Author: Julian Elischer <julian@whistle.com>
38  *
39  * $FreeBSD$
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 right, and data from right2left to left.
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_tee.h>
61 
62 /* Per hook info */
63 struct hookinfo {
64 	hook_p			hook;
65 	struct ng_tee_hookstat	stats;
66 };
67 
68 /* Per node info */
69 struct privdata {
70 	node_p			node;
71 	struct hookinfo		left;
72 	struct hookinfo		right;
73 	struct hookinfo		left2right;
74 	struct hookinfo		right2left;
75 };
76 typedef struct privdata *sc_p;
77 
78 /* Netgraph methods */
79 static ng_constructor_t	ngt_constructor;
80 static ng_rcvmsg_t	ngt_rcvmsg;
81 static ng_shutdown_t	ngt_rmnode;
82 static ng_newhook_t	ngt_newhook;
83 static ng_rcvdata_t	ngt_rcvdata;
84 static ng_disconnect_t	ngt_disconnect;
85 
86 /* Netgraph type descriptor */
87 static struct ng_type typestruct = {
88 	NG_VERSION,
89 	NG_TEE_NODE_TYPE,
90 	NULL,
91 	ngt_constructor,
92 	ngt_rcvmsg,
93 	ngt_rmnode,
94 	ngt_newhook,
95 	NULL,
96 	NULL,
97 	ngt_rcvdata,
98 	ngt_rcvdata,
99 	ngt_disconnect
100 };
101 NETGRAPH_INIT(tee, &typestruct);
102 
103 /*
104  * Node constructor
105  */
106 static int
107 ngt_constructor(node_p *nodep)
108 {
109 	sc_p privdata;
110 	int error = 0;
111 
112 	MALLOC(privdata, sc_p, sizeof(*privdata), M_NETGRAPH, M_WAITOK);
113 	if (privdata == NULL)
114 		return (ENOMEM);
115 	bzero(privdata, sizeof(*privdata));
116 
117 	if ((error = ng_make_node_common(&typestruct, nodep))) {
118 		FREE(privdata, M_NETGRAPH);
119 		return (error);
120 	}
121 	(*nodep)->private = privdata;
122 	privdata->node = *nodep;
123 	return (0);
124 }
125 
126 /*
127  * Add a hook
128  */
129 static int
130 ngt_newhook(node_p node, hook_p hook, const char *name)
131 {
132 	const sc_p sc = node->private;
133 
134 	if (strcmp(name, NG_TEE_HOOK_RIGHT) == 0) {
135 		sc->right.hook = hook;
136 		bzero(&sc->right.stats, sizeof(sc->right.stats));
137 		hook->private = &sc->right;
138 	} else if (strcmp(name, NG_TEE_HOOK_LEFT) == 0) {
139 		sc->left.hook = hook;
140 		bzero(&sc->left.stats, sizeof(sc->left.stats));
141 		hook->private = &sc->left;
142 	} else if (strcmp(name, NG_TEE_HOOK_RIGHT2LEFT) == 0) {
143 		sc->right2left.hook = hook;
144 		bzero(&sc->right2left.stats, sizeof(sc->right2left.stats));
145 		hook->private = &sc->right2left;
146 	} else if (strcmp(name, NG_TEE_HOOK_LEFT2RIGHT) == 0) {
147 		sc->left2right.hook = hook;
148 		bzero(&sc->left2right.stats, sizeof(sc->left2right.stats));
149 		hook->private = &sc->left2right;
150 	} else
151 		return (EINVAL);
152 	return (0);
153 }
154 
155 /*
156  * Receive a control message
157  */
158 static int
159 ngt_rcvmsg(node_p node, struct ng_mesg *msg, const char *retaddr,
160 	   struct ng_mesg **rptr)
161 {
162 	const sc_p sc = node->private;
163 	struct ng_mesg *resp = NULL;
164 	int error = 0;
165 
166 	switch (msg->header.typecookie) {
167 	case NGM_TEE_COOKIE:
168 		switch (msg->header.cmd) {
169 		case NGM_TEE_GET_STATS:
170 		    {
171 			struct ng_tee_stats *stats;
172 
173 			NG_MKRESPONSE(resp, msg,
174 			    sizeof(struct ng_tee_stats), M_NOWAIT);
175 			if (resp == NULL) {
176 				error = ENOMEM;
177 				goto done;
178 			}
179 			stats = (struct ng_tee_stats *) resp->data;
180 			bcopy(&sc->right.stats,
181 			    &stats->right, sizeof(stats->right));
182 			bcopy(&sc->left.stats,
183 			    &stats->left, sizeof(stats->left));
184 			bcopy(&sc->right2left.stats,
185 			    &stats->right2left, sizeof(stats->right2left));
186 			bcopy(&sc->left2right.stats,
187 			    &stats->left2right, sizeof(stats->left2right));
188 			break;
189 		    }
190 		case NGM_TEE_CLR_STATS:
191 			bzero(&sc->right.stats, sizeof(sc->right.stats));
192 			bzero(&sc->left.stats, sizeof(sc->left.stats));
193 			bzero(&sc->right2left.stats,
194 			    sizeof(sc->right2left.stats));
195 			bzero(&sc->left2right.stats,
196 			    sizeof(sc->left2right.stats));
197 			break;
198 		default:
199 			error = EINVAL;
200 			break;
201 		}
202 		break;
203 	default:
204 		error = EINVAL;
205 		break;
206 	}
207 	if (rptr)
208 		*rptr = resp;
209 	else if (resp)
210 		FREE(resp, M_NETGRAPH);
211 
212 done:
213 	FREE(msg, M_NETGRAPH);
214 	return (error);
215 }
216 
217 /*
218  * Receive data on a hook
219  *
220  * If data comes in the right link send a copy out right2left, and then
221  * send the original onwards out through the left link.
222  * Do the opposite for data coming in from the left link.
223  * Data coming in right2left or left2right is forwarded
224  * on through the appropriate destination hook as if it had come
225  * from the other side.
226  */
227 static int
228 ngt_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
229 {
230 	const sc_p sc = hook->node->private;
231 	struct hookinfo *const hinfo = (struct hookinfo *) hook->private;
232 	struct hookinfo *dest;
233 	struct hookinfo *dup;
234 	int error = 0;
235 
236 	/* Which hook? */
237 	if (hinfo == &sc->left) {
238 		dup = &sc->left2right;
239 		dest = &sc->right;
240 	} else if (hinfo == &sc->right) {
241 		dup = &sc->right2left;
242 		dest = &sc->left;
243 	} else if (hinfo == &sc->right2left) {
244 		dup = NULL;
245 		dest = &sc->left;
246 	} else if (hinfo == &sc->left2right) {
247 		dup = NULL;
248 		dest = &sc->right;
249 	} else
250 		panic("%s: no hook!", __FUNCTION__);
251 
252 	/* Update stats on incoming hook */
253 	hinfo->stats.inOctets += m->m_pkthdr.len;
254 	hinfo->stats.inFrames++;
255 
256 	/* Duplicate packet and meta info if requried */
257 	if (dup != NULL) {
258 		struct mbuf *m2;
259 		meta_p meta2;
260 
261 		/* Copy packet */
262 		m2 = m_copypacket(m, M_NOWAIT);
263 		if (m2 == NULL) {
264 			NG_FREE_DATA(m, meta);
265 			return (ENOBUFS);
266 		}
267 
268 		/* Copy meta info */
269 		MALLOC(meta2, meta_p,
270 		    meta->used_len, M_NETGRAPH, M_NOWAIT);
271 		if (meta2 == NULL) {
272 			m_freem(m2);
273 			NG_FREE_DATA(m, meta);
274 			return (ENOMEM);
275 		}
276 		meta2->allocated_len = meta->used_len;
277 		bcopy(meta, meta2, meta->used_len);
278 
279 		/* Deliver duplicate */
280 		dup->stats.outOctets += m->m_pkthdr.len;
281 		dup->stats.outFrames++;
282 		NG_SEND_DATA(error, dup->hook, m2, meta2);
283 	}
284 
285 	/* Deliver frame out destination hook */
286 	dest->stats.outOctets += m->m_pkthdr.len;
287 	dest->stats.outFrames++;
288 	NG_SEND_DATA(error, dest->hook, m, meta);
289 	return (0);
290 }
291 
292 /*
293  * Shutdown processing
294  *
295  * This is tricky. If we have both a left and right hook, then we
296  * probably want to extricate ourselves and leave the two peers
297  * still linked to each other. Otherwise we should just shut down as
298  * a normal node would.
299  *
300  * To keep the scope of info correct the routine to "extract" a node
301  * from two links is in ng_base.c.
302  */
303 static int
304 ngt_rmnode(node_p node)
305 {
306 	const sc_p privdata = node->private;
307 
308 	node->flags |= NG_INVALID;
309 	if (privdata->left.hook && privdata->right.hook)
310 		ng_bypass(privdata->left.hook, privdata->right.hook);
311 	ng_cutlinks(node);
312 	ng_unname(node);
313 	node->private = NULL;
314 	ng_unref(privdata->node);
315 	FREE(privdata, M_NETGRAPH);
316 	return (0);
317 }
318 
319 /*
320  * Hook disconnection
321  */
322 static int
323 ngt_disconnect(hook_p hook)
324 {
325 	struct hookinfo *const hinfo = (struct hookinfo *) hook->private;
326 
327 	KASSERT(hinfo != NULL, ("%s: null info", __FUNCTION__));
328 	hinfo->hook = NULL;
329 	if (hook->node->numhooks == 0)
330 		ng_rmnode(hook->node);
331 	return (0);
332 }
333 
334