[netsniff-ng] [PATCH] trafgen: Delegate creation of rfraw to dev_io API

2017-06-12 Thread Vadim Kochan
Simplify a bit of creation rfraw device by delegating it to the dev_io
API, also in case the output device is pcap file the --rfraw option
sets the link type to ieee80211 radio tap.

Signed-off-by: Vadim Kochan 
---
 trafgen.8 |  3 ++-
 trafgen.c | 25 +
 trafgen_dev.c | 36 +++-
 trafgen_dev.h |  4 +++-
 4 files changed, 49 insertions(+), 19 deletions(-)

diff --git a/trafgen.8 b/trafgen.8
index 50deacf..67aaaf9 100644
--- a/trafgen.8
+++ b/trafgen.8
@@ -98,7 +98,8 @@ a bigger memory footprint for the ring buffer.
 In case the output networking device is a wireless device, it is possible with
 trafgen to turn this into monitor mode and create a mon device that trafgen
 will be transmitting on instead of wlan, for instance. This enables trafgen
-to inject raw 802.11 frames.
+to inject raw 802.11 frames. In case if the output is a pcap file the link type
+is set to 127 (ieee80211 radio tap).
 .PP
 .SS -s , --smoke-test 
 In case this option is enabled, trafgen will perform a smoke test. In other
diff --git a/trafgen.c b/trafgen.c
index 6ae0076..3929734 100644
--- a/trafgen.c
+++ b/trafgen.c
@@ -45,7 +45,6 @@
 #include "lockme.h"
 #include "privs.h"
 #include "proc.h"
-#include "mac80211.h"
 #include "ioops.h"
 #include "irq.h"
 #include "config.h"
@@ -85,7 +84,7 @@ struct ctx {
unsigned long num;
unsigned int cpus;
uid_t uid; gid_t gid;
-   char *device, *device_trans, *rhost;
+   char *device, *rhost;
struct sockaddr_in dest;
struct shaper sh;
char *packet_str;
@@ -1022,7 +1021,7 @@ static unsigned int generate_srand_seed(void)
 
 static void on_panic_del_rfmon(void *arg)
 {
-   leave_rfmon_mac80211(arg);
+   dev_io_close(arg);
 }
 
 int main(int argc, char **argv)
@@ -1282,15 +1281,6 @@ int main(int argc, char **argv)
set_system_socket_memory(vals, array_size(vals));
xlockme();
 
-   if (ctx.rfraw) {
-   ctx.device_trans = xstrdup(ctx.device);
-   xfree(ctx.device);
-
-   enter_rfmon_mac80211(ctx.device_trans, );
-   panic_handler_add(on_panic_del_rfmon, ctx.device);
-   sleep(0);
-   }
-
if (ctx.pcap_in) {
ctx.dev_in = dev_io_open(ctx.pcap_in, DEV_IO_IN);
if (!ctx.dev_in)
@@ -1301,6 +1291,13 @@ int main(int argc, char **argv)
if (!ctx.dev_out)
panic("Failed to open output device\n");
 
+   if (ctx.rfraw) {
+   if (dev_io_link_type_set(ctx.dev_out, 
LINKTYPE_IEEE802_11_RADIOTAP))
+   panic("Failed to setup rfraw device\n");
+
+   panic_handler_add(on_panic_del_rfmon, ctx.dev_out);
+   }
+
protos_init(ctx.dev_out);
 
if (shaper_is_set() || (ctx.dev_in && dev_io_is_pcap(ctx.dev_in))
@@ -1356,9 +1353,6 @@ int main(int argc, char **argv)
die();
}
 
-   if (ctx.rfraw)
-   leave_rfmon_mac80211(ctx.device);
-
if (set_sock_mem)
reset_system_socket_memory(vals, array_size(vals));
 
@@ -1392,7 +1386,6 @@ thread_out:
 
argv_free(cpp_argv);
free(ctx.device);
-   free(ctx.device_trans);
free(ctx.rhost);
free(confname);
free(ctx.packet_str);
diff --git a/trafgen_dev.c b/trafgen_dev.c
index cd99a0c..46fb897 100644
--- a/trafgen_dev.c
+++ b/trafgen_dev.c
@@ -16,6 +16,8 @@
 #include "xmalloc.h"
 #include "pcap_io.h"
 #include "built_in.h"
+#include "mac80211.h"
+#include "linktype.h"
 #include "trafgen_dev.h"
 
 static int dev_pcap_open(struct dev_io *dev, const char *name, enum 
dev_io_mode_t mode)
@@ -164,9 +166,35 @@ static int dev_net_write(struct dev_io *dev, const uint8_t 
*buf, size_t len)
return sendto(dev->fd, buf, len, 0, (struct sockaddr *) , 
sizeof(saddr));
 }
 
+static int dev_net_set_link_type(struct dev_io *dev, int link_type)
+{
+   if (link_type != LINKTYPE_IEEE802_11 && link_type != 
LINKTYPE_IEEE802_11_RADIOTAP)
+   return 0;
+
+   dev->trans = xstrdup(dev->name);
+   xfree(dev->name);
+
+   enter_rfmon_mac80211(dev->trans, >name);
+   dev->ifindex = __device_ifindex(dev->name);
+   dev->dev_type = device_type(dev->name);
+   sleep(0);
+
+   return 0;
+}
+
+static void dev_net_close(struct dev_io *dev)
+{
+   if (dev->link_type == LINKTYPE_IEEE802_11 || dev->link_type == 
LINKTYPE_IEEE802_11_RADIOTAP)
+   leave_rfmon_mac80211(dev->name);
+
+   free(dev->trans);
+}
+
 static const struct dev_io_ops dev_net_ops = {
.open = dev_net_open,
.write = dev_net_write,
+   .set_link_type = dev_net_set_link_type,
+   .close = dev_net_close,
 };
 
 struct dev_io *dev_io_open(const char *name, enum dev_io_mode_t mode)
@@ -231,9 +259,15 @@ bool dev_io_is_pcap(struct dev_io *dev)
return dev->ops == _pcap_ops;
 }
 
-void 

[netsniff-ng] [PATCH v2] trafgen: Fix output pcap file name length trimming

2017-06-12 Thread Vadim Kochan
Trim output name to IFNAMSIZ only if the output is a networking device,
otherwise the following error occured if output name is greater then IFNAMSIZ:

~/src/netsniff-ng$ trafgen -n 1 '{ udp() }' -o /tmp/xx.pcap
No networking device or pcap file: /tmp/xx
Failed to open output device
---
v2:
Fixed stupid copy-paste of xtrsndup-ing device name

 trafgen.c | 2 +-
 trafgen_dev.c | 3 ++-
 2 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/trafgen.c b/trafgen.c
index 6ae0076..b40d362 100644
--- a/trafgen.c
+++ b/trafgen.c
@@ -1084,7 +1084,7 @@ int main(int argc, char **argv)
break;
case 'd':
case 'o':
-   ctx.device = xstrndup(optarg, IFNAMSIZ);
+   ctx.device = xstrdup(optarg);
break;
case 'H':
prio_high = true;
diff --git a/trafgen_dev.c b/trafgen_dev.c
index cd99a0c..80e7481 100644
--- a/trafgen_dev.c
+++ b/trafgen_dev.c
@@ -174,8 +174,10 @@ struct dev_io *dev_io_open(const char *name, enum 
dev_io_mode_t mode)
struct dev_io *dev = xzmalloc(sizeof(struct dev_io));
 
if (strstr(name, ".pcap")) {
+   dev->name = xstrdup(name);
dev->ops = _pcap_ops;
} else if (device_mtu(name) > 0) {
+   dev->name = xstrndup(name, IFNAMSIZ);
dev->ops = _net_ops;
} else {
fprintf(stderr, "No networking device or pcap file: %s\n", 
name);
@@ -189,7 +191,6 @@ struct dev_io *dev_io_open(const char *name, enum 
dev_io_mode_t mode)
}
}
 
-   dev->name = xstrdup(name);
return dev;
 };
 
-- 
2.9.3

-- 
You received this message because you are subscribed to the Google Groups 
"netsniff-ng" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to netsniff-ng+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[netsniff-ng] Re: [PATCH] trafgen: Fix output pcap file name length trimming

2017-06-12 Thread Vadim Kochan
On Mon, Jun 12, 2017 at 11:38 PM, Vadim Kochan  wrote:

> Trim output name to IFNAMSIZ only if the output is a networking device,
> otherwise the following error occured if output name is greater then
> IFNAMSIZ:
>
> ~/src/netsniff-ng$ trafgen -n 1 '{ udp() }' -o
> /tmp/xx.pcap
> No networking device or pcap file: /tmp/xx
> Failed to open output device
> ---
>  trafgen.c | 2 +-
>  trafgen_dev.c | 3 ++-
>  2 files changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/trafgen.c b/trafgen.c
> index 6ae0076..b40d362 100644
> --- a/trafgen.c
> +++ b/trafgen.c
> @@ -1084,7 +1084,7 @@ int main(int argc, char **argv)
> break;
> case 'd':
> case 'o':
> -   ctx.device = xstrndup(optarg, IFNAMSIZ);
> +   ctx.device = xstrdup(optarg);
> break;
> case 'H':
> prio_high = true;
> diff --git a/trafgen_dev.c b/trafgen_dev.c
> index cd99a0c..bbe1be8 100644
> --- a/trafgen_dev.c
> +++ b/trafgen_dev.c
> @@ -174,8 +174,10 @@ struct dev_io *dev_io_open(const char *name, enum
> dev_io_mode_t mode)
> struct dev_io *dev = xzmalloc(sizeof(struct dev_io));
>
> if (strstr(name, ".pcap")) {
> +   dev->name = xstrdup(name);
> dev->ops = _pcap_ops;
> } else if (device_mtu(name) > 0) {
> +   dev->name = xstrndup(optarg, IFNAMSIZ);
>
> Ops, sorry ... too late for fixing ...


> dev->ops = _net_ops;
> } else {
> fprintf(stderr, "No networking device or pcap file: %s\n",
> name);
> @@ -189,7 +191,6 @@ struct dev_io *dev_io_open(const char *name, enum
> dev_io_mode_t mode)
> }
> }
>
> -   dev->name = xstrdup(name);
> return dev;
>  };
>
> --
> 2.9.3
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"netsniff-ng" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to netsniff-ng+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[netsniff-ng] [PATCH] trafgen: Fix output pcap file name length trimming

2017-06-12 Thread Vadim Kochan
Trim output name to IFNAMSIZ only if the output is a networking device,
otherwise the following error occured if output name is greater then IFNAMSIZ:

~/src/netsniff-ng$ trafgen -n 1 '{ udp() }' -o /tmp/xx.pcap
No networking device or pcap file: /tmp/xx
Failed to open output device
---
 trafgen.c | 2 +-
 trafgen_dev.c | 3 ++-
 2 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/trafgen.c b/trafgen.c
index 6ae0076..b40d362 100644
--- a/trafgen.c
+++ b/trafgen.c
@@ -1084,7 +1084,7 @@ int main(int argc, char **argv)
break;
case 'd':
case 'o':
-   ctx.device = xstrndup(optarg, IFNAMSIZ);
+   ctx.device = xstrdup(optarg);
break;
case 'H':
prio_high = true;
diff --git a/trafgen_dev.c b/trafgen_dev.c
index cd99a0c..bbe1be8 100644
--- a/trafgen_dev.c
+++ b/trafgen_dev.c
@@ -174,8 +174,10 @@ struct dev_io *dev_io_open(const char *name, enum 
dev_io_mode_t mode)
struct dev_io *dev = xzmalloc(sizeof(struct dev_io));
 
if (strstr(name, ".pcap")) {
+   dev->name = xstrdup(name);
dev->ops = _pcap_ops;
} else if (device_mtu(name) > 0) {
+   dev->name = xstrndup(optarg, IFNAMSIZ);
dev->ops = _net_ops;
} else {
fprintf(stderr, "No networking device or pcap file: %s\n", 
name);
@@ -189,7 +191,6 @@ struct dev_io *dev_io_open(const char *name, enum 
dev_io_mode_t mode)
}
}
 
-   dev->name = xstrdup(name);
return dev;
 };
 
-- 
2.9.3

-- 
You received this message because you are subscribed to the Google Groups 
"netsniff-ng" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to netsniff-ng+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.