trafgen: Don't duplicate copyright/bug report/license string
[netsniff-ng.git] / netsniff-ng.c
blob05d1267045111fe52afd6cd8feae8933ce8b499a
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 int tx_sock;
109 static struct itimerval itimer;
110 static unsigned long frame_count_max = 0, interval = TX_KERNEL_PULL_INT;
112 #define __pcap_io pcap_ops[ctx->pcap]
114 static void signal_handler(int number)
116 switch (number) {
117 case SIGINT:
118 case SIGQUIT:
119 case SIGTERM:
120 sigint = 1;
121 case SIGHUP:
122 default:
123 break;
127 static void timer_elapsed(int unused __maybe_unused)
129 int ret;
131 set_itimer_interval_value(&itimer, 0, interval);
133 ret = pull_and_flush_tx_ring(tx_sock);
134 if (unlikely(ret < 0)) {
135 /* We could hit EBADF if the socket has been closed before
136 * the timer was triggered.
138 if (errno != EBADF && errno != ENOBUFS)
139 panic("Flushing TX_RING failed: %s!\n", strerror(errno));
142 setitimer(ITIMER_REAL, &itimer, NULL);
145 static void timer_purge(void)
147 int ret;
149 ret = pull_and_flush_tx_ring_wait(tx_sock);
150 if (unlikely(ret < 0)) {
151 if (errno != EBADF && errno != ENOBUFS)
152 panic("Flushing TX_RING failed: %s!\n", strerror(errno));
155 set_itimer_interval_value(&itimer, 0, 0);
156 setitimer(ITIMER_REAL, &itimer, NULL);
159 static void timer_next_dump(int unused __maybe_unused)
161 set_itimer_interval_value(&itimer, interval, 0);
162 next_dump = true;
163 setitimer(ITIMER_REAL, &itimer, NULL);
166 static inline bool dump_to_pcap(struct ctx *ctx)
168 return ctx->dump;
171 static void pcap_to_xmit(struct ctx *ctx)
173 uint8_t *out = NULL;
174 int ifindex, fd = 0, ret;
175 size_t size;
176 unsigned int it = 0;
177 unsigned long trunced = 0;
178 struct ring tx_ring;
179 struct frame_map *hdr;
180 struct sock_fprog bpf_ops;
181 struct timeval start, end, diff;
182 pcap_pkthdr_t phdr;
184 if (!device_up_and_running(ctx->device_out) && !ctx->rfraw)
185 panic("Device not up and running!\n");
187 bug_on(!__pcap_io);
189 tx_sock = pf_socket();
191 if (!strncmp("-", ctx->device_in, strlen("-"))) {
192 fd = dup_or_die(fileno(stdin));
193 close(fileno(stdin));
194 if (ctx->pcap == PCAP_OPS_MM)
195 ctx->pcap = PCAP_OPS_SG;
196 } else {
197 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
200 if (__pcap_io->init_once_pcap)
201 __pcap_io->init_once_pcap();
203 ret = __pcap_io->pull_fhdr_pcap(fd, &ctx->magic, &ctx->link_type);
204 if (ret)
205 panic("Error reading pcap header!\n");
207 if (__pcap_io->prepare_access_pcap) {
208 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_RD, ctx->jumbo);
209 if (ret)
210 panic("Error prepare reading pcap!\n");
213 if (ctx->rfraw) {
214 ctx->device_trans = xstrdup(ctx->device_out);
215 xfree(ctx->device_out);
217 enter_rfmon_mac80211(ctx->device_trans, &ctx->device_out);
218 if (ctx->link_type != LINKTYPE_IEEE802_11)
219 panic("Wrong linktype of pcap!\n");
222 ifindex = device_ifindex(ctx->device_out);
223 size = ring_size(ctx->device_out, ctx->reserve_size);
225 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
226 if (ctx->dump_bpf)
227 bpf_dump_all(&bpf_ops);
229 ring_tx_setup(&tx_ring, tx_sock, size, ifindex, ctx->jumbo, ctx->verbose);
231 dissector_init_all(ctx->print_mode);
233 if (ctx->cpu >= 0 && ifindex > 0) {
234 int irq = device_irq_number(ctx->device_out);
235 device_set_irq_affinity(irq, ctx->cpu);
237 if (ctx->verbose)
238 printf("IRQ: %s:%d > CPU%d\n",
239 ctx->device_out, irq, ctx->cpu);
242 if (ctx->kpull)
243 interval = ctx->kpull;
245 set_itimer_interval_value(&itimer, 0, interval);
246 setitimer(ITIMER_REAL, &itimer, NULL);
248 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
250 printf("Running! Hang up with ^C!\n\n");
251 fflush(stdout);
253 bug_on(gettimeofday(&start, NULL));
255 while (likely(sigint == 0)) {
256 while (user_may_pull_from_tx(tx_ring.frames[it].iov_base)) {
257 hdr = tx_ring.frames[it].iov_base;
258 out = ((uint8_t *) hdr) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
260 do {
261 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic, out,
262 ring_frame_size(&tx_ring));
263 if (unlikely(ret <= 0))
264 goto out;
266 if (ring_frame_size(&tx_ring) <
267 pcap_get_length(&phdr, ctx->magic)) {
268 pcap_set_length(&phdr, ctx->magic,
269 ring_frame_size(&tx_ring));
270 trunced++;
272 } while (ctx->filter &&
273 !bpf_run_filter(&bpf_ops, out,
274 pcap_get_length(&phdr, ctx->magic)));
276 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &hdr->tp_h, NULL);
278 ctx->tx_bytes += hdr->tp_h.tp_len;;
279 ctx->tx_packets++;
281 show_frame_hdr(out, hdr->tp_h.tp_snaplen,
282 ctx->link_type, hdr, ctx->print_mode);
284 dissector_entry_point(out, hdr->tp_h.tp_snaplen,
285 ctx->link_type, ctx->print_mode);
287 kernel_may_pull_from_tx(&hdr->tp_h);
289 it++;
290 if (it >= tx_ring.layout.tp_frame_nr)
291 it = 0;
293 if (unlikely(sigint == 1))
294 break;
296 if (frame_count_max != 0) {
297 if (ctx->tx_packets >= frame_count_max) {
298 sigint = 1;
299 break;
305 out:
306 bug_on(gettimeofday(&end, NULL));
307 timersub(&end, &start, &diff);
309 timer_purge();
311 bpf_release(&bpf_ops);
313 dissector_cleanup_all();
314 destroy_tx_ring(tx_sock, &tx_ring);
316 if (ctx->rfraw)
317 leave_rfmon_mac80211(ctx->device_out);
319 if (__pcap_io->prepare_close_pcap)
320 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
322 if (!strncmp("-", ctx->device_in, strlen("-")))
323 dup2(fd, fileno(stdin));
324 close(fd);
326 close(tx_sock);
328 fflush(stdout);
329 printf("\n");
330 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
331 printf("\r%12lu packets truncated in file\n", trunced);
332 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
333 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
336 static void receive_to_xmit(struct ctx *ctx)
338 short ifflags = 0;
339 uint8_t *in, *out;
340 int rx_sock, ifindex_in, ifindex_out, ret;
341 size_t size_in, size_out;
342 unsigned int it_in = 0, it_out = 0;
343 unsigned long frame_count = 0;
344 struct frame_map *hdr_in, *hdr_out;
345 struct ring tx_ring, rx_ring;
346 struct pollfd rx_poll;
347 struct sock_fprog bpf_ops;
349 if (!strncmp(ctx->device_in, ctx->device_out, IFNAMSIZ))
350 panic("Ingress/egress devices must be different!\n");
351 if (!device_up_and_running(ctx->device_out))
352 panic("Egress device not up and running!\n");
354 rx_sock = pf_socket();
355 tx_sock = pf_socket();
357 ifindex_in = device_ifindex(ctx->device_in);
358 ifindex_out = device_ifindex(ctx->device_out);
360 size_in = ring_size(ctx->device_in, ctx->reserve_size);
361 size_out = ring_size(ctx->device_out, ctx->reserve_size);
363 enable_kernel_bpf_jit_compiler();
365 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
366 if (ctx->dump_bpf)
367 bpf_dump_all(&bpf_ops);
368 bpf_attach_to_sock(rx_sock, &bpf_ops);
370 ring_rx_setup(&rx_ring, rx_sock, size_in, ifindex_in, &rx_poll, false, ctx->jumbo, ctx->verbose);
371 ring_tx_setup(&tx_ring, tx_sock, size_out, ifindex_out, ctx->jumbo, ctx->verbose);
373 dissector_init_all(ctx->print_mode);
375 if (ctx->promiscuous)
376 ifflags = device_enter_promiscuous_mode(ctx->device_in);
378 if (ctx->kpull)
379 interval = ctx->kpull;
381 set_itimer_interval_value(&itimer, 0, interval);
382 setitimer(ITIMER_REAL, &itimer, NULL);
384 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
386 printf("Running! Hang up with ^C!\n\n");
387 fflush(stdout);
389 while (likely(sigint == 0)) {
390 while (user_may_pull_from_rx(rx_ring.frames[it_in].iov_base)) {
391 hdr_in = rx_ring.frames[it_in].iov_base;
392 in = ((uint8_t *) hdr_in) + hdr_in->tp_h.tp_mac;
394 frame_count++;
396 if (ctx->packet_type != -1)
397 if (ctx->packet_type != hdr_in->s_ll.sll_pkttype)
398 goto next;
400 hdr_out = tx_ring.frames[it_out].iov_base;
401 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
403 while (!user_may_pull_from_tx(tx_ring.frames[it_out].iov_base) &&
404 likely(!sigint)) {
405 if (ctx->randomize)
406 next_rnd_slot(&it_out, &tx_ring);
407 else {
408 it_out++;
409 if (it_out >= tx_ring.layout.tp_frame_nr)
410 it_out = 0;
413 hdr_out = tx_ring.frames[it_out].iov_base;
414 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
417 tpacket_hdr_clone(&hdr_out->tp_h, &hdr_in->tp_h);
418 fmemcpy(out, in, hdr_in->tp_h.tp_len);
420 kernel_may_pull_from_tx(&hdr_out->tp_h);
421 if (ctx->randomize)
422 next_rnd_slot(&it_out, &tx_ring);
423 else {
424 it_out++;
425 if (it_out >= tx_ring.layout.tp_frame_nr)
426 it_out = 0;
429 show_frame_hdr(in, hdr_in->tp_h.tp_snaplen,
430 ctx->link_type, hdr_in, ctx->print_mode);
432 dissector_entry_point(in, hdr_in->tp_h.tp_snaplen,
433 ctx->link_type, ctx->print_mode);
435 if (frame_count_max != 0) {
436 if (frame_count >= frame_count_max) {
437 sigint = 1;
438 break;
442 next:
443 kernel_may_pull_from_rx(&hdr_in->tp_h);
445 it_in++;
446 if (it_in >= rx_ring.layout.tp_frame_nr)
447 it_in = 0;
449 if (unlikely(sigint == 1))
450 goto out;
453 ret = poll(&rx_poll, 1, -1);
454 if (unlikely(ret < 0)) {
455 if (errno != EINTR)
456 panic("Poll failed!\n");
460 out:
461 timer_purge();
463 sock_rx_net_stats(rx_sock, 0);
465 bpf_release(&bpf_ops);
467 dissector_cleanup_all();
469 destroy_tx_ring(tx_sock, &tx_ring);
470 destroy_rx_ring(rx_sock, &rx_ring);
472 if (ctx->promiscuous)
473 device_leave_promiscuous_mode(ctx->device_in, ifflags);
475 close(tx_sock);
476 close(rx_sock);
479 static void translate_pcap_to_txf(int fdo, uint8_t *out, size_t len)
481 size_t bytes_done = 0;
482 char bout[80];
484 slprintf(bout, sizeof(bout), "{\n ");
485 write_or_die(fdo, bout, strlen(bout));
487 while (bytes_done < len) {
488 slprintf(bout, sizeof(bout), "0x%02x, ", out[bytes_done]);
489 write_or_die(fdo, bout, strlen(bout));
491 bytes_done++;
493 if (bytes_done % 10 == 0) {
494 slprintf(bout, sizeof(bout), "\n");
495 write_or_die(fdo, bout, strlen(bout));
497 if (bytes_done < len) {
498 slprintf(bout, sizeof(bout), " ");
499 write_or_die(fdo, bout, strlen(bout));
503 if (bytes_done % 10 != 0) {
504 slprintf(bout, sizeof(bout), "\n");
505 write_or_die(fdo, bout, strlen(bout));
508 slprintf(bout, sizeof(bout), "}\n\n");
509 write_or_die(fdo, bout, strlen(bout));
512 static void read_pcap(struct ctx *ctx)
514 uint8_t *out;
515 int ret, fd, fdo = 0;
516 unsigned long trunced = 0;
517 size_t out_len;
518 pcap_pkthdr_t phdr;
519 struct sock_fprog bpf_ops;
520 struct frame_map fm;
521 struct timeval start, end, diff;
523 bug_on(!__pcap_io);
525 if (!strncmp("-", ctx->device_in, strlen("-"))) {
526 fd = dup_or_die(fileno(stdin));
527 close(fileno(stdin));
528 if (ctx->pcap == PCAP_OPS_MM)
529 ctx->pcap = PCAP_OPS_SG;
530 } else {
531 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
534 if (__pcap_io->init_once_pcap)
535 __pcap_io->init_once_pcap();
537 ret = __pcap_io->pull_fhdr_pcap(fd, &ctx->magic, &ctx->link_type);
538 if (ret)
539 panic("Error reading pcap header!\n");
541 if (__pcap_io->prepare_access_pcap) {
542 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_RD, ctx->jumbo);
543 if (ret)
544 panic("Error prepare reading pcap!\n");
547 fmemset(&fm, 0, sizeof(fm));
549 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
550 if (ctx->dump_bpf)
551 bpf_dump_all(&bpf_ops);
553 dissector_init_all(ctx->print_mode);
555 out_len = round_up(1024 * 1024, RUNTIME_PAGE_SIZE);
556 out = xmalloc_aligned(out_len, CO_CACHE_LINE_SIZE);
558 if (ctx->device_out) {
559 if (!strncmp("-", ctx->device_out, strlen("-"))) {
560 fdo = dup_or_die(fileno(stdout));
561 close(fileno(stdout));
562 } else {
563 fdo = open_or_die_m(ctx->device_out, O_RDWR | O_CREAT |
564 O_TRUNC | O_LARGEFILE, DEFFILEMODE);
568 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
570 printf("Running! Hang up with ^C!\n\n");
571 fflush(stdout);
573 bug_on(gettimeofday(&start, NULL));
575 while (likely(sigint == 0)) {
576 do {
577 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic,
578 out, out_len);
579 if (unlikely(ret < 0))
580 goto out;
582 if (unlikely(pcap_get_length(&phdr, ctx->magic) == 0)) {
583 trunced++;
584 continue;
587 if (unlikely(pcap_get_length(&phdr, ctx->magic) > out_len)) {
588 pcap_set_length(&phdr, ctx->magic, out_len);
589 trunced++;
591 } while (ctx->filter &&
592 !bpf_run_filter(&bpf_ops, out,
593 pcap_get_length(&phdr, ctx->magic)));
595 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &fm.tp_h, &fm.s_ll);
597 ctx->tx_bytes += fm.tp_h.tp_len;
598 ctx->tx_packets++;
600 show_frame_hdr(out, fm.tp_h.tp_snaplen, ctx->link_type, &fm,
601 ctx->print_mode);
603 dissector_entry_point(out, fm.tp_h.tp_snaplen,
604 ctx->link_type, ctx->print_mode);
606 if (ctx->device_out)
607 translate_pcap_to_txf(fdo, out, fm.tp_h.tp_snaplen);
609 if (frame_count_max != 0) {
610 if (ctx->tx_packets >= frame_count_max) {
611 sigint = 1;
612 break;
617 out:
619 bug_on(gettimeofday(&end, NULL));
620 timersub(&end, &start, &diff);
622 bpf_release(&bpf_ops);
624 dissector_cleanup_all();
626 if (__pcap_io->prepare_close_pcap)
627 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
629 xfree(out);
631 fflush(stdout);
632 printf("\n");
633 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
634 printf("\r%12lu packets truncated in file\n", trunced);
635 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
636 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
638 if (!strncmp("-", ctx->device_in, strlen("-")))
639 dup2(fd, fileno(stdin));
640 close(fd);
642 if (ctx->device_out) {
643 if (!strncmp("-", ctx->device_out, strlen("-")))
644 dup2(fdo, fileno(stdout));
645 close(fdo);
649 static void finish_multi_pcap_file(struct ctx *ctx, int fd)
651 __pcap_io->fsync_pcap(fd);
653 if (__pcap_io->prepare_close_pcap)
654 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
656 close(fd);
658 fmemset(&itimer, 0, sizeof(itimer));
659 setitimer(ITIMER_REAL, &itimer, NULL);
662 static int next_multi_pcap_file(struct ctx *ctx, int fd)
664 int ret;
665 char fname[512];
667 __pcap_io->fsync_pcap(fd);
669 if (__pcap_io->prepare_close_pcap)
670 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
672 close(fd);
674 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
675 ctx->prefix ? : "dump-", time(NULL));
677 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
678 O_LARGEFILE, DEFFILEMODE);
680 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
681 if (ret)
682 panic("Error writing pcap header!\n");
684 if (__pcap_io->prepare_access_pcap) {
685 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, true);
686 if (ret)
687 panic("Error prepare writing pcap!\n");
690 return fd;
693 static int begin_multi_pcap_file(struct ctx *ctx)
695 int fd, ret;
696 char fname[256];
698 bug_on(!__pcap_io);
700 if (ctx->device_out[strlen(ctx->device_out) - 1] == '/')
701 ctx->device_out[strlen(ctx->device_out) - 1] = 0;
703 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
704 ctx->prefix ? : "dump-", time(NULL));
706 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
707 O_LARGEFILE, DEFFILEMODE);
709 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
710 if (ret)
711 panic("Error writing pcap header!\n");
713 if (__pcap_io->prepare_access_pcap) {
714 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, true);
715 if (ret)
716 panic("Error prepare writing pcap!\n");
719 if (ctx->dump_mode == DUMP_INTERVAL_TIME) {
720 interval = ctx->dump_interval;
722 set_itimer_interval_value(&itimer, interval, 0);
723 setitimer(ITIMER_REAL, &itimer, NULL);
724 } else {
725 interval = 0;
728 return fd;
731 static void finish_single_pcap_file(struct ctx *ctx, int fd)
733 __pcap_io->fsync_pcap(fd);
735 if (__pcap_io->prepare_close_pcap)
736 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
738 if (strncmp("-", ctx->device_out, strlen("-")))
739 close(fd);
740 else
741 dup2(fd, fileno(stdout));
744 static int begin_single_pcap_file(struct ctx *ctx)
746 int fd, ret;
748 bug_on(!__pcap_io);
750 if (!strncmp("-", ctx->device_out, strlen("-"))) {
751 fd = dup_or_die(fileno(stdout));
752 close(fileno(stdout));
753 if (ctx->pcap == PCAP_OPS_MM)
754 ctx->pcap = PCAP_OPS_SG;
755 } else {
756 fd = open_or_die_m(ctx->device_out,
757 O_RDWR | O_CREAT | O_TRUNC |
758 O_LARGEFILE, DEFFILEMODE);
761 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
762 if (ret)
763 panic("Error writing pcap header!\n");
765 if (__pcap_io->prepare_access_pcap) {
766 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, true);
767 if (ret)
768 panic("Error prepare writing pcap!\n");
771 return fd;
774 static void print_pcap_file_stats(int sock, struct ctx *ctx)
776 int ret;
777 struct tpacket_stats kstats;
778 socklen_t slen = sizeof(kstats);
780 fmemset(&kstats, 0, sizeof(kstats));
782 ret = getsockopt(sock, SOL_PACKET, PACKET_STATISTICS, &kstats, &slen);
783 if (unlikely(ret))
784 panic("Cannot get packet statistics!\n");
786 if (ctx->print_mode == PRINT_NONE) {
787 printf(".(+%u/-%u)", kstats.tp_packets - kstats.tp_drops,
788 kstats.tp_drops);
789 fflush(stdout);
793 #ifdef HAVE_TPACKET3
794 static void walk_t3_block(struct block_desc *pbd, struct ctx *ctx,
795 int sock, int *fd, unsigned long *frame_count)
797 int num_pkts = pbd->h1.num_pkts, i;
798 struct tpacket3_hdr *hdr;
799 struct sockaddr_ll *sll;
801 hdr = (void *) ((uint8_t *) pbd + pbd->h1.offset_to_first_pkt);
802 sll = (void *) ((uint8_t *) hdr + TPACKET_ALIGN(sizeof(*hdr)));
804 for (i = 0; i < num_pkts && likely(sigint == 0); ++i) {
805 uint8_t *packet = ((uint8_t *) hdr + hdr->tp_mac);
806 pcap_pkthdr_t phdr;
808 if (ctx->packet_type != -1)
809 if (ctx->packet_type != sll->sll_pkttype)
810 goto next;
812 (*frame_count)++;
814 if (dump_to_pcap(ctx)) {
815 int ret;
817 tpacket3_hdr_to_pcap_pkthdr(hdr, sll, &phdr, ctx->magic);
819 ret = __pcap_io->write_pcap(*fd, &phdr, ctx->magic, packet,
820 pcap_get_length(&phdr, ctx->magic));
821 if (unlikely(ret != (int) pcap_get_total_length(&phdr, ctx->magic)))
822 panic("Write error to pcap!\n");
825 __show_frame_hdr(packet, hdr->tp_snaplen, ctx->link_type, sll,
826 hdr, ctx->print_mode, true);
828 dissector_entry_point(packet, hdr->tp_snaplen, ctx->link_type,
829 ctx->print_mode);
830 next:
831 hdr = (void *) ((uint8_t *) hdr + hdr->tp_next_offset);
832 sll = (void *) ((uint8_t *) hdr + TPACKET_ALIGN(sizeof(*hdr)));
834 if (frame_count_max != 0) {
835 if (unlikely(*frame_count >= frame_count_max)) {
836 sigint = 1;
837 break;
841 if (dump_to_pcap(ctx)) {
842 if (ctx->dump_mode == DUMP_INTERVAL_SIZE) {
843 interval += hdr->tp_snaplen;
844 if (interval > ctx->dump_interval) {
845 next_dump = true;
846 interval = 0;
850 if (next_dump) {
851 *fd = next_multi_pcap_file(ctx, *fd);
852 next_dump = false;
854 if (unlikely(ctx->verbose))
855 print_pcap_file_stats(sock, ctx);
860 #endif /* HAVE_TPACKET3 */
862 static void recv_only_or_dump(struct ctx *ctx)
864 short ifflags = 0;
865 int sock, ifindex, fd = 0, ret;
866 size_t size;
867 unsigned int it = 0;
868 struct ring rx_ring;
869 struct pollfd rx_poll;
870 struct sock_fprog bpf_ops;
871 struct timeval start, end, diff;
872 unsigned long frame_count = 0;
873 #ifdef HAVE_TPACKET3
874 struct block_desc *pbd;
875 #endif
877 sock = pf_socket();
879 if (ctx->rfraw) {
880 ctx->device_trans = xstrdup(ctx->device_in);
881 xfree(ctx->device_in);
883 enter_rfmon_mac80211(ctx->device_trans, &ctx->device_in);
884 ctx->link_type = LINKTYPE_IEEE802_11;
887 ifindex = device_ifindex(ctx->device_in);
889 size = ring_size(ctx->device_in, ctx->reserve_size);
891 enable_kernel_bpf_jit_compiler();
893 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
894 if (ctx->dump_bpf)
895 bpf_dump_all(&bpf_ops);
896 bpf_attach_to_sock(sock, &bpf_ops);
898 if (ctx->hwtimestamp) {
899 ret = set_sockopt_hwtimestamp(sock, ctx->device_in);
900 if (ret == 0 && ctx->verbose)
901 printf("HW timestamping enabled\n");
904 ring_rx_setup(&rx_ring, sock, size, ifindex, &rx_poll, true, true, ctx->verbose);
906 dissector_init_all(ctx->print_mode);
908 if (ctx->cpu >= 0 && ifindex > 0) {
909 int irq = device_irq_number(ctx->device_in);
910 device_set_irq_affinity(irq, ctx->cpu);
912 if (ctx->verbose)
913 printf("IRQ: %s:%d > CPU%d\n",
914 ctx->device_in, irq, ctx->cpu);
917 if (ctx->promiscuous)
918 ifflags = device_enter_promiscuous_mode(ctx->device_in);
920 if (dump_to_pcap(ctx) && __pcap_io->init_once_pcap)
921 __pcap_io->init_once_pcap();
923 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
925 if (dump_to_pcap(ctx)) {
926 struct stat stats;
928 ret = stat(ctx->device_out, &stats);
929 if (ret < 0)
930 ctx->dump_dir = 0;
931 else
932 ctx->dump_dir = S_ISDIR(stats.st_mode);
934 if (ctx->dump_dir)
935 fd = begin_multi_pcap_file(ctx);
936 else
937 fd = begin_single_pcap_file(ctx);
940 printf("Running! Hang up with ^C!\n\n");
941 fflush(stdout);
943 bug_on(gettimeofday(&start, NULL));
945 while (likely(sigint == 0)) {
946 #ifdef HAVE_TPACKET3
947 while (user_may_pull_from_rx_block((pbd = (void *)
948 rx_ring.frames[it].iov_base))) {
949 walk_t3_block(pbd, ctx, sock, &fd, &frame_count);
951 kernel_may_pull_from_rx_block(pbd);
952 it = (it + 1) % rx_ring.layout3.tp_block_nr;
954 if (unlikely(sigint == 1))
955 break;
957 #else
958 while (user_may_pull_from_rx(rx_ring.frames[it].iov_base)) {
959 struct frame_map *hdr = rx_ring.frames[it].iov_base;
960 uint8_t *packet = ((uint8_t *) hdr) + hdr->tp_h.tp_mac;
961 pcap_pkthdr_t phdr;
963 if (ctx->packet_type != -1)
964 if (ctx->packet_type != hdr->s_ll.sll_pkttype)
965 goto next;
967 frame_count++;
969 if (unlikely(ring_frame_size(&rx_ring) < hdr->tp_h.tp_snaplen)) {
970 /* XXX: silently ignore for now. We used to
971 * report them with sock_rx_net_stats() */
972 goto next;
975 if (dump_to_pcap(ctx)) {
976 tpacket_hdr_to_pcap_pkthdr(&hdr->tp_h, &hdr->s_ll, &phdr, ctx->magic);
978 ret = __pcap_io->write_pcap(fd, &phdr, ctx->magic, packet,
979 pcap_get_length(&phdr, ctx->magic));
980 if (unlikely(ret != (int) pcap_get_total_length(&phdr, ctx->magic)))
981 panic("Write error to pcap!\n");
984 show_frame_hdr(packet, hdr->tp_h.tp_snaplen,
985 ctx->link_type, hdr, ctx->print_mode);
987 dissector_entry_point(packet, hdr->tp_h.tp_snaplen,
988 ctx->link_type, ctx->print_mode);
990 if (frame_count_max != 0) {
991 if (unlikely(frame_count >= frame_count_max)) {
992 sigint = 1;
993 break;
997 next:
998 kernel_may_pull_from_rx(&hdr->tp_h);
999 it = (it + 1) % rx_ring.layout.tp_frame_nr;
1001 if (unlikely(sigint == 1))
1002 break;
1004 if (dump_to_pcap(ctx)) {
1005 if (ctx->dump_mode == DUMP_INTERVAL_SIZE) {
1006 interval += hdr->tp_h.tp_snaplen;
1007 if (interval > ctx->dump_interval) {
1008 next_dump = true;
1009 interval = 0;
1013 if (next_dump) {
1014 fd = next_multi_pcap_file(ctx, fd);
1015 next_dump = false;
1017 if (unlikely(ctx->verbose))
1018 print_pcap_file_stats(sock, ctx);
1022 #endif /* HAVE_TPACKET3 */
1024 ret = poll(&rx_poll, 1, -1);
1025 if (unlikely(ret < 0)) {
1026 if (errno != EINTR)
1027 panic("Poll failed!\n");
1031 bug_on(gettimeofday(&end, NULL));
1032 timersub(&end, &start, &diff);
1034 if (!(ctx->dump_dir && ctx->print_mode == PRINT_NONE)) {
1035 sock_rx_net_stats(sock, frame_count);
1037 printf("\r%12lu sec, %lu usec in total\n",
1038 diff.tv_sec, diff.tv_usec);
1039 } else {
1040 printf("\n\n");
1041 fflush(stdout);
1044 bpf_release(&bpf_ops);
1045 dissector_cleanup_all();
1046 destroy_rx_ring(sock, &rx_ring);
1048 if (ctx->promiscuous)
1049 device_leave_promiscuous_mode(ctx->device_in, ifflags);
1051 if (ctx->rfraw)
1052 leave_rfmon_mac80211(ctx->device_in);
1054 if (dump_to_pcap(ctx)) {
1055 if (ctx->dump_dir)
1056 finish_multi_pcap_file(ctx, fd);
1057 else
1058 finish_single_pcap_file(ctx, fd);
1061 close(sock);
1064 static void init_ctx(struct ctx *ctx)
1066 memset(ctx, 0, sizeof(*ctx));
1067 ctx->uid = getuid();
1068 ctx->uid = getgid();
1070 ctx->cpu = -1;
1071 ctx->packet_type = -1;
1073 ctx->magic = ORIGINAL_TCPDUMP_MAGIC;
1074 ctx->print_mode = PRINT_NORM;
1075 ctx->pcap = PCAP_OPS_SG;
1077 ctx->dump_mode = DUMP_INTERVAL_TIME;
1078 ctx->dump_interval = 60;
1080 ctx->promiscuous = true;
1081 ctx->randomize = false;
1082 ctx->hwtimestamp = true;
1085 static void destroy_ctx(struct ctx *ctx)
1087 free(ctx->device_in);
1088 free(ctx->device_out);
1089 free(ctx->device_trans);
1091 free(ctx->prefix);
1094 static void __noreturn help(void)
1096 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING);
1097 puts("http://www.netsniff-ng.org\n\n"
1098 "Usage: netsniff-ng [options] [filter-expression]\n"
1099 "Options:\n"
1100 " -i|-d|--dev|--in <dev|pcap|-> Input source as netdev, pcap or pcap stdin\n"
1101 " -o|--out <dev|pcap|dir|cfg|-> Output sink as netdev, pcap, directory, trafgen, or stdout\n"
1102 " -f|--filter <bpf-file|expr> Use BPF filter file from bpfc or tcpdump-like expression\n"
1103 " -t|--type <type> Filter for: host|broadcast|multicast|others|outgoing\n"
1104 " -F|--interval <size|time> Dump interval if -o is a dir: <num>KiB/MiB/GiB/s/sec/min/hrs\n"
1105 " -R|--rfraw Capture or inject raw 802.11 frames\n"
1106 " -n|--num <0|uint> Number of packets until exit (def: 0)\n"
1107 " -P|--prefix <name> Prefix for pcaps stored in directory\n"
1108 " -T|--magic <pcap-magic> Pcap magic number/pcap format to store, see -D\n"
1109 " -D|--dump-pcap-types Dump pcap types and magic numbers and quit\n"
1110 " -B|--dump-bpf Dump generated BPF assembly\n"
1111 " -r|--rand Randomize packet forwarding order (dev->dev)\n"
1112 " -M|--no-promisc No promiscuous mode for netdev\n"
1113 " -A|--no-sock-mem Don't tune core socket memory\n"
1114 " -N|--no-hwtimestamp Disable hardware time stamping\n"
1115 " -m|--mmap Mmap(2) pcap file I/O, e.g. for replaying pcaps\n"
1116 " -G|--sg Scatter/gather pcap file I/O\n"
1117 " -c|--clrw Use slower read(2)/write(2) I/O\n"
1118 " -S|--ring-size <size> Specify ring size to: <num>KiB/MiB/GiB\n"
1119 " -k|--kernel-pull <uint> Kernel pull from user interval in us (def: 10us)\n"
1120 " -J|--jumbo-support Support replay/fwd 64KB Super Jumbo Frames (def: 2048B)\n"
1121 " -b|--bind-cpu <cpu> Bind to specific CPU\n"
1122 " -u|--user <userid> Drop privileges and change to userid\n"
1123 " -g|--group <groupid> Drop privileges and change to groupid\n"
1124 " -H|--prio-high Make this high priority process\n"
1125 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
1126 " -s|--silent Do not print captured packets\n"
1127 " -q|--less Print less-verbose packet information\n"
1128 " -X|--hex Print packet data in hex format\n"
1129 " -l|--ascii Print human-readable packet data\n"
1130 " -U|--update Update GeoIP databases\n"
1131 " -V|--verbose Be more verbose\n"
1132 " -v|--version Show version and exit\n"
1133 " -h|--help Guess what?!\n\n"
1134 "Examples:\n"
1135 " netsniff-ng --in eth0 --out dump.pcap -s -T 0xa1b2c3d4 --b 0 tcp or udp\n"
1136 " netsniff-ng --in wlan0 --rfraw --out dump.pcap --silent --bind-cpu 0\n"
1137 " netsniff-ng --in dump.pcap --mmap --out eth0 -k1000 --silent --bind-cpu 0\n"
1138 " netsniff-ng --in dump.pcap --out dump.cfg --silent --bind-cpu 0\n"
1139 " netsniff-ng --in eth0 --out eth1 --silent --bind-cpu 0 -J --type host\n"
1140 " netsniff-ng --in eth1 --out /opt/probe/ -s -m --interval 100MiB -b 0\n"
1141 " netsniff-ng --in vlan0 --out dump.pcap -c -u `id -u bob` -g `id -g bob`\n"
1142 " netsniff-ng --in any --filter http.bpf --jumbo-support --ascii -V\n\n"
1143 "Note:\n"
1144 " For introducing bit errors, delays with random variation and more\n"
1145 " while replaying pcaps, make use of tc(8) with its disciplines (e.g. netem).\n\n"
1146 "Please report bugs to <bugs@netsniff-ng.org>\n"
1147 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1148 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1149 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1150 "Swiss federal institute of technology (ETH Zurich)\n"
1151 "License: GNU GPL version 2.0\n"
1152 "This is free software: you are free to change and redistribute it.\n"
1153 "There is NO WARRANTY, to the extent permitted by law.\n");
1154 die();
1157 static void __noreturn version(void)
1159 printf("\nnetsniff-ng %s, Git id: %s\n", VERSION_LONG, GITVERSION);
1160 puts("the packet sniffing beast\n"
1161 "http://www.netsniff-ng.org\n\n"
1162 "Please report bugs to <bugs@netsniff-ng.org>\n"
1163 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1164 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1165 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1166 "Swiss federal institute of technology (ETH Zurich)\n"
1167 "License: GNU GPL version 2.0\n"
1168 "This is free software: you are free to change and redistribute it.\n"
1169 "There is NO WARRANTY, to the extent permitted by law.\n");
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, 1, 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 (ctx.device_in && (device_mtu(ctx.device_in) ||
1428 !strncmp("any", ctx.device_in, strlen(ctx.device_in)))) {
1429 if (!ctx.rfraw)
1430 ctx.link_type = pcap_devtype_to_linktype(ctx.device_in);
1431 if (!ctx.device_out) {
1432 ctx.dump = 0;
1433 main_loop = recv_only_or_dump;
1434 } else if (device_mtu(ctx.device_out)) {
1435 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1436 main_loop = receive_to_xmit;
1437 } else {
1438 ctx.dump = 1;
1439 register_signal_f(SIGALRM, timer_next_dump, SA_SIGINFO);
1440 main_loop = recv_only_or_dump;
1441 if (!ops_touched)
1442 ctx.pcap = PCAP_OPS_SG;
1444 } else {
1445 if (ctx.device_out && device_mtu(ctx.device_out)) {
1446 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1447 main_loop = pcap_to_xmit;
1448 if (!ops_touched)
1449 ctx.pcap = PCAP_OPS_MM;
1450 } else {
1451 main_loop = read_pcap;
1452 if (!ops_touched)
1453 ctx.pcap = PCAP_OPS_SG;
1457 bug_on(!main_loop);
1459 init_geoip(0);
1460 if (setsockmem)
1461 set_system_socket_memory(vals, array_size(vals));
1462 if (!ctx.enforce)
1463 xlockme();
1465 if (ctx.verbose)
1466 printf("pcap file I/O method: %s\n", pcap_ops_group_to_str[ctx.pcap]);
1468 main_loop(&ctx);
1470 if (!ctx.enforce)
1471 xunlockme();
1472 if (setsockmem)
1473 reset_system_socket_memory(vals, array_size(vals));
1474 destroy_geoip();
1476 device_restore_irq_affinity_list();
1477 tprintf_cleanup();
1479 destroy_ctx(&ctx);
1480 return 0;