2
0
mirror of https://gitlab.isc.org/isc-projects/dhcp synced 2025-08-22 18:07:25 +00:00
isc-dhcp/common/icmp.c

207 lines
5.3 KiB
C
Raw Normal View History

1997-03-06 07:08:00 +00:00
/* dhcp.c
ICMP Protocol engine - for sending out pings and receiving
responses. */
/*
1999-03-16 05:50:46 +00:00
* Copyright (c) 1996-1999 Internet Software Consortium.
* Use is subject to license terms which appear in the file named
* ISC-LICENSE that should have accompanied this file when you
* received it. If a file named ISC-LICENSE did not accompany this
* file, or you are not sure the one you have is correct, you may
* obtain an applicable copy of the license at:
1997-03-06 07:08:00 +00:00
*
1999-03-16 05:50:46 +00:00
* http://www.isc.org/isc-license-1.0.html.
1997-03-06 07:08:00 +00:00
*
1999-03-16 05:50:46 +00:00
* This file is part of the ISC DHCP distribution. The documentation
* associated with this file is listed in the file DOCUMENTATION,
* included in the top-level directory of this release.
1997-03-06 07:08:00 +00:00
*
1999-03-16 05:50:46 +00:00
* Support and other services are available for ISC products - see
* http://www.isc.org for more information.
1997-03-06 07:08:00 +00:00
*/
1997-03-06 07:27:32 +00:00
#ifndef lint
static char copyright[] =
"$Id: icmp.c,v 1.18 2000/02/01 03:19:38 mellon Exp $ Copyright (c) 1997, 1998 The Internet Software Consortium. All rights reserved.\n";
1997-03-06 07:27:32 +00:00
#endif /* not lint */
#include "dhcpd.h"
#include "netinet/ip.h"
#include "netinet/ip_icmp.h"
1999-09-09 21:02:10 +00:00
struct icmp_state {
OMAPI_OBJECT_PREAMBLE;
int socket;
void (*icmp_handler) PROTO ((struct iaddr, u_int8_t *, int));
};
static struct icmp_state *icmp_state;
static omapi_object_type_t *dhcp_type_icmp;
1997-03-06 07:08:00 +00:00
/* Initialize the ICMP protocol. */
void icmp_startup (routep, handler)
int routep;
1997-03-06 07:27:32 +00:00
void (*handler) PROTO ((struct iaddr, u_int8_t *, int));
1997-03-06 07:08:00 +00:00
{
struct protoent *proto;
int protocol = 1;
struct sockaddr_in from;
1997-03-06 07:27:32 +00:00
int fd;
int state;
1999-09-09 21:02:10 +00:00
struct icmp_state *new;
omapi_object_t *h;
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)
log_fatal ("attempted to reinitialize icmp protocol");
1999-09-09 21:02:10 +00:00
result = omapi_object_type_register (&dhcp_type_icmp,
"icmp", 0, 0, 0, 0, 0, 0, 0, 0);
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));
new = (struct icmp_state *)dmalloc (sizeof *new, MDL);
1999-09-09 21:02:10 +00:00
if (!new)
log_fatal ("Unable to allocate state for icmp protocol");
memset (new, 0, sizeof *new);
new -> refcnt = 1;
new -> type = dhcp_type_icmp;
new -> icmp_handler = handler;
1997-03-06 07:08:00 +00:00
/* Get the protocol number (should be 1). */
proto = getprotobyname ("icmp");
if (proto)
protocol = proto -> p_proto;
/* Get a raw socket for the ICMP protocol. */
1999-09-09 21:02:10 +00:00
new -> socket = socket (AF_INET, SOCK_RAW, protocol);
if (new -> socket < 0)
log_fatal ("unable to create icmp socket: %m");
1997-03-06 07:08:00 +00:00
2000-01-05 18:01:41 +00:00
#if defined (HAVE_SETFD)
if (fcntl (new -> socket, F_SETFD, 1) < 0)
log_error ("Can't set close-on-exec on icmp socket: %m");
#endif
/* Make sure it does routing... */
state = 0;
1999-09-09 21:02:10 +00:00
if (setsockopt (new -> socket, SOL_SOCKET, SO_DONTROUTE,
(char *)&state, sizeof state) < 0)
1999-09-09 21:02:10 +00:00
log_fatal ("Can't disable SO_DONTROUTE on ICMP socket: %m");
result = omapi_register_io_object ((omapi_object_t *)new,
icmp_readsocket, 0,
icmp_echoreply, 0, 0);
if (result != ISC_R_SUCCESS)
log_fatal ("Can't register icmp handle: %s",
isc_result_totext (result));
icmp_state = new;
}
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;
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
#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;
#ifdef PTRSIZE_64BIT
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
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));
/* Send the ICMP packet... */
1999-09-09 21:02:10 +00:00
status = sendto (icmp_state -> socket, (char *)&icmp, sizeof icmp, 0,
1997-03-06 07:08:00 +00:00
(struct sockaddr *)&to, sizeof to);
if (status < 0)
log_error ("icmp_echorequest %s: %m", inet_ntoa(to.sin_addr));
1997-03-06 07:08:00 +00:00
if (status != sizeof icmp)
return 0;
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;
struct ip *ip;
1997-03-06 07:08:00 +00:00
struct sockaddr_in from;
unsigned char icbuf [1500];
int status;
socklen_t len;
int hlen;
1997-03-06 07:08:00 +00:00
struct iaddr ia;
1999-09-09 21:02:10 +00:00
struct icmp_state *state;
state = (struct icmp_state *)h;
1997-03-06 07:08:00 +00:00
len = sizeof from;
1999-09-09 21:02:10 +00:00
status = recvfrom (state -> socket, (char *)icbuf, sizeof icbuf, 0,
1997-03-06 07:08:00 +00:00
(struct sockaddr *)&from, &len);
1997-03-06 07:27:32 +00:00
if (status < 0) {
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
/* Find the IP header length... */
ip = (struct ip *)icbuf;
hlen = IP_HL (ip) << 2;
/* 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
}
len = status - hlen;
icfrom = (struct icmp *)(icbuf + hlen);
1997-03-06 07:08:00 +00:00
/* Silently discard ICMP packets that aren't echoreplies. */
if (icfrom -> icmp_type != ICMP_ECHOREPLY) {
1999-09-09 21:02:10 +00:00
return ISC_R_SUCCESS;
}
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;
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
}