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