netsniff-ng: Move variable definition
[netsniff-ng.git] / netsniff-ng.c
blob78b5666e918f621b126e22464a06a81b745f34b3
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2009-2013 Daniel Borkmann.
4 * Copyright 2010 Emmanuel Roullit.
5 * Subject to the GPL, version 2.
6 */
8 #define _GNU_SOURCE
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <signal.h>
12 #include <getopt.h>
13 #include <ctype.h>
14 #include <time.h>
15 #include <string.h>
16 #include <sys/socket.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <sys/time.h>
20 #include <sys/fsuid.h>
21 #include <unistd.h>
22 #include <stdbool.h>
23 #include <pthread.h>
24 #include <fcntl.h>
26 #include "ring_rx.h"
27 #include "ring_tx.h"
28 #include "mac80211.h"
29 #include "dev.h"
30 #include "built_in.h"
31 #include "pcap_io.h"
32 #include "privs.h"
33 #include "proc.h"
34 #include "bpf.h"
35 #include "ioops.h"
36 #include "die.h"
37 #include "irq.h"
38 #include "str.h"
39 #include "sig.h"
40 #include "config.h"
41 #include "sock.h"
42 #include "geoip.h"
43 #include "lockme.h"
44 #include "tprintf.h"
45 #include "timer.h"
46 #include "tstamping.h"
47 #include "dissector.h"
48 #include "xmalloc.h"
50 enum dump_mode {
51 DUMP_INTERVAL_TIME,
52 DUMP_INTERVAL_SIZE,
55 struct ctx {
56 char *device_in, *device_out, *device_trans, *filter, *prefix;
57 int cpu, rfraw, dump, print_mode, dump_dir, packet_type;
58 unsigned long kpull, dump_interval, tx_bytes, tx_packets;
59 size_t reserve_size;
60 bool randomize, promiscuous, enforce, jumbo, dump_bpf, hwtimestamp, verbose;
61 enum pcap_ops_groups pcap; enum dump_mode dump_mode;
62 uid_t uid; gid_t gid; uint32_t link_type, magic;
65 static volatile sig_atomic_t sigint = 0;
66 static volatile bool next_dump = false;
68 static const char *short_options = "d:i:o:rf:MNJt:S:k:n:b:HQmcsqXlvhF:RGAP:Vu:g:T:DBU";
69 static const struct option long_options[] = {
70 {"dev", required_argument, NULL, 'd'},
71 {"in", required_argument, NULL, 'i'},
72 {"out", required_argument, NULL, 'o'},
73 {"filter", required_argument, NULL, 'f'},
74 {"num", required_argument, NULL, 'n'},
75 {"type", required_argument, NULL, 't'},
76 {"interval", required_argument, NULL, 'F'},
77 {"ring-size", required_argument, NULL, 'S'},
78 {"kernel-pull", required_argument, NULL, 'k'},
79 {"bind-cpu", required_argument, NULL, 'b'},
80 {"prefix", required_argument, NULL, 'P'},
81 {"user", required_argument, NULL, 'u'},
82 {"group", required_argument, NULL, 'g'},
83 {"magic", required_argument, NULL, 'T'},
84 {"rand", no_argument, NULL, 'r'},
85 {"rfraw", no_argument, NULL, 'R'},
86 {"mmap", no_argument, NULL, 'm'},
87 {"sg", no_argument, NULL, 'G'},
88 {"clrw", no_argument, NULL, 'c'},
89 {"jumbo-support", no_argument, NULL, 'J'},
90 {"no-promisc", no_argument, NULL, 'M'},
91 {"no-hwtimestamp", no_argument, NULL, 'N'},
92 {"prio-high", no_argument, NULL, 'H'},
93 {"notouch-irq", no_argument, NULL, 'Q'},
94 {"dump-pcap-types", no_argument, NULL, 'D'},
95 {"dump-bpf", no_argument, NULL, 'B'},
96 {"silent", no_argument, NULL, 's'},
97 {"less", no_argument, NULL, 'q'},
98 {"hex", no_argument, NULL, 'X'},
99 {"ascii", no_argument, NULL, 'l'},
100 {"no-sock-mem", no_argument, NULL, 'A'},
101 {"update", no_argument, NULL, 'U'},
102 {"verbose", no_argument, NULL, 'V'},
103 {"version", no_argument, NULL, 'v'},
104 {"help", no_argument, NULL, 'h'},
105 {NULL, 0, NULL, 0}
108 static const char *copyright = "Please report bugs to <bugs@netsniff-ng.org>\n"
109 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
110 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
111 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
112 "Swiss federal institute of technology (ETH Zurich)\n"
113 "License: GNU GPL version 2.0\n"
114 "This is free software: you are free to change and redistribute it.\n"
115 "There is NO WARRANTY, to the extent permitted by law.\n";
117 static int tx_sock;
118 static struct itimerval itimer;
119 static unsigned long frame_count_max = 0, interval = TX_KERNEL_PULL_INT;
121 #define __pcap_io pcap_ops[ctx->pcap]
123 static void signal_handler(int number)
125 switch (number) {
126 case SIGINT:
127 case SIGQUIT:
128 case SIGTERM:
129 sigint = 1;
130 case SIGHUP:
131 default:
132 break;
136 static void timer_elapsed(int unused __maybe_unused)
138 int ret;
140 set_itimer_interval_value(&itimer, 0, interval);
142 ret = pull_and_flush_tx_ring(tx_sock);
143 if (unlikely(ret < 0)) {
144 /* We could hit EBADF if the socket has been closed before
145 * the timer was triggered.
147 if (errno != EBADF && errno != ENOBUFS)
148 panic("Flushing TX_RING failed: %s!\n", strerror(errno));
151 setitimer(ITIMER_REAL, &itimer, NULL);
154 static void timer_purge(void)
156 int ret;
158 ret = pull_and_flush_tx_ring_wait(tx_sock);
159 if (unlikely(ret < 0)) {
160 if (errno != EBADF && errno != ENOBUFS)
161 panic("Flushing TX_RING failed: %s!\n", strerror(errno));
164 set_itimer_interval_value(&itimer, 0, 0);
165 setitimer(ITIMER_REAL, &itimer, NULL);
168 static void timer_next_dump(int unused __maybe_unused)
170 set_itimer_interval_value(&itimer, interval, 0);
171 next_dump = true;
172 setitimer(ITIMER_REAL, &itimer, NULL);
175 static inline bool dump_to_pcap(struct ctx *ctx)
177 return ctx->dump;
180 static void pcap_to_xmit(struct ctx *ctx)
182 uint8_t *out = NULL;
183 int ifindex, fd = 0, ret;
184 size_t size;
185 unsigned int it = 0;
186 unsigned long trunced = 0;
187 struct ring tx_ring;
188 struct frame_map *hdr;
189 struct sock_fprog bpf_ops;
190 struct timeval start, end, diff;
191 pcap_pkthdr_t phdr;
193 if (!device_up_and_running(ctx->device_out) && !ctx->rfraw)
194 panic("Device not up and running!\n");
196 bug_on(!__pcap_io);
198 tx_sock = pf_socket();
200 if (!strncmp("-", ctx->device_in, strlen("-"))) {
201 fd = dup_or_die(fileno(stdin));
202 close(fileno(stdin));
203 if (ctx->pcap == PCAP_OPS_MM)
204 ctx->pcap = PCAP_OPS_SG;
205 } else {
206 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
209 if (__pcap_io->init_once_pcap)
210 __pcap_io->init_once_pcap();
212 ret = __pcap_io->pull_fhdr_pcap(fd, &ctx->magic, &ctx->link_type);
213 if (ret)
214 panic("Error reading pcap header!\n");
216 if (__pcap_io->prepare_access_pcap) {
217 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_RD, ctx->jumbo);
218 if (ret)
219 panic("Error prepare reading pcap!\n");
222 if (ctx->rfraw) {
223 ctx->device_trans = xstrdup(ctx->device_out);
224 xfree(ctx->device_out);
226 enter_rfmon_mac80211(ctx->device_trans, &ctx->device_out);
227 if (ctx->link_type != LINKTYPE_IEEE802_11)
228 panic("Wrong linktype of pcap!\n");
231 ifindex = device_ifindex(ctx->device_out);
232 size = ring_size(ctx->device_out, ctx->reserve_size);
234 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
235 if (ctx->dump_bpf)
236 bpf_dump_all(&bpf_ops);
238 ring_tx_setup(&tx_ring, tx_sock, size, ifindex, ctx->jumbo, ctx->verbose);
240 dissector_init_all(ctx->print_mode);
242 if (ctx->cpu >= 0 && ifindex > 0) {
243 int irq = device_irq_number(ctx->device_out);
244 device_set_irq_affinity(irq, ctx->cpu);
246 if (ctx->verbose)
247 printf("IRQ: %s:%d > CPU%d\n",
248 ctx->device_out, irq, ctx->cpu);
251 if (ctx->kpull)
252 interval = ctx->kpull;
254 set_itimer_interval_value(&itimer, 0, interval);
255 setitimer(ITIMER_REAL, &itimer, NULL);
257 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
259 printf("Running! Hang up with ^C!\n\n");
260 fflush(stdout);
262 bug_on(gettimeofday(&start, NULL));
264 while (likely(sigint == 0)) {
265 while (user_may_pull_from_tx(tx_ring.frames[it].iov_base)) {
266 hdr = tx_ring.frames[it].iov_base;
267 out = ((uint8_t *) hdr) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
269 do {
270 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic, out,
271 ring_frame_size(&tx_ring));
272 if (unlikely(ret <= 0))
273 goto out;
275 if (ring_frame_size(&tx_ring) <
276 pcap_get_length(&phdr, ctx->magic)) {
277 pcap_set_length(&phdr, ctx->magic,
278 ring_frame_size(&tx_ring));
279 trunced++;
281 } while (ctx->filter &&
282 !bpf_run_filter(&bpf_ops, out,
283 pcap_get_length(&phdr, ctx->magic)));
285 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &hdr->tp_h, NULL);
287 ctx->tx_bytes += hdr->tp_h.tp_len;;
288 ctx->tx_packets++;
290 show_frame_hdr(out, hdr->tp_h.tp_snaplen,
291 ctx->link_type, hdr, ctx->print_mode);
293 dissector_entry_point(out, hdr->tp_h.tp_snaplen,
294 ctx->link_type, ctx->print_mode);
296 kernel_may_pull_from_tx(&hdr->tp_h);
298 it++;
299 if (it >= tx_ring.layout.tp_frame_nr)
300 it = 0;
302 if (unlikely(sigint == 1))
303 break;
305 if (frame_count_max != 0) {
306 if (ctx->tx_packets >= frame_count_max) {
307 sigint = 1;
308 break;
314 out:
315 bug_on(gettimeofday(&end, NULL));
316 timersub(&end, &start, &diff);
318 timer_purge();
320 bpf_release(&bpf_ops);
322 dissector_cleanup_all();
323 destroy_tx_ring(tx_sock, &tx_ring);
325 if (ctx->rfraw)
326 leave_rfmon_mac80211(ctx->device_out);
328 if (__pcap_io->prepare_close_pcap)
329 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
331 if (!strncmp("-", ctx->device_in, strlen("-")))
332 dup2(fd, fileno(stdin));
333 close(fd);
335 close(tx_sock);
337 fflush(stdout);
338 printf("\n");
339 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
340 printf("\r%12lu packets truncated in file\n", trunced);
341 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
342 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
345 static void receive_to_xmit(struct ctx *ctx)
347 short ifflags = 0;
348 uint8_t *in, *out;
349 int rx_sock, ifindex_in, ifindex_out, ret;
350 size_t size_in, size_out;
351 unsigned int it_in = 0, it_out = 0;
352 unsigned long frame_count = 0;
353 struct frame_map *hdr_in, *hdr_out;
354 struct ring tx_ring, rx_ring;
355 struct pollfd rx_poll;
356 struct sock_fprog bpf_ops;
358 if (!strncmp(ctx->device_in, ctx->device_out, IFNAMSIZ))
359 panic("Ingress/egress devices must be different!\n");
360 if (!device_up_and_running(ctx->device_out))
361 panic("Egress device not up and running!\n");
363 rx_sock = pf_socket();
364 tx_sock = pf_socket();
366 ifindex_in = device_ifindex(ctx->device_in);
367 ifindex_out = device_ifindex(ctx->device_out);
369 size_in = ring_size(ctx->device_in, ctx->reserve_size);
370 size_out = ring_size(ctx->device_out, ctx->reserve_size);
372 enable_kernel_bpf_jit_compiler();
374 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
375 if (ctx->dump_bpf)
376 bpf_dump_all(&bpf_ops);
377 bpf_attach_to_sock(rx_sock, &bpf_ops);
379 ring_rx_setup(&rx_ring, rx_sock, size_in, ifindex_in, &rx_poll, false, ctx->jumbo, ctx->verbose);
380 ring_tx_setup(&tx_ring, tx_sock, size_out, ifindex_out, ctx->jumbo, ctx->verbose);
382 dissector_init_all(ctx->print_mode);
384 if (ctx->promiscuous)
385 ifflags = device_enter_promiscuous_mode(ctx->device_in);
387 if (ctx->kpull)
388 interval = ctx->kpull;
390 set_itimer_interval_value(&itimer, 0, interval);
391 setitimer(ITIMER_REAL, &itimer, NULL);
393 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
395 printf("Running! Hang up with ^C!\n\n");
396 fflush(stdout);
398 while (likely(sigint == 0)) {
399 while (user_may_pull_from_rx(rx_ring.frames[it_in].iov_base)) {
400 hdr_in = rx_ring.frames[it_in].iov_base;
401 in = ((uint8_t *) hdr_in) + hdr_in->tp_h.tp_mac;
403 frame_count++;
405 if (ctx->packet_type != -1)
406 if (ctx->packet_type != hdr_in->s_ll.sll_pkttype)
407 goto next;
409 hdr_out = tx_ring.frames[it_out].iov_base;
410 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
412 while (!user_may_pull_from_tx(tx_ring.frames[it_out].iov_base) &&
413 likely(!sigint)) {
414 if (ctx->randomize)
415 next_rnd_slot(&it_out, &tx_ring);
416 else {
417 it_out++;
418 if (it_out >= tx_ring.layout.tp_frame_nr)
419 it_out = 0;
422 hdr_out = tx_ring.frames[it_out].iov_base;
423 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
426 tpacket_hdr_clone(&hdr_out->tp_h, &hdr_in->tp_h);
427 fmemcpy(out, in, hdr_in->tp_h.tp_len);
429 kernel_may_pull_from_tx(&hdr_out->tp_h);
430 if (ctx->randomize)
431 next_rnd_slot(&it_out, &tx_ring);
432 else {
433 it_out++;
434 if (it_out >= tx_ring.layout.tp_frame_nr)
435 it_out = 0;
438 show_frame_hdr(in, hdr_in->tp_h.tp_snaplen,
439 ctx->link_type, hdr_in, ctx->print_mode);
441 dissector_entry_point(in, hdr_in->tp_h.tp_snaplen,
442 ctx->link_type, ctx->print_mode);
444 if (frame_count_max != 0) {
445 if (frame_count >= frame_count_max) {
446 sigint = 1;
447 break;
451 next:
452 kernel_may_pull_from_rx(&hdr_in->tp_h);
454 it_in++;
455 if (it_in >= rx_ring.layout.tp_frame_nr)
456 it_in = 0;
458 if (unlikely(sigint == 1))
459 goto out;
462 ret = poll(&rx_poll, 1, -1);
463 if (unlikely(ret < 0)) {
464 if (errno != EINTR)
465 panic("Poll failed!\n");
469 out:
470 timer_purge();
472 sock_rx_net_stats(rx_sock, 0);
474 bpf_release(&bpf_ops);
476 dissector_cleanup_all();
478 destroy_tx_ring(tx_sock, &tx_ring);
479 destroy_rx_ring(rx_sock, &rx_ring);
481 if (ctx->promiscuous)
482 device_leave_promiscuous_mode(ctx->device_in, ifflags);
484 close(tx_sock);
485 close(rx_sock);
488 static void translate_pcap_to_txf(int fdo, uint8_t *out, size_t len)
490 size_t bytes_done = 0;
491 char bout[80];
493 slprintf(bout, sizeof(bout), "{\n ");
494 write_or_die(fdo, bout, strlen(bout));
496 while (bytes_done < len) {
497 slprintf(bout, sizeof(bout), "0x%02x, ", out[bytes_done]);
498 write_or_die(fdo, bout, strlen(bout));
500 bytes_done++;
502 if (bytes_done % 10 == 0) {
503 slprintf(bout, sizeof(bout), "\n");
504 write_or_die(fdo, bout, strlen(bout));
506 if (bytes_done < len) {
507 slprintf(bout, sizeof(bout), " ");
508 write_or_die(fdo, bout, strlen(bout));
512 if (bytes_done % 10 != 0) {
513 slprintf(bout, sizeof(bout), "\n");
514 write_or_die(fdo, bout, strlen(bout));
517 slprintf(bout, sizeof(bout), "}\n\n");
518 write_or_die(fdo, bout, strlen(bout));
521 static void read_pcap(struct ctx *ctx)
523 uint8_t *out;
524 int ret, fd, fdo = 0;
525 unsigned long trunced = 0;
526 size_t out_len;
527 pcap_pkthdr_t phdr;
528 struct sock_fprog bpf_ops;
529 struct frame_map fm;
530 struct timeval start, end, diff;
532 bug_on(!__pcap_io);
534 if (!strncmp("-", ctx->device_in, strlen("-"))) {
535 fd = dup_or_die(fileno(stdin));
536 close(fileno(stdin));
537 if (ctx->pcap == PCAP_OPS_MM)
538 ctx->pcap = PCAP_OPS_SG;
539 } else {
540 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
543 if (__pcap_io->init_once_pcap)
544 __pcap_io->init_once_pcap();
546 ret = __pcap_io->pull_fhdr_pcap(fd, &ctx->magic, &ctx->link_type);
547 if (ret)
548 panic("Error reading pcap header!\n");
550 if (__pcap_io->prepare_access_pcap) {
551 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_RD, ctx->jumbo);
552 if (ret)
553 panic("Error prepare reading pcap!\n");
556 fmemset(&fm, 0, sizeof(fm));
558 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
559 if (ctx->dump_bpf)
560 bpf_dump_all(&bpf_ops);
562 dissector_init_all(ctx->print_mode);
564 out_len = round_up(1024 * 1024, RUNTIME_PAGE_SIZE);
565 out = xmalloc_aligned(out_len, CO_CACHE_LINE_SIZE);
567 if (ctx->device_out) {
568 if (!strncmp("-", ctx->device_out, strlen("-"))) {
569 fdo = dup_or_die(fileno(stdout));
570 close(fileno(stdout));
571 } else {
572 fdo = open_or_die_m(ctx->device_out, O_RDWR | O_CREAT |
573 O_TRUNC | O_LARGEFILE, DEFFILEMODE);
577 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
579 printf("Running! Hang up with ^C!\n\n");
580 fflush(stdout);
582 bug_on(gettimeofday(&start, NULL));
584 while (likely(sigint == 0)) {
585 do {
586 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic,
587 out, out_len);
588 if (unlikely(ret < 0))
589 goto out;
591 if (unlikely(pcap_get_length(&phdr, ctx->magic) == 0)) {
592 trunced++;
593 continue;
596 if (unlikely(pcap_get_length(&phdr, ctx->magic) > out_len)) {
597 pcap_set_length(&phdr, ctx->magic, out_len);
598 trunced++;
600 } while (ctx->filter &&
601 !bpf_run_filter(&bpf_ops, out,
602 pcap_get_length(&phdr, ctx->magic)));
604 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &fm.tp_h, &fm.s_ll);
606 ctx->tx_bytes += fm.tp_h.tp_len;
607 ctx->tx_packets++;
609 show_frame_hdr(out, fm.tp_h.tp_snaplen, ctx->link_type, &fm,
610 ctx->print_mode);
612 dissector_entry_point(out, fm.tp_h.tp_snaplen,
613 ctx->link_type, ctx->print_mode);
615 if (ctx->device_out)
616 translate_pcap_to_txf(fdo, out, fm.tp_h.tp_snaplen);
618 if (frame_count_max != 0) {
619 if (ctx->tx_packets >= frame_count_max) {
620 sigint = 1;
621 break;
626 out:
627 bug_on(gettimeofday(&end, NULL));
628 timersub(&end, &start, &diff);
630 bpf_release(&bpf_ops);
632 dissector_cleanup_all();
634 if (__pcap_io->prepare_close_pcap)
635 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
637 xfree(out);
639 fflush(stdout);
640 printf("\n");
641 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
642 printf("\r%12lu packets truncated in file\n", trunced);
643 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
644 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
646 if (!strncmp("-", ctx->device_in, strlen("-")))
647 dup2(fd, fileno(stdin));
648 close(fd);
650 if (ctx->device_out) {
651 if (!strncmp("-", ctx->device_out, strlen("-")))
652 dup2(fdo, fileno(stdout));
653 close(fdo);
657 static void finish_multi_pcap_file(struct ctx *ctx, int fd)
659 __pcap_io->fsync_pcap(fd);
661 if (__pcap_io->prepare_close_pcap)
662 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
664 close(fd);
666 fmemset(&itimer, 0, sizeof(itimer));
667 setitimer(ITIMER_REAL, &itimer, NULL);
670 static int next_multi_pcap_file(struct ctx *ctx, int fd)
672 int ret;
673 char fname[512];
675 __pcap_io->fsync_pcap(fd);
677 if (__pcap_io->prepare_close_pcap)
678 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
680 close(fd);
682 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
683 ctx->prefix ? : "dump-", time(NULL));
685 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
686 O_LARGEFILE, DEFFILEMODE);
688 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
689 if (ret)
690 panic("Error writing pcap header!\n");
692 if (__pcap_io->prepare_access_pcap) {
693 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, true);
694 if (ret)
695 panic("Error prepare writing pcap!\n");
698 return fd;
701 static int begin_multi_pcap_file(struct ctx *ctx)
703 int fd, ret;
704 char fname[256];
706 bug_on(!__pcap_io);
708 if (ctx->device_out[strlen(ctx->device_out) - 1] == '/')
709 ctx->device_out[strlen(ctx->device_out) - 1] = 0;
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 if (ctx->dump_mode == DUMP_INTERVAL_TIME) {
728 interval = ctx->dump_interval;
730 set_itimer_interval_value(&itimer, interval, 0);
731 setitimer(ITIMER_REAL, &itimer, NULL);
732 } else {
733 interval = 0;
736 return fd;
739 static void finish_single_pcap_file(struct ctx *ctx, int fd)
741 __pcap_io->fsync_pcap(fd);
743 if (__pcap_io->prepare_close_pcap)
744 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
746 if (strncmp("-", ctx->device_out, strlen("-")))
747 close(fd);
748 else
749 dup2(fd, fileno(stdout));
752 static int begin_single_pcap_file(struct ctx *ctx)
754 int fd, ret;
756 bug_on(!__pcap_io);
758 if (!strncmp("-", ctx->device_out, strlen("-"))) {
759 fd = dup_or_die(fileno(stdout));
760 close(fileno(stdout));
761 if (ctx->pcap == PCAP_OPS_MM)
762 ctx->pcap = PCAP_OPS_SG;
763 } else {
764 fd = open_or_die_m(ctx->device_out,
765 O_RDWR | O_CREAT | O_TRUNC |
766 O_LARGEFILE, DEFFILEMODE);
769 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
770 if (ret)
771 panic("Error writing pcap header!\n");
773 if (__pcap_io->prepare_access_pcap) {
774 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, true);
775 if (ret)
776 panic("Error prepare writing pcap!\n");
779 return fd;
782 static void print_pcap_file_stats(int sock, struct ctx *ctx)
784 int ret;
785 struct tpacket_stats kstats;
786 socklen_t slen = sizeof(kstats);
788 fmemset(&kstats, 0, sizeof(kstats));
790 ret = getsockopt(sock, SOL_PACKET, PACKET_STATISTICS, &kstats, &slen);
791 if (unlikely(ret))
792 panic("Cannot get packet statistics!\n");
794 if (ctx->print_mode == PRINT_NONE) {
795 printf(".(+%u/-%u)", kstats.tp_packets - kstats.tp_drops,
796 kstats.tp_drops);
797 fflush(stdout);
801 #ifdef HAVE_TPACKET3
802 static void walk_t3_block(struct block_desc *pbd, struct ctx *ctx,
803 int sock, int *fd, unsigned long *frame_count)
805 int num_pkts = pbd->h1.num_pkts, i;
806 struct tpacket3_hdr *hdr;
807 struct sockaddr_ll *sll;
809 hdr = (void *) ((uint8_t *) pbd + pbd->h1.offset_to_first_pkt);
810 sll = (void *) ((uint8_t *) hdr + TPACKET_ALIGN(sizeof(*hdr)));
812 for (i = 0; i < num_pkts && likely(sigint == 0); ++i) {
813 uint8_t *packet = ((uint8_t *) hdr + hdr->tp_mac);
814 pcap_pkthdr_t phdr;
816 if (ctx->packet_type != -1)
817 if (ctx->packet_type != sll->sll_pkttype)
818 goto next;
820 (*frame_count)++;
822 if (dump_to_pcap(ctx)) {
823 int ret;
825 tpacket3_hdr_to_pcap_pkthdr(hdr, sll, &phdr, ctx->magic);
827 ret = __pcap_io->write_pcap(*fd, &phdr, ctx->magic, packet,
828 pcap_get_length(&phdr, ctx->magic));
829 if (unlikely(ret != (int) pcap_get_total_length(&phdr, ctx->magic)))
830 panic("Write error to pcap!\n");
833 __show_frame_hdr(packet, hdr->tp_snaplen, ctx->link_type, sll,
834 hdr, ctx->print_mode, true);
836 dissector_entry_point(packet, hdr->tp_snaplen, ctx->link_type,
837 ctx->print_mode);
838 next:
839 hdr = (void *) ((uint8_t *) hdr + hdr->tp_next_offset);
840 sll = (void *) ((uint8_t *) hdr + TPACKET_ALIGN(sizeof(*hdr)));
842 if (frame_count_max != 0) {
843 if (unlikely(*frame_count >= frame_count_max)) {
844 sigint = 1;
845 break;
849 if (dump_to_pcap(ctx)) {
850 if (ctx->dump_mode == DUMP_INTERVAL_SIZE) {
851 interval += hdr->tp_snaplen;
852 if (interval > ctx->dump_interval) {
853 next_dump = true;
854 interval = 0;
858 if (next_dump) {
859 *fd = next_multi_pcap_file(ctx, *fd);
860 next_dump = false;
862 if (unlikely(ctx->verbose))
863 print_pcap_file_stats(sock, ctx);
868 #endif /* HAVE_TPACKET3 */
870 static void recv_only_or_dump(struct ctx *ctx)
872 short ifflags = 0;
873 int sock, ifindex, fd = 0, ret;
874 size_t size;
875 unsigned int it = 0;
876 struct ring rx_ring;
877 struct pollfd rx_poll;
878 struct sock_fprog bpf_ops;
879 struct timeval start, end, diff;
880 unsigned long frame_count = 0;
882 sock = pf_socket();
884 if (ctx->rfraw) {
885 ctx->device_trans = xstrdup(ctx->device_in);
886 xfree(ctx->device_in);
888 enter_rfmon_mac80211(ctx->device_trans, &ctx->device_in);
889 ctx->link_type = LINKTYPE_IEEE802_11;
892 ifindex = device_ifindex(ctx->device_in);
894 size = ring_size(ctx->device_in, ctx->reserve_size);
896 enable_kernel_bpf_jit_compiler();
898 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
899 if (ctx->dump_bpf)
900 bpf_dump_all(&bpf_ops);
901 bpf_attach_to_sock(sock, &bpf_ops);
903 if (ctx->hwtimestamp) {
904 ret = set_sockopt_hwtimestamp(sock, ctx->device_in);
905 if (ret == 0 && ctx->verbose)
906 printf("HW timestamping enabled\n");
909 ring_rx_setup(&rx_ring, sock, size, ifindex, &rx_poll, true, true, ctx->verbose);
911 dissector_init_all(ctx->print_mode);
913 if (ctx->cpu >= 0 && ifindex > 0) {
914 int irq = device_irq_number(ctx->device_in);
915 device_set_irq_affinity(irq, ctx->cpu);
917 if (ctx->verbose)
918 printf("IRQ: %s:%d > CPU%d\n",
919 ctx->device_in, irq, ctx->cpu);
922 if (ctx->promiscuous)
923 ifflags = device_enter_promiscuous_mode(ctx->device_in);
925 if (dump_to_pcap(ctx) && __pcap_io->init_once_pcap)
926 __pcap_io->init_once_pcap();
928 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
930 if (dump_to_pcap(ctx)) {
931 struct stat stats;
933 ret = stat(ctx->device_out, &stats);
934 if (ret < 0)
935 ctx->dump_dir = 0;
936 else
937 ctx->dump_dir = S_ISDIR(stats.st_mode);
939 if (ctx->dump_dir)
940 fd = begin_multi_pcap_file(ctx);
941 else
942 fd = begin_single_pcap_file(ctx);
945 printf("Running! Hang up with ^C!\n\n");
946 fflush(stdout);
948 bug_on(gettimeofday(&start, NULL));
950 while (likely(sigint == 0)) {
951 #ifdef HAVE_TPACKET3
952 struct block_desc *pbd;
954 while (user_may_pull_from_rx_block((pbd = rx_ring.frames[it].iov_base))) {
955 walk_t3_block(pbd, ctx, sock, &fd, &frame_count);
957 kernel_may_pull_from_rx_block(pbd);
958 it = (it + 1) % rx_ring.layout3.tp_block_nr;
960 if (unlikely(sigint == 1))
961 break;
963 #else
964 while (user_may_pull_from_rx(rx_ring.frames[it].iov_base)) {
965 struct frame_map *hdr = rx_ring.frames[it].iov_base;
966 uint8_t *packet = ((uint8_t *) hdr) + hdr->tp_h.tp_mac;
967 pcap_pkthdr_t phdr;
969 if (ctx->packet_type != -1)
970 if (ctx->packet_type != hdr->s_ll.sll_pkttype)
971 goto next;
973 frame_count++;
975 if (unlikely(ring_frame_size(&rx_ring) < hdr->tp_h.tp_snaplen)) {
976 /* XXX: silently ignore for now. We used to
977 * report them with sock_rx_net_stats() */
978 goto next;
981 if (dump_to_pcap(ctx)) {
982 tpacket_hdr_to_pcap_pkthdr(&hdr->tp_h, &hdr->s_ll, &phdr, ctx->magic);
984 ret = __pcap_io->write_pcap(fd, &phdr, ctx->magic, packet,
985 pcap_get_length(&phdr, ctx->magic));
986 if (unlikely(ret != (int) pcap_get_total_length(&phdr, ctx->magic)))
987 panic("Write error to pcap!\n");
990 show_frame_hdr(packet, hdr->tp_h.tp_snaplen,
991 ctx->link_type, hdr, ctx->print_mode);
993 dissector_entry_point(packet, hdr->tp_h.tp_snaplen,
994 ctx->link_type, ctx->print_mode);
996 if (frame_count_max != 0) {
997 if (unlikely(frame_count >= frame_count_max)) {
998 sigint = 1;
999 break;
1003 next:
1004 kernel_may_pull_from_rx(&hdr->tp_h);
1005 it = (it + 1) % rx_ring.layout.tp_frame_nr;
1007 if (unlikely(sigint == 1))
1008 break;
1010 if (dump_to_pcap(ctx)) {
1011 if (ctx->dump_mode == DUMP_INTERVAL_SIZE) {
1012 interval += hdr->tp_h.tp_snaplen;
1013 if (interval > ctx->dump_interval) {
1014 next_dump = true;
1015 interval = 0;
1019 if (next_dump) {
1020 fd = next_multi_pcap_file(ctx, fd);
1021 next_dump = false;
1023 if (unlikely(ctx->verbose))
1024 print_pcap_file_stats(sock, ctx);
1028 #endif /* HAVE_TPACKET3 */
1030 ret = poll(&rx_poll, 1, -1);
1031 if (unlikely(ret < 0)) {
1032 if (errno != EINTR)
1033 panic("Poll failed!\n");
1037 bug_on(gettimeofday(&end, NULL));
1038 timersub(&end, &start, &diff);
1040 if (!(ctx->dump_dir && ctx->print_mode == PRINT_NONE)) {
1041 sock_rx_net_stats(sock, frame_count);
1043 printf("\r%12lu sec, %lu usec in total\n",
1044 diff.tv_sec, diff.tv_usec);
1045 } else {
1046 printf("\n\n");
1047 fflush(stdout);
1050 bpf_release(&bpf_ops);
1051 dissector_cleanup_all();
1052 destroy_rx_ring(sock, &rx_ring);
1054 if (ctx->promiscuous)
1055 device_leave_promiscuous_mode(ctx->device_in, ifflags);
1057 if (ctx->rfraw)
1058 leave_rfmon_mac80211(ctx->device_in);
1060 if (dump_to_pcap(ctx)) {
1061 if (ctx->dump_dir)
1062 finish_multi_pcap_file(ctx, fd);
1063 else
1064 finish_single_pcap_file(ctx, fd);
1067 close(sock);
1070 static void init_ctx(struct ctx *ctx)
1072 memset(ctx, 0, sizeof(*ctx));
1073 ctx->uid = getuid();
1074 ctx->uid = getgid();
1076 ctx->cpu = -1;
1077 ctx->packet_type = -1;
1079 ctx->magic = ORIGINAL_TCPDUMP_MAGIC;
1080 ctx->print_mode = PRINT_NORM;
1081 ctx->pcap = PCAP_OPS_SG;
1083 ctx->dump_mode = DUMP_INTERVAL_TIME;
1084 ctx->dump_interval = 60;
1086 ctx->promiscuous = true;
1087 ctx->randomize = false;
1088 ctx->hwtimestamp = true;
1091 static void destroy_ctx(struct ctx *ctx)
1093 free(ctx->device_in);
1094 free(ctx->device_out);
1095 free(ctx->device_trans);
1097 free(ctx->prefix);
1100 static void __noreturn help(void)
1102 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING);
1103 puts("http://www.netsniff-ng.org\n\n"
1104 "Usage: netsniff-ng [options] [filter-expression]\n"
1105 "Options:\n"
1106 " -i|-d|--dev|--in <dev|pcap|-> Input source as netdev, pcap or pcap stdin\n"
1107 " -o|--out <dev|pcap|dir|cfg|-> Output sink as netdev, pcap, directory, trafgen, or stdout\n"
1108 " -f|--filter <bpf-file|expr> Use BPF filter file from bpfc or tcpdump-like expression\n"
1109 " -t|--type <type> Filter for: host|broadcast|multicast|others|outgoing\n"
1110 " -F|--interval <size|time> Dump interval if -o is a dir: <num>KiB/MiB/GiB/s/sec/min/hrs\n"
1111 " -R|--rfraw Capture or inject raw 802.11 frames\n"
1112 " -n|--num <0|uint> Number of packets until exit (def: 0)\n"
1113 " -P|--prefix <name> Prefix for pcaps stored in directory\n"
1114 " -T|--magic <pcap-magic> Pcap magic number/pcap format to store, see -D\n"
1115 " -D|--dump-pcap-types Dump pcap types and magic numbers and quit\n"
1116 " -B|--dump-bpf Dump generated BPF assembly\n"
1117 " -r|--rand Randomize packet forwarding order (dev->dev)\n"
1118 " -M|--no-promisc No promiscuous mode for netdev\n"
1119 " -A|--no-sock-mem Don't tune core socket memory\n"
1120 " -N|--no-hwtimestamp Disable hardware time stamping\n"
1121 " -m|--mmap Mmap(2) pcap file I/O, e.g. for replaying pcaps\n"
1122 " -G|--sg Scatter/gather pcap file I/O\n"
1123 " -c|--clrw Use slower read(2)/write(2) I/O\n"
1124 " -S|--ring-size <size> Specify ring size to: <num>KiB/MiB/GiB\n"
1125 " -k|--kernel-pull <uint> Kernel pull from user interval in us (def: 10us)\n"
1126 " -J|--jumbo-support Support replay/fwd 64KB Super Jumbo Frames (def: 2048B)\n"
1127 " -b|--bind-cpu <cpu> Bind to specific CPU\n"
1128 " -u|--user <userid> Drop privileges and change to userid\n"
1129 " -g|--group <groupid> Drop privileges and change to groupid\n"
1130 " -H|--prio-high Make this high priority process\n"
1131 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
1132 " -s|--silent Do not print captured packets\n"
1133 " -q|--less Print less-verbose packet information\n"
1134 " -X|--hex Print packet data in hex format\n"
1135 " -l|--ascii Print human-readable packet data\n"
1136 " -U|--update Update GeoIP databases\n"
1137 " -V|--verbose Be more verbose\n"
1138 " -v|--version Show version and exit\n"
1139 " -h|--help Guess what?!\n\n"
1140 "Examples:\n"
1141 " netsniff-ng --in eth0 --out dump.pcap -s -T 0xa1b2c3d4 --b 0 tcp or udp\n"
1142 " netsniff-ng --in wlan0 --rfraw --out dump.pcap --silent --bind-cpu 0\n"
1143 " netsniff-ng --in dump.pcap --mmap --out eth0 -k1000 --silent --bind-cpu 0\n"
1144 " netsniff-ng --in dump.pcap --out dump.cfg --silent --bind-cpu 0\n"
1145 " netsniff-ng --in eth0 --out eth1 --silent --bind-cpu 0 -J --type host\n"
1146 " netsniff-ng --in eth1 --out /opt/probe/ -s -m --interval 100MiB -b 0\n"
1147 " netsniff-ng --in vlan0 --out dump.pcap -c -u `id -u bob` -g `id -g bob`\n"
1148 " netsniff-ng --in any --filter http.bpf --jumbo-support --ascii -V\n\n"
1149 "Note:\n"
1150 " For introducing bit errors, delays with random variation and more\n"
1151 " while replaying pcaps, make use of tc(8) with its disciplines (e.g. netem).\n\n");
1152 puts(copyright);
1153 die();
1156 static void __noreturn version(void)
1158 printf("\nnetsniff-ng %s, Git id: %s\n", VERSION_LONG, GITVERSION);
1159 puts("the packet sniffing beast\n"
1160 "http://www.netsniff-ng.org\n\n");
1161 puts(copyright);
1162 die();
1165 int main(int argc, char **argv)
1167 char *ptr;
1168 int c, i, j, cpu_tmp, opt_index, ops_touched = 0, vals[4] = {0};
1169 bool prio_high = false, setsockmem = true;
1170 void (*main_loop)(struct ctx *ctx) = NULL;
1171 struct ctx ctx;
1173 init_ctx(&ctx);
1174 srand(time(NULL));
1176 while ((c = getopt_long(argc, argv, short_options, long_options,
1177 &opt_index)) != EOF) {
1178 switch (c) {
1179 case 'd':
1180 case 'i':
1181 ctx.device_in = xstrdup(optarg);
1182 break;
1183 case 'o':
1184 ctx.device_out = xstrdup(optarg);
1185 break;
1186 case 'P':
1187 ctx.prefix = xstrdup(optarg);
1188 break;
1189 case 'R':
1190 ctx.link_type = LINKTYPE_IEEE802_11;
1191 ctx.rfraw = 1;
1192 break;
1193 case 'r':
1194 ctx.randomize = true;
1195 break;
1196 case 'J':
1197 ctx.jumbo = true;
1198 break;
1199 case 'T':
1200 ctx.magic = (uint32_t) strtoul(optarg, NULL, 0);
1201 pcap_check_magic(ctx.magic);
1202 break;
1203 case 'f':
1204 ctx.filter = xstrdup(optarg);
1205 break;
1206 case 'M':
1207 ctx.promiscuous = false;
1208 break;
1209 case 'N':
1210 ctx.hwtimestamp = false;
1211 break;
1212 case 'A':
1213 setsockmem = false;
1214 break;
1215 case 'u':
1216 ctx.uid = strtoul(optarg, NULL, 0);
1217 ctx.enforce = true;
1218 break;
1219 case 'g':
1220 ctx.gid = strtoul(optarg, NULL, 0);
1221 ctx.enforce = true;
1222 break;
1223 case 't':
1224 if (!strncmp(optarg, "host", strlen("host")))
1225 ctx.packet_type = PACKET_HOST;
1226 else if (!strncmp(optarg, "broadcast", strlen("broadcast")))
1227 ctx.packet_type = PACKET_BROADCAST;
1228 else if (!strncmp(optarg, "multicast", strlen("multicast")))
1229 ctx.packet_type = PACKET_MULTICAST;
1230 else if (!strncmp(optarg, "others", strlen("others")))
1231 ctx.packet_type = PACKET_OTHERHOST;
1232 else if (!strncmp(optarg, "outgoing", strlen("outgoing")))
1233 ctx.packet_type = PACKET_OUTGOING;
1234 else
1235 ctx.packet_type = -1;
1236 break;
1237 case 'S':
1238 ptr = optarg;
1239 for (j = i = strlen(optarg); i > 0; --i) {
1240 if (!isdigit(optarg[j - i]))
1241 break;
1242 ptr++;
1245 if (!strncmp(ptr, "KiB", strlen("KiB")))
1246 ctx.reserve_size = 1 << 10;
1247 else if (!strncmp(ptr, "MiB", strlen("MiB")))
1248 ctx.reserve_size = 1 << 20;
1249 else if (!strncmp(ptr, "GiB", strlen("GiB")))
1250 ctx.reserve_size = 1 << 30;
1251 else
1252 panic("Syntax error in ring size param!\n");
1254 ctx.reserve_size *= strtoul(optarg, NULL, 0);
1255 break;
1256 case 'b':
1257 cpu_tmp = strtol(optarg, NULL, 0);
1259 cpu_affinity(cpu_tmp);
1260 if (ctx.cpu != -2)
1261 ctx.cpu = cpu_tmp;
1262 break;
1263 case 'H':
1264 prio_high = true;
1265 break;
1266 case 'c':
1267 ctx.pcap = PCAP_OPS_RW;
1268 ops_touched = 1;
1269 break;
1270 case 'm':
1271 ctx.pcap = PCAP_OPS_MM;
1272 ops_touched = 1;
1273 break;
1274 case 'G':
1275 ctx.pcap = PCAP_OPS_SG;
1276 ops_touched = 1;
1277 break;
1278 case 'Q':
1279 ctx.cpu = -2;
1280 break;
1281 case 's':
1282 ctx.print_mode = PRINT_NONE;
1283 break;
1284 case 'q':
1285 ctx.print_mode = PRINT_LESS;
1286 break;
1287 case 'X':
1288 ctx.print_mode =
1289 (ctx.print_mode == PRINT_ASCII) ?
1290 PRINT_HEX_ASCII : PRINT_HEX;
1291 break;
1292 case 'l':
1293 ctx.print_mode =
1294 (ctx.print_mode == PRINT_HEX) ?
1295 PRINT_HEX_ASCII : PRINT_ASCII;
1296 break;
1297 case 'k':
1298 ctx.kpull = strtoul(optarg, NULL, 0);
1299 break;
1300 case 'n':
1301 frame_count_max = strtoul(optarg, NULL, 0);
1302 break;
1303 case 'F':
1304 ptr = optarg;
1305 for (j = i = strlen(optarg); i > 0; --i) {
1306 if (!isdigit(optarg[j - i]))
1307 break;
1308 ptr++;
1311 if (!strncmp(ptr, "KiB", strlen("KiB"))) {
1312 ctx.dump_interval = 1 << 10;
1313 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1314 } else if (!strncmp(ptr, "MiB", strlen("MiB"))) {
1315 ctx.dump_interval = 1 << 20;
1316 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1317 } else if (!strncmp(ptr, "GiB", strlen("GiB"))) {
1318 ctx.dump_interval = 1 << 30;
1319 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1320 } else if (!strncmp(ptr, "sec", strlen("sec"))) {
1321 ctx.dump_interval = 1;
1322 ctx.dump_mode = DUMP_INTERVAL_TIME;
1323 } else if (!strncmp(ptr, "min", strlen("min"))) {
1324 ctx.dump_interval = 60;
1325 ctx.dump_mode = DUMP_INTERVAL_TIME;
1326 } else if (!strncmp(ptr, "hrs", strlen("hrs"))) {
1327 ctx.dump_interval = 60 * 60;
1328 ctx.dump_mode = DUMP_INTERVAL_TIME;
1329 } else if (!strncmp(ptr, "s", strlen("s"))) {
1330 ctx.dump_interval = 1;
1331 ctx.dump_mode = DUMP_INTERVAL_TIME;
1332 } else {
1333 panic("Syntax error in time/size param!\n");
1336 ctx.dump_interval *= strtoul(optarg, NULL, 0);
1337 break;
1338 case 'V':
1339 ctx.verbose = true;
1340 break;
1341 case 'B':
1342 ctx.dump_bpf = true;
1343 break;
1344 case 'D':
1345 pcap_dump_type_features();
1346 die();
1347 break;
1348 case 'U':
1349 update_geoip();
1350 die();
1351 break;
1352 case 'v':
1353 version();
1354 break;
1355 case 'h':
1356 help();
1357 break;
1358 case '?':
1359 switch (optopt) {
1360 case 'd':
1361 case 'i':
1362 case 'o':
1363 case 'f':
1364 case 't':
1365 case 'P':
1366 case 'F':
1367 case 'n':
1368 case 'S':
1369 case 'b':
1370 case 'k':
1371 case 'T':
1372 case 'u':
1373 case 'g':
1374 case 'e':
1375 panic("Option -%c requires an argument!\n",
1376 optopt);
1377 default:
1378 if (isprint(optopt))
1379 printf("Unknown option character `0x%X\'!\n", optopt);
1380 die();
1382 default:
1383 break;
1387 if (!ctx.filter && optind != argc) {
1388 int ret;
1389 off_t offset = 0;
1391 for (i = optind; i < argc; ++i) {
1392 size_t alen = strlen(argv[i]) + 2;
1393 size_t flen = ctx.filter ? strlen(ctx.filter) : 0;
1395 ctx.filter = xrealloc(ctx.filter, 1, flen + alen);
1396 ret = slprintf(ctx.filter + offset, strlen(argv[i]) + 2, "%s ", argv[i]);
1397 if (ret < 0)
1398 panic("Cannot concatenate filter string!\n");
1399 else
1400 offset += ret;
1404 if (!ctx.device_in)
1405 ctx.device_in = xstrdup("any");
1407 register_signal(SIGINT, signal_handler);
1408 register_signal(SIGQUIT, signal_handler);
1409 register_signal(SIGTERM, signal_handler);
1410 register_signal(SIGHUP, signal_handler);
1412 tprintf_init();
1414 if (prio_high) {
1415 set_proc_prio(-20);
1416 set_sched_status(SCHED_FIFO, sched_get_priority_max(SCHED_FIFO));
1419 if (device_mtu(ctx.device_in) || !strncmp("any", ctx.device_in, strlen(ctx.device_in))) {
1420 if (!ctx.rfraw)
1421 ctx.link_type = pcap_devtype_to_linktype(ctx.device_in);
1422 if (!ctx.device_out) {
1423 ctx.dump = 0;
1424 main_loop = recv_only_or_dump;
1425 } else if (device_mtu(ctx.device_out)) {
1426 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1427 main_loop = receive_to_xmit;
1428 } else {
1429 ctx.dump = 1;
1430 register_signal_f(SIGALRM, timer_next_dump, SA_SIGINFO);
1431 main_loop = recv_only_or_dump;
1432 if (!ops_touched)
1433 ctx.pcap = PCAP_OPS_SG;
1435 } else {
1436 if (ctx.device_out && device_mtu(ctx.device_out)) {
1437 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1438 main_loop = pcap_to_xmit;
1439 if (!ops_touched)
1440 ctx.pcap = PCAP_OPS_MM;
1441 } else {
1442 main_loop = read_pcap;
1443 if (!ops_touched)
1444 ctx.pcap = PCAP_OPS_SG;
1448 bug_on(!main_loop);
1450 init_geoip(0);
1451 if (setsockmem)
1452 set_system_socket_memory(vals, array_size(vals));
1453 if (!ctx.enforce)
1454 xlockme();
1456 if (ctx.verbose)
1457 printf("pcap file I/O method: %s\n", pcap_ops_group_to_str[ctx.pcap]);
1459 main_loop(&ctx);
1461 if (!ctx.enforce)
1462 xunlockme();
1463 if (setsockmem)
1464 reset_system_socket_memory(vals, array_size(vals));
1465 destroy_geoip();
1467 device_restore_irq_affinity_list();
1468 tprintf_cleanup();
1470 destroy_ctx(&ctx);
1471 return 0;