man: finish bpfc man page
[netsniff-ng.git] / netsniff-ng.c
blobeaf2416ad4b72345941d8e7d767592b0625422d3
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");
351 rx_sock = pf_socket();
352 tx_sock = pf_socket();
354 fmemset(&tx_ring, 0, sizeof(tx_ring));
355 fmemset(&rx_ring, 0, sizeof(rx_ring));
356 fmemset(&rx_poll, 0, sizeof(rx_poll));
357 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
359 ifindex_in = device_ifindex(ctx->device_in);
360 ifindex_out = device_ifindex(ctx->device_out);
362 size_in = ring_size(ctx->device_in, ctx->reserve_size);
363 size_out = ring_size(ctx->device_out, ctx->reserve_size);
365 enable_kernel_bpf_jit_compiler();
367 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
368 if (ctx->dump_bpf)
369 bpf_dump_all(&bpf_ops);
370 bpf_attach_to_sock(rx_sock, &bpf_ops);
372 setup_rx_ring_layout(rx_sock, &rx_ring, size_in, ctx->jumbo);
373 create_rx_ring(rx_sock, &rx_ring, ctx->verbose);
374 mmap_rx_ring(rx_sock, &rx_ring);
375 alloc_rx_ring_frames(&rx_ring);
376 bind_rx_ring(rx_sock, &rx_ring, ifindex_in);
377 prepare_polling(rx_sock, &rx_poll);
379 set_packet_loss_discard(tx_sock);
380 setup_tx_ring_layout(tx_sock, &tx_ring, size_out, ctx->jumbo);
381 create_tx_ring(tx_sock, &tx_ring, ctx->verbose);
382 mmap_tx_ring(tx_sock, &tx_ring);
383 alloc_tx_ring_frames(&tx_ring);
384 bind_tx_ring(tx_sock, &tx_ring, ifindex_out);
386 dissector_init_all(ctx->print_mode);
388 if (ctx->promiscuous)
389 ifflags = enter_promiscuous_mode(ctx->device_in);
391 if (ctx->kpull)
392 interval = ctx->kpull;
394 set_itimer_interval_value(&itimer, 0, interval);
395 setitimer(ITIMER_REAL, &itimer, NULL);
397 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
399 printf("Running! Hang up with ^C!\n\n");
400 fflush(stdout);
402 while (likely(sigint == 0)) {
403 while (user_may_pull_from_rx(rx_ring.frames[it_in].iov_base)) {
404 __label__ next;
406 hdr_in = rx_ring.frames[it_in].iov_base;
407 in = ((uint8_t *) hdr_in) + hdr_in->tp_h.tp_mac;
409 frame_count++;
411 if (ctx->packet_type != -1)
412 if (ctx->packet_type != hdr_in->s_ll.sll_pkttype)
413 goto next;
415 hdr_out = tx_ring.frames[it_out].iov_base;
416 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
418 for (; !user_may_pull_from_tx(tx_ring.frames[it_out].iov_base) &&
419 likely(!sigint);) {
420 if (ctx->randomize)
421 next_rnd_slot(&it_out, &tx_ring);
422 else {
423 it_out++;
424 if (it_out >= tx_ring.layout.tp_frame_nr)
425 it_out = 0;
428 hdr_out = tx_ring.frames[it_out].iov_base;
429 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
432 tpacket_hdr_clone(&hdr_out->tp_h, &hdr_in->tp_h);
433 fmemcpy(out, in, hdr_in->tp_h.tp_len);
435 kernel_may_pull_from_tx(&hdr_out->tp_h);
436 if (ctx->randomize)
437 next_rnd_slot(&it_out, &tx_ring);
438 else {
439 it_out++;
440 if (it_out >= tx_ring.layout.tp_frame_nr)
441 it_out = 0;
444 show_frame_hdr(hdr_in, ctx->print_mode);
446 dissector_entry_point(in, hdr_in->tp_h.tp_snaplen,
447 ctx->link_type, ctx->print_mode);
449 if (frame_count_max != 0) {
450 if (frame_count >= frame_count_max) {
451 sigint = 1;
452 break;
456 next:
458 kernel_may_pull_from_rx(&hdr_in->tp_h);
460 it_in++;
461 if (it_in >= rx_ring.layout.tp_frame_nr)
462 it_in = 0;
464 if (unlikely(sigint == 1))
465 goto out;
468 poll(&rx_poll, 1, -1);
471 out:
473 timer_purge();
475 sock_print_net_stats(rx_sock, 0);
477 bpf_release(&bpf_ops);
479 dissector_cleanup_all();
481 destroy_tx_ring(tx_sock, &tx_ring);
482 destroy_rx_ring(rx_sock, &rx_ring);
484 if (ctx->promiscuous)
485 leave_promiscuous_mode(ctx->device_in, ifflags);
487 close(tx_sock);
488 close(rx_sock);
491 static void translate_pcap_to_txf(int fdo, uint8_t *out, size_t len)
493 size_t bytes_done = 0;
494 char bout[80];
496 slprintf(bout, sizeof(bout), "{\n ");
497 write_or_die(fdo, bout, strlen(bout));
499 while (bytes_done < len) {
500 slprintf(bout, sizeof(bout), "0x%02x, ", out[bytes_done]);
501 write_or_die(fdo, bout, strlen(bout));
503 bytes_done++;
505 if (bytes_done % 10 == 0) {
506 slprintf(bout, sizeof(bout), "\n");
507 write_or_die(fdo, bout, strlen(bout));
509 if (bytes_done < len) {
510 slprintf(bout, sizeof(bout), " ");
511 write_or_die(fdo, bout, strlen(bout));
515 if (bytes_done % 10 != 0) {
516 slprintf(bout, sizeof(bout), "\n");
517 write_or_die(fdo, bout, strlen(bout));
520 slprintf(bout, sizeof(bout), "}\n\n");
521 write_or_die(fdo, bout, strlen(bout));
524 static void read_pcap(struct ctx *ctx)
526 __label__ out;
527 uint8_t *out;
528 int ret, fd, fdo = 0;
529 unsigned long trunced = 0;
530 size_t out_len;
531 pcap_pkthdr_t phdr;
532 struct sock_fprog bpf_ops;
533 struct frame_map fm;
534 struct timeval start, end, diff;
535 struct sockaddr_ll sll;
537 bug_on(!__pcap_io);
539 if (!strncmp("-", ctx->device_in, strlen("-"))) {
540 fd = dup(fileno(stdin));
541 close(fileno(stdin));
542 if (ctx->pcap == PCAP_OPS_MM)
543 ctx->pcap = PCAP_OPS_SG;
544 } else {
545 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
548 ret = __pcap_io->pull_fhdr_pcap(fd, &ctx->magic, &ctx->link_type);
549 if (ret)
550 panic("Error reading pcap header!\n");
552 if (__pcap_io->prepare_access_pcap) {
553 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_RD, ctx->jumbo);
554 if (ret)
555 panic("Error prepare reading pcap!\n");
558 fmemset(&fm, 0, sizeof(fm));
559 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
561 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
562 if (ctx->dump_bpf)
563 bpf_dump_all(&bpf_ops);
565 dissector_init_all(ctx->print_mode);
567 out_len = round_up(1024 * 1024, PAGE_SIZE);
568 out = xmalloc_aligned(out_len, CO_CACHE_LINE_SIZE);
570 if (ctx->device_out) {
571 if (!strncmp("-", ctx->device_out, strlen("-"))) {
572 fdo = dup(fileno(stdout));
573 close(fileno(stdout));
574 } else {
575 fdo = open_or_die_m(ctx->device_out, O_RDWR | O_CREAT |
576 O_TRUNC | O_LARGEFILE, DEFFILEMODE);
580 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
582 printf("Running! Hang up with ^C!\n\n");
583 fflush(stdout);
585 bug_on(gettimeofday(&start, NULL));
587 while (likely(sigint == 0)) {
588 do {
589 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic,
590 out, out_len);
591 if (unlikely(ret < 0))
592 goto out;
594 if (unlikely(pcap_get_length(&phdr, ctx->magic) == 0)) {
595 trunced++;
596 continue;
599 if (unlikely(pcap_get_length(&phdr, ctx->magic) > out_len)) {
600 pcap_set_length(&phdr, ctx->magic, out_len);
601 trunced++;
603 } while (ctx->filter &&
604 !bpf_run_filter(&bpf_ops, out,
605 pcap_get_length(&phdr, ctx->magic)));
607 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &fm.tp_h, &sll);
609 ctx->tx_bytes += fm.tp_h.tp_len;
610 ctx->tx_packets++;
612 show_frame_hdr(&fm, ctx->print_mode);
614 dissector_entry_point(out, fm.tp_h.tp_snaplen,
615 ctx->link_type, ctx->print_mode);
617 if (ctx->device_out)
618 translate_pcap_to_txf(fdo, out, fm.tp_h.tp_snaplen);
620 if (frame_count_max != 0) {
621 if (ctx->tx_packets >= frame_count_max) {
622 sigint = 1;
623 break;
628 out:
630 bug_on(gettimeofday(&end, NULL));
631 timersub(&end, &start, &diff);
633 bpf_release(&bpf_ops);
635 dissector_cleanup_all();
637 if (__pcap_io->prepare_close_pcap)
638 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
640 xfree(out);
642 fflush(stdout);
643 printf("\n");
644 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
645 printf("\r%12lu packets truncated in file\n", trunced);
646 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
647 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
649 if (!strncmp("-", ctx->device_in, strlen("-")))
650 dup2(fd, fileno(stdin));
651 close(fd);
653 if (ctx->device_out) {
654 if (!strncmp("-", ctx->device_out, strlen("-")))
655 dup2(fdo, fileno(stdout));
656 close(fdo);
660 static void finish_multi_pcap_file(struct ctx *ctx, int fd)
662 __pcap_io->fsync_pcap(fd);
664 if (__pcap_io->prepare_close_pcap)
665 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
667 close(fd);
669 fmemset(&itimer, 0, sizeof(itimer));
670 setitimer(ITIMER_REAL, &itimer, NULL);
673 static int next_multi_pcap_file(struct ctx *ctx, int fd)
675 int ret;
676 char fname[512];
678 __pcap_io->fsync_pcap(fd);
680 if (__pcap_io->prepare_close_pcap)
681 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
683 close(fd);
685 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
686 ctx->prefix ? : "dump-", time(0));
688 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
689 O_LARGEFILE, DEFFILEMODE);
691 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
692 if (ret)
693 panic("Error writing pcap header!\n");
695 if (__pcap_io->prepare_access_pcap) {
696 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, ctx->jumbo);
697 if (ret)
698 panic("Error prepare writing pcap!\n");
701 return fd;
704 static int begin_multi_pcap_file(struct ctx *ctx)
706 int fd, ret;
707 char fname[256];
709 bug_on(!__pcap_io);
711 if (ctx->device_out[strlen(ctx->device_out) - 1] == '/')
712 ctx->device_out[strlen(ctx->device_out) - 1] = 0;
714 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
715 ctx->prefix ? : "dump-", time(0));
717 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
718 O_LARGEFILE, DEFFILEMODE);
720 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
721 if (ret)
722 panic("Error writing pcap header!\n");
724 if (__pcap_io->prepare_access_pcap) {
725 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, ctx->jumbo);
726 if (ret)
727 panic("Error prepare writing pcap!\n");
730 if (ctx->dump_mode == DUMP_INTERVAL_TIME) {
731 interval = ctx->dump_interval;
733 set_itimer_interval_value(&itimer, interval, 0);
734 setitimer(ITIMER_REAL, &itimer, NULL);
735 } else {
736 interval = 0;
739 return fd;
742 static void finish_single_pcap_file(struct ctx *ctx, int fd)
744 __pcap_io->fsync_pcap(fd);
746 if (__pcap_io->prepare_close_pcap)
747 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
749 if (strncmp("-", ctx->device_out, strlen("-")))
750 close(fd);
751 else
752 dup2(fd, fileno(stdout));
755 static int begin_single_pcap_file(struct ctx *ctx)
757 int fd, ret;
759 bug_on(!__pcap_io);
761 if (!strncmp("-", ctx->device_out, strlen("-"))) {
762 fd = dup(fileno(stdout));
763 close(fileno(stdout));
764 if (ctx->pcap == PCAP_OPS_MM)
765 ctx->pcap = PCAP_OPS_SG;
766 } else {
767 fd = open_or_die_m(ctx->device_out,
768 O_RDWR | O_CREAT | O_TRUNC |
769 O_LARGEFILE, DEFFILEMODE);
772 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
773 if (ret)
774 panic("Error writing pcap header!\n");
776 if (__pcap_io->prepare_access_pcap) {
777 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, ctx->jumbo);
778 if (ret)
779 panic("Error prepare writing pcap!\n");
782 return fd;
785 static void print_pcap_file_stats(int sock, struct ctx *ctx, unsigned long skipped)
787 int ret;
788 unsigned long good, bad;
789 struct tpacket_stats kstats;
790 socklen_t slen = sizeof(kstats);
792 fmemset(&kstats, 0, sizeof(kstats));
794 ret = getsockopt(sock, SOL_PACKET, PACKET_STATISTICS, &kstats, &slen);
795 if (unlikely(ret))
796 panic("Cannot get packet statistics!\n");
798 if (ctx->print_mode == PRINT_NONE) {
799 good = kstats.tp_packets - kstats.tp_drops - skipped;
800 bad = kstats.tp_drops + skipped;
802 printf(".(+%lu/-%lu)", good, bad);
803 fflush(stdout);
807 static void recv_only_or_dump(struct ctx *ctx)
809 uint8_t *packet;
810 short ifflags = 0;
811 int sock, irq, ifindex, fd = 0, ret;
812 unsigned int size, it = 0;
813 unsigned long frame_count = 0, skipped = 0;
814 struct ring rx_ring;
815 struct pollfd rx_poll;
816 struct frame_map *hdr;
817 struct sock_fprog bpf_ops;
818 struct timeval start, end, diff;
819 pcap_pkthdr_t phdr;
821 sock = pf_socket();
823 if (ctx->rfraw) {
824 ctx->device_trans = xstrdup(ctx->device_in);
825 xfree(ctx->device_in);
827 enter_rfmon_mac80211(ctx->device_trans, &ctx->device_in);
828 ctx->link_type = LINKTYPE_IEEE802_11;
831 fmemset(&rx_ring, 0, sizeof(rx_ring));
832 fmemset(&rx_poll, 0, sizeof(rx_poll));
833 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
835 ifindex = device_ifindex(ctx->device_in);
837 size = ring_size(ctx->device_in, ctx->reserve_size);
839 enable_kernel_bpf_jit_compiler();
841 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
842 if (ctx->dump_bpf)
843 bpf_dump_all(&bpf_ops);
844 bpf_attach_to_sock(sock, &bpf_ops);
846 set_sockopt_hwtimestamp(sock, ctx->device_in);
848 setup_rx_ring_layout(sock, &rx_ring, size, ctx->jumbo);
849 create_rx_ring(sock, &rx_ring, ctx->verbose);
850 mmap_rx_ring(sock, &rx_ring);
851 alloc_rx_ring_frames(&rx_ring);
852 bind_rx_ring(sock, &rx_ring, ifindex);
854 prepare_polling(sock, &rx_poll);
855 dissector_init_all(ctx->print_mode);
857 if (ctx->cpu >= 0 && ifindex > 0) {
858 irq = device_irq_number(ctx->device_in);
859 device_bind_irq_to_cpu(irq, ctx->cpu);
861 if (ctx->verbose)
862 printf("IRQ: %s:%d > CPU%d\n",
863 ctx->device_in, irq, ctx->cpu);
866 if (ctx->promiscuous)
867 ifflags = enter_promiscuous_mode(ctx->device_in);
869 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
871 if (dump_to_pcap(ctx)) {
872 __label__ try_file;
873 struct stat stats;
875 fmemset(&stats, 0, sizeof(stats));
876 ret = stat(ctx->device_out, &stats);
877 if (ret < 0) {
878 ctx->dump_dir = 0;
879 goto try_file;
882 ctx->dump_dir = S_ISDIR(stats.st_mode);
883 if (ctx->dump_dir) {
884 fd = begin_multi_pcap_file(ctx);
885 } else {
886 try_file:
887 fd = begin_single_pcap_file(ctx);
891 printf("Running! Hang up with ^C!\n\n");
892 fflush(stdout);
894 bug_on(gettimeofday(&start, NULL));
896 while (likely(sigint == 0)) {
897 while (user_may_pull_from_rx(rx_ring.frames[it].iov_base)) {
898 __label__ next;
900 hdr = rx_ring.frames[it].iov_base;
901 packet = ((uint8_t *) hdr) + hdr->tp_h.tp_mac;
902 frame_count++;
904 if (ctx->packet_type != -1)
905 if (ctx->packet_type != hdr->s_ll.sll_pkttype)
906 goto next;
908 if (unlikely(ring_frame_size(&rx_ring) < hdr->tp_h.tp_snaplen)) {
909 skipped++;
910 goto next;
913 if (dump_to_pcap(ctx)) {
914 tpacket_hdr_to_pcap_pkthdr(&hdr->tp_h, &hdr->s_ll, &phdr, ctx->magic);
916 ret = __pcap_io->write_pcap(fd, &phdr, ctx->magic, packet,
917 pcap_get_length(&phdr, ctx->magic));
918 if (unlikely(ret != pcap_get_total_length(&phdr, ctx->magic)))
919 panic("Write error to pcap!\n");
922 show_frame_hdr(hdr, ctx->print_mode);
924 dissector_entry_point(packet, hdr->tp_h.tp_snaplen,
925 ctx->link_type, ctx->print_mode);
927 if (frame_count_max != 0) {
928 if (frame_count >= frame_count_max) {
929 sigint = 1;
930 break;
934 next:
936 kernel_may_pull_from_rx(&hdr->tp_h);
938 it++;
939 if (it >= rx_ring.layout.tp_frame_nr)
940 it = 0;
942 if (unlikely(sigint == 1))
943 break;
945 if (dump_to_pcap(ctx)) {
946 if (ctx->dump_mode == DUMP_INTERVAL_SIZE) {
947 interval += hdr->tp_h.tp_snaplen;
949 if (interval > ctx->dump_interval) {
950 next_dump = true;
951 interval = 0;
955 if (next_dump) {
956 fd = next_multi_pcap_file(ctx, fd);
957 next_dump = false;
959 if (ctx->verbose)
960 print_pcap_file_stats(sock, ctx, skipped);
965 poll(&rx_poll, 1, -1);
968 bug_on(gettimeofday(&end, NULL));
969 timersub(&end, &start, &diff);
971 if (!(ctx->dump_dir && ctx->print_mode == PRINT_NONE)) {
972 sock_print_net_stats(sock, skipped);
974 printf("\r%12lu sec, %lu usec in total\n",
975 diff.tv_sec, diff.tv_usec);
976 } else {
977 printf("\n\n");
978 fflush(stdout);
981 bpf_release(&bpf_ops);
982 dissector_cleanup_all();
983 destroy_rx_ring(sock, &rx_ring);
985 if (ctx->promiscuous)
986 leave_promiscuous_mode(ctx->device_in, ifflags);
988 if (ctx->rfraw)
989 leave_rfmon_mac80211(ctx->device_trans, ctx->device_in);
991 if (dump_to_pcap(ctx)) {
992 if (ctx->dump_dir)
993 finish_multi_pcap_file(ctx, fd);
994 else
995 finish_single_pcap_file(ctx, fd);
998 close(sock);
1001 static void help(void)
1003 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING);
1004 puts("http://www.netsniff-ng.org\n\n"
1005 "Usage: netsniff-ng [options] [filter-expression]\n"
1006 "Options:\n"
1007 " -i|-d|--dev|--in <dev|pcap|-> Input source as netdev, pcap or pcap stdin\n"
1008 " -o|--out <dev|pcap|dir|cfg|-> Output sink as netdev, pcap, directory, trafgen, or stdout\n"
1009 " -f|--filter <bpf-file|expr> Use BPF filter file from bpfc or tcpdump-like expression\n"
1010 " -t|--type <type> Filter for: host|broadcast|multicast|others|outgoing\n"
1011 " -F|--interval <size|time> Dump interval if -o is a dir: <num>KiB/MiB/GiB/s/sec/min/hrs\n"
1012 " -J|--jumbo-support Support for 64KB Super Jumbo Frames (def: 2048B)\n"
1013 " -R|--rfraw Capture or inject raw 802.11 frames\n"
1014 " -n|--num <0|uint> Number of packets until exit (def: 0)\n"
1015 " -P|--prefix <name> Prefix for pcaps stored in directory\n"
1016 " -T|--magic <pcap-magic> Pcap magic number/pcap format to store, see -D\n"
1017 " -D|--dump-pcap-types Dump pcap types and magic numbers and quit\n"
1018 " -B|--dump-bpf Dump generated BPF assembly\n"
1019 " -r|--rand Randomize packet forwarding order (dev->dev)\n"
1020 " -M|--no-promisc No promiscuous mode for netdev\n"
1021 " -A|--no-sock-mem Don't tune core socket memory\n"
1022 " -m|--mmap Mmap(2) pcap file i.e., for replaying pcaps\n"
1023 " -G|--sg Scatter/gather pcap file I/O\n"
1024 " -c|--clrw Use slower read(2)/write(2) I/O\n"
1025 " -S|--ring-size <size> Specify ring size to: <num>KiB/MiB/GiB\n"
1026 " -k|--kernel-pull <uint> Kernel pull from user interval in us (def: 10us)\n"
1027 " -b|--bind-cpu <cpu> Bind to specific CPU\n"
1028 " -u|--user <userid> Drop privileges and change to userid\n"
1029 " -g|--group <groupid> Drop privileges and change to groupid\n"
1030 " -H|--prio-high Make this high priority process\n"
1031 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
1032 " -s|--silent Do not print captured packets\n"
1033 " -q|--less Print less-verbose packet information\n"
1034 " -X|--hex Print packet data in hex format\n"
1035 " -l|--ascii Print human-readable packet data\n"
1036 " -U|--update Update GeoIP databases\n"
1037 " -V|--verbose Be more verbose\n"
1038 " -v|--version Show version\n"
1039 " -h|--help Guess what?!\n\n"
1040 "Examples:\n"
1041 " netsniff-ng --in eth0 --out dump.pcap -s -T 0xa1b2c3d4 --b 0 tcp or udp\n"
1042 " netsniff-ng --in wlan0 --rfraw --out dump.pcap --silent --bind-cpu 0\n"
1043 " netsniff-ng --in dump.pcap --mmap --out eth0 -k1000 --silent --bind-cpu 0\n"
1044 " netsniff-ng --in dump.pcap --out dump.cfg --silent --bind-cpu 0\n"
1045 " netsniff-ng --in eth0 --out eth1 --silent --bind-cpu 0 --type host\n"
1046 " netsniff-ng --in eth1 --out /opt/probe/ -s -m -J --interval 100MiB -b 0\n"
1047 " netsniff-ng --in vlan0 --out dump.pcap -c -u `id -u bob` -g `id -g bob`\n"
1048 " netsniff-ng --in any --filter http.bpf --jumbo-support --ascii -V\n\n"
1049 "Note:\n"
1050 " For introducing bit errors, delays with random variation and more\n"
1051 " while replaying pcaps, make use of tc(8) with its disciplines (e.g. netem).\n\n"
1052 "Please report bugs to <bugs@netsniff-ng.org>\n"
1053 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1054 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1055 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1056 "Swiss federal institute of technology (ETH Zurich)\n"
1057 "License: GNU GPL version 2.0\n"
1058 "This is free software: you are free to change and redistribute it.\n"
1059 "There is NO WARRANTY, to the extent permitted by law.\n");
1060 die();
1063 static void version(void)
1065 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING);
1066 puts("http://www.netsniff-ng.org\n\n"
1067 "Please report bugs to <bugs@netsniff-ng.org>\n"
1068 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1069 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1070 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1071 "Swiss federal institute of technology (ETH Zurich)\n"
1072 "License: GNU GPL version 2.0\n"
1073 "This is free software: you are free to change and redistribute it.\n"
1074 "There is NO WARRANTY, to the extent permitted by law.\n");
1075 die();
1078 int main(int argc, char **argv)
1080 char *ptr;
1081 int c, i, j, cpu_tmp, opt_index, ops_touched = 0, vals[4] = {0};
1082 bool prio_high = false, setsockmem = true;
1083 void (*main_loop)(struct ctx *ctx) = NULL;
1084 struct ctx ctx = {
1085 .link_type = LINKTYPE_EN10MB,
1086 .print_mode = PRINT_NORM,
1087 .cpu = -1,
1088 .packet_type = -1,
1089 .promiscuous = true,
1090 .randomize = false,
1091 .pcap = PCAP_OPS_SG,
1092 .dump_interval = 60,
1093 .dump_mode = DUMP_INTERVAL_TIME,
1094 .uid = getuid(),
1095 .gid = getgid(),
1096 .magic = ORIGINAL_TCPDUMP_MAGIC,
1099 srand(time(NULL));
1101 while ((c = getopt_long(argc, argv, short_options, long_options,
1102 &opt_index)) != EOF) {
1103 switch (c) {
1104 case 'd':
1105 case 'i':
1106 ctx.device_in = xstrdup(optarg);
1107 break;
1108 case 'o':
1109 ctx.device_out = xstrdup(optarg);
1110 break;
1111 case 'P':
1112 ctx.prefix = xstrdup(optarg);
1113 break;
1114 case 'R':
1115 ctx.link_type = LINKTYPE_IEEE802_11;
1116 ctx.rfraw = 1;
1117 break;
1118 case 'r':
1119 ctx.randomize = true;
1120 break;
1121 case 'J':
1122 ctx.jumbo = true;
1123 break;
1124 case 'T':
1125 ctx.magic = (uint32_t) strtoul(optarg, NULL, 0);
1126 pcap_check_magic(ctx.magic);
1127 break;
1128 case 'f':
1129 ctx.filter = xstrdup(optarg);
1130 break;
1131 case 'M':
1132 ctx.promiscuous = false;
1133 break;
1134 case 'A':
1135 setsockmem = false;
1136 break;
1137 case 'u':
1138 ctx.uid = strtoul(optarg, NULL, 0);
1139 ctx.enforce = true;
1140 break;
1141 case 'g':
1142 ctx.gid = strtoul(optarg, NULL, 0);
1143 ctx.enforce = true;
1144 break;
1145 case 't':
1146 if (!strncmp(optarg, "host", strlen("host")))
1147 ctx.packet_type = PACKET_HOST;
1148 else if (!strncmp(optarg, "broadcast", strlen("broadcast")))
1149 ctx.packet_type = PACKET_BROADCAST;
1150 else if (!strncmp(optarg, "multicast", strlen("multicast")))
1151 ctx.packet_type = PACKET_MULTICAST;
1152 else if (!strncmp(optarg, "others", strlen("others")))
1153 ctx.packet_type = PACKET_OTHERHOST;
1154 else if (!strncmp(optarg, "outgoing", strlen("outgoing")))
1155 ctx.packet_type = PACKET_OUTGOING;
1156 else
1157 ctx.packet_type = -1;
1158 break;
1159 case 'S':
1160 ptr = optarg;
1161 ctx.reserve_size = 0;
1163 for (j = i = strlen(optarg); i > 0; --i) {
1164 if (!isdigit(optarg[j - i]))
1165 break;
1166 ptr++;
1169 if (!strncmp(ptr, "KiB", strlen("KiB")))
1170 ctx.reserve_size = 1 << 10;
1171 else if (!strncmp(ptr, "MiB", strlen("MiB")))
1172 ctx.reserve_size = 1 << 20;
1173 else if (!strncmp(ptr, "GiB", strlen("GiB")))
1174 ctx.reserve_size = 1 << 30;
1175 else
1176 panic("Syntax error in ring size param!\n");
1177 *ptr = 0;
1179 ctx.reserve_size *= strtol(optarg, NULL, 0);
1180 break;
1181 case 'b':
1182 cpu_tmp = strtol(optarg, NULL, 0);
1184 cpu_affinity(cpu_tmp);
1185 if (ctx.cpu != -2)
1186 ctx.cpu = cpu_tmp;
1187 break;
1188 case 'H':
1189 prio_high = true;
1190 break;
1191 case 'c':
1192 ctx.pcap = PCAP_OPS_RW;
1193 ops_touched = 1;
1194 break;
1195 case 'm':
1196 ctx.pcap = PCAP_OPS_MM;
1197 ops_touched = 1;
1198 break;
1199 case 'G':
1200 ctx.pcap = PCAP_OPS_SG;
1201 ops_touched = 1;
1202 break;
1203 case 'Q':
1204 ctx.cpu = -2;
1205 break;
1206 case 's':
1207 ctx.print_mode = PRINT_NONE;
1208 break;
1209 case 'q':
1210 ctx.print_mode = PRINT_LESS;
1211 break;
1212 case 'X':
1213 ctx.print_mode =
1214 (ctx.print_mode == PRINT_ASCII) ?
1215 PRINT_HEX_ASCII : PRINT_HEX;
1216 break;
1217 case 'l':
1218 ctx.print_mode =
1219 (ctx.print_mode == PRINT_HEX) ?
1220 PRINT_HEX_ASCII : PRINT_ASCII;
1221 break;
1222 case 'k':
1223 ctx.kpull = strtol(optarg, NULL, 0);
1224 break;
1225 case 'n':
1226 frame_count_max = strtol(optarg, NULL, 0);
1227 break;
1228 case 'F':
1229 ptr = optarg;
1230 ctx.dump_interval = 0;
1232 for (j = i = strlen(optarg); i > 0; --i) {
1233 if (!isdigit(optarg[j - i]))
1234 break;
1235 ptr++;
1238 if (!strncmp(ptr, "KiB", strlen("KiB"))) {
1239 ctx.dump_interval = 1 << 10;
1240 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1241 } else if (!strncmp(ptr, "MiB", strlen("MiB"))) {
1242 ctx.dump_interval = 1 << 20;
1243 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1244 } else if (!strncmp(ptr, "GiB", strlen("GiB"))) {
1245 ctx.dump_interval = 1 << 30;
1246 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1247 } else if (!strncmp(ptr, "sec", strlen("sec"))) {
1248 ctx.dump_interval = 1;
1249 ctx.dump_mode = DUMP_INTERVAL_TIME;
1250 } else if (!strncmp(ptr, "min", strlen("min"))) {
1251 ctx.dump_interval = 60;
1252 ctx.dump_mode = DUMP_INTERVAL_TIME;
1253 } else if (!strncmp(ptr, "hrs", strlen("hrs"))) {
1254 ctx.dump_interval = 60 * 60;
1255 ctx.dump_mode = DUMP_INTERVAL_TIME;
1256 } else if (!strncmp(ptr, "s", strlen("s"))) {
1257 ctx.dump_interval = 1;
1258 ctx.dump_mode = DUMP_INTERVAL_TIME;
1259 } else {
1260 panic("Syntax error in time/size param!\n");
1263 *ptr = 0;
1264 ctx.dump_interval *= strtol(optarg, NULL, 0);
1265 break;
1266 case 'V':
1267 ctx.verbose = 1;
1268 break;
1269 case 'B':
1270 ctx.dump_bpf = true;
1271 break;
1272 case 'D':
1273 pcap_dump_type_features();
1274 die();
1275 break;
1276 case 'U':
1277 update_geoip();
1278 die();
1279 break;
1280 case 'v':
1281 version();
1282 break;
1283 case 'h':
1284 help();
1285 break;
1286 case '?':
1287 switch (optopt) {
1288 case 'd':
1289 case 'i':
1290 case 'o':
1291 case 'f':
1292 case 't':
1293 case 'P':
1294 case 'F':
1295 case 'n':
1296 case 'S':
1297 case 'b':
1298 case 'k':
1299 case 'T':
1300 case 'u':
1301 case 'g':
1302 case 'e':
1303 panic("Option -%c requires an argument!\n",
1304 optopt);
1305 default:
1306 if (isprint(optopt))
1307 printf("Unknown option character `0x%X\'!\n", optopt);
1308 die();
1310 default:
1311 break;
1315 if (!ctx.filter && optind != argc) {
1316 int ret;
1317 off_t offset = 0;
1319 for (i = optind; i < argc; ++i) {
1320 size_t alen = strlen(argv[i]) + 2;
1321 size_t flen = ctx.filter ? strlen(ctx.filter) : 0;
1323 ctx.filter = xrealloc(ctx.filter, 1, flen + alen);
1324 ret = slprintf(ctx.filter + offset, strlen(argv[i]) + 2, "%s ", argv[i]);
1325 if (ret < 0)
1326 panic("Cannot concatenate filter string!\n");
1327 else
1328 offset += ret;
1332 if (!ctx.device_in)
1333 ctx.device_in = xstrdup("any");
1335 register_signal(SIGINT, signal_handler);
1336 register_signal(SIGHUP, signal_handler);
1338 tprintf_init();
1340 if (prio_high) {
1341 set_proc_prio(get_default_proc_prio());
1342 set_sched_status(get_default_sched_policy(), get_default_sched_prio());
1345 if (ctx.device_in && (device_mtu(ctx.device_in) ||
1346 !strncmp("any", ctx.device_in, strlen(ctx.device_in)))) {
1347 if (!ctx.device_out) {
1348 ctx.dump = 0;
1349 main_loop = recv_only_or_dump;
1350 } else if (device_mtu(ctx.device_out)) {
1351 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1352 main_loop = receive_to_xmit;
1353 } else {
1354 ctx.dump = 1;
1355 register_signal_f(SIGALRM, timer_next_dump, SA_SIGINFO);
1356 main_loop = recv_only_or_dump;
1357 if (!ops_touched)
1358 ctx.pcap = PCAP_OPS_SG;
1360 } else {
1361 if (ctx.device_out && device_mtu(ctx.device_out)) {
1362 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1363 main_loop = pcap_to_xmit;
1364 if (!ops_touched)
1365 ctx.pcap = PCAP_OPS_MM;
1366 } else {
1367 main_loop = read_pcap;
1368 if (!ops_touched)
1369 ctx.pcap = PCAP_OPS_SG;
1373 bug_on(!main_loop);
1375 init_geoip(0);
1376 if (setsockmem)
1377 set_system_socket_memory(vals, array_size(vals));
1378 xlockme();
1380 main_loop(&ctx);
1382 xunlockme();
1383 if (setsockmem)
1384 reset_system_socket_memory(vals, array_size(vals));
1385 destroy_geoip();
1387 tprintf_cleanup();
1389 free(ctx.device_in);
1390 free(ctx.device_out);
1391 free(ctx.device_trans);
1392 free(ctx.prefix);
1394 return 0;