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