ring: Merge common ring_{rx,tx} initialization into own function
[netsniff-ng.git] / netsniff-ng.c
blobbee7f98956f235f1be86c7977dcdfdd7616850e7
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 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
214 if (ctx->rfraw) {
215 ctx->device_trans = xstrdup(ctx->device_out);
216 xfree(ctx->device_out);
218 enter_rfmon_mac80211(ctx->device_trans, &ctx->device_out);
219 if (ctx->link_type != LINKTYPE_IEEE802_11)
220 panic("Wrong linktype of pcap!\n");
223 ifindex = device_ifindex(ctx->device_out);
224 size = ring_size(ctx->device_out, ctx->reserve_size);
226 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
227 if (ctx->dump_bpf)
228 bpf_dump_all(&bpf_ops);
230 ring_tx_setup(&tx_ring, tx_sock, size, ifindex, ctx->jumbo, ctx->verbose);
232 dissector_init_all(ctx->print_mode);
234 if (ctx->cpu >= 0 && ifindex > 0) {
235 int irq = device_irq_number(ctx->device_out);
236 device_set_irq_affinity(irq, ctx->cpu);
238 if (ctx->verbose)
239 printf("IRQ: %s:%d > CPU%d\n",
240 ctx->device_out, irq, ctx->cpu);
243 if (ctx->kpull)
244 interval = ctx->kpull;
246 set_itimer_interval_value(&itimer, 0, interval);
247 setitimer(ITIMER_REAL, &itimer, NULL);
249 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
251 printf("Running! Hang up with ^C!\n\n");
252 fflush(stdout);
254 bug_on(gettimeofday(&start, NULL));
256 while (likely(sigint == 0)) {
257 while (user_may_pull_from_tx(tx_ring.frames[it].iov_base)) {
258 hdr = tx_ring.frames[it].iov_base;
259 out = ((uint8_t *) hdr) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
261 do {
262 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic, out,
263 ring_frame_size(&tx_ring));
264 if (unlikely(ret <= 0))
265 goto out;
267 if (ring_frame_size(&tx_ring) <
268 pcap_get_length(&phdr, ctx->magic)) {
269 pcap_set_length(&phdr, ctx->magic,
270 ring_frame_size(&tx_ring));
271 trunced++;
273 } while (ctx->filter &&
274 !bpf_run_filter(&bpf_ops, out,
275 pcap_get_length(&phdr, ctx->magic)));
277 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &hdr->tp_h, NULL);
279 ctx->tx_bytes += hdr->tp_h.tp_len;;
280 ctx->tx_packets++;
282 show_frame_hdr(hdr, ctx->print_mode);
284 dissector_entry_point(out, hdr->tp_h.tp_snaplen,
285 ctx->link_type, ctx->print_mode);
287 kernel_may_pull_from_tx(&hdr->tp_h);
289 it++;
290 if (it >= tx_ring.layout.tp_frame_nr)
291 it = 0;
293 if (unlikely(sigint == 1))
294 break;
296 if (frame_count_max != 0) {
297 if (ctx->tx_packets >= frame_count_max) {
298 sigint = 1;
299 break;
305 out:
306 bug_on(gettimeofday(&end, NULL));
307 timersub(&end, &start, &diff);
309 timer_purge();
311 bpf_release(&bpf_ops);
313 dissector_cleanup_all();
314 destroy_tx_ring(tx_sock, &tx_ring);
316 if (ctx->rfraw)
317 leave_rfmon_mac80211(ctx->device_out);
319 if (__pcap_io->prepare_close_pcap)
320 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
322 if (!strncmp("-", ctx->device_in, strlen("-")))
323 dup2(fd, fileno(stdin));
324 close(fd);
326 close(tx_sock);
328 fflush(stdout);
329 printf("\n");
330 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
331 printf("\r%12lu packets truncated in file\n", trunced);
332 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
333 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
336 static void receive_to_xmit(struct ctx *ctx)
338 short ifflags = 0;
339 uint8_t *in, *out;
340 int rx_sock, ifindex_in, ifindex_out, ret;
341 size_t size_in, size_out;
342 unsigned int it_in = 0, it_out = 0;
343 unsigned long frame_count = 0;
344 struct frame_map *hdr_in, *hdr_out;
345 struct ring tx_ring, rx_ring;
346 struct pollfd rx_poll;
347 struct sock_fprog bpf_ops;
349 if (!strncmp(ctx->device_in, ctx->device_out, IFNAMSIZ))
350 panic("Ingress/egress devices must be different!\n");
351 if (!device_up_and_running(ctx->device_out))
352 panic("Egress device not up and running!\n");
354 rx_sock = pf_socket();
355 tx_sock = pf_socket();
357 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
359 ifindex_in = device_ifindex(ctx->device_in);
360 ifindex_out = device_ifindex(ctx->device_out);
362 size_in = ring_size(ctx->device_in, ctx->reserve_size);
363 size_out = ring_size(ctx->device_out, ctx->reserve_size);
365 enable_kernel_bpf_jit_compiler();
367 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
368 if (ctx->dump_bpf)
369 bpf_dump_all(&bpf_ops);
370 bpf_attach_to_sock(rx_sock, &bpf_ops);
372 ring_rx_setup(&rx_ring, rx_sock, size_in, ifindex_in, &rx_poll, false, ctx->jumbo, ctx->verbose);
373 ring_tx_setup(&tx_ring, tx_sock, size_out, ifindex_out, ctx->jumbo, ctx->verbose);
375 dissector_init_all(ctx->print_mode);
377 if (ctx->promiscuous)
378 ifflags = device_enter_promiscuous_mode(ctx->device_in);
380 if (ctx->kpull)
381 interval = ctx->kpull;
383 set_itimer_interval_value(&itimer, 0, interval);
384 setitimer(ITIMER_REAL, &itimer, NULL);
386 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
388 printf("Running! Hang up with ^C!\n\n");
389 fflush(stdout);
391 while (likely(sigint == 0)) {
392 while (user_may_pull_from_rx(rx_ring.frames[it_in].iov_base)) {
393 hdr_in = rx_ring.frames[it_in].iov_base;
394 in = ((uint8_t *) hdr_in) + hdr_in->tp_h.tp_mac;
396 frame_count++;
398 if (ctx->packet_type != -1)
399 if (ctx->packet_type != hdr_in->s_ll.sll_pkttype)
400 goto next;
402 hdr_out = tx_ring.frames[it_out].iov_base;
403 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
405 while (!user_may_pull_from_tx(tx_ring.frames[it_out].iov_base) &&
406 likely(!sigint)) {
407 if (ctx->randomize)
408 next_rnd_slot(&it_out, &tx_ring);
409 else {
410 it_out++;
411 if (it_out >= tx_ring.layout.tp_frame_nr)
412 it_out = 0;
415 hdr_out = tx_ring.frames[it_out].iov_base;
416 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
419 tpacket_hdr_clone(&hdr_out->tp_h, &hdr_in->tp_h);
420 fmemcpy(out, in, hdr_in->tp_h.tp_len);
422 kernel_may_pull_from_tx(&hdr_out->tp_h);
423 if (ctx->randomize)
424 next_rnd_slot(&it_out, &tx_ring);
425 else {
426 it_out++;
427 if (it_out >= tx_ring.layout.tp_frame_nr)
428 it_out = 0;
431 show_frame_hdr(hdr_in, ctx->print_mode);
433 dissector_entry_point(in, hdr_in->tp_h.tp_snaplen,
434 ctx->link_type, ctx->print_mode);
436 if (frame_count_max != 0) {
437 if (frame_count >= frame_count_max) {
438 sigint = 1;
439 break;
443 next:
444 kernel_may_pull_from_rx(&hdr_in->tp_h);
446 it_in++;
447 if (it_in >= rx_ring.layout.tp_frame_nr)
448 it_in = 0;
450 if (unlikely(sigint == 1))
451 goto out;
454 ret = poll(&rx_poll, 1, -1);
455 if (unlikely(ret < 0)) {
456 if (errno != EINTR)
457 panic("Poll failed!\n");
461 out:
462 timer_purge();
464 sock_rx_net_stats(rx_sock, 0);
466 bpf_release(&bpf_ops);
468 dissector_cleanup_all();
470 destroy_tx_ring(tx_sock, &tx_ring);
471 destroy_rx_ring(rx_sock, &rx_ring);
473 if (ctx->promiscuous)
474 device_leave_promiscuous_mode(ctx->device_in, ifflags);
476 close(tx_sock);
477 close(rx_sock);
480 static void translate_pcap_to_txf(int fdo, uint8_t *out, size_t len)
482 size_t bytes_done = 0;
483 char bout[80];
485 slprintf(bout, sizeof(bout), "{\n ");
486 write_or_die(fdo, bout, strlen(bout));
488 while (bytes_done < len) {
489 slprintf(bout, sizeof(bout), "0x%02x, ", out[bytes_done]);
490 write_or_die(fdo, bout, strlen(bout));
492 bytes_done++;
494 if (bytes_done % 10 == 0) {
495 slprintf(bout, sizeof(bout), "\n");
496 write_or_die(fdo, bout, strlen(bout));
498 if (bytes_done < len) {
499 slprintf(bout, sizeof(bout), " ");
500 write_or_die(fdo, bout, strlen(bout));
504 if (bytes_done % 10 != 0) {
505 slprintf(bout, sizeof(bout), "\n");
506 write_or_die(fdo, bout, strlen(bout));
509 slprintf(bout, sizeof(bout), "}\n\n");
510 write_or_die(fdo, bout, strlen(bout));
513 static void read_pcap(struct ctx *ctx)
515 uint8_t *out;
516 int ret, fd, fdo = 0;
517 unsigned long trunced = 0;
518 size_t out_len;
519 pcap_pkthdr_t phdr;
520 struct sock_fprog bpf_ops;
521 struct frame_map fm;
522 struct timeval start, end, diff;
524 bug_on(!__pcap_io);
526 if (!strncmp("-", ctx->device_in, strlen("-"))) {
527 fd = dup_or_die(fileno(stdin));
528 close(fileno(stdin));
529 if (ctx->pcap == PCAP_OPS_MM)
530 ctx->pcap = PCAP_OPS_SG;
531 } else {
532 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
535 if (__pcap_io->init_once_pcap)
536 __pcap_io->init_once_pcap();
538 ret = __pcap_io->pull_fhdr_pcap(fd, &ctx->magic, &ctx->link_type);
539 if (ret)
540 panic("Error reading pcap header!\n");
542 if (__pcap_io->prepare_access_pcap) {
543 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_RD, ctx->jumbo);
544 if (ret)
545 panic("Error prepare reading pcap!\n");
548 fmemset(&fm, 0, sizeof(fm));
549 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
551 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
552 if (ctx->dump_bpf)
553 bpf_dump_all(&bpf_ops);
555 dissector_init_all(ctx->print_mode);
557 out_len = round_up(1024 * 1024, RUNTIME_PAGE_SIZE);
558 out = xmalloc_aligned(out_len, CO_CACHE_LINE_SIZE);
560 if (ctx->device_out) {
561 if (!strncmp("-", ctx->device_out, strlen("-"))) {
562 fdo = dup_or_die(fileno(stdout));
563 close(fileno(stdout));
564 } else {
565 fdo = open_or_die_m(ctx->device_out, O_RDWR | O_CREAT |
566 O_TRUNC | O_LARGEFILE, DEFFILEMODE);
570 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
572 printf("Running! Hang up with ^C!\n\n");
573 fflush(stdout);
575 bug_on(gettimeofday(&start, NULL));
577 while (likely(sigint == 0)) {
578 do {
579 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic,
580 out, out_len);
581 if (unlikely(ret < 0))
582 goto out;
584 if (unlikely(pcap_get_length(&phdr, ctx->magic) == 0)) {
585 trunced++;
586 continue;
589 if (unlikely(pcap_get_length(&phdr, ctx->magic) > out_len)) {
590 pcap_set_length(&phdr, ctx->magic, out_len);
591 trunced++;
593 } while (ctx->filter &&
594 !bpf_run_filter(&bpf_ops, out,
595 pcap_get_length(&phdr, ctx->magic)));
597 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &fm.tp_h, &fm.s_ll);
599 ctx->tx_bytes += fm.tp_h.tp_len;
600 ctx->tx_packets++;
602 show_frame_hdr(&fm, ctx->print_mode);
604 dissector_entry_point(out, fm.tp_h.tp_snaplen,
605 ctx->link_type, ctx->print_mode);
607 if (ctx->device_out)
608 translate_pcap_to_txf(fdo, out, fm.tp_h.tp_snaplen);
610 if (frame_count_max != 0) {
611 if (ctx->tx_packets >= frame_count_max) {
612 sigint = 1;
613 break;
618 out:
620 bug_on(gettimeofday(&end, NULL));
621 timersub(&end, &start, &diff);
623 bpf_release(&bpf_ops);
625 dissector_cleanup_all();
627 if (__pcap_io->prepare_close_pcap)
628 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
630 xfree(out);
632 fflush(stdout);
633 printf("\n");
634 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
635 printf("\r%12lu packets truncated in file\n", trunced);
636 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
637 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
639 if (!strncmp("-", ctx->device_in, strlen("-")))
640 dup2(fd, fileno(stdin));
641 close(fd);
643 if (ctx->device_out) {
644 if (!strncmp("-", ctx->device_out, strlen("-")))
645 dup2(fdo, fileno(stdout));
646 close(fdo);
650 static void finish_multi_pcap_file(struct ctx *ctx, int fd)
652 __pcap_io->fsync_pcap(fd);
654 if (__pcap_io->prepare_close_pcap)
655 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
657 close(fd);
659 fmemset(&itimer, 0, sizeof(itimer));
660 setitimer(ITIMER_REAL, &itimer, NULL);
663 static int next_multi_pcap_file(struct ctx *ctx, int fd)
665 int ret;
666 char fname[512];
668 __pcap_io->fsync_pcap(fd);
670 if (__pcap_io->prepare_close_pcap)
671 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
673 close(fd);
675 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
676 ctx->prefix ? : "dump-", time(NULL));
678 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
679 O_LARGEFILE, DEFFILEMODE);
681 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
682 if (ret)
683 panic("Error writing pcap header!\n");
685 if (__pcap_io->prepare_access_pcap) {
686 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, true);
687 if (ret)
688 panic("Error prepare writing pcap!\n");
691 return fd;
694 static int begin_multi_pcap_file(struct ctx *ctx)
696 int fd, ret;
697 char fname[256];
699 bug_on(!__pcap_io);
701 if (ctx->device_out[strlen(ctx->device_out) - 1] == '/')
702 ctx->device_out[strlen(ctx->device_out) - 1] = 0;
704 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
705 ctx->prefix ? : "dump-", time(NULL));
707 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
708 O_LARGEFILE, DEFFILEMODE);
710 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
711 if (ret)
712 panic("Error writing pcap header!\n");
714 if (__pcap_io->prepare_access_pcap) {
715 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, true);
716 if (ret)
717 panic("Error prepare writing pcap!\n");
720 if (ctx->dump_mode == DUMP_INTERVAL_TIME) {
721 interval = ctx->dump_interval;
723 set_itimer_interval_value(&itimer, interval, 0);
724 setitimer(ITIMER_REAL, &itimer, NULL);
725 } else {
726 interval = 0;
729 return fd;
732 static void finish_single_pcap_file(struct ctx *ctx, int fd)
734 __pcap_io->fsync_pcap(fd);
736 if (__pcap_io->prepare_close_pcap)
737 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
739 if (strncmp("-", ctx->device_out, strlen("-")))
740 close(fd);
741 else
742 dup2(fd, fileno(stdout));
745 static int begin_single_pcap_file(struct ctx *ctx)
747 int fd, ret;
749 bug_on(!__pcap_io);
751 if (!strncmp("-", ctx->device_out, strlen("-"))) {
752 fd = dup_or_die(fileno(stdout));
753 close(fileno(stdout));
754 if (ctx->pcap == PCAP_OPS_MM)
755 ctx->pcap = PCAP_OPS_SG;
756 } else {
757 fd = open_or_die_m(ctx->device_out,
758 O_RDWR | O_CREAT | O_TRUNC |
759 O_LARGEFILE, DEFFILEMODE);
762 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
763 if (ret)
764 panic("Error writing pcap header!\n");
766 if (__pcap_io->prepare_access_pcap) {
767 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, true);
768 if (ret)
769 panic("Error prepare writing pcap!\n");
772 return fd;
775 static void print_pcap_file_stats(int sock, struct ctx *ctx)
777 int ret;
778 struct tpacket_stats kstats;
779 socklen_t slen = sizeof(kstats);
781 fmemset(&kstats, 0, sizeof(kstats));
783 ret = getsockopt(sock, SOL_PACKET, PACKET_STATISTICS, &kstats, &slen);
784 if (unlikely(ret))
785 panic("Cannot get packet statistics!\n");
787 if (ctx->print_mode == PRINT_NONE) {
788 printf(".(+%u/-%u)", kstats.tp_packets - kstats.tp_drops,
789 kstats.tp_drops);
790 fflush(stdout);
794 static void walk_t3_block(struct block_desc *pbd, struct ctx *ctx,
795 int sock, int *fd, unsigned long *frame_count)
797 int num_pkts = pbd->h1.num_pkts, i;
798 struct tpacket3_hdr *hdr;
799 struct sockaddr_ll *sll;
801 hdr = (void *) ((uint8_t *) pbd + pbd->h1.offset_to_first_pkt);
802 sll = (void *) ((uint8_t *) hdr + TPACKET_ALIGN(sizeof(*hdr)));
804 for (i = 0; i < num_pkts && likely(sigint == 0); ++i) {
805 uint8_t *packet = ((uint8_t *) hdr + hdr->tp_mac);
806 pcap_pkthdr_t phdr;
808 if (ctx->packet_type != -1)
809 if (ctx->packet_type != sll->sll_pkttype)
810 goto next;
812 (*frame_count)++;
814 if (dump_to_pcap(ctx)) {
815 int ret;
817 tpacket3_hdr_to_pcap_pkthdr(hdr, sll, &phdr, ctx->magic);
819 ret = __pcap_io->write_pcap(*fd, &phdr, ctx->magic, packet,
820 pcap_get_length(&phdr, ctx->magic));
821 if (unlikely(ret != (int) pcap_get_total_length(&phdr, ctx->magic)))
822 panic("Write error to pcap!\n");
825 __show_frame_hdr(sll, hdr, ctx->print_mode, true);
827 dissector_entry_point(packet, hdr->tp_snaplen, ctx->link_type,
828 ctx->print_mode);
829 next:
830 hdr = (void *) ((uint8_t *) hdr + hdr->tp_next_offset);
831 sll = (void *) ((uint8_t *) hdr + TPACKET_ALIGN(sizeof(*hdr)));
833 if (frame_count_max != 0) {
834 if (unlikely(*frame_count >= frame_count_max)) {
835 sigint = 1;
836 break;
840 if (dump_to_pcap(ctx)) {
841 if (ctx->dump_mode == DUMP_INTERVAL_SIZE) {
842 interval += hdr->tp_snaplen;
843 if (interval > ctx->dump_interval) {
844 next_dump = true;
845 interval = 0;
849 if (next_dump) {
850 *fd = next_multi_pcap_file(ctx, *fd);
851 next_dump = false;
853 if (unlikely(ctx->verbose))
854 print_pcap_file_stats(sock, ctx);
860 static void recv_only_or_dump(struct ctx *ctx)
862 short ifflags = 0;
863 int sock, ifindex, fd = 0, ret;
864 size_t size;
865 unsigned int it = 0;
866 struct ring rx_ring;
867 struct pollfd rx_poll;
868 struct sock_fprog bpf_ops;
869 struct timeval start, end, diff;
870 struct block_desc *pbd;
871 unsigned long frame_count = 0;
873 sock = pf_socket();
875 if (ctx->rfraw) {
876 ctx->device_trans = xstrdup(ctx->device_in);
877 xfree(ctx->device_in);
879 enter_rfmon_mac80211(ctx->device_trans, &ctx->device_in);
880 ctx->link_type = LINKTYPE_IEEE802_11;
883 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
885 ifindex = device_ifindex(ctx->device_in);
887 size = ring_size(ctx->device_in, ctx->reserve_size);
889 enable_kernel_bpf_jit_compiler();
891 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
892 if (ctx->dump_bpf)
893 bpf_dump_all(&bpf_ops);
894 bpf_attach_to_sock(sock, &bpf_ops);
896 ret = set_sockopt_hwtimestamp(sock, ctx->device_in);
897 if (ret == 0 && ctx->verbose)
898 printf("HW timestamping enabled\n");
900 ring_rx_setup(&rx_ring, sock, size, ifindex, &rx_poll, true, true, ctx->verbose);
902 dissector_init_all(ctx->print_mode);
904 if (ctx->cpu >= 0 && ifindex > 0) {
905 int irq = device_irq_number(ctx->device_in);
906 device_set_irq_affinity(irq, ctx->cpu);
908 if (ctx->verbose)
909 printf("IRQ: %s:%d > CPU%d\n",
910 ctx->device_in, irq, ctx->cpu);
913 if (ctx->promiscuous)
914 ifflags = device_enter_promiscuous_mode(ctx->device_in);
916 if (dump_to_pcap(ctx) && __pcap_io->init_once_pcap)
917 __pcap_io->init_once_pcap();
919 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
921 if (dump_to_pcap(ctx)) {
922 struct stat stats;
924 ret = stat(ctx->device_out, &stats);
925 if (ret < 0)
926 ctx->dump_dir = 0;
927 else
928 ctx->dump_dir = S_ISDIR(stats.st_mode);
930 if (ctx->dump_dir)
931 fd = begin_multi_pcap_file(ctx);
932 else
933 fd = begin_single_pcap_file(ctx);
936 printf("Running! Hang up with ^C!\n\n");
937 fflush(stdout);
939 bug_on(gettimeofday(&start, NULL));
941 while (likely(sigint == 0)) {
942 while (user_may_pull_from_rx_block((pbd = (void *)
943 rx_ring.frames[it].iov_base))) {
944 walk_t3_block(pbd, ctx, sock, &fd, &frame_count);
946 kernel_may_pull_from_rx_block(pbd);
947 it = (it + 1) % rx_ring.layout3.tp_block_nr;
949 if (unlikely(sigint == 1))
950 break;
953 ret = poll(&rx_poll, 1, -1);
954 if (unlikely(ret < 0)) {
955 if (errno != EINTR)
956 panic("Poll failed!\n");
960 bug_on(gettimeofday(&end, NULL));
961 timersub(&end, &start, &diff);
963 if (!(ctx->dump_dir && ctx->print_mode == PRINT_NONE)) {
964 sock_rx_net_stats(sock, frame_count);
966 printf("\r%12lu sec, %lu usec in total\n",
967 diff.tv_sec, diff.tv_usec);
968 } else {
969 printf("\n\n");
970 fflush(stdout);
973 bpf_release(&bpf_ops);
974 dissector_cleanup_all();
975 destroy_rx_ring(sock, &rx_ring);
977 if (ctx->promiscuous)
978 device_leave_promiscuous_mode(ctx->device_in, ifflags);
980 if (ctx->rfraw)
981 leave_rfmon_mac80211(ctx->device_in);
983 if (dump_to_pcap(ctx)) {
984 if (ctx->dump_dir)
985 finish_multi_pcap_file(ctx, fd);
986 else
987 finish_single_pcap_file(ctx, fd);
990 close(sock);
993 static void init_ctx(struct ctx *ctx)
995 memset(ctx, 0, sizeof(*ctx));
996 ctx->uid = getuid();
997 ctx->uid = getgid();
999 ctx->cpu = -1;
1000 ctx->packet_type = -1;
1002 ctx->magic = ORIGINAL_TCPDUMP_MAGIC;
1003 ctx->print_mode = PRINT_NORM;
1004 ctx->pcap = PCAP_OPS_SG;
1006 ctx->dump_mode = DUMP_INTERVAL_TIME;
1007 ctx->dump_interval = 60;
1009 ctx->promiscuous = true;
1010 ctx->randomize = false;
1013 static void destroy_ctx(struct ctx *ctx)
1015 free(ctx->device_in);
1016 free(ctx->device_out);
1017 free(ctx->device_trans);
1019 free(ctx->prefix);
1022 static void __noreturn help(void)
1024 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING);
1025 puts("http://www.netsniff-ng.org\n\n"
1026 "Usage: netsniff-ng [options] [filter-expression]\n"
1027 "Options:\n"
1028 " -i|-d|--dev|--in <dev|pcap|-> Input source as netdev, pcap or pcap stdin\n"
1029 " -o|--out <dev|pcap|dir|cfg|-> Output sink as netdev, pcap, directory, trafgen, or stdout\n"
1030 " -f|--filter <bpf-file|expr> Use BPF filter file from bpfc or tcpdump-like expression\n"
1031 " -t|--type <type> Filter for: host|broadcast|multicast|others|outgoing\n"
1032 " -F|--interval <size|time> Dump interval if -o is a dir: <num>KiB/MiB/GiB/s/sec/min/hrs\n"
1033 " -R|--rfraw Capture or inject raw 802.11 frames\n"
1034 " -n|--num <0|uint> Number of packets until exit (def: 0)\n"
1035 " -P|--prefix <name> Prefix for pcaps stored in directory\n"
1036 " -T|--magic <pcap-magic> Pcap magic number/pcap format to store, see -D\n"
1037 " -D|--dump-pcap-types Dump pcap types and magic numbers and quit\n"
1038 " -B|--dump-bpf Dump generated BPF assembly\n"
1039 " -r|--rand Randomize packet forwarding order (dev->dev)\n"
1040 " -M|--no-promisc No promiscuous mode for netdev\n"
1041 " -A|--no-sock-mem Don't tune core socket memory\n"
1042 " -m|--mmap Mmap(2) pcap file I/O, e.g. for replaying pcaps\n"
1043 " -G|--sg Scatter/gather pcap file I/O\n"
1044 " -c|--clrw Use slower read(2)/write(2) I/O\n"
1045 " -S|--ring-size <size> Specify ring size to: <num>KiB/MiB/GiB\n"
1046 " -k|--kernel-pull <uint> Kernel pull from user interval in us (def: 10us)\n"
1047 " -J|--jumbo-support Support replay/fwd 64KB Super Jumbo Frames (def: 2048B)\n"
1048 " -b|--bind-cpu <cpu> Bind to specific CPU\n"
1049 " -u|--user <userid> Drop privileges and change to userid\n"
1050 " -g|--group <groupid> Drop privileges and change to groupid\n"
1051 " -H|--prio-high Make this high priority process\n"
1052 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
1053 " -s|--silent Do not print captured packets\n"
1054 " -q|--less Print less-verbose packet information\n"
1055 " -X|--hex Print packet data in hex format\n"
1056 " -l|--ascii Print human-readable packet data\n"
1057 " -U|--update Update GeoIP databases\n"
1058 " -V|--verbose Be more verbose\n"
1059 " -v|--version Show version and exit\n"
1060 " -h|--help Guess what?!\n\n"
1061 "Examples:\n"
1062 " netsniff-ng --in eth0 --out dump.pcap -s -T 0xa1b2c3d4 --b 0 tcp or udp\n"
1063 " netsniff-ng --in wlan0 --rfraw --out dump.pcap --silent --bind-cpu 0\n"
1064 " netsniff-ng --in dump.pcap --mmap --out eth0 -k1000 --silent --bind-cpu 0\n"
1065 " netsniff-ng --in dump.pcap --out dump.cfg --silent --bind-cpu 0\n"
1066 " netsniff-ng --in eth0 --out eth1 --silent --bind-cpu 0 -J --type host\n"
1067 " netsniff-ng --in eth1 --out /opt/probe/ -s -m --interval 100MiB -b 0\n"
1068 " netsniff-ng --in vlan0 --out dump.pcap -c -u `id -u bob` -g `id -g bob`\n"
1069 " netsniff-ng --in any --filter http.bpf --jumbo-support --ascii -V\n\n"
1070 "Note:\n"
1071 " For introducing bit errors, delays with random variation and more\n"
1072 " while replaying pcaps, make use of tc(8) with its disciplines (e.g. netem).\n\n"
1073 "Please report bugs to <bugs@netsniff-ng.org>\n"
1074 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1075 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1076 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1077 "Swiss federal institute of technology (ETH Zurich)\n"
1078 "License: GNU GPL version 2.0\n"
1079 "This is free software: you are free to change and redistribute it.\n"
1080 "There is NO WARRANTY, to the extent permitted by law.\n");
1081 die();
1084 static void __noreturn version(void)
1086 printf("\nnetsniff-ng %s, Git id: %s\n", VERSION_LONG, GITVERSION);
1087 puts("the packet sniffing beast\n"
1088 "http://www.netsniff-ng.org\n\n"
1089 "Please report bugs to <bugs@netsniff-ng.org>\n"
1090 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1091 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1092 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1093 "Swiss federal institute of technology (ETH Zurich)\n"
1094 "License: GNU GPL version 2.0\n"
1095 "This is free software: you are free to change and redistribute it.\n"
1096 "There is NO WARRANTY, to the extent permitted by law.\n");
1097 die();
1100 int main(int argc, char **argv)
1102 char *ptr;
1103 int c, i, j, cpu_tmp, opt_index, ops_touched = 0, vals[4] = {0};
1104 bool prio_high = false, setsockmem = true;
1105 void (*main_loop)(struct ctx *ctx) = NULL;
1106 struct ctx ctx;
1108 init_ctx(&ctx);
1109 srand(time(NULL));
1111 while ((c = getopt_long(argc, argv, short_options, long_options,
1112 &opt_index)) != EOF) {
1113 switch (c) {
1114 case 'd':
1115 case 'i':
1116 ctx.device_in = xstrdup(optarg);
1117 break;
1118 case 'o':
1119 ctx.device_out = xstrdup(optarg);
1120 break;
1121 case 'P':
1122 ctx.prefix = xstrdup(optarg);
1123 break;
1124 case 'R':
1125 ctx.link_type = LINKTYPE_IEEE802_11;
1126 ctx.rfraw = 1;
1127 break;
1128 case 'r':
1129 ctx.randomize = true;
1130 break;
1131 case 'J':
1132 ctx.jumbo = true;
1133 break;
1134 case 'T':
1135 ctx.magic = (uint32_t) strtoul(optarg, NULL, 0);
1136 pcap_check_magic(ctx.magic);
1137 break;
1138 case 'f':
1139 ctx.filter = xstrdup(optarg);
1140 break;
1141 case 'M':
1142 ctx.promiscuous = false;
1143 break;
1144 case 'A':
1145 setsockmem = false;
1146 break;
1147 case 'u':
1148 ctx.uid = strtoul(optarg, NULL, 0);
1149 ctx.enforce = true;
1150 break;
1151 case 'g':
1152 ctx.gid = strtoul(optarg, NULL, 0);
1153 ctx.enforce = true;
1154 break;
1155 case 't':
1156 if (!strncmp(optarg, "host", strlen("host")))
1157 ctx.packet_type = PACKET_HOST;
1158 else if (!strncmp(optarg, "broadcast", strlen("broadcast")))
1159 ctx.packet_type = PACKET_BROADCAST;
1160 else if (!strncmp(optarg, "multicast", strlen("multicast")))
1161 ctx.packet_type = PACKET_MULTICAST;
1162 else if (!strncmp(optarg, "others", strlen("others")))
1163 ctx.packet_type = PACKET_OTHERHOST;
1164 else if (!strncmp(optarg, "outgoing", strlen("outgoing")))
1165 ctx.packet_type = PACKET_OUTGOING;
1166 else
1167 ctx.packet_type = -1;
1168 break;
1169 case 'S':
1170 ptr = optarg;
1171 for (j = i = strlen(optarg); i > 0; --i) {
1172 if (!isdigit(optarg[j - i]))
1173 break;
1174 ptr++;
1177 if (!strncmp(ptr, "KiB", strlen("KiB")))
1178 ctx.reserve_size = 1 << 10;
1179 else if (!strncmp(ptr, "MiB", strlen("MiB")))
1180 ctx.reserve_size = 1 << 20;
1181 else if (!strncmp(ptr, "GiB", strlen("GiB")))
1182 ctx.reserve_size = 1 << 30;
1183 else
1184 panic("Syntax error in ring size param!\n");
1186 ctx.reserve_size *= strtoul(optarg, NULL, 0);
1187 break;
1188 case 'b':
1189 cpu_tmp = strtol(optarg, NULL, 0);
1191 cpu_affinity(cpu_tmp);
1192 if (ctx.cpu != -2)
1193 ctx.cpu = cpu_tmp;
1194 break;
1195 case 'H':
1196 prio_high = true;
1197 break;
1198 case 'c':
1199 ctx.pcap = PCAP_OPS_RW;
1200 ops_touched = 1;
1201 break;
1202 case 'm':
1203 ctx.pcap = PCAP_OPS_MM;
1204 ops_touched = 1;
1205 break;
1206 case 'G':
1207 ctx.pcap = PCAP_OPS_SG;
1208 ops_touched = 1;
1209 break;
1210 case 'Q':
1211 ctx.cpu = -2;
1212 break;
1213 case 's':
1214 ctx.print_mode = PRINT_NONE;
1215 break;
1216 case 'q':
1217 ctx.print_mode = PRINT_LESS;
1218 break;
1219 case 'X':
1220 ctx.print_mode =
1221 (ctx.print_mode == PRINT_ASCII) ?
1222 PRINT_HEX_ASCII : PRINT_HEX;
1223 break;
1224 case 'l':
1225 ctx.print_mode =
1226 (ctx.print_mode == PRINT_HEX) ?
1227 PRINT_HEX_ASCII : PRINT_ASCII;
1228 break;
1229 case 'k':
1230 ctx.kpull = strtoul(optarg, NULL, 0);
1231 break;
1232 case 'n':
1233 frame_count_max = strtoul(optarg, NULL, 0);
1234 break;
1235 case 'F':
1236 ptr = optarg;
1237 for (j = i = strlen(optarg); i > 0; --i) {
1238 if (!isdigit(optarg[j - i]))
1239 break;
1240 ptr++;
1243 if (!strncmp(ptr, "KiB", strlen("KiB"))) {
1244 ctx.dump_interval = 1 << 10;
1245 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1246 } else if (!strncmp(ptr, "MiB", strlen("MiB"))) {
1247 ctx.dump_interval = 1 << 20;
1248 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1249 } else if (!strncmp(ptr, "GiB", strlen("GiB"))) {
1250 ctx.dump_interval = 1 << 30;
1251 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1252 } else if (!strncmp(ptr, "sec", strlen("sec"))) {
1253 ctx.dump_interval = 1;
1254 ctx.dump_mode = DUMP_INTERVAL_TIME;
1255 } else if (!strncmp(ptr, "min", strlen("min"))) {
1256 ctx.dump_interval = 60;
1257 ctx.dump_mode = DUMP_INTERVAL_TIME;
1258 } else if (!strncmp(ptr, "hrs", strlen("hrs"))) {
1259 ctx.dump_interval = 60 * 60;
1260 ctx.dump_mode = DUMP_INTERVAL_TIME;
1261 } else if (!strncmp(ptr, "s", strlen("s"))) {
1262 ctx.dump_interval = 1;
1263 ctx.dump_mode = DUMP_INTERVAL_TIME;
1264 } else {
1265 panic("Syntax error in time/size param!\n");
1268 ctx.dump_interval *= strtoul(optarg, NULL, 0);
1269 break;
1270 case 'V':
1271 ctx.verbose = true;
1272 break;
1273 case 'B':
1274 ctx.dump_bpf = true;
1275 break;
1276 case 'D':
1277 pcap_dump_type_features();
1278 die();
1279 break;
1280 case 'U':
1281 update_geoip();
1282 die();
1283 break;
1284 case 'v':
1285 version();
1286 break;
1287 case 'h':
1288 help();
1289 break;
1290 case '?':
1291 switch (optopt) {
1292 case 'd':
1293 case 'i':
1294 case 'o':
1295 case 'f':
1296 case 't':
1297 case 'P':
1298 case 'F':
1299 case 'n':
1300 case 'S':
1301 case 'b':
1302 case 'k':
1303 case 'T':
1304 case 'u':
1305 case 'g':
1306 case 'e':
1307 panic("Option -%c requires an argument!\n",
1308 optopt);
1309 default:
1310 if (isprint(optopt))
1311 printf("Unknown option character `0x%X\'!\n", optopt);
1312 die();
1314 default:
1315 break;
1319 if (!ctx.filter && optind != argc) {
1320 int ret;
1321 off_t offset = 0;
1323 for (i = optind; i < argc; ++i) {
1324 size_t alen = strlen(argv[i]) + 2;
1325 size_t flen = ctx.filter ? strlen(ctx.filter) : 0;
1327 ctx.filter = xrealloc(ctx.filter, 1, flen + alen);
1328 ret = slprintf(ctx.filter + offset, strlen(argv[i]) + 2, "%s ", argv[i]);
1329 if (ret < 0)
1330 panic("Cannot concatenate filter string!\n");
1331 else
1332 offset += ret;
1336 if (!ctx.device_in)
1337 ctx.device_in = xstrdup("any");
1339 register_signal(SIGINT, signal_handler);
1340 register_signal(SIGQUIT, signal_handler);
1341 register_signal(SIGTERM, signal_handler);
1342 register_signal(SIGHUP, signal_handler);
1344 tprintf_init();
1346 if (prio_high) {
1347 set_proc_prio(-20);
1348 set_sched_status(SCHED_FIFO, sched_get_priority_max(SCHED_FIFO));
1351 if (ctx.device_in && (device_mtu(ctx.device_in) ||
1352 !strncmp("any", ctx.device_in, strlen(ctx.device_in)))) {
1353 if (!ctx.rfraw)
1354 ctx.link_type = pcap_devtype_to_linktype(ctx.device_in);
1355 if (!ctx.device_out) {
1356 ctx.dump = 0;
1357 main_loop = recv_only_or_dump;
1358 } else if (device_mtu(ctx.device_out)) {
1359 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1360 main_loop = receive_to_xmit;
1361 } else {
1362 ctx.dump = 1;
1363 register_signal_f(SIGALRM, timer_next_dump, SA_SIGINFO);
1364 main_loop = recv_only_or_dump;
1365 if (!ops_touched)
1366 ctx.pcap = PCAP_OPS_SG;
1368 } else {
1369 if (ctx.device_out && device_mtu(ctx.device_out)) {
1370 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1371 main_loop = pcap_to_xmit;
1372 if (!ops_touched)
1373 ctx.pcap = PCAP_OPS_MM;
1374 } else {
1375 main_loop = read_pcap;
1376 if (!ops_touched)
1377 ctx.pcap = PCAP_OPS_SG;
1381 bug_on(!main_loop);
1383 init_geoip(0);
1384 if (setsockmem)
1385 set_system_socket_memory(vals, array_size(vals));
1386 if (!ctx.enforce)
1387 xlockme();
1389 if (ctx.verbose)
1390 printf("pcap file I/O method: %s\n", pcap_ops_group_to_str[ctx.pcap]);
1392 main_loop(&ctx);
1394 if (!ctx.enforce)
1395 xunlockme();
1396 if (setsockmem)
1397 reset_system_socket_memory(vals, array_size(vals));
1398 destroy_geoip();
1400 device_restore_irq_affinity_list();
1401 tprintf_cleanup();
1403 destroy_ctx(&ctx);
1404 return 0;