trafgen: Use strtoul() instead of strtol() for unsigned long
[netsniff-ng.git] / netsniff-ng.c
blob8cbe4843e19edc5e2d68e2f459dfff5f02c76530
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 uint8_t *out = NULL;
172 int irq, ifindex, fd = 0, ret;
173 unsigned int size, it = 0;
174 unsigned long trunced = 0;
175 struct ring tx_ring;
176 struct frame_map *hdr;
177 struct sock_fprog bpf_ops;
178 struct timeval start, end, diff;
179 pcap_pkthdr_t phdr;
181 if (!device_up_and_running(ctx->device_out) && !ctx->rfraw)
182 panic("Device not up and running!\n");
184 bug_on(!__pcap_io);
186 tx_sock = pf_socket();
188 if (!strncmp("-", ctx->device_in, strlen("-"))) {
189 fd = dup_or_die(fileno(stdin));
190 close(fileno(stdin));
191 if (ctx->pcap == PCAP_OPS_MM)
192 ctx->pcap = PCAP_OPS_SG;
193 } else {
194 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
197 if (__pcap_io->init_once_pcap)
198 __pcap_io->init_once_pcap();
200 ret = __pcap_io->pull_fhdr_pcap(fd, &ctx->magic, &ctx->link_type);
201 if (ret)
202 panic("Error reading pcap header!\n");
204 if (__pcap_io->prepare_access_pcap) {
205 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_RD, ctx->jumbo);
206 if (ret)
207 panic("Error prepare reading pcap!\n");
210 fmemset(&tx_ring, 0, sizeof(tx_ring));
211 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
213 if (ctx->rfraw) {
214 ctx->device_trans = xstrdup(ctx->device_out);
215 xfree(ctx->device_out);
217 enter_rfmon_mac80211(ctx->device_trans, &ctx->device_out);
218 if (ctx->link_type != LINKTYPE_IEEE802_11)
219 panic("Wrong linktype of pcap!\n");
222 ifindex = device_ifindex(ctx->device_out);
224 size = ring_size(ctx->device_out, ctx->reserve_size);
226 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
227 if (ctx->dump_bpf)
228 bpf_dump_all(&bpf_ops);
230 set_packet_loss_discard(tx_sock);
232 setup_tx_ring_layout(tx_sock, &tx_ring, size, ctx->jumbo);
233 create_tx_ring(tx_sock, &tx_ring, ctx->verbose);
234 mmap_tx_ring(tx_sock, &tx_ring);
235 alloc_tx_ring_frames(tx_sock, &tx_ring);
236 bind_tx_ring(tx_sock, &tx_ring, ifindex);
238 dissector_init_all(ctx->print_mode);
240 if (ctx->cpu >= 0 && ifindex > 0) {
241 irq = device_irq_number(ctx->device_out);
242 device_set_irq_affinity(irq, ctx->cpu);
244 if (ctx->verbose)
245 printf("IRQ: %s:%d > CPU%d\n",
246 ctx->device_out, irq, ctx->cpu);
249 if (ctx->kpull)
250 interval = ctx->kpull;
252 set_itimer_interval_value(&itimer, 0, interval);
253 setitimer(ITIMER_REAL, &itimer, NULL);
255 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
257 printf("Running! Hang up with ^C!\n\n");
258 fflush(stdout);
260 bug_on(gettimeofday(&start, NULL));
262 while (likely(sigint == 0)) {
263 while (user_may_pull_from_tx(tx_ring.frames[it].iov_base)) {
264 hdr = tx_ring.frames[it].iov_base;
265 out = ((uint8_t *) hdr) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
267 do {
268 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic, out,
269 ring_frame_size(&tx_ring));
270 if (unlikely(ret <= 0))
271 goto out;
273 if (ring_frame_size(&tx_ring) <
274 pcap_get_length(&phdr, ctx->magic)) {
275 pcap_set_length(&phdr, ctx->magic,
276 ring_frame_size(&tx_ring));
277 trunced++;
279 } while (ctx->filter &&
280 !bpf_run_filter(&bpf_ops, out,
281 pcap_get_length(&phdr, ctx->magic)));
283 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &hdr->tp_h, NULL);
285 ctx->tx_bytes += hdr->tp_h.tp_len;;
286 ctx->tx_packets++;
288 show_frame_hdr(hdr, ctx->print_mode);
290 dissector_entry_point(out, hdr->tp_h.tp_snaplen,
291 ctx->link_type, ctx->print_mode);
293 kernel_may_pull_from_tx(&hdr->tp_h);
295 it++;
296 if (it >= tx_ring.layout.tp_frame_nr)
297 it = 0;
299 if (unlikely(sigint == 1))
300 break;
302 if (frame_count_max != 0) {
303 if (ctx->tx_packets >= frame_count_max) {
304 sigint = 1;
305 break;
311 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 hdr_in = rx_ring.frames[it_in].iov_base;
413 in = ((uint8_t *) hdr_in) + hdr_in->tp_h.tp_mac;
415 frame_count++;
417 if (ctx->packet_type != -1)
418 if (ctx->packet_type != hdr_in->s_ll.sll_pkttype)
419 goto next;
421 hdr_out = tx_ring.frames[it_out].iov_base;
422 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
424 for (; !user_may_pull_from_tx(tx_ring.frames[it_out].iov_base) &&
425 likely(!sigint);) {
426 if (ctx->randomize)
427 next_rnd_slot(&it_out, &tx_ring);
428 else {
429 it_out++;
430 if (it_out >= tx_ring.layout.tp_frame_nr)
431 it_out = 0;
434 hdr_out = tx_ring.frames[it_out].iov_base;
435 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
438 tpacket_hdr_clone(&hdr_out->tp_h, &hdr_in->tp_h);
439 fmemcpy(out, in, hdr_in->tp_h.tp_len);
441 kernel_may_pull_from_tx(&hdr_out->tp_h);
442 if (ctx->randomize)
443 next_rnd_slot(&it_out, &tx_ring);
444 else {
445 it_out++;
446 if (it_out >= tx_ring.layout.tp_frame_nr)
447 it_out = 0;
450 show_frame_hdr(hdr_in, ctx->print_mode);
452 dissector_entry_point(in, hdr_in->tp_h.tp_snaplen,
453 ctx->link_type, ctx->print_mode);
455 if (frame_count_max != 0) {
456 if (frame_count >= frame_count_max) {
457 sigint = 1;
458 break;
462 next:
463 kernel_may_pull_from_rx(&hdr_in->tp_h);
465 it_in++;
466 if (it_in >= rx_ring.layout.tp_frame_nr)
467 it_in = 0;
469 if (unlikely(sigint == 1))
470 goto out;
473 ret = poll(&rx_poll, 1, -1);
474 if (unlikely(ret < 0)) {
475 if (errno != EINTR)
476 panic("Poll failed!\n");
480 out:
481 timer_purge();
483 sock_rx_net_stats(rx_sock, 0);
485 bpf_release(&bpf_ops);
487 dissector_cleanup_all();
489 destroy_tx_ring(tx_sock, &tx_ring);
490 destroy_rx_ring(rx_sock, &rx_ring);
492 if (ctx->promiscuous)
493 device_leave_promiscuous_mode(ctx->device_in, ifflags);
495 close(tx_sock);
496 close(rx_sock);
499 static void translate_pcap_to_txf(int fdo, uint8_t *out, size_t len)
501 size_t bytes_done = 0;
502 char bout[80];
504 slprintf(bout, sizeof(bout), "{\n ");
505 write_or_die(fdo, bout, strlen(bout));
507 while (bytes_done < len) {
508 slprintf(bout, sizeof(bout), "0x%02x, ", out[bytes_done]);
509 write_or_die(fdo, bout, strlen(bout));
511 bytes_done++;
513 if (bytes_done % 10 == 0) {
514 slprintf(bout, sizeof(bout), "\n");
515 write_or_die(fdo, bout, strlen(bout));
517 if (bytes_done < len) {
518 slprintf(bout, sizeof(bout), " ");
519 write_or_die(fdo, bout, strlen(bout));
523 if (bytes_done % 10 != 0) {
524 slprintf(bout, sizeof(bout), "\n");
525 write_or_die(fdo, bout, strlen(bout));
528 slprintf(bout, sizeof(bout), "}\n\n");
529 write_or_die(fdo, bout, strlen(bout));
532 static void read_pcap(struct ctx *ctx)
534 uint8_t *out;
535 int ret, fd, fdo = 0;
536 unsigned long trunced = 0;
537 size_t out_len;
538 pcap_pkthdr_t phdr;
539 struct sock_fprog bpf_ops;
540 struct frame_map fm;
541 struct timeval start, end, diff;
543 bug_on(!__pcap_io);
545 if (!strncmp("-", ctx->device_in, strlen("-"))) {
546 fd = dup_or_die(fileno(stdin));
547 close(fileno(stdin));
548 if (ctx->pcap == PCAP_OPS_MM)
549 ctx->pcap = PCAP_OPS_SG;
550 } else {
551 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
554 if (__pcap_io->init_once_pcap)
555 __pcap_io->init_once_pcap();
557 ret = __pcap_io->pull_fhdr_pcap(fd, &ctx->magic, &ctx->link_type);
558 if (ret)
559 panic("Error reading pcap header!\n");
561 if (__pcap_io->prepare_access_pcap) {
562 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_RD, ctx->jumbo);
563 if (ret)
564 panic("Error prepare reading pcap!\n");
567 fmemset(&fm, 0, sizeof(fm));
568 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
570 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
571 if (ctx->dump_bpf)
572 bpf_dump_all(&bpf_ops);
574 dissector_init_all(ctx->print_mode);
576 out_len = round_up(1024 * 1024, RUNTIME_PAGE_SIZE);
577 out = xmalloc_aligned(out_len, CO_CACHE_LINE_SIZE);
579 if (ctx->device_out) {
580 if (!strncmp("-", ctx->device_out, strlen("-"))) {
581 fdo = dup_or_die(fileno(stdout));
582 close(fileno(stdout));
583 } else {
584 fdo = open_or_die_m(ctx->device_out, O_RDWR | O_CREAT |
585 O_TRUNC | O_LARGEFILE, DEFFILEMODE);
589 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
591 printf("Running! Hang up with ^C!\n\n");
592 fflush(stdout);
594 bug_on(gettimeofday(&start, NULL));
596 while (likely(sigint == 0)) {
597 do {
598 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic,
599 out, out_len);
600 if (unlikely(ret < 0))
601 goto out;
603 if (unlikely(pcap_get_length(&phdr, ctx->magic) == 0)) {
604 trunced++;
605 continue;
608 if (unlikely(pcap_get_length(&phdr, ctx->magic) > out_len)) {
609 pcap_set_length(&phdr, ctx->magic, out_len);
610 trunced++;
612 } while (ctx->filter &&
613 !bpf_run_filter(&bpf_ops, out,
614 pcap_get_length(&phdr, ctx->magic)));
616 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &fm.tp_h, &fm.s_ll);
618 ctx->tx_bytes += fm.tp_h.tp_len;
619 ctx->tx_packets++;
621 show_frame_hdr(&fm, ctx->print_mode);
623 dissector_entry_point(out, fm.tp_h.tp_snaplen,
624 ctx->link_type, ctx->print_mode);
626 if (ctx->device_out)
627 translate_pcap_to_txf(fdo, out, fm.tp_h.tp_snaplen);
629 if (frame_count_max != 0) {
630 if (ctx->tx_packets >= frame_count_max) {
631 sigint = 1;
632 break;
637 out:
639 bug_on(gettimeofday(&end, NULL));
640 timersub(&end, &start, &diff);
642 bpf_release(&bpf_ops);
644 dissector_cleanup_all();
646 if (__pcap_io->prepare_close_pcap)
647 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
649 xfree(out);
651 fflush(stdout);
652 printf("\n");
653 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
654 printf("\r%12lu packets truncated in file\n", trunced);
655 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
656 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
658 if (!strncmp("-", ctx->device_in, strlen("-")))
659 dup2(fd, fileno(stdin));
660 close(fd);
662 if (ctx->device_out) {
663 if (!strncmp("-", ctx->device_out, strlen("-")))
664 dup2(fdo, fileno(stdout));
665 close(fdo);
669 static void finish_multi_pcap_file(struct ctx *ctx, int fd)
671 __pcap_io->fsync_pcap(fd);
673 if (__pcap_io->prepare_close_pcap)
674 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
676 close(fd);
678 fmemset(&itimer, 0, sizeof(itimer));
679 setitimer(ITIMER_REAL, &itimer, NULL);
682 static int next_multi_pcap_file(struct ctx *ctx, int fd)
684 int ret;
685 char fname[512];
687 __pcap_io->fsync_pcap(fd);
689 if (__pcap_io->prepare_close_pcap)
690 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
692 close(fd);
694 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
695 ctx->prefix ? : "dump-", time(NULL));
697 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
698 O_LARGEFILE, DEFFILEMODE);
700 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
701 if (ret)
702 panic("Error writing pcap header!\n");
704 if (__pcap_io->prepare_access_pcap) {
705 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, true);
706 if (ret)
707 panic("Error prepare writing pcap!\n");
710 return fd;
713 static int begin_multi_pcap_file(struct ctx *ctx)
715 int fd, ret;
716 char fname[256];
718 bug_on(!__pcap_io);
720 if (ctx->device_out[strlen(ctx->device_out) - 1] == '/')
721 ctx->device_out[strlen(ctx->device_out) - 1] = 0;
723 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
724 ctx->prefix ? : "dump-", time(NULL));
726 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
727 O_LARGEFILE, DEFFILEMODE);
729 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
730 if (ret)
731 panic("Error writing pcap header!\n");
733 if (__pcap_io->prepare_access_pcap) {
734 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, true);
735 if (ret)
736 panic("Error prepare writing pcap!\n");
739 if (ctx->dump_mode == DUMP_INTERVAL_TIME) {
740 interval = ctx->dump_interval;
742 set_itimer_interval_value(&itimer, interval, 0);
743 setitimer(ITIMER_REAL, &itimer, NULL);
744 } else {
745 interval = 0;
748 return fd;
751 static void finish_single_pcap_file(struct ctx *ctx, int fd)
753 __pcap_io->fsync_pcap(fd);
755 if (__pcap_io->prepare_close_pcap)
756 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
758 if (strncmp("-", ctx->device_out, strlen("-")))
759 close(fd);
760 else
761 dup2(fd, fileno(stdout));
764 static int begin_single_pcap_file(struct ctx *ctx)
766 int fd, ret;
768 bug_on(!__pcap_io);
770 if (!strncmp("-", ctx->device_out, strlen("-"))) {
771 fd = dup_or_die(fileno(stdout));
772 close(fileno(stdout));
773 if (ctx->pcap == PCAP_OPS_MM)
774 ctx->pcap = PCAP_OPS_SG;
775 } else {
776 fd = open_or_die_m(ctx->device_out,
777 O_RDWR | O_CREAT | O_TRUNC |
778 O_LARGEFILE, DEFFILEMODE);
781 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
782 if (ret)
783 panic("Error writing pcap header!\n");
785 if (__pcap_io->prepare_access_pcap) {
786 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, true);
787 if (ret)
788 panic("Error prepare writing pcap!\n");
791 return fd;
794 static void print_pcap_file_stats(int sock, struct ctx *ctx)
796 int ret;
797 struct tpacket_stats kstats;
798 socklen_t slen = sizeof(kstats);
800 fmemset(&kstats, 0, sizeof(kstats));
802 ret = getsockopt(sock, SOL_PACKET, PACKET_STATISTICS, &kstats, &slen);
803 if (unlikely(ret))
804 panic("Cannot get packet statistics!\n");
806 if (ctx->print_mode == PRINT_NONE) {
807 printf(".(+%u/-%u)", kstats.tp_packets - kstats.tp_drops,
808 kstats.tp_drops);
809 fflush(stdout);
813 static void walk_t3_block(struct block_desc *pbd, struct ctx *ctx,
814 int sock, int *fd, unsigned long *frame_count)
816 uint8_t *packet;
817 int num_pkts = pbd->h1.num_pkts, i, ret;
818 struct tpacket3_hdr *hdr;
819 pcap_pkthdr_t phdr;
820 struct sockaddr_ll *sll;
822 hdr = (void *) ((uint8_t *) pbd + pbd->h1.offset_to_first_pkt);
823 sll = (void *) ((uint8_t *) hdr + TPACKET_ALIGN(sizeof(*hdr)));
825 for (i = 0; i < num_pkts && likely(sigint == 0); ++i) {
826 packet = ((uint8_t *) hdr + hdr->tp_mac);
828 if (ctx->packet_type != -1)
829 if (ctx->packet_type != sll->sll_pkttype)
830 goto next;
832 (*frame_count)++;
834 if (dump_to_pcap(ctx)) {
835 tpacket3_hdr_to_pcap_pkthdr(hdr, sll, &phdr, ctx->magic);
837 ret = __pcap_io->write_pcap(*fd, &phdr, ctx->magic, packet,
838 pcap_get_length(&phdr, ctx->magic));
839 if (unlikely(ret != (int) pcap_get_total_length(&phdr, ctx->magic)))
840 panic("Write error to pcap!\n");
843 __show_frame_hdr(sll, hdr, ctx->print_mode, true);
845 dissector_entry_point(packet, hdr->tp_snaplen, ctx->link_type,
846 ctx->print_mode);
847 next:
848 hdr = (void *) ((uint8_t *) hdr + hdr->tp_next_offset);
849 sll = (void *) ((uint8_t *) hdr + TPACKET_ALIGN(sizeof(*hdr)));
851 if (frame_count_max != 0) {
852 if (unlikely(*frame_count >= frame_count_max)) {
853 sigint = 1;
854 break;
858 if (dump_to_pcap(ctx)) {
859 if (ctx->dump_mode == DUMP_INTERVAL_SIZE) {
860 interval += hdr->tp_snaplen;
861 if (interval > ctx->dump_interval) {
862 next_dump = true;
863 interval = 0;
867 if (next_dump) {
868 *fd = next_multi_pcap_file(ctx, *fd);
869 next_dump = false;
871 if (unlikely(ctx->verbose))
872 print_pcap_file_stats(sock, ctx);
878 static void recv_only_or_dump(struct ctx *ctx)
880 short ifflags = 0;
881 int sock, irq, ifindex, fd = 0, ret;
882 unsigned int size, it = 0;
883 struct ring rx_ring;
884 struct pollfd rx_poll;
885 struct sock_fprog bpf_ops;
886 struct timeval start, end, diff;
887 struct block_desc *pbd;
888 unsigned long frame_count = 0;
890 sock = pf_socket();
892 if (ctx->rfraw) {
893 ctx->device_trans = xstrdup(ctx->device_in);
894 xfree(ctx->device_in);
896 enter_rfmon_mac80211(ctx->device_trans, &ctx->device_in);
897 ctx->link_type = LINKTYPE_IEEE802_11;
900 fmemset(&rx_ring, 0, sizeof(rx_ring));
901 fmemset(&rx_poll, 0, sizeof(rx_poll));
902 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
904 ifindex = device_ifindex(ctx->device_in);
906 size = ring_size(ctx->device_in, ctx->reserve_size);
908 enable_kernel_bpf_jit_compiler();
910 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
911 if (ctx->dump_bpf)
912 bpf_dump_all(&bpf_ops);
913 bpf_attach_to_sock(sock, &bpf_ops);
915 ret = set_sockopt_hwtimestamp(sock, ctx->device_in);
916 if (ret == 0 && ctx->verbose)
917 printf("HW timestamping enabled\n");
919 setup_rx_ring_layout(sock, &rx_ring, size, true, true);
920 create_rx_ring(sock, &rx_ring, ctx->verbose);
921 mmap_rx_ring(sock, &rx_ring);
922 alloc_rx_ring_frames(sock, &rx_ring);
923 bind_rx_ring(sock, &rx_ring, ifindex);
925 prepare_polling(sock, &rx_poll);
926 dissector_init_all(ctx->print_mode);
928 if (ctx->cpu >= 0 && ifindex > 0) {
929 irq = device_irq_number(ctx->device_in);
930 device_set_irq_affinity(irq, ctx->cpu);
932 if (ctx->verbose)
933 printf("IRQ: %s:%d > CPU%d\n",
934 ctx->device_in, irq, ctx->cpu);
937 if (ctx->promiscuous)
938 ifflags = device_enter_promiscuous_mode(ctx->device_in);
940 if (dump_to_pcap(ctx) && __pcap_io->init_once_pcap)
941 __pcap_io->init_once_pcap();
943 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
945 if (dump_to_pcap(ctx)) {
946 struct stat stats;
948 ret = stat(ctx->device_out, &stats);
949 if (ret < 0)
950 ctx->dump_dir = 0;
951 else
952 ctx->dump_dir = S_ISDIR(stats.st_mode);
954 if (ctx->dump_dir)
955 fd = begin_multi_pcap_file(ctx);
956 else
957 fd = begin_single_pcap_file(ctx);
960 printf("Running! Hang up with ^C!\n\n");
961 fflush(stdout);
963 bug_on(gettimeofday(&start, NULL));
965 while (likely(sigint == 0)) {
966 while (user_may_pull_from_rx_block((pbd = (void *)
967 rx_ring.frames[it].iov_base))) {
968 walk_t3_block(pbd, ctx, sock, &fd, &frame_count);
970 kernel_may_pull_from_rx_block(pbd);
971 it = (it + 1) % rx_ring.layout3.tp_block_nr;
973 if (unlikely(sigint == 1))
974 break;
977 ret = poll(&rx_poll, 1, -1);
978 if (unlikely(ret < 0)) {
979 if (errno != EINTR)
980 panic("Poll failed!\n");
984 bug_on(gettimeofday(&end, NULL));
985 timersub(&end, &start, &diff);
987 if (!(ctx->dump_dir && ctx->print_mode == PRINT_NONE)) {
988 sock_rx_net_stats(sock, frame_count);
990 printf("\r%12lu sec, %lu usec in total\n",
991 diff.tv_sec, diff.tv_usec);
992 } else {
993 printf("\n\n");
994 fflush(stdout);
997 bpf_release(&bpf_ops);
998 dissector_cleanup_all();
999 destroy_rx_ring(sock, &rx_ring);
1001 if (ctx->promiscuous)
1002 device_leave_promiscuous_mode(ctx->device_in, ifflags);
1004 if (ctx->rfraw)
1005 leave_rfmon_mac80211(ctx->device_in);
1007 if (dump_to_pcap(ctx)) {
1008 if (ctx->dump_dir)
1009 finish_multi_pcap_file(ctx, fd);
1010 else
1011 finish_single_pcap_file(ctx, fd);
1014 close(sock);
1017 static void init_ctx(struct ctx *ctx)
1019 memset(ctx, 0, sizeof(*ctx));
1020 ctx->uid = getuid();
1021 ctx->uid = getgid();
1023 ctx->cpu = -1;
1024 ctx->packet_type = -1;
1026 ctx->magic = ORIGINAL_TCPDUMP_MAGIC;
1027 ctx->print_mode = PRINT_NORM;
1028 ctx->pcap = PCAP_OPS_SG;
1030 ctx->dump_mode = DUMP_INTERVAL_TIME;
1031 ctx->dump_interval = 60;
1033 ctx->promiscuous = true;
1034 ctx->randomize = false;
1037 static void destroy_ctx(struct ctx *ctx)
1039 free(ctx->device_in);
1040 free(ctx->device_out);
1041 free(ctx->device_trans);
1043 free(ctx->prefix);
1046 static void __noreturn help(void)
1048 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING);
1049 puts("http://www.netsniff-ng.org\n\n"
1050 "Usage: netsniff-ng [options] [filter-expression]\n"
1051 "Options:\n"
1052 " -i|-d|--dev|--in <dev|pcap|-> Input source as netdev, pcap or pcap stdin\n"
1053 " -o|--out <dev|pcap|dir|cfg|-> Output sink as netdev, pcap, directory, trafgen, or stdout\n"
1054 " -f|--filter <bpf-file|expr> Use BPF filter file from bpfc or tcpdump-like expression\n"
1055 " -t|--type <type> Filter for: host|broadcast|multicast|others|outgoing\n"
1056 " -F|--interval <size|time> Dump interval if -o is a dir: <num>KiB/MiB/GiB/s/sec/min/hrs\n"
1057 " -R|--rfraw Capture or inject raw 802.11 frames\n"
1058 " -n|--num <0|uint> Number of packets until exit (def: 0)\n"
1059 " -P|--prefix <name> Prefix for pcaps stored in directory\n"
1060 " -T|--magic <pcap-magic> Pcap magic number/pcap format to store, see -D\n"
1061 " -D|--dump-pcap-types Dump pcap types and magic numbers and quit\n"
1062 " -B|--dump-bpf Dump generated BPF assembly\n"
1063 " -r|--rand Randomize packet forwarding order (dev->dev)\n"
1064 " -M|--no-promisc No promiscuous mode for netdev\n"
1065 " -A|--no-sock-mem Don't tune core socket memory\n"
1066 " -m|--mmap Mmap(2) pcap file I/O, e.g. for replaying pcaps\n"
1067 " -G|--sg Scatter/gather pcap file I/O\n"
1068 " -c|--clrw Use slower read(2)/write(2) I/O\n"
1069 " -S|--ring-size <size> Specify ring size to: <num>KiB/MiB/GiB\n"
1070 " -k|--kernel-pull <uint> Kernel pull from user interval in us (def: 10us)\n"
1071 " -J|--jumbo-support Support replay/fwd 64KB Super Jumbo Frames (def: 2048B)\n"
1072 " -b|--bind-cpu <cpu> Bind to specific CPU\n"
1073 " -u|--user <userid> Drop privileges and change to userid\n"
1074 " -g|--group <groupid> Drop privileges and change to groupid\n"
1075 " -H|--prio-high Make this high priority process\n"
1076 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
1077 " -s|--silent Do not print captured packets\n"
1078 " -q|--less Print less-verbose packet information\n"
1079 " -X|--hex Print packet data in hex format\n"
1080 " -l|--ascii Print human-readable packet data\n"
1081 " -U|--update Update GeoIP databases\n"
1082 " -V|--verbose Be more verbose\n"
1083 " -v|--version Show version and exit\n"
1084 " -h|--help Guess what?!\n\n"
1085 "Examples:\n"
1086 " netsniff-ng --in eth0 --out dump.pcap -s -T 0xa1b2c3d4 --b 0 tcp or udp\n"
1087 " netsniff-ng --in wlan0 --rfraw --out dump.pcap --silent --bind-cpu 0\n"
1088 " netsniff-ng --in dump.pcap --mmap --out eth0 -k1000 --silent --bind-cpu 0\n"
1089 " netsniff-ng --in dump.pcap --out dump.cfg --silent --bind-cpu 0\n"
1090 " netsniff-ng --in eth0 --out eth1 --silent --bind-cpu 0 -J --type host\n"
1091 " netsniff-ng --in eth1 --out /opt/probe/ -s -m --interval 100MiB -b 0\n"
1092 " netsniff-ng --in vlan0 --out dump.pcap -c -u `id -u bob` -g `id -g bob`\n"
1093 " netsniff-ng --in any --filter http.bpf --jumbo-support --ascii -V\n\n"
1094 "Note:\n"
1095 " For introducing bit errors, delays with random variation and more\n"
1096 " while replaying pcaps, make use of tc(8) with its disciplines (e.g. netem).\n\n"
1097 "Please report bugs to <bugs@netsniff-ng.org>\n"
1098 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1099 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1100 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1101 "Swiss federal institute of technology (ETH Zurich)\n"
1102 "License: GNU GPL version 2.0\n"
1103 "This is free software: you are free to change and redistribute it.\n"
1104 "There is NO WARRANTY, to the extent permitted by law.\n");
1105 die();
1108 static void __noreturn version(void)
1110 printf("\nnetsniff-ng %s, Git id: %s\n", VERSION_LONG, GITVERSION);
1111 puts("the packet sniffing beast\n"
1112 "http://www.netsniff-ng.org\n\n"
1113 "Please report bugs to <bugs@netsniff-ng.org>\n"
1114 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1115 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1116 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1117 "Swiss federal institute of technology (ETH Zurich)\n"
1118 "License: GNU GPL version 2.0\n"
1119 "This is free software: you are free to change and redistribute it.\n"
1120 "There is NO WARRANTY, to the extent permitted by law.\n");
1121 die();
1124 int main(int argc, char **argv)
1126 char *ptr;
1127 int c, i, j, cpu_tmp, opt_index, ops_touched = 0, vals[4] = {0};
1128 bool prio_high = false, setsockmem = true;
1129 void (*main_loop)(struct ctx *ctx) = NULL;
1130 struct ctx ctx;
1132 init_ctx(&ctx);
1133 srand(time(NULL));
1135 while ((c = getopt_long(argc, argv, short_options, long_options,
1136 &opt_index)) != EOF) {
1137 switch (c) {
1138 case 'd':
1139 case 'i':
1140 ctx.device_in = xstrdup(optarg);
1141 break;
1142 case 'o':
1143 ctx.device_out = xstrdup(optarg);
1144 break;
1145 case 'P':
1146 ctx.prefix = xstrdup(optarg);
1147 break;
1148 case 'R':
1149 ctx.link_type = LINKTYPE_IEEE802_11;
1150 ctx.rfraw = 1;
1151 break;
1152 case 'r':
1153 ctx.randomize = true;
1154 break;
1155 case 'J':
1156 ctx.jumbo = true;
1157 break;
1158 case 'T':
1159 ctx.magic = (uint32_t) strtoul(optarg, NULL, 0);
1160 pcap_check_magic(ctx.magic);
1161 break;
1162 case 'f':
1163 ctx.filter = xstrdup(optarg);
1164 break;
1165 case 'M':
1166 ctx.promiscuous = false;
1167 break;
1168 case 'A':
1169 setsockmem = false;
1170 break;
1171 case 'u':
1172 ctx.uid = strtoul(optarg, NULL, 0);
1173 ctx.enforce = true;
1174 break;
1175 case 'g':
1176 ctx.gid = strtoul(optarg, NULL, 0);
1177 ctx.enforce = true;
1178 break;
1179 case 't':
1180 if (!strncmp(optarg, "host", strlen("host")))
1181 ctx.packet_type = PACKET_HOST;
1182 else if (!strncmp(optarg, "broadcast", strlen("broadcast")))
1183 ctx.packet_type = PACKET_BROADCAST;
1184 else if (!strncmp(optarg, "multicast", strlen("multicast")))
1185 ctx.packet_type = PACKET_MULTICAST;
1186 else if (!strncmp(optarg, "others", strlen("others")))
1187 ctx.packet_type = PACKET_OTHERHOST;
1188 else if (!strncmp(optarg, "outgoing", strlen("outgoing")))
1189 ctx.packet_type = PACKET_OUTGOING;
1190 else
1191 ctx.packet_type = -1;
1192 break;
1193 case 'S':
1194 ptr = optarg;
1195 ctx.reserve_size = 0;
1197 for (j = i = strlen(optarg); i > 0; --i) {
1198 if (!isdigit(optarg[j - i]))
1199 break;
1200 ptr++;
1203 if (!strncmp(ptr, "KiB", strlen("KiB")))
1204 ctx.reserve_size = 1 << 10;
1205 else if (!strncmp(ptr, "MiB", strlen("MiB")))
1206 ctx.reserve_size = 1 << 20;
1207 else if (!strncmp(ptr, "GiB", strlen("GiB")))
1208 ctx.reserve_size = 1 << 30;
1209 else
1210 panic("Syntax error in ring size param!\n");
1212 ctx.reserve_size *= strtoul(optarg, NULL, 0);
1213 break;
1214 case 'b':
1215 cpu_tmp = strtol(optarg, NULL, 0);
1217 cpu_affinity(cpu_tmp);
1218 if (ctx.cpu != -2)
1219 ctx.cpu = cpu_tmp;
1220 break;
1221 case 'H':
1222 prio_high = true;
1223 break;
1224 case 'c':
1225 ctx.pcap = PCAP_OPS_RW;
1226 ops_touched = 1;
1227 break;
1228 case 'm':
1229 ctx.pcap = PCAP_OPS_MM;
1230 ops_touched = 1;
1231 break;
1232 case 'G':
1233 ctx.pcap = PCAP_OPS_SG;
1234 ops_touched = 1;
1235 break;
1236 case 'Q':
1237 ctx.cpu = -2;
1238 break;
1239 case 's':
1240 ctx.print_mode = PRINT_NONE;
1241 break;
1242 case 'q':
1243 ctx.print_mode = PRINT_LESS;
1244 break;
1245 case 'X':
1246 ctx.print_mode =
1247 (ctx.print_mode == PRINT_ASCII) ?
1248 PRINT_HEX_ASCII : PRINT_HEX;
1249 break;
1250 case 'l':
1251 ctx.print_mode =
1252 (ctx.print_mode == PRINT_HEX) ?
1253 PRINT_HEX_ASCII : PRINT_ASCII;
1254 break;
1255 case 'k':
1256 ctx.kpull = strtoul(optarg, NULL, 0);
1257 break;
1258 case 'n':
1259 frame_count_max = strtoul(optarg, NULL, 0);
1260 break;
1261 case 'F':
1262 ptr = optarg;
1263 ctx.dump_interval = 0;
1265 for (j = i = strlen(optarg); i > 0; --i) {
1266 if (!isdigit(optarg[j - i]))
1267 break;
1268 ptr++;
1271 if (!strncmp(ptr, "KiB", strlen("KiB"))) {
1272 ctx.dump_interval = 1 << 10;
1273 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1274 } else if (!strncmp(ptr, "MiB", strlen("MiB"))) {
1275 ctx.dump_interval = 1 << 20;
1276 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1277 } else if (!strncmp(ptr, "GiB", strlen("GiB"))) {
1278 ctx.dump_interval = 1 << 30;
1279 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1280 } else if (!strncmp(ptr, "sec", strlen("sec"))) {
1281 ctx.dump_interval = 1;
1282 ctx.dump_mode = DUMP_INTERVAL_TIME;
1283 } else if (!strncmp(ptr, "min", strlen("min"))) {
1284 ctx.dump_interval = 60;
1285 ctx.dump_mode = DUMP_INTERVAL_TIME;
1286 } else if (!strncmp(ptr, "hrs", strlen("hrs"))) {
1287 ctx.dump_interval = 60 * 60;
1288 ctx.dump_mode = DUMP_INTERVAL_TIME;
1289 } else if (!strncmp(ptr, "s", strlen("s"))) {
1290 ctx.dump_interval = 1;
1291 ctx.dump_mode = DUMP_INTERVAL_TIME;
1292 } else {
1293 panic("Syntax error in time/size param!\n");
1296 ctx.dump_interval *= strtoul(optarg, NULL, 0);
1297 break;
1298 case 'V':
1299 ctx.verbose = 1;
1300 break;
1301 case 'B':
1302 ctx.dump_bpf = true;
1303 break;
1304 case 'D':
1305 pcap_dump_type_features();
1306 die();
1307 break;
1308 case 'U':
1309 update_geoip();
1310 die();
1311 break;
1312 case 'v':
1313 version();
1314 break;
1315 case 'h':
1316 help();
1317 break;
1318 case '?':
1319 switch (optopt) {
1320 case 'd':
1321 case 'i':
1322 case 'o':
1323 case 'f':
1324 case 't':
1325 case 'P':
1326 case 'F':
1327 case 'n':
1328 case 'S':
1329 case 'b':
1330 case 'k':
1331 case 'T':
1332 case 'u':
1333 case 'g':
1334 case 'e':
1335 panic("Option -%c requires an argument!\n",
1336 optopt);
1337 default:
1338 if (isprint(optopt))
1339 printf("Unknown option character `0x%X\'!\n", optopt);
1340 die();
1342 default:
1343 break;
1347 if (!ctx.filter && optind != argc) {
1348 int ret;
1349 off_t offset = 0;
1351 for (i = optind; i < argc; ++i) {
1352 size_t alen = strlen(argv[i]) + 2;
1353 size_t flen = ctx.filter ? strlen(ctx.filter) : 0;
1355 ctx.filter = xrealloc(ctx.filter, 1, flen + alen);
1356 ret = slprintf(ctx.filter + offset, strlen(argv[i]) + 2, "%s ", argv[i]);
1357 if (ret < 0)
1358 panic("Cannot concatenate filter string!\n");
1359 else
1360 offset += ret;
1364 if (!ctx.device_in)
1365 ctx.device_in = xstrdup("any");
1367 register_signal(SIGINT, signal_handler);
1368 register_signal(SIGQUIT, signal_handler);
1369 register_signal(SIGTERM, signal_handler);
1370 register_signal(SIGHUP, signal_handler);
1372 tprintf_init();
1374 if (prio_high) {
1375 set_proc_prio(-20);
1376 set_sched_status(SCHED_FIFO, sched_get_priority_max(SCHED_FIFO));
1379 if (ctx.device_in && (device_mtu(ctx.device_in) ||
1380 !strncmp("any", ctx.device_in, strlen(ctx.device_in)))) {
1381 if (!ctx.rfraw)
1382 ctx.link_type = pcap_devtype_to_linktype(ctx.device_in);
1383 if (!ctx.device_out) {
1384 ctx.dump = 0;
1385 main_loop = recv_only_or_dump;
1386 } else if (device_mtu(ctx.device_out)) {
1387 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1388 main_loop = receive_to_xmit;
1389 } else {
1390 ctx.dump = 1;
1391 register_signal_f(SIGALRM, timer_next_dump, SA_SIGINFO);
1392 main_loop = recv_only_or_dump;
1393 if (!ops_touched)
1394 ctx.pcap = PCAP_OPS_SG;
1396 } else {
1397 if (ctx.device_out && device_mtu(ctx.device_out)) {
1398 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1399 main_loop = pcap_to_xmit;
1400 if (!ops_touched)
1401 ctx.pcap = PCAP_OPS_MM;
1402 } else {
1403 main_loop = read_pcap;
1404 if (!ops_touched)
1405 ctx.pcap = PCAP_OPS_SG;
1409 bug_on(!main_loop);
1411 init_geoip(0);
1412 if (setsockmem)
1413 set_system_socket_memory(vals, array_size(vals));
1414 if (!ctx.enforce)
1415 xlockme();
1417 if (ctx.verbose)
1418 printf("pcap file I/O method: %s\n", pcap_ops_group_to_str[ctx.pcap]);
1420 main_loop(&ctx);
1422 if (!ctx.enforce)
1423 xunlockme();
1424 if (setsockmem)
1425 reset_system_socket_memory(vals, array_size(vals));
1426 destroy_geoip();
1428 device_restore_irq_affinity_list();
1429 tprintf_cleanup();
1431 destroy_ctx(&ctx);
1432 return 0;