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 * 41 * $FreeBSD$ 42 * $Whistle: ng_tee.c,v 1.18 1999/11/01 09:24:52 julian Exp $ 43 */ 44 45 /* 46 * This node is like the tee(1) command and is useful for ``snooping.'' 47 * It has 4 hooks: left, right, left2right, and right2left. Data 48 * entering from the right is passed to the left and duplicated on 49 * right2left, and data entering from the left is passed to the right 50 * and duplicated on left2right. Data entering from left2right is 51 * sent to left, and data from right2left to right. 52 */ 53 54 #include <sys/param.h> 55 #include <sys/systm.h> 56 #include <sys/errno.h> 57 #include <sys/kernel.h> 58 #include <sys/malloc.h> 59 #include <sys/mbuf.h> 60 #include <netgraph/ng_message.h> 61 #include <netgraph/netgraph.h> 62 #include <netgraph/ng_parse.h> 63 #include <netgraph/ng_tee.h> 64 65 /* Per hook info */ 66 struct hookinfo { 67 hook_p hook; 68 struct ng_tee_hookstat stats; 69 }; 70 71 /* Per node info */ 72 struct privdata { 73 node_p node; 74 struct hookinfo left; 75 struct hookinfo right; 76 struct hookinfo left2right; 77 struct hookinfo right2left; 78 }; 79 typedef struct privdata *sc_p; 80 81 /* Netgraph methods */ 82 static ng_constructor_t ngt_constructor; 83 static ng_rcvmsg_t ngt_rcvmsg; 84 static ng_close_t ngt_close; 85 static ng_shutdown_t ngt_shutdown; 86 static ng_newhook_t ngt_newhook; 87 static ng_rcvdata_t ngt_rcvdata; 88 static ng_disconnect_t ngt_disconnect; 89 90 /* Parse type for struct ng_tee_hookstat */ 91 static const struct ng_parse_struct_field ng_tee_hookstat_type_fields[] 92 = NG_TEE_HOOKSTAT_INFO; 93 static const struct ng_parse_type ng_tee_hookstat_type = { 94 &ng_parse_struct_type, 95 &ng_tee_hookstat_type_fields 96 }; 97 98 /* Parse type for struct ng_tee_stats */ 99 static const struct ng_parse_struct_field ng_tee_stats_type_fields[] 100 = NG_TEE_STATS_INFO(&ng_tee_hookstat_type); 101 static const struct ng_parse_type ng_tee_stats_type = { 102 &ng_parse_struct_type, 103 &ng_tee_stats_type_fields 104 }; 105 106 /* List of commands and how to convert arguments to/from ASCII */ 107 static const struct ng_cmdlist ng_tee_cmds[] = { 108 { 109 NGM_TEE_COOKIE, 110 NGM_TEE_GET_STATS, 111 "getstats", 112 NULL, 113 &ng_tee_stats_type 114 }, 115 { 116 NGM_TEE_COOKIE, 117 NGM_TEE_CLR_STATS, 118 "clrstats", 119 NULL, 120 NULL 121 }, 122 { 123 NGM_TEE_COOKIE, 124 NGM_TEE_GETCLR_STATS, 125 "getclrstats", 126 NULL, 127 &ng_tee_stats_type 128 }, 129 { 0 } 130 }; 131 132 /* Netgraph type descriptor */ 133 static struct ng_type ng_tee_typestruct = { 134 .version = NG_ABI_VERSION, 135 .name = NG_TEE_NODE_TYPE, 136 .constructor = ngt_constructor, 137 .rcvmsg = ngt_rcvmsg, 138 .close = ngt_close, 139 .shutdown = ngt_shutdown, 140 .newhook = ngt_newhook, 141 .rcvdata = ngt_rcvdata, 142 .disconnect = ngt_disconnect, 143 .cmdlist = ng_tee_cmds, 144 }; 145 NETGRAPH_INIT(tee, &ng_tee_typestruct); 146 147 /* 148 * Node constructor 149 */ 150 static int 151 ngt_constructor(node_p node) 152 { 153 sc_p privdata; 154 155 MALLOC(privdata, sc_p, sizeof(*privdata), M_NETGRAPH, M_NOWAIT|M_ZERO); 156 if (privdata == NULL) 157 return (ENOMEM); 158 159 NG_NODE_SET_PRIVATE(node, privdata); 160 privdata->node = node; 161 return (0); 162 } 163 164 /* 165 * Add a hook 166 */ 167 static int 168 ngt_newhook(node_p node, hook_p hook, const char *name) 169 { 170 const sc_p sc = NG_NODE_PRIVATE(node); 171 172 if (strcmp(name, NG_TEE_HOOK_RIGHT) == 0) { 173 sc->right.hook = hook; 174 bzero(&sc->right.stats, sizeof(sc->right.stats)); 175 NG_HOOK_SET_PRIVATE(hook, &sc->right); 176 } else if (strcmp(name, NG_TEE_HOOK_LEFT) == 0) { 177 sc->left.hook = hook; 178 bzero(&sc->left.stats, sizeof(sc->left.stats)); 179 NG_HOOK_SET_PRIVATE(hook, &sc->left); 180 } else if (strcmp(name, NG_TEE_HOOK_RIGHT2LEFT) == 0) { 181 sc->right2left.hook = hook; 182 bzero(&sc->right2left.stats, sizeof(sc->right2left.stats)); 183 NG_HOOK_SET_PRIVATE(hook, &sc->right2left); 184 } else if (strcmp(name, NG_TEE_HOOK_LEFT2RIGHT) == 0) { 185 sc->left2right.hook = hook; 186 bzero(&sc->left2right.stats, sizeof(sc->left2right.stats)); 187 NG_HOOK_SET_PRIVATE(hook, &sc->left2right); 188 } else 189 return (EINVAL); 190 return (0); 191 } 192 193 /* 194 * Receive a control message 195 */ 196 static int 197 ngt_rcvmsg(node_p node, item_p item, hook_p lasthook) 198 { 199 const sc_p sc = NG_NODE_PRIVATE(node); 200 struct ng_mesg *resp = NULL; 201 int error = 0; 202 struct ng_mesg *msg; 203 204 NGI_GET_MSG(item, msg); 205 switch (msg->header.typecookie) { 206 case NGM_TEE_COOKIE: 207 switch (msg->header.cmd) { 208 case NGM_TEE_GET_STATS: 209 case NGM_TEE_CLR_STATS: 210 case NGM_TEE_GETCLR_STATS: 211 { 212 struct ng_tee_stats *stats; 213 214 if (msg->header.cmd != NGM_TEE_CLR_STATS) { 215 NG_MKRESPONSE(resp, msg, 216 sizeof(*stats), M_NOWAIT); 217 if (resp == NULL) { 218 error = ENOMEM; 219 goto done; 220 } 221 stats = (struct ng_tee_stats *)resp->data; 222 bcopy(&sc->right.stats, &stats->right, 223 sizeof(stats->right)); 224 bcopy(&sc->left.stats, &stats->left, 225 sizeof(stats->left)); 226 bcopy(&sc->right2left.stats, &stats->right2left, 227 sizeof(stats->right2left)); 228 bcopy(&sc->left2right.stats, &stats->left2right, 229 sizeof(stats->left2right)); 230 } 231 if (msg->header.cmd != NGM_TEE_GET_STATS) { 232 bzero(&sc->right.stats, 233 sizeof(sc->right.stats)); 234 bzero(&sc->left.stats, 235 sizeof(sc->left.stats)); 236 bzero(&sc->right2left.stats, 237 sizeof(sc->right2left.stats)); 238 bzero(&sc->left2right.stats, 239 sizeof(sc->left2right.stats)); 240 } 241 break; 242 } 243 default: 244 error = EINVAL; 245 break; 246 } 247 break; 248 case NGM_FLOW_COOKIE: 249 if (lasthook) { 250 if (lasthook == sc->left.hook) { 251 if (sc->right.hook) { 252 NGI_MSG(item) = msg; 253 NG_FWD_ITEM_HOOK(error, item, 254 sc->right.hook); 255 return (error); 256 } 257 } else { 258 if (sc->left.hook) { 259 NGI_MSG(item) = msg; 260 NG_FWD_ITEM_HOOK(error, item, 261 sc->left.hook); 262 return (error); 263 } 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 ngt_rcvdata(hook_p hook, item_p item) 289 { 290 const sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 291 struct hookinfo *const hinfo = NG_HOOK_PRIVATE(hook); 292 struct hookinfo *dest; 293 struct hookinfo *dup; 294 int error = 0; 295 struct mbuf *m; 296 297 m = NGI_M(item); 298 /* Which hook? */ 299 if (hinfo == &sc->left) { 300 dup = &sc->left2right; 301 dest = &sc->right; 302 } else if (hinfo == &sc->right) { 303 dup = &sc->right2left; 304 dest = &sc->left; 305 } else if (hinfo == &sc->right2left) { 306 dup = NULL; 307 dest = &sc->right; 308 } else if (hinfo == &sc->left2right) { 309 dup = NULL; 310 dest = &sc->left; 311 } else { 312 panic("%s: no hook!", __func__); 313 #ifdef RESTARTABLE_PANICS 314 return(EINVAL); 315 #endif 316 } 317 318 /* Update stats on incoming hook */ 319 hinfo->stats.inOctets += m->m_pkthdr.len; 320 hinfo->stats.inFrames++; 321 322 /* 323 * Don't make a copy if only the dup hook exists. 324 */ 325 if ((dup && dup->hook) && (dest->hook == NULL)) { 326 dest = dup; 327 dup = NULL; 328 } 329 330 /* Duplicate packet if requried */ 331 if (dup && dup->hook) { 332 struct mbuf *m2; 333 334 /* Copy packet (failure will not stop the original)*/ 335 m2 = m_dup(m, M_DONTWAIT); 336 if (m2) { 337 /* Deliver duplicate */ 338 dup->stats.outOctets += m->m_pkthdr.len; 339 dup->stats.outFrames++; 340 NG_SEND_DATA_ONLY(error, dup->hook, m2); 341 } 342 } 343 /* Deliver frame out destination hook */ 344 if (dest->hook) { 345 dest->stats.outOctets += m->m_pkthdr.len; 346 dest->stats.outFrames++; 347 NG_FWD_ITEM_HOOK(error, item, dest->hook); 348 } else 349 NG_FREE_ITEM(item); 350 return (error); 351 } 352 353 /* 354 * We are going to be shut down soon 355 * 356 * If we have both a left and right hook, then we probably want to extricate 357 * ourselves and leave the two peers still linked to each other. Otherwise we 358 * should just shut down as a normal node would. 359 */ 360 static int 361 ngt_close(node_p node) 362 { 363 const sc_p privdata = NG_NODE_PRIVATE(node); 364 365 if (privdata->left.hook && privdata->right.hook) 366 ng_bypass(privdata->left.hook, privdata->right.hook); 367 368 return (0); 369 } 370 371 /* 372 * Shutdown processing 373 */ 374 static int 375 ngt_shutdown(node_p node) 376 { 377 const sc_p privdata = NG_NODE_PRIVATE(node); 378 379 NG_NODE_SET_PRIVATE(node, NULL); 380 NG_NODE_UNREF(privdata->node); 381 FREE(privdata, M_NETGRAPH); 382 return (0); 383 } 384 385 /* 386 * Hook disconnection 387 */ 388 static int 389 ngt_disconnect(hook_p hook) 390 { 391 struct hookinfo *const hinfo = NG_HOOK_PRIVATE(hook); 392 393 KASSERT(hinfo != NULL, ("%s: null info", __func__)); 394 hinfo->hook = NULL; 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 401