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