netsniff-ng: also support tcpdump-like filters
[netsniff-ng.git] / src / netsniff-ng.c
blobb0e2c257c1cf35a27fbe8994ee6bade1623f7fba
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, jumbo_support, packet_type, verbose;
62 unsigned long kpull, dump_interval, reserve_size, tx_bytes, tx_packets;
63 bool randomize, promiscuous, enforce;
64 enum pcap_ops_groups pcap; enum dump_mode dump_mode;
65 uid_t uid; gid_t gid; uint32_t link_type;
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:B:HQmcsqXlvhF:RGAP:Vu:g:";
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 {"unbind-cpu", required_argument, NULL, 'B'},
85 {"prefix", required_argument, NULL, 'P'},
86 {"user", required_argument, NULL, 'u'},
87 {"group", required_argument, NULL, 'g'},
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 {"silent", no_argument, NULL, 's'},
98 {"less", no_argument, NULL, 'q'},
99 {"hex", no_argument, NULL, 'X'},
100 {"ascii", no_argument, NULL, 'l'},
101 {"no-sock-mem", no_argument, NULL, 'A'},
102 {"verbose", no_argument, NULL, 'V'},
103 {"version", no_argument, NULL, 'v'},
104 {"help", no_argument, NULL, 'h'},
105 {NULL, 0, NULL, 0}
108 static int tx_sock;
110 static struct itimerval itimer;
112 static unsigned long frame_count_max = 0, interval = TX_KERNEL_PULL_INT;
114 #define set_system_socket_memory(vals) \
115 do { \
116 if ((vals[0] = get_system_socket_mem(sock_rmem_max)) < SMEM_SUG_MAX) \
117 set_system_socket_mem(sock_rmem_max, SMEM_SUG_MAX); \
118 if ((vals[1] = get_system_socket_mem(sock_rmem_def)) < SMEM_SUG_DEF) \
119 set_system_socket_mem(sock_rmem_def, SMEM_SUG_DEF); \
120 if ((vals[2] = get_system_socket_mem(sock_wmem_max)) < SMEM_SUG_MAX) \
121 set_system_socket_mem(sock_wmem_max, SMEM_SUG_MAX); \
122 if ((vals[3] = get_system_socket_mem(sock_wmem_def)) < SMEM_SUG_DEF) \
123 set_system_socket_mem(sock_wmem_def, SMEM_SUG_DEF); \
124 } while (0)
126 #define reset_system_socket_memory(vals) \
127 do { \
128 set_system_socket_mem(sock_rmem_max, vals[0]); \
129 set_system_socket_mem(sock_rmem_def, vals[1]); \
130 set_system_socket_mem(sock_wmem_max, vals[2]); \
131 set_system_socket_mem(sock_wmem_def, vals[3]); \
132 } while (0)
134 #define __pcap_io pcap_ops[ctx->pcap]
136 static void signal_handler(int number)
138 switch (number) {
139 case SIGINT:
140 sigint = 1;
141 case SIGHUP:
142 default:
143 break;
147 static void timer_elapsed(int unused)
149 itimer.it_interval.tv_sec = 0;
150 itimer.it_interval.tv_usec = interval;
152 itimer.it_value.tv_sec = 0;
153 itimer.it_value.tv_usec = interval;
155 pull_and_flush_tx_ring(tx_sock);
156 setitimer(ITIMER_REAL, &itimer, NULL);
159 static void timer_next_dump(int unused)
161 itimer.it_interval.tv_sec = interval;
162 itimer.it_interval.tv_usec = 0;
164 itimer.it_value.tv_sec = interval;
165 itimer.it_value.tv_usec = 0;
167 next_dump = true;
168 setitimer(ITIMER_REAL, &itimer, NULL);
171 static inline bool dump_to_pcap(struct ctx *ctx)
173 return ctx->dump;
176 static void pcap_to_xmit(struct ctx *ctx)
178 __label__ out;
179 uint8_t *out = NULL;
180 int irq, ifindex, fd = 0, ret;
181 unsigned int size, it = 0;
182 unsigned long trunced = 0;
183 struct ring tx_ring;
184 struct frame_map *hdr;
185 struct sock_fprog bpf_ops;
186 struct timeval start, end, diff;
187 struct pcap_pkthdr phdr;
189 if (!device_up_and_running(ctx->device_out) && !ctx->rfraw)
190 panic("Device not up and running!\n");
192 bug_on(!__pcap_io);
194 tx_sock = pf_socket();
196 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
198 ret = __pcap_io->pull_file_header(fd, &ctx->link_type);
199 if (ret)
200 panic("Error reading pcap header!\n");
202 if (__pcap_io->prepare_reading_pcap) {
203 ret = __pcap_io->prepare_reading_pcap(fd);
204 if (ret)
205 panic("Error prepare reading pcap!\n");
208 fmemset(&tx_ring, 0, sizeof(tx_ring));
209 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
211 if (ctx->rfraw) {
212 ctx->device_trans = xstrdup(ctx->device_out);
213 xfree(ctx->device_out);
215 enter_rfmon_mac80211(ctx->device_trans, &ctx->device_out);
216 if (ctx->link_type != LINKTYPE_IEEE802_11)
217 panic("Wrong linktype of pcap!\n");
220 ifindex = device_ifindex(ctx->device_out);
222 size = ring_size(ctx->device_out, ctx->reserve_size);
224 bpf_parse_rules(ctx->device_out, ctx->filter, &bpf_ops);
226 set_packet_loss_discard(tx_sock);
227 set_sockopt_hwtimestamp(tx_sock, ctx->device_out);
229 setup_tx_ring_layout(tx_sock, &tx_ring, size, ctx->jumbo_support);
230 create_tx_ring(tx_sock, &tx_ring, ctx->verbose);
231 mmap_tx_ring(tx_sock, &tx_ring);
232 alloc_tx_ring_frames(&tx_ring);
233 bind_tx_ring(tx_sock, &tx_ring, ifindex);
235 dissector_init_all(ctx->print_mode);
237 if (ctx->cpu >= 0 && ifindex > 0) {
238 irq = device_irq_number(ctx->device_out);
239 device_bind_irq_to_cpu(irq, ctx->cpu);
241 if (ctx->verbose)
242 printf("IRQ: %s:%d > CPU%d\n",
243 ctx->device_out, irq, ctx->cpu);
246 if (ctx->kpull)
247 interval = ctx->kpull;
249 if (ctx->verbose) {
250 printf("BPF:\n");
251 bpf_dump_all(&bpf_ops);
253 printf("MD: TX %luus %s ", interval, pcap_ops[ctx->pcap]->name);
254 if (ctx->rfraw)
255 printf("802.11 raw via %s ", ctx->device_out);
256 #ifdef _LARGEFILE64_SOURCE
257 printf("lf64 ");
258 #endif
259 ioprio_print();
260 printf("\n");
263 itimer.it_interval.tv_sec = 0;
264 itimer.it_interval.tv_usec = interval;
266 itimer.it_value.tv_sec = 0;
267 itimer.it_value.tv_usec = interval;
269 setitimer(ITIMER_REAL, &itimer, NULL);
271 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
273 printf("Running! Hang up with ^C!\n\n");
274 fflush(stdout);
276 bug_on(gettimeofday(&start, NULL));
278 while (likely(sigint == 0)) {
279 while (user_may_pull_from_tx(tx_ring.frames[it].iov_base)) {
280 hdr = tx_ring.frames[it].iov_base;
282 /* Kernel assumes: data = ph.raw + po->tp_hdrlen -
283 * sizeof(struct sockaddr_ll); */
284 out = ((uint8_t *) hdr) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
286 do {
287 ret = __pcap_io->read_pcap_pkt(fd, &phdr, out,
288 ring_frame_size(&tx_ring));
289 if (unlikely(ret <= 0))
290 goto out;
292 if (ring_frame_size(&tx_ring) < phdr.len) {
293 phdr.len = ring_frame_size(&tx_ring);
294 trunced++;
296 } while (ctx->filter && !bpf_run_filter(&bpf_ops, out, phdr.len));
298 pcap_pkthdr_to_tpacket_hdr(&phdr, &hdr->tp_h);
300 ctx->tx_bytes += hdr->tp_h.tp_len;;
301 ctx->tx_packets++;
303 show_frame_hdr(hdr, ctx->print_mode, RING_MODE_EGRESS);
305 dissector_entry_point(out, hdr->tp_h.tp_snaplen,
306 ctx->link_type, ctx->print_mode);
308 kernel_may_pull_from_tx(&hdr->tp_h);
310 it++;
311 if (it >= tx_ring.layout.tp_frame_nr)
312 it = 0;
314 if (unlikely(sigint == 1))
315 break;
317 if (frame_count_max != 0) {
318 if (ctx->tx_packets >= frame_count_max) {
319 sigint = 1;
320 break;
326 out:
328 bug_on(gettimeofday(&end, NULL));
329 diff = tv_subtract(end, start);
331 bpf_release(&bpf_ops);
333 dissector_cleanup_all();
334 destroy_tx_ring(tx_sock, &tx_ring);
336 if (ctx->rfraw)
337 leave_rfmon_mac80211(ctx->device_trans, ctx->device_out);
339 if (__pcap_io->prepare_close_pcap)
340 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_READ);
342 close(fd);
343 close(tx_sock);
345 fflush(stdout);
346 printf("\n");
347 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
348 printf("\r%12lu packets truncated in file\n", trunced);
349 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
350 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
353 static void receive_to_xmit(struct ctx *ctx)
355 short ifflags = 0;
356 uint8_t *in, *out;
357 int rx_sock, ifindex_in, ifindex_out;
358 unsigned int size_in, size_out, it_in = 0, it_out = 0;
359 unsigned long frame_count = 0;
360 struct frame_map *hdr_in, *hdr_out;
361 struct ring tx_ring, rx_ring;
362 struct pollfd rx_poll;
363 struct sock_fprog bpf_ops;
365 if (!strncmp(ctx->device_in, ctx->device_out, IFNAMSIZ))
366 panic("Ingress/egress devices must be different!\n");
367 if (!device_up_and_running(ctx->device_out))
368 panic("Egress device not up and running!\n");
369 if (!device_up_and_running(ctx->device_in))
370 panic("Ingress device not up and running!\n");
372 rx_sock = pf_socket();
373 tx_sock = pf_socket();
375 fmemset(&tx_ring, 0, sizeof(tx_ring));
376 fmemset(&rx_ring, 0, sizeof(rx_ring));
377 fmemset(&rx_poll, 0, sizeof(rx_poll));
378 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
380 ifindex_in = device_ifindex(ctx->device_in);
381 ifindex_out = device_ifindex(ctx->device_out);
383 size_in = ring_size(ctx->device_in, ctx->reserve_size);
384 size_out = ring_size(ctx->device_out, ctx->reserve_size);
386 enable_kernel_bpf_jit_compiler();
388 bpf_parse_rules(ctx->device_in, ctx->filter, &bpf_ops);
389 bpf_attach_to_sock(rx_sock, &bpf_ops);
391 setup_rx_ring_layout(rx_sock, &rx_ring, size_in, ctx->jumbo_support);
392 create_rx_ring(rx_sock, &rx_ring, ctx->verbose);
393 mmap_rx_ring(rx_sock, &rx_ring);
394 alloc_rx_ring_frames(&rx_ring);
395 bind_rx_ring(rx_sock, &rx_ring, ifindex_in);
396 prepare_polling(rx_sock, &rx_poll);
398 set_packet_loss_discard(tx_sock);
399 setup_tx_ring_layout(tx_sock, &tx_ring, size_out, ctx->jumbo_support);
400 create_tx_ring(tx_sock, &tx_ring, ctx->verbose);
401 mmap_tx_ring(tx_sock, &tx_ring);
402 alloc_tx_ring_frames(&tx_ring);
403 bind_tx_ring(tx_sock, &tx_ring, ifindex_out);
405 dissector_init_all(ctx->print_mode);
407 if (ctx->promiscuous)
408 ifflags = enter_promiscuous_mode(ctx->device_in);
410 if (ctx->kpull)
411 interval = ctx->kpull;
413 itimer.it_interval.tv_sec = 0;
414 itimer.it_interval.tv_usec = interval;
416 itimer.it_value.tv_sec = 0;
417 itimer.it_value.tv_usec = interval;
419 setitimer(ITIMER_REAL, &itimer, NULL);
421 if (ctx->verbose) {
422 printf("BPF:\n");
423 bpf_dump_all(&bpf_ops);
425 printf("MD: RXTX %luus\n\n", interval);
428 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
430 printf("Running! Hang up with ^C!\n\n");
431 fflush(stdout);
433 while (likely(sigint == 0)) {
434 while (user_may_pull_from_rx(rx_ring.frames[it_in].iov_base)) {
435 __label__ next;
437 hdr_in = rx_ring.frames[it_in].iov_base;
438 in = ((uint8_t *) hdr_in) + hdr_in->tp_h.tp_mac;
440 frame_count++;
442 if (ctx->packet_type != -1)
443 if (ctx->packet_type != hdr_in->s_ll.sll_pkttype)
444 goto next;
446 hdr_out = tx_ring.frames[it_out].iov_base;
447 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
449 for (; !user_may_pull_from_tx(tx_ring.frames[it_out].iov_base) &&
450 likely(!sigint);) {
451 if (ctx->randomize)
452 next_rnd_slot(&it_out, &tx_ring);
453 else {
454 it_out++;
455 if (it_out >= tx_ring.layout.tp_frame_nr)
456 it_out = 0;
459 hdr_out = tx_ring.frames[it_out].iov_base;
460 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
463 tpacket_hdr_clone(&hdr_out->tp_h, &hdr_in->tp_h);
464 fmemcpy(out, in, hdr_in->tp_h.tp_len);
466 kernel_may_pull_from_tx(&hdr_out->tp_h);
467 if (ctx->randomize)
468 next_rnd_slot(&it_out, &tx_ring);
469 else {
470 it_out++;
471 if (it_out >= tx_ring.layout.tp_frame_nr)
472 it_out = 0;
475 show_frame_hdr(hdr_in, ctx->print_mode, RING_MODE_INGRESS);
477 dissector_entry_point(in, hdr_in->tp_h.tp_snaplen,
478 ctx->link_type, ctx->print_mode);
480 if (frame_count_max != 0) {
481 if (frame_count >= frame_count_max) {
482 sigint = 1;
483 break;
487 next:
489 kernel_may_pull_from_rx(&hdr_in->tp_h);
491 it_in++;
492 if (it_in >= rx_ring.layout.tp_frame_nr)
493 it_in = 0;
495 if (unlikely(sigint == 1))
496 goto out;
499 poll(&rx_poll, 1, -1);
500 poll_error_maybe_die(rx_sock, &rx_poll);
503 out:
505 sock_print_net_stats(rx_sock, 0);
507 bpf_release(&bpf_ops);
509 dissector_cleanup_all();
511 destroy_tx_ring(tx_sock, &tx_ring);
512 destroy_rx_ring(rx_sock, &rx_ring);
514 if (ctx->promiscuous)
515 leave_promiscuous_mode(ctx->device_in, ifflags);
517 close(tx_sock);
518 close(rx_sock);
521 static void translate_pcap_to_txf(int fdo, uint8_t *out, size_t len)
523 size_t bytes_done = 0;
524 char bout[80];
526 slprintf(bout, sizeof(bout), "{\n ");
527 write_or_die(fdo, bout, strlen(bout));
529 while (bytes_done < len) {
530 slprintf(bout, sizeof(bout), "0x%02x, ", out[bytes_done]);
531 write_or_die(fdo, bout, strlen(bout));
533 bytes_done++;
535 if (bytes_done % 10 == 0) {
536 slprintf(bout, sizeof(bout), "\n");
537 write_or_die(fdo, bout, strlen(bout));
539 if (bytes_done < len) {
540 slprintf(bout, sizeof(bout), " ");
541 write_or_die(fdo, bout, strlen(bout));
545 if (bytes_done % 10 != 0) {
546 slprintf(bout, sizeof(bout), "\n");
547 write_or_die(fdo, bout, strlen(bout));
550 slprintf(bout, sizeof(bout), "}\n\n");
551 write_or_die(fdo, bout, strlen(bout));
554 static void read_pcap(struct ctx *ctx)
556 __label__ out;
557 uint8_t *out;
558 int ret, fd, fdo = 0;
559 unsigned long trunced = 0;
560 size_t out_len;
561 struct pcap_pkthdr phdr;
562 struct sock_fprog bpf_ops;
563 struct frame_map fm;
564 struct timeval start, end, diff;
566 bug_on(!__pcap_io);
568 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
570 ret = __pcap_io->pull_file_header(fd, &ctx->link_type);
571 if (ret)
572 panic("Error reading pcap header!\n");
574 if (__pcap_io->prepare_reading_pcap) {
575 ret = __pcap_io->prepare_reading_pcap(fd);
576 if (ret)
577 panic("Error prepare reading pcap!\n");
580 fmemset(&fm, 0, sizeof(fm));
581 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
583 bpf_parse_rules("any", ctx->filter, &bpf_ops);
585 dissector_init_all(ctx->print_mode);
587 out_len = round_up(1024 * 1024, PAGE_SIZE);
588 out = xmalloc_aligned(out_len, CO_CACHE_LINE_SIZE);
590 if (ctx->verbose) {
591 printf("BPF:\n");
592 bpf_dump_all(&bpf_ops);
594 printf("MD: RD %s ", __pcap_io->name);
595 #ifdef _LARGEFILE64_SOURCE
596 printf("lf64 ");
597 #endif
598 ioprio_print();
599 printf("\n");
602 if (ctx->device_out)
603 fdo = open_or_die_m(ctx->device_out, O_RDWR | O_CREAT |
604 O_TRUNC | O_LARGEFILE, DEFFILEMODE);
606 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
608 printf("Running! Hang up with ^C!\n\n");
609 fflush(stdout);
611 bug_on(gettimeofday(&start, NULL));
613 while (likely(sigint == 0)) {
614 do {
615 ret = __pcap_io->read_pcap_pkt(fd, &phdr, out, out_len);
616 if (unlikely(ret < 0))
617 goto out;
619 if (unlikely(phdr.len == 0)) {
620 trunced++;
621 continue;
624 if (unlikely(phdr.len > out_len)) {
625 phdr.len = out_len;
626 trunced++;
628 } while (ctx->filter && !bpf_run_filter(&bpf_ops, out, phdr.len));
630 pcap_pkthdr_to_tpacket_hdr(&phdr, &fm.tp_h);
632 ctx->tx_bytes += fm.tp_h.tp_len;
633 ctx->tx_packets++;
635 show_frame_hdr(&fm, ctx->print_mode, RING_MODE_EGRESS);
637 dissector_entry_point(out, fm.tp_h.tp_snaplen,
638 ctx->link_type, ctx->print_mode);
640 if (ctx->device_out)
641 translate_pcap_to_txf(fdo, out, fm.tp_h.tp_snaplen);
643 if (frame_count_max != 0) {
644 if (ctx->tx_packets >= frame_count_max) {
645 sigint = 1;
646 break;
651 out:
653 bug_on(gettimeofday(&end, NULL));
654 diff = tv_subtract(end, start);
656 bpf_release(&bpf_ops);
658 dissector_cleanup_all();
660 if (__pcap_io->prepare_close_pcap)
661 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_READ);
663 close(fd);
664 if (ctx->device_out)
665 close(fdo);
667 xfree(out);
669 fflush(stdout);
670 printf("\n");
671 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
672 printf("\r%12lu packets truncated in file\n", trunced);
673 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
674 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
677 static void finish_multi_pcap_file(struct ctx *ctx, int fd)
679 __pcap_io->fsync_pcap(fd);
681 if (__pcap_io->prepare_close_pcap)
682 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WRITE);
684 close(fd);
686 fmemset(&itimer, 0, sizeof(itimer));
687 setitimer(ITIMER_REAL, &itimer, NULL);
690 static int next_multi_pcap_file(struct ctx *ctx, int fd)
692 int ret;
693 char fname[512];
695 __pcap_io->fsync_pcap(fd);
697 if (__pcap_io->prepare_close_pcap)
698 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WRITE);
700 close(fd);
702 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
703 ctx->prefix ? : "dump-", time(0));
705 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
706 O_LARGEFILE, DEFFILEMODE);
708 ret = __pcap_io->push_file_header(fd, ctx->link_type);
709 if (ret)
710 panic("Error writing pcap header!\n");
712 if (__pcap_io->prepare_writing_pcap) {
713 ret = __pcap_io->prepare_writing_pcap(fd);
714 if (ret)
715 panic("Error prepare writing pcap!\n");
718 return fd;
721 static int begin_multi_pcap_file(struct ctx *ctx)
723 int fd, ret;
724 char fname[256];
726 bug_on(!__pcap_io);
728 if (ctx->device_out[strlen(ctx->device_out) - 1] == '/')
729 ctx->device_out[strlen(ctx->device_out) - 1] = 0;
731 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
732 ctx->prefix ? : "dump-", time(0));
734 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
735 O_LARGEFILE, DEFFILEMODE);
737 ret = __pcap_io->push_file_header(fd, ctx->link_type);
738 if (ret)
739 panic("Error writing pcap header!\n");
741 if (__pcap_io->prepare_writing_pcap) {
742 ret = __pcap_io->prepare_writing_pcap(fd);
743 if (ret)
744 panic("Error prepare writing pcap!\n");
747 if (ctx->dump_mode == DUMP_INTERVAL_TIME) {
748 interval = ctx->dump_interval;
750 itimer.it_interval.tv_sec = interval;
751 itimer.it_interval.tv_usec = 0;
753 itimer.it_value.tv_sec = interval;
754 itimer.it_value.tv_usec = 0;
756 setitimer(ITIMER_REAL, &itimer, NULL);
757 } else {
758 interval = 0;
761 return fd;
764 static void finish_single_pcap_file(struct ctx *ctx, int fd)
766 __pcap_io->fsync_pcap(fd);
768 if (__pcap_io->prepare_close_pcap)
769 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WRITE);
771 close(fd);
774 static int begin_single_pcap_file(struct ctx *ctx)
776 int fd, ret;
778 bug_on(!__pcap_io);
780 fd = open_or_die_m(ctx->device_out, O_RDWR | O_CREAT | O_TRUNC |
781 O_LARGEFILE, DEFFILEMODE);
783 ret = __pcap_io->push_file_header(fd, ctx->link_type);
784 if (ret)
785 panic("Error writing pcap header!\n");
787 if (__pcap_io->prepare_writing_pcap) {
788 ret = __pcap_io->prepare_writing_pcap(fd);
789 if (ret)
790 panic("Error prepare writing pcap!\n");
793 return fd;
796 static void print_pcap_file_stats(int sock, struct ctx *ctx, unsigned long skipped)
798 unsigned long good, bad;
799 struct tpacket_stats kstats;
800 socklen_t slen = sizeof(kstats);
802 fmemset(&kstats, 0, sizeof(kstats));
803 getsockopt(sock, SOL_PACKET, PACKET_STATISTICS, &kstats, &slen);
805 if (ctx->print_mode == PRINT_NONE) {
806 good = kstats.tp_packets - kstats.tp_drops - skipped;
807 bad = kstats.tp_drops + skipped;
809 printf(".(+%lu/-%lu)", good, bad);
810 fflush(stdout);
814 static void recv_only_or_dump(struct ctx *ctx)
816 uint8_t *packet;
817 short ifflags = 0;
818 int sock, irq, ifindex, fd = 0, ret;
819 unsigned int size, it = 0;
820 unsigned long frame_count = 0, skipped = 0;
821 struct ring rx_ring;
822 struct pollfd rx_poll;
823 struct frame_map *hdr;
824 struct sock_fprog bpf_ops;
825 struct timeval start, end, diff;
826 struct pcap_pkthdr phdr;
828 if (!device_up_and_running(ctx->device_in) && !ctx->rfraw)
829 panic("Device not up and running!\n");
831 sock = pf_socket();
833 if (ctx->rfraw) {
834 ctx->device_trans = xstrdup(ctx->device_in);
835 xfree(ctx->device_in);
837 enter_rfmon_mac80211(ctx->device_trans, &ctx->device_in);
838 ctx->link_type = LINKTYPE_IEEE802_11;
841 fmemset(&rx_ring, 0, sizeof(rx_ring));
842 fmemset(&rx_poll, 0, sizeof(rx_poll));
843 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
845 ifindex = device_ifindex(ctx->device_in);
847 size = ring_size(ctx->device_in, ctx->reserve_size);
849 enable_kernel_bpf_jit_compiler();
851 bpf_parse_rules(ctx->device_in, ctx->filter, &bpf_ops);
852 bpf_attach_to_sock(sock, &bpf_ops);
854 set_sockopt_hwtimestamp(sock, ctx->device_in);
856 setup_rx_ring_layout(sock, &rx_ring, size, ctx->jumbo_support);
857 create_rx_ring(sock, &rx_ring, ctx->verbose);
858 mmap_rx_ring(sock, &rx_ring);
859 alloc_rx_ring_frames(&rx_ring);
860 bind_rx_ring(sock, &rx_ring, ifindex);
862 prepare_polling(sock, &rx_poll);
863 dissector_init_all(ctx->print_mode);
865 if (ctx->cpu >= 0 && ifindex > 0) {
866 irq = device_irq_number(ctx->device_in);
867 device_bind_irq_to_cpu(irq, ctx->cpu);
869 if (ctx->verbose)
870 printf("IRQ: %s:%d > CPU%d\n",
871 ctx->device_in, irq, ctx->cpu);
874 if (ctx->promiscuous)
875 ifflags = enter_promiscuous_mode(ctx->device_in);
877 if (ctx->verbose) {
878 printf("BPF:\n");
879 bpf_dump_all(&bpf_ops);
881 printf("MD: RX %s ", ctx->dump ? pcap_ops[ctx->pcap]->name : "");
882 if (ctx->rfraw)
883 printf("802.11 raw via %s ", ctx->device_in);
884 #ifdef _LARGEFILE64_SOURCE
885 printf("lf64 ");
886 #endif
887 ioprio_print();
888 printf("\n");
891 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
893 if (dump_to_pcap(ctx)) {
894 __label__ try_file;
895 struct stat stats;
897 fmemset(&stats, 0, sizeof(stats));
898 ret = stat(ctx->device_out, &stats);
899 if (ret < 0) {
900 ctx->dump_dir = 0;
901 goto try_file;
904 ctx->dump_dir = S_ISDIR(stats.st_mode);
905 if (ctx->dump_dir) {
906 fd = begin_multi_pcap_file(ctx);
907 } else {
908 try_file:
909 fd = begin_single_pcap_file(ctx);
913 printf("Running! Hang up with ^C!\n\n");
914 fflush(stdout);
916 bug_on(gettimeofday(&start, NULL));
918 while (likely(sigint == 0)) {
919 while (user_may_pull_from_rx(rx_ring.frames[it].iov_base)) {
920 __label__ next;
922 hdr = rx_ring.frames[it].iov_base;
923 packet = ((uint8_t *) hdr) + hdr->tp_h.tp_mac;
924 frame_count++;
926 if (ctx->packet_type != -1)
927 if (ctx->packet_type != hdr->s_ll.sll_pkttype)
928 goto next;
930 if (unlikely(ring_frame_size(&rx_ring) < hdr->tp_h.tp_snaplen)) {
931 skipped++;
932 goto next;
935 if (dump_to_pcap(ctx)) {
936 tpacket_hdr_to_pcap_pkthdr(&hdr->tp_h, &phdr);
938 ret = __pcap_io->write_pcap_pkt(fd, &phdr, packet, phdr.len);
939 if (unlikely(ret != sizeof(phdr) + phdr.len))
940 panic("Write error to pcap!\n");
943 show_frame_hdr(hdr, ctx->print_mode, RING_MODE_INGRESS);
945 dissector_entry_point(packet, hdr->tp_h.tp_snaplen,
946 ctx->link_type, ctx->print_mode);
948 if (frame_count_max != 0) {
949 if (frame_count >= frame_count_max) {
950 sigint = 1;
951 break;
955 next:
957 kernel_may_pull_from_rx(&hdr->tp_h);
959 it++;
960 if (it >= rx_ring.layout.tp_frame_nr)
961 it = 0;
963 if (unlikely(sigint == 1))
964 break;
966 if (dump_to_pcap(ctx)) {
967 if (ctx->dump_mode == DUMP_INTERVAL_SIZE) {
968 interval += hdr->tp_h.tp_snaplen;
970 if (interval > ctx->dump_interval) {
971 next_dump = true;
972 interval = 0;
976 if (next_dump) {
977 fd = next_multi_pcap_file(ctx, fd);
978 next_dump = false;
980 if (ctx->verbose)
981 print_pcap_file_stats(sock, ctx, skipped);
986 poll(&rx_poll, 1, -1);
987 poll_error_maybe_die(sock, &rx_poll);
990 bug_on(gettimeofday(&end, NULL));
991 diff = tv_subtract(end, start);
993 if (dump_to_pcap(ctx)) {
994 if (ctx->dump_dir)
995 finish_multi_pcap_file(ctx, fd);
996 else
997 finish_single_pcap_file(ctx, fd);
1000 if (!(ctx->dump_dir && ctx->print_mode == PRINT_NONE)) {
1001 sock_print_net_stats(sock, skipped);
1003 printf("\r%12lu sec, %lu usec in total\n",
1004 diff.tv_sec, diff.tv_usec);
1005 } else {
1006 printf("\n\n");
1007 fflush(stdout);
1010 bpf_release(&bpf_ops);
1011 dissector_cleanup_all();
1012 destroy_rx_ring(sock, &rx_ring);
1014 if (ctx->promiscuous)
1015 leave_promiscuous_mode(ctx->device_in, ifflags);
1017 if (ctx->rfraw)
1018 leave_rfmon_mac80211(ctx->device_trans, ctx->device_in);
1020 close(sock);
1023 static void help(void)
1025 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING);
1026 puts("http://www.netsniff-ng.org\n\n"
1027 "Usage: netsniff-ng [options] [filter-expression]\n"
1028 "Options:\n"
1029 " -i|-d|--dev|--in <dev|pcap> Input source as netdev or pcap\n"
1030 " -o|--out <dev|pcap|dir|cfg> Output sink as netdev, pcap, directory, trafgen file\n"
1031 " -f|--filter <bpf-file|expr> Use BPF filter file from bpfc or tcpdump-like expression\n"
1032 " -t|--type <type> Only handle packets of defined type:\n"
1033 " host|broadcast|multicast|others|outgoing\n"
1034 " -F|--interval <size/time> Dump interval in time or size if -o is a directory\n"
1035 " pcap swap spec: <num>KiB/MiB/GiB/s/sec/min/hrs\n"
1036 " -J|--jumbo-support Support for 64KB Super Jumbo Frames\n"
1037 " Default RX/TX slot: 2048Byte\n"
1038 " -R|--rfraw Capture or inject raw 802.11 frames\n"
1039 " -n|--num <uint> Number of packets until exit\n"
1040 " `-- 0 Loop until interrupted (default)\n"
1041 " `- n Send n packets and done\n"
1042 "Options for printing:\n"
1043 " -s|--silent Do not print captured packets\n"
1044 " -q|--less Print less-verbose packet information\n"
1045 " -X|--hex Print packet data in hex format\n"
1046 " -l|--ascii Print human-readable packet data\n"
1047 "Options, advanced:\n"
1048 " -P|--prefix <name> Prefix for pcaps stored in directory\n"
1049 " -r|--rand Randomize packet forwarding order\n"
1050 " -M|--no-promisc No promiscuous mode for netdev\n"
1051 " -A|--no-sock-mem Don't tune core socket memory\n"
1052 " -m|--mmap Mmap pcap file i.e., for replaying\n"
1053 " -G|--sg Scatter/gather pcap file I/O\n"
1054 " -c|--clrw Use slower read(2)/write(2) I/O\n"
1055 " -S|--ring-size <size> Manually set ring size to <size>:\n"
1056 " mmap space in KiB/MiB/GiB, e.g. \'10MiB\'\n"
1057 " -k|--kernel-pull <uint> Kernel pull from user interval in us\n"
1058 " Default is 10us where the TX_RING\n"
1059 " is populated with payload from uspace\n"
1060 " -b|--bind-cpu <cpu> Bind to specific CPU (or CPU-range)\n"
1061 " -B|--unbind-cpu <cpu> Forbid to use specific CPU (or CPU-range)\n"
1062 " -u|--user <userid> Drop privileges and change to userid\n"
1063 " -g|--group <groupid> Drop privileges and change to groupid\n"
1064 " -H|--prio-high Make this high priority process\n"
1065 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
1066 " -V|--verbose Be more verbose\n"
1067 " -v|--version Show version\n"
1068 " -h|--help Guess what?!\n\n"
1069 "Examples:\n"
1070 " netsniff-ng --in eth0 --out dump.pcap --silent --bind-cpu 0\n"
1071 " netsniff-ng --in wlan0 --rfraw --out dump.pcap --silent --bind-cpu 0\n"
1072 " netsniff-ng --in dump.pcap --mmap --out eth0 -k1000 --silent --bind-cpu 0\n"
1073 " netsniff-ng --in dump.pcap --out dump.cfg --silent --bind-cpu 0\n"
1074 " netsniff-ng --in eth0 --out eth1 --silent --bind-cpu 0 --type host\n"
1075 " netsniff-ng --in eth1 --out /opt/probe/ -s -m -J --interval 100MiB -b 0\n"
1076 " netsniff-ng --in vlan0 --out dump.pcap -c -u `id -u bob` -g `id -g bob`\n"
1077 " netsniff-ng --in any --filter http.bpf --jumbo-support --ascii -V\n\n"
1078 "Please report bugs to <bugs@netsniff-ng.org>\n"
1079 "Copyright (C) 2009-2013 Daniel Borkmann <daniel@netsniff-ng.org>\n"
1080 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel@netsniff-ng.org>\n"
1081 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1082 "License: GNU GPL version 2.0\n"
1083 "This is free software: you are free to change and redistribute it.\n"
1084 "There is NO WARRANTY, to the extent permitted by law.\n");
1085 die();
1088 static void version(void)
1090 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING);
1091 puts("http://www.netsniff-ng.org\n\n"
1092 "Please report bugs to <bugs@netsniff-ng.org>\n"
1093 "Copyright (C) 2009-2013 Daniel Borkmann <daniel@netsniff-ng.org>\n"
1094 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel@netsniff-ng.org>\n"
1095 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1096 "License: GNU GPL version 2.0\n"
1097 "This is free software: you are free to change and redistribute it.\n"
1098 "There is NO WARRANTY, to the extent permitted by law.\n");
1099 die();
1102 static void header(void)
1104 printf("%s%s%s\n", colorize_start(bold), "netsniff-ng " VERSION_STRING, colorize_end());
1107 int main(int argc, char **argv)
1109 char *ptr;
1110 int c, i, j, opt_index, ops_touched = 0, vals[4] = {0};
1111 bool prio_high = false, setsockmem = true;
1112 void (*main_loop)(struct ctx *ctx) = NULL;
1113 struct ctx ctx = {
1114 .link_type = LINKTYPE_EN10MB,
1115 .print_mode = PRINT_NORM,
1116 .cpu = -1,
1117 .packet_type = -1,
1118 .promiscuous = true,
1119 .randomize = false,
1120 .pcap = PCAP_OPS_SG,
1121 .dump_interval = 60,
1122 .dump_mode = DUMP_INTERVAL_TIME,
1123 .uid = getuid(),
1124 .gid = getgid()
1127 srand(time(NULL));
1129 while ((c = getopt_long(argc, argv, short_options, long_options,
1130 &opt_index)) != EOF) {
1131 switch (c) {
1132 case 'd':
1133 case 'i':
1134 ctx.device_in = xstrdup(optarg);
1135 break;
1136 case 'o':
1137 ctx.device_out = xstrdup(optarg);
1138 break;
1139 case 'P':
1140 ctx.prefix = xstrdup(optarg);
1141 break;
1142 case 'R':
1143 ctx.link_type = LINKTYPE_IEEE802_11;
1144 ctx.rfraw = 1;
1145 break;
1146 case 'r':
1147 ctx.randomize = true;
1148 break;
1149 case 'J':
1150 ctx.jumbo_support = 1;
1151 break;
1152 case 'f':
1153 ctx.filter = xstrdup(optarg);
1154 break;
1155 case 'M':
1156 ctx.promiscuous = false;
1157 break;
1158 case 'A':
1159 setsockmem = false;
1160 break;
1161 case 'u':
1162 ctx.uid = strtoul(optarg, NULL, 0);
1163 ctx.enforce = true;
1164 break;
1165 case 'g':
1166 ctx.gid = strtoul(optarg, NULL, 0);
1167 ctx.enforce = true;
1168 break;
1169 case 't':
1170 if (!strncmp(optarg, "host", strlen("host")))
1171 ctx.packet_type = PACKET_HOST;
1172 else if (!strncmp(optarg, "broadcast", strlen("broadcast")))
1173 ctx.packet_type = PACKET_BROADCAST;
1174 else if (!strncmp(optarg, "multicast", strlen("multicast")))
1175 ctx.packet_type = PACKET_MULTICAST;
1176 else if (!strncmp(optarg, "others", strlen("others")))
1177 ctx.packet_type = PACKET_OTHERHOST;
1178 else if (!strncmp(optarg, "outgoing", strlen("outgoing")))
1179 ctx.packet_type = PACKET_OUTGOING;
1180 else
1181 ctx.packet_type = -1;
1182 break;
1183 case 'S':
1184 ptr = optarg;
1185 ctx.reserve_size = 0;
1187 for (j = i = strlen(optarg); i > 0; --i) {
1188 if (!isdigit(optarg[j - i]))
1189 break;
1190 ptr++;
1193 if (!strncmp(ptr, "KiB", strlen("KiB")))
1194 ctx.reserve_size = 1 << 10;
1195 else if (!strncmp(ptr, "MiB", strlen("MiB")))
1196 ctx.reserve_size = 1 << 20;
1197 else if (!strncmp(ptr, "GiB", strlen("GiB")))
1198 ctx.reserve_size = 1 << 30;
1199 else
1200 panic("Syntax error in ring size param!\n");
1201 *ptr = 0;
1203 ctx.reserve_size *= strtol(optarg, NULL, 0);
1204 break;
1205 case 'b':
1206 set_cpu_affinity(optarg, 0);
1207 /* Take the first CPU for rebinding the IRQ */
1208 if (ctx.cpu != -2)
1209 ctx.cpu = strtol(optarg, NULL, 0);
1210 break;
1211 case 'B':
1212 set_cpu_affinity(optarg, 1);
1213 break;
1214 case 'H':
1215 prio_high = true;
1216 break;
1217 case 'c':
1218 ctx.pcap = PCAP_OPS_RW;
1219 ops_touched = 1;
1220 break;
1221 case 'm':
1222 ctx.pcap = PCAP_OPS_MMAP;
1223 ops_touched = 1;
1224 break;
1225 case 'G':
1226 ctx.pcap = PCAP_OPS_SG;
1227 ops_touched = 1;
1228 break;
1229 case 'Q':
1230 ctx.cpu = -2;
1231 break;
1232 case 's':
1233 ctx.print_mode = PRINT_NONE;
1234 break;
1235 case 'q':
1236 ctx.print_mode = PRINT_LESS;
1237 break;
1238 case 'X':
1239 ctx.print_mode =
1240 (ctx.print_mode == PRINT_ASCII) ?
1241 PRINT_HEX_ASCII : PRINT_HEX;
1242 break;
1243 case 'l':
1244 ctx.print_mode =
1245 (ctx.print_mode == PRINT_HEX) ?
1246 PRINT_HEX_ASCII : PRINT_ASCII;
1247 break;
1248 case 'k':
1249 ctx.kpull = strtol(optarg, NULL, 0);
1250 break;
1251 case 'n':
1252 frame_count_max = strtol(optarg, NULL, 0);
1253 break;
1254 case 'F':
1255 ptr = optarg;
1256 ctx.dump_interval = 0;
1258 for (j = i = strlen(optarg); i > 0; --i) {
1259 if (!isdigit(optarg[j - i]))
1260 break;
1261 ptr++;
1264 if (!strncmp(ptr, "KiB", strlen("KiB"))) {
1265 ctx.dump_interval = 1 << 10;
1266 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1267 } else if (!strncmp(ptr, "MiB", strlen("MiB"))) {
1268 ctx.dump_interval = 1 << 20;
1269 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1270 } else if (!strncmp(ptr, "GiB", strlen("GiB"))) {
1271 ctx.dump_interval = 1 << 30;
1272 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1273 } else if (!strncmp(ptr, "sec", strlen("sec"))) {
1274 ctx.dump_interval = 1;
1275 ctx.dump_mode = DUMP_INTERVAL_TIME;
1276 } else if (!strncmp(ptr, "min", strlen("min"))) {
1277 ctx.dump_interval = 60;
1278 ctx.dump_mode = DUMP_INTERVAL_TIME;
1279 } else if (!strncmp(ptr, "hrs", strlen("hrs"))) {
1280 ctx.dump_interval = 60 * 60;
1281 ctx.dump_mode = DUMP_INTERVAL_TIME;
1282 } else if (!strncmp(ptr, "s", strlen("s"))) {
1283 ctx.dump_interval = 1;
1284 ctx.dump_mode = DUMP_INTERVAL_TIME;
1285 } else {
1286 panic("Syntax error in time/size param!\n");
1289 *ptr = 0;
1290 ctx.dump_interval *= strtol(optarg, NULL, 0);
1291 break;
1292 case 'V':
1293 ctx.verbose = 1;
1294 break;
1295 case 'v':
1296 version();
1297 break;
1298 case 'h':
1299 help();
1300 break;
1301 case '?':
1302 switch (optopt) {
1303 case 'd':
1304 case 'i':
1305 case 'o':
1306 case 'f':
1307 case 't':
1308 case 'P':
1309 case 'F':
1310 case 'n':
1311 case 'S':
1312 case 'b':
1313 case 'k':
1314 case 'u':
1315 case 'g':
1316 case 'B':
1317 case 'e':
1318 panic("Option -%c requires an argument!\n",
1319 optopt);
1320 default:
1321 if (isprint(optopt))
1322 whine("Unknown option character "
1323 "`0x%X\'!\n", optopt);
1324 die();
1326 default:
1327 break;
1331 if (!ctx.filter && optind != argc) {
1332 int ret;
1333 off_t offset = 0;
1335 for (i = optind; i < argc; ++i) {
1336 size_t alen = strlen(argv[i]) + 2;
1337 size_t flen = ctx.filter ? strlen(ctx.filter) : 0;
1339 ctx.filter = xrealloc(ctx.filter, 1, flen + alen);
1340 ret = slprintf(ctx.filter + offset, strlen(argv[i]) + 2, "%s ", argv[i]);
1341 if (ret < 0)
1342 panic("Cannot concatenate filter string!\n");
1343 else
1344 offset += ret;
1348 if (!ctx.device_in)
1349 ctx.device_in = xstrdup("any");
1351 register_signal(SIGINT, signal_handler);
1352 register_signal(SIGHUP, signal_handler);
1354 header();
1356 tprintf_init();
1358 if (prio_high) {
1359 set_proc_prio(get_default_proc_prio());
1360 set_sched_status(get_default_sched_policy(), get_default_sched_prio());
1363 if (ctx.device_in && (device_mtu(ctx.device_in) ||
1364 !strncmp("any", ctx.device_in, strlen(ctx.device_in)))) {
1365 if (!ctx.device_out) {
1366 ctx.dump = 0;
1367 main_loop = recv_only_or_dump;
1368 } else if (device_mtu(ctx.device_out)) {
1369 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1370 main_loop = receive_to_xmit;
1371 } else {
1372 ctx.dump = 1;
1373 register_signal_f(SIGALRM, timer_next_dump, SA_SIGINFO);
1374 main_loop = recv_only_or_dump;
1375 if (!ops_touched)
1376 ctx.pcap = PCAP_OPS_SG;
1378 } else {
1379 if (ctx.device_out && device_mtu(ctx.device_out)) {
1380 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1381 main_loop = pcap_to_xmit;
1382 if (!ops_touched)
1383 ctx.pcap = PCAP_OPS_MMAP;
1384 } else {
1385 main_loop = read_pcap;
1386 if (!ops_touched)
1387 ctx.pcap = PCAP_OPS_SG;
1391 bug_on(!main_loop);
1393 init_pcap(ctx.pcap, ctx.jumbo_support);
1395 if (setsockmem)
1396 set_system_socket_memory(vals);
1397 xlockme();
1399 main_loop(&ctx);
1401 xunlockme();
1402 if (setsockmem)
1403 reset_system_socket_memory(vals);
1405 tprintf_cleanup();
1406 cleanup_pcap();
1408 free(ctx.device_in);
1409 free(ctx.device_out);
1410 free(ctx.device_trans);
1411 free(ctx.prefix);
1413 return 0;