4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
15 #include <sys/socket.h>
16 #include <netinet/in.h>
17 #include <netinet/icmp6.h>
19 #include <linux/types.h>
20 #include <linux/errqueue.h>
27 #include <arpa/inet.h>
30 #define SOL_IPV6 IPPROTO_IPV6
33 #ifndef IP_PMTUDISC_DO
34 #define IP_PMTUDISC_DO 3
36 #ifndef IPV6_PMTUDISC_DO
37 #define IPV6_PMTUDISC_DO 3
43 struct timeval sendtime;
46 struct hhistory his[64];
49 sa_family_t family = AF_INET6;
50 struct sockaddr_storage target;
62 #define HOST_COLUMN_SIZE 52
70 void data_wait(int fd)
78 select(fd+1, &fds, NULL, NULL, &tv);
81 void print_host(const char *a, const char *b, int both)
88 plen += strlen(b) + 3;
90 if (plen >= HOST_COLUMN_SIZE)
91 plen = HOST_COLUMN_SIZE - 1;
92 printf("%*s", HOST_COLUMN_SIZE - plen, "");
95 int recverr(int fd, int ttl)
98 struct probehdr rcvbuf;
102 struct cmsghdr *cmsg;
103 struct sock_extended_err *e;
104 struct sockaddr_storage addr;
106 struct timeval *rettv;
114 memset(&rcvbuf, -1, sizeof(rcvbuf));
115 iov.iov_base = &rcvbuf;
116 iov.iov_len = sizeof(rcvbuf);
117 msg.msg_name = (caddr_t)&addr;
118 msg.msg_namelen = sizeof(addr);
122 msg.msg_control = cbuf;
123 msg.msg_controllen = sizeof(cbuf);
125 gettimeofday(&tv, NULL);
126 res = recvmsg(fd, &msg, MSG_ERRQUEUE);
143 slot += ntohs(((struct sockaddr_in6 *)&addr)->sin6_port);
146 slot += ntohs(((struct sockaddr_in *)&addr)->sin_port);
150 if (slot >= 0 && slot < 63 && his[slot].hops) {
151 sndhops = his[slot].hops;
152 rettv = &his[slot].sendtime;
156 if (res == sizeof(rcvbuf)) {
157 if (rcvbuf.ttl == 0 || rcvbuf.tv.tv_sec == 0)
160 sndhops = rcvbuf.ttl;
165 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
166 switch (cmsg->cmsg_level) {
168 switch(cmsg->cmsg_type) {
170 e = (struct sock_extended_err *)CMSG_DATA(cmsg);
173 #ifdef IPV6_2292HOPLIMIT
174 case IPV6_2292HOPLIMIT:
176 rethops = *(int*)CMSG_DATA(cmsg);
179 printf("cmsg6:%d\n ", cmsg->cmsg_type);
183 switch(cmsg->cmsg_type) {
185 e = (struct sock_extended_err *)CMSG_DATA(cmsg);
188 rethops = *(__u8*)CMSG_DATA(cmsg);
191 printf("cmsg4:%d\n ", cmsg->cmsg_type);
199 if (e->ee_origin == SO_EE_ORIGIN_LOCAL)
200 printf("%2d?: %-32s ", ttl, "[LOCALHOST]");
201 else if (e->ee_origin == SO_EE_ORIGIN_ICMP6 ||
202 e->ee_origin == SO_EE_ORIGIN_ICMP) {
203 char abuf[NI_MAXHOST], hbuf[NI_MAXHOST];
204 struct sockaddr *sa = (struct sockaddr *)(e + 1);
208 printf("%2d: ", sndhops);
210 printf("%2d?: ", ttl);
212 switch (sa->sa_family) {
214 salen = sizeof(struct sockaddr_in6);
217 salen = sizeof(struct sockaddr_in);
223 if (no_resolve || show_both) {
224 if (getnameinfo(sa, salen,
225 abuf, sizeof(abuf), NULL, 0,
231 if (!no_resolve || show_both) {
233 if (getnameinfo(sa, salen,
234 hbuf, sizeof(hbuf), NULL, 0,
241 print_host(abuf, hbuf, show_both);
243 print_host(hbuf, abuf, show_both);
247 int diff = (tv.tv_sec-rettv->tv_sec)*1000000+(tv.tv_usec-rettv->tv_usec);
248 printf("%3d.%03dms ", diff/1000, diff%1000);
250 printf("(This broken router returned corrupted payload) ");
253 switch (e->ee_errno) {
258 printf("pmtu %d\n", e->ee_info);
264 hops_to = sndhops<0 ? ttl : sndhops;
271 if ((e->ee_origin == SO_EE_ORIGIN_ICMP &&
274 (e->ee_origin == SO_EE_ORIGIN_ICMP6 &&
279 rethops = 65-rethops;
280 else if (rethops<=128)
281 rethops = 129-rethops;
283 rethops = 256-rethops;
284 if (sndhops>=0 && rethops != sndhops)
285 printf("asymm %2d ", rethops);
286 else if (sndhops<0 && rethops != ttl)
287 printf("asymm %2d ", rethops);
309 int probe_ttl(int fd, int ttl)
313 struct probehdr *hdr = (struct probehdr*)sndbuf;
315 memset(sndbuf, 0, mtu);
319 for (i=0; i<10; i++) {
325 ((struct sockaddr_in6 *)&target)->sin6_port = htons(base_port + hisptr);
328 ((struct sockaddr_in *)&target)->sin_port = htons(base_port + hisptr);
331 gettimeofday(&hdr->tv, NULL);
332 his[hisptr].hops = ttl;
333 his[hisptr].sendtime = hdr->tv;
334 if (sendto(fd, sndbuf, mtu-overhead, 0, (struct sockaddr *)&target, targetlen) > 0)
336 res = recverr(fd, ttl);
337 his[hisptr].hops = 0;
343 hisptr = (hisptr + 1) & 63;
347 if (recv(fd, sndbuf, sizeof(sndbuf), MSG_DONTWAIT) > 0) {
348 printf("%2d?: reply received 8)\n", ttl);
351 return recverr(fd, ttl);
354 printf("%2d: send failed\n", ttl);
358 static void usage(void) __attribute((noreturn));
360 static void usage(void)
362 fprintf(stderr, "Usage: tracepath6 [-n] [-b] [-l <len>] <destination>[/<port>]\n");
367 int main(int argc, char **argv)
373 struct addrinfo hints, *ai, *ai0;
376 char pbuf[NI_MAXSERV];
378 while ((ch = getopt(argc, argv, "nbh?l:")) != EOF) {
387 if ((mtu = atoi(optarg)) <= overhead) {
388 fprintf(stderr, "Error: length must be >= %d\n", overhead);
403 p = strchr(argv[0], '/');
406 base_port = (unsigned)atoi(p+1);
410 sprintf(pbuf, "%u", base_port);
412 memset(&hints, 0, sizeof(hints));
413 hints.ai_family = family;
414 hints.ai_socktype = SOCK_DGRAM;
415 hints.ai_protocol = IPPROTO_UDP;
417 gai = getaddrinfo(argv[0], pbuf, &hints, &ai0);
419 herror("getaddrinfo"); /*XXX*/
424 for (ai = ai0; ai; ai = ai->ai_next) {
426 if (family && ai->ai_family != family)
428 family = ai->ai_family;
429 fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
432 memcpy(&target, ai->ai_addr, sizeof(target));
433 targetlen = ai->ai_addrlen;
437 perror("socket/connect");
446 on = IPV6_PMTUDISC_DO;
447 if (setsockopt(fd, SOL_IPV6, IPV6_MTU_DISCOVER, &on, sizeof(on)) &&
448 (on = IPV6_PMTUDISC_DO,
449 setsockopt(fd, SOL_IPV6, IPV6_MTU_DISCOVER, &on, sizeof(on)))) {
450 perror("IPV6_MTU_DISCOVER");
454 if (setsockopt(fd, SOL_IPV6, IPV6_RECVERR, &on, sizeof(on))) {
455 perror("IPV6_RECVERR");
459 #ifdef IPV6_RECVHOPLIMIT
460 setsockopt(fd, SOL_IPV6, IPV6_HOPLIMIT, &on, sizeof(on)) &&
461 setsockopt(fd, SOL_IPV6, IPV6_2292HOPLIMIT, &on, sizeof(on))
463 setsockopt(fd, SOL_IPV6, IPV6_HOPLIMIT, &on, sizeof(on))
466 perror("IPV6_HOPLIMIT");
469 if (!IN6_IS_ADDR_V4MAPPED(&(((struct sockaddr_in6 *)&target)->sin6_addr)))
477 if (setsockopt(fd, SOL_IP, IP_MTU_DISCOVER, &on, sizeof(on))) {
478 perror("IP_MTU_DISCOVER");
482 if (setsockopt(fd, SOL_IP, IP_RECVERR, &on, sizeof(on))) {
483 perror("IP_RECVERR");
486 if (setsockopt(fd, SOL_IP, IP_RECVTTL, &on, sizeof(on))) {
487 perror("IP_RECVTTL");
492 for (ttl=1; ttl<32; ttl++) {
499 if (setsockopt(fd, SOL_IPV6, IPV6_UNICAST_HOPS, &on, sizeof(on))) {
500 perror("IPV6_UNICAST_HOPS");
507 if (setsockopt(fd, SOL_IP, IP_TTL, &on, sizeof(on))) {
514 for (i=0; i<3; i++) {
518 res = probe_ttl(fd, ttl);
528 printf("%2d: no reply\n", ttl);
530 printf(" Too many hops: pmtu %d\n", mtu);
533 printf(" Resume: pmtu %d ", mtu);
535 printf("hops %d ", hops_to);
537 printf("back %d ", hops_from);