1 /* 2 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 3 * Use is subject to license terms. 4 * Copyright (c) 2017, Joyent, Inc. 5 */ 6 7 /* 8 * Copyright (c) 1988, 1989, 1991, 1994, 1995, 1996, 1997 9 * The Regents of the University of California. All rights reserved. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that: (1) source code distributions 13 * retain the above copyright notice and this paragraph in its entirety, (2) 14 * distributions including binary code include the above copyright notice and 15 * this paragraph in its entirety in the documentation or other materials 16 * provided with the distribution, and (3) all advertising materials mentioning 17 * features or use of this software display the following acknowledgement: 18 * ``This product includes software developed by the University of California, 19 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 20 * the University nor the names of its contributors may be used to endorse 21 * or promote products derived from this software without specific prior 22 * written permission. 23 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 24 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 26 * 27 * 28 * @(#)$Header: traceroute.c,v 1.49 97/06/13 02:30:23 leres Exp $ (LBL) 29 */ 30 31 #include <sys/param.h> 32 #include <sys/file.h> 33 #include <sys/ioctl.h> 34 #include <sys/socket.h> 35 #include <sys/time.h> 36 #include <sys/sysmacros.h> 37 38 #include <netinet/in_systm.h> 39 #include <netinet/in.h> 40 #include <netinet/ip.h> 41 #include <netinet/ip_var.h> 42 #include <netinet/ip_icmp.h> 43 #include <netinet/udp.h> 44 #include <netinet/udp_var.h> 45 #include <netinet/ip6.h> 46 #include <netinet/icmp6.h> 47 48 #include <arpa/inet.h> 49 50 #include <ctype.h> 51 #include <errno.h> 52 #include <malloc.h> 53 #include <memory.h> 54 #include <netdb.h> 55 #include <stdio.h> 56 #include <stdlib.h> 57 #include <strings.h> 58 #include <unistd.h> 59 #include <libintl.h> 60 #include <locale.h> 61 #include <signal.h> 62 #include <setjmp.h> 63 #include <limits.h> 64 #include <zone.h> 65 #include <thread.h> 66 #include <synch.h> 67 68 #include <priv_utils.h> 69 70 #include <libinetutil.h> 71 #include "traceroute.h" 72 73 #define MAX_SEQ 65535 /* max sequence value for ICMP */ 74 #define MAX_TRAFFIC_CLASS 255 /* max traffic class for IPv6 */ 75 #define MAX_FLOW_LABEL 0xFFFFF /* max flow label for IPv6 */ 76 #define MAX_TOS 255 /* max type-of-service for IPv4 */ 77 #define STR_LEN 30 78 79 /* store the information about a host */ 80 struct hostinfo { 81 char *name; /* hostname */ 82 int family; /* address family of the IP addresses */ 83 int num_addr; /* number of IP addresses */ 84 union any_in_addr *addrs; /* list of IP addresses */ 85 }; 86 87 /* used to store a bunch of protocol specific values */ 88 struct pr_set { 89 int family; /* AF_INET or AF_INET6 */ 90 char name[STR_LEN]; /* "IPv4" or "IPv6" */ 91 char icmp[STR_LEN]; /* "icmp" or "ipv6-icmp" */ 92 int icmp_minlen; 93 int addr_len; 94 int ip_hdr_len; 95 int packlen; 96 int sock_size; /* size of sockaddr_in or sockaddr_in6 */ 97 struct sockaddr *to; 98 struct sockaddr *from; 99 void *from_sin_addr; 100 union any_in_addr *gwIPlist; 101 /* pointers to v4/v6 functions */ 102 struct ip *(*set_buffers_fn) (int); 103 int (*check_reply_fn)(struct msghdr *, int, int, uchar_t *, uchar_t *); 104 boolean_t (*print_icmp_other_fn)(uchar_t, uchar_t); 105 void (*print_addr_fn)(uchar_t *, int, struct sockaddr *); 106 107 }; 108 109 /* 110 * LBNL bug fixed: in LBNL traceroute 'uchar_t packet[512];' 111 * Not sufficient to hold the complete packet for ECHO REPLY of a big probe. 112 * Packet size is reported incorrectly in such a case. 113 * Also this buffer needs to be 32 bit aligned. In the future the alignment 114 * requirement will be increased to 64 bit. So, let's use 64 bit alignment now. 115 */ 116 static uint64_t packet[(IP_MAXPACKET + 1)/8]; /* received packet */ 117 118 static struct ip *outip4; /* output buffer to send as an IPv4 datagram */ 119 static struct ip *outip6; /* output buffer to send as an IPv6 datagram */ 120 121 /* Used to store the ancillary data that comes with the received packets */ 122 static uint64_t ancillary_data[(IP_MAXPACKET + 1)/8]; 123 124 /* first get the gw names, later you'll resolve them based on the family */ 125 static char *gwlist[MAXMAX_GWS]; /* gateway names list */ 126 static union any_in_addr gwIPlist[MAX_GWS]; /* gateway IPv4 address list */ 127 static union any_in_addr gwIP6list[MAX_GWS6]; /* gateway IPv6 address list */ 128 129 static int family_input = AF_UNSPEC; /* User supplied protocol family */ 130 static int rcvsock4; /* receive (icmp) socket file descriptor */ 131 static int sndsock4; /* send (udp/icmp) socket file descriptor */ 132 static int rcvsock6; /* receive (icmp6) socket file descriptor */ 133 static int sndsock6; /* send (udp6/icmp6) socket file descriptor */ 134 int gw_count = 0; /* number of gateways */ 135 static struct sockaddr_in whereto; /* Who to try to reach */ 136 static struct sockaddr_in6 whereto6; 137 static struct sockaddr_in wherefrom; /* Who we are */ 138 static struct sockaddr_in6 wherefrom6; 139 static int packlen_input = 0; /* user input for packlen */ 140 141 char *prog; 142 static char *source_input = NULL; /* this is user arg. source, doesn't change */ 143 static char *source = NULL; /* this gets modified after name lookup */ 144 char *hostname; 145 static char *device = NULL; /* interface name */ 146 static struct pr_set *pr4; /* protocol info for IPv4 */ 147 static struct pr_set *pr6; /* protocol info for IPv6 */ 148 static struct ifaddrlist *al4; /* list of interfaces */ 149 static struct ifaddrlist *al6; /* list of interfaces */ 150 static uint_t if_index = 0; /* interface index */ 151 static int num_v4 = 0; /* count of IPv4 addresses */ 152 static int num_v6 = 0; /* count of IPv6 addresses */ 153 static int num_ifs4 = 0; /* count of local IPv4 interfaces */ 154 static int num_ifs6 = 0; /* count of local IPv6 interfaces */ 155 156 static int nprobes = 3; /* number of probes */ 157 static int max_ttl = 30; /* max number of hops */ 158 static int first_ttl = 1; /* initial number of hops */ 159 ushort_t ident; /* used to authenticate replies */ 160 ushort_t port = 32768 + 666; /* start udp dest port # for probe packets */ 161 162 static int options = 0; /* socket options */ 163 boolean_t verbose = _B_FALSE; /* verbose output */ 164 static int waittime = 5; /* time to wait for response (in seconds) */ 165 static struct timeval delay = {0, 0}; /* delay between consecutive probe */ 166 boolean_t nflag = _B_FALSE; /* print addresses numerically */ 167 static boolean_t showttl = _B_FALSE; /* print the ttl(hop limit) of recvd pkt */ 168 boolean_t useicmp = _B_FALSE; /* use icmp echo instead of udp packets */ 169 boolean_t docksum = _B_TRUE; /* calculate checksums */ 170 static boolean_t collect_stat = _B_FALSE; /* print statistics */ 171 boolean_t settos = _B_FALSE; /* set type-of-service field */ 172 int dontfrag = 0; /* IP*_DONTFRAG */ 173 static int max_timeout = 5; /* quit after this consecutive timeouts */ 174 static boolean_t probe_all = _B_FALSE; /* probe all the IFs of the target */ 175 static boolean_t pick_src = _B_FALSE; /* traceroute picks the src address */ 176 177 /* 178 * flow and class are specific to IPv6, tos and off are specific to IPv4. 179 * Each protocol uses the ones that are specific to itself, and ignores 180 * others. 181 */ 182 static uint_t flow = 0; /* IPv6 flow info */ 183 static uint_t class = 0; /* IPv6 class */ 184 uchar_t tos = 0; /* IPv4 type-of-service */ 185 ushort_t off = 0; /* set DF bit */ 186 187 static jmp_buf env; /* stack environment for longjmp() */ 188 boolean_t raw_req; /* if sndsock for IPv4 must be raw */ 189 190 /* 191 * Name service lookup related data. 192 */ 193 static mutex_t tr_nslock = ERRORCHECKMUTEX; 194 static boolean_t tr_nsactive = _B_FALSE; /* Lookup ongoing */ 195 static hrtime_t tr_nsstarttime; /* Start time */ 196 static int tr_nssleeptime = 2; /* Interval between checks */ 197 static int tr_nswarntime = 2; /* Interval to warn after */ 198 199 /* Forwards */ 200 static uint_t calc_packetlen(int, struct pr_set *); 201 extern int check_reply(struct msghdr *, int, int, uchar_t *, uchar_t *); 202 extern int check_reply6(struct msghdr *, int, int, uchar_t *, uchar_t *); 203 static double deltaT(struct timeval *, struct timeval *); 204 static char *device_name(struct ifaddrlist *, int, union any_in_addr *, 205 struct pr_set *); 206 extern void *find_ancillary_data(struct msghdr *, int, int); 207 static boolean_t has_addr(struct addrinfo *, union any_in_addr *); 208 static struct ifaddrlist *find_device(struct ifaddrlist *, int, char *); 209 static struct ifaddrlist *find_ifaddr(struct ifaddrlist *, int, 210 union any_in_addr *, int); 211 static void get_gwaddrs(char **, int, union any_in_addr *, 212 union any_in_addr *, int *, int *); 213 static void get_hostinfo(char *, int, struct addrinfo **); 214 char *inet_name(union any_in_addr *, int); 215 ushort_t in_cksum(ushort_t *, int); 216 extern int ip_hdr_length_v6(ip6_t *, int, uint8_t *); 217 extern char *pr_type(uchar_t); 218 extern char *pr_type6(uchar_t); 219 extern void print_addr(uchar_t *, int, struct sockaddr *); 220 extern void print_addr6(uchar_t *, int, struct sockaddr *); 221 extern boolean_t print_icmp_other(uchar_t, uchar_t); 222 extern boolean_t print_icmp_other6(uchar_t, uchar_t); 223 static void print_stats(int, int, double, double, double, double); 224 static void print_unknown_host_msg(const char *, const char *); 225 static void record_stats(double, int *, double *, double *, double *, double *); 226 static void resolve_nodes(int *, struct addrinfo **); 227 static void select_src_addr(union any_in_addr *, union any_in_addr *, int); 228 extern void send_probe(int, struct sockaddr *, struct ip *, int, int, 229 struct timeval *, int); 230 extern void send_probe6(int, struct msghdr *, struct ip *, int, int, 231 struct timeval *, int); 232 extern void set_ancillary_data(struct msghdr *, int, union any_in_addr *, int, 233 uint_t); 234 extern struct ip *set_buffers(int); 235 extern struct ip *set_buffers6(int); 236 extern void set_IPv4opt_sourcerouting(int, union any_in_addr *, 237 union any_in_addr *); 238 static void set_sin(struct sockaddr *, union any_in_addr *, int); 239 static int set_src_addr(struct pr_set *, struct ifaddrlist **); 240 static void setup_protocol(struct pr_set *, int); 241 static void setup_socket(struct pr_set *, int); 242 static void sig_handler(int); 243 static int str2int(const char *, const char *, int, int); 244 static double str2dbl(const char *, const char *, double, double); 245 static void trace_it(struct addrinfo *); 246 static void traceroute(union any_in_addr *, struct msghdr *, struct pr_set *, 247 int, struct ifaddrlist *); 248 static void tv_sub(struct timeval *, struct timeval *); 249 static void usage(void); 250 static int wait_for_reply(int, struct msghdr *, struct timeval *); 251 static double xsqrt(double); 252 static void *ns_warning_thr(void *); 253 254 /* 255 * main 256 */ 257 int 258 main(int argc, char **argv) 259 { 260 struct addrinfo *ai_dst = NULL; /* destination host */ 261 /* 262 * "probing_successful" indicates if we could successfully send probes, 263 * not necessarily received reply from the target (this behavior is from 264 * the original traceroute). It's _B_FALSE if packlen is invalid, or no 265 * interfaces found. 266 */ 267 boolean_t probing_successful = _B_FALSE; 268 int longjmp_return; /* return value from longjump */ 269 int i = 0; 270 char *cp; 271 int op; 272 char *ep; 273 char temp_buf[INET6_ADDRSTRLEN]; /* use for inet_ntop() */ 274 double pause; 275 276 /* 277 * A raw socket will be used for IPv4 if there is sufficient 278 * privilege. 279 */ 280 raw_req = priv_ineffect(PRIV_NET_RAWACCESS); 281 282 /* 283 * We'll need the privilege only when we open the sockets; that's 284 * when we'll fail if the program has insufficient privileges. 285 */ 286 (void) __init_suid_priv(PU_CLEARLIMITSET, PRIV_NET_ICMPACCESS, 287 raw_req ? PRIV_NET_RAWACCESS : NULL, NULL); 288 289 (void) setlinebuf(stdout); 290 291 if ((cp = strrchr(argv[0], '/')) != NULL) 292 prog = cp + 1; 293 else 294 prog = argv[0]; 295 296 opterr = 0; 297 while ((op = getopt(argc, argv, "adFIlnrSvxA:c:f:g:i:L:m:P:p:Q:q:s:" 298 "t:w:")) != EOF) { 299 switch (op) { 300 case 'A': 301 if (strcmp(optarg, "inet") == 0) { 302 family_input = AF_INET; 303 } else if (strcmp(optarg, "inet6") == 0) { 304 family_input = AF_INET6; 305 } else { 306 Fprintf(stderr, 307 "%s: unknown address family %s\n", 308 prog, optarg); 309 exit(EXIT_FAILURE); 310 } 311 break; 312 313 case 'a': 314 probe_all = _B_TRUE; 315 break; 316 317 case 'c': 318 class = str2int(optarg, "traffic class", 0, 319 MAX_TRAFFIC_CLASS); 320 break; 321 322 case 'd': 323 options |= SO_DEBUG; 324 break; 325 326 case 'f': 327 first_ttl = str2int(optarg, "first ttl", 1, MAXTTL); 328 break; 329 330 case 'F': 331 off = IP_DF; 332 dontfrag = 1; 333 break; 334 335 case 'g': 336 if (!raw_req) { 337 Fprintf(stderr, 338 "%s: privilege to specify a loose source " 339 "route gateway is unavailable\n", 340 prog); 341 exit(EXIT_FAILURE); 342 } 343 if (gw_count >= MAXMAX_GWS) { 344 Fprintf(stderr, 345 "%s: Too many gateways\n", prog); 346 exit(EXIT_FAILURE); 347 } 348 gwlist[gw_count] = strdup(optarg); 349 if (gwlist[gw_count] == NULL) { 350 Fprintf(stderr, "%s: strdup %s\n", prog, 351 strerror(errno)); 352 exit(EXIT_FAILURE); 353 } 354 355 ++gw_count; 356 break; 357 358 case 'l': 359 showttl = _B_TRUE; 360 break; 361 362 case 'i': 363 /* this can be IF name or IF index */ 364 if_index = (uint_t)strtol(optarg, &ep, 10); 365 366 /* convert IF index <--> IF name */ 367 if (errno != 0 || *ep != '\0') { 368 device = optarg; 369 if_index = if_nametoindex((const char *)device); 370 371 /* 372 * In case it fails, check to see if the problem 373 * is other than "IF not found". 374 */ 375 if (if_index == 0 && errno != ENXIO) { 376 Fprintf(stderr, "%s: if_nametoindex:" 377 "%s\n", prog, strerror(errno)); 378 exit(EXIT_FAILURE); 379 } 380 } else { 381 device = (char *)malloc(LIFNAMSIZ + 1); 382 if (device == NULL) { 383 Fprintf(stderr, "%s: malloc: %s\n", 384 prog, strerror(errno)); 385 exit(EXIT_FAILURE); 386 } 387 388 device = if_indextoname(if_index, device); 389 if (device != NULL) { 390 device[LIFNAMSIZ] = '\0'; 391 } else if (errno != ENXIO) { 392 /* 393 * The problem was other than "index 394 * not found". 395 */ 396 Fprintf(stderr, "%s: if_indextoname:" 397 "%s\n", prog, strerror(errno)); 398 exit(EXIT_FAILURE); 399 } 400 } 401 402 if (device == NULL || if_index == 0) { 403 Fprintf(stderr, "%s: interface %s " 404 "doesn't match any actual interfaces\n", 405 prog, optarg); 406 exit(EXIT_FAILURE); 407 } 408 break; 409 410 case 'I': 411 useicmp = _B_TRUE; 412 break; 413 414 case 'L': 415 flow = str2int(optarg, "flow label", 0, MAX_FLOW_LABEL); 416 break; 417 418 case 'm': 419 max_ttl = str2int(optarg, "max ttl(hop limit)", 1, 420 MAXTTL); 421 break; 422 423 case 'n': 424 nflag = _B_TRUE; 425 break; 426 427 case 'P': 428 pause = str2dbl(optarg, "pause", 0, INT_MAX); 429 delay.tv_sec = (time_t)pause; 430 delay.tv_usec = (suseconds_t)((pause - delay.tv_sec) * 431 1000000); 432 break; 433 434 case 'p': 435 port = str2int(optarg, "port", 1, MAX_PORT); 436 break; 437 438 case 'Q': 439 max_timeout = str2int(optarg, "max timeout", 1, -1); 440 break; 441 442 case 'q': 443 nprobes = str2int(optarg, "nprobes", 1, -1); 444 break; 445 446 case 'r': 447 options |= SO_DONTROUTE; 448 break; 449 450 case 'S': 451 collect_stat = _B_TRUE; 452 break; 453 454 case 's': 455 /* 456 * set the ip source address of the outbound 457 * probe (e.g., on a multi-homed host). 458 */ 459 source_input = optarg; 460 break; 461 462 case 't': 463 tos = (uchar_t)str2int(optarg, "tos", 0, MAX_TOS); 464 settos = _B_TRUE; 465 break; 466 467 case 'v': 468 verbose = _B_TRUE; 469 break; 470 471 case 'x': 472 docksum = _B_FALSE; 473 break; 474 475 case 'w': 476 waittime = str2int(optarg, "wait time", 2, -1); 477 break; 478 479 default: 480 usage(); 481 break; 482 } 483 } 484 485 /* 486 * If it's probe_all, SIGQUIT makes traceroute exit(). But we set the 487 * address to jump back to in traceroute(). Until then, we'll need to 488 * temporarily specify one. 489 */ 490 if (probe_all) { 491 if ((longjmp_return = setjmp(env)) != 0) { 492 if (longjmp_return == SIGQUIT) { 493 Printf("(exiting)\n"); 494 exit(EXIT_SUCCESS); 495 } else { /* should never happen */ 496 exit(EXIT_FAILURE); 497 } 498 } 499 (void) signal(SIGQUIT, sig_handler); 500 } 501 502 if ((gw_count > 0) && (options & SO_DONTROUTE)) { 503 Fprintf(stderr, "%s: loose source route gateways (-g)" 504 " cannot be specified when probe packets are sent" 505 " directly to a host on an attached network (-r)\n", 506 prog); 507 exit(EXIT_FAILURE); 508 } 509 510 i = argc - optind; 511 if (i == 1 || i == 2) { 512 hostname = argv[optind]; 513 514 if (i == 2) { 515 /* accept any length now, we'll check it later */ 516 packlen_input = str2int(argv[optind + 1], 517 "packet length", 0, -1); 518 } 519 } else { 520 usage(); 521 } 522 523 if (first_ttl > max_ttl) { 524 Fprintf(stderr, 525 "%s: first ttl(hop limit) (%d) may not be greater" 526 " than max ttl(hop limit) (%d)\n", 527 prog, first_ttl, max_ttl); 528 exit(EXIT_FAILURE); 529 } 530 531 /* 532 * Start up the name services warning thread. 533 */ 534 if (thr_create(NULL, 0, ns_warning_thr, NULL, 535 THR_DETACHED | THR_DAEMON, NULL) != 0) { 536 Fprintf(stderr, "%s: failed to create name services " 537 "thread: %s\n", prog, strerror(errno)); 538 exit(EXIT_FAILURE); 539 } 540 541 542 /* resolve hostnames */ 543 resolve_nodes(&family_input, &ai_dst); 544 if (ai_dst == NULL) { 545 exit(EXIT_FAILURE); 546 } 547 548 /* 549 * If it's probe_all, SIGINT makes traceroute skip to probing next IP 550 * address of the target. The new interrupt handler is assigned in 551 * traceroute() function. Until then let's ignore the signal. 552 */ 553 if (probe_all) 554 (void) signal(SIGINT, SIG_IGN); 555 556 ident = (getpid() & 0xffff) | 0x8000; 557 558 /* 559 * We KNOW that probe_all == TRUE if family is AF_UNSPEC, 560 * since family is set to the specific AF found unless it's 561 * probe_all. So if family == AF_UNSPEC, we need to init pr4 and pr6. 562 */ 563 switch (family_input) { 564 case AF_UNSPEC: 565 pr4 = (struct pr_set *)malloc(sizeof (struct pr_set)); 566 if (pr4 == NULL) { 567 Fprintf(stderr, 568 "%s: malloc %s\n", prog, strerror(errno)); 569 exit(EXIT_FAILURE); 570 } 571 pr6 = (struct pr_set *)malloc(sizeof (struct pr_set)); 572 if (pr6 == NULL) { 573 Fprintf(stderr, 574 "%s: malloc %s\n", prog, strerror(errno)); 575 exit(EXIT_FAILURE); 576 } 577 setup_protocol(pr6, AF_INET6); 578 setup_protocol(pr4, AF_INET); 579 outip6 = (*pr6->set_buffers_fn)(pr6->packlen); 580 setup_socket(pr6, pr6->packlen); 581 582 outip4 = (*pr4->set_buffers_fn)(pr4->packlen); 583 setup_socket(pr4, pr4->packlen); 584 num_ifs6 = set_src_addr(pr6, &al6); 585 num_ifs4 = set_src_addr(pr4, &al4); 586 break; 587 case AF_INET6: 588 pr6 = (struct pr_set *)malloc(sizeof (struct pr_set)); 589 if (pr6 == NULL) { 590 Fprintf(stderr, 591 "%s: malloc %s\n", prog, strerror(errno)); 592 exit(EXIT_FAILURE); 593 } 594 setup_protocol(pr6, AF_INET6); 595 outip6 = (*pr6->set_buffers_fn)(pr6->packlen); 596 setup_socket(pr6, pr6->packlen); 597 num_ifs6 = set_src_addr(pr6, &al6); 598 break; 599 case AF_INET: 600 pr4 = (struct pr_set *)malloc(sizeof (struct pr_set)); 601 if (pr4 == NULL) { 602 Fprintf(stderr, 603 "%s: malloc %s\n", prog, strerror(errno)); 604 exit(EXIT_FAILURE); 605 } 606 setup_protocol(pr4, AF_INET); 607 outip4 = (*pr4->set_buffers_fn)(pr4->packlen); 608 setup_socket(pr4, pr4->packlen); 609 num_ifs4 = set_src_addr(pr4, &al4); 610 break; 611 default: 612 Fprintf(stderr, "%s: unknow address family.\n", prog); 613 exit(EXIT_FAILURE); 614 } 615 616 if (num_v4 + num_v6 > 1 && !probe_all) { 617 if (ai_dst->ai_family == AF_INET) { 618 Fprintf(stderr, 619 "%s: Warning: %s has multiple addresses;" 620 " using %s\n", prog, hostname, 621 inet_ntop(AF_INET, 622 /* LINTED E_BAD_PTR_CAST_ALIGN */ 623 (void *)&((struct sockaddr_in *) 624 ai_dst->ai_addr)->sin_addr, 625 temp_buf, sizeof (temp_buf))); 626 } else { 627 Fprintf(stderr, 628 "%s: Warning: %s has multiple addresses;" 629 " using %s\n", prog, hostname, 630 inet_ntop(AF_INET6, 631 /* LINTED E_BAD_PTR_CAST_ALIGN */ 632 (void *)&((struct sockaddr_in6 *) 633 ai_dst->ai_addr)->sin6_addr, 634 temp_buf, sizeof (temp_buf))); 635 } 636 } 637 638 if (num_ifs4 + num_ifs6 > 0) { 639 trace_it(ai_dst); 640 probing_successful = _B_TRUE; 641 } 642 643 (void) close(rcvsock4); 644 (void) close(sndsock4); 645 (void) close(rcvsock6); 646 (void) close(sndsock6); 647 648 /* 649 * if we could probe any of the IP addresses of the target, that means 650 * this was a successful operation 651 */ 652 if (probing_successful) 653 return (EXIT_SUCCESS); 654 else 655 return (EXIT_FAILURE); 656 } 657 658 /* 659 * print "unknown host" message 660 */ 661 static void 662 print_unknown_host_msg(const char *protocol, const char *host) 663 { 664 Fprintf(stderr, "%s: unknown%s host %s\n", prog, protocol, host); 665 } 666 667 /* 668 * resolve destination host and gateways 669 */ 670 static void 671 resolve_nodes(int *family, struct addrinfo **ai_dstp) 672 { 673 struct addrinfo *ai_dst = NULL; 674 struct addrinfo *aip = NULL; 675 int num_resolved_gw = 0; 676 int num_resolved_gw6 = 0; 677 678 get_hostinfo(hostname, *family, &ai_dst); 679 if (ai_dst == NULL) { 680 print_unknown_host_msg("", hostname); 681 exit(EXIT_FAILURE); 682 } 683 /* Get a count of the v4 & v6 addresses */ 684 for (aip = ai_dst; aip != NULL; aip = aip->ai_next) { 685 switch (aip->ai_family) { 686 case AF_INET: 687 num_v4++; 688 break; 689 case AF_INET6: 690 num_v6++; 691 break; 692 } 693 } 694 695 if (*family == AF_UNSPEC && !probe_all) { 696 *family = ai_dst->ai_family; 697 } 698 699 /* resolve gateways */ 700 if (gw_count > 0) { 701 get_gwaddrs(gwlist, *family, gwIPlist, gwIP6list, 702 &num_resolved_gw, &num_resolved_gw6); 703 704 /* we couldn't resolve a gateway as an IPv6 host */ 705 if (num_resolved_gw6 != gw_count && num_v6 != 0) { 706 if (*family == AF_INET6 || *family == AF_UNSPEC) 707 print_unknown_host_msg(" IPv6", 708 gwlist[num_resolved_gw6]); 709 num_v6 = 0; 710 } 711 712 /* we couldn't resolve a gateway as an IPv4 host */ 713 if (num_resolved_gw != gw_count && num_v4 != 0) { 714 if (*family == AF_INET || *family == AF_UNSPEC) 715 print_unknown_host_msg(" IPv4", 716 gwlist[num_resolved_gw]); 717 num_v4 = 0; 718 } 719 } 720 721 *ai_dstp = (num_v4 + num_v6 > 0) ? ai_dst : NULL; 722 } 723 724 /* 725 * Given IP address or hostname, return v4 and v6 hostinfo lists. 726 * Assumes that hostinfo ** ptrs are non-null. 727 */ 728 static void 729 get_hostinfo(char *host, int family, struct addrinfo **aipp) 730 { 731 struct addrinfo hints, *ai; 732 struct in6_addr addr6; 733 struct in_addr addr; 734 char abuf[INET6_ADDRSTRLEN]; /* use for inet_ntop() */ 735 int rc; 736 737 /* 738 * Take care of v4-mapped addresses. It should run same as v4, after 739 * chopping off the prefix, leaving the IPv4 address 740 */ 741 if ((inet_pton(AF_INET6, host, &addr6) > 0) && 742 IN6_IS_ADDR_V4MAPPED(&addr6)) { 743 /* peel off the "mapping" stuff, leaving 32 bit IPv4 address */ 744 IN6_V4MAPPED_TO_INADDR(&addr6, &addr); 745 746 /* convert it back to a string */ 747 (void) inet_ntop(AF_INET, &addr, abuf, sizeof (abuf)); 748 749 /* now the host is an IPv4 address */ 750 (void) strcpy(host, abuf); 751 752 /* 753 * If it's a mapped address, we convert it into IPv4 754 * address because traceroute will send and receive IPv4 755 * packets for that address. Therefore, it's a failure case to 756 * ask get_hostinfo() to treat a mapped address as an IPv6 757 * address. 758 */ 759 if (family == AF_INET6) { 760 return; 761 } 762 } 763 764 (void) memset(&hints, 0, sizeof (hints)); 765 hints.ai_family = family; 766 hints.ai_flags = AI_ADDRCONFIG | AI_CANONNAME; 767 rc = getaddrinfo(host, NULL, &hints, &ai); 768 if (rc != 0) { 769 if (rc != EAI_NONAME) 770 Fprintf(stderr, "%s: getaddrinfo: %s\n", prog, 771 gai_strerror(rc)); 772 *aipp = NULL; 773 return; 774 } 775 *aipp = ai; 776 } 777 778 /* 779 * Calculate the packet length to be used, and check against the valid range. 780 * Returns -1 if range check fails. 781 */ 782 static uint_t 783 calc_packetlen(int plen_input, struct pr_set *pr) 784 { 785 int minpacket; /* min ip packet size */ 786 int optlen; /* length of ip options */ 787 int plen; 788 789 /* 790 * LBNL bug fixed: miscalculation of optlen 791 */ 792 if (gw_count > 0) { 793 /* 794 * IPv4: 795 * ---- 796 * 5 (NO OPs) + 3 (code, len, ptr) + gateways 797 * IP options field can hold up to 9 gateways. But the API 798 * allows you to specify only 8, because the last one is the 799 * destination host. When this packet is sent, on the wire 800 * you see one gateway replaced by 4 NO OPs. The other 1 NO 801 * OP is for alignment 802 * 803 * IPv6: 804 * ---- 805 * Well, formula is different, but the result is same. 806 * 8 byte fixed part for Type 0 Routing header, followed by 807 * gateway addresses 808 */ 809 optlen = 8 + gw_count * pr->addr_len; 810 } else { 811 optlen = 0; 812 } 813 814 /* take care of the packet length calculations and checks */ 815 minpacket = pr->ip_hdr_len + sizeof (struct outdata) + optlen; 816 if (useicmp) 817 minpacket += pr->icmp_minlen; /* minimum ICMP header size */ 818 else 819 minpacket += sizeof (struct udphdr); 820 plen = plen_input; 821 if (plen == 0) { 822 plen = minpacket; /* minimum sized packet */ 823 } else if (minpacket > plen || plen > IP_MAXPACKET) { 824 Fprintf(stderr, "%s: %s packet size must be >= %d and <= %d\n", 825 prog, pr->name, minpacket, IP_MAXPACKET); 826 return (0); 827 } 828 829 return (plen); 830 } 831 832 /* 833 * Sets the source address by resolving -i and -s arguments, or if -i and -s 834 * don't dictate any, it sets the pick_src to make sure traceroute uses the 835 * kernel's pick of the source address. 836 * Returns number of interfaces configured on the source host, 0 on error or 837 * there's no interface which is up amd not a loopback. 838 */ 839 static int 840 set_src_addr(struct pr_set *pr, struct ifaddrlist **alp) 841 { 842 union any_in_addr *ap; 843 struct ifaddrlist *al = NULL; 844 struct ifaddrlist *tmp1_al = NULL; 845 struct ifaddrlist *tmp2_al = NULL; 846 /* LINTED E_BAD_PTR_CAST_ALIGN */ 847 struct sockaddr_in *sin_from = (struct sockaddr_in *)pr->from; 848 /* LINTED E_BAD_PTR_CAST_ALIGN */ 849 struct sockaddr_in6 *sin6_from = (struct sockaddr_in6 *)pr->from; 850 struct addrinfo *aip; 851 char errbuf[ERRBUFSIZE]; 852 char abuf[INET6_ADDRSTRLEN]; /* use for inet_ntop() */ 853 int num_ifs; /* all the interfaces */ 854 int num_src_ifs; /* exclude loopback and down */ 855 int i; 856 uint_t ifaddrflags = 0; 857 858 source = source_input; 859 860 if (device != NULL) 861 ifaddrflags |= LIFC_UNDER_IPMP; 862 863 /* get the interface address list */ 864 num_ifs = ifaddrlist(&al, pr->family, ifaddrflags, errbuf); 865 if (num_ifs < 0) { 866 Fprintf(stderr, "%s: ifaddrlist: %s\n", prog, errbuf); 867 exit(EXIT_FAILURE); 868 } 869 870 num_src_ifs = 0; 871 for (i = 0; i < num_ifs; i++) { 872 if (!(al[i].flags & IFF_LOOPBACK) && (al[i].flags & IFF_UP)) 873 num_src_ifs++; 874 } 875 876 if (num_src_ifs == 0) { 877 Fprintf(stderr, "%s: can't find any %s network interfaces\n", 878 prog, pr->name); 879 return (0); 880 } 881 882 /* verify the device */ 883 if (device != NULL) { 884 tmp1_al = find_device(al, num_ifs, device); 885 886 if (tmp1_al == NULL) { 887 Fprintf(stderr, "%s: %s (index %d) is an invalid %s" 888 " interface\n", prog, device, if_index, pr->name); 889 free(al); 890 return (0); 891 } 892 } 893 894 /* verify the source address */ 895 if (source != NULL) { 896 get_hostinfo(source, pr->family, &aip); 897 if (aip == NULL) { 898 Fprintf(stderr, 899 "%s: %s is an invalid %s source address\n", 900 prog, source, pr->name); 901 902 free(al); 903 return (0); 904 } 905 906 source = aip->ai_canonname; 907 908 if (pr->family == AF_INET) 909 ap = (union any_in_addr *) 910 /* LINTED E_BAD_PTR_CAST_ALIGN */ 911 &((struct sockaddr_in *)aip->ai_addr)->sin_addr; 912 else 913 ap = (union any_in_addr *) 914 /* LINTED E_BAD_PTR_CAST_ALIGN */ 915 &((struct sockaddr_in6 *)aip->ai_addr)->sin6_addr; 916 917 /* 918 * LBNL bug fixed: used to accept any src address 919 */ 920 tmp2_al = find_ifaddr(al, num_ifs, ap, pr->family); 921 if (tmp2_al == NULL) { 922 (void) inet_ntop(pr->family, ap, abuf, sizeof (abuf)); 923 Fprintf(stderr, "%s: %s is not a local %s address\n", 924 prog, abuf, pr->name); 925 free(al); 926 freeaddrinfo(aip); 927 return (0); 928 } 929 } 930 931 pick_src = _B_FALSE; 932 933 if (source == NULL) { /* no -s used */ 934 if (device == NULL) { /* no -i used, no -s used */ 935 pick_src = _B_TRUE; 936 } else { /* -i used, no -s used */ 937 /* 938 * -i used, but not -s, and it's IPv4: set the source 939 * address to whatever the interface has configured on 940 * it. 941 */ 942 if (pr->family == AF_INET) 943 set_sin(pr->from, &(tmp1_al->addr), pr->family); 944 else 945 pick_src = _B_TRUE; 946 } 947 } else { /* -s used */ 948 if (device == NULL) { /* no -i used, -s used */ 949 set_sin(pr->from, ap, pr->family); 950 951 if (aip->ai_next != NULL) { 952 (void) inet_ntop(pr->family, pr->from_sin_addr, 953 abuf, sizeof (abuf)); 954 Fprintf(stderr, "%s: Warning: %s has multiple " 955 "addresses; using %s\n", prog, source, 956 abuf); 957 } 958 } else { /* -i and -s used */ 959 /* 960 * Make sure the source specified matches the 961 * interface address. You only care about this for IPv4 962 * IPv6 can handle IF not matching src address 963 */ 964 if (pr->family == AF_INET) { 965 if (!has_addr(aip, &tmp1_al->addr)) { 966 Fprintf(stderr, 967 "%s: %s is not on interface %s\n", 968 prog, source, device); 969 exit(EXIT_FAILURE); 970 } 971 /* 972 * make sure we use the one matching the 973 * interface's address 974 */ 975 *ap = tmp1_al->addr; 976 } 977 978 set_sin(pr->from, ap, pr->family); 979 } 980 } 981 982 /* 983 * Binding at this point will set the source address to be used 984 * for both IPv4 (when raw IP datagrams are not required) and 985 * IPv6. If the address being bound to is zero, then the kernel 986 * will end up choosing the source address when the datagram is 987 * sent. 988 * 989 * For raw IPv4 datagrams, the source address is initialized 990 * within traceroute() along with the outbound destination 991 * address. 992 */ 993 if (pr->family == AF_INET && !raw_req) { 994 sin_from->sin_family = AF_INET; 995 sin_from->sin_port = htons(ident); 996 if (bind(sndsock4, (struct sockaddr *)pr->from, 997 sizeof (struct sockaddr_in)) < 0) { 998 Fprintf(stderr, "%s: bind: %s\n", prog, 999 strerror(errno)); 1000 exit(EXIT_FAILURE); 1001 } 1002 } else if (pr->family == AF_INET6) { 1003 sin6_from->sin6_family = AF_INET6; 1004 sin6_from->sin6_port = htons(ident); 1005 if (bind(sndsock6, (struct sockaddr *)pr->from, 1006 sizeof (struct sockaddr_in6)) < 0) { 1007 Fprintf(stderr, "%s: bind: %s\n", prog, 1008 strerror(errno)); 1009 exit(EXIT_FAILURE); 1010 } 1011 1012 whereto6.sin6_flowinfo = htonl((class << 20) | flow); 1013 } 1014 *alp = al; 1015 return (num_ifs); 1016 } 1017 1018 /* 1019 * Returns the complete ifaddrlist structure matching the desired interface 1020 * address. Ignores interfaces which are either down or loopback. 1021 */ 1022 static struct ifaddrlist * 1023 find_ifaddr(struct ifaddrlist *al, int len, union any_in_addr *addr, 1024 int family) 1025 { 1026 struct ifaddrlist *tmp_al = al; 1027 int i; 1028 size_t addr_len = (family == AF_INET) ? sizeof (struct in_addr) : 1029 sizeof (struct in6_addr); 1030 1031 for (i = 0; i < len; i++, tmp_al++) { 1032 if ((!(tmp_al->flags & IFF_LOOPBACK) && 1033 (tmp_al->flags & IFF_UP)) && 1034 (memcmp(&tmp_al->addr, addr, addr_len) == 0)) 1035 break; 1036 } 1037 1038 if (i < len) { 1039 return (tmp_al); 1040 } else { 1041 return (NULL); 1042 } 1043 } 1044 1045 /* 1046 * Returns the complete ifaddrlist structure matching the desired interface name 1047 * Ignores interfaces which are either down or loopback. 1048 */ 1049 static struct ifaddrlist * 1050 find_device(struct ifaddrlist *al, int len, char *device) 1051 { 1052 struct ifaddrlist *tmp_al = al; 1053 int i; 1054 1055 for (i = 0; i < len; i++, tmp_al++) { 1056 if ((!(tmp_al->flags & IFF_LOOPBACK) && 1057 (tmp_al->flags & IFF_UP)) && 1058 (strcmp(tmp_al->device, device) == 0)) 1059 break; 1060 } 1061 1062 if (i < len) { 1063 return (tmp_al); 1064 } else { 1065 return (NULL); 1066 } 1067 } 1068 1069 /* 1070 * returns _B_TRUE if given hostinfo contains the given address 1071 */ 1072 static boolean_t 1073 has_addr(struct addrinfo *ai, union any_in_addr *addr) 1074 { 1075 struct addrinfo *ai_tmp = NULL; 1076 union any_in_addr *ap; 1077 1078 for (ai_tmp = ai; ai_tmp != NULL; ai_tmp = ai_tmp->ai_next) { 1079 if (ai_tmp->ai_family == AF_INET6) 1080 continue; 1081 ap = (union any_in_addr *) 1082 /* LINTED E_BAD_PTR_CAST_ALIGN */ 1083 &((struct sockaddr_in *)ai_tmp->ai_addr)->sin_addr; 1084 if (memcmp(ap, addr, sizeof (struct in_addr)) == 0) 1085 break; 1086 } 1087 1088 if (ai_tmp != NULL) { 1089 return (_B_TRUE); 1090 } else { 1091 return (_B_FALSE); 1092 } 1093 } 1094 1095 /* 1096 * Resolve the gateway names, splitting results into v4 and v6 lists. 1097 * Gateway addresses are added to the appropriate passed-in array; the 1098 * number of resolved gateways for each af is returned in resolved[6]. 1099 * Assumes that passed-in arrays are large enough for MAX_GWS[6] addrs 1100 * and resolved[6] ptrs are non-null; ignores array and counter if the 1101 * address family param makes them irrelevant. 1102 */ 1103 static void 1104 get_gwaddrs(char **gwlist, int family, union any_in_addr *gwIPlist, 1105 union any_in_addr *gwIPlist6, int *resolved, int *resolved6) 1106 { 1107 int i; 1108 boolean_t check_v4 = _B_TRUE, check_v6 = _B_TRUE; 1109 struct addrinfo *ai = NULL; 1110 struct addrinfo *aip = NULL; 1111 1112 *resolved = *resolved6 = 0; 1113 switch (family) { 1114 case AF_UNSPEC: 1115 break; 1116 case AF_INET: 1117 check_v6 = _B_FALSE; 1118 break; 1119 case AF_INET6: 1120 check_v4 = _B_FALSE; 1121 break; 1122 default: 1123 return; 1124 } 1125 1126 if (check_v4 && gw_count >= MAX_GWS) { 1127 check_v4 = _B_FALSE; 1128 Fprintf(stderr, "%s: too many IPv4 gateways\n", prog); 1129 num_v4 = 0; 1130 } 1131 if (check_v6 && gw_count >= MAX_GWS6) { 1132 check_v6 = _B_FALSE; 1133 Fprintf(stderr, "%s: too many IPv6 gateways\n", prog); 1134 num_v6 = 0; 1135 } 1136 1137 for (i = 0; i < gw_count; i++) { 1138 if (!check_v4 && !check_v6) 1139 return; 1140 get_hostinfo(gwlist[i], family, &ai); 1141 if (ai == NULL) 1142 return; 1143 if (check_v4 && num_v4 != 0) { 1144 check_v4 = _B_FALSE; 1145 for (aip = ai; aip != NULL; aip = aip->ai_next) { 1146 if (aip->ai_family == AF_INET) { 1147 /* LINTED E_BAD_PTR_CAST_ALIGN */ 1148 bcopy(&((struct sockaddr_in *) 1149 aip->ai_addr)->sin_addr, 1150 &gwIPlist[i].addr, 1151 aip->ai_addrlen); 1152 (*resolved)++; 1153 check_v4 = _B_TRUE; 1154 break; 1155 } 1156 } 1157 } else if (check_v4) { 1158 check_v4 = _B_FALSE; 1159 } 1160 if (check_v6 && num_v6 != 0) { 1161 check_v6 = _B_FALSE; 1162 for (aip = ai; aip != NULL; aip = aip->ai_next) { 1163 if (aip->ai_family == AF_INET6) { 1164 /* LINTED E_BAD_PTR_CAST_ALIGN */ 1165 bcopy(&((struct sockaddr_in6 *) 1166 aip->ai_addr)->sin6_addr, 1167 &gwIPlist6[i].addr6, 1168 aip->ai_addrlen); 1169 (*resolved6)++; 1170 check_v6 = _B_TRUE; 1171 break; 1172 } 1173 } 1174 } else if (check_v6) { 1175 check_v6 = _B_FALSE; 1176 } 1177 } 1178 freeaddrinfo(ai); 1179 } 1180 1181 /* 1182 * set protocol specific values here 1183 */ 1184 static void 1185 setup_protocol(struct pr_set *pr, int family) 1186 { 1187 /* 1188 * Set the global variables for each AF. This is going to save us lots 1189 * of "if (family == AF_INET)... else .." 1190 */ 1191 pr->family = family; 1192 1193 if (family == AF_INET) { 1194 if (!docksum) { 1195 Fprintf(stderr, 1196 "%s: Warning: checksums disabled\n", prog); 1197 } 1198 (void) strcpy(pr->name, "IPv4"); 1199 (void) strcpy(pr->icmp, "icmp"); 1200 pr->icmp_minlen = ICMP_MINLEN; 1201 pr->addr_len = sizeof (struct in_addr); 1202 pr->ip_hdr_len = sizeof (struct ip); 1203 pr->sock_size = sizeof (struct sockaddr_in); 1204 pr->to = (struct sockaddr *)&whereto; 1205 pr->from = (struct sockaddr *)&wherefrom; 1206 pr->from_sin_addr = (void *)&wherefrom.sin_addr; 1207 pr->gwIPlist = gwIPlist; 1208 pr->set_buffers_fn = set_buffers; 1209 pr->check_reply_fn = check_reply; 1210 pr->print_icmp_other_fn = print_icmp_other; 1211 pr->print_addr_fn = print_addr; 1212 pr->packlen = calc_packetlen(packlen_input, pr); 1213 } else { 1214 (void) strcpy(pr->name, "IPv6"); 1215 (void) strcpy(pr->icmp, "ipv6-icmp"); 1216 pr->icmp_minlen = ICMP6_MINLEN; 1217 pr->addr_len = sizeof (struct in6_addr); 1218 pr->ip_hdr_len = sizeof (struct ip6_hdr); 1219 pr->sock_size = sizeof (struct sockaddr_in6); 1220 pr->to = (struct sockaddr *)&whereto6; 1221 pr->from = (struct sockaddr *)&wherefrom6; 1222 pr->from_sin_addr = (void *)&wherefrom6.sin6_addr; 1223 pr->gwIPlist = gwIP6list; 1224 pr->set_buffers_fn = set_buffers6; 1225 pr->check_reply_fn = check_reply6; 1226 pr->print_icmp_other_fn = print_icmp_other6; 1227 pr->print_addr_fn = print_addr6; 1228 pr->packlen = calc_packetlen(packlen_input, pr); 1229 } 1230 if (pr->packlen == 0) 1231 exit(EXIT_FAILURE); 1232 } 1233 1234 /* 1235 * setup the sockets for the given protocol's address family 1236 */ 1237 static void 1238 setup_socket(struct pr_set *pr, int packet_len) 1239 { 1240 int on = 1; 1241 struct protoent *pe; 1242 int type; 1243 int proto; 1244 int int_op; 1245 int rsock; 1246 int ssock; 1247 1248 if ((pe = getprotobyname(pr->icmp)) == NULL) { 1249 Fprintf(stderr, "%s: unknown protocol %s\n", prog, pr->icmp); 1250 exit(EXIT_FAILURE); 1251 } 1252 1253 /* privilege bracketing */ 1254 (void) __priv_bracket(PRIV_ON); 1255 1256 if ((rsock = socket(pr->family, SOCK_RAW, pe->p_proto)) < 0) { 1257 Fprintf(stderr, "%s: icmp socket: %s\n", prog, strerror(errno)); 1258 exit(EXIT_FAILURE); 1259 } 1260 1261 if (options & SO_DEBUG) { 1262 if (setsockopt(rsock, SOL_SOCKET, SO_DEBUG, (char *)&on, 1263 sizeof (on)) < 0) { 1264 Fprintf(stderr, "%s: SO_DEBUG: %s\n", prog, 1265 strerror(errno)); 1266 exit(EXIT_FAILURE); 1267 } 1268 } 1269 if (options & SO_DONTROUTE) { 1270 if (setsockopt(rsock, SOL_SOCKET, SO_DONTROUTE, (char *)&on, 1271 sizeof (on)) < 0) { 1272 Fprintf(stderr, "%s: SO_DONTROUTE: %s\n", prog, 1273 strerror(errno)); 1274 exit(EXIT_FAILURE); 1275 } 1276 } 1277 1278 if (pr->family == AF_INET6) { 1279 /* Enable receipt of destination address info */ 1280 if (setsockopt(rsock, IPPROTO_IPV6, IPV6_RECVPKTINFO, 1281 (char *)&on, sizeof (on)) < 0) { 1282 Fprintf(stderr, "%s: IPV6_RECVPKTINFO: %s\n", prog, 1283 strerror(errno)); 1284 exit(EXIT_FAILURE); 1285 } 1286 /* Enable receipt of hoplimit info */ 1287 if (setsockopt(rsock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, 1288 (char *)&on, sizeof (on)) < 0) { 1289 Fprintf(stderr, "%s: IPV6_RECVHOPLIMIT: %s\n", prog, 1290 strerror(errno)); 1291 exit(EXIT_FAILURE); 1292 } 1293 1294 } 1295 1296 /* 1297 * Initialize the socket type and protocol based on the address 1298 * family, whether or not a raw IP socket is required (for IPv4) 1299 * or whether ICMP will be used instead of UDP. 1300 * 1301 * For historical reasons, the datagrams sent out by 1302 * traceroute(8) do not have the "don't fragment" flag set. For 1303 * this reason as well as the ability to set the Loose Source and 1304 * Record Route (LSRR) option, a raw IP socket will be used for 1305 * IPv4 when run in the global zone. Otherwise, the actual 1306 * datagram that will be sent will be a regular UDP or ICMP echo 1307 * request packet. However for convenience and for future options 1308 * when other IP header information may be specified using 1309 * traceroute, the buffer including the raw IP and UDP or ICMP 1310 * header is always filled in. When the probe is actually sent, 1311 * the size of the request and the start of the packet is set 1312 * according to the type of datagram to send. 1313 */ 1314 if (pr->family == AF_INET && raw_req) { 1315 type = SOCK_RAW; 1316 proto = IPPROTO_RAW; 1317 } else if (useicmp) { 1318 type = SOCK_RAW; 1319 if (pr->family == AF_INET) 1320 proto = IPPROTO_ICMP; 1321 else 1322 proto = IPPROTO_ICMPV6; 1323 } else { 1324 type = SOCK_DGRAM; 1325 proto = IPPROTO_UDP; 1326 } 1327 ssock = socket(pr->family, type, proto); 1328 1329 if (ssock < 0) { 1330 if (proto == IPPROTO_RAW) { 1331 Fprintf(stderr, "%s: raw socket: %s\n", prog, 1332 strerror(errno)); 1333 } else if (proto == IPPROTO_UDP) { 1334 Fprintf(stderr, "%s: udp socket: %s\n", prog, 1335 strerror(errno)); 1336 } else { 1337 Fprintf(stderr, "%s: icmp socket: %s\n", prog, 1338 strerror(errno)); 1339 } 1340 exit(EXIT_FAILURE); 1341 } 1342 1343 if (setsockopt(ssock, SOL_SOCKET, SO_SNDBUF, (char *)&packet_len, 1344 sizeof (packet_len)) < 0) { 1345 Fprintf(stderr, "%s: SO_SNDBUF: %s\n", prog, strerror(errno)); 1346 exit(EXIT_FAILURE); 1347 } 1348 1349 if (pr->family == AF_INET && raw_req) { 1350 if (setsockopt(ssock, IPPROTO_IP, IP_HDRINCL, (char *)&on, 1351 sizeof (on)) < 0) { 1352 Fprintf(stderr, "%s: IP_HDRINCL: %s\n", prog, 1353 strerror(errno)); 1354 exit(EXIT_FAILURE); 1355 } 1356 } 1357 1358 if (options & SO_DEBUG) { 1359 if (setsockopt(ssock, SOL_SOCKET, SO_DEBUG, (char *)&on, 1360 sizeof (on)) < 0) { 1361 Fprintf(stderr, "%s: SO_DEBUG: %s\n", prog, 1362 strerror(errno)); 1363 exit(EXIT_FAILURE); 1364 } 1365 } 1366 if (options & SO_DONTROUTE) { 1367 if (setsockopt(ssock, SOL_SOCKET, SO_DONTROUTE, 1368 (char *)&on, sizeof (on)) < 0) { 1369 Fprintf(stderr, "%s: SO_DONTROUTE: %s\n", prog, 1370 strerror(errno)); 1371 exit(EXIT_FAILURE); 1372 } 1373 } 1374 1375 /* 1376 * If a raw IPv4 packet is going to be sent, the Type of Service 1377 * field in the packet will be initialized in set_buffers(). 1378 * Otherwise, it is initialized here using the IPPROTO_IP level 1379 * socket option. 1380 */ 1381 if (settos && !raw_req) { 1382 int_op = tos; 1383 if (setsockopt(ssock, IPPROTO_IP, IP_TOS, (char *)&int_op, 1384 sizeof (int_op)) < 0) { 1385 Fprintf(stderr, "%s: IP_TOS: %s\n", prog, 1386 strerror(errno)); 1387 exit(EXIT_FAILURE); 1388 } 1389 } 1390 1391 /* We enable or disable to not depend on the kernel default */ 1392 if (pr->family == AF_INET) { 1393 if (setsockopt(ssock, IPPROTO_IP, IP_DONTFRAG, 1394 (char *)&dontfrag, sizeof (dontfrag)) == -1) { 1395 Fprintf(stderr, "%s: IP_DONTFRAG %s\n", prog, 1396 strerror(errno)); 1397 exit(EXIT_FAILURE); 1398 } 1399 } else { 1400 if (setsockopt(ssock, IPPROTO_IPV6, IPV6_DONTFRAG, 1401 (char *)&dontfrag, sizeof (dontfrag)) == -1) { 1402 Fprintf(stderr, "%s: IPV6_DONTFRAG %s\n", prog, 1403 strerror(errno)); 1404 exit(EXIT_FAILURE); 1405 } 1406 } 1407 1408 if (pr->family == AF_INET) { 1409 rcvsock4 = rsock; 1410 sndsock4 = ssock; 1411 } else { 1412 rcvsock6 = rsock; 1413 sndsock6 = ssock; 1414 } 1415 /* Revert to non-privileged user after configuring sockets */ 1416 (void) __priv_bracket(PRIV_OFF); 1417 } 1418 1419 /* 1420 * If we are "probing all", this function calls traceroute() for each IP address 1421 * of the target, otherwise calls only once. Returns _B_FALSE if traceroute() 1422 * fails. 1423 */ 1424 static void 1425 trace_it(struct addrinfo *ai_dst) 1426 { 1427 struct msghdr msg6; 1428 int num_dst_IPaddrs; 1429 struct addrinfo *aip; 1430 int i; 1431 1432 if (!probe_all) 1433 num_dst_IPaddrs = 1; 1434 else 1435 num_dst_IPaddrs = num_v4 + num_v6; 1436 1437 /* 1438 * Initialize the msg6 structure using the hoplimit for the first 1439 * probe packet, gateway addresses and the outgoing interface index. 1440 */ 1441 if (ai_dst->ai_family == AF_INET6 || (probe_all && num_v6)) { 1442 msg6.msg_control = NULL; 1443 msg6.msg_controllen = 0; 1444 set_ancillary_data(&msg6, first_ttl, pr6->gwIPlist, gw_count, 1445 if_index); 1446 } 1447 1448 /* run traceroute for all the IP addresses of the multihomed dest */ 1449 for (aip = ai_dst, i = 0; i < num_dst_IPaddrs && aip != NULL; i++) { 1450 union any_in_addr *addrp; 1451 if (aip->ai_family == AF_INET) { 1452 addrp = (union any_in_addr *) 1453 /* LINTED E_BAD_PTR_CAST_ALIGN */ 1454 &((struct sockaddr_in *) 1455 aip->ai_addr)->sin_addr; 1456 set_sin((struct sockaddr *)pr4->to, addrp, 1457 aip->ai_family); 1458 traceroute(addrp, &msg6, pr4, num_ifs4, al4); 1459 } else { 1460 addrp = (union any_in_addr *) 1461 /* LINTED E_BAD_PTR_CAST_ALIGN */ 1462 &((struct sockaddr_in6 *) 1463 aip->ai_addr)->sin6_addr; 1464 set_sin((struct sockaddr *)pr6->to, addrp, 1465 aip->ai_family); 1466 traceroute(addrp, &msg6, pr6, num_ifs6, al6); 1467 } 1468 aip = aip->ai_next; 1469 if (i < (num_dst_IPaddrs - 1)) 1470 (void) putchar('\n'); 1471 } 1472 } 1473 1474 /* 1475 * set the IP address in a sockaddr struct 1476 */ 1477 static void 1478 set_sin(struct sockaddr *sock, union any_in_addr *addr, int family) 1479 { 1480 sock->sa_family = family; 1481 1482 if (family == AF_INET) 1483 /* LINTED E_BAD_PTR_CAST_ALIGN */ 1484 ((struct sockaddr_in *)sock)->sin_addr = addr->addr; 1485 else 1486 /* LINTED E_BAD_PTR_CAST_ALIGN */ 1487 ((struct sockaddr_in6 *)sock)->sin6_addr = addr->addr6; 1488 } 1489 1490 /* 1491 * returns the IF name on which the given IP address is configured 1492 */ 1493 static char * 1494 device_name(struct ifaddrlist *al, int len, union any_in_addr *ip_addr, 1495 struct pr_set *pr) 1496 { 1497 int i; 1498 struct ifaddrlist *tmp_al; 1499 1500 tmp_al = al; 1501 1502 for (i = 0; i < len; i++, tmp_al++) { 1503 if (memcmp(&tmp_al->addr, ip_addr, pr->addr_len) == 0) { 1504 return (tmp_al->device); 1505 } 1506 } 1507 1508 return (NULL); 1509 } 1510 1511 /* 1512 * Trace the route to the host with given IP address. 1513 */ 1514 static void 1515 traceroute(union any_in_addr *ip_addr, struct msghdr *msg6, struct pr_set *pr, 1516 int num_ifs, struct ifaddrlist *al) 1517 { 1518 int ttl; 1519 int probe; 1520 uchar_t type; /* icmp type */ 1521 uchar_t code; /* icmp code */ 1522 int reply; 1523 int seq = 0; 1524 char abuf[INET6_ADDRSTRLEN]; /* use for inet_ntop() */ 1525 int longjmp_return; /* return value from longjump */ 1526 struct ip *ip = (struct ip *)packet; 1527 boolean_t got_there = _B_FALSE; /* we hit the destination */ 1528 static boolean_t first_pkt = _B_TRUE; 1529 int hoplimit; /* hoplimit for IPv6 packets */ 1530 struct in6_addr addr6; 1531 int num_src_ifs; /* excludes down and loopback */ 1532 struct msghdr in_msg; 1533 struct iovec iov; 1534 int *intp; 1535 int sndsock; 1536 int rcvsock; 1537 1538 msg6->msg_name = pr->to; 1539 msg6->msg_namelen = sizeof (struct sockaddr_in6); 1540 sndsock = (pr->family == AF_INET) ? sndsock4 : sndsock6; 1541 rcvsock = (pr->family == AF_INET) ? rcvsock4 : rcvsock6; 1542 1543 /* carry out the source address selection */ 1544 if (pick_src) { 1545 union any_in_addr src_addr; 1546 char *dev_name; 1547 int i; 1548 1549 /* 1550 * If there's a gateway, a routing header as a consequence, our 1551 * kernel picks the source address based on the first hop 1552 * address, rather than final destination address. 1553 */ 1554 if (gw_count > 0) { 1555 (void) select_src_addr(pr->gwIPlist, &src_addr, 1556 pr->family); 1557 } else { 1558 (void) select_src_addr(ip_addr, &src_addr, pr->family); 1559 } 1560 set_sin(pr->from, &src_addr, pr->family); 1561 1562 /* filter out down and loopback interfaces */ 1563 num_src_ifs = 0; 1564 for (i = 0; i < num_ifs; i++) { 1565 if (!(al[i].flags & IFF_LOOPBACK) && 1566 (al[i].flags & IFF_UP)) 1567 num_src_ifs++; 1568 } 1569 1570 if (num_src_ifs > 1) { 1571 dev_name = device_name(al, num_ifs, &src_addr, pr); 1572 if (dev_name == NULL) 1573 dev_name = "?"; 1574 1575 (void) inet_ntop(pr->family, pr->from_sin_addr, abuf, 1576 sizeof (abuf)); 1577 Fprintf(stderr, 1578 "%s: Warning: Multiple interfaces found;" 1579 " using %s @ %s\n", prog, abuf, dev_name); 1580 } 1581 } 1582 1583 if (pr->family == AF_INET) { 1584 outip4->ip_src = *(struct in_addr *)pr->from_sin_addr; 1585 outip4->ip_dst = ip_addr->addr; 1586 } 1587 1588 /* 1589 * If the hostname is an IPv6 literal address, let's not print it twice. 1590 */ 1591 if (pr->family == AF_INET6 && 1592 inet_pton(AF_INET6, hostname, &addr6) > 0) { 1593 Fprintf(stderr, "%s to %s", prog, hostname); 1594 } else { 1595 Fprintf(stderr, "%s to %s (%s)", prog, hostname, 1596 inet_ntop(pr->family, ip_addr, abuf, sizeof (abuf))); 1597 } 1598 1599 if (source) 1600 Fprintf(stderr, " from %s", source); 1601 Fprintf(stderr, ", %d hops max, %d byte packets\n", max_ttl, 1602 pr->packlen); 1603 (void) fflush(stderr); 1604 1605 /* 1606 * Setup the source routing for IPv4. For IPv6, we did the required 1607 * setup in the caller function, trace_it(), because it's independent 1608 * from the IP address of target. 1609 */ 1610 if (pr->family == AF_INET && gw_count > 0) 1611 set_IPv4opt_sourcerouting(sndsock, ip_addr, pr->gwIPlist); 1612 1613 if (probe_all) { 1614 /* interrupt handler sig_handler() jumps back to here */ 1615 if ((longjmp_return = setjmp(env)) != 0) { 1616 switch (longjmp_return) { 1617 case SIGINT: 1618 Printf("(skipping)\n"); 1619 return; 1620 case SIGQUIT: 1621 Printf("(exiting)\n"); 1622 exit(EXIT_SUCCESS); 1623 default: /* should never happen */ 1624 exit(EXIT_FAILURE); 1625 } 1626 } 1627 (void) signal(SIGINT, sig_handler); 1628 } 1629 1630 for (ttl = first_ttl; ttl <= max_ttl; ++ttl) { 1631 union any_in_addr lastaddr; 1632 int timeouts = 0; 1633 double rtt; /* for statistics */ 1634 int nreceived = 0; 1635 double rttmin, rttmax; 1636 double rttsum, rttssq; 1637 int unreachable; 1638 1639 got_there = _B_FALSE; 1640 unreachable = 0; 1641 1642 /* 1643 * The following line clears both IPv4 and IPv6 address stored 1644 * in the union. 1645 */ 1646 lastaddr.addr6 = in6addr_any; 1647 1648 if ((ttl == (first_ttl + 1)) && (options & SO_DONTROUTE)) { 1649 Fprintf(stderr, 1650 "%s: host %s is not on a directly-attached" 1651 " network\n", prog, hostname); 1652 break; 1653 } 1654 1655 Printf("%2d ", ttl); 1656 (void) fflush(stdout); 1657 1658 for (probe = 0; (probe < nprobes) && (timeouts < max_timeout); 1659 ++probe) { 1660 int cc; 1661 struct timeval t1, t2; 1662 1663 /* 1664 * Put a delay before sending this probe packet. Don't 1665 * delay it if it's the very first packet. 1666 */ 1667 if (!first_pkt) { 1668 if (delay.tv_sec > 0) 1669 (void) sleep((uint_t)delay.tv_sec); 1670 if (delay.tv_usec > 0) 1671 (void) usleep(delay.tv_usec); 1672 } else { 1673 first_pkt = _B_FALSE; 1674 } 1675 1676 (void) gettimeofday(&t1, NULL); 1677 1678 if (pr->family == AF_INET) { 1679 send_probe(sndsock, pr->to, outip4, seq, ttl, 1680 &t1, pr->packlen); 1681 } else { 1682 send_probe6(sndsock, msg6, outip6, seq, ttl, 1683 &t1, pr->packlen); 1684 } 1685 1686 /* prepare msghdr for recvmsg() */ 1687 in_msg.msg_name = pr->from; 1688 in_msg.msg_namelen = pr->sock_size; 1689 1690 iov.iov_base = (char *)packet; 1691 iov.iov_len = sizeof (packet); 1692 1693 in_msg.msg_iov = &iov; 1694 in_msg.msg_iovlen = 1; 1695 1696 in_msg.msg_control = ancillary_data; 1697 in_msg.msg_controllen = sizeof (ancillary_data); 1698 1699 while ((cc = wait_for_reply(rcvsock, &in_msg, 1700 &t1)) != 0) { 1701 (void) gettimeofday(&t2, NULL); 1702 1703 reply = (*pr->check_reply_fn) (&in_msg, cc, seq, 1704 &type, &code); 1705 1706 in_msg.msg_controllen = 1707 sizeof (ancillary_data); 1708 /* Skip short packet */ 1709 if (reply == REPLY_SHORT_PKT) { 1710 continue; 1711 } 1712 1713 timeouts = 0; 1714 1715 /* 1716 * if reply comes from a different host, print 1717 * the hostname 1718 */ 1719 if (memcmp(pr->from_sin_addr, &lastaddr, 1720 pr->addr_len) != 0) { 1721 (*pr->print_addr_fn) ((uchar_t *)packet, 1722 cc, pr->from); 1723 /* store the address response */ 1724 (void) memcpy(&lastaddr, 1725 pr->from_sin_addr, pr->addr_len); 1726 } 1727 1728 rtt = deltaT(&t1, &t2); 1729 if (collect_stat) { 1730 record_stats(rtt, &nreceived, &rttmin, 1731 &rttmax, &rttsum, &rttssq); 1732 } else { 1733 Printf(" %.3f ms", rtt); 1734 } 1735 1736 if (pr->family == AF_INET6) { 1737 intp = find_ancillary_data(&in_msg, 1738 IPPROTO_IPV6, IPV6_HOPLIMIT); 1739 if (intp == NULL) { 1740 Fprintf(stderr, 1741 "%s: can't find " 1742 "IPV6_HOPLIMIT ancillary " 1743 "data\n", prog); 1744 exit(EXIT_FAILURE); 1745 } 1746 hoplimit = *intp; 1747 } 1748 1749 if (reply == REPLY_GOT_TARGET) { 1750 got_there = _B_TRUE; 1751 1752 if (((pr->family == AF_INET) && 1753 (ip->ip_ttl <= 1)) || 1754 ((pr->family == AF_INET6) && 1755 (hoplimit <= 1))) 1756 Printf(" !"); 1757 } 1758 1759 if (!collect_stat && showttl) { 1760 if (pr->family == AF_INET) { 1761 Printf(" (ttl=%d)", 1762 (int)ip->ip_ttl); 1763 } else if (hoplimit != -1) { 1764 Printf(" (hop limit=%d)", 1765 hoplimit); 1766 } 1767 } 1768 1769 if (reply == REPLY_GOT_OTHER) { 1770 if ((*pr->print_icmp_other_fn) 1771 (type, code)) { 1772 unreachable++; 1773 } 1774 } 1775 1776 /* special case */ 1777 if (pr->family == AF_INET && 1778 type == ICMP_UNREACH && 1779 code == ICMP_UNREACH_PROTOCOL) 1780 got_there = _B_TRUE; 1781 1782 break; 1783 } 1784 1785 seq = (seq + 1) % (MAX_SEQ + 1); 1786 1787 if (cc == 0) { 1788 Printf(" *"); 1789 timeouts++; 1790 } 1791 1792 (void) fflush(stdout); 1793 } 1794 1795 if (collect_stat) { 1796 print_stats(probe, nreceived, rttmin, rttmax, rttsum, 1797 rttssq); 1798 } 1799 1800 (void) putchar('\n'); 1801 1802 /* either we hit the target or received too many unreachables */ 1803 if (got_there || 1804 (unreachable > 0 && unreachable >= nprobes - 1)) 1805 break; 1806 } 1807 1808 /* Ignore the SIGINT between traceroute() runs */ 1809 if (probe_all) 1810 (void) signal(SIGINT, SIG_IGN); 1811 } 1812 1813 /* 1814 * for a given destination address and address family, it finds out what 1815 * source address kernel is going to pick 1816 */ 1817 static void 1818 select_src_addr(union any_in_addr *dst_addr, union any_in_addr *src_addr, 1819 int family) 1820 { 1821 int tmp_fd; 1822 struct sockaddr *sock; 1823 struct sockaddr_in *sin; 1824 struct sockaddr_in6 *sin6; 1825 size_t sock_len; 1826 1827 sock = (struct sockaddr *)malloc(sizeof (struct sockaddr_in6)); 1828 if (sock == NULL) { 1829 Fprintf(stderr, "%s: malloc %s\n", prog, strerror(errno)); 1830 exit(EXIT_FAILURE); 1831 } 1832 (void) bzero(sock, sizeof (struct sockaddr_in6)); 1833 1834 if (family == AF_INET) { 1835 /* LINTED E_BAD_PTR_CAST_ALIGN */ 1836 sin = (struct sockaddr_in *)sock; 1837 sin->sin_family = AF_INET; 1838 sin->sin_addr = dst_addr->addr; 1839 sin->sin_port = IPPORT_ECHO; /* port shouldn't be 0 */ 1840 sock_len = sizeof (struct sockaddr_in); 1841 } else { 1842 /* LINTED E_BAD_PTR_CAST_ALIGN */ 1843 sin6 = (struct sockaddr_in6 *)sock; 1844 sin6->sin6_family = AF_INET6; 1845 sin6->sin6_addr = dst_addr->addr6; 1846 sin6->sin6_port = IPPORT_ECHO; /* port shouldn't be 0 */ 1847 sock_len = sizeof (struct sockaddr_in6); 1848 } 1849 1850 /* open a UDP socket */ 1851 if ((tmp_fd = socket(family, SOCK_DGRAM, 0)) < 0) { 1852 Fprintf(stderr, "%s: udp socket: %s\n", prog, 1853 strerror(errno)); 1854 exit(EXIT_FAILURE); 1855 } 1856 1857 /* connect it */ 1858 if (connect(tmp_fd, sock, sock_len) < 0) { 1859 /* 1860 * If there's no route to the destination, this connect() call 1861 * fails. We just return all-zero (wildcard) as the source 1862 * address, so that user can get to see "no route to dest" 1863 * message, as it'll try to send the probe packet out and will 1864 * receive ICMP unreachable. 1865 */ 1866 if (family == AF_INET) 1867 src_addr->addr.s_addr = INADDR_ANY; 1868 else 1869 src_addr->addr6 = in6addr_any; 1870 free(sock); 1871 return; 1872 } 1873 1874 /* get the local sock info */ 1875 if (getsockname(tmp_fd, sock, &sock_len) < 0) { 1876 Fprintf(stderr, "%s: getsockname: %s\n", prog, 1877 strerror(errno)); 1878 exit(EXIT_FAILURE); 1879 } 1880 1881 if (family == AF_INET) { 1882 /* LINTED E_BAD_PTR_CAST_ALIGN */ 1883 sin = (struct sockaddr_in *)sock; 1884 src_addr->addr = sin->sin_addr; 1885 } else { 1886 /* LINTED E_BAD_PTR_CAST_ALIGN */ 1887 sin6 = (struct sockaddr_in6 *)sock; 1888 src_addr->addr6 = sin6->sin6_addr; 1889 } 1890 1891 free(sock); 1892 (void) close(tmp_fd); 1893 } 1894 1895 /* 1896 * Checksum routine for Internet Protocol family headers (C Version) 1897 */ 1898 ushort_t 1899 in_cksum(ushort_t *addr, int len) 1900 { 1901 int nleft = len; 1902 ushort_t *w = addr; 1903 ushort_t answer; 1904 int sum = 0; 1905 1906 /* 1907 * Our algorithm is simple, using a 32 bit accumulator (sum), 1908 * we add sequential 16 bit words to it, and at the end, fold 1909 * back all the carry bits from the top 16 bits into the lower 1910 * 16 bits. 1911 */ 1912 while (nleft > 1) { 1913 sum += *w++; 1914 nleft -= 2; 1915 } 1916 1917 /* mop up an odd byte, if necessary */ 1918 if (nleft == 1) 1919 sum += *(uchar_t *)w; 1920 1921 /* add back carry outs from top 16 bits to low 16 bits */ 1922 sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */ 1923 sum += (sum >> 16); /* add carry */ 1924 answer = ~sum; /* truncate to 16 bits */ 1925 return (answer); 1926 } 1927 1928 /* 1929 * Wait until a reply arrives or timeout occurs. If packet arrived, read it 1930 * return the size of the packet read. 1931 */ 1932 static int 1933 wait_for_reply(int sock, struct msghdr *msg, struct timeval *tp) 1934 { 1935 fd_set fds; 1936 struct timeval now, wait; 1937 int cc = 0; 1938 int result; 1939 1940 (void) FD_ZERO(&fds); 1941 FD_SET(sock, &fds); 1942 1943 wait.tv_sec = tp->tv_sec + waittime; 1944 wait.tv_usec = tp->tv_usec; 1945 (void) gettimeofday(&now, NULL); 1946 tv_sub(&wait, &now); 1947 1948 if (wait.tv_sec < 0 || wait.tv_usec < 0) 1949 return (0); 1950 1951 result = select(sock + 1, &fds, (fd_set *)NULL, (fd_set *)NULL, &wait); 1952 1953 if (result == -1) { 1954 if (errno != EINTR) { 1955 Fprintf(stderr, "%s: select: %s\n", prog, 1956 strerror(errno)); 1957 } 1958 } else if (result > 0) 1959 cc = recvmsg(sock, msg, 0); 1960 1961 return (cc); 1962 } 1963 1964 /* 1965 * Construct an Internet address representation. If the nflag has been supplied, 1966 * give numeric value, otherwise try for symbolic name. 1967 */ 1968 char * 1969 inet_name(union any_in_addr *in, int family) 1970 { 1971 char *cp; 1972 static boolean_t first = _B_TRUE; 1973 static char domain[NI_MAXHOST + 1]; 1974 static char line[NI_MAXHOST + 1]; /* assuming */ 1975 /* (NI_MAXHOST + 1) >= INET6_ADDRSTRLEN */ 1976 char hbuf[NI_MAXHOST]; 1977 socklen_t slen; 1978 struct sockaddr_in sin; 1979 struct sockaddr_in6 sin6; 1980 struct sockaddr *sa; 1981 int flags; 1982 1983 switch (family) { 1984 case AF_INET: 1985 slen = sizeof (struct sockaddr_in); 1986 sin.sin_addr = in->addr; 1987 sin.sin_port = 0; 1988 sa = (struct sockaddr *)&sin; 1989 break; 1990 case AF_INET6: 1991 slen = sizeof (struct sockaddr_in6); 1992 sin6.sin6_addr = in->addr6; 1993 sin6.sin6_port = 0; 1994 sin6.sin6_scope_id = 0; 1995 sa = (struct sockaddr *)&sin6; 1996 break; 1997 default: 1998 (void) snprintf(line, sizeof (line), 1999 "<invalid address family>"); 2000 return (line); 2001 } 2002 sa->sa_family = family; 2003 2004 if (first && !nflag) { 2005 /* find out the domain name */ 2006 first = _B_FALSE; 2007 mutex_enter(&tr_nslock); 2008 tr_nsactive = _B_TRUE; 2009 tr_nsstarttime = gethrtime(); 2010 mutex_exit(&tr_nslock); 2011 if (gethostname(domain, MAXHOSTNAMELEN) == 0 && 2012 (cp = strchr(domain, '.')) != NULL) { 2013 (void) strncpy(domain, cp + 1, sizeof (domain) - 1); 2014 domain[sizeof (domain) - 1] = '\0'; 2015 } else { 2016 domain[0] = '\0'; 2017 } 2018 mutex_enter(&tr_nslock); 2019 tr_nsactive = _B_FALSE; 2020 mutex_exit(&tr_nslock); 2021 } 2022 2023 flags = (nflag) ? NI_NUMERICHOST : NI_NAMEREQD; 2024 mutex_enter(&tr_nslock); 2025 tr_nsactive = _B_TRUE; 2026 tr_nsstarttime = gethrtime(); 2027 mutex_exit(&tr_nslock); 2028 if (getnameinfo(sa, slen, hbuf, sizeof (hbuf), NULL, 0, flags) != 0) { 2029 if (inet_ntop(family, (const void *)&in->addr6, 2030 hbuf, sizeof (hbuf)) == NULL) 2031 hbuf[0] = 0; 2032 } else if (!nflag && (cp = strchr(hbuf, '.')) != NULL && 2033 strcmp(cp + 1, domain) == 0) { 2034 *cp = '\0'; 2035 } 2036 mutex_enter(&tr_nslock); 2037 tr_nsactive = _B_FALSE; 2038 mutex_exit(&tr_nslock); 2039 (void) strlcpy(line, hbuf, sizeof (line)); 2040 2041 return (line); 2042 } 2043 2044 /* 2045 * return the difference (in msec) between two time values 2046 */ 2047 static double 2048 deltaT(struct timeval *t1p, struct timeval *t2p) 2049 { 2050 double dt; 2051 2052 dt = (double)(t2p->tv_sec - t1p->tv_sec) * 1000.0 + 2053 (double)(t2p->tv_usec - t1p->tv_usec) / 1000.0; 2054 return (dt); 2055 } 2056 2057 /* 2058 * Subtract 2 timeval structs: out = out - in. 2059 * Out is assumed to be >= in. 2060 */ 2061 static void 2062 tv_sub(struct timeval *out, struct timeval *in) 2063 { 2064 if ((out->tv_usec -= in->tv_usec) < 0) { 2065 --out->tv_sec; 2066 out->tv_usec += 1000000; 2067 } 2068 out->tv_sec -= in->tv_sec; 2069 } 2070 2071 /* 2072 * record statistics 2073 */ 2074 static void 2075 record_stats(double rtt, int *nreceived, double *rttmin, double *rttmax, 2076 double *rttsum, double *rttssq) 2077 { 2078 if (*nreceived == 0) { 2079 *rttmin = rtt; 2080 *rttmax = rtt; 2081 *rttsum = rtt; 2082 *rttssq = rtt * rtt; 2083 } else { 2084 if (rtt < *rttmin) 2085 *rttmin = rtt; 2086 2087 if (rtt > *rttmax) 2088 *rttmax = rtt; 2089 2090 *rttsum += rtt; 2091 *rttssq += rtt * rtt; 2092 } 2093 2094 (*nreceived)++; 2095 } 2096 2097 /* 2098 * display statistics 2099 */ 2100 static void 2101 print_stats(int ntransmitted, int nreceived, double rttmin, double rttmax, 2102 double rttsum, double rttssq) 2103 { 2104 double rttavg; /* average round-trip time */ 2105 double rttstd; /* rtt standard deviation */ 2106 2107 if (ntransmitted > 0 && ntransmitted >= nreceived) { 2108 int missed = ntransmitted - nreceived; 2109 double loss = 100 * (double)missed / (double)ntransmitted; 2110 2111 if (nreceived > 0) { 2112 rttavg = rttsum / nreceived; 2113 rttstd = rttssq - (rttavg * rttsum); 2114 rttstd = xsqrt(rttstd / nreceived); 2115 2116 Printf(" %.3f", rttmin); 2117 Printf("/%.3f", rttavg); 2118 Printf("/%.3f", rttmax); 2119 2120 Printf(" (%.3f) ms ", rttstd); 2121 } 2122 2123 Printf(" %d/%d pkts", nreceived, ntransmitted); 2124 2125 if (nreceived == 0) 2126 Printf(" (100%% loss)"); 2127 else 2128 Printf(" (%.2g%% loss)", loss); 2129 } 2130 } 2131 2132 /* 2133 * square root function 2134 */ 2135 double 2136 xsqrt(double y) 2137 { 2138 double t, x; 2139 2140 if (y <= 0) { 2141 return (0.0); 2142 } 2143 2144 x = (y < 1.0) ? 1.0 : y; 2145 do { 2146 t = x; 2147 x = (t + (y/t))/2.0; 2148 } while (0 < x && x < t); 2149 2150 return (x); 2151 } 2152 2153 /* 2154 * String to double with optional min and max. 2155 */ 2156 static double 2157 str2dbl(const char *str, const char *what, double mi, double ma) 2158 { 2159 double val; 2160 char *ep; 2161 2162 errno = 0; 2163 2164 val = strtod(str, &ep); 2165 if (errno != 0 || *ep != '\0') { 2166 Fprintf(stderr, "%s: \"%s\" bad value for %s \n", 2167 prog, str, what); 2168 exit(EXIT_FAILURE); 2169 } 2170 if (val < mi && mi >= 0) { 2171 Fprintf(stderr, "%s: %s must be >= %f\n", prog, what, mi); 2172 exit(EXIT_FAILURE); 2173 } 2174 if (val > ma && ma >= 0) { 2175 Fprintf(stderr, "%s: %s must be <= %f\n", prog, what, ma); 2176 exit(EXIT_FAILURE); 2177 } 2178 return (val); 2179 } 2180 2181 /* 2182 * String to int with optional min and max. Handles decimal and hex. 2183 */ 2184 static int 2185 str2int(const char *str, const char *what, int mi, int ma) 2186 { 2187 const char *cp; 2188 int val; 2189 char *ep; 2190 2191 errno = 0; 2192 2193 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) { 2194 cp = str + 2; 2195 val = (int)strtol(cp, &ep, 16); 2196 } else { 2197 val = (int)strtol(str, &ep, 10); 2198 } 2199 if (errno != 0 || *ep != '\0') { 2200 Fprintf(stderr, "%s: \"%s\" bad value for %s \n", 2201 prog, str, what); 2202 exit(EXIT_FAILURE); 2203 } 2204 if (val < mi && mi >= 0) { 2205 if (mi == 0) { 2206 Fprintf(stderr, "%s: %s must be >= %d\n", 2207 prog, what, mi); 2208 } else { 2209 Fprintf(stderr, "%s: %s must be > %d\n", 2210 prog, what, mi - 1); 2211 } 2212 exit(EXIT_FAILURE); 2213 } 2214 if (val > ma && ma >= 0) { 2215 Fprintf(stderr, "%s: %s must be <= %d\n", prog, what, ma); 2216 exit(EXIT_FAILURE); 2217 } 2218 return (val); 2219 } 2220 2221 /* 2222 * This is the interrupt handler for SIGINT and SIGQUIT. It's completely handled 2223 * where it jumps to. 2224 */ 2225 static void 2226 sig_handler(int sig) 2227 { 2228 longjmp(env, sig); 2229 } 2230 2231 /* 2232 * display the usage of traceroute 2233 */ 2234 static void 2235 usage(void) 2236 { 2237 Fprintf(stderr, "Usage: %s [-adFIlnSvx] [-A address_family] " 2238 "[-c traffic_class]\n" 2239 "\t[-f first_hop] [-g gateway [-g gateway ...]| -r] [-i iface]\n" 2240 "\t[-L flow_label] [-m max_hop] [-P pause_sec] [-p port] " 2241 "[-Q max_timeout]\n" 2242 "\t[-q nqueries] [-s src_addr] [-t tos] [-w wait_time] host " 2243 "[packetlen]\n", prog); 2244 exit(EXIT_FAILURE); 2245 } 2246 2247 /* ARGSUSED */ 2248 static void * 2249 ns_warning_thr(void *unused) 2250 { 2251 for (;;) { 2252 hrtime_t now; 2253 2254 (void) sleep(tr_nssleeptime); 2255 2256 now = gethrtime(); 2257 mutex_enter(&tr_nslock); 2258 if (tr_nsactive && now - tr_nsstarttime >= 2259 tr_nswarntime * NANOSEC) { 2260 Fprintf(stderr, "%s: warning: responses " 2261 "received, but name service lookups are " 2262 "taking a while. Use %s -n to disable " 2263 "name service lookups.\n", 2264 prog, prog); 2265 mutex_exit(&tr_nslock); 2266 return (NULL); 2267 } 2268 mutex_exit(&tr_nslock); 2269 } 2270 2271 /* LINTED: E_STMT_NOT_REACHED */ 2272 return (NULL); 2273 } 2274