17c478bd9Sstevel@tonic-gate /*
2e11c3f44Smeem  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
4afee3dc6SRobert Mustacchi  * Copyright (c) 2017, Joyent, Inc.
57c478bd9Sstevel@tonic-gate  */
67c478bd9Sstevel@tonic-gate 
77c478bd9Sstevel@tonic-gate /*
87c478bd9Sstevel@tonic-gate  * Copyright (c) 1988, 1989, 1991, 1994, 1995, 1996, 1997
97c478bd9Sstevel@tonic-gate  *	The Regents of the University of California.  All rights reserved.
107c478bd9Sstevel@tonic-gate  *
117c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
127c478bd9Sstevel@tonic-gate  * modification, are permitted provided that: (1) source code distributions
137c478bd9Sstevel@tonic-gate  * retain the above copyright notice and this paragraph in its entirety, (2)
147c478bd9Sstevel@tonic-gate  * distributions including binary code include the above copyright notice and
157c478bd9Sstevel@tonic-gate  * this paragraph in its entirety in the documentation or other materials
167c478bd9Sstevel@tonic-gate  * provided with the distribution, and (3) all advertising materials mentioning
177c478bd9Sstevel@tonic-gate  * features or use of this software display the following acknowledgement:
187c478bd9Sstevel@tonic-gate  * ``This product includes software developed by the University of California,
197c478bd9Sstevel@tonic-gate  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
207c478bd9Sstevel@tonic-gate  * the University nor the names of its contributors may be used to endorse
217c478bd9Sstevel@tonic-gate  * or promote products derived from this software without specific prior
227c478bd9Sstevel@tonic-gate  * written permission.
237c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
247c478bd9Sstevel@tonic-gate  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
257c478bd9Sstevel@tonic-gate  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
267c478bd9Sstevel@tonic-gate  *
277c478bd9Sstevel@tonic-gate  *
287c478bd9Sstevel@tonic-gate  * @(#)$Header: traceroute.c,v 1.49 97/06/13 02:30:23 leres Exp $ (LBL)
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <sys/param.h>
327c478bd9Sstevel@tonic-gate #include <sys/file.h>
337c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
347c478bd9Sstevel@tonic-gate #include <sys/socket.h>
357c478bd9Sstevel@tonic-gate #include <sys/time.h>
367c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include <netinet/in_systm.h>
397c478bd9Sstevel@tonic-gate #include <netinet/in.h>
407c478bd9Sstevel@tonic-gate #include <netinet/ip.h>
417c478bd9Sstevel@tonic-gate #include <netinet/ip_var.h>
427c478bd9Sstevel@tonic-gate #include <netinet/ip_icmp.h>
437c478bd9Sstevel@tonic-gate #include <netinet/udp.h>
447c478bd9Sstevel@tonic-gate #include <netinet/udp_var.h>
457c478bd9Sstevel@tonic-gate #include <netinet/ip6.h>
467c478bd9Sstevel@tonic-gate #include <netinet/icmp6.h>
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate #include <ctype.h>
517c478bd9Sstevel@tonic-gate #include <errno.h>
527c478bd9Sstevel@tonic-gate #include <malloc.h>
537c478bd9Sstevel@tonic-gate #include <memory.h>
547c478bd9Sstevel@tonic-gate #include <netdb.h>
557c478bd9Sstevel@tonic-gate #include <stdio.h>
567c478bd9Sstevel@tonic-gate #include <stdlib.h>
577c478bd9Sstevel@tonic-gate #include <strings.h>
587c478bd9Sstevel@tonic-gate #include <unistd.h>
597c478bd9Sstevel@tonic-gate #include <libintl.h>
607c478bd9Sstevel@tonic-gate #include <locale.h>
617c478bd9Sstevel@tonic-gate #include <signal.h>
627c478bd9Sstevel@tonic-gate #include <setjmp.h>
637c478bd9Sstevel@tonic-gate #include <limits.h>
647c478bd9Sstevel@tonic-gate #include <zone.h>
65afee3dc6SRobert Mustacchi #include <thread.h>
66afee3dc6SRobert Mustacchi #include <synch.h>
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate #include <priv_utils.h>
697c478bd9Sstevel@tonic-gate 
700406ceaaSmeem #include <libinetutil.h>
717c478bd9Sstevel@tonic-gate #include "traceroute.h"
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate #define	MAX_SEQ			65535	/* max sequence value for ICMP */
747c478bd9Sstevel@tonic-gate #define	MAX_TRAFFIC_CLASS	255	/* max traffic class for IPv6 */
757c478bd9Sstevel@tonic-gate #define	MAX_FLOW_LABEL		0xFFFFF	/* max flow label for IPv6 */
767c478bd9Sstevel@tonic-gate #define	MAX_TOS			255	/* max type-of-service for IPv4 */
777c478bd9Sstevel@tonic-gate #define	STR_LEN			30
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate /* store the information about a host */
807c478bd9Sstevel@tonic-gate struct hostinfo {
817c478bd9Sstevel@tonic-gate 	char *name;		/* hostname */
827c478bd9Sstevel@tonic-gate 	int family;		/* address family of the IP addresses */
837c478bd9Sstevel@tonic-gate 	int num_addr;			/* number of IP addresses */
847c478bd9Sstevel@tonic-gate 	union any_in_addr *addrs;	/* list of IP addresses */
857c478bd9Sstevel@tonic-gate };
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate /* used to store a bunch of protocol specific values */
887c478bd9Sstevel@tonic-gate struct pr_set {
897c478bd9Sstevel@tonic-gate 	int family;		/* AF_INET or AF_INET6 */
907c478bd9Sstevel@tonic-gate 	char name[STR_LEN];	/* "IPv4" or "IPv6" */
917c478bd9Sstevel@tonic-gate 	char icmp[STR_LEN];	/* "icmp" or "ipv6-icmp" */
927c478bd9Sstevel@tonic-gate 	int icmp_minlen;
937c478bd9Sstevel@tonic-gate 	int addr_len;
947c478bd9Sstevel@tonic-gate 	int ip_hdr_len;
957c478bd9Sstevel@tonic-gate 	int packlen;
967c478bd9Sstevel@tonic-gate 	int sock_size;		/* size of sockaddr_in or sockaddr_in6 */
977c478bd9Sstevel@tonic-gate 	struct sockaddr *to;
987c478bd9Sstevel@tonic-gate 	struct sockaddr *from;
997c478bd9Sstevel@tonic-gate 	void *from_sin_addr;
1007c478bd9Sstevel@tonic-gate 	union any_in_addr *gwIPlist;
1017c478bd9Sstevel@tonic-gate 	/* pointers to v4/v6 functions */
1027c478bd9Sstevel@tonic-gate 	struct ip *(*set_buffers_fn) (int);
1037c478bd9Sstevel@tonic-gate 	int (*check_reply_fn)(struct msghdr *, int, int, uchar_t *, uchar_t *);
1047c478bd9Sstevel@tonic-gate 	boolean_t (*print_icmp_other_fn)(uchar_t, uchar_t);
1057c478bd9Sstevel@tonic-gate 	void (*print_addr_fn)(uchar_t *, int, struct sockaddr *);
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate };
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate /*
1107c478bd9Sstevel@tonic-gate  * LBNL bug fixed: in LBNL traceroute 'uchar_t packet[512];'
1117c478bd9Sstevel@tonic-gate  * Not sufficient to hold the complete packet for ECHO REPLY of a big probe.
1127c478bd9Sstevel@tonic-gate  * Packet size is reported incorrectly in such a case.
1137c478bd9Sstevel@tonic-gate  * Also this buffer needs to be 32 bit aligned. In the future the alignment
1147c478bd9Sstevel@tonic-gate  * requirement will be increased to 64 bit. So, let's use 64 bit alignment now.
1157c478bd9Sstevel@tonic-gate  */
1167c478bd9Sstevel@tonic-gate static uint64_t packet[(IP_MAXPACKET + 1)/8];	/* received packet */
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate static struct ip *outip4;	/* output buffer to send as an IPv4 datagram */
1197c478bd9Sstevel@tonic-gate static struct ip *outip6;	/* output buffer to send as an IPv6 datagram */
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate /* Used to store the ancillary data that comes with the received packets */
1227c478bd9Sstevel@tonic-gate static uint64_t ancillary_data[(IP_MAXPACKET + 1)/8];
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate /* first get the gw names, later you'll resolve them based on the family */
1257c478bd9Sstevel@tonic-gate static char *gwlist[MAXMAX_GWS];		/* gateway names list */
1267c478bd9Sstevel@tonic-gate static union any_in_addr gwIPlist[MAX_GWS];	/* gateway IPv4 address list */
1277c478bd9Sstevel@tonic-gate static union any_in_addr gwIP6list[MAX_GWS6];	/* gateway IPv6 address list */
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate static int family_input = AF_UNSPEC;	/* User supplied protocol family */
1307c478bd9Sstevel@tonic-gate static int rcvsock4;		/* receive (icmp) socket file descriptor */
1317c478bd9Sstevel@tonic-gate static int sndsock4;		/* send (udp/icmp) socket file descriptor */
1327c478bd9Sstevel@tonic-gate static int rcvsock6;		/* receive (icmp6) socket file descriptor */
1337c478bd9Sstevel@tonic-gate static int sndsock6;		/* send (udp6/icmp6) socket file descriptor */
1347c478bd9Sstevel@tonic-gate int gw_count = 0;		/* number of gateways */
1357c478bd9Sstevel@tonic-gate static struct sockaddr_in whereto;	/* Who to try to reach */
1367c478bd9Sstevel@tonic-gate static struct sockaddr_in6 whereto6;
1377c478bd9Sstevel@tonic-gate static struct sockaddr_in wherefrom;	/* Who we are */
1387c478bd9Sstevel@tonic-gate static struct sockaddr_in6 wherefrom6;
1397c478bd9Sstevel@tonic-gate static int packlen_input = 0;		/* user input for packlen */
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate char *prog;
1427c478bd9Sstevel@tonic-gate static char *source_input = NULL; /* this is user arg. source, doesn't change */
1437c478bd9Sstevel@tonic-gate static char *source = NULL;	/* this gets modified after name lookup */
1447c478bd9Sstevel@tonic-gate char *hostname;
1457c478bd9Sstevel@tonic-gate static char *device = NULL;   	/* interface name */
1467c478bd9Sstevel@tonic-gate static struct pr_set *pr4;	/* protocol info for IPv4 */
1477c478bd9Sstevel@tonic-gate static struct pr_set *pr6;	/* protocol info for IPv6 */
1487c478bd9Sstevel@tonic-gate static struct ifaddrlist *al4;	/* list of interfaces */
1497c478bd9Sstevel@tonic-gate static struct ifaddrlist *al6;	/* list of interfaces */
1507c478bd9Sstevel@tonic-gate static uint_t if_index = 0;	/* interface index */
1517c478bd9Sstevel@tonic-gate static int num_v4 = 0;		/* count of IPv4 addresses */
1527c478bd9Sstevel@tonic-gate static int num_v6 = 0;		/* count of IPv6 addresses */
1537c478bd9Sstevel@tonic-gate static int num_ifs4 = 0;	/* count of local IPv4 interfaces */
1547c478bd9Sstevel@tonic-gate static int num_ifs6 = 0;	/* count of local IPv6 interfaces */
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate static int nprobes = 3;		/* number of probes */
1577c478bd9Sstevel@tonic-gate static int max_ttl = 30;	/* max number of hops */
1587c478bd9Sstevel@tonic-gate static int first_ttl = 1;	/* initial number of hops */
1597c478bd9Sstevel@tonic-gate ushort_t ident;			/* used to authenticate replies */
1607c478bd9Sstevel@tonic-gate ushort_t port = 32768 + 666;	/* start udp dest port # for probe packets */
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate static int options = 0;		/* socket options */
1637c478bd9Sstevel@tonic-gate boolean_t verbose = _B_FALSE;	/* verbose output */
1647c478bd9Sstevel@tonic-gate static int waittime = 5;	/* time to wait for response (in seconds) */
1657c478bd9Sstevel@tonic-gate static struct timeval delay = {0, 0}; /* delay between consecutive probe */
1667c478bd9Sstevel@tonic-gate boolean_t nflag = _B_FALSE;	/* print addresses numerically */
1677c478bd9Sstevel@tonic-gate static boolean_t showttl = _B_FALSE; /* print the ttl(hop limit) of recvd pkt */
1687c478bd9Sstevel@tonic-gate boolean_t useicmp = _B_FALSE;  	/* use icmp echo instead of udp packets */
1697c478bd9Sstevel@tonic-gate boolean_t docksum = _B_TRUE;	/* calculate checksums */
1707c478bd9Sstevel@tonic-gate static boolean_t collect_stat = _B_FALSE;	/* print statistics */
1717c478bd9Sstevel@tonic-gate boolean_t settos = _B_FALSE;   	/* set type-of-service field */
172bd670b35SErik Nordmark int dontfrag = 0;		/* IP*_DONTFRAG */
1737c478bd9Sstevel@tonic-gate static int max_timeout = 5;	/* quit after this consecutive timeouts */
1747c478bd9Sstevel@tonic-gate static boolean_t probe_all = _B_FALSE;	/* probe all the IFs of the target */
1757c478bd9Sstevel@tonic-gate static boolean_t pick_src = _B_FALSE;	/* traceroute picks the src address */
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate /*
1787c478bd9Sstevel@tonic-gate  * flow and class are specific to IPv6, tos and off are specific to IPv4.
1797c478bd9Sstevel@tonic-gate  * Each protocol uses the ones that are specific to itself, and ignores
1807c478bd9Sstevel@tonic-gate  * others.
1817c478bd9Sstevel@tonic-gate  */
1827c478bd9Sstevel@tonic-gate static uint_t flow = 0;		/* IPv6 flow info */
1837c478bd9Sstevel@tonic-gate static uint_t class = 0;	/* IPv6 class */
1847c478bd9Sstevel@tonic-gate uchar_t tos = 0;		/* IPv4 type-of-service */
1857c478bd9Sstevel@tonic-gate ushort_t off = 0;		/* set DF bit */
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate static jmp_buf env;		/* stack environment for longjmp() */
1887c478bd9Sstevel@tonic-gate boolean_t raw_req;		/* if sndsock for IPv4 must be raw */
1897c478bd9Sstevel@tonic-gate 
190afee3dc6SRobert Mustacchi /*
191afee3dc6SRobert Mustacchi  * Name service lookup related data.
192afee3dc6SRobert Mustacchi  */
193afee3dc6SRobert Mustacchi static mutex_t tr_nslock = ERRORCHECKMUTEX;
194afee3dc6SRobert Mustacchi static boolean_t tr_nsactive = _B_FALSE;	/* Lookup ongoing */
195afee3dc6SRobert Mustacchi static hrtime_t tr_nsstarttime;			/* Start time */
196afee3dc6SRobert Mustacchi static int tr_nssleeptime = 2;			/* Interval between checks */
197afee3dc6SRobert Mustacchi static int tr_nswarntime = 2;			/* Interval to warn after */
198afee3dc6SRobert Mustacchi 
1997c478bd9Sstevel@tonic-gate /* Forwards */
2007c478bd9Sstevel@tonic-gate static uint_t calc_packetlen(int, struct pr_set *);
2017c478bd9Sstevel@tonic-gate extern int check_reply(struct msghdr *, int, int, uchar_t *, uchar_t *);
2027c478bd9Sstevel@tonic-gate extern int check_reply6(struct msghdr *, int, int, uchar_t *, uchar_t *);
2037c478bd9Sstevel@tonic-gate static double deltaT(struct timeval *, struct timeval *);
2047c478bd9Sstevel@tonic-gate static char *device_name(struct ifaddrlist *, int, union any_in_addr *,
2057c478bd9Sstevel@tonic-gate     struct pr_set *);
2067c478bd9Sstevel@tonic-gate extern void *find_ancillary_data(struct msghdr *, int, int);
2077c478bd9Sstevel@tonic-gate static boolean_t has_addr(struct addrinfo *, union any_in_addr *);
2087c478bd9Sstevel@tonic-gate static struct ifaddrlist *find_device(struct ifaddrlist *, int, char *);
2097c478bd9Sstevel@tonic-gate static struct ifaddrlist *find_ifaddr(struct ifaddrlist *, int,
2107c478bd9Sstevel@tonic-gate     union any_in_addr *, int);
2117c478bd9Sstevel@tonic-gate static void get_gwaddrs(char **, int, union any_in_addr *,
2127c478bd9Sstevel@tonic-gate     union any_in_addr *, int *, int *);
2137c478bd9Sstevel@tonic-gate static void get_hostinfo(char *, int, struct addrinfo **);
2147c478bd9Sstevel@tonic-gate char *inet_name(union any_in_addr *, int);
2157c478bd9Sstevel@tonic-gate ushort_t in_cksum(ushort_t *, int);
2167c478bd9Sstevel@tonic-gate extern int ip_hdr_length_v6(ip6_t *, int, uint8_t *);
2177c478bd9Sstevel@tonic-gate extern char *pr_type(uchar_t);
2187c478bd9Sstevel@tonic-gate extern char *pr_type6(uchar_t);
2197c478bd9Sstevel@tonic-gate extern void print_addr(uchar_t *, int, struct sockaddr *);
2207c478bd9Sstevel@tonic-gate extern void print_addr6(uchar_t *, int, struct sockaddr *);
2217c478bd9Sstevel@tonic-gate extern boolean_t print_icmp_other(uchar_t, uchar_t);
2227c478bd9Sstevel@tonic-gate extern boolean_t print_icmp_other6(uchar_t, uchar_t);
2237c478bd9Sstevel@tonic-gate static void print_stats(int, int, double, double, double, double);
2247c478bd9Sstevel@tonic-gate static void print_unknown_host_msg(const char *, const char *);
2257c478bd9Sstevel@tonic-gate static void record_stats(double, int *, double *, double *, double *, double *);
2267c478bd9Sstevel@tonic-gate static void resolve_nodes(int *, struct addrinfo **);
2277c478bd9Sstevel@tonic-gate static void select_src_addr(union any_in_addr *, union any_in_addr *, int);
2287c478bd9Sstevel@tonic-gate extern void send_probe(int, struct sockaddr *, struct ip *, int, int,
2297c478bd9Sstevel@tonic-gate     struct timeval *, int);
2307c478bd9Sstevel@tonic-gate extern void send_probe6(int, struct msghdr *, struct ip *, int, int,
2317c478bd9Sstevel@tonic-gate     struct timeval *, int);
2327c478bd9Sstevel@tonic-gate extern void set_ancillary_data(struct msghdr *, int, union any_in_addr *, int,
2337c478bd9Sstevel@tonic-gate     uint_t);
2347c478bd9Sstevel@tonic-gate extern struct ip *set_buffers(int);
2357c478bd9Sstevel@tonic-gate extern struct ip *set_buffers6(int);
2367c478bd9Sstevel@tonic-gate extern void set_IPv4opt_sourcerouting(int, union any_in_addr *,
2377c478bd9Sstevel@tonic-gate     union any_in_addr *);
2387c478bd9Sstevel@tonic-gate static void set_sin(struct sockaddr *, union any_in_addr *, int);
2397c478bd9Sstevel@tonic-gate static int set_src_addr(struct pr_set *, struct ifaddrlist **);
2407c478bd9Sstevel@tonic-gate static void setup_protocol(struct pr_set *, int);
2417c478bd9Sstevel@tonic-gate static void setup_socket(struct pr_set *, int);
2427c478bd9Sstevel@tonic-gate static void sig_handler(int);
2437c478bd9Sstevel@tonic-gate static int str2int(const char *, const char *, int, int);
2447c478bd9Sstevel@tonic-gate static double str2dbl(const char *, const char *, double, double);
2457c478bd9Sstevel@tonic-gate static void trace_it(struct addrinfo *);
2467c478bd9Sstevel@tonic-gate static void traceroute(union any_in_addr *, struct msghdr *, struct pr_set *,
2477c478bd9Sstevel@tonic-gate     int, struct ifaddrlist *);
2487c478bd9Sstevel@tonic-gate static void tv_sub(struct timeval *, struct timeval *);
2497c478bd9Sstevel@tonic-gate static void usage(void);
2507c478bd9Sstevel@tonic-gate static int wait_for_reply(int, struct msghdr *, struct timeval *);
2517c478bd9Sstevel@tonic-gate static double xsqrt(double);
252afee3dc6SRobert Mustacchi static void *ns_warning_thr(void *);
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate /*
2557c478bd9Sstevel@tonic-gate  * main
2567c478bd9Sstevel@tonic-gate  */
2578c332a0dSja int
main(int argc,char ** argv)2587c478bd9Sstevel@tonic-gate main(int argc, char **argv)
2597c478bd9Sstevel@tonic-gate {
2607c478bd9Sstevel@tonic-gate 	struct addrinfo *ai_dst = NULL;		/* destination host */
2617c478bd9Sstevel@tonic-gate 	/*
2627c478bd9Sstevel@tonic-gate 	 * "probing_successful" indicates if we could successfully send probes,
2637c478bd9Sstevel@tonic-gate 	 * not necessarily received reply from the target (this behavior is from
2647c478bd9Sstevel@tonic-gate 	 * the original traceroute). It's _B_FALSE if packlen is invalid, or no
2657c478bd9Sstevel@tonic-gate 	 * interfaces found.
2667c478bd9Sstevel@tonic-gate 	 */
2677c478bd9Sstevel@tonic-gate 	boolean_t probing_successful = _B_FALSE;
2687c478bd9Sstevel@tonic-gate 	int longjmp_return;			/* return value from longjump */
2697c478bd9Sstevel@tonic-gate 	int i = 0;
2707c478bd9Sstevel@tonic-gate 	char *cp;
2717c478bd9Sstevel@tonic-gate 	int op;
2727c478bd9Sstevel@tonic-gate 	char *ep;
2737c478bd9Sstevel@tonic-gate 	char temp_buf[INET6_ADDRSTRLEN];	/* use for inet_ntop() */
2747c478bd9Sstevel@tonic-gate 	double pause;
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	/*
2777c478bd9Sstevel@tonic-gate 	 * A raw socket will be used for IPv4 if there is sufficient
2787c478bd9Sstevel@tonic-gate 	 * privilege.
2797c478bd9Sstevel@tonic-gate 	 */
2807c478bd9Sstevel@tonic-gate 	raw_req = priv_ineffect(PRIV_NET_RAWACCESS);
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	/*
2837c478bd9Sstevel@tonic-gate 	 * We'll need the privilege only when we open the sockets; that's
2847c478bd9Sstevel@tonic-gate 	 * when we'll fail if the program has insufficient privileges.
2857c478bd9Sstevel@tonic-gate 	 */
2867c478bd9Sstevel@tonic-gate 	(void) __init_suid_priv(PU_CLEARLIMITSET, PRIV_NET_ICMPACCESS,
2877c478bd9Sstevel@tonic-gate 	    raw_req ? PRIV_NET_RAWACCESS : NULL, NULL);
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	(void) setlinebuf(stdout);
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	if ((cp = strrchr(argv[0], '/')) != NULL)
2927c478bd9Sstevel@tonic-gate 		prog = cp + 1;
2937c478bd9Sstevel@tonic-gate 	else
2947c478bd9Sstevel@tonic-gate 		prog = argv[0];
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	opterr = 0;
2977c478bd9Sstevel@tonic-gate 	while ((op = getopt(argc, argv, "adFIlnrSvxA:c:f:g:i:L:m:P:p:Q:q:s:"
2987c478bd9Sstevel@tonic-gate 	    "t:w:")) != EOF) {
2997c478bd9Sstevel@tonic-gate 		switch (op) {
3007c478bd9Sstevel@tonic-gate 		case 'A':
3017c478bd9Sstevel@tonic-gate 			if (strcmp(optarg, "inet") == 0) {
3027c478bd9Sstevel@tonic-gate 				family_input = AF_INET;
3037c478bd9Sstevel@tonic-gate 			} else if (strcmp(optarg, "inet6") == 0) {
3047c478bd9Sstevel@tonic-gate 				family_input = AF_INET6;
3057c478bd9Sstevel@tonic-gate 			} else {
3067c478bd9Sstevel@tonic-gate 				Fprintf(stderr,
3077c478bd9Sstevel@tonic-gate 				    "%s: unknown address family %s\n",
3087c478bd9Sstevel@tonic-gate 				    prog, optarg);
3097c478bd9Sstevel@tonic-gate 				exit(EXIT_FAILURE);
3107c478bd9Sstevel@tonic-gate 			}
3117c478bd9Sstevel@tonic-gate 			break;
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 		case 'a':
3147c478bd9Sstevel@tonic-gate 			probe_all = _B_TRUE;
3157c478bd9Sstevel@tonic-gate 			break;
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 		case 'c':
3187c478bd9Sstevel@tonic-gate 			class = str2int(optarg, "traffic class", 0,
3197c478bd9Sstevel@tonic-gate 			    MAX_TRAFFIC_CLASS);
3207c478bd9Sstevel@tonic-gate 			break;
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 		case 'd':
3237c478bd9Sstevel@tonic-gate 			options |= SO_DEBUG;
3247c478bd9Sstevel@tonic-gate 			break;
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 		case 'f':
3277c478bd9Sstevel@tonic-gate 			first_ttl = str2int(optarg, "first ttl", 1, MAXTTL);
3287c478bd9Sstevel@tonic-gate 			break;
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 		case 'F':
3317c478bd9Sstevel@tonic-gate 			off = IP_DF;
332bd670b35SErik Nordmark 			dontfrag = 1;
3337c478bd9Sstevel@tonic-gate 			break;
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 		case 'g':
3367c478bd9Sstevel@tonic-gate 			if (!raw_req) {
3377c478bd9Sstevel@tonic-gate 				Fprintf(stderr,
3387c478bd9Sstevel@tonic-gate 				    "%s: privilege to specify a loose source "
3397c478bd9Sstevel@tonic-gate 				    "route gateway is unavailable\n",
3407c478bd9Sstevel@tonic-gate 				    prog);
3417c478bd9Sstevel@tonic-gate 				exit(EXIT_FAILURE);
3427c478bd9Sstevel@tonic-gate 			}
3437d6fbbeaSblu 			if (gw_count >= MAXMAX_GWS) {
3447c478bd9Sstevel@tonic-gate 				Fprintf(stderr,
3457c478bd9Sstevel@tonic-gate 				    "%s: Too many gateways\n", prog);
3467c478bd9Sstevel@tonic-gate 				exit(EXIT_FAILURE);
3477c478bd9Sstevel@tonic-gate 			}
3487c478bd9Sstevel@tonic-gate 			gwlist[gw_count] = strdup(optarg);
3497c478bd9Sstevel@tonic-gate 			if (gwlist[gw_count] == NULL) {
3507c478bd9Sstevel@tonic-gate 				Fprintf(stderr, "%s: strdup %s\n", prog,
3517c478bd9Sstevel@tonic-gate 				    strerror(errno));
3527c478bd9Sstevel@tonic-gate 				exit(EXIT_FAILURE);
3537c478bd9Sstevel@tonic-gate 			}
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 			++gw_count;
3567c478bd9Sstevel@tonic-gate 			break;
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 		case 'l':
3597c478bd9Sstevel@tonic-gate 			showttl = _B_TRUE;
3607c478bd9Sstevel@tonic-gate 			break;
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 		case 'i':
3637c478bd9Sstevel@tonic-gate 			/* this can be IF name or IF index */
3647c478bd9Sstevel@tonic-gate 			if_index = (uint_t)strtol(optarg, &ep, 10);
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 			/* convert IF index <-->  IF name */
3677c478bd9Sstevel@tonic-gate 			if (errno != 0 || *ep != '\0') {
3687c478bd9Sstevel@tonic-gate 				device = optarg;
3697c478bd9Sstevel@tonic-gate 				if_index = if_nametoindex((const char *)device);
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 				/*
3727c478bd9Sstevel@tonic-gate 				 * In case it fails, check to see if the problem
3737c478bd9Sstevel@tonic-gate 				 * is other than "IF not found".
3747c478bd9Sstevel@tonic-gate 				 */
3757c478bd9Sstevel@tonic-gate 				if (if_index == 0 && errno != ENXIO) {
3767c478bd9Sstevel@tonic-gate 					Fprintf(stderr, "%s: if_nametoindex:"
3777c478bd9Sstevel@tonic-gate 					    "%s\n", prog, strerror(errno));
3787c478bd9Sstevel@tonic-gate 					exit(EXIT_FAILURE);
3797c478bd9Sstevel@tonic-gate 				}
3807c478bd9Sstevel@tonic-gate 			} else {
3817c478bd9Sstevel@tonic-gate 				device = (char *)malloc(LIFNAMSIZ + 1);
3827c478bd9Sstevel@tonic-gate 				if (device == NULL) {
3837c478bd9Sstevel@tonic-gate 					Fprintf(stderr, "%s: malloc: %s\n",
3847c478bd9Sstevel@tonic-gate 					    prog, strerror(errno));
3857c478bd9Sstevel@tonic-gate 					exit(EXIT_FAILURE);
3867c478bd9Sstevel@tonic-gate 				}
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 				device = if_indextoname(if_index, device);
3897c478bd9Sstevel@tonic-gate 				if (device != NULL) {
3907c478bd9Sstevel@tonic-gate 					device[LIFNAMSIZ] = '\0';
3917c478bd9Sstevel@tonic-gate 				} else if (errno != ENXIO) {
3927c478bd9Sstevel@tonic-gate 					/*
3937c478bd9Sstevel@tonic-gate 					 * The problem was other than "index
3947c478bd9Sstevel@tonic-gate 					 * not found".
3957c478bd9Sstevel@tonic-gate 					 */
3967c478bd9Sstevel@tonic-gate 					Fprintf(stderr, "%s: if_indextoname:"
3977c478bd9Sstevel@tonic-gate 					    "%s\n", prog, strerror(errno));
3987c478bd9Sstevel@tonic-gate 					exit(EXIT_FAILURE);
3997c478bd9Sstevel@tonic-gate 				}
4007c478bd9Sstevel@tonic-gate 			}
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 			if (device == NULL || if_index == 0) {
4037c478bd9Sstevel@tonic-gate 				Fprintf(stderr, "%s: interface %s "
4047c478bd9Sstevel@tonic-gate 				    "doesn't match any actual interfaces\n",
4057c478bd9Sstevel@tonic-gate 				    prog, optarg);
4067c478bd9Sstevel@tonic-gate 				exit(EXIT_FAILURE);
4077c478bd9Sstevel@tonic-gate 			}
4087c478bd9Sstevel@tonic-gate 			break;
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 		case 'I':
4117c478bd9Sstevel@tonic-gate 			useicmp = _B_TRUE;
4127c478bd9Sstevel@tonic-gate 			break;
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 		case 'L':
4157c478bd9Sstevel@tonic-gate 			flow = str2int(optarg, "flow label", 0, MAX_FLOW_LABEL);
4167c478bd9Sstevel@tonic-gate 			break;
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 		case 'm':
4197c478bd9Sstevel@tonic-gate 			max_ttl = str2int(optarg, "max ttl(hop limit)", 1,
4207c478bd9Sstevel@tonic-gate 			    MAXTTL);
4217c478bd9Sstevel@tonic-gate 			break;
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 		case 'n':
4247c478bd9Sstevel@tonic-gate 			nflag = _B_TRUE;
4257c478bd9Sstevel@tonic-gate 			break;
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 		case 'P':
4287c478bd9Sstevel@tonic-gate 			pause = str2dbl(optarg, "pause", 0, INT_MAX);
4297c478bd9Sstevel@tonic-gate 			delay.tv_sec = (time_t)pause;
4307c478bd9Sstevel@tonic-gate 			delay.tv_usec = (suseconds_t)((pause - delay.tv_sec) *
4317c478bd9Sstevel@tonic-gate 			    1000000);
4327c478bd9Sstevel@tonic-gate 			break;
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 		case 'p':
4357c478bd9Sstevel@tonic-gate 			port = str2int(optarg, "port", 1, MAX_PORT);
4367c478bd9Sstevel@tonic-gate 			break;
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 		case 'Q':
4397c478bd9Sstevel@tonic-gate 			max_timeout = str2int(optarg, "max timeout", 1, -1);
4407c478bd9Sstevel@tonic-gate 			break;
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 		case 'q':
4437c478bd9Sstevel@tonic-gate 			nprobes = str2int(optarg, "nprobes", 1, -1);
4447c478bd9Sstevel@tonic-gate 			break;
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 		case 'r':
4477c478bd9Sstevel@tonic-gate 			options |= SO_DONTROUTE;
4487c478bd9Sstevel@tonic-gate 			break;
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 		case 'S':
4517c478bd9Sstevel@tonic-gate 			collect_stat = _B_TRUE;
4527c478bd9Sstevel@tonic-gate 			break;
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 		case 's':
4557c478bd9Sstevel@tonic-gate 			/*
4567c478bd9Sstevel@tonic-gate 			 * set the ip source address of the outbound
4577c478bd9Sstevel@tonic-gate 			 * probe (e.g., on a multi-homed host).
4587c478bd9Sstevel@tonic-gate 			 */
4597c478bd9Sstevel@tonic-gate 			source_input = optarg;
4607c478bd9Sstevel@tonic-gate 			break;
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 		case 't':
4637c478bd9Sstevel@tonic-gate 			tos = (uchar_t)str2int(optarg, "tos", 0, MAX_TOS);
4647c478bd9Sstevel@tonic-gate 			settos = _B_TRUE;
4657c478bd9Sstevel@tonic-gate 			break;
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 		case 'v':
4687c478bd9Sstevel@tonic-gate 			verbose = _B_TRUE;
4697c478bd9Sstevel@tonic-gate 			break;
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 		case 'x':
4727c478bd9Sstevel@tonic-gate 			docksum = _B_FALSE;
4737c478bd9Sstevel@tonic-gate 			break;
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 		case 'w':
4767c478bd9Sstevel@tonic-gate 			waittime = str2int(optarg, "wait time", 2, -1);
4777c478bd9Sstevel@tonic-gate 			break;
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 		default:
4807c478bd9Sstevel@tonic-gate 			usage();
4817c478bd9Sstevel@tonic-gate 			break;
4827c478bd9Sstevel@tonic-gate 		}
4837c478bd9Sstevel@tonic-gate 	}
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 	/*
4867c478bd9Sstevel@tonic-gate 	 * If it's probe_all, SIGQUIT makes traceroute exit(). But we set the
4877c478bd9Sstevel@tonic-gate 	 * address to jump back to in traceroute(). Until then, we'll need to
4887c478bd9Sstevel@tonic-gate 	 * temporarily specify one.
4897c478bd9Sstevel@tonic-gate 	 */
4907c478bd9Sstevel@tonic-gate 	if (probe_all) {
4917c478bd9Sstevel@tonic-gate 		if ((longjmp_return = setjmp(env)) != 0) {
4927c478bd9Sstevel@tonic-gate 			if (longjmp_return == SIGQUIT) {
4937c478bd9Sstevel@tonic-gate 				Printf("(exiting)\n");
4947c478bd9Sstevel@tonic-gate 				exit(EXIT_SUCCESS);
4957c478bd9Sstevel@tonic-gate 			} else {		/* should never happen */
4967c478bd9Sstevel@tonic-gate 				exit(EXIT_FAILURE);
4977c478bd9Sstevel@tonic-gate 			}
4987c478bd9Sstevel@tonic-gate 		}
4997c478bd9Sstevel@tonic-gate 		(void) signal(SIGQUIT, sig_handler);
5007c478bd9Sstevel@tonic-gate 	}
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 	if ((gw_count > 0) && (options & SO_DONTROUTE)) {
5037c478bd9Sstevel@tonic-gate 		Fprintf(stderr, "%s: loose source route gateways (-g)"
5047c478bd9Sstevel@tonic-gate 		    " cannot be specified when probe packets are sent"
5057c478bd9Sstevel@tonic-gate 		    " directly to a host on an attached network (-r)\n",
5067c478bd9Sstevel@tonic-gate 		    prog);
5077c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
5087c478bd9Sstevel@tonic-gate 	}
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 	i = argc - optind;
5117c478bd9Sstevel@tonic-gate 	if (i == 1 || i == 2) {
5127c478bd9Sstevel@tonic-gate 		hostname = argv[optind];
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 		if (i == 2) {
5157c478bd9Sstevel@tonic-gate 			/* accept any length now, we'll check it later */
5167c478bd9Sstevel@tonic-gate 			packlen_input = str2int(argv[optind + 1],
5177c478bd9Sstevel@tonic-gate 			    "packet length", 0, -1);
5187c478bd9Sstevel@tonic-gate 		}
5197c478bd9Sstevel@tonic-gate 	} else {
5207c478bd9Sstevel@tonic-gate 		usage();
5217c478bd9Sstevel@tonic-gate 	}
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 	if (first_ttl > max_ttl) {
5247c478bd9Sstevel@tonic-gate 		Fprintf(stderr,
5257c478bd9Sstevel@tonic-gate 		    "%s: first ttl(hop limit) (%d) may not be greater"
5267c478bd9Sstevel@tonic-gate 		    " than max ttl(hop limit) (%d)\n",
5277c478bd9Sstevel@tonic-gate 		    prog, first_ttl, max_ttl);
5287c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
5297c478bd9Sstevel@tonic-gate 	}
5307c478bd9Sstevel@tonic-gate 
531afee3dc6SRobert Mustacchi 	/*
532afee3dc6SRobert Mustacchi 	 * Start up the name services warning thread.
533afee3dc6SRobert Mustacchi 	 */
534afee3dc6SRobert Mustacchi 	if (thr_create(NULL, 0, ns_warning_thr, NULL,
535afee3dc6SRobert Mustacchi 	    THR_DETACHED | THR_DAEMON, NULL) != 0) {
536afee3dc6SRobert Mustacchi 		Fprintf(stderr, "%s: failed to create name services "
537afee3dc6SRobert Mustacchi 		    "thread: %s\n", prog, strerror(errno));
538afee3dc6SRobert Mustacchi 		exit(EXIT_FAILURE);
539afee3dc6SRobert Mustacchi 	}
540afee3dc6SRobert Mustacchi 
541afee3dc6SRobert Mustacchi 
5427c478bd9Sstevel@tonic-gate 	/* resolve hostnames */
5437c478bd9Sstevel@tonic-gate 	resolve_nodes(&family_input, &ai_dst);
5447c478bd9Sstevel@tonic-gate 	if (ai_dst == NULL) {
5457c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
5467c478bd9Sstevel@tonic-gate 	}
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 	/*
5497c478bd9Sstevel@tonic-gate 	 * If it's probe_all, SIGINT makes traceroute skip to probing next IP
5507c478bd9Sstevel@tonic-gate 	 * address of the target. The new interrupt handler is assigned in
5517c478bd9Sstevel@tonic-gate 	 * traceroute() function. Until then let's ignore the signal.
5527c478bd9Sstevel@tonic-gate 	 */
5537c478bd9Sstevel@tonic-gate 	if (probe_all)
5547c478bd9Sstevel@tonic-gate 		(void) signal(SIGINT, SIG_IGN);
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 	ident = (getpid() & 0xffff) | 0x8000;
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate 	/*
5597c478bd9Sstevel@tonic-gate 	 * We KNOW that probe_all == TRUE if family is AF_UNSPEC,
5607c478bd9Sstevel@tonic-gate 	 * since family is set to the specific AF found unless it's
5617c478bd9Sstevel@tonic-gate 	 * probe_all. So if family == AF_UNSPEC, we need to init pr4 and pr6.
5627c478bd9Sstevel@tonic-gate 	 */
5637c478bd9Sstevel@tonic-gate 	switch (family_input) {
5647c478bd9Sstevel@tonic-gate 	case AF_UNSPEC:
5657c478bd9Sstevel@tonic-gate 		pr4 = (struct pr_set *)malloc(sizeof (struct pr_set));
5667c478bd9Sstevel@tonic-gate 		if (pr4 == NULL) {
5677c478bd9Sstevel@tonic-gate 			Fprintf(stderr,
5687c478bd9Sstevel@tonic-gate 			    "%s: malloc %s\n", prog, strerror(errno));
5697c478bd9Sstevel@tonic-gate 			exit(EXIT_FAILURE);
5707c478bd9Sstevel@tonic-gate 		}
5717c478bd9Sstevel@tonic-gate 		pr6 = (struct pr_set *)malloc(sizeof (struct pr_set));
5727c478bd9Sstevel@tonic-gate 		if (pr6 == NULL) {
5737c478bd9Sstevel@tonic-gate 			Fprintf(stderr,
5747c478bd9Sstevel@tonic-gate 			    "%s: malloc %s\n", prog, strerror(errno));
5757c478bd9Sstevel@tonic-gate 			exit(EXIT_FAILURE);
5767c478bd9Sstevel@tonic-gate 		}
5777c478bd9Sstevel@tonic-gate 		setup_protocol(pr6, AF_INET6);
5787c478bd9Sstevel@tonic-gate 		setup_protocol(pr4, AF_INET);
5797c478bd9Sstevel@tonic-gate 		outip6 = (*pr6->set_buffers_fn)(pr6->packlen);
5807c478bd9Sstevel@tonic-gate 		setup_socket(pr6, pr6->packlen);
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate 		outip4 = (*pr4->set_buffers_fn)(pr4->packlen);
5837c478bd9Sstevel@tonic-gate 		setup_socket(pr4, pr4->packlen);
5847c478bd9Sstevel@tonic-gate 		num_ifs6 = set_src_addr(pr6, &al6);
5857c478bd9Sstevel@tonic-gate 		num_ifs4 = set_src_addr(pr4, &al4);
5867c478bd9Sstevel@tonic-gate 		break;
5877c478bd9Sstevel@tonic-gate 	case AF_INET6:
5887c478bd9Sstevel@tonic-gate 		pr6 = (struct pr_set *)malloc(sizeof (struct pr_set));
5897c478bd9Sstevel@tonic-gate 		if (pr6 == NULL) {
5907c478bd9Sstevel@tonic-gate 			Fprintf(stderr,
5917c478bd9Sstevel@tonic-gate 			    "%s: malloc %s\n", prog, strerror(errno));
5927c478bd9Sstevel@tonic-gate 			exit(EXIT_FAILURE);
5937c478bd9Sstevel@tonic-gate 		}
5947c478bd9Sstevel@tonic-gate 		setup_protocol(pr6, AF_INET6);
5957c478bd9Sstevel@tonic-gate 		outip6 = (*pr6->set_buffers_fn)(pr6->packlen);
5967c478bd9Sstevel@tonic-gate 		setup_socket(pr6, pr6->packlen);
5977c478bd9Sstevel@tonic-gate 		num_ifs6 = set_src_addr(pr6, &al6);
5987c478bd9Sstevel@tonic-gate 		break;
5997c478bd9Sstevel@tonic-gate 	case AF_INET:
6007c478bd9Sstevel@tonic-gate 		pr4 = (struct pr_set *)malloc(sizeof (struct pr_set));
6017c478bd9Sstevel@tonic-gate 		if (pr4 == NULL) {
6027c478bd9Sstevel@tonic-gate 			Fprintf(stderr,
6037c478bd9Sstevel@tonic-gate 			    "%s: malloc %s\n", prog, strerror(errno));
6047c478bd9Sstevel@tonic-gate 			exit(EXIT_FAILURE);
6057c478bd9Sstevel@tonic-gate 		}
6067c478bd9Sstevel@tonic-gate 		setup_protocol(pr4, AF_INET);
6077c478bd9Sstevel@tonic-gate 		outip4 = (*pr4->set_buffers_fn)(pr4->packlen);
6087c478bd9Sstevel@tonic-gate 		setup_socket(pr4, pr4->packlen);
6097c478bd9Sstevel@tonic-gate 		num_ifs4 = set_src_addr(pr4, &al4);
6107c478bd9Sstevel@tonic-gate 		break;
6117c478bd9Sstevel@tonic-gate 	default:
6127c478bd9Sstevel@tonic-gate 		Fprintf(stderr, "%s: unknow address family.\n", prog);
6137c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
6147c478bd9Sstevel@tonic-gate 	}
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate 	if (num_v4 + num_v6 > 1 && !probe_all) {
6177c478bd9Sstevel@tonic-gate 		if (ai_dst->ai_family == AF_INET) {
6187c478bd9Sstevel@tonic-gate 			Fprintf(stderr,
6197c478bd9Sstevel@tonic-gate 			    "%s: Warning: %s has multiple addresses;"
6207c478bd9Sstevel@tonic-gate 			    " using %s\n", prog, hostname,
6217c478bd9Sstevel@tonic-gate 			    inet_ntop(AF_INET,
6227c478bd9Sstevel@tonic-gate 			    /* LINTED E_BAD_PTR_CAST_ALIGN */
6237c478bd9Sstevel@tonic-gate 			    (void *)&((struct sockaddr_in *)
6247c478bd9Sstevel@tonic-gate 			    ai_dst->ai_addr)->sin_addr,
6257c478bd9Sstevel@tonic-gate 			    temp_buf, sizeof (temp_buf)));
6267c478bd9Sstevel@tonic-gate 		} else {
6277c478bd9Sstevel@tonic-gate 			Fprintf(stderr,
6287c478bd9Sstevel@tonic-gate 			    "%s: Warning: %s has multiple addresses;"
6297c478bd9Sstevel@tonic-gate 			    " using %s\n", prog, hostname,
6307c478bd9Sstevel@tonic-gate 			    inet_ntop(AF_INET6,
6317c478bd9Sstevel@tonic-gate 			    /* LINTED E_BAD_PTR_CAST_ALIGN */
6327c478bd9Sstevel@tonic-gate 			    (void *)&((struct sockaddr_in6 *)
6337c478bd9Sstevel@tonic-gate 			    ai_dst->ai_addr)->sin6_addr,
6347c478bd9Sstevel@tonic-gate 			    temp_buf, sizeof (temp_buf)));
6357c478bd9Sstevel@tonic-gate 		}
6367c478bd9Sstevel@tonic-gate 	}
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 	if (num_ifs4 + num_ifs6 > 0) {
6397c478bd9Sstevel@tonic-gate 		trace_it(ai_dst);
6407c478bd9Sstevel@tonic-gate 		probing_successful = _B_TRUE;
6417c478bd9Sstevel@tonic-gate 	}
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 	(void) close(rcvsock4);
6447c478bd9Sstevel@tonic-gate 	(void) close(sndsock4);
6457c478bd9Sstevel@tonic-gate 	(void) close(rcvsock6);
6467c478bd9Sstevel@tonic-gate 	(void) close(sndsock6);
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 	/*
6497c478bd9Sstevel@tonic-gate 	 * if we could probe any of the IP addresses of the target, that means
6507c478bd9Sstevel@tonic-gate 	 * this was a successful operation
6517c478bd9Sstevel@tonic-gate 	 */
6527c478bd9Sstevel@tonic-gate 	if (probing_successful)
6538c332a0dSja 		return (EXIT_SUCCESS);
6547c478bd9Sstevel@tonic-gate 	else
6558c332a0dSja 		return (EXIT_FAILURE);
6567c478bd9Sstevel@tonic-gate }
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate /*
6597c478bd9Sstevel@tonic-gate  * print "unknown host" message
6607c478bd9Sstevel@tonic-gate  */
6617c478bd9Sstevel@tonic-gate static void
print_unknown_host_msg(const char * protocol,const char * host)6627c478bd9Sstevel@tonic-gate print_unknown_host_msg(const char *protocol, const char *host)
6637c478bd9Sstevel@tonic-gate {
6647c478bd9Sstevel@tonic-gate 	Fprintf(stderr, "%s: unknown%s host %s\n", prog, protocol, host);
6657c478bd9Sstevel@tonic-gate }
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate /*
6687c478bd9Sstevel@tonic-gate  * resolve destination host and gateways
6697c478bd9Sstevel@tonic-gate  */
6707c478bd9Sstevel@tonic-gate static void
resolve_nodes(int * family,struct addrinfo ** ai_dstp)6717c478bd9Sstevel@tonic-gate resolve_nodes(int *family, struct addrinfo **ai_dstp)
6727c478bd9Sstevel@tonic-gate {
6737c478bd9Sstevel@tonic-gate 	struct addrinfo *ai_dst = NULL;
6747c478bd9Sstevel@tonic-gate 	struct addrinfo *aip = NULL;
6757c478bd9Sstevel@tonic-gate 	int num_resolved_gw = 0;
6767c478bd9Sstevel@tonic-gate 	int num_resolved_gw6 = 0;
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 	get_hostinfo(hostname, *family, &ai_dst);
6797c478bd9Sstevel@tonic-gate 	if (ai_dst == NULL) {
6807c478bd9Sstevel@tonic-gate 		print_unknown_host_msg("", hostname);
6817c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
6827c478bd9Sstevel@tonic-gate 	}
6837c478bd9Sstevel@tonic-gate 	/* Get a count of the v4 & v6 addresses */
6847c478bd9Sstevel@tonic-gate 	for (aip = ai_dst; aip != NULL; aip = aip->ai_next) {
6857c478bd9Sstevel@tonic-gate 		switch (aip->ai_family) {
6867c478bd9Sstevel@tonic-gate 		case AF_INET:
6877c478bd9Sstevel@tonic-gate 			num_v4++;
6887c478bd9Sstevel@tonic-gate 			break;
6897c478bd9Sstevel@tonic-gate 		case AF_INET6:
6907c478bd9Sstevel@tonic-gate 			num_v6++;
6917c478bd9Sstevel@tonic-gate 			break;
6927c478bd9Sstevel@tonic-gate 		}
6937c478bd9Sstevel@tonic-gate 	}
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 	if (*family == AF_UNSPEC && !probe_all) {
6967c478bd9Sstevel@tonic-gate 		*family = ai_dst->ai_family;
6977c478bd9Sstevel@tonic-gate 	}
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 	/* resolve gateways */
7007c478bd9Sstevel@tonic-gate 	if (gw_count > 0) {
7017c478bd9Sstevel@tonic-gate 		get_gwaddrs(gwlist, *family, gwIPlist, gwIP6list,
7027c478bd9Sstevel@tonic-gate 		    &num_resolved_gw, &num_resolved_gw6);
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 		/* we couldn't resolve a gateway as an IPv6 host */
7057c478bd9Sstevel@tonic-gate 		if (num_resolved_gw6 != gw_count && num_v6 != 0) {
7067c478bd9Sstevel@tonic-gate 			if (*family == AF_INET6 || *family == AF_UNSPEC)
7077c478bd9Sstevel@tonic-gate 				print_unknown_host_msg(" IPv6",
7087c478bd9Sstevel@tonic-gate 				    gwlist[num_resolved_gw6]);
7097c478bd9Sstevel@tonic-gate 			num_v6 = 0;
7107c478bd9Sstevel@tonic-gate 		}
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 		/* we couldn't resolve a gateway as an IPv4 host */
7137c478bd9Sstevel@tonic-gate 		if (num_resolved_gw != gw_count && num_v4 != 0) {
7147c478bd9Sstevel@tonic-gate 			if (*family == AF_INET || *family == AF_UNSPEC)
7157c478bd9Sstevel@tonic-gate 				print_unknown_host_msg(" IPv4",
7167c478bd9Sstevel@tonic-gate 				    gwlist[num_resolved_gw]);
7177c478bd9Sstevel@tonic-gate 			num_v4 = 0;
7187c478bd9Sstevel@tonic-gate 		}
7197c478bd9Sstevel@tonic-gate 	}
7207c478bd9Sstevel@tonic-gate 
7217d6fbbeaSblu 	*ai_dstp = (num_v4 + num_v6 > 0) ? ai_dst : NULL;
7227c478bd9Sstevel@tonic-gate }
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate /*
7257c478bd9Sstevel@tonic-gate  * Given IP address or hostname, return v4 and v6 hostinfo lists.
7267c478bd9Sstevel@tonic-gate  * Assumes that hostinfo ** ptrs are non-null.
7277c478bd9Sstevel@tonic-gate  */
7287c478bd9Sstevel@tonic-gate static void
get_hostinfo(char * host,int family,struct addrinfo ** aipp)7297c478bd9Sstevel@tonic-gate get_hostinfo(char *host, int family, struct addrinfo **aipp)
7307c478bd9Sstevel@tonic-gate {
7317c478bd9Sstevel@tonic-gate 	struct addrinfo hints, *ai;
7327c478bd9Sstevel@tonic-gate 	struct in6_addr addr6;
7337c478bd9Sstevel@tonic-gate 	struct in_addr addr;
734e11c3f44Smeem 	char abuf[INET6_ADDRSTRLEN];	/* use for inet_ntop() */
7357c478bd9Sstevel@tonic-gate 	int rc;
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 	/*
7387c478bd9Sstevel@tonic-gate 	 * Take care of v4-mapped addresses. It should run same as v4, after
7397c478bd9Sstevel@tonic-gate 	 * chopping off the prefix, leaving the IPv4 address
7407c478bd9Sstevel@tonic-gate 	 */
7417c478bd9Sstevel@tonic-gate 	if ((inet_pton(AF_INET6, host, &addr6) > 0) &&
7427c478bd9Sstevel@tonic-gate 	    IN6_IS_ADDR_V4MAPPED(&addr6)) {
7437c478bd9Sstevel@tonic-gate 		/* peel off the "mapping" stuff, leaving 32 bit IPv4 address */
7447c478bd9Sstevel@tonic-gate 		IN6_V4MAPPED_TO_INADDR(&addr6, &addr);
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate 		/* convert it back to a string */
747e11c3f44Smeem 		(void) inet_ntop(AF_INET, &addr, abuf, sizeof (abuf));
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 		/* now the host is an IPv4 address */
750e11c3f44Smeem 		(void) strcpy(host, abuf);
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate 		/*
7537c478bd9Sstevel@tonic-gate 		 * If it's a mapped address, we convert it into IPv4
7547c478bd9Sstevel@tonic-gate 		 * address because traceroute will send and receive IPv4
7557c478bd9Sstevel@tonic-gate 		 * packets for that address. Therefore, it's a failure case to
7567c478bd9Sstevel@tonic-gate 		 * ask get_hostinfo() to treat a mapped address as an IPv6
7577c478bd9Sstevel@tonic-gate 		 * address.
7587c478bd9Sstevel@tonic-gate 		 */
7597c478bd9Sstevel@tonic-gate 		if (family == AF_INET6) {
7607c478bd9Sstevel@tonic-gate 			return;
7617c478bd9Sstevel@tonic-gate 		}
7627c478bd9Sstevel@tonic-gate 	}
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 	(void) memset(&hints, 0, sizeof (hints));
7657c478bd9Sstevel@tonic-gate 	hints.ai_family = family;
7667ebd3a9bSdme 	hints.ai_flags = AI_ADDRCONFIG | AI_CANONNAME;
7677c478bd9Sstevel@tonic-gate 	rc = getaddrinfo(host, NULL, &hints, &ai);
7687c478bd9Sstevel@tonic-gate 	if (rc != 0) {
7697c478bd9Sstevel@tonic-gate 		if (rc != EAI_NONAME)
7707c478bd9Sstevel@tonic-gate 			Fprintf(stderr, "%s: getaddrinfo: %s\n", prog,
7717c478bd9Sstevel@tonic-gate 			    gai_strerror(rc));
7727d6fbbeaSblu 		*aipp = NULL;
7737c478bd9Sstevel@tonic-gate 		return;
7747c478bd9Sstevel@tonic-gate 	}
7757c478bd9Sstevel@tonic-gate 	*aipp = ai;
7767c478bd9Sstevel@tonic-gate }
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate /*
7797c478bd9Sstevel@tonic-gate  * Calculate the packet length to be used, and check against the valid range.
7807c478bd9Sstevel@tonic-gate  * Returns -1 if range check fails.
7817c478bd9Sstevel@tonic-gate  */
7827c478bd9Sstevel@tonic-gate static uint_t
calc_packetlen(int plen_input,struct pr_set * pr)7837c478bd9Sstevel@tonic-gate calc_packetlen(int plen_input, struct pr_set *pr)
7847c478bd9Sstevel@tonic-gate {
7857c478bd9Sstevel@tonic-gate 	int minpacket;			/* min ip packet size */
7867c478bd9Sstevel@tonic-gate 	int optlen;			/* length of ip options */
7877c478bd9Sstevel@tonic-gate 	int plen;
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 	/*
7907c478bd9Sstevel@tonic-gate 	 * LBNL bug fixed: miscalculation of optlen
7917c478bd9Sstevel@tonic-gate 	 */
7927c478bd9Sstevel@tonic-gate 	if (gw_count > 0) {
7937c478bd9Sstevel@tonic-gate 		/*
7947c478bd9Sstevel@tonic-gate 		 * IPv4:
7957c478bd9Sstevel@tonic-gate 		 * ----
7967c478bd9Sstevel@tonic-gate 		 * 5 (NO OPs) + 3 (code, len, ptr) + gateways
7977c478bd9Sstevel@tonic-gate 		 * IP options field can hold up to 9 gateways. But the API
7987c478bd9Sstevel@tonic-gate 		 * allows you to specify only 8, because the last one is the
7997c478bd9Sstevel@tonic-gate 		 * destination host. When this packet is sent, on the wire
8007c478bd9Sstevel@tonic-gate 		 * you see one gateway replaced by 4 NO OPs. The other 1 NO
8017c478bd9Sstevel@tonic-gate 		 * OP is for alignment
8027c478bd9Sstevel@tonic-gate 		 *
8037c478bd9Sstevel@tonic-gate 		 * IPv6:
8047c478bd9Sstevel@tonic-gate 		 * ----
8057c478bd9Sstevel@tonic-gate 		 * Well, formula is different, but the result is same.
8067c478bd9Sstevel@tonic-gate 		 * 8 byte fixed part for Type 0 Routing header, followed by
8077c478bd9Sstevel@tonic-gate 		 * gateway addresses
8087c478bd9Sstevel@tonic-gate 		 */
8097c478bd9Sstevel@tonic-gate 		optlen = 8 + gw_count * pr->addr_len;
8107c478bd9Sstevel@tonic-gate 	} else {
8117c478bd9Sstevel@tonic-gate 		optlen = 0;
8127c478bd9Sstevel@tonic-gate 	}
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate 	/* take care of the packet length calculations and checks */
8157c478bd9Sstevel@tonic-gate 	minpacket = pr->ip_hdr_len + sizeof (struct outdata) + optlen;
8167c478bd9Sstevel@tonic-gate 	if (useicmp)
8177c478bd9Sstevel@tonic-gate 		minpacket += pr->icmp_minlen;	/* minimum ICMP header size */
8187c478bd9Sstevel@tonic-gate 	else
8197c478bd9Sstevel@tonic-gate 		minpacket += sizeof (struct udphdr);
8207c478bd9Sstevel@tonic-gate 	plen = plen_input;
8217c478bd9Sstevel@tonic-gate 	if (plen == 0) {
8227c478bd9Sstevel@tonic-gate 		plen = minpacket;		/* minimum sized packet */
8237c478bd9Sstevel@tonic-gate 	} else if (minpacket > plen || plen > IP_MAXPACKET) {
8247c478bd9Sstevel@tonic-gate 		Fprintf(stderr, "%s: %s packet size must be >= %d and <= %d\n",
8257c478bd9Sstevel@tonic-gate 		    prog, pr->name, minpacket, IP_MAXPACKET);
8267c478bd9Sstevel@tonic-gate 		return (0);
8277c478bd9Sstevel@tonic-gate 	}
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 	return (plen);
8307c478bd9Sstevel@tonic-gate }
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate /*
8337c478bd9Sstevel@tonic-gate  * Sets the source address by resolving -i and -s arguments, or if -i and -s
8347c478bd9Sstevel@tonic-gate  * don't dictate any, it sets the pick_src to make sure traceroute uses the
8357c478bd9Sstevel@tonic-gate  * kernel's pick of the source address.
8367c478bd9Sstevel@tonic-gate  * Returns number of interfaces configured on the source host, 0 on error or
8377c478bd9Sstevel@tonic-gate  * there's no interface which is up amd not a loopback.
8387c478bd9Sstevel@tonic-gate  */
8397c478bd9Sstevel@tonic-gate static int
set_src_addr(struct pr_set * pr,struct ifaddrlist ** alp)8407c478bd9Sstevel@tonic-gate set_src_addr(struct pr_set *pr, struct ifaddrlist **alp)
8417c478bd9Sstevel@tonic-gate {
8427c478bd9Sstevel@tonic-gate 	union any_in_addr *ap;
8437c478bd9Sstevel@tonic-gate 	struct ifaddrlist *al = NULL;
8447c478bd9Sstevel@tonic-gate 	struct ifaddrlist *tmp1_al = NULL;
8457c478bd9Sstevel@tonic-gate 	struct ifaddrlist *tmp2_al = NULL;
8467c478bd9Sstevel@tonic-gate 	/* LINTED E_BAD_PTR_CAST_ALIGN */
8477c478bd9Sstevel@tonic-gate 	struct sockaddr_in *sin_from = (struct sockaddr_in *)pr->from;
8487c478bd9Sstevel@tonic-gate 	/* LINTED E_BAD_PTR_CAST_ALIGN */
8497c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *sin6_from = (struct sockaddr_in6 *)pr->from;
8507c478bd9Sstevel@tonic-gate 	struct addrinfo *aip;
8517c478bd9Sstevel@tonic-gate 	char errbuf[ERRBUFSIZE];
852e11c3f44Smeem 	char abuf[INET6_ADDRSTRLEN];		/* use for inet_ntop() */
8537c478bd9Sstevel@tonic-gate 	int num_ifs;				/* all the interfaces  */
8547c478bd9Sstevel@tonic-gate 	int num_src_ifs;			/* exclude loopback and down */
8557c478bd9Sstevel@tonic-gate 	int i;
856e11c3f44Smeem 	uint_t ifaddrflags = 0;
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate 	source = source_input;
8597c478bd9Sstevel@tonic-gate 
860e11c3f44Smeem 	if (device != NULL)
861e11c3f44Smeem 		ifaddrflags |= LIFC_UNDER_IPMP;
862e11c3f44Smeem 
8637c478bd9Sstevel@tonic-gate 	/* get the interface address list */
864e11c3f44Smeem 	num_ifs = ifaddrlist(&al, pr->family, ifaddrflags, errbuf);
8657c478bd9Sstevel@tonic-gate 	if (num_ifs < 0) {
8667c478bd9Sstevel@tonic-gate 		Fprintf(stderr, "%s: ifaddrlist: %s\n", prog, errbuf);
8677c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
8687c478bd9Sstevel@tonic-gate 	}
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate 	num_src_ifs = 0;
8717c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_ifs; i++) {
8727c478bd9Sstevel@tonic-gate 		if (!(al[i].flags & IFF_LOOPBACK) && (al[i].flags & IFF_UP))
8737c478bd9Sstevel@tonic-gate 			num_src_ifs++;
8747c478bd9Sstevel@tonic-gate 	}
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate 	if (num_src_ifs == 0) {
8777c478bd9Sstevel@tonic-gate 		Fprintf(stderr, "%s: can't find any %s network interfaces\n",
8787c478bd9Sstevel@tonic-gate 		    prog, pr->name);
8797c478bd9Sstevel@tonic-gate 		return (0);
8807c478bd9Sstevel@tonic-gate 	}
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 	/* verify the device */
8837c478bd9Sstevel@tonic-gate 	if (device != NULL) {
8847c478bd9Sstevel@tonic-gate 		tmp1_al = find_device(al, num_ifs, device);
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate 		if (tmp1_al == NULL) {
8877c478bd9Sstevel@tonic-gate 			Fprintf(stderr, "%s: %s (index %d) is an invalid %s"
8887c478bd9Sstevel@tonic-gate 			    " interface\n", prog, device, if_index, pr->name);
8897c478bd9Sstevel@tonic-gate 			free(al);
8907c478bd9Sstevel@tonic-gate 			return (0);
8917c478bd9Sstevel@tonic-gate 		}
8927c478bd9Sstevel@tonic-gate 	}
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate 	/* verify the source address */
8957c478bd9Sstevel@tonic-gate 	if (source != NULL) {
8967c478bd9Sstevel@tonic-gate 		get_hostinfo(source, pr->family, &aip);
8977c478bd9Sstevel@tonic-gate 		if (aip == NULL) {
8987c478bd9Sstevel@tonic-gate 			Fprintf(stderr,
8997c478bd9Sstevel@tonic-gate 			    "%s: %s is an invalid %s source address\n",
9007c478bd9Sstevel@tonic-gate 			    prog, source, pr->name);
9017c478bd9Sstevel@tonic-gate 
9027c478bd9Sstevel@tonic-gate 			free(al);
9037c478bd9Sstevel@tonic-gate 			return (0);
9047c478bd9Sstevel@tonic-gate 		}
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate 		source = aip->ai_canonname;
9077ebd3a9bSdme 
9087ebd3a9bSdme 		if (pr->family == AF_INET)
9097ebd3a9bSdme 			ap = (union any_in_addr *)
9107ebd3a9bSdme 			    /* LINTED E_BAD_PTR_CAST_ALIGN */
911e11c3f44Smeem 			    &((struct sockaddr_in *)aip->ai_addr)->sin_addr;
9127ebd3a9bSdme 		else
9137ebd3a9bSdme 			ap = (union any_in_addr *)
9147ebd3a9bSdme 			    /* LINTED E_BAD_PTR_CAST_ALIGN */
915e11c3f44Smeem 			    &((struct sockaddr_in6 *)aip->ai_addr)->sin6_addr;
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate 		/*
9187c478bd9Sstevel@tonic-gate 		 * LBNL bug fixed: used to accept any src address
9197c478bd9Sstevel@tonic-gate 		 */
9207c478bd9Sstevel@tonic-gate 		tmp2_al = find_ifaddr(al, num_ifs, ap, pr->family);
9217c478bd9Sstevel@tonic-gate 		if (tmp2_al == NULL) {
922e11c3f44Smeem 			(void) inet_ntop(pr->family, ap, abuf, sizeof (abuf));
923e11c3f44Smeem 			Fprintf(stderr, "%s: %s is not a local %s address\n",
924e11c3f44Smeem 			    prog, abuf, pr->name);
9257c478bd9Sstevel@tonic-gate 			free(al);
9267c478bd9Sstevel@tonic-gate 			freeaddrinfo(aip);
9277c478bd9Sstevel@tonic-gate 			return (0);
9287c478bd9Sstevel@tonic-gate 		}
9297c478bd9Sstevel@tonic-gate 	}
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate 	pick_src = _B_FALSE;
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate 	if (source == NULL) {			/* no -s used */
9347c478bd9Sstevel@tonic-gate 		if (device == NULL) {		/* no -i used, no -s used */
9357c478bd9Sstevel@tonic-gate 			pick_src = _B_TRUE;
9367c478bd9Sstevel@tonic-gate 		} else {			/* -i used, no -s used */
9377c478bd9Sstevel@tonic-gate 			/*
9387c478bd9Sstevel@tonic-gate 			 * -i used, but not -s, and it's IPv4: set the source
9397c478bd9Sstevel@tonic-gate 			 * address to whatever the interface has configured on
9407c478bd9Sstevel@tonic-gate 			 * it.
9417c478bd9Sstevel@tonic-gate 			 */
9427c478bd9Sstevel@tonic-gate 			if (pr->family == AF_INET)
9437c478bd9Sstevel@tonic-gate 				set_sin(pr->from, &(tmp1_al->addr), pr->family);
9447c478bd9Sstevel@tonic-gate 			else
9457c478bd9Sstevel@tonic-gate 				pick_src = _B_TRUE;
9467c478bd9Sstevel@tonic-gate 		}
9477c478bd9Sstevel@tonic-gate 	} else {				/* -s used */
9487c478bd9Sstevel@tonic-gate 		if (device == NULL) {		/* no -i used, -s used */
9497c478bd9Sstevel@tonic-gate 			set_sin(pr->from, ap, pr->family);
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate 			if (aip->ai_next != NULL) {
952e11c3f44Smeem 				(void) inet_ntop(pr->family, pr->from_sin_addr,
953