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