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