promisc: Constify `ifname' parameters to {enter,leave}_promiscuous_mode()
[netsniff-ng.git] / astraceroute.c
blobb546943007c6ee20f947ce89ad3d11fd8c84106b
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 "ioops.h"
44 #include "csum.h"
45 #include "sock.h"
46 #include "geoip.h"
47 #include "ring_rx.h"
48 #include "built_in.h"
50 struct ctx {
51 char *host, *port, *dev, *payload;
52 size_t totlen, rcvlen;
53 int init_ttl, max_ttl, dns_resolv, queries, timeout;
54 int syn, ack, ecn, fin, psh, rst, urg, tos, nofrag, proto, show;
55 int sd_len, dport, latitude;
58 struct proto_ops {
59 int (*assembler)(uint8_t *packet, size_t len, int ttl, int proto,
60 const struct ctx *ctx, const struct sockaddr *dst,
61 const struct sockaddr *src);
62 const struct sock_filter *filter;
63 unsigned int flen;
64 size_t min_len_tcp, min_len_icmp;
65 int (*check)(uint8_t *packet, size_t len, int ttl, int id,
66 const struct sockaddr *src);
67 void (*handler)(uint8_t *packet, size_t len, int dns_resolv,
68 int latitude);
71 static sig_atomic_t sigint = 0;
73 static int assemble_ipv4(uint8_t *packet, size_t len, int ttl, int proto,
74 const struct ctx *ctx, const struct sockaddr *dst,
75 const struct sockaddr *src);
76 static int assemble_ipv6(uint8_t *packet, size_t len, int ttl, int proto,
77 const struct ctx *ctx, const struct sockaddr *dst,
78 const struct sockaddr *src);
79 static int check_ipv4(uint8_t *packet, size_t len, int ttl, int id,
80 const struct sockaddr *ss);
81 static void handle_ipv4(uint8_t *packet, size_t len, int dns_resolv,
82 int latitude);
83 static int check_ipv6(uint8_t *packet, size_t len, int ttl, int id,
84 const struct sockaddr *ss);
85 static void handle_ipv6(uint8_t *packet, size_t len, int dns_resolv,
86 int latitude);
88 static const char *short_options = "H:p:nNf:m:i:d:q:x:SAEFPURt:Gl:hv46X:ZuL";
89 static const struct option long_options[] = {
90 {"host", required_argument, NULL, 'H'},
91 {"port", required_argument, NULL, 'p'},
92 {"init-ttl", required_argument, NULL, 'f'},
93 {"max-ttl", required_argument, NULL, 'm'},
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 sigint = 1;
171 default:
172 break;
176 static void __noreturn help(void)
178 printf("\nastraceroute %s, autonomous system trace route utility\n", VERSION_STRING);
179 puts("http://www.netsniff-ng.org\n\n"
180 "Usage: astraceroute [options]\n"
181 "Options:\n"
182 " -H|--host <host> Host/IPv4/IPv6 to lookup AS route to\n"
183 " -p|--port <port> Hosts port to lookup AS route to\n"
184 " -i|-d|--dev <device> Networking device, e.g. eth0\n"
185 " -f|--init-ttl <ttl> Set initial TTL\n"
186 " -m|--max-ttl <ttl> Set maximum TTL (def: 30)\n"
187 " -q|--num-probes <num> Number of max probes for each hop (def: 2)\n"
188 " -x|--timeout <sec> Probe response timeout in sec (def: 3)\n"
189 " -X|--payload <string> Specify a payload string to test DPIs\n"
190 " -l|--totlen <len> Specify total packet len\n"
191 " -4|--ipv4 Use IPv4-only requests\n"
192 " -6|--ipv6 Use IPv6-only requests\n"
193 " -n|--numeric Do not do reverse DNS lookup for hops\n"
194 " -u|--update Update GeoIP databases\n"
195 " -L|--latitude Show latitude and longtitude\n"
196 " -N|--dns Do a reverse DNS lookup for hops\n"
197 " -S|--syn Set TCP SYN flag\n"
198 " -A|--ack Set TCP ACK flag\n"
199 " -F|--fin Set TCP FIN flag\n"
200 " -P|--psh Set TCP PSH flag\n"
201 " -U|--urg Set TCP URG flag\n"
202 " -R|--rst Set TCP RST flag\n"
203 " -E|--ecn-syn Send ECN SYN packets (RFC3168)\n"
204 " -t|--tos <tos> Set the IP TOS field\n"
205 " -G|--nofrag Set do not fragment bit\n"
206 " -Z|--show-packet Show returned packet on each hop\n"
207 " -v|--version Print version and exit\n"
208 " -h|--help Print this help and exit\n\n"
209 "Examples:\n"
210 " IPv4 trace of AS with TCP SYN probe (this will most-likely pass):\n"
211 " astraceroute -i eth0 -N -S -H netsniff-ng.org\n"
212 " IPv4 trace of AS with TCP ECN SYN probe:\n"
213 " astraceroute -i eth0 -N -E -H netsniff-ng.org\n"
214 " IPv4 trace of AS with TCP FIN probe:\n"
215 " astraceroute -i eth0 -N -F -H netsniff-ng.org\n"
216 " IPv4 trace of AS with Xmas probe:\n"
217 " astraceroute -i eth0 -N -FPU -H netsniff-ng.org\n"
218 " IPv4 trace of AS with Null probe with ASCII payload:\n"
219 " astraceroute -i eth0 -N -H netsniff-ng.org -X \"censor-me\" -Z\n"
220 " IPv6 trace of AS up to www.6bone.net:\n"
221 " astraceroute -6 -i eth0 -S -E -N -H www.6bone.net\n\n"
222 "Note:\n"
223 " If the TCP probe did not give any results, then astraceroute will\n"
224 " automatically probe for classic ICMP packets! To gather more\n"
225 " information about astraceroute's fetched AS numbers, see e.g.\n"
226 " http://bgp.he.net/AS<number>!\n\n"
227 "Please report bugs to <bugs@netsniff-ng.org>\n"
228 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
229 "Swiss federal institute of technology (ETH Zurich)\n"
230 "License: GNU GPL version 2.0\n"
231 "This is free software: you are free to change and redistribute it.\n"
232 "There is NO WARRANTY, to the extent permitted by law.\n");
233 die();
236 static void __noreturn version(void)
238 printf("\nastraceroute %s, Git id: %s\n", VERSION_LONG, GITVERSION);
239 puts("autonomous system trace route utility\n"
240 "http://www.netsniff-ng.org\n\n"
241 "Please report bugs to <bugs@netsniff-ng.org>\n"
242 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
243 "Swiss federal institute of technology (ETH Zurich)\n"
244 "License: GNU GPL version 2.0\n"
245 "This is free software: you are free to change and redistribute it.\n"
246 "There is NO WARRANTY, to the extent permitted by law.\n");
247 die();
250 static void __assemble_data(uint8_t *packet, size_t len, const char *payload)
252 size_t i;
254 if (payload == NULL) {
255 for (i = 0; i < len; ++i)
256 packet[i] = (uint8_t) rand();
257 } else {
258 size_t lmin = min(len, strlen(payload));
260 for (i = 0; i < lmin; ++i)
261 packet[i] = (uint8_t) payload[i];
262 for (i = lmin; i < len; ++i)
263 packet[i] = (uint8_t) rand();
267 static void __assemble_icmp4(uint8_t *packet, size_t len)
269 struct icmphdr *icmph = (struct icmphdr *) packet;
271 bug_on(len < sizeof(struct icmphdr));
273 icmph->type = ICMP_ECHO;
274 icmph->code = 0;
275 icmph->checksum = 0;
278 static void __assemble_icmp6(uint8_t *packet, size_t len)
280 struct icmp6hdr *icmp6h = (struct icmp6hdr *) packet;
282 bug_on(len < sizeof(struct icmp6hdr));
284 icmp6h->icmp6_type = ICMPV6_ECHO_REQUEST;
285 icmp6h->icmp6_code = 0;
286 icmp6h->icmp6_cksum = 0;
289 static void __assemble_tcp(uint8_t *packet, size_t len, int syn, int ack,
290 int urg, int fin, int rst, int psh, int ecn,
291 int dport)
293 struct tcphdr *tcph = (struct tcphdr *) packet;
295 bug_on(len < sizeof(struct tcphdr));
297 tcph->source = htons((uint16_t) rand());
298 tcph->dest = htons((uint16_t) dport);
300 tcph->seq = htonl(rand());
301 tcph->ack_seq = (!!ack ? htonl(rand()) : 0);
303 tcph->doff = 5;
305 tcph->syn = !!syn;
306 tcph->ack = !!ack;
307 tcph->urg = !!urg;
308 tcph->fin = !!fin;
309 tcph->rst = !!rst;
310 tcph->psh = !!psh;
311 tcph->ece = !!ecn;
312 tcph->cwr = !!ecn;
314 tcph->window = htons((uint16_t) (100 + (rand() % 65435)));
315 tcph->urg_ptr = (!!urg ? htons((uint16_t) rand()) : 0);
316 tcph->check = 0;
319 static int assemble_ipv4(uint8_t *packet, size_t len, int ttl, int proto,
320 const struct ctx *ctx, const struct sockaddr *dst,
321 const struct sockaddr *src)
323 uint8_t *data;
324 size_t data_len, off_next = 0;
325 struct iphdr *iph = (struct iphdr *) packet;
327 bug_on(!src || !dst);
328 bug_on(src->sa_family != PF_INET || dst->sa_family != PF_INET);
329 bug_on(len < sizeof(*iph) + min(sizeof(struct tcphdr),
330 sizeof(struct icmphdr)));
332 iph->ihl = 5;
333 iph->version = 4;
334 iph->tos = (uint8_t) ctx->tos;
336 iph->tot_len = htons((uint16_t) len);
337 iph->id = htons((uint16_t) rand());
339 iph->frag_off = ctx->nofrag ? IP_DF : 0;
340 iph->ttl = (uint8_t) ttl;
342 iph->saddr = ((const struct sockaddr_in *) src)->sin_addr.s_addr;
343 iph->daddr = ((const struct sockaddr_in *) dst)->sin_addr.s_addr;
345 iph->protocol = (uint8_t) proto;
347 data = packet + sizeof(*iph);
348 data_len = len - sizeof(*iph);
350 switch (proto) {
351 case IPPROTO_TCP:
352 __assemble_tcp(data, data_len, ctx->syn, ctx->ack, ctx->urg,
353 ctx->fin, ctx->rst, ctx->psh, ctx->ecn, ctx->dport);
354 off_next = sizeof(struct tcphdr);
355 break;
356 case IPPROTO_ICMP:
357 __assemble_icmp4(data, data_len);
358 off_next = sizeof(struct icmphdr);
359 break;
360 default:
361 bug();
364 data = packet + sizeof(*iph) + off_next;
365 data_len = len - sizeof(*iph) - off_next;
367 __assemble_data(data, data_len, ctx->payload);
369 iph->check = csum((unsigned short *) packet, ntohs(iph->tot_len) >> 1);
371 return ntohs(iph->id);
374 static int assemble_ipv6(uint8_t *packet, size_t len, int ttl, int proto,
375 const struct ctx *ctx, const struct sockaddr *dst,
376 const struct sockaddr *src)
378 uint8_t *data;
379 size_t data_len, off_next = 0;
380 struct ip6_hdr *ip6h = (struct ip6_hdr *) packet;
382 bug_on(!src || !dst);
383 bug_on(src->sa_family != PF_INET6 || dst->sa_family != PF_INET6);
384 bug_on(len < sizeof(*ip6h) + min(sizeof(struct tcphdr),
385 sizeof(struct icmp6hdr)));
387 ip6h->ip6_flow = htonl(rand() & 0x000fffff);
388 ip6h->ip6_vfc = 0x60;
390 ip6h->ip6_plen = htons((uint16_t) len - sizeof(*ip6h));
391 ip6h->ip6_nxt = (uint8_t) proto;
392 ip6h->ip6_hlim = (uint8_t) ttl;
394 memcpy(&ip6h->ip6_src, &(((const struct sockaddr_in6 *)
395 src)->sin6_addr), sizeof(ip6h->ip6_src));
396 memcpy(&ip6h->ip6_dst, &(((const struct sockaddr_in6 *)
397 dst)->sin6_addr), sizeof(ip6h->ip6_dst));
399 data = packet + sizeof(*ip6h);
400 data_len = len - sizeof(*ip6h);
402 switch (proto) {
403 case IPPROTO_TCP:
404 __assemble_tcp(data, data_len, ctx->syn, ctx->ack, ctx->urg,
405 ctx->fin, ctx->rst, ctx->psh, ctx->ecn, ctx->dport);
406 off_next = sizeof(struct tcphdr);
407 break;
408 case IPPROTO_ICMP:
409 case IPPROTO_ICMPV6:
410 __assemble_icmp6(data, data_len);
411 off_next = sizeof(struct icmp6hdr);
412 break;
413 default:
414 bug();
417 data = packet + sizeof(*ip6h) + off_next;
418 data_len = len - sizeof(*ip6h) - off_next;
420 __assemble_data(data, data_len, ctx->payload);
422 return ntohl(ip6h->ip6_flow) & 0x000fffff;
425 static int check_ipv4(uint8_t *packet, size_t len, int ttl __maybe_unused,
426 int id, const struct sockaddr *ss)
428 struct iphdr *iph = (struct iphdr *) packet;
429 struct iphdr *iph_inner;
430 struct icmphdr *icmph;
432 if (iph->protocol != IPPROTO_ICMP)
433 return -EINVAL;
434 if (iph->daddr != ((const struct sockaddr_in *) ss)->sin_addr.s_addr)
435 return -EINVAL;
437 icmph = (struct icmphdr *) (packet + sizeof(struct iphdr));
438 if (icmph->type != ICMP_TIME_EXCEEDED)
439 return -EINVAL;
440 if (icmph->code != ICMP_EXC_TTL)
441 return -EINVAL;
443 iph_inner = (struct iphdr *) (packet + sizeof(struct iphdr) +
444 sizeof(struct icmphdr));
445 if (ntohs(iph_inner->id) != id)
446 return -EINVAL;
448 return len;
451 static void handle_ipv4(uint8_t *packet, size_t len __maybe_unused,
452 int dns_resolv, int latitude)
454 char hbuff[NI_MAXHOST];
455 struct iphdr *iph = (struct iphdr *) packet;
456 struct sockaddr_in sd;
457 struct hostent *hent;
458 const char *as, *country, *city;
460 memset(hbuff, 0, sizeof(hbuff));
461 memset(&sd, 0, sizeof(sd));
462 sd.sin_family = PF_INET;
463 sd.sin_addr.s_addr = iph->saddr;
465 getnameinfo((struct sockaddr *) &sd, sizeof(sd),
466 hbuff, sizeof(hbuff), NULL, 0, NI_NUMERICHOST);
468 as = geoip4_as_name(sd);
469 country = geoip4_country_name(sd);
470 city = geoip4_city_name(sd);
472 if (dns_resolv) {
473 hent = gethostbyaddr(&sd.sin_addr, sizeof(sd.sin_addr), PF_INET);
474 if (hent)
475 printf(" %s (%s)", hent->h_name, hbuff);
476 else
477 printf(" %s", hbuff);
478 } else {
479 printf(" %s", hbuff);
481 if (as)
482 printf(" in %s", as);
483 if (country) {
484 printf(" in %s", country);
485 if (city)
486 printf(", %s", city);
488 if (latitude)
489 printf(" (%f/%f)", geoip4_latitude(sd), geoip4_longitude(sd));
492 static int check_ipv6(uint8_t *packet, size_t len, int ttl __maybe_unused,
493 int id, const struct sockaddr *ss)
495 struct ip6_hdr *ip6h = (struct ip6_hdr *) packet;
496 struct ip6_hdr *ip6h_inner;
497 struct icmp6hdr *icmp6h;
499 if (ip6h->ip6_nxt != 0x3a)
500 return -EINVAL;
501 if (memcmp(&ip6h->ip6_dst, &(((const struct sockaddr_in6 *)
502 ss)->sin6_addr), sizeof(ip6h->ip6_dst)))
503 return -EINVAL;
505 icmp6h = (struct icmp6hdr *) (packet + sizeof(*ip6h));
506 if (icmp6h->icmp6_type != ICMPV6_TIME_EXCEED)
507 return -EINVAL;
508 if (icmp6h->icmp6_code != ICMPV6_EXC_HOPLIMIT)
509 return -EINVAL;
511 ip6h_inner = (struct ip6_hdr *) (packet + sizeof(*ip6h) + sizeof(*icmp6h));
512 if ((ntohl(ip6h_inner->ip6_flow) & 0x000fffff) != (uint32_t) id)
513 return -EINVAL;
515 return len;
518 static void handle_ipv6(uint8_t *packet, size_t len __maybe_unused,
519 int dns_resolv, int latitude)
521 char hbuff[NI_MAXHOST];
522 struct ip6_hdr *ip6h = (struct ip6_hdr *) packet;
523 struct sockaddr_in6 sd;
524 struct hostent *hent;
525 const char *as, *country, *city;
527 memset(hbuff, 0, sizeof(hbuff));
528 memset(&sd, 0, sizeof(sd));
529 sd.sin6_family = PF_INET6;
530 memcpy(&sd.sin6_addr, &ip6h->ip6_src, sizeof(ip6h->ip6_src));
532 getnameinfo((struct sockaddr *) &sd, sizeof(sd),
533 hbuff, sizeof(hbuff), NULL, 0, NI_NUMERICHOST);
535 as = geoip6_as_name(sd);
536 country = geoip6_country_name(sd);
537 city = geoip6_city_name(sd);
539 if (dns_resolv) {
540 hent = gethostbyaddr(&sd.sin6_addr, sizeof(sd.sin6_addr), PF_INET6);
541 if (hent)
542 printf(" %s (%s)", hent->h_name, hbuff);
543 else
544 printf(" %s", hbuff);
545 } else {
546 printf(" %s", hbuff);
548 if (as)
549 printf(" in %s", as);
550 if (country) {
551 printf(" in %s", country);
552 if (city)
553 printf(", %s", city);
555 if (latitude)
556 printf(" (%f/%f)", geoip6_latitude(sd), geoip6_longitude(sd));
559 static void show_trace_info(struct ctx *ctx, const struct sockaddr_storage *ss,
560 const struct sockaddr_storage *sd)
562 char hbuffs[256], hbuffd[256];
564 memset(hbuffd, 0, sizeof(hbuffd));
565 getnameinfo((struct sockaddr *) sd, sizeof(*sd),
566 hbuffd, sizeof(hbuffd), NULL, 0, NI_NUMERICHOST);
568 memset(hbuffs, 0, sizeof(hbuffs));
569 getnameinfo((struct sockaddr *) ss, sizeof(*ss),
570 hbuffs, sizeof(hbuffs), NULL, 0, NI_NUMERICHOST);
572 printf("AS path IPv%d TCP trace from %s to %s:%s (%s) with len %zu "
573 "Bytes, %u max hops\n", ctx->proto == IPPROTO_IP ? 4 : 6,
574 hbuffs, hbuffd, ctx->port, ctx->host, ctx->totlen, ctx->max_ttl);
576 printf("Using flags SYN:%d,ACK:%d,ECN:%d,FIN:%d,PSH:%d,RST:%d,URG:%d\n",
577 ctx->syn, ctx->ack, ctx->ecn, ctx->fin, ctx->psh, ctx->rst, ctx->urg);
579 if (ctx->payload)
580 printf("With payload: \'%s\'\n", ctx->payload);
583 static int get_remote_fd(struct ctx *ctx, struct sockaddr_storage *ss,
584 struct sockaddr_storage *sd)
586 int fd = -1, ret, one = 1;
587 struct addrinfo hints, *ahead, *ai;
589 memset(&hints, 0, sizeof(hints));
590 hints.ai_family = PF_UNSPEC;
591 hints.ai_socktype = SOCK_STREAM;
592 hints.ai_protocol = IPPROTO_TCP;
593 hints.ai_flags = AI_NUMERICSERV;
595 ret = getaddrinfo(ctx->host, ctx->port, &hints, &ahead);
596 if (ret < 0)
597 panic("Cannot get address info!\n");
599 for (ai = ahead; ai != NULL && fd < 0; ai = ai->ai_next) {
600 if (!((ai->ai_family == PF_INET6 && ctx->proto == IPPROTO_IPV6) ||
601 (ai->ai_family == PF_INET && ctx->proto == IPPROTO_IP)))
602 continue;
604 fd = socket(ai->ai_family, SOCK_RAW, IPPROTO_RAW);
605 if (fd < 0)
606 continue;
608 memset(ss, 0, sizeof(*ss));
609 ret = device_address(ctx->dev, ai->ai_family, ss);
610 if (ret < 0)
611 panic("Cannot get own device address!\n");
613 ret = bind(fd, (struct sockaddr *) ss, sizeof(*ss));
614 if (ret < 0)
615 panic("Cannot bind socket!\n");
617 memset(sd, 0, sizeof(*sd));
618 memcpy(sd, ai->ai_addr, ai->ai_addrlen);
620 ctx->sd_len = ai->ai_addrlen;
621 ctx->dport = strtoul(ctx->port, NULL, 10);
623 ret = setsockopt(fd, ctx->proto, IP_HDRINCL, &one, sizeof(one));
624 if (ret < 0)
625 panic("Kernel does not support IP_HDRINCL!\n");
627 if (ai->ai_family == PF_INET6) {
628 struct sockaddr_in6 *sd6 = (struct sockaddr_in6 *) sd;
630 sd6->sin6_port = 0;
633 break;
636 freeaddrinfo(ahead);
638 if (fd < 0)
639 panic("Cannot create socket! Does remote "
640 "support IPv%d?!\n",
641 ctx->proto == IPPROTO_IP ? 4 : 6);
643 return fd;
646 static void inject_filter(struct ctx *ctx, int fd)
648 struct sock_fprog bpf_ops;
650 enable_kernel_bpf_jit_compiler();
652 memset(&bpf_ops, 0, sizeof(bpf_ops));
653 bpf_ops.filter = (struct sock_filter *) af_ops[ctx->proto].filter;
654 bpf_ops.len = af_ops[ctx->proto].flen;
656 bpf_attach_to_sock(fd, &bpf_ops);
659 static int __process_node(struct ctx *ctx, int fd, int fd_cap, int ttl,
660 int inner_proto, uint8_t *pkt_snd, uint8_t *pkt_rcv,
661 const struct sockaddr_storage *ss,
662 const struct sockaddr_storage *sd, struct timeval *diff)
664 int pkt_id, ret, timeout;
665 struct pollfd pfd;
666 struct timeval start, end;
668 prepare_polling(fd_cap, &pfd);
670 memset(pkt_snd, 0, ctx->totlen);
671 pkt_id = af_ops[ctx->proto].assembler(pkt_snd, ctx->totlen, ttl,
672 inner_proto, ctx,
673 (const struct sockaddr *) sd,
674 (const struct sockaddr *) ss);
676 ret = sendto(fd, pkt_snd, ctx->totlen, 0, (struct sockaddr *) sd,
677 ctx->sd_len);
678 if (ret < 0)
679 panic("sendto failed: %s\n", strerror(errno));
681 bug_on(gettimeofday(&start, NULL));
683 timeout = (ctx->timeout > 0 ? ctx->timeout : 2) * 1000;
685 ret = poll(&pfd, 1, timeout);
686 if (ret > 0 && pfd.revents & POLLIN && sigint == 0) {
687 bug_on(gettimeofday(&end, NULL));
688 if (diff)
689 timersub(&end, &start, diff);
691 ret = recvfrom(fd_cap, pkt_rcv, ctx->rcvlen, 0, NULL, NULL);
692 if (ret < (int) (sizeof(struct ethhdr) + af_ops[ctx->proto].min_len_icmp))
693 return -EIO;
695 return af_ops[ctx->proto].check(pkt_rcv + sizeof(struct ethhdr),
696 ret - sizeof(struct ethhdr), ttl,
697 pkt_id, (const struct sockaddr *) ss);
698 } else {
699 return -EIO;
702 return 0;
705 static void timerdiv(const unsigned long divisor, const struct timeval *tv,
706 struct timeval *result)
708 uint64_t x = ((uint64_t) tv->tv_sec * 1000 * 1000 + tv->tv_usec) / divisor;
710 result->tv_sec = x / 1000 / 1000;
711 result->tv_usec = x % (1000 * 1000);
714 static int timevalcmp(const void *t1, const void *t2)
716 if (timercmp((struct timeval *) t1, (struct timeval *) t2, <))
717 return -1;
718 if (timercmp((struct timeval *) t1, (struct timeval *) t2, >))
719 return 1;
721 return 0;
724 static int __process_time(struct ctx *ctx, int fd, int fd_cap, int ttl,
725 int inner_proto, uint8_t *pkt_snd, uint8_t *pkt_rcv,
726 const struct sockaddr_storage *ss,
727 const struct sockaddr_storage *sd)
729 size_t i, j = 0;
730 int good = 0, ret = -EIO, idx, ret_good = -EIO;
731 struct timeval probes[9], *tmp, sum, res;
732 uint8_t *trash = xmalloc(ctx->rcvlen);
733 char *cwait[] = { "-", "\\", "|", "/" };
734 const char *proto_short[] = {
735 [IPPROTO_TCP] = "t",
736 [IPPROTO_ICMP] = "i",
737 [IPPROTO_ICMPV6] = "i",
740 memset(probes, 0, sizeof(probes));
741 for (i = 0; i < array_size(probes) && sigint == 0; ++i) {
742 ret = __process_node(ctx, fd, fd_cap, ttl, inner_proto,
743 pkt_snd, good == 0 ? pkt_rcv : trash,
744 ss, sd, &probes[i]);
745 if (ret > 0) {
746 if (good == 0)
747 ret_good = ret;
748 good++;
751 if (good == 0 && ctx->queries == (int) i)
752 break;
754 usleep(50000);
756 printf("\r%2d: %s", ttl, cwait[j++]);
757 fflush(stdout);
758 if (j >= array_size(cwait))
759 j = 0;
762 if (good == 0) {
763 xfree(trash);
764 return -EIO;
767 tmp = xmalloc(sizeof(struct timeval) * good);
768 for (i = j = 0; i < array_size(probes); ++i) {
769 if (probes[i].tv_sec == 0 && probes[i].tv_usec == 0)
770 continue;
771 tmp[j].tv_sec = probes[i].tv_sec;
772 tmp[j].tv_usec = probes[i].tv_usec;
773 j++;
776 qsort(tmp, j, sizeof(struct timeval), timevalcmp);
778 printf("\r%2d: %s[", ttl, proto_short[inner_proto]);
779 idx = j / 2;
780 switch (j % 2) {
781 case 0:
782 timeradd(&tmp[idx], &tmp[idx - 1], &sum);
783 timerdiv(2, &sum, &res);
784 if (res.tv_sec > 0)
785 printf("%lu sec ", res.tv_sec);
786 printf("%7lu us", res.tv_usec);
787 break;
788 case 1:
789 if (tmp[idx].tv_sec > 0)
790 printf("%lu sec ", tmp[idx].tv_sec);
791 printf("%7lu us", tmp[idx].tv_usec);
792 break;
794 printf("]");
796 xfree(tmp);
797 xfree(trash);
799 return ret_good;
802 static int __probe_remote(struct ctx *ctx, int fd, int fd_cap, int ttl,
803 uint8_t *pkt_snd, uint8_t *pkt_rcv,
804 const struct sockaddr_storage *ss,
805 const struct sockaddr_storage *sd,
806 int inner_proto)
808 int ret = -EIO, tries = ctx->queries;
810 while (tries-- > 0 && sigint == 0) {
811 ret = __process_time(ctx, fd, fd_cap, ttl, inner_proto,
812 pkt_snd, pkt_rcv, ss, sd);
813 if (ret < 0)
814 continue;
816 af_ops[ctx->proto].handler(pkt_rcv + sizeof(struct ethhdr),
817 ret - sizeof(struct ethhdr),
818 ctx->dns_resolv, ctx->latitude);
819 if (ctx->show) {
820 struct pkt_buff *pkt;
822 printf("\n");
823 pkt = pkt_alloc(pkt_rcv, ret);
824 hex_ascii(pkt);
825 tprintf_flush();
826 pkt_free(pkt);
829 break;
832 return ret;
835 static int __process_ttl(struct ctx *ctx, int fd, int fd_cap, int ttl,
836 uint8_t *pkt_snd, uint8_t *pkt_rcv,
837 const struct sockaddr_storage *ss,
838 const struct sockaddr_storage *sd)
840 int ret = -EIO;
841 size_t i;
842 const int inner_protos[] = {
843 IPPROTO_TCP,
844 IPPROTO_ICMP,
847 printf("%2d: ", ttl);
848 fflush(stdout);
850 for (i = 0; i < array_size(inner_protos) && sigint == 0; ++i) {
851 ret = __probe_remote(ctx, fd, fd_cap, ttl, pkt_snd, pkt_rcv, ss, sd,
852 inner_protos[i]);
853 if (ret > 0)
854 break;
857 if (ret <= 0)
858 printf("\r%2d: ?[ no answer]", ttl);
859 if (ctx->show == 0)
860 printf("\n");
861 if (ctx->show && ret <= 0)
862 printf("\n\n");
864 fflush(stdout);
865 return 0;
868 static int main_trace(struct ctx *ctx)
870 int fd, fd_cap, ifindex, ttl;
871 struct ring dummy_ring;
872 struct sockaddr_storage ss, sd;
873 uint8_t *pkt_snd, *pkt_rcv;
875 fd = get_remote_fd(ctx, &ss, &sd);
876 fd_cap = pf_socket();
878 inject_filter(ctx, fd_cap);
880 ifindex = device_ifindex(ctx->dev);
881 bind_rx_ring(fd_cap, &dummy_ring, ifindex);
883 if (ctx->totlen < af_ops[ctx->proto].min_len_tcp) {
884 ctx->totlen = af_ops[ctx->proto].min_len_tcp;
885 if (ctx->payload)
886 ctx->totlen += strlen(ctx->payload);
889 ctx->rcvlen = device_mtu(ctx->dev) - sizeof(struct ethhdr);
890 if (ctx->totlen >= ctx->rcvlen)
891 panic("Packet len exceeds device MTU!\n");
893 pkt_snd = xmalloc(ctx->totlen);
894 pkt_rcv = xmalloc(ctx->rcvlen);
896 show_trace_info(ctx, &ss, &sd);
898 for (ttl = ctx->init_ttl; ttl <= ctx->max_ttl && sigint == 0; ++ttl)
899 __process_ttl(ctx, fd, fd_cap, ttl, pkt_snd, pkt_rcv,
900 &ss, &sd);
902 xfree(pkt_snd);
903 xfree(pkt_rcv);
905 close(fd_cap);
906 close(fd);
908 return 0;
911 int main(int argc, char **argv)
913 int c, opt_index, ret;
914 struct ctx ctx;
916 setfsuid(getuid());
917 setfsgid(getgid());
919 srand(time(NULL));
921 memset(&ctx, 0, sizeof(ctx));
922 ctx.init_ttl = 1;
923 ctx.max_ttl = 30;
924 ctx.queries = 2;
925 ctx.timeout = 2;
926 ctx.proto = IPPROTO_IP;
927 ctx.payload = NULL;
928 ctx.dev = xstrdup("eth0");
929 ctx.port = xstrdup("80");
931 while ((c = getopt_long(argc, argv, short_options, long_options,
932 &opt_index)) != EOF) {
933 switch (c) {
934 case 'h':
935 help();
936 break;
937 case 'v':
938 version();
939 break;
940 case 'u':
941 update_geoip();
942 die();
943 break;
944 case 'H':
945 ctx.host = xstrdup(optarg);
946 break;
947 case 'p':
948 if (ctx.port)
949 xfree(ctx.port);
950 ctx.port = xstrdup(optarg);
951 break;
952 case 'n':
953 ctx.dns_resolv = 0;
954 break;
955 case '4':
956 ctx.proto = IPPROTO_IP;
957 break;
958 case '6':
959 ctx.proto = IPPROTO_IPV6;
960 break;
961 case 'Z':
962 ctx.show = 1;
963 break;
964 case 'N':
965 ctx.dns_resolv = 1;
966 break;
967 case 'f':
968 ctx.init_ttl = atoi(optarg);
969 if (ctx.init_ttl <= 0)
970 help();
971 break;
972 case 'm':
973 ctx.max_ttl = atoi(optarg);
974 if (ctx.max_ttl <= 0)
975 help();
976 break;
977 case 'i':
978 case 'd':
979 free(ctx.dev);
980 ctx.dev = xstrdup(optarg);
981 break;
982 case 'q':
983 ctx.queries = atoi(optarg);
984 if (ctx.queries <= 0)
985 help();
986 break;
987 case 'x':
988 ctx.timeout = atoi(optarg);
989 if (ctx.timeout <= 0)
990 help();
991 break;
992 case 'L':
993 ctx.latitude = 1;
994 break;
995 case 'S':
996 ctx.syn = 1;
997 break;
998 case 'A':
999 ctx.ack = 1;
1000 break;
1001 case 'F':
1002 ctx.fin = 1;
1003 break;
1004 case 'U':
1005 ctx.urg = 1;
1006 break;
1007 case 'P':
1008 ctx.psh = 1;
1009 break;
1010 case 'R':
1011 ctx.rst = 1;
1012 break;
1013 case 'E':
1014 ctx.syn = 1;
1015 ctx.ecn = 1;
1016 break;
1017 case 't':
1018 ctx.tos = atoi(optarg);
1019 if (ctx.tos < 0)
1020 help();
1021 break;
1022 case 'G':
1023 ctx.nofrag = 1;
1024 break;
1025 case 'X':
1026 ctx.payload = xstrdup(optarg);
1027 break;
1028 case 'l':
1029 ctx.totlen = strtoul(optarg, NULL, 10);
1030 if (ctx.totlen == 0)
1031 help();
1032 break;
1033 case '?':
1034 switch (optopt) {
1035 case 'H':
1036 case 'p':
1037 case 'f':
1038 case 'm':
1039 case 'i':
1040 case 'd':
1041 case 'q':
1042 case 'x':
1043 case 'X':
1044 case 't':
1045 case 'l':
1046 panic("Option -%c requires an argument!\n",
1047 optopt);
1048 default:
1049 if (isprint(optopt))
1050 printf("Unknown option character `0x%X\'!\n", optopt);
1051 die();
1053 default:
1054 break;
1058 if (argc < 3 || !ctx.host || !ctx.port || ctx.init_ttl > ctx.max_ttl ||
1059 ctx.init_ttl > MAXTTL || ctx.max_ttl > MAXTTL)
1060 help();
1062 if (!device_up_and_running(ctx.dev))
1063 panic("Networking device not up and running!\n");
1064 if (device_mtu(ctx.dev) <= ctx.totlen)
1065 panic("Packet larger than device MTU!\n");
1067 register_signal(SIGHUP, signal_handler);
1068 register_signal(SIGINT, signal_handler);
1070 tprintf_init();
1071 init_geoip(1);
1073 ret = main_trace(&ctx);
1075 destroy_geoip();
1076 tprintf_cleanup();
1078 free(ctx.dev);
1079 free(ctx.host);
1080 free(ctx.port);
1081 free(ctx.payload);
1083 return ret;