Corrected spinning loop potential in getrandombytes
[netsniff-ng-old.git] / netsniff-ng.c
blob437c380cd4cda351de1d1bc338ac30439152ce08
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.
7 * "I knew that danger lay ahead, of course; but I did not expect to
8 * meet it in our own Shire. Can't a hobbit walk from the Water to the
9 * River in peace?" "But it is not your own Shire," said Gildor. "Others
10 * dwelt here before hobbits were; and others will dwell here again when
11 * hobbits are no more. The wide world is all about you: you can fence
12 * yourselves in, but you cannot for ever fence it out."
14 * -- The Lord of the Rings, Gildor to Frodo,
15 * Chapter 'Three is Company'.
18 #define _GNU_SOURCE
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <signal.h>
22 #include <getopt.h>
23 #include <ctype.h>
24 #include <time.h>
25 #include <string.h>
26 #include <sys/socket.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <sys/time.h>
30 #include <sys/fsuid.h>
31 #include <unistd.h>
32 #include <stdbool.h>
33 #include <pthread.h>
34 #include <fcntl.h>
36 #include "ring_rx.h"
37 #include "ring_tx.h"
38 #include "mac80211.h"
39 #include "xutils.h"
40 #include "built_in.h"
41 #include "pcap_io.h"
42 #include "bpf.h"
43 #include "xio.h"
44 #include "die.h"
45 #include "geoip.h"
46 #include "tprintf.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, verbose;
58 unsigned long kpull, dump_interval, reserve_size, tx_bytes, tx_packets;
59 bool randomize, promiscuous, enforce, jumbo, dump_bpf;
60 enum pcap_ops_groups pcap; enum dump_mode dump_mode;
61 uid_t uid; gid_t gid; uint32_t link_type, magic;
64 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:DB";
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;
109 static struct itimerval itimer;
111 static unsigned long frame_count_max = 0, interval = TX_KERNEL_PULL_INT;
113 #define __pcap_io pcap_ops[ctx->pcap]
115 static void signal_handler(int number)
117 switch (number) {
118 case SIGINT:
119 sigint = 1;
120 case SIGHUP:
121 default:
122 break;
126 static void timer_elapsed(int unused)
128 set_itimer_interval_value(&itimer, 0, interval);
129 pull_and_flush_tx_ring(tx_sock);
130 setitimer(ITIMER_REAL, &itimer, NULL);
133 static void timer_next_dump(int unused)
135 set_itimer_interval_value(&itimer, interval, 0);
136 next_dump = true;
137 setitimer(ITIMER_REAL, &itimer, NULL);
140 static inline bool dump_to_pcap(struct ctx *ctx)
142 return ctx->dump;
145 static void pcap_to_xmit(struct ctx *ctx)
147 __label__ out;
148 uint8_t *out = NULL;
149 int irq, ifindex, fd = 0, ret;
150 unsigned int size, it = 0;
151 unsigned long trunced = 0;
152 struct ring tx_ring;
153 struct frame_map *hdr;
154 struct sock_fprog bpf_ops;
155 struct timeval start, end, diff;
156 pcap_pkthdr_t phdr;
158 if (!device_up_and_running(ctx->device_out) && !ctx->rfraw)
159 panic("Device not up and running!\n");
161 bug_on(!__pcap_io);
163 tx_sock = pf_socket();
165 if (!strncmp("-", ctx->device_in, strlen("-"))) {
166 fd = dup(fileno(stdin));
167 close(fileno(stdin));
168 if (ctx->pcap == PCAP_OPS_MM)
169 ctx->pcap = PCAP_OPS_SG;
170 } else {
171 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
174 ret = __pcap_io->pull_fhdr_pcap(fd, &ctx->magic, &ctx->link_type);
175 if (ret)
176 panic("Error reading pcap header!\n");
178 if (__pcap_io->prepare_access_pcap) {
179 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_RD, ctx->jumbo);
180 if (ret)
181 panic("Error prepare reading pcap!\n");
184 fmemset(&tx_ring, 0, sizeof(tx_ring));
185 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
187 if (ctx->rfraw) {
188 ctx->device_trans = xstrdup(ctx->device_out);
189 xfree(ctx->device_out);
191 enter_rfmon_mac80211(ctx->device_trans, &ctx->device_out);
192 if (ctx->link_type != LINKTYPE_IEEE802_11)
193 panic("Wrong linktype of pcap!\n");
196 ifindex = device_ifindex(ctx->device_out);
198 size = ring_size(ctx->device_out, ctx->reserve_size);
200 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
201 if (ctx->dump_bpf)
202 bpf_dump_all(&bpf_ops);
204 set_packet_loss_discard(tx_sock);
205 set_sockopt_hwtimestamp(tx_sock, ctx->device_out);
207 setup_tx_ring_layout(tx_sock, &tx_ring, size, ctx->jumbo);
208 create_tx_ring(tx_sock, &tx_ring, ctx->verbose);
209 mmap_tx_ring(tx_sock, &tx_ring);
210 alloc_tx_ring_frames(&tx_ring);
211 bind_tx_ring(tx_sock, &tx_ring, ifindex);
213 dissector_init_all(ctx->print_mode);
215 if (ctx->cpu >= 0 && ifindex > 0) {
216 irq = device_irq_number(ctx->device_out);
217 device_bind_irq_to_cpu(irq, ctx->cpu);
219 if (ctx->verbose)
220 printf("IRQ: %s:%d > CPU%d\n",
221 ctx->device_out, irq, ctx->cpu);
224 if (ctx->kpull)
225 interval = ctx->kpull;
227 set_itimer_interval_value(&itimer, 0, interval);
228 setitimer(ITIMER_REAL, &itimer, NULL);
230 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
232 printf("Running! Hang up with ^C!\n\n");
233 fflush(stdout);
235 bug_on(gettimeofday(&start, NULL));
237 while (likely(sigint == 0)) {
238 while (user_may_pull_from_tx(tx_ring.frames[it].iov_base)) {
239 hdr = tx_ring.frames[it].iov_base;
240 out = ((uint8_t *) hdr) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
242 do {
243 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic, out,
244 ring_frame_size(&tx_ring));
245 if (unlikely(ret <= 0))
246 goto out;
248 if (ring_frame_size(&tx_ring) <
249 pcap_get_length(&phdr, ctx->magic)) {
250 pcap_set_length(&phdr, ctx->magic,
251 ring_frame_size(&tx_ring));
252 trunced++;
254 } while (ctx->filter &&
255 !bpf_run_filter(&bpf_ops, out,
256 pcap_get_length(&phdr, ctx->magic)));
258 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &hdr->tp_h, &hdr->s_ll);
260 ctx->tx_bytes += hdr->tp_h.tp_len;;
261 ctx->tx_packets++;
263 show_frame_hdr(hdr, ctx->print_mode);
265 dissector_entry_point(out, hdr->tp_h.tp_snaplen,
266 ctx->link_type, ctx->print_mode);
268 kernel_may_pull_from_tx(&hdr->tp_h);
270 it++;
271 if (it >= tx_ring.layout.tp_frame_nr)
272 it = 0;
274 if (unlikely(sigint == 1))
275 break;
277 if (frame_count_max != 0) {
278 if (ctx->tx_packets >= frame_count_max) {
279 sigint = 1;
280 break;
286 out:
288 bug_on(gettimeofday(&end, NULL));
289 timersub(&end, &start, &diff);
291 bpf_release(&bpf_ops);
293 dissector_cleanup_all();
294 destroy_tx_ring(tx_sock, &tx_ring);
296 if (ctx->rfraw)
297 leave_rfmon_mac80211(ctx->device_trans, ctx->device_out);
299 if (__pcap_io->prepare_close_pcap)
300 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
302 if (strncmp("-", ctx->device_in, strlen("-")))
303 close(fd);
304 else
305 dup2(fd, fileno(stdin));
307 close(tx_sock);
309 fflush(stdout);
310 printf("\n");
311 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
312 printf("\r%12lu packets truncated in file\n", trunced);
313 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
314 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
317 static void receive_to_xmit(struct ctx *ctx)
319 short ifflags = 0;
320 uint8_t *in, *out;
321 int rx_sock, ifindex_in, ifindex_out;
322 unsigned int size_in, size_out, it_in = 0, it_out = 0;
323 unsigned long frame_count = 0;
324 struct frame_map *hdr_in, *hdr_out;
325 struct ring tx_ring, rx_ring;
326 struct pollfd rx_poll;
327 struct sock_fprog bpf_ops;
329 if (!strncmp(ctx->device_in, ctx->device_out, IFNAMSIZ))
330 panic("Ingress/egress devices must be different!\n");
331 if (!device_up_and_running(ctx->device_out))
332 panic("Egress device not up and running!\n");
333 if (!device_up_and_running(ctx->device_in))
334 panic("Ingress device not up and running!\n");
336 rx_sock = pf_socket();
337 tx_sock = pf_socket();
339 fmemset(&tx_ring, 0, sizeof(tx_ring));
340 fmemset(&rx_ring, 0, sizeof(rx_ring));
341 fmemset(&rx_poll, 0, sizeof(rx_poll));
342 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
344 ifindex_in = device_ifindex(ctx->device_in);
345 ifindex_out = device_ifindex(ctx->device_out);
347 size_in = ring_size(ctx->device_in, ctx->reserve_size);
348 size_out = ring_size(ctx->device_out, ctx->reserve_size);
350 enable_kernel_bpf_jit_compiler();
352 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
353 if (ctx->dump_bpf)
354 bpf_dump_all(&bpf_ops);
355 bpf_attach_to_sock(rx_sock, &bpf_ops);
357 setup_rx_ring_layout(rx_sock, &rx_ring, size_in, ctx->jumbo);
358 create_rx_ring(rx_sock, &rx_ring, ctx->verbose);
359 mmap_rx_ring(rx_sock, &rx_ring);
360 alloc_rx_ring_frames(&rx_ring);
361 bind_rx_ring(rx_sock, &rx_ring, ifindex_in);
362 prepare_polling(rx_sock, &rx_poll);
364 set_packet_loss_discard(tx_sock);
365 setup_tx_ring_layout(tx_sock, &tx_ring, size_out, ctx->jumbo);
366 create_tx_ring(tx_sock, &tx_ring, ctx->verbose);
367 mmap_tx_ring(tx_sock, &tx_ring);
368 alloc_tx_ring_frames(&tx_ring);
369 bind_tx_ring(tx_sock, &tx_ring, ifindex_out);
371 dissector_init_all(ctx->print_mode);
373 if (ctx->promiscuous)
374 ifflags = enter_promiscuous_mode(ctx->device_in);
376 if (ctx->kpull)
377 interval = ctx->kpull;
379 set_itimer_interval_value(&itimer, 0, interval);
380 setitimer(ITIMER_REAL, &itimer, NULL);
382 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
384 printf("Running! Hang up with ^C!\n\n");
385 fflush(stdout);
387 while (likely(sigint == 0)) {
388 while (user_may_pull_from_rx(rx_ring.frames[it_in].iov_base)) {
389 __label__ next;
391 hdr_in = rx_ring.frames[it_in].iov_base;
392 in = ((uint8_t *) hdr_in) + hdr_in->tp_h.tp_mac;
394 frame_count++;
396 if (ctx->packet_type != -1)
397 if (ctx->packet_type != hdr_in->s_ll.sll_pkttype)
398 goto next;
400 hdr_out = tx_ring.frames[it_out].iov_base;
401 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
403 for (; !user_may_pull_from_tx(tx_ring.frames[it_out].iov_base) &&
404 likely(!sigint);) {
405 if (ctx->randomize)
406 next_rnd_slot(&it_out, &tx_ring);
407 else {
408 it_out++;
409 if (it_out >= tx_ring.layout.tp_frame_nr)
410 it_out = 0;
413 hdr_out = tx_ring.frames[it_out].iov_base;
414 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
417 tpacket_hdr_clone(&hdr_out->tp_h, &hdr_in->tp_h);
418 fmemcpy(out, in, hdr_in->tp_h.tp_len);
420 kernel_may_pull_from_tx(&hdr_out->tp_h);
421 if (ctx->randomize)
422 next_rnd_slot(&it_out, &tx_ring);
423 else {
424 it_out++;
425 if (it_out >= tx_ring.layout.tp_frame_nr)
426 it_out = 0;
429 show_frame_hdr(hdr_in, ctx->print_mode);
431 dissector_entry_point(in, hdr_in->tp_h.tp_snaplen,
432 ctx->link_type, ctx->print_mode);
434 if (frame_count_max != 0) {
435 if (frame_count >= frame_count_max) {
436 sigint = 1;
437 break;
441 next:
443 kernel_may_pull_from_rx(&hdr_in->tp_h);
445 it_in++;
446 if (it_in >= rx_ring.layout.tp_frame_nr)
447 it_in = 0;
449 if (unlikely(sigint == 1))
450 goto out;
453 poll(&rx_poll, 1, -1);
456 out:
458 sock_print_net_stats(rx_sock, 0);
460 bpf_release(&bpf_ops);
462 dissector_cleanup_all();
464 destroy_tx_ring(tx_sock, &tx_ring);
465 destroy_rx_ring(rx_sock, &rx_ring);
467 if (ctx->promiscuous)
468 leave_promiscuous_mode(ctx->device_in, ifflags);
470 close(tx_sock);
471 close(rx_sock);
474 static void translate_pcap_to_txf(int fdo, uint8_t *out, size_t len)
476 size_t bytes_done = 0;
477 char bout[80];
479 slprintf(bout, sizeof(bout), "{\n ");
480 write_or_die(fdo, bout, strlen(bout));
482 while (bytes_done < len) {
483 slprintf(bout, sizeof(bout), "0x%02x, ", out[bytes_done]);
484 write_or_die(fdo, bout, strlen(bout));
486 bytes_done++;
488 if (bytes_done % 10 == 0) {
489 slprintf(bout, sizeof(bout), "\n");
490 write_or_die(fdo, bout, strlen(bout));
492 if (bytes_done < len) {
493 slprintf(bout, sizeof(bout), " ");
494 write_or_die(fdo, bout, strlen(bout));
498 if (bytes_done % 10 != 0) {
499 slprintf(bout, sizeof(bout), "\n");
500 write_or_die(fdo, bout, strlen(bout));
503 slprintf(bout, sizeof(bout), "}\n\n");
504 write_or_die(fdo, bout, strlen(bout));
507 static void read_pcap(struct ctx *ctx)
509 __label__ out;
510 uint8_t *out;
511 int ret, fd, fdo = 0;
512 unsigned long trunced = 0;
513 size_t out_len;
514 pcap_pkthdr_t phdr;
515 struct sock_fprog bpf_ops;
516 struct frame_map fm;
517 struct timeval start, end, diff;
518 struct sockaddr_ll sll;
520 bug_on(!__pcap_io);
522 if (!strncmp("-", ctx->device_in, strlen("-"))) {
523 fd = dup(fileno(stdin));
524 close(fileno(stdin));
525 if (ctx->pcap == PCAP_OPS_MM)
526 ctx->pcap = PCAP_OPS_SG;
527 } else {
528 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
531 ret = __pcap_io->pull_fhdr_pcap(fd, &ctx->magic, &ctx->link_type);
532 if (ret)
533 panic("Error reading pcap header!\n");
535 if (__pcap_io->prepare_access_pcap) {
536 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_RD, ctx->jumbo);
537 if (ret)
538 panic("Error prepare reading pcap!\n");
541 fmemset(&fm, 0, sizeof(fm));
542 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
544 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
545 if (ctx->dump_bpf)
546 bpf_dump_all(&bpf_ops);
548 dissector_init_all(ctx->print_mode);
550 out_len = round_up(1024 * 1024, PAGE_SIZE);
551 out = xmalloc_aligned(out_len, CO_CACHE_LINE_SIZE);
553 if (ctx->device_out) {
554 if (!strncmp("-", ctx->device_out, strlen("-"))) {
555 fdo = dup(fileno(stdout));
556 close(fileno(stdout));
557 } else {
558 fdo = open_or_die_m(ctx->device_out, O_RDWR | O_CREAT |
559 O_TRUNC | O_LARGEFILE, DEFFILEMODE);
563 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
565 printf("Running! Hang up with ^C!\n\n");
566 fflush(stdout);
568 bug_on(gettimeofday(&start, NULL));
570 while (likely(sigint == 0)) {
571 do {
572 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic,
573 out, out_len);
574 if (unlikely(ret < 0))
575 goto out;
577 if (unlikely(pcap_get_length(&phdr, ctx->magic) == 0)) {
578 trunced++;
579 continue;
582 if (unlikely(pcap_get_length(&phdr, ctx->magic) > out_len)) {
583 pcap_set_length(&phdr, ctx->magic, out_len);
584 trunced++;
586 } while (ctx->filter &&
587 !bpf_run_filter(&bpf_ops, out,
588 pcap_get_length(&phdr, ctx->magic)));
590 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &fm.tp_h, &sll);
592 ctx->tx_bytes += fm.tp_h.tp_len;
593 ctx->tx_packets++;
595 show_frame_hdr(&fm, ctx->print_mode);
597 dissector_entry_point(out, fm.tp_h.tp_snaplen,
598 ctx->link_type, ctx->print_mode);
600 if (ctx->device_out)
601 translate_pcap_to_txf(fdo, out, fm.tp_h.tp_snaplen);
603 if (frame_count_max != 0) {
604 if (ctx->tx_packets >= frame_count_max) {
605 sigint = 1;
606 break;
611 out:
613 bug_on(gettimeofday(&end, NULL));
614 timersub(&end, &start, &diff);
616 bpf_release(&bpf_ops);
618 dissector_cleanup_all();
620 if (__pcap_io->prepare_close_pcap)
621 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
623 xfree(out);
625 fflush(stdout);
626 printf("\n");
627 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
628 printf("\r%12lu packets truncated in file\n", trunced);
629 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
630 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
632 if (strncmp("-", ctx->device_in, strlen("-")))
633 close(fd);
634 else
635 dup2(fd, fileno(stdin));
637 if (ctx->device_out) {
638 if (strncmp("-", ctx->device_out, strlen("-")))
639 close(fdo);
640 else
641 dup2(fdo, fileno(stdout));
645 static void finish_multi_pcap_file(struct ctx *ctx, int fd)
647 __pcap_io->fsync_pcap(fd);
649 if (__pcap_io->prepare_close_pcap)
650 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
652 close(fd);
654 fmemset(&itimer, 0, sizeof(itimer));
655 setitimer(ITIMER_REAL, &itimer, NULL);
658 static int next_multi_pcap_file(struct ctx *ctx, int fd)
660 int ret;
661 char fname[512];
663 __pcap_io->fsync_pcap(fd);
665 if (__pcap_io->prepare_close_pcap)
666 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
668 close(fd);
670 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
671 ctx->prefix ? : "dump-", time(0));
673 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
674 O_LARGEFILE, DEFFILEMODE);
676 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
677 if (ret)
678 panic("Error writing pcap header!\n");
680 if (__pcap_io->prepare_access_pcap) {
681 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, ctx->jumbo);
682 if (ret)
683 panic("Error prepare writing pcap!\n");
686 return fd;
689 static int begin_multi_pcap_file(struct ctx *ctx)
691 int fd, ret;
692 char fname[256];
694 bug_on(!__pcap_io);
696 if (ctx->device_out[strlen(ctx->device_out) - 1] == '/')
697 ctx->device_out[strlen(ctx->device_out) - 1] = 0;
699 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
700 ctx->prefix ? : "dump-", time(0));
702 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
703 O_LARGEFILE, DEFFILEMODE);
705 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
706 if (ret)
707 panic("Error writing pcap header!\n");
709 if (__pcap_io->prepare_access_pcap) {
710 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, ctx->jumbo);
711 if (ret)
712 panic("Error prepare writing pcap!\n");
715 if (ctx->dump_mode == DUMP_INTERVAL_TIME) {
716 interval = ctx->dump_interval;
718 set_itimer_interval_value(&itimer, interval, 0);
719 setitimer(ITIMER_REAL, &itimer, NULL);
720 } else {
721 interval = 0;
724 return fd;
727 static void finish_single_pcap_file(struct ctx *ctx, int fd)
729 __pcap_io->fsync_pcap(fd);
731 if (__pcap_io->prepare_close_pcap)
732 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
734 if (strncmp("-", ctx->device_out, strlen("-")))
735 close(fd);
736 else
737 dup2(fd, fileno(stdout));
740 static int begin_single_pcap_file(struct ctx *ctx)
742 int fd, ret;
744 bug_on(!__pcap_io);
746 if (!strncmp("-", ctx->device_out, strlen("-"))) {
747 fd = dup(fileno(stdout));
748 close(fileno(stdout));
749 if (ctx->pcap == PCAP_OPS_MM)
750 ctx->pcap = PCAP_OPS_SG;
751 } else {
752 fd = open_or_die_m(ctx->device_out,
753 O_RDWR | O_CREAT | O_TRUNC |
754 O_LARGEFILE, DEFFILEMODE);
757 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
758 if (ret)
759 panic("Error writing pcap header!\n");
761 if (__pcap_io->prepare_access_pcap) {
762 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, ctx->jumbo);
763 if (ret)
764 panic("Error prepare writing pcap!\n");
767 return fd;
770 static void print_pcap_file_stats(int sock, struct ctx *ctx, unsigned long skipped)
772 unsigned long good, bad;
773 struct tpacket_stats kstats;
774 socklen_t slen = sizeof(kstats);
776 fmemset(&kstats, 0, sizeof(kstats));
777 getsockopt(sock, SOL_PACKET, PACKET_STATISTICS, &kstats, &slen);
779 if (ctx->print_mode == PRINT_NONE) {
780 good = kstats.tp_packets - kstats.tp_drops - skipped;
781 bad = kstats.tp_drops + skipped;
783 printf(".(+%lu/-%lu)", good, bad);
784 fflush(stdout);
788 static void recv_only_or_dump(struct ctx *ctx)
790 uint8_t *packet;
791 short ifflags = 0;
792 int sock, irq, ifindex, fd = 0, ret;
793 unsigned int size, it = 0;
794 unsigned long frame_count = 0, skipped = 0;
795 struct ring rx_ring;
796 struct pollfd rx_poll;
797 struct frame_map *hdr;
798 struct sock_fprog bpf_ops;
799 struct timeval start, end, diff;
800 pcap_pkthdr_t phdr;
802 if (!device_up_and_running(ctx->device_in) && !ctx->rfraw)
803 panic("Device not up and running!\n");
805 sock = pf_socket();
807 if (ctx->rfraw) {
808 ctx->device_trans = xstrdup(ctx->device_in);
809 xfree(ctx->device_in);
811 enter_rfmon_mac80211(ctx->device_trans, &ctx->device_in);
812 ctx->link_type = LINKTYPE_IEEE802_11;
815 fmemset(&rx_ring, 0, sizeof(rx_ring));
816 fmemset(&rx_poll, 0, sizeof(rx_poll));
817 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
819 ifindex = device_ifindex(ctx->device_in);
821 size = ring_size(ctx->device_in, ctx->reserve_size);
823 enable_kernel_bpf_jit_compiler();
825 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
826 if (ctx->dump_bpf)
827 bpf_dump_all(&bpf_ops);
828 bpf_attach_to_sock(sock, &bpf_ops);
830 set_sockopt_hwtimestamp(sock, ctx->device_in);
832 setup_rx_ring_layout(sock, &rx_ring, size, ctx->jumbo);
833 create_rx_ring(sock, &rx_ring, ctx->verbose);
834 mmap_rx_ring(sock, &rx_ring);
835 alloc_rx_ring_frames(&rx_ring);
836 bind_rx_ring(sock, &rx_ring, ifindex);
838 prepare_polling(sock, &rx_poll);
839 dissector_init_all(ctx->print_mode);
841 if (ctx->cpu >= 0 && ifindex > 0) {
842 irq = device_irq_number(ctx->device_in);
843 device_bind_irq_to_cpu(irq, ctx->cpu);
845 if (ctx->verbose)
846 printf("IRQ: %s:%d > CPU%d\n",
847 ctx->device_in, irq, ctx->cpu);
850 if (ctx->promiscuous)
851 ifflags = enter_promiscuous_mode(ctx->device_in);
853 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
855 if (dump_to_pcap(ctx)) {
856 __label__ try_file;
857 struct stat stats;
859 fmemset(&stats, 0, sizeof(stats));
860 ret = stat(ctx->device_out, &stats);
861 if (ret < 0) {
862 ctx->dump_dir = 0;
863 goto try_file;
866 ctx->dump_dir = S_ISDIR(stats.st_mode);
867 if (ctx->dump_dir) {
868 fd = begin_multi_pcap_file(ctx);
869 } else {
870 try_file:
871 fd = begin_single_pcap_file(ctx);
875 printf("Running! Hang up with ^C!\n\n");
876 fflush(stdout);
878 bug_on(gettimeofday(&start, NULL));
880 while (likely(sigint == 0)) {
881 while (user_may_pull_from_rx(rx_ring.frames[it].iov_base)) {
882 __label__ next;
884 hdr = rx_ring.frames[it].iov_base;
885 packet = ((uint8_t *) hdr) + hdr->tp_h.tp_mac;
886 frame_count++;
888 if (ctx->packet_type != -1)
889 if (ctx->packet_type != hdr->s_ll.sll_pkttype)
890 goto next;
892 if (unlikely(ring_frame_size(&rx_ring) < hdr->tp_h.tp_snaplen)) {
893 skipped++;
894 goto next;
897 if (dump_to_pcap(ctx)) {
898 tpacket_hdr_to_pcap_pkthdr(&hdr->tp_h, &hdr->s_ll, &phdr, ctx->magic);
900 ret = __pcap_io->write_pcap(fd, &phdr, ctx->magic, packet,
901 pcap_get_length(&phdr, ctx->magic));
902 if (unlikely(ret != pcap_get_total_length(&phdr, ctx->magic)))
903 panic("Write error to pcap!\n");
906 show_frame_hdr(hdr, ctx->print_mode);
908 dissector_entry_point(packet, hdr->tp_h.tp_snaplen,
909 ctx->link_type, ctx->print_mode);
911 if (frame_count_max != 0) {
912 if (frame_count >= frame_count_max) {
913 sigint = 1;
914 break;
918 next:
920 kernel_may_pull_from_rx(&hdr->tp_h);
922 it++;
923 if (it >= rx_ring.layout.tp_frame_nr)
924 it = 0;
926 if (unlikely(sigint == 1))
927 break;
929 if (dump_to_pcap(ctx)) {
930 if (ctx->dump_mode == DUMP_INTERVAL_SIZE) {
931 interval += hdr->tp_h.tp_snaplen;
933 if (interval > ctx->dump_interval) {
934 next_dump = true;
935 interval = 0;
939 if (next_dump) {
940 fd = next_multi_pcap_file(ctx, fd);
941 next_dump = false;
943 if (ctx->verbose)
944 print_pcap_file_stats(sock, ctx, skipped);
949 poll(&rx_poll, 1, -1);
952 bug_on(gettimeofday(&end, NULL));
953 timersub(&end, &start, &diff);
955 if (!(ctx->dump_dir && ctx->print_mode == PRINT_NONE)) {
956 sock_print_net_stats(sock, skipped);
958 printf("\r%12lu sec, %lu usec in total\n",
959 diff.tv_sec, diff.tv_usec);
960 } else {
961 printf("\n\n");
962 fflush(stdout);
965 bpf_release(&bpf_ops);
966 dissector_cleanup_all();
967 destroy_rx_ring(sock, &rx_ring);
969 if (ctx->promiscuous)
970 leave_promiscuous_mode(ctx->device_in, ifflags);
972 if (ctx->rfraw)
973 leave_rfmon_mac80211(ctx->device_trans, ctx->device_in);
975 if (dump_to_pcap(ctx)) {
976 if (ctx->dump_dir)
977 finish_multi_pcap_file(ctx, fd);
978 else
979 finish_single_pcap_file(ctx, fd);
982 close(sock);
985 static void help(void)
987 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING);
988 puts("http://www.netsniff-ng.org\n\n"
989 "Usage: netsniff-ng [options] [filter-expression]\n"
990 "Options:\n"
991 " -i|-d|--dev|--in <dev|pcap|-> Input source as netdev, pcap or pcap stdin\n"
992 " -o|--out <dev|pcap|dir|cfg|-> Output sink as netdev, pcap, directory, trafgen, or stdout\n"
993 " -f|--filter <bpf-file|expr> Use BPF filter file from bpfc or tcpdump-like expression\n"
994 " -t|--type <type> Filter for: host|broadcast|multicast|others|outgoing\n"
995 " -F|--interval <size|time> Dump interval if -o is a dir: <num>KiB/MiB/GiB/s/sec/min/hrs\n"
996 " -J|--jumbo-support Support for 64KB Super Jumbo Frames (def: 2048B)\n"
997 " -R|--rfraw Capture or inject raw 802.11 frames\n"
998 " -n|--num <0|uint> Number of packets until exit (def: 0)\n"
999 " -P|--prefix <name> Prefix for pcaps stored in directory\n"
1000 " -T|--magic <pcap-magic> Pcap magic number/pcap format to store, see -D\n"
1001 " -D|--dump-pcap-types Dump pcap types and magic numbers and quit\n"
1002 " -B|--dump-bpf Dump generated BPF assembly\n"
1003 " -r|--rand Randomize packet forwarding order (dev->dev)\n"
1004 " -M|--no-promisc No promiscuous mode for netdev\n"
1005 " -A|--no-sock-mem Don't tune core socket memory\n"
1006 " -m|--mmap Mmap(2) pcap file i.e., for replaying pcaps\n"
1007 " -G|--sg Scatter/gather pcap file I/O\n"
1008 " -c|--clrw Use slower read(2)/write(2) I/O\n"
1009 " -S|--ring-size <size> Specify ring size to: <num>KiB/MiB/GiB\n"
1010 " -k|--kernel-pull <uint> Kernel pull from user interval in us (def: 10us)\n"
1011 " -b|--bind-cpu <cpu> Bind to specific CPU\n"
1012 " -u|--user <userid> Drop privileges and change to userid\n"
1013 " -g|--group <groupid> Drop privileges and change to groupid\n"
1014 " -H|--prio-high Make this high priority process\n"
1015 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
1016 " -s|--silent Do not print captured packets\n"
1017 " -q|--less Print less-verbose packet information\n"
1018 " -X|--hex Print packet data in hex format\n"
1019 " -l|--ascii Print human-readable packet data\n"
1020 " -U|--update Update GeoIP databases\n"
1021 " -V|--verbose Be more verbose\n"
1022 " -v|--version Show version\n"
1023 " -h|--help Guess what?!\n\n"
1024 "Examples:\n"
1025 " netsniff-ng --in eth0 --out dump.pcap -s -T 0xa1b2c3d4 --b 0 tcp or udp\n"
1026 " netsniff-ng --in wlan0 --rfraw --out dump.pcap --silent --bind-cpu 0\n"
1027 " netsniff-ng --in dump.pcap --mmap --out eth0 -k1000 --silent --bind-cpu 0\n"
1028 " netsniff-ng --in dump.pcap --out dump.cfg --silent --bind-cpu 0\n"
1029 " netsniff-ng --in eth0 --out eth1 --silent --bind-cpu 0 --type host\n"
1030 " netsniff-ng --in eth1 --out /opt/probe/ -s -m -J --interval 100MiB -b 0\n"
1031 " netsniff-ng --in vlan0 --out dump.pcap -c -u `id -u bob` -g `id -g bob`\n"
1032 " netsniff-ng --in any --filter http.bpf --jumbo-support --ascii -V\n\n"
1033 "Note:\n"
1034 " For introducing bit errors, delays with random variation and more\n"
1035 " while replaying pcaps, make use of tc(8) with its disciplines (e.g. netem).\n\n"
1036 "Please report bugs to <bugs@netsniff-ng.org>\n"
1037 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1038 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1039 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1040 "Swiss federal institute of technology (ETH Zurich)\n"
1041 "License: GNU GPL version 2.0\n"
1042 "This is free software: you are free to change and redistribute it.\n"
1043 "There is NO WARRANTY, to the extent permitted by law.\n");
1044 die();
1047 static void version(void)
1049 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING);
1050 puts("http://www.netsniff-ng.org\n\n"
1051 "Please report bugs to <bugs@netsniff-ng.org>\n"
1052 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1053 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1054 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1055 "Swiss federal institute of technology (ETH Zurich)\n"
1056 "License: GNU GPL version 2.0\n"
1057 "This is free software: you are free to change and redistribute it.\n"
1058 "There is NO WARRANTY, to the extent permitted by law.\n");
1059 die();
1062 int main(int argc, char **argv)
1064 char *ptr;
1065 int c, i, j, cpu_tmp, opt_index, ops_touched = 0, vals[4] = {0};
1066 bool prio_high = false, setsockmem = true;
1067 void (*main_loop)(struct ctx *ctx) = NULL;
1068 struct ctx ctx = {
1069 .link_type = LINKTYPE_EN10MB,
1070 .print_mode = PRINT_NORM,
1071 .cpu = -1,
1072 .packet_type = -1,
1073 .promiscuous = true,
1074 .randomize = false,
1075 .pcap = PCAP_OPS_SG,
1076 .dump_interval = 60,
1077 .dump_mode = DUMP_INTERVAL_TIME,
1078 .uid = getuid(),
1079 .gid = getgid(),
1080 .magic = ORIGINAL_TCPDUMP_MAGIC,
1083 srand(time(NULL));
1085 while ((c = getopt_long(argc, argv, short_options, long_options,
1086 &opt_index)) != EOF) {
1087 switch (c) {
1088 case 'd':
1089 case 'i':
1090 ctx.device_in = xstrdup(optarg);
1091 break;
1092 case 'o':
1093 ctx.device_out = xstrdup(optarg);
1094 break;
1095 case 'P':
1096 ctx.prefix = xstrdup(optarg);
1097 break;
1098 case 'R':
1099 ctx.link_type = LINKTYPE_IEEE802_11;
1100 ctx.rfraw = 1;
1101 break;
1102 case 'r':
1103 ctx.randomize = true;
1104 break;
1105 case 'J':
1106 ctx.jumbo = true;
1107 break;
1108 case 'T':
1109 ctx.magic = (uint32_t) strtoul(optarg, NULL, 0);
1110 pcap_check_magic(ctx.magic);
1111 break;
1112 case 'f':
1113 ctx.filter = xstrdup(optarg);
1114 break;
1115 case 'M':
1116 ctx.promiscuous = false;
1117 break;
1118 case 'A':
1119 setsockmem = false;
1120 break;
1121 case 'u':
1122 ctx.uid = strtoul(optarg, NULL, 0);
1123 ctx.enforce = true;
1124 break;
1125 case 'g':
1126 ctx.gid = strtoul(optarg, NULL, 0);
1127 ctx.enforce = true;
1128 break;
1129 case 't':
1130 if (!strncmp(optarg, "host", strlen("host")))
1131 ctx.packet_type = PACKET_HOST;
1132 else if (!strncmp(optarg, "broadcast", strlen("broadcast")))
1133 ctx.packet_type = PACKET_BROADCAST;
1134 else if (!strncmp(optarg, "multicast", strlen("multicast")))
1135 ctx.packet_type = PACKET_MULTICAST;
1136 else if (!strncmp(optarg, "others", strlen("others")))
1137 ctx.packet_type = PACKET_OTHERHOST;
1138 else if (!strncmp(optarg, "outgoing", strlen("outgoing")))
1139 ctx.packet_type = PACKET_OUTGOING;
1140 else
1141 ctx.packet_type = -1;
1142 break;
1143 case 'S':
1144 ptr = optarg;
1145 ctx.reserve_size = 0;
1147 for (j = i = strlen(optarg); i > 0; --i) {
1148 if (!isdigit(optarg[j - i]))
1149 break;
1150 ptr++;
1153 if (!strncmp(ptr, "KiB", strlen("KiB")))
1154 ctx.reserve_size = 1 << 10;
1155 else if (!strncmp(ptr, "MiB", strlen("MiB")))
1156 ctx.reserve_size = 1 << 20;
1157 else if (!strncmp(ptr, "GiB", strlen("GiB")))
1158 ctx.reserve_size = 1 << 30;
1159 else
1160 panic("Syntax error in ring size param!\n");
1161 *ptr = 0;
1163 ctx.reserve_size *= strtol(optarg, NULL, 0);
1164 break;
1165 case 'b':
1166 cpu_tmp = strtol(optarg, NULL, 0);
1168 cpu_affinity(cpu_tmp);
1169 if (ctx.cpu != -2)
1170 ctx.cpu = cpu_tmp;
1171 break;
1172 case 'H':
1173 prio_high = true;
1174 break;
1175 case 'c':
1176 ctx.pcap = PCAP_OPS_RW;
1177 ops_touched = 1;
1178 break;
1179 case 'm':
1180 ctx.pcap = PCAP_OPS_MM;
1181 ops_touched = 1;
1182 break;
1183 case 'G':
1184 ctx.pcap = PCAP_OPS_SG;
1185 ops_touched = 1;
1186 break;
1187 case 'Q':
1188 ctx.cpu = -2;
1189 break;
1190 case 's':
1191 ctx.print_mode = PRINT_NONE;
1192 break;
1193 case 'q':
1194 ctx.print_mode = PRINT_LESS;
1195 break;
1196 case 'X':
1197 ctx.print_mode =
1198 (ctx.print_mode == PRINT_ASCII) ?
1199 PRINT_HEX_ASCII : PRINT_HEX;
1200 break;
1201 case 'l':
1202 ctx.print_mode =
1203 (ctx.print_mode == PRINT_HEX) ?
1204 PRINT_HEX_ASCII : PRINT_ASCII;
1205 break;
1206 case 'k':
1207 ctx.kpull = strtol(optarg, NULL, 0);
1208 break;
1209 case 'n':
1210 frame_count_max = strtol(optarg, NULL, 0);
1211 break;
1212 case 'F':
1213 ptr = optarg;
1214 ctx.dump_interval = 0;
1216 for (j = i = strlen(optarg); i > 0; --i) {
1217 if (!isdigit(optarg[j - i]))
1218 break;
1219 ptr++;
1222 if (!strncmp(ptr, "KiB", strlen("KiB"))) {
1223 ctx.dump_interval = 1 << 10;
1224 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1225 } else if (!strncmp(ptr, "MiB", strlen("MiB"))) {
1226 ctx.dump_interval = 1 << 20;
1227 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1228 } else if (!strncmp(ptr, "GiB", strlen("GiB"))) {
1229 ctx.dump_interval = 1 << 30;
1230 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1231 } else if (!strncmp(ptr, "sec", strlen("sec"))) {
1232 ctx.dump_interval = 1;
1233 ctx.dump_mode = DUMP_INTERVAL_TIME;
1234 } else if (!strncmp(ptr, "min", strlen("min"))) {
1235 ctx.dump_interval = 60;
1236 ctx.dump_mode = DUMP_INTERVAL_TIME;
1237 } else if (!strncmp(ptr, "hrs", strlen("hrs"))) {
1238 ctx.dump_interval = 60 * 60;
1239 ctx.dump_mode = DUMP_INTERVAL_TIME;
1240 } else if (!strncmp(ptr, "s", strlen("s"))) {
1241 ctx.dump_interval = 1;
1242 ctx.dump_mode = DUMP_INTERVAL_TIME;
1243 } else {
1244 panic("Syntax error in time/size param!\n");
1247 *ptr = 0;
1248 ctx.dump_interval *= strtol(optarg, NULL, 0);
1249 break;
1250 case 'V':
1251 ctx.verbose = 1;
1252 break;
1253 case 'B':
1254 ctx.dump_bpf = true;
1255 break;
1256 case 'D':
1257 pcap_dump_type_features();
1258 die();
1259 break;
1260 case 'U':
1261 update_geoip();
1262 die();
1263 break;
1264 case 'v':
1265 version();
1266 break;
1267 case 'h':
1268 help();
1269 break;
1270 case '?':
1271 switch (optopt) {
1272 case 'd':
1273 case 'i':
1274 case 'o':
1275 case 'f':
1276 case 't':
1277 case 'P':
1278 case 'F':
1279 case 'n':
1280 case 'S':
1281 case 'b':
1282 case 'k':
1283 case 'T':
1284 case 'u':
1285 case 'g':
1286 case 'e':
1287 panic("Option -%c requires an argument!\n",
1288 optopt);
1289 default:
1290 if (isprint(optopt))
1291 printf("Unknown option character `0x%X\'!\n", optopt);
1292 die();
1294 default:
1295 break;
1299 if (!ctx.filter && optind != argc) {
1300 int ret;
1301 off_t offset = 0;
1303 for (i = optind; i < argc; ++i) {
1304 size_t alen = strlen(argv[i]) + 2;
1305 size_t flen = ctx.filter ? strlen(ctx.filter) : 0;
1307 ctx.filter = xrealloc(ctx.filter, 1, flen + alen);
1308 ret = slprintf(ctx.filter + offset, strlen(argv[i]) + 2, "%s ", argv[i]);
1309 if (ret < 0)
1310 panic("Cannot concatenate filter string!\n");
1311 else
1312 offset += ret;
1316 if (!ctx.device_in)
1317 ctx.device_in = xstrdup("any");
1319 register_signal(SIGINT, signal_handler);
1320 register_signal(SIGHUP, signal_handler);
1322 tprintf_init();
1324 if (prio_high) {
1325 set_proc_prio(get_default_proc_prio());
1326 set_sched_status(get_default_sched_policy(), get_default_sched_prio());
1329 if (ctx.device_in && (device_mtu(ctx.device_in) ||
1330 !strncmp("any", ctx.device_in, strlen(ctx.device_in)))) {
1331 if (!ctx.device_out) {
1332 ctx.dump = 0;
1333 main_loop = recv_only_or_dump;
1334 } else if (device_mtu(ctx.device_out)) {
1335 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1336 main_loop = receive_to_xmit;
1337 } else {
1338 ctx.dump = 1;
1339 register_signal_f(SIGALRM, timer_next_dump, SA_SIGINFO);
1340 main_loop = recv_only_or_dump;
1341 if (!ops_touched)
1342 ctx.pcap = PCAP_OPS_SG;
1344 } else {
1345 if (ctx.device_out && device_mtu(ctx.device_out)) {
1346 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1347 main_loop = pcap_to_xmit;
1348 if (!ops_touched)
1349 ctx.pcap = PCAP_OPS_MM;
1350 } else {
1351 main_loop = read_pcap;
1352 if (!ops_touched)
1353 ctx.pcap = PCAP_OPS_SG;
1357 bug_on(!main_loop);
1359 init_geoip(0);
1360 if (setsockmem)
1361 set_system_socket_memory(vals, array_size(vals));
1362 xlockme();
1364 main_loop(&ctx);
1366 xunlockme();
1367 if (setsockmem)
1368 reset_system_socket_memory(vals, array_size(vals));
1369 destroy_geoip();
1371 tprintf_cleanup();
1373 free(ctx.device_in);
1374 free(ctx.device_out);
1375 free(ctx.device_trans);
1376 free(ctx.prefix);
1378 return 0;