-ng mac80211: Print "HT Capabilities" more structurd
[netsniff-ng.git] / netsniff-ng.c
blob2b9732e49ef4a7d289f04ef91d2337dd47cd318c
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_func_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);
316 dissector_entry_point(out, hdr->tp_h.tp_snaplen,
317 ctx->link_type, ctx->print_mode);
319 kernel_may_pull_from_tx(&hdr->tp_h);
321 it++;
322 if (it >= tx_ring.layout.tp_frame_nr)
323 it = 0;
325 if (unlikely(sigint == 1))
326 break;
328 if (frame_count_max != 0) {
329 if (ctx->tx_packets >= frame_count_max) {
330 sigint = 1;
331 break;
337 out:
338 bug_on(gettimeofday(&end, NULL));
339 timersub(&end, &start, &diff);
341 timer_purge();
343 bpf_release(&bpf_ops);
345 dissector_cleanup_all();
346 destroy_tx_ring(tx_sock, &tx_ring);
348 if (ctx->rfraw)
349 leave_rfmon_mac80211(ctx->device_out);
351 if (__pcap_io->prepare_close_pcap)
352 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
354 if (!strncmp("-", ctx->device_in, strlen("-")))
355 dup2(fd, fileno(stdin));
356 close(fd);
358 close(tx_sock);
360 fflush(stdout);
361 printf("\n");
362 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
363 printf("\r%12lu packets truncated in file\n", trunced);
364 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
365 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
368 static void receive_to_xmit(struct ctx *ctx)
370 short ifflags = 0;
371 uint8_t *in, *out;
372 int rx_sock, ifindex_in, ifindex_out, ret;
373 size_t size_in, size_out;
374 unsigned int it_in = 0, it_out = 0;
375 unsigned long frame_count = 0;
376 struct frame_map *hdr_in, *hdr_out;
377 struct ring tx_ring, rx_ring;
378 struct pollfd rx_poll;
379 struct sock_fprog bpf_ops;
381 if (!strncmp(ctx->device_in, ctx->device_out, IFNAMSIZ))
382 panic("Ingress/egress devices must be different!\n");
383 if (!device_up_and_running(ctx->device_out))
384 panic("Egress device not up and running!\n");
386 rx_sock = pf_socket();
387 tx_sock = pf_socket();
389 ifindex_in = device_ifindex(ctx->device_in);
390 ifindex_out = device_ifindex(ctx->device_out);
392 size_in = ring_size(ctx->device_in, ctx->reserve_size);
393 size_out = ring_size(ctx->device_out, ctx->reserve_size);
395 enable_kernel_bpf_jit_compiler();
397 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
398 if (ctx->dump_bpf)
399 bpf_dump_all(&bpf_ops);
400 bpf_attach_to_sock(rx_sock, &bpf_ops);
402 ring_rx_setup(&rx_ring, rx_sock, size_in, ifindex_in, &rx_poll, false, ctx->jumbo,
403 ctx->verbose, ctx->fanout_group, ctx->fanout_type);
404 ring_tx_setup(&tx_ring, tx_sock, size_out, ifindex_out, ctx->jumbo, ctx->verbose);
406 dissector_init_all(ctx->print_mode);
408 if (ctx->promiscuous)
409 ifflags = device_enter_promiscuous_mode(ctx->device_in);
411 if (ctx->kpull)
412 interval = ctx->kpull;
414 set_itimer_interval_value(&itimer, 0, interval);
415 setitimer(ITIMER_REAL, &itimer, NULL);
417 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
419 printf("Running! Hang up with ^C!\n\n");
420 fflush(stdout);
422 while (likely(sigint == 0)) {
423 while (user_may_pull_from_rx(rx_ring.frames[it_in].iov_base)) {
424 hdr_in = rx_ring.frames[it_in].iov_base;
425 in = ((uint8_t *) hdr_in) + hdr_in->tp_h.tp_mac;
427 frame_count++;
429 if (ctx->packet_type != -1)
430 if (ctx->packet_type != hdr_in->s_ll.sll_pkttype)
431 goto next;
433 hdr_out = tx_ring.frames[it_out].iov_base;
434 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
436 while (!user_may_pull_from_tx(tx_ring.frames[it_out].iov_base) &&
437 likely(!sigint)) {
438 if (ctx->randomize)
439 next_rnd_slot(&it_out, &tx_ring);
440 else {
441 it_out++;
442 if (it_out >= tx_ring.layout.tp_frame_nr)
443 it_out = 0;
446 hdr_out = tx_ring.frames[it_out].iov_base;
447 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
450 tpacket_hdr_clone(&hdr_out->tp_h, &hdr_in->tp_h);
451 fmemcpy(out, in, hdr_in->tp_h.tp_len);
453 kernel_may_pull_from_tx(&hdr_out->tp_h);
454 if (ctx->randomize)
455 next_rnd_slot(&it_out, &tx_ring);
456 else {
457 it_out++;
458 if (it_out >= tx_ring.layout.tp_frame_nr)
459 it_out = 0;
462 show_frame_hdr(in, hdr_in->tp_h.tp_snaplen,
463 ctx->link_type, hdr_in, ctx->print_mode);
465 dissector_entry_point(in, hdr_in->tp_h.tp_snaplen,
466 ctx->link_type, ctx->print_mode);
468 if (frame_count_max != 0) {
469 if (frame_count >= frame_count_max) {
470 sigint = 1;
471 break;
475 next:
476 kernel_may_pull_from_rx(&hdr_in->tp_h);
478 it_in++;
479 if (it_in >= rx_ring.layout.tp_frame_nr)
480 it_in = 0;
482 if (unlikely(sigint == 1))
483 goto out;
486 ret = poll(&rx_poll, 1, -1);
487 if (unlikely(ret < 0)) {
488 if (errno != EINTR)
489 panic("Poll failed!\n");
493 out:
494 timer_purge();
496 sock_rx_net_stats(rx_sock, 0);
498 bpf_release(&bpf_ops);
500 dissector_cleanup_all();
502 destroy_tx_ring(tx_sock, &tx_ring);
503 destroy_rx_ring(rx_sock, &rx_ring);
505 if (ctx->promiscuous)
506 device_leave_promiscuous_mode(ctx->device_in, ifflags);
508 close(tx_sock);
509 close(rx_sock);
512 static void translate_pcap_to_txf(int fdo, uint8_t *out, size_t len)
514 size_t bytes_done = 0;
515 char bout[80];
517 slprintf(bout, sizeof(bout), "{\n ");
518 write_or_die(fdo, bout, strlen(bout));
520 while (bytes_done < len) {
521 slprintf(bout, sizeof(bout), "0x%02x,", out[bytes_done]);
522 write_or_die(fdo, bout, strlen(bout));
524 bytes_done++;
526 if (bytes_done % 10 == 0) {
527 slprintf(bout, sizeof(bout), "\n");
528 write_or_die(fdo, bout, strlen(bout));
530 if (bytes_done < len) {
531 slprintf(bout, sizeof(bout), " ");
532 write_or_die(fdo, bout, strlen(bout));
534 } else if (bytes_done < len) {
535 slprintf(bout, sizeof(bout), " ");
536 write_or_die(fdo, bout, strlen(bout));
539 if (bytes_done % 10 != 0) {
540 slprintf(bout, sizeof(bout), "\n");
541 write_or_die(fdo, bout, strlen(bout));
544 slprintf(bout, sizeof(bout), "}\n\n");
545 write_or_die(fdo, bout, strlen(bout));
548 static void read_pcap(struct ctx *ctx)
550 uint8_t *out;
551 int ret, fd, fdo = 0;
552 unsigned long trunced = 0;
553 size_t out_len;
554 pcap_pkthdr_t phdr;
555 struct sock_fprog bpf_ops;
556 struct frame_map fm;
557 struct timeval start, end, diff;
558 bool is_out_pcap = ctx->device_out && strstr(ctx->device_out, ".pcap");
559 const struct pcap_file_ops *pcap_out_ops = pcap_ops[PCAP_OPS_RW];
561 bug_on(!__pcap_io);
563 if (!strncmp("-", ctx->device_in, strlen("-"))) {
564 fd = dup_or_die(fileno(stdin));
565 close(fileno(stdin));
566 if (ctx->pcap == PCAP_OPS_MM)
567 ctx->pcap = PCAP_OPS_SG;
568 } else {
569 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
572 if (__pcap_io->init_once_pcap)
573 __pcap_io->init_once_pcap(false);
575 ret = __pcap_io->pull_fhdr_pcap(fd, &ctx->magic, &ctx->link_type);
576 if (ret)
577 panic("Error reading pcap header!\n");
579 if (__pcap_io->prepare_access_pcap) {
580 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_RD, ctx->jumbo);
581 if (ret)
582 panic("Error prepare reading pcap!\n");
585 fmemset(&fm, 0, sizeof(fm));
587 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
588 if (ctx->dump_bpf)
589 bpf_dump_all(&bpf_ops);
591 dissector_init_all(ctx->print_mode);
593 out_len = round_up(1024 * 1024, RUNTIME_PAGE_SIZE);
594 out = xmalloc_aligned(out_len, CO_CACHE_LINE_SIZE);
596 if (ctx->device_out) {
597 if (!strncmp("-", ctx->device_out, strlen("-"))) {
598 fdo = dup_or_die(fileno(stdout));
599 close(fileno(stdout));
600 } else {
601 fdo = open_or_die_m(ctx->device_out, O_RDWR | O_CREAT |
602 O_TRUNC | O_LARGEFILE, DEFFILEMODE);
606 if (is_out_pcap) {
607 ret = pcap_out_ops->push_fhdr_pcap(fdo, ctx->magic,
608 ctx->link_type);
609 if (ret)
610 panic("Error writing pcap header!\n");
613 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
615 printf("Running! Hang up with ^C!\n\n");
616 fflush(stdout);
618 bug_on(gettimeofday(&start, NULL));
620 while (likely(sigint == 0)) {
621 do {
622 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic,
623 out, out_len);
624 if (unlikely(ret < 0))
625 goto out;
627 if (unlikely(pcap_get_length(&phdr, ctx->magic) == 0)) {
628 trunced++;
629 continue;
632 if (unlikely(pcap_get_length(&phdr, ctx->magic) > out_len)) {
633 pcap_set_length(&phdr, ctx->magic, out_len);
634 trunced++;
636 } while (ctx->filter &&
637 !bpf_run_filter(&bpf_ops, out,
638 pcap_get_length(&phdr, ctx->magic)));
640 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &fm.tp_h, &fm.s_ll);
642 ctx->tx_bytes += fm.tp_h.tp_len;
643 ctx->tx_packets++;
645 show_frame_hdr(out, fm.tp_h.tp_snaplen, ctx->link_type, &fm,
646 ctx->print_mode);
648 dissector_entry_point(out, fm.tp_h.tp_snaplen,
649 ctx->link_type, ctx->print_mode);
651 if (is_out_pcap) {
652 size_t pcap_len = pcap_get_length(&phdr, ctx->magic);
653 int wlen = pcap_out_ops->write_pcap(fdo, &phdr,
654 ctx->magic, out,
655 pcap_len);
656 if (unlikely(wlen != (int)pcap_get_total_length(&phdr, ctx->magic)))
657 panic("Error writing to pcap!\n");
658 } else if (ctx->device_out) {
659 translate_pcap_to_txf(fdo, out, fm.tp_h.tp_snaplen);
662 if (frame_count_max != 0) {
663 if (ctx->tx_packets >= frame_count_max) {
664 sigint = 1;
665 break;
670 out:
671 bug_on(gettimeofday(&end, NULL));
672 timersub(&end, &start, &diff);
674 bpf_release(&bpf_ops);
676 dissector_cleanup_all();
678 if (__pcap_io->prepare_close_pcap)
679 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
681 xfree(out);
683 fflush(stdout);
684 printf("\n");
685 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
686 printf("\r%12lu packets truncated in file\n", trunced);
687 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
688 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
690 if (!strncmp("-", ctx->device_in, strlen("-")))
691 dup2(fd, fileno(stdin));
692 close(fd);
694 if (ctx->device_out) {
695 if (!strncmp("-", ctx->device_out, strlen("-")))
696 dup2(fdo, fileno(stdout));
697 close(fdo);
701 static void finish_multi_pcap_file(struct ctx *ctx, int fd)
703 __pcap_io->fsync_pcap(fd);
705 if (__pcap_io->prepare_close_pcap)
706 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
708 close(fd);
710 fmemset(&itimer, 0, sizeof(itimer));
711 setitimer(ITIMER_REAL, &itimer, NULL);
714 static int next_multi_pcap_file(struct ctx *ctx, int fd)
716 int ret;
717 char fname[512];
719 __pcap_io->fsync_pcap(fd);
721 if (__pcap_io->prepare_close_pcap)
722 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
724 close(fd);
726 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
727 ctx->prefix ? : "dump-", time(NULL));
729 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
730 O_LARGEFILE, DEFFILEMODE);
732 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
733 if (ret)
734 panic("Error writing pcap header!\n");
736 if (__pcap_io->prepare_access_pcap) {
737 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, true);
738 if (ret)
739 panic("Error prepare writing pcap!\n");
742 return fd;
745 static void reset_interval(struct ctx *ctx)
747 if (ctx->dump_mode == DUMP_INTERVAL_TIME) {
748 interval = ctx->dump_interval;
750 set_itimer_interval_value(&itimer, interval, 0);
751 setitimer(ITIMER_REAL, &itimer, NULL);
752 } else {
753 interval = 0;
757 static int begin_multi_pcap_file(struct ctx *ctx)
759 int fd, ret;
760 char fname[256];
762 bug_on(!__pcap_io);
764 if (ctx->device_out[strlen(ctx->device_out) - 1] == '/')
765 ctx->device_out[strlen(ctx->device_out) - 1] = 0;
767 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
768 ctx->prefix ? : "dump-", time(NULL));
770 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
771 O_LARGEFILE, DEFFILEMODE);
773 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
774 if (ret)
775 panic("Error writing pcap header!\n");
777 if (__pcap_io->prepare_access_pcap) {
778 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, true);
779 if (ret)
780 panic("Error prepare writing pcap!\n");
783 reset_interval(ctx);
785 return fd;
788 static void finish_single_pcap_file(struct ctx *ctx, int fd)
790 __pcap_io->fsync_pcap(fd);
792 if (__pcap_io->prepare_close_pcap)
793 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
795 if (strncmp("-", ctx->device_out, strlen("-")))
796 close(fd);
797 else
798 dup2(fd, fileno(stdout));
801 static int begin_single_pcap_file(struct ctx *ctx)
803 int fd, ret;
805 bug_on(!__pcap_io);
807 if (!strncmp("-", ctx->device_out, strlen("-"))) {
808 fd = dup_or_die(fileno(stdout));
809 close(fileno(stdout));
810 if (ctx->pcap == PCAP_OPS_MM)
811 ctx->pcap = PCAP_OPS_SG;
812 } else {
813 fd = open_or_die_m(ctx->device_out,
814 O_RDWR | O_CREAT | O_TRUNC |
815 O_LARGEFILE, DEFFILEMODE);
818 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
819 if (ret)
820 panic("Error writing pcap header!\n");
822 if (__pcap_io->prepare_access_pcap) {
823 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, true);
824 if (ret)
825 panic("Error prepare writing pcap!\n");
828 return fd;
831 static void print_pcap_file_stats(int sock, struct ctx *ctx)
833 int ret;
834 struct tpacket_stats kstats;
835 socklen_t slen = sizeof(kstats);
837 fmemset(&kstats, 0, sizeof(kstats));
839 ret = getsockopt(sock, SOL_PACKET, PACKET_STATISTICS, &kstats, &slen);
840 if (unlikely(ret))
841 panic("Cannot get packet statistics!\n");
843 if (ctx->print_mode == PRINT_NONE) {
844 printf(".(+%u/-%u)", kstats.tp_packets - kstats.tp_drops,
845 kstats.tp_drops);
846 fflush(stdout);
850 static void update_pcap_next_dump(struct ctx *ctx, unsigned long snaplen, int *fd, int sock)
852 if (!dump_to_pcap(ctx))
853 return;
855 if (ctx->dump_mode == DUMP_INTERVAL_SIZE) {
856 interval += snaplen;
857 if (interval > ctx->dump_interval) {
858 next_dump = true;
859 interval = 0;
863 if (sighup) {
864 if (ctx->verbose)
865 printf("SIGHUP received, prematurely rotating pcap\n");
866 sighup = 0;
867 next_dump = true;
868 reset_interval(ctx);
871 if (next_dump) {
872 *fd = next_multi_pcap_file(ctx, *fd);
873 next_dump = false;
875 if (ctx->verbose)
876 print_pcap_file_stats(sock, ctx);
880 #ifdef HAVE_TPACKET3
881 static void walk_t3_block(struct block_desc *pbd, struct ctx *ctx,
882 int sock, int *fd, unsigned long *frame_count)
884 int num_pkts = pbd->h1.num_pkts, i;
885 struct tpacket3_hdr *hdr;
886 struct sockaddr_ll *sll;
888 hdr = (void *) ((uint8_t *) pbd + pbd->h1.offset_to_first_pkt);
889 sll = (void *) ((uint8_t *) hdr + TPACKET_ALIGN(sizeof(*hdr)));
891 for (i = 0; i < num_pkts && likely(sigint == 0); ++i) {
892 uint8_t *packet = ((uint8_t *) hdr + hdr->tp_mac);
893 pcap_pkthdr_t phdr;
895 if (ctx->packet_type != -1)
896 if (ctx->packet_type != sll->sll_pkttype)
897 goto next;
899 (*frame_count)++;
901 if (dump_to_pcap(ctx)) {
902 int ret;
904 tpacket3_hdr_to_pcap_pkthdr(hdr, sll, &phdr, ctx->magic);
906 ret = __pcap_io->write_pcap(*fd, &phdr, ctx->magic, packet,
907 pcap_get_length(&phdr, ctx->magic));
908 if (unlikely(ret != (int) pcap_get_total_length(&phdr, ctx->magic)))
909 panic("Write error to pcap!\n");
912 __show_frame_hdr(packet, hdr->tp_snaplen, ctx->link_type, sll,
913 hdr, ctx->print_mode, true);
915 dissector_entry_point(packet, hdr->tp_snaplen, ctx->link_type,
916 ctx->print_mode);
917 next:
918 hdr = (void *) ((uint8_t *) hdr + hdr->tp_next_offset);
919 sll = (void *) ((uint8_t *) hdr + TPACKET_ALIGN(sizeof(*hdr)));
921 if (frame_count_max != 0) {
922 if (unlikely(*frame_count >= frame_count_max)) {
923 sigint = 1;
924 break;
928 update_pcap_next_dump(ctx, hdr->tp_snaplen, fd, sock);
931 #endif /* HAVE_TPACKET3 */
933 static void recv_only_or_dump(struct ctx *ctx)
935 short ifflags = 0;
936 int sock, ifindex, fd = 0, ret;
937 size_t size;
938 unsigned int it = 0;
939 struct ring rx_ring;
940 struct pollfd rx_poll;
941 struct sock_fprog bpf_ops;
942 struct timeval start, end, diff;
943 unsigned long frame_count = 0;
945 sock = pf_socket();
947 ifindex = device_ifindex(ctx->device_in);
949 size = ring_size(ctx->device_in, ctx->reserve_size);
951 enable_kernel_bpf_jit_compiler();
953 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
954 if (ctx->dump_bpf)
955 bpf_dump_all(&bpf_ops);
956 bpf_attach_to_sock(sock, &bpf_ops);
958 if (ctx->hwtimestamp) {
959 ret = set_sockopt_hwtimestamp(sock, ctx->device_in);
960 if (ret == 0 && ctx->verbose)
961 printf("HW timestamping enabled\n");
964 ring_rx_setup(&rx_ring, sock, size, ifindex, &rx_poll, is_defined(HAVE_TPACKET3), true,
965 ctx->verbose, ctx->fanout_group, ctx->fanout_type);
967 dissector_init_all(ctx->print_mode);
969 if (ctx->cpu >= 0 && ifindex > 0) {
970 int irq = device_irq_number(ctx->device_in);
971 device_set_irq_affinity(irq, ctx->cpu);
973 if (ctx->verbose)
974 printf("IRQ: %s:%d > CPU%d\n",
975 ctx->device_in, irq, ctx->cpu);
978 if (ctx->promiscuous)
979 ifflags = device_enter_promiscuous_mode(ctx->device_in);
981 if (dump_to_pcap(ctx) && __pcap_io->init_once_pcap)
982 __pcap_io->init_once_pcap(true);
984 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
986 if (dump_to_pcap(ctx)) {
987 struct stat stats;
989 ret = stat(ctx->device_out, &stats);
990 if (ret < 0)
991 ctx->dump_dir = 0;
992 else
993 ctx->dump_dir = S_ISDIR(stats.st_mode);
995 if (ctx->dump_dir)
996 fd = begin_multi_pcap_file(ctx);
997 else
998 fd = begin_single_pcap_file(ctx);
1001 printf("Running! Hang up with ^C!\n\n");
1002 fflush(stdout);
1004 bug_on(gettimeofday(&start, NULL));
1006 while (likely(sigint == 0)) {
1007 #ifdef HAVE_TPACKET3
1008 struct block_desc *pbd;
1010 while (user_may_pull_from_rx_block((pbd = rx_ring.frames[it].iov_base))) {
1011 walk_t3_block(pbd, ctx, sock, &fd, &frame_count);
1013 kernel_may_pull_from_rx_block(pbd);
1014 it = (it + 1) % rx_ring.layout3.tp_block_nr;
1016 if (unlikely(sigint == 1))
1017 break;
1019 #else
1020 while (user_may_pull_from_rx(rx_ring.frames[it].iov_base)) {
1021 struct frame_map *hdr = rx_ring.frames[it].iov_base;
1022 uint8_t *packet = ((uint8_t *) hdr) + hdr->tp_h.tp_mac;
1023 pcap_pkthdr_t phdr;
1025 if (ctx->packet_type != -1)
1026 if (ctx->packet_type != hdr->s_ll.sll_pkttype)
1027 goto next;
1029 frame_count++;
1031 if (unlikely(ring_frame_size(&rx_ring) < hdr->tp_h.tp_snaplen)) {
1032 /* XXX: silently ignore for now. We used to
1033 * report them with sock_rx_net_stats() */
1034 goto next;
1037 if (dump_to_pcap(ctx)) {
1038 tpacket_hdr_to_pcap_pkthdr(&hdr->tp_h, &hdr->s_ll, &phdr, ctx->magic);
1040 ret = __pcap_io->write_pcap(fd, &phdr, ctx->magic, packet,
1041 pcap_get_length(&phdr, ctx->magic));
1042 if (unlikely(ret != (int) pcap_get_total_length(&phdr, ctx->magic)))
1043 panic("Write error to pcap!\n");
1046 show_frame_hdr(packet, hdr->tp_h.tp_snaplen,
1047 ctx->link_type, hdr, ctx->print_mode);
1049 dissector_entry_point(packet, hdr->tp_h.tp_snaplen,
1050 ctx->link_type, ctx->print_mode);
1052 if (frame_count_max != 0) {
1053 if (unlikely(frame_count >= frame_count_max)) {
1054 sigint = 1;
1055 break;
1059 next:
1060 kernel_may_pull_from_rx(&hdr->tp_h);
1061 it = (it + 1) % rx_ring.layout.tp_frame_nr;
1063 if (unlikely(sigint == 1))
1064 break;
1066 update_pcap_next_dump(ctx, hdr->tp_h.tp_snaplen, &fd, sock);
1068 #endif /* HAVE_TPACKET3 */
1070 ret = poll(&rx_poll, 1, -1);
1071 if (unlikely(ret < 0)) {
1072 if (errno != EINTR)
1073 panic("Poll failed!\n");
1077 bug_on(gettimeofday(&end, NULL));
1078 timersub(&end, &start, &diff);
1080 if (!(ctx->dump_dir && ctx->print_mode == PRINT_NONE)) {
1081 sock_rx_net_stats(sock, frame_count);
1083 printf("\r%12lu sec, %lu usec in total\n",
1084 diff.tv_sec, diff.tv_usec);
1085 } else {
1086 printf("\n\n");
1087 fflush(stdout);
1090 bpf_release(&bpf_ops);
1091 dissector_cleanup_all();
1092 destroy_rx_ring(sock, &rx_ring);
1094 if (ctx->promiscuous)
1095 device_leave_promiscuous_mode(ctx->device_in, ifflags);
1097 if (ctx->rfraw)
1098 leave_rfmon_mac80211(ctx->device_in);
1100 if (dump_to_pcap(ctx)) {
1101 if (ctx->dump_dir)
1102 finish_multi_pcap_file(ctx, fd);
1103 else
1104 finish_single_pcap_file(ctx, fd);
1107 close(sock);
1110 static void init_ctx(struct ctx *ctx)
1112 memset(ctx, 0, sizeof(*ctx));
1114 ctx->uid = getuid();
1115 ctx->gid = getgid();
1117 ctx->cpu = -1;
1118 ctx->packet_type = -1;
1120 ctx->fanout_type = PACKET_FANOUT_ROLLOVER;
1122 ctx->magic = ORIGINAL_TCPDUMP_MAGIC;
1123 ctx->print_mode = PRINT_NORM;
1124 ctx->pcap = PCAP_OPS_SG;
1126 ctx->dump_mode = DUMP_INTERVAL_TIME;
1127 ctx->dump_interval = 60;
1129 ctx->promiscuous = true;
1130 ctx->randomize = false;
1131 ctx->hwtimestamp = true;
1134 static void destroy_ctx(struct ctx *ctx)
1136 free(ctx->device_in);
1137 free(ctx->device_out);
1138 free(ctx->device_trans);
1140 free(ctx->prefix);
1143 static void __noreturn help(void)
1145 printf("netsniff-ng %s, the packet sniffing beast\n", VERSION_STRING);
1146 puts("http://www.netsniff-ng.org\n\n"
1147 "Usage: netsniff-ng [options] [filter-expression]\n"
1148 "Options:\n"
1149 " -i|-d|--dev|--in <dev|pcap|-> Input source as netdev, pcap or pcap stdin\n"
1150 " -o|--out <dev|pcap|dir|cfg|-> Output sink as netdev, pcap, directory, trafgen, or stdout\n"
1151 " -C|--fanout-group <id> Join packet fanout group\n"
1152 " -K|--fanout-type <type> Apply fanout discipline: hash|lb|cpu|rnd|roll|qm\n"
1153 " -L|--fanout-opts <opts> Additional fanout options: defrag|roll\n"
1154 " -f|--filter <bpf-file|expr> Use BPF filter file from bpfc or tcpdump-like expression\n"
1155 " -t|--type <type> Filter for: host|broadcast|multicast|others|outgoing\n"
1156 " -F|--interval <size|time> Dump interval if -o is a dir: <num>KiB/MiB/GiB/s/sec/min/hrs\n"
1157 " -R|--rfraw Capture or inject raw 802.11 frames\n"
1158 " -n|--num <0|uint> Number of packets until exit (def: 0)\n"
1159 " -P|--prefix <name> Prefix for pcaps stored in directory\n"
1160 " -T|--magic <pcap-magic> Pcap magic number/pcap format to store, see -D\n"
1161 " -D|--dump-pcap-types Dump pcap types and magic numbers and quit\n"
1162 " -B|--dump-bpf Dump generated BPF assembly\n"
1163 " -r|--rand Randomize packet forwarding order (dev->dev)\n"
1164 " -M|--no-promisc No promiscuous mode for netdev\n"
1165 " -A|--no-sock-mem Don't tune core socket memory\n"
1166 " -N|--no-hwtimestamp Disable hardware time stamping\n"
1167 " -m|--mmap Mmap(2) pcap file I/O, e.g. for replaying pcaps\n"
1168 " -G|--sg Scatter/gather pcap file I/O\n"
1169 " -c|--clrw Use slower read(2)/write(2) I/O\n"
1170 " -S|--ring-size <size> Specify ring size to: <num>KiB/MiB/GiB\n"
1171 " -k|--kernel-pull <uint> Kernel pull from user interval in us (def: 10us)\n"
1172 " -J|--jumbo-support Support replay/fwd 64KB Super Jumbo Frames (def: 2048B)\n"
1173 " -b|--bind-cpu <cpu> Bind to specific CPU\n"
1174 " -u|--user <userid> Drop privileges and change to userid\n"
1175 " -g|--group <groupid> Drop privileges and change to groupid\n"
1176 " -H|--prio-high Make this high priority process\n"
1177 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
1178 " -s|--silent Do not print captured packets\n"
1179 " -q|--less Print less-verbose packet information\n"
1180 " -X|--hex Print packet data in hex format\n"
1181 " -l|--ascii Print human-readable packet data\n"
1182 " -U|--update Update GeoIP databases\n"
1183 " -V|--verbose Be more verbose\n"
1184 " -v|--version Show version and exit\n"
1185 " -h|--help Guess what?!\n\n"
1186 "Examples:\n"
1187 " netsniff-ng --in eth0 --out dump.pcap -s -T 0xa1b2c3d4 --b 0 tcp or udp\n"
1188 " netsniff-ng --in wlan0 --rfraw --out dump.pcap --silent --bind-cpu 0\n"
1189 " netsniff-ng --in dump.pcap --mmap --out eth0 -k1000 --silent --bind-cpu 0\n"
1190 " netsniff-ng --in dump.pcap --out dump.cfg --silent --bind-cpu 0\n"
1191 " netsniff-ng --in dump.pcap --out dump2.pcap --silent tcp\n"
1192 " netsniff-ng --in eth0 --out eth1 --silent --bind-cpu 0 -J --type host\n"
1193 " netsniff-ng --in eth1 --out /opt/probe/ -s -m --interval 100MiB -b 0\n"
1194 " netsniff-ng --in vlan0 --out dump.pcap -c -u `id -u bob` -g `id -g bob`\n"
1195 " netsniff-ng --in any --filter http.bpf --jumbo-support --ascii -V\n\n"
1196 "Note:\n"
1197 " For introducing bit errors, delays with random variation and more\n"
1198 " while replaying pcaps, make use of tc(8) with its disciplines (e.g. netem).\n");
1199 puts(copyright);
1200 die();
1203 static void __noreturn version(void)
1205 printf("netsniff-ng %s, Git id: %s\n", VERSION_LONG, GITVERSION);
1206 puts("the packet sniffing beast\n"
1207 "http://www.netsniff-ng.org\n");
1208 puts(copyright);
1209 die();
1212 int main(int argc, char **argv)
1214 char *ptr;
1215 int c, i, j, cpu_tmp, opt_index, ops_touched = 0, vals[4] = {0};
1216 bool prio_high = false, setsockmem = true;
1217 void (*main_loop)(struct ctx *ctx) = NULL;
1218 struct ctx ctx;
1220 init_ctx(&ctx);
1221 srand(time(NULL));
1223 while ((c = getopt_long(argc, argv, short_options, long_options,
1224 &opt_index)) != EOF) {
1225 switch (c) {
1226 case 'd':
1227 case 'i':
1228 ctx.device_in = xstrdup(optarg);
1229 break;
1230 case 'o':
1231 ctx.device_out = xstrdup(optarg);
1232 break;
1233 case 'P':
1234 ctx.prefix = xstrdup(optarg);
1235 break;
1236 case 'R':
1237 ctx.rfraw = 1;
1238 break;
1239 case 'r':
1240 ctx.randomize = true;
1241 break;
1242 case 'J':
1243 ctx.jumbo = true;
1244 break;
1245 case 'T':
1246 ctx.magic = (uint32_t) strtoul(optarg, NULL, 0);
1247 pcap_check_magic(ctx.magic);
1248 break;
1249 case 'f':
1250 ctx.filter = xstrdup(optarg);
1251 break;
1252 case 'M':
1253 ctx.promiscuous = false;
1254 break;
1255 case 'N':
1256 ctx.hwtimestamp = false;
1257 break;
1258 case 'A':
1259 setsockmem = false;
1260 break;
1261 case 'u':
1262 ctx.uid = strtoul(optarg, NULL, 0);
1263 ctx.enforce = true;
1264 break;
1265 case 'g':
1266 ctx.gid = strtoul(optarg, NULL, 0);
1267 ctx.enforce = true;
1268 break;
1269 case 'C':
1270 ctx.fanout_group = strtoul(optarg, NULL, 0);
1271 if (ctx.fanout_group == 0)
1272 panic("Non-zero fanout group id required!\n");
1273 break;
1274 case 'K':
1275 if (!strncmp(optarg, "hash", strlen("hash")))
1276 ctx.fanout_type = PACKET_FANOUT_HASH;
1277 else if (!strncmp(optarg, "lb", strlen("lb")))
1278 ctx.fanout_type = PACKET_FANOUT_LB;
1279 else if (!strncmp(optarg, "cpu", strlen("cpu")))
1280 ctx.fanout_type = PACKET_FANOUT_CPU;
1281 else if (!strncmp(optarg, "rnd", strlen("rnd")))
1282 ctx.fanout_type = PACKET_FANOUT_RND;
1283 else if (!strncmp(optarg, "roll", strlen("roll")))
1284 ctx.fanout_type = PACKET_FANOUT_ROLLOVER;
1285 else if (!strncmp(optarg, "qm", strlen("qm")))
1286 ctx.fanout_type = PACKET_FANOUT_QM;
1287 else
1288 panic("Unkown fanout type!\n");
1289 break;
1290 case 'L':
1291 if (!strncmp(optarg, "defrag", strlen("defrag")))
1292 ctx.fanout_type |= PACKET_FANOUT_FLAG_DEFRAG;
1293 else if (!strncmp(optarg, "roll", strlen("roll")))
1294 ctx.fanout_type |= PACKET_FANOUT_FLAG_ROLLOVER;
1295 else
1296 panic("Unkown fanout option!\n");
1297 break;
1298 case 't':
1299 if (!strncmp(optarg, "host", strlen("host")))
1300 ctx.packet_type = PACKET_HOST;
1301 else if (!strncmp(optarg, "broadcast", strlen("broadcast")))
1302 ctx.packet_type = PACKET_BROADCAST;
1303 else if (!strncmp(optarg, "multicast", strlen("multicast")))
1304 ctx.packet_type = PACKET_MULTICAST;
1305 else if (!strncmp(optarg, "others", strlen("others")))
1306 ctx.packet_type = PACKET_OTHERHOST;
1307 else if (!strncmp(optarg, "outgoing", strlen("outgoing")))
1308 ctx.packet_type = PACKET_OUTGOING;
1309 else
1310 ctx.packet_type = -1;
1311 break;
1312 case 'S':
1313 ptr = optarg;
1314 for (j = i = strlen(optarg); i > 0; --i) {
1315 if (!isdigit(optarg[j - i]))
1316 break;
1317 ptr++;
1320 if (!strncmp(ptr, "KiB", strlen("KiB")))
1321 ctx.reserve_size = 1 << 10;
1322 else if (!strncmp(ptr, "MiB", strlen("MiB")))
1323 ctx.reserve_size = 1 << 20;
1324 else if (!strncmp(ptr, "GiB", strlen("GiB")))
1325 ctx.reserve_size = 1 << 30;
1326 else
1327 panic("Syntax error in ring size param!\n");
1329 ctx.reserve_size *= strtoul(optarg, NULL, 0);
1330 break;
1331 case 'b':
1332 cpu_tmp = strtol(optarg, NULL, 0);
1334 cpu_affinity(cpu_tmp);
1335 if (ctx.cpu != -2)
1336 ctx.cpu = cpu_tmp;
1337 break;
1338 case 'H':
1339 prio_high = true;
1340 break;
1341 case 'c':
1342 ctx.pcap = PCAP_OPS_RW;
1343 ops_touched = 1;
1344 break;
1345 case 'm':
1346 ctx.pcap = PCAP_OPS_MM;
1347 ops_touched = 1;
1348 break;
1349 case 'G':
1350 ctx.pcap = PCAP_OPS_SG;
1351 ops_touched = 1;
1352 break;
1353 case 'Q':
1354 ctx.cpu = -2;
1355 break;
1356 case 's':
1357 ctx.print_mode = PRINT_NONE;
1358 break;
1359 case 'q':
1360 ctx.print_mode = PRINT_LESS;
1361 break;
1362 case 'X':
1363 ctx.print_mode =
1364 (ctx.print_mode == PRINT_ASCII) ?
1365 PRINT_HEX_ASCII : PRINT_HEX;
1366 break;
1367 case 'l':
1368 ctx.print_mode =
1369 (ctx.print_mode == PRINT_HEX) ?
1370 PRINT_HEX_ASCII : PRINT_ASCII;
1371 break;
1372 case 'k':
1373 ctx.kpull = strtoul(optarg, NULL, 0);
1374 break;
1375 case 'n':
1376 frame_count_max = strtoul(optarg, NULL, 0);
1377 break;
1378 case 'F':
1379 ptr = optarg;
1380 for (j = i = strlen(optarg); i > 0; --i) {
1381 if (!isdigit(optarg[j - i]))
1382 break;
1383 ptr++;
1386 if (!strncmp(ptr, "KiB", strlen("KiB"))) {
1387 ctx.dump_interval = 1 << 10;
1388 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1389 } else if (!strncmp(ptr, "MiB", strlen("MiB"))) {
1390 ctx.dump_interval = 1 << 20;
1391 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1392 } else if (!strncmp(ptr, "GiB", strlen("GiB"))) {
1393 ctx.dump_interval = 1 << 30;
1394 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1395 } else if (!strncmp(ptr, "sec", strlen("sec"))) {
1396 ctx.dump_interval = 1;
1397 ctx.dump_mode = DUMP_INTERVAL_TIME;
1398 } else if (!strncmp(ptr, "min", strlen("min"))) {
1399 ctx.dump_interval = 60;
1400 ctx.dump_mode = DUMP_INTERVAL_TIME;
1401 } else if (!strncmp(ptr, "hrs", strlen("hrs"))) {
1402 ctx.dump_interval = 60 * 60;
1403 ctx.dump_mode = DUMP_INTERVAL_TIME;
1404 } else if (!strncmp(ptr, "s", strlen("s"))) {
1405 ctx.dump_interval = 1;
1406 ctx.dump_mode = DUMP_INTERVAL_TIME;
1407 } else {
1408 panic("Syntax error in time/size param!\n");
1411 ctx.dump_interval *= strtoul(optarg, NULL, 0);
1412 break;
1413 case 'V':
1414 ctx.verbose = true;
1415 break;
1416 case 'B':
1417 ctx.dump_bpf = true;
1418 break;
1419 case 'D':
1420 pcap_dump_type_features();
1421 die();
1422 break;
1423 case 'U':
1424 update_geoip();
1425 die();
1426 break;
1427 case 'v':
1428 version();
1429 break;
1430 case 'h':
1431 help();
1432 break;
1433 case '?':
1434 switch (optopt) {
1435 case 'd':
1436 case 'i':
1437 case 'o':
1438 case 'f':
1439 case 't':
1440 case 'P':
1441 case 'F':
1442 case 'n':
1443 case 'S':
1444 case 'b':
1445 case 'k':
1446 case 'T':
1447 case 'u':
1448 case 'g':
1449 case 'e':
1450 panic("Option -%c requires an argument!\n",
1451 optopt);
1452 default:
1453 if (isprint(optopt))
1454 printf("Unknown option character `0x%X\'!\n", optopt);
1455 die();
1457 default:
1458 break;
1462 if (!ctx.filter && optind != argc) {
1463 int ret;
1464 off_t offset = 0;
1466 for (i = optind; i < argc; ++i) {
1467 size_t alen = strlen(argv[i]) + 2;
1468 size_t flen = ctx.filter ? strlen(ctx.filter) : 0;
1470 ctx.filter = xrealloc(ctx.filter, flen + alen);
1471 ret = slprintf(ctx.filter + offset, strlen(argv[i]) + 2, "%s ", argv[i]);
1472 if (ret < 0)
1473 panic("Cannot concatenate filter string!\n");
1474 else
1475 offset += ret;
1479 if (!ctx.device_in)
1480 ctx.device_in = xstrdup("any");
1482 register_signal(SIGINT, signal_handler);
1483 register_signal(SIGQUIT, signal_handler);
1484 register_signal(SIGTERM, signal_handler);
1485 register_signal(SIGHUP, signal_handler);
1487 tprintf_init();
1489 if (prio_high) {
1490 set_proc_prio(-20);
1491 set_sched_status(SCHED_FIFO, sched_get_priority_max(SCHED_FIFO));
1494 if (device_mtu(ctx.device_in) || !strncmp("any", ctx.device_in, strlen(ctx.device_in))) {
1495 if (ctx.rfraw)
1496 setup_rfmon_mac80211_dev(&ctx, &ctx.device_in);
1498 ctx.link_type = pcap_devtype_to_linktype(ctx.device_in);
1500 if (!ctx.device_out) {
1501 ctx.dump = 0;
1502 main_loop = recv_only_or_dump;
1503 } else if (device_mtu(ctx.device_out)) {
1504 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1505 main_loop = receive_to_xmit;
1506 } else {
1507 ctx.dump = 1;
1508 register_signal_f(SIGALRM, timer_next_dump, SA_SIGINFO);
1509 main_loop = recv_only_or_dump;
1510 if (!ops_touched)
1511 ctx.pcap = PCAP_OPS_SG;
1513 } else {
1514 if (ctx.device_out && device_mtu(ctx.device_out)) {
1515 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1516 main_loop = pcap_to_xmit;
1517 if (!ops_touched)
1518 ctx.pcap = PCAP_OPS_MM;
1519 } else {
1520 main_loop = read_pcap;
1521 if (!ops_touched)
1522 ctx.pcap = PCAP_OPS_SG;
1526 bug_on(!main_loop);
1528 init_geoip(0);
1529 if (setsockmem)
1530 set_system_socket_memory(vals, array_size(vals));
1531 if (!ctx.enforce)
1532 xlockme();
1534 if (ctx.verbose)
1535 printf("pcap file I/O method: %s\n", pcap_ops_group_to_str[ctx.pcap]);
1537 main_loop(&ctx);
1539 if (!ctx.enforce)
1540 xunlockme();
1541 if (setsockmem)
1542 reset_system_socket_memory(vals, array_size(vals));
1543 destroy_geoip();
1545 device_restore_irq_affinity_list();
1546 tprintf_cleanup();
1548 destroy_ctx(&ctx);
1549 return 0;