1 /* $OpenBSD: hce.c,v 1.80 2021/02/22 01:24:59 jmatthew Exp $ */ 2 3 /* 4 * Copyright (c) 2006 Pierre-Yves Ritschard <pyr@openbsd.org> 5 * 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. 9 * 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. 17 */ 18 19 #include <sys/types.h> 20 #include <sys/queue.h> 21 #include <sys/time.h> 22 #include <sys/uio.h> 23 24 #include <event.h> 25 #include <stdlib.h> 26 #include <string.h> 27 #include <unistd.h> 28 #include <imsg.h> 29 30 #include "relayd.h" 31 32 void hce_init(struct privsep *, struct privsep_proc *p, void *); 33 void hce_sig_handler(int sig, short, void *); 34 void hce_launch_checks(int, short, void *); 35 void hce_setup_events(void); 36 void hce_disable_events(void); 37 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 *); 41 42 static struct relayd *env = NULL; 43 int running = 0; 44 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 }, 49 }; 50 51 void 52 hce(struct privsep *ps, struct privsep_proc *p) 53 { 54 env = ps->ps_env; 55 56 /* this is needed for icmp tests */ 57 icmp_init(env); 58 59 proc_run(ps, p, procs, nitems(procs), hce_init, NULL); 60 } 61 62 void 63 hce_init(struct privsep *ps, struct privsep_proc *p, void *arg) 64 { 65 if (config_init(ps->ps_env) == -1) 66 fatal("failed to initialize configuration"); 67 68 env->sc_id = getpid() & 0xffff; 69 70 /* Allow maximum available sockets for TCP checks */ 71 socket_rlimit(-1); 72 73 if (pledge("stdio recvfd inet", NULL) == -1) 74 fatal("%s: pledge", __func__); 75 } 76 77 void 78 hce_setup_events(void) 79 { 80 struct timeval tv; 81 struct table *table; 82 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); 87 } 88 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) 93 continue; 94 table->tls_cfg = tls_config_new(); 95 tls_config_insecure_noverifycert(table->tls_cfg); 96 tls_config_insecure_noverifyname(table->tls_cfg); 97 } 98 } 99 } 100 101 void 102 hce_disable_events(void) 103 { 104 struct table *table; 105 struct host *host; 106 107 evtimer_del(&env->sc_ev); 108 TAILQ_FOREACH(table, env->sc_tables, entry) { 109 TAILQ_FOREACH(host, &table->hosts, entry) { 110 host->he = HCE_ABORT; 111 if (event_initialized(&host->cte.ev)) { 112 event_del(&host->cte.ev); 113 close(host->cte.s); 114 } 115 } 116 } 117 if (env->sc_has_icmp) { 118 event_del(&env->sc_icmp_send.ev); 119 event_del(&env->sc_icmp_recv.ev); 120 } 121 if (env->sc_has_icmp6) { 122 event_del(&env->sc_icmp6_send.ev); 123 event_del(&env->sc_icmp6_recv.ev); 124 } 125 } 126 127 void 128 hce_launch_checks(int fd, short event, void *arg) 129 { 130 struct host *host; 131 struct table *table; 132 struct timeval tv; 133 134 /* 135 * notify pfe checks are done and schedule next check 136 */ 137 proc_compose(env->sc_ps, PROC_PFE, IMSG_SYNC, NULL, 0); 138 TAILQ_FOREACH(table, env->sc_tables, entry) { 139 TAILQ_FOREACH(host, &table->hosts, entry) { 140 if ((host->flags & F_CHECK_DONE) == 0) 141 host->he = HCE_INTERVAL_TIMEOUT; 142 if (event_initialized(&host->cte.ev)) { 143 event_del(&host->cte.ev); 144 close(host->cte.s); 145 } 146 host->cte.s = -1; 147 } 148 } 149 150 getmonotime(&tv); 151 152 TAILQ_FOREACH(table, env->sc_tables, entry) { 153 if (table->conf.flags & F_DISABLE) 154 continue; 155 if (table->conf.skip_cnt) { 156 if (table->skipped++ > table->conf.skip_cnt) 157 table->skipped = 0; 158 if (table->skipped != 1) 159 continue; 160 } 161 if (table->conf.check == CHECK_NOCHECK) 162 fatalx("%s: unknown check type", __func__); 163 164 TAILQ_FOREACH(host, &table->hosts, entry) { 165 if (host->flags & F_DISABLE || host->conf.parentid) 166 continue; 167 bcopy(&tv, &host->cte.tv_start, 168 sizeof(host->cte.tv_start)); 169 switch (table->conf.check) { 170 case CHECK_ICMP: 171 schedule_icmp(env, host); 172 break; 173 case CHECK_SCRIPT: 174 check_script(env, host); 175 break; 176 default: 177 /* Any other TCP-style checks */ 178 host->last_up = host->up; 179 host->cte.host = host; 180 host->cte.table = table; 181 check_tcp(&host->cte); 182 break; 183 } 184 } 185 } 186 check_icmp(env, &tv); 187 188 bcopy(&env->sc_conf.interval, &tv, sizeof(tv)); 189 evtimer_add(&env->sc_ev, &tv); 190 } 191 192 void 193 hce_notify_done(struct host *host, enum host_error he) 194 { 195 struct table *table; 196 struct ctl_status st; 197 struct timeval tv_now, tv_dur; 198 u_long duration; 199 u_int logopt = RELAYD_OPT_LOGHOSTCHECK; 200 struct host *h, *hostnst; 201 int hostup; 202 const char *msg; 203 char *codemsg = NULL; 204 205 if ((hostnst = host_find(env, host->conf.id)) == NULL) 206 fatalx("%s: desynchronized", __func__); 207 208 if ((table = table_find(env, host->conf.tableid)) == NULL) 209 fatalx("%s: invalid table id", __func__); 210 211 if (hostnst->flags & F_DISABLE) { 212 if (env->sc_conf.opts & RELAYD_OPT_LOGUPDATE) { 213 log_info("host %s, check %s%s (ignoring result, " 214 "host disabled)", 215 host->conf.name, table_check(table->conf.check), 216 (table->conf.flags & F_TLS) ? " use tls" : ""); 217 } 218 host->flags |= (F_CHECK_SENT|F_CHECK_DONE); 219 return; 220 } 221 222 hostup = host->up; 223 host->he = he; 224 225 if (host->up == HOST_DOWN && host->retry_cnt) { 226 log_debug("%s: host %s retry %d", __func__, 227 host->conf.name, host->retry_cnt); 228 host->up = host->last_up; 229 host->retry_cnt--; 230 } else 231 host->retry_cnt = host->conf.retry; 232 if (host->up != HOST_UNKNOWN) { 233 host->check_cnt++; 234 if (host->up == HOST_UP) 235 host->up_cnt++; 236 } 237 st.id = host->conf.id; 238 st.up = host->up; 239 st.check_cnt = host->check_cnt; 240 st.retry_cnt = host->retry_cnt; 241 st.he = he; 242 host->flags |= (F_CHECK_SENT|F_CHECK_DONE); 243 msg = host_error(he); 244 if (msg) 245 log_debug("%s: %s (%s)", __func__, host->conf.name, msg); 246 247 proc_compose(env->sc_ps, PROC_PFE, IMSG_HOST_STATUS, &st, sizeof(st)); 248 if (host->up != host->last_up) 249 logopt = RELAYD_OPT_LOGUPDATE; 250 251 getmonotime(&tv_now); 252 timersub(&tv_now, &host->cte.tv_start, &tv_dur); 253 if (timercmp(&host->cte.tv_start, &tv_dur, >)) 254 duration = (tv_dur.tv_sec * 1000) + (tv_dur.tv_usec / 1000.0); 255 else 256 duration = 0; 257 258 if (env->sc_conf.opts & logopt) { 259 if (host->code > 0) 260 asprintf(&codemsg, ",%d", host->code); 261 log_info("host %s, check %s%s (%lums,%s%s), state %s -> %s, " 262 "availability %s", 263 host->conf.name, table_check(table->conf.check), 264 (table->conf.flags & F_TLS) ? " use tls" : "", duration, 265 msg, (codemsg != NULL) ? codemsg : "", 266 host_status(host->last_up), host_status(host->up), 267 print_availability(host->check_cnt, host->up_cnt)); 268 free(codemsg); 269 } 270 271 host->last_up = host->up; 272 273 if (SLIST_EMPTY(&host->children)) 274 return; 275 276 /* Notify for all other hosts that inherit the state from this one */ 277 SLIST_FOREACH(h, &host->children, child) { 278 h->up = hostup; 279 hce_notify_done(h, he); 280 } 281 } 282 283 int 284 hce_dispatch_pfe(int fd, struct privsep_proc *p, struct imsg *imsg) 285 { 286 objid_t id; 287 struct host *host; 288 struct table *table; 289 290 switch (imsg->hdr.type) { 291 case IMSG_HOST_DISABLE: 292 memcpy(&id, imsg->data, sizeof(id)); 293 if ((host = host_find(env, id)) == NULL) 294 fatalx("%s: desynchronized", __func__); 295 host->flags |= F_DISABLE; 296 host->up = HOST_UNKNOWN; 297 host->check_cnt = 0; 298 host->up_cnt = 0; 299 host->he = HCE_NONE; 300 break; 301 case IMSG_HOST_ENABLE: 302 memcpy(&id, imsg->data, sizeof(id)); 303 if ((host = host_find(env, id)) == NULL) 304 fatalx("%s: desynchronized", __func__); 305 host->flags &= ~(F_DISABLE); 306 host->up = HOST_UNKNOWN; 307 host->he = HCE_NONE; 308 break; 309 case IMSG_TABLE_DISABLE: 310 memcpy(&id, imsg->data, sizeof(id)); 311 if ((table = table_find(env, id)) == NULL) 312 fatalx("%s: desynchronized", __func__); 313 table->conf.flags |= F_DISABLE; 314 TAILQ_FOREACH(host, &table->hosts, entry) 315 host->up = HOST_UNKNOWN; 316 break; 317 case IMSG_TABLE_ENABLE: 318 memcpy(&id, imsg->data, sizeof(id)); 319 if ((table = table_find(env, id)) == NULL) 320 fatalx("%s: desynchronized", __func__); 321 table->conf.flags &= ~(F_DISABLE); 322 TAILQ_FOREACH(host, &table->hosts, entry) 323 host->up = HOST_UNKNOWN; 324 break; 325 case IMSG_CTL_POLL: 326 evtimer_del(&env->sc_ev); 327 TAILQ_FOREACH(table, env->sc_tables, entry) 328 table->skipped = 0; 329 hce_launch_checks(-1, EV_TIMEOUT, env); 330 break; 331 default: 332 return (-1); 333 } 334 335 return (0); 336 } 337 338 int 339 hce_dispatch_parent(int fd, struct privsep_proc *p, struct imsg *imsg) 340 { 341 struct ctl_script scr; 342 343 switch (imsg->hdr.type) { 344 case IMSG_SCRIPT: 345 IMSG_SIZE_CHECK(imsg, &scr); 346 bcopy(imsg->data, &scr, sizeof(scr)); 347 script_done(env, &scr); 348 break; 349 case IMSG_CFG_TABLE: 350 config_gettable(env, imsg); 351 break; 352 case IMSG_CFG_HOST: 353 config_gethost(env, imsg); 354 break; 355 case IMSG_CFG_DONE: 356 config_getcfg(env, imsg); 357 break; 358 case IMSG_CTL_START: 359 hce_setup_events(); 360 break; 361 case IMSG_CTL_RESET: 362 config_getreset(env, imsg); 363 break; 364 default: 365 return (-1); 366 } 367 368 return (0); 369 } 370 371 int 372 hce_dispatch_relay(int fd, struct privsep_proc *p, struct imsg *imsg) 373 { 374 switch (imsg->hdr.type) { 375 default: 376 break; 377 } 378 379 return (-1); 380 } 381