xref: /freebsd/usr.sbin/rtadvd/if.c (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
1 /*	$KAME: if.c,v 1.17 2001/01/21 15:27:30 itojun Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7  * Copyright (C) 2011 Hiroki Sato <hrs@FreeBSD.org>
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the project nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #include <sys/param.h>
36 #include <sys/socket.h>
37 #include <sys/sysctl.h>
38 #include <sys/ioctl.h>
39 #include <net/if.h>
40 #include <net/if_dl.h>
41 #include <net/if_types.h>
42 #include <net/ethernet.h>
43 #include <net/route.h>
44 #include <netinet/in.h>
45 #include <netinet/in_var.h>
46 #include <netinet/ip6.h>
47 #include <netinet/icmp6.h>
48 #include <netinet6/nd6.h>
49 #include <unistd.h>
50 #include <errno.h>
51 #include <netdb.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <syslog.h>
55 
56 #include "pathnames.h"
57 #include "rtadvd.h"
58 #include "if.h"
59 
60 #define ROUNDUP(a, size)					\
61 	(((a) & ((size)-1)) ? (1 + ((a) | ((size)-1))) : (a))
62 
63 #define	NEXT_SA(ap)							\
64 	(ap) = (struct sockaddr *)((caddr_t)(ap) +			\
65 	    ((ap)->sa_len ? ROUNDUP((ap)->sa_len, sizeof(u_long)) :	\
66 	    sizeof(u_long)))
67 
68 struct sockaddr_in6 sin6_linklocal_allnodes = {
69         .sin6_len =     sizeof(sin6_linklocal_allnodes),
70         .sin6_family =  AF_INET6,
71         .sin6_addr =    IN6ADDR_LINKLOCAL_ALLNODES_INIT,
72 };
73 
74 struct sockaddr_in6 sin6_linklocal_allrouters = {
75         .sin6_len =     sizeof(sin6_linklocal_allrouters),
76         .sin6_family =  AF_INET6,
77         .sin6_addr =    IN6ADDR_LINKLOCAL_ALLROUTERS_INIT,
78 };
79 
80 struct sockaddr_in6 sin6_sitelocal_allrouters = {
81         .sin6_len =     sizeof(sin6_sitelocal_allrouters),
82         .sin6_family =  AF_INET6,
83         .sin6_addr =    IN6ADDR_SITELOCAL_ALLROUTERS_INIT,
84 };
85 
86 struct sockinfo sock = { .si_fd = -1, .si_name = NULL };
87 struct sockinfo rtsock = { .si_fd = -1, .si_name = NULL };
88 struct sockinfo ctrlsock = { .si_fd = -1, .si_name = _PATH_CTRL_SOCK };
89 
90 char *mcastif;
91 
92 static void		get_rtaddrs(int, struct sockaddr *,
93 			    struct sockaddr **);
94 static struct if_msghdr	*get_next_msghdr(struct if_msghdr *,
95 			    struct if_msghdr *);
96 
97 static void
98 get_rtaddrs(int addrs, struct sockaddr *sa, struct sockaddr **rti_info)
99 {
100 	int i;
101 
102 	for (i = 0; i < RTAX_MAX; i++) {
103 		if (addrs & (1 << i)) {
104 			rti_info[i] = sa;
105 			NEXT_SA(sa);
106 		}
107 		else
108 			rti_info[i] = NULL;
109 	}
110 }
111 
112 #define ROUNDUP8(a) (1 + (((a) - 1) | 7))
113 int
114 lladdropt_length(struct sockaddr_dl *sdl)
115 {
116 	switch (sdl->sdl_type) {
117 	case IFT_ETHER:
118 	case IFT_L2VLAN:
119 	case IFT_BRIDGE:
120 		return (ROUNDUP8(ETHER_ADDR_LEN + 2));
121 	default:
122 		return (0);
123 	}
124 }
125 
126 void
127 lladdropt_fill(struct sockaddr_dl *sdl, struct nd_opt_hdr *ndopt)
128 {
129 	char *addr;
130 
131 	ndopt->nd_opt_type = ND_OPT_SOURCE_LINKADDR; /* fixed */
132 
133 	switch (sdl->sdl_type) {
134 	case IFT_ETHER:
135 	case IFT_L2VLAN:
136 	case IFT_BRIDGE:
137 		ndopt->nd_opt_len = (ROUNDUP8(ETHER_ADDR_LEN + 2)) >> 3;
138 		addr = (char *)(ndopt + 1);
139 		memcpy(addr, LLADDR(sdl), ETHER_ADDR_LEN);
140 		break;
141 	default:
142 		syslog(LOG_ERR, "<%s> unsupported link type(%d)",
143 		    __func__, sdl->sdl_type);
144 		exit(1);
145 	}
146 
147 	return;
148 }
149 
150 int
151 rtbuf_len(void)
152 {
153 	size_t len;
154 	int mib[6] = {CTL_NET, AF_ROUTE, 0, AF_INET6, NET_RT_DUMP, 0};
155 
156 	if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0)
157 		return (-1);
158 
159 	return (len);
160 }
161 
162 #define FILTER_MATCH(type, filter) ((0x1 << type) & filter)
163 #define SIN6(s) ((struct sockaddr_in6 *)(s))
164 #define SDL(s) ((struct sockaddr_dl *)(s))
165 char *
166 get_next_msg(char *buf, char *lim, int ifindex, size_t *lenp, int filter)
167 {
168 	struct rt_msghdr *rtm;
169 	struct ifa_msghdr *ifam;
170 	struct sockaddr *sa, *dst, *gw, *ifa, *rti_info[RTAX_MAX];
171 
172 	*lenp = 0;
173 	for (rtm = (struct rt_msghdr *)buf;
174 	     rtm < (struct rt_msghdr *)lim;
175 	     rtm = (struct rt_msghdr *)(((char *)rtm) + rtm->rtm_msglen)) {
176 		/* just for safety */
177 		if (!rtm->rtm_msglen) {
178 			syslog(LOG_WARNING, "<%s> rtm_msglen is 0 "
179 			    "(buf=%p lim=%p rtm=%p)", __func__,
180 			    buf, lim, rtm);
181 			break;
182 		}
183 		if (((struct rt_msghdr *)buf)->rtm_version != RTM_VERSION) {
184 			syslog(LOG_WARNING,
185 			    "<%s> routing message version mismatch "
186 			    "(buf=%p lim=%p rtm=%p)", __func__,
187 			    buf, lim, rtm);
188 			continue;
189 		}
190 
191 		if (FILTER_MATCH(rtm->rtm_type, filter) == 0)
192 			continue;
193 
194 		switch (rtm->rtm_type) {
195 		case RTM_GET:
196 		case RTM_ADD:
197 		case RTM_DELETE:
198 			/* address related checks */
199 			sa = (struct sockaddr *)(rtm + 1);
200 			get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
201 			if ((dst = rti_info[RTAX_DST]) == NULL ||
202 			    dst->sa_family != AF_INET6)
203 				continue;
204 
205 			if (IN6_IS_ADDR_LINKLOCAL(&SIN6(dst)->sin6_addr) ||
206 			    IN6_IS_ADDR_MULTICAST(&SIN6(dst)->sin6_addr))
207 				continue;
208 
209 			if ((gw = rti_info[RTAX_GATEWAY]) == NULL ||
210 			    gw->sa_family != AF_LINK)
211 				continue;
212 			if (ifindex && SDL(gw)->sdl_index != ifindex)
213 				continue;
214 
215 			if (rti_info[RTAX_NETMASK] == NULL)
216 				continue;
217 
218 			/* found */
219 			*lenp = rtm->rtm_msglen;
220 			return (char *)rtm;
221 			/* NOTREACHED */
222 		case RTM_NEWADDR:
223 		case RTM_DELADDR:
224 			ifam = (struct ifa_msghdr *)rtm;
225 
226 			/* address related checks */
227 			sa = (struct sockaddr *)(ifam + 1);
228 			get_rtaddrs(ifam->ifam_addrs, sa, rti_info);
229 			if ((ifa = rti_info[RTAX_IFA]) == NULL ||
230 			    (ifa->sa_family != AF_INET &&
231 			     ifa->sa_family != AF_INET6))
232 				continue;
233 
234 			if (ifa->sa_family == AF_INET6 &&
235 			    (IN6_IS_ADDR_LINKLOCAL(&SIN6(ifa)->sin6_addr) ||
236 			     IN6_IS_ADDR_MULTICAST(&SIN6(ifa)->sin6_addr)))
237 				continue;
238 
239 			if (ifindex && ifam->ifam_index != ifindex)
240 				continue;
241 
242 			/* found */
243 			*lenp = ifam->ifam_msglen;
244 			return (char *)rtm;
245 			/* NOTREACHED */
246 		case RTM_IFINFO:
247 		case RTM_IFANNOUNCE:
248 			/* found */
249 			*lenp = rtm->rtm_msglen;
250 			return (char *)rtm;
251 			/* NOTREACHED */
252 		}
253 	}
254 
255 	return ((char *)rtm);
256 }
257 #undef FILTER_MATCH
258 
259 struct in6_addr *
260 get_addr(char *buf)
261 {
262 	struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
263 	struct sockaddr *sa, *rti_info[RTAX_MAX];
264 
265 	sa = (struct sockaddr *)(rtm + 1);
266 	get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
267 
268 	return (&SIN6(rti_info[RTAX_DST])->sin6_addr);
269 }
270 
271 int
272 get_rtm_ifindex(char *buf)
273 {
274 	struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
275 	struct sockaddr *sa, *rti_info[RTAX_MAX];
276 
277 	sa = (struct sockaddr *)(rtm + 1);
278 	get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
279 
280 	return (((struct sockaddr_dl *)rti_info[RTAX_GATEWAY])->sdl_index);
281 }
282 
283 int
284 get_prefixlen(char *buf)
285 {
286 	struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
287 	struct sockaddr *sa, *rti_info[RTAX_MAX];
288 	char *p, *lim;
289 
290 	sa = (struct sockaddr *)(rtm + 1);
291 	get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
292 	sa = rti_info[RTAX_NETMASK];
293 
294 	p = (char *)(&SIN6(sa)->sin6_addr);
295 	lim = (char *)sa + sa->sa_len;
296 	return prefixlen(p, lim);
297 }
298 
299 int
300 prefixlen(unsigned char *p, unsigned char *lim)
301 {
302 	int masklen;
303 
304 	for (masklen = 0; p < lim; p++) {
305 		switch (*p) {
306 		case 0xff:
307 			masklen += 8;
308 			break;
309 		case 0xfe:
310 			masklen += 7;
311 			break;
312 		case 0xfc:
313 			masklen += 6;
314 			break;
315 		case 0xf8:
316 			masklen += 5;
317 			break;
318 		case 0xf0:
319 			masklen += 4;
320 			break;
321 		case 0xe0:
322 			masklen += 3;
323 			break;
324 		case 0xc0:
325 			masklen += 2;
326 			break;
327 		case 0x80:
328 			masklen += 1;
329 			break;
330 		case 0x00:
331 			break;
332 		default:
333 			return (-1);
334 		}
335 	}
336 
337 	return (masklen);
338 }
339 
340 struct ifinfo *
341 update_persist_ifinfo(struct ifilist_head_t *ifi_head, const char *ifname)
342 {
343 	struct ifinfo *ifi;
344 	int ifindex;
345 
346 	ifi = NULL;
347 	ifindex = if_nametoindex(ifname);
348 	TAILQ_FOREACH(ifi, ifi_head, ifi_next) {
349 		if (ifindex != 0) {
350 			if (ifindex == ifi->ifi_ifindex)
351 				break;
352 		} else {
353 			if (strncmp(ifname, ifi->ifi_ifname,
354 				sizeof(ifi->ifi_ifname)) == 0)
355 				break;
356 		}
357 	}
358 
359 	if (ifi == NULL) {
360 		/* A new ifinfo element is needed. */
361 		syslog(LOG_DEBUG, "<%s> new entry: %s", __func__,
362 		    ifname);
363 
364 		ELM_MALLOC(ifi, exit(1));
365 		ifi->ifi_ifindex = 0;
366 		strlcpy(ifi->ifi_ifname, ifname, sizeof(ifi->ifi_ifname));
367 		ifi->ifi_rainfo = NULL;
368 		ifi->ifi_state = IFI_STATE_UNCONFIGURED;
369 		TAILQ_INSERT_TAIL(ifi_head, ifi, ifi_next);
370 	}
371 
372 	ifi->ifi_persist = 1;
373 
374 	syslog(LOG_DEBUG, "<%s> %s is marked PERSIST", __func__,
375 	    ifi->ifi_ifname);
376 	syslog(LOG_DEBUG, "<%s> %s is state = %d", __func__,
377 	    ifi->ifi_ifname, ifi->ifi_state);
378 	return (ifi);
379 }
380 
381 int
382 update_ifinfo_nd_flags(struct ifinfo *ifi)
383 {
384 	struct in6_ndireq nd;
385 	int s;
386 	int error;
387 
388 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
389 		syslog(LOG_ERR,
390 		    "<%s> socket() failed.", __func__);
391 		return (1);
392 	}
393 	/* ND flags */
394 	memset(&nd, 0, sizeof(nd));
395 	strlcpy(nd.ifname, ifi->ifi_ifname,
396 	    sizeof(nd.ifname));
397 	error = ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd);
398 	if (error) {
399 		close(s);
400 		if (errno != EPFNOSUPPORT)
401 			syslog(LOG_ERR, "<%s> ioctl() failed.", __func__);
402 		return (1);
403 	}
404 	ifi->ifi_nd_flags = nd.ndi.flags;
405 	close(s);
406 
407 	return (0);
408 }
409 
410 #define MAX_SYSCTL_TRY 5
411 
412 struct ifinfo *
413 update_ifinfo(struct ifilist_head_t *ifi_head, int ifindex)
414 {
415 	struct if_msghdr *ifm;
416 	struct ifinfo *ifi = NULL;
417 	struct sockaddr *sa;
418 	struct sockaddr *rti_info[RTAX_MAX];
419 	char *msg;
420 	size_t len;
421 	char *lim;
422 	int mib[] = { CTL_NET, PF_ROUTE, 0, AF_INET6, NET_RT_IFLIST, 0 };
423 	int error, ntry;
424 
425 	syslog(LOG_DEBUG, "<%s> enter", __func__);
426 
427 	ntry = 0;
428 	do {
429 		/*
430 		 * We'll try to get addresses several times in case that
431 		 * the number of addresses is unexpectedly increased during
432 		 * the two sysctl calls.  This should rarely happen.
433 		 * Portability note: since FreeBSD does not add margin of
434 		 * memory at the first sysctl, the possibility of failure on
435 		 * the second sysctl call is a bit higher.
436 		 */
437 
438 		if (sysctl(mib, nitems(mib), NULL, &len, NULL, 0) < 0) {
439 			syslog(LOG_ERR,
440 			    "<%s> sysctl: NET_RT_IFLIST size get failed",
441 			    __func__);
442 			exit(1);
443 		}
444 		if ((msg = malloc(len)) == NULL) {
445 			syslog(LOG_ERR, "<%s> malloc failed", __func__);
446 			exit(1);
447 		}
448 		if (sysctl(mib, nitems(mib), msg, &len, NULL, 0) < 0) {
449 			if (errno != ENOMEM || ++ntry >= MAX_SYSCTL_TRY) {
450 				free(msg);
451 				syslog(LOG_ERR,
452 				    "<%s> sysctl: NET_RT_IFLIST get failed",
453 				    __func__);
454 				exit(1);
455 			}
456 			free(msg);
457 			msg = NULL;
458 		}
459 	} while (msg == NULL);
460 
461 	lim = msg + len;
462 	for (ifm = (struct if_msghdr *)msg;
463 	     ifm != NULL && ifm < (struct if_msghdr *)lim;
464 	     ifm = get_next_msghdr(ifm,(struct if_msghdr *)lim)) {
465 		int ifi_new;
466 
467 		syslog(LOG_DEBUG, "<%s> ifm = %p, lim = %p, diff = %zu",
468 		    __func__, ifm, lim, (char *)lim - (char *)ifm);
469 
470 		if (ifm->ifm_version != RTM_VERSION) {
471 			syslog(LOG_ERR,
472 			    "<%s> ifm_vesrion mismatch", __func__);
473 			exit(1);
474 		}
475 		if (ifm->ifm_msglen == 0) {
476 			syslog(LOG_WARNING,
477 			    "<%s> ifm_msglen is 0", __func__);
478 			free(msg);
479 			return (NULL);
480 		}
481 
482 		ifi_new = 0;
483 		if (ifm->ifm_type == RTM_IFINFO) {
484 			struct ifreq ifr;
485 			int s;
486 			char ifname[IFNAMSIZ];
487 
488 			syslog(LOG_DEBUG, "<%s> RTM_IFINFO found. "
489 			    "ifm_index = %d, ifindex = %d",
490 			    __func__, ifm->ifm_index, ifindex);
491 
492 			/* when ifindex is specified */
493 			if (ifindex != UPDATE_IFINFO_ALL &&
494 			    ifindex != ifm->ifm_index)
495 				continue;
496 
497 			/* ifname */
498 			if (if_indextoname(ifm->ifm_index, ifname) == NULL) {
499 				syslog(LOG_WARNING,
500 				    "<%s> ifname not found (idx=%d)",
501 				    __func__, ifm->ifm_index);
502 				continue;
503 			}
504 
505 			/* lookup an entry with the same ifindex */
506 			TAILQ_FOREACH(ifi, ifi_head, ifi_next) {
507 				if (ifm->ifm_index == ifi->ifi_ifindex)
508 					break;
509 				if (strncmp(ifname, ifi->ifi_ifname,
510 					sizeof(ifname)) == 0)
511 					break;
512 			}
513 			if (ifi == NULL) {
514 				syslog(LOG_DEBUG,
515 				    "<%s> new entry for idx=%d",
516 				    __func__, ifm->ifm_index);
517 				ELM_MALLOC(ifi, exit(1));
518 				ifi->ifi_rainfo = NULL;
519 				ifi->ifi_state = IFI_STATE_UNCONFIGURED;
520 				ifi->ifi_persist = 0;
521 				ifi_new = 1;
522 			}
523 			/* ifindex */
524 			ifi->ifi_ifindex = ifm->ifm_index;
525 
526 			/* ifname */
527 			strlcpy(ifi->ifi_ifname, ifname, IFNAMSIZ);
528 
529 			if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
530 				syslog(LOG_ERR,
531 				    "<%s> socket() failed.", __func__);
532 				if (ifi_new)
533 					free(ifi);
534 				continue;
535 			}
536 
537 			/* MTU  */
538 			ifi->ifi_phymtu = ifm->ifm_data.ifi_mtu;
539 			if (ifi->ifi_phymtu == 0) {
540 				memset(&ifr, 0, sizeof(ifr));
541 				ifr.ifr_addr.sa_family = AF_INET6;
542 				strlcpy(ifr.ifr_name, ifi->ifi_ifname,
543 				    sizeof(ifr.ifr_name));
544 				error = ioctl(s, SIOCGIFMTU, (caddr_t)&ifr);
545 				if (error) {
546 					close(s);
547 					syslog(LOG_ERR,
548 					    "<%s> ioctl() failed.",
549 					    __func__);
550 					if (ifi_new)
551 						free(ifi);
552 					continue;
553 				}
554 				ifi->ifi_phymtu = ifr.ifr_mtu;
555 				if (ifi->ifi_phymtu == 0) {
556 					syslog(LOG_WARNING,
557 					    "<%s> no interface mtu info"
558 					    " on %s.  %d will be used.",
559 					    __func__, ifi->ifi_ifname,
560 					    IPV6_MMTU);
561 					ifi->ifi_phymtu = IPV6_MMTU;
562 				}
563 			}
564 			close(s);
565 
566 			/* ND flags */
567 			error = update_ifinfo_nd_flags(ifi);
568 			if (error) {
569 				if (ifi_new)
570 					free(ifi);
571 				continue;
572 			}
573 
574 			/* SDL */
575 			sa = (struct sockaddr *)(ifm + 1);
576 			get_rtaddrs(ifm->ifm_addrs, sa, rti_info);
577 			if ((sa = rti_info[RTAX_IFP]) != NULL) {
578 				if (sa->sa_family == AF_LINK) {
579 					memcpy(&ifi->ifi_sdl,
580 					    (struct sockaddr_dl *)sa,
581 					    sizeof(ifi->ifi_sdl));
582 				}
583 			} else
584 				memset(&ifi->ifi_sdl, 0,
585 				    sizeof(ifi->ifi_sdl));
586 
587 			/* flags */
588 			ifi->ifi_flags = ifm->ifm_flags;
589 
590 			/* type */
591 			ifi->ifi_type = ifm->ifm_type;
592 		} else {
593 			syslog(LOG_ERR,
594 			    "out of sync parsing NET_RT_IFLIST\n"
595 			    "expected %d, got %d\n msglen = %d\n",
596 			    RTM_IFINFO, ifm->ifm_type, ifm->ifm_msglen);
597 			exit(1);
598 		}
599 
600 		if (ifi_new) {
601 			syslog(LOG_DEBUG,
602 			    "<%s> adding %s(idx=%d) to ifilist",
603 			    __func__, ifi->ifi_ifname, ifi->ifi_ifindex);
604 			TAILQ_INSERT_TAIL(ifi_head, ifi, ifi_next);
605 		}
606 	}
607 	free(msg);
608 
609 	if (mcastif != NULL) {
610 		error = sock_mc_rr_update(&sock, mcastif);
611 		if (error)
612 			exit(1);
613 	}
614 
615 	return (ifi);
616 }
617 
618 static struct if_msghdr *
619 get_next_msghdr(struct if_msghdr *ifm, struct if_msghdr *lim)
620 {
621 	struct ifa_msghdr *ifam;
622 
623 	for (ifam = (struct ifa_msghdr *)((char *)ifm + ifm->ifm_msglen);
624 	     ifam < (struct ifa_msghdr *)lim;
625 	     ifam = (struct ifa_msghdr *)((char *)ifam + ifam->ifam_msglen)) {
626 		if (!ifam->ifam_msglen) {
627 			syslog(LOG_WARNING,
628 			    "<%s> ifa_msglen is 0", __func__);
629 			return (NULL);
630 		}
631 		if (ifam->ifam_type != RTM_NEWADDR)
632 			break;
633 	}
634 
635 	return ((struct if_msghdr *)ifam);
636 }
637 
638 int
639 getinet6sysctl(int code)
640 {
641 	int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, 0 };
642 	int value;
643 	size_t size;
644 
645 	mib[3] = code;
646 	size = sizeof(value);
647 	if (sysctl(mib, sizeof(mib)/sizeof(mib[0]), &value, &size, NULL, 0)
648 	    < 0) {
649 		syslog(LOG_ERR, "<%s>: failed to get ip6 sysctl(%d): %s",
650 		    __func__, code,
651 		    strerror(errno));
652 		return (-1);
653 	}
654 	else
655 		return (value);
656 }
657 
658 
659 int
660 sock_mc_join(struct sockinfo *s, int ifindex)
661 {
662 	struct ipv6_mreq mreq;
663 	char ifname[IFNAMSIZ];
664 
665 	syslog(LOG_DEBUG, "<%s> enter", __func__);
666 
667 	if (ifindex == 0)
668 		return (1);
669 
670 	/*
671 	 * join all routers multicast address on each advertising
672 	 * interface.
673 	 */
674 	memset(&mreq, 0, sizeof(mreq));
675 	/* XXX */
676 	memcpy(&mreq.ipv6mr_multiaddr.s6_addr,
677 	    &sin6_linklocal_allrouters.sin6_addr,
678 	    sizeof(mreq.ipv6mr_multiaddr.s6_addr));
679 
680 	mreq.ipv6mr_interface = ifindex;
681 	if (setsockopt(s->si_fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq,
682 		sizeof(mreq)) < 0) {
683 		syslog(LOG_ERR,
684 		    "<%s> IPV6_JOIN_GROUP(link) on %s: %s",
685 		    __func__, if_indextoname(ifindex, ifname),
686 		    strerror(errno));
687 		return (1);
688 	}
689 	syslog(LOG_DEBUG,
690 	    "<%s> %s: join link-local all-routers MC group",
691 	    __func__, if_indextoname(ifindex, ifname));
692 
693 	return (0);
694 }
695 
696 int
697 sock_mc_leave(struct sockinfo *s, int ifindex)
698 {
699 	struct ipv6_mreq mreq;
700 	char ifname[IFNAMSIZ];
701 
702 	syslog(LOG_DEBUG, "<%s> enter", __func__);
703 
704 	if (ifindex == 0)
705 		return (1);
706 
707 	/*
708 	 * join all routers multicast address on each advertising
709 	 * interface.
710 	 */
711 
712 	memset(&mreq, 0, sizeof(mreq));
713 	/* XXX */
714 	memcpy(&mreq.ipv6mr_multiaddr.s6_addr,
715 	    &sin6_linklocal_allrouters.sin6_addr,
716 	    sizeof(mreq.ipv6mr_multiaddr.s6_addr));
717 
718 	mreq.ipv6mr_interface = ifindex;
719 	if (setsockopt(s->si_fd, IPPROTO_IPV6, IPV6_LEAVE_GROUP, &mreq,
720 		sizeof(mreq)) < 0) {
721 		syslog(LOG_ERR,
722 		    "<%s> IPV6_JOIN_LEAVE(link) on %s: %s",
723 		    __func__, if_indextoname(ifindex, ifname),
724 		    strerror(errno));
725 		return (1);
726 	}
727 	syslog(LOG_DEBUG,
728 	    "<%s> %s: leave link-local all-routers MC group",
729 	    __func__, if_indextoname(ifindex, ifname));
730 
731 	return (0);
732 }
733 
734 int
735 sock_mc_rr_update(struct sockinfo *s, char *mif)
736 {
737 	struct ipv6_mreq mreq;
738 
739 	syslog(LOG_DEBUG, "<%s> enter", __func__);
740 
741 	if (mif == NULL)
742 		return (1);
743 	/*
744 	 * When attending router renumbering, join all-routers site-local
745 	 * multicast group.
746 	 */
747 	/* XXX */
748 	memcpy(&mreq.ipv6mr_multiaddr.s6_addr,
749 	    &sin6_sitelocal_allrouters.sin6_addr,
750 	    sizeof(mreq.ipv6mr_multiaddr.s6_addr));
751 	if ((mreq.ipv6mr_interface = if_nametoindex(mif)) == 0) {
752 		syslog(LOG_ERR,
753 		    "<%s> invalid interface: %s",
754 		    __func__, mif);
755 		return (1);
756 	}
757 
758 	if (setsockopt(s->si_fd, IPPROTO_IPV6, IPV6_JOIN_GROUP,
759 		&mreq, sizeof(mreq)) < 0) {
760 		syslog(LOG_ERR,
761 		    "<%s> IPV6_JOIN_GROUP(site) on %s: %s",
762 		    __func__, mif, strerror(errno));
763 		return (1);
764 	}
765 
766 	syslog(LOG_DEBUG,
767 	    "<%s> %s: join site-local all-routers MC group",
768 	    __func__, mif);
769 
770 	return (0);
771 }
772