xref: /illumos-gate/usr/src/cmd/cmd-inet/usr.sbin/traceroute/traceroute.c (revision b55148877d473978f0b46d593fd6213fa526fcc5)
1 /*
2  * Copyright 2005 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 = (num_v4 + num_v6 > 0) ? ai_dst : NULL;
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 		*aipp = NULL;
752 		return;
753 	}
754 	*aipp = ai;
755 }
756 
757 /*
758  * Calculate the packet length to be used, and check against the valid range.
759  * Returns -1 if range check fails.
760  */
761 static uint_t
762 calc_packetlen(int plen_input, struct pr_set *pr)
763 {
764 	int minpacket;			/* min ip packet size */
765 	int optlen;			/* length of ip options */
766 	int plen;
767 
768 	/*
769 	 * LBNL bug fixed: miscalculation of optlen
770 	 */
771 	if (gw_count > 0) {
772 		/*
773 		 * IPv4:
774 		 * ----
775 		 * 5 (NO OPs) + 3 (code, len, ptr) + gateways
776 		 * IP options field can hold up to 9 gateways. But the API
777 		 * allows you to specify only 8, because the last one is the
778 		 * destination host. When this packet is sent, on the wire
779 		 * you see one gateway replaced by 4 NO OPs. The other 1 NO
780 		 * OP is for alignment
781 		 *
782 		 * IPv6:
783 		 * ----
784 		 * Well, formula is different, but the result is same.
785 		 * 8 byte fixed part for Type 0 Routing header, followed by
786 		 * gateway addresses
787 		 */
788 		optlen = 8 + gw_count * pr->addr_len;
789 	} else {
790 		optlen = 0;
791 	}
792 
793 	/* take care of the packet length calculations and checks */
794 	minpacket = pr->ip_hdr_len + sizeof (struct outdata) + optlen;
795 	if (useicmp)
796 		minpacket += pr->icmp_minlen;	/* minimum ICMP header size */
797 	else
798 		minpacket += sizeof (struct udphdr);
799 	plen = plen_input;
800 	if (plen == 0) {
801 		plen = minpacket;		/* minimum sized packet */
802 	} else if (minpacket > plen || plen > IP_MAXPACKET) {
803 		Fprintf(stderr, "%s: %s packet size must be >= %d and <= %d\n",
804 		    prog, pr->name, minpacket, IP_MAXPACKET);
805 		return (0);
806 	}
807 
808 	return (plen);
809 }
810 
811 /*
812  * Sets the source address by resolving -i and -s arguments, or if -i and -s
813  * don't dictate any, it sets the pick_src to make sure traceroute uses the
814  * kernel's pick of the source address.
815  * Returns number of interfaces configured on the source host, 0 on error or
816  * there's no interface which is up amd not a loopback.
817  */
818 static int
819 set_src_addr(struct pr_set *pr, struct ifaddrlist **alp)
820 {
821 	union any_in_addr *ap;
822 	struct ifaddrlist *al = NULL;
823 	struct ifaddrlist *tmp1_al = NULL;
824 	struct ifaddrlist *tmp2_al = NULL;
825 	/* LINTED E_BAD_PTR_CAST_ALIGN */
826 	struct sockaddr_in *sin_from = (struct sockaddr_in *)pr->from;
827 	/* LINTED E_BAD_PTR_CAST_ALIGN */
828 	struct sockaddr_in6 *sin6_from = (struct sockaddr_in6 *)pr->from;
829 	struct addrinfo *aip;
830 	char errbuf[ERRBUFSIZE];
831 	char temp_buf[INET6_ADDRSTRLEN];	/* use for inet_ntop() */
832 	int num_ifs;				/* all the interfaces  */
833 	int num_src_ifs;			/* exclude loopback and down */
834 	int i;
835 
836 	source = source_input;
837 
838 	/* get the interface address list */
839 	num_ifs = ifaddrlist(&al, pr->family, errbuf);
840 	if (num_ifs < 0) {
841 		Fprintf(stderr, "%s: ifaddrlist: %s\n", prog, errbuf);
842 		exit(EXIT_FAILURE);
843 	}
844 
845 	num_src_ifs = 0;
846 	for (i = 0; i < num_ifs; i++) {
847 		if (!(al[i].flags & IFF_LOOPBACK) && (al[i].flags & IFF_UP))
848 			num_src_ifs++;
849 	}
850 
851 	if (num_src_ifs == 0) {
852 		Fprintf(stderr, "%s: can't find any %s network interfaces\n",
853 		    prog, pr->name);
854 		return (0);
855 	}
856 
857 	/* verify the device */
858 	if (device != NULL) {
859 		tmp1_al = find_device(al, num_ifs, device);
860 
861 		if (tmp1_al == NULL) {
862 			Fprintf(stderr, "%s: %s (index %d) is an invalid %s"
863 			    " interface\n", prog, device, if_index, pr->name);
864 			free(al);
865 			return (0);
866 		}
867 	}
868 
869 	/* verify the source address */
870 	if (source != NULL) {
871 		get_hostinfo(source, pr->family, &aip);
872 		if (aip == NULL) {
873 			Fprintf(stderr,
874 			    "%s: %s is an invalid %s source address\n",
875 			    prog, source, pr->name);
876 
877 			free(al);
878 			return (0);
879 		}
880 
881 		source = aip->ai_canonname;
882 		ap = (union any_in_addr *)
883 		    /* LINTED E_BAD_PTR_CAST_ALIGN */
884 		    &((struct sockaddr_in6 *)
885 		    aip->ai_addr)->sin6_addr;
886 
887 		/*
888 		 * LBNL bug fixed: used to accept any src address
889 		 */
890 		tmp2_al = find_ifaddr(al, num_ifs, ap, pr->family);
891 
892 		if (tmp2_al == NULL) {
893 			Fprintf(stderr,
894 			    "%s: %s is an invalid %s source address\n", prog,
895 			    inet_ntop(pr->family, (const void *)ap,
896 				temp_buf, sizeof (temp_buf)),
897 			    pr->name);
898 
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 				Fprintf(stderr,
927 				    "%s: Warning: %s has multiple "
928 				    "addresses; using %s\n",
929 				    prog, source,
930 				    inet_ntop(pr->family,
931 					(const void *)pr->from_sin_addr,
932 					temp_buf, sizeof (temp_buf)));
933 			}
934 		} else {			/* -i and -s used */
935 			/*
936 			 * Make sure the source specified matches the
937 			 * interface address. You only care about this for IPv4
938 			 * IPv6 can handle IF not matching src address
939 			 */
940 			if (pr->family == AF_INET) {
941 				if (!has_addr(aip, &tmp1_al->addr)) {
942 					Fprintf(stderr,
943 					    "%s: %s is not on interface %s\n",
944 					    prog, source, device);
945 					exit(EXIT_FAILURE);
946 				}
947 				/*
948 				 * make sure we use the one matching the
949 				 * interface's address
950 				 */
951 				*ap = tmp1_al->addr;
952 			}
953 
954 			set_sin(pr->from, ap, pr->family);
955 		}
956 	}
957 
958 	/*
959 	 * Binding at this point will set the source address to be used
960 	 * for both IPv4 (when raw IP datagrams are not required) and
961 	 * IPv6.  If the address being bound to is zero, then the kernel
962 	 * will end up choosing the source address when the datagram is
963 	 * sent.
964 	 *
965 	 * For raw IPv4 datagrams, the source address is initialized
966 	 * within traceroute() along with the outbound destination
967 	 * address.
968 	 */
969 	if (pr->family == AF_INET && !raw_req) {
970 		sin_from->sin_family = AF_INET;
971 		sin_from->sin_port = htons(ident);
972 		if (bind(sndsock4, (struct sockaddr *)pr->from,
973 			sizeof (struct sockaddr_in)) < 0) {
974 			Fprintf(stderr, "%s: bind: %s\n", prog,
975 			    strerror(errno));
976 			exit(EXIT_FAILURE);
977 		}
978 	} else if (pr->family == AF_INET6) {
979 		sin6_from->sin6_family = AF_INET6;
980 		sin6_from->sin6_port = htons(ident);
981 		if (bind(sndsock6, (struct sockaddr *)pr->from,
982 			sizeof (struct sockaddr_in6)) < 0) {
983 			Fprintf(stderr, "%s: bind: %s\n", prog,
984 			    strerror(errno));
985 			exit(EXIT_FAILURE);
986 		}
987 
988 		whereto6.sin6_flowinfo = htonl((class << 20) | flow);
989 	}
990 	*alp = al;
991 	return (num_ifs);
992 }
993 
994 /*
995  * Returns the complete ifaddrlist structure matching the desired interface
996  * address. Ignores interfaces which are either down or loopback.
997  */
998 static struct ifaddrlist *
999 find_ifaddr(struct ifaddrlist *al, int len, union any_in_addr *addr,
1000     int family)
1001 {
1002 	struct ifaddrlist *tmp_al = al;
1003 	int i;
1004 	size_t addr_len = (family == AF_INET) ? sizeof (struct in_addr) :
1005 	    sizeof (struct in6_addr);
1006 
1007 	for (i = 0; i < len; i++, tmp_al++) {
1008 		if ((!(tmp_al->flags & IFF_LOOPBACK) &&
1009 		    (tmp_al->flags & IFF_UP)) &&
1010 		    (memcmp(&tmp_al->addr, addr, addr_len) == 0))
1011 			break;
1012 	}
1013 
1014 	if (i < len) {
1015 		return (tmp_al);
1016 	} else {
1017 		return (NULL);
1018 	}
1019 }
1020 
1021 /*
1022  * Returns the complete ifaddrlist structure matching the desired interface name
1023  * Ignores interfaces which are either down or loopback.
1024  */
1025 static struct ifaddrlist *
1026 find_device(struct ifaddrlist *al, int len, char *device)
1027 {
1028 	struct ifaddrlist *tmp_al = al;
1029 	int i;
1030 
1031 	for (i = 0; i < len; i++, tmp_al++) {
1032 		if ((!(tmp_al->flags & IFF_LOOPBACK) &&
1033 		    (tmp_al->flags & IFF_UP)) &&
1034 		    (strcmp(tmp_al->device, device) == 0))
1035 			break;
1036 	}
1037 
1038 	if (i < len) {
1039 		return (tmp_al);
1040 	} else {
1041 		return (NULL);
1042 	}
1043 }
1044 
1045 /*
1046  * returns _B_TRUE if given hostinfo contains the given address
1047  */
1048 static boolean_t
1049 has_addr(struct addrinfo *ai, union any_in_addr *addr)
1050 {
1051 	struct addrinfo *ai_tmp = NULL;
1052 	union any_in_addr *ap;
1053 
1054 	for (ai_tmp = ai; ai_tmp != NULL; ai_tmp = ai_tmp->ai_next) {
1055 		if (ai_tmp->ai_family == AF_INET6)
1056 			continue;
1057 		ap = (union any_in_addr *)
1058 		    /* LINTED E_BAD_PTR_CAST_ALIGN */
1059 		    &((struct sockaddr_in *)ai_tmp->ai_addr)->sin_addr;
1060 		if (memcmp(ap, addr, sizeof (struct in_addr)) == 0)
1061 			break;
1062 	}
1063 
1064 	if (ai_tmp != NULL) {
1065 		return (_B_TRUE);
1066 	} else {
1067 		return (_B_FALSE);
1068 	}
1069 }
1070 
1071 /*
1072  * Resolve the gateway names, splitting results into v4 and v6 lists.
1073  * Gateway addresses are added to the appropriate passed-in array; the
1074  * number of resolved gateways for each af is returned in resolved[6].
1075  * Assumes that passed-in arrays are large enough for MAX_GWS[6] addrs
1076  * and resolved[6] ptrs are non-null; ignores array and counter if the
1077  * address family param makes them irrelevant.
1078  */
1079 static void
1080 get_gwaddrs(char **gwlist, int family, union any_in_addr *gwIPlist,
1081     union any_in_addr *gwIPlist6, int *resolved, int *resolved6)
1082 {
1083 	int i;
1084 	boolean_t check_v4 = _B_TRUE, check_v6 = _B_TRUE;
1085 	struct addrinfo *ai = NULL;
1086 	struct addrinfo *aip = NULL;
1087 
1088 	*resolved = *resolved6 = 0;
1089 	switch (family) {
1090 	case AF_UNSPEC:
1091 		break;
1092 	case AF_INET:
1093 		check_v6 = _B_FALSE;
1094 		break;
1095 	case AF_INET6:
1096 		check_v4 = _B_FALSE;
1097 		break;
1098 	default:
1099 		return;
1100 	}
1101 
1102 	if (check_v4 && gw_count >= MAX_GWS) {
1103 		check_v4 = _B_FALSE;
1104 		Fprintf(stderr, "%s: too many IPv4 gateways\n", prog);
1105 		num_v4 = 0;
1106 	}
1107 	if (check_v6 && gw_count >= MAX_GWS6) {
1108 		check_v6 = _B_FALSE;
1109 		Fprintf(stderr, "%s: too many IPv6 gateways\n", prog);
1110 		num_v6 = 0;
1111 	}
1112 
1113 	for (i = 0; i < gw_count; i++) {
1114 		if (!check_v4 && !check_v6)
1115 			return;
1116 		get_hostinfo(gwlist[i], family, &ai);
1117 		if (ai == NULL)
1118 			return;
1119 		if (check_v4 && num_v4 != 0) {
1120 			check_v4 = _B_FALSE;
1121 			for (aip = ai; aip != NULL; aip = aip->ai_next) {
1122 				if (aip->ai_family == AF_INET) {
1123 					/* LINTED E_BAD_PTR_CAST_ALIGN */
1124 					bcopy(&((struct sockaddr_in *)
1125 					    aip->ai_addr)->sin_addr,
1126 					    &gwIPlist[i].addr,
1127 					    aip->ai_addrlen);
1128 					(*resolved)++;
1129 					check_v4 = _B_TRUE;
1130 					break;
1131 				}
1132 			}
1133 		} else if (check_v4) {
1134 			check_v4 = _B_FALSE;
1135 		}
1136 		if (check_v6 && num_v6 != 0) {
1137 			check_v6 = _B_FALSE;
1138 			for (aip = ai; aip != NULL; aip = aip->ai_next) {
1139 				if (aip->ai_family == AF_INET6) {
1140 					/* LINTED E_BAD_PTR_CAST_ALIGN */
1141 					bcopy(&((struct sockaddr_in6 *)
1142 					    aip->ai_addr)->sin6_addr,
1143 					    &gwIPlist6[i].addr6,
1144 					    aip->ai_addrlen);
1145 					(*resolved6)++;
1146 					check_v6 = _B_TRUE;
1147 					break;
1148 				}
1149 			}
1150 		} else if (check_v6) {
1151 			check_v6 = _B_FALSE;
1152 		}
1153 	}
1154 	freeaddrinfo(ai);
1155 }
1156 
1157 /*
1158  * set protocol specific values here
1159  */
1160 static void
1161 setup_protocol(struct pr_set *pr, int family)
1162 {
1163 	/*
1164 	 * Set the global variables for each AF. This is going to save us lots
1165 	 * of "if (family == AF_INET)... else .."
1166 	 */
1167 	pr->family = family;
1168 
1169 	if (family == AF_INET) {
1170 		if (!docksum) {
1171 			Fprintf(stderr,
1172 			    "%s: Warning: checksums disabled\n", prog);
1173 		}
1174 		(void) strcpy(pr->name, "IPv4");
1175 		(void) strcpy(pr->icmp, "icmp");
1176 		pr->icmp_minlen = ICMP_MINLEN;
1177 		pr->addr_len = sizeof (struct in_addr);
1178 		pr->ip_hdr_len = sizeof (struct ip);
1179 		pr->sock_size = sizeof (struct sockaddr_in);
1180 		pr->to = (struct sockaddr *)&whereto;
1181 		pr->from = (struct sockaddr *)&wherefrom;
1182 		pr->from_sin_addr = (void *)&wherefrom.sin_addr;
1183 		pr->gwIPlist = gwIPlist;
1184 		pr->set_buffers_fn = set_buffers;
1185 		pr->check_reply_fn = check_reply;
1186 		pr->print_icmp_other_fn = print_icmp_other;
1187 		pr->print_addr_fn = print_addr;
1188 		pr->packlen = calc_packetlen(packlen_input, pr);
1189 	} else {
1190 		(void) strcpy(pr->name, "IPv6");
1191 		(void) strcpy(pr->icmp, "ipv6-icmp");
1192 		pr->icmp_minlen = ICMP6_MINLEN;
1193 		pr->addr_len = sizeof (struct in6_addr);
1194 		pr->ip_hdr_len = sizeof (struct ip6_hdr);
1195 		pr->sock_size = sizeof (struct sockaddr_in6);
1196 		pr->to = (struct sockaddr *)&whereto6;
1197 		pr->from = (struct sockaddr *)&wherefrom6;
1198 		pr->from_sin_addr = (void *)&wherefrom6.sin6_addr;
1199 		pr->gwIPlist = gwIP6list;
1200 		pr->set_buffers_fn = set_buffers6;
1201 		pr->check_reply_fn = check_reply6;
1202 		pr->print_icmp_other_fn = print_icmp_other6;
1203 		pr->print_addr_fn = print_addr6;
1204 		pr->packlen = calc_packetlen(packlen_input, pr);
1205 	}
1206 	if (pr->packlen == 0)
1207 		exit(EXIT_FAILURE);
1208 }
1209 
1210 /*
1211  * setup the sockets for the given protocol's address family
1212  */
1213 static void
1214 setup_socket(struct pr_set *pr, int packet_len)
1215 {
1216 	int on = 1;
1217 	struct protoent *pe;
1218 	int type;
1219 	int proto;
1220 	int int_op;
1221 	int rsock;
1222 	int ssock;
1223 
1224 	if ((pe = getprotobyname(pr->icmp)) == NULL) {
1225 		Fprintf(stderr, "%s: unknown protocol %s\n", prog, pr->icmp);
1226 		exit(EXIT_FAILURE);
1227 	}
1228 
1229 	/* privilege bracketing */
1230 	(void) __priv_bracket(PRIV_ON);
1231 
1232 	if ((rsock = socket(pr->family, SOCK_RAW, pe->p_proto)) < 0) {
1233 		Fprintf(stderr, "%s: icmp socket: %s\n", prog, strerror(errno));
1234 		exit(EXIT_FAILURE);
1235 	}
1236 
1237 	if (options & SO_DEBUG) {
1238 		if (setsockopt(rsock, SOL_SOCKET, SO_DEBUG, (char *)&on,
1239 		    sizeof (on)) < 0) {
1240 			Fprintf(stderr, "%s: SO_DEBUG: %s\n", prog,
1241 			    strerror(errno));
1242 			exit(EXIT_FAILURE);
1243 		}
1244 	}
1245 	if (options & SO_DONTROUTE) {
1246 		if (setsockopt(rsock, SOL_SOCKET, SO_DONTROUTE, (char *)&on,
1247 		    sizeof (on)) < 0) {
1248 			Fprintf(stderr, "%s: SO_DONTROUTE: %s\n", prog,
1249 			    strerror(errno));
1250 			exit(EXIT_FAILURE);
1251 		}
1252 	}
1253 
1254 	if (pr->family == AF_INET6) {
1255 		/* Enable receipt of destination address info */
1256 		if (setsockopt(rsock, IPPROTO_IPV6, IPV6_RECVPKTINFO,
1257 		    (char *)&on, sizeof (on)) < 0) {
1258 			Fprintf(stderr, "%s: IPV6_RECVPKTINFO: %s\n", prog,
1259 			    strerror(errno));
1260 			exit(EXIT_FAILURE);
1261 		}
1262 		/* Enable receipt of hoplimit info */
1263 		if (setsockopt(rsock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT,
1264 		    (char *)&on, sizeof (on)) < 0) {
1265 			Fprintf(stderr, "%s: IPV6_RECVHOPLIMIT: %s\n", prog,
1266 			    strerror(errno));
1267 			exit(EXIT_FAILURE);
1268 		}
1269 
1270 	}
1271 
1272 	/*
1273 	 * Initialize the socket type and protocol based on the address
1274 	 * family, whether or not a raw IP socket is required (for IPv4)
1275 	 * or whether ICMP will be used instead of UDP.
1276 	 *
1277 	 * For historical reasons, the datagrams sent out by
1278 	 * traceroute(1M) do not have the "don't fragment" flag set.  For
1279 	 * this reason as well as the ability to set the Loose Source and
1280 	 * Record Route (LSRR) option, a raw IP socket will be used for
1281 	 * IPv4 when run in the global zone.  Otherwise, the actual
1282 	 * datagram that will be sent will be a regular UDP or ICMP echo
1283 	 * request packet.  However for convenience and for future options
1284 	 * when other IP header information may be specified using
1285 	 * traceroute, the buffer including the raw IP and UDP or ICMP
1286 	 * header is always filled in.  When the probe is actually sent,
1287 	 * the size of the request and the start of the packet is set
1288 	 * according to the type of datagram to send.
1289 	 */
1290 	if (pr->family == AF_INET && raw_req) {
1291 		type = SOCK_RAW;
1292 		proto = IPPROTO_RAW;
1293 	} else if (useicmp) {
1294 		type = SOCK_RAW;
1295 		if (pr->family == AF_INET)
1296 			proto = IPPROTO_ICMP;
1297 		else
1298 			proto = IPPROTO_ICMPV6;
1299 	} else {
1300 		type = SOCK_DGRAM;
1301 		proto = IPPROTO_UDP;
1302 	}
1303 	ssock = socket(pr->family, type, proto);
1304 
1305 	if (ssock < 0) {
1306 		if (proto == IPPROTO_RAW) {
1307 			Fprintf(stderr, "%s: raw socket: %s\n", prog,
1308 			    strerror(errno));
1309 		} else if (proto == IPPROTO_UDP) {
1310 			Fprintf(stderr, "%s: udp socket: %s\n", prog,
1311 			    strerror(errno));
1312 		} else {
1313 			Fprintf(stderr, "%s: icmp socket: %s\n", prog,
1314 			    strerror(errno));
1315 		}
1316 		exit(EXIT_FAILURE);
1317 	}
1318 
1319 	if (setsockopt(ssock, SOL_SOCKET, SO_SNDBUF, (char *)&packet_len,
1320 	    sizeof (packet_len)) < 0) {
1321 		Fprintf(stderr, "%s: SO_SNDBUF: %s\n", prog, strerror(errno));
1322 		exit(EXIT_FAILURE);
1323 	}
1324 
1325 	if (pr->family == AF_INET && raw_req) {
1326 		if (setsockopt(ssock, IPPROTO_IP, IP_HDRINCL, (char *)&on,
1327 		    sizeof (on)) < 0) {
1328 			Fprintf(stderr, "%s: IP_HDRINCL: %s\n", prog,
1329 			    strerror(errno));
1330 			exit(EXIT_FAILURE);
1331 		}
1332 	}
1333 
1334 	if (options & SO_DEBUG) {
1335 		if (setsockopt(ssock, SOL_SOCKET, SO_DEBUG, (char *)&on,
1336 		    sizeof (on)) < 0) {
1337 			Fprintf(stderr, "%s: SO_DEBUG: %s\n", prog,
1338 			    strerror(errno));
1339 			exit(EXIT_FAILURE);
1340 		}
1341 	}
1342 	if (options & SO_DONTROUTE) {
1343 		if (setsockopt(ssock, SOL_SOCKET, SO_DONTROUTE,
1344 		    (char *)&on, sizeof (on)) < 0) {
1345 			Fprintf(stderr, "%s: SO_DONTROUTE: %s\n", prog,
1346 			    strerror(errno));
1347 			exit(EXIT_FAILURE);
1348 		}
1349 	}
1350 
1351 	/*
1352 	 * If a raw IPv4 packet is going to be sent, the Type of Service
1353 	 * field in the packet will be initialized in set_buffers().
1354 	 * Otherwise, it is initialized here using the IPPROTO_IP level
1355 	 * socket option.
1356 	 */
1357 	if (settos && !raw_req) {
1358 		int_op = tos;
1359 		if (setsockopt(ssock, IPPROTO_IP, IP_TOS, (char *)&int_op,
1360 		    sizeof (int_op)) < 0) {
1361 			Fprintf(stderr, "%s: IP_TOS: %s\n", prog,
1362 			    strerror(errno));
1363 			exit(EXIT_FAILURE);
1364 		}
1365 	}
1366 	if (pr->family == AF_INET) {
1367 		rcvsock4 = rsock;
1368 		sndsock4 = ssock;
1369 	} else {
1370 		rcvsock6 = rsock;
1371 		sndsock6 = ssock;
1372 	}
1373 	/* Revert to non-privileged user after configuring sockets */
1374 	(void) __priv_bracket(PRIV_OFF);
1375 }
1376 
1377 /*
1378  * If we are "probing all", this function calls traceroute() for each IP address
1379  * of the target, otherwise calls only once. Returns _B_FALSE if traceroute()
1380  * fails.
1381  */
1382 static void
1383 trace_it(struct addrinfo *ai_dst)
1384 {
1385 	struct msghdr msg6;
1386 	int num_dst_IPaddrs;
1387 	struct addrinfo *aip;
1388 	int i;
1389 
1390 	if (!probe_all)
1391 		num_dst_IPaddrs = 1;
1392 	else
1393 		num_dst_IPaddrs = num_v4 + num_v6;
1394 
1395 	/*
1396 	 * Initialize the msg6 structure using the hoplimit for the first
1397 	 * probe packet, gateway addresses and the outgoing interface index.
1398 	 */
1399 	if (ai_dst->ai_family == AF_INET6 || (probe_all && num_v6)) {
1400 		msg6.msg_control = NULL;
1401 		msg6.msg_controllen = 0;
1402 		set_ancillary_data(&msg6, first_ttl, pr6->gwIPlist, gw_count,
1403 		    if_index);
1404 	}
1405 
1406 	/* run traceroute for all the IP addresses of the multihomed dest */
1407 	for (aip = ai_dst, i = 0; i < num_dst_IPaddrs && aip != NULL; i++) {
1408 		union any_in_addr *addrp;
1409 		if (aip->ai_family == AF_INET) {
1410 			addrp = (union any_in_addr *)
1411 			    /* LINTED E_BAD_PTR_CAST_ALIGN */
1412 			    &((struct sockaddr_in *)
1413 			    aip->ai_addr)->sin_addr;
1414 			set_sin((struct sockaddr *)pr4->to, addrp,
1415 			    aip->ai_family);
1416 			traceroute(addrp, &msg6, pr4, num_ifs4, al4);
1417 		} else {
1418 			addrp = (union any_in_addr *)
1419 			    /* LINTED E_BAD_PTR_CAST_ALIGN */
1420 			    &((struct sockaddr_in6 *)
1421 			    aip->ai_addr)->sin6_addr;
1422 			set_sin((struct sockaddr *)pr6->to, addrp,
1423 			    aip->ai_family);
1424 			traceroute(addrp, &msg6, pr6, num_ifs6, al6);
1425 		}
1426 		aip = aip->ai_next;
1427 		if (i < (num_dst_IPaddrs - 1))
1428 			(void) putchar('\n');
1429 	}
1430 }
1431 
1432 /*
1433  * set the IP address in a sockaddr struct
1434  */
1435 static void
1436 set_sin(struct sockaddr *sock, union any_in_addr *addr, int family)
1437 {
1438 	sock->sa_family = family;
1439 
1440 	if (family == AF_INET)
1441 		/* LINTED E_BAD_PTR_CAST_ALIGN */
1442 		((struct sockaddr_in *)sock)->sin_addr = addr->addr;
1443 	else
1444 		/* LINTED E_BAD_PTR_CAST_ALIGN */
1445 		((struct sockaddr_in6 *)sock)->sin6_addr = addr->addr6;
1446 }
1447 
1448 /*
1449  * returns the IF name on which the given IP address is configured
1450  */
1451 static char *
1452 device_name(struct ifaddrlist *al, int len, union any_in_addr *ip_addr,
1453     struct pr_set *pr)
1454 {
1455 	int i;
1456 	struct ifaddrlist *tmp_al;
1457 
1458 	tmp_al = al;
1459 
1460 	for (i = 0; i < len; i++, tmp_al++) {
1461 		if (memcmp(&tmp_al->addr, ip_addr, pr->addr_len) == 0) {
1462 			return (tmp_al->device);
1463 		}
1464 	}
1465 
1466 	return (NULL);
1467 }
1468 
1469 /*
1470  * Trace the route to the host with given IP address.
1471  */
1472 static void
1473 traceroute(union any_in_addr *ip_addr, struct msghdr *msg6, struct pr_set *pr,
1474     int num_ifs, struct ifaddrlist *al)
1475 {
1476 	int ttl;
1477 	int probe;
1478 	uchar_t type;				/* icmp type */
1479 	uchar_t code;				/* icmp code */
1480 	int reply;
1481 	int seq = 0;
1482 	char temp_buf[INET6_ADDRSTRLEN];	/* use for inet_ntop() */
1483 	int longjmp_return;			/* return value from longjump */
1484 	struct ip *ip = (struct ip *)packet;
1485 	boolean_t got_there = _B_FALSE;		/* we hit the destination */
1486 	static boolean_t first_pkt = _B_TRUE;
1487 	int hoplimit;				/* hoplimit for IPv6 packets */
1488 	struct in6_addr addr6;
1489 	int num_src_ifs;			/* excludes down and loopback */
1490 	struct msghdr in_msg;
1491 	struct iovec iov;
1492 	int *intp;
1493 	int sndsock;
1494 	int rcvsock;
1495 
1496 	msg6->msg_name = pr->to;
1497 	msg6->msg_namelen = sizeof (struct sockaddr_in6);
1498 	sndsock =  (pr->family == AF_INET) ? sndsock4 : sndsock6;
1499 	rcvsock =  (pr->family == AF_INET) ? rcvsock4 : rcvsock6;
1500 
1501 	/* carry out the source address selection */
1502 	if (pick_src) {
1503 		union any_in_addr src_addr;
1504 		char *dev_name;
1505 		int i;
1506 
1507 		/*
1508 		 * If there's a gateway, a routing header as a consequence, our
1509 		 * kernel picks the source address based on the first hop
1510 		 * address, rather than final destination address.
1511 		 */
1512 		if (gw_count > 0) {
1513 			(void) select_src_addr(pr->gwIPlist, &src_addr,
1514 			    pr->family);
1515 		} else {
1516 			(void) select_src_addr(ip_addr, &src_addr, pr->family);
1517 		}
1518 		set_sin(pr->from, &src_addr, pr->family);
1519 
1520 		/* filter out down and loopback interfaces */
1521 		num_src_ifs = 0;
1522 		for (i = 0; i < num_ifs; i++) {
1523 			if (!(al[i].flags & IFF_LOOPBACK) &&
1524 			    (al[i].flags & IFF_UP))
1525 				num_src_ifs++;
1526 		}
1527 
1528 		if (num_src_ifs > 1) {
1529 			dev_name = device_name(al, num_ifs, &src_addr, pr);
1530 			if (dev_name == NULL)
1531 				dev_name = "?";
1532 
1533 			Fprintf(stderr,
1534 			    "%s: Warning: Multiple interfaces found;"
1535 			    " using %s @ %s\n",
1536 			    prog, inet_ntop(pr->family,
1537 				(const void *)pr->from_sin_addr,
1538 				temp_buf, sizeof (temp_buf)),
1539 			    dev_name);
1540 		}
1541 	}
1542 
1543 	if (pr->family == AF_INET) {
1544 		outip4->ip_src = *(struct in_addr *)pr->from_sin_addr;
1545 		outip4->ip_dst = ip_addr->addr;
1546 	}
1547 
1548 	/*
1549 	 * If the hostname is an IPv6 literal address, let's not print it twice.
1550 	 */
1551 	if (pr->family == AF_INET6 &&
1552 	    inet_pton(AF_INET6, hostname, &addr6) > 0) {
1553 		Fprintf(stderr, "%s to %s", prog, hostname);
1554 	} else {
1555 		Fprintf(stderr, "%s to %s (%s)", prog, hostname,
1556 		    inet_ntop(pr->family, (const void *)ip_addr, temp_buf,
1557 			sizeof (temp_buf)));
1558 	}
1559 
1560 	if (source)
1561 		Fprintf(stderr, " from %s", source);
1562 	Fprintf(stderr, ", %d hops max, %d byte packets\n", max_ttl,
1563 	    pr->packlen);
1564 	(void) fflush(stderr);
1565 
1566 	/*
1567 	 * Setup the source routing for IPv4. For IPv6, we did the required
1568 	 * setup in the caller function, trace_it(), because it's independent
1569 	 * from the IP address of target.
1570 	 */
1571 	if (pr->family == AF_INET && gw_count > 0)
1572 		set_IPv4opt_sourcerouting(sndsock, ip_addr, pr->gwIPlist);
1573 
1574 	if (probe_all) {
1575 		/* interrupt handler sig_handler() jumps back to here */
1576 		if ((longjmp_return = setjmp(env)) != 0) {
1577 			switch (longjmp_return) {
1578 			case SIGINT:
1579 				Printf("(skipping)\n");
1580 				return;
1581 			case SIGQUIT:
1582 				Printf("(exiting)\n");
1583 				exit(EXIT_SUCCESS);
1584 			default:	/* should never happen */
1585 				exit(EXIT_FAILURE);
1586 			}
1587 		}
1588 		(void) signal(SIGINT, sig_handler);
1589 	}
1590 
1591 	for (ttl = first_ttl; ttl <= max_ttl; ++ttl) {
1592 		union any_in_addr lastaddr;
1593 		int timeouts = 0;
1594 		double rtt;		/* for statistics */
1595 		int nreceived = 0;
1596 		double rttmin, rttmax;
1597 		double rttsum, rttssq;
1598 		int unreachable;
1599 
1600 		got_there = _B_FALSE;
1601 		unreachable = 0;
1602 
1603 		/*
1604 		 * The following line clears both IPv4 and IPv6 address stored
1605 		 * in the union.
1606 		 */
1607 		lastaddr.addr6 = in6addr_any;
1608 
1609 		if ((ttl == (first_ttl + 1)) && (options & SO_DONTROUTE)) {
1610 			Fprintf(stderr,
1611 			    "%s: host %s is not on a directly-attached"
1612 			    " network\n", prog, hostname);
1613 			break;
1614 		}
1615 
1616 		Printf("%2d ", ttl);
1617 		(void) fflush(stdout);
1618 
1619 		for (probe = 0; (probe < nprobes) && (timeouts < max_timeout);
1620 		    ++probe) {
1621 			int cc;
1622 			struct timeval t1, t2;
1623 
1624 			/*
1625 			 * Put a delay before sending this probe packet. Don't
1626 			 * delay it if it's the very first packet.
1627 			 */
1628 			if (!first_pkt) {
1629 				if (delay.tv_sec > 0)
1630 					(void) sleep((uint_t)delay.tv_sec);
1631 				if (delay.tv_usec > 0)
1632 					(void) usleep(delay.tv_usec);
1633 			} else {
1634 				first_pkt = _B_FALSE;
1635 			}
1636 
1637 			(void) gettimeofday(&t1, NULL);
1638 
1639 			if (pr->family == AF_INET) {
1640 				send_probe(sndsock, pr->to, outip4, seq, ttl,
1641 				    &t1, pr->packlen);
1642 			} else {
1643 				send_probe6(sndsock, msg6, outip6, seq, ttl,
1644 				    &t1, pr->packlen);
1645 			}
1646 
1647 			/* prepare msghdr for recvmsg() */
1648 			in_msg.msg_name = pr->from;
1649 			in_msg.msg_namelen = pr->sock_size;
1650 
1651 			iov.iov_base = (char *)packet;
1652 			iov.iov_len = sizeof (packet);
1653 
1654 			in_msg.msg_iov = &iov;
1655 			in_msg.msg_iovlen = 1;
1656 
1657 			in_msg.msg_control = ancillary_data;
1658 			in_msg.msg_controllen = sizeof (ancillary_data);
1659 
1660 			while ((cc = wait_for_reply(rcvsock, &in_msg,
1661 			    &t1)) != 0) {
1662 				(void) gettimeofday(&t2, NULL);
1663 
1664 				reply = (*pr->check_reply_fn) (&in_msg, cc, seq,
1665 				    &type, &code);
1666 
1667 				in_msg.msg_controllen =
1668 				    sizeof (ancillary_data);
1669 				/* Skip short packet */
1670 				if (reply == REPLY_SHORT_PKT) {
1671 					continue;
1672 				}
1673 
1674 				timeouts = 0;
1675 
1676 				/*
1677 				 * if reply comes from a different host, print
1678 				 * the hostname
1679 				 */
1680 				if (memcmp(pr->from_sin_addr, &lastaddr,
1681 				    pr->addr_len) != 0) {
1682 					(*pr->print_addr_fn) ((uchar_t *)packet,
1683 					    cc, pr->from);
1684 					/* store the address response */
1685 					(void) memcpy(&lastaddr,
1686 					    pr->from_sin_addr, pr->addr_len);
1687 				}
1688 
1689 				rtt = deltaT(&t1, &t2);
1690 				if (collect_stat) {
1691 					record_stats(rtt, &nreceived, &rttmin,
1692 					    &rttmax, &rttsum, &rttssq);
1693 				} else {
1694 					Printf("  %.3f ms", rtt);
1695 				}
1696 
1697 				if (pr->family == AF_INET6) {
1698 					intp =
1699 					    (int *)find_ancillary_data(&in_msg,
1700 						IPPROTO_IPV6, IPV6_HOPLIMIT);
1701 					if (intp == NULL) {
1702 						Fprintf(stderr,
1703 						    "%s: can't find "
1704 						    "IPV6_HOPLIMIT ancillary "
1705 						    "data\n", prog);
1706 						exit(EXIT_FAILURE);
1707 					}
1708 					hoplimit = *intp;
1709 				}
1710 
1711 				if (reply == REPLY_GOT_TARGET) {
1712 					got_there = _B_TRUE;
1713 
1714 					if (((pr->family == AF_INET) &&
1715 					    (ip->ip_ttl <= 1)) ||
1716 					    ((pr->family == AF_INET6) &&
1717 					    (hoplimit <= 1)))
1718 						Printf(" !");
1719 				}
1720 
1721 				if (!collect_stat && showttl) {
1722 					if (pr->family == AF_INET) {
1723 						Printf(" (ttl=%d)",
1724 						    (int)ip->ip_ttl);
1725 					} else if (hoplimit != -1) {
1726 						Printf(" (hop limit=%d)",
1727 						    hoplimit);
1728 					}
1729 				}
1730 
1731 				if (reply == REPLY_GOT_OTHER) {
1732 					if ((*pr->print_icmp_other_fn)
1733 					    (type, code)) {
1734 						unreachable++;
1735 					}
1736 				}
1737 
1738 				/* special case */
1739 				if (pr->family == AF_INET &&
1740 				    type == ICMP_UNREACH &&
1741 				    code == ICMP_UNREACH_PROTOCOL)
1742 					got_there = _B_TRUE;
1743 
1744 				break;
1745 			}
1746 
1747 			seq = (seq + 1) % (MAX_SEQ + 1);
1748 
1749 			if (cc == 0) {
1750 				Printf(" *");
1751 				timeouts++;
1752 			}
1753 
1754 			(void) fflush(stdout);
1755 		}
1756 
1757 		if (collect_stat) {
1758 			print_stats(probe, nreceived, rttmin, rttmax, rttsum,
1759 			    rttssq);
1760 		}
1761 
1762 		(void) putchar('\n');
1763 
1764 		/* either we hit the target or received too many unreachables */
1765 		if (got_there ||
1766 		    (unreachable > 0 && unreachable >= nprobes - 1))
1767 			break;
1768 	}
1769 
1770 	/* Ignore the SIGINT between traceroute() runs */
1771 	if (probe_all)
1772 		(void) signal(SIGINT, SIG_IGN);
1773 }
1774 
1775 /*
1776  * for a given destination address and address family, it finds out what
1777  * source address kernel is going to pick
1778  */
1779 static void
1780 select_src_addr(union any_in_addr *dst_addr, union any_in_addr *src_addr,
1781     int family)
1782 {
1783 	int tmp_fd;
1784 	struct sockaddr *sock;
1785 	struct sockaddr_in *sin;
1786 	struct sockaddr_in6 *sin6;
1787 	size_t sock_len;
1788 
1789 	sock = (struct sockaddr *)malloc(sizeof (struct sockaddr_in6));
1790 	if (sock == NULL) {
1791 		Fprintf(stderr, "%s: malloc %s\n", prog, strerror(errno));
1792 		exit(EXIT_FAILURE);
1793 	}
1794 	(void) bzero(sock, sizeof (struct sockaddr_in6));
1795 
1796 	if (family == AF_INET) {
1797 		/* LINTED E_BAD_PTR_CAST_ALIGN */
1798 		sin = (struct sockaddr_in *)sock;
1799 		sin->sin_family = AF_INET;
1800 		sin->sin_addr = dst_addr->addr;
1801 		sin->sin_port = IPPORT_ECHO;	/* port shouldn't be 0 */
1802 		sock_len = sizeof (struct sockaddr_in);
1803 	} else {
1804 		/* LINTED E_BAD_PTR_CAST_ALIGN */
1805 		sin6 = (struct sockaddr_in6 *)sock;
1806 		sin6->sin6_family = AF_INET6;
1807 		sin6->sin6_addr = dst_addr->addr6;
1808 		sin6->sin6_port = IPPORT_ECHO;	/* port shouldn't be 0 */
1809 		sock_len = sizeof (struct sockaddr_in6);
1810 	}
1811 
1812 	/* open a UDP socket */
1813 	if ((tmp_fd = socket(family, SOCK_DGRAM, 0)) < 0) {
1814 		Fprintf(stderr, "%s: udp socket: %s\n", prog,
1815 		    strerror(errno));
1816 		exit(EXIT_FAILURE);
1817 	}
1818 
1819 	/* connect it */
1820 	if (connect(tmp_fd, sock, sock_len) < 0) {
1821 		/*
1822 		 * If there's no route to the destination, this connect() call
1823 		 * fails. We just return all-zero (wildcard) as the source
1824 		 * address, so that user can get to see "no route to dest"
1825 		 * message, as it'll try to send the probe packet out and will
1826 		 * receive ICMP unreachable.
1827 		 */
1828 		if (family == AF_INET)
1829 			src_addr->addr.s_addr = INADDR_ANY;
1830 		else
1831 			src_addr->addr6 = in6addr_any;
1832 		free(sock);
1833 		return;
1834 	}
1835 
1836 	/* get the local sock info */
1837 	if (getsockname(tmp_fd, sock, &sock_len) < 0) {
1838 		Fprintf(stderr, "%s: getsockname: %s\n", prog,
1839 		    strerror(errno));
1840 		exit(EXIT_FAILURE);
1841 	}
1842 
1843 	if (family == AF_INET) {
1844 		/* LINTED E_BAD_PTR_CAST_ALIGN */
1845 		sin = (struct sockaddr_in *)sock;
1846 		src_addr->addr = sin->sin_addr;
1847 	} else {
1848 		/* LINTED E_BAD_PTR_CAST_ALIGN */
1849 		sin6 = (struct sockaddr_in6 *)sock;
1850 		src_addr->addr6 = sin6->sin6_addr;
1851 	}
1852 
1853 	free(sock);
1854 	(void) close(tmp_fd);
1855 }
1856 
1857 /*
1858  * Checksum routine for Internet Protocol family headers (C Version)
1859  */
1860 ushort_t
1861 in_cksum(ushort_t *addr, int len)
1862 {
1863 	int nleft = len;
1864 	ushort_t *w = addr;
1865 	ushort_t answer;
1866 	int sum = 0;
1867 
1868 	/*
1869 	 *  Our algorithm is simple, using a 32 bit accumulator (sum),
1870 	 *  we add sequential 16 bit words to it, and at the end, fold
1871 	 *  back all the carry bits from the top 16 bits into the lower
1872 	 *  16 bits.
1873 	 */
1874 	while (nleft > 1)  {
1875 		sum += *w++;
1876 		nleft -= 2;
1877 	}
1878 
1879 	/* mop up an odd byte, if necessary */
1880 	if (nleft == 1)
1881 		sum += *(uchar_t *)w;
1882 
1883 	/* add back carry outs from top 16 bits to low 16 bits */
1884 	sum = (sum >> 16) + (sum & 0xffff);	/* add hi 16 to low 16 */
1885 	sum += (sum >> 16);			/* add carry */
1886 	answer = ~sum;				/* truncate to 16 bits */
1887 	return (answer);
1888 }
1889 
1890 /*
1891  * Wait until a reply arrives or timeout occurs. If packet arrived, read it
1892  * return the size of the packet read.
1893  */
1894 static int
1895 wait_for_reply(int sock, struct msghdr *msg, struct timeval *tp)
1896 {
1897 	fd_set fds;
1898 	struct timeval now, wait;
1899 	int cc = 0;
1900 	int result;
1901 
1902 	(void) FD_ZERO(&fds);
1903 	FD_SET(sock, &fds);
1904 
1905 	wait.tv_sec = tp->tv_sec + waittime;
1906 	wait.tv_usec = tp->tv_usec;
1907 	(void) gettimeofday(&now, NULL);
1908 	tv_sub(&wait, &now);
1909 
1910 	if (wait.tv_sec < 0 || wait.tv_usec < 0)
1911 		return (0);
1912 
1913 	result = select(sock + 1, &fds, (fd_set *)NULL, (fd_set *)NULL, &wait);
1914 
1915 	if (result == -1) {
1916 		if (errno != EINTR) {
1917 			Fprintf(stderr, "%s: select: %s\n", prog,
1918 			    strerror(errno));
1919 		}
1920 	} else if (result > 0)
1921 		cc = recvmsg(sock, msg, 0);
1922 
1923 	return (cc);
1924 }
1925 
1926 /*
1927  * Construct an Internet address representation. If the nflag has been supplied,
1928  * give numeric value, otherwise try for symbolic name.
1929  */
1930 char *
1931 inet_name(union any_in_addr *in, int family)
1932 {
1933 	char *cp;
1934 	static boolean_t first = _B_TRUE;
1935 	static char domain[NI_MAXHOST + 1];
1936 	static char line[NI_MAXHOST + 1];	/* assuming		*/
1937 				/* (NI_MAXHOST + 1) >= INET6_ADDRSTRLEN */
1938 	char hbuf[NI_MAXHOST];
1939 	socklen_t slen;
1940 	struct sockaddr_in sin;
1941 	struct sockaddr_in6 sin6;
1942 	struct sockaddr *sa;
1943 	int flags;
1944 
1945 	switch (family) {
1946 	case AF_INET:
1947 		slen = sizeof (struct sockaddr_in);
1948 		sin.sin_addr = in->addr;
1949 		sin.sin_port = 0;
1950 		sa = (struct sockaddr *)&sin;
1951 		break;
1952 	case AF_INET6:
1953 		slen = sizeof (struct sockaddr_in6);
1954 		sin6.sin6_addr = in->addr6;
1955 		sin6.sin6_port = 0;
1956 		sa = (struct sockaddr *)&sin6;
1957 		break;
1958 	deafult:
1959 		(void) snprintf(line, sizeof (line),
1960 		    "<invalid address family>");
1961 		return (line);
1962 	}
1963 	sa->sa_family = family;
1964 
1965 	if (first && !nflag) {
1966 		/* find out the domain name */
1967 		first = _B_FALSE;
1968 		if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
1969 		    (cp = strchr(domain, '.')) != NULL) {
1970 			(void) strncpy(domain, cp + 1, sizeof (domain) - 1);
1971 			domain[sizeof (domain) - 1] = '\0';
1972 		} else {
1973 			domain[0] = '\0';
1974 		}
1975 	}
1976 
1977 	flags = (nflag) ? NI_NUMERICHOST : NI_NAMEREQD;
1978 	if (getnameinfo(sa, slen, hbuf, sizeof (hbuf), NULL, 0, flags) != 0) {
1979 		if (inet_ntop(family, (const void *)&in->addr6,
1980 		    hbuf, sizeof (hbuf)) == NULL)
1981 			hbuf[0] = 0;
1982 	} else if (!nflag && (cp = strchr(hbuf, '.')) != NULL &&
1983 	    strcmp(cp + 1, domain) == 0) {
1984 		*cp = '\0';
1985 	}
1986 	(void) strlcpy(line, hbuf, sizeof (line));
1987 
1988 	return (line);
1989 }
1990 
1991 /*
1992  * return the difference (in msec) between two time values
1993  */
1994 static double
1995 deltaT(struct timeval *t1p, struct timeval *t2p)
1996 {
1997 	double dt;
1998 
1999 	dt = (double)(t2p->tv_sec - t1p->tv_sec) * 1000.0 +
2000 	    (double)(t2p->tv_usec - t1p->tv_usec) / 1000.0;
2001 	return (dt);
2002 }
2003 
2004 /*
2005  * Subtract 2 timeval structs:  out = out - in.
2006  * Out is assumed to be >= in.
2007  */
2008 static void
2009 tv_sub(struct timeval *out, struct timeval *in)
2010 {
2011 	if ((out->tv_usec -= in->tv_usec) < 0)   {
2012 		--out->tv_sec;
2013 		out->tv_usec += 1000000;
2014 	}
2015 	out->tv_sec -= in->tv_sec;
2016 }
2017 
2018 /*
2019  * record statistics
2020  */
2021 static void
2022 record_stats(double rtt, int *nreceived, double *rttmin, double *rttmax,
2023     double *rttsum, double *rttssq)
2024 {
2025 	if (*nreceived == 0) {
2026 		*rttmin = rtt;
2027 		*rttmax = rtt;
2028 		*rttsum = rtt;
2029 		*rttssq = rtt * rtt;
2030 	} else {
2031 		if (rtt < *rttmin)
2032 			*rttmin = rtt;
2033 
2034 		if (rtt > *rttmax)
2035 			*rttmax = rtt;
2036 
2037 		*rttsum += rtt;
2038 		*rttssq += rtt * rtt;
2039 	}
2040 
2041 	(*nreceived)++;
2042 }
2043 
2044 /*
2045  * display statistics
2046  */
2047 static void
2048 print_stats(int ntransmitted, int nreceived, double rttmin, double rttmax,
2049     double rttsum, double rttssq)
2050 {
2051 	double rttavg;			/* average round-trip time */
2052 	double rttstd;			/* rtt standard deviation */
2053 
2054 	if (ntransmitted > 0 && ntransmitted >= nreceived) {
2055 		int missed = ntransmitted - nreceived;
2056 		double loss = 100 * (double)missed / (double)ntransmitted;
2057 
2058 		if (nreceived > 0) {
2059 			rttavg = rttsum / nreceived;
2060 			rttstd = rttssq - (rttavg * rttsum);
2061 			rttstd = xsqrt(rttstd / nreceived);
2062 
2063 			Printf("  %.3f", rttmin);
2064 			Printf("/%.3f", rttavg);
2065 			Printf("/%.3f", rttmax);
2066 
2067 			Printf(" (%.3f) ms ", rttstd);
2068 		}
2069 
2070 		Printf(" %d/%d pkts", nreceived, ntransmitted);
2071 
2072 		if (nreceived == 0)
2073 			Printf(" (100%% loss)");
2074 		else
2075 			Printf(" (%.2g%% loss)", loss);
2076 	}
2077 }
2078 
2079 /*
2080  * square root function
2081  */
2082 double
2083 xsqrt(double y)
2084 {
2085 	double t, x;
2086 
2087 	if (y <= 0) {
2088 		return (0.0);
2089 	}
2090 
2091 	x = (y < 1.0) ? 1.0 : y;
2092 	do {
2093 		t = x;
2094 		x = (t + (y/t))/2.0;
2095 	} while (0 < x && x < t);
2096 
2097 	return (x);
2098 }
2099 
2100 /*
2101  * String to double with optional min and max.
2102  */
2103 static double
2104 str2dbl(const char *str, const char *what, double mi, double ma)
2105 {
2106 	double val;
2107 	char *ep;
2108 
2109 	errno = 0;
2110 
2111 	val = strtod(str, &ep);
2112 	if (errno != 0 || *ep != '\0') {
2113 		Fprintf(stderr, "%s: \"%s\" bad value for %s \n",
2114 		    prog, str, what);
2115 		exit(EXIT_FAILURE);
2116 	}
2117 	if (val < mi && mi >= 0) {
2118 		Fprintf(stderr, "%s: %s must be >= %f\n", prog, what, mi);
2119 		exit(EXIT_FAILURE);
2120 	}
2121 	if (val > ma && ma >= 0) {
2122 		Fprintf(stderr, "%s: %s must be <= %f\n", prog, what, ma);
2123 		exit(EXIT_FAILURE);
2124 	}
2125 	return (val);
2126 }
2127 
2128 /*
2129  * String to int with optional min and max. Handles decimal and hex.
2130  */
2131 static int
2132 str2int(const char *str, const char *what, int mi, int ma)
2133 {
2134 	const char *cp;
2135 	int val;
2136 	char *ep;
2137 
2138 	errno = 0;
2139 
2140 	if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) {
2141 		cp = str + 2;
2142 		val = (int)strtol(cp, &ep, 16);
2143 	} else {
2144 		val = (int)strtol(str, &ep, 10);
2145 	}
2146 	if (errno != 0 || *ep != '\0') {
2147 		Fprintf(stderr, "%s: \"%s\" bad value for %s \n",
2148 		    prog, str, what);
2149 		exit(EXIT_FAILURE);
2150 	}
2151 	if (val < mi && mi >= 0) {
2152 		if (mi == 0) {
2153 			Fprintf(stderr, "%s: %s must be >= %d\n",
2154 			    prog, what, mi);
2155 		} else {
2156 			Fprintf(stderr, "%s: %s must be > %d\n",
2157 			    prog, what, mi - 1);
2158 		}
2159 		exit(EXIT_FAILURE);
2160 	}
2161 	if (val > ma && ma >= 0) {
2162 		Fprintf(stderr, "%s: %s must be <= %d\n", prog, what, ma);
2163 		exit(EXIT_FAILURE);
2164 	}
2165 	return (val);
2166 }
2167 
2168 /*
2169  * This is the interrupt handler for SIGINT and SIGQUIT. It's completely handled
2170  * where it jumps to.
2171  */
2172 static void
2173 sig_handler(int sig)
2174 {
2175 	longjmp(env, sig);
2176 }
2177 
2178 /*
2179  * display the usage of traceroute
2180  */
2181 static void
2182 usage(void)
2183 {
2184 	Fprintf(stderr, "Usage: %s [-adFIlnSvx] [-A address_family] "
2185 "[-c traffic_class] \n"
2186 "\t[-f first_hop] [-g gateway [-g gateway ...]| -r] [-i iface]\n"
2187 "\t[-L flow_label] [-m max_hop] [-P pause_sec] [-p port] [-Q max_timeout]\n"
2188 "\t[-q nqueries] [-s src_addr] [-t tos] [-w wait_time] host [packetlen]\n",
2189 		prog);
2190 	exit(EXIT_FAILURE);
2191 }
2192