xref: /openbsd/usr.sbin/rad/engine.c (revision 2b2996d8)
1*2b2996d8Sflorian /*	$OpenBSD: engine.c,v 1.15 2019/03/15 16:47:19 florian Exp $	*/
253293e44Sflorian 
353293e44Sflorian /*
453293e44Sflorian  * Copyright (c) 2018 Florian Obser <florian@openbsd.org>
553293e44Sflorian  * Copyright (c) 2004, 2005 Claudio Jeker <claudio@openbsd.org>
653293e44Sflorian  * Copyright (c) 2004 Esben Norby <norby@openbsd.org>
753293e44Sflorian  * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
853293e44Sflorian  *
953293e44Sflorian  * Permission to use, copy, modify, and distribute this software for any
1053293e44Sflorian  * purpose with or without fee is hereby granted, provided that the above
1153293e44Sflorian  * copyright notice and this permission notice appear in all copies.
1253293e44Sflorian  *
1353293e44Sflorian  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1453293e44Sflorian  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1553293e44Sflorian  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1653293e44Sflorian  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1753293e44Sflorian  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1853293e44Sflorian  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1953293e44Sflorian  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2053293e44Sflorian  */
2153293e44Sflorian 
2253293e44Sflorian #include <sys/types.h>
2353293e44Sflorian #include <sys/queue.h>
2453293e44Sflorian #include <sys/socket.h>
2553293e44Sflorian #include <sys/syslog.h>
2653293e44Sflorian #include <sys/uio.h>
2753293e44Sflorian 
2853293e44Sflorian #include <netinet/in.h>
2953293e44Sflorian #include <net/if.h>
3053293e44Sflorian #include <arpa/inet.h>
3153293e44Sflorian #include <netinet/icmp6.h>
3253293e44Sflorian 
3353293e44Sflorian #include <errno.h>
3453293e44Sflorian #include <event.h>
3553293e44Sflorian #include <imsg.h>
3653293e44Sflorian #include <signal.h>
3753293e44Sflorian #include <stdlib.h>
3853293e44Sflorian #include <string.h>
3953293e44Sflorian #include <pwd.h>
4053293e44Sflorian #include <unistd.h>
4153293e44Sflorian 
4253293e44Sflorian #include "log.h"
4353293e44Sflorian #include "rad.h"
4453293e44Sflorian #include "engine.h"
4553293e44Sflorian 
460c40990eSflorian struct engine_iface {
470c40990eSflorian 	TAILQ_ENTRY(engine_iface)	entry;
480c40990eSflorian 	struct event			timer;
490c40990eSflorian 	uint32_t			if_index;
500c40990eSflorian };
510c40990eSflorian 
520c40990eSflorian TAILQ_HEAD(, engine_iface)	engine_interfaces;
530c40990eSflorian 
540c40990eSflorian 
5553293e44Sflorian __dead void		 engine_shutdown(void);
5653293e44Sflorian void			 engine_sig_handler(int sig, short, void *);
5753293e44Sflorian void			 engine_dispatch_frontend(int, short, void *);
5853293e44Sflorian void			 engine_dispatch_main(int, short, void *);
5953293e44Sflorian void			 parse_ra_rs(struct imsg_ra_rs *);
6053293e44Sflorian void			 parse_ra(struct imsg_ra_rs *);
6153293e44Sflorian void			 parse_rs(struct imsg_ra_rs *);
620c40990eSflorian void			 update_iface(uint32_t);
634a78c7cfSflorian void			 remove_iface(uint32_t);
640c40990eSflorian struct engine_iface	*find_engine_iface_by_id(uint32_t);
650c40990eSflorian void			 iface_timeout(int, short, void *);
6653293e44Sflorian 
6753293e44Sflorian struct rad_conf	*engine_conf;
6853293e44Sflorian struct imsgev		*iev_frontend;
6953293e44Sflorian struct imsgev		*iev_main;
7053293e44Sflorian struct sockaddr_in6	 all_nodes;
7153293e44Sflorian 
7253293e44Sflorian void
7353293e44Sflorian engine_sig_handler(int sig, short event, void *arg)
7453293e44Sflorian {
7553293e44Sflorian 	/*
7653293e44Sflorian 	 * Normal signal handler rules don't apply because libevent
7753293e44Sflorian 	 * decouples for us.
7853293e44Sflorian 	 */
7953293e44Sflorian 
8053293e44Sflorian 	switch (sig) {
8153293e44Sflorian 	case SIGINT:
8253293e44Sflorian 	case SIGTERM:
8353293e44Sflorian 		engine_shutdown();
8453293e44Sflorian 	default:
8553293e44Sflorian 		fatalx("unexpected signal");
8653293e44Sflorian 	}
8753293e44Sflorian }
8853293e44Sflorian 
8953293e44Sflorian void
9053293e44Sflorian engine(int debug, int verbose)
9153293e44Sflorian {
9253293e44Sflorian 	struct event		 ev_sigint, ev_sigterm;
9353293e44Sflorian 	struct passwd		*pw;
9453293e44Sflorian 
9553293e44Sflorian 	engine_conf = config_new_empty();
9653293e44Sflorian 
9753293e44Sflorian 	log_init(debug, LOG_DAEMON);
9853293e44Sflorian 	log_setverbose(verbose);
9953293e44Sflorian 
10053293e44Sflorian 	if ((pw = getpwnam(RAD_USER)) == NULL)
10153293e44Sflorian 		fatal("getpwnam");
10253293e44Sflorian 
10353293e44Sflorian 	if (chroot(pw->pw_dir) == -1)
10453293e44Sflorian 		fatal("chroot");
10553293e44Sflorian 	if (chdir("/") == -1)
10653293e44Sflorian 		fatal("chdir(\"/\")");
10753293e44Sflorian 
10853293e44Sflorian 	rad_process = PROC_ENGINE;
10953293e44Sflorian 	setproctitle("%s", log_procnames[rad_process]);
11053293e44Sflorian 	log_procinit(log_procnames[rad_process]);
11153293e44Sflorian 
11253293e44Sflorian 	if (setgroups(1, &pw->pw_gid) ||
11353293e44Sflorian 	    setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
11453293e44Sflorian 	    setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
11553293e44Sflorian 		fatal("can't drop privileges");
11653293e44Sflorian 
11753293e44Sflorian 	if (pledge("stdio recvfd", NULL) == -1)
11853293e44Sflorian 		fatal("pledge");
11953293e44Sflorian 
12053293e44Sflorian 	event_init();
12153293e44Sflorian 
12253293e44Sflorian 	/* Setup signal handler(s). */
12353293e44Sflorian 	signal_set(&ev_sigint, SIGINT, engine_sig_handler, NULL);
12453293e44Sflorian 	signal_set(&ev_sigterm, SIGTERM, engine_sig_handler, NULL);
12553293e44Sflorian 	signal_add(&ev_sigint, NULL);
12653293e44Sflorian 	signal_add(&ev_sigterm, NULL);
12753293e44Sflorian 	signal(SIGPIPE, SIG_IGN);
12853293e44Sflorian 	signal(SIGHUP, SIG_IGN);
12953293e44Sflorian 
13053293e44Sflorian 	/* Setup pipe and event handler to the main process. */
13153293e44Sflorian 	if ((iev_main = malloc(sizeof(struct imsgev))) == NULL)
13253293e44Sflorian 		fatal(NULL);
13353293e44Sflorian 
13453293e44Sflorian 	imsg_init(&iev_main->ibuf, 3);
13553293e44Sflorian 	iev_main->handler = engine_dispatch_main;
13653293e44Sflorian 
13753293e44Sflorian 	/* Setup event handlers. */
13853293e44Sflorian 	iev_main->events = EV_READ;
13953293e44Sflorian 	event_set(&iev_main->ev, iev_main->ibuf.fd, iev_main->events,
14053293e44Sflorian 	    iev_main->handler, iev_main);
14153293e44Sflorian 	event_add(&iev_main->ev, NULL);
14253293e44Sflorian 
14353293e44Sflorian 	all_nodes.sin6_len = sizeof(all_nodes);
14453293e44Sflorian 	all_nodes.sin6_family = AF_INET6;
14553293e44Sflorian 	if (inet_pton(AF_INET6, "ff02::1", &all_nodes.sin6_addr) != 1)
14653293e44Sflorian 		fatal("inet_pton");
14753293e44Sflorian 
1484a78c7cfSflorian 	TAILQ_INIT(&engine_interfaces);
1494a78c7cfSflorian 
15053293e44Sflorian 	event_dispatch();
15153293e44Sflorian 
15253293e44Sflorian 	engine_shutdown();
15353293e44Sflorian }
15453293e44Sflorian 
15553293e44Sflorian __dead void
15653293e44Sflorian engine_shutdown(void)
15753293e44Sflorian {
15853293e44Sflorian 	/* Close pipes. */
15953293e44Sflorian 	msgbuf_clear(&iev_frontend->ibuf.w);
16053293e44Sflorian 	close(iev_frontend->ibuf.fd);
16153293e44Sflorian 	msgbuf_clear(&iev_main->ibuf.w);
16253293e44Sflorian 	close(iev_main->ibuf.fd);
16353293e44Sflorian 
16453293e44Sflorian 	config_clear(engine_conf);
16553293e44Sflorian 
16653293e44Sflorian 	free(iev_frontend);
16753293e44Sflorian 	free(iev_main);
16853293e44Sflorian 
16953293e44Sflorian 	log_info("engine exiting");
17053293e44Sflorian 	exit(0);
17153293e44Sflorian }
17253293e44Sflorian 
17353293e44Sflorian int
17453293e44Sflorian engine_imsg_compose_frontend(int type, pid_t pid, void *data, uint16_t datalen)
17553293e44Sflorian {
17653293e44Sflorian 	return (imsg_compose_event(iev_frontend, type, 0, pid, -1,
17753293e44Sflorian 	    data, datalen));
17853293e44Sflorian }
17953293e44Sflorian 
18053293e44Sflorian void
18153293e44Sflorian engine_dispatch_frontend(int fd, short event, void *bula)
18253293e44Sflorian {
18353293e44Sflorian 	struct imsgev		*iev = bula;
18453293e44Sflorian 	struct imsgbuf		*ibuf;
18553293e44Sflorian 	struct imsg		 imsg;
18653293e44Sflorian 	struct imsg_ra_rs	 ra_rs;
18753293e44Sflorian 	ssize_t			 n;
1880c40990eSflorian 	uint32_t		 if_index;
18953293e44Sflorian 	int			 shut = 0, verbose;
19053293e44Sflorian 
19153293e44Sflorian 	ibuf = &iev->ibuf;
19253293e44Sflorian 
19353293e44Sflorian 	if (event & EV_READ) {
19453293e44Sflorian 		if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
19553293e44Sflorian 			fatal("imsg_read error");
19653293e44Sflorian 		if (n == 0)	/* Connection closed. */
19753293e44Sflorian 			shut = 1;
19853293e44Sflorian 	}
19953293e44Sflorian 	if (event & EV_WRITE) {
20053293e44Sflorian 		if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
20153293e44Sflorian 			fatal("msgbuf_write");
20253293e44Sflorian 		if (n == 0)	/* Connection closed. */
20353293e44Sflorian 			shut = 1;
20453293e44Sflorian 	}
20553293e44Sflorian 
20653293e44Sflorian 	for (;;) {
20753293e44Sflorian 		if ((n = imsg_get(ibuf, &imsg)) == -1)
20853293e44Sflorian 			fatal("%s: imsg_get error", __func__);
20953293e44Sflorian 		if (n == 0)	/* No more messages. */
21053293e44Sflorian 			break;
21153293e44Sflorian 
21253293e44Sflorian 		switch (imsg.hdr.type) {
21353293e44Sflorian 		case IMSG_RA_RS:
214b17c900dSpamela 			if (IMSG_DATA_SIZE(imsg) != sizeof(ra_rs))
21551a61992Spamela 				fatalx("%s: IMSG_RA_RS wrong length: %lu",
216b17c900dSpamela 				    __func__, IMSG_DATA_SIZE(imsg));
21753293e44Sflorian 			memcpy(&ra_rs, imsg.data, sizeof(ra_rs));
21853293e44Sflorian 			parse_ra_rs(&ra_rs);
21953293e44Sflorian 			break;
2200c40990eSflorian 		case IMSG_UPDATE_IF:
221b17c900dSpamela 			if (IMSG_DATA_SIZE(imsg) != sizeof(if_index))
22251a61992Spamela 				fatalx("%s: IMSG_UPDATE_IF wrong length: %lu",
223b17c900dSpamela 				    __func__, IMSG_DATA_SIZE(imsg));
2240c40990eSflorian 			memcpy(&if_index, imsg.data, sizeof(if_index));
2250c40990eSflorian 			update_iface(if_index);
2260c40990eSflorian 			break;
2274a78c7cfSflorian 		case IMSG_REMOVE_IF:
228b17c900dSpamela 			if (IMSG_DATA_SIZE(imsg) != sizeof(if_index))
22951a61992Spamela 				fatalx("%s: IMSG_REMOVE_IF wrong length: %lu",
230b17c900dSpamela 				    __func__, IMSG_DATA_SIZE(imsg));
2314a78c7cfSflorian 			memcpy(&if_index, imsg.data, sizeof(if_index));
2324a78c7cfSflorian 			remove_iface(if_index);
2334a78c7cfSflorian 			break;
23453293e44Sflorian 		case IMSG_CTL_LOG_VERBOSE:
2350eb5c43bSpamela 			if (IMSG_DATA_SIZE(imsg) != sizeof(verbose))
2360eb5c43bSpamela 				fatalx("%s: IMSG_CTL_LOG_VERBOSE wrong length: "
2370eb5c43bSpamela 				    "%lu", __func__, IMSG_DATA_SIZE(imsg));
23853293e44Sflorian 			memcpy(&verbose, imsg.data, sizeof(verbose));
23953293e44Sflorian 			log_setverbose(verbose);
24053293e44Sflorian 			break;
24153293e44Sflorian 		default:
24253293e44Sflorian 			log_debug("%s: unexpected imsg %d", __func__,
24353293e44Sflorian 			    imsg.hdr.type);
24453293e44Sflorian 			break;
24553293e44Sflorian 		}
24653293e44Sflorian 		imsg_free(&imsg);
24753293e44Sflorian 	}
24853293e44Sflorian 	if (!shut)
24953293e44Sflorian 		imsg_event_add(iev);
25053293e44Sflorian 	else {
25153293e44Sflorian 		/* This pipe is dead. Remove its event handler. */
25253293e44Sflorian 		event_del(&iev->ev);
25353293e44Sflorian 		event_loopexit(NULL);
25453293e44Sflorian 	}
25553293e44Sflorian }
25653293e44Sflorian 
25753293e44Sflorian void
25853293e44Sflorian engine_dispatch_main(int fd, short event, void *bula)
25953293e44Sflorian {
26053293e44Sflorian 	static struct rad_conf		*nconf;
26153293e44Sflorian 	static struct ra_iface_conf	*ra_iface_conf;
2628815eebdSflorian 	static struct ra_options_conf	*ra_options;
26353293e44Sflorian 	struct imsg			 imsg;
26453293e44Sflorian 	struct imsgev			*iev = bula;
26553293e44Sflorian 	struct imsgbuf			*ibuf;
26653293e44Sflorian 	struct ra_prefix_conf		*ra_prefix_conf;
2674c40b7e8Sflorian 	struct ra_rdnss_conf		*ra_rdnss_conf;
2684c40b7e8Sflorian 	struct ra_dnssl_conf		*ra_dnssl_conf;
26953293e44Sflorian 	ssize_t				 n;
27053293e44Sflorian 	int				 shut = 0;
27153293e44Sflorian 
27253293e44Sflorian 	ibuf = &iev->ibuf;
27353293e44Sflorian 
27453293e44Sflorian 	if (event & EV_READ) {
27553293e44Sflorian 		if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
27653293e44Sflorian 			fatal("imsg_read error");
27753293e44Sflorian 		if (n == 0)	/* Connection closed. */
27853293e44Sflorian 			shut = 1;
27953293e44Sflorian 	}
28053293e44Sflorian 	if (event & EV_WRITE) {
28153293e44Sflorian 		if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
28253293e44Sflorian 			fatal("msgbuf_write");
28353293e44Sflorian 		if (n == 0)	/* Connection closed. */
28453293e44Sflorian 			shut = 1;
28553293e44Sflorian 	}
28653293e44Sflorian 
28753293e44Sflorian 	for (;;) {
28853293e44Sflorian 		if ((n = imsg_get(ibuf, &imsg)) == -1)
28953293e44Sflorian 			fatal("%s: imsg_get error", __func__);
29053293e44Sflorian 		if (n == 0)	/* No more messages. */
29153293e44Sflorian 			break;
29253293e44Sflorian 
29353293e44Sflorian 		switch (imsg.hdr.type) {
29453293e44Sflorian 		case IMSG_SOCKET_IPC:
29553293e44Sflorian 			/*
29653293e44Sflorian 			 * Setup pipe and event handler to the frontend
29753293e44Sflorian 			 * process.
29853293e44Sflorian 			 */
2990eb5c43bSpamela 			if (iev_frontend)
3000eb5c43bSpamela 				fatalx("%s: received unexpected imsg fd "
30153293e44Sflorian 				    "to engine", __func__);
3020eb5c43bSpamela 
3030eb5c43bSpamela 			if ((fd = imsg.fd) == -1)
3040eb5c43bSpamela 				fatalx("%s: expected to receive imsg fd to "
30553293e44Sflorian 				   "engine but didn't receive any", __func__);
30653293e44Sflorian 
30753293e44Sflorian 			iev_frontend = malloc(sizeof(struct imsgev));
30853293e44Sflorian 			if (iev_frontend == NULL)
30953293e44Sflorian 				fatal(NULL);
31053293e44Sflorian 
31153293e44Sflorian 			imsg_init(&iev_frontend->ibuf, fd);
31253293e44Sflorian 			iev_frontend->handler = engine_dispatch_frontend;
31353293e44Sflorian 			iev_frontend->events = EV_READ;
31453293e44Sflorian 
31553293e44Sflorian 			event_set(&iev_frontend->ev, iev_frontend->ibuf.fd,
31653293e44Sflorian 			iev_frontend->events, iev_frontend->handler,
31753293e44Sflorian 			    iev_frontend);
31853293e44Sflorian 			event_add(&iev_frontend->ev, NULL);
31953293e44Sflorian 			break;
32053293e44Sflorian 		case IMSG_RECONF_CONF:
321*2b2996d8Sflorian 			if (nconf != NULL)
322*2b2996d8Sflorian 				fatalx("%s: IMSG_RECONF_CONF already in "
323*2b2996d8Sflorian 				    "progress", __func__);
3240eb5c43bSpamela 			if (IMSG_DATA_SIZE(imsg) != sizeof(struct rad_conf))
325ce1003a9Spamela 				fatalx("%s: IMSG_RECONF_CONF wrong length: %lu",
326ce1003a9Spamela 				    __func__, IMSG_DATA_SIZE(imsg));
32753293e44Sflorian 			if ((nconf = malloc(sizeof(struct rad_conf))) == NULL)
32853293e44Sflorian 				fatal(NULL);
32953293e44Sflorian 			memcpy(nconf, imsg.data, sizeof(struct rad_conf));
33053293e44Sflorian 			SIMPLEQ_INIT(&nconf->ra_iface_list);
3318815eebdSflorian 			SIMPLEQ_INIT(&nconf->ra_options.ra_rdnss_list);
3328815eebdSflorian 			SIMPLEQ_INIT(&nconf->ra_options.ra_dnssl_list);
3338815eebdSflorian 			ra_options = &nconf->ra_options;
33453293e44Sflorian 			break;
33553293e44Sflorian 		case IMSG_RECONF_RA_IFACE:
3360eb5c43bSpamela 			if (IMSG_DATA_SIZE(imsg) != sizeof(struct
3370eb5c43bSpamela 			    ra_iface_conf))
3380eb5c43bSpamela 				fatalx("%s: IMSG_RECONF_RA_IFACE wrong length: "
3390eb5c43bSpamela 				    "%lu", __func__, IMSG_DATA_SIZE(imsg));
34053293e44Sflorian 			if ((ra_iface_conf = malloc(sizeof(struct
34153293e44Sflorian 			    ra_iface_conf))) == NULL)
34253293e44Sflorian 				fatal(NULL);
34353293e44Sflorian 			memcpy(ra_iface_conf, imsg.data,
34453293e44Sflorian 			    sizeof(struct ra_iface_conf));
34553293e44Sflorian 			ra_iface_conf->autoprefix = NULL;
34653293e44Sflorian 			SIMPLEQ_INIT(&ra_iface_conf->ra_prefix_list);
3478815eebdSflorian 			SIMPLEQ_INIT(&ra_iface_conf->ra_options.ra_rdnss_list);
3488815eebdSflorian 			SIMPLEQ_INIT(&ra_iface_conf->ra_options.ra_dnssl_list);
34953293e44Sflorian 			SIMPLEQ_INSERT_TAIL(&nconf->ra_iface_list,
35053293e44Sflorian 			    ra_iface_conf, entry);
3518815eebdSflorian 			ra_options = &ra_iface_conf->ra_options;
35253293e44Sflorian 			break;
35353293e44Sflorian 		case IMSG_RECONF_RA_AUTOPREFIX:
3540eb5c43bSpamela 			if (IMSG_DATA_SIZE(imsg) != sizeof(struct
3550eb5c43bSpamela 			    ra_prefix_conf))
3560eb5c43bSpamela 				fatalx("%s: IMSG_RECONF_RA_AUTOPREFIX wrong "
3570eb5c43bSpamela 				    "length: %lu", __func__,
3580eb5c43bSpamela 				    IMSG_DATA_SIZE(imsg));
35953293e44Sflorian 			if ((ra_iface_conf->autoprefix = malloc(sizeof(struct
36053293e44Sflorian 			    ra_prefix_conf))) == NULL)
36153293e44Sflorian 				fatal(NULL);
36253293e44Sflorian 			memcpy(ra_iface_conf->autoprefix, imsg.data,
36353293e44Sflorian 			    sizeof(struct ra_prefix_conf));
36453293e44Sflorian 			break;
36553293e44Sflorian 		case IMSG_RECONF_RA_PREFIX:
3660eb5c43bSpamela 			if (IMSG_DATA_SIZE(imsg) != sizeof(struct
3670eb5c43bSpamela 			    ra_prefix_conf))
3680eb5c43bSpamela 				fatalx("%s: IMSG_RECONF_RA_PREFIX wrong "
3690eb5c43bSpamela 				    "length: %lu", __func__,
3700eb5c43bSpamela 				    IMSG_DATA_SIZE(imsg));
37153293e44Sflorian 			if ((ra_prefix_conf = malloc(sizeof(struct
37253293e44Sflorian 			    ra_prefix_conf))) == NULL)
37353293e44Sflorian 				fatal(NULL);
37453293e44Sflorian 			memcpy(ra_prefix_conf, imsg.data, sizeof(struct
37553293e44Sflorian 			    ra_prefix_conf));
37653293e44Sflorian 			SIMPLEQ_INSERT_TAIL(&ra_iface_conf->ra_prefix_list,
37753293e44Sflorian 			    ra_prefix_conf, entry);
37853293e44Sflorian 			break;
3794c40b7e8Sflorian 		case IMSG_RECONF_RA_RDNSS:
3800eb5c43bSpamela 			if(IMSG_DATA_SIZE(imsg) != sizeof(struct
3810eb5c43bSpamela 			    ra_rdnss_conf))
3820eb5c43bSpamela 				fatalx("%s: IMSG_RECONF_RA_RDNSS wrong length: "
3830eb5c43bSpamela 				    "%lu", __func__, IMSG_DATA_SIZE(imsg));
3844c40b7e8Sflorian 			if ((ra_rdnss_conf = malloc(sizeof(struct
3854c40b7e8Sflorian 			    ra_rdnss_conf))) == NULL)
3864c40b7e8Sflorian 				fatal(NULL);
3874c40b7e8Sflorian 			memcpy(ra_rdnss_conf, imsg.data, sizeof(struct
3884c40b7e8Sflorian 			    ra_rdnss_conf));
3898815eebdSflorian 			SIMPLEQ_INSERT_TAIL(&ra_options->ra_rdnss_list,
3904c40b7e8Sflorian 			    ra_rdnss_conf, entry);
3914c40b7e8Sflorian 			break;
3924c40b7e8Sflorian 		case IMSG_RECONF_RA_DNSSL:
3930eb5c43bSpamela 			if(IMSG_DATA_SIZE(imsg) != sizeof(struct
3940eb5c43bSpamela 			    ra_dnssl_conf))
3950eb5c43bSpamela 				fatalx("%s: IMSG_RECONF_RA_DNSSL wrong length: "
3960eb5c43bSpamela 				    "%lu", __func__, IMSG_DATA_SIZE(imsg));
3974c40b7e8Sflorian 			if ((ra_dnssl_conf = malloc(sizeof(struct
3984c40b7e8Sflorian 			    ra_dnssl_conf))) == NULL)
3994c40b7e8Sflorian 				fatal(NULL);
4004c40b7e8Sflorian 			memcpy(ra_dnssl_conf, imsg.data, sizeof(struct
4014c40b7e8Sflorian 			    ra_dnssl_conf));
4028815eebdSflorian 			SIMPLEQ_INSERT_TAIL(&ra_options->ra_dnssl_list,
4034c40b7e8Sflorian 			    ra_dnssl_conf, entry);
4044c40b7e8Sflorian 			break;
40553293e44Sflorian 		case IMSG_RECONF_END:
406*2b2996d8Sflorian 			if (nconf == NULL)
407*2b2996d8Sflorian 				fatalx("%s: IMSG_RECONF_END without "
408*2b2996d8Sflorian 				    "IMSG_RECONF_CONF", __func__);
40953293e44Sflorian 			merge_config(engine_conf, nconf);
41053293e44Sflorian 			nconf = NULL;
41153293e44Sflorian 			break;
41253293e44Sflorian 		default:
41353293e44Sflorian 			log_debug("%s: unexpected imsg %d", __func__,
41453293e44Sflorian 			    imsg.hdr.type);
41553293e44Sflorian 			break;
41653293e44Sflorian 		}
41753293e44Sflorian 		imsg_free(&imsg);
41853293e44Sflorian 	}
41953293e44Sflorian 	if (!shut)
42053293e44Sflorian 		imsg_event_add(iev);
42153293e44Sflorian 	else {
42253293e44Sflorian 		/* This pipe is dead. Remove its event handler. */
42353293e44Sflorian 		event_del(&iev->ev);
42453293e44Sflorian 		event_loopexit(NULL);
42553293e44Sflorian 	}
42653293e44Sflorian }
42753293e44Sflorian 
42853293e44Sflorian 
42953293e44Sflorian void
43053293e44Sflorian parse_ra_rs(struct imsg_ra_rs *ra_rs)
43153293e44Sflorian {
43253293e44Sflorian 	char			 ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ];
43353293e44Sflorian 	struct icmp6_hdr	*hdr;
43453293e44Sflorian 
43553293e44Sflorian 	hdr = (struct icmp6_hdr *) ra_rs->packet;
43653293e44Sflorian 
43753293e44Sflorian 	switch (hdr->icmp6_type) {
43853293e44Sflorian 	case ND_ROUTER_ADVERT:
43953293e44Sflorian 		parse_ra(ra_rs);
44053293e44Sflorian 		break;
44153293e44Sflorian 	case ND_ROUTER_SOLICIT:
44253293e44Sflorian 		parse_rs(ra_rs);
44353293e44Sflorian 		break;
44453293e44Sflorian 	default:
44553293e44Sflorian 		log_warnx("unexpected icmp6_type: %d from %s on %s",
44653293e44Sflorian 		    hdr->icmp6_type, inet_ntop(AF_INET6, &ra_rs->from.sin6_addr,
44753293e44Sflorian 		    ntopbuf, INET6_ADDRSTRLEN), if_indextoname(ra_rs->if_index,
44853293e44Sflorian 		    ifnamebuf));
44953293e44Sflorian 		break;
45053293e44Sflorian 	}
45153293e44Sflorian }
45253293e44Sflorian 
45353293e44Sflorian void
45453293e44Sflorian parse_ra(struct imsg_ra_rs *ra)
45553293e44Sflorian {
45653293e44Sflorian 	char			 ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ];
45753293e44Sflorian 	log_debug("got RA from %s on %s",
45853293e44Sflorian 	    inet_ntop(AF_INET6, &ra->from.sin6_addr, ntopbuf,
45953293e44Sflorian 	    INET6_ADDRSTRLEN), if_indextoname(ra->if_index,
46053293e44Sflorian 	    ifnamebuf));
46153293e44Sflorian 	/* XXX not yet */
46253293e44Sflorian }
46353293e44Sflorian 
46453293e44Sflorian void
46553293e44Sflorian parse_rs(struct imsg_ra_rs *rs)
46653293e44Sflorian {
46753293e44Sflorian 	struct nd_router_solicit	*nd_rs;
46853293e44Sflorian 	struct imsg_send_ra		 send_ra;
46953293e44Sflorian 	ssize_t				 len;
47053293e44Sflorian 	const char			*hbuf;
47153293e44Sflorian 	char				 ifnamebuf[IFNAMSIZ];
47253293e44Sflorian 	uint8_t				*p;
47353293e44Sflorian 
47453293e44Sflorian 	hbuf = sin6_to_str(&rs->from);
47553293e44Sflorian 
47653293e44Sflorian 	send_ra.if_index = rs->if_index;
47753293e44Sflorian 	memcpy(&send_ra.to, &all_nodes, sizeof(send_ra.to));
47853293e44Sflorian 
47953293e44Sflorian 	log_debug("got RS from %s on %s", hbuf, if_indextoname(rs->if_index,
48053293e44Sflorian 	    ifnamebuf));
48153293e44Sflorian 
48253293e44Sflorian 	len = rs->len;
48353293e44Sflorian 
48453293e44Sflorian 	if (!IN6_IS_ADDR_LINKLOCAL(&rs->from.sin6_addr)) {
48553293e44Sflorian 		log_warnx("RA from non link local address %s", hbuf);
48653293e44Sflorian 		return;
48753293e44Sflorian 	}
48853293e44Sflorian 
48953293e44Sflorian 	if ((size_t)len < sizeof(struct nd_router_solicit)) {
49053293e44Sflorian 		log_warnx("received too short message (%ld) from %s", len,
49153293e44Sflorian 		    hbuf);
49253293e44Sflorian 		return;
49353293e44Sflorian 	}
49453293e44Sflorian 
49553293e44Sflorian 	p = rs->packet;
49653293e44Sflorian 	nd_rs = (struct nd_router_solicit *)p;
49753293e44Sflorian 	len -= sizeof(struct nd_router_solicit);
49853293e44Sflorian 	p += sizeof(struct nd_router_solicit);
49953293e44Sflorian 
50053293e44Sflorian 	if (nd_rs->nd_rs_code != 0) {
50153293e44Sflorian 		log_warnx("invalid ICMPv6 code (%d) from %s", nd_rs->nd_rs_code,
50253293e44Sflorian 		    hbuf);
50353293e44Sflorian 		return;
50453293e44Sflorian 	}
50553293e44Sflorian 	while ((size_t)len >= sizeof(struct nd_opt_hdr)) {
50653293e44Sflorian 		struct nd_opt_hdr *nd_opt_hdr = (struct nd_opt_hdr *)p;
50753293e44Sflorian 
50853293e44Sflorian 		len -= sizeof(struct nd_opt_hdr);
50953293e44Sflorian 		p += sizeof(struct nd_opt_hdr);
51053293e44Sflorian 
51153293e44Sflorian 		if (nd_opt_hdr->nd_opt_len * 8 - 2 > len) {
51253293e44Sflorian 			log_warnx("invalid option len: %u > %ld",
51353293e44Sflorian 			    nd_opt_hdr->nd_opt_len, len);
51453293e44Sflorian 			return;
51553293e44Sflorian 		}
51653293e44Sflorian 		switch (nd_opt_hdr->nd_opt_type) {
51753293e44Sflorian 		case ND_OPT_SOURCE_LINKADDR:
51853293e44Sflorian 			log_debug("got RS with source linkaddr option");
51953293e44Sflorian 			memcpy(&send_ra.to, &rs->from, sizeof(send_ra.to));
52053293e44Sflorian 			break;
52153293e44Sflorian 		default:
52253293e44Sflorian 			log_debug("\t\tUNKNOWN: %d", nd_opt_hdr->nd_opt_type);
52353293e44Sflorian 			break;
52453293e44Sflorian 		}
52553293e44Sflorian 		len -= nd_opt_hdr->nd_opt_len * 8 - 2;
52653293e44Sflorian 		p += nd_opt_hdr->nd_opt_len * 8 - 2;
52753293e44Sflorian 	}
52853293e44Sflorian 	engine_imsg_compose_frontend(IMSG_SEND_RA, 0, &send_ra,
52953293e44Sflorian 	    sizeof(send_ra));
53053293e44Sflorian }
5310c40990eSflorian 
5320c40990eSflorian struct engine_iface*
5330c40990eSflorian find_engine_iface_by_id(uint32_t if_index)
5340c40990eSflorian {
5350c40990eSflorian 	struct engine_iface	*engine_iface;
5360c40990eSflorian 
5370c40990eSflorian 	TAILQ_FOREACH(engine_iface, &engine_interfaces, entry) {
5380c40990eSflorian 		if (engine_iface->if_index == if_index)
5390c40990eSflorian 			return engine_iface;
5400c40990eSflorian 	}
5410c40990eSflorian 	return (NULL);
5420c40990eSflorian }
5430c40990eSflorian 
5440c40990eSflorian void
5450c40990eSflorian update_iface(uint32_t if_index)
5460c40990eSflorian {
5470c40990eSflorian 	struct engine_iface	*engine_iface;
5480c40990eSflorian 	struct timeval		 tv;
5490c40990eSflorian 
5500c40990eSflorian 	if ((engine_iface = find_engine_iface_by_id(if_index)) == NULL) {
5510c40990eSflorian 		engine_iface = calloc(1, sizeof(*engine_iface));
5520c40990eSflorian 		engine_iface->if_index = if_index;
5530c40990eSflorian 		evtimer_set(&engine_iface->timer, iface_timeout, engine_iface);
5544a78c7cfSflorian 		TAILQ_INSERT_TAIL(&engine_interfaces, engine_iface, entry);
5550c40990eSflorian 	}
5560c40990eSflorian 
5570c40990eSflorian 	tv.tv_sec = 0;
5580c40990eSflorian 	tv.tv_usec = arc4random_uniform(1000000);
5590c40990eSflorian 	evtimer_add(&engine_iface->timer, &tv);
5600c40990eSflorian }
5610c40990eSflorian 
5620c40990eSflorian void
5634a78c7cfSflorian remove_iface(uint32_t if_index)
5644a78c7cfSflorian {
5654a78c7cfSflorian 	struct engine_iface	*engine_iface;
5664a78c7cfSflorian 	struct imsg_send_ra	 send_ra;
5674a78c7cfSflorian 	char			 if_name[IF_NAMESIZE];
5684a78c7cfSflorian 
5694a78c7cfSflorian 	if ((engine_iface = find_engine_iface_by_id(if_index)) == NULL) {
5704a78c7cfSflorian 		/* we don't know this interface, frontend can delete it */
5714a78c7cfSflorian 		engine_imsg_compose_frontend(IMSG_REMOVE_IF, 0,
5724a78c7cfSflorian 		    &if_index, sizeof(if_index));
5734a78c7cfSflorian 		return;
5744a78c7cfSflorian 	}
5754a78c7cfSflorian 
5764a78c7cfSflorian 	send_ra.if_index = engine_iface->if_index;
5774a78c7cfSflorian 	memcpy(&send_ra.to, &all_nodes, sizeof(send_ra.to));
5784a78c7cfSflorian 
5794a78c7cfSflorian 	TAILQ_REMOVE(&engine_interfaces, engine_iface, entry);
5804a78c7cfSflorian 	evtimer_del(&engine_iface->timer);
5814a78c7cfSflorian 
5824a78c7cfSflorian 	if (if_indextoname(if_index, if_name) != NULL)
5834a78c7cfSflorian 		engine_imsg_compose_frontend(IMSG_SEND_RA, 0, &send_ra,
5844a78c7cfSflorian 		    sizeof(send_ra));
5854a78c7cfSflorian 	engine_imsg_compose_frontend(IMSG_REMOVE_IF, 0,
5864a78c7cfSflorian 	    &engine_iface->if_index, sizeof(engine_iface->if_index));
5874a78c7cfSflorian 	free(engine_iface);
5884a78c7cfSflorian }
5894a78c7cfSflorian 
5904a78c7cfSflorian void
5910c40990eSflorian iface_timeout(int fd, short events, void *arg)
5920c40990eSflorian {
5930c40990eSflorian 	struct engine_iface	*engine_iface = (struct engine_iface *)arg;
5940c40990eSflorian 	struct imsg_send_ra	 send_ra;
5950c40990eSflorian 	struct timeval		 tv;
5960c40990eSflorian 
5970c40990eSflorian 	tv.tv_sec = MIN_RTR_ADV_INTERVAL +
5980c40990eSflorian 	    arc4random_uniform(MAX_RTR_ADV_INTERVAL - MIN_RTR_ADV_INTERVAL);
5990c40990eSflorian 	tv.tv_usec = arc4random_uniform(1000000);
6000c40990eSflorian 
6010c40990eSflorian 	log_debug("%s new timeout in %lld", __func__, tv.tv_sec);
6020c40990eSflorian 
6030c40990eSflorian 	evtimer_add(&engine_iface->timer, &tv);
6040c40990eSflorian 
6050c40990eSflorian 	send_ra.if_index = engine_iface->if_index;
6060c40990eSflorian 	memcpy(&send_ra.to, &all_nodes, sizeof(send_ra.to));
6070c40990eSflorian 	engine_imsg_compose_frontend(IMSG_SEND_RA, 0, &send_ra,
6080c40990eSflorian 	    sizeof(send_ra));
6090c40990eSflorian }
610