make: when mrproper, also remove Git tree unrelated files
[netsniff-ng.git] / netsniff-ng.c
blobecb9e9a74ef2b01a0e65ba4aa11ed58116b69501
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 ret = __pcap_io->pull_fhdr_pcap(fd, &ctx->magic, &ctx->link_type);
191 if (ret)
192 panic("Error reading pcap header!\n");
194 if (__pcap_io->prepare_access_pcap) {
195 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_RD, ctx->jumbo);
196 if (ret)
197 panic("Error prepare reading pcap!\n");
200 fmemset(&tx_ring, 0, sizeof(tx_ring));
201 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
203 if (ctx->rfraw) {
204 ctx->device_trans = xstrdup(ctx->device_out);
205 xfree(ctx->device_out);
207 enter_rfmon_mac80211(ctx->device_trans, &ctx->device_out);
208 if (ctx->link_type != LINKTYPE_IEEE802_11)
209 panic("Wrong linktype of pcap!\n");
212 ifindex = device_ifindex(ctx->device_out);
214 size = ring_size(ctx->device_out, ctx->reserve_size);
216 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
217 if (ctx->dump_bpf)
218 bpf_dump_all(&bpf_ops);
220 set_packet_loss_discard(tx_sock);
221 set_sockopt_hwtimestamp(tx_sock, ctx->device_out);
223 setup_tx_ring_layout(tx_sock, &tx_ring, size, ctx->jumbo);
224 create_tx_ring(tx_sock, &tx_ring, ctx->verbose);
225 mmap_tx_ring(tx_sock, &tx_ring);
226 alloc_tx_ring_frames(&tx_ring);
227 bind_tx_ring(tx_sock, &tx_ring, ifindex);
229 dissector_init_all(ctx->print_mode);
231 if (ctx->cpu >= 0 && ifindex > 0) {
232 irq = device_irq_number(ctx->device_out);
233 device_bind_irq_to_cpu(irq, ctx->cpu);
235 if (ctx->verbose)
236 printf("IRQ: %s:%d > CPU%d\n",
237 ctx->device_out, irq, ctx->cpu);
240 if (ctx->kpull)
241 interval = ctx->kpull;
243 set_itimer_interval_value(&itimer, 0, interval);
244 setitimer(ITIMER_REAL, &itimer, NULL);
246 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
248 printf("Running! Hang up with ^C!\n\n");
249 fflush(stdout);
251 bug_on(gettimeofday(&start, NULL));
253 while (likely(sigint == 0)) {
254 while (user_may_pull_from_tx(tx_ring.frames[it].iov_base)) {
255 hdr = tx_ring.frames[it].iov_base;
256 out = ((uint8_t *) hdr) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
258 do {
259 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic, out,
260 ring_frame_size(&tx_ring));
261 if (unlikely(ret <= 0))
262 goto out;
264 if (ring_frame_size(&tx_ring) <
265 pcap_get_length(&phdr, ctx->magic)) {
266 pcap_set_length(&phdr, ctx->magic,
267 ring_frame_size(&tx_ring));
268 trunced++;
270 } while (ctx->filter &&
271 !bpf_run_filter(&bpf_ops, out,
272 pcap_get_length(&phdr, ctx->magic)));
274 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &hdr->tp_h, &hdr->s_ll);
276 ctx->tx_bytes += hdr->tp_h.tp_len;;
277 ctx->tx_packets++;
279 show_frame_hdr(hdr, ctx->print_mode);
281 dissector_entry_point(out, hdr->tp_h.tp_snaplen,
282 ctx->link_type, ctx->print_mode);
284 kernel_may_pull_from_tx(&hdr->tp_h);
286 it++;
287 if (it >= tx_ring.layout.tp_frame_nr)
288 it = 0;
290 if (unlikely(sigint == 1))
291 break;
293 if (frame_count_max != 0) {
294 if (ctx->tx_packets >= frame_count_max) {
295 sigint = 1;
296 break;
302 out:
304 bug_on(gettimeofday(&end, NULL));
305 timersub(&end, &start, &diff);
307 timer_purge();
309 bpf_release(&bpf_ops);
311 dissector_cleanup_all();
312 destroy_tx_ring(tx_sock, &tx_ring);
314 if (ctx->rfraw)
315 leave_rfmon_mac80211(ctx->device_trans, ctx->device_out);
317 if (__pcap_io->prepare_close_pcap)
318 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
320 if (!strncmp("-", ctx->device_in, strlen("-")))
321 dup2(fd, fileno(stdin));
322 close(fd);
324 close(tx_sock);
326 fflush(stdout);
327 printf("\n");
328 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
329 printf("\r%12lu packets truncated in file\n", trunced);
330 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
331 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
334 static void receive_to_xmit(struct ctx *ctx)
336 short ifflags = 0;
337 uint8_t *in, *out;
338 int rx_sock, ifindex_in, ifindex_out;
339 unsigned int size_in, size_out, it_in = 0, it_out = 0;
340 unsigned long frame_count = 0;
341 struct frame_map *hdr_in, *hdr_out;
342 struct ring tx_ring, rx_ring;
343 struct pollfd rx_poll;
344 struct sock_fprog bpf_ops;
346 if (!strncmp(ctx->device_in, ctx->device_out, IFNAMSIZ))
347 panic("Ingress/egress devices must be different!\n");
348 if (!device_up_and_running(ctx->device_out))
349 panic("Egress device not up and running!\n");
350 if (!device_up_and_running(ctx->device_in))
351 panic("Ingress 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 ret = __pcap_io->pull_fhdr_pcap(fd, &ctx->magic, &ctx->link_type);
551 if (ret)
552 panic("Error reading pcap header!\n");
554 if (__pcap_io->prepare_access_pcap) {
555 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_RD, ctx->jumbo);
556 if (ret)
557 panic("Error prepare reading pcap!\n");
560 fmemset(&fm, 0, sizeof(fm));
561 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
563 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
564 if (ctx->dump_bpf)
565 bpf_dump_all(&bpf_ops);
567 dissector_init_all(ctx->print_mode);
569 out_len = round_up(1024 * 1024, PAGE_SIZE);
570 out = xmalloc_aligned(out_len, CO_CACHE_LINE_SIZE);
572 if (ctx->device_out) {
573 if (!strncmp("-", ctx->device_out, strlen("-"))) {
574 fdo = dup(fileno(stdout));
575 close(fileno(stdout));
576 } else {
577 fdo = open_or_die_m(ctx->device_out, O_RDWR | O_CREAT |
578 O_TRUNC | O_LARGEFILE, DEFFILEMODE);
582 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
584 printf("Running! Hang up with ^C!\n\n");
585 fflush(stdout);
587 bug_on(gettimeofday(&start, NULL));
589 while (likely(sigint == 0)) {
590 do {
591 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic,
592 out, out_len);
593 if (unlikely(ret < 0))
594 goto out;
596 if (unlikely(pcap_get_length(&phdr, ctx->magic) == 0)) {
597 trunced++;
598 continue;
601 if (unlikely(pcap_get_length(&phdr, ctx->magic) > out_len)) {
602 pcap_set_length(&phdr, ctx->magic, out_len);
603 trunced++;
605 } while (ctx->filter &&
606 !bpf_run_filter(&bpf_ops, out,
607 pcap_get_length(&phdr, ctx->magic)));
609 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &fm.tp_h, &sll);
611 ctx->tx_bytes += fm.tp_h.tp_len;
612 ctx->tx_packets++;
614 show_frame_hdr(&fm, ctx->print_mode);
616 dissector_entry_point(out, fm.tp_h.tp_snaplen,
617 ctx->link_type, ctx->print_mode);
619 if (ctx->device_out)
620 translate_pcap_to_txf(fdo, out, fm.tp_h.tp_snaplen);
622 if (frame_count_max != 0) {
623 if (ctx->tx_packets >= frame_count_max) {
624 sigint = 1;
625 break;
630 out:
632 bug_on(gettimeofday(&end, NULL));
633 timersub(&end, &start, &diff);
635 bpf_release(&bpf_ops);
637 dissector_cleanup_all();
639 if (__pcap_io->prepare_close_pcap)
640 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
642 xfree(out);
644 fflush(stdout);
645 printf("\n");
646 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
647 printf("\r%12lu packets truncated in file\n", trunced);
648 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
649 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
651 if (!strncmp("-", ctx->device_in, strlen("-")))
652 dup2(fd, fileno(stdin));
653 close(fd);
655 if (ctx->device_out) {
656 if (!strncmp("-", ctx->device_out, strlen("-")))
657 dup2(fdo, fileno(stdout));
658 close(fdo);
662 static void finish_multi_pcap_file(struct ctx *ctx, int fd)
664 __pcap_io->fsync_pcap(fd);
666 if (__pcap_io->prepare_close_pcap)
667 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
669 close(fd);
671 fmemset(&itimer, 0, sizeof(itimer));
672 setitimer(ITIMER_REAL, &itimer, NULL);
675 static int next_multi_pcap_file(struct ctx *ctx, int fd)
677 int ret;
678 char fname[512];
680 __pcap_io->fsync_pcap(fd);
682 if (__pcap_io->prepare_close_pcap)
683 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
685 close(fd);
687 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
688 ctx->prefix ? : "dump-", time(0));
690 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
691 O_LARGEFILE, DEFFILEMODE);
693 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
694 if (ret)
695 panic("Error writing pcap header!\n");
697 if (__pcap_io->prepare_access_pcap) {
698 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, ctx->jumbo);
699 if (ret)
700 panic("Error prepare writing pcap!\n");
703 return fd;
706 static int begin_multi_pcap_file(struct ctx *ctx)
708 int fd, ret;
709 char fname[256];
711 bug_on(!__pcap_io);
713 if (ctx->device_out[strlen(ctx->device_out) - 1] == '/')
714 ctx->device_out[strlen(ctx->device_out) - 1] = 0;
716 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
717 ctx->prefix ? : "dump-", time(0));
719 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
720 O_LARGEFILE, DEFFILEMODE);
722 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
723 if (ret)
724 panic("Error writing pcap header!\n");
726 if (__pcap_io->prepare_access_pcap) {
727 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, ctx->jumbo);
728 if (ret)
729 panic("Error prepare writing pcap!\n");
732 if (ctx->dump_mode == DUMP_INTERVAL_TIME) {
733 interval = ctx->dump_interval;
735 set_itimer_interval_value(&itimer, interval, 0);
736 setitimer(ITIMER_REAL, &itimer, NULL);
737 } else {
738 interval = 0;
741 return fd;
744 static void finish_single_pcap_file(struct ctx *ctx, int fd)
746 __pcap_io->fsync_pcap(fd);
748 if (__pcap_io->prepare_close_pcap)
749 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
751 if (strncmp("-", ctx->device_out, strlen("-")))
752 close(fd);
753 else
754 dup2(fd, fileno(stdout));
757 static int begin_single_pcap_file(struct ctx *ctx)
759 int fd, ret;
761 bug_on(!__pcap_io);
763 if (!strncmp("-", ctx->device_out, strlen("-"))) {
764 fd = dup(fileno(stdout));
765 close(fileno(stdout));
766 if (ctx->pcap == PCAP_OPS_MM)
767 ctx->pcap = PCAP_OPS_SG;
768 } else {
769 fd = open_or_die_m(ctx->device_out,
770 O_RDWR | O_CREAT | O_TRUNC |
771 O_LARGEFILE, DEFFILEMODE);
774 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
775 if (ret)
776 panic("Error writing pcap header!\n");
778 if (__pcap_io->prepare_access_pcap) {
779 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, ctx->jumbo);
780 if (ret)
781 panic("Error prepare writing pcap!\n");
784 return fd;
787 static void print_pcap_file_stats(int sock, struct ctx *ctx, unsigned long skipped)
789 int ret;
790 unsigned long good, bad;
791 struct tpacket_stats kstats;
792 socklen_t slen = sizeof(kstats);
794 fmemset(&kstats, 0, sizeof(kstats));
796 ret = getsockopt(sock, SOL_PACKET, PACKET_STATISTICS, &kstats, &slen);
797 if (unlikely(ret))
798 panic("Cannot get packet statistics!\n");
800 if (ctx->print_mode == PRINT_NONE) {
801 good = kstats.tp_packets - kstats.tp_drops - skipped;
802 bad = kstats.tp_drops + skipped;
804 printf(".(+%lu/-%lu)", good, bad);
805 fflush(stdout);
809 static void recv_only_or_dump(struct ctx *ctx)
811 uint8_t *packet;
812 short ifflags = 0;
813 int sock, irq, ifindex, fd = 0, ret;
814 unsigned int size, it = 0;
815 unsigned long frame_count = 0, skipped = 0;
816 struct ring rx_ring;
817 struct pollfd rx_poll;
818 struct frame_map *hdr;
819 struct sock_fprog bpf_ops;
820 struct timeval start, end, diff;
821 pcap_pkthdr_t phdr;
823 if (!device_up_and_running(ctx->device_in) && !ctx->rfraw)
824 panic("Device not up and running!\n");
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 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
876 if (dump_to_pcap(ctx)) {
877 __label__ try_file;
878 struct stat stats;
880 fmemset(&stats, 0, sizeof(stats));
881 ret = stat(ctx->device_out, &stats);
882 if (ret < 0) {
883 ctx->dump_dir = 0;
884 goto try_file;
887 ctx->dump_dir = S_ISDIR(stats.st_mode);
888 if (ctx->dump_dir) {
889 fd = begin_multi_pcap_file(ctx);
890 } else {
891 try_file:
892 fd = begin_single_pcap_file(ctx);
896 printf("Running! Hang up with ^C!\n\n");
897 fflush(stdout);
899 bug_on(gettimeofday(&start, NULL));
901 while (likely(sigint == 0)) {
902 while (user_may_pull_from_rx(rx_ring.frames[it].iov_base)) {
903 __label__ next;
905 hdr = rx_ring.frames[it].iov_base;
906 packet = ((uint8_t *) hdr) + hdr->tp_h.tp_mac;
907 frame_count++;
909 if (ctx->packet_type != -1)
910 if (ctx->packet_type != hdr->s_ll.sll_pkttype)
911 goto next;
913 if (unlikely(ring_frame_size(&rx_ring) < hdr->tp_h.tp_snaplen)) {
914 skipped++;
915 goto next;
918 if (dump_to_pcap(ctx)) {
919 tpacket_hdr_to_pcap_pkthdr(&hdr->tp_h, &hdr->s_ll, &phdr, ctx->magic);
921 ret = __pcap_io->write_pcap(fd, &phdr, ctx->magic, packet,
922 pcap_get_length(&phdr, ctx->magic));
923 if (unlikely(ret != pcap_get_total_length(&phdr, ctx->magic)))
924 panic("Write error to pcap!\n");
927 show_frame_hdr(hdr, ctx->print_mode);
929 dissector_entry_point(packet, hdr->tp_h.tp_snaplen,
930 ctx->link_type, ctx->print_mode);
932 if (frame_count_max != 0) {
933 if (frame_count >= frame_count_max) {
934 sigint = 1;
935 break;
939 next:
941 kernel_may_pull_from_rx(&hdr->tp_h);
943 it++;
944 if (it >= rx_ring.layout.tp_frame_nr)
945 it = 0;
947 if (unlikely(sigint == 1))
948 break;
950 if (dump_to_pcap(ctx)) {
951 if (ctx->dump_mode == DUMP_INTERVAL_SIZE) {
952 interval += hdr->tp_h.tp_snaplen;
954 if (interval > ctx->dump_interval) {
955 next_dump = true;
956 interval = 0;
960 if (next_dump) {
961 fd = next_multi_pcap_file(ctx, fd);
962 next_dump = false;
964 if (ctx->verbose)
965 print_pcap_file_stats(sock, ctx, skipped);
970 poll(&rx_poll, 1, -1);
973 bug_on(gettimeofday(&end, NULL));
974 timersub(&end, &start, &diff);
976 if (!(ctx->dump_dir && ctx->print_mode == PRINT_NONE)) {
977 sock_print_net_stats(sock, skipped);
979 printf("\r%12lu sec, %lu usec in total\n",
980 diff.tv_sec, diff.tv_usec);
981 } else {
982 printf("\n\n");
983 fflush(stdout);
986 bpf_release(&bpf_ops);
987 dissector_cleanup_all();
988 destroy_rx_ring(sock, &rx_ring);
990 if (ctx->promiscuous)
991 leave_promiscuous_mode(ctx->device_in, ifflags);
993 if (ctx->rfraw)
994 leave_rfmon_mac80211(ctx->device_trans, ctx->device_in);
996 if (dump_to_pcap(ctx)) {
997 if (ctx->dump_dir)
998 finish_multi_pcap_file(ctx, fd);
999 else
1000 finish_single_pcap_file(ctx, fd);
1003 close(sock);
1006 static void help(void)
1008 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING);
1009 puts("http://www.netsniff-ng.org\n\n"
1010 "Usage: netsniff-ng [options] [filter-expression]\n"
1011 "Options:\n"
1012 " -i|-d|--dev|--in <dev|pcap|-> Input source as netdev, pcap or pcap stdin\n"
1013 " -o|--out <dev|pcap|dir|cfg|-> Output sink as netdev, pcap, directory, trafgen, or stdout\n"
1014 " -f|--filter <bpf-file|expr> Use BPF filter file from bpfc or tcpdump-like expression\n"
1015 " -t|--type <type> Filter for: host|broadcast|multicast|others|outgoing\n"
1016 " -F|--interval <size|time> Dump interval if -o is a dir: <num>KiB/MiB/GiB/s/sec/min/hrs\n"
1017 " -J|--jumbo-support Support for 64KB Super Jumbo Frames (def: 2048B)\n"
1018 " -R|--rfraw Capture or inject raw 802.11 frames\n"
1019 " -n|--num <0|uint> Number of packets until exit (def: 0)\n"
1020 " -P|--prefix <name> Prefix for pcaps stored in directory\n"
1021 " -T|--magic <pcap-magic> Pcap magic number/pcap format to store, see -D\n"
1022 " -D|--dump-pcap-types Dump pcap types and magic numbers and quit\n"
1023 " -B|--dump-bpf Dump generated BPF assembly\n"
1024 " -r|--rand Randomize packet forwarding order (dev->dev)\n"
1025 " -M|--no-promisc No promiscuous mode for netdev\n"
1026 " -A|--no-sock-mem Don't tune core socket memory\n"
1027 " -m|--mmap Mmap(2) pcap file i.e., for replaying pcaps\n"
1028 " -G|--sg Scatter/gather pcap file I/O\n"
1029 " -c|--clrw Use slower read(2)/write(2) I/O\n"
1030 " -S|--ring-size <size> Specify ring size to: <num>KiB/MiB/GiB\n"
1031 " -k|--kernel-pull <uint> Kernel pull from user interval in us (def: 10us)\n"
1032 " -b|--bind-cpu <cpu> Bind to specific CPU\n"
1033 " -u|--user <userid> Drop privileges and change to userid\n"
1034 " -g|--group <groupid> Drop privileges and change to groupid\n"
1035 " -H|--prio-high Make this high priority process\n"
1036 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
1037 " -s|--silent Do not print captured packets\n"
1038 " -q|--less Print less-verbose packet information\n"
1039 " -X|--hex Print packet data in hex format\n"
1040 " -l|--ascii Print human-readable packet data\n"
1041 " -U|--update Update GeoIP databases\n"
1042 " -V|--verbose Be more verbose\n"
1043 " -v|--version Show version\n"
1044 " -h|--help Guess what?!\n\n"
1045 "Examples:\n"
1046 " netsniff-ng --in eth0 --out dump.pcap -s -T 0xa1b2c3d4 --b 0 tcp or udp\n"
1047 " netsniff-ng --in wlan0 --rfraw --out dump.pcap --silent --bind-cpu 0\n"
1048 " netsniff-ng --in dump.pcap --mmap --out eth0 -k1000 --silent --bind-cpu 0\n"
1049 " netsniff-ng --in dump.pcap --out dump.cfg --silent --bind-cpu 0\n"
1050 " netsniff-ng --in eth0 --out eth1 --silent --bind-cpu 0 --type host\n"
1051 " netsniff-ng --in eth1 --out /opt/probe/ -s -m -J --interval 100MiB -b 0\n"
1052 " netsniff-ng --in vlan0 --out dump.pcap -c -u `id -u bob` -g `id -g bob`\n"
1053 " netsniff-ng --in any --filter http.bpf --jumbo-support --ascii -V\n\n"
1054 "Note:\n"
1055 " For introducing bit errors, delays with random variation and more\n"
1056 " while replaying pcaps, make use of tc(8) with its disciplines (e.g. netem).\n\n"
1057 "Please report bugs to <bugs@netsniff-ng.org>\n"
1058 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1059 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1060 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1061 "Swiss federal institute of technology (ETH Zurich)\n"
1062 "License: GNU GPL version 2.0\n"
1063 "This is free software: you are free to change and redistribute it.\n"
1064 "There is NO WARRANTY, to the extent permitted by law.\n");
1065 die();
1068 static void version(void)
1070 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING);
1071 puts("http://www.netsniff-ng.org\n\n"
1072 "Please report bugs to <bugs@netsniff-ng.org>\n"
1073 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1074 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1075 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1076 "Swiss federal institute of technology (ETH Zurich)\n"
1077 "License: GNU GPL version 2.0\n"
1078 "This is free software: you are free to change and redistribute it.\n"
1079 "There is NO WARRANTY, to the extent permitted by law.\n");
1080 die();
1083 int main(int argc, char **argv)
1085 char *ptr;
1086 int c, i, j, cpu_tmp, opt_index, ops_touched = 0, vals[4] = {0};
1087 bool prio_high = false, setsockmem = true;
1088 void (*main_loop)(struct ctx *ctx) = NULL;
1089 struct ctx ctx = {
1090 .link_type = LINKTYPE_EN10MB,
1091 .print_mode = PRINT_NORM,
1092 .cpu = -1,
1093 .packet_type = -1,
1094 .promiscuous = true,
1095 .randomize = false,
1096 .pcap = PCAP_OPS_SG,
1097 .dump_interval = 60,
1098 .dump_mode = DUMP_INTERVAL_TIME,
1099 .uid = getuid(),
1100 .gid = getgid(),
1101 .magic = ORIGINAL_TCPDUMP_MAGIC,
1104 srand(time(NULL));
1106 while ((c = getopt_long(argc, argv, short_options, long_options,
1107 &opt_index)) != EOF) {
1108 switch (c) {
1109 case 'd':
1110 case 'i':
1111 ctx.device_in = xstrdup(optarg);
1112 break;
1113 case 'o':
1114 ctx.device_out = xstrdup(optarg);
1115 break;
1116 case 'P':
1117 ctx.prefix = xstrdup(optarg);
1118 break;
1119 case 'R':
1120 ctx.link_type = LINKTYPE_IEEE802_11;
1121 ctx.rfraw = 1;
1122 break;
1123 case 'r':
1124 ctx.randomize = true;
1125 break;
1126 case 'J':
1127 ctx.jumbo = true;
1128 break;
1129 case 'T':
1130 ctx.magic = (uint32_t) strtoul(optarg, NULL, 0);
1131 pcap_check_magic(ctx.magic);
1132 break;
1133 case 'f':
1134 ctx.filter = xstrdup(optarg);
1135 break;
1136 case 'M':
1137 ctx.promiscuous = false;
1138 break;
1139 case 'A':
1140 setsockmem = false;
1141 break;
1142 case 'u':
1143 ctx.uid = strtoul(optarg, NULL, 0);
1144 ctx.enforce = true;
1145 break;
1146 case 'g':
1147 ctx.gid = strtoul(optarg, NULL, 0);
1148 ctx.enforce = true;
1149 break;
1150 case 't':
1151 if (!strncmp(optarg, "host", strlen("host")))
1152 ctx.packet_type = PACKET_HOST;
1153 else if (!strncmp(optarg, "broadcast", strlen("broadcast")))
1154 ctx.packet_type = PACKET_BROADCAST;
1155 else if (!strncmp(optarg, "multicast", strlen("multicast")))
1156 ctx.packet_type = PACKET_MULTICAST;
1157 else if (!strncmp(optarg, "others", strlen("others")))
1158 ctx.packet_type = PACKET_OTHERHOST;
1159 else if (!strncmp(optarg, "outgoing", strlen("outgoing")))
1160 ctx.packet_type = PACKET_OUTGOING;
1161 else
1162 ctx.packet_type = -1;
1163 break;
1164 case 'S':
1165 ptr = optarg;
1166 ctx.reserve_size = 0;
1168 for (j = i = strlen(optarg); i > 0; --i) {
1169 if (!isdigit(optarg[j - i]))
1170 break;
1171 ptr++;
1174 if (!strncmp(ptr, "KiB", strlen("KiB")))
1175 ctx.reserve_size = 1 << 10;
1176 else if (!strncmp(ptr, "MiB", strlen("MiB")))
1177 ctx.reserve_size = 1 << 20;
1178 else if (!strncmp(ptr, "GiB", strlen("GiB")))
1179 ctx.reserve_size = 1 << 30;
1180 else
1181 panic("Syntax error in ring size param!\n");
1182 *ptr = 0;
1184 ctx.reserve_size *= strtol(optarg, NULL, 0);
1185 break;
1186 case 'b':
1187 cpu_tmp = strtol(optarg, NULL, 0);
1189 cpu_affinity(cpu_tmp);
1190 if (ctx.cpu != -2)
1191 ctx.cpu = cpu_tmp;
1192 break;
1193 case 'H':
1194 prio_high = true;
1195 break;
1196 case 'c':
1197 ctx.pcap = PCAP_OPS_RW;
1198 ops_touched = 1;
1199 break;
1200 case 'm':
1201 ctx.pcap = PCAP_OPS_MM;
1202 ops_touched = 1;
1203 break;
1204 case 'G':
1205 ctx.pcap = PCAP_OPS_SG;
1206 ops_touched = 1;
1207 break;
1208 case 'Q':
1209 ctx.cpu = -2;
1210 break;
1211 case 's':
1212 ctx.print_mode = PRINT_NONE;
1213 break;
1214 case 'q':
1215 ctx.print_mode = PRINT_LESS;
1216 break;
1217 case 'X':
1218 ctx.print_mode =
1219 (ctx.print_mode == PRINT_ASCII) ?
1220 PRINT_HEX_ASCII : PRINT_HEX;
1221 break;
1222 case 'l':
1223 ctx.print_mode =
1224 (ctx.print_mode == PRINT_HEX) ?
1225 PRINT_HEX_ASCII : PRINT_ASCII;
1226 break;
1227 case 'k':
1228 ctx.kpull = strtol(optarg, NULL, 0);
1229 break;
1230 case 'n':
1231 frame_count_max = strtol(optarg, NULL, 0);
1232 break;
1233 case 'F':
1234 ptr = optarg;
1235 ctx.dump_interval = 0;
1237 for (j = i = strlen(optarg); i > 0; --i) {
1238 if (!isdigit(optarg[j - i]))
1239 break;
1240 ptr++;
1243 if (!strncmp(ptr, "KiB", strlen("KiB"))) {
1244 ctx.dump_interval = 1 << 10;
1245 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1246 } else if (!strncmp(ptr, "MiB", strlen("MiB"))) {
1247 ctx.dump_interval = 1 << 20;
1248 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1249 } else if (!strncmp(ptr, "GiB", strlen("GiB"))) {
1250 ctx.dump_interval = 1 << 30;
1251 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1252 } else if (!strncmp(ptr, "sec", strlen("sec"))) {
1253 ctx.dump_interval = 1;
1254 ctx.dump_mode = DUMP_INTERVAL_TIME;
1255 } else if (!strncmp(ptr, "min", strlen("min"))) {
1256 ctx.dump_interval = 60;
1257 ctx.dump_mode = DUMP_INTERVAL_TIME;
1258 } else if (!strncmp(ptr, "hrs", strlen("hrs"))) {
1259 ctx.dump_interval = 60 * 60;
1260 ctx.dump_mode = DUMP_INTERVAL_TIME;
1261 } else if (!strncmp(ptr, "s", strlen("s"))) {
1262 ctx.dump_interval = 1;
1263 ctx.dump_mode = DUMP_INTERVAL_TIME;
1264 } else {
1265 panic("Syntax error in time/size param!\n");
1268 *ptr = 0;
1269 ctx.dump_interval *= strtol(optarg, NULL, 0);
1270 break;
1271 case 'V':
1272 ctx.verbose = 1;
1273 break;
1274 case 'B':
1275 ctx.dump_bpf = true;
1276 break;
1277 case 'D':
1278 pcap_dump_type_features();
1279 die();
1280 break;
1281 case 'U':
1282 update_geoip();
1283 die();
1284 break;
1285 case 'v':
1286 version();
1287 break;
1288 case 'h':
1289 help();
1290 break;
1291 case '?':
1292 switch (optopt) {
1293 case 'd':
1294 case 'i':
1295 case 'o':
1296 case 'f':
1297 case 't':
1298 case 'P':
1299 case 'F':
1300 case 'n':
1301 case 'S':
1302 case 'b':
1303 case 'k':
1304 case 'T':
1305 case 'u':
1306 case 'g':
1307 case 'e':
1308 panic("Option -%c requires an argument!\n",
1309 optopt);
1310 default:
1311 if (isprint(optopt))
1312 printf("Unknown option character `0x%X\'!\n", optopt);
1313 die();
1315 default:
1316 break;
1320 if (!ctx.filter && optind != argc) {
1321 int ret;
1322 off_t offset = 0;
1324 for (i = optind; i < argc; ++i) {
1325 size_t alen = strlen(argv[i]) + 2;
1326 size_t flen = ctx.filter ? strlen(ctx.filter) : 0;
1328 ctx.filter = xrealloc(ctx.filter, 1, flen + alen);
1329 ret = slprintf(ctx.filter + offset, strlen(argv[i]) + 2, "%s ", argv[i]);
1330 if (ret < 0)
1331 panic("Cannot concatenate filter string!\n");
1332 else
1333 offset += ret;
1337 if (!ctx.device_in)
1338 ctx.device_in = xstrdup("any");
1340 register_signal(SIGINT, signal_handler);
1341 register_signal(SIGHUP, signal_handler);
1343 tprintf_init();
1345 if (prio_high) {
1346 set_proc_prio(get_default_proc_prio());
1347 set_sched_status(get_default_sched_policy(), get_default_sched_prio());
1350 if (ctx.device_in && (device_mtu(ctx.device_in) ||
1351 !strncmp("any", ctx.device_in, strlen(ctx.device_in)))) {
1352 if (!ctx.device_out) {
1353 ctx.dump = 0;
1354 main_loop = recv_only_or_dump;
1355 } else if (device_mtu(ctx.device_out)) {
1356 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1357 main_loop = receive_to_xmit;
1358 } else {
1359 ctx.dump = 1;
1360 register_signal_f(SIGALRM, timer_next_dump, SA_SIGINFO);
1361 main_loop = recv_only_or_dump;
1362 if (!ops_touched)
1363 ctx.pcap = PCAP_OPS_SG;
1365 } else {
1366 if (ctx.device_out && device_mtu(ctx.device_out)) {
1367 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1368 main_loop = pcap_to_xmit;
1369 if (!ops_touched)
1370 ctx.pcap = PCAP_OPS_MM;
1371 } else {
1372 main_loop = read_pcap;
1373 if (!ops_touched)
1374 ctx.pcap = PCAP_OPS_SG;
1378 bug_on(!main_loop);
1380 init_geoip(0);
1381 if (setsockmem)
1382 set_system_socket_memory(vals, array_size(vals));
1383 xlockme();
1385 main_loop(&ctx);
1387 xunlockme();
1388 if (setsockmem)
1389 reset_system_socket_memory(vals, array_size(vals));
1390 destroy_geoip();
1392 tprintf_cleanup();
1394 free(ctx.device_in);
1395 free(ctx.device_out);
1396 free(ctx.device_trans);
1397 free(ctx.prefix);
1399 return 0;