3
ICMP Protocol engine - for sending out pings and receiving
7
* Copyright (c) 2004,2007 by Internet Systems Consortium, Inc. ("ISC")
8
* Copyright (c) 1996-2003 by Internet Software Consortium
10
* Permission to use, copy, modify, and distribute this software for any
11
* purpose with or without fee is hereby granted, provided that the above
12
* copyright notice and this permission notice appear in all copies.
14
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
15
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
17
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
20
* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
* Internet Systems Consortium, Inc.
24
* Redwood City, CA 94063
28
* This software has been written for Internet Systems Consortium
29
* by Ted Lemon in cooperation with Vixie Enterprises and Nominum, Inc.
30
* To learn more about Internet Systems Consortium, see
31
* ``http://www.isc.org/''. To learn more about Vixie Enterprises,
32
* see ``http://www.vix.com''. To learn more about Nominum, Inc., see
33
* ``http://www.nominum.com''.
37
#include "netinet/ip.h"
38
#include "netinet/ip_icmp.h"
40
struct icmp_state *icmp_state;
41
static omapi_object_type_t *dhcp_type_icmp;
44
OMAPI_OBJECT_ALLOC (icmp_state, struct icmp_state, dhcp_type_icmp)
47
trace_type_t *trace_icmp_input;
48
trace_type_t *trace_icmp_output;
51
/* Initialize the ICMP protocol. */
53
void icmp_startup (routep, handler)
55
void (*handler) PROTO ((struct iaddr, u_int8_t *, int));
57
struct protoent *proto;
62
/* Only initialize icmp once. */
64
log_fatal ("attempted to reinitialize icmp protocol");
66
result = omapi_object_type_register (&dhcp_type_icmp, "icmp",
67
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
68
sizeof (struct icmp_state),
71
if (result != ISC_R_SUCCESS)
72
log_fatal ("Can't register icmp object type: %s",
73
isc_result_totext (result));
75
icmp_state_allocate (&icmp_state, MDL);
76
icmp_state -> icmp_handler = handler;
79
trace_icmp_input = trace_type_register ("icmp-input", (void *)0,
80
trace_icmp_input_input,
81
trace_icmp_input_stop, MDL);
82
trace_icmp_output = trace_type_register ("icmp-output", (void *)0,
83
trace_icmp_output_input,
84
trace_icmp_output_stop, MDL);
86
/* If we're playing back a trace file, don't create the socket
87
or set up the callback. */
88
if (!trace_playback ()) {
90
/* Get the protocol number (should be 1). */
91
proto = getprotobyname ("icmp");
93
protocol = proto -> p_proto;
95
/* Get a raw socket for the ICMP protocol. */
96
icmp_state -> socket = socket (AF_INET, SOCK_RAW, protocol);
97
if (icmp_state -> socket < 0) {
99
log_error ("unable to create icmp socket: %m");
103
#if defined (HAVE_SETFD)
104
if (fcntl (icmp_state -> socket, F_SETFD, 1) < 0)
105
log_error ("Can't set close-on-exec on icmp: %m");
108
/* Make sure it does routing... */
110
if (setsockopt (icmp_state -> socket, SOL_SOCKET, SO_DONTROUTE,
111
(char *)&state, sizeof state) < 0)
112
log_fatal ("Can't disable SO_DONTROUTE on ICMP: %m");
114
result = (omapi_register_io_object
115
((omapi_object_t *)icmp_state,
116
icmp_readsocket, 0, icmp_echoreply, 0, 0));
117
if (result != ISC_R_SUCCESS)
118
log_fatal ("Can't register icmp handle: %s",
119
isc_result_totext (result));
120
#if defined (TRACING)
125
int icmp_readsocket (h)
128
struct icmp_state *state;
130
state = (struct icmp_state *)h;
131
return state -> socket;
134
int icmp_echorequest (addr)
137
struct sockaddr_in to;
140
#if defined (TRACING)
147
log_fatal ("ICMP protocol used before initialization.");
149
memset (&to, 0, sizeof(to));
151
to.sin_len = sizeof to;
153
to.sin_family = AF_INET;
154
to.sin_port = 0; /* unused. */
155
memcpy (&to.sin_addr, addr -> iabuf, sizeof to.sin_addr); /* XXX */
157
icmp.icmp_type = ICMP_ECHO;
161
#if SIZEOF_STRUCT_IADDR_P == 8
162
icmp.icmp_id = (((u_int32_t)(u_int64_t)addr) ^
163
(u_int32_t)(((u_int64_t)addr) >> 32));
165
icmp.icmp_id = (u_int32_t)addr;
167
memset (&icmp.icmp_dun, 0, sizeof icmp.icmp_dun);
169
icmp.icmp_cksum = wrapsum (checksum ((unsigned char *)&icmp,
172
#if defined (TRACING)
173
if (trace_playback ()) {
174
char *buf = (char *)0;
177
/* Consume the ICMP event. */
178
status = trace_get_packet (&trace_icmp_output, &buflen, &buf);
179
if (status != ISC_R_SUCCESS)
180
log_error ("icmp_echorequest: %s",
181
isc_result_totext (status));
185
if (trace_record ()) {
186
iov [0].buf = (char *)addr;
187
iov [0].len = sizeof *addr;
188
iov [1].buf = (char *)&icmp;
189
iov [1].len = sizeof icmp;
190
trace_write_packet_iov (trace_icmp_output,
194
/* Send the ICMP packet... */
195
status = sendto (icmp_state -> socket,
196
(char *)&icmp, sizeof icmp, 0,
197
(struct sockaddr *)&to, sizeof to);
199
log_error ("icmp_echorequest %s: %m",
200
inet_ntoa(to.sin_addr));
202
if (status != sizeof icmp)
204
#if defined (TRACING)
210
isc_result_t icmp_echoreply (h)
215
struct sockaddr_in from;
216
u_int8_t icbuf [1500];
221
struct icmp_state *state;
222
#if defined (TRACING)
226
state = (struct icmp_state *)h;
229
status = recvfrom (state -> socket, (char *)icbuf, sizeof icbuf, 0,
230
(struct sockaddr *)&from, &sl);
232
log_error ("icmp_echoreply: %m");
233
return ISC_R_UNEXPECTED;
236
/* Find the IP header length... */
237
ip = (struct ip *)icbuf;
241
if (status < hlen + (sizeof *icfrom)) {
242
return ISC_R_SUCCESS;
246
icfrom = (struct icmp *)(icbuf + hlen);
248
/* Silently discard ICMP packets that aren't echoreplies. */
249
if (icfrom -> icmp_type != ICMP_ECHOREPLY) {
250
return ISC_R_SUCCESS;
253
/* If we were given a second-stage handler, call it. */
254
if (state -> icmp_handler) {
255
memcpy (ia.iabuf, &from.sin_addr, sizeof from.sin_addr);
256
ia.len = sizeof from.sin_addr;
258
#if defined (TRACING)
259
if (trace_record ()) {
260
ia.len = htonl(ia.len);
261
iov [0].buf = (char *)&ia;
262
iov [0].len = sizeof ia;
263
iov [1].buf = (char *)icbuf;
265
trace_write_packet_iov (trace_icmp_input, 2, iov, MDL);
266
ia.len = ntohl(ia.len);
269
(*state -> icmp_handler) (ia, icbuf, len);
271
return ISC_R_SUCCESS;
274
#if defined (TRACING)
275
void trace_icmp_input_input (trace_type_t *ttype, unsigned length, char *buf)
279
ia = (struct iaddr *)buf;
280
ia->len = ntohl(ia->len);
281
icbuf = (u_int8_t *)(ia + 1);
282
if (icmp_state -> icmp_handler)
283
(*icmp_state -> icmp_handler) (*ia, icbuf,
284
(int)(length - sizeof ia));
287
void trace_icmp_input_stop (trace_type_t *ttype) { }
289
void trace_icmp_output_input (trace_type_t *ttype, unsigned length, char *buf)
294
if (length != (sizeof (*icmp) + (sizeof ia))) {
295
log_error ("trace_icmp_output_input: data size mismatch %d:%d",
296
length, (int)((sizeof (*icmp)) + (sizeof ia)));
300
memcpy (ia.iabuf, buf, 4);
301
icmp = (struct icmp *)(buf + 1);
303
log_error ("trace_icmp_output_input: unsent ping to %s", piaddr (ia));
306
void trace_icmp_output_stop (trace_type_t *ttype) { }