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