Merge branch 'master' of github.com:borkmann/netsniff-ng into lldp
[netsniff-ng.git] / netsniff-ng.c
blob41846c594a89f1a89d3b354fe16a6878b5deef84
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, dump_bpf;
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:DB";
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 {"dump-bpf", no_argument, NULL, 'B'},
99 {"silent", no_argument, NULL, 's'},
100 {"less", no_argument, NULL, 'q'},
101 {"hex", no_argument, NULL, 'X'},
102 {"ascii", no_argument, NULL, 'l'},
103 {"no-sock-mem", no_argument, NULL, 'A'},
104 {"verbose", no_argument, NULL, 'V'},
105 {"version", no_argument, NULL, 'v'},
106 {"help", no_argument, NULL, 'h'},
107 {NULL, 0, NULL, 0}
110 static int tx_sock;
112 static struct itimerval itimer;
114 static unsigned long frame_count_max = 0, interval = TX_KERNEL_PULL_INT;
116 #define __pcap_io pcap_ops[ctx->pcap]
118 static void signal_handler(int number)
120 switch (number) {
121 case SIGINT:
122 sigint = 1;
123 case SIGHUP:
124 default:
125 break;
129 static void timer_elapsed(int unused)
131 itimer.it_interval.tv_sec = 0;
132 itimer.it_interval.tv_usec = interval;
134 itimer.it_value.tv_sec = 0;
135 itimer.it_value.tv_usec = interval;
137 pull_and_flush_tx_ring(tx_sock);
138 setitimer(ITIMER_REAL, &itimer, NULL);
141 static void timer_next_dump(int unused)
143 itimer.it_interval.tv_sec = interval;
144 itimer.it_interval.tv_usec = 0;
146 itimer.it_value.tv_sec = interval;
147 itimer.it_value.tv_usec = 0;
149 next_dump = true;
150 setitimer(ITIMER_REAL, &itimer, NULL);
153 static inline bool dump_to_pcap(struct ctx *ctx)
155 return ctx->dump;
158 static void pcap_to_xmit(struct ctx *ctx)
160 __label__ out;
161 uint8_t *out = NULL;
162 int irq, ifindex, fd = 0, ret;
163 unsigned int size, it = 0;
164 unsigned long trunced = 0;
165 struct ring tx_ring;
166 struct frame_map *hdr;
167 struct sock_fprog bpf_ops;
168 struct timeval start, end, diff;
169 pcap_pkthdr_t phdr;
171 if (!device_up_and_running(ctx->device_out) && !ctx->rfraw)
172 panic("Device not up and running!\n");
174 bug_on(!__pcap_io);
176 tx_sock = pf_socket();
178 if (!strncmp("-", ctx->device_in, strlen("-"))) {
179 fd = dup(fileno(stdin));
180 close(fileno(stdin));
181 if (ctx->pcap == PCAP_OPS_MM)
182 ctx->pcap = PCAP_OPS_SG;
183 } else {
184 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
187 ret = __pcap_io->pull_fhdr_pcap(fd, &ctx->magic, &ctx->link_type);
188 if (ret)
189 panic("Error reading pcap header!\n");
191 if (__pcap_io->prepare_access_pcap) {
192 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_RD, ctx->jumbo);
193 if (ret)
194 panic("Error prepare reading pcap!\n");
197 fmemset(&tx_ring, 0, sizeof(tx_ring));
198 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
200 if (ctx->rfraw) {
201 ctx->device_trans = xstrdup(ctx->device_out);
202 xfree(ctx->device_out);
204 enter_rfmon_mac80211(ctx->device_trans, &ctx->device_out);
205 if (ctx->link_type != LINKTYPE_IEEE802_11)
206 panic("Wrong linktype of pcap!\n");
209 ifindex = device_ifindex(ctx->device_out);
211 size = ring_size(ctx->device_out, ctx->reserve_size);
213 bpf_parse_rules(ctx->device_out, ctx->filter, &bpf_ops);
214 if (ctx->dump_bpf)
215 bpf_dump_all(&bpf_ops);
217 set_packet_loss_discard(tx_sock);
218 set_sockopt_hwtimestamp(tx_sock, ctx->device_out);
220 setup_tx_ring_layout(tx_sock, &tx_ring, size, ctx->jumbo);
221 create_tx_ring(tx_sock, &tx_ring, ctx->verbose);
222 mmap_tx_ring(tx_sock, &tx_ring);
223 alloc_tx_ring_frames(&tx_ring);
224 bind_tx_ring(tx_sock, &tx_ring, ifindex);
226 dissector_init_all(ctx->print_mode);
228 if (ctx->cpu >= 0 && ifindex > 0) {
229 irq = device_irq_number(ctx->device_out);
230 device_bind_irq_to_cpu(irq, ctx->cpu);
232 if (ctx->verbose)
233 printf("IRQ: %s:%d > CPU%d\n",
234 ctx->device_out, irq, ctx->cpu);
237 if (ctx->kpull)
238 interval = ctx->kpull;
240 itimer.it_interval.tv_sec = 0;
241 itimer.it_interval.tv_usec = interval;
243 itimer.it_value.tv_sec = 0;
244 itimer.it_value.tv_usec = interval;
246 setitimer(ITIMER_REAL, &itimer, NULL);
248 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
250 printf("Running! Hang up with ^C!\n\n");
251 fflush(stdout);
253 bug_on(gettimeofday(&start, NULL));
255 while (likely(sigint == 0)) {
256 while (user_may_pull_from_tx(tx_ring.frames[it].iov_base)) {
257 hdr = tx_ring.frames[it].iov_base;
258 out = ((uint8_t *) hdr) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
260 do {
261 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic, out,
262 ring_frame_size(&tx_ring));
263 if (unlikely(ret <= 0))
264 goto out;
266 if (ring_frame_size(&tx_ring) <
267 pcap_get_length(&phdr, ctx->magic)) {
268 pcap_set_length(&phdr, ctx->magic,
269 ring_frame_size(&tx_ring));
270 trunced++;
272 } while (ctx->filter &&
273 !bpf_run_filter(&bpf_ops, out,
274 pcap_get_length(&phdr, ctx->magic)));
276 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &hdr->tp_h, &hdr->s_ll);
278 ctx->tx_bytes += hdr->tp_h.tp_len;;
279 ctx->tx_packets++;
281 show_frame_hdr(hdr, ctx->print_mode);
283 dissector_entry_point(out, hdr->tp_h.tp_snaplen,
284 ctx->link_type, ctx->print_mode);
286 kernel_may_pull_from_tx(&hdr->tp_h);
288 it++;
289 if (it >= tx_ring.layout.tp_frame_nr)
290 it = 0;
292 if (unlikely(sigint == 1))
293 break;
295 if (frame_count_max != 0) {
296 if (ctx->tx_packets >= frame_count_max) {
297 sigint = 1;
298 break;
304 out:
306 bug_on(gettimeofday(&end, NULL));
307 diff = tv_subtract(end, start);
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 close(fd);
322 else
323 dup2(fd, fileno(stdin));
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 if (ctx->dump_bpf)
372 bpf_dump_all(&bpf_ops);
373 bpf_attach_to_sock(rx_sock, &bpf_ops);
375 setup_rx_ring_layout(rx_sock, &rx_ring, size_in, ctx->jumbo);
376 create_rx_ring(rx_sock, &rx_ring, ctx->verbose);
377 mmap_rx_ring(rx_sock, &rx_ring);
378 alloc_rx_ring_frames(&rx_ring);
379 bind_rx_ring(rx_sock, &rx_ring, ifindex_in);
380 prepare_polling(rx_sock, &rx_poll);
382 set_packet_loss_discard(tx_sock);
383 setup_tx_ring_layout(tx_sock, &tx_ring, size_out, ctx->jumbo);
384 create_tx_ring(tx_sock, &tx_ring, ctx->verbose);
385 mmap_tx_ring(tx_sock, &tx_ring);
386 alloc_tx_ring_frames(&tx_ring);
387 bind_tx_ring(tx_sock, &tx_ring, ifindex_out);
389 dissector_init_all(ctx->print_mode);
391 if (ctx->promiscuous)
392 ifflags = enter_promiscuous_mode(ctx->device_in);
394 if (ctx->kpull)
395 interval = ctx->kpull;
397 itimer.it_interval.tv_sec = 0;
398 itimer.it_interval.tv_usec = interval;
400 itimer.it_value.tv_sec = 0;
401 itimer.it_value.tv_usec = interval;
403 setitimer(ITIMER_REAL, &itimer, NULL);
405 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
407 printf("Running! Hang up with ^C!\n\n");
408 fflush(stdout);
410 while (likely(sigint == 0)) {
411 while (user_may_pull_from_rx(rx_ring.frames[it_in].iov_base)) {
412 __label__ next;
414 hdr_in = rx_ring.frames[it_in].iov_base;
415 in = ((uint8_t *) hdr_in) + hdr_in->tp_h.tp_mac;
417 frame_count++;
419 if (ctx->packet_type != -1)
420 if (ctx->packet_type != hdr_in->s_ll.sll_pkttype)
421 goto next;
423 hdr_out = tx_ring.frames[it_out].iov_base;
424 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
426 for (; !user_may_pull_from_tx(tx_ring.frames[it_out].iov_base) &&
427 likely(!sigint);) {
428 if (ctx->randomize)
429 next_rnd_slot(&it_out, &tx_ring);
430 else {
431 it_out++;
432 if (it_out >= tx_ring.layout.tp_frame_nr)
433 it_out = 0;
436 hdr_out = tx_ring.frames[it_out].iov_base;
437 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
440 tpacket_hdr_clone(&hdr_out->tp_h, &hdr_in->tp_h);
441 fmemcpy(out, in, hdr_in->tp_h.tp_len);
443 kernel_may_pull_from_tx(&hdr_out->tp_h);
444 if (ctx->randomize)
445 next_rnd_slot(&it_out, &tx_ring);
446 else {
447 it_out++;
448 if (it_out >= tx_ring.layout.tp_frame_nr)
449 it_out = 0;
452 show_frame_hdr(hdr_in, ctx->print_mode);
454 dissector_entry_point(in, hdr_in->tp_h.tp_snaplen,
455 ctx->link_type, ctx->print_mode);
457 if (frame_count_max != 0) {
458 if (frame_count >= frame_count_max) {
459 sigint = 1;
460 break;
464 next:
466 kernel_may_pull_from_rx(&hdr_in->tp_h);
468 it_in++;
469 if (it_in >= rx_ring.layout.tp_frame_nr)
470 it_in = 0;
472 if (unlikely(sigint == 1))
473 goto out;
476 poll(&rx_poll, 1, -1);
477 poll_error_maybe_die(rx_sock, &rx_poll);
480 out:
482 sock_print_net_stats(rx_sock, 0);
484 bpf_release(&bpf_ops);
486 dissector_cleanup_all();
488 destroy_tx_ring(tx_sock, &tx_ring);
489 destroy_rx_ring(rx_sock, &rx_ring);
491 if (ctx->promiscuous)
492 leave_promiscuous_mode(ctx->device_in, ifflags);
494 close(tx_sock);
495 close(rx_sock);
498 static void translate_pcap_to_txf(int fdo, uint8_t *out, size_t len)
500 size_t bytes_done = 0;
501 char bout[80];
503 slprintf(bout, sizeof(bout), "{\n ");
504 write_or_die(fdo, bout, strlen(bout));
506 while (bytes_done < len) {
507 slprintf(bout, sizeof(bout), "0x%02x, ", out[bytes_done]);
508 write_or_die(fdo, bout, strlen(bout));
510 bytes_done++;
512 if (bytes_done % 10 == 0) {
513 slprintf(bout, sizeof(bout), "\n");
514 write_or_die(fdo, bout, strlen(bout));
516 if (bytes_done < len) {
517 slprintf(bout, sizeof(bout), " ");
518 write_or_die(fdo, bout, strlen(bout));
522 if (bytes_done % 10 != 0) {
523 slprintf(bout, sizeof(bout), "\n");
524 write_or_die(fdo, bout, strlen(bout));
527 slprintf(bout, sizeof(bout), "}\n\n");
528 write_or_die(fdo, bout, strlen(bout));
531 static void read_pcap(struct ctx *ctx)
533 __label__ out;
534 uint8_t *out;
535 int ret, fd, fdo = 0;
536 unsigned long trunced = 0;
537 size_t out_len;
538 pcap_pkthdr_t phdr;
539 struct sock_fprog bpf_ops;
540 struct frame_map fm;
541 struct timeval start, end, diff;
542 struct sockaddr_ll sll;
544 bug_on(!__pcap_io);
546 if (!strncmp("-", ctx->device_in, strlen("-"))) {
547 fd = dup(fileno(stdin));
548 close(fileno(stdin));
549 if (ctx->pcap == PCAP_OPS_MM)
550 ctx->pcap = PCAP_OPS_SG;
551 } else {
552 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
555 ret = __pcap_io->pull_fhdr_pcap(fd, &ctx->magic, &ctx->link_type);
556 if (ret)
557 panic("Error reading pcap header!\n");
559 if (__pcap_io->prepare_access_pcap) {
560 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_RD, ctx->jumbo);
561 if (ret)
562 panic("Error prepare reading pcap!\n");
565 fmemset(&fm, 0, sizeof(fm));
566 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
568 bpf_parse_rules("any", ctx->filter, &bpf_ops);
569 if (ctx->dump_bpf)
570 bpf_dump_all(&bpf_ops);
572 dissector_init_all(ctx->print_mode);
574 out_len = round_up(1024 * 1024, PAGE_SIZE);
575 out = xmalloc_aligned(out_len, CO_CACHE_LINE_SIZE);
577 if (ctx->device_out) {
578 if (!strncmp("-", ctx->device_out, strlen("-"))) {
579 fdo = dup(fileno(stdout));
580 close(fileno(stdout));
581 } else {
582 fdo = open_or_die_m(ctx->device_out, O_RDWR | O_CREAT |
583 O_TRUNC | O_LARGEFILE, DEFFILEMODE);
587 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
589 printf("Running! Hang up with ^C!\n\n");
590 fflush(stdout);
592 bug_on(gettimeofday(&start, NULL));
594 while (likely(sigint == 0)) {
595 do {
596 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic,
597 out, out_len);
598 if (unlikely(ret < 0))
599 goto out;
601 if (unlikely(pcap_get_length(&phdr, ctx->magic) == 0)) {
602 trunced++;
603 continue;
606 if (unlikely(pcap_get_length(&phdr, ctx->magic) > out_len)) {
607 pcap_set_length(&phdr, ctx->magic, out_len);
608 trunced++;
610 } while (ctx->filter &&
611 !bpf_run_filter(&bpf_ops, out,
612 pcap_get_length(&phdr, ctx->magic)));
614 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &fm.tp_h, &sll);
616 ctx->tx_bytes += fm.tp_h.tp_len;
617 ctx->tx_packets++;
619 show_frame_hdr(&fm, ctx->print_mode);
621 dissector_entry_point(out, fm.tp_h.tp_snaplen,
622 ctx->link_type, ctx->print_mode);
624 if (ctx->device_out)
625 translate_pcap_to_txf(fdo, out, fm.tp_h.tp_snaplen);
627 if (frame_count_max != 0) {
628 if (ctx->tx_packets >= frame_count_max) {
629 sigint = 1;
630 break;
635 out:
637 bug_on(gettimeofday(&end, NULL));
638 diff = tv_subtract(end, start);
640 bpf_release(&bpf_ops);
642 dissector_cleanup_all();
644 if (__pcap_io->prepare_close_pcap)
645 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
647 xfree(out);
649 fflush(stdout);
650 printf("\n");
651 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
652 printf("\r%12lu packets truncated in file\n", trunced);
653 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
654 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
656 if (strncmp("-", ctx->device_in, strlen("-")))
657 close(fd);
658 else
659 dup2(fd, fileno(stdin));
661 if (ctx->device_out) {
662 if (strncmp("-", ctx->device_out, strlen("-")))
663 close(fdo);
664 else
665 dup2(fdo, fileno(stdout));
669 static void finish_multi_pcap_file(struct ctx *ctx, int fd)
671 __pcap_io->fsync_pcap(fd);
673 if (__pcap_io->prepare_close_pcap)
674 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
676 close(fd);
678 fmemset(&itimer, 0, sizeof(itimer));
679 setitimer(ITIMER_REAL, &itimer, NULL);
682 static int next_multi_pcap_file(struct ctx *ctx, int fd)
684 int ret;
685 char fname[512];
687 __pcap_io->fsync_pcap(fd);
689 if (__pcap_io->prepare_close_pcap)
690 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
692 close(fd);
694 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
695 ctx->prefix ? : "dump-", time(0));
697 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
698 O_LARGEFILE, DEFFILEMODE);
700 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
701 if (ret)
702 panic("Error writing pcap header!\n");
704 if (__pcap_io->prepare_access_pcap) {
705 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, ctx->jumbo);
706 if (ret)
707 panic("Error prepare writing pcap!\n");
710 return fd;
713 static int begin_multi_pcap_file(struct ctx *ctx)
715 int fd, ret;
716 char fname[256];
718 bug_on(!__pcap_io);
720 if (ctx->device_out[strlen(ctx->device_out) - 1] == '/')
721 ctx->device_out[strlen(ctx->device_out) - 1] = 0;
723 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
724 ctx->prefix ? : "dump-", time(0));
726 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
727 O_LARGEFILE, DEFFILEMODE);
729 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
730 if (ret)
731 panic("Error writing pcap header!\n");
733 if (__pcap_io->prepare_access_pcap) {
734 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, ctx->jumbo);
735 if (ret)
736 panic("Error prepare writing pcap!\n");
739 if (ctx->dump_mode == DUMP_INTERVAL_TIME) {
740 interval = ctx->dump_interval;
742 itimer.it_interval.tv_sec = interval;
743 itimer.it_interval.tv_usec = 0;
745 itimer.it_value.tv_sec = interval;
746 itimer.it_value.tv_usec = 0;
748 setitimer(ITIMER_REAL, &itimer, NULL);
749 } else {
750 interval = 0;
753 return fd;
756 static void finish_single_pcap_file(struct ctx *ctx, int fd)
758 __pcap_io->fsync_pcap(fd);
760 if (__pcap_io->prepare_close_pcap)
761 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
763 if (strncmp("-", ctx->device_out, strlen("-")))
764 close(fd);
765 else
766 dup2(fd, fileno(stdout));
769 static int begin_single_pcap_file(struct ctx *ctx)
771 int fd, ret;
773 bug_on(!__pcap_io);
775 if (!strncmp("-", ctx->device_out, strlen("-"))) {
776 fd = dup(fileno(stdout));
777 close(fileno(stdout));
778 if (ctx->pcap == PCAP_OPS_MM)
779 ctx->pcap = PCAP_OPS_SG;
780 } else {
781 fd = open_or_die_m(ctx->device_out,
782 O_RDWR | O_CREAT | O_TRUNC |
783 O_LARGEFILE, DEFFILEMODE);
786 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
787 if (ret)
788 panic("Error writing pcap header!\n");
790 if (__pcap_io->prepare_access_pcap) {
791 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, ctx->jumbo);
792 if (ret)
793 panic("Error prepare writing pcap!\n");
796 return fd;
799 static void print_pcap_file_stats(int sock, struct ctx *ctx, unsigned long skipped)
801 unsigned long good, bad;
802 struct tpacket_stats kstats;
803 socklen_t slen = sizeof(kstats);
805 fmemset(&kstats, 0, sizeof(kstats));
806 getsockopt(sock, SOL_PACKET, PACKET_STATISTICS, &kstats, &slen);
808 if (ctx->print_mode == PRINT_NONE) {
809 good = kstats.tp_packets - kstats.tp_drops - skipped;
810 bad = kstats.tp_drops + skipped;
812 printf(".(+%lu/-%lu)", good, bad);
813 fflush(stdout);
817 static void recv_only_or_dump(struct ctx *ctx)
819 uint8_t *packet;
820 short ifflags = 0;
821 int sock, irq, ifindex, fd = 0, ret;
822 unsigned int size, it = 0;
823 unsigned long frame_count = 0, skipped = 0;
824 struct ring rx_ring;
825 struct pollfd rx_poll;
826 struct frame_map *hdr;
827 struct sock_fprog bpf_ops;
828 struct timeval start, end, diff;
829 pcap_pkthdr_t phdr;
831 if (!device_up_and_running(ctx->device_in) && !ctx->rfraw)
832 panic("Device not up and running!\n");
834 sock = pf_socket();
836 if (ctx->rfraw) {
837 ctx->device_trans = xstrdup(ctx->device_in);
838 xfree(ctx->device_in);
840 enter_rfmon_mac80211(ctx->device_trans, &ctx->device_in);
841 ctx->link_type = LINKTYPE_IEEE802_11;
844 fmemset(&rx_ring, 0, sizeof(rx_ring));
845 fmemset(&rx_poll, 0, sizeof(rx_poll));
846 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
848 ifindex = device_ifindex(ctx->device_in);
850 size = ring_size(ctx->device_in, ctx->reserve_size);
852 enable_kernel_bpf_jit_compiler();
854 bpf_parse_rules(ctx->device_in, ctx->filter, &bpf_ops);
855 if (ctx->dump_bpf)
856 bpf_dump_all(&bpf_ops);
857 bpf_attach_to_sock(sock, &bpf_ops);
859 set_sockopt_hwtimestamp(sock, ctx->device_in);
861 setup_rx_ring_layout(sock, &rx_ring, size, ctx->jumbo);
862 create_rx_ring(sock, &rx_ring, ctx->verbose);
863 mmap_rx_ring(sock, &rx_ring);
864 alloc_rx_ring_frames(&rx_ring);
865 bind_rx_ring(sock, &rx_ring, ifindex);
867 prepare_polling(sock, &rx_poll);
868 dissector_init_all(ctx->print_mode);
870 if (ctx->cpu >= 0 && ifindex > 0) {
871 irq = device_irq_number(ctx->device_in);
872 device_bind_irq_to_cpu(irq, ctx->cpu);
874 if (ctx->verbose)
875 printf("IRQ: %s:%d > CPU%d\n",
876 ctx->device_in, irq, ctx->cpu);
879 if (ctx->promiscuous)
880 ifflags = enter_promiscuous_mode(ctx->device_in);
882 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
884 if (dump_to_pcap(ctx)) {
885 __label__ try_file;
886 struct stat stats;
888 fmemset(&stats, 0, sizeof(stats));
889 ret = stat(ctx->device_out, &stats);
890 if (ret < 0) {
891 ctx->dump_dir = 0;
892 goto try_file;
895 ctx->dump_dir = S_ISDIR(stats.st_mode);
896 if (ctx->dump_dir) {
897 fd = begin_multi_pcap_file(ctx);
898 } else {
899 try_file:
900 fd = begin_single_pcap_file(ctx);
904 printf("Running! Hang up with ^C!\n\n");
905 fflush(stdout);
907 bug_on(gettimeofday(&start, NULL));
909 while (likely(sigint == 0)) {
910 while (user_may_pull_from_rx(rx_ring.frames[it].iov_base)) {
911 __label__ next;
913 hdr = rx_ring.frames[it].iov_base;
914 packet = ((uint8_t *) hdr) + hdr->tp_h.tp_mac;
915 frame_count++;
917 if (ctx->packet_type != -1)
918 if (ctx->packet_type != hdr->s_ll.sll_pkttype)
919 goto next;
921 if (unlikely(ring_frame_size(&rx_ring) < hdr->tp_h.tp_snaplen)) {
922 skipped++;
923 goto next;
926 if (dump_to_pcap(ctx)) {
927 tpacket_hdr_to_pcap_pkthdr(&hdr->tp_h, &hdr->s_ll, &phdr, ctx->magic);
929 ret = __pcap_io->write_pcap(fd, &phdr, ctx->magic, packet,
930 pcap_get_length(&phdr, ctx->magic));
931 if (unlikely(ret != pcap_get_total_length(&phdr, ctx->magic)))
932 panic("Write error to pcap!\n");
935 show_frame_hdr(hdr, ctx->print_mode);
937 dissector_entry_point(packet, hdr->tp_h.tp_snaplen,
938 ctx->link_type, ctx->print_mode);
940 if (frame_count_max != 0) {
941 if (frame_count >= frame_count_max) {
942 sigint = 1;
943 break;
947 next:
949 kernel_may_pull_from_rx(&hdr->tp_h);
951 it++;
952 if (it >= rx_ring.layout.tp_frame_nr)
953 it = 0;
955 if (unlikely(sigint == 1))
956 break;
958 if (dump_to_pcap(ctx)) {
959 if (ctx->dump_mode == DUMP_INTERVAL_SIZE) {
960 interval += hdr->tp_h.tp_snaplen;
962 if (interval > ctx->dump_interval) {
963 next_dump = true;
964 interval = 0;
968 if (next_dump) {
969 fd = next_multi_pcap_file(ctx, fd);
970 next_dump = false;
972 if (ctx->verbose)
973 print_pcap_file_stats(sock, ctx, skipped);
978 poll(&rx_poll, 1, -1);
979 poll_error_maybe_die(sock, &rx_poll);
982 bug_on(gettimeofday(&end, NULL));
983 diff = tv_subtract(end, start);
985 if (!(ctx->dump_dir && ctx->print_mode == PRINT_NONE)) {
986 sock_print_net_stats(sock, skipped);
988 printf("\r%12lu sec, %lu usec in total\n",
989 diff.tv_sec, diff.tv_usec);
990 } else {
991 printf("\n\n");
992 fflush(stdout);
995 bpf_release(&bpf_ops);
996 dissector_cleanup_all();
997 destroy_rx_ring(sock, &rx_ring);
999 if (ctx->promiscuous)
1000 leave_promiscuous_mode(ctx->device_in, ifflags);
1002 if (ctx->rfraw)
1003 leave_rfmon_mac80211(ctx->device_trans, ctx->device_in);
1005 if (dump_to_pcap(ctx)) {
1006 if (ctx->dump_dir)
1007 finish_multi_pcap_file(ctx, fd);
1008 else
1009 finish_single_pcap_file(ctx, fd);
1012 close(sock);
1015 static void help(void)
1017 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING);
1018 puts("http://www.netsniff-ng.org\n\n"
1019 "Usage: netsniff-ng [options] [filter-expression]\n"
1020 "Options:\n"
1021 " -i|-d|--dev|--in <dev|pcap|-> Input source as netdev, pcap or pcap stdin\n"
1022 " -o|--out <dev|pcap|dir|cfg|-> Output sink as netdev, pcap, directory, trafgen, or stdout\n"
1023 " -f|--filter <bpf-file|expr> Use BPF filter file from bpfc or tcpdump-like expression\n"
1024 " -t|--type <type> Filter for: host|broadcast|multicast|others|outgoing\n"
1025 " -F|--interval <size|time> Dump interval if -o is a dir: <num>KiB/MiB/GiB/s/sec/min/hrs\n"
1026 " -J|--jumbo-support Support for 64KB Super Jumbo Frames (def: 2048B)\n"
1027 " -R|--rfraw Capture or inject raw 802.11 frames\n"
1028 " -n|--num <0|uint> Number of packets until exit (def: 0)\n"
1029 " -P|--prefix <name> Prefix for pcaps stored in directory\n"
1030 " -T|--magic <pcap-magic> Pcap magic number/pcap format to store, see -D\n"
1031 " -D|--dump-pcap-types Dump pcap types and magic numbers and quit\n"
1032 " -B|--dump-bpf Dump generated BPF assembly\n"
1033 " -r|--rand Randomize packet forwarding order (dev->dev)\n"
1034 " -M|--no-promisc No promiscuous mode for netdev\n"
1035 " -A|--no-sock-mem Don't tune core socket memory\n"
1036 " -m|--mmap Mmap(2) pcap file i.e., for replaying pcaps\n"
1037 " -G|--sg Scatter/gather pcap file I/O\n"
1038 " -c|--clrw Use slower read(2)/write(2) I/O\n"
1039 " -S|--ring-size <size> Specify ring size to: <num>KiB/MiB/GiB\n"
1040 " -k|--kernel-pull <uint> Kernel pull from user interval in us (def: 10us)\n"
1041 " -b|--bind-cpu <cpu> Bind to specific CPU\n"
1042 " -u|--user <userid> Drop privileges and change to userid\n"
1043 " -g|--group <groupid> Drop privileges and change to groupid\n"
1044 " -H|--prio-high Make this high priority process\n"
1045 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
1046 " -s|--silent Do not print captured packets\n"
1047 " -q|--less Print less-verbose packet information\n"
1048 " -X|--hex Print packet data in hex format\n"
1049 " -l|--ascii Print human-readable packet data\n"
1050 " -V|--verbose Be more verbose\n"
1051 " -v|--version Show version\n"
1052 " -h|--help Guess what?!\n\n"
1053 "Examples:\n"
1054 " netsniff-ng --in eth0 --out dump.pcap -s -T 0xa1b2c3d4 --b 0 tcp or udp\n"
1055 " netsniff-ng --in wlan0 --rfraw --out dump.pcap --silent --bind-cpu 0\n"
1056 " netsniff-ng --in dump.pcap --mmap --out eth0 -k1000 --silent --bind-cpu 0\n"
1057 " netsniff-ng --in dump.pcap --out dump.cfg --silent --bind-cpu 0\n"
1058 " netsniff-ng --in eth0 --out eth1 --silent --bind-cpu 0 --type host\n"
1059 " netsniff-ng --in eth1 --out /opt/probe/ -s -m -J --interval 100MiB -b 0\n"
1060 " netsniff-ng --in vlan0 --out dump.pcap -c -u `id -u bob` -g `id -g bob`\n"
1061 " netsniff-ng --in any --filter http.bpf --jumbo-support --ascii -V\n\n"
1062 "Note:\n"
1063 " For introducing bit errors, delays with random variation and more\n"
1064 " while replaying pcaps, make use of tc(8) with its disciplines (e.g. netem).\n\n"
1065 "Please report bugs to <bugs@netsniff-ng.org>\n"
1066 "Copyright (C) 2009-2013 Daniel Borkmann <daniel@netsniff-ng.org>\n"
1067 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel@netsniff-ng.org>\n"
1068 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1069 "License: GNU GPL version 2.0\n"
1070 "This is free software: you are free to change and redistribute it.\n"
1071 "There is NO WARRANTY, to the extent permitted by law.\n");
1072 die();
1075 static void version(void)
1077 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING);
1078 puts("http://www.netsniff-ng.org\n\n"
1079 "Please report bugs to <bugs@netsniff-ng.org>\n"
1080 "Copyright (C) 2009-2013 Daniel Borkmann <daniel@netsniff-ng.org>\n"
1081 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel@netsniff-ng.org>\n"
1082 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1083 "License: GNU GPL version 2.0\n"
1084 "This is free software: you are free to change and redistribute it.\n"
1085 "There is NO WARRANTY, to the extent permitted by law.\n");
1086 die();
1089 int main(int argc, char **argv)
1091 char *ptr;
1092 int c, i, j, cpu_tmp, opt_index, ops_touched = 0, vals[4] = {0};
1093 bool prio_high = false, setsockmem = true;
1094 void (*main_loop)(struct ctx *ctx) = NULL;
1095 struct ctx ctx = {
1096 .link_type = LINKTYPE_EN10MB,
1097 .print_mode = PRINT_NORM,
1098 .cpu = -1,
1099 .packet_type = -1,
1100 .promiscuous = true,
1101 .randomize = false,
1102 .pcap = PCAP_OPS_SG,
1103 .dump_interval = 60,
1104 .dump_mode = DUMP_INTERVAL_TIME,
1105 .uid = getuid(),
1106 .gid = getgid(),
1107 .magic = ORIGINAL_TCPDUMP_MAGIC,
1110 srand(time(NULL));
1112 while ((c = getopt_long(argc, argv, short_options, long_options,
1113 &opt_index)) != EOF) {
1114 switch (c) {
1115 case 'd':
1116 case 'i':
1117 ctx.device_in = xstrdup(optarg);
1118 break;
1119 case 'o':
1120 ctx.device_out = xstrdup(optarg);
1121 break;
1122 case 'P':
1123 ctx.prefix = xstrdup(optarg);
1124 break;
1125 case 'R':
1126 ctx.link_type = LINKTYPE_IEEE802_11;
1127 ctx.rfraw = 1;
1128 break;
1129 case 'r':
1130 ctx.randomize = true;
1131 break;
1132 case 'J':
1133 ctx.jumbo = true;
1134 break;
1135 case 'T':
1136 ctx.magic = (uint32_t) strtoul(optarg, NULL, 0);
1137 pcap_check_magic(ctx.magic);
1138 break;
1139 case 'f':
1140 ctx.filter = xstrdup(optarg);
1141 break;
1142 case 'M':
1143 ctx.promiscuous = false;
1144 break;
1145 case 'A':
1146 setsockmem = false;
1147 break;
1148 case 'u':
1149 ctx.uid = strtoul(optarg, NULL, 0);
1150 ctx.enforce = true;
1151 break;
1152 case 'g':
1153 ctx.gid = strtoul(optarg, NULL, 0);
1154 ctx.enforce = true;
1155 break;
1156 case 't':
1157 if (!strncmp(optarg, "host", strlen("host")))
1158 ctx.packet_type = PACKET_HOST;
1159 else if (!strncmp(optarg, "broadcast", strlen("broadcast")))
1160 ctx.packet_type = PACKET_BROADCAST;
1161 else if (!strncmp(optarg, "multicast", strlen("multicast")))
1162 ctx.packet_type = PACKET_MULTICAST;
1163 else if (!strncmp(optarg, "others", strlen("others")))
1164 ctx.packet_type = PACKET_OTHERHOST;
1165 else if (!strncmp(optarg, "outgoing", strlen("outgoing")))
1166 ctx.packet_type = PACKET_OUTGOING;
1167 else
1168 ctx.packet_type = -1;
1169 break;
1170 case 'S':
1171 ptr = optarg;
1172 ctx.reserve_size = 0;
1174 for (j = i = strlen(optarg); i > 0; --i) {
1175 if (!isdigit(optarg[j - i]))
1176 break;
1177 ptr++;
1180 if (!strncmp(ptr, "KiB", strlen("KiB")))
1181 ctx.reserve_size = 1 << 10;
1182 else if (!strncmp(ptr, "MiB", strlen("MiB")))
1183 ctx.reserve_size = 1 << 20;
1184 else if (!strncmp(ptr, "GiB", strlen("GiB")))
1185 ctx.reserve_size = 1 << 30;
1186 else
1187 panic("Syntax error in ring size param!\n");
1188 *ptr = 0;
1190 ctx.reserve_size *= strtol(optarg, NULL, 0);
1191 break;
1192 case 'b':
1193 cpu_tmp = strtol(optarg, NULL, 0);
1195 cpu_affinity(cpu_tmp);
1196 if (ctx.cpu != -2)
1197 ctx.cpu = cpu_tmp;
1198 break;
1199 case 'H':
1200 prio_high = true;
1201 break;
1202 case 'c':
1203 ctx.pcap = PCAP_OPS_RW;
1204 ops_touched = 1;
1205 break;
1206 case 'm':
1207 ctx.pcap = PCAP_OPS_MM;
1208 ops_touched = 1;
1209 break;
1210 case 'G':
1211 ctx.pcap = PCAP_OPS_SG;
1212 ops_touched = 1;
1213 break;
1214 case 'Q':
1215 ctx.cpu = -2;
1216 break;
1217 case 's':
1218 ctx.print_mode = PRINT_NONE;
1219 break;
1220 case 'q':
1221 ctx.print_mode = PRINT_LESS;
1222 break;
1223 case 'X':
1224 ctx.print_mode =
1225 (ctx.print_mode == PRINT_ASCII) ?
1226 PRINT_HEX_ASCII : PRINT_HEX;
1227 break;
1228 case 'l':
1229 ctx.print_mode =
1230 (ctx.print_mode == PRINT_HEX) ?
1231 PRINT_HEX_ASCII : PRINT_ASCII;
1232 break;
1233 case 'k':
1234 ctx.kpull = strtol(optarg, NULL, 0);
1235 break;
1236 case 'n':
1237 frame_count_max = strtol(optarg, NULL, 0);
1238 break;
1239 case 'F':
1240 ptr = optarg;
1241 ctx.dump_interval = 0;
1243 for (j = i = strlen(optarg); i > 0; --i) {
1244 if (!isdigit(optarg[j - i]))
1245 break;
1246 ptr++;
1249 if (!strncmp(ptr, "KiB", strlen("KiB"))) {
1250 ctx.dump_interval = 1 << 10;
1251 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1252 } else if (!strncmp(ptr, "MiB", strlen("MiB"))) {
1253 ctx.dump_interval = 1 << 20;
1254 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1255 } else if (!strncmp(ptr, "GiB", strlen("GiB"))) {
1256 ctx.dump_interval = 1 << 30;
1257 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1258 } else if (!strncmp(ptr, "sec", strlen("sec"))) {
1259 ctx.dump_interval = 1;
1260 ctx.dump_mode = DUMP_INTERVAL_TIME;
1261 } else if (!strncmp(ptr, "min", strlen("min"))) {
1262 ctx.dump_interval = 60;
1263 ctx.dump_mode = DUMP_INTERVAL_TIME;
1264 } else if (!strncmp(ptr, "hrs", strlen("hrs"))) {
1265 ctx.dump_interval = 60 * 60;
1266 ctx.dump_mode = DUMP_INTERVAL_TIME;
1267 } else if (!strncmp(ptr, "s", strlen("s"))) {
1268 ctx.dump_interval = 1;
1269 ctx.dump_mode = DUMP_INTERVAL_TIME;
1270 } else {
1271 panic("Syntax error in time/size param!\n");
1274 *ptr = 0;
1275 ctx.dump_interval *= strtol(optarg, NULL, 0);
1276 break;
1277 case 'V':
1278 ctx.verbose = 1;
1279 break;
1280 case 'B':
1281 ctx.dump_bpf = true;
1282 break;
1283 case 'D':
1284 pcap_dump_type_features();
1285 die();
1286 break;
1287 case 'v':
1288 version();
1289 break;
1290 case 'h':
1291 help();
1292 break;
1293 case '?':
1294 switch (optopt) {
1295 case 'd':
1296 case 'i':
1297 case 'o':
1298 case 'f':
1299 case 't':
1300 case 'P':
1301 case 'F':
1302 case 'n':
1303 case 'S':
1304 case 'b':
1305 case 'k':
1306 case 'T':
1307 case 'u':
1308 case 'g':
1309 case 'e':
1310 panic("Option -%c requires an argument!\n",
1311 optopt);
1312 default:
1313 if (isprint(optopt))
1314 printf("Unknown option character `0x%X\'!\n", optopt);
1315 die();
1317 default:
1318 break;
1322 if (!ctx.filter && optind != argc) {
1323 int ret;
1324 off_t offset = 0;
1326 for (i = optind; i < argc; ++i) {
1327 size_t alen = strlen(argv[i]) + 2;
1328 size_t flen = ctx.filter ? strlen(ctx.filter) : 0;
1330 ctx.filter = xrealloc(ctx.filter, 1, flen + alen);
1331 ret = slprintf(ctx.filter + offset, strlen(argv[i]) + 2, "%s ", argv[i]);
1332 if (ret < 0)
1333 panic("Cannot concatenate filter string!\n");
1334 else
1335 offset += ret;
1339 if (!ctx.device_in)
1340 ctx.device_in = xstrdup("any");
1342 register_signal(SIGINT, signal_handler);
1343 register_signal(SIGHUP, signal_handler);
1345 tprintf_init();
1347 if (prio_high) {
1348 set_proc_prio(get_default_proc_prio());
1349 set_sched_status(get_default_sched_policy(), get_default_sched_prio());
1352 if (ctx.device_in && (device_mtu(ctx.device_in) ||
1353 !strncmp("any", ctx.device_in, strlen(ctx.device_in)))) {
1354 if (!ctx.device_out) {
1355 ctx.dump = 0;
1356 main_loop = recv_only_or_dump;
1357 } else if (device_mtu(ctx.device_out)) {
1358 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1359 main_loop = receive_to_xmit;
1360 } else {
1361 ctx.dump = 1;
1362 register_signal_f(SIGALRM, timer_next_dump, SA_SIGINFO);
1363 main_loop = recv_only_or_dump;
1364 if (!ops_touched)
1365 ctx.pcap = PCAP_OPS_SG;
1367 } else {
1368 if (ctx.device_out && device_mtu(ctx.device_out)) {
1369 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1370 main_loop = pcap_to_xmit;
1371 if (!ops_touched)
1372 ctx.pcap = PCAP_OPS_MM;
1373 } else {
1374 main_loop = read_pcap;
1375 if (!ops_touched)
1376 ctx.pcap = PCAP_OPS_SG;
1380 bug_on(!main_loop);
1382 if (setsockmem)
1383 set_system_socket_memory(vals, array_size(vals));
1384 xlockme();
1386 main_loop(&ctx);
1388 xunlockme();
1389 if (setsockmem)
1390 reset_system_socket_memory(vals, array_size(vals));
1392 tprintf_cleanup();
1394 free(ctx.device_in);
1395 free(ctx.device_out);
1396 free(ctx.device_trans);
1397 free(ctx.prefix);
1399 return 0;