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