/* $OpenBSD: slaacd.c,v 1.3 2017/03/20 16:15:37 florian Exp $ */ /* * Copyright (c) 2017 Florian Obser * Copyright (c) 2005 Claudio Jeker * Copyright (c) 2004 Esben Norby * Copyright (c) 2003, 2004 Henning Brauer * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "log.h" #include "slaacd.h" #include "frontend.h" #include "engine.h" #include "control.h" const char* imsg_type_name[] = { "IMSG_NONE", "IMSG_CTL_LOG_VERBOSE", "IMSG_CTL_SHOW_INTERFACE_INFO", "IMSG_CTL_SHOW_INTERFACE_INFO_RA", "IMSG_CTL_SHOW_INTERFACE_INFO_RA_PREFIX", "IMSG_CTL_SHOW_INTERFACE_INFO_RA_RDNS", "IMSG_CTL_SHOW_INTERFACE_INFO_RA_DNSSL", "IMSG_CTL_SHOW_FRONTEND_INFO", "IMSG_CTL_SHOW_MAIN_INFO", "IMSG_CTL_END", "IMSG_SOCKET_IPC", "IMSG_STARTUP", "IMSG_UPDATE_IF", "IMSG_REMOVE_IF", "IMSG_RA", "IMSG_CTL_SEND_SOLICITATION", "IMSG_PROPOSAL", }; __dead void usage(void); __dead void main_shutdown(void); void main_sig_handler(int, short, void *); static pid_t start_child(int, char *, int, int, int, char *); void main_dispatch_frontend(int, short, void *); void main_dispatch_engine(int, short, void *); void handle_proposal(struct imsg_proposal *); static int main_imsg_send_ipc_sockets(struct imsgbuf *, struct imsgbuf *); struct imsgev *iev_frontend; struct imsgev *iev_engine; pid_t frontend_pid; pid_t engine_pid; uint32_t cmd_opts; int routesock; char *csock; void main_sig_handler(int sig, short event, void *arg) { /* * Normal signal handler rules don't apply because libevent * decouples for us. */ switch (sig) { case SIGTERM: case SIGINT: main_shutdown(); case SIGHUP: log_debug("sighub received"); break; default: fatalx("unexpected signal"); } } __dead void usage(void) { extern char *__progname; fprintf(stderr, "usage: %s [-dv] [-s socket]\n", __progname); exit(1); } int main(int argc, char *argv[]) { struct event ev_sigint, ev_sigterm, ev_sighup; int ch; int debug = 0, engine_flag = 0, frontend_flag = 0; char *saved_argv0; int pipe_main2frontend[2]; int pipe_main2engine[2]; csock = SLAACD_SOCKET; log_init(1, LOG_DAEMON); /* Log to stderr until daemonized. */ log_setverbose(1); saved_argv0 = argv[0]; if (saved_argv0 == NULL) saved_argv0 = "slaacd"; while ((ch = getopt(argc, argv, "dEFs:v")) != -1) { switch (ch) { case 'd': debug = 1; break; case 'E': engine_flag = 1; break; case 'F': frontend_flag = 1; break; case 's': csock = optarg; break; case 'v': if (cmd_opts & OPT_VERBOSE) cmd_opts |= OPT_VERBOSE2; cmd_opts |= OPT_VERBOSE; break; default: usage(); } } argc -= optind; argv += optind; if (argc > 0 || (engine_flag && frontend_flag)) usage(); if (engine_flag) engine(debug, cmd_opts & OPT_VERBOSE); else if (frontend_flag) frontend(debug, cmd_opts & OPT_VERBOSE, csock); /* Check for root privileges. */ if (geteuid()) errx(1, "need root privileges"); /* Check for assigned daemon user */ if (getpwnam(SLAACD_USER) == NULL) errx(1, "unknown user %s", SLAACD_USER); log_init(debug, LOG_DAEMON); log_setverbose(cmd_opts & OPT_VERBOSE); if (!debug) daemon(1, 0); log_info("startup"); if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, PF_UNSPEC, pipe_main2frontend) == -1) fatal("main2frontend socketpair"); if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, PF_UNSPEC, pipe_main2engine) == -1) fatal("main2engine socketpair"); /* Start children. */ engine_pid = start_child(PROC_ENGINE, saved_argv0, pipe_main2engine[1], debug, cmd_opts & OPT_VERBOSE, NULL); frontend_pid = start_child(PROC_FRONTEND, saved_argv0, pipe_main2frontend[1], debug, cmd_opts & OPT_VERBOSE, csock); slaacd_process = PROC_MAIN; setproctitle(log_procnames[slaacd_process]); log_procinit(log_procnames[slaacd_process]); if ((routesock = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) fatal("route socket"); event_init(); /* Setup signal handler. */ signal_set(&ev_sigint, SIGINT, main_sig_handler, NULL); signal_set(&ev_sigterm, SIGTERM, main_sig_handler, NULL); signal_set(&ev_sighup, SIGHUP, main_sig_handler, NULL); signal_add(&ev_sigint, NULL); signal_add(&ev_sigterm, NULL); signal_add(&ev_sighup, NULL); signal(SIGPIPE, SIG_IGN); /* Setup pipes to children. */ if ((iev_frontend = malloc(sizeof(struct imsgev))) == NULL || (iev_engine = malloc(sizeof(struct imsgev))) == NULL) fatal(NULL); imsg_init(&iev_frontend->ibuf, pipe_main2frontend[0]); iev_frontend->handler = main_dispatch_frontend; imsg_init(&iev_engine->ibuf, pipe_main2engine[0]); iev_engine->handler = main_dispatch_engine; /* Setup event handlers for pipes to engine & frontend. */ iev_frontend->events = EV_READ; event_set(&iev_frontend->ev, iev_frontend->ibuf.fd, iev_frontend->events, iev_frontend->handler, iev_frontend); event_add(&iev_frontend->ev, NULL); iev_engine->events = EV_READ; event_set(&iev_engine->ev, iev_engine->ibuf.fd, iev_engine->events, iev_engine->handler, iev_engine); event_add(&iev_engine->ev, NULL); if (main_imsg_send_ipc_sockets(&iev_frontend->ibuf, &iev_engine->ibuf)) fatal("could not establish imsg links"); if (pledge("rpath stdio sendfd cpath", NULL) == -1) fatal("pledge"); main_imsg_compose_frontend(IMSG_STARTUP, 0, NULL, 0); event_dispatch(); main_shutdown(); return (0); } __dead void main_shutdown(void) { pid_t pid; int status; /* Close pipes. */ msgbuf_clear(&iev_frontend->ibuf.w); close(iev_frontend->ibuf.fd); msgbuf_clear(&iev_engine->ibuf.w); close(iev_engine->ibuf.fd); log_debug("waiting for children to terminate"); do { pid = wait(&status); if (pid == -1) { if (errno != EINTR && errno != ECHILD) fatal("wait"); } else if (WIFSIGNALED(status)) log_warnx("%s terminated; signal %d", (pid == engine_pid) ? "engine" : "frontend", WTERMSIG(status)); } while (pid != -1 || (pid == -1 && errno == EINTR)); free(iev_frontend); free(iev_engine); control_cleanup(csock); log_info("terminating"); exit(0); } static pid_t start_child(int p, char *argv0, int fd, int debug, int verbose, char *sockname) { char *argv[7]; int argc = 0; pid_t pid; switch (pid = fork()) { case -1: fatal("cannot fork"); case 0: break; default: close(fd); return (pid); } if (dup2(fd, 3) == -1) fatal("cannot setup imsg fd"); argv[argc++] = argv0; switch (p) { case PROC_MAIN: fatalx("Can not start main process"); case PROC_ENGINE: argv[argc++] = "-E"; break; case PROC_FRONTEND: argv[argc++] = "-F"; break; } if (debug) argv[argc++] = "-d"; if (verbose) argv[argc++] = "-v"; if (sockname) { argv[argc++] = "-s"; argv[argc++] = sockname; } argv[argc++] = NULL; execvp(argv0, argv); fatal("execvp"); } void main_dispatch_frontend(int fd, short event, void *bula) { struct imsgev *iev = bula; struct imsgbuf *ibuf; struct imsg imsg; ssize_t n; int shut = 0, verbose; ibuf = &iev->ibuf; if (event & EV_READ) { if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN) fatal("imsg_read error"); if (n == 0) /* Connection closed. */ shut = 1; } if (event & EV_WRITE) { if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN) fatal("msgbuf_write"); if (n == 0) /* Connection closed. */ shut = 1; } for (;;) { if ((n = imsg_get(ibuf, &imsg)) == -1) fatal("imsg_get"); if (n == 0) /* No more messages. */ break; switch (imsg.hdr.type) { case IMSG_CTL_LOG_VERBOSE: /* Already checked by frontend. */ memcpy(&verbose, imsg.data, sizeof(verbose)); log_setverbose(verbose); break; default: log_debug("%s: error handling imsg %d", __func__, imsg.hdr.type); break; } imsg_free(&imsg); } if (!shut) imsg_event_add(iev); else { /* This pipe is dead. Remove its event handler */ event_del(&iev->ev); event_loopexit(NULL); } } void main_dispatch_engine(int fd, short event, void *bula) { struct imsgev *iev = bula; struct imsgbuf *ibuf; struct imsg imsg; struct imsg_proposal proposal; ssize_t n; int shut = 0; ibuf = &iev->ibuf; if (event & EV_READ) { if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN) fatal("imsg_read error"); if (n == 0) /* Connection closed. */ shut = 1; } if (event & EV_WRITE) { if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN) fatal("msgbuf_write"); if (n == 0) /* Connection closed. */ shut = 1; } for (;;) { if ((n = imsg_get(ibuf, &imsg)) == -1) fatal("imsg_get"); if (n == 0) /* No more messages. */ break; switch (imsg.hdr.type) { case IMSG_PROPOSAL: if (imsg.hdr.len != IMSG_HEADER_SIZE + sizeof(proposal)) fatal("%s: IMSG_PROPOSAL wrong " "length: %d", __func__, imsg.hdr.len); memcpy(&proposal, imsg.data, sizeof(proposal)); handle_proposal(&proposal); break; default: log_debug("%s: error handling imsg %d", __func__, imsg.hdr.type); break; } imsg_free(&imsg); } if (!shut) imsg_event_add(iev); else { /* This pipe is dead. Remove its event handler. */ event_del(&iev->ev); event_loopexit(NULL); } } int main_imsg_compose_frontend(int type, pid_t pid, void *data, uint16_t datalen) { if (iev_frontend) return (imsg_compose_event(iev_frontend, type, 0, pid, -1, data, datalen)); else return (-1); } int main_imsg_compose_engine(int type, pid_t pid, void *data, uint16_t datalen) { if (iev_engine) return(imsg_compose_event(iev_engine, type, 0, pid, -1, data, datalen)); else return (-1); } void imsg_event_add(struct imsgev *iev) { iev->events = EV_READ; if (iev->ibuf.w.queued) iev->events |= EV_WRITE; event_del(&iev->ev); event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev); event_add(&iev->ev, NULL); } int imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid, pid_t pid, int fd, void *data, uint16_t datalen) { int ret; if ((ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data, datalen)) != -1) imsg_event_add(iev); return (ret); } static int main_imsg_send_ipc_sockets(struct imsgbuf *frontend_buf, struct imsgbuf *engine_buf) { int pipe_frontend2engine[2]; if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, PF_UNSPEC, pipe_frontend2engine) == -1) return (-1); if (imsg_compose(frontend_buf, IMSG_SOCKET_IPC, 0, 0, pipe_frontend2engine[0], NULL, 0) == -1) return (-1); imsg_flush(frontend_buf); if (imsg_compose(engine_buf, IMSG_SOCKET_IPC, 0, 0, pipe_frontend2engine[1], NULL, 0) == -1) return (-1); imsg_flush(engine_buf); return (0); } #define ROUNDUP(a) \ (((a) & (sizeof(long) - 1)) ? (1 + ((a) | (sizeof(long) - 1))) : (a)) void handle_proposal(struct imsg_proposal *proposal) { static int seq = 0; struct rt_msghdr rtm; struct sockaddr_rtstatic rtstatic; struct sockaddr_in6 ifa, mask, gateway; struct in6_addr prefix; struct iovec iov[11]; long pad = 0; int iovcnt = 0, padlen; uint8_t prefixlen; char *p; memset(&rtm, 0, sizeof(rtm)); rtm.rtm_version = RTM_VERSION; rtm.rtm_type = RTM_PROPOSAL; rtm.rtm_msglen = sizeof(rtm); rtm.rtm_tableid = 0; /* XXX imsg->rdomain; */ rtm.rtm_index = proposal->if_index; rtm.rtm_seq = ++seq; rtm.rtm_priority = RTP_PROPOSAL_SLAAC; rtm.rtm_addrs = RTA_NETMASK | RTA_IFA | RTA_STATIC; rtm.rtm_flags = RTF_UP; iov[iovcnt].iov_base = &rtm; iov[iovcnt++].iov_len = sizeof(rtm); memset(&mask, 0, sizeof(mask)); mask.sin6_family = AF_INET6; mask.sin6_len = sizeof(struct sockaddr_in6); mask.sin6_addr = proposal->mask; ifa = proposal->addr; iov[iovcnt].iov_base = &mask; iov[iovcnt++].iov_len = sizeof(mask); rtm.rtm_msglen += sizeof(mask); padlen = ROUNDUP(sizeof(mask)) - sizeof(mask); if (padlen > 0) { iov[iovcnt].iov_base = &pad; iov[iovcnt++].iov_len = padlen; rtm.rtm_msglen += padlen; } iov[iovcnt].iov_base = &ifa; iov[iovcnt++].iov_len = sizeof(ifa); rtm.rtm_msglen += sizeof(ifa); padlen = ROUNDUP(sizeof(ifa)) - sizeof(ifa); if (padlen > 0) { iov[iovcnt].iov_base = &pad; iov[iovcnt++].iov_len = padlen; rtm.rtm_msglen += padlen; } if (proposal->rdns.sr_len > 0) { rtm.rtm_addrs |= RTA_DNS; iov[iovcnt].iov_base = &proposal->rdns; iov[iovcnt++].iov_len = proposal->rdns.sr_len; rtm.rtm_msglen += proposal->rdns.sr_len; padlen = ROUNDUP(proposal->rdns.sr_len) - proposal->rdns.sr_len; if (padlen > 0) { iov[iovcnt].iov_base = &pad; iov[iovcnt++].iov_len = padlen; rtm.rtm_msglen += padlen; } } /* setup default route ::/0 */ prefixlen = 0; memset(&prefix, 0, sizeof(prefix)); gateway = proposal->gateway; memset(&rtstatic, 0, sizeof(rtstatic)); rtstatic.sr_family = AF_INET6; rtstatic.sr_len = offsetof(struct sockaddr_rtstatic, sr_static) + sizeof(uint8_t) + sizeof(struct in6_addr) + sizeof(struct sockaddr_in6); p = rtstatic.sr_static; memcpy(p, &prefixlen, sizeof(prefixlen)); p += sizeof(prefixlen); memcpy(p, &prefix, sizeof(prefix)); p += sizeof(prefix); memcpy(p, &gateway, sizeof(gateway)); iov[iovcnt].iov_base = &rtstatic; iov[iovcnt++].iov_len = rtstatic.sr_len; rtm.rtm_msglen += rtstatic.sr_len; padlen = ROUNDUP(rtstatic.sr_len) - rtstatic.sr_len; if (padlen > 0) { iov[iovcnt].iov_base = &pad; iov[iovcnt++].iov_len = padlen; rtm.rtm_msglen += padlen; } if (proposal->dnssl.sr_len > 0) { rtm.rtm_addrs |= RTA_SEARCH; iov[iovcnt].iov_base = &proposal->dnssl; iov[iovcnt++].iov_len = proposal->dnssl.sr_len; rtm.rtm_msglen += proposal->dnssl.sr_len; padlen = ROUNDUP(proposal->dnssl.sr_len) - proposal->dnssl.sr_len; if (padlen > 0) { iov[iovcnt].iov_base = &pad; iov[iovcnt++].iov_len = padlen; rtm.rtm_msglen += padlen; } } if (writev(routesock, iov, iovcnt) == -1) log_warn("failed to send proposal"); }