xref: /openbsd-src/usr.sbin/rad/rad.c (revision f1b790a5738b7375271fee81f99119b1f82f2cfd)
1*f1b790a5Sclaudio /*	$OpenBSD: rad.c,v 1.38 2024/11/21 13:38:15 claudio Exp $	*/
253293e44Sflorian 
353293e44Sflorian /*
453293e44Sflorian  * Copyright (c) 2018 Florian Obser <florian@openbsd.org>
553293e44Sflorian  * Copyright (c) 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 #include <sys/types.h>
2253293e44Sflorian #include <sys/queue.h>
2353293e44Sflorian #include <sys/socket.h>
2453293e44Sflorian #include <sys/syslog.h>
2553293e44Sflorian #include <sys/uio.h>
2653293e44Sflorian #include <sys/wait.h>
2753293e44Sflorian 
2853293e44Sflorian #include <netinet/in.h>
2953293e44Sflorian #include <net/if.h>
3048e174fdSflorian #include <net/route.h>
3153293e44Sflorian #include <netinet/if_ether.h>
3253293e44Sflorian #include <netinet6/in6_var.h>
3353293e44Sflorian #include <netinet/icmp6.h>
3453293e44Sflorian 
3553293e44Sflorian #include <err.h>
3653293e44Sflorian #include <errno.h>
3753293e44Sflorian #include <event.h>
38ef4f5895Syasuoka #include <fcntl.h>
3953293e44Sflorian #include <imsg.h>
4053293e44Sflorian #include <netdb.h>
4153293e44Sflorian #include <pwd.h>
4253293e44Sflorian #include <stdio.h>
4353293e44Sflorian #include <stdlib.h>
4453293e44Sflorian #include <string.h>
4553293e44Sflorian #include <signal.h>
4653293e44Sflorian #include <unistd.h>
4753293e44Sflorian 
4853293e44Sflorian #include "log.h"
4953293e44Sflorian #include "rad.h"
5053293e44Sflorian #include "frontend.h"
5153293e44Sflorian #include "engine.h"
5253293e44Sflorian #include "control.h"
5353293e44Sflorian 
543c6be478Sflorian enum rad_process {
553c6be478Sflorian 	PROC_MAIN,
563c6be478Sflorian 	PROC_ENGINE,
573c6be478Sflorian 	PROC_FRONTEND
583c6be478Sflorian };
593c6be478Sflorian 
6053293e44Sflorian __dead void	usage(void);
6153293e44Sflorian __dead void	main_shutdown(void);
6253293e44Sflorian 
6353293e44Sflorian void	main_sig_handler(int, short, void *);
6453293e44Sflorian 
653c6be478Sflorian static pid_t	start_child(enum rad_process, char *, int, int, int);
6653293e44Sflorian 
6753293e44Sflorian void	main_dispatch_frontend(int, short, void *);
6853293e44Sflorian void	main_dispatch_engine(int, short, void *);
69e88dba76Sflorian void	open_icmp6sock(int);
7053293e44Sflorian 
7153293e44Sflorian static int	main_imsg_send_ipc_sockets(struct imsgbuf *, struct imsgbuf *);
7253293e44Sflorian static int	main_imsg_send_config(struct rad_conf *);
7353293e44Sflorian 
7453293e44Sflorian int	main_reload(void);
7553293e44Sflorian int	main_sendboth(enum imsg_type, void *, uint16_t);
7653293e44Sflorian 
7753293e44Sflorian struct rad_conf		*main_conf;
78032fa683Sflorian static struct imsgev	*iev_frontend;
79032fa683Sflorian static struct imsgev	*iev_engine;
8053293e44Sflorian char			*conffile;
8153293e44Sflorian pid_t			 frontend_pid;
8253293e44Sflorian pid_t			 engine_pid;
8353293e44Sflorian uint32_t		 cmd_opts;
8453293e44Sflorian 
8553293e44Sflorian void
8653293e44Sflorian main_sig_handler(int sig, short event, void *arg)
8753293e44Sflorian {
8853293e44Sflorian 	/*
8953293e44Sflorian 	 * Normal signal handler rules don't apply because libevent
9053293e44Sflorian 	 * decouples for us.
9153293e44Sflorian 	 */
9253293e44Sflorian 
9353293e44Sflorian 	switch (sig) {
9453293e44Sflorian 	case SIGTERM:
9553293e44Sflorian 	case SIGINT:
96e95cbeb2Sflorian 		main_shutdown();
9792b9e3a9Sflorian 		break;
9853293e44Sflorian 	case SIGHUP:
9953293e44Sflorian 		if (main_reload() == -1)
10053293e44Sflorian 			log_warnx("configuration reload failed");
10153293e44Sflorian 		else
10253293e44Sflorian 			log_debug("configuration reloaded");
10353293e44Sflorian 		break;
10453293e44Sflorian 	default:
10553293e44Sflorian 		fatalx("unexpected signal");
10653293e44Sflorian 	}
10753293e44Sflorian }
10853293e44Sflorian 
10953293e44Sflorian __dead void
11053293e44Sflorian usage(void)
11153293e44Sflorian {
11253293e44Sflorian 	extern char *__progname;
11353293e44Sflorian 
11453293e44Sflorian 	fprintf(stderr, "usage: %s [-dnv] [-f file] [-s socket]\n",
11553293e44Sflorian 	    __progname);
11653293e44Sflorian 	exit(1);
11753293e44Sflorian }
11853293e44Sflorian 
11953293e44Sflorian int
12053293e44Sflorian main(int argc, char *argv[])
12153293e44Sflorian {
12253293e44Sflorian 	struct event		 ev_sigint, ev_sigterm, ev_sighup;
12353293e44Sflorian 	int			 ch;
12453293e44Sflorian 	int			 debug = 0, engine_flag = 0, frontend_flag = 0;
12553293e44Sflorian 	char			*saved_argv0;
12653293e44Sflorian 	int			 pipe_main2frontend[2];
12753293e44Sflorian 	int			 pipe_main2engine[2];
12848e174fdSflorian 	int			 frontend_routesock, rtfilter;
129e88dba76Sflorian 	int			 rtable_any = RTABLE_ANY;
130a778af8bSflorian 	int			 control_fd;
1311258db73Sflorian 	char			*csock;
13253293e44Sflorian 
133e0daf9a7Sflorian 	conffile = _PATH_CONF_FILE;
134e0daf9a7Sflorian 	csock = _PATH_RAD_SOCKET;
13553293e44Sflorian 
13653293e44Sflorian 	log_init(1, LOG_DAEMON);	/* Log to stderr until daemonized. */
13753293e44Sflorian 	log_setverbose(1);
13853293e44Sflorian 
13953293e44Sflorian 	saved_argv0 = argv[0];
14053293e44Sflorian 	if (saved_argv0 == NULL)
14153293e44Sflorian 		saved_argv0 = "rad";
14253293e44Sflorian 
14353293e44Sflorian 	while ((ch = getopt(argc, argv, "dEFf:ns:v")) != -1) {
14453293e44Sflorian 		switch (ch) {
14553293e44Sflorian 		case 'd':
14653293e44Sflorian 			debug = 1;
14753293e44Sflorian 			break;
14853293e44Sflorian 		case 'E':
14953293e44Sflorian 			engine_flag = 1;
15053293e44Sflorian 			break;
15153293e44Sflorian 		case 'F':
15253293e44Sflorian 			frontend_flag = 1;
15353293e44Sflorian 			break;
15453293e44Sflorian 		case 'f':
15553293e44Sflorian 			conffile = optarg;
15653293e44Sflorian 			break;
15753293e44Sflorian 		case 'n':
15853293e44Sflorian 			cmd_opts |= OPT_NOACTION;
15953293e44Sflorian 			break;
16053293e44Sflorian 		case 's':
16153293e44Sflorian 			csock = optarg;
16253293e44Sflorian 			break;
16353293e44Sflorian 		case 'v':
16453293e44Sflorian 			if (cmd_opts & OPT_VERBOSE)
16553293e44Sflorian 				cmd_opts |= OPT_VERBOSE2;
16653293e44Sflorian 			cmd_opts |= OPT_VERBOSE;
16753293e44Sflorian 			break;
16853293e44Sflorian 		default:
16953293e44Sflorian 			usage();
17053293e44Sflorian 		}
17153293e44Sflorian 	}
17253293e44Sflorian 
17353293e44Sflorian 	argc -= optind;
17453293e44Sflorian 	argv += optind;
17553293e44Sflorian 	if (argc > 0 || (engine_flag && frontend_flag))
17653293e44Sflorian 		usage();
17753293e44Sflorian 
17853293e44Sflorian 	if (engine_flag)
17953293e44Sflorian 		engine(debug, cmd_opts & OPT_VERBOSE);
18053293e44Sflorian 	else if (frontend_flag)
181a778af8bSflorian 		frontend(debug, cmd_opts & OPT_VERBOSE);
18253293e44Sflorian 
18353293e44Sflorian 	/* parse config file */
18453293e44Sflorian 	if ((main_conf = parse_config(conffile)) == NULL) {
18553293e44Sflorian 		exit(1);
18653293e44Sflorian 	}
18753293e44Sflorian 
18853293e44Sflorian 	if (cmd_opts & OPT_NOACTION) {
18953293e44Sflorian 		if (cmd_opts & OPT_VERBOSE)
19053293e44Sflorian 			print_config(main_conf);
19153293e44Sflorian 		else
19253293e44Sflorian 			fprintf(stderr, "configuration OK\n");
19353293e44Sflorian 		exit(0);
19453293e44Sflorian 	}
19553293e44Sflorian 
19653293e44Sflorian 	/* Check for root privileges. */
19753293e44Sflorian 	if (geteuid())
19853293e44Sflorian 		errx(1, "need root privileges");
19953293e44Sflorian 
20053293e44Sflorian 	/* Check for assigned daemon user */
20153293e44Sflorian 	if (getpwnam(RAD_USER) == NULL)
20253293e44Sflorian 		errx(1, "unknown user %s", RAD_USER);
20353293e44Sflorian 
20453293e44Sflorian 	log_init(debug, LOG_DAEMON);
20553293e44Sflorian 	log_setverbose(cmd_opts & OPT_VERBOSE);
20653293e44Sflorian 
20753293e44Sflorian 	if (!debug)
20853293e44Sflorian 		daemon(1, 0);
20953293e44Sflorian 
21053293e44Sflorian 	log_info("startup");
21153293e44Sflorian 
21253293e44Sflorian 	if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
21353293e44Sflorian 	    PF_UNSPEC, pipe_main2frontend) == -1)
21453293e44Sflorian 		fatal("main2frontend socketpair");
21553293e44Sflorian 	if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
21653293e44Sflorian 	    PF_UNSPEC, pipe_main2engine) == -1)
21753293e44Sflorian 		fatal("main2engine socketpair");
21853293e44Sflorian 
21953293e44Sflorian 	/* Start children. */
22053293e44Sflorian 	engine_pid = start_child(PROC_ENGINE, saved_argv0, pipe_main2engine[1],
2211258db73Sflorian 	    debug, cmd_opts & OPT_VERBOSE);
22253293e44Sflorian 	frontend_pid = start_child(PROC_FRONTEND, saved_argv0,
2231258db73Sflorian 	    pipe_main2frontend[1], debug, cmd_opts & OPT_VERBOSE);
22453293e44Sflorian 
2253c6be478Sflorian 	log_procinit("main");
22653293e44Sflorian 
22753293e44Sflorian 	event_init();
22853293e44Sflorian 
22953293e44Sflorian 	/* Setup signal handler. */
23053293e44Sflorian 	signal_set(&ev_sigint, SIGINT, main_sig_handler, NULL);
23153293e44Sflorian 	signal_set(&ev_sigterm, SIGTERM, main_sig_handler, NULL);
23253293e44Sflorian 	signal_set(&ev_sighup, SIGHUP, main_sig_handler, NULL);
23353293e44Sflorian 	signal_add(&ev_sigint, NULL);
23453293e44Sflorian 	signal_add(&ev_sigterm, NULL);
23553293e44Sflorian 	signal_add(&ev_sighup, NULL);
23653293e44Sflorian 	signal(SIGPIPE, SIG_IGN);
23753293e44Sflorian 
23853293e44Sflorian 	/* Setup pipes to children. */
23953293e44Sflorian 
24053293e44Sflorian 	if ((iev_frontend = malloc(sizeof(struct imsgev))) == NULL ||
24153293e44Sflorian 	    (iev_engine = malloc(sizeof(struct imsgev))) == NULL)
24253293e44Sflorian 		fatal(NULL);
243*f1b790a5Sclaudio 	if (imsgbuf_init(&iev_frontend->ibuf, pipe_main2frontend[0]) == -1)
244*f1b790a5Sclaudio 		fatal(NULL);
245*f1b790a5Sclaudio 	imsgbuf_allow_fdpass(&iev_frontend->ibuf);
24653293e44Sflorian 	iev_frontend->handler = main_dispatch_frontend;
247*f1b790a5Sclaudio 	if (imsgbuf_init(&iev_engine->ibuf, pipe_main2engine[0]) == -1)
248*f1b790a5Sclaudio 		fatal(NULL);
249*f1b790a5Sclaudio 	imsgbuf_allow_fdpass(&iev_engine->ibuf);
25053293e44Sflorian 	iev_engine->handler = main_dispatch_engine;
25153293e44Sflorian 
25253293e44Sflorian 	/* Setup event handlers for pipes to engine & frontend. */
25353293e44Sflorian 	iev_frontend->events = EV_READ;
25453293e44Sflorian 	event_set(&iev_frontend->ev, iev_frontend->ibuf.fd,
25553293e44Sflorian 	    iev_frontend->events, iev_frontend->handler, iev_frontend);
25653293e44Sflorian 	event_add(&iev_frontend->ev, NULL);
25753293e44Sflorian 
25853293e44Sflorian 	iev_engine->events = EV_READ;
25953293e44Sflorian 	event_set(&iev_engine->ev, iev_engine->ibuf.fd, iev_engine->events,
26053293e44Sflorian 	    iev_engine->handler, iev_engine);
26153293e44Sflorian 	event_add(&iev_engine->ev, NULL);
26253293e44Sflorian 
26353293e44Sflorian 	if (main_imsg_send_ipc_sockets(&iev_frontend->ibuf, &iev_engine->ibuf))
26453293e44Sflorian 		fatal("could not establish imsg links");
26553293e44Sflorian 
266b47fcd70Skrw 	if ((frontend_routesock = socket(AF_ROUTE, SOCK_RAW | SOCK_CLOEXEC,
267df69c215Sderaadt 	    AF_INET6)) == -1)
26848e174fdSflorian 		fatal("route socket");
26948e174fdSflorian 
27048e174fdSflorian 	rtfilter = ROUTE_FILTER(RTM_IFINFO) | ROUTE_FILTER(RTM_NEWADDR) |
271e88dba76Sflorian 	    ROUTE_FILTER(RTM_DELADDR) | ROUTE_FILTER(RTM_CHGADDRATTR);
272b47fcd70Skrw 	if (setsockopt(frontend_routesock, AF_ROUTE, ROUTE_MSGFILTER,
273df69c215Sderaadt 	    &rtfilter, sizeof(rtfilter)) == -1)
27448e174fdSflorian 		fatal("setsockopt(ROUTE_MSGFILTER)");
275e88dba76Sflorian 	if (setsockopt(frontend_routesock, AF_ROUTE, ROUTE_TABLEFILTER,
276e88dba76Sflorian 	    &rtable_any, sizeof(rtable_any)) == -1)
277e88dba76Sflorian 		fatal("setsockopt(ROUTE_TABLEFILTER)");
27848e174fdSflorian 
279a778af8bSflorian 	if ((control_fd = control_init(csock)) == -1)
280a778af8bSflorian 		fatalx("control socket setup failed");
281a778af8bSflorian 
282e88dba76Sflorian 	main_imsg_compose_frontend(IMSG_ROUTESOCK, frontend_routesock,
283e88dba76Sflorian 	    NULL, 0);
284e88dba76Sflorian 	main_imsg_compose_frontend(IMSG_CONTROLFD, control_fd, NULL, 0);
28553293e44Sflorian 	main_imsg_send_config(main_conf);
28653293e44Sflorian 
287e88dba76Sflorian 	if (pledge("stdio inet rpath sendfd mcast wroute", NULL) == -1)
28853293e44Sflorian 		fatal("pledge");
28953293e44Sflorian 
290e88dba76Sflorian 	main_imsg_compose_frontend(IMSG_STARTUP, -1, NULL, 0);
2918015e988Sflorian 
29253293e44Sflorian 	event_dispatch();
29353293e44Sflorian 
29453293e44Sflorian 	main_shutdown();
29553293e44Sflorian 	return (0);
29653293e44Sflorian }
29753293e44Sflorian 
29853293e44Sflorian __dead void
29953293e44Sflorian main_shutdown(void)
30053293e44Sflorian {
30153293e44Sflorian 	pid_t	 pid;
30253293e44Sflorian 	int	 status;
30353293e44Sflorian 
30453293e44Sflorian 	/* Close pipes. */
3059cbf9e90Sclaudio 	imsgbuf_clear(&iev_frontend->ibuf);
30653293e44Sflorian 	close(iev_frontend->ibuf.fd);
3079cbf9e90Sclaudio 	imsgbuf_clear(&iev_engine->ibuf);
30853293e44Sflorian 	close(iev_engine->ibuf.fd);
30953293e44Sflorian 
31053293e44Sflorian 	config_clear(main_conf);
31153293e44Sflorian 
31253293e44Sflorian 	log_debug("waiting for children to terminate");
31353293e44Sflorian 	do {
31453293e44Sflorian 		pid = wait(&status);
31553293e44Sflorian 		if (pid == -1) {
31653293e44Sflorian 			if (errno != EINTR && errno != ECHILD)
31753293e44Sflorian 				fatal("wait");
31853293e44Sflorian 		} else if (WIFSIGNALED(status))
31953293e44Sflorian 			log_warnx("%s terminated; signal %d",
32053293e44Sflorian 			    (pid == engine_pid) ? "engine" :
32153293e44Sflorian 			    "frontend", WTERMSIG(status));
32253293e44Sflorian 	} while (pid != -1 || (pid == -1 && errno == EINTR));
32353293e44Sflorian 
32453293e44Sflorian 	free(iev_frontend);
32553293e44Sflorian 	free(iev_engine);
32653293e44Sflorian 
32753293e44Sflorian 	log_info("terminating");
32853293e44Sflorian 	exit(0);
32953293e44Sflorian }
33053293e44Sflorian 
33153293e44Sflorian static pid_t
3323c6be478Sflorian start_child(enum rad_process p, char *argv0, int fd, int debug, int verbose)
33353293e44Sflorian {
33411806809Smestre 	char	*argv[6];
33553293e44Sflorian 	int	 argc = 0;
33653293e44Sflorian 	pid_t	 pid;
33753293e44Sflorian 
33853293e44Sflorian 	switch (pid = fork()) {
33953293e44Sflorian 	case -1:
34053293e44Sflorian 		fatal("cannot fork");
34153293e44Sflorian 	case 0:
34253293e44Sflorian 		break;
34353293e44Sflorian 	default:
34453293e44Sflorian 		close(fd);
34553293e44Sflorian 		return (pid);
34653293e44Sflorian 	}
34753293e44Sflorian 
348ef4f5895Syasuoka 	if (fd != 3) {
34953293e44Sflorian 		if (dup2(fd, 3) == -1)
35053293e44Sflorian 			fatal("cannot setup imsg fd");
351ef4f5895Syasuoka 	} else if (fcntl(fd, F_SETFD, 0) == -1)
352ef4f5895Syasuoka 		fatal("cannot setup imsg fd");
35353293e44Sflorian 
35453293e44Sflorian 	argv[argc++] = argv0;
35553293e44Sflorian 	switch (p) {
35653293e44Sflorian 	case PROC_MAIN:
35753293e44Sflorian 		fatalx("Can not start main process");
35853293e44Sflorian 	case PROC_ENGINE:
35953293e44Sflorian 		argv[argc++] = "-E";
36053293e44Sflorian 		break;
36153293e44Sflorian 	case PROC_FRONTEND:
36253293e44Sflorian 		argv[argc++] = "-F";
36353293e44Sflorian 		break;
36453293e44Sflorian 	}
36553293e44Sflorian 	if (debug)
36653293e44Sflorian 		argv[argc++] = "-d";
36753293e44Sflorian 	if (verbose)
36853293e44Sflorian 		argv[argc++] = "-v";
36953293e44Sflorian 	argv[argc++] = NULL;
37053293e44Sflorian 
37153293e44Sflorian 	execvp(argv0, argv);
37253293e44Sflorian 	fatal("execvp");
37353293e44Sflorian }
37453293e44Sflorian 
37553293e44Sflorian void
37653293e44Sflorian main_dispatch_frontend(int fd, short event, void *bula)
37753293e44Sflorian {
37853293e44Sflorian 	struct imsgev		*iev = bula;
37953293e44Sflorian 	struct imsgbuf		*ibuf;
38053293e44Sflorian 	struct imsg		 imsg;
38153293e44Sflorian 	ssize_t			 n;
38253293e44Sflorian 	int			 shut = 0, verbose;
383e88dba76Sflorian 	int			 rdomain;
38453293e44Sflorian 
38553293e44Sflorian 	ibuf = &iev->ibuf;
38653293e44Sflorian 
38753293e44Sflorian 	if (event & EV_READ) {
388668e5ba9Sclaudio 		if ((n = imsgbuf_read(ibuf)) == -1)
389dd7efffeSclaudio 			fatal("imsgbuf_read error");
39053293e44Sflorian 		if (n == 0)	/* Connection closed. */
39153293e44Sflorian 			shut = 1;
39253293e44Sflorian 	}
39353293e44Sflorian 	if (event & EV_WRITE) {
394dd7efffeSclaudio 		if (imsgbuf_write(ibuf) == -1) {
395c1aa9554Sclaudio 			if (errno == EPIPE)	/* connection closed */
39653293e44Sflorian 				shut = 1;
397c1aa9554Sclaudio 			else
398dd7efffeSclaudio 				fatal("imsgbuf_write");
399c1aa9554Sclaudio 		}
40053293e44Sflorian 	}
40153293e44Sflorian 
40253293e44Sflorian 	for (;;) {
40353293e44Sflorian 		if ((n = imsg_get(ibuf, &imsg)) == -1)
40453293e44Sflorian 			fatal("imsg_get");
40553293e44Sflorian 		if (n == 0)	/* No more messages. */
40653293e44Sflorian 			break;
40753293e44Sflorian 
40853293e44Sflorian 		switch (imsg.hdr.type) {
409e88dba76Sflorian 		case IMSG_OPEN_ICMP6SOCK:
410e88dba76Sflorian 			log_debug("IMSG_OPEN_ICMP6SOCK");
411e88dba76Sflorian 			if (IMSG_DATA_SIZE(imsg) != sizeof(rdomain))
412e88dba76Sflorian 				fatalx("%s: IMSG_OPEN_ICMP6SOCK wrong length: "
413e88dba76Sflorian 				    "%lu", __func__, IMSG_DATA_SIZE(imsg));
414e88dba76Sflorian 			memcpy(&rdomain, imsg.data, sizeof(rdomain));
415e88dba76Sflorian 			open_icmp6sock(rdomain);
41653293e44Sflorian 			break;
41753293e44Sflorian 		case IMSG_CTL_RELOAD:
41853293e44Sflorian 			if (main_reload() == -1)
41953293e44Sflorian 				log_warnx("configuration reload failed");
42053293e44Sflorian 			else
42153293e44Sflorian 				log_warnx("configuration reloaded");
42253293e44Sflorian 			break;
42353293e44Sflorian 		case IMSG_CTL_LOG_VERBOSE:
4240eb5c43bSpamela 			if (IMSG_DATA_SIZE(imsg) != sizeof(verbose))
4250eb5c43bSpamela 				fatalx("%s: IMSG_CTL_LOG_VERBOSE wrong length: "
4260eb5c43bSpamela 				    "%lu", __func__, IMSG_DATA_SIZE(imsg));
42753293e44Sflorian 			memcpy(&verbose, imsg.data, sizeof(verbose));
42853293e44Sflorian 			log_setverbose(verbose);
42953293e44Sflorian 			break;
43053293e44Sflorian 		default:
431d8c41d5eSflorian 			log_debug("%s: error handling imsg %d", __func__,
432d8c41d5eSflorian 			    imsg.hdr.type);
43353293e44Sflorian 			break;
43453293e44Sflorian 		}
43553293e44Sflorian 		imsg_free(&imsg);
43653293e44Sflorian 	}
43753293e44Sflorian 	if (!shut)
43853293e44Sflorian 		imsg_event_add(iev);
43953293e44Sflorian 	else {
44053293e44Sflorian 		/* This pipe is dead. Remove its event handler */
44153293e44Sflorian 		event_del(&iev->ev);
44253293e44Sflorian 		event_loopexit(NULL);
44353293e44Sflorian 	}
44453293e44Sflorian }
44553293e44Sflorian 
44653293e44Sflorian void
44753293e44Sflorian main_dispatch_engine(int fd, short event, void *bula)
44853293e44Sflorian {
44953293e44Sflorian 	struct imsgev	*iev = bula;
45053293e44Sflorian 	struct imsgbuf  *ibuf;
45153293e44Sflorian 	struct imsg	 imsg;
45253293e44Sflorian 	ssize_t		 n;
45353293e44Sflorian 	int		 shut = 0;
45453293e44Sflorian 
45553293e44Sflorian 	ibuf = &iev->ibuf;
45653293e44Sflorian 
45753293e44Sflorian 	if (event & EV_READ) {
458668e5ba9Sclaudio 		if ((n = imsgbuf_read(ibuf)) == -1)
459dd7efffeSclaudio 			fatal("imsgbuf_read error");
46053293e44Sflorian 		if (n == 0)	/* Connection closed. */
46153293e44Sflorian 			shut = 1;
46253293e44Sflorian 	}
46353293e44Sflorian 	if (event & EV_WRITE) {
464dd7efffeSclaudio 		if (imsgbuf_write(ibuf) == -1) {
465c1aa9554Sclaudio 			if (errno == EPIPE)	/* connection closed */
46653293e44Sflorian 				shut = 1;
467c1aa9554Sclaudio 			else
468dd7efffeSclaudio 				fatal("imsgbuf_write");
469c1aa9554Sclaudio 		}
47053293e44Sflorian 	}
47153293e44Sflorian 
47253293e44Sflorian 	for (;;) {
47353293e44Sflorian 		if ((n = imsg_get(ibuf, &imsg)) == -1)
47453293e44Sflorian 			fatal("imsg_get");
47553293e44Sflorian 		if (n == 0)	/* No more messages. */
47653293e44Sflorian 			break;
47753293e44Sflorian 
47853293e44Sflorian 		switch (imsg.hdr.type) {
47953293e44Sflorian 		default:
48053293e44Sflorian 			log_debug("%s: error handling imsg %d", __func__,
48153293e44Sflorian 			    imsg.hdr.type);
48253293e44Sflorian 			break;
48353293e44Sflorian 		}
48453293e44Sflorian 		imsg_free(&imsg);
48553293e44Sflorian 	}
48653293e44Sflorian 	if (!shut)
48753293e44Sflorian 		imsg_event_add(iev);
48853293e44Sflorian 	else {
48953293e44Sflorian 		/* This pipe is dead. Remove its event handler. */
49053293e44Sflorian 		event_del(&iev->ev);
49153293e44Sflorian 		event_loopexit(NULL);
49253293e44Sflorian 	}
49353293e44Sflorian }
49453293e44Sflorian 
495e88dba76Sflorian int
496e88dba76Sflorian main_imsg_compose_frontend(int type, int fd, void *data, uint16_t datalen)
49753293e44Sflorian {
49853293e44Sflorian 	if (iev_frontend)
499e88dba76Sflorian 		return (imsg_compose_event(iev_frontend, type, 0, 0, fd, data,
500e88dba76Sflorian 		    datalen));
501e88dba76Sflorian 	else
502e88dba76Sflorian 		return (-1);
50353293e44Sflorian }
50453293e44Sflorian 
50553293e44Sflorian void
50653293e44Sflorian main_imsg_compose_engine(int type, pid_t pid, void *data, uint16_t datalen)
50753293e44Sflorian {
50853293e44Sflorian 	if (iev_engine)
50953293e44Sflorian 		imsg_compose_event(iev_engine, type, 0, pid, -1, data,
51053293e44Sflorian 		    datalen);
51153293e44Sflorian }
51253293e44Sflorian 
51353293e44Sflorian void
51453293e44Sflorian imsg_event_add(struct imsgev *iev)
51553293e44Sflorian {
51653293e44Sflorian 	iev->events = EV_READ;
51731be28caSclaudio 	if (imsgbuf_queuelen(&iev->ibuf) > 0)
51853293e44Sflorian 		iev->events |= EV_WRITE;
51953293e44Sflorian 
52053293e44Sflorian 	event_del(&iev->ev);
52153293e44Sflorian 	event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
52253293e44Sflorian 	event_add(&iev->ev, NULL);
52353293e44Sflorian }
52453293e44Sflorian 
52553293e44Sflorian int
52653293e44Sflorian imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
52753293e44Sflorian     pid_t pid, int fd, void *data, uint16_t datalen)
52853293e44Sflorian {
52953293e44Sflorian 	int	ret;
53053293e44Sflorian 
53153293e44Sflorian 	if ((ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data,
53253293e44Sflorian 	    datalen)) != -1)
53353293e44Sflorian 		imsg_event_add(iev);
53453293e44Sflorian 
53553293e44Sflorian 	return (ret);
53653293e44Sflorian }
53753293e44Sflorian 
53853293e44Sflorian static int
53953293e44Sflorian main_imsg_send_ipc_sockets(struct imsgbuf *frontend_buf,
54053293e44Sflorian     struct imsgbuf *engine_buf)
54153293e44Sflorian {
54253293e44Sflorian 	int pipe_frontend2engine[2];
54353293e44Sflorian 
54453293e44Sflorian 	if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
54553293e44Sflorian 	    PF_UNSPEC, pipe_frontend2engine) == -1)
54653293e44Sflorian 		return (-1);
54753293e44Sflorian 
54853293e44Sflorian 	if (imsg_compose(frontend_buf, IMSG_SOCKET_IPC, 0, 0,
54953293e44Sflorian 	    pipe_frontend2engine[0], NULL, 0) == -1)
55053293e44Sflorian 		return (-1);
55153293e44Sflorian 	if (imsg_compose(engine_buf, IMSG_SOCKET_IPC, 0, 0,
55253293e44Sflorian 	    pipe_frontend2engine[1], NULL, 0) == -1)
55353293e44Sflorian 		return (-1);
55453293e44Sflorian 
55553293e44Sflorian 	return (0);
55653293e44Sflorian }
55753293e44Sflorian 
55853293e44Sflorian int
55953293e44Sflorian main_reload(void)
56053293e44Sflorian {
56153293e44Sflorian 	struct rad_conf *xconf;
56253293e44Sflorian 
56353293e44Sflorian 	if ((xconf = parse_config(conffile)) == NULL)
56453293e44Sflorian 		return (-1);
56553293e44Sflorian 
56653293e44Sflorian 	if (main_imsg_send_config(xconf) == -1)
56753293e44Sflorian 		return (-1);
56853293e44Sflorian 
56953293e44Sflorian 	merge_config(main_conf, xconf);
57053293e44Sflorian 
57153293e44Sflorian 	return (0);
57253293e44Sflorian }
57353293e44Sflorian 
57453293e44Sflorian int
57553293e44Sflorian main_imsg_send_config(struct rad_conf *xconf)
57653293e44Sflorian {
57753293e44Sflorian 	struct ra_iface_conf	*ra_iface_conf;
57853293e44Sflorian 	struct ra_prefix_conf	*ra_prefix_conf;
5794c40b7e8Sflorian 	struct ra_rdnss_conf	*ra_rdnss_conf;
5804c40b7e8Sflorian 	struct ra_dnssl_conf	*ra_dnssl_conf;
5815207bb19Sflorian 	struct ra_pref64_conf	*pref64;
58253293e44Sflorian 
58353293e44Sflorian 	/* Send fixed part of config to children. */
58453293e44Sflorian 	if (main_sendboth(IMSG_RECONF_CONF, xconf, sizeof(*xconf)) == -1)
58553293e44Sflorian 		return (-1);
58653293e44Sflorian 
5878815eebdSflorian 	/* send global dns options to children */
5888815eebdSflorian 	SIMPLEQ_FOREACH(ra_rdnss_conf, &xconf->ra_options.ra_rdnss_list,
5898815eebdSflorian 	    entry) {
5908815eebdSflorian 		if (main_sendboth(IMSG_RECONF_RA_RDNSS, ra_rdnss_conf,
5918815eebdSflorian 		    sizeof(*ra_rdnss_conf)) == -1)
5928815eebdSflorian 			return (-1);
5938815eebdSflorian 	}
5948815eebdSflorian 	SIMPLEQ_FOREACH(ra_dnssl_conf, &xconf->ra_options.ra_dnssl_list,
5958815eebdSflorian 	    entry) {
5968815eebdSflorian 		if (main_sendboth(IMSG_RECONF_RA_DNSSL, ra_dnssl_conf,
5978815eebdSflorian 		    sizeof(*ra_dnssl_conf)) == -1)
5988815eebdSflorian 			return (-1);
5998815eebdSflorian 	}
6008815eebdSflorian 
6015207bb19Sflorian 	/* send global pref64 list to children */
6025207bb19Sflorian 	SIMPLEQ_FOREACH(pref64, &xconf->ra_options.ra_pref64_list,
6035207bb19Sflorian 	    entry) {
6045207bb19Sflorian 		if (main_sendboth(IMSG_RECONF_RA_PREF64, pref64,
6055207bb19Sflorian 		    sizeof(*pref64)) == -1)
6065207bb19Sflorian 			return (-1);
6075207bb19Sflorian 	}
6085207bb19Sflorian 
60953293e44Sflorian 	/* Send the interface list to children. */
61053293e44Sflorian 	SIMPLEQ_FOREACH(ra_iface_conf, &xconf->ra_iface_list, entry) {
61153293e44Sflorian 		if (main_sendboth(IMSG_RECONF_RA_IFACE, ra_iface_conf,
61253293e44Sflorian 		    sizeof(*ra_iface_conf)) == -1)
61353293e44Sflorian 			return (-1);
61453293e44Sflorian 		if (ra_iface_conf->autoprefix) {
61553293e44Sflorian 			if (main_sendboth(IMSG_RECONF_RA_AUTOPREFIX,
61653293e44Sflorian 			    ra_iface_conf->autoprefix,
61753293e44Sflorian 			    sizeof(*ra_iface_conf->autoprefix)) == -1)
61853293e44Sflorian 				return (-1);
61953293e44Sflorian 		}
62053293e44Sflorian 		SIMPLEQ_FOREACH(ra_prefix_conf, &ra_iface_conf->ra_prefix_list,
62153293e44Sflorian 		    entry) {
62253293e44Sflorian 			if (main_sendboth(IMSG_RECONF_RA_PREFIX,
62353293e44Sflorian 			    ra_prefix_conf, sizeof(*ra_prefix_conf)) == -1)
62453293e44Sflorian 				return (-1);
62553293e44Sflorian 		}
6268815eebdSflorian 		SIMPLEQ_FOREACH(ra_rdnss_conf,
6278815eebdSflorian 		    &ra_iface_conf->ra_options.ra_rdnss_list, entry) {
6284c40b7e8Sflorian 			if (main_sendboth(IMSG_RECONF_RA_RDNSS, ra_rdnss_conf,
6294c40b7e8Sflorian 			    sizeof(*ra_rdnss_conf)) == -1)
6304c40b7e8Sflorian 				return (-1);
6314c40b7e8Sflorian 		}
6328815eebdSflorian 		SIMPLEQ_FOREACH(ra_dnssl_conf,
6338815eebdSflorian 		    &ra_iface_conf->ra_options.ra_dnssl_list, entry) {
6344c40b7e8Sflorian 			if (main_sendboth(IMSG_RECONF_RA_DNSSL, ra_dnssl_conf,
6354c40b7e8Sflorian 			    sizeof(*ra_dnssl_conf)) == -1)
6364c40b7e8Sflorian 				return (-1);
6374c40b7e8Sflorian 		}
6385207bb19Sflorian 		SIMPLEQ_FOREACH(pref64,
6395207bb19Sflorian 		    &ra_iface_conf->ra_options.ra_pref64_list, entry) {
6405207bb19Sflorian 			if (main_sendboth(IMSG_RECONF_RA_PREF64, pref64,
6415207bb19Sflorian 			    sizeof(*pref64)) == -1)
6425207bb19Sflorian 				return (-1);
6435207bb19Sflorian 		}
64453293e44Sflorian 	}
64553293e44Sflorian 
64653293e44Sflorian 	/* Tell children the revised config is now complete. */
64753293e44Sflorian 	if (main_sendboth(IMSG_RECONF_END, NULL, 0) == -1)
64853293e44Sflorian 		return (-1);
64953293e44Sflorian 
65053293e44Sflorian 	return (0);
65153293e44Sflorian }
65253293e44Sflorian 
65353293e44Sflorian int
65453293e44Sflorian main_sendboth(enum imsg_type type, void *buf, uint16_t len)
65553293e44Sflorian {
65653293e44Sflorian 	if (imsg_compose_event(iev_frontend, type, 0, 0, -1, buf, len) == -1)
65753293e44Sflorian 		return (-1);
65853293e44Sflorian 	if (imsg_compose_event(iev_engine, type, 0, 0, -1, buf, len) == -1)
65953293e44Sflorian 		return (-1);
66053293e44Sflorian 	return (0);
66153293e44Sflorian }
66253293e44Sflorian 
66353293e44Sflorian void
66453293e44Sflorian free_ra_iface_conf(struct ra_iface_conf *ra_iface_conf)
66553293e44Sflorian {
66653293e44Sflorian 	struct ra_prefix_conf	*prefix;
6675207bb19Sflorian 	struct ra_pref64_conf	*pref64;
66853293e44Sflorian 
66953293e44Sflorian 	if (!ra_iface_conf)
67053293e44Sflorian 		return;
67153293e44Sflorian 
67253293e44Sflorian 	free(ra_iface_conf->autoprefix);
67353293e44Sflorian 
6742ef977c0Sflorian 	while ((prefix = SIMPLEQ_FIRST(&ra_iface_conf->ra_prefix_list)) !=
6752ef977c0Sflorian 	    NULL) {
67653293e44Sflorian 		SIMPLEQ_REMOVE_HEAD(&ra_iface_conf->ra_prefix_list, entry);
67753293e44Sflorian 		free(prefix);
67853293e44Sflorian 	}
67953293e44Sflorian 
6808815eebdSflorian 	free_dns_options(&ra_iface_conf->ra_options);
6812ef977c0Sflorian 
6825207bb19Sflorian 	while ((pref64 =
6835207bb19Sflorian 	    SIMPLEQ_FIRST(&ra_iface_conf->ra_options.ra_pref64_list)) != NULL) {
6845207bb19Sflorian 		SIMPLEQ_REMOVE_HEAD(&ra_iface_conf->ra_options.ra_pref64_list,
6855207bb19Sflorian 		    entry);
6865207bb19Sflorian 		free(pref64);
6875207bb19Sflorian 	}
6885207bb19Sflorian 
68953293e44Sflorian 	free(ra_iface_conf);
69053293e44Sflorian }
69153293e44Sflorian 
69253293e44Sflorian void
6938815eebdSflorian free_dns_options(struct ra_options_conf *ra_options)
6948815eebdSflorian {
6958815eebdSflorian 	struct ra_rdnss_conf	*ra_rdnss;
6968815eebdSflorian 	struct ra_dnssl_conf	*ra_dnssl;
6978815eebdSflorian 
6988815eebdSflorian 	while ((ra_rdnss = SIMPLEQ_FIRST(&ra_options->ra_rdnss_list)) != NULL) {
6998815eebdSflorian 		SIMPLEQ_REMOVE_HEAD(&ra_options->ra_rdnss_list, entry);
7008815eebdSflorian 		free(ra_rdnss);
7018815eebdSflorian 	}
7028815eebdSflorian 	ra_options->rdnss_count = 0;
7038815eebdSflorian 
7048815eebdSflorian 	while ((ra_dnssl = SIMPLEQ_FIRST(&ra_options->ra_dnssl_list)) != NULL) {
7058815eebdSflorian 		SIMPLEQ_REMOVE_HEAD(&ra_options->ra_dnssl_list, entry);
7068815eebdSflorian 		free(ra_dnssl);
7078815eebdSflorian 	}
7088815eebdSflorian 	ra_options->dnssl_len = 0;
7098815eebdSflorian }
7108815eebdSflorian 
7118815eebdSflorian void
71253293e44Sflorian merge_config(struct rad_conf *conf, struct rad_conf *xconf)
71353293e44Sflorian {
71453293e44Sflorian 	struct ra_iface_conf	*ra_iface_conf;
7155207bb19Sflorian 	struct ra_pref64_conf	*pref64;
71653293e44Sflorian 
71753293e44Sflorian 	/* Remove & discard existing interfaces. */
71853293e44Sflorian 	while ((ra_iface_conf = SIMPLEQ_FIRST(&conf->ra_iface_list)) != NULL) {
71953293e44Sflorian 		SIMPLEQ_REMOVE_HEAD(&conf->ra_iface_list, entry);
72053293e44Sflorian 		free_ra_iface_conf(ra_iface_conf);
72153293e44Sflorian 	}
7228815eebdSflorian 	free_dns_options(&conf->ra_options);
7238815eebdSflorian 
7245207bb19Sflorian 	while ((pref64 = SIMPLEQ_FIRST(&conf->ra_options.ra_pref64_list))
7255207bb19Sflorian 	    != NULL) {
7265207bb19Sflorian 		SIMPLEQ_REMOVE_HEAD(&conf->ra_options.ra_pref64_list, entry);
7275207bb19Sflorian 		free(pref64);
7285207bb19Sflorian 	}
7295207bb19Sflorian 
7308815eebdSflorian 	conf->ra_options = xconf->ra_options;
7318815eebdSflorian 	SIMPLEQ_INIT(&conf->ra_options.ra_rdnss_list);
7328815eebdSflorian 	SIMPLEQ_INIT(&conf->ra_options.ra_dnssl_list);
7335207bb19Sflorian 	SIMPLEQ_INIT(&conf->ra_options.ra_pref64_list);
73453293e44Sflorian 
73553293e44Sflorian 	/* Add new interfaces. */
7364bf8a5d4Sbket 	SIMPLEQ_CONCAT(&conf->ra_iface_list, &xconf->ra_iface_list);
73753293e44Sflorian 
7388815eebdSflorian 	/* Add dns options */
7394bf8a5d4Sbket 	SIMPLEQ_CONCAT(&conf->ra_options.ra_rdnss_list,
7404bf8a5d4Sbket 	    &xconf->ra_options.ra_rdnss_list);
7414bf8a5d4Sbket 	SIMPLEQ_CONCAT(&conf->ra_options.ra_dnssl_list,
7424bf8a5d4Sbket 	    &xconf->ra_options.ra_dnssl_list);
7435207bb19Sflorian 	SIMPLEQ_CONCAT(&conf->ra_options.ra_pref64_list,
7445207bb19Sflorian 	    &xconf->ra_options.ra_pref64_list);
74553293e44Sflorian 	free(xconf);
74653293e44Sflorian }
74753293e44Sflorian 
74853293e44Sflorian struct rad_conf *
74953293e44Sflorian config_new_empty(void)
75053293e44Sflorian {
75153293e44Sflorian 	struct rad_conf	*xconf;
75253293e44Sflorian 
75353293e44Sflorian 	xconf = calloc(1, sizeof(*xconf));
75453293e44Sflorian 	if (xconf == NULL)
75553293e44Sflorian 		fatal(NULL);
75653293e44Sflorian 
75753293e44Sflorian 	SIMPLEQ_INIT(&xconf->ra_iface_list);
75853293e44Sflorian 
75953293e44Sflorian 	xconf->ra_options.dfr = 1;
76053293e44Sflorian 	xconf->ra_options.cur_hl = 0;
76153293e44Sflorian 	xconf->ra_options.m_flag = 0;
76253293e44Sflorian 	xconf->ra_options.o_flag = 0;
76303aa2005Sflorian 	xconf->ra_options.router_lifetime = ADV_DEFAULT_LIFETIME;
76453293e44Sflorian 	xconf->ra_options.reachable_time = 0;
76553293e44Sflorian 	xconf->ra_options.retrans_timer = 0;
76630ca3407Sflorian 	xconf->ra_options.source_link_addr = 1;
767cea17583Sbket 	xconf->ra_options.mtu = 0;
7688815eebdSflorian 	xconf->ra_options.rdns_lifetime = DEFAULT_RDNS_LIFETIME;
7698815eebdSflorian 	SIMPLEQ_INIT(&xconf->ra_options.ra_rdnss_list);
7708815eebdSflorian 	SIMPLEQ_INIT(&xconf->ra_options.ra_dnssl_list);
7715207bb19Sflorian 	SIMPLEQ_INIT(&xconf->ra_options.ra_pref64_list);
77253293e44Sflorian 
77353293e44Sflorian 	return (xconf);
77453293e44Sflorian }
77553293e44Sflorian 
77653293e44Sflorian void
77753293e44Sflorian config_clear(struct rad_conf *conf)
77853293e44Sflorian {
77953293e44Sflorian 	struct rad_conf	*xconf;
78053293e44Sflorian 
78153293e44Sflorian 	/* Merge current config with an empty config. */
78253293e44Sflorian 	xconf = config_new_empty();
78353293e44Sflorian 	merge_config(conf, xconf);
78453293e44Sflorian 
78553293e44Sflorian 	free(conf);
78653293e44Sflorian }
78753293e44Sflorian 
78816c642d1Sbluhm void
78916c642d1Sbluhm mask_prefix(struct in6_addr* in6, int len)
79053293e44Sflorian {
79153293e44Sflorian 	uint8_t	bitmask[8] = {0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe};
79253293e44Sflorian 	int	i, skip;
79353293e44Sflorian 
79453293e44Sflorian 	if (len < 0 || len > 128)
79553293e44Sflorian 		fatalx("invalid prefix length: %d", len);
79653293e44Sflorian 
79753293e44Sflorian 	skip = len / 8;
79853293e44Sflorian 
79916c642d1Sbluhm 	if (skip < 16)
80053293e44Sflorian 		in6->s6_addr[skip] &= bitmask[len % 8];
80153293e44Sflorian 
80253293e44Sflorian 	for (i = skip + 1; i < 16; i++)
80353293e44Sflorian 		in6->s6_addr[i] = 0;
80453293e44Sflorian }
80553293e44Sflorian 
80653293e44Sflorian const char*
80753293e44Sflorian sin6_to_str(struct sockaddr_in6 *sin6)
80853293e44Sflorian {
80953293e44Sflorian 	static char hbuf[NI_MAXHOST];
81053293e44Sflorian 	int error;
81153293e44Sflorian 
81253293e44Sflorian 	error = getnameinfo((struct sockaddr *)sin6, sin6->sin6_len, hbuf,
81353293e44Sflorian 	    sizeof(hbuf), NULL, 0, NI_NUMERICHOST | NI_NUMERICSERV);
81453293e44Sflorian 	if (error) {
81553293e44Sflorian 		log_warnx("%s", gai_strerror(error));
81653293e44Sflorian 		strlcpy(hbuf, "unknown", sizeof(hbuf));
81753293e44Sflorian 	}
81853293e44Sflorian 	return hbuf;
81953293e44Sflorian }
82053293e44Sflorian 
82153293e44Sflorian const char*
82253293e44Sflorian in6_to_str(struct in6_addr *in6)
82353293e44Sflorian {
82453293e44Sflorian 
82553293e44Sflorian 	struct sockaddr_in6	sin6;
82653293e44Sflorian 
82753293e44Sflorian 	memset(&sin6, 0, sizeof(sin6));
82853293e44Sflorian 	sin6.sin6_len = sizeof(sin6);
82953293e44Sflorian 	sin6.sin6_family = AF_INET6;
83053293e44Sflorian 	sin6.sin6_addr = *in6;
83153293e44Sflorian 
83253293e44Sflorian 	return (sin6_to_str(&sin6));
83353293e44Sflorian }
834e88dba76Sflorian 
835e88dba76Sflorian void
836e88dba76Sflorian open_icmp6sock(int rdomain)
837e88dba76Sflorian {
838e88dba76Sflorian 	int			 icmp6sock, on = 1, off = 0;
839e88dba76Sflorian 
840e88dba76Sflorian 	log_debug("%s: %d", __func__, rdomain);
841e88dba76Sflorian 
842e88dba76Sflorian 	if ((icmp6sock = socket(AF_INET6, SOCK_RAW | SOCK_CLOEXEC,
843e88dba76Sflorian 	    IPPROTO_ICMPV6)) == -1)
844e88dba76Sflorian 		fatal("ICMPv6 socket");
845e88dba76Sflorian 
846e88dba76Sflorian 	if (setsockopt(icmp6sock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on,
847e88dba76Sflorian 	    sizeof(on)) == -1)
848e88dba76Sflorian 		fatal("IPV6_RECVPKTINFO");
849e88dba76Sflorian 
850e88dba76Sflorian 	if (setsockopt(icmp6sock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &on,
851e88dba76Sflorian 	    sizeof(on)) == -1)
852e88dba76Sflorian 		fatal("IPV6_RECVHOPLIMIT");
853e88dba76Sflorian 
854e88dba76Sflorian 	if (setsockopt(icmp6sock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &off,
855e88dba76Sflorian 	    sizeof(off)) == -1)
856e88dba76Sflorian 		fatal("IPV6_RECVHOPLIMIT");
857e88dba76Sflorian 
858e88dba76Sflorian 	if (setsockopt(icmp6sock, SOL_SOCKET, SO_RTABLE, &rdomain,
859e88dba76Sflorian 	    sizeof(rdomain)) == -1) {
860e88dba76Sflorian 		/* we might race against removal of the rdomain */
861e88dba76Sflorian 		log_warn("setsockopt SO_RTABLE");
862e88dba76Sflorian 		close(icmp6sock);
863e88dba76Sflorian 		return;
864e88dba76Sflorian 	}
865e88dba76Sflorian 
866e88dba76Sflorian 	main_imsg_compose_frontend(IMSG_ICMP6SOCK, icmp6sock, &rdomain,
867e88dba76Sflorian 	    sizeof(rdomain));
868e88dba76Sflorian }
869