proc: move out process specific management functions
[netsniff-ng.git] / netsniff-ng.c
blob234d781725f8a9b5eb8bcc9fe8ac0cc6dba2bb51
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 "promisc.h"
31 #include "built_in.h"
32 #include "pcap_io.h"
33 #include "privs.h"
34 #include "proc.h"
35 #include "bpf.h"
36 #include "ioops.h"
37 #include "die.h"
38 #include "irq.h"
39 #include "str.h"
40 #include "geoip.h"
41 #include "lockme.h"
42 #include "tprintf.h"
43 #include "dissector.h"
44 #include "xmalloc.h"
46 extern int set_sockopt_hwtimestamp(int sock, const char *dev);
48 enum dump_mode {
49 DUMP_INTERVAL_TIME,
50 DUMP_INTERVAL_SIZE,
53 struct ctx {
54 char *device_in, *device_out, *device_trans, *filter, *prefix;
55 int cpu, rfraw, dump, print_mode, dump_dir, packet_type, verbose;
56 unsigned long kpull, dump_interval, reserve_size, tx_bytes, tx_packets;
57 bool randomize, promiscuous, enforce, jumbo, dump_bpf;
58 enum pcap_ops_groups pcap; enum dump_mode dump_mode;
59 uid_t uid; gid_t gid; uint32_t link_type, magic;
62 static volatile sig_atomic_t sigint = 0;
63 static volatile bool next_dump = false;
65 static const char *short_options = "d:i:o:rf:MJt:S:k:n:b:HQmcsqXlvhF:RGAP:Vu:g:T:DB";
66 static const struct option long_options[] = {
67 {"dev", required_argument, NULL, 'd'},
68 {"in", required_argument, NULL, 'i'},
69 {"out", required_argument, NULL, 'o'},
70 {"filter", required_argument, NULL, 'f'},
71 {"num", required_argument, NULL, 'n'},
72 {"type", required_argument, NULL, 't'},
73 {"interval", required_argument, NULL, 'F'},
74 {"ring-size", required_argument, NULL, 'S'},
75 {"kernel-pull", required_argument, NULL, 'k'},
76 {"bind-cpu", required_argument, NULL, 'b'},
77 {"prefix", required_argument, NULL, 'P'},
78 {"user", required_argument, NULL, 'u'},
79 {"group", required_argument, NULL, 'g'},
80 {"magic", required_argument, NULL, 'T'},
81 {"rand", no_argument, NULL, 'r'},
82 {"rfraw", no_argument, NULL, 'R'},
83 {"mmap", no_argument, NULL, 'm'},
84 {"sg", no_argument, NULL, 'G'},
85 {"clrw", no_argument, NULL, 'c'},
86 {"jumbo-support", no_argument, NULL, 'J'},
87 {"no-promisc", no_argument, NULL, 'M'},
88 {"prio-high", no_argument, NULL, 'H'},
89 {"notouch-irq", no_argument, NULL, 'Q'},
90 {"dump-pcap-types", no_argument, NULL, 'D'},
91 {"dump-bpf", no_argument, NULL, 'B'},
92 {"silent", no_argument, NULL, 's'},
93 {"less", no_argument, NULL, 'q'},
94 {"hex", no_argument, NULL, 'X'},
95 {"ascii", no_argument, NULL, 'l'},
96 {"no-sock-mem", no_argument, NULL, 'A'},
97 {"update", no_argument, NULL, 'U'},
98 {"verbose", no_argument, NULL, 'V'},
99 {"version", no_argument, NULL, 'v'},
100 {"help", no_argument, NULL, 'h'},
101 {NULL, 0, NULL, 0}
104 static int tx_sock;
105 static struct itimerval itimer;
106 static unsigned long frame_count_max = 0, interval = TX_KERNEL_PULL_INT;
108 #define __pcap_io pcap_ops[ctx->pcap]
110 static void signal_handler(int number)
112 switch (number) {
113 case SIGINT:
114 sigint = 1;
115 case SIGHUP:
116 default:
117 break;
121 static void timer_elapsed(int unused __maybe_unused)
123 int ret;
125 set_itimer_interval_value(&itimer, 0, interval);
127 ret = pull_and_flush_tx_ring(tx_sock);
128 if (unlikely(ret < 0)) {
129 /* We could hit EBADF if the socket has been closed before
130 * the timer was triggered.
132 if (errno != EBADF && errno != ENOBUFS)
133 panic("Flushing TX_RING failed: %s!\n", strerror(errno));
136 setitimer(ITIMER_REAL, &itimer, NULL);
139 static void timer_purge(void)
141 int ret;
143 ret = pull_and_flush_tx_ring_wait(tx_sock);
144 if (unlikely(ret < 0)) {
145 if (errno != EBADF && errno != ENOBUFS)
146 panic("Flushing TX_RING failed: %s!\n", strerror(errno));
149 set_itimer_interval_value(&itimer, 0, 0);
150 setitimer(ITIMER_REAL, &itimer, NULL);
153 static void timer_next_dump(int unused __maybe_unused)
155 set_itimer_interval_value(&itimer, interval, 0);
156 next_dump = true;
157 setitimer(ITIMER_REAL, &itimer, NULL);
160 static inline bool dump_to_pcap(struct ctx *ctx)
162 return ctx->dump;
165 static void pcap_to_xmit(struct ctx *ctx)
167 __label__ out;
168 uint8_t *out = NULL;
169 int irq, ifindex, fd = 0, ret;
170 unsigned int size, it = 0;
171 unsigned long trunced = 0;
172 struct ring tx_ring;
173 struct frame_map *hdr;
174 struct sock_fprog bpf_ops;
175 struct timeval start, end, diff;
176 pcap_pkthdr_t phdr;
178 if (!device_up_and_running(ctx->device_out) && !ctx->rfraw)
179 panic("Device not up and running!\n");
181 bug_on(!__pcap_io);
183 tx_sock = pf_socket();
185 if (!strncmp("-", ctx->device_in, strlen("-"))) {
186 fd = dup(fileno(stdin));
187 close(fileno(stdin));
188 if (ctx->pcap == PCAP_OPS_MM)
189 ctx->pcap = PCAP_OPS_SG;
190 } else {
191 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
194 if (__pcap_io->init_once_pcap)
195 __pcap_io->init_once_pcap();
197 ret = __pcap_io->pull_fhdr_pcap(fd, &ctx->magic, &ctx->link_type);
198 if (ret)
199 panic("Error reading pcap header!\n");
201 if (__pcap_io->prepare_access_pcap) {
202 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_RD, ctx->jumbo);
203 if (ret)
204 panic("Error prepare reading pcap!\n");
207 fmemset(&tx_ring, 0, sizeof(tx_ring));
208 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
210 if (ctx->rfraw) {
211 ctx->device_trans = xstrdup(ctx->device_out);
212 xfree(ctx->device_out);
214 enter_rfmon_mac80211(ctx->device_trans, &ctx->device_out);
215 if (ctx->link_type != LINKTYPE_IEEE802_11)
216 panic("Wrong linktype of pcap!\n");
219 ifindex = device_ifindex(ctx->device_out);
221 size = ring_size(ctx->device_out, ctx->reserve_size);
223 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
224 if (ctx->dump_bpf)
225 bpf_dump_all(&bpf_ops);
227 set_packet_loss_discard(tx_sock);
229 setup_tx_ring_layout(tx_sock, &tx_ring, size, ctx->jumbo);
230 create_tx_ring(tx_sock, &tx_ring, ctx->verbose);
231 mmap_tx_ring(tx_sock, &tx_ring);
232 alloc_tx_ring_frames(tx_sock, &tx_ring);
233 bind_tx_ring(tx_sock, &tx_ring, ifindex);
235 dissector_init_all(ctx->print_mode);
237 if (ctx->cpu >= 0 && ifindex > 0) {
238 irq = device_irq_number(ctx->device_out);
239 device_set_irq_affinity(irq, ctx->cpu);
241 if (ctx->verbose)
242 printf("IRQ: %s:%d > CPU%d\n",
243 ctx->device_out, irq, ctx->cpu);
246 if (ctx->kpull)
247 interval = ctx->kpull;
249 set_itimer_interval_value(&itimer, 0, interval);
250 setitimer(ITIMER_REAL, &itimer, NULL);
252 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
254 printf("Running! Hang up with ^C!\n\n");
255 fflush(stdout);
257 bug_on(gettimeofday(&start, NULL));
259 while (likely(sigint == 0)) {
260 while (user_may_pull_from_tx(tx_ring.frames[it].iov_base)) {
261 hdr = tx_ring.frames[it].iov_base;
262 out = ((uint8_t *) hdr) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
264 do {
265 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic, out,
266 ring_frame_size(&tx_ring));
267 if (unlikely(ret <= 0))
268 goto out;
270 if (ring_frame_size(&tx_ring) <
271 pcap_get_length(&phdr, ctx->magic)) {
272 pcap_set_length(&phdr, ctx->magic,
273 ring_frame_size(&tx_ring));
274 trunced++;
276 } while (ctx->filter &&
277 !bpf_run_filter(&bpf_ops, out,
278 pcap_get_length(&phdr, ctx->magic)));
280 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &hdr->tp_h, &hdr->s_ll);
282 ctx->tx_bytes += hdr->tp_h.tp_len;;
283 ctx->tx_packets++;
285 show_frame_hdr(hdr, ctx->print_mode);
287 dissector_entry_point(out, hdr->tp_h.tp_snaplen,
288 ctx->link_type, ctx->print_mode);
290 kernel_may_pull_from_tx(&hdr->tp_h);
292 it++;
293 if (it >= tx_ring.layout.tp_frame_nr)
294 it = 0;
296 if (unlikely(sigint == 1))
297 break;
299 if (frame_count_max != 0) {
300 if (ctx->tx_packets >= frame_count_max) {
301 sigint = 1;
302 break;
308 out:
310 bug_on(gettimeofday(&end, NULL));
311 timersub(&end, &start, &diff);
313 timer_purge();
315 bpf_release(&bpf_ops);
317 dissector_cleanup_all();
318 destroy_tx_ring(tx_sock, &tx_ring);
320 if (ctx->rfraw)
321 leave_rfmon_mac80211(ctx->device_trans, ctx->device_out);
323 if (__pcap_io->prepare_close_pcap)
324 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
326 if (!strncmp("-", ctx->device_in, strlen("-")))
327 dup2(fd, fileno(stdin));
328 close(fd);
330 close(tx_sock);
332 fflush(stdout);
333 printf("\n");
334 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
335 printf("\r%12lu packets truncated in file\n", trunced);
336 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
337 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
340 static void receive_to_xmit(struct ctx *ctx)
342 short ifflags = 0;
343 uint8_t *in, *out;
344 int rx_sock, ifindex_in, ifindex_out;
345 unsigned int size_in, size_out, it_in = 0, it_out = 0;
346 unsigned long frame_count = 0;
347 struct frame_map *hdr_in, *hdr_out;
348 struct ring tx_ring, rx_ring;
349 struct pollfd rx_poll;
350 struct sock_fprog bpf_ops;
352 if (!strncmp(ctx->device_in, ctx->device_out, IFNAMSIZ))
353 panic("Ingress/egress devices must be different!\n");
354 if (!device_up_and_running(ctx->device_out))
355 panic("Egress device not up and running!\n");
357 rx_sock = pf_socket();
358 tx_sock = pf_socket();
360 fmemset(&tx_ring, 0, sizeof(tx_ring));
361 fmemset(&rx_ring, 0, sizeof(rx_ring));
362 fmemset(&rx_poll, 0, sizeof(rx_poll));
363 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
365 ifindex_in = device_ifindex(ctx->device_in);
366 ifindex_out = device_ifindex(ctx->device_out);
368 size_in = ring_size(ctx->device_in, ctx->reserve_size);
369 size_out = ring_size(ctx->device_out, ctx->reserve_size);
371 enable_kernel_bpf_jit_compiler();
373 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
374 if (ctx->dump_bpf)
375 bpf_dump_all(&bpf_ops);
376 bpf_attach_to_sock(rx_sock, &bpf_ops);
378 setup_rx_ring_layout(rx_sock, &rx_ring, size_in, ctx->jumbo, false);
379 create_rx_ring(rx_sock, &rx_ring, ctx->verbose);
380 mmap_rx_ring(rx_sock, &rx_ring);
381 alloc_rx_ring_frames(rx_sock, &rx_ring);
382 bind_rx_ring(rx_sock, &rx_ring, ifindex_in);
383 prepare_polling(rx_sock, &rx_poll);
385 set_packet_loss_discard(tx_sock);
386 setup_tx_ring_layout(tx_sock, &tx_ring, size_out, ctx->jumbo);
387 create_tx_ring(tx_sock, &tx_ring, ctx->verbose);
388 mmap_tx_ring(tx_sock, &tx_ring);
389 alloc_tx_ring_frames(tx_sock, &tx_ring);
390 bind_tx_ring(tx_sock, &tx_ring, ifindex_out);
392 dissector_init_all(ctx->print_mode);
394 if (ctx->promiscuous)
395 ifflags = enter_promiscuous_mode(ctx->device_in);
397 if (ctx->kpull)
398 interval = ctx->kpull;
400 set_itimer_interval_value(&itimer, 0, interval);
401 setitimer(ITIMER_REAL, &itimer, NULL);
403 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
405 printf("Running! Hang up with ^C!\n\n");
406 fflush(stdout);
408 while (likely(sigint == 0)) {
409 while (user_may_pull_from_rx(rx_ring.frames[it_in].iov_base)) {
410 __label__ next;
412 hdr_in = rx_ring.frames[it_in].iov_base;
413 in = ((uint8_t *) hdr_in) + hdr_in->tp_h.tp_mac;
415 frame_count++;
417 if (ctx->packet_type != -1)
418 if (ctx->packet_type != hdr_in->s_ll.sll_pkttype)
419 goto next;
421 hdr_out = tx_ring.frames[it_out].iov_base;
422 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
424 for (; !user_may_pull_from_tx(tx_ring.frames[it_out].iov_base) &&
425 likely(!sigint);) {
426 if (ctx->randomize)
427 next_rnd_slot(&it_out, &tx_ring);
428 else {
429 it_out++;
430 if (it_out >= tx_ring.layout.tp_frame_nr)
431 it_out = 0;
434 hdr_out = tx_ring.frames[it_out].iov_base;
435 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
438 tpacket_hdr_clone(&hdr_out->tp_h, &hdr_in->tp_h);
439 fmemcpy(out, in, hdr_in->tp_h.tp_len);
441 kernel_may_pull_from_tx(&hdr_out->tp_h);
442 if (ctx->randomize)
443 next_rnd_slot(&it_out, &tx_ring);
444 else {
445 it_out++;
446 if (it_out >= tx_ring.layout.tp_frame_nr)
447 it_out = 0;
450 show_frame_hdr(hdr_in, ctx->print_mode);
452 dissector_entry_point(in, hdr_in->tp_h.tp_snaplen,
453 ctx->link_type, ctx->print_mode);
455 if (frame_count_max != 0) {
456 if (frame_count >= frame_count_max) {
457 sigint = 1;
458 break;
462 next:
464 kernel_may_pull_from_rx(&hdr_in->tp_h);
466 it_in++;
467 if (it_in >= rx_ring.layout.tp_frame_nr)
468 it_in = 0;
470 if (unlikely(sigint == 1))
471 goto out;
474 poll(&rx_poll, 1, -1);
477 out:
479 timer_purge();
481 sock_rx_net_stats(rx_sock);
483 bpf_release(&bpf_ops);
485 dissector_cleanup_all();
487 destroy_tx_ring(tx_sock, &tx_ring);
488 destroy_rx_ring(rx_sock, &rx_ring);
490 if (ctx->promiscuous)
491 leave_promiscuous_mode(ctx->device_in, ifflags);
493 close(tx_sock);
494 close(rx_sock);
497 static void translate_pcap_to_txf(int fdo, uint8_t *out, size_t len)
499 size_t bytes_done = 0;
500 char bout[80];
502 slprintf(bout, sizeof(bout), "{\n ");
503 write_or_die(fdo, bout, strlen(bout));
505 while (bytes_done < len) {
506 slprintf(bout, sizeof(bout), "0x%02x, ", out[bytes_done]);
507 write_or_die(fdo, bout, strlen(bout));
509 bytes_done++;
511 if (bytes_done % 10 == 0) {
512 slprintf(bout, sizeof(bout), "\n");
513 write_or_die(fdo, bout, strlen(bout));
515 if (bytes_done < len) {
516 slprintf(bout, sizeof(bout), " ");
517 write_or_die(fdo, bout, strlen(bout));
521 if (bytes_done % 10 != 0) {
522 slprintf(bout, sizeof(bout), "\n");
523 write_or_die(fdo, bout, strlen(bout));
526 slprintf(bout, sizeof(bout), "}\n\n");
527 write_or_die(fdo, bout, strlen(bout));
530 static void read_pcap(struct ctx *ctx)
532 __label__ out;
533 uint8_t *out;
534 int ret, fd, fdo = 0;
535 unsigned long trunced = 0;
536 size_t out_len;
537 pcap_pkthdr_t phdr;
538 struct sock_fprog bpf_ops;
539 struct frame_map fm;
540 struct timeval start, end, diff;
541 struct sockaddr_ll sll;
543 bug_on(!__pcap_io);
545 if (!strncmp("-", ctx->device_in, strlen("-"))) {
546 fd = dup(fileno(stdin));
547 close(fileno(stdin));
548 if (ctx->pcap == PCAP_OPS_MM)
549 ctx->pcap = PCAP_OPS_SG;
550 } else {
551 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
554 if (__pcap_io->init_once_pcap)
555 __pcap_io->init_once_pcap();
557 ret = __pcap_io->pull_fhdr_pcap(fd, &ctx->magic, &ctx->link_type);
558 if (ret)
559 panic("Error reading pcap header!\n");
561 if (__pcap_io->prepare_access_pcap) {
562 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_RD, ctx->jumbo);
563 if (ret)
564 panic("Error prepare reading pcap!\n");
567 fmemset(&fm, 0, sizeof(fm));
568 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
570 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
571 if (ctx->dump_bpf)
572 bpf_dump_all(&bpf_ops);
574 dissector_init_all(ctx->print_mode);
576 out_len = round_up(1024 * 1024, PAGE_SIZE);
577 out = xmalloc_aligned(out_len, CO_CACHE_LINE_SIZE);
579 if (ctx->device_out) {
580 if (!strncmp("-", ctx->device_out, strlen("-"))) {
581 fdo = dup(fileno(stdout));
582 close(fileno(stdout));
583 } else {
584 fdo = open_or_die_m(ctx->device_out, O_RDWR | O_CREAT |
585 O_TRUNC | O_LARGEFILE, DEFFILEMODE);
589 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
591 printf("Running! Hang up with ^C!\n\n");
592 fflush(stdout);
594 bug_on(gettimeofday(&start, NULL));
596 while (likely(sigint == 0)) {
597 do {
598 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic,
599 out, out_len);
600 if (unlikely(ret < 0))
601 goto out;
603 if (unlikely(pcap_get_length(&phdr, ctx->magic) == 0)) {
604 trunced++;
605 continue;
608 if (unlikely(pcap_get_length(&phdr, ctx->magic) > out_len)) {
609 pcap_set_length(&phdr, ctx->magic, out_len);
610 trunced++;
612 } while (ctx->filter &&
613 !bpf_run_filter(&bpf_ops, out,
614 pcap_get_length(&phdr, ctx->magic)));
616 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &fm.tp_h, &sll);
618 ctx->tx_bytes += fm.tp_h.tp_len;
619 ctx->tx_packets++;
621 show_frame_hdr(&fm, ctx->print_mode);
623 dissector_entry_point(out, fm.tp_h.tp_snaplen,
624 ctx->link_type, ctx->print_mode);
626 if (ctx->device_out)
627 translate_pcap_to_txf(fdo, out, fm.tp_h.tp_snaplen);
629 if (frame_count_max != 0) {
630 if (ctx->tx_packets >= frame_count_max) {
631 sigint = 1;
632 break;
637 out:
639 bug_on(gettimeofday(&end, NULL));
640 timersub(&end, &start, &diff);
642 bpf_release(&bpf_ops);
644 dissector_cleanup_all();
646 if (__pcap_io->prepare_close_pcap)
647 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
649 xfree(out);
651 fflush(stdout);
652 printf("\n");
653 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
654 printf("\r%12lu packets truncated in file\n", trunced);
655 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
656 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
658 if (!strncmp("-", ctx->device_in, strlen("-")))
659 dup2(fd, fileno(stdin));
660 close(fd);
662 if (ctx->device_out) {
663 if (!strncmp("-", ctx->device_out, strlen("-")))
664 dup2(fdo, fileno(stdout));
665 close(fdo);
669 static void finish_multi_pcap_file(struct ctx *ctx, int fd)
671 __pcap_io->fsync_pcap(fd);
673 if (__pcap_io->prepare_close_pcap)
674 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
676 close(fd);
678 fmemset(&itimer, 0, sizeof(itimer));
679 setitimer(ITIMER_REAL, &itimer, NULL);
682 static int next_multi_pcap_file(struct ctx *ctx, int fd)
684 int ret;
685 char fname[512];
687 __pcap_io->fsync_pcap(fd);
689 if (__pcap_io->prepare_close_pcap)
690 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
692 close(fd);
694 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
695 ctx->prefix ? : "dump-", time(0));
697 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
698 O_LARGEFILE, DEFFILEMODE);
700 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
701 if (ret)
702 panic("Error writing pcap header!\n");
704 if (__pcap_io->prepare_access_pcap) {
705 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, true);
706 if (ret)
707 panic("Error prepare writing pcap!\n");
710 return fd;
713 static int begin_multi_pcap_file(struct ctx *ctx)
715 int fd, ret;
716 char fname[256];
718 bug_on(!__pcap_io);
720 if (ctx->device_out[strlen(ctx->device_out) - 1] == '/')
721 ctx->device_out[strlen(ctx->device_out) - 1] = 0;
723 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
724 ctx->prefix ? : "dump-", time(0));
726 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
727 O_LARGEFILE, DEFFILEMODE);
729 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
730 if (ret)
731 panic("Error writing pcap header!\n");
733 if (__pcap_io->prepare_access_pcap) {
734 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, true);
735 if (ret)
736 panic("Error prepare writing pcap!\n");
739 if (ctx->dump_mode == DUMP_INTERVAL_TIME) {
740 interval = ctx->dump_interval;
742 set_itimer_interval_value(&itimer, interval, 0);
743 setitimer(ITIMER_REAL, &itimer, NULL);
744 } else {
745 interval = 0;
748 return fd;
751 static void finish_single_pcap_file(struct ctx *ctx, int fd)
753 __pcap_io->fsync_pcap(fd);
755 if (__pcap_io->prepare_close_pcap)
756 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
758 if (strncmp("-", ctx->device_out, strlen("-")))
759 close(fd);
760 else
761 dup2(fd, fileno(stdout));
764 static int begin_single_pcap_file(struct ctx *ctx)
766 int fd, ret;
768 bug_on(!__pcap_io);
770 if (!strncmp("-", ctx->device_out, strlen("-"))) {
771 fd = dup(fileno(stdout));
772 close(fileno(stdout));
773 if (ctx->pcap == PCAP_OPS_MM)
774 ctx->pcap = PCAP_OPS_SG;
775 } else {
776 fd = open_or_die_m(ctx->device_out,
777 O_RDWR | O_CREAT | O_TRUNC |
778 O_LARGEFILE, DEFFILEMODE);
781 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
782 if (ret)
783 panic("Error writing pcap header!\n");
785 if (__pcap_io->prepare_access_pcap) {
786 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, true);
787 if (ret)
788 panic("Error prepare writing pcap!\n");
791 return fd;
794 static void print_pcap_file_stats(int sock, struct ctx *ctx)
796 int ret;
797 struct tpacket_stats kstats;
798 socklen_t slen = sizeof(kstats);
800 fmemset(&kstats, 0, sizeof(kstats));
802 ret = getsockopt(sock, SOL_PACKET, PACKET_STATISTICS, &kstats, &slen);
803 if (unlikely(ret))
804 panic("Cannot get packet statistics!\n");
806 if (ctx->print_mode == PRINT_NONE) {
807 printf(".(+%u/-%u)", kstats.tp_packets - kstats.tp_drops,
808 kstats.tp_drops);
809 fflush(stdout);
813 static void walk_t3_block(struct block_desc *pbd, struct ctx *ctx,
814 int sock, int fd, unsigned long *frame_count)
816 uint8_t *packet;
817 int num_pkts = pbd->h1.num_pkts, i, ret;
818 struct tpacket3_hdr *hdr;
819 pcap_pkthdr_t phdr;
820 struct sockaddr_ll *sll;
822 hdr = (void *) ((uint8_t *) pbd + pbd->h1.offset_to_first_pkt);
823 sll = (void *) ((uint8_t *) hdr + TPACKET_ALIGN(sizeof(*hdr)));
825 for (i = 0; i < num_pkts && likely(sigint == 0); ++i) {
826 __label__ next;
827 packet = ((uint8_t *) hdr + hdr->tp_mac);
829 if (ctx->packet_type != -1)
830 if (ctx->packet_type != sll->sll_pkttype)
831 goto next;
833 (*frame_count)++;
835 if (dump_to_pcap(ctx)) {
836 tpacket3_hdr_to_pcap_pkthdr(hdr, sll, &phdr, ctx->magic);
838 ret = __pcap_io->write_pcap(fd, &phdr, ctx->magic, packet,
839 pcap_get_length(&phdr, ctx->magic));
840 if (unlikely(ret != pcap_get_total_length(&phdr, ctx->magic)))
841 panic("Write error to pcap!\n");
844 __show_frame_hdr(sll, hdr, ctx->print_mode, true);
846 dissector_entry_point(packet, hdr->tp_snaplen, ctx->link_type,
847 ctx->print_mode);
848 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 unsigned int size, it = 0;
885 struct ring rx_ring;
886 struct pollfd rx_poll;
887 struct sock_fprog bpf_ops;
888 struct timeval start, end, diff;
889 struct block_desc *pbd;
890 unsigned long frame_count = 0;
892 sock = pf_socket();
894 if (ctx->rfraw) {
895 ctx->device_trans = xstrdup(ctx->device_in);
896 xfree(ctx->device_in);
898 enter_rfmon_mac80211(ctx->device_trans, &ctx->device_in);
899 ctx->link_type = LINKTYPE_IEEE802_11;
902 fmemset(&rx_ring, 0, sizeof(rx_ring));
903 fmemset(&rx_poll, 0, sizeof(rx_poll));
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 set_sockopt_hwtimestamp(sock, ctx->device_in);
919 setup_rx_ring_layout(sock, &rx_ring, size, true, true);
920 create_rx_ring(sock, &rx_ring, ctx->verbose);
921 mmap_rx_ring(sock, &rx_ring);
922 alloc_rx_ring_frames(sock, &rx_ring);
923 bind_rx_ring(sock, &rx_ring, ifindex);
925 prepare_polling(sock, &rx_poll);
926 dissector_init_all(ctx->print_mode);
928 if (ctx->cpu >= 0 && ifindex > 0) {
929 irq = device_irq_number(ctx->device_in);
930 device_set_irq_affinity(irq, ctx->cpu);
932 if (ctx->verbose)
933 printf("IRQ: %s:%d > CPU%d\n",
934 ctx->device_in, irq, ctx->cpu);
937 if (ctx->promiscuous)
938 ifflags = enter_promiscuous_mode(ctx->device_in);
940 if (dump_to_pcap(ctx) && __pcap_io->init_once_pcap)
941 __pcap_io->init_once_pcap();
943 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
945 if (dump_to_pcap(ctx)) {
946 __label__ try_file;
947 struct stat stats;
949 fmemset(&stats, 0, sizeof(stats));
950 ret = stat(ctx->device_out, &stats);
951 if (ret < 0) {
952 ctx->dump_dir = 0;
953 goto try_file;
956 ctx->dump_dir = S_ISDIR(stats.st_mode);
957 if (ctx->dump_dir) {
958 fd = begin_multi_pcap_file(ctx);
959 } else {
960 try_file:
961 fd = begin_single_pcap_file(ctx);
965 printf("Running! Hang up with ^C!\n\n");
966 fflush(stdout);
968 bug_on(gettimeofday(&start, NULL));
970 while (likely(sigint == 0)) {
971 while (user_may_pull_from_rx_block((pbd = (void *)
972 rx_ring.frames[it].iov_base))) {
973 walk_t3_block(pbd, ctx, sock, fd, &frame_count);
975 kernel_may_pull_from_rx_block(pbd);
976 it = (it + 1) % rx_ring.layout3.tp_block_nr;
978 if (unlikely(sigint == 1))
979 break;
982 poll(&rx_poll, 1, -1);
985 bug_on(gettimeofday(&end, NULL));
986 timersub(&end, &start, &diff);
988 if (!(ctx->dump_dir && ctx->print_mode == PRINT_NONE)) {
989 sock_rx_net_stats(sock);
991 printf("\r%12lu sec, %lu usec in total\n",
992 diff.tv_sec, diff.tv_usec);
993 } else {
994 printf("\n\n");
995 fflush(stdout);
998 bpf_release(&bpf_ops);
999 dissector_cleanup_all();
1000 destroy_rx_ring(sock, &rx_ring);
1002 if (ctx->promiscuous)
1003 leave_promiscuous_mode(ctx->device_in, ifflags);
1005 if (ctx->rfraw)
1006 leave_rfmon_mac80211(ctx->device_trans, ctx->device_in);
1008 if (dump_to_pcap(ctx)) {
1009 if (ctx->dump_dir)
1010 finish_multi_pcap_file(ctx, fd);
1011 else
1012 finish_single_pcap_file(ctx, fd);
1015 close(sock);
1018 static void __noreturn help(void)
1020 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING);
1021 puts("http://www.netsniff-ng.org\n\n"
1022 "Usage: netsniff-ng [options] [filter-expression]\n"
1023 "Options:\n"
1024 " -i|-d|--dev|--in <dev|pcap|-> Input source as netdev, pcap or pcap stdin\n"
1025 " -o|--out <dev|pcap|dir|cfg|-> Output sink as netdev, pcap, directory, trafgen, or stdout\n"
1026 " -f|--filter <bpf-file|expr> Use BPF filter file from bpfc or tcpdump-like expression\n"
1027 " -t|--type <type> Filter for: host|broadcast|multicast|others|outgoing\n"
1028 " -F|--interval <size|time> Dump interval if -o is a dir: <num>KiB/MiB/GiB/s/sec/min/hrs\n"
1029 " -R|--rfraw Capture or inject raw 802.11 frames\n"
1030 " -n|--num <0|uint> Number of packets until exit (def: 0)\n"
1031 " -P|--prefix <name> Prefix for pcaps stored in directory\n"
1032 " -T|--magic <pcap-magic> Pcap magic number/pcap format to store, see -D\n"
1033 " -D|--dump-pcap-types Dump pcap types and magic numbers and quit\n"
1034 " -B|--dump-bpf Dump generated BPF assembly\n"
1035 " -r|--rand Randomize packet forwarding order (dev->dev)\n"
1036 " -M|--no-promisc No promiscuous mode for netdev\n"
1037 " -A|--no-sock-mem Don't tune core socket memory\n"
1038 " -m|--mmap Mmap(2) pcap file i.e., for replaying pcaps\n"
1039 " -G|--sg Scatter/gather pcap file I/O\n"
1040 " -c|--clrw Use slower read(2)/write(2) I/O\n"
1041 " -S|--ring-size <size> Specify ring size to: <num>KiB/MiB/GiB\n"
1042 " -k|--kernel-pull <uint> Kernel pull from user interval in us (def: 10us)\n"
1043 " -J|--jumbo-support Support replay/fwd 64KB Super Jumbo Frames (def: 2048B)\n"
1044 " -b|--bind-cpu <cpu> Bind to specific CPU\n"
1045 " -u|--user <userid> Drop privileges and change to userid\n"
1046 " -g|--group <groupid> Drop privileges and change to groupid\n"
1047 " -H|--prio-high Make this high priority process\n"
1048 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
1049 " -s|--silent Do not print captured packets\n"
1050 " -q|--less Print less-verbose packet information\n"
1051 " -X|--hex Print packet data in hex format\n"
1052 " -l|--ascii Print human-readable packet data\n"
1053 " -U|--update Update GeoIP databases\n"
1054 " -V|--verbose Be more verbose\n"
1055 " -v|--version Show version and exit\n"
1056 " -h|--help Guess what?!\n\n"
1057 "Examples:\n"
1058 " netsniff-ng --in eth0 --out dump.pcap -s -T 0xa1b2c3d4 --b 0 tcp or udp\n"
1059 " netsniff-ng --in wlan0 --rfraw --out dump.pcap --silent --bind-cpu 0\n"
1060 " netsniff-ng --in dump.pcap --mmap --out eth0 -k1000 --silent --bind-cpu 0\n"
1061 " netsniff-ng --in dump.pcap --out dump.cfg --silent --bind-cpu 0\n"
1062 " netsniff-ng --in eth0 --out eth1 --silent --bind-cpu 0 -J --type host\n"
1063 " netsniff-ng --in eth1 --out /opt/probe/ -s -m --interval 100MiB -b 0\n"
1064 " netsniff-ng --in vlan0 --out dump.pcap -c -u `id -u bob` -g `id -g bob`\n"
1065 " netsniff-ng --in any --filter http.bpf --jumbo-support --ascii -V\n\n"
1066 "Note:\n"
1067 " For introducing bit errors, delays with random variation and more\n"
1068 " while replaying pcaps, make use of tc(8) with its disciplines (e.g. netem).\n\n"
1069 "Please report bugs to <bugs@netsniff-ng.org>\n"
1070 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1071 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1072 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1073 "Swiss federal institute of technology (ETH Zurich)\n"
1074 "License: GNU GPL version 2.0\n"
1075 "This is free software: you are free to change and redistribute it.\n"
1076 "There is NO WARRANTY, to the extent permitted by law.\n");
1077 die();
1080 static void __noreturn version(void)
1082 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_LONG);
1083 puts("http://www.netsniff-ng.org\n\n"
1084 "Please report bugs to <bugs@netsniff-ng.org>\n"
1085 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1086 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1087 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1088 "Swiss federal institute of technology (ETH Zurich)\n"
1089 "License: GNU GPL version 2.0\n"
1090 "This is free software: you are free to change and redistribute it.\n"
1091 "There is NO WARRANTY, to the extent permitted by law.\n");
1092 die();
1095 int main(int argc, char **argv)
1097 char *ptr;
1098 int c, i, j, cpu_tmp, opt_index, ops_touched = 0, vals[4] = {0};
1099 bool prio_high = false, setsockmem = true;
1100 void (*main_loop)(struct ctx *ctx) = NULL;
1101 struct ctx ctx = {
1102 .link_type = LINKTYPE_EN10MB,
1103 .print_mode = PRINT_NORM,
1104 .cpu = -1,
1105 .packet_type = -1,
1106 .promiscuous = true,
1107 .randomize = false,
1108 .pcap = PCAP_OPS_SG,
1109 .dump_interval = 60,
1110 .dump_mode = DUMP_INTERVAL_TIME,
1111 .uid = getuid(),
1112 .gid = getgid(),
1113 .magic = ORIGINAL_TCPDUMP_MAGIC,
1116 srand(time(NULL));
1118 while ((c = getopt_long(argc, argv, short_options, long_options,
1119 &opt_index)) != EOF) {
1120 switch (c) {
1121 case 'd':
1122 case 'i':
1123 ctx.device_in = xstrdup(optarg);
1124 break;
1125 case 'o':
1126 ctx.device_out = xstrdup(optarg);
1127 break;
1128 case 'P':
1129 ctx.prefix = xstrdup(optarg);
1130 break;
1131 case 'R':
1132 ctx.link_type = LINKTYPE_IEEE802_11;
1133 ctx.rfraw = 1;
1134 break;
1135 case 'r':
1136 ctx.randomize = true;
1137 break;
1138 case 'J':
1139 ctx.jumbo = true;
1140 break;
1141 case 'T':
1142 ctx.magic = (uint32_t) strtoul(optarg, NULL, 0);
1143 pcap_check_magic(ctx.magic);
1144 break;
1145 case 'f':
1146 ctx.filter = xstrdup(optarg);
1147 break;
1148 case 'M':
1149 ctx.promiscuous = false;
1150 break;
1151 case 'A':
1152 setsockmem = false;
1153 break;
1154 case 'u':
1155 ctx.uid = strtoul(optarg, NULL, 0);
1156 ctx.enforce = true;
1157 break;
1158 case 'g':
1159 ctx.gid = strtoul(optarg, NULL, 0);
1160 ctx.enforce = true;
1161 break;
1162 case 't':
1163 if (!strncmp(optarg, "host", strlen("host")))
1164 ctx.packet_type = PACKET_HOST;
1165 else if (!strncmp(optarg, "broadcast", strlen("broadcast")))
1166 ctx.packet_type = PACKET_BROADCAST;
1167 else if (!strncmp(optarg, "multicast", strlen("multicast")))
1168 ctx.packet_type = PACKET_MULTICAST;
1169 else if (!strncmp(optarg, "others", strlen("others")))
1170 ctx.packet_type = PACKET_OTHERHOST;
1171 else if (!strncmp(optarg, "outgoing", strlen("outgoing")))
1172 ctx.packet_type = PACKET_OUTGOING;
1173 else
1174 ctx.packet_type = -1;
1175 break;
1176 case 'S':
1177 ptr = optarg;
1178 ctx.reserve_size = 0;
1180 for (j = i = strlen(optarg); i > 0; --i) {
1181 if (!isdigit(optarg[j - i]))
1182 break;
1183 ptr++;
1186 if (!strncmp(ptr, "KiB", strlen("KiB")))
1187 ctx.reserve_size = 1 << 10;
1188 else if (!strncmp(ptr, "MiB", strlen("MiB")))
1189 ctx.reserve_size = 1 << 20;
1190 else if (!strncmp(ptr, "GiB", strlen("GiB")))
1191 ctx.reserve_size = 1 << 30;
1192 else
1193 panic("Syntax error in ring size param!\n");
1194 *ptr = 0;
1196 ctx.reserve_size *= strtol(optarg, NULL, 0);
1197 break;
1198 case 'b':
1199 cpu_tmp = strtol(optarg, NULL, 0);
1201 cpu_affinity(cpu_tmp);
1202 if (ctx.cpu != -2)
1203 ctx.cpu = cpu_tmp;
1204 break;
1205 case 'H':
1206 prio_high = true;
1207 break;
1208 case 'c':
1209 ctx.pcap = PCAP_OPS_RW;
1210 ops_touched = 1;
1211 break;
1212 case 'm':
1213 ctx.pcap = PCAP_OPS_MM;
1214 ops_touched = 1;
1215 break;
1216 case 'G':
1217 ctx.pcap = PCAP_OPS_SG;
1218 ops_touched = 1;
1219 break;
1220 case 'Q':
1221 ctx.cpu = -2;
1222 break;
1223 case 's':
1224 ctx.print_mode = PRINT_NONE;
1225 break;
1226 case 'q':
1227 ctx.print_mode = PRINT_LESS;
1228 break;
1229 case 'X':
1230 ctx.print_mode =
1231 (ctx.print_mode == PRINT_ASCII) ?
1232 PRINT_HEX_ASCII : PRINT_HEX;
1233 break;
1234 case 'l':
1235 ctx.print_mode =
1236 (ctx.print_mode == PRINT_HEX) ?
1237 PRINT_HEX_ASCII : PRINT_ASCII;
1238 break;
1239 case 'k':
1240 ctx.kpull = strtol(optarg, NULL, 0);
1241 break;
1242 case 'n':
1243 frame_count_max = strtol(optarg, NULL, 0);
1244 break;
1245 case 'F':
1246 ptr = optarg;
1247 ctx.dump_interval = 0;
1249 for (j = i = strlen(optarg); i > 0; --i) {
1250 if (!isdigit(optarg[j - i]))
1251 break;
1252 ptr++;
1255 if (!strncmp(ptr, "KiB", strlen("KiB"))) {
1256 ctx.dump_interval = 1 << 10;
1257 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1258 } else if (!strncmp(ptr, "MiB", strlen("MiB"))) {
1259 ctx.dump_interval = 1 << 20;
1260 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1261 } else if (!strncmp(ptr, "GiB", strlen("GiB"))) {
1262 ctx.dump_interval = 1 << 30;
1263 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1264 } else if (!strncmp(ptr, "sec", strlen("sec"))) {
1265 ctx.dump_interval = 1;
1266 ctx.dump_mode = DUMP_INTERVAL_TIME;
1267 } else if (!strncmp(ptr, "min", strlen("min"))) {
1268 ctx.dump_interval = 60;
1269 ctx.dump_mode = DUMP_INTERVAL_TIME;
1270 } else if (!strncmp(ptr, "hrs", strlen("hrs"))) {
1271 ctx.dump_interval = 60 * 60;
1272 ctx.dump_mode = DUMP_INTERVAL_TIME;
1273 } else if (!strncmp(ptr, "s", strlen("s"))) {
1274 ctx.dump_interval = 1;
1275 ctx.dump_mode = DUMP_INTERVAL_TIME;
1276 } else {
1277 panic("Syntax error in time/size param!\n");
1280 *ptr = 0;
1281 ctx.dump_interval *= strtol(optarg, NULL, 0);
1282 break;
1283 case 'V':
1284 ctx.verbose = 1;
1285 break;
1286 case 'B':
1287 ctx.dump_bpf = true;
1288 break;
1289 case 'D':
1290 pcap_dump_type_features();
1291 die();
1292 break;
1293 case 'U':
1294 update_geoip();
1295 die();
1296 break;
1297 case 'v':
1298 version();
1299 break;
1300 case 'h':
1301 help();
1302 break;
1303 case '?':
1304 switch (optopt) {
1305 case 'd':
1306 case 'i':
1307 case 'o':
1308 case 'f':
1309 case 't':
1310 case 'P':
1311 case 'F':
1312 case 'n':
1313 case 'S':
1314 case 'b':
1315 case 'k':
1316 case 'T':
1317 case 'u':
1318 case 'g':
1319 case 'e':
1320 panic("Option -%c requires an argument!\n",
1321 optopt);
1322 default:
1323 if (isprint(optopt))
1324 printf("Unknown option character `0x%X\'!\n", optopt);
1325 die();
1327 default:
1328 break;
1332 if (!ctx.filter && optind != argc) {
1333 int ret;
1334 off_t offset = 0;
1336 for (i = optind; i < argc; ++i) {
1337 size_t alen = strlen(argv[i]) + 2;
1338 size_t flen = ctx.filter ? strlen(ctx.filter) : 0;
1340 ctx.filter = xrealloc(ctx.filter, 1, flen + alen);
1341 ret = slprintf(ctx.filter + offset, strlen(argv[i]) + 2, "%s ", argv[i]);
1342 if (ret < 0)
1343 panic("Cannot concatenate filter string!\n");
1344 else
1345 offset += ret;
1349 if (!ctx.device_in)
1350 ctx.device_in = xstrdup("any");
1352 register_signal(SIGINT, signal_handler);
1353 register_signal(SIGHUP, signal_handler);
1355 tprintf_init();
1357 if (prio_high) {
1358 set_proc_prio(-20);
1359 set_sched_status(SCHED_FIFO, sched_get_priority_max(SCHED_FIFO));
1362 if (ctx.device_in && (device_mtu(ctx.device_in) ||
1363 !strncmp("any", ctx.device_in, strlen(ctx.device_in)))) {
1364 if (!ctx.device_out) {
1365 ctx.dump = 0;
1366 main_loop = recv_only_or_dump;
1367 } else if (device_mtu(ctx.device_out)) {
1368 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1369 main_loop = receive_to_xmit;
1370 } else {
1371 ctx.dump = 1;
1372 register_signal_f(SIGALRM, timer_next_dump, SA_SIGINFO);
1373 main_loop = recv_only_or_dump;
1374 if (!ops_touched)
1375 ctx.pcap = PCAP_OPS_SG;
1377 } else {
1378 if (ctx.device_out && device_mtu(ctx.device_out)) {
1379 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1380 main_loop = pcap_to_xmit;
1381 if (!ops_touched)
1382 ctx.pcap = PCAP_OPS_MM;
1383 } else {
1384 main_loop = read_pcap;
1385 if (!ops_touched)
1386 ctx.pcap = PCAP_OPS_SG;
1390 bug_on(!main_loop);
1392 init_geoip(0);
1393 if (setsockmem)
1394 set_system_socket_memory(vals, array_size(vals));
1395 if (!ctx.enforce)
1396 xlockme();
1398 main_loop(&ctx);
1400 if (!ctx.enforce)
1401 xunlockme();
1402 if (setsockmem)
1403 reset_system_socket_memory(vals, array_size(vals));
1404 destroy_geoip();
1405 device_restore_irq_affinity_list();
1406 tprintf_cleanup();
1408 free(ctx.device_in);
1409 free(ctx.device_out);
1410 free(ctx.device_trans);
1411 free(ctx.prefix);
1413 return 0;