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