1 6b5c4000 2026-03-02 rsadowski /* $OpenBSD: hce.c,v 1.83 2026/03/02 19:28:01 rsadowski Exp $ */
4 8c736326 2007-09-28 pyr * Copyright (c) 2006 Pierre-Yves Ritschard <pyr@openbsd.org>
6 41042ecc 2006-12-16 reyk * Permission to use, copy, modify, and distribute this software for any
7 41042ecc 2006-12-16 reyk * purpose with or without fee is hereby granted, provided that the above
8 41042ecc 2006-12-16 reyk * copyright notice and this permission notice appear in all copies.
10 41042ecc 2006-12-16 reyk * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 41042ecc 2006-12-16 reyk * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 41042ecc 2006-12-16 reyk * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 41042ecc 2006-12-16 reyk * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 41042ecc 2006-12-16 reyk * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 41042ecc 2006-12-16 reyk * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 41042ecc 2006-12-16 reyk * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 d93c36af 2015-01-22 reyk #include <sys/types.h>
20 e1f9d231 2007-11-24 reyk #include <sys/queue.h>
21 41042ecc 2006-12-16 reyk #include <sys/time.h>
22 d93c36af 2015-01-22 reyk #include <sys/uio.h>
24 41042ecc 2006-12-16 reyk #include <event.h>
25 41042ecc 2006-12-16 reyk #include <stdlib.h>
26 41042ecc 2006-12-16 reyk #include <string.h>
27 41042ecc 2006-12-16 reyk #include <unistd.h>
28 d93c36af 2015-01-22 reyk #include <imsg.h>
30 6d0767ae 2007-12-07 reyk #include "relayd.h"
31 6b5c4000 2026-03-02 rsadowski #include "log.h"
33 dbef7d4f 2011-05-09 reyk void hce_init(struct privsep *, struct privsep_proc *p, void *);
34 dbef7d4f 2011-05-09 reyk void hce_launch_checks(int, short, void *);
35 dbef7d4f 2011-05-09 reyk void hce_setup_events(void);
36 dbef7d4f 2011-05-09 reyk void hce_disable_events(void);
38 dbef7d4f 2011-05-09 reyk int hce_dispatch_parent(int, struct privsep_proc *, struct imsg *);
39 dbef7d4f 2011-05-09 reyk int hce_dispatch_pfe(int, struct privsep_proc *, struct imsg *);
40 28bd28ce 2014-11-19 blambert int hce_dispatch_relay(int, struct privsep_proc *, struct imsg *);
42 6d0767ae 2007-12-07 reyk static struct relayd *env = NULL;
43 c9f38bc1 2007-05-31 pyr int running = 0;
45 dbef7d4f 2011-05-09 reyk static struct privsep_proc procs[] = {
46 dbef7d4f 2011-05-09 reyk { "parent", PROC_PARENT, hce_dispatch_parent },
47 dbef7d4f 2011-05-09 reyk { "pfe", PROC_PFE, hce_dispatch_pfe },
48 28bd28ce 2014-11-19 blambert { "relay", PROC_RELAY, hce_dispatch_relay },
52 dbef7d4f 2011-05-09 reyk hce(struct privsep *ps, struct privsep_proc *p)
54 dbef7d4f 2011-05-09 reyk env = ps->ps_env;
56 816ddb53 2007-01-11 reyk /* this is needed for icmp tests */
57 816ddb53 2007-01-11 reyk icmp_init(env);
59 94404df8 2016-09-02 reyk proc_run(ps, p, procs, nitems(procs), hce_init, NULL);
63 dbef7d4f 2011-05-09 reyk hce_init(struct privsep *ps, struct privsep_proc *p, void *arg)
65 591e6ab5 2011-05-19 reyk if (config_init(ps->ps_env) == -1)
66 591e6ab5 2011-05-19 reyk fatal("failed to initialize configuration");
68 dbef7d4f 2011-05-09 reyk env->sc_id = getpid() & 0xffff;
70 ea47f1c4 2010-11-30 reyk /* Allow maximum available sockets for TCP checks */
71 ea47f1c4 2010-11-30 reyk socket_rlimit(-1);
73 41ddf20a 2016-09-03 reyk if (pledge("stdio recvfd inet", NULL) == -1)
74 09e53fee 2017-05-28 benno fatal("%s: pledge", __func__);
78 0b3f2e39 2007-05-31 pyr hce_setup_events(void)
80 c9f38bc1 2007-05-31 pyr struct timeval tv;
81 c9f38bc1 2007-05-31 pyr struct table *table;
83 ebb0dc76 2017-12-18 benno if (!event_initialized(&env->sc_ev)) {
84 23fba4ad 2008-01-31 reyk evtimer_set(&env->sc_ev, hce_launch_checks, env);
85 30640247 2007-02-22 reyk bzero(&tv, sizeof(tv));
86 23fba4ad 2008-01-31 reyk evtimer_add(&env->sc_ev, &tv);
89 58fc320c 2016-09-02 reyk if (env->sc_conf.flags & F_TLS) {
90 23fba4ad 2008-01-31 reyk TAILQ_FOREACH(table, env->sc_tables, entry) {
91 096ff28b 2014-12-12 reyk if (!(table->conf.flags & F_TLS) ||
92 f4a6eef3 2017-05-27 claudio table->tls_cfg != NULL)
94 f4a6eef3 2017-05-27 claudio table->tls_cfg = tls_config_new();
95 277590d4 2022-06-03 tb if (table->tls_cfg == NULL)
96 277590d4 2022-06-03 tb fatalx("%s: tls_config_new", __func__);
97 f4a6eef3 2017-05-27 claudio tls_config_insecure_noverifycert(table->tls_cfg);
98 f4a6eef3 2017-05-27 claudio tls_config_insecure_noverifyname(table->tls_cfg);
104 0b3f2e39 2007-05-31 pyr hce_disable_events(void)
106 0b3f2e39 2007-05-31 pyr struct table *table;
107 0b3f2e39 2007-05-31 pyr struct host *host;
109 23fba4ad 2008-01-31 reyk evtimer_del(&env->sc_ev);
110 23fba4ad 2008-01-31 reyk TAILQ_FOREACH(table, env->sc_tables, entry) {
111 0b3f2e39 2007-05-31 pyr TAILQ_FOREACH(host, &table->hosts, entry) {
112 b2665266 2008-12-05 reyk host->he = HCE_ABORT;
113 591e6ab5 2011-05-19 reyk if (event_initialized(&host->cte.ev)) {
114 591e6ab5 2011-05-19 reyk event_del(&host->cte.ev);
115 591e6ab5 2011-05-19 reyk close(host->cte.s);
119 23fba4ad 2008-01-31 reyk if (env->sc_has_icmp) {
120 23fba4ad 2008-01-31 reyk event_del(&env->sc_icmp_send.ev);
121 23fba4ad 2008-01-31 reyk event_del(&env->sc_icmp_recv.ev);
123 23fba4ad 2008-01-31 reyk if (env->sc_has_icmp6) {
124 23fba4ad 2008-01-31 reyk event_del(&env->sc_icmp6_send.ev);
125 23fba4ad 2008-01-31 reyk event_del(&env->sc_icmp6_recv.ev);
130 41042ecc 2006-12-16 reyk hce_launch_checks(int fd, short event, void *arg)
132 4ab45c57 2006-12-16 reyk struct host *host;
133 4ab45c57 2006-12-16 reyk struct table *table;
134 816ddb53 2007-01-11 reyk struct timeval tv;
137 816ddb53 2007-01-11 reyk * notify pfe checks are done and schedule next check
139 141b9058 2015-12-02 reyk proc_compose(env->sc_ps, PROC_PFE, IMSG_SYNC, NULL, 0);
140 23fba4ad 2008-01-31 reyk TAILQ_FOREACH(table, env->sc_tables, entry) {
141 816ddb53 2007-01-11 reyk TAILQ_FOREACH(host, &table->hosts, entry) {
142 b2665266 2008-12-05 reyk if ((host->flags & F_CHECK_DONE) == 0)
143 b2665266 2008-12-05 reyk host->he = HCE_INTERVAL_TIMEOUT;
144 591e6ab5 2011-05-19 reyk if (event_initialized(&host->cte.ev)) {
145 591e6ab5 2011-05-19 reyk event_del(&host->cte.ev);
146 591e6ab5 2011-05-19 reyk close(host->cte.s);
148 591e6ab5 2011-05-19 reyk host->cte.s = -1;
152 b7f7a948 2013-03-10 reyk getmonotime(&tv);
154 23fba4ad 2008-01-31 reyk TAILQ_FOREACH(table, env->sc_tables, entry) {
155 87178699 2007-05-27 pyr if (table->conf.flags & F_DISABLE)
157 182ad205 2007-11-20 pyr if (table->conf.skip_cnt) {
158 182ad205 2007-11-20 pyr if (table->skipped++ > table->conf.skip_cnt)
159 182ad205 2007-11-20 pyr table->skipped = 0;
160 182ad205 2007-11-20 pyr if (table->skipped != 1)
163 87178699 2007-05-27 pyr if (table->conf.check == CHECK_NOCHECK)
164 09e53fee 2017-05-28 benno fatalx("%s: unknown check type", __func__);
166 ae98db81 2006-12-25 reyk TAILQ_FOREACH(host, &table->hosts, entry) {
167 17acabee 2008-07-19 reyk if (host->flags & F_DISABLE || host->conf.parentid)
169 27026d2a 2011-02-08 sthen bcopy(&tv, &host->cte.tv_start,
170 27026d2a 2011-02-08 sthen sizeof(host->cte.tv_start));
171 292100ef 2007-05-29 reyk switch (table->conf.check) {
172 292100ef 2007-05-29 reyk case CHECK_ICMP:
173 816ddb53 2007-01-11 reyk schedule_icmp(env, host);
175 292100ef 2007-05-29 reyk case CHECK_SCRIPT:
176 dbef7d4f 2011-05-09 reyk check_script(env, host);
179 292100ef 2007-05-29 reyk /* Any other TCP-style checks */
180 292100ef 2007-05-29 reyk host->last_up = host->up;
181 292100ef 2007-05-29 reyk host->cte.host = host;
182 292100ef 2007-05-29 reyk host->cte.table = table;
183 292100ef 2007-05-29 reyk check_tcp(&host->cte);
188 816ddb53 2007-01-11 reyk check_icmp(env, &tv);
190 58fc320c 2016-09-02 reyk bcopy(&env->sc_conf.interval, &tv, sizeof(tv));
191 23fba4ad 2008-01-31 reyk evtimer_add(&env->sc_ev, &tv);
195 b2665266 2008-12-05 reyk hce_notify_done(struct host *host, enum host_error he)
197 f802d393 2007-02-07 reyk struct table *table;
198 41042ecc 2006-12-16 reyk struct ctl_status st;
199 f802d393 2007-02-07 reyk struct timeval tv_now, tv_dur;
200 f802d393 2007-02-07 reyk u_long duration;
201 37ed6014 2018-08-06 benno u_int logopt = RELAYD_OPT_LOGHOSTCHECK;
202 8680b599 2012-05-09 giovanni struct host *h, *hostnst;
203 17acabee 2008-07-19 reyk int hostup;
204 b2665266 2008-12-05 reyk const char *msg;
205 e7687e91 2016-01-11 benno char *codemsg = NULL;
207 8680b599 2012-05-09 giovanni if ((hostnst = host_find(env, host->conf.id)) == NULL)
208 09e53fee 2017-05-28 benno fatalx("%s: desynchronized", __func__);
210 8680b599 2012-05-09 giovanni if ((table = table_find(env, host->conf.tableid)) == NULL)
211 09e53fee 2017-05-28 benno fatalx("%s: invalid table id", __func__);
213 8680b599 2012-05-09 giovanni if (hostnst->flags & F_DISABLE) {
214 58fc320c 2016-09-02 reyk if (env->sc_conf.opts & RELAYD_OPT_LOGUPDATE) {
215 8680b599 2012-05-09 giovanni log_info("host %s, check %s%s (ignoring result, "
216 8680b599 2012-05-09 giovanni "host disabled)",
217 8680b599 2012-05-09 giovanni host->conf.name, table_check(table->conf.check),
218 096ff28b 2014-12-12 reyk (table->conf.flags & F_TLS) ? " use tls" : "");
220 8680b599 2012-05-09 giovanni host->flags |= (F_CHECK_SENT|F_CHECK_DONE);
221 8680b599 2012-05-09 giovanni return;
224 17acabee 2008-07-19 reyk hostup = host->up;
225 b2665266 2008-12-05 reyk host->he = he;
227 30640247 2007-02-22 reyk if (host->up == HOST_DOWN && host->retry_cnt) {
228 666bd316 2011-05-05 reyk log_debug("%s: host %s retry %d", __func__,
229 87178699 2007-05-27 pyr host->conf.name, host->retry_cnt);
230 fc15ca4a 2007-03-06 reyk host->up = host->last_up;
231 30640247 2007-02-22 reyk host->retry_cnt--;
233 87178699 2007-05-27 pyr host->retry_cnt = host->conf.retry;
234 30640247 2007-02-22 reyk if (host->up != HOST_UNKNOWN) {
235 30640247 2007-02-22 reyk host->check_cnt++;
236 30640247 2007-02-22 reyk if (host->up == HOST_UP)
237 30640247 2007-02-22 reyk host->up_cnt++;
239 87178699 2007-05-27 pyr st.id = host->conf.id;
240 ae98db81 2006-12-25 reyk st.up = host->up;
241 30640247 2007-02-22 reyk st.check_cnt = host->check_cnt;
242 30640247 2007-02-22 reyk st.retry_cnt = host->retry_cnt;
243 b2665266 2008-12-05 reyk st.he = he;
244 816ddb53 2007-01-11 reyk host->flags |= (F_CHECK_SENT|F_CHECK_DONE);
245 b2665266 2008-12-05 reyk msg = host_error(he);
247 666bd316 2011-05-05 reyk log_debug("%s: %s (%s)", __func__, host->conf.name, msg);
249 141b9058 2015-12-02 reyk proc_compose(env->sc_ps, PROC_PFE, IMSG_HOST_STATUS, &st, sizeof(st));
250 30640247 2007-02-22 reyk if (host->up != host->last_up)
251 6d0767ae 2007-12-07 reyk logopt = RELAYD_OPT_LOGUPDATE;
253 b7f7a948 2013-03-10 reyk getmonotime(&tv_now);
254 f802d393 2007-02-07 reyk timersub(&tv_now, &host->cte.tv_start, &tv_dur);
255 f802d393 2007-02-07 reyk if (timercmp(&host->cte.tv_start, &tv_dur, >))
256 f802d393 2007-02-07 reyk duration = (tv_dur.tv_sec * 1000) + (tv_dur.tv_usec / 1000.0);
258 f802d393 2007-02-07 reyk duration = 0;
260 58fc320c 2016-09-02 reyk if (env->sc_conf.opts & logopt) {
261 e7687e91 2016-01-11 benno if (host->code > 0)
262 e7687e91 2016-01-11 benno asprintf(&codemsg, ",%d", host->code);
263 e7687e91 2016-01-11 benno log_info("host %s, check %s%s (%lums,%s%s), state %s -> %s, "
264 30640247 2007-02-22 reyk "availability %s",
265 87178699 2007-05-27 pyr host->conf.name, table_check(table->conf.check),
266 096ff28b 2014-12-12 reyk (table->conf.flags & F_TLS) ? " use tls" : "", duration,
267 e7687e91 2016-01-11 benno msg, (codemsg != NULL) ? codemsg : "",
268 30640247 2007-02-22 reyk host_status(host->last_up), host_status(host->up),
269 30640247 2007-02-22 reyk print_availability(host->check_cnt, host->up_cnt));
270 e7687e91 2016-01-11 benno free(codemsg);
273 f802d393 2007-02-07 reyk host->last_up = host->up;
275 aebf7fd4 2008-07-19 reyk if (SLIST_EMPTY(&host->children))
278 17acabee 2008-07-19 reyk /* Notify for all other hosts that inherit the state from this one */
279 aebf7fd4 2008-07-19 reyk SLIST_FOREACH(h, &host->children, child) {
280 e8962295 2008-09-29 reyk h->up = hostup;
281 b2665266 2008-12-05 reyk hce_notify_done(h, he);
286 dbef7d4f 2011-05-09 reyk hce_dispatch_pfe(int fd, struct privsep_proc *p, struct imsg *imsg)
288 41042ecc 2006-12-16 reyk objid_t id;
289 41042ecc 2006-12-16 reyk struct host *host;
290 41042ecc 2006-12-16 reyk struct table *table;
292 dbef7d4f 2011-05-09 reyk switch (imsg->hdr.type) {
293 dbef7d4f 2011-05-09 reyk case IMSG_HOST_DISABLE:
294 dbef7d4f 2011-05-09 reyk memcpy(&id, imsg->data, sizeof(id));
295 dbef7d4f 2011-05-09 reyk if ((host = host_find(env, id)) == NULL)
296 09e53fee 2017-05-28 benno fatalx("%s: desynchronized", __func__);
297 dbef7d4f 2011-05-09 reyk host->flags |= F_DISABLE;
298 dbef7d4f 2011-05-09 reyk host->up = HOST_UNKNOWN;
299 dbef7d4f 2011-05-09 reyk host->check_cnt = 0;
300 dbef7d4f 2011-05-09 reyk host->up_cnt = 0;
301 dbef7d4f 2011-05-09 reyk host->he = HCE_NONE;
303 dbef7d4f 2011-05-09 reyk case IMSG_HOST_ENABLE:
304 dbef7d4f 2011-05-09 reyk memcpy(&id, imsg->data, sizeof(id));
305 dbef7d4f 2011-05-09 reyk if ((host = host_find(env, id)) == NULL)
306 09e53fee 2017-05-28 benno fatalx("%s: desynchronized", __func__);
307 dbef7d4f 2011-05-09 reyk host->flags &= ~(F_DISABLE);
308 dbef7d4f 2011-05-09 reyk host->up = HOST_UNKNOWN;
309 dbef7d4f 2011-05-09 reyk host->he = HCE_NONE;
311 dbef7d4f 2011-05-09 reyk case IMSG_TABLE_DISABLE:
312 dbef7d4f 2011-05-09 reyk memcpy(&id, imsg->data, sizeof(id));
313 dbef7d4f 2011-05-09 reyk if ((table = table_find(env, id)) == NULL)
314 09e53fee 2017-05-28 benno fatalx("%s: desynchronized", __func__);
315 dbef7d4f 2011-05-09 reyk table->conf.flags |= F_DISABLE;
316 dbef7d4f 2011-05-09 reyk TAILQ_FOREACH(host, &table->hosts, entry)
317 41042ecc 2006-12-16 reyk host->up = HOST_UNKNOWN;
319 dbef7d4f 2011-05-09 reyk case IMSG_TABLE_ENABLE:
320 dbef7d4f 2011-05-09 reyk memcpy(&id, imsg->data, sizeof(id));
321 dbef7d4f 2011-05-09 reyk if ((table = table_find(env, id)) == NULL)
322 09e53fee 2017-05-28 benno fatalx("%s: desynchronized", __func__);
323 dbef7d4f 2011-05-09 reyk table->conf.flags &= ~(F_DISABLE);
324 dbef7d4f 2011-05-09 reyk TAILQ_FOREACH(host, &table->hosts, entry)
325 41042ecc 2006-12-16 reyk host->up = HOST_UNKNOWN;
327 dbef7d4f 2011-05-09 reyk case IMSG_CTL_POLL:
328 dbef7d4f 2011-05-09 reyk evtimer_del(&env->sc_ev);
329 dbef7d4f 2011-05-09 reyk TAILQ_FOREACH(table, env->sc_tables, entry)
330 dbef7d4f 2011-05-09 reyk table->skipped = 0;
331 dbef7d4f 2011-05-09 reyk hce_launch_checks(-1, EV_TIMEOUT, env);
334 dbef7d4f 2011-05-09 reyk return (-1);
337 dbef7d4f 2011-05-09 reyk return (0);
341 dbef7d4f 2011-05-09 reyk hce_dispatch_parent(int fd, struct privsep_proc *p, struct imsg *imsg)
343 292100ef 2007-05-29 reyk struct ctl_script scr;
345 dbef7d4f 2011-05-09 reyk switch (imsg->hdr.type) {
346 dbef7d4f 2011-05-09 reyk case IMSG_SCRIPT:
347 dbef7d4f 2011-05-09 reyk IMSG_SIZE_CHECK(imsg, &scr);
348 dbef7d4f 2011-05-09 reyk bcopy(imsg->data, &scr, sizeof(scr));
349 dbef7d4f 2011-05-09 reyk script_done(env, &scr);
351 591e6ab5 2011-05-19 reyk case IMSG_CFG_TABLE:
352 591e6ab5 2011-05-19 reyk config_gettable(env, imsg);
354 591e6ab5 2011-05-19 reyk case IMSG_CFG_HOST:
355 591e6ab5 2011-05-19 reyk config_gethost(env, imsg);
357 591e6ab5 2011-05-19 reyk case IMSG_CFG_DONE:
358 591e6ab5 2011-05-19 reyk config_getcfg(env, imsg);
360 996c1f22 2012-01-21 camield case IMSG_CTL_START:
361 dbef7d4f 2011-05-09 reyk hce_setup_events();
363 591e6ab5 2011-05-19 reyk case IMSG_CTL_RESET:
364 591e6ab5 2011-05-19 reyk config_getreset(env, imsg);
367 dbef7d4f 2011-05-09 reyk return (-1);
370 dbef7d4f 2011-05-09 reyk return (0);
374 28bd28ce 2014-11-19 blambert hce_dispatch_relay(int fd, struct privsep_proc *p, struct imsg *imsg)
376 28bd28ce 2014-11-19 blambert switch (imsg->hdr.type) {
377 28bd28ce 2014-11-19 blambert default:
381 28bd28ce 2014-11-19 blambert return (-1);