trafgen: fix -t 0 option to use sendto
[netsniff-ng-new.git] / astraceroute.c
blobd70802a0d927bc64137430db33b800cb53f52cfa
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/if_ether.h>
25 #include <netinet/ip.h>
26 #include <netinet/ip6.h>
27 #include <netinet/in.h>
28 #include <errno.h>
29 #include <netdb.h>
30 #include <sys/time.h>
31 #include <arpa/inet.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, const struct ctx *ctx)
271 uint8_t *data;
272 size_t data_len;
273 struct icmphdr *icmph = (struct icmphdr *) packet;
275 bug_on(len < sizeof(*icmph));
277 icmph->type = ICMP_ECHO;
278 icmph->code = 0;
279 icmph->checksum = 0;
281 data = packet + sizeof(*icmph);
282 data_len = len - sizeof(*icmph);
284 __assemble_data(data, data_len, ctx->payload);
286 icmph->checksum = csum((unsigned short *)packet, len / 2);
289 static void __assemble_icmp6(uint8_t *packet, size_t len, const struct ctx *ctx,
290 const struct sockaddr *dst, const struct sockaddr *src)
292 uint8_t *data;
293 size_t data_len;
294 struct ip6_hdr ip6hdr;
295 struct icmp6hdr *icmp6h = (struct icmp6hdr *) packet;
297 bug_on(len < sizeof(*icmp6h));
299 icmp6h->icmp6_type = ICMPV6_ECHO_REQUEST;
300 icmp6h->icmp6_code = 0;
301 icmp6h->icmp6_cksum = 0;
303 data = packet + sizeof(*icmp6h);
304 data_len = len - sizeof(*icmp6h);
306 __assemble_data(data, data_len, ctx->payload);
308 memcpy(&ip6hdr.ip6_src, &((const struct sockaddr_in6 *) src)->sin6_addr,
309 sizeof(ip6hdr.ip6_src));
310 memcpy(&ip6hdr.ip6_dst, &((const struct sockaddr_in6 *) dst)->sin6_addr,
311 sizeof(ip6hdr.ip6_dst));
313 icmp6h->icmp6_cksum = p6_csum(&ip6hdr, packet, sizeof(*icmp6h) + data_len, IPPROTO_ICMPV6);
316 static size_t __assemble_tcp_header(struct tcphdr *tcph, const struct ctx *ctx)
318 tcph->source = htons((uint16_t) rand());
319 tcph->dest = htons((uint16_t) ctx->dport);
321 tcph->seq = htonl(rand());
322 tcph->ack_seq = (!!ctx->ack ? htonl(rand()) : 0);
324 tcph->doff = 5;
326 tcph->syn = !!ctx->syn;
327 tcph->ack = !!ctx->ack;
328 tcph->urg = !!ctx->urg;
329 tcph->fin = !!ctx->fin;
330 tcph->rst = !!ctx->rst;
331 tcph->psh = !!ctx->psh;
332 tcph->ece = !!ctx->ecn;
333 tcph->cwr = !!ctx->ecn;
335 tcph->window = htons((uint16_t) (100 + (rand() % 65435)));
336 tcph->check = 0;
337 tcph->urg_ptr = (!!ctx->urg ? htons((uint16_t) rand()) : 0);
339 return tcph->doff * 4;
342 static void __assemble_tcp(uint8_t *packet, size_t len, const struct ctx *ctx,
343 const struct sockaddr *dst, const struct sockaddr *src)
346 uint8_t *data;
347 size_t tcp_len, data_len;
348 struct ip iphdr;
349 struct tcphdr *tcph = (struct tcphdr *) packet;
351 bug_on(len < sizeof(*tcph));
353 tcp_len = __assemble_tcp_header(tcph, ctx);
355 data = packet + tcp_len;
356 data_len = len - tcp_len;
358 __assemble_data(data, data_len, ctx->payload);
360 memcpy(&iphdr.ip_src, &((const struct sockaddr_in *) src)->sin_addr.s_addr,
361 sizeof(iphdr.ip_src));
362 memcpy(&iphdr.ip_dst, &((const struct sockaddr_in *) dst)->sin_addr.s_addr,
363 sizeof(iphdr.ip_dst));
365 tcph->check = p4_csum(&iphdr, packet, tcp_len + data_len, IPPROTO_TCP);
368 static void __assemble_tcp6(uint8_t *packet, size_t len, const struct ctx *ctx,
369 const struct sockaddr *dst, const struct sockaddr *src)
371 uint8_t *data;
372 size_t tcp_len, data_len;
373 struct ip6_hdr ip6hdr;
374 struct tcphdr *tcph = (struct tcphdr *) packet;
376 bug_on(len < sizeof(*tcph));
378 tcp_len = __assemble_tcp_header(tcph, ctx);
380 data = packet + tcp_len;
381 data_len = len - tcp_len;
383 __assemble_data(data, data_len, ctx->payload);
385 memcpy(&ip6hdr.ip6_src, &((const struct sockaddr_in6 *) src)->sin6_addr,
386 sizeof(ip6hdr.ip6_src));
387 memcpy(&ip6hdr.ip6_dst, &((const struct sockaddr_in6 *) dst)->sin6_addr,
388 sizeof(ip6hdr.ip6_dst));
390 tcph->check = p6_csum(&ip6hdr, packet, tcp_len + data_len, IPPROTO_TCP);
393 static int assemble_ipv4(uint8_t *packet, size_t len, int ttl, int proto,
394 const struct ctx *ctx, const struct sockaddr *dst,
395 const struct sockaddr *src)
397 uint8_t *data;
398 size_t data_len;
399 struct iphdr *iph = (struct iphdr *) packet;
401 bug_on(!src || !dst);
402 bug_on(src->sa_family != PF_INET || dst->sa_family != PF_INET);
403 bug_on(len < sizeof(*iph) + min(sizeof(struct tcphdr),
404 sizeof(struct icmphdr)));
406 iph->ihl = 5;
407 iph->version = 4;
408 iph->tos = (uint8_t) ctx->tos;
410 iph->tot_len = htons((uint16_t) len);
411 iph->id = htons((uint16_t) rand());
413 iph->frag_off = ctx->nofrag ? IP_DF : 0;
414 iph->ttl = (uint8_t) ttl;
416 iph->saddr = ((const struct sockaddr_in *) src)->sin_addr.s_addr;
417 iph->daddr = ((const struct sockaddr_in *) dst)->sin_addr.s_addr;
419 iph->protocol = (uint8_t) proto;
420 iph->check = 0;
422 data = packet + sizeof(*iph);
423 data_len = len - sizeof(*iph);
425 switch (proto) {
426 case IPPROTO_TCP:
427 __assemble_tcp(data, data_len, ctx, dst, src);
428 break;
430 case IPPROTO_ICMP:
431 __assemble_icmp4(data, data_len, ctx);
432 break;
434 default:
435 bug();
439 iph->check = csum((unsigned short *) packet, ntohs(iph->tot_len) >> 1);
441 return ntohs(iph->id);
444 static int assemble_ipv6(uint8_t *packet, size_t len, int ttl, int proto,
445 const struct ctx *ctx, const struct sockaddr *dst,
446 const struct sockaddr *src)
448 uint8_t *data;
449 size_t data_len;
450 struct ip6_hdr *ip6h = (struct ip6_hdr *) packet;
452 bug_on(!src || !dst);
453 bug_on(src->sa_family != PF_INET6 || dst->sa_family != PF_INET6);
454 bug_on(len < sizeof(*ip6h) + min(sizeof(struct tcphdr),
455 sizeof(struct icmp6hdr)));
457 ip6h->ip6_flow = htonl(rand() & 0x000fffff);
458 ip6h->ip6_vfc = 0x60;
460 ip6h->ip6_plen = htons((uint16_t) len - sizeof(*ip6h));
461 ip6h->ip6_nxt = (uint8_t) proto;
462 ip6h->ip6_hlim = (uint8_t) ttl;
464 memcpy(&ip6h->ip6_src, &(((const struct sockaddr_in6 *)
465 src)->sin6_addr), sizeof(ip6h->ip6_src));
466 memcpy(&ip6h->ip6_dst, &(((const struct sockaddr_in6 *)
467 dst)->sin6_addr), sizeof(ip6h->ip6_dst));
469 data = packet + sizeof(*ip6h);
470 data_len = len - sizeof(*ip6h);
472 switch (proto) {
473 case IPPROTO_TCP:
474 __assemble_tcp6(data, data_len, ctx, dst, src);
475 break;
477 case IPPROTO_ICMP:
478 case IPPROTO_ICMPV6:
479 __assemble_icmp6(data, data_len, ctx, dst, src);
480 break;
482 default:
483 bug();
486 return ntohl(ip6h->ip6_flow) & 0x000fffff;
489 static int check_ipv4(uint8_t *packet, size_t len, int ttl __maybe_unused,
490 int id, const struct sockaddr *ss)
492 struct iphdr *iph = (struct iphdr *) packet;
493 struct iphdr *iph_inner;
494 struct icmphdr *icmph;
496 if (iph->protocol != IPPROTO_ICMP)
497 return -EINVAL;
498 if (iph->daddr != ((const struct sockaddr_in *) ss)->sin_addr.s_addr)
499 return -EINVAL;
501 icmph = (struct icmphdr *) (packet + sizeof(struct iphdr));
502 if (icmph->type != ICMP_TIME_EXCEEDED)
503 return -EINVAL;
504 if (icmph->code != ICMP_EXC_TTL)
505 return -EINVAL;
507 iph_inner = (struct iphdr *) (packet + sizeof(struct iphdr) +
508 sizeof(struct icmphdr));
509 if (ntohs(iph_inner->id) != id)
510 return -EINVAL;
512 return len;
515 static void handle_ipv4(uint8_t *packet, size_t len __maybe_unused,
516 int dns_resolv, int latitude)
518 char hbuff[NI_MAXHOST];
519 struct iphdr *iph = (struct iphdr *) packet;
520 struct sockaddr_in sd;
521 struct hostent *hent;
522 const char *as, *country;
523 char *city;
525 memset(hbuff, 0, sizeof(hbuff));
526 memset(&sd, 0, sizeof(sd));
527 sd.sin_family = PF_INET;
528 sd.sin_addr.s_addr = iph->saddr;
530 getnameinfo((struct sockaddr *) &sd, sizeof(sd),
531 hbuff, sizeof(hbuff), NULL, 0, NI_NUMERICHOST);
533 as = geoip4_as_name(&sd);
534 country = geoip4_country_name(&sd);
535 city = geoip4_city_name(&sd);
537 if (dns_resolv) {
538 hent = gethostbyaddr(&sd.sin_addr, sizeof(sd.sin_addr), PF_INET);
539 if (hent)
540 printf(" %s (%s)", hent->h_name, hbuff);
541 else
542 printf(" %s", hbuff);
543 } else {
544 printf(" %s", hbuff);
546 if (as)
547 printf(" in %s", as);
548 if (country) {
549 printf(" in %s", country);
550 if (city)
551 printf(", %s", city);
553 if (latitude)
554 printf(" (%f/%f)", geoip4_latitude(&sd), geoip4_longitude(&sd));
556 free(city);
559 static int check_ipv6(uint8_t *packet, size_t len, int ttl __maybe_unused,
560 int id, const struct sockaddr *ss)
562 struct ip6_hdr *ip6h = (struct ip6_hdr *) packet;
563 struct ip6_hdr *ip6h_inner;
564 struct icmp6hdr *icmp6h;
566 if (ip6h->ip6_nxt != IPPROTO_ICMPV6)
567 return -EINVAL;
568 if (memcmp(&ip6h->ip6_dst, &(((const struct sockaddr_in6 *)
569 ss)->sin6_addr), sizeof(ip6h->ip6_dst)))
570 return -EINVAL;
572 icmp6h = (struct icmp6hdr *) (packet + sizeof(*ip6h));
573 if (icmp6h->icmp6_type != ICMPV6_TIME_EXCEED)
574 return -EINVAL;
575 if (icmp6h->icmp6_code != ICMPV6_EXC_HOPLIMIT)
576 return -EINVAL;
578 ip6h_inner = (struct ip6_hdr *) (packet + sizeof(*ip6h) + sizeof(*icmp6h));
579 if ((ntohl(ip6h_inner->ip6_flow) & 0x000fffff) != (uint32_t) id)
580 return -EINVAL;
582 return len;
585 static void handle_ipv6(uint8_t *packet, size_t len __maybe_unused,
586 int dns_resolv, int latitude)
588 char hbuff[NI_MAXHOST];
589 struct ip6_hdr *ip6h = (struct ip6_hdr *) packet;
590 struct sockaddr_in6 sd;
591 struct hostent *hent;
592 const char *as, *country;
593 char *city;
595 memset(hbuff, 0, sizeof(hbuff));
596 memset(&sd, 0, sizeof(sd));
597 sd.sin6_family = PF_INET6;
598 memcpy(&sd.sin6_addr, &ip6h->ip6_src, sizeof(ip6h->ip6_src));
600 getnameinfo((struct sockaddr *) &sd, sizeof(sd),
601 hbuff, sizeof(hbuff), NULL, 0, NI_NUMERICHOST);
603 as = geoip6_as_name(&sd);
604 country = geoip6_country_name(&sd);
605 city = geoip6_city_name(&sd);
607 if (dns_resolv) {
608 hent = gethostbyaddr(&sd.sin6_addr, sizeof(sd.sin6_addr), PF_INET6);
609 if (hent)
610 printf(" %s (%s)", hent->h_name, hbuff);
611 else
612 printf(" %s", hbuff);
613 } else {
614 printf(" %s", hbuff);
616 if (as)
617 printf(" in %s", as);
618 if (country) {
619 printf(" in %s", country);
620 if (city)
621 printf(", %s", city);
623 if (latitude)
624 printf(" (%f/%f)", geoip6_latitude(&sd), geoip6_longitude(&sd));
626 free(city);
629 static void show_trace_info(struct ctx *ctx, const struct sockaddr_storage *ss,
630 const struct sockaddr_storage *sd)
632 char hbuffs[256], hbuffd[256];
634 memset(hbuffd, 0, sizeof(hbuffd));
635 getnameinfo((struct sockaddr *) sd, sizeof(*sd),
636 hbuffd, sizeof(hbuffd), NULL, 0, NI_NUMERICHOST);
638 memset(hbuffs, 0, sizeof(hbuffs));
639 getnameinfo((struct sockaddr *) ss, sizeof(*ss),
640 hbuffs, sizeof(hbuffs), NULL, 0, NI_NUMERICHOST);
642 printf("AS path IPv%d TCP trace from %s to %s:%s (%s) with len %zu "
643 "Bytes, %u max hops\n", ctx->proto == IPPROTO_IP ? 4 : 6,
644 hbuffs, hbuffd, ctx->port, ctx->host, ctx->totlen, ctx->max_ttl);
646 printf("Using flags SYN:%d,ACK:%d,ECN:%d,FIN:%d,PSH:%d,RST:%d,URG:%d\n",
647 ctx->syn, ctx->ack, ctx->ecn, ctx->fin, ctx->psh, ctx->rst, ctx->urg);
649 if (ctx->payload)
650 printf("With payload: \'%s\'\n", ctx->payload);
653 static int get_remote_fd(struct ctx *ctx, struct sockaddr_storage *ss,
654 struct sockaddr_storage *sd)
656 int fd = -1, ret, one = 1, af = AF_INET;
657 struct addrinfo hints, *ahead, *ai;
658 unsigned char bind_ip[sizeof(struct in6_addr)];
660 memset(&hints, 0, sizeof(hints));
661 hints.ai_family = PF_UNSPEC;
662 hints.ai_socktype = SOCK_STREAM;
663 hints.ai_protocol = IPPROTO_TCP;
664 hints.ai_flags = AI_NUMERICSERV;
666 ret = getaddrinfo(ctx->host, ctx->port, &hints, &ahead);
667 if (ret < 0)
668 panic("Cannot get address info!\n");
670 for (ai = ahead; ai != NULL && fd < 0; ai = ai->ai_next) {
671 if (!((ai->ai_family == PF_INET6 && ctx->proto == IPPROTO_IPV6) ||
672 (ai->ai_family == PF_INET && ctx->proto == IPPROTO_IP)))
673 continue;
675 fd = socket(ai->ai_family, SOCK_RAW, IPPROTO_RAW);
676 if (fd < 0)
677 continue;
679 memset(ss, 0, sizeof(*ss));
680 ret = device_address(ctx->dev, ai->ai_family, ss);
681 if (ret < 0 && !ctx->bind_addr)
682 panic("Cannot get own device address!\n");
684 if (ctx->bind_addr) {
685 if (ctx->proto == IPPROTO_IPV6)
686 af = AF_INET6;
688 if (inet_pton(af, ctx->bind_addr, &bind_ip) != 1)
689 panic("Address is invalid!\n");
691 if (af == AF_INET6) {
692 struct sockaddr_in6 *ss6 = (struct sockaddr_in6 *) ss;
693 memcpy(&ss6->sin6_addr.s6_addr, &bind_ip, sizeof(struct in6_addr));
694 } else {
695 struct sockaddr_in *ss4 = (struct sockaddr_in *) ss;
696 memcpy(&ss4->sin_addr.s_addr, &bind_ip, sizeof(struct in_addr));
700 ret = bind(fd, (struct sockaddr *) ss, sizeof(*ss));
701 if (ret < 0)
702 panic("Cannot bind socket!\n");
704 memset(sd, 0, sizeof(*sd));
705 memcpy(sd, ai->ai_addr, ai->ai_addrlen);
707 ctx->sd_len = ai->ai_addrlen;
708 ctx->dport = strtoul(ctx->port, NULL, 10);
710 ret = setsockopt(fd, ctx->proto, IP_HDRINCL, &one, sizeof(one));
711 if (ret < 0)
712 panic("Kernel does not support IP_HDRINCL!\n");
714 if (ai->ai_family == PF_INET6) {
715 struct sockaddr_in6 *sd6 = (struct sockaddr_in6 *) sd;
717 sd6->sin6_port = 0;
720 break;
723 freeaddrinfo(ahead);
725 if (fd < 0)
726 panic("Cannot create socket! Does remote "
727 "support IPv%d?!\n",
728 ctx->proto == IPPROTO_IP ? 4 : 6);
730 return fd;
733 static void inject_filter(struct ctx *ctx, int fd)
735 struct sock_fprog bpf_ops;
737 enable_kernel_bpf_jit_compiler();
739 memset(&bpf_ops, 0, sizeof(bpf_ops));
740 bpf_ops.filter = (struct sock_filter *) af_ops[ctx->proto].filter;
741 bpf_ops.len = af_ops[ctx->proto].flen;
743 bpf_attach_to_sock(fd, &bpf_ops);
746 static int __process_node(struct ctx *ctx, int fd, int fd_cap, int ttl,
747 int inner_proto, uint8_t *pkt_snd, uint8_t *pkt_rcv,
748 const struct sockaddr_storage *ss,
749 const struct sockaddr_storage *sd, struct timeval *diff)
751 int pkt_id, ret, timeout;
752 struct pollfd pfd;
753 struct timeval start, end;
755 prepare_polling(fd_cap, &pfd);
757 memset(pkt_snd, 0, ctx->totlen);
758 pkt_id = af_ops[ctx->proto].assembler(pkt_snd, ctx->totlen, ttl,
759 inner_proto, ctx,
760 (const struct sockaddr *) sd,
761 (const struct sockaddr *) ss);
763 ret = sendto(fd, pkt_snd, ctx->totlen, 0, (struct sockaddr *) sd,
764 ctx->sd_len);
765 if (ret < 0)
766 panic("sendto failed: %s\n", strerror(errno));
768 bug_on(gettimeofday(&start, NULL));
770 timeout = (ctx->timeout > 0 ? ctx->timeout : 2) * 1000;
772 ret = poll(&pfd, 1, timeout);
773 if (ret > 0 && pfd.revents & POLLIN && sigint == 0) {
774 bug_on(gettimeofday(&end, NULL));
775 if (diff)
776 timersub(&end, &start, diff);
778 ret = recvfrom(fd_cap, pkt_rcv, ctx->rcvlen, 0, NULL, NULL);
779 if (ret < (int) (sizeof(struct ethhdr) + af_ops[ctx->proto].min_len_icmp))
780 return -EIO;
782 return af_ops[ctx->proto].check(pkt_rcv + sizeof(struct ethhdr),
783 ret - sizeof(struct ethhdr), ttl,
784 pkt_id, (const struct sockaddr *) ss);
785 } else {
786 return -EIO;
789 return 0;
792 static void timerdiv(const unsigned long divisor, const struct timeval *tv,
793 struct timeval *result)
795 uint64_t x = ((uint64_t) tv->tv_sec * 1000 * 1000 + tv->tv_usec) / divisor;
797 result->tv_sec = x / 1000 / 1000;
798 result->tv_usec = x % (1000 * 1000);
801 static int timevalcmp(const void *t1, const void *t2)
803 if (timercmp((struct timeval *) t1, (struct timeval *) t2, <))
804 return -1;
805 if (timercmp((struct timeval *) t1, (struct timeval *) t2, >))
806 return 1;
808 return 0;
811 static const char *proto_short(int proto)
813 switch (proto) {
814 case IPPROTO_TCP:
815 return "t";
816 case IPPROTO_ICMP:
817 case IPPROTO_ICMPV6:
818 return "i";
819 default:
820 return "?";
824 static int __process_time(struct ctx *ctx, int fd, int fd_cap, int ttl,
825 int inner_proto, uint8_t *pkt_snd, uint8_t *pkt_rcv,
826 const struct sockaddr_storage *ss,
827 const struct sockaddr_storage *sd)
829 size_t i, j = 0;
830 int good = 0, ret = -EIO, idx, ret_good = -EIO;
831 struct timeval probes[9], *tmp, sum, res;
832 uint8_t *trash = xmalloc(ctx->rcvlen);
833 char *cwait[] = { "-", "\\", "|", "/" };
835 memset(probes, 0, sizeof(probes));
836 for (i = 0; i < array_size(probes) && sigint == 0; ++i) {
837 ret = __process_node(ctx, fd, fd_cap, ttl, inner_proto,
838 pkt_snd, good == 0 ? pkt_rcv : trash,
839 ss, sd, &probes[i]);
840 if (ret > 0) {
841 if (good == 0)
842 ret_good = ret;
843 good++;
846 if (good == 0 && ctx->queries == (int) i)
847 break;
849 usleep(50000);
851 printf("\r%2d: %s", ttl, cwait[j++]);
852 fflush(stdout);
853 if (j >= array_size(cwait))
854 j = 0;
857 if (good == 0) {
858 xfree(trash);
859 return -EIO;
862 tmp = xcalloc(good, sizeof(struct timeval));
863 for (i = j = 0; i < array_size(probes); ++i) {
864 if (probes[i].tv_sec == 0 && probes[i].tv_usec == 0)
865 continue;
866 tmp[j].tv_sec = probes[i].tv_sec;
867 tmp[j].tv_usec = probes[i].tv_usec;
868 j++;
871 qsort(tmp, j, sizeof(struct timeval), timevalcmp);
873 printf("\r%2d: %s[", ttl, proto_short(inner_proto));
874 idx = j / 2;
875 switch (j % 2) {
876 case 0:
877 timeradd(&tmp[idx], &tmp[idx - 1], &sum);
878 timerdiv(2, &sum, &res);
879 if (res.tv_sec > 0)
880 printf("%lu sec ", res.tv_sec);
881 printf("%7lu us", res.tv_usec);
882 break;
883 case 1:
884 if (tmp[idx].tv_sec > 0)
885 printf("%lu sec ", tmp[idx].tv_sec);
886 printf("%7lu us", tmp[idx].tv_usec);
887 break;
889 printf("]");
891 xfree(tmp);
892 xfree(trash);
894 return ret_good;
897 static int __probe_remote(struct ctx *ctx, int fd, int fd_cap, int ttl,
898 uint8_t *pkt_snd, uint8_t *pkt_rcv,
899 const struct sockaddr_storage *ss,
900 const struct sockaddr_storage *sd,
901 int inner_proto)
903 int ret = -EIO, tries = ctx->queries;
905 while (tries-- > 0 && sigint == 0) {
906 ret = __process_time(ctx, fd, fd_cap, ttl, inner_proto,
907 pkt_snd, pkt_rcv, ss, sd);
908 if (ret < 0)
909 continue;
911 af_ops[ctx->proto].handler(pkt_rcv + sizeof(struct ethhdr),
912 ret - sizeof(struct ethhdr),
913 ctx->dns_resolv, ctx->latitude);
914 if (ctx->show) {
915 struct pkt_buff *pkt;
917 printf("\n");
918 pkt = pkt_alloc(pkt_rcv, ret);
919 hex_ascii(pkt);
920 tprintf_flush();
921 pkt_free(pkt);
924 break;
927 return ret;
930 static int __process_ttl(struct ctx *ctx, int fd, int fd_cap, int ttl,
931 uint8_t *pkt_snd, uint8_t *pkt_rcv,
932 const struct sockaddr_storage *ss,
933 const struct sockaddr_storage *sd)
935 int ret = -EIO;
936 size_t i;
937 const int inner_protos[] = {
938 IPPROTO_TCP,
939 IPPROTO_ICMP,
942 printf("%2d: ", ttl);
943 fflush(stdout);
945 for (i = 0; i < array_size(inner_protos) && sigint == 0; ++i) {
946 ret = __probe_remote(ctx, fd, fd_cap, ttl, pkt_snd, pkt_rcv, ss, sd,
947 inner_protos[i]);
948 if (ret > 0)
949 break;
952 if (ret <= 0)
953 printf("\r%2d: ?[ no answer]", ttl);
954 if (ctx->show == 0)
955 printf("\n");
956 if (ctx->show && ret <= 0)
957 printf("\n\n");
959 fflush(stdout);
960 return 0;
963 static int main_trace(struct ctx *ctx)
965 int fd, fd_cap, ifindex, ttl;
966 struct ring dummy_ring;
967 struct sockaddr_storage ss, sd;
968 uint8_t *pkt_snd, *pkt_rcv;
970 fd = get_remote_fd(ctx, &ss, &sd);
971 fd_cap = pf_socket();
973 inject_filter(ctx, fd_cap);
975 ifindex = device_ifindex(ctx->dev);
976 bind_ring_generic(fd_cap, &dummy_ring, ifindex, false);
978 if (ctx->totlen < af_ops[ctx->proto].min_len_tcp) {
979 ctx->totlen = af_ops[ctx->proto].min_len_tcp;
980 if (ctx->payload)
981 ctx->totlen += strlen(ctx->payload);
984 ctx->rcvlen = device_mtu(ctx->dev) - sizeof(struct ethhdr);
985 if (ctx->totlen >= ctx->rcvlen)
986 panic("Packet len exceeds device MTU!\n");
988 pkt_snd = xmalloc(ctx->totlen);
989 pkt_rcv = xmalloc(ctx->rcvlen);
991 show_trace_info(ctx, &ss, &sd);
993 for (ttl = ctx->init_ttl; ttl <= ctx->max_ttl && sigint == 0; ++ttl)
994 __process_ttl(ctx, fd, fd_cap, ttl, pkt_snd, pkt_rcv,
995 &ss, &sd);
997 xfree(pkt_snd);
998 xfree(pkt_rcv);
1000 close(fd_cap);
1001 close(fd);
1003 return 0;
1006 int main(int argc, char **argv)
1008 int c, ret;
1009 struct ctx ctx;
1011 setfsuid(getuid());
1012 setfsgid(getgid());
1014 srand(time(NULL));
1016 memset(&ctx, 0, sizeof(ctx));
1017 ctx.init_ttl = 1;
1018 ctx.max_ttl = 30;
1019 ctx.queries = 2;
1020 ctx.timeout = 2;
1021 ctx.proto = IPPROTO_IP;
1022 ctx.payload = NULL;
1023 ctx.dev = xstrdup("eth0");
1024 ctx.port = xstrdup("80");
1025 ctx.bind_addr = NULL;
1027 while ((c = getopt_long(argc, argv, short_options, long_options,
1028 NULL)) != EOF) {
1029 switch (c) {
1030 case 'h':
1031 help();
1032 break;
1033 case 'v':
1034 version();
1035 break;
1036 case 'u':
1037 update_geoip();
1038 die();
1039 break;
1040 case 'H':
1041 ctx.host = xstrdup(optarg);
1042 break;
1043 case 'p':
1044 if (ctx.port)
1045 xfree(ctx.port);
1046 ctx.port = xstrdup(optarg);
1047 break;
1048 case 'n':
1049 ctx.dns_resolv = 0;
1050 break;
1051 case '4':
1052 ctx.proto = IPPROTO_IP;
1053 break;
1054 case '6':
1055 ctx.proto = IPPROTO_IPV6;
1056 break;
1057 case 'Z':
1058 ctx.show = 1;
1059 break;
1060 case 'N':
1061 ctx.dns_resolv = 1;
1062 break;
1063 case 'f':
1064 ctx.init_ttl = atoi(optarg);
1065 if (ctx.init_ttl <= 0)
1066 help();
1067 break;
1068 case 'm':
1069 ctx.max_ttl = atoi(optarg);
1070 if (ctx.max_ttl <= 0)
1071 help();
1072 break;
1073 case 'b':
1074 ctx.bind_addr = xstrdup(optarg);
1075 break;
1076 case 'i':
1077 case 'd':
1078 free(ctx.dev);
1079 ctx.dev = xstrdup(optarg);
1080 break;
1081 case 'q':
1082 ctx.queries = atoi(optarg);
1083 if (ctx.queries <= 0)
1084 help();
1085 break;
1086 case 'x':
1087 ctx.timeout = atoi(optarg);
1088 if (ctx.timeout <= 0)
1089 help();
1090 break;
1091 case 'L':
1092 ctx.latitude = 1;
1093 break;
1094 case 'S':
1095 ctx.syn = 1;
1096 break;
1097 case 'A':
1098 ctx.ack = 1;
1099 break;
1100 case 'F':
1101 ctx.fin = 1;
1102 break;
1103 case 'U':
1104 ctx.urg = 1;
1105 break;
1106 case 'P':
1107 ctx.psh = 1;
1108 break;
1109 case 'R':
1110 ctx.rst = 1;
1111 break;
1112 case 'E':
1113 ctx.syn = 1;
1114 ctx.ecn = 1;
1115 break;
1116 case 't':
1117 ctx.tos = atoi(optarg);
1118 if (ctx.tos < 0)
1119 help();
1120 break;
1121 case 'G':
1122 ctx.nofrag = 1;
1123 break;
1124 case 'X':
1125 ctx.payload = xstrdup(optarg);
1126 break;
1127 case 'l':
1128 ctx.totlen = strtoul(optarg, NULL, 10);
1129 if (ctx.totlen == 0)
1130 help();
1131 break;
1132 case '?':
1133 switch (optopt) {
1134 case 'H':
1135 case 'p':
1136 case 'f':
1137 case 'm':
1138 case 'i':
1139 case 'd':
1140 case 'q':
1141 case 'x':
1142 case 'X':
1143 case 't':
1144 case 'l':
1145 panic("Option -%c requires an argument!\n",
1146 optopt);
1147 default:
1148 if (isprint(optopt))
1149 printf("Unknown option character `0x%X\'!\n", optopt);
1150 die();
1152 default:
1153 break;
1157 if (argc < 3 || !ctx.host || !ctx.port || ctx.init_ttl > ctx.max_ttl ||
1158 ctx.init_ttl > MAXTTL || ctx.max_ttl > MAXTTL)
1159 help();
1161 if (!device_up_and_running(ctx.dev))
1162 panic("Networking device not up and running!\n");
1163 if (device_mtu(ctx.dev) <= ctx.totlen)
1164 panic("Packet larger than device MTU!\n");
1166 register_signal(SIGHUP, signal_handler);
1167 register_signal(SIGINT, signal_handler);
1168 register_signal(SIGQUIT, signal_handler);
1169 register_signal(SIGTERM, signal_handler);
1171 tprintf_init();
1172 init_geoip(1);
1174 ret = main_trace(&ctx);
1176 destroy_geoip();
1177 tprintf_cleanup();
1179 free(ctx.dev);
1180 free(ctx.host);
1181 free(ctx.port);
1182 free(ctx.bind_addr);
1183 free(ctx.payload);
1185 return ret;