1 /* $OpenBSD: icmp.c,v 1.9 2004/09/16 18:35:43 deraadt Exp $ */
2
3 /*
4 * Copyright (c) 1997, 1998 The Internet Software Consortium.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of The Internet Software Consortium nor the names
17 * of its contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
21 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
22 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
25 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * This software has been written for the Internet Software Consortium
35 * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie
36 * Enterprises. To learn more about the Internet Software Consortium,
37 * see ``http://www.vix.com/isc''. To learn more about Vixie
38 * Enterprises, see ``http://www.vix.com''.
39 */
40
41 #include "dhcpd.h"
42 #include <netinet/in_systm.h>
43 #include <netinet/ip.h>
44 #include <netinet/ip_icmp.h>
45
46 static int icmp_protocol_initialized;
47 static int icmp_protocol_fd;
48
49 /* Initialize the ICMP protocol. */
50
51 void
icmp_startup(int routep,void (* handler)(struct iaddr,u_int8_t *,int))52 icmp_startup(int routep, void (*handler)(struct iaddr, u_int8_t *, int))
53 {
54 struct protoent *proto;
55 int protocol = 1, state;
56
57 /* Only initialize icmp once. */
58 if (icmp_protocol_initialized)
59 error("attempted to reinitialize icmp protocol");
60 icmp_protocol_initialized = 1;
61
62 /* Get the protocol number (should be 1). */
63 if ((proto = getprotobyname("icmp")) != NULL)
64 protocol = proto->p_proto;
65
66 /* Get a raw socket for the ICMP protocol. */
67 if ((icmp_protocol_fd = socket(AF_INET, SOCK_RAW, protocol)) == -1)
68 error("unable to create icmp socket: %m");
69
70 /* Make sure it does routing... */
71 state = 0;
72 if (setsockopt(icmp_protocol_fd, SOL_SOCKET, SO_DONTROUTE,
73 &state, sizeof(state)) < 0)
74 error("Unable to disable SO_DONTROUTE on ICMP socket: %m");
75
76 add_protocol("icmp", icmp_protocol_fd, icmp_echoreply, (void *)handler);
77 }
78
79 int
icmp_echorequest(struct iaddr * addr)80 icmp_echorequest(struct iaddr *addr)
81 {
82 struct sockaddr_in to;
83 struct icmp icmp;
84 int status;
85
86 if (!icmp_protocol_initialized)
87 error("attempt to use ICMP protocol before initialization.");
88
89 bzero(&to, sizeof(to));
90 to.sin_len = sizeof to;
91 to.sin_family = AF_INET;
92 memcpy(&to.sin_addr, addr->iabuf, sizeof to.sin_addr); /* XXX */
93
94 icmp.icmp_type = ICMP_ECHO;
95 icmp.icmp_code = 0;
96 icmp.icmp_cksum = 0;
97 icmp.icmp_seq = 0;
98 icmp.icmp_id = getpid() & 0xffff;
99
100 icmp.icmp_cksum = wrapsum(checksum((unsigned char *)&icmp,
101 sizeof(icmp), 0));
102
103 /* Send the ICMP packet... */
104 status = sendto(icmp_protocol_fd, &icmp, sizeof(icmp), 0,
105 (struct sockaddr *)&to, sizeof(to));
106 if (status < 0)
107 warning("icmp_echorequest %s: %m", inet_ntoa(to.sin_addr));
108
109 if (status != sizeof icmp)
110 return 0;
111 return 1;
112 }
113
114 void
icmp_echoreply(struct protocol * protocol)115 icmp_echoreply(struct protocol *protocol)
116 {
117 void (*handler)(struct iaddr, u_int8_t *, int);
118 struct sockaddr_in from;
119 u_int8_t icbuf[1500];
120 struct icmp *icfrom;
121 int status, len;
122 socklen_t salen;
123 struct iaddr ia;
124
125 salen = sizeof from;
126 status = recvfrom(protocol->fd, icbuf, sizeof(icbuf), 0,
127 (struct sockaddr *)&from, &salen);
128 if (status < 0) {
129 warning("icmp_echoreply: %m");
130 return;
131 }
132
133 /* Probably not for us. */
134 if (status < (sizeof(struct ip)) + (sizeof *icfrom))
135 return;
136
137 len = status - sizeof(struct ip);
138 icfrom = (struct icmp *)(icbuf + sizeof(struct ip));
139
140 /* Silently discard ICMP packets that aren't echoreplies. */
141 if (icfrom->icmp_type != ICMP_ECHOREPLY)
142 return;
143
144 /* If we were given a second-stage handler, call it. */
145 if (protocol->local) {
146 handler = ((void (*)(struct iaddr, u_int8_t *, int))
147 protocol->local);
148 memcpy(ia.iabuf, &from.sin_addr, sizeof from.sin_addr);
149 ia.len = sizeof from.sin_addr;
150 (*handler)(ia, icbuf, len);
151 }
152 }
153