From dce80974dcd2114ebb4a3ed3060d4d636cbb9d1d Mon Sep 17 00:00:00 2001 From: Vadim Kochan Date: Mon, 20 Apr 2015 12:43:08 +0300 Subject: netsniff-ng: Delete rfmon mac80211 device in case of panic netsniff-ng does not delete created rfmon device in case of panic (for example - bad pcap filter expression), so added ability to add callback func when panic will be happen and delete rfmon device. Signed-off-by: Vadim Kochan Signed-off-by: Tobias Klauser --- astraceroute/Makefile | 1 + bpfc/Makefile | 1 + curvetun/Makefile | 1 + die.c | 31 +++++++++++++++++++++++++++++++ die.h | 5 +++++ flowtop/Makefile | 1 + ifpps/Makefile | 1 + mausezahn/Makefile | 2 ++ netsniff-ng.c | 26 +++++++++++++++++--------- netsniff-ng/Makefile | 1 + trafgen/Makefile | 1 + 11 files changed, 62 insertions(+), 9 deletions(-) create mode 100644 die.c diff --git a/astraceroute/Makefile b/astraceroute/Makefile index 6fd0b17..2e80a11 100644 --- a/astraceroute/Makefile +++ b/astraceroute/Makefile @@ -16,6 +16,7 @@ astraceroute-objs = xmalloc.o \ link.o \ dev.o \ ring.o \ + die.o \ astraceroute.o ifeq ($(CONFIG_GEOIP), 1) diff --git a/bpfc/Makefile b/bpfc/Makefile index 5c8b8c9..b8a3787 100644 --- a/bpfc/Makefile +++ b/bpfc/Makefile @@ -5,6 +5,7 @@ bpfc-objs = xmalloc.o \ bpf.o \ bpf_lexer.yy.o \ bpf_parser.tab.o \ + die.o \ bpfc.o bpfc-lex = bpf_lexer.yy.o diff --git a/curvetun/Makefile b/curvetun/Makefile index eeebd11..d9ae339 100644 --- a/curvetun/Makefile +++ b/curvetun/Makefile @@ -22,6 +22,7 @@ curvetun-objs = xmalloc.o \ ioexact.o \ ioops.o \ cpusched.o \ + die.o \ curvetun_mgmt_servers.o \ curvetun_mgmt_users.o \ curvetun_server.o \ diff --git a/die.c b/die.c new file mode 100644 index 0000000..4c0014d --- /dev/null +++ b/die.c @@ -0,0 +1,31 @@ +/* + * Subject to the GPL, version 2. + */ + +#include "xmalloc.h" + +struct panic_func { + void *arg; + void (*on_panic)(void *arg); + struct panic_func *next; +}; + +static struct panic_func *panic_funcs; + +void panic_func_add(void (*on_panic)(void *arg), void *arg) +{ + struct panic_func *handler = xmallocz(sizeof(*handler)); + + handler->arg = arg; + handler->on_panic = on_panic; + handler->next = panic_funcs; + panic_funcs = handler; +}; + +void call_on_panic_funcs(void) +{ + struct panic_func *it; + + for (it = panic_funcs; it; it = it->next) + it->on_panic(it->arg); +} diff --git a/die.h b/die.h index 919f3ae..0d709d0 100644 --- a/die.h +++ b/die.h @@ -12,6 +12,9 @@ #include "built_in.h" +extern void panic_func_add(void (*on_panic)(void *arg), void *arg); +extern void call_on_panic_funcs(void); + static inline void panic(const char *format, ...) __check_format_printf(1, 2); static inline void syslog_panic(const char *format, ...) __check_format_printf(1, 2); @@ -20,11 +23,13 @@ static inline void syslog_maybe(bool cond, int priority, static inline void __noreturn __die_hard(void) { + call_on_panic_funcs(); exit(EXIT_FAILURE); } static inline void __noreturn __die_harder(void) { + call_on_panic_funcs(); _exit(EXIT_FAILURE); } diff --git a/flowtop/Makefile b/flowtop/Makefile index 85acb43..41865c7 100644 --- a/flowtop/Makefile +++ b/flowtop/Makefile @@ -21,6 +21,7 @@ flowtop-objs = xmalloc.o \ lookup.o \ tprintf.o \ screen.o \ + die.o \ flowtop.o ifeq ($(CONFIG_GEOIP), 1) diff --git a/ifpps/Makefile b/ifpps/Makefile index 4d1a9b6..1625ea9 100644 --- a/ifpps/Makefile +++ b/ifpps/Makefile @@ -10,6 +10,7 @@ ifpps-objs = xmalloc.o \ dev.o \ sig.o \ screen.o \ + die.o \ ifpps.o ifpps-eflags = $(shell pkg-config --cflags ncurses 2> /dev/null) diff --git a/mausezahn/Makefile b/mausezahn/Makefile index 7943738..08918b5 100644 --- a/mausezahn/Makefile +++ b/mausezahn/Makefile @@ -6,6 +6,8 @@ mausezahn-libs = -lcli \ -lm mausezahn-objs = str.o \ + die.o \ + xmalloc.o \ staging/layer1.o \ staging/layer2.o \ staging/layer3.o \ diff --git a/netsniff-ng.c b/netsniff-ng.c index 2eafe31..c0d70c8 100644 --- a/netsniff-ng.c +++ b/netsniff-ng.c @@ -184,6 +184,20 @@ static inline bool dump_to_pcap(struct ctx *ctx) return ctx->dump; } +static void on_panic_del_rfmon(void *arg) +{ + leave_rfmon_mac80211(arg); +} + +static inline void setup_rfmon_mac80211_dev(struct ctx *ctx, char **rfmon_dev) +{ + ctx->device_trans = xstrdup(*rfmon_dev); + xfree(*rfmon_dev); + + enter_rfmon_mac80211(ctx->device_trans, rfmon_dev); + panic_func_add(on_panic_del_rfmon, *rfmon_dev); +} + static void pcap_to_xmit(struct ctx *ctx) { uint8_t *out = NULL; @@ -227,10 +241,8 @@ static void pcap_to_xmit(struct ctx *ctx) } if (ctx->rfraw) { - ctx->device_trans = xstrdup(ctx->device_out); - xfree(ctx->device_out); + setup_rfmon_mac80211_dev(ctx, &ctx->device_out); - enter_rfmon_mac80211(ctx->device_trans, &ctx->device_out); if (ctx->link_type != LINKTYPE_IEEE802_11 && ctx->link_type != LINKTYPE_IEEE802_11_RADIOTAP) panic("Wrong linktype of pcap!\n"); @@ -1464,12 +1476,8 @@ int main(int argc, char **argv) } if (device_mtu(ctx.device_in) || !strncmp("any", ctx.device_in, strlen(ctx.device_in))) { - if (ctx.rfraw) { - ctx.device_trans = xstrdup(ctx.device_in); - xfree(ctx.device_in); - - enter_rfmon_mac80211(ctx.device_trans, &ctx.device_in); - } + if (ctx.rfraw) + setup_rfmon_mac80211_dev(&ctx, &ctx.device_in); ctx.link_type = pcap_devtype_to_linktype(ctx.device_in); diff --git a/netsniff-ng/Makefile b/netsniff-ng/Makefile index bc5e032..63bf58f 100644 --- a/netsniff-ng/Makefile +++ b/netsniff-ng/Makefile @@ -63,6 +63,7 @@ netsniff-ng-objs = dissector.o \ tprintf.o \ timer.o \ mac80211.o \ + die.o \ netsniff-ng.o ifeq ($(CONFIG_LIBPCAP), 1) diff --git a/trafgen/Makefile b/trafgen/Makefile index d395b16..b37e62d 100644 --- a/trafgen/Makefile +++ b/trafgen/Makefile @@ -3,6 +3,7 @@ trafgen-libs = $(shell pkg-config --libs libnl-3.0) \ -lm trafgen-objs = xmalloc.o \ + die.o \ ioops.o \ privs.o \ proc.o \ -- cgit v1.2.3-54-g00ecf