1 /* $OpenBSD: check_icmp.c,v 1.49 2026/03/02 19:28:01 rsadowski Exp $ */
4 * Copyright (c) 2006 Pierre-Yves Ritschard <pyr@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
20 #include <sys/queue.h>
21 #include <sys/socket.h>
22 #include <sys/sysctl.h>
25 #include <netinet/in.h>
26 #include <netinet/ip.h>
27 #include <netinet/ip_icmp.h>
28 #include <netinet/icmp6.h>
29 #include <arpa/inet.h>
40 void icmp_setup(struct relayd *, struct ctl_icmp_event *, int);
41 void check_icmp_add(struct ctl_icmp_event *, int, struct timeval *,
42 void (*)(int, short, void *));
43 int icmp_checks_done(struct ctl_icmp_event *);
44 void icmp_checks_timeout(struct ctl_icmp_event *, enum host_error);
45 void send_icmp(int, short, void *);
46 void recv_icmp(int, short, void *);
47 int in_cksum(u_short *, int);
50 icmp_setup(struct relayd *env, struct ctl_icmp_event *cie, int af)
52 int proto = IPPROTO_ICMP, val;
55 proto = IPPROTO_ICMPV6;
56 if ((cie->s = socket(af, SOCK_RAW | SOCK_NONBLOCK, proto)) == -1)
57 fatal("%s: socket", __func__);
58 val = ICMP_RCVBUF_SIZE;
59 if (setsockopt(cie->s, SOL_SOCKET, SO_RCVBUF, &val, sizeof(val)) == -1)
60 fatal("%s: setsockopt", __func__);
66 icmp_init(struct relayd *env)
68 icmp_setup(env, &env->sc_icmp_send, AF_INET);
69 icmp_setup(env, &env->sc_icmp_recv, AF_INET);
70 icmp_setup(env, &env->sc_icmp6_send, AF_INET6);
71 icmp_setup(env, &env->sc_icmp6_recv, AF_INET6);
72 env->sc_id = getpid() & 0xffff;
76 schedule_icmp(struct relayd *env, struct host *host)
78 host->last_up = host->up;
79 host->flags &= ~(F_CHECK_SENT|F_CHECK_DONE);
81 if (((struct sockaddr *)&host->conf.ss)->sa_family == AF_INET)
84 env->sc_has_icmp6 = 1;
88 check_icmp_add(struct ctl_icmp_event *cie, int flags, struct timeval *start,
89 void (*fn)(int, short, void *))
94 bcopy(start, &cie->tv_start, sizeof(cie->tv_start));
95 bcopy(&cie->env->sc_conf.timeout, &tv, sizeof(tv));
96 getmonotime(&cie->tv_start);
98 event_set(&cie->ev, cie->s, EV_TIMEOUT|flags, fn, cie);
99 event_add(&cie->ev, &tv);
103 check_icmp(struct relayd *env, struct timeval *tv)
105 if (env->sc_has_icmp) {
106 check_icmp_add(&env->sc_icmp_recv, EV_READ, tv, recv_icmp);
107 check_icmp_add(&env->sc_icmp_send, EV_WRITE, tv, send_icmp);
109 if (env->sc_has_icmp6) {
110 check_icmp_add(&env->sc_icmp6_recv, EV_READ, tv, recv_icmp);
111 check_icmp_add(&env->sc_icmp6_send, EV_WRITE, tv, send_icmp);
116 icmp_checks_done(struct ctl_icmp_event *cie)
121 TAILQ_FOREACH(table, cie->env->sc_tables, entry) {
122 if (table->conf.flags & F_DISABLE ||
123 table->conf.check != CHECK_ICMP)
125 TAILQ_FOREACH(host, &table->hosts, entry) {
126 if (((struct sockaddr *)&host->conf.ss)->sa_family !=
129 if (!(host->flags & F_CHECK_DONE))
137 icmp_checks_timeout(struct ctl_icmp_event *cie, enum host_error he)
142 TAILQ_FOREACH(table, cie->env->sc_tables, entry) {
143 if (table->conf.flags & F_DISABLE ||
144 table->conf.check != CHECK_ICMP)
146 TAILQ_FOREACH(host, &table->hosts, entry) {
147 if (((struct sockaddr *)&host->conf.ss)->sa_family !=
150 if (!(host->flags & (F_CHECK_DONE|F_DISABLE))) {
151 host->up = HOST_DOWN;
152 hce_notify_done(host, he);
159 send_icmp(int s, short event, void *arg)
161 struct ctl_icmp_event *cie = arg;
166 struct icmp6_hdr *icp6;
168 u_char packet[ICMP_BUF_SIZE];
173 if (event == EV_TIMEOUT) {
174 icmp_checks_timeout(cie, HCE_ICMP_WRITE_TIMEOUT);
178 bzero(&packet, sizeof(packet));
179 icp = (struct icmp *)packet;
180 icp6 = (struct icmp6_hdr *)packet;
181 if (cie->af == AF_INET) {
182 icp->icmp_type = ICMP_ECHO;
184 icp->icmp_id = htons(cie->env->sc_id);
186 slen = sizeof(struct sockaddr_in);
188 icp6->icmp6_type = ICMP6_ECHO_REQUEST;
189 icp6->icmp6_code = 0;
190 icp6->icmp6_cksum = 0;
191 icp6->icmp6_id = htons(cie->env->sc_id);
192 slen = sizeof(struct sockaddr_in6);
195 TAILQ_FOREACH(table, cie->env->sc_tables, entry) {
196 if (table->conf.check != CHECK_ICMP ||
197 table->conf.flags & F_DISABLE)
199 TAILQ_FOREACH(host, &table->hosts, entry) {
200 if (host->flags & (F_DISABLE | F_CHECK_SENT) ||
203 if (((struct sockaddr *)&host->conf.ss)->sa_family !=
207 to = (struct sockaddr *)&host->conf.ss;
208 id = htonl(host->conf.id);
210 if (cie->af == AF_INET) {
211 icp->icmp_seq = htons(i);
214 icp->icmp_cksum = in_cksum((u_short *)icp,
217 icp6->icmp6_seq = htons(i);
218 icp6->icmp6_cksum = 0;
219 memcpy(packet + sizeof(*icp6), &id, sizeof(id));
220 icp6->icmp6_cksum = in_cksum((u_short *)icp6,
224 ttl = host->conf.ttl;
228 if (setsockopt(s, IPPROTO_IP, IP_TTL,
229 &ttl, sizeof(ttl)) == -1)
230 log_warn("%s: setsockopt",
233 /* Revert to default TTL */
235 if (getsockopt(s, IPPROTO_IP,
236 IP_IPDEFTTL, &ttl, &len) == 0) {
237 if (setsockopt(s, IPPROTO_IP,
238 IP_TTL, &ttl, len) == -1)
243 log_warn("%s: getsockopt",
249 if (setsockopt(s, IPPROTO_IPV6,
250 IPV6_UNICAST_HOPS, &ttl,
252 log_warn("%s: setsockopt",
255 /* Revert to default hop limit */
257 if (setsockopt(s, IPPROTO_IPV6,
258 IPV6_UNICAST_HOPS, &ttl,
260 log_warn("%s: setsockopt",
266 r = sendto(s, packet, sizeof(packet), 0, to, slen);
268 if (errno == EAGAIN || errno == EINTR)
270 host->flags |= F_CHECK_SENT|F_CHECK_DONE;
271 host->up = HOST_DOWN;
272 } else if (r != sizeof(packet))
274 host->flags |= F_CHECK_SENT;
281 event_again(&cie->ev, s, EV_TIMEOUT|EV_WRITE, send_icmp,
282 &cie->tv_start, &cie->env->sc_conf.timeout, cie);
286 recv_icmp(int s, short event, void *arg)
288 struct ctl_icmp_event *cie = arg;
289 u_char packet[ICMP_BUF_SIZE];
291 struct sockaddr_storage ss;
293 struct icmp6_hdr *icp6;
299 if (event == EV_TIMEOUT) {
300 icmp_checks_timeout(cie, HCE_ICMP_READ_TIMEOUT);
304 bzero(&packet, sizeof(packet));
305 bzero(&ss, sizeof(ss));
308 r = recvfrom(s, packet, sizeof(packet), 0,
309 (struct sockaddr *)&ss, &slen);
310 if (r == -1 || r != ICMP_BUF_SIZE) {
311 if (r == -1 && errno != EAGAIN && errno != EINTR)
312 log_debug("%s: receive error", __func__);
316 if (cie->af == AF_INET) {
317 icp = (struct icmp *)(packet + sizeof(struct ip));
318 icpid = ntohs(icp->icmp_id);
321 icp6 = (struct icmp6_hdr *)packet;
322 icpid = ntohs(icp6->icmp6_id);
323 memcpy(&id, packet + sizeof(*icp6), sizeof(id));
325 if (icpid != cie->env->sc_id)
328 host = host_find(cie->env, id);
330 log_warn("%s: ping for unknown host received", __func__);
333 if (bcmp(&ss, &host->conf.ss, slen)) {
334 log_warnx("%s: forged icmp packet?", __func__);
339 host->flags |= F_CHECK_DONE;
340 hce_notify_done(host, HCE_ICMP_OK);
342 if (icmp_checks_done(cie))
346 event_again(&cie->ev, s, EV_TIMEOUT|EV_READ, recv_icmp,
347 &cie->tv_start, &cie->env->sc_conf.timeout, cie);
350 /* in_cksum from ping.c --
351 * Checksum routine for Internet Protocol family headers (C Version)
353 * Copyright (c) 1989, 1993
354 * The Regents of the University of California. All rights reserved.
356 * This code is derived from software contributed to Berkeley by
359 * Redistribution and use in source and binary forms, with or without
360 * modification, are permitted provided that the following conditions
362 * 1. Redistributions of source code must retain the above copyright
363 * notice, this list of conditions and the following disclaimer.
364 * 2. Redistributions in binary form must reproduce the above copyright
365 * notice, this list of conditions and the following disclaimer in the
366 * documentation and/or other materials provided with the distribution.
367 * 3. Neither the name of the University nor the names of its contributors
368 * may be used to endorse or promote products derived from this software
369 * without specific prior written permission.
371 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
372 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
373 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
374 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
375 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
376 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
377 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
378 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
379 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
380 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
384 in_cksum(u_short *addr, int len)
392 * Our algorithm is simple, using a 32 bit accumulator (sum), we add
393 * sequential 16 bit words to it, and at the end, fold back all the
394 * carry bits from the top 16 bits into the lower 16 bits.
401 /* mop up an odd byte, if necessary */
403 *(u_char *)(&answer) = *(u_char *)w ;
407 /* add back carry outs from top 16 bits to low 16 bits */
408 sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
409 sum += (sum >> 16); /* add carry */
410 answer = ~sum; /* truncate to 16 bits */