man: improvements to mausezahn.8: CoS (PCP), DEI (CFI)
[netsniff-ng.git] / netsniff-ng.c
blob2738b9a47f85630225aac31d5dc09b9a9cff4434
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 "promisc.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 "sock.h"
41 #include "geoip.h"
42 #include "lockme.h"
43 #include "tprintf.h"
44 #include "timer.h"
45 #include "tstamping.h"
46 #include "dissector.h"
47 #include "xmalloc.h"
49 enum dump_mode {
50 DUMP_INTERVAL_TIME,
51 DUMP_INTERVAL_SIZE,
54 struct ctx {
55 char *device_in, *device_out, *device_trans, *filter, *prefix;
56 int cpu, rfraw, dump, print_mode, dump_dir, packet_type, verbose;
57 unsigned long kpull, dump_interval, reserve_size, tx_bytes, tx_packets;
58 bool randomize, promiscuous, enforce, jumbo, dump_bpf;
59 enum pcap_ops_groups pcap; enum dump_mode dump_mode;
60 uid_t uid; gid_t gid; uint32_t link_type, magic;
63 static volatile sig_atomic_t sigint = 0;
64 static volatile bool next_dump = false;
66 static const char *short_options = "d:i:o:rf:MJt:S:k:n:b:HQmcsqXlvhF:RGAP:Vu:g:T:DB";
67 static const struct option long_options[] = {
68 {"dev", required_argument, NULL, 'd'},
69 {"in", required_argument, NULL, 'i'},
70 {"out", required_argument, NULL, 'o'},
71 {"filter", required_argument, NULL, 'f'},
72 {"num", required_argument, NULL, 'n'},
73 {"type", required_argument, NULL, 't'},
74 {"interval", required_argument, NULL, 'F'},
75 {"ring-size", required_argument, NULL, 'S'},
76 {"kernel-pull", required_argument, NULL, 'k'},
77 {"bind-cpu", required_argument, NULL, 'b'},
78 {"prefix", required_argument, NULL, 'P'},
79 {"user", required_argument, NULL, 'u'},
80 {"group", required_argument, NULL, 'g'},
81 {"magic", required_argument, NULL, 'T'},
82 {"rand", no_argument, NULL, 'r'},
83 {"rfraw", no_argument, NULL, 'R'},
84 {"mmap", no_argument, NULL, 'm'},
85 {"sg", no_argument, NULL, 'G'},
86 {"clrw", no_argument, NULL, 'c'},
87 {"jumbo-support", no_argument, NULL, 'J'},
88 {"no-promisc", no_argument, NULL, 'M'},
89 {"prio-high", no_argument, NULL, 'H'},
90 {"notouch-irq", no_argument, NULL, 'Q'},
91 {"dump-pcap-types", no_argument, NULL, 'D'},
92 {"dump-bpf", no_argument, NULL, 'B'},
93 {"silent", no_argument, NULL, 's'},
94 {"less", no_argument, NULL, 'q'},
95 {"hex", no_argument, NULL, 'X'},
96 {"ascii", no_argument, NULL, 'l'},
97 {"no-sock-mem", no_argument, NULL, 'A'},
98 {"update", no_argument, NULL, 'U'},
99 {"verbose", no_argument, NULL, 'V'},
100 {"version", no_argument, NULL, 'v'},
101 {"help", no_argument, NULL, 'h'},
102 {NULL, 0, NULL, 0}
105 static int tx_sock;
106 static struct itimerval itimer;
107 static unsigned long frame_count_max = 0, interval = TX_KERNEL_PULL_INT;
109 #define __pcap_io pcap_ops[ctx->pcap]
111 static void signal_handler(int number)
113 switch (number) {
114 case SIGINT:
115 sigint = 1;
116 case SIGHUP:
117 default:
118 break;
122 static void timer_elapsed(int unused __maybe_unused)
124 int ret;
126 set_itimer_interval_value(&itimer, 0, interval);
128 ret = pull_and_flush_tx_ring(tx_sock);
129 if (unlikely(ret < 0)) {
130 /* We could hit EBADF if the socket has been closed before
131 * the timer was triggered.
133 if (errno != EBADF && errno != ENOBUFS)
134 panic("Flushing TX_RING failed: %s!\n", strerror(errno));
137 setitimer(ITIMER_REAL, &itimer, NULL);
140 static void timer_purge(void)
142 int ret;
144 ret = pull_and_flush_tx_ring_wait(tx_sock);
145 if (unlikely(ret < 0)) {
146 if (errno != EBADF && errno != ENOBUFS)
147 panic("Flushing TX_RING failed: %s!\n", strerror(errno));
150 set_itimer_interval_value(&itimer, 0, 0);
151 setitimer(ITIMER_REAL, &itimer, NULL);
154 static void timer_next_dump(int unused __maybe_unused)
156 set_itimer_interval_value(&itimer, interval, 0);
157 next_dump = true;
158 setitimer(ITIMER_REAL, &itimer, NULL);
161 static inline bool dump_to_pcap(struct ctx *ctx)
163 return ctx->dump;
166 static void pcap_to_xmit(struct ctx *ctx)
168 __label__ out;
169 uint8_t *out = NULL;
170 int irq, ifindex, fd = 0, ret;
171 unsigned int size, it = 0;
172 unsigned long trunced = 0;
173 struct ring tx_ring;
174 struct frame_map *hdr;
175 struct sock_fprog bpf_ops;
176 struct timeval start, end, diff;
177 pcap_pkthdr_t phdr;
179 if (!device_up_and_running(ctx->device_out) && !ctx->rfraw)
180 panic("Device not up and running!\n");
182 bug_on(!__pcap_io);
184 tx_sock = pf_socket();
186 if (!strncmp("-", ctx->device_in, strlen("-"))) {
187 fd = dup_or_die(fileno(stdin));
188 close(fileno(stdin));
189 if (ctx->pcap == PCAP_OPS_MM)
190 ctx->pcap = PCAP_OPS_SG;
191 } else {
192 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
195 if (__pcap_io->init_once_pcap)
196 __pcap_io->init_once_pcap();
198 ret = __pcap_io->pull_fhdr_pcap(fd, &ctx->magic, &ctx->link_type);
199 if (ret)
200 panic("Error reading pcap header!\n");
202 if (__pcap_io->prepare_access_pcap) {
203 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_RD, ctx->jumbo);
204 if (ret)
205 panic("Error prepare reading pcap!\n");
208 fmemset(&tx_ring, 0, sizeof(tx_ring));
209 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
211 if (ctx->rfraw) {
212 ctx->device_trans = xstrdup(ctx->device_out);
213 xfree(ctx->device_out);
215 enter_rfmon_mac80211(ctx->device_trans, &ctx->device_out);
216 if (ctx->link_type != LINKTYPE_IEEE802_11)
217 panic("Wrong linktype of pcap!\n");
220 ifindex = device_ifindex(ctx->device_out);
222 size = ring_size(ctx->device_out, ctx->reserve_size);
224 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
225 if (ctx->dump_bpf)
226 bpf_dump_all(&bpf_ops);
228 set_packet_loss_discard(tx_sock);
230 setup_tx_ring_layout(tx_sock, &tx_ring, size, ctx->jumbo);
231 create_tx_ring(tx_sock, &tx_ring, ctx->verbose);
232 mmap_tx_ring(tx_sock, &tx_ring);
233 alloc_tx_ring_frames(tx_sock, &tx_ring);
234 bind_tx_ring(tx_sock, &tx_ring, ifindex);
236 dissector_init_all(ctx->print_mode);
238 if (ctx->cpu >= 0 && ifindex > 0) {
239 irq = device_irq_number(ctx->device_out);
240 device_set_irq_affinity(irq, ctx->cpu);
242 if (ctx->verbose)
243 printf("IRQ: %s:%d > CPU%d\n",
244 ctx->device_out, irq, ctx->cpu);
247 if (ctx->kpull)
248 interval = ctx->kpull;
250 set_itimer_interval_value(&itimer, 0, interval);
251 setitimer(ITIMER_REAL, &itimer, NULL);
253 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
255 printf("Running! Hang up with ^C!\n\n");
256 fflush(stdout);
258 bug_on(gettimeofday(&start, NULL));
260 while (likely(sigint == 0)) {
261 while (user_may_pull_from_tx(tx_ring.frames[it].iov_base)) {
262 hdr = tx_ring.frames[it].iov_base;
263 out = ((uint8_t *) hdr) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
265 do {
266 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic, out,
267 ring_frame_size(&tx_ring));
268 if (unlikely(ret <= 0))
269 goto out;
271 if (ring_frame_size(&tx_ring) <
272 pcap_get_length(&phdr, ctx->magic)) {
273 pcap_set_length(&phdr, ctx->magic,
274 ring_frame_size(&tx_ring));
275 trunced++;
277 } while (ctx->filter &&
278 !bpf_run_filter(&bpf_ops, out,
279 pcap_get_length(&phdr, ctx->magic)));
281 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &hdr->tp_h, &hdr->s_ll);
283 ctx->tx_bytes += hdr->tp_h.tp_len;;
284 ctx->tx_packets++;
286 show_frame_hdr(hdr, ctx->print_mode);
288 dissector_entry_point(out, hdr->tp_h.tp_snaplen,
289 ctx->link_type, ctx->print_mode);
291 kernel_may_pull_from_tx(&hdr->tp_h);
293 it++;
294 if (it >= tx_ring.layout.tp_frame_nr)
295 it = 0;
297 if (unlikely(sigint == 1))
298 break;
300 if (frame_count_max != 0) {
301 if (ctx->tx_packets >= frame_count_max) {
302 sigint = 1;
303 break;
309 out:
311 bug_on(gettimeofday(&end, NULL));
312 timersub(&end, &start, &diff);
314 timer_purge();
316 bpf_release(&bpf_ops);
318 dissector_cleanup_all();
319 destroy_tx_ring(tx_sock, &tx_ring);
321 if (ctx->rfraw)
322 leave_rfmon_mac80211(ctx->device_trans, ctx->device_out);
324 if (__pcap_io->prepare_close_pcap)
325 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
327 if (!strncmp("-", ctx->device_in, strlen("-")))
328 dup2(fd, fileno(stdin));
329 close(fd);
331 close(tx_sock);
333 fflush(stdout);
334 printf("\n");
335 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
336 printf("\r%12lu packets truncated in file\n", trunced);
337 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
338 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
341 static void receive_to_xmit(struct ctx *ctx)
343 short ifflags = 0;
344 uint8_t *in, *out;
345 int rx_sock, ifindex_in, ifindex_out;
346 unsigned int size_in, size_out, it_in = 0, it_out = 0;
347 unsigned long frame_count = 0;
348 struct frame_map *hdr_in, *hdr_out;
349 struct ring tx_ring, rx_ring;
350 struct pollfd rx_poll;
351 struct sock_fprog bpf_ops;
353 if (!strncmp(ctx->device_in, ctx->device_out, IFNAMSIZ))
354 panic("Ingress/egress devices must be different!\n");
355 if (!device_up_and_running(ctx->device_out))
356 panic("Egress device not up and running!\n");
358 rx_sock = pf_socket();
359 tx_sock = pf_socket();
361 fmemset(&tx_ring, 0, sizeof(tx_ring));
362 fmemset(&rx_ring, 0, sizeof(rx_ring));
363 fmemset(&rx_poll, 0, sizeof(rx_poll));
364 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
366 ifindex_in = device_ifindex(ctx->device_in);
367 ifindex_out = device_ifindex(ctx->device_out);
369 size_in = ring_size(ctx->device_in, ctx->reserve_size);
370 size_out = ring_size(ctx->device_out, ctx->reserve_size);
372 enable_kernel_bpf_jit_compiler();
374 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
375 if (ctx->dump_bpf)
376 bpf_dump_all(&bpf_ops);
377 bpf_attach_to_sock(rx_sock, &bpf_ops);
379 setup_rx_ring_layout(rx_sock, &rx_ring, size_in, ctx->jumbo, false);
380 create_rx_ring(rx_sock, &rx_ring, ctx->verbose);
381 mmap_rx_ring(rx_sock, &rx_ring);
382 alloc_rx_ring_frames(rx_sock, &rx_ring);
383 bind_rx_ring(rx_sock, &rx_ring, ifindex_in);
384 prepare_polling(rx_sock, &rx_poll);
386 set_packet_loss_discard(tx_sock);
387 setup_tx_ring_layout(tx_sock, &tx_ring, size_out, ctx->jumbo);
388 create_tx_ring(tx_sock, &tx_ring, ctx->verbose);
389 mmap_tx_ring(tx_sock, &tx_ring);
390 alloc_tx_ring_frames(tx_sock, &tx_ring);
391 bind_tx_ring(tx_sock, &tx_ring, ifindex_out);
393 dissector_init_all(ctx->print_mode);
395 if (ctx->promiscuous)
396 ifflags = enter_promiscuous_mode(ctx->device_in);
398 if (ctx->kpull)
399 interval = ctx->kpull;
401 set_itimer_interval_value(&itimer, 0, interval);
402 setitimer(ITIMER_REAL, &itimer, NULL);
404 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
406 printf("Running! Hang up with ^C!\n\n");
407 fflush(stdout);
409 while (likely(sigint == 0)) {
410 while (user_may_pull_from_rx(rx_ring.frames[it_in].iov_base)) {
411 __label__ next;
413 hdr_in = rx_ring.frames[it_in].iov_base;
414 in = ((uint8_t *) hdr_in) + hdr_in->tp_h.tp_mac;
416 frame_count++;
418 if (ctx->packet_type != -1)
419 if (ctx->packet_type != hdr_in->s_ll.sll_pkttype)
420 goto next;
422 hdr_out = tx_ring.frames[it_out].iov_base;
423 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
425 for (; !user_may_pull_from_tx(tx_ring.frames[it_out].iov_base) &&
426 likely(!sigint);) {
427 if (ctx->randomize)
428 next_rnd_slot(&it_out, &tx_ring);
429 else {
430 it_out++;
431 if (it_out >= tx_ring.layout.tp_frame_nr)
432 it_out = 0;
435 hdr_out = tx_ring.frames[it_out].iov_base;
436 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
439 tpacket_hdr_clone(&hdr_out->tp_h, &hdr_in->tp_h);
440 fmemcpy(out, in, hdr_in->tp_h.tp_len);
442 kernel_may_pull_from_tx(&hdr_out->tp_h);
443 if (ctx->randomize)
444 next_rnd_slot(&it_out, &tx_ring);
445 else {
446 it_out++;
447 if (it_out >= tx_ring.layout.tp_frame_nr)
448 it_out = 0;
451 show_frame_hdr(hdr_in, ctx->print_mode);
453 dissector_entry_point(in, hdr_in->tp_h.tp_snaplen,
454 ctx->link_type, ctx->print_mode);
456 if (frame_count_max != 0) {
457 if (frame_count >= frame_count_max) {
458 sigint = 1;
459 break;
463 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 poll(&rx_poll, 1, -1);
478 out:
480 timer_purge();
482 sock_rx_net_stats(rx_sock);
484 bpf_release(&bpf_ops);
486 dissector_cleanup_all();
488 destroy_tx_ring(tx_sock, &tx_ring);
489 destroy_rx_ring(rx_sock, &rx_ring);
491 if (ctx->promiscuous)
492 leave_promiscuous_mode(ctx->device_in, ifflags);
494 close(tx_sock);
495 close(rx_sock);
498 static void translate_pcap_to_txf(int fdo, uint8_t *out, size_t len)
500 size_t bytes_done = 0;
501 char bout[80];
503 slprintf(bout, sizeof(bout), "{\n ");
504 write_or_die(fdo, bout, strlen(bout));
506 while (bytes_done < len) {
507 slprintf(bout, sizeof(bout), "0x%02x, ", out[bytes_done]);
508 write_or_die(fdo, bout, strlen(bout));
510 bytes_done++;
512 if (bytes_done % 10 == 0) {
513 slprintf(bout, sizeof(bout), "\n");
514 write_or_die(fdo, bout, strlen(bout));
516 if (bytes_done < len) {
517 slprintf(bout, sizeof(bout), " ");
518 write_or_die(fdo, bout, strlen(bout));
522 if (bytes_done % 10 != 0) {
523 slprintf(bout, sizeof(bout), "\n");
524 write_or_die(fdo, bout, strlen(bout));
527 slprintf(bout, sizeof(bout), "}\n\n");
528 write_or_die(fdo, bout, strlen(bout));
531 static void read_pcap(struct ctx *ctx)
533 __label__ out;
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;
542 struct sockaddr_ll sll;
544 bug_on(!__pcap_io);
546 if (!strncmp("-", ctx->device_in, strlen("-"))) {
547 fd = dup_or_die(fileno(stdin));
548 close(fileno(stdin));
549 if (ctx->pcap == PCAP_OPS_MM)
550 ctx->pcap = PCAP_OPS_SG;
551 } else {
552 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
555 if (__pcap_io->init_once_pcap)
556 __pcap_io->init_once_pcap();
558 ret = __pcap_io->pull_fhdr_pcap(fd, &ctx->magic, &ctx->link_type);
559 if (ret)
560 panic("Error reading pcap header!\n");
562 if (__pcap_io->prepare_access_pcap) {
563 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_RD, ctx->jumbo);
564 if (ret)
565 panic("Error prepare reading pcap!\n");
568 fmemset(&fm, 0, sizeof(fm));
569 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
571 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
572 if (ctx->dump_bpf)
573 bpf_dump_all(&bpf_ops);
575 dissector_init_all(ctx->print_mode);
577 out_len = round_up(1024 * 1024, PAGE_SIZE);
578 out = xmalloc_aligned(out_len, CO_CACHE_LINE_SIZE);
580 if (ctx->device_out) {
581 if (!strncmp("-", ctx->device_out, strlen("-"))) {
582 fdo = dup_or_die(fileno(stdout));
583 close(fileno(stdout));
584 } else {
585 fdo = open_or_die_m(ctx->device_out, O_RDWR | O_CREAT |
586 O_TRUNC | O_LARGEFILE, DEFFILEMODE);
590 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
592 printf("Running! Hang up with ^C!\n\n");
593 fflush(stdout);
595 bug_on(gettimeofday(&start, NULL));
597 while (likely(sigint == 0)) {
598 do {
599 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic,
600 out, out_len);
601 if (unlikely(ret < 0))
602 goto out;
604 if (unlikely(pcap_get_length(&phdr, ctx->magic) == 0)) {
605 trunced++;
606 continue;
609 if (unlikely(pcap_get_length(&phdr, ctx->magic) > out_len)) {
610 pcap_set_length(&phdr, ctx->magic, out_len);
611 trunced++;
613 } while (ctx->filter &&
614 !bpf_run_filter(&bpf_ops, out,
615 pcap_get_length(&phdr, ctx->magic)));
617 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &fm.tp_h, &sll);
619 ctx->tx_bytes += fm.tp_h.tp_len;
620 ctx->tx_packets++;
622 show_frame_hdr(&fm, ctx->print_mode);
624 dissector_entry_point(out, fm.tp_h.tp_snaplen,
625 ctx->link_type, ctx->print_mode);
627 if (ctx->device_out)
628 translate_pcap_to_txf(fdo, out, fm.tp_h.tp_snaplen);
630 if (frame_count_max != 0) {
631 if (ctx->tx_packets >= frame_count_max) {
632 sigint = 1;
633 break;
638 out:
640 bug_on(gettimeofday(&end, NULL));
641 timersub(&end, &start, &diff);
643 bpf_release(&bpf_ops);
645 dissector_cleanup_all();
647 if (__pcap_io->prepare_close_pcap)
648 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
650 xfree(out);
652 fflush(stdout);
653 printf("\n");
654 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
655 printf("\r%12lu packets truncated in file\n", trunced);
656 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
657 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
659 if (!strncmp("-", ctx->device_in, strlen("-")))
660 dup2(fd, fileno(stdin));
661 close(fd);
663 if (ctx->device_out) {
664 if (!strncmp("-", ctx->device_out, strlen("-")))
665 dup2(fdo, fileno(stdout));
666 close(fdo);
670 static void finish_multi_pcap_file(struct ctx *ctx, int fd)
672 __pcap_io->fsync_pcap(fd);
674 if (__pcap_io->prepare_close_pcap)
675 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
677 close(fd);
679 fmemset(&itimer, 0, sizeof(itimer));
680 setitimer(ITIMER_REAL, &itimer, NULL);
683 static int next_multi_pcap_file(struct ctx *ctx, int fd)
685 int ret;
686 char fname[512];
688 __pcap_io->fsync_pcap(fd);
690 if (__pcap_io->prepare_close_pcap)
691 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
693 close(fd);
695 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
696 ctx->prefix ? : "dump-", time(0));
698 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
699 O_LARGEFILE, DEFFILEMODE);
701 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
702 if (ret)
703 panic("Error writing pcap header!\n");
705 if (__pcap_io->prepare_access_pcap) {
706 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, true);
707 if (ret)
708 panic("Error prepare writing pcap!\n");
711 return fd;
714 static int begin_multi_pcap_file(struct ctx *ctx)
716 int fd, ret;
717 char fname[256];
719 bug_on(!__pcap_io);
721 if (ctx->device_out[strlen(ctx->device_out) - 1] == '/')
722 ctx->device_out[strlen(ctx->device_out) - 1] = 0;
724 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
725 ctx->prefix ? : "dump-", time(0));
727 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
728 O_LARGEFILE, DEFFILEMODE);
730 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
731 if (ret)
732 panic("Error writing pcap header!\n");
734 if (__pcap_io->prepare_access_pcap) {
735 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, true);
736 if (ret)
737 panic("Error prepare writing pcap!\n");
740 if (ctx->dump_mode == DUMP_INTERVAL_TIME) {
741 interval = ctx->dump_interval;
743 set_itimer_interval_value(&itimer, interval, 0);
744 setitimer(ITIMER_REAL, &itimer, NULL);
745 } else {
746 interval = 0;
749 return fd;
752 static void finish_single_pcap_file(struct ctx *ctx, int fd)
754 __pcap_io->fsync_pcap(fd);
756 if (__pcap_io->prepare_close_pcap)
757 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
759 if (strncmp("-", ctx->device_out, strlen("-")))
760 close(fd);
761 else
762 dup2(fd, fileno(stdout));
765 static int begin_single_pcap_file(struct ctx *ctx)
767 int fd, ret;
769 bug_on(!__pcap_io);
771 if (!strncmp("-", ctx->device_out, strlen("-"))) {
772 fd = dup_or_die(fileno(stdout));
773 close(fileno(stdout));
774 if (ctx->pcap == PCAP_OPS_MM)
775 ctx->pcap = PCAP_OPS_SG;
776 } else {
777 fd = open_or_die_m(ctx->device_out,
778 O_RDWR | O_CREAT | O_TRUNC |
779 O_LARGEFILE, DEFFILEMODE);
782 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
783 if (ret)
784 panic("Error writing pcap header!\n");
786 if (__pcap_io->prepare_access_pcap) {
787 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, true);
788 if (ret)
789 panic("Error prepare writing pcap!\n");
792 return fd;
795 static void print_pcap_file_stats(int sock, struct ctx *ctx)
797 int ret;
798 struct tpacket_stats kstats;
799 socklen_t slen = sizeof(kstats);
801 fmemset(&kstats, 0, sizeof(kstats));
803 ret = getsockopt(sock, SOL_PACKET, PACKET_STATISTICS, &kstats, &slen);
804 if (unlikely(ret))
805 panic("Cannot get packet statistics!\n");
807 if (ctx->print_mode == PRINT_NONE) {
808 printf(".(+%u/-%u)", kstats.tp_packets - kstats.tp_drops,
809 kstats.tp_drops);
810 fflush(stdout);
814 static void walk_t3_block(struct block_desc *pbd, struct ctx *ctx,
815 int sock, int *fd, unsigned long *frame_count)
817 uint8_t *packet;
818 int num_pkts = pbd->h1.num_pkts, i, ret;
819 struct tpacket3_hdr *hdr;
820 pcap_pkthdr_t phdr;
821 struct sockaddr_ll *sll;
823 hdr = (void *) ((uint8_t *) pbd + pbd->h1.offset_to_first_pkt);
824 sll = (void *) ((uint8_t *) hdr + TPACKET_ALIGN(sizeof(*hdr)));
826 for (i = 0; i < num_pkts && likely(sigint == 0); ++i) {
827 __label__ next;
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 != 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:
851 hdr = (void *) ((uint8_t *) hdr + hdr->tp_next_offset);
852 sll = (void *) ((uint8_t *) hdr + TPACKET_ALIGN(sizeof(*hdr)));
854 if (frame_count_max != 0) {
855 if (unlikely(*frame_count >= frame_count_max)) {
856 sigint = 1;
857 break;
861 if (dump_to_pcap(ctx)) {
862 if (ctx->dump_mode == DUMP_INTERVAL_SIZE) {
863 interval += hdr->tp_snaplen;
864 if (interval > ctx->dump_interval) {
865 next_dump = true;
866 interval = 0;
870 if (next_dump) {
871 *fd = next_multi_pcap_file(ctx, *fd);
872 next_dump = false;
874 if (unlikely(ctx->verbose))
875 print_pcap_file_stats(sock, ctx);
881 static void recv_only_or_dump(struct ctx *ctx)
883 short ifflags = 0;
884 int sock, irq, ifindex, fd = 0, ret;
885 unsigned int size, 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(&rx_poll, 0, sizeof(rx_poll));
905 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
907 ifindex = device_ifindex(ctx->device_in);
909 size = ring_size(ctx->device_in, ctx->reserve_size);
911 enable_kernel_bpf_jit_compiler();
913 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
914 if (ctx->dump_bpf)
915 bpf_dump_all(&bpf_ops);
916 bpf_attach_to_sock(sock, &bpf_ops);
918 set_sockopt_hwtimestamp(sock, ctx->device_in);
920 setup_rx_ring_layout(sock, &rx_ring, size, true, true);
921 create_rx_ring(sock, &rx_ring, ctx->verbose);
922 mmap_rx_ring(sock, &rx_ring);
923 alloc_rx_ring_frames(sock, &rx_ring);
924 bind_rx_ring(sock, &rx_ring, ifindex);
926 prepare_polling(sock, &rx_poll);
927 dissector_init_all(ctx->print_mode);
929 if (ctx->cpu >= 0 && ifindex > 0) {
930 irq = device_irq_number(ctx->device_in);
931 device_set_irq_affinity(irq, ctx->cpu);
933 if (ctx->verbose)
934 printf("IRQ: %s:%d > CPU%d\n",
935 ctx->device_in, irq, ctx->cpu);
938 if (ctx->promiscuous)
939 ifflags = enter_promiscuous_mode(ctx->device_in);
941 if (dump_to_pcap(ctx) && __pcap_io->init_once_pcap)
942 __pcap_io->init_once_pcap();
944 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
946 if (dump_to_pcap(ctx)) {
947 __label__ try_file;
948 struct stat stats;
950 fmemset(&stats, 0, sizeof(stats));
951 ret = stat(ctx->device_out, &stats);
952 if (ret < 0) {
953 ctx->dump_dir = 0;
954 goto try_file;
957 ctx->dump_dir = S_ISDIR(stats.st_mode);
958 if (ctx->dump_dir) {
959 fd = begin_multi_pcap_file(ctx);
960 } else {
961 try_file:
962 fd = begin_single_pcap_file(ctx);
966 printf("Running! Hang up with ^C!\n\n");
967 fflush(stdout);
969 bug_on(gettimeofday(&start, NULL));
971 while (likely(sigint == 0)) {
972 while (user_may_pull_from_rx_block((pbd = (void *)
973 rx_ring.frames[it].iov_base))) {
974 walk_t3_block(pbd, ctx, sock, &fd, &frame_count);
976 kernel_may_pull_from_rx_block(pbd);
977 it = (it + 1) % rx_ring.layout3.tp_block_nr;
979 if (unlikely(sigint == 1))
980 break;
983 poll(&rx_poll, 1, -1);
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);
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 leave_promiscuous_mode(ctx->device_in, ifflags);
1006 if (ctx->rfraw)
1007 leave_rfmon_mac80211(ctx->device_trans, 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 __noreturn help(void)
1021 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING);
1022 puts("http://www.netsniff-ng.org\n\n"
1023 "Usage: netsniff-ng [options] [filter-expression]\n"
1024 "Options:\n"
1025 " -i|-d|--dev|--in <dev|pcap|-> Input source as netdev, pcap or pcap stdin\n"
1026 " -o|--out <dev|pcap|dir|cfg|-> Output sink as netdev, pcap, directory, trafgen, or stdout\n"
1027 " -f|--filter <bpf-file|expr> Use BPF filter file from bpfc or tcpdump-like expression\n"
1028 " -t|--type <type> Filter for: host|broadcast|multicast|others|outgoing\n"
1029 " -F|--interval <size|time> Dump interval if -o is a dir: <num>KiB/MiB/GiB/s/sec/min/hrs\n"
1030 " -R|--rfraw Capture or inject raw 802.11 frames\n"
1031 " -n|--num <0|uint> Number of packets until exit (def: 0)\n"
1032 " -P|--prefix <name> Prefix for pcaps stored in directory\n"
1033 " -T|--magic <pcap-magic> Pcap magic number/pcap format to store, see -D\n"
1034 " -D|--dump-pcap-types Dump pcap types and magic numbers and quit\n"
1035 " -B|--dump-bpf Dump generated BPF assembly\n"
1036 " -r|--rand Randomize packet forwarding order (dev->dev)\n"
1037 " -M|--no-promisc No promiscuous mode for netdev\n"
1038 " -A|--no-sock-mem Don't tune core socket memory\n"
1039 " -m|--mmap Mmap(2) pcap file i.e., for replaying pcaps\n"
1040 " -G|--sg Scatter/gather pcap file I/O\n"
1041 " -c|--clrw Use slower read(2)/write(2) I/O\n"
1042 " -S|--ring-size <size> Specify ring size to: <num>KiB/MiB/GiB\n"
1043 " -k|--kernel-pull <uint> Kernel pull from user interval in us (def: 10us)\n"
1044 " -J|--jumbo-support Support replay/fwd 64KB Super Jumbo Frames (def: 2048B)\n"
1045 " -b|--bind-cpu <cpu> Bind to specific CPU\n"
1046 " -u|--user <userid> Drop privileges and change to userid\n"
1047 " -g|--group <groupid> Drop privileges and change to groupid\n"
1048 " -H|--prio-high Make this high priority process\n"
1049 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
1050 " -s|--silent Do not print captured packets\n"
1051 " -q|--less Print less-verbose packet information\n"
1052 " -X|--hex Print packet data in hex format\n"
1053 " -l|--ascii Print human-readable packet data\n"
1054 " -U|--update Update GeoIP databases\n"
1055 " -V|--verbose Be more verbose\n"
1056 " -v|--version Show version and exit\n"
1057 " -h|--help Guess what?!\n\n"
1058 "Examples:\n"
1059 " netsniff-ng --in eth0 --out dump.pcap -s -T 0xa1b2c3d4 --b 0 tcp or udp\n"
1060 " netsniff-ng --in wlan0 --rfraw --out dump.pcap --silent --bind-cpu 0\n"
1061 " netsniff-ng --in dump.pcap --mmap --out eth0 -k1000 --silent --bind-cpu 0\n"
1062 " netsniff-ng --in dump.pcap --out dump.cfg --silent --bind-cpu 0\n"
1063 " netsniff-ng --in eth0 --out eth1 --silent --bind-cpu 0 -J --type host\n"
1064 " netsniff-ng --in eth1 --out /opt/probe/ -s -m --interval 100MiB -b 0\n"
1065 " netsniff-ng --in vlan0 --out dump.pcap -c -u `id -u bob` -g `id -g bob`\n"
1066 " netsniff-ng --in any --filter http.bpf --jumbo-support --ascii -V\n\n"
1067 "Note:\n"
1068 " For introducing bit errors, delays with random variation and more\n"
1069 " while replaying pcaps, make use of tc(8) with its disciplines (e.g. netem).\n\n"
1070 "Please report bugs to <bugs@netsniff-ng.org>\n"
1071 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1072 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1073 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1074 "Swiss federal institute of technology (ETH Zurich)\n"
1075 "License: GNU GPL version 2.0\n"
1076 "This is free software: you are free to change and redistribute it.\n"
1077 "There is NO WARRANTY, to the extent permitted by law.\n");
1078 die();
1081 static void __noreturn version(void)
1083 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_LONG);
1084 puts("http://www.netsniff-ng.org\n\n"
1085 "Please report bugs to <bugs@netsniff-ng.org>\n"
1086 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1087 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1088 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1089 "Swiss federal institute of technology (ETH Zurich)\n"
1090 "License: GNU GPL version 2.0\n"
1091 "This is free software: you are free to change and redistribute it.\n"
1092 "There is NO WARRANTY, to the extent permitted by law.\n");
1093 die();
1096 int main(int argc, char **argv)
1098 char *ptr;
1099 int c, i, j, cpu_tmp, opt_index, ops_touched = 0, vals[4] = {0};
1100 bool prio_high = false, setsockmem = true;
1101 void (*main_loop)(struct ctx *ctx) = NULL;
1102 struct ctx ctx = {
1103 .link_type = LINKTYPE_EN10MB,
1104 .print_mode = PRINT_NORM,
1105 .cpu = -1,
1106 .packet_type = -1,
1107 .promiscuous = true,
1108 .randomize = false,
1109 .pcap = PCAP_OPS_SG,
1110 .dump_interval = 60,
1111 .dump_mode = DUMP_INTERVAL_TIME,
1112 .uid = getuid(),
1113 .gid = getgid(),
1114 .magic = ORIGINAL_TCPDUMP_MAGIC,
1117 srand(time(NULL));
1119 while ((c = getopt_long(argc, argv, short_options, long_options,
1120 &opt_index)) != EOF) {
1121 switch (c) {
1122 case 'd':
1123 case 'i':
1124 ctx.device_in = xstrdup(optarg);
1125 break;
1126 case 'o':
1127 ctx.device_out = xstrdup(optarg);
1128 break;
1129 case 'P':
1130 ctx.prefix = xstrdup(optarg);
1131 break;
1132 case 'R':
1133 ctx.link_type = LINKTYPE_IEEE802_11;
1134 ctx.rfraw = 1;
1135 break;
1136 case 'r':
1137 ctx.randomize = true;
1138 break;
1139 case 'J':
1140 ctx.jumbo = true;
1141 break;
1142 case 'T':
1143 ctx.magic = (uint32_t) strtoul(optarg, NULL, 0);
1144 pcap_check_magic(ctx.magic);
1145 break;
1146 case 'f':
1147 ctx.filter = xstrdup(optarg);
1148 break;
1149 case 'M':
1150 ctx.promiscuous = false;
1151 break;
1152 case 'A':
1153 setsockmem = false;
1154 break;
1155 case 'u':
1156 ctx.uid = strtoul(optarg, NULL, 0);
1157 ctx.enforce = true;
1158 break;
1159 case 'g':
1160 ctx.gid = strtoul(optarg, NULL, 0);
1161 ctx.enforce = true;
1162 break;
1163 case 't':
1164 if (!strncmp(optarg, "host", strlen("host")))
1165 ctx.packet_type = PACKET_HOST;
1166 else if (!strncmp(optarg, "broadcast", strlen("broadcast")))
1167 ctx.packet_type = PACKET_BROADCAST;
1168 else if (!strncmp(optarg, "multicast", strlen("multicast")))
1169 ctx.packet_type = PACKET_MULTICAST;
1170 else if (!strncmp(optarg, "others", strlen("others")))
1171 ctx.packet_type = PACKET_OTHERHOST;
1172 else if (!strncmp(optarg, "outgoing", strlen("outgoing")))
1173 ctx.packet_type = PACKET_OUTGOING;
1174 else
1175 ctx.packet_type = -1;
1176 break;
1177 case 'S':
1178 ptr = optarg;
1179 ctx.reserve_size = 0;
1181 for (j = i = strlen(optarg); i > 0; --i) {
1182 if (!isdigit(optarg[j - i]))
1183 break;
1184 ptr++;
1187 if (!strncmp(ptr, "KiB", strlen("KiB")))
1188 ctx.reserve_size = 1 << 10;
1189 else if (!strncmp(ptr, "MiB", strlen("MiB")))
1190 ctx.reserve_size = 1 << 20;
1191 else if (!strncmp(ptr, "GiB", strlen("GiB")))
1192 ctx.reserve_size = 1 << 30;
1193 else
1194 panic("Syntax error in ring size param!\n");
1195 *ptr = 0;
1197 ctx.reserve_size *= strtol(optarg, NULL, 0);
1198 break;
1199 case 'b':
1200 cpu_tmp = strtol(optarg, NULL, 0);
1202 cpu_affinity(cpu_tmp);
1203 if (ctx.cpu != -2)
1204 ctx.cpu = cpu_tmp;
1205 break;
1206 case 'H':
1207 prio_high = true;
1208 break;
1209 case 'c':
1210 ctx.pcap = PCAP_OPS_RW;
1211 ops_touched = 1;
1212 break;
1213 case 'm':
1214 ctx.pcap = PCAP_OPS_MM;
1215 ops_touched = 1;
1216 break;
1217 case 'G':
1218 ctx.pcap = PCAP_OPS_SG;
1219 ops_touched = 1;
1220 break;
1221 case 'Q':
1222 ctx.cpu = -2;
1223 break;
1224 case 's':
1225 ctx.print_mode = PRINT_NONE;
1226 break;
1227 case 'q':
1228 ctx.print_mode = PRINT_LESS;
1229 break;
1230 case 'X':
1231 ctx.print_mode =
1232 (ctx.print_mode == PRINT_ASCII) ?
1233 PRINT_HEX_ASCII : PRINT_HEX;
1234 break;
1235 case 'l':
1236 ctx.print_mode =
1237 (ctx.print_mode == PRINT_HEX) ?
1238 PRINT_HEX_ASCII : PRINT_ASCII;
1239 break;
1240 case 'k':
1241 ctx.kpull = strtol(optarg, NULL, 0);
1242 break;
1243 case 'n':
1244 frame_count_max = strtol(optarg, NULL, 0);
1245 break;
1246 case 'F':
1247 ptr = optarg;
1248 ctx.dump_interval = 0;
1250 for (j = i = strlen(optarg); i > 0; --i) {
1251 if (!isdigit(optarg[j - i]))
1252 break;
1253 ptr++;
1256 if (!strncmp(ptr, "KiB", strlen("KiB"))) {
1257 ctx.dump_interval = 1 << 10;
1258 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1259 } else if (!strncmp(ptr, "MiB", strlen("MiB"))) {
1260 ctx.dump_interval = 1 << 20;
1261 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1262 } else if (!strncmp(ptr, "GiB", strlen("GiB"))) {
1263 ctx.dump_interval = 1 << 30;
1264 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1265 } else if (!strncmp(ptr, "sec", strlen("sec"))) {
1266 ctx.dump_interval = 1;
1267 ctx.dump_mode = DUMP_INTERVAL_TIME;
1268 } else if (!strncmp(ptr, "min", strlen("min"))) {
1269 ctx.dump_interval = 60;
1270 ctx.dump_mode = DUMP_INTERVAL_TIME;
1271 } else if (!strncmp(ptr, "hrs", strlen("hrs"))) {
1272 ctx.dump_interval = 60 * 60;
1273 ctx.dump_mode = DUMP_INTERVAL_TIME;
1274 } else if (!strncmp(ptr, "s", strlen("s"))) {
1275 ctx.dump_interval = 1;
1276 ctx.dump_mode = DUMP_INTERVAL_TIME;
1277 } else {
1278 panic("Syntax error in time/size param!\n");
1281 *ptr = 0;
1282 ctx.dump_interval *= strtol(optarg, NULL, 0);
1283 break;
1284 case 'V':
1285 ctx.verbose = 1;
1286 break;
1287 case 'B':
1288 ctx.dump_bpf = true;
1289 break;
1290 case 'D':
1291 pcap_dump_type_features();
1292 die();
1293 break;
1294 case 'U':
1295 update_geoip();
1296 die();
1297 break;
1298 case 'v':
1299 version();
1300 break;
1301 case 'h':
1302 help();
1303 break;
1304 case '?':
1305 switch (optopt) {
1306 case 'd':
1307 case 'i':
1308 case 'o':
1309 case 'f':
1310 case 't':
1311 case 'P':
1312 case 'F':
1313 case 'n':
1314 case 'S':
1315 case 'b':
1316 case 'k':
1317 case 'T':
1318 case 'u':
1319 case 'g':
1320 case 'e':
1321 panic("Option -%c requires an argument!\n",
1322 optopt);
1323 default:
1324 if (isprint(optopt))
1325 printf("Unknown option character `0x%X\'!\n", optopt);
1326 die();
1328 default:
1329 break;
1333 if (!ctx.filter && optind != argc) {
1334 int ret;
1335 off_t offset = 0;
1337 for (i = optind; i < argc; ++i) {
1338 size_t alen = strlen(argv[i]) + 2;
1339 size_t flen = ctx.filter ? strlen(ctx.filter) : 0;
1341 ctx.filter = xrealloc(ctx.filter, 1, flen + alen);
1342 ret = slprintf(ctx.filter + offset, strlen(argv[i]) + 2, "%s ", argv[i]);
1343 if (ret < 0)
1344 panic("Cannot concatenate filter string!\n");
1345 else
1346 offset += ret;
1350 if (!ctx.device_in)
1351 ctx.device_in = xstrdup("any");
1353 register_signal(SIGINT, signal_handler);
1354 register_signal(SIGHUP, signal_handler);
1356 tprintf_init();
1358 if (prio_high) {
1359 set_proc_prio(-20);
1360 set_sched_status(SCHED_FIFO, sched_get_priority_max(SCHED_FIFO));
1363 if (ctx.device_in && (device_mtu(ctx.device_in) ||
1364 !strncmp("any", ctx.device_in, strlen(ctx.device_in)))) {
1365 if (!ctx.device_out) {
1366 ctx.dump = 0;
1367 main_loop = recv_only_or_dump;
1368 } else if (device_mtu(ctx.device_out)) {
1369 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1370 main_loop = receive_to_xmit;
1371 } else {
1372 ctx.dump = 1;
1373 register_signal_f(SIGALRM, timer_next_dump, SA_SIGINFO);
1374 main_loop = recv_only_or_dump;
1375 if (!ops_touched)
1376 ctx.pcap = PCAP_OPS_SG;
1378 } else {
1379 if (ctx.device_out && device_mtu(ctx.device_out)) {
1380 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1381 main_loop = pcap_to_xmit;
1382 if (!ops_touched)
1383 ctx.pcap = PCAP_OPS_MM;
1384 } else {
1385 main_loop = read_pcap;
1386 if (!ops_touched)
1387 ctx.pcap = PCAP_OPS_SG;
1391 bug_on(!main_loop);
1393 init_geoip(0);
1394 if (setsockmem)
1395 set_system_socket_memory(vals, array_size(vals));
1396 if (!ctx.enforce)
1397 xlockme();
1399 main_loop(&ctx);
1401 if (!ctx.enforce)
1402 xunlockme();
1403 if (setsockmem)
1404 reset_system_socket_memory(vals, array_size(vals));
1405 destroy_geoip();
1406 device_restore_irq_affinity_list();
1407 tprintf_cleanup();
1409 free(ctx.device_in);
1410 free(ctx.device_out);
1411 free(ctx.device_trans);
1412 free(ctx.prefix);
1414 return 0;