pcap_io: minor: fix some quirks
[netsniff-ng.git] / netsniff-ng.c
blob2391d3903017bc0f5e73ec1c447d44aef760247e
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);
225 setup_tx_ring_layout(tx_sock, &tx_ring, size, ctx->jumbo);
226 create_tx_ring(tx_sock, &tx_ring, ctx->verbose);
227 mmap_tx_ring(tx_sock, &tx_ring);
228 alloc_tx_ring_frames(&tx_ring);
229 bind_tx_ring(tx_sock, &tx_ring, ifindex);
231 dissector_init_all(ctx->print_mode);
233 if (ctx->cpu >= 0 && ifindex > 0) {
234 irq = device_irq_number(ctx->device_out);
235 device_bind_irq_to_cpu(irq, ctx->cpu);
237 if (ctx->verbose)
238 printf("IRQ: %s:%d > CPU%d\n",
239 ctx->device_out, irq, ctx->cpu);
242 if (ctx->kpull)
243 interval = ctx->kpull;
245 set_itimer_interval_value(&itimer, 0, interval);
246 setitimer(ITIMER_REAL, &itimer, NULL);
248 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
250 printf("Running! Hang up with ^C!\n\n");
251 fflush(stdout);
253 bug_on(gettimeofday(&start, NULL));
255 while (likely(sigint == 0)) {
256 while (user_may_pull_from_tx(tx_ring.frames[it].iov_base)) {
257 hdr = tx_ring.frames[it].iov_base;
258 out = ((uint8_t *) hdr) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
260 do {
261 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic, out,
262 ring_frame_size(&tx_ring));
263 if (unlikely(ret <= 0))
264 goto out;
266 if (ring_frame_size(&tx_ring) <
267 pcap_get_length(&phdr, ctx->magic)) {
268 pcap_set_length(&phdr, ctx->magic,
269 ring_frame_size(&tx_ring));
270 trunced++;
272 } while (ctx->filter &&
273 !bpf_run_filter(&bpf_ops, out,
274 pcap_get_length(&phdr, ctx->magic)));
276 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &hdr->tp_h, &hdr->s_ll);
278 ctx->tx_bytes += hdr->tp_h.tp_len;;
279 ctx->tx_packets++;
281 show_frame_hdr(hdr, ctx->print_mode);
283 dissector_entry_point(out, hdr->tp_h.tp_snaplen,
284 ctx->link_type, ctx->print_mode);
286 kernel_may_pull_from_tx(&hdr->tp_h);
288 it++;
289 if (it >= tx_ring.layout.tp_frame_nr)
290 it = 0;
292 if (unlikely(sigint == 1))
293 break;
295 if (frame_count_max != 0) {
296 if (ctx->tx_packets >= frame_count_max) {
297 sigint = 1;
298 break;
304 out:
306 bug_on(gettimeofday(&end, NULL));
307 timersub(&end, &start, &diff);
309 timer_purge();
311 bpf_release(&bpf_ops);
313 dissector_cleanup_all();
314 destroy_tx_ring(tx_sock, &tx_ring);
316 if (ctx->rfraw)
317 leave_rfmon_mac80211(ctx->device_trans, ctx->device_out);
319 if (__pcap_io->prepare_close_pcap)
320 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
322 if (!strncmp("-", ctx->device_in, strlen("-")))
323 dup2(fd, fileno(stdin));
324 close(fd);
326 close(tx_sock);
328 fflush(stdout);
329 printf("\n");
330 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
331 printf("\r%12lu packets truncated in file\n", trunced);
332 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
333 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
336 static void receive_to_xmit(struct ctx *ctx)
338 short ifflags = 0;
339 uint8_t *in, *out;
340 int rx_sock, ifindex_in, ifindex_out;
341 unsigned int size_in, size_out, it_in = 0, it_out = 0;
342 unsigned long frame_count = 0;
343 struct frame_map *hdr_in, *hdr_out;
344 struct ring tx_ring, rx_ring;
345 struct pollfd rx_poll;
346 struct sock_fprog bpf_ops;
348 if (!strncmp(ctx->device_in, ctx->device_out, IFNAMSIZ))
349 panic("Ingress/egress devices must be different!\n");
350 if (!device_up_and_running(ctx->device_out))
351 panic("Egress device not up and running!\n");
353 rx_sock = pf_socket();
354 tx_sock = pf_socket();
356 fmemset(&tx_ring, 0, sizeof(tx_ring));
357 fmemset(&rx_ring, 0, sizeof(rx_ring));
358 fmemset(&rx_poll, 0, sizeof(rx_poll));
359 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
361 ifindex_in = device_ifindex(ctx->device_in);
362 ifindex_out = device_ifindex(ctx->device_out);
364 size_in = ring_size(ctx->device_in, ctx->reserve_size);
365 size_out = ring_size(ctx->device_out, ctx->reserve_size);
367 enable_kernel_bpf_jit_compiler();
369 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
370 if (ctx->dump_bpf)
371 bpf_dump_all(&bpf_ops);
372 bpf_attach_to_sock(rx_sock, &bpf_ops);
374 setup_rx_ring_layout(rx_sock, &rx_ring, size_in, ctx->jumbo);
375 create_rx_ring(rx_sock, &rx_ring, ctx->verbose);
376 mmap_rx_ring(rx_sock, &rx_ring);
377 alloc_rx_ring_frames(&rx_ring);
378 bind_rx_ring(rx_sock, &rx_ring, ifindex_in);
379 prepare_polling(rx_sock, &rx_poll);
381 set_packet_loss_discard(tx_sock);
382 setup_tx_ring_layout(tx_sock, &tx_ring, size_out, ctx->jumbo);
383 create_tx_ring(tx_sock, &tx_ring, ctx->verbose);
384 mmap_tx_ring(tx_sock, &tx_ring);
385 alloc_tx_ring_frames(&tx_ring);
386 bind_tx_ring(tx_sock, &tx_ring, ifindex_out);
388 dissector_init_all(ctx->print_mode);
390 if (ctx->promiscuous)
391 ifflags = enter_promiscuous_mode(ctx->device_in);
393 if (ctx->kpull)
394 interval = ctx->kpull;
396 set_itimer_interval_value(&itimer, 0, interval);
397 setitimer(ITIMER_REAL, &itimer, NULL);
399 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
401 printf("Running! Hang up with ^C!\n\n");
402 fflush(stdout);
404 while (likely(sigint == 0)) {
405 while (user_may_pull_from_rx(rx_ring.frames[it_in].iov_base)) {
406 __label__ next;
408 hdr_in = rx_ring.frames[it_in].iov_base;
409 in = ((uint8_t *) hdr_in) + hdr_in->tp_h.tp_mac;
411 frame_count++;
413 if (ctx->packet_type != -1)
414 if (ctx->packet_type != hdr_in->s_ll.sll_pkttype)
415 goto next;
417 hdr_out = tx_ring.frames[it_out].iov_base;
418 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
420 for (; !user_may_pull_from_tx(tx_ring.frames[it_out].iov_base) &&
421 likely(!sigint);) {
422 if (ctx->randomize)
423 next_rnd_slot(&it_out, &tx_ring);
424 else {
425 it_out++;
426 if (it_out >= tx_ring.layout.tp_frame_nr)
427 it_out = 0;
430 hdr_out = tx_ring.frames[it_out].iov_base;
431 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
434 tpacket_hdr_clone(&hdr_out->tp_h, &hdr_in->tp_h);
435 fmemcpy(out, in, hdr_in->tp_h.tp_len);
437 kernel_may_pull_from_tx(&hdr_out->tp_h);
438 if (ctx->randomize)
439 next_rnd_slot(&it_out, &tx_ring);
440 else {
441 it_out++;
442 if (it_out >= tx_ring.layout.tp_frame_nr)
443 it_out = 0;
446 show_frame_hdr(hdr_in, ctx->print_mode);
448 dissector_entry_point(in, hdr_in->tp_h.tp_snaplen,
449 ctx->link_type, ctx->print_mode);
451 if (frame_count_max != 0) {
452 if (frame_count >= frame_count_max) {
453 sigint = 1;
454 break;
458 next:
460 kernel_may_pull_from_rx(&hdr_in->tp_h);
462 it_in++;
463 if (it_in >= rx_ring.layout.tp_frame_nr)
464 it_in = 0;
466 if (unlikely(sigint == 1))
467 goto out;
470 poll(&rx_poll, 1, -1);
473 out:
475 timer_purge();
477 sock_print_net_stats(rx_sock, 0);
479 bpf_release(&bpf_ops);
481 dissector_cleanup_all();
483 destroy_tx_ring(tx_sock, &tx_ring);
484 destroy_rx_ring(rx_sock, &rx_ring);
486 if (ctx->promiscuous)
487 leave_promiscuous_mode(ctx->device_in, ifflags);
489 close(tx_sock);
490 close(rx_sock);
493 static void translate_pcap_to_txf(int fdo, uint8_t *out, size_t len)
495 size_t bytes_done = 0;
496 char bout[80];
498 slprintf(bout, sizeof(bout), "{\n ");
499 write_or_die(fdo, bout, strlen(bout));
501 while (bytes_done < len) {
502 slprintf(bout, sizeof(bout), "0x%02x, ", out[bytes_done]);
503 write_or_die(fdo, bout, strlen(bout));
505 bytes_done++;
507 if (bytes_done % 10 == 0) {
508 slprintf(bout, sizeof(bout), "\n");
509 write_or_die(fdo, bout, strlen(bout));
511 if (bytes_done < len) {
512 slprintf(bout, sizeof(bout), " ");
513 write_or_die(fdo, bout, strlen(bout));
517 if (bytes_done % 10 != 0) {
518 slprintf(bout, sizeof(bout), "\n");
519 write_or_die(fdo, bout, strlen(bout));
522 slprintf(bout, sizeof(bout), "}\n\n");
523 write_or_die(fdo, bout, strlen(bout));
526 static void read_pcap(struct ctx *ctx)
528 __label__ out;
529 uint8_t *out;
530 int ret, fd, fdo = 0;
531 unsigned long trunced = 0;
532 size_t out_len;
533 pcap_pkthdr_t phdr;
534 struct sock_fprog bpf_ops;
535 struct frame_map fm;
536 struct timeval start, end, diff;
537 struct sockaddr_ll sll;
539 bug_on(!__pcap_io);
541 if (!strncmp("-", ctx->device_in, strlen("-"))) {
542 fd = dup(fileno(stdin));
543 close(fileno(stdin));
544 if (ctx->pcap == PCAP_OPS_MM)
545 ctx->pcap = PCAP_OPS_SG;
546 } else {
547 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
550 if (__pcap_io->init_once_pcap)
551 __pcap_io->init_once_pcap();
553 ret = __pcap_io->pull_fhdr_pcap(fd, &ctx->magic, &ctx->link_type);
554 if (ret)
555 panic("Error reading pcap header!\n");
557 if (__pcap_io->prepare_access_pcap) {
558 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_RD, ctx->jumbo);
559 if (ret)
560 panic("Error prepare reading pcap!\n");
563 fmemset(&fm, 0, sizeof(fm));
564 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
566 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
567 if (ctx->dump_bpf)
568 bpf_dump_all(&bpf_ops);
570 dissector_init_all(ctx->print_mode);
572 out_len = round_up(1024 * 1024, PAGE_SIZE);
573 out = xmalloc_aligned(out_len, CO_CACHE_LINE_SIZE);
575 if (ctx->device_out) {
576 if (!strncmp("-", ctx->device_out, strlen("-"))) {
577 fdo = dup(fileno(stdout));
578 close(fileno(stdout));
579 } else {
580 fdo = open_or_die_m(ctx->device_out, O_RDWR | O_CREAT |
581 O_TRUNC | O_LARGEFILE, DEFFILEMODE);
585 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
587 printf("Running! Hang up with ^C!\n\n");
588 fflush(stdout);
590 bug_on(gettimeofday(&start, NULL));
592 while (likely(sigint == 0)) {
593 do {
594 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic,
595 out, out_len);
596 if (unlikely(ret < 0))
597 goto out;
599 if (unlikely(pcap_get_length(&phdr, ctx->magic) == 0)) {
600 trunced++;
601 continue;
604 if (unlikely(pcap_get_length(&phdr, ctx->magic) > out_len)) {
605 pcap_set_length(&phdr, ctx->magic, out_len);
606 trunced++;
608 } while (ctx->filter &&
609 !bpf_run_filter(&bpf_ops, out,
610 pcap_get_length(&phdr, ctx->magic)));
612 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &fm.tp_h, &sll);
614 ctx->tx_bytes += fm.tp_h.tp_len;
615 ctx->tx_packets++;
617 show_frame_hdr(&fm, ctx->print_mode);
619 dissector_entry_point(out, fm.tp_h.tp_snaplen,
620 ctx->link_type, ctx->print_mode);
622 if (ctx->device_out)
623 translate_pcap_to_txf(fdo, out, fm.tp_h.tp_snaplen);
625 if (frame_count_max != 0) {
626 if (ctx->tx_packets >= frame_count_max) {
627 sigint = 1;
628 break;
633 out:
635 bug_on(gettimeofday(&end, NULL));
636 timersub(&end, &start, &diff);
638 bpf_release(&bpf_ops);
640 dissector_cleanup_all();
642 if (__pcap_io->prepare_close_pcap)
643 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
645 xfree(out);
647 fflush(stdout);
648 printf("\n");
649 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
650 printf("\r%12lu packets truncated in file\n", trunced);
651 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
652 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
654 if (!strncmp("-", ctx->device_in, strlen("-")))
655 dup2(fd, fileno(stdin));
656 close(fd);
658 if (ctx->device_out) {
659 if (!strncmp("-", ctx->device_out, strlen("-")))
660 dup2(fdo, fileno(stdout));
661 close(fdo);
665 static void finish_multi_pcap_file(struct ctx *ctx, int fd)
667 __pcap_io->fsync_pcap(fd);
669 if (__pcap_io->prepare_close_pcap)
670 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
672 close(fd);
674 fmemset(&itimer, 0, sizeof(itimer));
675 setitimer(ITIMER_REAL, &itimer, NULL);
678 static int next_multi_pcap_file(struct ctx *ctx, int fd)
680 int ret;
681 char fname[512];
683 __pcap_io->fsync_pcap(fd);
685 if (__pcap_io->prepare_close_pcap)
686 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
688 close(fd);
690 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
691 ctx->prefix ? : "dump-", time(0));
693 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
694 O_LARGEFILE, DEFFILEMODE);
696 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
697 if (ret)
698 panic("Error writing pcap header!\n");
700 if (__pcap_io->prepare_access_pcap) {
701 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, ctx->jumbo);
702 if (ret)
703 panic("Error prepare writing pcap!\n");
706 return fd;
709 static int begin_multi_pcap_file(struct ctx *ctx)
711 int fd, ret;
712 char fname[256];
714 bug_on(!__pcap_io);
716 if (ctx->device_out[strlen(ctx->device_out) - 1] == '/')
717 ctx->device_out[strlen(ctx->device_out) - 1] = 0;
719 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
720 ctx->prefix ? : "dump-", time(0));
722 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
723 O_LARGEFILE, DEFFILEMODE);
725 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
726 if (ret)
727 panic("Error writing pcap header!\n");
729 if (__pcap_io->prepare_access_pcap) {
730 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, ctx->jumbo);
731 if (ret)
732 panic("Error prepare writing pcap!\n");
735 if (ctx->dump_mode == DUMP_INTERVAL_TIME) {
736 interval = ctx->dump_interval;
738 set_itimer_interval_value(&itimer, interval, 0);
739 setitimer(ITIMER_REAL, &itimer, NULL);
740 } else {
741 interval = 0;
744 return fd;
747 static void finish_single_pcap_file(struct ctx *ctx, int fd)
749 __pcap_io->fsync_pcap(fd);
751 if (__pcap_io->prepare_close_pcap)
752 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
754 if (strncmp("-", ctx->device_out, strlen("-")))
755 close(fd);
756 else
757 dup2(fd, fileno(stdout));
760 static int begin_single_pcap_file(struct ctx *ctx)
762 int fd, ret;
764 bug_on(!__pcap_io);
766 if (!strncmp("-", ctx->device_out, strlen("-"))) {
767 fd = dup(fileno(stdout));
768 close(fileno(stdout));
769 if (ctx->pcap == PCAP_OPS_MM)
770 ctx->pcap = PCAP_OPS_SG;
771 } else {
772 fd = open_or_die_m(ctx->device_out,
773 O_RDWR | O_CREAT | O_TRUNC |
774 O_LARGEFILE, DEFFILEMODE);
777 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
778 if (ret)
779 panic("Error writing pcap header!\n");
781 if (__pcap_io->prepare_access_pcap) {
782 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, ctx->jumbo);
783 if (ret)
784 panic("Error prepare writing pcap!\n");
787 return fd;
790 static void print_pcap_file_stats(int sock, struct ctx *ctx, unsigned long skipped)
792 int ret;
793 unsigned long good, bad;
794 struct tpacket_stats kstats;
795 socklen_t slen = sizeof(kstats);
797 fmemset(&kstats, 0, sizeof(kstats));
799 ret = getsockopt(sock, SOL_PACKET, PACKET_STATISTICS, &kstats, &slen);
800 if (unlikely(ret))
801 panic("Cannot get packet statistics!\n");
803 if (ctx->print_mode == PRINT_NONE) {
804 good = kstats.tp_packets - kstats.tp_drops - skipped;
805 bad = kstats.tp_drops + skipped;
807 printf(".(+%lu/-%lu)", good, bad);
808 fflush(stdout);
812 static void recv_only_or_dump(struct ctx *ctx)
814 uint8_t *packet;
815 short ifflags = 0;
816 int sock, irq, ifindex, fd = 0, ret;
817 unsigned int size, it = 0;
818 unsigned long frame_count = 0, skipped = 0;
819 struct ring rx_ring;
820 struct pollfd rx_poll;
821 struct frame_map *hdr;
822 struct sock_fprog bpf_ops;
823 struct timeval start, end, diff;
824 pcap_pkthdr_t phdr;
826 sock = pf_socket();
828 if (ctx->rfraw) {
829 ctx->device_trans = xstrdup(ctx->device_in);
830 xfree(ctx->device_in);
832 enter_rfmon_mac80211(ctx->device_trans, &ctx->device_in);
833 ctx->link_type = LINKTYPE_IEEE802_11;
836 fmemset(&rx_ring, 0, sizeof(rx_ring));
837 fmemset(&rx_poll, 0, sizeof(rx_poll));
838 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
840 ifindex = device_ifindex(ctx->device_in);
842 size = ring_size(ctx->device_in, ctx->reserve_size);
844 enable_kernel_bpf_jit_compiler();
846 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
847 if (ctx->dump_bpf)
848 bpf_dump_all(&bpf_ops);
849 bpf_attach_to_sock(sock, &bpf_ops);
851 set_sockopt_hwtimestamp(sock, ctx->device_in);
853 setup_rx_ring_layout(sock, &rx_ring, size, ctx->jumbo);
854 create_rx_ring(sock, &rx_ring, ctx->verbose);
855 mmap_rx_ring(sock, &rx_ring);
856 alloc_rx_ring_frames(&rx_ring);
857 bind_rx_ring(sock, &rx_ring, ifindex);
859 prepare_polling(sock, &rx_poll);
860 dissector_init_all(ctx->print_mode);
862 if (ctx->cpu >= 0 && ifindex > 0) {
863 irq = device_irq_number(ctx->device_in);
864 device_bind_irq_to_cpu(irq, ctx->cpu);
866 if (ctx->verbose)
867 printf("IRQ: %s:%d > CPU%d\n",
868 ctx->device_in, irq, ctx->cpu);
871 if (ctx->promiscuous)
872 ifflags = enter_promiscuous_mode(ctx->device_in);
874 if (dump_to_pcap(ctx) && __pcap_io->init_once_pcap)
875 __pcap_io->init_once_pcap();
877 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
879 if (dump_to_pcap(ctx)) {
880 __label__ try_file;
881 struct stat stats;
883 fmemset(&stats, 0, sizeof(stats));
884 ret = stat(ctx->device_out, &stats);
885 if (ret < 0) {
886 ctx->dump_dir = 0;
887 goto try_file;
890 ctx->dump_dir = S_ISDIR(stats.st_mode);
891 if (ctx->dump_dir) {
892 fd = begin_multi_pcap_file(ctx);
893 } else {
894 try_file:
895 fd = begin_single_pcap_file(ctx);
899 printf("Running! Hang up with ^C!\n\n");
900 fflush(stdout);
902 bug_on(gettimeofday(&start, NULL));
904 while (likely(sigint == 0)) {
905 while (user_may_pull_from_rx(rx_ring.frames[it].iov_base)) {
906 __label__ next;
908 hdr = rx_ring.frames[it].iov_base;
909 packet = ((uint8_t *) hdr) + hdr->tp_h.tp_mac;
910 frame_count++;
912 if (ctx->packet_type != -1)
913 if (ctx->packet_type != hdr->s_ll.sll_pkttype)
914 goto next;
916 if (unlikely(ring_frame_size(&rx_ring) < hdr->tp_h.tp_snaplen)) {
917 skipped++;
918 goto next;
921 if (dump_to_pcap(ctx)) {
922 tpacket_hdr_to_pcap_pkthdr(&hdr->tp_h, &hdr->s_ll, &phdr, ctx->magic);
924 ret = __pcap_io->write_pcap(fd, &phdr, ctx->magic, packet,
925 pcap_get_length(&phdr, ctx->magic));
926 if (unlikely(ret != pcap_get_total_length(&phdr, ctx->magic)))
927 panic("Write error to pcap!\n");
930 show_frame_hdr(hdr, ctx->print_mode);
932 dissector_entry_point(packet, hdr->tp_h.tp_snaplen,
933 ctx->link_type, ctx->print_mode);
935 if (frame_count_max != 0) {
936 if (frame_count >= frame_count_max) {
937 sigint = 1;
938 break;
942 next:
944 kernel_may_pull_from_rx(&hdr->tp_h);
946 it++;
947 if (it >= rx_ring.layout.tp_frame_nr)
948 it = 0;
950 if (unlikely(sigint == 1))
951 break;
953 if (dump_to_pcap(ctx)) {
954 if (ctx->dump_mode == DUMP_INTERVAL_SIZE) {
955 interval += hdr->tp_h.tp_snaplen;
957 if (interval > ctx->dump_interval) {
958 next_dump = true;
959 interval = 0;
963 if (next_dump) {
964 fd = next_multi_pcap_file(ctx, fd);
965 next_dump = false;
967 if (ctx->verbose)
968 print_pcap_file_stats(sock, ctx, skipped);
973 poll(&rx_poll, 1, -1);
976 bug_on(gettimeofday(&end, NULL));
977 timersub(&end, &start, &diff);
979 if (!(ctx->dump_dir && ctx->print_mode == PRINT_NONE)) {
980 sock_print_net_stats(sock, skipped);
982 printf("\r%12lu sec, %lu usec in total\n",
983 diff.tv_sec, diff.tv_usec);
984 } else {
985 printf("\n\n");
986 fflush(stdout);
989 bpf_release(&bpf_ops);
990 dissector_cleanup_all();
991 destroy_rx_ring(sock, &rx_ring);
993 if (ctx->promiscuous)
994 leave_promiscuous_mode(ctx->device_in, ifflags);
996 if (ctx->rfraw)
997 leave_rfmon_mac80211(ctx->device_trans, ctx->device_in);
999 if (dump_to_pcap(ctx)) {
1000 if (ctx->dump_dir)
1001 finish_multi_pcap_file(ctx, fd);
1002 else
1003 finish_single_pcap_file(ctx, fd);
1006 close(sock);
1009 static void help(void)
1011 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING);
1012 puts("http://www.netsniff-ng.org\n\n"
1013 "Usage: netsniff-ng [options] [filter-expression]\n"
1014 "Options:\n"
1015 " -i|-d|--dev|--in <dev|pcap|-> Input source as netdev, pcap or pcap stdin\n"
1016 " -o|--out <dev|pcap|dir|cfg|-> Output sink as netdev, pcap, directory, trafgen, or stdout\n"
1017 " -f|--filter <bpf-file|expr> Use BPF filter file from bpfc or tcpdump-like expression\n"
1018 " -t|--type <type> Filter for: host|broadcast|multicast|others|outgoing\n"
1019 " -F|--interval <size|time> Dump interval if -o is a dir: <num>KiB/MiB/GiB/s/sec/min/hrs\n"
1020 " -J|--jumbo-support Support for 64KB Super Jumbo Frames (def: 2048B)\n"
1021 " -R|--rfraw Capture or inject raw 802.11 frames\n"
1022 " -n|--num <0|uint> Number of packets until exit (def: 0)\n"
1023 " -P|--prefix <name> Prefix for pcaps stored in directory\n"
1024 " -T|--magic <pcap-magic> Pcap magic number/pcap format to store, see -D\n"
1025 " -D|--dump-pcap-types Dump pcap types and magic numbers and quit\n"
1026 " -B|--dump-bpf Dump generated BPF assembly\n"
1027 " -r|--rand Randomize packet forwarding order (dev->dev)\n"
1028 " -M|--no-promisc No promiscuous mode for netdev\n"
1029 " -A|--no-sock-mem Don't tune core socket memory\n"
1030 " -m|--mmap Mmap(2) pcap file i.e., for replaying pcaps\n"
1031 " -G|--sg Scatter/gather pcap file I/O\n"
1032 " -c|--clrw Use slower read(2)/write(2) I/O\n"
1033 " -S|--ring-size <size> Specify ring size to: <num>KiB/MiB/GiB\n"
1034 " -k|--kernel-pull <uint> Kernel pull from user interval in us (def: 10us)\n"
1035 " -b|--bind-cpu <cpu> Bind to specific CPU\n"
1036 " -u|--user <userid> Drop privileges and change to userid\n"
1037 " -g|--group <groupid> Drop privileges and change to groupid\n"
1038 " -H|--prio-high Make this high priority process\n"
1039 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
1040 " -s|--silent Do not print captured packets\n"
1041 " -q|--less Print less-verbose packet information\n"
1042 " -X|--hex Print packet data in hex format\n"
1043 " -l|--ascii Print human-readable packet data\n"
1044 " -U|--update Update GeoIP databases\n"
1045 " -V|--verbose Be more verbose\n"
1046 " -v|--version Show version\n"
1047 " -h|--help Guess what?!\n\n"
1048 "Examples:\n"
1049 " netsniff-ng --in eth0 --out dump.pcap -s -T 0xa1b2c3d4 --b 0 tcp or udp\n"
1050 " netsniff-ng --in wlan0 --rfraw --out dump.pcap --silent --bind-cpu 0\n"
1051 " netsniff-ng --in dump.pcap --mmap --out eth0 -k1000 --silent --bind-cpu 0\n"
1052 " netsniff-ng --in dump.pcap --out dump.cfg --silent --bind-cpu 0\n"
1053 " netsniff-ng --in eth0 --out eth1 --silent --bind-cpu 0 --type host\n"
1054 " netsniff-ng --in eth1 --out /opt/probe/ -s -m -J --interval 100MiB -b 0\n"
1055 " netsniff-ng --in vlan0 --out dump.pcap -c -u `id -u bob` -g `id -g bob`\n"
1056 " netsniff-ng --in any --filter http.bpf --jumbo-support --ascii -V\n\n"
1057 "Note:\n"
1058 " For introducing bit errors, delays with random variation and more\n"
1059 " while replaying pcaps, make use of tc(8) with its disciplines (e.g. netem).\n\n"
1060 "Please report bugs to <bugs@netsniff-ng.org>\n"
1061 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1062 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1063 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1064 "Swiss federal institute of technology (ETH Zurich)\n"
1065 "License: GNU GPL version 2.0\n"
1066 "This is free software: you are free to change and redistribute it.\n"
1067 "There is NO WARRANTY, to the extent permitted by law.\n");
1068 die();
1071 static void version(void)
1073 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING);
1074 puts("http://www.netsniff-ng.org\n\n"
1075 "Please report bugs to <bugs@netsniff-ng.org>\n"
1076 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1077 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1078 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1079 "Swiss federal institute of technology (ETH Zurich)\n"
1080 "License: GNU GPL version 2.0\n"
1081 "This is free software: you are free to change and redistribute it.\n"
1082 "There is NO WARRANTY, to the extent permitted by law.\n");
1083 die();
1086 int main(int argc, char **argv)
1088 char *ptr;
1089 int c, i, j, cpu_tmp, opt_index, ops_touched = 0, vals[4] = {0};
1090 bool prio_high = false, setsockmem = true;
1091 void (*main_loop)(struct ctx *ctx) = NULL;
1092 struct ctx ctx = {
1093 .link_type = LINKTYPE_EN10MB,
1094 .print_mode = PRINT_NORM,
1095 .cpu = -1,
1096 .packet_type = -1,
1097 .promiscuous = true,
1098 .randomize = false,
1099 .pcap = PCAP_OPS_SG,
1100 .dump_interval = 60,
1101 .dump_mode = DUMP_INTERVAL_TIME,
1102 .uid = getuid(),
1103 .gid = getgid(),
1104 .magic = ORIGINAL_TCPDUMP_MAGIC,
1107 srand(time(NULL));
1109 while ((c = getopt_long(argc, argv, short_options, long_options,
1110 &opt_index)) != EOF) {
1111 switch (c) {
1112 case 'd':
1113 case 'i':
1114 ctx.device_in = xstrdup(optarg);
1115 break;
1116 case 'o':
1117 ctx.device_out = xstrdup(optarg);
1118 break;
1119 case 'P':
1120 ctx.prefix = xstrdup(optarg);
1121 break;
1122 case 'R':
1123 ctx.link_type = LINKTYPE_IEEE802_11;
1124 ctx.rfraw = 1;
1125 break;
1126 case 'r':
1127 ctx.randomize = true;
1128 break;
1129 case 'J':
1130 ctx.jumbo = true;
1131 break;
1132 case 'T':
1133 ctx.magic = (uint32_t) strtoul(optarg, NULL, 0);
1134 pcap_check_magic(ctx.magic);
1135 break;
1136 case 'f':
1137 ctx.filter = xstrdup(optarg);
1138 break;
1139 case 'M':
1140 ctx.promiscuous = false;
1141 break;
1142 case 'A':
1143 setsockmem = false;
1144 break;
1145 case 'u':
1146 ctx.uid = strtoul(optarg, NULL, 0);
1147 ctx.enforce = true;
1148 break;
1149 case 'g':
1150 ctx.gid = strtoul(optarg, NULL, 0);
1151 ctx.enforce = true;
1152 break;
1153 case 't':
1154 if (!strncmp(optarg, "host", strlen("host")))
1155 ctx.packet_type = PACKET_HOST;
1156 else if (!strncmp(optarg, "broadcast", strlen("broadcast")))
1157 ctx.packet_type = PACKET_BROADCAST;
1158 else if (!strncmp(optarg, "multicast", strlen("multicast")))
1159 ctx.packet_type = PACKET_MULTICAST;
1160 else if (!strncmp(optarg, "others", strlen("others")))
1161 ctx.packet_type = PACKET_OTHERHOST;
1162 else if (!strncmp(optarg, "outgoing", strlen("outgoing")))
1163 ctx.packet_type = PACKET_OUTGOING;
1164 else
1165 ctx.packet_type = -1;
1166 break;
1167 case 'S':
1168 ptr = optarg;
1169 ctx.reserve_size = 0;
1171 for (j = i = strlen(optarg); i > 0; --i) {
1172 if (!isdigit(optarg[j - i]))
1173 break;
1174 ptr++;
1177 if (!strncmp(ptr, "KiB", strlen("KiB")))
1178 ctx.reserve_size = 1 << 10;
1179 else if (!strncmp(ptr, "MiB", strlen("MiB")))
1180 ctx.reserve_size = 1 << 20;
1181 else if (!strncmp(ptr, "GiB", strlen("GiB")))
1182 ctx.reserve_size = 1 << 30;
1183 else
1184 panic("Syntax error in ring size param!\n");
1185 *ptr = 0;
1187 ctx.reserve_size *= strtol(optarg, NULL, 0);
1188 break;
1189 case 'b':
1190 cpu_tmp = strtol(optarg, NULL, 0);
1192 cpu_affinity(cpu_tmp);
1193 if (ctx.cpu != -2)
1194 ctx.cpu = cpu_tmp;
1195 break;
1196 case 'H':
1197 prio_high = true;
1198 break;
1199 case 'c':
1200 ctx.pcap = PCAP_OPS_RW;
1201 ops_touched = 1;
1202 break;
1203 case 'm':
1204 ctx.pcap = PCAP_OPS_MM;
1205 ops_touched = 1;
1206 break;
1207 case 'G':
1208 ctx.pcap = PCAP_OPS_SG;
1209 ops_touched = 1;
1210 break;
1211 case 'Q':
1212 ctx.cpu = -2;
1213 break;
1214 case 's':
1215 ctx.print_mode = PRINT_NONE;
1216 break;
1217 case 'q':
1218 ctx.print_mode = PRINT_LESS;
1219 break;
1220 case 'X':
1221 ctx.print_mode =
1222 (ctx.print_mode == PRINT_ASCII) ?
1223 PRINT_HEX_ASCII : PRINT_HEX;
1224 break;
1225 case 'l':
1226 ctx.print_mode =
1227 (ctx.print_mode == PRINT_HEX) ?
1228 PRINT_HEX_ASCII : PRINT_ASCII;
1229 break;
1230 case 'k':
1231 ctx.kpull = strtol(optarg, NULL, 0);
1232 break;
1233 case 'n':
1234 frame_count_max = strtol(optarg, NULL, 0);
1235 break;
1236 case 'F':
1237 ptr = optarg;
1238 ctx.dump_interval = 0;
1240 for (j = i = strlen(optarg); i > 0; --i) {
1241 if (!isdigit(optarg[j - i]))
1242 break;
1243 ptr++;
1246 if (!strncmp(ptr, "KiB", strlen("KiB"))) {
1247 ctx.dump_interval = 1 << 10;
1248 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1249 } else if (!strncmp(ptr, "MiB", strlen("MiB"))) {
1250 ctx.dump_interval = 1 << 20;
1251 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1252 } else if (!strncmp(ptr, "GiB", strlen("GiB"))) {
1253 ctx.dump_interval = 1 << 30;
1254 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1255 } else if (!strncmp(ptr, "sec", strlen("sec"))) {
1256 ctx.dump_interval = 1;
1257 ctx.dump_mode = DUMP_INTERVAL_TIME;
1258 } else if (!strncmp(ptr, "min", strlen("min"))) {
1259 ctx.dump_interval = 60;
1260 ctx.dump_mode = DUMP_INTERVAL_TIME;
1261 } else if (!strncmp(ptr, "hrs", strlen("hrs"))) {
1262 ctx.dump_interval = 60 * 60;
1263 ctx.dump_mode = DUMP_INTERVAL_TIME;
1264 } else if (!strncmp(ptr, "s", strlen("s"))) {
1265 ctx.dump_interval = 1;
1266 ctx.dump_mode = DUMP_INTERVAL_TIME;
1267 } else {
1268 panic("Syntax error in time/size param!\n");
1271 *ptr = 0;
1272 ctx.dump_interval *= strtol(optarg, NULL, 0);
1273 break;
1274 case 'V':
1275 ctx.verbose = 1;
1276 break;
1277 case 'B':
1278 ctx.dump_bpf = true;
1279 break;
1280 case 'D':
1281 pcap_dump_type_features();
1282 die();
1283 break;
1284 case 'U':
1285 update_geoip();
1286 die();
1287 break;
1288 case 'v':
1289 version();
1290 break;
1291 case 'h':
1292 help();
1293 break;
1294 case '?':
1295 switch (optopt) {
1296 case 'd':
1297 case 'i':
1298 case 'o':
1299 case 'f':
1300 case 't':
1301 case 'P':
1302 case 'F':
1303 case 'n':
1304 case 'S':
1305 case 'b':
1306 case 'k':
1307 case 'T':
1308 case 'u':
1309 case 'g':
1310 case 'e':
1311 panic("Option -%c requires an argument!\n",
1312 optopt);
1313 default:
1314 if (isprint(optopt))
1315 printf("Unknown option character `0x%X\'!\n", optopt);
1316 die();
1318 default:
1319 break;
1323 if (!ctx.filter && optind != argc) {
1324 int ret;
1325 off_t offset = 0;
1327 for (i = optind; i < argc; ++i) {
1328 size_t alen = strlen(argv[i]) + 2;
1329 size_t flen = ctx.filter ? strlen(ctx.filter) : 0;
1331 ctx.filter = xrealloc(ctx.filter, 1, flen + alen);
1332 ret = slprintf(ctx.filter + offset, strlen(argv[i]) + 2, "%s ", argv[i]);
1333 if (ret < 0)
1334 panic("Cannot concatenate filter string!\n");
1335 else
1336 offset += ret;
1340 if (!ctx.device_in)
1341 ctx.device_in = xstrdup("any");
1343 register_signal(SIGINT, signal_handler);
1344 register_signal(SIGHUP, signal_handler);
1346 tprintf_init();
1348 if (prio_high) {
1349 set_proc_prio(get_default_proc_prio());
1350 set_sched_status(get_default_sched_policy(), get_default_sched_prio());
1353 if (ctx.device_in && (device_mtu(ctx.device_in) ||
1354 !strncmp("any", ctx.device_in, strlen(ctx.device_in)))) {
1355 if (!ctx.device_out) {
1356 ctx.dump = 0;
1357 main_loop = recv_only_or_dump;
1358 } else if (device_mtu(ctx.device_out)) {
1359 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1360 main_loop = receive_to_xmit;
1361 } else {
1362 ctx.dump = 1;
1363 register_signal_f(SIGALRM, timer_next_dump, SA_SIGINFO);
1364 main_loop = recv_only_or_dump;
1365 if (!ops_touched)
1366 ctx.pcap = PCAP_OPS_SG;
1368 } else {
1369 if (ctx.device_out && device_mtu(ctx.device_out)) {
1370 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1371 main_loop = pcap_to_xmit;
1372 if (!ops_touched)
1373 ctx.pcap = PCAP_OPS_MM;
1374 } else {
1375 main_loop = read_pcap;
1376 if (!ops_touched)
1377 ctx.pcap = PCAP_OPS_SG;
1381 bug_on(!main_loop);
1383 init_geoip(0);
1384 if (setsockmem)
1385 set_system_socket_memory(vals, array_size(vals));
1386 if (!ctx.enforce)
1387 xlockme();
1389 main_loop(&ctx);
1391 if (!ctx.enforce)
1392 xunlockme();
1393 if (setsockmem)
1394 reset_system_socket_memory(vals, array_size(vals));
1395 destroy_geoip();
1397 tprintf_cleanup();
1399 free(ctx.device_in);
1400 free(ctx.device_out);
1401 free(ctx.device_trans);
1402 free(ctx.prefix);
1404 return 0;