xio: add nacl's randombyte function
[netsniff-ng.git] / netsniff-ng.c
blob6c0f345965555ee00fa83b69e21e5b4a724b57a0
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: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 {"unbind-cpu", required_argument, NULL, 'B'},
85 {"prefix", required_argument, NULL, 'P'},
86 {"user", required_argument, NULL, 'u'},
87 {"group", required_argument, NULL, 'g'},
88 {"magic", required_argument, NULL, 'T'},
89 {"rand", no_argument, NULL, 'r'},
90 {"rfraw", no_argument, NULL, 'R'},
91 {"mmap", no_argument, NULL, 'm'},
92 {"sg", no_argument, NULL, 'G'},
93 {"clrw", no_argument, NULL, 'c'},
94 {"jumbo-support", no_argument, NULL, 'J'},
95 {"no-promisc", no_argument, NULL, 'M'},
96 {"prio-high", no_argument, NULL, 'H'},
97 {"notouch-irq", no_argument, NULL, 'Q'},
98 {"dump-pcap-types", no_argument, NULL, 'D'},
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 set_system_socket_memory(vals) \
117 do { \
118 if ((vals[0] = get_system_socket_mem(sock_rmem_max)) < SMEM_SUG_MAX) \
119 set_system_socket_mem(sock_rmem_max, SMEM_SUG_MAX); \
120 if ((vals[1] = get_system_socket_mem(sock_rmem_def)) < SMEM_SUG_DEF) \
121 set_system_socket_mem(sock_rmem_def, SMEM_SUG_DEF); \
122 if ((vals[2] = get_system_socket_mem(sock_wmem_max)) < SMEM_SUG_MAX) \
123 set_system_socket_mem(sock_wmem_max, SMEM_SUG_MAX); \
124 if ((vals[3] = get_system_socket_mem(sock_wmem_def)) < SMEM_SUG_DEF) \
125 set_system_socket_mem(sock_wmem_def, SMEM_SUG_DEF); \
126 } while (0)
128 #define reset_system_socket_memory(vals) \
129 do { \
130 set_system_socket_mem(sock_rmem_max, vals[0]); \
131 set_system_socket_mem(sock_rmem_def, vals[1]); \
132 set_system_socket_mem(sock_wmem_max, vals[2]); \
133 set_system_socket_mem(sock_wmem_def, vals[3]); \
134 } while (0)
136 #define __pcap_io pcap_ops[ctx->pcap]
138 static void signal_handler(int number)
140 switch (number) {
141 case SIGINT:
142 sigint = 1;
143 case SIGHUP:
144 default:
145 break;
149 static void timer_elapsed(int unused)
151 itimer.it_interval.tv_sec = 0;
152 itimer.it_interval.tv_usec = interval;
154 itimer.it_value.tv_sec = 0;
155 itimer.it_value.tv_usec = interval;
157 pull_and_flush_tx_ring(tx_sock);
158 setitimer(ITIMER_REAL, &itimer, NULL);
161 static void timer_next_dump(int unused)
163 itimer.it_interval.tv_sec = interval;
164 itimer.it_interval.tv_usec = 0;
166 itimer.it_value.tv_sec = interval;
167 itimer.it_value.tv_usec = 0;
169 next_dump = true;
170 setitimer(ITIMER_REAL, &itimer, NULL);
173 static inline bool dump_to_pcap(struct ctx *ctx)
175 return ctx->dump;
178 static void pcap_to_xmit(struct ctx *ctx)
180 __label__ out;
181 uint8_t *out = NULL;
182 int irq, ifindex, fd = 0, ret;
183 unsigned int size, it = 0;
184 unsigned long trunced = 0;
185 struct ring tx_ring;
186 struct frame_map *hdr;
187 struct sock_fprog bpf_ops;
188 struct timeval start, end, diff;
189 pcap_pkthdr_t phdr;
191 if (!device_up_and_running(ctx->device_out) && !ctx->rfraw)
192 panic("Device not up and running!\n");
194 bug_on(!__pcap_io);
196 tx_sock = pf_socket();
198 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
200 ret = __pcap_io->pull_fhdr_pcap(fd, &ctx->magic, &ctx->link_type);
201 if (ret)
202 panic("Error reading pcap header!\n");
204 if (__pcap_io->prepare_access_pcap) {
205 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_RD, ctx->jumbo);
206 if (ret)
207 panic("Error prepare reading pcap!\n");
210 fmemset(&tx_ring, 0, sizeof(tx_ring));
211 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
213 if (ctx->rfraw) {
214 ctx->device_trans = xstrdup(ctx->device_out);
215 xfree(ctx->device_out);
217 enter_rfmon_mac80211(ctx->device_trans, &ctx->device_out);
218 if (ctx->link_type != LINKTYPE_IEEE802_11)
219 panic("Wrong linktype of pcap!\n");
222 ifindex = device_ifindex(ctx->device_out);
224 size = ring_size(ctx->device_out, ctx->reserve_size);
226 bpf_parse_rules(ctx->device_out, ctx->filter, &bpf_ops);
228 set_packet_loss_discard(tx_sock);
229 set_sockopt_hwtimestamp(tx_sock, ctx->device_out);
231 setup_tx_ring_layout(tx_sock, &tx_ring, size, ctx->jumbo);
232 create_tx_ring(tx_sock, &tx_ring, ctx->verbose);
233 mmap_tx_ring(tx_sock, &tx_ring);
234 alloc_tx_ring_frames(&tx_ring);
235 bind_tx_ring(tx_sock, &tx_ring, ifindex);
237 dissector_init_all(ctx->print_mode);
239 if (ctx->cpu >= 0 && ifindex > 0) {
240 irq = device_irq_number(ctx->device_out);
241 device_bind_irq_to_cpu(irq, ctx->cpu);
243 if (ctx->verbose)
244 printf("IRQ: %s:%d > CPU%d\n",
245 ctx->device_out, irq, ctx->cpu);
248 if (ctx->kpull)
249 interval = ctx->kpull;
251 if (ctx->verbose) {
252 printf("BPF:\n");
253 bpf_dump_all(&bpf_ops);
255 printf("MD: TX %luus %s ", interval, pcap_ops_group_to_str[ctx->pcap]);
256 if (ctx->rfraw)
257 printf("802.11 raw via %s ", ctx->device_out);
258 #ifdef _LARGEFILE64_SOURCE
259 printf("lf64 ");
260 #endif
261 ioprio_print();
262 printf("\n");
265 itimer.it_interval.tv_sec = 0;
266 itimer.it_interval.tv_usec = interval;
268 itimer.it_value.tv_sec = 0;
269 itimer.it_value.tv_usec = interval;
271 setitimer(ITIMER_REAL, &itimer, NULL);
273 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
275 printf("Running! Hang up with ^C!\n\n");
276 fflush(stdout);
278 bug_on(gettimeofday(&start, NULL));
280 while (likely(sigint == 0)) {
281 while (user_may_pull_from_tx(tx_ring.frames[it].iov_base)) {
282 hdr = tx_ring.frames[it].iov_base;
283 out = ((uint8_t *) hdr) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
285 do {
286 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic, out,
287 ring_frame_size(&tx_ring));
288 if (unlikely(ret <= 0))
289 goto out;
291 if (ring_frame_size(&tx_ring) <
292 pcap_get_length(&phdr, ctx->magic)) {
293 pcap_set_length(&phdr, ctx->magic,
294 ring_frame_size(&tx_ring));
295 trunced++;
297 } while (ctx->filter &&
298 !bpf_run_filter(&bpf_ops, out,
299 pcap_get_length(&phdr, ctx->magic)));
301 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &hdr->tp_h, &hdr->s_ll);
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_RD);
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->device_in, 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);
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);
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 pcap_pkthdr_t phdr;
565 struct sock_fprog bpf_ops;
566 struct frame_map fm;
567 struct timeval start, end, diff;
568 struct sockaddr_ll sll;
570 bug_on(!__pcap_io);
572 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
574 ret = __pcap_io->pull_fhdr_pcap(fd, &ctx->magic, &ctx->link_type);
575 if (ret)
576 panic("Error reading pcap header!\n");
578 if (__pcap_io->prepare_access_pcap) {
579 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_RD, ctx->jumbo);
580 if (ret)
581 panic("Error prepare reading pcap!\n");
584 fmemset(&fm, 0, sizeof(fm));
585 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
587 bpf_parse_rules("any", ctx->filter, &bpf_ops);
589 dissector_init_all(ctx->print_mode);
591 out_len = round_up(1024 * 1024, PAGE_SIZE);
592 out = xmalloc_aligned(out_len, CO_CACHE_LINE_SIZE);
594 if (ctx->verbose) {
595 printf("BPF:\n");
596 bpf_dump_all(&bpf_ops);
598 printf("MD: RD %s ", pcap_ops_group_to_str[ctx->pcap]);
599 #ifdef _LARGEFILE64_SOURCE
600 printf("lf64 ");
601 #endif
602 ioprio_print();
603 printf("\n");
606 if (ctx->device_out)
607 fdo = open_or_die_m(ctx->device_out, O_RDWR | O_CREAT |
608 O_TRUNC | O_LARGEFILE, DEFFILEMODE);
610 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
612 printf("Running! Hang up with ^C!\n\n");
613 fflush(stdout);
615 bug_on(gettimeofday(&start, NULL));
617 while (likely(sigint == 0)) {
618 do {
619 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic,
620 out, out_len);
621 if (unlikely(ret < 0))
622 goto out;
624 if (unlikely(pcap_get_length(&phdr, ctx->magic) == 0)) {
625 trunced++;
626 continue;
629 if (unlikely(pcap_get_length(&phdr, ctx->magic) > out_len)) {
630 pcap_set_length(&phdr, ctx->magic, out_len);
631 trunced++;
633 } while (ctx->filter &&
634 !bpf_run_filter(&bpf_ops, out,
635 pcap_get_length(&phdr, ctx->magic)));
637 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &fm.tp_h, &sll);
639 ctx->tx_bytes += fm.tp_h.tp_len;
640 ctx->tx_packets++;
642 show_frame_hdr(&fm, ctx->print_mode, RING_MODE_EGRESS);
644 dissector_entry_point(out, fm.tp_h.tp_snaplen,
645 ctx->link_type, ctx->print_mode);
647 if (ctx->device_out)
648 translate_pcap_to_txf(fdo, out, fm.tp_h.tp_snaplen);
650 if (frame_count_max != 0) {
651 if (ctx->tx_packets >= frame_count_max) {
652 sigint = 1;
653 break;
658 out:
660 bug_on(gettimeofday(&end, NULL));
661 diff = tv_subtract(end, start);
663 bpf_release(&bpf_ops);
665 dissector_cleanup_all();
667 if (__pcap_io->prepare_close_pcap)
668 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
670 close(fd);
671 if (ctx->device_out)
672 close(fdo);
674 xfree(out);
676 fflush(stdout);
677 printf("\n");
678 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
679 printf("\r%12lu packets truncated in file\n", trunced);
680 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
681 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
684 static void finish_multi_pcap_file(struct ctx *ctx, int fd)
686 __pcap_io->fsync_pcap(fd);
688 if (__pcap_io->prepare_close_pcap)
689 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
691 close(fd);
693 fmemset(&itimer, 0, sizeof(itimer));
694 setitimer(ITIMER_REAL, &itimer, NULL);
697 static int next_multi_pcap_file(struct ctx *ctx, int fd)
699 int ret;
700 char fname[512];
702 __pcap_io->fsync_pcap(fd);
704 if (__pcap_io->prepare_close_pcap)
705 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
707 close(fd);
709 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
710 ctx->prefix ? : "dump-", time(0));
712 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
713 O_LARGEFILE, DEFFILEMODE);
715 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
716 if (ret)
717 panic("Error writing pcap header!\n");
719 if (__pcap_io->prepare_access_pcap) {
720 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, ctx->jumbo);
721 if (ret)
722 panic("Error prepare writing pcap!\n");
725 return fd;
728 static int begin_multi_pcap_file(struct ctx *ctx)
730 int fd, ret;
731 char fname[256];
733 bug_on(!__pcap_io);
735 if (ctx->device_out[strlen(ctx->device_out) - 1] == '/')
736 ctx->device_out[strlen(ctx->device_out) - 1] = 0;
738 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
739 ctx->prefix ? : "dump-", time(0));
741 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
742 O_LARGEFILE, DEFFILEMODE);
744 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
745 if (ret)
746 panic("Error writing pcap header!\n");
748 if (__pcap_io->prepare_access_pcap) {
749 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, ctx->jumbo);
750 if (ret)
751 panic("Error prepare writing pcap!\n");
754 if (ctx->dump_mode == DUMP_INTERVAL_TIME) {
755 interval = ctx->dump_interval;
757 itimer.it_interval.tv_sec = interval;
758 itimer.it_interval.tv_usec = 0;
760 itimer.it_value.tv_sec = interval;
761 itimer.it_value.tv_usec = 0;
763 setitimer(ITIMER_REAL, &itimer, NULL);
764 } else {
765 interval = 0;
768 return fd;
771 static void finish_single_pcap_file(struct ctx *ctx, int fd)
773 __pcap_io->fsync_pcap(fd);
775 if (__pcap_io->prepare_close_pcap)
776 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
778 close(fd);
781 static int begin_single_pcap_file(struct ctx *ctx)
783 int fd, ret;
785 bug_on(!__pcap_io);
787 fd = open_or_die_m(ctx->device_out, O_RDWR | O_CREAT | O_TRUNC |
788 O_LARGEFILE, DEFFILEMODE);
790 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
791 if (ret)
792 panic("Error writing pcap header!\n");
794 if (__pcap_io->prepare_access_pcap) {
795 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, ctx->jumbo);
796 if (ret)
797 panic("Error prepare writing pcap!\n");
800 return fd;
803 static void print_pcap_file_stats(int sock, struct ctx *ctx, unsigned long skipped)
805 unsigned long good, bad;
806 struct tpacket_stats kstats;
807 socklen_t slen = sizeof(kstats);
809 fmemset(&kstats, 0, sizeof(kstats));
810 getsockopt(sock, SOL_PACKET, PACKET_STATISTICS, &kstats, &slen);
812 if (ctx->print_mode == PRINT_NONE) {
813 good = kstats.tp_packets - kstats.tp_drops - skipped;
814 bad = kstats.tp_drops + skipped;
816 printf(".(+%lu/-%lu)", good, bad);
817 fflush(stdout);
821 static void recv_only_or_dump(struct ctx *ctx)
823 uint8_t *packet;
824 short ifflags = 0;
825 int sock, irq, ifindex, fd = 0, ret;
826 unsigned int size, it = 0;
827 unsigned long frame_count = 0, skipped = 0;
828 struct ring rx_ring;
829 struct pollfd rx_poll;
830 struct frame_map *hdr;
831 struct sock_fprog bpf_ops;
832 struct timeval start, end, diff;
833 pcap_pkthdr_t phdr;
835 if (!device_up_and_running(ctx->device_in) && !ctx->rfraw)
836 panic("Device not up and running!\n");
838 sock = pf_socket();
840 if (ctx->rfraw) {
841 ctx->device_trans = xstrdup(ctx->device_in);
842 xfree(ctx->device_in);
844 enter_rfmon_mac80211(ctx->device_trans, &ctx->device_in);
845 ctx->link_type = LINKTYPE_IEEE802_11;
848 fmemset(&rx_ring, 0, sizeof(rx_ring));
849 fmemset(&rx_poll, 0, sizeof(rx_poll));
850 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
852 ifindex = device_ifindex(ctx->device_in);
854 size = ring_size(ctx->device_in, ctx->reserve_size);
856 enable_kernel_bpf_jit_compiler();
858 bpf_parse_rules(ctx->device_in, ctx->filter, &bpf_ops);
859 bpf_attach_to_sock(sock, &bpf_ops);
861 set_sockopt_hwtimestamp(sock, ctx->device_in);
863 setup_rx_ring_layout(sock, &rx_ring, size, ctx->jumbo);
864 create_rx_ring(sock, &rx_ring, ctx->verbose);
865 mmap_rx_ring(sock, &rx_ring);
866 alloc_rx_ring_frames(&rx_ring);
867 bind_rx_ring(sock, &rx_ring, ifindex);
869 prepare_polling(sock, &rx_poll);
870 dissector_init_all(ctx->print_mode);
872 if (ctx->cpu >= 0 && ifindex > 0) {
873 irq = device_irq_number(ctx->device_in);
874 device_bind_irq_to_cpu(irq, ctx->cpu);
876 if (ctx->verbose)
877 printf("IRQ: %s:%d > CPU%d\n",
878 ctx->device_in, irq, ctx->cpu);
881 if (ctx->promiscuous)
882 ifflags = enter_promiscuous_mode(ctx->device_in);
884 if (ctx->verbose) {
885 printf("BPF:\n");
886 bpf_dump_all(&bpf_ops);
888 printf("MD: RX %s ", ctx->dump ? pcap_ops_group_to_str[ctx->pcap] : "");
889 if (ctx->rfraw)
890 printf("802.11 raw via %s ", ctx->device_in);
891 #ifdef _LARGEFILE64_SOURCE
892 printf("lf64 ");
893 #endif
894 ioprio_print();
895 printf("\n");
898 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
900 if (dump_to_pcap(ctx)) {
901 __label__ try_file;
902 struct stat stats;
904 fmemset(&stats, 0, sizeof(stats));
905 ret = stat(ctx->device_out, &stats);
906 if (ret < 0) {
907 ctx->dump_dir = 0;
908 goto try_file;
911 ctx->dump_dir = S_ISDIR(stats.st_mode);
912 if (ctx->dump_dir) {
913 fd = begin_multi_pcap_file(ctx);
914 } else {
915 try_file:
916 fd = begin_single_pcap_file(ctx);
920 printf("Running! Hang up with ^C!\n\n");
921 fflush(stdout);
923 bug_on(gettimeofday(&start, NULL));
925 while (likely(sigint == 0)) {
926 while (user_may_pull_from_rx(rx_ring.frames[it].iov_base)) {
927 __label__ next;
929 hdr = rx_ring.frames[it].iov_base;
930 packet = ((uint8_t *) hdr) + hdr->tp_h.tp_mac;
931 frame_count++;
933 if (ctx->packet_type != -1)
934 if (ctx->packet_type != hdr->s_ll.sll_pkttype)
935 goto next;
937 if (unlikely(ring_frame_size(&rx_ring) < hdr->tp_h.tp_snaplen)) {
938 skipped++;
939 goto next;
942 if (dump_to_pcap(ctx)) {
943 tpacket_hdr_to_pcap_pkthdr(&hdr->tp_h, &hdr->s_ll, &phdr, ctx->magic);
945 ret = __pcap_io->write_pcap(fd, &phdr, ctx->magic, packet,
946 pcap_get_length(&phdr, ctx->magic));
947 if (unlikely(ret != pcap_get_total_length(&phdr, ctx->magic)))
948 panic("Write error to pcap!\n");
951 show_frame_hdr(hdr, ctx->print_mode, RING_MODE_INGRESS);
953 dissector_entry_point(packet, hdr->tp_h.tp_snaplen,
954 ctx->link_type, ctx->print_mode);
956 if (frame_count_max != 0) {
957 if (frame_count >= frame_count_max) {
958 sigint = 1;
959 break;
963 next:
965 kernel_may_pull_from_rx(&hdr->tp_h);
967 it++;
968 if (it >= rx_ring.layout.tp_frame_nr)
969 it = 0;
971 if (unlikely(sigint == 1))
972 break;
974 if (dump_to_pcap(ctx)) {
975 if (ctx->dump_mode == DUMP_INTERVAL_SIZE) {
976 interval += hdr->tp_h.tp_snaplen;
978 if (interval > ctx->dump_interval) {
979 next_dump = true;
980 interval = 0;
984 if (next_dump) {
985 fd = next_multi_pcap_file(ctx, fd);
986 next_dump = false;
988 if (ctx->verbose)
989 print_pcap_file_stats(sock, ctx, skipped);
994 poll(&rx_poll, 1, -1);
995 poll_error_maybe_die(sock, &rx_poll);
998 bug_on(gettimeofday(&end, NULL));
999 diff = tv_subtract(end, start);
1001 if (dump_to_pcap(ctx)) {
1002 if (ctx->dump_dir)
1003 finish_multi_pcap_file(ctx, fd);
1004 else
1005 finish_single_pcap_file(ctx, fd);
1008 if (!(ctx->dump_dir && ctx->print_mode == PRINT_NONE)) {
1009 sock_print_net_stats(sock, skipped);
1011 printf("\r%12lu sec, %lu usec in total\n",
1012 diff.tv_sec, diff.tv_usec);
1013 } else {
1014 printf("\n\n");
1015 fflush(stdout);
1018 bpf_release(&bpf_ops);
1019 dissector_cleanup_all();
1020 destroy_rx_ring(sock, &rx_ring);
1022 if (ctx->promiscuous)
1023 leave_promiscuous_mode(ctx->device_in, ifflags);
1025 if (ctx->rfraw)
1026 leave_rfmon_mac80211(ctx->device_trans, ctx->device_in);
1028 close(sock);
1031 static void help(void)
1033 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING);
1034 puts("http://www.netsniff-ng.org\n\n"
1035 "Usage: netsniff-ng [options] [filter-expression]\n"
1036 "Options:\n"
1037 " -i|-d|--dev|--in <dev|pcap> Input source as netdev or pcap\n"
1038 " -o|--out <dev|pcap|dir|cfg> Output sink as netdev, pcap, directory, trafgen file\n"
1039 " -f|--filter <bpf-file|expr> Use BPF filter file from bpfc or tcpdump-like expression\n"
1040 " -t|--type <type> Only handle packets of defined type:\n"
1041 " host|broadcast|multicast|others|outgoing\n"
1042 " -F|--interval <size/time> Dump interval in time or size if -o is a directory\n"
1043 " pcap swap spec: <num>KiB/MiB/GiB/s/sec/min/hrs\n"
1044 " -J|--jumbo-support Support for 64KB Super Jumbo Frames\n"
1045 " Default RX/TX slot: 2048Byte\n"
1046 " -R|--rfraw Capture or inject raw 802.11 frames\n"
1047 " -n|--num <uint> Number of packets until exit\n"
1048 " `-- 0 Loop until interrupted (default)\n"
1049 " `- n Send n packets and done\n"
1050 "Options for printing:\n"
1051 " -s|--silent Do not print captured packets\n"
1052 " -q|--less Print less-verbose packet information\n"
1053 " -X|--hex Print packet data in hex format\n"
1054 " -l|--ascii Print human-readable packet data\n"
1055 "Options, advanced:\n"
1056 " -P|--prefix <name> Prefix for pcaps stored in directory\n"
1057 " -T|--magic <pcap-magic> Pcap magic number/pcap type to process\n"
1058 " -r|--rand Randomize packet forwarding order\n"
1059 " -M|--no-promisc No promiscuous mode for netdev\n"
1060 " -A|--no-sock-mem Don't tune core socket memory\n"
1061 " -m|--mmap Mmap pcap file i.e., for replaying\n"
1062 " -G|--sg Scatter/gather pcap file I/O\n"
1063 " -c|--clrw Use slower read(2)/write(2) I/O\n"
1064 " -S|--ring-size <size> Manually set ring size to <size>:\n"
1065 " mmap space in KiB/MiB/GiB, e.g. \'10MiB\'\n"
1066 " -k|--kernel-pull <uint> Kernel pull from user interval in us\n"
1067 " Default is 10us where the TX_RING\n"
1068 " is populated with payload from uspace\n"
1069 " -b|--bind-cpu <cpu> Bind to specific CPU (or CPU-range)\n"
1070 " -B|--unbind-cpu <cpu> Forbid to use specific CPU (or CPU-range)\n"
1071 " -u|--user <userid> Drop privileges and change to userid\n"
1072 " -g|--group <groupid> Drop privileges and change to groupid\n"
1073 " -H|--prio-high Make this high priority process\n"
1074 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
1075 " -V|--verbose Be more verbose\n"
1076 " -D|--dump-pcap-types Dump pcap types and magic numbers\n"
1077 " -v|--version Show version\n"
1078 " -h|--help Guess what?!\n\n"
1079 "Examples:\n"
1080 " netsniff-ng --in eth0 --out dump.pcap --silent -T 0xa1b2c3d4 --bind-cpu 0\n"
1081 " netsniff-ng --in wlan0 --rfraw --out dump.pcap --silent --bind-cpu 0\n"
1082 " netsniff-ng --in dump.pcap --mmap --out eth0 -k1000 --silent --bind-cpu 0\n"
1083 " netsniff-ng --in dump.pcap --out dump.cfg --silent --bind-cpu 0\n"
1084 " netsniff-ng --in eth0 --out eth1 --silent --bind-cpu 0 --type host\n"
1085 " netsniff-ng --in eth1 --out /opt/probe/ -s -m -J --interval 100MiB -b 0\n"
1086 " netsniff-ng --in vlan0 --out dump.pcap -c -u `id -u bob` -g `id -g bob`\n"
1087 " netsniff-ng --in any --filter http.bpf --jumbo-support --ascii -V\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(),
1135 .magic = ORIGINAL_TCPDUMP_MAGIC,
1138 srand(time(NULL));
1140 while ((c = getopt_long(argc, argv, short_options, long_options,
1141 &opt_index)) != EOF) {
1142 switch (c) {
1143 case 'd':
1144 case 'i':
1145 ctx.device_in = xstrdup(optarg);
1146 break;
1147 case 'o':
1148 ctx.device_out = xstrdup(optarg);
1149 break;
1150 case 'P':
1151 ctx.prefix = xstrdup(optarg);
1152 break;
1153 case 'R':
1154 ctx.link_type = LINKTYPE_IEEE802_11;
1155 ctx.rfraw = 1;
1156 break;
1157 case 'r':
1158 ctx.randomize = true;
1159 break;
1160 case 'J':
1161 ctx.jumbo = true;
1162 break;
1163 case 'T':
1164 ctx.magic = (uint32_t) strtoul(optarg, NULL, 0);
1165 pcap_check_magic(ctx.magic);
1166 break;
1167 case 'f':
1168 ctx.filter = xstrdup(optarg);
1169 break;
1170 case 'M':
1171 ctx.promiscuous = false;
1172 break;
1173 case 'A':
1174 setsockmem = false;
1175 break;
1176 case 'u':
1177 ctx.uid = strtoul(optarg, NULL, 0);
1178 ctx.enforce = true;
1179 break;
1180 case 'g':
1181 ctx.gid = strtoul(optarg, NULL, 0);
1182 ctx.enforce = true;
1183 break;
1184 case 't':
1185 if (!strncmp(optarg, "host", strlen("host")))
1186 ctx.packet_type = PACKET_HOST;
1187 else if (!strncmp(optarg, "broadcast", strlen("broadcast")))
1188 ctx.packet_type = PACKET_BROADCAST;
1189 else if (!strncmp(optarg, "multicast", strlen("multicast")))
1190 ctx.packet_type = PACKET_MULTICAST;
1191 else if (!strncmp(optarg, "others", strlen("others")))
1192 ctx.packet_type = PACKET_OTHERHOST;
1193 else if (!strncmp(optarg, "outgoing", strlen("outgoing")))
1194 ctx.packet_type = PACKET_OUTGOING;
1195 else
1196 ctx.packet_type = -1;
1197 break;
1198 case 'S':
1199 ptr = optarg;
1200 ctx.reserve_size = 0;
1202 for (j = i = strlen(optarg); i > 0; --i) {
1203 if (!isdigit(optarg[j - i]))
1204 break;
1205 ptr++;
1208 if (!strncmp(ptr, "KiB", strlen("KiB")))
1209 ctx.reserve_size = 1 << 10;
1210 else if (!strncmp(ptr, "MiB", strlen("MiB")))
1211 ctx.reserve_size = 1 << 20;
1212 else if (!strncmp(ptr, "GiB", strlen("GiB")))
1213 ctx.reserve_size = 1 << 30;
1214 else
1215 panic("Syntax error in ring size param!\n");
1216 *ptr = 0;
1218 ctx.reserve_size *= strtol(optarg, NULL, 0);
1219 break;
1220 case 'b':
1221 set_cpu_affinity(optarg, 0);
1222 /* Take the first CPU for rebinding the IRQ */
1223 if (ctx.cpu != -2)
1224 ctx.cpu = strtol(optarg, NULL, 0);
1225 break;
1226 case 'B':
1227 set_cpu_affinity(optarg, 1);
1228 break;
1229 case 'H':
1230 prio_high = true;
1231 break;
1232 case 'c':
1233 ctx.pcap = PCAP_OPS_RW;
1234 ops_touched = 1;
1235 break;
1236 case 'm':
1237 ctx.pcap = PCAP_OPS_MM;
1238 ops_touched = 1;
1239 break;
1240 case 'G':
1241 ctx.pcap = PCAP_OPS_SG;
1242 ops_touched = 1;
1243 break;
1244 case 'Q':
1245 ctx.cpu = -2;
1246 break;
1247 case 's':
1248 ctx.print_mode = PRINT_NONE;
1249 break;
1250 case 'q':
1251 ctx.print_mode = PRINT_LESS;
1252 break;
1253 case 'X':
1254 ctx.print_mode =
1255 (ctx.print_mode == PRINT_ASCII) ?
1256 PRINT_HEX_ASCII : PRINT_HEX;
1257 break;
1258 case 'l':
1259 ctx.print_mode =
1260 (ctx.print_mode == PRINT_HEX) ?
1261 PRINT_HEX_ASCII : PRINT_ASCII;
1262 break;
1263 case 'k':
1264 ctx.kpull = strtol(optarg, NULL, 0);
1265 break;
1266 case 'n':
1267 frame_count_max = strtol(optarg, NULL, 0);
1268 break;
1269 case 'F':
1270 ptr = optarg;
1271 ctx.dump_interval = 0;
1273 for (j = i = strlen(optarg); i > 0; --i) {
1274 if (!isdigit(optarg[j - i]))
1275 break;
1276 ptr++;
1279 if (!strncmp(ptr, "KiB", strlen("KiB"))) {
1280 ctx.dump_interval = 1 << 10;
1281 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1282 } else if (!strncmp(ptr, "MiB", strlen("MiB"))) {
1283 ctx.dump_interval = 1 << 20;
1284 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1285 } else if (!strncmp(ptr, "GiB", strlen("GiB"))) {
1286 ctx.dump_interval = 1 << 30;
1287 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1288 } else if (!strncmp(ptr, "sec", strlen("sec"))) {
1289 ctx.dump_interval = 1;
1290 ctx.dump_mode = DUMP_INTERVAL_TIME;
1291 } else if (!strncmp(ptr, "min", strlen("min"))) {
1292 ctx.dump_interval = 60;
1293 ctx.dump_mode = DUMP_INTERVAL_TIME;
1294 } else if (!strncmp(ptr, "hrs", strlen("hrs"))) {
1295 ctx.dump_interval = 60 * 60;
1296 ctx.dump_mode = DUMP_INTERVAL_TIME;
1297 } else if (!strncmp(ptr, "s", strlen("s"))) {
1298 ctx.dump_interval = 1;
1299 ctx.dump_mode = DUMP_INTERVAL_TIME;
1300 } else {
1301 panic("Syntax error in time/size param!\n");
1304 *ptr = 0;
1305 ctx.dump_interval *= strtol(optarg, NULL, 0);
1306 break;
1307 case 'V':
1308 ctx.verbose = 1;
1309 break;
1310 case 'D':
1311 pcap_dump_type_features();
1312 die();
1313 break;
1314 case 'v':
1315 version();
1316 break;
1317 case 'h':
1318 help();
1319 break;
1320 case '?':
1321 switch (optopt) {
1322 case 'd':
1323 case 'i':
1324 case 'o':
1325 case 'f':
1326 case 't':
1327 case 'P':
1328 case 'F':
1329 case 'n':
1330 case 'S':
1331 case 'b':
1332 case 'k':
1333 case 'T':
1334 case 'u':
1335 case 'g':
1336 case 'B':
1337 case 'e':
1338 panic("Option -%c requires an argument!\n",
1339 optopt);
1340 default:
1341 if (isprint(optopt))
1342 whine("Unknown option character "
1343 "`0x%X\'!\n", optopt);
1344 die();
1346 default:
1347 break;
1351 if (!ctx.filter && optind != argc) {
1352 int ret;
1353 off_t offset = 0;
1355 for (i = optind; i < argc; ++i) {
1356 size_t alen = strlen(argv[i]) + 2;
1357 size_t flen = ctx.filter ? strlen(ctx.filter) : 0;
1359 ctx.filter = xrealloc(ctx.filter, 1, flen + alen);
1360 ret = slprintf(ctx.filter + offset, strlen(argv[i]) + 2, "%s ", argv[i]);
1361 if (ret < 0)
1362 panic("Cannot concatenate filter string!\n");
1363 else
1364 offset += ret;
1368 if (!ctx.device_in)
1369 ctx.device_in = xstrdup("any");
1371 register_signal(SIGINT, signal_handler);
1372 register_signal(SIGHUP, signal_handler);
1374 header();
1376 tprintf_init();
1378 if (prio_high) {
1379 set_proc_prio(get_default_proc_prio());
1380 set_sched_status(get_default_sched_policy(), get_default_sched_prio());
1383 if (ctx.device_in && (device_mtu(ctx.device_in) ||
1384 !strncmp("any", ctx.device_in, strlen(ctx.device_in)))) {
1385 if (!ctx.device_out) {
1386 ctx.dump = 0;
1387 main_loop = recv_only_or_dump;
1388 } else if (device_mtu(ctx.device_out)) {
1389 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1390 main_loop = receive_to_xmit;
1391 } else {
1392 ctx.dump = 1;
1393 register_signal_f(SIGALRM, timer_next_dump, SA_SIGINFO);
1394 main_loop = recv_only_or_dump;
1395 if (!ops_touched)
1396 ctx.pcap = PCAP_OPS_SG;
1398 } else {
1399 if (ctx.device_out && device_mtu(ctx.device_out)) {
1400 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1401 main_loop = pcap_to_xmit;
1402 if (!ops_touched)
1403 ctx.pcap = PCAP_OPS_MM;
1404 } else {
1405 main_loop = read_pcap;
1406 if (!ops_touched)
1407 ctx.pcap = PCAP_OPS_SG;
1411 bug_on(!main_loop);
1413 if (setsockmem)
1414 set_system_socket_memory(vals);
1415 xlockme();
1417 main_loop(&ctx);
1419 xunlockme();
1420 if (setsockmem)
1421 reset_system_socket_memory(vals);
1423 tprintf_cleanup();
1425 free(ctx.device_in);
1426 free(ctx.device_out);
1427 free(ctx.device_trans);
1428 free(ctx.prefix);
1430 return 0;