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