17c478bd9Sstevel@tonic-gate /*
2*0406ceaaSmeem * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
37c478bd9Sstevel@tonic-gate * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate */
57c478bd9Sstevel@tonic-gate
67c478bd9Sstevel@tonic-gate /*
77c478bd9Sstevel@tonic-gate * Copyright (c) 1988, 1989, 1991, 1994, 1995, 1996, 1997
87c478bd9Sstevel@tonic-gate * The Regents of the University of California. All rights reserved.
97c478bd9Sstevel@tonic-gate *
107c478bd9Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
117c478bd9Sstevel@tonic-gate * modification, are permitted provided that: (1) source code distributions
127c478bd9Sstevel@tonic-gate * retain the above copyright notice and this paragraph in its entirety, (2)
137c478bd9Sstevel@tonic-gate * distributions including binary code include the above copyright notice and
147c478bd9Sstevel@tonic-gate * this paragraph in its entirety in the documentation or other materials
157c478bd9Sstevel@tonic-gate * provided with the distribution, and (3) all advertising materials mentioning
167c478bd9Sstevel@tonic-gate * features or use of this software display the following acknowledgement:
177c478bd9Sstevel@tonic-gate * ``This product includes software developed by the University of California,
187c478bd9Sstevel@tonic-gate * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
197c478bd9Sstevel@tonic-gate * the University nor the names of its contributors may be used to endorse
207c478bd9Sstevel@tonic-gate * or promote products derived from this software without specific prior
217c478bd9Sstevel@tonic-gate * written permission.
227c478bd9Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
237c478bd9Sstevel@tonic-gate * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
247c478bd9Sstevel@tonic-gate * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
257c478bd9Sstevel@tonic-gate *
267c478bd9Sstevel@tonic-gate *
277c478bd9Sstevel@tonic-gate * @(#)$Header: traceroute.c,v 1.49 97/06/13 02:30:23 leres Exp $ (LBL)
287c478bd9Sstevel@tonic-gate */
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
317c478bd9Sstevel@tonic-gate
327c478bd9Sstevel@tonic-gate #include <sys/socket.h>
337c478bd9Sstevel@tonic-gate
347c478bd9Sstevel@tonic-gate #include <stdio.h>
357c478bd9Sstevel@tonic-gate #include <stdlib.h>
367c478bd9Sstevel@tonic-gate #include <ctype.h>
377c478bd9Sstevel@tonic-gate #include <strings.h>
387c478bd9Sstevel@tonic-gate #include <libintl.h>
397c478bd9Sstevel@tonic-gate #include <errno.h>
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gate #include <netinet/in_systm.h>
427c478bd9Sstevel@tonic-gate #include <netinet/in.h>
437c478bd9Sstevel@tonic-gate #include <netinet/ip.h>
447c478bd9Sstevel@tonic-gate #include <netinet/ip_var.h>
457c478bd9Sstevel@tonic-gate #include <netinet/ip_icmp.h>
467c478bd9Sstevel@tonic-gate #include <netinet/udp.h>
477c478bd9Sstevel@tonic-gate #include <netinet/udp_var.h>
487c478bd9Sstevel@tonic-gate
497c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
507c478bd9Sstevel@tonic-gate #include <netdb.h>
517c478bd9Sstevel@tonic-gate
52*0406ceaaSmeem #include <libinetutil.h>
537c478bd9Sstevel@tonic-gate #include "traceroute.h"
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate /*
567c478bd9Sstevel@tonic-gate * IPv4 source routing option.
577c478bd9Sstevel@tonic-gate * In order to avoid padding for the alignment of IPv4 addresses, ipsr_addrs
587c478bd9Sstevel@tonic-gate * is defined as a 2-D array of uint8_t, instead of 1-D array of struct in_addr.
597c478bd9Sstevel@tonic-gate */
607c478bd9Sstevel@tonic-gate struct ip_sourceroute {
617c478bd9Sstevel@tonic-gate uint8_t ipsr_code;
627c478bd9Sstevel@tonic-gate uint8_t ipsr_len;
637c478bd9Sstevel@tonic-gate uint8_t ipsr_ptr;
647c478bd9Sstevel@tonic-gate /* up to 9 IPv4 addresses */
657c478bd9Sstevel@tonic-gate uint8_t ipsr_addrs[1][sizeof (struct in_addr)];
667c478bd9Sstevel@tonic-gate };
677c478bd9Sstevel@tonic-gate
687c478bd9Sstevel@tonic-gate int check_reply(struct msghdr *, int, int, uchar_t *, uchar_t *);
697c478bd9Sstevel@tonic-gate extern ushort_t in_cksum(ushort_t *, int);
707c478bd9Sstevel@tonic-gate extern char *inet_name(union any_in_addr *, int);
717c478bd9Sstevel@tonic-gate static char *pr_type(uchar_t);
727c478bd9Sstevel@tonic-gate void print_addr(uchar_t *, int, struct sockaddr *);
737c478bd9Sstevel@tonic-gate boolean_t print_icmp_other(uchar_t, uchar_t);
747c478bd9Sstevel@tonic-gate void send_probe(int, struct sockaddr *, struct ip *, int, int,
757c478bd9Sstevel@tonic-gate struct timeval *, int);
767c478bd9Sstevel@tonic-gate struct ip *set_buffers(int);
777c478bd9Sstevel@tonic-gate void set_IPv4opt_sourcerouting(int, union any_in_addr *, union any_in_addr *);
787c478bd9Sstevel@tonic-gate
797c478bd9Sstevel@tonic-gate /*
807c478bd9Sstevel@tonic-gate * prepares the buffer to be sent as an IP datagram
817c478bd9Sstevel@tonic-gate */
827c478bd9Sstevel@tonic-gate struct ip *
set_buffers(int plen)837c478bd9Sstevel@tonic-gate set_buffers(int plen)
847c478bd9Sstevel@tonic-gate {
857c478bd9Sstevel@tonic-gate struct ip *outip;
867c478bd9Sstevel@tonic-gate uchar_t *outp; /* packet following the IP header (UDP/ICMP) */
877c478bd9Sstevel@tonic-gate struct udphdr *outudp;
887c478bd9Sstevel@tonic-gate struct icmp *outicmp;
897c478bd9Sstevel@tonic-gate int optlen = 0;
907c478bd9Sstevel@tonic-gate
917c478bd9Sstevel@tonic-gate outip = (struct ip *)malloc((size_t)plen);
927c478bd9Sstevel@tonic-gate if (outip == NULL) {
937c478bd9Sstevel@tonic-gate Fprintf(stderr, "%s: malloc: %s\n", prog, strerror(errno));
947c478bd9Sstevel@tonic-gate exit(EXIT_FAILURE);
957c478bd9Sstevel@tonic-gate }
967c478bd9Sstevel@tonic-gate
977c478bd9Sstevel@tonic-gate if (gw_count > 0) {
987c478bd9Sstevel@tonic-gate /* 8 = 5 (NO OPs) + 3 (code, len, ptr) */
997c478bd9Sstevel@tonic-gate optlen = 8 + gw_count * sizeof (struct in_addr);
1007c478bd9Sstevel@tonic-gate }
1017c478bd9Sstevel@tonic-gate
1027c478bd9Sstevel@tonic-gate (void) memset((char *)outip, 0, (size_t)plen);
1037c478bd9Sstevel@tonic-gate outp = (uchar_t *)(outip + 1);
1047c478bd9Sstevel@tonic-gate
1057c478bd9Sstevel@tonic-gate outip->ip_v = IPVERSION;
1067c478bd9Sstevel@tonic-gate if (settos)
1077c478bd9Sstevel@tonic-gate outip->ip_tos = tos;
1087c478bd9Sstevel@tonic-gate
1097c478bd9Sstevel@tonic-gate /*
1107c478bd9Sstevel@tonic-gate * LBNL bug fixed: missing '- optlen' before, causing optlen
1117c478bd9Sstevel@tonic-gate * added twice
1127c478bd9Sstevel@tonic-gate *
1137c478bd9Sstevel@tonic-gate * BSD bug: BSD touches the header fields 'len' and 'ip_off'
1147c478bd9Sstevel@tonic-gate * even when HDRINCL is set. It applies htons() on these
1157c478bd9Sstevel@tonic-gate * fields. It should send the header untouched when HDRINCL
1167c478bd9Sstevel@tonic-gate * is set.
1177c478bd9Sstevel@tonic-gate */
1187c478bd9Sstevel@tonic-gate outip->ip_len = htons(plen - optlen);
1197c478bd9Sstevel@tonic-gate outip->ip_off = htons(off);
1207c478bd9Sstevel@tonic-gate outip->ip_hl = (outp - (uchar_t *)outip) >> 2;
1217c478bd9Sstevel@tonic-gate
1227c478bd9Sstevel@tonic-gate /* setup ICMP or UDP */
1237c478bd9Sstevel@tonic-gate if (useicmp) {
1247c478bd9Sstevel@tonic-gate outip->ip_p = IPPROTO_ICMP;
1257c478bd9Sstevel@tonic-gate
1267c478bd9Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */
1277c478bd9Sstevel@tonic-gate outicmp = (struct icmp *)outp;
1287c478bd9Sstevel@tonic-gate outicmp->icmp_type = ICMP_ECHO;
1297c478bd9Sstevel@tonic-gate outicmp->icmp_id = htons(ident);
1307c478bd9Sstevel@tonic-gate } else {
1317c478bd9Sstevel@tonic-gate outip->ip_p = IPPROTO_UDP;
1327c478bd9Sstevel@tonic-gate
1337c478bd9Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */
1347c478bd9Sstevel@tonic-gate outudp = (struct udphdr *)outp;
1357c478bd9Sstevel@tonic-gate outudp->uh_sport = htons(ident);
1367c478bd9Sstevel@tonic-gate outudp->uh_ulen =
1377c478bd9Sstevel@tonic-gate htons((ushort_t)(plen - (sizeof (struct ip) + optlen)));
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate
1407c478bd9Sstevel@tonic-gate return (outip);
1417c478bd9Sstevel@tonic-gate }
1427c478bd9Sstevel@tonic-gate
1437c478bd9Sstevel@tonic-gate /*
1447c478bd9Sstevel@tonic-gate * Setup the source routing for IPv4.
1457c478bd9Sstevel@tonic-gate */
1467c478bd9Sstevel@tonic-gate void
set_IPv4opt_sourcerouting(int sndsock,union any_in_addr * ip_addr,union any_in_addr * gwIPlist)1477c478bd9Sstevel@tonic-gate set_IPv4opt_sourcerouting(int sndsock, union any_in_addr *ip_addr,
1487c478bd9Sstevel@tonic-gate union any_in_addr *gwIPlist)
1497c478bd9Sstevel@tonic-gate {
1507c478bd9Sstevel@tonic-gate struct protoent *pe;
1517c478bd9Sstevel@tonic-gate struct ip_sourceroute *srp;
1527c478bd9Sstevel@tonic-gate uchar_t optlist[MAX_IPOPTLEN];
1537c478bd9Sstevel@tonic-gate int i;
1547d6fbbeaSblu int gwV4_count;
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate if ((pe = getprotobyname("ip")) == NULL) {
1577c478bd9Sstevel@tonic-gate Fprintf(stderr, "%s: unknown protocol ip\n", prog);
1587c478bd9Sstevel@tonic-gate exit(EXIT_FAILURE);
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate
1617d6fbbeaSblu gwV4_count = (gw_count < MAX_GWS) ? gw_count : MAX_GWS - 1;
1627c478bd9Sstevel@tonic-gate /* final hop */
1637d6fbbeaSblu gwIPlist[gwV4_count].addr = ip_addr->addr;
1647c478bd9Sstevel@tonic-gate
1657c478bd9Sstevel@tonic-gate /*
1667c478bd9Sstevel@tonic-gate * the option length passed to setsockopt() needs to be a multiple of
1677c478bd9Sstevel@tonic-gate * 32 bits. Therefore we need to use a 1-byte padding (source routing
1687c478bd9Sstevel@tonic-gate * information takes 4x+3 bytes).
1697c478bd9Sstevel@tonic-gate */
1707c478bd9Sstevel@tonic-gate optlist[0] = IPOPT_NOP;
1717c478bd9Sstevel@tonic-gate
1727c478bd9Sstevel@tonic-gate srp = (struct ip_sourceroute *)&optlist[1];
1737c478bd9Sstevel@tonic-gate srp->ipsr_code = IPOPT_LSRR;
1747c478bd9Sstevel@tonic-gate /* 3 = 1 (code) + 1 (len) + 1 (ptr) */
1757d6fbbeaSblu srp->ipsr_len = 3 + (gwV4_count + 1) * sizeof (gwIPlist[0].addr);
1767c478bd9Sstevel@tonic-gate srp->ipsr_ptr = IPOPT_MINOFF;
1777c478bd9Sstevel@tonic-gate
1787d6fbbeaSblu for (i = 0; i <= gwV4_count; i++) {
1797c478bd9Sstevel@tonic-gate (void) bcopy((char *)&gwIPlist[i].addr, &srp->ipsr_addrs[i],
1807c478bd9Sstevel@tonic-gate sizeof (struct in_addr));
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate
1837c478bd9Sstevel@tonic-gate if (setsockopt(sndsock, pe->p_proto, IP_OPTIONS, (const char *)optlist,
1847c478bd9Sstevel@tonic-gate srp->ipsr_len + 1) < 0) {
1857c478bd9Sstevel@tonic-gate Fprintf(stderr, "%s: IP_OPTIONS: %s\n", prog, strerror(errno));
1867c478bd9Sstevel@tonic-gate exit(EXIT_FAILURE);
1877c478bd9Sstevel@tonic-gate }
1887c478bd9Sstevel@tonic-gate }
1897c478bd9Sstevel@tonic-gate
1907c478bd9Sstevel@tonic-gate /*
1917c478bd9Sstevel@tonic-gate * send a probe packet to the destination
1927c478bd9Sstevel@tonic-gate */
1937c478bd9Sstevel@tonic-gate void
send_probe(int sndsock,struct sockaddr * to,struct ip * outip,int seq,int ttl,struct timeval * tp,int packlen)1947c478bd9Sstevel@tonic-gate send_probe(int sndsock, struct sockaddr *to, struct ip *outip,
1957c478bd9Sstevel@tonic-gate int seq, int ttl, struct timeval *tp, int packlen)
1967c478bd9Sstevel@tonic-gate {
1977c478bd9Sstevel@tonic-gate int cc;
1987c478bd9Sstevel@tonic-gate struct udpiphdr *ui;
1997c478bd9Sstevel@tonic-gate uchar_t *outp; /* packet following the IP header (UDP/ICMP) */
2007c478bd9Sstevel@tonic-gate struct udphdr *outudp;
2017c478bd9Sstevel@tonic-gate struct icmp *outicmp;
2027c478bd9Sstevel@tonic-gate struct outdata *outdata;
2037c478bd9Sstevel@tonic-gate struct ip tip;
2047c478bd9Sstevel@tonic-gate int optlen = 0;
2057c478bd9Sstevel@tonic-gate int send_size;
2067c478bd9Sstevel@tonic-gate
2077c478bd9Sstevel@tonic-gate /* initialize buffer pointers */
2087c478bd9Sstevel@tonic-gate outp = (uchar_t *)(outip + 1);
2097c478bd9Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */
2107c478bd9Sstevel@tonic-gate outudp = (struct udphdr *)outp;
2117c478bd9Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */
2127c478bd9Sstevel@tonic-gate outicmp = (struct icmp *)outp;
2137c478bd9Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */
2147c478bd9Sstevel@tonic-gate outdata = (struct outdata *)(outp + ICMP_MINLEN);
2157c478bd9Sstevel@tonic-gate
2167c478bd9Sstevel@tonic-gate if (gw_count > 0) {
2177c478bd9Sstevel@tonic-gate /* 8 = 5 (NO OPs) + 3 (code, len, ptr) */
2187c478bd9Sstevel@tonic-gate optlen = 8 + gw_count * sizeof (struct in_addr);
2197c478bd9Sstevel@tonic-gate }
2207c478bd9Sstevel@tonic-gate
2217c478bd9Sstevel@tonic-gate if (raw_req) {
2227c478bd9Sstevel@tonic-gate send_size = packlen - optlen;
2237c478bd9Sstevel@tonic-gate } else if (useicmp) {
2247c478bd9Sstevel@tonic-gate send_size = packlen - optlen - sizeof (struct ip);
2257c478bd9Sstevel@tonic-gate } else {
2267c478bd9Sstevel@tonic-gate send_size = packlen - optlen - sizeof (struct ip) -
2277c478bd9Sstevel@tonic-gate sizeof (struct udphdr);
2287c478bd9Sstevel@tonic-gate }
2297c478bd9Sstevel@tonic-gate
2307c478bd9Sstevel@tonic-gate outip->ip_ttl = ttl;
2317c478bd9Sstevel@tonic-gate outip->ip_id = htons(ident + seq);
2327c478bd9Sstevel@tonic-gate
2337c478bd9Sstevel@tonic-gate /*
2347c478bd9Sstevel@tonic-gate * If a raw IPv4 packet is going to be sent, the Time to Live
2357c478bd9Sstevel@tonic-gate * field in the packet was initialized above. Otherwise, it is
2367c478bd9Sstevel@tonic-gate * initialized here using the IPPROTO_IP level socket option.
2377c478bd9Sstevel@tonic-gate */
2387c478bd9Sstevel@tonic-gate if (!raw_req) {
2397c478bd9Sstevel@tonic-gate if (setsockopt(sndsock, IPPROTO_IP, IP_TTL, (char *)&ttl,
2407c478bd9Sstevel@tonic-gate sizeof (ttl)) < 0) {
2417c478bd9Sstevel@tonic-gate Fprintf(stderr, "%s: IP_TTL: %s\n", prog,
2427c478bd9Sstevel@tonic-gate strerror(errno));
2437c478bd9Sstevel@tonic-gate exit(EXIT_FAILURE);
2447c478bd9Sstevel@tonic-gate }
2457c478bd9Sstevel@tonic-gate }
2467c478bd9Sstevel@tonic-gate
2477c478bd9Sstevel@tonic-gate /*
2487c478bd9Sstevel@tonic-gate * In most cases, the kernel will recalculate the ip checksum.
2497c478bd9Sstevel@tonic-gate * But we must do it anyway so that the udp checksum comes out
2507c478bd9Sstevel@tonic-gate * right.
2517c478bd9Sstevel@tonic-gate */
2527c478bd9Sstevel@tonic-gate if (docksum) {
2537c478bd9Sstevel@tonic-gate outip->ip_sum =
2547c478bd9Sstevel@tonic-gate in_cksum((ushort_t *)outip, sizeof (*outip) + optlen);
2557c478bd9Sstevel@tonic-gate if (outip->ip_sum == 0)
2567c478bd9Sstevel@tonic-gate outip->ip_sum = 0xffff;
2577c478bd9Sstevel@tonic-gate }
2587c478bd9Sstevel@tonic-gate
2597c478bd9Sstevel@tonic-gate /* Payload */
2607c478bd9Sstevel@tonic-gate outdata->seq = seq;
2617c478bd9Sstevel@tonic-gate outdata->ttl = ttl;
2627c478bd9Sstevel@tonic-gate outdata->tv = *tp;
2637c478bd9Sstevel@tonic-gate
2647c478bd9Sstevel@tonic-gate if (useicmp) {
2657c478bd9Sstevel@tonic-gate outicmp->icmp_seq = htons(seq);
2667c478bd9Sstevel@tonic-gate } else {
2677c478bd9Sstevel@tonic-gate outudp->uh_dport = htons((port + seq) % (MAX_PORT + 1));
2687c478bd9Sstevel@tonic-gate }
2697c478bd9Sstevel@tonic-gate
2707c478bd9Sstevel@tonic-gate if (!raw_req)
2717c478bd9Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */
2727c478bd9Sstevel@tonic-gate ((struct sockaddr_in *)to)->sin_port = outudp->uh_dport;
2737c478bd9Sstevel@tonic-gate
2747c478bd9Sstevel@tonic-gate /* (We can only do the checksum if we know our ip address) */
2757c478bd9Sstevel@tonic-gate if (docksum) {
2767c478bd9Sstevel@tonic-gate if (useicmp) {
2777c478bd9Sstevel@tonic-gate outicmp->icmp_cksum = 0;
2787c478bd9Sstevel@tonic-gate outicmp->icmp_cksum = in_cksum((ushort_t *)outicmp,
2797c478bd9Sstevel@tonic-gate packlen - (sizeof (struct ip) + optlen));
2807c478bd9Sstevel@tonic-gate if (outicmp->icmp_cksum == 0)
2817c478bd9Sstevel@tonic-gate outicmp->icmp_cksum = 0xffff;
2827c478bd9Sstevel@tonic-gate } else {
2837c478bd9Sstevel@tonic-gate /* Checksum (must save and restore ip header) */
2847c478bd9Sstevel@tonic-gate tip = *outip;
2857c478bd9Sstevel@tonic-gate ui = (struct udpiphdr *)outip;
2867c478bd9Sstevel@tonic-gate ui->ui_next = 0;
2877c478bd9Sstevel@tonic-gate ui->ui_prev = 0;
2887c478bd9Sstevel@tonic-gate ui->ui_x1 = 0;
2897c478bd9Sstevel@tonic-gate ui->ui_len = outudp->uh_ulen;
2907c478bd9Sstevel@tonic-gate outudp->uh_sum = 0;
2917c478bd9Sstevel@tonic-gate outudp->uh_sum = in_cksum((ushort_t *)ui, packlen);
2927c478bd9Sstevel@tonic-gate if (outudp->uh_sum == 0)
2937c478bd9Sstevel@tonic-gate outudp->uh_sum = 0xffff;
2947c478bd9Sstevel@tonic-gate *outip = tip;
2957c478bd9Sstevel@tonic-gate }
2967c478bd9Sstevel@tonic-gate }
2977c478bd9Sstevel@tonic-gate
2987c478bd9Sstevel@tonic-gate if (raw_req) {
2997c478bd9Sstevel@tonic-gate cc = sendto(sndsock, (char *)outip, send_size, 0, to,
3007c478bd9Sstevel@tonic-gate sizeof (struct sockaddr_in));
3017c478bd9Sstevel@tonic-gate } else if (useicmp) {
3027c478bd9Sstevel@tonic-gate cc = sendto(sndsock, (char *)outicmp, send_size, 0, to,
3037c478bd9Sstevel@tonic-gate sizeof (struct sockaddr_in));
3047c478bd9Sstevel@tonic-gate } else {
3057c478bd9Sstevel@tonic-gate cc = sendto(sndsock, (char *)outp, send_size, 0, to,
3067c478bd9Sstevel@tonic-gate sizeof (struct sockaddr_in));
3077c478bd9Sstevel@tonic-gate }
3087c478bd9Sstevel@tonic-gate
3097c478bd9Sstevel@tonic-gate if (cc < 0 || cc != send_size) {
3107c478bd9Sstevel@tonic-gate if (cc < 0) {
3117c478bd9Sstevel@tonic-gate Fprintf(stderr, "%s: sendto: %s\n", prog,
3127c478bd9Sstevel@tonic-gate strerror(errno));
3137c478bd9Sstevel@tonic-gate }
3147c478bd9Sstevel@tonic-gate Printf("%s: wrote %s %d chars, ret=%d\n",
3157c478bd9Sstevel@tonic-gate prog, hostname, send_size, cc);
3167c478bd9Sstevel@tonic-gate (void) fflush(stdout);
3177c478bd9Sstevel@tonic-gate }
3187c478bd9Sstevel@tonic-gate }
3197c478bd9Sstevel@tonic-gate
3207c478bd9Sstevel@tonic-gate /*
3217c478bd9Sstevel@tonic-gate * Check out the reply packet to see if it's what we were expecting.
3227c478bd9Sstevel@tonic-gate * Returns REPLY_GOT_TARGET if the reply comes from the target
3237c478bd9Sstevel@tonic-gate * REPLY_GOT_GATEWAY if an intermediate gateway sends TIME_EXCEEDED
3247c478bd9Sstevel@tonic-gate * REPLY_GOT_OTHER for other kinds of unreachables indicating none of
3257c478bd9Sstevel@tonic-gate * the above two cases
3267c478bd9Sstevel@tonic-gate *
3277c478bd9Sstevel@tonic-gate * It also sets the icmp type and icmp code values
3287c478bd9Sstevel@tonic-gate */
3297c478bd9Sstevel@tonic-gate int
check_reply(struct msghdr * msg,int cc,int seq,uchar_t * type,uchar_t * code)3307c478bd9Sstevel@tonic-gate check_reply(struct msghdr *msg, int cc, int seq, uchar_t *type, uchar_t *code)
3317c478bd9Sstevel@tonic-gate {
3327c478bd9Sstevel@tonic-gate uchar_t *buf = msg->msg_iov->iov_base;
3337c478bd9Sstevel@tonic-gate struct sockaddr_in *from_in = (struct sockaddr_in *)msg->msg_name;
3347c478bd9Sstevel@tonic-gate struct icmp *icp;
3357c478bd9Sstevel@tonic-gate int hlen;
3367c478bd9Sstevel@tonic-gate int save_cc = cc;
3377c478bd9Sstevel@tonic-gate struct ip *ip;
3387c478bd9Sstevel@tonic-gate
3397c478bd9Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */
3407c478bd9Sstevel@tonic-gate ip = (struct ip *)buf;
3417c478bd9Sstevel@tonic-gate hlen = ip->ip_hl << 2;
3427c478bd9Sstevel@tonic-gate if (cc < hlen + ICMP_MINLEN) {
3437c478bd9Sstevel@tonic-gate if (verbose) {
3447c478bd9Sstevel@tonic-gate Printf("packet too short (%d bytes) from %s\n",
3457c478bd9Sstevel@tonic-gate cc, inet_ntoa(from_in->sin_addr));
3467c478bd9Sstevel@tonic-gate }
3477c478bd9Sstevel@tonic-gate return (REPLY_SHORT_PKT);
3487c478bd9Sstevel@tonic-gate }
3497c478bd9Sstevel@tonic-gate cc -= hlen;
3507c478bd9Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */
3517c478bd9Sstevel@tonic-gate icp = (struct icmp *)(buf + hlen);
3527c478bd9Sstevel@tonic-gate
3537c478bd9Sstevel@tonic-gate *type = icp->icmp_type;
3547c478bd9Sstevel@tonic-gate *code = icp->icmp_code;
3557c478bd9Sstevel@tonic-gate
3567c478bd9Sstevel@tonic-gate /*
3577c478bd9Sstevel@tonic-gate * traceroute interpretes only ICMP_TIMXCEED_INTRANS, ICMP_UNREACH and
3587c478bd9Sstevel@tonic-gate * ICMP_ECHOREPLY, ignores others
3597c478bd9Sstevel@tonic-gate */
3607c478bd9Sstevel@tonic-gate if ((*type == ICMP_TIMXCEED && *code == ICMP_TIMXCEED_INTRANS) ||
3617c478bd9Sstevel@tonic-gate *type == ICMP_UNREACH || *type == ICMP_ECHOREPLY) {
3627c478bd9Sstevel@tonic-gate struct ip *hip;
3637c478bd9Sstevel@tonic-gate struct udphdr *up;
3647c478bd9Sstevel@tonic-gate struct icmp *hicmp;
3657c478bd9Sstevel@tonic-gate
3667c478bd9Sstevel@tonic-gate cc -= ICMP_MINLEN;
3677c478bd9Sstevel@tonic-gate hip = &icp->icmp_ip;
3687c478bd9Sstevel@tonic-gate hlen = hip->ip_hl << 2;
3697c478bd9Sstevel@tonic-gate cc -= hlen;
3707c478bd9Sstevel@tonic-gate if (useicmp) {
3717c478bd9Sstevel@tonic-gate if (*type == ICMP_ECHOREPLY &&
3727c478bd9Sstevel@tonic-gate icp->icmp_id == htons(ident) &&
3737c478bd9Sstevel@tonic-gate icp->icmp_seq == htons(seq))
3747c478bd9Sstevel@tonic-gate return (REPLY_GOT_TARGET);
3757c478bd9Sstevel@tonic-gate
3767c478bd9Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */
3777c478bd9Sstevel@tonic-gate hicmp = (struct icmp *)((uchar_t *)hip + hlen);
3787c478bd9Sstevel@tonic-gate
3797c478bd9Sstevel@tonic-gate if (ICMP_MINLEN <= cc &&
3807c478bd9Sstevel@tonic-gate hip->ip_p == IPPROTO_ICMP &&
3817c478bd9Sstevel@tonic-gate hicmp->icmp_id == htons(ident) &&
3827c478bd9Sstevel@tonic-gate hicmp->icmp_seq == htons(seq)) {
3837c478bd9Sstevel@tonic-gate return ((*type == ICMP_TIMXCEED) ?
3847c478bd9Sstevel@tonic-gate REPLY_GOT_GATEWAY : REPLY_GOT_OTHER);
3857c478bd9Sstevel@tonic-gate }
3867c478bd9Sstevel@tonic-gate } else {
3877c478bd9Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */
3887c478bd9Sstevel@tonic-gate up = (struct udphdr *)((uchar_t *)hip + hlen);
3897c478bd9Sstevel@tonic-gate /*
3907c478bd9Sstevel@tonic-gate * at least 4 bytes of UDP header is required for this
3917c478bd9Sstevel@tonic-gate * check
3927c478bd9Sstevel@tonic-gate */
3937c478bd9Sstevel@tonic-gate if (4 <= cc &&
3947c478bd9Sstevel@tonic-gate hip->ip_p == IPPROTO_UDP &&
3957c478bd9Sstevel@tonic-gate up->uh_sport == htons(ident) &&
3967c478bd9Sstevel@tonic-gate up->uh_dport == htons((port + seq) %
3977c478bd9Sstevel@tonic-gate (MAX_PORT + 1))) {
3987c478bd9Sstevel@tonic-gate if (*type == ICMP_UNREACH &&
3997c478bd9Sstevel@tonic-gate *code == ICMP_UNREACH_PORT) {
4007c478bd9Sstevel@tonic-gate return (REPLY_GOT_TARGET);
4017c478bd9Sstevel@tonic-gate } else if (*type == ICMP_TIMXCEED) {
4027c478bd9Sstevel@tonic-gate return (REPLY_GOT_GATEWAY);
4037c478bd9Sstevel@tonic-gate } else {
4047c478bd9Sstevel@tonic-gate return (REPLY_GOT_OTHER);
4057c478bd9Sstevel@tonic-gate }
4067c478bd9Sstevel@tonic-gate }
4077c478bd9Sstevel@tonic-gate }
4087c478bd9Sstevel@tonic-gate }
4097c478bd9Sstevel@tonic-gate
4107c478bd9Sstevel@tonic-gate if (verbose) {
4117c478bd9Sstevel@tonic-gate int i, j;
4127c478bd9Sstevel@tonic-gate uchar_t *lp = (uchar_t *)ip;
4137c478bd9Sstevel@tonic-gate
4147c478bd9Sstevel@tonic-gate cc = save_cc;
4157c478bd9Sstevel@tonic-gate Printf("\n%d bytes from %s to ", cc,
4167c478bd9Sstevel@tonic-gate inet_ntoa(from_in->sin_addr));
4177c478bd9Sstevel@tonic-gate Printf("%s: icmp type %d (%s) code %d\n",
4187c478bd9Sstevel@tonic-gate inet_ntoa(ip->ip_dst), *type, pr_type(*type), *code);
4197c478bd9Sstevel@tonic-gate for (i = 0; i < cc; i += 4) {
4207c478bd9Sstevel@tonic-gate Printf("%2d: x", i);
4217c478bd9Sstevel@tonic-gate for (j = 0; ((j < 4) && ((i + j) < cc)); j++)
4227c478bd9Sstevel@tonic-gate Printf("%2.2x", *lp++);
4237c478bd9Sstevel@tonic-gate (void) putchar('\n');
4247c478bd9Sstevel@tonic-gate }
4257c478bd9Sstevel@tonic-gate }
4267c478bd9Sstevel@tonic-gate
4277c478bd9Sstevel@tonic-gate return (REPLY_SHORT_PKT);
4287c478bd9Sstevel@tonic-gate }
4297c478bd9Sstevel@tonic-gate
4307c478bd9Sstevel@tonic-gate /*
4317c478bd9Sstevel@tonic-gate * convert an ICMP "type" field to a printable string.
4327c478bd9Sstevel@tonic-gate */
4337c478bd9Sstevel@tonic-gate static char *
pr_type(uchar_t type)4347c478bd9Sstevel@tonic-gate pr_type(uchar_t type)
4357c478bd9Sstevel@tonic-gate {
4367c478bd9Sstevel@tonic-gate static struct icmptype_table ttab[] = {
4377c478bd9Sstevel@tonic-gate {ICMP_ECHOREPLY, "Echo Reply"},
4387c478bd9Sstevel@tonic-gate {1, "ICMP 1"},
4397c478bd9Sstevel@tonic-gate {2, "ICMP 2"},
4407c478bd9Sstevel@tonic-gate {ICMP_UNREACH, "Dest Unreachable"},
4417c478bd9Sstevel@tonic-gate {ICMP_SOURCEQUENCH, "Source Quench"},
4427c478bd9Sstevel@tonic-gate {ICMP_REDIRECT, "Redirect"},
4437c478bd9Sstevel@tonic-gate {6, "ICMP 6"},
4447c478bd9Sstevel@tonic-gate {7, "ICMP 7"},
4457c478bd9Sstevel@tonic-gate {ICMP_ECHO, "Echo"},
4467c478bd9Sstevel@tonic-gate {ICMP_ROUTERADVERT, "Router Advertisement"},
4477c478bd9Sstevel@tonic-gate {ICMP_ROUTERSOLICIT, "Router Solicitation"},
4487c478bd9Sstevel@tonic-gate {ICMP_TIMXCEED, "Time Exceeded"},
4497c478bd9Sstevel@tonic-gate {ICMP_PARAMPROB, "Param Problem"},
4507c478bd9Sstevel@tonic-gate {ICMP_TSTAMP, "Timestamp"},
4517c478bd9Sstevel@tonic-gate {ICMP_TSTAMPREPLY, "Timestamp Reply"},
4527c478bd9Sstevel@tonic-gate {ICMP_IREQ, "Info Request"},
4537c478bd9Sstevel@tonic-gate {ICMP_IREQREPLY, "Info Reply"},
4547c478bd9Sstevel@tonic-gate {ICMP_MASKREQ, "Netmask Request"},
4557c478bd9Sstevel@tonic-gate {ICMP_MASKREPLY, "Netmask Reply"}
4567c478bd9Sstevel@tonic-gate };
4577c478bd9Sstevel@tonic-gate int i = 0;
4587c478bd9Sstevel@tonic-gate
4597c478bd9Sstevel@tonic-gate for (i = 0; i < A_CNT(ttab); i++) {
4607c478bd9Sstevel@tonic-gate if (ttab[i].type == type)
4617c478bd9Sstevel@tonic-gate return (ttab[i].message);
4627c478bd9Sstevel@tonic-gate }
4637c478bd9Sstevel@tonic-gate
4647c478bd9Sstevel@tonic-gate return ("OUT-OF-RANGE");
4657c478bd9Sstevel@tonic-gate }
4667c478bd9Sstevel@tonic-gate
4677c478bd9Sstevel@tonic-gate /*
4687c478bd9Sstevel@tonic-gate * print the IPv4 src address of the reply packet
4697c478bd9Sstevel@tonic-gate */
4707c478bd9Sstevel@tonic-gate void
print_addr(uchar_t * buf,int cc,struct sockaddr * from)4717c478bd9Sstevel@tonic-gate print_addr(uchar_t *buf, int cc, struct sockaddr *from)
4727c478bd9Sstevel@tonic-gate {
4737c478bd9Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */
4747c478bd9Sstevel@tonic-gate struct sockaddr_in *from_in = (struct sockaddr_in *)from;
4757c478bd9Sstevel@tonic-gate struct ip *ip;
4767c478bd9Sstevel@tonic-gate union any_in_addr ip_addr;
4777c478bd9Sstevel@tonic-gate
4787c478bd9Sstevel@tonic-gate ip_addr.addr = from_in->sin_addr;
4797c478bd9Sstevel@tonic-gate
4807c478bd9Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */
4817c478bd9Sstevel@tonic-gate ip = (struct ip *)buf;
4827c478bd9Sstevel@tonic-gate
4837c478bd9Sstevel@tonic-gate if (nflag) {
4847c478bd9Sstevel@tonic-gate Printf(" %s", inet_ntoa(from_in->sin_addr));
4857c478bd9Sstevel@tonic-gate } else {
4867c478bd9Sstevel@tonic-gate Printf(" %s (%s)", inet_name(&ip_addr, AF_INET),
4877c478bd9Sstevel@tonic-gate inet_ntoa(from_in->sin_addr));
4887c478bd9Sstevel@tonic-gate }
4897c478bd9Sstevel@tonic-gate
4907c478bd9Sstevel@tonic-gate if (verbose)
4917c478bd9Sstevel@tonic-gate Printf(" %d bytes to %s", cc, inet_ntoa(ip->ip_dst));
4927c478bd9Sstevel@tonic-gate }
4937c478bd9Sstevel@tonic-gate
4947c478bd9Sstevel@tonic-gate /*
4957c478bd9Sstevel@tonic-gate * ICMP messages which doesn't mean we got the target, or we got a gateway, are
4967c478bd9Sstevel@tonic-gate * processed here. It returns _B_TRUE if it's some sort of 'unreachable'.
4977c478bd9Sstevel@tonic-gate */
4987c478bd9Sstevel@tonic-gate boolean_t
print_icmp_other(uchar_t type,uchar_t code)4997c478bd9Sstevel@tonic-gate print_icmp_other(uchar_t type, uchar_t code)
5007c478bd9Sstevel@tonic-gate {
5017c478bd9Sstevel@tonic-gate boolean_t unreach = _B_FALSE;
5027c478bd9Sstevel@tonic-gate
5037c478bd9Sstevel@tonic-gate /*
5047c478bd9Sstevel@tonic-gate * this function only prints '!*' for ICMP unreachable messages,
5057c478bd9Sstevel@tonic-gate * ignores others.
5067c478bd9Sstevel@tonic-gate */
5077c478bd9Sstevel@tonic-gate if (type != ICMP_UNREACH) {
5087c478bd9Sstevel@tonic-gate return (_B_FALSE);
5097c478bd9Sstevel@tonic-gate }
5107c478bd9Sstevel@tonic-gate
5117c478bd9Sstevel@tonic-gate switch (code) {
5127c478bd9Sstevel@tonic-gate case ICMP_UNREACH_PORT:
5137c478bd9Sstevel@tonic-gate break;
5147c478bd9Sstevel@tonic-gate
5157c478bd9Sstevel@tonic-gate case ICMP_UNREACH_NET_UNKNOWN:
5167c478bd9Sstevel@tonic-gate case ICMP_UNREACH_NET:
5177c478bd9Sstevel@tonic-gate unreach = _B_TRUE;
5187c478bd9Sstevel@tonic-gate Printf(" !N");
5197c478bd9Sstevel@tonic-gate break;
5207c478bd9Sstevel@tonic-gate
5217c478bd9Sstevel@tonic-gate case ICMP_UNREACH_HOST_UNKNOWN:
5227c478bd9Sstevel@tonic-gate case ICMP_UNREACH_HOST:
5237c478bd9Sstevel@tonic-gate unreach = _B_TRUE;
5247c478bd9Sstevel@tonic-gate Printf(" !H");
5257c478bd9Sstevel@tonic-gate break;
5267c478bd9Sstevel@tonic-gate
5277c478bd9Sstevel@tonic-gate case ICMP_UNREACH_PROTOCOL:
5287c478bd9Sstevel@tonic-gate Printf(" !P");
5297c478bd9Sstevel@tonic-gate break;
5307c478bd9Sstevel@tonic-gate
5317c478bd9Sstevel@tonic-gate case ICMP_UNREACH_NEEDFRAG:
5327c478bd9Sstevel@tonic-gate unreach = _B_TRUE;
5337c478bd9Sstevel@tonic-gate Printf(" !F");
5347c478bd9Sstevel@tonic-gate break;
5357c478bd9Sstevel@tonic-gate
5367c478bd9Sstevel@tonic-gate case ICMP_UNREACH_SRCFAIL:
5377c478bd9Sstevel@tonic-gate unreach = _B_TRUE;
5387c478bd9Sstevel@tonic-gate Printf(" !S");
5397c478bd9Sstevel@tonic-gate break;
5407c478bd9Sstevel@tonic-gate
5417c478bd9Sstevel@tonic-gate case ICMP_UNREACH_FILTER_PROHIB:
5427c478bd9Sstevel@tonic-gate case ICMP_UNREACH_NET_PROHIB:
5437c478bd9Sstevel@tonic-gate case ICMP_UNREACH_HOST_PROHIB:
5447c478bd9Sstevel@tonic-gate unreach = _B_TRUE;
5457c478bd9Sstevel@tonic-gate Printf(" !X");
5467c478bd9Sstevel@tonic-gate break;
5477c478bd9Sstevel@tonic-gate
5487c478bd9Sstevel@tonic-gate case ICMP_UNREACH_TOSNET:
5497c478bd9Sstevel@tonic-gate case ICMP_UNREACH_TOSHOST:
5507c478bd9Sstevel@tonic-gate unreach = _B_TRUE;
5517c478bd9Sstevel@tonic-gate Printf(" !T");
5527c478bd9Sstevel@tonic-gate break;
5537c478bd9Sstevel@tonic-gate
5547c478bd9Sstevel@tonic-gate case ICMP_UNREACH_ISOLATED:
5557c478bd9Sstevel@tonic-gate case ICMP_UNREACH_HOST_PRECEDENCE:
5567c478bd9Sstevel@tonic-gate case ICMP_UNREACH_PRECEDENCE_CUTOFF:
5577c478bd9Sstevel@tonic-gate unreach = _B_TRUE;
5587c478bd9Sstevel@tonic-gate Printf(" !U");
5597c478bd9Sstevel@tonic-gate break;
5607c478bd9Sstevel@tonic-gate
5617c478bd9Sstevel@tonic-gate default:
5627c478bd9Sstevel@tonic-gate unreach = _B_TRUE;
5637c478bd9Sstevel@tonic-gate Printf(" !<%d>", code);
5647c478bd9Sstevel@tonic-gate break;
5657c478bd9Sstevel@tonic-gate }
5667c478bd9Sstevel@tonic-gate
5677c478bd9Sstevel@tonic-gate return (unreach);
5687c478bd9Sstevel@tonic-gate }
569