diff -urNb mpd-3.18/src/iface.c mpd-3.18.patched/src/iface.c --- mpd-3.18/src/iface.c Tue May 4 22:12:28 2004 +++ mpd-3.18.patched/src/iface.c Fri Jan 21 16:49:54 2005 @@ -28,6 +28,7 @@ #include #include #include +#include /* * DEFINITIONS @@ -279,7 +280,12 @@ /* Reset bpf node statistics */ memset(&iface->idleStats, 0, sizeof(iface->idleStats)); + if (gEnableTee) { + snprintf(path, sizeof(path), "%s:%s.%s", iface->ifname, + NG_IFACE_HOOK_INET, NG_TEE_HOOK_RIGHT); + } else { snprintf(path, sizeof(path), "%s:%s", iface->ifname, NG_IFACE_HOOK_INET); + } if (NgSendMsg(bund->csock, path, NGM_BPF_COOKIE, NGM_BPF_CLR_STATS, BPF_HOOK_IFACE, sizeof(BPF_HOOK_IFACE)) < 0) Log(LG_ERR, ("[%s] can't clear %s stats: %s", @@ -832,7 +838,12 @@ /* Get updated bpf node traffic statistics */ oldStats = iface->idleStats; + if (gEnableTee) { + snprintf(path, sizeof(path), "%s:%s.%s", iface->ifname, + NG_IFACE_HOOK_INET, NG_TEE_HOOK_RIGHT); + } else { snprintf(path, sizeof(path), "%s:%s", iface->ifname, NG_IFACE_HOOK_INET); + } if (NgSendMsg(bund->csock, path, NGM_BPF_COOKIE, NGM_BPF_GET_STATS, BPF_HOOK_IFACE, sizeof(BPF_HOOK_IFACE)) < 0) { Log(LG_ERR, ("[%s] can't get %s stats: %s", diff -urNb mpd-3.18/src/main.c mpd-3.18.patched/src/main.c --- mpd-3.18/src/main.c Tue May 4 22:12:28 2004 +++ mpd-3.18.patched/src/main.c Fri Jan 21 16:49:54 2005 @@ -64,6 +64,8 @@ "Show version information" }, { 0, 'h', "help", "", "Show usage information" }, + { 0, 't', "tee", "", + "Insert ng_tee into netgraph" }, }; #define OPTLIST_SIZE (sizeof(OptList) / sizeof(*OptList)) @@ -81,6 +83,7 @@ Bund *gBundles; int gNumLinks; int gNumBundles; + int gEnableTee = FALSE; const char *gConfigFile = CONF_FILE; const char *gConfDirectory = PATH_CONF_DIR; @@ -514,6 +517,9 @@ exit(EX_NORMAL); case 'h': Usage(EX_NORMAL); + case 't': + gEnableTee = TRUE; + return(0); default: assert(0); } diff -urNb mpd-3.18/src/ngfunc.c mpd-3.18.patched/src/ngfunc.c --- mpd-3.18/src/ngfunc.c Tue May 4 22:12:28 2004 +++ mpd-3.18.patched/src/ngfunc.c Fri Jan 21 19:39:35 2005 @@ -28,6 +28,7 @@ #include #include #include +#include /* * DEFINITIONS @@ -249,17 +250,30 @@ goto fail; } - /* Connect the other side of the bpf node to the iface node */ + /* Connect the other side of the bpf node to the iface or tee node */ snprintf(path, sizeof(path), "%s.%s", MPD_HOOK_PPP, NG_PPP_HOOK_INET); + if (gEnableTee) { + snprintf(cn.path, sizeof(cn.path), "%s:%s", + b->iface.ifname, NG_IFACE_HOOK_INET); + snprintf(cn.peerhook, sizeof(cn.peerhook), "%s", NG_TEE_HOOK_RIGHT); + } else { snprintf(cn.path, sizeof(cn.path), "%s:", b->iface.ifname); - snprintf(cn.ourhook, sizeof(cn.ourhook), "%s", BPF_HOOK_IFACE); snprintf(cn.peerhook, sizeof(cn.peerhook), "%s", NG_IFACE_HOOK_INET); + } + snprintf(cn.ourhook, sizeof(cn.ourhook), "%s", BPF_HOOK_IFACE); + if (NgSendMsg(b->csock, path, NGM_GENERIC_COOKIE, NGM_CONNECT, &cn, sizeof(cn)) < 0) { + if (gEnableTee) { + Log(LG_ERR, ("[%s] can't connect %s and %s: %s", + b->name, BPF_HOOK_IFACE, NG_TEE_HOOK_RIGHT, strerror(errno))); + goto fail; + } else { Log(LG_ERR, ("[%s] can't connect %s and %s: %s", b->name, BPF_HOOK_IFACE, NG_IFACE_HOOK_INET, strerror(errno))); goto fail; } + } /* Connect a hook from the bpf node to our socket node */ snprintf(cn.path, sizeof(cn.path), "%s.%s", MPD_HOOK_PPP, NG_PPP_HOOK_INET); @@ -390,6 +404,7 @@ struct nodeinfo *const ni = (struct nodeinfo *)(void *)u.reply.data; struct ngm_rmhook rm; struct ngm_mkpeer mp; + struct ngm_connect cn; int rtn = 0; /* If ifname is not null, create interfaces until it gets created */ @@ -441,6 +456,38 @@ snprintf(buf, max, "%s", ni->name); done: + + /* Disconnect temporary hook */ + snprintf(rm.ourhook, sizeof(rm.ourhook), "%s", TEMPHOOK); + if (NgSendMsg(b->csock, ".", + NGM_GENERIC_COOKIE, NGM_RMHOOK, &rm, sizeof(rm)) < 0) { + Log(LG_ERR, ("[%s] can't remove hook %s: %s", + b->name, TEMPHOOK, strerror(errno))); + rtn = -1; + } + + /* Add netgraph tee node to created iface if configured */ + if (gEnableTee && (rtn != -1)) { + /* Create tee node (as a temporary peer of the socket node) */ + snprintf(mp.type, sizeof(mp.type), "%s", NG_TEE_NODE_TYPE); + snprintf(mp.ourhook, sizeof(mp.ourhook), "%s", TEMPHOOK); + snprintf(mp.peerhook, sizeof(mp.peerhook), "%s", NG_TEE_HOOK_RIGHT); + if (NgSendMsg(b->csock, ".", + NGM_GENERIC_COOKIE, NGM_MKPEER, &mp, sizeof(mp)) < 0) { + Log(LG_ERR, ("[%s] can't create %s node: %s", + b->name, NG_TEE_NODE_TYPE, strerror(errno))); + return(-1); + } + /* Connect tee and iface nodes */ + snprintf(cn.path, sizeof(cn.path), "%s:", ni->name); + snprintf(cn.ourhook, sizeof(cn.ourhook), "%s", NG_TEE_HOOK_LEFT); + snprintf(cn.peerhook, sizeof(cn.peerhook), "%s", NG_IFACE_HOOK_INET); + if (NgSendMsg(b->csock, TEMPHOOK, + NGM_GENERIC_COOKIE, NGM_CONNECT, &cn, sizeof(cn)) < 0) { + Log(LG_ERR, ("[%s] can't connect %s and %s: %s", + b->name, NG_TEE_HOOK_LEFT, NG_IFACE_HOOK_INET, strerror(errno))); + rtn = -1; + } /* Disconnect temporary hook */ snprintf(rm.ourhook, sizeof(rm.ourhook), "%s", TEMPHOOK); if (NgSendMsg(b->csock, ".", @@ -449,6 +496,7 @@ b->name, TEMPHOOK, strerror(errno))); rtn = -1; } + } /* Done */ return(rtn); @@ -473,7 +521,12 @@ char path[NG_PATHLEN + 1]; /* Get absolute path to bpf node */ + if (gEnableTee) { + snprintf(path, sizeof(path), "%s:%s.%s", b->iface.ifname, + NG_IFACE_HOOK_INET, NG_TEE_HOOK_RIGHT); + } else { snprintf(path, sizeof(path), "%s:%s", b->iface.ifname, NG_IFACE_HOOK_INET); + } /* First, configure the hook on the interface node side of the BPF node */ memset(&u, 0, sizeof(u)); @@ -590,10 +643,23 @@ Bund bund_save; Link lnk_save; int k; + struct ngm_rmhook rm; if (iface) { + if (gEnableTee) { + /* Disconnect tee:right hook */ + snprintf(path, sizeof(path), "%s:%s", + b->iface.ifname, NG_IFACE_HOOK_INET); + snprintf(rm.ourhook, sizeof(rm.ourhook), "%s", NG_TEE_HOOK_RIGHT); + if (NgSendMsg(b->csock, path, + NGM_GENERIC_COOKIE, NGM_RMHOOK, &rm, sizeof(rm)) < 0) { + Log(LG_ERR, ("[%s] can't remove hook %s: %s", + b->name, NG_TEE_HOOK_RIGHT, strerror(errno))); + } + } else { snprintf(path, sizeof(path), "%s:", b->iface.ifname); NgFuncShutdownNode(b, b->name, path); + } } lnk_save = lnk; bund_save = bund; diff -urNb mpd-3.18/src/ppp.h mpd-3.18.patched/src/ppp.h --- mpd-3.18/src/ppp.h Tue May 4 22:12:28 2004 +++ mpd-3.18.patched/src/ppp.h Fri Jan 21 16:49:54 2005 @@ -84,6 +84,7 @@ extern int gNumLinks; /* Total number of links */ extern int gNumBundles; /* Total number of bundles */ + extern int gEnableTee; /* Insert ng_tee into netgraph */ extern Bund bund; /* Current bundle */ extern Link lnk; /* Current link */