xref: /openbsd-src/usr.sbin/rad/frontend.c (revision 746bf85ef77f47f1e658d909fa3ddb3e26aa65bd)
1 /*	$OpenBSD: frontend.c,v 1.20 2019/01/29 15:43:33 florian 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 
22 /*
23  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
24  * All rights reserved.
25  *
26  * Redistribution and use in source and binary forms, with or without
27  * modification, are permitted provided that the following conditions
28  * are met:
29  * 1. Redistributions of source code must retain the above copyright
30  *    notice, this list of conditions and the following disclaimer.
31  * 2. Redistributions in binary form must reproduce the above copyright
32  *    notice, this list of conditions and the following disclaimer in the
33  *    documentation and/or other materials provided with the distribution.
34  * 3. Neither the name of the project nor the names of its contributors
35  *    may be used to endorse or promote products derived from this software
36  *    without specific prior written permission.
37  *
38  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
39  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
41  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
42  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
43  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
44  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
45  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
46  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
47  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  */
50 
51 #include <sys/types.h>
52 #include <sys/ioctl.h>
53 #include <sys/queue.h>
54 #include <sys/socket.h>
55 #include <sys/syslog.h>
56 #include <sys/uio.h>
57 
58 #include <net/if.h>
59 #include <net/if_dl.h>
60 #include <net/if_types.h>
61 #include <net/route.h>
62 
63 #include <arpa/inet.h>
64 
65 #include <netinet/in.h>
66 #include <netinet/if_ether.h>
67 #include <netinet6/nd6.h>
68 #include <netinet6/in6_var.h>
69 #include <netinet/ip6.h>
70 #include <netinet6/ip6_var.h>
71 #include <netinet/icmp6.h>
72 
73 #include <ctype.h>
74 #include <errno.h>
75 #include <event.h>
76 #include <ifaddrs.h>
77 #include <imsg.h>
78 #include <pwd.h>
79 #include <signal.h>
80 #include <stdio.h>
81 #include <stdlib.h>
82 #include <string.h>
83 #include <unistd.h>
84 
85 #include "log.h"
86 #include "rad.h"
87 #include "frontend.h"
88 #include "control.h"
89 
90 #define	RA_MAX_SIZE		1500
91 #define	ROUTE_SOCKET_BUF_SIZE	16384
92 
93 struct icmp6_ev {
94 	struct event		 ev;
95 	uint8_t			 answer[1500];
96 	struct msghdr		 rcvmhdr;
97 	struct iovec		 rcviov[1];
98 	struct sockaddr_in6	 from;
99 } icmp6ev;
100 
101 struct ra_iface {
102 	TAILQ_ENTRY(ra_iface)		entry;
103 	struct ra_prefix_conf_head	prefixes;
104 	char				name[IF_NAMESIZE];
105 	char				conf_name[IF_NAMESIZE];
106 	uint32_t			if_index;
107 	int				removed;
108 	int				prefix_count;
109 	size_t				datalen;
110 	uint8_t				data[RA_MAX_SIZE];
111 };
112 
113 TAILQ_HEAD(, ra_iface)	ra_interfaces;
114 
115 __dead void		 frontend_shutdown(void);
116 void			 frontend_sig_handler(int, short, void *);
117 void			 frontend_startup(void);
118 void			 icmp6_receive(int, short, void *);
119 void			 join_all_routers_mcast_group(struct ra_iface *);
120 void			 leave_all_routers_mcast_group(struct ra_iface *);
121 void			 merge_ra_interface(char *, char *);
122 void			 merge_ra_interfaces(void);
123 struct ra_iface		*find_ra_iface_by_id(uint32_t);
124 struct ra_iface		*find_ra_iface_by_name(char *);
125 struct ra_iface_conf	*find_ra_iface_conf(struct ra_iface_conf_head *,
126 			    char *);
127 struct ra_prefix_conf	*find_ra_prefix_conf(struct ra_prefix_conf_head*,
128 			    struct in6_addr *, int);
129 void			 add_new_prefix_to_ra_iface(struct ra_iface *r,
130 			    struct in6_addr *, int, struct ra_prefix_conf *);
131 void			 free_ra_iface(struct ra_iface *);
132 int			 in6_mask2prefixlen(struct in6_addr *);
133 void			 get_interface_prefixes(struct ra_iface *,
134 			     struct ra_prefix_conf *);
135 void			 build_packet(struct ra_iface *);
136 void			 build_leaving_packet(struct ra_iface *);
137 void			 ra_output(struct ra_iface *, struct sockaddr_in6 *);
138 void			 get_rtaddrs(int, struct sockaddr *,
139 			     struct sockaddr **);
140 void			 route_receive(int, short, void *);
141 void			 handle_route_message(struct rt_msghdr *,
142 			     struct sockaddr **);
143 
144 struct rad_conf	*frontend_conf;
145 struct imsgev		*iev_main;
146 struct imsgev		*iev_engine;
147 struct event		 ev_route;
148 int			 icmp6sock = -1, ioctlsock = -1;
149 struct ipv6_mreq	 all_routers;
150 struct sockaddr_in6	 all_nodes;
151 struct msghdr		 sndmhdr;
152 struct iovec		 sndiov[2];
153 
154 void
155 frontend_sig_handler(int sig, short event, void *bula)
156 {
157 	/*
158 	 * Normal signal handler rules don't apply because libevent
159 	 * decouples for us.
160 	 */
161 
162 	switch (sig) {
163 	case SIGINT:
164 	case SIGTERM:
165 		frontend_shutdown();
166 	default:
167 		fatalx("unexpected signal");
168 	}
169 }
170 
171 void
172 frontend(int debug, int verbose)
173 {
174 	struct event		 ev_sigint, ev_sigterm;
175 	struct passwd		*pw;
176 	size_t			 rcvcmsglen, sndcmsgbuflen;
177 	uint8_t			*rcvcmsgbuf;
178 	uint8_t			*sndcmsgbuf = NULL;
179 
180 	frontend_conf = config_new_empty();
181 	control_state.fd = -1;
182 
183 	log_init(debug, LOG_DAEMON);
184 	log_setverbose(verbose);
185 
186 	if ((pw = getpwnam(RAD_USER)) == NULL)
187 		fatal("getpwnam");
188 
189 	if (chroot(pw->pw_dir) == -1)
190 		fatal("chroot");
191 	if (chdir("/") == -1)
192 		fatal("chdir(\"/\")");
193 
194 	rad_process = PROC_FRONTEND;
195 	setproctitle("%s", log_procnames[rad_process]);
196 	log_procinit(log_procnames[rad_process]);
197 
198 	if (setgroups(1, &pw->pw_gid) ||
199 	    setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
200 	    setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
201 		fatal("can't drop privileges");
202 
203 	/* XXX pass in from main */
204 	if ((ioctlsock = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0)) < 0)
205 		fatal("socket");
206 
207 	if (pledge("stdio inet unix recvfd route mcast", NULL) == -1)
208 		fatal("pledge");
209 
210 	event_init();
211 
212 	/* Setup signal handler. */
213 	signal_set(&ev_sigint, SIGINT, frontend_sig_handler, NULL);
214 	signal_set(&ev_sigterm, SIGTERM, frontend_sig_handler, NULL);
215 	signal_add(&ev_sigint, NULL);
216 	signal_add(&ev_sigterm, NULL);
217 	signal(SIGPIPE, SIG_IGN);
218 	signal(SIGHUP, SIG_IGN);
219 
220 	/* Setup pipe and event handler to the parent process. */
221 	if ((iev_main = malloc(sizeof(struct imsgev))) == NULL)
222 		fatal(NULL);
223 	imsg_init(&iev_main->ibuf, 3);
224 	iev_main->handler = frontend_dispatch_main;
225 	iev_main->events = EV_READ;
226 	event_set(&iev_main->ev, iev_main->ibuf.fd, iev_main->events,
227 	    iev_main->handler, iev_main);
228 	event_add(&iev_main->ev, NULL);
229 
230 	rcvcmsglen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
231 	    CMSG_SPACE(sizeof(int));
232 	if((rcvcmsgbuf = malloc(rcvcmsglen)) == NULL)
233 		fatal("malloc");
234 
235 	icmp6ev.rcviov[0].iov_base = (caddr_t)icmp6ev.answer;
236 	icmp6ev.rcviov[0].iov_len = sizeof(icmp6ev.answer);
237 	icmp6ev.rcvmhdr.msg_name = (caddr_t)&icmp6ev.from;
238 	icmp6ev.rcvmhdr.msg_namelen = sizeof(icmp6ev.from);
239 	icmp6ev.rcvmhdr.msg_iov = icmp6ev.rcviov;
240 	icmp6ev.rcvmhdr.msg_iovlen = 1;
241 	icmp6ev.rcvmhdr.msg_control = (caddr_t) rcvcmsgbuf;
242 	icmp6ev.rcvmhdr.msg_controllen = rcvcmsglen;
243 
244 	if (inet_pton(AF_INET6, "ff02::2",
245 	    &all_routers.ipv6mr_multiaddr.s6_addr) == -1)
246 		fatal("inet_pton");
247 
248 	all_nodes.sin6_len = sizeof(all_nodes);
249 	all_nodes.sin6_family = AF_INET6;
250 	if (inet_pton(AF_INET6, "ff02::1", &all_nodes.sin6_addr) != 1)
251 		fatal("inet_pton");
252 
253 	sndcmsgbuflen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
254 	    CMSG_SPACE(sizeof(int));
255 	if ((sndcmsgbuf = malloc(sndcmsgbuflen)) == NULL)
256 		fatal("%s", __func__);
257 
258 	sndmhdr.msg_namelen = sizeof(struct sockaddr_in6);
259 	sndmhdr.msg_iov = sndiov;
260 	sndmhdr.msg_iovlen = 1;
261 	sndmhdr.msg_control = sndcmsgbuf;
262 	sndmhdr.msg_controllen = sndcmsgbuflen;
263 
264 	TAILQ_INIT(&ra_interfaces);
265 
266 	event_dispatch();
267 
268 	frontend_shutdown();
269 }
270 
271 __dead void
272 frontend_shutdown(void)
273 {
274 	/* Close pipes. */
275 	msgbuf_write(&iev_engine->ibuf.w);
276 	msgbuf_clear(&iev_engine->ibuf.w);
277 	close(iev_engine->ibuf.fd);
278 	msgbuf_write(&iev_main->ibuf.w);
279 	msgbuf_clear(&iev_main->ibuf.w);
280 	close(iev_main->ibuf.fd);
281 
282 	config_clear(frontend_conf);
283 
284 	free(iev_engine);
285 	free(iev_main);
286 
287 	log_info("frontend exiting");
288 	exit(0);
289 }
290 
291 int
292 frontend_imsg_compose_main(int type, pid_t pid, void *data, uint16_t datalen)
293 {
294 	return (imsg_compose_event(iev_main, type, 0, pid, -1, data,
295 	    datalen));
296 }
297 
298 int
299 frontend_imsg_compose_engine(int type, pid_t pid, void *data, uint16_t datalen)
300 {
301 	return (imsg_compose_event(iev_engine, type, 0, pid, -1, data,
302 	    datalen));
303 }
304 
305 void
306 frontend_dispatch_main(int fd, short event, void *bula)
307 {
308 	static struct rad_conf		*nconf;
309 	static struct ra_iface_conf	*ra_iface_conf;
310 	static struct ra_options_conf	*ra_options;
311 	struct imsg			 imsg;
312 	struct imsgev			*iev = bula;
313 	struct imsgbuf			*ibuf = &iev->ibuf;
314 	struct ra_prefix_conf		*ra_prefix_conf;
315 	struct ra_rdnss_conf		*ra_rdnss_conf;
316 	struct ra_dnssl_conf		*ra_dnssl_conf;
317 	int				 n, shut = 0;
318 
319 	if (event & EV_READ) {
320 		if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
321 			fatal("imsg_read error");
322 		if (n == 0)	/* Connection closed. */
323 			shut = 1;
324 	}
325 	if (event & EV_WRITE) {
326 		if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
327 			fatal("msgbuf_write");
328 		if (n == 0)	/* Connection closed. */
329 			shut = 1;
330 	}
331 
332 	for (;;) {
333 		if ((n = imsg_get(ibuf, &imsg)) == -1)
334 			fatal("%s: imsg_get error", __func__);
335 		if (n == 0)	/* No more messages. */
336 			break;
337 
338 		switch (imsg.hdr.type) {
339 		case IMSG_SOCKET_IPC:
340 			/*
341 			 * Setup pipe and event handler to the engine
342 			 * process.
343 			 */
344 			if (iev_engine) {
345 				log_warnx("%s: received unexpected imsg fd "
346 				    "to frontend", __func__);
347 				break;
348 			}
349 			if ((fd = imsg.fd) == -1) {
350 				log_warnx("%s: expected to receive imsg fd to "
351 				   "frontend but didn't receive any",
352 				   __func__);
353 				break;
354 			}
355 
356 			iev_engine = malloc(sizeof(struct imsgev));
357 			if (iev_engine == NULL)
358 				fatal(NULL);
359 
360 			imsg_init(&iev_engine->ibuf, fd);
361 			iev_engine->handler = frontend_dispatch_engine;
362 			iev_engine->events = EV_READ;
363 
364 			event_set(&iev_engine->ev, iev_engine->ibuf.fd,
365 			iev_engine->events, iev_engine->handler, iev_engine);
366 			event_add(&iev_engine->ev, NULL);
367 			break;
368 		case IMSG_RECONF_CONF:
369 			if ((nconf = malloc(sizeof(struct rad_conf))) ==
370 			    NULL)
371 				fatal(NULL);
372 			memcpy(nconf, imsg.data, sizeof(struct rad_conf));
373 			SIMPLEQ_INIT(&nconf->ra_iface_list);
374 			SIMPLEQ_INIT(&nconf->ra_options.ra_rdnss_list);
375 			SIMPLEQ_INIT(&nconf->ra_options.ra_dnssl_list);
376 			ra_options = &nconf->ra_options;
377 			break;
378 		case IMSG_RECONF_RA_IFACE:
379 			if ((ra_iface_conf = malloc(sizeof(struct
380 			    ra_iface_conf))) == NULL)
381 				fatal(NULL);
382 			memcpy(ra_iface_conf, imsg.data, sizeof(struct
383 			    ra_iface_conf));
384 			ra_iface_conf->autoprefix = NULL;
385 			SIMPLEQ_INIT(&ra_iface_conf->ra_prefix_list);
386 			SIMPLEQ_INIT(&ra_iface_conf->ra_options.ra_rdnss_list);
387 			SIMPLEQ_INIT(&ra_iface_conf->ra_options.ra_dnssl_list);
388 			SIMPLEQ_INSERT_TAIL(&nconf->ra_iface_list,
389 			    ra_iface_conf, entry);
390 			ra_options = &ra_iface_conf->ra_options;
391 			break;
392 		case IMSG_RECONF_RA_AUTOPREFIX:
393 			if ((ra_iface_conf->autoprefix = malloc(sizeof(struct
394 			    ra_prefix_conf))) == NULL)
395 				fatal(NULL);
396 			memcpy(ra_iface_conf->autoprefix, imsg.data,
397 			    sizeof(struct ra_prefix_conf));
398 			break;
399 		case IMSG_RECONF_RA_PREFIX:
400 			if ((ra_prefix_conf = malloc(sizeof(struct
401 			    ra_prefix_conf))) == NULL)
402 				fatal(NULL);
403 			memcpy(ra_prefix_conf, imsg.data,
404 			    sizeof(struct ra_prefix_conf));
405 			SIMPLEQ_INSERT_TAIL(&ra_iface_conf->ra_prefix_list,
406 			    ra_prefix_conf, entry);
407 			break;
408 		case IMSG_RECONF_RA_RDNSS:
409 			if ((ra_rdnss_conf = malloc(sizeof(struct
410 			    ra_rdnss_conf))) == NULL)
411 				fatal(NULL);
412 			memcpy(ra_rdnss_conf, imsg.data, sizeof(struct
413 			    ra_rdnss_conf));
414 			SIMPLEQ_INSERT_TAIL(&ra_options->ra_rdnss_list,
415 			    ra_rdnss_conf, entry);
416 			break;
417 		case IMSG_RECONF_RA_DNSSL:
418 			if ((ra_dnssl_conf = malloc(sizeof(struct
419 			    ra_dnssl_conf))) == NULL)
420 				fatal(NULL);
421 			memcpy(ra_dnssl_conf, imsg.data, sizeof(struct
422 			    ra_dnssl_conf));
423 			SIMPLEQ_INSERT_TAIL(&ra_options->ra_dnssl_list,
424 			    ra_dnssl_conf, entry);
425 			break;
426 		case IMSG_RECONF_END:
427 			merge_config(frontend_conf, nconf);
428 			merge_ra_interfaces();
429 			nconf = NULL;
430 			break;
431 		case IMSG_ICMP6SOCK:
432 			if ((icmp6sock = imsg.fd) == -1)
433 				fatalx("%s: expected to receive imsg "
434 				    "ICMPv6 fd but didn't receive any",
435 				    __func__);
436 			event_set(&icmp6ev.ev, icmp6sock, EV_READ | EV_PERSIST,
437 			    icmp6_receive, NULL);
438 		case IMSG_ROUTESOCK:
439 			if ((fd = imsg.fd) == -1)
440 				fatalx("%s: expected to receive imsg "
441 				    "routesocket fd but didn't receive any",
442 				    __func__);
443 			event_set(&ev_route, fd, EV_READ | EV_PERSIST,
444 			    route_receive, NULL);
445 			break;
446 		case IMSG_STARTUP:
447 			if (pledge("stdio inet unix route mcast", NULL) == -1)
448 				fatal("pledge");
449 			frontend_startup();
450 			break;
451 		case IMSG_CONTROLFD:
452 			if ((fd = imsg.fd) == -1)
453 				fatalx("%s: expected to receive imsg "
454 				    "control fd but didn't receive any",
455 				    __func__);
456 			control_state.fd = fd;
457 			/* Listen on control socket. */
458 			TAILQ_INIT(&ctl_conns);
459 			control_listen();
460 			break;
461 		default:
462 			log_debug("%s: error handling imsg %d", __func__,
463 			    imsg.hdr.type);
464 			break;
465 		}
466 		imsg_free(&imsg);
467 	}
468 	if (!shut)
469 		imsg_event_add(iev);
470 	else {
471 		/* This pipe is dead. Remove its event handler. */
472 		event_del(&iev->ev);
473 		event_loopexit(NULL);
474 	}
475 }
476 
477 void
478 frontend_dispatch_engine(int fd, short event, void *bula)
479 {
480 	struct imsgev		*iev = bula;
481 	struct imsgbuf		*ibuf = &iev->ibuf;
482 	struct imsg		 imsg;
483 	struct imsg_send_ra	 send_ra;
484 	struct ra_iface		*ra_iface;
485 	uint32_t		 if_index;
486 	int			 n, shut = 0;
487 
488 	if (event & EV_READ) {
489 		if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
490 			fatal("imsg_read error");
491 		if (n == 0)	/* Connection closed. */
492 			shut = 1;
493 	}
494 	if (event & EV_WRITE) {
495 		if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
496 			fatal("msgbuf_write");
497 		if (n == 0)	/* Connection closed. */
498 			shut = 1;
499 	}
500 
501 	for (;;) {
502 		if ((n = imsg_get(ibuf, &imsg)) == -1)
503 			fatal("%s: imsg_get error", __func__);
504 		if (n == 0)	/* No more messages. */
505 			break;
506 
507 		switch (imsg.hdr.type) {
508 		case IMSG_SEND_RA:
509 			if (imsg.hdr.len != IMSG_HEADER_SIZE + sizeof(send_ra))
510 				fatal("%s: IMSG_SEND_RA wrong length: %d",
511 				    __func__, imsg.hdr.len);
512 			memcpy(&send_ra, imsg.data, sizeof(send_ra));
513 			ra_iface = find_ra_iface_by_id(send_ra.if_index);
514 			if (ra_iface)
515 				ra_output(ra_iface, &send_ra.to);
516 			break;
517 		case IMSG_REMOVE_IF:
518 			if (imsg.hdr.len != IMSG_HEADER_SIZE + sizeof(if_index))
519 				fatal("%s: IMSG_REMOVE_IF wrong length: %d",
520 				    __func__, imsg.hdr.len);
521 			memcpy(&if_index, imsg.data, sizeof(if_index));
522 			ra_iface = find_ra_iface_by_id(if_index);
523 			if (ra_iface) {
524 				TAILQ_REMOVE(&ra_interfaces, ra_iface, entry);
525 				free_ra_iface(ra_iface);
526 			}
527 			break;
528 		default:
529 			log_debug("%s: error handling imsg %d", __func__,
530 			    imsg.hdr.type);
531 			break;
532 		}
533 		imsg_free(&imsg);
534 	}
535 	if (!shut)
536 		imsg_event_add(iev);
537 	else {
538 		/* This pipe is dead. Remove its event handler. */
539 		event_del(&iev->ev);
540 		event_loopexit(NULL);
541 	}
542 }
543 
544 void
545 frontend_startup(void)
546 {
547 	if (!event_initialized(&ev_route))
548 		fatalx("%s: did not receive a route socket from the main "
549 		    "process", __func__);
550 
551 	event_add(&ev_route, NULL);
552 
553 	if (!event_initialized(&icmp6ev.ev))
554 		fatalx("%s: did not receive a icmp6 socket fd from the main "
555 		    "process", __func__);
556 
557 	event_add(&icmp6ev.ev, NULL);
558 
559 	frontend_imsg_compose_main(IMSG_STARTUP_DONE, 0, NULL, 0);
560 }
561 
562 
563 void
564 icmp6_receive(int fd, short events, void *arg)
565 {
566 	struct imsg_ra_rs	 ra_rs;
567 	struct in6_pktinfo	*pi = NULL;
568 	struct cmsghdr		*cm;
569 	ssize_t			 len;
570 	int			 if_index = 0, *hlimp = NULL;
571 	char			 ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ];
572 
573 	if ((len = recvmsg(fd, &icmp6ev.rcvmhdr, 0)) < 0) {
574 		log_warn("recvmsg");
575 		return;
576 	}
577 
578 	/* extract optional information via Advanced API */
579 	for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(&icmp6ev.rcvmhdr); cm;
580 	    cm = (struct cmsghdr *)CMSG_NXTHDR(&icmp6ev.rcvmhdr, cm)) {
581 		if (cm->cmsg_level == IPPROTO_IPV6 &&
582 		    cm->cmsg_type == IPV6_PKTINFO &&
583 		    cm->cmsg_len == CMSG_LEN(sizeof(struct in6_pktinfo))) {
584 			pi = (struct in6_pktinfo *)(CMSG_DATA(cm));
585 			if_index = pi->ipi6_ifindex;
586 		}
587 		if (cm->cmsg_level == IPPROTO_IPV6 &&
588 		    cm->cmsg_type == IPV6_HOPLIMIT &&
589 		    cm->cmsg_len == CMSG_LEN(sizeof(int)))
590 			hlimp = (int *)CMSG_DATA(cm);
591 	}
592 
593 	if (if_index == 0) {
594 		log_warnx("failed to get receiving interface");
595 		return;
596 	}
597 
598 	if (hlimp == NULL) {
599 		log_warnx("failed to get receiving hop limit");
600 		return;
601 	}
602 
603 	if (*hlimp != 255) {
604 		log_warnx("invalid RA or RS with hop limit of %d from %s on %s",
605 		    *hlimp, inet_ntop(AF_INET6, &icmp6ev.from.sin6_addr,
606 		    ntopbuf, INET6_ADDRSTRLEN), if_indextoname(if_index,
607 		    ifnamebuf));
608 		return;
609 	}
610 
611 	log_debug("RA or RS with hop limit of %d from %s on %s",
612 	    *hlimp, inet_ntop(AF_INET6, &icmp6ev.from.sin6_addr,
613 	    ntopbuf, INET6_ADDRSTRLEN), if_indextoname(if_index,
614 	    ifnamebuf));
615 
616 	if ((size_t)len > sizeof(ra_rs.packet)) {
617 		log_warnx("invalid RA or RS with size %ld from %s on %s",
618 		    len, inet_ntop(AF_INET6, &icmp6ev.from.sin6_addr,
619 		    ntopbuf, INET6_ADDRSTRLEN), if_indextoname(if_index,
620 		    ifnamebuf));
621 		return;
622 	}
623 
624 	ra_rs.if_index = if_index;
625 	memcpy(&ra_rs.from,  &icmp6ev.from, sizeof(ra_rs.from));
626 	ra_rs.len = len;
627 	memcpy(ra_rs.packet, icmp6ev.answer, len);
628 
629 	frontend_imsg_compose_engine(IMSG_RA_RS, 0, &ra_rs, sizeof(ra_rs));
630 }
631 
632 void
633 join_all_routers_mcast_group(struct ra_iface *ra_iface)
634 {
635 	log_debug("joining multicast group on %s", ra_iface->name);
636 	all_routers.ipv6mr_interface = ra_iface->if_index;
637 	if (setsockopt(icmp6sock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
638 	    &all_routers, sizeof(all_routers)) == -1)
639 		fatal("IPV6_JOIN_GROUP(%s)", ra_iface->name);
640 }
641 
642 void
643 leave_all_routers_mcast_group(struct ra_iface *ra_iface)
644 {
645 	log_debug("leaving multicast group on %s", ra_iface->name);
646 	all_routers.ipv6mr_interface = ra_iface->if_index;
647 	setsockopt(icmp6sock, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
648 	    &all_routers, sizeof(all_routers));
649 }
650 
651 struct ra_iface*
652 find_ra_iface_by_id(uint32_t if_index)
653 {
654 	struct ra_iface	*ra_iface;
655 
656 	TAILQ_FOREACH(ra_iface, &ra_interfaces, entry) {
657 		if (ra_iface->if_index == if_index)
658 			return ra_iface;
659 	}
660 	return (NULL);
661 }
662 
663 struct ra_iface*
664 find_ra_iface_by_name(char *if_name)
665 {
666 	struct ra_iface	*ra_iface;
667 
668 	TAILQ_FOREACH(ra_iface, &ra_interfaces, entry) {
669 		if (strcmp(ra_iface->name, if_name) == 0)
670 			return ra_iface;
671 	}
672 	return (NULL);
673 }
674 
675 struct ra_iface_conf*
676 find_ra_iface_conf(struct ra_iface_conf_head *head, char *if_name)
677 {
678 	struct ra_iface_conf	*ra_iface_conf;
679 
680 	SIMPLEQ_FOREACH(ra_iface_conf, head, entry) {
681 		if (strcmp(ra_iface_conf->name, if_name) == 0)
682 			return ra_iface_conf;
683 	}
684 	return (NULL);
685 }
686 
687 void
688 merge_ra_interface(char *name, char *conf_name)
689 {
690 	struct ra_iface		*ra_iface;
691 	uint32_t		 if_index;
692 
693 	if ((ra_iface = find_ra_iface_by_name(name)) != NULL) {
694 		log_debug("keeping interface %s", name);
695 		ra_iface->removed = 0;
696 		return;
697 	}
698 
699 	log_debug("new interface %s", name);
700 	if ((if_index = if_nametoindex(name)) == 0)
701 		return;
702 	log_debug("adding interface %s", name);
703 	if ((ra_iface = calloc(1, sizeof(*ra_iface))) == NULL)
704 		fatal("%s", __func__);
705 
706 	strlcpy(ra_iface->name, name, sizeof(ra_iface->name));
707 	strlcpy(ra_iface->conf_name, conf_name,
708 	    sizeof(ra_iface->conf_name));
709 
710 	ra_iface->if_index = if_index;
711 	SIMPLEQ_INIT(&ra_iface->prefixes);
712 	TAILQ_INSERT_TAIL(&ra_interfaces, ra_iface, entry);
713 	join_all_routers_mcast_group(ra_iface);
714 }
715 
716 void
717 merge_ra_interfaces(void)
718 {
719 	struct ra_iface_conf	*ra_iface_conf;
720 	struct ra_prefix_conf	*ra_prefix_conf;
721 	struct ra_iface		*ra_iface;
722 	struct ifgroupreq	 ifgr;
723 	struct ifg_req		*ifg;
724 	char			*conf_name;
725 	unsigned int		 len;
726 
727 	TAILQ_FOREACH(ra_iface, &ra_interfaces, entry)
728 		ra_iface->removed = 1;
729 
730 	SIMPLEQ_FOREACH(ra_iface_conf, &frontend_conf->ra_iface_list, entry) {
731 		conf_name = ra_iface_conf->name;
732 
733 		/* check if network interface or group */
734 		if (isdigit((unsigned char)conf_name[strlen(conf_name) - 1])) {
735 			merge_ra_interface(conf_name, conf_name);
736 		} else {
737 			log_debug("interface group %s", conf_name);
738 
739 			memset(&ifgr, 0, sizeof(ifgr));
740 			strlcpy(ifgr.ifgr_name, conf_name,
741 			    sizeof(ifgr.ifgr_name));
742 			if (ioctl(ioctlsock, SIOCGIFGMEMB,
743 			    (caddr_t)&ifgr) == -1)
744 				continue;
745 
746 			len = ifgr.ifgr_len;
747 			if ((ifgr.ifgr_groups = calloc(1, len)) == NULL)
748 				fatal("%s: calloc", __func__);
749 			if (ioctl(ioctlsock, SIOCGIFGMEMB,
750 			    (caddr_t)&ifgr) == -1) {
751 				log_debug("group %s without members",
752 				    conf_name);
753 				free(ifgr.ifgr_groups);
754 				continue;
755 			}
756 
757 			for (ifg = ifgr.ifgr_groups;
758 			    (ifg != NULL) && (len >= sizeof(struct ifg_req));
759 			    ifg++) {
760 				len -= sizeof(struct ifg_req);
761 				merge_ra_interface(ifg->ifgrq_member,
762 				    conf_name);
763 			}
764 			free(ifgr.ifgr_groups);
765 		}
766 	}
767 
768 	TAILQ_FOREACH(ra_iface, &ra_interfaces, entry) {
769 		while ((ra_prefix_conf = SIMPLEQ_FIRST(&ra_iface->prefixes))
770 		    != NULL) {
771 			SIMPLEQ_REMOVE_HEAD(&ra_iface->prefixes,
772 			    entry);
773 			free(ra_prefix_conf);
774 		}
775 		ra_iface->prefix_count = 0;
776 
777 		if (ra_iface->removed) {
778 			log_debug("iface removed: %s", ra_iface->name);
779 			build_leaving_packet(ra_iface);
780 			frontend_imsg_compose_engine(IMSG_REMOVE_IF, 0,
781 			    &ra_iface->if_index, sizeof(ra_iface->if_index));
782 			continue;
783 		}
784 
785 		ra_iface_conf = find_ra_iface_conf(
786 		    &frontend_conf->ra_iface_list, ra_iface->conf_name);
787 
788 		log_debug("add static prefixes for %s", ra_iface->name);
789 
790 		SIMPLEQ_FOREACH(ra_prefix_conf, &ra_iface_conf->ra_prefix_list,
791 		    entry) {
792 			add_new_prefix_to_ra_iface(ra_iface,
793 			    &ra_prefix_conf->prefix,
794 			    ra_prefix_conf->prefixlen, ra_prefix_conf);
795 		}
796 
797 		if (ra_iface_conf->autoprefix)
798 			get_interface_prefixes(ra_iface,
799 			    ra_iface_conf->autoprefix);
800 
801 		build_packet(ra_iface);
802 	}
803 }
804 
805 void
806 free_ra_iface(struct ra_iface *ra_iface)
807 {
808 	struct ra_prefix_conf	*prefix;
809 
810 	leave_all_routers_mcast_group(ra_iface);
811 
812 	while ((prefix = SIMPLEQ_FIRST(&ra_iface->prefixes)) != NULL) {
813 		SIMPLEQ_REMOVE_HEAD(&ra_iface->prefixes, entry);
814 		free(prefix);
815 	}
816 
817 	free(ra_iface);
818 }
819 
820 /* from kame via ifconfig, where it's called prefix() */
821 int
822 in6_mask2prefixlen(struct in6_addr *in6)
823 {
824 	u_char *nam = (u_char *)in6;
825 	int byte, bit, plen = 0, size = sizeof(struct in6_addr);
826 
827 	for (byte = 0; byte < size; byte++, plen += 8)
828 		if (nam[byte] != 0xff)
829 			break;
830 	if (byte == size)
831 		return (plen);
832 	for (bit = 7; bit != 0; bit--, plen++)
833 		if (!(nam[byte] & (1 << bit)))
834 			break;
835 	for (; bit != 0; bit--)
836 		if (nam[byte] & (1 << bit))
837 			return (0);
838 	byte++;
839 	for (; byte < size; byte++)
840 		if (nam[byte])
841 			return (0);
842 	return (plen);
843 }
844 
845 void
846 get_interface_prefixes(struct ra_iface *ra_iface, struct ra_prefix_conf
847     *autoprefix)
848 {
849 	struct in6_ifreq	 ifr6;
850 	struct ifaddrs		*ifap, *ifa;
851 	struct sockaddr_in6	*sin6;
852 	int			 prefixlen;
853 
854 	log_debug("%s: %s", __func__, ra_iface->name);
855 
856 	if (getifaddrs(&ifap) != 0)
857 		fatal("getifaddrs");
858 
859 	for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
860 		if (strcmp(ra_iface->name, ifa->ifa_name) != 0)
861 			continue;
862 		if (ifa->ifa_addr->sa_family != AF_INET6)
863 			continue;
864 
865 		sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
866 
867 		if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
868 			continue;
869 
870 		memset(&ifr6, 0, sizeof(ifr6));
871 		(void) strlcpy(ifr6.ifr_name, ra_iface->name,
872 		    sizeof(ifr6.ifr_name));
873 		memcpy(&ifr6.ifr_addr, sin6, sizeof(ifr6.ifr_addr));
874 
875 		if (ioctl(ioctlsock, SIOCGIFNETMASK_IN6, (caddr_t)&ifr6) < 0)
876 			continue; /* addr got deleted while we were looking */
877 
878 		prefixlen = in6_mask2prefixlen(&((struct sockaddr_in6 *)
879 		    &ifr6.ifr_addr)->sin6_addr);
880 
881 		if (prefixlen == 128)
882 			continue;
883 
884 		mask_prefix(&sin6->sin6_addr, prefixlen);
885 
886 		add_new_prefix_to_ra_iface(ra_iface, &sin6->sin6_addr,
887 		    prefixlen, autoprefix);
888 	}
889 	freeifaddrs(ifap);
890 }
891 
892 struct ra_prefix_conf*
893 find_ra_prefix_conf(struct ra_prefix_conf_head* head, struct in6_addr *prefix,
894     int prefixlen)
895 {
896 	struct ra_prefix_conf	*ra_prefix_conf;
897 
898 	SIMPLEQ_FOREACH(ra_prefix_conf, head, entry) {
899 		if (ra_prefix_conf->prefixlen == prefixlen &&
900 		    memcmp(&ra_prefix_conf->prefix, prefix, sizeof(*prefix)) ==
901 		    0)
902 			return (ra_prefix_conf);
903 	}
904 	return (NULL);
905 }
906 
907 void
908 add_new_prefix_to_ra_iface(struct ra_iface *ra_iface, struct in6_addr *addr,
909     int prefixlen, struct ra_prefix_conf *ra_prefix_conf)
910 {
911 	struct ra_prefix_conf	*new_ra_prefix_conf;
912 
913 	if (find_ra_prefix_conf(&ra_iface->prefixes, addr, prefixlen)) {
914 		log_debug("ignoring duplicate %s/%d prefix",
915 		    in6_to_str(addr), prefixlen);
916 		return;
917 	}
918 
919 	log_debug("adding %s/%d prefix", in6_to_str(addr), prefixlen);
920 
921 	if ((new_ra_prefix_conf = calloc(1, sizeof(*ra_prefix_conf))) == NULL)
922 		fatal("%s", __func__);
923 	new_ra_prefix_conf->prefix = *addr;
924 	new_ra_prefix_conf->prefixlen = prefixlen;
925 	new_ra_prefix_conf->vltime = ra_prefix_conf->vltime;
926 	new_ra_prefix_conf->pltime = ra_prefix_conf->pltime;
927 	new_ra_prefix_conf->aflag = ra_prefix_conf->aflag;
928 	new_ra_prefix_conf->lflag = ra_prefix_conf->lflag;
929 	SIMPLEQ_INSERT_TAIL(&ra_iface->prefixes, new_ra_prefix_conf, entry);
930 	ra_iface->prefix_count++;
931 }
932 
933 void
934 build_packet(struct ra_iface *ra_iface)
935 {
936 	struct nd_router_advert		*ra;
937 	struct nd_opt_mtu		*ndopt_mtu;
938 	struct nd_opt_prefix_info	*ndopt_pi;
939 	struct ra_iface_conf		*ra_iface_conf;
940 	struct ra_options_conf		*ra_options_conf;
941 	struct ra_prefix_conf		*ra_prefix_conf;
942 	struct nd_opt_rdnss		*ndopt_rdnss;
943 	struct nd_opt_dnssl		*ndopt_dnssl;
944 	struct ra_rdnss_conf		*ra_rdnss;
945 	struct ra_dnssl_conf		*ra_dnssl;
946 	size_t				 len, label_len;
947 	uint8_t				*p, buf[RA_MAX_SIZE];
948 	char				*label_start, *label_end;
949 
950 	ra_iface_conf = find_ra_iface_conf(&frontend_conf->ra_iface_list,
951 	    ra_iface->conf_name);
952 	ra_options_conf = &ra_iface_conf->ra_options;
953 
954 	len = sizeof(*ra);
955 	if (ra_options_conf->mtu > 0)
956 		len += sizeof(*ndopt_mtu);
957 	len += sizeof(*ndopt_pi) * ra_iface->prefix_count;
958 	if (ra_iface_conf->ra_options.rdnss_count > 0)
959 		len += sizeof(*ndopt_rdnss) +
960 		    ra_iface_conf->ra_options.rdnss_count *
961 		    sizeof(struct in6_addr);
962 
963 	if (ra_iface_conf->ra_options.dnssl_len > 0)
964 		/* round up to 8 byte boundary */
965 		len += sizeof(*ndopt_dnssl) +
966 		    ((ra_iface_conf->ra_options.dnssl_len + 7) & ~7);
967 
968 	if (len > sizeof(ra_iface->data))
969 		fatal("%s: packet too big", __func__); /* XXX send multiple */
970 
971 	p = buf;
972 
973 	ra = (struct nd_router_advert *)p;
974 
975 	memset(ra, 0, sizeof(*ra));
976 
977 	ra->nd_ra_type = ND_ROUTER_ADVERT;
978 	ra->nd_ra_curhoplimit = ra_options_conf->cur_hl;
979 	if (ra_options_conf->m_flag)
980 		ra->nd_ra_flags_reserved |= ND_RA_FLAG_MANAGED;
981 	if (ra_options_conf->o_flag)
982 		ra->nd_ra_flags_reserved |= ND_RA_FLAG_OTHER;
983 	if (ra_iface->removed)
984 		/* tell clients that we are no longer a default router */
985 		ra->nd_ra_router_lifetime = 0;
986 	else if (ra_options_conf->dfr) {
987 		ra->nd_ra_router_lifetime =
988 		    htons(ra_options_conf->router_lifetime);
989 	}
990 	ra->nd_ra_reachable = htonl(ra_options_conf->reachable_time);
991 	ra->nd_ra_retransmit = htonl(ra_options_conf->retrans_timer);
992 	p += sizeof(*ra);
993 
994 	if (ra_options_conf->mtu > 0) {
995 		ndopt_mtu = (struct nd_opt_mtu *)p;
996 		ndopt_mtu->nd_opt_mtu_type = ND_OPT_MTU;
997 		ndopt_mtu->nd_opt_mtu_len = 1;
998 		ndopt_mtu->nd_opt_mtu_reserved = 0;
999 		ndopt_mtu->nd_opt_mtu_mtu = htonl(ra_options_conf->mtu);
1000 		p += sizeof(*ndopt_mtu);
1001 	}
1002 
1003 	SIMPLEQ_FOREACH(ra_prefix_conf, &ra_iface->prefixes, entry) {
1004 		ndopt_pi = (struct nd_opt_prefix_info *)p;
1005 		memset(ndopt_pi, 0, sizeof(*ndopt_pi));
1006 		ndopt_pi->nd_opt_pi_type = ND_OPT_PREFIX_INFORMATION;
1007 		ndopt_pi->nd_opt_pi_len = 4;
1008 		ndopt_pi->nd_opt_pi_prefix_len = ra_prefix_conf->prefixlen;
1009 		if (ra_prefix_conf->lflag)
1010 			ndopt_pi->nd_opt_pi_flags_reserved |=
1011 			    ND_OPT_PI_FLAG_ONLINK;
1012 		if (ra_prefix_conf->aflag)
1013 			ndopt_pi->nd_opt_pi_flags_reserved |=
1014 			    ND_OPT_PI_FLAG_AUTO;
1015 		ndopt_pi->nd_opt_pi_valid_time = htonl(ra_prefix_conf->vltime);
1016 		ndopt_pi->nd_opt_pi_preferred_time =
1017 		    htonl(ra_prefix_conf->pltime);
1018 		ndopt_pi->nd_opt_pi_prefix = ra_prefix_conf->prefix;
1019 
1020 		p += sizeof(*ndopt_pi);
1021 	}
1022 
1023 	if (ra_iface_conf->ra_options.rdnss_count > 0) {
1024 		ndopt_rdnss = (struct nd_opt_rdnss *)p;
1025 		ndopt_rdnss->nd_opt_rdnss_type = ND_OPT_RDNSS;
1026 		ndopt_rdnss->nd_opt_rdnss_len = 1 +
1027 		    ra_iface_conf->ra_options.rdnss_count * 2;
1028 		ndopt_rdnss->nd_opt_rdnss_reserved = 0;
1029 		ndopt_rdnss->nd_opt_rdnss_lifetime =
1030 		    htonl(ra_iface_conf->ra_options.rdns_lifetime);
1031 		p += sizeof(struct nd_opt_rdnss);
1032 		SIMPLEQ_FOREACH(ra_rdnss,
1033 		    &ra_iface_conf->ra_options.ra_rdnss_list, entry) {
1034 			memcpy(p, &ra_rdnss->rdnss, sizeof(ra_rdnss->rdnss));
1035 			p += sizeof(ra_rdnss->rdnss);
1036 		}
1037 	}
1038 
1039 	if (ra_iface_conf->ra_options.dnssl_len > 0) {
1040 		ndopt_dnssl = (struct nd_opt_dnssl *)p;
1041 		ndopt_dnssl->nd_opt_dnssl_type = ND_OPT_DNSSL;
1042 		/* round up to 8 byte boundary */
1043 		ndopt_dnssl->nd_opt_dnssl_len = 1 +
1044 		    ((ra_iface_conf->ra_options.dnssl_len + 7) & ~7) / 8;
1045 		ndopt_dnssl->nd_opt_dnssl_reserved = 0;
1046 		ndopt_dnssl->nd_opt_dnssl_lifetime =
1047 		    htonl(ra_iface_conf->ra_options.rdns_lifetime);
1048 		p += sizeof(struct nd_opt_dnssl);
1049 
1050 		SIMPLEQ_FOREACH(ra_dnssl,
1051 		    &ra_iface_conf->ra_options.ra_dnssl_list, entry) {
1052 			label_start = ra_dnssl->search;
1053 			while ((label_end = strchr(label_start, '.')) != NULL) {
1054 				label_len = label_end - label_start;
1055 				*p++ = label_len;
1056 				memcpy(p, label_start, label_len);
1057 				p += label_len;
1058 				label_start = label_end + 1;
1059 			}
1060 			*p++ = '\0'; /* last dot */
1061 		}
1062 		/* zero pad */
1063 		while (((uintptr_t)p) % 8 != 0)
1064 			*p++ = '\0';
1065 	}
1066 
1067 	if (len != ra_iface->datalen || memcmp(buf, ra_iface->data, len)
1068 	    != 0) {
1069 		memcpy(ra_iface->data, buf, len);
1070 		ra_iface->datalen = len;
1071 		/* packet changed; tell engine to send new advertisments */
1072 		frontend_imsg_compose_engine(IMSG_UPDATE_IF, 0,
1073 		    &ra_iface->if_index, sizeof(ra_iface->if_index));
1074 	}
1075 }
1076 
1077 void
1078 build_leaving_packet(struct ra_iface *ra_iface)
1079 {
1080 	struct nd_router_advert		 ra;
1081 	size_t				 len;
1082 
1083 	len = sizeof(ra);
1084 
1085 	memset(&ra, 0, sizeof(ra));
1086 
1087 	ra.nd_ra_type = ND_ROUTER_ADVERT;
1088 
1089 	memcpy(ra_iface->data, &ra, sizeof(ra));
1090 	ra_iface->datalen = sizeof(ra);
1091 }
1092 
1093 void
1094 ra_output(struct ra_iface *ra_iface, struct sockaddr_in6 *to)
1095 {
1096 
1097 	struct cmsghdr		*cm;
1098 	struct in6_pktinfo	*pi;
1099 	ssize_t			 len;
1100 	int			 hoplimit = 255;
1101 
1102 	sndmhdr.msg_name = to;
1103 	sndmhdr.msg_iov[0].iov_base = ra_iface->data;
1104 	sndmhdr.msg_iov[0].iov_len = ra_iface->datalen;
1105 
1106 	cm = CMSG_FIRSTHDR(&sndmhdr);
1107 	/* specify the outgoing interface */
1108 	cm->cmsg_level = IPPROTO_IPV6;
1109 	cm->cmsg_type = IPV6_PKTINFO;
1110 	cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
1111 	pi = (struct in6_pktinfo *)CMSG_DATA(cm);
1112 	memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr));
1113 	pi->ipi6_ifindex = ra_iface->if_index;
1114 
1115 	/* specify the hop limit of the packet */
1116 	cm = CMSG_NXTHDR(&sndmhdr, cm);
1117 	cm->cmsg_level = IPPROTO_IPV6;
1118 	cm->cmsg_type = IPV6_HOPLIMIT;
1119 	cm->cmsg_len = CMSG_LEN(sizeof(int));
1120 	memcpy(CMSG_DATA(cm), &hoplimit, sizeof(int));
1121 
1122 	log_debug("send RA on %s", ra_iface->name);
1123 
1124 	len = sendmsg(icmp6sock, &sndmhdr, 0);
1125 	if (len < 0)
1126 		log_warn("sendmsg on %s", ra_iface->name);
1127 
1128 }
1129 
1130 #define ROUNDUP(a) \
1131 	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
1132 
1133 void
1134 get_rtaddrs(int addrs, struct sockaddr *sa, struct sockaddr **rti_info)
1135 {
1136 	int	i;
1137 
1138 	for (i = 0; i < RTAX_MAX; i++) {
1139 		if (addrs & (1 << i)) {
1140 			rti_info[i] = sa;
1141 			sa = (struct sockaddr *)((char *)(sa) +
1142 			    ROUNDUP(sa->sa_len));
1143 		} else
1144 			rti_info[i] = NULL;
1145 	}
1146 }
1147 
1148 void
1149 route_receive(int fd, short events, void *arg)
1150 {
1151 	static uint8_t			 *buf;
1152 
1153 	struct rt_msghdr		*rtm;
1154 	struct sockaddr			*sa, *rti_info[RTAX_MAX];
1155 	ssize_t				 n;
1156 
1157 	if (buf == NULL) {
1158 		buf = malloc(ROUTE_SOCKET_BUF_SIZE);
1159 		if (buf == NULL)
1160 			fatal("malloc");
1161 	}
1162 	rtm = (struct rt_msghdr *)buf;
1163 	if ((n = read(fd, buf, ROUTE_SOCKET_BUF_SIZE)) == -1) {
1164 		if (errno == EAGAIN || errno == EINTR)
1165 			return;
1166 		log_warn("dispatch_rtmsg: read error");
1167 		return;
1168 	}
1169 
1170 	if (n == 0)
1171 		fatal("routing socket closed");
1172 
1173 	if (n < (ssize_t)sizeof(rtm->rtm_msglen) || n < rtm->rtm_msglen) {
1174 		log_warnx("partial rtm of %zd in buffer", n);
1175 		return;
1176 	}
1177 
1178 	if (rtm->rtm_version != RTM_VERSION)
1179 		return;
1180 
1181 	sa = (struct sockaddr *)(buf + rtm->rtm_hdrlen);
1182 	get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
1183 
1184 	handle_route_message(rtm, rti_info);
1185 }
1186 
1187 void
1188 handle_route_message(struct rt_msghdr *rtm, struct sockaddr **rti_info)
1189 {
1190 	switch (rtm->rtm_type) {
1191 	case RTM_IFINFO:
1192 	case RTM_NEWADDR:
1193 	case RTM_DELADDR:
1194 		/*
1195 		 * do the same thing as after a config reload when interfaces
1196 		 * change or IPv6 addresses show up / disappear
1197 		 */
1198 		merge_ra_interfaces();
1199 		break;
1200 	default:
1201 		log_debug("unexpected RTM: %d", rtm->rtm_type);
1202 		break;
1203 	}
1204 }
1205