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