ifpps: Remove unnecessary memset()
[netsniff-ng.git] / astraceroute.c
blobddf352bd61988bba06d38243d2304abd1ff9a688
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 int init_ttl, max_ttl, dns_resolv, queries, timeout, totlen, rcvlen;
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 unsigned int 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: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 {"dev", required_argument, NULL, 'd'},
94 {"num-probes", required_argument, NULL, 'q'},
95 {"timeout", required_argument, NULL, 'x'},
96 {"tos", required_argument, NULL, 't'},
97 {"payload", required_argument, NULL, 'X'},
98 {"totlen", required_argument, NULL, 'l'},
99 {"numeric", no_argument, NULL, 'n'},
100 {"latitude", no_argument, NULL, 'L'},
101 {"update", no_argument, NULL, 'u'},
102 {"dns", no_argument, NULL, 'N'},
103 {"ipv4", no_argument, NULL, '4'},
104 {"ipv6", no_argument, NULL, '6'},
105 {"syn", no_argument, NULL, 'S'},
106 {"ack", no_argument, NULL, 'A'},
107 {"urg", no_argument, NULL, 'U'},
108 {"fin", no_argument, NULL, 'F'},
109 {"psh", no_argument, NULL, 'P'},
110 {"rst", no_argument, NULL, 'R'},
111 {"ecn-syn", no_argument, NULL, 'E'},
112 {"show-packet", no_argument, NULL, 'Z'},
113 {"nofrag", no_argument, NULL, 'G'},
114 {"version", no_argument, NULL, 'v'},
115 {"help", no_argument, NULL, 'h'},
116 {NULL, 0, NULL, 0}
119 static const struct sock_filter ipv4_icmp_type_11[] = {
120 { 0x28, 0, 0, 0x0000000c }, /* ldh [12] */
121 { 0x15, 0, 8, 0x00000800 }, /* jneq #0x800, drop */
122 { 0x30, 0, 0, 0x00000017 }, /* ldb [23] */
123 { 0x15, 0, 6, 0x00000001 }, /* jneq #0x1, drop */
124 { 0x28, 0, 0, 0x00000014 }, /* ldh [20] */
125 { 0x45, 4, 0, 0x00001fff }, /* jset #0x1fff, drop */
126 { 0xb1, 0, 0, 0x0000000e }, /* ldxb 4*([14]&0xf) */
127 { 0x50, 0, 0, 0x0000000e }, /* ldb [x + 14] */
128 { 0x15, 0, 1, 0x0000000b }, /* jneq #0xb, drop */
129 { 0x06, 0, 0, 0xffffffff }, /* ret #-1 */
130 { 0x06, 0, 0, 0x00000000 }, /* drop: ret #0 */
133 static const struct sock_filter ipv6_icmp6_type_3[] = {
134 { 0x28, 0, 0, 0x0000000c }, /* ldh [12] */
135 { 0x15, 0, 5, 0x000086dd }, /* jneq #0x86dd, drop */
136 { 0x30, 0, 0, 0x00000014 }, /* ldb [20] */
137 { 0x15, 0, 3, 0x0000003a }, /* jneq #0x3a, drop */
138 { 0x30, 0, 0, 0x00000036 }, /* ldb [54] */
139 { 0x15, 0, 1, 0x00000003 }, /* jneq #0x3, drop */
140 { 0x06, 0, 0, 0xffffffff }, /* ret #-1 */
141 { 0x06, 0, 0, 0x00000000 }, /* drop: ret #0 */
144 static const struct proto_ops af_ops[] = {
145 [IPPROTO_IP] = {
146 .assembler = assemble_ipv4,
147 .handler = handle_ipv4,
148 .check = check_ipv4,
149 .filter = ipv4_icmp_type_11,
150 .flen = array_size(ipv4_icmp_type_11),
151 .min_len_tcp = sizeof(struct iphdr) + sizeof(struct tcphdr),
152 .min_len_icmp = sizeof(struct iphdr) + sizeof(struct icmphdr),
154 [IPPROTO_IPV6] = {
155 .assembler = assemble_ipv6,
156 .handler = handle_ipv6,
157 .check = check_ipv6,
158 .filter = ipv6_icmp6_type_3,
159 .flen = array_size(ipv6_icmp6_type_3),
160 .min_len_tcp = sizeof(struct ip6_hdr) + sizeof(struct tcphdr),
161 .min_len_icmp = sizeof(struct ip6_hdr) + sizeof(struct icmp6hdr),
165 static void signal_handler(int number)
167 switch (number) {
168 case SIGINT:
169 sigint = 1;
170 default:
171 break;
175 static void __noreturn help(void)
177 printf("\nastraceroute %s, autonomous system trace route utility\n", VERSION_STRING);
178 puts("http://www.netsniff-ng.org\n\n"
179 "Usage: astraceroute [options]\n"
180 "Options:\n"
181 " -H|--host <host> Host/IPv4/IPv6 to lookup AS route to\n"
182 " -p|--port <port> Hosts port to lookup AS route to\n"
183 " -i|-d|--dev <device> Networking device, e.g. eth0\n"
184 " -f|--init-ttl <ttl> Set initial TTL\n"
185 " -m|--max-ttl <ttl> Set maximum TTL (def: 30)\n"
186 " -q|--num-probes <num> Number of max probes for each hop (def: 2)\n"
187 " -x|--timeout <sec> Probe response timeout in sec (def: 3)\n"
188 " -X|--payload <string> Specify a payload string to test DPIs\n"
189 " -l|--totlen <len> Specify total packet len\n"
190 " -4|--ipv4 Use IPv4-only requests\n"
191 " -6|--ipv6 Use IPv6-only requests\n"
192 " -n|--numeric Do not do reverse DNS lookup for hops\n"
193 " -u|--update Update GeoIP databases\n"
194 " -L|--latitude Show latitude and longtitude\n"
195 " -N|--dns Do a reverse DNS lookup for hops\n"
196 " -S|--syn Set TCP SYN flag\n"
197 " -A|--ack Set TCP ACK flag\n"
198 " -F|--fin Set TCP FIN flag\n"
199 " -P|--psh Set TCP PSH flag\n"
200 " -U|--urg Set TCP URG flag\n"
201 " -R|--rst Set TCP RST flag\n"
202 " -E|--ecn-syn Send ECN SYN packets (RFC3168)\n"
203 " -t|--tos <tos> Set the IP TOS field\n"
204 " -G|--nofrag Set do not fragment bit\n"
205 " -Z|--show-packet Show returned packet on each hop\n"
206 " -v|--version Print version and exit\n"
207 " -h|--help Print this help and exit\n\n"
208 "Examples:\n"
209 " IPv4 trace of AS with TCP SYN probe (this will most-likely pass):\n"
210 " astraceroute -i eth0 -N -S -H netsniff-ng.org\n"
211 " IPv4 trace of AS with TCP ECN SYN probe:\n"
212 " astraceroute -i eth0 -N -E -H netsniff-ng.org\n"
213 " IPv4 trace of AS with TCP FIN probe:\n"
214 " astraceroute -i eth0 -N -F -H netsniff-ng.org\n"
215 " IPv4 trace of AS with Xmas probe:\n"
216 " astraceroute -i eth0 -N -FPU -H netsniff-ng.org\n"
217 " IPv4 trace of AS with Null probe with ASCII payload:\n"
218 " astraceroute -i eth0 -N -H netsniff-ng.org -X \"censor-me\" -Z\n"
219 " IPv6 trace of AS up to www.6bone.net:\n"
220 " astraceroute -6 -i eth0 -S -E -N -H www.6bone.net\n\n"
221 "Note:\n"
222 " If the TCP probe did not give any results, then astraceroute will\n"
223 " automatically probe for classic ICMP packets! To gather more\n"
224 " information about astraceroute's fetched AS numbers, see e.g.\n"
225 " http://bgp.he.net/AS<number>!\n\n"
226 "Please report bugs to <bugs@netsniff-ng.org>\n"
227 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
228 "Swiss federal institute of technology (ETH Zurich)\n"
229 "License: GNU GPL version 2.0\n"
230 "This is free software: you are free to change and redistribute it.\n"
231 "There is NO WARRANTY, to the extent permitted by law.\n");
232 die();
235 static void __noreturn version(void)
237 printf("\nastraceroute %s, Git id: %s\n", VERSION_LONG, GITVERSION);
238 puts("autonomous system trace route utility\n"
239 "http://www.netsniff-ng.org\n\n"
240 "Please report bugs to <bugs@netsniff-ng.org>\n"
241 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
242 "Swiss federal institute of technology (ETH Zurich)\n"
243 "License: GNU GPL version 2.0\n"
244 "This is free software: you are free to change and redistribute it.\n"
245 "There is NO WARRANTY, to the extent permitted by law.\n");
246 die();
249 static void __assemble_data(uint8_t *packet, size_t len, const char *payload)
251 int i;
253 if (payload == NULL) {
254 for (i = 0; i < len; ++i)
255 packet[i] = (uint8_t) rand();
256 } else {
257 int lmin = min(len, strlen(payload));
259 for (i = 0; i < lmin; ++i)
260 packet[i] = (uint8_t) payload[i];
261 for (i = lmin; i < len; ++i)
262 packet[i] = (uint8_t) rand();
266 static void __assemble_icmp4(uint8_t *packet, size_t len)
268 struct icmphdr *icmph = (struct icmphdr *) packet;
270 bug_on(len < sizeof(struct icmphdr));
272 icmph->type = ICMP_ECHO;
273 icmph->code = 0;
274 icmph->checksum = 0;
277 static void __assemble_icmp6(uint8_t *packet, size_t len)
279 struct icmp6hdr *icmp6h = (struct icmp6hdr *) packet;
281 bug_on(len < sizeof(struct icmp6hdr));
283 icmp6h->icmp6_type = ICMPV6_ECHO_REQUEST;
284 icmp6h->icmp6_code = 0;
285 icmp6h->icmp6_cksum = 0;
288 static void __assemble_tcp(uint8_t *packet, size_t len, int syn, int ack,
289 int urg, int fin, int rst, int psh, int ecn,
290 int dport)
292 struct tcphdr *tcph = (struct tcphdr *) packet;
294 bug_on(len < sizeof(struct tcphdr));
296 tcph->source = htons((uint16_t) rand());
297 tcph->dest = htons((uint16_t) dport);
299 tcph->seq = htonl(rand());
300 tcph->ack_seq = (!!ack ? htonl(rand()) : 0);
302 tcph->doff = 5;
304 tcph->syn = !!syn;
305 tcph->ack = !!ack;
306 tcph->urg = !!urg;
307 tcph->fin = !!fin;
308 tcph->rst = !!rst;
309 tcph->psh = !!psh;
310 tcph->ece = !!ecn;
311 tcph->cwr = !!ecn;
313 tcph->window = htons((uint16_t) (100 + (rand() % 65435)));
314 tcph->urg_ptr = (!!urg ? htons((uint16_t) rand()) : 0);
315 tcph->check = 0;
318 static int assemble_ipv4(uint8_t *packet, size_t len, int ttl, int proto,
319 const struct ctx *ctx, const struct sockaddr *dst,
320 const struct sockaddr *src)
322 uint8_t *data;
323 size_t data_len, off_next = 0;
324 struct iphdr *iph = (struct iphdr *) packet;
326 bug_on(!src || !dst);
327 bug_on(src->sa_family != PF_INET || dst->sa_family != PF_INET);
328 bug_on(len < sizeof(*iph) + min(sizeof(struct tcphdr),
329 sizeof(struct icmphdr)));
331 iph->ihl = 5;
332 iph->version = 4;
333 iph->tos = (uint8_t) ctx->tos;
335 iph->tot_len = htons((uint16_t) len);
336 iph->id = htons((uint16_t) rand());
338 iph->frag_off = ctx->nofrag ? IP_DF : 0;
339 iph->ttl = (uint8_t) ttl;
341 iph->saddr = ((const struct sockaddr_in *) src)->sin_addr.s_addr;
342 iph->daddr = ((const struct sockaddr_in *) dst)->sin_addr.s_addr;
344 iph->protocol = (uint8_t) proto;
346 data = packet + sizeof(*iph);
347 data_len = len - sizeof(*iph);
349 switch (proto) {
350 case IPPROTO_TCP:
351 __assemble_tcp(data, data_len, ctx->syn, ctx->ack, ctx->urg,
352 ctx->fin, ctx->rst, ctx->psh, ctx->ecn, ctx->dport);
353 off_next = sizeof(struct tcphdr);
354 break;
355 case IPPROTO_ICMP:
356 __assemble_icmp4(data, data_len);
357 off_next = sizeof(struct icmphdr);
358 break;
359 default:
360 bug();
363 data = packet + sizeof(*iph) + off_next;
364 data_len = len - sizeof(*iph) - off_next;
366 __assemble_data(data, data_len, ctx->payload);
368 iph->check = csum((unsigned short *) packet, ntohs(iph->tot_len) >> 1);
370 return ntohs(iph->id);
373 static int assemble_ipv6(uint8_t *packet, size_t len, int ttl, int proto,
374 const struct ctx *ctx, const struct sockaddr *dst,
375 const struct sockaddr *src)
377 uint8_t *data;
378 size_t data_len, off_next = 0;
379 struct ip6_hdr *ip6h = (struct ip6_hdr *) packet;
381 bug_on(!src || !dst);
382 bug_on(src->sa_family != PF_INET6 || dst->sa_family != PF_INET6);
383 bug_on(len < sizeof(*ip6h) + min(sizeof(struct tcphdr),
384 sizeof(struct icmp6hdr)));
386 ip6h->ip6_flow = htonl(rand() & 0x000fffff);
387 ip6h->ip6_vfc = 0x60;
389 ip6h->ip6_plen = htons((uint16_t) len - sizeof(*ip6h));
390 ip6h->ip6_nxt = (uint8_t) proto;
391 ip6h->ip6_hlim = (uint8_t) ttl;
393 memcpy(&ip6h->ip6_src, &(((const struct sockaddr_in6 *)
394 src)->sin6_addr), sizeof(ip6h->ip6_src));
395 memcpy(&ip6h->ip6_dst, &(((const struct sockaddr_in6 *)
396 dst)->sin6_addr), sizeof(ip6h->ip6_dst));
398 data = packet + sizeof(*ip6h);
399 data_len = len - sizeof(*ip6h);
401 switch (proto) {
402 case IPPROTO_TCP:
403 __assemble_tcp(data, data_len, ctx->syn, ctx->ack, ctx->urg,
404 ctx->fin, ctx->rst, ctx->psh, ctx->ecn, ctx->dport);
405 off_next = sizeof(struct tcphdr);
406 break;
407 case IPPROTO_ICMP:
408 case IPPROTO_ICMPV6:
409 __assemble_icmp6(data, data_len);
410 off_next = sizeof(struct icmp6hdr);
411 break;
412 default:
413 bug();
416 data = packet + sizeof(*ip6h) + off_next;
417 data_len = len - sizeof(*ip6h) - off_next;
419 __assemble_data(data, data_len, ctx->payload);
421 return ntohl(ip6h->ip6_flow) & 0x000fffff;
424 static int check_ipv4(uint8_t *packet, size_t len, int ttl, int id,
425 const struct sockaddr *ss)
427 struct iphdr *iph = (struct iphdr *) packet;
428 struct iphdr *iph_inner;
429 struct icmphdr *icmph;
431 if (iph->protocol != IPPROTO_ICMP)
432 return -EINVAL;
433 if (iph->daddr != ((const struct sockaddr_in *) ss)->sin_addr.s_addr)
434 return -EINVAL;
436 icmph = (struct icmphdr *) (packet + sizeof(struct iphdr));
437 if (icmph->type != ICMP_TIME_EXCEEDED)
438 return -EINVAL;
439 if (icmph->code != ICMP_EXC_TTL)
440 return -EINVAL;
442 iph_inner = (struct iphdr *) (packet + sizeof(struct iphdr) +
443 sizeof(struct icmphdr));
444 if (ntohs(iph_inner->id) != id)
445 return -EINVAL;
447 return len;
450 static void handle_ipv4(uint8_t *packet, size_t len, int dns_resolv, int latitude)
452 char hbuff[NI_MAXHOST];
453 struct iphdr *iph = (struct iphdr *) packet;
454 struct sockaddr_in sd;
455 struct hostent *hent;
456 const char *as, *country, *city;
458 memset(hbuff, 0, sizeof(hbuff));
459 memset(&sd, 0, sizeof(sd));
460 sd.sin_family = PF_INET;
461 sd.sin_addr.s_addr = iph->saddr;
463 getnameinfo((struct sockaddr *) &sd, sizeof(sd),
464 hbuff, sizeof(hbuff), NULL, 0, NI_NUMERICHOST);
466 as = geoip4_as_name(sd);
467 country = geoip4_country_name(sd);
468 city = geoip4_city_name(sd);
470 if (dns_resolv) {
471 hent = gethostbyaddr(&sd.sin_addr, sizeof(sd.sin_addr), PF_INET);
472 if (hent)
473 printf(" %s (%s)", hent->h_name, hbuff);
474 else
475 printf(" %s", hbuff);
476 } else {
477 printf(" %s", hbuff);
479 if (as)
480 printf(" in %s", as);
481 if (country) {
482 printf(" in %s", country);
483 if (city)
484 printf(", %s", city);
486 if (latitude)
487 printf(" (%f/%f)", geoip4_latitude(sd), geoip4_longitude(sd));
490 static int check_ipv6(uint8_t *packet, size_t len, int ttl, int id,
491 const struct sockaddr *ss)
493 struct ip6_hdr *ip6h = (struct ip6_hdr *) packet;
494 struct ip6_hdr *ip6h_inner;
495 struct icmp6hdr *icmp6h;
497 if (ip6h->ip6_nxt != 0x3a)
498 return -EINVAL;
499 if (memcmp(&ip6h->ip6_dst, &(((const struct sockaddr_in6 *)
500 ss)->sin6_addr), sizeof(ip6h->ip6_dst)))
501 return -EINVAL;
503 icmp6h = (struct icmp6hdr *) (packet + sizeof(*ip6h));
504 if (icmp6h->icmp6_type != ICMPV6_TIME_EXCEED)
505 return -EINVAL;
506 if (icmp6h->icmp6_code != ICMPV6_EXC_HOPLIMIT)
507 return -EINVAL;
509 ip6h_inner = (struct ip6_hdr *) (packet + sizeof(*ip6h) + sizeof(*icmp6h));
510 if ((ntohl(ip6h_inner->ip6_flow) & 0x000fffff) != id)
511 return -EINVAL;
513 return len;
516 static void handle_ipv6(uint8_t *packet, size_t len, int dns_resolv, int latitude)
518 char hbuff[NI_MAXHOST];
519 struct ip6_hdr *ip6h = (struct ip6_hdr *) packet;
520 struct sockaddr_in6 sd;
521 struct hostent *hent;
522 const char *as, *country, *city;
524 memset(hbuff, 0, sizeof(hbuff));
525 memset(&sd, 0, sizeof(sd));
526 sd.sin6_family = PF_INET6;
527 memcpy(&sd.sin6_addr, &ip6h->ip6_src, sizeof(ip6h->ip6_src));
529 getnameinfo((struct sockaddr *) &sd, sizeof(sd),
530 hbuff, sizeof(hbuff), NULL, 0, NI_NUMERICHOST);
532 as = geoip6_as_name(sd);
533 country = geoip6_country_name(sd);
534 city = geoip6_city_name(sd);
536 if (dns_resolv) {
537 hent = gethostbyaddr(&sd.sin6_addr, sizeof(sd.sin6_addr), PF_INET6);
538 if (hent)
539 printf(" %s (%s)", hent->h_name, hbuff);
540 else
541 printf(" %s", hbuff);
542 } else {
543 printf(" %s", hbuff);
545 if (as)
546 printf(" in %s", as);
547 if (country) {
548 printf(" in %s", country);
549 if (city)
550 printf(", %s", city);
552 if (latitude)
553 printf(" (%f/%f)", geoip6_latitude(sd), geoip6_longitude(sd));
556 static void show_trace_info(struct ctx *ctx, const struct sockaddr_storage *ss,
557 const struct sockaddr_storage *sd)
559 char hbuffs[256], hbuffd[256];
561 memset(hbuffd, 0, sizeof(hbuffd));
562 getnameinfo((struct sockaddr *) sd, sizeof(*sd),
563 hbuffd, sizeof(hbuffd), NULL, 0, NI_NUMERICHOST);
565 memset(hbuffs, 0, sizeof(hbuffs));
566 getnameinfo((struct sockaddr *) ss, sizeof(*ss),
567 hbuffs, sizeof(hbuffs), NULL, 0, NI_NUMERICHOST);
569 printf("AS path IPv%d TCP trace from %s to %s:%s (%s) with len %d "
570 "Bytes, %u max hops\n", ctx->proto == IPPROTO_IP ? 4 : 6,
571 hbuffs, hbuffd, ctx->port, ctx->host, ctx->totlen, ctx->max_ttl);
573 printf("Using flags SYN:%d,ACK:%d,ECN:%d,FIN:%d,PSH:%d,RST:%d,URG:%d\n",
574 ctx->syn, ctx->ack, ctx->ecn, ctx->fin, ctx->psh, ctx->rst, ctx->urg);
576 if (ctx->payload)
577 printf("With payload: \'%s\'\n", ctx->payload);
580 static int get_remote_fd(struct ctx *ctx, struct sockaddr_storage *ss,
581 struct sockaddr_storage *sd)
583 int fd = -1, ret, one = 1;
584 struct addrinfo hints, *ahead, *ai;
586 memset(&hints, 0, sizeof(hints));
587 hints.ai_family = PF_UNSPEC;
588 hints.ai_socktype = SOCK_STREAM;
589 hints.ai_protocol = IPPROTO_TCP;
590 hints.ai_flags = AI_NUMERICSERV;
592 ret = getaddrinfo(ctx->host, ctx->port, &hints, &ahead);
593 if (ret < 0)
594 panic("Cannot get address info!\n");
596 for (ai = ahead; ai != NULL && fd < 0; ai = ai->ai_next) {
597 if (!((ai->ai_family == PF_INET6 && ctx->proto == IPPROTO_IPV6) ||
598 (ai->ai_family == PF_INET && ctx->proto == IPPROTO_IP)))
599 continue;
601 fd = socket(ai->ai_family, SOCK_RAW, IPPROTO_RAW);
602 if (fd < 0)
603 continue;
605 memset(ss, 0, sizeof(*ss));
606 ret = device_address(ctx->dev, ai->ai_family, ss);
607 if (ret < 0)
608 panic("Cannot get own device address!\n");
610 ret = bind(fd, (struct sockaddr *) ss, sizeof(*ss));
611 if (ret < 0)
612 panic("Cannot bind socket!\n");
614 memset(sd, 0, sizeof(*sd));
615 memcpy(sd, ai->ai_addr, ai->ai_addrlen);
617 ctx->sd_len = ai->ai_addrlen;
618 ctx->dport = strtoul(ctx->port, NULL, 10);
620 ret = setsockopt(fd, ctx->proto, IP_HDRINCL, &one, sizeof(one));
621 if (ret < 0)
622 panic("Kernel does not support IP_HDRINCL!\n");
624 if (ai->ai_family == PF_INET6) {
625 struct sockaddr_in6 *sd6 = (struct sockaddr_in6 *) sd;
627 sd6->sin6_port = 0;
630 break;
633 freeaddrinfo(ahead);
635 if (fd < 0)
636 panic("Cannot create socket! Does remote "
637 "support IPv%d?!\n",
638 ctx->proto == IPPROTO_IP ? 4 : 6);
640 return fd;
643 static void inject_filter(struct ctx *ctx, int fd)
645 struct sock_fprog bpf_ops;
647 enable_kernel_bpf_jit_compiler();
649 memset(&bpf_ops, 0, sizeof(bpf_ops));
650 bpf_ops.filter = (struct sock_filter *) af_ops[ctx->proto].filter;
651 bpf_ops.len = af_ops[ctx->proto].flen;
653 bpf_attach_to_sock(fd, &bpf_ops);
656 static int __process_node(struct ctx *ctx, int fd, int fd_cap, int ttl,
657 int inner_proto, uint8_t *pkt_snd, uint8_t *pkt_rcv,
658 const struct sockaddr_storage *ss,
659 const struct sockaddr_storage *sd, struct timeval *diff)
661 int pkt_id, ret, timeout;
662 struct pollfd pfd;
663 struct timeval start, end;
665 prepare_polling(fd_cap, &pfd);
667 memset(pkt_snd, 0, ctx->totlen);
668 pkt_id = af_ops[ctx->proto].assembler(pkt_snd, ctx->totlen, ttl,
669 inner_proto, ctx,
670 (const struct sockaddr *) sd,
671 (const struct sockaddr *) ss);
673 ret = sendto(fd, pkt_snd, ctx->totlen, 0, (struct sockaddr *) sd,
674 ctx->sd_len);
675 if (ret < 0)
676 panic("sendto failed: %s\n", strerror(errno));
678 bug_on(gettimeofday(&start, NULL));
680 timeout = (ctx->timeout > 0 ? ctx->timeout : 2) * 1000;
682 ret = poll(&pfd, 1, timeout);
683 if (ret > 0 && pfd.revents & POLLIN && sigint == 0) {
684 bug_on(gettimeofday(&end, NULL));
685 if (diff)
686 timersub(&end, &start, diff);
688 ret = recvfrom(fd_cap, pkt_rcv, ctx->rcvlen, 0, NULL, NULL);
689 if (ret < sizeof(struct ethhdr) + af_ops[ctx->proto].min_len_icmp)
690 return -EIO;
692 return af_ops[ctx->proto].check(pkt_rcv + sizeof(struct ethhdr),
693 ret - sizeof(struct ethhdr), ttl,
694 pkt_id, (const struct sockaddr *) ss);
695 } else {
696 return -EIO;
699 return 0;
702 static void timerdiv(const unsigned long divisor, const struct timeval *tv,
703 struct timeval *result)
705 uint64_t x = ((uint64_t) tv->tv_sec * 1000 * 1000 + tv->tv_usec) / divisor;
707 result->tv_sec = x / 1000 / 1000;
708 result->tv_usec = x % (1000 * 1000);
711 static int timevalcmp(const void *t1, const void *t2)
713 if (timercmp((struct timeval *) t1, (struct timeval *) t2, <))
714 return -1;
715 if (timercmp((struct timeval *) t1, (struct timeval *) t2, >))
716 return 1;
718 return 0;
721 static int __process_time(struct ctx *ctx, int fd, int fd_cap, int ttl,
722 int inner_proto, uint8_t *pkt_snd, uint8_t *pkt_rcv,
723 const struct sockaddr_storage *ss,
724 const struct sockaddr_storage *sd)
726 int good = 0, i, j = 0, ret = -EIO, idx, ret_good = -EIO;
727 struct timeval probes[9], *tmp, sum, res;
728 uint8_t *trash = xmalloc(ctx->rcvlen);
729 char *cwait[] = { "-", "\\", "|", "/" };
730 const char *proto_short[] = {
731 [IPPROTO_TCP] = "t",
732 [IPPROTO_ICMP] = "i",
733 [IPPROTO_ICMPV6] = "i",
736 memset(probes, 0, sizeof(probes));
737 for (i = 0; i < array_size(probes) && sigint == 0; ++i) {
738 ret = __process_node(ctx, fd, fd_cap, ttl, inner_proto,
739 pkt_snd, good == 0 ? pkt_rcv : trash,
740 ss, sd, &probes[i]);
741 if (ret > 0) {
742 if (good == 0)
743 ret_good = ret;
744 good++;
747 if (good == 0 && ctx->queries == i)
748 break;
750 usleep(50000);
752 printf("\r%2d: %s", ttl, cwait[j++]);
753 fflush(stdout);
754 if (j >= array_size(cwait))
755 j = 0;
758 if (good == 0) {
759 xfree(trash);
760 return -EIO;
763 tmp = xmalloc(sizeof(struct timeval) * good);
764 for (i = j = 0; i < array_size(probes); ++i) {
765 if (probes[i].tv_sec == 0 && probes[i].tv_usec == 0)
766 continue;
767 tmp[j].tv_sec = probes[i].tv_sec;
768 tmp[j].tv_usec = probes[i].tv_usec;
769 j++;
772 qsort(tmp, j, sizeof(struct timeval), timevalcmp);
774 printf("\r%2d: %s[", ttl, proto_short[inner_proto]);
775 idx = j / 2;
776 switch (j % 2) {
777 case 0:
778 timeradd(&tmp[idx], &tmp[idx - 1], &sum);
779 timerdiv(2, &sum, &res);
780 if (res.tv_sec > 0)
781 printf("%lu sec ", res.tv_sec);
782 printf("%7lu us", res.tv_usec);
783 break;
784 case 1:
785 if (tmp[idx].tv_sec > 0)
786 printf("%lu sec ", tmp[idx].tv_sec);
787 printf("%7lu us", tmp[idx].tv_usec);
788 break;
790 printf("]");
792 xfree(tmp);
793 xfree(trash);
795 return ret_good;
798 static int __probe_remote(struct ctx *ctx, int fd, int fd_cap, int ttl,
799 uint8_t *pkt_snd, uint8_t *pkt_rcv,
800 const struct sockaddr_storage *ss,
801 const struct sockaddr_storage *sd,
802 int inner_proto)
804 int ret = -EIO, tries = ctx->queries;
806 while (tries-- > 0 && sigint == 0) {
807 ret = __process_time(ctx, fd, fd_cap, ttl, inner_proto,
808 pkt_snd, pkt_rcv, ss, sd);
809 if (ret < 0)
810 continue;
812 af_ops[ctx->proto].handler(pkt_rcv + sizeof(struct ethhdr),
813 ret - sizeof(struct ethhdr),
814 ctx->dns_resolv, ctx->latitude);
815 if (ctx->show) {
816 struct pkt_buff *pkt;
818 printf("\n");
819 pkt = pkt_alloc(pkt_rcv, ret);
820 hex_ascii(pkt);
821 tprintf_flush();
822 pkt_free(pkt);
825 break;
828 return ret;
831 static int __process_ttl(struct ctx *ctx, int fd, int fd_cap, int ttl,
832 uint8_t *pkt_snd, uint8_t *pkt_rcv,
833 const struct sockaddr_storage *ss,
834 const struct sockaddr_storage *sd)
836 int ret = -EIO, i;
837 const int inner_protos[] = {
838 IPPROTO_TCP,
839 IPPROTO_ICMP,
842 printf("%2d: ", ttl);
843 fflush(stdout);
845 for (i = 0; i < array_size(inner_protos) && sigint == 0; ++i) {
846 ret = __probe_remote(ctx, fd, fd_cap, ttl, pkt_snd, pkt_rcv, ss, sd,
847 inner_protos[i]);
848 if (ret > 0)
849 break;
852 if (ret <= 0)
853 printf("\r%2d: ?[ no answer]", ttl);
854 if (ctx->show == 0)
855 printf("\n");
856 if (ctx->show && ret <= 0)
857 printf("\n\n");
859 fflush(stdout);
860 return 0;
863 static int main_trace(struct ctx *ctx)
865 int fd, fd_cap, ifindex, ttl;
866 struct ring dummy_ring;
867 struct sockaddr_storage ss, sd;
868 uint8_t *pkt_snd, *pkt_rcv;
870 fd = get_remote_fd(ctx, &ss, &sd);
871 fd_cap = pf_socket();
873 inject_filter(ctx, fd_cap);
875 ifindex = device_ifindex(ctx->dev);
876 bind_rx_ring(fd_cap, &dummy_ring, ifindex);
878 if (ctx->totlen < af_ops[ctx->proto].min_len_tcp) {
879 ctx->totlen = af_ops[ctx->proto].min_len_tcp;
880 if (ctx->payload)
881 ctx->totlen += strlen(ctx->payload);
884 ctx->rcvlen = device_mtu(ctx->dev) - sizeof(struct ethhdr);
885 if (ctx->totlen >= ctx->rcvlen)
886 panic("Packet len exceeds device MTU!\n");
888 pkt_snd = xmalloc(ctx->totlen);
889 pkt_rcv = xmalloc(ctx->rcvlen);
891 show_trace_info(ctx, &ss, &sd);
893 for (ttl = ctx->init_ttl; ttl <= ctx->max_ttl && sigint == 0; ++ttl)
894 __process_ttl(ctx, fd, fd_cap, ttl, pkt_snd, pkt_rcv,
895 &ss, &sd);
897 xfree(pkt_snd);
898 xfree(pkt_rcv);
900 close(fd_cap);
901 close(fd);
903 return 0;
906 int main(int argc, char **argv)
908 int c, opt_index, ret;
909 struct ctx ctx;
911 setfsuid(getuid());
912 setfsgid(getgid());
914 srand(time(NULL));
916 memset(&ctx, 0, sizeof(ctx));
917 ctx.init_ttl = 1;
918 ctx.max_ttl = 30;
919 ctx.queries = 2;
920 ctx.timeout = 2;
921 ctx.proto = IPPROTO_IP;
922 ctx.payload = NULL;
923 ctx.dev = xstrdup("eth0");
924 ctx.port = xstrdup("80");
926 while ((c = getopt_long(argc, argv, short_options, long_options,
927 &opt_index)) != EOF) {
928 switch (c) {
929 case 'h':
930 help();
931 break;
932 case 'v':
933 version();
934 break;
935 case 'u':
936 update_geoip();
937 die();
938 break;
939 case 'H':
940 ctx.host = xstrdup(optarg);
941 break;
942 case 'p':
943 if (ctx.port)
944 xfree(ctx.port);
945 ctx.port = xstrdup(optarg);
946 break;
947 case 'n':
948 ctx.dns_resolv = 0;
949 break;
950 case '4':
951 ctx.proto = IPPROTO_IP;
952 break;
953 case '6':
954 ctx.proto = IPPROTO_IPV6;
955 break;
956 case 'Z':
957 ctx.show = 1;
958 break;
959 case 'N':
960 ctx.dns_resolv = 1;
961 break;
962 case 'f':
963 ctx.init_ttl = atoi(optarg);
964 if (ctx.init_ttl <= 0)
965 help();
966 break;
967 case 'm':
968 ctx.max_ttl = atoi(optarg);
969 if (ctx.max_ttl <= 0)
970 help();
971 break;
972 case 'i':
973 case 'd':
974 free(ctx.dev);
975 ctx.dev = xstrdup(optarg);
976 break;
977 case 'q':
978 ctx.queries = atoi(optarg);
979 if (ctx.queries <= 0)
980 help();
981 break;
982 case 'x':
983 ctx.timeout = atoi(optarg);
984 if (ctx.timeout <= 0)
985 help();
986 break;
987 case 'L':
988 ctx.latitude = 1;
989 break;
990 case 'S':
991 ctx.syn = 1;
992 break;
993 case 'A':
994 ctx.ack = 1;
995 break;
996 case 'F':
997 ctx.fin = 1;
998 break;
999 case 'U':
1000 ctx.urg = 1;
1001 break;
1002 case 'P':
1003 ctx.psh = 1;
1004 break;
1005 case 'R':
1006 ctx.rst = 1;
1007 break;
1008 case 'E':
1009 ctx.syn = 1;
1010 ctx.ecn = 1;
1011 break;
1012 case 't':
1013 ctx.tos = atoi(optarg);
1014 if (ctx.tos < 0)
1015 help();
1016 break;
1017 case 'G':
1018 ctx.nofrag = 1;
1019 break;
1020 case 'X':
1021 ctx.payload = xstrdup(optarg);
1022 break;
1023 case 'l':
1024 ctx.totlen = atoi(optarg);
1025 if (ctx.totlen <= 0)
1026 help();
1027 break;
1028 case '?':
1029 switch (optopt) {
1030 case 'H':
1031 case 'p':
1032 case 'f':
1033 case 'm':
1034 case 'i':
1035 case 'd':
1036 case 'q':
1037 case 'x':
1038 case 'X':
1039 case 't':
1040 case 'l':
1041 panic("Option -%c requires an argument!\n",
1042 optopt);
1043 default:
1044 if (isprint(optopt))
1045 printf("Unknown option character `0x%X\'!\n", optopt);
1046 die();
1048 default:
1049 break;
1053 if (argc < 3 || !ctx.host || !ctx.port || ctx.init_ttl > ctx.max_ttl ||
1054 ctx.init_ttl > MAXTTL || ctx.max_ttl > MAXTTL)
1055 help();
1057 if (!device_up_and_running(ctx.dev))
1058 panic("Networking device not up and running!\n");
1059 if (device_mtu(ctx.dev) <= ctx.totlen)
1060 panic("Packet larger than device MTU!\n");
1062 register_signal(SIGHUP, signal_handler);
1063 register_signal(SIGINT, signal_handler);
1065 tprintf_init();
1066 init_geoip(1);
1068 ret = main_trace(&ctx);
1070 destroy_geoip();
1071 tprintf_cleanup();
1073 free(ctx.dev);
1074 free(ctx.host);
1075 free(ctx.port);
1076 free(ctx.payload);
1078 return ret;