trafgen: if no buffspace available, schedule and retry
[netsniff-ng.git] / src / netsniff-ng.c
blob1d8f0d8940ad5f044a9904ad3b683b290ba37d04
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 <unistd.h>
36 #include <stdbool.h>
37 #include <pthread.h>
38 #include <fcntl.h>
40 #include "ring_rx.h"
41 #include "ring_tx.h"
42 #include "mac80211.h"
43 #include "xutils.h"
44 #include "built_in.h"
45 #include "pcap.h"
46 #include "bpf.h"
47 #include "xio.h"
48 #include "die.h"
49 #include "tprintf.h"
50 #include "dissector.h"
51 #include "xmalloc.h"
53 enum dump_mode {
54 DUMP_INTERVAL_TIME,
55 DUMP_INTERVAL_SIZE,
58 struct ctx {
59 char *device_in, *device_out, *device_trans, *filter, *prefix;
60 int cpu, rfraw, dump, print_mode, dump_dir, jumbo_support, packet_type, verbose;
61 unsigned long kpull, dump_interval, reserve_size, tx_bytes, tx_packets;
62 bool randomize, promiscuous;
63 enum pcap_ops_groups pcap;
64 enum dump_mode dump_mode;
65 uint32_t link_type;
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:B:HQmcsqXlvhF:RgAP:V";
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 {"unbind-cpu", required_argument, NULL, 'B'},
85 {"prefix", required_argument, NULL, 'P'},
86 {"rand", no_argument, NULL, 'r'},
87 {"rfraw", no_argument, NULL, 'R'},
88 {"mmap", no_argument, NULL, 'm'},
89 {"sg", no_argument, NULL, 'g'},
90 {"clrw", no_argument, NULL, 'c'},
91 {"jumbo-support", no_argument, NULL, 'J'},
92 {"no-promisc", no_argument, NULL, 'M'},
93 {"prio-high", no_argument, NULL, 'H'},
94 {"notouch-irq", no_argument, NULL, 'Q'},
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 {"verbose", no_argument, NULL, 'V'},
101 {"version", no_argument, NULL, 'v'},
102 {"help", no_argument, NULL, 'h'},
103 {NULL, 0, NULL, 0}
106 static int tx_sock;
108 static struct itimerval itimer;
110 static unsigned long frame_count_max = 0, interval = TX_KERNEL_PULL_INT;
112 #define set_system_socket_memory(vals) \
113 do { \
114 if ((vals[0] = get_system_socket_mem(sock_rmem_max)) < SMEM_SUG_MAX) \
115 set_system_socket_mem(sock_rmem_max, SMEM_SUG_MAX); \
116 if ((vals[1] = get_system_socket_mem(sock_rmem_def)) < SMEM_SUG_DEF) \
117 set_system_socket_mem(sock_rmem_def, SMEM_SUG_DEF); \
118 if ((vals[2] = get_system_socket_mem(sock_wmem_max)) < SMEM_SUG_MAX) \
119 set_system_socket_mem(sock_wmem_max, SMEM_SUG_MAX); \
120 if ((vals[3] = get_system_socket_mem(sock_wmem_def)) < SMEM_SUG_DEF) \
121 set_system_socket_mem(sock_wmem_def, SMEM_SUG_DEF); \
122 } while (0)
124 #define reset_system_socket_memory(vals) \
125 do { \
126 set_system_socket_mem(sock_rmem_max, vals[0]); \
127 set_system_socket_mem(sock_rmem_def, vals[1]); \
128 set_system_socket_mem(sock_wmem_max, vals[2]); \
129 set_system_socket_mem(sock_wmem_def, vals[3]); \
130 } while (0)
132 #define __pcap_io pcap_ops[ctx->pcap]
134 static void signal_handler(int number)
136 switch (number) {
137 case SIGINT:
138 sigint = 1;
139 case SIGHUP:
140 default:
141 break;
145 static void timer_elapsed(int unused)
147 itimer.it_interval.tv_sec = 0;
148 itimer.it_interval.tv_usec = interval;
150 itimer.it_value.tv_sec = 0;
151 itimer.it_value.tv_usec = interval;
153 pull_and_flush_tx_ring(tx_sock);
154 setitimer(ITIMER_REAL, &itimer, NULL);
157 static void timer_next_dump(int unused)
159 itimer.it_interval.tv_sec = interval;
160 itimer.it_interval.tv_usec = 0;
162 itimer.it_value.tv_sec = interval;
163 itimer.it_value.tv_usec = 0;
165 next_dump = true;
166 setitimer(ITIMER_REAL, &itimer, NULL);
169 static inline bool dump_to_pcap(struct ctx *ctx)
171 return ctx->dump;
174 static void pcap_to_xmit(struct ctx *ctx)
176 __label__ out;
177 uint8_t *out = NULL;
178 int irq, ifindex, fd = 0, ret;
179 unsigned int size, it = 0;
180 unsigned long trunced = 0;
181 struct ring tx_ring;
182 struct frame_map *hdr;
183 struct sock_fprog bpf_ops;
184 struct timeval start, end, diff;
185 struct pcap_pkthdr phdr;
187 if (!device_up_and_running(ctx->device_out) && !ctx->rfraw)
188 panic("Device not up and running!\n");
190 bug_on(!__pcap_io);
192 tx_sock = pf_socket();
194 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
196 ret = __pcap_io->pull_file_header(fd, &ctx->link_type);
197 if (ret)
198 panic("Error reading pcap header!\n");
200 if (__pcap_io->prepare_reading_pcap) {
201 ret = __pcap_io->prepare_reading_pcap(fd);
202 if (ret)
203 panic("Error prepare reading pcap!\n");
206 fmemset(&tx_ring, 0, sizeof(tx_ring));
207 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
209 if (ctx->rfraw) {
210 ctx->device_trans = xstrdup(ctx->device_out);
211 xfree(ctx->device_out);
213 enter_rfmon_mac80211(ctx->device_trans, &ctx->device_out);
214 if (ctx->link_type != LINKTYPE_IEEE802_11)
215 panic("Wrong linktype of pcap!\n");
218 ifindex = device_ifindex(ctx->device_out);
220 size = ring_size(ctx->device_out, ctx->reserve_size);
222 bpf_parse_rules(ctx->filter, &bpf_ops);
224 set_packet_loss_discard(tx_sock);
225 set_sockopt_hwtimestamp(tx_sock, ctx->device_out);
227 setup_tx_ring_layout(tx_sock, &tx_ring, size, ctx->jumbo_support);
228 create_tx_ring(tx_sock, &tx_ring, ctx->verbose);
229 mmap_tx_ring(tx_sock, &tx_ring);
230 alloc_tx_ring_frames(&tx_ring);
231 bind_tx_ring(tx_sock, &tx_ring, ifindex);
233 dissector_init_all(ctx->print_mode);
235 if (ctx->cpu >= 0 && ifindex > 0) {
236 irq = device_irq_number(ctx->device_out);
237 device_bind_irq_to_cpu(irq, ctx->cpu);
239 if (ctx->verbose)
240 printf("IRQ: %s:%d > CPU%d\n",
241 ctx->device_out, irq, ctx->cpu);
244 if (ctx->kpull)
245 interval = ctx->kpull;
247 if (ctx->verbose) {
248 printf("BPF:\n");
249 bpf_dump_all(&bpf_ops);
251 printf("MD: TX %luus %s ", interval, pcap_ops[ctx->pcap]->name);
252 if (ctx->rfraw)
253 printf("802.11 raw via %s ", ctx->device_out);
254 #ifdef _LARGEFILE64_SOURCE
255 printf("lf64 ");
256 #endif
257 ioprio_print();
258 printf("\n");
260 printf("Running! Hang up with ^C!\n\n");
261 fflush(stdout);
263 itimer.it_interval.tv_sec = 0;
264 itimer.it_interval.tv_usec = interval;
266 itimer.it_value.tv_sec = 0;
267 itimer.it_value.tv_usec = interval;
269 setitimer(ITIMER_REAL, &itimer, NULL);
271 bug_on(gettimeofday(&start, NULL));
273 while (likely(sigint == 0)) {
274 while (user_may_pull_from_tx(tx_ring.frames[it].iov_base)) {
275 hdr = tx_ring.frames[it].iov_base;
277 /* Kernel assumes: data = ph.raw + po->tp_hdrlen -
278 * sizeof(struct sockaddr_ll); */
279 out = ((uint8_t *) hdr) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
281 do {
282 ret = __pcap_io->read_pcap_pkt(fd, &phdr, out,
283 ring_frame_size(&tx_ring));
284 if (unlikely(ret <= 0))
285 goto out;
287 if (ring_frame_size(&tx_ring) < phdr.len) {
288 phdr.len = ring_frame_size(&tx_ring);
289 trunced++;
291 } while (ctx->filter && !bpf_run_filter(&bpf_ops, out, phdr.len));
293 pcap_pkthdr_to_tpacket_hdr(&phdr, &hdr->tp_h);
295 ctx->tx_bytes += hdr->tp_h.tp_len;;
296 ctx->tx_packets++;
298 show_frame_hdr(hdr, ctx->print_mode, RING_MODE_EGRESS);
300 dissector_entry_point(out, hdr->tp_h.tp_snaplen,
301 ctx->link_type, ctx->print_mode);
303 kernel_may_pull_from_tx(&hdr->tp_h);
305 it++;
306 if (it >= tx_ring.layout.tp_frame_nr)
307 it = 0;
309 if (unlikely(sigint == 1))
310 break;
312 if (frame_count_max != 0) {
313 if (ctx->tx_packets >= frame_count_max) {
314 sigint = 1;
315 break;
321 out:
323 bug_on(gettimeofday(&end, NULL));
324 diff = tv_subtract(end, start);
326 bpf_release(&bpf_ops);
328 dissector_cleanup_all();
329 destroy_tx_ring(tx_sock, &tx_ring);
331 if (ctx->rfraw)
332 leave_rfmon_mac80211(ctx->device_trans, ctx->device_out);
334 if (__pcap_io->prepare_close_pcap)
335 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_READ);
337 close(fd);
338 close(tx_sock);
340 fflush(stdout);
341 printf("\n");
342 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
343 printf("\r%12lu packets truncated in file\n", trunced);
344 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
345 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
348 static void receive_to_xmit(struct ctx *ctx)
350 short ifflags = 0;
351 uint8_t *in, *out;
352 int rx_sock, ifindex_in, ifindex_out;
353 unsigned int size_in, size_out, it_in = 0, it_out = 0;
354 unsigned long frame_count = 0;
355 struct frame_map *hdr_in, *hdr_out;
356 struct ring tx_ring, rx_ring;
357 struct pollfd rx_poll;
358 struct sock_fprog bpf_ops;
360 if (!strncmp(ctx->device_in, ctx->device_out, IFNAMSIZ))
361 panic("Ingress/egress devices must be different!\n");
362 if (!device_up_and_running(ctx->device_out))
363 panic("Egress device not up and running!\n");
364 if (!device_up_and_running(ctx->device_in))
365 panic("Ingress device not up and running!\n");
367 rx_sock = pf_socket();
368 tx_sock = pf_socket();
370 fmemset(&tx_ring, 0, sizeof(tx_ring));
371 fmemset(&rx_ring, 0, sizeof(rx_ring));
372 fmemset(&rx_poll, 0, sizeof(rx_poll));
373 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
375 ifindex_in = device_ifindex(ctx->device_in);
376 ifindex_out = device_ifindex(ctx->device_out);
378 size_in = ring_size(ctx->device_in, ctx->reserve_size);
379 size_out = ring_size(ctx->device_out, ctx->reserve_size);
381 enable_kernel_bpf_jit_compiler();
383 bpf_parse_rules(ctx->filter, &bpf_ops);
384 bpf_attach_to_sock(rx_sock, &bpf_ops);
386 setup_rx_ring_layout(rx_sock, &rx_ring, size_in, ctx->jumbo_support);
387 create_rx_ring(rx_sock, &rx_ring, ctx->verbose);
388 mmap_rx_ring(rx_sock, &rx_ring);
389 alloc_rx_ring_frames(&rx_ring);
390 bind_rx_ring(rx_sock, &rx_ring, ifindex_in);
391 prepare_polling(rx_sock, &rx_poll);
393 set_packet_loss_discard(tx_sock);
394 setup_tx_ring_layout(tx_sock, &tx_ring, size_out, ctx->jumbo_support);
395 create_tx_ring(tx_sock, &tx_ring, ctx->verbose);
396 mmap_tx_ring(tx_sock, &tx_ring);
397 alloc_tx_ring_frames(&tx_ring);
398 bind_tx_ring(tx_sock, &tx_ring, ifindex_out);
400 dissector_init_all(ctx->print_mode);
402 if (ctx->promiscuous)
403 ifflags = enter_promiscuous_mode(ctx->device_in);
405 if (ctx->kpull)
406 interval = ctx->kpull;
408 itimer.it_interval.tv_sec = 0;
409 itimer.it_interval.tv_usec = interval;
411 itimer.it_value.tv_sec = 0;
412 itimer.it_value.tv_usec = interval;
414 setitimer(ITIMER_REAL, &itimer, NULL);
416 if (ctx->verbose) {
417 printf("BPF:\n");
418 bpf_dump_all(&bpf_ops);
420 printf("MD: RXTX %luus\n\n", interval);
422 printf("Running! Hang up with ^C!\n\n");
423 fflush(stdout);
425 while (likely(sigint == 0)) {
426 while (user_may_pull_from_rx(rx_ring.frames[it_in].iov_base)) {
427 __label__ next;
429 hdr_in = rx_ring.frames[it_in].iov_base;
430 in = ((uint8_t *) hdr_in) + hdr_in->tp_h.tp_mac;
432 frame_count++;
434 if (ctx->packet_type != -1)
435 if (ctx->packet_type != hdr_in->s_ll.sll_pkttype)
436 goto next;
438 hdr_out = tx_ring.frames[it_out].iov_base;
439 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
441 for (; !user_may_pull_from_tx(tx_ring.frames[it_out].iov_base) &&
442 likely(!sigint);) {
443 if (ctx->randomize)
444 next_rnd_slot(&it_out, &tx_ring);
445 else {
446 it_out++;
447 if (it_out >= tx_ring.layout.tp_frame_nr)
448 it_out = 0;
451 hdr_out = tx_ring.frames[it_out].iov_base;
452 out = ((uint8_t *) hdr_out) + TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
455 tpacket_hdr_clone(&hdr_out->tp_h, &hdr_in->tp_h);
456 fmemcpy(out, in, hdr_in->tp_h.tp_len);
458 kernel_may_pull_from_tx(&hdr_out->tp_h);
459 if (ctx->randomize)
460 next_rnd_slot(&it_out, &tx_ring);
461 else {
462 it_out++;
463 if (it_out >= tx_ring.layout.tp_frame_nr)
464 it_out = 0;
467 show_frame_hdr(hdr_in, ctx->print_mode, RING_MODE_INGRESS);
469 dissector_entry_point(in, hdr_in->tp_h.tp_snaplen,
470 ctx->link_type, ctx->print_mode);
472 if (frame_count_max != 0) {
473 if (frame_count >= frame_count_max) {
474 sigint = 1;
475 break;
479 next:
481 kernel_may_pull_from_rx(&hdr_in->tp_h);
483 it_in++;
484 if (it_in >= rx_ring.layout.tp_frame_nr)
485 it_in = 0;
487 if (unlikely(sigint == 1))
488 goto out;
491 poll(&rx_poll, 1, -1);
492 poll_error_maybe_die(rx_sock, &rx_poll);
495 out:
497 sock_print_net_stats(rx_sock, 0);
499 bpf_release(&bpf_ops);
501 dissector_cleanup_all();
503 destroy_tx_ring(tx_sock, &tx_ring);
504 destroy_rx_ring(rx_sock, &rx_ring);
506 if (ctx->promiscuous)
507 leave_promiscuous_mode(ctx->device_in, ifflags);
509 close(tx_sock);
510 close(rx_sock);
513 static void translate_pcap_to_txf(int fdo, uint8_t *out, size_t len)
515 size_t bytes_done = 0;
516 char bout[80];
518 slprintf(bout, sizeof(bout), "{\n ");
519 write_or_die(fdo, bout, strlen(bout));
521 while (bytes_done < len) {
522 slprintf(bout, sizeof(bout), "0x%02x, ", out[bytes_done]);
523 write_or_die(fdo, bout, strlen(bout));
525 bytes_done++;
527 if (bytes_done % 10 == 0) {
528 slprintf(bout, sizeof(bout), "\n");
529 write_or_die(fdo, bout, strlen(bout));
531 if (bytes_done < len) {
532 slprintf(bout, sizeof(bout), " ");
533 write_or_die(fdo, bout, strlen(bout));
537 if (bytes_done % 10 != 0) {
538 slprintf(bout, sizeof(bout), "\n");
539 write_or_die(fdo, bout, strlen(bout));
542 slprintf(bout, sizeof(bout), "}\n\n");
543 write_or_die(fdo, bout, strlen(bout));
546 static void read_pcap(struct ctx *ctx)
548 __label__ out;
549 uint8_t *out;
550 int ret, fd, fdo = 0;
551 unsigned long trunced = 0;
552 size_t out_len;
553 struct pcap_pkthdr phdr;
554 struct sock_fprog bpf_ops;
555 struct frame_map fm;
556 struct timeval start, end, diff;
558 bug_on(!__pcap_io);
560 fd = open_or_die(ctx->device_in, O_RDONLY | O_LARGEFILE | O_NOATIME);
562 ret = __pcap_io->pull_file_header(fd, &ctx->link_type);
563 if (ret)
564 panic("Error reading pcap header!\n");
566 if (__pcap_io->prepare_reading_pcap) {
567 ret = __pcap_io->prepare_reading_pcap(fd);
568 if (ret)
569 panic("Error prepare reading pcap!\n");
572 fmemset(&fm, 0, sizeof(fm));
573 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
575 bpf_parse_rules(ctx->filter, &bpf_ops);
577 dissector_init_all(ctx->print_mode);
579 out_len = round_up(1024 * 1024, PAGE_SIZE);
580 out = xmalloc_aligned(out_len, CO_CACHE_LINE_SIZE);
582 if (ctx->verbose) {
583 printf("BPF:\n");
584 bpf_dump_all(&bpf_ops);
586 printf("MD: RD %s ", __pcap_io->name);
587 #ifdef _LARGEFILE64_SOURCE
588 printf("lf64 ");
589 #endif
590 ioprio_print();
591 printf("\n");
593 printf("Running! Hang up with ^C!\n\n");
594 fflush(stdout);
596 if (ctx->device_out)
597 fdo = open_or_die_m(ctx->device_out, O_RDWR | O_CREAT |
598 O_TRUNC | O_LARGEFILE, DEFFILEMODE);
600 bug_on(gettimeofday(&start, NULL));
602 while (likely(sigint == 0)) {
603 do {
604 ret = __pcap_io->read_pcap_pkt(fd, &phdr, out, out_len);
605 if (unlikely(ret < 0))
606 goto out;
608 if (unlikely(phdr.len == 0)) {
609 trunced++;
610 continue;
613 if (unlikely(phdr.len > out_len)) {
614 phdr.len = out_len;
615 trunced++;
617 } while (ctx->filter && !bpf_run_filter(&bpf_ops, out, phdr.len));
619 pcap_pkthdr_to_tpacket_hdr(&phdr, &fm.tp_h);
621 ctx->tx_bytes += fm.tp_h.tp_len;
622 ctx->tx_packets++;
624 show_frame_hdr(&fm, ctx->print_mode, RING_MODE_EGRESS);
626 dissector_entry_point(out, fm.tp_h.tp_snaplen,
627 ctx->link_type, ctx->print_mode);
629 if (ctx->device_out)
630 translate_pcap_to_txf(fdo, out, fm.tp_h.tp_snaplen);
632 if (frame_count_max != 0) {
633 if (ctx->tx_packets >= frame_count_max) {
634 sigint = 1;
635 break;
640 out:
642 bug_on(gettimeofday(&end, NULL));
643 diff = tv_subtract(end, start);
645 bpf_release(&bpf_ops);
647 dissector_cleanup_all();
649 if (__pcap_io->prepare_close_pcap)
650 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_READ);
652 close(fd);
653 if (ctx->device_out)
654 close(fdo);
656 xfree(out);
658 fflush(stdout);
659 printf("\n");
660 printf("\r%12lu packets outgoing\n", ctx->tx_packets);
661 printf("\r%12lu packets truncated in file\n", trunced);
662 printf("\r%12lu bytes outgoing\n", ctx->tx_bytes);
663 printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);
666 static void finish_multi_pcap_file(struct ctx *ctx, int fd)
668 __pcap_io->fsync_pcap(fd);
670 if (__pcap_io->prepare_close_pcap)
671 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WRITE);
673 close(fd);
675 fmemset(&itimer, 0, sizeof(itimer));
676 setitimer(ITIMER_REAL, &itimer, NULL);
679 static int next_multi_pcap_file(struct ctx *ctx, int fd)
681 int ret;
682 char fname[512];
684 __pcap_io->fsync_pcap(fd);
686 if (__pcap_io->prepare_close_pcap)
687 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WRITE);
689 close(fd);
691 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
692 ctx->prefix ? : "dump-", time(0));
694 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
695 O_LARGEFILE, DEFFILEMODE);
697 ret = __pcap_io->push_file_header(fd, ctx->link_type);
698 if (ret)
699 panic("Error writing pcap header!\n");
701 if (__pcap_io->prepare_writing_pcap) {
702 ret = __pcap_io->prepare_writing_pcap(fd);
703 if (ret)
704 panic("Error prepare writing pcap!\n");
707 return fd;
710 static int begin_multi_pcap_file(struct ctx *ctx)
712 int fd, ret;
713 char fname[256];
715 bug_on(!__pcap_io);
717 if (ctx->device_out[strlen(ctx->device_out) - 1] == '/')
718 ctx->device_out[strlen(ctx->device_out) - 1] = 0;
720 slprintf(fname, sizeof(fname), "%s/%s%lu.pcap", ctx->device_out,
721 ctx->prefix ? : "dump-", time(0));
723 fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC |
724 O_LARGEFILE, DEFFILEMODE);
726 ret = __pcap_io->push_file_header(fd, ctx->link_type);
727 if (ret)
728 panic("Error writing pcap header!\n");
730 if (__pcap_io->prepare_writing_pcap) {
731 ret = __pcap_io->prepare_writing_pcap(fd);
732 if (ret)
733 panic("Error prepare writing pcap!\n");
736 if (ctx->dump_mode == DUMP_INTERVAL_TIME) {
737 interval = ctx->dump_interval;
739 itimer.it_interval.tv_sec = interval;
740 itimer.it_interval.tv_usec = 0;
742 itimer.it_value.tv_sec = interval;
743 itimer.it_value.tv_usec = 0;
745 setitimer(ITIMER_REAL, &itimer, NULL);
746 } else {
747 interval = 0;
750 return fd;
753 static void finish_single_pcap_file(struct ctx *ctx, int fd)
755 __pcap_io->fsync_pcap(fd);
757 if (__pcap_io->prepare_close_pcap)
758 __pcap_io->prepare_close_pcap(fd, PCAP_MODE_WRITE);
760 close(fd);
763 static int begin_single_pcap_file(struct ctx *ctx)
765 int fd, ret;
767 bug_on(!__pcap_io);
769 fd = open_or_die_m(ctx->device_out, O_RDWR | O_CREAT | O_TRUNC |
770 O_LARGEFILE, DEFFILEMODE);
772 ret = __pcap_io->push_file_header(fd, ctx->link_type);
773 if (ret)
774 panic("Error writing pcap header!\n");
776 if (__pcap_io->prepare_writing_pcap) {
777 ret = __pcap_io->prepare_writing_pcap(fd);
778 if (ret)
779 panic("Error prepare writing pcap!\n");
782 return fd;
785 static void print_pcap_file_stats(int sock, struct ctx *ctx, unsigned long skipped)
787 unsigned long good, bad;
788 struct tpacket_stats kstats;
789 socklen_t slen = sizeof(kstats);
791 fmemset(&kstats, 0, sizeof(kstats));
792 getsockopt(sock, SOL_PACKET, PACKET_STATISTICS, &kstats, &slen);
794 if (ctx->print_mode == PRINT_NONE) {
795 good = kstats.tp_packets - kstats.tp_drops - skipped;
796 bad = kstats.tp_drops + skipped;
798 printf(".(+%lu/-%lu)", good, bad);
799 fflush(stdout);
803 static void recv_only_or_dump(struct ctx *ctx)
805 uint8_t *packet;
806 short ifflags = 0;
807 int sock, irq, ifindex, fd = 0, ret;
808 unsigned int size, it = 0;
809 unsigned long frame_count = 0, skipped = 0;
810 struct ring rx_ring;
811 struct pollfd rx_poll;
812 struct frame_map *hdr;
813 struct sock_fprog bpf_ops;
814 struct timeval start, end, diff;
815 struct pcap_pkthdr phdr;
817 if (!device_up_and_running(ctx->device_in) && !ctx->rfraw)
818 panic("Device not up and running!\n");
820 sock = pf_socket();
822 if (ctx->rfraw) {
823 ctx->device_trans = xstrdup(ctx->device_in);
824 xfree(ctx->device_in);
826 enter_rfmon_mac80211(ctx->device_trans, &ctx->device_in);
827 ctx->link_type = LINKTYPE_IEEE802_11;
830 if (dump_to_pcap(ctx)) {
831 __label__ try_file;
832 struct stat stats;
834 fmemset(&stats, 0, sizeof(stats));
835 ret = stat(ctx->device_out, &stats);
836 if (ret < 0) {
837 ctx->dump_dir = 0;
838 goto try_file;
841 ctx->dump_dir = S_ISDIR(stats.st_mode);
842 if (ctx->dump_dir) {
843 fd = begin_multi_pcap_file(ctx);
844 } else {
845 try_file:
846 fd = begin_single_pcap_file(ctx);
850 fmemset(&rx_ring, 0, sizeof(rx_ring));
851 fmemset(&rx_poll, 0, sizeof(rx_poll));
852 fmemset(&bpf_ops, 0, sizeof(bpf_ops));
854 ifindex = device_ifindex(ctx->device_in);
856 size = ring_size(ctx->device_in, ctx->reserve_size);
858 enable_kernel_bpf_jit_compiler();
860 bpf_parse_rules(ctx->filter, &bpf_ops);
861 bpf_attach_to_sock(sock, &bpf_ops);
863 set_sockopt_hwtimestamp(sock, ctx->device_in);
865 setup_rx_ring_layout(sock, &rx_ring, size, ctx->jumbo_support);
866 create_rx_ring(sock, &rx_ring, ctx->verbose);
867 mmap_rx_ring(sock, &rx_ring);
868 alloc_rx_ring_frames(&rx_ring);
869 bind_rx_ring(sock, &rx_ring, ifindex);
871 prepare_polling(sock, &rx_poll);
872 dissector_init_all(ctx->print_mode);
874 if (ctx->cpu >= 0 && ifindex > 0) {
875 irq = device_irq_number(ctx->device_in);
876 device_bind_irq_to_cpu(irq, ctx->cpu);
878 if (ctx->verbose)
879 printf("IRQ: %s:%d > CPU%d\n",
880 ctx->device_in, irq, ctx->cpu);
883 if (ctx->promiscuous)
884 ifflags = enter_promiscuous_mode(ctx->device_in);
886 if (ctx->verbose) {
887 printf("BPF:\n");
888 bpf_dump_all(&bpf_ops);
890 printf("MD: RX %s ", ctx->dump ? pcap_ops[ctx->pcap]->name : "");
891 if (ctx->rfraw)
892 printf("802.11 raw via %s ", ctx->device_in);
893 #ifdef _LARGEFILE64_SOURCE
894 printf("lf64 ");
895 #endif
896 ioprio_print();
897 printf("\n");
899 printf("Running! Hang up with ^C!\n\n");
900 fflush(stdout);
902 bug_on(gettimeofday(&start, NULL));
904 while (likely(sigint == 0)) {
905 while (user_may_pull_from_rx(rx_ring.frames[it].iov_base)) {
906 __label__ next;
908 hdr = rx_ring.frames[it].iov_base;
909 packet = ((uint8_t *) hdr) + hdr->tp_h.tp_mac;
910 frame_count++;
912 if (ctx->packet_type != -1)
913 if (ctx->packet_type != hdr->s_ll.sll_pkttype)
914 goto next;
916 if (unlikely(ring_frame_size(&rx_ring) < hdr->tp_h.tp_snaplen)) {
917 skipped++;
918 goto next;
921 if (dump_to_pcap(ctx)) {
922 tpacket_hdr_to_pcap_pkthdr(&hdr->tp_h, &phdr);
924 ret = __pcap_io->write_pcap_pkt(fd, &phdr, packet, phdr.len);
925 if (unlikely(ret != sizeof(phdr) + phdr.len))
926 panic("Write error to pcap!\n");
929 show_frame_hdr(hdr, ctx->print_mode, RING_MODE_INGRESS);
931 dissector_entry_point(packet, hdr->tp_h.tp_snaplen,
932 ctx->link_type, ctx->print_mode);
934 if (frame_count_max != 0) {
935 if (frame_count >= frame_count_max) {
936 sigint = 1;
937 break;
941 next:
943 kernel_may_pull_from_rx(&hdr->tp_h);
945 it++;
946 if (it >= rx_ring.layout.tp_frame_nr)
947 it = 0;
949 if (unlikely(sigint == 1))
950 break;
952 if (dump_to_pcap(ctx)) {
953 if (ctx->dump_mode == DUMP_INTERVAL_SIZE) {
954 interval += hdr->tp_h.tp_snaplen;
956 if (interval > ctx->dump_interval) {
957 next_dump = true;
958 interval = 0;
962 if (next_dump) {
963 fd = next_multi_pcap_file(ctx, fd);
964 next_dump = false;
966 if (ctx->verbose)
967 print_pcap_file_stats(sock, ctx, skipped);
972 poll(&rx_poll, 1, -1);
973 poll_error_maybe_die(sock, &rx_poll);
976 bug_on(gettimeofday(&end, NULL));
977 diff = tv_subtract(end, start);
979 if (!(ctx->dump_dir && ctx->print_mode == PRINT_NONE)) {
980 sock_print_net_stats(sock, skipped);
982 printf("\r%12lu sec, %lu usec in total\n",
983 diff.tv_sec, diff.tv_usec);
984 } else {
985 printf("\n\n");
986 fflush(stdout);
989 bpf_release(&bpf_ops);
990 dissector_cleanup_all();
991 destroy_rx_ring(sock, &rx_ring);
993 if (ctx->promiscuous)
994 leave_promiscuous_mode(ctx->device_in, ifflags);
996 if (ctx->rfraw)
997 leave_rfmon_mac80211(ctx->device_trans, ctx->device_in);
999 close(sock);
1001 if (dump_to_pcap(ctx)) {
1002 if (ctx->dump_dir)
1003 finish_multi_pcap_file(ctx, fd);
1004 else
1005 finish_single_pcap_file(ctx, fd);
1009 static void help(void)
1011 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING);
1012 puts("http://www.netsniff-ng.org\n\n"
1013 "Usage: netsniff-ng [options]\n"
1014 "Options:\n"
1015 " -i|-d|--dev|--in <dev|pcap> Input source as netdev or pcap\n"
1016 " -o|--out <dev|pcap|dir|txf> Output sink as netdev, pcap, directory, txf file\n"
1017 " -f|--filter <bpf-file> Use BPF filter file from bpfc\n"
1018 " -t|--type <type> Only handle packets of defined type:\n"
1019 " host|broadcast|multicast|others|outgoing\n"
1020 " -F|--interval <size/time> Dump interval in time or size if -o is a directory\n"
1021 " pcap swap spec: <num>KiB/MiB/GiB/s/sec/min/hrs\n"
1022 " -J|--jumbo-support Support for 64KB Super Jumbo Frames\n"
1023 " Default RX/TX slot: 2048Byte\n"
1024 " -R|--rfraw Capture or inject raw 802.11 frames\n"
1025 " -n|--num <uint> Number of packets until exit\n"
1026 " `-- 0 Loop until interrupted (default)\n"
1027 " `- n Send n packets and done\n"
1028 "Options for printing:\n"
1029 " -s|--silent Do not print captured packets\n"
1030 " -q|--less Print less-verbose packet information\n"
1031 " -X|--hex Print packet data in hex format\n"
1032 " -l|--ascii Print human-readable packet data\n"
1033 "Options, advanced:\n"
1034 " -P|--prefix <name> Prefix for pcaps stored in directory\n"
1035 " -r|--rand Randomize packet forwarding order\n"
1036 " -M|--no-promisc No promiscuous mode for netdev\n"
1037 " -A|--no-sock-mem Don't tune core socket memory\n"
1038 " -m|--mmap Mmap pcap file i.e., for replaying\n"
1039 " -g|--sg Scatter/gather pcap file I/O\n"
1040 " -c|--clrw Use slower read(2)/write(2) I/O\n"
1041 " -S|--ring-size <size> Manually set ring size to <size>:\n"
1042 " mmap space in KiB/MiB/GiB, e.g. \'10MiB\'\n"
1043 " -k|--kernel-pull <uint> Kernel pull from user interval in us\n"
1044 " Default is 10us where the TX_RING\n"
1045 " is populated with payload from uspace\n"
1046 " -b|--bind-cpu <cpu> Bind to specific CPU (or CPU-range)\n"
1047 " -B|--unbind-cpu <cpu> Forbid to use specific CPU (or CPU-range)\n"
1048 " -H|--prio-high Make this high priority process\n"
1049 " -Q|--notouch-irq Do not touch IRQ CPU affinity of NIC\n"
1050 " -V|--verbose Be more verbose\n"
1051 " -v|--version Show version\n"
1052 " -h|--help Guess what?!\n\n"
1053 "Examples:\n"
1054 " netsniff-ng --in eth0 --out dump.pcap --silent --bind-cpu 0\n"
1055 " netsniff-ng --in wlan0 --rfraw --out dump.pcap --silent --bind-cpu 0\n"
1056 " netsniff-ng --in dump.pcap --mmap --out eth0 -k1000 --silent --bind-cpu 0\n"
1057 " netsniff-ng --in dump.pcap --out dump.txf --silent --bind-cpu 0\n"
1058 " netsniff-ng --in eth0 --out eth1 --silent --bind-cpu 0 --type host\n"
1059 " netsniff-ng --in eth1 --out /opt/probe/ -s -m -J --interval 100MiB -b 0\n"
1060 " netsniff-ng --in any --filter http.bpf --jumbo-support --ascii -V\n\n"
1061 "Note:\n"
1062 " This tool is targeted for network developers! You should\n"
1063 " be aware of what you are doing and what these options above\n"
1064 " mean! Use netsniff-ng's bpfc compiler for generating filter files.\n"
1065 " Further, netsniff-ng automatically enables the kernel BPF JIT\n"
1066 " if present. Txf file output is only possible if the input source\n"
1067 " is a pcap file.\n\n"
1068 "Please report bugs to <bugs@netsniff-ng.org>\n"
1069 "Copyright (C) 2009-2013 Daniel Borkmann <daniel@netsniff-ng.org>\n"
1070 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel@netsniff-ng.org>\n"
1071 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1072 "License: GNU GPL version 2.0\n"
1073 "This is free software: you are free to change and redistribute it.\n"
1074 "There is NO WARRANTY, to the extent permitted by law.\n");
1075 die();
1078 static void version(void)
1080 printf("\nnetsniff-ng %s, the packet sniffing beast\n", VERSION_STRING);
1081 puts("http://www.netsniff-ng.org\n\n"
1082 "Please report bugs to <bugs@netsniff-ng.org>\n"
1083 "Copyright (C) 2009-2013 Daniel Borkmann <daniel@netsniff-ng.org>\n"
1084 "Copyright (C) 2009-2012 Emmanuel Roullit <emmanuel@netsniff-ng.org>\n"
1085 "Copyright (C) 2012 Markus Amend <markus@netsniff-ng.org>\n"
1086 "License: GNU GPL version 2.0\n"
1087 "This is free software: you are free to change and redistribute it.\n"
1088 "There is NO WARRANTY, to the extent permitted by law.\n");
1089 die();
1092 static void header(void)
1094 printf("%s%s%s\n", colorize_start(bold), "netsniff-ng " VERSION_STRING, colorize_end());
1097 int main(int argc, char **argv)
1099 char *ptr;
1100 int c, i, j, opt_index, ops_touched = 0, vals[4] = {0};
1101 bool prio_high = false, setsockmem = true;
1102 void (*main_loop)(struct ctx *ctx) = NULL;
1103 struct ctx ctx = {
1104 .link_type = LINKTYPE_EN10MB,
1105 .print_mode = PRINT_NORM,
1106 .cpu = -1,
1107 .packet_type = -1,
1108 .promiscuous = true,
1109 .randomize = false,
1110 .pcap = PCAP_OPS_SG,
1111 .dump_interval = 60,
1112 .dump_mode = DUMP_INTERVAL_TIME,
1115 srand(time(NULL));
1117 while ((c = getopt_long(argc, argv, short_options, long_options,
1118 &opt_index)) != EOF) {
1119 switch (c) {
1120 case 'd':
1121 case 'i':
1122 ctx.device_in = xstrdup(optarg);
1123 break;
1124 case 'o':
1125 ctx.device_out = xstrdup(optarg);
1126 break;
1127 case 'P':
1128 ctx.prefix = xstrdup(optarg);
1129 break;
1130 case 'R':
1131 ctx.link_type = LINKTYPE_IEEE802_11;
1132 ctx.rfraw = 1;
1133 break;
1134 case 'r':
1135 ctx.randomize = true;
1136 break;
1137 case 'J':
1138 ctx.jumbo_support = 1;
1139 break;
1140 case 'f':
1141 ctx.filter = xstrdup(optarg);
1142 break;
1143 case 'M':
1144 ctx.promiscuous = false;
1145 break;
1146 case 'A':
1147 setsockmem = false;
1148 break;
1149 case 't':
1150 if (!strncmp(optarg, "host", strlen("host")))
1151 ctx.packet_type = PACKET_HOST;
1152 else if (!strncmp(optarg, "broadcast", strlen("broadcast")))
1153 ctx.packet_type = PACKET_BROADCAST;
1154 else if (!strncmp(optarg, "multicast", strlen("multicast")))
1155 ctx.packet_type = PACKET_MULTICAST;
1156 else if (!strncmp(optarg, "others", strlen("others")))
1157 ctx.packet_type = PACKET_OTHERHOST;
1158 else if (!strncmp(optarg, "outgoing", strlen("outgoing")))
1159 ctx.packet_type = PACKET_OUTGOING;
1160 else
1161 ctx.packet_type = -1;
1162 break;
1163 case 'S':
1164 ptr = optarg;
1165 ctx.reserve_size = 0;
1167 for (j = i = strlen(optarg); i > 0; --i) {
1168 if (!isdigit(optarg[j - i]))
1169 break;
1170 ptr++;
1173 if (!strncmp(ptr, "KiB", strlen("KiB")))
1174 ctx.reserve_size = 1 << 10;
1175 else if (!strncmp(ptr, "MiB", strlen("MiB")))
1176 ctx.reserve_size = 1 << 20;
1177 else if (!strncmp(ptr, "GiB", strlen("GiB")))
1178 ctx.reserve_size = 1 << 30;
1179 else
1180 panic("Syntax error in ring size param!\n");
1181 *ptr = 0;
1183 ctx.reserve_size *= strtol(optarg, NULL, 0);
1184 break;
1185 case 'b':
1186 set_cpu_affinity(optarg, 0);
1187 /* Take the first CPU for rebinding the IRQ */
1188 if (ctx.cpu != -2)
1189 ctx.cpu = strtol(optarg, NULL, 0);
1190 break;
1191 case 'B':
1192 set_cpu_affinity(optarg, 1);
1193 break;
1194 case 'H':
1195 prio_high = true;
1196 break;
1197 case 'c':
1198 ctx.pcap = PCAP_OPS_RW;
1199 ops_touched = 1;
1200 break;
1201 case 'm':
1202 ctx.pcap = PCAP_OPS_MMAP;
1203 ops_touched = 1;
1204 break;
1205 case 'g':
1206 ctx.pcap = PCAP_OPS_SG;
1207 ops_touched = 1;
1208 break;
1209 case 'Q':
1210 ctx.cpu = -2;
1211 break;
1212 case 's':
1213 ctx.print_mode = PRINT_NONE;
1214 break;
1215 case 'q':
1216 ctx.print_mode = PRINT_LESS;
1217 break;
1218 case 'X':
1219 ctx.print_mode =
1220 (ctx.print_mode == PRINT_ASCII) ?
1221 PRINT_HEX_ASCII : PRINT_HEX;
1222 break;
1223 case 'l':
1224 ctx.print_mode =
1225 (ctx.print_mode == PRINT_HEX) ?
1226 PRINT_HEX_ASCII : PRINT_ASCII;
1227 break;
1228 case 'k':
1229 ctx.kpull = strtol(optarg, NULL, 0);
1230 break;
1231 case 'n':
1232 frame_count_max = strtol(optarg, NULL, 0);
1233 break;
1234 case 'F':
1235 ptr = optarg;
1236 ctx.dump_interval = 0;
1238 for (j = i = strlen(optarg); i > 0; --i) {
1239 if (!isdigit(optarg[j - i]))
1240 break;
1241 ptr++;
1244 if (!strncmp(ptr, "KiB", strlen("KiB"))) {
1245 ctx.dump_interval = 1 << 10;
1246 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1247 } else if (!strncmp(ptr, "MiB", strlen("MiB"))) {
1248 ctx.dump_interval = 1 << 20;
1249 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1250 } else if (!strncmp(ptr, "GiB", strlen("GiB"))) {
1251 ctx.dump_interval = 1 << 30;
1252 ctx.dump_mode = DUMP_INTERVAL_SIZE;
1253 } else if (!strncmp(ptr, "sec", strlen("sec"))) {
1254 ctx.dump_interval = 1;
1255 ctx.dump_mode = DUMP_INTERVAL_TIME;
1256 } else if (!strncmp(ptr, "min", strlen("min"))) {
1257 ctx.dump_interval = 60;
1258 ctx.dump_mode = DUMP_INTERVAL_TIME;
1259 } else if (!strncmp(ptr, "hrs", strlen("hrs"))) {
1260 ctx.dump_interval = 60 * 60;
1261 ctx.dump_mode = DUMP_INTERVAL_TIME;
1262 } else if (!strncmp(ptr, "s", strlen("s"))) {
1263 ctx.dump_interval = 1;
1264 ctx.dump_mode = DUMP_INTERVAL_TIME;
1265 } else {
1266 panic("Syntax error in time/size param!\n");
1269 *ptr = 0;
1270 ctx.dump_interval *= strtol(optarg, NULL, 0);
1271 break;
1272 case 'V':
1273 ctx.verbose = 1;
1274 break;
1275 case 'v':
1276 version();
1277 break;
1278 case 'h':
1279 help();
1280 break;
1281 case '?':
1282 switch (optopt) {
1283 case 'd':
1284 case 'i':
1285 case 'o':
1286 case 'f':
1287 case 't':
1288 case 'P':
1289 case 'F':
1290 case 'n':
1291 case 'S':
1292 case 'b':
1293 case 'k':
1294 case 'B':
1295 case 'e':
1296 panic("Option -%c requires an argument!\n",
1297 optopt);
1298 default:
1299 if (isprint(optopt))
1300 whine("Unknown option character "
1301 "`0x%X\'!\n", optopt);
1302 die();
1304 default:
1305 break;
1309 if (!ctx.device_in)
1310 ctx.device_in = xstrdup("any");
1312 register_signal(SIGINT, signal_handler);
1313 register_signal(SIGHUP, signal_handler);
1315 header();
1317 init_pcap(ctx.jumbo_support);
1318 tprintf_init();
1320 if (prio_high) {
1321 set_proc_prio(get_default_proc_prio());
1322 set_sched_status(get_default_sched_policy(), get_default_sched_prio());
1325 if (ctx.device_in && (device_mtu(ctx.device_in) ||
1326 !strncmp("any", ctx.device_in, strlen(ctx.device_in)))) {
1327 if (!ctx.device_out) {
1328 ctx.dump = 0;
1329 main_loop = recv_only_or_dump;
1330 } else if (device_mtu(ctx.device_out)) {
1331 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1332 main_loop = receive_to_xmit;
1333 } else {
1334 ctx.dump = 1;
1335 register_signal_f(SIGALRM, timer_next_dump, SA_SIGINFO);
1336 main_loop = recv_only_or_dump;
1337 if (!ops_touched)
1338 ctx.pcap = PCAP_OPS_SG;
1340 } else {
1341 if (ctx.device_out && device_mtu(ctx.device_out)) {
1342 register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
1343 main_loop = pcap_to_xmit;
1344 if (!ops_touched)
1345 ctx.pcap = PCAP_OPS_MMAP;
1346 } else {
1347 main_loop = read_pcap;
1348 if (!ops_touched)
1349 ctx.pcap = PCAP_OPS_SG;
1353 bug_on(!main_loop);
1355 if (setsockmem)
1356 set_system_socket_memory(vals);
1358 main_loop(&ctx);
1360 if (setsockmem)
1361 reset_system_socket_memory(vals);
1363 tprintf_cleanup();
1364 cleanup_pcap();
1366 free(ctx.device_in);
1367 free(ctx.device_out);
1368 free(ctx.device_trans);
1369 free(ctx.prefix);
1371 return 0;