netsniff-ng: trafgen: reset rmem/wmem on exit
[netsniff-ng.git] / src / netsniff-ng.c
blob41eb72a36f4b7bb00e2391c64e25129bda8b2fbc
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2009-2011 Daniel Borkmann.
5 * Copyright 2010 Emmanuel Roullit.
6 * Subject to the GPL, version 2.
8 * The first sniffer that invoked both, the zero-copy RX_RING as well as
9 * the zero-copy TX_RING for high-performance network I/O and scatter/gather
10 * or mmaped PCAP I/O.
12 * "I knew that danger lay ahead, of course; but I did not expect to
13 * meet it in our own Shire. Can't a hobbit walk from the Water to the
14 * River in peace?" "But it is not your own Shire," said Gildor. "Others
15 * dwelt here before hobbits were; and others will dwell here again when
16 * hobbits are no more. The wide world is all about you: you can fence
17 * yourselves in, but you cannot for ever fence it out."
19 * -- The Lord of the Rings, Gildor to Frodo,
20 * Chapter 'Three is Company'.
25 =head1 NAME
27 netsniff-ng - the packet sniffing beast
29 =head1 SYNOPSIS
31 netsniff-ng -i|-d|--dev|--in <dev|pcap> -o|--out <dev|pcap|dir|txf>
32 [-f|--filter <bpf-file>][-t|--type <type>][-F|--interval <uint>]
33 [-s|--silent][-J|--jumbo-support][-n|--num <uint>][-r|--rand]
34 [-M|--no-promisc][-m|--mmap | -c|--clrw][-S|--ring-size <size>]
35 [-k|--kernel-pull <uint>][-b|--bind-cpu <cpu> | -B|--unbind-cpu <cpu>]
36 [-H|--prio-high][-Q|--notouch-irq][-q|--less | -X|--hex | -l|--ascii]
37 [-v|--version][-h|--help]
39 =head1 DESCRIPTION
41 The first sniffer that invoked both, the zero-copy RX_RING as well as
42 the zero-copy TX_RING for high-performance network I/O and scatter/gather
43 or mmaped PCAP I/O.
45 =head1 EXAMPLES
47 =over
49 =item netsniff-ng --in eth0 --out dump.pcap
51 Capture traffic from interface 'eth0' and save it pcap file 'dump.pcap'
53 =item netsniff-ng --in any --filter http.bpf --payload
55 Capture HTTP traffic from any interface and print its payload on stdout
57 =item netsniff-ng --in wlan0 --bind-cpu 0,1
59 Capture all traffic from wlan0 interface.
60 Schedule process on CPU 0 and 1.
62 =back
64 =head1 OPTIONS
66 =over
68 =item -i|-d|--dev|--in <dev|pcap>
70 Input source. Can be a network device or pcap file.
72 =item -o|--out <dev|pcap|dir|txf>
74 Output sink. Can be a network device, pcap file, a trafgen txf file or a
75 directory. (There's only pcap to txf translation possible.)
77 =item -f|--filter <bpf-file>
79 Use BPF filter file from bpfc.
81 =item -t|--type <type>
83 =over
85 =item Only handle packets of defined type:
87 =over
89 =item - broadcast
91 =item - multicast
93 =item - others
95 =item - outgoing
97 =back
99 =back
101 =item -F|--interval <uint>
103 Dump interval in seconds. if -o is a directory, a new pcap will be created at each interval.
104 The older files are left untouched. (default value: 60 seconds)
106 =item -s|--silent
108 Do not print captured packets to stdout.
110 =item -J|--jumbo-support
112 Support for 64KB Super Jumbo Frames.
114 =item -n|--num <uint>
116 When zerp, capture/replay until SIGINT is received (default).
117 When non-zero, capture/replay the number of packets.
119 =item -r|--rand
121 Randomize packet forwarding order (replay mode only).
123 =item -M|--no-promisc
125 Do not place the interface in promiscuous mode.
127 =item -m|--mmap
129 Mmap pcap file i.e., for replaying. Default: scatter/gather I/O.
131 =item -c|--clrw
133 Instead of using scatter/gather I/O use slower read(2)/write(2) I/O.
135 =item -S|--ring-size <size>
137 Manually set ring size in KB/MB/GB, e.g. '10MB'.
139 =item -k|--kernel-pull <uint>
141 Kernel pull from user interval in microseconds. Default is 10us. (replay mode only).
143 =item -b|--bind-cpu <cpu>
145 Bind to specific CPU (or CPU-range).
147 =item -B|--unbind-cpu <cpu>
149 Forbid to use specific CPU (or CPU-range).
151 =item -H|--prio-high
153 Run the process in high-priority mode.
155 =item -Q|--notouch-irq
157 Do not touch IRQ CPU affinity of NIC.
159 =item -q|--less
161 Print less-verbose packet information.
163 =item -X|--hex
165 Print packet data in hex format.
167 =item -l|--ascii
169 Print human-readable packet data.
171 =item -v|--version
173 Print version.
175 =item -h|--help
177 Print help text and lists all options.
179 =back
181 =head1 AUTHOR
183 Written by Daniel Borkmann <daniel@netsniff-ng.org> and Emmanuel Roullit <emmanuel@netsniff-ng.org>
185 =head1 DOCUMENTATION
187 Documentation by Emmanuel Roullit <emmanuel@netsniff-ng.org>
189 =head1 BUGS
191 Please report bugs to <bugs@netsniff-ng.org>
193 =cut
197 #define _GNU_SOURCE
198 #include <stdio.h>
199 #include <stdlib.h>
200 #include <signal.h>
201 #include <getopt.h>
202 #include <ctype.h>
203 #include <time.h>
204 #include <string.h>
205 #include <sys/socket.h>
206 #include <sys/types.h>
207 #include <sys/stat.h>
208 #include <sys/time.h>
209 #include <unistd.h>
210 #include <stdbool.h>
211 #include <pthread.h>
212 #include <fcntl.h>
214 #include "ring_rx.h"
215 #include "ring_tx.h"
216 #include "mac80211.h"
217 #include "xutils.h"
218 #include "built_in.h"
219 #include "pcap.h"
220 #include "bpf.h"
221 #include "xio.h"
222 #include "die.h"
223 #include "tprintf.h"
224 #include "dissector.h"
225 #include "xmalloc.h"
226 #include "mtrand.h"
228 #define CPU_UNKNOWN -1
229 #define CPU_NOTOUCH -2
230 #define PACKET_ALL -1
231 #define DUMP_INTERVAL 60
233 struct mode {
234 char *device_in;
235 char *device_out;
236 char *device_trans;
237 char *filter;
238 int cpu;
239 int rfraw;
240 int dump;
241 uint32_t link_type;
242 int print_mode;
243 unsigned int reserve_size;
244 int packet_type;
245 bool randomize;
246 bool promiscuous;
247 enum pcap_ops_groups pcap;
248 unsigned long kpull;
249 int jumbo_support;
250 int dump_dir;
251 unsigned long dump_interval;
254 struct tx_stats {
255 unsigned long tx_bytes;
256 unsigned long tx_packets;
259 volatile sig_atomic_t sigint = 0;
261 static int tx_sock;
262 static unsigned long frame_cnt_max = 0;
263 static unsigned long interval = TX_KERNEL_PULL_INT;
264 static struct itimerval itimer;
265 static volatile bool next_dump = false;
267 static const char *short_options = "d:i:o:rf:MJt:S:k:n:b:B:HQmcsqXlvhF:RgA";
269 static struct option long_options[] = {
270 {"dev", required_argument, 0, 'd'},
271 {"in", required_argument, 0, 'i'},
272 {"out", required_argument, 0, 'o'},
273 {"rand", no_argument, 0, 'r'},
274 {"rfraw", no_argument, 0, 'R'},
275 {"mmap", no_argument, 0, 'm'},
276 {"sg", no_argument, 0, 'g'},
277 {"clrw", no_argument, 0, 'c'},
278 {"jumbo-support", no_argument, 0, 'J'},
279 {"filter", required_argument, 0, 'f'},
280 {"no-promisc", no_argument, 0, 'M'},
281 {"num", required_argument, 0, 'n'},
282 {"type", required_argument, 0, 't'},
283 {"interval", required_argument, 0, 'F'},
284 {"ring-size", required_argument, 0, 'S'},
285 {"kernel-pull", required_argument, 0, 'k'},
286 {"bind-cpu", required_argument, 0, 'b'},
287 {"unbind-cpu", required_argument, 0, 'B'},
288 {"prio-high", no_argument, 0, 'H'},
289 {"notouch-irq", no_argument, 0, 'Q'},
290 {"silent", no_argument, 0, 's'},
291 {"less", no_argument, 0, 'q'},
292 {"hex", no_argument, 0, 'X'},
293 {"ascii", no_argument, 0, 'l'},
294 {"no-sock-mem", no_argument, 0, 'A'},
295 {"version", no_argument, 0, 'v'},
296 {"help", no_argument, 0, 'h'},
297 {0, 0, 0, 0}
300 static void signal_handler(int number)
302 switch (number) {
303 case SIGINT:
304 sigint = 1;
305 break;
306 case SIGHUP:
307 break;
308 default:
309 break;
313 static void timer_elapsed(int number)
315 itimer.it_interval.tv_sec = 0;
316 itimer.it_interval.tv_usec = interval;
317 itimer.it_value.tv_sec = 0;
318 itimer.it_value.tv_usec = interval;
320 pull_and_flush_tx_ring(tx_sock);
321 setitimer(ITIMER_REAL, &itimer, NULL);
324 static void timer_next_dump(int number)
326 itimer.it_interval.tv_sec = interval;
327 itimer.it_interval.tv_usec = 0;
328 itimer.it_value.tv_sec = interval;
329 itimer.it_value.tv_usec = 0;
331 next_dump = true;
332 setitimer(ITIMER_REAL, &itimer, NULL);
335 static void enter_mode_pcap_to_tx(struct mode *mode)
337 int irq, ifindex, fd = 0, ret;
338 unsigned int size, it = 0;
339 struct ring tx_ring;
340 struct frame_map *hdr;
341 struct sock_fprog bpf_ops;
342 struct tx_stats stats;
343 uint8_t *out = NULL;
344 unsigned long trunced = 0;
345 struct timeval start, end, diff;
347 if (!device_up_and_running(mode->device_out))
348 panic("Device not up and running!\n");
350 tx_sock = pf_socket();
352 if (!pcap_ops[mode->pcap])
353 panic("pcap group not supported!\n");
354 fd = open_or_die(mode->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
355 ret = pcap_ops[mode->pcap]->pull_file_header(fd, &mode->link_type);
356 if (ret)
357 panic("error reading pcap header!\n");
358 if (pcap_ops[mode->pcap]->prepare_reading_pcap) {
359 ret = pcap_ops[mode->pcap]->prepare_reading_pcap(fd);
360 if (ret)
361 panic("error prepare reading pcap!\n");
364 fmemset(&tx_ring, 0, sizeof(tx_ring));
365 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
366 fmemset(&stats, 0, sizeof(stats));
368 if (mode->rfraw) {
369 mode->device_trans = xstrdup(mode->device_out);
370 xfree(mode->device_out);
372 enter_rfmon_mac80211(mode->device_trans, &mode->device_out);
373 if (mode->link_type != LINKTYPE_IEEE802_11)
374 panic("Wrong linktype of pcap!\n");
377 ifindex = device_ifindex(mode->device_out);
378 size = ring_size(mode->device_out, mode->reserve_size);
380 bpf_parse_rules(mode->filter, &bpf_ops);
382 set_packet_loss_discard(tx_sock);
383 set_sockopt_hwtimestamp(tx_sock, mode->device_out);
384 setup_tx_ring_layout(tx_sock, &tx_ring, size, mode->jumbo_support);
385 create_tx_ring(tx_sock, &tx_ring);
386 mmap_tx_ring(tx_sock, &tx_ring);
387 alloc_tx_ring_frames(&tx_ring);
388 bind_tx_ring(tx_sock, &tx_ring, ifindex);
390 dissector_init_all(mode->print_mode);
392 if (mode->cpu >= 0 && ifindex > 0) {
393 irq = device_irq_number(mode->device_out);
394 device_bind_irq_to_cpu(mode->cpu, irq);
395 printf("IRQ: %s:%d > CPU%d\n", mode->device_out, irq,
396 mode->cpu);
399 if (mode->kpull)
400 interval = mode->kpull;
402 itimer.it_interval.tv_sec = 0;
403 itimer.it_interval.tv_usec = interval;
404 itimer.it_value.tv_sec = 0;
405 itimer.it_value.tv_usec = interval;
406 setitimer(ITIMER_REAL, &itimer, NULL);
408 printf("BPF:\n");
409 bpf_dump_all(&bpf_ops);
410 printf("MD: TX %luus %s ", interval, pcap_ops[mode->pcap]->name);
411 if (mode->rfraw)
412 printf("802.11 raw via %s ", mode->device_out);
413 #ifdef _LARGEFILE64_SOURCE
414 printf("lf64 ");
415 #endif
416 ioprio_print();
417 printf("\n");
419 gettimeofday(&start, NULL);
421 while (likely(sigint == 0)) {
422 while (user_may_pull_from_tx(tx_ring.frames[it].iov_base)) {
423 struct pcap_pkthdr phdr;
424 hdr = tx_ring.frames[it].iov_base;
425 /* Kernel assumes: data = ph.raw + po->tp_hdrlen -
426 * sizeof(struct sockaddr_ll); */
427 out = ((uint8_t *) hdr) + TPACKET_HDRLEN -
428 sizeof(struct sockaddr_ll);
430 do {
431 memset(&phdr, 0, sizeof(phdr));
432 ret = pcap_ops[mode->pcap]->read_pcap_pkt(fd, &phdr,
433 out, ring_frame_size(&tx_ring));
434 if (unlikely(ret <= 0))
435 goto out;
436 if (ring_frame_size(&tx_ring) < phdr.len) {
437 phdr.len = ring_frame_size(&tx_ring);
438 trunced++;
440 } while (mode->filter && !bpf_run_filter(&bpf_ops, out, phdr.len));
441 pcap_pkthdr_to_tpacket_hdr(&phdr, &hdr->tp_h);
443 stats.tx_bytes += hdr->tp_h.tp_len;;
444 stats.tx_packets++;
446 show_frame_hdr(hdr, mode->print_mode, RING_MODE_EGRESS);
447 dissector_entry_point(out, hdr->tp_h.tp_snaplen,
448 mode->link_type, mode->print_mode);
450 kernel_may_pull_from_tx(&hdr->tp_h);
451 next_slot_prewr(&it, &tx_ring);
453 if (unlikely(sigint == 1))
454 break;
455 if (frame_cnt_max != 0 &&
456 stats.tx_packets >= frame_cnt_max) {
457 sigint = 1;
458 break;
462 out:
463 gettimeofday(&end, NULL);
464 diff = tv_subtract(end, start);
466 fflush(stdout);
467 printf("\n");
468 printf("\r%12lu frames outgoing\n", stats.tx_packets);
469 printf("\r%12lu frames truncated (larger than frame)\n", trunced);
470 printf("\r%12lu bytes outgoing\n", stats.tx_bytes);
471 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
473 bpf_release(&bpf_ops);
474 dissector_cleanup_all();
475 destroy_tx_ring(tx_sock, &tx_ring);
477 if (mode->rfraw)
478 leave_rfmon_mac80211(mode->device_trans, mode->device_out);
480 close(tx_sock);
481 if (pcap_ops[mode->pcap]->prepare_close_pcap)
482 pcap_ops[mode->pcap]->prepare_close_pcap(fd, PCAP_MODE_READ);
483 close(fd);
486 static void enter_mode_rx_to_tx(struct mode *mode)
488 int rx_sock, ifindex_in, ifindex_out;
489 unsigned int size_in, size_out, it_in = 0, it_out = 0;
490 unsigned long fcnt = 0;
491 uint8_t *in, *out;
492 short ifflags = 0;
493 struct frame_map *hdr_in, *hdr_out;
494 struct ring tx_ring;
495 struct ring rx_ring;
496 struct pollfd rx_poll;
497 struct sock_fprog bpf_ops;
499 if (!strncmp(mode->device_in, mode->device_out,
500 strlen(mode->device_in)))
501 panic("Ingress/egress devices must be different!\n");
502 if (!device_up_and_running(mode->device_out))
503 panic("Egress device not up and running!\n");
504 if (!device_up_and_running(mode->device_in))
505 panic("Ingress device not up and running!\n");
507 rx_sock = pf_socket();
508 tx_sock = pf_socket();
510 fmemset(&tx_ring, 0, sizeof(tx_ring));
511 fmemset(&rx_ring, 0, sizeof(rx_ring));
512 fmemset(&rx_poll, 0, sizeof(rx_poll));
513 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
515 ifindex_in = device_ifindex(mode->device_in);
516 size_in = ring_size(mode->device_in, mode->reserve_size);
518 ifindex_out = device_ifindex(mode->device_out);
519 size_out = ring_size(mode->device_out, mode->reserve_size);
521 enable_kernel_bpf_jit_compiler();
522 bpf_parse_rules(mode->filter, &bpf_ops);
523 bpf_attach_to_sock(rx_sock, &bpf_ops);
525 setup_rx_ring_layout(rx_sock, &rx_ring, size_in, mode->jumbo_support);
526 create_rx_ring(rx_sock, &rx_ring);
527 mmap_rx_ring(rx_sock, &rx_ring);
528 alloc_rx_ring_frames(&rx_ring);
529 bind_rx_ring(rx_sock, &rx_ring, ifindex_in);
530 prepare_polling(rx_sock, &rx_poll);
532 set_packet_loss_discard(tx_sock);
533 setup_tx_ring_layout(tx_sock, &tx_ring, size_out, mode->jumbo_support);
534 create_tx_ring(tx_sock, &tx_ring);
535 mmap_tx_ring(tx_sock, &tx_ring);
536 alloc_tx_ring_frames(&tx_ring);
537 bind_tx_ring(tx_sock, &tx_ring, ifindex_out);
539 mt_init_by_seed_time();
540 dissector_init_all(mode->print_mode);
542 if (mode->promiscuous == true) {
543 ifflags = enter_promiscuous_mode(mode->device_in);
544 printf("PROMISC\n");
547 if (mode->kpull)
548 interval = mode->kpull;
550 itimer.it_interval.tv_sec = 0;
551 itimer.it_interval.tv_usec = interval;
552 itimer.it_value.tv_sec = 0;
553 itimer.it_value.tv_usec = interval;
554 setitimer(ITIMER_REAL, &itimer, NULL);
556 printf("BPF:\n");
557 bpf_dump_all(&bpf_ops);
558 printf("MD: RXTX %luus\n\n", interval);
559 printf("Running! Hang up with ^C!\n\n");
561 while (likely(sigint == 0)) {
562 while (user_may_pull_from_rx(rx_ring.frames[it_in].iov_base)) {
563 hdr_in = rx_ring.frames[it_in].iov_base;
564 in = ((uint8_t *) hdr_in) + hdr_in->tp_h.tp_mac;
565 fcnt++;
566 if (mode->packet_type != PACKET_ALL)
567 if (mode->packet_type != hdr_in->s_ll.sll_pkttype)
568 goto next;
570 hdr_out = tx_ring.frames[it_out].iov_base;
571 out = ((uint8_t *) hdr_out) + TPACKET_HDRLEN -
572 sizeof(struct sockaddr_ll);
574 for (; !user_may_pull_from_tx(tx_ring.frames[it_out].iov_base) &&
575 likely(!sigint);) {
576 if (mode->randomize)
577 next_rnd_slot(&it_out, &tx_ring);
578 else
579 next_slot(&it_out, &tx_ring);
580 hdr_out = tx_ring.frames[it_out].iov_base;
581 out = ((uint8_t *) hdr_out) + TPACKET_HDRLEN -
582 sizeof(struct sockaddr_ll);
585 tpacket_hdr_clone(&hdr_out->tp_h, &hdr_in->tp_h);
586 fmemcpy(out, in, hdr_in->tp_h.tp_len);
588 kernel_may_pull_from_tx(&hdr_out->tp_h);
589 if (mode->randomize)
590 next_rnd_slot(&it_out, &tx_ring);
591 else
592 next_slot(&it_out, &tx_ring);
594 show_frame_hdr(hdr_in, mode->print_mode, RING_MODE_INGRESS);
595 dissector_entry_point(in, hdr_in->tp_h.tp_snaplen,
596 mode->link_type, mode->print_mode);
598 if (frame_cnt_max != 0 && fcnt >= frame_cnt_max) {
599 sigint = 1;
600 break;
602 next:
603 kernel_may_pull_from_rx(&hdr_in->tp_h);
604 next_slot(&it_in, &rx_ring);
606 if (unlikely(sigint == 1))
607 goto out;
610 poll(&rx_poll, 1, -1);
611 poll_error_maybe_die(rx_sock, &rx_poll);
613 out:
614 sock_print_net_stats(rx_sock, 0);
616 bpf_release(&bpf_ops);
617 dissector_cleanup_all();
618 destroy_tx_ring(tx_sock, &tx_ring);
619 destroy_rx_ring(rx_sock, &rx_ring);
621 if (mode->promiscuous == true)
622 leave_promiscuous_mode(mode->device_in, ifflags);
624 close(tx_sock);
625 close(rx_sock);
628 static void enter_mode_read_pcap(struct mode *mode)
630 int ret, fd, fdo = 0;
631 struct pcap_pkthdr phdr;
632 struct sock_fprog bpf_ops;
633 struct tx_stats stats;
634 struct frame_map fm;
635 uint8_t *out;
636 size_t out_len;
637 unsigned long trunced = 0;
638 struct timeval start, end, diff;
640 if (!pcap_ops[mode->pcap])
641 panic("pcap group not supported!\n");
642 fd = open_or_die(mode->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
643 ret = pcap_ops[mode->pcap]->pull_file_header(fd, &mode->link_type);
644 if (ret)
645 panic("error reading pcap header!\n");
646 if (pcap_ops[mode->pcap]->prepare_reading_pcap) {
647 ret = pcap_ops[mode->pcap]->prepare_reading_pcap(fd);
648 if (ret)
649 panic("error prepare reading pcap!\n");
652 fmemset(&fm, 0, sizeof(fm));
653 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
654 fmemset(&stats, 0, sizeof(stats));
656 bpf_parse_rules(mode->filter, &bpf_ops);
657 dissector_init_all(mode->print_mode);
659 out_len = 64 * 1024;
660 out = xmalloc_aligned(out_len, CO_CACHE_LINE_SIZE);
662 printf("BPF:\n");
663 bpf_dump_all(&bpf_ops);
664 printf("MD: RD %s ", pcap_ops[mode->pcap]->name);
665 #ifdef _LARGEFILE64_SOURCE
666 printf("lf64 ");
667 #endif
668 ioprio_print();
669 printf("\n");
671 if (mode->device_out) {
672 fdo = open_or_die_m(mode->device_out, O_RDWR | O_CREAT |
673 O_TRUNC | O_LARGEFILE, DEFFILEMODE);
676 gettimeofday(&start, NULL);
678 while (likely(sigint == 0)) {
679 do {
680 memset(&phdr, 0, sizeof(phdr));
681 ret = pcap_ops[mode->pcap]->read_pcap_pkt(fd, &phdr,
682 out, out_len);
683 if (unlikely(ret < 0))
684 goto out;
685 if (unlikely(phdr.len == 0)) {
686 trunced++;
687 continue;
689 if (unlikely(phdr.len > out_len)) {
690 phdr.len = out_len;
691 trunced++;
693 } while (mode->filter &&
694 !bpf_run_filter(&bpf_ops, out, phdr.len));
696 pcap_pkthdr_to_tpacket_hdr(&phdr, &fm.tp_h);
698 stats.tx_bytes += fm.tp_h.tp_len;
699 stats.tx_packets++;
701 show_frame_hdr(&fm, mode->print_mode, RING_MODE_EGRESS);
702 dissector_entry_point(out, fm.tp_h.tp_snaplen,
703 mode->link_type, mode->print_mode);
705 if (mode->device_out) {
706 int i = 0;
707 char bout[80];
708 slprintf(bout, sizeof(bout), "{\n ");
709 write_or_die(fdo, bout, strlen(bout));
711 while (i < fm.tp_h.tp_snaplen) {
712 slprintf(bout, sizeof(bout), "0x%02x, ", out[i]);
713 write_or_die(fdo, bout, strlen(bout));
714 i++;
715 if (i % 10 == 0) {
716 slprintf(bout, sizeof(bout), "\n", out[i]);
717 write_or_die(fdo, bout, strlen(bout));
718 if (i < fm.tp_h.tp_snaplen) {
719 slprintf(bout, sizeof(bout), " ", out[i]);
720 write_or_die(fdo, bout, strlen(bout));
724 if (i % 10 != 0) {
725 slprintf(bout, sizeof(bout), "\n");
726 write_or_die(fdo, bout, strlen(bout));
728 slprintf(bout, sizeof(bout), "}\n\n");
729 write_or_die(fdo, bout, strlen(bout));
732 if (frame_cnt_max != 0 &&
733 stats.tx_packets >= frame_cnt_max) {
734 sigint = 1;
735 break;
738 out:
739 gettimeofday(&end, NULL);
740 diff = tv_subtract(end, start);
742 fflush(stdout);
743 printf("\n");
744 printf("\r%12lu frames outgoing\n", stats.tx_packets);
745 printf("\r%12lu frames truncated (larger than mtu)\n", trunced);
746 printf("\r%12lu bytes outgoing\n", stats.tx_bytes);
747 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
749 xfree(out);
751 bpf_release(&bpf_ops);
752 dissector_cleanup_all();
753 if (pcap_ops[mode->pcap]->prepare_close_pcap)
754 pcap_ops[mode->pcap]->prepare_close_pcap(fd, PCAP_MODE_READ);
755 close(fd);
757 if (mode->device_out)
758 close(fdo);
761 static void finish_multi_pcap_file(struct mode *mode, int fd)
763 pcap_ops[mode->pcap]->fsync_pcap(fd);
764 if (pcap_ops[mode->pcap]->prepare_close_pcap)
765 pcap_ops[mode->pcap]->prepare_close_pcap(fd, PCAP_MODE_WRITE);
766 close(fd);
768 fmemset(&itimer, 0, sizeof(itimer));
769 setitimer(ITIMER_REAL, &itimer, NULL);
772 static int next_multi_pcap_file(struct mode *mode, int fd)
774 int ret;
775 char tmp[512];
777 pcap_ops[mode->pcap]->fsync_pcap(fd);
778 if (pcap_ops[mode->pcap]->prepare_close_pcap)
779 pcap_ops[mode->pcap]->prepare_close_pcap(fd, PCAP_MODE_WRITE);
780 close(fd);
782 slprintf(tmp, sizeof(tmp), "%s/%lu.pcap", mode->device_out, time(0));
784 fd = open_or_die_m(tmp, O_RDWR | O_CREAT | O_TRUNC | O_LARGEFILE,
785 DEFFILEMODE);
786 ret = pcap_ops[mode->pcap]->push_file_header(fd, mode->link_type);
787 if (ret)
788 panic("error writing pcap header!\n");
789 if (pcap_ops[mode->pcap]->prepare_writing_pcap) {
790 ret = pcap_ops[mode->pcap]->prepare_writing_pcap(fd);
791 if (ret)
792 panic("error prepare writing pcap!\n");
795 return fd;
798 static int begin_multi_pcap_file(struct mode *mode)
800 int fd, ret;
801 char tmp[512];
803 if (!pcap_ops[mode->pcap])
804 panic("pcap group not supported!\n");
805 if (mode->device_out[strlen(mode->device_out) - 1] == '/')
806 mode->device_out[strlen(mode->device_out) - 1] = 0;
808 slprintf(tmp, sizeof(tmp), "%s/%lu.pcap", mode->device_out, time(0));
810 fd = open_or_die_m(tmp, O_RDWR | O_CREAT | O_TRUNC | O_LARGEFILE,
811 DEFFILEMODE);
812 ret = pcap_ops[mode->pcap]->push_file_header(fd, mode->link_type);
813 if (ret)
814 panic("error writing pcap header!\n");
815 if (pcap_ops[mode->pcap]->prepare_writing_pcap) {
816 ret = pcap_ops[mode->pcap]->prepare_writing_pcap(fd);
817 if (ret)
818 panic("error prepare writing pcap!\n");
821 interval = mode->dump_interval;
822 itimer.it_interval.tv_sec = interval;
823 itimer.it_interval.tv_usec = 0;
824 itimer.it_value.tv_sec = interval;
825 itimer.it_value.tv_usec = 0;
826 setitimer(ITIMER_REAL, &itimer, NULL);
828 return fd;
831 static void finish_single_pcap_file(struct mode *mode, int fd)
833 pcap_ops[mode->pcap]->fsync_pcap(fd);
834 if (pcap_ops[mode->pcap]->prepare_close_pcap)
835 pcap_ops[mode->pcap]->prepare_close_pcap(fd, PCAP_MODE_WRITE);
836 close(fd);
839 static int begin_single_pcap_file(struct mode *mode)
841 int fd, ret;
843 if (!pcap_ops[mode->pcap])
844 panic("pcap group not supported!\n");
845 fd = open_or_die_m(mode->device_out,
846 O_RDWR | O_CREAT | O_TRUNC | O_LARGEFILE,
847 DEFFILEMODE);
848 ret = pcap_ops[mode->pcap]->push_file_header(fd, mode->link_type);
849 if (ret)
850 panic("error writing pcap header!\n");
851 if (pcap_ops[mode->pcap]->prepare_writing_pcap) {
852 ret = pcap_ops[mode->pcap]->prepare_writing_pcap(fd);
853 if (ret)
854 panic("error prepare writing pcap!\n");
857 return fd;
860 static void enter_mode_rx_only_or_dump(struct mode *mode)
862 int sock, irq, ifindex, fd = 0, ret;
863 unsigned int size, it = 0;
864 unsigned long fcnt = 0, skipped = 0;
865 short ifflags = 0;
866 uint8_t *packet;
867 struct ring rx_ring;
868 struct pollfd rx_poll;
869 struct frame_map *hdr;
870 struct sock_fprog bpf_ops;
871 struct timeval start, end, diff;
873 if (!device_up_and_running(mode->device_in))
874 panic("Device not up and running!\n");
876 sock = pf_socket();
878 if (mode->rfraw) {
879 mode->device_trans = xstrdup(mode->device_in);
880 xfree(mode->device_in);
882 enter_rfmon_mac80211(mode->device_trans, &mode->device_in);
883 mode->link_type = LINKTYPE_IEEE802_11;
886 if (mode->dump) {
887 struct stat tmp;
888 fmemset(&tmp, 0, sizeof(tmp));
889 ret = stat(mode->device_out, &tmp);
890 if (ret < 0) {
891 mode->dump_dir = 0;
892 goto try_file;
894 mode->dump_dir = !!S_ISDIR(tmp.st_mode);
895 if (mode->dump_dir) {
896 fd = begin_multi_pcap_file(mode);
897 } else {
898 try_file:
899 fd = begin_single_pcap_file(mode);
903 fmemset(&rx_ring, 0, sizeof(rx_ring));
904 fmemset(&rx_poll, 0, sizeof(rx_poll));
905 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
907 ifindex = device_ifindex(mode->device_in);
908 size = ring_size(mode->device_in, mode->reserve_size);
910 enable_kernel_bpf_jit_compiler();
911 bpf_parse_rules(mode->filter, &bpf_ops);
912 bpf_attach_to_sock(sock, &bpf_ops);
914 set_sockopt_hwtimestamp(sock, mode->device_in);
915 setup_rx_ring_layout(sock, &rx_ring, size, mode->jumbo_support);
916 create_rx_ring(sock, &rx_ring);
917 mmap_rx_ring(sock, &rx_ring);
918 alloc_rx_ring_frames(&rx_ring);
919 bind_rx_ring(sock, &rx_ring, ifindex);
921 prepare_polling(sock, &rx_poll);
922 dissector_init_all(mode->print_mode);
924 if (mode->cpu >= 0 && ifindex > 0) {
925 irq = device_irq_number(mode->device_in);
926 device_bind_irq_to_cpu(mode->cpu, irq);
927 printf("IRQ: %s:%d > CPU%d\n", mode->device_in, irq,
928 mode->cpu);
931 if (mode->promiscuous == true) {
932 ifflags = enter_promiscuous_mode(mode->device_in);
933 printf("PROMISC\n");
936 printf("BPF:\n");
937 bpf_dump_all(&bpf_ops);
938 printf("MD: RX %s ", mode->dump ? pcap_ops[mode->pcap]->name : "");
939 if (mode->rfraw)
940 printf("802.11 raw via %s ", mode->device_in);
941 #ifdef _LARGEFILE64_SOURCE
942 printf("lf64 ");
943 #endif
944 ioprio_print();
945 printf("\n");
947 gettimeofday(&start, NULL);
949 while (likely(sigint == 0)) {
950 while (user_may_pull_from_rx(rx_ring.frames[it].iov_base)) {
951 hdr = rx_ring.frames[it].iov_base;
952 packet = ((uint8_t *) hdr) + hdr->tp_h.tp_mac;
953 fcnt++;
955 if (mode->packet_type != PACKET_ALL)
956 if (mode->packet_type != hdr->s_ll.sll_pkttype)
957 goto next;
958 if (unlikely(ring_frame_size(&rx_ring) <
959 hdr->tp_h.tp_snaplen)) {
960 skipped++;
961 goto next;
963 if (mode->dump) {
964 struct pcap_pkthdr phdr;
965 tpacket_hdr_to_pcap_pkthdr(&hdr->tp_h, &phdr);
966 ret = pcap_ops[mode->pcap]->write_pcap_pkt(fd, &phdr,
967 packet, phdr.len);
968 if (unlikely(ret != sizeof(phdr) + phdr.len))
969 panic("Write error to pcap!\n");
972 show_frame_hdr(hdr, mode->print_mode, RING_MODE_INGRESS);
973 dissector_entry_point(packet, hdr->tp_h.tp_snaplen,
974 mode->link_type, mode->print_mode);
976 if (frame_cnt_max != 0 && fcnt >= frame_cnt_max) {
977 sigint = 1;
978 break;
980 next:
981 kernel_may_pull_from_rx(&hdr->tp_h);
982 next_slot_prerd(&it, &rx_ring);
984 if (unlikely(sigint == 1))
985 break;
986 if (mode->dump && next_dump) {
987 struct tpacket_stats kstats;
988 socklen_t slen = sizeof(kstats);
989 fmemset(&kstats, 0, sizeof(kstats));
990 getsockopt(sock, SOL_PACKET, PACKET_STATISTICS,
991 &kstats, &slen);
992 fd = next_multi_pcap_file(mode, fd);
993 next_dump = false;
994 if (mode->print_mode == FNTTYPE_PRINT_NONE) {
995 printf(".(+%lu/-%lu)",
996 1UL * kstats.tp_packets -
997 kstats.tp_drops -
998 skipped, 1UL * kstats.tp_drops +
999 skipped);
1000 fflush(stdout);
1005 poll(&rx_poll, 1, -1);
1006 poll_error_maybe_die(sock, &rx_poll);
1009 gettimeofday(&end, NULL);
1010 diff = tv_subtract(end, start);
1012 if (!(mode->dump_dir && mode->print_mode == FNTTYPE_PRINT_NONE)) {
1013 sock_print_net_stats(sock, skipped);
1014 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec,
1015 diff.tv_usec);
1016 } else {
1017 printf("\n\n");
1018 fflush(stdout);
1021 bpf_release(&bpf_ops);
1022 dissector_cleanup_all();
1023 destroy_rx_ring(sock, &rx_ring);
1025 if (mode->promiscuous == true)
1026 leave_promiscuous_mode(mode->device_in, ifflags);
1028 if (mode->rfraw)
1029 leave_rfmon_mac80211(mode->device_trans, mode->device_in);
1031 close(sock);
1033 if (mode->dump) {
1034 if (mode->dump_dir)
1035 finish_multi_pcap_file(mode, fd);
1036 else
1037 finish_single_pcap_file(mode, fd);
1041 static void help(void)
1043 printf("\n%s %s, the packet sniffing beast\n", PROGNAME_STRING,
1044 VERSION_STRING);
1045 printf("http://www.netsniff-ng.org\n\n");
1046 printf("Usage: netsniff-ng [options]\n");
1047 printf("Options:\n");
1048 printf(" -i|-d|--dev|--in <dev|pcap> Input source as netdev or pcap\n");
1049 printf(" -o|--out <dev|pcap|dir|txf> Output sink as netdev, pcap, directory, txf file\n");
1050 printf(" -f|--filter <bpf-file> Use BPF filter file from bpfc\n");
1051 printf(" -t|--type <type> Only handle packets of defined type:\n");
1052 printf(" host|broadcast|multicast|others|outgoing\n");
1053 printf(" -F|--interval <uint> Dump interval in sec if -o is a directory where\n");
1054 printf(" pcap files should be stored (default: 60)\n");
1055 printf(" -J|--jumbo-support Support for 64KB Super Jumbo Frames\n");
1056 printf(" Default RX/TX slot: 2048Byte\n");
1057 printf(" -R|--rfraw Capture or inject raw 802.11 frames\n");
1058 printf(" -n|--num <uint> Number of packets until exit\n");
1059 printf(" `-- 0 Loop until interrupted (default)\n");
1060 printf(" `- n Send n packets and done\n");
1061 printf("Options for printing:\n");
1062 printf(" -s|--silent Do not print captured packets\n");
1063 printf(" -q|--less Print less-verbose packet information\n");
1064 printf(" -X|--hex Print packet data in hex format\n");
1065 printf(" -l|--ascii Print human-readable packet data\n");
1066 printf("Options, advanced:\n");
1067 printf(" -r|--rand Randomize packet forwarding order\n");
1068 printf(" -M|--no-promisc No promiscuous mode for netdev\n");
1069 printf(" -A|--no-sock-mem Don't tune core socket memory\n");
1070 printf(" -m|--mmap Mmap pcap file i.e., for replaying\n");
1071 printf(" -g|--sg Scatter/gather pcap file I/O\n");
1072 printf(" -c|--clrw Use slower read(2)/write(2) I/O\n");
1073 printf(" -S|--ring-size <size> Manually set ring size to <size>:\n");
1074 printf(" mmap space in KB/MB/GB, e.g. \'10MB\'\n");
1075 printf(" -k|--kernel-pull <uint> Kernel pull from user interval in us\n");
1076 printf(" Default is 10us where the TX_RING\n");
1077 printf(" is populated with payload from uspace\n");
1078 printf(" -b|--bind-cpu <cpu> Bind to specific CPU (or CPU-range)\n");
1079 printf(" -B|--unbind-cpu <cpu> Forbid to use specific CPU (or CPU-range)\n");
1080 printf(" -H|--prio-high Make this high priority process\n");
1081 printf(" -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n");
1082 printf(" -v|--version Show version\n");
1083 printf(" -h|--help Guess what?!\n");
1084 printf("\n");
1085 printf("Examples:\n");
1086 printf(" netsniff-ng --in eth0 --out dump.pcap --silent --bind-cpu 0\n");
1087 printf(" netsniff-ng --in wlan0 --rfraw --out dump.pcap --silent --bind-cpu 0\n");
1088 printf(" netsniff-ng --in dump.pcap --mmap --out eth0 --silent --bind-cpu 0\n");
1089 printf(" netsniff-ng --in dump.pcap --out dump.txf --silent --bind-cpu 0\n");
1090 printf(" netsniff-ng --in eth0 --out eth1 --silent --bind-cpu 0 --type host\n");
1091 printf(" netsniff-ng --in eth1 --out /opt/probe1/ -s -m -J --interval 30 -b 0\n");
1092 printf(" netsniff-ng --in any --filter http.bpf --jumbo-support --ascii\n");
1093 printf("\n");
1094 printf("Note:\n");
1095 printf(" This tool is targeted for network developers! You should\n");
1096 printf(" be aware of what you are doing and what these options above\n");
1097 printf(" mean! Use netsniff-ng's bpfc compiler for generating filter files.\n");
1098 printf(" Further, netsniff-ng automatically enables the kernel BPF JIT\n");
1099 printf(" if present. Txf file output is only possible if the input source\n");
1100 printf(" is a pcap file.\n");
1101 printf("\n");
1102 printf("Please report bugs to <bugs@netsniff-ng.org>\n");
1103 printf("Copyright (C) 2009-2012 Daniel Borkmann <daniel@netsniff-ng.org>\n");
1104 printf("Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel@netsniff-ng.org>\n");
1105 printf("License: GNU GPL version 2\n");
1106 printf("This is free software: you are free to change and redistribute it.\n");
1107 printf("There is NO WARRANTY, to the extent permitted by law.\n\n");
1108 die();
1111 static void version(void)
1113 printf("\n%s %s, the packet sniffing beast\n", PROGNAME_STRING,
1114 VERSION_STRING);
1115 printf("http://www.netsniff-ng.org\n\n");
1116 printf("Please report bugs to <bugs@netsniff-ng.org>\n");
1117 printf("Copyright (C) 2009-2012 Daniel Borkmann <daniel@netsniff-ng.org>\n");
1118 printf("Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel@netsniff-ng.org>\n");
1119 printf("License: GNU GPL version 2\n");
1120 printf("This is free software: you are free to change and redistribute it.\n");
1121 printf("There is NO WARRANTY, to the extent permitted by law.\n\n");
1122 die();
1125 static void header(void)
1127 printf("%s%s%s\n", colorize_start(bold), PROGNAME_STRING " "
1128 VERSION_STRING, colorize_end());
1131 int main(int argc, char **argv)
1133 int c, i, j, opt_index, ops_touched = 0;
1134 int vals[4] = {0};
1135 char *ptr;
1136 bool prio_high = false;
1137 bool setsockmem = true;
1138 struct mode mode;
1139 void (*enter_mode)(struct mode *mode) = NULL;
1141 check_for_root_maybe_die();
1143 fmemset(&mode, 0, sizeof(mode));
1144 mode.link_type = LINKTYPE_EN10MB;
1145 mode.print_mode = FNTTYPE_PRINT_NORM;
1146 mode.cpu = CPU_UNKNOWN;
1147 mode.packet_type = PACKET_ALL;
1148 mode.promiscuous = true;
1149 mode.randomize = false;
1150 mode.pcap = PCAP_OPS_SG;
1151 mode.dump_interval = DUMP_INTERVAL;
1153 while ((c = getopt_long(argc, argv, short_options, long_options,
1154 &opt_index)) != EOF) {
1155 switch (c) {
1156 case 'd':
1157 case 'i':
1158 mode.device_in = xstrdup(optarg);
1159 break;
1160 case 'o':
1161 mode.device_out = xstrdup(optarg);
1162 break;
1163 case 'R':
1164 mode.link_type = LINKTYPE_IEEE802_11;
1165 mode.rfraw = 1;
1166 break;
1167 case 'r':
1168 mode.randomize = true;
1169 break;
1170 case 'J':
1171 mode.jumbo_support = 1;
1172 break;
1173 case 'f':
1174 mode.filter = xstrdup(optarg);
1175 break;
1176 case 'M':
1177 mode.promiscuous = false;
1178 break;
1179 case 'A':
1180 setsockmem = false;
1181 break;
1182 case 't':
1183 if (!strncmp(optarg, "host", strlen("host")))
1184 mode.packet_type = PACKET_HOST;
1185 else if (!strncmp(optarg, "broadcast", strlen("broadcast")))
1186 mode.packet_type = PACKET_BROADCAST;
1187 else if (!strncmp(optarg, "multicast", strlen("multicast")))
1188 mode.packet_type = PACKET_MULTICAST;
1189 else if (!strncmp(optarg, "others", strlen("others")))
1190 mode.packet_type = PACKET_OTHERHOST;
1191 else if (!strncmp(optarg, "outgoing", strlen("outgoing")))
1192 mode.packet_type = PACKET_OUTGOING;
1193 else
1194 mode.packet_type = PACKET_ALL;
1195 break;
1196 case 'S':
1197 ptr = optarg;
1198 mode.reserve_size = 0;
1200 for (j = i = strlen(optarg); i > 0; --i) {
1201 if (!isdigit(optarg[j - i]))
1202 break;
1203 ptr++;
1206 if (!strncmp(ptr, "KB", strlen("KB")))
1207 mode.reserve_size = 1 << 10;
1208 else if (!strncmp(ptr, "MB", strlen("MB")))
1209 mode.reserve_size = 1 << 20;
1210 else if (!strncmp(ptr, "GB", strlen("GB")))
1211 mode.reserve_size = 1 << 30;
1212 else
1213 panic("Syntax error in ring size param!\n");
1215 *ptr = 0;
1216 mode.reserve_size *= atoi(optarg);
1217 break;
1218 case 'b':
1219 set_cpu_affinity(optarg, 0);
1220 if (mode.cpu != CPU_NOTOUCH)
1221 mode.cpu = atoi(optarg);
1222 break;
1223 case 'B':
1224 set_cpu_affinity(optarg, 1);
1225 break;
1226 case 'H':
1227 prio_high = true;
1228 break;
1229 case 'c':
1230 mode.pcap = PCAP_OPS_RW;
1231 ops_touched = 1;
1232 break;
1233 case 'm':
1234 mode.pcap = PCAP_OPS_MMAP;
1235 ops_touched = 1;
1236 break;
1237 case 'g':
1238 mode.pcap = PCAP_OPS_SG;
1239 ops_touched = 1;
1240 break;
1241 case 'Q':
1242 mode.cpu = CPU_NOTOUCH;
1243 break;
1244 case 's':
1245 mode.print_mode = FNTTYPE_PRINT_NONE;
1246 break;
1247 case 'q':
1248 mode.print_mode = FNTTYPE_PRINT_LESS;
1249 break;
1250 case 'X':
1251 mode.print_mode = (mode.print_mode == FNTTYPE_PRINT_ASCII) ?
1252 FNTTYPE_PRINT_HEX_ASCII : FNTTYPE_PRINT_HEX;
1253 break;
1254 case 'l':
1255 mode.print_mode = (mode.print_mode == FNTTYPE_PRINT_HEX) ?
1256 FNTTYPE_PRINT_HEX_ASCII : FNTTYPE_PRINT_ASCII;
1257 break;
1258 case 'k':
1259 mode.kpull = (unsigned long) atol(optarg);
1260 break;
1261 case 'n':
1262 frame_cnt_max = (unsigned long) atol(optarg);
1263 break;
1264 case 'F':
1265 mode.dump_interval = (unsigned long) atol(optarg);
1266 break;
1267 case 'v':
1268 version();
1269 break;
1270 case 'h':
1271 help();
1272 break;
1273 case '?':
1274 switch (optopt) {
1275 case 'd':
1276 case 'i':
1277 case 'o':
1278 case 'f':
1279 case 't':
1280 case 'F':
1281 case 'n':
1282 case 'S':
1283 case 'b':
1284 case 'k':
1285 case 'B':
1286 case 'e':
1287 panic("Option -%c requires an argument!\n",
1288 optopt);
1289 default:
1290 if (isprint(optopt))
1291 whine("Unknown option character "
1292 "`0x%X\'!\n", optopt);
1293 die();
1295 default:
1296 break;
1300 if (!mode.device_in)
1301 mode.device_in = xstrdup("any");
1303 register_signal(SIGINT, signal_handler);
1304 register_signal(SIGHUP, signal_handler);
1306 init_pcap(mode.jumbo_support);
1307 tprintf_init();
1308 header();
1310 if (prio_high == true) {
1311 set_proc_prio(get_default_proc_prio());
1312 set_sched_status(get_default_sched_policy(),
1313 get_default_sched_prio());
1316 if (setsockmem == true) {
1317 if ((vals[0] = get_system_socket_mem(sock_rmem_max)) < SMEM_SUG_MAX)
1318 set_system_socket_mem(sock_rmem_max, SMEM_SUG_MAX);
1319 if ((vals[1] = get_system_socket_mem(sock_rmem_def)) < SMEM_SUG_DEF)
1320 set_system_socket_mem(sock_rmem_def, SMEM_SUG_DEF);
1321 if ((vals[2] = get_system_socket_mem(sock_wmem_max)) < SMEM_SUG_MAX)
1322 set_system_socket_mem(sock_wmem_max, SMEM_SUG_MAX);
1323 if ((vals[3] = get_system_socket_mem(sock_wmem_def)) < SMEM_SUG_DEF)
1324 set_system_socket_mem(sock_wmem_def, SMEM_SUG_DEF);
1327 if (mode.device_in && (device_mtu(mode.device_in) ||
1328 !strncmp("any", mode.device_in, strlen(mode.device_in)))) {
1329 if (!mode.device_out) {
1330 mode.dump = 0;
1331 enter_mode = enter_mode_rx_only_or_dump;
1332 } else if (device_mtu(mode.device_out)) {
1333 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1334 enter_mode = enter_mode_rx_to_tx;
1335 } else {
1336 mode.dump = 1;
1337 register_signal_f(SIGALRM, timer_next_dump, SA_SIGINFO);
1338 enter_mode = enter_mode_rx_only_or_dump;
1339 if (!ops_touched)
1340 mode.pcap = PCAP_OPS_SG;
1342 } else {
1343 if (mode.device_out && device_mtu(mode.device_out)) {
1344 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1345 enter_mode = enter_mode_pcap_to_tx;
1346 if (!ops_touched)
1347 mode.pcap = PCAP_OPS_MMAP;
1348 } else {
1349 enter_mode = enter_mode_read_pcap;
1350 if (!ops_touched)
1351 mode.pcap = PCAP_OPS_SG;
1355 if (!enter_mode)
1356 panic("Selection not supported!\n");
1357 enter_mode(&mode);
1359 tprintf_cleanup();
1360 cleanup_pcap();
1362 if (setsockmem == true) {
1363 set_system_socket_mem(sock_rmem_max, vals[0]);
1364 set_system_socket_mem(sock_rmem_def, vals[1]);
1365 set_system_socket_mem(sock_wmem_max, vals[2]);
1366 set_system_socket_mem(sock_wmem_def, vals[3]);
1369 if (mode.device_in)
1370 xfree(mode.device_in);
1371 if (mode.device_out)
1372 xfree(mode.device_out);
1373 if (mode.device_trans)
1374 xfree(mode.device_trans);
1376 return 0;