1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 1991-2003 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate * 26*7c478bd9Sstevel@tonic-gate * icmp4.c, Code implementing the Internet Control Message Protocol (v4) ICMP. 27*7c478bd9Sstevel@tonic-gate */ 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 30*7c478bd9Sstevel@tonic-gate 31*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 32*7c478bd9Sstevel@tonic-gate #include <socket_impl.h> 33*7c478bd9Sstevel@tonic-gate #include <socket_inet.h> 34*7c478bd9Sstevel@tonic-gate #include <sys/salib.h> 35*7c478bd9Sstevel@tonic-gate #include <sys/socket.h> 36*7c478bd9Sstevel@tonic-gate #include <netinet/in_systm.h> 37*7c478bd9Sstevel@tonic-gate #include <netinet/in.h> 38*7c478bd9Sstevel@tonic-gate #include <netinet/ip.h> 39*7c478bd9Sstevel@tonic-gate #include <netinet/udp.h> 40*7c478bd9Sstevel@tonic-gate #include <netinet/ip_icmp.h> 41*7c478bd9Sstevel@tonic-gate #include <sys/bootconf.h> 42*7c478bd9Sstevel@tonic-gate #include <sys/fcntl.h> 43*7c478bd9Sstevel@tonic-gate 44*7c478bd9Sstevel@tonic-gate #include "icmp4.h" 45*7c478bd9Sstevel@tonic-gate #include "ipv4.h" 46*7c478bd9Sstevel@tonic-gate #include "ipv4_impl.h" 47*7c478bd9Sstevel@tonic-gate #include "mac.h" 48*7c478bd9Sstevel@tonic-gate #include "v4_sum_impl.h" 49*7c478bd9Sstevel@tonic-gate #include <sys/bootdebug.h> 50*7c478bd9Sstevel@tonic-gate 51*7c478bd9Sstevel@tonic-gate /* 52*7c478bd9Sstevel@tonic-gate * Handle ICMP redirects, ICMP echo messages. We only deal with redirects to a 53*7c478bd9Sstevel@tonic-gate * different default router by changing the default route to the specified 54*7c478bd9Sstevel@tonic-gate * value. 55*7c478bd9Sstevel@tonic-gate */ 56*7c478bd9Sstevel@tonic-gate void 57*7c478bd9Sstevel@tonic-gate icmp4(struct inetgram *igp, struct ip *iphp, uint16_t iphlen, 58*7c478bd9Sstevel@tonic-gate struct in_addr ipsrc) 59*7c478bd9Sstevel@tonic-gate { 60*7c478bd9Sstevel@tonic-gate int icmp_len; 61*7c478bd9Sstevel@tonic-gate struct in_addr our_ip; 62*7c478bd9Sstevel@tonic-gate struct icmp *icmphp; 63*7c478bd9Sstevel@tonic-gate 64*7c478bd9Sstevel@tonic-gate icmp_len = ntohs(iphp->ip_len) - iphlen; 65*7c478bd9Sstevel@tonic-gate if (icmp_len < ICMP_MINLEN) { 66*7c478bd9Sstevel@tonic-gate #ifdef DEBUG 67*7c478bd9Sstevel@tonic-gate printf("icmp4: ICMP message from %s is too short\n", 68*7c478bd9Sstevel@tonic-gate inet_ntoa(ipsrc)); 69*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */ 70*7c478bd9Sstevel@tonic-gate return; 71*7c478bd9Sstevel@tonic-gate } 72*7c478bd9Sstevel@tonic-gate 73*7c478bd9Sstevel@tonic-gate icmphp = (struct icmp *)(igp->igm_mp->b_rptr + iphlen); 74*7c478bd9Sstevel@tonic-gate 75*7c478bd9Sstevel@tonic-gate /* check alignment */ 76*7c478bd9Sstevel@tonic-gate if ((uintptr_t)icmphp % sizeof (uint16_t)) { 77*7c478bd9Sstevel@tonic-gate dprintf("icmp4: ICMP header not aligned (from %s)\n", 78*7c478bd9Sstevel@tonic-gate inet_ntoa(ipsrc)); 79*7c478bd9Sstevel@tonic-gate return; 80*7c478bd9Sstevel@tonic-gate } 81*7c478bd9Sstevel@tonic-gate if (ipv4cksum((uint16_t *)icmphp, icmp_len) != 0) { 82*7c478bd9Sstevel@tonic-gate dprintf("icmp4: Bad ICMP checksum (from %s)\n", 83*7c478bd9Sstevel@tonic-gate inet_ntoa(ipsrc)); 84*7c478bd9Sstevel@tonic-gate return; 85*7c478bd9Sstevel@tonic-gate } 86*7c478bd9Sstevel@tonic-gate switch (icmphp->icmp_type) { 87*7c478bd9Sstevel@tonic-gate case ICMP_REDIRECT: 88*7c478bd9Sstevel@tonic-gate if (icmphp->icmp_code != ICMP_REDIRECT_HOST) 89*7c478bd9Sstevel@tonic-gate break; 90*7c478bd9Sstevel@tonic-gate dprintf("ICMP Redirect to gateway %s.\n", 91*7c478bd9Sstevel@tonic-gate inet_ntoa(icmphp->icmp_gwaddr)); 92*7c478bd9Sstevel@tonic-gate if (ipv4_route(IPV4_ADD_ROUTE, RT_HOST, &icmphp->icmp_ip.ip_dst, 93*7c478bd9Sstevel@tonic-gate &icmphp->icmp_gwaddr) != 0) { 94*7c478bd9Sstevel@tonic-gate dprintf("icmp4: Cannot add route %s, %d\n", 95*7c478bd9Sstevel@tonic-gate inet_ntoa(icmphp->icmp_ip.ip_dst), errno); 96*7c478bd9Sstevel@tonic-gate } 97*7c478bd9Sstevel@tonic-gate break; 98*7c478bd9Sstevel@tonic-gate case ICMP_UNREACH: 99*7c478bd9Sstevel@tonic-gate /* 100*7c478bd9Sstevel@tonic-gate * Need to highlight an error on the socket, and save the 101*7c478bd9Sstevel@tonic-gate * destination address so that we can ensure it is destined 102*7c478bd9Sstevel@tonic-gate * to this socket. We need the port number to be sure... 103*7c478bd9Sstevel@tonic-gate */ 104*7c478bd9Sstevel@tonic-gate dprintf("ICMP destination unreachable (%s)\n", 105*7c478bd9Sstevel@tonic-gate inet_ntoa(icmphp->icmp_ip.ip_dst)); 106*7c478bd9Sstevel@tonic-gate break; 107*7c478bd9Sstevel@tonic-gate case ICMP_ECHO: 108*7c478bd9Sstevel@tonic-gate /* 109*7c478bd9Sstevel@tonic-gate * swap the source and destination IP addresses 110*7c478bd9Sstevel@tonic-gate * and send a reply right out. 111*7c478bd9Sstevel@tonic-gate */ 112*7c478bd9Sstevel@tonic-gate ipv4_getipaddr(&our_ip); 113*7c478bd9Sstevel@tonic-gate if (our_ip.s_addr != INADDR_ANY) { 114*7c478bd9Sstevel@tonic-gate int s; 115*7c478bd9Sstevel@tonic-gate struct sockaddr_in dest; 116*7c478bd9Sstevel@tonic-gate 117*7c478bd9Sstevel@tonic-gate if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0) { 118*7c478bd9Sstevel@tonic-gate dprintf("icmp4: socket: %d\n", errno); 119*7c478bd9Sstevel@tonic-gate break; 120*7c478bd9Sstevel@tonic-gate } 121*7c478bd9Sstevel@tonic-gate icmphp->icmp_type = ICMP_ECHOREPLY; 122*7c478bd9Sstevel@tonic-gate icmphp->icmp_cksum = 0; 123*7c478bd9Sstevel@tonic-gate icmphp->icmp_cksum = ipv4cksum((uint16_t *)icmphp, 124*7c478bd9Sstevel@tonic-gate icmp_len); 125*7c478bd9Sstevel@tonic-gate dest.sin_family = AF_INET; 126*7c478bd9Sstevel@tonic-gate dest.sin_addr.s_addr = ipsrc.s_addr; 127*7c478bd9Sstevel@tonic-gate dest.sin_port = htons(0); 128*7c478bd9Sstevel@tonic-gate (void) sendto(s, (caddr_t)icmphp, icmp_len, 0, 129*7c478bd9Sstevel@tonic-gate (const struct sockaddr *)&dest, sizeof (dest)); 130*7c478bd9Sstevel@tonic-gate (void) socket_close(s); 131*7c478bd9Sstevel@tonic-gate } 132*7c478bd9Sstevel@tonic-gate break; 133*7c478bd9Sstevel@tonic-gate default: 134*7c478bd9Sstevel@tonic-gate dprintf("icmp4: Unsupported ICMP message type: 0x%x " 135*7c478bd9Sstevel@tonic-gate "received from %s\n", icmphp->icmp_type, inet_ntoa(ipsrc)); 136*7c478bd9Sstevel@tonic-gate break; 137*7c478bd9Sstevel@tonic-gate } 138*7c478bd9Sstevel@tonic-gate } 139