/* * netsniff-ng - the packet sniffing beast * Copyright 2011 - 2013 Daniel Borkmann , * Swiss federal institute of technology (ETH Zurich) * Subject to the GPL, version 2. */ #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "xmalloc.h" #include "die.h" #include "str.h" #include "sig.h" #include "sock.h" #include "cpus.h" #include "lockme.h" #include "privs.h" #include "proc.h" #include "ioops.h" #include "irq.h" #include "config.h" #include "built_in.h" #include "trafgen_conf.h" #include "tprintf.h" #include "timer.h" #include "ring_tx.h" #include "csum.h" #include "trafgen_proto.h" #include "pcap_io.h" #include "trafgen_dev.h" enum shaper_type { SHAPER_NONE, SHAPER_DELAY, SHAPER_PKTS, SHAPER_BYTES, SHAPER_TSTAMP, }; struct shaper { enum shaper_type type; unsigned long long sent; unsigned long long rate; struct timeval tstamp; struct timespec delay; struct timeval start; struct timeval end; }; struct ctx { bool rand, rfraw, jumbo_support, verbose, smoke_test, enforce, qdisc_path; size_t reserve_size; struct dev_io *dev_out; struct dev_io *dev_in; unsigned long num; unsigned int cpus; uid_t uid; gid_t gid; char *device, *rhost; struct sockaddr_in dest; struct shaper sh; char *packet_str; char *pcap_in; }; struct cpu_stats { unsigned long tv_sec, tv_usec; unsigned long long tx_packets, tx_bytes; unsigned long long cf_packets, cf_bytes; unsigned long long cd_packets; sig_atomic_t state; }; static sig_atomic_t sigint = 0; struct packet *packets = NULL; size_t plen = 0; struct packet_dyn *packet_dyn = NULL; size_t dlen = 0; static const char *short_options = "d:c:n:t:vJhS:rk:i:o:VRs:P:eE:pu:g:CHQqD:b:"; static const struct option long_options[] = { {"dev", required_argument, NULL, 'd'}, {"out", required_argument, NULL, 'o'}, {"in", required_argument, NULL, 'i'}, {"conf", required_argument, NULL, 'c'}, {"num", required_argument, NULL, 'n'}, {"gap", required_argument, NULL, 't'}, {"rate", required_argument, NULL, 'b'}, {"cpus", required_argument, NULL, 'P'}, {"ring-size", required_argument, NULL, 'S'}, {"kernel-pull", required_argument, NULL, 'k'}, {"smoke-test", required_argument, NULL, 's'}, {"seed", required_argument, NULL, 'E'}, {"user", required_argument, NULL, 'u'}, {"group", required_argument, NULL, 'g'}, {"prio-high", no_argument, NULL, 'H'}, {"notouch-irq", no_argument, NULL, 'Q'}, {"no-sock-mem", no_argument, NULL, 'A'}, {"qdisc-path", no_argument, NULL, 'q'}, {"jumbo-support", no_argument, NULL, 'J'}, {"no-cpu-stats", no_argument, NULL, 'C'}, {"cpp", no_argument, NULL, 'p'}, {"define", required_argument, NULL, 'D'}, {"rfraw", no_argument, NULL, 'R'}, {"rand", no_argument, NULL, 'r'}, {"verbose", no_argument, NULL, 'V'}, {"version", no_argument, NULL, 'v'}, {"example", no_argument, NULL, 'e'}, {"help", no_argument, NULL, 'h'}, {NULL, 0, NULL, 0} }; static const char *copyright = "Please report bugs at https://github.com/netsniff-ng/netsniff-ng/issues\n" "Copyright (C) 2011-2013 Daniel Borkmann ,\n" "Swiss federal institute of technology (ETH Zurich)\n" "License: GNU GPL version 2.0\n" "This is free software: you are free to change and redistribute it.\n" "There is NO WARRANTY, to the extent permitted by law."; static struct cpu_stats *stats; static unsigned int seed; #define CPU_STATS_STATE_CFG 1 #define CPU_STATS_STATE_CHK 2 #define CPU_STATS_STATE_RES 4 #ifndef ICMP_FILTER # define ICMP_FILTER 1 struct icmp_filter { __u32 data; }; #endif #define SMOKE_N_PROBES 100 #define PKT_MIN_LEN 14 static void signal_handler(int number) { switch (number) { case SIGINT: case SIGQUIT: case SIGTERM: sigint = 1; case SIGHUP: default: break; } } static void __noreturn help(void) { printf("trafgen %s, multithreaded zero-copy network packet generator\n", VERSION_STRING); puts("http://www.netsniff-ng.org\n\n" "Usage: trafgen [options] [packet]\n" "Options:\n" " -i|-c|--in|--conf Packet configuration file/stdin\n" " -o|-d|--out|--dev Networking device or configuration file i.e., eth0\n" " -p|--cpp Run packet config through C preprocessor\n" " -D|--define Add macro/define for C preprocessor\n" " -J|--jumbo-support Support 64KB super jumbo frames (def: 2048B)\n" " -R|--rfraw Inject raw 802.11 frames\n" " -s|--smoke-test Probe if machine survived fuzz-tested packet\n" " -n|--num Number of packets until exit (def: 0)\n" " -r|--rand Randomize packet selection (def: round robin)\n" " -P|--cpus Specify number of forks(<= CPUs) (def: #CPUs)\n" " -t|--gap