build: flowtop: Only build ioops with GeoIP support enabled
[netsniff-ng.git] / netsniff-ng.c
blob397f50d2791acfa212dff1ef7c89918f0e087c2a
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 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 if (ctx->rfraw) {
213 ctx->device_trans = xstrdup(ctx->device_out);
214 xfree(ctx->device_out);
216 enter_rfmon_mac80211(ctx->device_trans, &ctx->device_out);
217 if (ctx->link_type != LINKTYPE_IEEE802_11)
218 panic("Wrong linktype of pcap!\n");
221 ifindex = device_ifindex(ctx->device_out);
222 size = ring_size(ctx->device_out, ctx->reserve_size);
224 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
225 if (ctx->dump_bpf)
226 bpf_dump_all(&bpf_ops);
228 ring_tx_setup(&tx_ring, tx_sock, size, ifindex, ctx->jumbo, ctx->verbose);
230 dissector_init_all(ctx->print_mode);
232 if (ctx->cpu >= 0 && ifindex > 0) {
233 int irq = device_irq_number(ctx->device_out);
234 device_set_irq_affinity(irq, ctx->cpu);
236 if (ctx->verbose)
237 printf("IRQ: %s:%d > CPU%d\n",
238 ctx->device_out, irq, ctx->cpu);
241 if (ctx->kpull)
242 interval = ctx->kpull;
244 set_itimer_interval_value(&itimer, 0, interval);
245 setitimer(ITIMER_REAL, &itimer, NULL);
247 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
249 printf("Running! Hang up with ^C!\n\n");
250 fflush(stdout);
252 bug_on(gettimeofday(&start, NULL));
254 while (likely(sigint == 0)) {
255 while (user_may_pull_from_tx(tx_ring.frames[it].iov_base)) {
256 hdr = tx_ring.frames[it].iov_base;
257 out = ((uint8_t *) hdr) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
259 do {
260 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic, out,
261 ring_frame_size(&tx_ring));
262 if (unlikely(ret <= 0))
263 goto out;
265 if (ring_frame_size(&tx_ring) <
266 pcap_get_length(&phdr, ctx->magic)) {
267 pcap_set_length(&phdr, ctx->magic,
268 ring_frame_size(&tx_ring));
269 trunced++;
271 } while (ctx->filter &&
272 !bpf_run_filter(&bpf_ops, out,
273 pcap_get_length(&phdr, ctx->magic)));
275 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &hdr->tp_h, NULL);
277 ctx->tx_bytes += hdr->tp_h.tp_len;;
278 ctx->tx_packets++;
280 show_frame_hdr(out, hdr->tp_h.tp_snaplen,
281 ctx->link_type, hdr, ctx->print_mode);
283 dissector_entry_point(out, hdr->tp_h.tp_snaplen,
284 ctx->link_type, ctx->print_mode);
286 kernel_may_pull_from_tx(&hdr->tp_h);
288 it++;
289 if (it >= tx_ring.layout.tp_frame_nr)
290 it = 0;
292 if (unlikely(sigint == 1))
293 break;
295 if (frame_count_max != 0) {
296 if (ctx->tx_packets >= frame_count_max) {
297 sigint = 1;
298 break;
304 out:
305 bug_on(gettimeofday(&end, NULL));
306 timersub(&end, &start, &diff);
308 timer_purge();
310 bpf_release(&bpf_ops);
312 dissector_cleanup_all();
313 destroy_tx_ring(tx_sock, &tx_ring);
315 if (ctx->rfraw)
316 leave_rfmon_mac80211(ctx->device_out);
318 if (__pcap_io->prepare_close_pcap)
319 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
321 if (!strncmp("-", ctx->device_in, strlen("-")))
322 dup2(fd, fileno(stdin));
323 close(fd);
325 close(tx_sock);
327 fflush(stdout);
328 printf("\n");
329 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
330 printf("\r%12lu packets truncated in file\n", trunced);
331 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
332 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
335 static void receive_to_xmit(struct ctx *ctx)
337 short ifflags = 0;
338 uint8_t *in, *out;
339 int rx_sock, ifindex_in, ifindex_out, ret;
340 size_t size_in, size_out;
341 unsigned int it_in = 0, it_out = 0;
342 unsigned long frame_count = 0;
343 struct frame_map *hdr_in, *hdr_out;
344 struct ring tx_ring, rx_ring;
345 struct pollfd rx_poll;
346 struct sock_fprog bpf_ops;
348 if (!strncmp(ctx->device_in, ctx->device_out, IFNAMSIZ))
349 panic("Ingress/egress devices must be different!\n");
350 if (!device_up_and_running(ctx->device_out))
351 panic("Egress device not up and running!\n");
353 rx_sock = pf_socket();
354 tx_sock = pf_socket();
356 ifindex_in = device_ifindex(ctx->device_in);
357 ifindex_out = device_ifindex(ctx->device_out);
359 size_in = ring_size(ctx->device_in, ctx->reserve_size);
360 size_out = ring_size(ctx->device_out, ctx->reserve_size);
362 enable_kernel_bpf_jit_compiler();
364 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
365 if (ctx->dump_bpf)
366 bpf_dump_all(&bpf_ops);
367 bpf_attach_to_sock(rx_sock, &bpf_ops);
369 ring_rx_setup(&rx_ring, rx_sock, size_in, ifindex_in, &rx_poll, false, ctx->jumbo, ctx->verbose);
370 ring_tx_setup(&tx_ring, tx_sock, size_out, ifindex_out, ctx->jumbo, ctx->verbose);
372 dissector_init_all(ctx->print_mode);
374 if (ctx->promiscuous)
375 ifflags = device_enter_promiscuous_mode(ctx->device_in);
377 if (ctx->kpull)
378 interval = ctx->kpull;
380 set_itimer_interval_value(&itimer, 0, interval);
381 setitimer(ITIMER_REAL, &itimer, NULL);
383 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
385 printf("Running! Hang up with ^C!\n\n");
386 fflush(stdout);
388 while (likely(sigint == 0)) {
389 while (user_may_pull_from_rx(rx_ring.frames[it_in].iov_base)) {
390 hdr_in = rx_ring.frames[it_in].iov_base;
391 in = ((uint8_t *) hdr_in) + hdr_in->tp_h.tp_mac;
393 frame_count++;
395 if (ctx->packet_type != -1)
396 if (ctx->packet_type != hdr_in->s_ll.sll_pkttype)
397 goto next;
399 hdr_out = tx_ring.frames[it_out].iov_base;
400 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
402 while (!user_may_pull_from_tx(tx_ring.frames[it_out].iov_base) &&
403 likely(!sigint)) {
404 if (ctx->randomize)
405 next_rnd_slot(&it_out, &tx_ring);
406 else {
407 it_out++;
408 if (it_out >= tx_ring.layout.tp_frame_nr)
409 it_out = 0;
412 hdr_out = tx_ring.frames[it_out].iov_base;
413 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
416 tpacket_hdr_clone(&hdr_out->tp_h, &hdr_in->tp_h);
417 fmemcpy(out, in, hdr_in->tp_h.tp_len);
419 kernel_may_pull_from_tx(&hdr_out->tp_h);
420 if (ctx->randomize)
421 next_rnd_slot(&it_out, &tx_ring);
422 else {
423 it_out++;
424 if (it_out >= tx_ring.layout.tp_frame_nr)
425 it_out = 0;
428 show_frame_hdr(in, hdr_in->tp_h.tp_snaplen,
429 ctx->link_type, hdr_in, ctx->print_mode);
431 dissector_entry_point(in, hdr_in->tp_h.tp_snaplen,
432 ctx->link_type, ctx->print_mode);
434 if (frame_count_max != 0) {
435 if (frame_count >= frame_count_max) {
436 sigint = 1;
437 break;
441 next:
442 kernel_may_pull_from_rx(&hdr_in->tp_h);
444 it_in++;
445 if (it_in >= rx_ring.layout.tp_frame_nr)
446 it_in = 0;
448 if (unlikely(sigint == 1))
449 goto out;
452 ret = poll(&rx_poll, 1, -1);
453 if (unlikely(ret < 0)) {
454 if (errno != EINTR)
455 panic("Poll failed!\n");
459 out:
460 timer_purge();
462 sock_rx_net_stats(rx_sock, 0);
464 bpf_release(&bpf_ops);
466 dissector_cleanup_all();
468 destroy_tx_ring(tx_sock, &tx_ring);
469 destroy_rx_ring(rx_sock, &rx_ring);
471 if (ctx->promiscuous)
472 device_leave_promiscuous_mode(ctx->device_in, ifflags);
474 close(tx_sock);
475 close(rx_sock);
478 static void translate_pcap_to_txf(int fdo, uint8_t *out, size_t len)
480 size_t bytes_done = 0;
481 char bout[80];
483 slprintf(bout, sizeof(bout), "{\n ");
484 write_or_die(fdo, bout, strlen(bout));
486 while (bytes_done < len) {
487 slprintf(bout, sizeof(bout), "0x%02x, ", out[bytes_done]);
488 write_or_die(fdo, bout, strlen(bout));
490 bytes_done++;
492 if (bytes_done % 10 == 0) {
493 slprintf(bout, sizeof(bout), "\n");
494 write_or_die(fdo, bout, strlen(bout));
496 if (bytes_done < len) {
497 slprintf(bout, sizeof(bout), " ");
498 write_or_die(fdo, bout, strlen(bout));
502 if (bytes_done % 10 != 0) {
503 slprintf(bout, sizeof(bout), "\n");
504 write_or_die(fdo, bout, strlen(bout));
507 slprintf(bout, sizeof(bout), "}\n\n");
508 write_or_die(fdo, bout, strlen(bout));
511 static void read_pcap(struct ctx *ctx)
513 uint8_t *out;
514 int ret, fd, fdo = 0;
515 unsigned long trunced = 0;
516 size_t out_len;
517 pcap_pkthdr_t phdr;
518 struct sock_fprog bpf_ops;
519 struct frame_map fm;
520 struct timeval start, end, diff;
522 bug_on(!__pcap_io);
524 if (!strncmp("-", ctx->device_in, strlen("-"))) {
525 fd = dup_or_die(fileno(stdin));
526 close(fileno(stdin));
527 if (ctx->pcap == PCAP_OPS_MM)
528 ctx->pcap = PCAP_OPS_SG;
529 } else {
530 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
533 if (__pcap_io->init_once_pcap)
534 __pcap_io->init_once_pcap();
536 ret = __pcap_io->pull_fhdr_pcap(fd, &ctx->magic, &ctx->link_type);
537 if (ret)
538 panic("Error reading pcap header!\n");
540 if (__pcap_io->prepare_access_pcap) {
541 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_RD, ctx->jumbo);
542 if (ret)
543 panic("Error prepare reading pcap!\n");
546 fmemset(&fm, 0, sizeof(fm));
548 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
549 if (ctx->dump_bpf)
550 bpf_dump_all(&bpf_ops);
552 dissector_init_all(ctx->print_mode);
554 out_len = round_up(1024 * 1024, RUNTIME_PAGE_SIZE);
555 out = xmalloc_aligned(out_len, CO_CACHE_LINE_SIZE);
557 if (ctx->device_out) {
558 if (!strncmp("-", ctx->device_out, strlen("-"))) {
559 fdo = dup_or_die(fileno(stdout));
560 close(fileno(stdout));
561 } else {
562 fdo = open_or_die_m(ctx->device_out, O_RDWR | O_CREAT |
563 O_TRUNC | O_LARGEFILE, DEFFILEMODE);
567 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
569 printf("Running! Hang up with ^C!\n\n");
570 fflush(stdout);
572 bug_on(gettimeofday(&start, NULL));
574 while (likely(sigint == 0)) {
575 do {
576 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic,
577 out, out_len);
578 if (unlikely(ret < 0))
579 goto out;
581 if (unlikely(pcap_get_length(&phdr, ctx->magic) == 0)) {
582 trunced++;
583 continue;
586 if (unlikely(pcap_get_length(&phdr, ctx->magic) > out_len)) {
587 pcap_set_length(&phdr, ctx->magic, out_len);
588 trunced++;
590 } while (ctx->filter &&
591 !bpf_run_filter(&bpf_ops, out,
592 pcap_get_length(&phdr, ctx->magic)));
594 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &fm.tp_h, &fm.s_ll);
596 ctx->tx_bytes += fm.tp_h.tp_len;
597 ctx->tx_packets++;
599 show_frame_hdr(out, fm.tp_h.tp_snaplen, ctx->link_type, &fm,
600 ctx->print_mode);
602 dissector_entry_point(out, fm.tp_h.tp_snaplen,
603 ctx->link_type, ctx->print_mode);
605 if (ctx->device_out)
606 translate_pcap_to_txf(fdo, out, fm.tp_h.tp_snaplen);
608 if (frame_count_max != 0) {
609 if (ctx->tx_packets >= frame_count_max) {
610 sigint = 1;
611 break;
616 out:
618 bug_on(gettimeofday(&end, NULL));
619 timersub(&end, &start, &diff);
621 bpf_release(&bpf_ops);
623 dissector_cleanup_all();
625 if (__pcap_io->prepare_close_pcap)
626 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
628 xfree(out);
630 fflush(stdout);
631 printf("\n");
632 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
633 printf("\r%12lu packets truncated in file\n", trunced);
634 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
635 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
637 if (!strncmp("-", ctx->device_in, strlen("-")))
638 dup2(fd, fileno(stdin));
639 close(fd);
641 if (ctx->device_out) {
642 if (!strncmp("-", ctx->device_out, strlen("-")))
643 dup2(fdo, fileno(stdout));
644 close(fdo);
648 static void finish_multi_pcap_file(struct ctx *ctx, int fd)
650 __pcap_io->fsync_pcap(fd);
652 if (__pcap_io->prepare_close_pcap)
653 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
655 close(fd);
657 fmemset(&itimer, 0, sizeof(itimer));
658 setitimer(ITIMER_REAL, &itimer, NULL);
661 static int next_multi_pcap_file(struct ctx *ctx, int fd)
663 int ret;
664 char fname[512];
666 __pcap_io->fsync_pcap(fd);
668 if (__pcap_io->prepare_close_pcap)
669 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
671 close(fd);
673 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
674 ctx->prefix ? : "dump-", time(NULL));
676 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
677 O_LARGEFILE, DEFFILEMODE);
679 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
680 if (ret)
681 panic("Error writing pcap header!\n");
683 if (__pcap_io->prepare_access_pcap) {
684 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, true);
685 if (ret)
686 panic("Error prepare writing pcap!\n");
689 return fd;
692 static int begin_multi_pcap_file(struct ctx *ctx)
694 int fd, ret;
695 char fname[256];
697 bug_on(!__pcap_io);
699 if (ctx->device_out[strlen(ctx->device_out) - 1] == '/')
700 ctx->device_out[strlen(ctx->device_out) - 1] = 0;
702 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
703 ctx->prefix ? : "dump-", time(NULL));
705 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
706 O_LARGEFILE, DEFFILEMODE);
708 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
709 if (ret)
710 panic("Error writing pcap header!\n");
712 if (__pcap_io->prepare_access_pcap) {
713 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, true);
714 if (ret)
715 panic("Error prepare writing pcap!\n");
718 if (ctx->dump_mode == DUMP_INTERVAL_TIME) {
719 interval = ctx->dump_interval;
721 set_itimer_interval_value(&itimer, interval, 0);
722 setitimer(ITIMER_REAL, &itimer, NULL);
723 } else {
724 interval = 0;
727 return fd;
730 static void finish_single_pcap_file(struct ctx *ctx, int fd)
732 __pcap_io->fsync_pcap(fd);
734 if (__pcap_io->prepare_close_pcap)
735 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
737 if (strncmp("-", ctx->device_out, strlen("-")))
738 close(fd);
739 else
740 dup2(fd, fileno(stdout));
743 static int begin_single_pcap_file(struct ctx *ctx)
745 int fd, ret;
747 bug_on(!__pcap_io);
749 if (!strncmp("-", ctx->device_out, strlen("-"))) {
750 fd = dup_or_die(fileno(stdout));
751 close(fileno(stdout));
752 if (ctx->pcap == PCAP_OPS_MM)
753 ctx->pcap = PCAP_OPS_SG;
754 } else {
755 fd = open_or_die_m(ctx->device_out,
756 O_RDWR | O_CREAT | O_TRUNC |
757 O_LARGEFILE, DEFFILEMODE);
760 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
761 if (ret)
762 panic("Error writing pcap header!\n");
764 if (__pcap_io->prepare_access_pcap) {
765 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, true);
766 if (ret)
767 panic("Error prepare writing pcap!\n");
770 return fd;
773 static void print_pcap_file_stats(int sock, struct ctx *ctx)
775 int ret;
776 struct tpacket_stats kstats;
777 socklen_t slen = sizeof(kstats);
779 fmemset(&kstats, 0, sizeof(kstats));
781 ret = getsockopt(sock, SOL_PACKET, PACKET_STATISTICS, &kstats, &slen);
782 if (unlikely(ret))
783 panic("Cannot get packet statistics!\n");
785 if (ctx->print_mode == PRINT_NONE) {
786 printf(".(+%u/-%u)", kstats.tp_packets - kstats.tp_drops,
787 kstats.tp_drops);
788 fflush(stdout);
792 static void walk_t3_block(struct block_desc *pbd, struct ctx *ctx,
793 int sock, int *fd, unsigned long *frame_count)
795 int num_pkts = pbd->h1.num_pkts, i;
796 struct tpacket3_hdr *hdr;
797 struct sockaddr_ll *sll;
799 hdr = (void *) ((uint8_t *) pbd + pbd->h1.offset_to_first_pkt);
800 sll = (void *) ((uint8_t *) hdr + TPACKET_ALIGN(sizeof(*hdr)));
802 for (i = 0; i < num_pkts && likely(sigint == 0); ++i) {
803 uint8_t *packet = ((uint8_t *) hdr + hdr->tp_mac);
804 pcap_pkthdr_t phdr;
806 if (ctx->packet_type != -1)
807 if (ctx->packet_type != sll->sll_pkttype)
808 goto next;
810 (*frame_count)++;
812 if (dump_to_pcap(ctx)) {
813 int ret;
815 tpacket3_hdr_to_pcap_pkthdr(hdr, sll, &phdr, ctx->magic);
817 ret = __pcap_io->write_pcap(*fd, &phdr, ctx->magic, packet,
818 pcap_get_length(&phdr, ctx->magic));
819 if (unlikely(ret != (int) pcap_get_total_length(&phdr, ctx->magic)))
820 panic("Write error to pcap!\n");
823 __show_frame_hdr(packet, hdr->tp_snaplen, ctx->link_type, sll,
824 hdr, ctx->print_mode, true);
826 dissector_entry_point(packet, hdr->tp_snaplen, ctx->link_type,
827 ctx->print_mode);
828 next:
829 hdr = (void *) ((uint8_t *) hdr + hdr->tp_next_offset);
830 sll = (void *) ((uint8_t *) hdr + TPACKET_ALIGN(sizeof(*hdr)));
832 if (frame_count_max != 0) {
833 if (unlikely(*frame_count >= frame_count_max)) {
834 sigint = 1;
835 break;
839 if (dump_to_pcap(ctx)) {
840 if (ctx->dump_mode == DUMP_INTERVAL_SIZE) {
841 interval += hdr->tp_snaplen;
842 if (interval > ctx->dump_interval) {
843 next_dump = true;
844 interval = 0;
848 if (next_dump) {
849 *fd = next_multi_pcap_file(ctx, *fd);
850 next_dump = false;
852 if (unlikely(ctx->verbose))
853 print_pcap_file_stats(sock, ctx);
859 static void recv_only_or_dump(struct ctx *ctx)
861 short ifflags = 0;
862 int sock, ifindex, fd = 0, ret;
863 size_t size;
864 unsigned int it = 0;
865 struct ring rx_ring;
866 struct pollfd rx_poll;
867 struct sock_fprog bpf_ops;
868 struct timeval start, end, diff;
869 struct block_desc *pbd;
870 unsigned long frame_count = 0;
872 sock = pf_socket();
874 if (ctx->rfraw) {
875 ctx->device_trans = xstrdup(ctx->device_in);
876 xfree(ctx->device_in);
878 enter_rfmon_mac80211(ctx->device_trans, &ctx->device_in);
879 ctx->link_type = LINKTYPE_IEEE802_11;
882 ifindex = device_ifindex(ctx->device_in);
884 size = ring_size(ctx->device_in, ctx->reserve_size);
886 enable_kernel_bpf_jit_compiler();
888 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
889 if (ctx->dump_bpf)
890 bpf_dump_all(&bpf_ops);
891 bpf_attach_to_sock(sock, &bpf_ops);
893 ret = set_sockopt_hwtimestamp(sock, ctx->device_in);
894 if (ret == 0 && ctx->verbose)
895 printf("HW timestamping enabled\n");
897 ring_rx_setup(&rx_ring, sock, size, ifindex, &rx_poll, true, true, ctx->verbose);
899 dissector_init_all(ctx->print_mode);
901 if (ctx->cpu >= 0 && ifindex > 0) {
902 int irq = device_irq_number(ctx->device_in);
903 device_set_irq_affinity(irq, ctx->cpu);
905 if (ctx->verbose)
906 printf("IRQ: %s:%d > CPU%d\n",
907 ctx->device_in, irq, ctx->cpu);
910 if (ctx->promiscuous)
911 ifflags = device_enter_promiscuous_mode(ctx->device_in);
913 if (dump_to_pcap(ctx) && __pcap_io->init_once_pcap)
914 __pcap_io->init_once_pcap();
916 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
918 if (dump_to_pcap(ctx)) {
919 struct stat stats;
921 ret = stat(ctx->device_out, &stats);
922 if (ret < 0)
923 ctx->dump_dir = 0;
924 else
925 ctx->dump_dir = S_ISDIR(stats.st_mode);
927 if (ctx->dump_dir)
928 fd = begin_multi_pcap_file(ctx);
929 else
930 fd = begin_single_pcap_file(ctx);
933 printf("Running! Hang up with ^C!\n\n");
934 fflush(stdout);
936 bug_on(gettimeofday(&start, NULL));
938 while (likely(sigint == 0)) {
939 while (user_may_pull_from_rx_block((pbd = (void *)
940 rx_ring.frames[it].iov_base))) {
941 walk_t3_block(pbd, ctx, sock, &fd, &frame_count);
943 kernel_may_pull_from_rx_block(pbd);
944 it = (it + 1) % rx_ring.layout3.tp_block_nr;
946 if (unlikely(sigint == 1))
947 break;
950 ret = poll(&rx_poll, 1, -1);
951 if (unlikely(ret < 0)) {
952 if (errno != EINTR)
953 panic("Poll failed!\n");
957 bug_on(gettimeofday(&end, NULL));
958 timersub(&end, &start, &diff);
960 if (!(ctx->dump_dir && ctx->print_mode == PRINT_NONE)) {
961 sock_rx_net_stats(sock, frame_count);
963 printf("\r%12lu sec, %lu usec in total\n",
964 diff.tv_sec, diff.tv_usec);
965 } else {
966 printf("\n\n");
967 fflush(stdout);
970 bpf_release(&bpf_ops);
971 dissector_cleanup_all();
972 destroy_rx_ring(sock, &rx_ring);
974 if (ctx->promiscuous)
975 device_leave_promiscuous_mode(ctx->device_in, ifflags);
977 if (ctx->rfraw)
978 leave_rfmon_mac80211(ctx->device_in);
980 if (dump_to_pcap(ctx)) {
981 if (ctx->dump_dir)
982 finish_multi_pcap_file(ctx, fd);
983 else
984 finish_single_pcap_file(ctx, fd);
987 close(sock);
990 static void init_ctx(struct ctx *ctx)
992 memset(ctx, 0, sizeof(*ctx));
993 ctx->uid = getuid();
994 ctx->uid = getgid();
996 ctx->cpu = -1;
997 ctx->packet_type = -1;
999 ctx->magic = ORIGINAL_TCPDUMP_MAGIC;
1000 ctx->print_mode = PRINT_NORM;
1001 ctx->pcap = PCAP_OPS_SG;
1003 ctx->dump_mode = DUMP_INTERVAL_TIME;
1004 ctx->dump_interval = 60;
1006 ctx->promiscuous = true;
1007 ctx->randomize = false;
1010 static void destroy_ctx(struct ctx *ctx)
1012 free(ctx->device_in);
1013 free(ctx->device_out);
1014 free(ctx->device_trans);
1016 free(ctx->prefix);
1019 static void __noreturn help(void)
1021 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING);
1022 puts("http://www.netsniff-ng.org\n\n"
1023 "Usage: netsniff-ng [options] [filter-expression]\n"
1024 "Options:\n"
1025 " -i|-d|--dev|--in <dev|pcap|-> Input source as netdev, pcap or pcap stdin\n"
1026 " -o|--out <dev|pcap|dir|cfg|-> Output sink as netdev, pcap, directory, trafgen, or stdout\n"
1027 " -f|--filter <bpf-file|expr> Use BPF filter file from bpfc or tcpdump-like expression\n"
1028 " -t|--type <type> Filter for: host|broadcast|multicast|others|outgoing\n"
1029 " -F|--interval <size|time> Dump interval if -o is a dir: <num>KiB/MiB/GiB/s/sec/min/hrs\n"
1030 " -R|--rfraw Capture or inject raw 802.11 frames\n"
1031 " -n|--num <0|uint> Number of packets until exit (def: 0)\n"
1032 " -P|--prefix <name> Prefix for pcaps stored in directory\n"
1033 " -T|--magic <pcap-magic> Pcap magic number/pcap format to store, see -D\n"
1034 " -D|--dump-pcap-types Dump pcap types and magic numbers and quit\n"
1035 " -B|--dump-bpf Dump generated BPF assembly\n"
1036 " -r|--rand Randomize packet forwarding order (dev->dev)\n"
1037 " -M|--no-promisc No promiscuous mode for netdev\n"
1038 " -A|--no-sock-mem Don't tune core socket memory\n"
1039 " -m|--mmap Mmap(2) pcap file I/O, e.g. for replaying pcaps\n"
1040 " -G|--sg Scatter/gather pcap file I/O\n"
1041 " -c|--clrw Use slower read(2)/write(2) I/O\n"
1042 " -S|--ring-size <size> Specify ring size to: <num>KiB/MiB/GiB\n"
1043 " -k|--kernel-pull <uint> Kernel pull from user interval in us (def: 10us)\n"
1044 " -J|--jumbo-support Support replay/fwd 64KB Super Jumbo Frames (def: 2048B)\n"
1045 " -b|--bind-cpu <cpu> Bind to specific CPU\n"
1046 " -u|--user <userid> Drop privileges and change to userid\n"
1047 " -g|--group <groupid> Drop privileges and change to groupid\n"
1048 " -H|--prio-high Make this high priority process\n"
1049 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
1050 " -s|--silent Do not print captured packets\n"
1051 " -q|--less Print less-verbose packet information\n"
1052 " -X|--hex Print packet data in hex format\n"
1053 " -l|--ascii Print human-readable packet data\n"
1054 " -U|--update Update GeoIP databases\n"
1055 " -V|--verbose Be more verbose\n"
1056 " -v|--version Show version and exit\n"
1057 " -h|--help Guess what?!\n\n"
1058 "Examples:\n"
1059 " netsniff-ng --in eth0 --out dump.pcap -s -T 0xa1b2c3d4 --b 0 tcp or udp\n"
1060 " netsniff-ng --in wlan0 --rfraw --out dump.pcap --silent --bind-cpu 0\n"
1061 " netsniff-ng --in dump.pcap --mmap --out eth0 -k1000 --silent --bind-cpu 0\n"
1062 " netsniff-ng --in dump.pcap --out dump.cfg --silent --bind-cpu 0\n"
1063 " netsniff-ng --in eth0 --out eth1 --silent --bind-cpu 0 -J --type host\n"
1064 " netsniff-ng --in eth1 --out /opt/probe/ -s -m --interval 100MiB -b 0\n"
1065 " netsniff-ng --in vlan0 --out dump.pcap -c -u `id -u bob` -g `id -g bob`\n"
1066 " netsniff-ng --in any --filter http.bpf --jumbo-support --ascii -V\n\n"
1067 "Note:\n"
1068 " For introducing bit errors, delays with random variation and more\n"
1069 " while replaying pcaps, make use of tc(8) with its disciplines (e.g. netem).\n\n"
1070 "Please report bugs to <bugs@netsniff-ng.org>\n"
1071 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1072 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1073 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1074 "Swiss federal institute of technology (ETH Zurich)\n"
1075 "License: GNU GPL version 2.0\n"
1076 "This is free software: you are free to change and redistribute it.\n"
1077 "There is NO WARRANTY, to the extent permitted by law.\n");
1078 die();
1081 static void __noreturn version(void)
1083 printf("\nnetsniff-ng %s, Git id: %s\n", VERSION_LONG, GITVERSION);
1084 puts("the packet sniffing beast\n"
1085 "http://www.netsniff-ng.org\n\n"
1086 "Please report bugs to <bugs@netsniff-ng.org>\n"
1087 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1088 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1089 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1090 "Swiss federal institute of technology (ETH Zurich)\n"
1091 "License: GNU GPL version 2.0\n"
1092 "This is free software: you are free to change and redistribute it.\n"
1093 "There is NO WARRANTY, to the extent permitted by law.\n");
1094 die();
1097 int main(int argc, char **argv)
1099 char *ptr;
1100 int c, i, j, cpu_tmp, opt_index, ops_touched = 0, vals[4] = {0};
1101 bool prio_high = false, setsockmem = true;
1102 void (*main_loop)(struct ctx *ctx) = NULL;
1103 struct ctx ctx;
1105 init_ctx(&ctx);
1106 srand(time(NULL));
1108 while ((c = getopt_long(argc, argv, short_options, long_options,
1109 &opt_index)) != EOF) {
1110 switch (c) {
1111 case 'd':
1112 case 'i':
1113 ctx.device_in = xstrdup(optarg);
1114 break;
1115 case 'o':
1116 ctx.device_out = xstrdup(optarg);
1117 break;
1118 case 'P':
1119 ctx.prefix = xstrdup(optarg);
1120 break;
1121 case 'R':
1122 ctx.link_type = LINKTYPE_IEEE802_11;
1123 ctx.rfraw = 1;
1124 break;
1125 case 'r':
1126 ctx.randomize = true;
1127 break;
1128 case 'J':
1129 ctx.jumbo = true;
1130 break;
1131 case 'T':
1132 ctx.magic = (uint32_t) strtoul(optarg, NULL, 0);
1133 pcap_check_magic(ctx.magic);
1134 break;
1135 case 'f':
1136 ctx.filter = xstrdup(optarg);
1137 break;
1138 case 'M':
1139 ctx.promiscuous = false;
1140 break;
1141 case 'A':
1142 setsockmem = false;
1143 break;
1144 case 'u':
1145 ctx.uid = strtoul(optarg, NULL, 0);
1146 ctx.enforce = true;
1147 break;
1148 case 'g':
1149 ctx.gid = strtoul(optarg, NULL, 0);
1150 ctx.enforce = true;
1151 break;
1152 case 't':
1153 if (!strncmp(optarg, "host", strlen("host")))
1154 ctx.packet_type = PACKET_HOST;
1155 else if (!strncmp(optarg, "broadcast", strlen("broadcast")))
1156 ctx.packet_type = PACKET_BROADCAST;
1157 else if (!strncmp(optarg, "multicast", strlen("multicast")))
1158 ctx.packet_type = PACKET_MULTICAST;
1159 else if (!strncmp(optarg, "others", strlen("others")))
1160 ctx.packet_type = PACKET_OTHERHOST;
1161 else if (!strncmp(optarg, "outgoing", strlen("outgoing")))
1162 ctx.packet_type = PACKET_OUTGOING;
1163 else
1164 ctx.packet_type = -1;
1165 break;
1166 case 'S':
1167 ptr = optarg;
1168 for (j = i = strlen(optarg); i > 0; --i) {
1169 if (!isdigit(optarg[j - i]))
1170 break;
1171 ptr++;
1174 if (!strncmp(ptr, "KiB", strlen("KiB")))
1175 ctx.reserve_size = 1 << 10;
1176 else if (!strncmp(ptr, "MiB", strlen("MiB")))
1177 ctx.reserve_size = 1 << 20;
1178 else if (!strncmp(ptr, "GiB", strlen("GiB")))
1179 ctx.reserve_size = 1 << 30;
1180 else
1181 panic("Syntax error in ring size param!\n");
1183 ctx.reserve_size *= strtoul(optarg, NULL, 0);
1184 break;
1185 case 'b':
1186 cpu_tmp = strtol(optarg, NULL, 0);
1188 cpu_affinity(cpu_tmp);
1189 if (ctx.cpu != -2)
1190 ctx.cpu = cpu_tmp;
1191 break;
1192 case 'H':
1193 prio_high = true;
1194 break;
1195 case 'c':
1196 ctx.pcap = PCAP_OPS_RW;
1197 ops_touched = 1;
1198 break;
1199 case 'm':
1200 ctx.pcap = PCAP_OPS_MM;
1201 ops_touched = 1;
1202 break;
1203 case 'G':
1204 ctx.pcap = PCAP_OPS_SG;
1205 ops_touched = 1;
1206 break;
1207 case 'Q':
1208 ctx.cpu = -2;
1209 break;
1210 case 's':
1211 ctx.print_mode = PRINT_NONE;
1212 break;
1213 case 'q':
1214 ctx.print_mode = PRINT_LESS;
1215 break;
1216 case 'X':
1217 ctx.print_mode =
1218 (ctx.print_mode == PRINT_ASCII) ?
1219 PRINT_HEX_ASCII : PRINT_HEX;
1220 break;
1221 case 'l':
1222 ctx.print_mode =
1223 (ctx.print_mode == PRINT_HEX) ?
1224 PRINT_HEX_ASCII : PRINT_ASCII;
1225 break;
1226 case 'k':
1227 ctx.kpull = strtoul(optarg, NULL, 0);
1228 break;
1229 case 'n':
1230 frame_count_max = strtoul(optarg, NULL, 0);
1231 break;
1232 case 'F':
1233 ptr = optarg;
1234 for (j = i = strlen(optarg); i > 0; --i) {
1235 if (!isdigit(optarg[j - i]))
1236 break;
1237 ptr++;
1240 if (!strncmp(ptr, "KiB", strlen("KiB"))) {
1241 ctx.dump_interval = 1 << 10;
1242 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1243 } else if (!strncmp(ptr, "MiB", strlen("MiB"))) {
1244 ctx.dump_interval = 1 << 20;
1245 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1246 } else if (!strncmp(ptr, "GiB", strlen("GiB"))) {
1247 ctx.dump_interval = 1 << 30;
1248 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1249 } else if (!strncmp(ptr, "sec", strlen("sec"))) {
1250 ctx.dump_interval = 1;
1251 ctx.dump_mode = DUMP_INTERVAL_TIME;
1252 } else if (!strncmp(ptr, "min", strlen("min"))) {
1253 ctx.dump_interval = 60;
1254 ctx.dump_mode = DUMP_INTERVAL_TIME;
1255 } else if (!strncmp(ptr, "hrs", strlen("hrs"))) {
1256 ctx.dump_interval = 60 * 60;
1257 ctx.dump_mode = DUMP_INTERVAL_TIME;
1258 } else if (!strncmp(ptr, "s", strlen("s"))) {
1259 ctx.dump_interval = 1;
1260 ctx.dump_mode = DUMP_INTERVAL_TIME;
1261 } else {
1262 panic("Syntax error in time/size param!\n");
1265 ctx.dump_interval *= strtoul(optarg, NULL, 0);
1266 break;
1267 case 'V':
1268 ctx.verbose = true;
1269 break;
1270 case 'B':
1271 ctx.dump_bpf = true;
1272 break;
1273 case 'D':
1274 pcap_dump_type_features();
1275 die();
1276 break;
1277 case 'U':
1278 update_geoip();
1279 die();
1280 break;
1281 case 'v':
1282 version();
1283 break;
1284 case 'h':
1285 help();
1286 break;
1287 case '?':
1288 switch (optopt) {
1289 case 'd':
1290 case 'i':
1291 case 'o':
1292 case 'f':
1293 case 't':
1294 case 'P':
1295 case 'F':
1296 case 'n':
1297 case 'S':
1298 case 'b':
1299 case 'k':
1300 case 'T':
1301 case 'u':
1302 case 'g':
1303 case 'e':
1304 panic("Option -%c requires an argument!\n",
1305 optopt);
1306 default:
1307 if (isprint(optopt))
1308 printf("Unknown option character `0x%X\'!\n", optopt);
1309 die();
1311 default:
1312 break;
1316 if (!ctx.filter && optind != argc) {
1317 int ret;
1318 off_t offset = 0;
1320 for (i = optind; i < argc; ++i) {
1321 size_t alen = strlen(argv[i]) + 2;
1322 size_t flen = ctx.filter ? strlen(ctx.filter) : 0;
1324 ctx.filter = xrealloc(ctx.filter, 1, flen + alen);
1325 ret = slprintf(ctx.filter + offset, strlen(argv[i]) + 2, "%s ", argv[i]);
1326 if (ret < 0)
1327 panic("Cannot concatenate filter string!\n");
1328 else
1329 offset += ret;
1333 if (!ctx.device_in)
1334 ctx.device_in = xstrdup("any");
1336 register_signal(SIGINT, signal_handler);
1337 register_signal(SIGQUIT, signal_handler);
1338 register_signal(SIGTERM, signal_handler);
1339 register_signal(SIGHUP, signal_handler);
1341 tprintf_init();
1343 if (prio_high) {
1344 set_proc_prio(-20);
1345 set_sched_status(SCHED_FIFO, sched_get_priority_max(SCHED_FIFO));
1348 if (ctx.device_in && (device_mtu(ctx.device_in) ||
1349 !strncmp("any", ctx.device_in, strlen(ctx.device_in)))) {
1350 if (!ctx.rfraw)
1351 ctx.link_type = pcap_devtype_to_linktype(ctx.device_in);
1352 if (!ctx.device_out) {
1353 ctx.dump = 0;
1354 main_loop = recv_only_or_dump;
1355 } else if (device_mtu(ctx.device_out)) {
1356 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1357 main_loop = receive_to_xmit;
1358 } else {
1359 ctx.dump = 1;
1360 register_signal_f(SIGALRM, timer_next_dump, SA_SIGINFO);
1361 main_loop = recv_only_or_dump;
1362 if (!ops_touched)
1363 ctx.pcap = PCAP_OPS_SG;
1365 } else {
1366 if (ctx.device_out && device_mtu(ctx.device_out)) {
1367 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1368 main_loop = pcap_to_xmit;
1369 if (!ops_touched)
1370 ctx.pcap = PCAP_OPS_MM;
1371 } else {
1372 main_loop = read_pcap;
1373 if (!ops_touched)
1374 ctx.pcap = PCAP_OPS_SG;
1378 bug_on(!main_loop);
1380 init_geoip(0);
1381 if (setsockmem)
1382 set_system_socket_memory(vals, array_size(vals));
1383 if (!ctx.enforce)
1384 xlockme();
1386 if (ctx.verbose)
1387 printf("pcap file I/O method: %s\n", pcap_ops_group_to_str[ctx.pcap]);
1389 main_loop(&ctx);
1391 if (!ctx.enforce)
1392 xunlockme();
1393 if (setsockmem)
1394 reset_system_socket_memory(vals, array_size(vals));
1395 destroy_geoip();
1397 device_restore_irq_affinity_list();
1398 tprintf_cleanup();
1400 destroy_ctx(&ctx);
1401 return 0;