1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 3*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 4*7c478bd9Sstevel@tonic-gate */ 5*7c478bd9Sstevel@tonic-gate 6*7c478bd9Sstevel@tonic-gate /* 7*7c478bd9Sstevel@tonic-gate * Copyright (c) 1988, 1989, 1991, 1994, 1995, 1996, 1997 8*7c478bd9Sstevel@tonic-gate * The Regents of the University of California. All rights reserved. 9*7c478bd9Sstevel@tonic-gate * 10*7c478bd9Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 11*7c478bd9Sstevel@tonic-gate * modification, are permitted provided that: (1) source code distributions 12*7c478bd9Sstevel@tonic-gate * retain the above copyright notice and this paragraph in its entirety, (2) 13*7c478bd9Sstevel@tonic-gate * distributions including binary code include the above copyright notice and 14*7c478bd9Sstevel@tonic-gate * this paragraph in its entirety in the documentation or other materials 15*7c478bd9Sstevel@tonic-gate * provided with the distribution, and (3) all advertising materials mentioning 16*7c478bd9Sstevel@tonic-gate * features or use of this software display the following acknowledgement: 17*7c478bd9Sstevel@tonic-gate * ``This product includes software developed by the University of California, 18*7c478bd9Sstevel@tonic-gate * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 19*7c478bd9Sstevel@tonic-gate * the University nor the names of its contributors may be used to endorse 20*7c478bd9Sstevel@tonic-gate * or promote products derived from this software without specific prior 21*7c478bd9Sstevel@tonic-gate * written permission. 22*7c478bd9Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 23*7c478bd9Sstevel@tonic-gate * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 24*7c478bd9Sstevel@tonic-gate * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 25*7c478bd9Sstevel@tonic-gate * 26*7c478bd9Sstevel@tonic-gate * 27*7c478bd9Sstevel@tonic-gate * @(#)$Header: traceroute.c,v 1.49 97/06/13 02:30:23 leres Exp $ (LBL) 28*7c478bd9Sstevel@tonic-gate */ 29*7c478bd9Sstevel@tonic-gate 30*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 31*7c478bd9Sstevel@tonic-gate 32*7c478bd9Sstevel@tonic-gate #include <sys/socket.h> 33*7c478bd9Sstevel@tonic-gate 34*7c478bd9Sstevel@tonic-gate #include <stdio.h> 35*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 36*7c478bd9Sstevel@tonic-gate #include <ctype.h> 37*7c478bd9Sstevel@tonic-gate #include <strings.h> 38*7c478bd9Sstevel@tonic-gate #include <libintl.h> 39*7c478bd9Sstevel@tonic-gate #include <errno.h> 40*7c478bd9Sstevel@tonic-gate 41*7c478bd9Sstevel@tonic-gate #include <netinet/in_systm.h> 42*7c478bd9Sstevel@tonic-gate #include <netinet/in.h> 43*7c478bd9Sstevel@tonic-gate #include <netinet/ip.h> 44*7c478bd9Sstevel@tonic-gate #include <netinet/ip_var.h> 45*7c478bd9Sstevel@tonic-gate #include <netinet/ip_icmp.h> 46*7c478bd9Sstevel@tonic-gate #include <netinet/udp.h> 47*7c478bd9Sstevel@tonic-gate #include <netinet/udp_var.h> 48*7c478bd9Sstevel@tonic-gate 49*7c478bd9Sstevel@tonic-gate #include <arpa/inet.h> 50*7c478bd9Sstevel@tonic-gate #include <netdb.h> 51*7c478bd9Sstevel@tonic-gate 52*7c478bd9Sstevel@tonic-gate #include <ifaddrlist.h> 53*7c478bd9Sstevel@tonic-gate #include "traceroute.h" 54*7c478bd9Sstevel@tonic-gate 55*7c478bd9Sstevel@tonic-gate /* 56*7c478bd9Sstevel@tonic-gate * IPv4 source routing option. 57*7c478bd9Sstevel@tonic-gate * In order to avoid padding for the alignment of IPv4 addresses, ipsr_addrs 58*7c478bd9Sstevel@tonic-gate * is defined as a 2-D array of uint8_t, instead of 1-D array of struct in_addr. 59*7c478bd9Sstevel@tonic-gate */ 60*7c478bd9Sstevel@tonic-gate struct ip_sourceroute { 61*7c478bd9Sstevel@tonic-gate uint8_t ipsr_code; 62*7c478bd9Sstevel@tonic-gate uint8_t ipsr_len; 63*7c478bd9Sstevel@tonic-gate uint8_t ipsr_ptr; 64*7c478bd9Sstevel@tonic-gate /* up to 9 IPv4 addresses */ 65*7c478bd9Sstevel@tonic-gate uint8_t ipsr_addrs[1][sizeof (struct in_addr)]; 66*7c478bd9Sstevel@tonic-gate }; 67*7c478bd9Sstevel@tonic-gate 68*7c478bd9Sstevel@tonic-gate int check_reply(struct msghdr *, int, int, uchar_t *, uchar_t *); 69*7c478bd9Sstevel@tonic-gate extern ushort_t in_cksum(ushort_t *, int); 70*7c478bd9Sstevel@tonic-gate extern char *inet_name(union any_in_addr *, int); 71*7c478bd9Sstevel@tonic-gate static char *pr_type(uchar_t); 72*7c478bd9Sstevel@tonic-gate void print_addr(uchar_t *, int, struct sockaddr *); 73*7c478bd9Sstevel@tonic-gate boolean_t print_icmp_other(uchar_t, uchar_t); 74*7c478bd9Sstevel@tonic-gate void send_probe(int, struct sockaddr *, struct ip *, int, int, 75*7c478bd9Sstevel@tonic-gate struct timeval *, int); 76*7c478bd9Sstevel@tonic-gate struct ip *set_buffers(int); 77*7c478bd9Sstevel@tonic-gate void set_IPv4opt_sourcerouting(int, union any_in_addr *, union any_in_addr *); 78*7c478bd9Sstevel@tonic-gate 79*7c478bd9Sstevel@tonic-gate /* 80*7c478bd9Sstevel@tonic-gate * prepares the buffer to be sent as an IP datagram 81*7c478bd9Sstevel@tonic-gate */ 82*7c478bd9Sstevel@tonic-gate struct ip * 83*7c478bd9Sstevel@tonic-gate set_buffers(int plen) 84*7c478bd9Sstevel@tonic-gate { 85*7c478bd9Sstevel@tonic-gate struct ip *outip; 86*7c478bd9Sstevel@tonic-gate uchar_t *outp; /* packet following the IP header (UDP/ICMP) */ 87*7c478bd9Sstevel@tonic-gate struct udphdr *outudp; 88*7c478bd9Sstevel@tonic-gate struct icmp *outicmp; 89*7c478bd9Sstevel@tonic-gate int optlen = 0; 90*7c478bd9Sstevel@tonic-gate 91*7c478bd9Sstevel@tonic-gate outip = (struct ip *)malloc((size_t)plen); 92*7c478bd9Sstevel@tonic-gate if (outip == NULL) { 93*7c478bd9Sstevel@tonic-gate Fprintf(stderr, "%s: malloc: %s\n", prog, strerror(errno)); 94*7c478bd9Sstevel@tonic-gate exit(EXIT_FAILURE); 95*7c478bd9Sstevel@tonic-gate } 96*7c478bd9Sstevel@tonic-gate 97*7c478bd9Sstevel@tonic-gate if (gw_count > 0) { 98*7c478bd9Sstevel@tonic-gate /* 8 = 5 (NO OPs) + 3 (code, len, ptr) */ 99*7c478bd9Sstevel@tonic-gate optlen = 8 + gw_count * sizeof (struct in_addr); 100*7c478bd9Sstevel@tonic-gate } 101*7c478bd9Sstevel@tonic-gate 102*7c478bd9Sstevel@tonic-gate (void) memset((char *)outip, 0, (size_t)plen); 103*7c478bd9Sstevel@tonic-gate outp = (uchar_t *)(outip + 1); 104*7c478bd9Sstevel@tonic-gate 105*7c478bd9Sstevel@tonic-gate outip->ip_v = IPVERSION; 106*7c478bd9Sstevel@tonic-gate if (settos) 107*7c478bd9Sstevel@tonic-gate outip->ip_tos = tos; 108*7c478bd9Sstevel@tonic-gate 109*7c478bd9Sstevel@tonic-gate /* 110*7c478bd9Sstevel@tonic-gate * LBNL bug fixed: missing '- optlen' before, causing optlen 111*7c478bd9Sstevel@tonic-gate * added twice 112*7c478bd9Sstevel@tonic-gate * 113*7c478bd9Sstevel@tonic-gate * BSD bug: BSD touches the header fields 'len' and 'ip_off' 114*7c478bd9Sstevel@tonic-gate * even when HDRINCL is set. It applies htons() on these 115*7c478bd9Sstevel@tonic-gate * fields. It should send the header untouched when HDRINCL 116*7c478bd9Sstevel@tonic-gate * is set. 117*7c478bd9Sstevel@tonic-gate */ 118*7c478bd9Sstevel@tonic-gate outip->ip_len = htons(plen - optlen); 119*7c478bd9Sstevel@tonic-gate outip->ip_off = htons(off); 120*7c478bd9Sstevel@tonic-gate outip->ip_hl = (outp - (uchar_t *)outip) >> 2; 121*7c478bd9Sstevel@tonic-gate 122*7c478bd9Sstevel@tonic-gate /* setup ICMP or UDP */ 123*7c478bd9Sstevel@tonic-gate if (useicmp) { 124*7c478bd9Sstevel@tonic-gate outip->ip_p = IPPROTO_ICMP; 125*7c478bd9Sstevel@tonic-gate 126*7c478bd9Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 127*7c478bd9Sstevel@tonic-gate outicmp = (struct icmp *)outp; 128*7c478bd9Sstevel@tonic-gate outicmp->icmp_type = ICMP_ECHO; 129*7c478bd9Sstevel@tonic-gate outicmp->icmp_id = htons(ident); 130*7c478bd9Sstevel@tonic-gate } else { 131*7c478bd9Sstevel@tonic-gate outip->ip_p = IPPROTO_UDP; 132*7c478bd9Sstevel@tonic-gate 133*7c478bd9Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 134*7c478bd9Sstevel@tonic-gate outudp = (struct udphdr *)outp; 135*7c478bd9Sstevel@tonic-gate outudp->uh_sport = htons(ident); 136*7c478bd9Sstevel@tonic-gate outudp->uh_ulen = 137*7c478bd9Sstevel@tonic-gate htons((ushort_t)(plen - (sizeof (struct ip) + optlen))); 138*7c478bd9Sstevel@tonic-gate } 139*7c478bd9Sstevel@tonic-gate 140*7c478bd9Sstevel@tonic-gate return (outip); 141*7c478bd9Sstevel@tonic-gate } 142*7c478bd9Sstevel@tonic-gate 143*7c478bd9Sstevel@tonic-gate /* 144*7c478bd9Sstevel@tonic-gate * Setup the source routing for IPv4. 145*7c478bd9Sstevel@tonic-gate */ 146*7c478bd9Sstevel@tonic-gate void 147*7c478bd9Sstevel@tonic-gate set_IPv4opt_sourcerouting(int sndsock, union any_in_addr *ip_addr, 148*7c478bd9Sstevel@tonic-gate union any_in_addr *gwIPlist) 149*7c478bd9Sstevel@tonic-gate { 150*7c478bd9Sstevel@tonic-gate struct protoent *pe; 151*7c478bd9Sstevel@tonic-gate struct ip_sourceroute *srp; 152*7c478bd9Sstevel@tonic-gate uchar_t optlist[MAX_IPOPTLEN]; 153*7c478bd9Sstevel@tonic-gate int i; 154*7c478bd9Sstevel@tonic-gate 155*7c478bd9Sstevel@tonic-gate if ((pe = getprotobyname("ip")) == NULL) { 156*7c478bd9Sstevel@tonic-gate Fprintf(stderr, "%s: unknown protocol ip\n", prog); 157*7c478bd9Sstevel@tonic-gate exit(EXIT_FAILURE); 158*7c478bd9Sstevel@tonic-gate } 159*7c478bd9Sstevel@tonic-gate 160*7c478bd9Sstevel@tonic-gate /* final hop */ 161*7c478bd9Sstevel@tonic-gate gwIPlist[gw_count].addr = ip_addr->addr; 162*7c478bd9Sstevel@tonic-gate 163*7c478bd9Sstevel@tonic-gate /* 164*7c478bd9Sstevel@tonic-gate * the option length passed to setsockopt() needs to be a multiple of 165*7c478bd9Sstevel@tonic-gate * 32 bits. Therefore we need to use a 1-byte padding (source routing 166*7c478bd9Sstevel@tonic-gate * information takes 4x+3 bytes). 167*7c478bd9Sstevel@tonic-gate */ 168*7c478bd9Sstevel@tonic-gate optlist[0] = IPOPT_NOP; 169*7c478bd9Sstevel@tonic-gate 170*7c478bd9Sstevel@tonic-gate srp = (struct ip_sourceroute *)&optlist[1]; 171*7c478bd9Sstevel@tonic-gate srp->ipsr_code = IPOPT_LSRR; 172*7c478bd9Sstevel@tonic-gate /* 3 = 1 (code) + 1 (len) + 1 (ptr) */ 173*7c478bd9Sstevel@tonic-gate srp->ipsr_len = 3 + (gw_count + 1) * sizeof (gwIPlist[0].addr); 174*7c478bd9Sstevel@tonic-gate srp->ipsr_ptr = IPOPT_MINOFF; 175*7c478bd9Sstevel@tonic-gate 176*7c478bd9Sstevel@tonic-gate for (i = 0; i <= gw_count; i++) { 177*7c478bd9Sstevel@tonic-gate (void) bcopy((char *)&gwIPlist[i].addr, &srp->ipsr_addrs[i], 178*7c478bd9Sstevel@tonic-gate sizeof (struct in_addr)); 179*7c478bd9Sstevel@tonic-gate } 180*7c478bd9Sstevel@tonic-gate 181*7c478bd9Sstevel@tonic-gate if (setsockopt(sndsock, pe->p_proto, IP_OPTIONS, (const char *)optlist, 182*7c478bd9Sstevel@tonic-gate srp->ipsr_len + 1) < 0) { 183*7c478bd9Sstevel@tonic-gate Fprintf(stderr, "%s: IP_OPTIONS: %s\n", prog, strerror(errno)); 184*7c478bd9Sstevel@tonic-gate exit(EXIT_FAILURE); 185*7c478bd9Sstevel@tonic-gate } 186*7c478bd9Sstevel@tonic-gate } 187*7c478bd9Sstevel@tonic-gate 188*7c478bd9Sstevel@tonic-gate /* 189*7c478bd9Sstevel@tonic-gate * send a probe packet to the destination 190*7c478bd9Sstevel@tonic-gate */ 191*7c478bd9Sstevel@tonic-gate void 192*7c478bd9Sstevel@tonic-gate send_probe(int sndsock, struct sockaddr *to, struct ip *outip, 193*7c478bd9Sstevel@tonic-gate int seq, int ttl, struct timeval *tp, int packlen) 194*7c478bd9Sstevel@tonic-gate { 195*7c478bd9Sstevel@tonic-gate int cc; 196*7c478bd9Sstevel@tonic-gate struct udpiphdr *ui; 197*7c478bd9Sstevel@tonic-gate uchar_t *outp; /* packet following the IP header (UDP/ICMP) */ 198*7c478bd9Sstevel@tonic-gate struct udphdr *outudp; 199*7c478bd9Sstevel@tonic-gate struct icmp *outicmp; 200*7c478bd9Sstevel@tonic-gate struct outdata *outdata; 201*7c478bd9Sstevel@tonic-gate struct ip tip; 202*7c478bd9Sstevel@tonic-gate int optlen = 0; 203*7c478bd9Sstevel@tonic-gate int send_size; 204*7c478bd9Sstevel@tonic-gate 205*7c478bd9Sstevel@tonic-gate /* initialize buffer pointers */ 206*7c478bd9Sstevel@tonic-gate outp = (uchar_t *)(outip + 1); 207*7c478bd9Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 208*7c478bd9Sstevel@tonic-gate outudp = (struct udphdr *)outp; 209*7c478bd9Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 210*7c478bd9Sstevel@tonic-gate outicmp = (struct icmp *)outp; 211*7c478bd9Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 212*7c478bd9Sstevel@tonic-gate outdata = (struct outdata *)(outp + ICMP_MINLEN); 213*7c478bd9Sstevel@tonic-gate 214*7c478bd9Sstevel@tonic-gate if (gw_count > 0) { 215*7c478bd9Sstevel@tonic-gate /* 8 = 5 (NO OPs) + 3 (code, len, ptr) */ 216*7c478bd9Sstevel@tonic-gate optlen = 8 + gw_count * sizeof (struct in_addr); 217*7c478bd9Sstevel@tonic-gate } 218*7c478bd9Sstevel@tonic-gate 219*7c478bd9Sstevel@tonic-gate if (raw_req) { 220*7c478bd9Sstevel@tonic-gate send_size = packlen - optlen; 221*7c478bd9Sstevel@tonic-gate } else if (useicmp) { 222*7c478bd9Sstevel@tonic-gate send_size = packlen - optlen - sizeof (struct ip); 223*7c478bd9Sstevel@tonic-gate } else { 224*7c478bd9Sstevel@tonic-gate send_size = packlen - optlen - sizeof (struct ip) - 225*7c478bd9Sstevel@tonic-gate sizeof (struct udphdr); 226*7c478bd9Sstevel@tonic-gate } 227*7c478bd9Sstevel@tonic-gate 228*7c478bd9Sstevel@tonic-gate outip->ip_ttl = ttl; 229*7c478bd9Sstevel@tonic-gate outip->ip_id = htons(ident + seq); 230*7c478bd9Sstevel@tonic-gate 231*7c478bd9Sstevel@tonic-gate /* 232*7c478bd9Sstevel@tonic-gate * If a raw IPv4 packet is going to be sent, the Time to Live 233*7c478bd9Sstevel@tonic-gate * field in the packet was initialized above. Otherwise, it is 234*7c478bd9Sstevel@tonic-gate * initialized here using the IPPROTO_IP level socket option. 235*7c478bd9Sstevel@tonic-gate */ 236*7c478bd9Sstevel@tonic-gate if (!raw_req) { 237*7c478bd9Sstevel@tonic-gate if (setsockopt(sndsock, IPPROTO_IP, IP_TTL, (char *)&ttl, 238*7c478bd9Sstevel@tonic-gate sizeof (ttl)) < 0) { 239*7c478bd9Sstevel@tonic-gate Fprintf(stderr, "%s: IP_TTL: %s\n", prog, 240*7c478bd9Sstevel@tonic-gate strerror(errno)); 241*7c478bd9Sstevel@tonic-gate exit(EXIT_FAILURE); 242*7c478bd9Sstevel@tonic-gate } 243*7c478bd9Sstevel@tonic-gate } 244*7c478bd9Sstevel@tonic-gate 245*7c478bd9Sstevel@tonic-gate /* 246*7c478bd9Sstevel@tonic-gate * In most cases, the kernel will recalculate the ip checksum. 247*7c478bd9Sstevel@tonic-gate * But we must do it anyway so that the udp checksum comes out 248*7c478bd9Sstevel@tonic-gate * right. 249*7c478bd9Sstevel@tonic-gate */ 250*7c478bd9Sstevel@tonic-gate if (docksum) { 251*7c478bd9Sstevel@tonic-gate outip->ip_sum = 252*7c478bd9Sstevel@tonic-gate in_cksum((ushort_t *)outip, sizeof (*outip) + optlen); 253*7c478bd9Sstevel@tonic-gate if (outip->ip_sum == 0) 254*7c478bd9Sstevel@tonic-gate outip->ip_sum = 0xffff; 255*7c478bd9Sstevel@tonic-gate } 256*7c478bd9Sstevel@tonic-gate 257*7c478bd9Sstevel@tonic-gate /* Payload */ 258*7c478bd9Sstevel@tonic-gate outdata->seq = seq; 259*7c478bd9Sstevel@tonic-gate outdata->ttl = ttl; 260*7c478bd9Sstevel@tonic-gate outdata->tv = *tp; 261*7c478bd9Sstevel@tonic-gate 262*7c478bd9Sstevel@tonic-gate if (useicmp) { 263*7c478bd9Sstevel@tonic-gate outicmp->icmp_seq = htons(seq); 264*7c478bd9Sstevel@tonic-gate } else { 265*7c478bd9Sstevel@tonic-gate outudp->uh_dport = htons((port + seq) % (MAX_PORT + 1)); 266*7c478bd9Sstevel@tonic-gate } 267*7c478bd9Sstevel@tonic-gate 268*7c478bd9Sstevel@tonic-gate if (!raw_req) 269*7c478bd9Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 270*7c478bd9Sstevel@tonic-gate ((struct sockaddr_in *)to)->sin_port = outudp->uh_dport; 271*7c478bd9Sstevel@tonic-gate 272*7c478bd9Sstevel@tonic-gate /* (We can only do the checksum if we know our ip address) */ 273*7c478bd9Sstevel@tonic-gate if (docksum) { 274*7c478bd9Sstevel@tonic-gate if (useicmp) { 275*7c478bd9Sstevel@tonic-gate outicmp->icmp_cksum = 0; 276*7c478bd9Sstevel@tonic-gate outicmp->icmp_cksum = in_cksum((ushort_t *)outicmp, 277*7c478bd9Sstevel@tonic-gate packlen - (sizeof (struct ip) + optlen)); 278*7c478bd9Sstevel@tonic-gate if (outicmp->icmp_cksum == 0) 279*7c478bd9Sstevel@tonic-gate outicmp->icmp_cksum = 0xffff; 280*7c478bd9Sstevel@tonic-gate } else { 281*7c478bd9Sstevel@tonic-gate /* Checksum (must save and restore ip header) */ 282*7c478bd9Sstevel@tonic-gate tip = *outip; 283*7c478bd9Sstevel@tonic-gate ui = (struct udpiphdr *)outip; 284*7c478bd9Sstevel@tonic-gate ui->ui_next = 0; 285*7c478bd9Sstevel@tonic-gate ui->ui_prev = 0; 286*7c478bd9Sstevel@tonic-gate ui->ui_x1 = 0; 287*7c478bd9Sstevel@tonic-gate ui->ui_len = outudp->uh_ulen; 288*7c478bd9Sstevel@tonic-gate outudp->uh_sum = 0; 289*7c478bd9Sstevel@tonic-gate outudp->uh_sum = in_cksum((ushort_t *)ui, packlen); 290*7c478bd9Sstevel@tonic-gate if (outudp->uh_sum == 0) 291*7c478bd9Sstevel@tonic-gate outudp->uh_sum = 0xffff; 292*7c478bd9Sstevel@tonic-gate *outip = tip; 293*7c478bd9Sstevel@tonic-gate } 294*7c478bd9Sstevel@tonic-gate } 295*7c478bd9Sstevel@tonic-gate 296*7c478bd9Sstevel@tonic-gate if (raw_req) { 297*7c478bd9Sstevel@tonic-gate cc = sendto(sndsock, (char *)outip, send_size, 0, to, 298*7c478bd9Sstevel@tonic-gate sizeof (struct sockaddr_in)); 299*7c478bd9Sstevel@tonic-gate } else if (useicmp) { 300*7c478bd9Sstevel@tonic-gate cc = sendto(sndsock, (char *)outicmp, send_size, 0, to, 301*7c478bd9Sstevel@tonic-gate sizeof (struct sockaddr_in)); 302*7c478bd9Sstevel@tonic-gate } else { 303*7c478bd9Sstevel@tonic-gate cc = sendto(sndsock, (char *)outp, send_size, 0, to, 304*7c478bd9Sstevel@tonic-gate sizeof (struct sockaddr_in)); 305*7c478bd9Sstevel@tonic-gate } 306*7c478bd9Sstevel@tonic-gate 307*7c478bd9Sstevel@tonic-gate if (cc < 0 || cc != send_size) { 308*7c478bd9Sstevel@tonic-gate if (cc < 0) { 309*7c478bd9Sstevel@tonic-gate Fprintf(stderr, "%s: sendto: %s\n", prog, 310*7c478bd9Sstevel@tonic-gate strerror(errno)); 311*7c478bd9Sstevel@tonic-gate } 312*7c478bd9Sstevel@tonic-gate Printf("%s: wrote %s %d chars, ret=%d\n", 313*7c478bd9Sstevel@tonic-gate prog, hostname, send_size, cc); 314*7c478bd9Sstevel@tonic-gate (void) fflush(stdout); 315*7c478bd9Sstevel@tonic-gate } 316*7c478bd9Sstevel@tonic-gate } 317*7c478bd9Sstevel@tonic-gate 318*7c478bd9Sstevel@tonic-gate /* 319*7c478bd9Sstevel@tonic-gate * Check out the reply packet to see if it's what we were expecting. 320*7c478bd9Sstevel@tonic-gate * Returns REPLY_GOT_TARGET if the reply comes from the target 321*7c478bd9Sstevel@tonic-gate * REPLY_GOT_GATEWAY if an intermediate gateway sends TIME_EXCEEDED 322*7c478bd9Sstevel@tonic-gate * REPLY_GOT_OTHER for other kinds of unreachables indicating none of 323*7c478bd9Sstevel@tonic-gate * the above two cases 324*7c478bd9Sstevel@tonic-gate * 325*7c478bd9Sstevel@tonic-gate * It also sets the icmp type and icmp code values 326*7c478bd9Sstevel@tonic-gate */ 327*7c478bd9Sstevel@tonic-gate int 328*7c478bd9Sstevel@tonic-gate check_reply(struct msghdr *msg, int cc, int seq, uchar_t *type, uchar_t *code) 329*7c478bd9Sstevel@tonic-gate { 330*7c478bd9Sstevel@tonic-gate uchar_t *buf = msg->msg_iov->iov_base; 331*7c478bd9Sstevel@tonic-gate struct sockaddr_in *from_in = (struct sockaddr_in *)msg->msg_name; 332*7c478bd9Sstevel@tonic-gate struct icmp *icp; 333*7c478bd9Sstevel@tonic-gate int hlen; 334*7c478bd9Sstevel@tonic-gate int save_cc = cc; 335*7c478bd9Sstevel@tonic-gate struct ip *ip; 336*7c478bd9Sstevel@tonic-gate 337*7c478bd9Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 338*7c478bd9Sstevel@tonic-gate ip = (struct ip *)buf; 339*7c478bd9Sstevel@tonic-gate hlen = ip->ip_hl << 2; 340*7c478bd9Sstevel@tonic-gate if (cc < hlen + ICMP_MINLEN) { 341*7c478bd9Sstevel@tonic-gate if (verbose) { 342*7c478bd9Sstevel@tonic-gate Printf("packet too short (%d bytes) from %s\n", 343*7c478bd9Sstevel@tonic-gate cc, inet_ntoa(from_in->sin_addr)); 344*7c478bd9Sstevel@tonic-gate } 345*7c478bd9Sstevel@tonic-gate return (REPLY_SHORT_PKT); 346*7c478bd9Sstevel@tonic-gate } 347*7c478bd9Sstevel@tonic-gate cc -= hlen; 348*7c478bd9Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 349*7c478bd9Sstevel@tonic-gate icp = (struct icmp *)(buf + hlen); 350*7c478bd9Sstevel@tonic-gate 351*7c478bd9Sstevel@tonic-gate *type = icp->icmp_type; 352*7c478bd9Sstevel@tonic-gate *code = icp->icmp_code; 353*7c478bd9Sstevel@tonic-gate 354*7c478bd9Sstevel@tonic-gate /* 355*7c478bd9Sstevel@tonic-gate * traceroute interpretes only ICMP_TIMXCEED_INTRANS, ICMP_UNREACH and 356*7c478bd9Sstevel@tonic-gate * ICMP_ECHOREPLY, ignores others 357*7c478bd9Sstevel@tonic-gate */ 358*7c478bd9Sstevel@tonic-gate if ((*type == ICMP_TIMXCEED && *code == ICMP_TIMXCEED_INTRANS) || 359*7c478bd9Sstevel@tonic-gate *type == ICMP_UNREACH || *type == ICMP_ECHOREPLY) { 360*7c478bd9Sstevel@tonic-gate struct ip *hip; 361*7c478bd9Sstevel@tonic-gate struct udphdr *up; 362*7c478bd9Sstevel@tonic-gate struct icmp *hicmp; 363*7c478bd9Sstevel@tonic-gate 364*7c478bd9Sstevel@tonic-gate cc -= ICMP_MINLEN; 365*7c478bd9Sstevel@tonic-gate hip = &icp->icmp_ip; 366*7c478bd9Sstevel@tonic-gate hlen = hip->ip_hl << 2; 367*7c478bd9Sstevel@tonic-gate cc -= hlen; 368*7c478bd9Sstevel@tonic-gate if (useicmp) { 369*7c478bd9Sstevel@tonic-gate if (*type == ICMP_ECHOREPLY && 370*7c478bd9Sstevel@tonic-gate icp->icmp_id == htons(ident) && 371*7c478bd9Sstevel@tonic-gate icp->icmp_seq == htons(seq)) 372*7c478bd9Sstevel@tonic-gate return (REPLY_GOT_TARGET); 373*7c478bd9Sstevel@tonic-gate 374*7c478bd9Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 375*7c478bd9Sstevel@tonic-gate hicmp = (struct icmp *)((uchar_t *)hip + hlen); 376*7c478bd9Sstevel@tonic-gate 377*7c478bd9Sstevel@tonic-gate if (ICMP_MINLEN <= cc && 378*7c478bd9Sstevel@tonic-gate hip->ip_p == IPPROTO_ICMP && 379*7c478bd9Sstevel@tonic-gate hicmp->icmp_id == htons(ident) && 380*7c478bd9Sstevel@tonic-gate hicmp->icmp_seq == htons(seq)) { 381*7c478bd9Sstevel@tonic-gate return ((*type == ICMP_TIMXCEED) ? 382*7c478bd9Sstevel@tonic-gate REPLY_GOT_GATEWAY : REPLY_GOT_OTHER); 383*7c478bd9Sstevel@tonic-gate } 384*7c478bd9Sstevel@tonic-gate } else { 385*7c478bd9Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 386*7c478bd9Sstevel@tonic-gate up = (struct udphdr *)((uchar_t *)hip + hlen); 387*7c478bd9Sstevel@tonic-gate /* 388*7c478bd9Sstevel@tonic-gate * at least 4 bytes of UDP header is required for this 389*7c478bd9Sstevel@tonic-gate * check 390*7c478bd9Sstevel@tonic-gate */ 391*7c478bd9Sstevel@tonic-gate if (4 <= cc && 392*7c478bd9Sstevel@tonic-gate hip->ip_p == IPPROTO_UDP && 393*7c478bd9Sstevel@tonic-gate up->uh_sport == htons(ident) && 394*7c478bd9Sstevel@tonic-gate up->uh_dport == htons((port + seq) % 395*7c478bd9Sstevel@tonic-gate (MAX_PORT + 1))) { 396*7c478bd9Sstevel@tonic-gate if (*type == ICMP_UNREACH && 397*7c478bd9Sstevel@tonic-gate *code == ICMP_UNREACH_PORT) { 398*7c478bd9Sstevel@tonic-gate return (REPLY_GOT_TARGET); 399*7c478bd9Sstevel@tonic-gate } else if (*type == ICMP_TIMXCEED) { 400*7c478bd9Sstevel@tonic-gate return (REPLY_GOT_GATEWAY); 401*7c478bd9Sstevel@tonic-gate } else { 402*7c478bd9Sstevel@tonic-gate return (REPLY_GOT_OTHER); 403*7c478bd9Sstevel@tonic-gate } 404*7c478bd9Sstevel@tonic-gate } 405*7c478bd9Sstevel@tonic-gate } 406*7c478bd9Sstevel@tonic-gate } 407*7c478bd9Sstevel@tonic-gate 408*7c478bd9Sstevel@tonic-gate if (verbose) { 409*7c478bd9Sstevel@tonic-gate int i, j; 410*7c478bd9Sstevel@tonic-gate uchar_t *lp = (uchar_t *)ip; 411*7c478bd9Sstevel@tonic-gate 412*7c478bd9Sstevel@tonic-gate cc = save_cc; 413*7c478bd9Sstevel@tonic-gate Printf("\n%d bytes from %s to ", cc, 414*7c478bd9Sstevel@tonic-gate inet_ntoa(from_in->sin_addr)); 415*7c478bd9Sstevel@tonic-gate Printf("%s: icmp type %d (%s) code %d\n", 416*7c478bd9Sstevel@tonic-gate inet_ntoa(ip->ip_dst), *type, pr_type(*type), *code); 417*7c478bd9Sstevel@tonic-gate for (i = 0; i < cc; i += 4) { 418*7c478bd9Sstevel@tonic-gate Printf("%2d: x", i); 419*7c478bd9Sstevel@tonic-gate for (j = 0; ((j < 4) && ((i + j) < cc)); j++) 420*7c478bd9Sstevel@tonic-gate Printf("%2.2x", *lp++); 421*7c478bd9Sstevel@tonic-gate (void) putchar('\n'); 422*7c478bd9Sstevel@tonic-gate } 423*7c478bd9Sstevel@tonic-gate } 424*7c478bd9Sstevel@tonic-gate 425*7c478bd9Sstevel@tonic-gate return (REPLY_SHORT_PKT); 426*7c478bd9Sstevel@tonic-gate } 427*7c478bd9Sstevel@tonic-gate 428*7c478bd9Sstevel@tonic-gate /* 429*7c478bd9Sstevel@tonic-gate * convert an ICMP "type" field to a printable string. 430*7c478bd9Sstevel@tonic-gate */ 431*7c478bd9Sstevel@tonic-gate static char * 432*7c478bd9Sstevel@tonic-gate pr_type(uchar_t type) 433*7c478bd9Sstevel@tonic-gate { 434*7c478bd9Sstevel@tonic-gate static struct icmptype_table ttab[] = { 435*7c478bd9Sstevel@tonic-gate {ICMP_ECHOREPLY, "Echo Reply"}, 436*7c478bd9Sstevel@tonic-gate {1, "ICMP 1"}, 437*7c478bd9Sstevel@tonic-gate {2, "ICMP 2"}, 438*7c478bd9Sstevel@tonic-gate {ICMP_UNREACH, "Dest Unreachable"}, 439*7c478bd9Sstevel@tonic-gate {ICMP_SOURCEQUENCH, "Source Quench"}, 440*7c478bd9Sstevel@tonic-gate {ICMP_REDIRECT, "Redirect"}, 441*7c478bd9Sstevel@tonic-gate {6, "ICMP 6"}, 442*7c478bd9Sstevel@tonic-gate {7, "ICMP 7"}, 443*7c478bd9Sstevel@tonic-gate {ICMP_ECHO, "Echo"}, 444*7c478bd9Sstevel@tonic-gate {ICMP_ROUTERADVERT, "Router Advertisement"}, 445*7c478bd9Sstevel@tonic-gate {ICMP_ROUTERSOLICIT, "Router Solicitation"}, 446*7c478bd9Sstevel@tonic-gate {ICMP_TIMXCEED, "Time Exceeded"}, 447*7c478bd9Sstevel@tonic-gate {ICMP_PARAMPROB, "Param Problem"}, 448*7c478bd9Sstevel@tonic-gate {ICMP_TSTAMP, "Timestamp"}, 449*7c478bd9Sstevel@tonic-gate {ICMP_TSTAMPREPLY, "Timestamp Reply"}, 450*7c478bd9Sstevel@tonic-gate {ICMP_IREQ, "Info Request"}, 451*7c478bd9Sstevel@tonic-gate {ICMP_IREQREPLY, "Info Reply"}, 452*7c478bd9Sstevel@tonic-gate {ICMP_MASKREQ, "Netmask Request"}, 453*7c478bd9Sstevel@tonic-gate {ICMP_MASKREPLY, "Netmask Reply"} 454*7c478bd9Sstevel@tonic-gate }; 455*7c478bd9Sstevel@tonic-gate int i = 0; 456*7c478bd9Sstevel@tonic-gate 457*7c478bd9Sstevel@tonic-gate for (i = 0; i < A_CNT(ttab); i++) { 458*7c478bd9Sstevel@tonic-gate if (ttab[i].type == type) 459*7c478bd9Sstevel@tonic-gate return (ttab[i].message); 460*7c478bd9Sstevel@tonic-gate } 461*7c478bd9Sstevel@tonic-gate 462*7c478bd9Sstevel@tonic-gate return ("OUT-OF-RANGE"); 463*7c478bd9Sstevel@tonic-gate } 464*7c478bd9Sstevel@tonic-gate 465*7c478bd9Sstevel@tonic-gate /* 466*7c478bd9Sstevel@tonic-gate * print the IPv4 src address of the reply packet 467*7c478bd9Sstevel@tonic-gate */ 468*7c478bd9Sstevel@tonic-gate void 469*7c478bd9Sstevel@tonic-gate print_addr(uchar_t *buf, int cc, struct sockaddr *from) 470*7c478bd9Sstevel@tonic-gate { 471*7c478bd9Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 472*7c478bd9Sstevel@tonic-gate struct sockaddr_in *from_in = (struct sockaddr_in *)from; 473*7c478bd9Sstevel@tonic-gate struct ip *ip; 474*7c478bd9Sstevel@tonic-gate union any_in_addr ip_addr; 475*7c478bd9Sstevel@tonic-gate 476*7c478bd9Sstevel@tonic-gate ip_addr.addr = from_in->sin_addr; 477*7c478bd9Sstevel@tonic-gate 478*7c478bd9Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 479*7c478bd9Sstevel@tonic-gate ip = (struct ip *)buf; 480*7c478bd9Sstevel@tonic-gate 481*7c478bd9Sstevel@tonic-gate if (nflag) { 482*7c478bd9Sstevel@tonic-gate Printf(" %s", inet_ntoa(from_in->sin_addr)); 483*7c478bd9Sstevel@tonic-gate } else { 484*7c478bd9Sstevel@tonic-gate Printf(" %s (%s)", inet_name(&ip_addr, AF_INET), 485*7c478bd9Sstevel@tonic-gate inet_ntoa(from_in->sin_addr)); 486*7c478bd9Sstevel@tonic-gate } 487*7c478bd9Sstevel@tonic-gate 488*7c478bd9Sstevel@tonic-gate if (verbose) 489*7c478bd9Sstevel@tonic-gate Printf(" %d bytes to %s", cc, inet_ntoa(ip->ip_dst)); 490*7c478bd9Sstevel@tonic-gate } 491*7c478bd9Sstevel@tonic-gate 492*7c478bd9Sstevel@tonic-gate /* 493*7c478bd9Sstevel@tonic-gate * ICMP messages which doesn't mean we got the target, or we got a gateway, are 494*7c478bd9Sstevel@tonic-gate * processed here. It returns _B_TRUE if it's some sort of 'unreachable'. 495*7c478bd9Sstevel@tonic-gate */ 496*7c478bd9Sstevel@tonic-gate boolean_t 497*7c478bd9Sstevel@tonic-gate print_icmp_other(uchar_t type, uchar_t code) 498*7c478bd9Sstevel@tonic-gate { 499*7c478bd9Sstevel@tonic-gate boolean_t unreach = _B_FALSE; 500*7c478bd9Sstevel@tonic-gate 501*7c478bd9Sstevel@tonic-gate /* 502*7c478bd9Sstevel@tonic-gate * this function only prints '!*' for ICMP unreachable messages, 503*7c478bd9Sstevel@tonic-gate * ignores others. 504*7c478bd9Sstevel@tonic-gate */ 505*7c478bd9Sstevel@tonic-gate if (type != ICMP_UNREACH) { 506*7c478bd9Sstevel@tonic-gate return (_B_FALSE); 507*7c478bd9Sstevel@tonic-gate } 508*7c478bd9Sstevel@tonic-gate 509*7c478bd9Sstevel@tonic-gate switch (code) { 510*7c478bd9Sstevel@tonic-gate case ICMP_UNREACH_PORT: 511*7c478bd9Sstevel@tonic-gate break; 512*7c478bd9Sstevel@tonic-gate 513*7c478bd9Sstevel@tonic-gate case ICMP_UNREACH_NET_UNKNOWN: 514*7c478bd9Sstevel@tonic-gate case ICMP_UNREACH_NET: 515*7c478bd9Sstevel@tonic-gate unreach = _B_TRUE; 516*7c478bd9Sstevel@tonic-gate Printf(" !N"); 517*7c478bd9Sstevel@tonic-gate break; 518*7c478bd9Sstevel@tonic-gate 519*7c478bd9Sstevel@tonic-gate case ICMP_UNREACH_HOST_UNKNOWN: 520*7c478bd9Sstevel@tonic-gate case ICMP_UNREACH_HOST: 521*7c478bd9Sstevel@tonic-gate unreach = _B_TRUE; 522*7c478bd9Sstevel@tonic-gate Printf(" !H"); 523*7c478bd9Sstevel@tonic-gate break; 524*7c478bd9Sstevel@tonic-gate 525*7c478bd9Sstevel@tonic-gate case ICMP_UNREACH_PROTOCOL: 526*7c478bd9Sstevel@tonic-gate Printf(" !P"); 527*7c478bd9Sstevel@tonic-gate break; 528*7c478bd9Sstevel@tonic-gate 529*7c478bd9Sstevel@tonic-gate case ICMP_UNREACH_NEEDFRAG: 530*7c478bd9Sstevel@tonic-gate unreach = _B_TRUE; 531*7c478bd9Sstevel@tonic-gate Printf(" !F"); 532*7c478bd9Sstevel@tonic-gate break; 533*7c478bd9Sstevel@tonic-gate 534*7c478bd9Sstevel@tonic-gate case ICMP_UNREACH_SRCFAIL: 535*7c478bd9Sstevel@tonic-gate unreach = _B_TRUE; 536*7c478bd9Sstevel@tonic-gate Printf(" !S"); 537*7c478bd9Sstevel@tonic-gate break; 538*7c478bd9Sstevel@tonic-gate 539*7c478bd9Sstevel@tonic-gate case ICMP_UNREACH_FILTER_PROHIB: 540*7c478bd9Sstevel@tonic-gate case ICMP_UNREACH_NET_PROHIB: 541*7c478bd9Sstevel@tonic-gate case ICMP_UNREACH_HOST_PROHIB: 542*7c478bd9Sstevel@tonic-gate unreach = _B_TRUE; 543*7c478bd9Sstevel@tonic-gate Printf(" !X"); 544*7c478bd9Sstevel@tonic-gate break; 545*7c478bd9Sstevel@tonic-gate 546*7c478bd9Sstevel@tonic-gate case ICMP_UNREACH_TOSNET: 547*7c478bd9Sstevel@tonic-gate case ICMP_UNREACH_TOSHOST: 548*7c478bd9Sstevel@tonic-gate unreach = _B_TRUE; 549*7c478bd9Sstevel@tonic-gate Printf(" !T"); 550*7c478bd9Sstevel@tonic-gate break; 551*7c478bd9Sstevel@tonic-gate 552*7c478bd9Sstevel@tonic-gate case ICMP_UNREACH_ISOLATED: 553*7c478bd9Sstevel@tonic-gate case ICMP_UNREACH_HOST_PRECEDENCE: 554*7c478bd9Sstevel@tonic-gate case ICMP_UNREACH_PRECEDENCE_CUTOFF: 555*7c478bd9Sstevel@tonic-gate unreach = _B_TRUE; 556*7c478bd9Sstevel@tonic-gate Printf(" !U"); 557*7c478bd9Sstevel@tonic-gate break; 558*7c478bd9Sstevel@tonic-gate 559*7c478bd9Sstevel@tonic-gate default: 560*7c478bd9Sstevel@tonic-gate unreach = _B_TRUE; 561*7c478bd9Sstevel@tonic-gate Printf(" !<%d>", code); 562*7c478bd9Sstevel@tonic-gate break; 563*7c478bd9Sstevel@tonic-gate } 564*7c478bd9Sstevel@tonic-gate 565*7c478bd9Sstevel@tonic-gate return (unreach); 566*7c478bd9Sstevel@tonic-gate } 567