flowtop: Remove unused parameters from draw_help() and draw_footer()
[netsniff-ng.git] / astraceroute.c
blob96009212ccc99f99f08f89521172c90f32f55d3a
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2011 - 2013 Daniel Borkmann.
4 * Subject to the GPL, version 2.
5 */
7 #define _BSD_SOURCE
8 #define _DEFAULT_SOURCE
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <signal.h>
12 #include <getopt.h>
13 #include <ctype.h>
14 #include <stdint.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <sys/socket.h>
18 #include <sys/fsuid.h>
19 #include <fcntl.h>
20 #include <time.h>
21 #include <string.h>
22 #include <asm/byteorder.h>
23 #include <linux/tcp.h>
24 #include <netinet/ip.h>
25 #include <netinet/ip6.h>
26 #include <netinet/in.h>
27 #include <errno.h>
28 #include <netdb.h>
29 #include <sys/time.h>
30 #include <arpa/inet.h>
31 #include <linux/if_ether.h>
32 #include <linux/icmp.h>
33 #include <linux/icmpv6.h>
35 #include "bpf.h"
36 #include "die.h"
37 #include "dev.h"
38 #include "sig.h"
39 #include "config.h"
40 #include "tprintf.h"
41 #include "pkt_buff.h"
42 #include "proto.h"
43 #include "xmalloc.h"
44 #include "csum.h"
45 #include "sock.h"
46 #include "geoip.h"
47 #include "ring.h"
48 #include "built_in.h"
50 struct ctx {
51 char *host, *port, *dev, *payload, *bind_addr;
52 size_t totlen, rcvlen;
53 socklen_t sd_len;
54 int init_ttl, max_ttl, dns_resolv, queries, timeout;
55 int syn, ack, ecn, fin, psh, rst, urg, tos, nofrag, proto, show;
56 int dport, latitude;
59 struct proto_ops {
60 int (*assembler)(uint8_t *packet, size_t len, int ttl, int proto,
61 const struct ctx *ctx, const struct sockaddr *dst,
62 const struct sockaddr *src);
63 const struct sock_filter *filter;
64 unsigned int flen;
65 size_t min_len_tcp, min_len_icmp;
66 int (*check)(uint8_t *packet, size_t len, int ttl, int id,
67 const struct sockaddr *src);
68 void (*handler)(uint8_t *packet, size_t len, int dns_resolv,
69 int latitude);
72 static sig_atomic_t sigint = 0;
74 static int assemble_ipv4(uint8_t *packet, size_t len, int ttl, int proto,
75 const struct ctx *ctx, const struct sockaddr *dst,
76 const struct sockaddr *src);
77 static int assemble_ipv6(uint8_t *packet, size_t len, int ttl, int proto,
78 const struct ctx *ctx, const struct sockaddr *dst,
79 const struct sockaddr *src);
80 static int check_ipv4(uint8_t *packet, size_t len, int ttl, int id,
81 const struct sockaddr *ss);
82 static void handle_ipv4(uint8_t *packet, size_t len, int dns_resolv,
83 int latitude);
84 static int check_ipv6(uint8_t *packet, size_t len, int ttl, int id,
85 const struct sockaddr *ss);
86 static void handle_ipv6(uint8_t *packet, size_t len, int dns_resolv,
87 int latitude);
89 static const char *short_options = "H:p:nNf:m:b:i:d:q:x:SAEFPURt:Gl:hv46X:ZuL";
90 static const struct option long_options[] = {
91 {"host", required_argument, NULL, 'H'},
92 {"port", required_argument, NULL, 'p'},
93 {"init-ttl", required_argument, NULL, 'f'},
94 {"max-ttl", required_argument, NULL, 'm'},
95 {"bind", required_argument, NULL, 'b'},
96 {"dev", required_argument, NULL, 'd'},
97 {"num-probes", required_argument, NULL, 'q'},
98 {"timeout", required_argument, NULL, 'x'},
99 {"tos", required_argument, NULL, 't'},
100 {"payload", required_argument, NULL, 'X'},
101 {"totlen", required_argument, NULL, 'l'},
102 {"numeric", no_argument, NULL, 'n'},
103 {"latitude", no_argument, NULL, 'L'},
104 {"update", no_argument, NULL, 'u'},
105 {"dns", no_argument, NULL, 'N'},
106 {"ipv4", no_argument, NULL, '4'},
107 {"ipv6", no_argument, NULL, '6'},
108 {"syn", no_argument, NULL, 'S'},
109 {"ack", no_argument, NULL, 'A'},
110 {"urg", no_argument, NULL, 'U'},
111 {"fin", no_argument, NULL, 'F'},
112 {"psh", no_argument, NULL, 'P'},
113 {"rst", no_argument, NULL, 'R'},
114 {"ecn-syn", no_argument, NULL, 'E'},
115 {"show-packet", no_argument, NULL, 'Z'},
116 {"nofrag", no_argument, NULL, 'G'},
117 {"version", no_argument, NULL, 'v'},
118 {"help", no_argument, NULL, 'h'},
119 {NULL, 0, NULL, 0}
122 static const char *copyright = "Please report bugs to <netsniff-ng@googlegroups.com>\n"
123 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
124 "Swiss federal institute of technology (ETH Zurich)\n"
125 "License: GNU GPL version 2.0\n"
126 "This is free software: you are free to change and redistribute it.\n"
127 "There is NO WARRANTY, to the extent permitted by law.";
129 static const struct sock_filter ipv4_icmp_type_11[] = {
130 { 0x28, 0, 0, 0x0000000c }, /* ldh [12] */
131 { 0x15, 0, 8, 0x00000800 }, /* jneq #0x800, drop */
132 { 0x30, 0, 0, 0x00000017 }, /* ldb [23] */
133 { 0x15, 0, 6, 0x00000001 }, /* jneq #0x1, drop */
134 { 0x28, 0, 0, 0x00000014 }, /* ldh [20] */
135 { 0x45, 4, 0, 0x00001fff }, /* jset #0x1fff, drop */
136 { 0xb1, 0, 0, 0x0000000e }, /* ldxb 4*([14]&0xf) */
137 { 0x50, 0, 0, 0x0000000e }, /* ldb [x + 14] */
138 { 0x15, 0, 1, 0x0000000b }, /* jneq #0xb, drop */
139 { 0x06, 0, 0, 0xffffffff }, /* ret #-1 */
140 { 0x06, 0, 0, 0x00000000 }, /* drop: ret #0 */
143 static const struct sock_filter ipv6_icmp6_type_3[] = {
144 { 0x28, 0, 0, 0x0000000c }, /* ldh [12] */
145 { 0x15, 0, 5, 0x000086dd }, /* jneq #0x86dd, drop */
146 { 0x30, 0, 0, 0x00000014 }, /* ldb [20] */
147 { 0x15, 0, 3, 0x0000003a }, /* jneq #0x3a, drop */
148 { 0x30, 0, 0, 0x00000036 }, /* ldb [54] */
149 { 0x15, 0, 1, 0x00000003 }, /* jneq #0x3, drop */
150 { 0x06, 0, 0, 0xffffffff }, /* ret #-1 */
151 { 0x06, 0, 0, 0x00000000 }, /* drop: ret #0 */
154 static const struct proto_ops af_ops[] = {
155 [IPPROTO_IP] = {
156 .assembler = assemble_ipv4,
157 .handler = handle_ipv4,
158 .check = check_ipv4,
159 .filter = ipv4_icmp_type_11,
160 .flen = array_size(ipv4_icmp_type_11),
161 .min_len_tcp = sizeof(struct iphdr) + sizeof(struct tcphdr),
162 .min_len_icmp = sizeof(struct iphdr) + sizeof(struct icmphdr),
164 [IPPROTO_IPV6] = {
165 .assembler = assemble_ipv6,
166 .handler = handle_ipv6,
167 .check = check_ipv6,
168 .filter = ipv6_icmp6_type_3,
169 .flen = array_size(ipv6_icmp6_type_3),
170 .min_len_tcp = sizeof(struct ip6_hdr) + sizeof(struct tcphdr),
171 .min_len_icmp = sizeof(struct ip6_hdr) + sizeof(struct icmp6hdr),
175 static void signal_handler(int number)
177 switch (number) {
178 case SIGINT:
179 case SIGQUIT:
180 case SIGTERM:
181 sigint = 1;
182 default:
183 break;
187 static void __noreturn help(void)
189 printf("astraceroute %s, autonomous system trace route utility\n", VERSION_STRING);
190 puts("http://www.netsniff-ng.org\n\n"
191 "Usage: astraceroute [options]\n"
192 "Options:\n"
193 " -H|--host <host> Host/IPv4/IPv6 to lookup AS route to\n"
194 " -p|--port <port> Hosts port to lookup AS route to\n"
195 " -i|-d|--dev <device> Networking device, e.g. eth0\n"
196 " -b|--bind <IP> IP address to bind to, Must specify -6 for an IPv6 address\n"
197 " -f|--init-ttl <ttl> Set initial TTL\n"
198 " -m|--max-ttl <ttl> Set maximum TTL (def: 30)\n"
199 " -q|--num-probes <num> Number of max probes for each hop (def: 2)\n"
200 " -x|--timeout <sec> Probe response timeout in sec (def: 3)\n"
201 " -X|--payload <string> Specify a payload string to test DPIs\n"
202 " -l|--totlen <len> Specify total packet len\n"
203 " -4|--ipv4 Use IPv4-only requests\n"
204 " -6|--ipv6 Use IPv6-only requests\n"
205 " -n|--numeric Do not do reverse DNS lookup for hops\n"
206 " -u|--update Update GeoIP databases\n"
207 " -L|--latitude Show latitude and longitude\n"
208 " -N|--dns Do a reverse DNS lookup for hops\n"
209 " -S|--syn Set TCP SYN flag\n"
210 " -A|--ack Set TCP ACK flag\n"
211 " -F|--fin Set TCP FIN flag\n"
212 " -P|--psh Set TCP PSH flag\n"
213 " -U|--urg Set TCP URG flag\n"
214 " -R|--rst Set TCP RST flag\n"
215 " -E|--ecn-syn Send ECN SYN packets (RFC3168)\n"
216 " -t|--tos <tos> Set the IP TOS field\n"
217 " -G|--nofrag Set do not fragment bit\n"
218 " -Z|--show-packet Show returned packet on each hop\n"
219 " -v|--version Print version and exit\n"
220 " -h|--help Print this help and exit\n\n"
221 "Examples:\n"
222 " IPv4 trace of AS with TCP SYN probe (this will most-likely pass):\n"
223 " astraceroute -i eth0 -N -S -H netsniff-ng.org\n"
224 " IPv4 trace of AS with TCP ECN SYN probe:\n"
225 " astraceroute -i eth0 -N -E -H netsniff-ng.org\n"
226 " IPv4 trace of AS with TCP FIN probe:\n"
227 " astraceroute -i eth0 -N -F -H netsniff-ng.org\n"
228 " IPv4 trace of AS with Xmas probe:\n"
229 " astraceroute -i eth0 -N -FPU -H netsniff-ng.org\n"
230 " IPv4 trace of AS with Null probe with ASCII payload:\n"
231 " astraceroute -i eth0 -N -H netsniff-ng.org -X \"censor-me\" -Z\n"
232 " IPv6 trace of AS up to www.6bone.net:\n"
233 " astraceroute -6 -i eth0 -S -E -N -H www.6bone.net\n\n"
234 "Note:\n"
235 " If the TCP probe did not give any results, then astraceroute will\n"
236 " automatically probe for classic ICMP packets! To gather more\n"
237 " information about astraceroute's fetched AS numbers, see e.g.\n"
238 " http://bgp.he.net/AS<number>!\n");
239 puts(copyright);
240 die();
243 static void __noreturn version(void)
245 printf("astraceroute %s, Git id: %s\n", VERSION_LONG, GITVERSION);
246 puts("autonomous system trace route utility\n"
247 "http://www.netsniff-ng.org\n");
248 puts(copyright);
249 die();
252 static void __assemble_data(uint8_t *packet, size_t len, const char *payload)
254 size_t i;
256 if (payload == NULL) {
257 for (i = 0; i < len; ++i)
258 packet[i] = (uint8_t) rand();
259 } else {
260 size_t lmin = min(len, strlen(payload));
262 for (i = 0; i < lmin; ++i)
263 packet[i] = (uint8_t) payload[i];
264 for (i = lmin; i < len; ++i)
265 packet[i] = (uint8_t) rand();
269 static void __assemble_icmp4(uint8_t *packet, size_t len)
271 struct icmphdr *icmph = (struct icmphdr *) packet;
273 bug_on(len < sizeof(struct icmphdr));
275 icmph->type = ICMP_ECHO;
276 icmph->code = 0;
277 icmph->checksum = 0;
280 static void __assemble_icmp6(uint8_t *packet, size_t len)
282 struct icmp6hdr *icmp6h = (struct icmp6hdr *) packet;
284 bug_on(len < sizeof(struct icmp6hdr));
286 icmp6h->icmp6_type = ICMPV6_ECHO_REQUEST;
287 icmp6h->icmp6_code = 0;
288 icmp6h->icmp6_cksum = 0;
291 static void __assemble_tcp(uint8_t *packet, size_t len, int syn, int ack,
292 int urg, int fin, int rst, int psh, int ecn,
293 int dport)
295 struct tcphdr *tcph = (struct tcphdr *) packet;
297 bug_on(len < sizeof(struct tcphdr));
299 tcph->source = htons((uint16_t) rand());
300 tcph->dest = htons((uint16_t) dport);
302 tcph->seq = htonl(rand());
303 tcph->ack_seq = (!!ack ? htonl(rand()) : 0);
305 tcph->doff = 5;
307 tcph->syn = !!syn;
308 tcph->ack = !!ack;
309 tcph->urg = !!urg;
310 tcph->fin = !!fin;
311 tcph->rst = !!rst;
312 tcph->psh = !!psh;
313 tcph->ece = !!ecn;
314 tcph->cwr = !!ecn;
316 tcph->window = htons((uint16_t) (100 + (rand() % 65435)));
317 tcph->urg_ptr = (!!urg ? htons((uint16_t) rand()) : 0);
318 tcph->check = 0;
321 static int assemble_ipv4(uint8_t *packet, size_t len, int ttl, int proto,
322 const struct ctx *ctx, const struct sockaddr *dst,
323 const struct sockaddr *src)
325 uint8_t *data;
326 size_t data_len, off_next = 0;
327 struct iphdr *iph = (struct iphdr *) packet;
329 bug_on(!src || !dst);
330 bug_on(src->sa_family != PF_INET || dst->sa_family != PF_INET);
331 bug_on(len < sizeof(*iph) + min(sizeof(struct tcphdr),
332 sizeof(struct icmphdr)));
334 iph->ihl = 5;
335 iph->version = 4;
336 iph->tos = (uint8_t) ctx->tos;
338 iph->tot_len = htons((uint16_t) len);
339 iph->id = htons((uint16_t) rand());
341 iph->frag_off = ctx->nofrag ? IP_DF : 0;
342 iph->ttl = (uint8_t) ttl;
344 iph->saddr = ((const struct sockaddr_in *) src)->sin_addr.s_addr;
345 iph->daddr = ((const struct sockaddr_in *) dst)->sin_addr.s_addr;
347 iph->protocol = (uint8_t) proto;
349 data = packet + sizeof(*iph);
350 data_len = len - sizeof(*iph);
352 switch (proto) {
353 case IPPROTO_TCP:
354 __assemble_tcp(data, data_len, ctx->syn, ctx->ack, ctx->urg,
355 ctx->fin, ctx->rst, ctx->psh, ctx->ecn, ctx->dport);
356 off_next = sizeof(struct tcphdr);
357 break;
358 case IPPROTO_ICMP:
359 __assemble_icmp4(data, data_len);
360 off_next = sizeof(struct icmphdr);
361 break;
362 default:
363 bug();
366 data = packet + sizeof(*iph) + off_next;
367 data_len = len - sizeof(*iph) - off_next;
369 __assemble_data(data, data_len, ctx->payload);
371 iph->check = csum((unsigned short *) packet, ntohs(iph->tot_len) >> 1);
373 return ntohs(iph->id);
376 static int assemble_ipv6(uint8_t *packet, size_t len, int ttl, int proto,
377 const struct ctx *ctx, const struct sockaddr *dst,
378 const struct sockaddr *src)
380 uint8_t *data;
381 size_t data_len, off_next = 0;
382 struct ip6_hdr *ip6h = (struct ip6_hdr *) packet;
384 bug_on(!src || !dst);
385 bug_on(src->sa_family != PF_INET6 || dst->sa_family != PF_INET6);
386 bug_on(len < sizeof(*ip6h) + min(sizeof(struct tcphdr),
387 sizeof(struct icmp6hdr)));
389 ip6h->ip6_flow = htonl(rand() & 0x000fffff);
390 ip6h->ip6_vfc = 0x60;
392 ip6h->ip6_plen = htons((uint16_t) len - sizeof(*ip6h));
393 ip6h->ip6_nxt = (uint8_t) proto;
394 ip6h->ip6_hlim = (uint8_t) ttl;
396 memcpy(&ip6h->ip6_src, &(((const struct sockaddr_in6 *)
397 src)->sin6_addr), sizeof(ip6h->ip6_src));
398 memcpy(&ip6h->ip6_dst, &(((const struct sockaddr_in6 *)
399 dst)->sin6_addr), sizeof(ip6h->ip6_dst));
401 data = packet + sizeof(*ip6h);
402 data_len = len - sizeof(*ip6h);
404 switch (proto) {
405 case IPPROTO_TCP:
406 __assemble_tcp(data, data_len, ctx->syn, ctx->ack, ctx->urg,
407 ctx->fin, ctx->rst, ctx->psh, ctx->ecn, ctx->dport);
408 off_next = sizeof(struct tcphdr);
409 break;
410 case IPPROTO_ICMP:
411 case IPPROTO_ICMPV6:
412 __assemble_icmp6(data, data_len);
413 off_next = sizeof(struct icmp6hdr);
414 break;
415 default:
416 bug();
419 data = packet + sizeof(*ip6h) + off_next;
420 data_len = len - sizeof(*ip6h) - off_next;
422 __assemble_data(data, data_len, ctx->payload);
424 return ntohl(ip6h->ip6_flow) & 0x000fffff;
427 static int check_ipv4(uint8_t *packet, size_t len, int ttl __maybe_unused,
428 int id, const struct sockaddr *ss)
430 struct iphdr *iph = (struct iphdr *) packet;
431 struct iphdr *iph_inner;
432 struct icmphdr *icmph;
434 if (iph->protocol != IPPROTO_ICMP)
435 return -EINVAL;
436 if (iph->daddr != ((const struct sockaddr_in *) ss)->sin_addr.s_addr)
437 return -EINVAL;
439 icmph = (struct icmphdr *) (packet + sizeof(struct iphdr));
440 if (icmph->type != ICMP_TIME_EXCEEDED)
441 return -EINVAL;
442 if (icmph->code != ICMP_EXC_TTL)
443 return -EINVAL;
445 iph_inner = (struct iphdr *) (packet + sizeof(struct iphdr) +
446 sizeof(struct icmphdr));
447 if (ntohs(iph_inner->id) != id)
448 return -EINVAL;
450 return len;
453 static void handle_ipv4(uint8_t *packet, size_t len __maybe_unused,
454 int dns_resolv, int latitude)
456 char hbuff[NI_MAXHOST];
457 struct iphdr *iph = (struct iphdr *) packet;
458 struct sockaddr_in sd;
459 struct hostent *hent;
460 const char *as, *country, *city;
462 memset(hbuff, 0, sizeof(hbuff));
463 memset(&sd, 0, sizeof(sd));
464 sd.sin_family = PF_INET;
465 sd.sin_addr.s_addr = iph->saddr;
467 getnameinfo((struct sockaddr *) &sd, sizeof(sd),
468 hbuff, sizeof(hbuff), NULL, 0, NI_NUMERICHOST);
470 as = geoip4_as_name(&sd);
471 country = geoip4_country_name(&sd);
472 city = geoip4_city_name(&sd);
474 if (dns_resolv) {
475 hent = gethostbyaddr(&sd.sin_addr, sizeof(sd.sin_addr), PF_INET);
476 if (hent)
477 printf(" %s (%s)", hent->h_name, hbuff);
478 else
479 printf(" %s", hbuff);
480 } else {
481 printf(" %s", hbuff);
483 if (as)
484 printf(" in %s", as);
485 if (country) {
486 printf(" in %s", country);
487 if (city)
488 printf(", %s", city);
490 if (latitude)
491 printf(" (%f/%f)", geoip4_latitude(&sd), geoip4_longitude(&sd));
494 static int check_ipv6(uint8_t *packet, size_t len, int ttl __maybe_unused,
495 int id, const struct sockaddr *ss)
497 struct ip6_hdr *ip6h = (struct ip6_hdr *) packet;
498 struct ip6_hdr *ip6h_inner;
499 struct icmp6hdr *icmp6h;
501 if (ip6h->ip6_nxt != 0x3a)
502 return -EINVAL;
503 if (memcmp(&ip6h->ip6_dst, &(((const struct sockaddr_in6 *)
504 ss)->sin6_addr), sizeof(ip6h->ip6_dst)))
505 return -EINVAL;
507 icmp6h = (struct icmp6hdr *) (packet + sizeof(*ip6h));
508 if (icmp6h->icmp6_type != ICMPV6_TIME_EXCEED)
509 return -EINVAL;
510 if (icmp6h->icmp6_code != ICMPV6_EXC_HOPLIMIT)
511 return -EINVAL;
513 ip6h_inner = (struct ip6_hdr *) (packet + sizeof(*ip6h) + sizeof(*icmp6h));
514 if ((ntohl(ip6h_inner->ip6_flow) & 0x000fffff) != (uint32_t) id)
515 return -EINVAL;
517 return len;
520 static void handle_ipv6(uint8_t *packet, size_t len __maybe_unused,
521 int dns_resolv, int latitude)
523 char hbuff[NI_MAXHOST];
524 struct ip6_hdr *ip6h = (struct ip6_hdr *) packet;
525 struct sockaddr_in6 sd;
526 struct hostent *hent;
527 const char *as, *country, *city;
529 memset(hbuff, 0, sizeof(hbuff));
530 memset(&sd, 0, sizeof(sd));
531 sd.sin6_family = PF_INET6;
532 memcpy(&sd.sin6_addr, &ip6h->ip6_src, sizeof(ip6h->ip6_src));
534 getnameinfo((struct sockaddr *) &sd, sizeof(sd),
535 hbuff, sizeof(hbuff), NULL, 0, NI_NUMERICHOST);
537 as = geoip6_as_name(&sd);
538 country = geoip6_country_name(&sd);
539 city = geoip6_city_name(&sd);
541 if (dns_resolv) {
542 hent = gethostbyaddr(&sd.sin6_addr, sizeof(sd.sin6_addr), PF_INET6);
543 if (hent)
544 printf(" %s (%s)", hent->h_name, hbuff);
545 else
546 printf(" %s", hbuff);
547 } else {
548 printf(" %s", hbuff);
550 if (as)
551 printf(" in %s", as);
552 if (country) {
553 printf(" in %s", country);
554 if (city)
555 printf(", %s", city);
557 if (latitude)
558 printf(" (%f/%f)", geoip6_latitude(&sd), geoip6_longitude(&sd));
561 static void show_trace_info(struct ctx *ctx, const struct sockaddr_storage *ss,
562 const struct sockaddr_storage *sd)
564 char hbuffs[256], hbuffd[256];
566 memset(hbuffd, 0, sizeof(hbuffd));
567 getnameinfo((struct sockaddr *) sd, sizeof(*sd),
568 hbuffd, sizeof(hbuffd), NULL, 0, NI_NUMERICHOST);
570 memset(hbuffs, 0, sizeof(hbuffs));
571 getnameinfo((struct sockaddr *) ss, sizeof(*ss),
572 hbuffs, sizeof(hbuffs), NULL, 0, NI_NUMERICHOST);
574 printf("AS path IPv%d TCP trace from %s to %s:%s (%s) with len %zu "
575 "Bytes, %u max hops\n", ctx->proto == IPPROTO_IP ? 4 : 6,
576 hbuffs, hbuffd, ctx->port, ctx->host, ctx->totlen, ctx->max_ttl);
578 printf("Using flags SYN:%d,ACK:%d,ECN:%d,FIN:%d,PSH:%d,RST:%d,URG:%d\n",
579 ctx->syn, ctx->ack, ctx->ecn, ctx->fin, ctx->psh, ctx->rst, ctx->urg);
581 if (ctx->payload)
582 printf("With payload: \'%s\'\n", ctx->payload);
585 static int get_remote_fd(struct ctx *ctx, struct sockaddr_storage *ss,
586 struct sockaddr_storage *sd)
588 int fd = -1, ret, one = 1, af = AF_INET;
589 struct addrinfo hints, *ahead, *ai;
590 unsigned char bind_ip[sizeof(struct in6_addr)];
592 memset(&hints, 0, sizeof(hints));
593 hints.ai_family = PF_UNSPEC;
594 hints.ai_socktype = SOCK_STREAM;
595 hints.ai_protocol = IPPROTO_TCP;
596 hints.ai_flags = AI_NUMERICSERV;
598 ret = getaddrinfo(ctx->host, ctx->port, &hints, &ahead);
599 if (ret < 0)
600 panic("Cannot get address info!\n");
602 for (ai = ahead; ai != NULL && fd < 0; ai = ai->ai_next) {
603 if (!((ai->ai_family == PF_INET6 && ctx->proto == IPPROTO_IPV6) ||
604 (ai->ai_family == PF_INET && ctx->proto == IPPROTO_IP)))
605 continue;
607 fd = socket(ai->ai_family, SOCK_RAW, IPPROTO_RAW);
608 if (fd < 0)
609 continue;
611 memset(ss, 0, sizeof(*ss));
612 ret = device_address(ctx->dev, ai->ai_family, ss);
613 if (ret < 0 && !ctx->bind_addr)
614 panic("Cannot get own device address!\n");
616 if (ctx->bind_addr) {
617 if (ctx->proto == IPPROTO_IPV6)
618 af = AF_INET6;
620 if (inet_pton(af, ctx->bind_addr, &bind_ip) != 1)
621 panic("Address is invalid!\n");
623 if (af == AF_INET6) {
624 struct sockaddr_in6 *ss6 = (struct sockaddr_in6 *) ss;
625 memcpy(&ss6->sin6_addr.s6_addr, &bind_ip, sizeof(struct in6_addr));
626 } else {
627 struct sockaddr_in *ss4 = (struct sockaddr_in *) ss;
628 memcpy(&ss4->sin_addr.s_addr, &bind_ip, sizeof(struct in_addr));
632 ret = bind(fd, (struct sockaddr *) ss, sizeof(*ss));
633 if (ret < 0)
634 panic("Cannot bind socket!\n");
636 memset(sd, 0, sizeof(*sd));
637 memcpy(sd, ai->ai_addr, ai->ai_addrlen);
639 ctx->sd_len = ai->ai_addrlen;
640 ctx->dport = strtoul(ctx->port, NULL, 10);
642 ret = setsockopt(fd, ctx->proto, IP_HDRINCL, &one, sizeof(one));
643 if (ret < 0)
644 panic("Kernel does not support IP_HDRINCL!\n");
646 if (ai->ai_family == PF_INET6) {
647 struct sockaddr_in6 *sd6 = (struct sockaddr_in6 *) sd;
649 sd6->sin6_port = 0;
652 break;
655 freeaddrinfo(ahead);
657 if (fd < 0)
658 panic("Cannot create socket! Does remote "
659 "support IPv%d?!\n",
660 ctx->proto == IPPROTO_IP ? 4 : 6);
662 return fd;
665 static void inject_filter(struct ctx *ctx, int fd)
667 struct sock_fprog bpf_ops;
669 enable_kernel_bpf_jit_compiler();
671 memset(&bpf_ops, 0, sizeof(bpf_ops));
672 bpf_ops.filter = (struct sock_filter *) af_ops[ctx->proto].filter;
673 bpf_ops.len = af_ops[ctx->proto].flen;
675 bpf_attach_to_sock(fd, &bpf_ops);
678 static int __process_node(struct ctx *ctx, int fd, int fd_cap, int ttl,
679 int inner_proto, uint8_t *pkt_snd, uint8_t *pkt_rcv,
680 const struct sockaddr_storage *ss,
681 const struct sockaddr_storage *sd, struct timeval *diff)
683 int pkt_id, ret, timeout;
684 struct pollfd pfd;
685 struct timeval start, end;
687 prepare_polling(fd_cap, &pfd);
689 memset(pkt_snd, 0, ctx->totlen);
690 pkt_id = af_ops[ctx->proto].assembler(pkt_snd, ctx->totlen, ttl,
691 inner_proto, ctx,
692 (const struct sockaddr *) sd,
693 (const struct sockaddr *) ss);
695 ret = sendto(fd, pkt_snd, ctx->totlen, 0, (struct sockaddr *) sd,
696 ctx->sd_len);
697 if (ret < 0)
698 panic("sendto failed: %s\n", strerror(errno));
700 bug_on(gettimeofday(&start, NULL));
702 timeout = (ctx->timeout > 0 ? ctx->timeout : 2) * 1000;
704 ret = poll(&pfd, 1, timeout);
705 if (ret > 0 && pfd.revents & POLLIN && sigint == 0) {
706 bug_on(gettimeofday(&end, NULL));
707 if (diff)
708 timersub(&end, &start, diff);
710 ret = recvfrom(fd_cap, pkt_rcv, ctx->rcvlen, 0, NULL, NULL);
711 if (ret < (int) (sizeof(struct ethhdr) + af_ops[ctx->proto].min_len_icmp))
712 return -EIO;
714 return af_ops[ctx->proto].check(pkt_rcv + sizeof(struct ethhdr),
715 ret - sizeof(struct ethhdr), ttl,
716 pkt_id, (const struct sockaddr *) ss);
717 } else {
718 return -EIO;
721 return 0;
724 static void timerdiv(const unsigned long divisor, const struct timeval *tv,
725 struct timeval *result)
727 uint64_t x = ((uint64_t) tv->tv_sec * 1000 * 1000 + tv->tv_usec) / divisor;
729 result->tv_sec = x / 1000 / 1000;
730 result->tv_usec = x % (1000 * 1000);
733 static int timevalcmp(const void *t1, const void *t2)
735 if (timercmp((struct timeval *) t1, (struct timeval *) t2, <))
736 return -1;
737 if (timercmp((struct timeval *) t1, (struct timeval *) t2, >))
738 return 1;
740 return 0;
743 static int __process_time(struct ctx *ctx, int fd, int fd_cap, int ttl,
744 int inner_proto, uint8_t *pkt_snd, uint8_t *pkt_rcv,
745 const struct sockaddr_storage *ss,
746 const struct sockaddr_storage *sd)
748 size_t i, j = 0;
749 int good = 0, ret = -EIO, idx, ret_good = -EIO;
750 struct timeval probes[9], *tmp, sum, res;
751 uint8_t *trash = xmalloc(ctx->rcvlen);
752 char *cwait[] = { "-", "\\", "|", "/" };
753 const char *proto_short[] = {
754 [IPPROTO_TCP] = "t",
755 [IPPROTO_ICMP] = "i",
756 [IPPROTO_ICMPV6] = "i",
759 memset(probes, 0, sizeof(probes));
760 for (i = 0; i < array_size(probes) && sigint == 0; ++i) {
761 ret = __process_node(ctx, fd, fd_cap, ttl, inner_proto,
762 pkt_snd, good == 0 ? pkt_rcv : trash,
763 ss, sd, &probes[i]);
764 if (ret > 0) {
765 if (good == 0)
766 ret_good = ret;
767 good++;
770 if (good == 0 && ctx->queries == (int) i)
771 break;
773 usleep(50000);
775 printf("\r%2d: %s", ttl, cwait[j++]);
776 fflush(stdout);
777 if (j >= array_size(cwait))
778 j = 0;
781 if (good == 0) {
782 xfree(trash);
783 return -EIO;
786 tmp = xcalloc(good, sizeof(struct timeval));
787 for (i = j = 0; i < array_size(probes); ++i) {
788 if (probes[i].tv_sec == 0 && probes[i].tv_usec == 0)
789 continue;
790 tmp[j].tv_sec = probes[i].tv_sec;
791 tmp[j].tv_usec = probes[i].tv_usec;
792 j++;
795 qsort(tmp, j, sizeof(struct timeval), timevalcmp);
797 printf("\r%2d: %s[", ttl, proto_short[inner_proto]);
798 idx = j / 2;
799 switch (j % 2) {
800 case 0:
801 timeradd(&tmp[idx], &tmp[idx - 1], &sum);
802 timerdiv(2, &sum, &res);
803 if (res.tv_sec > 0)
804 printf("%lu sec ", res.tv_sec);
805 printf("%7lu us", res.tv_usec);
806 break;
807 case 1:
808 if (tmp[idx].tv_sec > 0)
809 printf("%lu sec ", tmp[idx].tv_sec);
810 printf("%7lu us", tmp[idx].tv_usec);
811 break;
813 printf("]");
815 xfree(tmp);
816 xfree(trash);
818 return ret_good;
821 static int __probe_remote(struct ctx *ctx, int fd, int fd_cap, int ttl,
822 uint8_t *pkt_snd, uint8_t *pkt_rcv,
823 const struct sockaddr_storage *ss,
824 const struct sockaddr_storage *sd,
825 int inner_proto)
827 int ret = -EIO, tries = ctx->queries;
829 while (tries-- > 0 && sigint == 0) {
830 ret = __process_time(ctx, fd, fd_cap, ttl, inner_proto,
831 pkt_snd, pkt_rcv, ss, sd);
832 if (ret < 0)
833 continue;
835 af_ops[ctx->proto].handler(pkt_rcv + sizeof(struct ethhdr),
836 ret - sizeof(struct ethhdr),
837 ctx->dns_resolv, ctx->latitude);
838 if (ctx->show) {
839 struct pkt_buff *pkt;
841 printf("\n");
842 pkt = pkt_alloc(pkt_rcv, ret);
843 hex_ascii(pkt);
844 tprintf_flush();
845 pkt_free(pkt);
848 break;
851 return ret;
854 static int __process_ttl(struct ctx *ctx, int fd, int fd_cap, int ttl,
855 uint8_t *pkt_snd, uint8_t *pkt_rcv,
856 const struct sockaddr_storage *ss,
857 const struct sockaddr_storage *sd)
859 int ret = -EIO;
860 size_t i;
861 const int inner_protos[] = {
862 IPPROTO_TCP,
863 IPPROTO_ICMP,
866 printf("%2d: ", ttl);
867 fflush(stdout);
869 for (i = 0; i < array_size(inner_protos) && sigint == 0; ++i) {
870 ret = __probe_remote(ctx, fd, fd_cap, ttl, pkt_snd, pkt_rcv, ss, sd,
871 inner_protos[i]);
872 if (ret > 0)
873 break;
876 if (ret <= 0)
877 printf("\r%2d: ?[ no answer]", ttl);
878 if (ctx->show == 0)
879 printf("\n");
880 if (ctx->show && ret <= 0)
881 printf("\n\n");
883 fflush(stdout);
884 return 0;
887 static int main_trace(struct ctx *ctx)
889 int fd, fd_cap, ifindex, ttl;
890 struct ring dummy_ring;
891 struct sockaddr_storage ss, sd;
892 uint8_t *pkt_snd, *pkt_rcv;
894 fd = get_remote_fd(ctx, &ss, &sd);
895 fd_cap = pf_socket();
897 inject_filter(ctx, fd_cap);
899 ifindex = device_ifindex(ctx->dev);
900 bind_ring_generic(fd_cap, &dummy_ring, ifindex, false);
902 if (ctx->totlen < af_ops[ctx->proto].min_len_tcp) {
903 ctx->totlen = af_ops[ctx->proto].min_len_tcp;
904 if (ctx->payload)
905 ctx->totlen += strlen(ctx->payload);
908 ctx->rcvlen = device_mtu(ctx->dev) - sizeof(struct ethhdr);
909 if (ctx->totlen >= ctx->rcvlen)
910 panic("Packet len exceeds device MTU!\n");
912 pkt_snd = xmalloc(ctx->totlen);
913 pkt_rcv = xmalloc(ctx->rcvlen);
915 show_trace_info(ctx, &ss, &sd);
917 for (ttl = ctx->init_ttl; ttl <= ctx->max_ttl && sigint == 0; ++ttl)
918 __process_ttl(ctx, fd, fd_cap, ttl, pkt_snd, pkt_rcv,
919 &ss, &sd);
921 xfree(pkt_snd);
922 xfree(pkt_rcv);
924 close(fd_cap);
925 close(fd);
927 return 0;
930 int main(int argc, char **argv)
932 int c, opt_index, ret;
933 struct ctx ctx;
935 setfsuid(getuid());
936 setfsgid(getgid());
938 srand(time(NULL));
940 memset(&ctx, 0, sizeof(ctx));
941 ctx.init_ttl = 1;
942 ctx.max_ttl = 30;
943 ctx.queries = 2;
944 ctx.timeout = 2;
945 ctx.proto = IPPROTO_IP;
946 ctx.payload = NULL;
947 ctx.dev = xstrdup("eth0");
948 ctx.port = xstrdup("80");
949 ctx.bind_addr = NULL;
951 while ((c = getopt_long(argc, argv, short_options, long_options,
952 &opt_index)) != EOF) {
953 switch (c) {
954 case 'h':
955 help();
956 break;
957 case 'v':
958 version();
959 break;
960 case 'u':
961 update_geoip();
962 die();
963 break;
964 case 'H':
965 ctx.host = xstrdup(optarg);
966 break;
967 case 'p':
968 if (ctx.port)
969 xfree(ctx.port);
970 ctx.port = xstrdup(optarg);
971 break;
972 case 'n':
973 ctx.dns_resolv = 0;
974 break;
975 case '4':
976 ctx.proto = IPPROTO_IP;
977 break;
978 case '6':
979 ctx.proto = IPPROTO_IPV6;
980 break;
981 case 'Z':
982 ctx.show = 1;
983 break;
984 case 'N':
985 ctx.dns_resolv = 1;
986 break;
987 case 'f':
988 ctx.init_ttl = atoi(optarg);
989 if (ctx.init_ttl <= 0)
990 help();
991 break;
992 case 'm':
993 ctx.max_ttl = atoi(optarg);
994 if (ctx.max_ttl <= 0)
995 help();
996 break;
997 case 'b':
998 ctx.bind_addr = xstrdup(optarg);
999 break;
1000 case 'i':
1001 case 'd':
1002 free(ctx.dev);
1003 ctx.dev = xstrdup(optarg);
1004 break;
1005 case 'q':
1006 ctx.queries = atoi(optarg);
1007 if (ctx.queries <= 0)
1008 help();
1009 break;
1010 case 'x':
1011 ctx.timeout = atoi(optarg);
1012 if (ctx.timeout <= 0)
1013 help();
1014 break;
1015 case 'L':
1016 ctx.latitude = 1;
1017 break;
1018 case 'S':
1019 ctx.syn = 1;
1020 break;
1021 case 'A':
1022 ctx.ack = 1;
1023 break;
1024 case 'F':
1025 ctx.fin = 1;
1026 break;
1027 case 'U':
1028 ctx.urg = 1;
1029 break;
1030 case 'P':
1031 ctx.psh = 1;
1032 break;
1033 case 'R':
1034 ctx.rst = 1;
1035 break;
1036 case 'E':
1037 ctx.syn = 1;
1038 ctx.ecn = 1;
1039 break;
1040 case 't':
1041 ctx.tos = atoi(optarg);
1042 if (ctx.tos < 0)
1043 help();
1044 break;
1045 case 'G':
1046 ctx.nofrag = 1;
1047 break;
1048 case 'X':
1049 ctx.payload = xstrdup(optarg);
1050 break;
1051 case 'l':
1052 ctx.totlen = strtoul(optarg, NULL, 10);
1053 if (ctx.totlen == 0)
1054 help();
1055 break;
1056 case '?':
1057 switch (optopt) {
1058 case 'H':
1059 case 'p':
1060 case 'f':
1061 case 'm':
1062 case 'i':
1063 case 'd':
1064 case 'q':
1065 case 'x':
1066 case 'X':
1067 case 't':
1068 case 'l':
1069 panic("Option -%c requires an argument!\n",
1070 optopt);
1071 default:
1072 if (isprint(optopt))
1073 printf("Unknown option character `0x%X\'!\n", optopt);
1074 die();
1076 default:
1077 break;
1081 if (argc < 3 || !ctx.host || !ctx.port || ctx.init_ttl > ctx.max_ttl ||
1082 ctx.init_ttl > MAXTTL || ctx.max_ttl > MAXTTL)
1083 help();
1085 if (!device_up_and_running(ctx.dev))
1086 panic("Networking device not up and running!\n");
1087 if (device_mtu(ctx.dev) <= ctx.totlen)
1088 panic("Packet larger than device MTU!\n");
1090 register_signal(SIGHUP, signal_handler);
1091 register_signal(SIGINT, signal_handler);
1092 register_signal(SIGQUIT, signal_handler);
1093 register_signal(SIGTERM, signal_handler);
1095 tprintf_init();
1096 init_geoip(1);
1098 ret = main_trace(&ctx);
1100 destroy_geoip();
1101 tprintf_cleanup();
1103 free(ctx.dev);
1104 free(ctx.host);
1105 free(ctx.port);
1106 free(ctx.bind_addr);
1107 free(ctx.payload);
1109 return ret;