1 /* $OpenBSD: hce.c,v 1.75 2016/09/03 14:09:04 reyk 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("hce: pledge"); 75 } 76 77 void 78 hce_setup_events(void) 79 { 80 struct timeval tv; 81 struct table *table; 82 83 if (!(TAILQ_EMPTY(env->sc_tables) || 84 event_initialized(&env->sc_ev))) { 85 evtimer_set(&env->sc_ev, hce_launch_checks, env); 86 bzero(&tv, sizeof(tv)); 87 evtimer_add(&env->sc_ev, &tv); 88 } 89 90 if (env->sc_conf.flags & F_TLS) { 91 TAILQ_FOREACH(table, env->sc_tables, entry) { 92 if (!(table->conf.flags & F_TLS) || 93 table->ssl_ctx != NULL) 94 continue; 95 table->ssl_ctx = ssl_ctx_create(env); 96 } 97 } 98 } 99 100 void 101 hce_disable_events(void) 102 { 103 struct table *table; 104 struct host *host; 105 106 evtimer_del(&env->sc_ev); 107 TAILQ_FOREACH(table, env->sc_tables, entry) { 108 TAILQ_FOREACH(host, &table->hosts, entry) { 109 host->he = HCE_ABORT; 110 if (event_initialized(&host->cte.ev)) { 111 event_del(&host->cte.ev); 112 close(host->cte.s); 113 } 114 } 115 } 116 if (env->sc_has_icmp) { 117 event_del(&env->sc_icmp_send.ev); 118 event_del(&env->sc_icmp_recv.ev); 119 } 120 if (env->sc_has_icmp6) { 121 event_del(&env->sc_icmp6_send.ev); 122 event_del(&env->sc_icmp6_recv.ev); 123 } 124 } 125 126 void 127 hce_launch_checks(int fd, short event, void *arg) 128 { 129 struct host *host; 130 struct table *table; 131 struct timeval tv; 132 133 /* 134 * notify pfe checks are done and schedule next check 135 */ 136 proc_compose(env->sc_ps, PROC_PFE, IMSG_SYNC, NULL, 0); 137 TAILQ_FOREACH(table, env->sc_tables, entry) { 138 TAILQ_FOREACH(host, &table->hosts, entry) { 139 if ((host->flags & F_CHECK_DONE) == 0) 140 host->he = HCE_INTERVAL_TIMEOUT; 141 host->flags &= ~(F_CHECK_SENT|F_CHECK_DONE); 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("hce_launch_checks: unknown check type"); 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; 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("hce_notify_done: desynchronized"); 207 208 if ((table = table_find(env, host->conf.tableid)) == NULL) 209 fatalx("hce_notify_done: invalid table id"); 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 else 251 logopt = RELAYD_OPT_LOGNOTIFY; 252 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); 257 else 258 duration = 0; 259 260 if (env->sc_conf.opts & logopt) { 261 if (host->code > 0) 262 asprintf(&codemsg, ",%d", host->code); 263 log_info("host %s, check %s%s (%lums,%s%s), state %s -> %s, " 264 "availability %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)); 270 free(codemsg); 271 } 272 273 host->last_up = host->up; 274 275 if (SLIST_EMPTY(&host->children)) 276 return; 277 278 /* Notify for all other hosts that inherit the state from this one */ 279 SLIST_FOREACH(h, &host->children, child) { 280 h->up = hostup; 281 hce_notify_done(h, he); 282 } 283 } 284 285 int 286 hce_dispatch_pfe(int fd, struct privsep_proc *p, struct imsg *imsg) 287 { 288 objid_t id; 289 struct host *host; 290 struct table *table; 291 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("hce_dispatch_pfe: desynchronized"); 297 host->flags |= F_DISABLE; 298 host->up = HOST_UNKNOWN; 299 host->check_cnt = 0; 300 host->up_cnt = 0; 301 host->he = HCE_NONE; 302 break; 303 case IMSG_HOST_ENABLE: 304 memcpy(&id, imsg->data, sizeof(id)); 305 if ((host = host_find(env, id)) == NULL) 306 fatalx("hce_dispatch_pfe: desynchronized"); 307 host->flags &= ~(F_DISABLE); 308 host->up = HOST_UNKNOWN; 309 host->he = HCE_NONE; 310 break; 311 case IMSG_TABLE_DISABLE: 312 memcpy(&id, imsg->data, sizeof(id)); 313 if ((table = table_find(env, id)) == NULL) 314 fatalx("hce_dispatch_pfe: desynchronized"); 315 table->conf.flags |= F_DISABLE; 316 TAILQ_FOREACH(host, &table->hosts, entry) 317 host->up = HOST_UNKNOWN; 318 break; 319 case IMSG_TABLE_ENABLE: 320 memcpy(&id, imsg->data, sizeof(id)); 321 if ((table = table_find(env, id)) == NULL) 322 fatalx("hce_dispatch_pfe: desynchronized"); 323 table->conf.flags &= ~(F_DISABLE); 324 TAILQ_FOREACH(host, &table->hosts, entry) 325 host->up = HOST_UNKNOWN; 326 break; 327 case IMSG_CTL_POLL: 328 evtimer_del(&env->sc_ev); 329 TAILQ_FOREACH(table, env->sc_tables, entry) 330 table->skipped = 0; 331 hce_launch_checks(-1, EV_TIMEOUT, env); 332 break; 333 default: 334 return (-1); 335 } 336 337 return (0); 338 } 339 340 int 341 hce_dispatch_parent(int fd, struct privsep_proc *p, struct imsg *imsg) 342 { 343 struct ctl_script scr; 344 345 switch (imsg->hdr.type) { 346 case IMSG_SCRIPT: 347 IMSG_SIZE_CHECK(imsg, &scr); 348 bcopy(imsg->data, &scr, sizeof(scr)); 349 script_done(env, &scr); 350 break; 351 case IMSG_CFG_TABLE: 352 config_gettable(env, imsg); 353 break; 354 case IMSG_CFG_HOST: 355 config_gethost(env, imsg); 356 break; 357 case IMSG_CFG_DONE: 358 config_getcfg(env, imsg); 359 break; 360 case IMSG_CTL_START: 361 hce_setup_events(); 362 break; 363 case IMSG_CTL_RESET: 364 config_getreset(env, imsg); 365 break; 366 default: 367 return (-1); 368 } 369 370 return (0); 371 } 372 373 int 374 hce_dispatch_relay(int fd, struct privsep_proc *p, struct imsg *imsg) 375 { 376 switch (imsg->hdr.type) { 377 default: 378 break; 379 } 380 381 return (-1); 382 } 383