netsniff-ng: add comment wrt NOATIME and fix whitespace
[netsniff-ng.git] / netsniff-ng.c
blobafa522f0e4829bc61ddf94f8a6d17403aa64ffda
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;
62 enum dump_mode dump_mode;
63 uid_t uid;
64 gid_t gid;
65 uint32_t link_type, magic;
66 uint32_t fanout_group, fanout_type;
69 static volatile sig_atomic_t sigint = 0, sighup = 0;
70 static volatile bool next_dump = false;
72 static const char *short_options = "d:i:o:rf:MNJt:S:k:n:b:HQmcsqXlvhF:RGAP:Vu:g:T:DBUC:K:L:";
73 static const struct option long_options[] = {
74 {"dev", required_argument, NULL, 'd'},
75 {"in", required_argument, NULL, 'i'},
76 {"out", required_argument, NULL, 'o'},
77 {"filter", required_argument, NULL, 'f'},
78 {"num", required_argument, NULL, 'n'},
79 {"type", required_argument, NULL, 't'},
80 {"interval", required_argument, NULL, 'F'},
81 {"ring-size", required_argument, NULL, 'S'},
82 {"kernel-pull", required_argument, NULL, 'k'},
83 {"bind-cpu", required_argument, NULL, 'b'},
84 {"prefix", required_argument, NULL, 'P'},
85 {"user", required_argument, NULL, 'u'},
86 {"group", required_argument, NULL, 'g'},
87 {"magic", required_argument, NULL, 'T'},
88 {"fanout-group", required_argument, NULL, 'C'},
89 {"fanout-type", required_argument, NULL, 'K'},
90 {"fanout-opts", required_argument, NULL, 'L'},
91 {"rand", no_argument, NULL, 'r'},
92 {"rfraw", no_argument, NULL, 'R'},
93 {"mmap", no_argument, NULL, 'm'},
94 {"sg", no_argument, NULL, 'G'},
95 {"clrw", no_argument, NULL, 'c'},
96 {"jumbo-support", no_argument, NULL, 'J'},
97 {"no-promisc", no_argument, NULL, 'M'},
98 {"no-hwtimestamp", no_argument, NULL, 'N'},
99 {"prio-high", no_argument, NULL, 'H'},
100 {"notouch-irq", no_argument, NULL, 'Q'},
101 {"dump-pcap-types", no_argument, NULL, 'D'},
102 {"dump-bpf", no_argument, NULL, 'B'},
103 {"silent", no_argument, NULL, 's'},
104 {"less", no_argument, NULL, 'q'},
105 {"hex", no_argument, NULL, 'X'},
106 {"ascii", no_argument, NULL, 'l'},
107 {"no-sock-mem", no_argument, NULL, 'A'},
108 {"update", no_argument, NULL, 'U'},
109 {"verbose", no_argument, NULL, 'V'},
110 {"version", no_argument, NULL, 'v'},
111 {"help", no_argument, NULL, 'h'},
112 {NULL, 0, NULL, 0}
115 static const char *copyright = "Please report bugs to <bugs@netsniff-ng.org>\n"
116 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
117 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
118 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
119 "Swiss federal institute of technology (ETH Zurich)\n"
120 "License: GNU GPL version 2.0\n"
121 "This is free software: you are free to change and redistribute it.\n"
122 "There is NO WARRANTY, to the extent permitted by law.";
124 static int tx_sock;
125 static struct itimerval itimer;
126 static unsigned long frame_count_max = 0, interval = TX_KERNEL_PULL_INT;
128 #define __pcap_io pcap_ops[ctx->pcap]
130 static void signal_handler(int number)
132 switch (number) {
133 case SIGINT:
134 case SIGQUIT:
135 case SIGTERM:
136 sigint = 1;
137 break;
138 case SIGHUP:
139 sighup = 1;
140 break;
141 default:
142 break;
146 static void timer_elapsed(int unused __maybe_unused)
148 int ret;
150 set_itimer_interval_value(&itimer, 0, interval);
152 ret = pull_and_flush_tx_ring(tx_sock);
153 if (unlikely(ret < 0)) {
154 /* We could hit EBADF if the socket has been closed before
155 * the timer was triggered.
157 if (errno != EBADF && errno != ENOBUFS)
158 panic("Flushing TX_RING failed: %s!\n", strerror(errno));
161 setitimer(ITIMER_REAL, &itimer, NULL);
164 static void timer_purge(void)
166 int ret;
168 ret = pull_and_flush_tx_ring_wait(tx_sock);
169 if (unlikely(ret < 0)) {
170 if (errno != EBADF && errno != ENOBUFS)
171 panic("Flushing TX_RING failed: %s!\n", strerror(errno));
174 set_itimer_interval_value(&itimer, 0, 0);
175 setitimer(ITIMER_REAL, &itimer, NULL);
178 static void timer_next_dump(int unused __maybe_unused)
180 set_itimer_interval_value(&itimer, interval, 0);
181 next_dump = true;
182 setitimer(ITIMER_REAL, &itimer, NULL);
185 static inline bool dump_to_pcap(struct ctx *ctx)
187 return ctx->dump;
190 static void on_panic_del_rfmon(void *arg)
192 leave_rfmon_mac80211(arg);
195 static inline void setup_rfmon_mac80211_dev(struct ctx *ctx, char **rfmon_dev)
197 ctx->device_trans = xstrdup(*rfmon_dev);
198 xfree(*rfmon_dev);
200 enter_rfmon_mac80211(ctx->device_trans, rfmon_dev);
201 panic_handler_add(on_panic_del_rfmon, *rfmon_dev);
204 static void pcap_to_xmit(struct ctx *ctx)
206 uint8_t *out = NULL;
207 int ifindex, fd = 0, ret;
208 size_t size;
209 unsigned int it = 0;
210 unsigned long trunced = 0;
211 struct ring tx_ring;
212 struct frame_map *hdr;
213 struct sock_fprog bpf_ops;
214 struct timeval start, end, diff;
215 pcap_pkthdr_t phdr;
217 if (!device_up_and_running(ctx->device_out) && !ctx->rfraw)
218 panic("Device not up and running!\n");
220 bug_on(!__pcap_io);
222 tx_sock = pf_socket();
224 if (!strncmp("-", ctx->device_in, strlen("-"))) {
225 fd = dup_or_die(fileno(stdin));
226 close(fileno(stdin));
227 if (ctx->pcap == PCAP_OPS_MM)
228 ctx->pcap = PCAP_OPS_SG;
229 } else {
230 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
233 if (__pcap_io->init_once_pcap)
234 __pcap_io->init_once_pcap(true);
236 ret = __pcap_io->pull_fhdr_pcap(fd, &ctx->magic, &ctx->link_type);
237 if (ret)
238 panic("Error reading pcap header!\n");
240 if (__pcap_io->prepare_access_pcap) {
241 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_RD, ctx->jumbo);
242 if (ret)
243 panic("Error prepare reading pcap!\n");
246 if (ctx->rfraw) {
247 setup_rfmon_mac80211_dev(ctx, &ctx->device_out);
249 if (ctx->link_type != LINKTYPE_IEEE802_11 &&
250 ctx->link_type != LINKTYPE_IEEE802_11_RADIOTAP)
251 panic("Wrong linktype of pcap!\n");
254 ifindex = device_ifindex(ctx->device_out);
255 size = ring_size(ctx->device_out, ctx->reserve_size);
257 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
258 if (ctx->dump_bpf)
259 bpf_dump_all(&bpf_ops);
261 ring_tx_setup(&tx_ring, tx_sock, size, ifindex, ctx->jumbo, ctx->verbose);
263 dissector_init_all(ctx->print_mode);
265 if (ctx->cpu >= 0 && ifindex > 0) {
266 int irq = device_irq_number(ctx->device_out);
267 device_set_irq_affinity(irq, ctx->cpu);
269 if (ctx->verbose)
270 printf("IRQ: %s:%d > CPU%d\n",
271 ctx->device_out, irq, ctx->cpu);
274 if (ctx->kpull)
275 interval = ctx->kpull;
277 set_itimer_interval_value(&itimer, 0, interval);
278 setitimer(ITIMER_REAL, &itimer, NULL);
280 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
282 printf("Running! Hang up with ^C!\n\n");
283 fflush(stdout);
285 bug_on(gettimeofday(&start, NULL));
287 while (likely(sigint == 0)) {
288 while (user_may_pull_from_tx(tx_ring.frames[it].iov_base)) {
289 hdr = tx_ring.frames[it].iov_base;
290 out = ((uint8_t *) hdr) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
292 do {
293 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic, out,
294 ring_frame_size(&tx_ring));
295 if (unlikely(ret <= 0))
296 goto out;
298 if (ring_frame_size(&tx_ring) <
299 pcap_get_length(&phdr, ctx->magic)) {
300 pcap_set_length(&phdr, ctx->magic,
301 ring_frame_size(&tx_ring));
302 trunced++;
304 } while (ctx->filter &&
305 !bpf_run_filter(&bpf_ops, out,
306 pcap_get_length(&phdr, ctx->magic)));
308 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &hdr->tp_h, NULL);
310 ctx->tx_bytes += hdr->tp_h.tp_len;;
311 ctx->tx_packets++;
313 show_frame_hdr(out, hdr->tp_h.tp_snaplen,
314 ctx->link_type, hdr, ctx->print_mode,
315 ctx->tx_packets);
317 dissector_entry_point(out, hdr->tp_h.tp_snaplen,
318 ctx->link_type, ctx->print_mode,
319 hdr->s_ll.sll_protocol);
321 kernel_may_pull_from_tx(&hdr->tp_h);
323 it++;
324 if (it >= tx_ring.layout.tp_frame_nr)
325 it = 0;
327 if (unlikely(sigint == 1))
328 break;
330 if (frame_count_max != 0) {
331 if (ctx->tx_packets >= frame_count_max) {
332 sigint = 1;
333 break;
339 out:
340 bug_on(gettimeofday(&end, NULL));
341 timersub(&end, &start, &diff);
343 timer_purge();
345 bpf_release(&bpf_ops);
347 dissector_cleanup_all();
348 destroy_tx_ring(tx_sock, &tx_ring);
350 if (ctx->rfraw)
351 leave_rfmon_mac80211(ctx->device_out);
353 if (__pcap_io->prepare_close_pcap)
354 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
356 if (!strncmp("-", ctx->device_in, strlen("-")))
357 dup2(fd, fileno(stdin));
358 close(fd);
360 close(tx_sock);
362 fflush(stdout);
363 printf("\n");
364 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
365 printf("\r%12lu packets truncated in file\n", trunced);
366 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
367 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
370 static void receive_to_xmit(struct ctx *ctx)
372 short ifflags = 0;
373 uint8_t *in, *out;
374 int rx_sock, ifindex_in, ifindex_out, ret;
375 size_t size_in, size_out;
376 unsigned int it_in = 0, it_out = 0;
377 unsigned long frame_count = 0;
378 struct frame_map *hdr_in, *hdr_out;
379 struct ring tx_ring, rx_ring;
380 struct pollfd rx_poll;
381 struct sock_fprog bpf_ops;
383 if (!strncmp(ctx->device_in, ctx->device_out, IFNAMSIZ))
384 panic("Ingress/egress devices must be different!\n");
385 if (!device_up_and_running(ctx->device_out))
386 panic("Egress device not up and running!\n");
388 rx_sock = pf_socket();
389 tx_sock = pf_socket();
391 ifindex_in = device_ifindex(ctx->device_in);
392 ifindex_out = device_ifindex(ctx->device_out);
394 size_in = ring_size(ctx->device_in, ctx->reserve_size);
395 size_out = ring_size(ctx->device_out, ctx->reserve_size);
397 enable_kernel_bpf_jit_compiler();
399 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
400 if (ctx->dump_bpf)
401 bpf_dump_all(&bpf_ops);
402 bpf_attach_to_sock(rx_sock, &bpf_ops);
404 ring_rx_setup(&rx_ring, rx_sock, size_in, ifindex_in, &rx_poll, false, ctx->jumbo,
405 ctx->verbose, ctx->fanout_group, ctx->fanout_type);
406 ring_tx_setup(&tx_ring, tx_sock, size_out, ifindex_out, ctx->jumbo, ctx->verbose);
408 dissector_init_all(ctx->print_mode);
410 if (ctx->promiscuous)
411 ifflags = device_enter_promiscuous_mode(ctx->device_in);
413 if (ctx->kpull)
414 interval = ctx->kpull;
416 set_itimer_interval_value(&itimer, 0, interval);
417 setitimer(ITIMER_REAL, &itimer, NULL);
419 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
421 printf("Running! Hang up with ^C!\n\n");
422 fflush(stdout);
424 while (likely(sigint == 0)) {
425 while (user_may_pull_from_rx(rx_ring.frames[it_in].iov_base)) {
426 hdr_in = rx_ring.frames[it_in].iov_base;
427 in = ((uint8_t *) hdr_in) + hdr_in->tp_h.tp_mac;
429 frame_count++;
431 if (ctx->packet_type != -1)
432 if (ctx->packet_type != hdr_in->s_ll.sll_pkttype)
433 goto next;
435 hdr_out = tx_ring.frames[it_out].iov_base;
436 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
438 while (!user_may_pull_from_tx(tx_ring.frames[it_out].iov_base) &&
439 likely(!sigint)) {
440 if (ctx->randomize)
441 next_rnd_slot(&it_out, &tx_ring);
442 else {
443 it_out++;
444 if (it_out >= tx_ring.layout.tp_frame_nr)
445 it_out = 0;
448 hdr_out = tx_ring.frames[it_out].iov_base;
449 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
452 tpacket_hdr_clone(&hdr_out->tp_h, &hdr_in->tp_h);
453 fmemcpy(out, in, hdr_in->tp_h.tp_len);
455 kernel_may_pull_from_tx(&hdr_out->tp_h);
456 if (ctx->randomize)
457 next_rnd_slot(&it_out, &tx_ring);
458 else {
459 it_out++;
460 if (it_out >= tx_ring.layout.tp_frame_nr)
461 it_out = 0;
464 show_frame_hdr(in, hdr_in->tp_h.tp_snaplen,
465 ctx->link_type, hdr_in, ctx->print_mode,
466 frame_count);
468 dissector_entry_point(in, hdr_in->tp_h.tp_snaplen,
469 ctx->link_type, ctx->print_mode,
470 hdr_in->s_ll.sll_protocol);
472 if (frame_count_max != 0) {
473 if (frame_count >= frame_count_max) {
474 sigint = 1;
475 break;
479 next:
480 kernel_may_pull_from_rx(&hdr_in->tp_h);
482 it_in++;
483 if (it_in >= rx_ring.layout.tp_frame_nr)
484 it_in = 0;
486 if (unlikely(sigint == 1))
487 goto out;
490 ret = poll(&rx_poll, 1, -1);
491 if (unlikely(ret < 0)) {
492 if (errno != EINTR)
493 panic("Poll failed!\n");
497 out:
498 timer_purge();
500 sock_rx_net_stats(rx_sock, 0);
502 bpf_release(&bpf_ops);
504 dissector_cleanup_all();
506 destroy_tx_ring(tx_sock, &tx_ring);
507 destroy_rx_ring(rx_sock, &rx_ring);
509 if (ctx->promiscuous)
510 device_leave_promiscuous_mode(ctx->device_in, ifflags);
512 close(tx_sock);
513 close(rx_sock);
516 static void translate_pcap_to_txf(int fdo, uint8_t *out, size_t len)
518 size_t bytes_done = 0;
519 char bout[80];
521 slprintf(bout, sizeof(bout), "{\n ");
522 write_or_die(fdo, bout, strlen(bout));
524 while (bytes_done < len) {
525 slprintf(bout, sizeof(bout), "0x%02x,", out[bytes_done]);
526 write_or_die(fdo, bout, strlen(bout));
528 bytes_done++;
530 if (bytes_done % 10 == 0) {
531 slprintf(bout, sizeof(bout), "\n");
532 write_or_die(fdo, bout, strlen(bout));
534 if (bytes_done < len) {
535 slprintf(bout, sizeof(bout), " ");
536 write_or_die(fdo, bout, strlen(bout));
538 } else if (bytes_done < len) {
539 slprintf(bout, sizeof(bout), " ");
540 write_or_die(fdo, bout, strlen(bout));
543 if (bytes_done % 10 != 0) {
544 slprintf(bout, sizeof(bout), "\n");
545 write_or_die(fdo, bout, strlen(bout));
548 slprintf(bout, sizeof(bout), "}\n\n");
549 write_or_die(fdo, bout, strlen(bout));
552 static void read_pcap(struct ctx *ctx)
554 uint8_t *out;
555 int ret, fd, fdo = 0;
556 unsigned long trunced = 0;
557 size_t out_len;
558 pcap_pkthdr_t phdr;
559 struct sock_fprog bpf_ops;
560 struct frame_map fm;
561 struct timeval start, end, diff;
562 bool is_out_pcap = ctx->device_out && strstr(ctx->device_out, ".pcap");
563 const struct pcap_file_ops *pcap_out_ops = pcap_ops[PCAP_OPS_RW];
565 bug_on(!__pcap_io);
567 if (!strncmp("-", ctx->device_in, strlen("-"))) {
568 fd = dup_or_die(fileno(stdin));
569 close(fileno(stdin));
570 if (ctx->pcap == PCAP_OPS_MM)
571 ctx->pcap = PCAP_OPS_SG;
572 } else {
573 /* O_NOATIME requires privileges, in case we don't have
574 * them, retry without them at a minor cost of updating
575 * atime in case the fs has been mounted as such.
577 fd = open(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
578 if (fd < 0 && errno == EPERM)
579 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE);
580 if (fd < 0)
581 panic("Cannot open file %s! %s.\n", ctx->device_in,
582 strerror(errno));
585 if (__pcap_io->init_once_pcap)
586 __pcap_io->init_once_pcap(false);
588 ret = __pcap_io->pull_fhdr_pcap(fd, &ctx->magic, &ctx->link_type);
589 if (ret)
590 panic("Error reading pcap header!\n");
592 if (__pcap_io->prepare_access_pcap) {
593 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_RD, ctx->jumbo);
594 if (ret)
595 panic("Error prepare reading pcap!\n");
598 fmemset(&fm, 0, sizeof(fm));
600 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
601 if (ctx->dump_bpf)
602 bpf_dump_all(&bpf_ops);
604 dissector_init_all(ctx->print_mode);
606 out_len = round_up(1024 * 1024, RUNTIME_PAGE_SIZE);
607 out = xmalloc_aligned(out_len, CO_CACHE_LINE_SIZE);
609 if (ctx->device_out) {
610 if (!strncmp("-", ctx->device_out, strlen("-"))) {
611 fdo = dup_or_die(fileno(stdout));
612 close(fileno(stdout));
613 } else {
614 fdo = open_or_die_m(ctx->device_out, O_RDWR | O_CREAT |
615 O_TRUNC | O_LARGEFILE, DEFFILEMODE);
619 if (is_out_pcap) {
620 ret = pcap_out_ops->push_fhdr_pcap(fdo, ctx->magic,
621 ctx->link_type);
622 if (ret)
623 panic("Error writing pcap header!\n");
626 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
628 printf("Running! Hang up with ^C!\n\n");
629 fflush(stdout);
631 bug_on(gettimeofday(&start, NULL));
633 while (likely(sigint == 0)) {
634 do {
635 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic,
636 out, out_len);
637 if (unlikely(ret < 0))
638 goto out;
640 if (unlikely(pcap_get_length(&phdr, ctx->magic) == 0)) {
641 trunced++;
642 continue;
645 if (unlikely(pcap_get_length(&phdr, ctx->magic) > out_len)) {
646 pcap_set_length(&phdr, ctx->magic, out_len);
647 trunced++;
649 } while (ctx->filter &&
650 !bpf_run_filter(&bpf_ops, out,
651 pcap_get_length(&phdr, ctx->magic)));
653 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &fm.tp_h, &fm.s_ll);
655 ctx->tx_bytes += fm.tp_h.tp_len;
656 ctx->tx_packets++;
658 show_frame_hdr(out, fm.tp_h.tp_snaplen, ctx->link_type, &fm,
659 ctx->print_mode, ctx->tx_packets);
661 dissector_entry_point(out, fm.tp_h.tp_snaplen,
662 ctx->link_type, ctx->print_mode,
663 fm.s_ll.sll_protocol);
665 if (is_out_pcap) {
666 size_t pcap_len = pcap_get_length(&phdr, ctx->magic);
667 int wlen = pcap_out_ops->write_pcap(fdo, &phdr,
668 ctx->magic, out,
669 pcap_len);
670 if (unlikely(wlen != (int)pcap_get_total_length(&phdr, ctx->magic)))
671 panic("Error writing to pcap!\n");
672 } else if (ctx->device_out) {
673 translate_pcap_to_txf(fdo, out, fm.tp_h.tp_snaplen);
676 if (frame_count_max != 0) {
677 if (ctx->tx_packets >= frame_count_max) {
678 sigint = 1;
679 break;
684 out:
685 bug_on(gettimeofday(&end, NULL));
686 timersub(&end, &start, &diff);
688 bpf_release(&bpf_ops);
690 dissector_cleanup_all();
692 if (__pcap_io->prepare_close_pcap)
693 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
695 xfree(out);
697 fflush(stdout);
698 printf("\n");
699 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
700 printf("\r%12lu packets truncated in file\n", trunced);
701 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
702 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
704 if (!strncmp("-", ctx->device_in, strlen("-")))
705 dup2(fd, fileno(stdin));
706 close(fd);
708 if (ctx->device_out) {
709 if (!strncmp("-", ctx->device_out, strlen("-")))
710 dup2(fdo, fileno(stdout));
711 close(fdo);
715 static void finish_multi_pcap_file(struct ctx *ctx, int fd)
717 __pcap_io->fsync_pcap(fd);
719 if (__pcap_io->prepare_close_pcap)
720 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
722 close(fd);
724 fmemset(&itimer, 0, sizeof(itimer));
725 setitimer(ITIMER_REAL, &itimer, NULL);
728 static int next_multi_pcap_file(struct ctx *ctx, int fd)
730 int ret;
731 char fname[512];
733 __pcap_io->fsync_pcap(fd);
735 if (__pcap_io->prepare_close_pcap)
736 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
738 close(fd);
740 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
741 ctx->prefix ? : "dump-", time(NULL));
743 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
744 O_LARGEFILE, DEFFILEMODE);
746 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
747 if (ret)
748 panic("Error writing pcap header!\n");
750 if (__pcap_io->prepare_access_pcap) {
751 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, true);
752 if (ret)
753 panic("Error prepare writing pcap!\n");
756 return fd;
759 static void reset_interval(struct ctx *ctx)
761 if (ctx->dump_mode == DUMP_INTERVAL_TIME) {
762 interval = ctx->dump_interval;
764 set_itimer_interval_value(&itimer, interval, 0);
765 setitimer(ITIMER_REAL, &itimer, NULL);
766 } else {
767 interval = 0;
771 static int begin_multi_pcap_file(struct ctx *ctx)
773 int fd, ret;
774 char fname[256];
776 bug_on(!__pcap_io);
778 if (ctx->device_out[strlen(ctx->device_out) - 1] == '/')
779 ctx->device_out[strlen(ctx->device_out) - 1] = 0;
781 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
782 ctx->prefix ? : "dump-", time(NULL));
784 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
785 O_LARGEFILE, DEFFILEMODE);
787 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
788 if (ret)
789 panic("Error writing pcap header!\n");
791 if (__pcap_io->prepare_access_pcap) {
792 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, true);
793 if (ret)
794 panic("Error prepare writing pcap!\n");
797 reset_interval(ctx);
799 return fd;
802 static void finish_single_pcap_file(struct ctx *ctx, int fd)
804 __pcap_io->fsync_pcap(fd);
806 if (__pcap_io->prepare_close_pcap)
807 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
809 if (strncmp("-", ctx->device_out, strlen("-")))
810 close(fd);
811 else
812 dup2(fd, fileno(stdout));
815 static int begin_single_pcap_file(struct ctx *ctx)
817 int fd, ret;
819 bug_on(!__pcap_io);
821 if (!strncmp("-", ctx->device_out, strlen("-"))) {
822 fd = dup_or_die(fileno(stdout));
823 close(fileno(stdout));
824 if (ctx->pcap == PCAP_OPS_MM)
825 ctx->pcap = PCAP_OPS_SG;
826 } else {
827 fd = open_or_die_m(ctx->device_out,
828 O_RDWR | O_CREAT | O_TRUNC |
829 O_LARGEFILE, DEFFILEMODE);
832 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
833 if (ret)
834 panic("Error writing pcap header!\n");
836 if (__pcap_io->prepare_access_pcap) {
837 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, true);
838 if (ret)
839 panic("Error prepare writing pcap!\n");
842 return fd;
845 static void print_pcap_file_stats(int sock, struct ctx *ctx)
847 int ret;
848 struct tpacket_stats kstats;
849 socklen_t slen = sizeof(kstats);
851 fmemset(&kstats, 0, sizeof(kstats));
853 ret = getsockopt(sock, SOL_PACKET, PACKET_STATISTICS, &kstats, &slen);
854 if (unlikely(ret))
855 panic("Cannot get packet statistics!\n");
857 if (ctx->print_mode == PRINT_NONE) {
858 printf(".(+%u/-%u)", kstats.tp_packets - kstats.tp_drops,
859 kstats.tp_drops);
860 fflush(stdout);
864 static void update_pcap_next_dump(struct ctx *ctx, unsigned long snaplen, int *fd, int sock)
866 if (!dump_to_pcap(ctx))
867 return;
869 if (ctx->dump_mode == DUMP_INTERVAL_SIZE) {
870 interval += snaplen;
871 if (interval > ctx->dump_interval) {
872 next_dump = true;
873 interval = 0;
877 if (sighup) {
878 if (ctx->verbose)
879 printf("SIGHUP received, prematurely rotating pcap\n");
880 sighup = 0;
881 next_dump = true;
882 reset_interval(ctx);
885 if (next_dump) {
886 *fd = next_multi_pcap_file(ctx, *fd);
887 next_dump = false;
889 if (ctx->verbose)
890 print_pcap_file_stats(sock, ctx);
894 #ifdef HAVE_TPACKET3
895 static void walk_t3_block(struct block_desc *pbd, struct ctx *ctx,
896 int sock, int *fd, unsigned long *frame_count)
898 int num_pkts = pbd->h1.num_pkts, i;
899 struct tpacket3_hdr *hdr;
900 struct sockaddr_ll *sll;
902 hdr = (void *) ((uint8_t *) pbd + pbd->h1.offset_to_first_pkt);
903 sll = (void *) ((uint8_t *) hdr + TPACKET_ALIGN(sizeof(*hdr)));
905 for (i = 0; i < num_pkts && likely(sigint == 0); ++i) {
906 uint8_t *packet = ((uint8_t *) hdr + hdr->tp_mac);
907 pcap_pkthdr_t phdr;
909 if (ctx->packet_type != -1)
910 if (ctx->packet_type != sll->sll_pkttype)
911 goto next;
913 (*frame_count)++;
915 if (dump_to_pcap(ctx)) {
916 int ret;
918 tpacket3_hdr_to_pcap_pkthdr(hdr, sll, &phdr, ctx->magic);
920 ret = __pcap_io->write_pcap(*fd, &phdr, ctx->magic, packet,
921 pcap_get_length(&phdr, ctx->magic));
922 if (unlikely(ret != (int) pcap_get_total_length(&phdr, ctx->magic)))
923 panic("Write error to pcap!\n");
926 __show_frame_hdr(packet, hdr->tp_snaplen, ctx->link_type, sll,
927 hdr, ctx->print_mode, true, *frame_count);
929 dissector_entry_point(packet, hdr->tp_snaplen, ctx->link_type,
930 ctx->print_mode, sll->sll_protocol);
931 next:
932 hdr = (void *) ((uint8_t *) hdr + hdr->tp_next_offset);
933 sll = (void *) ((uint8_t *) hdr + TPACKET_ALIGN(sizeof(*hdr)));
935 if (frame_count_max != 0) {
936 if (unlikely(*frame_count >= frame_count_max)) {
937 sigint = 1;
938 break;
942 update_pcap_next_dump(ctx, hdr->tp_snaplen, fd, sock);
945 #endif /* HAVE_TPACKET3 */
947 static void recv_only_or_dump(struct ctx *ctx)
949 short ifflags = 0;
950 int sock, ifindex, fd = 0, ret;
951 size_t size;
952 unsigned int it = 0;
953 struct ring rx_ring;
954 struct pollfd rx_poll;
955 struct sock_fprog bpf_ops;
956 struct timeval start, end, diff;
957 unsigned long frame_count = 0;
959 sock = pf_socket();
961 ifindex = device_ifindex(ctx->device_in);
963 size = ring_size(ctx->device_in, ctx->reserve_size);
965 enable_kernel_bpf_jit_compiler();
967 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
968 if (ctx->dump_bpf)
969 bpf_dump_all(&bpf_ops);
970 bpf_attach_to_sock(sock, &bpf_ops);
972 if (ctx->hwtimestamp) {
973 ret = set_sockopt_hwtimestamp(sock, ctx->device_in);
974 if (ret == 0 && ctx->verbose)
975 printf("HW timestamping enabled\n");
978 ring_rx_setup(&rx_ring, sock, size, ifindex, &rx_poll, is_defined(HAVE_TPACKET3), true,
979 ctx->verbose, ctx->fanout_group, ctx->fanout_type);
981 dissector_init_all(ctx->print_mode);
983 if (ctx->cpu >= 0 && ifindex > 0) {
984 int irq = device_irq_number(ctx->device_in);
985 device_set_irq_affinity(irq, ctx->cpu);
987 if (ctx->verbose)
988 printf("IRQ: %s:%d > CPU%d\n",
989 ctx->device_in, irq, ctx->cpu);
992 if (ctx->promiscuous)
993 ifflags = device_enter_promiscuous_mode(ctx->device_in);
995 if (dump_to_pcap(ctx) && __pcap_io->init_once_pcap)
996 __pcap_io->init_once_pcap(true);
998 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
1000 if (dump_to_pcap(ctx)) {
1001 struct stat stats;
1003 ret = stat(ctx->device_out, &stats);
1004 if (ret < 0)
1005 ctx->dump_dir = 0;
1006 else
1007 ctx->dump_dir = S_ISDIR(stats.st_mode);
1009 if (ctx->dump_dir)
1010 fd = begin_multi_pcap_file(ctx);
1011 else
1012 fd = begin_single_pcap_file(ctx);
1015 printf("Running! Hang up with ^C!\n\n");
1016 fflush(stdout);
1018 bug_on(gettimeofday(&start, NULL));
1020 while (likely(sigint == 0)) {
1021 #ifdef HAVE_TPACKET3
1022 struct block_desc *pbd;
1024 while (user_may_pull_from_rx_block((pbd = rx_ring.frames[it].iov_base))) {
1025 walk_t3_block(pbd, ctx, sock, &fd, &frame_count);
1027 kernel_may_pull_from_rx_block(pbd);
1028 it = (it + 1) % rx_ring.layout3.tp_block_nr;
1030 if (unlikely(sigint == 1))
1031 break;
1033 #else
1034 while (user_may_pull_from_rx(rx_ring.frames[it].iov_base)) {
1035 struct frame_map *hdr = rx_ring.frames[it].iov_base;
1036 uint8_t *packet = ((uint8_t *) hdr) + hdr->tp_h.tp_mac;
1037 pcap_pkthdr_t phdr;
1039 if (ctx->packet_type != -1)
1040 if (ctx->packet_type != hdr->s_ll.sll_pkttype)
1041 goto next;
1043 frame_count++;
1045 if (unlikely(ring_frame_size(&rx_ring) < hdr->tp_h.tp_snaplen)) {
1046 /* XXX: silently ignore for now. We used to
1047 * report them with sock_rx_net_stats() */
1048 goto next;
1051 if (dump_to_pcap(ctx)) {
1052 tpacket_hdr_to_pcap_pkthdr(&hdr->tp_h, &hdr->s_ll, &phdr, ctx->magic);
1054 ret = __pcap_io->write_pcap(fd, &phdr, ctx->magic, packet,
1055 pcap_get_length(&phdr, ctx->magic));
1056 if (unlikely(ret != (int) pcap_get_total_length(&phdr, ctx->magic)))
1057 panic("Write error to pcap!\n");
1060 show_frame_hdr(packet, hdr->tp_h.tp_snaplen,
1061 ctx->link_type, hdr, ctx->print_mode,
1062 frame_count);
1064 dissector_entry_point(packet, hdr->tp_h.tp_snaplen,
1065 ctx->link_type, ctx->print_mode,
1066 hdr->s_ll.sll_protocol);
1068 if (frame_count_max != 0) {
1069 if (unlikely(frame_count >= frame_count_max)) {
1070 sigint = 1;
1071 break;
1075 next:
1076 kernel_may_pull_from_rx(&hdr->tp_h);
1077 it = (it + 1) % rx_ring.layout.tp_frame_nr;
1079 if (unlikely(sigint == 1))
1080 break;
1082 update_pcap_next_dump(ctx, hdr->tp_h.tp_snaplen, &fd, sock);
1084 #endif /* HAVE_TPACKET3 */
1086 ret = poll(&rx_poll, 1, -1);
1087 if (unlikely(ret < 0)) {
1088 if (errno != EINTR)
1089 panic("Poll failed!\n");
1093 bug_on(gettimeofday(&end, NULL));
1094 timersub(&end, &start, &diff);
1096 if (!(ctx->dump_dir && ctx->print_mode == PRINT_NONE)) {
1097 sock_rx_net_stats(sock, frame_count);
1099 printf("\r%12lu sec, %lu usec in total\n",
1100 diff.tv_sec, diff.tv_usec);
1101 } else {
1102 printf("\n\n");
1103 fflush(stdout);
1106 bpf_release(&bpf_ops);
1107 dissector_cleanup_all();
1108 destroy_rx_ring(sock, &rx_ring);
1110 if (ctx->promiscuous)
1111 device_leave_promiscuous_mode(ctx->device_in, ifflags);
1113 if (ctx->rfraw)
1114 leave_rfmon_mac80211(ctx->device_in);
1116 if (dump_to_pcap(ctx)) {
1117 if (ctx->dump_dir)
1118 finish_multi_pcap_file(ctx, fd);
1119 else
1120 finish_single_pcap_file(ctx, fd);
1123 close(sock);
1126 static void init_ctx(struct ctx *ctx)
1128 memset(ctx, 0, sizeof(*ctx));
1130 ctx->uid = getuid();
1131 ctx->gid = getgid();
1133 ctx->cpu = -1;
1134 ctx->packet_type = -1;
1136 ctx->fanout_type = PACKET_FANOUT_ROLLOVER;
1138 ctx->magic = ORIGINAL_TCPDUMP_MAGIC;
1139 ctx->print_mode = PRINT_NORM;
1140 ctx->pcap = PCAP_OPS_SG;
1142 ctx->dump_mode = DUMP_INTERVAL_TIME;
1143 ctx->dump_interval = 60;
1145 ctx->promiscuous = true;
1146 ctx->randomize = false;
1147 ctx->hwtimestamp = true;
1150 static void destroy_ctx(struct ctx *ctx)
1152 free(ctx->device_in);
1153 free(ctx->device_out);
1154 free(ctx->device_trans);
1156 free(ctx->prefix);
1159 static void __noreturn help(void)
1161 printf("netsniff-ng %s, the packet sniffing beast\n", VERSION_STRING);
1162 puts("http://www.netsniff-ng.org\n\n"
1163 "Usage: netsniff-ng [options] [filter-expression]\n"
1164 "Options:\n"
1165 " -i|-d|--dev|--in <dev|pcap|-> Input source as netdev, pcap or pcap stdin\n"
1166 " -o|--out <dev|pcap|dir|cfg|-> Output sink as netdev, pcap, directory, trafgen, or stdout\n"
1167 " -C|--fanout-group <id> Join packet fanout group\n"
1168 " -K|--fanout-type <type> Apply fanout discipline: hash|lb|cpu|rnd|roll|qm\n"
1169 " -L|--fanout-opts <opts> Additional fanout options: defrag|roll\n"
1170 " -f|--filter <bpf-file|expr> Use BPF filter file from bpfc or tcpdump-like expression\n"
1171 " -t|--type <type> Filter for: host|broadcast|multicast|others|outgoing\n"
1172 " -F|--interval <size|time> Dump interval if -o is a dir: <num>KiB/MiB/GiB/s/sec/min/hrs\n"
1173 " -R|--rfraw Capture or inject raw 802.11 frames\n"
1174 " -n|--num <0|uint> Number of packets until exit (def: 0)\n"
1175 " -P|--prefix <name> Prefix for pcaps stored in directory\n"
1176 " -T|--magic <pcap-magic> Pcap magic number/pcap format to store, see -D\n"
1177 " -D|--dump-pcap-types Dump pcap types and magic numbers and quit\n"
1178 " -B|--dump-bpf Dump generated BPF assembly\n"
1179 " -r|--rand Randomize packet forwarding order (dev->dev)\n"
1180 " -M|--no-promisc No promiscuous mode for netdev\n"
1181 " -A|--no-sock-mem Don't tune core socket memory\n"
1182 " -N|--no-hwtimestamp Disable hardware time stamping\n"
1183 " -m|--mmap Mmap(2) pcap file I/O, e.g. for replaying pcaps\n"
1184 " -G|--sg Scatter/gather pcap file I/O\n"
1185 " -c|--clrw Use slower read(2)/write(2) I/O\n"
1186 " -S|--ring-size <size> Specify ring size to: <num>KiB/MiB/GiB\n"
1187 " -k|--kernel-pull <uint> Kernel pull from user interval in us (def: 10us)\n"
1188 " -J|--jumbo-support Support replay/fwd 64KB Super Jumbo Frames (def: 2048B)\n"
1189 " -b|--bind-cpu <cpu> Bind to specific CPU\n"
1190 " -u|--user <userid> Drop privileges and change to userid\n"
1191 " -g|--group <groupid> Drop privileges and change to groupid\n"
1192 " -H|--prio-high Make this high priority process\n"
1193 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
1194 " -s|--silent Do not print captured packets\n"
1195 " -q|--less Print less-verbose packet information\n"
1196 " -X|--hex Print packet data in hex format\n"
1197 " -l|--ascii Print human-readable packet data\n"
1198 " -U|--update Update GeoIP databases\n"
1199 " -V|--verbose Be more verbose\n"
1200 " -v|--version Show version and exit\n"
1201 " -h|--help Guess what?!\n\n"
1202 "Examples:\n"
1203 " netsniff-ng --in eth0 --out dump.pcap -s -T 0xa1b2c3d4 --b 0 tcp or udp\n"
1204 " netsniff-ng --in wlan0 --rfraw --out dump.pcap --silent --bind-cpu 0\n"
1205 " netsniff-ng --in dump.pcap --mmap --out eth0 -k1000 --silent --bind-cpu 0\n"
1206 " netsniff-ng --in dump.pcap --out dump.cfg --silent --bind-cpu 0\n"
1207 " netsniff-ng --in dump.pcap --out dump2.pcap --silent tcp\n"
1208 " netsniff-ng --in eth0 --out eth1 --silent --bind-cpu 0 -J --type host\n"
1209 " netsniff-ng --in eth1 --out /opt/probe/ -s -m --interval 100MiB -b 0\n"
1210 " netsniff-ng --in vlan0 --out dump.pcap -c -u `id -u bob` -g `id -g bob`\n"
1211 " netsniff-ng --in any --filter http.bpf --jumbo-support --ascii -V\n\n"
1212 "Note:\n"
1213 " For introducing bit errors, delays with random variation and more\n"
1214 " while replaying pcaps, make use of tc(8) with its disciplines (e.g. netem).\n");
1215 puts(copyright);
1216 die();
1219 static void __noreturn version(void)
1221 printf("netsniff-ng %s, Git id: %s\n", VERSION_LONG, GITVERSION);
1222 puts("the packet sniffing beast\n"
1223 "http://www.netsniff-ng.org\n");
1224 puts(copyright);
1225 die();
1228 int main(int argc, char **argv)
1230 char *ptr;
1231 int c, i, j, cpu_tmp, opt_index, ops_touched = 0, vals[4] = {0};
1232 bool prio_high = false, setsockmem = true;
1233 void (*main_loop)(struct ctx *ctx) = NULL;
1234 struct ctx ctx;
1236 init_ctx(&ctx);
1237 srand(time(NULL));
1239 while ((c = getopt_long(argc, argv, short_options, long_options,
1240 &opt_index)) != EOF) {
1241 switch (c) {
1242 case 'd':
1243 case 'i':
1244 ctx.device_in = xstrdup(optarg);
1245 break;
1246 case 'o':
1247 ctx.device_out = xstrdup(optarg);
1248 break;
1249 case 'P':
1250 ctx.prefix = xstrdup(optarg);
1251 break;
1252 case 'R':
1253 ctx.rfraw = 1;
1254 break;
1255 case 'r':
1256 ctx.randomize = true;
1257 break;
1258 case 'J':
1259 ctx.jumbo = true;
1260 break;
1261 case 'T':
1262 ctx.magic = (uint32_t) strtoul(optarg, NULL, 0);
1263 pcap_check_magic(ctx.magic);
1264 break;
1265 case 'f':
1266 ctx.filter = xstrdup(optarg);
1267 break;
1268 case 'M':
1269 ctx.promiscuous = false;
1270 break;
1271 case 'N':
1272 ctx.hwtimestamp = false;
1273 break;
1274 case 'A':
1275 setsockmem = false;
1276 break;
1277 case 'u':
1278 ctx.uid = strtoul(optarg, NULL, 0);
1279 ctx.enforce = true;
1280 break;
1281 case 'g':
1282 ctx.gid = strtoul(optarg, NULL, 0);
1283 ctx.enforce = true;
1284 break;
1285 case 'C':
1286 ctx.fanout_group = strtoul(optarg, NULL, 0);
1287 if (ctx.fanout_group == 0)
1288 panic("Non-zero fanout group id required!\n");
1289 break;
1290 case 'K':
1291 if (!strncmp(optarg, "hash", strlen("hash")))
1292 ctx.fanout_type = PACKET_FANOUT_HASH;
1293 else if (!strncmp(optarg, "lb", strlen("lb")) ||
1294 !strncmp(optarg, "rr", strlen("rr")))
1295 ctx.fanout_type = PACKET_FANOUT_LB;
1296 else if (!strncmp(optarg, "cpu", strlen("cpu")))
1297 ctx.fanout_type = PACKET_FANOUT_CPU;
1298 else if (!strncmp(optarg, "rnd", strlen("rnd")))
1299 ctx.fanout_type = PACKET_FANOUT_RND;
1300 else if (!strncmp(optarg, "roll", strlen("roll")))
1301 ctx.fanout_type = PACKET_FANOUT_ROLLOVER;
1302 else if (!strncmp(optarg, "qm", strlen("qm")))
1303 ctx.fanout_type = PACKET_FANOUT_QM;
1304 else
1305 panic("Unkown fanout type!\n");
1306 break;
1307 case 'L':
1308 if (!strncmp(optarg, "defrag", strlen("defrag")))
1309 ctx.fanout_type |= PACKET_FANOUT_FLAG_DEFRAG;
1310 else if (!strncmp(optarg, "roll", strlen("roll")))
1311 ctx.fanout_type |= PACKET_FANOUT_FLAG_ROLLOVER;
1312 else
1313 panic("Unkown fanout option!\n");
1314 break;
1315 case 't':
1316 if (!strncmp(optarg, "host", strlen("host")))
1317 ctx.packet_type = PACKET_HOST;
1318 else if (!strncmp(optarg, "broadcast", strlen("broadcast")))
1319 ctx.packet_type = PACKET_BROADCAST;
1320 else if (!strncmp(optarg, "multicast", strlen("multicast")))
1321 ctx.packet_type = PACKET_MULTICAST;
1322 else if (!strncmp(optarg, "others", strlen("others")))
1323 ctx.packet_type = PACKET_OTHERHOST;
1324 else if (!strncmp(optarg, "outgoing", strlen("outgoing")))
1325 ctx.packet_type = PACKET_OUTGOING;
1326 else
1327 ctx.packet_type = -1;
1328 break;
1329 case 'S':
1330 ptr = optarg;
1331 for (j = i = strlen(optarg); i > 0; --i) {
1332 if (!isdigit(optarg[j - i]))
1333 break;
1334 ptr++;
1337 if (!strncmp(ptr, "KiB", strlen("KiB")))
1338 ctx.reserve_size = 1 << 10;
1339 else if (!strncmp(ptr, "MiB", strlen("MiB")))
1340 ctx.reserve_size = 1 << 20;
1341 else if (!strncmp(ptr, "GiB", strlen("GiB")))
1342 ctx.reserve_size = 1 << 30;
1343 else
1344 panic("Syntax error in ring size param!\n");
1346 ctx.reserve_size *= strtoul(optarg, NULL, 0);
1347 break;
1348 case 'b':
1349 cpu_tmp = strtol(optarg, NULL, 0);
1351 cpu_affinity(cpu_tmp);
1352 if (ctx.cpu != -2)
1353 ctx.cpu = cpu_tmp;
1354 break;
1355 case 'H':
1356 prio_high = true;
1357 break;
1358 case 'c':
1359 ctx.pcap = PCAP_OPS_RW;
1360 ops_touched = 1;
1361 break;
1362 case 'm':
1363 ctx.pcap = PCAP_OPS_MM;
1364 ops_touched = 1;
1365 break;
1366 case 'G':
1367 ctx.pcap = PCAP_OPS_SG;
1368 ops_touched = 1;
1369 break;
1370 case 'Q':
1371 ctx.cpu = -2;
1372 break;
1373 case 's':
1374 ctx.print_mode = PRINT_NONE;
1375 break;
1376 case 'q':
1377 ctx.print_mode = PRINT_LESS;
1378 break;
1379 case 'X':
1380 ctx.print_mode =
1381 (ctx.print_mode == PRINT_ASCII) ?
1382 PRINT_HEX_ASCII : PRINT_HEX;
1383 break;
1384 case 'l':
1385 ctx.print_mode =
1386 (ctx.print_mode == PRINT_HEX) ?
1387 PRINT_HEX_ASCII : PRINT_ASCII;
1388 break;
1389 case 'k':
1390 ctx.kpull = strtoul(optarg, NULL, 0);
1391 break;
1392 case 'n':
1393 frame_count_max = strtoul(optarg, NULL, 0);
1394 break;
1395 case 'F':
1396 ptr = optarg;
1397 for (j = i = strlen(optarg); i > 0; --i) {
1398 if (!isdigit(optarg[j - i]))
1399 break;
1400 ptr++;
1403 if (!strncmp(ptr, "KiB", strlen("KiB"))) {
1404 ctx.dump_interval = 1 << 10;
1405 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1406 } else if (!strncmp(ptr, "MiB", strlen("MiB"))) {
1407 ctx.dump_interval = 1 << 20;
1408 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1409 } else if (!strncmp(ptr, "GiB", strlen("GiB"))) {
1410 ctx.dump_interval = 1 << 30;
1411 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1412 } else if (!strncmp(ptr, "sec", strlen("sec"))) {
1413 ctx.dump_interval = 1;
1414 ctx.dump_mode = DUMP_INTERVAL_TIME;
1415 } else if (!strncmp(ptr, "min", strlen("min"))) {
1416 ctx.dump_interval = 60;
1417 ctx.dump_mode = DUMP_INTERVAL_TIME;
1418 } else if (!strncmp(ptr, "hrs", strlen("hrs"))) {
1419 ctx.dump_interval = 60 * 60;
1420 ctx.dump_mode = DUMP_INTERVAL_TIME;
1421 } else if (!strncmp(ptr, "s", strlen("s"))) {
1422 ctx.dump_interval = 1;
1423 ctx.dump_mode = DUMP_INTERVAL_TIME;
1424 } else {
1425 panic("Syntax error in time/size param!\n");
1428 ctx.dump_interval *= strtoul(optarg, NULL, 0);
1429 break;
1430 case 'V':
1431 ctx.verbose = true;
1432 break;
1433 case 'B':
1434 ctx.dump_bpf = true;
1435 break;
1436 case 'D':
1437 pcap_dump_type_features();
1438 die();
1439 break;
1440 case 'U':
1441 update_geoip();
1442 die();
1443 break;
1444 case 'v':
1445 version();
1446 break;
1447 case 'h':
1448 help();
1449 break;
1450 case '?':
1451 switch (optopt) {
1452 case 'd':
1453 case 'i':
1454 case 'o':
1455 case 'f':
1456 case 't':
1457 case 'P':
1458 case 'F':
1459 case 'n':
1460 case 'S':
1461 case 'b':
1462 case 'k':
1463 case 'T':
1464 case 'u':
1465 case 'g':
1466 case 'e':
1467 panic("Option -%c requires an argument!\n",
1468 optopt);
1469 default:
1470 if (isprint(optopt))
1471 printf("Unknown option character `0x%X\'!\n", optopt);
1472 die();
1474 default:
1475 break;
1479 if (!ctx.filter && optind != argc) {
1480 int ret;
1481 off_t offset = 0;
1483 for (i = optind; i < argc; ++i) {
1484 size_t alen = strlen(argv[i]) + 2;
1485 size_t flen = ctx.filter ? strlen(ctx.filter) : 0;
1487 ctx.filter = xrealloc(ctx.filter, flen + alen);
1488 ret = slprintf(ctx.filter + offset, strlen(argv[i]) + 2, "%s ", argv[i]);
1489 if (ret < 0)
1490 panic("Cannot concatenate filter string!\n");
1491 else
1492 offset += ret;
1496 if (!ctx.device_in)
1497 ctx.device_in = xstrdup("any");
1499 register_signal(SIGINT, signal_handler);
1500 register_signal(SIGQUIT, signal_handler);
1501 register_signal(SIGTERM, signal_handler);
1502 register_signal(SIGHUP, signal_handler);
1504 tprintf_init();
1506 if (prio_high) {
1507 set_proc_prio(-20);
1508 set_sched_status(SCHED_FIFO, sched_get_priority_max(SCHED_FIFO));
1511 if (device_mtu(ctx.device_in) || !strncmp("any", ctx.device_in, strlen(ctx.device_in))) {
1512 if (ctx.rfraw)
1513 setup_rfmon_mac80211_dev(&ctx, &ctx.device_in);
1515 ctx.link_type = pcap_devtype_to_linktype(ctx.device_in);
1517 if (!ctx.device_out) {
1518 ctx.dump = 0;
1519 main_loop = recv_only_or_dump;
1520 } else if (device_mtu(ctx.device_out)) {
1521 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1522 main_loop = receive_to_xmit;
1523 } else {
1524 ctx.dump = 1;
1525 register_signal_f(SIGALRM, timer_next_dump, SA_SIGINFO);
1526 main_loop = recv_only_or_dump;
1527 if (!ops_touched)
1528 ctx.pcap = PCAP_OPS_SG;
1530 } else {
1531 if (ctx.device_out && device_mtu(ctx.device_out)) {
1532 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1533 main_loop = pcap_to_xmit;
1534 if (!ops_touched)
1535 ctx.pcap = PCAP_OPS_MM;
1536 } else {
1537 main_loop = read_pcap;
1538 if (!ops_touched)
1539 ctx.pcap = PCAP_OPS_SG;
1543 bug_on(!main_loop);
1545 init_geoip(0);
1546 if (setsockmem)
1547 set_system_socket_memory(vals, array_size(vals));
1548 if (!ctx.enforce)
1549 xlockme();
1551 if (ctx.verbose)
1552 printf("pcap file I/O method: %s\n", pcap_ops_group_to_str[ctx.pcap]);
1554 main_loop(&ctx);
1556 if (!ctx.enforce)
1557 xunlockme();
1558 if (setsockmem)
1559 reset_system_socket_memory(vals, array_size(vals));
1560 destroy_geoip();
1562 device_restore_irq_affinity_list();
1563 tprintf_cleanup();
1565 destroy_ctx(&ctx);
1566 return 0;