17c478bdstevel@tonic-gate/*
2e11c3f4meem * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
37c478bdstevel@tonic-gate * Use is subject to license terms.
4afee3dcRobert Mustacchi * Copyright (c) 2017, Joyent, Inc.
57c478bdstevel@tonic-gate */
67c478bdstevel@tonic-gate
77c478bdstevel@tonic-gate/*
87c478bdstevel@tonic-gate * Copyright (c) 1988, 1989, 1991, 1994, 1995, 1996, 1997
97c478bdstevel@tonic-gate *	The Regents of the University of California.  All rights reserved.
107c478bdstevel@tonic-gate *
117c478bdstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
127c478bdstevel@tonic-gate * modification, are permitted provided that: (1) source code distributions
137c478bdstevel@tonic-gate * retain the above copyright notice and this paragraph in its entirety, (2)
147c478bdstevel@tonic-gate * distributions including binary code include the above copyright notice and
157c478bdstevel@tonic-gate * this paragraph in its entirety in the documentation or other materials
167c478bdstevel@tonic-gate * provided with the distribution, and (3) all advertising materials mentioning
177c478bdstevel@tonic-gate * features or use of this software display the following acknowledgement:
187c478bdstevel@tonic-gate * ``This product includes software developed by the University of California,
197c478bdstevel@tonic-gate * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
207c478bdstevel@tonic-gate * the University nor the names of its contributors may be used to endorse
217c478bdstevel@tonic-gate * or promote products derived from this software without specific prior
227c478bdstevel@tonic-gate * written permission.
237c478bdstevel@tonic-gate * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
247c478bdstevel@tonic-gate * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
257c478bdstevel@tonic-gate * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
267c478bdstevel@tonic-gate *
277c478bdstevel@tonic-gate *
287c478bdstevel@tonic-gate * @(#)$Header: traceroute.c,v 1.49 97/06/13 02:30:23 leres Exp $ (LBL)
297c478bdstevel@tonic-gate */
307c478bdstevel@tonic-gate
317c478bdstevel@tonic-gate#include <sys/param.h>
327c478bdstevel@tonic-gate#include <sys/file.h>
337c478bdstevel@tonic-gate#include <sys/ioctl.h>
347c478bdstevel@tonic-gate#include <sys/socket.h>
357c478bdstevel@tonic-gate#include <sys/time.h>
367c478bdstevel@tonic-gate#include <sys/sysmacros.h>
377c478bdstevel@tonic-gate
387c478bdstevel@tonic-gate#include <netinet/in_systm.h>
397c478bdstevel@tonic-gate#include <netinet/in.h>
407c478bdstevel@tonic-gate#include <netinet/ip.h>
417c478bdstevel@tonic-gate#include <netinet/ip_var.h>
427c478bdstevel@tonic-gate#include <netinet/ip_icmp.h>
437c478bdstevel@tonic-gate#include <netinet/udp.h>
447c478bdstevel@tonic-gate#include <netinet/udp_var.h>
457c478bdstevel@tonic-gate#include <netinet/ip6.h>
467c478bdstevel@tonic-gate#include <netinet/icmp6.h>
477c478bdstevel@tonic-gate
487c478bdstevel@tonic-gate#include <arpa/inet.h>
497c478bdstevel@tonic-gate
507c478bdstevel@tonic-gate#include <ctype.h>
517c478bdstevel@tonic-gate#include <errno.h>
527c478bdstevel@tonic-gate#include <malloc.h>
537c478bdstevel@tonic-gate#include <memory.h>
547c478bdstevel@tonic-gate#include <netdb.h>
557c478bdstevel@tonic-gate#include <stdio.h>
567c478bdstevel@tonic-gate#include <stdlib.h>
577c478bdstevel@tonic-gate#include <strings.h>
587c478bdstevel@tonic-gate#include <unistd.h>
597c478bdstevel@tonic-gate#include <libintl.h>
607c478bdstevel@tonic-gate#include <locale.h>
617c478bdstevel@tonic-gate#include <signal.h>
627c478bdstevel@tonic-gate#include <setjmp.h>
637c478bdstevel@tonic-gate#include <limits.h>
647c478bdstevel@tonic-gate#include <zone.h>
65afee3dcRobert Mustacchi#include <thread.h>
66afee3dcRobert Mustacchi#include <synch.h>
677c478bdstevel@tonic-gate
687c478bdstevel@tonic-gate#include <priv_utils.h>
697c478bdstevel@tonic-gate
700406ceameem#include <libinetutil.h>
717c478bdstevel@tonic-gate#include "traceroute.h"
727c478bdstevel@tonic-gate
737c478bdstevel@tonic-gate#define	MAX_SEQ			65535	/* max sequence value for ICMP */
747c478bdstevel@tonic-gate#define	MAX_TRAFFIC_CLASS	255	/* max traffic class for IPv6 */
757c478bdstevel@tonic-gate#define	MAX_FLOW_LABEL		0xFFFFF	/* max flow label for IPv6 */
767c478bdstevel@tonic-gate#define	MAX_TOS			255	/* max type-of-service for IPv4 */
777c478bdstevel@tonic-gate#define	STR_LEN			30
787c478bdstevel@tonic-gate
797c478bdstevel@tonic-gate/* store the information about a host */
807c478bdstevel@tonic-gatestruct hostinfo {
817c478bdstevel@tonic-gate	char *name;		/* hostname */
827c478bdstevel@tonic-gate	int family;		/* address family of the IP addresses */
837c478bdstevel@tonic-gate	int num_addr;			/* number of IP addresses */
847c478bdstevel@tonic-gate	union any_in_addr *addrs;	/* list of IP addresses */
857c478bdstevel@tonic-gate};
867c478bdstevel@tonic-gate
877c478bdstevel@tonic-gate/* used to store a bunch of protocol specific values */
887c478bdstevel@tonic-gatestruct pr_set {
897c478bdstevel@tonic-gate	int family;		/* AF_INET or AF_INET6 */
907c478bdstevel@tonic-gate	char name[STR_LEN];	/* "IPv4" or "IPv6" */
917c478bdstevel@tonic-gate	char icmp[STR_LEN];	/* "icmp" or "ipv6-icmp" */
927c478bdstevel@tonic-gate	int icmp_minlen;
937c478bdstevel@tonic-gate	int addr_len;
947c478bdstevel@tonic-gate	int ip_hdr_len;
957c478bdstevel@tonic-gate	int packlen;
967c478bdstevel@tonic-gate	int sock_size;		/* size of sockaddr_in or sockaddr_in6 */
977c478bdstevel@tonic-gate	struct sockaddr *to;
987c478bdstevel@tonic-gate	struct sockaddr *from;
997c478bdstevel@tonic-gate	void *from_sin_addr;
1007c478bdstevel@tonic-gate	union any_in_addr *gwIPlist;
1017c478bdstevel@tonic-gate	/* pointers to v4/v6 functions */
1027c478bdstevel@tonic-gate	struct ip *(*set_buffers_fn) (int);
1037c478bdstevel@tonic-gate	int (*check_reply_fn)(struct msghdr *, int, int, uchar_t *, uchar_t *);
1047c478bdstevel@tonic-gate	boolean_t (*print_icmp_other_fn)(uchar_t, uchar_t);
1057c478bdstevel@tonic-gate	void (*print_addr_fn)(uchar_t *, int, struct sockaddr *);
1067c478bdstevel@tonic-gate
1077c478bdstevel@tonic-gate};
1087c478bdstevel@tonic-gate
1097c478bdstevel@tonic-gate/*
1107c478bdstevel@tonic-gate * LBNL bug fixed: in LBNL traceroute 'uchar_t packet[512];'
1117c478bdstevel@tonic-gate * Not sufficient to hold the complete packet for ECHO REPLY of a big probe.
1127c478bdstevel@tonic-gate * Packet size is reported incorrectly in such a case.
1137c478bdstevel@tonic-gate * Also this buffer needs to be 32 bit aligned. In the future the alignment
1147c478bdstevel@tonic-gate * requirement will be increased to 64 bit. So, let's use 64 bit alignment now.
1157c478bdstevel@tonic-gate */
1167c478bdstevel@tonic-gatestatic uint64_t packet[(IP_MAXPACKET + 1)/8];	/* received packet */
1177c478bdstevel@tonic-gate
1187c478bdstevel@tonic-gatestatic struct ip *outip4;	/* output buffer to send as an IPv4 datagram */
1197c478bdstevel@tonic-gatestatic struct ip *outip6;	/* output buffer to send as an IPv6 datagram */
1207c478bdstevel@tonic-gate
1217c478bdstevel@tonic-gate/* Used to store the ancillary data that comes with the received packets */
1227c478bdstevel@tonic-gatestatic uint64_t ancillary_data[(IP_MAXPACKET + 1)/8];
1237c478bdstevel@tonic-gate
1247c478bdstevel@tonic-gate/* first get the gw names, later you'll resolve them based on the family */
1257c478bdstevel@tonic-gatestatic char *gwlist[MAXMAX_GWS];		/* gateway names list */
1267c478bdstevel@tonic-gatestatic union any_in_addr gwIPlist[MAX_GWS];	/* gateway IPv4 address list */
1277c478bdstevel@tonic-gatestatic union any_in_addr gwIP6list[MAX_GWS6];	/* gateway IPv6 address list */
1287c478bdstevel@tonic-gate
1297c478bdstevel@tonic-gatestatic int family_input = AF_UNSPEC;	/* User supplied protocol family */
1307c478bdstevel@tonic-gatestatic int rcvsock4;		/* receive (icmp) socket file descriptor */
1317c478bdstevel@tonic-gatestatic int sndsock4;		/* send (udp/icmp) socket file descriptor */
1327c478bdstevel@tonic-gatestatic int rcvsock6;		/* receive (icmp6) socket file descriptor */
1337c478bdstevel@tonic-gatestatic int sndsock6;		/* send (udp6/icmp6) socket file descriptor */
1347c478bdstevel@tonic-gateint gw_count = 0;		/* number of gateways */
1357c478bdstevel@tonic-gatestatic struct sockaddr_in whereto;	/* Who to try to reach */
1367c478bdstevel@tonic-gatestatic struct sockaddr_in6 whereto6;
1377c478bdstevel@tonic-gatestatic struct sockaddr_in wherefrom;	/* Who we are */
1387c478bdstevel@tonic-gatestatic struct sockaddr_in6 wherefrom6;
1397c478bdstevel@tonic-gatestatic int packlen_input = 0;		/* user input for packlen */
1407c478bdstevel@tonic-gate
1417c478bdstevel@tonic-gatechar *prog;
1427c478bdstevel@tonic-gatestatic char *source_input = NULL; /* this is user arg. source, doesn't change */
1437c478bdstevel@tonic-gatestatic char *source = NULL;	/* this gets modified after name lookup */
1447c478bdstevel@tonic-gatechar *hostname;
1457c478bdstevel@tonic-gatestatic char *device = NULL;   	/* interface name */
1467c478bdstevel@tonic-gatestatic struct pr_set *pr4;	/* protocol info for IPv4 */
1477c478bdstevel@tonic-gatestatic struct pr_set *pr6;	/* protocol info for IPv6 */
1487c478bdstevel@tonic-gatestatic struct ifaddrlist *al4;	/* list of interfaces */
1497c478bdstevel@tonic-gatestatic struct ifaddrlist *al6;	/* list of interfaces */
1507c478bdstevel@tonic-gatestatic uint_t if_index = 0;	/* interface index */
1517c478bdstevel@tonic-gatestatic int num_v4 = 0;		/* count of IPv4 addresses */
1527c478bdstevel@tonic-gatestatic int num_v6 = 0;		/* count of IPv6 addresses */
1537c478bdstevel@tonic-gatestatic int num_ifs4 = 0;	/* count of local IPv4 interfaces */
1547c478bdstevel@tonic-gatestatic int num_ifs6 = 0;	/* count of local IPv6 interfaces */
1557c478bdstevel@tonic-gate
1567c478bdstevel@tonic-gatestatic int nprobes = 3;		/* number of probes */
1577c478bdstevel@tonic-gatestatic int max_ttl = 30;	/* max number of hops */
1587c478bdstevel@tonic-gatestatic int first_ttl = 1;	/* initial number of hops */
1597c478bdstevel@tonic-gateushort_t ident;			/* used to authenticate replies */
1607c478bdstevel@tonic-gateushort_t port = 32768 + 666;	/* start udp dest port # for probe packets */
1617c478bdstevel@tonic-gate
1627c478bdstevel@tonic-gatestatic int options = 0;		/* socket options */
1637c478bdstevel@tonic-gateboolean_t verbose = _B_FALSE;	/* verbose output */
1647c478bdstevel@tonic-gatestatic int waittime = 5;	/* time to wait for response (in seconds) */
1657c478bdstevel@tonic-gatestatic struct timeval delay = {0, 0}; /* delay between consecutive probe */
1667c478bdstevel@tonic-gateboolean_t nflag = _B_FALSE;	/* print addresses numerically */
1677c478bdstevel@tonic-gatestatic boolean_t showttl = _B_FALSE; /* print the ttl(hop limit) of recvd pkt */
1687c478bdstevel@tonic-gateboolean_t useicmp = _B_FALSE;  	/* use icmp echo instead of udp packets */
1697c478bdstevel@tonic-gateboolean_t docksum = _B_TRUE;	/* calculate checksums */
1707c478bdstevel@tonic-gatestatic boolean_t collect_stat = _B_FALSE;	/* print statistics */
1717c478bdstevel@tonic-gateboolean_t settos = _B_FALSE;   	/* set type-of-service field */
172bd670b3Erik Nordmarkint dontfrag = 0;		/* IP*_DONTFRAG */
1737c478bdstevel@tonic-gatestatic int max_timeout = 5;	/* quit after this consecutive timeouts */
1747c478bdstevel@tonic-gatestatic boolean_t probe_all = _B_FALSE;	/* probe all the IFs of the target */
1757c478bdstevel@tonic-gatestatic boolean_t pick_src = _B_FALSE;	/* traceroute picks the src address */
1767c478bdstevel@tonic-gate
1777c478bdstevel@tonic-gate/*
1787c478bdstevel@tonic-gate * flow and class are specific to IPv6, tos and off are specific to IPv4.
1797c478bdstevel@tonic-gate * Each protocol uses the ones that are specific to itself, and ignores
1807c478bdstevel@tonic-gate * others.
1817c478bdstevel@tonic-gate */
1827c478bdstevel@tonic-gatestatic uint_t flow = 0;		/* IPv6 flow info */
1837c478bdstevel@tonic-gatestatic uint_t class = 0;	/* IPv6 class */
1847c478bdstevel@tonic-gateuchar_t tos = 0;		/* IPv4 type-of-service */
1857c478bdstevel@tonic-gateushort_t off = 0;		/* set DF bit */
1867c478bdstevel@tonic-gate
1877c478bdstevel@tonic-gatestatic jmp_buf env;		/* stack environment for longjmp() */
1887c478bdstevel@tonic-gateboolean_t raw_req;		/* if sndsock for IPv4 must be raw */
1897c478bdstevel@tonic-gate
190afee3dcRobert Mustacchi/*
191afee3dcRobert Mustacchi * Name service lookup related data.
192afee3dcRobert Mustacchi */
193afee3dcRobert Mustacchistatic mutex_t tr_nslock = ERRORCHECKMUTEX;
194afee3dcRobert Mustacchistatic boolean_t tr_nsactive = _B_FALSE;	/* Lookup ongoing */
195afee3dcRobert Mustacchistatic hrtime_t tr_nsstarttime;			/* Start time */
196afee3dcRobert Mustacchistatic int tr_nssleeptime = 2;			/* Interval between checks */
197afee3dcRobert Mustacchistatic int tr_nswarntime = 2;			/* Interval to warn after */
198afee3dcRobert Mustacchi
1997c478bdstevel@tonic-gate/* Forwards */
2007c478bdstevel@tonic-gatestatic uint_t calc_packetlen(int, struct pr_set *);
2017c478bdstevel@tonic-gateextern int check_reply(struct msghdr *, int, int, uchar_t *, uchar_t *);
2027c478bdstevel@tonic-gateextern int check_reply6(struct msghdr *, int, int, uchar_t *, uchar_t *);
2037c478bdstevel@tonic-gatestatic double deltaT(struct timeval *, struct timeval *);
2047c478bdstevel@tonic-gatestatic char *device_name(struct ifaddrlist *, int, union any_in_addr *,
2057c478bdstevel@tonic-gate    struct pr_set *);
2067c478bdstevel@tonic-gateextern void *find_ancillary_data(struct msghdr *, int, int);
2077c478bdstevel@tonic-gatestatic boolean_t has_addr(struct addrinfo *, union any_in_addr *);
2087c478bdstevel@tonic-gatestatic struct ifaddrlist *find_device(struct ifaddrlist *, int, char *);
2097c478bdstevel@tonic-gatestatic struct ifaddrlist *find_ifaddr(struct ifaddrlist *, int,
2107c478bdstevel@tonic-gate    union any_in_addr *, int);
2117c478bdstevel@tonic-gatestatic void get_gwaddrs(char **, int, union any_in_addr *,
2127c478bdstevel@tonic-gate    union any_in_addr *, int *, int *);
2137c478bdstevel@tonic-gatestatic void get_hostinfo(char *, int, struct addrinfo **);
2147c478bdstevel@tonic-gatechar *inet_name(union any_in_addr *, int);
2157c478bdstevel@tonic-gateushort_t in_cksum(ushort_t *, int);
2167c478bdstevel@tonic-gateextern int ip_hdr_length_v6(ip6_t *, int, uint8_t *);
2177c478bdstevel@tonic-gateextern char *pr_type(uchar_t);
2187c478bdstevel@tonic-gateextern char *pr_type6(uchar_t);
2197c478bdstevel@tonic-gateextern void print_addr(uchar_t *, int, struct sockaddr *);
2207c478bdstevel@tonic-gateextern void print_addr6(uchar_t *, int, struct sockaddr *);
2217c478bdstevel@tonic-gateextern boolean_t print_icmp_other(uchar_t, uchar_t);
2227c478bdstevel@tonic-gateextern boolean_t print_icmp_other6(uchar_t, uchar_t);
2237c478bdstevel@tonic-gatestatic void print_stats(int, int, double, double, double, double);
2247c478bdstevel@tonic-gatestatic void print_unknown_host_msg(const char *, const char *);
2257c478bdstevel@tonic-gatestatic void record_stats(double, int *, double *, double *, double *, double *);
2267c478bdstevel@tonic-gatestatic void resolve_nodes(int *, struct addrinfo **);
2277c478bdstevel@tonic-gatestatic void select_src_addr(union any_in_addr *, union any_in_addr *, int);
2287c478bdstevel@tonic-gateextern void send_probe(int, struct sockaddr *, struct ip *, int, int,
2297c478bdstevel@tonic-gate    struct timeval *, int);
2307c478bdstevel@tonic-gateextern void send_probe6(int, struct msghdr *, struct ip *, int, int,
2317c478bdstevel@tonic-gate    struct timeval *, int);
2327c478bdstevel@tonic-gateextern void set_ancillary_data(struct msghdr *, int, union any_in_addr *, int,
2337c478bdstevel@tonic-gate    uint_t);
2347c478bdstevel@tonic-gateextern struct ip *set_buffers(int);
2357c478bdstevel@tonic-gateextern struct ip *set_buffers6(int);
2367c478bdstevel@tonic-gateextern void set_IPv4opt_sourcerouting(int, union any_in_addr *,
2377c478bdstevel@tonic-gate    union any_in_addr *);
2387c478bdstevel@tonic-gatestatic void set_sin(struct sockaddr *, union any_in_addr *, int);
2397c478bdstevel@tonic-gatestatic int set_src_addr(struct pr_set *, struct ifaddrlist **);
2407c478bdstevel@tonic-gatestatic void setup_protocol(struct pr_set *, int);
2417c478bdstevel@tonic-gatestatic void setup_socket(struct pr_set *, int);
2427c478bdstevel@tonic-gatestatic void sig_handler(int);
2437c478bdstevel@tonic-gatestatic int str2int(const char *, const char *, int, int);
2447c478bdstevel@tonic-gatestatic double str2dbl(const char *, const char *, double, double);
2457c478bdstevel@tonic-gatestatic void trace_it(struct addrinfo *);
2467c478bdstevel@tonic-gatestatic void traceroute(union any_in_addr *, struct msghdr *, struct pr_set *,
2477c478bdstevel@tonic-gate    int, struct ifaddrlist *);
2487c478bdstevel@tonic-gatestatic void tv_sub(struct timeval *, struct timeval *);
2497c478bdstevel@tonic-gatestatic void usage(void);
2507c478bdstevel@tonic-gatestatic int wait_for_reply(int, struct msghdr *, struct timeval *);
2517c478bdstevel@tonic-gatestatic double xsqrt(double);
252afee3dcRobert Mustacchistatic void *ns_warning_thr(void *);
2537c478bdstevel@tonic-gate
2547c478bdstevel@tonic-gate/*
2557c478bdstevel@tonic-gate * main
2567c478bdstevel@tonic-gate */
2578c332a0jaint
2587c478bdstevel@tonic-gatemain(int argc, char **argv)
2597c478bdstevel@tonic-gate{
2607c478bdstevel@tonic-gate	struct addrinfo *ai_dst = NULL;		/* destination host */
2617c478bdstevel@tonic-gate	/*
2627c478bdstevel@tonic-gate	 * "probing_successful" indicates if we could successfully send probes,
2637c478bdstevel@tonic-gate	 * not necessarily received reply from the target (this behavior is from
2647c478bdstevel@tonic-gate	 * the original traceroute). It's _B_FALSE if packlen is invalid, or no
2657c478bdstevel@tonic-gate	 * interfaces found.
2667c478bdstevel@tonic-gate	 */
2677c478bdstevel@tonic-gate	boolean_t probing_successful = _B_FALSE;
2687c478bdstevel@tonic-gate	int longjmp_return;			/* return value from longjump */
2697c478bdstevel@tonic-gate	int i = 0;
2707c478bdstevel@tonic-gate	char *cp;
2717c478bdstevel@tonic-gate	int op;
2727c478bdstevel@tonic-gate	char *ep;
2737c478bdstevel@tonic-gate	char temp_buf[INET6_ADDRSTRLEN];	/* use for inet_ntop() */
2747c478bdstevel@tonic-gate	double pause;
2757c478bdstevel@tonic-gate
2767c478bdstevel@tonic-gate	/*
2777c478bdstevel@tonic-gate	 * A raw socket will be used for IPv4 if there is sufficient
2787c478bdstevel@tonic-gate	 * privilege.
2797c478bdstevel@tonic-gate	 */
2807c478bdstevel@tonic-gate	raw_req = priv_ineffect(PRIV_NET_RAWACCESS);
2817c478bdstevel@tonic-gate
2827c478bdstevel@tonic-gate	/*
2837c478bdstevel@tonic-gate	 * We'll need the privilege only when we open the sockets; that's
2847c478bdstevel@tonic-gate	 * when we'll fail if the program has insufficient privileges.
2857c478bdstevel@tonic-gate	 */
2867c478bdstevel@tonic-gate	(void) __init_suid_priv(PU_CLEARLIMITSET, PRIV_NET_ICMPACCESS,
2877c478bdstevel@tonic-gate	    raw_req ? PRIV_NET_RAWACCESS : NULL, NULL);
2887c478bdstevel@tonic-gate
2897c478bdstevel@tonic-gate	(void) setlinebuf(stdout);
2907c478bdstevel@tonic-gate
2917c478bdstevel@tonic-gate	if ((cp = strrchr(argv[0], '/')) != NULL)
2927c478bdstevel@tonic-gate		prog = cp + 1;
2937c478bdstevel@tonic-gate	else
2947c478bdstevel@tonic-gate		prog = argv[0];
2957c478bdstevel@tonic-gate
2967c478bdstevel@tonic-gate	opterr = 0;
2977c478bdstevel@tonic-gate	while ((op = getopt(argc, argv, "adFIlnrSvxA:c:f:g:i:L:m:P:p:Q:q:s:"
2987c478bdstevel@tonic-gate	    "t:w:")) != EOF) {
2997c478bdstevel@tonic-gate		switch (op) {
3007c478bdstevel@tonic-gate		case 'A':
3017c478bdstevel@tonic-gate			if (strcmp(optarg, "inet") == 0) {
3027c478bdstevel@tonic-gate				family_input = AF_INET;
3037c478bdstevel@tonic-gate			} else if (strcmp(optarg, "inet6") == 0) {
3047c478bdstevel@tonic-gate				family_input = AF_INET6;
3057c478bdstevel@tonic-gate			} else {
3067c478bdstevel@tonic-gate				Fprintf(stderr,
3077c478bdstevel@tonic-gate				    "%s: unknown address family %s\n",
3087c478bdstevel@tonic-gate				    prog, optarg);
3097c478bdstevel@tonic-gate				exit(EXIT_FAILURE);
3107c478bdstevel@tonic-gate			}
3117c478bdstevel@tonic-gate			break;
3127c478bdstevel@tonic-gate
3137c478bdstevel@tonic-gate		case 'a':
3147c478bdstevel@tonic-gate			probe_all = _B_TRUE;
3157c478bdstevel@tonic-gate			break;
3167c478bdstevel@tonic-gate
3177c478bdstevel@tonic-gate		case 'c':
3187c478bdstevel@tonic-gate			class = str2int(optarg, "traffic class", 0,
3197c478bdstevel@tonic-gate			    MAX_TRAFFIC_CLASS);
3207c478bdstevel@tonic-gate			break;
3217c478bdstevel@tonic-gate
3227c478bdstevel@tonic-gate		case 'd':
3237c478bdstevel@tonic-gate			options |= SO_DEBUG;
3247c478bdstevel@tonic-gate			break;
3257c478bdstevel@tonic-gate
3267c478bdstevel@tonic-gate		case 'f':
3277c478bdstevel@tonic-gate			first_ttl = str2int(optarg, "first ttl", 1, MAXTTL);
3287c478bdstevel@tonic-gate			break;
3297c478bdstevel@tonic-gate
3307c478bdstevel@tonic-gate		case 'F':
3317c478bdstevel@tonic-gate			off = IP_DF;
332bd670b3Erik Nordmark			dontfrag = 1;
3337c478bdstevel@tonic-gate			break;
3347c478bdstevel@tonic-gate
3357c478bdstevel@tonic-gate		case 'g':
3367c478bdstevel@tonic-gate			if (!raw_req) {
3377c478bdstevel@tonic-gate				Fprintf(stderr,
3387c478bdstevel@tonic-gate				    "%s: privilege to specify a loose source "
3397c478bdstevel@tonic-gate				    "route gateway is unavailable\n",
3407c478bdstevel@tonic-gate				    prog);
3417c478bdstevel@tonic-gate				exit(EXIT_FAILURE);
3427c478bdstevel@tonic-gate			}
3437d6fbbeblu			if (gw_count >= MAXMAX_GWS) {
3447c478bdstevel@tonic-gate				Fprintf(stderr,
3457c478bdstevel@tonic-gate				    "%s: Too many gateways\n", prog);
3467c478bdstevel@tonic-gate				exit(EXIT_FAILURE);
3477c478bdstevel@tonic-gate			}
3487c478bdstevel@tonic-gate			gwlist[gw_count] = strdup(optarg);
3497c478bdstevel@tonic-gate			if (gwlist[gw_count] == NULL) {
3507c478bdstevel@tonic-gate				Fprintf(stderr, "%s: strdup %s\n", prog,
3517c478bdstevel@tonic-gate				    strerror(errno));
3527c478bdstevel@tonic-gate				exit(EXIT_FAILURE);
3537c478bdstevel@tonic-gate			}
3547c478bdstevel@tonic-gate
3557c478bdstevel@tonic-gate			++gw_count;
3567c478bdstevel@tonic-gate			break;
3577c478bdstevel@tonic-gate
3587c478bdstevel@tonic-gate		case 'l':
3597c478bdstevel@tonic-gate			showttl = _B_TRUE;
3607c478bdstevel@tonic-gate			break;
3617c478bdstevel@tonic-gate
3627c478bdstevel@tonic-gate		case 'i':
3637c478bdstevel@tonic-gate			/* this can be IF name or IF index */
3647c478bdstevel@tonic-gate			if_index = (uint_t)strtol(optarg, &ep, 10);
3657c478bdstevel@tonic-gate
3667c478bdstevel@tonic-gate			/* convert IF index <-->  IF name */
3677c478bdstevel@tonic-gate			if (errno != 0 || *ep != '\0') {
3687c478bdstevel@tonic-gate				device = optarg;
3697c478bdstevel@tonic-gate				if_index = if_nametoindex((const char *)device);
3707c478bdstevel@tonic-gate
3717c478bdstevel@tonic-gate				/*
3727c478bdstevel@tonic-gate				 * In case it fails, check to see if the problem
3737c478bdstevel@tonic-gate				 * is other than "IF not found".
3747c478bdstevel@tonic-gate				 */
3757c478bdstevel@tonic-gate				if (if_index == 0 && errno != ENXIO) {
3767c478bdstevel@tonic-gate					Fprintf(stderr, "%s: if_nametoindex:"
3777c478bdstevel@tonic-gate					    "%s\n", prog, strerror(errno));
3787c478bdstevel@tonic-gate					exit(EXIT_FAILURE);
3797c478bdstevel@tonic-gate				}
3807c478bdstevel@tonic-gate			} else {
3817c478bdstevel@tonic-gate				device = (char *)malloc(LIFNAMSIZ + 1);
3827c478bdstevel@tonic-gate				if (device == NULL) {
3837c478bdstevel@tonic-gate					Fprintf(stderr, "%s: malloc: %s\n",
3847c478bdstevel@tonic-gate					    prog, strerror(errno));
3857c478bdstevel@tonic-gate					exit(EXIT_FAILURE);
3867c478bdstevel@tonic-gate				}
3877c478bdstevel@tonic-gate
3887c478bdstevel@tonic-gate				device = if_indextoname(if_index, device);
3897c478bdstevel@tonic-gate				if (device != NULL) {
3907c478bdstevel@tonic-gate					device[LIFNAMSIZ] = '\0';
3917c478bdstevel@tonic-gate				} else if (errno != ENXIO) {
3927c478bdstevel@tonic-gate					/*
3937c478bdstevel@tonic-gate					 * The problem was other than "index
3947c478bdstevel@tonic-gate					 * not found".
3957c478bdstevel@tonic-gate					 */
3967c478bdstevel@tonic-gate					Fprintf(stderr, "%s: if_indextoname:"
3977c478bdstevel@tonic-gate					    "%s\n", prog, strerror(errno));
3987c478bdstevel@tonic-gate					exit(EXIT_FAILURE);
3997c478bdstevel@tonic-gate				}
4007c478bdstevel@tonic-gate			}
4017c478bdstevel@tonic-gate
4027c478bdstevel@tonic-gate			if (device == NULL || if_index == 0) {
4037c478bdstevel@tonic-gate				Fprintf(stderr, "%s: interface %s "
4047c478bdstevel@tonic-gate				    "doesn't match any actual interfaces\n",
4057c478bdstevel@tonic-gate				    prog, optarg);
4067c478bdstevel@tonic-gate				exit(EXIT_FAILURE);
4077c478bdstevel@tonic-gate			}
4087c478bdstevel@tonic-gate			break;
4097c478bdstevel@tonic-gate
4107c478bdstevel@tonic-gate		case 'I':
4117c478bdstevel@tonic-gate			useicmp = _B_TRUE;
4127c478bdstevel@tonic-gate			break;
4137c478bdstevel@tonic-gate
4147c478bdstevel@tonic-gate		case 'L':
4157c478bdstevel@tonic-gate			flow = str2int(optarg, "flow label", 0, MAX_FLOW_LABEL);
4167c478bdstevel@tonic-gate			break;
4177c478bdstevel@tonic-gate
4187c478bdstevel@tonic-gate		case 'm':
4197c478bdstevel@tonic-gate			max_ttl = str2int(optarg, "max ttl(hop limit)", 1,
4207c478bdstevel@tonic-gate			    MAXTTL);
4217c478bdstevel@tonic-gate			break;
4227c478bdstevel@tonic-gate
4237c478bdstevel@tonic-gate		case 'n':
4247c478bdstevel@tonic-gate			nflag = _B_TRUE;
4257c478bdstevel@tonic-gate			break;
4267c478bdstevel@tonic-gate
4277c478bdstevel@tonic-gate		case 'P':
4287c478bdstevel@tonic-gate			pause = str2dbl(optarg, "pause", 0, INT_MAX);
4297c478bdstevel@tonic-gate			delay.tv_sec = (time_t)pause;
4307c478bdstevel@tonic-gate			delay.tv_usec = (suseconds_t)((pause - delay.tv_sec) *
4317c478bdstevel@tonic-gate			    1000000);
4327c478bdstevel@tonic-gate			break;
4337c478bdstevel@tonic-gate
4347c478bdstevel@tonic-gate		case 'p':
4357c478bdstevel@tonic-gate			port = str2int(optarg, "port", 1, MAX_PORT);
4367c478bdstevel@tonic-gate			break;
4377c478bdstevel@tonic-gate
4387c478bdstevel@tonic-gate		case 'Q':
4397c478bdstevel@tonic-gate			max_timeout = str2int(optarg, "max timeout", 1, -1);
4407c478bdstevel@tonic-gate			break;
4417c478bdstevel@tonic-gate
4427c478bdstevel@tonic-gate		case 'q':
4437c478bdstevel@tonic-gate			nprobes = str2int(optarg, "nprobes", 1, -1);
4447c478bdstevel@tonic-gate			break;
4457c478bdstevel@tonic-gate
4467c478bdstevel@tonic-gate		case 'r':
4477c478bdstevel@tonic-gate			options |= SO_DONTROUTE;
4487c478bdstevel@tonic-gate			break;
4497c478bdstevel@tonic-gate
4507c478bdstevel@tonic-gate		case 'S':
4517c478bdstevel@tonic-gate			collect_stat = _B_TRUE;
4527c478bdstevel@tonic-gate			break;
4537c478bdstevel@tonic-gate
4547c478bdstevel@tonic-gate		case 's':
4557c478bdstevel@tonic-gate			/*
4567c478bdstevel@tonic-gate			 * set the ip source address of the outbound
4577c478bdstevel@tonic-gate			 * probe (e.g., on a multi-homed host).
4587c478bdstevel@tonic-gate			 */
4597c478bdstevel@tonic-gate			source_input = optarg;
4607c478bdstevel@tonic-gate			break;
4617c478bdstevel@tonic-gate
4627c478bdstevel@tonic-gate		case 't':
4637c478bdstevel@tonic-gate			tos = (uchar_t)str2int(optarg, "tos", 0, MAX_TOS);
4647c478bdstevel@tonic-gate			settos = _B_TRUE;
4657c478bdstevel@tonic-gate			break;
4667c478bdstevel@tonic-gate
4677c478bdstevel@tonic-gate		case 'v':
4687c478bdstevel@tonic-gate			verbose = _B_TRUE;
4697c478bdstevel@tonic-gate			break;
4707c478bdstevel@tonic-gate
4717c478bdstevel@tonic-gate		case 'x':
4727c478bdstevel@tonic-gate			docksum = _B_FALSE;
4737c478bdstevel@tonic-gate			break;
4747c478bdstevel@tonic-gate
4757c478bdstevel@tonic-gate		case 'w':
4767c478bdstevel@tonic-gate			waittime = str2int(optarg, "wait time", 2, -1);
4777c478bdstevel@tonic-gate			break;
4787c478bdstevel@tonic-gate
4797c478bdstevel@tonic-gate		default:
4807c478bdstevel@tonic-gate			usage();
4817c478bdstevel@tonic-gate			break;
4827c478bdstevel@tonic-gate		}
4837c478bdstevel@tonic-gate	}
4847c478bdstevel@tonic-gate
4857c478bdstevel@tonic-gate	/*
4867c478bdstevel@tonic-gate	 * If it's probe_all, SIGQUIT makes traceroute exit(). But we set the
4877c478bdstevel@tonic-gate	 * address to jump back to in traceroute(). Until then, we'll need to
4887c478bdstevel@tonic-gate	 * temporarily specify one.
4897c478bdstevel@tonic-gate	 */
4907c478bdstevel@tonic-gate	if (probe_all) {
4917c478bdstevel@tonic-gate		if ((longjmp_return = setjmp(env)) != 0) {
4927c478bdstevel@tonic-gate			if (longjmp_return == SIGQUIT) {
4937c478bdstevel@tonic-gate				Printf("(exiting)\n");
4947c478bdstevel@tonic-gate				exit(EXIT_SUCCESS);
4957c478bdstevel@tonic-gate			} else {		/* should never happen */
4967c478bdstevel@tonic-gate				exit(EXIT_FAILURE);
4977c478bdstevel@tonic-gate			}
4987c478bdstevel@tonic-gate		}
4997c478bdstevel@tonic-gate		(void) signal(SIGQUIT, sig_handler);
5007c478bdstevel@tonic-gate	}
5017c478bdstevel@tonic-gate
5027c478bdstevel@tonic-gate	if ((gw_count > 0) && (options & SO_DONTROUTE)) {
5037c478bdstevel@tonic-gate		Fprintf(stderr, "%s: loose source route gateways (-g)"
5047c478bdstevel@tonic-gate		    " cannot be specified when probe packets are sent"
5057c478bdstevel@tonic-gate		    " directly to a host on an attached network (-r)\n",
5067c478bdstevel@tonic-gate		    prog);
5077c478bdstevel@tonic-gate		exit(EXIT_FAILURE);
5087c478bdstevel@tonic-gate	}
5097c478bdstevel@tonic-gate
5107c478bdstevel@tonic-gate	i = argc - optind;
5117c478bdstevel@tonic-gate	if (i == 1 || i == 2) {
5127c478bdstevel@tonic-gate		hostname = argv[optind];
5137c478bdstevel@tonic-gate
5147c478bdstevel@tonic-gate		if (i == 2) {
5157c478bdstevel@tonic-gate			/* accept any length now, we'll check it later */
5167c478bdstevel@tonic-gate			packlen_input = str2int(argv[optind + 1],
5177c478bdstevel@tonic-gate			    "packet length", 0, -1);
5187c478bdstevel@tonic-gate		}
5197c478bdstevel@tonic-gate	} else {
5207c478bdstevel@tonic-gate		usage();
5217c478bdstevel@tonic-gate	}
5227c478bdstevel@tonic-gate
5237c478bdstevel@tonic-gate	if (first_ttl > max_ttl) {
5247c478bdstevel@tonic-gate		Fprintf(stderr,
5257c478bdstevel@tonic-gate		    "%s: first ttl(hop limit) (%d) may not be greater"
5267c478bdstevel@tonic-gate		    " than max ttl(hop limit) (%d)\n",
5277c478bdstevel@tonic-gate		    prog, first_ttl, max_ttl);
5287c478bdstevel@tonic-gate		exit(EXIT_FAILURE);
5297c478bdstevel@tonic-gate	}
5307c478bdstevel@tonic-gate
531afee3dcRobert Mustacchi	/*
532afee3dcRobert Mustacchi	 * Start up the name services warning thread.
533afee3dcRobert Mustacchi	 */
534afee3dcRobert Mustacchi	if (thr_create(NULL, 0, ns_warning_thr, NULL,
535afee3dcRobert Mustacchi	    THR_DETACHED | THR_DAEMON, NULL) != 0) {
536afee3dcRobert Mustacchi		Fprintf(stderr, "%s: failed to create name services "
537afee3dcRobert Mustacchi		    "thread: %s\n", prog, strerror(errno));
538afee3dcRobert Mustacchi		exit(EXIT_FAILURE);
539afee3dcRobert Mustacchi	}
540afee3dcRobert Mustacchi
541afee3dcRobert Mustacchi
5427c478bdstevel@tonic-gate	/* resolve hostnames */
5437c478bdstevel@tonic-gate	resolve_nodes(&family_input, &ai_dst);
5447c478bdstevel@tonic-gate	if (ai_dst == NULL) {
5457c478bdstevel@tonic-gate		exit(EXIT_FAILURE);
5467c478bdstevel@tonic-gate	}
5477c478bdstevel@tonic-gate
5487c478bdstevel@tonic-gate	/*
5497c478bdstevel@tonic-gate	 * If it's probe_all, SIGINT makes traceroute skip to probing next IP
5507c478bdstevel@tonic-gate	 * address of the target. The new interrupt handler is assigned in
5517c478bdstevel@tonic-gate	 * traceroute() function. Until then let's ignore the signal.
5527c478bdstevel@tonic-gate	 */
5537c478bdstevel@tonic-gate	if (probe_all)
5547c478bdstevel@tonic-gate		(void) signal(SIGINT, SIG_IGN);
5557c478bdstevel@tonic-gate
5567c478bdstevel@tonic-gate	ident = (getpid() & 0xffff) | 0x8000;
5577c478bdstevel@tonic-gate
5587c478bdstevel@tonic-gate	/*
5597c478bdstevel@tonic-gate	 * We KNOW that probe_all == TRUE if family is AF_UNSPEC,
5607c478bdstevel@tonic-gate	 * since family is set to the specific AF found unless it's
5617c478bdstevel@tonic-gate	 * probe_all. So if family == AF_UNSPEC, we need to init pr4 and pr6.
5627c478bdstevel@tonic-gate	 */
5637c478bdstevel@tonic-gate	switch (family_input) {
5647c478bdstevel@tonic-gate	case AF_UNSPEC:
5657c478bdstevel@tonic-gate		pr4 = (struct pr_set *)malloc(sizeof (struct pr_set));
5667c478bdstevel@tonic-gate		if (pr4 == NULL) {
5677c478bdstevel@tonic-gate			Fprintf(stderr,
5687c478bdstevel@tonic-gate			    "%s: malloc %s\n", prog, strerror(errno));
5697c478bdstevel@tonic-gate			exit(EXIT_FAILURE);
5707c478bdstevel@tonic-gate		}
5717c478bdstevel@tonic-gate		pr6 = (struct pr_set *)malloc(sizeof (struct pr_set));
5727c478bdstevel@tonic-gate		if (pr6 == NULL) {
5737c478bdstevel@tonic-gate			Fprintf(stderr,
5747c478bdstevel@tonic-gate			    "%s: malloc %s\n", prog, strerror(errno));
5757c478bdstevel@tonic-gate			exit(EXIT_FAILURE);
5767c478bdstevel@tonic-gate		}
5777c478bdstevel@tonic-gate		setup_protocol(pr6, AF_INET6);
5787c478bdstevel@tonic-gate		setup_protocol(pr4, AF_INET);
5797c478bdstevel@tonic-gate		outip6 = (*pr6->set_buffers_fn)(pr6->packlen);
5807c478bdstevel@tonic-gate		setup_socket(pr6, pr6->packlen);
5817c478bdstevel@tonic-gate
5827c478bdstevel@tonic-gate		outip4 = (*pr4->set_buffers_fn)(pr4->packlen);
5837c478bdstevel@tonic-gate		setup_socket(pr4, pr4->packlen);
5847c478bdstevel@tonic-gate		num_ifs6 = set_src_addr(pr6, &al6);
5857c478bdstevel@tonic-gate		num_ifs4 = set_src_addr(pr4, &al4);
5867c478bdstevel@tonic-gate		break;
5877c478bdstevel@tonic-gate	case AF_INET6:
5887c478bdstevel@tonic-gate		pr6 = (struct pr_set *)malloc(sizeof (struct pr_set));
5897c478bdstevel@tonic-gate		if (pr6 == NULL) {
5907c478bdstevel@tonic-gate			Fprintf(stderr,
5917c478bdstevel@tonic-gate			    "%s: malloc %s\n", prog, strerror(errno));
5927c478bdstevel@tonic-gate			exit(EXIT_FAILURE);
5937c478bdstevel@tonic-gate		}
5947c478bdstevel@tonic-gate		setup_protocol(pr6, AF_INET6);
5957c478bdstevel@tonic-gate		outip6 = (*pr6->set_buffers_fn)(pr6->packlen);
5967c478bdstevel@tonic-gate		setup_socket(pr6, pr6->packlen);
5977c478bdstevel@tonic-gate		num_ifs6 = set_src_addr(pr6, &al6);
5987c478bdstevel@tonic-gate		break;
5997c478bdstevel@tonic-gate	case AF_INET:
6007c478bdstevel@tonic-gate		pr4 = (struct pr_set *)malloc(sizeof (struct pr_set));
6017c478bdstevel@tonic-gate		if (pr4 == NULL) {
6027c478bdstevel@tonic-gate			Fprintf(stderr,
6037c478bdstevel@tonic-gate			    "%s: malloc %s\n", prog, strerror(errno));
6047c478bdstevel@tonic-gate			exit(EXIT_FAILURE);
6057c478bdstevel@tonic-gate		}
6067c478bdstevel@tonic-gate		setup_protocol(pr4, AF_INET);
6077c478bdstevel@tonic-gate		outip4 = (*pr4->set_buffers_fn)(pr4->packlen);
6087c478bdstevel@tonic-gate		setup_socket(pr4, pr4->packlen);
6097c478bdstevel@tonic-gate		num_ifs4 = set_src_addr(pr4, &al4);
6107c478bdstevel@tonic-gate		break;
6117c478bdstevel@tonic-gate	default:
6127c478bdstevel@tonic-gate		Fprintf(stderr, "%s: unknow address family.\n", prog);
6137c478bdstevel@tonic-gate		exit(EXIT_FAILURE);
6147c478bdstevel@tonic-gate	}
6157c478bdstevel@tonic-gate
6167c478bdstevel@tonic-gate	if (num_v4 + num_v6 > 1 && !probe_all) {
6177c478bdstevel@tonic-gate		if (ai_dst->ai_family == AF_INET) {
6187c478bdstevel@tonic-gate			Fprintf(stderr,
6197c478bdstevel@tonic-gate			    "%s: Warning: %s has multiple addresses;"
6207c478bdstevel@tonic-gate			    " using %s\n", prog, hostname,
6217c478bdstevel@tonic-gate			    inet_ntop(AF_INET,
6227c478bdstevel@tonic-gate			    /* LINTED E_BAD_PTR_CAST_ALIGN */
6237c478bdstevel@tonic-gate			    (void *)&((struct sockaddr_in *)
6247c478bdstevel@tonic-gate			    ai_dst->ai_addr)->sin_addr,
6257c478bdstevel@tonic-gate			    temp_buf, sizeof (temp_buf)));
6267c478bdstevel@tonic-gate		} else {
6277c478bdstevel@tonic-gate			Fprintf(stderr,
6287c478bdstevel@tonic-gate			    "%s: Warning: %s has multiple addresses;"
6297c478bdstevel@tonic-gate			    " using %s\n", prog, hostname,
6307c478bdstevel@tonic-gate			    inet_ntop(AF_INET6,
6317c478bdstevel@tonic-gate			    /* LINTED E_BAD_PTR_CAST_ALIGN */
6327c478bdstevel@tonic-gate			    (void *)&((struct sockaddr_in6 *)
6337c478bdstevel@tonic-gate			    ai_dst->ai_addr)->sin6_addr,
6347c478bdstevel@tonic-gate			    temp_buf, sizeof (temp_buf)));
6357c478bdstevel@tonic-gate		}
6367c478bdstevel@tonic-gate	}
6377c478bdstevel@tonic-gate
6387c478bdstevel@tonic-gate	if (num_ifs4 + num_ifs6 > 0) {
6397c478bdstevel@tonic-gate		trace_it(ai_dst);
6407c478bdstevel@tonic-gate		probing_successful = _B_TRUE;
6417c478bdstevel@tonic-gate	}
6427c478bdstevel@tonic-gate
6437c478bdstevel@tonic-gate	(void) close(rcvsock4);
6447c478bdstevel@tonic-gate	(void) close(sndsock4);
6457c478bdstevel@tonic-gate	(void) close(rcvsock6);
6467c478bdstevel@tonic-gate	(void) close(sndsock6);
6477c478bdstevel@tonic-gate
6487c478bdstevel@tonic-gate	/*
6497c478bdstevel@tonic-gate	 * if we could probe any of the IP addresses of the target, that means
6507c478bdstevel@tonic-gate	 * this was a successful operation
6517c478bdstevel@tonic-gate	 */
6527c478bdstevel@tonic-gate	if (probing_successful)
6538c332a0ja		return (EXIT_SUCCESS);
6547c478bdstevel@tonic-gate	else
6558c332a0ja		return (EXIT_FAILURE);
6567c478bdstevel@tonic-gate}
6577c478bdstevel@tonic-gate
6587c478bdstevel@tonic-gate/*
6597c478bdstevel@tonic-gate * print "unknown host" message
6607c478bdstevel@tonic-gate */
6617c478bdstevel@tonic-gatestatic void
6627c478bdstevel@tonic-gateprint_unknown_host_msg(const char *protocol, const char *host)
6637c478bdstevel@tonic-gate{
6647c478bdstevel@tonic-gate	Fprintf(stderr, "%s: unknown%s host %s\n", prog, protocol, host);
6657c478bdstevel@tonic-gate}
6667c478bdstevel@tonic-gate
6677c478bdstevel@tonic-gate/*
6687c478bdstevel@tonic-gate * resolve destination host and gateways
6697c478bdstevel@tonic-gate */
6707c478bdstevel@tonic-gatestatic void
6717c478bdstevel@tonic-gateresolve_nodes(int *family, struct addrinfo **ai_dstp)
6727c478bdstevel@tonic-gate{
6737c478bdstevel@tonic-gate	struct addrinfo *ai_dst = NULL;
6747c478bdstevel@tonic-gate	struct addrinfo *aip = NULL;
6757c478bdstevel@tonic-gate	int num_resolved_gw = 0;
6767c478bdstevel@tonic-gate	int num_resolved_gw6 = 0;
6777c478bdstevel@tonic-gate
6787c478bdstevel@tonic-gate	get_hostinfo(hostname, *family, &ai_dst);
6797c478bdstevel@tonic-gate	if (ai_dst == NULL) {
6807c478bdstevel@tonic-gate		print_unknown_host_msg("", hostname);
6817c478bdstevel@tonic-gate		exit(EXIT_FAILURE);
6827c478bdstevel@tonic-gate	}
6837c478bdstevel@tonic-gate	/* Get a count of the v4 & v6 addresses */
6847c478bdstevel@tonic-gate	for (aip = ai_dst; aip != NULL; aip = aip->ai_next) {
6857c478bdstevel@tonic-gate		switch (aip->ai_family) {
6867c478bdstevel@tonic-gate		case AF_INET:
6877c478bdstevel@tonic-gate			num_v4++;
6887c478bdstevel@tonic-gate			break;
6897c478bdstevel@tonic-gate		case AF_INET6:
6907c478bdstevel@tonic-gate			num_v6++;
6917c478bdstevel@tonic-gate			break;
6927c478bdstevel@tonic-gate		}
6937c478bdstevel@tonic-gate	}
6947c478bdstevel@tonic-gate
6957c478bdstevel@tonic-gate	if (*family == AF_UNSPEC && !probe_all) {
6967c478bdstevel@tonic-gate		*family = ai_dst->ai_family;
6977c478bdstevel@tonic-gate	}
6987c478bdstevel@tonic-gate
6997c478bdstevel@tonic-gate	/* resolve gateways */
7007c478bdstevel@tonic-gate	if (gw_count > 0) {
7017c478bdstevel@tonic-gate		get_gwaddrs(gwlist, *family, gwIPlist, gwIP6list,
7027c478bdstevel@tonic-gate		    &num_resolved_gw, &num_resolved_gw6);
7037c478bdstevel@tonic-gate
7047c478bdstevel@tonic-gate		/* we couldn't resolve a gateway as an IPv6 host */
7057c478bdstevel@tonic-gate		if (num_resolved_gw6 != gw_count && num_v6 != 0) {
7067c478bdstevel@tonic-gate			if (*family == AF_INET6 || *family == AF_UNSPEC)
7077c478bdstevel@tonic-gate				print_unknown_host_msg(" IPv6",
7087c478bdstevel@tonic-gate				    gwlist[num_resolved_gw6]);
7097c478bdstevel@tonic-gate			num_v6 = 0;
7107c478bdstevel@tonic-gate		}
7117c478bdstevel@tonic-gate
7127c478bdstevel@tonic-gate		/* we couldn't resolve a gateway as an IPv4 host */
7137c478bdstevel@tonic-gate		if (num_resolved_gw != gw_count && num_v4 != 0) {
7147c478bdstevel@tonic-gate			if (*family == AF_INET || *family == AF_UNSPEC)
7157c478bdstevel@tonic-gate				print_unknown_host_msg(" IPv4",
7167c478bdstevel@tonic-gate				    gwlist[num_resolved_gw]);
7177c478bdstevel@tonic-gate			num_v4 = 0;
7187c478bdstevel@tonic-gate		}
7197c478bdstevel@tonic-gate	}
7207c478bdstevel@tonic-gate
7217d6fbbeblu	*ai_dstp = (num_v4 + num_v6 > 0) ? ai_dst : NULL;
7227c478bdstevel@tonic-gate}
7237c478bdstevel@tonic-gate
7247c478bdstevel@tonic-gate/*
7257c478bdstevel@tonic-gate * Given IP address or hostname, return v4 and v6 hostinfo lists.
7267c478bdstevel@tonic-gate * Assumes that hostinfo ** ptrs are non-null.
7277c478bdstevel@tonic-gate */
7287c478bdstevel@tonic-gatestatic void
7297c478bdstevel@tonic-gateget_hostinfo(char *host, int family, struct addrinfo **aipp)
7307c478bdstevel@tonic-gate{
7317c478bdstevel@tonic-gate	struct addrinfo hints, *ai;
7327c478bdstevel@tonic-gate	struct in6_addr addr6;
7337c478bdstevel@tonic-gate	struct in_addr addr;
734e11c3f4meem	char abuf[INET6_ADDRSTRLEN];	/* use for inet_ntop() */
7357c478bdstevel@tonic-gate	int rc;
7367c478bdstevel@tonic-gate
7377c478bdstevel@tonic-gate	/*
7387c478bdstevel@tonic-gate	 * Take care of v4-mapped addresses. It should run same as v4, after
7397c478bdstevel@tonic-gate	 * chopping off the prefix, leaving the IPv4 address
7407c478bdstevel@tonic-gate	 */
7417c478bdstevel@tonic-gate	if ((inet_pton(AF_INET6, host, &addr6) > 0) &&
7427c478bdstevel@tonic-gate	    IN6_IS_ADDR_V4MAPPED(&addr6)) {
7437c478bdstevel@tonic-gate		/* peel off the "mapping" stuff, leaving 32 bit IPv4 address */
7447c478bdstevel@tonic-gate		IN6_V4MAPPED_TO_INADDR(&addr6, &addr);
7457c478bdstevel@tonic-gate
7467c478bdstevel@tonic-gate		/* convert it back to a string */
747e11c3f4meem		(void) inet_ntop(AF_INET, &addr, abuf, sizeof (abuf));
7487c478bdstevel@tonic-gate
7497c478bdstevel@tonic-gate		/* now the host is an IPv4 address */
750e11c3f4meem		(void) strcpy(host, abuf);
7517c478bdstevel@tonic-gate
7527c478bdstevel@tonic-gate		/*
7537c478bdstevel@tonic-gate		 * If it's a mapped address, we convert it into IPv4
7547c478bdstevel@tonic-gate		 * address because traceroute will send and receive IPv4
7557c478bdstevel@tonic-gate		 * packets for that address. Therefore, it's a failure case to
7567c478bdstevel@tonic-gate		 * ask get_hostinfo() to treat a mapped address as an IPv6
7577c478bdstevel@tonic-gate		 * address.
7587c478bdstevel@tonic-gate		 */
7597c478bdstevel@tonic-gate		if (family == AF_INET6) {
7607c478bdstevel@tonic-gate			return;
7617c478bdstevel@tonic-gate		}
7627c478bdstevel@tonic-gate	}
7637c478bdstevel@tonic-gate
7647c478bdstevel@tonic-gate	(void) memset(&hints, 0, sizeof (hints));
7657c478bdstevel@tonic-gate	hints.ai_family = family;
7667ebd3a9dme	hints.ai_flags = AI_ADDRCONFIG | AI_CANONNAME;
7677c478bdstevel@tonic-gate	rc = getaddrinfo(host, NULL, &hints, &ai);
7687c478bdstevel@tonic-gate	if (rc != 0) {
7697c478bdstevel@tonic-gate		if (rc != EAI_NONAME)
7707c478bdstevel@tonic-gate			Fprintf(stderr, "%s: getaddrinfo: %s\n", prog,
7717c478bdstevel@tonic-gate			    gai_strerror(rc));
7727d6fbbeblu		*aipp = NULL;
7737c478bdstevel@tonic-gate		return;
7747c478bdstevel@tonic-gate	}
7757c478bdstevel@tonic-gate	*aipp = ai;
7767c478bdstevel@tonic-gate}
7777c478bdstevel@tonic-gate
7787c478bdstevel@tonic-gate/*
7797c478bdstevel@tonic-gate * Calculate the packet length to be used, and check against the valid range.
7807c478bdstevel@tonic-gate * Returns -1 if range check fails.
7817c478bdstevel@tonic-gate */
7827c478bdstevel@tonic-gatestatic uint_t
7837c478bdstevel@tonic-gatecalc_packetlen(int plen_input, struct pr_set *pr)
7847c478bdstevel@tonic-gate{
7857c478bdstevel@tonic-gate	int minpacket;			/* min ip packet size */
7867c478bdstevel@tonic-gate	int optlen;			/* length of ip options */
7877c478bdstevel@tonic-gate	int plen;
7887c478bdstevel@tonic-gate
7897c478bdstevel@tonic-gate	/*
7907c478bdstevel@tonic-gate	 * LBNL bug fixed: miscalculation of optlen
7917c478bdstevel@tonic-gate	 */
7927c478bdstevel@tonic-gate	if (gw_count > 0) {
7937c478bdstevel@tonic-gate		/*
7947c478bdstevel@tonic-gate		 * IPv4:
7957c478bdstevel@tonic-gate		 * ----
7967c478bdstevel@tonic-gate		 * 5 (NO OPs) + 3 (code, len, ptr) + gateways
7977c478bdstevel@tonic-gate		 * IP options field can hold up to 9 gateways. But the API
7987c478bdstevel@tonic-gate		 * allows you to specify only 8, because the last one is the
7997c478bdstevel@tonic-gate		 * destination host. When this packet is sent, on the wire
8007c478bdstevel@tonic-gate		 * you see one gateway replaced by 4 NO OPs. The other 1 NO
8017c478bdstevel@tonic-gate		 * OP is for alignment
8027c478bdstevel@tonic-gate		 *
8037c478bdstevel@tonic-gate		 * IPv6:
8047c478bdstevel@tonic-gate		 * ----
8057c478bdstevel@tonic-gate		 * Well, formula is different, but the result is same.
8067c478bdstevel@tonic-gate		 * 8 byte fixed part for Type 0 Routing header, followed by
8077c478bdstevel@tonic-gate		 * gateway addresses
8087c478bdstevel@tonic-gate		 */
8097c478bdstevel@tonic-gate		optlen = 8 + gw_count * pr->addr_len;
8107c478bdstevel@tonic-gate	} else {
8117c478bdstevel@tonic-gate		optlen = 0;
8127c478bdstevel@tonic-gate	}
8137c478bdstevel@tonic-gate
8147c478bdstevel@tonic-gate	/* take care of the packet length calculations and checks */
8157c478bdstevel@tonic-gate	minpacket = pr->ip_hdr_len + sizeof (struct outdata) + optlen;
8167c478bdstevel@tonic-gate	if (useicmp)
8177c478bdstevel@tonic-gate		minpacket += pr->icmp_minlen;	/* minimum ICMP header size */
8187c478bdstevel@tonic-gate	else
8197c478bdstevel@tonic-gate		minpacket += sizeof (struct udphdr);
8207c478bdstevel@tonic-gate	plen = plen_input;
8217c478bdstevel@tonic-gate	if (plen == 0) {
8227c478bdstevel@tonic-gate		plen = minpacket;		/* minimum sized packet */
8237c478bdstevel@tonic-gate	} else if (minpacket > plen || plen > IP_MAXPACKET) {
8247c478bdstevel@tonic-gate		Fprintf(stderr, "%s: %s packet size must be >= %d and <= %d\n",
8257c478bdstevel@tonic-gate		    prog, pr->name, minpacket, IP_MAXPACKET);
8267c478bdstevel@tonic-gate		return (0);
8277c478bdstevel@tonic-gate	}
8287c478bdstevel@tonic-gate
8297c478bdstevel@tonic-gate	return (plen);
8307c478bdstevel@tonic-gate}
8317c478bdstevel@tonic-gate
8327c478bdstevel@tonic-gate/*
8337c478bdstevel@tonic-gate * Sets the source address by resolving -i and -s arguments, or if -i and -s
8347c478bdstevel@tonic-gate * don't dictate any, it sets the pick_src to make sure traceroute uses the
8357c478bdstevel@tonic-gate * kernel's pick of the source address.
8367c478bdstevel@tonic-gate * Returns number of interfaces configured on the source host, 0 on error or
8377c478bdstevel@tonic-gate * there's no interface which is up amd not a loopback.
8387c478bdstevel@tonic-gate */
8397c478bdstevel@tonic-gatestatic int
8407c478bdstevel@tonic-gateset_src_addr(struct pr_set *pr, struct ifaddrlist **alp)
8417c478bdstevel@tonic-gate{
8427c478bdstevel@tonic-gate	union any_in_addr *ap;
8437c478bdstevel@tonic-gate	struct ifaddrlist *al = NULL;
8447c478bdstevel@tonic-gate	struct ifaddrlist *tmp1_al = NULL;
8457c478bdstevel@tonic-gate	struct ifaddrlist *tmp2_al = NULL;
8467c478bdstevel@tonic-gate	/* LINTED E_BAD_PTR_CAST_ALIGN */
8477c478bdstevel@tonic-gate	struct sockaddr_in *sin_from = (struct sockaddr_in *)pr->from;
8487c478bdstevel@tonic-gate	/* LINTED E_BAD_PTR_CAST_ALIGN */
8497c478bdstevel@tonic-gate	struct sockaddr_in6 *sin6_from = (struct sockaddr_in6 *)pr->from;
8507c478bdstevel@tonic-gate	struct addrinfo *aip;
8517c478bdstevel@tonic-gate	char errbuf[ERRBUFSIZE];
852e11c3f4meem	char abuf[INET6_ADDRSTRLEN];		/* use for inet_ntop() */
8537c478bdstevel@tonic-gate	int num_ifs;				/* all the interfaces  */
8547c478bdstevel@tonic-gate	int num_src_ifs;			/* exclude loopback and down */
8557c478bdstevel@tonic-gate	int i;
856e11c3f4meem	uint_t ifaddrflags = 0;
8577c478bdstevel@tonic-gate
8587c478bdstevel@tonic-gate	source = source_input;
8597c478bdstevel@tonic-gate
860e11c3f4meem	if (device != NULL)
861e11c3f4meem		ifaddrflags |= LIFC_UNDER_IPMP;
862e11c3f4meem
8637c478bdstevel@tonic-gate	/* get the interface address list */
864e11c3f4meem	num_ifs = ifaddrlist(&al, pr->family, ifaddrflags, errbuf);
8657c478bdstevel@tonic-gate	if (num_ifs < 0) {
8667c478bdstevel@tonic-gate		Fprintf(stderr, "%s: ifaddrlist: %s\n", prog, errbuf);
8677c478bdstevel@tonic-gate		exit(EXIT_FAILURE);
8687c478bdstevel@tonic-gate	}
8697c478bdstevel@tonic-gate
8707c478bdstevel@tonic-gate	num_src_ifs = 0;
8717c478bdstevel@tonic-gate	for (i = 0; i < num_ifs; i++) {
8727c478bdstevel@tonic-gate		if (!(al[i].flags & IFF_LOOPBACK) && (al[i].flags & IFF_UP))
8737c478bdstevel@tonic-gate			num_src_ifs++;
8747c478bdstevel@tonic-gate	}
8757c478bdstevel@tonic-gate
8767c478bdstevel@tonic-gate	if (num_src_ifs == 0) {
8777c478bdstevel@tonic-gate		Fprintf(stderr, "%s: can't find any %s network interfaces\n",
8787c478bdstevel@tonic-gate		    prog, pr->name);
8797c478bdstevel@tonic-gate		return (0);
8807c478bdstevel@tonic-gate	}
8817c478bdstevel@tonic-gate
8827c478bdstevel@tonic-gate	/* verify the device */
8837c478bdstevel@tonic-gate	if (device != NULL) {
8847c478bdstevel@tonic-gate		tmp1_al = find_device(al, num_ifs, device);
8857c478bdstevel@tonic-gate
8867c478bdstevel@tonic-gate		if (tmp1_al == NULL) {
8877c478bdstevel@tonic-gate			Fprintf(stderr, "%s: %s (index %d) is an invalid %s"
8887c478bdstevel@tonic-gate			    " interface\n", prog, device, if_index, pr->name);
8897c478bdstevel@tonic-gate			free(al);
8907c478bdstevel@tonic-gate			return (0);
8917c478bdstevel@tonic-gate		}
8927c478bdstevel@tonic-gate	}
8937c478bdstevel@tonic-gate
8947c478bdstevel@tonic-gate	/* verify the source address */
8957c478bdstevel@tonic-gate	if (source != NULL) {
8967c478bdstevel@tonic-gate		get_hostinfo(source, pr->family, &aip);
8977c478bdstevel@tonic-gate		if (aip == NULL) {
8987c478bdstevel@tonic-gate			Fprintf(stderr,
8997c478bdstevel@tonic-gate			    "%s: %s is an invalid %s source address\n",
9007c478bdstevel@tonic-gate			    prog, source, pr->name);
9017c478bdstevel@tonic-gate
9027c478bdstevel@tonic-gate			free(al);
9037c478bdstevel@tonic-gate			return (0);
9047c478bdstevel@tonic-gate		}
9057c478bdstevel@tonic-gate
9067c478bdstevel@tonic-gate		source = aip->ai_canonname;
9077ebd3a9dme
9087ebd3a9dme		if (pr->family == AF_INET)
9097ebd3a9dme			ap = (union any_in_addr *)
9107ebd3a9dme			    /* LINTED E_BAD_PTR_CAST_ALIGN */
911e11c3f4meem			    &((struct sockaddr_in *)aip->ai_addr)->sin_addr;
9127ebd3a9dme		else
9137ebd3a9dme			ap = (union any_in_addr *)
9147ebd3a9dme			    /* LINTED E_BAD_PTR_CAST_ALIGN */
915e11c3f4meem			    &((struct sockaddr_in6 *)aip->ai_addr)->sin6_addr;
9167c478bdstevel@tonic-gate
9177c478bdstevel@tonic-gate		/*
9187c478bdstevel@tonic-gate		 * LBNL bug fixed: used to accept any src address
9197c478bdstevel@tonic-gate		 */
9207c478bdstevel@tonic-gate		tmp2_al = find_ifaddr(al, num_ifs, ap, pr->family);
9217c478bdstevel@tonic-gate		if (tmp2_al == NULL) {
922e11c3f4meem			(void) inet_ntop(pr->family, ap, abuf, sizeof (abuf));
923e11c3f4meem			Fprintf(stderr, "%s: %s is not a local %s address\n",
924e11c3f4meem			    prog, abuf, pr->name);
9257c478bdstevel@tonic-gate			free(al);
9267c478bdstevel@tonic-gate			freeaddrinfo(aip);
9277c478bdstevel@tonic-gate			return (0);
9287c478bdstevel@tonic-gate		}
9297c478bdstevel@tonic-gate	}
9307c478bdstevel@tonic-gate
9317c478bdstevel@tonic-gate	pick_src = _B_FALSE;
9327c478bdstevel@tonic-gate
9337c478bdstevel@tonic-gate	if (source == NULL) {			/* no -s used */
9347c478bdstevel@tonic-gate		if (device == NULL) {		/* no -i used, no -s used */
9357c478bdstevel@tonic-gate			pick_src = _B_TRUE;
9367c478bdstevel@tonic-gate		} else {			/* -i used, no -s used */
9377c478bdstevel@tonic-gate			/*
9387c478bdstevel@tonic-gate			 * -i used, but not -s, and it's IPv4: set the source
9397c478bdstevel@tonic-gate			 * address to whatever the interface has configured on
9407c478bdstevel@tonic-gate			 * it.
9417c478bdstevel@tonic-gate			 */
9427c478bdstevel@tonic-gate			if (pr->family == AF_INET)
9437c478bdstevel@tonic-gate				set_sin(pr->from, &(tmp1_al->addr), pr->family);
9447c478bdstevel@tonic-gate			else
9457c478bdstevel@tonic-gate				pick_src = _B_TRUE;
9467c478bdstevel@tonic-gate		}
9477c478bdstevel@tonic-gate	} else {				/* -s used */
9487c478bdstevel@tonic-gate		if (device == NULL) {		/* no -i used, -s used */
9497c478bdstevel@tonic-gate			set_sin(pr->from, ap, pr->family);
9507c478bdstevel@tonic-gate
9517c478bdstevel@tonic-gate			if (aip->ai_next != NULL) {
952e11c3f4meem				(void) inet_ntop(pr->family, pr->from_sin_addr,
953e11c3f4meem				    abuf, sizeof (abuf));
954e11c3f4meem				Fprintf(stderr, "%s: Warning: %s has multiple "
955e11c3f4meem				    "addresses; using %s\n", prog, source,
956e11c3f4meem				    abuf);
9577c478bdstevel@tonic-gate			}
9587c478bdstevel@tonic-gate		} else {			/* -i and -s used */
9597c478bdstevel@tonic-gate			/*
9607c478bdstevel@tonic-gate			 * Make sure the source specified matches the
9617c478bdstevel@tonic-gate			 * interface address. You only care about this for IPv4
9627c478bdstevel@tonic-gate			 * IPv6 can handle IF not matching src address
9637c478bdstevel@tonic-gate			 */
9647c478bdstevel@tonic-gate			if (pr->family == AF_INET) {
9657c478bdstevel@tonic-gate				if (!has_addr(aip, &tmp1_al->addr)) {
9667c478bdstevel@tonic-gate					Fprintf(stderr,
9677c478bdstevel@tonic-gate					    "%s: %s is not on interface %s\n",
9687c478bdstevel@tonic-gate					    prog, source, device);
9697c478bdstevel@tonic-gate					exit(EXIT_FAILURE);
9707c478bdstevel@tonic-gate				}
9717c478bdstevel@tonic-gate				/*
9727c478bdstevel@tonic-gate				 * make sure we use the one matching the
9737c478bdstevel@tonic-gate				 * interface's address
9747c478bdstevel@tonic-gate				 */
9757c478bdstevel@tonic-gate				*ap = tmp1_al->addr;
9767c478bdstevel@tonic-gate			}
9777c478bdstevel@tonic-gate
9787c478bdstevel@tonic-gate			set_sin(pr->from, ap, pr->family);
9797c478bdstevel@tonic-gate		}
9807c478bdstevel@tonic-gate	}
9817c478bdstevel@tonic-gate
9827c478bdstevel@tonic-gate	/*
9837c478bdstevel@tonic-gate	 * Binding at this point will set the source address to be used
9847c478bdstevel@tonic-gate	 * for both IPv4 (when raw IP datagrams are not required) and
9857c478bdstevel@tonic-gate	 * IPv6.  If the address being bound to is zero, then the kernel
9867c478bdstevel@tonic-gate	 * will end up choosing the source address when the datagram is
9877c478bdstevel@tonic-gate	 * sent.
9887c478bdstevel@tonic-gate	 *
9897c478bdstevel@tonic-gate	 * For raw IPv4 datagrams, the source address is initialized
9907c478bdstevel@tonic-gate	 * within traceroute() along with the outbound destination
9917c478bdstevel@tonic-gate	 * address.
9927c478bdstevel@tonic-gate	 */
9937c478bdstevel@tonic-gate	if (pr->family == AF_INET && !raw_req) {
9947c478bdstevel@tonic-gate		sin_from->sin_family = AF_INET;
9957c478bdstevel@tonic-gate		sin_from->sin_port = htons(ident);
9967c478bdstevel@tonic-gate		if (bind(sndsock4, (struct sockaddr *)pr->from,
9977c478bdstevel@tonic-gate			sizeof (struct sockaddr_in)) < 0) {
9987c478bdstevel@tonic-gate			Fprintf(stderr, "%s: bind: %s\n", prog,
9997c478bdstevel@tonic-gate			    strerror(errno));
10007c478bdstevel@tonic-gate			exit(EXIT_FAILURE);
10017c478bdstevel@tonic-gate		}
10027c478bdstevel@tonic-gate	} else if (pr->family == AF_INET6) {
10037c478bdstevel@tonic-gate		sin6_from->sin6_family = AF_INET6;
10047c478bdstevel@tonic-gate		sin6_from->sin6_port = htons(ident);
10057c478bdstevel@tonic-gate		if (bind(sndsock6, (struct sockaddr *)pr->from,
10067c478bdstevel@tonic-gate			sizeof (struct sockaddr_in6)) < 0) {
10077c478bdstevel@tonic-gate			Fprintf(stderr, "%s: bind: %s\n", prog,
10087c478bdstevel@tonic-gate			    strerror(errno));
10097c478bdstevel@tonic-gate			exit(EXIT_FAILURE);
10107c478bdstevel@tonic-gate		}
10117c478bdstevel@tonic-gate
10127c478bdstevel@tonic-gate		whereto6.sin6_flowinfo = htonl((class << 20) | flow);
10137c478bdstevel@tonic-gate	}
10147c478bdstevel@tonic-gate	*alp = al;
10157c478bdstevel@tonic-gate	return (num_ifs);
10167c478bdstevel@tonic-gate}
10177c478bdstevel@tonic-gate
10187c478bdstevel@tonic-gate/*
10197c478bdstevel@tonic-gate * Returns the complete ifaddrlist structure matching the desired interface
10207c478bdstevel@tonic-gate * address. Ignores interfaces which are either down or loopback.
10217c478bdstevel@tonic-gate */
10227c478bdstevel@tonic-gatestatic struct ifaddrlist *
10237c478bdstevel@tonic-gatefind_ifaddr(struct ifaddrlist *al, int len, union any_in_addr *addr,
10247c478bdstevel@tonic-gate    int family)
10257c478bdstevel@tonic-gate{
10267c478bdstevel@tonic-gate	struct ifaddrlist *tmp_al = al;
10277c478bdstevel@tonic-gate	int i;
10287c478bdstevel@tonic-gate	size_t addr_len = (family == AF_INET) ? sizeof (struct in_addr) :
10297c478bdstevel@tonic-gate	    sizeof (struct in6_addr);
10307c478bdstevel@tonic-gate
10317c478bdstevel@tonic-gate	for (i = 0; i < len; i++, tmp_al++) {
10327c478bdstevel@tonic-gate		if ((!(tmp_al->flags & IFF_LOOPBACK) &&
10337c478bdstevel@tonic-gate		    (tmp_al->flags & IFF_UP)) &&
10347c478bdstevel@tonic-gate		    (memcmp(&tmp_al->addr, addr, addr_len) == 0))
10357c478bdstevel@tonic-gate			break;
10367c478bdstevel@tonic-gate	}
10377c478bdstevel@tonic-gate
10387c478bdstevel@tonic-gate	if (i < len) {
10397c478bdstevel@tonic-gate		return (tmp_al);
10407c478bdstevel@tonic-gate	} else {
10417c478bdstevel@tonic-gate		return (NULL);
10427c478bdstevel@tonic-gate	}
10437c478bdstevel@tonic-gate}
10447c478bdstevel@tonic-gate
10457c478bdstevel@tonic-gate/*
10467c478bdstevel@tonic-gate * Returns the complete ifaddrlist structure matching the desired interface name
10477c478bdstevel@tonic-gate * Ignores interfaces which are either down or loopback.
10487c478bdstevel@tonic-gate */
10497c478bdstevel@tonic-gatestatic struct ifaddrlist *
10507c478bdstevel@tonic-gatefind_device(struct ifaddrlist *al, int len, char *device)
10517c478bdstevel@tonic-gate{
10527c478bdstevel@tonic-gate	struct ifaddrlist *tmp_al = al;
10537c478bdstevel@tonic-gate	int i;
10547c478bdstevel@tonic-gate
10557c478bdstevel@tonic-gate	for (i = 0; i < len; i++, tmp_al++) {
10567c478bdstevel@tonic-gate		if ((!(tmp_al->flags & IFF_LOOPBACK) &&
10577c478bdstevel@tonic-gate		    (tmp_al->flags & IFF_UP)) &&
10587c478bdstevel@tonic-gate		    (strcmp(tmp_al->device, device) == 0))
10597c478bdstevel@tonic-gate			break;
10607c478bdstevel@tonic-gate	}
10617c478bdstevel@tonic-gate
10627c478bdstevel@tonic-gate	if (i < len) {
10637c478bdstevel@tonic-gate		return (tmp_al);
10647c478bdstevel@tonic-gate	} else {
10657c478bdstevel@tonic-gate		return (NULL);
10667c478bdstevel@tonic-gate	}
10677c478bdstevel@tonic-gate}
10687c478bdstevel@tonic-gate
10697c478bdstevel@tonic-gate/*
10707c478bdstevel@tonic-gate * returns _B_TRUE if given hostinfo contains the given address
10717c478bdstevel@tonic-gate */
10727c478bdstevel@tonic-gatestatic boolean_t
10737c478bdstevel@tonic-gatehas_addr(struct addrinfo *ai, union any_in_addr *addr)
10747c478bdstevel@tonic-gate{
10757c478bdstevel@tonic-gate	struct addrinfo *ai_tmp = NULL;
10767c478bdstevel@tonic-gate	union any_in_addr *ap;
10777c478bdstevel@tonic-gate
10787c478bdstevel@tonic-gate	for (ai_tmp = ai; ai_tmp != NULL; ai_tmp = ai_tmp->ai_next) {
10797c478bdstevel@tonic-gate		if (ai_tmp->ai_family == AF_INET6)
10807c478bdstevel@tonic-gate			continue;
10817c478bdstevel@tonic-gate		ap = (union any_in_addr *)
10827c478bdstevel@tonic-gate		    /* LINTED E_BAD_PTR_CAST_ALIGN */
10837c478bdstevel@tonic-gate		    &((struct sockaddr_in *)ai_tmp->ai_addr)->sin_addr;
10847c478bdstevel@tonic-gate		if (memcmp(ap, addr, sizeof (struct in_addr)) == 0)
10857c478bdstevel@tonic-gate			break;
10867c478bdstevel@tonic-gate	}
10877c478bdstevel@tonic-gate
10887c478bdstevel@tonic-gate	if (ai_tmp != NULL) {
10897c478bdstevel@tonic-gate		return (_B_TRUE);
10907c478bdstevel@tonic-gate	} else {
10917c478bdstevel@tonic-gate		return (_B_FALSE);
10927c478bdstevel@tonic-gate	}
10937c478bdstevel@tonic-gate}
10947c478bdstevel@tonic-gate
10957c478bdstevel@tonic-gate/*
10967c478bdstevel@tonic-gate * Resolve the gateway names, splitting results into v4 and v6 lists.
10977c478bdstevel@tonic-gate * Gateway addresses are added to the appropriate passed-in array; the
10987c478bdstevel@tonic-gate * number of resolved gateways for each af is returned in resolved[6].
10997c478bdstevel@tonic-gate * Assumes that passed-in arrays are large enough for MAX_GWS[6] addrs
11007c478bdstevel@tonic-gate * and resolved[6] ptrs are non-null; ignores array and counter if the
11017c478bdstevel@tonic-gate * address family param makes them irrelevant.
11027c478bdstevel@tonic-gate */
11037c478bdstevel@tonic-gatestatic void
11047c478bdstevel@tonic-gateget_gwaddrs(char **gwlist, int family, union any_in_addr *gwIPlist,
11057c478bdstevel@tonic-gate    union any_in_addr *gwIPlist6, int *resolved, int *resolved6)
11067c478bdstevel@tonic-gate{
11077c478bdstevel@tonic-gate	int i;
11087c478bdstevel@tonic-gate	boolean_t check_v4 = _B_TRUE, check_v6 = _B_TRUE;
11097c478bdstevel@tonic-gate	struct addrinfo *ai = NULL;
11107c478bdstevel@tonic-gate	struct addrinfo *aip = NULL;
11117c478bdstevel@tonic-gate
11127c478bdstevel@tonic-gate	*resolved = *resolved6 = 0;
11137c478bdstevel@tonic-gate	switch (family) {
11147c478bdstevel@tonic-gate	case AF_UNSPEC:
11157c478bdstevel@tonic-gate		break;
11167c478bdstevel@tonic-gate	case AF_INET:
11177c478bdstevel@tonic-gate		check_v6 = _B_FALSE;
11187c478bdstevel@tonic-gate		break;
11197c478bdstevel@tonic-gate	case AF_INET6:
11207c478bdstevel@tonic-gate		check_v4 = _B_FALSE;
11217c478bdstevel@tonic-gate		break;
11227c478bdstevel@tonic-gate	default:
11237c478bdstevel@tonic-gate		return;
11247c478bdstevel@tonic-gate	}
11257c478bdstevel@tonic-gate
11267c478bdstevel@tonic-gate	if (check_v4 && gw_count >= MAX_GWS) {
11277c478bdstevel@tonic-gate		check_v4 = _B_FALSE;
11287c478bdstevel@tonic-gate		Fprintf(stderr, "%s: too many IPv4 gateways\n", prog);
11297d6fbbeblu		num_v4 = 0;
11307c478bdstevel@tonic-gate	}
11317c478bdstevel@tonic-gate	if (check_v6 && gw_count >= MAX_GWS6) {
11327c478bdstevel@tonic-gate		check_v6 = _B_FALSE;
11337c478bdstevel@tonic-gate		Fprintf(stderr, "%s: too many IPv6 gateways\n", prog);
11347d6fbbeblu		num_v6 = 0;
11357c478bdstevel@tonic-gate	}
11367c478bdstevel@tonic-gate
11377c478bdstevel@tonic-gate	for (i = 0; i < gw_count; i++) {
11387c478bdstevel@tonic-gate		if (!check_v4 && !check_v6)
11397c478bdstevel@tonic-gate			return;
11407c478bdstevel@tonic-gate		get_hostinfo(gwlist[i], family, &ai);
11417c478bdstevel@tonic-gate		if (ai == NULL)
11427c478bdstevel@tonic-gate			return;
11437c478bdstevel@tonic-gate		if (check_v4 && num_v4 != 0) {
11447d6fbbeblu			check_v4 = _B_FALSE;
11457c478bdstevel@tonic-gate			for (aip = ai; aip != NULL; aip = aip->ai_next) {
11467c478bdstevel@tonic-gate				if (aip->ai_family == AF_INET) {
11477c478bdstevel@tonic-gate					/* LINTED E_BAD_PTR_CAST_ALIGN */
11487c478bdstevel@tonic-gate					bcopy(&((struct sockaddr_in *)
11497c478bdstevel@tonic-gate					    aip->ai_addr)->sin_addr,
11507c478bdstevel@tonic-gate					    &gwIPlist[i].addr,
11517c478bdstevel@tonic-gate					    aip->ai_addrlen);
11527c478bdstevel@tonic-gate					(*resolved)++;
11537d6fbbeblu					check_v4 = _B_TRUE;
11547c478bdstevel@tonic-gate					break;
11557c478bdstevel@tonic-gate				}
11567c478bdstevel@tonic-gate			}
11577c478bdstevel@tonic-gate		} else if (check_v4) {
11587c478bdstevel@tonic-gate			check_v4 = _B_FALSE;
11597c478bdstevel@tonic-gate		}
11607c478bdstevel@tonic-gate		if (check_v6 && num_v6 != 0) {
11617d6fbbeblu			check_v6 = _B_FALSE;
11627c478bdstevel@tonic-gate			for (aip = ai; aip != NULL; aip = aip->ai_next) {
11637c478bdstevel@tonic-gate				if (aip->ai_family == AF_INET6) {
11647c478bdstevel@tonic-gate					/* LINTED E_BAD_PTR_CAST_ALIGN */
11657c478bdstevel@tonic-gate					bcopy(&((struct sockaddr_in6 *)
11667c478bdstevel@tonic-gate					    aip->ai_addr)->sin6_addr,
11677c478bdstevel@tonic-gate					    &gwIPlist6[i].addr6,
11687c478bdstevel@tonic-gate					    aip->ai_addrlen);
11697c478bdstevel@tonic-gate					(*resolved6)++;
11707d6fbbeblu					check_v6 = _B_TRUE;
11717c478bdstevel@tonic-gate					break;
11727c478bdstevel@tonic-gate				}
11737c478bdstevel@tonic-gate			}
11747c478bdstevel@tonic-gate		} else if (check_v6) {
11757c478bdstevel@tonic-gate			check_v6 = _B_FALSE;
11767c478bdstevel@tonic-gate		}
11777c478bdstevel@tonic-gate	}
11787c478bdstevel@tonic-gate	freeaddrinfo(ai);
11797c478bdstevel@tonic-gate}
11807c478bdstevel@tonic-gate
11817c478bdstevel@tonic-gate/*
11827c478bdstevel@tonic-gate * set protocol specific values here
11837c478bdstevel@tonic-gate */
11847c478bdstevel@tonic-gatestatic void
11857c478bdstevel@tonic-gatesetup_protocol(struct pr_set *pr, int family)
11867c478bdstevel@tonic-gate{
11877c478bdstevel@tonic-gate	/*
11887c478bdstevel@tonic-gate	 * Set the global variables for each AF. This is going to save us lots
11897c478bdstevel@tonic-gate	 * of "if (family == AF_INET)... else .."
11907c478bdstevel@tonic-gate	 */
11917c478bdstevel@tonic-gate	pr->family = family;
11927c478bdstevel@tonic-gate
11937c478bdstevel@tonic-gate	if (family == AF_INET) {
11947c478bdstevel@tonic-gate		if (!docksum) {
11957c478bdstevel@tonic-gate			Fprintf(stderr,
11967c478bdstevel@tonic-gate			    "%s: Warning: checksums disabled\n", prog);
11977c478bdstevel@tonic-gate		}
11987c478bdstevel@tonic-gate		(void) strcpy(pr->name, "IPv4");
11997c478bdstevel@tonic-gate		(void) strcpy(pr->icmp, "icmp");
12007c478bdstevel@tonic-gate		pr->icmp_minlen = ICMP_MINLEN;
12017c478bdstevel@tonic-gate		pr->addr_len = sizeof (struct in_addr);
12027c478bdstevel@tonic-gate		pr->ip_hdr_len = sizeof (struct ip);
12037c478bdstevel@tonic-gate		pr->sock_size = sizeof (struct sockaddr_in);
12047c478bdstevel@tonic-gate		pr->to = (struct sockaddr *)&whereto;
12057c478bdstevel@tonic-gate		pr->from = (struct sockaddr *)&wherefrom;
12067c478bdstevel@tonic-gate		pr->from_sin_addr = (void *)&wherefrom.sin_addr;
12077c478bdstevel@tonic-gate		pr->gwIPlist = gwIPlist;
12087c478bdstevel@tonic-gate		pr->set_buffers_fn = set_buffers;
12097c478bdstevel@tonic-gate		pr->check_reply_fn = check_reply;
12107c478bdstevel@tonic-gate		pr->print_icmp_other_fn = print_icmp_other;
12117c478bdstevel@tonic-gate		pr->print_addr_fn = print_addr;
12127c478bdstevel@tonic-gate		pr->packlen = calc_packetlen(packlen_input, pr);
12137c478bdstevel@tonic-gate	} else {
12147c478bdstevel@tonic-gate		(void) strcpy(pr->name, "IPv6");
12157c478bdstevel@tonic-gate		(void) strcpy(pr->icmp, "ipv6-icmp");
12167c478bdstevel@tonic-gate		pr->icmp_minlen = ICMP6_MINLEN;
12177c478bdstevel@tonic-gate		pr->addr_len = sizeof (struct in6_addr);
12187c478bdstevel@tonic-gate		pr->ip_hdr_len = sizeof (struct ip6_hdr);
12197c478bdstevel@tonic-gate		pr->sock_size = sizeof (struct sockaddr_in6);
12207c478bdstevel@tonic-gate		pr->to = (struct sockaddr *)&whereto6;
12217c478bdstevel@tonic-gate		pr->from = (struct sockaddr *)&wherefrom6;
12227c478bdstevel@tonic-gate		pr->from_sin_addr = (void *)&wherefrom6.sin6_addr;
12237c478bdstevel@tonic-gate		pr->gwIPlist = gwIP6list;
12247c478bdstevel@tonic-gate		pr->set_buffers_fn = set_buffers6;
12257c478bdstevel@tonic-gate		pr->check_reply_fn = check_reply6;
12267c478bdstevel@tonic-gate		pr->print_icmp_other_fn = print_icmp_other6;
12277c478bdstevel@tonic-gate		pr->print_addr_fn = print_addr6;
12287c478bdstevel@tonic-gate		pr->packlen = calc_packetlen(packlen_input, pr);
12297c478bdstevel@tonic-gate	}
12307c478bdstevel@tonic-gate	if (pr->packlen == 0)
12317c478bdstevel@tonic-gate		exit(EXIT_FAILURE);
12327c478bdstevel@tonic-gate}
12337c478bdstevel@tonic-gate
12347c478bdstevel@tonic-gate/*
12357c478bdstevel@tonic-gate * setup the sockets for the given protocol's address family
12367c478bdstevel@tonic-gate */
12377c478bdstevel@tonic-gatestatic void
12387c478bdstevel@tonic-gatesetup_socket(struct pr_set *pr, int packet_len)
12397c478bdstevel@tonic-gate{
12407c478bdstevel@tonic-gate	int on = 1;
12417c478bdstevel@tonic-gate	struct protoent *pe;
12427c478bdstevel@tonic-gate	int type;
12437c478bdstevel@tonic-gate	int proto;
12447c478bdstevel@tonic-gate	int int_op;
12457c478bdstevel@tonic-gate	int rsock;
12467c478bdstevel@tonic-gate	int ssock;
12477c478bdstevel@tonic-gate
12487c478bdstevel@tonic-gate	if ((pe = getprotobyname(pr->icmp)) == NULL) {
12497c478bdstevel@tonic-gate		Fprintf(stderr, "%s: unknown protocol %s\n", prog, pr->icmp);
12507c478bdstevel@tonic-gate		exit(EXIT_FAILURE);
12517c478bdstevel@tonic-gate	}
12527c478bdstevel@tonic-gate
12537c478bdstevel@tonic-gate	/* privilege bracketing */
12547c478bdstevel@tonic-gate	(void) __priv_bracket(PRIV_ON);
12557c478bdstevel@tonic-gate
12567c478bdstevel@tonic-gate	if ((rsock = socket(pr->family, SOCK_RAW, pe->p_proto)) < 0) {
12577c478bdstevel@tonic-gate		Fprintf(stderr, "%s: icmp socket: %s\n", prog, strerror(errno));
12587c478bdstevel@tonic-gate		exit(EXIT_FAILURE);
12597c478bdstevel@tonic-gate	}
12607c478bdstevel@tonic-gate
12617c478bdstevel@tonic-gate	if (options & SO_DEBUG) {
12627c478bdstevel@tonic-gate		if (setsockopt(rsock, SOL_SOCKET, SO_DEBUG, (char *)&on,
12637c478bdstevel@tonic-gate		    sizeof (on)) < 0) {
12647c478bdstevel@tonic-gate			Fprintf(stderr, "%s: SO_DEBUG: %s\n", prog,
12657c478bdstevel@tonic-gate			    strerror(errno));
12667c478bdstevel@tonic-gate			exit(EXIT_FAILURE);
12677c478bdstevel@tonic-gate		}
12687c478bdstevel@tonic-gate	}
12697c478bdstevel@tonic-gate	if (options & SO_DONTROUTE) {
12707c478bdstevel@tonic-gate		if (setsockopt(rsock, SOL_SOCKET, SO_DONTROUTE, (char *)&on,
12717c478bdstevel@tonic-gate		    sizeof (on)) < 0) {
12727c478bdstevel@tonic-gate			Fprintf(stderr, "%s: SO_DONTROUTE: %s\n", prog,
12737c478bdstevel@tonic-gate			    strerror(errno));
12747c478bdstevel@tonic-gate			exit(EXIT_FAILURE);
12757c478bdstevel@tonic-gate		}
12767c478bdstevel@tonic-gate	}
12777c478bdstevel@tonic-gate
12787c478bdstevel@tonic-gate	if (pr->family == AF_INET6) {
12797c478bdstevel@tonic-gate		/* Enable receipt of destination address info */
12807c478bdstevel@tonic-gate		if (setsockopt(rsock, IPPROTO_IPV6, IPV6_RECVPKTINFO,
12817c478bdstevel@tonic-gate		    (char *)&on, sizeof (on)) < 0) {
12827c478bdstevel@tonic-gate			Fprintf(stderr, "%s: IPV6_RECVPKTINFO: %s\n", prog,
12837c478bdstevel@tonic-gate			    strerror(errno));
12847c478bdstevel@tonic-gate			exit(EXIT_FAILURE);
12857c478bdstevel@tonic-gate		}
12867c478bdstevel@tonic-gate		/* Enable receipt of hoplimit info */
12877c478bdstevel@tonic-gate		if (setsockopt(rsock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT,
12887c478bdstevel@tonic-gate		    (char *)&on, sizeof (on)) < 0) {
12897c478bdstevel@tonic-gate			Fprintf(stderr, "%s: IPV6_RECVHOPLIMIT: %s\n", prog,
12907c478bdstevel@tonic-gate			    strerror(errno));
12917c478bdstevel@tonic-gate			exit(EXIT_FAILURE);
12927c478bdstevel@tonic-gate		}
12937c478bdstevel@tonic-gate
12947c478bdstevel@tonic-gate	}
12957c478bdstevel@tonic-gate
12967c478bdstevel@tonic-gate	/*
12977c478bdstevel@tonic-gate	 * Initialize the socket type and protocol based on the address
12987c478bdstevel@tonic-gate	 * family, whether or not a raw IP socket is required (for IPv4)
12997c478bdstevel@tonic-gate	 * or whether ICMP will be used instead of UDP.
13007c478bdstevel@tonic-gate	 *
13017c478bdstevel@tonic-gate	 * For historical reasons, the datagrams sent out by
13027c478bdstevel@tonic-gate	 * traceroute(1M) do not have the "don't fragment" flag set.  For
13037c478bdstevel@tonic-gate	 * this reason as well as the ability to set the Loose Source and
13047c478bdstevel@tonic-gate	 * Record Route (LSRR) option, a raw IP socket will be used for
13057c478bdstevel@tonic-gate	 * IPv4 when run in the global zone.  Otherwise, the actual
13067c478bdstevel@tonic-gate	 * datagram that will be sent will be a regular UDP or ICMP echo
13077c478bdstevel@tonic-gate	 * request packet.  However for convenience and for future options
13087c478bdstevel@tonic-gate	 * when other IP header information may be specified using
13097c478bdstevel@tonic-gate	 * traceroute, the buffer including the raw IP and UDP or ICMP
13107c478bdstevel@tonic-gate	 * header is always filled in.  When the probe is actually sent,
13117c478bdstevel@tonic-gate	 * the size of the request and the start of the packet is set
13127c478bdstevel@tonic-gate	 * according to the type of datagram to send.
13137c478bdstevel@tonic-gate	 */
13147c478bdstevel@tonic-gate	if (pr->family == AF_INET && raw_req) {
13157c478bdstevel@tonic-gate		type = SOCK_RAW;
13167c478bdstevel@tonic-gate		proto = IPPROTO_RAW;
13177c478bdstevel@tonic-gate	} else if (useicmp) {
13187c478bdstevel@tonic-gate		type = SOCK_RAW;
13197c478bdstevel@tonic-gate		if (pr->family == AF_INET)
13207c478bdstevel@tonic-gate			proto = IPPROTO_ICMP;
13217c478bdstevel@tonic-gate		else
13227c478bdstevel@tonic-gate			proto = IPPROTO_ICMPV6;
13237c478bdstevel@tonic-gate	} else {
13247c478bdstevel@tonic-gate		type = SOCK_DGRAM;
13257c478bdstevel@tonic-gate		proto = IPPROTO_UDP;
13267c478bdstevel@tonic-gate	}
13277c478bdstevel@tonic-gate	ssock = socket(pr->family, type, proto);
13287c478bdstevel@tonic-gate
13297c478bdstevel@tonic-gate	if (ssock < 0) {
13307c478bdstevel@tonic-gate		if (proto == IPPROTO_RAW) {
13317c478bdstevel@tonic-gate			Fprintf(stderr, "%s: raw socket: %s\n", prog,
13327c478bdstevel@tonic-gate			    strerror(errno));
13337c478bdstevel@tonic-gate		} else if (proto == IPPROTO_UDP) {
13347c478bdstevel@tonic-gate			Fprintf(stderr, "%s: udp socket: %s\n", prog,
13357c478bdstevel@tonic-gate			    strerror(errno));
13367c478bdstevel@tonic-gate		} else {
13377c478bdstevel@tonic-gate			Fprintf(stderr, "%s: icmp socket: %s\n", prog,
13387c478bdstevel@tonic-gate			    strerror(errno));
13397c478bdstevel@tonic-gate		}
13407c478bdstevel@tonic-gate		exit(EXIT_FAILURE);
13417c478bdstevel@tonic-gate	}
13427c478bdstevel@tonic-gate
13437c478bdstevel@tonic-gate	if (setsockopt(ssock, SOL_SOCKET, SO_SNDBUF, (char *)&packet_len,
13447c478bdstevel@tonic-gate	    sizeof (packet_len)) < 0) {
13457c478bdstevel@tonic-gate		Fprintf(stderr, "%s: SO_SNDBUF: %s\n", prog, strerror(errno));
13467c478bdstevel@tonic-gate		exit(EXIT_FAILURE);
13477c478bdstevel@tonic-gate	}
13487c478bdstevel@tonic-gate
13497c478bdstevel@tonic-gate	if (pr->family == AF_INET && raw_req) {
13507c478bdstevel@tonic-gate		if (setsockopt(ssock, IPPROTO_IP, IP_HDRINCL, (char *)&on,
13517c478bdstevel@tonic-gate		    sizeof (on)) < 0) {
13527c478bdstevel@tonic-gate			Fprintf(stderr, "%s: IP_HDRINCL: %s\n", prog,
13537c478bdstevel@tonic-gate			    strerror(errno));
13547c478bdstevel@tonic-gate			exit(EXIT_FAILURE);
13557c478bdstevel@tonic-gate		}
13567c478bdstevel@tonic-gate	}
13577c478bdstevel@tonic-gate
13587c478bdstevel@tonic-gate	if (options & SO_DEBUG) {
13597c478bdstevel@tonic-gate		if (setsockopt(ssock, SOL_SOCKET, SO_DEBUG, (char *)&on,
13607c478bdstevel@tonic-gate		    sizeof (on)) < 0) {
13617c478bdstevel@tonic-gate			Fprintf(stderr, "%s: SO_DEBUG: %s\n", prog,
13627c478bdstevel@tonic-gate			    strerror(errno));
13637c478bdstevel@tonic-gate			exit(EXIT_FAILURE);
13647c478bdstevel@tonic-gate		}
13657c478bdstevel@tonic-gate	}
13667c478bdstevel@tonic-gate	if (options & SO_DONTROUTE) {
13677c478bdstevel@tonic-gate		if (setsockopt(ssock, SOL_SOCKET, SO_DONTROUTE,
13687c478bdstevel@tonic-gate		    (char *)&on, sizeof (on)) < 0) {
13697c478bdstevel@tonic-gate			Fprintf(stderr, "%s: SO_DONTROUTE: %s\n", prog,
13707c478bdstevel@tonic-gate			    strerror(errno));
13717c478bdstevel@tonic-gate			exit(EXIT_FAILURE);
13727c478bdstevel@tonic-gate		}
13737c478bdstevel@tonic-gate	}
13747c478bdstevel@tonic-gate
13757c478bdstevel@tonic-gate	/*
13767c478bdstevel@tonic-gate	 * If a raw IPv4 packet is going to be sent, the Type of Service
13777c478bdstevel@tonic-gate	 * field in the packet will be initialized in set_buffers().
13787c478bdstevel@tonic-gate	 * Otherwise, it is initialized here using the IPPROTO_IP level
13797c478bdstevel@tonic-gate	 * socket option.
13807c478bdstevel@tonic-gate	 */
13817c478bdstevel@tonic-gate	if (settos && !raw_req) {
13827c478bdstevel@tonic-gate		int_op = tos;
13837c478bdstevel@tonic-gate		if (setsockopt(ssock, IPPROTO_IP, IP_TOS, (char *)&int_op,
13847c478bdstevel@tonic-gate		    sizeof (int_op)) < 0) {
13857c478bdstevel@tonic-gate			Fprintf(stderr, "%s: IP_TOS: %s\n", prog,
13867c478bdstevel@tonic-gate			    strerror(errno));
13877c478bdstevel@tonic-gate			exit(EXIT_FAILURE);
13887c478bdstevel@tonic-gate		}
13897c478bdstevel@tonic-gate	}
1390bd670b3Erik Nordmark
1391bd670b3Erik Nordmark	/* We enable or disable to not depend on the kernel default */
1392bd670b3Erik Nordmark	if (pr->family == AF_INET) {
1393bd670b3Erik Nordmark		if (setsockopt(ssock, IPPROTO_IP, IP_DONTFRAG,
1394bd670b3Erik Nordmark		    (char *)&dontfrag, sizeof (dontfrag)) == -1) {
1395bd670b3Erik Nordmark			Fprintf(stderr, "%s: IP_DONTFRAG %s\n", prog,
1396bd670b3Erik Nordmark			    strerror(errno));
1397bd670b3Erik Nordmark			exit(EXIT_FAILURE);
1398bd670b3Erik Nordmark		}
1399bd670b3Erik Nordmark	} else {
1400bd670b3Erik Nordmark		if (setsockopt(ssock, IPPROTO_IPV6, IPV6_DONTFRAG,
1401bd670b3Erik Nordmark		    (char *)&dontfrag, sizeof (dontfrag)) == -1) {
1402