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 #define MAX_SYSCTL_TRY 5 412 413 struct ifinfo * 414 update_ifinfo(struct ifilist_head_t *ifi_head, int ifindex) 415 { 416 struct if_msghdr *ifm; 417 struct ifinfo *ifi = NULL; 418 struct sockaddr *sa; 419 struct sockaddr *rti_info[RTAX_MAX]; 420 char *msg; 421 size_t len; 422 char *lim; 423 int mib[] = { CTL_NET, PF_ROUTE, 0, AF_INET6, NET_RT_IFLIST, 0 }; 424 int error, ntry; 425 426 syslog(LOG_DEBUG, "<%s> enter", __func__); 427 428 ntry = 0; 429 do { 430 /* 431 * We'll try to get addresses several times in case that 432 * the number of addresses is unexpectedly increased during 433 * the two sysctl calls. This should rarely happen. 434 * Portability note: since FreeBSD does not add margin of 435 * memory at the first sysctl, the possibility of failure on 436 * the second sysctl call is a bit higher. 437 */ 438 439 if (sysctl(mib, nitems(mib), NULL, &len, NULL, 0) < 0) { 440 syslog(LOG_ERR, 441 "<%s> sysctl: NET_RT_IFLIST size get failed", 442 __func__); 443 exit(1); 444 } 445 if ((msg = malloc(len)) == NULL) { 446 syslog(LOG_ERR, "<%s> malloc failed", __func__); 447 exit(1); 448 } 449 if (sysctl(mib, nitems(mib), msg, &len, NULL, 0) < 0) { 450 if (errno != ENOMEM || ++ntry >= MAX_SYSCTL_TRY) { 451 free(msg); 452 syslog(LOG_ERR, 453 "<%s> sysctl: NET_RT_IFLIST get failed", 454 __func__); 455 exit(1); 456 } 457 free(msg); 458 msg = NULL; 459 } 460 } while (msg == NULL); 461 462 lim = msg + len; 463 for (ifm = (struct if_msghdr *)msg; 464 ifm != NULL && ifm < (struct if_msghdr *)lim; 465 ifm = get_next_msghdr(ifm,(struct if_msghdr *)lim)) { 466 int ifi_new; 467 468 syslog(LOG_DEBUG, "<%s> ifm = %p, lim = %p, diff = %zu", 469 __func__, ifm, lim, (char *)lim - (char *)ifm); 470 471 if (ifm->ifm_version != RTM_VERSION) { 472 syslog(LOG_ERR, 473 "<%s> ifm_vesrion mismatch", __func__); 474 exit(1); 475 } 476 if (ifm->ifm_msglen == 0) { 477 syslog(LOG_WARNING, 478 "<%s> ifm_msglen is 0", __func__); 479 free(msg); 480 return (NULL); 481 } 482 483 ifi_new = 0; 484 if (ifm->ifm_type == RTM_IFINFO) { 485 struct ifreq ifr; 486 int s; 487 char ifname[IFNAMSIZ]; 488 489 syslog(LOG_DEBUG, "<%s> RTM_IFINFO found. " 490 "ifm_index = %d, ifindex = %d", 491 __func__, ifm->ifm_index, ifindex); 492 493 /* when ifindex is specified */ 494 if (ifindex != UPDATE_IFINFO_ALL && 495 ifindex != ifm->ifm_index) 496 continue; 497 498 /* ifname */ 499 if (if_indextoname(ifm->ifm_index, ifname) == NULL) { 500 syslog(LOG_WARNING, 501 "<%s> ifname not found (idx=%d)", 502 __func__, ifm->ifm_index); 503 continue; 504 } 505 506 /* lookup an entry with the same ifindex */ 507 TAILQ_FOREACH(ifi, ifi_head, ifi_next) { 508 if (ifm->ifm_index == ifi->ifi_ifindex) 509 break; 510 if (strncmp(ifname, ifi->ifi_ifname, 511 sizeof(ifname)) == 0) 512 break; 513 } 514 if (ifi == NULL) { 515 syslog(LOG_DEBUG, 516 "<%s> new entry for idx=%d", 517 __func__, ifm->ifm_index); 518 ELM_MALLOC(ifi, exit(1)); 519 ifi->ifi_rainfo = NULL; 520 ifi->ifi_state = IFI_STATE_UNCONFIGURED; 521 ifi->ifi_persist = 0; 522 ifi_new = 1; 523 } 524 /* ifindex */ 525 ifi->ifi_ifindex = ifm->ifm_index; 526 527 /* ifname */ 528 strlcpy(ifi->ifi_ifname, ifname, IFNAMSIZ); 529 530 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { 531 syslog(LOG_ERR, 532 "<%s> socket() failed.", __func__); 533 if (ifi_new) 534 free(ifi); 535 continue; 536 } 537 538 /* MTU */ 539 ifi->ifi_phymtu = ifm->ifm_data.ifi_mtu; 540 if (ifi->ifi_phymtu == 0) { 541 memset(&ifr, 0, sizeof(ifr)); 542 ifr.ifr_addr.sa_family = AF_INET6; 543 strlcpy(ifr.ifr_name, ifi->ifi_ifname, 544 sizeof(ifr.ifr_name)); 545 error = ioctl(s, SIOCGIFMTU, (caddr_t)&ifr); 546 if (error) { 547 close(s); 548 syslog(LOG_ERR, 549 "<%s> ioctl() failed.", 550 __func__); 551 if (ifi_new) 552 free(ifi); 553 continue; 554 } 555 ifi->ifi_phymtu = ifr.ifr_mtu; 556 if (ifi->ifi_phymtu == 0) { 557 syslog(LOG_WARNING, 558 "<%s> no interface mtu info" 559 " on %s. %d will be used.", 560 __func__, ifi->ifi_ifname, 561 IPV6_MMTU); 562 ifi->ifi_phymtu = IPV6_MMTU; 563 } 564 } 565 close(s); 566 567 /* ND flags */ 568 error = update_ifinfo_nd_flags(ifi); 569 if (error) { 570 if (ifi_new) 571 free(ifi); 572 continue; 573 } 574 575 /* SDL */ 576 sa = (struct sockaddr *)(ifm + 1); 577 get_rtaddrs(ifm->ifm_addrs, sa, rti_info); 578 if ((sa = rti_info[RTAX_IFP]) != NULL) { 579 if (sa->sa_family == AF_LINK) { 580 memcpy(&ifi->ifi_sdl, 581 (struct sockaddr_dl *)sa, 582 sizeof(ifi->ifi_sdl)); 583 } 584 } else 585 memset(&ifi->ifi_sdl, 0, 586 sizeof(ifi->ifi_sdl)); 587 588 /* flags */ 589 ifi->ifi_flags = ifm->ifm_flags; 590 591 /* type */ 592 ifi->ifi_type = ifm->ifm_type; 593 } else { 594 syslog(LOG_ERR, 595 "out of sync parsing NET_RT_IFLIST\n" 596 "expected %d, got %d\n msglen = %d\n", 597 RTM_IFINFO, ifm->ifm_type, ifm->ifm_msglen); 598 exit(1); 599 } 600 601 if (ifi_new) { 602 syslog(LOG_DEBUG, 603 "<%s> adding %s(idx=%d) to ifilist", 604 __func__, ifi->ifi_ifname, ifi->ifi_ifindex); 605 TAILQ_INSERT_TAIL(ifi_head, ifi, ifi_next); 606 } 607 } 608 free(msg); 609 610 if (mcastif != NULL) { 611 error = sock_mc_rr_update(&sock, mcastif); 612 if (error) 613 exit(1); 614 } 615 616 return (ifi); 617 } 618 619 static struct if_msghdr * 620 get_next_msghdr(struct if_msghdr *ifm, struct if_msghdr *lim) 621 { 622 struct ifa_msghdr *ifam; 623 624 for (ifam = (struct ifa_msghdr *)((char *)ifm + ifm->ifm_msglen); 625 ifam < (struct ifa_msghdr *)lim; 626 ifam = (struct ifa_msghdr *)((char *)ifam + ifam->ifam_msglen)) { 627 if (!ifam->ifam_msglen) { 628 syslog(LOG_WARNING, 629 "<%s> ifa_msglen is 0", __func__); 630 return (NULL); 631 } 632 if (ifam->ifam_type != RTM_NEWADDR) 633 break; 634 } 635 636 return ((struct if_msghdr *)ifam); 637 } 638 639 int 640 getinet6sysctl(int code) 641 { 642 int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, 0 }; 643 int value; 644 size_t size; 645 646 mib[3] = code; 647 size = sizeof(value); 648 if (sysctl(mib, sizeof(mib)/sizeof(mib[0]), &value, &size, NULL, 0) 649 < 0) { 650 syslog(LOG_ERR, "<%s>: failed to get ip6 sysctl(%d): %s", 651 __func__, code, 652 strerror(errno)); 653 return (-1); 654 } 655 else 656 return (value); 657 } 658 659 660 int 661 sock_mc_join(struct sockinfo *s, int ifindex) 662 { 663 struct ipv6_mreq mreq; 664 char ifname[IFNAMSIZ]; 665 666 syslog(LOG_DEBUG, "<%s> enter", __func__); 667 668 if (ifindex == 0) 669 return (1); 670 671 /* 672 * join all routers multicast address on each advertising 673 * interface. 674 */ 675 memset(&mreq, 0, sizeof(mreq)); 676 /* XXX */ 677 memcpy(&mreq.ipv6mr_multiaddr.s6_addr, 678 &sin6_linklocal_allrouters.sin6_addr, 679 sizeof(mreq.ipv6mr_multiaddr.s6_addr)); 680 681 mreq.ipv6mr_interface = ifindex; 682 if (setsockopt(s->si_fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq, 683 sizeof(mreq)) < 0) { 684 syslog(LOG_ERR, 685 "<%s> IPV6_JOIN_GROUP(link) on %s: %s", 686 __func__, if_indextoname(ifindex, ifname), 687 strerror(errno)); 688 return (1); 689 } 690 syslog(LOG_DEBUG, 691 "<%s> %s: join link-local all-routers MC group", 692 __func__, if_indextoname(ifindex, ifname)); 693 694 return (0); 695 } 696 697 int 698 sock_mc_leave(struct sockinfo *s, int ifindex) 699 { 700 struct ipv6_mreq mreq; 701 char ifname[IFNAMSIZ]; 702 703 syslog(LOG_DEBUG, "<%s> enter", __func__); 704 705 if (ifindex == 0) 706 return (1); 707 708 /* 709 * join all routers multicast address on each advertising 710 * interface. 711 */ 712 713 memset(&mreq, 0, sizeof(mreq)); 714 /* XXX */ 715 memcpy(&mreq.ipv6mr_multiaddr.s6_addr, 716 &sin6_linklocal_allrouters.sin6_addr, 717 sizeof(mreq.ipv6mr_multiaddr.s6_addr)); 718 719 mreq.ipv6mr_interface = ifindex; 720 if (setsockopt(s->si_fd, IPPROTO_IPV6, IPV6_LEAVE_GROUP, &mreq, 721 sizeof(mreq)) < 0) { 722 syslog(LOG_ERR, 723 "<%s> IPV6_JOIN_LEAVE(link) on %s: %s", 724 __func__, if_indextoname(ifindex, ifname), 725 strerror(errno)); 726 return (1); 727 } 728 syslog(LOG_DEBUG, 729 "<%s> %s: leave link-local all-routers MC group", 730 __func__, if_indextoname(ifindex, ifname)); 731 732 return (0); 733 } 734 735 int 736 sock_mc_rr_update(struct sockinfo *s, char *mif) 737 { 738 struct ipv6_mreq mreq; 739 740 syslog(LOG_DEBUG, "<%s> enter", __func__); 741 742 if (mif == NULL) 743 return (1); 744 /* 745 * When attending router renumbering, join all-routers site-local 746 * multicast group. 747 */ 748 /* XXX */ 749 memcpy(&mreq.ipv6mr_multiaddr.s6_addr, 750 &sin6_sitelocal_allrouters.sin6_addr, 751 sizeof(mreq.ipv6mr_multiaddr.s6_addr)); 752 if ((mreq.ipv6mr_interface = if_nametoindex(mif)) == 0) { 753 syslog(LOG_ERR, 754 "<%s> invalid interface: %s", 755 __func__, mif); 756 return (1); 757 } 758 759 if (setsockopt(s->si_fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, 760 &mreq, sizeof(mreq)) < 0) { 761 syslog(LOG_ERR, 762 "<%s> IPV6_JOIN_GROUP(site) on %s: %s", 763 __func__, mif, strerror(errno)); 764 return (1); 765 } 766 767 syslog(LOG_DEBUG, 768 "<%s> %s: join site-local all-routers MC group", 769 __func__, mif); 770 771 return (0); 772 } 773