1*58031b70Sclaudio /* $OpenBSD: icmp.c,v 1.19 2019/10/03 13:36:15 claudio Exp $ */
2e853bc5dShenning
3e853bc5dShenning /*
4e853bc5dShenning * Copyright (c) 1997, 1998 The Internet Software Consortium.
5e853bc5dShenning * All rights reserved.
6e853bc5dShenning *
7e853bc5dShenning * Redistribution and use in source and binary forms, with or without
8e853bc5dShenning * modification, are permitted provided that the following conditions
9e853bc5dShenning * are met:
10e853bc5dShenning *
11e853bc5dShenning * 1. Redistributions of source code must retain the above copyright
12e853bc5dShenning * notice, this list of conditions and the following disclaimer.
13e853bc5dShenning * 2. Redistributions in binary form must reproduce the above copyright
14e853bc5dShenning * notice, this list of conditions and the following disclaimer in the
15e853bc5dShenning * documentation and/or other materials provided with the distribution.
16e853bc5dShenning * 3. Neither the name of The Internet Software Consortium nor the names
17e853bc5dShenning * of its contributors may be used to endorse or promote products derived
18e853bc5dShenning * from this software without specific prior written permission.
19e853bc5dShenning *
20e853bc5dShenning * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
21e853bc5dShenning * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
22e853bc5dShenning * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23e853bc5dShenning * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24e853bc5dShenning * DISCLAIMED. IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
25e853bc5dShenning * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26e853bc5dShenning * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27e853bc5dShenning * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28e853bc5dShenning * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29e853bc5dShenning * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30e853bc5dShenning * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31e853bc5dShenning * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32e853bc5dShenning * SUCH DAMAGE.
33e853bc5dShenning *
34e853bc5dShenning * This software has been written for the Internet Software Consortium
35e853bc5dShenning * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie
36e853bc5dShenning * Enterprises. To learn more about the Internet Software Consortium,
37e853bc5dShenning * see ``http://www.vix.com/isc''. To learn more about Vixie
38e853bc5dShenning * Enterprises, see ``http://www.vix.com''.
39e853bc5dShenning */
40e853bc5dShenning
41837cddffSkrw #include <sys/types.h>
42837cddffSkrw #include <sys/socket.h>
43837cddffSkrw
44837cddffSkrw #include <arpa/inet.h>
45837cddffSkrw
46837cddffSkrw #include <net/if.h>
47837cddffSkrw
48e853bc5dShenning #include <netinet/ip.h>
49e853bc5dShenning #include <netinet/ip_icmp.h>
50e853bc5dShenning
51837cddffSkrw #include <netdb.h>
52837cddffSkrw #include <stdio.h>
53837cddffSkrw #include <string.h>
54837cddffSkrw #include <unistd.h>
55837cddffSkrw
56837cddffSkrw #include "dhcp.h"
57837cddffSkrw #include "tree.h"
58837cddffSkrw #include "dhcpd.h"
59c525a185Skrw #include "log.h"
60837cddffSkrw
61e853bc5dShenning static int icmp_protocol_initialized;
62e853bc5dShenning static int icmp_protocol_fd;
63e853bc5dShenning
64e853bc5dShenning /* Initialize the ICMP protocol. */
65e853bc5dShenning
664221dff3Sderaadt void
icmp_startup(int routep,void (* handler)(struct iaddr,u_int8_t *,int))67e3f7fea4Spelikan icmp_startup(int routep, void (*handler)(struct iaddr, u_int8_t *, int))
68e853bc5dShenning {
69e853bc5dShenning struct protoent *proto;
704221dff3Sderaadt int protocol = 1, state;
71e853bc5dShenning
72e853bc5dShenning /* Only initialize icmp once. */
73e853bc5dShenning if (icmp_protocol_initialized)
74c525a185Skrw fatalx("attempted to reinitialize icmp protocol");
75e853bc5dShenning icmp_protocol_initialized = 1;
76e853bc5dShenning
77e853bc5dShenning /* Get the protocol number (should be 1). */
7817506e25Shenning if ((proto = getprotobyname("icmp")) != NULL)
79e853bc5dShenning protocol = proto->p_proto;
80e853bc5dShenning
81e853bc5dShenning /* Get a raw socket for the ICMP protocol. */
8217506e25Shenning if ((icmp_protocol_fd = socket(AF_INET, SOCK_RAW, protocol)) == -1)
830438cf0aSkrw fatal("unable to create icmp socket");
84e853bc5dShenning
85e853bc5dShenning /* Make sure it does routing... */
86e853bc5dShenning state = 0;
87e853bc5dShenning if (setsockopt(icmp_protocol_fd, SOL_SOCKET, SO_DONTROUTE,
889bb003e4Sclaudio &state, sizeof(state)) == -1)
890438cf0aSkrw fatal("Unable to disable SO_DONTROUTE on ICMP socket");
90e853bc5dShenning
9135318e8fSkrw add_protocol("icmp", icmp_protocol_fd, icmp_echoreply,
9235318e8fSkrw (void *)handler);
93e853bc5dShenning }
94e853bc5dShenning
954221dff3Sderaadt int
icmp_echorequest(struct iaddr * addr)964221dff3Sderaadt icmp_echorequest(struct iaddr *addr)
97e853bc5dShenning {
98e853bc5dShenning struct sockaddr_in to;
99e853bc5dShenning struct icmp icmp;
100e853bc5dShenning int status;
101e853bc5dShenning
102e853bc5dShenning if (!icmp_protocol_initialized)
103c525a185Skrw fatalx("attempt to use ICMP protocol before initialization.");
104e853bc5dShenning
105359ce2c3Smestre memset(&to, 0, sizeof(to));
106e853bc5dShenning to.sin_len = sizeof to;
107e853bc5dShenning to.sin_family = AF_INET;
108e853bc5dShenning memcpy(&to.sin_addr, addr->iabuf, sizeof to.sin_addr); /* XXX */
109e853bc5dShenning
110*58031b70Sclaudio memset(&icmp, 0, sizeof(icmp));
111e853bc5dShenning icmp.icmp_type = ICMP_ECHO;
112490ecf04Shenning icmp.icmp_id = getpid() & 0xffff;
113e853bc5dShenning
114e853bc5dShenning icmp.icmp_cksum = wrapsum(checksum((unsigned char *)&icmp,
11517506e25Shenning sizeof(icmp), 0));
116e853bc5dShenning
117e853bc5dShenning /* Send the ICMP packet... */
11817506e25Shenning status = sendto(icmp_protocol_fd, &icmp, sizeof(icmp), 0,
11917506e25Shenning (struct sockaddr *)&to, sizeof(to));
1209bb003e4Sclaudio if (status == -1)
1210438cf0aSkrw log_warn("icmp_echorequest %s", inet_ntoa(to.sin_addr));
122e853bc5dShenning
123e853bc5dShenning if (status != sizeof icmp)
124e853bc5dShenning return 0;
125e853bc5dShenning return 1;
126e853bc5dShenning }
127e853bc5dShenning
1284221dff3Sderaadt void
icmp_echoreply(struct protocol * protocol)1294221dff3Sderaadt icmp_echoreply(struct protocol *protocol)
130e853bc5dShenning {
131e3f7fea4Spelikan void (*handler)(struct iaddr, u_int8_t *, int);
132e853bc5dShenning struct sockaddr_in from;
133e853bc5dShenning u_int8_t icbuf[1500];
1344221dff3Sderaadt struct icmp *icfrom;
135e853bc5dShenning int status, len;
136e853bc5dShenning socklen_t salen;
137e853bc5dShenning struct iaddr ia;
138e853bc5dShenning
139e853bc5dShenning salen = sizeof from;
14017506e25Shenning status = recvfrom(protocol->fd, icbuf, sizeof(icbuf), 0,
141e853bc5dShenning (struct sockaddr *)&from, &salen);
1429bb003e4Sclaudio if (status == -1) {
1430438cf0aSkrw log_warn("icmp_echoreply");
144e853bc5dShenning return;
145e853bc5dShenning }
146e853bc5dShenning
147e853bc5dShenning /* Probably not for us. */
1484221dff3Sderaadt if (status < (sizeof(struct ip)) + (sizeof *icfrom))
149e853bc5dShenning return;
150e853bc5dShenning
151e853bc5dShenning len = status - sizeof(struct ip);
152e853bc5dShenning icfrom = (struct icmp *)(icbuf + sizeof(struct ip));
153e853bc5dShenning
154e853bc5dShenning /* Silently discard ICMP packets that aren't echoreplies. */
1554221dff3Sderaadt if (icfrom->icmp_type != ICMP_ECHOREPLY)
156e853bc5dShenning return;
157e853bc5dShenning
158e3f7fea4Spelikan /* If we were given a second-stage handler, call it. */
159e3f7fea4Spelikan if (protocol->local) {
160e3f7fea4Spelikan handler = ((void (*)(struct iaddr, u_int8_t *, int))
161e3f7fea4Spelikan protocol->local);
162e853bc5dShenning memcpy(ia.iabuf, &from.sin_addr, sizeof from.sin_addr);
163e853bc5dShenning ia.len = sizeof from.sin_addr;
164e3f7fea4Spelikan (*handler)(ia, icbuf, len);
165e3f7fea4Spelikan }
166e853bc5dShenning }
167