flowtop: Print error cause if errno is set when panic
[netsniff-ng.git] / netsniff-ng.c
bloba2067d3c8a96a99111447291f0c796e7bb9cbf1c
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2009-2013 Daniel Borkmann.
4 * Copyright 2010 Emmanuel Roullit.
5 * Subject to the GPL, version 2.
6 */
8 #define _GNU_SOURCE
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <signal.h>
12 #include <getopt.h>
13 #include <ctype.h>
14 #include <time.h>
15 #include <string.h>
16 #include <sys/socket.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <sys/time.h>
20 #include <sys/fsuid.h>
21 #include <unistd.h>
22 #include <stdbool.h>
23 #include <pthread.h>
24 #include <fcntl.h>
26 #include "ring_rx.h"
27 #include "ring_tx.h"
28 #include "mac80211.h"
29 #include "dev.h"
30 #include "built_in.h"
31 #include "pcap_io.h"
32 #include "privs.h"
33 #include "proc.h"
34 #include "bpf.h"
35 #include "ioops.h"
36 #include "die.h"
37 #include "irq.h"
38 #include "str.h"
39 #include "sig.h"
40 #include "config.h"
41 #include "sock.h"
42 #include "geoip.h"
43 #include "lockme.h"
44 #include "tprintf.h"
45 #include "timer.h"
46 #include "tstamping.h"
47 #include "dissector.h"
48 #include "xmalloc.h"
50 enum dump_mode {
51 DUMP_INTERVAL_TIME,
52 DUMP_INTERVAL_SIZE,
55 struct ctx {
56 char *device_in, *device_out, *device_trans, *filter, *prefix;
57 int cpu, rfraw, dump, print_mode, dump_dir, packet_type;
58 unsigned long kpull, dump_interval, tx_bytes, tx_packets;
59 size_t reserve_size;
60 bool randomize, promiscuous, enforce, jumbo, dump_bpf, hwtimestamp, verbose;
61 enum pcap_ops_groups pcap; enum dump_mode dump_mode;
62 uid_t uid; gid_t gid; uint32_t link_type, magic;
65 static volatile sig_atomic_t sigint = 0;
66 static volatile bool next_dump = false;
68 static const char *short_options = "d:i:o:rf:MNJt:S:k:n:b:HQmcsqXlvhF:RGAP:Vu:g:T:DBU";
69 static const struct option long_options[] = {
70 {"dev", required_argument, NULL, 'd'},
71 {"in", required_argument, NULL, 'i'},
72 {"out", required_argument, NULL, 'o'},
73 {"filter", required_argument, NULL, 'f'},
74 {"num", required_argument, NULL, 'n'},
75 {"type", required_argument, NULL, 't'},
76 {"interval", required_argument, NULL, 'F'},
77 {"ring-size", required_argument, NULL, 'S'},
78 {"kernel-pull", required_argument, NULL, 'k'},
79 {"bind-cpu", required_argument, NULL, 'b'},
80 {"prefix", required_argument, NULL, 'P'},
81 {"user", required_argument, NULL, 'u'},
82 {"group", required_argument, NULL, 'g'},
83 {"magic", required_argument, NULL, 'T'},
84 {"rand", no_argument, NULL, 'r'},
85 {"rfraw", no_argument, NULL, 'R'},
86 {"mmap", no_argument, NULL, 'm'},
87 {"sg", no_argument, NULL, 'G'},
88 {"clrw", no_argument, NULL, 'c'},
89 {"jumbo-support", no_argument, NULL, 'J'},
90 {"no-promisc", no_argument, NULL, 'M'},
91 {"no-hwtimestamp", no_argument, NULL, 'N'},
92 {"prio-high", no_argument, NULL, 'H'},
93 {"notouch-irq", no_argument, NULL, 'Q'},
94 {"dump-pcap-types", no_argument, NULL, 'D'},
95 {"dump-bpf", no_argument, NULL, 'B'},
96 {"silent", no_argument, NULL, 's'},
97 {"less", no_argument, NULL, 'q'},
98 {"hex", no_argument, NULL, 'X'},
99 {"ascii", no_argument, NULL, 'l'},
100 {"no-sock-mem", no_argument, NULL, 'A'},
101 {"update", no_argument, NULL, 'U'},
102 {"verbose", no_argument, NULL, 'V'},
103 {"version", no_argument, NULL, 'v'},
104 {"help", no_argument, NULL, 'h'},
105 {NULL, 0, NULL, 0}
108 static const char *copyright = "Please report bugs to <bugs@netsniff-ng.org>\n"
109 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
110 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
111 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
112 "Swiss federal institute of technology (ETH Zurich)\n"
113 "License: GNU GPL version 2.0\n"
114 "This is free software: you are free to change and redistribute it.\n"
115 "There is NO WARRANTY, to the extent permitted by law.";
117 static int tx_sock;
118 static struct itimerval itimer;
119 static unsigned long frame_count_max = 0, interval = TX_KERNEL_PULL_INT;
121 #define __pcap_io pcap_ops[ctx->pcap]
123 static void signal_handler(int number)
125 switch (number) {
126 case SIGINT:
127 case SIGQUIT:
128 case SIGTERM:
129 sigint = 1;
130 case SIGHUP:
131 default:
132 break;
136 static void timer_elapsed(int unused __maybe_unused)
138 int ret;
140 set_itimer_interval_value(&itimer, 0, interval);
142 ret = pull_and_flush_tx_ring(tx_sock);
143 if (unlikely(ret < 0)) {
144 /* We could hit EBADF if the socket has been closed before
145 * the timer was triggered.
147 if (errno != EBADF && errno != ENOBUFS)
148 panic("Flushing TX_RING failed: %s!\n", strerror(errno));
151 setitimer(ITIMER_REAL, &itimer, NULL);
154 static void timer_purge(void)
156 int ret;
158 ret = pull_and_flush_tx_ring_wait(tx_sock);
159 if (unlikely(ret < 0)) {
160 if (errno != EBADF && errno != ENOBUFS)
161 panic("Flushing TX_RING failed: %s!\n", strerror(errno));
164 set_itimer_interval_value(&itimer, 0, 0);
165 setitimer(ITIMER_REAL, &itimer, NULL);
168 static void timer_next_dump(int unused __maybe_unused)
170 set_itimer_interval_value(&itimer, interval, 0);
171 next_dump = true;
172 setitimer(ITIMER_REAL, &itimer, NULL);
175 static inline bool dump_to_pcap(struct ctx *ctx)
177 return ctx->dump;
180 static void pcap_to_xmit(struct ctx *ctx)
182 uint8_t *out = NULL;
183 int ifindex, fd = 0, ret;
184 size_t size;
185 unsigned int it = 0;
186 unsigned long trunced = 0;
187 struct ring tx_ring;
188 struct frame_map *hdr;
189 struct sock_fprog bpf_ops;
190 struct timeval start, end, diff;
191 pcap_pkthdr_t phdr;
193 if (!device_up_and_running(ctx->device_out) && !ctx->rfraw)
194 panic("Device not up and running!\n");
196 bug_on(!__pcap_io);
198 tx_sock = pf_socket();
200 if (!strncmp("-", ctx->device_in, strlen("-"))) {
201 fd = dup_or_die(fileno(stdin));
202 close(fileno(stdin));
203 if (ctx->pcap == PCAP_OPS_MM)
204 ctx->pcap = PCAP_OPS_SG;
205 } else {
206 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
209 if (__pcap_io->init_once_pcap)
210 __pcap_io->init_once_pcap();
212 ret = __pcap_io->pull_fhdr_pcap(fd, &ctx->magic, &ctx->link_type);
213 if (ret)
214 panic("Error reading pcap header!\n");
216 if (__pcap_io->prepare_access_pcap) {
217 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_RD, ctx->jumbo);
218 if (ret)
219 panic("Error prepare reading pcap!\n");
222 if (ctx->rfraw) {
223 ctx->device_trans = xstrdup(ctx->device_out);
224 xfree(ctx->device_out);
226 enter_rfmon_mac80211(ctx->device_trans, &ctx->device_out);
227 if (ctx->link_type != LINKTYPE_IEEE802_11)
228 panic("Wrong linktype of pcap!\n");
231 ifindex = device_ifindex(ctx->device_out);
232 size = ring_size(ctx->device_out, ctx->reserve_size);
234 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
235 if (ctx->dump_bpf)
236 bpf_dump_all(&bpf_ops);
238 ring_tx_setup(&tx_ring, tx_sock, size, ifindex, ctx->jumbo, ctx->verbose);
240 dissector_init_all(ctx->print_mode);
242 if (ctx->cpu >= 0 && ifindex > 0) {
243 int irq = device_irq_number(ctx->device_out);
244 device_set_irq_affinity(irq, ctx->cpu);
246 if (ctx->verbose)
247 printf("IRQ: %s:%d > CPU%d\n",
248 ctx->device_out, irq, ctx->cpu);
251 if (ctx->kpull)
252 interval = ctx->kpull;
254 set_itimer_interval_value(&itimer, 0, interval);
255 setitimer(ITIMER_REAL, &itimer, NULL);
257 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
259 printf("Running! Hang up with ^C!\n\n");
260 fflush(stdout);
262 bug_on(gettimeofday(&start, NULL));
264 while (likely(sigint == 0)) {
265 while (user_may_pull_from_tx(tx_ring.frames[it].iov_base)) {
266 hdr = tx_ring.frames[it].iov_base;
267 out = ((uint8_t *) hdr) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
269 do {
270 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic, out,
271 ring_frame_size(&tx_ring));
272 if (unlikely(ret <= 0))
273 goto out;
275 if (ring_frame_size(&tx_ring) <
276 pcap_get_length(&phdr, ctx->magic)) {
277 pcap_set_length(&phdr, ctx->magic,
278 ring_frame_size(&tx_ring));
279 trunced++;
281 } while (ctx->filter &&
282 !bpf_run_filter(&bpf_ops, out,
283 pcap_get_length(&phdr, ctx->magic)));
285 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &hdr->tp_h, NULL);
287 ctx->tx_bytes += hdr->tp_h.tp_len;;
288 ctx->tx_packets++;
290 show_frame_hdr(out, hdr->tp_h.tp_snaplen,
291 ctx->link_type, hdr, ctx->print_mode);
293 dissector_entry_point(out, hdr->tp_h.tp_snaplen,
294 ctx->link_type, ctx->print_mode);
296 kernel_may_pull_from_tx(&hdr->tp_h);
298 it++;
299 if (it >= tx_ring.layout.tp_frame_nr)
300 it = 0;
302 if (unlikely(sigint == 1))
303 break;
305 if (frame_count_max != 0) {
306 if (ctx->tx_packets >= frame_count_max) {
307 sigint = 1;
308 break;
314 out:
315 bug_on(gettimeofday(&end, NULL));
316 timersub(&end, &start, &diff);
318 timer_purge();
320 bpf_release(&bpf_ops);
322 dissector_cleanup_all();
323 destroy_tx_ring(tx_sock, &tx_ring);
325 if (ctx->rfraw)
326 leave_rfmon_mac80211(ctx->device_out);
328 if (__pcap_io->prepare_close_pcap)
329 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
331 if (!strncmp("-", ctx->device_in, strlen("-")))
332 dup2(fd, fileno(stdin));
333 close(fd);
335 close(tx_sock);
337 fflush(stdout);
338 printf("\n");
339 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
340 printf("\r%12lu packets truncated in file\n", trunced);
341 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
342 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
345 static void receive_to_xmit(struct ctx *ctx)
347 short ifflags = 0;
348 uint8_t *in, *out;
349 int rx_sock, ifindex_in, ifindex_out, ret;
350 size_t size_in, size_out;
351 unsigned int it_in = 0, it_out = 0;
352 unsigned long frame_count = 0;
353 struct frame_map *hdr_in, *hdr_out;
354 struct ring tx_ring, rx_ring;
355 struct pollfd rx_poll;
356 struct sock_fprog bpf_ops;
358 if (!strncmp(ctx->device_in, ctx->device_out, IFNAMSIZ))
359 panic("Ingress/egress devices must be different!\n");
360 if (!device_up_and_running(ctx->device_out))
361 panic("Egress device not up and running!\n");
363 rx_sock = pf_socket();
364 tx_sock = pf_socket();
366 ifindex_in = device_ifindex(ctx->device_in);
367 ifindex_out = device_ifindex(ctx->device_out);
369 size_in = ring_size(ctx->device_in, ctx->reserve_size);
370 size_out = ring_size(ctx->device_out, ctx->reserve_size);
372 enable_kernel_bpf_jit_compiler();
374 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
375 if (ctx->dump_bpf)
376 bpf_dump_all(&bpf_ops);
377 bpf_attach_to_sock(rx_sock, &bpf_ops);
379 ring_rx_setup(&rx_ring, rx_sock, size_in, ifindex_in, &rx_poll, false, ctx->jumbo, ctx->verbose);
380 ring_tx_setup(&tx_ring, tx_sock, size_out, ifindex_out, ctx->jumbo, ctx->verbose);
382 dissector_init_all(ctx->print_mode);
384 if (ctx->promiscuous)
385 ifflags = device_enter_promiscuous_mode(ctx->device_in);
387 if (ctx->kpull)
388 interval = ctx->kpull;
390 set_itimer_interval_value(&itimer, 0, interval);
391 setitimer(ITIMER_REAL, &itimer, NULL);
393 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
395 printf("Running! Hang up with ^C!\n\n");
396 fflush(stdout);
398 while (likely(sigint == 0)) {
399 while (user_may_pull_from_rx(rx_ring.frames[it_in].iov_base)) {
400 hdr_in = rx_ring.frames[it_in].iov_base;
401 in = ((uint8_t *) hdr_in) + hdr_in->tp_h.tp_mac;
403 frame_count++;
405 if (ctx->packet_type != -1)
406 if (ctx->packet_type != hdr_in->s_ll.sll_pkttype)
407 goto next;
409 hdr_out = tx_ring.frames[it_out].iov_base;
410 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
412 while (!user_may_pull_from_tx(tx_ring.frames[it_out].iov_base) &&
413 likely(!sigint)) {
414 if (ctx->randomize)
415 next_rnd_slot(&it_out, &tx_ring);
416 else {
417 it_out++;
418 if (it_out >= tx_ring.layout.tp_frame_nr)
419 it_out = 0;
422 hdr_out = tx_ring.frames[it_out].iov_base;
423 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
426 tpacket_hdr_clone(&hdr_out->tp_h, &hdr_in->tp_h);
427 fmemcpy(out, in, hdr_in->tp_h.tp_len);
429 kernel_may_pull_from_tx(&hdr_out->tp_h);
430 if (ctx->randomize)
431 next_rnd_slot(&it_out, &tx_ring);
432 else {
433 it_out++;
434 if (it_out >= tx_ring.layout.tp_frame_nr)
435 it_out = 0;
438 show_frame_hdr(in, hdr_in->tp_h.tp_snaplen,
439 ctx->link_type, hdr_in, ctx->print_mode);
441 dissector_entry_point(in, hdr_in->tp_h.tp_snaplen,
442 ctx->link_type, ctx->print_mode);
444 if (frame_count_max != 0) {
445 if (frame_count >= frame_count_max) {
446 sigint = 1;
447 break;
451 next:
452 kernel_may_pull_from_rx(&hdr_in->tp_h);
454 it_in++;
455 if (it_in >= rx_ring.layout.tp_frame_nr)
456 it_in = 0;
458 if (unlikely(sigint == 1))
459 goto out;
462 ret = poll(&rx_poll, 1, -1);
463 if (unlikely(ret < 0)) {
464 if (errno != EINTR)
465 panic("Poll failed!\n");
469 out:
470 timer_purge();
472 sock_rx_net_stats(rx_sock, 0);
474 bpf_release(&bpf_ops);
476 dissector_cleanup_all();
478 destroy_tx_ring(tx_sock, &tx_ring);
479 destroy_rx_ring(rx_sock, &rx_ring);
481 if (ctx->promiscuous)
482 device_leave_promiscuous_mode(ctx->device_in, ifflags);
484 close(tx_sock);
485 close(rx_sock);
488 static void translate_pcap_to_txf(int fdo, uint8_t *out, size_t len)
490 size_t bytes_done = 0;
491 char bout[80];
493 slprintf(bout, sizeof(bout), "{\n ");
494 write_or_die(fdo, bout, strlen(bout));
496 while (bytes_done < len) {
497 slprintf(bout, sizeof(bout), "0x%02x, ", out[bytes_done]);
498 write_or_die(fdo, bout, strlen(bout));
500 bytes_done++;
502 if (bytes_done % 10 == 0) {
503 slprintf(bout, sizeof(bout), "\n");
504 write_or_die(fdo, bout, strlen(bout));
506 if (bytes_done < len) {
507 slprintf(bout, sizeof(bout), " ");
508 write_or_die(fdo, bout, strlen(bout));
512 if (bytes_done % 10 != 0) {
513 slprintf(bout, sizeof(bout), "\n");
514 write_or_die(fdo, bout, strlen(bout));
517 slprintf(bout, sizeof(bout), "}\n\n");
518 write_or_die(fdo, bout, strlen(bout));
521 static void read_pcap(struct ctx *ctx)
523 uint8_t *out;
524 int ret, fd, fdo = 0;
525 unsigned long trunced = 0;
526 size_t out_len;
527 pcap_pkthdr_t phdr;
528 struct sock_fprog bpf_ops;
529 struct frame_map fm;
530 struct timeval start, end, diff;
531 bool is_out_pcap = ctx->device_out && strstr(ctx->device_out, ".pcap");
532 const struct pcap_file_ops *pcap_out_ops = pcap_ops[PCAP_OPS_RW];
534 bug_on(!__pcap_io);
536 if (!strncmp("-", ctx->device_in, strlen("-"))) {
537 fd = dup_or_die(fileno(stdin));
538 close(fileno(stdin));
539 if (ctx->pcap == PCAP_OPS_MM)
540 ctx->pcap = PCAP_OPS_SG;
541 } else {
542 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
545 if (__pcap_io->init_once_pcap)
546 __pcap_io->init_once_pcap();
548 ret = __pcap_io->pull_fhdr_pcap(fd, &ctx->magic, &ctx->link_type);
549 if (ret)
550 panic("Error reading pcap header!\n");
552 if (__pcap_io->prepare_access_pcap) {
553 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_RD, ctx->jumbo);
554 if (ret)
555 panic("Error prepare reading pcap!\n");
558 fmemset(&fm, 0, sizeof(fm));
560 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
561 if (ctx->dump_bpf)
562 bpf_dump_all(&bpf_ops);
564 dissector_init_all(ctx->print_mode);
566 out_len = round_up(1024 * 1024, RUNTIME_PAGE_SIZE);
567 out = xmalloc_aligned(out_len, CO_CACHE_LINE_SIZE);
569 if (ctx->device_out) {
570 if (!strncmp("-", ctx->device_out, strlen("-"))) {
571 fdo = dup_or_die(fileno(stdout));
572 close(fileno(stdout));
573 } else {
574 fdo = open_or_die_m(ctx->device_out, O_RDWR | O_CREAT |
575 O_TRUNC | O_LARGEFILE, DEFFILEMODE);
579 if (is_out_pcap) {
580 ret = pcap_out_ops->push_fhdr_pcap(fdo, ctx->magic,
581 ctx->link_type);
582 if (ret)
583 panic("Error writing pcap header!\n");
586 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
588 printf("Running! Hang up with ^C!\n\n");
589 fflush(stdout);
591 bug_on(gettimeofday(&start, NULL));
593 while (likely(sigint == 0)) {
594 do {
595 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic,
596 out, out_len);
597 if (unlikely(ret < 0))
598 goto out;
600 if (unlikely(pcap_get_length(&phdr, ctx->magic) == 0)) {
601 trunced++;
602 continue;
605 if (unlikely(pcap_get_length(&phdr, ctx->magic) > out_len)) {
606 pcap_set_length(&phdr, ctx->magic, out_len);
607 trunced++;
609 } while (ctx->filter &&
610 !bpf_run_filter(&bpf_ops, out,
611 pcap_get_length(&phdr, ctx->magic)));
613 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &fm.tp_h, &fm.s_ll);
615 ctx->tx_bytes += fm.tp_h.tp_len;
616 ctx->tx_packets++;
618 show_frame_hdr(out, fm.tp_h.tp_snaplen, ctx->link_type, &fm,
619 ctx->print_mode);
621 dissector_entry_point(out, fm.tp_h.tp_snaplen,
622 ctx->link_type, ctx->print_mode);
624 if (is_out_pcap) {
625 size_t pcap_len = pcap_get_length(&phdr, ctx->magic);
626 int wlen = pcap_out_ops->write_pcap(fdo, &phdr,
627 ctx->magic, out,
628 pcap_len);
629 if (unlikely(wlen != (int)pcap_get_total_length(&phdr, ctx->magic)))
630 panic("Error writing to pcap!\n");
631 } else if (ctx->device_out) {
632 translate_pcap_to_txf(fdo, out, fm.tp_h.tp_snaplen);
635 if (frame_count_max != 0) {
636 if (ctx->tx_packets >= frame_count_max) {
637 sigint = 1;
638 break;
643 out:
644 bug_on(gettimeofday(&end, NULL));
645 timersub(&end, &start, &diff);
647 bpf_release(&bpf_ops);
649 dissector_cleanup_all();
651 if (__pcap_io->prepare_close_pcap)
652 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
654 xfree(out);
656 fflush(stdout);
657 printf("\n");
658 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
659 printf("\r%12lu packets truncated in file\n", trunced);
660 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
661 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
663 if (!strncmp("-", ctx->device_in, strlen("-")))
664 dup2(fd, fileno(stdin));
665 close(fd);
667 if (ctx->device_out) {
668 if (!strncmp("-", ctx->device_out, strlen("-")))
669 dup2(fdo, fileno(stdout));
670 close(fdo);
674 static void finish_multi_pcap_file(struct ctx *ctx, int fd)
676 __pcap_io->fsync_pcap(fd);
678 if (__pcap_io->prepare_close_pcap)
679 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
681 close(fd);
683 fmemset(&itimer, 0, sizeof(itimer));
684 setitimer(ITIMER_REAL, &itimer, NULL);
687 static int next_multi_pcap_file(struct ctx *ctx, int fd)
689 int ret;
690 char fname[512];
692 __pcap_io->fsync_pcap(fd);
694 if (__pcap_io->prepare_close_pcap)
695 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
697 close(fd);
699 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
700 ctx->prefix ? : "dump-", time(NULL));
702 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
703 O_LARGEFILE, DEFFILEMODE);
705 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
706 if (ret)
707 panic("Error writing pcap header!\n");
709 if (__pcap_io->prepare_access_pcap) {
710 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, true);
711 if (ret)
712 panic("Error prepare writing pcap!\n");
715 return fd;
718 static int begin_multi_pcap_file(struct ctx *ctx)
720 int fd, ret;
721 char fname[256];
723 bug_on(!__pcap_io);
725 if (ctx->device_out[strlen(ctx->device_out) - 1] == '/')
726 ctx->device_out[strlen(ctx->device_out) - 1] = 0;
728 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
729 ctx->prefix ? : "dump-", time(NULL));
731 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
732 O_LARGEFILE, DEFFILEMODE);
734 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
735 if (ret)
736 panic("Error writing pcap header!\n");
738 if (__pcap_io->prepare_access_pcap) {
739 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, true);
740 if (ret)
741 panic("Error prepare writing pcap!\n");
744 if (ctx->dump_mode == DUMP_INTERVAL_TIME) {
745 interval = ctx->dump_interval;
747 set_itimer_interval_value(&itimer, interval, 0);
748 setitimer(ITIMER_REAL, &itimer, NULL);
749 } else {
750 interval = 0;
753 return fd;
756 static void finish_single_pcap_file(struct ctx *ctx, int fd)
758 __pcap_io->fsync_pcap(fd);
760 if (__pcap_io->prepare_close_pcap)
761 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
763 if (strncmp("-", ctx->device_out, strlen("-")))
764 close(fd);
765 else
766 dup2(fd, fileno(stdout));
769 static int begin_single_pcap_file(struct ctx *ctx)
771 int fd, ret;
773 bug_on(!__pcap_io);
775 if (!strncmp("-", ctx->device_out, strlen("-"))) {
776 fd = dup_or_die(fileno(stdout));
777 close(fileno(stdout));
778 if (ctx->pcap == PCAP_OPS_MM)
779 ctx->pcap = PCAP_OPS_SG;
780 } else {
781 fd = open_or_die_m(ctx->device_out,
782 O_RDWR | O_CREAT | O_TRUNC |
783 O_LARGEFILE, DEFFILEMODE);
786 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
787 if (ret)
788 panic("Error writing pcap header!\n");
790 if (__pcap_io->prepare_access_pcap) {
791 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, true);
792 if (ret)
793 panic("Error prepare writing pcap!\n");
796 return fd;
799 static void print_pcap_file_stats(int sock, struct ctx *ctx)
801 int ret;
802 struct tpacket_stats kstats;
803 socklen_t slen = sizeof(kstats);
805 fmemset(&kstats, 0, sizeof(kstats));
807 ret = getsockopt(sock, SOL_PACKET, PACKET_STATISTICS, &kstats, &slen);
808 if (unlikely(ret))
809 panic("Cannot get packet statistics!\n");
811 if (ctx->print_mode == PRINT_NONE) {
812 printf(".(+%u/-%u)", kstats.tp_packets - kstats.tp_drops,
813 kstats.tp_drops);
814 fflush(stdout);
818 static void update_pcap_next_dump(struct ctx *ctx, unsigned long snaplen, int *fd, int sock)
820 if (!dump_to_pcap(ctx))
821 return;
823 if (ctx->dump_mode == DUMP_INTERVAL_SIZE) {
824 interval += snaplen;
825 if (interval > ctx->dump_interval) {
826 next_dump = true;
827 interval = 0;
831 if (next_dump) {
832 *fd = next_multi_pcap_file(ctx, *fd);
833 next_dump = false;
835 if (ctx->verbose)
836 print_pcap_file_stats(sock, ctx);
840 #ifdef HAVE_TPACKET3
841 static void walk_t3_block(struct block_desc *pbd, struct ctx *ctx,
842 int sock, int *fd, unsigned long *frame_count)
844 int num_pkts = pbd->h1.num_pkts, i;
845 struct tpacket3_hdr *hdr;
846 struct sockaddr_ll *sll;
848 hdr = (void *) ((uint8_t *) pbd + pbd->h1.offset_to_first_pkt);
849 sll = (void *) ((uint8_t *) hdr + TPACKET_ALIGN(sizeof(*hdr)));
851 for (i = 0; i < num_pkts && likely(sigint == 0); ++i) {
852 uint8_t *packet = ((uint8_t *) hdr + hdr->tp_mac);
853 pcap_pkthdr_t phdr;
855 if (ctx->packet_type != -1)
856 if (ctx->packet_type != sll->sll_pkttype)
857 goto next;
859 (*frame_count)++;
861 if (dump_to_pcap(ctx)) {
862 int ret;
864 tpacket3_hdr_to_pcap_pkthdr(hdr, sll, &phdr, ctx->magic);
866 ret = __pcap_io->write_pcap(*fd, &phdr, ctx->magic, packet,
867 pcap_get_length(&phdr, ctx->magic));
868 if (unlikely(ret != (int) pcap_get_total_length(&phdr, ctx->magic)))
869 panic("Write error to pcap!\n");
872 __show_frame_hdr(packet, hdr->tp_snaplen, ctx->link_type, sll,
873 hdr, ctx->print_mode, true);
875 dissector_entry_point(packet, hdr->tp_snaplen, ctx->link_type,
876 ctx->print_mode);
877 next:
878 hdr = (void *) ((uint8_t *) hdr + hdr->tp_next_offset);
879 sll = (void *) ((uint8_t *) hdr + TPACKET_ALIGN(sizeof(*hdr)));
881 if (frame_count_max != 0) {
882 if (unlikely(*frame_count >= frame_count_max)) {
883 sigint = 1;
884 break;
888 update_pcap_next_dump(ctx, hdr->tp_snaplen, fd, sock);
891 #endif /* HAVE_TPACKET3 */
893 static void recv_only_or_dump(struct ctx *ctx)
895 short ifflags = 0;
896 int sock, ifindex, fd = 0, ret;
897 size_t size;
898 unsigned int it = 0;
899 struct ring rx_ring;
900 struct pollfd rx_poll;
901 struct sock_fprog bpf_ops;
902 struct timeval start, end, diff;
903 unsigned long frame_count = 0;
905 sock = pf_socket();
907 if (ctx->rfraw) {
908 ctx->device_trans = xstrdup(ctx->device_in);
909 xfree(ctx->device_in);
911 enter_rfmon_mac80211(ctx->device_trans, &ctx->device_in);
912 ctx->link_type = LINKTYPE_IEEE802_11;
915 ifindex = device_ifindex(ctx->device_in);
917 size = ring_size(ctx->device_in, ctx->reserve_size);
919 enable_kernel_bpf_jit_compiler();
921 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
922 if (ctx->dump_bpf)
923 bpf_dump_all(&bpf_ops);
924 bpf_attach_to_sock(sock, &bpf_ops);
926 if (ctx->hwtimestamp) {
927 ret = set_sockopt_hwtimestamp(sock, ctx->device_in);
928 if (ret == 0 && ctx->verbose)
929 printf("HW timestamping enabled\n");
932 ring_rx_setup(&rx_ring, sock, size, ifindex, &rx_poll, is_defined(HAVE_TPACKET3), true, ctx->verbose);
934 dissector_init_all(ctx->print_mode);
936 if (ctx->cpu >= 0 && ifindex > 0) {
937 int irq = device_irq_number(ctx->device_in);
938 device_set_irq_affinity(irq, ctx->cpu);
940 if (ctx->verbose)
941 printf("IRQ: %s:%d > CPU%d\n",
942 ctx->device_in, irq, ctx->cpu);
945 if (ctx->promiscuous)
946 ifflags = device_enter_promiscuous_mode(ctx->device_in);
948 if (dump_to_pcap(ctx) && __pcap_io->init_once_pcap)
949 __pcap_io->init_once_pcap();
951 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
953 if (dump_to_pcap(ctx)) {
954 struct stat stats;
956 ret = stat(ctx->device_out, &stats);
957 if (ret < 0)
958 ctx->dump_dir = 0;
959 else
960 ctx->dump_dir = S_ISDIR(stats.st_mode);
962 if (ctx->dump_dir)
963 fd = begin_multi_pcap_file(ctx);
964 else
965 fd = begin_single_pcap_file(ctx);
968 printf("Running! Hang up with ^C!\n\n");
969 fflush(stdout);
971 bug_on(gettimeofday(&start, NULL));
973 while (likely(sigint == 0)) {
974 #ifdef HAVE_TPACKET3
975 struct block_desc *pbd;
977 while (user_may_pull_from_rx_block((pbd = rx_ring.frames[it].iov_base))) {
978 walk_t3_block(pbd, ctx, sock, &fd, &frame_count);
980 kernel_may_pull_from_rx_block(pbd);
981 it = (it + 1) % rx_ring.layout3.tp_block_nr;
983 if (unlikely(sigint == 1))
984 break;
986 #else
987 while (user_may_pull_from_rx(rx_ring.frames[it].iov_base)) {
988 struct frame_map *hdr = rx_ring.frames[it].iov_base;
989 uint8_t *packet = ((uint8_t *) hdr) + hdr->tp_h.tp_mac;
990 pcap_pkthdr_t phdr;
992 if (ctx->packet_type != -1)
993 if (ctx->packet_type != hdr->s_ll.sll_pkttype)
994 goto next;
996 frame_count++;
998 if (unlikely(ring_frame_size(&rx_ring) < hdr->tp_h.tp_snaplen)) {
999 /* XXX: silently ignore for now. We used to
1000 * report them with sock_rx_net_stats() */
1001 goto next;
1004 if (dump_to_pcap(ctx)) {
1005 tpacket_hdr_to_pcap_pkthdr(&hdr->tp_h, &hdr->s_ll, &phdr, ctx->magic);
1007 ret = __pcap_io->write_pcap(fd, &phdr, ctx->magic, packet,
1008 pcap_get_length(&phdr, ctx->magic));
1009 if (unlikely(ret != (int) pcap_get_total_length(&phdr, ctx->magic)))
1010 panic("Write error to pcap!\n");
1013 show_frame_hdr(packet, hdr->tp_h.tp_snaplen,
1014 ctx->link_type, hdr, ctx->print_mode);
1016 dissector_entry_point(packet, hdr->tp_h.tp_snaplen,
1017 ctx->link_type, ctx->print_mode);
1019 if (frame_count_max != 0) {
1020 if (unlikely(frame_count >= frame_count_max)) {
1021 sigint = 1;
1022 break;
1026 next:
1027 kernel_may_pull_from_rx(&hdr->tp_h);
1028 it = (it + 1) % rx_ring.layout.tp_frame_nr;
1030 if (unlikely(sigint == 1))
1031 break;
1033 update_pcap_next_dump(ctx, hdr->tp_h.tp_snaplen, &fd, sock);
1035 #endif /* HAVE_TPACKET3 */
1037 ret = poll(&rx_poll, 1, -1);
1038 if (unlikely(ret < 0)) {
1039 if (errno != EINTR)
1040 panic("Poll failed!\n");
1044 bug_on(gettimeofday(&end, NULL));
1045 timersub(&end, &start, &diff);
1047 if (!(ctx->dump_dir && ctx->print_mode == PRINT_NONE)) {
1048 sock_rx_net_stats(sock, frame_count);
1050 printf("\r%12lu sec, %lu usec in total\n",
1051 diff.tv_sec, diff.tv_usec);
1052 } else {
1053 printf("\n\n");
1054 fflush(stdout);
1057 bpf_release(&bpf_ops);
1058 dissector_cleanup_all();
1059 destroy_rx_ring(sock, &rx_ring);
1061 if (ctx->promiscuous)
1062 device_leave_promiscuous_mode(ctx->device_in, ifflags);
1064 if (ctx->rfraw)
1065 leave_rfmon_mac80211(ctx->device_in);
1067 if (dump_to_pcap(ctx)) {
1068 if (ctx->dump_dir)
1069 finish_multi_pcap_file(ctx, fd);
1070 else
1071 finish_single_pcap_file(ctx, fd);
1074 close(sock);
1077 static void init_ctx(struct ctx *ctx)
1079 memset(ctx, 0, sizeof(*ctx));
1080 ctx->uid = getuid();
1081 ctx->uid = getgid();
1083 ctx->cpu = -1;
1084 ctx->packet_type = -1;
1086 ctx->magic = ORIGINAL_TCPDUMP_MAGIC;
1087 ctx->print_mode = PRINT_NORM;
1088 ctx->pcap = PCAP_OPS_SG;
1090 ctx->dump_mode = DUMP_INTERVAL_TIME;
1091 ctx->dump_interval = 60;
1093 ctx->promiscuous = true;
1094 ctx->randomize = false;
1095 ctx->hwtimestamp = true;
1098 static void destroy_ctx(struct ctx *ctx)
1100 free(ctx->device_in);
1101 free(ctx->device_out);
1102 free(ctx->device_trans);
1104 free(ctx->prefix);
1107 static void __noreturn help(void)
1109 printf("netsniff-ng %s, the packet sniffing beast\n", VERSION_STRING);
1110 puts("http://www.netsniff-ng.org\n\n"
1111 "Usage: netsniff-ng [options] [filter-expression]\n"
1112 "Options:\n"
1113 " -i|-d|--dev|--in <dev|pcap|-> Input source as netdev, pcap or pcap stdin\n"
1114 " -o|--out <dev|pcap|dir|cfg|-> Output sink as netdev, pcap, directory, trafgen, or stdout\n"
1115 " -f|--filter <bpf-file|expr> Use BPF filter file from bpfc or tcpdump-like expression\n"
1116 " -t|--type <type> Filter for: host|broadcast|multicast|others|outgoing\n"
1117 " -F|--interval <size|time> Dump interval if -o is a dir: <num>KiB/MiB/GiB/s/sec/min/hrs\n"
1118 " -R|--rfraw Capture or inject raw 802.11 frames\n"
1119 " -n|--num <0|uint> Number of packets until exit (def: 0)\n"
1120 " -P|--prefix <name> Prefix for pcaps stored in directory\n"
1121 " -T|--magic <pcap-magic> Pcap magic number/pcap format to store, see -D\n"
1122 " -D|--dump-pcap-types Dump pcap types and magic numbers and quit\n"
1123 " -B|--dump-bpf Dump generated BPF assembly\n"
1124 " -r|--rand Randomize packet forwarding order (dev->dev)\n"
1125 " -M|--no-promisc No promiscuous mode for netdev\n"
1126 " -A|--no-sock-mem Don't tune core socket memory\n"
1127 " -N|--no-hwtimestamp Disable hardware time stamping\n"
1128 " -m|--mmap Mmap(2) pcap file I/O, e.g. for replaying pcaps\n"
1129 " -G|--sg Scatter/gather pcap file I/O\n"
1130 " -c|--clrw Use slower read(2)/write(2) I/O\n"
1131 " -S|--ring-size <size> Specify ring size to: <num>KiB/MiB/GiB\n"
1132 " -k|--kernel-pull <uint> Kernel pull from user interval in us (def: 10us)\n"
1133 " -J|--jumbo-support Support replay/fwd 64KB Super Jumbo Frames (def: 2048B)\n"
1134 " -b|--bind-cpu <cpu> Bind to specific CPU\n"
1135 " -u|--user <userid> Drop privileges and change to userid\n"
1136 " -g|--group <groupid> Drop privileges and change to groupid\n"
1137 " -H|--prio-high Make this high priority process\n"
1138 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
1139 " -s|--silent Do not print captured packets\n"
1140 " -q|--less Print less-verbose packet information\n"
1141 " -X|--hex Print packet data in hex format\n"
1142 " -l|--ascii Print human-readable packet data\n"
1143 " -U|--update Update GeoIP databases\n"
1144 " -V|--verbose Be more verbose\n"
1145 " -v|--version Show version and exit\n"
1146 " -h|--help Guess what?!\n\n"
1147 "Examples:\n"
1148 " netsniff-ng --in eth0 --out dump.pcap -s -T 0xa1b2c3d4 --b 0 tcp or udp\n"
1149 " netsniff-ng --in wlan0 --rfraw --out dump.pcap --silent --bind-cpu 0\n"
1150 " netsniff-ng --in dump.pcap --mmap --out eth0 -k1000 --silent --bind-cpu 0\n"
1151 " netsniff-ng --in dump.pcap --out dump.cfg --silent --bind-cpu 0\n"
1152 " netsniff-ng --in dump.pcap --out dump2.pcap --silent tcp\n"
1153 " netsniff-ng --in eth0 --out eth1 --silent --bind-cpu 0 -J --type host\n"
1154 " netsniff-ng --in eth1 --out /opt/probe/ -s -m --interval 100MiB -b 0\n"
1155 " netsniff-ng --in vlan0 --out dump.pcap -c -u `id -u bob` -g `id -g bob`\n"
1156 " netsniff-ng --in any --filter http.bpf --jumbo-support --ascii -V\n\n"
1157 "Note:\n"
1158 " For introducing bit errors, delays with random variation and more\n"
1159 " while replaying pcaps, make use of tc(8) with its disciplines (e.g. netem).\n");
1160 puts(copyright);
1161 die();
1164 static void __noreturn version(void)
1166 printf("netsniff-ng %s, Git id: %s\n", VERSION_LONG, GITVERSION);
1167 puts("the packet sniffing beast\n"
1168 "http://www.netsniff-ng.org\n");
1169 puts(copyright);
1170 die();
1173 int main(int argc, char **argv)
1175 char *ptr;
1176 int c, i, j, cpu_tmp, opt_index, ops_touched = 0, vals[4] = {0};
1177 bool prio_high = false, setsockmem = true;
1178 void (*main_loop)(struct ctx *ctx) = NULL;
1179 struct ctx ctx;
1181 init_ctx(&ctx);
1182 srand(time(NULL));
1184 while ((c = getopt_long(argc, argv, short_options, long_options,
1185 &opt_index)) != EOF) {
1186 switch (c) {
1187 case 'd':
1188 case 'i':
1189 ctx.device_in = xstrdup(optarg);
1190 break;
1191 case 'o':
1192 ctx.device_out = xstrdup(optarg);
1193 break;
1194 case 'P':
1195 ctx.prefix = xstrdup(optarg);
1196 break;
1197 case 'R':
1198 ctx.link_type = LINKTYPE_IEEE802_11;
1199 ctx.rfraw = 1;
1200 break;
1201 case 'r':
1202 ctx.randomize = true;
1203 break;
1204 case 'J':
1205 ctx.jumbo = true;
1206 break;
1207 case 'T':
1208 ctx.magic = (uint32_t) strtoul(optarg, NULL, 0);
1209 pcap_check_magic(ctx.magic);
1210 break;
1211 case 'f':
1212 ctx.filter = xstrdup(optarg);
1213 break;
1214 case 'M':
1215 ctx.promiscuous = false;
1216 break;
1217 case 'N':
1218 ctx.hwtimestamp = false;
1219 break;
1220 case 'A':
1221 setsockmem = false;
1222 break;
1223 case 'u':
1224 ctx.uid = strtoul(optarg, NULL, 0);
1225 ctx.enforce = true;
1226 break;
1227 case 'g':
1228 ctx.gid = strtoul(optarg, NULL, 0);
1229 ctx.enforce = true;
1230 break;
1231 case 't':
1232 if (!strncmp(optarg, "host", strlen("host")))
1233 ctx.packet_type = PACKET_HOST;
1234 else if (!strncmp(optarg, "broadcast", strlen("broadcast")))
1235 ctx.packet_type = PACKET_BROADCAST;
1236 else if (!strncmp(optarg, "multicast", strlen("multicast")))
1237 ctx.packet_type = PACKET_MULTICAST;
1238 else if (!strncmp(optarg, "others", strlen("others")))
1239 ctx.packet_type = PACKET_OTHERHOST;
1240 else if (!strncmp(optarg, "outgoing", strlen("outgoing")))
1241 ctx.packet_type = PACKET_OUTGOING;
1242 else
1243 ctx.packet_type = -1;
1244 break;
1245 case 'S':
1246 ptr = optarg;
1247 for (j = i = strlen(optarg); i > 0; --i) {
1248 if (!isdigit(optarg[j - i]))
1249 break;
1250 ptr++;
1253 if (!strncmp(ptr, "KiB", strlen("KiB")))
1254 ctx.reserve_size = 1 << 10;
1255 else if (!strncmp(ptr, "MiB", strlen("MiB")))
1256 ctx.reserve_size = 1 << 20;
1257 else if (!strncmp(ptr, "GiB", strlen("GiB")))
1258 ctx.reserve_size = 1 << 30;
1259 else
1260 panic("Syntax error in ring size param!\n");
1262 ctx.reserve_size *= strtoul(optarg, NULL, 0);
1263 break;
1264 case 'b':
1265 cpu_tmp = strtol(optarg, NULL, 0);
1267 cpu_affinity(cpu_tmp);
1268 if (ctx.cpu != -2)
1269 ctx.cpu = cpu_tmp;
1270 break;
1271 case 'H':
1272 prio_high = true;
1273 break;
1274 case 'c':
1275 ctx.pcap = PCAP_OPS_RW;
1276 ops_touched = 1;
1277 break;
1278 case 'm':
1279 ctx.pcap = PCAP_OPS_MM;
1280 ops_touched = 1;
1281 break;
1282 case 'G':
1283 ctx.pcap = PCAP_OPS_SG;
1284 ops_touched = 1;
1285 break;
1286 case 'Q':
1287 ctx.cpu = -2;
1288 break;
1289 case 's':
1290 ctx.print_mode = PRINT_NONE;
1291 break;
1292 case 'q':
1293 ctx.print_mode = PRINT_LESS;
1294 break;
1295 case 'X':
1296 ctx.print_mode =
1297 (ctx.print_mode == PRINT_ASCII) ?
1298 PRINT_HEX_ASCII : PRINT_HEX;
1299 break;
1300 case 'l':
1301 ctx.print_mode =
1302 (ctx.print_mode == PRINT_HEX) ?
1303 PRINT_HEX_ASCII : PRINT_ASCII;
1304 break;
1305 case 'k':
1306 ctx.kpull = strtoul(optarg, NULL, 0);
1307 break;
1308 case 'n':
1309 frame_count_max = strtoul(optarg, NULL, 0);
1310 break;
1311 case 'F':
1312 ptr = optarg;
1313 for (j = i = strlen(optarg); i > 0; --i) {
1314 if (!isdigit(optarg[j - i]))
1315 break;
1316 ptr++;
1319 if (!strncmp(ptr, "KiB", strlen("KiB"))) {
1320 ctx.dump_interval = 1 << 10;
1321 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1322 } else if (!strncmp(ptr, "MiB", strlen("MiB"))) {
1323 ctx.dump_interval = 1 << 20;
1324 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1325 } else if (!strncmp(ptr, "GiB", strlen("GiB"))) {
1326 ctx.dump_interval = 1 << 30;
1327 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1328 } else if (!strncmp(ptr, "sec", strlen("sec"))) {
1329 ctx.dump_interval = 1;
1330 ctx.dump_mode = DUMP_INTERVAL_TIME;
1331 } else if (!strncmp(ptr, "min", strlen("min"))) {
1332 ctx.dump_interval = 60;
1333 ctx.dump_mode = DUMP_INTERVAL_TIME;
1334 } else if (!strncmp(ptr, "hrs", strlen("hrs"))) {
1335 ctx.dump_interval = 60 * 60;
1336 ctx.dump_mode = DUMP_INTERVAL_TIME;
1337 } else if (!strncmp(ptr, "s", strlen("s"))) {
1338 ctx.dump_interval = 1;
1339 ctx.dump_mode = DUMP_INTERVAL_TIME;
1340 } else {
1341 panic("Syntax error in time/size param!\n");
1344 ctx.dump_interval *= strtoul(optarg, NULL, 0);
1345 break;
1346 case 'V':
1347 ctx.verbose = true;
1348 break;
1349 case 'B':
1350 ctx.dump_bpf = true;
1351 break;
1352 case 'D':
1353 pcap_dump_type_features();
1354 die();
1355 break;
1356 case 'U':
1357 update_geoip();
1358 die();
1359 break;
1360 case 'v':
1361 version();
1362 break;
1363 case 'h':
1364 help();
1365 break;
1366 case '?':
1367 switch (optopt) {
1368 case 'd':
1369 case 'i':
1370 case 'o':
1371 case 'f':
1372 case 't':
1373 case 'P':
1374 case 'F':
1375 case 'n':
1376 case 'S':
1377 case 'b':
1378 case 'k':
1379 case 'T':
1380 case 'u':
1381 case 'g':
1382 case 'e':
1383 panic("Option -%c requires an argument!\n",
1384 optopt);
1385 default:
1386 if (isprint(optopt))
1387 printf("Unknown option character `0x%X\'!\n", optopt);
1388 die();
1390 default:
1391 break;
1395 if (!ctx.filter && optind != argc) {
1396 int ret;
1397 off_t offset = 0;
1399 for (i = optind; i < argc; ++i) {
1400 size_t alen = strlen(argv[i]) + 2;
1401 size_t flen = ctx.filter ? strlen(ctx.filter) : 0;
1403 ctx.filter = xrealloc(ctx.filter, flen + alen);
1404 ret = slprintf(ctx.filter + offset, strlen(argv[i]) + 2, "%s ", argv[i]);
1405 if (ret < 0)
1406 panic("Cannot concatenate filter string!\n");
1407 else
1408 offset += ret;
1412 if (!ctx.device_in)
1413 ctx.device_in = xstrdup("any");
1415 register_signal(SIGINT, signal_handler);
1416 register_signal(SIGQUIT, signal_handler);
1417 register_signal(SIGTERM, signal_handler);
1418 register_signal(SIGHUP, signal_handler);
1420 tprintf_init();
1422 if (prio_high) {
1423 set_proc_prio(-20);
1424 set_sched_status(SCHED_FIFO, sched_get_priority_max(SCHED_FIFO));
1427 if (device_mtu(ctx.device_in) || !strncmp("any", ctx.device_in, strlen(ctx.device_in))) {
1428 if (!ctx.rfraw)
1429 ctx.link_type = pcap_devtype_to_linktype(ctx.device_in);
1430 if (!ctx.device_out) {
1431 ctx.dump = 0;
1432 main_loop = recv_only_or_dump;
1433 } else if (device_mtu(ctx.device_out)) {
1434 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1435 main_loop = receive_to_xmit;
1436 } else {
1437 ctx.dump = 1;
1438 register_signal_f(SIGALRM, timer_next_dump, SA_SIGINFO);
1439 main_loop = recv_only_or_dump;
1440 if (!ops_touched)
1441 ctx.pcap = PCAP_OPS_SG;
1443 } else {
1444 if (ctx.device_out && device_mtu(ctx.device_out)) {
1445 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1446 main_loop = pcap_to_xmit;
1447 if (!ops_touched)
1448 ctx.pcap = PCAP_OPS_MM;
1449 } else {
1450 main_loop = read_pcap;
1451 if (!ops_touched)
1452 ctx.pcap = PCAP_OPS_SG;
1456 bug_on(!main_loop);
1458 init_geoip(0);
1459 if (setsockmem)
1460 set_system_socket_memory(vals, array_size(vals));
1461 if (!ctx.enforce)
1462 xlockme();
1464 if (ctx.verbose)
1465 printf("pcap file I/O method: %s\n", pcap_ops_group_to_str[ctx.pcap]);
1467 main_loop(&ctx);
1469 if (!ctx.enforce)
1470 xunlockme();
1471 if (setsockmem)
1472 reset_system_socket_memory(vals, array_size(vals));
1473 destroy_geoip();
1475 device_restore_irq_affinity_list();
1476 tprintf_cleanup();
1478 destroy_ctx(&ctx);
1479 return 0;