build: flowtop: Only build ioops with GeoIP support enabled
[netsniff-ng.git] / astraceroute.c
blob6b2ffc59833fddd78b67feda3d3f6f81e47729ec
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 #include <stdio.h>
9 #include <stdlib.h>
10 #include <signal.h>
11 #include <getopt.h>
12 #include <ctype.h>
13 #include <stdint.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <sys/socket.h>
17 #include <sys/fsuid.h>
18 #include <fcntl.h>
19 #include <time.h>
20 #include <string.h>
21 #include <asm/byteorder.h>
22 #include <linux/tcp.h>
23 #include <netinet/ip.h>
24 #include <netinet/ip6.h>
25 #include <netinet/in.h>
26 #include <errno.h>
27 #include <netdb.h>
28 #include <sys/time.h>
29 #include <arpa/inet.h>
30 #include <linux/if_ether.h>
31 #include <linux/icmp.h>
32 #include <linux/icmpv6.h>
34 #include "bpf.h"
35 #include "die.h"
36 #include "dev.h"
37 #include "sig.h"
38 #include "config.h"
39 #include "tprintf.h"
40 #include "pkt_buff.h"
41 #include "proto.h"
42 #include "xmalloc.h"
43 #include "csum.h"
44 #include "sock.h"
45 #include "geoip.h"
46 #include "ring.h"
47 #include "built_in.h"
49 struct ctx {
50 char *host, *port, *dev, *payload, *bind_addr;
51 size_t totlen, rcvlen;
52 int init_ttl, max_ttl, dns_resolv, queries, timeout;
53 int syn, ack, ecn, fin, psh, rst, urg, tos, nofrag, proto, show;
54 int sd_len, dport, latitude;
57 struct proto_ops {
58 int (*assembler)(uint8_t *packet, size_t len, int ttl, int proto,
59 const struct ctx *ctx, const struct sockaddr *dst,
60 const struct sockaddr *src);
61 const struct sock_filter *filter;
62 unsigned int flen;
63 size_t min_len_tcp, min_len_icmp;
64 int (*check)(uint8_t *packet, size_t len, int ttl, int id,
65 const struct sockaddr *src);
66 void (*handler)(uint8_t *packet, size_t len, int dns_resolv,
67 int latitude);
70 static sig_atomic_t sigint = 0;
72 static int assemble_ipv4(uint8_t *packet, size_t len, int ttl, int proto,
73 const struct ctx *ctx, const struct sockaddr *dst,
74 const struct sockaddr *src);
75 static int assemble_ipv6(uint8_t *packet, size_t len, int ttl, int proto,
76 const struct ctx *ctx, const struct sockaddr *dst,
77 const struct sockaddr *src);
78 static int check_ipv4(uint8_t *packet, size_t len, int ttl, int id,
79 const struct sockaddr *ss);
80 static void handle_ipv4(uint8_t *packet, size_t len, int dns_resolv,
81 int latitude);
82 static int check_ipv6(uint8_t *packet, size_t len, int ttl, int id,
83 const struct sockaddr *ss);
84 static void handle_ipv6(uint8_t *packet, size_t len, int dns_resolv,
85 int latitude);
87 static const char *short_options = "H:p:nNf:m:b:i:d:q:x:SAEFPURt:Gl:hv46X:ZuL";
88 static const struct option long_options[] = {
89 {"host", required_argument, NULL, 'H'},
90 {"port", required_argument, NULL, 'p'},
91 {"init-ttl", required_argument, NULL, 'f'},
92 {"max-ttl", required_argument, NULL, 'm'},
93 {"bind", required_argument, NULL, 'b'},
94 {"dev", required_argument, NULL, 'd'},
95 {"num-probes", required_argument, NULL, 'q'},
96 {"timeout", required_argument, NULL, 'x'},
97 {"tos", required_argument, NULL, 't'},
98 {"payload", required_argument, NULL, 'X'},
99 {"totlen", required_argument, NULL, 'l'},
100 {"numeric", no_argument, NULL, 'n'},
101 {"latitude", no_argument, NULL, 'L'},
102 {"update", no_argument, NULL, 'u'},
103 {"dns", no_argument, NULL, 'N'},
104 {"ipv4", no_argument, NULL, '4'},
105 {"ipv6", no_argument, NULL, '6'},
106 {"syn", no_argument, NULL, 'S'},
107 {"ack", no_argument, NULL, 'A'},
108 {"urg", no_argument, NULL, 'U'},
109 {"fin", no_argument, NULL, 'F'},
110 {"psh", no_argument, NULL, 'P'},
111 {"rst", no_argument, NULL, 'R'},
112 {"ecn-syn", no_argument, NULL, 'E'},
113 {"show-packet", no_argument, NULL, 'Z'},
114 {"nofrag", no_argument, NULL, 'G'},
115 {"version", no_argument, NULL, 'v'},
116 {"help", no_argument, NULL, 'h'},
117 {NULL, 0, NULL, 0}
120 static const struct sock_filter ipv4_icmp_type_11[] = {
121 { 0x28, 0, 0, 0x0000000c }, /* ldh [12] */
122 { 0x15, 0, 8, 0x00000800 }, /* jneq #0x800, drop */
123 { 0x30, 0, 0, 0x00000017 }, /* ldb [23] */
124 { 0x15, 0, 6, 0x00000001 }, /* jneq #0x1, drop */
125 { 0x28, 0, 0, 0x00000014 }, /* ldh [20] */
126 { 0x45, 4, 0, 0x00001fff }, /* jset #0x1fff, drop */
127 { 0xb1, 0, 0, 0x0000000e }, /* ldxb 4*([14]&0xf) */
128 { 0x50, 0, 0, 0x0000000e }, /* ldb [x + 14] */
129 { 0x15, 0, 1, 0x0000000b }, /* jneq #0xb, drop */
130 { 0x06, 0, 0, 0xffffffff }, /* ret #-1 */
131 { 0x06, 0, 0, 0x00000000 }, /* drop: ret #0 */
134 static const struct sock_filter ipv6_icmp6_type_3[] = {
135 { 0x28, 0, 0, 0x0000000c }, /* ldh [12] */
136 { 0x15, 0, 5, 0x000086dd }, /* jneq #0x86dd, drop */
137 { 0x30, 0, 0, 0x00000014 }, /* ldb [20] */
138 { 0x15, 0, 3, 0x0000003a }, /* jneq #0x3a, drop */
139 { 0x30, 0, 0, 0x00000036 }, /* ldb [54] */
140 { 0x15, 0, 1, 0x00000003 }, /* jneq #0x3, drop */
141 { 0x06, 0, 0, 0xffffffff }, /* ret #-1 */
142 { 0x06, 0, 0, 0x00000000 }, /* drop: ret #0 */
145 static const struct proto_ops af_ops[] = {
146 [IPPROTO_IP] = {
147 .assembler = assemble_ipv4,
148 .handler = handle_ipv4,
149 .check = check_ipv4,
150 .filter = ipv4_icmp_type_11,
151 .flen = array_size(ipv4_icmp_type_11),
152 .min_len_tcp = sizeof(struct iphdr) + sizeof(struct tcphdr),
153 .min_len_icmp = sizeof(struct iphdr) + sizeof(struct icmphdr),
155 [IPPROTO_IPV6] = {
156 .assembler = assemble_ipv6,
157 .handler = handle_ipv6,
158 .check = check_ipv6,
159 .filter = ipv6_icmp6_type_3,
160 .flen = array_size(ipv6_icmp6_type_3),
161 .min_len_tcp = sizeof(struct ip6_hdr) + sizeof(struct tcphdr),
162 .min_len_icmp = sizeof(struct ip6_hdr) + sizeof(struct icmp6hdr),
166 static void signal_handler(int number)
168 switch (number) {
169 case SIGINT:
170 case SIGQUIT:
171 case SIGTERM:
172 sigint = 1;
173 default:
174 break;
178 static void __noreturn help(void)
180 printf("\nastraceroute %s, autonomous system trace route utility\n", VERSION_STRING);
181 puts("http://www.netsniff-ng.org\n\n"
182 "Usage: astraceroute [options]\n"
183 "Options:\n"
184 " -H|--host <host> Host/IPv4/IPv6 to lookup AS route to\n"
185 " -p|--port <port> Hosts port to lookup AS route to\n"
186 " -i|-d|--dev <device> Networking device, e.g. eth0\n"
187 " -b|--bind <IP> IP address to bind to, Must specify -6 for an IPv6 address\n"
188 " -f|--init-ttl <ttl> Set initial TTL\n"
189 " -m|--max-ttl <ttl> Set maximum TTL (def: 30)\n"
190 " -q|--num-probes <num> Number of max probes for each hop (def: 2)\n"
191 " -x|--timeout <sec> Probe response timeout in sec (def: 3)\n"
192 " -X|--payload <string> Specify a payload string to test DPIs\n"
193 " -l|--totlen <len> Specify total packet len\n"
194 " -4|--ipv4 Use IPv4-only requests\n"
195 " -6|--ipv6 Use IPv6-only requests\n"
196 " -n|--numeric Do not do reverse DNS lookup for hops\n"
197 " -u|--update Update GeoIP databases\n"
198 " -L|--latitude Show latitude and longitude\n"
199 " -N|--dns Do a reverse DNS lookup for hops\n"
200 " -S|--syn Set TCP SYN flag\n"
201 " -A|--ack Set TCP ACK flag\n"
202 " -F|--fin Set TCP FIN flag\n"
203 " -P|--psh Set TCP PSH flag\n"
204 " -U|--urg Set TCP URG flag\n"
205 " -R|--rst Set TCP RST flag\n"
206 " -E|--ecn-syn Send ECN SYN packets (RFC3168)\n"
207 " -t|--tos <tos> Set the IP TOS field\n"
208 " -G|--nofrag Set do not fragment bit\n"
209 " -Z|--show-packet Show returned packet on each hop\n"
210 " -v|--version Print version and exit\n"
211 " -h|--help Print this help and exit\n\n"
212 "Examples:\n"
213 " IPv4 trace of AS with TCP SYN probe (this will most-likely pass):\n"
214 " astraceroute -i eth0 -N -S -H netsniff-ng.org\n"
215 " IPv4 trace of AS with TCP ECN SYN probe:\n"
216 " astraceroute -i eth0 -N -E -H netsniff-ng.org\n"
217 " IPv4 trace of AS with TCP FIN probe:\n"
218 " astraceroute -i eth0 -N -F -H netsniff-ng.org\n"
219 " IPv4 trace of AS with Xmas probe:\n"
220 " astraceroute -i eth0 -N -FPU -H netsniff-ng.org\n"
221 " IPv4 trace of AS with Null probe with ASCII payload:\n"
222 " astraceroute -i eth0 -N -H netsniff-ng.org -X \"censor-me\" -Z\n"
223 " IPv6 trace of AS up to www.6bone.net:\n"
224 " astraceroute -6 -i eth0 -S -E -N -H www.6bone.net\n\n"
225 "Note:\n"
226 " If the TCP probe did not give any results, then astraceroute will\n"
227 " automatically probe for classic ICMP packets! To gather more\n"
228 " information about astraceroute's fetched AS numbers, see e.g.\n"
229 " http://bgp.he.net/AS<number>!\n\n"
230 "Please report bugs to <bugs@netsniff-ng.org>\n"
231 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
232 "Swiss federal institute of technology (ETH Zurich)\n"
233 "License: GNU GPL version 2.0\n"
234 "This is free software: you are free to change and redistribute it.\n"
235 "There is NO WARRANTY, to the extent permitted by law.\n");
236 die();
239 static void __noreturn version(void)
241 printf("\nastraceroute %s, Git id: %s\n", VERSION_LONG, GITVERSION);
242 puts("autonomous system trace route utility\n"
243 "http://www.netsniff-ng.org\n\n"
244 "Please report bugs to <bugs@netsniff-ng.org>\n"
245 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
246 "Swiss federal institute of technology (ETH Zurich)\n"
247 "License: GNU GPL version 2.0\n"
248 "This is free software: you are free to change and redistribute it.\n"
249 "There is NO WARRANTY, to the extent permitted by law.\n");
250 die();
253 static void __assemble_data(uint8_t *packet, size_t len, const char *payload)
255 size_t i;
257 if (payload == NULL) {
258 for (i = 0; i < len; ++i)
259 packet[i] = (uint8_t) rand();
260 } else {
261 size_t lmin = min(len, strlen(payload));
263 for (i = 0; i < lmin; ++i)
264 packet[i] = (uint8_t) payload[i];
265 for (i = lmin; i < len; ++i)
266 packet[i] = (uint8_t) rand();
270 static void __assemble_icmp4(uint8_t *packet, size_t len)
272 struct icmphdr *icmph = (struct icmphdr *) packet;
274 bug_on(len < sizeof(struct icmphdr));
276 icmph->type = ICMP_ECHO;
277 icmph->code = 0;
278 icmph->checksum = 0;
281 static void __assemble_icmp6(uint8_t *packet, size_t len)
283 struct icmp6hdr *icmp6h = (struct icmp6hdr *) packet;
285 bug_on(len < sizeof(struct icmp6hdr));
287 icmp6h->icmp6_type = ICMPV6_ECHO_REQUEST;
288 icmp6h->icmp6_code = 0;
289 icmp6h->icmp6_cksum = 0;
292 static void __assemble_tcp(uint8_t *packet, size_t len, int syn, int ack,
293 int urg, int fin, int rst, int psh, int ecn,
294 int dport)
296 struct tcphdr *tcph = (struct tcphdr *) packet;
298 bug_on(len < sizeof(struct tcphdr));
300 tcph->source = htons((uint16_t) rand());
301 tcph->dest = htons((uint16_t) dport);
303 tcph->seq = htonl(rand());
304 tcph->ack_seq = (!!ack ? htonl(rand()) : 0);
306 tcph->doff = 5;
308 tcph->syn = !!syn;
309 tcph->ack = !!ack;
310 tcph->urg = !!urg;
311 tcph->fin = !!fin;
312 tcph->rst = !!rst;
313 tcph->psh = !!psh;
314 tcph->ece = !!ecn;
315 tcph->cwr = !!ecn;
317 tcph->window = htons((uint16_t) (100 + (rand() % 65435)));
318 tcph->urg_ptr = (!!urg ? htons((uint16_t) rand()) : 0);
319 tcph->check = 0;
322 static int assemble_ipv4(uint8_t *packet, size_t len, int ttl, int proto,
323 const struct ctx *ctx, const struct sockaddr *dst,
324 const struct sockaddr *src)
326 uint8_t *data;
327 size_t data_len, off_next = 0;
328 struct iphdr *iph = (struct iphdr *) packet;
330 bug_on(!src || !dst);
331 bug_on(src->sa_family != PF_INET || dst->sa_family != PF_INET);
332 bug_on(len < sizeof(*iph) + min(sizeof(struct tcphdr),
333 sizeof(struct icmphdr)));
335 iph->ihl = 5;
336 iph->version = 4;
337 iph->tos = (uint8_t) ctx->tos;
339 iph->tot_len = htons((uint16_t) len);
340 iph->id = htons((uint16_t) rand());
342 iph->frag_off = ctx->nofrag ? IP_DF : 0;
343 iph->ttl = (uint8_t) ttl;
345 iph->saddr = ((const struct sockaddr_in *) src)->sin_addr.s_addr;
346 iph->daddr = ((const struct sockaddr_in *) dst)->sin_addr.s_addr;
348 iph->protocol = (uint8_t) proto;
350 data = packet + sizeof(*iph);
351 data_len = len - sizeof(*iph);
353 switch (proto) {
354 case IPPROTO_TCP:
355 __assemble_tcp(data, data_len, ctx->syn, ctx->ack, ctx->urg,
356 ctx->fin, ctx->rst, ctx->psh, ctx->ecn, ctx->dport);
357 off_next = sizeof(struct tcphdr);
358 break;
359 case IPPROTO_ICMP:
360 __assemble_icmp4(data, data_len);
361 off_next = sizeof(struct icmphdr);
362 break;
363 default:
364 bug();
367 data = packet + sizeof(*iph) + off_next;
368 data_len = len - sizeof(*iph) - off_next;
370 __assemble_data(data, data_len, ctx->payload);
372 iph->check = csum((unsigned short *) packet, ntohs(iph->tot_len) >> 1);
374 return ntohs(iph->id);
377 static int assemble_ipv6(uint8_t *packet, size_t len, int ttl, int proto,
378 const struct ctx *ctx, const struct sockaddr *dst,
379 const struct sockaddr *src)
381 uint8_t *data;
382 size_t data_len, off_next = 0;
383 struct ip6_hdr *ip6h = (struct ip6_hdr *) packet;
385 bug_on(!src || !dst);
386 bug_on(src->sa_family != PF_INET6 || dst->sa_family != PF_INET6);
387 bug_on(len < sizeof(*ip6h) + min(sizeof(struct tcphdr),
388 sizeof(struct icmp6hdr)));
390 ip6h->ip6_flow = htonl(rand() & 0x000fffff);
391 ip6h->ip6_vfc = 0x60;
393 ip6h->ip6_plen = htons((uint16_t) len - sizeof(*ip6h));
394 ip6h->ip6_nxt = (uint8_t) proto;
395 ip6h->ip6_hlim = (uint8_t) ttl;
397 memcpy(&ip6h->ip6_src, &(((const struct sockaddr_in6 *)
398 src)->sin6_addr), sizeof(ip6h->ip6_src));
399 memcpy(&ip6h->ip6_dst, &(((const struct sockaddr_in6 *)
400 dst)->sin6_addr), sizeof(ip6h->ip6_dst));
402 data = packet + sizeof(*ip6h);
403 data_len = len - sizeof(*ip6h);
405 switch (proto) {
406 case IPPROTO_TCP:
407 __assemble_tcp(data, data_len, ctx->syn, ctx->ack, ctx->urg,
408 ctx->fin, ctx->rst, ctx->psh, ctx->ecn, ctx->dport);
409 off_next = sizeof(struct tcphdr);
410 break;
411 case IPPROTO_ICMP:
412 case IPPROTO_ICMPV6:
413 __assemble_icmp6(data, data_len);
414 off_next = sizeof(struct icmp6hdr);
415 break;
416 default:
417 bug();
420 data = packet + sizeof(*ip6h) + off_next;
421 data_len = len - sizeof(*ip6h) - off_next;
423 __assemble_data(data, data_len, ctx->payload);
425 return ntohl(ip6h->ip6_flow) & 0x000fffff;
428 static int check_ipv4(uint8_t *packet, size_t len, int ttl __maybe_unused,
429 int id, const struct sockaddr *ss)
431 struct iphdr *iph = (struct iphdr *) packet;
432 struct iphdr *iph_inner;
433 struct icmphdr *icmph;
435 if (iph->protocol != IPPROTO_ICMP)
436 return -EINVAL;
437 if (iph->daddr != ((const struct sockaddr_in *) ss)->sin_addr.s_addr)
438 return -EINVAL;
440 icmph = (struct icmphdr *) (packet + sizeof(struct iphdr));
441 if (icmph->type != ICMP_TIME_EXCEEDED)
442 return -EINVAL;
443 if (icmph->code != ICMP_EXC_TTL)
444 return -EINVAL;
446 iph_inner = (struct iphdr *) (packet + sizeof(struct iphdr) +
447 sizeof(struct icmphdr));
448 if (ntohs(iph_inner->id) != id)
449 return -EINVAL;
451 return len;
454 static void handle_ipv4(uint8_t *packet, size_t len __maybe_unused,
455 int dns_resolv, int latitude)
457 char hbuff[NI_MAXHOST];
458 struct iphdr *iph = (struct iphdr *) packet;
459 struct sockaddr_in sd;
460 struct hostent *hent;
461 const char *as, *country, *city;
463 memset(hbuff, 0, sizeof(hbuff));
464 memset(&sd, 0, sizeof(sd));
465 sd.sin_family = PF_INET;
466 sd.sin_addr.s_addr = iph->saddr;
468 getnameinfo((struct sockaddr *) &sd, sizeof(sd),
469 hbuff, sizeof(hbuff), NULL, 0, NI_NUMERICHOST);
471 as = geoip4_as_name(&sd);
472 country = geoip4_country_name(&sd);
473 city = geoip4_city_name(&sd);
475 if (dns_resolv) {
476 hent = gethostbyaddr(&sd.sin_addr, sizeof(sd.sin_addr), PF_INET);
477 if (hent)
478 printf(" %s (%s)", hent->h_name, hbuff);
479 else
480 printf(" %s", hbuff);
481 } else {
482 printf(" %s", hbuff);
484 if (as)
485 printf(" in %s", as);
486 if (country) {
487 printf(" in %s", country);
488 if (city)
489 printf(", %s", city);
491 if (latitude)
492 printf(" (%f/%f)", geoip4_latitude(&sd), geoip4_longitude(&sd));
495 static int check_ipv6(uint8_t *packet, size_t len, int ttl __maybe_unused,
496 int id, const struct sockaddr *ss)
498 struct ip6_hdr *ip6h = (struct ip6_hdr *) packet;
499 struct ip6_hdr *ip6h_inner;
500 struct icmp6hdr *icmp6h;
502 if (ip6h->ip6_nxt != 0x3a)
503 return -EINVAL;
504 if (memcmp(&ip6h->ip6_dst, &(((const struct sockaddr_in6 *)
505 ss)->sin6_addr), sizeof(ip6h->ip6_dst)))
506 return -EINVAL;
508 icmp6h = (struct icmp6hdr *) (packet + sizeof(*ip6h));
509 if (icmp6h->icmp6_type != ICMPV6_TIME_EXCEED)
510 return -EINVAL;
511 if (icmp6h->icmp6_code != ICMPV6_EXC_HOPLIMIT)
512 return -EINVAL;
514 ip6h_inner = (struct ip6_hdr *) (packet + sizeof(*ip6h) + sizeof(*icmp6h));
515 if ((ntohl(ip6h_inner->ip6_flow) & 0x000fffff) != (uint32_t) id)
516 return -EINVAL;
518 return len;
521 static void handle_ipv6(uint8_t *packet, size_t len __maybe_unused,
522 int dns_resolv, int latitude)
524 char hbuff[NI_MAXHOST];
525 struct ip6_hdr *ip6h = (struct ip6_hdr *) packet;
526 struct sockaddr_in6 sd;
527 struct hostent *hent;
528 const char *as, *country, *city;
530 memset(hbuff, 0, sizeof(hbuff));
531 memset(&sd, 0, sizeof(sd));
532 sd.sin6_family = PF_INET6;
533 memcpy(&sd.sin6_addr, &ip6h->ip6_src, sizeof(ip6h->ip6_src));
535 getnameinfo((struct sockaddr *) &sd, sizeof(sd),
536 hbuff, sizeof(hbuff), NULL, 0, NI_NUMERICHOST);
538 as = geoip6_as_name(&sd);
539 country = geoip6_country_name(&sd);
540 city = geoip6_city_name(&sd);
542 if (dns_resolv) {
543 hent = gethostbyaddr(&sd.sin6_addr, sizeof(sd.sin6_addr), PF_INET6);
544 if (hent)
545 printf(" %s (%s)", hent->h_name, hbuff);
546 else
547 printf(" %s", hbuff);
548 } else {
549 printf(" %s", hbuff);
551 if (as)
552 printf(" in %s", as);
553 if (country) {
554 printf(" in %s", country);
555 if (city)
556 printf(", %s", city);
558 if (latitude)
559 printf(" (%f/%f)", geoip6_latitude(&sd), geoip6_longitude(&sd));
562 static void show_trace_info(struct ctx *ctx, const struct sockaddr_storage *ss,
563 const struct sockaddr_storage *sd)
565 char hbuffs[256], hbuffd[256];
567 memset(hbuffd, 0, sizeof(hbuffd));
568 getnameinfo((struct sockaddr *) sd, sizeof(*sd),
569 hbuffd, sizeof(hbuffd), NULL, 0, NI_NUMERICHOST);
571 memset(hbuffs, 0, sizeof(hbuffs));
572 getnameinfo((struct sockaddr *) ss, sizeof(*ss),
573 hbuffs, sizeof(hbuffs), NULL, 0, NI_NUMERICHOST);
575 printf("AS path IPv%d TCP trace from %s to %s:%s (%s) with len %zu "
576 "Bytes, %u max hops\n", ctx->proto == IPPROTO_IP ? 4 : 6,
577 hbuffs, hbuffd, ctx->port, ctx->host, ctx->totlen, ctx->max_ttl);
579 printf("Using flags SYN:%d,ACK:%d,ECN:%d,FIN:%d,PSH:%d,RST:%d,URG:%d\n",
580 ctx->syn, ctx->ack, ctx->ecn, ctx->fin, ctx->psh, ctx->rst, ctx->urg);
582 if (ctx->payload)
583 printf("With payload: \'%s\'\n", ctx->payload);
586 static int get_remote_fd(struct ctx *ctx, struct sockaddr_storage *ss,
587 struct sockaddr_storage *sd)
589 int fd = -1, ret, one = 1, af = AF_INET;
590 struct addrinfo hints, *ahead, *ai;
591 unsigned char bind_ip[sizeof(struct in6_addr)];
593 memset(&hints, 0, sizeof(hints));
594 hints.ai_family = PF_UNSPEC;
595 hints.ai_socktype = SOCK_STREAM;
596 hints.ai_protocol = IPPROTO_TCP;
597 hints.ai_flags = AI_NUMERICSERV;
599 ret = getaddrinfo(ctx->host, ctx->port, &hints, &ahead);
600 if (ret < 0)
601 panic("Cannot get address info!\n");
603 for (ai = ahead; ai != NULL && fd < 0; ai = ai->ai_next) {
604 if (!((ai->ai_family == PF_INET6 && ctx->proto == IPPROTO_IPV6) ||
605 (ai->ai_family == PF_INET && ctx->proto == IPPROTO_IP)))
606 continue;
608 fd = socket(ai->ai_family, SOCK_RAW, IPPROTO_RAW);
609 if (fd < 0)
610 continue;
612 memset(ss, 0, sizeof(*ss));
613 ret = device_address(ctx->dev, ai->ai_family, ss);
614 if (ret < 0 && !ctx->bind_addr)
615 panic("Cannot get own device address!\n");
617 if (ctx->bind_addr) {
618 if (ctx->proto == IPPROTO_IPV6)
619 af = AF_INET6;
621 if (inet_pton(af, ctx->bind_addr, &bind_ip) != 1)
622 panic("Address is invalid!\n");
624 if (af == AF_INET6) {
625 struct sockaddr_in6 *ss6 = (struct sockaddr_in6 *) ss;
626 memcpy(&ss6->sin6_addr.s6_addr, &bind_ip, sizeof(struct in6_addr));
627 } else {
628 struct sockaddr_in *ss4 = (struct sockaddr_in *) ss;
629 memcpy(&ss4->sin_addr.s_addr, &bind_ip, sizeof(struct in_addr));
633 ret = bind(fd, (struct sockaddr *) ss, sizeof(*ss));
634 if (ret < 0)
635 panic("Cannot bind socket!\n");
637 memset(sd, 0, sizeof(*sd));
638 memcpy(sd, ai->ai_addr, ai->ai_addrlen);
640 ctx->sd_len = ai->ai_addrlen;
641 ctx->dport = strtoul(ctx->port, NULL, 10);
643 ret = setsockopt(fd, ctx->proto, IP_HDRINCL, &one, sizeof(one));
644 if (ret < 0)
645 panic("Kernel does not support IP_HDRINCL!\n");
647 if (ai->ai_family == PF_INET6) {
648 struct sockaddr_in6 *sd6 = (struct sockaddr_in6 *) sd;
650 sd6->sin6_port = 0;
653 break;
656 freeaddrinfo(ahead);
658 if (fd < 0)
659 panic("Cannot create socket! Does remote "
660 "support IPv%d?!\n",
661 ctx->proto == IPPROTO_IP ? 4 : 6);
663 return fd;
666 static void inject_filter(struct ctx *ctx, int fd)
668 struct sock_fprog bpf_ops;
670 enable_kernel_bpf_jit_compiler();
672 memset(&bpf_ops, 0, sizeof(bpf_ops));
673 bpf_ops.filter = (struct sock_filter *) af_ops[ctx->proto].filter;
674 bpf_ops.len = af_ops[ctx->proto].flen;
676 bpf_attach_to_sock(fd, &bpf_ops);
679 static int __process_node(struct ctx *ctx, int fd, int fd_cap, int ttl,
680 int inner_proto, uint8_t *pkt_snd, uint8_t *pkt_rcv,
681 const struct sockaddr_storage *ss,
682 const struct sockaddr_storage *sd, struct timeval *diff)
684 int pkt_id, ret, timeout;
685 struct pollfd pfd;
686 struct timeval start, end;
688 prepare_polling(fd_cap, &pfd);
690 memset(pkt_snd, 0, ctx->totlen);
691 pkt_id = af_ops[ctx->proto].assembler(pkt_snd, ctx->totlen, ttl,
692 inner_proto, ctx,
693 (const struct sockaddr *) sd,
694 (const struct sockaddr *) ss);
696 ret = sendto(fd, pkt_snd, ctx->totlen, 0, (struct sockaddr *) sd,
697 ctx->sd_len);
698 if (ret < 0)
699 panic("sendto failed: %s\n", strerror(errno));
701 bug_on(gettimeofday(&start, NULL));
703 timeout = (ctx->timeout > 0 ? ctx->timeout : 2) * 1000;
705 ret = poll(&pfd, 1, timeout);
706 if (ret > 0 && pfd.revents & POLLIN && sigint == 0) {
707 bug_on(gettimeofday(&end, NULL));
708 if (diff)
709 timersub(&end, &start, diff);
711 ret = recvfrom(fd_cap, pkt_rcv, ctx->rcvlen, 0, NULL, NULL);
712 if (ret < (int) (sizeof(struct ethhdr) + af_ops[ctx->proto].min_len_icmp))
713 return -EIO;
715 return af_ops[ctx->proto].check(pkt_rcv + sizeof(struct ethhdr),
716 ret - sizeof(struct ethhdr), ttl,
717 pkt_id, (const struct sockaddr *) ss);
718 } else {
719 return -EIO;
722 return 0;
725 static void timerdiv(const unsigned long divisor, const struct timeval *tv,
726 struct timeval *result)
728 uint64_t x = ((uint64_t) tv->tv_sec * 1000 * 1000 + tv->tv_usec) / divisor;
730 result->tv_sec = x / 1000 / 1000;
731 result->tv_usec = x % (1000 * 1000);
734 static int timevalcmp(const void *t1, const void *t2)
736 if (timercmp((struct timeval *) t1, (struct timeval *) t2, <))
737 return -1;
738 if (timercmp((struct timeval *) t1, (struct timeval *) t2, >))
739 return 1;
741 return 0;
744 static int __process_time(struct ctx *ctx, int fd, int fd_cap, int ttl,
745 int inner_proto, uint8_t *pkt_snd, uint8_t *pkt_rcv,
746 const struct sockaddr_storage *ss,
747 const struct sockaddr_storage *sd)
749 size_t i, j = 0;
750 int good = 0, ret = -EIO, idx, ret_good = -EIO;
751 struct timeval probes[9], *tmp, sum, res;
752 uint8_t *trash = xmalloc(ctx->rcvlen);
753 char *cwait[] = { "-", "\\", "|", "/" };
754 const char *proto_short[] = {
755 [IPPROTO_TCP] = "t",
756 [IPPROTO_ICMP] = "i",
757 [IPPROTO_ICMPV6] = "i",
760 memset(probes, 0, sizeof(probes));
761 for (i = 0; i < array_size(probes) && sigint == 0; ++i) {
762 ret = __process_node(ctx, fd, fd_cap, ttl, inner_proto,
763 pkt_snd, good == 0 ? pkt_rcv : trash,
764 ss, sd, &probes[i]);
765 if (ret > 0) {
766 if (good == 0)
767 ret_good = ret;
768 good++;
771 if (good == 0 && ctx->queries == (int) i)
772 break;
774 usleep(50000);
776 printf("\r%2d: %s", ttl, cwait[j++]);
777 fflush(stdout);
778 if (j >= array_size(cwait))
779 j = 0;
782 if (good == 0) {
783 xfree(trash);
784 return -EIO;
787 tmp = xcalloc(good, sizeof(struct timeval));
788 for (i = j = 0; i < array_size(probes); ++i) {
789 if (probes[i].tv_sec == 0 && probes[i].tv_usec == 0)
790 continue;
791 tmp[j].tv_sec = probes[i].tv_sec;
792 tmp[j].tv_usec = probes[i].tv_usec;
793 j++;
796 qsort(tmp, j, sizeof(struct timeval), timevalcmp);
798 printf("\r%2d: %s[", ttl, proto_short[inner_proto]);
799 idx = j / 2;
800 switch (j % 2) {
801 case 0:
802 timeradd(&tmp[idx], &tmp[idx - 1], &sum);
803 timerdiv(2, &sum, &res);
804 if (res.tv_sec > 0)
805 printf("%lu sec ", res.tv_sec);
806 printf("%7lu us", res.tv_usec);
807 break;
808 case 1:
809 if (tmp[idx].tv_sec > 0)
810 printf("%lu sec ", tmp[idx].tv_sec);
811 printf("%7lu us", tmp[idx].tv_usec);
812 break;
814 printf("]");
816 xfree(tmp);
817 xfree(trash);
819 return ret_good;
822 static int __probe_remote(struct ctx *ctx, int fd, int fd_cap, int ttl,
823 uint8_t *pkt_snd, uint8_t *pkt_rcv,
824 const struct sockaddr_storage *ss,
825 const struct sockaddr_storage *sd,
826 int inner_proto)
828 int ret = -EIO, tries = ctx->queries;
830 while (tries-- > 0 && sigint == 0) {
831 ret = __process_time(ctx, fd, fd_cap, ttl, inner_proto,
832 pkt_snd, pkt_rcv, ss, sd);
833 if (ret < 0)
834 continue;
836 af_ops[ctx->proto].handler(pkt_rcv + sizeof(struct ethhdr),
837 ret - sizeof(struct ethhdr),
838 ctx->dns_resolv, ctx->latitude);
839 if (ctx->show) {
840 struct pkt_buff *pkt;
842 printf("\n");
843 pkt = pkt_alloc(pkt_rcv, ret);
844 hex_ascii(pkt);
845 tprintf_flush();
846 pkt_free(pkt);
849 break;
852 return ret;
855 static int __process_ttl(struct ctx *ctx, int fd, int fd_cap, int ttl,
856 uint8_t *pkt_snd, uint8_t *pkt_rcv,
857 const struct sockaddr_storage *ss,
858 const struct sockaddr_storage *sd)
860 int ret = -EIO;
861 size_t i;
862 const int inner_protos[] = {
863 IPPROTO_TCP,
864 IPPROTO_ICMP,
867 printf("%2d: ", ttl);
868 fflush(stdout);
870 for (i = 0; i < array_size(inner_protos) && sigint == 0; ++i) {
871 ret = __probe_remote(ctx, fd, fd_cap, ttl, pkt_snd, pkt_rcv, ss, sd,
872 inner_protos[i]);
873 if (ret > 0)
874 break;
877 if (ret <= 0)
878 printf("\r%2d: ?[ no answer]", ttl);
879 if (ctx->show == 0)
880 printf("\n");
881 if (ctx->show && ret <= 0)
882 printf("\n\n");
884 fflush(stdout);
885 return 0;
888 static int main_trace(struct ctx *ctx)
890 int fd, fd_cap, ifindex, ttl;
891 struct ring dummy_ring;
892 struct sockaddr_storage ss, sd;
893 uint8_t *pkt_snd, *pkt_rcv;
895 fd = get_remote_fd(ctx, &ss, &sd);
896 fd_cap = pf_socket();
898 inject_filter(ctx, fd_cap);
900 ifindex = device_ifindex(ctx->dev);
901 bind_ring_generic(fd_cap, &dummy_ring, ifindex, false);
903 if (ctx->totlen < af_ops[ctx->proto].min_len_tcp) {
904 ctx->totlen = af_ops[ctx->proto].min_len_tcp;
905 if (ctx->payload)
906 ctx->totlen += strlen(ctx->payload);
909 ctx->rcvlen = device_mtu(ctx->dev) - sizeof(struct ethhdr);
910 if (ctx->totlen >= ctx->rcvlen)
911 panic("Packet len exceeds device MTU!\n");
913 pkt_snd = xmalloc(ctx->totlen);
914 pkt_rcv = xmalloc(ctx->rcvlen);
916 show_trace_info(ctx, &ss, &sd);
918 for (ttl = ctx->init_ttl; ttl <= ctx->max_ttl && sigint == 0; ++ttl)
919 __process_ttl(ctx, fd, fd_cap, ttl, pkt_snd, pkt_rcv,
920 &ss, &sd);
922 xfree(pkt_snd);
923 xfree(pkt_rcv);
925 close(fd_cap);
926 close(fd);
928 return 0;
931 int main(int argc, char **argv)
933 int c, opt_index, ret;
934 struct ctx ctx;
936 setfsuid(getuid());
937 setfsgid(getgid());
939 srand(time(NULL));
941 memset(&ctx, 0, sizeof(ctx));
942 ctx.init_ttl = 1;
943 ctx.max_ttl = 30;
944 ctx.queries = 2;
945 ctx.timeout = 2;
946 ctx.proto = IPPROTO_IP;
947 ctx.payload = NULL;
948 ctx.dev = xstrdup("eth0");
949 ctx.port = xstrdup("80");
950 ctx.bind_addr = NULL;
952 while ((c = getopt_long(argc, argv, short_options, long_options,
953 &opt_index)) != EOF) {
954 switch (c) {
955 case 'h':
956 help();
957 break;
958 case 'v':
959 version();
960 break;
961 case 'u':
962 update_geoip();
963 die();
964 break;
965 case 'H':
966 ctx.host = xstrdup(optarg);
967 break;
968 case 'p':
969 if (ctx.port)
970 xfree(ctx.port);
971 ctx.port = xstrdup(optarg);
972 break;
973 case 'n':
974 ctx.dns_resolv = 0;
975 break;
976 case '4':
977 ctx.proto = IPPROTO_IP;
978 break;
979 case '6':
980 ctx.proto = IPPROTO_IPV6;
981 break;
982 case 'Z':
983 ctx.show = 1;
984 break;
985 case 'N':
986 ctx.dns_resolv = 1;
987 break;
988 case 'f':
989 ctx.init_ttl = atoi(optarg);
990 if (ctx.init_ttl <= 0)
991 help();
992 break;
993 case 'm':
994 ctx.max_ttl = atoi(optarg);
995 if (ctx.max_ttl <= 0)
996 help();
997 break;
998 case 'b':
999 ctx.bind_addr = xstrdup(optarg);
1000 break;
1001 case 'i':
1002 case 'd':
1003 free(ctx.dev);
1004 ctx.dev = xstrdup(optarg);
1005 break;
1006 case 'q':
1007 ctx.queries = atoi(optarg);
1008 if (ctx.queries <= 0)
1009 help();
1010 break;
1011 case 'x':
1012 ctx.timeout = atoi(optarg);
1013 if (ctx.timeout <= 0)
1014 help();
1015 break;
1016 case 'L':
1017 ctx.latitude = 1;
1018 break;
1019 case 'S':
1020 ctx.syn = 1;
1021 break;
1022 case 'A':
1023 ctx.ack = 1;
1024 break;
1025 case 'F':
1026 ctx.fin = 1;
1027 break;
1028 case 'U':
1029 ctx.urg = 1;
1030 break;
1031 case 'P':
1032 ctx.psh = 1;
1033 break;
1034 case 'R':
1035 ctx.rst = 1;
1036 break;
1037 case 'E':
1038 ctx.syn = 1;
1039 ctx.ecn = 1;
1040 break;
1041 case 't':
1042 ctx.tos = atoi(optarg);
1043 if (ctx.tos < 0)
1044 help();
1045 break;
1046 case 'G':
1047 ctx.nofrag = 1;
1048 break;
1049 case 'X':
1050 ctx.payload = xstrdup(optarg);
1051 break;
1052 case 'l':
1053 ctx.totlen = strtoul(optarg, NULL, 10);
1054 if (ctx.totlen == 0)
1055 help();
1056 break;
1057 case '?':
1058 switch (optopt) {
1059 case 'H':
1060 case 'p':
1061 case 'f':
1062 case 'm':
1063 case 'i':
1064 case 'd':
1065 case 'q':
1066 case 'x':
1067 case 'X':
1068 case 't':
1069 case 'l':
1070 panic("Option -%c requires an argument!\n",
1071 optopt);
1072 default:
1073 if (isprint(optopt))
1074 printf("Unknown option character `0x%X\'!\n", optopt);
1075 die();
1077 default:
1078 break;
1082 if (argc < 3 || !ctx.host || !ctx.port || ctx.init_ttl > ctx.max_ttl ||
1083 ctx.init_ttl > MAXTTL || ctx.max_ttl > MAXTTL)
1084 help();
1086 if (!device_up_and_running(ctx.dev))
1087 panic("Networking device not up and running!\n");
1088 if (device_mtu(ctx.dev) <= ctx.totlen)
1089 panic("Packet larger than device MTU!\n");
1091 register_signal(SIGHUP, signal_handler);
1092 register_signal(SIGINT, signal_handler);
1093 register_signal(SIGQUIT, signal_handler);
1094 register_signal(SIGTERM, signal_handler);
1096 tprintf_init();
1097 init_geoip(1);
1099 ret = main_trace(&ctx);
1101 destroy_geoip();
1102 tprintf_cleanup();
1104 free(ctx.dev);
1105 free(ctx.host);
1106 free(ctx.port);
1107 free(ctx.bind_addr);
1108 free(ctx.payload);
1110 return ret;