netsniff-ng: Print process name of the netlink message origin
[netsniff-ng.git] / netsniff-ng.c
blobc1261150fea3557b8d577d15b82986f21a5d819c
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2009-2013 Daniel Borkmann.
4 * Copyright 2010 Emmanuel Roullit.
5 * Subject to the GPL, version 2.
6 */
8 #define _GNU_SOURCE
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <signal.h>
12 #include <getopt.h>
13 #include <ctype.h>
14 #include <time.h>
15 #include <string.h>
16 #include <sys/socket.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <sys/time.h>
20 #include <sys/fsuid.h>
21 #include <unistd.h>
22 #include <stdbool.h>
23 #include <pthread.h>
24 #include <fcntl.h>
26 #include "ring_rx.h"
27 #include "ring_tx.h"
28 #include "mac80211.h"
29 #include "dev.h"
30 #include "built_in.h"
31 #include "pcap_io.h"
32 #include "privs.h"
33 #include "proc.h"
34 #include "bpf.h"
35 #include "ioops.h"
36 #include "die.h"
37 #include "irq.h"
38 #include "str.h"
39 #include "sig.h"
40 #include "config.h"
41 #include "sock.h"
42 #include "geoip.h"
43 #include "lockme.h"
44 #include "tprintf.h"
45 #include "timer.h"
46 #include "tstamping.h"
47 #include "dissector.h"
48 #include "xmalloc.h"
50 enum dump_mode {
51 DUMP_INTERVAL_TIME,
52 DUMP_INTERVAL_SIZE,
55 struct ctx {
56 char *device_in, *device_out, *device_trans, *filter, *prefix;
57 int cpu, rfraw, dump, print_mode, dump_dir, packet_type;
58 unsigned long kpull, dump_interval, tx_bytes, tx_packets;
59 size_t reserve_size;
60 bool randomize, promiscuous, enforce, jumbo, dump_bpf, verbose;
61 enum pcap_ops_groups pcap; enum dump_mode dump_mode;
62 uid_t uid; gid_t gid; uint32_t link_type, magic;
65 static volatile sig_atomic_t sigint = 0;
66 static volatile bool next_dump = false;
68 static const char *short_options = "d:i:o:rf:MJt:S:k:n:b:HQmcsqXlvhF:RGAP:Vu:g:T:DBU";
69 static const struct option long_options[] = {
70 {"dev", required_argument, NULL, 'd'},
71 {"in", required_argument, NULL, 'i'},
72 {"out", required_argument, NULL, 'o'},
73 {"filter", required_argument, NULL, 'f'},
74 {"num", required_argument, NULL, 'n'},
75 {"type", required_argument, NULL, 't'},
76 {"interval", required_argument, NULL, 'F'},
77 {"ring-size", required_argument, NULL, 'S'},
78 {"kernel-pull", required_argument, NULL, 'k'},
79 {"bind-cpu", required_argument, NULL, 'b'},
80 {"prefix", required_argument, NULL, 'P'},
81 {"user", required_argument, NULL, 'u'},
82 {"group", required_argument, NULL, 'g'},
83 {"magic", required_argument, NULL, 'T'},
84 {"rand", no_argument, NULL, 'r'},
85 {"rfraw", no_argument, NULL, 'R'},
86 {"mmap", no_argument, NULL, 'm'},
87 {"sg", no_argument, NULL, 'G'},
88 {"clrw", no_argument, NULL, 'c'},
89 {"jumbo-support", no_argument, NULL, 'J'},
90 {"no-promisc", no_argument, NULL, 'M'},
91 {"prio-high", no_argument, NULL, 'H'},
92 {"notouch-irq", no_argument, NULL, 'Q'},
93 {"dump-pcap-types", no_argument, NULL, 'D'},
94 {"dump-bpf", no_argument, NULL, 'B'},
95 {"silent", no_argument, NULL, 's'},
96 {"less", no_argument, NULL, 'q'},
97 {"hex", no_argument, NULL, 'X'},
98 {"ascii", no_argument, NULL, 'l'},
99 {"no-sock-mem", no_argument, NULL, 'A'},
100 {"update", no_argument, NULL, 'U'},
101 {"verbose", no_argument, NULL, 'V'},
102 {"version", no_argument, NULL, 'v'},
103 {"help", no_argument, NULL, 'h'},
104 {NULL, 0, NULL, 0}
107 static int tx_sock;
108 static struct itimerval itimer;
109 static unsigned long frame_count_max = 0, interval = TX_KERNEL_PULL_INT;
111 #define __pcap_io pcap_ops[ctx->pcap]
113 static void signal_handler(int number)
115 switch (number) {
116 case SIGINT:
117 case SIGQUIT:
118 case SIGTERM:
119 sigint = 1;
120 case SIGHUP:
121 default:
122 break;
126 static void timer_elapsed(int unused __maybe_unused)
128 int ret;
130 set_itimer_interval_value(&itimer, 0, interval);
132 ret = pull_and_flush_tx_ring(tx_sock);
133 if (unlikely(ret < 0)) {
134 /* We could hit EBADF if the socket has been closed before
135 * the timer was triggered.
137 if (errno != EBADF && errno != ENOBUFS)
138 panic("Flushing TX_RING failed: %s!\n", strerror(errno));
141 setitimer(ITIMER_REAL, &itimer, NULL);
144 static void timer_purge(void)
146 int ret;
148 ret = pull_and_flush_tx_ring_wait(tx_sock);
149 if (unlikely(ret < 0)) {
150 if (errno != EBADF && errno != ENOBUFS)
151 panic("Flushing TX_RING failed: %s!\n", strerror(errno));
154 set_itimer_interval_value(&itimer, 0, 0);
155 setitimer(ITIMER_REAL, &itimer, NULL);
158 static void timer_next_dump(int unused __maybe_unused)
160 set_itimer_interval_value(&itimer, interval, 0);
161 next_dump = true;
162 setitimer(ITIMER_REAL, &itimer, NULL);
165 static inline bool dump_to_pcap(struct ctx *ctx)
167 return ctx->dump;
170 static void pcap_to_xmit(struct ctx *ctx)
172 uint8_t *out = NULL;
173 int ifindex, fd = 0, ret;
174 size_t size;
175 unsigned int it = 0;
176 unsigned long trunced = 0;
177 struct ring tx_ring;
178 struct frame_map *hdr;
179 struct sock_fprog bpf_ops;
180 struct timeval start, end, diff;
181 pcap_pkthdr_t phdr;
183 if (!device_up_and_running(ctx->device_out) && !ctx->rfraw)
184 panic("Device not up and running!\n");
186 bug_on(!__pcap_io);
188 tx_sock = pf_socket();
190 if (!strncmp("-", ctx->device_in, strlen("-"))) {
191 fd = dup_or_die(fileno(stdin));
192 close(fileno(stdin));
193 if (ctx->pcap == PCAP_OPS_MM)
194 ctx->pcap = PCAP_OPS_SG;
195 } else {
196 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
199 if (__pcap_io->init_once_pcap)
200 __pcap_io->init_once_pcap();
202 ret = __pcap_io->pull_fhdr_pcap(fd, &ctx->magic, &ctx->link_type);
203 if (ret)
204 panic("Error reading pcap header!\n");
206 if (__pcap_io->prepare_access_pcap) {
207 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_RD, ctx->jumbo);
208 if (ret)
209 panic("Error prepare reading pcap!\n");
212 if (ctx->rfraw) {
213 ctx->device_trans = xstrdup(ctx->device_out);
214 xfree(ctx->device_out);
216 enter_rfmon_mac80211(ctx->device_trans, &ctx->device_out);
217 if (ctx->link_type != LINKTYPE_IEEE802_11)
218 panic("Wrong linktype of pcap!\n");
221 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 ring_tx_setup(&tx_ring, tx_sock, size, ifindex, ctx->jumbo, ctx->verbose);
230 dissector_init_all(ctx->print_mode);
232 if (ctx->cpu >= 0 && ifindex > 0) {
233 int irq = device_irq_number(ctx->device_out);
234 device_set_irq_affinity(irq, ctx->cpu);
236 if (ctx->verbose)
237 printf("IRQ: %s:%d > CPU%d\n",
238 ctx->device_out, irq, ctx->cpu);
241 if (ctx->kpull)
242 interval = ctx->kpull;
244 set_itimer_interval_value(&itimer, 0, interval);
245 setitimer(ITIMER_REAL, &itimer, NULL);
247 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
249 printf("Running! Hang up with ^C!\n\n");
250 fflush(stdout);
252 bug_on(gettimeofday(&start, NULL));
254 while (likely(sigint == 0)) {
255 while (user_may_pull_from_tx(tx_ring.frames[it].iov_base)) {
256 hdr = tx_ring.frames[it].iov_base;
257 out = ((uint8_t *) hdr) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
259 do {
260 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic, out,
261 ring_frame_size(&tx_ring));
262 if (unlikely(ret <= 0))
263 goto out;
265 if (ring_frame_size(&tx_ring) <
266 pcap_get_length(&phdr, ctx->magic)) {
267 pcap_set_length(&phdr, ctx->magic,
268 ring_frame_size(&tx_ring));
269 trunced++;
271 } while (ctx->filter &&
272 !bpf_run_filter(&bpf_ops, out,
273 pcap_get_length(&phdr, ctx->magic)));
275 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &hdr->tp_h, NULL);
277 ctx->tx_bytes += hdr->tp_h.tp_len;;
278 ctx->tx_packets++;
280 show_frame_hdr(hdr, ctx->print_mode);
282 dissector_entry_point(out, hdr->tp_h.tp_snaplen,
283 ctx->link_type, ctx->print_mode);
285 kernel_may_pull_from_tx(&hdr->tp_h);
287 it++;
288 if (it >= tx_ring.layout.tp_frame_nr)
289 it = 0;
291 if (unlikely(sigint == 1))
292 break;
294 if (frame_count_max != 0) {
295 if (ctx->tx_packets >= frame_count_max) {
296 sigint = 1;
297 break;
303 out:
304 bug_on(gettimeofday(&end, NULL));
305 timersub(&end, &start, &diff);
307 timer_purge();
309 bpf_release(&bpf_ops);
311 dissector_cleanup_all();
312 destroy_tx_ring(tx_sock, &tx_ring);
314 if (ctx->rfraw)
315 leave_rfmon_mac80211(ctx->device_out);
317 if (__pcap_io->prepare_close_pcap)
318 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
320 if (!strncmp("-", ctx->device_in, strlen("-")))
321 dup2(fd, fileno(stdin));
322 close(fd);
324 close(tx_sock);
326 fflush(stdout);
327 printf("\n");
328 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
329 printf("\r%12lu packets truncated in file\n", trunced);
330 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
331 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
334 static void receive_to_xmit(struct ctx *ctx)
336 short ifflags = 0;
337 uint8_t *in, *out;
338 int rx_sock, ifindex_in, ifindex_out, ret;
339 size_t size_in, size_out;
340 unsigned int it_in = 0, it_out = 0;
341 unsigned long frame_count = 0;
342 struct frame_map *hdr_in, *hdr_out;
343 struct ring tx_ring, rx_ring;
344 struct pollfd rx_poll;
345 struct sock_fprog bpf_ops;
347 if (!strncmp(ctx->device_in, ctx->device_out, IFNAMSIZ))
348 panic("Ingress/egress devices must be different!\n");
349 if (!device_up_and_running(ctx->device_out))
350 panic("Egress device not up and running!\n");
352 rx_sock = pf_socket();
353 tx_sock = pf_socket();
355 ifindex_in = device_ifindex(ctx->device_in);
356 ifindex_out = device_ifindex(ctx->device_out);
358 size_in = ring_size(ctx->device_in, ctx->reserve_size);
359 size_out = ring_size(ctx->device_out, ctx->reserve_size);
361 enable_kernel_bpf_jit_compiler();
363 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
364 if (ctx->dump_bpf)
365 bpf_dump_all(&bpf_ops);
366 bpf_attach_to_sock(rx_sock, &bpf_ops);
368 ring_rx_setup(&rx_ring, rx_sock, size_in, ifindex_in, &rx_poll, false, ctx->jumbo, ctx->verbose);
369 ring_tx_setup(&tx_ring, tx_sock, size_out, ifindex_out, ctx->jumbo, ctx->verbose);
371 dissector_init_all(ctx->print_mode);
373 if (ctx->promiscuous)
374 ifflags = device_enter_promiscuous_mode(ctx->device_in);
376 if (ctx->kpull)
377 interval = ctx->kpull;
379 set_itimer_interval_value(&itimer, 0, interval);
380 setitimer(ITIMER_REAL, &itimer, NULL);
382 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
384 printf("Running! Hang up with ^C!\n\n");
385 fflush(stdout);
387 while (likely(sigint == 0)) {
388 while (user_may_pull_from_rx(rx_ring.frames[it_in].iov_base)) {
389 hdr_in = rx_ring.frames[it_in].iov_base;
390 in = ((uint8_t *) hdr_in) + hdr_in->tp_h.tp_mac;
392 frame_count++;
394 if (ctx->packet_type != -1)
395 if (ctx->packet_type != hdr_in->s_ll.sll_pkttype)
396 goto next;
398 hdr_out = tx_ring.frames[it_out].iov_base;
399 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
401 while (!user_may_pull_from_tx(tx_ring.frames[it_out].iov_base) &&
402 likely(!sigint)) {
403 if (ctx->randomize)
404 next_rnd_slot(&it_out, &tx_ring);
405 else {
406 it_out++;
407 if (it_out >= tx_ring.layout.tp_frame_nr)
408 it_out = 0;
411 hdr_out = tx_ring.frames[it_out].iov_base;
412 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
415 tpacket_hdr_clone(&hdr_out->tp_h, &hdr_in->tp_h);
416 fmemcpy(out, in, hdr_in->tp_h.tp_len);
418 kernel_may_pull_from_tx(&hdr_out->tp_h);
419 if (ctx->randomize)
420 next_rnd_slot(&it_out, &tx_ring);
421 else {
422 it_out++;
423 if (it_out >= tx_ring.layout.tp_frame_nr)
424 it_out = 0;
427 show_frame_hdr(hdr_in, ctx->print_mode);
429 dissector_entry_point(in, hdr_in->tp_h.tp_snaplen,
430 ctx->link_type, ctx->print_mode);
432 if (frame_count_max != 0) {
433 if (frame_count >= frame_count_max) {
434 sigint = 1;
435 break;
439 next:
440 kernel_may_pull_from_rx(&hdr_in->tp_h);
442 it_in++;
443 if (it_in >= rx_ring.layout.tp_frame_nr)
444 it_in = 0;
446 if (unlikely(sigint == 1))
447 goto out;
450 ret = poll(&rx_poll, 1, -1);
451 if (unlikely(ret < 0)) {
452 if (errno != EINTR)
453 panic("Poll failed!\n");
457 out:
458 timer_purge();
460 sock_rx_net_stats(rx_sock, 0);
462 bpf_release(&bpf_ops);
464 dissector_cleanup_all();
466 destroy_tx_ring(tx_sock, &tx_ring);
467 destroy_rx_ring(rx_sock, &rx_ring);
469 if (ctx->promiscuous)
470 device_leave_promiscuous_mode(ctx->device_in, ifflags);
472 close(tx_sock);
473 close(rx_sock);
476 static void translate_pcap_to_txf(int fdo, uint8_t *out, size_t len)
478 size_t bytes_done = 0;
479 char bout[80];
481 slprintf(bout, sizeof(bout), "{\n ");
482 write_or_die(fdo, bout, strlen(bout));
484 while (bytes_done < len) {
485 slprintf(bout, sizeof(bout), "0x%02x, ", out[bytes_done]);
486 write_or_die(fdo, bout, strlen(bout));
488 bytes_done++;
490 if (bytes_done % 10 == 0) {
491 slprintf(bout, sizeof(bout), "\n");
492 write_or_die(fdo, bout, strlen(bout));
494 if (bytes_done < len) {
495 slprintf(bout, sizeof(bout), " ");
496 write_or_die(fdo, bout, strlen(bout));
500 if (bytes_done % 10 != 0) {
501 slprintf(bout, sizeof(bout), "\n");
502 write_or_die(fdo, bout, strlen(bout));
505 slprintf(bout, sizeof(bout), "}\n\n");
506 write_or_die(fdo, bout, strlen(bout));
509 static void read_pcap(struct ctx *ctx)
511 uint8_t *out;
512 int ret, fd, fdo = 0;
513 unsigned long trunced = 0;
514 size_t out_len;
515 pcap_pkthdr_t phdr;
516 struct sock_fprog bpf_ops;
517 struct frame_map fm;
518 struct timeval start, end, diff;
520 bug_on(!__pcap_io);
522 if (!strncmp("-", ctx->device_in, strlen("-"))) {
523 fd = dup_or_die(fileno(stdin));
524 close(fileno(stdin));
525 if (ctx->pcap == PCAP_OPS_MM)
526 ctx->pcap = PCAP_OPS_SG;
527 } else {
528 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
531 if (__pcap_io->init_once_pcap)
532 __pcap_io->init_once_pcap();
534 ret = __pcap_io->pull_fhdr_pcap(fd, &ctx->magic, &ctx->link_type);
535 if (ret)
536 panic("Error reading pcap header!\n");
538 if (__pcap_io->prepare_access_pcap) {
539 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_RD, ctx->jumbo);
540 if (ret)
541 panic("Error prepare reading pcap!\n");
544 fmemset(&fm, 0, sizeof(fm));
546 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
547 if (ctx->dump_bpf)
548 bpf_dump_all(&bpf_ops);
550 dissector_init_all(ctx->print_mode);
552 out_len = round_up(1024 * 1024, RUNTIME_PAGE_SIZE);
553 out = xmalloc_aligned(out_len, CO_CACHE_LINE_SIZE);
555 if (ctx->device_out) {
556 if (!strncmp("-", ctx->device_out, strlen("-"))) {
557 fdo = dup_or_die(fileno(stdout));
558 close(fileno(stdout));
559 } else {
560 fdo = open_or_die_m(ctx->device_out, O_RDWR | O_CREAT |
561 O_TRUNC | O_LARGEFILE, DEFFILEMODE);
565 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
567 printf("Running! Hang up with ^C!\n\n");
568 fflush(stdout);
570 bug_on(gettimeofday(&start, NULL));
572 while (likely(sigint == 0)) {
573 do {
574 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic,
575 out, out_len);
576 if (unlikely(ret < 0))
577 goto out;
579 if (unlikely(pcap_get_length(&phdr, ctx->magic) == 0)) {
580 trunced++;
581 continue;
584 if (unlikely(pcap_get_length(&phdr, ctx->magic) > out_len)) {
585 pcap_set_length(&phdr, ctx->magic, out_len);
586 trunced++;
588 } while (ctx->filter &&
589 !bpf_run_filter(&bpf_ops, out,
590 pcap_get_length(&phdr, ctx->magic)));
592 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &fm.tp_h, &fm.s_ll);
594 ctx->tx_bytes += fm.tp_h.tp_len;
595 ctx->tx_packets++;
597 show_frame_hdr(&fm, ctx->print_mode);
599 dissector_entry_point(out, fm.tp_h.tp_snaplen,
600 ctx->link_type, ctx->print_mode);
602 if (ctx->device_out)
603 translate_pcap_to_txf(fdo, out, fm.tp_h.tp_snaplen);
605 if (frame_count_max != 0) {
606 if (ctx->tx_packets >= frame_count_max) {
607 sigint = 1;
608 break;
613 out:
615 bug_on(gettimeofday(&end, NULL));
616 timersub(&end, &start, &diff);
618 bpf_release(&bpf_ops);
620 dissector_cleanup_all();
622 if (__pcap_io->prepare_close_pcap)
623 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
625 xfree(out);
627 fflush(stdout);
628 printf("\n");
629 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
630 printf("\r%12lu packets truncated in file\n", trunced);
631 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
632 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
634 if (!strncmp("-", ctx->device_in, strlen("-")))
635 dup2(fd, fileno(stdin));
636 close(fd);
638 if (ctx->device_out) {
639 if (!strncmp("-", ctx->device_out, strlen("-")))
640 dup2(fdo, fileno(stdout));
641 close(fdo);
645 static void finish_multi_pcap_file(struct ctx *ctx, int fd)
647 __pcap_io->fsync_pcap(fd);
649 if (__pcap_io->prepare_close_pcap)
650 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
652 close(fd);
654 fmemset(&itimer, 0, sizeof(itimer));
655 setitimer(ITIMER_REAL, &itimer, NULL);
658 static int next_multi_pcap_file(struct ctx *ctx, int fd)
660 int ret;
661 char fname[512];
663 __pcap_io->fsync_pcap(fd);
665 if (__pcap_io->prepare_close_pcap)
666 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
668 close(fd);
670 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
671 ctx->prefix ? : "dump-", time(NULL));
673 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
674 O_LARGEFILE, DEFFILEMODE);
676 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
677 if (ret)
678 panic("Error writing pcap header!\n");
680 if (__pcap_io->prepare_access_pcap) {
681 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, true);
682 if (ret)
683 panic("Error prepare writing pcap!\n");
686 return fd;
689 static int begin_multi_pcap_file(struct ctx *ctx)
691 int fd, ret;
692 char fname[256];
694 bug_on(!__pcap_io);
696 if (ctx->device_out[strlen(ctx->device_out) - 1] == '/')
697 ctx->device_out[strlen(ctx->device_out) - 1] = 0;
699 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
700 ctx->prefix ? : "dump-", time(NULL));
702 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
703 O_LARGEFILE, DEFFILEMODE);
705 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
706 if (ret)
707 panic("Error writing pcap header!\n");
709 if (__pcap_io->prepare_access_pcap) {
710 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, true);
711 if (ret)
712 panic("Error prepare writing pcap!\n");
715 if (ctx->dump_mode == DUMP_INTERVAL_TIME) {
716 interval = ctx->dump_interval;
718 set_itimer_interval_value(&itimer, interval, 0);
719 setitimer(ITIMER_REAL, &itimer, NULL);
720 } else {
721 interval = 0;
724 return fd;
727 static void finish_single_pcap_file(struct ctx *ctx, int fd)
729 __pcap_io->fsync_pcap(fd);
731 if (__pcap_io->prepare_close_pcap)
732 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
734 if (strncmp("-", ctx->device_out, strlen("-")))
735 close(fd);
736 else
737 dup2(fd, fileno(stdout));
740 static int begin_single_pcap_file(struct ctx *ctx)
742 int fd, ret;
744 bug_on(!__pcap_io);
746 if (!strncmp("-", ctx->device_out, strlen("-"))) {
747 fd = dup_or_die(fileno(stdout));
748 close(fileno(stdout));
749 if (ctx->pcap == PCAP_OPS_MM)
750 ctx->pcap = PCAP_OPS_SG;
751 } else {
752 fd = open_or_die_m(ctx->device_out,
753 O_RDWR | O_CREAT | O_TRUNC |
754 O_LARGEFILE, DEFFILEMODE);
757 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
758 if (ret)
759 panic("Error writing pcap header!\n");
761 if (__pcap_io->prepare_access_pcap) {
762 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, true);
763 if (ret)
764 panic("Error prepare writing pcap!\n");
767 return fd;
770 static void print_pcap_file_stats(int sock, struct ctx *ctx)
772 int ret;
773 struct tpacket_stats kstats;
774 socklen_t slen = sizeof(kstats);
776 fmemset(&kstats, 0, sizeof(kstats));
778 ret = getsockopt(sock, SOL_PACKET, PACKET_STATISTICS, &kstats, &slen);
779 if (unlikely(ret))
780 panic("Cannot get packet statistics!\n");
782 if (ctx->print_mode == PRINT_NONE) {
783 printf(".(+%u/-%u)", kstats.tp_packets - kstats.tp_drops,
784 kstats.tp_drops);
785 fflush(stdout);
789 static void walk_t3_block(struct block_desc *pbd, struct ctx *ctx,
790 int sock, int *fd, unsigned long *frame_count)
792 int num_pkts = pbd->h1.num_pkts, i;
793 struct tpacket3_hdr *hdr;
794 struct sockaddr_ll *sll;
796 hdr = (void *) ((uint8_t *) pbd + pbd->h1.offset_to_first_pkt);
797 sll = (void *) ((uint8_t *) hdr + TPACKET_ALIGN(sizeof(*hdr)));
799 for (i = 0; i < num_pkts && likely(sigint == 0); ++i) {
800 uint8_t *packet = ((uint8_t *) hdr + hdr->tp_mac);
801 pcap_pkthdr_t phdr;
803 if (ctx->packet_type != -1)
804 if (ctx->packet_type != sll->sll_pkttype)
805 goto next;
807 (*frame_count)++;
809 if (dump_to_pcap(ctx)) {
810 int ret;
812 tpacket3_hdr_to_pcap_pkthdr(hdr, sll, &phdr, ctx->magic);
814 ret = __pcap_io->write_pcap(*fd, &phdr, ctx->magic, packet,
815 pcap_get_length(&phdr, ctx->magic));
816 if (unlikely(ret != (int) pcap_get_total_length(&phdr, ctx->magic)))
817 panic("Write error to pcap!\n");
820 __show_frame_hdr(sll, hdr, ctx->print_mode, true);
822 dissector_entry_point(packet, hdr->tp_snaplen, ctx->link_type,
823 ctx->print_mode);
824 next:
825 hdr = (void *) ((uint8_t *) hdr + hdr->tp_next_offset);
826 sll = (void *) ((uint8_t *) hdr + TPACKET_ALIGN(sizeof(*hdr)));
828 if (frame_count_max != 0) {
829 if (unlikely(*frame_count >= frame_count_max)) {
830 sigint = 1;
831 break;
835 if (dump_to_pcap(ctx)) {
836 if (ctx->dump_mode == DUMP_INTERVAL_SIZE) {
837 interval += hdr->tp_snaplen;
838 if (interval > ctx->dump_interval) {
839 next_dump = true;
840 interval = 0;
844 if (next_dump) {
845 *fd = next_multi_pcap_file(ctx, *fd);
846 next_dump = false;
848 if (unlikely(ctx->verbose))
849 print_pcap_file_stats(sock, ctx);
855 static void recv_only_or_dump(struct ctx *ctx)
857 short ifflags = 0;
858 int sock, ifindex, fd = 0, ret;
859 size_t size;
860 unsigned int it = 0;
861 struct ring rx_ring;
862 struct pollfd rx_poll;
863 struct sock_fprog bpf_ops;
864 struct timeval start, end, diff;
865 struct block_desc *pbd;
866 unsigned long frame_count = 0;
868 sock = pf_socket();
870 if (ctx->rfraw) {
871 ctx->device_trans = xstrdup(ctx->device_in);
872 xfree(ctx->device_in);
874 enter_rfmon_mac80211(ctx->device_trans, &ctx->device_in);
875 ctx->link_type = LINKTYPE_IEEE802_11;
878 ifindex = device_ifindex(ctx->device_in);
880 size = ring_size(ctx->device_in, ctx->reserve_size);
882 enable_kernel_bpf_jit_compiler();
884 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
885 if (ctx->dump_bpf)
886 bpf_dump_all(&bpf_ops);
887 bpf_attach_to_sock(sock, &bpf_ops);
889 ret = set_sockopt_hwtimestamp(sock, ctx->device_in);
890 if (ret == 0 && ctx->verbose)
891 printf("HW timestamping enabled\n");
893 ring_rx_setup(&rx_ring, sock, size, ifindex, &rx_poll, true, true, ctx->verbose);
895 dissector_init_all(ctx->print_mode);
897 if (ctx->cpu >= 0 && ifindex > 0) {
898 int irq = device_irq_number(ctx->device_in);
899 device_set_irq_affinity(irq, ctx->cpu);
901 if (ctx->verbose)
902 printf("IRQ: %s:%d > CPU%d\n",
903 ctx->device_in, irq, ctx->cpu);
906 if (ctx->promiscuous)
907 ifflags = device_enter_promiscuous_mode(ctx->device_in);
909 if (dump_to_pcap(ctx) && __pcap_io->init_once_pcap)
910 __pcap_io->init_once_pcap();
912 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
914 if (dump_to_pcap(ctx)) {
915 struct stat stats;
917 ret = stat(ctx->device_out, &stats);
918 if (ret < 0)
919 ctx->dump_dir = 0;
920 else
921 ctx->dump_dir = S_ISDIR(stats.st_mode);
923 if (ctx->dump_dir)
924 fd = begin_multi_pcap_file(ctx);
925 else
926 fd = begin_single_pcap_file(ctx);
929 printf("Running! Hang up with ^C!\n\n");
930 fflush(stdout);
932 bug_on(gettimeofday(&start, NULL));
934 while (likely(sigint == 0)) {
935 while (user_may_pull_from_rx_block((pbd = (void *)
936 rx_ring.frames[it].iov_base))) {
937 walk_t3_block(pbd, ctx, sock, &fd, &frame_count);
939 kernel_may_pull_from_rx_block(pbd);
940 it = (it + 1) % rx_ring.layout3.tp_block_nr;
942 if (unlikely(sigint == 1))
943 break;
946 ret = poll(&rx_poll, 1, -1);
947 if (unlikely(ret < 0)) {
948 if (errno != EINTR)
949 panic("Poll failed!\n");
953 bug_on(gettimeofday(&end, NULL));
954 timersub(&end, &start, &diff);
956 if (!(ctx->dump_dir && ctx->print_mode == PRINT_NONE)) {
957 sock_rx_net_stats(sock, frame_count);
959 printf("\r%12lu sec, %lu usec in total\n",
960 diff.tv_sec, diff.tv_usec);
961 } else {
962 printf("\n\n");
963 fflush(stdout);
966 bpf_release(&bpf_ops);
967 dissector_cleanup_all();
968 destroy_rx_ring(sock, &rx_ring);
970 if (ctx->promiscuous)
971 device_leave_promiscuous_mode(ctx->device_in, ifflags);
973 if (ctx->rfraw)
974 leave_rfmon_mac80211(ctx->device_in);
976 if (dump_to_pcap(ctx)) {
977 if (ctx->dump_dir)
978 finish_multi_pcap_file(ctx, fd);
979 else
980 finish_single_pcap_file(ctx, fd);
983 close(sock);
986 static void init_ctx(struct ctx *ctx)
988 memset(ctx, 0, sizeof(*ctx));
989 ctx->uid = getuid();
990 ctx->uid = getgid();
992 ctx->cpu = -1;
993 ctx->packet_type = -1;
995 ctx->magic = ORIGINAL_TCPDUMP_MAGIC;
996 ctx->print_mode = PRINT_NORM;
997 ctx->pcap = PCAP_OPS_SG;
999 ctx->dump_mode = DUMP_INTERVAL_TIME;
1000 ctx->dump_interval = 60;
1002 ctx->promiscuous = true;
1003 ctx->randomize = false;
1006 static void destroy_ctx(struct ctx *ctx)
1008 free(ctx->device_in);
1009 free(ctx->device_out);
1010 free(ctx->device_trans);
1012 free(ctx->prefix);
1015 static void __noreturn help(void)
1017 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING);
1018 puts("http://www.netsniff-ng.org\n\n"
1019 "Usage: netsniff-ng [options] [filter-expression]\n"
1020 "Options:\n"
1021 " -i|-d|--dev|--in <dev|pcap|-> Input source as netdev, pcap or pcap stdin\n"
1022 " -o|--out <dev|pcap|dir|cfg|-> Output sink as netdev, pcap, directory, trafgen, or stdout\n"
1023 " -f|--filter <bpf-file|expr> Use BPF filter file from bpfc or tcpdump-like expression\n"
1024 " -t|--type <type> Filter for: host|broadcast|multicast|others|outgoing\n"
1025 " -F|--interval <size|time> Dump interval if -o is a dir: <num>KiB/MiB/GiB/s/sec/min/hrs\n"
1026 " -R|--rfraw Capture or inject raw 802.11 frames\n"
1027 " -n|--num <0|uint> Number of packets until exit (def: 0)\n"
1028 " -P|--prefix <name> Prefix for pcaps stored in directory\n"
1029 " -T|--magic <pcap-magic> Pcap magic number/pcap format to store, see -D\n"
1030 " -D|--dump-pcap-types Dump pcap types and magic numbers and quit\n"
1031 " -B|--dump-bpf Dump generated BPF assembly\n"
1032 " -r|--rand Randomize packet forwarding order (dev->dev)\n"
1033 " -M|--no-promisc No promiscuous mode for netdev\n"
1034 " -A|--no-sock-mem Don't tune core socket memory\n"
1035 " -m|--mmap Mmap(2) pcap file I/O, e.g. for replaying pcaps\n"
1036 " -G|--sg Scatter/gather pcap file I/O\n"
1037 " -c|--clrw Use slower read(2)/write(2) I/O\n"
1038 " -S|--ring-size <size> Specify ring size to: <num>KiB/MiB/GiB\n"
1039 " -k|--kernel-pull <uint> Kernel pull from user interval in us (def: 10us)\n"
1040 " -J|--jumbo-support Support replay/fwd 64KB Super Jumbo Frames (def: 2048B)\n"
1041 " -b|--bind-cpu <cpu> Bind to specific CPU\n"
1042 " -u|--user <userid> Drop privileges and change to userid\n"
1043 " -g|--group <groupid> Drop privileges and change to groupid\n"
1044 " -H|--prio-high Make this high priority process\n"
1045 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
1046 " -s|--silent Do not print captured packets\n"
1047 " -q|--less Print less-verbose packet information\n"
1048 " -X|--hex Print packet data in hex format\n"
1049 " -l|--ascii Print human-readable packet data\n"
1050 " -U|--update Update GeoIP databases\n"
1051 " -V|--verbose Be more verbose\n"
1052 " -v|--version Show version and exit\n"
1053 " -h|--help Guess what?!\n\n"
1054 "Examples:\n"
1055 " netsniff-ng --in eth0 --out dump.pcap -s -T 0xa1b2c3d4 --b 0 tcp or udp\n"
1056 " netsniff-ng --in wlan0 --rfraw --out dump.pcap --silent --bind-cpu 0\n"
1057 " netsniff-ng --in dump.pcap --mmap --out eth0 -k1000 --silent --bind-cpu 0\n"
1058 " netsniff-ng --in dump.pcap --out dump.cfg --silent --bind-cpu 0\n"
1059 " netsniff-ng --in eth0 --out eth1 --silent --bind-cpu 0 -J --type host\n"
1060 " netsniff-ng --in eth1 --out /opt/probe/ -s -m --interval 100MiB -b 0\n"
1061 " netsniff-ng --in vlan0 --out dump.pcap -c -u `id -u bob` -g `id -g bob`\n"
1062 " netsniff-ng --in any --filter http.bpf --jumbo-support --ascii -V\n\n"
1063 "Note:\n"
1064 " For introducing bit errors, delays with random variation and more\n"
1065 " while replaying pcaps, make use of tc(8) with its disciplines (e.g. netem).\n\n"
1066 "Please report bugs to <bugs@netsniff-ng.org>\n"
1067 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1068 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1069 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1070 "Swiss federal institute of technology (ETH Zurich)\n"
1071 "License: GNU GPL version 2.0\n"
1072 "This is free software: you are free to change and redistribute it.\n"
1073 "There is NO WARRANTY, to the extent permitted by law.\n");
1074 die();
1077 static void __noreturn version(void)
1079 printf("\nnetsniff-ng %s, Git id: %s\n", VERSION_LONG, GITVERSION);
1080 puts("the packet sniffing beast\n"
1081 "http://www.netsniff-ng.org\n\n"
1082 "Please report bugs to <bugs@netsniff-ng.org>\n"
1083 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1084 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1085 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1086 "Swiss federal institute of technology (ETH Zurich)\n"
1087 "License: GNU GPL version 2.0\n"
1088 "This is free software: you are free to change and redistribute it.\n"
1089 "There is NO WARRANTY, to the extent permitted by law.\n");
1090 die();
1093 int main(int argc, char **argv)
1095 char *ptr;
1096 int c, i, j, cpu_tmp, opt_index, ops_touched = 0, vals[4] = {0};
1097 bool prio_high = false, setsockmem = true;
1098 void (*main_loop)(struct ctx *ctx) = NULL;
1099 struct ctx ctx;
1101 init_ctx(&ctx);
1102 srand(time(NULL));
1104 while ((c = getopt_long(argc, argv, short_options, long_options,
1105 &opt_index)) != EOF) {
1106 switch (c) {
1107 case 'd':
1108 case 'i':
1109 ctx.device_in = xstrdup(optarg);
1110 break;
1111 case 'o':
1112 ctx.device_out = xstrdup(optarg);
1113 break;
1114 case 'P':
1115 ctx.prefix = xstrdup(optarg);
1116 break;
1117 case 'R':
1118 ctx.link_type = LINKTYPE_IEEE802_11;
1119 ctx.rfraw = 1;
1120 break;
1121 case 'r':
1122 ctx.randomize = true;
1123 break;
1124 case 'J':
1125 ctx.jumbo = true;
1126 break;
1127 case 'T':
1128 ctx.magic = (uint32_t) strtoul(optarg, NULL, 0);
1129 pcap_check_magic(ctx.magic);
1130 break;
1131 case 'f':
1132 ctx.filter = xstrdup(optarg);
1133 break;
1134 case 'M':
1135 ctx.promiscuous = false;
1136 break;
1137 case 'A':
1138 setsockmem = false;
1139 break;
1140 case 'u':
1141 ctx.uid = strtoul(optarg, NULL, 0);
1142 ctx.enforce = true;
1143 break;
1144 case 'g':
1145 ctx.gid = strtoul(optarg, NULL, 0);
1146 ctx.enforce = true;
1147 break;
1148 case 't':
1149 if (!strncmp(optarg, "host", strlen("host")))
1150 ctx.packet_type = PACKET_HOST;
1151 else if (!strncmp(optarg, "broadcast", strlen("broadcast")))
1152 ctx.packet_type = PACKET_BROADCAST;
1153 else if (!strncmp(optarg, "multicast", strlen("multicast")))
1154 ctx.packet_type = PACKET_MULTICAST;
1155 else if (!strncmp(optarg, "others", strlen("others")))
1156 ctx.packet_type = PACKET_OTHERHOST;
1157 else if (!strncmp(optarg, "outgoing", strlen("outgoing")))
1158 ctx.packet_type = PACKET_OUTGOING;
1159 else
1160 ctx.packet_type = -1;
1161 break;
1162 case 'S':
1163 ptr = optarg;
1164 for (j = i = strlen(optarg); i > 0; --i) {
1165 if (!isdigit(optarg[j - i]))
1166 break;
1167 ptr++;
1170 if (!strncmp(ptr, "KiB", strlen("KiB")))
1171 ctx.reserve_size = 1 << 10;
1172 else if (!strncmp(ptr, "MiB", strlen("MiB")))
1173 ctx.reserve_size = 1 << 20;
1174 else if (!strncmp(ptr, "GiB", strlen("GiB")))
1175 ctx.reserve_size = 1 << 30;
1176 else
1177 panic("Syntax error in ring size param!\n");
1179 ctx.reserve_size *= strtoul(optarg, NULL, 0);
1180 break;
1181 case 'b':
1182 cpu_tmp = strtol(optarg, NULL, 0);
1184 cpu_affinity(cpu_tmp);
1185 if (ctx.cpu != -2)
1186 ctx.cpu = cpu_tmp;
1187 break;
1188 case 'H':
1189 prio_high = true;
1190 break;
1191 case 'c':
1192 ctx.pcap = PCAP_OPS_RW;
1193 ops_touched = 1;
1194 break;
1195 case 'm':
1196 ctx.pcap = PCAP_OPS_MM;
1197 ops_touched = 1;
1198 break;
1199 case 'G':
1200 ctx.pcap = PCAP_OPS_SG;
1201 ops_touched = 1;
1202 break;
1203 case 'Q':
1204 ctx.cpu = -2;
1205 break;
1206 case 's':
1207 ctx.print_mode = PRINT_NONE;
1208 break;
1209 case 'q':
1210 ctx.print_mode = PRINT_LESS;
1211 break;
1212 case 'X':
1213 ctx.print_mode =
1214 (ctx.print_mode == PRINT_ASCII) ?
1215 PRINT_HEX_ASCII : PRINT_HEX;
1216 break;
1217 case 'l':
1218 ctx.print_mode =
1219 (ctx.print_mode == PRINT_HEX) ?
1220 PRINT_HEX_ASCII : PRINT_ASCII;
1221 break;
1222 case 'k':
1223 ctx.kpull = strtoul(optarg, NULL, 0);
1224 break;
1225 case 'n':
1226 frame_count_max = strtoul(optarg, NULL, 0);
1227 break;
1228 case 'F':
1229 ptr = optarg;
1230 for (j = i = strlen(optarg); i > 0; --i) {
1231 if (!isdigit(optarg[j - i]))
1232 break;
1233 ptr++;
1236 if (!strncmp(ptr, "KiB", strlen("KiB"))) {
1237 ctx.dump_interval = 1 << 10;
1238 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1239 } else if (!strncmp(ptr, "MiB", strlen("MiB"))) {
1240 ctx.dump_interval = 1 << 20;
1241 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1242 } else if (!strncmp(ptr, "GiB", strlen("GiB"))) {
1243 ctx.dump_interval = 1 << 30;
1244 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1245 } else if (!strncmp(ptr, "sec", strlen("sec"))) {
1246 ctx.dump_interval = 1;
1247 ctx.dump_mode = DUMP_INTERVAL_TIME;
1248 } else if (!strncmp(ptr, "min", strlen("min"))) {
1249 ctx.dump_interval = 60;
1250 ctx.dump_mode = DUMP_INTERVAL_TIME;
1251 } else if (!strncmp(ptr, "hrs", strlen("hrs"))) {
1252 ctx.dump_interval = 60 * 60;
1253 ctx.dump_mode = DUMP_INTERVAL_TIME;
1254 } else if (!strncmp(ptr, "s", strlen("s"))) {
1255 ctx.dump_interval = 1;
1256 ctx.dump_mode = DUMP_INTERVAL_TIME;
1257 } else {
1258 panic("Syntax error in time/size param!\n");
1261 ctx.dump_interval *= strtoul(optarg, NULL, 0);
1262 break;
1263 case 'V':
1264 ctx.verbose = true;
1265 break;
1266 case 'B':
1267 ctx.dump_bpf = true;
1268 break;
1269 case 'D':
1270 pcap_dump_type_features();
1271 die();
1272 break;
1273 case 'U':
1274 update_geoip();
1275 die();
1276 break;
1277 case 'v':
1278 version();
1279 break;
1280 case 'h':
1281 help();
1282 break;
1283 case '?':
1284 switch (optopt) {
1285 case 'd':
1286 case 'i':
1287 case 'o':
1288 case 'f':
1289 case 't':
1290 case 'P':
1291 case 'F':
1292 case 'n':
1293 case 'S':
1294 case 'b':
1295 case 'k':
1296 case 'T':
1297 case 'u':
1298 case 'g':
1299 case 'e':
1300 panic("Option -%c requires an argument!\n",
1301 optopt);
1302 default:
1303 if (isprint(optopt))
1304 printf("Unknown option character `0x%X\'!\n", optopt);
1305 die();
1307 default:
1308 break;
1312 if (!ctx.filter && optind != argc) {
1313 int ret;
1314 off_t offset = 0;
1316 for (i = optind; i < argc; ++i) {
1317 size_t alen = strlen(argv[i]) + 2;
1318 size_t flen = ctx.filter ? strlen(ctx.filter) : 0;
1320 ctx.filter = xrealloc(ctx.filter, 1, flen + alen);
1321 ret = slprintf(ctx.filter + offset, strlen(argv[i]) + 2, "%s ", argv[i]);
1322 if (ret < 0)
1323 panic("Cannot concatenate filter string!\n");
1324 else
1325 offset += ret;
1329 if (!ctx.device_in)
1330 ctx.device_in = xstrdup("any");
1332 register_signal(SIGINT, signal_handler);
1333 register_signal(SIGQUIT, signal_handler);
1334 register_signal(SIGTERM, signal_handler);
1335 register_signal(SIGHUP, signal_handler);
1337 tprintf_init();
1339 if (prio_high) {
1340 set_proc_prio(-20);
1341 set_sched_status(SCHED_FIFO, sched_get_priority_max(SCHED_FIFO));
1344 if (ctx.device_in && (device_mtu(ctx.device_in) ||
1345 !strncmp("any", ctx.device_in, strlen(ctx.device_in)))) {
1346 if (!ctx.rfraw)
1347 ctx.link_type = pcap_devtype_to_linktype(ctx.device_in);
1348 if (!ctx.device_out) {
1349 ctx.dump = 0;
1350 main_loop = recv_only_or_dump;
1351 } else if (device_mtu(ctx.device_out)) {
1352 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1353 main_loop = receive_to_xmit;
1354 } else {
1355 ctx.dump = 1;
1356 register_signal_f(SIGALRM, timer_next_dump, SA_SIGINFO);
1357 main_loop = recv_only_or_dump;
1358 if (!ops_touched)
1359 ctx.pcap = PCAP_OPS_SG;
1361 } else {
1362 if (ctx.device_out && device_mtu(ctx.device_out)) {
1363 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1364 main_loop = pcap_to_xmit;
1365 if (!ops_touched)
1366 ctx.pcap = PCAP_OPS_MM;
1367 } else {
1368 main_loop = read_pcap;
1369 if (!ops_touched)
1370 ctx.pcap = PCAP_OPS_SG;
1374 bug_on(!main_loop);
1376 init_geoip(0);
1377 if (setsockmem)
1378 set_system_socket_memory(vals, array_size(vals));
1379 if (!ctx.enforce)
1380 xlockme();
1382 if (ctx.verbose)
1383 printf("pcap file I/O method: %s\n", pcap_ops_group_to_str[ctx.pcap]);
1385 main_loop(&ctx);
1387 if (!ctx.enforce)
1388 xunlockme();
1389 if (setsockmem)
1390 reset_system_socket_memory(vals, array_size(vals));
1391 destroy_geoip();
1393 device_restore_irq_affinity_list();
1394 tprintf_cleanup();
1396 destroy_ctx(&ctx);
1397 return 0;