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 #ifdef NG_SEPARATE_MALLOC 64 static MALLOC_DEFINE(M_NETGRAPH_TEE, "netgraph_tee", "netgraph tee node"); 65 #else 66 #define M_NETGRAPH_TEE M_NETGRAPH 67 #endif 68 69 /* Per hook info */ 70 struct hookinfo { 71 hook_p hook; 72 struct hookinfo *dest, *dup; 73 struct ng_tee_hookstat stats; 74 }; 75 typedef struct hookinfo *hi_p; 76 77 /* Per node info */ 78 struct privdata { 79 struct hookinfo left; 80 struct hookinfo right; 81 struct hookinfo left2right; 82 struct hookinfo right2left; 83 }; 84 typedef struct privdata *sc_p; 85 86 /* Netgraph methods */ 87 static ng_constructor_t ng_tee_constructor; 88 static ng_rcvmsg_t ng_tee_rcvmsg; 89 static ng_close_t ng_tee_close; 90 static ng_shutdown_t ng_tee_shutdown; 91 static ng_newhook_t ng_tee_newhook; 92 static ng_rcvdata_t ng_tee_rcvdata; 93 static ng_disconnect_t ng_tee_disconnect; 94 95 /* Parse type for struct ng_tee_hookstat */ 96 static const struct ng_parse_struct_field ng_tee_hookstat_type_fields[] 97 = NG_TEE_HOOKSTAT_INFO; 98 static const struct ng_parse_type ng_tee_hookstat_type = { 99 &ng_parse_struct_type, 100 &ng_tee_hookstat_type_fields 101 }; 102 103 /* Parse type for struct ng_tee_stats */ 104 static const struct ng_parse_struct_field ng_tee_stats_type_fields[] 105 = NG_TEE_STATS_INFO(&ng_tee_hookstat_type); 106 static const struct ng_parse_type ng_tee_stats_type = { 107 &ng_parse_struct_type, 108 &ng_tee_stats_type_fields 109 }; 110 111 /* List of commands and how to convert arguments to/from ASCII */ 112 static const struct ng_cmdlist ng_tee_cmds[] = { 113 { 114 NGM_TEE_COOKIE, 115 NGM_TEE_GET_STATS, 116 "getstats", 117 NULL, 118 &ng_tee_stats_type 119 }, 120 { 121 NGM_TEE_COOKIE, 122 NGM_TEE_CLR_STATS, 123 "clrstats", 124 NULL, 125 NULL 126 }, 127 { 128 NGM_TEE_COOKIE, 129 NGM_TEE_GETCLR_STATS, 130 "getclrstats", 131 NULL, 132 &ng_tee_stats_type 133 }, 134 { 0 } 135 }; 136 137 /* Netgraph type descriptor */ 138 static struct ng_type ng_tee_typestruct = { 139 .version = NG_ABI_VERSION, 140 .name = NG_TEE_NODE_TYPE, 141 .constructor = ng_tee_constructor, 142 .rcvmsg = ng_tee_rcvmsg, 143 .close = ng_tee_close, 144 .shutdown = ng_tee_shutdown, 145 .newhook = ng_tee_newhook, 146 .rcvdata = ng_tee_rcvdata, 147 .disconnect = ng_tee_disconnect, 148 .cmdlist = ng_tee_cmds, 149 }; 150 NETGRAPH_INIT(tee, &ng_tee_typestruct); 151 152 /* 153 * Node constructor 154 */ 155 static int 156 ng_tee_constructor(node_p node) 157 { 158 sc_p privdata; 159 160 privdata = malloc(sizeof(*privdata), M_NETGRAPH_TEE, M_WAITOK | M_ZERO); 161 162 NG_NODE_SET_PRIVATE(node, privdata); 163 return (0); 164 } 165 166 /* 167 * Add a hook 168 */ 169 static int 170 ng_tee_newhook(node_p node, hook_p hook, const char *name) 171 { 172 sc_p privdata = NG_NODE_PRIVATE(node); 173 hi_p hinfo; 174 175 /* Precalculate internal paths. */ 176 if (strcmp(name, NG_TEE_HOOK_RIGHT) == 0) { 177 hinfo = &privdata->right; 178 if (privdata->left.dest) 179 privdata->left.dup = privdata->left.dest; 180 privdata->left.dest = hinfo; 181 privdata->right2left.dest = hinfo; 182 } else if (strcmp(name, NG_TEE_HOOK_LEFT) == 0) { 183 hinfo = &privdata->left; 184 if (privdata->right.dest) 185 privdata->right.dup = privdata->right.dest; 186 privdata->right.dest = hinfo; 187 privdata->left2right.dest = hinfo; 188 } else if (strcmp(name, NG_TEE_HOOK_RIGHT2LEFT) == 0) { 189 hinfo = &privdata->right2left; 190 if (privdata->right.dest) 191 privdata->right.dup = hinfo; 192 else 193 privdata->right.dest = hinfo; 194 } else if (strcmp(name, NG_TEE_HOOK_LEFT2RIGHT) == 0) { 195 hinfo = &privdata->left2right; 196 if (privdata->left.dest) 197 privdata->left.dup = hinfo; 198 else 199 privdata->left.dest = hinfo; 200 } else 201 return (EINVAL); 202 hinfo->hook = hook; 203 bzero(&hinfo->stats, sizeof(hinfo->stats)); 204 NG_HOOK_SET_PRIVATE(hook, hinfo); 205 return (0); 206 } 207 208 /* 209 * Receive a control message 210 */ 211 static int 212 ng_tee_rcvmsg(node_p node, item_p item, hook_p lasthook) 213 { 214 const sc_p sc = NG_NODE_PRIVATE(node); 215 struct ng_mesg *resp = NULL; 216 int error = 0; 217 struct ng_mesg *msg; 218 219 NGI_GET_MSG(item, msg); 220 switch (msg->header.typecookie) { 221 case NGM_TEE_COOKIE: 222 switch (msg->header.cmd) { 223 case NGM_TEE_GET_STATS: 224 case NGM_TEE_CLR_STATS: 225 case NGM_TEE_GETCLR_STATS: 226 { 227 struct ng_tee_stats *stats; 228 229 if (msg->header.cmd != NGM_TEE_CLR_STATS) { 230 NG_MKRESPONSE(resp, msg, 231 sizeof(*stats), M_NOWAIT); 232 if (resp == NULL) { 233 error = ENOMEM; 234 goto done; 235 } 236 stats = (struct ng_tee_stats *)resp->data; 237 bcopy(&sc->right.stats, &stats->right, 238 sizeof(stats->right)); 239 bcopy(&sc->left.stats, &stats->left, 240 sizeof(stats->left)); 241 bcopy(&sc->right2left.stats, &stats->right2left, 242 sizeof(stats->right2left)); 243 bcopy(&sc->left2right.stats, &stats->left2right, 244 sizeof(stats->left2right)); 245 } 246 if (msg->header.cmd != NGM_TEE_GET_STATS) { 247 bzero(&sc->right.stats, 248 sizeof(sc->right.stats)); 249 bzero(&sc->left.stats, 250 sizeof(sc->left.stats)); 251 bzero(&sc->right2left.stats, 252 sizeof(sc->right2left.stats)); 253 bzero(&sc->left2right.stats, 254 sizeof(sc->left2right.stats)); 255 } 256 break; 257 } 258 default: 259 error = EINVAL; 260 break; 261 } 262 break; 263 case NGM_FLOW_COOKIE: 264 if (lasthook == sc->left.hook || lasthook == sc->right.hook) { 265 hi_p const hinfo = NG_HOOK_PRIVATE(lasthook); 266 if (hinfo && hinfo->dest) { 267 NGI_MSG(item) = msg; 268 NG_FWD_ITEM_HOOK(error, item, hinfo->dest->hook); 269 return (error); 270 } 271 } 272 break; 273 default: 274 error = EINVAL; 275 break; 276 } 277 done: 278 NG_RESPOND_MSG(error, node, item, resp); 279 NG_FREE_MSG(msg); 280 return (error); 281 } 282 283 /* 284 * Receive data on a hook 285 * 286 * If data comes in the right link send a copy out right2left, and then 287 * send the original onwards out through the left link. 288 * Do the opposite for data coming in from the left link. 289 * Data coming in right2left or left2right is forwarded 290 * on through the appropriate destination hook as if it had come 291 * from the other side. 292 */ 293 static int 294 ng_tee_rcvdata(hook_p hook, item_p item) 295 { 296 const hi_p hinfo = NG_HOOK_PRIVATE(hook); 297 hi_p h; 298 int error = 0; 299 struct mbuf *m; 300 301 m = NGI_M(item); 302 303 /* Update stats on incoming hook */ 304 hinfo->stats.inOctets += m->m_pkthdr.len; 305 hinfo->stats.inFrames++; 306 307 /* Duplicate packet if requried */ 308 if (hinfo->dup) { 309 struct mbuf *m2; 310 311 /* Copy packet (failure will not stop the original)*/ 312 m2 = m_dup(m, M_NOWAIT); 313 if (m2) { 314 /* Deliver duplicate */ 315 h = hinfo->dup; 316 NG_SEND_DATA_ONLY(error, h->hook, m2); 317 if (error == 0) { 318 h->stats.outOctets += m->m_pkthdr.len; 319 h->stats.outFrames++; 320 } 321 } 322 } 323 /* Deliver frame out destination hook */ 324 if (hinfo->dest) { 325 h = hinfo->dest; 326 h->stats.outOctets += m->m_pkthdr.len; 327 h->stats.outFrames++; 328 NG_FWD_ITEM_HOOK(error, item, h->hook); 329 } else 330 NG_FREE_ITEM(item); 331 return (error); 332 } 333 334 /* 335 * We are going to be shut down soon 336 * 337 * If we have both a left and right hook, then we probably want to extricate 338 * ourselves and leave the two peers still linked to each other. Otherwise we 339 * should just shut down as a normal node would. 340 */ 341 static int 342 ng_tee_close(node_p node) 343 { 344 const sc_p privdata = NG_NODE_PRIVATE(node); 345 346 if (privdata->left.hook && privdata->right.hook) 347 ng_bypass(privdata->left.hook, privdata->right.hook); 348 349 return (0); 350 } 351 352 /* 353 * Shutdown processing 354 */ 355 static int 356 ng_tee_shutdown(node_p node) 357 { 358 const sc_p privdata = NG_NODE_PRIVATE(node); 359 360 NG_NODE_SET_PRIVATE(node, NULL); 361 free(privdata, M_NETGRAPH_TEE); 362 NG_NODE_UNREF(node); 363 return (0); 364 } 365 366 /* 367 * Hook disconnection 368 */ 369 static int 370 ng_tee_disconnect(hook_p hook) 371 { 372 sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 373 hi_p const hinfo = NG_HOOK_PRIVATE(hook); 374 375 KASSERT(hinfo != NULL, ("%s: null info", __func__)); 376 hinfo->hook = NULL; 377 378 /* Recalculate internal paths. */ 379 if (sc->left.dest == hinfo) { 380 sc->left.dest = sc->left.dup; 381 sc->left.dup = NULL; 382 } else if (sc->left.dup == hinfo) 383 sc->left.dup = NULL; 384 if (sc->right.dest == hinfo) { 385 sc->right.dest = sc->right.dup; 386 sc->right.dup = NULL; 387 } else if (sc->right.dup == hinfo) 388 sc->right.dup = NULL; 389 if (sc->left2right.dest == hinfo) 390 sc->left2right.dest = NULL; 391 if (sc->right2left.dest == hinfo) 392 sc->right2left.dest = NULL; 393 394 /* Die when last hook disconnected. */ 395 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0) && 396 NG_NODE_IS_VALID(NG_HOOK_NODE(hook))) 397 ng_rmnode_self(NG_HOOK_NODE(hook)); 398 return (0); 399 } 400