10Sstevel@tonic-gate /* 2*8485SPeter.Memishian@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 30Sstevel@tonic-gate * Use is subject to license terms. 40Sstevel@tonic-gate */ 50Sstevel@tonic-gate 60Sstevel@tonic-gate /* 70Sstevel@tonic-gate * Copyright (c) 1988, 1989, 1991, 1994, 1995, 1996, 1997 80Sstevel@tonic-gate * The Regents of the University of California. All rights reserved. 90Sstevel@tonic-gate * 100Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 110Sstevel@tonic-gate * modification, are permitted provided that: (1) source code distributions 120Sstevel@tonic-gate * retain the above copyright notice and this paragraph in its entirety, (2) 130Sstevel@tonic-gate * distributions including binary code include the above copyright notice and 140Sstevel@tonic-gate * this paragraph in its entirety in the documentation or other materials 150Sstevel@tonic-gate * provided with the distribution, and (3) all advertising materials mentioning 160Sstevel@tonic-gate * features or use of this software display the following acknowledgement: 170Sstevel@tonic-gate * ``This product includes software developed by the University of California, 180Sstevel@tonic-gate * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 190Sstevel@tonic-gate * the University nor the names of its contributors may be used to endorse 200Sstevel@tonic-gate * or promote products derived from this software without specific prior 210Sstevel@tonic-gate * written permission. 220Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 230Sstevel@tonic-gate * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 240Sstevel@tonic-gate * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 250Sstevel@tonic-gate * 260Sstevel@tonic-gate * 270Sstevel@tonic-gate * @(#)$Header: traceroute.c,v 1.49 97/06/13 02:30:23 leres Exp $ (LBL) 280Sstevel@tonic-gate */ 290Sstevel@tonic-gate 300Sstevel@tonic-gate #include <sys/param.h> 310Sstevel@tonic-gate #include <sys/file.h> 320Sstevel@tonic-gate #include <sys/ioctl.h> 330Sstevel@tonic-gate #include <sys/socket.h> 340Sstevel@tonic-gate #include <sys/time.h> 350Sstevel@tonic-gate #include <sys/sysmacros.h> 360Sstevel@tonic-gate 370Sstevel@tonic-gate #include <netinet/in_systm.h> 380Sstevel@tonic-gate #include <netinet/in.h> 390Sstevel@tonic-gate #include <netinet/ip.h> 400Sstevel@tonic-gate #include <netinet/ip_var.h> 410Sstevel@tonic-gate #include <netinet/ip_icmp.h> 420Sstevel@tonic-gate #include <netinet/udp.h> 430Sstevel@tonic-gate #include <netinet/udp_var.h> 440Sstevel@tonic-gate #include <netinet/ip6.h> 450Sstevel@tonic-gate #include <netinet/icmp6.h> 460Sstevel@tonic-gate 470Sstevel@tonic-gate #include <arpa/inet.h> 480Sstevel@tonic-gate 490Sstevel@tonic-gate #include <ctype.h> 500Sstevel@tonic-gate #include <errno.h> 510Sstevel@tonic-gate #include <malloc.h> 520Sstevel@tonic-gate #include <memory.h> 530Sstevel@tonic-gate #include <netdb.h> 540Sstevel@tonic-gate #include <stdio.h> 550Sstevel@tonic-gate #include <stdlib.h> 560Sstevel@tonic-gate #include <strings.h> 570Sstevel@tonic-gate #include <unistd.h> 580Sstevel@tonic-gate #include <libintl.h> 590Sstevel@tonic-gate #include <locale.h> 600Sstevel@tonic-gate #include <signal.h> 610Sstevel@tonic-gate #include <setjmp.h> 620Sstevel@tonic-gate #include <limits.h> 630Sstevel@tonic-gate #include <zone.h> 640Sstevel@tonic-gate 650Sstevel@tonic-gate #include <priv_utils.h> 660Sstevel@tonic-gate 671254Smeem #include <libinetutil.h> 680Sstevel@tonic-gate #include "traceroute.h" 690Sstevel@tonic-gate 700Sstevel@tonic-gate #define MAX_SEQ 65535 /* max sequence value for ICMP */ 710Sstevel@tonic-gate #define MAX_TRAFFIC_CLASS 255 /* max traffic class for IPv6 */ 720Sstevel@tonic-gate #define MAX_FLOW_LABEL 0xFFFFF /* max flow label for IPv6 */ 730Sstevel@tonic-gate #define MAX_TOS 255 /* max type-of-service for IPv4 */ 740Sstevel@tonic-gate #define STR_LEN 30 750Sstevel@tonic-gate 760Sstevel@tonic-gate /* store the information about a host */ 770Sstevel@tonic-gate struct hostinfo { 780Sstevel@tonic-gate char *name; /* hostname */ 790Sstevel@tonic-gate int family; /* address family of the IP addresses */ 800Sstevel@tonic-gate int num_addr; /* number of IP addresses */ 810Sstevel@tonic-gate union any_in_addr *addrs; /* list of IP addresses */ 820Sstevel@tonic-gate }; 830Sstevel@tonic-gate 840Sstevel@tonic-gate /* used to store a bunch of protocol specific values */ 850Sstevel@tonic-gate struct pr_set { 860Sstevel@tonic-gate int family; /* AF_INET or AF_INET6 */ 870Sstevel@tonic-gate char name[STR_LEN]; /* "IPv4" or "IPv6" */ 880Sstevel@tonic-gate char icmp[STR_LEN]; /* "icmp" or "ipv6-icmp" */ 890Sstevel@tonic-gate int icmp_minlen; 900Sstevel@tonic-gate int addr_len; 910Sstevel@tonic-gate int ip_hdr_len; 920Sstevel@tonic-gate int packlen; 930Sstevel@tonic-gate int sock_size; /* size of sockaddr_in or sockaddr_in6 */ 940Sstevel@tonic-gate struct sockaddr *to; 950Sstevel@tonic-gate struct sockaddr *from; 960Sstevel@tonic-gate void *from_sin_addr; 970Sstevel@tonic-gate union any_in_addr *gwIPlist; 980Sstevel@tonic-gate /* pointers to v4/v6 functions */ 990Sstevel@tonic-gate struct ip *(*set_buffers_fn) (int); 1000Sstevel@tonic-gate int (*check_reply_fn)(struct msghdr *, int, int, uchar_t *, uchar_t *); 1010Sstevel@tonic-gate boolean_t (*print_icmp_other_fn)(uchar_t, uchar_t); 1020Sstevel@tonic-gate void (*print_addr_fn)(uchar_t *, int, struct sockaddr *); 1030Sstevel@tonic-gate 1040Sstevel@tonic-gate }; 1050Sstevel@tonic-gate 1060Sstevel@tonic-gate /* 1070Sstevel@tonic-gate * LBNL bug fixed: in LBNL traceroute 'uchar_t packet[512];' 1080Sstevel@tonic-gate * Not sufficient to hold the complete packet for ECHO REPLY of a big probe. 1090Sstevel@tonic-gate * Packet size is reported incorrectly in such a case. 1100Sstevel@tonic-gate * Also this buffer needs to be 32 bit aligned. In the future the alignment 1110Sstevel@tonic-gate * requirement will be increased to 64 bit. So, let's use 64 bit alignment now. 1120Sstevel@tonic-gate */ 1130Sstevel@tonic-gate static uint64_t packet[(IP_MAXPACKET + 1)/8]; /* received packet */ 1140Sstevel@tonic-gate 1150Sstevel@tonic-gate static struct ip *outip4; /* output buffer to send as an IPv4 datagram */ 1160Sstevel@tonic-gate static struct ip *outip6; /* output buffer to send as an IPv6 datagram */ 1170Sstevel@tonic-gate 1180Sstevel@tonic-gate /* Used to store the ancillary data that comes with the received packets */ 1190Sstevel@tonic-gate static uint64_t ancillary_data[(IP_MAXPACKET + 1)/8]; 1200Sstevel@tonic-gate 1210Sstevel@tonic-gate /* first get the gw names, later you'll resolve them based on the family */ 1220Sstevel@tonic-gate static char *gwlist[MAXMAX_GWS]; /* gateway names list */ 1230Sstevel@tonic-gate static union any_in_addr gwIPlist[MAX_GWS]; /* gateway IPv4 address list */ 1240Sstevel@tonic-gate static union any_in_addr gwIP6list[MAX_GWS6]; /* gateway IPv6 address list */ 1250Sstevel@tonic-gate 1260Sstevel@tonic-gate static int family_input = AF_UNSPEC; /* User supplied protocol family */ 1270Sstevel@tonic-gate static int rcvsock4; /* receive (icmp) socket file descriptor */ 1280Sstevel@tonic-gate static int sndsock4; /* send (udp/icmp) socket file descriptor */ 1290Sstevel@tonic-gate static int rcvsock6; /* receive (icmp6) socket file descriptor */ 1300Sstevel@tonic-gate static int sndsock6; /* send (udp6/icmp6) socket file descriptor */ 1310Sstevel@tonic-gate int gw_count = 0; /* number of gateways */ 1320Sstevel@tonic-gate static struct sockaddr_in whereto; /* Who to try to reach */ 1330Sstevel@tonic-gate static struct sockaddr_in6 whereto6; 1340Sstevel@tonic-gate static struct sockaddr_in wherefrom; /* Who we are */ 1350Sstevel@tonic-gate static struct sockaddr_in6 wherefrom6; 1360Sstevel@tonic-gate static int packlen_input = 0; /* user input for packlen */ 1370Sstevel@tonic-gate 1380Sstevel@tonic-gate char *prog; 1390Sstevel@tonic-gate static char *source_input = NULL; /* this is user arg. source, doesn't change */ 1400Sstevel@tonic-gate static char *source = NULL; /* this gets modified after name lookup */ 1410Sstevel@tonic-gate char *hostname; 1420Sstevel@tonic-gate static char *device = NULL; /* interface name */ 1430Sstevel@tonic-gate static struct pr_set *pr4; /* protocol info for IPv4 */ 1440Sstevel@tonic-gate static struct pr_set *pr6; /* protocol info for IPv6 */ 1450Sstevel@tonic-gate static struct ifaddrlist *al4; /* list of interfaces */ 1460Sstevel@tonic-gate static struct ifaddrlist *al6; /* list of interfaces */ 1470Sstevel@tonic-gate static uint_t if_index = 0; /* interface index */ 1480Sstevel@tonic-gate static int num_v4 = 0; /* count of IPv4 addresses */ 1490Sstevel@tonic-gate static int num_v6 = 0; /* count of IPv6 addresses */ 1500Sstevel@tonic-gate static int num_ifs4 = 0; /* count of local IPv4 interfaces */ 1510Sstevel@tonic-gate static int num_ifs6 = 0; /* count of local IPv6 interfaces */ 1520Sstevel@tonic-gate 1530Sstevel@tonic-gate static int nprobes = 3; /* number of probes */ 1540Sstevel@tonic-gate static int max_ttl = 30; /* max number of hops */ 1550Sstevel@tonic-gate static int first_ttl = 1; /* initial number of hops */ 1560Sstevel@tonic-gate ushort_t ident; /* used to authenticate replies */ 1570Sstevel@tonic-gate ushort_t port = 32768 + 666; /* start udp dest port # for probe packets */ 1580Sstevel@tonic-gate 1590Sstevel@tonic-gate static int options = 0; /* socket options */ 1600Sstevel@tonic-gate boolean_t verbose = _B_FALSE; /* verbose output */ 1610Sstevel@tonic-gate static int waittime = 5; /* time to wait for response (in seconds) */ 1620Sstevel@tonic-gate static struct timeval delay = {0, 0}; /* delay between consecutive probe */ 1630Sstevel@tonic-gate boolean_t nflag = _B_FALSE; /* print addresses numerically */ 1640Sstevel@tonic-gate static boolean_t showttl = _B_FALSE; /* print the ttl(hop limit) of recvd pkt */ 1650Sstevel@tonic-gate boolean_t useicmp = _B_FALSE; /* use icmp echo instead of udp packets */ 1660Sstevel@tonic-gate boolean_t docksum = _B_TRUE; /* calculate checksums */ 1670Sstevel@tonic-gate static boolean_t collect_stat = _B_FALSE; /* print statistics */ 1680Sstevel@tonic-gate boolean_t settos = _B_FALSE; /* set type-of-service field */ 1690Sstevel@tonic-gate static int max_timeout = 5; /* quit after this consecutive timeouts */ 1700Sstevel@tonic-gate static boolean_t probe_all = _B_FALSE; /* probe all the IFs of the target */ 1710Sstevel@tonic-gate static boolean_t pick_src = _B_FALSE; /* traceroute picks the src address */ 1720Sstevel@tonic-gate 1730Sstevel@tonic-gate /* 1740Sstevel@tonic-gate * flow and class are specific to IPv6, tos and off are specific to IPv4. 1750Sstevel@tonic-gate * Each protocol uses the ones that are specific to itself, and ignores 1760Sstevel@tonic-gate * others. 1770Sstevel@tonic-gate */ 1780Sstevel@tonic-gate static uint_t flow = 0; /* IPv6 flow info */ 1790Sstevel@tonic-gate static uint_t class = 0; /* IPv6 class */ 1800Sstevel@tonic-gate uchar_t tos = 0; /* IPv4 type-of-service */ 1810Sstevel@tonic-gate ushort_t off = 0; /* set DF bit */ 1820Sstevel@tonic-gate 1830Sstevel@tonic-gate static jmp_buf env; /* stack environment for longjmp() */ 1840Sstevel@tonic-gate boolean_t raw_req; /* if sndsock for IPv4 must be raw */ 1850Sstevel@tonic-gate 1860Sstevel@tonic-gate /* Forwards */ 1870Sstevel@tonic-gate static uint_t calc_packetlen(int, struct pr_set *); 1880Sstevel@tonic-gate extern int check_reply(struct msghdr *, int, int, uchar_t *, uchar_t *); 1890Sstevel@tonic-gate extern int check_reply6(struct msghdr *, int, int, uchar_t *, uchar_t *); 1900Sstevel@tonic-gate static double deltaT(struct timeval *, struct timeval *); 1910Sstevel@tonic-gate static char *device_name(struct ifaddrlist *, int, union any_in_addr *, 1920Sstevel@tonic-gate struct pr_set *); 1930Sstevel@tonic-gate extern void *find_ancillary_data(struct msghdr *, int, int); 1940Sstevel@tonic-gate static boolean_t has_addr(struct addrinfo *, union any_in_addr *); 1950Sstevel@tonic-gate static struct ifaddrlist *find_device(struct ifaddrlist *, int, char *); 1960Sstevel@tonic-gate static struct ifaddrlist *find_ifaddr(struct ifaddrlist *, int, 1970Sstevel@tonic-gate union any_in_addr *, int); 1980Sstevel@tonic-gate static void get_gwaddrs(char **, int, union any_in_addr *, 1990Sstevel@tonic-gate union any_in_addr *, int *, int *); 2000Sstevel@tonic-gate static void get_hostinfo(char *, int, struct addrinfo **); 2010Sstevel@tonic-gate char *inet_name(union any_in_addr *, int); 2020Sstevel@tonic-gate ushort_t in_cksum(ushort_t *, int); 2030Sstevel@tonic-gate extern int ip_hdr_length_v6(ip6_t *, int, uint8_t *); 2040Sstevel@tonic-gate extern char *pr_type(uchar_t); 2050Sstevel@tonic-gate extern char *pr_type6(uchar_t); 2060Sstevel@tonic-gate extern void print_addr(uchar_t *, int, struct sockaddr *); 2070Sstevel@tonic-gate extern void print_addr6(uchar_t *, int, struct sockaddr *); 2080Sstevel@tonic-gate extern boolean_t print_icmp_other(uchar_t, uchar_t); 2090Sstevel@tonic-gate extern boolean_t print_icmp_other6(uchar_t, uchar_t); 2100Sstevel@tonic-gate static void print_stats(int, int, double, double, double, double); 2110Sstevel@tonic-gate static void print_unknown_host_msg(const char *, const char *); 2120Sstevel@tonic-gate static void record_stats(double, int *, double *, double *, double *, double *); 2130Sstevel@tonic-gate static void resolve_nodes(int *, struct addrinfo **); 2140Sstevel@tonic-gate static void select_src_addr(union any_in_addr *, union any_in_addr *, int); 2150Sstevel@tonic-gate extern void send_probe(int, struct sockaddr *, struct ip *, int, int, 2160Sstevel@tonic-gate struct timeval *, int); 2170Sstevel@tonic-gate extern void send_probe6(int, struct msghdr *, struct ip *, int, int, 2180Sstevel@tonic-gate struct timeval *, int); 2190Sstevel@tonic-gate extern void set_ancillary_data(struct msghdr *, int, union any_in_addr *, int, 2200Sstevel@tonic-gate uint_t); 2210Sstevel@tonic-gate extern struct ip *set_buffers(int); 2220Sstevel@tonic-gate extern struct ip *set_buffers6(int); 2230Sstevel@tonic-gate extern void set_IPv4opt_sourcerouting(int, union any_in_addr *, 2240Sstevel@tonic-gate union any_in_addr *); 2250Sstevel@tonic-gate static void set_sin(struct sockaddr *, union any_in_addr *, int); 2260Sstevel@tonic-gate static int set_src_addr(struct pr_set *, struct ifaddrlist **); 2270Sstevel@tonic-gate static void setup_protocol(struct pr_set *, int); 2280Sstevel@tonic-gate static void setup_socket(struct pr_set *, int); 2290Sstevel@tonic-gate static void sig_handler(int); 2300Sstevel@tonic-gate static int str2int(const char *, const char *, int, int); 2310Sstevel@tonic-gate static double str2dbl(const char *, const char *, double, double); 2320Sstevel@tonic-gate static void trace_it(struct addrinfo *); 2330Sstevel@tonic-gate static void traceroute(union any_in_addr *, struct msghdr *, struct pr_set *, 2340Sstevel@tonic-gate int, struct ifaddrlist *); 2350Sstevel@tonic-gate static void tv_sub(struct timeval *, struct timeval *); 2360Sstevel@tonic-gate static void usage(void); 2370Sstevel@tonic-gate static int wait_for_reply(int, struct msghdr *, struct timeval *); 2380Sstevel@tonic-gate static double xsqrt(double); 2390Sstevel@tonic-gate 2400Sstevel@tonic-gate /* 2410Sstevel@tonic-gate * main 2420Sstevel@tonic-gate */ 243443Sja97890 int 2440Sstevel@tonic-gate main(int argc, char **argv) 2450Sstevel@tonic-gate { 2460Sstevel@tonic-gate struct addrinfo *ai_dst = NULL; /* destination host */ 2470Sstevel@tonic-gate /* 2480Sstevel@tonic-gate * "probing_successful" indicates if we could successfully send probes, 2490Sstevel@tonic-gate * not necessarily received reply from the target (this behavior is from 2500Sstevel@tonic-gate * the original traceroute). It's _B_FALSE if packlen is invalid, or no 2510Sstevel@tonic-gate * interfaces found. 2520Sstevel@tonic-gate */ 2530Sstevel@tonic-gate boolean_t probing_successful = _B_FALSE; 2540Sstevel@tonic-gate int longjmp_return; /* return value from longjump */ 2550Sstevel@tonic-gate int i = 0; 2560Sstevel@tonic-gate char *cp; 2570Sstevel@tonic-gate int op; 2580Sstevel@tonic-gate char *ep; 2590Sstevel@tonic-gate char temp_buf[INET6_ADDRSTRLEN]; /* use for inet_ntop() */ 2600Sstevel@tonic-gate double pause; 2610Sstevel@tonic-gate 2620Sstevel@tonic-gate /* 2630Sstevel@tonic-gate * A raw socket will be used for IPv4 if there is sufficient 2640Sstevel@tonic-gate * privilege. 2650Sstevel@tonic-gate */ 2660Sstevel@tonic-gate raw_req = priv_ineffect(PRIV_NET_RAWACCESS); 2670Sstevel@tonic-gate 2680Sstevel@tonic-gate /* 2690Sstevel@tonic-gate * We'll need the privilege only when we open the sockets; that's 2700Sstevel@tonic-gate * when we'll fail if the program has insufficient privileges. 2710Sstevel@tonic-gate */ 2720Sstevel@tonic-gate (void) __init_suid_priv(PU_CLEARLIMITSET, PRIV_NET_ICMPACCESS, 2730Sstevel@tonic-gate raw_req ? PRIV_NET_RAWACCESS : NULL, NULL); 2740Sstevel@tonic-gate 2750Sstevel@tonic-gate (void) setlinebuf(stdout); 2760Sstevel@tonic-gate 2770Sstevel@tonic-gate if ((cp = strrchr(argv[0], '/')) != NULL) 2780Sstevel@tonic-gate prog = cp + 1; 2790Sstevel@tonic-gate else 2800Sstevel@tonic-gate prog = argv[0]; 2810Sstevel@tonic-gate 2820Sstevel@tonic-gate opterr = 0; 2830Sstevel@tonic-gate while ((op = getopt(argc, argv, "adFIlnrSvxA:c:f:g:i:L:m:P:p:Q:q:s:" 2840Sstevel@tonic-gate "t:w:")) != EOF) { 2850Sstevel@tonic-gate switch (op) { 2860Sstevel@tonic-gate case 'A': 2870Sstevel@tonic-gate if (strcmp(optarg, "inet") == 0) { 2880Sstevel@tonic-gate family_input = AF_INET; 2890Sstevel@tonic-gate } else if (strcmp(optarg, "inet6") == 0) { 2900Sstevel@tonic-gate family_input = AF_INET6; 2910Sstevel@tonic-gate } else { 2920Sstevel@tonic-gate Fprintf(stderr, 2930Sstevel@tonic-gate "%s: unknown address family %s\n", 2940Sstevel@tonic-gate prog, optarg); 2950Sstevel@tonic-gate exit(EXIT_FAILURE); 2960Sstevel@tonic-gate } 2970Sstevel@tonic-gate break; 2980Sstevel@tonic-gate 2990Sstevel@tonic-gate case 'a': 3000Sstevel@tonic-gate probe_all = _B_TRUE; 3010Sstevel@tonic-gate break; 3020Sstevel@tonic-gate 3030Sstevel@tonic-gate case 'c': 3040Sstevel@tonic-gate class = str2int(optarg, "traffic class", 0, 3050Sstevel@tonic-gate MAX_TRAFFIC_CLASS); 3060Sstevel@tonic-gate break; 3070Sstevel@tonic-gate 3080Sstevel@tonic-gate case 'd': 3090Sstevel@tonic-gate options |= SO_DEBUG; 3100Sstevel@tonic-gate break; 3110Sstevel@tonic-gate 3120Sstevel@tonic-gate case 'f': 3130Sstevel@tonic-gate first_ttl = str2int(optarg, "first ttl", 1, MAXTTL); 3140Sstevel@tonic-gate break; 3150Sstevel@tonic-gate 3160Sstevel@tonic-gate case 'F': 3170Sstevel@tonic-gate off = IP_DF; 3180Sstevel@tonic-gate break; 3190Sstevel@tonic-gate 3200Sstevel@tonic-gate case 'g': 3210Sstevel@tonic-gate if (!raw_req) { 3220Sstevel@tonic-gate Fprintf(stderr, 3230Sstevel@tonic-gate "%s: privilege to specify a loose source " 3240Sstevel@tonic-gate "route gateway is unavailable\n", 3250Sstevel@tonic-gate prog); 3260Sstevel@tonic-gate exit(EXIT_FAILURE); 3270Sstevel@tonic-gate } 328378Sblu if (gw_count >= MAXMAX_GWS) { 3290Sstevel@tonic-gate Fprintf(stderr, 3300Sstevel@tonic-gate "%s: Too many gateways\n", prog); 3310Sstevel@tonic-gate exit(EXIT_FAILURE); 3320Sstevel@tonic-gate } 3330Sstevel@tonic-gate gwlist[gw_count] = strdup(optarg); 3340Sstevel@tonic-gate if (gwlist[gw_count] == NULL) { 3350Sstevel@tonic-gate Fprintf(stderr, "%s: strdup %s\n", prog, 3360Sstevel@tonic-gate strerror(errno)); 3370Sstevel@tonic-gate exit(EXIT_FAILURE); 3380Sstevel@tonic-gate } 3390Sstevel@tonic-gate 3400Sstevel@tonic-gate ++gw_count; 3410Sstevel@tonic-gate break; 3420Sstevel@tonic-gate 3430Sstevel@tonic-gate case 'l': 3440Sstevel@tonic-gate showttl = _B_TRUE; 3450Sstevel@tonic-gate break; 3460Sstevel@tonic-gate 3470Sstevel@tonic-gate case 'i': 3480Sstevel@tonic-gate /* this can be IF name or IF index */ 3490Sstevel@tonic-gate if_index = (uint_t)strtol(optarg, &ep, 10); 3500Sstevel@tonic-gate 3510Sstevel@tonic-gate /* convert IF index <--> IF name */ 3520Sstevel@tonic-gate if (errno != 0 || *ep != '\0') { 3530Sstevel@tonic-gate device = optarg; 3540Sstevel@tonic-gate if_index = if_nametoindex((const char *)device); 3550Sstevel@tonic-gate 3560Sstevel@tonic-gate /* 3570Sstevel@tonic-gate * In case it fails, check to see if the problem 3580Sstevel@tonic-gate * is other than "IF not found". 3590Sstevel@tonic-gate */ 3600Sstevel@tonic-gate if (if_index == 0 && errno != ENXIO) { 3610Sstevel@tonic-gate Fprintf(stderr, "%s: if_nametoindex:" 3620Sstevel@tonic-gate "%s\n", prog, strerror(errno)); 3630Sstevel@tonic-gate exit(EXIT_FAILURE); 3640Sstevel@tonic-gate } 3650Sstevel@tonic-gate } else { 3660Sstevel@tonic-gate device = (char *)malloc(LIFNAMSIZ + 1); 3670Sstevel@tonic-gate if (device == NULL) { 3680Sstevel@tonic-gate Fprintf(stderr, "%s: malloc: %s\n", 3690Sstevel@tonic-gate prog, strerror(errno)); 3700Sstevel@tonic-gate exit(EXIT_FAILURE); 3710Sstevel@tonic-gate } 3720Sstevel@tonic-gate 3730Sstevel@tonic-gate device = if_indextoname(if_index, device); 3740Sstevel@tonic-gate if (device != NULL) { 3750Sstevel@tonic-gate device[LIFNAMSIZ] = '\0'; 3760Sstevel@tonic-gate } else if (errno != ENXIO) { 3770Sstevel@tonic-gate /* 3780Sstevel@tonic-gate * The problem was other than "index 3790Sstevel@tonic-gate * not found". 3800Sstevel@tonic-gate */ 3810Sstevel@tonic-gate Fprintf(stderr, "%s: if_indextoname:" 3820Sstevel@tonic-gate "%s\n", prog, strerror(errno)); 3830Sstevel@tonic-gate exit(EXIT_FAILURE); 3840Sstevel@tonic-gate } 3850Sstevel@tonic-gate } 3860Sstevel@tonic-gate 3870Sstevel@tonic-gate if (device == NULL || if_index == 0) { 3880Sstevel@tonic-gate Fprintf(stderr, "%s: interface %s " 3890Sstevel@tonic-gate "doesn't match any actual interfaces\n", 3900Sstevel@tonic-gate prog, optarg); 3910Sstevel@tonic-gate exit(EXIT_FAILURE); 3920Sstevel@tonic-gate } 3930Sstevel@tonic-gate break; 3940Sstevel@tonic-gate 3950Sstevel@tonic-gate case 'I': 3960Sstevel@tonic-gate useicmp = _B_TRUE; 3970Sstevel@tonic-gate break; 3980Sstevel@tonic-gate 3990Sstevel@tonic-gate case 'L': 4000Sstevel@tonic-gate flow = str2int(optarg, "flow label", 0, MAX_FLOW_LABEL); 4010Sstevel@tonic-gate break; 4020Sstevel@tonic-gate 4030Sstevel@tonic-gate case 'm': 4040Sstevel@tonic-gate max_ttl = str2int(optarg, "max ttl(hop limit)", 1, 4050Sstevel@tonic-gate MAXTTL); 4060Sstevel@tonic-gate break; 4070Sstevel@tonic-gate 4080Sstevel@tonic-gate case 'n': 4090Sstevel@tonic-gate nflag = _B_TRUE; 4100Sstevel@tonic-gate break; 4110Sstevel@tonic-gate 4120Sstevel@tonic-gate case 'P': 4130Sstevel@tonic-gate pause = str2dbl(optarg, "pause", 0, INT_MAX); 4140Sstevel@tonic-gate delay.tv_sec = (time_t)pause; 4150Sstevel@tonic-gate delay.tv_usec = (suseconds_t)((pause - delay.tv_sec) * 4160Sstevel@tonic-gate 1000000); 4170Sstevel@tonic-gate break; 4180Sstevel@tonic-gate 4190Sstevel@tonic-gate case 'p': 4200Sstevel@tonic-gate port = str2int(optarg, "port", 1, MAX_PORT); 4210Sstevel@tonic-gate break; 4220Sstevel@tonic-gate 4230Sstevel@tonic-gate case 'Q': 4240Sstevel@tonic-gate max_timeout = str2int(optarg, "max timeout", 1, -1); 4250Sstevel@tonic-gate break; 4260Sstevel@tonic-gate 4270Sstevel@tonic-gate case 'q': 4280Sstevel@tonic-gate nprobes = str2int(optarg, "nprobes", 1, -1); 4290Sstevel@tonic-gate break; 4300Sstevel@tonic-gate 4310Sstevel@tonic-gate case 'r': 4320Sstevel@tonic-gate options |= SO_DONTROUTE; 4330Sstevel@tonic-gate break; 4340Sstevel@tonic-gate 4350Sstevel@tonic-gate case 'S': 4360Sstevel@tonic-gate collect_stat = _B_TRUE; 4370Sstevel@tonic-gate break; 4380Sstevel@tonic-gate 4390Sstevel@tonic-gate case 's': 4400Sstevel@tonic-gate /* 4410Sstevel@tonic-gate * set the ip source address of the outbound 4420Sstevel@tonic-gate * probe (e.g., on a multi-homed host). 4430Sstevel@tonic-gate */ 4440Sstevel@tonic-gate source_input = optarg; 4450Sstevel@tonic-gate break; 4460Sstevel@tonic-gate 4470Sstevel@tonic-gate case 't': 4480Sstevel@tonic-gate tos = (uchar_t)str2int(optarg, "tos", 0, MAX_TOS); 4490Sstevel@tonic-gate settos = _B_TRUE; 4500Sstevel@tonic-gate break; 4510Sstevel@tonic-gate 4520Sstevel@tonic-gate case 'v': 4530Sstevel@tonic-gate verbose = _B_TRUE; 4540Sstevel@tonic-gate break; 4550Sstevel@tonic-gate 4560Sstevel@tonic-gate case 'x': 4570Sstevel@tonic-gate docksum = _B_FALSE; 4580Sstevel@tonic-gate break; 4590Sstevel@tonic-gate 4600Sstevel@tonic-gate case 'w': 4610Sstevel@tonic-gate waittime = str2int(optarg, "wait time", 2, -1); 4620Sstevel@tonic-gate break; 4630Sstevel@tonic-gate 4640Sstevel@tonic-gate default: 4650Sstevel@tonic-gate usage(); 4660Sstevel@tonic-gate break; 4670Sstevel@tonic-gate } 4680Sstevel@tonic-gate } 4690Sstevel@tonic-gate 4700Sstevel@tonic-gate /* 4710Sstevel@tonic-gate * If it's probe_all, SIGQUIT makes traceroute exit(). But we set the 4720Sstevel@tonic-gate * address to jump back to in traceroute(). Until then, we'll need to 4730Sstevel@tonic-gate * temporarily specify one. 4740Sstevel@tonic-gate */ 4750Sstevel@tonic-gate if (probe_all) { 4760Sstevel@tonic-gate if ((longjmp_return = setjmp(env)) != 0) { 4770Sstevel@tonic-gate if (longjmp_return == SIGQUIT) { 4780Sstevel@tonic-gate Printf("(exiting)\n"); 4790Sstevel@tonic-gate exit(EXIT_SUCCESS); 4800Sstevel@tonic-gate } else { /* should never happen */ 4810Sstevel@tonic-gate exit(EXIT_FAILURE); 4820Sstevel@tonic-gate } 4830Sstevel@tonic-gate } 4840Sstevel@tonic-gate (void) signal(SIGQUIT, sig_handler); 4850Sstevel@tonic-gate } 4860Sstevel@tonic-gate 4870Sstevel@tonic-gate if ((gw_count > 0) && (options & SO_DONTROUTE)) { 4880Sstevel@tonic-gate Fprintf(stderr, "%s: loose source route gateways (-g)" 4890Sstevel@tonic-gate " cannot be specified when probe packets are sent" 4900Sstevel@tonic-gate " directly to a host on an attached network (-r)\n", 4910Sstevel@tonic-gate prog); 4920Sstevel@tonic-gate exit(EXIT_FAILURE); 4930Sstevel@tonic-gate } 4940Sstevel@tonic-gate 4950Sstevel@tonic-gate i = argc - optind; 4960Sstevel@tonic-gate if (i == 1 || i == 2) { 4970Sstevel@tonic-gate hostname = argv[optind]; 4980Sstevel@tonic-gate 4990Sstevel@tonic-gate if (i == 2) { 5000Sstevel@tonic-gate /* accept any length now, we'll check it later */ 5010Sstevel@tonic-gate packlen_input = str2int(argv[optind + 1], 5020Sstevel@tonic-gate "packet length", 0, -1); 5030Sstevel@tonic-gate } 5040Sstevel@tonic-gate } else { 5050Sstevel@tonic-gate usage(); 5060Sstevel@tonic-gate } 5070Sstevel@tonic-gate 5080Sstevel@tonic-gate if (first_ttl > max_ttl) { 5090Sstevel@tonic-gate Fprintf(stderr, 5100Sstevel@tonic-gate "%s: first ttl(hop limit) (%d) may not be greater" 5110Sstevel@tonic-gate " than max ttl(hop limit) (%d)\n", 5120Sstevel@tonic-gate prog, first_ttl, max_ttl); 5130Sstevel@tonic-gate exit(EXIT_FAILURE); 5140Sstevel@tonic-gate } 5150Sstevel@tonic-gate 5160Sstevel@tonic-gate /* resolve hostnames */ 5170Sstevel@tonic-gate resolve_nodes(&family_input, &ai_dst); 5180Sstevel@tonic-gate if (ai_dst == NULL) { 5190Sstevel@tonic-gate exit(EXIT_FAILURE); 5200Sstevel@tonic-gate } 5210Sstevel@tonic-gate 5220Sstevel@tonic-gate /* 5230Sstevel@tonic-gate * If it's probe_all, SIGINT makes traceroute skip to probing next IP 5240Sstevel@tonic-gate * address of the target. The new interrupt handler is assigned in 5250Sstevel@tonic-gate * traceroute() function. Until then let's ignore the signal. 5260Sstevel@tonic-gate */ 5270Sstevel@tonic-gate if (probe_all) 5280Sstevel@tonic-gate (void) signal(SIGINT, SIG_IGN); 5290Sstevel@tonic-gate 5300Sstevel@tonic-gate ident = (getpid() & 0xffff) | 0x8000; 5310Sstevel@tonic-gate 5320Sstevel@tonic-gate /* 5330Sstevel@tonic-gate * We KNOW that probe_all == TRUE if family is AF_UNSPEC, 5340Sstevel@tonic-gate * since family is set to the specific AF found unless it's 5350Sstevel@tonic-gate * probe_all. So if family == AF_UNSPEC, we need to init pr4 and pr6. 5360Sstevel@tonic-gate */ 5370Sstevel@tonic-gate switch (family_input) { 5380Sstevel@tonic-gate case AF_UNSPEC: 5390Sstevel@tonic-gate pr4 = (struct pr_set *)malloc(sizeof (struct pr_set)); 5400Sstevel@tonic-gate if (pr4 == NULL) { 5410Sstevel@tonic-gate Fprintf(stderr, 5420Sstevel@tonic-gate "%s: malloc %s\n", prog, strerror(errno)); 5430Sstevel@tonic-gate exit(EXIT_FAILURE); 5440Sstevel@tonic-gate } 5450Sstevel@tonic-gate pr6 = (struct pr_set *)malloc(sizeof (struct pr_set)); 5460Sstevel@tonic-gate if (pr6 == NULL) { 5470Sstevel@tonic-gate Fprintf(stderr, 5480Sstevel@tonic-gate "%s: malloc %s\n", prog, strerror(errno)); 5490Sstevel@tonic-gate exit(EXIT_FAILURE); 5500Sstevel@tonic-gate } 5510Sstevel@tonic-gate setup_protocol(pr6, AF_INET6); 5520Sstevel@tonic-gate setup_protocol(pr4, AF_INET); 5530Sstevel@tonic-gate outip6 = (*pr6->set_buffers_fn)(pr6->packlen); 5540Sstevel@tonic-gate setup_socket(pr6, pr6->packlen); 5550Sstevel@tonic-gate 5560Sstevel@tonic-gate outip4 = (*pr4->set_buffers_fn)(pr4->packlen); 5570Sstevel@tonic-gate setup_socket(pr4, pr4->packlen); 5580Sstevel@tonic-gate num_ifs6 = set_src_addr(pr6, &al6); 5590Sstevel@tonic-gate num_ifs4 = set_src_addr(pr4, &al4); 5600Sstevel@tonic-gate break; 5610Sstevel@tonic-gate case AF_INET6: 5620Sstevel@tonic-gate pr6 = (struct pr_set *)malloc(sizeof (struct pr_set)); 5630Sstevel@tonic-gate if (pr6 == NULL) { 5640Sstevel@tonic-gate Fprintf(stderr, 5650Sstevel@tonic-gate "%s: malloc %s\n", prog, strerror(errno)); 5660Sstevel@tonic-gate exit(EXIT_FAILURE); 5670Sstevel@tonic-gate } 5680Sstevel@tonic-gate setup_protocol(pr6, AF_INET6); 5690Sstevel@tonic-gate outip6 = (*pr6->set_buffers_fn)(pr6->packlen); 5700Sstevel@tonic-gate setup_socket(pr6, pr6->packlen); 5710Sstevel@tonic-gate num_ifs6 = set_src_addr(pr6, &al6); 5720Sstevel@tonic-gate break; 5730Sstevel@tonic-gate case AF_INET: 5740Sstevel@tonic-gate pr4 = (struct pr_set *)malloc(sizeof (struct pr_set)); 5750Sstevel@tonic-gate if (pr4 == NULL) { 5760Sstevel@tonic-gate Fprintf(stderr, 5770Sstevel@tonic-gate "%s: malloc %s\n", prog, strerror(errno)); 5780Sstevel@tonic-gate exit(EXIT_FAILURE); 5790Sstevel@tonic-gate } 5800Sstevel@tonic-gate setup_protocol(pr4, AF_INET); 5810Sstevel@tonic-gate outip4 = (*pr4->set_buffers_fn)(pr4->packlen); 5820Sstevel@tonic-gate setup_socket(pr4, pr4->packlen); 5830Sstevel@tonic-gate num_ifs4 = set_src_addr(pr4, &al4); 5840Sstevel@tonic-gate break; 5850Sstevel@tonic-gate default: 5860Sstevel@tonic-gate Fprintf(stderr, "%s: unknow address family.\n", prog); 5870Sstevel@tonic-gate exit(EXIT_FAILURE); 5880Sstevel@tonic-gate } 5890Sstevel@tonic-gate 5900Sstevel@tonic-gate if (num_v4 + num_v6 > 1 && !probe_all) { 5910Sstevel@tonic-gate if (ai_dst->ai_family == AF_INET) { 5920Sstevel@tonic-gate Fprintf(stderr, 5930Sstevel@tonic-gate "%s: Warning: %s has multiple addresses;" 5940Sstevel@tonic-gate " using %s\n", prog, hostname, 5950Sstevel@tonic-gate inet_ntop(AF_INET, 5960Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 5970Sstevel@tonic-gate (void *)&((struct sockaddr_in *) 5980Sstevel@tonic-gate ai_dst->ai_addr)->sin_addr, 5990Sstevel@tonic-gate temp_buf, sizeof (temp_buf))); 6000Sstevel@tonic-gate } else { 6010Sstevel@tonic-gate Fprintf(stderr, 6020Sstevel@tonic-gate "%s: Warning: %s has multiple addresses;" 6030Sstevel@tonic-gate " using %s\n", prog, hostname, 6040Sstevel@tonic-gate inet_ntop(AF_INET6, 6050Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 6060Sstevel@tonic-gate (void *)&((struct sockaddr_in6 *) 6070Sstevel@tonic-gate ai_dst->ai_addr)->sin6_addr, 6080Sstevel@tonic-gate temp_buf, sizeof (temp_buf))); 6090Sstevel@tonic-gate } 6100Sstevel@tonic-gate } 6110Sstevel@tonic-gate 6120Sstevel@tonic-gate if (num_ifs4 + num_ifs6 > 0) { 6130Sstevel@tonic-gate trace_it(ai_dst); 6140Sstevel@tonic-gate probing_successful = _B_TRUE; 6150Sstevel@tonic-gate } 6160Sstevel@tonic-gate 6170Sstevel@tonic-gate (void) close(rcvsock4); 6180Sstevel@tonic-gate (void) close(sndsock4); 6190Sstevel@tonic-gate (void) close(rcvsock6); 6200Sstevel@tonic-gate (void) close(sndsock6); 6210Sstevel@tonic-gate 6220Sstevel@tonic-gate /* 6230Sstevel@tonic-gate * if we could probe any of the IP addresses of the target, that means 6240Sstevel@tonic-gate * this was a successful operation 6250Sstevel@tonic-gate */ 6260Sstevel@tonic-gate if (probing_successful) 627443Sja97890 return (EXIT_SUCCESS); 6280Sstevel@tonic-gate else 629443Sja97890 return (EXIT_FAILURE); 6300Sstevel@tonic-gate } 6310Sstevel@tonic-gate 6320Sstevel@tonic-gate /* 6330Sstevel@tonic-gate * print "unknown host" message 6340Sstevel@tonic-gate */ 6350Sstevel@tonic-gate static void 6360Sstevel@tonic-gate print_unknown_host_msg(const char *protocol, const char *host) 6370Sstevel@tonic-gate { 6380Sstevel@tonic-gate Fprintf(stderr, "%s: unknown%s host %s\n", prog, protocol, host); 6390Sstevel@tonic-gate } 6400Sstevel@tonic-gate 6410Sstevel@tonic-gate /* 6420Sstevel@tonic-gate * resolve destination host and gateways 6430Sstevel@tonic-gate */ 6440Sstevel@tonic-gate static void 6450Sstevel@tonic-gate resolve_nodes(int *family, struct addrinfo **ai_dstp) 6460Sstevel@tonic-gate { 6470Sstevel@tonic-gate struct addrinfo *ai_dst = NULL; 6480Sstevel@tonic-gate struct addrinfo *aip = NULL; 6490Sstevel@tonic-gate int num_resolved_gw = 0; 6500Sstevel@tonic-gate int num_resolved_gw6 = 0; 6510Sstevel@tonic-gate 6520Sstevel@tonic-gate get_hostinfo(hostname, *family, &ai_dst); 6530Sstevel@tonic-gate if (ai_dst == NULL) { 6540Sstevel@tonic-gate print_unknown_host_msg("", hostname); 6550Sstevel@tonic-gate exit(EXIT_FAILURE); 6560Sstevel@tonic-gate } 6570Sstevel@tonic-gate /* Get a count of the v4 & v6 addresses */ 6580Sstevel@tonic-gate for (aip = ai_dst; aip != NULL; aip = aip->ai_next) { 6590Sstevel@tonic-gate switch (aip->ai_family) { 6600Sstevel@tonic-gate case AF_INET: 6610Sstevel@tonic-gate num_v4++; 6620Sstevel@tonic-gate break; 6630Sstevel@tonic-gate case AF_INET6: 6640Sstevel@tonic-gate num_v6++; 6650Sstevel@tonic-gate break; 6660Sstevel@tonic-gate } 6670Sstevel@tonic-gate } 6680Sstevel@tonic-gate 6690Sstevel@tonic-gate if (*family == AF_UNSPEC && !probe_all) { 6700Sstevel@tonic-gate *family = ai_dst->ai_family; 6710Sstevel@tonic-gate } 6720Sstevel@tonic-gate 6730Sstevel@tonic-gate /* resolve gateways */ 6740Sstevel@tonic-gate if (gw_count > 0) { 6750Sstevel@tonic-gate get_gwaddrs(gwlist, *family, gwIPlist, gwIP6list, 6760Sstevel@tonic-gate &num_resolved_gw, &num_resolved_gw6); 6770Sstevel@tonic-gate 6780Sstevel@tonic-gate /* we couldn't resolve a gateway as an IPv6 host */ 6790Sstevel@tonic-gate if (num_resolved_gw6 != gw_count && num_v6 != 0) { 6800Sstevel@tonic-gate if (*family == AF_INET6 || *family == AF_UNSPEC) 6810Sstevel@tonic-gate print_unknown_host_msg(" IPv6", 6820Sstevel@tonic-gate gwlist[num_resolved_gw6]); 6830Sstevel@tonic-gate num_v6 = 0; 6840Sstevel@tonic-gate } 6850Sstevel@tonic-gate 6860Sstevel@tonic-gate /* we couldn't resolve a gateway as an IPv4 host */ 6870Sstevel@tonic-gate if (num_resolved_gw != gw_count && num_v4 != 0) { 6880Sstevel@tonic-gate if (*family == AF_INET || *family == AF_UNSPEC) 6890Sstevel@tonic-gate print_unknown_host_msg(" IPv4", 6900Sstevel@tonic-gate gwlist[num_resolved_gw]); 6910Sstevel@tonic-gate num_v4 = 0; 6920Sstevel@tonic-gate } 6930Sstevel@tonic-gate } 6940Sstevel@tonic-gate 695378Sblu *ai_dstp = (num_v4 + num_v6 > 0) ? ai_dst : NULL; 6960Sstevel@tonic-gate } 6970Sstevel@tonic-gate 6980Sstevel@tonic-gate /* 6990Sstevel@tonic-gate * Given IP address or hostname, return v4 and v6 hostinfo lists. 7000Sstevel@tonic-gate * Assumes that hostinfo ** ptrs are non-null. 7010Sstevel@tonic-gate */ 7020Sstevel@tonic-gate static void 7030Sstevel@tonic-gate get_hostinfo(char *host, int family, struct addrinfo **aipp) 7040Sstevel@tonic-gate { 7050Sstevel@tonic-gate struct addrinfo hints, *ai; 7060Sstevel@tonic-gate struct in6_addr addr6; 7070Sstevel@tonic-gate struct in_addr addr; 708*8485SPeter.Memishian@Sun.COM char abuf[INET6_ADDRSTRLEN]; /* use for inet_ntop() */ 7090Sstevel@tonic-gate int rc; 7100Sstevel@tonic-gate 7110Sstevel@tonic-gate /* 7120Sstevel@tonic-gate * Take care of v4-mapped addresses. It should run same as v4, after 7130Sstevel@tonic-gate * chopping off the prefix, leaving the IPv4 address 7140Sstevel@tonic-gate */ 7150Sstevel@tonic-gate if ((inet_pton(AF_INET6, host, &addr6) > 0) && 7160Sstevel@tonic-gate IN6_IS_ADDR_V4MAPPED(&addr6)) { 7170Sstevel@tonic-gate /* peel off the "mapping" stuff, leaving 32 bit IPv4 address */ 7180Sstevel@tonic-gate IN6_V4MAPPED_TO_INADDR(&addr6, &addr); 7190Sstevel@tonic-gate 7200Sstevel@tonic-gate /* convert it back to a string */ 721*8485SPeter.Memishian@Sun.COM (void) inet_ntop(AF_INET, &addr, abuf, sizeof (abuf)); 7220Sstevel@tonic-gate 7230Sstevel@tonic-gate /* now the host is an IPv4 address */ 724*8485SPeter.Memishian@Sun.COM (void) strcpy(host, abuf); 7250Sstevel@tonic-gate 7260Sstevel@tonic-gate /* 7270Sstevel@tonic-gate * If it's a mapped address, we convert it into IPv4 7280Sstevel@tonic-gate * address because traceroute will send and receive IPv4 7290Sstevel@tonic-gate * packets for that address. Therefore, it's a failure case to 7300Sstevel@tonic-gate * ask get_hostinfo() to treat a mapped address as an IPv6 7310Sstevel@tonic-gate * address. 7320Sstevel@tonic-gate */ 7330Sstevel@tonic-gate if (family == AF_INET6) { 7340Sstevel@tonic-gate return; 7350Sstevel@tonic-gate } 7360Sstevel@tonic-gate } 7370Sstevel@tonic-gate 7380Sstevel@tonic-gate (void) memset(&hints, 0, sizeof (hints)); 7390Sstevel@tonic-gate hints.ai_family = family; 7401122Sdme hints.ai_flags = AI_ADDRCONFIG | AI_CANONNAME; 7410Sstevel@tonic-gate rc = getaddrinfo(host, NULL, &hints, &ai); 7420Sstevel@tonic-gate if (rc != 0) { 7430Sstevel@tonic-gate if (rc != EAI_NONAME) 7440Sstevel@tonic-gate Fprintf(stderr, "%s: getaddrinfo: %s\n", prog, 7450Sstevel@tonic-gate gai_strerror(rc)); 746378Sblu *aipp = NULL; 7470Sstevel@tonic-gate return; 7480Sstevel@tonic-gate } 7490Sstevel@tonic-gate *aipp = ai; 7500Sstevel@tonic-gate } 7510Sstevel@tonic-gate 7520Sstevel@tonic-gate /* 7530Sstevel@tonic-gate * Calculate the packet length to be used, and check against the valid range. 7540Sstevel@tonic-gate * Returns -1 if range check fails. 7550Sstevel@tonic-gate */ 7560Sstevel@tonic-gate static uint_t 7570Sstevel@tonic-gate calc_packetlen(int plen_input, struct pr_set *pr) 7580Sstevel@tonic-gate { 7590Sstevel@tonic-gate int minpacket; /* min ip packet size */ 7600Sstevel@tonic-gate int optlen; /* length of ip options */ 7610Sstevel@tonic-gate int plen; 7620Sstevel@tonic-gate 7630Sstevel@tonic-gate /* 7640Sstevel@tonic-gate * LBNL bug fixed: miscalculation of optlen 7650Sstevel@tonic-gate */ 7660Sstevel@tonic-gate if (gw_count > 0) { 7670Sstevel@tonic-gate /* 7680Sstevel@tonic-gate * IPv4: 7690Sstevel@tonic-gate * ---- 7700Sstevel@tonic-gate * 5 (NO OPs) + 3 (code, len, ptr) + gateways 7710Sstevel@tonic-gate * IP options field can hold up to 9 gateways. But the API 7720Sstevel@tonic-gate * allows you to specify only 8, because the last one is the 7730Sstevel@tonic-gate * destination host. When this packet is sent, on the wire 7740Sstevel@tonic-gate * you see one gateway replaced by 4 NO OPs. The other 1 NO 7750Sstevel@tonic-gate * OP is for alignment 7760Sstevel@tonic-gate * 7770Sstevel@tonic-gate * IPv6: 7780Sstevel@tonic-gate * ---- 7790Sstevel@tonic-gate * Well, formula is different, but the result is same. 7800Sstevel@tonic-gate * 8 byte fixed part for Type 0 Routing header, followed by 7810Sstevel@tonic-gate * gateway addresses 7820Sstevel@tonic-gate */ 7830Sstevel@tonic-gate optlen = 8 + gw_count * pr->addr_len; 7840Sstevel@tonic-gate } else { 7850Sstevel@tonic-gate optlen = 0; 7860Sstevel@tonic-gate } 7870Sstevel@tonic-gate 7880Sstevel@tonic-gate /* take care of the packet length calculations and checks */ 7890Sstevel@tonic-gate minpacket = pr->ip_hdr_len + sizeof (struct outdata) + optlen; 7900Sstevel@tonic-gate if (useicmp) 7910Sstevel@tonic-gate minpacket += pr->icmp_minlen; /* minimum ICMP header size */ 7920Sstevel@tonic-gate else 7930Sstevel@tonic-gate minpacket += sizeof (struct udphdr); 7940Sstevel@tonic-gate plen = plen_input; 7950Sstevel@tonic-gate if (plen == 0) { 7960Sstevel@tonic-gate plen = minpacket; /* minimum sized packet */ 7970Sstevel@tonic-gate } else if (minpacket > plen || plen > IP_MAXPACKET) { 7980Sstevel@tonic-gate Fprintf(stderr, "%s: %s packet size must be >= %d and <= %d\n", 7990Sstevel@tonic-gate prog, pr->name, minpacket, IP_MAXPACKET); 8000Sstevel@tonic-gate return (0); 8010Sstevel@tonic-gate } 8020Sstevel@tonic-gate 8030Sstevel@tonic-gate return (plen); 8040Sstevel@tonic-gate } 8050Sstevel@tonic-gate 8060Sstevel@tonic-gate /* 8070Sstevel@tonic-gate * Sets the source address by resolving -i and -s arguments, or if -i and -s 8080Sstevel@tonic-gate * don't dictate any, it sets the pick_src to make sure traceroute uses the 8090Sstevel@tonic-gate * kernel's pick of the source address. 8100Sstevel@tonic-gate * Returns number of interfaces configured on the source host, 0 on error or 8110Sstevel@tonic-gate * there's no interface which is up amd not a loopback. 8120Sstevel@tonic-gate */ 8130Sstevel@tonic-gate static int 8140Sstevel@tonic-gate set_src_addr(struct pr_set *pr, struct ifaddrlist **alp) 8150Sstevel@tonic-gate { 8160Sstevel@tonic-gate union any_in_addr *ap; 8170Sstevel@tonic-gate struct ifaddrlist *al = NULL; 8180Sstevel@tonic-gate struct ifaddrlist *tmp1_al = NULL; 8190Sstevel@tonic-gate struct ifaddrlist *tmp2_al = NULL; 8200Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 8210Sstevel@tonic-gate struct sockaddr_in *sin_from = (struct sockaddr_in *)pr->from; 8220Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 8230Sstevel@tonic-gate struct sockaddr_in6 *sin6_from = (struct sockaddr_in6 *)pr->from; 8240Sstevel@tonic-gate struct addrinfo *aip; 8250Sstevel@tonic-gate char errbuf[ERRBUFSIZE]; 826*8485SPeter.Memishian@Sun.COM char abuf[INET6_ADDRSTRLEN]; /* use for inet_ntop() */ 8270Sstevel@tonic-gate int num_ifs; /* all the interfaces */ 8280Sstevel@tonic-gate int num_src_ifs; /* exclude loopback and down */ 8290Sstevel@tonic-gate int i; 830*8485SPeter.Memishian@Sun.COM uint_t ifaddrflags = 0; 8310Sstevel@tonic-gate 8320Sstevel@tonic-gate source = source_input; 8330Sstevel@tonic-gate 834*8485SPeter.Memishian@Sun.COM if (device != NULL) 835*8485SPeter.Memishian@Sun.COM ifaddrflags |= LIFC_UNDER_IPMP; 836*8485SPeter.Memishian@Sun.COM 8370Sstevel@tonic-gate /* get the interface address list */ 838*8485SPeter.Memishian@Sun.COM num_ifs = ifaddrlist(&al, pr->family, ifaddrflags, errbuf); 8390Sstevel@tonic-gate if (num_ifs < 0) { 8400Sstevel@tonic-gate Fprintf(stderr, "%s: ifaddrlist: %s\n", prog, errbuf); 8410Sstevel@tonic-gate exit(EXIT_FAILURE); 8420Sstevel@tonic-gate } 8430Sstevel@tonic-gate 8440Sstevel@tonic-gate num_src_ifs = 0; 8450Sstevel@tonic-gate for (i = 0; i < num_ifs; i++) { 8460Sstevel@tonic-gate if (!(al[i].flags & IFF_LOOPBACK) && (al[i].flags & IFF_UP)) 8470Sstevel@tonic-gate num_src_ifs++; 8480Sstevel@tonic-gate } 8490Sstevel@tonic-gate 8500Sstevel@tonic-gate if (num_src_ifs == 0) { 8510Sstevel@tonic-gate Fprintf(stderr, "%s: can't find any %s network interfaces\n", 8520Sstevel@tonic-gate prog, pr->name); 8530Sstevel@tonic-gate return (0); 8540Sstevel@tonic-gate } 8550Sstevel@tonic-gate 8560Sstevel@tonic-gate /* verify the device */ 8570Sstevel@tonic-gate if (device != NULL) { 8580Sstevel@tonic-gate tmp1_al = find_device(al, num_ifs, device); 8590Sstevel@tonic-gate 8600Sstevel@tonic-gate if (tmp1_al == NULL) { 8610Sstevel@tonic-gate Fprintf(stderr, "%s: %s (index %d) is an invalid %s" 8620Sstevel@tonic-gate " interface\n", prog, device, if_index, pr->name); 8630Sstevel@tonic-gate free(al); 8640Sstevel@tonic-gate return (0); 8650Sstevel@tonic-gate } 8660Sstevel@tonic-gate } 8670Sstevel@tonic-gate 8680Sstevel@tonic-gate /* verify the source address */ 8690Sstevel@tonic-gate if (source != NULL) { 8700Sstevel@tonic-gate get_hostinfo(source, pr->family, &aip); 8710Sstevel@tonic-gate if (aip == NULL) { 8720Sstevel@tonic-gate Fprintf(stderr, 8730Sstevel@tonic-gate "%s: %s is an invalid %s source address\n", 8740Sstevel@tonic-gate prog, source, pr->name); 8750Sstevel@tonic-gate 8760Sstevel@tonic-gate free(al); 8770Sstevel@tonic-gate return (0); 8780Sstevel@tonic-gate } 8790Sstevel@tonic-gate 8800Sstevel@tonic-gate source = aip->ai_canonname; 8811122Sdme 8821122Sdme if (pr->family == AF_INET) 8831122Sdme ap = (union any_in_addr *) 8841122Sdme /* LINTED E_BAD_PTR_CAST_ALIGN */ 885*8485SPeter.Memishian@Sun.COM &((struct sockaddr_in *)aip->ai_addr)->sin_addr; 8861122Sdme else 8871122Sdme ap = (union any_in_addr *) 8881122Sdme /* LINTED E_BAD_PTR_CAST_ALIGN */ 889*8485SPeter.Memishian@Sun.COM &((struct sockaddr_in6 *)aip->ai_addr)->sin6_addr; 8900Sstevel@tonic-gate 8910Sstevel@tonic-gate /* 8920Sstevel@tonic-gate * LBNL bug fixed: used to accept any src address 8930Sstevel@tonic-gate */ 8940Sstevel@tonic-gate tmp2_al = find_ifaddr(al, num_ifs, ap, pr->family); 8950Sstevel@tonic-gate if (tmp2_al == NULL) { 896*8485SPeter.Memishian@Sun.COM (void) inet_ntop(pr->family, ap, abuf, sizeof (abuf)); 897*8485SPeter.Memishian@Sun.COM Fprintf(stderr, "%s: %s is not a local %s address\n", 898*8485SPeter.Memishian@Sun.COM prog, abuf, pr->name); 8990Sstevel@tonic-gate free(al); 9000Sstevel@tonic-gate freeaddrinfo(aip); 9010Sstevel@tonic-gate return (0); 9020Sstevel@tonic-gate } 9030Sstevel@tonic-gate } 9040Sstevel@tonic-gate 9050Sstevel@tonic-gate pick_src = _B_FALSE; 9060Sstevel@tonic-gate 9070Sstevel@tonic-gate if (source == NULL) { /* no -s used */ 9080Sstevel@tonic-gate if (device == NULL) { /* no -i used, no -s used */ 9090Sstevel@tonic-gate pick_src = _B_TRUE; 9100Sstevel@tonic-gate } else { /* -i used, no -s used */ 9110Sstevel@tonic-gate /* 9120Sstevel@tonic-gate * -i used, but not -s, and it's IPv4: set the source 9130Sstevel@tonic-gate * address to whatever the interface has configured on 9140Sstevel@tonic-gate * it. 9150Sstevel@tonic-gate */ 9160Sstevel@tonic-gate if (pr->family == AF_INET) 9170Sstevel@tonic-gate set_sin(pr->from, &(tmp1_al->addr), pr->family); 9180Sstevel@tonic-gate else 9190Sstevel@tonic-gate pick_src = _B_TRUE; 9200Sstevel@tonic-gate } 9210Sstevel@tonic-gate } else { /* -s used */ 9220Sstevel@tonic-gate if (device == NULL) { /* no -i used, -s used */ 9230Sstevel@tonic-gate set_sin(pr->from, ap, pr->family); 9240Sstevel@tonic-gate 9250Sstevel@tonic-gate if (aip->ai_next != NULL) { 926*8485SPeter.Memishian@Sun.COM (void) inet_ntop(pr->family, pr->from_sin_addr, 927*8485SPeter.Memishian@Sun.COM abuf, sizeof (abuf)); 928*8485SPeter.Memishian@Sun.COM Fprintf(stderr, "%s: Warning: %s has multiple " 929*8485SPeter.Memishian@Sun.COM "addresses; using %s\n", prog, source, 930*8485SPeter.Memishian@Sun.COM abuf); 9310Sstevel@tonic-gate } 9320Sstevel@tonic-gate } else { /* -i and -s used */ 9330Sstevel@tonic-gate /* 9340Sstevel@tonic-gate * Make sure the source specified matches the 9350Sstevel@tonic-gate * interface address. You only care about this for IPv4 9360Sstevel@tonic-gate * IPv6 can handle IF not matching src address 9370Sstevel@tonic-gate */ 9380Sstevel@tonic-gate if (pr->family == AF_INET) { 9390Sstevel@tonic-gate if (!has_addr(aip, &tmp1_al->addr)) { 9400Sstevel@tonic-gate Fprintf(stderr, 9410Sstevel@tonic-gate "%s: %s is not on interface %s\n", 9420Sstevel@tonic-gate prog, source, device); 9430Sstevel@tonic-gate exit(EXIT_FAILURE); 9440Sstevel@tonic-gate } 9450Sstevel@tonic-gate /* 9460Sstevel@tonic-gate * make sure we use the one matching the 9470Sstevel@tonic-gate * interface's address 9480Sstevel@tonic-gate */ 9490Sstevel@tonic-gate *ap = tmp1_al->addr; 9500Sstevel@tonic-gate } 9510Sstevel@tonic-gate 9520Sstevel@tonic-gate set_sin(pr->from, ap, pr->family); 9530Sstevel@tonic-gate } 9540Sstevel@tonic-gate } 9550Sstevel@tonic-gate 9560Sstevel@tonic-gate /* 9570Sstevel@tonic-gate * Binding at this point will set the source address to be used 9580Sstevel@tonic-gate * for both IPv4 (when raw IP datagrams are not required) and 9590Sstevel@tonic-gate * IPv6. If the address being bound to is zero, then the kernel 9600Sstevel@tonic-gate * will end up choosing the source address when the datagram is 9610Sstevel@tonic-gate * sent. 9620Sstevel@tonic-gate * 9630Sstevel@tonic-gate * For raw IPv4 datagrams, the source address is initialized 9640Sstevel@tonic-gate * within traceroute() along with the outbound destination 9650Sstevel@tonic-gate * address. 9660Sstevel@tonic-gate */ 9670Sstevel@tonic-gate if (pr->family == AF_INET && !raw_req) { 9680Sstevel@tonic-gate sin_from->sin_family = AF_INET; 9690Sstevel@tonic-gate sin_from->sin_port = htons(ident); 9700Sstevel@tonic-gate if (bind(sndsock4, (struct sockaddr *)pr->from, 9710Sstevel@tonic-gate sizeof (struct sockaddr_in)) < 0) { 9720Sstevel@tonic-gate Fprintf(stderr, "%s: bind: %s\n", prog, 9730Sstevel@tonic-gate strerror(errno)); 9740Sstevel@tonic-gate exit(EXIT_FAILURE); 9750Sstevel@tonic-gate } 9760Sstevel@tonic-gate } else if (pr->family == AF_INET6) { 9770Sstevel@tonic-gate sin6_from->sin6_family = AF_INET6; 9780Sstevel@tonic-gate sin6_from->sin6_port = htons(ident); 9790Sstevel@tonic-gate if (bind(sndsock6, (struct sockaddr *)pr->from, 9800Sstevel@tonic-gate sizeof (struct sockaddr_in6)) < 0) { 9810Sstevel@tonic-gate Fprintf(stderr, "%s: bind: %s\n", prog, 9820Sstevel@tonic-gate strerror(errno)); 9830Sstevel@tonic-gate exit(EXIT_FAILURE); 9840Sstevel@tonic-gate } 9850Sstevel@tonic-gate 9860Sstevel@tonic-gate whereto6.sin6_flowinfo = htonl((class << 20) | flow); 9870Sstevel@tonic-gate } 9880Sstevel@tonic-gate *alp = al; 9890Sstevel@tonic-gate return (num_ifs); 9900Sstevel@tonic-gate } 9910Sstevel@tonic-gate 9920Sstevel@tonic-gate /* 9930Sstevel@tonic-gate * Returns the complete ifaddrlist structure matching the desired interface 9940Sstevel@tonic-gate * address. Ignores interfaces which are either down or loopback. 9950Sstevel@tonic-gate */ 9960Sstevel@tonic-gate static struct ifaddrlist * 9970Sstevel@tonic-gate find_ifaddr(struct ifaddrlist *al, int len, union any_in_addr *addr, 9980Sstevel@tonic-gate int family) 9990Sstevel@tonic-gate { 10000Sstevel@tonic-gate struct ifaddrlist *tmp_al = al; 10010Sstevel@tonic-gate int i; 10020Sstevel@tonic-gate size_t addr_len = (family == AF_INET) ? sizeof (struct in_addr) : 10030Sstevel@tonic-gate sizeof (struct in6_addr); 10040Sstevel@tonic-gate 10050Sstevel@tonic-gate for (i = 0; i < len; i++, tmp_al++) { 10060Sstevel@tonic-gate if ((!(tmp_al->flags & IFF_LOOPBACK) && 10070Sstevel@tonic-gate (tmp_al->flags & IFF_UP)) && 10080Sstevel@tonic-gate (memcmp(&tmp_al->addr, addr, addr_len) == 0)) 10090Sstevel@tonic-gate break; 10100Sstevel@tonic-gate } 10110Sstevel@tonic-gate 10120Sstevel@tonic-gate if (i < len) { 10130Sstevel@tonic-gate return (tmp_al); 10140Sstevel@tonic-gate } else { 10150Sstevel@tonic-gate return (NULL); 10160Sstevel@tonic-gate } 10170Sstevel@tonic-gate } 10180Sstevel@tonic-gate 10190Sstevel@tonic-gate /* 10200Sstevel@tonic-gate * Returns the complete ifaddrlist structure matching the desired interface name 10210Sstevel@tonic-gate * Ignores interfaces which are either down or loopback. 10220Sstevel@tonic-gate */ 10230Sstevel@tonic-gate static struct ifaddrlist * 10240Sstevel@tonic-gate find_device(struct ifaddrlist *al, int len, char *device) 10250Sstevel@tonic-gate { 10260Sstevel@tonic-gate struct ifaddrlist *tmp_al = al; 10270Sstevel@tonic-gate int i; 10280Sstevel@tonic-gate 10290Sstevel@tonic-gate for (i = 0; i < len; i++, tmp_al++) { 10300Sstevel@tonic-gate if ((!(tmp_al->flags & IFF_LOOPBACK) && 10310Sstevel@tonic-gate (tmp_al->flags & IFF_UP)) && 10320Sstevel@tonic-gate (strcmp(tmp_al->device, device) == 0)) 10330Sstevel@tonic-gate break; 10340Sstevel@tonic-gate } 10350Sstevel@tonic-gate 10360Sstevel@tonic-gate if (i < len) { 10370Sstevel@tonic-gate return (tmp_al); 10380Sstevel@tonic-gate } else { 10390Sstevel@tonic-gate return (NULL); 10400Sstevel@tonic-gate } 10410Sstevel@tonic-gate } 10420Sstevel@tonic-gate 10430Sstevel@tonic-gate /* 10440Sstevel@tonic-gate * returns _B_TRUE if given hostinfo contains the given address 10450Sstevel@tonic-gate */ 10460Sstevel@tonic-gate static boolean_t 10470Sstevel@tonic-gate has_addr(struct addrinfo *ai, union any_in_addr *addr) 10480Sstevel@tonic-gate { 10490Sstevel@tonic-gate struct addrinfo *ai_tmp = NULL; 10500Sstevel@tonic-gate union any_in_addr *ap; 10510Sstevel@tonic-gate 10520Sstevel@tonic-gate for (ai_tmp = ai; ai_tmp != NULL; ai_tmp = ai_tmp->ai_next) { 10530Sstevel@tonic-gate if (ai_tmp->ai_family == AF_INET6) 10540Sstevel@tonic-gate continue; 10550Sstevel@tonic-gate ap = (union any_in_addr *) 10560Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 10570Sstevel@tonic-gate &((struct sockaddr_in *)ai_tmp->ai_addr)->sin_addr; 10580Sstevel@tonic-gate if (memcmp(ap, addr, sizeof (struct in_addr)) == 0) 10590Sstevel@tonic-gate break; 10600Sstevel@tonic-gate } 10610Sstevel@tonic-gate 10620Sstevel@tonic-gate if (ai_tmp != NULL) { 10630Sstevel@tonic-gate return (_B_TRUE); 10640Sstevel@tonic-gate } else { 10650Sstevel@tonic-gate return (_B_FALSE); 10660Sstevel@tonic-gate } 10670Sstevel@tonic-gate } 10680Sstevel@tonic-gate 10690Sstevel@tonic-gate /* 10700Sstevel@tonic-gate * Resolve the gateway names, splitting results into v4 and v6 lists. 10710Sstevel@tonic-gate * Gateway addresses are added to the appropriate passed-in array; the 10720Sstevel@tonic-gate * number of resolved gateways for each af is returned in resolved[6]. 10730Sstevel@tonic-gate * Assumes that passed-in arrays are large enough for MAX_GWS[6] addrs 10740Sstevel@tonic-gate * and resolved[6] ptrs are non-null; ignores array and counter if the 10750Sstevel@tonic-gate * address family param makes them irrelevant. 10760Sstevel@tonic-gate */ 10770Sstevel@tonic-gate static void 10780Sstevel@tonic-gate get_gwaddrs(char **gwlist, int family, union any_in_addr *gwIPlist, 10790Sstevel@tonic-gate union any_in_addr *gwIPlist6, int *resolved, int *resolved6) 10800Sstevel@tonic-gate { 10810Sstevel@tonic-gate int i; 10820Sstevel@tonic-gate boolean_t check_v4 = _B_TRUE, check_v6 = _B_TRUE; 10830Sstevel@tonic-gate struct addrinfo *ai = NULL; 10840Sstevel@tonic-gate struct addrinfo *aip = NULL; 10850Sstevel@tonic-gate 10860Sstevel@tonic-gate *resolved = *resolved6 = 0; 10870Sstevel@tonic-gate switch (family) { 10880Sstevel@tonic-gate case AF_UNSPEC: 10890Sstevel@tonic-gate break; 10900Sstevel@tonic-gate case AF_INET: 10910Sstevel@tonic-gate check_v6 = _B_FALSE; 10920Sstevel@tonic-gate break; 10930Sstevel@tonic-gate case AF_INET6: 10940Sstevel@tonic-gate check_v4 = _B_FALSE; 10950Sstevel@tonic-gate break; 10960Sstevel@tonic-gate default: 10970Sstevel@tonic-gate return; 10980Sstevel@tonic-gate } 10990Sstevel@tonic-gate 11000Sstevel@tonic-gate if (check_v4 && gw_count >= MAX_GWS) { 11010Sstevel@tonic-gate check_v4 = _B_FALSE; 11020Sstevel@tonic-gate Fprintf(stderr, "%s: too many IPv4 gateways\n", prog); 1103378Sblu num_v4 = 0; 11040Sstevel@tonic-gate } 11050Sstevel@tonic-gate if (check_v6 && gw_count >= MAX_GWS6) { 11060Sstevel@tonic-gate check_v6 = _B_FALSE; 11070Sstevel@tonic-gate Fprintf(stderr, "%s: too many IPv6 gateways\n", prog); 1108378Sblu num_v6 = 0; 11090Sstevel@tonic-gate } 11100Sstevel@tonic-gate 11110Sstevel@tonic-gate for (i = 0; i < gw_count; i++) { 11120Sstevel@tonic-gate if (!check_v4 && !check_v6) 11130Sstevel@tonic-gate return; 11140Sstevel@tonic-gate get_hostinfo(gwlist[i], family, &ai); 11150Sstevel@tonic-gate if (ai == NULL) 11160Sstevel@tonic-gate return; 11170Sstevel@tonic-gate if (check_v4 && num_v4 != 0) { 1118378Sblu check_v4 = _B_FALSE; 11190Sstevel@tonic-gate for (aip = ai; aip != NULL; aip = aip->ai_next) { 11200Sstevel@tonic-gate if (aip->ai_family == AF_INET) { 11210Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 11220Sstevel@tonic-gate bcopy(&((struct sockaddr_in *) 11230Sstevel@tonic-gate aip->ai_addr)->sin_addr, 11240Sstevel@tonic-gate &gwIPlist[i].addr, 11250Sstevel@tonic-gate aip->ai_addrlen); 11260Sstevel@tonic-gate (*resolved)++; 1127378Sblu check_v4 = _B_TRUE; 11280Sstevel@tonic-gate break; 11290Sstevel@tonic-gate } 11300Sstevel@tonic-gate } 11310Sstevel@tonic-gate } else if (check_v4) { 11320Sstevel@tonic-gate check_v4 = _B_FALSE; 11330Sstevel@tonic-gate } 11340Sstevel@tonic-gate if (check_v6 && num_v6 != 0) { 1135378Sblu check_v6 = _B_FALSE; 11360Sstevel@tonic-gate for (aip = ai; aip != NULL; aip = aip->ai_next) { 11370Sstevel@tonic-gate if (aip->ai_family == AF_INET6) { 11380Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 11390Sstevel@tonic-gate bcopy(&((struct sockaddr_in6 *) 11400Sstevel@tonic-gate aip->ai_addr)->sin6_addr, 11410Sstevel@tonic-gate &gwIPlist6[i].addr6, 11420Sstevel@tonic-gate aip->ai_addrlen); 11430Sstevel@tonic-gate (*resolved6)++; 1144378Sblu check_v6 = _B_TRUE; 11450Sstevel@tonic-gate break; 11460Sstevel@tonic-gate } 11470Sstevel@tonic-gate } 11480Sstevel@tonic-gate } else if (check_v6) { 11490Sstevel@tonic-gate check_v6 = _B_FALSE; 11500Sstevel@tonic-gate } 11510Sstevel@tonic-gate } 11520Sstevel@tonic-gate freeaddrinfo(ai); 11530Sstevel@tonic-gate } 11540Sstevel@tonic-gate 11550Sstevel@tonic-gate /* 11560Sstevel@tonic-gate * set protocol specific values here 11570Sstevel@tonic-gate */ 11580Sstevel@tonic-gate static void 11590Sstevel@tonic-gate setup_protocol(struct pr_set *pr, int family) 11600Sstevel@tonic-gate { 11610Sstevel@tonic-gate /* 11620Sstevel@tonic-gate * Set the global variables for each AF. This is going to save us lots 11630Sstevel@tonic-gate * of "if (family == AF_INET)... else .." 11640Sstevel@tonic-gate */ 11650Sstevel@tonic-gate pr->family = family; 11660Sstevel@tonic-gate 11670Sstevel@tonic-gate if (family == AF_INET) { 11680Sstevel@tonic-gate if (!docksum) { 11690Sstevel@tonic-gate Fprintf(stderr, 11700Sstevel@tonic-gate "%s: Warning: checksums disabled\n", prog); 11710Sstevel@tonic-gate } 11720Sstevel@tonic-gate (void) strcpy(pr->name, "IPv4"); 11730Sstevel@tonic-gate (void) strcpy(pr->icmp, "icmp"); 11740Sstevel@tonic-gate pr->icmp_minlen = ICMP_MINLEN; 11750Sstevel@tonic-gate pr->addr_len = sizeof (struct in_addr); 11760Sstevel@tonic-gate pr->ip_hdr_len = sizeof (struct ip); 11770Sstevel@tonic-gate pr->sock_size = sizeof (struct sockaddr_in); 11780Sstevel@tonic-gate pr->to = (struct sockaddr *)&whereto; 11790Sstevel@tonic-gate pr->from = (struct sockaddr *)&wherefrom; 11800Sstevel@tonic-gate pr->from_sin_addr = (void *)&wherefrom.sin_addr; 11810Sstevel@tonic-gate pr->gwIPlist = gwIPlist; 11820Sstevel@tonic-gate pr->set_buffers_fn = set_buffers; 11830Sstevel@tonic-gate pr->check_reply_fn = check_reply; 11840Sstevel@tonic-gate pr->print_icmp_other_fn = print_icmp_other; 11850Sstevel@tonic-gate pr->print_addr_fn = print_addr; 11860Sstevel@tonic-gate pr->packlen = calc_packetlen(packlen_input, pr); 11870Sstevel@tonic-gate } else { 11880Sstevel@tonic-gate (void) strcpy(pr->name, "IPv6"); 11890Sstevel@tonic-gate (void) strcpy(pr->icmp, "ipv6-icmp"); 11900Sstevel@tonic-gate pr->icmp_minlen = ICMP6_MINLEN; 11910Sstevel@tonic-gate pr->addr_len = sizeof (struct in6_addr); 11920Sstevel@tonic-gate pr->ip_hdr_len = sizeof (struct ip6_hdr); 11930Sstevel@tonic-gate pr->sock_size = sizeof (struct sockaddr_in6); 11940Sstevel@tonic-gate pr->to = (struct sockaddr *)&whereto6; 11950Sstevel@tonic-gate pr->from = (struct sockaddr *)&wherefrom6; 11960Sstevel@tonic-gate pr->from_sin_addr = (void *)&wherefrom6.sin6_addr; 11970Sstevel@tonic-gate pr->gwIPlist = gwIP6list; 11980Sstevel@tonic-gate pr->set_buffers_fn = set_buffers6; 11990Sstevel@tonic-gate pr->check_reply_fn = check_reply6; 12000Sstevel@tonic-gate pr->print_icmp_other_fn = print_icmp_other6; 12010Sstevel@tonic-gate pr->print_addr_fn = print_addr6; 12020Sstevel@tonic-gate pr->packlen = calc_packetlen(packlen_input, pr); 12030Sstevel@tonic-gate } 12040Sstevel@tonic-gate if (pr->packlen == 0) 12050Sstevel@tonic-gate exit(EXIT_FAILURE); 12060Sstevel@tonic-gate } 12070Sstevel@tonic-gate 12080Sstevel@tonic-gate /* 12090Sstevel@tonic-gate * setup the sockets for the given protocol's address family 12100Sstevel@tonic-gate */ 12110Sstevel@tonic-gate static void 12120Sstevel@tonic-gate setup_socket(struct pr_set *pr, int packet_len) 12130Sstevel@tonic-gate { 12140Sstevel@tonic-gate int on = 1; 12150Sstevel@tonic-gate struct protoent *pe; 12160Sstevel@tonic-gate int type; 12170Sstevel@tonic-gate int proto; 12180Sstevel@tonic-gate int int_op; 12190Sstevel@tonic-gate int rsock; 12200Sstevel@tonic-gate int ssock; 12210Sstevel@tonic-gate 12220Sstevel@tonic-gate if ((pe = getprotobyname(pr->icmp)) == NULL) { 12230Sstevel@tonic-gate Fprintf(stderr, "%s: unknown protocol %s\n", prog, pr->icmp); 12240Sstevel@tonic-gate exit(EXIT_FAILURE); 12250Sstevel@tonic-gate } 12260Sstevel@tonic-gate 12270Sstevel@tonic-gate /* privilege bracketing */ 12280Sstevel@tonic-gate (void) __priv_bracket(PRIV_ON); 12290Sstevel@tonic-gate 12300Sstevel@tonic-gate if ((rsock = socket(pr->family, SOCK_RAW, pe->p_proto)) < 0) { 12310Sstevel@tonic-gate Fprintf(stderr, "%s: icmp socket: %s\n", prog, strerror(errno)); 12320Sstevel@tonic-gate exit(EXIT_FAILURE); 12330Sstevel@tonic-gate } 12340Sstevel@tonic-gate 12350Sstevel@tonic-gate if (options & SO_DEBUG) { 12360Sstevel@tonic-gate if (setsockopt(rsock, SOL_SOCKET, SO_DEBUG, (char *)&on, 12370Sstevel@tonic-gate sizeof (on)) < 0) { 12380Sstevel@tonic-gate Fprintf(stderr, "%s: SO_DEBUG: %s\n", prog, 12390Sstevel@tonic-gate strerror(errno)); 12400Sstevel@tonic-gate exit(EXIT_FAILURE); 12410Sstevel@tonic-gate } 12420Sstevel@tonic-gate } 12430Sstevel@tonic-gate if (options & SO_DONTROUTE) { 12440Sstevel@tonic-gate if (setsockopt(rsock, SOL_SOCKET, SO_DONTROUTE, (char *)&on, 12450Sstevel@tonic-gate sizeof (on)) < 0) { 12460Sstevel@tonic-gate Fprintf(stderr, "%s: SO_DONTROUTE: %s\n", prog, 12470Sstevel@tonic-gate strerror(errno)); 12480Sstevel@tonic-gate exit(EXIT_FAILURE); 12490Sstevel@tonic-gate } 12500Sstevel@tonic-gate } 12510Sstevel@tonic-gate 12520Sstevel@tonic-gate if (pr->family == AF_INET6) { 12530Sstevel@tonic-gate /* Enable receipt of destination address info */ 12540Sstevel@tonic-gate if (setsockopt(rsock, IPPROTO_IPV6, IPV6_RECVPKTINFO, 12550Sstevel@tonic-gate (char *)&on, sizeof (on)) < 0) { 12560Sstevel@tonic-gate Fprintf(stderr, "%s: IPV6_RECVPKTINFO: %s\n", prog, 12570Sstevel@tonic-gate strerror(errno)); 12580Sstevel@tonic-gate exit(EXIT_FAILURE); 12590Sstevel@tonic-gate } 12600Sstevel@tonic-gate /* Enable receipt of hoplimit info */ 12610Sstevel@tonic-gate if (setsockopt(rsock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, 12620Sstevel@tonic-gate (char *)&on, sizeof (on)) < 0) { 12630Sstevel@tonic-gate Fprintf(stderr, "%s: IPV6_RECVHOPLIMIT: %s\n", prog, 12640Sstevel@tonic-gate strerror(errno)); 12650Sstevel@tonic-gate exit(EXIT_FAILURE); 12660Sstevel@tonic-gate } 12670Sstevel@tonic-gate 12680Sstevel@tonic-gate } 12690Sstevel@tonic-gate 12700Sstevel@tonic-gate /* 12710Sstevel@tonic-gate * Initialize the socket type and protocol based on the address 12720Sstevel@tonic-gate * family, whether or not a raw IP socket is required (for IPv4) 12730Sstevel@tonic-gate * or whether ICMP will be used instead of UDP. 12740Sstevel@tonic-gate * 12750Sstevel@tonic-gate * For historical reasons, the datagrams sent out by 12760Sstevel@tonic-gate * traceroute(1M) do not have the "don't fragment" flag set. For 12770Sstevel@tonic-gate * this reason as well as the ability to set the Loose Source and 12780Sstevel@tonic-gate * Record Route (LSRR) option, a raw IP socket will be used for 12790Sstevel@tonic-gate * IPv4 when run in the global zone. Otherwise, the actual 12800Sstevel@tonic-gate * datagram that will be sent will be a regular UDP or ICMP echo 12810Sstevel@tonic-gate * request packet. However for convenience and for future options 12820Sstevel@tonic-gate * when other IP header information may be specified using 12830Sstevel@tonic-gate * traceroute, the buffer including the raw IP and UDP or ICMP 12840Sstevel@tonic-gate * header is always filled in. When the probe is actually sent, 12850Sstevel@tonic-gate * the size of the request and the start of the packet is set 12860Sstevel@tonic-gate * according to the type of datagram to send. 12870Sstevel@tonic-gate */ 12880Sstevel@tonic-gate if (pr->family == AF_INET && raw_req) { 12890Sstevel@tonic-gate type = SOCK_RAW; 12900Sstevel@tonic-gate proto = IPPROTO_RAW; 12910Sstevel@tonic-gate } else if (useicmp) { 12920Sstevel@tonic-gate type = SOCK_RAW; 12930Sstevel@tonic-gate if (pr->family == AF_INET) 12940Sstevel@tonic-gate proto = IPPROTO_ICMP; 12950Sstevel@tonic-gate else 12960Sstevel@tonic-gate proto = IPPROTO_ICMPV6; 12970Sstevel@tonic-gate } else { 12980Sstevel@tonic-gate type = SOCK_DGRAM; 12990Sstevel@tonic-gate proto = IPPROTO_UDP; 13000Sstevel@tonic-gate } 13010Sstevel@tonic-gate ssock = socket(pr->family, type, proto); 13020Sstevel@tonic-gate 13030Sstevel@tonic-gate if (ssock < 0) { 13040Sstevel@tonic-gate if (proto == IPPROTO_RAW) { 13050Sstevel@tonic-gate Fprintf(stderr, "%s: raw socket: %s\n", prog, 13060Sstevel@tonic-gate strerror(errno)); 13070Sstevel@tonic-gate } else if (proto == IPPROTO_UDP) { 13080Sstevel@tonic-gate Fprintf(stderr, "%s: udp socket: %s\n", prog, 13090Sstevel@tonic-gate strerror(errno)); 13100Sstevel@tonic-gate } else { 13110Sstevel@tonic-gate Fprintf(stderr, "%s: icmp socket: %s\n", prog, 13120Sstevel@tonic-gate strerror(errno)); 13130Sstevel@tonic-gate } 13140Sstevel@tonic-gate exit(EXIT_FAILURE); 13150Sstevel@tonic-gate } 13160Sstevel@tonic-gate 13170Sstevel@tonic-gate if (setsockopt(ssock, SOL_SOCKET, SO_SNDBUF, (char *)&packet_len, 13180Sstevel@tonic-gate sizeof (packet_len)) < 0) { 13190Sstevel@tonic-gate Fprintf(stderr, "%s: SO_SNDBUF: %s\n", prog, strerror(errno)); 13200Sstevel@tonic-gate exit(EXIT_FAILURE); 13210Sstevel@tonic-gate } 13220Sstevel@tonic-gate 13230Sstevel@tonic-gate if (pr->family == AF_INET && raw_req) { 13240Sstevel@tonic-gate if (setsockopt(ssock, IPPROTO_IP, IP_HDRINCL, (char *)&on, 13250Sstevel@tonic-gate sizeof (on)) < 0) { 13260Sstevel@tonic-gate Fprintf(stderr, "%s: IP_HDRINCL: %s\n", prog, 13270Sstevel@tonic-gate strerror(errno)); 13280Sstevel@tonic-gate exit(EXIT_FAILURE); 13290Sstevel@tonic-gate } 13300Sstevel@tonic-gate } 13310Sstevel@tonic-gate 13320Sstevel@tonic-gate if (options & SO_DEBUG) { 13330Sstevel@tonic-gate if (setsockopt(ssock, SOL_SOCKET, SO_DEBUG, (char *)&on, 13340Sstevel@tonic-gate sizeof (on)) < 0) { 13350Sstevel@tonic-gate Fprintf(stderr, "%s: SO_DEBUG: %s\n", prog, 13360Sstevel@tonic-gate strerror(errno)); 13370Sstevel@tonic-gate exit(EXIT_FAILURE); 13380Sstevel@tonic-gate } 13390Sstevel@tonic-gate } 13400Sstevel@tonic-gate if (options & SO_DONTROUTE) { 13410Sstevel@tonic-gate if (setsockopt(ssock, SOL_SOCKET, SO_DONTROUTE, 13420Sstevel@tonic-gate (char *)&on, sizeof (on)) < 0) { 13430Sstevel@tonic-gate Fprintf(stderr, "%s: SO_DONTROUTE: %s\n", prog, 13440Sstevel@tonic-gate strerror(errno)); 13450Sstevel@tonic-gate exit(EXIT_FAILURE); 13460Sstevel@tonic-gate } 13470Sstevel@tonic-gate } 13480Sstevel@tonic-gate 13490Sstevel@tonic-gate /* 13500Sstevel@tonic-gate * If a raw IPv4 packet is going to be sent, the Type of Service 13510Sstevel@tonic-gate * field in the packet will be initialized in set_buffers(). 13520Sstevel@tonic-gate * Otherwise, it is initialized here using the IPPROTO_IP level 13530Sstevel@tonic-gate * socket option. 13540Sstevel@tonic-gate */ 13550Sstevel@tonic-gate if (settos && !raw_req) { 13560Sstevel@tonic-gate int_op = tos; 13570Sstevel@tonic-gate if (setsockopt(ssock, IPPROTO_IP, IP_TOS, (char *)&int_op, 13580Sstevel@tonic-gate sizeof (int_op)) < 0) { 13590Sstevel@tonic-gate Fprintf(stderr, "%s: IP_TOS: %s\n", prog, 13600Sstevel@tonic-gate strerror(errno)); 13610Sstevel@tonic-gate exit(EXIT_FAILURE); 13620Sstevel@tonic-gate } 13630Sstevel@tonic-gate } 13640Sstevel@tonic-gate if (pr->family == AF_INET) { 13650Sstevel@tonic-gate rcvsock4 = rsock; 13660Sstevel@tonic-gate sndsock4 = ssock; 13670Sstevel@tonic-gate } else { 13680Sstevel@tonic-gate rcvsock6 = rsock; 13690Sstevel@tonic-gate sndsock6 = ssock; 13700Sstevel@tonic-gate } 13710Sstevel@tonic-gate /* Revert to non-privileged user after configuring sockets */ 13720Sstevel@tonic-gate (void) __priv_bracket(PRIV_OFF); 13730Sstevel@tonic-gate } 13740Sstevel@tonic-gate 13750Sstevel@tonic-gate /* 13760Sstevel@tonic-gate * If we are "probing all", this function calls traceroute() for each IP address 13770Sstevel@tonic-gate * of the target, otherwise calls only once. Returns _B_FALSE if traceroute() 13780Sstevel@tonic-gate * fails. 13790Sstevel@tonic-gate */ 13800Sstevel@tonic-gate static void 13810Sstevel@tonic-gate trace_it(struct addrinfo *ai_dst) 13820Sstevel@tonic-gate { 13830Sstevel@tonic-gate struct msghdr msg6; 13840Sstevel@tonic-gate int num_dst_IPaddrs; 13850Sstevel@tonic-gate struct addrinfo *aip; 13860Sstevel@tonic-gate int i; 13870Sstevel@tonic-gate 13880Sstevel@tonic-gate if (!probe_all) 13890Sstevel@tonic-gate num_dst_IPaddrs = 1; 13900Sstevel@tonic-gate else 13910Sstevel@tonic-gate num_dst_IPaddrs = num_v4 + num_v6; 13920Sstevel@tonic-gate 13930Sstevel@tonic-gate /* 13940Sstevel@tonic-gate * Initialize the msg6 structure using the hoplimit for the first 13950Sstevel@tonic-gate * probe packet, gateway addresses and the outgoing interface index. 13960Sstevel@tonic-gate */ 13970Sstevel@tonic-gate if (ai_dst->ai_family == AF_INET6 || (probe_all && num_v6)) { 13980Sstevel@tonic-gate msg6.msg_control = NULL; 13990Sstevel@tonic-gate msg6.msg_controllen = 0; 14000Sstevel@tonic-gate set_ancillary_data(&msg6, first_ttl, pr6->gwIPlist, gw_count, 14010Sstevel@tonic-gate if_index); 14020Sstevel@tonic-gate } 14030Sstevel@tonic-gate 14040Sstevel@tonic-gate /* run traceroute for all the IP addresses of the multihomed dest */ 14050Sstevel@tonic-gate for (aip = ai_dst, i = 0; i < num_dst_IPaddrs && aip != NULL; i++) { 14060Sstevel@tonic-gate union any_in_addr *addrp; 14070Sstevel@tonic-gate if (aip->ai_family == AF_INET) { 14080Sstevel@tonic-gate addrp = (union any_in_addr *) 14090Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 14100Sstevel@tonic-gate &((struct sockaddr_in *) 14110Sstevel@tonic-gate aip->ai_addr)->sin_addr; 14120Sstevel@tonic-gate set_sin((struct sockaddr *)pr4->to, addrp, 14130Sstevel@tonic-gate aip->ai_family); 14140Sstevel@tonic-gate traceroute(addrp, &msg6, pr4, num_ifs4, al4); 14150Sstevel@tonic-gate } else { 14160Sstevel@tonic-gate addrp = (union any_in_addr *) 14170Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 14180Sstevel@tonic-gate &((struct sockaddr_in6 *) 14190Sstevel@tonic-gate aip->ai_addr)->sin6_addr; 14200Sstevel@tonic-gate set_sin((struct sockaddr *)pr6->to, addrp, 14210Sstevel@tonic-gate aip->ai_family); 14220Sstevel@tonic-gate traceroute(addrp, &msg6, pr6, num_ifs6, al6); 14230Sstevel@tonic-gate } 14240Sstevel@tonic-gate aip = aip->ai_next; 14250Sstevel@tonic-gate if (i < (num_dst_IPaddrs - 1)) 14260Sstevel@tonic-gate (void) putchar('\n'); 14270Sstevel@tonic-gate } 14280Sstevel@tonic-gate } 14290Sstevel@tonic-gate 14300Sstevel@tonic-gate /* 14310Sstevel@tonic-gate * set the IP address in a sockaddr struct 14320Sstevel@tonic-gate */ 14330Sstevel@tonic-gate static void 14340Sstevel@tonic-gate set_sin(struct sockaddr *sock, union any_in_addr *addr, int family) 14350Sstevel@tonic-gate { 14360Sstevel@tonic-gate sock->sa_family = family; 14370Sstevel@tonic-gate 14380Sstevel@tonic-gate if (family == AF_INET) 14390Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 14400Sstevel@tonic-gate ((struct sockaddr_in *)sock)->sin_addr = addr->addr; 14410Sstevel@tonic-gate else 14420Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 14430Sstevel@tonic-gate ((struct sockaddr_in6 *)sock)->sin6_addr = addr->addr6; 14440Sstevel@tonic-gate } 14450Sstevel@tonic-gate 14460Sstevel@tonic-gate /* 14470Sstevel@tonic-gate * returns the IF name on which the given IP address is configured 14480Sstevel@tonic-gate */ 14490Sstevel@tonic-gate static char * 14500Sstevel@tonic-gate device_name(struct ifaddrlist *al, int len, union any_in_addr *ip_addr, 14510Sstevel@tonic-gate struct pr_set *pr) 14520Sstevel@tonic-gate { 14530Sstevel@tonic-gate int i; 14540Sstevel@tonic-gate struct ifaddrlist *tmp_al; 14550Sstevel@tonic-gate 14560Sstevel@tonic-gate tmp_al = al; 14570Sstevel@tonic-gate 14580Sstevel@tonic-gate for (i = 0; i < len; i++, tmp_al++) { 14590Sstevel@tonic-gate if (memcmp(&tmp_al->addr, ip_addr, pr->addr_len) == 0) { 14600Sstevel@tonic-gate return (tmp_al->device); 14610Sstevel@tonic-gate } 14620Sstevel@tonic-gate } 14630Sstevel@tonic-gate 14640Sstevel@tonic-gate return (NULL); 14650Sstevel@tonic-gate } 14660Sstevel@tonic-gate 14670Sstevel@tonic-gate /* 14680Sstevel@tonic-gate * Trace the route to the host with given IP address. 14690Sstevel@tonic-gate */ 14700Sstevel@tonic-gate static void 14710Sstevel@tonic-gate traceroute(union any_in_addr *ip_addr, struct msghdr *msg6, struct pr_set *pr, 14720Sstevel@tonic-gate int num_ifs, struct ifaddrlist *al) 14730Sstevel@tonic-gate { 14740Sstevel@tonic-gate int ttl; 14750Sstevel@tonic-gate int probe; 14760Sstevel@tonic-gate uchar_t type; /* icmp type */ 14770Sstevel@tonic-gate uchar_t code; /* icmp code */ 14780Sstevel@tonic-gate int reply; 14790Sstevel@tonic-gate int seq = 0; 1480*8485SPeter.Memishian@Sun.COM char abuf[INET6_ADDRSTRLEN]; /* use for inet_ntop() */ 14810Sstevel@tonic-gate int longjmp_return; /* return value from longjump */ 14820Sstevel@tonic-gate struct ip *ip = (struct ip *)packet; 14830Sstevel@tonic-gate boolean_t got_there = _B_FALSE; /* we hit the destination */ 14840Sstevel@tonic-gate static boolean_t first_pkt = _B_TRUE; 14850Sstevel@tonic-gate int hoplimit; /* hoplimit for IPv6 packets */ 14860Sstevel@tonic-gate struct in6_addr addr6; 14870Sstevel@tonic-gate int num_src_ifs; /* excludes down and loopback */ 14880Sstevel@tonic-gate struct msghdr in_msg; 14890Sstevel@tonic-gate struct iovec iov; 14900Sstevel@tonic-gate int *intp; 14910Sstevel@tonic-gate int sndsock; 14920Sstevel@tonic-gate int rcvsock; 14930Sstevel@tonic-gate 14940Sstevel@tonic-gate msg6->msg_name = pr->to; 14950Sstevel@tonic-gate msg6->msg_namelen = sizeof (struct sockaddr_in6); 14960Sstevel@tonic-gate sndsock = (pr->family == AF_INET) ? sndsock4 : sndsock6; 14970Sstevel@tonic-gate rcvsock = (pr->family == AF_INET) ? rcvsock4 : rcvsock6; 14980Sstevel@tonic-gate 14990Sstevel@tonic-gate /* carry out the source address selection */ 15000Sstevel@tonic-gate if (pick_src) { 15010Sstevel@tonic-gate union any_in_addr src_addr; 15020Sstevel@tonic-gate char *dev_name; 15030Sstevel@tonic-gate int i; 15040Sstevel@tonic-gate 15050Sstevel@tonic-gate /* 15060Sstevel@tonic-gate * If there's a gateway, a routing header as a consequence, our 15070Sstevel@tonic-gate * kernel picks the source address based on the first hop 15080Sstevel@tonic-gate * address, rather than final destination address. 15090Sstevel@tonic-gate */ 15100Sstevel@tonic-gate if (gw_count > 0) { 15110Sstevel@tonic-gate (void) select_src_addr(pr->gwIPlist, &src_addr, 15120Sstevel@tonic-gate pr->family); 15130Sstevel@tonic-gate } else { 15140Sstevel@tonic-gate (void) select_src_addr(ip_addr, &src_addr, pr->family); 15150Sstevel@tonic-gate } 15160Sstevel@tonic-gate set_sin(pr->from, &src_addr, pr->family); 15170Sstevel@tonic-gate 15180Sstevel@tonic-gate /* filter out down and loopback interfaces */ 15190Sstevel@tonic-gate num_src_ifs = 0; 15200Sstevel@tonic-gate for (i = 0; i < num_ifs; i++) { 15210Sstevel@tonic-gate if (!(al[i].flags & IFF_LOOPBACK) && 15220Sstevel@tonic-gate (al[i].flags & IFF_UP)) 15230Sstevel@tonic-gate num_src_ifs++; 15240Sstevel@tonic-gate } 15250Sstevel@tonic-gate 15260Sstevel@tonic-gate if (num_src_ifs > 1) { 15270Sstevel@tonic-gate dev_name = device_name(al, num_ifs, &src_addr, pr); 15280Sstevel@tonic-gate if (dev_name == NULL) 15290Sstevel@tonic-gate dev_name = "?"; 15300Sstevel@tonic-gate 1531*8485SPeter.Memishian@Sun.COM (void) inet_ntop(pr->family, pr->from_sin_addr, abuf, 1532*8485SPeter.Memishian@Sun.COM sizeof (abuf)); 15330Sstevel@tonic-gate Fprintf(stderr, 15340Sstevel@tonic-gate "%s: Warning: Multiple interfaces found;" 1535*8485SPeter.Memishian@Sun.COM " using %s @ %s\n", prog, abuf, dev_name); 15360Sstevel@tonic-gate } 15370Sstevel@tonic-gate } 15380Sstevel@tonic-gate 15390Sstevel@tonic-gate if (pr->family == AF_INET) { 15400Sstevel@tonic-gate outip4->ip_src = *(struct in_addr *)pr->from_sin_addr; 15410Sstevel@tonic-gate outip4->ip_dst = ip_addr->addr; 15420Sstevel@tonic-gate } 15430Sstevel@tonic-gate 15440Sstevel@tonic-gate /* 15450Sstevel@tonic-gate * If the hostname is an IPv6 literal address, let's not print it twice. 15460Sstevel@tonic-gate */ 15470Sstevel@tonic-gate if (pr->family == AF_INET6 && 15480Sstevel@tonic-gate inet_pton(AF_INET6, hostname, &addr6) > 0) { 15490Sstevel@tonic-gate Fprintf(stderr, "%s to %s", prog, hostname); 15500Sstevel@tonic-gate } else { 15510Sstevel@tonic-gate Fprintf(stderr, "%s to %s (%s)", prog, hostname, 1552*8485SPeter.Memishian@Sun.COM inet_ntop(pr->family, ip_addr, abuf, sizeof (abuf))); 15530Sstevel@tonic-gate } 15540Sstevel@tonic-gate 15550Sstevel@tonic-gate if (source) 15560Sstevel@tonic-gate Fprintf(stderr, " from %s", source); 15570Sstevel@tonic-gate Fprintf(stderr, ", %d hops max, %d byte packets\n", max_ttl, 15580Sstevel@tonic-gate pr->packlen); 15590Sstevel@tonic-gate (void) fflush(stderr); 15600Sstevel@tonic-gate 15610Sstevel@tonic-gate /* 15620Sstevel@tonic-gate * Setup the source routing for IPv4. For IPv6, we did the required 15630Sstevel@tonic-gate * setup in the caller function, trace_it(), because it's independent 15640Sstevel@tonic-gate * from the IP address of target. 15650Sstevel@tonic-gate */ 15660Sstevel@tonic-gate if (pr->family == AF_INET && gw_count > 0) 15670Sstevel@tonic-gate set_IPv4opt_sourcerouting(sndsock, ip_addr, pr->gwIPlist); 15680Sstevel@tonic-gate 15690Sstevel@tonic-gate if (probe_all) { 15700Sstevel@tonic-gate /* interrupt handler sig_handler() jumps back to here */ 15710Sstevel@tonic-gate if ((longjmp_return = setjmp(env)) != 0) { 15720Sstevel@tonic-gate switch (longjmp_return) { 15730Sstevel@tonic-gate case SIGINT: 15740Sstevel@tonic-gate Printf("(skipping)\n"); 15750Sstevel@tonic-gate return; 15760Sstevel@tonic-gate case SIGQUIT: 15770Sstevel@tonic-gate Printf("(exiting)\n"); 15780Sstevel@tonic-gate exit(EXIT_SUCCESS); 15790Sstevel@tonic-gate default: /* should never happen */ 15800Sstevel@tonic-gate exit(EXIT_FAILURE); 15810Sstevel@tonic-gate } 15820Sstevel@tonic-gate } 15830Sstevel@tonic-gate (void) signal(SIGINT, sig_handler); 15840Sstevel@tonic-gate } 15850Sstevel@tonic-gate 15860Sstevel@tonic-gate for (ttl = first_ttl; ttl <= max_ttl; ++ttl) { 15870Sstevel@tonic-gate union any_in_addr lastaddr; 15880Sstevel@tonic-gate int timeouts = 0; 15890Sstevel@tonic-gate double rtt; /* for statistics */ 15900Sstevel@tonic-gate int nreceived = 0; 15910Sstevel@tonic-gate double rttmin, rttmax; 15920Sstevel@tonic-gate double rttsum, rttssq; 15930Sstevel@tonic-gate int unreachable; 15940Sstevel@tonic-gate 15950Sstevel@tonic-gate got_there = _B_FALSE; 15960Sstevel@tonic-gate unreachable = 0; 15970Sstevel@tonic-gate 15980Sstevel@tonic-gate /* 15990Sstevel@tonic-gate * The following line clears both IPv4 and IPv6 address stored 16000Sstevel@tonic-gate * in the union. 16010Sstevel@tonic-gate */ 16020Sstevel@tonic-gate lastaddr.addr6 = in6addr_any; 16030Sstevel@tonic-gate 16040Sstevel@tonic-gate if ((ttl == (first_ttl + 1)) && (options & SO_DONTROUTE)) { 16050Sstevel@tonic-gate Fprintf(stderr, 16060Sstevel@tonic-gate "%s: host %s is not on a directly-attached" 16070Sstevel@tonic-gate " network\n", prog, hostname); 16080Sstevel@tonic-gate break; 16090Sstevel@tonic-gate } 16100Sstevel@tonic-gate 16110Sstevel@tonic-gate Printf("%2d ", ttl); 16120Sstevel@tonic-gate (void) fflush(stdout); 16130Sstevel@tonic-gate 16140Sstevel@tonic-gate for (probe = 0; (probe < nprobes) && (timeouts < max_timeout); 16150Sstevel@tonic-gate ++probe) { 16160Sstevel@tonic-gate int cc; 16170Sstevel@tonic-gate struct timeval t1, t2; 16180Sstevel@tonic-gate 16190Sstevel@tonic-gate /* 16200Sstevel@tonic-gate * Put a delay before sending this probe packet. Don't 16210Sstevel@tonic-gate * delay it if it's the very first packet. 16220Sstevel@tonic-gate */ 16230Sstevel@tonic-gate if (!first_pkt) { 16240Sstevel@tonic-gate if (delay.tv_sec > 0) 16250Sstevel@tonic-gate (void) sleep((uint_t)delay.tv_sec); 16260Sstevel@tonic-gate if (delay.tv_usec > 0) 16270Sstevel@tonic-gate (void) usleep(delay.tv_usec); 16280Sstevel@tonic-gate } else { 16290Sstevel@tonic-gate first_pkt = _B_FALSE; 16300Sstevel@tonic-gate } 16310Sstevel@tonic-gate 16320Sstevel@tonic-gate (void) gettimeofday(&t1, NULL); 16330Sstevel@tonic-gate 16340Sstevel@tonic-gate if (pr->family == AF_INET) { 16350Sstevel@tonic-gate send_probe(sndsock, pr->to, outip4, seq, ttl, 16360Sstevel@tonic-gate &t1, pr->packlen); 16370Sstevel@tonic-gate } else { 16380Sstevel@tonic-gate send_probe6(sndsock, msg6, outip6, seq, ttl, 16390Sstevel@tonic-gate &t1, pr->packlen); 16400Sstevel@tonic-gate } 16410Sstevel@tonic-gate 16420Sstevel@tonic-gate /* prepare msghdr for recvmsg() */ 16430Sstevel@tonic-gate in_msg.msg_name = pr->from; 16440Sstevel@tonic-gate in_msg.msg_namelen = pr->sock_size; 16450Sstevel@tonic-gate 16460Sstevel@tonic-gate iov.iov_base = (char *)packet; 16470Sstevel@tonic-gate iov.iov_len = sizeof (packet); 16480Sstevel@tonic-gate 16490Sstevel@tonic-gate in_msg.msg_iov = &iov; 16500Sstevel@tonic-gate in_msg.msg_iovlen = 1; 16510Sstevel@tonic-gate 16520Sstevel@tonic-gate in_msg.msg_control = ancillary_data; 16530Sstevel@tonic-gate in_msg.msg_controllen = sizeof (ancillary_data); 16540Sstevel@tonic-gate 16550Sstevel@tonic-gate while ((cc = wait_for_reply(rcvsock, &in_msg, 16560Sstevel@tonic-gate &t1)) != 0) { 16570Sstevel@tonic-gate (void) gettimeofday(&t2, NULL); 16580Sstevel@tonic-gate 16590Sstevel@tonic-gate reply = (*pr->check_reply_fn) (&in_msg, cc, seq, 16600Sstevel@tonic-gate &type, &code); 16610Sstevel@tonic-gate 16620Sstevel@tonic-gate in_msg.msg_controllen = 16630Sstevel@tonic-gate sizeof (ancillary_data); 16640Sstevel@tonic-gate /* Skip short packet */ 16650Sstevel@tonic-gate if (reply == REPLY_SHORT_PKT) { 16660Sstevel@tonic-gate continue; 16670Sstevel@tonic-gate } 16680Sstevel@tonic-gate 16690Sstevel@tonic-gate timeouts = 0; 16700Sstevel@tonic-gate 16710Sstevel@tonic-gate /* 16720Sstevel@tonic-gate * if reply comes from a different host, print 16730Sstevel@tonic-gate * the hostname 16740Sstevel@tonic-gate */ 16750Sstevel@tonic-gate if (memcmp(pr->from_sin_addr, &lastaddr, 16760Sstevel@tonic-gate pr->addr_len) != 0) { 16770Sstevel@tonic-gate (*pr->print_addr_fn) ((uchar_t *)packet, 16780Sstevel@tonic-gate cc, pr->from); 16790Sstevel@tonic-gate /* store the address response */ 16800Sstevel@tonic-gate (void) memcpy(&lastaddr, 16810Sstevel@tonic-gate pr->from_sin_addr, pr->addr_len); 16820Sstevel@tonic-gate } 16830Sstevel@tonic-gate 16840Sstevel@tonic-gate rtt = deltaT(&t1, &t2); 16850Sstevel@tonic-gate if (collect_stat) { 16860Sstevel@tonic-gate record_stats(rtt, &nreceived, &rttmin, 16870Sstevel@tonic-gate &rttmax, &rttsum, &rttssq); 16880Sstevel@tonic-gate } else { 16890Sstevel@tonic-gate Printf(" %.3f ms", rtt); 16900Sstevel@tonic-gate } 16910Sstevel@tonic-gate 16920Sstevel@tonic-gate if (pr->family == AF_INET6) { 1693*8485SPeter.Memishian@Sun.COM intp = find_ancillary_data(&in_msg, 1694*8485SPeter.Memishian@Sun.COM IPPROTO_IPV6, IPV6_HOPLIMIT); 16950Sstevel@tonic-gate if (intp == NULL) { 16960Sstevel@tonic-gate Fprintf(stderr, 16970Sstevel@tonic-gate "%s: can't find " 16980Sstevel@tonic-gate "IPV6_HOPLIMIT ancillary " 16990Sstevel@tonic-gate "data\n", prog); 17000Sstevel@tonic-gate exit(EXIT_FAILURE); 17010Sstevel@tonic-gate } 17020Sstevel@tonic-gate hoplimit = *intp; 17030Sstevel@tonic-gate } 17040Sstevel@tonic-gate 17050Sstevel@tonic-gate if (reply == REPLY_GOT_TARGET) { 17060Sstevel@tonic-gate got_there = _B_TRUE; 17070Sstevel@tonic-gate 17080Sstevel@tonic-gate if (((pr->family == AF_INET) && 17090Sstevel@tonic-gate (ip->ip_ttl <= 1)) || 17100Sstevel@tonic-gate ((pr->family == AF_INET6) && 17110Sstevel@tonic-gate (hoplimit <= 1))) 17120Sstevel@tonic-gate Printf(" !"); 17130Sstevel@tonic-gate } 17140Sstevel@tonic-gate 17150Sstevel@tonic-gate if (!collect_stat && showttl) { 17160Sstevel@tonic-gate if (pr->family == AF_INET) { 17170Sstevel@tonic-gate Printf(" (ttl=%d)", 17180Sstevel@tonic-gate (int)ip->ip_ttl); 17190Sstevel@tonic-gate } else if (hoplimit != -1) { 17200Sstevel@tonic-gate Printf(" (hop limit=%d)", 17210Sstevel@tonic-gate hoplimit); 17220Sstevel@tonic-gate } 17230Sstevel@tonic-gate } 17240Sstevel@tonic-gate 17250Sstevel@tonic-gate if (reply == REPLY_GOT_OTHER) { 17260Sstevel@tonic-gate if ((*pr->print_icmp_other_fn) 17270Sstevel@tonic-gate (type, code)) { 17280Sstevel@tonic-gate unreachable++; 17290Sstevel@tonic-gate } 17300Sstevel@tonic-gate } 17310Sstevel@tonic-gate 17320Sstevel@tonic-gate /* special case */ 17330Sstevel@tonic-gate if (pr->family == AF_INET && 17340Sstevel@tonic-gate type == ICMP_UNREACH && 17350Sstevel@tonic-gate code == ICMP_UNREACH_PROTOCOL) 17360Sstevel@tonic-gate got_there = _B_TRUE; 17370Sstevel@tonic-gate 17380Sstevel@tonic-gate break; 17390Sstevel@tonic-gate } 17400Sstevel@tonic-gate 17410Sstevel@tonic-gate seq = (seq + 1) % (MAX_SEQ + 1); 17420Sstevel@tonic-gate 17430Sstevel@tonic-gate if (cc == 0) { 17440Sstevel@tonic-gate Printf(" *"); 17450Sstevel@tonic-gate timeouts++; 17460Sstevel@tonic-gate } 17470Sstevel@tonic-gate 17480Sstevel@tonic-gate (void) fflush(stdout); 17490Sstevel@tonic-gate } 17500Sstevel@tonic-gate 17510Sstevel@tonic-gate if (collect_stat) { 17520Sstevel@tonic-gate print_stats(probe, nreceived, rttmin, rttmax, rttsum, 17530Sstevel@tonic-gate rttssq); 17540Sstevel@tonic-gate } 17550Sstevel@tonic-gate 17560Sstevel@tonic-gate (void) putchar('\n'); 17570Sstevel@tonic-gate 17580Sstevel@tonic-gate /* either we hit the target or received too many unreachables */ 17590Sstevel@tonic-gate if (got_there || 17600Sstevel@tonic-gate (unreachable > 0 && unreachable >= nprobes - 1)) 17610Sstevel@tonic-gate break; 17620Sstevel@tonic-gate } 17630Sstevel@tonic-gate 17640Sstevel@tonic-gate /* Ignore the SIGINT between traceroute() runs */ 17650Sstevel@tonic-gate if (probe_all) 17660Sstevel@tonic-gate (void) signal(SIGINT, SIG_IGN); 17670Sstevel@tonic-gate } 17680Sstevel@tonic-gate 17690Sstevel@tonic-gate /* 17700Sstevel@tonic-gate * for a given destination address and address family, it finds out what 17710Sstevel@tonic-gate * source address kernel is going to pick 17720Sstevel@tonic-gate */ 17730Sstevel@tonic-gate static void 17740Sstevel@tonic-gate select_src_addr(union any_in_addr *dst_addr, union any_in_addr *src_addr, 17750Sstevel@tonic-gate int family) 17760Sstevel@tonic-gate { 17770Sstevel@tonic-gate int tmp_fd; 17780Sstevel@tonic-gate struct sockaddr *sock; 17790Sstevel@tonic-gate struct sockaddr_in *sin; 17800Sstevel@tonic-gate struct sockaddr_in6 *sin6; 17810Sstevel@tonic-gate size_t sock_len; 17820Sstevel@tonic-gate 17830Sstevel@tonic-gate sock = (struct sockaddr *)malloc(sizeof (struct sockaddr_in6)); 17840Sstevel@tonic-gate if (sock == NULL) { 17850Sstevel@tonic-gate Fprintf(stderr, "%s: malloc %s\n", prog, strerror(errno)); 17860Sstevel@tonic-gate exit(EXIT_FAILURE); 17870Sstevel@tonic-gate } 17880Sstevel@tonic-gate (void) bzero(sock, sizeof (struct sockaddr_in6)); 17890Sstevel@tonic-gate 17900Sstevel@tonic-gate if (family == AF_INET) { 17910Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 17920Sstevel@tonic-gate sin = (struct sockaddr_in *)sock; 17930Sstevel@tonic-gate sin->sin_family = AF_INET; 17940Sstevel@tonic-gate sin->sin_addr = dst_addr->addr; 17950Sstevel@tonic-gate sin->sin_port = IPPORT_ECHO; /* port shouldn't be 0 */ 17960Sstevel@tonic-gate sock_len = sizeof (struct sockaddr_in); 17970Sstevel@tonic-gate } else { 17980Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 17990Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)sock; 18000Sstevel@tonic-gate sin6->sin6_family = AF_INET6; 18010Sstevel@tonic-gate sin6->sin6_addr = dst_addr->addr6; 18020Sstevel@tonic-gate sin6->sin6_port = IPPORT_ECHO; /* port shouldn't be 0 */ 18030Sstevel@tonic-gate sock_len = sizeof (struct sockaddr_in6); 18040Sstevel@tonic-gate } 18050Sstevel@tonic-gate 18060Sstevel@tonic-gate /* open a UDP socket */ 18070Sstevel@tonic-gate if ((tmp_fd = socket(family, SOCK_DGRAM, 0)) < 0) { 18080Sstevel@tonic-gate Fprintf(stderr, "%s: udp socket: %s\n", prog, 18090Sstevel@tonic-gate strerror(errno)); 18100Sstevel@tonic-gate exit(EXIT_FAILURE); 18110Sstevel@tonic-gate } 18120Sstevel@tonic-gate 18130Sstevel@tonic-gate /* connect it */ 18140Sstevel@tonic-gate if (connect(tmp_fd, sock, sock_len) < 0) { 18150Sstevel@tonic-gate /* 18160Sstevel@tonic-gate * If there's no route to the destination, this connect() call 18170Sstevel@tonic-gate * fails. We just return all-zero (wildcard) as the source 18180Sstevel@tonic-gate * address, so that user can get to see "no route to dest" 18190Sstevel@tonic-gate * message, as it'll try to send the probe packet out and will 18200Sstevel@tonic-gate * receive ICMP unreachable. 18210Sstevel@tonic-gate */ 18220Sstevel@tonic-gate if (family == AF_INET) 18230Sstevel@tonic-gate src_addr->addr.s_addr = INADDR_ANY; 18240Sstevel@tonic-gate else 18250Sstevel@tonic-gate src_addr->addr6 = in6addr_any; 18260Sstevel@tonic-gate free(sock); 18270Sstevel@tonic-gate return; 18280Sstevel@tonic-gate } 18290Sstevel@tonic-gate 18300Sstevel@tonic-gate /* get the local sock info */ 18310Sstevel@tonic-gate if (getsockname(tmp_fd, sock, &sock_len) < 0) { 18320Sstevel@tonic-gate Fprintf(stderr, "%s: getsockname: %s\n", prog, 18330Sstevel@tonic-gate strerror(errno)); 18340Sstevel@tonic-gate exit(EXIT_FAILURE); 18350Sstevel@tonic-gate } 18360Sstevel@tonic-gate 18370Sstevel@tonic-gate if (family == AF_INET) { 18380Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 18390Sstevel@tonic-gate sin = (struct sockaddr_in *)sock; 18400Sstevel@tonic-gate src_addr->addr = sin->sin_addr; 18410Sstevel@tonic-gate } else { 18420Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 18430Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)sock; 18440Sstevel@tonic-gate src_addr->addr6 = sin6->sin6_addr; 18450Sstevel@tonic-gate } 18460Sstevel@tonic-gate 18470Sstevel@tonic-gate free(sock); 18480Sstevel@tonic-gate (void) close(tmp_fd); 18490Sstevel@tonic-gate } 18500Sstevel@tonic-gate 18510Sstevel@tonic-gate /* 18520Sstevel@tonic-gate * Checksum routine for Internet Protocol family headers (C Version) 18530Sstevel@tonic-gate */ 18540Sstevel@tonic-gate ushort_t 18550Sstevel@tonic-gate in_cksum(ushort_t *addr, int len) 18560Sstevel@tonic-gate { 18570Sstevel@tonic-gate int nleft = len; 18580Sstevel@tonic-gate ushort_t *w = addr; 18590Sstevel@tonic-gate ushort_t answer; 18600Sstevel@tonic-gate int sum = 0; 18610Sstevel@tonic-gate 18620Sstevel@tonic-gate /* 18630Sstevel@tonic-gate * Our algorithm is simple, using a 32 bit accumulator (sum), 18640Sstevel@tonic-gate * we add sequential 16 bit words to it, and at the end, fold 18650Sstevel@tonic-gate * back all the carry bits from the top 16 bits into the lower 18660Sstevel@tonic-gate * 16 bits. 18670Sstevel@tonic-gate */ 18680Sstevel@tonic-gate while (nleft > 1) { 18690Sstevel@tonic-gate sum += *w++; 18700Sstevel@tonic-gate nleft -= 2; 18710Sstevel@tonic-gate } 18720Sstevel@tonic-gate 18730Sstevel@tonic-gate /* mop up an odd byte, if necessary */ 18740Sstevel@tonic-gate if (nleft == 1) 18750Sstevel@tonic-gate sum += *(uchar_t *)w; 18760Sstevel@tonic-gate 18770Sstevel@tonic-gate /* add back carry outs from top 16 bits to low 16 bits */ 18780Sstevel@tonic-gate sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */ 18790Sstevel@tonic-gate sum += (sum >> 16); /* add carry */ 18800Sstevel@tonic-gate answer = ~sum; /* truncate to 16 bits */ 18810Sstevel@tonic-gate return (answer); 18820Sstevel@tonic-gate } 18830Sstevel@tonic-gate 18840Sstevel@tonic-gate /* 18850Sstevel@tonic-gate * Wait until a reply arrives or timeout occurs. If packet arrived, read it 18860Sstevel@tonic-gate * return the size of the packet read. 18870Sstevel@tonic-gate */ 18880Sstevel@tonic-gate static int 18890Sstevel@tonic-gate wait_for_reply(int sock, struct msghdr *msg, struct timeval *tp) 18900Sstevel@tonic-gate { 18910Sstevel@tonic-gate fd_set fds; 18920Sstevel@tonic-gate struct timeval now, wait; 18930Sstevel@tonic-gate int cc = 0; 18940Sstevel@tonic-gate int result; 18950Sstevel@tonic-gate 18960Sstevel@tonic-gate (void) FD_ZERO(&fds); 18970Sstevel@tonic-gate FD_SET(sock, &fds); 18980Sstevel@tonic-gate 18990Sstevel@tonic-gate wait.tv_sec = tp->tv_sec + waittime; 19000Sstevel@tonic-gate wait.tv_usec = tp->tv_usec; 19010Sstevel@tonic-gate (void) gettimeofday(&now, NULL); 19020Sstevel@tonic-gate tv_sub(&wait, &now); 19030Sstevel@tonic-gate 19040Sstevel@tonic-gate if (wait.tv_sec < 0 || wait.tv_usec < 0) 19050Sstevel@tonic-gate return (0); 19060Sstevel@tonic-gate 19070Sstevel@tonic-gate result = select(sock + 1, &fds, (fd_set *)NULL, (fd_set *)NULL, &wait); 19080Sstevel@tonic-gate 19090Sstevel@tonic-gate if (result == -1) { 19100Sstevel@tonic-gate if (errno != EINTR) { 19110Sstevel@tonic-gate Fprintf(stderr, "%s: select: %s\n", prog, 19120Sstevel@tonic-gate strerror(errno)); 19130Sstevel@tonic-gate } 19140Sstevel@tonic-gate } else if (result > 0) 19150Sstevel@tonic-gate cc = recvmsg(sock, msg, 0); 19160Sstevel@tonic-gate 19170Sstevel@tonic-gate return (cc); 19180Sstevel@tonic-gate } 19190Sstevel@tonic-gate 19200Sstevel@tonic-gate /* 19210Sstevel@tonic-gate * Construct an Internet address representation. If the nflag has been supplied, 19220Sstevel@tonic-gate * give numeric value, otherwise try for symbolic name. 19230Sstevel@tonic-gate */ 19240Sstevel@tonic-gate char * 19250Sstevel@tonic-gate inet_name(union any_in_addr *in, int family) 19260Sstevel@tonic-gate { 19270Sstevel@tonic-gate char *cp; 19280Sstevel@tonic-gate static boolean_t first = _B_TRUE; 19290Sstevel@tonic-gate static char domain[NI_MAXHOST + 1]; 19300Sstevel@tonic-gate static char line[NI_MAXHOST + 1]; /* assuming */ 19310Sstevel@tonic-gate /* (NI_MAXHOST + 1) >= INET6_ADDRSTRLEN */ 19320Sstevel@tonic-gate char hbuf[NI_MAXHOST]; 19330Sstevel@tonic-gate socklen_t slen; 19340Sstevel@tonic-gate struct sockaddr_in sin; 19350Sstevel@tonic-gate struct sockaddr_in6 sin6; 19360Sstevel@tonic-gate struct sockaddr *sa; 19370Sstevel@tonic-gate int flags; 19380Sstevel@tonic-gate 19390Sstevel@tonic-gate switch (family) { 19400Sstevel@tonic-gate case AF_INET: 19410Sstevel@tonic-gate slen = sizeof (struct sockaddr_in); 19420Sstevel@tonic-gate sin.sin_addr = in->addr; 19430Sstevel@tonic-gate sin.sin_port = 0; 19440Sstevel@tonic-gate sa = (struct sockaddr *)&sin; 19450Sstevel@tonic-gate break; 19460Sstevel@tonic-gate case AF_INET6: 19470Sstevel@tonic-gate slen = sizeof (struct sockaddr_in6); 19480Sstevel@tonic-gate sin6.sin6_addr = in->addr6; 19490Sstevel@tonic-gate sin6.sin6_port = 0; 19505246Sdanmcd sin6.sin6_scope_id = 0; 19510Sstevel@tonic-gate sa = (struct sockaddr *)&sin6; 19520Sstevel@tonic-gate break; 19535246Sdanmcd default: 19540Sstevel@tonic-gate (void) snprintf(line, sizeof (line), 19550Sstevel@tonic-gate "<invalid address family>"); 19560Sstevel@tonic-gate return (line); 19570Sstevel@tonic-gate } 19580Sstevel@tonic-gate sa->sa_family = family; 19590Sstevel@tonic-gate 19600Sstevel@tonic-gate if (first && !nflag) { 19610Sstevel@tonic-gate /* find out the domain name */ 19620Sstevel@tonic-gate first = _B_FALSE; 19630Sstevel@tonic-gate if (gethostname(domain, MAXHOSTNAMELEN) == 0 && 19640Sstevel@tonic-gate (cp = strchr(domain, '.')) != NULL) { 19650Sstevel@tonic-gate (void) strncpy(domain, cp + 1, sizeof (domain) - 1); 19660Sstevel@tonic-gate domain[sizeof (domain) - 1] = '\0'; 19670Sstevel@tonic-gate } else { 19680Sstevel@tonic-gate domain[0] = '\0'; 19690Sstevel@tonic-gate } 19700Sstevel@tonic-gate } 19710Sstevel@tonic-gate 19720Sstevel@tonic-gate flags = (nflag) ? NI_NUMERICHOST : NI_NAMEREQD; 19730Sstevel@tonic-gate if (getnameinfo(sa, slen, hbuf, sizeof (hbuf), NULL, 0, flags) != 0) { 19740Sstevel@tonic-gate if (inet_ntop(family, (const void *)&in->addr6, 19750Sstevel@tonic-gate hbuf, sizeof (hbuf)) == NULL) 19760Sstevel@tonic-gate hbuf[0] = 0; 19770Sstevel@tonic-gate } else if (!nflag && (cp = strchr(hbuf, '.')) != NULL && 19780Sstevel@tonic-gate strcmp(cp + 1, domain) == 0) { 19790Sstevel@tonic-gate *cp = '\0'; 19800Sstevel@tonic-gate } 19810Sstevel@tonic-gate (void) strlcpy(line, hbuf, sizeof (line)); 19820Sstevel@tonic-gate 19830Sstevel@tonic-gate return (line); 19840Sstevel@tonic-gate } 19850Sstevel@tonic-gate 19860Sstevel@tonic-gate /* 19870Sstevel@tonic-gate * return the difference (in msec) between two time values 19880Sstevel@tonic-gate */ 19890Sstevel@tonic-gate static double 19900Sstevel@tonic-gate deltaT(struct timeval *t1p, struct timeval *t2p) 19910Sstevel@tonic-gate { 19920Sstevel@tonic-gate double dt; 19930Sstevel@tonic-gate 19940Sstevel@tonic-gate dt = (double)(t2p->tv_sec - t1p->tv_sec) * 1000.0 + 19950Sstevel@tonic-gate (double)(t2p->tv_usec - t1p->tv_usec) / 1000.0; 19960Sstevel@tonic-gate return (dt); 19970Sstevel@tonic-gate } 19980Sstevel@tonic-gate 19990Sstevel@tonic-gate /* 20000Sstevel@tonic-gate * Subtract 2 timeval structs: out = out - in. 20010Sstevel@tonic-gate * Out is assumed to be >= in. 20020Sstevel@tonic-gate */ 20030Sstevel@tonic-gate static void 20040Sstevel@tonic-gate tv_sub(struct timeval *out, struct timeval *in) 20050Sstevel@tonic-gate { 20060Sstevel@tonic-gate if ((out->tv_usec -= in->tv_usec) < 0) { 20070Sstevel@tonic-gate --out->tv_sec; 20080Sstevel@tonic-gate out->tv_usec += 1000000; 20090Sstevel@tonic-gate } 20100Sstevel@tonic-gate out->tv_sec -= in->tv_sec; 20110Sstevel@tonic-gate } 20120Sstevel@tonic-gate 20130Sstevel@tonic-gate /* 20140Sstevel@tonic-gate * record statistics 20150Sstevel@tonic-gate */ 20160Sstevel@tonic-gate static void 20170Sstevel@tonic-gate record_stats(double rtt, int *nreceived, double *rttmin, double *rttmax, 20180Sstevel@tonic-gate double *rttsum, double *rttssq) 20190Sstevel@tonic-gate { 20200Sstevel@tonic-gate if (*nreceived == 0) { 20210Sstevel@tonic-gate *rttmin = rtt; 20220Sstevel@tonic-gate *rttmax = rtt; 20230Sstevel@tonic-gate *rttsum = rtt; 20240Sstevel@tonic-gate *rttssq = rtt * rtt; 20250Sstevel@tonic-gate } else { 20260Sstevel@tonic-gate if (rtt < *rttmin) 20270Sstevel@tonic-gate *rttmin = rtt; 20280Sstevel@tonic-gate 20290Sstevel@tonic-gate if (rtt > *rttmax) 20300Sstevel@tonic-gate *rttmax = rtt; 20310Sstevel@tonic-gate 20320Sstevel@tonic-gate *rttsum += rtt; 20330Sstevel@tonic-gate *rttssq += rtt * rtt; 20340Sstevel@tonic-gate } 20350Sstevel@tonic-gate 20360Sstevel@tonic-gate (*nreceived)++; 20370Sstevel@tonic-gate } 20380Sstevel@tonic-gate 20390Sstevel@tonic-gate /* 20400Sstevel@tonic-gate * display statistics 20410Sstevel@tonic-gate */ 20420Sstevel@tonic-gate static void 20430Sstevel@tonic-gate print_stats(int ntransmitted, int nreceived, double rttmin, double rttmax, 20440Sstevel@tonic-gate double rttsum, double rttssq) 20450Sstevel@tonic-gate { 20460Sstevel@tonic-gate double rttavg; /* average round-trip time */ 20470Sstevel@tonic-gate double rttstd; /* rtt standard deviation */ 20480Sstevel@tonic-gate 20490Sstevel@tonic-gate if (ntransmitted > 0 && ntransmitted >= nreceived) { 20500Sstevel@tonic-gate int missed = ntransmitted - nreceived; 20510Sstevel@tonic-gate double loss = 100 * (double)missed / (double)ntransmitted; 20520Sstevel@tonic-gate 20530Sstevel@tonic-gate if (nreceived > 0) { 20540Sstevel@tonic-gate rttavg = rttsum / nreceived; 20550Sstevel@tonic-gate rttstd = rttssq - (rttavg * rttsum); 20560Sstevel@tonic-gate rttstd = xsqrt(rttstd / nreceived); 20570Sstevel@tonic-gate 20580Sstevel@tonic-gate Printf(" %.3f", rttmin); 20590Sstevel@tonic-gate Printf("/%.3f", rttavg); 20600Sstevel@tonic-gate Printf("/%.3f", rttmax); 20610Sstevel@tonic-gate 20620Sstevel@tonic-gate Printf(" (%.3f) ms ", rttstd); 20630Sstevel@tonic-gate } 20640Sstevel@tonic-gate 20650Sstevel@tonic-gate Printf(" %d/%d pkts", nreceived, ntransmitted); 20660Sstevel@tonic-gate 20670Sstevel@tonic-gate if (nreceived == 0) 20680Sstevel@tonic-gate Printf(" (100%% loss)"); 20690Sstevel@tonic-gate else 20700Sstevel@tonic-gate Printf(" (%.2g%% loss)", loss); 20710Sstevel@tonic-gate } 20720Sstevel@tonic-gate } 20730Sstevel@tonic-gate 20740Sstevel@tonic-gate /* 20750Sstevel@tonic-gate * square root function 20760Sstevel@tonic-gate */ 20770Sstevel@tonic-gate double 20780Sstevel@tonic-gate xsqrt(double y) 20790Sstevel@tonic-gate { 20800Sstevel@tonic-gate double t, x; 20810Sstevel@tonic-gate 20820Sstevel@tonic-gate if (y <= 0) { 20830Sstevel@tonic-gate return (0.0); 20840Sstevel@tonic-gate } 20850Sstevel@tonic-gate 20860Sstevel@tonic-gate x = (y < 1.0) ? 1.0 : y; 20870Sstevel@tonic-gate do { 20880Sstevel@tonic-gate t = x; 20890Sstevel@tonic-gate x = (t + (y/t))/2.0; 20900Sstevel@tonic-gate } while (0 < x && x < t); 20910Sstevel@tonic-gate 20920Sstevel@tonic-gate return (x); 20930Sstevel@tonic-gate } 20940Sstevel@tonic-gate 20950Sstevel@tonic-gate /* 20960Sstevel@tonic-gate * String to double with optional min and max. 20970Sstevel@tonic-gate */ 20980Sstevel@tonic-gate static double 20990Sstevel@tonic-gate str2dbl(const char *str, const char *what, double mi, double ma) 21000Sstevel@tonic-gate { 21010Sstevel@tonic-gate double val; 21020Sstevel@tonic-gate char *ep; 21030Sstevel@tonic-gate 21040Sstevel@tonic-gate errno = 0; 21050Sstevel@tonic-gate 21060Sstevel@tonic-gate val = strtod(str, &ep); 21070Sstevel@tonic-gate if (errno != 0 || *ep != '\0') { 21080Sstevel@tonic-gate Fprintf(stderr, "%s: \"%s\" bad value for %s \n", 21090Sstevel@tonic-gate prog, str, what); 21100Sstevel@tonic-gate exit(EXIT_FAILURE); 21110Sstevel@tonic-gate } 21120Sstevel@tonic-gate if (val < mi && mi >= 0) { 21130Sstevel@tonic-gate Fprintf(stderr, "%s: %s must be >= %f\n", prog, what, mi); 21140Sstevel@tonic-gate exit(EXIT_FAILURE); 21150Sstevel@tonic-gate } 21160Sstevel@tonic-gate if (val > ma && ma >= 0) { 21170Sstevel@tonic-gate Fprintf(stderr, "%s: %s must be <= %f\n", prog, what, ma); 21180Sstevel@tonic-gate exit(EXIT_FAILURE); 21190Sstevel@tonic-gate } 21200Sstevel@tonic-gate return (val); 21210Sstevel@tonic-gate } 21220Sstevel@tonic-gate 21230Sstevel@tonic-gate /* 21240Sstevel@tonic-gate * String to int with optional min and max. Handles decimal and hex. 21250Sstevel@tonic-gate */ 21260Sstevel@tonic-gate static int 21270Sstevel@tonic-gate str2int(const char *str, const char *what, int mi, int ma) 21280Sstevel@tonic-gate { 21290Sstevel@tonic-gate const char *cp; 21300Sstevel@tonic-gate int val; 21310Sstevel@tonic-gate char *ep; 21320Sstevel@tonic-gate 21330Sstevel@tonic-gate errno = 0; 21340Sstevel@tonic-gate 21350Sstevel@tonic-gate if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) { 21360Sstevel@tonic-gate cp = str + 2; 21370Sstevel@tonic-gate val = (int)strtol(cp, &ep, 16); 21380Sstevel@tonic-gate } else { 21390Sstevel@tonic-gate val = (int)strtol(str, &ep, 10); 21400Sstevel@tonic-gate } 21410Sstevel@tonic-gate if (errno != 0 || *ep != '\0') { 21420Sstevel@tonic-gate Fprintf(stderr, "%s: \"%s\" bad value for %s \n", 21430Sstevel@tonic-gate prog, str, what); 21440Sstevel@tonic-gate exit(EXIT_FAILURE); 21450Sstevel@tonic-gate } 21460Sstevel@tonic-gate if (val < mi && mi >= 0) { 21470Sstevel@tonic-gate if (mi == 0) { 21480Sstevel@tonic-gate Fprintf(stderr, "%s: %s must be >= %d\n", 21490Sstevel@tonic-gate prog, what, mi); 21500Sstevel@tonic-gate } else { 21510Sstevel@tonic-gate Fprintf(stderr, "%s: %s must be > %d\n", 21520Sstevel@tonic-gate prog, what, mi - 1); 21530Sstevel@tonic-gate } 21540Sstevel@tonic-gate exit(EXIT_FAILURE); 21550Sstevel@tonic-gate } 21560Sstevel@tonic-gate if (val > ma && ma >= 0) { 21570Sstevel@tonic-gate Fprintf(stderr, "%s: %s must be <= %d\n", prog, what, ma); 21580Sstevel@tonic-gate exit(EXIT_FAILURE); 21590Sstevel@tonic-gate } 21600Sstevel@tonic-gate return (val); 21610Sstevel@tonic-gate } 21620Sstevel@tonic-gate 21630Sstevel@tonic-gate /* 21640Sstevel@tonic-gate * This is the interrupt handler for SIGINT and SIGQUIT. It's completely handled 21650Sstevel@tonic-gate * where it jumps to. 21660Sstevel@tonic-gate */ 21670Sstevel@tonic-gate static void 21680Sstevel@tonic-gate sig_handler(int sig) 21690Sstevel@tonic-gate { 21700Sstevel@tonic-gate longjmp(env, sig); 21710Sstevel@tonic-gate } 21720Sstevel@tonic-gate 21730Sstevel@tonic-gate /* 21740Sstevel@tonic-gate * display the usage of traceroute 21750Sstevel@tonic-gate */ 21760Sstevel@tonic-gate static void 21770Sstevel@tonic-gate usage(void) 21780Sstevel@tonic-gate { 21790Sstevel@tonic-gate Fprintf(stderr, "Usage: %s [-adFIlnSvx] [-A address_family] " 2180*8485SPeter.Memishian@Sun.COM "[-c traffic_class]\n" 2181*8485SPeter.Memishian@Sun.COM "\t[-f first_hop] [-g gateway [-g gateway ...]| -r] [-i iface]\n" 2182*8485SPeter.Memishian@Sun.COM "\t[-L flow_label] [-m max_hop] [-P pause_sec] [-p port] " 2183*8485SPeter.Memishian@Sun.COM "[-Q max_timeout]\n" 2184*8485SPeter.Memishian@Sun.COM "\t[-q nqueries] [-s src_addr] [-t tos] [-w wait_time] host " 2185*8485SPeter.Memishian@Sun.COM "[packetlen]\n", prog); 21860Sstevel@tonic-gate exit(EXIT_FAILURE); 21870Sstevel@tonic-gate } 2188