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