netsniff-ng: pcap: fix multi-file dump with dropped privs
[netsniff-ng.git] / src / netsniff-ng.c
blob748c55c62926c44e5365d04555002db3707b62e2
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;
65 enum dump_mode dump_mode;
66 uid_t uid;
67 gid_t gid;
68 uint32_t link_type;
71 volatile sig_atomic_t sigint = 0;
73 static volatile bool next_dump = false;
75 static const char *short_options = "d:i:o:rf:MJt:S:k:n:b:B:HQmcsqXlvhF:RGAP:Vu:g:";
76 static const struct option long_options[] = {
77 {"dev", required_argument, NULL, 'd'},
78 {"in", required_argument, NULL, 'i'},
79 {"out", required_argument, NULL, 'o'},
80 {"filter", required_argument, NULL, 'f'},
81 {"num", required_argument, NULL, 'n'},
82 {"type", required_argument, NULL, 't'},
83 {"interval", required_argument, NULL, 'F'},
84 {"ring-size", required_argument, NULL, 'S'},
85 {"kernel-pull", required_argument, NULL, 'k'},
86 {"bind-cpu", required_argument, NULL, 'b'},
87 {"unbind-cpu", required_argument, NULL, 'B'},
88 {"prefix", required_argument, NULL, 'P'},
89 {"user", required_argument, NULL, 'u'},
90 {"group", required_argument, NULL, 'g'},
91 {"rand", no_argument, NULL, 'r'},
92 {"rfraw", no_argument, NULL, 'R'},
93 {"mmap", no_argument, NULL, 'm'},
94 {"sg", no_argument, NULL, 'G'},
95 {"clrw", no_argument, NULL, 'c'},
96 {"jumbo-support", no_argument, NULL, 'J'},
97 {"no-promisc", no_argument, NULL, 'M'},
98 {"prio-high", no_argument, NULL, 'H'},
99 {"notouch-irq", no_argument, NULL, 'Q'},
100 {"silent", no_argument, NULL, 's'},
101 {"less", no_argument, NULL, 'q'},
102 {"hex", no_argument, NULL, 'X'},
103 {"ascii", no_argument, NULL, 'l'},
104 {"no-sock-mem", no_argument, NULL, 'A'},
105 {"verbose", no_argument, NULL, 'V'},
106 {"version", no_argument, NULL, 'v'},
107 {"help", no_argument, NULL, 'h'},
108 {NULL, 0, NULL, 0}
111 static int tx_sock;
113 static struct itimerval itimer;
115 static unsigned long frame_count_max = 0, interval = TX_KERNEL_PULL_INT;
117 #define set_system_socket_memory(vals) \
118 do { \
119 if ((vals[0] = get_system_socket_mem(sock_rmem_max)) < SMEM_SUG_MAX) \
120 set_system_socket_mem(sock_rmem_max, SMEM_SUG_MAX); \
121 if ((vals[1] = get_system_socket_mem(sock_rmem_def)) < SMEM_SUG_DEF) \
122 set_system_socket_mem(sock_rmem_def, SMEM_SUG_DEF); \
123 if ((vals[2] = get_system_socket_mem(sock_wmem_max)) < SMEM_SUG_MAX) \
124 set_system_socket_mem(sock_wmem_max, SMEM_SUG_MAX); \
125 if ((vals[3] = get_system_socket_mem(sock_wmem_def)) < SMEM_SUG_DEF) \
126 set_system_socket_mem(sock_wmem_def, SMEM_SUG_DEF); \
127 } while (0)
129 #define reset_system_socket_memory(vals) \
130 do { \
131 set_system_socket_mem(sock_rmem_max, vals[0]); \
132 set_system_socket_mem(sock_rmem_def, vals[1]); \
133 set_system_socket_mem(sock_wmem_max, vals[2]); \
134 set_system_socket_mem(sock_wmem_def, vals[3]); \
135 } while (0)
137 #define __pcap_io pcap_ops[ctx->pcap]
139 static void signal_handler(int number)
141 switch (number) {
142 case SIGINT:
143 sigint = 1;
144 case SIGHUP:
145 default:
146 break;
150 static void timer_elapsed(int unused)
152 itimer.it_interval.tv_sec = 0;
153 itimer.it_interval.tv_usec = interval;
155 itimer.it_value.tv_sec = 0;
156 itimer.it_value.tv_usec = interval;
158 pull_and_flush_tx_ring(tx_sock);
159 setitimer(ITIMER_REAL, &itimer, NULL);
162 static void timer_next_dump(int unused)
164 itimer.it_interval.tv_sec = interval;
165 itimer.it_interval.tv_usec = 0;
167 itimer.it_value.tv_sec = interval;
168 itimer.it_value.tv_usec = 0;
170 next_dump = true;
171 setitimer(ITIMER_REAL, &itimer, NULL);
174 static inline bool dump_to_pcap(struct ctx *ctx)
176 return ctx->dump;
179 static void pcap_to_xmit(struct ctx *ctx)
181 __label__ out;
182 uint8_t *out = NULL;
183 int irq, ifindex, fd = 0, ret;
184 unsigned int size, it = 0;
185 unsigned long trunced = 0;
186 struct ring tx_ring;
187 struct frame_map *hdr;
188 struct sock_fprog bpf_ops;
189 struct timeval start, end, diff;
190 struct pcap_pkthdr phdr;
192 if (!device_up_and_running(ctx->device_out) && !ctx->rfraw)
193 panic("Device not up and running!\n");
195 bug_on(!__pcap_io);
197 tx_sock = pf_socket();
199 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
201 ret = __pcap_io->pull_file_header(fd, &ctx->link_type);
202 if (ret)
203 panic("Error reading pcap header!\n");
205 if (__pcap_io->prepare_reading_pcap) {
206 ret = __pcap_io->prepare_reading_pcap(fd);
207 if (ret)
208 panic("Error prepare reading pcap!\n");
211 fmemset(&tx_ring, 0, sizeof(tx_ring));
212 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
214 if (ctx->rfraw) {
215 ctx->device_trans = xstrdup(ctx->device_out);
216 xfree(ctx->device_out);
218 enter_rfmon_mac80211(ctx->device_trans, &ctx->device_out);
219 if (ctx->link_type != LINKTYPE_IEEE802_11)
220 panic("Wrong linktype of pcap!\n");
223 ifindex = device_ifindex(ctx->device_out);
225 size = ring_size(ctx->device_out, ctx->reserve_size);
227 bpf_parse_rules(ctx->filter, &bpf_ops);
229 set_packet_loss_discard(tx_sock);
230 set_sockopt_hwtimestamp(tx_sock, ctx->device_out);
232 setup_tx_ring_layout(tx_sock, &tx_ring, size, ctx->jumbo_support);
233 create_tx_ring(tx_sock, &tx_ring, ctx->verbose);
234 mmap_tx_ring(tx_sock, &tx_ring);
235 alloc_tx_ring_frames(&tx_ring);
236 bind_tx_ring(tx_sock, &tx_ring, ifindex);
238 dissector_init_all(ctx->print_mode);
240 if (ctx->cpu >= 0 && ifindex > 0) {
241 irq = device_irq_number(ctx->device_out);
242 device_bind_irq_to_cpu(irq, ctx->cpu);
244 if (ctx->verbose)
245 printf("IRQ: %s:%d > CPU%d\n",
246 ctx->device_out, irq, ctx->cpu);
249 if (ctx->kpull)
250 interval = ctx->kpull;
252 if (ctx->verbose) {
253 printf("BPF:\n");
254 bpf_dump_all(&bpf_ops);
256 printf("MD: TX %luus %s ", interval, pcap_ops[ctx->pcap]->name);
257 if (ctx->rfraw)
258 printf("802.11 raw via %s ", ctx->device_out);
259 #ifdef _LARGEFILE64_SOURCE
260 printf("lf64 ");
261 #endif
262 ioprio_print();
263 printf("\n");
266 itimer.it_interval.tv_sec = 0;
267 itimer.it_interval.tv_usec = interval;
269 itimer.it_value.tv_sec = 0;
270 itimer.it_value.tv_usec = interval;
272 setitimer(ITIMER_REAL, &itimer, NULL);
274 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
276 printf("Running! Hang up with ^C!\n\n");
277 fflush(stdout);
279 bug_on(gettimeofday(&start, NULL));
281 while (likely(sigint == 0)) {
282 while (user_may_pull_from_tx(tx_ring.frames[it].iov_base)) {
283 hdr = tx_ring.frames[it].iov_base;
285 /* Kernel assumes: data = ph.raw + po->tp_hdrlen -
286 * sizeof(struct sockaddr_ll); */
287 out = ((uint8_t *) hdr) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
289 do {
290 ret = __pcap_io->read_pcap_pkt(fd, &phdr, out,
291 ring_frame_size(&tx_ring));
292 if (unlikely(ret <= 0))
293 goto out;
295 if (ring_frame_size(&tx_ring) < phdr.len) {
296 phdr.len = ring_frame_size(&tx_ring);
297 trunced++;
299 } while (ctx->filter && !bpf_run_filter(&bpf_ops, out, phdr.len));
301 pcap_pkthdr_to_tpacket_hdr(&phdr, &hdr->tp_h);
303 ctx->tx_bytes += hdr->tp_h.tp_len;;
304 ctx->tx_packets++;
306 show_frame_hdr(hdr, ctx->print_mode, RING_MODE_EGRESS);
308 dissector_entry_point(out, hdr->tp_h.tp_snaplen,
309 ctx->link_type, ctx->print_mode);
311 kernel_may_pull_from_tx(&hdr->tp_h);
313 it++;
314 if (it >= tx_ring.layout.tp_frame_nr)
315 it = 0;
317 if (unlikely(sigint == 1))
318 break;
320 if (frame_count_max != 0) {
321 if (ctx->tx_packets >= frame_count_max) {
322 sigint = 1;
323 break;
329 out:
331 bug_on(gettimeofday(&end, NULL));
332 diff = tv_subtract(end, start);
334 bpf_release(&bpf_ops);
336 dissector_cleanup_all();
337 destroy_tx_ring(tx_sock, &tx_ring);
339 if (ctx->rfraw)
340 leave_rfmon_mac80211(ctx->device_trans, ctx->device_out);
342 if (__pcap_io->prepare_close_pcap)
343 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_READ);
345 close(fd);
346 close(tx_sock);
348 fflush(stdout);
349 printf("\n");
350 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
351 printf("\r%12lu packets truncated in file\n", trunced);
352 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
353 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
356 static void receive_to_xmit(struct ctx *ctx)
358 short ifflags = 0;
359 uint8_t *in, *out;
360 int rx_sock, ifindex_in, ifindex_out;
361 unsigned int size_in, size_out, it_in = 0, it_out = 0;
362 unsigned long frame_count = 0;
363 struct frame_map *hdr_in, *hdr_out;
364 struct ring tx_ring, rx_ring;
365 struct pollfd rx_poll;
366 struct sock_fprog bpf_ops;
368 if (!strncmp(ctx->device_in, ctx->device_out, IFNAMSIZ))
369 panic("Ingress/egress devices must be different!\n");
370 if (!device_up_and_running(ctx->device_out))
371 panic("Egress device not up and running!\n");
372 if (!device_up_and_running(ctx->device_in))
373 panic("Ingress device not up and running!\n");
375 rx_sock = pf_socket();
376 tx_sock = pf_socket();
378 fmemset(&tx_ring, 0, sizeof(tx_ring));
379 fmemset(&rx_ring, 0, sizeof(rx_ring));
380 fmemset(&rx_poll, 0, sizeof(rx_poll));
381 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
383 ifindex_in = device_ifindex(ctx->device_in);
384 ifindex_out = device_ifindex(ctx->device_out);
386 size_in = ring_size(ctx->device_in, ctx->reserve_size);
387 size_out = ring_size(ctx->device_out, ctx->reserve_size);
389 enable_kernel_bpf_jit_compiler();
391 bpf_parse_rules(ctx->filter, &bpf_ops);
392 bpf_attach_to_sock(rx_sock, &bpf_ops);
394 setup_rx_ring_layout(rx_sock, &rx_ring, size_in, ctx->jumbo_support);
395 create_rx_ring(rx_sock, &rx_ring, ctx->verbose);
396 mmap_rx_ring(rx_sock, &rx_ring);
397 alloc_rx_ring_frames(&rx_ring);
398 bind_rx_ring(rx_sock, &rx_ring, ifindex_in);
399 prepare_polling(rx_sock, &rx_poll);
401 set_packet_loss_discard(tx_sock);
402 setup_tx_ring_layout(tx_sock, &tx_ring, size_out, ctx->jumbo_support);
403 create_tx_ring(tx_sock, &tx_ring, ctx->verbose);
404 mmap_tx_ring(tx_sock, &tx_ring);
405 alloc_tx_ring_frames(&tx_ring);
406 bind_tx_ring(tx_sock, &tx_ring, ifindex_out);
408 dissector_init_all(ctx->print_mode);
410 if (ctx->promiscuous)
411 ifflags = enter_promiscuous_mode(ctx->device_in);
413 if (ctx->kpull)
414 interval = ctx->kpull;
416 itimer.it_interval.tv_sec = 0;
417 itimer.it_interval.tv_usec = interval;
419 itimer.it_value.tv_sec = 0;
420 itimer.it_value.tv_usec = interval;
422 setitimer(ITIMER_REAL, &itimer, NULL);
424 if (ctx->verbose) {
425 printf("BPF:\n");
426 bpf_dump_all(&bpf_ops);
428 printf("MD: RXTX %luus\n\n", interval);
431 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
433 printf("Running! Hang up with ^C!\n\n");
434 fflush(stdout);
436 while (likely(sigint == 0)) {
437 while (user_may_pull_from_rx(rx_ring.frames[it_in].iov_base)) {
438 __label__ next;
440 hdr_in = rx_ring.frames[it_in].iov_base;
441 in = ((uint8_t *) hdr_in) + hdr_in->tp_h.tp_mac;
443 frame_count++;
445 if (ctx->packet_type != -1)
446 if (ctx->packet_type != hdr_in->s_ll.sll_pkttype)
447 goto next;
449 hdr_out = tx_ring.frames[it_out].iov_base;
450 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
452 for (; !user_may_pull_from_tx(tx_ring.frames[it_out].iov_base) &&
453 likely(!sigint);) {
454 if (ctx->randomize)
455 next_rnd_slot(&it_out, &tx_ring);
456 else {
457 it_out++;
458 if (it_out >= tx_ring.layout.tp_frame_nr)
459 it_out = 0;
462 hdr_out = tx_ring.frames[it_out].iov_base;
463 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
466 tpacket_hdr_clone(&hdr_out->tp_h, &hdr_in->tp_h);
467 fmemcpy(out, in, hdr_in->tp_h.tp_len);
469 kernel_may_pull_from_tx(&hdr_out->tp_h);
470 if (ctx->randomize)
471 next_rnd_slot(&it_out, &tx_ring);
472 else {
473 it_out++;
474 if (it_out >= tx_ring.layout.tp_frame_nr)
475 it_out = 0;
478 show_frame_hdr(hdr_in, ctx->print_mode, RING_MODE_INGRESS);
480 dissector_entry_point(in, hdr_in->tp_h.tp_snaplen,
481 ctx->link_type, ctx->print_mode);
483 if (frame_count_max != 0) {
484 if (frame_count >= frame_count_max) {
485 sigint = 1;
486 break;
490 next:
492 kernel_may_pull_from_rx(&hdr_in->tp_h);
494 it_in++;
495 if (it_in >= rx_ring.layout.tp_frame_nr)
496 it_in = 0;
498 if (unlikely(sigint == 1))
499 goto out;
502 poll(&rx_poll, 1, -1);
503 poll_error_maybe_die(rx_sock, &rx_poll);
506 out:
508 sock_print_net_stats(rx_sock, 0);
510 bpf_release(&bpf_ops);
512 dissector_cleanup_all();
514 destroy_tx_ring(tx_sock, &tx_ring);
515 destroy_rx_ring(rx_sock, &rx_ring);
517 if (ctx->promiscuous)
518 leave_promiscuous_mode(ctx->device_in, ifflags);
520 close(tx_sock);
521 close(rx_sock);
524 static void translate_pcap_to_txf(int fdo, uint8_t *out, size_t len)
526 size_t bytes_done = 0;
527 char bout[80];
529 slprintf(bout, sizeof(bout), "{\n ");
530 write_or_die(fdo, bout, strlen(bout));
532 while (bytes_done < len) {
533 slprintf(bout, sizeof(bout), "0x%02x, ", out[bytes_done]);
534 write_or_die(fdo, bout, strlen(bout));
536 bytes_done++;
538 if (bytes_done % 10 == 0) {
539 slprintf(bout, sizeof(bout), "\n");
540 write_or_die(fdo, bout, strlen(bout));
542 if (bytes_done < len) {
543 slprintf(bout, sizeof(bout), " ");
544 write_or_die(fdo, bout, strlen(bout));
548 if (bytes_done % 10 != 0) {
549 slprintf(bout, sizeof(bout), "\n");
550 write_or_die(fdo, bout, strlen(bout));
553 slprintf(bout, sizeof(bout), "}\n\n");
554 write_or_die(fdo, bout, strlen(bout));
557 static void read_pcap(struct ctx *ctx)
559 __label__ out;
560 uint8_t *out;
561 int ret, fd, fdo = 0;
562 unsigned long trunced = 0;
563 size_t out_len;
564 struct pcap_pkthdr phdr;
565 struct sock_fprog bpf_ops;
566 struct frame_map fm;
567 struct timeval start, end, diff;
569 bug_on(!__pcap_io);
571 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
573 ret = __pcap_io->pull_file_header(fd, &ctx->link_type);
574 if (ret)
575 panic("Error reading pcap header!\n");
577 if (__pcap_io->prepare_reading_pcap) {
578 ret = __pcap_io->prepare_reading_pcap(fd);
579 if (ret)
580 panic("Error prepare reading pcap!\n");
583 fmemset(&fm, 0, sizeof(fm));
584 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
586 bpf_parse_rules(ctx->filter, &bpf_ops);
588 dissector_init_all(ctx->print_mode);
590 out_len = round_up(1024 * 1024, PAGE_SIZE);
591 out = xmalloc_aligned(out_len, CO_CACHE_LINE_SIZE);
593 if (ctx->verbose) {
594 printf("BPF:\n");
595 bpf_dump_all(&bpf_ops);
597 printf("MD: RD %s ", __pcap_io->name);
598 #ifdef _LARGEFILE64_SOURCE
599 printf("lf64 ");
600 #endif
601 ioprio_print();
602 printf("\n");
605 if (ctx->device_out)
606 fdo = open_or_die_m(ctx->device_out, O_RDWR | O_CREAT |
607 O_TRUNC | O_LARGEFILE, DEFFILEMODE);
609 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
611 printf("Running! Hang up with ^C!\n\n");
612 fflush(stdout);
614 bug_on(gettimeofday(&start, NULL));
616 while (likely(sigint == 0)) {
617 do {
618 ret = __pcap_io->read_pcap_pkt(fd, &phdr, out, out_len);
619 if (unlikely(ret < 0))
620 goto out;
622 if (unlikely(phdr.len == 0)) {
623 trunced++;
624 continue;
627 if (unlikely(phdr.len > out_len)) {
628 phdr.len = out_len;
629 trunced++;
631 } while (ctx->filter && !bpf_run_filter(&bpf_ops, out, phdr.len));
633 pcap_pkthdr_to_tpacket_hdr(&phdr, &fm.tp_h);
635 ctx->tx_bytes += fm.tp_h.tp_len;
636 ctx->tx_packets++;
638 show_frame_hdr(&fm, ctx->print_mode, RING_MODE_EGRESS);
640 dissector_entry_point(out, fm.tp_h.tp_snaplen,
641 ctx->link_type, ctx->print_mode);
643 if (ctx->device_out)
644 translate_pcap_to_txf(fdo, out, fm.tp_h.tp_snaplen);
646 if (frame_count_max != 0) {
647 if (ctx->tx_packets >= frame_count_max) {
648 sigint = 1;
649 break;
654 out:
656 bug_on(gettimeofday(&end, NULL));
657 diff = tv_subtract(end, start);
659 bpf_release(&bpf_ops);
661 dissector_cleanup_all();
663 if (__pcap_io->prepare_close_pcap)
664 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_READ);
666 close(fd);
667 if (ctx->device_out)
668 close(fdo);
670 xfree(out);
672 fflush(stdout);
673 printf("\n");
674 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
675 printf("\r%12lu packets truncated in file\n", trunced);
676 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
677 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
680 static void finish_multi_pcap_file(struct ctx *ctx, int fd)
682 __pcap_io->fsync_pcap(fd);
684 if (__pcap_io->prepare_close_pcap)
685 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WRITE);
687 close(fd);
689 fmemset(&itimer, 0, sizeof(itimer));
690 setitimer(ITIMER_REAL, &itimer, NULL);
693 static int next_multi_pcap_file(struct ctx *ctx, int fd)
695 int ret;
696 char fname[512];
698 __pcap_io->fsync_pcap(fd);
700 if (__pcap_io->prepare_close_pcap)
701 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WRITE);
703 close(fd);
705 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
706 ctx->prefix ? : "dump-", time(0));
708 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
709 O_LARGEFILE, DEFFILEMODE);
711 ret = __pcap_io->push_file_header(fd, ctx->link_type);
712 if (ret)
713 panic("Error writing pcap header!\n");
715 if (__pcap_io->prepare_writing_pcap) {
716 ret = __pcap_io->prepare_writing_pcap(fd);
717 if (ret)
718 panic("Error prepare writing pcap!\n");
721 return fd;
724 static int begin_multi_pcap_file(struct ctx *ctx)
726 int fd, ret;
727 char fname[256];
729 bug_on(!__pcap_io);
731 if (ctx->device_out[strlen(ctx->device_out) - 1] == '/')
732 ctx->device_out[strlen(ctx->device_out) - 1] = 0;
734 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
735 ctx->prefix ? : "dump-", time(0));
737 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
738 O_LARGEFILE, DEFFILEMODE);
740 ret = __pcap_io->push_file_header(fd, ctx->link_type);
741 if (ret)
742 panic("Error writing pcap header!\n");
744 if (__pcap_io->prepare_writing_pcap) {
745 ret = __pcap_io->prepare_writing_pcap(fd);
746 if (ret)
747 panic("Error prepare writing pcap!\n");
750 if (ctx->dump_mode == DUMP_INTERVAL_TIME) {
751 interval = ctx->dump_interval;
753 itimer.it_interval.tv_sec = interval;
754 itimer.it_interval.tv_usec = 0;
756 itimer.it_value.tv_sec = interval;
757 itimer.it_value.tv_usec = 0;
759 setitimer(ITIMER_REAL, &itimer, NULL);
760 } else {
761 interval = 0;
764 return fd;
767 static void finish_single_pcap_file(struct ctx *ctx, int fd)
769 __pcap_io->fsync_pcap(fd);
771 if (__pcap_io->prepare_close_pcap)
772 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WRITE);
774 close(fd);
777 static int begin_single_pcap_file(struct ctx *ctx)
779 int fd, ret;
781 bug_on(!__pcap_io);
783 fd = open_or_die_m(ctx->device_out, O_RDWR | O_CREAT | O_TRUNC |
784 O_LARGEFILE, DEFFILEMODE);
786 ret = __pcap_io->push_file_header(fd, ctx->link_type);
787 if (ret)
788 panic("Error writing pcap header!\n");
790 if (__pcap_io->prepare_writing_pcap) {
791 ret = __pcap_io->prepare_writing_pcap(fd);
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 struct pcap_pkthdr 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->filter, &bpf_ops);
855 bpf_attach_to_sock(sock, &bpf_ops);
857 set_sockopt_hwtimestamp(sock, ctx->device_in);
859 setup_rx_ring_layout(sock, &rx_ring, size, ctx->jumbo_support);
860 create_rx_ring(sock, &rx_ring, ctx->verbose);
861 mmap_rx_ring(sock, &rx_ring);
862 alloc_rx_ring_frames(&rx_ring);
863 bind_rx_ring(sock, &rx_ring, ifindex);
865 prepare_polling(sock, &rx_poll);
866 dissector_init_all(ctx->print_mode);
868 if (ctx->cpu >= 0 && ifindex > 0) {
869 irq = device_irq_number(ctx->device_in);
870 device_bind_irq_to_cpu(irq, ctx->cpu);
872 if (ctx->verbose)
873 printf("IRQ: %s:%d > CPU%d\n",
874 ctx->device_in, irq, ctx->cpu);
877 if (ctx->promiscuous)
878 ifflags = enter_promiscuous_mode(ctx->device_in);
880 if (ctx->verbose) {
881 printf("BPF:\n");
882 bpf_dump_all(&bpf_ops);
884 printf("MD: RX %s ", ctx->dump ? pcap_ops[ctx->pcap]->name : "");
885 if (ctx->rfraw)
886 printf("802.11 raw via %s ", ctx->device_in);
887 #ifdef _LARGEFILE64_SOURCE
888 printf("lf64 ");
889 #endif
890 ioprio_print();
891 printf("\n");
894 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
896 if (dump_to_pcap(ctx)) {
897 __label__ try_file;
898 struct stat stats;
900 fmemset(&stats, 0, sizeof(stats));
901 ret = stat(ctx->device_out, &stats);
902 if (ret < 0) {
903 ctx->dump_dir = 0;
904 goto try_file;
907 ctx->dump_dir = S_ISDIR(stats.st_mode);
908 if (ctx->dump_dir) {
909 fd = begin_multi_pcap_file(ctx);
910 } else {
911 try_file:
912 fd = begin_single_pcap_file(ctx);
916 printf("Running! Hang up with ^C!\n\n");
917 fflush(stdout);
919 bug_on(gettimeofday(&start, NULL));
921 while (likely(sigint == 0)) {
922 while (user_may_pull_from_rx(rx_ring.frames[it].iov_base)) {
923 __label__ next;
925 hdr = rx_ring.frames[it].iov_base;
926 packet = ((uint8_t *) hdr) + hdr->tp_h.tp_mac;
927 frame_count++;
929 if (ctx->packet_type != -1)
930 if (ctx->packet_type != hdr->s_ll.sll_pkttype)
931 goto next;
933 if (unlikely(ring_frame_size(&rx_ring) < hdr->tp_h.tp_snaplen)) {
934 skipped++;
935 goto next;
938 if (dump_to_pcap(ctx)) {
939 tpacket_hdr_to_pcap_pkthdr(&hdr->tp_h, &phdr);
941 ret = __pcap_io->write_pcap_pkt(fd, &phdr, packet, phdr.len);
942 if (unlikely(ret != sizeof(phdr) + phdr.len))
943 panic("Write error to pcap!\n");
946 show_frame_hdr(hdr, ctx->print_mode, RING_MODE_INGRESS);
948 dissector_entry_point(packet, hdr->tp_h.tp_snaplen,
949 ctx->link_type, ctx->print_mode);
951 if (frame_count_max != 0) {
952 if (frame_count >= frame_count_max) {
953 sigint = 1;
954 break;
958 next:
960 kernel_may_pull_from_rx(&hdr->tp_h);
962 it++;
963 if (it >= rx_ring.layout.tp_frame_nr)
964 it = 0;
966 if (unlikely(sigint == 1))
967 break;
969 if (dump_to_pcap(ctx)) {
970 if (ctx->dump_mode == DUMP_INTERVAL_SIZE) {
971 interval += hdr->tp_h.tp_snaplen;
973 if (interval > ctx->dump_interval) {
974 next_dump = true;
975 interval = 0;
979 if (next_dump) {
980 fd = next_multi_pcap_file(ctx, fd);
981 next_dump = false;
983 if (ctx->verbose)
984 print_pcap_file_stats(sock, ctx, skipped);
989 poll(&rx_poll, 1, -1);
990 poll_error_maybe_die(sock, &rx_poll);
993 bug_on(gettimeofday(&end, NULL));
994 diff = tv_subtract(end, start);
996 if (dump_to_pcap(ctx)) {
997 if (ctx->dump_dir)
998 finish_multi_pcap_file(ctx, fd);
999 else
1000 finish_single_pcap_file(ctx, fd);
1003 if (!(ctx->dump_dir && ctx->print_mode == PRINT_NONE)) {
1004 sock_print_net_stats(sock, skipped);
1006 printf("\r%12lu sec, %lu usec in total\n",
1007 diff.tv_sec, diff.tv_usec);
1008 } else {
1009 printf("\n\n");
1010 fflush(stdout);
1013 bpf_release(&bpf_ops);
1014 dissector_cleanup_all();
1015 destroy_rx_ring(sock, &rx_ring);
1017 if (ctx->promiscuous)
1018 leave_promiscuous_mode(ctx->device_in, ifflags);
1020 if (ctx->rfraw)
1021 leave_rfmon_mac80211(ctx->device_trans, ctx->device_in);
1023 close(sock);
1026 static void help(void)
1028 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING);
1029 puts("http://www.netsniff-ng.org\n\n"
1030 "Usage: netsniff-ng [options]\n"
1031 "Options:\n"
1032 " -i|-d|--dev|--in <dev|pcap> Input source as netdev or pcap\n"
1033 " -o|--out <dev|pcap|dir|txf> Output sink as netdev, pcap, directory, txf file\n"
1034 " -f|--filter <bpf-file> Use BPF filter file from bpfc\n"
1035 " -t|--type <type> Only handle packets of defined type:\n"
1036 " host|broadcast|multicast|others|outgoing\n"
1037 " -F|--interval <size/time> Dump interval in time or size if -o is a directory\n"
1038 " pcap swap spec: <num>KiB/MiB/GiB/s/sec/min/hrs\n"
1039 " -J|--jumbo-support Support for 64KB Super Jumbo Frames\n"
1040 " Default RX/TX slot: 2048Byte\n"
1041 " -R|--rfraw Capture or inject raw 802.11 frames\n"
1042 " -n|--num <uint> Number of packets until exit\n"
1043 " `-- 0 Loop until interrupted (default)\n"
1044 " `- n Send n packets and done\n"
1045 "Options for printing:\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 "Options, advanced:\n"
1051 " -P|--prefix <name> Prefix for pcaps stored in directory\n"
1052 " -r|--rand Randomize packet forwarding order\n"
1053 " -M|--no-promisc No promiscuous mode for netdev\n"
1054 " -A|--no-sock-mem Don't tune core socket memory\n"
1055 " -m|--mmap Mmap pcap file i.e., for replaying\n"
1056 " -G|--sg Scatter/gather pcap file I/O\n"
1057 " -c|--clrw Use slower read(2)/write(2) I/O\n"
1058 " -S|--ring-size <size> Manually set ring size to <size>:\n"
1059 " mmap space in KiB/MiB/GiB, e.g. \'10MiB\'\n"
1060 " -k|--kernel-pull <uint> Kernel pull from user interval in us\n"
1061 " Default is 10us where the TX_RING\n"
1062 " is populated with payload from uspace\n"
1063 " -b|--bind-cpu <cpu> Bind to specific CPU (or CPU-range)\n"
1064 " -B|--unbind-cpu <cpu> Forbid to use specific CPU (or CPU-range)\n"
1065 " -u|--user <userid> Drop privileges and change to userid\n"
1066 " -g|--group <groupid> Drop privileges and change to groupid\n"
1067 " -H|--prio-high Make this high priority process\n"
1068 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
1069 " -V|--verbose Be more verbose\n"
1070 " -v|--version Show version\n"
1071 " -h|--help Guess what?!\n\n"
1072 "Examples:\n"
1073 " netsniff-ng --in eth0 --out dump.pcap --silent --bind-cpu 0\n"
1074 " netsniff-ng --in wlan0 --rfraw --out dump.pcap --silent --bind-cpu 0\n"
1075 " netsniff-ng --in dump.pcap --mmap --out eth0 -k1000 --silent --bind-cpu 0\n"
1076 " netsniff-ng --in dump.pcap --out dump.txf --silent --bind-cpu 0\n"
1077 " netsniff-ng --in eth0 --out eth1 --silent --bind-cpu 0 --type host\n"
1078 " netsniff-ng --in eth1 --out /opt/probe/ -s -m -J --interval 100MiB -b 0\n"
1079 " netsniff-ng --in vlan0 --out dump.pcap -c -u `id -u bob` -g `id -g bob`\n"
1080 " netsniff-ng --in any --filter http.bpf --jumbo-support --ascii -V\n\n"
1081 "Note:\n"
1082 " This tool is targeted for network developers! You should\n"
1083 " be aware of what you are doing and what these options above\n"
1084 " mean! Use netsniff-ng's bpfc compiler for generating filter files.\n"
1085 " Further, netsniff-ng automatically enables the kernel BPF JIT\n"
1086 " if present. Txf file output is only possible if the input source\n"
1087 " is a pcap file.\n\n"
1088 "Please report bugs to <bugs@netsniff-ng.org>\n"
1089 "Copyright (C) 2009-2013 Daniel Borkmann <daniel@netsniff-ng.org>\n"
1090 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel@netsniff-ng.org>\n"
1091 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1092 "License: GNU GPL version 2.0\n"
1093 "This is free software: you are free to change and redistribute it.\n"
1094 "There is NO WARRANTY, to the extent permitted by law.\n");
1095 die();
1098 static void version(void)
1100 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING);
1101 puts("http://www.netsniff-ng.org\n\n"
1102 "Please report bugs to <bugs@netsniff-ng.org>\n"
1103 "Copyright (C) 2009-2013 Daniel Borkmann <daniel@netsniff-ng.org>\n"
1104 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel@netsniff-ng.org>\n"
1105 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1106 "License: GNU GPL version 2.0\n"
1107 "This is free software: you are free to change and redistribute it.\n"
1108 "There is NO WARRANTY, to the extent permitted by law.\n");
1109 die();
1112 static void header(void)
1114 printf("%s%s%s\n", colorize_start(bold), "netsniff-ng " VERSION_STRING, colorize_end());
1117 int main(int argc, char **argv)
1119 char *ptr;
1120 int c, i, j, opt_index, ops_touched = 0, vals[4] = {0};
1121 bool prio_high = false, setsockmem = true;
1122 void (*main_loop)(struct ctx *ctx) = NULL;
1123 struct ctx ctx = {
1124 .link_type = LINKTYPE_EN10MB,
1125 .print_mode = PRINT_NORM,
1126 .cpu = -1,
1127 .packet_type = -1,
1128 .promiscuous = true,
1129 .randomize = false,
1130 .pcap = PCAP_OPS_SG,
1131 .dump_interval = 60,
1132 .dump_mode = DUMP_INTERVAL_TIME,
1133 .uid = getuid(),
1134 .gid = getgid()
1137 srand(time(NULL));
1139 while ((c = getopt_long(argc, argv, short_options, long_options,
1140 &opt_index)) != EOF) {
1141 switch (c) {
1142 case 'd':
1143 case 'i':
1144 ctx.device_in = xstrdup(optarg);
1145 break;
1146 case 'o':
1147 ctx.device_out = xstrdup(optarg);
1148 break;
1149 case 'P':
1150 ctx.prefix = xstrdup(optarg);
1151 break;
1152 case 'R':
1153 ctx.link_type = LINKTYPE_IEEE802_11;
1154 ctx.rfraw = 1;
1155 break;
1156 case 'r':
1157 ctx.randomize = true;
1158 break;
1159 case 'J':
1160 ctx.jumbo_support = 1;
1161 break;
1162 case 'f':
1163 ctx.filter = xstrdup(optarg);
1164 break;
1165 case 'M':
1166 ctx.promiscuous = false;
1167 break;
1168 case 'A':
1169 setsockmem = false;
1170 break;
1171 case 'u':
1172 ctx.uid = strtoul(optarg, NULL, 0);
1173 ctx.enforce = true;
1174 break;
1175 case 'g':
1176 ctx.gid = strtoul(optarg, NULL, 0);
1177 ctx.enforce = true;
1178 break;
1179 case 't':
1180 if (!strncmp(optarg, "host", strlen("host")))
1181 ctx.packet_type = PACKET_HOST;
1182 else if (!strncmp(optarg, "broadcast", strlen("broadcast")))
1183 ctx.packet_type = PACKET_BROADCAST;
1184 else if (!strncmp(optarg, "multicast", strlen("multicast")))
1185 ctx.packet_type = PACKET_MULTICAST;
1186 else if (!strncmp(optarg, "others", strlen("others")))
1187 ctx.packet_type = PACKET_OTHERHOST;
1188 else if (!strncmp(optarg, "outgoing", strlen("outgoing")))
1189 ctx.packet_type = PACKET_OUTGOING;
1190 else
1191 ctx.packet_type = -1;
1192 break;
1193 case 'S':
1194 ptr = optarg;
1195 ctx.reserve_size = 0;
1197 for (j = i = strlen(optarg); i > 0; --i) {
1198 if (!isdigit(optarg[j - i]))
1199 break;
1200 ptr++;
1203 if (!strncmp(ptr, "KiB", strlen("KiB")))
1204 ctx.reserve_size = 1 << 10;
1205 else if (!strncmp(ptr, "MiB", strlen("MiB")))
1206 ctx.reserve_size = 1 << 20;
1207 else if (!strncmp(ptr, "GiB", strlen("GiB")))
1208 ctx.reserve_size = 1 << 30;
1209 else
1210 panic("Syntax error in ring size param!\n");
1211 *ptr = 0;
1213 ctx.reserve_size *= strtol(optarg, NULL, 0);
1214 break;
1215 case 'b':
1216 set_cpu_affinity(optarg, 0);
1217 /* Take the first CPU for rebinding the IRQ */
1218 if (ctx.cpu != -2)
1219 ctx.cpu = strtol(optarg, NULL, 0);
1220 break;
1221 case 'B':
1222 set_cpu_affinity(optarg, 1);
1223 break;
1224 case 'H':
1225 prio_high = true;
1226 break;
1227 case 'c':
1228 ctx.pcap = PCAP_OPS_RW;
1229 ops_touched = 1;
1230 break;
1231 case 'm':
1232 ctx.pcap = PCAP_OPS_MMAP;
1233 ops_touched = 1;
1234 break;
1235 case 'G':
1236 ctx.pcap = PCAP_OPS_SG;
1237 ops_touched = 1;
1238 break;
1239 case 'Q':
1240 ctx.cpu = -2;
1241 break;
1242 case 's':
1243 ctx.print_mode = PRINT_NONE;
1244 break;
1245 case 'q':
1246 ctx.print_mode = PRINT_LESS;
1247 break;
1248 case 'X':
1249 ctx.print_mode =
1250 (ctx.print_mode == PRINT_ASCII) ?
1251 PRINT_HEX_ASCII : PRINT_HEX;
1252 break;
1253 case 'l':
1254 ctx.print_mode =
1255 (ctx.print_mode == PRINT_HEX) ?
1256 PRINT_HEX_ASCII : PRINT_ASCII;
1257 break;
1258 case 'k':
1259 ctx.kpull = strtol(optarg, NULL, 0);
1260 break;
1261 case 'n':
1262 frame_count_max = strtol(optarg, NULL, 0);
1263 break;
1264 case 'F':
1265 ptr = optarg;
1266 ctx.dump_interval = 0;
1268 for (j = i = strlen(optarg); i > 0; --i) {
1269 if (!isdigit(optarg[j - i]))
1270 break;
1271 ptr++;
1274 if (!strncmp(ptr, "KiB", strlen("KiB"))) {
1275 ctx.dump_interval = 1 << 10;
1276 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1277 } else if (!strncmp(ptr, "MiB", strlen("MiB"))) {
1278 ctx.dump_interval = 1 << 20;
1279 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1280 } else if (!strncmp(ptr, "GiB", strlen("GiB"))) {
1281 ctx.dump_interval = 1 << 30;
1282 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1283 } else if (!strncmp(ptr, "sec", strlen("sec"))) {
1284 ctx.dump_interval = 1;
1285 ctx.dump_mode = DUMP_INTERVAL_TIME;
1286 } else if (!strncmp(ptr, "min", strlen("min"))) {
1287 ctx.dump_interval = 60;
1288 ctx.dump_mode = DUMP_INTERVAL_TIME;
1289 } else if (!strncmp(ptr, "hrs", strlen("hrs"))) {
1290 ctx.dump_interval = 60 * 60;
1291 ctx.dump_mode = DUMP_INTERVAL_TIME;
1292 } else if (!strncmp(ptr, "s", strlen("s"))) {
1293 ctx.dump_interval = 1;
1294 ctx.dump_mode = DUMP_INTERVAL_TIME;
1295 } else {
1296 panic("Syntax error in time/size param!\n");
1299 *ptr = 0;
1300 ctx.dump_interval *= strtol(optarg, NULL, 0);
1301 break;
1302 case 'V':
1303 ctx.verbose = 1;
1304 break;
1305 case 'v':
1306 version();
1307 break;
1308 case 'h':
1309 help();
1310 break;
1311 case '?':
1312 switch (optopt) {
1313 case 'd':
1314 case 'i':
1315 case 'o':
1316 case 'f':
1317 case 't':
1318 case 'P':
1319 case 'F':
1320 case 'n':
1321 case 'S':
1322 case 'b':
1323 case 'k':
1324 case 'u':
1325 case 'g':
1326 case 'B':
1327 case 'e':
1328 panic("Option -%c requires an argument!\n",
1329 optopt);
1330 default:
1331 if (isprint(optopt))
1332 whine("Unknown option character "
1333 "`0x%X\'!\n", optopt);
1334 die();
1336 default:
1337 break;
1341 if (!ctx.device_in)
1342 ctx.device_in = xstrdup("any");
1344 register_signal(SIGINT, signal_handler);
1345 register_signal(SIGHUP, signal_handler);
1347 header();
1349 tprintf_init();
1351 if (prio_high) {
1352 set_proc_prio(get_default_proc_prio());
1353 set_sched_status(get_default_sched_policy(), get_default_sched_prio());
1356 if (ctx.device_in && (device_mtu(ctx.device_in) ||
1357 !strncmp("any", ctx.device_in, strlen(ctx.device_in)))) {
1358 if (!ctx.device_out) {
1359 ctx.dump = 0;
1360 main_loop = recv_only_or_dump;
1361 } else if (device_mtu(ctx.device_out)) {
1362 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1363 main_loop = receive_to_xmit;
1364 } else {
1365 ctx.dump = 1;
1366 register_signal_f(SIGALRM, timer_next_dump, SA_SIGINFO);
1367 main_loop = recv_only_or_dump;
1368 if (!ops_touched)
1369 ctx.pcap = PCAP_OPS_SG;
1371 } else {
1372 if (ctx.device_out && device_mtu(ctx.device_out)) {
1373 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1374 main_loop = pcap_to_xmit;
1375 if (!ops_touched)
1376 ctx.pcap = PCAP_OPS_MMAP;
1377 } else {
1378 main_loop = read_pcap;
1379 if (!ops_touched)
1380 ctx.pcap = PCAP_OPS_SG;
1384 bug_on(!main_loop);
1386 init_pcap(ctx.pcap, ctx.jumbo_support);
1388 if (setsockmem)
1389 set_system_socket_memory(vals);
1390 xlockme();
1392 main_loop(&ctx);
1394 xunlockme();
1395 if (setsockmem)
1396 reset_system_socket_memory(vals);
1398 tprintf_cleanup();
1399 cleanup_pcap();
1401 free(ctx.device_in);
1402 free(ctx.device_out);
1403 free(ctx.device_trans);
1404 free(ctx.prefix);
1406 return 0;