trafgen: lexer/parser: fix cpu() selection and whitespacing
[netsniff-ng.git] / netsniff-ng.c
blob0f97acdd8d7742e7e8b46218d177700531fbee18
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 enum dump_mode {
41 DUMP_INTERVAL_TIME,
42 DUMP_INTERVAL_SIZE,
45 struct ctx {
46 char *device_in, *device_out, *device_trans, *filter, *prefix;
47 int cpu, rfraw, dump, print_mode, dump_dir, packet_type, verbose;
48 unsigned long kpull, dump_interval, reserve_size, tx_bytes, tx_packets;
49 bool randomize, promiscuous, enforce, jumbo, dump_bpf;
50 enum pcap_ops_groups pcap; enum dump_mode dump_mode;
51 uid_t uid; gid_t gid; uint32_t link_type, magic;
54 volatile sig_atomic_t sigint = 0;
56 static volatile bool next_dump = false;
58 static const char *short_options = "d:i:o:rf:MJt:S:k:n:b:HQmcsqXlvhF:RGAP:Vu:g:T:DB";
59 static const struct option long_options[] = {
60 {"dev", required_argument, NULL, 'd'},
61 {"in", required_argument, NULL, 'i'},
62 {"out", required_argument, NULL, 'o'},
63 {"filter", required_argument, NULL, 'f'},
64 {"num", required_argument, NULL, 'n'},
65 {"type", required_argument, NULL, 't'},
66 {"interval", required_argument, NULL, 'F'},
67 {"ring-size", required_argument, NULL, 'S'},
68 {"kernel-pull", required_argument, NULL, 'k'},
69 {"bind-cpu", required_argument, NULL, 'b'},
70 {"prefix", required_argument, NULL, 'P'},
71 {"user", required_argument, NULL, 'u'},
72 {"group", required_argument, NULL, 'g'},
73 {"magic", required_argument, NULL, 'T'},
74 {"rand", no_argument, NULL, 'r'},
75 {"rfraw", no_argument, NULL, 'R'},
76 {"mmap", no_argument, NULL, 'm'},
77 {"sg", no_argument, NULL, 'G'},
78 {"clrw", no_argument, NULL, 'c'},
79 {"jumbo-support", no_argument, NULL, 'J'},
80 {"no-promisc", no_argument, NULL, 'M'},
81 {"prio-high", no_argument, NULL, 'H'},
82 {"notouch-irq", no_argument, NULL, 'Q'},
83 {"dump-pcap-types", no_argument, NULL, 'D'},
84 {"dump-bpf", no_argument, NULL, 'B'},
85 {"silent", no_argument, NULL, 's'},
86 {"less", no_argument, NULL, 'q'},
87 {"hex", no_argument, NULL, 'X'},
88 {"ascii", no_argument, NULL, 'l'},
89 {"no-sock-mem", no_argument, NULL, 'A'},
90 {"update", no_argument, NULL, 'U'},
91 {"verbose", no_argument, NULL, 'V'},
92 {"version", no_argument, NULL, 'v'},
93 {"help", no_argument, NULL, 'h'},
94 {NULL, 0, NULL, 0}
97 static int tx_sock;
99 static struct itimerval itimer;
101 static unsigned long frame_count_max = 0, interval = TX_KERNEL_PULL_INT;
103 #define __pcap_io pcap_ops[ctx->pcap]
105 static void signal_handler(int number)
107 switch (number) {
108 case SIGINT:
109 sigint = 1;
110 case SIGHUP:
111 default:
112 break;
116 static void timer_elapsed(int unused)
118 int ret;
120 set_itimer_interval_value(&itimer, 0, interval);
122 ret = pull_and_flush_tx_ring(tx_sock);
123 if (unlikely(ret < 0)) {
124 /* We could hit EBADF if the socket has been closed before
125 * the timer was triggered.
127 if (errno != EBADF && errno != ENOBUFS)
128 panic("Flushing TX_RING failed: %s!\n", strerror(errno));
131 setitimer(ITIMER_REAL, &itimer, NULL);
134 static void timer_next_dump(int unused)
136 set_itimer_interval_value(&itimer, interval, 0);
137 next_dump = true;
138 setitimer(ITIMER_REAL, &itimer, NULL);
141 static inline bool dump_to_pcap(struct ctx *ctx)
143 return ctx->dump;
146 static void pcap_to_xmit(struct ctx *ctx)
148 __label__ out;
149 uint8_t *out = NULL;
150 int irq, ifindex, fd = 0, ret;
151 unsigned int size, it = 0;
152 unsigned long trunced = 0;
153 struct ring tx_ring;
154 struct frame_map *hdr;
155 struct sock_fprog bpf_ops;
156 struct timeval start, end, diff;
157 pcap_pkthdr_t phdr;
159 if (!device_up_and_running(ctx->device_out) && !ctx->rfraw)
160 panic("Device not up and running!\n");
162 bug_on(!__pcap_io);
164 tx_sock = pf_socket();
166 if (!strncmp("-", ctx->device_in, strlen("-"))) {
167 fd = dup(fileno(stdin));
168 close(fileno(stdin));
169 if (ctx->pcap == PCAP_OPS_MM)
170 ctx->pcap = PCAP_OPS_SG;
171 } else {
172 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
175 ret = __pcap_io->pull_fhdr_pcap(fd, &ctx->magic, &ctx->link_type);
176 if (ret)
177 panic("Error reading pcap header!\n");
179 if (__pcap_io->prepare_access_pcap) {
180 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_RD, ctx->jumbo);
181 if (ret)
182 panic("Error prepare reading pcap!\n");
185 fmemset(&tx_ring, 0, sizeof(tx_ring));
186 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
188 if (ctx->rfraw) {
189 ctx->device_trans = xstrdup(ctx->device_out);
190 xfree(ctx->device_out);
192 enter_rfmon_mac80211(ctx->device_trans, &ctx->device_out);
193 if (ctx->link_type != LINKTYPE_IEEE802_11)
194 panic("Wrong linktype of pcap!\n");
197 ifindex = device_ifindex(ctx->device_out);
199 size = ring_size(ctx->device_out, ctx->reserve_size);
201 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
202 if (ctx->dump_bpf)
203 bpf_dump_all(&bpf_ops);
205 set_packet_loss_discard(tx_sock);
206 set_sockopt_hwtimestamp(tx_sock, ctx->device_out);
208 setup_tx_ring_layout(tx_sock, &tx_ring, size, ctx->jumbo);
209 create_tx_ring(tx_sock, &tx_ring, ctx->verbose);
210 mmap_tx_ring(tx_sock, &tx_ring);
211 alloc_tx_ring_frames(&tx_ring);
212 bind_tx_ring(tx_sock, &tx_ring, ifindex);
214 dissector_init_all(ctx->print_mode);
216 if (ctx->cpu >= 0 && ifindex > 0) {
217 irq = device_irq_number(ctx->device_out);
218 device_bind_irq_to_cpu(irq, ctx->cpu);
220 if (ctx->verbose)
221 printf("IRQ: %s:%d > CPU%d\n",
222 ctx->device_out, irq, ctx->cpu);
225 if (ctx->kpull)
226 interval = ctx->kpull;
228 set_itimer_interval_value(&itimer, 0, interval);
229 setitimer(ITIMER_REAL, &itimer, NULL);
231 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
233 printf("Running! Hang up with ^C!\n\n");
234 fflush(stdout);
236 bug_on(gettimeofday(&start, NULL));
238 while (likely(sigint == 0)) {
239 while (user_may_pull_from_tx(tx_ring.frames[it].iov_base)) {
240 hdr = tx_ring.frames[it].iov_base;
241 out = ((uint8_t *) hdr) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
243 do {
244 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic, out,
245 ring_frame_size(&tx_ring));
246 if (unlikely(ret <= 0))
247 goto out;
249 if (ring_frame_size(&tx_ring) <
250 pcap_get_length(&phdr, ctx->magic)) {
251 pcap_set_length(&phdr, ctx->magic,
252 ring_frame_size(&tx_ring));
253 trunced++;
255 } while (ctx->filter &&
256 !bpf_run_filter(&bpf_ops, out,
257 pcap_get_length(&phdr, ctx->magic)));
259 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &hdr->tp_h, &hdr->s_ll);
261 ctx->tx_bytes += hdr->tp_h.tp_len;;
262 ctx->tx_packets++;
264 show_frame_hdr(hdr, ctx->print_mode);
266 dissector_entry_point(out, hdr->tp_h.tp_snaplen,
267 ctx->link_type, ctx->print_mode);
269 kernel_may_pull_from_tx(&hdr->tp_h);
271 it++;
272 if (it >= tx_ring.layout.tp_frame_nr)
273 it = 0;
275 if (unlikely(sigint == 1))
276 break;
278 if (frame_count_max != 0) {
279 if (ctx->tx_packets >= frame_count_max) {
280 sigint = 1;
281 break;
287 out:
289 bug_on(gettimeofday(&end, NULL));
290 timersub(&end, &start, &diff);
292 bpf_release(&bpf_ops);
294 dissector_cleanup_all();
295 destroy_tx_ring(tx_sock, &tx_ring);
297 if (ctx->rfraw)
298 leave_rfmon_mac80211(ctx->device_trans, ctx->device_out);
300 if (__pcap_io->prepare_close_pcap)
301 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
303 if (!strncmp("-", ctx->device_in, strlen("-")))
304 dup2(fd, fileno(stdin));
305 close(fd);
307 close(tx_sock);
309 fflush(stdout);
310 printf("\n");
311 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
312 printf("\r%12lu packets truncated in file\n", trunced);
313 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
314 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
317 static void receive_to_xmit(struct ctx *ctx)
319 short ifflags = 0;
320 uint8_t *in, *out;
321 int rx_sock, ifindex_in, ifindex_out;
322 unsigned int size_in, size_out, it_in = 0, it_out = 0;
323 unsigned long frame_count = 0;
324 struct frame_map *hdr_in, *hdr_out;
325 struct ring tx_ring, rx_ring;
326 struct pollfd rx_poll;
327 struct sock_fprog bpf_ops;
329 if (!strncmp(ctx->device_in, ctx->device_out, IFNAMSIZ))
330 panic("Ingress/egress devices must be different!\n");
331 if (!device_up_and_running(ctx->device_out))
332 panic("Egress device not up and running!\n");
333 if (!device_up_and_running(ctx->device_in))
334 panic("Ingress device not up and running!\n");
336 rx_sock = pf_socket();
337 tx_sock = pf_socket();
339 fmemset(&tx_ring, 0, sizeof(tx_ring));
340 fmemset(&rx_ring, 0, sizeof(rx_ring));
341 fmemset(&rx_poll, 0, sizeof(rx_poll));
342 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
344 ifindex_in = device_ifindex(ctx->device_in);
345 ifindex_out = device_ifindex(ctx->device_out);
347 size_in = ring_size(ctx->device_in, ctx->reserve_size);
348 size_out = ring_size(ctx->device_out, ctx->reserve_size);
350 enable_kernel_bpf_jit_compiler();
352 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
353 if (ctx->dump_bpf)
354 bpf_dump_all(&bpf_ops);
355 bpf_attach_to_sock(rx_sock, &bpf_ops);
357 setup_rx_ring_layout(rx_sock, &rx_ring, size_in, ctx->jumbo);
358 create_rx_ring(rx_sock, &rx_ring, ctx->verbose);
359 mmap_rx_ring(rx_sock, &rx_ring);
360 alloc_rx_ring_frames(&rx_ring);
361 bind_rx_ring(rx_sock, &rx_ring, ifindex_in);
362 prepare_polling(rx_sock, &rx_poll);
364 set_packet_loss_discard(tx_sock);
365 setup_tx_ring_layout(tx_sock, &tx_ring, size_out, ctx->jumbo);
366 create_tx_ring(tx_sock, &tx_ring, ctx->verbose);
367 mmap_tx_ring(tx_sock, &tx_ring);
368 alloc_tx_ring_frames(&tx_ring);
369 bind_tx_ring(tx_sock, &tx_ring, ifindex_out);
371 dissector_init_all(ctx->print_mode);
373 if (ctx->promiscuous)
374 ifflags = 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 __label__ next;
391 hdr_in = rx_ring.frames[it_in].iov_base;
392 in = ((uint8_t *) hdr_in) + hdr_in->tp_h.tp_mac;
394 frame_count++;
396 if (ctx->packet_type != -1)
397 if (ctx->packet_type != hdr_in->s_ll.sll_pkttype)
398 goto next;
400 hdr_out = tx_ring.frames[it_out].iov_base;
401 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
403 for (; !user_may_pull_from_tx(tx_ring.frames[it_out].iov_base) &&
404 likely(!sigint);) {
405 if (ctx->randomize)
406 next_rnd_slot(&it_out, &tx_ring);
407 else {
408 it_out++;
409 if (it_out >= tx_ring.layout.tp_frame_nr)
410 it_out = 0;
413 hdr_out = tx_ring.frames[it_out].iov_base;
414 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
417 tpacket_hdr_clone(&hdr_out->tp_h, &hdr_in->tp_h);
418 fmemcpy(out, in, hdr_in->tp_h.tp_len);
420 kernel_may_pull_from_tx(&hdr_out->tp_h);
421 if (ctx->randomize)
422 next_rnd_slot(&it_out, &tx_ring);
423 else {
424 it_out++;
425 if (it_out >= tx_ring.layout.tp_frame_nr)
426 it_out = 0;
429 show_frame_hdr(hdr_in, ctx->print_mode);
431 dissector_entry_point(in, hdr_in->tp_h.tp_snaplen,
432 ctx->link_type, ctx->print_mode);
434 if (frame_count_max != 0) {
435 if (frame_count >= frame_count_max) {
436 sigint = 1;
437 break;
441 next:
443 kernel_may_pull_from_rx(&hdr_in->tp_h);
445 it_in++;
446 if (it_in >= rx_ring.layout.tp_frame_nr)
447 it_in = 0;
449 if (unlikely(sigint == 1))
450 goto out;
453 poll(&rx_poll, 1, -1);
456 out:
458 sock_print_net_stats(rx_sock, 0);
460 bpf_release(&bpf_ops);
462 dissector_cleanup_all();
464 destroy_tx_ring(tx_sock, &tx_ring);
465 destroy_rx_ring(rx_sock, &rx_ring);
467 if (ctx->promiscuous)
468 leave_promiscuous_mode(ctx->device_in, ifflags);
470 close(tx_sock);
471 close(rx_sock);
474 static void translate_pcap_to_txf(int fdo, uint8_t *out, size_t len)
476 size_t bytes_done = 0;
477 char bout[80];
479 slprintf(bout, sizeof(bout), "{\n ");
480 write_or_die(fdo, bout, strlen(bout));
482 while (bytes_done < len) {
483 slprintf(bout, sizeof(bout), "0x%02x, ", out[bytes_done]);
484 write_or_die(fdo, bout, strlen(bout));
486 bytes_done++;
488 if (bytes_done % 10 == 0) {
489 slprintf(bout, sizeof(bout), "\n");
490 write_or_die(fdo, bout, strlen(bout));
492 if (bytes_done < len) {
493 slprintf(bout, sizeof(bout), " ");
494 write_or_die(fdo, bout, strlen(bout));
498 if (bytes_done % 10 != 0) {
499 slprintf(bout, sizeof(bout), "\n");
500 write_or_die(fdo, bout, strlen(bout));
503 slprintf(bout, sizeof(bout), "}\n\n");
504 write_or_die(fdo, bout, strlen(bout));
507 static void read_pcap(struct ctx *ctx)
509 __label__ out;
510 uint8_t *out;
511 int ret, fd, fdo = 0;
512 unsigned long trunced = 0;
513 size_t out_len;
514 pcap_pkthdr_t phdr;
515 struct sock_fprog bpf_ops;
516 struct frame_map fm;
517 struct timeval start, end, diff;
518 struct sockaddr_ll sll;
520 bug_on(!__pcap_io);
522 if (!strncmp("-", ctx->device_in, strlen("-"))) {
523 fd = dup(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 ret = __pcap_io->pull_fhdr_pcap(fd, &ctx->magic, &ctx->link_type);
532 if (ret)
533 panic("Error reading pcap header!\n");
535 if (__pcap_io->prepare_access_pcap) {
536 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_RD, ctx->jumbo);
537 if (ret)
538 panic("Error prepare reading pcap!\n");
541 fmemset(&fm, 0, sizeof(fm));
542 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
544 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
545 if (ctx->dump_bpf)
546 bpf_dump_all(&bpf_ops);
548 dissector_init_all(ctx->print_mode);
550 out_len = round_up(1024 * 1024, PAGE_SIZE);
551 out = xmalloc_aligned(out_len, CO_CACHE_LINE_SIZE);
553 if (ctx->device_out) {
554 if (!strncmp("-", ctx->device_out, strlen("-"))) {
555 fdo = dup(fileno(stdout));
556 close(fileno(stdout));
557 } else {
558 fdo = open_or_die_m(ctx->device_out, O_RDWR | O_CREAT |
559 O_TRUNC | O_LARGEFILE, DEFFILEMODE);
563 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
565 printf("Running! Hang up with ^C!\n\n");
566 fflush(stdout);
568 bug_on(gettimeofday(&start, NULL));
570 while (likely(sigint == 0)) {
571 do {
572 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic,
573 out, out_len);
574 if (unlikely(ret < 0))
575 goto out;
577 if (unlikely(pcap_get_length(&phdr, ctx->magic) == 0)) {
578 trunced++;
579 continue;
582 if (unlikely(pcap_get_length(&phdr, ctx->magic) > out_len)) {
583 pcap_set_length(&phdr, ctx->magic, out_len);
584 trunced++;
586 } while (ctx->filter &&
587 !bpf_run_filter(&bpf_ops, out,
588 pcap_get_length(&phdr, ctx->magic)));
590 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &fm.tp_h, &sll);
592 ctx->tx_bytes += fm.tp_h.tp_len;
593 ctx->tx_packets++;
595 show_frame_hdr(&fm, ctx->print_mode);
597 dissector_entry_point(out, fm.tp_h.tp_snaplen,
598 ctx->link_type, ctx->print_mode);
600 if (ctx->device_out)
601 translate_pcap_to_txf(fdo, out, fm.tp_h.tp_snaplen);
603 if (frame_count_max != 0) {
604 if (ctx->tx_packets >= frame_count_max) {
605 sigint = 1;
606 break;
611 out:
613 bug_on(gettimeofday(&end, NULL));
614 timersub(&end, &start, &diff);
616 bpf_release(&bpf_ops);
618 dissector_cleanup_all();
620 if (__pcap_io->prepare_close_pcap)
621 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
623 xfree(out);
625 fflush(stdout);
626 printf("\n");
627 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
628 printf("\r%12lu packets truncated in file\n", trunced);
629 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
630 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
632 if (!strncmp("-", ctx->device_in, strlen("-")))
633 dup2(fd, fileno(stdin));
634 close(fd);
636 if (ctx->device_out) {
637 if (!strncmp("-", ctx->device_out, strlen("-")))
638 dup2(fdo, fileno(stdout));
639 close(fdo);
643 static void finish_multi_pcap_file(struct ctx *ctx, int fd)
645 __pcap_io->fsync_pcap(fd);
647 if (__pcap_io->prepare_close_pcap)
648 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
650 close(fd);
652 fmemset(&itimer, 0, sizeof(itimer));
653 setitimer(ITIMER_REAL, &itimer, NULL);
656 static int next_multi_pcap_file(struct ctx *ctx, int fd)
658 int ret;
659 char fname[512];
661 __pcap_io->fsync_pcap(fd);
663 if (__pcap_io->prepare_close_pcap)
664 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
666 close(fd);
668 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
669 ctx->prefix ? : "dump-", time(0));
671 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
672 O_LARGEFILE, DEFFILEMODE);
674 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
675 if (ret)
676 panic("Error writing pcap header!\n");
678 if (__pcap_io->prepare_access_pcap) {
679 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, ctx->jumbo);
680 if (ret)
681 panic("Error prepare writing pcap!\n");
684 return fd;
687 static int begin_multi_pcap_file(struct ctx *ctx)
689 int fd, ret;
690 char fname[256];
692 bug_on(!__pcap_io);
694 if (ctx->device_out[strlen(ctx->device_out) - 1] == '/')
695 ctx->device_out[strlen(ctx->device_out) - 1] = 0;
697 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
698 ctx->prefix ? : "dump-", time(0));
700 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
701 O_LARGEFILE, DEFFILEMODE);
703 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
704 if (ret)
705 panic("Error writing pcap header!\n");
707 if (__pcap_io->prepare_access_pcap) {
708 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, ctx->jumbo);
709 if (ret)
710 panic("Error prepare writing pcap!\n");
713 if (ctx->dump_mode == DUMP_INTERVAL_TIME) {
714 interval = ctx->dump_interval;
716 set_itimer_interval_value(&itimer, interval, 0);
717 setitimer(ITIMER_REAL, &itimer, NULL);
718 } else {
719 interval = 0;
722 return fd;
725 static void finish_single_pcap_file(struct ctx *ctx, int fd)
727 __pcap_io->fsync_pcap(fd);
729 if (__pcap_io->prepare_close_pcap)
730 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
732 if (strncmp("-", ctx->device_out, strlen("-")))
733 close(fd);
734 else
735 dup2(fd, fileno(stdout));
738 static int begin_single_pcap_file(struct ctx *ctx)
740 int fd, ret;
742 bug_on(!__pcap_io);
744 if (!strncmp("-", ctx->device_out, strlen("-"))) {
745 fd = dup(fileno(stdout));
746 close(fileno(stdout));
747 if (ctx->pcap == PCAP_OPS_MM)
748 ctx->pcap = PCAP_OPS_SG;
749 } else {
750 fd = open_or_die_m(ctx->device_out,
751 O_RDWR | O_CREAT | O_TRUNC |
752 O_LARGEFILE, DEFFILEMODE);
755 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
756 if (ret)
757 panic("Error writing pcap header!\n");
759 if (__pcap_io->prepare_access_pcap) {
760 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, ctx->jumbo);
761 if (ret)
762 panic("Error prepare writing pcap!\n");
765 return fd;
768 static void print_pcap_file_stats(int sock, struct ctx *ctx, unsigned long skipped)
770 int ret;
771 unsigned long good, bad;
772 struct tpacket_stats kstats;
773 socklen_t slen = sizeof(kstats);
775 fmemset(&kstats, 0, sizeof(kstats));
777 ret = getsockopt(sock, SOL_PACKET, PACKET_STATISTICS, &kstats, &slen);
778 if (unlikely(ret))
779 panic("Cannot get packet statistics!\n");
781 if (ctx->print_mode == PRINT_NONE) {
782 good = kstats.tp_packets - kstats.tp_drops - skipped;
783 bad = kstats.tp_drops + skipped;
785 printf(".(+%lu/-%lu)", good, bad);
786 fflush(stdout);
790 static void recv_only_or_dump(struct ctx *ctx)
792 uint8_t *packet;
793 short ifflags = 0;
794 int sock, irq, ifindex, fd = 0, ret;
795 unsigned int size, it = 0;
796 unsigned long frame_count = 0, skipped = 0;
797 struct ring rx_ring;
798 struct pollfd rx_poll;
799 struct frame_map *hdr;
800 struct sock_fprog bpf_ops;
801 struct timeval start, end, diff;
802 pcap_pkthdr_t phdr;
804 if (!device_up_and_running(ctx->device_in) && !ctx->rfraw)
805 panic("Device not up and running!\n");
807 sock = pf_socket();
809 if (ctx->rfraw) {
810 ctx->device_trans = xstrdup(ctx->device_in);
811 xfree(ctx->device_in);
813 enter_rfmon_mac80211(ctx->device_trans, &ctx->device_in);
814 ctx->link_type = LINKTYPE_IEEE802_11;
817 fmemset(&rx_ring, 0, sizeof(rx_ring));
818 fmemset(&rx_poll, 0, sizeof(rx_poll));
819 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
821 ifindex = device_ifindex(ctx->device_in);
823 size = ring_size(ctx->device_in, ctx->reserve_size);
825 enable_kernel_bpf_jit_compiler();
827 bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
828 if (ctx->dump_bpf)
829 bpf_dump_all(&bpf_ops);
830 bpf_attach_to_sock(sock, &bpf_ops);
832 set_sockopt_hwtimestamp(sock, ctx->device_in);
834 setup_rx_ring_layout(sock, &rx_ring, size, ctx->jumbo);
835 create_rx_ring(sock, &rx_ring, ctx->verbose);
836 mmap_rx_ring(sock, &rx_ring);
837 alloc_rx_ring_frames(&rx_ring);
838 bind_rx_ring(sock, &rx_ring, ifindex);
840 prepare_polling(sock, &rx_poll);
841 dissector_init_all(ctx->print_mode);
843 if (ctx->cpu >= 0 && ifindex > 0) {
844 irq = device_irq_number(ctx->device_in);
845 device_bind_irq_to_cpu(irq, ctx->cpu);
847 if (ctx->verbose)
848 printf("IRQ: %s:%d > CPU%d\n",
849 ctx->device_in, irq, ctx->cpu);
852 if (ctx->promiscuous)
853 ifflags = enter_promiscuous_mode(ctx->device_in);
855 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
857 if (dump_to_pcap(ctx)) {
858 __label__ try_file;
859 struct stat stats;
861 fmemset(&stats, 0, sizeof(stats));
862 ret = stat(ctx->device_out, &stats);
863 if (ret < 0) {
864 ctx->dump_dir = 0;
865 goto try_file;
868 ctx->dump_dir = S_ISDIR(stats.st_mode);
869 if (ctx->dump_dir) {
870 fd = begin_multi_pcap_file(ctx);
871 } else {
872 try_file:
873 fd = begin_single_pcap_file(ctx);
877 printf("Running! Hang up with ^C!\n\n");
878 fflush(stdout);
880 bug_on(gettimeofday(&start, NULL));
882 while (likely(sigint == 0)) {
883 while (user_may_pull_from_rx(rx_ring.frames[it].iov_base)) {
884 __label__ next;
886 hdr = rx_ring.frames[it].iov_base;
887 packet = ((uint8_t *) hdr) + hdr->tp_h.tp_mac;
888 frame_count++;
890 if (ctx->packet_type != -1)
891 if (ctx->packet_type != hdr->s_ll.sll_pkttype)
892 goto next;
894 if (unlikely(ring_frame_size(&rx_ring) < hdr->tp_h.tp_snaplen)) {
895 skipped++;
896 goto next;
899 if (dump_to_pcap(ctx)) {
900 tpacket_hdr_to_pcap_pkthdr(&hdr->tp_h, &hdr->s_ll, &phdr, ctx->magic);
902 ret = __pcap_io->write_pcap(fd, &phdr, ctx->magic, packet,
903 pcap_get_length(&phdr, ctx->magic));
904 if (unlikely(ret != pcap_get_total_length(&phdr, ctx->magic)))
905 panic("Write error to pcap!\n");
908 show_frame_hdr(hdr, ctx->print_mode);
910 dissector_entry_point(packet, hdr->tp_h.tp_snaplen,
911 ctx->link_type, ctx->print_mode);
913 if (frame_count_max != 0) {
914 if (frame_count >= frame_count_max) {
915 sigint = 1;
916 break;
920 next:
922 kernel_may_pull_from_rx(&hdr->tp_h);
924 it++;
925 if (it >= rx_ring.layout.tp_frame_nr)
926 it = 0;
928 if (unlikely(sigint == 1))
929 break;
931 if (dump_to_pcap(ctx)) {
932 if (ctx->dump_mode == DUMP_INTERVAL_SIZE) {
933 interval += hdr->tp_h.tp_snaplen;
935 if (interval > ctx->dump_interval) {
936 next_dump = true;
937 interval = 0;
941 if (next_dump) {
942 fd = next_multi_pcap_file(ctx, fd);
943 next_dump = false;
945 if (ctx->verbose)
946 print_pcap_file_stats(sock, ctx, skipped);
951 poll(&rx_poll, 1, -1);
954 bug_on(gettimeofday(&end, NULL));
955 timersub(&end, &start, &diff);
957 if (!(ctx->dump_dir && ctx->print_mode == PRINT_NONE)) {
958 sock_print_net_stats(sock, skipped);
960 printf("\r%12lu sec, %lu usec in total\n",
961 diff.tv_sec, diff.tv_usec);
962 } else {
963 printf("\n\n");
964 fflush(stdout);
967 bpf_release(&bpf_ops);
968 dissector_cleanup_all();
969 destroy_rx_ring(sock, &rx_ring);
971 if (ctx->promiscuous)
972 leave_promiscuous_mode(ctx->device_in, ifflags);
974 if (ctx->rfraw)
975 leave_rfmon_mac80211(ctx->device_trans, ctx->device_in);
977 if (dump_to_pcap(ctx)) {
978 if (ctx->dump_dir)
979 finish_multi_pcap_file(ctx, fd);
980 else
981 finish_single_pcap_file(ctx, fd);
984 close(sock);
987 static void help(void)
989 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING);
990 puts("http://www.netsniff-ng.org\n\n"
991 "Usage: netsniff-ng [options] [filter-expression]\n"
992 "Options:\n"
993 " -i|-d|--dev|--in <dev|pcap|-> Input source as netdev, pcap or pcap stdin\n"
994 " -o|--out <dev|pcap|dir|cfg|-> Output sink as netdev, pcap, directory, trafgen, or stdout\n"
995 " -f|--filter <bpf-file|expr> Use BPF filter file from bpfc or tcpdump-like expression\n"
996 " -t|--type <type> Filter for: host|broadcast|multicast|others|outgoing\n"
997 " -F|--interval <size|time> Dump interval if -o is a dir: <num>KiB/MiB/GiB/s/sec/min/hrs\n"
998 " -J|--jumbo-support Support for 64KB Super Jumbo Frames (def: 2048B)\n"
999 " -R|--rfraw Capture or inject raw 802.11 frames\n"
1000 " -n|--num <0|uint> Number of packets until exit (def: 0)\n"
1001 " -P|--prefix <name> Prefix for pcaps stored in directory\n"
1002 " -T|--magic <pcap-magic> Pcap magic number/pcap format to store, see -D\n"
1003 " -D|--dump-pcap-types Dump pcap types and magic numbers and quit\n"
1004 " -B|--dump-bpf Dump generated BPF assembly\n"
1005 " -r|--rand Randomize packet forwarding order (dev->dev)\n"
1006 " -M|--no-promisc No promiscuous mode for netdev\n"
1007 " -A|--no-sock-mem Don't tune core socket memory\n"
1008 " -m|--mmap Mmap(2) pcap file i.e., for replaying pcaps\n"
1009 " -G|--sg Scatter/gather pcap file I/O\n"
1010 " -c|--clrw Use slower read(2)/write(2) I/O\n"
1011 " -S|--ring-size <size> Specify ring size to: <num>KiB/MiB/GiB\n"
1012 " -k|--kernel-pull <uint> Kernel pull from user interval in us (def: 10us)\n"
1013 " -b|--bind-cpu <cpu> Bind to specific CPU\n"
1014 " -u|--user <userid> Drop privileges and change to userid\n"
1015 " -g|--group <groupid> Drop privileges and change to groupid\n"
1016 " -H|--prio-high Make this high priority process\n"
1017 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
1018 " -s|--silent Do not print captured packets\n"
1019 " -q|--less Print less-verbose packet information\n"
1020 " -X|--hex Print packet data in hex format\n"
1021 " -l|--ascii Print human-readable packet data\n"
1022 " -U|--update Update GeoIP databases\n"
1023 " -V|--verbose Be more verbose\n"
1024 " -v|--version Show version\n"
1025 " -h|--help Guess what?!\n\n"
1026 "Examples:\n"
1027 " netsniff-ng --in eth0 --out dump.pcap -s -T 0xa1b2c3d4 --b 0 tcp or udp\n"
1028 " netsniff-ng --in wlan0 --rfraw --out dump.pcap --silent --bind-cpu 0\n"
1029 " netsniff-ng --in dump.pcap --mmap --out eth0 -k1000 --silent --bind-cpu 0\n"
1030 " netsniff-ng --in dump.pcap --out dump.cfg --silent --bind-cpu 0\n"
1031 " netsniff-ng --in eth0 --out eth1 --silent --bind-cpu 0 --type host\n"
1032 " netsniff-ng --in eth1 --out /opt/probe/ -s -m -J --interval 100MiB -b 0\n"
1033 " netsniff-ng --in vlan0 --out dump.pcap -c -u `id -u bob` -g `id -g bob`\n"
1034 " netsniff-ng --in any --filter http.bpf --jumbo-support --ascii -V\n\n"
1035 "Note:\n"
1036 " For introducing bit errors, delays with random variation and more\n"
1037 " while replaying pcaps, make use of tc(8) with its disciplines (e.g. netem).\n\n"
1038 "Please report bugs to <bugs@netsniff-ng.org>\n"
1039 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1040 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1041 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1042 "Swiss federal institute of technology (ETH Zurich)\n"
1043 "License: GNU GPL version 2.0\n"
1044 "This is free software: you are free to change and redistribute it.\n"
1045 "There is NO WARRANTY, to the extent permitted by law.\n");
1046 die();
1049 static void version(void)
1051 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING);
1052 puts("http://www.netsniff-ng.org\n\n"
1053 "Please report bugs to <bugs@netsniff-ng.org>\n"
1054 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
1055 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>\n"
1056 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1057 "Swiss federal institute of technology (ETH Zurich)\n"
1058 "License: GNU GPL version 2.0\n"
1059 "This is free software: you are free to change and redistribute it.\n"
1060 "There is NO WARRANTY, to the extent permitted by law.\n");
1061 die();
1064 int main(int argc, char **argv)
1066 char *ptr;
1067 int c, i, j, cpu_tmp, opt_index, ops_touched = 0, vals[4] = {0};
1068 bool prio_high = false, setsockmem = true;
1069 void (*main_loop)(struct ctx *ctx) = NULL;
1070 struct ctx ctx = {
1071 .link_type = LINKTYPE_EN10MB,
1072 .print_mode = PRINT_NORM,
1073 .cpu = -1,
1074 .packet_type = -1,
1075 .promiscuous = true,
1076 .randomize = false,
1077 .pcap = PCAP_OPS_SG,
1078 .dump_interval = 60,
1079 .dump_mode = DUMP_INTERVAL_TIME,
1080 .uid = getuid(),
1081 .gid = getgid(),
1082 .magic = ORIGINAL_TCPDUMP_MAGIC,
1085 srand(time(NULL));
1087 while ((c = getopt_long(argc, argv, short_options, long_options,
1088 &opt_index)) != EOF) {
1089 switch (c) {
1090 case 'd':
1091 case 'i':
1092 ctx.device_in = xstrdup(optarg);
1093 break;
1094 case 'o':
1095 ctx.device_out = xstrdup(optarg);
1096 break;
1097 case 'P':
1098 ctx.prefix = xstrdup(optarg);
1099 break;
1100 case 'R':
1101 ctx.link_type = LINKTYPE_IEEE802_11;
1102 ctx.rfraw = 1;
1103 break;
1104 case 'r':
1105 ctx.randomize = true;
1106 break;
1107 case 'J':
1108 ctx.jumbo = true;
1109 break;
1110 case 'T':
1111 ctx.magic = (uint32_t) strtoul(optarg, NULL, 0);
1112 pcap_check_magic(ctx.magic);
1113 break;
1114 case 'f':
1115 ctx.filter = xstrdup(optarg);
1116 break;
1117 case 'M':
1118 ctx.promiscuous = false;
1119 break;
1120 case 'A':
1121 setsockmem = false;
1122 break;
1123 case 'u':
1124 ctx.uid = strtoul(optarg, NULL, 0);
1125 ctx.enforce = true;
1126 break;
1127 case 'g':
1128 ctx.gid = strtoul(optarg, NULL, 0);
1129 ctx.enforce = true;
1130 break;
1131 case 't':
1132 if (!strncmp(optarg, "host", strlen("host")))
1133 ctx.packet_type = PACKET_HOST;
1134 else if (!strncmp(optarg, "broadcast", strlen("broadcast")))
1135 ctx.packet_type = PACKET_BROADCAST;
1136 else if (!strncmp(optarg, "multicast", strlen("multicast")))
1137 ctx.packet_type = PACKET_MULTICAST;
1138 else if (!strncmp(optarg, "others", strlen("others")))
1139 ctx.packet_type = PACKET_OTHERHOST;
1140 else if (!strncmp(optarg, "outgoing", strlen("outgoing")))
1141 ctx.packet_type = PACKET_OUTGOING;
1142 else
1143 ctx.packet_type = -1;
1144 break;
1145 case 'S':
1146 ptr = optarg;
1147 ctx.reserve_size = 0;
1149 for (j = i = strlen(optarg); i > 0; --i) {
1150 if (!isdigit(optarg[j - i]))
1151 break;
1152 ptr++;
1155 if (!strncmp(ptr, "KiB", strlen("KiB")))
1156 ctx.reserve_size = 1 << 10;
1157 else if (!strncmp(ptr, "MiB", strlen("MiB")))
1158 ctx.reserve_size = 1 << 20;
1159 else if (!strncmp(ptr, "GiB", strlen("GiB")))
1160 ctx.reserve_size = 1 << 30;
1161 else
1162 panic("Syntax error in ring size param!\n");
1163 *ptr = 0;
1165 ctx.reserve_size *= strtol(optarg, NULL, 0);
1166 break;
1167 case 'b':
1168 cpu_tmp = strtol(optarg, NULL, 0);
1170 cpu_affinity(cpu_tmp);
1171 if (ctx.cpu != -2)
1172 ctx.cpu = cpu_tmp;
1173 break;
1174 case 'H':
1175 prio_high = true;
1176 break;
1177 case 'c':
1178 ctx.pcap = PCAP_OPS_RW;
1179 ops_touched = 1;
1180 break;
1181 case 'm':
1182 ctx.pcap = PCAP_OPS_MM;
1183 ops_touched = 1;
1184 break;
1185 case 'G':
1186 ctx.pcap = PCAP_OPS_SG;
1187 ops_touched = 1;
1188 break;
1189 case 'Q':
1190 ctx.cpu = -2;
1191 break;
1192 case 's':
1193 ctx.print_mode = PRINT_NONE;
1194 break;
1195 case 'q':
1196 ctx.print_mode = PRINT_LESS;
1197 break;
1198 case 'X':
1199 ctx.print_mode =
1200 (ctx.print_mode == PRINT_ASCII) ?
1201 PRINT_HEX_ASCII : PRINT_HEX;
1202 break;
1203 case 'l':
1204 ctx.print_mode =
1205 (ctx.print_mode == PRINT_HEX) ?
1206 PRINT_HEX_ASCII : PRINT_ASCII;
1207 break;
1208 case 'k':
1209 ctx.kpull = strtol(optarg, NULL, 0);
1210 break;
1211 case 'n':
1212 frame_count_max = strtol(optarg, NULL, 0);
1213 break;
1214 case 'F':
1215 ptr = optarg;
1216 ctx.dump_interval = 0;
1218 for (j = i = strlen(optarg); i > 0; --i) {
1219 if (!isdigit(optarg[j - i]))
1220 break;
1221 ptr++;
1224 if (!strncmp(ptr, "KiB", strlen("KiB"))) {
1225 ctx.dump_interval = 1 << 10;
1226 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1227 } else if (!strncmp(ptr, "MiB", strlen("MiB"))) {
1228 ctx.dump_interval = 1 << 20;
1229 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1230 } else if (!strncmp(ptr, "GiB", strlen("GiB"))) {
1231 ctx.dump_interval = 1 << 30;
1232 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1233 } else if (!strncmp(ptr, "sec", strlen("sec"))) {
1234 ctx.dump_interval = 1;
1235 ctx.dump_mode = DUMP_INTERVAL_TIME;
1236 } else if (!strncmp(ptr, "min", strlen("min"))) {
1237 ctx.dump_interval = 60;
1238 ctx.dump_mode = DUMP_INTERVAL_TIME;
1239 } else if (!strncmp(ptr, "hrs", strlen("hrs"))) {
1240 ctx.dump_interval = 60 * 60;
1241 ctx.dump_mode = DUMP_INTERVAL_TIME;
1242 } else if (!strncmp(ptr, "s", strlen("s"))) {
1243 ctx.dump_interval = 1;
1244 ctx.dump_mode = DUMP_INTERVAL_TIME;
1245 } else {
1246 panic("Syntax error in time/size param!\n");
1249 *ptr = 0;
1250 ctx.dump_interval *= strtol(optarg, NULL, 0);
1251 break;
1252 case 'V':
1253 ctx.verbose = 1;
1254 break;
1255 case 'B':
1256 ctx.dump_bpf = true;
1257 break;
1258 case 'D':
1259 pcap_dump_type_features();
1260 die();
1261 break;
1262 case 'U':
1263 update_geoip();
1264 die();
1265 break;
1266 case 'v':
1267 version();
1268 break;
1269 case 'h':
1270 help();
1271 break;
1272 case '?':
1273 switch (optopt) {
1274 case 'd':
1275 case 'i':
1276 case 'o':
1277 case 'f':
1278 case 't':
1279 case 'P':
1280 case 'F':
1281 case 'n':
1282 case 'S':
1283 case 'b':
1284 case 'k':
1285 case 'T':
1286 case 'u':
1287 case 'g':
1288 case 'e':
1289 panic("Option -%c requires an argument!\n",
1290 optopt);
1291 default:
1292 if (isprint(optopt))
1293 printf("Unknown option character `0x%X\'!\n", optopt);
1294 die();
1296 default:
1297 break;
1301 if (!ctx.filter && optind != argc) {
1302 int ret;
1303 off_t offset = 0;
1305 for (i = optind; i < argc; ++i) {
1306 size_t alen = strlen(argv[i]) + 2;
1307 size_t flen = ctx.filter ? strlen(ctx.filter) : 0;
1309 ctx.filter = xrealloc(ctx.filter, 1, flen + alen);
1310 ret = slprintf(ctx.filter + offset, strlen(argv[i]) + 2, "%s ", argv[i]);
1311 if (ret < 0)
1312 panic("Cannot concatenate filter string!\n");
1313 else
1314 offset += ret;
1318 if (!ctx.device_in)
1319 ctx.device_in = xstrdup("any");
1321 register_signal(SIGINT, signal_handler);
1322 register_signal(SIGHUP, signal_handler);
1324 tprintf_init();
1326 if (prio_high) {
1327 set_proc_prio(get_default_proc_prio());
1328 set_sched_status(get_default_sched_policy(), get_default_sched_prio());
1331 if (ctx.device_in && (device_mtu(ctx.device_in) ||
1332 !strncmp("any", ctx.device_in, strlen(ctx.device_in)))) {
1333 if (!ctx.device_out) {
1334 ctx.dump = 0;
1335 main_loop = recv_only_or_dump;
1336 } else if (device_mtu(ctx.device_out)) {
1337 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1338 main_loop = receive_to_xmit;
1339 } else {
1340 ctx.dump = 1;
1341 register_signal_f(SIGALRM, timer_next_dump, SA_SIGINFO);
1342 main_loop = recv_only_or_dump;
1343 if (!ops_touched)
1344 ctx.pcap = PCAP_OPS_SG;
1346 } else {
1347 if (ctx.device_out && device_mtu(ctx.device_out)) {
1348 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1349 main_loop = pcap_to_xmit;
1350 if (!ops_touched)
1351 ctx.pcap = PCAP_OPS_MM;
1352 } else {
1353 main_loop = read_pcap;
1354 if (!ops_touched)
1355 ctx.pcap = PCAP_OPS_SG;
1359 bug_on(!main_loop);
1361 init_geoip(0);
1362 if (setsockmem)
1363 set_system_socket_memory(vals, array_size(vals));
1364 xlockme();
1366 main_loop(&ctx);
1368 xunlockme();
1369 if (setsockmem)
1370 reset_system_socket_memory(vals, array_size(vals));
1371 destroy_geoip();
1373 tprintf_cleanup();
1375 free(ctx.device_in);
1376 free(ctx.device_out);
1377 free(ctx.device_trans);
1378 free(ctx.prefix);
1380 return 0;