build: Add Makefile target for Coverity scanner
[netsniff-ng.git] / netsniff-ng.c
blob682fb39d05ab33873afdb7a460cb40c892a5b471
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 sigint = 1;
117 case SIGHUP:
118 default:
119 break;
123 static void timer_elapsed(int unused __maybe_unused)
125 int ret;
127 set_itimer_interval_value(&itimer, 0, interval);
129 ret = pull_and_flush_tx_ring(tx_sock);
130 if (unlikely(ret < 0)) {
131 /* We could hit EBADF if the socket has been closed before
132 * the timer was triggered.
134 if (errno != EBADF && errno != ENOBUFS)
135 panic("Flushing TX_RING failed: %s!\n", strerror(errno));
138 setitimer(ITIMER_REAL, &itimer, NULL);
141 static void timer_purge(void)
143 int ret;
145 ret = pull_and_flush_tx_ring_wait(tx_sock);
146 if (unlikely(ret < 0)) {
147 if (errno != EBADF && errno != ENOBUFS)
148 panic("Flushing TX_RING failed: %s!\n", strerror(errno));
151 set_itimer_interval_value(&itimer, 0, 0);
152 setitimer(ITIMER_REAL, &itimer, NULL);
155 static void timer_next_dump(int unused __maybe_unused)
157 set_itimer_interval_value(&itimer, interval, 0);
158 next_dump = true;
159 setitimer(ITIMER_REAL, &itimer, NULL);
162 static inline bool dump_to_pcap(struct ctx *ctx)
164 return ctx->dump;
167 static void pcap_to_xmit(struct ctx *ctx)
169 __label__ out;
170 uint8_t *out = NULL;
171 int irq, ifindex, fd = 0, ret;
172 unsigned int size, it = 0;
173 unsigned long trunced = 0;
174 struct ring tx_ring;
175 struct frame_map *hdr;
176 struct sock_fprog bpf_ops;
177 struct timeval start, end, diff;
178 pcap_pkthdr_t phdr;
180 if (!device_up_and_running(ctx->device_out) && !ctx->rfraw)
181 panic("Device not up and running!\n");
183 bug_on(!__pcap_io);
185 tx_sock = pf_socket();
187 if (!strncmp("-", ctx->device_in, strlen("-"))) {
188 fd = dup_or_die(fileno(stdin));
189 close(fileno(stdin));
190 if (ctx->pcap == PCAP_OPS_MM)
191 ctx->pcap = PCAP_OPS_SG;
192 } else {
193 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
196 if (__pcap_io->init_once_pcap)
197 __pcap_io->init_once_pcap();
199 ret = __pcap_io->pull_fhdr_pcap(fd, &ctx->magic, &ctx->link_type);
200 if (ret)
201 panic("Error reading pcap header!\n");
203 if (__pcap_io->prepare_access_pcap) {
204 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_RD, ctx->jumbo);
205 if (ret)
206 panic("Error prepare reading pcap!\n");
209 fmemset(&tx_ring, 0, sizeof(tx_ring));
210 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
212 if (ctx->rfraw) {
213 ctx->device_trans = xstrdup(ctx->device_out);
214 xfree(ctx->device_out);
216 enter_rfmon_mac80211(ctx->device_trans, &ctx->device_out);
217 if (ctx->link_type != LINKTYPE_IEEE802_11)
218 panic("Wrong linktype of pcap!\n");
221 ifindex = device_ifindex(ctx->device_out);
223 size = ring_size(ctx->device_out, ctx->reserve_size);
225 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
226 if (ctx->dump_bpf)
227 bpf_dump_all(&bpf_ops);
229 set_packet_loss_discard(tx_sock);
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_sock, &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_set_irq_affinity(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 set_itimer_interval_value(&itimer, 0, interval);
252 setitimer(ITIMER_REAL, &itimer, NULL);
254 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
256 printf("Running! Hang up with ^C!\n\n");
257 fflush(stdout);
259 bug_on(gettimeofday(&start, NULL));
261 while (likely(sigint == 0)) {
262 while (user_may_pull_from_tx(tx_ring.frames[it].iov_base)) {
263 hdr = tx_ring.frames[it].iov_base;
264 out = ((uint8_t *) hdr) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
266 do {
267 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic, out,
268 ring_frame_size(&tx_ring));
269 if (unlikely(ret <= 0))
270 goto out;
272 if (ring_frame_size(&tx_ring) <
273 pcap_get_length(&phdr, ctx->magic)) {
274 pcap_set_length(&phdr, ctx->magic,
275 ring_frame_size(&tx_ring));
276 trunced++;
278 } while (ctx->filter &&
279 !bpf_run_filter(&bpf_ops, out,
280 pcap_get_length(&phdr, ctx->magic)));
282 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &hdr->tp_h);
284 ctx->tx_bytes += hdr->tp_h.tp_len;;
285 ctx->tx_packets++;
287 show_frame_hdr(hdr, ctx->print_mode);
289 dissector_entry_point(out, hdr->tp_h.tp_snaplen,
290 ctx->link_type, ctx->print_mode);
292 kernel_may_pull_from_tx(&hdr->tp_h);
294 it++;
295 if (it >= tx_ring.layout.tp_frame_nr)
296 it = 0;
298 if (unlikely(sigint == 1))
299 break;
301 if (frame_count_max != 0) {
302 if (ctx->tx_packets >= frame_count_max) {
303 sigint = 1;
304 break;
310 out:
312 bug_on(gettimeofday(&end, NULL));
313 timersub(&end, &start, &diff);
315 timer_purge();
317 bpf_release(&bpf_ops);
319 dissector_cleanup_all();
320 destroy_tx_ring(tx_sock, &tx_ring);
322 if (ctx->rfraw)
323 leave_rfmon_mac80211(ctx->device_out);
325 if (__pcap_io->prepare_close_pcap)
326 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
328 if (!strncmp("-", ctx->device_in, strlen("-")))
329 dup2(fd, fileno(stdin));
330 close(fd);
332 close(tx_sock);
334 fflush(stdout);
335 printf("\n");
336 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
337 printf("\r%12lu packets truncated in file\n", trunced);
338 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
339 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
342 static void receive_to_xmit(struct ctx *ctx)
344 short ifflags = 0;
345 uint8_t *in, *out;
346 int rx_sock, ifindex_in, ifindex_out, ret;
347 unsigned int size_in, size_out, it_in = 0, it_out = 0;
348 unsigned long frame_count = 0;
349 struct frame_map *hdr_in, *hdr_out;
350 struct ring tx_ring, rx_ring;
351 struct pollfd rx_poll;
352 struct sock_fprog bpf_ops;
354 if (!strncmp(ctx->device_in, ctx->device_out, IFNAMSIZ))
355 panic("Ingress/egress devices must be different!\n");
356 if (!device_up_and_running(ctx->device_out))
357 panic("Egress device not up and running!\n");
359 rx_sock = pf_socket();
360 tx_sock = pf_socket();
362 fmemset(&tx_ring, 0, sizeof(tx_ring));
363 fmemset(&rx_ring, 0, sizeof(rx_ring));
364 fmemset(&rx_poll, 0, sizeof(rx_poll));
365 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
367 ifindex_in = device_ifindex(ctx->device_in);
368 ifindex_out = device_ifindex(ctx->device_out);
370 size_in = ring_size(ctx->device_in, ctx->reserve_size);
371 size_out = ring_size(ctx->device_out, ctx->reserve_size);
373 enable_kernel_bpf_jit_compiler();
375 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
376 if (ctx->dump_bpf)
377 bpf_dump_all(&bpf_ops);
378 bpf_attach_to_sock(rx_sock, &bpf_ops);
380 setup_rx_ring_layout(rx_sock, &rx_ring, size_in, ctx->jumbo, false);
381 create_rx_ring(rx_sock, &rx_ring, ctx->verbose);
382 mmap_rx_ring(rx_sock, &rx_ring);
383 alloc_rx_ring_frames(rx_sock, &rx_ring);
384 bind_rx_ring(rx_sock, &rx_ring, ifindex_in);
385 prepare_polling(rx_sock, &rx_poll);
387 set_packet_loss_discard(tx_sock);
388 setup_tx_ring_layout(tx_sock, &tx_ring, size_out, ctx->jumbo);
389 create_tx_ring(tx_sock, &tx_ring, ctx->verbose);
390 mmap_tx_ring(tx_sock, &tx_ring);
391 alloc_tx_ring_frames(tx_sock, &tx_ring);
392 bind_tx_ring(tx_sock, &tx_ring, ifindex_out);
394 dissector_init_all(ctx->print_mode);
396 if (ctx->promiscuous)
397 ifflags = device_enter_promiscuous_mode(ctx->device_in);
399 if (ctx->kpull)
400 interval = ctx->kpull;
402 set_itimer_interval_value(&itimer, 0, interval);
403 setitimer(ITIMER_REAL, &itimer, NULL);
405 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
407 printf("Running! Hang up with ^C!\n\n");
408 fflush(stdout);
410 while (likely(sigint == 0)) {
411 while (user_may_pull_from_rx(rx_ring.frames[it_in].iov_base)) {
412 __label__ next;
414 hdr_in = rx_ring.frames[it_in].iov_base;
415 in = ((uint8_t *) hdr_in) + hdr_in->tp_h.tp_mac;
417 frame_count++;
419 if (ctx->packet_type != -1)
420 if (ctx->packet_type != hdr_in->s_ll.sll_pkttype)
421 goto next;
423 hdr_out = tx_ring.frames[it_out].iov_base;
424 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
426 for (; !user_may_pull_from_tx(tx_ring.frames[it_out].iov_base) &&
427 likely(!sigint);) {
428 if (ctx->randomize)
429 next_rnd_slot(&it_out, &tx_ring);
430 else {
431 it_out++;
432 if (it_out >= tx_ring.layout.tp_frame_nr)
433 it_out = 0;
436 hdr_out = tx_ring.frames[it_out].iov_base;
437 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
440 tpacket_hdr_clone(&hdr_out->tp_h, &hdr_in->tp_h);
441 fmemcpy(out, in, hdr_in->tp_h.tp_len);
443 kernel_may_pull_from_tx(&hdr_out->tp_h);
444 if (ctx->randomize)
445 next_rnd_slot(&it_out, &tx_ring);
446 else {
447 it_out++;
448 if (it_out >= tx_ring.layout.tp_frame_nr)
449 it_out = 0;
452 show_frame_hdr(hdr_in, ctx->print_mode);
454 dissector_entry_point(in, hdr_in->tp_h.tp_snaplen,
455 ctx->link_type, ctx->print_mode);
457 if (frame_count_max != 0) {
458 if (frame_count >= frame_count_max) {
459 sigint = 1;
460 break;
464 next:
466 kernel_may_pull_from_rx(&hdr_in->tp_h);
468 it_in++;
469 if (it_in >= rx_ring.layout.tp_frame_nr)
470 it_in = 0;
472 if (unlikely(sigint == 1))
473 goto out;
476 ret = poll(&rx_poll, 1, -1);
477 if (unlikely(ret < 0))
478 panic("Poll failed!\n");
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 device_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;
546 bug_on(!__pcap_io);
548 if (!strncmp("-", ctx->device_in, strlen("-"))) {
549 fd = dup_or_die(fileno(stdin));
550 close(fileno(stdin));
551 if (ctx->pcap == PCAP_OPS_MM)
552 ctx->pcap = PCAP_OPS_SG;
553 } else {
554 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
557 if (__pcap_io->init_once_pcap)
558 __pcap_io->init_once_pcap();
560 ret = __pcap_io->pull_fhdr_pcap(fd, &ctx->magic, &ctx->link_type);
561 if (ret)
562 panic("Error reading pcap header!\n");
564 if (__pcap_io->prepare_access_pcap) {
565 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_RD, ctx->jumbo);
566 if (ret)
567 panic("Error prepare reading pcap!\n");
570 fmemset(&fm, 0, sizeof(fm));
571 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
573 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
574 if (ctx->dump_bpf)
575 bpf_dump_all(&bpf_ops);
577 dissector_init_all(ctx->print_mode);
579 out_len = round_up(1024 * 1024, PAGE_SIZE);
580 out = xmalloc_aligned(out_len, CO_CACHE_LINE_SIZE);
582 if (ctx->device_out) {
583 if (!strncmp("-", ctx->device_out, strlen("-"))) {
584 fdo = dup_or_die(fileno(stdout));
585 close(fileno(stdout));
586 } else {
587 fdo = open_or_die_m(ctx->device_out, O_RDWR | O_CREAT |
588 O_TRUNC | O_LARGEFILE, DEFFILEMODE);
592 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
594 printf("Running! Hang up with ^C!\n\n");
595 fflush(stdout);
597 bug_on(gettimeofday(&start, NULL));
599 while (likely(sigint == 0)) {
600 do {
601 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic,
602 out, out_len);
603 if (unlikely(ret < 0))
604 goto out;
606 if (unlikely(pcap_get_length(&phdr, ctx->magic) == 0)) {
607 trunced++;
608 continue;
611 if (unlikely(pcap_get_length(&phdr, ctx->magic) > out_len)) {
612 pcap_set_length(&phdr, ctx->magic, out_len);
613 trunced++;
615 } while (ctx->filter &&
616 !bpf_run_filter(&bpf_ops, out,
617 pcap_get_length(&phdr, ctx->magic)));
619 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &fm.tp_h);
621 ctx->tx_bytes += fm.tp_h.tp_len;
622 ctx->tx_packets++;
624 show_frame_hdr(&fm, ctx->print_mode);
626 dissector_entry_point(out, fm.tp_h.tp_snaplen,
627 ctx->link_type, ctx->print_mode);
629 if (ctx->device_out)
630 translate_pcap_to_txf(fdo, out, fm.tp_h.tp_snaplen);
632 if (frame_count_max != 0) {
633 if (ctx->tx_packets >= frame_count_max) {
634 sigint = 1;
635 break;
640 out:
642 bug_on(gettimeofday(&end, NULL));
643 timersub(&end, &start, &diff);
645 bpf_release(&bpf_ops);
647 dissector_cleanup_all();
649 if (__pcap_io->prepare_close_pcap)
650 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
652 xfree(out);
654 fflush(stdout);
655 printf("\n");
656 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
657 printf("\r%12lu packets truncated in file\n", trunced);
658 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
659 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
661 if (!strncmp("-", ctx->device_in, strlen("-")))
662 dup2(fd, fileno(stdin));
663 close(fd);
665 if (ctx->device_out) {
666 if (!strncmp("-", ctx->device_out, strlen("-")))
667 dup2(fdo, fileno(stdout));
668 close(fdo);
672 static void finish_multi_pcap_file(struct ctx *ctx, int fd)
674 __pcap_io->fsync_pcap(fd);
676 if (__pcap_io->prepare_close_pcap)
677 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
679 close(fd);
681 fmemset(&itimer, 0, sizeof(itimer));
682 setitimer(ITIMER_REAL, &itimer, NULL);
685 static int next_multi_pcap_file(struct ctx *ctx, int fd)
687 int ret;
688 char fname[512];
690 __pcap_io->fsync_pcap(fd);
692 if (__pcap_io->prepare_close_pcap)
693 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
695 close(fd);
697 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
698 ctx->prefix ? : "dump-", time(NULL));
700 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
701 O_LARGEFILE, DEFFILEMODE);
703 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
704 if (ret)
705 panic("Error writing pcap header!\n");
707 if (__pcap_io->prepare_access_pcap) {
708 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, true);
709 if (ret)
710 panic("Error prepare writing pcap!\n");
713 return fd;
716 static int begin_multi_pcap_file(struct ctx *ctx)
718 int fd, ret;
719 char fname[256];
721 bug_on(!__pcap_io);
723 if (ctx->device_out[strlen(ctx->device_out) - 1] == '/')
724 ctx->device_out[strlen(ctx->device_out) - 1] = 0;
726 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
727 ctx->prefix ? : "dump-", time(NULL));
729 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
730 O_LARGEFILE, DEFFILEMODE);
732 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
733 if (ret)
734 panic("Error writing pcap header!\n");
736 if (__pcap_io->prepare_access_pcap) {
737 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, true);
738 if (ret)
739 panic("Error prepare writing pcap!\n");
742 if (ctx->dump_mode == DUMP_INTERVAL_TIME) {
743 interval = ctx->dump_interval;
745 set_itimer_interval_value(&itimer, interval, 0);
746 setitimer(ITIMER_REAL, &itimer, NULL);
747 } else {
748 interval = 0;
751 return fd;
754 static void finish_single_pcap_file(struct ctx *ctx, int fd)
756 __pcap_io->fsync_pcap(fd);
758 if (__pcap_io->prepare_close_pcap)
759 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
761 if (strncmp("-", ctx->device_out, strlen("-")))
762 close(fd);
763 else
764 dup2(fd, fileno(stdout));
767 static int begin_single_pcap_file(struct ctx *ctx)
769 int fd, ret;
771 bug_on(!__pcap_io);
773 if (!strncmp("-", ctx->device_out, strlen("-"))) {
774 fd = dup_or_die(fileno(stdout));
775 close(fileno(stdout));
776 if (ctx->pcap == PCAP_OPS_MM)
777 ctx->pcap = PCAP_OPS_SG;
778 } else {
779 fd = open_or_die_m(ctx->device_out,
780 O_RDWR | O_CREAT | O_TRUNC |
781 O_LARGEFILE, DEFFILEMODE);
784 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
785 if (ret)
786 panic("Error writing pcap header!\n");
788 if (__pcap_io->prepare_access_pcap) {
789 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, true);
790 if (ret)
791 panic("Error prepare writing pcap!\n");
794 return fd;
797 static void print_pcap_file_stats(int sock, struct ctx *ctx)
799 int ret;
800 struct tpacket_stats kstats;
801 socklen_t slen = sizeof(kstats);
803 fmemset(&kstats, 0, sizeof(kstats));
805 ret = getsockopt(sock, SOL_PACKET, PACKET_STATISTICS, &kstats, &slen);
806 if (unlikely(ret))
807 panic("Cannot get packet statistics!\n");
809 if (ctx->print_mode == PRINT_NONE) {
810 printf(".(+%u/-%u)", kstats.tp_packets - kstats.tp_drops,
811 kstats.tp_drops);
812 fflush(stdout);
816 static void walk_t3_block(struct block_desc *pbd, struct ctx *ctx,
817 int sock, int *fd, unsigned long *frame_count)
819 uint8_t *packet;
820 int num_pkts = pbd->h1.num_pkts, i, ret;
821 struct tpacket3_hdr *hdr;
822 pcap_pkthdr_t phdr;
823 struct sockaddr_ll *sll;
825 hdr = (void *) ((uint8_t *) pbd + pbd->h1.offset_to_first_pkt);
826 sll = (void *) ((uint8_t *) hdr + TPACKET_ALIGN(sizeof(*hdr)));
828 for (i = 0; i < num_pkts && likely(sigint == 0); ++i) {
829 __label__ next;
830 packet = ((uint8_t *) hdr + hdr->tp_mac);
832 if (ctx->packet_type != -1)
833 if (ctx->packet_type != sll->sll_pkttype)
834 goto next;
836 (*frame_count)++;
838 if (dump_to_pcap(ctx)) {
839 tpacket3_hdr_to_pcap_pkthdr(hdr, sll, &phdr, ctx->magic);
841 ret = __pcap_io->write_pcap(*fd, &phdr, ctx->magic, packet,
842 pcap_get_length(&phdr, ctx->magic));
843 if (unlikely(ret != (int) pcap_get_total_length(&phdr, ctx->magic)))
844 panic("Write error to pcap!\n");
847 __show_frame_hdr(sll, hdr, ctx->print_mode, true);
849 dissector_entry_point(packet, hdr->tp_snaplen, ctx->link_type,
850 ctx->print_mode);
851 next:
853 hdr = (void *) ((uint8_t *) hdr + hdr->tp_next_offset);
854 sll = (void *) ((uint8_t *) hdr + TPACKET_ALIGN(sizeof(*hdr)));
856 if (frame_count_max != 0) {
857 if (unlikely(*frame_count >= frame_count_max)) {
858 sigint = 1;
859 break;
863 if (dump_to_pcap(ctx)) {
864 if (ctx->dump_mode == DUMP_INTERVAL_SIZE) {
865 interval += hdr->tp_snaplen;
866 if (interval > ctx->dump_interval) {
867 next_dump = true;
868 interval = 0;
872 if (next_dump) {
873 *fd = next_multi_pcap_file(ctx, *fd);
874 next_dump = false;
876 if (unlikely(ctx->verbose))
877 print_pcap_file_stats(sock, ctx);
883 static void recv_only_or_dump(struct ctx *ctx)
885 short ifflags = 0;
886 int sock, irq, ifindex, fd = 0, ret;
887 unsigned int size, it = 0;
888 struct ring rx_ring;
889 struct pollfd rx_poll;
890 struct sock_fprog bpf_ops;
891 struct timeval start, end, diff;
892 struct block_desc *pbd;
893 unsigned long frame_count = 0;
895 sock = pf_socket();
897 if (ctx->rfraw) {
898 ctx->device_trans = xstrdup(ctx->device_in);
899 xfree(ctx->device_in);
901 enter_rfmon_mac80211(ctx->device_trans, &ctx->device_in);
902 ctx->link_type = LINKTYPE_IEEE802_11;
905 fmemset(&rx_ring, 0, sizeof(rx_ring));
906 fmemset(&rx_poll, 0, sizeof(rx_poll));
907 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
909 ifindex = device_ifindex(ctx->device_in);
911 size = ring_size(ctx->device_in, ctx->reserve_size);
913 enable_kernel_bpf_jit_compiler();
915 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
916 if (ctx->dump_bpf)
917 bpf_dump_all(&bpf_ops);
918 bpf_attach_to_sock(sock, &bpf_ops);
920 ret = set_sockopt_hwtimestamp(sock, ctx->device_in);
921 if (ret == 0 && ctx->verbose)
922 printf("HW timestamping enabled\n");
924 setup_rx_ring_layout(sock, &rx_ring, size, true, true);
925 create_rx_ring(sock, &rx_ring, ctx->verbose);
926 mmap_rx_ring(sock, &rx_ring);
927 alloc_rx_ring_frames(sock, &rx_ring);
928 bind_rx_ring(sock, &rx_ring, ifindex);
930 prepare_polling(sock, &rx_poll);
931 dissector_init_all(ctx->print_mode);
933 if (ctx->cpu >= 0 && ifindex > 0) {
934 irq = device_irq_number(ctx->device_in);
935 device_set_irq_affinity(irq, ctx->cpu);
937 if (ctx->verbose)
938 printf("IRQ: %s:%d > CPU%d\n",
939 ctx->device_in, irq, ctx->cpu);
942 if (ctx->promiscuous)
943 ifflags = device_enter_promiscuous_mode(ctx->device_in);
945 if (dump_to_pcap(ctx) && __pcap_io->init_once_pcap)
946 __pcap_io->init_once_pcap();
948 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
950 if (dump_to_pcap(ctx)) {
951 __label__ try_file;
952 struct stat stats;
954 fmemset(&stats, 0, sizeof(stats));
955 ret = stat(ctx->device_out, &stats);
956 if (ret < 0) {
957 ctx->dump_dir = 0;
958 goto try_file;
961 ctx->dump_dir = S_ISDIR(stats.st_mode);
962 if (ctx->dump_dir) {
963 fd = begin_multi_pcap_file(ctx);
964 } else {
965 try_file:
966 fd = begin_single_pcap_file(ctx);
970 printf("Running! Hang up with ^C!\n\n");
971 fflush(stdout);
973 bug_on(gettimeofday(&start, NULL));
975 while (likely(sigint == 0)) {
976 while (user_may_pull_from_rx_block((pbd = (void *)
977 rx_ring.frames[it].iov_base))) {
978 walk_t3_block(pbd, ctx, sock, &fd, &frame_count);
980 kernel_may_pull_from_rx_block(pbd);
981 it = (it + 1) % rx_ring.layout3.tp_block_nr;
983 if (unlikely(sigint == 1))
984 break;
987 ret = poll(&rx_poll, 1, -1);
988 if (unlikely(ret < 0))
989 panic("Poll failed!\n");
992 bug_on(gettimeofday(&end, NULL));
993 timersub(&end, &start, &diff);
995 if (!(ctx->dump_dir && ctx->print_mode == PRINT_NONE)) {
996 sock_rx_net_stats(sock, frame_count);
998 printf("\r%12lu sec, %lu usec in total\n",
999 diff.tv_sec, diff.tv_usec);
1000 } else {
1001 printf("\n\n");
1002 fflush(stdout);
1005 bpf_release(&bpf_ops);
1006 dissector_cleanup_all();
1007 destroy_rx_ring(sock, &rx_ring);
1009 if (ctx->promiscuous)
1010 device_leave_promiscuous_mode(ctx->device_in, ifflags);
1012 if (ctx->rfraw)
1013 leave_rfmon_mac80211(ctx->device_in);
1015 if (dump_to_pcap(ctx)) {
1016 if (ctx->dump_dir)
1017 finish_multi_pcap_file(ctx, fd);
1018 else
1019 finish_single_pcap_file(ctx, fd);
1022 close(sock);
1025 static void init_ctx(struct ctx *ctx)
1027 memset(ctx, 0, sizeof(*ctx));
1028 ctx->uid = getuid();
1029 ctx->uid = getgid();
1031 ctx->cpu = -1;
1032 ctx->packet_type = -1;
1034 ctx->magic = ORIGINAL_TCPDUMP_MAGIC;
1035 ctx->print_mode = PRINT_NORM;
1036 ctx->pcap = PCAP_OPS_SG;
1038 ctx->dump_mode = DUMP_INTERVAL_TIME;
1039 ctx->dump_interval = 60;
1041 ctx->promiscuous = true;
1042 ctx->randomize = false;
1045 static void destroy_ctx(struct ctx *ctx)
1047 free(ctx->device_in);
1048 free(ctx->device_out);
1049 free(ctx->device_trans);
1051 free(ctx->prefix);
1054 static void __noreturn help(void)
1056 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING);
1057 puts("http://www.netsniff-ng.org\n\n"
1058 "Usage: netsniff-ng [options] [filter-expression]\n"
1059 "Options:\n"
1060 " -i|-d|--dev|--in <dev|pcap|-> Input source as netdev, pcap or pcap stdin\n"
1061 " -o|--out <dev|pcap|dir|cfg|-> Output sink as netdev, pcap, directory, trafgen, or stdout\n"
1062 " -f|--filter <bpf-file|expr> Use BPF filter file from bpfc or tcpdump-like expression\n"
1063 " -t|--type <type> Filter for: host|broadcast|multicast|others|outgoing\n"
1064 " -F|--interval <size|time> Dump interval if -o is a dir: <num>KiB/MiB/GiB/s/sec/min/hrs\n"
1065 " -R|--rfraw Capture or inject raw 802.11 frames\n"
1066 " -n|--num <0|uint> Number of packets until exit (def: 0)\n"
1067 " -P|--prefix <name> Prefix for pcaps stored in directory\n"
1068 " -T|--magic <pcap-magic> Pcap magic number/pcap format to store, see -D\n"
1069 " -D|--dump-pcap-types Dump pcap types and magic numbers and quit\n"
1070 " -B|--dump-bpf Dump generated BPF assembly\n"
1071 " -r|--rand Randomize packet forwarding order (dev->dev)\n"
1072 " -M|--no-promisc No promiscuous mode for netdev\n"
1073 " -A|--no-sock-mem Don't tune core socket memory\n"
1074 " -m|--mmap Mmap(2) pcap file i.e., for replaying pcaps\n"
1075 " -G|--sg Scatter/gather pcap file I/O\n"
1076 " -c|--clrw Use slower read(2)/write(2) I/O\n"
1077 " -S|--ring-size <size> Specify ring size to: <num>KiB/MiB/GiB\n"
1078 " -k|--kernel-pull <uint> Kernel pull from user interval in us (def: 10us)\n"
1079 " -J|--jumbo-support Support replay/fwd 64KB Super Jumbo Frames (def: 2048B)\n"
1080 " -b|--bind-cpu <cpu> Bind to specific CPU\n"
1081 " -u|--user <userid> Drop privileges and change to userid\n"
1082 " -g|--group <groupid> Drop privileges and change to groupid\n"
1083 " -H|--prio-high Make this high priority process\n"
1084 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
1085 " -s|--silent Do not print captured packets\n"
1086 " -q|--less Print less-verbose packet information\n"
1087 " -X|--hex Print packet data in hex format\n"
1088 " -l|--ascii Print human-readable packet data\n"
1089 " -U|--update Update GeoIP databases\n"
1090 " -V|--verbose Be more verbose\n"
1091 " -v|--version Show version and exit\n"
1092 " -h|--help Guess what?!\n\n"
1093 "Examples:\n"
1094 " netsniff-ng --in eth0 --out dump.pcap -s -T 0xa1b2c3d4 --b 0 tcp or udp\n"
1095 " netsniff-ng --in wlan0 --rfraw --out dump.pcap --silent --bind-cpu 0\n"
1096 " netsniff-ng --in dump.pcap --mmap --out eth0 -k1000 --silent --bind-cpu 0\n"
1097 " netsniff-ng --in dump.pcap --out dump.cfg --silent --bind-cpu 0\n"
1098 " netsniff-ng --in eth0 --out eth1 --silent --bind-cpu 0 -J --type host\n"
1099 " netsniff-ng --in eth1 --out /opt/probe/ -s -m --interval 100MiB -b 0\n"
1100 " netsniff-ng --in vlan0 --out dump.pcap -c -u `id -u bob` -g `id -g bob`\n"
1101 " netsniff-ng --in any --filter http.bpf --jumbo-support --ascii -V\n\n"
1102 "Note:\n"
1103 " For introducing bit errors, delays with random variation and more\n"
1104 " while replaying pcaps, make use of tc(8) with its disciplines (e.g. netem).\n\n"
1105 "Please report bugs to <bugs@netsniff-ng.org>\n"
1106 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1107 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1108 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1109 "Swiss federal institute of technology (ETH Zurich)\n"
1110 "License: GNU GPL version 2.0\n"
1111 "This is free software: you are free to change and redistribute it.\n"
1112 "There is NO WARRANTY, to the extent permitted by law.\n");
1113 die();
1116 static void __noreturn version(void)
1118 printf("\nnetsniff-ng %s, Git id: %s\n", VERSION_LONG, GITVERSION);
1119 puts("the packet sniffing beast\n"
1120 "http://www.netsniff-ng.org\n\n"
1121 "Please report bugs to <bugs@netsniff-ng.org>\n"
1122 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1123 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1124 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1125 "Swiss federal institute of technology (ETH Zurich)\n"
1126 "License: GNU GPL version 2.0\n"
1127 "This is free software: you are free to change and redistribute it.\n"
1128 "There is NO WARRANTY, to the extent permitted by law.\n");
1129 die();
1132 int main(int argc, char **argv)
1134 char *ptr;
1135 int c, i, j, cpu_tmp, opt_index, ops_touched = 0, vals[4] = {0};
1136 bool prio_high = false, setsockmem = true;
1137 void (*main_loop)(struct ctx *ctx) = NULL;
1138 struct ctx ctx;
1140 init_ctx(&ctx);
1141 srand(time(NULL));
1143 while ((c = getopt_long(argc, argv, short_options, long_options,
1144 &opt_index)) != EOF) {
1145 switch (c) {
1146 case 'd':
1147 case 'i':
1148 ctx.device_in = xstrdup(optarg);
1149 break;
1150 case 'o':
1151 ctx.device_out = xstrdup(optarg);
1152 break;
1153 case 'P':
1154 ctx.prefix = xstrdup(optarg);
1155 break;
1156 case 'R':
1157 ctx.link_type = LINKTYPE_IEEE802_11;
1158 ctx.rfraw = 1;
1159 break;
1160 case 'r':
1161 ctx.randomize = true;
1162 break;
1163 case 'J':
1164 ctx.jumbo = true;
1165 break;
1166 case 'T':
1167 ctx.magic = (uint32_t) strtoul(optarg, NULL, 0);
1168 pcap_check_magic(ctx.magic);
1169 break;
1170 case 'f':
1171 ctx.filter = xstrdup(optarg);
1172 break;
1173 case 'M':
1174 ctx.promiscuous = false;
1175 break;
1176 case 'A':
1177 setsockmem = false;
1178 break;
1179 case 'u':
1180 ctx.uid = strtoul(optarg, NULL, 0);
1181 ctx.enforce = true;
1182 break;
1183 case 'g':
1184 ctx.gid = strtoul(optarg, NULL, 0);
1185 ctx.enforce = true;
1186 break;
1187 case 't':
1188 if (!strncmp(optarg, "host", strlen("host")))
1189 ctx.packet_type = PACKET_HOST;
1190 else if (!strncmp(optarg, "broadcast", strlen("broadcast")))
1191 ctx.packet_type = PACKET_BROADCAST;
1192 else if (!strncmp(optarg, "multicast", strlen("multicast")))
1193 ctx.packet_type = PACKET_MULTICAST;
1194 else if (!strncmp(optarg, "others", strlen("others")))
1195 ctx.packet_type = PACKET_OTHERHOST;
1196 else if (!strncmp(optarg, "outgoing", strlen("outgoing")))
1197 ctx.packet_type = PACKET_OUTGOING;
1198 else
1199 ctx.packet_type = -1;
1200 break;
1201 case 'S':
1202 ptr = optarg;
1203 ctx.reserve_size = 0;
1205 for (j = i = strlen(optarg); i > 0; --i) {
1206 if (!isdigit(optarg[j - i]))
1207 break;
1208 ptr++;
1211 if (!strncmp(ptr, "KiB", strlen("KiB")))
1212 ctx.reserve_size = 1 << 10;
1213 else if (!strncmp(ptr, "MiB", strlen("MiB")))
1214 ctx.reserve_size = 1 << 20;
1215 else if (!strncmp(ptr, "GiB", strlen("GiB")))
1216 ctx.reserve_size = 1 << 30;
1217 else
1218 panic("Syntax error in ring size param!\n");
1219 *ptr = 0;
1221 ctx.reserve_size *= strtol(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 = strtol(optarg, NULL, 0);
1266 break;
1267 case 'n':
1268 frame_count_max = strtol(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 *ptr = 0;
1306 ctx.dump_interval *= strtol(optarg, NULL, 0);
1307 break;
1308 case 'V':
1309 ctx.verbose = 1;
1310 break;
1311 case 'B':
1312 ctx.dump_bpf = true;
1313 break;
1314 case 'D':
1315 pcap_dump_type_features();
1316 die();
1317 break;
1318 case 'U':
1319 update_geoip();
1320 die();
1321 break;
1322 case 'v':
1323 version();
1324 break;
1325 case 'h':
1326 help();
1327 break;
1328 case '?':
1329 switch (optopt) {
1330 case 'd':
1331 case 'i':
1332 case 'o':
1333 case 'f':
1334 case 't':
1335 case 'P':
1336 case 'F':
1337 case 'n':
1338 case 'S':
1339 case 'b':
1340 case 'k':
1341 case 'T':
1342 case 'u':
1343 case 'g':
1344 case 'e':
1345 panic("Option -%c requires an argument!\n",
1346 optopt);
1347 default:
1348 if (isprint(optopt))
1349 printf("Unknown option character `0x%X\'!\n", optopt);
1350 die();
1352 default:
1353 break;
1357 if (!ctx.filter && optind != argc) {
1358 int ret;
1359 off_t offset = 0;
1361 for (i = optind; i < argc; ++i) {
1362 size_t alen = strlen(argv[i]) + 2;
1363 size_t flen = ctx.filter ? strlen(ctx.filter) : 0;
1365 ctx.filter = xrealloc(ctx.filter, 1, flen + alen);
1366 ret = slprintf(ctx.filter + offset, strlen(argv[i]) + 2, "%s ", argv[i]);
1367 if (ret < 0)
1368 panic("Cannot concatenate filter string!\n");
1369 else
1370 offset += ret;
1374 if (!ctx.device_in)
1375 ctx.device_in = xstrdup("any");
1377 register_signal(SIGINT, signal_handler);
1378 register_signal(SIGHUP, signal_handler);
1380 tprintf_init();
1382 if (prio_high) {
1383 set_proc_prio(-20);
1384 set_sched_status(SCHED_FIFO, sched_get_priority_max(SCHED_FIFO));
1387 if (ctx.device_in && (device_mtu(ctx.device_in) ||
1388 !strncmp("any", ctx.device_in, strlen(ctx.device_in)))) {
1389 if (!ctx.rfraw)
1390 ctx.link_type = pcap_devtype_to_linktype(ctx.device_in);
1391 if (!ctx.device_out) {
1392 ctx.dump = 0;
1393 main_loop = recv_only_or_dump;
1394 } else if (device_mtu(ctx.device_out)) {
1395 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1396 main_loop = receive_to_xmit;
1397 } else {
1398 ctx.dump = 1;
1399 register_signal_f(SIGALRM, timer_next_dump, SA_SIGINFO);
1400 main_loop = recv_only_or_dump;
1401 if (!ops_touched)
1402 ctx.pcap = PCAP_OPS_SG;
1404 } else {
1405 if (ctx.device_out && device_mtu(ctx.device_out)) {
1406 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1407 main_loop = pcap_to_xmit;
1408 if (!ops_touched)
1409 ctx.pcap = PCAP_OPS_MM;
1410 } else {
1411 main_loop = read_pcap;
1412 if (!ops_touched)
1413 ctx.pcap = PCAP_OPS_SG;
1417 bug_on(!main_loop);
1419 init_geoip(0);
1420 if (setsockmem)
1421 set_system_socket_memory(vals, array_size(vals));
1422 if (!ctx.enforce)
1423 xlockme();
1425 main_loop(&ctx);
1427 if (!ctx.enforce)
1428 xunlockme();
1429 if (setsockmem)
1430 reset_system_socket_memory(vals, array_size(vals));
1431 destroy_geoip();
1433 device_restore_irq_affinity_list();
1434 tprintf_cleanup();
1436 destroy_ctx(&ctx);
1437 return 0;