xref: /openbsd/usr.sbin/rad/rad.c (revision 905646f0)
1 /*	$OpenBSD: rad.c,v 1.23 2020/06/26 19:00:08 bket Exp $	*/
2 
3 /*
4  * Copyright (c) 2018 Florian Obser <florian@openbsd.org>
5  * Copyright (c) 2005 Claudio Jeker <claudio@openbsd.org>
6  * Copyright (c) 2004 Esben Norby <norby@openbsd.org>
7  * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
8  *
9  * Permission to use, copy, modify, and distribute this software for any
10  * purpose with or without fee is hereby granted, provided that the above
11  * copyright notice and this permission notice appear in all copies.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20  */
21 #include <sys/types.h>
22 #include <sys/queue.h>
23 #include <sys/socket.h>
24 #include <sys/syslog.h>
25 #include <sys/uio.h>
26 #include <sys/wait.h>
27 
28 #include <netinet/in.h>
29 #include <net/if.h>
30 #include <net/route.h>
31 #include <netinet/in.h>
32 #include <netinet/if_ether.h>
33 #include <netinet6/in6_var.h>
34 #include <netinet/icmp6.h>
35 
36 #include <err.h>
37 #include <errno.h>
38 #include <event.h>
39 #include <fcntl.h>
40 #include <imsg.h>
41 #include <netdb.h>
42 #include <pwd.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <signal.h>
47 #include <unistd.h>
48 
49 #include "log.h"
50 #include "rad.h"
51 #include "frontend.h"
52 #include "engine.h"
53 #include "control.h"
54 
55 __dead void	usage(void);
56 __dead void	main_shutdown(void);
57 
58 void	main_sig_handler(int, short, void *);
59 
60 static pid_t	start_child(int, char *, int, int, int);
61 
62 void	main_dispatch_frontend(int, short, void *);
63 void	main_dispatch_engine(int, short, void *);
64 
65 static int	main_imsg_send_ipc_sockets(struct imsgbuf *, struct imsgbuf *);
66 static int	main_imsg_send_config(struct rad_conf *);
67 
68 int	main_reload(void);
69 int	main_sendboth(enum imsg_type, void *, uint16_t);
70 
71 void	in6_prefixlen2mask(struct in6_addr *, int len);
72 
73 struct rad_conf	*main_conf;
74 struct imsgev		*iev_frontend;
75 struct imsgev		*iev_engine;
76 char			*conffile;
77 
78 pid_t	 frontend_pid;
79 pid_t	 engine_pid;
80 
81 uint32_t cmd_opts;
82 
83 void
84 main_sig_handler(int sig, short event, void *arg)
85 {
86 	/*
87 	 * Normal signal handler rules don't apply because libevent
88 	 * decouples for us.
89 	 */
90 
91 	switch (sig) {
92 	case SIGTERM:
93 	case SIGINT:
94 		main_shutdown();
95 		break;
96 	case SIGHUP:
97 		if (main_reload() == -1)
98 			log_warnx("configuration reload failed");
99 		else
100 			log_debug("configuration reloaded");
101 		break;
102 	default:
103 		fatalx("unexpected signal");
104 	}
105 }
106 
107 __dead void
108 usage(void)
109 {
110 	extern char *__progname;
111 
112 	fprintf(stderr, "usage: %s [-dnv] [-f file] [-s socket]\n",
113 	    __progname);
114 	exit(1);
115 }
116 
117 int
118 main(int argc, char *argv[])
119 {
120 	struct event		 ev_sigint, ev_sigterm, ev_sighup;
121 	struct icmp6_filter	 filt;
122 	int			 ch;
123 	int			 debug = 0, engine_flag = 0, frontend_flag = 0;
124 	char			*saved_argv0;
125 	int			 pipe_main2frontend[2];
126 	int			 pipe_main2engine[2];
127 	int			 icmp6sock, on = 1, off = 0;
128 	int			 frontend_routesock, rtfilter;
129 	int			 control_fd;
130 	char			*csock;
131 
132 	conffile = CONF_FILE;
133 	csock = RAD_SOCKET;
134 
135 	log_init(1, LOG_DAEMON);	/* Log to stderr until daemonized. */
136 	log_setverbose(1);
137 
138 	saved_argv0 = argv[0];
139 	if (saved_argv0 == NULL)
140 		saved_argv0 = "rad";
141 
142 	while ((ch = getopt(argc, argv, "dEFf:ns:v")) != -1) {
143 		switch (ch) {
144 		case 'd':
145 			debug = 1;
146 			break;
147 		case 'E':
148 			engine_flag = 1;
149 			break;
150 		case 'F':
151 			frontend_flag = 1;
152 			break;
153 		case 'f':
154 			conffile = optarg;
155 			break;
156 		case 'n':
157 			cmd_opts |= OPT_NOACTION;
158 			break;
159 		case 's':
160 			csock = optarg;
161 			break;
162 		case 'v':
163 			if (cmd_opts & OPT_VERBOSE)
164 				cmd_opts |= OPT_VERBOSE2;
165 			cmd_opts |= OPT_VERBOSE;
166 			break;
167 		default:
168 			usage();
169 		}
170 	}
171 
172 	argc -= optind;
173 	argv += optind;
174 	if (argc > 0 || (engine_flag && frontend_flag))
175 		usage();
176 
177 	if (engine_flag)
178 		engine(debug, cmd_opts & OPT_VERBOSE);
179 	else if (frontend_flag)
180 		frontend(debug, cmd_opts & OPT_VERBOSE);
181 
182 	/* parse config file */
183 	if ((main_conf = parse_config(conffile)) == NULL) {
184 		exit(1);
185 	}
186 
187 	if (cmd_opts & OPT_NOACTION) {
188 		if (cmd_opts & OPT_VERBOSE)
189 			print_config(main_conf);
190 		else
191 			fprintf(stderr, "configuration OK\n");
192 		exit(0);
193 	}
194 
195 	/* Check for root privileges. */
196 	if (geteuid())
197 		errx(1, "need root privileges");
198 
199 	/* Check for assigned daemon user */
200 	if (getpwnam(RAD_USER) == NULL)
201 		errx(1, "unknown user %s", RAD_USER);
202 
203 	log_init(debug, LOG_DAEMON);
204 	log_setverbose(cmd_opts & OPT_VERBOSE);
205 
206 	if (!debug)
207 		daemon(1, 0);
208 
209 	log_info("startup");
210 
211 	if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
212 	    PF_UNSPEC, pipe_main2frontend) == -1)
213 		fatal("main2frontend socketpair");
214 	if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
215 	    PF_UNSPEC, pipe_main2engine) == -1)
216 		fatal("main2engine socketpair");
217 
218 	/* Start children. */
219 	engine_pid = start_child(PROC_ENGINE, saved_argv0, pipe_main2engine[1],
220 	    debug, cmd_opts & OPT_VERBOSE);
221 	frontend_pid = start_child(PROC_FRONTEND, saved_argv0,
222 	    pipe_main2frontend[1], debug, cmd_opts & OPT_VERBOSE);
223 
224 	rad_process = PROC_MAIN;
225 	log_procinit(log_procnames[rad_process]);
226 
227 	event_init();
228 
229 	/* Setup signal handler. */
230 	signal_set(&ev_sigint, SIGINT, main_sig_handler, NULL);
231 	signal_set(&ev_sigterm, SIGTERM, main_sig_handler, NULL);
232 	signal_set(&ev_sighup, SIGHUP, main_sig_handler, NULL);
233 	signal_add(&ev_sigint, NULL);
234 	signal_add(&ev_sigterm, NULL);
235 	signal_add(&ev_sighup, NULL);
236 	signal(SIGPIPE, SIG_IGN);
237 
238 	/* Setup pipes to children. */
239 
240 	if ((iev_frontend = malloc(sizeof(struct imsgev))) == NULL ||
241 	    (iev_engine = malloc(sizeof(struct imsgev))) == NULL)
242 		fatal(NULL);
243 	imsg_init(&iev_frontend->ibuf, pipe_main2frontend[0]);
244 	iev_frontend->handler = main_dispatch_frontend;
245 	imsg_init(&iev_engine->ibuf, pipe_main2engine[0]);
246 	iev_engine->handler = main_dispatch_engine;
247 
248 	/* Setup event handlers for pipes to engine & frontend. */
249 	iev_frontend->events = EV_READ;
250 	event_set(&iev_frontend->ev, iev_frontend->ibuf.fd,
251 	    iev_frontend->events, iev_frontend->handler, iev_frontend);
252 	event_add(&iev_frontend->ev, NULL);
253 
254 	iev_engine->events = EV_READ;
255 	event_set(&iev_engine->ev, iev_engine->ibuf.fd, iev_engine->events,
256 	    iev_engine->handler, iev_engine);
257 	event_add(&iev_engine->ev, NULL);
258 
259 	if (main_imsg_send_ipc_sockets(&iev_frontend->ibuf, &iev_engine->ibuf))
260 		fatal("could not establish imsg links");
261 
262 	if ((icmp6sock = socket(AF_INET6, SOCK_RAW | SOCK_CLOEXEC,
263 	    IPPROTO_ICMPV6)) == -1)
264 		fatal("ICMPv6 socket");
265 
266 	if (setsockopt(icmp6sock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on,
267 	    sizeof(on)) == -1)
268 		fatal("IPV6_RECVPKTINFO");
269 
270 	if (setsockopt(icmp6sock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &on,
271 	    sizeof(on)) == -1)
272 		fatal("IPV6_RECVHOPLIMIT");
273 
274 	if (setsockopt(icmp6sock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &off,
275 	    sizeof(off)) == -1)
276 		fatal("IPV6_RECVHOPLIMIT");
277 
278 	/* only router advertisements and solicitations */
279 	ICMP6_FILTER_SETBLOCKALL(&filt);
280 	ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filt);
281 	ICMP6_FILTER_SETPASS(ND_ROUTER_SOLICIT, &filt);
282 	if (setsockopt(icmp6sock, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
283 	    sizeof(filt)) == -1)
284 		fatal("ICMP6_FILTER");
285 
286 	if ((frontend_routesock = socket(AF_ROUTE, SOCK_RAW | SOCK_CLOEXEC,
287 	    AF_INET6)) == -1)
288 		fatal("route socket");
289 
290 	rtfilter = ROUTE_FILTER(RTM_IFINFO) | ROUTE_FILTER(RTM_NEWADDR) |
291 	    ROUTE_FILTER(RTM_DELADDR);
292 	if (setsockopt(frontend_routesock, AF_ROUTE, ROUTE_MSGFILTER,
293 	    &rtfilter, sizeof(rtfilter)) == -1)
294 		fatal("setsockopt(ROUTE_MSGFILTER)");
295 
296 	if ((control_fd = control_init(csock)) == -1)
297 		fatalx("control socket setup failed");
298 
299 	main_imsg_compose_frontend_fd(IMSG_ICMP6SOCK, 0, icmp6sock);
300 	main_imsg_compose_frontend_fd(IMSG_ROUTESOCK, 0, frontend_routesock);
301 	main_imsg_compose_frontend_fd(IMSG_CONTROLFD, 0, control_fd);
302 	main_imsg_send_config(main_conf);
303 
304 	if (pledge("stdio rpath sendfd", NULL) == -1)
305 		fatal("pledge");
306 
307 	main_imsg_compose_frontend(IMSG_STARTUP, 0, NULL, 0);
308 
309 	event_dispatch();
310 
311 	main_shutdown();
312 	return (0);
313 }
314 
315 __dead void
316 main_shutdown(void)
317 {
318 	pid_t	 pid;
319 	int	 status;
320 
321 	/* Close pipes. */
322 	msgbuf_clear(&iev_frontend->ibuf.w);
323 	close(iev_frontend->ibuf.fd);
324 	msgbuf_clear(&iev_engine->ibuf.w);
325 	close(iev_engine->ibuf.fd);
326 
327 	config_clear(main_conf);
328 
329 	log_debug("waiting for children to terminate");
330 	do {
331 		pid = wait(&status);
332 		if (pid == -1) {
333 			if (errno != EINTR && errno != ECHILD)
334 				fatal("wait");
335 		} else if (WIFSIGNALED(status))
336 			log_warnx("%s terminated; signal %d",
337 			    (pid == engine_pid) ? "engine" :
338 			    "frontend", WTERMSIG(status));
339 	} while (pid != -1 || (pid == -1 && errno == EINTR));
340 
341 	free(iev_frontend);
342 	free(iev_engine);
343 
344 	log_info("terminating");
345 	exit(0);
346 }
347 
348 static pid_t
349 start_child(int p, char *argv0, int fd, int debug, int verbose)
350 {
351 	char	*argv[6];
352 	int	 argc = 0;
353 	pid_t	 pid;
354 
355 	switch (pid = fork()) {
356 	case -1:
357 		fatal("cannot fork");
358 	case 0:
359 		break;
360 	default:
361 		close(fd);
362 		return (pid);
363 	}
364 
365 	if (fd != 3) {
366 		if (dup2(fd, 3) == -1)
367 			fatal("cannot setup imsg fd");
368 	} else if (fcntl(fd, F_SETFD, 0) == -1)
369 		fatal("cannot setup imsg fd");
370 
371 	argv[argc++] = argv0;
372 	switch (p) {
373 	case PROC_MAIN:
374 		fatalx("Can not start main process");
375 	case PROC_ENGINE:
376 		argv[argc++] = "-E";
377 		break;
378 	case PROC_FRONTEND:
379 		argv[argc++] = "-F";
380 		break;
381 	}
382 	if (debug)
383 		argv[argc++] = "-d";
384 	if (verbose)
385 		argv[argc++] = "-v";
386 	argv[argc++] = NULL;
387 
388 	execvp(argv0, argv);
389 	fatal("execvp");
390 }
391 
392 void
393 main_dispatch_frontend(int fd, short event, void *bula)
394 {
395 	struct imsgev		*iev = bula;
396 	struct imsgbuf		*ibuf;
397 	struct imsg		 imsg;
398 	ssize_t			 n;
399 	int			 shut = 0, verbose;
400 
401 	ibuf = &iev->ibuf;
402 
403 	if (event & EV_READ) {
404 		if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
405 			fatal("imsg_read error");
406 		if (n == 0)	/* Connection closed. */
407 			shut = 1;
408 	}
409 	if (event & EV_WRITE) {
410 		if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
411 			fatal("msgbuf_write");
412 		if (n == 0)	/* Connection closed. */
413 			shut = 1;
414 	}
415 
416 	for (;;) {
417 		if ((n = imsg_get(ibuf, &imsg)) == -1)
418 			fatal("imsg_get");
419 		if (n == 0)	/* No more messages. */
420 			break;
421 
422 		switch (imsg.hdr.type) {
423 		case IMSG_STARTUP_DONE:
424 			if (pledge("stdio rpath", NULL) == -1)
425 				fatal("pledge");
426 			break;
427 		case IMSG_CTL_RELOAD:
428 			if (main_reload() == -1)
429 				log_warnx("configuration reload failed");
430 			else
431 				log_warnx("configuration reloaded");
432 			break;
433 		case IMSG_CTL_LOG_VERBOSE:
434 			if (IMSG_DATA_SIZE(imsg) != sizeof(verbose))
435 				fatalx("%s: IMSG_CTL_LOG_VERBOSE wrong length: "
436 				    "%lu", __func__, IMSG_DATA_SIZE(imsg));
437 			memcpy(&verbose, imsg.data, sizeof(verbose));
438 			log_setverbose(verbose);
439 			break;
440 		default:
441 			log_debug("%s: error handling imsg %d", __func__,
442 			    imsg.hdr.type);
443 			break;
444 		}
445 		imsg_free(&imsg);
446 	}
447 	if (!shut)
448 		imsg_event_add(iev);
449 	else {
450 		/* This pipe is dead. Remove its event handler */
451 		event_del(&iev->ev);
452 		event_loopexit(NULL);
453 	}
454 }
455 
456 void
457 main_dispatch_engine(int fd, short event, void *bula)
458 {
459 	struct imsgev	*iev = bula;
460 	struct imsgbuf  *ibuf;
461 	struct imsg	 imsg;
462 	ssize_t		 n;
463 	int		 shut = 0;
464 
465 	ibuf = &iev->ibuf;
466 
467 	if (event & EV_READ) {
468 		if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
469 			fatal("imsg_read error");
470 		if (n == 0)	/* Connection closed. */
471 			shut = 1;
472 	}
473 	if (event & EV_WRITE) {
474 		if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
475 			fatal("msgbuf_write");
476 		if (n == 0)	/* Connection closed. */
477 			shut = 1;
478 	}
479 
480 	for (;;) {
481 		if ((n = imsg_get(ibuf, &imsg)) == -1)
482 			fatal("imsg_get");
483 		if (n == 0)	/* No more messages. */
484 			break;
485 
486 		switch (imsg.hdr.type) {
487 		default:
488 			log_debug("%s: error handling imsg %d", __func__,
489 			    imsg.hdr.type);
490 			break;
491 		}
492 		imsg_free(&imsg);
493 	}
494 	if (!shut)
495 		imsg_event_add(iev);
496 	else {
497 		/* This pipe is dead. Remove its event handler. */
498 		event_del(&iev->ev);
499 		event_loopexit(NULL);
500 	}
501 }
502 
503 void
504 main_imsg_compose_frontend(int type, pid_t pid, void *data, uint16_t datalen)
505 {
506 	if (iev_frontend)
507 		imsg_compose_event(iev_frontend, type, 0, pid, -1, data,
508 		    datalen);
509 }
510 
511 void
512 main_imsg_compose_frontend_fd(int type, pid_t pid, int fd)
513 {
514 	if (iev_frontend)
515 		imsg_compose_event(iev_frontend, type, 0, pid, fd, NULL, 0);
516 }
517 
518 
519 void
520 main_imsg_compose_engine(int type, pid_t pid, void *data, uint16_t datalen)
521 {
522 	if (iev_engine)
523 		imsg_compose_event(iev_engine, type, 0, pid, -1, data,
524 		    datalen);
525 }
526 
527 void
528 imsg_event_add(struct imsgev *iev)
529 {
530 	iev->events = EV_READ;
531 	if (iev->ibuf.w.queued)
532 		iev->events |= EV_WRITE;
533 
534 	event_del(&iev->ev);
535 	event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
536 	event_add(&iev->ev, NULL);
537 }
538 
539 int
540 imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
541     pid_t pid, int fd, void *data, uint16_t datalen)
542 {
543 	int	ret;
544 
545 	if ((ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data,
546 	    datalen)) != -1)
547 		imsg_event_add(iev);
548 
549 	return (ret);
550 }
551 
552 static int
553 main_imsg_send_ipc_sockets(struct imsgbuf *frontend_buf,
554     struct imsgbuf *engine_buf)
555 {
556 	int pipe_frontend2engine[2];
557 
558 	if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
559 	    PF_UNSPEC, pipe_frontend2engine) == -1)
560 		return (-1);
561 
562 	if (imsg_compose(frontend_buf, IMSG_SOCKET_IPC, 0, 0,
563 	    pipe_frontend2engine[0], NULL, 0) == -1)
564 		return (-1);
565 	if (imsg_compose(engine_buf, IMSG_SOCKET_IPC, 0, 0,
566 	    pipe_frontend2engine[1], NULL, 0) == -1)
567 		return (-1);
568 
569 	return (0);
570 }
571 
572 int
573 main_reload(void)
574 {
575 	struct rad_conf *xconf;
576 
577 	if ((xconf = parse_config(conffile)) == NULL)
578 		return (-1);
579 
580 	if (main_imsg_send_config(xconf) == -1)
581 		return (-1);
582 
583 	merge_config(main_conf, xconf);
584 
585 	return (0);
586 }
587 
588 int
589 main_imsg_send_config(struct rad_conf *xconf)
590 {
591 	struct ra_iface_conf	*ra_iface_conf;
592 	struct ra_prefix_conf	*ra_prefix_conf;
593 	struct ra_rdnss_conf	*ra_rdnss_conf;
594 	struct ra_dnssl_conf	*ra_dnssl_conf;
595 
596 	/* Send fixed part of config to children. */
597 	if (main_sendboth(IMSG_RECONF_CONF, xconf, sizeof(*xconf)) == -1)
598 		return (-1);
599 
600 	/* send global dns options to children */
601 	SIMPLEQ_FOREACH(ra_rdnss_conf, &xconf->ra_options.ra_rdnss_list,
602 	    entry) {
603 		if (main_sendboth(IMSG_RECONF_RA_RDNSS, ra_rdnss_conf,
604 		    sizeof(*ra_rdnss_conf)) == -1)
605 			return (-1);
606 	}
607 	SIMPLEQ_FOREACH(ra_dnssl_conf, &xconf->ra_options.ra_dnssl_list,
608 	    entry) {
609 		if (main_sendboth(IMSG_RECONF_RA_DNSSL, ra_dnssl_conf,
610 		    sizeof(*ra_dnssl_conf)) == -1)
611 			return (-1);
612 	}
613 
614 	/* Send the interface list to children. */
615 	SIMPLEQ_FOREACH(ra_iface_conf, &xconf->ra_iface_list, entry) {
616 		if (main_sendboth(IMSG_RECONF_RA_IFACE, ra_iface_conf,
617 		    sizeof(*ra_iface_conf)) == -1)
618 			return (-1);
619 		if (ra_iface_conf->autoprefix) {
620 			if (main_sendboth(IMSG_RECONF_RA_AUTOPREFIX,
621 			    ra_iface_conf->autoprefix,
622 			    sizeof(*ra_iface_conf->autoprefix)) == -1)
623 				return (-1);
624 		}
625 		SIMPLEQ_FOREACH(ra_prefix_conf, &ra_iface_conf->ra_prefix_list,
626 		    entry) {
627 			if (main_sendboth(IMSG_RECONF_RA_PREFIX,
628 			    ra_prefix_conf, sizeof(*ra_prefix_conf)) == -1)
629 				return (-1);
630 		}
631 		SIMPLEQ_FOREACH(ra_rdnss_conf,
632 		    &ra_iface_conf->ra_options.ra_rdnss_list, entry) {
633 			if (main_sendboth(IMSG_RECONF_RA_RDNSS, ra_rdnss_conf,
634 			    sizeof(*ra_rdnss_conf)) == -1)
635 				return (-1);
636 		}
637 		SIMPLEQ_FOREACH(ra_dnssl_conf,
638 		    &ra_iface_conf->ra_options.ra_dnssl_list, entry) {
639 			if (main_sendboth(IMSG_RECONF_RA_DNSSL, ra_dnssl_conf,
640 			    sizeof(*ra_dnssl_conf)) == -1)
641 				return (-1);
642 		}
643 	}
644 
645 	/* Tell children the revised config is now complete. */
646 	if (main_sendboth(IMSG_RECONF_END, NULL, 0) == -1)
647 		return (-1);
648 
649 	return (0);
650 }
651 
652 int
653 main_sendboth(enum imsg_type type, void *buf, uint16_t len)
654 {
655 	if (imsg_compose_event(iev_frontend, type, 0, 0, -1, buf, len) == -1)
656 		return (-1);
657 	if (imsg_compose_event(iev_engine, type, 0, 0, -1, buf, len) == -1)
658 		return (-1);
659 	return (0);
660 }
661 
662 void
663 free_ra_iface_conf(struct ra_iface_conf *ra_iface_conf)
664 {
665 	struct ra_prefix_conf	*prefix;
666 
667 	if (!ra_iface_conf)
668 		return;
669 
670 	free(ra_iface_conf->autoprefix);
671 
672 	while ((prefix = SIMPLEQ_FIRST(&ra_iface_conf->ra_prefix_list)) !=
673 	    NULL) {
674 		SIMPLEQ_REMOVE_HEAD(&ra_iface_conf->ra_prefix_list, entry);
675 		free(prefix);
676 	}
677 
678 	free_dns_options(&ra_iface_conf->ra_options);
679 
680 	free(ra_iface_conf);
681 }
682 
683 void
684 free_dns_options(struct ra_options_conf *ra_options)
685 {
686 	struct ra_rdnss_conf	*ra_rdnss;
687 	struct ra_dnssl_conf	*ra_dnssl;
688 
689 	while ((ra_rdnss = SIMPLEQ_FIRST(&ra_options->ra_rdnss_list)) != NULL) {
690 		SIMPLEQ_REMOVE_HEAD(&ra_options->ra_rdnss_list, entry);
691 		free(ra_rdnss);
692 	}
693 	ra_options->rdnss_count = 0;
694 
695 	while ((ra_dnssl = SIMPLEQ_FIRST(&ra_options->ra_dnssl_list)) != NULL) {
696 		SIMPLEQ_REMOVE_HEAD(&ra_options->ra_dnssl_list, entry);
697 		free(ra_dnssl);
698 	}
699 	ra_options->dnssl_len = 0;
700 }
701 
702 void
703 merge_config(struct rad_conf *conf, struct rad_conf *xconf)
704 {
705 	struct ra_iface_conf	*ra_iface_conf;
706 
707 	/* Remove & discard existing interfaces. */
708 	while ((ra_iface_conf = SIMPLEQ_FIRST(&conf->ra_iface_list)) != NULL) {
709 		SIMPLEQ_REMOVE_HEAD(&conf->ra_iface_list, entry);
710 		free_ra_iface_conf(ra_iface_conf);
711 	}
712 	free_dns_options(&conf->ra_options);
713 
714 	conf->ra_options = xconf->ra_options;
715 	SIMPLEQ_INIT(&conf->ra_options.ra_rdnss_list);
716 	SIMPLEQ_INIT(&conf->ra_options.ra_dnssl_list);
717 
718 	/* Add new interfaces. */
719 	SIMPLEQ_CONCAT(&conf->ra_iface_list, &xconf->ra_iface_list);
720 
721 	/* Add dns options */
722 	SIMPLEQ_CONCAT(&conf->ra_options.ra_rdnss_list,
723 	    &xconf->ra_options.ra_rdnss_list);
724 	SIMPLEQ_CONCAT(&conf->ra_options.ra_dnssl_list,
725 	    &xconf->ra_options.ra_dnssl_list);
726 	free(xconf);
727 }
728 
729 struct rad_conf *
730 config_new_empty(void)
731 {
732 	struct rad_conf	*xconf;
733 
734 	xconf = calloc(1, sizeof(*xconf));
735 	if (xconf == NULL)
736 		fatal(NULL);
737 
738 	SIMPLEQ_INIT(&xconf->ra_iface_list);
739 
740 	xconf->ra_options.dfr = 1;
741 	xconf->ra_options.cur_hl = 0;
742 	xconf->ra_options.m_flag = 0;
743 	xconf->ra_options.o_flag = 0;
744 	xconf->ra_options.router_lifetime = ADV_DEFAULT_LIFETIME;
745 	xconf->ra_options.reachable_time = 0;
746 	xconf->ra_options.retrans_timer = 0;
747 	xconf->ra_options.mtu = 0;
748 	xconf->ra_options.rdns_lifetime = DEFAULT_RDNS_LIFETIME;
749 	SIMPLEQ_INIT(&xconf->ra_options.ra_rdnss_list);
750 	SIMPLEQ_INIT(&xconf->ra_options.ra_dnssl_list);
751 
752 	return (xconf);
753 }
754 
755 void
756 config_clear(struct rad_conf *conf)
757 {
758 	struct rad_conf	*xconf;
759 
760 	/* Merge current config with an empty config. */
761 	xconf = config_new_empty();
762 	merge_config(conf, xconf);
763 
764 	free(conf);
765 }
766 
767 void
768 mask_prefix(struct in6_addr* in6, int len)
769 {
770 	uint8_t	bitmask[8] = {0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe};
771 	int	i, skip;
772 
773 	if (len < 0 || len > 128)
774 		fatalx("invalid prefix length: %d", len);
775 
776 	skip = len / 8;
777 
778 	if (skip < 16)
779 		in6->s6_addr[skip] &= bitmask[len % 8];
780 
781 	for (i = skip + 1; i < 16; i++)
782 		in6->s6_addr[i] = 0;
783 }
784 
785 const char*
786 sin6_to_str(struct sockaddr_in6 *sin6)
787 {
788 	static char hbuf[NI_MAXHOST];
789 	int error;
790 
791 	error = getnameinfo((struct sockaddr *)sin6, sin6->sin6_len, hbuf,
792 	    sizeof(hbuf), NULL, 0, NI_NUMERICHOST | NI_NUMERICSERV);
793 	if (error) {
794 		log_warnx("%s", gai_strerror(error));
795 		strlcpy(hbuf, "unknown", sizeof(hbuf));
796 	}
797 	return hbuf;
798 }
799 
800 const char*
801 in6_to_str(struct in6_addr *in6)
802 {
803 
804 	struct sockaddr_in6	sin6;
805 
806 	memset(&sin6, 0, sizeof(sin6));
807 	sin6.sin6_len = sizeof(sin6);
808 	sin6.sin6_family = AF_INET6;
809 	sin6.sin6_addr = *in6;
810 
811 	return (sin6_to_str(&sin6));
812 }
813