docs: install: Fix a minor typo
[netsniff-ng.git] / netsniff-ng.c
blob351dcc9c30126c0d04fa0047fbd3a1c9de4e1341
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2009-2013 Daniel Borkmann.
4 * Copyright 2010 Emmanuel Roullit.
5 * Subject to the GPL, version 2.
6 */
8 #define _GNU_SOURCE
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <signal.h>
12 #include <getopt.h>
13 #include <ctype.h>
14 #include <time.h>
15 #include <string.h>
16 #include <sys/socket.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <sys/time.h>
20 #include <sys/fsuid.h>
21 #include <unistd.h>
22 #include <stdbool.h>
23 #include <pthread.h>
24 #include <fcntl.h>
26 #include "ring_rx.h"
27 #include "ring_tx.h"
28 #include "mac80211.h"
29 #include "dev.h"
30 #include "built_in.h"
31 #include "pcap_io.h"
32 #include "privs.h"
33 #include "proc.h"
34 #include "bpf.h"
35 #include "ioops.h"
36 #include "die.h"
37 #include "irq.h"
38 #include "str.h"
39 #include "sig.h"
40 #include "config.h"
41 #include "sock.h"
42 #include "geoip.h"
43 #include "lockme.h"
44 #include "tprintf.h"
45 #include "timer.h"
46 #include "tstamping.h"
47 #include "dissector.h"
48 #include "xmalloc.h"
50 enum dump_mode {
51 DUMP_INTERVAL_TIME,
52 DUMP_INTERVAL_SIZE,
55 struct ctx {
56 char *device_in, *device_out, *device_trans, *filter, *prefix;
57 int cpu, rfraw, dump, print_mode, dump_dir, packet_type, verbose;
58 unsigned long kpull, dump_interval, reserve_size, tx_bytes, tx_packets;
59 bool randomize, promiscuous, enforce, jumbo, dump_bpf;
60 enum pcap_ops_groups pcap; enum dump_mode dump_mode;
61 uid_t uid; gid_t gid; uint32_t link_type, magic;
64 static volatile sig_atomic_t sigint = 0;
65 static volatile bool next_dump = false;
67 static const char *short_options = "d:i:o:rf:MJt:S:k:n:b:HQmcsqXlvhF:RGAP:Vu:g:T:DBU";
68 static const struct option long_options[] = {
69 {"dev", required_argument, NULL, 'd'},
70 {"in", required_argument, NULL, 'i'},
71 {"out", required_argument, NULL, 'o'},
72 {"filter", required_argument, NULL, 'f'},
73 {"num", required_argument, NULL, 'n'},
74 {"type", required_argument, NULL, 't'},
75 {"interval", required_argument, NULL, 'F'},
76 {"ring-size", required_argument, NULL, 'S'},
77 {"kernel-pull", required_argument, NULL, 'k'},
78 {"bind-cpu", required_argument, NULL, 'b'},
79 {"prefix", required_argument, NULL, 'P'},
80 {"user", required_argument, NULL, 'u'},
81 {"group", required_argument, NULL, 'g'},
82 {"magic", required_argument, NULL, 'T'},
83 {"rand", no_argument, NULL, 'r'},
84 {"rfraw", no_argument, NULL, 'R'},
85 {"mmap", no_argument, NULL, 'm'},
86 {"sg", no_argument, NULL, 'G'},
87 {"clrw", no_argument, NULL, 'c'},
88 {"jumbo-support", no_argument, NULL, 'J'},
89 {"no-promisc", no_argument, NULL, 'M'},
90 {"prio-high", no_argument, NULL, 'H'},
91 {"notouch-irq", no_argument, NULL, 'Q'},
92 {"dump-pcap-types", no_argument, NULL, 'D'},
93 {"dump-bpf", no_argument, NULL, 'B'},
94 {"silent", no_argument, NULL, 's'},
95 {"less", no_argument, NULL, 'q'},
96 {"hex", no_argument, NULL, 'X'},
97 {"ascii", no_argument, NULL, 'l'},
98 {"no-sock-mem", no_argument, NULL, 'A'},
99 {"update", no_argument, NULL, 'U'},
100 {"verbose", no_argument, NULL, 'V'},
101 {"version", no_argument, NULL, 'v'},
102 {"help", no_argument, NULL, 'h'},
103 {NULL, 0, NULL, 0}
106 static int tx_sock;
107 static struct itimerval itimer;
108 static unsigned long frame_count_max = 0, interval = TX_KERNEL_PULL_INT;
110 #define __pcap_io pcap_ops[ctx->pcap]
112 static void signal_handler(int number)
114 switch (number) {
115 case SIGINT:
116 case SIGQUIT:
117 case SIGTERM:
118 sigint = 1;
119 case SIGHUP:
120 default:
121 break;
125 static void timer_elapsed(int unused __maybe_unused)
127 int ret;
129 set_itimer_interval_value(&itimer, 0, interval);
131 ret = pull_and_flush_tx_ring(tx_sock);
132 if (unlikely(ret < 0)) {
133 /* We could hit EBADF if the socket has been closed before
134 * the timer was triggered.
136 if (errno != EBADF && errno != ENOBUFS)
137 panic("Flushing TX_RING failed: %s!\n", strerror(errno));
140 setitimer(ITIMER_REAL, &itimer, NULL);
143 static void timer_purge(void)
145 int ret;
147 ret = pull_and_flush_tx_ring_wait(tx_sock);
148 if (unlikely(ret < 0)) {
149 if (errno != EBADF && errno != ENOBUFS)
150 panic("Flushing TX_RING failed: %s!\n", strerror(errno));
153 set_itimer_interval_value(&itimer, 0, 0);
154 setitimer(ITIMER_REAL, &itimer, NULL);
157 static void timer_next_dump(int unused __maybe_unused)
159 set_itimer_interval_value(&itimer, interval, 0);
160 next_dump = true;
161 setitimer(ITIMER_REAL, &itimer, NULL);
164 static inline bool dump_to_pcap(struct ctx *ctx)
166 return ctx->dump;
169 static void pcap_to_xmit(struct ctx *ctx)
171 __label__ out;
172 uint8_t *out = NULL;
173 int irq, ifindex, fd = 0, ret;
174 unsigned int size, it = 0;
175 unsigned long trunced = 0;
176 struct ring tx_ring;
177 struct frame_map *hdr;
178 struct sock_fprog bpf_ops;
179 struct timeval start, end, diff;
180 pcap_pkthdr_t phdr;
182 if (!device_up_and_running(ctx->device_out) && !ctx->rfraw)
183 panic("Device not up and running!\n");
185 bug_on(!__pcap_io);
187 tx_sock = pf_socket();
189 if (!strncmp("-", ctx->device_in, strlen("-"))) {
190 fd = dup_or_die(fileno(stdin));
191 close(fileno(stdin));
192 if (ctx->pcap == PCAP_OPS_MM)
193 ctx->pcap = PCAP_OPS_SG;
194 } else {
195 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
198 if (__pcap_io->init_once_pcap)
199 __pcap_io->init_once_pcap();
201 ret = __pcap_io->pull_fhdr_pcap(fd, &ctx->magic, &ctx->link_type);
202 if (ret)
203 panic("Error reading pcap header!\n");
205 if (__pcap_io->prepare_access_pcap) {
206 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_RD, ctx->jumbo);
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, ctx->link_type);
228 if (ctx->dump_bpf)
229 bpf_dump_all(&bpf_ops);
231 set_packet_loss_discard(tx_sock);
233 setup_tx_ring_layout(tx_sock, &tx_ring, size, ctx->jumbo);
234 create_tx_ring(tx_sock, &tx_ring, ctx->verbose);
235 mmap_tx_ring(tx_sock, &tx_ring);
236 alloc_tx_ring_frames(tx_sock, &tx_ring);
237 bind_tx_ring(tx_sock, &tx_ring, ifindex);
239 dissector_init_all(ctx->print_mode);
241 if (ctx->cpu >= 0 && ifindex > 0) {
242 irq = device_irq_number(ctx->device_out);
243 device_set_irq_affinity(irq, ctx->cpu);
245 if (ctx->verbose)
246 printf("IRQ: %s:%d > CPU%d\n",
247 ctx->device_out, irq, ctx->cpu);
250 if (ctx->kpull)
251 interval = ctx->kpull;
253 set_itimer_interval_value(&itimer, 0, interval);
254 setitimer(ITIMER_REAL, &itimer, NULL);
256 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
258 printf("Running! Hang up with ^C!\n\n");
259 fflush(stdout);
261 bug_on(gettimeofday(&start, NULL));
263 while (likely(sigint == 0)) {
264 while (user_may_pull_from_tx(tx_ring.frames[it].iov_base)) {
265 hdr = tx_ring.frames[it].iov_base;
266 out = ((uint8_t *) hdr) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
268 do {
269 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic, out,
270 ring_frame_size(&tx_ring));
271 if (unlikely(ret <= 0))
272 goto out;
274 if (ring_frame_size(&tx_ring) <
275 pcap_get_length(&phdr, ctx->magic)) {
276 pcap_set_length(&phdr, ctx->magic,
277 ring_frame_size(&tx_ring));
278 trunced++;
280 } while (ctx->filter &&
281 !bpf_run_filter(&bpf_ops, out,
282 pcap_get_length(&phdr, ctx->magic)));
284 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &hdr->tp_h);
286 ctx->tx_bytes += hdr->tp_h.tp_len;;
287 ctx->tx_packets++;
289 show_frame_hdr(hdr, ctx->print_mode);
291 dissector_entry_point(out, hdr->tp_h.tp_snaplen,
292 ctx->link_type, ctx->print_mode);
294 kernel_may_pull_from_tx(&hdr->tp_h);
296 it++;
297 if (it >= tx_ring.layout.tp_frame_nr)
298 it = 0;
300 if (unlikely(sigint == 1))
301 break;
303 if (frame_count_max != 0) {
304 if (ctx->tx_packets >= frame_count_max) {
305 sigint = 1;
306 break;
312 out:
314 bug_on(gettimeofday(&end, NULL));
315 timersub(&end, &start, &diff);
317 timer_purge();
319 bpf_release(&bpf_ops);
321 dissector_cleanup_all();
322 destroy_tx_ring(tx_sock, &tx_ring);
324 if (ctx->rfraw)
325 leave_rfmon_mac80211(ctx->device_out);
327 if (__pcap_io->prepare_close_pcap)
328 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
330 if (!strncmp("-", ctx->device_in, strlen("-")))
331 dup2(fd, fileno(stdin));
332 close(fd);
334 close(tx_sock);
336 fflush(stdout);
337 printf("\n");
338 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
339 printf("\r%12lu packets truncated in file\n", trunced);
340 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
341 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
344 static void receive_to_xmit(struct ctx *ctx)
346 short ifflags = 0;
347 uint8_t *in, *out;
348 int rx_sock, ifindex_in, ifindex_out, ret;
349 unsigned int size_in, size_out, it_in = 0, it_out = 0;
350 unsigned long frame_count = 0;
351 struct frame_map *hdr_in, *hdr_out;
352 struct ring tx_ring, rx_ring;
353 struct pollfd rx_poll;
354 struct sock_fprog bpf_ops;
356 if (!strncmp(ctx->device_in, ctx->device_out, IFNAMSIZ))
357 panic("Ingress/egress devices must be different!\n");
358 if (!device_up_and_running(ctx->device_out))
359 panic("Egress device not up and running!\n");
361 rx_sock = pf_socket();
362 tx_sock = pf_socket();
364 fmemset(&tx_ring, 0, sizeof(tx_ring));
365 fmemset(&rx_ring, 0, sizeof(rx_ring));
366 fmemset(&rx_poll, 0, sizeof(rx_poll));
367 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
369 ifindex_in = device_ifindex(ctx->device_in);
370 ifindex_out = device_ifindex(ctx->device_out);
372 size_in = ring_size(ctx->device_in, ctx->reserve_size);
373 size_out = ring_size(ctx->device_out, ctx->reserve_size);
375 enable_kernel_bpf_jit_compiler();
377 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
378 if (ctx->dump_bpf)
379 bpf_dump_all(&bpf_ops);
380 bpf_attach_to_sock(rx_sock, &bpf_ops);
382 setup_rx_ring_layout(rx_sock, &rx_ring, size_in, ctx->jumbo, false);
383 create_rx_ring(rx_sock, &rx_ring, ctx->verbose);
384 mmap_rx_ring(rx_sock, &rx_ring);
385 alloc_rx_ring_frames(rx_sock, &rx_ring);
386 bind_rx_ring(rx_sock, &rx_ring, ifindex_in);
387 prepare_polling(rx_sock, &rx_poll);
389 set_packet_loss_discard(tx_sock);
390 setup_tx_ring_layout(tx_sock, &tx_ring, size_out, ctx->jumbo);
391 create_tx_ring(tx_sock, &tx_ring, ctx->verbose);
392 mmap_tx_ring(tx_sock, &tx_ring);
393 alloc_tx_ring_frames(tx_sock, &tx_ring);
394 bind_tx_ring(tx_sock, &tx_ring, ifindex_out);
396 dissector_init_all(ctx->print_mode);
398 if (ctx->promiscuous)
399 ifflags = device_enter_promiscuous_mode(ctx->device_in);
401 if (ctx->kpull)
402 interval = ctx->kpull;
404 set_itimer_interval_value(&itimer, 0, interval);
405 setitimer(ITIMER_REAL, &itimer, NULL);
407 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
409 printf("Running! Hang up with ^C!\n\n");
410 fflush(stdout);
412 while (likely(sigint == 0)) {
413 while (user_may_pull_from_rx(rx_ring.frames[it_in].iov_base)) {
414 __label__ next;
416 hdr_in = rx_ring.frames[it_in].iov_base;
417 in = ((uint8_t *) hdr_in) + hdr_in->tp_h.tp_mac;
419 frame_count++;
421 if (ctx->packet_type != -1)
422 if (ctx->packet_type != hdr_in->s_ll.sll_pkttype)
423 goto next;
425 hdr_out = tx_ring.frames[it_out].iov_base;
426 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
428 for (; !user_may_pull_from_tx(tx_ring.frames[it_out].iov_base) &&
429 likely(!sigint);) {
430 if (ctx->randomize)
431 next_rnd_slot(&it_out, &tx_ring);
432 else {
433 it_out++;
434 if (it_out >= tx_ring.layout.tp_frame_nr)
435 it_out = 0;
438 hdr_out = tx_ring.frames[it_out].iov_base;
439 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
442 tpacket_hdr_clone(&hdr_out->tp_h, &hdr_in->tp_h);
443 fmemcpy(out, in, hdr_in->tp_h.tp_len);
445 kernel_may_pull_from_tx(&hdr_out->tp_h);
446 if (ctx->randomize)
447 next_rnd_slot(&it_out, &tx_ring);
448 else {
449 it_out++;
450 if (it_out >= tx_ring.layout.tp_frame_nr)
451 it_out = 0;
454 show_frame_hdr(hdr_in, ctx->print_mode);
456 dissector_entry_point(in, hdr_in->tp_h.tp_snaplen,
457 ctx->link_type, ctx->print_mode);
459 if (frame_count_max != 0) {
460 if (frame_count >= frame_count_max) {
461 sigint = 1;
462 break;
466 next:
468 kernel_may_pull_from_rx(&hdr_in->tp_h);
470 it_in++;
471 if (it_in >= rx_ring.layout.tp_frame_nr)
472 it_in = 0;
474 if (unlikely(sigint == 1))
475 goto out;
478 ret = poll(&rx_poll, 1, -1);
479 if (unlikely(ret < 0)) {
480 if (errno != EINTR)
481 panic("Poll failed!\n");
485 out:
487 timer_purge();
489 sock_rx_net_stats(rx_sock, 0);
491 bpf_release(&bpf_ops);
493 dissector_cleanup_all();
495 destroy_tx_ring(tx_sock, &tx_ring);
496 destroy_rx_ring(rx_sock, &rx_ring);
498 if (ctx->promiscuous)
499 device_leave_promiscuous_mode(ctx->device_in, ifflags);
501 close(tx_sock);
502 close(rx_sock);
505 static void translate_pcap_to_txf(int fdo, uint8_t *out, size_t len)
507 size_t bytes_done = 0;
508 char bout[80];
510 slprintf(bout, sizeof(bout), "{\n ");
511 write_or_die(fdo, bout, strlen(bout));
513 while (bytes_done < len) {
514 slprintf(bout, sizeof(bout), "0x%02x, ", out[bytes_done]);
515 write_or_die(fdo, bout, strlen(bout));
517 bytes_done++;
519 if (bytes_done % 10 == 0) {
520 slprintf(bout, sizeof(bout), "\n");
521 write_or_die(fdo, bout, strlen(bout));
523 if (bytes_done < len) {
524 slprintf(bout, sizeof(bout), " ");
525 write_or_die(fdo, bout, strlen(bout));
529 if (bytes_done % 10 != 0) {
530 slprintf(bout, sizeof(bout), "\n");
531 write_or_die(fdo, bout, strlen(bout));
534 slprintf(bout, sizeof(bout), "}\n\n");
535 write_or_die(fdo, bout, strlen(bout));
538 static void read_pcap(struct ctx *ctx)
540 __label__ out;
541 uint8_t *out;
542 int ret, fd, fdo = 0;
543 unsigned long trunced = 0;
544 size_t out_len;
545 pcap_pkthdr_t phdr;
546 struct sock_fprog bpf_ops;
547 struct frame_map fm;
548 struct timeval start, end, diff;
550 bug_on(!__pcap_io);
552 if (!strncmp("-", ctx->device_in, strlen("-"))) {
553 fd = dup_or_die(fileno(stdin));
554 close(fileno(stdin));
555 if (ctx->pcap == PCAP_OPS_MM)
556 ctx->pcap = PCAP_OPS_SG;
557 } else {
558 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
561 if (__pcap_io->init_once_pcap)
562 __pcap_io->init_once_pcap();
564 ret = __pcap_io->pull_fhdr_pcap(fd, &ctx->magic, &ctx->link_type);
565 if (ret)
566 panic("Error reading pcap header!\n");
568 if (__pcap_io->prepare_access_pcap) {
569 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_RD, ctx->jumbo);
570 if (ret)
571 panic("Error prepare reading pcap!\n");
574 fmemset(&fm, 0, sizeof(fm));
575 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
577 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
578 if (ctx->dump_bpf)
579 bpf_dump_all(&bpf_ops);
581 dissector_init_all(ctx->print_mode);
583 out_len = round_up(1024 * 1024, PAGE_SIZE);
584 out = xmalloc_aligned(out_len, CO_CACHE_LINE_SIZE);
586 if (ctx->device_out) {
587 if (!strncmp("-", ctx->device_out, strlen("-"))) {
588 fdo = dup_or_die(fileno(stdout));
589 close(fileno(stdout));
590 } else {
591 fdo = open_or_die_m(ctx->device_out, O_RDWR | O_CREAT |
592 O_TRUNC | O_LARGEFILE, DEFFILEMODE);
596 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
598 printf("Running! Hang up with ^C!\n\n");
599 fflush(stdout);
601 bug_on(gettimeofday(&start, NULL));
603 while (likely(sigint == 0)) {
604 do {
605 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic,
606 out, out_len);
607 if (unlikely(ret < 0))
608 goto out;
610 if (unlikely(pcap_get_length(&phdr, ctx->magic) == 0)) {
611 trunced++;
612 continue;
615 if (unlikely(pcap_get_length(&phdr, ctx->magic) > out_len)) {
616 pcap_set_length(&phdr, ctx->magic, out_len);
617 trunced++;
619 } while (ctx->filter &&
620 !bpf_run_filter(&bpf_ops, out,
621 pcap_get_length(&phdr, ctx->magic)));
623 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &fm.tp_h);
625 ctx->tx_bytes += fm.tp_h.tp_len;
626 ctx->tx_packets++;
628 show_frame_hdr(&fm, ctx->print_mode);
630 dissector_entry_point(out, fm.tp_h.tp_snaplen,
631 ctx->link_type, ctx->print_mode);
633 if (ctx->device_out)
634 translate_pcap_to_txf(fdo, out, fm.tp_h.tp_snaplen);
636 if (frame_count_max != 0) {
637 if (ctx->tx_packets >= frame_count_max) {
638 sigint = 1;
639 break;
644 out:
646 bug_on(gettimeofday(&end, NULL));
647 timersub(&end, &start, &diff);
649 bpf_release(&bpf_ops);
651 dissector_cleanup_all();
653 if (__pcap_io->prepare_close_pcap)
654 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
656 xfree(out);
658 fflush(stdout);
659 printf("\n");
660 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
661 printf("\r%12lu packets truncated in file\n", trunced);
662 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
663 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
665 if (!strncmp("-", ctx->device_in, strlen("-")))
666 dup2(fd, fileno(stdin));
667 close(fd);
669 if (ctx->device_out) {
670 if (!strncmp("-", ctx->device_out, strlen("-")))
671 dup2(fdo, fileno(stdout));
672 close(fdo);
676 static void finish_multi_pcap_file(struct ctx *ctx, int fd)
678 __pcap_io->fsync_pcap(fd);
680 if (__pcap_io->prepare_close_pcap)
681 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
683 close(fd);
685 fmemset(&itimer, 0, sizeof(itimer));
686 setitimer(ITIMER_REAL, &itimer, NULL);
689 static int next_multi_pcap_file(struct ctx *ctx, int fd)
691 int ret;
692 char fname[512];
694 __pcap_io->fsync_pcap(fd);
696 if (__pcap_io->prepare_close_pcap)
697 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
699 close(fd);
701 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
702 ctx->prefix ? : "dump-", time(NULL));
704 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
705 O_LARGEFILE, DEFFILEMODE);
707 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
708 if (ret)
709 panic("Error writing pcap header!\n");
711 if (__pcap_io->prepare_access_pcap) {
712 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, true);
713 if (ret)
714 panic("Error prepare writing pcap!\n");
717 return fd;
720 static int begin_multi_pcap_file(struct ctx *ctx)
722 int fd, ret;
723 char fname[256];
725 bug_on(!__pcap_io);
727 if (ctx->device_out[strlen(ctx->device_out) - 1] == '/')
728 ctx->device_out[strlen(ctx->device_out) - 1] = 0;
730 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
731 ctx->prefix ? : "dump-", time(NULL));
733 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
734 O_LARGEFILE, DEFFILEMODE);
736 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
737 if (ret)
738 panic("Error writing pcap header!\n");
740 if (__pcap_io->prepare_access_pcap) {
741 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, true);
742 if (ret)
743 panic("Error prepare writing pcap!\n");
746 if (ctx->dump_mode == DUMP_INTERVAL_TIME) {
747 interval = ctx->dump_interval;
749 set_itimer_interval_value(&itimer, interval, 0);
750 setitimer(ITIMER_REAL, &itimer, NULL);
751 } else {
752 interval = 0;
755 return fd;
758 static void finish_single_pcap_file(struct ctx *ctx, int fd)
760 __pcap_io->fsync_pcap(fd);
762 if (__pcap_io->prepare_close_pcap)
763 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
765 if (strncmp("-", ctx->device_out, strlen("-")))
766 close(fd);
767 else
768 dup2(fd, fileno(stdout));
771 static int begin_single_pcap_file(struct ctx *ctx)
773 int fd, ret;
775 bug_on(!__pcap_io);
777 if (!strncmp("-", ctx->device_out, strlen("-"))) {
778 fd = dup_or_die(fileno(stdout));
779 close(fileno(stdout));
780 if (ctx->pcap == PCAP_OPS_MM)
781 ctx->pcap = PCAP_OPS_SG;
782 } else {
783 fd = open_or_die_m(ctx->device_out,
784 O_RDWR | O_CREAT | O_TRUNC |
785 O_LARGEFILE, DEFFILEMODE);
788 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
789 if (ret)
790 panic("Error writing pcap header!\n");
792 if (__pcap_io->prepare_access_pcap) {
793 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, true);
794 if (ret)
795 panic("Error prepare writing pcap!\n");
798 return fd;
801 static void print_pcap_file_stats(int sock, struct ctx *ctx)
803 int ret;
804 struct tpacket_stats kstats;
805 socklen_t slen = sizeof(kstats);
807 fmemset(&kstats, 0, sizeof(kstats));
809 ret = getsockopt(sock, SOL_PACKET, PACKET_STATISTICS, &kstats, &slen);
810 if (unlikely(ret))
811 panic("Cannot get packet statistics!\n");
813 if (ctx->print_mode == PRINT_NONE) {
814 printf(".(+%u/-%u)", kstats.tp_packets - kstats.tp_drops,
815 kstats.tp_drops);
816 fflush(stdout);
820 static void walk_t3_block(struct block_desc *pbd, struct ctx *ctx,
821 int sock, int *fd, unsigned long *frame_count)
823 uint8_t *packet;
824 int num_pkts = pbd->h1.num_pkts, i, ret;
825 struct tpacket3_hdr *hdr;
826 pcap_pkthdr_t phdr;
827 struct sockaddr_ll *sll;
829 hdr = (void *) ((uint8_t *) pbd + pbd->h1.offset_to_first_pkt);
830 sll = (void *) ((uint8_t *) hdr + TPACKET_ALIGN(sizeof(*hdr)));
832 for (i = 0; i < num_pkts && likely(sigint == 0); ++i) {
833 __label__ next;
834 packet = ((uint8_t *) hdr + hdr->tp_mac);
836 if (ctx->packet_type != -1)
837 if (ctx->packet_type != sll->sll_pkttype)
838 goto next;
840 (*frame_count)++;
842 if (dump_to_pcap(ctx)) {
843 tpacket3_hdr_to_pcap_pkthdr(hdr, sll, &phdr, ctx->magic);
845 ret = __pcap_io->write_pcap(*fd, &phdr, ctx->magic, packet,
846 pcap_get_length(&phdr, ctx->magic));
847 if (unlikely(ret != (int) pcap_get_total_length(&phdr, ctx->magic)))
848 panic("Write error to pcap!\n");
851 __show_frame_hdr(sll, hdr, ctx->print_mode, true);
853 dissector_entry_point(packet, hdr->tp_snaplen, ctx->link_type,
854 ctx->print_mode);
855 next:
857 hdr = (void *) ((uint8_t *) hdr + hdr->tp_next_offset);
858 sll = (void *) ((uint8_t *) hdr + TPACKET_ALIGN(sizeof(*hdr)));
860 if (frame_count_max != 0) {
861 if (unlikely(*frame_count >= frame_count_max)) {
862 sigint = 1;
863 break;
867 if (dump_to_pcap(ctx)) {
868 if (ctx->dump_mode == DUMP_INTERVAL_SIZE) {
869 interval += hdr->tp_snaplen;
870 if (interval > ctx->dump_interval) {
871 next_dump = true;
872 interval = 0;
876 if (next_dump) {
877 *fd = next_multi_pcap_file(ctx, *fd);
878 next_dump = false;
880 if (unlikely(ctx->verbose))
881 print_pcap_file_stats(sock, ctx);
887 static void recv_only_or_dump(struct ctx *ctx)
889 short ifflags = 0;
890 int sock, irq, ifindex, fd = 0, ret;
891 unsigned int size, it = 0;
892 struct ring rx_ring;
893 struct pollfd rx_poll;
894 struct sock_fprog bpf_ops;
895 struct timeval start, end, diff;
896 struct block_desc *pbd;
897 unsigned long frame_count = 0;
899 sock = pf_socket();
901 if (ctx->rfraw) {
902 ctx->device_trans = xstrdup(ctx->device_in);
903 xfree(ctx->device_in);
905 enter_rfmon_mac80211(ctx->device_trans, &ctx->device_in);
906 ctx->link_type = LINKTYPE_IEEE802_11;
909 fmemset(&rx_ring, 0, sizeof(rx_ring));
910 fmemset(&rx_poll, 0, sizeof(rx_poll));
911 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
913 ifindex = device_ifindex(ctx->device_in);
915 size = ring_size(ctx->device_in, ctx->reserve_size);
917 enable_kernel_bpf_jit_compiler();
919 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
920 if (ctx->dump_bpf)
921 bpf_dump_all(&bpf_ops);
922 bpf_attach_to_sock(sock, &bpf_ops);
924 ret = set_sockopt_hwtimestamp(sock, ctx->device_in);
925 if (ret == 0 && ctx->verbose)
926 printf("HW timestamping enabled\n");
928 setup_rx_ring_layout(sock, &rx_ring, size, true, true);
929 create_rx_ring(sock, &rx_ring, ctx->verbose);
930 mmap_rx_ring(sock, &rx_ring);
931 alloc_rx_ring_frames(sock, &rx_ring);
932 bind_rx_ring(sock, &rx_ring, ifindex);
934 prepare_polling(sock, &rx_poll);
935 dissector_init_all(ctx->print_mode);
937 if (ctx->cpu >= 0 && ifindex > 0) {
938 irq = device_irq_number(ctx->device_in);
939 device_set_irq_affinity(irq, ctx->cpu);
941 if (ctx->verbose)
942 printf("IRQ: %s:%d > CPU%d\n",
943 ctx->device_in, irq, ctx->cpu);
946 if (ctx->promiscuous)
947 ifflags = device_enter_promiscuous_mode(ctx->device_in);
949 if (dump_to_pcap(ctx) && __pcap_io->init_once_pcap)
950 __pcap_io->init_once_pcap();
952 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
954 if (dump_to_pcap(ctx)) {
955 struct stat stats;
957 ret = stat(ctx->device_out, &stats);
958 if (ret < 0)
959 ctx->dump_dir = 0;
960 else
961 ctx->dump_dir = S_ISDIR(stats.st_mode);
963 if (ctx->dump_dir)
964 fd = begin_multi_pcap_file(ctx);
965 else
966 fd = begin_single_pcap_file(ctx);
969 printf("Running! Hang up with ^C!\n\n");
970 fflush(stdout);
972 bug_on(gettimeofday(&start, NULL));
974 while (likely(sigint == 0)) {
975 while (user_may_pull_from_rx_block((pbd = (void *)
976 rx_ring.frames[it].iov_base))) {
977 walk_t3_block(pbd, ctx, sock, &fd, &frame_count);
979 kernel_may_pull_from_rx_block(pbd);
980 it = (it + 1) % rx_ring.layout3.tp_block_nr;
982 if (unlikely(sigint == 1))
983 break;
986 ret = poll(&rx_poll, 1, -1);
987 if (unlikely(ret < 0)) {
988 if (errno != EINTR)
989 panic("Poll failed!\n");
993 bug_on(gettimeofday(&end, NULL));
994 timersub(&end, &start, &diff);
996 if (!(ctx->dump_dir && ctx->print_mode == PRINT_NONE)) {
997 sock_rx_net_stats(sock, frame_count);
999 printf("\r%12lu sec, %lu usec in total\n",
1000 diff.tv_sec, diff.tv_usec);
1001 } else {
1002 printf("\n\n");
1003 fflush(stdout);
1006 bpf_release(&bpf_ops);
1007 dissector_cleanup_all();
1008 destroy_rx_ring(sock, &rx_ring);
1010 if (ctx->promiscuous)
1011 device_leave_promiscuous_mode(ctx->device_in, ifflags);
1013 if (ctx->rfraw)
1014 leave_rfmon_mac80211(ctx->device_in);
1016 if (dump_to_pcap(ctx)) {
1017 if (ctx->dump_dir)
1018 finish_multi_pcap_file(ctx, fd);
1019 else
1020 finish_single_pcap_file(ctx, fd);
1023 close(sock);
1026 static void init_ctx(struct ctx *ctx)
1028 memset(ctx, 0, sizeof(*ctx));
1029 ctx->uid = getuid();
1030 ctx->uid = getgid();
1032 ctx->cpu = -1;
1033 ctx->packet_type = -1;
1035 ctx->magic = ORIGINAL_TCPDUMP_MAGIC;
1036 ctx->print_mode = PRINT_NORM;
1037 ctx->pcap = PCAP_OPS_SG;
1039 ctx->dump_mode = DUMP_INTERVAL_TIME;
1040 ctx->dump_interval = 60;
1042 ctx->promiscuous = true;
1043 ctx->randomize = false;
1046 static void destroy_ctx(struct ctx *ctx)
1048 free(ctx->device_in);
1049 free(ctx->device_out);
1050 free(ctx->device_trans);
1052 free(ctx->prefix);
1055 static void __noreturn help(void)
1057 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING);
1058 puts("http://www.netsniff-ng.org\n\n"
1059 "Usage: netsniff-ng [options] [filter-expression]\n"
1060 "Options:\n"
1061 " -i|-d|--dev|--in <dev|pcap|-> Input source as netdev, pcap or pcap stdin\n"
1062 " -o|--out <dev|pcap|dir|cfg|-> Output sink as netdev, pcap, directory, trafgen, or stdout\n"
1063 " -f|--filter <bpf-file|expr> Use BPF filter file from bpfc or tcpdump-like expression\n"
1064 " -t|--type <type> Filter for: host|broadcast|multicast|others|outgoing\n"
1065 " -F|--interval <size|time> Dump interval if -o is a dir: <num>KiB/MiB/GiB/s/sec/min/hrs\n"
1066 " -R|--rfraw Capture or inject raw 802.11 frames\n"
1067 " -n|--num <0|uint> Number of packets until exit (def: 0)\n"
1068 " -P|--prefix <name> Prefix for pcaps stored in directory\n"
1069 " -T|--magic <pcap-magic> Pcap magic number/pcap format to store, see -D\n"
1070 " -D|--dump-pcap-types Dump pcap types and magic numbers and quit\n"
1071 " -B|--dump-bpf Dump generated BPF assembly\n"
1072 " -r|--rand Randomize packet forwarding order (dev->dev)\n"
1073 " -M|--no-promisc No promiscuous mode for netdev\n"
1074 " -A|--no-sock-mem Don't tune core socket memory\n"
1075 " -m|--mmap Mmap(2) pcap file I/O, e.g. for replaying pcaps\n"
1076 " -G|--sg Scatter/gather pcap file I/O\n"
1077 " -c|--clrw Use slower read(2)/write(2) I/O\n"
1078 " -S|--ring-size <size> Specify ring size to: <num>KiB/MiB/GiB\n"
1079 " -k|--kernel-pull <uint> Kernel pull from user interval in us (def: 10us)\n"
1080 " -J|--jumbo-support Support replay/fwd 64KB Super Jumbo Frames (def: 2048B)\n"
1081 " -b|--bind-cpu <cpu> Bind to specific CPU\n"
1082 " -u|--user <userid> Drop privileges and change to userid\n"
1083 " -g|--group <groupid> Drop privileges and change to groupid\n"
1084 " -H|--prio-high Make this high priority process\n"
1085 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
1086 " -s|--silent Do not print captured packets\n"
1087 " -q|--less Print less-verbose packet information\n"
1088 " -X|--hex Print packet data in hex format\n"
1089 " -l|--ascii Print human-readable packet data\n"
1090 " -U|--update Update GeoIP databases\n"
1091 " -V|--verbose Be more verbose\n"
1092 " -v|--version Show version and exit\n"
1093 " -h|--help Guess what?!\n\n"
1094 "Examples:\n"
1095 " netsniff-ng --in eth0 --out dump.pcap -s -T 0xa1b2c3d4 --b 0 tcp or udp\n"
1096 " netsniff-ng --in wlan0 --rfraw --out dump.pcap --silent --bind-cpu 0\n"
1097 " netsniff-ng --in dump.pcap --mmap --out eth0 -k1000 --silent --bind-cpu 0\n"
1098 " netsniff-ng --in dump.pcap --out dump.cfg --silent --bind-cpu 0\n"
1099 " netsniff-ng --in eth0 --out eth1 --silent --bind-cpu 0 -J --type host\n"
1100 " netsniff-ng --in eth1 --out /opt/probe/ -s -m --interval 100MiB -b 0\n"
1101 " netsniff-ng --in vlan0 --out dump.pcap -c -u `id -u bob` -g `id -g bob`\n"
1102 " netsniff-ng --in any --filter http.bpf --jumbo-support --ascii -V\n\n"
1103 "Note:\n"
1104 " For introducing bit errors, delays with random variation and more\n"
1105 " while replaying pcaps, make use of tc(8) with its disciplines (e.g. netem).\n\n"
1106 "Please report bugs to <bugs@netsniff-ng.org>\n"
1107 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1108 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1109 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1110 "Swiss federal institute of technology (ETH Zurich)\n"
1111 "License: GNU GPL version 2.0\n"
1112 "This is free software: you are free to change and redistribute it.\n"
1113 "There is NO WARRANTY, to the extent permitted by law.\n");
1114 die();
1117 static void __noreturn version(void)
1119 printf("\nnetsniff-ng %s, Git id: %s\n", VERSION_LONG, GITVERSION);
1120 puts("the packet sniffing beast\n"
1121 "http://www.netsniff-ng.org\n\n"
1122 "Please report bugs to <bugs@netsniff-ng.org>\n"
1123 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1124 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1125 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1126 "Swiss federal institute of technology (ETH Zurich)\n"
1127 "License: GNU GPL version 2.0\n"
1128 "This is free software: you are free to change and redistribute it.\n"
1129 "There is NO WARRANTY, to the extent permitted by law.\n");
1130 die();
1133 int main(int argc, char **argv)
1135 char *ptr;
1136 int c, i, j, cpu_tmp, opt_index, ops_touched = 0, vals[4] = {0};
1137 bool prio_high = false, setsockmem = true;
1138 void (*main_loop)(struct ctx *ctx) = NULL;
1139 struct ctx ctx;
1141 init_ctx(&ctx);
1142 srand(time(NULL));
1144 while ((c = getopt_long(argc, argv, short_options, long_options,
1145 &opt_index)) != EOF) {
1146 switch (c) {
1147 case 'd':
1148 case 'i':
1149 ctx.device_in = xstrdup(optarg);
1150 break;
1151 case 'o':
1152 ctx.device_out = xstrdup(optarg);
1153 break;
1154 case 'P':
1155 ctx.prefix = xstrdup(optarg);
1156 break;
1157 case 'R':
1158 ctx.link_type = LINKTYPE_IEEE802_11;
1159 ctx.rfraw = 1;
1160 break;
1161 case 'r':
1162 ctx.randomize = true;
1163 break;
1164 case 'J':
1165 ctx.jumbo = true;
1166 break;
1167 case 'T':
1168 ctx.magic = (uint32_t) strtoul(optarg, NULL, 0);
1169 pcap_check_magic(ctx.magic);
1170 break;
1171 case 'f':
1172 ctx.filter = xstrdup(optarg);
1173 break;
1174 case 'M':
1175 ctx.promiscuous = false;
1176 break;
1177 case 'A':
1178 setsockmem = false;
1179 break;
1180 case 'u':
1181 ctx.uid = strtoul(optarg, NULL, 0);
1182 ctx.enforce = true;
1183 break;
1184 case 'g':
1185 ctx.gid = strtoul(optarg, NULL, 0);
1186 ctx.enforce = true;
1187 break;
1188 case 't':
1189 if (!strncmp(optarg, "host", strlen("host")))
1190 ctx.packet_type = PACKET_HOST;
1191 else if (!strncmp(optarg, "broadcast", strlen("broadcast")))
1192 ctx.packet_type = PACKET_BROADCAST;
1193 else if (!strncmp(optarg, "multicast", strlen("multicast")))
1194 ctx.packet_type = PACKET_MULTICAST;
1195 else if (!strncmp(optarg, "others", strlen("others")))
1196 ctx.packet_type = PACKET_OTHERHOST;
1197 else if (!strncmp(optarg, "outgoing", strlen("outgoing")))
1198 ctx.packet_type = PACKET_OUTGOING;
1199 else
1200 ctx.packet_type = -1;
1201 break;
1202 case 'S':
1203 ptr = optarg;
1204 ctx.reserve_size = 0;
1206 for (j = i = strlen(optarg); i > 0; --i) {
1207 if (!isdigit(optarg[j - i]))
1208 break;
1209 ptr++;
1212 if (!strncmp(ptr, "KiB", strlen("KiB")))
1213 ctx.reserve_size = 1 << 10;
1214 else if (!strncmp(ptr, "MiB", strlen("MiB")))
1215 ctx.reserve_size = 1 << 20;
1216 else if (!strncmp(ptr, "GiB", strlen("GiB")))
1217 ctx.reserve_size = 1 << 30;
1218 else
1219 panic("Syntax error in ring size param!\n");
1221 ctx.reserve_size *= strtoul(optarg, NULL, 0);
1222 break;
1223 case 'b':
1224 cpu_tmp = strtol(optarg, NULL, 0);
1226 cpu_affinity(cpu_tmp);
1227 if (ctx.cpu != -2)
1228 ctx.cpu = cpu_tmp;
1229 break;
1230 case 'H':
1231 prio_high = true;
1232 break;
1233 case 'c':
1234 ctx.pcap = PCAP_OPS_RW;
1235 ops_touched = 1;
1236 break;
1237 case 'm':
1238 ctx.pcap = PCAP_OPS_MM;
1239 ops_touched = 1;
1240 break;
1241 case 'G':
1242 ctx.pcap = PCAP_OPS_SG;
1243 ops_touched = 1;
1244 break;
1245 case 'Q':
1246 ctx.cpu = -2;
1247 break;
1248 case 's':
1249 ctx.print_mode = PRINT_NONE;
1250 break;
1251 case 'q':
1252 ctx.print_mode = PRINT_LESS;
1253 break;
1254 case 'X':
1255 ctx.print_mode =
1256 (ctx.print_mode == PRINT_ASCII) ?
1257 PRINT_HEX_ASCII : PRINT_HEX;
1258 break;
1259 case 'l':
1260 ctx.print_mode =
1261 (ctx.print_mode == PRINT_HEX) ?
1262 PRINT_HEX_ASCII : PRINT_ASCII;
1263 break;
1264 case 'k':
1265 ctx.kpull = strtoul(optarg, NULL, 0);
1266 break;
1267 case 'n':
1268 frame_count_max = strtoul(optarg, NULL, 0);
1269 break;
1270 case 'F':
1271 ptr = optarg;
1272 ctx.dump_interval = 0;
1274 for (j = i = strlen(optarg); i > 0; --i) {
1275 if (!isdigit(optarg[j - i]))
1276 break;
1277 ptr++;
1280 if (!strncmp(ptr, "KiB", strlen("KiB"))) {
1281 ctx.dump_interval = 1 << 10;
1282 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1283 } else if (!strncmp(ptr, "MiB", strlen("MiB"))) {
1284 ctx.dump_interval = 1 << 20;
1285 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1286 } else if (!strncmp(ptr, "GiB", strlen("GiB"))) {
1287 ctx.dump_interval = 1 << 30;
1288 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1289 } else if (!strncmp(ptr, "sec", strlen("sec"))) {
1290 ctx.dump_interval = 1;
1291 ctx.dump_mode = DUMP_INTERVAL_TIME;
1292 } else if (!strncmp(ptr, "min", strlen("min"))) {
1293 ctx.dump_interval = 60;
1294 ctx.dump_mode = DUMP_INTERVAL_TIME;
1295 } else if (!strncmp(ptr, "hrs", strlen("hrs"))) {
1296 ctx.dump_interval = 60 * 60;
1297 ctx.dump_mode = DUMP_INTERVAL_TIME;
1298 } else if (!strncmp(ptr, "s", strlen("s"))) {
1299 ctx.dump_interval = 1;
1300 ctx.dump_mode = DUMP_INTERVAL_TIME;
1301 } else {
1302 panic("Syntax error in time/size param!\n");
1305 ctx.dump_interval *= strtoul(optarg, NULL, 0);
1306 break;
1307 case 'V':
1308 ctx.verbose = 1;
1309 break;
1310 case 'B':
1311 ctx.dump_bpf = true;
1312 break;
1313 case 'D':
1314 pcap_dump_type_features();
1315 die();
1316 break;
1317 case 'U':
1318 update_geoip();
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(SIGQUIT, signal_handler);
1378 register_signal(SIGTERM, signal_handler);
1379 register_signal(SIGHUP, signal_handler);
1381 tprintf_init();
1383 if (prio_high) {
1384 set_proc_prio(-20);
1385 set_sched_status(SCHED_FIFO, sched_get_priority_max(SCHED_FIFO));
1388 if (ctx.device_in && (device_mtu(ctx.device_in) ||
1389 !strncmp("any", ctx.device_in, strlen(ctx.device_in)))) {
1390 if (!ctx.rfraw)
1391 ctx.link_type = pcap_devtype_to_linktype(ctx.device_in);
1392 if (!ctx.device_out) {
1393 ctx.dump = 0;
1394 main_loop = recv_only_or_dump;
1395 } else if (device_mtu(ctx.device_out)) {
1396 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1397 main_loop = receive_to_xmit;
1398 } else {
1399 ctx.dump = 1;
1400 register_signal_f(SIGALRM, timer_next_dump, SA_SIGINFO);
1401 main_loop = recv_only_or_dump;
1402 if (!ops_touched)
1403 ctx.pcap = PCAP_OPS_SG;
1405 } else {
1406 if (ctx.device_out && device_mtu(ctx.device_out)) {
1407 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1408 main_loop = pcap_to_xmit;
1409 if (!ops_touched)
1410 ctx.pcap = PCAP_OPS_MM;
1411 } else {
1412 main_loop = read_pcap;
1413 if (!ops_touched)
1414 ctx.pcap = PCAP_OPS_SG;
1418 bug_on(!main_loop);
1420 init_geoip(0);
1421 if (setsockmem)
1422 set_system_socket_memory(vals, array_size(vals));
1423 if (!ctx.enforce)
1424 xlockme();
1426 if (ctx.verbose)
1427 printf("pcap file I/O method: %s\n", pcap_ops_group_to_str[ctx.pcap]);
1429 main_loop(&ctx);
1431 if (!ctx.enforce)
1432 xunlockme();
1433 if (setsockmem)
1434 reset_system_socket_memory(vals, array_size(vals));
1435 destroy_geoip();
1437 device_restore_irq_affinity_list();
1438 tprintf_cleanup();
1440 destroy_ctx(&ctx);
1441 return 0;