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@freebsd.org> 38 *
|
39 * $FreeBSD: head/sys/netgraph/ng_tee.c 69922 2000-12-12 18:52:14Z julian $
|
39 * $FreeBSD: head/sys/netgraph/ng_tee.c 70159 2000-12-18 20:03:32Z julian $ |
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_parse.h> 61#include <netgraph/ng_tee.h> 62 63/* Per hook info */ 64struct hookinfo { 65 hook_p hook; 66 struct ng_tee_hookstat stats; 67}; 68 69/* Per node info */ 70struct privdata { 71 node_p node; 72 struct hookinfo left; 73 struct hookinfo right; 74 struct hookinfo left2right; 75 struct hookinfo right2left; 76}; 77typedef struct privdata *sc_p; 78 79/* Netgraph methods */ 80static ng_constructor_t ngt_constructor; 81static ng_rcvmsg_t ngt_rcvmsg; 82static ng_shutdown_t ngt_rmnode; 83static ng_newhook_t ngt_newhook; 84static ng_rcvdata_t ngt_rcvdata; 85static ng_disconnect_t ngt_disconnect; 86 87/* Parse type for struct ng_tee_hookstat */ 88static const struct ng_parse_struct_info 89 ng_tee_hookstat_type_info = NG_TEE_HOOKSTAT_INFO; 90static const struct ng_parse_type ng_tee_hookstat_type = { 91 &ng_parse_struct_type, 92 &ng_tee_hookstat_type_info, 93}; 94 95/* Parse type for struct ng_tee_stats */ 96static const struct ng_parse_struct_info 97 ng_tee_stats_type_info = NG_TEE_STATS_INFO(&ng_tee_hookstat_type); 98static const struct ng_parse_type ng_tee_stats_type = { 99 &ng_parse_struct_type, 100 &ng_tee_stats_type_info, 101}; 102 103/* List of commands and how to convert arguments to/from ASCII */ 104static const struct ng_cmdlist ng_tee_cmds[] = { 105 { 106 NGM_TEE_COOKIE, 107 NGM_TEE_GET_STATS, 108 "getstats", 109 NULL, 110 &ng_tee_stats_type 111 }, 112 { 113 NGM_TEE_COOKIE, 114 NGM_TEE_CLR_STATS, 115 "clrstats", 116 NULL, 117 NULL 118 }, 119 { 120 NGM_TEE_COOKIE, 121 NGM_TEE_GETCLR_STATS, 122 "getclrstats", 123 NULL, 124 &ng_tee_stats_type 125 }, 126 { 0 } 127}; 128 129/* Netgraph type descriptor */ 130static struct ng_type ng_tee_typestruct = {
|
131 NG_VERSION,
|
131 NG_ABI_VERSION, |
132 NG_TEE_NODE_TYPE, 133 NULL, 134 ngt_constructor, 135 ngt_rcvmsg, 136 ngt_rmnode, 137 ngt_newhook, 138 NULL, 139 NULL, 140 ngt_rcvdata, 141 ngt_disconnect, 142 ng_tee_cmds 143}; 144NETGRAPH_INIT(tee, &ng_tee_typestruct); 145 146/* 147 * Node constructor 148 */ 149static int 150ngt_constructor(node_p *nodep) 151{ 152 sc_p privdata; 153 int error = 0; 154 155 MALLOC(privdata, sc_p, sizeof(*privdata), M_NETGRAPH, M_NOWAIT|M_ZERO); 156 if (privdata == NULL) 157 return (ENOMEM); 158 159 if ((error = ng_make_node_common(&ng_tee_typestruct, nodep))) { 160 FREE(privdata, M_NETGRAPH); 161 return (error); 162 } 163 (*nodep)->private = privdata; 164 privdata->node = *nodep; 165 return (0); 166} 167 168/* 169 * Add a hook 170 */ 171static int 172ngt_newhook(node_p node, hook_p hook, const char *name) 173{ 174 const sc_p sc = node->private; 175 176 if (strcmp(name, NG_TEE_HOOK_RIGHT) == 0) { 177 sc->right.hook = hook; 178 bzero(&sc->right.stats, sizeof(sc->right.stats)); 179 hook->private = &sc->right; 180 } else if (strcmp(name, NG_TEE_HOOK_LEFT) == 0) { 181 sc->left.hook = hook; 182 bzero(&sc->left.stats, sizeof(sc->left.stats)); 183 hook->private = &sc->left; 184 } else if (strcmp(name, NG_TEE_HOOK_RIGHT2LEFT) == 0) { 185 sc->right2left.hook = hook; 186 bzero(&sc->right2left.stats, sizeof(sc->right2left.stats)); 187 hook->private = &sc->right2left; 188 } else if (strcmp(name, NG_TEE_HOOK_LEFT2RIGHT) == 0) { 189 sc->left2right.hook = hook; 190 bzero(&sc->left2right.stats, sizeof(sc->left2right.stats)); 191 hook->private = &sc->left2right; 192 } else 193 return (EINVAL); 194 return (0); 195} 196 197/* 198 * Receive a control message 199 */ 200static int 201ngt_rcvmsg(node_p node, struct ng_mesg *msg, const char *retaddr, 202 struct ng_mesg **rptr, hook_p lasthook) 203{ 204 const sc_p sc = node->private; 205 struct ng_mesg *resp = NULL; 206 int error = 0; 207 208 switch (msg->header.typecookie) { 209 case NGM_TEE_COOKIE: 210 switch (msg->header.cmd) { 211 case NGM_TEE_GET_STATS: 212 case NGM_TEE_CLR_STATS: 213 case NGM_TEE_GETCLR_STATS: 214 { 215 struct ng_tee_stats *stats; 216 217 if (msg->header.cmd != NGM_TEE_CLR_STATS) { 218 NG_MKRESPONSE(resp, msg, 219 sizeof(*stats), M_NOWAIT); 220 if (resp == NULL) { 221 error = ENOMEM; 222 goto done; 223 } 224 stats = (struct ng_tee_stats *)resp->data; 225 bcopy(&sc->right.stats, &stats->right, 226 sizeof(stats->right)); 227 bcopy(&sc->left.stats, &stats->left, 228 sizeof(stats->left)); 229 bcopy(&sc->right2left.stats, &stats->right2left, 230 sizeof(stats->right2left)); 231 bcopy(&sc->left2right.stats, &stats->left2right, 232 sizeof(stats->left2right)); 233 } 234 if (msg->header.cmd != NGM_TEE_GET_STATS) { 235 bzero(&sc->right.stats, 236 sizeof(sc->right.stats)); 237 bzero(&sc->left.stats, 238 sizeof(sc->left.stats)); 239 bzero(&sc->right2left.stats, 240 sizeof(sc->right2left.stats)); 241 bzero(&sc->left2right.stats, 242 sizeof(sc->left2right.stats)); 243 } 244 break; 245 } 246 default: 247 error = EINVAL; 248 break; 249 } 250 break; 251 default: 252 error = EINVAL; 253 break; 254 } 255 if (rptr) 256 *rptr = resp; 257 else if (resp) 258 FREE(resp, M_NETGRAPH); 259 260done: 261 FREE(msg, M_NETGRAPH); 262 return (error); 263} 264 265/* 266 * Receive data on a hook 267 * 268 * If data comes in the right link send a copy out right2left, and then 269 * send the original onwards out through the left link. 270 * Do the opposite for data coming in from the left link. 271 * Data coming in right2left or left2right is forwarded 272 * on through the appropriate destination hook as if it had come 273 * from the other side. 274 */ 275static int 276ngt_rcvdata(hook_p hook, struct mbuf *m, meta_p meta, 277 struct mbuf **ret_m, meta_p *ret_meta, struct ng_mesg **resp) 278{ 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; 283 int error = 0; 284 285 /* Which hook? */ 286 if (hinfo == &sc->left) { 287 dup = &sc->left2right; 288 dest = &sc->right; 289 } else if (hinfo == &sc->right) { 290 dup = &sc->right2left; 291 dest = &sc->left; 292 } else if (hinfo == &sc->right2left) { 293 dup = NULL; 294 dest = &sc->left; 295 } else if (hinfo == &sc->left2right) { 296 dup = NULL; 297 dest = &sc->right; 298 } else 299 panic("%s: no hook!", __FUNCTION__); 300 301 /* Update stats on incoming hook */ 302 hinfo->stats.inOctets += m->m_pkthdr.len; 303 hinfo->stats.inFrames++; 304 305 /* Duplicate packet and meta info if requried */ 306 if (dup != NULL) { 307 struct mbuf *m2; 308 meta_p meta2; 309 310 /* Copy packet */ 311 m2 = m_dup(m, M_NOWAIT); 312 if (m2 == NULL) { 313 NG_FREE_DATA(m, meta); 314 return (ENOBUFS); 315 } 316 317 /* Copy meta info */ 318 if (meta != NULL) { 319 MALLOC(meta2, meta_p, 320 meta->used_len, M_NETGRAPH, M_NOWAIT); 321 if (meta2 == NULL) { 322 m_freem(m2); 323 NG_FREE_DATA(m, meta); 324 return (ENOMEM); 325 } 326 bcopy(meta, meta2, meta->used_len); 327 meta2->allocated_len = meta->used_len; 328 } else 329 meta2 = NULL; 330 331 /* Deliver duplicate */ 332 dup->stats.outOctets += m->m_pkthdr.len; 333 dup->stats.outFrames++; 334 NG_SEND_DATA(error, dup->hook, m2, meta2); 335 } 336 337 /* Deliver frame out destination hook */ 338 dest->stats.outOctets += m->m_pkthdr.len; 339 dest->stats.outFrames++; 340 NG_SEND_DATA(error, dest->hook, m, meta); 341 return (0); 342} 343 344/* 345 * Shutdown processing 346 * 347 * This is tricky. If we have both a left and right hook, then we 348 * probably want to extricate ourselves and leave the two peers 349 * still linked to each other. Otherwise we should just shut down as 350 * a normal node would. 351 * 352 * To keep the scope of info correct the routine to "extract" a node 353 * from two links is in ng_base.c. 354 */ 355static int 356ngt_rmnode(node_p node) 357{ 358 const sc_p privdata = node->private; 359 360 node->flags |= NG_INVALID; 361 if (privdata->left.hook && privdata->right.hook) 362 ng_bypass(privdata->left.hook, privdata->right.hook); 363 ng_cutlinks(node); 364 ng_unname(node); 365 node->private = NULL; 366 ng_unref(privdata->node); 367 FREE(privdata, M_NETGRAPH); 368 return (0); 369} 370 371/* 372 * Hook disconnection 373 */ 374static int 375ngt_disconnect(hook_p hook) 376{ 377 struct hookinfo *const hinfo = (struct hookinfo *) hook->private; 378 379 KASSERT(hinfo != NULL, ("%s: null info", __FUNCTION__)); 380 hinfo->hook = NULL; 381 if (hook->node->numhooks == 0) 382 ng_rmnode(hook->node); 383 return (0); 384} 385
|