ifpps: Add __noreturn attribute to exiting functions
[netsniff-ng.git] / netsniff-ng.c
bloba2f4faca2ded668a8a7b97e82b75a592d8987b3e
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 "xutils.h"
30 #include "built_in.h"
31 #include "pcap_io.h"
32 #include "bpf.h"
33 #include "xio.h"
34 #include "die.h"
35 #include "geoip.h"
36 #include "tprintf.h"
37 #include "dissector.h"
38 #include "xmalloc.h"
40 extern int set_sockopt_hwtimestamp(int sock, const char *dev);
42 enum dump_mode {
43 DUMP_INTERVAL_TIME,
44 DUMP_INTERVAL_SIZE,
47 struct ctx {
48 char *device_in, *device_out, *device_trans, *filter, *prefix;
49 int cpu, rfraw, dump, print_mode, dump_dir, packet_type, verbose;
50 unsigned long kpull, dump_interval, reserve_size, tx_bytes, tx_packets;
51 bool randomize, promiscuous, enforce, jumbo, dump_bpf;
52 enum pcap_ops_groups pcap; enum dump_mode dump_mode;
53 uid_t uid; gid_t gid; uint32_t link_type, magic;
56 volatile sig_atomic_t sigint = 0;
58 static volatile bool next_dump = false;
60 static const char *short_options = "d:i:o:rf:MJt:S:k:n:b:HQmcsqXlvhF:RGAP:Vu:g:T:DB";
61 static const struct option long_options[] = {
62 {"dev", required_argument, NULL, 'd'},
63 {"in", required_argument, NULL, 'i'},
64 {"out", required_argument, NULL, 'o'},
65 {"filter", required_argument, NULL, 'f'},
66 {"num", required_argument, NULL, 'n'},
67 {"type", required_argument, NULL, 't'},
68 {"interval", required_argument, NULL, 'F'},
69 {"ring-size", required_argument, NULL, 'S'},
70 {"kernel-pull", required_argument, NULL, 'k'},
71 {"bind-cpu", required_argument, NULL, 'b'},
72 {"prefix", required_argument, NULL, 'P'},
73 {"user", required_argument, NULL, 'u'},
74 {"group", required_argument, NULL, 'g'},
75 {"magic", required_argument, NULL, 'T'},
76 {"rand", no_argument, NULL, 'r'},
77 {"rfraw", no_argument, NULL, 'R'},
78 {"mmap", no_argument, NULL, 'm'},
79 {"sg", no_argument, NULL, 'G'},
80 {"clrw", no_argument, NULL, 'c'},
81 {"jumbo-support", no_argument, NULL, 'J'},
82 {"no-promisc", no_argument, NULL, 'M'},
83 {"prio-high", no_argument, NULL, 'H'},
84 {"notouch-irq", no_argument, NULL, 'Q'},
85 {"dump-pcap-types", no_argument, NULL, 'D'},
86 {"dump-bpf", no_argument, NULL, 'B'},
87 {"silent", no_argument, NULL, 's'},
88 {"less", no_argument, NULL, 'q'},
89 {"hex", no_argument, NULL, 'X'},
90 {"ascii", no_argument, NULL, 'l'},
91 {"no-sock-mem", no_argument, NULL, 'A'},
92 {"update", no_argument, NULL, 'U'},
93 {"verbose", no_argument, NULL, 'V'},
94 {"version", no_argument, NULL, 'v'},
95 {"help", no_argument, NULL, 'h'},
96 {NULL, 0, NULL, 0}
99 static int tx_sock;
101 static struct itimerval itimer;
103 static unsigned long frame_count_max = 0, interval = TX_KERNEL_PULL_INT;
105 #define __pcap_io pcap_ops[ctx->pcap]
107 static void signal_handler(int number)
109 switch (number) {
110 case SIGINT:
111 sigint = 1;
112 case SIGHUP:
113 default:
114 break;
118 static void timer_elapsed(int unused)
120 int ret;
122 set_itimer_interval_value(&itimer, 0, interval);
124 ret = pull_and_flush_tx_ring(tx_sock);
125 if (unlikely(ret < 0)) {
126 /* We could hit EBADF if the socket has been closed before
127 * the timer was triggered.
129 if (errno != EBADF && errno != ENOBUFS)
130 panic("Flushing TX_RING failed: %s!\n", strerror(errno));
133 setitimer(ITIMER_REAL, &itimer, NULL);
136 static void timer_purge(void)
138 int ret;
140 ret = pull_and_flush_tx_ring_wait(tx_sock);
141 if (unlikely(ret < 0)) {
142 if (errno != EBADF && errno != ENOBUFS)
143 panic("Flushing TX_RING failed: %s!\n", strerror(errno));
146 set_itimer_interval_value(&itimer, 0, 0);
147 setitimer(ITIMER_REAL, &itimer, NULL);
151 static void timer_next_dump(int unused)
153 set_itimer_interval_value(&itimer, interval, 0);
154 next_dump = true;
155 setitimer(ITIMER_REAL, &itimer, NULL);
158 static inline bool dump_to_pcap(struct ctx *ctx)
160 return ctx->dump;
163 static void pcap_to_xmit(struct ctx *ctx)
165 __label__ out;
166 uint8_t *out = NULL;
167 int irq, ifindex, fd = 0, ret;
168 unsigned int size, it = 0;
169 unsigned long trunced = 0;
170 struct ring tx_ring;
171 struct frame_map *hdr;
172 struct sock_fprog bpf_ops;
173 struct timeval start, end, diff;
174 pcap_pkthdr_t phdr;
176 if (!device_up_and_running(ctx->device_out) && !ctx->rfraw)
177 panic("Device not up and running!\n");
179 bug_on(!__pcap_io);
181 tx_sock = pf_socket();
183 if (!strncmp("-", ctx->device_in, strlen("-"))) {
184 fd = dup(fileno(stdin));
185 close(fileno(stdin));
186 if (ctx->pcap == PCAP_OPS_MM)
187 ctx->pcap = PCAP_OPS_SG;
188 } else {
189 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
192 if (__pcap_io->init_once_pcap)
193 __pcap_io->init_once_pcap();
195 ret = __pcap_io->pull_fhdr_pcap(fd, &ctx->magic, &ctx->link_type);
196 if (ret)
197 panic("Error reading pcap header!\n");
199 if (__pcap_io->prepare_access_pcap) {
200 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_RD, ctx->jumbo);
201 if (ret)
202 panic("Error prepare reading pcap!\n");
205 fmemset(&tx_ring, 0, sizeof(tx_ring));
206 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
208 if (ctx->rfraw) {
209 ctx->device_trans = xstrdup(ctx->device_out);
210 xfree(ctx->device_out);
212 enter_rfmon_mac80211(ctx->device_trans, &ctx->device_out);
213 if (ctx->link_type != LINKTYPE_IEEE802_11)
214 panic("Wrong linktype of pcap!\n");
217 ifindex = device_ifindex(ctx->device_out);
219 size = ring_size(ctx->device_out, ctx->reserve_size);
221 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
222 if (ctx->dump_bpf)
223 bpf_dump_all(&bpf_ops);
225 set_packet_loss_discard(tx_sock);
227 setup_tx_ring_layout(tx_sock, &tx_ring, size, ctx->jumbo);
228 create_tx_ring(tx_sock, &tx_ring, ctx->verbose);
229 mmap_tx_ring(tx_sock, &tx_ring);
230 alloc_tx_ring_frames(&tx_ring);
231 bind_tx_ring(tx_sock, &tx_ring, ifindex);
233 dissector_init_all(ctx->print_mode);
235 if (ctx->cpu >= 0 && ifindex > 0) {
236 irq = device_irq_number(ctx->device_out);
237 device_bind_irq_to_cpu(irq, ctx->cpu);
239 if (ctx->verbose)
240 printf("IRQ: %s:%d > CPU%d\n",
241 ctx->device_out, irq, ctx->cpu);
244 if (ctx->kpull)
245 interval = ctx->kpull;
247 set_itimer_interval_value(&itimer, 0, interval);
248 setitimer(ITIMER_REAL, &itimer, NULL);
250 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
252 printf("Running! Hang up with ^C!\n\n");
253 fflush(stdout);
255 bug_on(gettimeofday(&start, NULL));
257 while (likely(sigint == 0)) {
258 while (user_may_pull_from_tx(tx_ring.frames[it].iov_base)) {
259 hdr = tx_ring.frames[it].iov_base;
260 out = ((uint8_t *) hdr) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
262 do {
263 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic, out,
264 ring_frame_size(&tx_ring));
265 if (unlikely(ret <= 0))
266 goto out;
268 if (ring_frame_size(&tx_ring) <
269 pcap_get_length(&phdr, ctx->magic)) {
270 pcap_set_length(&phdr, ctx->magic,
271 ring_frame_size(&tx_ring));
272 trunced++;
274 } while (ctx->filter &&
275 !bpf_run_filter(&bpf_ops, out,
276 pcap_get_length(&phdr, ctx->magic)));
278 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &hdr->tp_h, &hdr->s_ll);
280 ctx->tx_bytes += hdr->tp_h.tp_len;;
281 ctx->tx_packets++;
283 show_frame_hdr(hdr, ctx->print_mode);
285 dissector_entry_point(out, hdr->tp_h.tp_snaplen,
286 ctx->link_type, ctx->print_mode);
288 kernel_may_pull_from_tx(&hdr->tp_h);
290 it++;
291 if (it >= tx_ring.layout.tp_frame_nr)
292 it = 0;
294 if (unlikely(sigint == 1))
295 break;
297 if (frame_count_max != 0) {
298 if (ctx->tx_packets >= frame_count_max) {
299 sigint = 1;
300 break;
306 out:
308 bug_on(gettimeofday(&end, NULL));
309 timersub(&end, &start, &diff);
311 timer_purge();
313 bpf_release(&bpf_ops);
315 dissector_cleanup_all();
316 destroy_tx_ring(tx_sock, &tx_ring);
318 if (ctx->rfraw)
319 leave_rfmon_mac80211(ctx->device_trans, ctx->device_out);
321 if (__pcap_io->prepare_close_pcap)
322 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
324 if (!strncmp("-", ctx->device_in, strlen("-")))
325 dup2(fd, fileno(stdin));
326 close(fd);
328 close(tx_sock);
330 fflush(stdout);
331 printf("\n");
332 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
333 printf("\r%12lu packets truncated in file\n", trunced);
334 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
335 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
338 static void receive_to_xmit(struct ctx *ctx)
340 short ifflags = 0;
341 uint8_t *in, *out;
342 int rx_sock, ifindex_in, ifindex_out;
343 unsigned int size_in, size_out, it_in = 0, it_out = 0;
344 unsigned long frame_count = 0;
345 struct frame_map *hdr_in, *hdr_out;
346 struct ring tx_ring, rx_ring;
347 struct pollfd rx_poll;
348 struct sock_fprog bpf_ops;
350 if (!strncmp(ctx->device_in, ctx->device_out, IFNAMSIZ))
351 panic("Ingress/egress devices must be different!\n");
352 if (!device_up_and_running(ctx->device_out))
353 panic("Egress device not up and running!\n");
355 rx_sock = pf_socket();
356 tx_sock = pf_socket();
358 fmemset(&tx_ring, 0, sizeof(tx_ring));
359 fmemset(&rx_ring, 0, sizeof(rx_ring));
360 fmemset(&rx_poll, 0, sizeof(rx_poll));
361 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
363 ifindex_in = device_ifindex(ctx->device_in);
364 ifindex_out = device_ifindex(ctx->device_out);
366 size_in = ring_size(ctx->device_in, ctx->reserve_size);
367 size_out = ring_size(ctx->device_out, ctx->reserve_size);
369 enable_kernel_bpf_jit_compiler();
371 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
372 if (ctx->dump_bpf)
373 bpf_dump_all(&bpf_ops);
374 bpf_attach_to_sock(rx_sock, &bpf_ops);
376 setup_rx_ring_layout(rx_sock, &rx_ring, size_in, ctx->jumbo);
377 create_rx_ring(rx_sock, &rx_ring, ctx->verbose);
378 mmap_rx_ring(rx_sock, &rx_ring);
379 alloc_rx_ring_frames(&rx_ring);
380 bind_rx_ring(rx_sock, &rx_ring, ifindex_in);
381 prepare_polling(rx_sock, &rx_poll);
383 set_packet_loss_discard(tx_sock);
384 setup_tx_ring_layout(tx_sock, &tx_ring, size_out, ctx->jumbo);
385 create_tx_ring(tx_sock, &tx_ring, ctx->verbose);
386 mmap_tx_ring(tx_sock, &tx_ring);
387 alloc_tx_ring_frames(&tx_ring);
388 bind_tx_ring(tx_sock, &tx_ring, ifindex_out);
390 dissector_init_all(ctx->print_mode);
392 if (ctx->promiscuous)
393 ifflags = enter_promiscuous_mode(ctx->device_in);
395 if (ctx->kpull)
396 interval = ctx->kpull;
398 set_itimer_interval_value(&itimer, 0, interval);
399 setitimer(ITIMER_REAL, &itimer, NULL);
401 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
403 printf("Running! Hang up with ^C!\n\n");
404 fflush(stdout);
406 while (likely(sigint == 0)) {
407 while (user_may_pull_from_rx(rx_ring.frames[it_in].iov_base)) {
408 __label__ next;
410 hdr_in = rx_ring.frames[it_in].iov_base;
411 in = ((uint8_t *) hdr_in) + hdr_in->tp_h.tp_mac;
413 frame_count++;
415 if (ctx->packet_type != -1)
416 if (ctx->packet_type != hdr_in->s_ll.sll_pkttype)
417 goto next;
419 hdr_out = tx_ring.frames[it_out].iov_base;
420 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
422 for (; !user_may_pull_from_tx(tx_ring.frames[it_out].iov_base) &&
423 likely(!sigint);) {
424 if (ctx->randomize)
425 next_rnd_slot(&it_out, &tx_ring);
426 else {
427 it_out++;
428 if (it_out >= tx_ring.layout.tp_frame_nr)
429 it_out = 0;
432 hdr_out = tx_ring.frames[it_out].iov_base;
433 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
436 tpacket_hdr_clone(&hdr_out->tp_h, &hdr_in->tp_h);
437 fmemcpy(out, in, hdr_in->tp_h.tp_len);
439 kernel_may_pull_from_tx(&hdr_out->tp_h);
440 if (ctx->randomize)
441 next_rnd_slot(&it_out, &tx_ring);
442 else {
443 it_out++;
444 if (it_out >= tx_ring.layout.tp_frame_nr)
445 it_out = 0;
448 show_frame_hdr(hdr_in, ctx->print_mode);
450 dissector_entry_point(in, hdr_in->tp_h.tp_snaplen,
451 ctx->link_type, ctx->print_mode);
453 if (frame_count_max != 0) {
454 if (frame_count >= frame_count_max) {
455 sigint = 1;
456 break;
460 next:
462 kernel_may_pull_from_rx(&hdr_in->tp_h);
464 it_in++;
465 if (it_in >= rx_ring.layout.tp_frame_nr)
466 it_in = 0;
468 if (unlikely(sigint == 1))
469 goto out;
472 poll(&rx_poll, 1, -1);
475 out:
477 timer_purge();
479 sock_print_net_stats(rx_sock, 0);
481 bpf_release(&bpf_ops);
483 dissector_cleanup_all();
485 destroy_tx_ring(tx_sock, &tx_ring);
486 destroy_rx_ring(rx_sock, &rx_ring);
488 if (ctx->promiscuous)
489 leave_promiscuous_mode(ctx->device_in, ifflags);
491 close(tx_sock);
492 close(rx_sock);
495 static void translate_pcap_to_txf(int fdo, uint8_t *out, size_t len)
497 size_t bytes_done = 0;
498 char bout[80];
500 slprintf(bout, sizeof(bout), "{\n ");
501 write_or_die(fdo, bout, strlen(bout));
503 while (bytes_done < len) {
504 slprintf(bout, sizeof(bout), "0x%02x, ", out[bytes_done]);
505 write_or_die(fdo, bout, strlen(bout));
507 bytes_done++;
509 if (bytes_done % 10 == 0) {
510 slprintf(bout, sizeof(bout), "\n");
511 write_or_die(fdo, bout, strlen(bout));
513 if (bytes_done < len) {
514 slprintf(bout, sizeof(bout), " ");
515 write_or_die(fdo, bout, strlen(bout));
519 if (bytes_done % 10 != 0) {
520 slprintf(bout, sizeof(bout), "\n");
521 write_or_die(fdo, bout, strlen(bout));
524 slprintf(bout, sizeof(bout), "}\n\n");
525 write_or_die(fdo, bout, strlen(bout));
528 static void read_pcap(struct ctx *ctx)
530 __label__ out;
531 uint8_t *out;
532 int ret, fd, fdo = 0;
533 unsigned long trunced = 0;
534 size_t out_len;
535 pcap_pkthdr_t phdr;
536 struct sock_fprog bpf_ops;
537 struct frame_map fm;
538 struct timeval start, end, diff;
539 struct sockaddr_ll sll;
541 bug_on(!__pcap_io);
543 if (!strncmp("-", ctx->device_in, strlen("-"))) {
544 fd = dup(fileno(stdin));
545 close(fileno(stdin));
546 if (ctx->pcap == PCAP_OPS_MM)
547 ctx->pcap = PCAP_OPS_SG;
548 } else {
549 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
552 if (__pcap_io->init_once_pcap)
553 __pcap_io->init_once_pcap();
555 ret = __pcap_io->pull_fhdr_pcap(fd, &ctx->magic, &ctx->link_type);
556 if (ret)
557 panic("Error reading pcap header!\n");
559 if (__pcap_io->prepare_access_pcap) {
560 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_RD, ctx->jumbo);
561 if (ret)
562 panic("Error prepare reading pcap!\n");
565 fmemset(&fm, 0, sizeof(fm));
566 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
568 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
569 if (ctx->dump_bpf)
570 bpf_dump_all(&bpf_ops);
572 dissector_init_all(ctx->print_mode);
574 out_len = round_up(1024 * 1024, PAGE_SIZE);
575 out = xmalloc_aligned(out_len, CO_CACHE_LINE_SIZE);
577 if (ctx->device_out) {
578 if (!strncmp("-", ctx->device_out, strlen("-"))) {
579 fdo = dup(fileno(stdout));
580 close(fileno(stdout));
581 } else {
582 fdo = open_or_die_m(ctx->device_out, O_RDWR | O_CREAT |
583 O_TRUNC | O_LARGEFILE, DEFFILEMODE);
587 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
589 printf("Running! Hang up with ^C!\n\n");
590 fflush(stdout);
592 bug_on(gettimeofday(&start, NULL));
594 while (likely(sigint == 0)) {
595 do {
596 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic,
597 out, out_len);
598 if (unlikely(ret < 0))
599 goto out;
601 if (unlikely(pcap_get_length(&phdr, ctx->magic) == 0)) {
602 trunced++;
603 continue;
606 if (unlikely(pcap_get_length(&phdr, ctx->magic) > out_len)) {
607 pcap_set_length(&phdr, ctx->magic, out_len);
608 trunced++;
610 } while (ctx->filter &&
611 !bpf_run_filter(&bpf_ops, out,
612 pcap_get_length(&phdr, ctx->magic)));
614 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &fm.tp_h, &sll);
616 ctx->tx_bytes += fm.tp_h.tp_len;
617 ctx->tx_packets++;
619 show_frame_hdr(&fm, ctx->print_mode);
621 dissector_entry_point(out, fm.tp_h.tp_snaplen,
622 ctx->link_type, ctx->print_mode);
624 if (ctx->device_out)
625 translate_pcap_to_txf(fdo, out, fm.tp_h.tp_snaplen);
627 if (frame_count_max != 0) {
628 if (ctx->tx_packets >= frame_count_max) {
629 sigint = 1;
630 break;
635 out:
637 bug_on(gettimeofday(&end, NULL));
638 timersub(&end, &start, &diff);
640 bpf_release(&bpf_ops);
642 dissector_cleanup_all();
644 if (__pcap_io->prepare_close_pcap)
645 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
647 xfree(out);
649 fflush(stdout);
650 printf("\n");
651 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
652 printf("\r%12lu packets truncated in file\n", trunced);
653 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
654 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
656 if (!strncmp("-", ctx->device_in, strlen("-")))
657 dup2(fd, fileno(stdin));
658 close(fd);
660 if (ctx->device_out) {
661 if (!strncmp("-", ctx->device_out, strlen("-")))
662 dup2(fdo, fileno(stdout));
663 close(fdo);
667 static void finish_multi_pcap_file(struct ctx *ctx, int fd)
669 __pcap_io->fsync_pcap(fd);
671 if (__pcap_io->prepare_close_pcap)
672 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
674 close(fd);
676 fmemset(&itimer, 0, sizeof(itimer));
677 setitimer(ITIMER_REAL, &itimer, NULL);
680 static int next_multi_pcap_file(struct ctx *ctx, int fd)
682 int ret;
683 char fname[512];
685 __pcap_io->fsync_pcap(fd);
687 if (__pcap_io->prepare_close_pcap)
688 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
690 close(fd);
692 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
693 ctx->prefix ? : "dump-", time(0));
695 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
696 O_LARGEFILE, DEFFILEMODE);
698 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
699 if (ret)
700 panic("Error writing pcap header!\n");
702 if (__pcap_io->prepare_access_pcap) {
703 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, ctx->jumbo);
704 if (ret)
705 panic("Error prepare writing pcap!\n");
708 return fd;
711 static int begin_multi_pcap_file(struct ctx *ctx)
713 int fd, ret;
714 char fname[256];
716 bug_on(!__pcap_io);
718 if (ctx->device_out[strlen(ctx->device_out) - 1] == '/')
719 ctx->device_out[strlen(ctx->device_out) - 1] = 0;
721 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
722 ctx->prefix ? : "dump-", time(0));
724 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
725 O_LARGEFILE, DEFFILEMODE);
727 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
728 if (ret)
729 panic("Error writing pcap header!\n");
731 if (__pcap_io->prepare_access_pcap) {
732 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, ctx->jumbo);
733 if (ret)
734 panic("Error prepare writing pcap!\n");
737 if (ctx->dump_mode == DUMP_INTERVAL_TIME) {
738 interval = ctx->dump_interval;
740 set_itimer_interval_value(&itimer, interval, 0);
741 setitimer(ITIMER_REAL, &itimer, NULL);
742 } else {
743 interval = 0;
746 return fd;
749 static void finish_single_pcap_file(struct ctx *ctx, int fd)
751 __pcap_io->fsync_pcap(fd);
753 if (__pcap_io->prepare_close_pcap)
754 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
756 if (strncmp("-", ctx->device_out, strlen("-")))
757 close(fd);
758 else
759 dup2(fd, fileno(stdout));
762 static int begin_single_pcap_file(struct ctx *ctx)
764 int fd, ret;
766 bug_on(!__pcap_io);
768 if (!strncmp("-", ctx->device_out, strlen("-"))) {
769 fd = dup(fileno(stdout));
770 close(fileno(stdout));
771 if (ctx->pcap == PCAP_OPS_MM)
772 ctx->pcap = PCAP_OPS_SG;
773 } else {
774 fd = open_or_die_m(ctx->device_out,
775 O_RDWR | O_CREAT | O_TRUNC |
776 O_LARGEFILE, DEFFILEMODE);
779 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
780 if (ret)
781 panic("Error writing pcap header!\n");
783 if (__pcap_io->prepare_access_pcap) {
784 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, ctx->jumbo);
785 if (ret)
786 panic("Error prepare writing pcap!\n");
789 return fd;
792 static void print_pcap_file_stats(int sock, struct ctx *ctx, unsigned long skipped)
794 int ret;
795 unsigned long good, bad;
796 struct tpacket_stats kstats;
797 socklen_t slen = sizeof(kstats);
799 fmemset(&kstats, 0, sizeof(kstats));
801 ret = getsockopt(sock, SOL_PACKET, PACKET_STATISTICS, &kstats, &slen);
802 if (unlikely(ret))
803 panic("Cannot get packet statistics!\n");
805 if (ctx->print_mode == PRINT_NONE) {
806 good = kstats.tp_packets - kstats.tp_drops - skipped;
807 bad = kstats.tp_drops + skipped;
809 printf(".(+%lu/-%lu)", good, bad);
810 fflush(stdout);
814 static void recv_only_or_dump(struct ctx *ctx)
816 uint8_t *packet;
817 short ifflags = 0;
818 int sock, irq, ifindex, fd = 0, ret;
819 unsigned int size, it = 0;
820 unsigned long frame_count = 0, skipped = 0;
821 struct ring rx_ring;
822 struct pollfd rx_poll;
823 struct frame_map *hdr;
824 struct sock_fprog bpf_ops;
825 struct timeval start, end, diff;
826 pcap_pkthdr_t phdr;
828 sock = pf_socket();
830 if (ctx->rfraw) {
831 ctx->device_trans = xstrdup(ctx->device_in);
832 xfree(ctx->device_in);
834 enter_rfmon_mac80211(ctx->device_trans, &ctx->device_in);
835 ctx->link_type = LINKTYPE_IEEE802_11;
838 fmemset(&rx_ring, 0, sizeof(rx_ring));
839 fmemset(&rx_poll, 0, sizeof(rx_poll));
840 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
842 ifindex = device_ifindex(ctx->device_in);
844 size = ring_size(ctx->device_in, ctx->reserve_size);
846 enable_kernel_bpf_jit_compiler();
848 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
849 if (ctx->dump_bpf)
850 bpf_dump_all(&bpf_ops);
851 bpf_attach_to_sock(sock, &bpf_ops);
853 set_sockopt_hwtimestamp(sock, ctx->device_in);
855 setup_rx_ring_layout(sock, &rx_ring, size, ctx->jumbo);
856 create_rx_ring(sock, &rx_ring, ctx->verbose);
857 mmap_rx_ring(sock, &rx_ring);
858 alloc_rx_ring_frames(&rx_ring);
859 bind_rx_ring(sock, &rx_ring, ifindex);
861 prepare_polling(sock, &rx_poll);
862 dissector_init_all(ctx->print_mode);
864 if (ctx->cpu >= 0 && ifindex > 0) {
865 irq = device_irq_number(ctx->device_in);
866 device_bind_irq_to_cpu(irq, ctx->cpu);
868 if (ctx->verbose)
869 printf("IRQ: %s:%d > CPU%d\n",
870 ctx->device_in, irq, ctx->cpu);
873 if (ctx->promiscuous)
874 ifflags = enter_promiscuous_mode(ctx->device_in);
876 if (dump_to_pcap(ctx) && __pcap_io->init_once_pcap)
877 __pcap_io->init_once_pcap();
879 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
881 if (dump_to_pcap(ctx)) {
882 __label__ try_file;
883 struct stat stats;
885 fmemset(&stats, 0, sizeof(stats));
886 ret = stat(ctx->device_out, &stats);
887 if (ret < 0) {
888 ctx->dump_dir = 0;
889 goto try_file;
892 ctx->dump_dir = S_ISDIR(stats.st_mode);
893 if (ctx->dump_dir) {
894 fd = begin_multi_pcap_file(ctx);
895 } else {
896 try_file:
897 fd = begin_single_pcap_file(ctx);
901 printf("Running! Hang up with ^C!\n\n");
902 fflush(stdout);
904 bug_on(gettimeofday(&start, NULL));
906 while (likely(sigint == 0)) {
907 while (user_may_pull_from_rx(rx_ring.frames[it].iov_base)) {
908 __label__ next;
910 hdr = rx_ring.frames[it].iov_base;
911 packet = ((uint8_t *) hdr) + hdr->tp_h.tp_mac;
912 frame_count++;
914 if (ctx->packet_type != -1)
915 if (ctx->packet_type != hdr->s_ll.sll_pkttype)
916 goto next;
918 if (unlikely(ring_frame_size(&rx_ring) < hdr->tp_h.tp_snaplen)) {
919 skipped++;
920 goto next;
923 if (dump_to_pcap(ctx)) {
924 tpacket_hdr_to_pcap_pkthdr(&hdr->tp_h, &hdr->s_ll, &phdr, ctx->magic);
926 ret = __pcap_io->write_pcap(fd, &phdr, ctx->magic, packet,
927 pcap_get_length(&phdr, ctx->magic));
928 if (unlikely(ret != pcap_get_total_length(&phdr, ctx->magic)))
929 panic("Write error to pcap!\n");
932 show_frame_hdr(hdr, ctx->print_mode);
934 dissector_entry_point(packet, hdr->tp_h.tp_snaplen,
935 ctx->link_type, ctx->print_mode);
937 if (frame_count_max != 0) {
938 if (frame_count >= frame_count_max) {
939 sigint = 1;
940 break;
944 next:
946 kernel_may_pull_from_rx(&hdr->tp_h);
948 it++;
949 if (it >= rx_ring.layout.tp_frame_nr)
950 it = 0;
952 if (unlikely(sigint == 1))
953 break;
955 if (dump_to_pcap(ctx)) {
956 if (ctx->dump_mode == DUMP_INTERVAL_SIZE) {
957 interval += hdr->tp_h.tp_snaplen;
959 if (interval > ctx->dump_interval) {
960 next_dump = true;
961 interval = 0;
965 if (next_dump) {
966 fd = next_multi_pcap_file(ctx, fd);
967 next_dump = false;
969 if (ctx->verbose)
970 print_pcap_file_stats(sock, ctx, skipped);
975 poll(&rx_poll, 1, -1);
978 bug_on(gettimeofday(&end, NULL));
979 timersub(&end, &start, &diff);
981 if (!(ctx->dump_dir && ctx->print_mode == PRINT_NONE)) {
982 sock_print_net_stats(sock, skipped);
984 printf("\r%12lu sec, %lu usec in total\n",
985 diff.tv_sec, diff.tv_usec);
986 } else {
987 printf("\n\n");
988 fflush(stdout);
991 bpf_release(&bpf_ops);
992 dissector_cleanup_all();
993 destroy_rx_ring(sock, &rx_ring);
995 if (ctx->promiscuous)
996 leave_promiscuous_mode(ctx->device_in, ifflags);
998 if (ctx->rfraw)
999 leave_rfmon_mac80211(ctx->device_trans, ctx->device_in);
1001 if (dump_to_pcap(ctx)) {
1002 if (ctx->dump_dir)
1003 finish_multi_pcap_file(ctx, fd);
1004 else
1005 finish_single_pcap_file(ctx, fd);
1008 close(sock);
1011 static void __noreturn help(void)
1013 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING);
1014 puts("http://www.netsniff-ng.org\n\n"
1015 "Usage: netsniff-ng [options] [filter-expression]\n"
1016 "Options:\n"
1017 " -i|-d|--dev|--in <dev|pcap|-> Input source as netdev, pcap or pcap stdin\n"
1018 " -o|--out <dev|pcap|dir|cfg|-> Output sink as netdev, pcap, directory, trafgen, or stdout\n"
1019 " -f|--filter <bpf-file|expr> Use BPF filter file from bpfc or tcpdump-like expression\n"
1020 " -t|--type <type> Filter for: host|broadcast|multicast|others|outgoing\n"
1021 " -F|--interval <size|time> Dump interval if -o is a dir: <num>KiB/MiB/GiB/s/sec/min/hrs\n"
1022 " -J|--jumbo-support Support for 64KB Super Jumbo Frames (def: 2048B)\n"
1023 " -R|--rfraw Capture or inject raw 802.11 frames\n"
1024 " -n|--num <0|uint> Number of packets until exit (def: 0)\n"
1025 " -P|--prefix <name> Prefix for pcaps stored in directory\n"
1026 " -T|--magic <pcap-magic> Pcap magic number/pcap format to store, see -D\n"
1027 " -D|--dump-pcap-types Dump pcap types and magic numbers and quit\n"
1028 " -B|--dump-bpf Dump generated BPF assembly\n"
1029 " -r|--rand Randomize packet forwarding order (dev->dev)\n"
1030 " -M|--no-promisc No promiscuous mode for netdev\n"
1031 " -A|--no-sock-mem Don't tune core socket memory\n"
1032 " -m|--mmap Mmap(2) pcap file i.e., for replaying pcaps\n"
1033 " -G|--sg Scatter/gather pcap file I/O\n"
1034 " -c|--clrw Use slower read(2)/write(2) I/O\n"
1035 " -S|--ring-size <size> Specify ring size to: <num>KiB/MiB/GiB\n"
1036 " -k|--kernel-pull <uint> Kernel pull from user interval in us (def: 10us)\n"
1037 " -b|--bind-cpu <cpu> Bind to specific CPU\n"
1038 " -u|--user <userid> Drop privileges and change to userid\n"
1039 " -g|--group <groupid> Drop privileges and change to groupid\n"
1040 " -H|--prio-high Make this high priority process\n"
1041 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
1042 " -s|--silent Do not print captured packets\n"
1043 " -q|--less Print less-verbose packet information\n"
1044 " -X|--hex Print packet data in hex format\n"
1045 " -l|--ascii Print human-readable packet data\n"
1046 " -U|--update Update GeoIP databases\n"
1047 " -V|--verbose Be more verbose\n"
1048 " -v|--version Show version and exit\n"
1049 " -h|--help Guess what?!\n\n"
1050 "Examples:\n"
1051 " netsniff-ng --in eth0 --out dump.pcap -s -T 0xa1b2c3d4 --b 0 tcp or udp\n"
1052 " netsniff-ng --in wlan0 --rfraw --out dump.pcap --silent --bind-cpu 0\n"
1053 " netsniff-ng --in dump.pcap --mmap --out eth0 -k1000 --silent --bind-cpu 0\n"
1054 " netsniff-ng --in dump.pcap --out dump.cfg --silent --bind-cpu 0\n"
1055 " netsniff-ng --in eth0 --out eth1 --silent --bind-cpu 0 --type host\n"
1056 " netsniff-ng --in eth1 --out /opt/probe/ -s -m -J --interval 100MiB -b 0\n"
1057 " netsniff-ng --in vlan0 --out dump.pcap -c -u `id -u bob` -g `id -g bob`\n"
1058 " netsniff-ng --in any --filter http.bpf --jumbo-support --ascii -V\n\n"
1059 "Note:\n"
1060 " For introducing bit errors, delays with random variation and more\n"
1061 " while replaying pcaps, make use of tc(8) with its disciplines (e.g. netem).\n\n"
1062 "Please report bugs to <bugs@netsniff-ng.org>\n"
1063 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1064 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1065 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1066 "Swiss federal institute of technology (ETH Zurich)\n"
1067 "License: GNU GPL version 2.0\n"
1068 "This is free software: you are free to change and redistribute it.\n"
1069 "There is NO WARRANTY, to the extent permitted by law.\n");
1070 die();
1073 static void __noreturn version(void)
1075 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING);
1076 puts("http://www.netsniff-ng.org\n\n"
1077 "Please report bugs to <bugs@netsniff-ng.org>\n"
1078 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1079 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1080 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1081 "Swiss federal institute of technology (ETH Zurich)\n"
1082 "License: GNU GPL version 2.0\n"
1083 "This is free software: you are free to change and redistribute it.\n"
1084 "There is NO WARRANTY, to the extent permitted by law.\n");
1085 die();
1088 int main(int argc, char **argv)
1090 char *ptr;
1091 int c, i, j, cpu_tmp, opt_index, ops_touched = 0, vals[4] = {0};
1092 bool prio_high = false, setsockmem = true;
1093 void (*main_loop)(struct ctx *ctx) = NULL;
1094 struct ctx ctx = {
1095 .link_type = LINKTYPE_EN10MB,
1096 .print_mode = PRINT_NORM,
1097 .cpu = -1,
1098 .packet_type = -1,
1099 .promiscuous = true,
1100 .randomize = false,
1101 .pcap = PCAP_OPS_SG,
1102 .dump_interval = 60,
1103 .dump_mode = DUMP_INTERVAL_TIME,
1104 .uid = getuid(),
1105 .gid = getgid(),
1106 .magic = ORIGINAL_TCPDUMP_MAGIC,
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 ctx.reserve_size = 0;
1173 for (j = i = strlen(optarg); i > 0; --i) {
1174 if (!isdigit(optarg[j - i]))
1175 break;
1176 ptr++;
1179 if (!strncmp(ptr, "KiB", strlen("KiB")))
1180 ctx.reserve_size = 1 << 10;
1181 else if (!strncmp(ptr, "MiB", strlen("MiB")))
1182 ctx.reserve_size = 1 << 20;
1183 else if (!strncmp(ptr, "GiB", strlen("GiB")))
1184 ctx.reserve_size = 1 << 30;
1185 else
1186 panic("Syntax error in ring size param!\n");
1187 *ptr = 0;
1189 ctx.reserve_size *= strtol(optarg, NULL, 0);
1190 break;
1191 case 'b':
1192 cpu_tmp = strtol(optarg, NULL, 0);
1194 cpu_affinity(cpu_tmp);
1195 if (ctx.cpu != -2)
1196 ctx.cpu = cpu_tmp;
1197 break;
1198 case 'H':
1199 prio_high = true;
1200 break;
1201 case 'c':
1202 ctx.pcap = PCAP_OPS_RW;
1203 ops_touched = 1;
1204 break;
1205 case 'm':
1206 ctx.pcap = PCAP_OPS_MM;
1207 ops_touched = 1;
1208 break;
1209 case 'G':
1210 ctx.pcap = PCAP_OPS_SG;
1211 ops_touched = 1;
1212 break;
1213 case 'Q':
1214 ctx.cpu = -2;
1215 break;
1216 case 's':
1217 ctx.print_mode = PRINT_NONE;
1218 break;
1219 case 'q':
1220 ctx.print_mode = PRINT_LESS;
1221 break;
1222 case 'X':
1223 ctx.print_mode =
1224 (ctx.print_mode == PRINT_ASCII) ?
1225 PRINT_HEX_ASCII : PRINT_HEX;
1226 break;
1227 case 'l':
1228 ctx.print_mode =
1229 (ctx.print_mode == PRINT_HEX) ?
1230 PRINT_HEX_ASCII : PRINT_ASCII;
1231 break;
1232 case 'k':
1233 ctx.kpull = strtol(optarg, NULL, 0);
1234 break;
1235 case 'n':
1236 frame_count_max = strtol(optarg, NULL, 0);
1237 break;
1238 case 'F':
1239 ptr = optarg;
1240 ctx.dump_interval = 0;
1242 for (j = i = strlen(optarg); i > 0; --i) {
1243 if (!isdigit(optarg[j - i]))
1244 break;
1245 ptr++;
1248 if (!strncmp(ptr, "KiB", strlen("KiB"))) {
1249 ctx.dump_interval = 1 << 10;
1250 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1251 } else if (!strncmp(ptr, "MiB", strlen("MiB"))) {
1252 ctx.dump_interval = 1 << 20;
1253 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1254 } else if (!strncmp(ptr, "GiB", strlen("GiB"))) {
1255 ctx.dump_interval = 1 << 30;
1256 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1257 } else if (!strncmp(ptr, "sec", strlen("sec"))) {
1258 ctx.dump_interval = 1;
1259 ctx.dump_mode = DUMP_INTERVAL_TIME;
1260 } else if (!strncmp(ptr, "min", strlen("min"))) {
1261 ctx.dump_interval = 60;
1262 ctx.dump_mode = DUMP_INTERVAL_TIME;
1263 } else if (!strncmp(ptr, "hrs", strlen("hrs"))) {
1264 ctx.dump_interval = 60 * 60;
1265 ctx.dump_mode = DUMP_INTERVAL_TIME;
1266 } else if (!strncmp(ptr, "s", strlen("s"))) {
1267 ctx.dump_interval = 1;
1268 ctx.dump_mode = DUMP_INTERVAL_TIME;
1269 } else {
1270 panic("Syntax error in time/size param!\n");
1273 *ptr = 0;
1274 ctx.dump_interval *= strtol(optarg, NULL, 0);
1275 break;
1276 case 'V':
1277 ctx.verbose = 1;
1278 break;
1279 case 'B':
1280 ctx.dump_bpf = true;
1281 break;
1282 case 'D':
1283 pcap_dump_type_features();
1284 die();
1285 break;
1286 case 'U':
1287 update_geoip();
1288 die();
1289 break;
1290 case 'v':
1291 version();
1292 break;
1293 case 'h':
1294 help();
1295 break;
1296 case '?':
1297 switch (optopt) {
1298 case 'd':
1299 case 'i':
1300 case 'o':
1301 case 'f':
1302 case 't':
1303 case 'P':
1304 case 'F':
1305 case 'n':
1306 case 'S':
1307 case 'b':
1308 case 'k':
1309 case 'T':
1310 case 'u':
1311 case 'g':
1312 case 'e':
1313 panic("Option -%c requires an argument!\n",
1314 optopt);
1315 default:
1316 if (isprint(optopt))
1317 printf("Unknown option character `0x%X\'!\n", optopt);
1318 die();
1320 default:
1321 break;
1325 if (!ctx.filter && optind != argc) {
1326 int ret;
1327 off_t offset = 0;
1329 for (i = optind; i < argc; ++i) {
1330 size_t alen = strlen(argv[i]) + 2;
1331 size_t flen = ctx.filter ? strlen(ctx.filter) : 0;
1333 ctx.filter = xrealloc(ctx.filter, 1, flen + alen);
1334 ret = slprintf(ctx.filter + offset, strlen(argv[i]) + 2, "%s ", argv[i]);
1335 if (ret < 0)
1336 panic("Cannot concatenate filter string!\n");
1337 else
1338 offset += ret;
1342 if (!ctx.device_in)
1343 ctx.device_in = xstrdup("any");
1345 register_signal(SIGINT, signal_handler);
1346 register_signal(SIGHUP, signal_handler);
1348 tprintf_init();
1350 if (prio_high) {
1351 set_proc_prio(get_default_proc_prio());
1352 set_sched_status(get_default_sched_policy(), get_default_sched_prio());
1355 if (ctx.device_in && (device_mtu(ctx.device_in) ||
1356 !strncmp("any", ctx.device_in, strlen(ctx.device_in)))) {
1357 if (!ctx.device_out) {
1358 ctx.dump = 0;
1359 main_loop = recv_only_or_dump;
1360 } else if (device_mtu(ctx.device_out)) {
1361 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1362 main_loop = receive_to_xmit;
1363 } else {
1364 ctx.dump = 1;
1365 register_signal_f(SIGALRM, timer_next_dump, SA_SIGINFO);
1366 main_loop = recv_only_or_dump;
1367 if (!ops_touched)
1368 ctx.pcap = PCAP_OPS_SG;
1370 } else {
1371 if (ctx.device_out && device_mtu(ctx.device_out)) {
1372 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1373 main_loop = pcap_to_xmit;
1374 if (!ops_touched)
1375 ctx.pcap = PCAP_OPS_MM;
1376 } else {
1377 main_loop = read_pcap;
1378 if (!ops_touched)
1379 ctx.pcap = PCAP_OPS_SG;
1383 bug_on(!main_loop);
1385 init_geoip(0);
1386 if (setsockmem)
1387 set_system_socket_memory(vals, array_size(vals));
1388 if (!ctx.enforce)
1389 xlockme();
1391 main_loop(&ctx);
1393 if (!ctx.enforce)
1394 xunlockme();
1395 if (setsockmem)
1396 reset_system_socket_memory(vals, array_size(vals));
1397 destroy_geoip();
1399 tprintf_cleanup();
1401 free(ctx.device_in);
1402 free(ctx.device_out);
1403 free(ctx.device_trans);
1404 free(ctx.prefix);
1406 return 0;