ring_tx: user_may_pull_from_tx: fix check for user frame
[netsniff-ng.git] / netsniff-ng.c
blobc1e0d269c0f5d50d36768bf69d4c37dda39e7c20
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 int ret;
120 set_itimer_interval_value(&itimer, 0, interval);
122 ret = pull_and_flush_tx_ring(tx_sock);
123 if (unlikely(ret < 0)) {
124 /* We could hit EBADF if the socket has been closed before
125 * the timer was triggered.
127 if (errno != EBADF && errno != ENOBUFS)
128 panic("Flushing TX_RING failed: %s!\n", strerror(errno));
131 setitimer(ITIMER_REAL, &itimer, NULL);
134 static void timer_purge(void)
136 int ret;
138 ret = pull_and_flush_tx_ring_wait(tx_sock);
139 if (unlikely(ret < 0)) {
140 if (errno != EBADF && errno != ENOBUFS)
141 panic("Flushing TX_RING failed: %s!\n", strerror(errno));
144 set_itimer_interval_value(&itimer, 0, 0);
145 setitimer(ITIMER_REAL, &itimer, NULL);
149 static void timer_next_dump(int unused)
151 set_itimer_interval_value(&itimer, interval, 0);
152 next_dump = true;
153 setitimer(ITIMER_REAL, &itimer, NULL);
156 static inline bool dump_to_pcap(struct ctx *ctx)
158 return ctx->dump;
161 static void pcap_to_xmit(struct ctx *ctx)
163 __label__ out;
164 uint8_t *out = NULL;
165 int irq, ifindex, fd = 0, ret;
166 unsigned int size, it = 0;
167 unsigned long trunced = 0;
168 struct ring tx_ring;
169 struct frame_map *hdr;
170 struct sock_fprog bpf_ops;
171 struct timeval start, end, diff;
172 pcap_pkthdr_t phdr;
174 if (!device_up_and_running(ctx->device_out) && !ctx->rfraw)
175 panic("Device not up and running!\n");
177 bug_on(!__pcap_io);
179 tx_sock = pf_socket();
181 if (!strncmp("-", ctx->device_in, strlen("-"))) {
182 fd = dup(fileno(stdin));
183 close(fileno(stdin));
184 if (ctx->pcap == PCAP_OPS_MM)
185 ctx->pcap = PCAP_OPS_SG;
186 } else {
187 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
190 if (__pcap_io->init_once_pcap)
191 __pcap_io->init_once_pcap();
193 ret = __pcap_io->pull_fhdr_pcap(fd, &ctx->magic, &ctx->link_type);
194 if (ret)
195 panic("Error reading pcap header!\n");
197 if (__pcap_io->prepare_access_pcap) {
198 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_RD, ctx->jumbo);
199 if (ret)
200 panic("Error prepare reading pcap!\n");
203 fmemset(&tx_ring, 0, sizeof(tx_ring));
204 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
206 if (ctx->rfraw) {
207 ctx->device_trans = xstrdup(ctx->device_out);
208 xfree(ctx->device_out);
210 enter_rfmon_mac80211(ctx->device_trans, &ctx->device_out);
211 if (ctx->link_type != LINKTYPE_IEEE802_11)
212 panic("Wrong linktype of pcap!\n");
215 ifindex = device_ifindex(ctx->device_out);
217 size = ring_size(ctx->device_out, ctx->reserve_size);
219 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
220 if (ctx->dump_bpf)
221 bpf_dump_all(&bpf_ops);
223 set_packet_loss_discard(tx_sock);
224 set_sockopt_hwtimestamp(tx_sock, ctx->device_out);
226 setup_tx_ring_layout(tx_sock, &tx_ring, size, ctx->jumbo);
227 create_tx_ring(tx_sock, &tx_ring, ctx->verbose);
228 mmap_tx_ring(tx_sock, &tx_ring);
229 alloc_tx_ring_frames(&tx_ring);
230 bind_tx_ring(tx_sock, &tx_ring, ifindex);
232 dissector_init_all(ctx->print_mode);
234 if (ctx->cpu >= 0 && ifindex > 0) {
235 irq = device_irq_number(ctx->device_out);
236 device_bind_irq_to_cpu(irq, ctx->cpu);
238 if (ctx->verbose)
239 printf("IRQ: %s:%d > CPU%d\n",
240 ctx->device_out, irq, ctx->cpu);
243 if (ctx->kpull)
244 interval = ctx->kpull;
246 set_itimer_interval_value(&itimer, 0, interval);
247 setitimer(ITIMER_REAL, &itimer, NULL);
249 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
251 printf("Running! Hang up with ^C!\n\n");
252 fflush(stdout);
254 bug_on(gettimeofday(&start, NULL));
256 while (likely(sigint == 0)) {
257 while (user_may_pull_from_tx(tx_ring.frames[it].iov_base)) {
258 hdr = tx_ring.frames[it].iov_base;
259 out = ((uint8_t *) hdr) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
261 do {
262 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic, out,
263 ring_frame_size(&tx_ring));
264 if (unlikely(ret <= 0))
265 goto out;
267 if (ring_frame_size(&tx_ring) <
268 pcap_get_length(&phdr, ctx->magic)) {
269 pcap_set_length(&phdr, ctx->magic,
270 ring_frame_size(&tx_ring));
271 trunced++;
273 } while (ctx->filter &&
274 !bpf_run_filter(&bpf_ops, out,
275 pcap_get_length(&phdr, ctx->magic)));
277 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &hdr->tp_h, &hdr->s_ll);
279 ctx->tx_bytes += hdr->tp_h.tp_len;;
280 ctx->tx_packets++;
282 show_frame_hdr(hdr, ctx->print_mode);
284 dissector_entry_point(out, hdr->tp_h.tp_snaplen,
285 ctx->link_type, ctx->print_mode);
287 kernel_may_pull_from_tx(&hdr->tp_h);
289 it++;
290 if (it >= tx_ring.layout.tp_frame_nr)
291 it = 0;
293 if (unlikely(sigint == 1))
294 break;
296 if (frame_count_max != 0) {
297 if (ctx->tx_packets >= frame_count_max) {
298 sigint = 1;
299 break;
305 out:
307 bug_on(gettimeofday(&end, NULL));
308 timersub(&end, &start, &diff);
310 timer_purge();
312 bpf_release(&bpf_ops);
314 dissector_cleanup_all();
315 destroy_tx_ring(tx_sock, &tx_ring);
317 if (ctx->rfraw)
318 leave_rfmon_mac80211(ctx->device_trans, ctx->device_out);
320 if (__pcap_io->prepare_close_pcap)
321 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
323 if (!strncmp("-", ctx->device_in, strlen("-")))
324 dup2(fd, fileno(stdin));
325 close(fd);
327 close(tx_sock);
329 fflush(stdout);
330 printf("\n");
331 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
332 printf("\r%12lu packets truncated in file\n", trunced);
333 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
334 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
337 static void receive_to_xmit(struct ctx *ctx)
339 short ifflags = 0;
340 uint8_t *in, *out;
341 int rx_sock, ifindex_in, ifindex_out;
342 unsigned int size_in, size_out, it_in = 0, it_out = 0;
343 unsigned long frame_count = 0;
344 struct frame_map *hdr_in, *hdr_out;
345 struct ring tx_ring, rx_ring;
346 struct pollfd rx_poll;
347 struct sock_fprog bpf_ops;
349 if (!strncmp(ctx->device_in, ctx->device_out, IFNAMSIZ))
350 panic("Ingress/egress devices must be different!\n");
351 if (!device_up_and_running(ctx->device_out))
352 panic("Egress device not up and running!\n");
354 rx_sock = pf_socket();
355 tx_sock = pf_socket();
357 fmemset(&tx_ring, 0, sizeof(tx_ring));
358 fmemset(&rx_ring, 0, sizeof(rx_ring));
359 fmemset(&rx_poll, 0, sizeof(rx_poll));
360 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
362 ifindex_in = device_ifindex(ctx->device_in);
363 ifindex_out = device_ifindex(ctx->device_out);
365 size_in = ring_size(ctx->device_in, ctx->reserve_size);
366 size_out = ring_size(ctx->device_out, ctx->reserve_size);
368 enable_kernel_bpf_jit_compiler();
370 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
371 if (ctx->dump_bpf)
372 bpf_dump_all(&bpf_ops);
373 bpf_attach_to_sock(rx_sock, &bpf_ops);
375 setup_rx_ring_layout(rx_sock, &rx_ring, size_in, ctx->jumbo);
376 create_rx_ring(rx_sock, &rx_ring, ctx->verbose);
377 mmap_rx_ring(rx_sock, &rx_ring);
378 alloc_rx_ring_frames(&rx_ring);
379 bind_rx_ring(rx_sock, &rx_ring, ifindex_in);
380 prepare_polling(rx_sock, &rx_poll);
382 set_packet_loss_discard(tx_sock);
383 setup_tx_ring_layout(tx_sock, &tx_ring, size_out, ctx->jumbo);
384 create_tx_ring(tx_sock, &tx_ring, ctx->verbose);
385 mmap_tx_ring(tx_sock, &tx_ring);
386 alloc_tx_ring_frames(&tx_ring);
387 bind_tx_ring(tx_sock, &tx_ring, ifindex_out);
389 dissector_init_all(ctx->print_mode);
391 if (ctx->promiscuous)
392 ifflags = enter_promiscuous_mode(ctx->device_in);
394 if (ctx->kpull)
395 interval = ctx->kpull;
397 set_itimer_interval_value(&itimer, 0, interval);
398 setitimer(ITIMER_REAL, &itimer, NULL);
400 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
402 printf("Running! Hang up with ^C!\n\n");
403 fflush(stdout);
405 while (likely(sigint == 0)) {
406 while (user_may_pull_from_rx(rx_ring.frames[it_in].iov_base)) {
407 __label__ next;
409 hdr_in = rx_ring.frames[it_in].iov_base;
410 in = ((uint8_t *) hdr_in) + hdr_in->tp_h.tp_mac;
412 frame_count++;
414 if (ctx->packet_type != -1)
415 if (ctx->packet_type != hdr_in->s_ll.sll_pkttype)
416 goto next;
418 hdr_out = tx_ring.frames[it_out].iov_base;
419 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
421 for (; !user_may_pull_from_tx(tx_ring.frames[it_out].iov_base) &&
422 likely(!sigint);) {
423 if (ctx->randomize)
424 next_rnd_slot(&it_out, &tx_ring);
425 else {
426 it_out++;
427 if (it_out >= tx_ring.layout.tp_frame_nr)
428 it_out = 0;
431 hdr_out = tx_ring.frames[it_out].iov_base;
432 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
435 tpacket_hdr_clone(&hdr_out->tp_h, &hdr_in->tp_h);
436 fmemcpy(out, in, hdr_in->tp_h.tp_len);
438 kernel_may_pull_from_tx(&hdr_out->tp_h);
439 if (ctx->randomize)
440 next_rnd_slot(&it_out, &tx_ring);
441 else {
442 it_out++;
443 if (it_out >= tx_ring.layout.tp_frame_nr)
444 it_out = 0;
447 show_frame_hdr(hdr_in, ctx->print_mode);
449 dissector_entry_point(in, hdr_in->tp_h.tp_snaplen,
450 ctx->link_type, ctx->print_mode);
452 if (frame_count_max != 0) {
453 if (frame_count >= frame_count_max) {
454 sigint = 1;
455 break;
459 next:
461 kernel_may_pull_from_rx(&hdr_in->tp_h);
463 it_in++;
464 if (it_in >= rx_ring.layout.tp_frame_nr)
465 it_in = 0;
467 if (unlikely(sigint == 1))
468 goto out;
471 poll(&rx_poll, 1, -1);
474 out:
476 timer_purge();
478 sock_print_net_stats(rx_sock, 0);
480 bpf_release(&bpf_ops);
482 dissector_cleanup_all();
484 destroy_tx_ring(tx_sock, &tx_ring);
485 destroy_rx_ring(rx_sock, &rx_ring);
487 if (ctx->promiscuous)
488 leave_promiscuous_mode(ctx->device_in, ifflags);
490 close(tx_sock);
491 close(rx_sock);
494 static void translate_pcap_to_txf(int fdo, uint8_t *out, size_t len)
496 size_t bytes_done = 0;
497 char bout[80];
499 slprintf(bout, sizeof(bout), "{\n ");
500 write_or_die(fdo, bout, strlen(bout));
502 while (bytes_done < len) {
503 slprintf(bout, sizeof(bout), "0x%02x, ", out[bytes_done]);
504 write_or_die(fdo, bout, strlen(bout));
506 bytes_done++;
508 if (bytes_done % 10 == 0) {
509 slprintf(bout, sizeof(bout), "\n");
510 write_or_die(fdo, bout, strlen(bout));
512 if (bytes_done < len) {
513 slprintf(bout, sizeof(bout), " ");
514 write_or_die(fdo, bout, strlen(bout));
518 if (bytes_done % 10 != 0) {
519 slprintf(bout, sizeof(bout), "\n");
520 write_or_die(fdo, bout, strlen(bout));
523 slprintf(bout, sizeof(bout), "}\n\n");
524 write_or_die(fdo, bout, strlen(bout));
527 static void read_pcap(struct ctx *ctx)
529 __label__ out;
530 uint8_t *out;
531 int ret, fd, fdo = 0;
532 unsigned long trunced = 0;
533 size_t out_len;
534 pcap_pkthdr_t phdr;
535 struct sock_fprog bpf_ops;
536 struct frame_map fm;
537 struct timeval start, end, diff;
538 struct sockaddr_ll sll;
540 bug_on(!__pcap_io);
542 if (!strncmp("-", ctx->device_in, strlen("-"))) {
543 fd = dup(fileno(stdin));
544 close(fileno(stdin));
545 if (ctx->pcap == PCAP_OPS_MM)
546 ctx->pcap = PCAP_OPS_SG;
547 } else {
548 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
551 if (__pcap_io->init_once_pcap)
552 __pcap_io->init_once_pcap();
554 ret = __pcap_io->pull_fhdr_pcap(fd, &ctx->magic, &ctx->link_type);
555 if (ret)
556 panic("Error reading pcap header!\n");
558 if (__pcap_io->prepare_access_pcap) {
559 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_RD, ctx->jumbo);
560 if (ret)
561 panic("Error prepare reading pcap!\n");
564 fmemset(&fm, 0, sizeof(fm));
565 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
567 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
568 if (ctx->dump_bpf)
569 bpf_dump_all(&bpf_ops);
571 dissector_init_all(ctx->print_mode);
573 out_len = round_up(1024 * 1024, PAGE_SIZE);
574 out = xmalloc_aligned(out_len, CO_CACHE_LINE_SIZE);
576 if (ctx->device_out) {
577 if (!strncmp("-", ctx->device_out, strlen("-"))) {
578 fdo = dup(fileno(stdout));
579 close(fileno(stdout));
580 } else {
581 fdo = open_or_die_m(ctx->device_out, O_RDWR | O_CREAT |
582 O_TRUNC | O_LARGEFILE, DEFFILEMODE);
586 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
588 printf("Running! Hang up with ^C!\n\n");
589 fflush(stdout);
591 bug_on(gettimeofday(&start, NULL));
593 while (likely(sigint == 0)) {
594 do {
595 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic,
596 out, out_len);
597 if (unlikely(ret < 0))
598 goto out;
600 if (unlikely(pcap_get_length(&phdr, ctx->magic) == 0)) {
601 trunced++;
602 continue;
605 if (unlikely(pcap_get_length(&phdr, ctx->magic) > out_len)) {
606 pcap_set_length(&phdr, ctx->magic, out_len);
607 trunced++;
609 } while (ctx->filter &&
610 !bpf_run_filter(&bpf_ops, out,
611 pcap_get_length(&phdr, ctx->magic)));
613 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &fm.tp_h, &sll);
615 ctx->tx_bytes += fm.tp_h.tp_len;
616 ctx->tx_packets++;
618 show_frame_hdr(&fm, ctx->print_mode);
620 dissector_entry_point(out, fm.tp_h.tp_snaplen,
621 ctx->link_type, ctx->print_mode);
623 if (ctx->device_out)
624 translate_pcap_to_txf(fdo, out, fm.tp_h.tp_snaplen);
626 if (frame_count_max != 0) {
627 if (ctx->tx_packets >= frame_count_max) {
628 sigint = 1;
629 break;
634 out:
636 bug_on(gettimeofday(&end, NULL));
637 timersub(&end, &start, &diff);
639 bpf_release(&bpf_ops);
641 dissector_cleanup_all();
643 if (__pcap_io->prepare_close_pcap)
644 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
646 xfree(out);
648 fflush(stdout);
649 printf("\n");
650 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
651 printf("\r%12lu packets truncated in file\n", trunced);
652 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
653 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
655 if (!strncmp("-", ctx->device_in, strlen("-")))
656 dup2(fd, fileno(stdin));
657 close(fd);
659 if (ctx->device_out) {
660 if (!strncmp("-", ctx->device_out, strlen("-")))
661 dup2(fdo, fileno(stdout));
662 close(fdo);
666 static void finish_multi_pcap_file(struct ctx *ctx, int fd)
668 __pcap_io->fsync_pcap(fd);
670 if (__pcap_io->prepare_close_pcap)
671 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
673 close(fd);
675 fmemset(&itimer, 0, sizeof(itimer));
676 setitimer(ITIMER_REAL, &itimer, NULL);
679 static int next_multi_pcap_file(struct ctx *ctx, int fd)
681 int ret;
682 char fname[512];
684 __pcap_io->fsync_pcap(fd);
686 if (__pcap_io->prepare_close_pcap)
687 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
689 close(fd);
691 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
692 ctx->prefix ? : "dump-", time(0));
694 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
695 O_LARGEFILE, DEFFILEMODE);
697 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
698 if (ret)
699 panic("Error writing pcap header!\n");
701 if (__pcap_io->prepare_access_pcap) {
702 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, ctx->jumbo);
703 if (ret)
704 panic("Error prepare writing pcap!\n");
707 return fd;
710 static int begin_multi_pcap_file(struct ctx *ctx)
712 int fd, ret;
713 char fname[256];
715 bug_on(!__pcap_io);
717 if (ctx->device_out[strlen(ctx->device_out) - 1] == '/')
718 ctx->device_out[strlen(ctx->device_out) - 1] = 0;
720 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
721 ctx->prefix ? : "dump-", time(0));
723 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
724 O_LARGEFILE, DEFFILEMODE);
726 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
727 if (ret)
728 panic("Error writing pcap header!\n");
730 if (__pcap_io->prepare_access_pcap) {
731 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, ctx->jumbo);
732 if (ret)
733 panic("Error prepare writing pcap!\n");
736 if (ctx->dump_mode == DUMP_INTERVAL_TIME) {
737 interval = ctx->dump_interval;
739 set_itimer_interval_value(&itimer, interval, 0);
740 setitimer(ITIMER_REAL, &itimer, NULL);
741 } else {
742 interval = 0;
745 return fd;
748 static void finish_single_pcap_file(struct ctx *ctx, int fd)
750 __pcap_io->fsync_pcap(fd);
752 if (__pcap_io->prepare_close_pcap)
753 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
755 if (strncmp("-", ctx->device_out, strlen("-")))
756 close(fd);
757 else
758 dup2(fd, fileno(stdout));
761 static int begin_single_pcap_file(struct ctx *ctx)
763 int fd, ret;
765 bug_on(!__pcap_io);
767 if (!strncmp("-", ctx->device_out, strlen("-"))) {
768 fd = dup(fileno(stdout));
769 close(fileno(stdout));
770 if (ctx->pcap == PCAP_OPS_MM)
771 ctx->pcap = PCAP_OPS_SG;
772 } else {
773 fd = open_or_die_m(ctx->device_out,
774 O_RDWR | O_CREAT | O_TRUNC |
775 O_LARGEFILE, DEFFILEMODE);
778 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
779 if (ret)
780 panic("Error writing pcap header!\n");
782 if (__pcap_io->prepare_access_pcap) {
783 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, ctx->jumbo);
784 if (ret)
785 panic("Error prepare writing pcap!\n");
788 return fd;
791 static void print_pcap_file_stats(int sock, struct ctx *ctx, unsigned long skipped)
793 int ret;
794 unsigned long good, bad;
795 struct tpacket_stats kstats;
796 socklen_t slen = sizeof(kstats);
798 fmemset(&kstats, 0, sizeof(kstats));
800 ret = getsockopt(sock, SOL_PACKET, PACKET_STATISTICS, &kstats, &slen);
801 if (unlikely(ret))
802 panic("Cannot get packet statistics!\n");
804 if (ctx->print_mode == PRINT_NONE) {
805 good = kstats.tp_packets - kstats.tp_drops - skipped;
806 bad = kstats.tp_drops + skipped;
808 printf(".(+%lu/-%lu)", good, bad);
809 fflush(stdout);
813 static void recv_only_or_dump(struct ctx *ctx)
815 uint8_t *packet;
816 short ifflags = 0;
817 int sock, irq, ifindex, fd = 0, ret;
818 unsigned int size, it = 0;
819 unsigned long frame_count = 0, skipped = 0;
820 struct ring rx_ring;
821 struct pollfd rx_poll;
822 struct frame_map *hdr;
823 struct sock_fprog bpf_ops;
824 struct timeval start, end, diff;
825 pcap_pkthdr_t phdr;
827 sock = pf_socket();
829 if (ctx->rfraw) {
830 ctx->device_trans = xstrdup(ctx->device_in);
831 xfree(ctx->device_in);
833 enter_rfmon_mac80211(ctx->device_trans, &ctx->device_in);
834 ctx->link_type = LINKTYPE_IEEE802_11;
837 fmemset(&rx_ring, 0, sizeof(rx_ring));
838 fmemset(&rx_poll, 0, sizeof(rx_poll));
839 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
841 ifindex = device_ifindex(ctx->device_in);
843 size = ring_size(ctx->device_in, ctx->reserve_size);
845 enable_kernel_bpf_jit_compiler();
847 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
848 if (ctx->dump_bpf)
849 bpf_dump_all(&bpf_ops);
850 bpf_attach_to_sock(sock, &bpf_ops);
852 set_sockopt_hwtimestamp(sock, ctx->device_in);
854 setup_rx_ring_layout(sock, &rx_ring, size, ctx->jumbo);
855 create_rx_ring(sock, &rx_ring, ctx->verbose);
856 mmap_rx_ring(sock, &rx_ring);
857 alloc_rx_ring_frames(&rx_ring);
858 bind_rx_ring(sock, &rx_ring, ifindex);
860 prepare_polling(sock, &rx_poll);
861 dissector_init_all(ctx->print_mode);
863 if (ctx->cpu >= 0 && ifindex > 0) {
864 irq = device_irq_number(ctx->device_in);
865 device_bind_irq_to_cpu(irq, ctx->cpu);
867 if (ctx->verbose)
868 printf("IRQ: %s:%d > CPU%d\n",
869 ctx->device_in, irq, ctx->cpu);
872 if (ctx->promiscuous)
873 ifflags = enter_promiscuous_mode(ctx->device_in);
875 if (dump_to_pcap(ctx) && __pcap_io->init_once_pcap)
876 __pcap_io->init_once_pcap();
878 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
880 if (dump_to_pcap(ctx)) {
881 __label__ try_file;
882 struct stat stats;
884 fmemset(&stats, 0, sizeof(stats));
885 ret = stat(ctx->device_out, &stats);
886 if (ret < 0) {
887 ctx->dump_dir = 0;
888 goto try_file;
891 ctx->dump_dir = S_ISDIR(stats.st_mode);
892 if (ctx->dump_dir) {
893 fd = begin_multi_pcap_file(ctx);
894 } else {
895 try_file:
896 fd = begin_single_pcap_file(ctx);
900 printf("Running! Hang up with ^C!\n\n");
901 fflush(stdout);
903 bug_on(gettimeofday(&start, NULL));
905 while (likely(sigint == 0)) {
906 while (user_may_pull_from_rx(rx_ring.frames[it].iov_base)) {
907 __label__ next;
909 hdr = rx_ring.frames[it].iov_base;
910 packet = ((uint8_t *) hdr) + hdr->tp_h.tp_mac;
911 frame_count++;
913 if (ctx->packet_type != -1)
914 if (ctx->packet_type != hdr->s_ll.sll_pkttype)
915 goto next;
917 if (unlikely(ring_frame_size(&rx_ring) < hdr->tp_h.tp_snaplen)) {
918 skipped++;
919 goto next;
922 if (dump_to_pcap(ctx)) {
923 tpacket_hdr_to_pcap_pkthdr(&hdr->tp_h, &hdr->s_ll, &phdr, ctx->magic);
925 ret = __pcap_io->write_pcap(fd, &phdr, ctx->magic, packet,
926 pcap_get_length(&phdr, ctx->magic));
927 if (unlikely(ret != pcap_get_total_length(&phdr, ctx->magic)))
928 panic("Write error to pcap!\n");
931 show_frame_hdr(hdr, ctx->print_mode);
933 dissector_entry_point(packet, hdr->tp_h.tp_snaplen,
934 ctx->link_type, ctx->print_mode);
936 if (frame_count_max != 0) {
937 if (frame_count >= frame_count_max) {
938 sigint = 1;
939 break;
943 next:
945 kernel_may_pull_from_rx(&hdr->tp_h);
947 it++;
948 if (it >= rx_ring.layout.tp_frame_nr)
949 it = 0;
951 if (unlikely(sigint == 1))
952 break;
954 if (dump_to_pcap(ctx)) {
955 if (ctx->dump_mode == DUMP_INTERVAL_SIZE) {
956 interval += hdr->tp_h.tp_snaplen;
958 if (interval > ctx->dump_interval) {
959 next_dump = true;
960 interval = 0;
964 if (next_dump) {
965 fd = next_multi_pcap_file(ctx, fd);
966 next_dump = false;
968 if (ctx->verbose)
969 print_pcap_file_stats(sock, ctx, skipped);
974 poll(&rx_poll, 1, -1);
977 bug_on(gettimeofday(&end, NULL));
978 timersub(&end, &start, &diff);
980 if (!(ctx->dump_dir && ctx->print_mode == PRINT_NONE)) {
981 sock_print_net_stats(sock, skipped);
983 printf("\r%12lu sec, %lu usec in total\n",
984 diff.tv_sec, diff.tv_usec);
985 } else {
986 printf("\n\n");
987 fflush(stdout);
990 bpf_release(&bpf_ops);
991 dissector_cleanup_all();
992 destroy_rx_ring(sock, &rx_ring);
994 if (ctx->promiscuous)
995 leave_promiscuous_mode(ctx->device_in, ifflags);
997 if (ctx->rfraw)
998 leave_rfmon_mac80211(ctx->device_trans, ctx->device_in);
1000 if (dump_to_pcap(ctx)) {
1001 if (ctx->dump_dir)
1002 finish_multi_pcap_file(ctx, fd);
1003 else
1004 finish_single_pcap_file(ctx, fd);
1007 close(sock);
1010 static void help(void)
1012 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING);
1013 puts("http://www.netsniff-ng.org\n\n"
1014 "Usage: netsniff-ng [options] [filter-expression]\n"
1015 "Options:\n"
1016 " -i|-d|--dev|--in <dev|pcap|-> Input source as netdev, pcap or pcap stdin\n"
1017 " -o|--out <dev|pcap|dir|cfg|-> Output sink as netdev, pcap, directory, trafgen, or stdout\n"
1018 " -f|--filter <bpf-file|expr> Use BPF filter file from bpfc or tcpdump-like expression\n"
1019 " -t|--type <type> Filter for: host|broadcast|multicast|others|outgoing\n"
1020 " -F|--interval <size|time> Dump interval if -o is a dir: <num>KiB/MiB/GiB/s/sec/min/hrs\n"
1021 " -J|--jumbo-support Support for 64KB Super Jumbo Frames (def: 2048B)\n"
1022 " -R|--rfraw Capture or inject raw 802.11 frames\n"
1023 " -n|--num <0|uint> Number of packets until exit (def: 0)\n"
1024 " -P|--prefix <name> Prefix for pcaps stored in directory\n"
1025 " -T|--magic <pcap-magic> Pcap magic number/pcap format to store, see -D\n"
1026 " -D|--dump-pcap-types Dump pcap types and magic numbers and quit\n"
1027 " -B|--dump-bpf Dump generated BPF assembly\n"
1028 " -r|--rand Randomize packet forwarding order (dev->dev)\n"
1029 " -M|--no-promisc No promiscuous mode for netdev\n"
1030 " -A|--no-sock-mem Don't tune core socket memory\n"
1031 " -m|--mmap Mmap(2) pcap file i.e., for replaying pcaps\n"
1032 " -G|--sg Scatter/gather pcap file I/O\n"
1033 " -c|--clrw Use slower read(2)/write(2) I/O\n"
1034 " -S|--ring-size <size> Specify ring size to: <num>KiB/MiB/GiB\n"
1035 " -k|--kernel-pull <uint> Kernel pull from user interval in us (def: 10us)\n"
1036 " -b|--bind-cpu <cpu> Bind to specific CPU\n"
1037 " -u|--user <userid> Drop privileges and change to userid\n"
1038 " -g|--group <groupid> Drop privileges and change to groupid\n"
1039 " -H|--prio-high Make this high priority process\n"
1040 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
1041 " -s|--silent Do not print captured packets\n"
1042 " -q|--less Print less-verbose packet information\n"
1043 " -X|--hex Print packet data in hex format\n"
1044 " -l|--ascii Print human-readable packet data\n"
1045 " -U|--update Update GeoIP databases\n"
1046 " -V|--verbose Be more verbose\n"
1047 " -v|--version Show version\n"
1048 " -h|--help Guess what?!\n\n"
1049 "Examples:\n"
1050 " netsniff-ng --in eth0 --out dump.pcap -s -T 0xa1b2c3d4 --b 0 tcp or udp\n"
1051 " netsniff-ng --in wlan0 --rfraw --out dump.pcap --silent --bind-cpu 0\n"
1052 " netsniff-ng --in dump.pcap --mmap --out eth0 -k1000 --silent --bind-cpu 0\n"
1053 " netsniff-ng --in dump.pcap --out dump.cfg --silent --bind-cpu 0\n"
1054 " netsniff-ng --in eth0 --out eth1 --silent --bind-cpu 0 --type host\n"
1055 " netsniff-ng --in eth1 --out /opt/probe/ -s -m -J --interval 100MiB -b 0\n"
1056 " netsniff-ng --in vlan0 --out dump.pcap -c -u `id -u bob` -g `id -g bob`\n"
1057 " netsniff-ng --in any --filter http.bpf --jumbo-support --ascii -V\n\n"
1058 "Note:\n"
1059 " For introducing bit errors, delays with random variation and more\n"
1060 " while replaying pcaps, make use of tc(8) with its disciplines (e.g. netem).\n\n"
1061 "Please report bugs to <bugs@netsniff-ng.org>\n"
1062 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1063 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1064 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1065 "Swiss federal institute of technology (ETH Zurich)\n"
1066 "License: GNU GPL version 2.0\n"
1067 "This is free software: you are free to change and redistribute it.\n"
1068 "There is NO WARRANTY, to the extent permitted by law.\n");
1069 die();
1072 static void version(void)
1074 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING);
1075 puts("http://www.netsniff-ng.org\n\n"
1076 "Please report bugs to <bugs@netsniff-ng.org>\n"
1077 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1078 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1079 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1080 "Swiss federal institute of technology (ETH Zurich)\n"
1081 "License: GNU GPL version 2.0\n"
1082 "This is free software: you are free to change and redistribute it.\n"
1083 "There is NO WARRANTY, to the extent permitted by law.\n");
1084 die();
1087 int main(int argc, char **argv)
1089 char *ptr;
1090 int c, i, j, cpu_tmp, opt_index, ops_touched = 0, vals[4] = {0};
1091 bool prio_high = false, setsockmem = true;
1092 void (*main_loop)(struct ctx *ctx) = NULL;
1093 struct ctx ctx = {
1094 .link_type = LINKTYPE_EN10MB,
1095 .print_mode = PRINT_NORM,
1096 .cpu = -1,
1097 .packet_type = -1,
1098 .promiscuous = true,
1099 .randomize = false,
1100 .pcap = PCAP_OPS_SG,
1101 .dump_interval = 60,
1102 .dump_mode = DUMP_INTERVAL_TIME,
1103 .uid = getuid(),
1104 .gid = getgid(),
1105 .magic = ORIGINAL_TCPDUMP_MAGIC,
1108 srand(time(NULL));
1110 while ((c = getopt_long(argc, argv, short_options, long_options,
1111 &opt_index)) != EOF) {
1112 switch (c) {
1113 case 'd':
1114 case 'i':
1115 ctx.device_in = xstrdup(optarg);
1116 break;
1117 case 'o':
1118 ctx.device_out = xstrdup(optarg);
1119 break;
1120 case 'P':
1121 ctx.prefix = xstrdup(optarg);
1122 break;
1123 case 'R':
1124 ctx.link_type = LINKTYPE_IEEE802_11;
1125 ctx.rfraw = 1;
1126 break;
1127 case 'r':
1128 ctx.randomize = true;
1129 break;
1130 case 'J':
1131 ctx.jumbo = true;
1132 break;
1133 case 'T':
1134 ctx.magic = (uint32_t) strtoul(optarg, NULL, 0);
1135 pcap_check_magic(ctx.magic);
1136 break;
1137 case 'f':
1138 ctx.filter = xstrdup(optarg);
1139 break;
1140 case 'M':
1141 ctx.promiscuous = false;
1142 break;
1143 case 'A':
1144 setsockmem = false;
1145 break;
1146 case 'u':
1147 ctx.uid = strtoul(optarg, NULL, 0);
1148 ctx.enforce = true;
1149 break;
1150 case 'g':
1151 ctx.gid = strtoul(optarg, NULL, 0);
1152 ctx.enforce = true;
1153 break;
1154 case 't':
1155 if (!strncmp(optarg, "host", strlen("host")))
1156 ctx.packet_type = PACKET_HOST;
1157 else if (!strncmp(optarg, "broadcast", strlen("broadcast")))
1158 ctx.packet_type = PACKET_BROADCAST;
1159 else if (!strncmp(optarg, "multicast", strlen("multicast")))
1160 ctx.packet_type = PACKET_MULTICAST;
1161 else if (!strncmp(optarg, "others", strlen("others")))
1162 ctx.packet_type = PACKET_OTHERHOST;
1163 else if (!strncmp(optarg, "outgoing", strlen("outgoing")))
1164 ctx.packet_type = PACKET_OUTGOING;
1165 else
1166 ctx.packet_type = -1;
1167 break;
1168 case 'S':
1169 ptr = optarg;
1170 ctx.reserve_size = 0;
1172 for (j = i = strlen(optarg); i > 0; --i) {
1173 if (!isdigit(optarg[j - i]))
1174 break;
1175 ptr++;
1178 if (!strncmp(ptr, "KiB", strlen("KiB")))
1179 ctx.reserve_size = 1 << 10;
1180 else if (!strncmp(ptr, "MiB", strlen("MiB")))
1181 ctx.reserve_size = 1 << 20;
1182 else if (!strncmp(ptr, "GiB", strlen("GiB")))
1183 ctx.reserve_size = 1 << 30;
1184 else
1185 panic("Syntax error in ring size param!\n");
1186 *ptr = 0;
1188 ctx.reserve_size *= strtol(optarg, NULL, 0);
1189 break;
1190 case 'b':
1191 cpu_tmp = strtol(optarg, NULL, 0);
1193 cpu_affinity(cpu_tmp);
1194 if (ctx.cpu != -2)
1195 ctx.cpu = cpu_tmp;
1196 break;
1197 case 'H':
1198 prio_high = true;
1199 break;
1200 case 'c':
1201 ctx.pcap = PCAP_OPS_RW;
1202 ops_touched = 1;
1203 break;
1204 case 'm':
1205 ctx.pcap = PCAP_OPS_MM;
1206 ops_touched = 1;
1207 break;
1208 case 'G':
1209 ctx.pcap = PCAP_OPS_SG;
1210 ops_touched = 1;
1211 break;
1212 case 'Q':
1213 ctx.cpu = -2;
1214 break;
1215 case 's':
1216 ctx.print_mode = PRINT_NONE;
1217 break;
1218 case 'q':
1219 ctx.print_mode = PRINT_LESS;
1220 break;
1221 case 'X':
1222 ctx.print_mode =
1223 (ctx.print_mode == PRINT_ASCII) ?
1224 PRINT_HEX_ASCII : PRINT_HEX;
1225 break;
1226 case 'l':
1227 ctx.print_mode =
1228 (ctx.print_mode == PRINT_HEX) ?
1229 PRINT_HEX_ASCII : PRINT_ASCII;
1230 break;
1231 case 'k':
1232 ctx.kpull = strtol(optarg, NULL, 0);
1233 break;
1234 case 'n':
1235 frame_count_max = strtol(optarg, NULL, 0);
1236 break;
1237 case 'F':
1238 ptr = optarg;
1239 ctx.dump_interval = 0;
1241 for (j = i = strlen(optarg); i > 0; --i) {
1242 if (!isdigit(optarg[j - i]))
1243 break;
1244 ptr++;
1247 if (!strncmp(ptr, "KiB", strlen("KiB"))) {
1248 ctx.dump_interval = 1 << 10;
1249 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1250 } else if (!strncmp(ptr, "MiB", strlen("MiB"))) {
1251 ctx.dump_interval = 1 << 20;
1252 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1253 } else if (!strncmp(ptr, "GiB", strlen("GiB"))) {
1254 ctx.dump_interval = 1 << 30;
1255 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1256 } else if (!strncmp(ptr, "sec", strlen("sec"))) {
1257 ctx.dump_interval = 1;
1258 ctx.dump_mode = DUMP_INTERVAL_TIME;
1259 } else if (!strncmp(ptr, "min", strlen("min"))) {
1260 ctx.dump_interval = 60;
1261 ctx.dump_mode = DUMP_INTERVAL_TIME;
1262 } else if (!strncmp(ptr, "hrs", strlen("hrs"))) {
1263 ctx.dump_interval = 60 * 60;
1264 ctx.dump_mode = DUMP_INTERVAL_TIME;
1265 } else if (!strncmp(ptr, "s", strlen("s"))) {
1266 ctx.dump_interval = 1;
1267 ctx.dump_mode = DUMP_INTERVAL_TIME;
1268 } else {
1269 panic("Syntax error in time/size param!\n");
1272 *ptr = 0;
1273 ctx.dump_interval *= strtol(optarg, NULL, 0);
1274 break;
1275 case 'V':
1276 ctx.verbose = 1;
1277 break;
1278 case 'B':
1279 ctx.dump_bpf = true;
1280 break;
1281 case 'D':
1282 pcap_dump_type_features();
1283 die();
1284 break;
1285 case 'U':
1286 update_geoip();
1287 die();
1288 break;
1289 case 'v':
1290 version();
1291 break;
1292 case 'h':
1293 help();
1294 break;
1295 case '?':
1296 switch (optopt) {
1297 case 'd':
1298 case 'i':
1299 case 'o':
1300 case 'f':
1301 case 't':
1302 case 'P':
1303 case 'F':
1304 case 'n':
1305 case 'S':
1306 case 'b':
1307 case 'k':
1308 case 'T':
1309 case 'u':
1310 case 'g':
1311 case 'e':
1312 panic("Option -%c requires an argument!\n",
1313 optopt);
1314 default:
1315 if (isprint(optopt))
1316 printf("Unknown option character `0x%X\'!\n", optopt);
1317 die();
1319 default:
1320 break;
1324 if (!ctx.filter && optind != argc) {
1325 int ret;
1326 off_t offset = 0;
1328 for (i = optind; i < argc; ++i) {
1329 size_t alen = strlen(argv[i]) + 2;
1330 size_t flen = ctx.filter ? strlen(ctx.filter) : 0;
1332 ctx.filter = xrealloc(ctx.filter, 1, flen + alen);
1333 ret = slprintf(ctx.filter + offset, strlen(argv[i]) + 2, "%s ", argv[i]);
1334 if (ret < 0)
1335 panic("Cannot concatenate filter string!\n");
1336 else
1337 offset += ret;
1341 if (!ctx.device_in)
1342 ctx.device_in = xstrdup("any");
1344 register_signal(SIGINT, signal_handler);
1345 register_signal(SIGHUP, signal_handler);
1347 tprintf_init();
1349 if (prio_high) {
1350 set_proc_prio(get_default_proc_prio());
1351 set_sched_status(get_default_sched_policy(), get_default_sched_prio());
1354 if (ctx.device_in && (device_mtu(ctx.device_in) ||
1355 !strncmp("any", ctx.device_in, strlen(ctx.device_in)))) {
1356 if (!ctx.device_out) {
1357 ctx.dump = 0;
1358 main_loop = recv_only_or_dump;
1359 } else if (device_mtu(ctx.device_out)) {
1360 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1361 main_loop = receive_to_xmit;
1362 } else {
1363 ctx.dump = 1;
1364 register_signal_f(SIGALRM, timer_next_dump, SA_SIGINFO);
1365 main_loop = recv_only_or_dump;
1366 if (!ops_touched)
1367 ctx.pcap = PCAP_OPS_SG;
1369 } else {
1370 if (ctx.device_out && device_mtu(ctx.device_out)) {
1371 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1372 main_loop = pcap_to_xmit;
1373 if (!ops_touched)
1374 ctx.pcap = PCAP_OPS_MM;
1375 } else {
1376 main_loop = read_pcap;
1377 if (!ops_touched)
1378 ctx.pcap = PCAP_OPS_SG;
1382 bug_on(!main_loop);
1384 init_geoip(0);
1385 if (setsockmem)
1386 set_system_socket_memory(vals, array_size(vals));
1387 if (!ctx.enforce)
1388 xlockme();
1390 main_loop(&ctx);
1392 if (!ctx.enforce)
1393 xunlockme();
1394 if (setsockmem)
1395 reset_system_socket_memory(vals, array_size(vals));
1396 destroy_geoip();
1398 tprintf_cleanup();
1400 free(ctx.device_in);
1401 free(ctx.device_out);
1402 free(ctx.device_trans);
1403 free(ctx.prefix);
1405 return 0;