1997-03-06 07:08:00 +00:00
|
|
|
/* dhcp.c
|
|
|
|
|
|
|
|
ICMP Protocol engine - for sending out pings and receiving
|
|
|
|
responses. */
|
|
|
|
|
|
|
|
/*
|
2022-01-25 16:24:16 +01:00
|
|
|
* Copyright (C) 2004-2022 Internet Systems Consortium, Inc. ("ISC")
|
2005-03-17 20:15:29 +00:00
|
|
|
* Copyright (c) 1996-2003 by Internet Software Consortium
|
1997-03-06 07:08:00 +00:00
|
|
|
*
|
2017-07-12 09:23:23 -04:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
1997-03-06 07:08:00 +00:00
|
|
|
*
|
2005-03-17 20:15:29 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
|
|
|
|
* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
1997-03-06 07:08:00 +00:00
|
|
|
*
|
2005-03-17 20:15:29 +00:00
|
|
|
* Internet Systems Consortium, Inc.
|
2022-01-19 20:13:19 +01:00
|
|
|
* PO Box 360
|
|
|
|
* Newmarket, NH 03857 USA
|
2005-03-17 20:15:29 +00:00
|
|
|
* <info@isc.org>
|
2009-07-23 18:52:21 +00:00
|
|
|
* https://www.isc.org/
|
2000-03-17 04:00:32 +00:00
|
|
|
*
|
1997-03-06 07:08:00 +00:00
|
|
|
*/
|
|
|
|
|
1997-03-06 07:27:32 +00:00
|
|
|
#include "dhcpd.h"
|
|
|
|
#include "netinet/ip.h"
|
|
|
|
#include "netinet/ip_icmp.h"
|
|
|
|
|
2001-06-27 00:31:20 +00:00
|
|
|
struct icmp_state *icmp_state;
|
1999-09-09 21:02:10 +00:00
|
|
|
static omapi_object_type_t *dhcp_type_icmp;
|
2001-04-23 21:41:36 +00:00
|
|
|
static int no_icmp;
|
1997-03-06 07:08:00 +00:00
|
|
|
|
2001-06-27 00:31:20 +00:00
|
|
|
OMAPI_OBJECT_ALLOC (icmp_state, struct icmp_state, dhcp_type_icmp)
|
|
|
|
|
2001-02-12 19:42:21 +00:00
|
|
|
#if defined (TRACING)
|
|
|
|
trace_type_t *trace_icmp_input;
|
|
|
|
trace_type_t *trace_icmp_output;
|
|
|
|
#endif
|
|
|
|
|
1997-03-06 07:08:00 +00:00
|
|
|
/* Initialize the ICMP protocol. */
|
|
|
|
|
|
|
|
void icmp_startup (routep, handler)
|
|
|
|
int routep;
|
2011-05-11 14:01:25 +00:00
|
|
|
void (*handler) (struct iaddr, u_int8_t *, int);
|
1997-03-06 07:08:00 +00:00
|
|
|
{
|
|
|
|
struct protoent *proto;
|
|
|
|
int protocol = 1;
|
1997-05-09 08:05:28 +00:00
|
|
|
int state;
|
1999-09-09 21:02:10 +00:00
|
|
|
isc_result_t result;
|
1997-03-06 07:08:00 +00:00
|
|
|
|
|
|
|
/* Only initialize icmp once. */
|
1999-09-09 21:02:10 +00:00
|
|
|
if (dhcp_type_icmp)
|
1999-02-24 17:56:53 +00:00
|
|
|
log_fatal ("attempted to reinitialize icmp protocol");
|
1999-09-09 21:02:10 +00:00
|
|
|
|
2000-10-10 22:33:56 +00:00
|
|
|
result = omapi_object_type_register (&dhcp_type_icmp, "icmp",
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
2005-03-17 20:15:29 +00:00
|
|
|
sizeof (struct icmp_state),
|
|
|
|
0, RC_MISC);
|
1999-09-09 21:02:10 +00:00
|
|
|
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
log_fatal ("Can't register icmp object type: %s",
|
|
|
|
isc_result_totext (result));
|
|
|
|
|
2001-06-27 00:31:20 +00:00
|
|
|
icmp_state_allocate (&icmp_state, MDL);
|
|
|
|
icmp_state -> icmp_handler = handler;
|
1997-03-06 07:08:00 +00:00
|
|
|
|
2001-02-12 19:42:21 +00:00
|
|
|
#if defined (TRACING)
|
|
|
|
trace_icmp_input = trace_type_register ("icmp-input", (void *)0,
|
|
|
|
trace_icmp_input_input,
|
|
|
|
trace_icmp_input_stop, MDL);
|
|
|
|
trace_icmp_output = trace_type_register ("icmp-output", (void *)0,
|
|
|
|
trace_icmp_output_input,
|
|
|
|
trace_icmp_output_stop, MDL);
|
|
|
|
|
|
|
|
/* If we're playing back a trace file, don't create the socket
|
|
|
|
or set up the callback. */
|
|
|
|
if (!trace_playback ()) {
|
|
|
|
#endif
|
|
|
|
/* Get the protocol number (should be 1). */
|
|
|
|
proto = getprotobyname ("icmp");
|
|
|
|
if (proto)
|
|
|
|
protocol = proto -> p_proto;
|
2022-01-19 20:14:16 +01:00
|
|
|
|
2001-02-12 19:42:21 +00:00
|
|
|
/* Get a raw socket for the ICMP protocol. */
|
2001-06-27 00:31:20 +00:00
|
|
|
icmp_state -> socket = socket (AF_INET, SOCK_RAW, protocol);
|
|
|
|
if (icmp_state -> socket < 0) {
|
2001-04-23 21:41:36 +00:00
|
|
|
no_icmp = 1;
|
|
|
|
log_error ("unable to create icmp socket: %m");
|
|
|
|
return;
|
|
|
|
}
|
1997-03-06 07:08:00 +00:00
|
|
|
|
2000-01-05 18:01:41 +00:00
|
|
|
#if defined (HAVE_SETFD)
|
2001-06-27 00:31:20 +00:00
|
|
|
if (fcntl (icmp_state -> socket, F_SETFD, 1) < 0)
|
2001-02-12 19:42:21 +00:00
|
|
|
log_error ("Can't set close-on-exec on icmp: %m");
|
2000-01-05 18:01:41 +00:00
|
|
|
#endif
|
|
|
|
|
2001-02-12 19:42:21 +00:00
|
|
|
/* Make sure it does routing... */
|
|
|
|
state = 0;
|
2001-06-27 00:31:20 +00:00
|
|
|
if (setsockopt (icmp_state -> socket, SOL_SOCKET, SO_DONTROUTE,
|
2001-02-12 19:42:21 +00:00
|
|
|
(char *)&state, sizeof state) < 0)
|
|
|
|
log_fatal ("Can't disable SO_DONTROUTE on ICMP: %m");
|
|
|
|
|
2001-06-27 00:31:20 +00:00
|
|
|
result = (omapi_register_io_object
|
|
|
|
((omapi_object_t *)icmp_state,
|
|
|
|
icmp_readsocket, 0, icmp_echoreply, 0, 0));
|
2001-02-12 19:42:21 +00:00
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
log_fatal ("Can't register icmp handle: %s",
|
|
|
|
isc_result_totext (result));
|
|
|
|
#if defined (TRACING)
|
|
|
|
}
|
|
|
|
#endif
|
1999-09-09 21:02:10 +00:00
|
|
|
}
|
1997-03-06 07:08:00 +00:00
|
|
|
|
1999-09-09 21:02:10 +00:00
|
|
|
int icmp_readsocket (h)
|
|
|
|
omapi_object_t *h;
|
|
|
|
{
|
|
|
|
struct icmp_state *state;
|
|
|
|
|
|
|
|
state = (struct icmp_state *)h;
|
|
|
|
return state -> socket;
|
1997-03-06 07:08:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int icmp_echorequest (addr)
|
|
|
|
struct iaddr *addr;
|
|
|
|
{
|
|
|
|
struct sockaddr_in to;
|
|
|
|
struct icmp icmp;
|
|
|
|
int status;
|
2001-02-12 19:42:21 +00:00
|
|
|
#if defined (TRACING)
|
|
|
|
trace_iov_t iov [2];
|
|
|
|
#endif
|
1997-03-06 07:08:00 +00:00
|
|
|
|
2001-04-23 21:41:36 +00:00
|
|
|
if (no_icmp)
|
|
|
|
return 1;
|
1999-09-09 21:02:10 +00:00
|
|
|
if (!icmp_state)
|
|
|
|
log_fatal ("ICMP protocol used before initialization.");
|
1997-03-06 07:08:00 +00:00
|
|
|
|
2002-06-09 22:16:34 +00:00
|
|
|
memset (&to, 0, sizeof(to));
|
1997-03-06 07:08:00 +00:00
|
|
|
#ifdef HAVE_SA_LEN
|
|
|
|
to.sin_len = sizeof to;
|
|
|
|
#endif
|
|
|
|
to.sin_family = AF_INET;
|
|
|
|
to.sin_port = 0; /* unused. */
|
|
|
|
memcpy (&to.sin_addr, addr -> iabuf, sizeof to.sin_addr); /* XXX */
|
|
|
|
|
|
|
|
icmp.icmp_type = ICMP_ECHO;
|
|
|
|
icmp.icmp_code = 0;
|
|
|
|
icmp.icmp_cksum = 0;
|
|
|
|
icmp.icmp_seq = 0;
|
2007-05-23 10:35:11 +00:00
|
|
|
#if SIZEOF_STRUCT_IADDR_P == 8
|
1997-05-09 08:05:28 +00:00
|
|
|
icmp.icmp_id = (((u_int32_t)(u_int64_t)addr) ^
|
|
|
|
(u_int32_t)(((u_int64_t)addr) >> 32));
|
1997-03-06 07:08:00 +00:00
|
|
|
#else
|
|
|
|
icmp.icmp_id = (u_int32_t)addr;
|
|
|
|
#endif
|
1998-06-25 02:55:56 +00:00
|
|
|
memset (&icmp.icmp_dun, 0, sizeof icmp.icmp_dun);
|
1997-03-06 07:08:00 +00:00
|
|
|
|
|
|
|
icmp.icmp_cksum = wrapsum (checksum ((unsigned char *)&icmp,
|
|
|
|
sizeof icmp, 0));
|
|
|
|
|
2001-02-12 19:42:21 +00:00
|
|
|
#if defined (TRACING)
|
|
|
|
if (trace_playback ()) {
|
|
|
|
char *buf = (char *)0;
|
|
|
|
unsigned buflen = 0;
|
|
|
|
|
|
|
|
/* Consume the ICMP event. */
|
|
|
|
status = trace_get_packet (&trace_icmp_output, &buflen, &buf);
|
|
|
|
if (status != ISC_R_SUCCESS)
|
|
|
|
log_error ("icmp_echorequest: %s",
|
|
|
|
isc_result_totext (status));
|
|
|
|
if (buf)
|
|
|
|
dfree (buf, MDL);
|
|
|
|
} else {
|
|
|
|
if (trace_record ()) {
|
|
|
|
iov [0].buf = (char *)addr;
|
|
|
|
iov [0].len = sizeof *addr;
|
|
|
|
iov [1].buf = (char *)&icmp;
|
|
|
|
iov [1].len = sizeof icmp;
|
|
|
|
trace_write_packet_iov (trace_icmp_output,
|
|
|
|
2, iov, MDL);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
/* Send the ICMP packet... */
|
|
|
|
status = sendto (icmp_state -> socket,
|
|
|
|
(char *)&icmp, sizeof icmp, 0,
|
|
|
|
(struct sockaddr *)&to, sizeof to);
|
|
|
|
if (status < 0)
|
|
|
|
log_error ("icmp_echorequest %s: %m",
|
|
|
|
inet_ntoa(to.sin_addr));
|
|
|
|
|
|
|
|
if (status != sizeof icmp)
|
|
|
|
return 0;
|
|
|
|
#if defined (TRACING)
|
|
|
|
}
|
|
|
|
#endif
|
1997-03-06 07:08:00 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1999-09-09 21:02:10 +00:00
|
|
|
isc_result_t icmp_echoreply (h)
|
|
|
|
omapi_object_t *h;
|
1997-03-06 07:08:00 +00:00
|
|
|
{
|
|
|
|
struct icmp *icfrom;
|
1998-01-12 01:00:42 +00:00
|
|
|
struct ip *ip;
|
1997-03-06 07:08:00 +00:00
|
|
|
struct sockaddr_in from;
|
2001-02-17 21:34:56 +00:00
|
|
|
u_int8_t icbuf [1500];
|
1997-03-06 07:08:00 +00:00
|
|
|
int status;
|
2000-02-03 03:43:51 +00:00
|
|
|
SOCKLEN_T sl;
|
2000-02-01 18:19:48 +00:00
|
|
|
int hlen, len;
|
1997-03-06 07:08:00 +00:00
|
|
|
struct iaddr ia;
|
1999-09-09 21:02:10 +00:00
|
|
|
struct icmp_state *state;
|
2001-02-12 19:42:21 +00:00
|
|
|
#if defined (TRACING)
|
|
|
|
trace_iov_t iov [2];
|
|
|
|
#endif
|
1999-09-09 21:02:10 +00:00
|
|
|
|
|
|
|
state = (struct icmp_state *)h;
|
1997-03-06 07:08:00 +00:00
|
|
|
|
2000-02-01 18:19:48 +00:00
|
|
|
sl = sizeof from;
|
1999-09-09 21:02:10 +00:00
|
|
|
status = recvfrom (state -> socket, (char *)icbuf, sizeof icbuf, 0,
|
2000-02-01 18:19:48 +00:00
|
|
|
(struct sockaddr *)&from, &sl);
|
1997-03-06 07:27:32 +00:00
|
|
|
if (status < 0) {
|
1999-02-24 17:56:53 +00:00
|
|
|
log_error ("icmp_echoreply: %m");
|
1999-09-09 21:02:10 +00:00
|
|
|
return ISC_R_UNEXPECTED;
|
1997-03-06 07:08:00 +00:00
|
|
|
}
|
1997-06-04 20:59:40 +00:00
|
|
|
|
1998-01-12 01:00:42 +00:00
|
|
|
/* Find the IP header length... */
|
|
|
|
ip = (struct ip *)icbuf;
|
2000-02-01 18:25:09 +00:00
|
|
|
hlen = IP_HL (ip);
|
1998-01-12 01:00:42 +00:00
|
|
|
|
|
|
|
/* Short packet? */
|
|
|
|
if (status < hlen + (sizeof *icfrom)) {
|
1999-09-09 21:02:10 +00:00
|
|
|
return ISC_R_SUCCESS;
|
1997-03-06 07:08:00 +00:00
|
|
|
}
|
|
|
|
|
1998-01-12 01:00:42 +00:00
|
|
|
len = status - hlen;
|
|
|
|
icfrom = (struct icmp *)(icbuf + hlen);
|
1997-03-06 07:08:00 +00:00
|
|
|
|
|
|
|
/* Silently discard ICMP packets that aren't echoreplies. */
|
1997-03-08 00:22:01 +00:00
|
|
|
if (icfrom -> icmp_type != ICMP_ECHOREPLY) {
|
1999-09-09 21:02:10 +00:00
|
|
|
return ISC_R_SUCCESS;
|
1997-03-08 00:22:01 +00:00
|
|
|
}
|
1997-03-06 07:08:00 +00:00
|
|
|
|
|
|
|
/* If we were given a second-stage handler, call it. */
|
1999-09-09 21:02:10 +00:00
|
|
|
if (state -> icmp_handler) {
|
1997-03-06 07:27:32 +00:00
|
|
|
memcpy (ia.iabuf, &from.sin_addr, sizeof from.sin_addr);
|
1997-03-06 07:08:00 +00:00
|
|
|
ia.len = sizeof from.sin_addr;
|
|
|
|
|
2001-02-12 19:42:21 +00:00
|
|
|
#if defined (TRACING)
|
|
|
|
if (trace_record ()) {
|
2001-03-14 15:56:27 +00:00
|
|
|
ia.len = htonl(ia.len);
|
2001-02-12 19:42:21 +00:00
|
|
|
iov [0].buf = (char *)&ia;
|
|
|
|
iov [0].len = sizeof ia;
|
2001-02-17 21:34:56 +00:00
|
|
|
iov [1].buf = (char *)icbuf;
|
2001-02-12 19:42:21 +00:00
|
|
|
iov [1].len = len;
|
|
|
|
trace_write_packet_iov (trace_icmp_input, 2, iov, MDL);
|
2001-03-14 15:56:27 +00:00
|
|
|
ia.len = ntohl(ia.len);
|
2001-02-12 19:42:21 +00:00
|
|
|
}
|
|
|
|
#endif
|
1999-09-09 21:02:10 +00:00
|
|
|
(*state -> icmp_handler) (ia, icbuf, len);
|
1997-03-06 07:08:00 +00:00
|
|
|
}
|
1999-09-09 21:02:10 +00:00
|
|
|
return ISC_R_SUCCESS;
|
1997-03-06 07:08:00 +00:00
|
|
|
}
|
2001-02-12 19:42:21 +00:00
|
|
|
|
|
|
|
#if defined (TRACING)
|
|
|
|
void trace_icmp_input_input (trace_type_t *ttype, unsigned length, char *buf)
|
|
|
|
{
|
|
|
|
struct iaddr *ia;
|
2001-02-17 21:34:56 +00:00
|
|
|
u_int8_t *icbuf;
|
2001-02-12 19:42:21 +00:00
|
|
|
ia = (struct iaddr *)buf;
|
2001-03-14 15:56:27 +00:00
|
|
|
ia->len = ntohl(ia->len);
|
2001-02-17 21:34:56 +00:00
|
|
|
icbuf = (u_int8_t *)(ia + 1);
|
2001-02-12 19:42:21 +00:00
|
|
|
if (icmp_state -> icmp_handler)
|
|
|
|
(*icmp_state -> icmp_handler) (*ia, icbuf,
|
|
|
|
(int)(length - sizeof ia));
|
|
|
|
}
|
|
|
|
|
|
|
|
void trace_icmp_input_stop (trace_type_t *ttype) { }
|
|
|
|
|
|
|
|
void trace_icmp_output_input (trace_type_t *ttype, unsigned length, char *buf)
|
|
|
|
{
|
2001-06-27 00:31:20 +00:00
|
|
|
struct iaddr ia;
|
2001-02-12 19:42:21 +00:00
|
|
|
|
2013-05-15 10:49:54 +10:00
|
|
|
if (length != (sizeof (struct icmp) + sizeof (ia))) {
|
2001-02-12 19:42:21 +00:00
|
|
|
log_error ("trace_icmp_output_input: data size mismatch %d:%d",
|
2013-05-15 10:49:54 +10:00
|
|
|
length, (int)(sizeof (struct icmp) + sizeof (ia)));
|
2001-02-12 19:42:21 +00:00
|
|
|
return;
|
|
|
|
}
|
2001-06-27 00:31:20 +00:00
|
|
|
ia.len = 4;
|
|
|
|
memcpy (ia.iabuf, buf, 4);
|
2001-02-12 19:42:21 +00:00
|
|
|
|
2001-06-27 00:31:20 +00:00
|
|
|
log_error ("trace_icmp_output_input: unsent ping to %s", piaddr (ia));
|
2001-02-12 19:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void trace_icmp_output_stop (trace_type_t *ttype) { }
|
|
|
|
#endif /* TRACING */
|