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