10Sstevel@tonic-gate /* 2*378Sblu * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 30Sstevel@tonic-gate * Use is subject to license terms. 40Sstevel@tonic-gate */ 50Sstevel@tonic-gate 60Sstevel@tonic-gate /* 70Sstevel@tonic-gate * Copyright (c) 1988, 1989, 1991, 1994, 1995, 1996, 1997 80Sstevel@tonic-gate * The Regents of the University of California. All rights reserved. 90Sstevel@tonic-gate * 100Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 110Sstevel@tonic-gate * modification, are permitted provided that: (1) source code distributions 120Sstevel@tonic-gate * retain the above copyright notice and this paragraph in its entirety, (2) 130Sstevel@tonic-gate * distributions including binary code include the above copyright notice and 140Sstevel@tonic-gate * this paragraph in its entirety in the documentation or other materials 150Sstevel@tonic-gate * provided with the distribution, and (3) all advertising materials mentioning 160Sstevel@tonic-gate * features or use of this software display the following acknowledgement: 170Sstevel@tonic-gate * ``This product includes software developed by the University of California, 180Sstevel@tonic-gate * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 190Sstevel@tonic-gate * the University nor the names of its contributors may be used to endorse 200Sstevel@tonic-gate * or promote products derived from this software without specific prior 210Sstevel@tonic-gate * written permission. 220Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 230Sstevel@tonic-gate * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 240Sstevel@tonic-gate * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 250Sstevel@tonic-gate * 260Sstevel@tonic-gate * 270Sstevel@tonic-gate * @(#)$Header: traceroute.c,v 1.49 97/06/13 02:30:23 leres Exp $ (LBL) 280Sstevel@tonic-gate */ 290Sstevel@tonic-gate 300Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 310Sstevel@tonic-gate 320Sstevel@tonic-gate #include <sys/socket.h> 330Sstevel@tonic-gate 340Sstevel@tonic-gate #include <stdio.h> 350Sstevel@tonic-gate #include <stdlib.h> 360Sstevel@tonic-gate #include <ctype.h> 370Sstevel@tonic-gate #include <strings.h> 380Sstevel@tonic-gate #include <libintl.h> 390Sstevel@tonic-gate #include <errno.h> 400Sstevel@tonic-gate 410Sstevel@tonic-gate #include <netinet/in_systm.h> 420Sstevel@tonic-gate #include <netinet/in.h> 430Sstevel@tonic-gate #include <netinet/ip.h> 440Sstevel@tonic-gate #include <netinet/ip_var.h> 450Sstevel@tonic-gate #include <netinet/ip_icmp.h> 460Sstevel@tonic-gate #include <netinet/udp.h> 470Sstevel@tonic-gate #include <netinet/udp_var.h> 480Sstevel@tonic-gate 490Sstevel@tonic-gate #include <arpa/inet.h> 500Sstevel@tonic-gate #include <netdb.h> 510Sstevel@tonic-gate 520Sstevel@tonic-gate #include <ifaddrlist.h> 530Sstevel@tonic-gate #include "traceroute.h" 540Sstevel@tonic-gate 550Sstevel@tonic-gate /* 560Sstevel@tonic-gate * IPv4 source routing option. 570Sstevel@tonic-gate * In order to avoid padding for the alignment of IPv4 addresses, ipsr_addrs 580Sstevel@tonic-gate * is defined as a 2-D array of uint8_t, instead of 1-D array of struct in_addr. 590Sstevel@tonic-gate */ 600Sstevel@tonic-gate struct ip_sourceroute { 610Sstevel@tonic-gate uint8_t ipsr_code; 620Sstevel@tonic-gate uint8_t ipsr_len; 630Sstevel@tonic-gate uint8_t ipsr_ptr; 640Sstevel@tonic-gate /* up to 9 IPv4 addresses */ 650Sstevel@tonic-gate uint8_t ipsr_addrs[1][sizeof (struct in_addr)]; 660Sstevel@tonic-gate }; 670Sstevel@tonic-gate 680Sstevel@tonic-gate int check_reply(struct msghdr *, int, int, uchar_t *, uchar_t *); 690Sstevel@tonic-gate extern ushort_t in_cksum(ushort_t *, int); 700Sstevel@tonic-gate extern char *inet_name(union any_in_addr *, int); 710Sstevel@tonic-gate static char *pr_type(uchar_t); 720Sstevel@tonic-gate void print_addr(uchar_t *, int, struct sockaddr *); 730Sstevel@tonic-gate boolean_t print_icmp_other(uchar_t, uchar_t); 740Sstevel@tonic-gate void send_probe(int, struct sockaddr *, struct ip *, int, int, 750Sstevel@tonic-gate struct timeval *, int); 760Sstevel@tonic-gate struct ip *set_buffers(int); 770Sstevel@tonic-gate void set_IPv4opt_sourcerouting(int, union any_in_addr *, union any_in_addr *); 780Sstevel@tonic-gate 790Sstevel@tonic-gate /* 800Sstevel@tonic-gate * prepares the buffer to be sent as an IP datagram 810Sstevel@tonic-gate */ 820Sstevel@tonic-gate struct ip * 830Sstevel@tonic-gate set_buffers(int plen) 840Sstevel@tonic-gate { 850Sstevel@tonic-gate struct ip *outip; 860Sstevel@tonic-gate uchar_t *outp; /* packet following the IP header (UDP/ICMP) */ 870Sstevel@tonic-gate struct udphdr *outudp; 880Sstevel@tonic-gate struct icmp *outicmp; 890Sstevel@tonic-gate int optlen = 0; 900Sstevel@tonic-gate 910Sstevel@tonic-gate outip = (struct ip *)malloc((size_t)plen); 920Sstevel@tonic-gate if (outip == NULL) { 930Sstevel@tonic-gate Fprintf(stderr, "%s: malloc: %s\n", prog, strerror(errno)); 940Sstevel@tonic-gate exit(EXIT_FAILURE); 950Sstevel@tonic-gate } 960Sstevel@tonic-gate 970Sstevel@tonic-gate if (gw_count > 0) { 980Sstevel@tonic-gate /* 8 = 5 (NO OPs) + 3 (code, len, ptr) */ 990Sstevel@tonic-gate optlen = 8 + gw_count * sizeof (struct in_addr); 1000Sstevel@tonic-gate } 1010Sstevel@tonic-gate 1020Sstevel@tonic-gate (void) memset((char *)outip, 0, (size_t)plen); 1030Sstevel@tonic-gate outp = (uchar_t *)(outip + 1); 1040Sstevel@tonic-gate 1050Sstevel@tonic-gate outip->ip_v = IPVERSION; 1060Sstevel@tonic-gate if (settos) 1070Sstevel@tonic-gate outip->ip_tos = tos; 1080Sstevel@tonic-gate 1090Sstevel@tonic-gate /* 1100Sstevel@tonic-gate * LBNL bug fixed: missing '- optlen' before, causing optlen 1110Sstevel@tonic-gate * added twice 1120Sstevel@tonic-gate * 1130Sstevel@tonic-gate * BSD bug: BSD touches the header fields 'len' and 'ip_off' 1140Sstevel@tonic-gate * even when HDRINCL is set. It applies htons() on these 1150Sstevel@tonic-gate * fields. It should send the header untouched when HDRINCL 1160Sstevel@tonic-gate * is set. 1170Sstevel@tonic-gate */ 1180Sstevel@tonic-gate outip->ip_len = htons(plen - optlen); 1190Sstevel@tonic-gate outip->ip_off = htons(off); 1200Sstevel@tonic-gate outip->ip_hl = (outp - (uchar_t *)outip) >> 2; 1210Sstevel@tonic-gate 1220Sstevel@tonic-gate /* setup ICMP or UDP */ 1230Sstevel@tonic-gate if (useicmp) { 1240Sstevel@tonic-gate outip->ip_p = IPPROTO_ICMP; 1250Sstevel@tonic-gate 1260Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 1270Sstevel@tonic-gate outicmp = (struct icmp *)outp; 1280Sstevel@tonic-gate outicmp->icmp_type = ICMP_ECHO; 1290Sstevel@tonic-gate outicmp->icmp_id = htons(ident); 1300Sstevel@tonic-gate } else { 1310Sstevel@tonic-gate outip->ip_p = IPPROTO_UDP; 1320Sstevel@tonic-gate 1330Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 1340Sstevel@tonic-gate outudp = (struct udphdr *)outp; 1350Sstevel@tonic-gate outudp->uh_sport = htons(ident); 1360Sstevel@tonic-gate outudp->uh_ulen = 1370Sstevel@tonic-gate htons((ushort_t)(plen - (sizeof (struct ip) + optlen))); 1380Sstevel@tonic-gate } 1390Sstevel@tonic-gate 1400Sstevel@tonic-gate return (outip); 1410Sstevel@tonic-gate } 1420Sstevel@tonic-gate 1430Sstevel@tonic-gate /* 1440Sstevel@tonic-gate * Setup the source routing for IPv4. 1450Sstevel@tonic-gate */ 1460Sstevel@tonic-gate void 1470Sstevel@tonic-gate set_IPv4opt_sourcerouting(int sndsock, union any_in_addr *ip_addr, 1480Sstevel@tonic-gate union any_in_addr *gwIPlist) 1490Sstevel@tonic-gate { 1500Sstevel@tonic-gate struct protoent *pe; 1510Sstevel@tonic-gate struct ip_sourceroute *srp; 1520Sstevel@tonic-gate uchar_t optlist[MAX_IPOPTLEN]; 1530Sstevel@tonic-gate int i; 154*378Sblu int gwV4_count; 1550Sstevel@tonic-gate 1560Sstevel@tonic-gate if ((pe = getprotobyname("ip")) == NULL) { 1570Sstevel@tonic-gate Fprintf(stderr, "%s: unknown protocol ip\n", prog); 1580Sstevel@tonic-gate exit(EXIT_FAILURE); 1590Sstevel@tonic-gate } 1600Sstevel@tonic-gate 161*378Sblu gwV4_count = (gw_count < MAX_GWS) ? gw_count : MAX_GWS - 1; 1620Sstevel@tonic-gate /* final hop */ 163*378Sblu gwIPlist[gwV4_count].addr = ip_addr->addr; 1640Sstevel@tonic-gate 1650Sstevel@tonic-gate /* 1660Sstevel@tonic-gate * the option length passed to setsockopt() needs to be a multiple of 1670Sstevel@tonic-gate * 32 bits. Therefore we need to use a 1-byte padding (source routing 1680Sstevel@tonic-gate * information takes 4x+3 bytes). 1690Sstevel@tonic-gate */ 1700Sstevel@tonic-gate optlist[0] = IPOPT_NOP; 1710Sstevel@tonic-gate 1720Sstevel@tonic-gate srp = (struct ip_sourceroute *)&optlist[1]; 1730Sstevel@tonic-gate srp->ipsr_code = IPOPT_LSRR; 1740Sstevel@tonic-gate /* 3 = 1 (code) + 1 (len) + 1 (ptr) */ 175*378Sblu srp->ipsr_len = 3 + (gwV4_count + 1) * sizeof (gwIPlist[0].addr); 1760Sstevel@tonic-gate srp->ipsr_ptr = IPOPT_MINOFF; 1770Sstevel@tonic-gate 178*378Sblu for (i = 0; i <= gwV4_count; i++) { 1790Sstevel@tonic-gate (void) bcopy((char *)&gwIPlist[i].addr, &srp->ipsr_addrs[i], 1800Sstevel@tonic-gate sizeof (struct in_addr)); 1810Sstevel@tonic-gate } 1820Sstevel@tonic-gate 1830Sstevel@tonic-gate if (setsockopt(sndsock, pe->p_proto, IP_OPTIONS, (const char *)optlist, 1840Sstevel@tonic-gate srp->ipsr_len + 1) < 0) { 1850Sstevel@tonic-gate Fprintf(stderr, "%s: IP_OPTIONS: %s\n", prog, strerror(errno)); 1860Sstevel@tonic-gate exit(EXIT_FAILURE); 1870Sstevel@tonic-gate } 1880Sstevel@tonic-gate } 1890Sstevel@tonic-gate 1900Sstevel@tonic-gate /* 1910Sstevel@tonic-gate * send a probe packet to the destination 1920Sstevel@tonic-gate */ 1930Sstevel@tonic-gate void 1940Sstevel@tonic-gate send_probe(int sndsock, struct sockaddr *to, struct ip *outip, 1950Sstevel@tonic-gate int seq, int ttl, struct timeval *tp, int packlen) 1960Sstevel@tonic-gate { 1970Sstevel@tonic-gate int cc; 1980Sstevel@tonic-gate struct udpiphdr *ui; 1990Sstevel@tonic-gate uchar_t *outp; /* packet following the IP header (UDP/ICMP) */ 2000Sstevel@tonic-gate struct udphdr *outudp; 2010Sstevel@tonic-gate struct icmp *outicmp; 2020Sstevel@tonic-gate struct outdata *outdata; 2030Sstevel@tonic-gate struct ip tip; 2040Sstevel@tonic-gate int optlen = 0; 2050Sstevel@tonic-gate int send_size; 2060Sstevel@tonic-gate 2070Sstevel@tonic-gate /* initialize buffer pointers */ 2080Sstevel@tonic-gate outp = (uchar_t *)(outip + 1); 2090Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 2100Sstevel@tonic-gate outudp = (struct udphdr *)outp; 2110Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 2120Sstevel@tonic-gate outicmp = (struct icmp *)outp; 2130Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 2140Sstevel@tonic-gate outdata = (struct outdata *)(outp + ICMP_MINLEN); 2150Sstevel@tonic-gate 2160Sstevel@tonic-gate if (gw_count > 0) { 2170Sstevel@tonic-gate /* 8 = 5 (NO OPs) + 3 (code, len, ptr) */ 2180Sstevel@tonic-gate optlen = 8 + gw_count * sizeof (struct in_addr); 2190Sstevel@tonic-gate } 2200Sstevel@tonic-gate 2210Sstevel@tonic-gate if (raw_req) { 2220Sstevel@tonic-gate send_size = packlen - optlen; 2230Sstevel@tonic-gate } else if (useicmp) { 2240Sstevel@tonic-gate send_size = packlen - optlen - sizeof (struct ip); 2250Sstevel@tonic-gate } else { 2260Sstevel@tonic-gate send_size = packlen - optlen - sizeof (struct ip) - 2270Sstevel@tonic-gate sizeof (struct udphdr); 2280Sstevel@tonic-gate } 2290Sstevel@tonic-gate 2300Sstevel@tonic-gate outip->ip_ttl = ttl; 2310Sstevel@tonic-gate outip->ip_id = htons(ident + seq); 2320Sstevel@tonic-gate 2330Sstevel@tonic-gate /* 2340Sstevel@tonic-gate * If a raw IPv4 packet is going to be sent, the Time to Live 2350Sstevel@tonic-gate * field in the packet was initialized above. Otherwise, it is 2360Sstevel@tonic-gate * initialized here using the IPPROTO_IP level socket option. 2370Sstevel@tonic-gate */ 2380Sstevel@tonic-gate if (!raw_req) { 2390Sstevel@tonic-gate if (setsockopt(sndsock, IPPROTO_IP, IP_TTL, (char *)&ttl, 2400Sstevel@tonic-gate sizeof (ttl)) < 0) { 2410Sstevel@tonic-gate Fprintf(stderr, "%s: IP_TTL: %s\n", prog, 2420Sstevel@tonic-gate strerror(errno)); 2430Sstevel@tonic-gate exit(EXIT_FAILURE); 2440Sstevel@tonic-gate } 2450Sstevel@tonic-gate } 2460Sstevel@tonic-gate 2470Sstevel@tonic-gate /* 2480Sstevel@tonic-gate * In most cases, the kernel will recalculate the ip checksum. 2490Sstevel@tonic-gate * But we must do it anyway so that the udp checksum comes out 2500Sstevel@tonic-gate * right. 2510Sstevel@tonic-gate */ 2520Sstevel@tonic-gate if (docksum) { 2530Sstevel@tonic-gate outip->ip_sum = 2540Sstevel@tonic-gate in_cksum((ushort_t *)outip, sizeof (*outip) + optlen); 2550Sstevel@tonic-gate if (outip->ip_sum == 0) 2560Sstevel@tonic-gate outip->ip_sum = 0xffff; 2570Sstevel@tonic-gate } 2580Sstevel@tonic-gate 2590Sstevel@tonic-gate /* Payload */ 2600Sstevel@tonic-gate outdata->seq = seq; 2610Sstevel@tonic-gate outdata->ttl = ttl; 2620Sstevel@tonic-gate outdata->tv = *tp; 2630Sstevel@tonic-gate 2640Sstevel@tonic-gate if (useicmp) { 2650Sstevel@tonic-gate outicmp->icmp_seq = htons(seq); 2660Sstevel@tonic-gate } else { 2670Sstevel@tonic-gate outudp->uh_dport = htons((port + seq) % (MAX_PORT + 1)); 2680Sstevel@tonic-gate } 2690Sstevel@tonic-gate 2700Sstevel@tonic-gate if (!raw_req) 2710Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 2720Sstevel@tonic-gate ((struct sockaddr_in *)to)->sin_port = outudp->uh_dport; 2730Sstevel@tonic-gate 2740Sstevel@tonic-gate /* (We can only do the checksum if we know our ip address) */ 2750Sstevel@tonic-gate if (docksum) { 2760Sstevel@tonic-gate if (useicmp) { 2770Sstevel@tonic-gate outicmp->icmp_cksum = 0; 2780Sstevel@tonic-gate outicmp->icmp_cksum = in_cksum((ushort_t *)outicmp, 2790Sstevel@tonic-gate packlen - (sizeof (struct ip) + optlen)); 2800Sstevel@tonic-gate if (outicmp->icmp_cksum == 0) 2810Sstevel@tonic-gate outicmp->icmp_cksum = 0xffff; 2820Sstevel@tonic-gate } else { 2830Sstevel@tonic-gate /* Checksum (must save and restore ip header) */ 2840Sstevel@tonic-gate tip = *outip; 2850Sstevel@tonic-gate ui = (struct udpiphdr *)outip; 2860Sstevel@tonic-gate ui->ui_next = 0; 2870Sstevel@tonic-gate ui->ui_prev = 0; 2880Sstevel@tonic-gate ui->ui_x1 = 0; 2890Sstevel@tonic-gate ui->ui_len = outudp->uh_ulen; 2900Sstevel@tonic-gate outudp->uh_sum = 0; 2910Sstevel@tonic-gate outudp->uh_sum = in_cksum((ushort_t *)ui, packlen); 2920Sstevel@tonic-gate if (outudp->uh_sum == 0) 2930Sstevel@tonic-gate outudp->uh_sum = 0xffff; 2940Sstevel@tonic-gate *outip = tip; 2950Sstevel@tonic-gate } 2960Sstevel@tonic-gate } 2970Sstevel@tonic-gate 2980Sstevel@tonic-gate if (raw_req) { 2990Sstevel@tonic-gate cc = sendto(sndsock, (char *)outip, send_size, 0, to, 3000Sstevel@tonic-gate sizeof (struct sockaddr_in)); 3010Sstevel@tonic-gate } else if (useicmp) { 3020Sstevel@tonic-gate cc = sendto(sndsock, (char *)outicmp, send_size, 0, to, 3030Sstevel@tonic-gate sizeof (struct sockaddr_in)); 3040Sstevel@tonic-gate } else { 3050Sstevel@tonic-gate cc = sendto(sndsock, (char *)outp, send_size, 0, to, 3060Sstevel@tonic-gate sizeof (struct sockaddr_in)); 3070Sstevel@tonic-gate } 3080Sstevel@tonic-gate 3090Sstevel@tonic-gate if (cc < 0 || cc != send_size) { 3100Sstevel@tonic-gate if (cc < 0) { 3110Sstevel@tonic-gate Fprintf(stderr, "%s: sendto: %s\n", prog, 3120Sstevel@tonic-gate strerror(errno)); 3130Sstevel@tonic-gate } 3140Sstevel@tonic-gate Printf("%s: wrote %s %d chars, ret=%d\n", 3150Sstevel@tonic-gate prog, hostname, send_size, cc); 3160Sstevel@tonic-gate (void) fflush(stdout); 3170Sstevel@tonic-gate } 3180Sstevel@tonic-gate } 3190Sstevel@tonic-gate 3200Sstevel@tonic-gate /* 3210Sstevel@tonic-gate * Check out the reply packet to see if it's what we were expecting. 3220Sstevel@tonic-gate * Returns REPLY_GOT_TARGET if the reply comes from the target 3230Sstevel@tonic-gate * REPLY_GOT_GATEWAY if an intermediate gateway sends TIME_EXCEEDED 3240Sstevel@tonic-gate * REPLY_GOT_OTHER for other kinds of unreachables indicating none of 3250Sstevel@tonic-gate * the above two cases 3260Sstevel@tonic-gate * 3270Sstevel@tonic-gate * It also sets the icmp type and icmp code values 3280Sstevel@tonic-gate */ 3290Sstevel@tonic-gate int 3300Sstevel@tonic-gate check_reply(struct msghdr *msg, int cc, int seq, uchar_t *type, uchar_t *code) 3310Sstevel@tonic-gate { 3320Sstevel@tonic-gate uchar_t *buf = msg->msg_iov->iov_base; 3330Sstevel@tonic-gate struct sockaddr_in *from_in = (struct sockaddr_in *)msg->msg_name; 3340Sstevel@tonic-gate struct icmp *icp; 3350Sstevel@tonic-gate int hlen; 3360Sstevel@tonic-gate int save_cc = cc; 3370Sstevel@tonic-gate struct ip *ip; 3380Sstevel@tonic-gate 3390Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 3400Sstevel@tonic-gate ip = (struct ip *)buf; 3410Sstevel@tonic-gate hlen = ip->ip_hl << 2; 3420Sstevel@tonic-gate if (cc < hlen + ICMP_MINLEN) { 3430Sstevel@tonic-gate if (verbose) { 3440Sstevel@tonic-gate Printf("packet too short (%d bytes) from %s\n", 3450Sstevel@tonic-gate cc, inet_ntoa(from_in->sin_addr)); 3460Sstevel@tonic-gate } 3470Sstevel@tonic-gate return (REPLY_SHORT_PKT); 3480Sstevel@tonic-gate } 3490Sstevel@tonic-gate cc -= hlen; 3500Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 3510Sstevel@tonic-gate icp = (struct icmp *)(buf + hlen); 3520Sstevel@tonic-gate 3530Sstevel@tonic-gate *type = icp->icmp_type; 3540Sstevel@tonic-gate *code = icp->icmp_code; 3550Sstevel@tonic-gate 3560Sstevel@tonic-gate /* 3570Sstevel@tonic-gate * traceroute interpretes only ICMP_TIMXCEED_INTRANS, ICMP_UNREACH and 3580Sstevel@tonic-gate * ICMP_ECHOREPLY, ignores others 3590Sstevel@tonic-gate */ 3600Sstevel@tonic-gate if ((*type == ICMP_TIMXCEED && *code == ICMP_TIMXCEED_INTRANS) || 3610Sstevel@tonic-gate *type == ICMP_UNREACH || *type == ICMP_ECHOREPLY) { 3620Sstevel@tonic-gate struct ip *hip; 3630Sstevel@tonic-gate struct udphdr *up; 3640Sstevel@tonic-gate struct icmp *hicmp; 3650Sstevel@tonic-gate 3660Sstevel@tonic-gate cc -= ICMP_MINLEN; 3670Sstevel@tonic-gate hip = &icp->icmp_ip; 3680Sstevel@tonic-gate hlen = hip->ip_hl << 2; 3690Sstevel@tonic-gate cc -= hlen; 3700Sstevel@tonic-gate if (useicmp) { 3710Sstevel@tonic-gate if (*type == ICMP_ECHOREPLY && 3720Sstevel@tonic-gate icp->icmp_id == htons(ident) && 3730Sstevel@tonic-gate icp->icmp_seq == htons(seq)) 3740Sstevel@tonic-gate return (REPLY_GOT_TARGET); 3750Sstevel@tonic-gate 3760Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 3770Sstevel@tonic-gate hicmp = (struct icmp *)((uchar_t *)hip + hlen); 3780Sstevel@tonic-gate 3790Sstevel@tonic-gate if (ICMP_MINLEN <= cc && 3800Sstevel@tonic-gate hip->ip_p == IPPROTO_ICMP && 3810Sstevel@tonic-gate hicmp->icmp_id == htons(ident) && 3820Sstevel@tonic-gate hicmp->icmp_seq == htons(seq)) { 3830Sstevel@tonic-gate return ((*type == ICMP_TIMXCEED) ? 3840Sstevel@tonic-gate REPLY_GOT_GATEWAY : REPLY_GOT_OTHER); 3850Sstevel@tonic-gate } 3860Sstevel@tonic-gate } else { 3870Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 3880Sstevel@tonic-gate up = (struct udphdr *)((uchar_t *)hip + hlen); 3890Sstevel@tonic-gate /* 3900Sstevel@tonic-gate * at least 4 bytes of UDP header is required for this 3910Sstevel@tonic-gate * check 3920Sstevel@tonic-gate */ 3930Sstevel@tonic-gate if (4 <= cc && 3940Sstevel@tonic-gate hip->ip_p == IPPROTO_UDP && 3950Sstevel@tonic-gate up->uh_sport == htons(ident) && 3960Sstevel@tonic-gate up->uh_dport == htons((port + seq) % 3970Sstevel@tonic-gate (MAX_PORT + 1))) { 3980Sstevel@tonic-gate if (*type == ICMP_UNREACH && 3990Sstevel@tonic-gate *code == ICMP_UNREACH_PORT) { 4000Sstevel@tonic-gate return (REPLY_GOT_TARGET); 4010Sstevel@tonic-gate } else if (*type == ICMP_TIMXCEED) { 4020Sstevel@tonic-gate return (REPLY_GOT_GATEWAY); 4030Sstevel@tonic-gate } else { 4040Sstevel@tonic-gate return (REPLY_GOT_OTHER); 4050Sstevel@tonic-gate } 4060Sstevel@tonic-gate } 4070Sstevel@tonic-gate } 4080Sstevel@tonic-gate } 4090Sstevel@tonic-gate 4100Sstevel@tonic-gate if (verbose) { 4110Sstevel@tonic-gate int i, j; 4120Sstevel@tonic-gate uchar_t *lp = (uchar_t *)ip; 4130Sstevel@tonic-gate 4140Sstevel@tonic-gate cc = save_cc; 4150Sstevel@tonic-gate Printf("\n%d bytes from %s to ", cc, 4160Sstevel@tonic-gate inet_ntoa(from_in->sin_addr)); 4170Sstevel@tonic-gate Printf("%s: icmp type %d (%s) code %d\n", 4180Sstevel@tonic-gate inet_ntoa(ip->ip_dst), *type, pr_type(*type), *code); 4190Sstevel@tonic-gate for (i = 0; i < cc; i += 4) { 4200Sstevel@tonic-gate Printf("%2d: x", i); 4210Sstevel@tonic-gate for (j = 0; ((j < 4) && ((i + j) < cc)); j++) 4220Sstevel@tonic-gate Printf("%2.2x", *lp++); 4230Sstevel@tonic-gate (void) putchar('\n'); 4240Sstevel@tonic-gate } 4250Sstevel@tonic-gate } 4260Sstevel@tonic-gate 4270Sstevel@tonic-gate return (REPLY_SHORT_PKT); 4280Sstevel@tonic-gate } 4290Sstevel@tonic-gate 4300Sstevel@tonic-gate /* 4310Sstevel@tonic-gate * convert an ICMP "type" field to a printable string. 4320Sstevel@tonic-gate */ 4330Sstevel@tonic-gate static char * 4340Sstevel@tonic-gate pr_type(uchar_t type) 4350Sstevel@tonic-gate { 4360Sstevel@tonic-gate static struct icmptype_table ttab[] = { 4370Sstevel@tonic-gate {ICMP_ECHOREPLY, "Echo Reply"}, 4380Sstevel@tonic-gate {1, "ICMP 1"}, 4390Sstevel@tonic-gate {2, "ICMP 2"}, 4400Sstevel@tonic-gate {ICMP_UNREACH, "Dest Unreachable"}, 4410Sstevel@tonic-gate {ICMP_SOURCEQUENCH, "Source Quench"}, 4420Sstevel@tonic-gate {ICMP_REDIRECT, "Redirect"}, 4430Sstevel@tonic-gate {6, "ICMP 6"}, 4440Sstevel@tonic-gate {7, "ICMP 7"}, 4450Sstevel@tonic-gate {ICMP_ECHO, "Echo"}, 4460Sstevel@tonic-gate {ICMP_ROUTERADVERT, "Router Advertisement"}, 4470Sstevel@tonic-gate {ICMP_ROUTERSOLICIT, "Router Solicitation"}, 4480Sstevel@tonic-gate {ICMP_TIMXCEED, "Time Exceeded"}, 4490Sstevel@tonic-gate {ICMP_PARAMPROB, "Param Problem"}, 4500Sstevel@tonic-gate {ICMP_TSTAMP, "Timestamp"}, 4510Sstevel@tonic-gate {ICMP_TSTAMPREPLY, "Timestamp Reply"}, 4520Sstevel@tonic-gate {ICMP_IREQ, "Info Request"}, 4530Sstevel@tonic-gate {ICMP_IREQREPLY, "Info Reply"}, 4540Sstevel@tonic-gate {ICMP_MASKREQ, "Netmask Request"}, 4550Sstevel@tonic-gate {ICMP_MASKREPLY, "Netmask Reply"} 4560Sstevel@tonic-gate }; 4570Sstevel@tonic-gate int i = 0; 4580Sstevel@tonic-gate 4590Sstevel@tonic-gate for (i = 0; i < A_CNT(ttab); i++) { 4600Sstevel@tonic-gate if (ttab[i].type == type) 4610Sstevel@tonic-gate return (ttab[i].message); 4620Sstevel@tonic-gate } 4630Sstevel@tonic-gate 4640Sstevel@tonic-gate return ("OUT-OF-RANGE"); 4650Sstevel@tonic-gate } 4660Sstevel@tonic-gate 4670Sstevel@tonic-gate /* 4680Sstevel@tonic-gate * print the IPv4 src address of the reply packet 4690Sstevel@tonic-gate */ 4700Sstevel@tonic-gate void 4710Sstevel@tonic-gate print_addr(uchar_t *buf, int cc, struct sockaddr *from) 4720Sstevel@tonic-gate { 4730Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 4740Sstevel@tonic-gate struct sockaddr_in *from_in = (struct sockaddr_in *)from; 4750Sstevel@tonic-gate struct ip *ip; 4760Sstevel@tonic-gate union any_in_addr ip_addr; 4770Sstevel@tonic-gate 4780Sstevel@tonic-gate ip_addr.addr = from_in->sin_addr; 4790Sstevel@tonic-gate 4800Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 4810Sstevel@tonic-gate ip = (struct ip *)buf; 4820Sstevel@tonic-gate 4830Sstevel@tonic-gate if (nflag) { 4840Sstevel@tonic-gate Printf(" %s", inet_ntoa(from_in->sin_addr)); 4850Sstevel@tonic-gate } else { 4860Sstevel@tonic-gate Printf(" %s (%s)", inet_name(&ip_addr, AF_INET), 4870Sstevel@tonic-gate inet_ntoa(from_in->sin_addr)); 4880Sstevel@tonic-gate } 4890Sstevel@tonic-gate 4900Sstevel@tonic-gate if (verbose) 4910Sstevel@tonic-gate Printf(" %d bytes to %s", cc, inet_ntoa(ip->ip_dst)); 4920Sstevel@tonic-gate } 4930Sstevel@tonic-gate 4940Sstevel@tonic-gate /* 4950Sstevel@tonic-gate * ICMP messages which doesn't mean we got the target, or we got a gateway, are 4960Sstevel@tonic-gate * processed here. It returns _B_TRUE if it's some sort of 'unreachable'. 4970Sstevel@tonic-gate */ 4980Sstevel@tonic-gate boolean_t 4990Sstevel@tonic-gate print_icmp_other(uchar_t type, uchar_t code) 5000Sstevel@tonic-gate { 5010Sstevel@tonic-gate boolean_t unreach = _B_FALSE; 5020Sstevel@tonic-gate 5030Sstevel@tonic-gate /* 5040Sstevel@tonic-gate * this function only prints '!*' for ICMP unreachable messages, 5050Sstevel@tonic-gate * ignores others. 5060Sstevel@tonic-gate */ 5070Sstevel@tonic-gate if (type != ICMP_UNREACH) { 5080Sstevel@tonic-gate return (_B_FALSE); 5090Sstevel@tonic-gate } 5100Sstevel@tonic-gate 5110Sstevel@tonic-gate switch (code) { 5120Sstevel@tonic-gate case ICMP_UNREACH_PORT: 5130Sstevel@tonic-gate break; 5140Sstevel@tonic-gate 5150Sstevel@tonic-gate case ICMP_UNREACH_NET_UNKNOWN: 5160Sstevel@tonic-gate case ICMP_UNREACH_NET: 5170Sstevel@tonic-gate unreach = _B_TRUE; 5180Sstevel@tonic-gate Printf(" !N"); 5190Sstevel@tonic-gate break; 5200Sstevel@tonic-gate 5210Sstevel@tonic-gate case ICMP_UNREACH_HOST_UNKNOWN: 5220Sstevel@tonic-gate case ICMP_UNREACH_HOST: 5230Sstevel@tonic-gate unreach = _B_TRUE; 5240Sstevel@tonic-gate Printf(" !H"); 5250Sstevel@tonic-gate break; 5260Sstevel@tonic-gate 5270Sstevel@tonic-gate case ICMP_UNREACH_PROTOCOL: 5280Sstevel@tonic-gate Printf(" !P"); 5290Sstevel@tonic-gate break; 5300Sstevel@tonic-gate 5310Sstevel@tonic-gate case ICMP_UNREACH_NEEDFRAG: 5320Sstevel@tonic-gate unreach = _B_TRUE; 5330Sstevel@tonic-gate Printf(" !F"); 5340Sstevel@tonic-gate break; 5350Sstevel@tonic-gate 5360Sstevel@tonic-gate case ICMP_UNREACH_SRCFAIL: 5370Sstevel@tonic-gate unreach = _B_TRUE; 5380Sstevel@tonic-gate Printf(" !S"); 5390Sstevel@tonic-gate break; 5400Sstevel@tonic-gate 5410Sstevel@tonic-gate case ICMP_UNREACH_FILTER_PROHIB: 5420Sstevel@tonic-gate case ICMP_UNREACH_NET_PROHIB: 5430Sstevel@tonic-gate case ICMP_UNREACH_HOST_PROHIB: 5440Sstevel@tonic-gate unreach = _B_TRUE; 5450Sstevel@tonic-gate Printf(" !X"); 5460Sstevel@tonic-gate break; 5470Sstevel@tonic-gate 5480Sstevel@tonic-gate case ICMP_UNREACH_TOSNET: 5490Sstevel@tonic-gate case ICMP_UNREACH_TOSHOST: 5500Sstevel@tonic-gate unreach = _B_TRUE; 5510Sstevel@tonic-gate Printf(" !T"); 5520Sstevel@tonic-gate break; 5530Sstevel@tonic-gate 5540Sstevel@tonic-gate case ICMP_UNREACH_ISOLATED: 5550Sstevel@tonic-gate case ICMP_UNREACH_HOST_PRECEDENCE: 5560Sstevel@tonic-gate case ICMP_UNREACH_PRECEDENCE_CUTOFF: 5570Sstevel@tonic-gate unreach = _B_TRUE; 5580Sstevel@tonic-gate Printf(" !U"); 5590Sstevel@tonic-gate break; 5600Sstevel@tonic-gate 5610Sstevel@tonic-gate default: 5620Sstevel@tonic-gate unreach = _B_TRUE; 5630Sstevel@tonic-gate Printf(" !<%d>", code); 5640Sstevel@tonic-gate break; 5650Sstevel@tonic-gate } 5660Sstevel@tonic-gate 5670Sstevel@tonic-gate return (unreach); 5680Sstevel@tonic-gate } 569