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