all: import netsniff-ng 0.5.8-rc0 source
[netsniff-ng.git] / netsniff-ng.c
blobad5fd04f0477c31a0f8d2f6ee9601a12ca80271a
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 enum dump_mode {
41 DUMP_INTERVAL_TIME,
42 DUMP_INTERVAL_SIZE,
45 struct ctx {
46 char *device_in, *device_out, *device_trans, *filter, *prefix;
47 int cpu, rfraw, dump, print_mode, dump_dir, packet_type, verbose;
48 unsigned long kpull, dump_interval, reserve_size, tx_bytes, tx_packets;
49 bool randomize, promiscuous, enforce, jumbo, dump_bpf;
50 enum pcap_ops_groups pcap; enum dump_mode dump_mode;
51 uid_t uid; gid_t gid; uint32_t link_type, magic;
54 volatile sig_atomic_t sigint = 0;
56 static volatile bool next_dump = false;
58 static const char *short_options = "d:i:o:rf:MJt:S:k:n:b:HQmcsqXlvhF:RGAP:Vu:g:T:DB";
59 static const struct option long_options[] = {
60 {"dev", required_argument, NULL, 'd'},
61 {"in", required_argument, NULL, 'i'},
62 {"out", required_argument, NULL, 'o'},
63 {"filter", required_argument, NULL, 'f'},
64 {"num", required_argument, NULL, 'n'},
65 {"type", required_argument, NULL, 't'},
66 {"interval", required_argument, NULL, 'F'},
67 {"ring-size", required_argument, NULL, 'S'},
68 {"kernel-pull", required_argument, NULL, 'k'},
69 {"bind-cpu", required_argument, NULL, 'b'},
70 {"prefix", required_argument, NULL, 'P'},
71 {"user", required_argument, NULL, 'u'},
72 {"group", required_argument, NULL, 'g'},
73 {"magic", required_argument, NULL, 'T'},
74 {"rand", no_argument, NULL, 'r'},
75 {"rfraw", no_argument, NULL, 'R'},
76 {"mmap", no_argument, NULL, 'm'},
77 {"sg", no_argument, NULL, 'G'},
78 {"clrw", no_argument, NULL, 'c'},
79 {"jumbo-support", no_argument, NULL, 'J'},
80 {"no-promisc", no_argument, NULL, 'M'},
81 {"prio-high", no_argument, NULL, 'H'},
82 {"notouch-irq", no_argument, NULL, 'Q'},
83 {"dump-pcap-types", no_argument, NULL, 'D'},
84 {"dump-bpf", no_argument, NULL, 'B'},
85 {"silent", no_argument, NULL, 's'},
86 {"less", no_argument, NULL, 'q'},
87 {"hex", no_argument, NULL, 'X'},
88 {"ascii", no_argument, NULL, 'l'},
89 {"no-sock-mem", no_argument, NULL, 'A'},
90 {"update", no_argument, NULL, 'U'},
91 {"verbose", no_argument, NULL, 'V'},
92 {"version", no_argument, NULL, 'v'},
93 {"help", no_argument, NULL, 'h'},
94 {NULL, 0, NULL, 0}
97 static int tx_sock;
99 static struct itimerval itimer;
101 static unsigned long frame_count_max = 0, interval = TX_KERNEL_PULL_INT;
103 #define __pcap_io pcap_ops[ctx->pcap]
105 static void signal_handler(int number)
107 switch (number) {
108 case SIGINT:
109 sigint = 1;
110 case SIGHUP:
111 default:
112 break;
116 static void timer_elapsed(int unused)
118 set_itimer_interval_value(&itimer, 0, interval);
119 pull_and_flush_tx_ring(tx_sock);
120 setitimer(ITIMER_REAL, &itimer, NULL);
123 static void timer_next_dump(int unused)
125 set_itimer_interval_value(&itimer, interval, 0);
126 next_dump = true;
127 setitimer(ITIMER_REAL, &itimer, NULL);
130 static inline bool dump_to_pcap(struct ctx *ctx)
132 return ctx->dump;
135 static void pcap_to_xmit(struct ctx *ctx)
137 __label__ out;
138 uint8_t *out = NULL;
139 int irq, ifindex, fd = 0, ret;
140 unsigned int size, it = 0;
141 unsigned long trunced = 0;
142 struct ring tx_ring;
143 struct frame_map *hdr;
144 struct sock_fprog bpf_ops;
145 struct timeval start, end, diff;
146 pcap_pkthdr_t phdr;
148 if (!device_up_and_running(ctx->device_out) && !ctx->rfraw)
149 panic("Device not up and running!\n");
151 bug_on(!__pcap_io);
153 tx_sock = pf_socket();
155 if (!strncmp("-", ctx->device_in, strlen("-"))) {
156 fd = dup(fileno(stdin));
157 close(fileno(stdin));
158 if (ctx->pcap == PCAP_OPS_MM)
159 ctx->pcap = PCAP_OPS_SG;
160 } else {
161 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
164 ret = __pcap_io->pull_fhdr_pcap(fd, &ctx->magic, &ctx->link_type);
165 if (ret)
166 panic("Error reading pcap header!\n");
168 if (__pcap_io->prepare_access_pcap) {
169 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_RD, ctx->jumbo);
170 if (ret)
171 panic("Error prepare reading pcap!\n");
174 fmemset(&tx_ring, 0, sizeof(tx_ring));
175 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
177 if (ctx->rfraw) {
178 ctx->device_trans = xstrdup(ctx->device_out);
179 xfree(ctx->device_out);
181 enter_rfmon_mac80211(ctx->device_trans, &ctx->device_out);
182 if (ctx->link_type != LINKTYPE_IEEE802_11)
183 panic("Wrong linktype of pcap!\n");
186 ifindex = device_ifindex(ctx->device_out);
188 size = ring_size(ctx->device_out, ctx->reserve_size);
190 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
191 if (ctx->dump_bpf)
192 bpf_dump_all(&bpf_ops);
194 set_packet_loss_discard(tx_sock);
195 set_sockopt_hwtimestamp(tx_sock, ctx->device_out);
197 setup_tx_ring_layout(tx_sock, &tx_ring, size, ctx->jumbo);
198 create_tx_ring(tx_sock, &tx_ring, ctx->verbose);
199 mmap_tx_ring(tx_sock, &tx_ring);
200 alloc_tx_ring_frames(&tx_ring);
201 bind_tx_ring(tx_sock, &tx_ring, ifindex);
203 dissector_init_all(ctx->print_mode);
205 if (ctx->cpu >= 0 && ifindex > 0) {
206 irq = device_irq_number(ctx->device_out);
207 device_bind_irq_to_cpu(irq, ctx->cpu);
209 if (ctx->verbose)
210 printf("IRQ: %s:%d > CPU%d\n",
211 ctx->device_out, irq, ctx->cpu);
214 if (ctx->kpull)
215 interval = ctx->kpull;
217 set_itimer_interval_value(&itimer, 0, interval);
218 setitimer(ITIMER_REAL, &itimer, NULL);
220 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
222 printf("Running! Hang up with ^C!\n\n");
223 fflush(stdout);
225 bug_on(gettimeofday(&start, NULL));
227 while (likely(sigint == 0)) {
228 while (user_may_pull_from_tx(tx_ring.frames[it].iov_base)) {
229 hdr = tx_ring.frames[it].iov_base;
230 out = ((uint8_t *) hdr) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
232 do {
233 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic, out,
234 ring_frame_size(&tx_ring));
235 if (unlikely(ret <= 0))
236 goto out;
238 if (ring_frame_size(&tx_ring) <
239 pcap_get_length(&phdr, ctx->magic)) {
240 pcap_set_length(&phdr, ctx->magic,
241 ring_frame_size(&tx_ring));
242 trunced++;
244 } while (ctx->filter &&
245 !bpf_run_filter(&bpf_ops, out,
246 pcap_get_length(&phdr, ctx->magic)));
248 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &hdr->tp_h, &hdr->s_ll);
250 ctx->tx_bytes += hdr->tp_h.tp_len;;
251 ctx->tx_packets++;
253 show_frame_hdr(hdr, ctx->print_mode);
255 dissector_entry_point(out, hdr->tp_h.tp_snaplen,
256 ctx->link_type, ctx->print_mode);
258 kernel_may_pull_from_tx(&hdr->tp_h);
260 it++;
261 if (it >= tx_ring.layout.tp_frame_nr)
262 it = 0;
264 if (unlikely(sigint == 1))
265 break;
267 if (frame_count_max != 0) {
268 if (ctx->tx_packets >= frame_count_max) {
269 sigint = 1;
270 break;
276 out:
278 bug_on(gettimeofday(&end, NULL));
279 timersub(&end, &start, &diff);
281 bpf_release(&bpf_ops);
283 dissector_cleanup_all();
284 destroy_tx_ring(tx_sock, &tx_ring);
286 if (ctx->rfraw)
287 leave_rfmon_mac80211(ctx->device_trans, ctx->device_out);
289 if (__pcap_io->prepare_close_pcap)
290 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
292 if (strncmp("-", ctx->device_in, strlen("-")))
293 close(fd);
294 else
295 dup2(fd, fileno(stdin));
297 close(tx_sock);
299 fflush(stdout);
300 printf("\n");
301 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
302 printf("\r%12lu packets truncated in file\n", trunced);
303 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
304 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
307 static void receive_to_xmit(struct ctx *ctx)
309 short ifflags = 0;
310 uint8_t *in, *out;
311 int rx_sock, ifindex_in, ifindex_out;
312 unsigned int size_in, size_out, it_in = 0, it_out = 0;
313 unsigned long frame_count = 0;
314 struct frame_map *hdr_in, *hdr_out;
315 struct ring tx_ring, rx_ring;
316 struct pollfd rx_poll;
317 struct sock_fprog bpf_ops;
319 if (!strncmp(ctx->device_in, ctx->device_out, IFNAMSIZ))
320 panic("Ingress/egress devices must be different!\n");
321 if (!device_up_and_running(ctx->device_out))
322 panic("Egress device not up and running!\n");
323 if (!device_up_and_running(ctx->device_in))
324 panic("Ingress device not up and running!\n");
326 rx_sock = pf_socket();
327 tx_sock = pf_socket();
329 fmemset(&tx_ring, 0, sizeof(tx_ring));
330 fmemset(&rx_ring, 0, sizeof(rx_ring));
331 fmemset(&rx_poll, 0, sizeof(rx_poll));
332 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
334 ifindex_in = device_ifindex(ctx->device_in);
335 ifindex_out = device_ifindex(ctx->device_out);
337 size_in = ring_size(ctx->device_in, ctx->reserve_size);
338 size_out = ring_size(ctx->device_out, ctx->reserve_size);
340 enable_kernel_bpf_jit_compiler();
342 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
343 if (ctx->dump_bpf)
344 bpf_dump_all(&bpf_ops);
345 bpf_attach_to_sock(rx_sock, &bpf_ops);
347 setup_rx_ring_layout(rx_sock, &rx_ring, size_in, ctx->jumbo);
348 create_rx_ring(rx_sock, &rx_ring, ctx->verbose);
349 mmap_rx_ring(rx_sock, &rx_ring);
350 alloc_rx_ring_frames(&rx_ring);
351 bind_rx_ring(rx_sock, &rx_ring, ifindex_in);
352 prepare_polling(rx_sock, &rx_poll);
354 set_packet_loss_discard(tx_sock);
355 setup_tx_ring_layout(tx_sock, &tx_ring, size_out, ctx->jumbo);
356 create_tx_ring(tx_sock, &tx_ring, ctx->verbose);
357 mmap_tx_ring(tx_sock, &tx_ring);
358 alloc_tx_ring_frames(&tx_ring);
359 bind_tx_ring(tx_sock, &tx_ring, ifindex_out);
361 dissector_init_all(ctx->print_mode);
363 if (ctx->promiscuous)
364 ifflags = enter_promiscuous_mode(ctx->device_in);
366 if (ctx->kpull)
367 interval = ctx->kpull;
369 set_itimer_interval_value(&itimer, 0, interval);
370 setitimer(ITIMER_REAL, &itimer, NULL);
372 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
374 printf("Running! Hang up with ^C!\n\n");
375 fflush(stdout);
377 while (likely(sigint == 0)) {
378 while (user_may_pull_from_rx(rx_ring.frames[it_in].iov_base)) {
379 __label__ next;
381 hdr_in = rx_ring.frames[it_in].iov_base;
382 in = ((uint8_t *) hdr_in) + hdr_in->tp_h.tp_mac;
384 frame_count++;
386 if (ctx->packet_type != -1)
387 if (ctx->packet_type != hdr_in->s_ll.sll_pkttype)
388 goto next;
390 hdr_out = tx_ring.frames[it_out].iov_base;
391 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
393 for (; !user_may_pull_from_tx(tx_ring.frames[it_out].iov_base) &&
394 likely(!sigint);) {
395 if (ctx->randomize)
396 next_rnd_slot(&it_out, &tx_ring);
397 else {
398 it_out++;
399 if (it_out >= tx_ring.layout.tp_frame_nr)
400 it_out = 0;
403 hdr_out = tx_ring.frames[it_out].iov_base;
404 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
407 tpacket_hdr_clone(&hdr_out->tp_h, &hdr_in->tp_h);
408 fmemcpy(out, in, hdr_in->tp_h.tp_len);
410 kernel_may_pull_from_tx(&hdr_out->tp_h);
411 if (ctx->randomize)
412 next_rnd_slot(&it_out, &tx_ring);
413 else {
414 it_out++;
415 if (it_out >= tx_ring.layout.tp_frame_nr)
416 it_out = 0;
419 show_frame_hdr(hdr_in, ctx->print_mode);
421 dissector_entry_point(in, hdr_in->tp_h.tp_snaplen,
422 ctx->link_type, ctx->print_mode);
424 if (frame_count_max != 0) {
425 if (frame_count >= frame_count_max) {
426 sigint = 1;
427 break;
431 next:
433 kernel_may_pull_from_rx(&hdr_in->tp_h);
435 it_in++;
436 if (it_in >= rx_ring.layout.tp_frame_nr)
437 it_in = 0;
439 if (unlikely(sigint == 1))
440 goto out;
443 poll(&rx_poll, 1, -1);
446 out:
448 sock_print_net_stats(rx_sock, 0);
450 bpf_release(&bpf_ops);
452 dissector_cleanup_all();
454 destroy_tx_ring(tx_sock, &tx_ring);
455 destroy_rx_ring(rx_sock, &rx_ring);
457 if (ctx->promiscuous)
458 leave_promiscuous_mode(ctx->device_in, ifflags);
460 close(tx_sock);
461 close(rx_sock);
464 static void translate_pcap_to_txf(int fdo, uint8_t *out, size_t len)
466 size_t bytes_done = 0;
467 char bout[80];
469 slprintf(bout, sizeof(bout), "{\n ");
470 write_or_die(fdo, bout, strlen(bout));
472 while (bytes_done < len) {
473 slprintf(bout, sizeof(bout), "0x%02x, ", out[bytes_done]);
474 write_or_die(fdo, bout, strlen(bout));
476 bytes_done++;
478 if (bytes_done % 10 == 0) {
479 slprintf(bout, sizeof(bout), "\n");
480 write_or_die(fdo, bout, strlen(bout));
482 if (bytes_done < len) {
483 slprintf(bout, sizeof(bout), " ");
484 write_or_die(fdo, bout, strlen(bout));
488 if (bytes_done % 10 != 0) {
489 slprintf(bout, sizeof(bout), "\n");
490 write_or_die(fdo, bout, strlen(bout));
493 slprintf(bout, sizeof(bout), "}\n\n");
494 write_or_die(fdo, bout, strlen(bout));
497 static void read_pcap(struct ctx *ctx)
499 __label__ out;
500 uint8_t *out;
501 int ret, fd, fdo = 0;
502 unsigned long trunced = 0;
503 size_t out_len;
504 pcap_pkthdr_t phdr;
505 struct sock_fprog bpf_ops;
506 struct frame_map fm;
507 struct timeval start, end, diff;
508 struct sockaddr_ll sll;
510 bug_on(!__pcap_io);
512 if (!strncmp("-", ctx->device_in, strlen("-"))) {
513 fd = dup(fileno(stdin));
514 close(fileno(stdin));
515 if (ctx->pcap == PCAP_OPS_MM)
516 ctx->pcap = PCAP_OPS_SG;
517 } else {
518 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
521 ret = __pcap_io->pull_fhdr_pcap(fd, &ctx->magic, &ctx->link_type);
522 if (ret)
523 panic("Error reading pcap header!\n");
525 if (__pcap_io->prepare_access_pcap) {
526 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_RD, ctx->jumbo);
527 if (ret)
528 panic("Error prepare reading pcap!\n");
531 fmemset(&fm, 0, sizeof(fm));
532 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
534 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
535 if (ctx->dump_bpf)
536 bpf_dump_all(&bpf_ops);
538 dissector_init_all(ctx->print_mode);
540 out_len = round_up(1024 * 1024, PAGE_SIZE);
541 out = xmalloc_aligned(out_len, CO_CACHE_LINE_SIZE);
543 if (ctx->device_out) {
544 if (!strncmp("-", ctx->device_out, strlen("-"))) {
545 fdo = dup(fileno(stdout));
546 close(fileno(stdout));
547 } else {
548 fdo = open_or_die_m(ctx->device_out, O_RDWR | O_CREAT |
549 O_TRUNC | O_LARGEFILE, DEFFILEMODE);
553 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
555 printf("Running! Hang up with ^C!\n\n");
556 fflush(stdout);
558 bug_on(gettimeofday(&start, NULL));
560 while (likely(sigint == 0)) {
561 do {
562 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic,
563 out, out_len);
564 if (unlikely(ret < 0))
565 goto out;
567 if (unlikely(pcap_get_length(&phdr, ctx->magic) == 0)) {
568 trunced++;
569 continue;
572 if (unlikely(pcap_get_length(&phdr, ctx->magic) > out_len)) {
573 pcap_set_length(&phdr, ctx->magic, out_len);
574 trunced++;
576 } while (ctx->filter &&
577 !bpf_run_filter(&bpf_ops, out,
578 pcap_get_length(&phdr, ctx->magic)));
580 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &fm.tp_h, &sll);
582 ctx->tx_bytes += fm.tp_h.tp_len;
583 ctx->tx_packets++;
585 show_frame_hdr(&fm, ctx->print_mode);
587 dissector_entry_point(out, fm.tp_h.tp_snaplen,
588 ctx->link_type, ctx->print_mode);
590 if (ctx->device_out)
591 translate_pcap_to_txf(fdo, out, fm.tp_h.tp_snaplen);
593 if (frame_count_max != 0) {
594 if (ctx->tx_packets >= frame_count_max) {
595 sigint = 1;
596 break;
601 out:
603 bug_on(gettimeofday(&end, NULL));
604 timersub(&end, &start, &diff);
606 bpf_release(&bpf_ops);
608 dissector_cleanup_all();
610 if (__pcap_io->prepare_close_pcap)
611 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
613 xfree(out);
615 fflush(stdout);
616 printf("\n");
617 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
618 printf("\r%12lu packets truncated in file\n", trunced);
619 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
620 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
622 if (strncmp("-", ctx->device_in, strlen("-")))
623 close(fd);
624 else
625 dup2(fd, fileno(stdin));
627 if (ctx->device_out) {
628 if (strncmp("-", ctx->device_out, strlen("-")))
629 close(fdo);
630 else
631 dup2(fdo, fileno(stdout));
635 static void finish_multi_pcap_file(struct ctx *ctx, int fd)
637 __pcap_io->fsync_pcap(fd);
639 if (__pcap_io->prepare_close_pcap)
640 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
642 close(fd);
644 fmemset(&itimer, 0, sizeof(itimer));
645 setitimer(ITIMER_REAL, &itimer, NULL);
648 static int next_multi_pcap_file(struct ctx *ctx, int fd)
650 int ret;
651 char fname[512];
653 __pcap_io->fsync_pcap(fd);
655 if (__pcap_io->prepare_close_pcap)
656 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
658 close(fd);
660 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
661 ctx->prefix ? : "dump-", time(0));
663 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
664 O_LARGEFILE, DEFFILEMODE);
666 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
667 if (ret)
668 panic("Error writing pcap header!\n");
670 if (__pcap_io->prepare_access_pcap) {
671 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, ctx->jumbo);
672 if (ret)
673 panic("Error prepare writing pcap!\n");
676 return fd;
679 static int begin_multi_pcap_file(struct ctx *ctx)
681 int fd, ret;
682 char fname[256];
684 bug_on(!__pcap_io);
686 if (ctx->device_out[strlen(ctx->device_out) - 1] == '/')
687 ctx->device_out[strlen(ctx->device_out) - 1] = 0;
689 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
690 ctx->prefix ? : "dump-", time(0));
692 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
693 O_LARGEFILE, DEFFILEMODE);
695 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
696 if (ret)
697 panic("Error writing pcap header!\n");
699 if (__pcap_io->prepare_access_pcap) {
700 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, ctx->jumbo);
701 if (ret)
702 panic("Error prepare writing pcap!\n");
705 if (ctx->dump_mode == DUMP_INTERVAL_TIME) {
706 interval = ctx->dump_interval;
708 set_itimer_interval_value(&itimer, interval, 0);
709 setitimer(ITIMER_REAL, &itimer, NULL);
710 } else {
711 interval = 0;
714 return fd;
717 static void finish_single_pcap_file(struct ctx *ctx, int fd)
719 __pcap_io->fsync_pcap(fd);
721 if (__pcap_io->prepare_close_pcap)
722 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
724 if (strncmp("-", ctx->device_out, strlen("-")))
725 close(fd);
726 else
727 dup2(fd, fileno(stdout));
730 static int begin_single_pcap_file(struct ctx *ctx)
732 int fd, ret;
734 bug_on(!__pcap_io);
736 if (!strncmp("-", ctx->device_out, strlen("-"))) {
737 fd = dup(fileno(stdout));
738 close(fileno(stdout));
739 if (ctx->pcap == PCAP_OPS_MM)
740 ctx->pcap = PCAP_OPS_SG;
741 } else {
742 fd = open_or_die_m(ctx->device_out,
743 O_RDWR | O_CREAT | O_TRUNC |
744 O_LARGEFILE, DEFFILEMODE);
747 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
748 if (ret)
749 panic("Error writing pcap header!\n");
751 if (__pcap_io->prepare_access_pcap) {
752 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, ctx->jumbo);
753 if (ret)
754 panic("Error prepare writing pcap!\n");
757 return fd;
760 static void print_pcap_file_stats(int sock, struct ctx *ctx, unsigned long skipped)
762 unsigned long good, bad;
763 struct tpacket_stats kstats;
764 socklen_t slen = sizeof(kstats);
766 fmemset(&kstats, 0, sizeof(kstats));
767 getsockopt(sock, SOL_PACKET, PACKET_STATISTICS, &kstats, &slen);
769 if (ctx->print_mode == PRINT_NONE) {
770 good = kstats.tp_packets - kstats.tp_drops - skipped;
771 bad = kstats.tp_drops + skipped;
773 printf(".(+%lu/-%lu)", good, bad);
774 fflush(stdout);
778 static void recv_only_or_dump(struct ctx *ctx)
780 uint8_t *packet;
781 short ifflags = 0;
782 int sock, irq, ifindex, fd = 0, ret;
783 unsigned int size, it = 0;
784 unsigned long frame_count = 0, skipped = 0;
785 struct ring rx_ring;
786 struct pollfd rx_poll;
787 struct frame_map *hdr;
788 struct sock_fprog bpf_ops;
789 struct timeval start, end, diff;
790 pcap_pkthdr_t phdr;
792 if (!device_up_and_running(ctx->device_in) && !ctx->rfraw)
793 panic("Device not up and running!\n");
795 sock = pf_socket();
797 if (ctx->rfraw) {
798 ctx->device_trans = xstrdup(ctx->device_in);
799 xfree(ctx->device_in);
801 enter_rfmon_mac80211(ctx->device_trans, &ctx->device_in);
802 ctx->link_type = LINKTYPE_IEEE802_11;
805 fmemset(&rx_ring, 0, sizeof(rx_ring));
806 fmemset(&rx_poll, 0, sizeof(rx_poll));
807 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
809 ifindex = device_ifindex(ctx->device_in);
811 size = ring_size(ctx->device_in, ctx->reserve_size);
813 enable_kernel_bpf_jit_compiler();
815 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
816 if (ctx->dump_bpf)
817 bpf_dump_all(&bpf_ops);
818 bpf_attach_to_sock(sock, &bpf_ops);
820 set_sockopt_hwtimestamp(sock, ctx->device_in);
822 setup_rx_ring_layout(sock, &rx_ring, size, ctx->jumbo);
823 create_rx_ring(sock, &rx_ring, ctx->verbose);
824 mmap_rx_ring(sock, &rx_ring);
825 alloc_rx_ring_frames(&rx_ring);
826 bind_rx_ring(sock, &rx_ring, ifindex);
828 prepare_polling(sock, &rx_poll);
829 dissector_init_all(ctx->print_mode);
831 if (ctx->cpu >= 0 && ifindex > 0) {
832 irq = device_irq_number(ctx->device_in);
833 device_bind_irq_to_cpu(irq, ctx->cpu);
835 if (ctx->verbose)
836 printf("IRQ: %s:%d > CPU%d\n",
837 ctx->device_in, irq, ctx->cpu);
840 if (ctx->promiscuous)
841 ifflags = enter_promiscuous_mode(ctx->device_in);
843 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
845 if (dump_to_pcap(ctx)) {
846 __label__ try_file;
847 struct stat stats;
849 fmemset(&stats, 0, sizeof(stats));
850 ret = stat(ctx->device_out, &stats);
851 if (ret < 0) {
852 ctx->dump_dir = 0;
853 goto try_file;
856 ctx->dump_dir = S_ISDIR(stats.st_mode);
857 if (ctx->dump_dir) {
858 fd = begin_multi_pcap_file(ctx);
859 } else {
860 try_file:
861 fd = begin_single_pcap_file(ctx);
865 printf("Running! Hang up with ^C!\n\n");
866 fflush(stdout);
868 bug_on(gettimeofday(&start, NULL));
870 while (likely(sigint == 0)) {
871 while (user_may_pull_from_rx(rx_ring.frames[it].iov_base)) {
872 __label__ next;
874 hdr = rx_ring.frames[it].iov_base;
875 packet = ((uint8_t *) hdr) + hdr->tp_h.tp_mac;
876 frame_count++;
878 if (ctx->packet_type != -1)
879 if (ctx->packet_type != hdr->s_ll.sll_pkttype)
880 goto next;
882 if (unlikely(ring_frame_size(&rx_ring) < hdr->tp_h.tp_snaplen)) {
883 skipped++;
884 goto next;
887 if (dump_to_pcap(ctx)) {
888 tpacket_hdr_to_pcap_pkthdr(&hdr->tp_h, &hdr->s_ll, &phdr, ctx->magic);
890 ret = __pcap_io->write_pcap(fd, &phdr, ctx->magic, packet,
891 pcap_get_length(&phdr, ctx->magic));
892 if (unlikely(ret != pcap_get_total_length(&phdr, ctx->magic)))
893 panic("Write error to pcap!\n");
896 show_frame_hdr(hdr, ctx->print_mode);
898 dissector_entry_point(packet, hdr->tp_h.tp_snaplen,
899 ctx->link_type, ctx->print_mode);
901 if (frame_count_max != 0) {
902 if (frame_count >= frame_count_max) {
903 sigint = 1;
904 break;
908 next:
910 kernel_may_pull_from_rx(&hdr->tp_h);
912 it++;
913 if (it >= rx_ring.layout.tp_frame_nr)
914 it = 0;
916 if (unlikely(sigint == 1))
917 break;
919 if (dump_to_pcap(ctx)) {
920 if (ctx->dump_mode == DUMP_INTERVAL_SIZE) {
921 interval += hdr->tp_h.tp_snaplen;
923 if (interval > ctx->dump_interval) {
924 next_dump = true;
925 interval = 0;
929 if (next_dump) {
930 fd = next_multi_pcap_file(ctx, fd);
931 next_dump = false;
933 if (ctx->verbose)
934 print_pcap_file_stats(sock, ctx, skipped);
939 poll(&rx_poll, 1, -1);
942 bug_on(gettimeofday(&end, NULL));
943 timersub(&end, &start, &diff);
945 if (!(ctx->dump_dir && ctx->print_mode == PRINT_NONE)) {
946 sock_print_net_stats(sock, skipped);
948 printf("\r%12lu sec, %lu usec in total\n",
949 diff.tv_sec, diff.tv_usec);
950 } else {
951 printf("\n\n");
952 fflush(stdout);
955 bpf_release(&bpf_ops);
956 dissector_cleanup_all();
957 destroy_rx_ring(sock, &rx_ring);
959 if (ctx->promiscuous)
960 leave_promiscuous_mode(ctx->device_in, ifflags);
962 if (ctx->rfraw)
963 leave_rfmon_mac80211(ctx->device_trans, ctx->device_in);
965 if (dump_to_pcap(ctx)) {
966 if (ctx->dump_dir)
967 finish_multi_pcap_file(ctx, fd);
968 else
969 finish_single_pcap_file(ctx, fd);
972 close(sock);
975 static void help(void)
977 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING);
978 puts("http://www.netsniff-ng.org\n\n"
979 "Usage: netsniff-ng [options] [filter-expression]\n"
980 "Options:\n"
981 " -i|-d|--dev|--in <dev|pcap|-> Input source as netdev, pcap or pcap stdin\n"
982 " -o|--out <dev|pcap|dir|cfg|-> Output sink as netdev, pcap, directory, trafgen, or stdout\n"
983 " -f|--filter <bpf-file|expr> Use BPF filter file from bpfc or tcpdump-like expression\n"
984 " -t|--type <type> Filter for: host|broadcast|multicast|others|outgoing\n"
985 " -F|--interval <size|time> Dump interval if -o is a dir: <num>KiB/MiB/GiB/s/sec/min/hrs\n"
986 " -J|--jumbo-support Support for 64KB Super Jumbo Frames (def: 2048B)\n"
987 " -R|--rfraw Capture or inject raw 802.11 frames\n"
988 " -n|--num <0|uint> Number of packets until exit (def: 0)\n"
989 " -P|--prefix <name> Prefix for pcaps stored in directory\n"
990 " -T|--magic <pcap-magic> Pcap magic number/pcap format to store, see -D\n"
991 " -D|--dump-pcap-types Dump pcap types and magic numbers and quit\n"
992 " -B|--dump-bpf Dump generated BPF assembly\n"
993 " -r|--rand Randomize packet forwarding order (dev->dev)\n"
994 " -M|--no-promisc No promiscuous mode for netdev\n"
995 " -A|--no-sock-mem Don't tune core socket memory\n"
996 " -m|--mmap Mmap(2) pcap file i.e., for replaying pcaps\n"
997 " -G|--sg Scatter/gather pcap file I/O\n"
998 " -c|--clrw Use slower read(2)/write(2) I/O\n"
999 " -S|--ring-size <size> Specify ring size to: <num>KiB/MiB/GiB\n"
1000 " -k|--kernel-pull <uint> Kernel pull from user interval in us (def: 10us)\n"
1001 " -b|--bind-cpu <cpu> Bind to specific CPU\n"
1002 " -u|--user <userid> Drop privileges and change to userid\n"
1003 " -g|--group <groupid> Drop privileges and change to groupid\n"
1004 " -H|--prio-high Make this high priority process\n"
1005 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
1006 " -s|--silent Do not print captured packets\n"
1007 " -q|--less Print less-verbose packet information\n"
1008 " -X|--hex Print packet data in hex format\n"
1009 " -l|--ascii Print human-readable packet data\n"
1010 " -U|--update Update GeoIP databases\n"
1011 " -V|--verbose Be more verbose\n"
1012 " -v|--version Show version\n"
1013 " -h|--help Guess what?!\n\n"
1014 "Examples:\n"
1015 " netsniff-ng --in eth0 --out dump.pcap -s -T 0xa1b2c3d4 --b 0 tcp or udp\n"
1016 " netsniff-ng --in wlan0 --rfraw --out dump.pcap --silent --bind-cpu 0\n"
1017 " netsniff-ng --in dump.pcap --mmap --out eth0 -k1000 --silent --bind-cpu 0\n"
1018 " netsniff-ng --in dump.pcap --out dump.cfg --silent --bind-cpu 0\n"
1019 " netsniff-ng --in eth0 --out eth1 --silent --bind-cpu 0 --type host\n"
1020 " netsniff-ng --in eth1 --out /opt/probe/ -s -m -J --interval 100MiB -b 0\n"
1021 " netsniff-ng --in vlan0 --out dump.pcap -c -u `id -u bob` -g `id -g bob`\n"
1022 " netsniff-ng --in any --filter http.bpf --jumbo-support --ascii -V\n\n"
1023 "Note:\n"
1024 " For introducing bit errors, delays with random variation and more\n"
1025 " while replaying pcaps, make use of tc(8) with its disciplines (e.g. netem).\n\n"
1026 "Please report bugs to <bugs@netsniff-ng.org>\n"
1027 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1028 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1029 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1030 "Swiss federal institute of technology (ETH Zurich)\n"
1031 "License: GNU GPL version 2.0\n"
1032 "This is free software: you are free to change and redistribute it.\n"
1033 "There is NO WARRANTY, to the extent permitted by law.\n");
1034 die();
1037 static void version(void)
1039 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING);
1040 puts("http://www.netsniff-ng.org\n\n"
1041 "Please report bugs to <bugs@netsniff-ng.org>\n"
1042 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1043 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1044 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1045 "Swiss federal institute of technology (ETH Zurich)\n"
1046 "License: GNU GPL version 2.0\n"
1047 "This is free software: you are free to change and redistribute it.\n"
1048 "There is NO WARRANTY, to the extent permitted by law.\n");
1049 die();
1052 int main(int argc, char **argv)
1054 char *ptr;
1055 int c, i, j, cpu_tmp, opt_index, ops_touched = 0, vals[4] = {0};
1056 bool prio_high = false, setsockmem = true;
1057 void (*main_loop)(struct ctx *ctx) = NULL;
1058 struct ctx ctx = {
1059 .link_type = LINKTYPE_EN10MB,
1060 .print_mode = PRINT_NORM,
1061 .cpu = -1,
1062 .packet_type = -1,
1063 .promiscuous = true,
1064 .randomize = false,
1065 .pcap = PCAP_OPS_SG,
1066 .dump_interval = 60,
1067 .dump_mode = DUMP_INTERVAL_TIME,
1068 .uid = getuid(),
1069 .gid = getgid(),
1070 .magic = ORIGINAL_TCPDUMP_MAGIC,
1073 srand(time(NULL));
1075 while ((c = getopt_long(argc, argv, short_options, long_options,
1076 &opt_index)) != EOF) {
1077 switch (c) {
1078 case 'd':
1079 case 'i':
1080 ctx.device_in = xstrdup(optarg);
1081 break;
1082 case 'o':
1083 ctx.device_out = xstrdup(optarg);
1084 break;
1085 case 'P':
1086 ctx.prefix = xstrdup(optarg);
1087 break;
1088 case 'R':
1089 ctx.link_type = LINKTYPE_IEEE802_11;
1090 ctx.rfraw = 1;
1091 break;
1092 case 'r':
1093 ctx.randomize = true;
1094 break;
1095 case 'J':
1096 ctx.jumbo = true;
1097 break;
1098 case 'T':
1099 ctx.magic = (uint32_t) strtoul(optarg, NULL, 0);
1100 pcap_check_magic(ctx.magic);
1101 break;
1102 case 'f':
1103 ctx.filter = xstrdup(optarg);
1104 break;
1105 case 'M':
1106 ctx.promiscuous = false;
1107 break;
1108 case 'A':
1109 setsockmem = false;
1110 break;
1111 case 'u':
1112 ctx.uid = strtoul(optarg, NULL, 0);
1113 ctx.enforce = true;
1114 break;
1115 case 'g':
1116 ctx.gid = strtoul(optarg, NULL, 0);
1117 ctx.enforce = true;
1118 break;
1119 case 't':
1120 if (!strncmp(optarg, "host", strlen("host")))
1121 ctx.packet_type = PACKET_HOST;
1122 else if (!strncmp(optarg, "broadcast", strlen("broadcast")))
1123 ctx.packet_type = PACKET_BROADCAST;
1124 else if (!strncmp(optarg, "multicast", strlen("multicast")))
1125 ctx.packet_type = PACKET_MULTICAST;
1126 else if (!strncmp(optarg, "others", strlen("others")))
1127 ctx.packet_type = PACKET_OTHERHOST;
1128 else if (!strncmp(optarg, "outgoing", strlen("outgoing")))
1129 ctx.packet_type = PACKET_OUTGOING;
1130 else
1131 ctx.packet_type = -1;
1132 break;
1133 case 'S':
1134 ptr = optarg;
1135 ctx.reserve_size = 0;
1137 for (j = i = strlen(optarg); i > 0; --i) {
1138 if (!isdigit(optarg[j - i]))
1139 break;
1140 ptr++;
1143 if (!strncmp(ptr, "KiB", strlen("KiB")))
1144 ctx.reserve_size = 1 << 10;
1145 else if (!strncmp(ptr, "MiB", strlen("MiB")))
1146 ctx.reserve_size = 1 << 20;
1147 else if (!strncmp(ptr, "GiB", strlen("GiB")))
1148 ctx.reserve_size = 1 << 30;
1149 else
1150 panic("Syntax error in ring size param!\n");
1151 *ptr = 0;
1153 ctx.reserve_size *= strtol(optarg, NULL, 0);
1154 break;
1155 case 'b':
1156 cpu_tmp = strtol(optarg, NULL, 0);
1158 cpu_affinity(cpu_tmp);
1159 if (ctx.cpu != -2)
1160 ctx.cpu = cpu_tmp;
1161 break;
1162 case 'H':
1163 prio_high = true;
1164 break;
1165 case 'c':
1166 ctx.pcap = PCAP_OPS_RW;
1167 ops_touched = 1;
1168 break;
1169 case 'm':
1170 ctx.pcap = PCAP_OPS_MM;
1171 ops_touched = 1;
1172 break;
1173 case 'G':
1174 ctx.pcap = PCAP_OPS_SG;
1175 ops_touched = 1;
1176 break;
1177 case 'Q':
1178 ctx.cpu = -2;
1179 break;
1180 case 's':
1181 ctx.print_mode = PRINT_NONE;
1182 break;
1183 case 'q':
1184 ctx.print_mode = PRINT_LESS;
1185 break;
1186 case 'X':
1187 ctx.print_mode =
1188 (ctx.print_mode == PRINT_ASCII) ?
1189 PRINT_HEX_ASCII : PRINT_HEX;
1190 break;
1191 case 'l':
1192 ctx.print_mode =
1193 (ctx.print_mode == PRINT_HEX) ?
1194 PRINT_HEX_ASCII : PRINT_ASCII;
1195 break;
1196 case 'k':
1197 ctx.kpull = strtol(optarg, NULL, 0);
1198 break;
1199 case 'n':
1200 frame_count_max = strtol(optarg, NULL, 0);
1201 break;
1202 case 'F':
1203 ptr = optarg;
1204 ctx.dump_interval = 0;
1206 for (j = i = strlen(optarg); i > 0; --i) {
1207 if (!isdigit(optarg[j - i]))
1208 break;
1209 ptr++;
1212 if (!strncmp(ptr, "KiB", strlen("KiB"))) {
1213 ctx.dump_interval = 1 << 10;
1214 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1215 } else if (!strncmp(ptr, "MiB", strlen("MiB"))) {
1216 ctx.dump_interval = 1 << 20;
1217 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1218 } else if (!strncmp(ptr, "GiB", strlen("GiB"))) {
1219 ctx.dump_interval = 1 << 30;
1220 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1221 } else if (!strncmp(ptr, "sec", strlen("sec"))) {
1222 ctx.dump_interval = 1;
1223 ctx.dump_mode = DUMP_INTERVAL_TIME;
1224 } else if (!strncmp(ptr, "min", strlen("min"))) {
1225 ctx.dump_interval = 60;
1226 ctx.dump_mode = DUMP_INTERVAL_TIME;
1227 } else if (!strncmp(ptr, "hrs", strlen("hrs"))) {
1228 ctx.dump_interval = 60 * 60;
1229 ctx.dump_mode = DUMP_INTERVAL_TIME;
1230 } else if (!strncmp(ptr, "s", strlen("s"))) {
1231 ctx.dump_interval = 1;
1232 ctx.dump_mode = DUMP_INTERVAL_TIME;
1233 } else {
1234 panic("Syntax error in time/size param!\n");
1237 *ptr = 0;
1238 ctx.dump_interval *= strtol(optarg, NULL, 0);
1239 break;
1240 case 'V':
1241 ctx.verbose = 1;
1242 break;
1243 case 'B':
1244 ctx.dump_bpf = true;
1245 break;
1246 case 'D':
1247 pcap_dump_type_features();
1248 die();
1249 break;
1250 case 'U':
1251 update_geoip();
1252 die();
1253 break;
1254 case 'v':
1255 version();
1256 break;
1257 case 'h':
1258 help();
1259 break;
1260 case '?':
1261 switch (optopt) {
1262 case 'd':
1263 case 'i':
1264 case 'o':
1265 case 'f':
1266 case 't':
1267 case 'P':
1268 case 'F':
1269 case 'n':
1270 case 'S':
1271 case 'b':
1272 case 'k':
1273 case 'T':
1274 case 'u':
1275 case 'g':
1276 case 'e':
1277 panic("Option -%c requires an argument!\n",
1278 optopt);
1279 default:
1280 if (isprint(optopt))
1281 printf("Unknown option character `0x%X\'!\n", optopt);
1282 die();
1284 default:
1285 break;
1289 if (!ctx.filter && optind != argc) {
1290 int ret;
1291 off_t offset = 0;
1293 for (i = optind; i < argc; ++i) {
1294 size_t alen = strlen(argv[i]) + 2;
1295 size_t flen = ctx.filter ? strlen(ctx.filter) : 0;
1297 ctx.filter = xrealloc(ctx.filter, 1, flen + alen);
1298 ret = slprintf(ctx.filter + offset, strlen(argv[i]) + 2, "%s ", argv[i]);
1299 if (ret < 0)
1300 panic("Cannot concatenate filter string!\n");
1301 else
1302 offset += ret;
1306 if (!ctx.device_in)
1307 ctx.device_in = xstrdup("any");
1309 register_signal(SIGINT, signal_handler);
1310 register_signal(SIGHUP, signal_handler);
1312 tprintf_init();
1314 if (prio_high) {
1315 set_proc_prio(get_default_proc_prio());
1316 set_sched_status(get_default_sched_policy(), get_default_sched_prio());
1319 if (ctx.device_in && (device_mtu(ctx.device_in) ||
1320 !strncmp("any", ctx.device_in, strlen(ctx.device_in)))) {
1321 if (!ctx.device_out) {
1322 ctx.dump = 0;
1323 main_loop = recv_only_or_dump;
1324 } else if (device_mtu(ctx.device_out)) {
1325 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1326 main_loop = receive_to_xmit;
1327 } else {
1328 ctx.dump = 1;
1329 register_signal_f(SIGALRM, timer_next_dump, SA_SIGINFO);
1330 main_loop = recv_only_or_dump;
1331 if (!ops_touched)
1332 ctx.pcap = PCAP_OPS_SG;
1334 } else {
1335 if (ctx.device_out && device_mtu(ctx.device_out)) {
1336 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1337 main_loop = pcap_to_xmit;
1338 if (!ops_touched)
1339 ctx.pcap = PCAP_OPS_MM;
1340 } else {
1341 main_loop = read_pcap;
1342 if (!ops_touched)
1343 ctx.pcap = PCAP_OPS_SG;
1347 bug_on(!main_loop);
1349 init_geoip(0);
1350 if (setsockmem)
1351 set_system_socket_memory(vals, array_size(vals));
1352 xlockme();
1354 main_loop(&ctx);
1356 xunlockme();
1357 if (setsockmem)
1358 reset_system_socket_memory(vals, array_size(vals));
1359 destroy_geoip();
1361 tprintf_cleanup();
1363 free(ctx.device_in);
1364 free(ctx.device_out);
1365 free(ctx.device_trans);
1366 free(ctx.prefix);
1368 return 0;