| Commit | Line | Data |
|---|---|---|
| b06ebda0 MD |
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: src/sys/netgraph/ng_tee.c,v 1.35 2008/02/24 10:13:32 mav Exp $ | |
| 5a975a3d | 42 | * $DragonFly: src/sys/netgraph7/ng_tee.c,v 1.2 2008/06/26 23:05:35 dillon Exp $ |
| b06ebda0 MD |
43 | * $Whistle: ng_tee.c,v 1.18 1999/11/01 09:24:52 julian Exp $ |
| 44 | */ | |
| 45 | ||
| 46 | /* | |
| 47 | * This node is like the tee(1) command and is useful for ``snooping.'' | |
| 48 | * It has 4 hooks: left, right, left2right, and right2left. Data | |
| 49 | * entering from the right is passed to the left and duplicated on | |
| 50 | * right2left, and data entering from the left is passed to the right | |
| 51 | * and duplicated on left2right. Data entering from left2right is | |
| 52 | * sent to left, and data from right2left to right. | |
| 53 | */ | |
| 54 | ||
| 55 | #include <sys/param.h> | |
| 56 | #include <sys/systm.h> | |
| 57 | #include <sys/errno.h> | |
| 58 | #include <sys/kernel.h> | |
| 59 | #include <sys/malloc.h> | |
| 60 | #include <sys/mbuf.h> | |
| 5a975a3d MD |
61 | #include "ng_message.h" |
| 62 | #include "netgraph.h" | |
| 63 | #include "ng_parse.h" | |
| 64 | #include "ng_tee.h" | |
| b06ebda0 MD |
65 | |
| 66 | /* Per hook info */ | |
| 67 | struct hookinfo { | |
| 68 | hook_p hook; | |
| 69 | struct hookinfo *dest, *dup; | |
| 70 | struct ng_tee_hookstat stats; | |
| 71 | }; | |
| 72 | typedef struct hookinfo *hi_p; | |
| 73 | ||
| 74 | /* Per node info */ | |
| 75 | struct privdata { | |
| 76 | struct hookinfo left; | |
| 77 | struct hookinfo right; | |
| 78 | struct hookinfo left2right; | |
| 79 | struct hookinfo right2left; | |
| 80 | }; | |
| 81 | typedef struct privdata *sc_p; | |
| 82 | ||
| 83 | /* Netgraph methods */ | |
| 84 | static ng_constructor_t ng_tee_constructor; | |
| 85 | static ng_rcvmsg_t ng_tee_rcvmsg; | |
| 86 | static ng_close_t ng_tee_close; | |
| 87 | static ng_shutdown_t ng_tee_shutdown; | |
| 88 | static ng_newhook_t ng_tee_newhook; | |
| 89 | static ng_rcvdata_t ng_tee_rcvdata; | |
| 90 | static ng_disconnect_t ng_tee_disconnect; | |
| 91 | ||
| 92 | /* Parse type for struct ng_tee_hookstat */ | |
| 93 | static const struct ng_parse_struct_field ng_tee_hookstat_type_fields[] | |
| 94 | = NG_TEE_HOOKSTAT_INFO; | |
| 95 | static const struct ng_parse_type ng_tee_hookstat_type = { | |
| 96 | &ng_parse_struct_type, | |
| 97 | &ng_tee_hookstat_type_fields | |
| 98 | }; | |
| 99 | ||
| 100 | /* Parse type for struct ng_tee_stats */ | |
| 101 | static const struct ng_parse_struct_field ng_tee_stats_type_fields[] | |
| 102 | = NG_TEE_STATS_INFO(&ng_tee_hookstat_type); | |
| 103 | static const struct ng_parse_type ng_tee_stats_type = { | |
| 104 | &ng_parse_struct_type, | |
| 105 | &ng_tee_stats_type_fields | |
| 106 | }; | |
| 107 | ||
| 108 | /* List of commands and how to convert arguments to/from ASCII */ | |
| 109 | static const struct ng_cmdlist ng_tee_cmds[] = { | |
| 110 | { | |
| 111 | NGM_TEE_COOKIE, | |
| 112 | NGM_TEE_GET_STATS, | |
| 113 | "getstats", | |
| 114 | NULL, | |
| 115 | &ng_tee_stats_type | |
| 116 | }, | |
| 117 | { | |
| 118 | NGM_TEE_COOKIE, | |
| 119 | NGM_TEE_CLR_STATS, | |
| 120 | "clrstats", | |
| 121 | NULL, | |
| 122 | NULL | |
| 123 | }, | |
| 124 | { | |
| 125 | NGM_TEE_COOKIE, | |
| 126 | NGM_TEE_GETCLR_STATS, | |
| 127 | "getclrstats", | |
| 128 | NULL, | |
| 129 | &ng_tee_stats_type | |
| 130 | }, | |
| 131 | { 0 } | |
| 132 | }; | |
| 133 | ||
| 134 | /* Netgraph type descriptor */ | |
| 135 | static struct ng_type ng_tee_typestruct = { | |
| 136 | .version = NG_ABI_VERSION, | |
| 137 | .name = NG_TEE_NODE_TYPE, | |
| 138 | .constructor = ng_tee_constructor, | |
| 139 | .rcvmsg = ng_tee_rcvmsg, | |
| 140 | .close = ng_tee_close, | |
| 141 | .shutdown = ng_tee_shutdown, | |
| 142 | .newhook = ng_tee_newhook, | |
| 143 | .rcvdata = ng_tee_rcvdata, | |
| 144 | .disconnect = ng_tee_disconnect, | |
| 145 | .cmdlist = ng_tee_cmds, | |
| 146 | }; | |
| 147 | NETGRAPH_INIT(tee, &ng_tee_typestruct); | |
| 148 | ||
| 149 | /* | |
| 150 | * Node constructor | |
| 151 | */ | |
| 152 | static int | |
| 153 | ng_tee_constructor(node_p node) | |
| 154 | { | |
| 155 | sc_p privdata; | |
| 156 | ||
| fc025606 SW |
157 | privdata = kmalloc(sizeof(*privdata), M_NETGRAPH, |
| 158 | M_WAITOK | M_NULLOK | M_ZERO); | |
| b06ebda0 MD |
159 | if (privdata == NULL) |
| 160 | return (ENOMEM); | |
| 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 pathes. */ | |
| 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, | |
| 5a975a3d | 231 | sizeof(*stats), M_WAITOK | M_NULLOK); |
| b06ebda0 MD |
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)*/ | |
| 5a975a3d | 312 | m2 = m_dup(m, MB_DONTWAIT); |
| b06ebda0 MD |
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); | |
| fc025606 | 361 | kfree(privdata, M_NETGRAPH); |
| b06ebda0 MD |
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 pathes. */ | |
| 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 | } |