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