Ditch the sysvinit stuff
[profile/ivi/iputils.git] / tracepath6.c
1 /*
2  * tracepath6.c
3  *
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.
8  *
9  * Authors:     Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10  */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <sys/socket.h>
16 #include <netinet/in.h>
17 #include <netinet/icmp6.h>
18
19 #include <linux/types.h>
20 #include <linux/errqueue.h>
21 #include <errno.h>
22 #include <string.h>
23 #include <netdb.h>
24 #include <resolv.h>
25 #include <sys/time.h>
26 #include <sys/uio.h>
27 #include <arpa/inet.h>
28
29 #ifndef SOL_IPV6
30 #define SOL_IPV6 IPPROTO_IPV6
31 #endif
32
33 #ifndef IP_PMTUDISC_DO
34 #define IP_PMTUDISC_DO          3
35 #endif
36 #ifndef IPV6_PMTUDISC_DO
37 #define IPV6_PMTUDISC_DO        3
38 #endif
39
40 struct hhistory
41 {
42         int     hops;
43         struct timeval sendtime;
44 };
45
46 struct hhistory his[64];
47 int hisptr;
48
49 sa_family_t family = AF_INET6;
50 struct sockaddr_storage target;
51 socklen_t targetlen;
52 __u16 base_port;
53
54 int overhead = 48;
55 int mtu = 128000;
56 int hops_to = -1;
57 int hops_from = -1;
58 int no_resolve = 0;
59 int show_both = 0;
60 int mapped;
61
62 #define HOST_COLUMN_SIZE        52
63
64 struct probehdr
65 {
66         __u32 ttl;
67         struct timeval tv;
68 };
69
70 void data_wait(int fd)
71 {
72         fd_set fds;
73         struct timeval tv;
74         FD_ZERO(&fds);
75         FD_SET(fd, &fds);
76         tv.tv_sec = 1;
77         tv.tv_usec = 0;
78         select(fd+1, &fds, NULL, NULL, &tv);
79 }
80
81 void print_host(const char *a, const char *b, int both)
82 {
83         size_t plen = 0;
84         printf("%s", a);
85         plen = strlen(a);
86         if (both) {
87                 printf(" (%s)", b);
88                 plen += strlen(b) + 3;
89         }
90         if (plen >= HOST_COLUMN_SIZE)
91                 plen = HOST_COLUMN_SIZE - 1;
92         printf("%*s", HOST_COLUMN_SIZE - plen, "");
93 }
94
95 int recverr(int fd, int ttl)
96 {
97         int res;
98         struct probehdr rcvbuf;
99         char cbuf[512];
100         struct iovec  iov;
101         struct msghdr msg;
102         struct cmsghdr *cmsg;
103         struct sock_extended_err *e;
104         struct sockaddr_storage addr;
105         struct timeval tv;
106         struct timeval *rettv;
107         int slot = 0;
108         int rethops;
109         int sndhops;
110         int progress = -1;
111         int broken_router;
112
113 restart:
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);
119         msg.msg_iov = &iov;
120         msg.msg_iovlen = 1;
121         msg.msg_flags = 0;
122         msg.msg_control = cbuf;
123         msg.msg_controllen = sizeof(cbuf);
124
125         gettimeofday(&tv, NULL);
126         res = recvmsg(fd, &msg, MSG_ERRQUEUE);
127         if (res < 0) {
128                 if (errno == EAGAIN)
129                         return progress;
130                 goto restart;
131         }
132
133         progress = mtu;
134
135         rethops = -1;
136         sndhops = -1;
137         e = NULL;
138         rettv = NULL;
139
140         slot = -base_port;
141         switch (family) {
142         case AF_INET6:
143                 slot += ntohs(((struct sockaddr_in6 *)&addr)->sin6_port);
144                 break;
145         case AF_INET:
146                 slot += ntohs(((struct sockaddr_in *)&addr)->sin_port);
147                 break;
148         }
149
150         if (slot >= 0 && slot < 63 && his[slot].hops) {
151                 sndhops = his[slot].hops;
152                 rettv = &his[slot].sendtime;
153                 his[slot].hops = 0;
154         }
155         broken_router = 0;
156         if (res == sizeof(rcvbuf)) {
157                 if (rcvbuf.ttl == 0 || rcvbuf.tv.tv_sec == 0)
158                         broken_router = 1;
159                 else {
160                         sndhops = rcvbuf.ttl;
161                         rettv = &rcvbuf.tv;
162                 }
163         }
164
165         for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
166                 switch (cmsg->cmsg_level) {
167                 case SOL_IPV6:
168                         switch(cmsg->cmsg_type) {
169                         case IPV6_RECVERR:
170                                 e = (struct sock_extended_err *)CMSG_DATA(cmsg);
171                                 break;
172                         case IPV6_HOPLIMIT:
173 #ifdef IPV6_2292HOPLIMIT
174                         case IPV6_2292HOPLIMIT:
175 #endif
176                                 rethops = *(int*)CMSG_DATA(cmsg);
177                                 break;
178                         default:
179                                 printf("cmsg6:%d\n ", cmsg->cmsg_type);
180                         }
181                         break;
182                 case SOL_IP:
183                         switch(cmsg->cmsg_type) {
184                         case IP_RECVERR:
185                                 e = (struct sock_extended_err *)CMSG_DATA(cmsg);
186                                 break;
187                         case IP_TTL:
188                                 rethops = *(__u8*)CMSG_DATA(cmsg);
189                                 break;
190                         default:
191                                 printf("cmsg4:%d\n ", cmsg->cmsg_type);
192                         }
193                 }
194         }
195         if (e == NULL) {
196                 printf("no info\n");
197                 return 0;
198         }
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);
205                 socklen_t salen;
206
207                 if (sndhops>0)
208                         printf("%2d:  ", sndhops);
209                 else
210                         printf("%2d?: ", ttl);
211
212                 switch (sa->sa_family) {
213                 case AF_INET6:
214                         salen = sizeof(struct sockaddr_in6);
215                         break;
216                 case AF_INET:
217                         salen = sizeof(struct sockaddr_in);
218                         break;
219                 default:
220                         salen = 0;
221                 }
222
223                 if (no_resolve || show_both) {
224                         if (getnameinfo(sa, salen,
225                                         abuf, sizeof(abuf), NULL, 0,
226                                         NI_NUMERICHOST))
227                                 strcpy(abuf, "???");
228                 } else
229                         abuf[0] = 0;
230
231                 if (!no_resolve || show_both) {
232                         fflush(stdout);
233                         if (getnameinfo(sa, salen,
234                                         hbuf, sizeof(hbuf), NULL, 0,
235                                         0))
236                                 strcpy(hbuf, "???");
237                 } else
238                         hbuf[0] = 0;
239
240                 if (no_resolve)
241                         print_host(abuf, hbuf, show_both);
242                 else
243                         print_host(hbuf, abuf, show_both);
244         }
245
246         if (rettv) {
247                 int diff = (tv.tv_sec-rettv->tv_sec)*1000000+(tv.tv_usec-rettv->tv_usec);
248                 printf("%3d.%03dms ", diff/1000, diff%1000);
249                 if (broken_router)
250                         printf("(This broken router returned corrupted payload) ");
251         }
252
253         switch (e->ee_errno) {
254         case ETIMEDOUT:
255                 printf("\n");
256                 break;
257         case EMSGSIZE:
258                 printf("pmtu %d\n", e->ee_info);
259                 mtu = e->ee_info;
260                 progress = mtu;
261                 break;
262         case ECONNREFUSED:
263                 printf("reached\n");
264                 hops_to = sndhops<0 ? ttl : sndhops;
265                 hops_from = rethops;
266                 return 0;
267         case EPROTO:
268                 printf("!P\n");
269                 return 0;
270         case EHOSTUNREACH:
271                 if ((e->ee_origin == SO_EE_ORIGIN_ICMP &&
272                      e->ee_type == 11 &&
273                      e->ee_code == 0) ||
274                     (e->ee_origin == SO_EE_ORIGIN_ICMP6 &&
275                      e->ee_type == 3 &&
276                      e->ee_code == 0)) {
277                         if (rethops>=0) {
278                                 if (rethops<=64)
279                                         rethops = 65-rethops;
280                                 else if (rethops<=128)
281                                         rethops = 129-rethops;
282                                 else
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);
288                         }
289                         printf("\n");
290                         break;
291                 }
292                 printf("!H\n");
293                 return 0;
294         case ENETUNREACH:
295                 printf("!N\n");
296                 return 0;
297         case EACCES:
298                 printf("!A\n");
299                 return 0;
300         default:
301                 printf("\n");
302                 errno = e->ee_errno;
303                 perror("NET ERROR");
304                 return 0;
305         }
306         goto restart;
307 }
308
309 int probe_ttl(int fd, int ttl)
310 {
311         int i;
312         char sndbuf[mtu];
313         struct probehdr *hdr = (struct probehdr*)sndbuf;
314
315         memset(sndbuf, 0, mtu);
316
317 restart:
318
319         for (i=0; i<10; i++) {
320                 int res;
321
322                 hdr->ttl = ttl;
323                 switch (family) {
324                 case AF_INET6:
325                         ((struct sockaddr_in6 *)&target)->sin6_port = htons(base_port + hisptr);
326                         break;
327                 case AF_INET:
328                         ((struct sockaddr_in *)&target)->sin_port = htons(base_port + hisptr);
329                         break;
330                 }
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)
335                         break;
336                 res = recverr(fd, ttl);
337                 his[hisptr].hops = 0;
338                 if (res==0)
339                         return 0;
340                 if (res > 0)
341                         goto restart;
342         }
343         hisptr = (hisptr + 1) & 63;
344
345         if (i<10) {
346                 data_wait(fd);
347                 if (recv(fd, sndbuf, sizeof(sndbuf), MSG_DONTWAIT) > 0) {
348                         printf("%2d?: reply received 8)\n", ttl);
349                         return 0;
350                 }
351                 return recverr(fd, ttl);
352         }
353
354         printf("%2d:  send failed\n", ttl);
355         return 0;
356 }
357
358 static void usage(void) __attribute((noreturn));
359
360 static void usage(void)
361 {
362         fprintf(stderr, "Usage: tracepath6 [-n] [-b] [-l <len>] <destination>[/<port>]\n");
363         exit(-1);
364 }
365
366
367 int main(int argc, char **argv)
368 {
369         int fd;
370         int on;
371         int ttl;
372         char *p;
373         struct addrinfo hints, *ai, *ai0;
374         int ch;
375         int gai;
376         char pbuf[NI_MAXSERV];
377
378         while ((ch = getopt(argc, argv, "nbh?l:")) != EOF) {
379                 switch(ch) {
380                 case 'n':
381                         no_resolve = 1;
382                         break;
383                 case 'b':
384                         show_both = 1;
385                         break;
386                 case 'l':
387                         if ((mtu = atoi(optarg)) <= overhead) {
388                                 fprintf(stderr, "Error: length must be >= %d\n", overhead);
389                                 exit(1);
390                         }
391                         break;
392                 default:
393                         usage();
394                 }
395         }
396
397         argc -= optind;
398         argv += optind;
399
400         if (argc != 1)
401                 usage();
402
403         p = strchr(argv[0], '/');
404         if (p) {
405                 *p = 0;
406                 base_port = (unsigned)atoi(p+1);
407         } else {
408                 base_port = 44444;
409         }
410         sprintf(pbuf, "%u", base_port);
411
412         memset(&hints, 0, sizeof(hints));
413         hints.ai_family = family;
414         hints.ai_socktype = SOCK_DGRAM;
415         hints.ai_protocol = IPPROTO_UDP;
416         hints.ai_flags = 0;
417         gai = getaddrinfo(argv[0], pbuf, &hints, &ai0);
418         if (gai) {
419                 herror("getaddrinfo");  /*XXX*/
420                 exit(1);
421         }
422
423         fd = -1;
424         for (ai = ai0; ai; ai = ai->ai_next) {
425                 /* sanity check */
426                 if (family && ai->ai_family != family)
427                         continue;
428                 family = ai->ai_family;
429                 fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
430                 if (fd < 0)
431                         continue;
432                 memcpy(&target, ai->ai_addr, sizeof(target));
433                 targetlen = ai->ai_addrlen;
434                 break;
435         }
436         if (fd < 0) {
437                 perror("socket/connect");
438                 exit(1);
439         }
440         freeaddrinfo(ai0);
441
442         switch (family) {
443         case AF_INET6:
444                 mtu = 128000;
445                 overhead = 48;
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");
451                         exit(1);
452                 }
453                 on = 1;
454                 if (setsockopt(fd, SOL_IPV6, IPV6_RECVERR, &on, sizeof(on))) {
455                         perror("IPV6_RECVERR");
456                         exit(1);
457                 }
458                 if (
459 #ifdef IPV6_RECVHOPLIMIT
460                     setsockopt(fd, SOL_IPV6, IPV6_HOPLIMIT, &on, sizeof(on)) &&
461                     setsockopt(fd, SOL_IPV6, IPV6_2292HOPLIMIT, &on, sizeof(on))
462 #else
463                     setsockopt(fd, SOL_IPV6, IPV6_HOPLIMIT, &on, sizeof(on))
464 #endif
465                     ) {
466                         perror("IPV6_HOPLIMIT");
467                         exit(1);
468                 }
469                 if (!IN6_IS_ADDR_V4MAPPED(&(((struct sockaddr_in6 *)&target)->sin6_addr)))
470                         break;
471                 mapped = 1;
472                 /*FALLTHROUGH*/
473         case AF_INET:
474                 mtu = 65535;
475                 overhead = 28;
476                 on = IP_PMTUDISC_DO;
477                 if (setsockopt(fd, SOL_IP, IP_MTU_DISCOVER, &on, sizeof(on))) {
478                         perror("IP_MTU_DISCOVER");
479                         exit(1);
480                 }
481                 on = 1;
482                 if (setsockopt(fd, SOL_IP, IP_RECVERR, &on, sizeof(on))) {
483                         perror("IP_RECVERR");
484                         exit(1);
485                 }
486                 if (setsockopt(fd, SOL_IP, IP_RECVTTL, &on, sizeof(on))) {
487                         perror("IP_RECVTTL");
488                         exit(1);
489                 }
490         }
491
492         for (ttl=1; ttl<32; ttl++) {
493                 int res;
494                 int i;
495
496                 on = ttl;
497                 switch (family) {
498                 case AF_INET6:
499                         if (setsockopt(fd, SOL_IPV6, IPV6_UNICAST_HOPS, &on, sizeof(on))) {
500                                 perror("IPV6_UNICAST_HOPS");
501                                 exit(1);
502                         }
503                         if (!mapped)
504                                 break;
505                         /*FALLTHROUGH*/
506                 case AF_INET:
507                         if (setsockopt(fd, SOL_IP, IP_TTL, &on, sizeof(on))) {
508                                 perror("IP_TTL");
509                                 exit(1);
510                         }
511                 }
512
513 restart:
514                 for (i=0; i<3; i++) {
515                         int old_mtu;
516
517                         old_mtu = mtu;
518                         res = probe_ttl(fd, ttl);
519                         if (mtu != old_mtu)
520                                 goto restart;
521                         if (res == 0)
522                                 goto done;
523                         if (res > 0)
524                                 break;
525                 }
526
527                 if (res < 0)
528                         printf("%2d:  no reply\n", ttl);
529         }
530         printf("     Too many hops: pmtu %d\n", mtu);
531
532 done:
533         printf("     Resume: pmtu %d ", mtu);
534         if (hops_to>=0)
535                 printf("hops %d ", hops_to);
536         if (hops_from>=0)
537                 printf("back %d ", hops_from);
538         printf("\n");
539         exit(0);
540 }