forked from iputils/iputils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracepath.c
624 lines (559 loc) · 14.1 KB
/
tracepath.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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
/*
* tracepath.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]>
*/
#define _GNU_SOURCE
#include <assert.h>
#include <arpa/inet.h>
#include <errno.h>
#include <limits.h>
#include <netdb.h>
#include <netinet/in.h>
#include <resolv.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/uio.h>
#include <time.h>
#include <unistd.h>
/*
* Keep linux/ includes after standard headers.
* https://github.com/iputils/iputils/issues/168
*/
#include <linux/errqueue.h>
#include <linux/icmp.h>
#include <linux/icmpv6.h>
#include <linux/types.h>
#include "iputils_common.h"
#ifdef USE_IDN
# define getnameinfo_flags NI_IDN
#else
# define getnameinfo_flags 0
#endif
enum {
MAX_PROBES = 10,
MAX_HOPS_DEFAULT = 30,
MAX_HOPS_LIMIT = 255,
HOST_COLUMN_SIZE = 52,
HIS_ARRAY_SIZE = 64,
DEFAULT_OVERHEAD_IPV4 = 28,
DEFAULT_OVERHEAD_IPV6 = 48,
DEFAULT_MTU_IPV4 = 65535,
DEFAULT_MTU_IPV6 = 128000,
DEFAULT_BASEPORT = 44444,
ANCILLARY_DATA_LEN = 512,
};
struct hhistory {
int hops;
struct timespec sendtime;
};
struct probehdr {
uint32_t ttl;
struct timespec ts;
};
struct run_state {
struct hhistory his[HIS_ARRAY_SIZE];
int hisptr;
struct sockaddr_storage target;
struct addrinfo *ai;
int socket_fd;
socklen_t targetlen;
uint16_t base_port;
uint8_t ttl;
int max_hops;
int overhead;
int mtu;
void *pktbuf;
int hops_to;
int hops_from;
unsigned int
no_resolve:1,
show_both:1,
mapped:1;
};
/*
* All includes, definitions, struct declarations, and global variables are
* above. After this comment all you can find is functions.
*/
static void data_wait(struct run_state const *const ctl)
{
fd_set fds;
struct timeval tv = {
.tv_sec = 1,
.tv_usec = 0
};
FD_ZERO(&fds);
FD_SET(ctl->socket_fd, &fds);
select(ctl->socket_fd + 1, &fds, NULL, NULL, &tv);
}
static void print_host(struct run_state const *const ctl, char const *const a,
char const *const b)
{
int plen;
plen = printf("%s", a);
if (ctl->show_both)
plen += printf(" (%s)", b);
if (plen >= HOST_COLUMN_SIZE)
plen = HOST_COLUMN_SIZE - 1;
printf("%*s", HOST_COLUMN_SIZE - plen, "");
}
static int recverr(struct run_state *const ctl)
{
ssize_t recv_size;
struct probehdr rcvbuf;
char cbuf[ANCILLARY_DATA_LEN];
struct cmsghdr *cmsg;
struct sock_extended_err *e;
struct sockaddr_storage addr;
struct timespec ts;
struct timespec *retts;
int slot = 0;
int rethops;
int sndhops;
int progress = -1;
int broken_router;
char hnamebuf[NI_MAXHOST] = "";
struct iovec iov = {
.iov_base = &rcvbuf,
.iov_len = sizeof(rcvbuf)
};
struct msghdr msg;
const struct msghdr reset = {
.msg_name = (uint8_t *)&addr,
.msg_namelen = sizeof(addr),
.msg_iov = &iov,
.msg_iovlen = 1,
.msg_control = cbuf,
.msg_controllen = sizeof(cbuf),
0
};
restart:
memset(&rcvbuf, -1, sizeof(rcvbuf));
msg = reset;
clock_gettime(CLOCK_MONOTONIC, &ts);
recv_size = recvmsg(ctl->socket_fd, &msg, MSG_ERRQUEUE);
if (recv_size < 0) {
if (errno == EAGAIN)
return progress;
goto restart;
}
progress = ctl->mtu;
rethops = -1;
sndhops = -1;
e = NULL;
retts = NULL;
broken_router = 0;
slot = -ctl->base_port;
switch (ctl->ai->ai_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 < (HIS_ARRAY_SIZE - 1) && ctl->his[slot].hops) {
sndhops = ctl->his[slot].hops;
retts = &ctl->his[slot].sendtime;
ctl->his[slot].hops = 0;
}
if (recv_size == sizeof(rcvbuf)) {
if (rcvbuf.ttl == 0 || (rcvbuf.ts.tv_sec == 0 && rcvbuf.ts.tv_nsec == 0))
broken_router = 1;
else {
sndhops = rcvbuf.ttl;
retts = &rcvbuf.ts;
}
}
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 = *(uint8_t *)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 ", ctl->ttl, _("[LOCALHOST]"));
else if (e->ee_origin == SO_EE_ORIGIN_ICMP6 ||
e->ee_origin == SO_EE_ORIGIN_ICMP) {
char abuf[NI_MAXHOST];
struct sockaddr *sa = (struct sockaddr *)(e + 1);
socklen_t salen;
if (sndhops > 0)
printf("%2d: ", sndhops);
else
printf("%2d?: ", ctl->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 (ctl->no_resolve || ctl->show_both) {
if (getnameinfo(sa, salen, abuf, sizeof(abuf), NULL, 0,
NI_NUMERICHOST))
strcpy(abuf, "???");
} else
abuf[0] = 0;
if (!ctl->no_resolve || ctl->show_both) {
fflush(stdout);
if (getnameinfo(sa, salen, hnamebuf, sizeof hnamebuf, NULL, 0,
getnameinfo_flags))
strcpy(hnamebuf, "???");
} else
hnamebuf[0] = 0;
if (ctl->no_resolve)
print_host(ctl, abuf, hnamebuf);
else
print_host(ctl, hnamebuf, abuf);
}
if (retts) {
struct timespec res;
timespecsub(&ts, retts, &res);
printf(_("%3lld.%03ldms "), (long long int)res.tv_sec * 1000
+ res.tv_nsec / 1000000, (res.tv_nsec % 1000000) / 1000);
if (broken_router)
printf(_("(This broken router returned corrupted payload) "));
}
if (rethops <= 64)
rethops = 65 - rethops;
else if (rethops <= 128)
rethops = 129 - rethops;
else
rethops = 256 - rethops;
switch (e->ee_errno) {
case ETIMEDOUT:
printf("\n");
break;
case EMSGSIZE:
printf(_("pmtu %d\n"), e->ee_info);
ctl->mtu = e->ee_info;
progress = ctl->mtu;
break;
case ECONNREFUSED:
printf(_("reached\n"));
ctl->hops_to = sndhops < 0 ? ctl->ttl : sndhops;
ctl->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 == ICMP_TIME_EXCEEDED &&
e->ee_code == ICMP_EXC_TTL) ||
(e->ee_origin == SO_EE_ORIGIN_ICMP6 &&
e->ee_type == ICMPV6_TIME_EXCEED &&
e->ee_code == ICMPV6_EXC_HOPLIMIT)) {
if (rethops >= 0) {
if ((sndhops >= 0 && rethops != sndhops) ||
(sndhops < 0 && rethops != ctl->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");
error(0, e->ee_errno, _("NET ERROR"));
return 0;
}
goto restart;
}
static int probe_ttl(struct run_state *const ctl)
{
int i;
struct probehdr *hdr = ctl->pktbuf;
memset(ctl->pktbuf, 0, ctl->mtu);
restart:
for (i = 0; i < MAX_PROBES; i++) {
int res;
hdr->ttl = ctl->ttl;
switch (ctl->ai->ai_family) {
case AF_INET6:
((struct sockaddr_in6 *)&ctl->target)->sin6_port =
htons(ctl->base_port + ctl->hisptr);
break;
case AF_INET:
((struct sockaddr_in *)&ctl->target)->sin_port =
htons(ctl->base_port + ctl->hisptr);
break;
}
clock_gettime(CLOCK_MONOTONIC, &hdr->ts);
ctl->his[ctl->hisptr].hops = ctl->ttl;
ctl->his[ctl->hisptr].sendtime = hdr->ts;
if (sendto(ctl->socket_fd, ctl->pktbuf, ctl->mtu - ctl->overhead, 0,
(struct sockaddr *)&ctl->target, ctl->targetlen) > 0)
break;
res = recverr(ctl);
ctl->his[ctl->hisptr].hops = 0;
if (res == 0)
return 0;
if (res > 0)
goto restart;
}
ctl->hisptr = (ctl->hisptr + 1) & (HIS_ARRAY_SIZE - 1);
if (i < MAX_PROBES) {
data_wait(ctl);
if (recv(ctl->socket_fd, ctl->pktbuf, ctl->mtu, MSG_DONTWAIT) > 0) {
printf(_("%2d?: reply received 8)\n"), ctl->ttl);
return 0;
}
return recverr(ctl);
}
printf(_("%2d: send failed\n"), ctl->ttl);
return 0;
}
static void usage(void)
{
fprintf(stderr, _(
"\nUsage\n"
" tracepath [options] <destination>\n"
"\nOptions:\n"
" -4 use IPv4\n"
" -6 use IPv6\n"
" -b print both name and IP\n"
" -l <length> use packet <length>\n"
" -m <hops> use maximum <hops>\n"
" -n no reverse DNS name resolution\n"
" -p <port> use destination <port>\n"
" -V print version and exit\n"
" <destination> DNS name or IP address\n"
"\nFor more details see tracepath(8).\n"));
exit(-1);
}
int main(int argc, char **argv)
{
struct run_state ctl = {
.max_hops = MAX_HOPS_DEFAULT,
.hops_to = -1,
.hops_from = -1,
0
};
struct addrinfo hints = {
.ai_family = AF_UNSPEC,
.ai_socktype = SOCK_DGRAM,
.ai_protocol = IPPROTO_UDP,
#ifdef USE_IDN
.ai_flags = AI_IDN | AI_CANONNAME,
#endif
};
struct addrinfo *result;
int ch;
int status;
int on;
char *p;
char pbuf[NI_MAXSERV];
atexit(close_stdout);
#if defined(USE_IDN) || defined(ENABLE_NLS)
setlocale(LC_ALL, "");
#ifdef ENABLE_NLS
bindtextdomain (PACKAGE_NAME, LOCALEDIR);
textdomain (PACKAGE_NAME);
#endif
#endif
/* Support being called using `tracepath4` or `tracepath6` symlinks */
if (argv[0][strlen(argv[0]) - 1] == '4')
hints.ai_family = AF_INET;
else if (argv[0][strlen(argv[0]) - 1] == '6')
hints.ai_family = AF_INET6;
while ((ch = getopt(argc, argv, "46nbh?l:m:p:V")) != EOF) {
switch (ch) {
case '4':
if (hints.ai_family == AF_INET6)
error(2, 0, _("Only one -4 or -6 option may be specified"));
hints.ai_family = AF_INET;
break;
case '6':
if (hints.ai_family == AF_INET)
error(2, 0, _("Only one -4 or -6 option may be specified"));
hints.ai_family = AF_INET6;
break;
case 'n':
ctl.no_resolve = 1;
break;
case 'b':
ctl.show_both = 1;
break;
case 'l':
ctl.mtu = strtol_or_err(optarg, _("invalid argument"), ctl.overhead, INT_MAX);
break;
case 'm':
ctl.max_hops = strtol_or_err(optarg, _("invalid argument"), 0, MAX_HOPS_LIMIT);
break;
case 'p':
ctl.base_port = strtol_or_err(optarg, _("invalid argument"), 0, UINT16_MAX);
break;
case 'V':
printf(IPUTILS_VERSION("tracepath"));
print_config();
return 0;
default:
usage();
}
}
argc -= optind;
argv += optind;
if (argc != 1)
usage();
/* Backward compatibility */
if (!ctl.base_port) {
p = strchr(argv[0], '/');
if (p) {
*p = 0;
ctl.base_port = strtol_or_err(p + 1, _("invalid argument"), 0, UINT16_MAX);
} else
ctl.base_port = DEFAULT_BASEPORT;
}
sprintf(pbuf, "%u", ctl.base_port);
status = getaddrinfo(argv[0], pbuf, &hints, &result);
if (status || !result) {
error(1, 0, "%s: %s", argv[0], gai_strerror(status));
abort();
}
for (ctl.ai = result; ctl.ai; ctl.ai = ctl.ai->ai_next) {
assert(ctl.ai != NULL);
if (ctl.ai->ai_family != AF_INET6 && ctl.ai->ai_family != AF_INET)
continue;
ctl.socket_fd = socket(ctl.ai->ai_family, ctl.ai->ai_socktype, ctl.ai->ai_protocol);
if (ctl.socket_fd < 0)
continue;
memcpy(&ctl.target, ctl.ai->ai_addr, ctl.ai->ai_addrlen);
ctl.targetlen = ctl.ai->ai_addrlen;
break;
}
if (ctl.socket_fd < 0)
error(1, errno, "socket/connect");
switch (ctl.ai->ai_family) {
case AF_INET6:
ctl.overhead = DEFAULT_OVERHEAD_IPV6;
if (!ctl.mtu)
ctl.mtu = DEFAULT_MTU_IPV6;
if (ctl.mtu <= ctl.overhead)
goto pktlen_error;
on = IPV6_PMTUDISC_PROBE;
if (setsockopt(ctl.socket_fd, SOL_IPV6, IPV6_MTU_DISCOVER, &on, sizeof(on)) &&
(on = IPV6_PMTUDISC_DO, setsockopt(ctl.socket_fd, SOL_IPV6,
IPV6_MTU_DISCOVER, &on, sizeof(on))))
error(1, errno, "IPV6_MTU_DISCOVER");
on = 1;
if (setsockopt(ctl.socket_fd, SOL_IPV6, IPV6_RECVERR, &on, sizeof(on)))
error(1, errno, "IPV6_RECVERR");
if (setsockopt(ctl.socket_fd, SOL_IPV6, IPV6_HOPLIMIT, &on, sizeof(on))
#ifdef IPV6_RECVHOPLIMIT
&& setsockopt(ctl.socket_fd, SOL_IPV6, IPV6_2292HOPLIMIT, &on, sizeof(on))
#endif
)
error(1, errno, "IPV6_HOPLIMIT");
if (!IN6_IS_ADDR_V4MAPPED(&(((struct sockaddr_in6 *)&ctl.target)->sin6_addr)))
break;
ctl.mapped = 1;
/*FALLTHROUGH*/
case AF_INET:
ctl.overhead = DEFAULT_OVERHEAD_IPV4;
if (!ctl.mtu)
ctl.mtu = DEFAULT_MTU_IPV4;
if (ctl.mtu <= ctl.overhead)
goto pktlen_error;
on = IP_PMTUDISC_PROBE;
if (setsockopt(ctl.socket_fd, SOL_IP, IP_MTU_DISCOVER, &on, sizeof(on)))
error(1, errno, "IP_MTU_DISCOVER");
on = 1;
if (setsockopt(ctl.socket_fd, SOL_IP, IP_RECVERR, &on, sizeof(on)))
error(1, errno, "IP_RECVERR");
if (setsockopt(ctl.socket_fd, SOL_IP, IP_RECVTTL, &on, sizeof(on)))
error(1, errno, "IP_RECVTTL");
}
ctl.pktbuf = malloc(ctl.mtu);
if (!ctl.pktbuf)
error(1, errno, "malloc");
for (ctl.ttl = 1; ctl.ttl <= ctl.max_hops; ctl.ttl++) {
int res = -1;
int i;
on = ctl.ttl;
switch (ctl.ai->ai_family) {
case AF_INET6:
if (setsockopt(ctl.socket_fd, SOL_IPV6, IPV6_UNICAST_HOPS, &on, sizeof(on)))
error(1, errno, "IPV6_UNICAST_HOPS");
if (!ctl.mapped)
break;
/*FALLTHROUGH*/
case AF_INET:
if (setsockopt(ctl.socket_fd, SOL_IP, IP_TTL, &on, sizeof(on)))
error(1, errno, "IP_TTL");
}
restart:
for (i = 0; i < 3; i++) {
int old_mtu;
old_mtu = ctl.mtu;
res = probe_ttl(&ctl);
if (ctl.mtu != old_mtu)
goto restart;
if (res == 0)
goto done;
if (res > 0)
break;
}
if (res < 0)
printf(_("%2d: no reply\n"), ctl.ttl);
}
printf(" Too many hops: pmtu %d\n", ctl.mtu);
done:
freeaddrinfo(result);
printf(_(" Resume: pmtu %d "), ctl.mtu);
if (ctl.hops_to >= 0)
printf(_("hops %d "), ctl.hops_to);
if (ctl.hops_from >= 0)
printf(_("back %d "), ctl.hops_from);
printf("\n");
exit(0);
pktlen_error:
error(1, 0, _("pktlen must be within: %d < value <= %d"), ctl.overhead, INT_MAX);
}