build: Rename LD command variable to LDQ
[netsniff-ng.git] / netsniff-ng.c
blobee9dc38cedbab254f64dcacd754f618329286b91
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;
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 case SIGHUP:
138 default:
139 break;
143 static void timer_elapsed(int unused __maybe_unused)
145 int ret;
147 set_itimer_interval_value(&itimer, 0, interval);
149 ret = pull_and_flush_tx_ring(tx_sock);
150 if (unlikely(ret < 0)) {
151 /* We could hit EBADF if the socket has been closed before
152 * the timer was triggered.
154 if (errno != EBADF && errno != ENOBUFS)
155 panic("Flushing TX_RING failed: %s!\n", strerror(errno));
158 setitimer(ITIMER_REAL, &itimer, NULL);
161 static void timer_purge(void)
163 int ret;
165 ret = pull_and_flush_tx_ring_wait(tx_sock);
166 if (unlikely(ret < 0)) {
167 if (errno != EBADF && errno != ENOBUFS)
168 panic("Flushing TX_RING failed: %s!\n", strerror(errno));
171 set_itimer_interval_value(&itimer, 0, 0);
172 setitimer(ITIMER_REAL, &itimer, NULL);
175 static void timer_next_dump(int unused __maybe_unused)
177 set_itimer_interval_value(&itimer, interval, 0);
178 next_dump = true;
179 setitimer(ITIMER_REAL, &itimer, NULL);
182 static inline bool dump_to_pcap(struct ctx *ctx)
184 return ctx->dump;
187 static void on_panic_del_rfmon(void *arg)
189 leave_rfmon_mac80211(arg);
192 static inline void setup_rfmon_mac80211_dev(struct ctx *ctx, char **rfmon_dev)
194 ctx->device_trans = xstrdup(*rfmon_dev);
195 xfree(*rfmon_dev);
197 enter_rfmon_mac80211(ctx->device_trans, rfmon_dev);
198 panic_func_add(on_panic_del_rfmon, *rfmon_dev);
201 static void pcap_to_xmit(struct ctx *ctx)
203 uint8_t *out = NULL;
204 int ifindex, fd = 0, ret;
205 size_t size;
206 unsigned int it = 0;
207 unsigned long trunced = 0;
208 struct ring tx_ring;
209 struct frame_map *hdr;
210 struct sock_fprog bpf_ops;
211 struct timeval start, end, diff;
212 pcap_pkthdr_t phdr;
214 if (!device_up_and_running(ctx->device_out) && !ctx->rfraw)
215 panic("Device not up and running!\n");
217 bug_on(!__pcap_io);
219 tx_sock = pf_socket();
221 if (!strncmp("-", ctx->device_in, strlen("-"))) {
222 fd = dup_or_die(fileno(stdin));
223 close(fileno(stdin));
224 if (ctx->pcap == PCAP_OPS_MM)
225 ctx->pcap = PCAP_OPS_SG;
226 } else {
227 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
230 if (__pcap_io->init_once_pcap)
231 __pcap_io->init_once_pcap(true);
233 ret = __pcap_io->pull_fhdr_pcap(fd, &ctx->magic, &ctx->link_type);
234 if (ret)
235 panic("Error reading pcap header!\n");
237 if (__pcap_io->prepare_access_pcap) {
238 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_RD, ctx->jumbo);
239 if (ret)
240 panic("Error prepare reading pcap!\n");
243 if (ctx->rfraw) {
244 setup_rfmon_mac80211_dev(ctx, &ctx->device_out);
246 if (ctx->link_type != LINKTYPE_IEEE802_11 &&
247 ctx->link_type != LINKTYPE_IEEE802_11_RADIOTAP)
248 panic("Wrong linktype of pcap!\n");
251 ifindex = device_ifindex(ctx->device_out);
252 size = ring_size(ctx->device_out, ctx->reserve_size);
254 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
255 if (ctx->dump_bpf)
256 bpf_dump_all(&bpf_ops);
258 ring_tx_setup(&tx_ring, tx_sock, size, ifindex, ctx->jumbo, ctx->verbose);
260 dissector_init_all(ctx->print_mode);
262 if (ctx->cpu >= 0 && ifindex > 0) {
263 int irq = device_irq_number(ctx->device_out);
264 device_set_irq_affinity(irq, ctx->cpu);
266 if (ctx->verbose)
267 printf("IRQ: %s:%d > CPU%d\n",
268 ctx->device_out, irq, ctx->cpu);
271 if (ctx->kpull)
272 interval = ctx->kpull;
274 set_itimer_interval_value(&itimer, 0, interval);
275 setitimer(ITIMER_REAL, &itimer, NULL);
277 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
279 printf("Running! Hang up with ^C!\n\n");
280 fflush(stdout);
282 bug_on(gettimeofday(&start, NULL));
284 while (likely(sigint == 0)) {
285 while (user_may_pull_from_tx(tx_ring.frames[it].iov_base)) {
286 hdr = tx_ring.frames[it].iov_base;
287 out = ((uint8_t *) hdr) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
289 do {
290 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic, out,
291 ring_frame_size(&tx_ring));
292 if (unlikely(ret <= 0))
293 goto out;
295 if (ring_frame_size(&tx_ring) <
296 pcap_get_length(&phdr, ctx->magic)) {
297 pcap_set_length(&phdr, ctx->magic,
298 ring_frame_size(&tx_ring));
299 trunced++;
301 } while (ctx->filter &&
302 !bpf_run_filter(&bpf_ops, out,
303 pcap_get_length(&phdr, ctx->magic)));
305 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &hdr->tp_h, NULL);
307 ctx->tx_bytes += hdr->tp_h.tp_len;;
308 ctx->tx_packets++;
310 show_frame_hdr(out, hdr->tp_h.tp_snaplen,
311 ctx->link_type, hdr, ctx->print_mode);
313 dissector_entry_point(out, hdr->tp_h.tp_snaplen,
314 ctx->link_type, ctx->print_mode);
316 kernel_may_pull_from_tx(&hdr->tp_h);
318 it++;
319 if (it >= tx_ring.layout.tp_frame_nr)
320 it = 0;
322 if (unlikely(sigint == 1))
323 break;
325 if (frame_count_max != 0) {
326 if (ctx->tx_packets >= frame_count_max) {
327 sigint = 1;
328 break;
334 out:
335 bug_on(gettimeofday(&end, NULL));
336 timersub(&end, &start, &diff);
338 timer_purge();
340 bpf_release(&bpf_ops);
342 dissector_cleanup_all();
343 destroy_tx_ring(tx_sock, &tx_ring);
345 if (ctx->rfraw)
346 leave_rfmon_mac80211(ctx->device_out);
348 if (__pcap_io->prepare_close_pcap)
349 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
351 if (!strncmp("-", ctx->device_in, strlen("-")))
352 dup2(fd, fileno(stdin));
353 close(fd);
355 close(tx_sock);
357 fflush(stdout);
358 printf("\n");
359 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
360 printf("\r%12lu packets truncated in file\n", trunced);
361 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
362 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
365 static void receive_to_xmit(struct ctx *ctx)
367 short ifflags = 0;
368 uint8_t *in, *out;
369 int rx_sock, ifindex_in, ifindex_out, ret;
370 size_t size_in, size_out;
371 unsigned int it_in = 0, it_out = 0;
372 unsigned long frame_count = 0;
373 struct frame_map *hdr_in, *hdr_out;
374 struct ring tx_ring, rx_ring;
375 struct pollfd rx_poll;
376 struct sock_fprog bpf_ops;
378 if (!strncmp(ctx->device_in, ctx->device_out, IFNAMSIZ))
379 panic("Ingress/egress devices must be different!\n");
380 if (!device_up_and_running(ctx->device_out))
381 panic("Egress device not up and running!\n");
383 rx_sock = pf_socket();
384 tx_sock = pf_socket();
386 ifindex_in = device_ifindex(ctx->device_in);
387 ifindex_out = device_ifindex(ctx->device_out);
389 size_in = ring_size(ctx->device_in, ctx->reserve_size);
390 size_out = ring_size(ctx->device_out, ctx->reserve_size);
392 enable_kernel_bpf_jit_compiler();
394 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
395 if (ctx->dump_bpf)
396 bpf_dump_all(&bpf_ops);
397 bpf_attach_to_sock(rx_sock, &bpf_ops);
399 ring_rx_setup(&rx_ring, rx_sock, size_in, ifindex_in, &rx_poll, false, ctx->jumbo,
400 ctx->verbose, ctx->fanout_group, ctx->fanout_type);
401 ring_tx_setup(&tx_ring, tx_sock, size_out, ifindex_out, ctx->jumbo, ctx->verbose);
403 dissector_init_all(ctx->print_mode);
405 if (ctx->promiscuous)
406 ifflags = device_enter_promiscuous_mode(ctx->device_in);
408 if (ctx->kpull)
409 interval = ctx->kpull;
411 set_itimer_interval_value(&itimer, 0, interval);
412 setitimer(ITIMER_REAL, &itimer, NULL);
414 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
416 printf("Running! Hang up with ^C!\n\n");
417 fflush(stdout);
419 while (likely(sigint == 0)) {
420 while (user_may_pull_from_rx(rx_ring.frames[it_in].iov_base)) {
421 hdr_in = rx_ring.frames[it_in].iov_base;
422 in = ((uint8_t *) hdr_in) + hdr_in->tp_h.tp_mac;
424 frame_count++;
426 if (ctx->packet_type != -1)
427 if (ctx->packet_type != hdr_in->s_ll.sll_pkttype)
428 goto next;
430 hdr_out = tx_ring.frames[it_out].iov_base;
431 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
433 while (!user_may_pull_from_tx(tx_ring.frames[it_out].iov_base) &&
434 likely(!sigint)) {
435 if (ctx->randomize)
436 next_rnd_slot(&it_out, &tx_ring);
437 else {
438 it_out++;
439 if (it_out >= tx_ring.layout.tp_frame_nr)
440 it_out = 0;
443 hdr_out = tx_ring.frames[it_out].iov_base;
444 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
447 tpacket_hdr_clone(&hdr_out->tp_h, &hdr_in->tp_h);
448 fmemcpy(out, in, hdr_in->tp_h.tp_len);
450 kernel_may_pull_from_tx(&hdr_out->tp_h);
451 if (ctx->randomize)
452 next_rnd_slot(&it_out, &tx_ring);
453 else {
454 it_out++;
455 if (it_out >= tx_ring.layout.tp_frame_nr)
456 it_out = 0;
459 show_frame_hdr(in, hdr_in->tp_h.tp_snaplen,
460 ctx->link_type, hdr_in, ctx->print_mode);
462 dissector_entry_point(in, hdr_in->tp_h.tp_snaplen,
463 ctx->link_type, ctx->print_mode);
465 if (frame_count_max != 0) {
466 if (frame_count >= frame_count_max) {
467 sigint = 1;
468 break;
472 next:
473 kernel_may_pull_from_rx(&hdr_in->tp_h);
475 it_in++;
476 if (it_in >= rx_ring.layout.tp_frame_nr)
477 it_in = 0;
479 if (unlikely(sigint == 1))
480 goto out;
483 ret = poll(&rx_poll, 1, -1);
484 if (unlikely(ret < 0)) {
485 if (errno != EINTR)
486 panic("Poll failed!\n");
490 out:
491 timer_purge();
493 sock_rx_net_stats(rx_sock, 0);
495 bpf_release(&bpf_ops);
497 dissector_cleanup_all();
499 destroy_tx_ring(tx_sock, &tx_ring);
500 destroy_rx_ring(rx_sock, &rx_ring);
502 if (ctx->promiscuous)
503 device_leave_promiscuous_mode(ctx->device_in, ifflags);
505 close(tx_sock);
506 close(rx_sock);
509 static void translate_pcap_to_txf(int fdo, uint8_t *out, size_t len)
511 size_t bytes_done = 0;
512 char bout[80];
514 slprintf(bout, sizeof(bout), "{\n ");
515 write_or_die(fdo, bout, strlen(bout));
517 while (bytes_done < len) {
518 slprintf(bout, sizeof(bout), "0x%02x,", out[bytes_done]);
519 write_or_die(fdo, bout, strlen(bout));
521 bytes_done++;
523 if (bytes_done % 10 == 0) {
524 slprintf(bout, sizeof(bout), "\n");
525 write_or_die(fdo, bout, strlen(bout));
527 if (bytes_done < len) {
528 slprintf(bout, sizeof(bout), " ");
529 write_or_die(fdo, bout, strlen(bout));
531 } else if (bytes_done < len) {
532 slprintf(bout, sizeof(bout), " ");
533 write_or_die(fdo, bout, strlen(bout));
536 if (bytes_done % 10 != 0) {
537 slprintf(bout, sizeof(bout), "\n");
538 write_or_die(fdo, bout, strlen(bout));
541 slprintf(bout, sizeof(bout), "}\n\n");
542 write_or_die(fdo, bout, strlen(bout));
545 static void read_pcap(struct ctx *ctx)
547 uint8_t *out;
548 int ret, fd, fdo = 0;
549 unsigned long trunced = 0;
550 size_t out_len;
551 pcap_pkthdr_t phdr;
552 struct sock_fprog bpf_ops;
553 struct frame_map fm;
554 struct timeval start, end, diff;
555 bool is_out_pcap = ctx->device_out && strstr(ctx->device_out, ".pcap");
556 const struct pcap_file_ops *pcap_out_ops = pcap_ops[PCAP_OPS_RW];
558 bug_on(!__pcap_io);
560 if (!strncmp("-", ctx->device_in, strlen("-"))) {
561 fd = dup_or_die(fileno(stdin));
562 close(fileno(stdin));
563 if (ctx->pcap == PCAP_OPS_MM)
564 ctx->pcap = PCAP_OPS_SG;
565 } else {
566 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
569 if (__pcap_io->init_once_pcap)
570 __pcap_io->init_once_pcap(false);
572 ret = __pcap_io->pull_fhdr_pcap(fd, &ctx->magic, &ctx->link_type);
573 if (ret)
574 panic("Error reading pcap header!\n");
576 if (__pcap_io->prepare_access_pcap) {
577 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_RD, ctx->jumbo);
578 if (ret)
579 panic("Error prepare reading pcap!\n");
582 fmemset(&fm, 0, sizeof(fm));
584 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
585 if (ctx->dump_bpf)
586 bpf_dump_all(&bpf_ops);
588 dissector_init_all(ctx->print_mode);
590 out_len = round_up(1024 * 1024, RUNTIME_PAGE_SIZE);
591 out = xmalloc_aligned(out_len, CO_CACHE_LINE_SIZE);
593 if (ctx->device_out) {
594 if (!strncmp("-", ctx->device_out, strlen("-"))) {
595 fdo = dup_or_die(fileno(stdout));
596 close(fileno(stdout));
597 } else {
598 fdo = open_or_die_m(ctx->device_out, O_RDWR | O_CREAT |
599 O_TRUNC | O_LARGEFILE, DEFFILEMODE);
603 if (is_out_pcap) {
604 ret = pcap_out_ops->push_fhdr_pcap(fdo, ctx->magic,
605 ctx->link_type);
606 if (ret)
607 panic("Error writing pcap header!\n");
610 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
612 printf("Running! Hang up with ^C!\n\n");
613 fflush(stdout);
615 bug_on(gettimeofday(&start, NULL));
617 while (likely(sigint == 0)) {
618 do {
619 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic,
620 out, out_len);
621 if (unlikely(ret < 0))
622 goto out;
624 if (unlikely(pcap_get_length(&phdr, ctx->magic) == 0)) {
625 trunced++;
626 continue;
629 if (unlikely(pcap_get_length(&phdr, ctx->magic) > out_len)) {
630 pcap_set_length(&phdr, ctx->magic, out_len);
631 trunced++;
633 } while (ctx->filter &&
634 !bpf_run_filter(&bpf_ops, out,
635 pcap_get_length(&phdr, ctx->magic)));
637 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &fm.tp_h, &fm.s_ll);
639 ctx->tx_bytes += fm.tp_h.tp_len;
640 ctx->tx_packets++;
642 show_frame_hdr(out, fm.tp_h.tp_snaplen, ctx->link_type, &fm,
643 ctx->print_mode);
645 dissector_entry_point(out, fm.tp_h.tp_snaplen,
646 ctx->link_type, ctx->print_mode);
648 if (is_out_pcap) {
649 size_t pcap_len = pcap_get_length(&phdr, ctx->magic);
650 int wlen = pcap_out_ops->write_pcap(fdo, &phdr,
651 ctx->magic, out,
652 pcap_len);
653 if (unlikely(wlen != (int)pcap_get_total_length(&phdr, ctx->magic)))
654 panic("Error writing to pcap!\n");
655 } else if (ctx->device_out) {
656 translate_pcap_to_txf(fdo, out, fm.tp_h.tp_snaplen);
659 if (frame_count_max != 0) {
660 if (ctx->tx_packets >= frame_count_max) {
661 sigint = 1;
662 break;
667 out:
668 bug_on(gettimeofday(&end, NULL));
669 timersub(&end, &start, &diff);
671 bpf_release(&bpf_ops);
673 dissector_cleanup_all();
675 if (__pcap_io->prepare_close_pcap)
676 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
678 xfree(out);
680 fflush(stdout);
681 printf("\n");
682 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
683 printf("\r%12lu packets truncated in file\n", trunced);
684 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
685 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
687 if (!strncmp("-", ctx->device_in, strlen("-")))
688 dup2(fd, fileno(stdin));
689 close(fd);
691 if (ctx->device_out) {
692 if (!strncmp("-", ctx->device_out, strlen("-")))
693 dup2(fdo, fileno(stdout));
694 close(fdo);
698 static void finish_multi_pcap_file(struct ctx *ctx, int fd)
700 __pcap_io->fsync_pcap(fd);
702 if (__pcap_io->prepare_close_pcap)
703 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
705 close(fd);
707 fmemset(&itimer, 0, sizeof(itimer));
708 setitimer(ITIMER_REAL, &itimer, NULL);
711 static int next_multi_pcap_file(struct ctx *ctx, int fd)
713 int ret;
714 char fname[512];
716 __pcap_io->fsync_pcap(fd);
718 if (__pcap_io->prepare_close_pcap)
719 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
721 close(fd);
723 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
724 ctx->prefix ? : "dump-", time(NULL));
726 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
727 O_LARGEFILE, DEFFILEMODE);
729 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
730 if (ret)
731 panic("Error writing pcap header!\n");
733 if (__pcap_io->prepare_access_pcap) {
734 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, true);
735 if (ret)
736 panic("Error prepare writing pcap!\n");
739 return fd;
742 static int begin_multi_pcap_file(struct ctx *ctx)
744 int fd, ret;
745 char fname[256];
747 bug_on(!__pcap_io);
749 if (ctx->device_out[strlen(ctx->device_out) - 1] == '/')
750 ctx->device_out[strlen(ctx->device_out) - 1] = 0;
752 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
753 ctx->prefix ? : "dump-", time(NULL));
755 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
756 O_LARGEFILE, DEFFILEMODE);
758 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
759 if (ret)
760 panic("Error writing pcap header!\n");
762 if (__pcap_io->prepare_access_pcap) {
763 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, true);
764 if (ret)
765 panic("Error prepare writing pcap!\n");
768 if (ctx->dump_mode == DUMP_INTERVAL_TIME) {
769 interval = ctx->dump_interval;
771 set_itimer_interval_value(&itimer, interval, 0);
772 setitimer(ITIMER_REAL, &itimer, NULL);
773 } else {
774 interval = 0;
777 return fd;
780 static void finish_single_pcap_file(struct ctx *ctx, int fd)
782 __pcap_io->fsync_pcap(fd);
784 if (__pcap_io->prepare_close_pcap)
785 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
787 if (strncmp("-", ctx->device_out, strlen("-")))
788 close(fd);
789 else
790 dup2(fd, fileno(stdout));
793 static int begin_single_pcap_file(struct ctx *ctx)
795 int fd, ret;
797 bug_on(!__pcap_io);
799 if (!strncmp("-", ctx->device_out, strlen("-"))) {
800 fd = dup_or_die(fileno(stdout));
801 close(fileno(stdout));
802 if (ctx->pcap == PCAP_OPS_MM)
803 ctx->pcap = PCAP_OPS_SG;
804 } else {
805 fd = open_or_die_m(ctx->device_out,
806 O_RDWR | O_CREAT | O_TRUNC |
807 O_LARGEFILE, DEFFILEMODE);
810 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
811 if (ret)
812 panic("Error writing pcap header!\n");
814 if (__pcap_io->prepare_access_pcap) {
815 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, true);
816 if (ret)
817 panic("Error prepare writing pcap!\n");
820 return fd;
823 static void print_pcap_file_stats(int sock, struct ctx *ctx)
825 int ret;
826 struct tpacket_stats kstats;
827 socklen_t slen = sizeof(kstats);
829 fmemset(&kstats, 0, sizeof(kstats));
831 ret = getsockopt(sock, SOL_PACKET, PACKET_STATISTICS, &kstats, &slen);
832 if (unlikely(ret))
833 panic("Cannot get packet statistics!\n");
835 if (ctx->print_mode == PRINT_NONE) {
836 printf(".(+%u/-%u)", kstats.tp_packets - kstats.tp_drops,
837 kstats.tp_drops);
838 fflush(stdout);
842 static void update_pcap_next_dump(struct ctx *ctx, unsigned long snaplen, int *fd, int sock)
844 if (!dump_to_pcap(ctx))
845 return;
847 if (ctx->dump_mode == DUMP_INTERVAL_SIZE) {
848 interval += snaplen;
849 if (interval > ctx->dump_interval) {
850 next_dump = true;
851 interval = 0;
855 if (next_dump) {
856 *fd = next_multi_pcap_file(ctx, *fd);
857 next_dump = false;
859 if (ctx->verbose)
860 print_pcap_file_stats(sock, ctx);
864 #ifdef HAVE_TPACKET3
865 static void walk_t3_block(struct block_desc *pbd, struct ctx *ctx,
866 int sock, int *fd, unsigned long *frame_count)
868 int num_pkts = pbd->h1.num_pkts, i;
869 struct tpacket3_hdr *hdr;
870 struct sockaddr_ll *sll;
872 hdr = (void *) ((uint8_t *) pbd + pbd->h1.offset_to_first_pkt);
873 sll = (void *) ((uint8_t *) hdr + TPACKET_ALIGN(sizeof(*hdr)));
875 for (i = 0; i < num_pkts && likely(sigint == 0); ++i) {
876 uint8_t *packet = ((uint8_t *) hdr + hdr->tp_mac);
877 pcap_pkthdr_t phdr;
879 if (ctx->packet_type != -1)
880 if (ctx->packet_type != sll->sll_pkttype)
881 goto next;
883 (*frame_count)++;
885 if (dump_to_pcap(ctx)) {
886 int ret;
888 tpacket3_hdr_to_pcap_pkthdr(hdr, sll, &phdr, ctx->magic);
890 ret = __pcap_io->write_pcap(*fd, &phdr, ctx->magic, packet,
891 pcap_get_length(&phdr, ctx->magic));
892 if (unlikely(ret != (int) pcap_get_total_length(&phdr, ctx->magic)))
893 panic("Write error to pcap!\n");
896 __show_frame_hdr(packet, hdr->tp_snaplen, ctx->link_type, sll,
897 hdr, ctx->print_mode, true);
899 dissector_entry_point(packet, hdr->tp_snaplen, ctx->link_type,
900 ctx->print_mode);
901 next:
902 hdr = (void *) ((uint8_t *) hdr + hdr->tp_next_offset);
903 sll = (void *) ((uint8_t *) hdr + TPACKET_ALIGN(sizeof(*hdr)));
905 if (frame_count_max != 0) {
906 if (unlikely(*frame_count >= frame_count_max)) {
907 sigint = 1;
908 break;
912 update_pcap_next_dump(ctx, hdr->tp_snaplen, fd, sock);
915 #endif /* HAVE_TPACKET3 */
917 static void recv_only_or_dump(struct ctx *ctx)
919 short ifflags = 0;
920 int sock, ifindex, fd = 0, ret;
921 size_t size;
922 unsigned int it = 0;
923 struct ring rx_ring;
924 struct pollfd rx_poll;
925 struct sock_fprog bpf_ops;
926 struct timeval start, end, diff;
927 unsigned long frame_count = 0;
929 sock = pf_socket();
931 ifindex = device_ifindex(ctx->device_in);
933 size = ring_size(ctx->device_in, ctx->reserve_size);
935 enable_kernel_bpf_jit_compiler();
937 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
938 if (ctx->dump_bpf)
939 bpf_dump_all(&bpf_ops);
940 bpf_attach_to_sock(sock, &bpf_ops);
942 if (ctx->hwtimestamp) {
943 ret = set_sockopt_hwtimestamp(sock, ctx->device_in);
944 if (ret == 0 && ctx->verbose)
945 printf("HW timestamping enabled\n");
948 ring_rx_setup(&rx_ring, sock, size, ifindex, &rx_poll, is_defined(HAVE_TPACKET3), true,
949 ctx->verbose, ctx->fanout_group, ctx->fanout_type);
951 dissector_init_all(ctx->print_mode);
953 if (ctx->cpu >= 0 && ifindex > 0) {
954 int irq = device_irq_number(ctx->device_in);
955 device_set_irq_affinity(irq, ctx->cpu);
957 if (ctx->verbose)
958 printf("IRQ: %s:%d > CPU%d\n",
959 ctx->device_in, irq, ctx->cpu);
962 if (ctx->promiscuous)
963 ifflags = device_enter_promiscuous_mode(ctx->device_in);
965 if (dump_to_pcap(ctx) && __pcap_io->init_once_pcap)
966 __pcap_io->init_once_pcap(true);
968 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
970 if (dump_to_pcap(ctx)) {
971 struct stat stats;
973 ret = stat(ctx->device_out, &stats);
974 if (ret < 0)
975 ctx->dump_dir = 0;
976 else
977 ctx->dump_dir = S_ISDIR(stats.st_mode);
979 if (ctx->dump_dir)
980 fd = begin_multi_pcap_file(ctx);
981 else
982 fd = begin_single_pcap_file(ctx);
985 printf("Running! Hang up with ^C!\n\n");
986 fflush(stdout);
988 bug_on(gettimeofday(&start, NULL));
990 while (likely(sigint == 0)) {
991 #ifdef HAVE_TPACKET3
992 struct block_desc *pbd;
994 while (user_may_pull_from_rx_block((pbd = rx_ring.frames[it].iov_base))) {
995 walk_t3_block(pbd, ctx, sock, &fd, &frame_count);
997 kernel_may_pull_from_rx_block(pbd);
998 it = (it + 1) % rx_ring.layout3.tp_block_nr;
1000 if (unlikely(sigint == 1))
1001 break;
1003 #else
1004 while (user_may_pull_from_rx(rx_ring.frames[it].iov_base)) {
1005 struct frame_map *hdr = rx_ring.frames[it].iov_base;
1006 uint8_t *packet = ((uint8_t *) hdr) + hdr->tp_h.tp_mac;
1007 pcap_pkthdr_t phdr;
1009 if (ctx->packet_type != -1)
1010 if (ctx->packet_type != hdr->s_ll.sll_pkttype)
1011 goto next;
1013 frame_count++;
1015 if (unlikely(ring_frame_size(&rx_ring) < hdr->tp_h.tp_snaplen)) {
1016 /* XXX: silently ignore for now. We used to
1017 * report them with sock_rx_net_stats() */
1018 goto next;
1021 if (dump_to_pcap(ctx)) {
1022 tpacket_hdr_to_pcap_pkthdr(&hdr->tp_h, &hdr->s_ll, &phdr, ctx->magic);
1024 ret = __pcap_io->write_pcap(fd, &phdr, ctx->magic, packet,
1025 pcap_get_length(&phdr, ctx->magic));
1026 if (unlikely(ret != (int) pcap_get_total_length(&phdr, ctx->magic)))
1027 panic("Write error to pcap!\n");
1030 show_frame_hdr(packet, hdr->tp_h.tp_snaplen,
1031 ctx->link_type, hdr, ctx->print_mode);
1033 dissector_entry_point(packet, hdr->tp_h.tp_snaplen,
1034 ctx->link_type, ctx->print_mode);
1036 if (frame_count_max != 0) {
1037 if (unlikely(frame_count >= frame_count_max)) {
1038 sigint = 1;
1039 break;
1043 next:
1044 kernel_may_pull_from_rx(&hdr->tp_h);
1045 it = (it + 1) % rx_ring.layout.tp_frame_nr;
1047 if (unlikely(sigint == 1))
1048 break;
1050 update_pcap_next_dump(ctx, hdr->tp_h.tp_snaplen, &fd, sock);
1052 #endif /* HAVE_TPACKET3 */
1054 ret = poll(&rx_poll, 1, -1);
1055 if (unlikely(ret < 0)) {
1056 if (errno != EINTR)
1057 panic("Poll failed!\n");
1061 bug_on(gettimeofday(&end, NULL));
1062 timersub(&end, &start, &diff);
1064 if (!(ctx->dump_dir && ctx->print_mode == PRINT_NONE)) {
1065 sock_rx_net_stats(sock, frame_count);
1067 printf("\r%12lu sec, %lu usec in total\n",
1068 diff.tv_sec, diff.tv_usec);
1069 } else {
1070 printf("\n\n");
1071 fflush(stdout);
1074 bpf_release(&bpf_ops);
1075 dissector_cleanup_all();
1076 destroy_rx_ring(sock, &rx_ring);
1078 if (ctx->promiscuous)
1079 device_leave_promiscuous_mode(ctx->device_in, ifflags);
1081 if (ctx->rfraw)
1082 leave_rfmon_mac80211(ctx->device_in);
1084 if (dump_to_pcap(ctx)) {
1085 if (ctx->dump_dir)
1086 finish_multi_pcap_file(ctx, fd);
1087 else
1088 finish_single_pcap_file(ctx, fd);
1091 close(sock);
1094 static void init_ctx(struct ctx *ctx)
1096 memset(ctx, 0, sizeof(*ctx));
1098 ctx->uid = getuid();
1099 ctx->gid = getgid();
1101 ctx->cpu = -1;
1102 ctx->packet_type = -1;
1104 ctx->fanout_type = PACKET_FANOUT_ROLLOVER;
1106 ctx->magic = ORIGINAL_TCPDUMP_MAGIC;
1107 ctx->print_mode = PRINT_NORM;
1108 ctx->pcap = PCAP_OPS_SG;
1110 ctx->dump_mode = DUMP_INTERVAL_TIME;
1111 ctx->dump_interval = 60;
1113 ctx->promiscuous = true;
1114 ctx->randomize = false;
1115 ctx->hwtimestamp = true;
1118 static void destroy_ctx(struct ctx *ctx)
1120 free(ctx->device_in);
1121 free(ctx->device_out);
1122 free(ctx->device_trans);
1124 free(ctx->prefix);
1127 static void __noreturn help(void)
1129 printf("netsniff-ng %s, the packet sniffing beast\n", VERSION_STRING);
1130 puts("http://www.netsniff-ng.org\n\n"
1131 "Usage: netsniff-ng [options] [filter-expression]\n"
1132 "Options:\n"
1133 " -i|-d|--dev|--in <dev|pcap|-> Input source as netdev, pcap or pcap stdin\n"
1134 " -o|--out <dev|pcap|dir|cfg|-> Output sink as netdev, pcap, directory, trafgen, or stdout\n"
1135 " -C|--fanout-group <id> Join packet fanout group\n"
1136 " -K|--fanout-type <type> Apply fanout discipline: hash|lb|cpu|rnd|roll|qm\n"
1137 " -L|--fanout-opts <opts> Additional fanout options: defrag|roll\n"
1138 " -f|--filter <bpf-file|expr> Use BPF filter file from bpfc or tcpdump-like expression\n"
1139 " -t|--type <type> Filter for: host|broadcast|multicast|others|outgoing\n"
1140 " -F|--interval <size|time> Dump interval if -o is a dir: <num>KiB/MiB/GiB/s/sec/min/hrs\n"
1141 " -R|--rfraw Capture or inject raw 802.11 frames\n"
1142 " -n|--num <0|uint> Number of packets until exit (def: 0)\n"
1143 " -P|--prefix <name> Prefix for pcaps stored in directory\n"
1144 " -T|--magic <pcap-magic> Pcap magic number/pcap format to store, see -D\n"
1145 " -D|--dump-pcap-types Dump pcap types and magic numbers and quit\n"
1146 " -B|--dump-bpf Dump generated BPF assembly\n"
1147 " -r|--rand Randomize packet forwarding order (dev->dev)\n"
1148 " -M|--no-promisc No promiscuous mode for netdev\n"
1149 " -A|--no-sock-mem Don't tune core socket memory\n"
1150 " -N|--no-hwtimestamp Disable hardware time stamping\n"
1151 " -m|--mmap Mmap(2) pcap file I/O, e.g. for replaying pcaps\n"
1152 " -G|--sg Scatter/gather pcap file I/O\n"
1153 " -c|--clrw Use slower read(2)/write(2) I/O\n"
1154 " -S|--ring-size <size> Specify ring size to: <num>KiB/MiB/GiB\n"
1155 " -k|--kernel-pull <uint> Kernel pull from user interval in us (def: 10us)\n"
1156 " -J|--jumbo-support Support replay/fwd 64KB Super Jumbo Frames (def: 2048B)\n"
1157 " -b|--bind-cpu <cpu> Bind to specific CPU\n"
1158 " -u|--user <userid> Drop privileges and change to userid\n"
1159 " -g|--group <groupid> Drop privileges and change to groupid\n"
1160 " -H|--prio-high Make this high priority process\n"
1161 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
1162 " -s|--silent Do not print captured packets\n"
1163 " -q|--less Print less-verbose packet information\n"
1164 " -X|--hex Print packet data in hex format\n"
1165 " -l|--ascii Print human-readable packet data\n"
1166 " -U|--update Update GeoIP databases\n"
1167 " -V|--verbose Be more verbose\n"
1168 " -v|--version Show version and exit\n"
1169 " -h|--help Guess what?!\n\n"
1170 "Examples:\n"
1171 " netsniff-ng --in eth0 --out dump.pcap -s -T 0xa1b2c3d4 --b 0 tcp or udp\n"
1172 " netsniff-ng --in wlan0 --rfraw --out dump.pcap --silent --bind-cpu 0\n"
1173 " netsniff-ng --in dump.pcap --mmap --out eth0 -k1000 --silent --bind-cpu 0\n"
1174 " netsniff-ng --in dump.pcap --out dump.cfg --silent --bind-cpu 0\n"
1175 " netsniff-ng --in dump.pcap --out dump2.pcap --silent tcp\n"
1176 " netsniff-ng --in eth0 --out eth1 --silent --bind-cpu 0 -J --type host\n"
1177 " netsniff-ng --in eth1 --out /opt/probe/ -s -m --interval 100MiB -b 0\n"
1178 " netsniff-ng --in vlan0 --out dump.pcap -c -u `id -u bob` -g `id -g bob`\n"
1179 " netsniff-ng --in any --filter http.bpf --jumbo-support --ascii -V\n\n"
1180 "Note:\n"
1181 " For introducing bit errors, delays with random variation and more\n"
1182 " while replaying pcaps, make use of tc(8) with its disciplines (e.g. netem).\n");
1183 puts(copyright);
1184 die();
1187 static void __noreturn version(void)
1189 printf("netsniff-ng %s, Git id: %s\n", VERSION_LONG, GITVERSION);
1190 puts("the packet sniffing beast\n"
1191 "http://www.netsniff-ng.org\n");
1192 puts(copyright);
1193 die();
1196 int main(int argc, char **argv)
1198 char *ptr;
1199 int c, i, j, cpu_tmp, opt_index, ops_touched = 0, vals[4] = {0};
1200 bool prio_high = false, setsockmem = true;
1201 void (*main_loop)(struct ctx *ctx) = NULL;
1202 struct ctx ctx;
1204 init_ctx(&ctx);
1205 srand(time(NULL));
1207 while ((c = getopt_long(argc, argv, short_options, long_options,
1208 &opt_index)) != EOF) {
1209 switch (c) {
1210 case 'd':
1211 case 'i':
1212 ctx.device_in = xstrdup(optarg);
1213 break;
1214 case 'o':
1215 ctx.device_out = xstrdup(optarg);
1216 break;
1217 case 'P':
1218 ctx.prefix = xstrdup(optarg);
1219 break;
1220 case 'R':
1221 ctx.rfraw = 1;
1222 break;
1223 case 'r':
1224 ctx.randomize = true;
1225 break;
1226 case 'J':
1227 ctx.jumbo = true;
1228 break;
1229 case 'T':
1230 ctx.magic = (uint32_t) strtoul(optarg, NULL, 0);
1231 pcap_check_magic(ctx.magic);
1232 break;
1233 case 'f':
1234 ctx.filter = xstrdup(optarg);
1235 break;
1236 case 'M':
1237 ctx.promiscuous = false;
1238 break;
1239 case 'N':
1240 ctx.hwtimestamp = false;
1241 break;
1242 case 'A':
1243 setsockmem = false;
1244 break;
1245 case 'u':
1246 ctx.uid = strtoul(optarg, NULL, 0);
1247 ctx.enforce = true;
1248 break;
1249 case 'g':
1250 ctx.gid = strtoul(optarg, NULL, 0);
1251 ctx.enforce = true;
1252 break;
1253 case 'C':
1254 ctx.fanout_group = strtoul(optarg, NULL, 0);
1255 if (ctx.fanout_group == 0)
1256 panic("Non-zero fanout group id required!\n");
1257 break;
1258 case 'K':
1259 if (!strncmp(optarg, "hash", strlen("hash")))
1260 ctx.fanout_type = PACKET_FANOUT_HASH;
1261 else if (!strncmp(optarg, "lb", strlen("lb")))
1262 ctx.fanout_type = PACKET_FANOUT_LB;
1263 else if (!strncmp(optarg, "cpu", strlen("cpu")))
1264 ctx.fanout_type = PACKET_FANOUT_CPU;
1265 else if (!strncmp(optarg, "rnd", strlen("rnd")))
1266 ctx.fanout_type = PACKET_FANOUT_RND;
1267 else if (!strncmp(optarg, "roll", strlen("roll")))
1268 ctx.fanout_type = PACKET_FANOUT_ROLLOVER;
1269 else if (!strncmp(optarg, "qm", strlen("qm")))
1270 ctx.fanout_type = PACKET_FANOUT_QM;
1271 else
1272 panic("Unkown fanout type!\n");
1273 break;
1274 case 'L':
1275 if (!strncmp(optarg, "defrag", strlen("defrag")))
1276 ctx.fanout_type |= PACKET_FANOUT_FLAG_DEFRAG;
1277 else if (!strncmp(optarg, "roll", strlen("roll")))
1278 ctx.fanout_type |= PACKET_FANOUT_FLAG_ROLLOVER;
1279 else
1280 panic("Unkown fanout option!\n");
1281 break;
1282 case 't':
1283 if (!strncmp(optarg, "host", strlen("host")))
1284 ctx.packet_type = PACKET_HOST;
1285 else if (!strncmp(optarg, "broadcast", strlen("broadcast")))
1286 ctx.packet_type = PACKET_BROADCAST;
1287 else if (!strncmp(optarg, "multicast", strlen("multicast")))
1288 ctx.packet_type = PACKET_MULTICAST;
1289 else if (!strncmp(optarg, "others", strlen("others")))
1290 ctx.packet_type = PACKET_OTHERHOST;
1291 else if (!strncmp(optarg, "outgoing", strlen("outgoing")))
1292 ctx.packet_type = PACKET_OUTGOING;
1293 else
1294 ctx.packet_type = -1;
1295 break;
1296 case 'S':
1297 ptr = optarg;
1298 for (j = i = strlen(optarg); i > 0; --i) {
1299 if (!isdigit(optarg[j - i]))
1300 break;
1301 ptr++;
1304 if (!strncmp(ptr, "KiB", strlen("KiB")))
1305 ctx.reserve_size = 1 << 10;
1306 else if (!strncmp(ptr, "MiB", strlen("MiB")))
1307 ctx.reserve_size = 1 << 20;
1308 else if (!strncmp(ptr, "GiB", strlen("GiB")))
1309 ctx.reserve_size = 1 << 30;
1310 else
1311 panic("Syntax error in ring size param!\n");
1313 ctx.reserve_size *= strtoul(optarg, NULL, 0);
1314 break;
1315 case 'b':
1316 cpu_tmp = strtol(optarg, NULL, 0);
1318 cpu_affinity(cpu_tmp);
1319 if (ctx.cpu != -2)
1320 ctx.cpu = cpu_tmp;
1321 break;
1322 case 'H':
1323 prio_high = true;
1324 break;
1325 case 'c':
1326 ctx.pcap = PCAP_OPS_RW;
1327 ops_touched = 1;
1328 break;
1329 case 'm':
1330 ctx.pcap = PCAP_OPS_MM;
1331 ops_touched = 1;
1332 break;
1333 case 'G':
1334 ctx.pcap = PCAP_OPS_SG;
1335 ops_touched = 1;
1336 break;
1337 case 'Q':
1338 ctx.cpu = -2;
1339 break;
1340 case 's':
1341 ctx.print_mode = PRINT_NONE;
1342 break;
1343 case 'q':
1344 ctx.print_mode = PRINT_LESS;
1345 break;
1346 case 'X':
1347 ctx.print_mode =
1348 (ctx.print_mode == PRINT_ASCII) ?
1349 PRINT_HEX_ASCII : PRINT_HEX;
1350 break;
1351 case 'l':
1352 ctx.print_mode =
1353 (ctx.print_mode == PRINT_HEX) ?
1354 PRINT_HEX_ASCII : PRINT_ASCII;
1355 break;
1356 case 'k':
1357 ctx.kpull = strtoul(optarg, NULL, 0);
1358 break;
1359 case 'n':
1360 frame_count_max = strtoul(optarg, NULL, 0);
1361 break;
1362 case 'F':
1363 ptr = optarg;
1364 for (j = i = strlen(optarg); i > 0; --i) {
1365 if (!isdigit(optarg[j - i]))
1366 break;
1367 ptr++;
1370 if (!strncmp(ptr, "KiB", strlen("KiB"))) {
1371 ctx.dump_interval = 1 << 10;
1372 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1373 } else if (!strncmp(ptr, "MiB", strlen("MiB"))) {
1374 ctx.dump_interval = 1 << 20;
1375 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1376 } else if (!strncmp(ptr, "GiB", strlen("GiB"))) {
1377 ctx.dump_interval = 1 << 30;
1378 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1379 } else if (!strncmp(ptr, "sec", strlen("sec"))) {
1380 ctx.dump_interval = 1;
1381 ctx.dump_mode = DUMP_INTERVAL_TIME;
1382 } else if (!strncmp(ptr, "min", strlen("min"))) {
1383 ctx.dump_interval = 60;
1384 ctx.dump_mode = DUMP_INTERVAL_TIME;
1385 } else if (!strncmp(ptr, "hrs", strlen("hrs"))) {
1386 ctx.dump_interval = 60 * 60;
1387 ctx.dump_mode = DUMP_INTERVAL_TIME;
1388 } else if (!strncmp(ptr, "s", strlen("s"))) {
1389 ctx.dump_interval = 1;
1390 ctx.dump_mode = DUMP_INTERVAL_TIME;
1391 } else {
1392 panic("Syntax error in time/size param!\n");
1395 ctx.dump_interval *= strtoul(optarg, NULL, 0);
1396 break;
1397 case 'V':
1398 ctx.verbose = true;
1399 break;
1400 case 'B':
1401 ctx.dump_bpf = true;
1402 break;
1403 case 'D':
1404 pcap_dump_type_features();
1405 die();
1406 break;
1407 case 'U':
1408 update_geoip();
1409 die();
1410 break;
1411 case 'v':
1412 version();
1413 break;
1414 case 'h':
1415 help();
1416 break;
1417 case '?':
1418 switch (optopt) {
1419 case 'd':
1420 case 'i':
1421 case 'o':
1422 case 'f':
1423 case 't':
1424 case 'P':
1425 case 'F':
1426 case 'n':
1427 case 'S':
1428 case 'b':
1429 case 'k':
1430 case 'T':
1431 case 'u':
1432 case 'g':
1433 case 'e':
1434 panic("Option -%c requires an argument!\n",
1435 optopt);
1436 default:
1437 if (isprint(optopt))
1438 printf("Unknown option character `0x%X\'!\n", optopt);
1439 die();
1441 default:
1442 break;
1446 if (!ctx.filter && optind != argc) {
1447 int ret;
1448 off_t offset = 0;
1450 for (i = optind; i < argc; ++i) {
1451 size_t alen = strlen(argv[i]) + 2;
1452 size_t flen = ctx.filter ? strlen(ctx.filter) : 0;
1454 ctx.filter = xrealloc(ctx.filter, flen + alen);
1455 ret = slprintf(ctx.filter + offset, strlen(argv[i]) + 2, "%s ", argv[i]);
1456 if (ret < 0)
1457 panic("Cannot concatenate filter string!\n");
1458 else
1459 offset += ret;
1463 if (!ctx.device_in)
1464 ctx.device_in = xstrdup("any");
1466 register_signal(SIGINT, signal_handler);
1467 register_signal(SIGQUIT, signal_handler);
1468 register_signal(SIGTERM, signal_handler);
1469 register_signal(SIGHUP, signal_handler);
1471 tprintf_init();
1473 if (prio_high) {
1474 set_proc_prio(-20);
1475 set_sched_status(SCHED_FIFO, sched_get_priority_max(SCHED_FIFO));
1478 if (device_mtu(ctx.device_in) || !strncmp("any", ctx.device_in, strlen(ctx.device_in))) {
1479 if (ctx.rfraw)
1480 setup_rfmon_mac80211_dev(&ctx, &ctx.device_in);
1482 ctx.link_type = pcap_devtype_to_linktype(ctx.device_in);
1484 if (!ctx.device_out) {
1485 ctx.dump = 0;
1486 main_loop = recv_only_or_dump;
1487 } else if (device_mtu(ctx.device_out)) {
1488 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1489 main_loop = receive_to_xmit;
1490 } else {
1491 ctx.dump = 1;
1492 register_signal_f(SIGALRM, timer_next_dump, SA_SIGINFO);
1493 main_loop = recv_only_or_dump;
1494 if (!ops_touched)
1495 ctx.pcap = PCAP_OPS_SG;
1497 } else {
1498 if (ctx.device_out && device_mtu(ctx.device_out)) {
1499 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1500 main_loop = pcap_to_xmit;
1501 if (!ops_touched)
1502 ctx.pcap = PCAP_OPS_MM;
1503 } else {
1504 main_loop = read_pcap;
1505 if (!ops_touched)
1506 ctx.pcap = PCAP_OPS_SG;
1510 bug_on(!main_loop);
1512 init_geoip(0);
1513 if (setsockmem)
1514 set_system_socket_memory(vals, array_size(vals));
1515 if (!ctx.enforce)
1516 xlockme();
1518 if (ctx.verbose)
1519 printf("pcap file I/O method: %s\n", pcap_ops_group_to_str[ctx.pcap]);
1521 main_loop(&ctx);
1523 if (!ctx.enforce)
1524 xunlockme();
1525 if (setsockmem)
1526 reset_system_socket_memory(vals, array_size(vals));
1527 destroy_geoip();
1529 device_restore_irq_affinity_list();
1530 tprintf_cleanup();
1532 destroy_ctx(&ctx);
1533 return 0;