ifpps: Remove unnecessary memset()
[netsniff-ng.git] / netsniff-ng.c
blob7f03d4b5d0f54e5f202c79908d0256194de31630
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 #define NEED_TCPDUMP_LIKE_FILTER
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <signal.h>
14 #include <getopt.h>
15 #include <ctype.h>
16 #include <time.h>
17 #include <string.h>
18 #include <sys/socket.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <sys/time.h>
22 #include <sys/fsuid.h>
23 #include <unistd.h>
24 #include <stdbool.h>
25 #include <pthread.h>
26 #include <fcntl.h>
28 #include "ring_rx.h"
29 #include "ring_tx.h"
30 #include "mac80211.h"
31 #include "promisc.h"
32 #include "built_in.h"
33 #include "pcap_io.h"
34 #include "privs.h"
35 #include "proc.h"
36 #include "bpf.h"
37 #include "ioops.h"
38 #include "die.h"
39 #include "irq.h"
40 #include "str.h"
41 #include "sig.h"
42 #include "config.h"
43 #include "sock.h"
44 #include "geoip.h"
45 #include "lockme.h"
46 #include "tprintf.h"
47 #include "timer.h"
48 #include "tstamping.h"
49 #include "dissector.h"
50 #include "xmalloc.h"
52 enum dump_mode {
53 DUMP_INTERVAL_TIME,
54 DUMP_INTERVAL_SIZE,
57 struct ctx {
58 char *device_in, *device_out, *device_trans, *filter, *prefix;
59 int cpu, rfraw, dump, print_mode, dump_dir, packet_type, verbose;
60 unsigned long kpull, dump_interval, reserve_size, tx_bytes, tx_packets;
61 bool randomize, promiscuous, enforce, jumbo, dump_bpf;
62 enum pcap_ops_groups pcap; enum dump_mode dump_mode;
63 uid_t uid; gid_t gid; uint32_t link_type, magic;
66 static volatile sig_atomic_t sigint = 0;
67 static volatile bool next_dump = false;
69 static const char *short_options = "d:i:o:rf:MJt:S:k:n:b:HQmcsqXlvhF:RGAP:Vu:g:T:DB";
70 static const struct option long_options[] = {
71 {"dev", required_argument, NULL, 'd'},
72 {"in", required_argument, NULL, 'i'},
73 {"out", required_argument, NULL, 'o'},
74 {"filter", required_argument, NULL, 'f'},
75 {"num", required_argument, NULL, 'n'},
76 {"type", required_argument, NULL, 't'},
77 {"interval", required_argument, NULL, 'F'},
78 {"ring-size", required_argument, NULL, 'S'},
79 {"kernel-pull", required_argument, NULL, 'k'},
80 {"bind-cpu", required_argument, NULL, 'b'},
81 {"prefix", required_argument, NULL, 'P'},
82 {"user", required_argument, NULL, 'u'},
83 {"group", required_argument, NULL, 'g'},
84 {"magic", required_argument, NULL, 'T'},
85 {"rand", no_argument, NULL, 'r'},
86 {"rfraw", no_argument, NULL, 'R'},
87 {"mmap", no_argument, NULL, 'm'},
88 {"sg", no_argument, NULL, 'G'},
89 {"clrw", no_argument, NULL, 'c'},
90 {"jumbo-support", no_argument, NULL, 'J'},
91 {"no-promisc", no_argument, NULL, 'M'},
92 {"prio-high", no_argument, NULL, 'H'},
93 {"notouch-irq", no_argument, NULL, 'Q'},
94 {"dump-pcap-types", no_argument, NULL, 'D'},
95 {"dump-bpf", no_argument, NULL, 'B'},
96 {"silent", no_argument, NULL, 's'},
97 {"less", no_argument, NULL, 'q'},
98 {"hex", no_argument, NULL, 'X'},
99 {"ascii", no_argument, NULL, 'l'},
100 {"no-sock-mem", no_argument, NULL, 'A'},
101 {"update", no_argument, NULL, 'U'},
102 {"verbose", no_argument, NULL, 'V'},
103 {"version", no_argument, NULL, 'v'},
104 {"help", no_argument, NULL, 'h'},
105 {NULL, 0, NULL, 0}
108 static int tx_sock;
109 static struct itimerval itimer;
110 static unsigned long frame_count_max = 0, interval = TX_KERNEL_PULL_INT;
112 #define __pcap_io pcap_ops[ctx->pcap]
114 static void signal_handler(int number)
116 switch (number) {
117 case SIGINT:
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, &hdr->s_ll);
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_trans, 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;
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 = 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 poll(&rx_poll, 1, -1);
481 out:
483 timer_purge();
485 sock_rx_net_stats(rx_sock, 0);
487 bpf_release(&bpf_ops);
489 dissector_cleanup_all();
491 destroy_tx_ring(tx_sock, &tx_ring);
492 destroy_rx_ring(rx_sock, &rx_ring);
494 if (ctx->promiscuous)
495 leave_promiscuous_mode(ctx->device_in, ifflags);
497 close(tx_sock);
498 close(rx_sock);
501 static void translate_pcap_to_txf(int fdo, uint8_t *out, size_t len)
503 size_t bytes_done = 0;
504 char bout[80];
506 slprintf(bout, sizeof(bout), "{\n ");
507 write_or_die(fdo, bout, strlen(bout));
509 while (bytes_done < len) {
510 slprintf(bout, sizeof(bout), "0x%02x, ", out[bytes_done]);
511 write_or_die(fdo, bout, strlen(bout));
513 bytes_done++;
515 if (bytes_done % 10 == 0) {
516 slprintf(bout, sizeof(bout), "\n");
517 write_or_die(fdo, bout, strlen(bout));
519 if (bytes_done < len) {
520 slprintf(bout, sizeof(bout), " ");
521 write_or_die(fdo, bout, strlen(bout));
525 if (bytes_done % 10 != 0) {
526 slprintf(bout, sizeof(bout), "\n");
527 write_or_die(fdo, bout, strlen(bout));
530 slprintf(bout, sizeof(bout), "}\n\n");
531 write_or_die(fdo, bout, strlen(bout));
534 static void read_pcap(struct ctx *ctx)
536 __label__ out;
537 uint8_t *out;
538 int ret, fd, fdo = 0;
539 unsigned long trunced = 0;
540 size_t out_len;
541 pcap_pkthdr_t phdr;
542 struct sock_fprog bpf_ops;
543 struct frame_map fm;
544 struct timeval start, end, diff;
545 struct sockaddr_ll sll;
547 bug_on(!__pcap_io);
549 if (!strncmp("-", ctx->device_in, strlen("-"))) {
550 fd = dup_or_die(fileno(stdin));
551 close(fileno(stdin));
552 if (ctx->pcap == PCAP_OPS_MM)
553 ctx->pcap = PCAP_OPS_SG;
554 } else {
555 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
558 if (__pcap_io->init_once_pcap)
559 __pcap_io->init_once_pcap();
561 ret = __pcap_io->pull_fhdr_pcap(fd, &ctx->magic, &ctx->link_type);
562 if (ret)
563 panic("Error reading pcap header!\n");
565 if (__pcap_io->prepare_access_pcap) {
566 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_RD, ctx->jumbo);
567 if (ret)
568 panic("Error prepare reading pcap!\n");
571 fmemset(&fm, 0, sizeof(fm));
572 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
574 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
575 if (ctx->dump_bpf)
576 bpf_dump_all(&bpf_ops);
578 dissector_init_all(ctx->print_mode);
580 out_len = round_up(1024 * 1024, PAGE_SIZE);
581 out = xmalloc_aligned(out_len, CO_CACHE_LINE_SIZE);
583 if (ctx->device_out) {
584 if (!strncmp("-", ctx->device_out, strlen("-"))) {
585 fdo = dup_or_die(fileno(stdout));
586 close(fileno(stdout));
587 } else {
588 fdo = open_or_die_m(ctx->device_out, O_RDWR | O_CREAT |
589 O_TRUNC | O_LARGEFILE, DEFFILEMODE);
593 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
595 printf("Running! Hang up with ^C!\n\n");
596 fflush(stdout);
598 bug_on(gettimeofday(&start, NULL));
600 while (likely(sigint == 0)) {
601 do {
602 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic,
603 out, out_len);
604 if (unlikely(ret < 0))
605 goto out;
607 if (unlikely(pcap_get_length(&phdr, ctx->magic) == 0)) {
608 trunced++;
609 continue;
612 if (unlikely(pcap_get_length(&phdr, ctx->magic) > out_len)) {
613 pcap_set_length(&phdr, ctx->magic, out_len);
614 trunced++;
616 } while (ctx->filter &&
617 !bpf_run_filter(&bpf_ops, out,
618 pcap_get_length(&phdr, ctx->magic)));
620 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &fm.tp_h, &sll);
622 ctx->tx_bytes += fm.tp_h.tp_len;
623 ctx->tx_packets++;
625 show_frame_hdr(&fm, ctx->print_mode);
627 dissector_entry_point(out, fm.tp_h.tp_snaplen,
628 ctx->link_type, ctx->print_mode);
630 if (ctx->device_out)
631 translate_pcap_to_txf(fdo, out, fm.tp_h.tp_snaplen);
633 if (frame_count_max != 0) {
634 if (ctx->tx_packets >= frame_count_max) {
635 sigint = 1;
636 break;
641 out:
643 bug_on(gettimeofday(&end, NULL));
644 timersub(&end, &start, &diff);
646 bpf_release(&bpf_ops);
648 dissector_cleanup_all();
650 if (__pcap_io->prepare_close_pcap)
651 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
653 xfree(out);
655 fflush(stdout);
656 printf("\n");
657 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
658 printf("\r%12lu packets truncated in file\n", trunced);
659 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
660 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
662 if (!strncmp("-", ctx->device_in, strlen("-")))
663 dup2(fd, fileno(stdin));
664 close(fd);
666 if (ctx->device_out) {
667 if (!strncmp("-", ctx->device_out, strlen("-")))
668 dup2(fdo, fileno(stdout));
669 close(fdo);
673 static void finish_multi_pcap_file(struct ctx *ctx, int fd)
675 __pcap_io->fsync_pcap(fd);
677 if (__pcap_io->prepare_close_pcap)
678 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
680 close(fd);
682 fmemset(&itimer, 0, sizeof(itimer));
683 setitimer(ITIMER_REAL, &itimer, NULL);
686 static int next_multi_pcap_file(struct ctx *ctx, int fd)
688 int ret;
689 char fname[512];
691 __pcap_io->fsync_pcap(fd);
693 if (__pcap_io->prepare_close_pcap)
694 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
696 close(fd);
698 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
699 ctx->prefix ? : "dump-", time(0));
701 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
702 O_LARGEFILE, DEFFILEMODE);
704 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
705 if (ret)
706 panic("Error writing pcap header!\n");
708 if (__pcap_io->prepare_access_pcap) {
709 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, true);
710 if (ret)
711 panic("Error prepare writing pcap!\n");
714 return fd;
717 static int begin_multi_pcap_file(struct ctx *ctx)
719 int fd, ret;
720 char fname[256];
722 bug_on(!__pcap_io);
724 if (ctx->device_out[strlen(ctx->device_out) - 1] == '/')
725 ctx->device_out[strlen(ctx->device_out) - 1] = 0;
727 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
728 ctx->prefix ? : "dump-", time(0));
730 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
731 O_LARGEFILE, DEFFILEMODE);
733 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
734 if (ret)
735 panic("Error writing pcap header!\n");
737 if (__pcap_io->prepare_access_pcap) {
738 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, true);
739 if (ret)
740 panic("Error prepare writing pcap!\n");
743 if (ctx->dump_mode == DUMP_INTERVAL_TIME) {
744 interval = ctx->dump_interval;
746 set_itimer_interval_value(&itimer, interval, 0);
747 setitimer(ITIMER_REAL, &itimer, NULL);
748 } else {
749 interval = 0;
752 return fd;
755 static void finish_single_pcap_file(struct ctx *ctx, int fd)
757 __pcap_io->fsync_pcap(fd);
759 if (__pcap_io->prepare_close_pcap)
760 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
762 if (strncmp("-", ctx->device_out, strlen("-")))
763 close(fd);
764 else
765 dup2(fd, fileno(stdout));
768 static int begin_single_pcap_file(struct ctx *ctx)
770 int fd, ret;
772 bug_on(!__pcap_io);
774 if (!strncmp("-", ctx->device_out, strlen("-"))) {
775 fd = dup_or_die(fileno(stdout));
776 close(fileno(stdout));
777 if (ctx->pcap == PCAP_OPS_MM)
778 ctx->pcap = PCAP_OPS_SG;
779 } else {
780 fd = open_or_die_m(ctx->device_out,
781 O_RDWR | O_CREAT | O_TRUNC |
782 O_LARGEFILE, DEFFILEMODE);
785 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
786 if (ret)
787 panic("Error writing pcap header!\n");
789 if (__pcap_io->prepare_access_pcap) {
790 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, true);
791 if (ret)
792 panic("Error prepare writing pcap!\n");
795 return fd;
798 static void print_pcap_file_stats(int sock, struct ctx *ctx)
800 int ret;
801 struct tpacket_stats kstats;
802 socklen_t slen = sizeof(kstats);
804 fmemset(&kstats, 0, sizeof(kstats));
806 ret = getsockopt(sock, SOL_PACKET, PACKET_STATISTICS, &kstats, &slen);
807 if (unlikely(ret))
808 panic("Cannot get packet statistics!\n");
810 if (ctx->print_mode == PRINT_NONE) {
811 printf(".(+%u/-%u)", kstats.tp_packets - kstats.tp_drops,
812 kstats.tp_drops);
813 fflush(stdout);
817 static void walk_t3_block(struct block_desc *pbd, struct ctx *ctx,
818 int sock, int *fd, unsigned long *frame_count)
820 uint8_t *packet;
821 int num_pkts = pbd->h1.num_pkts, i, ret;
822 struct tpacket3_hdr *hdr;
823 pcap_pkthdr_t phdr;
824 struct sockaddr_ll *sll;
826 hdr = (void *) ((uint8_t *) pbd + pbd->h1.offset_to_first_pkt);
827 sll = (void *) ((uint8_t *) hdr + TPACKET_ALIGN(sizeof(*hdr)));
829 for (i = 0; i < num_pkts && likely(sigint == 0); ++i) {
830 __label__ next;
831 packet = ((uint8_t *) hdr + hdr->tp_mac);
833 if (ctx->packet_type != -1)
834 if (ctx->packet_type != sll->sll_pkttype)
835 goto next;
837 (*frame_count)++;
839 if (dump_to_pcap(ctx)) {
840 tpacket3_hdr_to_pcap_pkthdr(hdr, sll, &phdr, ctx->magic);
842 ret = __pcap_io->write_pcap(*fd, &phdr, ctx->magic, packet,
843 pcap_get_length(&phdr, ctx->magic));
844 if (unlikely(ret != pcap_get_total_length(&phdr, ctx->magic)))
845 panic("Write error to pcap!\n");
848 __show_frame_hdr(sll, hdr, ctx->print_mode, true);
850 dissector_entry_point(packet, hdr->tp_snaplen, ctx->link_type,
851 ctx->print_mode);
852 next:
854 hdr = (void *) ((uint8_t *) hdr + hdr->tp_next_offset);
855 sll = (void *) ((uint8_t *) hdr + TPACKET_ALIGN(sizeof(*hdr)));
857 if (frame_count_max != 0) {
858 if (unlikely(*frame_count >= frame_count_max)) {
859 sigint = 1;
860 break;
864 if (dump_to_pcap(ctx)) {
865 if (ctx->dump_mode == DUMP_INTERVAL_SIZE) {
866 interval += hdr->tp_snaplen;
867 if (interval > ctx->dump_interval) {
868 next_dump = true;
869 interval = 0;
873 if (next_dump) {
874 *fd = next_multi_pcap_file(ctx, *fd);
875 next_dump = false;
877 if (unlikely(ctx->verbose))
878 print_pcap_file_stats(sock, ctx);
884 static void recv_only_or_dump(struct ctx *ctx)
886 short ifflags = 0;
887 int sock, irq, ifindex, fd = 0, ret;
888 unsigned int size, it = 0;
889 struct ring rx_ring;
890 struct pollfd rx_poll;
891 struct sock_fprog bpf_ops;
892 struct timeval start, end, diff;
893 struct block_desc *pbd;
894 unsigned long frame_count = 0;
896 sock = pf_socket();
898 if (ctx->rfraw) {
899 ctx->device_trans = xstrdup(ctx->device_in);
900 xfree(ctx->device_in);
902 enter_rfmon_mac80211(ctx->device_trans, &ctx->device_in);
903 ctx->link_type = LINKTYPE_IEEE802_11;
906 fmemset(&rx_ring, 0, sizeof(rx_ring));
907 fmemset(&rx_poll, 0, sizeof(rx_poll));
908 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
910 ifindex = device_ifindex(ctx->device_in);
912 size = ring_size(ctx->device_in, ctx->reserve_size);
914 enable_kernel_bpf_jit_compiler();
916 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
917 if (ctx->dump_bpf)
918 bpf_dump_all(&bpf_ops);
919 bpf_attach_to_sock(sock, &bpf_ops);
921 ret = set_sockopt_hwtimestamp(sock, ctx->device_in);
922 if (ret == 0 && ctx->verbose)
923 printf("HW timestamping enabled\n");
925 setup_rx_ring_layout(sock, &rx_ring, size, true, true);
926 create_rx_ring(sock, &rx_ring, ctx->verbose);
927 mmap_rx_ring(sock, &rx_ring);
928 alloc_rx_ring_frames(sock, &rx_ring);
929 bind_rx_ring(sock, &rx_ring, ifindex);
931 prepare_polling(sock, &rx_poll);
932 dissector_init_all(ctx->print_mode);
934 if (ctx->cpu >= 0 && ifindex > 0) {
935 irq = device_irq_number(ctx->device_in);
936 device_set_irq_affinity(irq, ctx->cpu);
938 if (ctx->verbose)
939 printf("IRQ: %s:%d > CPU%d\n",
940 ctx->device_in, irq, ctx->cpu);
943 if (ctx->promiscuous)
944 ifflags = enter_promiscuous_mode(ctx->device_in);
946 if (dump_to_pcap(ctx) && __pcap_io->init_once_pcap)
947 __pcap_io->init_once_pcap();
949 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
951 if (dump_to_pcap(ctx)) {
952 __label__ try_file;
953 struct stat stats;
955 fmemset(&stats, 0, sizeof(stats));
956 ret = stat(ctx->device_out, &stats);
957 if (ret < 0) {
958 ctx->dump_dir = 0;
959 goto try_file;
962 ctx->dump_dir = S_ISDIR(stats.st_mode);
963 if (ctx->dump_dir) {
964 fd = begin_multi_pcap_file(ctx);
965 } else {
966 try_file:
967 fd = begin_single_pcap_file(ctx);
971 printf("Running! Hang up with ^C!\n\n");
972 fflush(stdout);
974 bug_on(gettimeofday(&start, NULL));
976 while (likely(sigint == 0)) {
977 while (user_may_pull_from_rx_block((pbd = (void *)
978 rx_ring.frames[it].iov_base))) {
979 walk_t3_block(pbd, ctx, sock, &fd, &frame_count);
981 kernel_may_pull_from_rx_block(pbd);
982 it = (it + 1) % rx_ring.layout3.tp_block_nr;
984 if (unlikely(sigint == 1))
985 break;
988 poll(&rx_poll, 1, -1);
991 bug_on(gettimeofday(&end, NULL));
992 timersub(&end, &start, &diff);
994 if (!(ctx->dump_dir && ctx->print_mode == PRINT_NONE)) {
995 sock_rx_net_stats(sock, frame_count);
997 printf("\r%12lu sec, %lu usec in total\n",
998 diff.tv_sec, diff.tv_usec);
999 } else {
1000 printf("\n\n");
1001 fflush(stdout);
1004 bpf_release(&bpf_ops);
1005 dissector_cleanup_all();
1006 destroy_rx_ring(sock, &rx_ring);
1008 if (ctx->promiscuous)
1009 leave_promiscuous_mode(ctx->device_in, ifflags);
1011 if (ctx->rfraw)
1012 leave_rfmon_mac80211(ctx->device_trans, ctx->device_in);
1014 if (dump_to_pcap(ctx)) {
1015 if (ctx->dump_dir)
1016 finish_multi_pcap_file(ctx, fd);
1017 else
1018 finish_single_pcap_file(ctx, fd);
1021 close(sock);
1024 static void init_ctx(struct ctx *ctx)
1026 memset(ctx, 0, sizeof(*ctx));
1027 ctx->uid = getuid();
1028 ctx->uid = getgid();
1030 ctx->cpu = -1;
1031 ctx->packet_type = -1;
1033 ctx->magic = ORIGINAL_TCPDUMP_MAGIC;
1034 ctx->print_mode = PRINT_NORM;
1035 ctx->pcap = PCAP_OPS_SG;
1037 ctx->dump_mode = DUMP_INTERVAL_TIME;
1038 ctx->dump_interval = 60;
1040 ctx->promiscuous = true;
1041 ctx->randomize = false;
1044 static void destroy_ctx(struct ctx *ctx)
1046 free(ctx->device_in);
1047 free(ctx->device_out);
1048 free(ctx->device_trans);
1050 free(ctx->prefix);
1053 static void __noreturn 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 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 " -R|--rfraw Capture or inject raw 802.11 frames\n"
1065 " -n|--num <0|uint> Number of packets until exit (def: 0)\n"
1066 " -P|--prefix <name> Prefix for pcaps stored in directory\n"
1067 " -T|--magic <pcap-magic> Pcap magic number/pcap format to store, see -D\n"
1068 " -D|--dump-pcap-types Dump pcap types and magic numbers and quit\n"
1069 " -B|--dump-bpf Dump generated BPF assembly\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 " -J|--jumbo-support Support replay/fwd 64KB Super Jumbo Frames (def: 2048B)\n"
1079 " -b|--bind-cpu <cpu> Bind to specific CPU\n"
1080 " -u|--user <userid> Drop privileges and change to userid\n"
1081 " -g|--group <groupid> Drop privileges and change to groupid\n"
1082 " -H|--prio-high Make this high priority process\n"
1083 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
1084 " -s|--silent Do not print captured packets\n"
1085 " -q|--less Print less-verbose packet information\n"
1086 " -X|--hex Print packet data in hex format\n"
1087 " -l|--ascii Print human-readable packet data\n"
1088 " -U|--update Update GeoIP databases\n"
1089 " -V|--verbose Be more verbose\n"
1090 " -v|--version Show version and exit\n"
1091 " -h|--help Guess what?!\n\n"
1092 "Examples:\n"
1093 " netsniff-ng --in eth0 --out dump.pcap -s -T 0xa1b2c3d4 --b 0 tcp or udp\n"
1094 " netsniff-ng --in wlan0 --rfraw --out dump.pcap --silent --bind-cpu 0\n"
1095 " netsniff-ng --in dump.pcap --mmap --out eth0 -k1000 --silent --bind-cpu 0\n"
1096 " netsniff-ng --in dump.pcap --out dump.cfg --silent --bind-cpu 0\n"
1097 " netsniff-ng --in eth0 --out eth1 --silent --bind-cpu 0 -J --type host\n"
1098 " netsniff-ng --in eth1 --out /opt/probe/ -s -m --interval 100MiB -b 0\n"
1099 " netsniff-ng --in vlan0 --out dump.pcap -c -u `id -u bob` -g `id -g bob`\n"
1100 " netsniff-ng --in any --filter http.bpf --jumbo-support --ascii -V\n\n"
1101 "Note:\n"
1102 " For introducing bit errors, delays with random variation and more\n"
1103 " while replaying pcaps, make use of tc(8) with its disciplines (e.g. netem).\n\n"
1104 "Please report bugs to <bugs@netsniff-ng.org>\n"
1105 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1106 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1107 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1108 "Swiss federal institute of technology (ETH Zurich)\n"
1109 "License: GNU GPL version 2.0\n"
1110 "This is free software: you are free to change and redistribute it.\n"
1111 "There is NO WARRANTY, to the extent permitted by law.\n");
1112 die();
1115 static void __noreturn version(void)
1117 printf("\nnetsniff-ng %s, Git id: %s\n", VERSION_LONG, GITVERSION);
1118 puts("the packet sniffing beast\n"
1119 "http://www.netsniff-ng.org\n\n"
1120 "Please report bugs to <bugs@netsniff-ng.org>\n"
1121 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1122 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1123 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1124 "Swiss federal institute of technology (ETH Zurich)\n"
1125 "License: GNU GPL version 2.0\n"
1126 "This is free software: you are free to change and redistribute it.\n"
1127 "There is NO WARRANTY, to the extent permitted by law.\n");
1128 die();
1131 int main(int argc, char **argv)
1133 char *ptr;
1134 int c, i, j, cpu_tmp, opt_index, ops_touched = 0, vals[4] = {0};
1135 bool prio_high = false, setsockmem = true;
1136 void (*main_loop)(struct ctx *ctx) = NULL;
1137 struct ctx ctx;
1139 init_ctx(&ctx);
1140 srand(time(NULL));
1142 while ((c = getopt_long(argc, argv, short_options, long_options,
1143 &opt_index)) != EOF) {
1144 switch (c) {
1145 case 'd':
1146 case 'i':
1147 ctx.device_in = xstrdup(optarg);
1148 break;
1149 case 'o':
1150 ctx.device_out = xstrdup(optarg);
1151 break;
1152 case 'P':
1153 ctx.prefix = xstrdup(optarg);
1154 break;
1155 case 'R':
1156 ctx.link_type = LINKTYPE_IEEE802_11;
1157 ctx.rfraw = 1;
1158 break;
1159 case 'r':
1160 ctx.randomize = true;
1161 break;
1162 case 'J':
1163 ctx.jumbo = true;
1164 break;
1165 case 'T':
1166 ctx.magic = (uint32_t) strtoul(optarg, NULL, 0);
1167 pcap_check_magic(ctx.magic);
1168 break;
1169 case 'f':
1170 ctx.filter = xstrdup(optarg);
1171 break;
1172 case 'M':
1173 ctx.promiscuous = false;
1174 break;
1175 case 'A':
1176 setsockmem = false;
1177 break;
1178 case 'u':
1179 ctx.uid = strtoul(optarg, NULL, 0);
1180 ctx.enforce = true;
1181 break;
1182 case 'g':
1183 ctx.gid = strtoul(optarg, NULL, 0);
1184 ctx.enforce = true;
1185 break;
1186 case 't':
1187 if (!strncmp(optarg, "host", strlen("host")))
1188 ctx.packet_type = PACKET_HOST;
1189 else if (!strncmp(optarg, "broadcast", strlen("broadcast")))
1190 ctx.packet_type = PACKET_BROADCAST;
1191 else if (!strncmp(optarg, "multicast", strlen("multicast")))
1192 ctx.packet_type = PACKET_MULTICAST;
1193 else if (!strncmp(optarg, "others", strlen("others")))
1194 ctx.packet_type = PACKET_OTHERHOST;
1195 else if (!strncmp(optarg, "outgoing", strlen("outgoing")))
1196 ctx.packet_type = PACKET_OUTGOING;
1197 else
1198 ctx.packet_type = -1;
1199 break;
1200 case 'S':
1201 ptr = optarg;
1202 ctx.reserve_size = 0;
1204 for (j = i = strlen(optarg); i > 0; --i) {
1205 if (!isdigit(optarg[j - i]))
1206 break;
1207 ptr++;
1210 if (!strncmp(ptr, "KiB", strlen("KiB")))
1211 ctx.reserve_size = 1 << 10;
1212 else if (!strncmp(ptr, "MiB", strlen("MiB")))
1213 ctx.reserve_size = 1 << 20;
1214 else if (!strncmp(ptr, "GiB", strlen("GiB")))
1215 ctx.reserve_size = 1 << 30;
1216 else
1217 panic("Syntax error in ring size param!\n");
1218 *ptr = 0;
1220 ctx.reserve_size *= strtol(optarg, NULL, 0);
1221 break;
1222 case 'b':
1223 cpu_tmp = strtol(optarg, NULL, 0);
1225 cpu_affinity(cpu_tmp);
1226 if (ctx.cpu != -2)
1227 ctx.cpu = cpu_tmp;
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 '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(SIGHUP, signal_handler);
1379 tprintf_init();
1381 if (prio_high) {
1382 set_proc_prio(-20);
1383 set_sched_status(SCHED_FIFO, sched_get_priority_max(SCHED_FIFO));
1386 if (ctx.device_in && (device_mtu(ctx.device_in) ||
1387 !strncmp("any", ctx.device_in, strlen(ctx.device_in)))) {
1388 if (!ctx.rfraw)
1389 ctx.link_type = pcap_devtype_to_linktype(ctx.device_in);
1390 if (!ctx.device_out) {
1391 ctx.dump = 0;
1392 main_loop = recv_only_or_dump;
1393 } else if (device_mtu(ctx.device_out)) {
1394 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1395 main_loop = receive_to_xmit;
1396 } else {
1397 ctx.dump = 1;
1398 register_signal_f(SIGALRM, timer_next_dump, SA_SIGINFO);
1399 main_loop = recv_only_or_dump;
1400 if (!ops_touched)
1401 ctx.pcap = PCAP_OPS_SG;
1403 } else {
1404 if (ctx.device_out && device_mtu(ctx.device_out)) {
1405 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1406 main_loop = pcap_to_xmit;
1407 if (!ops_touched)
1408 ctx.pcap = PCAP_OPS_MM;
1409 } else {
1410 main_loop = read_pcap;
1411 if (!ops_touched)
1412 ctx.pcap = PCAP_OPS_SG;
1416 bug_on(!main_loop);
1418 init_geoip(0);
1419 if (setsockmem)
1420 set_system_socket_memory(vals, array_size(vals));
1421 if (!ctx.enforce)
1422 xlockme();
1424 main_loop(&ctx);
1426 if (!ctx.enforce)
1427 xunlockme();
1428 if (setsockmem)
1429 reset_system_socket_memory(vals, array_size(vals));
1430 destroy_geoip();
1432 device_restore_irq_affinity_list();
1433 tprintf_cleanup();
1435 destroy_ctx(&ctx);
1436 return 0;