3 ICMP Protocol engine - for sending out pings and receiving
7 * Copyright (c) 1996-2002 Internet Software Consortium.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of The Internet Software Consortium nor the names
20 * of its contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
24 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 * DISCLAIMED. IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
31 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
32 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
34 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * This software has been written for the Internet Software Consortium
38 * by Ted Lemon in cooperation with Vixie Enterprises and Nominum, Inc.
39 * To learn more about the Internet Software Consortium, see
40 * ``http://www.isc.org/''. To learn more about Vixie Enterprises,
41 * see ``http://www.vix.com''. To learn more about Nominum, Inc., see
42 * ``http://www.nominum.com''.
46 static char copyright[] =
47 "$Id: icmp.c,v 1.30.2.5 2002/11/17 02:26:58 dhankins Exp $ Copyright (c) 1996-2002 The Internet Software Consortium. All rights reserved.\n";
51 #include "netinet/ip.h"
52 #include "netinet/ip_icmp.h"
54 struct icmp_state *icmp_state;
55 static omapi_object_type_t *dhcp_type_icmp;
58 OMAPI_OBJECT_ALLOC (icmp_state, struct icmp_state, dhcp_type_icmp)
61 trace_type_t *trace_icmp_input;
62 trace_type_t *trace_icmp_output;
65 /* Initialize the ICMP protocol. */
67 void icmp_startup (routep, handler)
69 void (*handler) PROTO ((struct iaddr, u_int8_t *, int));
71 struct protoent *proto;
73 struct sockaddr_in from;
76 struct icmp_state *new;
80 /* Only initialize icmp once. */
82 log_fatal ("attempted to reinitialize icmp protocol");
84 result = omapi_object_type_register (&dhcp_type_icmp, "icmp",
85 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
86 sizeof (struct icmp_state),
89 if (result != ISC_R_SUCCESS)
90 log_fatal ("Can't register icmp object type: %s",
91 isc_result_totext (result));
93 icmp_state_allocate (&icmp_state, MDL);
94 icmp_state -> icmp_handler = handler;
97 trace_icmp_input = trace_type_register ("icmp-input", (void *)0,
98 trace_icmp_input_input,
99 trace_icmp_input_stop, MDL);
100 trace_icmp_output = trace_type_register ("icmp-output", (void *)0,
101 trace_icmp_output_input,
102 trace_icmp_output_stop, MDL);
104 /* If we're playing back a trace file, don't create the socket
105 or set up the callback. */
106 if (!trace_playback ()) {
108 /* Get the protocol number (should be 1). */
109 proto = getprotobyname ("icmp");
111 protocol = proto -> p_proto;
113 /* Get a raw socket for the ICMP protocol. */
114 icmp_state -> socket = socket (AF_INET, SOCK_RAW, protocol);
115 if (icmp_state -> socket < 0) {
117 log_error ("unable to create icmp socket: %m");
121 #if defined (HAVE_SETFD)
122 if (fcntl (icmp_state -> socket, F_SETFD, 1) < 0)
123 log_error ("Can't set close-on-exec on icmp: %m");
126 /* Make sure it does routing... */
128 if (setsockopt (icmp_state -> socket, SOL_SOCKET, SO_DONTROUTE,
129 (char *)&state, sizeof state) < 0)
130 log_fatal ("Can't disable SO_DONTROUTE on ICMP: %m");
132 result = (omapi_register_io_object
133 ((omapi_object_t *)icmp_state,
134 icmp_readsocket, 0, icmp_echoreply, 0, 0));
135 if (result != ISC_R_SUCCESS)
136 log_fatal ("Can't register icmp handle: %s",
137 isc_result_totext (result));
138 #if defined (TRACING)
143 int icmp_readsocket (h)
146 struct icmp_state *state;
148 state = (struct icmp_state *)h;
149 return state -> socket;
152 int icmp_echorequest (addr)
155 struct sockaddr_in to;
158 #if defined (TRACING)
165 log_fatal ("ICMP protocol used before initialization.");
167 memset (&to, 0, sizeof(to));
169 to.sin_len = sizeof to;
171 to.sin_family = AF_INET;
172 to.sin_port = 0; /* unused. */
173 memcpy (&to.sin_addr, addr -> iabuf, sizeof to.sin_addr); /* XXX */
175 icmp.icmp_type = ICMP_ECHO;
180 icmp.icmp_id = (((u_int32_t)(u_int64_t)addr) ^
181 (u_int32_t)(((u_int64_t)addr) >> 32));
183 icmp.icmp_id = (u_int32_t)addr;
185 memset (&icmp.icmp_dun, 0, sizeof icmp.icmp_dun);
187 icmp.icmp_cksum = wrapsum (checksum ((unsigned char *)&icmp,
190 #if defined (TRACING)
191 if (trace_playback ()) {
192 char *buf = (char *)0;
195 /* Consume the ICMP event. */
196 status = trace_get_packet (&trace_icmp_output, &buflen, &buf);
197 if (status != ISC_R_SUCCESS)
198 log_error ("icmp_echorequest: %s",
199 isc_result_totext (status));
203 if (trace_record ()) {
204 iov [0].buf = (char *)addr;
205 iov [0].len = sizeof *addr;
206 iov [1].buf = (char *)&icmp;
207 iov [1].len = sizeof icmp;
208 trace_write_packet_iov (trace_icmp_output,
212 /* Send the ICMP packet... */
213 status = sendto (icmp_state -> socket,
214 (char *)&icmp, sizeof icmp, 0,
215 (struct sockaddr *)&to, sizeof to);
217 log_error ("icmp_echorequest %s: %m",
218 inet_ntoa(to.sin_addr));
220 if (status != sizeof icmp)
222 #if defined (TRACING)
228 isc_result_t icmp_echoreply (h)
233 struct sockaddr_in from;
234 u_int8_t icbuf [1500];
239 struct icmp_state *state;
240 #if defined (TRACING)
244 state = (struct icmp_state *)h;
247 status = recvfrom (state -> socket, (char *)icbuf, sizeof icbuf, 0,
248 (struct sockaddr *)&from, &sl);
250 log_error ("icmp_echoreply: %m");
251 return ISC_R_UNEXPECTED;
254 /* Find the IP header length... */
255 ip = (struct ip *)icbuf;
259 if (status < hlen + (sizeof *icfrom)) {
260 return ISC_R_SUCCESS;
264 icfrom = (struct icmp *)(icbuf + hlen);
266 /* Silently discard ICMP packets that aren't echoreplies. */
267 if (icfrom -> icmp_type != ICMP_ECHOREPLY) {
268 return ISC_R_SUCCESS;
271 /* If we were given a second-stage handler, call it. */
272 if (state -> icmp_handler) {
273 memcpy (ia.iabuf, &from.sin_addr, sizeof from.sin_addr);
274 ia.len = sizeof from.sin_addr;
276 #if defined (TRACING)
277 if (trace_record ()) {
278 ia.len = htonl(ia.len);
279 iov [0].buf = (char *)&ia;
280 iov [0].len = sizeof ia;
281 iov [1].buf = (char *)icbuf;
283 trace_write_packet_iov (trace_icmp_input, 2, iov, MDL);
284 ia.len = ntohl(ia.len);
287 (*state -> icmp_handler) (ia, icbuf, len);
289 return ISC_R_SUCCESS;
292 #if defined (TRACING)
293 void trace_icmp_input_input (trace_type_t *ttype, unsigned length, char *buf)
298 ia = (struct iaddr *)buf;
299 ia->len = ntohl(ia->len);
300 icbuf = (u_int8_t *)(ia + 1);
301 if (icmp_state -> icmp_handler)
302 (*icmp_state -> icmp_handler) (*ia, icbuf,
303 (int)(length - sizeof ia));
306 void trace_icmp_input_stop (trace_type_t *ttype) { }
308 void trace_icmp_output_input (trace_type_t *ttype, unsigned length, char *buf)
313 if (length != (sizeof (*icmp) + (sizeof ia))) {
314 log_error ("trace_icmp_output_input: data size mismatch %d:%d",
315 length, (int)((sizeof (*icmp)) + (sizeof ia)));
319 memcpy (ia.iabuf, buf, 4);
320 icmp = (struct icmp *)(buf + 1);
322 log_error ("trace_icmp_output_input: unsent ping to %s", piaddr (ia));
325 void trace_icmp_output_stop (trace_type_t *ttype) { }