trafgen: Remove unnecessary initialization of ct.reserve_size
[netsniff-ng.git] / netsniff-ng.c
blobf22137dbcf8e0937669cd3c6958775a77ef0ec7c
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;
58 unsigned long kpull, dump_interval, tx_bytes, tx_packets;
59 size_t reserve_size;
60 bool randomize, promiscuous, enforce, jumbo, dump_bpf, verbose;
61 enum pcap_ops_groups pcap; enum dump_mode dump_mode;
62 uid_t uid; gid_t gid; uint32_t link_type, magic;
65 static volatile sig_atomic_t sigint = 0;
66 static volatile bool next_dump = false;
68 static const char *short_options = "d:i:o:rf:MJt:S:k:n:b:HQmcsqXlvhF:RGAP:Vu:g:T:DBU";
69 static const struct option long_options[] = {
70 {"dev", required_argument, NULL, 'd'},
71 {"in", required_argument, NULL, 'i'},
72 {"out", required_argument, NULL, 'o'},
73 {"filter", required_argument, NULL, 'f'},
74 {"num", required_argument, NULL, 'n'},
75 {"type", required_argument, NULL, 't'},
76 {"interval", required_argument, NULL, 'F'},
77 {"ring-size", required_argument, NULL, 'S'},
78 {"kernel-pull", required_argument, NULL, 'k'},
79 {"bind-cpu", required_argument, NULL, 'b'},
80 {"prefix", required_argument, NULL, 'P'},
81 {"user", required_argument, NULL, 'u'},
82 {"group", required_argument, NULL, 'g'},
83 {"magic", required_argument, NULL, 'T'},
84 {"rand", no_argument, NULL, 'r'},
85 {"rfraw", no_argument, NULL, 'R'},
86 {"mmap", no_argument, NULL, 'm'},
87 {"sg", no_argument, NULL, 'G'},
88 {"clrw", no_argument, NULL, 'c'},
89 {"jumbo-support", no_argument, NULL, 'J'},
90 {"no-promisc", no_argument, NULL, 'M'},
91 {"prio-high", no_argument, NULL, 'H'},
92 {"notouch-irq", no_argument, NULL, 'Q'},
93 {"dump-pcap-types", no_argument, NULL, 'D'},
94 {"dump-bpf", no_argument, NULL, 'B'},
95 {"silent", no_argument, NULL, 's'},
96 {"less", no_argument, NULL, 'q'},
97 {"hex", no_argument, NULL, 'X'},
98 {"ascii", no_argument, NULL, 'l'},
99 {"no-sock-mem", no_argument, NULL, 'A'},
100 {"update", no_argument, NULL, 'U'},
101 {"verbose", no_argument, NULL, 'V'},
102 {"version", no_argument, NULL, 'v'},
103 {"help", no_argument, NULL, 'h'},
104 {NULL, 0, NULL, 0}
107 static int tx_sock;
108 static struct itimerval itimer;
109 static unsigned long frame_count_max = 0, interval = TX_KERNEL_PULL_INT;
111 #define __pcap_io pcap_ops[ctx->pcap]
113 static void signal_handler(int number)
115 switch (number) {
116 case SIGINT:
117 case SIGQUIT:
118 case SIGTERM:
119 sigint = 1;
120 case SIGHUP:
121 default:
122 break;
126 static void timer_elapsed(int unused __maybe_unused)
128 int ret;
130 set_itimer_interval_value(&itimer, 0, interval);
132 ret = pull_and_flush_tx_ring(tx_sock);
133 if (unlikely(ret < 0)) {
134 /* We could hit EBADF if the socket has been closed before
135 * the timer was triggered.
137 if (errno != EBADF && errno != ENOBUFS)
138 panic("Flushing TX_RING failed: %s!\n", strerror(errno));
141 setitimer(ITIMER_REAL, &itimer, NULL);
144 static void timer_purge(void)
146 int ret;
148 ret = pull_and_flush_tx_ring_wait(tx_sock);
149 if (unlikely(ret < 0)) {
150 if (errno != EBADF && errno != ENOBUFS)
151 panic("Flushing TX_RING failed: %s!\n", strerror(errno));
154 set_itimer_interval_value(&itimer, 0, 0);
155 setitimer(ITIMER_REAL, &itimer, NULL);
158 static void timer_next_dump(int unused __maybe_unused)
160 set_itimer_interval_value(&itimer, interval, 0);
161 next_dump = true;
162 setitimer(ITIMER_REAL, &itimer, NULL);
165 static inline bool dump_to_pcap(struct ctx *ctx)
167 return ctx->dump;
170 static void pcap_to_xmit(struct ctx *ctx)
172 uint8_t *out = NULL;
173 int irq, ifindex, fd = 0, ret;
174 size_t size;
175 unsigned int it = 0;
176 unsigned long trunced = 0;
177 struct ring tx_ring;
178 struct frame_map *hdr;
179 struct sock_fprog bpf_ops;
180 struct timeval start, end, diff;
181 pcap_pkthdr_t phdr;
183 if (!device_up_and_running(ctx->device_out) && !ctx->rfraw)
184 panic("Device not up and running!\n");
186 bug_on(!__pcap_io);
188 tx_sock = pf_socket();
190 if (!strncmp("-", ctx->device_in, strlen("-"))) {
191 fd = dup_or_die(fileno(stdin));
192 close(fileno(stdin));
193 if (ctx->pcap == PCAP_OPS_MM)
194 ctx->pcap = PCAP_OPS_SG;
195 } else {
196 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
199 if (__pcap_io->init_once_pcap)
200 __pcap_io->init_once_pcap();
202 ret = __pcap_io->pull_fhdr_pcap(fd, &ctx->magic, &ctx->link_type);
203 if (ret)
204 panic("Error reading pcap header!\n");
206 if (__pcap_io->prepare_access_pcap) {
207 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_RD, ctx->jumbo);
208 if (ret)
209 panic("Error prepare reading pcap!\n");
212 fmemset(&tx_ring, 0, sizeof(tx_ring));
213 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
215 if (ctx->rfraw) {
216 ctx->device_trans = xstrdup(ctx->device_out);
217 xfree(ctx->device_out);
219 enter_rfmon_mac80211(ctx->device_trans, &ctx->device_out);
220 if (ctx->link_type != LINKTYPE_IEEE802_11)
221 panic("Wrong linktype of pcap!\n");
224 ifindex = device_ifindex(ctx->device_out);
226 size = ring_size(ctx->device_out, ctx->reserve_size);
228 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
229 if (ctx->dump_bpf)
230 bpf_dump_all(&bpf_ops);
232 set_packet_loss_discard(tx_sock);
234 setup_tx_ring_layout(tx_sock, &tx_ring, size, ctx->jumbo);
235 create_tx_ring(tx_sock, &tx_ring, ctx->verbose);
236 mmap_tx_ring(tx_sock, &tx_ring);
237 alloc_tx_ring_frames(tx_sock, &tx_ring);
238 bind_tx_ring(tx_sock, &tx_ring, ifindex);
240 dissector_init_all(ctx->print_mode);
242 if (ctx->cpu >= 0 && ifindex > 0) {
243 irq = device_irq_number(ctx->device_out);
244 device_set_irq_affinity(irq, ctx->cpu);
246 if (ctx->verbose)
247 printf("IRQ: %s:%d > CPU%d\n",
248 ctx->device_out, irq, ctx->cpu);
251 if (ctx->kpull)
252 interval = ctx->kpull;
254 set_itimer_interval_value(&itimer, 0, interval);
255 setitimer(ITIMER_REAL, &itimer, NULL);
257 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
259 printf("Running! Hang up with ^C!\n\n");
260 fflush(stdout);
262 bug_on(gettimeofday(&start, NULL));
264 while (likely(sigint == 0)) {
265 while (user_may_pull_from_tx(tx_ring.frames[it].iov_base)) {
266 hdr = tx_ring.frames[it].iov_base;
267 out = ((uint8_t *) hdr) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
269 do {
270 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic, out,
271 ring_frame_size(&tx_ring));
272 if (unlikely(ret <= 0))
273 goto out;
275 if (ring_frame_size(&tx_ring) <
276 pcap_get_length(&phdr, ctx->magic)) {
277 pcap_set_length(&phdr, ctx->magic,
278 ring_frame_size(&tx_ring));
279 trunced++;
281 } while (ctx->filter &&
282 !bpf_run_filter(&bpf_ops, out,
283 pcap_get_length(&phdr, ctx->magic)));
285 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &hdr->tp_h, NULL);
287 ctx->tx_bytes += hdr->tp_h.tp_len;;
288 ctx->tx_packets++;
290 show_frame_hdr(hdr, ctx->print_mode);
292 dissector_entry_point(out, hdr->tp_h.tp_snaplen,
293 ctx->link_type, ctx->print_mode);
295 kernel_may_pull_from_tx(&hdr->tp_h);
297 it++;
298 if (it >= tx_ring.layout.tp_frame_nr)
299 it = 0;
301 if (unlikely(sigint == 1))
302 break;
304 if (frame_count_max != 0) {
305 if (ctx->tx_packets >= frame_count_max) {
306 sigint = 1;
307 break;
313 out:
314 bug_on(gettimeofday(&end, NULL));
315 timersub(&end, &start, &diff);
317 timer_purge();
319 bpf_release(&bpf_ops);
321 dissector_cleanup_all();
322 destroy_tx_ring(tx_sock, &tx_ring);
324 if (ctx->rfraw)
325 leave_rfmon_mac80211(ctx->device_out);
327 if (__pcap_io->prepare_close_pcap)
328 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
330 if (!strncmp("-", ctx->device_in, strlen("-")))
331 dup2(fd, fileno(stdin));
332 close(fd);
334 close(tx_sock);
336 fflush(stdout);
337 printf("\n");
338 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
339 printf("\r%12lu packets truncated in file\n", trunced);
340 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
341 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
344 static void receive_to_xmit(struct ctx *ctx)
346 short ifflags = 0;
347 uint8_t *in, *out;
348 int rx_sock, ifindex_in, ifindex_out, ret;
349 size_t size_in, size_out;
350 unsigned int it_in = 0, it_out = 0;
351 unsigned long frame_count = 0;
352 struct frame_map *hdr_in, *hdr_out;
353 struct ring tx_ring, rx_ring;
354 struct pollfd rx_poll;
355 struct sock_fprog bpf_ops;
357 if (!strncmp(ctx->device_in, ctx->device_out, IFNAMSIZ))
358 panic("Ingress/egress devices must be different!\n");
359 if (!device_up_and_running(ctx->device_out))
360 panic("Egress device not up and running!\n");
362 rx_sock = pf_socket();
363 tx_sock = pf_socket();
365 fmemset(&tx_ring, 0, sizeof(tx_ring));
366 fmemset(&rx_ring, 0, sizeof(rx_ring));
367 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
369 ifindex_in = device_ifindex(ctx->device_in);
370 ifindex_out = device_ifindex(ctx->device_out);
372 size_in = ring_size(ctx->device_in, ctx->reserve_size);
373 size_out = ring_size(ctx->device_out, ctx->reserve_size);
375 enable_kernel_bpf_jit_compiler();
377 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
378 if (ctx->dump_bpf)
379 bpf_dump_all(&bpf_ops);
380 bpf_attach_to_sock(rx_sock, &bpf_ops);
382 setup_rx_ring_layout(rx_sock, &rx_ring, size_in, ctx->jumbo, false);
383 create_rx_ring(rx_sock, &rx_ring, ctx->verbose);
384 mmap_rx_ring(rx_sock, &rx_ring);
385 alloc_rx_ring_frames(rx_sock, &rx_ring);
386 bind_rx_ring(rx_sock, &rx_ring, ifindex_in);
387 prepare_polling(rx_sock, &rx_poll);
389 set_packet_loss_discard(tx_sock);
390 setup_tx_ring_layout(tx_sock, &tx_ring, size_out, ctx->jumbo);
391 create_tx_ring(tx_sock, &tx_ring, ctx->verbose);
392 mmap_tx_ring(tx_sock, &tx_ring);
393 alloc_tx_ring_frames(tx_sock, &tx_ring);
394 bind_tx_ring(tx_sock, &tx_ring, ifindex_out);
396 dissector_init_all(ctx->print_mode);
398 if (ctx->promiscuous)
399 ifflags = device_enter_promiscuous_mode(ctx->device_in);
401 if (ctx->kpull)
402 interval = ctx->kpull;
404 set_itimer_interval_value(&itimer, 0, interval);
405 setitimer(ITIMER_REAL, &itimer, NULL);
407 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
409 printf("Running! Hang up with ^C!\n\n");
410 fflush(stdout);
412 while (likely(sigint == 0)) {
413 while (user_may_pull_from_rx(rx_ring.frames[it_in].iov_base)) {
414 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 while (!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:
465 kernel_may_pull_from_rx(&hdr_in->tp_h);
467 it_in++;
468 if (it_in >= rx_ring.layout.tp_frame_nr)
469 it_in = 0;
471 if (unlikely(sigint == 1))
472 goto out;
475 ret = poll(&rx_poll, 1, -1);
476 if (unlikely(ret < 0)) {
477 if (errno != EINTR)
478 panic("Poll failed!\n");
482 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 uint8_t *out;
537 int ret, fd, fdo = 0;
538 unsigned long trunced = 0;
539 size_t out_len;
540 pcap_pkthdr_t phdr;
541 struct sock_fprog bpf_ops;
542 struct frame_map fm;
543 struct timeval start, end, diff;
545 bug_on(!__pcap_io);
547 if (!strncmp("-", ctx->device_in, strlen("-"))) {
548 fd = dup_or_die(fileno(stdin));
549 close(fileno(stdin));
550 if (ctx->pcap == PCAP_OPS_MM)
551 ctx->pcap = PCAP_OPS_SG;
552 } else {
553 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
556 if (__pcap_io->init_once_pcap)
557 __pcap_io->init_once_pcap();
559 ret = __pcap_io->pull_fhdr_pcap(fd, &ctx->magic, &ctx->link_type);
560 if (ret)
561 panic("Error reading pcap header!\n");
563 if (__pcap_io->prepare_access_pcap) {
564 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_RD, ctx->jumbo);
565 if (ret)
566 panic("Error prepare reading pcap!\n");
569 fmemset(&fm, 0, sizeof(fm));
570 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
572 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
573 if (ctx->dump_bpf)
574 bpf_dump_all(&bpf_ops);
576 dissector_init_all(ctx->print_mode);
578 out_len = round_up(1024 * 1024, RUNTIME_PAGE_SIZE);
579 out = xmalloc_aligned(out_len, CO_CACHE_LINE_SIZE);
581 if (ctx->device_out) {
582 if (!strncmp("-", ctx->device_out, strlen("-"))) {
583 fdo = dup_or_die(fileno(stdout));
584 close(fileno(stdout));
585 } else {
586 fdo = open_or_die_m(ctx->device_out, O_RDWR | O_CREAT |
587 O_TRUNC | O_LARGEFILE, DEFFILEMODE);
591 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
593 printf("Running! Hang up with ^C!\n\n");
594 fflush(stdout);
596 bug_on(gettimeofday(&start, NULL));
598 while (likely(sigint == 0)) {
599 do {
600 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic,
601 out, out_len);
602 if (unlikely(ret < 0))
603 goto out;
605 if (unlikely(pcap_get_length(&phdr, ctx->magic) == 0)) {
606 trunced++;
607 continue;
610 if (unlikely(pcap_get_length(&phdr, ctx->magic) > out_len)) {
611 pcap_set_length(&phdr, ctx->magic, out_len);
612 trunced++;
614 } while (ctx->filter &&
615 !bpf_run_filter(&bpf_ops, out,
616 pcap_get_length(&phdr, ctx->magic)));
618 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &fm.tp_h, &fm.s_ll);
620 ctx->tx_bytes += fm.tp_h.tp_len;
621 ctx->tx_packets++;
623 show_frame_hdr(&fm, ctx->print_mode);
625 dissector_entry_point(out, fm.tp_h.tp_snaplen,
626 ctx->link_type, ctx->print_mode);
628 if (ctx->device_out)
629 translate_pcap_to_txf(fdo, out, fm.tp_h.tp_snaplen);
631 if (frame_count_max != 0) {
632 if (ctx->tx_packets >= frame_count_max) {
633 sigint = 1;
634 break;
639 out:
641 bug_on(gettimeofday(&end, NULL));
642 timersub(&end, &start, &diff);
644 bpf_release(&bpf_ops);
646 dissector_cleanup_all();
648 if (__pcap_io->prepare_close_pcap)
649 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
651 xfree(out);
653 fflush(stdout);
654 printf("\n");
655 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
656 printf("\r%12lu packets truncated in file\n", trunced);
657 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
658 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
660 if (!strncmp("-", ctx->device_in, strlen("-")))
661 dup2(fd, fileno(stdin));
662 close(fd);
664 if (ctx->device_out) {
665 if (!strncmp("-", ctx->device_out, strlen("-")))
666 dup2(fdo, fileno(stdout));
667 close(fdo);
671 static void finish_multi_pcap_file(struct ctx *ctx, int fd)
673 __pcap_io->fsync_pcap(fd);
675 if (__pcap_io->prepare_close_pcap)
676 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
678 close(fd);
680 fmemset(&itimer, 0, sizeof(itimer));
681 setitimer(ITIMER_REAL, &itimer, NULL);
684 static int next_multi_pcap_file(struct ctx *ctx, int fd)
686 int ret;
687 char fname[512];
689 __pcap_io->fsync_pcap(fd);
691 if (__pcap_io->prepare_close_pcap)
692 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
694 close(fd);
696 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
697 ctx->prefix ? : "dump-", time(NULL));
699 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
700 O_LARGEFILE, DEFFILEMODE);
702 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
703 if (ret)
704 panic("Error writing pcap header!\n");
706 if (__pcap_io->prepare_access_pcap) {
707 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, true);
708 if (ret)
709 panic("Error prepare writing pcap!\n");
712 return fd;
715 static int begin_multi_pcap_file(struct ctx *ctx)
717 int fd, ret;
718 char fname[256];
720 bug_on(!__pcap_io);
722 if (ctx->device_out[strlen(ctx->device_out) - 1] == '/')
723 ctx->device_out[strlen(ctx->device_out) - 1] = 0;
725 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
726 ctx->prefix ? : "dump-", time(NULL));
728 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
729 O_LARGEFILE, DEFFILEMODE);
731 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
732 if (ret)
733 panic("Error writing pcap header!\n");
735 if (__pcap_io->prepare_access_pcap) {
736 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, true);
737 if (ret)
738 panic("Error prepare writing pcap!\n");
741 if (ctx->dump_mode == DUMP_INTERVAL_TIME) {
742 interval = ctx->dump_interval;
744 set_itimer_interval_value(&itimer, interval, 0);
745 setitimer(ITIMER_REAL, &itimer, NULL);
746 } else {
747 interval = 0;
750 return fd;
753 static void finish_single_pcap_file(struct ctx *ctx, int fd)
755 __pcap_io->fsync_pcap(fd);
757 if (__pcap_io->prepare_close_pcap)
758 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
760 if (strncmp("-", ctx->device_out, strlen("-")))
761 close(fd);
762 else
763 dup2(fd, fileno(stdout));
766 static int begin_single_pcap_file(struct ctx *ctx)
768 int fd, ret;
770 bug_on(!__pcap_io);
772 if (!strncmp("-", ctx->device_out, strlen("-"))) {
773 fd = dup_or_die(fileno(stdout));
774 close(fileno(stdout));
775 if (ctx->pcap == PCAP_OPS_MM)
776 ctx->pcap = PCAP_OPS_SG;
777 } else {
778 fd = open_or_die_m(ctx->device_out,
779 O_RDWR | O_CREAT | O_TRUNC |
780 O_LARGEFILE, DEFFILEMODE);
783 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
784 if (ret)
785 panic("Error writing pcap header!\n");
787 if (__pcap_io->prepare_access_pcap) {
788 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, true);
789 if (ret)
790 panic("Error prepare writing pcap!\n");
793 return fd;
796 static void print_pcap_file_stats(int sock, struct ctx *ctx)
798 int ret;
799 struct tpacket_stats kstats;
800 socklen_t slen = sizeof(kstats);
802 fmemset(&kstats, 0, sizeof(kstats));
804 ret = getsockopt(sock, SOL_PACKET, PACKET_STATISTICS, &kstats, &slen);
805 if (unlikely(ret))
806 panic("Cannot get packet statistics!\n");
808 if (ctx->print_mode == PRINT_NONE) {
809 printf(".(+%u/-%u)", kstats.tp_packets - kstats.tp_drops,
810 kstats.tp_drops);
811 fflush(stdout);
815 static void walk_t3_block(struct block_desc *pbd, struct ctx *ctx,
816 int sock, int *fd, unsigned long *frame_count)
818 uint8_t *packet;
819 int num_pkts = pbd->h1.num_pkts, i, ret;
820 struct tpacket3_hdr *hdr;
821 pcap_pkthdr_t phdr;
822 struct sockaddr_ll *sll;
824 hdr = (void *) ((uint8_t *) pbd + pbd->h1.offset_to_first_pkt);
825 sll = (void *) ((uint8_t *) hdr + TPACKET_ALIGN(sizeof(*hdr)));
827 for (i = 0; i < num_pkts && likely(sigint == 0); ++i) {
828 packet = ((uint8_t *) hdr + hdr->tp_mac);
830 if (ctx->packet_type != -1)
831 if (ctx->packet_type != sll->sll_pkttype)
832 goto next;
834 (*frame_count)++;
836 if (dump_to_pcap(ctx)) {
837 tpacket3_hdr_to_pcap_pkthdr(hdr, sll, &phdr, ctx->magic);
839 ret = __pcap_io->write_pcap(*fd, &phdr, ctx->magic, packet,
840 pcap_get_length(&phdr, ctx->magic));
841 if (unlikely(ret != (int) pcap_get_total_length(&phdr, ctx->magic)))
842 panic("Write error to pcap!\n");
845 __show_frame_hdr(sll, hdr, ctx->print_mode, true);
847 dissector_entry_point(packet, hdr->tp_snaplen, ctx->link_type,
848 ctx->print_mode);
849 next:
850 hdr = (void *) ((uint8_t *) hdr + hdr->tp_next_offset);
851 sll = (void *) ((uint8_t *) hdr + TPACKET_ALIGN(sizeof(*hdr)));
853 if (frame_count_max != 0) {
854 if (unlikely(*frame_count >= frame_count_max)) {
855 sigint = 1;
856 break;
860 if (dump_to_pcap(ctx)) {
861 if (ctx->dump_mode == DUMP_INTERVAL_SIZE) {
862 interval += hdr->tp_snaplen;
863 if (interval > ctx->dump_interval) {
864 next_dump = true;
865 interval = 0;
869 if (next_dump) {
870 *fd = next_multi_pcap_file(ctx, *fd);
871 next_dump = false;
873 if (unlikely(ctx->verbose))
874 print_pcap_file_stats(sock, ctx);
880 static void recv_only_or_dump(struct ctx *ctx)
882 short ifflags = 0;
883 int sock, irq, ifindex, fd = 0, ret;
884 size_t size;
885 unsigned int it = 0;
886 struct ring rx_ring;
887 struct pollfd rx_poll;
888 struct sock_fprog bpf_ops;
889 struct timeval start, end, diff;
890 struct block_desc *pbd;
891 unsigned long frame_count = 0;
893 sock = pf_socket();
895 if (ctx->rfraw) {
896 ctx->device_trans = xstrdup(ctx->device_in);
897 xfree(ctx->device_in);
899 enter_rfmon_mac80211(ctx->device_trans, &ctx->device_in);
900 ctx->link_type = LINKTYPE_IEEE802_11;
903 fmemset(&rx_ring, 0, sizeof(rx_ring));
904 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
906 ifindex = device_ifindex(ctx->device_in);
908 size = ring_size(ctx->device_in, ctx->reserve_size);
910 enable_kernel_bpf_jit_compiler();
912 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
913 if (ctx->dump_bpf)
914 bpf_dump_all(&bpf_ops);
915 bpf_attach_to_sock(sock, &bpf_ops);
917 ret = set_sockopt_hwtimestamp(sock, ctx->device_in);
918 if (ret == 0 && ctx->verbose)
919 printf("HW timestamping enabled\n");
921 setup_rx_ring_layout(sock, &rx_ring, size, true, true);
922 create_rx_ring(sock, &rx_ring, ctx->verbose);
923 mmap_rx_ring(sock, &rx_ring);
924 alloc_rx_ring_frames(sock, &rx_ring);
925 bind_rx_ring(sock, &rx_ring, ifindex);
927 prepare_polling(sock, &rx_poll);
928 dissector_init_all(ctx->print_mode);
930 if (ctx->cpu >= 0 && ifindex > 0) {
931 irq = device_irq_number(ctx->device_in);
932 device_set_irq_affinity(irq, ctx->cpu);
934 if (ctx->verbose)
935 printf("IRQ: %s:%d > CPU%d\n",
936 ctx->device_in, irq, ctx->cpu);
939 if (ctx->promiscuous)
940 ifflags = device_enter_promiscuous_mode(ctx->device_in);
942 if (dump_to_pcap(ctx) && __pcap_io->init_once_pcap)
943 __pcap_io->init_once_pcap();
945 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
947 if (dump_to_pcap(ctx)) {
948 struct stat stats;
950 ret = stat(ctx->device_out, &stats);
951 if (ret < 0)
952 ctx->dump_dir = 0;
953 else
954 ctx->dump_dir = S_ISDIR(stats.st_mode);
956 if (ctx->dump_dir)
957 fd = begin_multi_pcap_file(ctx);
958 else
959 fd = begin_single_pcap_file(ctx);
962 printf("Running! Hang up with ^C!\n\n");
963 fflush(stdout);
965 bug_on(gettimeofday(&start, NULL));
967 while (likely(sigint == 0)) {
968 while (user_may_pull_from_rx_block((pbd = (void *)
969 rx_ring.frames[it].iov_base))) {
970 walk_t3_block(pbd, ctx, sock, &fd, &frame_count);
972 kernel_may_pull_from_rx_block(pbd);
973 it = (it + 1) % rx_ring.layout3.tp_block_nr;
975 if (unlikely(sigint == 1))
976 break;
979 ret = poll(&rx_poll, 1, -1);
980 if (unlikely(ret < 0)) {
981 if (errno != EINTR)
982 panic("Poll failed!\n");
986 bug_on(gettimeofday(&end, NULL));
987 timersub(&end, &start, &diff);
989 if (!(ctx->dump_dir && ctx->print_mode == PRINT_NONE)) {
990 sock_rx_net_stats(sock, frame_count);
992 printf("\r%12lu sec, %lu usec in total\n",
993 diff.tv_sec, diff.tv_usec);
994 } else {
995 printf("\n\n");
996 fflush(stdout);
999 bpf_release(&bpf_ops);
1000 dissector_cleanup_all();
1001 destroy_rx_ring(sock, &rx_ring);
1003 if (ctx->promiscuous)
1004 device_leave_promiscuous_mode(ctx->device_in, ifflags);
1006 if (ctx->rfraw)
1007 leave_rfmon_mac80211(ctx->device_in);
1009 if (dump_to_pcap(ctx)) {
1010 if (ctx->dump_dir)
1011 finish_multi_pcap_file(ctx, fd);
1012 else
1013 finish_single_pcap_file(ctx, fd);
1016 close(sock);
1019 static void init_ctx(struct ctx *ctx)
1021 memset(ctx, 0, sizeof(*ctx));
1022 ctx->uid = getuid();
1023 ctx->uid = getgid();
1025 ctx->cpu = -1;
1026 ctx->packet_type = -1;
1028 ctx->magic = ORIGINAL_TCPDUMP_MAGIC;
1029 ctx->print_mode = PRINT_NORM;
1030 ctx->pcap = PCAP_OPS_SG;
1032 ctx->dump_mode = DUMP_INTERVAL_TIME;
1033 ctx->dump_interval = 60;
1035 ctx->promiscuous = true;
1036 ctx->randomize = false;
1039 static void destroy_ctx(struct ctx *ctx)
1041 free(ctx->device_in);
1042 free(ctx->device_out);
1043 free(ctx->device_trans);
1045 free(ctx->prefix);
1048 static void __noreturn help(void)
1050 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING);
1051 puts("http://www.netsniff-ng.org\n\n"
1052 "Usage: netsniff-ng [options] [filter-expression]\n"
1053 "Options:\n"
1054 " -i|-d|--dev|--in <dev|pcap|-> Input source as netdev, pcap or pcap stdin\n"
1055 " -o|--out <dev|pcap|dir|cfg|-> Output sink as netdev, pcap, directory, trafgen, or stdout\n"
1056 " -f|--filter <bpf-file|expr> Use BPF filter file from bpfc or tcpdump-like expression\n"
1057 " -t|--type <type> Filter for: host|broadcast|multicast|others|outgoing\n"
1058 " -F|--interval <size|time> Dump interval if -o is a dir: <num>KiB/MiB/GiB/s/sec/min/hrs\n"
1059 " -R|--rfraw Capture or inject raw 802.11 frames\n"
1060 " -n|--num <0|uint> Number of packets until exit (def: 0)\n"
1061 " -P|--prefix <name> Prefix for pcaps stored in directory\n"
1062 " -T|--magic <pcap-magic> Pcap magic number/pcap format to store, see -D\n"
1063 " -D|--dump-pcap-types Dump pcap types and magic numbers and quit\n"
1064 " -B|--dump-bpf Dump generated BPF assembly\n"
1065 " -r|--rand Randomize packet forwarding order (dev->dev)\n"
1066 " -M|--no-promisc No promiscuous mode for netdev\n"
1067 " -A|--no-sock-mem Don't tune core socket memory\n"
1068 " -m|--mmap Mmap(2) pcap file I/O, e.g. for replaying pcaps\n"
1069 " -G|--sg Scatter/gather pcap file I/O\n"
1070 " -c|--clrw Use slower read(2)/write(2) I/O\n"
1071 " -S|--ring-size <size> Specify ring size to: <num>KiB/MiB/GiB\n"
1072 " -k|--kernel-pull <uint> Kernel pull from user interval in us (def: 10us)\n"
1073 " -J|--jumbo-support Support replay/fwd 64KB Super Jumbo Frames (def: 2048B)\n"
1074 " -b|--bind-cpu <cpu> Bind to specific CPU\n"
1075 " -u|--user <userid> Drop privileges and change to userid\n"
1076 " -g|--group <groupid> Drop privileges and change to groupid\n"
1077 " -H|--prio-high Make this high priority process\n"
1078 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
1079 " -s|--silent Do not print captured packets\n"
1080 " -q|--less Print less-verbose packet information\n"
1081 " -X|--hex Print packet data in hex format\n"
1082 " -l|--ascii Print human-readable packet data\n"
1083 " -U|--update Update GeoIP databases\n"
1084 " -V|--verbose Be more verbose\n"
1085 " -v|--version Show version and exit\n"
1086 " -h|--help Guess what?!\n\n"
1087 "Examples:\n"
1088 " netsniff-ng --in eth0 --out dump.pcap -s -T 0xa1b2c3d4 --b 0 tcp or udp\n"
1089 " netsniff-ng --in wlan0 --rfraw --out dump.pcap --silent --bind-cpu 0\n"
1090 " netsniff-ng --in dump.pcap --mmap --out eth0 -k1000 --silent --bind-cpu 0\n"
1091 " netsniff-ng --in dump.pcap --out dump.cfg --silent --bind-cpu 0\n"
1092 " netsniff-ng --in eth0 --out eth1 --silent --bind-cpu 0 -J --type host\n"
1093 " netsniff-ng --in eth1 --out /opt/probe/ -s -m --interval 100MiB -b 0\n"
1094 " netsniff-ng --in vlan0 --out dump.pcap -c -u `id -u bob` -g `id -g bob`\n"
1095 " netsniff-ng --in any --filter http.bpf --jumbo-support --ascii -V\n\n"
1096 "Note:\n"
1097 " For introducing bit errors, delays with random variation and more\n"
1098 " while replaying pcaps, make use of tc(8) with its disciplines (e.g. netem).\n\n"
1099 "Please report bugs to <bugs@netsniff-ng.org>\n"
1100 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1101 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1102 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1103 "Swiss federal institute of technology (ETH Zurich)\n"
1104 "License: GNU GPL version 2.0\n"
1105 "This is free software: you are free to change and redistribute it.\n"
1106 "There is NO WARRANTY, to the extent permitted by law.\n");
1107 die();
1110 static void __noreturn version(void)
1112 printf("\nnetsniff-ng %s, Git id: %s\n", VERSION_LONG, GITVERSION);
1113 puts("the packet sniffing beast\n"
1114 "http://www.netsniff-ng.org\n\n"
1115 "Please report bugs to <bugs@netsniff-ng.org>\n"
1116 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1117 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1118 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1119 "Swiss federal institute of technology (ETH Zurich)\n"
1120 "License: GNU GPL version 2.0\n"
1121 "This is free software: you are free to change and redistribute it.\n"
1122 "There is NO WARRANTY, to the extent permitted by law.\n");
1123 die();
1126 int main(int argc, char **argv)
1128 char *ptr;
1129 int c, i, j, cpu_tmp, opt_index, ops_touched = 0, vals[4] = {0};
1130 bool prio_high = false, setsockmem = true;
1131 void (*main_loop)(struct ctx *ctx) = NULL;
1132 struct ctx ctx;
1134 init_ctx(&ctx);
1135 srand(time(NULL));
1137 while ((c = getopt_long(argc, argv, short_options, long_options,
1138 &opt_index)) != EOF) {
1139 switch (c) {
1140 case 'd':
1141 case 'i':
1142 ctx.device_in = xstrdup(optarg);
1143 break;
1144 case 'o':
1145 ctx.device_out = xstrdup(optarg);
1146 break;
1147 case 'P':
1148 ctx.prefix = xstrdup(optarg);
1149 break;
1150 case 'R':
1151 ctx.link_type = LINKTYPE_IEEE802_11;
1152 ctx.rfraw = 1;
1153 break;
1154 case 'r':
1155 ctx.randomize = true;
1156 break;
1157 case 'J':
1158 ctx.jumbo = true;
1159 break;
1160 case 'T':
1161 ctx.magic = (uint32_t) strtoul(optarg, NULL, 0);
1162 pcap_check_magic(ctx.magic);
1163 break;
1164 case 'f':
1165 ctx.filter = xstrdup(optarg);
1166 break;
1167 case 'M':
1168 ctx.promiscuous = false;
1169 break;
1170 case 'A':
1171 setsockmem = false;
1172 break;
1173 case 'u':
1174 ctx.uid = strtoul(optarg, NULL, 0);
1175 ctx.enforce = true;
1176 break;
1177 case 'g':
1178 ctx.gid = strtoul(optarg, NULL, 0);
1179 ctx.enforce = true;
1180 break;
1181 case 't':
1182 if (!strncmp(optarg, "host", strlen("host")))
1183 ctx.packet_type = PACKET_HOST;
1184 else if (!strncmp(optarg, "broadcast", strlen("broadcast")))
1185 ctx.packet_type = PACKET_BROADCAST;
1186 else if (!strncmp(optarg, "multicast", strlen("multicast")))
1187 ctx.packet_type = PACKET_MULTICAST;
1188 else if (!strncmp(optarg, "others", strlen("others")))
1189 ctx.packet_type = PACKET_OTHERHOST;
1190 else if (!strncmp(optarg, "outgoing", strlen("outgoing")))
1191 ctx.packet_type = PACKET_OUTGOING;
1192 else
1193 ctx.packet_type = -1;
1194 break;
1195 case 'S':
1196 ptr = optarg;
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 for (j = i = strlen(optarg); i > 0; --i) {
1264 if (!isdigit(optarg[j - i]))
1265 break;
1266 ptr++;
1269 if (!strncmp(ptr, "KiB", strlen("KiB"))) {
1270 ctx.dump_interval = 1 << 10;
1271 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1272 } else if (!strncmp(ptr, "MiB", strlen("MiB"))) {
1273 ctx.dump_interval = 1 << 20;
1274 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1275 } else if (!strncmp(ptr, "GiB", strlen("GiB"))) {
1276 ctx.dump_interval = 1 << 30;
1277 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1278 } else if (!strncmp(ptr, "sec", strlen("sec"))) {
1279 ctx.dump_interval = 1;
1280 ctx.dump_mode = DUMP_INTERVAL_TIME;
1281 } else if (!strncmp(ptr, "min", strlen("min"))) {
1282 ctx.dump_interval = 60;
1283 ctx.dump_mode = DUMP_INTERVAL_TIME;
1284 } else if (!strncmp(ptr, "hrs", strlen("hrs"))) {
1285 ctx.dump_interval = 60 * 60;
1286 ctx.dump_mode = DUMP_INTERVAL_TIME;
1287 } else if (!strncmp(ptr, "s", strlen("s"))) {
1288 ctx.dump_interval = 1;
1289 ctx.dump_mode = DUMP_INTERVAL_TIME;
1290 } else {
1291 panic("Syntax error in time/size param!\n");
1294 ctx.dump_interval *= strtoul(optarg, NULL, 0);
1295 break;
1296 case 'V':
1297 ctx.verbose = true;
1298 break;
1299 case 'B':
1300 ctx.dump_bpf = true;
1301 break;
1302 case 'D':
1303 pcap_dump_type_features();
1304 die();
1305 break;
1306 case 'U':
1307 update_geoip();
1308 die();
1309 break;
1310 case 'v':
1311 version();
1312 break;
1313 case 'h':
1314 help();
1315 break;
1316 case '?':
1317 switch (optopt) {
1318 case 'd':
1319 case 'i':
1320 case 'o':
1321 case 'f':
1322 case 't':
1323 case 'P':
1324 case 'F':
1325 case 'n':
1326 case 'S':
1327 case 'b':
1328 case 'k':
1329 case 'T':
1330 case 'u':
1331 case 'g':
1332 case 'e':
1333 panic("Option -%c requires an argument!\n",
1334 optopt);
1335 default:
1336 if (isprint(optopt))
1337 printf("Unknown option character `0x%X\'!\n", optopt);
1338 die();
1340 default:
1341 break;
1345 if (!ctx.filter && optind != argc) {
1346 int ret;
1347 off_t offset = 0;
1349 for (i = optind; i < argc; ++i) {
1350 size_t alen = strlen(argv[i]) + 2;
1351 size_t flen = ctx.filter ? strlen(ctx.filter) : 0;
1353 ctx.filter = xrealloc(ctx.filter, 1, flen + alen);
1354 ret = slprintf(ctx.filter + offset, strlen(argv[i]) + 2, "%s ", argv[i]);
1355 if (ret < 0)
1356 panic("Cannot concatenate filter string!\n");
1357 else
1358 offset += ret;
1362 if (!ctx.device_in)
1363 ctx.device_in = xstrdup("any");
1365 register_signal(SIGINT, signal_handler);
1366 register_signal(SIGQUIT, signal_handler);
1367 register_signal(SIGTERM, signal_handler);
1368 register_signal(SIGHUP, signal_handler);
1370 tprintf_init();
1372 if (prio_high) {
1373 set_proc_prio(-20);
1374 set_sched_status(SCHED_FIFO, sched_get_priority_max(SCHED_FIFO));
1377 if (ctx.device_in && (device_mtu(ctx.device_in) ||
1378 !strncmp("any", ctx.device_in, strlen(ctx.device_in)))) {
1379 if (!ctx.rfraw)
1380 ctx.link_type = pcap_devtype_to_linktype(ctx.device_in);
1381 if (!ctx.device_out) {
1382 ctx.dump = 0;
1383 main_loop = recv_only_or_dump;
1384 } else if (device_mtu(ctx.device_out)) {
1385 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1386 main_loop = receive_to_xmit;
1387 } else {
1388 ctx.dump = 1;
1389 register_signal_f(SIGALRM, timer_next_dump, SA_SIGINFO);
1390 main_loop = recv_only_or_dump;
1391 if (!ops_touched)
1392 ctx.pcap = PCAP_OPS_SG;
1394 } else {
1395 if (ctx.device_out && device_mtu(ctx.device_out)) {
1396 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1397 main_loop = pcap_to_xmit;
1398 if (!ops_touched)
1399 ctx.pcap = PCAP_OPS_MM;
1400 } else {
1401 main_loop = read_pcap;
1402 if (!ops_touched)
1403 ctx.pcap = PCAP_OPS_SG;
1407 bug_on(!main_loop);
1409 init_geoip(0);
1410 if (setsockmem)
1411 set_system_socket_memory(vals, array_size(vals));
1412 if (!ctx.enforce)
1413 xlockme();
1415 if (ctx.verbose)
1416 printf("pcap file I/O method: %s\n", pcap_ops_group_to_str[ctx.pcap]);
1418 main_loop(&ctx);
1420 if (!ctx.enforce)
1421 xunlockme();
1422 if (setsockmem)
1423 reset_system_socket_memory(vals, array_size(vals));
1424 destroy_geoip();
1426 device_restore_irq_affinity_list();
1427 tprintf_cleanup();
1429 destroy_ctx(&ctx);
1430 return 0;