5499134f65dda2564c85b23f69fa893ccee7ae64
[netsniff-ng.git] / netsniff-ng.c
blob5499134f65dda2564c85b23f69fa893ccee7ae64
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2009-2013 Daniel Borkmann.
5 * Copyright 2010 Emmanuel Roullit.
6 * Subject to the GPL, version 2.
8 * The first sniffer that invoked both, the zero-copy RX_RING as well as
9 * the zero-copy TX_RING for high-performance network I/O and scatter/gather
10 * or mmaped PCAP I/O.
12 * "I knew that danger lay ahead, of course; but I did not expect to
13 * meet it in our own Shire. Can't a hobbit walk from the Water to the
14 * River in peace?" "But it is not your own Shire," said Gildor. "Others
15 * dwelt here before hobbits were; and others will dwell here again when
16 * hobbits are no more. The wide world is all about you: you can fence
17 * yourselves in, but you cannot for ever fence it out."
19 * -- The Lord of the Rings, Gildor to Frodo,
20 * Chapter 'Three is Company'.
23 #define _GNU_SOURCE
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <signal.h>
27 #include <getopt.h>
28 #include <ctype.h>
29 #include <time.h>
30 #include <string.h>
31 #include <sys/socket.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <sys/time.h>
35 #include <sys/fsuid.h>
36 #include <unistd.h>
37 #include <stdbool.h>
38 #include <pthread.h>
39 #include <fcntl.h>
41 #include "ring_rx.h"
42 #include "ring_tx.h"
43 #include "mac80211.h"
44 #include "xutils.h"
45 #include "built_in.h"
46 #include "pcap.h"
47 #include "bpf.h"
48 #include "xio.h"
49 #include "die.h"
50 #include "tprintf.h"
51 #include "dissector.h"
52 #include "xmalloc.h"
54 enum dump_mode {
55 DUMP_INTERVAL_TIME,
56 DUMP_INTERVAL_SIZE,
59 struct ctx {
60 char *device_in, *device_out, *device_trans, *filter, *prefix;
61 int cpu, rfraw, dump, print_mode, dump_dir, packet_type, verbose;
62 unsigned long kpull, dump_interval, reserve_size, tx_bytes, tx_packets;
63 bool randomize, promiscuous, enforce, jumbo;
64 enum pcap_ops_groups pcap; enum dump_mode dump_mode;
65 uid_t uid; gid_t gid; uint32_t link_type, magic;
68 volatile sig_atomic_t sigint = 0;
70 static volatile bool next_dump = false;
72 static const char *short_options = "d:i:o:rf:MJt:S:k:n:b:HQmcsqXlvhF:RGAP:Vu:g:T:D";
73 static const struct option long_options[] = {
74 {"dev", required_argument, NULL, 'd'},
75 {"in", required_argument, NULL, 'i'},
76 {"out", required_argument, NULL, 'o'},
77 {"filter", required_argument, NULL, 'f'},
78 {"num", required_argument, NULL, 'n'},
79 {"type", required_argument, NULL, 't'},
80 {"interval", required_argument, NULL, 'F'},
81 {"ring-size", required_argument, NULL, 'S'},
82 {"kernel-pull", required_argument, NULL, 'k'},
83 {"bind-cpu", required_argument, NULL, 'b'},
84 {"prefix", required_argument, NULL, 'P'},
85 {"user", required_argument, NULL, 'u'},
86 {"group", required_argument, NULL, 'g'},
87 {"magic", required_argument, NULL, 'T'},
88 {"rand", no_argument, NULL, 'r'},
89 {"rfraw", no_argument, NULL, 'R'},
90 {"mmap", no_argument, NULL, 'm'},
91 {"sg", no_argument, NULL, 'G'},
92 {"clrw", no_argument, NULL, 'c'},
93 {"jumbo-support", no_argument, NULL, 'J'},
94 {"no-promisc", no_argument, NULL, 'M'},
95 {"prio-high", no_argument, NULL, 'H'},
96 {"notouch-irq", no_argument, NULL, 'Q'},
97 {"dump-pcap-types", no_argument, NULL, 'D'},
98 {"silent", no_argument, NULL, 's'},
99 {"less", no_argument, NULL, 'q'},
100 {"hex", no_argument, NULL, 'X'},
101 {"ascii", no_argument, NULL, 'l'},
102 {"no-sock-mem", no_argument, NULL, 'A'},
103 {"verbose", no_argument, NULL, 'V'},
104 {"version", no_argument, NULL, 'v'},
105 {"help", no_argument, NULL, 'h'},
106 {NULL, 0, NULL, 0}
109 static int tx_sock;
111 static struct itimerval itimer;
113 static unsigned long frame_count_max = 0, interval = TX_KERNEL_PULL_INT;
115 #define __pcap_io pcap_ops[ctx->pcap]
117 static void signal_handler(int number)
119 switch (number) {
120 case SIGINT:
121 sigint = 1;
122 case SIGHUP:
123 default:
124 break;
128 static void timer_elapsed(int unused)
130 itimer.it_interval.tv_sec = 0;
131 itimer.it_interval.tv_usec = interval;
133 itimer.it_value.tv_sec = 0;
134 itimer.it_value.tv_usec = interval;
136 pull_and_flush_tx_ring(tx_sock);
137 setitimer(ITIMER_REAL, &itimer, NULL);
140 static void timer_next_dump(int unused)
142 itimer.it_interval.tv_sec = interval;
143 itimer.it_interval.tv_usec = 0;
145 itimer.it_value.tv_sec = interval;
146 itimer.it_value.tv_usec = 0;
148 next_dump = true;
149 setitimer(ITIMER_REAL, &itimer, NULL);
152 static inline bool dump_to_pcap(struct ctx *ctx)
154 return ctx->dump;
157 static void pcap_to_xmit(struct ctx *ctx)
159 __label__ out;
160 uint8_t *out = NULL;
161 int irq, ifindex, fd = 0, ret;
162 unsigned int size, it = 0;
163 unsigned long trunced = 0;
164 struct ring tx_ring;
165 struct frame_map *hdr;
166 struct sock_fprog bpf_ops;
167 struct timeval start, end, diff;
168 pcap_pkthdr_t phdr;
170 if (!device_up_and_running(ctx->device_out) && !ctx->rfraw)
171 panic("Device not up and running!\n");
173 bug_on(!__pcap_io);
175 tx_sock = pf_socket();
177 if (!strncmp("-", ctx->device_in, strlen("-"))) {
178 fd = dup(fileno(stdin));
179 close(fileno(stdin));
180 if (ctx->pcap == PCAP_OPS_MM)
181 ctx->pcap = PCAP_OPS_SG;
182 } else {
183 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
186 ret = __pcap_io->pull_fhdr_pcap(fd, &ctx->magic, &ctx->link_type);
187 if (ret)
188 panic("Error reading pcap header!\n");
190 if (__pcap_io->prepare_access_pcap) {
191 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_RD, ctx->jumbo);
192 if (ret)
193 panic("Error prepare reading pcap!\n");
196 fmemset(&tx_ring, 0, sizeof(tx_ring));
197 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
199 if (ctx->rfraw) {
200 ctx->device_trans = xstrdup(ctx->device_out);
201 xfree(ctx->device_out);
203 enter_rfmon_mac80211(ctx->device_trans, &ctx->device_out);
204 if (ctx->link_type != LINKTYPE_IEEE802_11)
205 panic("Wrong linktype of pcap!\n");
208 ifindex = device_ifindex(ctx->device_out);
210 size = ring_size(ctx->device_out, ctx->reserve_size);
212 bpf_parse_rules(ctx->device_out, ctx->filter, &bpf_ops);
214 set_packet_loss_discard(tx_sock);
215 set_sockopt_hwtimestamp(tx_sock, ctx->device_out);
217 setup_tx_ring_layout(tx_sock, &tx_ring, size, ctx->jumbo);
218 create_tx_ring(tx_sock, &tx_ring, ctx->verbose);
219 mmap_tx_ring(tx_sock, &tx_ring);
220 alloc_tx_ring_frames(&tx_ring);
221 bind_tx_ring(tx_sock, &tx_ring, ifindex);
223 dissector_init_all(ctx->print_mode);
225 if (ctx->cpu >= 0 && ifindex > 0) {
226 irq = device_irq_number(ctx->device_out);
227 device_bind_irq_to_cpu(irq, ctx->cpu);
229 if (ctx->verbose)
230 printf("IRQ: %s:%d > CPU%d\n",
231 ctx->device_out, irq, ctx->cpu);
234 if (ctx->kpull)
235 interval = ctx->kpull;
237 if (ctx->verbose) {
238 printf("BPF:\n");
239 bpf_dump_all(&bpf_ops);
241 printf("MD: TX %luus %s ", interval, pcap_ops_group_to_str[ctx->pcap]);
242 if (ctx->rfraw)
243 printf("802.11 raw via %s ", ctx->device_out);
244 #ifdef _LARGEFILE64_SOURCE
245 printf("lf64 ");
246 #endif
247 ioprio_print();
248 printf("\n");
251 itimer.it_interval.tv_sec = 0;
252 itimer.it_interval.tv_usec = interval;
254 itimer.it_value.tv_sec = 0;
255 itimer.it_value.tv_usec = interval;
257 setitimer(ITIMER_REAL, &itimer, NULL);
259 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
261 printf("Running! Hang up with ^C!\n\n");
262 fflush(stdout);
264 bug_on(gettimeofday(&start, NULL));
266 while (likely(sigint == 0)) {
267 while (user_may_pull_from_tx(tx_ring.frames[it].iov_base)) {
268 hdr = tx_ring.frames[it].iov_base;
269 out = ((uint8_t *) hdr) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
271 do {
272 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic, out,
273 ring_frame_size(&tx_ring));
274 if (unlikely(ret <= 0))
275 goto out;
277 if (ring_frame_size(&tx_ring) <
278 pcap_get_length(&phdr, ctx->magic)) {
279 pcap_set_length(&phdr, ctx->magic,
280 ring_frame_size(&tx_ring));
281 trunced++;
283 } while (ctx->filter &&
284 !bpf_run_filter(&bpf_ops, out,
285 pcap_get_length(&phdr, ctx->magic)));
287 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &hdr->tp_h, &hdr->s_ll);
289 ctx->tx_bytes += hdr->tp_h.tp_len;;
290 ctx->tx_packets++;
292 show_frame_hdr(hdr, ctx->print_mode);
294 dissector_entry_point(out, hdr->tp_h.tp_snaplen,
295 ctx->link_type, ctx->print_mode);
297 kernel_may_pull_from_tx(&hdr->tp_h);
299 it++;
300 if (it >= tx_ring.layout.tp_frame_nr)
301 it = 0;
303 if (unlikely(sigint == 1))
304 break;
306 if (frame_count_max != 0) {
307 if (ctx->tx_packets >= frame_count_max) {
308 sigint = 1;
309 break;
315 out:
317 bug_on(gettimeofday(&end, NULL));
318 diff = tv_subtract(end, start);
320 bpf_release(&bpf_ops);
322 dissector_cleanup_all();
323 destroy_tx_ring(tx_sock, &tx_ring);
325 if (ctx->rfraw)
326 leave_rfmon_mac80211(ctx->device_trans, ctx->device_out);
328 if (__pcap_io->prepare_close_pcap)
329 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
331 if (strncmp("-", ctx->device_in, strlen("-")))
332 close(fd);
333 else
334 dup2(fd, fileno(stdin));
336 close(tx_sock);
338 fflush(stdout);
339 printf("\n");
340 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
341 printf("\r%12lu packets truncated in file\n", trunced);
342 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
343 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
346 static void receive_to_xmit(struct ctx *ctx)
348 short ifflags = 0;
349 uint8_t *in, *out;
350 int rx_sock, ifindex_in, ifindex_out;
351 unsigned int size_in, size_out, it_in = 0, it_out = 0;
352 unsigned long frame_count = 0;
353 struct frame_map *hdr_in, *hdr_out;
354 struct ring tx_ring, rx_ring;
355 struct pollfd rx_poll;
356 struct sock_fprog bpf_ops;
358 if (!strncmp(ctx->device_in, ctx->device_out, IFNAMSIZ))
359 panic("Ingress/egress devices must be different!\n");
360 if (!device_up_and_running(ctx->device_out))
361 panic("Egress device not up and running!\n");
362 if (!device_up_and_running(ctx->device_in))
363 panic("Ingress device not up and running!\n");
365 rx_sock = pf_socket();
366 tx_sock = pf_socket();
368 fmemset(&tx_ring, 0, sizeof(tx_ring));
369 fmemset(&rx_ring, 0, sizeof(rx_ring));
370 fmemset(&rx_poll, 0, sizeof(rx_poll));
371 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
373 ifindex_in = device_ifindex(ctx->device_in);
374 ifindex_out = device_ifindex(ctx->device_out);
376 size_in = ring_size(ctx->device_in, ctx->reserve_size);
377 size_out = ring_size(ctx->device_out, ctx->reserve_size);
379 enable_kernel_bpf_jit_compiler();
381 bpf_parse_rules(ctx->device_in, ctx->filter, &bpf_ops);
382 bpf_attach_to_sock(rx_sock, &bpf_ops);
384 setup_rx_ring_layout(rx_sock, &rx_ring, size_in, ctx->jumbo);
385 create_rx_ring(rx_sock, &rx_ring, ctx->verbose);
386 mmap_rx_ring(rx_sock, &rx_ring);
387 alloc_rx_ring_frames(&rx_ring);
388 bind_rx_ring(rx_sock, &rx_ring, ifindex_in);
389 prepare_polling(rx_sock, &rx_poll);
391 set_packet_loss_discard(tx_sock);
392 setup_tx_ring_layout(tx_sock, &tx_ring, size_out, ctx->jumbo);
393 create_tx_ring(tx_sock, &tx_ring, ctx->verbose);
394 mmap_tx_ring(tx_sock, &tx_ring);
395 alloc_tx_ring_frames(&tx_ring);
396 bind_tx_ring(tx_sock, &tx_ring, ifindex_out);
398 dissector_init_all(ctx->print_mode);
400 if (ctx->promiscuous)
401 ifflags = enter_promiscuous_mode(ctx->device_in);
403 if (ctx->kpull)
404 interval = ctx->kpull;
406 itimer.it_interval.tv_sec = 0;
407 itimer.it_interval.tv_usec = interval;
409 itimer.it_value.tv_sec = 0;
410 itimer.it_value.tv_usec = interval;
412 setitimer(ITIMER_REAL, &itimer, NULL);
414 if (ctx->verbose) {
415 printf("BPF:\n");
416 bpf_dump_all(&bpf_ops);
418 printf("MD: RXTX %luus\n\n", interval);
421 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
423 printf("Running! Hang up with ^C!\n\n");
424 fflush(stdout);
426 while (likely(sigint == 0)) {
427 while (user_may_pull_from_rx(rx_ring.frames[it_in].iov_base)) {
428 __label__ next;
430 hdr_in = rx_ring.frames[it_in].iov_base;
431 in = ((uint8_t *) hdr_in) + hdr_in->tp_h.tp_mac;
433 frame_count++;
435 if (ctx->packet_type != -1)
436 if (ctx->packet_type != hdr_in->s_ll.sll_pkttype)
437 goto next;
439 hdr_out = tx_ring.frames[it_out].iov_base;
440 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
442 for (; !user_may_pull_from_tx(tx_ring.frames[it_out].iov_base) &&
443 likely(!sigint);) {
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 hdr_out = tx_ring.frames[it_out].iov_base;
453 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
456 tpacket_hdr_clone(&hdr_out->tp_h, &hdr_in->tp_h);
457 fmemcpy(out, in, hdr_in->tp_h.tp_len);
459 kernel_may_pull_from_tx(&hdr_out->tp_h);
460 if (ctx->randomize)
461 next_rnd_slot(&it_out, &tx_ring);
462 else {
463 it_out++;
464 if (it_out >= tx_ring.layout.tp_frame_nr)
465 it_out = 0;
468 show_frame_hdr(hdr_in, ctx->print_mode);
470 dissector_entry_point(in, hdr_in->tp_h.tp_snaplen,
471 ctx->link_type, ctx->print_mode);
473 if (frame_count_max != 0) {
474 if (frame_count >= frame_count_max) {
475 sigint = 1;
476 break;
480 next:
482 kernel_may_pull_from_rx(&hdr_in->tp_h);
484 it_in++;
485 if (it_in >= rx_ring.layout.tp_frame_nr)
486 it_in = 0;
488 if (unlikely(sigint == 1))
489 goto out;
492 poll(&rx_poll, 1, -1);
493 poll_error_maybe_die(rx_sock, &rx_poll);
496 out:
498 sock_print_net_stats(rx_sock, 0);
500 bpf_release(&bpf_ops);
502 dissector_cleanup_all();
504 destroy_tx_ring(tx_sock, &tx_ring);
505 destroy_rx_ring(rx_sock, &rx_ring);
507 if (ctx->promiscuous)
508 leave_promiscuous_mode(ctx->device_in, ifflags);
510 close(tx_sock);
511 close(rx_sock);
514 static void translate_pcap_to_txf(int fdo, uint8_t *out, size_t len)
516 size_t bytes_done = 0;
517 char bout[80];
519 slprintf(bout, sizeof(bout), "{\n ");
520 write_or_die(fdo, bout, strlen(bout));
522 while (bytes_done < len) {
523 slprintf(bout, sizeof(bout), "0x%02x, ", out[bytes_done]);
524 write_or_die(fdo, bout, strlen(bout));
526 bytes_done++;
528 if (bytes_done % 10 == 0) {
529 slprintf(bout, sizeof(bout), "\n");
530 write_or_die(fdo, bout, strlen(bout));
532 if (bytes_done < len) {
533 slprintf(bout, sizeof(bout), " ");
534 write_or_die(fdo, bout, strlen(bout));
538 if (bytes_done % 10 != 0) {
539 slprintf(bout, sizeof(bout), "\n");
540 write_or_die(fdo, bout, strlen(bout));
543 slprintf(bout, sizeof(bout), "}\n\n");
544 write_or_die(fdo, bout, strlen(bout));
547 static void read_pcap(struct ctx *ctx)
549 __label__ out;
550 uint8_t *out;
551 int ret, fd, fdo = 0;
552 unsigned long trunced = 0;
553 size_t out_len;
554 pcap_pkthdr_t phdr;
555 struct sock_fprog bpf_ops;
556 struct frame_map fm;
557 struct timeval start, end, diff;
558 struct sockaddr_ll sll;
560 bug_on(!__pcap_io);
562 if (!strncmp("-", ctx->device_in, strlen("-"))) {
563 fd = dup(fileno(stdin));
564 close(fileno(stdin));
565 if (ctx->pcap == PCAP_OPS_MM)
566 ctx->pcap = PCAP_OPS_SG;
567 } else {
568 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
571 ret = __pcap_io->pull_fhdr_pcap(fd, &ctx->magic, &ctx->link_type);
572 if (ret)
573 panic("Error reading pcap header!\n");
575 if (__pcap_io->prepare_access_pcap) {
576 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_RD, ctx->jumbo);
577 if (ret)
578 panic("Error prepare reading pcap!\n");
581 fmemset(&fm, 0, sizeof(fm));
582 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
584 bpf_parse_rules("any", ctx->filter, &bpf_ops);
586 dissector_init_all(ctx->print_mode);
588 out_len = round_up(1024 * 1024, PAGE_SIZE);
589 out = xmalloc_aligned(out_len, CO_CACHE_LINE_SIZE);
591 if (ctx->verbose) {
592 printf("BPF:\n");
593 bpf_dump_all(&bpf_ops);
595 printf("MD: RD %s ", pcap_ops_group_to_str[ctx->pcap]);
596 #ifdef _LARGEFILE64_SOURCE
597 printf("lf64 ");
598 #endif
599 ioprio_print();
600 printf("\n");
603 if (ctx->device_out) {
604 if (!strncmp("-", ctx->device_out, strlen("-"))) {
605 fdo = dup(fileno(stdout));
606 close(fileno(stdout));
607 } else {
608 fdo = open_or_die_m(ctx->device_out, O_RDWR | O_CREAT |
609 O_TRUNC | O_LARGEFILE, DEFFILEMODE);
613 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
615 printf("Running! Hang up with ^C!\n\n");
616 fflush(stdout);
618 bug_on(gettimeofday(&start, NULL));
620 while (likely(sigint == 0)) {
621 do {
622 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic,
623 out, out_len);
624 if (unlikely(ret < 0))
625 goto out;
627 if (unlikely(pcap_get_length(&phdr, ctx->magic) == 0)) {
628 trunced++;
629 continue;
632 if (unlikely(pcap_get_length(&phdr, ctx->magic) > out_len)) {
633 pcap_set_length(&phdr, ctx->magic, out_len);
634 trunced++;
636 } while (ctx->filter &&
637 !bpf_run_filter(&bpf_ops, out,
638 pcap_get_length(&phdr, ctx->magic)));
640 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &fm.tp_h, &sll);
642 ctx->tx_bytes += fm.tp_h.tp_len;
643 ctx->tx_packets++;
645 show_frame_hdr(&fm, ctx->print_mode);
647 dissector_entry_point(out, fm.tp_h.tp_snaplen,
648 ctx->link_type, ctx->print_mode);
650 if (ctx->device_out)
651 translate_pcap_to_txf(fdo, out, fm.tp_h.tp_snaplen);
653 if (frame_count_max != 0) {
654 if (ctx->tx_packets >= frame_count_max) {
655 sigint = 1;
656 break;
661 out:
663 bug_on(gettimeofday(&end, NULL));
664 diff = tv_subtract(end, start);
666 bpf_release(&bpf_ops);
668 dissector_cleanup_all();
670 if (__pcap_io->prepare_close_pcap)
671 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
673 xfree(out);
675 fflush(stdout);
676 printf("\n");
677 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
678 printf("\r%12lu packets truncated in file\n", trunced);
679 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
680 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
682 if (strncmp("-", ctx->device_in, strlen("-")))
683 close(fd);
684 else
685 dup2(fd, fileno(stdin));
687 if (ctx->device_out) {
688 if (strncmp("-", ctx->device_out, strlen("-")))
689 close(fdo);
690 else
691 dup2(fdo, fileno(stdout));
695 static void finish_multi_pcap_file(struct ctx *ctx, int fd)
697 __pcap_io->fsync_pcap(fd);
699 if (__pcap_io->prepare_close_pcap)
700 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
702 close(fd);
704 fmemset(&itimer, 0, sizeof(itimer));
705 setitimer(ITIMER_REAL, &itimer, NULL);
708 static int next_multi_pcap_file(struct ctx *ctx, int fd)
710 int ret;
711 char fname[512];
713 __pcap_io->fsync_pcap(fd);
715 if (__pcap_io->prepare_close_pcap)
716 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
718 close(fd);
720 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
721 ctx->prefix ? : "dump-", time(0));
723 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
724 O_LARGEFILE, DEFFILEMODE);
726 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
727 if (ret)
728 panic("Error writing pcap header!\n");
730 if (__pcap_io->prepare_access_pcap) {
731 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, ctx->jumbo);
732 if (ret)
733 panic("Error prepare writing pcap!\n");
736 return fd;
739 static int begin_multi_pcap_file(struct ctx *ctx)
741 int fd, ret;
742 char fname[256];
744 bug_on(!__pcap_io);
746 if (ctx->device_out[strlen(ctx->device_out) - 1] == '/')
747 ctx->device_out[strlen(ctx->device_out) - 1] = 0;
749 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
750 ctx->prefix ? : "dump-", time(0));
752 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
753 O_LARGEFILE, DEFFILEMODE);
755 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
756 if (ret)
757 panic("Error writing pcap header!\n");
759 if (__pcap_io->prepare_access_pcap) {
760 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, ctx->jumbo);
761 if (ret)
762 panic("Error prepare writing pcap!\n");
765 if (ctx->dump_mode == DUMP_INTERVAL_TIME) {
766 interval = ctx->dump_interval;
768 itimer.it_interval.tv_sec = interval;
769 itimer.it_interval.tv_usec = 0;
771 itimer.it_value.tv_sec = interval;
772 itimer.it_value.tv_usec = 0;
774 setitimer(ITIMER_REAL, &itimer, NULL);
775 } else {
776 interval = 0;
779 return fd;
782 static void finish_single_pcap_file(struct ctx *ctx, int fd)
784 __pcap_io->fsync_pcap(fd);
786 if (__pcap_io->prepare_close_pcap)
787 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
789 if (strncmp("-", ctx->device_out, strlen("-")))
790 close(fd);
791 else
792 dup2(fd, fileno(stdout));
795 static int begin_single_pcap_file(struct ctx *ctx)
797 int fd, ret;
799 bug_on(!__pcap_io);
801 if (!strncmp("-", ctx->device_out, strlen("-"))) {
802 fd = dup(fileno(stdout));
803 close(fileno(stdout));
804 if (ctx->pcap == PCAP_OPS_MM)
805 ctx->pcap = PCAP_OPS_SG;
806 } else {
807 fd = open_or_die_m(ctx->device_out,
808 O_RDWR | O_CREAT | O_TRUNC |
809 O_LARGEFILE, DEFFILEMODE);
812 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
813 if (ret)
814 panic("Error writing pcap header!\n");
816 if (__pcap_io->prepare_access_pcap) {
817 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, ctx->jumbo);
818 if (ret)
819 panic("Error prepare writing pcap!\n");
822 return fd;
825 static void print_pcap_file_stats(int sock, struct ctx *ctx, unsigned long skipped)
827 unsigned long good, bad;
828 struct tpacket_stats kstats;
829 socklen_t slen = sizeof(kstats);
831 fmemset(&kstats, 0, sizeof(kstats));
832 getsockopt(sock, SOL_PACKET, PACKET_STATISTICS, &kstats, &slen);
834 if (ctx->print_mode == PRINT_NONE) {
835 good = kstats.tp_packets - kstats.tp_drops - skipped;
836 bad = kstats.tp_drops + skipped;
838 printf(".(+%lu/-%lu)", good, bad);
839 fflush(stdout);
843 static void recv_only_or_dump(struct ctx *ctx)
845 uint8_t *packet;
846 short ifflags = 0;
847 int sock, irq, ifindex, fd = 0, ret;
848 unsigned int size, it = 0;
849 unsigned long frame_count = 0, skipped = 0;
850 struct ring rx_ring;
851 struct pollfd rx_poll;
852 struct frame_map *hdr;
853 struct sock_fprog bpf_ops;
854 struct timeval start, end, diff;
855 pcap_pkthdr_t phdr;
857 if (!device_up_and_running(ctx->device_in) && !ctx->rfraw)
858 panic("Device not up and running!\n");
860 sock = pf_socket();
862 if (ctx->rfraw) {
863 ctx->device_trans = xstrdup(ctx->device_in);
864 xfree(ctx->device_in);
866 enter_rfmon_mac80211(ctx->device_trans, &ctx->device_in);
867 ctx->link_type = LINKTYPE_IEEE802_11;
870 fmemset(&rx_ring, 0, sizeof(rx_ring));
871 fmemset(&rx_poll, 0, sizeof(rx_poll));
872 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
874 ifindex = device_ifindex(ctx->device_in);
876 size = ring_size(ctx->device_in, ctx->reserve_size);
878 enable_kernel_bpf_jit_compiler();
880 bpf_parse_rules(ctx->device_in, ctx->filter, &bpf_ops);
881 bpf_attach_to_sock(sock, &bpf_ops);
883 set_sockopt_hwtimestamp(sock, ctx->device_in);
885 setup_rx_ring_layout(sock, &rx_ring, size, ctx->jumbo);
886 create_rx_ring(sock, &rx_ring, ctx->verbose);
887 mmap_rx_ring(sock, &rx_ring);
888 alloc_rx_ring_frames(&rx_ring);
889 bind_rx_ring(sock, &rx_ring, ifindex);
891 prepare_polling(sock, &rx_poll);
892 dissector_init_all(ctx->print_mode);
894 if (ctx->cpu >= 0 && ifindex > 0) {
895 irq = device_irq_number(ctx->device_in);
896 device_bind_irq_to_cpu(irq, ctx->cpu);
898 if (ctx->verbose)
899 printf("IRQ: %s:%d > CPU%d\n",
900 ctx->device_in, irq, ctx->cpu);
903 if (ctx->promiscuous)
904 ifflags = enter_promiscuous_mode(ctx->device_in);
906 if (ctx->verbose) {
907 printf("BPF:\n");
908 bpf_dump_all(&bpf_ops);
910 printf("MD: RX %s ", ctx->dump ? pcap_ops_group_to_str[ctx->pcap] : "");
911 if (ctx->rfraw)
912 printf("802.11 raw via %s ", ctx->device_in);
913 #ifdef _LARGEFILE64_SOURCE
914 printf("lf64 ");
915 #endif
916 ioprio_print();
917 printf("\n");
920 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
922 if (dump_to_pcap(ctx)) {
923 __label__ try_file;
924 struct stat stats;
926 fmemset(&stats, 0, sizeof(stats));
927 ret = stat(ctx->device_out, &stats);
928 if (ret < 0) {
929 ctx->dump_dir = 0;
930 goto try_file;
933 ctx->dump_dir = S_ISDIR(stats.st_mode);
934 if (ctx->dump_dir) {
935 fd = begin_multi_pcap_file(ctx);
936 } else {
937 try_file:
938 fd = begin_single_pcap_file(ctx);
942 printf("Running! Hang up with ^C!\n\n");
943 fflush(stdout);
945 bug_on(gettimeofday(&start, NULL));
947 while (likely(sigint == 0)) {
948 while (user_may_pull_from_rx(rx_ring.frames[it].iov_base)) {
949 __label__ next;
951 hdr = rx_ring.frames[it].iov_base;
952 packet = ((uint8_t *) hdr) + hdr->tp_h.tp_mac;
953 frame_count++;
955 if (ctx->packet_type != -1)
956 if (ctx->packet_type != hdr->s_ll.sll_pkttype)
957 goto next;
959 if (unlikely(ring_frame_size(&rx_ring) < hdr->tp_h.tp_snaplen)) {
960 skipped++;
961 goto next;
964 if (dump_to_pcap(ctx)) {
965 tpacket_hdr_to_pcap_pkthdr(&hdr->tp_h, &hdr->s_ll, &phdr, ctx->magic);
967 ret = __pcap_io->write_pcap(fd, &phdr, ctx->magic, packet,
968 pcap_get_length(&phdr, ctx->magic));
969 if (unlikely(ret != pcap_get_total_length(&phdr, ctx->magic)))
970 panic("Write error to pcap!\n");
973 show_frame_hdr(hdr, ctx->print_mode);
975 dissector_entry_point(packet, hdr->tp_h.tp_snaplen,
976 ctx->link_type, ctx->print_mode);
978 if (frame_count_max != 0) {
979 if (frame_count >= frame_count_max) {
980 sigint = 1;
981 break;
985 next:
987 kernel_may_pull_from_rx(&hdr->tp_h);
989 it++;
990 if (it >= rx_ring.layout.tp_frame_nr)
991 it = 0;
993 if (unlikely(sigint == 1))
994 break;
996 if (dump_to_pcap(ctx)) {
997 if (ctx->dump_mode == DUMP_INTERVAL_SIZE) {
998 interval += hdr->tp_h.tp_snaplen;
1000 if (interval > ctx->dump_interval) {
1001 next_dump = true;
1002 interval = 0;
1006 if (next_dump) {
1007 fd = next_multi_pcap_file(ctx, fd);
1008 next_dump = false;
1010 if (ctx->verbose)
1011 print_pcap_file_stats(sock, ctx, skipped);
1016 poll(&rx_poll, 1, -1);
1017 poll_error_maybe_die(sock, &rx_poll);
1020 bug_on(gettimeofday(&end, NULL));
1021 diff = tv_subtract(end, start);
1023 if (!(ctx->dump_dir && ctx->print_mode == PRINT_NONE)) {
1024 sock_print_net_stats(sock, skipped);
1026 printf("\r%12lu sec, %lu usec in total\n",
1027 diff.tv_sec, diff.tv_usec);
1028 } else {
1029 printf("\n\n");
1030 fflush(stdout);
1033 bpf_release(&bpf_ops);
1034 dissector_cleanup_all();
1035 destroy_rx_ring(sock, &rx_ring);
1037 if (ctx->promiscuous)
1038 leave_promiscuous_mode(ctx->device_in, ifflags);
1040 if (ctx->rfraw)
1041 leave_rfmon_mac80211(ctx->device_trans, ctx->device_in);
1043 if (dump_to_pcap(ctx)) {
1044 if (ctx->dump_dir)
1045 finish_multi_pcap_file(ctx, fd);
1046 else
1047 finish_single_pcap_file(ctx, fd);
1050 close(sock);
1053 static void help(void)
1055 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING);
1056 puts("http://www.netsniff-ng.org\n\n"
1057 "Usage: netsniff-ng [options] [filter-expression]\n"
1058 "Options:\n"
1059 " -i|-d|--dev|--in <dev|pcap|-> Input source as netdev, pcap or pcap stdin\n"
1060 " -o|--out <dev|pcap|dir|cfg|-> Output sink as netdev, pcap, directory, trafgen, or pcap stdout\n"
1061 " -f|--filter <bpf-file|expr> Use BPF filter file from bpfc or tcpdump-like expression\n"
1062 " -t|--type <type> Filter for: host|broadcast|multicast|others|outgoing\n"
1063 " -F|--interval <size|time> Dump interval if -o is a dir: <num>KiB/MiB/GiB/s/sec/min/hrs\n"
1064 " -J|--jumbo-support Support for 64KB Super Jumbo Frames (def: 2048B)\n"
1065 " -R|--rfraw Capture or inject raw 802.11 frames\n"
1066 " -n|--num <0|uint> Number of packets until exit (def: 0)\n"
1067 " -P|--prefix <name> Prefix for pcaps stored in directory\n"
1068 " -T|--magic <pcap-magic> Pcap magic number/pcap format to store, see -D\n"
1069 " -D|--dump-pcap-types Dump pcap types and magic numbers and quit\n"
1070 " -r|--rand Randomize packet forwarding order (dev->dev)\n"
1071 " -M|--no-promisc No promiscuous mode for netdev\n"
1072 " -A|--no-sock-mem Don't tune core socket memory\n"
1073 " -m|--mmap Mmap(2) pcap file i.e., for replaying pcaps\n"
1074 " -G|--sg Scatter/gather pcap file I/O\n"
1075 " -c|--clrw Use slower read(2)/write(2) I/O\n"
1076 " -S|--ring-size <size> Specify ring size to: <num>KiB/MiB/GiB\n"
1077 " -k|--kernel-pull <uint> Kernel pull from user interval in us (def: 10us)\n"
1078 " -b|--bind-cpu <cpu> Bind to specific CPU\n"
1079 " -u|--user <userid> Drop privileges and change to userid\n"
1080 " -g|--group <groupid> Drop privileges and change to groupid\n"
1081 " -H|--prio-high Make this high priority process\n"
1082 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
1083 " -s|--silent Do not print captured packets\n"
1084 " -q|--less Print less-verbose packet information\n"
1085 " -X|--hex Print packet data in hex format\n"
1086 " -l|--ascii Print human-readable packet data\n"
1087 " -V|--verbose Be more verbose\n"
1088 " -v|--version Show version\n"
1089 " -h|--help Guess what?!\n\n"
1090 "Examples:\n"
1091 " netsniff-ng --in eth0 --out dump.pcap -s -T 0xa1b2c3d4 --b 0 tcp or udp\n"
1092 " netsniff-ng --in wlan0 --rfraw --out dump.pcap --silent --bind-cpu 0\n"
1093 " netsniff-ng --in dump.pcap --mmap --out eth0 -k1000 --silent --bind-cpu 0\n"
1094 " netsniff-ng --in dump.pcap --out dump.cfg --silent --bind-cpu 0\n"
1095 " netsniff-ng --in eth0 --out eth1 --silent --bind-cpu 0 --type host\n"
1096 " netsniff-ng --in eth1 --out /opt/probe/ -s -m -J --interval 100MiB -b 0\n"
1097 " netsniff-ng --in vlan0 --out dump.pcap -c -u `id -u bob` -g `id -g bob`\n"
1098 " netsniff-ng --in any --filter http.bpf --jumbo-support --ascii -V\n\n"
1099 "Note:\n"
1100 " For introducing bit errors, delays with random variation and more\n"
1101 " while replaying pcaps, make use of tc(8) with its disciplines (e.g. netem).\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 version(void)
1114 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING);
1115 puts("http://www.netsniff-ng.org\n\n"
1116 "Please report bugs to <bugs@netsniff-ng.org>\n"
1117 "Copyright (C) 2009-2013 Daniel Borkmann <daniel@netsniff-ng.org>\n"
1118 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel@netsniff-ng.org>\n"
1119 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1120 "License: GNU GPL version 2.0\n"
1121 "This is free software: you are free to change and redistribute it.\n"
1122 "There is NO WARRANTY, to the extent permitted by law.\n");
1123 die();
1126 int main(int argc, char **argv)
1128 char *ptr;
1129 int c, i, j, cpu_tmp, opt_index, ops_touched = 0, vals[4] = {0};
1130 bool prio_high = false, setsockmem = true;
1131 void (*main_loop)(struct ctx *ctx) = NULL;
1132 struct ctx ctx = {
1133 .link_type = LINKTYPE_EN10MB,
1134 .print_mode = PRINT_NORM,
1135 .cpu = -1,
1136 .packet_type = -1,
1137 .promiscuous = true,
1138 .randomize = false,
1139 .pcap = PCAP_OPS_SG,
1140 .dump_interval = 60,
1141 .dump_mode = DUMP_INTERVAL_TIME,
1142 .uid = getuid(),
1143 .gid = getgid(),
1144 .magic = ORIGINAL_TCPDUMP_MAGIC,
1147 srand(time(NULL));
1149 while ((c = getopt_long(argc, argv, short_options, long_options,
1150 &opt_index)) != EOF) {
1151 switch (c) {
1152 case 'd':
1153 case 'i':
1154 ctx.device_in = xstrdup(optarg);
1155 break;
1156 case 'o':
1157 ctx.device_out = xstrdup(optarg);
1158 break;
1159 case 'P':
1160 ctx.prefix = xstrdup(optarg);
1161 break;
1162 case 'R':
1163 ctx.link_type = LINKTYPE_IEEE802_11;
1164 ctx.rfraw = 1;
1165 break;
1166 case 'r':
1167 ctx.randomize = true;
1168 break;
1169 case 'J':
1170 ctx.jumbo = true;
1171 break;
1172 case 'T':
1173 ctx.magic = (uint32_t) strtoul(optarg, NULL, 0);
1174 pcap_check_magic(ctx.magic);
1175 break;
1176 case 'f':
1177 ctx.filter = xstrdup(optarg);
1178 break;
1179 case 'M':
1180 ctx.promiscuous = false;
1181 break;
1182 case 'A':
1183 setsockmem = false;
1184 break;
1185 case 'u':
1186 ctx.uid = strtoul(optarg, NULL, 0);
1187 ctx.enforce = true;
1188 break;
1189 case 'g':
1190 ctx.gid = strtoul(optarg, NULL, 0);
1191 ctx.enforce = true;
1192 break;
1193 case 't':
1194 if (!strncmp(optarg, "host", strlen("host")))
1195 ctx.packet_type = PACKET_HOST;
1196 else if (!strncmp(optarg, "broadcast", strlen("broadcast")))
1197 ctx.packet_type = PACKET_BROADCAST;
1198 else if (!strncmp(optarg, "multicast", strlen("multicast")))
1199 ctx.packet_type = PACKET_MULTICAST;
1200 else if (!strncmp(optarg, "others", strlen("others")))
1201 ctx.packet_type = PACKET_OTHERHOST;
1202 else if (!strncmp(optarg, "outgoing", strlen("outgoing")))
1203 ctx.packet_type = PACKET_OUTGOING;
1204 else
1205 ctx.packet_type = -1;
1206 break;
1207 case 'S':
1208 ptr = optarg;
1209 ctx.reserve_size = 0;
1211 for (j = i = strlen(optarg); i > 0; --i) {
1212 if (!isdigit(optarg[j - i]))
1213 break;
1214 ptr++;
1217 if (!strncmp(ptr, "KiB", strlen("KiB")))
1218 ctx.reserve_size = 1 << 10;
1219 else if (!strncmp(ptr, "MiB", strlen("MiB")))
1220 ctx.reserve_size = 1 << 20;
1221 else if (!strncmp(ptr, "GiB", strlen("GiB")))
1222 ctx.reserve_size = 1 << 30;
1223 else
1224 panic("Syntax error in ring size param!\n");
1225 *ptr = 0;
1227 ctx.reserve_size *= strtol(optarg, NULL, 0);
1228 break;
1229 case 'b':
1230 cpu_tmp = strtol(optarg, NULL, 0);
1232 cpu_affinity(cpu_tmp);
1233 if (ctx.cpu != -2)
1234 ctx.cpu = cpu_tmp;
1235 break;
1236 case 'H':
1237 prio_high = true;
1238 break;
1239 case 'c':
1240 ctx.pcap = PCAP_OPS_RW;
1241 ops_touched = 1;
1242 break;
1243 case 'm':
1244 ctx.pcap = PCAP_OPS_MM;
1245 ops_touched = 1;
1246 break;
1247 case 'G':
1248 ctx.pcap = PCAP_OPS_SG;
1249 ops_touched = 1;
1250 break;
1251 case 'Q':
1252 ctx.cpu = -2;
1253 break;
1254 case 's':
1255 ctx.print_mode = PRINT_NONE;
1256 break;
1257 case 'q':
1258 ctx.print_mode = PRINT_LESS;
1259 break;
1260 case 'X':
1261 ctx.print_mode =
1262 (ctx.print_mode == PRINT_ASCII) ?
1263 PRINT_HEX_ASCII : PRINT_HEX;
1264 break;
1265 case 'l':
1266 ctx.print_mode =
1267 (ctx.print_mode == PRINT_HEX) ?
1268 PRINT_HEX_ASCII : PRINT_ASCII;
1269 break;
1270 case 'k':
1271 ctx.kpull = strtol(optarg, NULL, 0);
1272 break;
1273 case 'n':
1274 frame_count_max = strtol(optarg, NULL, 0);
1275 break;
1276 case 'F':
1277 ptr = optarg;
1278 ctx.dump_interval = 0;
1280 for (j = i = strlen(optarg); i > 0; --i) {
1281 if (!isdigit(optarg[j - i]))
1282 break;
1283 ptr++;
1286 if (!strncmp(ptr, "KiB", strlen("KiB"))) {
1287 ctx.dump_interval = 1 << 10;
1288 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1289 } else if (!strncmp(ptr, "MiB", strlen("MiB"))) {
1290 ctx.dump_interval = 1 << 20;
1291 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1292 } else if (!strncmp(ptr, "GiB", strlen("GiB"))) {
1293 ctx.dump_interval = 1 << 30;
1294 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1295 } else if (!strncmp(ptr, "sec", strlen("sec"))) {
1296 ctx.dump_interval = 1;
1297 ctx.dump_mode = DUMP_INTERVAL_TIME;
1298 } else if (!strncmp(ptr, "min", strlen("min"))) {
1299 ctx.dump_interval = 60;
1300 ctx.dump_mode = DUMP_INTERVAL_TIME;
1301 } else if (!strncmp(ptr, "hrs", strlen("hrs"))) {
1302 ctx.dump_interval = 60 * 60;
1303 ctx.dump_mode = DUMP_INTERVAL_TIME;
1304 } else if (!strncmp(ptr, "s", strlen("s"))) {
1305 ctx.dump_interval = 1;
1306 ctx.dump_mode = DUMP_INTERVAL_TIME;
1307 } else {
1308 panic("Syntax error in time/size param!\n");
1311 *ptr = 0;
1312 ctx.dump_interval *= strtol(optarg, NULL, 0);
1313 break;
1314 case 'V':
1315 ctx.verbose = 1;
1316 break;
1317 case 'D':
1318 pcap_dump_type_features();
1319 die();
1320 break;
1321 case 'v':
1322 version();
1323 break;
1324 case 'h':
1325 help();
1326 break;
1327 case '?':
1328 switch (optopt) {
1329 case 'd':
1330 case 'i':
1331 case 'o':
1332 case 'f':
1333 case 't':
1334 case 'P':
1335 case 'F':
1336 case 'n':
1337 case 'S':
1338 case 'b':
1339 case 'k':
1340 case 'T':
1341 case 'u':
1342 case 'g':
1343 case 'e':
1344 panic("Option -%c requires an argument!\n",
1345 optopt);
1346 default:
1347 if (isprint(optopt))
1348 printf("Unknown option character `0x%X\'!\n", optopt);
1349 die();
1351 default:
1352 break;
1356 if (!ctx.filter && optind != argc) {
1357 int ret;
1358 off_t offset = 0;
1360 for (i = optind; i < argc; ++i) {
1361 size_t alen = strlen(argv[i]) + 2;
1362 size_t flen = ctx.filter ? strlen(ctx.filter) : 0;
1364 ctx.filter = xrealloc(ctx.filter, 1, flen + alen);
1365 ret = slprintf(ctx.filter + offset, strlen(argv[i]) + 2, "%s ", argv[i]);
1366 if (ret < 0)
1367 panic("Cannot concatenate filter string!\n");
1368 else
1369 offset += ret;
1373 if (!ctx.device_in)
1374 ctx.device_in = xstrdup("any");
1376 register_signal(SIGINT, signal_handler);
1377 register_signal(SIGHUP, signal_handler);
1379 tprintf_init();
1381 if (prio_high) {
1382 set_proc_prio(get_default_proc_prio());
1383 set_sched_status(get_default_sched_policy(), get_default_sched_prio());
1386 if (ctx.device_in && (device_mtu(ctx.device_in) ||
1387 !strncmp("any", ctx.device_in, strlen(ctx.device_in)))) {
1388 if (!ctx.device_out) {
1389 ctx.dump = 0;
1390 main_loop = recv_only_or_dump;
1391 } else if (device_mtu(ctx.device_out)) {
1392 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1393 main_loop = receive_to_xmit;
1394 } else {
1395 ctx.dump = 1;
1396 register_signal_f(SIGALRM, timer_next_dump, SA_SIGINFO);
1397 main_loop = recv_only_or_dump;
1398 if (!ops_touched)
1399 ctx.pcap = PCAP_OPS_SG;
1401 } else {
1402 if (ctx.device_out && device_mtu(ctx.device_out)) {
1403 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1404 main_loop = pcap_to_xmit;
1405 if (!ops_touched)
1406 ctx.pcap = PCAP_OPS_MM;
1407 } else {
1408 main_loop = read_pcap;
1409 if (!ops_touched)
1410 ctx.pcap = PCAP_OPS_SG;
1414 bug_on(!main_loop);
1416 if (setsockmem)
1417 set_system_socket_memory(vals, array_size(vals));
1418 xlockme();
1420 main_loop(&ctx);
1422 xunlockme();
1423 if (setsockmem)
1424 reset_system_socket_memory(vals, array_size(vals));
1426 tprintf_cleanup();
1428 free(ctx.device_in);
1429 free(ctx.device_out);
1430 free(ctx.device_trans);
1431 free(ctx.prefix);
1433 return 0;