17c478bd9Sstevel@tonic-gate /*
27d6fbbeaSblu  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate /*
77c478bd9Sstevel@tonic-gate  * Copyright (c) 1988, 1989, 1991, 1994, 1995, 1996, 1997
87c478bd9Sstevel@tonic-gate  *	The Regents of the University of California.  All rights reserved.
97c478bd9Sstevel@tonic-gate  *
107c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
117c478bd9Sstevel@tonic-gate  * modification, are permitted provided that: (1) source code distributions
127c478bd9Sstevel@tonic-gate  * retain the above copyright notice and this paragraph in its entirety, (2)
137c478bd9Sstevel@tonic-gate  * distributions including binary code include the above copyright notice and
147c478bd9Sstevel@tonic-gate  * this paragraph in its entirety in the documentation or other materials
157c478bd9Sstevel@tonic-gate  * provided with the distribution, and (3) all advertising materials mentioning
167c478bd9Sstevel@tonic-gate  * features or use of this software display the following acknowledgement:
177c478bd9Sstevel@tonic-gate  * ``This product includes software developed by the University of California,
187c478bd9Sstevel@tonic-gate  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
197c478bd9Sstevel@tonic-gate  * the University nor the names of its contributors may be used to endorse
207c478bd9Sstevel@tonic-gate  * or promote products derived from this software without specific prior
217c478bd9Sstevel@tonic-gate  * written permission.
227c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
237c478bd9Sstevel@tonic-gate  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
247c478bd9Sstevel@tonic-gate  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
257c478bd9Sstevel@tonic-gate  *
267c478bd9Sstevel@tonic-gate  *
277c478bd9Sstevel@tonic-gate  * @(#)$Header: traceroute.c,v 1.49 97/06/13 02:30:23 leres Exp $ (LBL)
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <sys/param.h>
337c478bd9Sstevel@tonic-gate #include <sys/file.h>
347c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
357c478bd9Sstevel@tonic-gate #include <sys/socket.h>
367c478bd9Sstevel@tonic-gate #include <sys/time.h>
377c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #include <netinet/in_systm.h>
407c478bd9Sstevel@tonic-gate #include <netinet/in.h>
417c478bd9Sstevel@tonic-gate #include <netinet/ip.h>
427c478bd9Sstevel@tonic-gate #include <netinet/ip_var.h>
437c478bd9Sstevel@tonic-gate #include <netinet/ip_icmp.h>
447c478bd9Sstevel@tonic-gate #include <netinet/udp.h>
457c478bd9Sstevel@tonic-gate #include <netinet/udp_var.h>
467c478bd9Sstevel@tonic-gate #include <netinet/ip6.h>
477c478bd9Sstevel@tonic-gate #include <netinet/icmp6.h>
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #include <ctype.h>
527c478bd9Sstevel@tonic-gate #include <errno.h>
537c478bd9Sstevel@tonic-gate #include <malloc.h>
547c478bd9Sstevel@tonic-gate #include <memory.h>
557c478bd9Sstevel@tonic-gate #include <netdb.h>
567c478bd9Sstevel@tonic-gate #include <stdio.h>
577c478bd9Sstevel@tonic-gate #include <stdlib.h>
587c478bd9Sstevel@tonic-gate #include <strings.h>
597c478bd9Sstevel@tonic-gate #include <unistd.h>
607c478bd9Sstevel@tonic-gate #include <libintl.h>
617c478bd9Sstevel@tonic-gate #include <locale.h>
627c478bd9Sstevel@tonic-gate #include <signal.h>
637c478bd9Sstevel@tonic-gate #include <setjmp.h>
647c478bd9Sstevel@tonic-gate #include <limits.h>
657c478bd9Sstevel@tonic-gate #include <zone.h>
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate #include <priv_utils.h>
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate #include <ifaddrlist.h>
717c478bd9Sstevel@tonic-gate #include "traceroute.h"
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate #define	MAX_SEQ			65535	/* max sequence value for ICMP */
747c478bd9Sstevel@tonic-gate #define	MAX_TRAFFIC_CLASS	255	/* max traffic class for IPv6 */
757c478bd9Sstevel@tonic-gate #define	MAX_FLOW_LABEL		0xFFFFF	/* max flow label for IPv6 */
767c478bd9Sstevel@tonic-gate #define	MAX_TOS			255	/* max type-of-service for IPv4 */
777c478bd9Sstevel@tonic-gate #define	STR_LEN			30
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate /* store the information about a host */
807c478bd9Sstevel@tonic-gate struct hostinfo {
817c478bd9Sstevel@tonic-gate 	char *name;		/* hostname */
827c478bd9Sstevel@tonic-gate 	int family;		/* address family of the IP addresses */
837c478bd9Sstevel@tonic-gate 	int num_addr;			/* number of IP addresses */
847c478bd9Sstevel@tonic-gate 	union any_in_addr *addrs;	/* list of IP addresses */
857c478bd9Sstevel@tonic-gate };
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate /* used to store a bunch of protocol specific values */
887c478bd9Sstevel@tonic-gate struct pr_set {
897c478bd9Sstevel@tonic-gate 	int family;		/* AF_INET or AF_INET6 */
907c478bd9Sstevel@tonic-gate 	char name[STR_LEN];	/* "IPv4" or "IPv6" */
917c478bd9Sstevel@tonic-gate 	char icmp[STR_LEN];	/* "icmp" or "ipv6-icmp" */
927c478bd9Sstevel@tonic-gate 	int icmp_minlen;
937c478bd9Sstevel@tonic-gate 	int addr_len;
947c478bd9Sstevel@tonic-gate 	int ip_hdr_len;
957c478bd9Sstevel@tonic-gate 	int packlen;
967c478bd9Sstevel@tonic-gate 	int sock_size;		/* size of sockaddr_in or sockaddr_in6 */
977c478bd9Sstevel@tonic-gate 	struct sockaddr *to;
987c478bd9Sstevel@tonic-gate 	struct sockaddr *from;
997c478bd9Sstevel@tonic-gate 	void *from_sin_addr;
1007c478bd9Sstevel@tonic-gate 	union any_in_addr *gwIPlist;
1017c478bd9Sstevel@tonic-gate 	/* pointers to v4/v6 functions */
1027c478bd9Sstevel@tonic-gate 	struct ip *(*set_buffers_fn) (int);
1037c478bd9Sstevel@tonic-gate 	int (*check_reply_fn)(struct msghdr *, int, int, uchar_t *, uchar_t *);
1047c478bd9Sstevel@tonic-gate 	boolean_t (*print_icmp_other_fn)(uchar_t, uchar_t);
1057c478bd9Sstevel@tonic-gate 	void (*print_addr_fn)(uchar_t *, int, struct sockaddr *);
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate };
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate /*
1107c478bd9Sstevel@tonic-gate  * LBNL bug fixed: in LBNL traceroute 'uchar_t packet[512];'
1117c478bd9Sstevel@tonic-gate  * Not sufficient to hold the complete packet for ECHO REPLY of a big probe.
1127c478bd9Sstevel@tonic-gate  * Packet size is reported incorrectly in such a case.
1137c478bd9Sstevel@tonic-gate  * Also this buffer needs to be 32 bit aligned. In the future the alignment
1147c478bd9Sstevel@tonic-gate  * requirement will be increased to 64 bit. So, let's use 64 bit alignment now.
1157c478bd9Sstevel@tonic-gate  */
1167c478bd9Sstevel@tonic-gate static uint64_t packet[(IP_MAXPACKET + 1)/8];	/* received packet */
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate static struct ip *outip4;	/* output buffer to send as an IPv4 datagram */
1197c478bd9Sstevel@tonic-gate static struct ip *outip6;	/* output buffer to send as an IPv6 datagram */
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate /* Used to store the ancillary data that comes with the received packets */
1227c478bd9Sstevel@tonic-gate static uint64_t ancillary_data[(IP_MAXPACKET + 1)/8];
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate /* first get the gw names, later you'll resolve them based on the family */
1257c478bd9Sstevel@tonic-gate static char *gwlist[MAXMAX_GWS];		/* gateway names list */
1267c478bd9Sstevel@tonic-gate static union any_in_addr gwIPlist[MAX_GWS];	/* gateway IPv4 address list */
1277c478bd9Sstevel@tonic-gate static union any_in_addr gwIP6list[MAX_GWS6];	/* gateway IPv6 address list */
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate static int family_input = AF_UNSPEC;	/* User supplied protocol family */
1307c478bd9Sstevel@tonic-gate static int rcvsock4;		/* receive (icmp) socket file descriptor */
1317c478bd9Sstevel@tonic-gate static int sndsock4;		/* send (udp/icmp) socket file descriptor */
1327c478bd9Sstevel@tonic-gate static int rcvsock6;		/* receive (icmp6) socket file descriptor */
1337c478bd9Sstevel@tonic-gate static int sndsock6;		/* send (udp6/icmp6) socket file descriptor */
1347c478bd9Sstevel@tonic-gate int gw_count = 0;		/* number of gateways */
1357c478bd9Sstevel@tonic-gate static struct sockaddr_in whereto;	/* Who to try to reach */
1367c478bd9Sstevel@tonic-gate static struct sockaddr_in6 whereto6;
1377c478bd9Sstevel@tonic-gate static struct sockaddr_in wherefrom;	/* Who we are */
1387c478bd9Sstevel@tonic-gate static struct sockaddr_in6 wherefrom6;
1397c478bd9Sstevel@tonic-gate static int packlen_input = 0;		/* user input for packlen */
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate char *prog;
1427c478bd9Sstevel@tonic-gate static char *source_input = NULL; /* this is user arg. source, doesn't change */
1437c478bd9Sstevel@tonic-gate static char *source = NULL;	/* this gets modified after name lookup */
1447c478bd9Sstevel@tonic-gate char *hostname;
1457c478bd9Sstevel@tonic-gate static char *device = NULL;   	/* interface name */
1467c478bd9Sstevel@tonic-gate static struct pr_set *pr4;	/* protocol info for IPv4 */
1477c478bd9Sstevel@tonic-gate static struct pr_set *pr6;	/* protocol info for IPv6 */
1487c478bd9Sstevel@tonic-gate static struct ifaddrlist *al4;	/* list of interfaces */
1497c478bd9Sstevel@tonic-gate static struct ifaddrlist *al6;	/* list of interfaces */
1507c478bd9Sstevel@tonic-gate static uint_t if_index = 0;	/* interface index */
1517c478bd9Sstevel@tonic-gate static int num_v4 = 0;		/* count of IPv4 addresses */
1527c478bd9Sstevel@tonic-gate static int num_v6 = 0;		/* count of IPv6 addresses */
1537c478bd9Sstevel@tonic-gate static int num_ifs4 = 0;	/* count of local IPv4 interfaces */
1547c478bd9Sstevel@tonic-gate static int num_ifs6 = 0;	/* count of local IPv6 interfaces */
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate static int nprobes = 3;		/* number of probes */
1577c478bd9Sstevel@tonic-gate static int max_ttl = 30;	/* max number of hops */
1587c478bd9Sstevel@tonic-gate static int first_ttl = 1;	/* initial number of hops */
1597c478bd9Sstevel@tonic-gate ushort_t ident;			/* used to authenticate replies */
1607c478bd9Sstevel@tonic-gate ushort_t port = 32768 + 666;	/* start udp dest port # for probe packets */
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate static int options = 0;		/* socket options */
1637c478bd9Sstevel@tonic-gate boolean_t verbose = _B_FALSE;	/* verbose output */
1647c478bd9Sstevel@tonic-gate static int waittime = 5;	/* time to wait for response (in seconds) */
1657c478bd9Sstevel@tonic-gate static struct timeval delay = {0, 0}; /* delay between consecutive probe */
1667c478bd9Sstevel@tonic-gate boolean_t nflag = _B_FALSE;	/* print addresses numerically */
1677c478bd9Sstevel@tonic-gate static boolean_t showttl = _B_FALSE; /* print the ttl(hop limit) of recvd pkt */
1687c478bd9Sstevel@tonic-gate boolean_t useicmp = _B_FALSE;  	/* use icmp echo instead of udp packets */
1697c478bd9Sstevel@tonic-gate boolean_t docksum = _B_TRUE;	/* calculate checksums */
1707c478bd9Sstevel@tonic-gate static boolean_t collect_stat = _B_FALSE;	/* print statistics */
1717c478bd9Sstevel@tonic-gate boolean_t settos = _B_FALSE;   	/* set type-of-service field */
1727c478bd9Sstevel@tonic-gate static int max_timeout = 5;	/* quit after this consecutive timeouts */
1737c478bd9Sstevel@tonic-gate static boolean_t probe_all = _B_FALSE;	/* probe all the IFs of the target */
1747c478bd9Sstevel@tonic-gate static boolean_t pick_src = _B_FALSE;	/* traceroute picks the src address */
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate /*
1777c478bd9Sstevel@tonic-gate  * flow and class are specific to IPv6, tos and off are specific to IPv4.
1787c478bd9Sstevel@tonic-gate  * Each protocol uses the ones that are specific to itself, and ignores
1797c478bd9Sstevel@tonic-gate  * others.
1807c478bd9Sstevel@tonic-gate  */
1817c478bd9Sstevel@tonic-gate static uint_t flow = 0;		/* IPv6 flow info */
1827c478bd9Sstevel@tonic-gate static uint_t class = 0;	/* IPv6 class */
1837c478bd9Sstevel@tonic-gate uchar_t tos = 0;		/* IPv4 type-of-service */
1847c478bd9Sstevel@tonic-gate ushort_t off = 0;		/* set DF bit */
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate static jmp_buf env;		/* stack environment for longjmp() */
1877c478bd9Sstevel@tonic-gate boolean_t raw_req;		/* if sndsock for IPv4 must be raw */
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate /* Forwards */
1907c478bd9Sstevel@tonic-gate static uint_t calc_packetlen(int, struct pr_set *);
1917c478bd9Sstevel@tonic-gate extern int check_reply(struct msghdr *, int, int, uchar_t *, uchar_t *);
1927c478bd9Sstevel@tonic-gate extern int check_reply6(struct msghdr *, int, int, uchar_t *, uchar_t *);
1937c478bd9Sstevel@tonic-gate static double deltaT(struct timeval *, struct timeval *);
1947c478bd9Sstevel@tonic-gate static char *device_name(struct ifaddrlist *, int, union any_in_addr *,
1957c478bd9Sstevel@tonic-gate     struct pr_set *);
1967c478bd9Sstevel@tonic-gate extern void *find_ancillary_data(struct msghdr *, int, int);
1977c478bd9Sstevel@tonic-gate static boolean_t has_addr(struct addrinfo *, union any_in_addr *);
1987c478bd9Sstevel@tonic-gate static struct ifaddrlist *find_device(struct ifaddrlist *, int, char *);
1997c478bd9Sstevel@tonic-gate static struct ifaddrlist *find_ifaddr(struct ifaddrlist *, int,
2007c478bd9Sstevel@tonic-gate     union any_in_addr *, int);
2017c478bd9Sstevel@tonic-gate static void get_gwaddrs(char **, int, union any_in_addr *,
2027c478bd9Sstevel@tonic-gate     union any_in_addr *, int *, int *);
2037c478bd9Sstevel@tonic-gate static void get_hostinfo(char *, int, struct addrinfo **);
2047c478bd9Sstevel@tonic-gate char *inet_name(union any_in_addr *, int);
2057c478bd9Sstevel@tonic-gate ushort_t in_cksum(ushort_t *, int);
2067c478bd9Sstevel@tonic-gate extern int ip_hdr_length_v6(ip6_t *, int, uint8_t *);
2077c478bd9Sstevel@tonic-gate extern char *pr_type(uchar_t);
2087c478bd9Sstevel@tonic-gate extern char *pr_type6(uchar_t);
2097c478bd9Sstevel@tonic-gate extern void print_addr(uchar_t *, int, struct sockaddr *);
2107c478bd9Sstevel@tonic-gate extern void print_addr6(uchar_t *, int, struct sockaddr *);
2117c478bd9Sstevel@tonic-gate extern boolean_t print_icmp_other(uchar_t, uchar_t);
2127c478bd9Sstevel@tonic-gate extern boolean_t print_icmp_other6(uchar_t, uchar_t);
2137c478bd9Sstevel@tonic-gate static void print_stats(int, int, double, double, double, double);
2147c478bd9Sstevel@tonic-gate static void print_unknown_host_msg(const char *, const char *);
2157c478bd9Sstevel@tonic-gate static void record_stats(double, int *, double *, double *, double *, double *);
2167c478bd9Sstevel@tonic-gate static void resolve_nodes(int *, struct addrinfo **);
2177c478bd9Sstevel@tonic-gate static void select_src_addr(union any_in_addr *, union any_in_addr *, int);
2187c478bd9Sstevel@tonic-gate extern void send_probe(int, struct sockaddr *, struct ip *, int, int,
2197c478bd9Sstevel@tonic-gate     struct timeval *, int);
2207c478bd9Sstevel@tonic-gate extern void send_probe6(int, struct msghdr *, struct ip *, int, int,
2217c478bd9Sstevel@tonic-gate     struct timeval *, int);
2227c478bd9Sstevel@tonic-gate extern void set_ancillary_data(struct msghdr *, int, union any_in_addr *, int,
2237c478bd9Sstevel@tonic-gate     uint_t);
2247c478bd9Sstevel@tonic-gate extern struct ip *set_buffers(int);
2257c478bd9Sstevel@tonic-gate extern struct ip *set_buffers6(int);
2267c478bd9Sstevel@tonic-gate extern void set_IPv4opt_sourcerouting(int, union any_in_addr *,
2277c478bd9Sstevel@tonic-gate     union any_in_addr *);
2287c478bd9Sstevel@tonic-gate static void set_sin(struct sockaddr *, union any_in_addr *, int);
2297c478bd9Sstevel@tonic-gate static int set_src_addr(struct pr_set *, struct ifaddrlist **);
2307c478bd9Sstevel@tonic-gate static void setup_protocol(struct pr_set *, int);
2317c478bd9Sstevel@tonic-gate static void setup_socket(struct pr_set *, int);
2327c478bd9Sstevel@tonic-gate static void sig_handler(int);
2337c478bd9Sstevel@tonic-gate static int str2int(const char *, const char *, int, int);
2347c478bd9Sstevel@tonic-gate static double str2dbl(const char *, const char *, double, double);
2357c478bd9Sstevel@tonic-gate static void trace_it(struct addrinfo *);
2367c478bd9Sstevel@tonic-gate static void traceroute(union any_in_addr *, struct msghdr *, struct pr_set *,
2377c478bd9Sstevel@tonic-gate     int, struct ifaddrlist *);
2387c478bd9Sstevel@tonic-gate static void tv_sub(struct timeval *, struct timeval *);
2397c478bd9Sstevel@tonic-gate static void usage(void);
2407c478bd9Sstevel@tonic-gate static int wait_for_reply(int, struct msghdr *, struct timeval *);
2417c478bd9Sstevel@tonic-gate static double xsqrt(double);
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate /*
2447c478bd9Sstevel@tonic-gate  * main
2457c478bd9Sstevel@tonic-gate  */
246*8c332a0dSja int
2477c478bd9Sstevel@tonic-gate main(int argc, char **argv)
2487c478bd9Sstevel@tonic-gate {
2497c478bd9Sstevel@tonic-gate 	struct addrinfo *ai_dst = NULL;		/* destination host */
2507c478bd9Sstevel@tonic-gate 	/*
2517c478bd9Sstevel@tonic-gate 	 * "probing_successful" indicates if we could successfully send probes,
2527c478bd9Sstevel@tonic-gate 	 * not necessarily received reply from the target (this behavior is from
2537c478bd9Sstevel@tonic-gate 	 * the original traceroute). It's _B_FALSE if packlen is invalid, or no
2547c478bd9Sstevel@tonic-gate 	 * interfaces found.
2557c478bd9Sstevel@tonic-gate 	 */
2567c478bd9Sstevel@tonic-gate 	boolean_t probing_successful = _B_FALSE;
2577c478bd9Sstevel@tonic-gate 	int longjmp_return;			/* return value from longjump */
2587c478bd9Sstevel@tonic-gate 	int i = 0;
2597c478bd9Sstevel@tonic-gate 	char *cp;
2607c478bd9Sstevel@tonic-gate 	int op;
2617c478bd9Sstevel@tonic-gate 	char *ep;
2627c478bd9Sstevel@tonic-gate 	char temp_buf[INET6_ADDRSTRLEN];	/* use for inet_ntop() */
2637c478bd9Sstevel@tonic-gate 	double pause;
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	/*
2667c478bd9Sstevel@tonic-gate 	 * A raw socket will be used for IPv4 if there is sufficient
2677c478bd9Sstevel@tonic-gate 	 * privilege.
2687c478bd9Sstevel@tonic-gate 	 */
2697c478bd9Sstevel@tonic-gate 	raw_req = priv_ineffect(PRIV_NET_RAWACCESS);
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	/*
2727c478bd9Sstevel@tonic-gate 	 * We'll need the privilege only when we open the sockets; that's
2737c478bd9Sstevel@tonic-gate 	 * when we'll fail if the program has insufficient privileges.
2747c478bd9Sstevel@tonic-gate 	 */
2757c478bd9Sstevel@tonic-gate 	(void) __init_suid_priv(PU_CLEARLIMITSET, PRIV_NET_ICMPACCESS,
2767c478bd9Sstevel@tonic-gate 	    raw_req ? PRIV_NET_RAWACCESS : NULL, NULL);
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	(void) setlinebuf(stdout);
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	if ((cp = strrchr(argv[0], '/')) != NULL)
2817c478bd9Sstevel@tonic-gate 		prog = cp + 1;
2827c478bd9Sstevel@tonic-gate 	else
2837c478bd9Sstevel@tonic-gate 		prog = argv[0];
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	opterr = 0;
2867c478bd9Sstevel@tonic-gate 	while ((op = getopt(argc, argv, "adFIlnrSvxA:c:f:g:i:L:m:P:p:Q:q:s:"
2877c478bd9Sstevel@tonic-gate 	    "t:w:")) != EOF) {
2887c478bd9Sstevel@tonic-gate 		switch (op) {
2897c478bd9Sstevel@tonic-gate 		case 'A':
2907c478bd9Sstevel@tonic-gate 			if (strcmp(optarg, "inet") == 0) {
2917c478bd9Sstevel@tonic-gate 				family_input = AF_INET;
2927c478bd9Sstevel@tonic-gate 			} else if (strcmp(optarg, "inet6") == 0) {
2937c478bd9Sstevel@tonic-gate 				family_input = AF_INET6;
2947c478bd9Sstevel@tonic-gate 			} else {
2957c478bd9Sstevel@tonic-gate 				Fprintf(stderr,
2967c478bd9Sstevel@tonic-gate 				    "%s: unknown address family %s\n",
2977c478bd9Sstevel@tonic-gate 				    prog, optarg);
2987c478bd9Sstevel@tonic-gate 				exit(EXIT_FAILURE);
2997c478bd9Sstevel@tonic-gate 			}
3007c478bd9Sstevel@tonic-gate 			break;
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 		case 'a':
3037c478bd9Sstevel@tonic-gate 			probe_all = _B_TRUE;
3047c478bd9Sstevel@tonic-gate 			break;
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 		case 'c':
3077c478bd9Sstevel@tonic-gate 			class = str2int(optarg, "traffic class", 0,
3087c478bd9Sstevel@tonic-gate 			    MAX_TRAFFIC_CLASS);
3097c478bd9Sstevel@tonic-gate 			break;
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 		case 'd':
3127c478bd9Sstevel@tonic-gate 			options |= SO_DEBUG;
3137c478bd9Sstevel@tonic-gate 			break;
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 		case 'f':
3167c478bd9Sstevel@tonic-gate 			first_ttl = str2int(optarg, "first ttl", 1, MAXTTL);
3177c478bd9Sstevel@tonic-gate 			break;
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 		case 'F':
3207c478bd9Sstevel@tonic-gate 			off = IP_DF;
3217c478bd9Sstevel@tonic-gate 			break;
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 		case 'g':
3247c478bd9Sstevel@tonic-gate 			if (!raw_req) {
3257c478bd9Sstevel@tonic-gate 				Fprintf(stderr,
3267c478bd9Sstevel@tonic-gate 				    "%s: privilege to specify a loose source "
3277c478bd9Sstevel@tonic-gate 				    "route gateway is unavailable\n",
3287c478bd9Sstevel@tonic-gate 				    prog);
3297c478bd9Sstevel@tonic-gate 				exit(EXIT_FAILURE);
3307c478bd9Sstevel@tonic-gate 			}
3317d6fbbeaSblu 			if (gw_count >= MAXMAX_GWS) {
3327c478bd9Sstevel@tonic-gate 				Fprintf(stderr,
3337c478bd9Sstevel@tonic-gate 				    "%s: Too many gateways\n", prog);
3347c478bd9Sstevel@tonic-gate 				exit(EXIT_FAILURE);
3357c478bd9Sstevel@tonic-gate 			}
3367c478bd9Sstevel@tonic-gate 			gwlist[gw_count] = strdup(optarg);
3377c478bd9Sstevel@tonic-gate 			if (gwlist[gw_count] == NULL) {
3387c478bd9Sstevel@tonic-gate 				Fprintf(stderr, "%s: strdup %s\n", prog,
3397c478bd9Sstevel@tonic-gate 				    strerror(errno));
3407c478bd9Sstevel@tonic-gate 				exit(EXIT_FAILURE);
3417c478bd9Sstevel@tonic-gate 			}
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 			++gw_count;
3447c478bd9Sstevel@tonic-gate 			break;
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 		case 'l':
3477c478bd9Sstevel@tonic-gate 			showttl = _B_TRUE;
3487c478bd9Sstevel@tonic-gate 			break;
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 		case 'i':
3517c478bd9Sstevel@tonic-gate 			/* this can be IF name or IF index */
3527c478bd9Sstevel@tonic-gate 			if_index = (uint_t)strtol(optarg, &ep, 10);
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 			/* convert IF index <-->  IF name */
3557c478bd9Sstevel@tonic-gate 			if (errno != 0 || *ep != '\0') {
3567c478bd9Sstevel@tonic-gate 				device = optarg;
3577c478bd9Sstevel@tonic-gate 				if_index = if_nametoindex((const char *)device);
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 				/*
3607c478bd9Sstevel@tonic-gate 				 * In case it fails, check to see if the problem
3617c478bd9Sstevel@tonic-gate 				 * is other than "IF not found".
3627c478bd9Sstevel@tonic-gate 				 */
3637c478bd9Sstevel@tonic-gate 				if (if_index == 0 && errno != ENXIO) {
3647c478bd9Sstevel@tonic-gate 					Fprintf(stderr, "%s: if_nametoindex:"
3657c478bd9Sstevel@tonic-gate 					    "%s\n", prog, strerror(errno));
3667c478bd9Sstevel@tonic-gate 					exit(EXIT_FAILURE);
3677c478bd9Sstevel@tonic-gate 				}
3687c478bd9Sstevel@tonic-gate 			} else {
3697c478bd9Sstevel@tonic-gate 				device = (char *)malloc(LIFNAMSIZ + 1);
3707c478bd9Sstevel@tonic-gate 				if (device == NULL) {
3717c478bd9Sstevel@tonic-gate 					Fprintf(stderr, "%s: malloc: %s\n",
3727c478bd9Sstevel@tonic-gate 					    prog, strerror(errno));
3737c478bd9Sstevel@tonic-gate 					exit(EXIT_FAILURE);
3747c478bd9Sstevel@tonic-gate 				}
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 				device = if_indextoname(if_index, device);
3777c478bd9Sstevel@tonic-gate 				if (device != NULL) {
3787c478bd9Sstevel@tonic-gate 					device[LIFNAMSIZ] = '\0';
3797c478bd9Sstevel@tonic-gate 				} else if (errno != ENXIO) {
3807c478bd9Sstevel@tonic-gate 					/*
3817c478bd9Sstevel@tonic-gate 					 * The problem was other than "index
3827c478bd9Sstevel@tonic-gate 					 * not found".
3837c478bd9Sstevel@tonic-gate 					 */
3847c478bd9Sstevel@tonic-gate 					Fprintf(stderr, "%s: if_indextoname:"
3857c478bd9Sstevel@tonic-gate 					    "%s\n", prog, strerror(errno));
3867c478bd9Sstevel@tonic-gate 					exit(EXIT_FAILURE);
3877c478bd9Sstevel@tonic-gate 				}
3887c478bd9Sstevel@tonic-gate 			}
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 			if (device == NULL || if_index == 0) {
3917c478bd9Sstevel@tonic-gate 				Fprintf(stderr, "%s: interface %s "
3927c478bd9Sstevel@tonic-gate 				    "doesn't match any actual interfaces\n",
3937c478bd9Sstevel@tonic-gate 				    prog, optarg);
3947c478bd9Sstevel@tonic-gate 				exit(EXIT_FAILURE);
3957c478bd9Sstevel@tonic-gate 			}
3967c478bd9Sstevel@tonic-gate 			break;
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 		case 'I':
3997c478bd9Sstevel@tonic-gate 			useicmp = _B_TRUE;
4007c478bd9Sstevel@tonic-gate 			break;
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 		case 'L':
4037c478bd9Sstevel@tonic-gate 			flow = str2int(optarg, "flow label", 0, MAX_FLOW_LABEL);
4047c478bd9Sstevel@tonic-gate 			break;
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 		case 'm':
4077c478bd9Sstevel@tonic-gate 			max_ttl = str2int(optarg, "max ttl(hop limit)", 1,
4087c478bd9Sstevel@tonic-gate 			    MAXTTL);
4097c478bd9Sstevel@tonic-gate 			break;
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 		case 'n':
4127c478bd9Sstevel@tonic-gate 			nflag = _B_TRUE;
4137c478bd9Sstevel@tonic-gate 			break;
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 		case 'P':
4167c478bd9Sstevel@tonic-gate 			pause = str2dbl(optarg, "pause", 0, INT_MAX);
4177c478bd9Sstevel@tonic-gate 			delay.tv_sec = (time_t)pause;
4187c478bd9Sstevel@tonic-gate 			delay.tv_usec = (suseconds_t)((pause - delay.tv_sec) *
4197c478bd9Sstevel@tonic-gate 			    1000000);
4207c478bd9Sstevel@tonic-gate 			break;
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 		case 'p':
4237c478bd9Sstevel@tonic-gate 			port = str2int(optarg, "port", 1, MAX_PORT);
4247c478bd9Sstevel@tonic-gate 			break;
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 		case 'Q':
4277c478bd9Sstevel@tonic-gate 			max_timeout = str2int(optarg, "max timeout", 1, -1);
4287c478bd9Sstevel@tonic-gate 			break;
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 		case 'q':
4317c478bd9Sstevel@tonic-gate 			nprobes = str2int(optarg, "nprobes", 1, -1);
4327c478bd9Sstevel@tonic-gate 			break;
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 		case 'r':
4357c478bd9Sstevel@tonic-gate 			options |= SO_DONTROUTE;
4367c478bd9Sstevel@tonic-gate 			break;
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 		case 'S':
4397c478bd9Sstevel@tonic-gate 			collect_stat = _B_TRUE;
4407c478bd9Sstevel@tonic-gate 			break;
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 		case 's':
4437c478bd9Sstevel@tonic-gate 			/*
4447c478bd9Sstevel@tonic-gate 			 * set the ip source address of the outbound
4457c478bd9Sstevel@tonic-gate 			 * probe (e.g., on a multi-homed host).
4467c478bd9Sstevel@tonic-gate 			 */
4477c478bd9Sstevel@tonic-gate 			source_input = optarg;
4487c478bd9Sstevel@tonic-gate 			break;
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 		case 't':
4517c478bd9Sstevel@tonic-gate 			tos = (uchar_t)str2int(optarg, "tos", 0, MAX_TOS);
4527c478bd9Sstevel@tonic-gate 			settos = _B_TRUE;
4537c478bd9Sstevel@tonic-gate 			break;
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 		case 'v':
4567c478bd9Sstevel@tonic-gate 			verbose = _B_TRUE;
4577c478bd9Sstevel@tonic-gate 			break;
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 		case 'x':
4607c478bd9Sstevel@tonic-gate 			docksum = _B_FALSE;
4617c478bd9Sstevel@tonic-gate 			break;
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 		case 'w':
4647c478bd9Sstevel@tonic-gate 			waittime = str2int(optarg, "wait time", 2, -1);
4657c478bd9Sstevel@tonic-gate 			break;
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 		default:
4687c478bd9Sstevel@tonic-gate 			usage();
4697c478bd9Sstevel@tonic-gate 			break;
4707c478bd9Sstevel@tonic-gate 		}
4717c478bd9Sstevel@tonic-gate 	}
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 	/*
4747c478bd9Sstevel@tonic-gate 	 * If it's probe_all, SIGQUIT makes traceroute exit(). But we set the
4757c478bd9Sstevel@tonic-gate 	 * address to jump back to in traceroute(). Until then, we'll need to
4767c478bd9Sstevel@tonic-gate 	 * temporarily specify one.
4777c478bd9Sstevel@tonic-gate 	 */
4787c478bd9Sstevel@tonic-gate 	if (probe_all) {
4797c478bd9Sstevel@tonic-gate 		if ((longjmp_return = setjmp(env)) != 0) {
4807c478bd9Sstevel@tonic-gate 			if (longjmp_return == SIGQUIT) {
4817c478bd9Sstevel@tonic-gate 				Printf("(exiting)\n");
4827c478bd9Sstevel@tonic-gate 				exit(EXIT_SUCCESS);
4837c478bd9Sstevel@tonic-gate 			} else {		/* should never happen */
4847c478bd9Sstevel@tonic-gate 				exit(EXIT_FAILURE);
4857c478bd9Sstevel@tonic-gate 			}
4867c478bd9Sstevel@tonic-gate 		}
4877c478bd9Sstevel@tonic-gate 		(void) signal(SIGQUIT, sig_handler);
4887c478bd9Sstevel@tonic-gate 	}
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 	if ((gw_count > 0) && (options & SO_DONTROUTE)) {
4917c478bd9Sstevel@tonic-gate 		Fprintf(stderr, "%s: loose source route gateways (-g)"
4927c478bd9Sstevel@tonic-gate 		    " cannot be specified when probe packets are sent"
4937c478bd9Sstevel@tonic-gate 		    " directly to a host on an attached network (-r)\n",
4947c478bd9Sstevel@tonic-gate 		    prog);
4957c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
4967c478bd9Sstevel@tonic-gate 	}
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 	i = argc - optind;
4997c478bd9Sstevel@tonic-gate 	if (i == 1 || i == 2) {
5007c478bd9Sstevel@tonic-gate 		hostname = argv[optind];
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 		if (i == 2) {
5037c478bd9Sstevel@tonic-gate 			/* accept any length now, we'll check it later */
5047c478bd9Sstevel@tonic-gate 			packlen_input = str2int(argv[optind + 1],
5057c478bd9Sstevel@tonic-gate 			    "packet length", 0, -1);
5067c478bd9Sstevel@tonic-gate 		}
5077c478bd9Sstevel@tonic-gate 	} else {
5087c478bd9Sstevel@tonic-gate 		usage();
5097c478bd9Sstevel@tonic-gate 	}
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 	if (first_ttl > max_ttl) {
5127c478bd9Sstevel@tonic-gate 		Fprintf(stderr,
5137c478bd9Sstevel@tonic-gate 		    "%s: first ttl(hop limit) (%d) may not be greater"
5147c478bd9Sstevel@tonic-gate 		    " than max ttl(hop limit) (%d)\n",
5157c478bd9Sstevel@tonic-gate 		    prog, first_ttl, max_ttl);
5167c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
5177c478bd9Sstevel@tonic-gate 	}
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 	/* resolve hostnames */
5207c478bd9Sstevel@tonic-gate 	resolve_nodes(&family_input, &ai_dst);
5217c478bd9Sstevel@tonic-gate 	if (ai_dst == NULL) {
5227c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
5237c478bd9Sstevel@tonic-gate 	}
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	/*
5267c478bd9Sstevel@tonic-gate 	 * If it's probe_all, SIGINT makes traceroute skip to probing next IP
5277c478bd9Sstevel@tonic-gate 	 * address of the target. The new interrupt handler is assigned in
5287c478bd9Sstevel@tonic-gate 	 * traceroute() function. Until then let's ignore the signal.
5297c478bd9Sstevel@tonic-gate 	 */
5307c478bd9Sstevel@tonic-gate 	if (probe_all)
5317c478bd9Sstevel@tonic-gate 		(void) signal(SIGINT, SIG_IGN);
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	ident = (getpid() & 0xffff) | 0x8000;
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	/*
5367c478bd9Sstevel@tonic-gate 	 * We KNOW that probe_all == TRUE if family is AF_UNSPEC,
5377c478bd9Sstevel@tonic-gate 	 * since family is set to the specific AF found unless it's
5387c478bd9Sstevel@tonic-gate 	 * probe_all. So if family == AF_UNSPEC, we need to init pr4 and pr6.
5397c478bd9Sstevel@tonic-gate 	 */
5407c478bd9Sstevel@tonic-gate 	switch (family_input) {
5417c478bd9Sstevel@tonic-gate 	case AF_UNSPEC:
5427c478bd9Sstevel@tonic-gate 		pr4 = (struct pr_set *)malloc(sizeof (struct pr_set));
5437c478bd9Sstevel@tonic-gate 		if (pr4 == NULL) {
5447c478bd9Sstevel@tonic-gate 			Fprintf(stderr,
5457c478bd9Sstevel@tonic-gate 			    "%s: malloc %s\n", prog, strerror(errno));
5467c478bd9Sstevel@tonic-gate 			exit(EXIT_FAILURE);
5477c478bd9Sstevel@tonic-gate 		}
5487c478bd9Sstevel@tonic-gate 		pr6 = (struct pr_set *)malloc(sizeof (struct pr_set));
5497c478bd9Sstevel@tonic-gate 		if (pr6 == NULL) {
5507c478bd9Sstevel@tonic-gate 			Fprintf(stderr,
5517c478bd9Sstevel@tonic-gate 			    "%s: malloc %s\n", prog, strerror(errno));
5527c478bd9Sstevel@tonic-gate 			exit(EXIT_FAILURE);
5537c478bd9Sstevel@tonic-gate 		}
5547c478bd9Sstevel@tonic-gate 		setup_protocol(pr6, AF_INET6);
5557c478bd9Sstevel@tonic-gate 		setup_protocol(pr4, AF_INET);
5567c478bd9Sstevel@tonic-gate 		outip6 = (*pr6->set_buffers_fn)(pr6->packlen);
5577c478bd9Sstevel@tonic-gate 		setup_socket(pr6, pr6->packlen);
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 		outip4 = (*pr4->set_buffers_fn)(pr4->packlen);
5607c478bd9Sstevel@tonic-gate 		setup_socket(pr4, pr4->packlen);
5617c478bd9Sstevel@tonic-gate 		num_ifs6 = set_src_addr(pr6, &al6);
5627c478bd9Sstevel@tonic-gate 		num_ifs4 = set_src_addr(pr4, &al4);
5637c478bd9Sstevel@tonic-gate 		break;
5647c478bd9Sstevel@tonic-gate 	case AF_INET6:
5657c478bd9Sstevel@tonic-gate 		pr6 = (struct pr_set *)malloc(sizeof (struct pr_set));
5667c478bd9Sstevel@tonic-gate 		if (pr6 == NULL) {
5677c478bd9Sstevel@tonic-gate 			Fprintf(stderr,
5687c478bd9Sstevel@tonic-gate 			    "%s: malloc %s\n", prog, strerror(errno));
5697c478bd9Sstevel@tonic-gate 			exit(EXIT_FAILURE);
5707c478bd9Sstevel@tonic-gate 		}
5717c478bd9Sstevel@tonic-gate 		setup_protocol(pr6, AF_INET6);
5727c478bd9Sstevel@tonic-gate 		outip6 = (*pr6->set_buffers_fn)(pr6->packlen);
5737c478bd9Sstevel@tonic-gate 		setup_socket(pr6, pr6->packlen);
5747c478bd9Sstevel@tonic-gate 		num_ifs6 = set_src_addr(pr6, &al6);
5757c478bd9Sstevel@tonic-gate 		break;
5767c478bd9Sstevel@tonic-gate 	case AF_INET:
5777c478bd9Sstevel@tonic-gate 		pr4 = (struct pr_set *)malloc(sizeof (struct pr_set));
5787c478bd9Sstevel@tonic-gate 		if (pr4 == NULL) {
5797c478bd9Sstevel@tonic-gate 			Fprintf(stderr,
5807c478bd9Sstevel@tonic-gate 			    "%s: malloc %s\n", prog, strerror(errno));
5817c478bd9Sstevel@tonic-gate 			exit(EXIT_FAILURE);
5827c478bd9Sstevel@tonic-gate 		}
5837c478bd9Sstevel@tonic-gate 		setup_protocol(pr4, AF_INET);
5847c478bd9Sstevel@tonic-gate 		outip4 = (*pr4->set_buffers_fn)(pr4->packlen);
5857c478bd9Sstevel@tonic-gate 		setup_socket(pr4, pr4->packlen);
5867c478bd9Sstevel@tonic-gate 		num_ifs4 = set_src_addr(pr4, &al4);
5877c478bd9Sstevel@tonic-gate 		break;
5887c478bd9Sstevel@tonic-gate 	default:
5897c478bd9Sstevel@tonic-gate 		Fprintf(stderr, "%s: unknow address family.\n", prog);
5907c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
5917c478bd9Sstevel@tonic-gate 	}
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 	if (num_v4 + num_v6 > 1 && !probe_all) {
5947c478bd9Sstevel@tonic-gate 		if (ai_dst->ai_family == AF_INET) {
5957c478bd9Sstevel@tonic-gate 			Fprintf(stderr,
5967c478bd9Sstevel@tonic-gate 			    "%s: Warning: %s has multiple addresses;"
5977c478bd9Sstevel@tonic-gate 			    " using %s\n", prog, hostname,
5987c478bd9Sstevel@tonic-gate 			    inet_ntop(AF_INET,
5997c478bd9Sstevel@tonic-gate 			    /* LINTED E_BAD_PTR_CAST_ALIGN */
6007c478bd9Sstevel@tonic-gate 			    (void *)&((struct sockaddr_in *)
6017c478bd9Sstevel@tonic-gate 			    ai_dst->ai_addr)->sin_addr,
6027c478bd9Sstevel@tonic-gate 			    temp_buf, sizeof (temp_buf)));
6037c478bd9Sstevel@tonic-gate 		} else {
6047c478bd9Sstevel@tonic-gate 			Fprintf(stderr,
6057c478bd9Sstevel@tonic-gate 			    "%s: Warning: %s has multiple addresses;"
6067c478bd9Sstevel@tonic-gate 			    " using %s\n", prog, hostname,
6077c478bd9Sstevel@tonic-gate 			    inet_ntop(AF_INET6,
6087c478bd9Sstevel@tonic-gate 			    /* LINTED E_BAD_PTR_CAST_ALIGN */
6097c478bd9Sstevel@tonic-gate 			    (void *)&((struct sockaddr_in6 *)
6107c478bd9Sstevel@tonic-gate 			    ai_dst->ai_addr)->sin6_addr,
6117c478bd9Sstevel@tonic-gate 			    temp_buf, sizeof (temp_buf)));
6127c478bd9Sstevel@tonic-gate 		}
6137c478bd9Sstevel@tonic-gate 	}
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 	if (num_ifs4 + num_ifs6 > 0) {
6167c478bd9Sstevel@tonic-gate 		trace_it(ai_dst);
6177c478bd9Sstevel@tonic-gate 		probing_successful = _B_TRUE;
6187c478bd9Sstevel@tonic-gate 	}
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 	(void) close(rcvsock4);
6217c478bd9Sstevel@tonic-gate 	(void) close(sndsock4);
6227c478bd9Sstevel@tonic-gate 	(void) close(rcvsock6);
6237c478bd9Sstevel@tonic-gate 	(void) close(sndsock6);
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 	/*
6267c478bd9Sstevel@tonic-gate 	 * if we could probe any of the IP addresses of the target, that means
6277c478bd9Sstevel@tonic-gate 	 * this was a successful operation
6287c478bd9Sstevel@tonic-gate 	 */
6297c478bd9Sstevel@tonic-gate 	if (probing_successful)
630*8c332a0dSja 		return (EXIT_SUCCESS);
6317c478bd9Sstevel@tonic-gate 	else
632*8c332a0dSja 		return (EXIT_FAILURE);
6337c478bd9Sstevel@tonic-gate }
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate /*
6367c478bd9Sstevel@tonic-gate  * print "unknown host" message
6377c478bd9Sstevel@tonic-gate  */
6387c478bd9Sstevel@tonic-gate static void
6397c478bd9Sstevel@tonic-gate print_unknown_host_msg(const char *protocol, const char *host)
6407c478bd9Sstevel@tonic-gate {
6417c478bd9Sstevel@tonic-gate 	Fprintf(stderr, "%s: unknown%s host %s\n", prog, protocol, host);
6427c478bd9Sstevel@tonic-gate }
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate /*
6457c478bd9Sstevel@tonic-gate  * resolve destination host and gateways
6467c478bd9Sstevel@tonic-gate  */
6477c478bd9Sstevel@tonic-gate static void
6487c478bd9Sstevel@tonic-gate resolve_nodes(int *family, struct addrinfo **ai_dstp)
6497c478bd9Sstevel@tonic-gate {
6507c478bd9Sstevel@tonic-gate 	struct addrinfo *ai_dst = NULL;
6517c478bd9Sstevel@tonic-gate 	struct addrinfo *aip = NULL;
6527c478bd9Sstevel@tonic-gate 	int num_resolved_gw = 0;
6537c478bd9Sstevel@tonic-gate 	int num_resolved_gw6 = 0;
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate 	get_hostinfo(hostname, *family, &ai_dst);
6567c478bd9Sstevel@tonic-gate 	if (ai_dst == NULL) {
6577c478bd9Sstevel@tonic-gate 		print_unknown_host_msg("", hostname);
6587c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
6597c478bd9Sstevel@tonic-gate 	}
6607c478bd9Sstevel@tonic-gate 	/* Get a count of the v4 & v6 addresses */
6617c478bd9Sstevel@tonic-gate 	for (aip = ai_dst; aip != NULL; aip = aip->ai_next) {
6627c478bd9Sstevel@tonic-gate 		switch (aip->ai_family) {
6637c478bd9Sstevel@tonic-gate 		case AF_INET:
6647c478bd9Sstevel@tonic-gate 			num_v4++;
6657c478bd9Sstevel@tonic-gate 			break;
6667c478bd9Sstevel@tonic-gate 		case AF_INET6:
6677c478bd9Sstevel@tonic-gate 			num_v6++;
6687c478bd9Sstevel@tonic-gate 			break;
6697c478bd9Sstevel@tonic-gate 		}
6707c478bd9Sstevel@tonic-gate 	}
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 	if (*family == AF_UNSPEC && !probe_all) {
6737c478bd9Sstevel@tonic-gate 		*family = ai_dst->ai_family;
6747c478bd9Sstevel@tonic-gate 	}
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 	/* resolve gateways */
6777c478bd9Sstevel@tonic-gate 	if (gw_count > 0) {
6787c478bd9Sstevel@tonic-gate 		get_gwaddrs(gwlist, *family, gwIPlist, gwIP6list,
6797c478bd9Sstevel@tonic-gate 		    &num_resolved_gw, &num_resolved_gw6);
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate 		/* we couldn't resolve a gateway as an IPv6 host */
6827c478bd9Sstevel@tonic-gate 		if (num_resolved_gw6 != gw_count && num_v6 != 0) {
6837c478bd9Sstevel@tonic-gate 			if (*family == AF_INET6 || *family == AF_UNSPEC)
6847c478bd9Sstevel@tonic-gate 				print_unknown_host_msg(" IPv6",
6857c478bd9Sstevel@tonic-gate 				    gwlist[num_resolved_gw6]);
6867c478bd9Sstevel@tonic-gate 			num_v6 = 0;
6877c478bd9Sstevel@tonic-gate 		}
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 		/* we couldn't resolve a gateway as an IPv4 host */
6907c478bd9Sstevel@tonic-gate 		if (num_resolved_gw != gw_count && num_v4 != 0) {
6917c478bd9Sstevel@tonic-gate 			if (*family == AF_INET || *family == AF_UNSPEC)
6927c478bd9Sstevel@tonic-gate 				print_unknown_host_msg(" IPv4",
6937c478bd9Sstevel@tonic-gate 				    gwlist[num_resolved_gw]);
6947c478bd9Sstevel@tonic-gate 			num_v4 = 0;
6957c478bd9Sstevel@tonic-gate 		}
6967c478bd9Sstevel@tonic-gate 	}
6977c478bd9Sstevel@tonic-gate 
6987d6fbbeaSblu 	*ai_dstp = (num_v4 + num_v6 > 0) ? ai_dst : NULL;
6997c478bd9Sstevel@tonic-gate }
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate /*
7027c478bd9Sstevel@tonic-gate  * Given IP address or hostname, return v4 and v6 hostinfo lists.
7037c478bd9Sstevel@tonic-gate  * Assumes that hostinfo ** ptrs are non-null.
7047c478bd9Sstevel@tonic-gate  */
7057c478bd9Sstevel@tonic-gate static void
7067c478bd9Sstevel@tonic-gate get_hostinfo(char *host, int family, struct addrinfo **aipp)
7077c478bd9Sstevel@tonic-gate {
7087c478bd9Sstevel@tonic-gate 	struct addrinfo hints, *ai;
7097c478bd9Sstevel@tonic-gate 	struct in6_addr addr6;
7107c478bd9Sstevel@tonic-gate 	struct in_addr addr;
7117c478bd9Sstevel@tonic-gate 	char temp_buf[INET6_ADDRSTRLEN];	/* use for inet_ntop() */
7127c478bd9Sstevel@tonic-gate 	int rc;
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 	/*
7157c478bd9Sstevel@tonic-gate 	 * Take care of v4-mapped addresses. It should run same as v4, after
7167c478bd9Sstevel@tonic-gate 	 * chopping off the prefix, leaving the IPv4 address
7177c478bd9Sstevel@tonic-gate 	 */
7187c478bd9Sstevel@tonic-gate 	if ((inet_pton(AF_INET6, host, &addr6) > 0) &&
7197c478bd9Sstevel@tonic-gate 	    IN6_IS_ADDR_V4MAPPED(&addr6)) {
7207c478bd9Sstevel@tonic-gate 		/* peel off the "mapping" stuff, leaving 32 bit IPv4 address */
7217c478bd9Sstevel@tonic-gate 		IN6_V4MAPPED_TO_INADDR(&addr6, &addr);
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 		/* convert it back to a string */
7247c478bd9Sstevel@tonic-gate 		(void) inet_ntop(AF_INET, (void *)&addr, temp_buf,
7257c478bd9Sstevel@tonic-gate 		    sizeof (temp_buf));
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate 		/* now the host is an IPv4 address */
7287c478bd9Sstevel@tonic-gate 		(void) strcpy(host, temp_buf);
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 		/*
7317c478bd9Sstevel@tonic-gate 		 * If it's a mapped address, we convert it into IPv4
7327c478bd9Sstevel@tonic-gate 		 * address because traceroute will send and receive IPv4
7337c478bd9Sstevel@tonic-gate 		 * packets for that address. Therefore, it's a failure case to
7347c478bd9Sstevel@tonic-gate 		 * ask get_hostinfo() to treat a mapped address as an IPv6
7357c478bd9Sstevel@tonic-gate 		 * address.
7367c478bd9Sstevel@tonic-gate 		 */
7377c478bd9Sstevel@tonic-gate 		if (family == AF_INET6) {
7387c478bd9Sstevel@tonic-gate 			return;
7397c478bd9Sstevel@tonic-gate 		}
7407c478bd9Sstevel@tonic-gate 	}
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 	(void) memset(&hints, 0, sizeof (hints));
7437c478bd9Sstevel@tonic-gate 	hints.ai_family = family;
7447c478bd9Sstevel@tonic-gate 	hints.ai_flags = AI_ADDRCONFIG;
7457c478bd9Sstevel@tonic-gate 	rc = getaddrinfo(host, NULL, &hints, &ai);
7467c478bd9Sstevel@tonic-gate 	if (rc != 0) {
7477c478bd9Sstevel@tonic-gate 		if (rc != EAI_NONAME)
7487c478bd9Sstevel@tonic-gate 			Fprintf(stderr, "%s: getaddrinfo: %s\n", prog,
7497c478bd9Sstevel@tonic-gate 			    gai_strerror(rc));
7507d6fbbeaSblu 		*aipp = NULL;
7517c478bd9Sstevel@tonic-gate 		return;
7527c478bd9Sstevel@tonic-gate 	}
7537c478bd9Sstevel@tonic-gate 	*aipp = ai;
7547c478bd9Sstevel@tonic-gate }
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate /*
7577c478bd9Sstevel@tonic-gate  * Calculate the packet length to be used, and check against the valid range.
7587c478bd9Sstevel@tonic-gate  * Returns -1 if range check fails.
7597c478bd9Sstevel@tonic-gate  */
7607c478bd9Sstevel@tonic-gate static uint_t
7617c478bd9Sstevel@tonic-gate calc_packetlen(int plen_input, struct pr_set *pr)
7627c478bd9Sstevel@tonic-gate {
7637c478bd9Sstevel@tonic-gate 	int minpacket;			/* min ip packet size */
7647c478bd9Sstevel@tonic-gate 	int optlen;			/* length of ip options */
7657c478bd9Sstevel@tonic-gate 	int plen;
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate 	/*
7687c478bd9Sstevel@tonic-gate 	 * LBNL bug fixed: miscalculation of optlen
7697c478bd9Sstevel@tonic-gate 	 */
7707c478bd9Sstevel@tonic-gate 	if (gw_count > 0) {
7717c478bd9Sstevel@tonic-gate 		/*
7727c478bd9Sstevel@tonic-gate 		 * IPv4:
7737c478bd9Sstevel@tonic-gate 		 * ----
7747c478bd9Sstevel@tonic-gate 		 * 5 (NO OPs) + 3 (code, len, ptr) + gateways
7757c478bd9Sstevel@tonic-gate 		 * IP options field can hold up to 9 gateways. But the API
7767c478bd9Sstevel@tonic-gate 		 * allows you to specify only 8, because the last one is the
7777c478bd9Sstevel@tonic-gate 		 * destination host. When this packet is sent, on the wire
7787c478bd9Sstevel@tonic-gate 		 * you see one gateway replaced by 4 NO OPs. The other 1 NO
7797c478bd9Sstevel@tonic-gate 		 * OP is for alignment
7807c478bd9Sstevel@tonic-gate 		 *
7817c478bd9Sstevel@tonic-gate 		 * IPv6:
7827c478bd9Sstevel@tonic-gate 		 * ----
7837c478bd9Sstevel@tonic-gate 		 * Well, formula is different, but the result is same.
7847c478bd9Sstevel@tonic-gate 		 * 8 byte fixed part for Type 0 Routing header, followed by
7857c478bd9Sstevel@tonic-gate 		 * gateway addresses
7867c478bd9Sstevel@tonic-gate 		 */
7877c478bd9Sstevel@tonic-gate 		optlen = 8 + gw_count * pr->addr_len;
7887c478bd9Sstevel@tonic-gate 	} else {
7897c478bd9Sstevel@tonic-gate 		optlen = 0;
7907c478bd9Sstevel@tonic-gate 	}
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 	/* take care of the packet length calculations and checks */
7937c478bd9Sstevel@tonic-gate 	minpacket = pr->ip_hdr_len + sizeof (struct outdata) + optlen;
7947c478bd9Sstevel@tonic-gate 	if (useicmp)
7957c478bd9Sstevel@tonic-gate 		minpacket += pr->icmp_minlen;	/* minimum ICMP header size */
7967c478bd9Sstevel@tonic-gate 	else
7977c478bd9Sstevel@tonic-gate 		minpacket += sizeof (struct udphdr);
7987c478bd9Sstevel@tonic-gate 	plen = plen_input;
7997c478bd9Sstevel@tonic-gate 	if (plen == 0) {
8007c478bd9Sstevel@tonic-gate 		plen = minpacket;		/* minimum sized packet */
8017c478bd9Sstevel@tonic-gate 	} else if (minpacket > plen || plen > IP_MAXPACKET) {
8027c478bd9Sstevel@tonic-gate 		Fprintf(stderr, "%s: %s packet size must be >= %d and <= %d\n",
8037c478bd9Sstevel@tonic-gate 		    prog, pr->name, minpacket, IP_MAXPACKET);
8047c478bd9Sstevel@tonic-gate 		return (0);
8057c478bd9Sstevel@tonic-gate 	}
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 	return (plen);
8087c478bd9Sstevel@tonic-gate }
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate /*
8117c478bd9Sstevel@tonic-gate  * Sets the source address by resolving -i and -s arguments, or if -i and -s
8127c478bd9Sstevel@tonic-gate  * don't dictate any, it sets the pick_src to make sure traceroute uses the
8137c478bd9Sstevel@tonic-gate  * kernel's pick of the source address.
8147c478bd9Sstevel@tonic-gate  * Returns number of interfaces configured on the source host, 0 on error or
8157c478bd9Sstevel@tonic-gate  * there's no interface which is up amd not a loopback.
8167c478bd9Sstevel@tonic-gate  */
8177c478bd9Sstevel@tonic-gate static int
8187c478bd9Sstevel@tonic-gate set_src_addr(struct pr_set *pr, struct ifaddrlist **alp)
8197c478bd9Sstevel@tonic-gate {
8207c478bd9Sstevel@tonic-gate 	union any_in_addr *ap;
8217c478bd9Sstevel@tonic-gate 	struct ifaddrlist *al = NULL;
8227c478bd9Sstevel@tonic-gate 	struct ifaddrlist *tmp1_al = NULL;
8237c478bd9Sstevel@tonic-gate 	struct ifaddrlist *tmp2_al = NULL;
8247c478bd9Sstevel@tonic-gate 	/* LINTED E_BAD_PTR_CAST_ALIGN */
8257c478bd9Sstevel@tonic-gate 	struct sockaddr_in *sin_from = (struct sockaddr_in *)pr->from;
8267c478bd9Sstevel@tonic-gate 	/* LINTED E_BAD_PTR_CAST_ALIGN */
8277c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *sin6_from = (struct sockaddr_in6 *)pr->from;
8287c478bd9Sstevel@tonic-gate 	struct addrinfo *aip;
8297c478bd9Sstevel@tonic-gate 	char errbuf[ERRBUFSIZE];
8307c478bd9Sstevel@tonic-gate 	char temp_buf[INET6_ADDRSTRLEN];	/* use for inet_ntop() */
8317c478bd9Sstevel@tonic-gate 	int num_ifs;				/* all the interfaces  */
8327c478bd9Sstevel@tonic-gate 	int num_src_ifs;			/* exclude loopback and down */
8337c478bd9Sstevel@tonic-gate 	int i;
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 	source = source_input;
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate 	/* get the interface address list */
8387c478bd9Sstevel@tonic-gate 	num_ifs = ifaddrlist(&al, pr->family, errbuf);
8397c478bd9Sstevel@tonic-gate 	if (num_ifs < 0) {
8407c478bd9Sstevel@tonic-gate 		Fprintf(stderr, "%s: ifaddrlist: %s\n", prog, errbuf);
8417c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
8427c478bd9Sstevel@tonic-gate 	}
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate 	num_src_ifs = 0;
8457c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_ifs; i++) {
8467c478bd9Sstevel@tonic-gate 		if (!(al[i].flags & IFF_LOOPBACK) && (al[i].flags & IFF_UP))
8477c478bd9Sstevel@tonic-gate 			num_src_ifs++;
8487c478bd9Sstevel@tonic-gate 	}
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 	if (num_src_ifs == 0) {
8517c478bd9Sstevel@tonic-gate 		Fprintf(stderr, "%s: can't find any %s network interfaces\n",
8527c478bd9Sstevel@tonic-gate 		    prog, pr->name);
8537c478bd9Sstevel@tonic-gate 		return (0);
8547c478bd9Sstevel@tonic-gate 	}
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate 	/* verify the device */
8577c478bd9Sstevel@tonic-gate 	if (device != NULL) {
8587c478bd9Sstevel@tonic-gate 		tmp1_al = find_device(al, num_ifs, device);
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate 		if (tmp1_al == NULL) {
8617c478bd9Sstevel@tonic-gate 			Fprintf(stderr, "%s: %s (index %d) is an invalid %s"
8627c478bd9Sstevel@tonic-gate 			    " interface\n", prog, device, if_index, pr->name);
8637c478bd9Sstevel@tonic-gate 			free(al);
8647c478bd9Sstevel@tonic-gate 			return (0);
8657c478bd9Sstevel@tonic-gate 		}
8667c478bd9Sstevel@tonic-gate 	}
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 	/* verify the source address */
8697c478bd9Sstevel@tonic-gate 	if (source != NULL) {
8707c478bd9Sstevel@tonic-gate 		get_hostinfo(source, pr->family, &aip);
8717c478bd9Sstevel@tonic-gate 		if (aip == NULL) {
8727c478bd9Sstevel@tonic-gate 			Fprintf(stderr,
8737c478bd9Sstevel@tonic-gate 			    "%s: %s is an invalid %s source address\n",
8747c478bd9Sstevel@tonic-gate 			    prog, source, pr->name);
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate 			free(al);
8777c478bd9Sstevel@tonic-gate 			return (0);
8787c478bd9Sstevel@tonic-gate 		}
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate 		source = aip->ai_canonname;
8817c478bd9Sstevel@tonic-gate 		ap = (union any_in_addr *)
8827c478bd9Sstevel@tonic-gate 		    /* LINTED E_BAD_PTR_CAST_ALIGN */
8837c478bd9Sstevel@tonic-gate 		    &((struct sockaddr_in6 *)
8847c478bd9Sstevel@tonic-gate 		    aip->ai_addr)->sin6_addr;
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate 		/*
8877c478bd9Sstevel@tonic-gate 		 * LBNL bug fixed: used to accept any src address
8887c478bd9Sstevel@tonic-gate 		 */
8897c478bd9Sstevel@tonic-gate 		tmp2_al = find_ifaddr(al, num_ifs, ap, pr->family);
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate 		if (tmp2_al == NULL) {
8927c478bd9Sstevel@tonic-gate 			Fprintf(stderr,
8937c478bd9Sstevel@tonic-gate 			    "%s: %s is an invalid %s source address\n", prog,
8947c478bd9Sstevel@tonic-gate 			    inet_ntop(pr->family, (const void *)ap,
8957c478bd9Sstevel@tonic-gate 				temp_buf, sizeof (temp_buf)),
8967c478bd9Sstevel@tonic-gate 			    pr->name);
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate 			free(al);
8997c478bd9Sstevel@tonic-gate 			freeaddrinfo(aip);
9007c478bd9Sstevel@tonic-gate 			return (0);
9017c478bd9Sstevel@tonic-gate 		}
9027c478bd9Sstevel@tonic-gate 	}
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 	pick_src = _B_FALSE;
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate 	if (source == NULL) {			/* no -s used */
9077c478bd9Sstevel@tonic-gate 		if (device == NULL) {		/* no -i used, no -s used */
9087c478bd9Sstevel@tonic-gate 			pick_src = _B_TRUE;
9097c478bd9Sstevel@tonic-gate 		} else {			/* -i used, no -s used */
9107c478bd9Sstevel@tonic-gate 			/*
9117c478bd9Sstevel@tonic-gate 			 * -i used, but not -s, and it's IPv4: set the source
9127c478bd9Sstevel@tonic-gate 			 * address to whatever the interface has configured on
9137c478bd9Sstevel@tonic-gate 			 * it.
9147c478bd9Sstevel@tonic-gate 			 */
9157c478bd9Sstevel@tonic-gate 			if (pr->family == AF_INET)
9167c478bd9Sstevel@tonic-gate 				set_sin(pr->from, &(tmp1_al->addr), pr->family);
9177c478bd9Sstevel@tonic-gate 			else
9187c478bd9Sstevel@tonic-gate 				pick_src = _B_TRUE;
9197c478bd9Sstevel@tonic-gate 		}
9207c478bd9Sstevel@tonic-gate 	} else {				/* -s used */
9217c478bd9Sstevel@tonic-gate 		if (device == NULL) {		/* no -i used, -s used */
9227c478bd9Sstevel@tonic-gate 			set_sin(pr->from, ap, pr->family);
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate 			if (aip->ai_next != NULL) {
9257c478bd9Sstevel@tonic-gate 				Fprintf(stderr,
9267c478bd9Sstevel@tonic-gate 				    "%s: Warning: %s has multiple "
9277c478bd9Sstevel@tonic-gate 				    "addresses; using %s\n",
9287c478bd9Sstevel@tonic-gate 				    prog, source,
9297c478bd9Sstevel@tonic-gate 				    inet_ntop(pr->family,
9307c478bd9Sstevel@tonic-gate 					(const void *)pr->from_sin_addr,
9317c478bd9Sstevel@tonic-gate 					temp_buf, sizeof (temp_buf)));
9327c478bd9Sstevel@tonic-gate 			}
9337c478bd9Sstevel@tonic-gate 		} else {			/* -i and -s used */
9347c478bd9Sstevel@tonic-gate 			/*
9357c478bd9Sstevel@tonic-gate 			 * Make sure the source specified matches the
9367c478bd9Sstevel@tonic-gate 			 * interface address. You only care about this for IPv4
9377c478bd9Sstevel@tonic-gate 			 * IPv6 can handle IF not matching src address
9387c478bd9Sstevel@tonic-gate 			 */
9397c478bd9Sstevel@tonic-gate 			if (pr->family == AF_INET) {
9407c478bd9Sstevel@tonic-gate 				if (!has_addr(aip, &tmp1_al->addr)) {
9417c478bd9Sstevel@tonic-gate 					Fprintf(stderr,
9427c478bd9Sstevel@tonic-gate 					    "%s: %s is not on interface %s\n",
9437c478bd9Sstevel@tonic-gate 					    prog, source, device);
9447c478bd9Sstevel@tonic-gate 					exit(EXIT_FAILURE);
9457c478bd9Sstevel@tonic-gate 				}
9467c478bd9Sstevel@tonic-gate 				/*
9477c478bd9Sstevel@tonic-gate 				 * make sure we use the one matching the
9487c478bd9Sstevel@tonic-gate 				 * interface's address
9497c478bd9Sstevel@tonic-gate 				 */
9507c478bd9Sstevel@tonic-gate 				*ap = tmp1_al->addr;
9517c478bd9Sstevel@tonic-gate 			}
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 			set_sin(pr->from, ap, pr->family);
9547c478bd9Sstevel@tonic-gate 		}
9557c478bd9Sstevel@tonic-gate 	}
9567c478bd9Sstevel@tonic-gate 
9577c478bd9Sstevel@tonic-gate 	/*
9587c478bd9Sstevel@tonic-gate 	 * Binding at this point will set the source address to be used
9597c478bd9Sstevel@tonic-gate 	 * for both IPv4 (when raw IP datagrams are not required) and
9607c478bd9Sstevel@tonic-gate 	 * IPv6.  If the address being bound to is zero, then the kernel
9617c478bd9Sstevel@tonic-gate 	 * will end up choosing the source address when the datagram is
9627c478bd9Sstevel@tonic-gate 	 * sent.
9637c478bd9Sstevel@tonic-gate 	 *
9647c478bd9Sstevel@tonic-gate 	 * For raw IPv4 datagrams, the source address is initialized
9657c478bd9Sstevel@tonic-gate 	 * within traceroute() along with the outbound destination
9667c478bd9Sstevel@tonic-gate 	 * address.
9677c478bd9Sstevel@tonic-gate 	 */
9687c478bd9Sstevel@tonic-gate 	if (pr->family == AF_INET && !raw_req) {
9697c478bd9Sstevel@tonic-gate 		sin_from->sin_family = AF_INET;
9707c478bd9Sstevel@tonic-gate 		sin_from->sin_port = htons(ident);
9717c478bd9Sstevel@tonic-gate 		if (bind(sndsock4, (struct sockaddr *)pr->from,
9727c478bd9Sstevel@tonic-gate 			sizeof (struct sockaddr_in)) < 0) {
9737c478bd9Sstevel@tonic-gate 			Fprintf(stderr, "%s: bind: %s\n", prog,
9747c478bd9Sstevel@tonic-gate 			    strerror(errno));
9757c478bd9Sstevel@tonic-gate 			exit(EXIT_FAILURE);
9767c478bd9Sstevel@tonic-gate 		}
9777c478bd9Sstevel@tonic-gate 	} else if (pr->family == AF_INET6) {
9787c478bd9Sstevel@tonic-gate 		sin6_from->sin6_family = AF_INET6;
9797c478bd9Sstevel@tonic-gate 		sin6_from->sin6_port = htons(ident);
9807c478bd9Sstevel@tonic-gate 		if (bind(sndsock6, (struct sockaddr *)pr->from,
9817c478bd9Sstevel@tonic-gate 			sizeof (struct sockaddr_in6)) < 0) {
9827c478bd9Sstevel@tonic-gate 			Fprintf(stderr, "%s: bind: %s\n", prog,
9837c478bd9Sstevel@tonic-gate 			    strerror(errno));
9847c478bd9Sstevel@tonic-gate 			exit(EXIT_FAILURE);
9857c478bd9Sstevel@tonic-gate 		}
9867c478bd9Sstevel@tonic-gate 
9877c478bd9Sstevel@tonic-gate 		whereto6.sin6_flowinfo = htonl((class << 20) | flow);
9887c478bd9Sstevel@tonic-gate 	}
9897c478bd9Sstevel@tonic-gate 	*alp = al;
9907c478bd9Sstevel@tonic-gate 	return (num_ifs);
9917c478bd9Sstevel@tonic-gate }
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate /*
9947c478bd9Sstevel@tonic-gate  * Returns the complete ifaddrlist structure matching the desired interface
9957c478bd9Sstevel@tonic-gate  * address. Ignores interfaces which are either down or loopback.
9967c478bd9Sstevel@tonic-gate  */
9977c478bd9Sstevel@tonic-gate static struct ifaddrlist *
9987c478bd9Sstevel@tonic-gate find_ifaddr(struct ifaddrlist *al, int len, union any_in_addr *addr,
9997c478bd9Sstevel@tonic-gate     int family)
10007c478bd9Sstevel@tonic-gate {
10017c478bd9Sstevel@tonic-gate 	struct ifaddrlist *tmp_al = al;
10027c478bd9Sstevel@tonic-gate 	int i;
10037c478bd9Sstevel@tonic-gate 	size_t addr_len = (family == AF_INET) ? sizeof (struct in_addr) :
10047c478bd9Sstevel@tonic-gate 	    sizeof (struct in6_addr);
10057c478bd9Sstevel@tonic-gate 
10067c478bd9Sstevel@tonic-gate 	for (i = 0; i < len; i++, tmp_al++) {
10077c478bd9Sstevel@tonic-gate 		if ((!(tmp_al->flags & IFF_LOOPBACK) &&
10087c478bd9Sstevel@tonic-gate 		    (tmp_al->flags & IFF_UP)) &&
10097c478bd9Sstevel@tonic-gate 		    (memcmp(&tmp_al->addr, addr, addr_len) == 0))
10107c478bd9Sstevel@tonic-gate 			break;
10117c478bd9Sstevel@tonic-gate 	}
10127c478bd9Sstevel@tonic-gate 
10137c478bd9Sstevel@tonic-gate 	if (i < len) {
10147c478bd9Sstevel@tonic-gate 		return (tmp_al);
10157c478bd9Sstevel@tonic-gate 	} else {
10167c478bd9Sstevel@tonic-gate 		return (NULL);
10177c478bd9Sstevel@tonic-gate 	}
10187c478bd9Sstevel@tonic-gate }
10197c478bd9Sstevel@tonic-gate 
10207c478bd9Sstevel@tonic-gate /*
10217c478bd9Sstevel@tonic-gate  * Returns the complete ifaddrlist structure matching the desired interface name
10227c478bd9Sstevel@tonic-gate  * Ignores interfaces which are either down or loopback.
10237c478bd9Sstevel@tonic-gate  */
10247c478bd9Sstevel@tonic-gate static struct ifaddrlist *
10257c478bd9Sstevel@tonic-gate find_device(struct ifaddrlist *al, int len, char *device)
10267c478bd9Sstevel@tonic-gate {
10277c478bd9Sstevel@tonic-gate 	struct ifaddrlist *tmp_al = al;
10287c478bd9Sstevel@tonic-gate 	int i;
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate 	for (i = 0; i < len; i++, tmp_al++) {
10317c478bd9Sstevel@tonic-gate 		if ((!(tmp_al->flags & IFF_LOOPBACK) &&
10327c478bd9Sstevel@tonic-gate 		    (tmp_al->flags & IFF_UP)) &&
10337c478bd9Sstevel@tonic-gate 		    (strcmp(tmp_al->device, device) == 0))
10347c478bd9Sstevel@tonic-gate 			break;
10357c478bd9Sstevel@tonic-gate 	}
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate 	if (i < len) {
10387c478bd9Sstevel@tonic-gate 		return (tmp_al);
10397c478bd9Sstevel@tonic-gate 	} else {
10407c478bd9Sstevel@tonic-gate 		return (NULL);
10417c478bd9Sstevel@tonic-gate 	}
10427c478bd9Sstevel@tonic-gate }
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate /*
10457c478bd9Sstevel@tonic-gate  * returns _B_TRUE if given hostinfo contains the given address
10467c478bd9Sstevel@tonic-gate  */
10477c478bd9Sstevel@tonic-gate static boolean_t
10487c478bd9Sstevel@tonic-gate has_addr(struct addrinfo *ai, union any_in_addr *addr)
10497c478bd9Sstevel@tonic-gate {
10507c478bd9Sstevel@tonic-gate 	struct addrinfo *ai_tmp = NULL;
10517c478bd9Sstevel@tonic-gate 	union any_in_addr *ap;
10527c478bd9Sstevel@tonic-gate 
10537c478bd9Sstevel@tonic-gate 	for (ai_tmp = ai; ai_tmp != NULL; ai_tmp = ai_tmp->ai_next) {
10547c478bd9Sstevel@tonic-gate 		if (ai_tmp->ai_family == AF_INET6)
10557c478bd9Sstevel@tonic-gate 			continue;
10567c478bd9Sstevel@tonic-gate 		ap = (union any_in_addr *)
10577c478bd9Sstevel@tonic-gate 		    /* LINTED E_BAD_PTR_CAST_ALIGN */
10587c478bd9Sstevel@tonic-gate 		    &((struct sockaddr_in *)ai_tmp->ai_addr)->sin_addr;
10597c478bd9Sstevel@tonic-gate 		if (memcmp(ap, addr, sizeof (struct in_addr)) == 0)
10607c478bd9Sstevel@tonic-gate 			break;
10617c478bd9Sstevel@tonic-gate 	}
10627c478bd9Sstevel@tonic-gate 
10637c478bd9Sstevel@tonic-gate 	if (ai_tmp != NULL) {
10647c478bd9Sstevel@tonic-gate 		return (_B_TRUE);
10657c478bd9Sstevel@tonic-gate 	} else {
10667c478bd9Sstevel@tonic-gate 		return (_B_FALSE);
10677c478bd9Sstevel@tonic-gate 	}
10687c478bd9Sstevel@tonic-gate }
10697c478bd9Sstevel@tonic-gate 
10707c478bd9Sstevel@tonic-gate /*
10717c478bd9Sstevel@tonic-gate  * Resolve the gateway names, splitting results into v4 and v6 lists.
10727c478bd9Sstevel@tonic-gate  * Gateway addresses are added to the appropriate passed-in array; the
10737c478bd9Sstevel@tonic-gate  * number of resolved gateways for each af is returned in resolved[6].
10747c478bd9Sstevel@tonic-gate  * Assumes that passed-in arrays are large enough for MAX_GWS[6] addrs
10757c478bd9Sstevel@tonic-gate  * and resolved[6] ptrs are non-null; ignores array and counter if the
10767c478bd9Sstevel@tonic-gate  * address family param makes them irrelevant.
10777c478bd9Sstevel@tonic-gate  */
10787c478bd9Sstevel@tonic-gate static void
10797c478bd9Sstevel@tonic-gate get_gwaddrs(char **gwlist, int family, union any_in_addr *gwIPlist,
10807c478bd9Sstevel@tonic-gate     union any_in_addr *gwIPlist6, int *resolved, int *resolved6)
10817c478bd9Sstevel@tonic-gate {
10827c478bd9Sstevel@tonic-gate 	int i;
10837c478bd9Sstevel@tonic-gate 	boolean_t check_v4 = _B_TRUE, check_v6 = _B_TRUE;
10847c478bd9Sstevel@tonic-gate 	struct addrinfo *ai = NULL;
10857c478bd9Sstevel@tonic-gate 	struct addrinfo *aip = NULL;
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate 	*resolved = *resolved6 = 0;
10887c478bd9Sstevel@tonic-gate 	switch (family) {
10897c478bd9Sstevel@tonic-gate 	case AF_UNSPEC:
10907c478bd9Sstevel@tonic-gate 		break;
10917c478bd9Sstevel@tonic-gate 	case AF_INET:
10927c478bd9Sstevel@tonic-gate 		check_v6 = _B_FALSE;
10937c478bd9Sstevel@tonic-gate 		break;
10947c478bd9Sstevel@tonic-gate 	case AF_INET6:
10957c478bd9Sstevel@tonic-gate 		check_v4 = _B_FALSE;
10967c478bd9Sstevel@tonic-gate 		break;
10977c478bd9Sstevel@tonic-gate 	default:
10987c478bd9Sstevel@tonic-gate 		return;
10997c478bd9Sstevel@tonic-gate 	}
11007c478bd9Sstevel@tonic-gate 
11017c478bd9Sstevel@tonic-gate 	if (check_v4 && gw_count >= MAX_GWS) {
11027c478bd9Sstevel@tonic-gate 		check_v4 = _B_FALSE;
11037c478bd9Sstevel@tonic-gate 		Fprintf(stderr, "%s: too many IPv4 gateways\n", prog);
11047d6fbbeaSblu 		num_v4 = 0;
11057c478bd9Sstevel@tonic-gate 	}
11067c478bd9Sstevel@tonic-gate 	if (check_v6 && gw_count >= MAX_GWS6) {
11077c478bd9Sstevel@tonic-gate 		check_v6 = _B_FALSE;
11087c478bd9Sstevel@tonic-gate 		Fprintf(stderr, "%s: too many IPv6 gateways\n", prog);
11097d6fbbeaSblu 		num_v6 = 0;
11107c478bd9Sstevel@tonic-gate 	}
11117c478bd9Sstevel@tonic-gate 
11127c478bd9Sstevel@tonic-gate 	for (i = 0; i < gw_count; i++) {
11137c478bd9Sstevel@tonic-gate 		if (!check_v4 && !check_v6)
11147c478bd9Sstevel@tonic-gate 			return;
11157c478bd9Sstevel@tonic-gate 		get_hostinfo(gwlist[i], family, &ai);
11167c478bd9Sstevel@tonic-gate 		if (ai == NULL)
11177c478bd9Sstevel@tonic-gate 			return;
11187c478bd9Sstevel@tonic-gate 		if (check_v4 && num_v4 != 0) {
11197d6fbbeaSblu 			check_v4 = _B_FALSE;
11207c478bd9Sstevel@tonic-gate 			for (aip = ai; aip != NULL; aip = aip->ai_next) {
11217c478bd9Sstevel@tonic-gate 				if (aip->ai_family == AF_INET) {
11227c478bd9Sstevel@tonic-gate 					/* LINTED E_BAD_PTR_CAST_ALIGN */
11237c478bd9Sstevel@tonic-gate 					bcopy(&((struct sockaddr_in *)
11247c478bd9Sstevel@tonic-gate 					    aip->ai_addr)->sin_addr,
11257c478bd9Sstevel@tonic-gate 					    &gwIPlist[i].addr,
11267c478bd9Sstevel@tonic-gate 					    aip->ai_addrlen);
11277c478bd9Sstevel@tonic-gate 					(*resolved)++;
11287d6fbbeaSblu 					check_v4 = _B_TRUE;
11297c478bd9Sstevel@tonic-gate 					break;
11307c478bd9Sstevel@tonic-gate 				}
11317c478bd9Sstevel@tonic-gate 			}
11327c478bd9Sstevel@tonic-gate 		} else if (check_v4) {
11337c478bd9Sstevel@tonic-gate 			check_v4 = _B_FALSE;
11347c478bd9Sstevel@tonic-gate 		}
11357c478bd9Sstevel@tonic-gate 		if (check_v6 && num_v6 != 0) {
11367d6fbbeaSblu 			check_v6 = _B_FALSE;
11377c478bd9Sstevel@tonic-gate 			for (aip = ai; aip != NULL; aip = aip->ai_next) {
11387c478bd9Sstevel@tonic-gate 				if (aip->ai_family == AF_INET6) {
11397c478bd9Sstevel@tonic-gate 					/* LINTED E_BAD_PTR_CAST_ALIGN */
11407c478bd9Sstevel@tonic-gate 					bcopy(&((struct sockaddr_in6 *)
11417c478bd9Sstevel@tonic-gate 					    aip->ai_addr)->sin6_addr,
11427c478bd9Sstevel@tonic-gate 					    &gwIPlist6[i].addr6,
11437c478bd9Sstevel@tonic-gate 					    aip->ai_addrlen);
11447c478bd9Sstevel@tonic-gate 					(*resolved6)++;
11457d6fbbeaSblu 					check_v6 = _B_TRUE;
11467c478bd9Sstevel@tonic-gate 					break;
11477c478bd9Sstevel@tonic-gate 				}
11487c478bd9Sstevel@tonic-gate 			}
11497c478bd9Sstevel@tonic-gate 		} else if (check_v6) {
11507c478bd9Sstevel@tonic-gate 			check_v6 = _B_FALSE;
11517c478bd9Sstevel@tonic-gate 		}
11527c478bd9Sstevel@tonic-gate 	}
11537c478bd9Sstevel@tonic-gate 	freeaddrinfo(ai);
11547c478bd9Sstevel@tonic-gate }
11557c478bd9Sstevel@tonic-gate 
11567c478bd9Sstevel@tonic-gate /*
11577c478bd9Sstevel@tonic-gate  * set protocol specific values here
11587c478bd9Sstevel@tonic-gate  */
11597c478bd9Sstevel@tonic-gate static void
11607c478bd9Sstevel@tonic-gate setup_protocol(struct pr_set *pr, int family)
11617c478bd9Sstevel@tonic-gate {
11627c478bd9Sstevel@tonic-gate 	/*
11637c478bd9Sstevel@tonic-gate 	 * Set the global variables for each AF. This is going to save us lots
11647c478bd9Sstevel@tonic-gate 	 * of "if (family == AF_INET)... else .."
11657c478bd9Sstevel@tonic-gate 	 */
11667c478bd9Sstevel@tonic-gate 	pr->family = family;
11677c478bd9Sstevel@tonic-gate 
11687c478bd9Sstevel@tonic-gate 	if (family == AF_INET) {
11697c478bd9Sstevel@tonic-gate 		if (!docksum) {
11707c478bd9Sstevel@tonic-gate 			Fprintf(stderr,
11717c478bd9Sstevel@tonic-gate 			    "%s: Warning: checksums disabled\n", prog);
11727c478bd9Sstevel@tonic-gate 		}
11737c478bd9Sstevel@tonic-gate 		(void) strcpy(pr->name, "IPv4");
11747c478bd9Sstevel@tonic-gate 		(void) strcpy(pr->icmp, "icmp");
11757c478bd9Sstevel@tonic-gate 		pr->icmp_minlen = ICMP_MINLEN;
11767c478bd9Sstevel@tonic-gate 		pr->addr_len = sizeof (struct in_addr);
11777c478bd9Sstevel@tonic-gate 		pr->ip_hdr_len = sizeof (struct ip);
11787c478bd9Sstevel@tonic-gate 		pr->sock_size = sizeof (struct sockaddr_in);
11797c478bd9Sstevel@tonic-gate 		pr->to = (struct sockaddr *)&whereto;
11807c478bd9Sstevel@tonic-gate 		pr->from = (struct sockaddr *)&wherefrom;
11817c478bd9Sstevel@tonic-gate 		pr->from_sin_addr = (void *)&wherefrom.sin_addr;
11827c478bd9Sstevel@tonic-gate 		pr->gwIPlist = gwIPlist;
11837c478bd9Sstevel@tonic-gate 		pr->set_buffers_fn = set_buffers;
11847c478bd9Sstevel@tonic-gate 		pr->check_reply_fn = check_reply;
11857c478bd9Sstevel@tonic-gate 		pr->print_icmp_other_fn = print_icmp_other;
11867c478bd9Sstevel@tonic-gate 		pr->print_addr_fn = print_addr;
11877c478bd9Sstevel@tonic-gate 		pr->packlen = calc_packetlen(packlen_input, pr);
11887c478bd9Sstevel@tonic-gate 	} else {
11897c478bd9Sstevel@tonic-gate 		(void) strcpy(pr->name, "IPv6");
11907c478bd9Sstevel@tonic-gate 		(void) strcpy(pr->icmp, "ipv6-icmp");
11917c478bd9Sstevel@tonic-gate 		pr->icmp_minlen = ICMP6_MINLEN;
11927c478bd9Sstevel@tonic-gate 		pr->addr_len = sizeof (struct in6_addr);
11937c478bd9Sstevel@tonic-gate 		pr->ip_hdr_len = sizeof (struct ip6_hdr);
11947c478bd9Sstevel@tonic-gate 		pr->sock_size = sizeof (struct sockaddr_in6);
11957c478bd9Sstevel@tonic-gate 		pr->to = (struct sockaddr *)&whereto6;
11967c478bd9Sstevel@tonic-gate 		pr->from = (struct sockaddr *)&wherefrom6;
11977c478bd9Sstevel@tonic-gate 		pr->from_sin_addr = (void *)&wherefrom6.sin6_addr;
11987c478bd9Sstevel@tonic-gate 		pr->gwIPlist = gwIP6list;
11997c478bd9Sstevel@tonic-gate 		pr->set_buffers_fn = set_buffers6;
12007c478bd9Sstevel@tonic-gate 		pr->check_reply_fn = check_reply6;
12017c478bd9Sstevel@tonic-gate 		pr->print_icmp_other_fn = print_icmp_other6;
12027c478bd9Sstevel@tonic-gate 		pr->print_addr_fn = print_addr6;
12037c478bd9Sstevel@tonic-gate 		pr->packlen = calc_packetlen(packlen_input, pr);
12047c478bd9Sstevel@tonic-gate 	}
12057c478bd9Sstevel@tonic-gate 	if (pr->packlen == 0)
12067c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
12077c478bd9Sstevel@tonic-gate }
12087c478bd9Sstevel@tonic-gate 
12097c478bd9Sstevel@tonic-gate /*
12107c478bd9Sstevel@tonic-gate  * setup the sockets for the given protocol's address family
12117c478bd9Sstevel@tonic-gate  */
12127c478bd9Sstevel@tonic-gate static void
12137c478bd9Sstevel@tonic-gate setup_socket(struct pr_set *pr, int packet_len)
12147c478bd9Sstevel@tonic-gate {
12157c478bd9Sstevel@tonic-gate 	int on = 1;
12167c478bd9Sstevel@tonic-gate 	struct protoent *pe;
12177c478bd9Sstevel@tonic-gate 	int type;
12187c478bd9Sstevel@tonic-gate 	int proto;
12197c478bd9Sstevel@tonic-gate 	int int_op;
12207c478bd9Sstevel@tonic-gate 	int rsock;
12217c478bd9Sstevel@tonic-gate 	int ssock;
12227c478bd9Sstevel@tonic-gate 
12237c478bd9Sstevel@tonic-gate 	if ((pe = getprotobyname(pr->icmp)) == NULL) {
12247c478bd9Sstevel@tonic-gate 		Fprintf(stderr, "%s: unknown protocol %s\n", prog, pr->icmp);
12257c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
12267c478bd9Sstevel@tonic-gate 	}
12277c478bd9Sstevel@tonic-gate 
12287c478bd9Sstevel@tonic-gate 	/* privilege bracketing */
12297c478bd9Sstevel@tonic-gate 	(void) __priv_bracket(PRIV_ON);
12307c478bd9Sstevel@tonic-gate 
12317c478bd9Sstevel@tonic-gate 	if ((rsock = socket(pr->family, SOCK_RAW, pe->p_proto)) < 0) {
12327c478bd9Sstevel@tonic-gate 		Fprintf(stderr, "%s: icmp socket: %s\n", prog, strerror(errno));
12337c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
12347c478bd9Sstevel@tonic-gate 	}
12357c478bd9Sstevel@tonic-gate 
12367c478bd9Sstevel@tonic-gate 	if (options & SO_DEBUG) {
12377c478bd9Sstevel@tonic-gate 		if (setsockopt(rsock, SOL_SOCKET, SO_DEBUG, (char *)&on,
12387c478bd9Sstevel@tonic-gate 		    sizeof (on)) < 0) {
12397c478bd9Sstevel@tonic-gate 			Fprintf(stderr, "%s: SO_DEBUG: %s\n", prog,
12407c478bd9Sstevel@tonic-gate 			    strerror(errno));
12417c478bd9Sstevel@tonic-gate 			exit(EXIT_FAILURE);
12427c478bd9Sstevel@tonic-gate 		}
12437c478bd9Sstevel@tonic-gate 	}
12447c478bd9Sstevel@tonic-gate 	if (options & SO_DONTROUTE) {
12457c478bd9Sstevel@tonic-gate 		if (setsockopt(rsock, SOL_SOCKET, SO_DONTROUTE, (char *)&on,
12467c478bd9Sstevel@tonic-gate 		    sizeof (on)) < 0) {
12477c478bd9Sstevel@tonic-gate 			Fprintf(stderr, "%s: SO_DONTROUTE: %s\n", prog,
12487c478bd9Sstevel@tonic-gate 			    strerror(errno));
12497c478bd9Sstevel@tonic-gate 			exit(EXIT_FAILURE);
12507c478bd9Sstevel@tonic-gate 		}
12517c478bd9Sstevel@tonic-gate 	}
12527c478bd9Sstevel@tonic-gate 
12537c478bd9Sstevel@tonic-gate 	if (pr->family == AF_INET6) {
12547c478bd9Sstevel@tonic-gate 		/* Enable receipt of destination address info */
12557c478bd9Sstevel@tonic-gate 		if (setsockopt(rsock, IPPROTO_IPV6, IPV6_RECVPKTINFO,
12567c478bd9Sstevel@tonic-gate 		    (char *)&on, sizeof (on)) < 0) {
12577c478bd9Sstevel@tonic-gate 			Fprintf(stderr, "%s: IPV6_RECVPKTINFO: %s\n", prog,
12587c478bd9Sstevel@tonic-gate 			    strerror(errno));
12597c478bd9Sstevel@tonic-gate 			exit(EXIT_FAILURE);
12607c478bd9Sstevel@tonic-gate 		}
12617c478bd9Sstevel@tonic-gate 		/* Enable receipt of hoplimit info */
12627c478bd9Sstevel@tonic-gate 		if (setsockopt(rsock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT,
12637c478bd9Sstevel@tonic-gate 		    (char *)&on, sizeof (on)) < 0) {
12647c478bd9Sstevel@tonic-gate 			Fprintf(stderr, "%s: IPV6_RECVHOPLIMIT: %s\n", prog,
12657c478bd9Sstevel@tonic-gate 			    strerror(errno));
12667c478bd9Sstevel@tonic-gate 			exit(EXIT_FAILURE);
12677c478bd9Sstevel@tonic-gate 		}
12687c478bd9Sstevel@tonic-gate 
12697c478bd9Sstevel@tonic-gate 	}
12707c478bd9Sstevel@tonic-gate 
12717c478bd9Sstevel@tonic-gate 	/*
12727c478bd9Sstevel@tonic-gate 	 * Initialize the socket type and protocol based on the address
12737c478bd9Sstevel@tonic-gate 	 * family, whether or not a raw IP socket is required (for IPv4)
12747c478bd9Sstevel@tonic-gate 	 * or whether ICMP will be used instead of UDP.
12757c478bd9Sstevel@tonic-gate 	 *
12767c478bd9Sstevel@tonic-gate 	 * For historical reasons, the datagrams sent out by
12777c478bd9Sstevel@tonic-gate 	 * traceroute(1M) do not have the "don't fragment" flag set.  For
12787c478bd9Sstevel@tonic-gate 	 * this reason as well as the ability to set the Loose Source and
12797c478bd9Sstevel@tonic-gate 	 * Record Route (LSRR) option, a raw IP socket will be used for
12807c478bd9Sstevel@tonic-gate 	 * IPv4 when run in the global zone.  Otherwise, the actual
12817c478bd9Sstevel@tonic-gate 	 * datagram that will be sent will be a regular UDP or ICMP echo
12827c478bd9Sstevel@tonic-gate 	 * request packet.  However for convenience and for future options
12837c478bd9Sstevel@tonic-gate 	 * when other IP header information may be specified using
12847c478bd9Sstevel@tonic-gate 	 * traceroute, the buffer including the raw IP and UDP or ICMP
12857c478bd9Sstevel@tonic-gate 	 * header is always filled in.  When the probe is actually sent,
12867c478bd9Sstevel@tonic-gate 	 * the size of the request and the start of the packet is set
12877c478bd9Sstevel@tonic-gate 	 * according to the type of datagram to send.
12887c478bd9Sstevel@tonic-gate 	 */
12897c478bd9Sstevel@tonic-gate 	if (pr->family == AF_INET && raw_req) {
12907c478bd9Sstevel@tonic-gate 		type = SOCK_RAW;
12917c478bd9Sstevel@tonic-gate 		proto = IPPROTO_RAW;
12927c478bd9Sstevel@tonic-gate 	} else if (useicmp) {
12937c478bd9Sstevel@tonic-gate 		type = SOCK_RAW;
12947c478bd9Sstevel@tonic-gate 		if (pr->family == AF_INET)
12957c478bd9Sstevel@tonic-gate 			proto = IPPROTO_ICMP;
12967c478bd9Sstevel@tonic-gate 		else
12977c478bd9Sstevel@tonic-gate 			proto = IPPROTO_ICMPV6;
12987c478bd9Sstevel@tonic-gate 	} else {
12997c478bd9Sstevel@tonic-gate 		type = SOCK_DGRAM;
13007c478bd9Sstevel@tonic-gate 		proto = IPPROTO_UDP;
13017c478bd9Sstevel@tonic-gate 	}
13027c478bd9Sstevel@tonic-gate 	ssock = socket(pr->family, type, proto);
13037c478bd9Sstevel@tonic-gate 
13047c478bd9Sstevel@tonic-gate 	if (ssock < 0) {
13057c478bd9Sstevel@tonic-gate 		if (proto == IPPROTO_RAW) {
13067c478bd9Sstevel@tonic-gate 			Fprintf(stderr, "%s: raw socket: %s\n", prog,
13077c478bd9Sstevel@tonic-gate 			    strerror(errno));
13087c478bd9Sstevel@tonic-gate 		} else if (proto == IPPROTO_UDP) {
13097c478bd9Sstevel@tonic-gate 			Fprintf(stderr, "%s: udp socket: %s\n", prog,
13107c478bd9Sstevel@tonic-gate 			    strerror(errno));
13117c478bd9Sstevel@tonic-gate 		} else {
13127c478bd9Sstevel@tonic-gate 			Fprintf(stderr, "%s: icmp socket: %s\n", prog,
13137c478bd9Sstevel@tonic-gate 			    strerror(errno));
13147c478bd9Sstevel@tonic-gate 		}
13157c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
13167c478bd9Sstevel@tonic-gate 	}
13177c478bd9Sstevel@tonic-gate 
13187c478bd9Sstevel@tonic-gate 	if (setsockopt(ssock, SOL_SOCKET, SO_SNDBUF, (char *)&packet_len,
13197c478bd9Sstevel@tonic-gate 	    sizeof (packet_len)) < 0) {
13207c478bd9Sstevel@tonic-gate 		Fprintf(stderr, "%s: SO_SNDBUF: %s\n", prog, strerror(errno));
13217c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
13227c478bd9Sstevel@tonic-gate 	}
13237c478bd9Sstevel@tonic-gate 
13247c478bd9Sstevel@tonic-gate 	if (pr->family == AF_INET && raw_req) {
13257c478bd9Sstevel@tonic-gate 		if (setsockopt(ssock, IPPROTO_IP, IP_HDRINCL, (char *)&on,
13267c478bd9Sstevel@tonic-gate 		    sizeof (on)) < 0) {
13277c478bd9Sstevel@tonic-gate 			Fprintf(stderr, "%s: IP_HDRINCL: %s\n", prog,
13287c478bd9Sstevel@tonic-gate 			    strerror(errno));
13297c478bd9Sstevel@tonic-gate 			exit(EXIT_FAILURE);
13307c478bd9Sstevel@tonic-gate 		}
13317c478bd9Sstevel@tonic-gate 	}
13327c478bd9Sstevel@tonic-gate 
13337c478bd9Sstevel@tonic-gate 	if (options & SO_DEBUG) {
13347c478bd9Sstevel@tonic-gate 		if (setsockopt(ssock, SOL_SOCKET, SO_DEBUG, (char *)&on,
13357c478bd9Sstevel@tonic-gate 		    sizeof (on)) < 0) {
13367c478bd9Sstevel@tonic-gate 			Fprintf(stderr, "%s: SO_DEBUG: %s\n", prog,
13377c478bd9Sstevel@tonic-gate 			    strerror(errno));
13387c478bd9Sstevel@tonic-gate 			exit(EXIT_FAILURE);
13397c478bd9Sstevel@tonic-gate 		}
13407c478bd9Sstevel@tonic-gate 	}
13417c478bd9Sstevel@tonic-gate 	if (options & SO_DONTROUTE) {
13427c478bd9Sstevel@tonic-gate 		if (setsockopt(ssock, SOL_SOCKET, SO_DONTROUTE,
13437c478bd9Sstevel@tonic-gate 		    (char *)&on, sizeof (on)) < 0) {
13447c478bd9Sstevel@tonic-gate 			Fprintf(stderr, "%s: SO_DONTROUTE: %s\n", prog,
13457c478bd9Sstevel@tonic-gate 			    strerror(errno));
13467c478bd9Sstevel@tonic-gate 			exit(EXIT_FAILURE);
13477c478bd9Sstevel@tonic-gate 		}
13487c478bd9Sstevel@tonic-gate 	}
13497c478bd9Sstevel@tonic-gate 
13507c478bd9Sstevel@tonic-gate 	/*
13517c478bd9Sstevel@tonic-gate 	 * If a raw IPv4 packet is going to be sent, the Type of Service
13527c478bd9Sstevel@tonic-gate 	 * field in the packet will be initialized in set_buffers().
13537c478bd9Sstevel@tonic-gate 	 * Otherwise, it is initialized here using the IPPROTO_IP level
13547c478bd9Sstevel@tonic-gate 	 * socket option.
13557c478bd9Sstevel@tonic-gate 	 */
13567c478bd9Sstevel@tonic-gate 	if (settos && !raw_req) {
13577c478bd9Sstevel@tonic-gate 		int_op = tos;
13587c478bd9Sstevel@tonic-gate 		if (setsockopt(ssock, IPPROTO_IP, IP_TOS, (char *)&int_op,
13597c478bd9Sstevel@tonic-gate 		    sizeof (int_op)) < 0) {
13607c478bd9Sstevel@tonic-gate 			Fprintf(stderr, "%s: IP_TOS: %s\n", prog,
13617c478bd9Sstevel@tonic-gate 			    strerror(errno));
13627c478bd9Sstevel@tonic-gate 			exit(EXIT_FAILURE);
13637c478bd9Sstevel@tonic-gate 		}
13647c478bd9Sstevel@tonic-gate 	}
13657c478bd9Sstevel@tonic-gate 	if (pr->family == AF_INET) {
13667c478bd9Sstevel@tonic-gate 		rcvsock4 = rsock;
13677c478bd9Sstevel@tonic-gate 		sndsock4 = ssock;
13687c478bd9Sstevel@tonic-gate 	} else {
13697c478bd9Sstevel@tonic-gate 		rcvsock6 = rsock;
13707c478bd9Sstevel@tonic-gate 		sndsock6 = ssock;
13717c478bd9Sstevel@tonic-gate 	}
13727c478bd9Sstevel@tonic-gate 	/* Revert to non-privileged user after configuring sockets */
13737c478bd9Sstevel@tonic-gate 	(void) __priv_bracket(PRIV_OFF);
13747c478bd9Sstevel@tonic-gate }
13757c478bd9Sstevel@tonic-gate 
13767c478bd9Sstevel@tonic-gate /*
13777c478bd9Sstevel@tonic-gate  * If we are "probing all", this function calls traceroute() for each IP address
13787c478bd9Sstevel@tonic-gate  * of the target, otherwise calls only once. Returns _B_FALSE if traceroute()
13797c478bd9Sstevel@tonic-gate  * fails.
13807c478bd9Sstevel@tonic-gate  */
13817c478bd9Sstevel@tonic-gate static void
13827c478bd9Sstevel@tonic-gate trace_it(struct addrinfo *ai_dst)
13837c478bd9Sstevel@tonic-gate {
13847c478bd9Sstevel@tonic-gate 	struct msghdr msg6;
13857c478bd9Sstevel@tonic-gate 	int num_dst_IPaddrs;
13867c478bd9Sstevel@tonic-gate 	struct addrinfo *aip;
13877c478bd9Sstevel@tonic-gate 	int i;
13887c478bd9Sstevel@tonic-gate 
13897c478bd9Sstevel@tonic-gate 	if (!probe_all)
13907c478bd9Sstevel@tonic-gate 		num_dst_IPaddrs = 1;
13917c478bd9Sstevel@tonic-gate 	else
13927c478bd9Sstevel@tonic-gate 		num_dst_IPaddrs = num_v4 + num_v6;
13937c478bd9Sstevel@tonic-gate 
13947c478bd9Sstevel@tonic-gate 	/*
13957c478bd9Sstevel@tonic-gate 	 * Initialize the msg6 structure using the hoplimit for the first
13967c478bd9Sstevel@tonic-gate 	 * probe packet, gateway addresses and the outgoing interface index.
13977c478bd9Sstevel@tonic-gate 	 */
13987c478bd9Sstevel@tonic-gate 	if (ai_dst->ai_family == AF_INET6 || (probe_all && num_v6)) {
13997c478bd9Sstevel@tonic-gate 		msg6.msg_control = NULL;
14007c478bd9Sstevel@tonic-gate 		msg6.msg_controllen = 0;
14017c478bd9Sstevel@tonic-gate 		set_ancillary_data(&msg6, first_ttl, pr6->gwIPlist, gw_count,
14027c478bd9Sstevel@tonic-gate 		    if_index);
14037c478bd9Sstevel@tonic-gate 	}
14047c478bd9Sstevel@tonic-gate 
14057c478bd9Sstevel@tonic-gate 	/* run traceroute for all the IP addresses of the multihomed dest */
14067c478bd9Sstevel@tonic-gate 	for (aip = ai_dst, i = 0; i < num_dst_IPaddrs && aip != NULL; i++) {
14077c478bd9Sstevel@tonic-gate 		union any_in_addr *addrp;
14087c478bd9Sstevel@tonic-gate 		if (aip->ai_family == AF_INET) {
14097c478bd9Sstevel@tonic-gate 			addrp = (union any_in_addr *)
14107c478bd9Sstevel@tonic-gate 			    /* LINTED E_BAD_PTR_CAST_ALIGN */
14117c478bd9Sstevel@tonic-gate 			    &((struct sockaddr_in *)
14127c478bd9Sstevel@tonic-gate 			    aip->ai_addr)->sin_addr;
14137c478bd9Sstevel@tonic-gate 			set_sin((struct sockaddr *)pr4->to, addrp,
14147c478bd9Sstevel@tonic-gate 			    aip->ai_family);
14157c478bd9Sstevel@tonic-gate 			traceroute(addrp, &msg6, pr4, num_ifs4, al4);
14167c478bd9Sstevel@tonic-gate 		} else {
14177c478bd9Sstevel@tonic-gate 			addrp = (union any_in_addr *)
14187c478bd9Sstevel@tonic-gate 			    /* LINTED E_BAD_PTR_CAST_ALIGN */
14197c478bd9Sstevel@tonic-gate 			    &((struct sockaddr_in6 *)
14207c478bd9Sstevel@tonic-gate 			    aip->ai_addr)->sin6_addr;
14217c478bd9Sstevel@tonic-gate 			set_sin((struct sockaddr *)pr6->to, addrp,
14227c478bd9Sstevel@tonic-gate 			    aip->ai_family);
14237c478bd9Sstevel@tonic-gate 			traceroute(addrp, &msg6, pr6, num_ifs6, al6);
14247c478bd9Sstevel@tonic-gate 		}
14257c478bd9Sstevel@tonic-gate 		aip = aip->ai_next;
14267c478bd9Sstevel@tonic-gate 		if (i < (num_dst_IPaddrs - 1))
14277c478bd9Sstevel@tonic-gate 			(void) putchar('\n');
14287c478bd9Sstevel@tonic-gate 	}
14297c478bd9Sstevel@tonic-gate }
14307c478bd9Sstevel@tonic-gate 
14317c478bd9Sstevel@tonic-gate /*
14327c478bd9Sstevel@tonic-gate  * set the IP address in a sockaddr struct
14337c478bd9Sstevel@tonic-gate  */
14347c478bd9Sstevel@tonic-gate static void
14357c478bd9Sstevel@tonic-gate set_sin(struct sockaddr *sock, union any_in_addr *addr, int family)
14367c478bd9Sstevel@tonic-gate {
14377c478bd9Sstevel@tonic-gate 	sock->sa_family = family;
14387c478bd9Sstevel@tonic-gate 
14397c478bd9Sstevel@tonic-gate 	if (family == AF_INET)
14407c478bd9Sstevel@tonic-gate 		/* LINTED E_BAD_PTR_CAST_ALIGN */
14417c478bd9Sstevel@tonic-gate 		((struct sockaddr_in *)sock)->sin_addr = addr->addr;
14427c478bd9Sstevel@tonic-gate 	else
14437c478bd9Sstevel@tonic-gate 		/* LINTED E_BAD_PTR_CAST_ALIGN */
14447c478bd9Sstevel@tonic-gate 		((struct sockaddr_in6 *)sock)->sin6_addr = addr->addr6;
14457c478bd9Sstevel@tonic-gate }
14467c478bd9Sstevel@tonic-gate 
14477c478bd9Sstevel@tonic-gate /*
14487c478bd9Sstevel@tonic-gate  * returns the IF name on which the given IP address is configured
14497c478bd9Sstevel@tonic-gate  */
14507c478bd9Sstevel@tonic-gate static char *
14517c478bd9Sstevel@tonic-gate device_name(struct ifaddrlist *al, int len, union any_in_addr *ip_addr,
14527c478bd9Sstevel@tonic-gate     struct pr_set *pr)
14537c478bd9Sstevel@tonic-gate {
14547c478bd9Sstevel@tonic-gate 	int i;
14557c478bd9Sstevel@tonic-gate 	struct ifaddrlist *tmp_al;
14567c478bd9Sstevel@tonic-gate 
14577c478bd9Sstevel@tonic-gate 	tmp_al = al;
14587c478bd9Sstevel@tonic-gate 
14597c478bd9Sstevel@tonic-gate 	for (i = 0; i < len; i++, tmp_al++) {
14607c478bd9Sstevel@tonic-gate 		if (memcmp(&tmp_al->addr, ip_addr, pr->addr_len) == 0) {
14617c478bd9Sstevel@tonic-gate 			return (tmp_al->device);
14627c478bd9Sstevel@tonic-gate 		}
14637c478bd9Sstevel@tonic-gate 	}
14647c478bd9Sstevel@tonic-gate 
14657c478bd9Sstevel@tonic-gate 	return (NULL);
14667c478bd9Sstevel@tonic-gate }
14677c478bd9Sstevel@tonic-gate 
14687c478bd9Sstevel@tonic-gate /*
14697c478bd9Sstevel@tonic-gate  * Trace the route to the host with given IP address.
14707c478bd9Sstevel@tonic-gate  */
14717c478bd9Sstevel@tonic-gate static void
14727c478bd9Sstevel@tonic-gate traceroute(union any_in_addr *ip_addr, struct msghdr *msg6, struct pr_set *pr,
14737c478bd9Sstevel@tonic-gate     int num_ifs, struct ifaddrlist *al)
14747c478bd9Sstevel@tonic-gate {
14757c478bd9Sstevel@tonic-gate 	int ttl;
14767c478bd9Sstevel@tonic-gate 	int probe;
14777c478bd9Sstevel@tonic-gate 	uchar_t type;				/* icmp type */
14787c478bd9Sstevel@tonic-gate 	uchar_t code;				/* icmp code */
14797c478bd9Sstevel@tonic-gate 	int reply;
14807c478bd9Sstevel@tonic-gate 	int seq = 0;
14817c478bd9Sstevel@tonic-gate 	char temp_buf[INET6_ADDRSTRLEN];	/* use for inet_ntop() */
14827c478bd9Sstevel@tonic-gate 	int longjmp_return;			/* return value from longjump */
14837c478bd9Sstevel@tonic-gate 	struct ip *ip = (struct ip *)packet;
14847c478bd9Sstevel@tonic-gate 	boolean_t got_there = _B_FALSE;		/* we hit the destination */
14857c478bd9Sstevel@tonic-gate 	static boolean_t first_pkt = _B_TRUE;
14867c478bd9Sstevel@tonic-gate 	int hoplimit;				/* hoplimit for IPv6 packets */
14877c478bd9Sstevel@tonic-gate 	struct in6_addr addr6;
14887c478bd9Sstevel@tonic-gate 	int num_src_ifs;			/* excludes down and loopback */
14897c478bd9Sstevel@tonic-gate 	struct msghdr in_msg;
14907c478bd9Sstevel@tonic-gate 	struct iovec iov;
14917c478bd9Sstevel@tonic-gate 	int *intp;
14927c478bd9Sstevel@tonic-gate 	int sndsock;
14937c478bd9Sstevel@tonic-gate 	int rcvsock;
14947c478bd9Sstevel@tonic-gate 
14957c478bd9Sstevel@tonic-gate 	msg6->msg_name = pr->to;
14967c478bd9Sstevel@tonic-gate 	msg6->msg_namelen = sizeof (struct sockaddr_in6);
14977c478bd9Sstevel@tonic-gate 	sndsock =  (pr->family == AF_INET) ? sndsock4 : sndsock6;
14987c478bd9Sstevel@tonic-gate 	rcvsock =  (pr->family == AF_INET) ? rcvsock4 : rcvsock6;
14997c478bd9Sstevel@tonic-gate 
15007c478bd9Sstevel@tonic-gate 	/* carry out the source address selection */
15017c478bd9Sstevel@tonic-gate 	if (pick_src) {
15027c478bd9Sstevel@tonic-gate 		union any_in_addr src_addr;
15037c478bd9Sstevel@tonic-gate 		char *dev_name;
15047c478bd9Sstevel@tonic-gate 		int i;
15057c478bd9Sstevel@tonic-gate 
15067c478bd9Sstevel@tonic-gate 		/*
15077c478bd9Sstevel@tonic-gate 		 * If there's a gateway, a routing header as a consequence, our
15087c478bd9Sstevel@tonic-gate 		 * kernel picks the source address based on the first hop
15097c478bd9Sstevel@tonic-gate 		 * address, rather than final destination address.
15107c478bd9Sstevel@tonic-gate 		 */
15117c478bd9Sstevel@tonic-gate 		if (gw_count > 0) {
15127c478bd9Sstevel@tonic-gate 			(void) select_src_addr(pr->gwIPlist, &src_addr,
15137c478bd9Sstevel@tonic-gate 			    pr->family);
15147c478bd9Sstevel@tonic-gate 		} else {
15157c478bd9Sstevel@tonic-gate 			(void) select_src_addr(ip_addr, &src_addr, pr->family);
15167c478bd9Sstevel@tonic-gate 		}
15177c478bd9Sstevel@tonic-gate 		set_sin(pr->from, &src_addr, pr->family);
15187c478bd9Sstevel@tonic-gate 
15197c478bd9Sstevel@tonic-gate 		/* filter out down and loopback interfaces */
15207c478bd9Sstevel@tonic-gate 		num_src_ifs = 0;
15217c478bd9Sstevel@tonic-gate 		for (i = 0; i < num_ifs; i++) {
15227c478bd9Sstevel@tonic-gate 			if (!(al[i].flags & IFF_LOOPBACK) &&
15237c478bd9Sstevel@tonic-gate 			    (al[i].flags & IFF_UP))
15247c478bd9Sstevel@tonic-gate 				num_src_ifs++;
15257c478bd9Sstevel@tonic-gate 		}
15267c478bd9Sstevel@tonic-gate 
15277c478bd9Sstevel@tonic-gate 		if (num_src_ifs > 1) {
15287c478bd9Sstevel@tonic-gate 			dev_name = device_name(al, num_ifs, &src_addr, pr);
15297c478bd9Sstevel@tonic-gate 			if (dev_name == NULL)
15307c478bd9Sstevel@tonic-gate 				dev_name = "?";
15317c478bd9Sstevel@tonic-gate 
15327c478bd9Sstevel@tonic-gate 			Fprintf(stderr,
15337c478bd9Sstevel@tonic-gate 			    "%s: Warning: Multiple interfaces found;"
15347c478bd9Sstevel@tonic-gate 			    " using %s @ %s\n",
15357c478bd9Sstevel@tonic-gate 			    prog, inet_ntop(pr->family,
15367c478bd9Sstevel@tonic-gate 				(const void *)pr->from_sin_addr,
15377c478bd9Sstevel@tonic-gate 				temp_buf, sizeof (temp_buf)),
15387c478bd9Sstevel@tonic-gate 			    dev_name);
15397c478bd9Sstevel@tonic-gate 		}
15407c478bd9Sstevel@tonic-gate 	}
15417c478bd9Sstevel@tonic-gate 
15427c478bd9Sstevel@tonic-gate 	if (pr->family == AF_INET) {
15437c478bd9Sstevel@tonic-gate 		outip4->ip_src = *(struct in_addr *)pr->from_sin_addr;
15447c478bd9Sstevel@tonic-gate 		outip4->ip_dst = ip_addr->addr;
15457c478bd9Sstevel@tonic-gate 	}
15467c478bd9Sstevel@tonic-gate 
15477c478bd9Sstevel@tonic-gate 	/*
15487c478bd9Sstevel@tonic-gate 	 * If the hostname is an IPv6 literal address, let's not print it twice.
15497c478bd9Sstevel@tonic-gate 	 */
15507c478bd9Sstevel@tonic-gate 	if (pr->family == AF_INET6 &&
15517c478bd9Sstevel@tonic-gate 	    inet_pton(AF_INET6, hostname, &addr6) > 0) {
15527c478bd9Sstevel@tonic-gate 		Fprintf(stderr, "%s to %s", prog, hostname);
15537c478bd9Sstevel@tonic-gate 	} else {
15547c478bd9Sstevel@tonic-gate 		Fprintf(stderr, "%s to %s (%s)", prog, hostname,
15557c478bd9Sstevel@tonic-gate 		    inet_ntop(pr->family, (const void *)ip_addr, temp_buf,
15567c478bd9Sstevel@tonic-gate 			sizeof (temp_buf)));
15577c478bd9Sstevel@tonic-gate 	}
15587c478bd9Sstevel@tonic-gate 
15597c478bd9Sstevel@tonic-gate 	if (source)
15607c478bd9Sstevel@tonic-gate 		Fprintf(stderr, " from %s", source);
15617c478bd9Sstevel@tonic-gate 	Fprintf(stderr, ", %d hops max, %d byte packets\n", max_ttl,
15627c478bd9Sstevel@tonic-gate 	    pr->packlen);
15637c478bd9Sstevel@tonic-gate 	(void) fflush(stderr);
15647c478bd9Sstevel@tonic-gate 
15657c478bd9Sstevel@tonic-gate 	/*
15667c478bd9Sstevel@tonic-gate 	 * Setup the source routing for IPv4. For IPv6, we did the required
15677c478bd9Sstevel@tonic-gate 	 * setup in the caller function, trace_it(), because it's independent
15687c478bd9Sstevel@tonic-gate 	 * from the IP address of target.
15697c478bd9Sstevel@tonic-gate 	 */
15707c478bd9Sstevel@tonic-gate 	if (pr->family == AF_INET && gw_count > 0)
15717c478bd9Sstevel@tonic-gate 		set_IPv4opt_sourcerouting(sndsock, ip_addr, pr->gwIPlist);
15727c478bd9Sstevel@tonic-gate 
15737c478bd9Sstevel@tonic-gate 	if (probe_all) {
15747c478bd9Sstevel@tonic-gate 		/* interrupt handler sig_handler() jumps back to here */
15757c478bd9Sstevel@tonic-gate 		if ((longjmp_return = setjmp(env)) != 0) {
15767c478bd9Sstevel@tonic-gate 			switch (longjmp_return) {
15777c478bd9Sstevel@tonic-gate 			case SIGINT:
15787c478bd9Sstevel@tonic-gate 				Printf("(skipping)\n");
15797c478bd9Sstevel@tonic-gate 				return;
15807c478bd9Sstevel@tonic-gate 			case SIGQUIT:
15817c478bd9Sstevel@tonic-gate 				Printf("(exiting)\n");
15827c478bd9Sstevel@tonic-gate 				exit(EXIT_SUCCESS);
15837c478bd9Sstevel@tonic-gate 			default:	/* should never happen */
15847c478bd9Sstevel@tonic-gate 				exit(EXIT_FAILURE);
15857c478bd9Sstevel@tonic-gate 			}
15867c478bd9Sstevel@tonic-gate 		}
15877c478bd9Sstevel@tonic-gate 		(void) signal(SIGINT, sig_handler);
15887c478bd9Sstevel@tonic-gate 	}
15897c478bd9Sstevel@tonic-gate 
15907c478bd9Sstevel@tonic-gate 	for (ttl = first_ttl; ttl <= max_ttl; ++ttl) {
15917c478bd9Sstevel@tonic-gate 		union any_in_addr lastaddr;
15927c478bd9Sstevel@tonic-gate 		int timeouts = 0;
15937c478bd9Sstevel@tonic-gate 		double rtt;		/* for statistics */
15947c478bd9Sstevel@tonic-gate 		int nreceived = 0;
15957c478bd9Sstevel@tonic-gate 		double rttmin, rttmax;
15967c478bd9Sstevel@tonic-gate 		double rttsum, rttssq;
15977c478bd9Sstevel@tonic-gate 		int unreachable;
15987c478bd9Sstevel@tonic-gate 
15997c478bd9Sstevel@tonic-gate 		got_there = _B_FALSE;
16007c478bd9Sstevel@tonic-gate 		unreachable = 0;
16017c478bd9Sstevel@tonic-gate 
16027c478bd9Sstevel@tonic-gate 		/*
16037c478bd9Sstevel@tonic-gate 		 * The following line clears both IPv4 and IPv6 address stored
16047c478bd9Sstevel@tonic-gate 		 * in the union.
16057c478bd9Sstevel@tonic-gate 		 */
16067c478bd9Sstevel@tonic-gate 		lastaddr.addr6 = in6addr_any;
16077c478bd9Sstevel@tonic-gate 
16087c478bd9Sstevel@tonic-gate 		if ((ttl == (first_ttl + 1)) && (options & SO_DONTROUTE)) {
16097c478bd9Sstevel@tonic-gate 			Fprintf(stderr,
16107c478bd9Sstevel@tonic-gate 			    "%s: host %s is not on a directly-attached"
16117c478bd9Sstevel@tonic-gate 			    " network\n", prog, hostname);
16127c478bd9Sstevel@tonic-gate 			break;
16137c478bd9Sstevel@tonic-gate 		}
16147c478bd9Sstevel@tonic-gate 
16157c478bd9Sstevel@tonic-gate 		Printf("%2d ", ttl);
16167c478bd9Sstevel@tonic-gate 		(void) fflush(stdout);
16177c478bd9Sstevel@tonic-gate 
16187c478bd9Sstevel@tonic-gate 		for (probe = 0; (probe < nprobes) && (timeouts < max_timeout);
16197c478bd9Sstevel@tonic-gate 		    ++probe) {
16207c478bd9Sstevel@tonic-gate 			int cc;
16217c478bd9Sstevel@tonic-gate 			struct timeval t1, t2;
16227c478bd9Sstevel@tonic-gate 
16237c478bd9Sstevel@tonic-gate 			/*
16247c478bd9Sstevel@tonic-gate 			 * Put a delay before sending this probe packet. Don't
16257c478bd9Sstevel@tonic-gate 			 * delay it if it's the very first packet.
16267c478bd9Sstevel@tonic-gate 			 */
16277c478bd9Sstevel@tonic-gate 			if (!first_pkt) {
16287c478bd9Sstevel@tonic-gate 				if (delay.tv_sec > 0)
16297c478bd9Sstevel@tonic-gate 					(void) sleep((uint_t)delay.tv_sec);
16307c478bd9Sstevel@tonic-gate 				if (delay.tv_usec > 0)
16317c478bd9Sstevel@tonic-gate 					(void) usleep(delay.tv_usec);
16327c478bd9Sstevel@tonic-gate 			} else {
16337c478bd9Sstevel@tonic-gate 				first_pkt = _B_FALSE;
16347c478bd9Sstevel@tonic-gate 			}
16357c478bd9Sstevel@tonic-gate 
16367c478bd9Sstevel@tonic-gate 			(void) gettimeofday(&t1, NULL);
16377c478bd9Sstevel@tonic-gate 
16387c478bd9Sstevel@tonic-gate 			if (pr->family == AF_INET) {
16397c478bd9Sstevel@tonic-gate 				send_probe(sndsock, pr->to, outip4, seq, ttl,
16407c478bd9Sstevel@tonic-gate 				    &t1, pr->packlen);
16417c478bd9Sstevel@tonic-gate 			} else {
16427c478bd9Sstevel@tonic-gate 				send_probe6(sndsock, msg6, outip6, seq, ttl,
16437c478bd9Sstevel@tonic-gate 				    &t1, pr->packlen);
16447c478bd9Sstevel@tonic-gate 			}
16457c478bd9Sstevel@tonic-gate 
16467c478bd9Sstevel@tonic-gate 			/* prepare msghdr for recvmsg() */
16477c478bd9Sstevel@tonic-gate 			in_msg.msg_name = pr->from;
16487c478bd9Sstevel@tonic-gate 			in_msg.msg_namelen = pr->sock_size;
16497c478bd9Sstevel@tonic-gate 
16507c478bd9Sstevel@tonic-gate 			iov.iov_base = (char *)packet;
16517c478bd9Sstevel@tonic-gate 			iov.iov_len = sizeof (packet);
16527c478bd9Sstevel@tonic-gate 
16537c478bd9Sstevel@tonic-gate 			in_msg.msg_iov = &iov;
16547c478bd9Sstevel@tonic-gate 			in_msg.msg_iovlen = 1;
16557c478bd9Sstevel@tonic-gate 
16567c478bd9Sstevel@tonic-gate 			in_msg.msg_control = ancillary_data;
16577c478bd9Sstevel@tonic-gate 			in_msg.msg_controllen = sizeof (ancillary_data);
16587c478bd9Sstevel@tonic-gate 
16597c478bd9Sstevel@tonic-gate 			while ((cc = wait_for_reply(rcvsock, &in_msg,
16607c478bd9Sstevel@tonic-gate 			    &t1)) != 0) {
16617c478bd9Sstevel@tonic-gate 				(void) gettimeofday(&t2, NULL);
16627c478bd9Sstevel@tonic-gate 
16637c478bd9Sstevel@tonic-gate 				reply = (*pr->check_reply_fn) (&in_msg, cc, seq,
16647c478bd9Sstevel@tonic-gate 				    &type, &code);
16657c478bd9Sstevel@tonic-gate 
16667c478bd9Sstevel@tonic-gate 				in_msg.msg_controllen =
16677c478bd9Sstevel@tonic-gate 				    sizeof (ancillary_data);
16687c478bd9Sstevel@tonic-gate 				/* Skip short packet */
16697c478bd9Sstevel@tonic-gate 				if (reply == REPLY_SHORT_PKT) {
16707c478bd9Sstevel@tonic-gate 					continue;
16717c478bd9Sstevel@tonic-gate 				}
16727c478bd9Sstevel@tonic-gate 
16737c478bd9Sstevel@tonic-gate 				timeouts = 0;
16747c478bd9Sstevel@tonic-gate 
16757c478bd9Sstevel@tonic-gate 				/*
16767c478bd9Sstevel@tonic-gate 				 * if reply comes from a different host, print
16777c478bd9Sstevel@tonic-gate 				 * the hostname
16787c478bd9Sstevel@tonic-gate 				 */
16797c478bd9Sstevel@tonic-gate 				if (memcmp(pr->from_sin_addr, &lastaddr,
16807c478bd9Sstevel@tonic-gate 				    pr->addr_len) != 0) {
16817c478bd9Sstevel@tonic-gate 					(*pr->print_addr_fn) ((uchar_t *)packet,
16827c478bd9Sstevel@tonic-gate 					    cc, pr->from);
16837c478bd9Sstevel@tonic-gate 					/* store the address response */
16847c478bd9Sstevel@tonic-gate 					(void) memcpy(&lastaddr,
16857c478bd9Sstevel@tonic-gate 					    pr->from_sin_addr, pr->addr_len);
16867c478bd9Sstevel@tonic-gate 				}
16877c478bd9Sstevel@tonic-gate 
16887c478bd9Sstevel@tonic-gate 				rtt = deltaT(&t1, &t2);
16897c478bd9Sstevel@tonic-gate 				if (collect_stat) {
16907c478bd9Sstevel@tonic-gate 					record_stats(rtt, &nreceived, &rttmin,
16917c478bd9Sstevel@tonic-gate 					    &rttmax, &rttsum, &rttssq);
16927c478bd9Sstevel@tonic-gate 				} else {
16937c478bd9Sstevel@tonic-gate 					Printf("  %.3f ms", rtt);
16947c478bd9Sstevel@tonic-gate 				}
16957c478bd9Sstevel@tonic-gate 
16967c478bd9Sstevel@tonic-gate 				if (pr->family == AF_INET6) {
16977c478bd9Sstevel@tonic-gate 					intp =
16987c478bd9Sstevel@tonic-gate 					    (int *)find_ancillary_data(&in_msg,
16997c478bd9Sstevel@tonic-gate 						IPPROTO_IPV6, IPV6_HOPLIMIT);
17007c478bd9Sstevel@tonic-gate 					if (intp == NULL) {
17017c478bd9Sstevel@tonic-gate 						Fprintf(stderr,
17027c478bd9Sstevel@tonic-gate 						    "%s: can't find "
17037c478bd9Sstevel@tonic-gate 						    "IPV6_HOPLIMIT ancillary "
17047c478bd9Sstevel@tonic-gate 						    "data\n", prog);
17057c478bd9Sstevel@tonic-gate 						exit(EXIT_FAILURE);
17067c478bd9Sstevel@tonic-gate 					}
17077c478bd9Sstevel@tonic-gate 					hoplimit = *intp;
17087c478bd9Sstevel@tonic-gate 				}
17097c478bd9Sstevel@tonic-gate 
17107c478bd9Sstevel@tonic-gate 				if (reply == REPLY_GOT_TARGET) {
17117c478bd9Sstevel@tonic-gate 					got_there = _B_TRUE;
17127c478bd9Sstevel@tonic-gate 
17137c478bd9Sstevel@tonic-gate 					if (((pr->family == AF_INET) &&
17147c478bd9Sstevel@tonic-gate 					    (ip->ip_ttl <= 1)) ||
17157c478bd9Sstevel@tonic-gate 					    ((pr->family == AF_INET6) &&
17167c478bd9Sstevel@tonic-gate 					    (hoplimit <= 1)))
17177c478bd9Sstevel@tonic-gate 						Printf(" !");
17187c478bd9Sstevel@tonic-gate 				}
17197c478bd9Sstevel@tonic-gate 
17207c478bd9Sstevel@tonic-gate 				if (!collect_stat && showttl) {
17217c478bd9Sstevel@tonic-gate 					if (pr->family == AF_INET) {
17227c478bd9Sstevel@tonic-gate 						Printf(" (ttl=%d)",
17237c478bd9Sstevel@tonic-gate 						    (int)ip->ip_ttl);
17247c478bd9Sstevel@tonic-gate 					} else if (hoplimit != -1) {
17257c478bd9Sstevel@tonic-gate 						Printf(" (hop limit=%d)",
17267c478bd9Sstevel@tonic-gate 						    hoplimit);
17277c478bd9Sstevel@tonic-gate 					}
17287c478bd9Sstevel@tonic-gate 				}
17297c478bd9Sstevel@tonic-gate 
17307c478bd9Sstevel@tonic-gate 				if (reply == REPLY_GOT_OTHER) {
17317c478bd9Sstevel@tonic-gate 					if ((*pr->print_icmp_other_fn)
17327c478bd9Sstevel@tonic-gate 					    (type, code)) {
17337c478bd9Sstevel@tonic-gate 						unreachable++;
17347c478bd9Sstevel@tonic-gate 					}
17357c478bd9Sstevel@tonic-gate 				}
17367c478bd9Sstevel@tonic-gate 
17377c478bd9Sstevel@tonic-gate 				/* special case */
17387c478bd9Sstevel@tonic-gate 				if (pr->family == AF_INET &&
17397c478bd9Sstevel@tonic-gate 				    type == ICMP_UNREACH &&
17407c478bd9Sstevel@tonic-gate 				    code == ICMP_UNREACH_PROTOCOL)
17417c478bd9Sstevel@tonic-gate 					got_there = _B_TRUE;
17427c478bd9Sstevel@tonic-gate 
17437c478bd9Sstevel@tonic-gate 				break;
17447c478bd9Sstevel@tonic-gate 			}
17457c478bd9Sstevel@tonic-gate 
17467c478bd9Sstevel@tonic-gate 			seq = (seq + 1) % (MAX_SEQ + 1);
17477c478bd9Sstevel@tonic-gate 
17487c478bd9Sstevel@tonic-gate 			if (cc == 0) {
17497c478bd9Sstevel@tonic-gate 				Printf(" *");
17507c478bd9Sstevel@tonic-gate 				timeouts++;
17517c478bd9Sstevel@tonic-gate 			}
17527c478bd9Sstevel@tonic-gate 
17537c478bd9Sstevel@tonic-gate 			(void) fflush(stdout);
17547c478bd9Sstevel@tonic-gate 		}
17557c478bd9Sstevel@tonic-gate 
17567c478bd9Sstevel@tonic-gate 		if (collect_stat) {
17577c478bd9Sstevel@tonic-gate 			print_stats(probe, nreceived, rttmin, rttmax, rttsum,
17587c478bd9Sstevel@tonic-gate 			    rttssq);
17597c478bd9Sstevel@tonic-gate 		}
17607c478bd9Sstevel@tonic-gate 
17617c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
17627c478bd9Sstevel@tonic-gate 
17637c478bd9Sstevel@tonic-gate 		/* either we hit the target or received too many unreachables */
17647c478bd9Sstevel@tonic-gate 		if (got_there ||
17657c478bd9Sstevel@tonic-gate 		    (unreachable > 0 && unreachable >= nprobes - 1))
17667c478bd9Sstevel@tonic-gate 			break;
17677c478bd9Sstevel@tonic-gate 	}
17687c478bd9Sstevel@tonic-gate 
17697c478bd9Sstevel@tonic-gate 	/* Ignore the SIGINT between traceroute() runs */
17707c478bd9Sstevel@tonic-gate 	if (probe_all)
17717c478bd9Sstevel@tonic-gate 		(void) signal(SIGINT, SIG_IGN);
17727c478bd9Sstevel@tonic-gate }
17737c478bd9Sstevel@tonic-gate 
17747c478bd9Sstevel@tonic-gate /*
17757c478bd9Sstevel@tonic-gate  * for a given destination address and address family, it finds out what
17767c478bd9Sstevel@tonic-gate  * source address kernel is going to pick
17777c478bd9Sstevel@tonic-gate  */
17787c478bd9Sstevel@tonic-gate static void
17797c478bd9Sstevel@tonic-gate select_src_addr(union any_in_addr *dst_addr, union any_in_addr *src_addr,
17807c478bd9Sstevel@tonic-gate     int family)
17817c478bd9Sstevel@tonic-gate {
17827c478bd9Sstevel@tonic-gate 	int tmp_fd;
17837c478bd9Sstevel@tonic-gate 	struct sockaddr *sock;
17847c478bd9Sstevel@tonic-gate 	struct sockaddr_in *sin;
17857c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *sin6;
17867c478bd9Sstevel@tonic-gate 	size_t sock_len;
17877c478bd9Sstevel@tonic-gate 
17887c478bd9Sstevel@tonic-gate 	sock = (struct sockaddr *)malloc(sizeof (struct sockaddr_in6));
17897c478bd9Sstevel@tonic-gate 	if (sock == NULL) {
17907c478bd9Sstevel@tonic-gate 		Fprintf(stderr, "%s: malloc %s\n", prog, strerror(errno));
17917c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
17927c478bd9Sstevel@tonic-gate 	}
17937c478bd9Sstevel@tonic-gate 	(void) bzero(sock, sizeof (struct sockaddr_in6));
17947c478bd9Sstevel@tonic-gate 
17957c478bd9Sstevel@tonic-gate 	if (family == AF_INET) {
17967c478bd9Sstevel@tonic-gate 		/* LINTED E_BAD_PTR_CAST_ALIGN */
17977c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)sock;
17987c478bd9Sstevel@tonic-gate 		sin->sin_family = AF_INET;
17997c478bd9Sstevel@tonic-gate 		sin->sin_addr = dst_addr->addr;
18007c478bd9Sstevel@tonic-gate 		sin->sin_port = IPPORT_ECHO;	/* port shouldn't be 0 */
18017c478bd9Sstevel@tonic-gate 		sock_len = sizeof (struct sockaddr_in);
18027c478bd9Sstevel@tonic-gate 	} else {
18037c478bd9Sstevel@tonic-gate 		/* LINTED E_BAD_PTR_CAST_ALIGN */
18047c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)sock;
18057c478bd9Sstevel@tonic-gate 		sin6->sin6_family = AF_INET6;
18067c478bd9Sstevel@tonic-gate 		sin6->sin6_addr = dst_addr->addr6;
18077c478bd9Sstevel@tonic-gate 		sin6->sin6_port = IPPORT_ECHO;	/* port shouldn't be 0 */
18087c478bd9Sstevel@tonic-gate 		sock_len = sizeof (struct sockaddr_in6);
18097c478bd9Sstevel@tonic-gate 	}
18107c478bd9Sstevel@tonic-gate 
18117c478bd9Sstevel@tonic-gate 	/* open a UDP socket */
18127c478bd9Sstevel@tonic-gate 	if ((tmp_fd = socket(family, SOCK_DGRAM, 0)) < 0) {
18137c478bd9Sstevel@tonic-gate 		Fprintf(stderr, "%s: udp socket: %s\n", prog,
18147c478bd9Sstevel@tonic-gate 		    strerror(errno));
18157c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
18167c478bd9Sstevel@tonic-gate 	}
18177c478bd9Sstevel@tonic-gate 
18187c478bd9Sstevel@tonic-gate 	/* connect it */
18197c478bd9Sstevel@tonic-gate 	if (connect(tmp_fd, sock, sock_len) < 0) {
18207c478bd9Sstevel@tonic-gate 		/*
18217c478bd9Sstevel@tonic-gate 		 * If there's no route to the destination, this connect() call
18227c478bd9Sstevel@tonic-gate 		 * fails. We just return all-zero (wildcard) as the source
18237c478bd9Sstevel@tonic-gate 		 * address, so that user can get to see "no route to dest"
18247c478bd9Sstevel@tonic-gate 		 * message, as it'll try to send the probe packet out and will
18257c478bd9Sstevel@tonic-gate 		 * receive ICMP unreachable.
18267c478bd9Sstevel@tonic-gate 		 */
18277c478bd9Sstevel@tonic-gate 		if (family == AF_INET)
18287c478bd9Sstevel@tonic-gate 			src_addr->addr.s_addr = INADDR_ANY;
18297c478bd9Sstevel@tonic-gate 		else
18307c478bd9Sstevel@tonic-gate 			src_addr->addr6 = in6addr_any;
18317c478bd9Sstevel@tonic-gate 		free(sock);
18327c478bd9Sstevel@tonic-gate 		return;
18337c478bd9Sstevel@tonic-gate 	}
18347c478bd9Sstevel@tonic-gate 
18357c478bd9Sstevel@tonic-gate 	/* get the local sock info */
18367c478bd9Sstevel@tonic-gate 	if (getsockname(tmp_fd, sock, &sock_len) < 0) {
18377c478bd9Sstevel@tonic-gate 		Fprintf(stderr, "%s: getsockname: %s\n", prog,
18387c478bd9Sstevel@tonic-gate 		    strerror(errno));
18397c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
18407c478bd9Sstevel@tonic-gate 	}
18417c478bd9Sstevel@tonic-gate 
18427c478bd9Sstevel@tonic-gate 	if (family == AF_INET) {
18437c478bd9Sstevel@tonic-gate 		/* LINTED E_BAD_PTR_CAST_ALIGN */
18447c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)sock;
18457c478bd9Sstevel@tonic-gate 		src_addr->addr = sin->sin_addr;
18467c478bd9Sstevel@tonic-gate 	} else {
18477c478bd9Sstevel@tonic-gate 		/* LINTED E_BAD_PTR_CAST_ALIGN */
18487c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)sock;
18497c478bd9Sstevel@tonic-gate 		src_addr->addr6 = sin6->sin6_addr;
18507c478bd9Sstevel@tonic-gate 	}
18517c478bd9Sstevel@tonic-gate 
18527c478bd9Sstevel@tonic-gate 	free(sock);
18537c478bd9Sstevel@tonic-gate 	(void) close(tmp_fd);
18547c478bd9Sstevel@tonic-gate }
18557c478bd9Sstevel@tonic-gate 
18567c478bd9Sstevel@tonic-gate /*
18577c478bd9Sstevel@tonic-gate  * Checksum routine for Internet Protocol family headers (C Version)
18587c478bd9Sstevel@tonic-gate  */
18597c478bd9Sstevel@tonic-gate ushort_t
18607c478bd9Sstevel@tonic-gate in_cksum(ushort_t *addr, int len)
18617c478bd9Sstevel@tonic-gate {
18627c478bd9Sstevel@tonic-gate 	int nleft = len;
18637c478bd9Sstevel@tonic-gate 	ushort_t *w = addr;
18647c478bd9Sstevel@tonic-gate 	ushort_t answer;
18657c478bd9Sstevel@tonic-gate 	int sum = 0;
18667c478bd9Sstevel@tonic-gate 
18677c478bd9Sstevel@tonic-gate 	/*
18687c478bd9Sstevel@tonic-gate 	 *  Our algorithm is simple, using a 32 bit accumulator (sum),
18697c478bd9Sstevel@tonic-gate 	 *  we add sequential 16 bit words to it, and at the end, fold
18707c478bd9Sstevel@tonic-gate 	 *  back all the carry bits from the top 16 bits into the lower
18717c478bd9Sstevel@tonic-gate 	 *  16 bits.
18727c478bd9Sstevel@tonic-gate 	 */
18737c478bd9Sstevel@tonic-gate 	while (nleft > 1)  {
18747c478bd9Sstevel@tonic-gate 		sum += *w++;
18757c478bd9Sstevel@tonic-gate 		nleft -= 2;
18767c478bd9Sstevel@tonic-gate 	}
18777c478bd9Sstevel@tonic-gate 
18787c478bd9Sstevel@tonic-gate 	/* mop up an odd byte, if necessary */
18797c478bd9Sstevel@tonic-gate 	if (nleft == 1)
18807c478bd9Sstevel@tonic-gate 		sum += *(uchar_t *)w;
18817c478bd9Sstevel@tonic-gate 
18827c478bd9Sstevel@tonic-gate 	/* add back carry outs from top 16 bits to low 16 bits */
18837c478bd9Sstevel@tonic-gate 	sum = (sum >> 16) + (sum & 0xffff);	/* add hi 16 to low 16 */
18847c478bd9Sstevel@tonic-gate 	sum += (sum >> 16);			/* add carry */
18857c478bd9Sstevel@tonic-gate 	answer = ~sum;				/* truncate to 16 bits */
18867c478bd9Sstevel@tonic-gate 	return (answer);
18877c478bd9Sstevel@tonic-gate }
18887c478bd9Sstevel@tonic-gate 
18897c478bd9Sstevel@tonic-gate /*
18907c478bd9Sstevel@tonic-gate  * Wait until a reply arrives or timeout occurs. If packet arrived, read it
18917c478bd9Sstevel@tonic-gate  * return the size of the packet read.
18927c478bd9Sstevel@tonic-gate  */
18937c478bd9Sstevel@tonic-gate static int
18947c478bd9Sstevel@tonic-gate wait_for_reply(int sock, struct msghdr *msg, struct timeval *tp)
18957c478bd9Sstevel@tonic-gate {
18967c478bd9Sstevel@tonic-gate 	fd_set fds;
18977c478bd9Sstevel@tonic-gate 	struct timeval now, wait;
18987c478bd9Sstevel@tonic-gate 	int cc = 0;
18997c478bd9Sstevel@tonic-gate 	int result;
19007c478bd9Sstevel@tonic-gate 
19017c478bd9Sstevel@tonic-gate 	(void) FD_ZERO(&fds);
19027c478bd9Sstevel@tonic-gate 	FD_SET(sock, &fds);
19037c478bd9Sstevel@tonic-gate 
19047c478bd9Sstevel@tonic-gate 	wait.tv_sec = tp->tv_sec + waittime;
19057c478bd9Sstevel@tonic-gate 	wait.tv_usec = tp->tv_usec;
19067c478bd9Sstevel@tonic-gate 	(void) gettimeofday(&now, NULL);
19077c478bd9Sstevel@tonic-gate 	tv_sub(&wait, &now);
19087c478bd9Sstevel@tonic-gate 
19097c478bd9Sstevel@tonic-gate 	if (wait.tv_sec < 0 || wait.tv_usec < 0)
19107c478bd9Sstevel@tonic-gate 		return (0);
19117c478bd9Sstevel@tonic-gate 
19127c478bd9Sstevel@tonic-gate 	result = select(sock + 1, &fds, (fd_set *)NULL, (fd_set *)NULL, &wait);
19137c478bd9Sstevel@tonic-gate 
19147c478bd9Sstevel@tonic-gate 	if (result == -1) {
19157c478bd9Sstevel@tonic-gate 		if (errno != EINTR) {
19167c478bd9Sstevel@tonic-gate 			Fprintf(stderr, "%s: select: %s\n", prog,
19177c478bd9Sstevel@tonic-gate 			    strerror(errno));
19187c478bd9Sstevel@tonic-gate 		}
19197c478bd9Sstevel@tonic-gate 	} else if (result > 0)
19207c478bd9Sstevel@tonic-gate 		cc = recvmsg(sock, msg, 0);
19217c478bd9Sstevel@tonic-gate 
19227c478bd9Sstevel@tonic-gate 	return (cc);
19237c478bd9Sstevel@tonic-gate }
19247c478bd9Sstevel@tonic-gate 
19257c478bd9Sstevel@tonic-gate /*
19267c478bd9Sstevel@tonic-gate  * Construct an Internet address representation. If the nflag has been supplied,
19277c478bd9Sstevel@tonic-gate  * give numeric value, otherwise try for symbolic name.
19287c478bd9Sstevel@tonic-gate  */
19297c478bd9Sstevel@tonic-gate char *
19307c478bd9Sstevel@tonic-gate inet_name(union any_in_addr *in, int family)
19317c478bd9Sstevel@tonic-gate {
19327c478bd9Sstevel@tonic-gate 	char *cp;
19337c478bd9Sstevel@tonic-gate 	static boolean_t first = _B_TRUE;
19347c478bd9Sstevel@tonic-gate 	static char domain[NI_MAXHOST + 1];
19357c478bd9Sstevel@tonic-gate 	static char line[NI_MAXHOST + 1];	/* assuming		*/
19367c478bd9Sstevel@tonic-gate 				/* (NI_MAXHOST + 1) >= INET6_ADDRSTRLEN */
19377c478bd9Sstevel@tonic-gate 	char hbuf[NI_MAXHOST];
19387c478bd9Sstevel@tonic-gate 	socklen_t slen;
19397c478bd9Sstevel@tonic-gate 	struct sockaddr_in sin;
19407c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 sin6;
19417c478bd9Sstevel@tonic-gate 	struct sockaddr *sa;
19427c478bd9Sstevel@tonic-gate 	int flags;
19437c478bd9Sstevel@tonic-gate 
19447c478bd9Sstevel@tonic-gate 	switch (family) {
19457c478bd9Sstevel@tonic-gate 	case AF_INET:
19467c478bd9Sstevel@tonic-gate 		slen = sizeof (struct sockaddr_in);
19477c478bd9Sstevel@tonic-gate 		sin.sin_addr = in->addr;
19487c478bd9Sstevel@tonic-gate 		sin.sin_port = 0;
19497c478bd9Sstevel@tonic-gate 		sa = (struct sockaddr *)&sin;
19507c478bd9Sstevel@tonic-gate 		break;
19517c478bd9Sstevel@tonic-gate 	case AF_INET6:
19527c478bd9Sstevel@tonic-gate 		slen = sizeof (struct sockaddr_in6);
19537c478bd9Sstevel@tonic-gate 		sin6.sin6_addr = in->addr6;
19547c478bd9Sstevel@tonic-gate 		sin6.sin6_port = 0;
19557c478bd9Sstevel@tonic-gate 		sa = (struct sockaddr *)&sin6;
19567c478bd9Sstevel@tonic-gate 		break;
19577c478bd9Sstevel@tonic-gate 	deafult:
19587c478bd9Sstevel@tonic-gate 		(void) snprintf(line, sizeof (line),
19597c478bd9Sstevel@tonic-gate 		    "<invalid address family>");
19607c478bd9Sstevel@tonic-gate 		return (line);
19617c478bd9Sstevel@tonic-gate 	}
19627c478bd9Sstevel@tonic-gate 	sa->sa_family = family;
19637c478bd9Sstevel@tonic-gate 
19647c478bd9Sstevel@tonic-gate 	if (first && !nflag) {
19657c478bd9Sstevel@tonic-gate 		/* find out the domain name */
19667c478bd9Sstevel@tonic-gate 		first = _B_FALSE;
19677c478bd9Sstevel@tonic-gate 		if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
19687c478bd9Sstevel@tonic-gate 		    (cp = strchr(domain, '.')) != NULL) {
19697c478bd9Sstevel@tonic-gate 			(void) strncpy(domain, cp + 1, sizeof (domain) - 1);
19707c478bd9Sstevel@tonic-gate 			domain[sizeof (domain) - 1] = '\0';
19717c478bd9Sstevel@tonic-gate 		} else {
19727c478bd9Sstevel@tonic-gate 			domain[0] = '\0';
19737c478bd9Sstevel@tonic-gate 		}
19747c478bd9Sstevel@tonic-gate 	}
19757c478bd9Sstevel@tonic-gate 
19767c478bd9Sstevel@tonic-gate 	flags = (nflag) ? NI_NUMERICHOST : NI_NAMEREQD;
19777c478bd9Sstevel@tonic-gate 	if (getnameinfo(sa, slen, hbuf, sizeof (hbuf), NULL, 0, flags) != 0) {
19787c478bd9Sstevel@tonic-gate 		if (inet_ntop(family, (const void *)&in->addr6,
19797c478bd9Sstevel@tonic-gate 		    hbuf, sizeof (hbuf)) == NULL)
19807c478bd9Sstevel@tonic-gate 			hbuf[0] = 0;
19817c478bd9Sstevel@tonic-gate 	} else if (!nflag && (cp = strchr(hbuf, '.')) != NULL &&
19827c478bd9Sstevel@tonic-gate 	    strcmp(cp + 1, domain) == 0) {
19837c478bd9Sstevel@tonic-gate 		*cp = '\0';
19847c478bd9Sstevel@tonic-gate 	}
19857c478bd9Sstevel@tonic-gate 	(void) strlcpy(line, hbuf, sizeof (line));
19867c478bd9Sstevel@tonic-gate 
19877c478bd9Sstevel@tonic-gate 	return (line);
19887c478bd9Sstevel@tonic-gate }
19897c478bd9Sstevel@tonic-gate 
19907c478bd9Sstevel@tonic-gate /*
19917c478bd9Sstevel@tonic-gate  * return the difference (in msec) between two time values
19927c478bd9Sstevel@tonic-gate  */
19937c478bd9Sstevel@tonic-gate static double
19947c478bd9Sstevel@tonic-gate deltaT(struct timeval *t1p, struct timeval *t2p)
19957c478bd9Sstevel@tonic-gate {
19967c478bd9Sstevel@tonic-gate 	double dt;
19977c478bd9Sstevel@tonic-gate 
19987c478bd9Sstevel@tonic-gate 	dt = (double)(t2p->tv_sec - t1p->tv_sec) * 1000.0 +
19997c478bd9Sstevel@tonic-gate 	    (double)(t2p->tv_usec - t1p->tv_usec) / 1000.0;
20007c478bd9Sstevel@tonic-gate 	return (dt);
20017c478bd9Sstevel@tonic-gate }
20027c478bd9Sstevel@tonic-gate 
20037c478bd9Sstevel@tonic-gate /*
20047c478bd9Sstevel@tonic-gate  * Subtract 2 timeval structs:  out = out - in.
20057c478bd9Sstevel@tonic-gate  * Out is assumed to be >= in.
20067c478bd9Sstevel@tonic-gate  */
20077c478bd9Sstevel@tonic-gate static void
20087c478bd9Sstevel@tonic-gate tv_sub(struct timeval *out, struct timeval *in)
20097c478bd9Sstevel@tonic-gate {
20107c478bd9Sstevel@tonic-gate 	if ((out->tv_usec -= in->tv_usec) < 0)   {
20117c478bd9Sstevel@tonic-gate 		--out->tv_sec;
20127c478bd9Sstevel@tonic-gate 		out->tv_usec += 1000000;
20137c478bd9Sstevel@tonic-gate 	}
20147c478bd9Sstevel@tonic-gate 	out->tv_sec -= in->tv_sec;
20157c478bd9Sstevel@tonic-gate }
20167c478bd9Sstevel@tonic-gate 
20177c478bd9Sstevel@tonic-gate /*
20187c478bd9Sstevel@tonic-gate  * record statistics
20197c478bd9Sstevel@tonic-gate  */
20207c478bd9Sstevel@tonic-gate static void
20217c478bd9Sstevel@tonic-gate record_stats(double rtt, int *nreceived, double *rttmin, double *rttmax,
20227c478bd9Sstevel@tonic-gate     double *rttsum, double *rttssq)
20237c478bd9Sstevel@tonic-gate {
20247c478bd9Sstevel@tonic-gate 	if (*nreceived == 0) {
20257c478bd9Sstevel@tonic-gate 		*rttmin = rtt;
20267c478bd9Sstevel@tonic-gate 		*rttmax = rtt;
20277c478bd9Sstevel@tonic-gate 		*rttsum = rtt;
20287c478bd9Sstevel@tonic-gate 		*rttssq = rtt * rtt;
20297c478bd9Sstevel@tonic-gate 	} else {
20307c478bd9Sstevel@tonic-gate 		if (rtt < *rttmin)
20317c478bd9Sstevel@tonic-gate 			*rttmin = rtt;
20327c478bd9Sstevel@tonic-gate 
20337c478bd9Sstevel@tonic-gate 		if (rtt > *rttmax)
20347c478bd9Sstevel@tonic-gate 			*rttmax = rtt;
20357c478bd9Sstevel@tonic-gate 
20367c478bd9Sstevel@tonic-gate 		*rttsum += rtt;
20377c478bd9Sstevel@tonic-gate 		*rttssq += rtt * rtt;
20387c478bd9Sstevel@tonic-gate 	}
20397c478bd9Sstevel@tonic-gate 
20407c478bd9Sstevel@tonic-gate 	(*nreceived)++;
20417c478bd9Sstevel@tonic-gate }
20427c478bd9Sstevel@tonic-gate 
20437c478bd9Sstevel@tonic-gate /*
20447c478bd9Sstevel@tonic-gate  * display statistics
20457c478bd9Sstevel@tonic-gate  */
20467c478bd9Sstevel@tonic-gate static void
20477c478bd9Sstevel@tonic-gate print_stats(int ntransmitted, int nreceived, double rttmin, double rttmax,
20487c478bd9Sstevel@tonic-gate     double rttsum, double rttssq)
20497c478bd9Sstevel@tonic-gate {
20507c478bd9Sstevel@tonic-gate 	double rttavg;			/* average round-trip time */
20517c478bd9Sstevel@tonic-gate 	double rttstd;			/* rtt standard deviation */
20527c478bd9Sstevel@tonic-gate 
20537c478bd9Sstevel@tonic-gate 	if (ntransmitted > 0 && ntransmitted >= nreceived) {
20547c478bd9Sstevel@tonic-gate 		int missed = ntransmitted - nreceived;
20557c478bd9Sstevel@tonic-gate 		double loss = 100 * (double)missed / (double)ntransmitted;
20567c478bd9Sstevel@tonic-gate 
20577c478bd9Sstevel@tonic-gate 		if (nreceived > 0) {
20587c478bd9Sstevel@tonic-gate 			rttavg = rttsum / nreceived;
20597c478bd9Sstevel@tonic-gate 			rttstd = rttssq - (rttavg * rttsum);
20607c478bd9Sstevel@tonic-gate 			rttstd = xsqrt(rttstd / nreceived);
20617c478bd9Sstevel@tonic-gate 
20627c478bd9Sstevel@tonic-gate 			Printf("  %.3f", rttmin);
20637c478bd9Sstevel@tonic-gate 			Printf("/%.3f", rttavg);
20647c478bd9Sstevel@tonic-gate 			Printf("/%.3f", rttmax);
20657c478bd9Sstevel@tonic-gate 
20667c478bd9Sstevel@tonic-gate 			Printf(" (%.3f) ms ", rttstd);
20677c478bd9Sstevel@tonic-gate 		}
20687c478bd9Sstevel@tonic-gate 
20697c478bd9Sstevel@tonic-gate 		Printf(" %d/%d pkts", nreceived, ntransmitted);
20707c478bd9Sstevel@tonic-gate 
20717c478bd9Sstevel@tonic-gate 		if (nreceived == 0)
20727c478bd9Sstevel@tonic-gate 			Printf(" (100%% loss)");
20737c478bd9Sstevel@tonic-gate 		else
20747c478bd9Sstevel@tonic-gate 			Printf(" (%.2g%% loss)", loss);
20757c478bd9Sstevel@tonic-gate 	}
20767c478bd9Sstevel@tonic-gate }
20777c478bd9Sstevel@tonic-gate 
20787c478bd9Sstevel@tonic-gate /*
20797c478bd9Sstevel@tonic-gate  * square root function
20807c478bd9Sstevel@tonic-gate  */
20817c478bd9Sstevel@tonic-gate double
20827c478bd9Sstevel@tonic-gate xsqrt(double y)
20837c478bd9Sstevel@tonic-gate {
20847c478bd9Sstevel@tonic-gate 	double t, x;
20857c478bd9Sstevel@tonic-gate 
20867c478bd9Sstevel@tonic-gate 	if (y <= 0) {
20877c478bd9Sstevel@tonic-gate 		return (0.0);
20887c478bd9Sstevel@tonic-gate 	}
20897c478bd9Sstevel@tonic-gate 
20907c478bd9Sstevel@tonic-gate 	x = (y < 1.0) ? 1.0 : y;
20917c478bd9Sstevel@tonic-gate 	do {
20927c478bd9Sstevel@tonic-gate 		t = x;
20937c478bd9Sstevel@tonic-gate 		x = (t + (y/t))/2.0;
20947c478bd9Sstevel@tonic-gate 	} while (0 < x && x < t);
20957c478bd9Sstevel@tonic-gate 
20967c478bd9Sstevel@tonic-gate 	return (x);
20977c478bd9Sstevel@tonic-gate }
20987c478bd9Sstevel@tonic-gate 
20997c478bd9Sstevel@tonic-gate /*
21007c478bd9Sstevel@tonic-gate  * String to double with optional min and max.
21017c478bd9Sstevel@tonic-gate  */
21027c478bd9Sstevel@tonic-gate static double
21037c478bd9Sstevel@tonic-gate str2dbl(const char *str, const char *what, double mi, double ma)
21047c478bd9Sstevel@tonic-gate {
21057c478bd9Sstevel@tonic-gate 	double val;
21067c478bd9Sstevel@tonic-gate 	char *ep;
21077c478bd9Sstevel@tonic-gate 
21087c478bd9Sstevel@tonic-gate 	errno = 0;
21097c478bd9Sstevel@tonic-gate 
21107c478bd9Sstevel@tonic-gate 	val = strtod(str, &ep);
21117c478bd9Sstevel@tonic-gate 	if (errno != 0 || *ep != '\0') {
21127c478bd9Sstevel@tonic-gate 		Fprintf(stderr, "%s: \"%s\" bad value for %s \n",
21137c478bd9Sstevel@tonic-gate 		    prog, str, what);
21147c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
21157c478bd9Sstevel@tonic-gate 	}
21167c478bd9Sstevel@tonic-gate 	if (val < mi && mi >= 0) {
21177c478bd9Sstevel@tonic-gate 		Fprintf(stderr, "%s: %s must be >= %f\n", prog, what, mi);
21187c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
21197c478bd9Sstevel@tonic-gate 	}
21207c478bd9Sstevel@tonic-gate 	if (val > ma && ma >= 0) {
21217c478bd9Sstevel@tonic-gate 		Fprintf(stderr, "%s: %s must be <= %f\n", prog, what, ma);
21227c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
21237c478bd9Sstevel@tonic-gate 	}
21247c478bd9Sstevel@tonic-gate 	return (val);
21257c478bd9Sstevel@tonic-gate }
21267c478bd9Sstevel@tonic-gate 
21277c478bd9Sstevel@tonic-gate /*
21287c478bd9Sstevel@tonic-gate  * String to int with optional min and max. Handles decimal and hex.
21297c478bd9Sstevel@tonic-gate  */
21307c478bd9Sstevel@tonic-gate static int
21317c478bd9Sstevel@tonic-gate str2int(const char *str, const char *what, int mi, int ma)
21327c478bd9Sstevel@tonic-gate {
21337c478bd9Sstevel@tonic-gate 	const char *cp;
21347c478bd9Sstevel@tonic-gate 	int val;
21357c478bd9Sstevel@tonic-gate 	char *ep;
21367c478bd9Sstevel@tonic-gate 
21377c478bd9Sstevel@tonic-gate 	errno = 0;
21387c478bd9Sstevel@tonic-gate 
21397c478bd9Sstevel@tonic-gate 	if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) {
21407c478bd9Sstevel@tonic-gate 		cp = str + 2;
21417c478bd9Sstevel@tonic-gate 		val = (int)strtol(cp, &ep, 16);
21427c478bd9Sstevel@tonic-gate 	} else {
21437c478bd9Sstevel@tonic-gate 		val = (int)strtol(str, &ep, 10);
21447c478bd9Sstevel@tonic-gate 	}
21457c478bd9Sstevel@tonic-gate 	if (errno != 0 || *ep != '\0') {
21467c478bd9Sstevel@tonic-gate 		Fprintf(stderr, "%s: \"%s\" bad value for %s \n",
21477c478bd9Sstevel@tonic-gate 		    prog, str, what);
21487c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
21497c478bd9Sstevel@tonic-gate 	}
21507c478bd9Sstevel@tonic-gate 	if (val < mi && mi >= 0) {
21517c478bd9Sstevel@tonic-gate 		if (mi == 0) {
21527c478bd9Sstevel@tonic-gate 			Fprintf(stderr, "%s: %s must be >= %d\n",
21537c478bd9Sstevel@tonic-gate 			    prog, what, mi);
21547c478bd9Sstevel@tonic-gate 		} else {
21557c478bd9Sstevel@tonic-gate 			Fprintf(stderr, "%s: %s must be > %d\n",
21567c478bd9Sstevel@tonic-gate 			    prog, what, mi - 1);
21577c478bd9Sstevel@tonic-gate 		}
21587c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
21597c478bd9Sstevel@tonic-gate 	}
21607c478bd9Sstevel@tonic-gate 	if (val > ma && ma >= 0) {
21617c478bd9Sstevel@tonic-gate 		Fprintf(stderr, "%s: %s must be <= %d\n", prog, what, ma);
21627c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
21637c478bd9Sstevel@tonic-gate 	}
21647c478bd9Sstevel@tonic-gate 	return (val);
21657c478bd9Sstevel@tonic-gate }
21667c478bd9Sstevel@tonic-gate 
21677c478bd9Sstevel@tonic-gate /*
21687c478bd9Sstevel@tonic-gate  * This is the interrupt handler for SIGINT and SIGQUIT. It's completely handled
21697c478bd9Sstevel@tonic-gate  * where it jumps to.
21707c478bd9Sstevel@tonic-gate  */
21717c478bd9Sstevel@tonic-gate static void
21727c478bd9Sstevel@tonic-gate sig_handler(int sig)
21737c478bd9Sstevel@tonic-gate {
21747c478bd9Sstevel@tonic-gate 	longjmp(env, sig);
21757c478bd9Sstevel@tonic-gate }
21767c478bd9Sstevel@tonic-gate 
21777c478bd9Sstevel@tonic-gate /*
21787c478bd9Sstevel@tonic-gate  * display the usage of traceroute
21797c478bd9Sstevel@tonic-gate  */
21807c478bd9Sstevel@tonic-gate static void
21817c478bd9Sstevel@tonic-gate usage(void)
21827c478bd9Sstevel@tonic-gate {
21837c478bd9Sstevel@tonic-gate 	Fprintf(stderr, "Usage: %s [-adFIlnSvx] [-A address_family] "
21847c478bd9Sstevel@tonic-gate "[-c traffic_class] \n"
21857c478bd9Sstevel@tonic-gate "\t[-f first_hop] [-g gateway [-g gateway ...]| -r] [-i iface]\n"
21867c478bd9Sstevel@tonic-gate "\t[-L flow_label] [-m max_hop] [-P pause_sec] [-p port] [-Q max_timeout]\n"
21877c478bd9Sstevel@tonic-gate "\t[-q nqueries] [-s src_addr] [-t tos] [-w wait_time] host [packetlen]\n",
21887c478bd9Sstevel@tonic-gate 		prog);
21897c478bd9Sstevel@tonic-gate 	exit(EXIT_FAILURE);
21907c478bd9Sstevel@tonic-gate }
2191