docs: mention txhashes in trafgen
[netsniff-ng.git] / netsniff-ng.c
blobb711097dca24589b27f540e41f004ba448571768
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2009-2013 Daniel Borkmann.
5 * Copyright 2010 Emmanuel Roullit.
6 * Subject to the GPL, version 2.
8 * The first sniffer that invoked both, the zero-copy RX_RING as well as
9 * the zero-copy TX_RING for high-performance network I/O and scatter/gather
10 * or mmaped PCAP I/O.
12 * "I knew that danger lay ahead, of course; but I did not expect to
13 * meet it in our own Shire. Can't a hobbit walk from the Water to the
14 * River in peace?" "But it is not your own Shire," said Gildor. "Others
15 * dwelt here before hobbits were; and others will dwell here again when
16 * hobbits are no more. The wide world is all about you: you can fence
17 * yourselves in, but you cannot for ever fence it out."
19 * -- The Lord of the Rings, Gildor to Frodo,
20 * Chapter 'Three is Company'.
23 #define _GNU_SOURCE
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <signal.h>
27 #include <getopt.h>
28 #include <ctype.h>
29 #include <time.h>
30 #include <string.h>
31 #include <sys/socket.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <sys/time.h>
35 #include <sys/fsuid.h>
36 #include <unistd.h>
37 #include <stdbool.h>
38 #include <pthread.h>
39 #include <fcntl.h>
41 #include "ring_rx.h"
42 #include "ring_tx.h"
43 #include "mac80211.h"
44 #include "xutils.h"
45 #include "built_in.h"
46 #include "pcap.h"
47 #include "bpf.h"
48 #include "xio.h"
49 #include "die.h"
50 #include "tprintf.h"
51 #include "dissector.h"
52 #include "xmalloc.h"
54 enum dump_mode {
55 DUMP_INTERVAL_TIME,
56 DUMP_INTERVAL_SIZE,
59 struct ctx {
60 char *device_in, *device_out, *device_trans, *filter, *prefix;
61 int cpu, rfraw, dump, print_mode, dump_dir, packet_type, verbose;
62 unsigned long kpull, dump_interval, reserve_size, tx_bytes, tx_packets;
63 bool randomize, promiscuous, enforce, jumbo;
64 enum pcap_ops_groups pcap; enum dump_mode dump_mode;
65 uid_t uid; gid_t gid; uint32_t link_type, magic;
68 volatile sig_atomic_t sigint = 0;
70 static volatile bool next_dump = false;
72 static const char *short_options = "d:i:o:rf:MJt:S:k:n:b:HQmcsqXlvhF:RGAP:Vu:g:T:D";
73 static const struct option long_options[] = {
74 {"dev", required_argument, NULL, 'd'},
75 {"in", required_argument, NULL, 'i'},
76 {"out", required_argument, NULL, 'o'},
77 {"filter", required_argument, NULL, 'f'},
78 {"num", required_argument, NULL, 'n'},
79 {"type", required_argument, NULL, 't'},
80 {"interval", required_argument, NULL, 'F'},
81 {"ring-size", required_argument, NULL, 'S'},
82 {"kernel-pull", required_argument, NULL, 'k'},
83 {"bind-cpu", required_argument, NULL, 'b'},
84 {"prefix", required_argument, NULL, 'P'},
85 {"user", required_argument, NULL, 'u'},
86 {"group", required_argument, NULL, 'g'},
87 {"magic", required_argument, NULL, 'T'},
88 {"rand", no_argument, NULL, 'r'},
89 {"rfraw", no_argument, NULL, 'R'},
90 {"mmap", no_argument, NULL, 'm'},
91 {"sg", no_argument, NULL, 'G'},
92 {"clrw", no_argument, NULL, 'c'},
93 {"jumbo-support", no_argument, NULL, 'J'},
94 {"no-promisc", no_argument, NULL, 'M'},
95 {"prio-high", no_argument, NULL, 'H'},
96 {"notouch-irq", no_argument, NULL, 'Q'},
97 {"dump-pcap-types", no_argument, NULL, 'D'},
98 {"silent", no_argument, NULL, 's'},
99 {"less", no_argument, NULL, 'q'},
100 {"hex", no_argument, NULL, 'X'},
101 {"ascii", no_argument, NULL, 'l'},
102 {"no-sock-mem", no_argument, NULL, 'A'},
103 {"verbose", no_argument, NULL, 'V'},
104 {"version", no_argument, NULL, 'v'},
105 {"help", no_argument, NULL, 'h'},
106 {NULL, 0, NULL, 0}
109 static int tx_sock;
111 static struct itimerval itimer;
113 static unsigned long frame_count_max = 0, interval = TX_KERNEL_PULL_INT;
115 #define __pcap_io pcap_ops[ctx->pcap]
117 static void signal_handler(int number)
119 switch (number) {
120 case SIGINT:
121 sigint = 1;
122 case SIGHUP:
123 default:
124 break;
128 static void timer_elapsed(int unused)
130 itimer.it_interval.tv_sec = 0;
131 itimer.it_interval.tv_usec = interval;
133 itimer.it_value.tv_sec = 0;
134 itimer.it_value.tv_usec = interval;
136 pull_and_flush_tx_ring(tx_sock);
137 setitimer(ITIMER_REAL, &itimer, NULL);
140 static void timer_next_dump(int unused)
142 itimer.it_interval.tv_sec = interval;
143 itimer.it_interval.tv_usec = 0;
145 itimer.it_value.tv_sec = interval;
146 itimer.it_value.tv_usec = 0;
148 next_dump = true;
149 setitimer(ITIMER_REAL, &itimer, NULL);
152 static inline bool dump_to_pcap(struct ctx *ctx)
154 return ctx->dump;
157 static void pcap_to_xmit(struct ctx *ctx)
159 __label__ out;
160 uint8_t *out = NULL;
161 int irq, ifindex, fd = 0, ret;
162 unsigned int size, it = 0;
163 unsigned long trunced = 0;
164 struct ring tx_ring;
165 struct frame_map *hdr;
166 struct sock_fprog bpf_ops;
167 struct timeval start, end, diff;
168 pcap_pkthdr_t phdr;
170 if (!device_up_and_running(ctx->device_out) && !ctx->rfraw)
171 panic("Device not up and running!\n");
173 bug_on(!__pcap_io);
175 tx_sock = pf_socket();
177 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
179 ret = __pcap_io->pull_fhdr_pcap(fd, &ctx->magic, &ctx->link_type);
180 if (ret)
181 panic("Error reading pcap header!\n");
183 if (__pcap_io->prepare_access_pcap) {
184 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_RD, ctx->jumbo);
185 if (ret)
186 panic("Error prepare reading pcap!\n");
189 fmemset(&tx_ring, 0, sizeof(tx_ring));
190 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
192 if (ctx->rfraw) {
193 ctx->device_trans = xstrdup(ctx->device_out);
194 xfree(ctx->device_out);
196 enter_rfmon_mac80211(ctx->device_trans, &ctx->device_out);
197 if (ctx->link_type != LINKTYPE_IEEE802_11)
198 panic("Wrong linktype of pcap!\n");
201 ifindex = device_ifindex(ctx->device_out);
203 size = ring_size(ctx->device_out, ctx->reserve_size);
205 bpf_parse_rules(ctx->device_out, ctx->filter, &bpf_ops);
207 set_packet_loss_discard(tx_sock);
208 set_sockopt_hwtimestamp(tx_sock, ctx->device_out);
210 setup_tx_ring_layout(tx_sock, &tx_ring, size, ctx->jumbo);
211 create_tx_ring(tx_sock, &tx_ring, ctx->verbose);
212 mmap_tx_ring(tx_sock, &tx_ring);
213 alloc_tx_ring_frames(&tx_ring);
214 bind_tx_ring(tx_sock, &tx_ring, ifindex);
216 dissector_init_all(ctx->print_mode);
218 if (ctx->cpu >= 0 && ifindex > 0) {
219 irq = device_irq_number(ctx->device_out);
220 device_bind_irq_to_cpu(irq, ctx->cpu);
222 if (ctx->verbose)
223 printf("IRQ: %s:%d > CPU%d\n",
224 ctx->device_out, irq, ctx->cpu);
227 if (ctx->kpull)
228 interval = ctx->kpull;
230 if (ctx->verbose) {
231 printf("BPF:\n");
232 bpf_dump_all(&bpf_ops);
234 printf("MD: TX %luus %s ", interval, pcap_ops_group_to_str[ctx->pcap]);
235 if (ctx->rfraw)
236 printf("802.11 raw via %s ", ctx->device_out);
237 #ifdef _LARGEFILE64_SOURCE
238 printf("lf64 ");
239 #endif
240 ioprio_print();
241 printf("\n");
244 itimer.it_interval.tv_sec = 0;
245 itimer.it_interval.tv_usec = interval;
247 itimer.it_value.tv_sec = 0;
248 itimer.it_value.tv_usec = interval;
250 setitimer(ITIMER_REAL, &itimer, NULL);
252 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
254 printf("Running! Hang up with ^C!\n\n");
255 fflush(stdout);
257 bug_on(gettimeofday(&start, NULL));
259 while (likely(sigint == 0)) {
260 while (user_may_pull_from_tx(tx_ring.frames[it].iov_base)) {
261 hdr = tx_ring.frames[it].iov_base;
262 out = ((uint8_t *) hdr) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
264 do {
265 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic, out,
266 ring_frame_size(&tx_ring));
267 if (unlikely(ret <= 0))
268 goto out;
270 if (ring_frame_size(&tx_ring) <
271 pcap_get_length(&phdr, ctx->magic)) {
272 pcap_set_length(&phdr, ctx->magic,
273 ring_frame_size(&tx_ring));
274 trunced++;
276 } while (ctx->filter &&
277 !bpf_run_filter(&bpf_ops, out,
278 pcap_get_length(&phdr, ctx->magic)));
280 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &hdr->tp_h, &hdr->s_ll);
282 ctx->tx_bytes += hdr->tp_h.tp_len;;
283 ctx->tx_packets++;
285 show_frame_hdr(hdr, ctx->print_mode);
287 dissector_entry_point(out, hdr->tp_h.tp_snaplen,
288 ctx->link_type, ctx->print_mode);
290 kernel_may_pull_from_tx(&hdr->tp_h);
292 it++;
293 if (it >= tx_ring.layout.tp_frame_nr)
294 it = 0;
296 if (unlikely(sigint == 1))
297 break;
299 if (frame_count_max != 0) {
300 if (ctx->tx_packets >= frame_count_max) {
301 sigint = 1;
302 break;
308 out:
310 bug_on(gettimeofday(&end, NULL));
311 diff = tv_subtract(end, start);
313 bpf_release(&bpf_ops);
315 dissector_cleanup_all();
316 destroy_tx_ring(tx_sock, &tx_ring);
318 if (ctx->rfraw)
319 leave_rfmon_mac80211(ctx->device_trans, ctx->device_out);
321 if (__pcap_io->prepare_close_pcap)
322 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
324 close(fd);
325 close(tx_sock);
327 fflush(stdout);
328 printf("\n");
329 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
330 printf("\r%12lu packets truncated in file\n", trunced);
331 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
332 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
335 static void receive_to_xmit(struct ctx *ctx)
337 short ifflags = 0;
338 uint8_t *in, *out;
339 int rx_sock, ifindex_in, ifindex_out;
340 unsigned int size_in, size_out, it_in = 0, it_out = 0;
341 unsigned long frame_count = 0;
342 struct frame_map *hdr_in, *hdr_out;
343 struct ring tx_ring, rx_ring;
344 struct pollfd rx_poll;
345 struct sock_fprog bpf_ops;
347 if (!strncmp(ctx->device_in, ctx->device_out, IFNAMSIZ))
348 panic("Ingress/egress devices must be different!\n");
349 if (!device_up_and_running(ctx->device_out))
350 panic("Egress device not up and running!\n");
351 if (!device_up_and_running(ctx->device_in))
352 panic("Ingress device not up and running!\n");
354 rx_sock = pf_socket();
355 tx_sock = pf_socket();
357 fmemset(&tx_ring, 0, sizeof(tx_ring));
358 fmemset(&rx_ring, 0, sizeof(rx_ring));
359 fmemset(&rx_poll, 0, sizeof(rx_poll));
360 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
362 ifindex_in = device_ifindex(ctx->device_in);
363 ifindex_out = device_ifindex(ctx->device_out);
365 size_in = ring_size(ctx->device_in, ctx->reserve_size);
366 size_out = ring_size(ctx->device_out, ctx->reserve_size);
368 enable_kernel_bpf_jit_compiler();
370 bpf_parse_rules(ctx->device_in, ctx->filter, &bpf_ops);
371 bpf_attach_to_sock(rx_sock, &bpf_ops);
373 setup_rx_ring_layout(rx_sock, &rx_ring, size_in, ctx->jumbo);
374 create_rx_ring(rx_sock, &rx_ring, ctx->verbose);
375 mmap_rx_ring(rx_sock, &rx_ring);
376 alloc_rx_ring_frames(&rx_ring);
377 bind_rx_ring(rx_sock, &rx_ring, ifindex_in);
378 prepare_polling(rx_sock, &rx_poll);
380 set_packet_loss_discard(tx_sock);
381 setup_tx_ring_layout(tx_sock, &tx_ring, size_out, ctx->jumbo);
382 create_tx_ring(tx_sock, &tx_ring, ctx->verbose);
383 mmap_tx_ring(tx_sock, &tx_ring);
384 alloc_tx_ring_frames(&tx_ring);
385 bind_tx_ring(tx_sock, &tx_ring, ifindex_out);
387 dissector_init_all(ctx->print_mode);
389 if (ctx->promiscuous)
390 ifflags = enter_promiscuous_mode(ctx->device_in);
392 if (ctx->kpull)
393 interval = ctx->kpull;
395 itimer.it_interval.tv_sec = 0;
396 itimer.it_interval.tv_usec = interval;
398 itimer.it_value.tv_sec = 0;
399 itimer.it_value.tv_usec = interval;
401 setitimer(ITIMER_REAL, &itimer, NULL);
403 if (ctx->verbose) {
404 printf("BPF:\n");
405 bpf_dump_all(&bpf_ops);
407 printf("MD: RXTX %luus\n\n", interval);
410 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
412 printf("Running! Hang up with ^C!\n\n");
413 fflush(stdout);
415 while (likely(sigint == 0)) {
416 while (user_may_pull_from_rx(rx_ring.frames[it_in].iov_base)) {
417 __label__ next;
419 hdr_in = rx_ring.frames[it_in].iov_base;
420 in = ((uint8_t *) hdr_in) + hdr_in->tp_h.tp_mac;
422 frame_count++;
424 if (ctx->packet_type != -1)
425 if (ctx->packet_type != hdr_in->s_ll.sll_pkttype)
426 goto next;
428 hdr_out = tx_ring.frames[it_out].iov_base;
429 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
431 for (; !user_may_pull_from_tx(tx_ring.frames[it_out].iov_base) &&
432 likely(!sigint);) {
433 if (ctx->randomize)
434 next_rnd_slot(&it_out, &tx_ring);
435 else {
436 it_out++;
437 if (it_out >= tx_ring.layout.tp_frame_nr)
438 it_out = 0;
441 hdr_out = tx_ring.frames[it_out].iov_base;
442 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
445 tpacket_hdr_clone(&hdr_out->tp_h, &hdr_in->tp_h);
446 fmemcpy(out, in, hdr_in->tp_h.tp_len);
448 kernel_may_pull_from_tx(&hdr_out->tp_h);
449 if (ctx->randomize)
450 next_rnd_slot(&it_out, &tx_ring);
451 else {
452 it_out++;
453 if (it_out >= tx_ring.layout.tp_frame_nr)
454 it_out = 0;
457 show_frame_hdr(hdr_in, ctx->print_mode);
459 dissector_entry_point(in, hdr_in->tp_h.tp_snaplen,
460 ctx->link_type, ctx->print_mode);
462 if (frame_count_max != 0) {
463 if (frame_count >= frame_count_max) {
464 sigint = 1;
465 break;
469 next:
471 kernel_may_pull_from_rx(&hdr_in->tp_h);
473 it_in++;
474 if (it_in >= rx_ring.layout.tp_frame_nr)
475 it_in = 0;
477 if (unlikely(sigint == 1))
478 goto out;
481 poll(&rx_poll, 1, -1);
482 poll_error_maybe_die(rx_sock, &rx_poll);
485 out:
487 sock_print_net_stats(rx_sock, 0);
489 bpf_release(&bpf_ops);
491 dissector_cleanup_all();
493 destroy_tx_ring(tx_sock, &tx_ring);
494 destroy_rx_ring(rx_sock, &rx_ring);
496 if (ctx->promiscuous)
497 leave_promiscuous_mode(ctx->device_in, ifflags);
499 close(tx_sock);
500 close(rx_sock);
503 static void translate_pcap_to_txf(int fdo, uint8_t *out, size_t len)
505 size_t bytes_done = 0;
506 char bout[80];
508 slprintf(bout, sizeof(bout), "{\n ");
509 write_or_die(fdo, bout, strlen(bout));
511 while (bytes_done < len) {
512 slprintf(bout, sizeof(bout), "0x%02x, ", out[bytes_done]);
513 write_or_die(fdo, bout, strlen(bout));
515 bytes_done++;
517 if (bytes_done % 10 == 0) {
518 slprintf(bout, sizeof(bout), "\n");
519 write_or_die(fdo, bout, strlen(bout));
521 if (bytes_done < len) {
522 slprintf(bout, sizeof(bout), " ");
523 write_or_die(fdo, bout, strlen(bout));
527 if (bytes_done % 10 != 0) {
528 slprintf(bout, sizeof(bout), "\n");
529 write_or_die(fdo, bout, strlen(bout));
532 slprintf(bout, sizeof(bout), "}\n\n");
533 write_or_die(fdo, bout, strlen(bout));
536 static void read_pcap(struct ctx *ctx)
538 __label__ out;
539 uint8_t *out;
540 int ret, fd, fdo = 0;
541 unsigned long trunced = 0;
542 size_t out_len;
543 pcap_pkthdr_t phdr;
544 struct sock_fprog bpf_ops;
545 struct frame_map fm;
546 struct timeval start, end, diff;
547 struct sockaddr_ll sll;
549 bug_on(!__pcap_io);
551 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
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("any", ctx->filter, &bpf_ops);
568 dissector_init_all(ctx->print_mode);
570 out_len = round_up(1024 * 1024, PAGE_SIZE);
571 out = xmalloc_aligned(out_len, CO_CACHE_LINE_SIZE);
573 if (ctx->verbose) {
574 printf("BPF:\n");
575 bpf_dump_all(&bpf_ops);
577 printf("MD: RD %s ", pcap_ops_group_to_str[ctx->pcap]);
578 #ifdef _LARGEFILE64_SOURCE
579 printf("lf64 ");
580 #endif
581 ioprio_print();
582 printf("\n");
585 if (ctx->device_out)
586 fdo = open_or_die_m(ctx->device_out, O_RDWR | O_CREAT |
587 O_TRUNC | O_LARGEFILE, DEFFILEMODE);
589 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
591 printf("Running! Hang up with ^C!\n\n");
592 fflush(stdout);
594 bug_on(gettimeofday(&start, NULL));
596 while (likely(sigint == 0)) {
597 do {
598 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic,
599 out, out_len);
600 if (unlikely(ret < 0))
601 goto out;
603 if (unlikely(pcap_get_length(&phdr, ctx->magic) == 0)) {
604 trunced++;
605 continue;
608 if (unlikely(pcap_get_length(&phdr, ctx->magic) > out_len)) {
609 pcap_set_length(&phdr, ctx->magic, out_len);
610 trunced++;
612 } while (ctx->filter &&
613 !bpf_run_filter(&bpf_ops, out,
614 pcap_get_length(&phdr, ctx->magic)));
616 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &fm.tp_h, &sll);
618 ctx->tx_bytes += fm.tp_h.tp_len;
619 ctx->tx_packets++;
621 show_frame_hdr(&fm, ctx->print_mode);
623 dissector_entry_point(out, fm.tp_h.tp_snaplen,
624 ctx->link_type, ctx->print_mode);
626 if (ctx->device_out)
627 translate_pcap_to_txf(fdo, out, fm.tp_h.tp_snaplen);
629 if (frame_count_max != 0) {
630 if (ctx->tx_packets >= frame_count_max) {
631 sigint = 1;
632 break;
637 out:
639 bug_on(gettimeofday(&end, NULL));
640 diff = tv_subtract(end, start);
642 bpf_release(&bpf_ops);
644 dissector_cleanup_all();
646 if (__pcap_io->prepare_close_pcap)
647 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
649 close(fd);
650 if (ctx->device_out)
651 close(fdo);
653 xfree(out);
655 fflush(stdout);
656 printf("\n");
657 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
658 printf("\r%12lu packets truncated in file\n", trunced);
659 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
660 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
663 static void finish_multi_pcap_file(struct ctx *ctx, int fd)
665 __pcap_io->fsync_pcap(fd);
667 if (__pcap_io->prepare_close_pcap)
668 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
670 close(fd);
672 fmemset(&itimer, 0, sizeof(itimer));
673 setitimer(ITIMER_REAL, &itimer, NULL);
676 static int next_multi_pcap_file(struct ctx *ctx, int fd)
678 int ret;
679 char fname[512];
681 __pcap_io->fsync_pcap(fd);
683 if (__pcap_io->prepare_close_pcap)
684 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
686 close(fd);
688 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
689 ctx->prefix ? : "dump-", time(0));
691 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
692 O_LARGEFILE, DEFFILEMODE);
694 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
695 if (ret)
696 panic("Error writing pcap header!\n");
698 if (__pcap_io->prepare_access_pcap) {
699 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, ctx->jumbo);
700 if (ret)
701 panic("Error prepare writing pcap!\n");
704 return fd;
707 static int begin_multi_pcap_file(struct ctx *ctx)
709 int fd, ret;
710 char fname[256];
712 bug_on(!__pcap_io);
714 if (ctx->device_out[strlen(ctx->device_out) - 1] == '/')
715 ctx->device_out[strlen(ctx->device_out) - 1] = 0;
717 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
718 ctx->prefix ? : "dump-", time(0));
720 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
721 O_LARGEFILE, DEFFILEMODE);
723 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
724 if (ret)
725 panic("Error writing pcap header!\n");
727 if (__pcap_io->prepare_access_pcap) {
728 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, ctx->jumbo);
729 if (ret)
730 panic("Error prepare writing pcap!\n");
733 if (ctx->dump_mode == DUMP_INTERVAL_TIME) {
734 interval = ctx->dump_interval;
736 itimer.it_interval.tv_sec = interval;
737 itimer.it_interval.tv_usec = 0;
739 itimer.it_value.tv_sec = interval;
740 itimer.it_value.tv_usec = 0;
742 setitimer(ITIMER_REAL, &itimer, NULL);
743 } else {
744 interval = 0;
747 return fd;
750 static void finish_single_pcap_file(struct ctx *ctx, int fd)
752 __pcap_io->fsync_pcap(fd);
754 if (__pcap_io->prepare_close_pcap)
755 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
757 close(fd);
760 static int begin_single_pcap_file(struct ctx *ctx)
762 int fd, ret;
764 bug_on(!__pcap_io);
766 fd = open_or_die_m(ctx->device_out, O_RDWR | O_CREAT | O_TRUNC |
767 O_LARGEFILE, DEFFILEMODE);
769 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
770 if (ret)
771 panic("Error writing pcap header!\n");
773 if (__pcap_io->prepare_access_pcap) {
774 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, ctx->jumbo);
775 if (ret)
776 panic("Error prepare writing pcap!\n");
779 return fd;
782 static void print_pcap_file_stats(int sock, struct ctx *ctx, unsigned long skipped)
784 unsigned long good, bad;
785 struct tpacket_stats kstats;
786 socklen_t slen = sizeof(kstats);
788 fmemset(&kstats, 0, sizeof(kstats));
789 getsockopt(sock, SOL_PACKET, PACKET_STATISTICS, &kstats, &slen);
791 if (ctx->print_mode == PRINT_NONE) {
792 good = kstats.tp_packets - kstats.tp_drops - skipped;
793 bad = kstats.tp_drops + skipped;
795 printf(".(+%lu/-%lu)", good, bad);
796 fflush(stdout);
800 static void recv_only_or_dump(struct ctx *ctx)
802 uint8_t *packet;
803 short ifflags = 0;
804 int sock, irq, ifindex, fd = 0, ret;
805 unsigned int size, it = 0;
806 unsigned long frame_count = 0, skipped = 0;
807 struct ring rx_ring;
808 struct pollfd rx_poll;
809 struct frame_map *hdr;
810 struct sock_fprog bpf_ops;
811 struct timeval start, end, diff;
812 pcap_pkthdr_t phdr;
814 if (!device_up_and_running(ctx->device_in) && !ctx->rfraw)
815 panic("Device not up and running!\n");
817 sock = pf_socket();
819 if (ctx->rfraw) {
820 ctx->device_trans = xstrdup(ctx->device_in);
821 xfree(ctx->device_in);
823 enter_rfmon_mac80211(ctx->device_trans, &ctx->device_in);
824 ctx->link_type = LINKTYPE_IEEE802_11;
827 fmemset(&rx_ring, 0, sizeof(rx_ring));
828 fmemset(&rx_poll, 0, sizeof(rx_poll));
829 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
831 ifindex = device_ifindex(ctx->device_in);
833 size = ring_size(ctx->device_in, ctx->reserve_size);
835 enable_kernel_bpf_jit_compiler();
837 bpf_parse_rules(ctx->device_in, ctx->filter, &bpf_ops);
838 bpf_attach_to_sock(sock, &bpf_ops);
840 set_sockopt_hwtimestamp(sock, ctx->device_in);
842 setup_rx_ring_layout(sock, &rx_ring, size, ctx->jumbo);
843 create_rx_ring(sock, &rx_ring, ctx->verbose);
844 mmap_rx_ring(sock, &rx_ring);
845 alloc_rx_ring_frames(&rx_ring);
846 bind_rx_ring(sock, &rx_ring, ifindex);
848 prepare_polling(sock, &rx_poll);
849 dissector_init_all(ctx->print_mode);
851 if (ctx->cpu >= 0 && ifindex > 0) {
852 irq = device_irq_number(ctx->device_in);
853 device_bind_irq_to_cpu(irq, ctx->cpu);
855 if (ctx->verbose)
856 printf("IRQ: %s:%d > CPU%d\n",
857 ctx->device_in, irq, ctx->cpu);
860 if (ctx->promiscuous)
861 ifflags = enter_promiscuous_mode(ctx->device_in);
863 if (ctx->verbose) {
864 printf("BPF:\n");
865 bpf_dump_all(&bpf_ops);
867 printf("MD: RX %s ", ctx->dump ? pcap_ops_group_to_str[ctx->pcap] : "");
868 if (ctx->rfraw)
869 printf("802.11 raw via %s ", ctx->device_in);
870 #ifdef _LARGEFILE64_SOURCE
871 printf("lf64 ");
872 #endif
873 ioprio_print();
874 printf("\n");
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);
974 poll_error_maybe_die(sock, &rx_poll);
977 bug_on(gettimeofday(&end, NULL));
978 diff = tv_subtract(end, start);
980 if (dump_to_pcap(ctx)) {
981 if (ctx->dump_dir)
982 finish_multi_pcap_file(ctx, fd);
983 else
984 finish_single_pcap_file(ctx, fd);
987 if (!(ctx->dump_dir && ctx->print_mode == PRINT_NONE)) {
988 sock_print_net_stats(sock, skipped);
990 printf("\r%12lu sec, %lu usec in total\n",
991 diff.tv_sec, diff.tv_usec);
992 } else {
993 printf("\n\n");
994 fflush(stdout);
997 bpf_release(&bpf_ops);
998 dissector_cleanup_all();
999 destroy_rx_ring(sock, &rx_ring);
1001 if (ctx->promiscuous)
1002 leave_promiscuous_mode(ctx->device_in, ifflags);
1004 if (ctx->rfraw)
1005 leave_rfmon_mac80211(ctx->device_trans, ctx->device_in);
1007 close(sock);
1010 static void help(void)
1012 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING);
1013 puts("http://www.netsniff-ng.org\n\n"
1014 "Usage: netsniff-ng [options] [filter-expression]\n"
1015 "Options:\n"
1016 " -i|-d|--dev|--in <dev|pcap> Input source as netdev or pcap\n"
1017 " -o|--out <dev|pcap|dir|cfg> Output sink as netdev, pcap, directory, trafgen file\n"
1018 " -f|--filter <bpf-file|expr> Use BPF filter file from bpfc or tcpdump-like expression\n"
1019 " -t|--type <type> Only handle packets of defined type:\n"
1020 " host|broadcast|multicast|others|outgoing\n"
1021 " -F|--interval <size/time> Dump interval in time or size if -o is a directory\n"
1022 " pcap swap spec: <num>KiB/MiB/GiB/s/sec/min/hrs\n"
1023 " -J|--jumbo-support Support for 64KB Super Jumbo Frames\n"
1024 " Default RX/TX slot: 2048Byte\n"
1025 " -R|--rfraw Capture or inject raw 802.11 frames\n"
1026 " -n|--num <uint> Number of packets until exit\n"
1027 " `-- 0 Loop until interrupted (default)\n"
1028 " `- n Send n packets and done\n"
1029 "Options for printing:\n"
1030 " -s|--silent Do not print captured packets\n"
1031 " -q|--less Print less-verbose packet information\n"
1032 " -X|--hex Print packet data in hex format\n"
1033 " -l|--ascii Print human-readable packet data\n"
1034 "Options, advanced:\n"
1035 " -P|--prefix <name> Prefix for pcaps stored in directory\n"
1036 " -T|--magic <pcap-magic> Pcap magic number/pcap type to process\n"
1037 " -r|--rand Randomize packet forwarding order\n"
1038 " -M|--no-promisc No promiscuous mode for netdev\n"
1039 " -A|--no-sock-mem Don't tune core socket memory\n"
1040 " -m|--mmap Mmap pcap file i.e., for replaying\n"
1041 " -G|--sg Scatter/gather pcap file I/O\n"
1042 " -c|--clrw Use slower read(2)/write(2) I/O\n"
1043 " -S|--ring-size <size> Manually set ring size to <size>:\n"
1044 " mmap space in KiB/MiB/GiB, e.g. \'10MiB\'\n"
1045 " -k|--kernel-pull <uint> Kernel pull from user interval in us\n"
1046 " Default is 10us where the TX_RING\n"
1047 " is populated with payload from uspace\n"
1048 " -b|--bind-cpu <cpu> Bind to specific CPU (or CPU-range)\n"
1049 " -B|--unbind-cpu <cpu> Forbid to use specific CPU (or CPU-range)\n"
1050 " -u|--user <userid> Drop privileges and change to userid\n"
1051 " -g|--group <groupid> Drop privileges and change to groupid\n"
1052 " -H|--prio-high Make this high priority process\n"
1053 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
1054 " -V|--verbose Be more verbose\n"
1055 " -D|--dump-pcap-types Dump pcap types and magic numbers\n"
1056 " -v|--version Show version\n"
1057 " -h|--help Guess what?!\n\n"
1058 "Examples:\n"
1059 " netsniff-ng --in eth0 --out dump.pcap --silent -T 0xa1b2c3d4 --bind-cpu 0\n"
1060 " netsniff-ng --in wlan0 --rfraw --out dump.pcap --silent --bind-cpu 0\n"
1061 " netsniff-ng --in dump.pcap --mmap --out eth0 -k1000 --silent --bind-cpu 0\n"
1062 " netsniff-ng --in dump.pcap --out dump.cfg --silent --bind-cpu 0\n"
1063 " netsniff-ng --in eth0 --out eth1 --silent --bind-cpu 0 --type host\n"
1064 " netsniff-ng --in eth1 --out /opt/probe/ -s -m -J --interval 100MiB -b 0\n"
1065 " netsniff-ng --in vlan0 --out dump.pcap -c -u `id -u bob` -g `id -g bob`\n"
1066 " netsniff-ng --in any --filter http.bpf --jumbo-support --ascii -V\n\n"
1067 "Please report bugs to <bugs@netsniff-ng.org>\n"
1068 "Copyright (C) 2009-2013 Daniel Borkmann <daniel@netsniff-ng.org>\n"
1069 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel@netsniff-ng.org>\n"
1070 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1071 "License: GNU GPL version 2.0\n"
1072 "This is free software: you are free to change and redistribute it.\n"
1073 "There is NO WARRANTY, to the extent permitted by law.\n");
1074 die();
1077 static void version(void)
1079 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING);
1080 puts("http://www.netsniff-ng.org\n\n"
1081 "Please report bugs to <bugs@netsniff-ng.org>\n"
1082 "Copyright (C) 2009-2013 Daniel Borkmann <daniel@netsniff-ng.org>\n"
1083 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel@netsniff-ng.org>\n"
1084 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1085 "License: GNU GPL version 2.0\n"
1086 "This is free software: you are free to change and redistribute it.\n"
1087 "There is NO WARRANTY, to the extent permitted by law.\n");
1088 die();
1091 static void header(void)
1093 printf("%s%s%s\n", colorize_start(bold), "netsniff-ng " VERSION_STRING, colorize_end());
1096 int main(int argc, char **argv)
1098 char *ptr;
1099 int c, i, j, cpu_tmp, opt_index, ops_touched = 0, vals[4] = {0};
1100 bool prio_high = false, setsockmem = true;
1101 void (*main_loop)(struct ctx *ctx) = NULL;
1102 struct ctx ctx = {
1103 .link_type = LINKTYPE_EN10MB,
1104 .print_mode = PRINT_NORM,
1105 .cpu = -1,
1106 .packet_type = -1,
1107 .promiscuous = true,
1108 .randomize = false,
1109 .pcap = PCAP_OPS_SG,
1110 .dump_interval = 60,
1111 .dump_mode = DUMP_INTERVAL_TIME,
1112 .uid = getuid(),
1113 .gid = getgid(),
1114 .magic = ORIGINAL_TCPDUMP_MAGIC,
1117 srand(time(NULL));
1119 while ((c = getopt_long(argc, argv, short_options, long_options,
1120 &opt_index)) != EOF) {
1121 switch (c) {
1122 case 'd':
1123 case 'i':
1124 ctx.device_in = xstrdup(optarg);
1125 break;
1126 case 'o':
1127 ctx.device_out = xstrdup(optarg);
1128 break;
1129 case 'P':
1130 ctx.prefix = xstrdup(optarg);
1131 break;
1132 case 'R':
1133 ctx.link_type = LINKTYPE_IEEE802_11;
1134 ctx.rfraw = 1;
1135 break;
1136 case 'r':
1137 ctx.randomize = true;
1138 break;
1139 case 'J':
1140 ctx.jumbo = true;
1141 break;
1142 case 'T':
1143 ctx.magic = (uint32_t) strtoul(optarg, NULL, 0);
1144 pcap_check_magic(ctx.magic);
1145 break;
1146 case 'f':
1147 ctx.filter = xstrdup(optarg);
1148 break;
1149 case 'M':
1150 ctx.promiscuous = false;
1151 break;
1152 case 'A':
1153 setsockmem = false;
1154 break;
1155 case 'u':
1156 ctx.uid = strtoul(optarg, NULL, 0);
1157 ctx.enforce = true;
1158 break;
1159 case 'g':
1160 ctx.gid = strtoul(optarg, NULL, 0);
1161 ctx.enforce = true;
1162 break;
1163 case 't':
1164 if (!strncmp(optarg, "host", strlen("host")))
1165 ctx.packet_type = PACKET_HOST;
1166 else if (!strncmp(optarg, "broadcast", strlen("broadcast")))
1167 ctx.packet_type = PACKET_BROADCAST;
1168 else if (!strncmp(optarg, "multicast", strlen("multicast")))
1169 ctx.packet_type = PACKET_MULTICAST;
1170 else if (!strncmp(optarg, "others", strlen("others")))
1171 ctx.packet_type = PACKET_OTHERHOST;
1172 else if (!strncmp(optarg, "outgoing", strlen("outgoing")))
1173 ctx.packet_type = PACKET_OUTGOING;
1174 else
1175 ctx.packet_type = -1;
1176 break;
1177 case 'S':
1178 ptr = optarg;
1179 ctx.reserve_size = 0;
1181 for (j = i = strlen(optarg); i > 0; --i) {
1182 if (!isdigit(optarg[j - i]))
1183 break;
1184 ptr++;
1187 if (!strncmp(ptr, "KiB", strlen("KiB")))
1188 ctx.reserve_size = 1 << 10;
1189 else if (!strncmp(ptr, "MiB", strlen("MiB")))
1190 ctx.reserve_size = 1 << 20;
1191 else if (!strncmp(ptr, "GiB", strlen("GiB")))
1192 ctx.reserve_size = 1 << 30;
1193 else
1194 panic("Syntax error in ring size param!\n");
1195 *ptr = 0;
1197 ctx.reserve_size *= strtol(optarg, NULL, 0);
1198 break;
1199 case 'b':
1200 cpu_tmp = strtol(optarg, NULL, 0);
1202 cpu_affinity(cpu_tmp);
1203 if (ctx.cpu != -2)
1204 ctx.cpu = cpu_tmp;
1205 break;
1206 case 'H':
1207 prio_high = true;
1208 break;
1209 case 'c':
1210 ctx.pcap = PCAP_OPS_RW;
1211 ops_touched = 1;
1212 break;
1213 case 'm':
1214 ctx.pcap = PCAP_OPS_MM;
1215 ops_touched = 1;
1216 break;
1217 case 'G':
1218 ctx.pcap = PCAP_OPS_SG;
1219 ops_touched = 1;
1220 break;
1221 case 'Q':
1222 ctx.cpu = -2;
1223 break;
1224 case 's':
1225 ctx.print_mode = PRINT_NONE;
1226 break;
1227 case 'q':
1228 ctx.print_mode = PRINT_LESS;
1229 break;
1230 case 'X':
1231 ctx.print_mode =
1232 (ctx.print_mode == PRINT_ASCII) ?
1233 PRINT_HEX_ASCII : PRINT_HEX;
1234 break;
1235 case 'l':
1236 ctx.print_mode =
1237 (ctx.print_mode == PRINT_HEX) ?
1238 PRINT_HEX_ASCII : PRINT_ASCII;
1239 break;
1240 case 'k':
1241 ctx.kpull = strtol(optarg, NULL, 0);
1242 break;
1243 case 'n':
1244 frame_count_max = strtol(optarg, NULL, 0);
1245 break;
1246 case 'F':
1247 ptr = optarg;
1248 ctx.dump_interval = 0;
1250 for (j = i = strlen(optarg); i > 0; --i) {
1251 if (!isdigit(optarg[j - i]))
1252 break;
1253 ptr++;
1256 if (!strncmp(ptr, "KiB", strlen("KiB"))) {
1257 ctx.dump_interval = 1 << 10;
1258 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1259 } else if (!strncmp(ptr, "MiB", strlen("MiB"))) {
1260 ctx.dump_interval = 1 << 20;
1261 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1262 } else if (!strncmp(ptr, "GiB", strlen("GiB"))) {
1263 ctx.dump_interval = 1 << 30;
1264 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1265 } else if (!strncmp(ptr, "sec", strlen("sec"))) {
1266 ctx.dump_interval = 1;
1267 ctx.dump_mode = DUMP_INTERVAL_TIME;
1268 } else if (!strncmp(ptr, "min", strlen("min"))) {
1269 ctx.dump_interval = 60;
1270 ctx.dump_mode = DUMP_INTERVAL_TIME;
1271 } else if (!strncmp(ptr, "hrs", strlen("hrs"))) {
1272 ctx.dump_interval = 60 * 60;
1273 ctx.dump_mode = DUMP_INTERVAL_TIME;
1274 } else if (!strncmp(ptr, "s", strlen("s"))) {
1275 ctx.dump_interval = 1;
1276 ctx.dump_mode = DUMP_INTERVAL_TIME;
1277 } else {
1278 panic("Syntax error in time/size param!\n");
1281 *ptr = 0;
1282 ctx.dump_interval *= strtol(optarg, NULL, 0);
1283 break;
1284 case 'V':
1285 ctx.verbose = 1;
1286 break;
1287 case 'D':
1288 pcap_dump_type_features();
1289 die();
1290 break;
1291 case 'v':
1292 version();
1293 break;
1294 case 'h':
1295 help();
1296 break;
1297 case '?':
1298 switch (optopt) {
1299 case 'd':
1300 case 'i':
1301 case 'o':
1302 case 'f':
1303 case 't':
1304 case 'P':
1305 case 'F':
1306 case 'n':
1307 case 'S':
1308 case 'b':
1309 case 'k':
1310 case 'T':
1311 case 'u':
1312 case 'g':
1313 case 'e':
1314 panic("Option -%c requires an argument!\n",
1315 optopt);
1316 default:
1317 if (isprint(optopt))
1318 whine("Unknown option character "
1319 "`0x%X\'!\n", optopt);
1320 die();
1322 default:
1323 break;
1327 if (!ctx.filter && optind != argc) {
1328 int ret;
1329 off_t offset = 0;
1331 for (i = optind; i < argc; ++i) {
1332 size_t alen = strlen(argv[i]) + 2;
1333 size_t flen = ctx.filter ? strlen(ctx.filter) : 0;
1335 ctx.filter = xrealloc(ctx.filter, 1, flen + alen);
1336 ret = slprintf(ctx.filter + offset, strlen(argv[i]) + 2, "%s ", argv[i]);
1337 if (ret < 0)
1338 panic("Cannot concatenate filter string!\n");
1339 else
1340 offset += ret;
1344 if (!ctx.device_in)
1345 ctx.device_in = xstrdup("any");
1347 register_signal(SIGINT, signal_handler);
1348 register_signal(SIGHUP, signal_handler);
1350 header();
1352 tprintf_init();
1354 if (prio_high) {
1355 set_proc_prio(get_default_proc_prio());
1356 set_sched_status(get_default_sched_policy(), get_default_sched_prio());
1359 if (ctx.device_in && (device_mtu(ctx.device_in) ||
1360 !strncmp("any", ctx.device_in, strlen(ctx.device_in)))) {
1361 if (!ctx.device_out) {
1362 ctx.dump = 0;
1363 main_loop = recv_only_or_dump;
1364 } else if (device_mtu(ctx.device_out)) {
1365 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1366 main_loop = receive_to_xmit;
1367 } else {
1368 ctx.dump = 1;
1369 register_signal_f(SIGALRM, timer_next_dump, SA_SIGINFO);
1370 main_loop = recv_only_or_dump;
1371 if (!ops_touched)
1372 ctx.pcap = PCAP_OPS_SG;
1374 } else {
1375 if (ctx.device_out && device_mtu(ctx.device_out)) {
1376 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1377 main_loop = pcap_to_xmit;
1378 if (!ops_touched)
1379 ctx.pcap = PCAP_OPS_MM;
1380 } else {
1381 main_loop = read_pcap;
1382 if (!ops_touched)
1383 ctx.pcap = PCAP_OPS_SG;
1387 bug_on(!main_loop);
1389 if (setsockmem)
1390 set_system_socket_memory(vals, array_size(vals));
1391 xlockme();
1393 main_loop(&ctx);
1395 xunlockme();
1396 if (setsockmem)
1397 reset_system_socket_memory(vals, array_size(vals));
1399 tprintf_cleanup();
1401 free(ctx.device_in);
1402 free(ctx.device_out);
1403 free(ctx.device_trans);
1404 free(ctx.prefix);
1406 return 0;