1 /* $OpenBSD: hce.c,v 1.83 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>
33 void hce_init(struct privsep *, struct privsep_proc *p, void *);
34 void hce_launch_checks(int, short, void *);
35 void hce_setup_events(void);
36 void hce_disable_events(void);
38 int hce_dispatch_parent(int, struct privsep_proc *, struct imsg *);
39 int hce_dispatch_pfe(int, struct privsep_proc *, struct imsg *);
40 int hce_dispatch_relay(int, struct privsep_proc *, struct imsg *);
42 static struct relayd *env = NULL;
45 static struct privsep_proc procs[] = {
46 { "parent", PROC_PARENT, hce_dispatch_parent },
47 { "pfe", PROC_PFE, hce_dispatch_pfe },
48 { "relay", PROC_RELAY, hce_dispatch_relay },
52 hce(struct privsep *ps, struct privsep_proc *p)
56 /* this is needed for icmp tests */
59 proc_run(ps, p, procs, nitems(procs), hce_init, NULL);
63 hce_init(struct privsep *ps, struct privsep_proc *p, void *arg)
65 if (config_init(ps->ps_env) == -1)
66 fatal("failed to initialize configuration");
68 env->sc_id = getpid() & 0xffff;
70 /* Allow maximum available sockets for TCP checks */
73 if (pledge("stdio recvfd inet", NULL) == -1)
74 fatal("%s: pledge", __func__);
78 hce_setup_events(void)
83 if (!event_initialized(&env->sc_ev)) {
84 evtimer_set(&env->sc_ev, hce_launch_checks, env);
85 bzero(&tv, sizeof(tv));
86 evtimer_add(&env->sc_ev, &tv);
89 if (env->sc_conf.flags & F_TLS) {
90 TAILQ_FOREACH(table, env->sc_tables, entry) {
91 if (!(table->conf.flags & F_TLS) ||
92 table->tls_cfg != NULL)
94 table->tls_cfg = tls_config_new();
95 if (table->tls_cfg == NULL)
96 fatalx("%s: tls_config_new", __func__);
97 tls_config_insecure_noverifycert(table->tls_cfg);
98 tls_config_insecure_noverifyname(table->tls_cfg);
104 hce_disable_events(void)
109 evtimer_del(&env->sc_ev);
110 TAILQ_FOREACH(table, env->sc_tables, entry) {
111 TAILQ_FOREACH(host, &table->hosts, entry) {
112 host->he = HCE_ABORT;
113 if (event_initialized(&host->cte.ev)) {
114 event_del(&host->cte.ev);
119 if (env->sc_has_icmp) {
120 event_del(&env->sc_icmp_send.ev);
121 event_del(&env->sc_icmp_recv.ev);
123 if (env->sc_has_icmp6) {
124 event_del(&env->sc_icmp6_send.ev);
125 event_del(&env->sc_icmp6_recv.ev);
130 hce_launch_checks(int fd, short event, void *arg)
137 * notify pfe checks are done and schedule next check
139 proc_compose(env->sc_ps, PROC_PFE, IMSG_SYNC, NULL, 0);
140 TAILQ_FOREACH(table, env->sc_tables, entry) {
141 TAILQ_FOREACH(host, &table->hosts, entry) {
142 if ((host->flags & F_CHECK_DONE) == 0)
143 host->he = HCE_INTERVAL_TIMEOUT;
144 if (event_initialized(&host->cte.ev)) {
145 event_del(&host->cte.ev);
154 TAILQ_FOREACH(table, env->sc_tables, entry) {
155 if (table->conf.flags & F_DISABLE)
157 if (table->conf.skip_cnt) {
158 if (table->skipped++ > table->conf.skip_cnt)
160 if (table->skipped != 1)
163 if (table->conf.check == CHECK_NOCHECK)
164 fatalx("%s: unknown check type", __func__);
166 TAILQ_FOREACH(host, &table->hosts, entry) {
167 if (host->flags & F_DISABLE || host->conf.parentid)
169 bcopy(&tv, &host->cte.tv_start,
170 sizeof(host->cte.tv_start));
171 switch (table->conf.check) {
173 schedule_icmp(env, host);
176 check_script(env, host);
179 /* Any other TCP-style checks */
180 host->last_up = host->up;
181 host->cte.host = host;
182 host->cte.table = table;
183 check_tcp(&host->cte);
188 check_icmp(env, &tv);
190 bcopy(&env->sc_conf.interval, &tv, sizeof(tv));
191 evtimer_add(&env->sc_ev, &tv);
195 hce_notify_done(struct host *host, enum host_error he)
198 struct ctl_status st;
199 struct timeval tv_now, tv_dur;
201 u_int logopt = RELAYD_OPT_LOGHOSTCHECK;
202 struct host *h, *hostnst;
205 char *codemsg = NULL;
207 if ((hostnst = host_find(env, host->conf.id)) == NULL)
208 fatalx("%s: desynchronized", __func__);
210 if ((table = table_find(env, host->conf.tableid)) == NULL)
211 fatalx("%s: invalid table id", __func__);
213 if (hostnst->flags & F_DISABLE) {
214 if (env->sc_conf.opts & RELAYD_OPT_LOGUPDATE) {
215 log_info("host %s, check %s%s (ignoring result, "
217 host->conf.name, table_check(table->conf.check),
218 (table->conf.flags & F_TLS) ? " use tls" : "");
220 host->flags |= (F_CHECK_SENT|F_CHECK_DONE);
227 if (host->up == HOST_DOWN && host->retry_cnt) {
228 log_debug("%s: host %s retry %d", __func__,
229 host->conf.name, host->retry_cnt);
230 host->up = host->last_up;
233 host->retry_cnt = host->conf.retry;
234 if (host->up != HOST_UNKNOWN) {
236 if (host->up == HOST_UP)
239 st.id = host->conf.id;
241 st.check_cnt = host->check_cnt;
242 st.retry_cnt = host->retry_cnt;
244 host->flags |= (F_CHECK_SENT|F_CHECK_DONE);
245 msg = host_error(he);
247 log_debug("%s: %s (%s)", __func__, host->conf.name, msg);
249 proc_compose(env->sc_ps, PROC_PFE, IMSG_HOST_STATUS, &st, sizeof(st));
250 if (host->up != host->last_up)
251 logopt = RELAYD_OPT_LOGUPDATE;
253 getmonotime(&tv_now);
254 timersub(&tv_now, &host->cte.tv_start, &tv_dur);
255 if (timercmp(&host->cte.tv_start, &tv_dur, >))
256 duration = (tv_dur.tv_sec * 1000) + (tv_dur.tv_usec / 1000.0);
260 if (env->sc_conf.opts & logopt) {
262 asprintf(&codemsg, ",%d", host->code);
263 log_info("host %s, check %s%s (%lums,%s%s), state %s -> %s, "
265 host->conf.name, table_check(table->conf.check),
266 (table->conf.flags & F_TLS) ? " use tls" : "", duration,
267 msg, (codemsg != NULL) ? codemsg : "",
268 host_status(host->last_up), host_status(host->up),
269 print_availability(host->check_cnt, host->up_cnt));
273 host->last_up = host->up;
275 if (SLIST_EMPTY(&host->children))
278 /* Notify for all other hosts that inherit the state from this one */
279 SLIST_FOREACH(h, &host->children, child) {
281 hce_notify_done(h, he);
286 hce_dispatch_pfe(int fd, struct privsep_proc *p, struct imsg *imsg)
292 switch (imsg->hdr.type) {
293 case IMSG_HOST_DISABLE:
294 memcpy(&id, imsg->data, sizeof(id));
295 if ((host = host_find(env, id)) == NULL)
296 fatalx("%s: desynchronized", __func__);
297 host->flags |= F_DISABLE;
298 host->up = HOST_UNKNOWN;
303 case IMSG_HOST_ENABLE:
304 memcpy(&id, imsg->data, sizeof(id));
305 if ((host = host_find(env, id)) == NULL)
306 fatalx("%s: desynchronized", __func__);
307 host->flags &= ~(F_DISABLE);
308 host->up = HOST_UNKNOWN;
311 case IMSG_TABLE_DISABLE:
312 memcpy(&id, imsg->data, sizeof(id));
313 if ((table = table_find(env, id)) == NULL)
314 fatalx("%s: desynchronized", __func__);
315 table->conf.flags |= F_DISABLE;
316 TAILQ_FOREACH(host, &table->hosts, entry)
317 host->up = HOST_UNKNOWN;
319 case IMSG_TABLE_ENABLE:
320 memcpy(&id, imsg->data, sizeof(id));
321 if ((table = table_find(env, id)) == NULL)
322 fatalx("%s: desynchronized", __func__);
323 table->conf.flags &= ~(F_DISABLE);
324 TAILQ_FOREACH(host, &table->hosts, entry)
325 host->up = HOST_UNKNOWN;
328 evtimer_del(&env->sc_ev);
329 TAILQ_FOREACH(table, env->sc_tables, entry)
331 hce_launch_checks(-1, EV_TIMEOUT, env);
341 hce_dispatch_parent(int fd, struct privsep_proc *p, struct imsg *imsg)
343 struct ctl_script scr;
345 switch (imsg->hdr.type) {
347 IMSG_SIZE_CHECK(imsg, &scr);
348 bcopy(imsg->data, &scr, sizeof(scr));
349 script_done(env, &scr);
352 config_gettable(env, imsg);
355 config_gethost(env, imsg);
358 config_getcfg(env, imsg);
364 config_getreset(env, imsg);
374 hce_dispatch_relay(int fd, struct privsep_proc *p, struct imsg *imsg)
376 switch (imsg->hdr.type) {