netsniff-ng: add packet fanout support
[netsniff-ng.git] / netsniff-ng.c
blob2eafe31ba5cd78997e71170ea51d3d13c80b9d52
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 pcap_to_xmit(struct ctx *ctx)
189 uint8_t *out = NULL;
190 int ifindex, fd = 0, ret;
191 size_t size;
192 unsigned int it = 0;
193 unsigned long trunced = 0;
194 struct ring tx_ring;
195 struct frame_map *hdr;
196 struct sock_fprog bpf_ops;
197 struct timeval start, end, diff;
198 pcap_pkthdr_t phdr;
200 if (!device_up_and_running(ctx->device_out) && !ctx->rfraw)
201 panic("Device not up and running!\n");
203 bug_on(!__pcap_io);
205 tx_sock = pf_socket();
207 if (!strncmp("-", ctx->device_in, strlen("-"))) {
208 fd = dup_or_die(fileno(stdin));
209 close(fileno(stdin));
210 if (ctx->pcap == PCAP_OPS_MM)
211 ctx->pcap = PCAP_OPS_SG;
212 } else {
213 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
216 if (__pcap_io->init_once_pcap)
217 __pcap_io->init_once_pcap();
219 ret = __pcap_io->pull_fhdr_pcap(fd, &ctx->magic, &ctx->link_type);
220 if (ret)
221 panic("Error reading pcap header!\n");
223 if (__pcap_io->prepare_access_pcap) {
224 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_RD, ctx->jumbo);
225 if (ret)
226 panic("Error prepare reading pcap!\n");
229 if (ctx->rfraw) {
230 ctx->device_trans = xstrdup(ctx->device_out);
231 xfree(ctx->device_out);
233 enter_rfmon_mac80211(ctx->device_trans, &ctx->device_out);
234 if (ctx->link_type != LINKTYPE_IEEE802_11 &&
235 ctx->link_type != LINKTYPE_IEEE802_11_RADIOTAP)
236 panic("Wrong linktype of pcap!\n");
239 ifindex = device_ifindex(ctx->device_out);
240 size = ring_size(ctx->device_out, ctx->reserve_size);
242 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
243 if (ctx->dump_bpf)
244 bpf_dump_all(&bpf_ops);
246 ring_tx_setup(&tx_ring, tx_sock, size, ifindex, ctx->jumbo, ctx->verbose);
248 dissector_init_all(ctx->print_mode);
250 if (ctx->cpu >= 0 && ifindex > 0) {
251 int irq = device_irq_number(ctx->device_out);
252 device_set_irq_affinity(irq, ctx->cpu);
254 if (ctx->verbose)
255 printf("IRQ: %s:%d > CPU%d\n",
256 ctx->device_out, irq, ctx->cpu);
259 if (ctx->kpull)
260 interval = ctx->kpull;
262 set_itimer_interval_value(&itimer, 0, interval);
263 setitimer(ITIMER_REAL, &itimer, NULL);
265 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
267 printf("Running! Hang up with ^C!\n\n");
268 fflush(stdout);
270 bug_on(gettimeofday(&start, NULL));
272 while (likely(sigint == 0)) {
273 while (user_may_pull_from_tx(tx_ring.frames[it].iov_base)) {
274 hdr = tx_ring.frames[it].iov_base;
275 out = ((uint8_t *) hdr) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
277 do {
278 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic, out,
279 ring_frame_size(&tx_ring));
280 if (unlikely(ret <= 0))
281 goto out;
283 if (ring_frame_size(&tx_ring) <
284 pcap_get_length(&phdr, ctx->magic)) {
285 pcap_set_length(&phdr, ctx->magic,
286 ring_frame_size(&tx_ring));
287 trunced++;
289 } while (ctx->filter &&
290 !bpf_run_filter(&bpf_ops, out,
291 pcap_get_length(&phdr, ctx->magic)));
293 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &hdr->tp_h, NULL);
295 ctx->tx_bytes += hdr->tp_h.tp_len;;
296 ctx->tx_packets++;
298 show_frame_hdr(out, hdr->tp_h.tp_snaplen,
299 ctx->link_type, hdr, ctx->print_mode);
301 dissector_entry_point(out, hdr->tp_h.tp_snaplen,
302 ctx->link_type, ctx->print_mode);
304 kernel_may_pull_from_tx(&hdr->tp_h);
306 it++;
307 if (it >= tx_ring.layout.tp_frame_nr)
308 it = 0;
310 if (unlikely(sigint == 1))
311 break;
313 if (frame_count_max != 0) {
314 if (ctx->tx_packets >= frame_count_max) {
315 sigint = 1;
316 break;
322 out:
323 bug_on(gettimeofday(&end, NULL));
324 timersub(&end, &start, &diff);
326 timer_purge();
328 bpf_release(&bpf_ops);
330 dissector_cleanup_all();
331 destroy_tx_ring(tx_sock, &tx_ring);
333 if (ctx->rfraw)
334 leave_rfmon_mac80211(ctx->device_out);
336 if (__pcap_io->prepare_close_pcap)
337 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
339 if (!strncmp("-", ctx->device_in, strlen("-")))
340 dup2(fd, fileno(stdin));
341 close(fd);
343 close(tx_sock);
345 fflush(stdout);
346 printf("\n");
347 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
348 printf("\r%12lu packets truncated in file\n", trunced);
349 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
350 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
353 static void receive_to_xmit(struct ctx *ctx)
355 short ifflags = 0;
356 uint8_t *in, *out;
357 int rx_sock, ifindex_in, ifindex_out, ret;
358 size_t size_in, size_out;
359 unsigned int it_in = 0, it_out = 0;
360 unsigned long frame_count = 0;
361 struct frame_map *hdr_in, *hdr_out;
362 struct ring tx_ring, rx_ring;
363 struct pollfd rx_poll;
364 struct sock_fprog bpf_ops;
366 if (!strncmp(ctx->device_in, ctx->device_out, IFNAMSIZ))
367 panic("Ingress/egress devices must be different!\n");
368 if (!device_up_and_running(ctx->device_out))
369 panic("Egress device not up and running!\n");
371 rx_sock = pf_socket();
372 tx_sock = pf_socket();
374 ifindex_in = device_ifindex(ctx->device_in);
375 ifindex_out = device_ifindex(ctx->device_out);
377 size_in = ring_size(ctx->device_in, ctx->reserve_size);
378 size_out = ring_size(ctx->device_out, ctx->reserve_size);
380 enable_kernel_bpf_jit_compiler();
382 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
383 if (ctx->dump_bpf)
384 bpf_dump_all(&bpf_ops);
385 bpf_attach_to_sock(rx_sock, &bpf_ops);
387 ring_rx_setup(&rx_ring, rx_sock, size_in, ifindex_in, &rx_poll, false, ctx->jumbo,
388 ctx->verbose, ctx->fanout_group, ctx->fanout_type);
389 ring_tx_setup(&tx_ring, tx_sock, size_out, ifindex_out, ctx->jumbo, ctx->verbose);
391 dissector_init_all(ctx->print_mode);
393 if (ctx->promiscuous)
394 ifflags = device_enter_promiscuous_mode(ctx->device_in);
396 if (ctx->kpull)
397 interval = ctx->kpull;
399 set_itimer_interval_value(&itimer, 0, interval);
400 setitimer(ITIMER_REAL, &itimer, NULL);
402 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
404 printf("Running! Hang up with ^C!\n\n");
405 fflush(stdout);
407 while (likely(sigint == 0)) {
408 while (user_may_pull_from_rx(rx_ring.frames[it_in].iov_base)) {
409 hdr_in = rx_ring.frames[it_in].iov_base;
410 in = ((uint8_t *) hdr_in) + hdr_in->tp_h.tp_mac;
412 frame_count++;
414 if (ctx->packet_type != -1)
415 if (ctx->packet_type != hdr_in->s_ll.sll_pkttype)
416 goto next;
418 hdr_out = tx_ring.frames[it_out].iov_base;
419 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
421 while (!user_may_pull_from_tx(tx_ring.frames[it_out].iov_base) &&
422 likely(!sigint)) {
423 if (ctx->randomize)
424 next_rnd_slot(&it_out, &tx_ring);
425 else {
426 it_out++;
427 if (it_out >= tx_ring.layout.tp_frame_nr)
428 it_out = 0;
431 hdr_out = tx_ring.frames[it_out].iov_base;
432 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
435 tpacket_hdr_clone(&hdr_out->tp_h, &hdr_in->tp_h);
436 fmemcpy(out, in, hdr_in->tp_h.tp_len);
438 kernel_may_pull_from_tx(&hdr_out->tp_h);
439 if (ctx->randomize)
440 next_rnd_slot(&it_out, &tx_ring);
441 else {
442 it_out++;
443 if (it_out >= tx_ring.layout.tp_frame_nr)
444 it_out = 0;
447 show_frame_hdr(in, hdr_in->tp_h.tp_snaplen,
448 ctx->link_type, hdr_in, ctx->print_mode);
450 dissector_entry_point(in, hdr_in->tp_h.tp_snaplen,
451 ctx->link_type, ctx->print_mode);
453 if (frame_count_max != 0) {
454 if (frame_count >= frame_count_max) {
455 sigint = 1;
456 break;
460 next:
461 kernel_may_pull_from_rx(&hdr_in->tp_h);
463 it_in++;
464 if (it_in >= rx_ring.layout.tp_frame_nr)
465 it_in = 0;
467 if (unlikely(sigint == 1))
468 goto out;
471 ret = poll(&rx_poll, 1, -1);
472 if (unlikely(ret < 0)) {
473 if (errno != EINTR)
474 panic("Poll failed!\n");
478 out:
479 timer_purge();
481 sock_rx_net_stats(rx_sock, 0);
483 bpf_release(&bpf_ops);
485 dissector_cleanup_all();
487 destroy_tx_ring(tx_sock, &tx_ring);
488 destroy_rx_ring(rx_sock, &rx_ring);
490 if (ctx->promiscuous)
491 device_leave_promiscuous_mode(ctx->device_in, ifflags);
493 close(tx_sock);
494 close(rx_sock);
497 static void translate_pcap_to_txf(int fdo, uint8_t *out, size_t len)
499 size_t bytes_done = 0;
500 char bout[80];
502 slprintf(bout, sizeof(bout), "{\n ");
503 write_or_die(fdo, bout, strlen(bout));
505 while (bytes_done < len) {
506 slprintf(bout, sizeof(bout), "0x%02x,", out[bytes_done]);
507 write_or_die(fdo, bout, strlen(bout));
509 bytes_done++;
511 if (bytes_done % 10 == 0) {
512 slprintf(bout, sizeof(bout), "\n");
513 write_or_die(fdo, bout, strlen(bout));
515 if (bytes_done < len) {
516 slprintf(bout, sizeof(bout), " ");
517 write_or_die(fdo, bout, strlen(bout));
519 } else if (bytes_done < len) {
520 slprintf(bout, sizeof(bout), " ");
521 write_or_die(fdo, bout, strlen(bout));
524 if (bytes_done % 10 != 0) {
525 slprintf(bout, sizeof(bout), "\n");
526 write_or_die(fdo, bout, strlen(bout));
529 slprintf(bout, sizeof(bout), "}\n\n");
530 write_or_die(fdo, bout, strlen(bout));
533 static void read_pcap(struct ctx *ctx)
535 uint8_t *out;
536 int ret, fd, fdo = 0;
537 unsigned long trunced = 0;
538 size_t out_len;
539 pcap_pkthdr_t phdr;
540 struct sock_fprog bpf_ops;
541 struct frame_map fm;
542 struct timeval start, end, diff;
543 bool is_out_pcap = ctx->device_out && strstr(ctx->device_out, ".pcap");
544 const struct pcap_file_ops *pcap_out_ops = pcap_ops[PCAP_OPS_RW];
546 bug_on(!__pcap_io);
548 if (!strncmp("-", ctx->device_in, strlen("-"))) {
549 fd = dup_or_die(fileno(stdin));
550 close(fileno(stdin));
551 if (ctx->pcap == PCAP_OPS_MM)
552 ctx->pcap = PCAP_OPS_SG;
553 } else {
554 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
557 if (__pcap_io->init_once_pcap)
558 __pcap_io->init_once_pcap();
560 ret = __pcap_io->pull_fhdr_pcap(fd, &ctx->magic, &ctx->link_type);
561 if (ret)
562 panic("Error reading pcap header!\n");
564 if (__pcap_io->prepare_access_pcap) {
565 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_RD, ctx->jumbo);
566 if (ret)
567 panic("Error prepare reading pcap!\n");
570 fmemset(&fm, 0, sizeof(fm));
572 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
573 if (ctx->dump_bpf)
574 bpf_dump_all(&bpf_ops);
576 dissector_init_all(ctx->print_mode);
578 out_len = round_up(1024 * 1024, RUNTIME_PAGE_SIZE);
579 out = xmalloc_aligned(out_len, CO_CACHE_LINE_SIZE);
581 if (ctx->device_out) {
582 if (!strncmp("-", ctx->device_out, strlen("-"))) {
583 fdo = dup_or_die(fileno(stdout));
584 close(fileno(stdout));
585 } else {
586 fdo = open_or_die_m(ctx->device_out, O_RDWR | O_CREAT |
587 O_TRUNC | O_LARGEFILE, DEFFILEMODE);
591 if (is_out_pcap) {
592 ret = pcap_out_ops->push_fhdr_pcap(fdo, ctx->magic,
593 ctx->link_type);
594 if (ret)
595 panic("Error writing pcap header!\n");
598 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
600 printf("Running! Hang up with ^C!\n\n");
601 fflush(stdout);
603 bug_on(gettimeofday(&start, NULL));
605 while (likely(sigint == 0)) {
606 do {
607 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic,
608 out, out_len);
609 if (unlikely(ret < 0))
610 goto out;
612 if (unlikely(pcap_get_length(&phdr, ctx->magic) == 0)) {
613 trunced++;
614 continue;
617 if (unlikely(pcap_get_length(&phdr, ctx->magic) > out_len)) {
618 pcap_set_length(&phdr, ctx->magic, out_len);
619 trunced++;
621 } while (ctx->filter &&
622 !bpf_run_filter(&bpf_ops, out,
623 pcap_get_length(&phdr, ctx->magic)));
625 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &fm.tp_h, &fm.s_ll);
627 ctx->tx_bytes += fm.tp_h.tp_len;
628 ctx->tx_packets++;
630 show_frame_hdr(out, fm.tp_h.tp_snaplen, ctx->link_type, &fm,
631 ctx->print_mode);
633 dissector_entry_point(out, fm.tp_h.tp_snaplen,
634 ctx->link_type, ctx->print_mode);
636 if (is_out_pcap) {
637 size_t pcap_len = pcap_get_length(&phdr, ctx->magic);
638 int wlen = pcap_out_ops->write_pcap(fdo, &phdr,
639 ctx->magic, out,
640 pcap_len);
641 if (unlikely(wlen != (int)pcap_get_total_length(&phdr, ctx->magic)))
642 panic("Error writing to pcap!\n");
643 } else if (ctx->device_out) {
644 translate_pcap_to_txf(fdo, out, fm.tp_h.tp_snaplen);
647 if (frame_count_max != 0) {
648 if (ctx->tx_packets >= frame_count_max) {
649 sigint = 1;
650 break;
655 out:
656 bug_on(gettimeofday(&end, NULL));
657 timersub(&end, &start, &diff);
659 bpf_release(&bpf_ops);
661 dissector_cleanup_all();
663 if (__pcap_io->prepare_close_pcap)
664 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
666 xfree(out);
668 fflush(stdout);
669 printf("\n");
670 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
671 printf("\r%12lu packets truncated in file\n", trunced);
672 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
673 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
675 if (!strncmp("-", ctx->device_in, strlen("-")))
676 dup2(fd, fileno(stdin));
677 close(fd);
679 if (ctx->device_out) {
680 if (!strncmp("-", ctx->device_out, strlen("-")))
681 dup2(fdo, fileno(stdout));
682 close(fdo);
686 static void finish_multi_pcap_file(struct ctx *ctx, int fd)
688 __pcap_io->fsync_pcap(fd);
690 if (__pcap_io->prepare_close_pcap)
691 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
693 close(fd);
695 fmemset(&itimer, 0, sizeof(itimer));
696 setitimer(ITIMER_REAL, &itimer, NULL);
699 static int next_multi_pcap_file(struct ctx *ctx, int fd)
701 int ret;
702 char fname[512];
704 __pcap_io->fsync_pcap(fd);
706 if (__pcap_io->prepare_close_pcap)
707 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
709 close(fd);
711 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
712 ctx->prefix ? : "dump-", time(NULL));
714 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
715 O_LARGEFILE, DEFFILEMODE);
717 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
718 if (ret)
719 panic("Error writing pcap header!\n");
721 if (__pcap_io->prepare_access_pcap) {
722 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, true);
723 if (ret)
724 panic("Error prepare writing pcap!\n");
727 return fd;
730 static int begin_multi_pcap_file(struct ctx *ctx)
732 int fd, ret;
733 char fname[256];
735 bug_on(!__pcap_io);
737 if (ctx->device_out[strlen(ctx->device_out) - 1] == '/')
738 ctx->device_out[strlen(ctx->device_out) - 1] = 0;
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 if (ctx->dump_mode == DUMP_INTERVAL_TIME) {
757 interval = ctx->dump_interval;
759 set_itimer_interval_value(&itimer, interval, 0);
760 setitimer(ITIMER_REAL, &itimer, NULL);
761 } else {
762 interval = 0;
765 return fd;
768 static void finish_single_pcap_file(struct ctx *ctx, int fd)
770 __pcap_io->fsync_pcap(fd);
772 if (__pcap_io->prepare_close_pcap)
773 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
775 if (strncmp("-", ctx->device_out, strlen("-")))
776 close(fd);
777 else
778 dup2(fd, fileno(stdout));
781 static int begin_single_pcap_file(struct ctx *ctx)
783 int fd, ret;
785 bug_on(!__pcap_io);
787 if (!strncmp("-", ctx->device_out, strlen("-"))) {
788 fd = dup_or_die(fileno(stdout));
789 close(fileno(stdout));
790 if (ctx->pcap == PCAP_OPS_MM)
791 ctx->pcap = PCAP_OPS_SG;
792 } else {
793 fd = open_or_die_m(ctx->device_out,
794 O_RDWR | O_CREAT | O_TRUNC |
795 O_LARGEFILE, DEFFILEMODE);
798 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
799 if (ret)
800 panic("Error writing pcap header!\n");
802 if (__pcap_io->prepare_access_pcap) {
803 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, true);
804 if (ret)
805 panic("Error prepare writing pcap!\n");
808 return fd;
811 static void print_pcap_file_stats(int sock, struct ctx *ctx)
813 int ret;
814 struct tpacket_stats kstats;
815 socklen_t slen = sizeof(kstats);
817 fmemset(&kstats, 0, sizeof(kstats));
819 ret = getsockopt(sock, SOL_PACKET, PACKET_STATISTICS, &kstats, &slen);
820 if (unlikely(ret))
821 panic("Cannot get packet statistics!\n");
823 if (ctx->print_mode == PRINT_NONE) {
824 printf(".(+%u/-%u)", kstats.tp_packets - kstats.tp_drops,
825 kstats.tp_drops);
826 fflush(stdout);
830 static void update_pcap_next_dump(struct ctx *ctx, unsigned long snaplen, int *fd, int sock)
832 if (!dump_to_pcap(ctx))
833 return;
835 if (ctx->dump_mode == DUMP_INTERVAL_SIZE) {
836 interval += snaplen;
837 if (interval > ctx->dump_interval) {
838 next_dump = true;
839 interval = 0;
843 if (next_dump) {
844 *fd = next_multi_pcap_file(ctx, *fd);
845 next_dump = false;
847 if (ctx->verbose)
848 print_pcap_file_stats(sock, ctx);
852 #ifdef HAVE_TPACKET3
853 static void walk_t3_block(struct block_desc *pbd, struct ctx *ctx,
854 int sock, int *fd, unsigned long *frame_count)
856 int num_pkts = pbd->h1.num_pkts, i;
857 struct tpacket3_hdr *hdr;
858 struct sockaddr_ll *sll;
860 hdr = (void *) ((uint8_t *) pbd + pbd->h1.offset_to_first_pkt);
861 sll = (void *) ((uint8_t *) hdr + TPACKET_ALIGN(sizeof(*hdr)));
863 for (i = 0; i < num_pkts && likely(sigint == 0); ++i) {
864 uint8_t *packet = ((uint8_t *) hdr + hdr->tp_mac);
865 pcap_pkthdr_t phdr;
867 if (ctx->packet_type != -1)
868 if (ctx->packet_type != sll->sll_pkttype)
869 goto next;
871 (*frame_count)++;
873 if (dump_to_pcap(ctx)) {
874 int ret;
876 tpacket3_hdr_to_pcap_pkthdr(hdr, sll, &phdr, ctx->magic);
878 ret = __pcap_io->write_pcap(*fd, &phdr, ctx->magic, packet,
879 pcap_get_length(&phdr, ctx->magic));
880 if (unlikely(ret != (int) pcap_get_total_length(&phdr, ctx->magic)))
881 panic("Write error to pcap!\n");
884 __show_frame_hdr(packet, hdr->tp_snaplen, ctx->link_type, sll,
885 hdr, ctx->print_mode, true);
887 dissector_entry_point(packet, hdr->tp_snaplen, ctx->link_type,
888 ctx->print_mode);
889 next:
890 hdr = (void *) ((uint8_t *) hdr + hdr->tp_next_offset);
891 sll = (void *) ((uint8_t *) hdr + TPACKET_ALIGN(sizeof(*hdr)));
893 if (frame_count_max != 0) {
894 if (unlikely(*frame_count >= frame_count_max)) {
895 sigint = 1;
896 break;
900 update_pcap_next_dump(ctx, hdr->tp_snaplen, fd, sock);
903 #endif /* HAVE_TPACKET3 */
905 static void recv_only_or_dump(struct ctx *ctx)
907 short ifflags = 0;
908 int sock, ifindex, fd = 0, ret;
909 size_t size;
910 unsigned int it = 0;
911 struct ring rx_ring;
912 struct pollfd rx_poll;
913 struct sock_fprog bpf_ops;
914 struct timeval start, end, diff;
915 unsigned long frame_count = 0;
917 sock = pf_socket();
919 ifindex = device_ifindex(ctx->device_in);
921 size = ring_size(ctx->device_in, ctx->reserve_size);
923 enable_kernel_bpf_jit_compiler();
925 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
926 if (ctx->dump_bpf)
927 bpf_dump_all(&bpf_ops);
928 bpf_attach_to_sock(sock, &bpf_ops);
930 if (ctx->hwtimestamp) {
931 ret = set_sockopt_hwtimestamp(sock, ctx->device_in);
932 if (ret == 0 && ctx->verbose)
933 printf("HW timestamping enabled\n");
936 ring_rx_setup(&rx_ring, sock, size, ifindex, &rx_poll, is_defined(HAVE_TPACKET3), true,
937 ctx->verbose, ctx->fanout_group, ctx->fanout_type);
939 dissector_init_all(ctx->print_mode);
941 if (ctx->cpu >= 0 && ifindex > 0) {
942 int irq = device_irq_number(ctx->device_in);
943 device_set_irq_affinity(irq, ctx->cpu);
945 if (ctx->verbose)
946 printf("IRQ: %s:%d > CPU%d\n",
947 ctx->device_in, irq, ctx->cpu);
950 if (ctx->promiscuous)
951 ifflags = device_enter_promiscuous_mode(ctx->device_in);
953 if (dump_to_pcap(ctx) && __pcap_io->init_once_pcap)
954 __pcap_io->init_once_pcap();
956 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
958 if (dump_to_pcap(ctx)) {
959 struct stat stats;
961 ret = stat(ctx->device_out, &stats);
962 if (ret < 0)
963 ctx->dump_dir = 0;
964 else
965 ctx->dump_dir = S_ISDIR(stats.st_mode);
967 if (ctx->dump_dir)
968 fd = begin_multi_pcap_file(ctx);
969 else
970 fd = begin_single_pcap_file(ctx);
973 printf("Running! Hang up with ^C!\n\n");
974 fflush(stdout);
976 bug_on(gettimeofday(&start, NULL));
978 while (likely(sigint == 0)) {
979 #ifdef HAVE_TPACKET3
980 struct block_desc *pbd;
982 while (user_may_pull_from_rx_block((pbd = rx_ring.frames[it].iov_base))) {
983 walk_t3_block(pbd, ctx, sock, &fd, &frame_count);
985 kernel_may_pull_from_rx_block(pbd);
986 it = (it + 1) % rx_ring.layout3.tp_block_nr;
988 if (unlikely(sigint == 1))
989 break;
991 #else
992 while (user_may_pull_from_rx(rx_ring.frames[it].iov_base)) {
993 struct frame_map *hdr = rx_ring.frames[it].iov_base;
994 uint8_t *packet = ((uint8_t *) hdr) + hdr->tp_h.tp_mac;
995 pcap_pkthdr_t phdr;
997 if (ctx->packet_type != -1)
998 if (ctx->packet_type != hdr->s_ll.sll_pkttype)
999 goto next;
1001 frame_count++;
1003 if (unlikely(ring_frame_size(&rx_ring) < hdr->tp_h.tp_snaplen)) {
1004 /* XXX: silently ignore for now. We used to
1005 * report them with sock_rx_net_stats() */
1006 goto next;
1009 if (dump_to_pcap(ctx)) {
1010 tpacket_hdr_to_pcap_pkthdr(&hdr->tp_h, &hdr->s_ll, &phdr, ctx->magic);
1012 ret = __pcap_io->write_pcap(fd, &phdr, ctx->magic, packet,
1013 pcap_get_length(&phdr, ctx->magic));
1014 if (unlikely(ret != (int) pcap_get_total_length(&phdr, ctx->magic)))
1015 panic("Write error to pcap!\n");
1018 show_frame_hdr(packet, hdr->tp_h.tp_snaplen,
1019 ctx->link_type, hdr, ctx->print_mode);
1021 dissector_entry_point(packet, hdr->tp_h.tp_snaplen,
1022 ctx->link_type, ctx->print_mode);
1024 if (frame_count_max != 0) {
1025 if (unlikely(frame_count >= frame_count_max)) {
1026 sigint = 1;
1027 break;
1031 next:
1032 kernel_may_pull_from_rx(&hdr->tp_h);
1033 it = (it + 1) % rx_ring.layout.tp_frame_nr;
1035 if (unlikely(sigint == 1))
1036 break;
1038 update_pcap_next_dump(ctx, hdr->tp_h.tp_snaplen, &fd, sock);
1040 #endif /* HAVE_TPACKET3 */
1042 ret = poll(&rx_poll, 1, -1);
1043 if (unlikely(ret < 0)) {
1044 if (errno != EINTR)
1045 panic("Poll failed!\n");
1049 bug_on(gettimeofday(&end, NULL));
1050 timersub(&end, &start, &diff);
1052 if (!(ctx->dump_dir && ctx->print_mode == PRINT_NONE)) {
1053 sock_rx_net_stats(sock, frame_count);
1055 printf("\r%12lu sec, %lu usec in total\n",
1056 diff.tv_sec, diff.tv_usec);
1057 } else {
1058 printf("\n\n");
1059 fflush(stdout);
1062 bpf_release(&bpf_ops);
1063 dissector_cleanup_all();
1064 destroy_rx_ring(sock, &rx_ring);
1066 if (ctx->promiscuous)
1067 device_leave_promiscuous_mode(ctx->device_in, ifflags);
1069 if (ctx->rfraw)
1070 leave_rfmon_mac80211(ctx->device_in);
1072 if (dump_to_pcap(ctx)) {
1073 if (ctx->dump_dir)
1074 finish_multi_pcap_file(ctx, fd);
1075 else
1076 finish_single_pcap_file(ctx, fd);
1079 close(sock);
1082 static void init_ctx(struct ctx *ctx)
1084 memset(ctx, 0, sizeof(*ctx));
1086 ctx->uid = getuid();
1087 ctx->uid = getgid();
1089 ctx->cpu = -1;
1090 ctx->packet_type = -1;
1092 ctx->fanout_type = PACKET_FANOUT_ROLLOVER;
1094 ctx->magic = ORIGINAL_TCPDUMP_MAGIC;
1095 ctx->print_mode = PRINT_NORM;
1096 ctx->pcap = PCAP_OPS_SG;
1098 ctx->dump_mode = DUMP_INTERVAL_TIME;
1099 ctx->dump_interval = 60;
1101 ctx->promiscuous = true;
1102 ctx->randomize = false;
1103 ctx->hwtimestamp = true;
1106 static void destroy_ctx(struct ctx *ctx)
1108 free(ctx->device_in);
1109 free(ctx->device_out);
1110 free(ctx->device_trans);
1112 free(ctx->prefix);
1115 static void __noreturn help(void)
1117 printf("netsniff-ng %s, the packet sniffing beast\n", VERSION_STRING);
1118 puts("http://www.netsniff-ng.org\n\n"
1119 "Usage: netsniff-ng [options] [filter-expression]\n"
1120 "Options:\n"
1121 " -i|-d|--dev|--in <dev|pcap|-> Input source as netdev, pcap or pcap stdin\n"
1122 " -o|--out <dev|pcap|dir|cfg|-> Output sink as netdev, pcap, directory, trafgen, or stdout\n"
1123 " -C|--fanout-group <id> Join packet fanout group\n"
1124 " -K|--fanout-type <type> Apply fanout discipline: hash|lb|cpu|rnd|roll|qm\n"
1125 " -L|--fanout-opts <opts> Additional fanout options: defrag|roll\n"
1126 " -f|--filter <bpf-file|expr> Use BPF filter file from bpfc or tcpdump-like expression\n"
1127 " -t|--type <type> Filter for: host|broadcast|multicast|others|outgoing\n"
1128 " -F|--interval <size|time> Dump interval if -o is a dir: <num>KiB/MiB/GiB/s/sec/min/hrs\n"
1129 " -R|--rfraw Capture or inject raw 802.11 frames\n"
1130 " -n|--num <0|uint> Number of packets until exit (def: 0)\n"
1131 " -P|--prefix <name> Prefix for pcaps stored in directory\n"
1132 " -T|--magic <pcap-magic> Pcap magic number/pcap format to store, see -D\n"
1133 " -D|--dump-pcap-types Dump pcap types and magic numbers and quit\n"
1134 " -B|--dump-bpf Dump generated BPF assembly\n"
1135 " -r|--rand Randomize packet forwarding order (dev->dev)\n"
1136 " -M|--no-promisc No promiscuous mode for netdev\n"
1137 " -A|--no-sock-mem Don't tune core socket memory\n"
1138 " -N|--no-hwtimestamp Disable hardware time stamping\n"
1139 " -m|--mmap Mmap(2) pcap file I/O, e.g. for replaying pcaps\n"
1140 " -G|--sg Scatter/gather pcap file I/O\n"
1141 " -c|--clrw Use slower read(2)/write(2) I/O\n"
1142 " -S|--ring-size <size> Specify ring size to: <num>KiB/MiB/GiB\n"
1143 " -k|--kernel-pull <uint> Kernel pull from user interval in us (def: 10us)\n"
1144 " -J|--jumbo-support Support replay/fwd 64KB Super Jumbo Frames (def: 2048B)\n"
1145 " -b|--bind-cpu <cpu> Bind to specific CPU\n"
1146 " -u|--user <userid> Drop privileges and change to userid\n"
1147 " -g|--group <groupid> Drop privileges and change to groupid\n"
1148 " -H|--prio-high Make this high priority process\n"
1149 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
1150 " -s|--silent Do not print captured packets\n"
1151 " -q|--less Print less-verbose packet information\n"
1152 " -X|--hex Print packet data in hex format\n"
1153 " -l|--ascii Print human-readable packet data\n"
1154 " -U|--update Update GeoIP databases\n"
1155 " -V|--verbose Be more verbose\n"
1156 " -v|--version Show version and exit\n"
1157 " -h|--help Guess what?!\n\n"
1158 "Examples:\n"
1159 " netsniff-ng --in eth0 --out dump.pcap -s -T 0xa1b2c3d4 --b 0 tcp or udp\n"
1160 " netsniff-ng --in wlan0 --rfraw --out dump.pcap --silent --bind-cpu 0\n"
1161 " netsniff-ng --in dump.pcap --mmap --out eth0 -k1000 --silent --bind-cpu 0\n"
1162 " netsniff-ng --in dump.pcap --out dump.cfg --silent --bind-cpu 0\n"
1163 " netsniff-ng --in dump.pcap --out dump2.pcap --silent tcp\n"
1164 " netsniff-ng --in eth0 --out eth1 --silent --bind-cpu 0 -J --type host\n"
1165 " netsniff-ng --in eth1 --out /opt/probe/ -s -m --interval 100MiB -b 0\n"
1166 " netsniff-ng --in vlan0 --out dump.pcap -c -u `id -u bob` -g `id -g bob`\n"
1167 " netsniff-ng --in any --filter http.bpf --jumbo-support --ascii -V\n\n"
1168 "Note:\n"
1169 " For introducing bit errors, delays with random variation and more\n"
1170 " while replaying pcaps, make use of tc(8) with its disciplines (e.g. netem).\n");
1171 puts(copyright);
1172 die();
1175 static void __noreturn version(void)
1177 printf("netsniff-ng %s, Git id: %s\n", VERSION_LONG, GITVERSION);
1178 puts("the packet sniffing beast\n"
1179 "http://www.netsniff-ng.org\n");
1180 puts(copyright);
1181 die();
1184 int main(int argc, char **argv)
1186 char *ptr;
1187 int c, i, j, cpu_tmp, opt_index, ops_touched = 0, vals[4] = {0};
1188 bool prio_high = false, setsockmem = true;
1189 void (*main_loop)(struct ctx *ctx) = NULL;
1190 struct ctx ctx;
1192 init_ctx(&ctx);
1193 srand(time(NULL));
1195 while ((c = getopt_long(argc, argv, short_options, long_options,
1196 &opt_index)) != EOF) {
1197 switch (c) {
1198 case 'd':
1199 case 'i':
1200 ctx.device_in = xstrdup(optarg);
1201 break;
1202 case 'o':
1203 ctx.device_out = xstrdup(optarg);
1204 break;
1205 case 'P':
1206 ctx.prefix = xstrdup(optarg);
1207 break;
1208 case 'R':
1209 ctx.rfraw = 1;
1210 break;
1211 case 'r':
1212 ctx.randomize = true;
1213 break;
1214 case 'J':
1215 ctx.jumbo = true;
1216 break;
1217 case 'T':
1218 ctx.magic = (uint32_t) strtoul(optarg, NULL, 0);
1219 pcap_check_magic(ctx.magic);
1220 break;
1221 case 'f':
1222 ctx.filter = xstrdup(optarg);
1223 break;
1224 case 'M':
1225 ctx.promiscuous = false;
1226 break;
1227 case 'N':
1228 ctx.hwtimestamp = false;
1229 break;
1230 case 'A':
1231 setsockmem = false;
1232 break;
1233 case 'u':
1234 ctx.uid = strtoul(optarg, NULL, 0);
1235 ctx.enforce = true;
1236 break;
1237 case 'g':
1238 ctx.gid = strtoul(optarg, NULL, 0);
1239 ctx.enforce = true;
1240 break;
1241 case 'C':
1242 ctx.fanout_group = strtoul(optarg, NULL, 0);
1243 if (ctx.fanout_group == 0)
1244 panic("Non-zero fanout group id required!\n");
1245 break;
1246 case 'K':
1247 if (!strncmp(optarg, "hash", strlen("hash")))
1248 ctx.fanout_type = PACKET_FANOUT_HASH;
1249 else if (!strncmp(optarg, "lb", strlen("lb")))
1250 ctx.fanout_type = PACKET_FANOUT_LB;
1251 else if (!strncmp(optarg, "cpu", strlen("cpu")))
1252 ctx.fanout_type = PACKET_FANOUT_CPU;
1253 else if (!strncmp(optarg, "rnd", strlen("rnd")))
1254 ctx.fanout_type = PACKET_FANOUT_RND;
1255 else if (!strncmp(optarg, "roll", strlen("roll")))
1256 ctx.fanout_type = PACKET_FANOUT_ROLLOVER;
1257 else if (!strncmp(optarg, "qm", strlen("qm")))
1258 ctx.fanout_type = PACKET_FANOUT_QM;
1259 else
1260 panic("Unkown fanout type!\n");
1261 break;
1262 case 'L':
1263 if (!strncmp(optarg, "defrag", strlen("defrag")))
1264 ctx.fanout_type |= PACKET_FANOUT_FLAG_DEFRAG;
1265 else if (!strncmp(optarg, "roll", strlen("roll")))
1266 ctx.fanout_type |= PACKET_FANOUT_FLAG_ROLLOVER;
1267 else
1268 panic("Unkown fanout option!\n");
1269 break;
1270 case 't':
1271 if (!strncmp(optarg, "host", strlen("host")))
1272 ctx.packet_type = PACKET_HOST;
1273 else if (!strncmp(optarg, "broadcast", strlen("broadcast")))
1274 ctx.packet_type = PACKET_BROADCAST;
1275 else if (!strncmp(optarg, "multicast", strlen("multicast")))
1276 ctx.packet_type = PACKET_MULTICAST;
1277 else if (!strncmp(optarg, "others", strlen("others")))
1278 ctx.packet_type = PACKET_OTHERHOST;
1279 else if (!strncmp(optarg, "outgoing", strlen("outgoing")))
1280 ctx.packet_type = PACKET_OUTGOING;
1281 else
1282 ctx.packet_type = -1;
1283 break;
1284 case 'S':
1285 ptr = optarg;
1286 for (j = i = strlen(optarg); i > 0; --i) {
1287 if (!isdigit(optarg[j - i]))
1288 break;
1289 ptr++;
1292 if (!strncmp(ptr, "KiB", strlen("KiB")))
1293 ctx.reserve_size = 1 << 10;
1294 else if (!strncmp(ptr, "MiB", strlen("MiB")))
1295 ctx.reserve_size = 1 << 20;
1296 else if (!strncmp(ptr, "GiB", strlen("GiB")))
1297 ctx.reserve_size = 1 << 30;
1298 else
1299 panic("Syntax error in ring size param!\n");
1301 ctx.reserve_size *= strtoul(optarg, NULL, 0);
1302 break;
1303 case 'b':
1304 cpu_tmp = strtol(optarg, NULL, 0);
1306 cpu_affinity(cpu_tmp);
1307 if (ctx.cpu != -2)
1308 ctx.cpu = cpu_tmp;
1309 break;
1310 case 'H':
1311 prio_high = true;
1312 break;
1313 case 'c':
1314 ctx.pcap = PCAP_OPS_RW;
1315 ops_touched = 1;
1316 break;
1317 case 'm':
1318 ctx.pcap = PCAP_OPS_MM;
1319 ops_touched = 1;
1320 break;
1321 case 'G':
1322 ctx.pcap = PCAP_OPS_SG;
1323 ops_touched = 1;
1324 break;
1325 case 'Q':
1326 ctx.cpu = -2;
1327 break;
1328 case 's':
1329 ctx.print_mode = PRINT_NONE;
1330 break;
1331 case 'q':
1332 ctx.print_mode = PRINT_LESS;
1333 break;
1334 case 'X':
1335 ctx.print_mode =
1336 (ctx.print_mode == PRINT_ASCII) ?
1337 PRINT_HEX_ASCII : PRINT_HEX;
1338 break;
1339 case 'l':
1340 ctx.print_mode =
1341 (ctx.print_mode == PRINT_HEX) ?
1342 PRINT_HEX_ASCII : PRINT_ASCII;
1343 break;
1344 case 'k':
1345 ctx.kpull = strtoul(optarg, NULL, 0);
1346 break;
1347 case 'n':
1348 frame_count_max = strtoul(optarg, NULL, 0);
1349 break;
1350 case 'F':
1351 ptr = optarg;
1352 for (j = i = strlen(optarg); i > 0; --i) {
1353 if (!isdigit(optarg[j - i]))
1354 break;
1355 ptr++;
1358 if (!strncmp(ptr, "KiB", strlen("KiB"))) {
1359 ctx.dump_interval = 1 << 10;
1360 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1361 } else if (!strncmp(ptr, "MiB", strlen("MiB"))) {
1362 ctx.dump_interval = 1 << 20;
1363 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1364 } else if (!strncmp(ptr, "GiB", strlen("GiB"))) {
1365 ctx.dump_interval = 1 << 30;
1366 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1367 } else if (!strncmp(ptr, "sec", strlen("sec"))) {
1368 ctx.dump_interval = 1;
1369 ctx.dump_mode = DUMP_INTERVAL_TIME;
1370 } else if (!strncmp(ptr, "min", strlen("min"))) {
1371 ctx.dump_interval = 60;
1372 ctx.dump_mode = DUMP_INTERVAL_TIME;
1373 } else if (!strncmp(ptr, "hrs", strlen("hrs"))) {
1374 ctx.dump_interval = 60 * 60;
1375 ctx.dump_mode = DUMP_INTERVAL_TIME;
1376 } else if (!strncmp(ptr, "s", strlen("s"))) {
1377 ctx.dump_interval = 1;
1378 ctx.dump_mode = DUMP_INTERVAL_TIME;
1379 } else {
1380 panic("Syntax error in time/size param!\n");
1383 ctx.dump_interval *= strtoul(optarg, NULL, 0);
1384 break;
1385 case 'V':
1386 ctx.verbose = true;
1387 break;
1388 case 'B':
1389 ctx.dump_bpf = true;
1390 break;
1391 case 'D':
1392 pcap_dump_type_features();
1393 die();
1394 break;
1395 case 'U':
1396 update_geoip();
1397 die();
1398 break;
1399 case 'v':
1400 version();
1401 break;
1402 case 'h':
1403 help();
1404 break;
1405 case '?':
1406 switch (optopt) {
1407 case 'd':
1408 case 'i':
1409 case 'o':
1410 case 'f':
1411 case 't':
1412 case 'P':
1413 case 'F':
1414 case 'n':
1415 case 'S':
1416 case 'b':
1417 case 'k':
1418 case 'T':
1419 case 'u':
1420 case 'g':
1421 case 'e':
1422 panic("Option -%c requires an argument!\n",
1423 optopt);
1424 default:
1425 if (isprint(optopt))
1426 printf("Unknown option character `0x%X\'!\n", optopt);
1427 die();
1429 default:
1430 break;
1434 if (!ctx.filter && optind != argc) {
1435 int ret;
1436 off_t offset = 0;
1438 for (i = optind; i < argc; ++i) {
1439 size_t alen = strlen(argv[i]) + 2;
1440 size_t flen = ctx.filter ? strlen(ctx.filter) : 0;
1442 ctx.filter = xrealloc(ctx.filter, flen + alen);
1443 ret = slprintf(ctx.filter + offset, strlen(argv[i]) + 2, "%s ", argv[i]);
1444 if (ret < 0)
1445 panic("Cannot concatenate filter string!\n");
1446 else
1447 offset += ret;
1451 if (!ctx.device_in)
1452 ctx.device_in = xstrdup("any");
1454 register_signal(SIGINT, signal_handler);
1455 register_signal(SIGQUIT, signal_handler);
1456 register_signal(SIGTERM, signal_handler);
1457 register_signal(SIGHUP, signal_handler);
1459 tprintf_init();
1461 if (prio_high) {
1462 set_proc_prio(-20);
1463 set_sched_status(SCHED_FIFO, sched_get_priority_max(SCHED_FIFO));
1466 if (device_mtu(ctx.device_in) || !strncmp("any", ctx.device_in, strlen(ctx.device_in))) {
1467 if (ctx.rfraw) {
1468 ctx.device_trans = xstrdup(ctx.device_in);
1469 xfree(ctx.device_in);
1471 enter_rfmon_mac80211(ctx.device_trans, &ctx.device_in);
1474 ctx.link_type = pcap_devtype_to_linktype(ctx.device_in);
1476 if (!ctx.device_out) {
1477 ctx.dump = 0;
1478 main_loop = recv_only_or_dump;
1479 } else if (device_mtu(ctx.device_out)) {
1480 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1481 main_loop = receive_to_xmit;
1482 } else {
1483 ctx.dump = 1;
1484 register_signal_f(SIGALRM, timer_next_dump, SA_SIGINFO);
1485 main_loop = recv_only_or_dump;
1486 if (!ops_touched)
1487 ctx.pcap = PCAP_OPS_SG;
1489 } else {
1490 if (ctx.device_out && device_mtu(ctx.device_out)) {
1491 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1492 main_loop = pcap_to_xmit;
1493 if (!ops_touched)
1494 ctx.pcap = PCAP_OPS_MM;
1495 } else {
1496 main_loop = read_pcap;
1497 if (!ops_touched)
1498 ctx.pcap = PCAP_OPS_SG;
1502 bug_on(!main_loop);
1504 init_geoip(0);
1505 if (setsockmem)
1506 set_system_socket_memory(vals, array_size(vals));
1507 if (!ctx.enforce)
1508 xlockme();
1510 if (ctx.verbose)
1511 printf("pcap file I/O method: %s\n", pcap_ops_group_to_str[ctx.pcap]);
1513 main_loop(&ctx);
1515 if (!ctx.enforce)
1516 xunlockme();
1517 if (setsockmem)
1518 reset_system_socket_memory(vals, array_size(vals));
1519 destroy_geoip();
1521 device_restore_irq_affinity_list();
1522 tprintf_cleanup();
1524 destroy_ctx(&ctx);
1525 return 0;