build: Enable customization of sysconfdir (ETCDIR)
[netsniff-ng.git] / netsniff-ng.c
blob427b5e94f173abea3843362558587a0e16412f1f
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 static void update_pcap_next_dump(struct ctx *ctx, unsigned long snaplen, int *fd, int sock)
803 if (!dump_to_pcap(ctx))
804 return;
806 if (ctx->dump_mode == DUMP_INTERVAL_SIZE) {
807 interval += snaplen;
808 if (interval > ctx->dump_interval) {
809 next_dump = true;
810 interval = 0;
814 if (next_dump) {
815 *fd = next_multi_pcap_file(ctx, *fd);
816 next_dump = false;
818 if (ctx->verbose)
819 print_pcap_file_stats(sock, ctx);
823 #ifdef HAVE_TPACKET3
824 static void walk_t3_block(struct block_desc *pbd, struct ctx *ctx,
825 int sock, int *fd, unsigned long *frame_count)
827 int num_pkts = pbd->h1.num_pkts, i;
828 struct tpacket3_hdr *hdr;
829 struct sockaddr_ll *sll;
831 hdr = (void *) ((uint8_t *) pbd + pbd->h1.offset_to_first_pkt);
832 sll = (void *) ((uint8_t *) hdr + TPACKET_ALIGN(sizeof(*hdr)));
834 for (i = 0; i < num_pkts && likely(sigint == 0); ++i) {
835 uint8_t *packet = ((uint8_t *) hdr + hdr->tp_mac);
836 pcap_pkthdr_t phdr;
838 if (ctx->packet_type != -1)
839 if (ctx->packet_type != sll->sll_pkttype)
840 goto next;
842 (*frame_count)++;
844 if (dump_to_pcap(ctx)) {
845 int ret;
847 tpacket3_hdr_to_pcap_pkthdr(hdr, sll, &phdr, ctx->magic);
849 ret = __pcap_io->write_pcap(*fd, &phdr, ctx->magic, packet,
850 pcap_get_length(&phdr, ctx->magic));
851 if (unlikely(ret != (int) pcap_get_total_length(&phdr, ctx->magic)))
852 panic("Write error to pcap!\n");
855 __show_frame_hdr(packet, hdr->tp_snaplen, ctx->link_type, sll,
856 hdr, ctx->print_mode, true);
858 dissector_entry_point(packet, hdr->tp_snaplen, ctx->link_type,
859 ctx->print_mode);
860 next:
861 hdr = (void *) ((uint8_t *) hdr + hdr->tp_next_offset);
862 sll = (void *) ((uint8_t *) hdr + TPACKET_ALIGN(sizeof(*hdr)));
864 if (frame_count_max != 0) {
865 if (unlikely(*frame_count >= frame_count_max)) {
866 sigint = 1;
867 break;
871 update_pcap_next_dump(ctx, hdr->tp_snaplen, fd, sock);
874 #endif /* HAVE_TPACKET3 */
876 static void recv_only_or_dump(struct ctx *ctx)
878 short ifflags = 0;
879 int sock, ifindex, fd = 0, ret;
880 size_t size;
881 unsigned int it = 0;
882 struct ring rx_ring;
883 struct pollfd rx_poll;
884 struct sock_fprog bpf_ops;
885 struct timeval start, end, diff;
886 unsigned long frame_count = 0;
888 sock = pf_socket();
890 if (ctx->rfraw) {
891 ctx->device_trans = xstrdup(ctx->device_in);
892 xfree(ctx->device_in);
894 enter_rfmon_mac80211(ctx->device_trans, &ctx->device_in);
895 ctx->link_type = LINKTYPE_IEEE802_11;
898 ifindex = device_ifindex(ctx->device_in);
900 size = ring_size(ctx->device_in, ctx->reserve_size);
902 enable_kernel_bpf_jit_compiler();
904 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
905 if (ctx->dump_bpf)
906 bpf_dump_all(&bpf_ops);
907 bpf_attach_to_sock(sock, &bpf_ops);
909 if (ctx->hwtimestamp) {
910 ret = set_sockopt_hwtimestamp(sock, ctx->device_in);
911 if (ret == 0 && ctx->verbose)
912 printf("HW timestamping enabled\n");
915 ring_rx_setup(&rx_ring, sock, size, ifindex, &rx_poll, is_defined(HAVE_TPACKET3), true, ctx->verbose);
917 dissector_init_all(ctx->print_mode);
919 if (ctx->cpu >= 0 && ifindex > 0) {
920 int irq = device_irq_number(ctx->device_in);
921 device_set_irq_affinity(irq, ctx->cpu);
923 if (ctx->verbose)
924 printf("IRQ: %s:%d > CPU%d\n",
925 ctx->device_in, irq, ctx->cpu);
928 if (ctx->promiscuous)
929 ifflags = device_enter_promiscuous_mode(ctx->device_in);
931 if (dump_to_pcap(ctx) && __pcap_io->init_once_pcap)
932 __pcap_io->init_once_pcap();
934 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
936 if (dump_to_pcap(ctx)) {
937 struct stat stats;
939 ret = stat(ctx->device_out, &stats);
940 if (ret < 0)
941 ctx->dump_dir = 0;
942 else
943 ctx->dump_dir = S_ISDIR(stats.st_mode);
945 if (ctx->dump_dir)
946 fd = begin_multi_pcap_file(ctx);
947 else
948 fd = begin_single_pcap_file(ctx);
951 printf("Running! Hang up with ^C!\n\n");
952 fflush(stdout);
954 bug_on(gettimeofday(&start, NULL));
956 while (likely(sigint == 0)) {
957 #ifdef HAVE_TPACKET3
958 struct block_desc *pbd;
960 while (user_may_pull_from_rx_block((pbd = rx_ring.frames[it].iov_base))) {
961 walk_t3_block(pbd, ctx, sock, &fd, &frame_count);
963 kernel_may_pull_from_rx_block(pbd);
964 it = (it + 1) % rx_ring.layout3.tp_block_nr;
966 if (unlikely(sigint == 1))
967 break;
969 #else
970 while (user_may_pull_from_rx(rx_ring.frames[it].iov_base)) {
971 struct frame_map *hdr = rx_ring.frames[it].iov_base;
972 uint8_t *packet = ((uint8_t *) hdr) + hdr->tp_h.tp_mac;
973 pcap_pkthdr_t phdr;
975 if (ctx->packet_type != -1)
976 if (ctx->packet_type != hdr->s_ll.sll_pkttype)
977 goto next;
979 frame_count++;
981 if (unlikely(ring_frame_size(&rx_ring) < hdr->tp_h.tp_snaplen)) {
982 /* XXX: silently ignore for now. We used to
983 * report them with sock_rx_net_stats() */
984 goto next;
987 if (dump_to_pcap(ctx)) {
988 tpacket_hdr_to_pcap_pkthdr(&hdr->tp_h, &hdr->s_ll, &phdr, ctx->magic);
990 ret = __pcap_io->write_pcap(fd, &phdr, ctx->magic, packet,
991 pcap_get_length(&phdr, ctx->magic));
992 if (unlikely(ret != (int) pcap_get_total_length(&phdr, ctx->magic)))
993 panic("Write error to pcap!\n");
996 show_frame_hdr(packet, hdr->tp_h.tp_snaplen,
997 ctx->link_type, hdr, ctx->print_mode);
999 dissector_entry_point(packet, hdr->tp_h.tp_snaplen,
1000 ctx->link_type, ctx->print_mode);
1002 if (frame_count_max != 0) {
1003 if (unlikely(frame_count >= frame_count_max)) {
1004 sigint = 1;
1005 break;
1009 next:
1010 kernel_may_pull_from_rx(&hdr->tp_h);
1011 it = (it + 1) % rx_ring.layout.tp_frame_nr;
1013 if (unlikely(sigint == 1))
1014 break;
1016 update_pcap_next_dump(ctx, hdr->tp_h.tp_snaplen, &fd, sock);
1018 #endif /* HAVE_TPACKET3 */
1020 ret = poll(&rx_poll, 1, -1);
1021 if (unlikely(ret < 0)) {
1022 if (errno != EINTR)
1023 panic("Poll failed!\n");
1027 bug_on(gettimeofday(&end, NULL));
1028 timersub(&end, &start, &diff);
1030 if (!(ctx->dump_dir && ctx->print_mode == PRINT_NONE)) {
1031 sock_rx_net_stats(sock, frame_count);
1033 printf("\r%12lu sec, %lu usec in total\n",
1034 diff.tv_sec, diff.tv_usec);
1035 } else {
1036 printf("\n\n");
1037 fflush(stdout);
1040 bpf_release(&bpf_ops);
1041 dissector_cleanup_all();
1042 destroy_rx_ring(sock, &rx_ring);
1044 if (ctx->promiscuous)
1045 device_leave_promiscuous_mode(ctx->device_in, ifflags);
1047 if (ctx->rfraw)
1048 leave_rfmon_mac80211(ctx->device_in);
1050 if (dump_to_pcap(ctx)) {
1051 if (ctx->dump_dir)
1052 finish_multi_pcap_file(ctx, fd);
1053 else
1054 finish_single_pcap_file(ctx, fd);
1057 close(sock);
1060 static void init_ctx(struct ctx *ctx)
1062 memset(ctx, 0, sizeof(*ctx));
1063 ctx->uid = getuid();
1064 ctx->uid = getgid();
1066 ctx->cpu = -1;
1067 ctx->packet_type = -1;
1069 ctx->magic = ORIGINAL_TCPDUMP_MAGIC;
1070 ctx->print_mode = PRINT_NORM;
1071 ctx->pcap = PCAP_OPS_SG;
1073 ctx->dump_mode = DUMP_INTERVAL_TIME;
1074 ctx->dump_interval = 60;
1076 ctx->promiscuous = true;
1077 ctx->randomize = false;
1078 ctx->hwtimestamp = true;
1081 static void destroy_ctx(struct ctx *ctx)
1083 free(ctx->device_in);
1084 free(ctx->device_out);
1085 free(ctx->device_trans);
1087 free(ctx->prefix);
1090 static void __noreturn help(void)
1092 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING);
1093 puts("http://www.netsniff-ng.org\n\n"
1094 "Usage: netsniff-ng [options] [filter-expression]\n"
1095 "Options:\n"
1096 " -i|-d|--dev|--in <dev|pcap|-> Input source as netdev, pcap or pcap stdin\n"
1097 " -o|--out <dev|pcap|dir|cfg|-> Output sink as netdev, pcap, directory, trafgen, or stdout\n"
1098 " -f|--filter <bpf-file|expr> Use BPF filter file from bpfc or tcpdump-like expression\n"
1099 " -t|--type <type> Filter for: host|broadcast|multicast|others|outgoing\n"
1100 " -F|--interval <size|time> Dump interval if -o is a dir: <num>KiB/MiB/GiB/s/sec/min/hrs\n"
1101 " -R|--rfraw Capture or inject raw 802.11 frames\n"
1102 " -n|--num <0|uint> Number of packets until exit (def: 0)\n"
1103 " -P|--prefix <name> Prefix for pcaps stored in directory\n"
1104 " -T|--magic <pcap-magic> Pcap magic number/pcap format to store, see -D\n"
1105 " -D|--dump-pcap-types Dump pcap types and magic numbers and quit\n"
1106 " -B|--dump-bpf Dump generated BPF assembly\n"
1107 " -r|--rand Randomize packet forwarding order (dev->dev)\n"
1108 " -M|--no-promisc No promiscuous mode for netdev\n"
1109 " -A|--no-sock-mem Don't tune core socket memory\n"
1110 " -N|--no-hwtimestamp Disable hardware time stamping\n"
1111 " -m|--mmap Mmap(2) pcap file I/O, e.g. for replaying pcaps\n"
1112 " -G|--sg Scatter/gather pcap file I/O\n"
1113 " -c|--clrw Use slower read(2)/write(2) I/O\n"
1114 " -S|--ring-size <size> Specify ring size to: <num>KiB/MiB/GiB\n"
1115 " -k|--kernel-pull <uint> Kernel pull from user interval in us (def: 10us)\n"
1116 " -J|--jumbo-support Support replay/fwd 64KB Super Jumbo Frames (def: 2048B)\n"
1117 " -b|--bind-cpu <cpu> Bind to specific CPU\n"
1118 " -u|--user <userid> Drop privileges and change to userid\n"
1119 " -g|--group <groupid> Drop privileges and change to groupid\n"
1120 " -H|--prio-high Make this high priority process\n"
1121 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
1122 " -s|--silent Do not print captured packets\n"
1123 " -q|--less Print less-verbose packet information\n"
1124 " -X|--hex Print packet data in hex format\n"
1125 " -l|--ascii Print human-readable packet data\n"
1126 " -U|--update Update GeoIP databases\n"
1127 " -V|--verbose Be more verbose\n"
1128 " -v|--version Show version and exit\n"
1129 " -h|--help Guess what?!\n\n"
1130 "Examples:\n"
1131 " netsniff-ng --in eth0 --out dump.pcap -s -T 0xa1b2c3d4 --b 0 tcp or udp\n"
1132 " netsniff-ng --in wlan0 --rfraw --out dump.pcap --silent --bind-cpu 0\n"
1133 " netsniff-ng --in dump.pcap --mmap --out eth0 -k1000 --silent --bind-cpu 0\n"
1134 " netsniff-ng --in dump.pcap --out dump.cfg --silent --bind-cpu 0\n"
1135 " netsniff-ng --in eth0 --out eth1 --silent --bind-cpu 0 -J --type host\n"
1136 " netsniff-ng --in eth1 --out /opt/probe/ -s -m --interval 100MiB -b 0\n"
1137 " netsniff-ng --in vlan0 --out dump.pcap -c -u `id -u bob` -g `id -g bob`\n"
1138 " netsniff-ng --in any --filter http.bpf --jumbo-support --ascii -V\n\n"
1139 "Note:\n"
1140 " For introducing bit errors, delays with random variation and more\n"
1141 " while replaying pcaps, make use of tc(8) with its disciplines (e.g. netem).\n\n");
1142 puts(copyright);
1143 die();
1146 static void __noreturn version(void)
1148 printf("\nnetsniff-ng %s, Git id: %s\n", VERSION_LONG, GITVERSION);
1149 puts("the packet sniffing beast\n"
1150 "http://www.netsniff-ng.org\n\n");
1151 puts(copyright);
1152 die();
1155 int main(int argc, char **argv)
1157 char *ptr;
1158 int c, i, j, cpu_tmp, opt_index, ops_touched = 0, vals[4] = {0};
1159 bool prio_high = false, setsockmem = true;
1160 void (*main_loop)(struct ctx *ctx) = NULL;
1161 struct ctx ctx;
1163 init_ctx(&ctx);
1164 srand(time(NULL));
1166 while ((c = getopt_long(argc, argv, short_options, long_options,
1167 &opt_index)) != EOF) {
1168 switch (c) {
1169 case 'd':
1170 case 'i':
1171 ctx.device_in = xstrdup(optarg);
1172 break;
1173 case 'o':
1174 ctx.device_out = xstrdup(optarg);
1175 break;
1176 case 'P':
1177 ctx.prefix = xstrdup(optarg);
1178 break;
1179 case 'R':
1180 ctx.link_type = LINKTYPE_IEEE802_11;
1181 ctx.rfraw = 1;
1182 break;
1183 case 'r':
1184 ctx.randomize = true;
1185 break;
1186 case 'J':
1187 ctx.jumbo = true;
1188 break;
1189 case 'T':
1190 ctx.magic = (uint32_t) strtoul(optarg, NULL, 0);
1191 pcap_check_magic(ctx.magic);
1192 break;
1193 case 'f':
1194 ctx.filter = xstrdup(optarg);
1195 break;
1196 case 'M':
1197 ctx.promiscuous = false;
1198 break;
1199 case 'N':
1200 ctx.hwtimestamp = false;
1201 break;
1202 case 'A':
1203 setsockmem = false;
1204 break;
1205 case 'u':
1206 ctx.uid = strtoul(optarg, NULL, 0);
1207 ctx.enforce = true;
1208 break;
1209 case 'g':
1210 ctx.gid = strtoul(optarg, NULL, 0);
1211 ctx.enforce = true;
1212 break;
1213 case 't':
1214 if (!strncmp(optarg, "host", strlen("host")))
1215 ctx.packet_type = PACKET_HOST;
1216 else if (!strncmp(optarg, "broadcast", strlen("broadcast")))
1217 ctx.packet_type = PACKET_BROADCAST;
1218 else if (!strncmp(optarg, "multicast", strlen("multicast")))
1219 ctx.packet_type = PACKET_MULTICAST;
1220 else if (!strncmp(optarg, "others", strlen("others")))
1221 ctx.packet_type = PACKET_OTHERHOST;
1222 else if (!strncmp(optarg, "outgoing", strlen("outgoing")))
1223 ctx.packet_type = PACKET_OUTGOING;
1224 else
1225 ctx.packet_type = -1;
1226 break;
1227 case 'S':
1228 ptr = optarg;
1229 for (j = i = strlen(optarg); i > 0; --i) {
1230 if (!isdigit(optarg[j - i]))
1231 break;
1232 ptr++;
1235 if (!strncmp(ptr, "KiB", strlen("KiB")))
1236 ctx.reserve_size = 1 << 10;
1237 else if (!strncmp(ptr, "MiB", strlen("MiB")))
1238 ctx.reserve_size = 1 << 20;
1239 else if (!strncmp(ptr, "GiB", strlen("GiB")))
1240 ctx.reserve_size = 1 << 30;
1241 else
1242 panic("Syntax error in ring size param!\n");
1244 ctx.reserve_size *= strtoul(optarg, NULL, 0);
1245 break;
1246 case 'b':
1247 cpu_tmp = strtol(optarg, NULL, 0);
1249 cpu_affinity(cpu_tmp);
1250 if (ctx.cpu != -2)
1251 ctx.cpu = cpu_tmp;
1252 break;
1253 case 'H':
1254 prio_high = true;
1255 break;
1256 case 'c':
1257 ctx.pcap = PCAP_OPS_RW;
1258 ops_touched = 1;
1259 break;
1260 case 'm':
1261 ctx.pcap = PCAP_OPS_MM;
1262 ops_touched = 1;
1263 break;
1264 case 'G':
1265 ctx.pcap = PCAP_OPS_SG;
1266 ops_touched = 1;
1267 break;
1268 case 'Q':
1269 ctx.cpu = -2;
1270 break;
1271 case 's':
1272 ctx.print_mode = PRINT_NONE;
1273 break;
1274 case 'q':
1275 ctx.print_mode = PRINT_LESS;
1276 break;
1277 case 'X':
1278 ctx.print_mode =
1279 (ctx.print_mode == PRINT_ASCII) ?
1280 PRINT_HEX_ASCII : PRINT_HEX;
1281 break;
1282 case 'l':
1283 ctx.print_mode =
1284 (ctx.print_mode == PRINT_HEX) ?
1285 PRINT_HEX_ASCII : PRINT_ASCII;
1286 break;
1287 case 'k':
1288 ctx.kpull = strtoul(optarg, NULL, 0);
1289 break;
1290 case 'n':
1291 frame_count_max = strtoul(optarg, NULL, 0);
1292 break;
1293 case 'F':
1294 ptr = optarg;
1295 for (j = i = strlen(optarg); i > 0; --i) {
1296 if (!isdigit(optarg[j - i]))
1297 break;
1298 ptr++;
1301 if (!strncmp(ptr, "KiB", strlen("KiB"))) {
1302 ctx.dump_interval = 1 << 10;
1303 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1304 } else if (!strncmp(ptr, "MiB", strlen("MiB"))) {
1305 ctx.dump_interval = 1 << 20;
1306 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1307 } else if (!strncmp(ptr, "GiB", strlen("GiB"))) {
1308 ctx.dump_interval = 1 << 30;
1309 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1310 } else if (!strncmp(ptr, "sec", strlen("sec"))) {
1311 ctx.dump_interval = 1;
1312 ctx.dump_mode = DUMP_INTERVAL_TIME;
1313 } else if (!strncmp(ptr, "min", strlen("min"))) {
1314 ctx.dump_interval = 60;
1315 ctx.dump_mode = DUMP_INTERVAL_TIME;
1316 } else if (!strncmp(ptr, "hrs", strlen("hrs"))) {
1317 ctx.dump_interval = 60 * 60;
1318 ctx.dump_mode = DUMP_INTERVAL_TIME;
1319 } else if (!strncmp(ptr, "s", strlen("s"))) {
1320 ctx.dump_interval = 1;
1321 ctx.dump_mode = DUMP_INTERVAL_TIME;
1322 } else {
1323 panic("Syntax error in time/size param!\n");
1326 ctx.dump_interval *= strtoul(optarg, NULL, 0);
1327 break;
1328 case 'V':
1329 ctx.verbose = true;
1330 break;
1331 case 'B':
1332 ctx.dump_bpf = true;
1333 break;
1334 case 'D':
1335 pcap_dump_type_features();
1336 die();
1337 break;
1338 case 'U':
1339 update_geoip();
1340 die();
1341 break;
1342 case 'v':
1343 version();
1344 break;
1345 case 'h':
1346 help();
1347 break;
1348 case '?':
1349 switch (optopt) {
1350 case 'd':
1351 case 'i':
1352 case 'o':
1353 case 'f':
1354 case 't':
1355 case 'P':
1356 case 'F':
1357 case 'n':
1358 case 'S':
1359 case 'b':
1360 case 'k':
1361 case 'T':
1362 case 'u':
1363 case 'g':
1364 case 'e':
1365 panic("Option -%c requires an argument!\n",
1366 optopt);
1367 default:
1368 if (isprint(optopt))
1369 printf("Unknown option character `0x%X\'!\n", optopt);
1370 die();
1372 default:
1373 break;
1377 if (!ctx.filter && optind != argc) {
1378 int ret;
1379 off_t offset = 0;
1381 for (i = optind; i < argc; ++i) {
1382 size_t alen = strlen(argv[i]) + 2;
1383 size_t flen = ctx.filter ? strlen(ctx.filter) : 0;
1385 ctx.filter = xrealloc(ctx.filter, 1, flen + alen);
1386 ret = slprintf(ctx.filter + offset, strlen(argv[i]) + 2, "%s ", argv[i]);
1387 if (ret < 0)
1388 panic("Cannot concatenate filter string!\n");
1389 else
1390 offset += ret;
1394 if (!ctx.device_in)
1395 ctx.device_in = xstrdup("any");
1397 register_signal(SIGINT, signal_handler);
1398 register_signal(SIGQUIT, signal_handler);
1399 register_signal(SIGTERM, signal_handler);
1400 register_signal(SIGHUP, signal_handler);
1402 tprintf_init();
1404 if (prio_high) {
1405 set_proc_prio(-20);
1406 set_sched_status(SCHED_FIFO, sched_get_priority_max(SCHED_FIFO));
1409 if (device_mtu(ctx.device_in) || !strncmp("any", ctx.device_in, strlen(ctx.device_in))) {
1410 if (!ctx.rfraw)
1411 ctx.link_type = pcap_devtype_to_linktype(ctx.device_in);
1412 if (!ctx.device_out) {
1413 ctx.dump = 0;
1414 main_loop = recv_only_or_dump;
1415 } else if (device_mtu(ctx.device_out)) {
1416 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1417 main_loop = receive_to_xmit;
1418 } else {
1419 ctx.dump = 1;
1420 register_signal_f(SIGALRM, timer_next_dump, SA_SIGINFO);
1421 main_loop = recv_only_or_dump;
1422 if (!ops_touched)
1423 ctx.pcap = PCAP_OPS_SG;
1425 } else {
1426 if (ctx.device_out && device_mtu(ctx.device_out)) {
1427 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1428 main_loop = pcap_to_xmit;
1429 if (!ops_touched)
1430 ctx.pcap = PCAP_OPS_MM;
1431 } else {
1432 main_loop = read_pcap;
1433 if (!ops_touched)
1434 ctx.pcap = PCAP_OPS_SG;
1438 bug_on(!main_loop);
1440 init_geoip(0);
1441 if (setsockmem)
1442 set_system_socket_memory(vals, array_size(vals));
1443 if (!ctx.enforce)
1444 xlockme();
1446 if (ctx.verbose)
1447 printf("pcap file I/O method: %s\n", pcap_ops_group_to_str[ctx.pcap]);
1449 main_loop(&ctx);
1451 if (!ctx.enforce)
1452 xunlockme();
1453 if (setsockmem)
1454 reset_system_socket_memory(vals, array_size(vals));
1455 destroy_geoip();
1457 device_restore_irq_affinity_list();
1458 tprintf_cleanup();
1460 destroy_ctx(&ctx);
1461 return 0;