netsniff-ng/ring: Make verbose flag bool
[netsniff-ng.git] / netsniff-ng.c
blob52ec00f40ef044e59f0ec80c7e3e3e58cb68adf6
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, 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:MJt: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 {"prio-high", no_argument, NULL, 'H'},
92 {"notouch-irq", no_argument, NULL, 'Q'},
93 {"dump-pcap-types", no_argument, NULL, 'D'},
94 {"dump-bpf", no_argument, NULL, 'B'},
95 {"silent", no_argument, NULL, 's'},
96 {"less", no_argument, NULL, 'q'},
97 {"hex", no_argument, NULL, 'X'},
98 {"ascii", no_argument, NULL, 'l'},
99 {"no-sock-mem", no_argument, NULL, 'A'},
100 {"update", no_argument, NULL, 'U'},
101 {"verbose", no_argument, NULL, 'V'},
102 {"version", no_argument, NULL, 'v'},
103 {"help", no_argument, NULL, 'h'},
104 {NULL, 0, NULL, 0}
107 static int tx_sock;
108 static struct itimerval itimer;
109 static unsigned long frame_count_max = 0, interval = TX_KERNEL_PULL_INT;
111 #define __pcap_io pcap_ops[ctx->pcap]
113 static void signal_handler(int number)
115 switch (number) {
116 case SIGINT:
117 case SIGQUIT:
118 case SIGTERM:
119 sigint = 1;
120 case SIGHUP:
121 default:
122 break;
126 static void timer_elapsed(int unused __maybe_unused)
128 int ret;
130 set_itimer_interval_value(&itimer, 0, interval);
132 ret = pull_and_flush_tx_ring(tx_sock);
133 if (unlikely(ret < 0)) {
134 /* We could hit EBADF if the socket has been closed before
135 * the timer was triggered.
137 if (errno != EBADF && errno != ENOBUFS)
138 panic("Flushing TX_RING failed: %s!\n", strerror(errno));
141 setitimer(ITIMER_REAL, &itimer, NULL);
144 static void timer_purge(void)
146 int ret;
148 ret = pull_and_flush_tx_ring_wait(tx_sock);
149 if (unlikely(ret < 0)) {
150 if (errno != EBADF && errno != ENOBUFS)
151 panic("Flushing TX_RING failed: %s!\n", strerror(errno));
154 set_itimer_interval_value(&itimer, 0, 0);
155 setitimer(ITIMER_REAL, &itimer, NULL);
158 static void timer_next_dump(int unused __maybe_unused)
160 set_itimer_interval_value(&itimer, interval, 0);
161 next_dump = true;
162 setitimer(ITIMER_REAL, &itimer, NULL);
165 static inline bool dump_to_pcap(struct ctx *ctx)
167 return ctx->dump;
170 static void pcap_to_xmit(struct ctx *ctx)
172 uint8_t *out = NULL;
173 int irq, ifindex, fd = 0, ret;
174 size_t size;
175 unsigned int it = 0;
176 unsigned long trunced = 0;
177 struct ring tx_ring;
178 struct frame_map *hdr;
179 struct sock_fprog bpf_ops;
180 struct timeval start, end, diff;
181 pcap_pkthdr_t phdr;
183 if (!device_up_and_running(ctx->device_out) && !ctx->rfraw)
184 panic("Device not up and running!\n");
186 bug_on(!__pcap_io);
188 tx_sock = pf_socket();
190 if (!strncmp("-", ctx->device_in, strlen("-"))) {
191 fd = dup_or_die(fileno(stdin));
192 close(fileno(stdin));
193 if (ctx->pcap == PCAP_OPS_MM)
194 ctx->pcap = PCAP_OPS_SG;
195 } else {
196 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
199 if (__pcap_io->init_once_pcap)
200 __pcap_io->init_once_pcap();
202 ret = __pcap_io->pull_fhdr_pcap(fd, &ctx->magic, &ctx->link_type);
203 if (ret)
204 panic("Error reading pcap header!\n");
206 if (__pcap_io->prepare_access_pcap) {
207 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_RD, ctx->jumbo);
208 if (ret)
209 panic("Error prepare reading pcap!\n");
212 fmemset(&tx_ring, 0, sizeof(tx_ring));
213 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
215 if (ctx->rfraw) {
216 ctx->device_trans = xstrdup(ctx->device_out);
217 xfree(ctx->device_out);
219 enter_rfmon_mac80211(ctx->device_trans, &ctx->device_out);
220 if (ctx->link_type != LINKTYPE_IEEE802_11)
221 panic("Wrong linktype of pcap!\n");
224 ifindex = device_ifindex(ctx->device_out);
226 size = ring_size(ctx->device_out, ctx->reserve_size);
228 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
229 if (ctx->dump_bpf)
230 bpf_dump_all(&bpf_ops);
232 set_packet_loss_discard(tx_sock);
234 setup_tx_ring_layout(tx_sock, &tx_ring, size, ctx->jumbo);
235 create_tx_ring(tx_sock, &tx_ring, ctx->verbose);
236 mmap_tx_ring(tx_sock, &tx_ring);
237 alloc_tx_ring_frames(tx_sock, &tx_ring);
238 bind_tx_ring(tx_sock, &tx_ring, ifindex);
240 dissector_init_all(ctx->print_mode);
242 if (ctx->cpu >= 0 && ifindex > 0) {
243 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(hdr, ctx->print_mode);
292 dissector_entry_point(out, hdr->tp_h.tp_snaplen,
293 ctx->link_type, ctx->print_mode);
295 kernel_may_pull_from_tx(&hdr->tp_h);
297 it++;
298 if (it >= tx_ring.layout.tp_frame_nr)
299 it = 0;
301 if (unlikely(sigint == 1))
302 break;
304 if (frame_count_max != 0) {
305 if (ctx->tx_packets >= frame_count_max) {
306 sigint = 1;
307 break;
313 out:
314 bug_on(gettimeofday(&end, NULL));
315 timersub(&end, &start, &diff);
317 timer_purge();
319 bpf_release(&bpf_ops);
321 dissector_cleanup_all();
322 destroy_tx_ring(tx_sock, &tx_ring);
324 if (ctx->rfraw)
325 leave_rfmon_mac80211(ctx->device_out);
327 if (__pcap_io->prepare_close_pcap)
328 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
330 if (!strncmp("-", ctx->device_in, strlen("-")))
331 dup2(fd, fileno(stdin));
332 close(fd);
334 close(tx_sock);
336 fflush(stdout);
337 printf("\n");
338 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
339 printf("\r%12lu packets truncated in file\n", trunced);
340 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
341 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
344 static void receive_to_xmit(struct ctx *ctx)
346 short ifflags = 0;
347 uint8_t *in, *out;
348 int rx_sock, ifindex_in, ifindex_out, ret;
349 size_t size_in, size_out;
350 unsigned int it_in = 0, it_out = 0;
351 unsigned long frame_count = 0;
352 struct frame_map *hdr_in, *hdr_out;
353 struct ring tx_ring, rx_ring;
354 struct pollfd rx_poll;
355 struct sock_fprog bpf_ops;
357 if (!strncmp(ctx->device_in, ctx->device_out, IFNAMSIZ))
358 panic("Ingress/egress devices must be different!\n");
359 if (!device_up_and_running(ctx->device_out))
360 panic("Egress device not up and running!\n");
362 rx_sock = pf_socket();
363 tx_sock = pf_socket();
365 fmemset(&tx_ring, 0, sizeof(tx_ring));
366 fmemset(&rx_ring, 0, sizeof(rx_ring));
367 fmemset(&rx_poll, 0, sizeof(rx_poll));
368 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
370 ifindex_in = device_ifindex(ctx->device_in);
371 ifindex_out = device_ifindex(ctx->device_out);
373 size_in = ring_size(ctx->device_in, ctx->reserve_size);
374 size_out = ring_size(ctx->device_out, ctx->reserve_size);
376 enable_kernel_bpf_jit_compiler();
378 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
379 if (ctx->dump_bpf)
380 bpf_dump_all(&bpf_ops);
381 bpf_attach_to_sock(rx_sock, &bpf_ops);
383 setup_rx_ring_layout(rx_sock, &rx_ring, size_in, ctx->jumbo, false);
384 create_rx_ring(rx_sock, &rx_ring, ctx->verbose);
385 mmap_rx_ring(rx_sock, &rx_ring);
386 alloc_rx_ring_frames(rx_sock, &rx_ring);
387 bind_rx_ring(rx_sock, &rx_ring, ifindex_in);
388 prepare_polling(rx_sock, &rx_poll);
390 set_packet_loss_discard(tx_sock);
391 setup_tx_ring_layout(tx_sock, &tx_ring, size_out, ctx->jumbo);
392 create_tx_ring(tx_sock, &tx_ring, ctx->verbose);
393 mmap_tx_ring(tx_sock, &tx_ring);
394 alloc_tx_ring_frames(tx_sock, &tx_ring);
395 bind_tx_ring(tx_sock, &tx_ring, ifindex_out);
397 dissector_init_all(ctx->print_mode);
399 if (ctx->promiscuous)
400 ifflags = device_enter_promiscuous_mode(ctx->device_in);
402 if (ctx->kpull)
403 interval = ctx->kpull;
405 set_itimer_interval_value(&itimer, 0, interval);
406 setitimer(ITIMER_REAL, &itimer, NULL);
408 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
410 printf("Running! Hang up with ^C!\n\n");
411 fflush(stdout);
413 while (likely(sigint == 0)) {
414 while (user_may_pull_from_rx(rx_ring.frames[it_in].iov_base)) {
415 hdr_in = rx_ring.frames[it_in].iov_base;
416 in = ((uint8_t *) hdr_in) + hdr_in->tp_h.tp_mac;
418 frame_count++;
420 if (ctx->packet_type != -1)
421 if (ctx->packet_type != hdr_in->s_ll.sll_pkttype)
422 goto next;
424 hdr_out = tx_ring.frames[it_out].iov_base;
425 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
427 while (!user_may_pull_from_tx(tx_ring.frames[it_out].iov_base) &&
428 likely(!sigint)) {
429 if (ctx->randomize)
430 next_rnd_slot(&it_out, &tx_ring);
431 else {
432 it_out++;
433 if (it_out >= tx_ring.layout.tp_frame_nr)
434 it_out = 0;
437 hdr_out = tx_ring.frames[it_out].iov_base;
438 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
441 tpacket_hdr_clone(&hdr_out->tp_h, &hdr_in->tp_h);
442 fmemcpy(out, in, hdr_in->tp_h.tp_len);
444 kernel_may_pull_from_tx(&hdr_out->tp_h);
445 if (ctx->randomize)
446 next_rnd_slot(&it_out, &tx_ring);
447 else {
448 it_out++;
449 if (it_out >= tx_ring.layout.tp_frame_nr)
450 it_out = 0;
453 show_frame_hdr(hdr_in, ctx->print_mode);
455 dissector_entry_point(in, hdr_in->tp_h.tp_snaplen,
456 ctx->link_type, ctx->print_mode);
458 if (frame_count_max != 0) {
459 if (frame_count >= frame_count_max) {
460 sigint = 1;
461 break;
465 next:
466 kernel_may_pull_from_rx(&hdr_in->tp_h);
468 it_in++;
469 if (it_in >= rx_ring.layout.tp_frame_nr)
470 it_in = 0;
472 if (unlikely(sigint == 1))
473 goto out;
476 ret = poll(&rx_poll, 1, -1);
477 if (unlikely(ret < 0)) {
478 if (errno != EINTR)
479 panic("Poll failed!\n");
483 out:
484 timer_purge();
486 sock_rx_net_stats(rx_sock, 0);
488 bpf_release(&bpf_ops);
490 dissector_cleanup_all();
492 destroy_tx_ring(tx_sock, &tx_ring);
493 destroy_rx_ring(rx_sock, &rx_ring);
495 if (ctx->promiscuous)
496 device_leave_promiscuous_mode(ctx->device_in, ifflags);
498 close(tx_sock);
499 close(rx_sock);
502 static void translate_pcap_to_txf(int fdo, uint8_t *out, size_t len)
504 size_t bytes_done = 0;
505 char bout[80];
507 slprintf(bout, sizeof(bout), "{\n ");
508 write_or_die(fdo, bout, strlen(bout));
510 while (bytes_done < len) {
511 slprintf(bout, sizeof(bout), "0x%02x, ", out[bytes_done]);
512 write_or_die(fdo, bout, strlen(bout));
514 bytes_done++;
516 if (bytes_done % 10 == 0) {
517 slprintf(bout, sizeof(bout), "\n");
518 write_or_die(fdo, bout, strlen(bout));
520 if (bytes_done < len) {
521 slprintf(bout, sizeof(bout), " ");
522 write_or_die(fdo, bout, strlen(bout));
526 if (bytes_done % 10 != 0) {
527 slprintf(bout, sizeof(bout), "\n");
528 write_or_die(fdo, bout, strlen(bout));
531 slprintf(bout, sizeof(bout), "}\n\n");
532 write_or_die(fdo, bout, strlen(bout));
535 static void read_pcap(struct ctx *ctx)
537 uint8_t *out;
538 int ret, fd, fdo = 0;
539 unsigned long trunced = 0;
540 size_t out_len;
541 pcap_pkthdr_t phdr;
542 struct sock_fprog bpf_ops;
543 struct frame_map fm;
544 struct timeval start, end, diff;
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));
571 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
573 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
574 if (ctx->dump_bpf)
575 bpf_dump_all(&bpf_ops);
577 dissector_init_all(ctx->print_mode);
579 out_len = round_up(1024 * 1024, RUNTIME_PAGE_SIZE);
580 out = xmalloc_aligned(out_len, CO_CACHE_LINE_SIZE);
582 if (ctx->device_out) {
583 if (!strncmp("-", ctx->device_out, strlen("-"))) {
584 fdo = dup_or_die(fileno(stdout));
585 close(fileno(stdout));
586 } else {
587 fdo = open_or_die_m(ctx->device_out, O_RDWR | O_CREAT |
588 O_TRUNC | O_LARGEFILE, DEFFILEMODE);
592 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
594 printf("Running! Hang up with ^C!\n\n");
595 fflush(stdout);
597 bug_on(gettimeofday(&start, NULL));
599 while (likely(sigint == 0)) {
600 do {
601 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic,
602 out, out_len);
603 if (unlikely(ret < 0))
604 goto out;
606 if (unlikely(pcap_get_length(&phdr, ctx->magic) == 0)) {
607 trunced++;
608 continue;
611 if (unlikely(pcap_get_length(&phdr, ctx->magic) > out_len)) {
612 pcap_set_length(&phdr, ctx->magic, out_len);
613 trunced++;
615 } while (ctx->filter &&
616 !bpf_run_filter(&bpf_ops, out,
617 pcap_get_length(&phdr, ctx->magic)));
619 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &fm.tp_h, &fm.s_ll);
621 ctx->tx_bytes += fm.tp_h.tp_len;
622 ctx->tx_packets++;
624 show_frame_hdr(&fm, ctx->print_mode);
626 dissector_entry_point(out, fm.tp_h.tp_snaplen,
627 ctx->link_type, ctx->print_mode);
629 if (ctx->device_out)
630 translate_pcap_to_txf(fdo, out, fm.tp_h.tp_snaplen);
632 if (frame_count_max != 0) {
633 if (ctx->tx_packets >= frame_count_max) {
634 sigint = 1;
635 break;
640 out:
642 bug_on(gettimeofday(&end, NULL));
643 timersub(&end, &start, &diff);
645 bpf_release(&bpf_ops);
647 dissector_cleanup_all();
649 if (__pcap_io->prepare_close_pcap)
650 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
652 xfree(out);
654 fflush(stdout);
655 printf("\n");
656 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
657 printf("\r%12lu packets truncated in file\n", trunced);
658 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
659 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
661 if (!strncmp("-", ctx->device_in, strlen("-")))
662 dup2(fd, fileno(stdin));
663 close(fd);
665 if (ctx->device_out) {
666 if (!strncmp("-", ctx->device_out, strlen("-")))
667 dup2(fdo, fileno(stdout));
668 close(fdo);
672 static void finish_multi_pcap_file(struct ctx *ctx, int fd)
674 __pcap_io->fsync_pcap(fd);
676 if (__pcap_io->prepare_close_pcap)
677 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
679 close(fd);
681 fmemset(&itimer, 0, sizeof(itimer));
682 setitimer(ITIMER_REAL, &itimer, NULL);
685 static int next_multi_pcap_file(struct ctx *ctx, int fd)
687 int ret;
688 char fname[512];
690 __pcap_io->fsync_pcap(fd);
692 if (__pcap_io->prepare_close_pcap)
693 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
695 close(fd);
697 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
698 ctx->prefix ? : "dump-", time(NULL));
700 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
701 O_LARGEFILE, DEFFILEMODE);
703 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
704 if (ret)
705 panic("Error writing pcap header!\n");
707 if (__pcap_io->prepare_access_pcap) {
708 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, true);
709 if (ret)
710 panic("Error prepare writing pcap!\n");
713 return fd;
716 static int begin_multi_pcap_file(struct ctx *ctx)
718 int fd, ret;
719 char fname[256];
721 bug_on(!__pcap_io);
723 if (ctx->device_out[strlen(ctx->device_out) - 1] == '/')
724 ctx->device_out[strlen(ctx->device_out) - 1] = 0;
726 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
727 ctx->prefix ? : "dump-", time(NULL));
729 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
730 O_LARGEFILE, DEFFILEMODE);
732 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
733 if (ret)
734 panic("Error writing pcap header!\n");
736 if (__pcap_io->prepare_access_pcap) {
737 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, true);
738 if (ret)
739 panic("Error prepare writing pcap!\n");
742 if (ctx->dump_mode == DUMP_INTERVAL_TIME) {
743 interval = ctx->dump_interval;
745 set_itimer_interval_value(&itimer, interval, 0);
746 setitimer(ITIMER_REAL, &itimer, NULL);
747 } else {
748 interval = 0;
751 return fd;
754 static void finish_single_pcap_file(struct ctx *ctx, int fd)
756 __pcap_io->fsync_pcap(fd);
758 if (__pcap_io->prepare_close_pcap)
759 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
761 if (strncmp("-", ctx->device_out, strlen("-")))
762 close(fd);
763 else
764 dup2(fd, fileno(stdout));
767 static int begin_single_pcap_file(struct ctx *ctx)
769 int fd, ret;
771 bug_on(!__pcap_io);
773 if (!strncmp("-", ctx->device_out, strlen("-"))) {
774 fd = dup_or_die(fileno(stdout));
775 close(fileno(stdout));
776 if (ctx->pcap == PCAP_OPS_MM)
777 ctx->pcap = PCAP_OPS_SG;
778 } else {
779 fd = open_or_die_m(ctx->device_out,
780 O_RDWR | O_CREAT | O_TRUNC |
781 O_LARGEFILE, DEFFILEMODE);
784 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
785 if (ret)
786 panic("Error writing pcap header!\n");
788 if (__pcap_io->prepare_access_pcap) {
789 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, true);
790 if (ret)
791 panic("Error prepare writing pcap!\n");
794 return fd;
797 static void print_pcap_file_stats(int sock, struct ctx *ctx)
799 int ret;
800 struct tpacket_stats kstats;
801 socklen_t slen = sizeof(kstats);
803 fmemset(&kstats, 0, sizeof(kstats));
805 ret = getsockopt(sock, SOL_PACKET, PACKET_STATISTICS, &kstats, &slen);
806 if (unlikely(ret))
807 panic("Cannot get packet statistics!\n");
809 if (ctx->print_mode == PRINT_NONE) {
810 printf(".(+%u/-%u)", kstats.tp_packets - kstats.tp_drops,
811 kstats.tp_drops);
812 fflush(stdout);
816 static void walk_t3_block(struct block_desc *pbd, struct ctx *ctx,
817 int sock, int *fd, unsigned long *frame_count)
819 uint8_t *packet;
820 int num_pkts = pbd->h1.num_pkts, i, ret;
821 struct tpacket3_hdr *hdr;
822 pcap_pkthdr_t phdr;
823 struct sockaddr_ll *sll;
825 hdr = (void *) ((uint8_t *) pbd + pbd->h1.offset_to_first_pkt);
826 sll = (void *) ((uint8_t *) hdr + TPACKET_ALIGN(sizeof(*hdr)));
828 for (i = 0; i < num_pkts && likely(sigint == 0); ++i) {
829 packet = ((uint8_t *) hdr + hdr->tp_mac);
831 if (ctx->packet_type != -1)
832 if (ctx->packet_type != sll->sll_pkttype)
833 goto next;
835 (*frame_count)++;
837 if (dump_to_pcap(ctx)) {
838 tpacket3_hdr_to_pcap_pkthdr(hdr, sll, &phdr, ctx->magic);
840 ret = __pcap_io->write_pcap(*fd, &phdr, ctx->magic, packet,
841 pcap_get_length(&phdr, ctx->magic));
842 if (unlikely(ret != (int) pcap_get_total_length(&phdr, ctx->magic)))
843 panic("Write error to pcap!\n");
846 __show_frame_hdr(sll, hdr, ctx->print_mode, true);
848 dissector_entry_point(packet, hdr->tp_snaplen, ctx->link_type,
849 ctx->print_mode);
850 next:
851 hdr = (void *) ((uint8_t *) hdr + hdr->tp_next_offset);
852 sll = (void *) ((uint8_t *) hdr + TPACKET_ALIGN(sizeof(*hdr)));
854 if (frame_count_max != 0) {
855 if (unlikely(*frame_count >= frame_count_max)) {
856 sigint = 1;
857 break;
861 if (dump_to_pcap(ctx)) {
862 if (ctx->dump_mode == DUMP_INTERVAL_SIZE) {
863 interval += hdr->tp_snaplen;
864 if (interval > ctx->dump_interval) {
865 next_dump = true;
866 interval = 0;
870 if (next_dump) {
871 *fd = next_multi_pcap_file(ctx, *fd);
872 next_dump = false;
874 if (unlikely(ctx->verbose))
875 print_pcap_file_stats(sock, ctx);
881 static void recv_only_or_dump(struct ctx *ctx)
883 short ifflags = 0;
884 int sock, irq, ifindex, fd = 0, ret;
885 size_t size;
886 unsigned int it = 0;
887 struct ring rx_ring;
888 struct pollfd rx_poll;
889 struct sock_fprog bpf_ops;
890 struct timeval start, end, diff;
891 struct block_desc *pbd;
892 unsigned long frame_count = 0;
894 sock = pf_socket();
896 if (ctx->rfraw) {
897 ctx->device_trans = xstrdup(ctx->device_in);
898 xfree(ctx->device_in);
900 enter_rfmon_mac80211(ctx->device_trans, &ctx->device_in);
901 ctx->link_type = LINKTYPE_IEEE802_11;
904 fmemset(&rx_ring, 0, sizeof(rx_ring));
905 fmemset(&rx_poll, 0, sizeof(rx_poll));
906 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
908 ifindex = device_ifindex(ctx->device_in);
910 size = ring_size(ctx->device_in, ctx->reserve_size);
912 enable_kernel_bpf_jit_compiler();
914 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
915 if (ctx->dump_bpf)
916 bpf_dump_all(&bpf_ops);
917 bpf_attach_to_sock(sock, &bpf_ops);
919 ret = set_sockopt_hwtimestamp(sock, ctx->device_in);
920 if (ret == 0 && ctx->verbose)
921 printf("HW timestamping enabled\n");
923 setup_rx_ring_layout(sock, &rx_ring, size, true, true);
924 create_rx_ring(sock, &rx_ring, ctx->verbose);
925 mmap_rx_ring(sock, &rx_ring);
926 alloc_rx_ring_frames(sock, &rx_ring);
927 bind_rx_ring(sock, &rx_ring, ifindex);
929 prepare_polling(sock, &rx_poll);
930 dissector_init_all(ctx->print_mode);
932 if (ctx->cpu >= 0 && ifindex > 0) {
933 irq = device_irq_number(ctx->device_in);
934 device_set_irq_affinity(irq, ctx->cpu);
936 if (ctx->verbose)
937 printf("IRQ: %s:%d > CPU%d\n",
938 ctx->device_in, irq, ctx->cpu);
941 if (ctx->promiscuous)
942 ifflags = device_enter_promiscuous_mode(ctx->device_in);
944 if (dump_to_pcap(ctx) && __pcap_io->init_once_pcap)
945 __pcap_io->init_once_pcap();
947 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
949 if (dump_to_pcap(ctx)) {
950 struct stat stats;
952 ret = stat(ctx->device_out, &stats);
953 if (ret < 0)
954 ctx->dump_dir = 0;
955 else
956 ctx->dump_dir = S_ISDIR(stats.st_mode);
958 if (ctx->dump_dir)
959 fd = begin_multi_pcap_file(ctx);
960 else
961 fd = begin_single_pcap_file(ctx);
964 printf("Running! Hang up with ^C!\n\n");
965 fflush(stdout);
967 bug_on(gettimeofday(&start, NULL));
969 while (likely(sigint == 0)) {
970 while (user_may_pull_from_rx_block((pbd = (void *)
971 rx_ring.frames[it].iov_base))) {
972 walk_t3_block(pbd, ctx, sock, &fd, &frame_count);
974 kernel_may_pull_from_rx_block(pbd);
975 it = (it + 1) % rx_ring.layout3.tp_block_nr;
977 if (unlikely(sigint == 1))
978 break;
981 ret = poll(&rx_poll, 1, -1);
982 if (unlikely(ret < 0)) {
983 if (errno != EINTR)
984 panic("Poll failed!\n");
988 bug_on(gettimeofday(&end, NULL));
989 timersub(&end, &start, &diff);
991 if (!(ctx->dump_dir && ctx->print_mode == PRINT_NONE)) {
992 sock_rx_net_stats(sock, frame_count);
994 printf("\r%12lu sec, %lu usec in total\n",
995 diff.tv_sec, diff.tv_usec);
996 } else {
997 printf("\n\n");
998 fflush(stdout);
1001 bpf_release(&bpf_ops);
1002 dissector_cleanup_all();
1003 destroy_rx_ring(sock, &rx_ring);
1005 if (ctx->promiscuous)
1006 device_leave_promiscuous_mode(ctx->device_in, ifflags);
1008 if (ctx->rfraw)
1009 leave_rfmon_mac80211(ctx->device_in);
1011 if (dump_to_pcap(ctx)) {
1012 if (ctx->dump_dir)
1013 finish_multi_pcap_file(ctx, fd);
1014 else
1015 finish_single_pcap_file(ctx, fd);
1018 close(sock);
1021 static void init_ctx(struct ctx *ctx)
1023 memset(ctx, 0, sizeof(*ctx));
1024 ctx->uid = getuid();
1025 ctx->uid = getgid();
1027 ctx->cpu = -1;
1028 ctx->packet_type = -1;
1030 ctx->magic = ORIGINAL_TCPDUMP_MAGIC;
1031 ctx->print_mode = PRINT_NORM;
1032 ctx->pcap = PCAP_OPS_SG;
1034 ctx->dump_mode = DUMP_INTERVAL_TIME;
1035 ctx->dump_interval = 60;
1037 ctx->promiscuous = true;
1038 ctx->randomize = false;
1041 static void destroy_ctx(struct ctx *ctx)
1043 free(ctx->device_in);
1044 free(ctx->device_out);
1045 free(ctx->device_trans);
1047 free(ctx->prefix);
1050 static void __noreturn help(void)
1052 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING);
1053 puts("http://www.netsniff-ng.org\n\n"
1054 "Usage: netsniff-ng [options] [filter-expression]\n"
1055 "Options:\n"
1056 " -i|-d|--dev|--in <dev|pcap|-> Input source as netdev, pcap or pcap stdin\n"
1057 " -o|--out <dev|pcap|dir|cfg|-> Output sink as netdev, pcap, directory, trafgen, or stdout\n"
1058 " -f|--filter <bpf-file|expr> Use BPF filter file from bpfc or tcpdump-like expression\n"
1059 " -t|--type <type> Filter for: host|broadcast|multicast|others|outgoing\n"
1060 " -F|--interval <size|time> Dump interval if -o is a dir: <num>KiB/MiB/GiB/s/sec/min/hrs\n"
1061 " -R|--rfraw Capture or inject raw 802.11 frames\n"
1062 " -n|--num <0|uint> Number of packets until exit (def: 0)\n"
1063 " -P|--prefix <name> Prefix for pcaps stored in directory\n"
1064 " -T|--magic <pcap-magic> Pcap magic number/pcap format to store, see -D\n"
1065 " -D|--dump-pcap-types Dump pcap types and magic numbers and quit\n"
1066 " -B|--dump-bpf Dump generated BPF assembly\n"
1067 " -r|--rand Randomize packet forwarding order (dev->dev)\n"
1068 " -M|--no-promisc No promiscuous mode for netdev\n"
1069 " -A|--no-sock-mem Don't tune core socket memory\n"
1070 " -m|--mmap Mmap(2) pcap file I/O, e.g. for replaying pcaps\n"
1071 " -G|--sg Scatter/gather pcap file I/O\n"
1072 " -c|--clrw Use slower read(2)/write(2) I/O\n"
1073 " -S|--ring-size <size> Specify ring size to: <num>KiB/MiB/GiB\n"
1074 " -k|--kernel-pull <uint> Kernel pull from user interval in us (def: 10us)\n"
1075 " -J|--jumbo-support Support replay/fwd 64KB Super Jumbo Frames (def: 2048B)\n"
1076 " -b|--bind-cpu <cpu> Bind to specific CPU\n"
1077 " -u|--user <userid> Drop privileges and change to userid\n"
1078 " -g|--group <groupid> Drop privileges and change to groupid\n"
1079 " -H|--prio-high Make this high priority process\n"
1080 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
1081 " -s|--silent Do not print captured packets\n"
1082 " -q|--less Print less-verbose packet information\n"
1083 " -X|--hex Print packet data in hex format\n"
1084 " -l|--ascii Print human-readable packet data\n"
1085 " -U|--update Update GeoIP databases\n"
1086 " -V|--verbose Be more verbose\n"
1087 " -v|--version Show version and exit\n"
1088 " -h|--help Guess what?!\n\n"
1089 "Examples:\n"
1090 " netsniff-ng --in eth0 --out dump.pcap -s -T 0xa1b2c3d4 --b 0 tcp or udp\n"
1091 " netsniff-ng --in wlan0 --rfraw --out dump.pcap --silent --bind-cpu 0\n"
1092 " netsniff-ng --in dump.pcap --mmap --out eth0 -k1000 --silent --bind-cpu 0\n"
1093 " netsniff-ng --in dump.pcap --out dump.cfg --silent --bind-cpu 0\n"
1094 " netsniff-ng --in eth0 --out eth1 --silent --bind-cpu 0 -J --type host\n"
1095 " netsniff-ng --in eth1 --out /opt/probe/ -s -m --interval 100MiB -b 0\n"
1096 " netsniff-ng --in vlan0 --out dump.pcap -c -u `id -u bob` -g `id -g bob`\n"
1097 " netsniff-ng --in any --filter http.bpf --jumbo-support --ascii -V\n\n"
1098 "Note:\n"
1099 " For introducing bit errors, delays with random variation and more\n"
1100 " while replaying pcaps, make use of tc(8) with its disciplines (e.g. netem).\n\n"
1101 "Please report bugs to <bugs@netsniff-ng.org>\n"
1102 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1103 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1104 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1105 "Swiss federal institute of technology (ETH Zurich)\n"
1106 "License: GNU GPL version 2.0\n"
1107 "This is free software: you are free to change and redistribute it.\n"
1108 "There is NO WARRANTY, to the extent permitted by law.\n");
1109 die();
1112 static void __noreturn version(void)
1114 printf("\nnetsniff-ng %s, Git id: %s\n", VERSION_LONG, GITVERSION);
1115 puts("the packet sniffing beast\n"
1116 "http://www.netsniff-ng.org\n\n"
1117 "Please report bugs to <bugs@netsniff-ng.org>\n"
1118 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1119 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1120 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1121 "Swiss federal institute of technology (ETH Zurich)\n"
1122 "License: GNU GPL version 2.0\n"
1123 "This is free software: you are free to change and redistribute it.\n"
1124 "There is NO WARRANTY, to the extent permitted by law.\n");
1125 die();
1128 int main(int argc, char **argv)
1130 char *ptr;
1131 int c, i, j, cpu_tmp, opt_index, ops_touched = 0, vals[4] = {0};
1132 bool prio_high = false, setsockmem = true;
1133 void (*main_loop)(struct ctx *ctx) = NULL;
1134 struct ctx ctx;
1136 init_ctx(&ctx);
1137 srand(time(NULL));
1139 while ((c = getopt_long(argc, argv, short_options, long_options,
1140 &opt_index)) != EOF) {
1141 switch (c) {
1142 case 'd':
1143 case 'i':
1144 ctx.device_in = xstrdup(optarg);
1145 break;
1146 case 'o':
1147 ctx.device_out = xstrdup(optarg);
1148 break;
1149 case 'P':
1150 ctx.prefix = xstrdup(optarg);
1151 break;
1152 case 'R':
1153 ctx.link_type = LINKTYPE_IEEE802_11;
1154 ctx.rfraw = 1;
1155 break;
1156 case 'r':
1157 ctx.randomize = true;
1158 break;
1159 case 'J':
1160 ctx.jumbo = true;
1161 break;
1162 case 'T':
1163 ctx.magic = (uint32_t) strtoul(optarg, NULL, 0);
1164 pcap_check_magic(ctx.magic);
1165 break;
1166 case 'f':
1167 ctx.filter = xstrdup(optarg);
1168 break;
1169 case 'M':
1170 ctx.promiscuous = false;
1171 break;
1172 case 'A':
1173 setsockmem = false;
1174 break;
1175 case 'u':
1176 ctx.uid = strtoul(optarg, NULL, 0);
1177 ctx.enforce = true;
1178 break;
1179 case 'g':
1180 ctx.gid = strtoul(optarg, NULL, 0);
1181 ctx.enforce = true;
1182 break;
1183 case 't':
1184 if (!strncmp(optarg, "host", strlen("host")))
1185 ctx.packet_type = PACKET_HOST;
1186 else if (!strncmp(optarg, "broadcast", strlen("broadcast")))
1187 ctx.packet_type = PACKET_BROADCAST;
1188 else if (!strncmp(optarg, "multicast", strlen("multicast")))
1189 ctx.packet_type = PACKET_MULTICAST;
1190 else if (!strncmp(optarg, "others", strlen("others")))
1191 ctx.packet_type = PACKET_OTHERHOST;
1192 else if (!strncmp(optarg, "outgoing", strlen("outgoing")))
1193 ctx.packet_type = PACKET_OUTGOING;
1194 else
1195 ctx.packet_type = -1;
1196 break;
1197 case 'S':
1198 ptr = optarg;
1199 ctx.reserve_size = 0;
1201 for (j = i = strlen(optarg); i > 0; --i) {
1202 if (!isdigit(optarg[j - i]))
1203 break;
1204 ptr++;
1207 if (!strncmp(ptr, "KiB", strlen("KiB")))
1208 ctx.reserve_size = 1 << 10;
1209 else if (!strncmp(ptr, "MiB", strlen("MiB")))
1210 ctx.reserve_size = 1 << 20;
1211 else if (!strncmp(ptr, "GiB", strlen("GiB")))
1212 ctx.reserve_size = 1 << 30;
1213 else
1214 panic("Syntax error in ring size param!\n");
1216 ctx.reserve_size *= strtoul(optarg, NULL, 0);
1217 break;
1218 case 'b':
1219 cpu_tmp = strtol(optarg, NULL, 0);
1221 cpu_affinity(cpu_tmp);
1222 if (ctx.cpu != -2)
1223 ctx.cpu = cpu_tmp;
1224 break;
1225 case 'H':
1226 prio_high = true;
1227 break;
1228 case 'c':
1229 ctx.pcap = PCAP_OPS_RW;
1230 ops_touched = 1;
1231 break;
1232 case 'm':
1233 ctx.pcap = PCAP_OPS_MM;
1234 ops_touched = 1;
1235 break;
1236 case 'G':
1237 ctx.pcap = PCAP_OPS_SG;
1238 ops_touched = 1;
1239 break;
1240 case 'Q':
1241 ctx.cpu = -2;
1242 break;
1243 case 's':
1244 ctx.print_mode = PRINT_NONE;
1245 break;
1246 case 'q':
1247 ctx.print_mode = PRINT_LESS;
1248 break;
1249 case 'X':
1250 ctx.print_mode =
1251 (ctx.print_mode == PRINT_ASCII) ?
1252 PRINT_HEX_ASCII : PRINT_HEX;
1253 break;
1254 case 'l':
1255 ctx.print_mode =
1256 (ctx.print_mode == PRINT_HEX) ?
1257 PRINT_HEX_ASCII : PRINT_ASCII;
1258 break;
1259 case 'k':
1260 ctx.kpull = strtoul(optarg, NULL, 0);
1261 break;
1262 case 'n':
1263 frame_count_max = strtoul(optarg, NULL, 0);
1264 break;
1265 case 'F':
1266 ptr = optarg;
1267 ctx.dump_interval = 0;
1269 for (j = i = strlen(optarg); i > 0; --i) {
1270 if (!isdigit(optarg[j - i]))
1271 break;
1272 ptr++;
1275 if (!strncmp(ptr, "KiB", strlen("KiB"))) {
1276 ctx.dump_interval = 1 << 10;
1277 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1278 } else if (!strncmp(ptr, "MiB", strlen("MiB"))) {
1279 ctx.dump_interval = 1 << 20;
1280 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1281 } else if (!strncmp(ptr, "GiB", strlen("GiB"))) {
1282 ctx.dump_interval = 1 << 30;
1283 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1284 } else if (!strncmp(ptr, "sec", strlen("sec"))) {
1285 ctx.dump_interval = 1;
1286 ctx.dump_mode = DUMP_INTERVAL_TIME;
1287 } else if (!strncmp(ptr, "min", strlen("min"))) {
1288 ctx.dump_interval = 60;
1289 ctx.dump_mode = DUMP_INTERVAL_TIME;
1290 } else if (!strncmp(ptr, "hrs", strlen("hrs"))) {
1291 ctx.dump_interval = 60 * 60;
1292 ctx.dump_mode = DUMP_INTERVAL_TIME;
1293 } else if (!strncmp(ptr, "s", strlen("s"))) {
1294 ctx.dump_interval = 1;
1295 ctx.dump_mode = DUMP_INTERVAL_TIME;
1296 } else {
1297 panic("Syntax error in time/size param!\n");
1300 ctx.dump_interval *= strtoul(optarg, NULL, 0);
1301 break;
1302 case 'V':
1303 ctx.verbose = true;
1304 break;
1305 case 'B':
1306 ctx.dump_bpf = true;
1307 break;
1308 case 'D':
1309 pcap_dump_type_features();
1310 die();
1311 break;
1312 case 'U':
1313 update_geoip();
1314 die();
1315 break;
1316 case 'v':
1317 version();
1318 break;
1319 case 'h':
1320 help();
1321 break;
1322 case '?':
1323 switch (optopt) {
1324 case 'd':
1325 case 'i':
1326 case 'o':
1327 case 'f':
1328 case 't':
1329 case 'P':
1330 case 'F':
1331 case 'n':
1332 case 'S':
1333 case 'b':
1334 case 'k':
1335 case 'T':
1336 case 'u':
1337 case 'g':
1338 case 'e':
1339 panic("Option -%c requires an argument!\n",
1340 optopt);
1341 default:
1342 if (isprint(optopt))
1343 printf("Unknown option character `0x%X\'!\n", optopt);
1344 die();
1346 default:
1347 break;
1351 if (!ctx.filter && optind != argc) {
1352 int ret;
1353 off_t offset = 0;
1355 for (i = optind; i < argc; ++i) {
1356 size_t alen = strlen(argv[i]) + 2;
1357 size_t flen = ctx.filter ? strlen(ctx.filter) : 0;
1359 ctx.filter = xrealloc(ctx.filter, 1, flen + alen);
1360 ret = slprintf(ctx.filter + offset, strlen(argv[i]) + 2, "%s ", argv[i]);
1361 if (ret < 0)
1362 panic("Cannot concatenate filter string!\n");
1363 else
1364 offset += ret;
1368 if (!ctx.device_in)
1369 ctx.device_in = xstrdup("any");
1371 register_signal(SIGINT, signal_handler);
1372 register_signal(SIGQUIT, signal_handler);
1373 register_signal(SIGTERM, signal_handler);
1374 register_signal(SIGHUP, signal_handler);
1376 tprintf_init();
1378 if (prio_high) {
1379 set_proc_prio(-20);
1380 set_sched_status(SCHED_FIFO, sched_get_priority_max(SCHED_FIFO));
1383 if (ctx.device_in && (device_mtu(ctx.device_in) ||
1384 !strncmp("any", ctx.device_in, strlen(ctx.device_in)))) {
1385 if (!ctx.rfraw)
1386 ctx.link_type = pcap_devtype_to_linktype(ctx.device_in);
1387 if (!ctx.device_out) {
1388 ctx.dump = 0;
1389 main_loop = recv_only_or_dump;
1390 } else if (device_mtu(ctx.device_out)) {
1391 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1392 main_loop = receive_to_xmit;
1393 } else {
1394 ctx.dump = 1;
1395 register_signal_f(SIGALRM, timer_next_dump, SA_SIGINFO);
1396 main_loop = recv_only_or_dump;
1397 if (!ops_touched)
1398 ctx.pcap = PCAP_OPS_SG;
1400 } else {
1401 if (ctx.device_out && device_mtu(ctx.device_out)) {
1402 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1403 main_loop = pcap_to_xmit;
1404 if (!ops_touched)
1405 ctx.pcap = PCAP_OPS_MM;
1406 } else {
1407 main_loop = read_pcap;
1408 if (!ops_touched)
1409 ctx.pcap = PCAP_OPS_SG;
1413 bug_on(!main_loop);
1415 init_geoip(0);
1416 if (setsockmem)
1417 set_system_socket_memory(vals, array_size(vals));
1418 if (!ctx.enforce)
1419 xlockme();
1421 if (ctx.verbose)
1422 printf("pcap file I/O method: %s\n", pcap_ops_group_to_str[ctx.pcap]);
1424 main_loop(&ctx);
1426 if (!ctx.enforce)
1427 xunlockme();
1428 if (setsockmem)
1429 reset_system_socket_memory(vals, array_size(vals));
1430 destroy_geoip();
1432 device_restore_irq_affinity_list();
1433 tprintf_cleanup();
1435 destroy_ctx(&ctx);
1436 return 0;