stddef: add some more useful definitions
[netsniff-ng.git] / netsniff-ng.c
blob2a99e43881f5c8193ddd91139e3945eb80d76c91
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2009-2013 Daniel Borkmann.
5 * Copyright 2010 Emmanuel Roullit.
6 * Subject to the GPL, version 2.
8 * The first sniffer that invoked both, the zero-copy RX_RING as well as
9 * the zero-copy TX_RING for high-performance network I/O and scatter/gather
10 * or mmaped PCAP I/O.
12 * "I knew that danger lay ahead, of course; but I did not expect to
13 * meet it in our own Shire. Can't a hobbit walk from the Water to the
14 * River in peace?" "But it is not your own Shire," said Gildor. "Others
15 * dwelt here before hobbits were; and others will dwell here again when
16 * hobbits are no more. The wide world is all about you: you can fence
17 * yourselves in, but you cannot for ever fence it out."
19 * -- The Lord of the Rings, Gildor to Frodo,
20 * Chapter 'Three is Company'.
23 #define _GNU_SOURCE
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <signal.h>
27 #include <getopt.h>
28 #include <ctype.h>
29 #include <time.h>
30 #include <string.h>
31 #include <sys/socket.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <sys/time.h>
35 #include <sys/fsuid.h>
36 #include <unistd.h>
37 #include <stdbool.h>
38 #include <pthread.h>
39 #include <fcntl.h>
41 #include "ring_rx.h"
42 #include "ring_tx.h"
43 #include "mac80211.h"
44 #include "xutils.h"
45 #include "built_in.h"
46 #include "pcap.h"
47 #include "bpf.h"
48 #include "xio.h"
49 #include "die.h"
50 #include "tprintf.h"
51 #include "dissector.h"
52 #include "xmalloc.h"
54 enum dump_mode {
55 DUMP_INTERVAL_TIME,
56 DUMP_INTERVAL_SIZE,
59 struct ctx {
60 char *device_in, *device_out, *device_trans, *filter, *prefix;
61 int cpu, rfraw, dump, print_mode, dump_dir, packet_type, verbose;
62 unsigned long kpull, dump_interval, reserve_size, tx_bytes, tx_packets;
63 bool randomize, promiscuous, enforce, jumbo, dump_bpf;
64 enum pcap_ops_groups pcap; enum dump_mode dump_mode;
65 uid_t uid; gid_t gid; uint32_t link_type, magic;
68 volatile sig_atomic_t sigint = 0;
70 static volatile bool next_dump = false;
72 static const char *short_options = "d:i:o:rf:MJt:S:k:n:b:HQmcsqXlvhF:RGAP:Vu:g:T:DB";
73 static const struct option long_options[] = {
74 {"dev", required_argument, NULL, 'd'},
75 {"in", required_argument, NULL, 'i'},
76 {"out", required_argument, NULL, 'o'},
77 {"filter", required_argument, NULL, 'f'},
78 {"num", required_argument, NULL, 'n'},
79 {"type", required_argument, NULL, 't'},
80 {"interval", required_argument, NULL, 'F'},
81 {"ring-size", required_argument, NULL, 'S'},
82 {"kernel-pull", required_argument, NULL, 'k'},
83 {"bind-cpu", required_argument, NULL, 'b'},
84 {"prefix", required_argument, NULL, 'P'},
85 {"user", required_argument, NULL, 'u'},
86 {"group", required_argument, NULL, 'g'},
87 {"magic", required_argument, NULL, 'T'},
88 {"rand", no_argument, NULL, 'r'},
89 {"rfraw", no_argument, NULL, 'R'},
90 {"mmap", no_argument, NULL, 'm'},
91 {"sg", no_argument, NULL, 'G'},
92 {"clrw", no_argument, NULL, 'c'},
93 {"jumbo-support", no_argument, NULL, 'J'},
94 {"no-promisc", no_argument, NULL, 'M'},
95 {"prio-high", no_argument, NULL, 'H'},
96 {"notouch-irq", no_argument, NULL, 'Q'},
97 {"dump-pcap-types", no_argument, NULL, 'D'},
98 {"dump-bpf", no_argument, NULL, 'B'},
99 {"silent", no_argument, NULL, 's'},
100 {"less", no_argument, NULL, 'q'},
101 {"hex", no_argument, NULL, 'X'},
102 {"ascii", no_argument, NULL, 'l'},
103 {"no-sock-mem", no_argument, NULL, 'A'},
104 {"verbose", no_argument, NULL, 'V'},
105 {"version", no_argument, NULL, 'v'},
106 {"help", no_argument, NULL, 'h'},
107 {NULL, 0, NULL, 0}
110 static int tx_sock;
112 static struct itimerval itimer;
114 static unsigned long frame_count_max = 0, interval = TX_KERNEL_PULL_INT;
116 #define __pcap_io pcap_ops[ctx->pcap]
118 static void signal_handler(int number)
120 switch (number) {
121 case SIGINT:
122 sigint = 1;
123 case SIGHUP:
124 default:
125 break;
129 static void timer_elapsed(int unused)
131 set_itimer_interval_value(&itimer, 0, interval);
132 pull_and_flush_tx_ring(tx_sock);
133 setitimer(ITIMER_REAL, &itimer, NULL);
136 static void timer_next_dump(int unused)
138 set_itimer_interval_value(&itimer, 0, interval);
139 next_dump = true;
140 setitimer(ITIMER_REAL, &itimer, NULL);
143 static inline bool dump_to_pcap(struct ctx *ctx)
145 return ctx->dump;
148 static void pcap_to_xmit(struct ctx *ctx)
150 __label__ out;
151 uint8_t *out = NULL;
152 int irq, ifindex, fd = 0, ret;
153 unsigned int size, it = 0;
154 unsigned long trunced = 0;
155 struct ring tx_ring;
156 struct frame_map *hdr;
157 struct sock_fprog bpf_ops;
158 struct timeval start, end, diff;
159 pcap_pkthdr_t phdr;
161 if (!device_up_and_running(ctx->device_out) && !ctx->rfraw)
162 panic("Device not up and running!\n");
164 bug_on(!__pcap_io);
166 tx_sock = pf_socket();
168 if (!strncmp("-", ctx->device_in, strlen("-"))) {
169 fd = dup(fileno(stdin));
170 close(fileno(stdin));
171 if (ctx->pcap == PCAP_OPS_MM)
172 ctx->pcap = PCAP_OPS_SG;
173 } else {
174 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
177 ret = __pcap_io->pull_fhdr_pcap(fd, &ctx->magic, &ctx->link_type);
178 if (ret)
179 panic("Error reading pcap header!\n");
181 if (__pcap_io->prepare_access_pcap) {
182 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_RD, ctx->jumbo);
183 if (ret)
184 panic("Error prepare reading pcap!\n");
187 fmemset(&tx_ring, 0, sizeof(tx_ring));
188 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
190 if (ctx->rfraw) {
191 ctx->device_trans = xstrdup(ctx->device_out);
192 xfree(ctx->device_out);
194 enter_rfmon_mac80211(ctx->device_trans, &ctx->device_out);
195 if (ctx->link_type != LINKTYPE_IEEE802_11)
196 panic("Wrong linktype of pcap!\n");
199 ifindex = device_ifindex(ctx->device_out);
201 size = ring_size(ctx->device_out, ctx->reserve_size);
203 bpf_parse_rules(ctx->device_out, ctx->filter, &bpf_ops);
204 if (ctx->dump_bpf)
205 bpf_dump_all(&bpf_ops);
207 set_packet_loss_discard(tx_sock);
208 set_sockopt_hwtimestamp(tx_sock, ctx->device_out);
210 setup_tx_ring_layout(tx_sock, &tx_ring, size, ctx->jumbo);
211 create_tx_ring(tx_sock, &tx_ring, ctx->verbose);
212 mmap_tx_ring(tx_sock, &tx_ring);
213 alloc_tx_ring_frames(&tx_ring);
214 bind_tx_ring(tx_sock, &tx_ring, ifindex);
216 dissector_init_all(ctx->print_mode);
218 if (ctx->cpu >= 0 && ifindex > 0) {
219 irq = device_irq_number(ctx->device_out);
220 device_bind_irq_to_cpu(irq, ctx->cpu);
222 if (ctx->verbose)
223 printf("IRQ: %s:%d > CPU%d\n",
224 ctx->device_out, irq, ctx->cpu);
227 if (ctx->kpull)
228 interval = ctx->kpull;
230 set_itimer_interval_value(&itimer, 0, interval);
231 setitimer(ITIMER_REAL, &itimer, NULL);
233 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
235 printf("Running! Hang up with ^C!\n\n");
236 fflush(stdout);
238 bug_on(gettimeofday(&start, NULL));
240 while (likely(sigint == 0)) {
241 while (user_may_pull_from_tx(tx_ring.frames[it].iov_base)) {
242 hdr = tx_ring.frames[it].iov_base;
243 out = ((uint8_t *) hdr) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
245 do {
246 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic, out,
247 ring_frame_size(&tx_ring));
248 if (unlikely(ret <= 0))
249 goto out;
251 if (ring_frame_size(&tx_ring) <
252 pcap_get_length(&phdr, ctx->magic)) {
253 pcap_set_length(&phdr, ctx->magic,
254 ring_frame_size(&tx_ring));
255 trunced++;
257 } while (ctx->filter &&
258 !bpf_run_filter(&bpf_ops, out,
259 pcap_get_length(&phdr, ctx->magic)));
261 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &hdr->tp_h, &hdr->s_ll);
263 ctx->tx_bytes += hdr->tp_h.tp_len;;
264 ctx->tx_packets++;
266 show_frame_hdr(hdr, ctx->print_mode);
268 dissector_entry_point(out, hdr->tp_h.tp_snaplen,
269 ctx->link_type, ctx->print_mode);
271 kernel_may_pull_from_tx(&hdr->tp_h);
273 it++;
274 if (it >= tx_ring.layout.tp_frame_nr)
275 it = 0;
277 if (unlikely(sigint == 1))
278 break;
280 if (frame_count_max != 0) {
281 if (ctx->tx_packets >= frame_count_max) {
282 sigint = 1;
283 break;
289 out:
291 bug_on(gettimeofday(&end, NULL));
292 diff = tv_subtract(end, start);
294 bpf_release(&bpf_ops);
296 dissector_cleanup_all();
297 destroy_tx_ring(tx_sock, &tx_ring);
299 if (ctx->rfraw)
300 leave_rfmon_mac80211(ctx->device_trans, ctx->device_out);
302 if (__pcap_io->prepare_close_pcap)
303 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
305 if (strncmp("-", ctx->device_in, strlen("-")))
306 close(fd);
307 else
308 dup2(fd, fileno(stdin));
310 close(tx_sock);
312 fflush(stdout);
313 printf("\n");
314 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
315 printf("\r%12lu packets truncated in file\n", trunced);
316 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
317 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
320 static void receive_to_xmit(struct ctx *ctx)
322 short ifflags = 0;
323 uint8_t *in, *out;
324 int rx_sock, ifindex_in, ifindex_out;
325 unsigned int size_in, size_out, it_in = 0, it_out = 0;
326 unsigned long frame_count = 0;
327 struct frame_map *hdr_in, *hdr_out;
328 struct ring tx_ring, rx_ring;
329 struct pollfd rx_poll;
330 struct sock_fprog bpf_ops;
332 if (!strncmp(ctx->device_in, ctx->device_out, IFNAMSIZ))
333 panic("Ingress/egress devices must be different!\n");
334 if (!device_up_and_running(ctx->device_out))
335 panic("Egress device not up and running!\n");
336 if (!device_up_and_running(ctx->device_in))
337 panic("Ingress device not up and running!\n");
339 rx_sock = pf_socket();
340 tx_sock = pf_socket();
342 fmemset(&tx_ring, 0, sizeof(tx_ring));
343 fmemset(&rx_ring, 0, sizeof(rx_ring));
344 fmemset(&rx_poll, 0, sizeof(rx_poll));
345 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
347 ifindex_in = device_ifindex(ctx->device_in);
348 ifindex_out = device_ifindex(ctx->device_out);
350 size_in = ring_size(ctx->device_in, ctx->reserve_size);
351 size_out = ring_size(ctx->device_out, ctx->reserve_size);
353 enable_kernel_bpf_jit_compiler();
355 bpf_parse_rules(ctx->device_in, ctx->filter, &bpf_ops);
356 if (ctx->dump_bpf)
357 bpf_dump_all(&bpf_ops);
358 bpf_attach_to_sock(rx_sock, &bpf_ops);
360 setup_rx_ring_layout(rx_sock, &rx_ring, size_in, ctx->jumbo);
361 create_rx_ring(rx_sock, &rx_ring, ctx->verbose);
362 mmap_rx_ring(rx_sock, &rx_ring);
363 alloc_rx_ring_frames(&rx_ring);
364 bind_rx_ring(rx_sock, &rx_ring, ifindex_in);
365 prepare_polling(rx_sock, &rx_poll);
367 set_packet_loss_discard(tx_sock);
368 setup_tx_ring_layout(tx_sock, &tx_ring, size_out, ctx->jumbo);
369 create_tx_ring(tx_sock, &tx_ring, ctx->verbose);
370 mmap_tx_ring(tx_sock, &tx_ring);
371 alloc_tx_ring_frames(&tx_ring);
372 bind_tx_ring(tx_sock, &tx_ring, ifindex_out);
374 dissector_init_all(ctx->print_mode);
376 if (ctx->promiscuous)
377 ifflags = enter_promiscuous_mode(ctx->device_in);
379 if (ctx->kpull)
380 interval = ctx->kpull;
382 set_itimer_interval_value(&itimer, 0, interval);
383 setitimer(ITIMER_REAL, &itimer, NULL);
385 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
387 printf("Running! Hang up with ^C!\n\n");
388 fflush(stdout);
390 while (likely(sigint == 0)) {
391 while (user_may_pull_from_rx(rx_ring.frames[it_in].iov_base)) {
392 __label__ next;
394 hdr_in = rx_ring.frames[it_in].iov_base;
395 in = ((uint8_t *) hdr_in) + hdr_in->tp_h.tp_mac;
397 frame_count++;
399 if (ctx->packet_type != -1)
400 if (ctx->packet_type != hdr_in->s_ll.sll_pkttype)
401 goto next;
403 hdr_out = tx_ring.frames[it_out].iov_base;
404 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
406 for (; !user_may_pull_from_tx(tx_ring.frames[it_out].iov_base) &&
407 likely(!sigint);) {
408 if (ctx->randomize)
409 next_rnd_slot(&it_out, &tx_ring);
410 else {
411 it_out++;
412 if (it_out >= tx_ring.layout.tp_frame_nr)
413 it_out = 0;
416 hdr_out = tx_ring.frames[it_out].iov_base;
417 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
420 tpacket_hdr_clone(&hdr_out->tp_h, &hdr_in->tp_h);
421 fmemcpy(out, in, hdr_in->tp_h.tp_len);
423 kernel_may_pull_from_tx(&hdr_out->tp_h);
424 if (ctx->randomize)
425 next_rnd_slot(&it_out, &tx_ring);
426 else {
427 it_out++;
428 if (it_out >= tx_ring.layout.tp_frame_nr)
429 it_out = 0;
432 show_frame_hdr(hdr_in, ctx->print_mode);
434 dissector_entry_point(in, hdr_in->tp_h.tp_snaplen,
435 ctx->link_type, ctx->print_mode);
437 if (frame_count_max != 0) {
438 if (frame_count >= frame_count_max) {
439 sigint = 1;
440 break;
444 next:
446 kernel_may_pull_from_rx(&hdr_in->tp_h);
448 it_in++;
449 if (it_in >= rx_ring.layout.tp_frame_nr)
450 it_in = 0;
452 if (unlikely(sigint == 1))
453 goto out;
456 poll(&rx_poll, 1, -1);
457 poll_error_maybe_die(rx_sock, &rx_poll);
460 out:
462 sock_print_net_stats(rx_sock, 0);
464 bpf_release(&bpf_ops);
466 dissector_cleanup_all();
468 destroy_tx_ring(tx_sock, &tx_ring);
469 destroy_rx_ring(rx_sock, &rx_ring);
471 if (ctx->promiscuous)
472 leave_promiscuous_mode(ctx->device_in, ifflags);
474 close(tx_sock);
475 close(rx_sock);
478 static void translate_pcap_to_txf(int fdo, uint8_t *out, size_t len)
480 size_t bytes_done = 0;
481 char bout[80];
483 slprintf(bout, sizeof(bout), "{\n ");
484 write_or_die(fdo, bout, strlen(bout));
486 while (bytes_done < len) {
487 slprintf(bout, sizeof(bout), "0x%02x, ", out[bytes_done]);
488 write_or_die(fdo, bout, strlen(bout));
490 bytes_done++;
492 if (bytes_done % 10 == 0) {
493 slprintf(bout, sizeof(bout), "\n");
494 write_or_die(fdo, bout, strlen(bout));
496 if (bytes_done < len) {
497 slprintf(bout, sizeof(bout), " ");
498 write_or_die(fdo, bout, strlen(bout));
502 if (bytes_done % 10 != 0) {
503 slprintf(bout, sizeof(bout), "\n");
504 write_or_die(fdo, bout, strlen(bout));
507 slprintf(bout, sizeof(bout), "}\n\n");
508 write_or_die(fdo, bout, strlen(bout));
511 static void read_pcap(struct ctx *ctx)
513 __label__ out;
514 uint8_t *out;
515 int ret, fd, fdo = 0;
516 unsigned long trunced = 0;
517 size_t out_len;
518 pcap_pkthdr_t phdr;
519 struct sock_fprog bpf_ops;
520 struct frame_map fm;
521 struct timeval start, end, diff;
522 struct sockaddr_ll sll;
524 bug_on(!__pcap_io);
526 if (!strncmp("-", ctx->device_in, strlen("-"))) {
527 fd = dup(fileno(stdin));
528 close(fileno(stdin));
529 if (ctx->pcap == PCAP_OPS_MM)
530 ctx->pcap = PCAP_OPS_SG;
531 } else {
532 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
535 ret = __pcap_io->pull_fhdr_pcap(fd, &ctx->magic, &ctx->link_type);
536 if (ret)
537 panic("Error reading pcap header!\n");
539 if (__pcap_io->prepare_access_pcap) {
540 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_RD, ctx->jumbo);
541 if (ret)
542 panic("Error prepare reading pcap!\n");
545 fmemset(&fm, 0, sizeof(fm));
546 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
548 bpf_parse_rules("any", ctx->filter, &bpf_ops);
549 if (ctx->dump_bpf)
550 bpf_dump_all(&bpf_ops);
552 dissector_init_all(ctx->print_mode);
554 out_len = round_up(1024 * 1024, PAGE_SIZE);
555 out = xmalloc_aligned(out_len, CO_CACHE_LINE_SIZE);
557 if (ctx->device_out) {
558 if (!strncmp("-", ctx->device_out, strlen("-"))) {
559 fdo = dup(fileno(stdout));
560 close(fileno(stdout));
561 } else {
562 fdo = open_or_die_m(ctx->device_out, O_RDWR | O_CREAT |
563 O_TRUNC | O_LARGEFILE, DEFFILEMODE);
567 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
569 printf("Running! Hang up with ^C!\n\n");
570 fflush(stdout);
572 bug_on(gettimeofday(&start, NULL));
574 while (likely(sigint == 0)) {
575 do {
576 ret = __pcap_io->read_pcap(fd, &phdr, ctx->magic,
577 out, out_len);
578 if (unlikely(ret < 0))
579 goto out;
581 if (unlikely(pcap_get_length(&phdr, ctx->magic) == 0)) {
582 trunced++;
583 continue;
586 if (unlikely(pcap_get_length(&phdr, ctx->magic) > out_len)) {
587 pcap_set_length(&phdr, ctx->magic, out_len);
588 trunced++;
590 } while (ctx->filter &&
591 !bpf_run_filter(&bpf_ops, out,
592 pcap_get_length(&phdr, ctx->magic)));
594 pcap_pkthdr_to_tpacket_hdr(&phdr, ctx->magic, &fm.tp_h, &sll);
596 ctx->tx_bytes += fm.tp_h.tp_len;
597 ctx->tx_packets++;
599 show_frame_hdr(&fm, ctx->print_mode);
601 dissector_entry_point(out, fm.tp_h.tp_snaplen,
602 ctx->link_type, ctx->print_mode);
604 if (ctx->device_out)
605 translate_pcap_to_txf(fdo, out, fm.tp_h.tp_snaplen);
607 if (frame_count_max != 0) {
608 if (ctx->tx_packets >= frame_count_max) {
609 sigint = 1;
610 break;
615 out:
617 bug_on(gettimeofday(&end, NULL));
618 diff = tv_subtract(end, start);
620 bpf_release(&bpf_ops);
622 dissector_cleanup_all();
624 if (__pcap_io->prepare_close_pcap)
625 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD);
627 xfree(out);
629 fflush(stdout);
630 printf("\n");
631 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
632 printf("\r%12lu packets truncated in file\n", trunced);
633 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
634 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
636 if (strncmp("-", ctx->device_in, strlen("-")))
637 close(fd);
638 else
639 dup2(fd, fileno(stdin));
641 if (ctx->device_out) {
642 if (strncmp("-", ctx->device_out, strlen("-")))
643 close(fdo);
644 else
645 dup2(fdo, fileno(stdout));
649 static void finish_multi_pcap_file(struct ctx *ctx, int fd)
651 __pcap_io->fsync_pcap(fd);
653 if (__pcap_io->prepare_close_pcap)
654 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
656 close(fd);
658 fmemset(&itimer, 0, sizeof(itimer));
659 setitimer(ITIMER_REAL, &itimer, NULL);
662 static int next_multi_pcap_file(struct ctx *ctx, int fd)
664 int ret;
665 char fname[512];
667 __pcap_io->fsync_pcap(fd);
669 if (__pcap_io->prepare_close_pcap)
670 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
672 close(fd);
674 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
675 ctx->prefix ? : "dump-", time(0));
677 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
678 O_LARGEFILE, DEFFILEMODE);
680 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
681 if (ret)
682 panic("Error writing pcap header!\n");
684 if (__pcap_io->prepare_access_pcap) {
685 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, ctx->jumbo);
686 if (ret)
687 panic("Error prepare writing pcap!\n");
690 return fd;
693 static int begin_multi_pcap_file(struct ctx *ctx)
695 int fd, ret;
696 char fname[256];
698 bug_on(!__pcap_io);
700 if (ctx->device_out[strlen(ctx->device_out) - 1] == '/')
701 ctx->device_out[strlen(ctx->device_out) - 1] = 0;
703 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
704 ctx->prefix ? : "dump-", time(0));
706 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
707 O_LARGEFILE, DEFFILEMODE);
709 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
710 if (ret)
711 panic("Error writing pcap header!\n");
713 if (__pcap_io->prepare_access_pcap) {
714 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, ctx->jumbo);
715 if (ret)
716 panic("Error prepare writing pcap!\n");
719 if (ctx->dump_mode == DUMP_INTERVAL_TIME) {
720 interval = ctx->dump_interval;
722 set_itimer_interval_value(&itimer, interval, 0);
723 setitimer(ITIMER_REAL, &itimer, NULL);
724 } else {
725 interval = 0;
728 return fd;
731 static void finish_single_pcap_file(struct ctx *ctx, int fd)
733 __pcap_io->fsync_pcap(fd);
735 if (__pcap_io->prepare_close_pcap)
736 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WR);
738 if (strncmp("-", ctx->device_out, strlen("-")))
739 close(fd);
740 else
741 dup2(fd, fileno(stdout));
744 static int begin_single_pcap_file(struct ctx *ctx)
746 int fd, ret;
748 bug_on(!__pcap_io);
750 if (!strncmp("-", ctx->device_out, strlen("-"))) {
751 fd = dup(fileno(stdout));
752 close(fileno(stdout));
753 if (ctx->pcap == PCAP_OPS_MM)
754 ctx->pcap = PCAP_OPS_SG;
755 } else {
756 fd = open_or_die_m(ctx->device_out,
757 O_RDWR | O_CREAT | O_TRUNC |
758 O_LARGEFILE, DEFFILEMODE);
761 ret = __pcap_io->push_fhdr_pcap(fd, ctx->magic, ctx->link_type);
762 if (ret)
763 panic("Error writing pcap header!\n");
765 if (__pcap_io->prepare_access_pcap) {
766 ret = __pcap_io->prepare_access_pcap(fd, PCAP_MODE_WR, ctx->jumbo);
767 if (ret)
768 panic("Error prepare writing pcap!\n");
771 return fd;
774 static void print_pcap_file_stats(int sock, struct ctx *ctx, unsigned long skipped)
776 unsigned long good, bad;
777 struct tpacket_stats kstats;
778 socklen_t slen = sizeof(kstats);
780 fmemset(&kstats, 0, sizeof(kstats));
781 getsockopt(sock, SOL_PACKET, PACKET_STATISTICS, &kstats, &slen);
783 if (ctx->print_mode == PRINT_NONE) {
784 good = kstats.tp_packets - kstats.tp_drops - skipped;
785 bad = kstats.tp_drops + skipped;
787 printf(".(+%lu/-%lu)", good, bad);
788 fflush(stdout);
792 static void recv_only_or_dump(struct ctx *ctx)
794 uint8_t *packet;
795 short ifflags = 0;
796 int sock, irq, ifindex, fd = 0, ret;
797 unsigned int size, it = 0;
798 unsigned long frame_count = 0, skipped = 0;
799 struct ring rx_ring;
800 struct pollfd rx_poll;
801 struct frame_map *hdr;
802 struct sock_fprog bpf_ops;
803 struct timeval start, end, diff;
804 pcap_pkthdr_t phdr;
806 if (!device_up_and_running(ctx->device_in) && !ctx->rfraw)
807 panic("Device not up and running!\n");
809 sock = pf_socket();
811 if (ctx->rfraw) {
812 ctx->device_trans = xstrdup(ctx->device_in);
813 xfree(ctx->device_in);
815 enter_rfmon_mac80211(ctx->device_trans, &ctx->device_in);
816 ctx->link_type = LINKTYPE_IEEE802_11;
819 fmemset(&rx_ring, 0, sizeof(rx_ring));
820 fmemset(&rx_poll, 0, sizeof(rx_poll));
821 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
823 ifindex = device_ifindex(ctx->device_in);
825 size = ring_size(ctx->device_in, ctx->reserve_size);
827 enable_kernel_bpf_jit_compiler();
829 bpf_parse_rules(ctx->device_in, ctx->filter, &bpf_ops);
830 if (ctx->dump_bpf)
831 bpf_dump_all(&bpf_ops);
832 bpf_attach_to_sock(sock, &bpf_ops);
834 set_sockopt_hwtimestamp(sock, ctx->device_in);
836 setup_rx_ring_layout(sock, &rx_ring, size, ctx->jumbo);
837 create_rx_ring(sock, &rx_ring, ctx->verbose);
838 mmap_rx_ring(sock, &rx_ring);
839 alloc_rx_ring_frames(&rx_ring);
840 bind_rx_ring(sock, &rx_ring, ifindex);
842 prepare_polling(sock, &rx_poll);
843 dissector_init_all(ctx->print_mode);
845 if (ctx->cpu >= 0 && ifindex > 0) {
846 irq = device_irq_number(ctx->device_in);
847 device_bind_irq_to_cpu(irq, ctx->cpu);
849 if (ctx->verbose)
850 printf("IRQ: %s:%d > CPU%d\n",
851 ctx->device_in, irq, ctx->cpu);
854 if (ctx->promiscuous)
855 ifflags = enter_promiscuous_mode(ctx->device_in);
857 drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
859 if (dump_to_pcap(ctx)) {
860 __label__ try_file;
861 struct stat stats;
863 fmemset(&stats, 0, sizeof(stats));
864 ret = stat(ctx->device_out, &stats);
865 if (ret < 0) {
866 ctx->dump_dir = 0;
867 goto try_file;
870 ctx->dump_dir = S_ISDIR(stats.st_mode);
871 if (ctx->dump_dir) {
872 fd = begin_multi_pcap_file(ctx);
873 } else {
874 try_file:
875 fd = begin_single_pcap_file(ctx);
879 printf("Running! Hang up with ^C!\n\n");
880 fflush(stdout);
882 bug_on(gettimeofday(&start, NULL));
884 while (likely(sigint == 0)) {
885 while (user_may_pull_from_rx(rx_ring.frames[it].iov_base)) {
886 __label__ next;
888 hdr = rx_ring.frames[it].iov_base;
889 packet = ((uint8_t *) hdr) + hdr->tp_h.tp_mac;
890 frame_count++;
892 if (ctx->packet_type != -1)
893 if (ctx->packet_type != hdr->s_ll.sll_pkttype)
894 goto next;
896 if (unlikely(ring_frame_size(&rx_ring) < hdr->tp_h.tp_snaplen)) {
897 skipped++;
898 goto next;
901 if (dump_to_pcap(ctx)) {
902 tpacket_hdr_to_pcap_pkthdr(&hdr->tp_h, &hdr->s_ll, &phdr, ctx->magic);
904 ret = __pcap_io->write_pcap(fd, &phdr, ctx->magic, packet,
905 pcap_get_length(&phdr, ctx->magic));
906 if (unlikely(ret != pcap_get_total_length(&phdr, ctx->magic)))
907 panic("Write error to pcap!\n");
910 show_frame_hdr(hdr, ctx->print_mode);
912 dissector_entry_point(packet, hdr->tp_h.tp_snaplen,
913 ctx->link_type, ctx->print_mode);
915 if (frame_count_max != 0) {
916 if (frame_count >= frame_count_max) {
917 sigint = 1;
918 break;
922 next:
924 kernel_may_pull_from_rx(&hdr->tp_h);
926 it++;
927 if (it >= rx_ring.layout.tp_frame_nr)
928 it = 0;
930 if (unlikely(sigint == 1))
931 break;
933 if (dump_to_pcap(ctx)) {
934 if (ctx->dump_mode == DUMP_INTERVAL_SIZE) {
935 interval += hdr->tp_h.tp_snaplen;
937 if (interval > ctx->dump_interval) {
938 next_dump = true;
939 interval = 0;
943 if (next_dump) {
944 fd = next_multi_pcap_file(ctx, fd);
945 next_dump = false;
947 if (ctx->verbose)
948 print_pcap_file_stats(sock, ctx, skipped);
953 poll(&rx_poll, 1, -1);
954 poll_error_maybe_die(sock, &rx_poll);
957 bug_on(gettimeofday(&end, NULL));
958 diff = tv_subtract(end, start);
960 if (!(ctx->dump_dir && ctx->print_mode == PRINT_NONE)) {
961 sock_print_net_stats(sock, skipped);
963 printf("\r%12lu sec, %lu usec in total\n",
964 diff.tv_sec, diff.tv_usec);
965 } else {
966 printf("\n\n");
967 fflush(stdout);
970 bpf_release(&bpf_ops);
971 dissector_cleanup_all();
972 destroy_rx_ring(sock, &rx_ring);
974 if (ctx->promiscuous)
975 leave_promiscuous_mode(ctx->device_in, ifflags);
977 if (ctx->rfraw)
978 leave_rfmon_mac80211(ctx->device_trans, ctx->device_in);
980 if (dump_to_pcap(ctx)) {
981 if (ctx->dump_dir)
982 finish_multi_pcap_file(ctx, fd);
983 else
984 finish_single_pcap_file(ctx, fd);
987 close(sock);
990 static void help(void)
992 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING);
993 puts("http://www.netsniff-ng.org\n\n"
994 "Usage: netsniff-ng [options] [filter-expression]\n"
995 "Options:\n"
996 " -i|-d|--dev|--in <dev|pcap|-> Input source as netdev, pcap or pcap stdin\n"
997 " -o|--out <dev|pcap|dir|cfg|-> Output sink as netdev, pcap, directory, trafgen, or stdout\n"
998 " -f|--filter <bpf-file|expr> Use BPF filter file from bpfc or tcpdump-like expression\n"
999 " -t|--type <type> Filter for: host|broadcast|multicast|others|outgoing\n"
1000 " -F|--interval <size|time> Dump interval if -o is a dir: <num>KiB/MiB/GiB/s/sec/min/hrs\n"
1001 " -J|--jumbo-support Support for 64KB Super Jumbo Frames (def: 2048B)\n"
1002 " -R|--rfraw Capture or inject raw 802.11 frames\n"
1003 " -n|--num <0|uint> Number of packets until exit (def: 0)\n"
1004 " -P|--prefix <name> Prefix for pcaps stored in directory\n"
1005 " -T|--magic <pcap-magic> Pcap magic number/pcap format to store, see -D\n"
1006 " -D|--dump-pcap-types Dump pcap types and magic numbers and quit\n"
1007 " -B|--dump-bpf Dump generated BPF assembly\n"
1008 " -r|--rand Randomize packet forwarding order (dev->dev)\n"
1009 " -M|--no-promisc No promiscuous mode for netdev\n"
1010 " -A|--no-sock-mem Don't tune core socket memory\n"
1011 " -m|--mmap Mmap(2) pcap file i.e., for replaying pcaps\n"
1012 " -G|--sg Scatter/gather pcap file I/O\n"
1013 " -c|--clrw Use slower read(2)/write(2) I/O\n"
1014 " -S|--ring-size <size> Specify ring size to: <num>KiB/MiB/GiB\n"
1015 " -k|--kernel-pull <uint> Kernel pull from user interval in us (def: 10us)\n"
1016 " -b|--bind-cpu <cpu> Bind to specific CPU\n"
1017 " -u|--user <userid> Drop privileges and change to userid\n"
1018 " -g|--group <groupid> Drop privileges and change to groupid\n"
1019 " -H|--prio-high Make this high priority process\n"
1020 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
1021 " -s|--silent Do not print captured packets\n"
1022 " -q|--less Print less-verbose packet information\n"
1023 " -X|--hex Print packet data in hex format\n"
1024 " -l|--ascii Print human-readable packet data\n"
1025 " -V|--verbose Be more verbose\n"
1026 " -v|--version Show version\n"
1027 " -h|--help Guess what?!\n\n"
1028 "Examples:\n"
1029 " netsniff-ng --in eth0 --out dump.pcap -s -T 0xa1b2c3d4 --b 0 tcp or udp\n"
1030 " netsniff-ng --in wlan0 --rfraw --out dump.pcap --silent --bind-cpu 0\n"
1031 " netsniff-ng --in dump.pcap --mmap --out eth0 -k1000 --silent --bind-cpu 0\n"
1032 " netsniff-ng --in dump.pcap --out dump.cfg --silent --bind-cpu 0\n"
1033 " netsniff-ng --in eth0 --out eth1 --silent --bind-cpu 0 --type host\n"
1034 " netsniff-ng --in eth1 --out /opt/probe/ -s -m -J --interval 100MiB -b 0\n"
1035 " netsniff-ng --in vlan0 --out dump.pcap -c -u `id -u bob` -g `id -g bob`\n"
1036 " netsniff-ng --in any --filter http.bpf --jumbo-support --ascii -V\n\n"
1037 "Note:\n"
1038 " For introducing bit errors, delays with random variation and more\n"
1039 " while replaying pcaps, make use of tc(8) with its disciplines (e.g. netem).\n\n"
1040 "Please report bugs to <bugs@netsniff-ng.org>\n"
1041 "Copyright (C) 2009-2013 Daniel Borkmann <daniel@netsniff-ng.org>\n"
1042 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel@netsniff-ng.org>\n"
1043 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1044 "License: GNU GPL version 2.0\n"
1045 "This is free software: you are free to change and redistribute it.\n"
1046 "There is NO WARRANTY, to the extent permitted by law.\n");
1047 die();
1050 static void version(void)
1052 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING);
1053 puts("http://www.netsniff-ng.org\n\n"
1054 "Please report bugs to <bugs@netsniff-ng.org>\n"
1055 "Copyright (C) 2009-2013 Daniel Borkmann <daniel@netsniff-ng.org>\n"
1056 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel@netsniff-ng.org>\n"
1057 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\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 'v':
1263 version();
1264 break;
1265 case 'h':
1266 help();
1267 break;
1268 case '?':
1269 switch (optopt) {
1270 case 'd':
1271 case 'i':
1272 case 'o':
1273 case 'f':
1274 case 't':
1275 case 'P':
1276 case 'F':
1277 case 'n':
1278 case 'S':
1279 case 'b':
1280 case 'k':
1281 case 'T':
1282 case 'u':
1283 case 'g':
1284 case 'e':
1285 panic("Option -%c requires an argument!\n",
1286 optopt);
1287 default:
1288 if (isprint(optopt))
1289 printf("Unknown option character `0x%X\'!\n", optopt);
1290 die();
1292 default:
1293 break;
1297 if (!ctx.filter && optind != argc) {
1298 int ret;
1299 off_t offset = 0;
1301 for (i = optind; i < argc; ++i) {
1302 size_t alen = strlen(argv[i]) + 2;
1303 size_t flen = ctx.filter ? strlen(ctx.filter) : 0;
1305 ctx.filter = xrealloc(ctx.filter, 1, flen + alen);
1306 ret = slprintf(ctx.filter + offset, strlen(argv[i]) + 2, "%s ", argv[i]);
1307 if (ret < 0)
1308 panic("Cannot concatenate filter string!\n");
1309 else
1310 offset += ret;
1314 if (!ctx.device_in)
1315 ctx.device_in = xstrdup("any");
1317 register_signal(SIGINT, signal_handler);
1318 register_signal(SIGHUP, signal_handler);
1320 tprintf_init();
1322 if (prio_high) {
1323 set_proc_prio(get_default_proc_prio());
1324 set_sched_status(get_default_sched_policy(), get_default_sched_prio());
1327 if (ctx.device_in && (device_mtu(ctx.device_in) ||
1328 !strncmp("any", ctx.device_in, strlen(ctx.device_in)))) {
1329 if (!ctx.device_out) {
1330 ctx.dump = 0;
1331 main_loop = recv_only_or_dump;
1332 } else if (device_mtu(ctx.device_out)) {
1333 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1334 main_loop = receive_to_xmit;
1335 } else {
1336 ctx.dump = 1;
1337 register_signal_f(SIGALRM, timer_next_dump, SA_SIGINFO);
1338 main_loop = recv_only_or_dump;
1339 if (!ops_touched)
1340 ctx.pcap = PCAP_OPS_SG;
1342 } else {
1343 if (ctx.device_out && device_mtu(ctx.device_out)) {
1344 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1345 main_loop = pcap_to_xmit;
1346 if (!ops_touched)
1347 ctx.pcap = PCAP_OPS_MM;
1348 } else {
1349 main_loop = read_pcap;
1350 if (!ops_touched)
1351 ctx.pcap = PCAP_OPS_SG;
1355 bug_on(!main_loop);
1357 if (setsockmem)
1358 set_system_socket_memory(vals, array_size(vals));
1359 xlockme();
1361 main_loop(&ctx);
1363 xunlockme();
1364 if (setsockmem)
1365 reset_system_socket_memory(vals, array_size(vals));
1367 tprintf_cleanup();
1369 free(ctx.device_in);
1370 free(ctx.device_out);
1371 free(ctx.device_trans);
1372 free(ctx.prefix);
1374 return 0;