ifpps: Rename variable containing top number of CPUs
[netsniff-ng.git] / netsniff-ng.c
blob4d9abc8dc4d036a77588d452dcef460bd3e37da9
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 "xutils.h"
30 #include "built_in.h"
31 #include "pcap_io.h"
32 #include "bpf.h"
33 #include "xio.h"
34 #include "die.h"
35 #include "geoip.h"
36 #include "tprintf.h"
37 #include "dissector.h"
38 #include "xmalloc.h"
40 extern int set_sockopt_hwtimestamp(int sock, const char *dev);
42 enum dump_mode {
43 DUMP_INTERVAL_TIME,
44 DUMP_INTERVAL_SIZE,
47 struct ctx {
48 char *device_in, *device_out, *device_trans, *filter, *prefix;
49 int cpu, rfraw, dump, print_mode, dump_dir, packet_type, verbose;
50 unsigned long kpull, dump_interval, reserve_size, tx_bytes, tx_packets;
51 bool randomize, promiscuous, enforce, jumbo, dump_bpf;
52 enum pcap_ops_groups pcap; enum dump_mode dump_mode;
53 uid_t uid; gid_t gid; uint32_t link_type, magic;
56 volatile sig_atomic_t sigint = 0;
58 static volatile bool next_dump = false;
60 static const char *short_options = "d:i:o:rf:MJt:S:k:n:b:HQmcsqXlvhF:RGAP:Vu:g:T:DB";
61 static const struct option long_options[] = {
62 {"dev", required_argument, NULL, 'd'},
63 {"in", required_argument, NULL, 'i'},
64 {"out", required_argument, NULL, 'o'},
65 {"filter", required_argument, NULL, 'f'},
66 {"num", required_argument, NULL, 'n'},
67 {"type", required_argument, NULL, 't'},
68 {"interval", required_argument, NULL, 'F'},
69 {"ring-size", required_argument, NULL, 'S'},
70 {"kernel-pull", required_argument, NULL, 'k'},
71 {"bind-cpu", required_argument, NULL, 'b'},
72 {"prefix", required_argument, NULL, 'P'},
73 {"user", required_argument, NULL, 'u'},
74 {"group", required_argument, NULL, 'g'},
75 {"magic", required_argument, NULL, 'T'},
76 {"rand", no_argument, NULL, 'r'},
77 {"rfraw", no_argument, NULL, 'R'},
78 {"mmap", no_argument, NULL, 'm'},
79 {"sg", no_argument, NULL, 'G'},
80 {"clrw", no_argument, NULL, 'c'},
81 {"jumbo-support", no_argument, NULL, 'J'},
82 {"no-promisc", no_argument, NULL, 'M'},
83 {"prio-high", no_argument, NULL, 'H'},
84 {"notouch-irq", no_argument, NULL, 'Q'},
85 {"dump-pcap-types", no_argument, NULL, 'D'},
86 {"dump-bpf", no_argument, NULL, 'B'},
87 {"silent", no_argument, NULL, 's'},
88 {"less", no_argument, NULL, 'q'},
89 {"hex", no_argument, NULL, 'X'},
90 {"ascii", no_argument, NULL, 'l'},
91 {"no-sock-mem", no_argument, NULL, 'A'},
92 {"update", no_argument, NULL, 'U'},
93 {"verbose", no_argument, NULL, 'V'},
94 {"version", no_argument, NULL, 'v'},
95 {"help", no_argument, NULL, 'h'},
96 {NULL, 0, NULL, 0}
99 static int tx_sock;
101 static struct itimerval itimer;
103 static unsigned long frame_count_max = 0, interval = TX_KERNEL_PULL_INT;
105 #define __pcap_io pcap_ops[ctx->pcap]
107 static void signal_handler(int number)
109 switch (number) {
110 case SIGINT:
111 sigint = 1;
112 case SIGHUP:
113 default:
114 break;
118 static void timer_elapsed(int unused __maybe_unused)
120 int ret;
122 set_itimer_interval_value(&itimer, 0, interval);
124 ret = pull_and_flush_tx_ring(tx_sock);
125 if (unlikely(ret < 0)) {
126 /* We could hit EBADF if the socket has been closed before
127 * the timer was triggered.
129 if (errno != EBADF && errno != ENOBUFS)
130 panic("Flushing TX_RING failed: %s!\n", strerror(errno));
133 setitimer(ITIMER_REAL, &itimer, NULL);
136 static void timer_purge(void)
138 int ret;
140 ret = pull_and_flush_tx_ring_wait(tx_sock);
141 if (unlikely(ret < 0)) {
142 if (errno != EBADF && errno != ENOBUFS)
143 panic("Flushing TX_RING failed: %s!\n", strerror(errno));
146 set_itimer_interval_value(&itimer, 0, 0);
147 setitimer(ITIMER_REAL, &itimer, NULL);
150 static void timer_next_dump(int unused __maybe_unused)
152 set_itimer_interval_value(&itimer, interval, 0);
153 next_dump = true;
154 setitimer(ITIMER_REAL, &itimer, NULL);
157 static inline bool dump_to_pcap(struct ctx *ctx)
159 return ctx->dump;
162 static void pcap_to_xmit(struct ctx *ctx)
164 __label__ out;
165 uint8_t *out = NULL;
166 int irq, ifindex, fd = 0, ret;
167 unsigned int size, it = 0;
168 unsigned long trunced = 0;
169 struct ring tx_ring;
170 struct frame_map *hdr;
171 struct sock_fprog bpf_ops;
172 struct timeval start, end, diff;
173 pcap_pkthdr_t phdr;
175 if (!device_up_and_running(ctx->device_out) && !ctx->rfraw)
176 panic("Device not up and running!\n");
178 bug_on(!__pcap_io);
180 tx_sock = pf_socket();
182 if (!strncmp("-", ctx->device_in, strlen("-"))) {
183 fd = dup(fileno(stdin));
184 close(fileno(stdin));
185 if (ctx->pcap == PCAP_OPS_MM)
186 ctx->pcap = PCAP_OPS_SG;
187 } else {
188 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
191 if (__pcap_io->init_once_pcap)
192 __pcap_io->init_once_pcap();
194 ret = __pcap_io->pull_fhdr_pcap(fd, &ctx->magic, &ctx->link_type);
195 if (ret)
196 panic("Error reading pcap header!\n");
198 if (__pcap_io->prepare_access_pcap) {
199 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_RD, ctx->jumbo);
200 if (ret)
201 panic("Error prepare reading pcap!\n");
204 fmemset(&tx_ring, 0, sizeof(tx_ring));
205 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
207 if (ctx->rfraw) {
208 ctx->device_trans = xstrdup(ctx->device_out);
209 xfree(ctx->device_out);
211 enter_rfmon_mac80211(ctx->device_trans, &ctx->device_out);
212 if (ctx->link_type != LINKTYPE_IEEE802_11)
213 panic("Wrong linktype of pcap!\n");
216 ifindex = device_ifindex(ctx->device_out);
218 size = ring_size(ctx->device_out, ctx->reserve_size);
220 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
221 if (ctx->dump_bpf)
222 bpf_dump_all(&bpf_ops);
224 set_packet_loss_discard(tx_sock);
226 setup_tx_ring_layout(tx_sock, &tx_ring, size, ctx->jumbo);
227 create_tx_ring(tx_sock, &tx_ring, ctx->verbose);
228 mmap_tx_ring(tx_sock, &tx_ring);
229 alloc_tx_ring_frames(tx_sock, &tx_ring);
230 bind_tx_ring(tx_sock, &tx_ring, ifindex);
232 dissector_init_all(ctx->print_mode);
234 if (ctx->cpu >= 0 && ifindex > 0) {
235 irq = device_irq_number(ctx->device_out);
236 device_bind_irq_to_cpu(irq, ctx->cpu);
238 if (ctx->verbose)
239 printf("IRQ: %s:%d > CPU%d\n",
240 ctx->device_out, irq, ctx->cpu);
243 if (ctx->kpull)
244 interval = ctx->kpull;
246 set_itimer_interval_value(&itimer, 0, interval);
247 setitimer(ITIMER_REAL, &itimer, NULL);
249 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
251 printf("Running! Hang up with ^C!\n\n");
252 fflush(stdout);
254 bug_on(gettimeofday(&start, NULL));
256 while (likely(sigint == 0)) {
257 while (user_may_pull_from_tx(tx_ring.frames[it].iov_base)) {
258 hdr = tx_ring.frames[it].iov_base;
259 out = ((uint8_t *) hdr) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
261 do {
262 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic, out,
263 ring_frame_size(&tx_ring));
264 if (unlikely(ret <= 0))
265 goto out;
267 if (ring_frame_size(&tx_ring) <
268 pcap_get_length(&phdr, ctx->magic)) {
269 pcap_set_length(&phdr, ctx->magic,
270 ring_frame_size(&tx_ring));
271 trunced++;
273 } while (ctx->filter &&
274 !bpf_run_filter(&bpf_ops, out,
275 pcap_get_length(&phdr, ctx->magic)));
277 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &hdr->tp_h, &hdr->s_ll);
279 ctx->tx_bytes += hdr->tp_h.tp_len;;
280 ctx->tx_packets++;
282 show_frame_hdr(hdr, ctx->print_mode);
284 dissector_entry_point(out, hdr->tp_h.tp_snaplen,
285 ctx->link_type, ctx->print_mode);
287 kernel_may_pull_from_tx(&hdr->tp_h);
289 it++;
290 if (it >= tx_ring.layout.tp_frame_nr)
291 it = 0;
293 if (unlikely(sigint == 1))
294 break;
296 if (frame_count_max != 0) {
297 if (ctx->tx_packets >= frame_count_max) {
298 sigint = 1;
299 break;
305 out:
307 bug_on(gettimeofday(&end, NULL));
308 timersub(&end, &start, &diff);
310 timer_purge();
312 bpf_release(&bpf_ops);
314 dissector_cleanup_all();
315 destroy_tx_ring(tx_sock, &tx_ring);
317 if (ctx->rfraw)
318 leave_rfmon_mac80211(ctx->device_trans, ctx->device_out);
320 if (__pcap_io->prepare_close_pcap)
321 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
323 if (!strncmp("-", ctx->device_in, strlen("-")))
324 dup2(fd, fileno(stdin));
325 close(fd);
327 close(tx_sock);
329 fflush(stdout);
330 printf("\n");
331 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
332 printf("\r%12lu packets truncated in file\n", trunced);
333 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
334 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
337 static void receive_to_xmit(struct ctx *ctx)
339 short ifflags = 0;
340 uint8_t *in, *out;
341 int rx_sock, ifindex_in, ifindex_out;
342 unsigned int size_in, size_out, it_in = 0, it_out = 0;
343 unsigned long frame_count = 0;
344 struct frame_map *hdr_in, *hdr_out;
345 struct ring tx_ring, rx_ring;
346 struct pollfd rx_poll;
347 struct sock_fprog bpf_ops;
349 if (!strncmp(ctx->device_in, ctx->device_out, IFNAMSIZ))
350 panic("Ingress/egress devices must be different!\n");
351 if (!device_up_and_running(ctx->device_out))
352 panic("Egress device not up and running!\n");
354 rx_sock = pf_socket();
355 tx_sock = pf_socket();
357 fmemset(&tx_ring, 0, sizeof(tx_ring));
358 fmemset(&rx_ring, 0, sizeof(rx_ring));
359 fmemset(&rx_poll, 0, sizeof(rx_poll));
360 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
362 ifindex_in = device_ifindex(ctx->device_in);
363 ifindex_out = device_ifindex(ctx->device_out);
365 size_in = ring_size(ctx->device_in, ctx->reserve_size);
366 size_out = ring_size(ctx->device_out, ctx->reserve_size);
368 enable_kernel_bpf_jit_compiler();
370 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
371 if (ctx->dump_bpf)
372 bpf_dump_all(&bpf_ops);
373 bpf_attach_to_sock(rx_sock, &bpf_ops);
375 setup_rx_ring_layout(rx_sock, &rx_ring, size_in, ctx->jumbo, false);
376 create_rx_ring(rx_sock, &rx_ring, ctx->verbose);
377 mmap_rx_ring(rx_sock, &rx_ring);
378 alloc_rx_ring_frames(rx_sock, &rx_ring);
379 bind_rx_ring(rx_sock, &rx_ring, ifindex_in);
380 prepare_polling(rx_sock, &rx_poll);
382 set_packet_loss_discard(tx_sock);
383 setup_tx_ring_layout(tx_sock, &tx_ring, size_out, ctx->jumbo);
384 create_tx_ring(tx_sock, &tx_ring, ctx->verbose);
385 mmap_tx_ring(tx_sock, &tx_ring);
386 alloc_tx_ring_frames(tx_sock, &tx_ring);
387 bind_tx_ring(tx_sock, &tx_ring, ifindex_out);
389 dissector_init_all(ctx->print_mode);
391 if (ctx->promiscuous)
392 ifflags = enter_promiscuous_mode(ctx->device_in);
394 if (ctx->kpull)
395 interval = ctx->kpull;
397 set_itimer_interval_value(&itimer, 0, interval);
398 setitimer(ITIMER_REAL, &itimer, NULL);
400 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
402 printf("Running! Hang up with ^C!\n\n");
403 fflush(stdout);
405 while (likely(sigint == 0)) {
406 while (user_may_pull_from_rx(rx_ring.frames[it_in].iov_base)) {
407 __label__ next;
409 hdr_in = rx_ring.frames[it_in].iov_base;
410 in = ((uint8_t *) hdr_in) + hdr_in->tp_h.tp_mac;
412 frame_count++;
414 if (ctx->packet_type != -1)
415 if (ctx->packet_type != hdr_in->s_ll.sll_pkttype)
416 goto next;
418 hdr_out = tx_ring.frames[it_out].iov_base;
419 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
421 for (; !user_may_pull_from_tx(tx_ring.frames[it_out].iov_base) &&
422 likely(!sigint);) {
423 if (ctx->randomize)
424 next_rnd_slot(&it_out, &tx_ring);
425 else {
426 it_out++;
427 if (it_out >= tx_ring.layout.tp_frame_nr)
428 it_out = 0;
431 hdr_out = tx_ring.frames[it_out].iov_base;
432 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
435 tpacket_hdr_clone(&hdr_out->tp_h, &hdr_in->tp_h);
436 fmemcpy(out, in, hdr_in->tp_h.tp_len);
438 kernel_may_pull_from_tx(&hdr_out->tp_h);
439 if (ctx->randomize)
440 next_rnd_slot(&it_out, &tx_ring);
441 else {
442 it_out++;
443 if (it_out >= tx_ring.layout.tp_frame_nr)
444 it_out = 0;
447 show_frame_hdr(hdr_in, ctx->print_mode);
449 dissector_entry_point(in, hdr_in->tp_h.tp_snaplen,
450 ctx->link_type, ctx->print_mode);
452 if (frame_count_max != 0) {
453 if (frame_count >= frame_count_max) {
454 sigint = 1;
455 break;
459 next:
461 kernel_may_pull_from_rx(&hdr_in->tp_h);
463 it_in++;
464 if (it_in >= rx_ring.layout.tp_frame_nr)
465 it_in = 0;
467 if (unlikely(sigint == 1))
468 goto out;
471 poll(&rx_poll, 1, -1);
474 out:
476 timer_purge();
478 sock_print_net_stats(rx_sock);
480 bpf_release(&bpf_ops);
482 dissector_cleanup_all();
484 destroy_tx_ring(tx_sock, &tx_ring);
485 destroy_rx_ring(rx_sock, &rx_ring);
487 if (ctx->promiscuous)
488 leave_promiscuous_mode(ctx->device_in, ifflags);
490 close(tx_sock);
491 close(rx_sock);
494 static void translate_pcap_to_txf(int fdo, uint8_t *out, size_t len)
496 size_t bytes_done = 0;
497 char bout[80];
499 slprintf(bout, sizeof(bout), "{\n ");
500 write_or_die(fdo, bout, strlen(bout));
502 while (bytes_done < len) {
503 slprintf(bout, sizeof(bout), "0x%02x, ", out[bytes_done]);
504 write_or_die(fdo, bout, strlen(bout));
506 bytes_done++;
508 if (bytes_done % 10 == 0) {
509 slprintf(bout, sizeof(bout), "\n");
510 write_or_die(fdo, bout, strlen(bout));
512 if (bytes_done < len) {
513 slprintf(bout, sizeof(bout), " ");
514 write_or_die(fdo, bout, strlen(bout));
518 if (bytes_done % 10 != 0) {
519 slprintf(bout, sizeof(bout), "\n");
520 write_or_die(fdo, bout, strlen(bout));
523 slprintf(bout, sizeof(bout), "}\n\n");
524 write_or_die(fdo, bout, strlen(bout));
527 static void read_pcap(struct ctx *ctx)
529 __label__ out;
530 uint8_t *out;
531 int ret, fd, fdo = 0;
532 unsigned long trunced = 0;
533 size_t out_len;
534 pcap_pkthdr_t phdr;
535 struct sock_fprog bpf_ops;
536 struct frame_map fm;
537 struct timeval start, end, diff;
538 struct sockaddr_ll sll;
540 bug_on(!__pcap_io);
542 if (!strncmp("-", ctx->device_in, strlen("-"))) {
543 fd = dup(fileno(stdin));
544 close(fileno(stdin));
545 if (ctx->pcap == PCAP_OPS_MM)
546 ctx->pcap = PCAP_OPS_SG;
547 } else {
548 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
551 if (__pcap_io->init_once_pcap)
552 __pcap_io->init_once_pcap();
554 ret = __pcap_io->pull_fhdr_pcap(fd, &ctx->magic, &ctx->link_type);
555 if (ret)
556 panic("Error reading pcap header!\n");
558 if (__pcap_io->prepare_access_pcap) {
559 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_RD, ctx->jumbo);
560 if (ret)
561 panic("Error prepare reading pcap!\n");
564 fmemset(&fm, 0, sizeof(fm));
565 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
567 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
568 if (ctx->dump_bpf)
569 bpf_dump_all(&bpf_ops);
571 dissector_init_all(ctx->print_mode);
573 out_len = round_up(1024 * 1024, PAGE_SIZE);
574 out = xmalloc_aligned(out_len, CO_CACHE_LINE_SIZE);
576 if (ctx->device_out) {
577 if (!strncmp("-", ctx->device_out, strlen("-"))) {
578 fdo = dup(fileno(stdout));
579 close(fileno(stdout));
580 } else {
581 fdo = open_or_die_m(ctx->device_out, O_RDWR | O_CREAT |
582 O_TRUNC | O_LARGEFILE, DEFFILEMODE);
586 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
588 printf("Running! Hang up with ^C!\n\n");
589 fflush(stdout);
591 bug_on(gettimeofday(&start, NULL));
593 while (likely(sigint == 0)) {
594 do {
595 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic,
596 out, out_len);
597 if (unlikely(ret < 0))
598 goto out;
600 if (unlikely(pcap_get_length(&phdr, ctx->magic) == 0)) {
601 trunced++;
602 continue;
605 if (unlikely(pcap_get_length(&phdr, ctx->magic) > out_len)) {
606 pcap_set_length(&phdr, ctx->magic, out_len);
607 trunced++;
609 } while (ctx->filter &&
610 !bpf_run_filter(&bpf_ops, out,
611 pcap_get_length(&phdr, ctx->magic)));
613 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &fm.tp_h, &sll);
615 ctx->tx_bytes += fm.tp_h.tp_len;
616 ctx->tx_packets++;
618 show_frame_hdr(&fm, ctx->print_mode);
620 dissector_entry_point(out, fm.tp_h.tp_snaplen,
621 ctx->link_type, ctx->print_mode);
623 if (ctx->device_out)
624 translate_pcap_to_txf(fdo, out, fm.tp_h.tp_snaplen);
626 if (frame_count_max != 0) {
627 if (ctx->tx_packets >= frame_count_max) {
628 sigint = 1;
629 break;
634 out:
636 bug_on(gettimeofday(&end, NULL));
637 timersub(&end, &start, &diff);
639 bpf_release(&bpf_ops);
641 dissector_cleanup_all();
643 if (__pcap_io->prepare_close_pcap)
644 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
646 xfree(out);
648 fflush(stdout);
649 printf("\n");
650 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
651 printf("\r%12lu packets truncated in file\n", trunced);
652 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
653 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
655 if (!strncmp("-", ctx->device_in, strlen("-")))
656 dup2(fd, fileno(stdin));
657 close(fd);
659 if (ctx->device_out) {
660 if (!strncmp("-", ctx->device_out, strlen("-")))
661 dup2(fdo, fileno(stdout));
662 close(fdo);
666 static void finish_multi_pcap_file(struct ctx *ctx, int fd)
668 __pcap_io->fsync_pcap(fd);
670 if (__pcap_io->prepare_close_pcap)
671 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
673 close(fd);
675 fmemset(&itimer, 0, sizeof(itimer));
676 setitimer(ITIMER_REAL, &itimer, NULL);
679 static int next_multi_pcap_file(struct ctx *ctx, int fd)
681 int ret;
682 char fname[512];
684 __pcap_io->fsync_pcap(fd);
686 if (__pcap_io->prepare_close_pcap)
687 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
689 close(fd);
691 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
692 ctx->prefix ? : "dump-", time(0));
694 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
695 O_LARGEFILE, DEFFILEMODE);
697 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
698 if (ret)
699 panic("Error writing pcap header!\n");
701 if (__pcap_io->prepare_access_pcap) {
702 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, true);
703 if (ret)
704 panic("Error prepare writing pcap!\n");
707 return fd;
710 static int begin_multi_pcap_file(struct ctx *ctx)
712 int fd, ret;
713 char fname[256];
715 bug_on(!__pcap_io);
717 if (ctx->device_out[strlen(ctx->device_out) - 1] == '/')
718 ctx->device_out[strlen(ctx->device_out) - 1] = 0;
720 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
721 ctx->prefix ? : "dump-", time(0));
723 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
724 O_LARGEFILE, DEFFILEMODE);
726 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
727 if (ret)
728 panic("Error writing pcap header!\n");
730 if (__pcap_io->prepare_access_pcap) {
731 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, true);
732 if (ret)
733 panic("Error prepare writing pcap!\n");
736 if (ctx->dump_mode == DUMP_INTERVAL_TIME) {
737 interval = ctx->dump_interval;
739 set_itimer_interval_value(&itimer, interval, 0);
740 setitimer(ITIMER_REAL, &itimer, NULL);
741 } else {
742 interval = 0;
745 return fd;
748 static void finish_single_pcap_file(struct ctx *ctx, int fd)
750 __pcap_io->fsync_pcap(fd);
752 if (__pcap_io->prepare_close_pcap)
753 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
755 if (strncmp("-", ctx->device_out, strlen("-")))
756 close(fd);
757 else
758 dup2(fd, fileno(stdout));
761 static int begin_single_pcap_file(struct ctx *ctx)
763 int fd, ret;
765 bug_on(!__pcap_io);
767 if (!strncmp("-", ctx->device_out, strlen("-"))) {
768 fd = dup(fileno(stdout));
769 close(fileno(stdout));
770 if (ctx->pcap == PCAP_OPS_MM)
771 ctx->pcap = PCAP_OPS_SG;
772 } else {
773 fd = open_or_die_m(ctx->device_out,
774 O_RDWR | O_CREAT | O_TRUNC |
775 O_LARGEFILE, DEFFILEMODE);
778 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
779 if (ret)
780 panic("Error writing pcap header!\n");
782 if (__pcap_io->prepare_access_pcap) {
783 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, true);
784 if (ret)
785 panic("Error prepare writing pcap!\n");
788 return fd;
791 static void print_pcap_file_stats(int sock, struct ctx *ctx)
793 int ret;
794 struct tpacket_stats kstats;
795 socklen_t slen = sizeof(kstats);
797 fmemset(&kstats, 0, sizeof(kstats));
799 ret = getsockopt(sock, SOL_PACKET, PACKET_STATISTICS, &kstats, &slen);
800 if (unlikely(ret))
801 panic("Cannot get packet statistics!\n");
803 if (ctx->print_mode == PRINT_NONE) {
804 printf(".(+%u/-%u)", kstats.tp_packets - kstats.tp_drops,
805 kstats.tp_drops);
806 fflush(stdout);
810 static void walk_t3_block(struct block_desc *pbd, struct ctx *ctx,
811 int sock, int fd)
813 uint8_t *packet;
814 int num_pkts = pbd->h1.num_pkts, i, ret;
815 unsigned long frame_count = 0;
816 struct tpacket3_hdr *hdr;
817 pcap_pkthdr_t phdr;
818 struct sockaddr_ll *sll;
820 hdr = (void *) ((uint8_t *) pbd + pbd->h1.offset_to_first_pkt);
821 sll = (void *) ((uint8_t *) hdr + TPACKET_ALIGN(sizeof(*hdr)));
823 for (i = 0; i < num_pkts && likely(sigint == 0); ++i) {
824 __label__ next;
825 packet = ((uint8_t *) hdr + hdr->tp_mac);
826 frame_count++;
828 if (ctx->packet_type != -1)
829 if (ctx->packet_type != sll->sll_pkttype)
830 goto next;
832 if (dump_to_pcap(ctx)) {
833 tpacket3_hdr_to_pcap_pkthdr(hdr, sll, &phdr, ctx->magic);
835 ret = __pcap_io->write_pcap(fd, &phdr, ctx->magic, packet,
836 pcap_get_length(&phdr, ctx->magic));
837 if (unlikely(ret != pcap_get_total_length(&phdr, ctx->magic)))
838 panic("Write error to pcap!\n");
841 __show_frame_hdr(sll, hdr, ctx->print_mode, true);
843 dissector_entry_point(packet, hdr->tp_snaplen, ctx->link_type,
844 ctx->print_mode);
845 next:
847 hdr = (void *) ((uint8_t *) hdr + hdr->tp_next_offset);
848 sll = (void *) ((uint8_t *) hdr + TPACKET_ALIGN(sizeof(*hdr)));
850 if (frame_count_max != 0) {
851 if (frame_count >= frame_count_max) {
852 sigint = 1;
853 break;
857 if (dump_to_pcap(ctx)) {
858 if (ctx->dump_mode == DUMP_INTERVAL_SIZE) {
859 interval += hdr->tp_snaplen;
860 if (interval > ctx->dump_interval) {
861 next_dump = true;
862 interval = 0;
866 if (next_dump) {
867 fd = next_multi_pcap_file(ctx, fd);
868 next_dump = false;
870 if (unlikely(ctx->verbose))
871 print_pcap_file_stats(sock, ctx);
877 static void recv_only_or_dump(struct ctx *ctx)
879 short ifflags = 0;
880 int sock, irq, ifindex, fd = 0, ret;
881 unsigned int size, it = 0;
882 struct ring rx_ring;
883 struct pollfd rx_poll;
884 struct sock_fprog bpf_ops;
885 struct timeval start, end, diff;
886 struct block_desc *pbd;
888 sock = pf_socket();
890 if (ctx->rfraw) {
891 ctx->device_trans = xstrdup(ctx->device_in);
892 xfree(ctx->device_in);
894 enter_rfmon_mac80211(ctx->device_trans, &ctx->device_in);
895 ctx->link_type = LINKTYPE_IEEE802_11;
898 fmemset(&rx_ring, 0, sizeof(rx_ring));
899 fmemset(&rx_poll, 0, sizeof(rx_poll));
900 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
902 ifindex = device_ifindex(ctx->device_in);
904 size = ring_size(ctx->device_in, ctx->reserve_size);
906 enable_kernel_bpf_jit_compiler();
908 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
909 if (ctx->dump_bpf)
910 bpf_dump_all(&bpf_ops);
911 bpf_attach_to_sock(sock, &bpf_ops);
913 set_sockopt_hwtimestamp(sock, ctx->device_in);
915 setup_rx_ring_layout(sock, &rx_ring, size, true, true);
916 create_rx_ring(sock, &rx_ring, ctx->verbose);
917 mmap_rx_ring(sock, &rx_ring);
918 alloc_rx_ring_frames(sock, &rx_ring);
919 bind_rx_ring(sock, &rx_ring, ifindex);
921 prepare_polling(sock, &rx_poll);
922 dissector_init_all(ctx->print_mode);
924 if (ctx->cpu >= 0 && ifindex > 0) {
925 irq = device_irq_number(ctx->device_in);
926 device_bind_irq_to_cpu(irq, ctx->cpu);
928 if (ctx->verbose)
929 printf("IRQ: %s:%d > CPU%d\n",
930 ctx->device_in, irq, ctx->cpu);
933 if (ctx->promiscuous)
934 ifflags = enter_promiscuous_mode(ctx->device_in);
936 if (dump_to_pcap(ctx) && __pcap_io->init_once_pcap)
937 __pcap_io->init_once_pcap();
939 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
941 if (dump_to_pcap(ctx)) {
942 __label__ try_file;
943 struct stat stats;
945 fmemset(&stats, 0, sizeof(stats));
946 ret = stat(ctx->device_out, &stats);
947 if (ret < 0) {
948 ctx->dump_dir = 0;
949 goto try_file;
952 ctx->dump_dir = S_ISDIR(stats.st_mode);
953 if (ctx->dump_dir) {
954 fd = begin_multi_pcap_file(ctx);
955 } else {
956 try_file:
957 fd = begin_single_pcap_file(ctx);
961 printf("Running! Hang up with ^C!\n\n");
962 fflush(stdout);
964 bug_on(gettimeofday(&start, NULL));
966 while (likely(sigint == 0)) {
967 while (user_may_pull_from_rx_block((pbd = (void *)
968 rx_ring.frames[it].iov_base))) {
969 walk_t3_block(pbd, ctx, sock, fd);
971 kernel_may_pull_from_rx_block(pbd);
972 it = (it + 1) % rx_ring.layout3.tp_block_nr;
974 if (unlikely(sigint == 1))
975 break;
978 poll(&rx_poll, 1, -1);
981 bug_on(gettimeofday(&end, NULL));
982 timersub(&end, &start, &diff);
984 if (!(ctx->dump_dir && ctx->print_mode == PRINT_NONE)) {
985 sock_print_net_stats(sock);
987 printf("\r%12lu sec, %lu usec in total\n",
988 diff.tv_sec, diff.tv_usec);
989 } else {
990 printf("\n\n");
991 fflush(stdout);
994 bpf_release(&bpf_ops);
995 dissector_cleanup_all();
996 destroy_rx_ring(sock, &rx_ring);
998 if (ctx->promiscuous)
999 leave_promiscuous_mode(ctx->device_in, ifflags);
1001 if (ctx->rfraw)
1002 leave_rfmon_mac80211(ctx->device_trans, ctx->device_in);
1004 if (dump_to_pcap(ctx)) {
1005 if (ctx->dump_dir)
1006 finish_multi_pcap_file(ctx, fd);
1007 else
1008 finish_single_pcap_file(ctx, fd);
1011 close(sock);
1014 static void __noreturn help(void)
1016 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING);
1017 puts("http://www.netsniff-ng.org\n\n"
1018 "Usage: netsniff-ng [options] [filter-expression]\n"
1019 "Options:\n"
1020 " -i|-d|--dev|--in <dev|pcap|-> Input source as netdev, pcap or pcap stdin\n"
1021 " -o|--out <dev|pcap|dir|cfg|-> Output sink as netdev, pcap, directory, trafgen, or stdout\n"
1022 " -f|--filter <bpf-file|expr> Use BPF filter file from bpfc or tcpdump-like expression\n"
1023 " -t|--type <type> Filter for: host|broadcast|multicast|others|outgoing\n"
1024 " -F|--interval <size|time> Dump interval if -o is a dir: <num>KiB/MiB/GiB/s/sec/min/hrs\n"
1025 " -R|--rfraw Capture or inject raw 802.11 frames\n"
1026 " -n|--num <0|uint> Number of packets until exit (def: 0)\n"
1027 " -P|--prefix <name> Prefix for pcaps stored in directory\n"
1028 " -T|--magic <pcap-magic> Pcap magic number/pcap format to store, see -D\n"
1029 " -D|--dump-pcap-types Dump pcap types and magic numbers and quit\n"
1030 " -B|--dump-bpf Dump generated BPF assembly\n"
1031 " -r|--rand Randomize packet forwarding order (dev->dev)\n"
1032 " -M|--no-promisc No promiscuous mode for netdev\n"
1033 " -A|--no-sock-mem Don't tune core socket memory\n"
1034 " -m|--mmap Mmap(2) pcap file i.e., for replaying pcaps\n"
1035 " -G|--sg Scatter/gather pcap file I/O\n"
1036 " -c|--clrw Use slower read(2)/write(2) I/O\n"
1037 " -S|--ring-size <size> Specify ring size to: <num>KiB/MiB/GiB\n"
1038 " -k|--kernel-pull <uint> Kernel pull from user interval in us (def: 10us)\n"
1039 " -J|--jumbo-support Support replay/fwd 64KB Super Jumbo Frames (def: 2048B)\n"
1040 " -b|--bind-cpu <cpu> Bind to specific CPU\n"
1041 " -u|--user <userid> Drop privileges and change to userid\n"
1042 " -g|--group <groupid> Drop privileges and change to groupid\n"
1043 " -H|--prio-high Make this high priority process\n"
1044 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
1045 " -s|--silent Do not print captured packets\n"
1046 " -q|--less Print less-verbose packet information\n"
1047 " -X|--hex Print packet data in hex format\n"
1048 " -l|--ascii Print human-readable packet data\n"
1049 " -U|--update Update GeoIP databases\n"
1050 " -V|--verbose Be more verbose\n"
1051 " -v|--version Show version and exit\n"
1052 " -h|--help Guess what?!\n\n"
1053 "Examples:\n"
1054 " netsniff-ng --in eth0 --out dump.pcap -s -T 0xa1b2c3d4 --b 0 tcp or udp\n"
1055 " netsniff-ng --in wlan0 --rfraw --out dump.pcap --silent --bind-cpu 0\n"
1056 " netsniff-ng --in dump.pcap --mmap --out eth0 -k1000 --silent --bind-cpu 0\n"
1057 " netsniff-ng --in dump.pcap --out dump.cfg --silent --bind-cpu 0\n"
1058 " netsniff-ng --in eth0 --out eth1 --silent --bind-cpu 0 -J --type host\n"
1059 " netsniff-ng --in eth1 --out /opt/probe/ -s -m --interval 100MiB -b 0\n"
1060 " netsniff-ng --in vlan0 --out dump.pcap -c -u `id -u bob` -g `id -g bob`\n"
1061 " netsniff-ng --in any --filter http.bpf --jumbo-support --ascii -V\n\n"
1062 "Note:\n"
1063 " For introducing bit errors, delays with random variation and more\n"
1064 " while replaying pcaps, make use of tc(8) with its disciplines (e.g. netem).\n\n"
1065 "Please report bugs to <bugs@netsniff-ng.org>\n"
1066 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1067 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1068 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1069 "Swiss federal institute of technology (ETH Zurich)\n"
1070 "License: GNU GPL version 2.0\n"
1071 "This is free software: you are free to change and redistribute it.\n"
1072 "There is NO WARRANTY, to the extent permitted by law.\n");
1073 die();
1076 static void __noreturn version(void)
1078 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_LONG);
1079 puts("http://www.netsniff-ng.org\n\n"
1080 "Please report bugs to <bugs@netsniff-ng.org>\n"
1081 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1082 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1083 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1084 "Swiss federal institute of technology (ETH Zurich)\n"
1085 "License: GNU GPL version 2.0\n"
1086 "This is free software: you are free to change and redistribute it.\n"
1087 "There is NO WARRANTY, to the extent permitted by law.\n");
1088 die();
1091 int main(int argc, char **argv)
1093 char *ptr;
1094 int c, i, j, cpu_tmp, opt_index, ops_touched = 0, vals[4] = {0};
1095 bool prio_high = false, setsockmem = true;
1096 void (*main_loop)(struct ctx *ctx) = NULL;
1097 struct ctx ctx = {
1098 .link_type = LINKTYPE_EN10MB,
1099 .print_mode = PRINT_NORM,
1100 .cpu = -1,
1101 .packet_type = -1,
1102 .promiscuous = true,
1103 .randomize = false,
1104 .pcap = PCAP_OPS_SG,
1105 .dump_interval = 60,
1106 .dump_mode = DUMP_INTERVAL_TIME,
1107 .uid = getuid(),
1108 .gid = getgid(),
1109 .magic = ORIGINAL_TCPDUMP_MAGIC,
1112 srand(time(NULL));
1114 while ((c = getopt_long(argc, argv, short_options, long_options,
1115 &opt_index)) != EOF) {
1116 switch (c) {
1117 case 'd':
1118 case 'i':
1119 ctx.device_in = xstrdup(optarg);
1120 break;
1121 case 'o':
1122 ctx.device_out = xstrdup(optarg);
1123 break;
1124 case 'P':
1125 ctx.prefix = xstrdup(optarg);
1126 break;
1127 case 'R':
1128 ctx.link_type = LINKTYPE_IEEE802_11;
1129 ctx.rfraw = 1;
1130 break;
1131 case 'r':
1132 ctx.randomize = true;
1133 break;
1134 case 'J':
1135 ctx.jumbo = true;
1136 break;
1137 case 'T':
1138 ctx.magic = (uint32_t) strtoul(optarg, NULL, 0);
1139 pcap_check_magic(ctx.magic);
1140 break;
1141 case 'f':
1142 ctx.filter = xstrdup(optarg);
1143 break;
1144 case 'M':
1145 ctx.promiscuous = false;
1146 break;
1147 case 'A':
1148 setsockmem = false;
1149 break;
1150 case 'u':
1151 ctx.uid = strtoul(optarg, NULL, 0);
1152 ctx.enforce = true;
1153 break;
1154 case 'g':
1155 ctx.gid = strtoul(optarg, NULL, 0);
1156 ctx.enforce = true;
1157 break;
1158 case 't':
1159 if (!strncmp(optarg, "host", strlen("host")))
1160 ctx.packet_type = PACKET_HOST;
1161 else if (!strncmp(optarg, "broadcast", strlen("broadcast")))
1162 ctx.packet_type = PACKET_BROADCAST;
1163 else if (!strncmp(optarg, "multicast", strlen("multicast")))
1164 ctx.packet_type = PACKET_MULTICAST;
1165 else if (!strncmp(optarg, "others", strlen("others")))
1166 ctx.packet_type = PACKET_OTHERHOST;
1167 else if (!strncmp(optarg, "outgoing", strlen("outgoing")))
1168 ctx.packet_type = PACKET_OUTGOING;
1169 else
1170 ctx.packet_type = -1;
1171 break;
1172 case 'S':
1173 ptr = optarg;
1174 ctx.reserve_size = 0;
1176 for (j = i = strlen(optarg); i > 0; --i) {
1177 if (!isdigit(optarg[j - i]))
1178 break;
1179 ptr++;
1182 if (!strncmp(ptr, "KiB", strlen("KiB")))
1183 ctx.reserve_size = 1 << 10;
1184 else if (!strncmp(ptr, "MiB", strlen("MiB")))
1185 ctx.reserve_size = 1 << 20;
1186 else if (!strncmp(ptr, "GiB", strlen("GiB")))
1187 ctx.reserve_size = 1 << 30;
1188 else
1189 panic("Syntax error in ring size param!\n");
1190 *ptr = 0;
1192 ctx.reserve_size *= strtol(optarg, NULL, 0);
1193 break;
1194 case 'b':
1195 cpu_tmp = strtol(optarg, NULL, 0);
1197 cpu_affinity(cpu_tmp);
1198 if (ctx.cpu != -2)
1199 ctx.cpu = cpu_tmp;
1200 break;
1201 case 'H':
1202 prio_high = true;
1203 break;
1204 case 'c':
1205 ctx.pcap = PCAP_OPS_RW;
1206 ops_touched = 1;
1207 break;
1208 case 'm':
1209 ctx.pcap = PCAP_OPS_MM;
1210 ops_touched = 1;
1211 break;
1212 case 'G':
1213 ctx.pcap = PCAP_OPS_SG;
1214 ops_touched = 1;
1215 break;
1216 case 'Q':
1217 ctx.cpu = -2;
1218 break;
1219 case 's':
1220 ctx.print_mode = PRINT_NONE;
1221 break;
1222 case 'q':
1223 ctx.print_mode = PRINT_LESS;
1224 break;
1225 case 'X':
1226 ctx.print_mode =
1227 (ctx.print_mode == PRINT_ASCII) ?
1228 PRINT_HEX_ASCII : PRINT_HEX;
1229 break;
1230 case 'l':
1231 ctx.print_mode =
1232 (ctx.print_mode == PRINT_HEX) ?
1233 PRINT_HEX_ASCII : PRINT_ASCII;
1234 break;
1235 case 'k':
1236 ctx.kpull = strtol(optarg, NULL, 0);
1237 break;
1238 case 'n':
1239 frame_count_max = strtol(optarg, NULL, 0);
1240 break;
1241 case 'F':
1242 ptr = optarg;
1243 ctx.dump_interval = 0;
1245 for (j = i = strlen(optarg); i > 0; --i) {
1246 if (!isdigit(optarg[j - i]))
1247 break;
1248 ptr++;
1251 if (!strncmp(ptr, "KiB", strlen("KiB"))) {
1252 ctx.dump_interval = 1 << 10;
1253 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1254 } else if (!strncmp(ptr, "MiB", strlen("MiB"))) {
1255 ctx.dump_interval = 1 << 20;
1256 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1257 } else if (!strncmp(ptr, "GiB", strlen("GiB"))) {
1258 ctx.dump_interval = 1 << 30;
1259 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1260 } else if (!strncmp(ptr, "sec", strlen("sec"))) {
1261 ctx.dump_interval = 1;
1262 ctx.dump_mode = DUMP_INTERVAL_TIME;
1263 } else if (!strncmp(ptr, "min", strlen("min"))) {
1264 ctx.dump_interval = 60;
1265 ctx.dump_mode = DUMP_INTERVAL_TIME;
1266 } else if (!strncmp(ptr, "hrs", strlen("hrs"))) {
1267 ctx.dump_interval = 60 * 60;
1268 ctx.dump_mode = DUMP_INTERVAL_TIME;
1269 } else if (!strncmp(ptr, "s", strlen("s"))) {
1270 ctx.dump_interval = 1;
1271 ctx.dump_mode = DUMP_INTERVAL_TIME;
1272 } else {
1273 panic("Syntax error in time/size param!\n");
1276 *ptr = 0;
1277 ctx.dump_interval *= strtol(optarg, NULL, 0);
1278 break;
1279 case 'V':
1280 ctx.verbose = 1;
1281 break;
1282 case 'B':
1283 ctx.dump_bpf = true;
1284 break;
1285 case 'D':
1286 pcap_dump_type_features();
1287 die();
1288 break;
1289 case 'U':
1290 update_geoip();
1291 die();
1292 break;
1293 case 'v':
1294 version();
1295 break;
1296 case 'h':
1297 help();
1298 break;
1299 case '?':
1300 switch (optopt) {
1301 case 'd':
1302 case 'i':
1303 case 'o':
1304 case 'f':
1305 case 't':
1306 case 'P':
1307 case 'F':
1308 case 'n':
1309 case 'S':
1310 case 'b':
1311 case 'k':
1312 case 'T':
1313 case 'u':
1314 case 'g':
1315 case 'e':
1316 panic("Option -%c requires an argument!\n",
1317 optopt);
1318 default:
1319 if (isprint(optopt))
1320 printf("Unknown option character `0x%X\'!\n", optopt);
1321 die();
1323 default:
1324 break;
1328 if (!ctx.filter && optind != argc) {
1329 int ret;
1330 off_t offset = 0;
1332 for (i = optind; i < argc; ++i) {
1333 size_t alen = strlen(argv[i]) + 2;
1334 size_t flen = ctx.filter ? strlen(ctx.filter) : 0;
1336 ctx.filter = xrealloc(ctx.filter, 1, flen + alen);
1337 ret = slprintf(ctx.filter + offset, strlen(argv[i]) + 2, "%s ", argv[i]);
1338 if (ret < 0)
1339 panic("Cannot concatenate filter string!\n");
1340 else
1341 offset += ret;
1345 if (!ctx.device_in)
1346 ctx.device_in = xstrdup("any");
1348 register_signal(SIGINT, signal_handler);
1349 register_signal(SIGHUP, signal_handler);
1351 tprintf_init();
1353 if (prio_high) {
1354 set_proc_prio(get_default_proc_prio());
1355 set_sched_status(get_default_sched_policy(), get_default_sched_prio());
1358 if (ctx.device_in && (device_mtu(ctx.device_in) ||
1359 !strncmp("any", ctx.device_in, strlen(ctx.device_in)))) {
1360 if (!ctx.device_out) {
1361 ctx.dump = 0;
1362 main_loop = recv_only_or_dump;
1363 } else if (device_mtu(ctx.device_out)) {
1364 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1365 main_loop = receive_to_xmit;
1366 } else {
1367 ctx.dump = 1;
1368 register_signal_f(SIGALRM, timer_next_dump, SA_SIGINFO);
1369 main_loop = recv_only_or_dump;
1370 if (!ops_touched)
1371 ctx.pcap = PCAP_OPS_SG;
1373 } else {
1374 if (ctx.device_out && device_mtu(ctx.device_out)) {
1375 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1376 main_loop = pcap_to_xmit;
1377 if (!ops_touched)
1378 ctx.pcap = PCAP_OPS_MM;
1379 } else {
1380 main_loop = read_pcap;
1381 if (!ops_touched)
1382 ctx.pcap = PCAP_OPS_SG;
1386 bug_on(!main_loop);
1388 init_geoip(0);
1389 if (setsockmem)
1390 set_system_socket_memory(vals, array_size(vals));
1391 if (!ctx.enforce)
1392 xlockme();
1394 main_loop(&ctx);
1396 if (!ctx.enforce)
1397 xunlockme();
1398 if (setsockmem)
1399 reset_system_socket_memory(vals, array_size(vals));
1400 destroy_geoip();
1402 tprintf_cleanup();
1404 free(ctx.device_in);
1405 free(ctx.device_out);
1406 free(ctx.device_trans);
1407 free(ctx.prefix);
1409 return 0;