1 /* $NetBSD: in6.c,v 1.52 2001/12/21 08:54:53 itojun Exp $ */ 2 /* $KAME: in6.c,v 1.198 2001/07/18 09:12:38 itojun Exp $ */ 3 4 /* 5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the project nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 /* 34 * Copyright (c) 1982, 1986, 1991, 1993 35 * The Regents of the University of California. All rights reserved. 36 * 37 * Redistribution and use in source and binary forms, with or without 38 * modification, are permitted provided that the following conditions 39 * are met: 40 * 1. Redistributions of source code must retain the above copyright 41 * notice, this list of conditions and the following disclaimer. 42 * 2. Redistributions in binary form must reproduce the above copyright 43 * notice, this list of conditions and the following disclaimer in the 44 * documentation and/or other materials provided with the distribution. 45 * 3. All advertising materials mentioning features or use of this software 46 * must display the following acknowledgement: 47 * This product includes software developed by the University of 48 * California, Berkeley and its contributors. 49 * 4. Neither the name of the University nor the names of its contributors 50 * may be used to endorse or promote products derived from this software 51 * without specific prior written permission. 52 * 53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 63 * SUCH DAMAGE. 64 * 65 * @(#)in.c 8.2 (Berkeley) 11/15/93 66 */ 67 68 #include <sys/cdefs.h> 69 __KERNEL_RCSID(0, "$NetBSD: in6.c,v 1.52 2001/12/21 08:54:53 itojun Exp $"); 70 71 #include "opt_inet.h" 72 73 #include <sys/param.h> 74 #include <sys/ioctl.h> 75 #include <sys/errno.h> 76 #include <sys/malloc.h> 77 #include <sys/socket.h> 78 #include <sys/socketvar.h> 79 #include <sys/sockio.h> 80 #include <sys/systm.h> 81 #include <sys/proc.h> 82 #include <sys/time.h> 83 #include <sys/kernel.h> 84 #include <sys/syslog.h> 85 86 #include <net/if.h> 87 #include <net/if_types.h> 88 #include <net/route.h> 89 #include <net/if_dl.h> 90 91 #include <netinet/in.h> 92 #include <netinet/in_var.h> 93 #include <net/if_ether.h> 94 95 #include <netinet/ip6.h> 96 #include <netinet6/ip6_var.h> 97 #include <netinet6/nd6.h> 98 #include <netinet6/mld6_var.h> 99 #include <netinet6/ip6_mroute.h> 100 #include <netinet6/in6_ifattach.h> 101 102 #include <net/net_osdep.h> 103 104 /* enable backward compatibility code for obsoleted ioctls */ 105 #define COMPAT_IN6IFIOCTL 106 107 /* 108 * Definitions of some costant IP6 addresses. 109 */ 110 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT; 111 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT; 112 const struct in6_addr in6addr_nodelocal_allnodes = 113 IN6ADDR_NODELOCAL_ALLNODES_INIT; 114 const struct in6_addr in6addr_linklocal_allnodes = 115 IN6ADDR_LINKLOCAL_ALLNODES_INIT; 116 const struct in6_addr in6addr_linklocal_allrouters = 117 IN6ADDR_LINKLOCAL_ALLROUTERS_INIT; 118 119 const struct in6_addr in6mask0 = IN6MASK0; 120 const struct in6_addr in6mask32 = IN6MASK32; 121 const struct in6_addr in6mask64 = IN6MASK64; 122 const struct in6_addr in6mask96 = IN6MASK96; 123 const struct in6_addr in6mask128 = IN6MASK128; 124 125 const struct sockaddr_in6 sa6_any = {sizeof(sa6_any), AF_INET6, 126 0, 0, IN6ADDR_ANY_INIT, 0}; 127 128 static int in6_lifaddr_ioctl __P((struct socket *, u_long, caddr_t, 129 struct ifnet *, struct proc *)); 130 static int in6_ifinit __P((struct ifnet *, struct in6_ifaddr *, 131 struct sockaddr_in6 *, int)); 132 133 /* 134 * This structure is used to keep track of in6_multi chains which belong to 135 * deleted interface addresses. 136 */ 137 static LIST_HEAD(, multi6_kludge) in6_mk; /* XXX BSS initialization */ 138 139 struct multi6_kludge { 140 LIST_ENTRY(multi6_kludge) mk_entry; 141 struct ifnet *mk_ifp; 142 struct in6_multihead mk_head; 143 }; 144 145 /* 146 * Check if the loopback entry will be automatically generated. 147 * if 0 returned, will not be automatically generated. 148 * if 1 returned, will be automatically generated. 149 */ 150 static int 151 in6_is_ifloop_auto(struct ifaddr *ifa) 152 { 153 #define SIN6(s) ((struct sockaddr_in6 *)s) 154 /* 155 * If RTF_CLONING is unset, or (IFF_LOOPBACK | IFF_POINTOPOINT), 156 * or netmask is all0 or all1, then cloning will not happen, 157 * then we can't rely on its loopback entry generation. 158 */ 159 if ((ifa->ifa_flags & RTF_CLONING) == 0 || 160 (ifa->ifa_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) || 161 (SIN6(ifa->ifa_netmask)->sin6_len == sizeof(struct sockaddr_in6) 162 && 163 IN6_ARE_ADDR_EQUAL(&SIN6(ifa->ifa_netmask)->sin6_addr, 164 &in6mask128)) || 165 ((struct sockaddr_in6 *)ifa->ifa_netmask)->sin6_len == 0) 166 return 0; 167 else 168 return 1; 169 #undef SIN6 170 } 171 172 /* 173 * Subroutine for in6_ifaddloop() and in6_ifremloop(). 174 * This routine does actual work. 175 */ 176 static void 177 in6_ifloop_request(int cmd, struct ifaddr *ifa) 178 { 179 struct sockaddr_in6 lo_sa; 180 struct sockaddr_in6 all1_sa; 181 struct rtentry *nrt = NULL, **nrtp = NULL; 182 183 bzero(&lo_sa, sizeof(lo_sa)); 184 bzero(&all1_sa, sizeof(all1_sa)); 185 lo_sa.sin6_family = AF_INET6; 186 lo_sa.sin6_len = sizeof(struct sockaddr_in6); 187 all1_sa = lo_sa; 188 lo_sa.sin6_addr = in6addr_loopback; 189 all1_sa.sin6_addr = in6mask128; 190 191 /* 192 * So we add or remove static loopback entry, here. 193 * This request for deletion could fail, e.g. when we remove 194 * an address right after adding it. 195 */ 196 if (cmd == RTM_ADD) 197 nrtp = &nrt; 198 rtrequest(cmd, ifa->ifa_addr, 199 (struct sockaddr *)&lo_sa, 200 (struct sockaddr *)&all1_sa, 201 RTF_UP|RTF_HOST, nrtp); 202 203 /* 204 * Make sure rt_ifa be equal to IFA, the second argument of the 205 * function. 206 * We need this because when we refer to rt_ifa->ia6_flags in 207 * ip6_input, we assume that the rt_ifa points to the address instead 208 * of the loopback address. 209 */ 210 if (cmd == RTM_ADD && nrt && ifa != nrt->rt_ifa) { 211 IFAFREE(nrt->rt_ifa); 212 IFAREF(ifa); 213 nrt->rt_ifa = ifa; 214 } 215 if (nrt) 216 nrt->rt_refcnt--; 217 } 218 219 /* 220 * Add ownaddr as loopback rtentry, if necessary(ex. on p2p link). 221 * Because, KAME needs loopback rtentry for ownaddr check in 222 * ip6_input(). 223 */ 224 static void 225 in6_ifaddloop(struct ifaddr *ifa) 226 { 227 if (!in6_is_ifloop_auto(ifa)) { 228 struct rtentry *rt; 229 230 /* If there is no loopback entry, allocate one. */ 231 rt = rtalloc1(ifa->ifa_addr, 0); 232 if (rt == 0 || (rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) 233 in6_ifloop_request(RTM_ADD, ifa); 234 if (rt) 235 rt->rt_refcnt--; 236 } 237 } 238 239 /* 240 * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(), 241 * if it exists. 242 */ 243 static void 244 in6_ifremloop(struct ifaddr *ifa) 245 { 246 if (!in6_is_ifloop_auto(ifa)) { 247 struct in6_ifaddr *ia; 248 int ia_count = 0; 249 250 /* If only one ifa for the loopback entry, delete it. */ 251 for (ia = in6_ifaddr; ia; ia = ia->ia_next) { 252 if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), 253 &ia->ia_addr.sin6_addr)) { 254 ia_count++; 255 if (ia_count > 1) 256 break; 257 } 258 } 259 if (ia_count == 1) 260 in6_ifloop_request(RTM_DELETE, ifa); 261 } 262 } 263 264 int 265 in6_ifindex2scopeid(idx) 266 int idx; 267 { 268 struct ifnet *ifp; 269 struct ifaddr *ifa; 270 struct sockaddr_in6 *sin6; 271 272 if (idx < 0 || if_index < idx) 273 return -1; 274 ifp = ifindex2ifnet[idx]; 275 if (!ifp) 276 return -1; 277 278 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next) 279 { 280 if (ifa->ifa_addr->sa_family != AF_INET6) 281 continue; 282 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr; 283 if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) 284 return sin6->sin6_scope_id & 0xffff; 285 } 286 287 return -1; 288 } 289 290 int 291 in6_mask2len(mask) 292 struct in6_addr *mask; 293 { 294 int x, y; 295 296 for (x = 0; x < sizeof(*mask); x++) { 297 if (mask->s6_addr8[x] != 0xff) 298 break; 299 } 300 y = 0; 301 if (x < sizeof(*mask)) { 302 for (y = 0; y < 8; y++) { 303 if ((mask->s6_addr8[x] & (0x80 >> y)) == 0) 304 break; 305 } 306 } 307 return x * 8 + y; 308 } 309 310 void 311 in6_len2mask(mask, len) 312 struct in6_addr *mask; 313 int len; 314 { 315 int i; 316 317 bzero(mask, sizeof(*mask)); 318 for (i = 0; i < len / 8; i++) 319 mask->s6_addr8[i] = 0xff; 320 if (len % 8) 321 mask->s6_addr8[i] = (0xff00 >> (len % 8)) & 0xff; 322 } 323 324 #define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa)) 325 #define ia62ifa(ia6) (&((ia6)->ia_ifa)) 326 327 int 328 in6_control(so, cmd, data, ifp, p) 329 struct socket *so; 330 u_long cmd; 331 caddr_t data; 332 struct ifnet *ifp; 333 struct proc *p; 334 { 335 struct in6_ifreq *ifr = (struct in6_ifreq *)data; 336 struct in6_ifaddr *ia, *oia; 337 struct in6_aliasreq *ifra = (struct in6_aliasreq *)data; 338 struct sockaddr_in6 oldaddr; 339 #ifdef COMPAT_IN6IFIOCTL 340 struct sockaddr_in6 net; 341 #endif 342 int error = 0, hostIsNew, prefixIsNew; 343 int newifaddr; 344 time_t time_second = (time_t)time.tv_sec; 345 int privileged; 346 347 privileged = 0; 348 if (p && !suser(p->p_ucred, &p->p_acflag)) 349 privileged++; 350 351 switch (cmd) { 352 case SIOCGETSGCNT_IN6: 353 case SIOCGETMIFCNT_IN6: 354 return (mrt6_ioctl(cmd, data)); 355 } 356 357 if (ifp == NULL) 358 return(EOPNOTSUPP); 359 360 switch (cmd) { 361 case SIOCSNDFLUSH_IN6: 362 case SIOCSPFXFLUSH_IN6: 363 case SIOCSRTRFLUSH_IN6: 364 case SIOCSDEFIFACE_IN6: 365 case SIOCSIFINFO_FLAGS: 366 if (!privileged) 367 return(EPERM); 368 /* fall through */ 369 case SIOCGIFINFO_IN6: 370 case SIOCGDRLST_IN6: 371 case SIOCGPRLST_IN6: 372 case SIOCGNBRINFO_IN6: 373 case SIOCGDEFIFACE_IN6: 374 return(nd6_ioctl(cmd, data, ifp)); 375 } 376 377 switch (cmd) { 378 case SIOCSIFPREFIX_IN6: 379 case SIOCDIFPREFIX_IN6: 380 case SIOCAIFPREFIX_IN6: 381 case SIOCCIFPREFIX_IN6: 382 case SIOCSGIFPREFIX_IN6: 383 if (!privileged) 384 return(EPERM); 385 /* fall through */ 386 case SIOCGIFPREFIX_IN6: 387 return(in6_prefix_ioctl(so, cmd, data, ifp)); 388 } 389 390 switch (cmd) { 391 case SIOCALIFADDR: 392 case SIOCDLIFADDR: 393 if (!privileged) 394 return(EPERM); 395 /* fall through */ 396 case SIOCGLIFADDR: 397 return in6_lifaddr_ioctl(so, cmd, data, ifp, p); 398 } 399 400 /* 401 * Find address for this interface, if it exists. 402 */ 403 if (ifra->ifra_addr.sin6_family == AF_INET6) { /* XXX */ 404 struct sockaddr_in6 *sa6 = 405 (struct sockaddr_in6 *)&ifra->ifra_addr; 406 407 if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr)) { 408 if (sa6->sin6_addr.s6_addr16[1] == 0) { 409 /* interface ID is not embedded by the user */ 410 sa6->sin6_addr.s6_addr16[1] = 411 htons(ifp->if_index); 412 } else if (sa6->sin6_addr.s6_addr16[1] != 413 htons(ifp->if_index)) { 414 return(EINVAL); /* link ID contradicts */ 415 } 416 if (sa6->sin6_scope_id) { 417 if (sa6->sin6_scope_id != 418 (u_int32_t)ifp->if_index) 419 return(EINVAL); 420 sa6->sin6_scope_id = 0; /* XXX: good way? */ 421 } 422 } 423 ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr); 424 } else 425 ia = NULL; 426 427 switch (cmd) { 428 429 case SIOCDIFADDR_IN6: 430 /* 431 * for IPv4, we look for existing in_ifaddr here to allow 432 * "ifconfig if0 delete" to remove the first IPv4 address on 433 * the interface. For IPv6, as the spec allows multiple 434 * interface address from the day one, we consider "remove the 435 * first one" semantics to be not preferable. 436 */ 437 if (ia == NULL) 438 return(EADDRNOTAVAIL); 439 /* FALLTHROUGH */ 440 case SIOCAIFADDR_IN6: 441 case SIOCSIFADDR_IN6: 442 #ifdef COMPAT_IN6IFIOCTL 443 case SIOCSIFDSTADDR_IN6: 444 case SIOCSIFNETMASK_IN6: 445 /* 446 * Since IPv6 allows a node to assign multiple addresses 447 * on a single interface, SIOCSIFxxx ioctls are not suitable 448 * and should be unused. 449 */ 450 #endif 451 if (ifra->ifra_addr.sin6_family != AF_INET6) 452 return(EAFNOSUPPORT); 453 if (!privileged) 454 return(EPERM); 455 if (ia == NULL) { 456 ia = (struct in6_ifaddr *) 457 malloc(sizeof(*ia), M_IFADDR, M_WAITOK); 458 if (ia == NULL) 459 return (ENOBUFS); 460 bzero((caddr_t)ia, sizeof(*ia)); 461 /* Initialize the address and masks */ 462 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr; 463 ia->ia_addr.sin6_family = AF_INET6; 464 ia->ia_addr.sin6_len = sizeof(ia->ia_addr); 465 if (ifp->if_flags & IFF_POINTOPOINT) { 466 ia->ia_ifa.ifa_dstaddr 467 = (struct sockaddr *)&ia->ia_dstaddr; 468 ia->ia_dstaddr.sin6_family = AF_INET6; 469 ia->ia_dstaddr.sin6_len = sizeof(ia->ia_dstaddr); 470 } else { 471 ia->ia_ifa.ifa_dstaddr = NULL; 472 bzero(&ia->ia_dstaddr, sizeof(ia->ia_dstaddr)); 473 } 474 ia->ia_ifa.ifa_netmask 475 = (struct sockaddr *)&ia->ia_prefixmask; 476 477 ia->ia_ifp = ifp; 478 if ((oia = in6_ifaddr) != NULL) { 479 for ( ; oia->ia_next; oia = oia->ia_next) 480 continue; 481 oia->ia_next = ia; 482 } else 483 in6_ifaddr = ia; 484 IFAREF(&ia->ia_ifa); 485 486 TAILQ_INSERT_TAIL(&ifp->if_addrlist, &ia->ia_ifa, 487 ifa_list); 488 IFAREF(&ia->ia_ifa); 489 490 newifaddr = 1; 491 } else 492 newifaddr = 0; 493 494 if (cmd == SIOCAIFADDR_IN6) { 495 /* sanity for overflow - beware unsigned */ 496 struct in6_addrlifetime *lt; 497 lt = &ifra->ifra_lifetime; 498 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME 499 && lt->ia6t_vltime + time_second < time_second) { 500 return EINVAL; 501 } 502 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME 503 && lt->ia6t_pltime + time_second < time_second) { 504 return EINVAL; 505 } 506 } 507 break; 508 509 case SIOCGIFADDR_IN6: 510 /* This interface is basically deprecated. use SIOCGIFCONF. */ 511 /* fall through */ 512 case SIOCGIFAFLAG_IN6: 513 case SIOCGIFNETMASK_IN6: 514 case SIOCGIFDSTADDR_IN6: 515 case SIOCGIFALIFETIME_IN6: 516 /* must think again about its semantics */ 517 if (ia == NULL) 518 return(EADDRNOTAVAIL); 519 break; 520 case SIOCSIFALIFETIME_IN6: 521 { 522 struct in6_addrlifetime *lt; 523 524 if (!privileged) 525 return(EPERM); 526 if (ia == NULL) 527 return(EADDRNOTAVAIL); 528 /* sanity for overflow - beware unsigned */ 529 lt = &ifr->ifr_ifru.ifru_lifetime; 530 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME 531 && lt->ia6t_vltime + time_second < time_second) { 532 return EINVAL; 533 } 534 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME 535 && lt->ia6t_pltime + time_second < time_second) { 536 return EINVAL; 537 } 538 break; 539 } 540 } 541 542 switch (cmd) { 543 544 case SIOCGIFADDR_IN6: 545 ifr->ifr_addr = ia->ia_addr; 546 break; 547 548 case SIOCGIFDSTADDR_IN6: 549 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 550 return(EINVAL); 551 /* 552 * XXX: should we check if ifa_dstaddr is NULL and return 553 * an error? 554 */ 555 ifr->ifr_dstaddr = ia->ia_dstaddr; 556 break; 557 558 case SIOCGIFNETMASK_IN6: 559 ifr->ifr_addr = ia->ia_prefixmask; 560 break; 561 562 case SIOCGIFAFLAG_IN6: 563 ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags; 564 break; 565 566 case SIOCGIFSTAT_IN6: 567 if (ifp == NULL) 568 return EINVAL; 569 if (in6_ifstat == NULL || ifp->if_index >= in6_ifstatmax 570 || in6_ifstat[ifp->if_index] == NULL) { 571 /* return EAFNOSUPPORT? */ 572 bzero(&ifr->ifr_ifru.ifru_stat, 573 sizeof(ifr->ifr_ifru.ifru_stat)); 574 } else 575 ifr->ifr_ifru.ifru_stat = *in6_ifstat[ifp->if_index]; 576 break; 577 578 case SIOCGIFSTAT_ICMP6: 579 if (ifp == NULL) 580 return EINVAL; 581 if (icmp6_ifstat == NULL || ifp->if_index >= icmp6_ifstatmax || 582 icmp6_ifstat[ifp->if_index] == NULL) { 583 /* return EAFNOSUPPORT? */ 584 bzero(&ifr->ifr_ifru.ifru_stat, 585 sizeof(ifr->ifr_ifru.ifru_icmp6stat)); 586 } else 587 ifr->ifr_ifru.ifru_icmp6stat = 588 *icmp6_ifstat[ifp->if_index]; 589 break; 590 591 #ifdef COMPAT_IN6IFIOCTL /* should be unused */ 592 case SIOCSIFDSTADDR_IN6: 593 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 594 return(EINVAL); 595 oldaddr = ia->ia_dstaddr; 596 ia->ia_dstaddr = ifr->ifr_dstaddr; 597 598 /* link-local index check */ 599 if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_dstaddr.sin6_addr)) { 600 if (ia->ia_dstaddr.sin6_addr.s6_addr16[1] == 0) { 601 /* interface ID is not embedded by the user */ 602 ia->ia_dstaddr.sin6_addr.s6_addr16[1] 603 = htons(ifp->if_index); 604 } else if (ia->ia_dstaddr.sin6_addr.s6_addr16[1] != 605 htons(ifp->if_index)) { 606 ia->ia_dstaddr = oldaddr; 607 return(EINVAL); /* ifid contradicts */ 608 } 609 } 610 611 if (ifp->if_ioctl && (error = (ifp->if_ioctl) 612 (ifp, SIOCSIFDSTADDR, (caddr_t)ia))) { 613 ia->ia_dstaddr = oldaddr; 614 return(error); 615 } 616 if (ia->ia_flags & IFA_ROUTE) { 617 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr; 618 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST); 619 ia->ia_ifa.ifa_dstaddr = 620 (struct sockaddr *)&ia->ia_dstaddr; 621 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP); 622 } 623 break; 624 625 #endif 626 case SIOCGIFALIFETIME_IN6: 627 ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime; 628 break; 629 630 case SIOCSIFALIFETIME_IN6: 631 ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime; 632 /* for sanity */ 633 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) { 634 ia->ia6_lifetime.ia6t_expire = 635 time_second + ia->ia6_lifetime.ia6t_vltime; 636 } else 637 ia->ia6_lifetime.ia6t_expire = 0; 638 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) { 639 ia->ia6_lifetime.ia6t_preferred = 640 time_second + ia->ia6_lifetime.ia6t_pltime; 641 } else 642 ia->ia6_lifetime.ia6t_preferred = 0; 643 break; 644 645 case SIOCSIFADDR_IN6: 646 error = in6_ifinit(ifp, ia, &ifr->ifr_addr, 1); 647 #if 0 648 /* 649 * the code chokes if we are to assign multiple addresses with 650 * the same address prefix (rtinit() will return EEXIST, which 651 * is not fatal actually). we will get memory leak if we 652 * don't do it. 653 * -> we may want to hide EEXIST from rtinit(). 654 */ 655 undo: 656 if (error && newifaddr) { 657 TAILQ_REMOVE(&ifp->if_addrlist, &ia->ia_ifa, ifa_list); 658 IFAFREE(&ia->ia_ifa); 659 660 oia = ia; 661 if (oia == (ia = in6_ifaddr)) 662 in6_ifaddr = ia->ia_next; 663 else { 664 while (ia->ia_next && (ia->ia_next != oia)) 665 ia = ia->ia_next; 666 if (ia->ia_next) 667 ia->ia_next = oia->ia_next; 668 else { 669 printf("Didn't unlink in6_ifaddr " 670 "from list\n"); 671 } 672 } 673 IFAFREE(&oia->ia_ifa); 674 } 675 #endif 676 return error; 677 678 #ifdef COMPAT_IN6IFIOCTL /* XXX should be unused */ 679 case SIOCSIFNETMASK_IN6: 680 ia->ia_prefixmask = ifr->ifr_addr; 681 bzero(&net, sizeof(net)); 682 net.sin6_len = sizeof(struct sockaddr_in6); 683 net.sin6_family = AF_INET6; 684 net.sin6_port = htons(0); 685 net.sin6_flowinfo = htonl(0); 686 net.sin6_addr.s6_addr32[0] 687 = ia->ia_addr.sin6_addr.s6_addr32[0] & 688 ia->ia_prefixmask.sin6_addr.s6_addr32[0]; 689 net.sin6_addr.s6_addr32[1] 690 = ia->ia_addr.sin6_addr.s6_addr32[1] & 691 ia->ia_prefixmask.sin6_addr.s6_addr32[1]; 692 net.sin6_addr.s6_addr32[2] 693 = ia->ia_addr.sin6_addr.s6_addr32[2] & 694 ia->ia_prefixmask.sin6_addr.s6_addr32[2]; 695 net.sin6_addr.s6_addr32[3] 696 = ia->ia_addr.sin6_addr.s6_addr32[3] & 697 ia->ia_prefixmask.sin6_addr.s6_addr32[3]; 698 ia->ia_net = net; 699 break; 700 #endif 701 702 case SIOCAIFADDR_IN6: 703 prefixIsNew = 0; 704 hostIsNew = 1; 705 706 if (ifra->ifra_addr.sin6_len == 0) { 707 ifra->ifra_addr = ia->ia_addr; 708 hostIsNew = 0; 709 } else if (IN6_ARE_ADDR_EQUAL(&ifra->ifra_addr.sin6_addr, 710 &ia->ia_addr.sin6_addr)) 711 hostIsNew = 0; 712 713 /* Validate address families: */ 714 /* 715 * The destination address for a p2p link must have a family 716 * of AF_UNSPEC or AF_INET6. 717 */ 718 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 && 719 ifra->ifra_dstaddr.sin6_family != AF_INET6 && 720 ifra->ifra_dstaddr.sin6_family != AF_UNSPEC) 721 return(EAFNOSUPPORT); 722 /* 723 * The prefixmask must have a family of AF_UNSPEC or AF_INET6. 724 */ 725 if (ifra->ifra_prefixmask.sin6_family != AF_INET6 && 726 ifra->ifra_prefixmask.sin6_family != AF_UNSPEC) 727 return(EAFNOSUPPORT); 728 729 if (ifra->ifra_prefixmask.sin6_len) { 730 in6_ifscrub(ifp, ia); 731 ia->ia_prefixmask = ifra->ifra_prefixmask; 732 prefixIsNew = 1; 733 } 734 if ((ifp->if_flags & IFF_POINTOPOINT) && 735 (ifra->ifra_dstaddr.sin6_family == AF_INET6)) { 736 in6_ifscrub(ifp, ia); 737 oldaddr = ia->ia_dstaddr; 738 ia->ia_dstaddr = ifra->ifra_dstaddr; 739 /* link-local index check: should be a separate function? */ 740 if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_dstaddr.sin6_addr)) { 741 if (ia->ia_dstaddr.sin6_addr.s6_addr16[1] == 0) { 742 /* 743 * interface ID is not embedded by 744 * the user 745 */ 746 ia->ia_dstaddr.sin6_addr.s6_addr16[1] 747 = htons(ifp->if_index); 748 } else if (ia->ia_dstaddr.sin6_addr.s6_addr16[1] != 749 htons(ifp->if_index)) { 750 ia->ia_dstaddr = oldaddr; 751 return(EINVAL); /* ifid contradicts */ 752 } 753 } 754 prefixIsNew = 1; /* We lie; but effect's the same */ 755 } 756 if (hostIsNew || prefixIsNew) { 757 error = in6_ifinit(ifp, ia, &ifra->ifra_addr, 0); 758 #if 0 759 if (error) 760 goto undo; 761 #endif 762 } 763 if (hostIsNew && (ifp->if_flags & IFF_MULTICAST)) { 764 int error_local = 0; 765 766 /* 767 * join solicited multicast addr for new host id 768 */ 769 struct in6_addr llsol; 770 bzero(&llsol, sizeof(struct in6_addr)); 771 llsol.s6_addr16[0] = htons(0xff02); 772 llsol.s6_addr16[1] = htons(ifp->if_index); 773 llsol.s6_addr32[1] = 0; 774 llsol.s6_addr32[2] = htonl(1); 775 llsol.s6_addr32[3] = 776 ifra->ifra_addr.sin6_addr.s6_addr32[3]; 777 llsol.s6_addr8[12] = 0xff; 778 (void)in6_addmulti(&llsol, ifp, &error_local); 779 if (error == 0) 780 error = error_local; 781 } 782 783 ia->ia6_flags = ifra->ifra_flags; 784 ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /*safety*/ 785 786 ia->ia6_lifetime = ifra->ifra_lifetime; 787 /* for sanity */ 788 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) { 789 ia->ia6_lifetime.ia6t_expire = 790 time_second + ia->ia6_lifetime.ia6t_vltime; 791 } else 792 ia->ia6_lifetime.ia6t_expire = 0; 793 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) { 794 ia->ia6_lifetime.ia6t_preferred = 795 time_second + ia->ia6_lifetime.ia6t_pltime; 796 } else 797 ia->ia6_lifetime.ia6t_preferred = 0; 798 799 /* 800 * make sure to initialize ND6 information. this is to 801 * workaround issues with interfaces with IPv6 addresses, 802 * which have never brought # up. we are assuming that it is 803 * safe to nd6_ifattach multiple times. 804 */ 805 nd6_ifattach(ifp); 806 807 /* 808 * Perform DAD, if needed. 809 * XXX It may be of use, if we can administratively 810 * disable DAD. 811 */ 812 switch (ifp->if_type) { 813 case IFT_ARCNET: 814 case IFT_ETHER: 815 case IFT_FDDI: 816 case IFT_IEEE1394: 817 #if 0 818 case IFT_ATM: 819 case IFT_SLIP: 820 case IFT_PPP: 821 #endif 822 ia->ia6_flags |= IN6_IFF_TENTATIVE; 823 nd6_dad_start(&ia->ia_ifa, NULL); 824 break; 825 case IFT_FAITH: 826 case IFT_GIF: 827 case IFT_LOOP: 828 default: 829 break; 830 } 831 832 if (hostIsNew) { 833 int iilen; 834 int error_local = 0; 835 836 iilen = (sizeof(ia->ia_prefixmask.sin6_addr) << 3) - 837 in6_mask2len(&ia->ia_prefixmask.sin6_addr); 838 error_local = in6_prefix_add_ifid(iilen, ia); 839 if (error == 0) 840 error = error_local; 841 } 842 843 return(error); 844 845 case SIOCDIFADDR_IN6: 846 in6_purgeaddr(&ia->ia_ifa, ifp); 847 break; 848 849 default: 850 if (ifp == NULL || ifp->if_ioctl == 0) 851 return(EOPNOTSUPP); 852 return((*ifp->if_ioctl)(ifp, cmd, data)); 853 } 854 855 return(0); 856 } 857 858 void 859 in6_purgeaddr(ifa, ifp) 860 struct ifaddr *ifa; 861 struct ifnet *ifp; 862 { 863 struct in6_ifaddr *oia, *ia = (void *) ifa; 864 865 /* stop DAD processing */ 866 nd6_dad_stop(ifa); 867 868 in6_ifscrub(ifp, ia); 869 870 if (ifp->if_flags & IFF_MULTICAST) { 871 /* 872 * delete solicited multicast addr for deleting host id 873 */ 874 struct in6_multi *in6m; 875 struct in6_addr llsol; 876 bzero(&llsol, sizeof(struct in6_addr)); 877 llsol.s6_addr16[0] = htons(0xff02); 878 llsol.s6_addr16[1] = htons(ifp->if_index); 879 llsol.s6_addr32[1] = 0; 880 llsol.s6_addr32[2] = htonl(1); 881 llsol.s6_addr32[3] = 882 ia->ia_addr.sin6_addr.s6_addr32[3]; 883 llsol.s6_addr8[12] = 0xff; 884 885 IN6_LOOKUP_MULTI(llsol, ifp, in6m); 886 if (in6m) 887 in6_delmulti(in6m); 888 } 889 890 TAILQ_REMOVE(&ifp->if_addrlist, &ia->ia_ifa, ifa_list); 891 IFAFREE(&ia->ia_ifa); 892 893 oia = ia; 894 if (oia == (ia = in6_ifaddr)) 895 in6_ifaddr = ia->ia_next; 896 else { 897 while (ia->ia_next && (ia->ia_next != oia)) 898 ia = ia->ia_next; 899 if (ia->ia_next) 900 ia->ia_next = oia->ia_next; 901 else 902 printf("Didn't unlink in6_ifaddr from list\n"); 903 } 904 { 905 int iilen; 906 907 iilen = (sizeof(oia->ia_prefixmask.sin6_addr) << 3) - 908 in6_mask2len(&oia->ia_prefixmask.sin6_addr); 909 in6_prefix_remove_ifid(iilen, oia); 910 } 911 if (oia->ia6_multiaddrs.lh_first != NULL) { 912 /* 913 * XXX thorpej@netbsd.org -- if the interface is going 914 * XXX away, don't save the multicast entries, delete them! 915 */ 916 if (oia->ia_ifa.ifa_ifp->if_output == if_nulloutput) { 917 struct in6_multi *in6m; 918 919 while ((in6m = 920 LIST_FIRST(&oia->ia6_multiaddrs)) != NULL) 921 in6_delmulti(in6m); 922 } else 923 in6_savemkludge(oia); 924 } 925 926 IFAFREE(&oia->ia_ifa); 927 } 928 929 void 930 in6_purgeif(ifp) 931 struct ifnet *ifp; 932 { 933 struct ifaddr *ifa, *nifa; 934 935 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL; ifa = nifa) 936 { 937 nifa = TAILQ_NEXT(ifa, ifa_list); 938 if (ifa->ifa_addr->sa_family != AF_INET6) 939 continue; 940 in6_purgeaddr(ifa, ifp); 941 } 942 943 in6_ifdetach(ifp); 944 } 945 946 /* 947 * SIOC[GAD]LIFADDR. 948 * SIOCGLIFADDR: get first address. (?) 949 * SIOCGLIFADDR with IFLR_PREFIX: 950 * get first address that matches the specified prefix. 951 * SIOCALIFADDR: add the specified address. 952 * SIOCALIFADDR with IFLR_PREFIX: 953 * add the specified prefix, filling hostid part from 954 * the first link-local address. prefixlen must be <= 64. 955 * SIOCDLIFADDR: delete the specified address. 956 * SIOCDLIFADDR with IFLR_PREFIX: 957 * delete the first address that matches the specified prefix. 958 * return values: 959 * EINVAL on invalid parameters 960 * EADDRNOTAVAIL on prefix match failed/specified address not found 961 * other values may be returned from in6_ioctl() 962 * 963 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64. 964 * this is to accomodate address naming scheme other than RFC2374, 965 * in the future. 966 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374 967 * address encoding scheme. (see figure on page 8) 968 */ 969 static int 970 in6_lifaddr_ioctl(so, cmd, data, ifp, p) 971 struct socket *so; 972 u_long cmd; 973 caddr_t data; 974 struct ifnet *ifp; 975 struct proc *p; 976 { 977 struct if_laddrreq *iflr = (struct if_laddrreq *)data; 978 struct ifaddr *ifa; 979 struct sockaddr *sa; 980 981 /* sanity checks */ 982 if (!data || !ifp) { 983 panic("invalid argument to in6_lifaddr_ioctl"); 984 /* NOTREACHED */ 985 } 986 987 switch (cmd) { 988 case SIOCGLIFADDR: 989 /* address must be specified on GET with IFLR_PREFIX */ 990 if ((iflr->flags & IFLR_PREFIX) == 0) 991 break; 992 /* FALLTHROUGH */ 993 case SIOCALIFADDR: 994 case SIOCDLIFADDR: 995 /* address must be specified on ADD and DELETE */ 996 sa = (struct sockaddr *)&iflr->addr; 997 if (sa->sa_family != AF_INET6) 998 return EINVAL; 999 if (sa->sa_len != sizeof(struct sockaddr_in6)) 1000 return EINVAL; 1001 /* XXX need improvement */ 1002 sa = (struct sockaddr *)&iflr->dstaddr; 1003 if (sa->sa_family && sa->sa_family != AF_INET6) 1004 return EINVAL; 1005 if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6)) 1006 return EINVAL; 1007 break; 1008 default: /* shouldn't happen */ 1009 #if 0 1010 panic("invalid cmd to in6_lifaddr_ioctl"); 1011 /* NOTREACHED */ 1012 #else 1013 return EOPNOTSUPP; 1014 #endif 1015 } 1016 if (sizeof(struct in6_addr) * 8 < iflr->prefixlen) 1017 return EINVAL; 1018 1019 switch (cmd) { 1020 case SIOCALIFADDR: 1021 { 1022 struct in6_aliasreq ifra; 1023 struct in6_addr *hostid = NULL; 1024 int prefixlen; 1025 1026 if ((iflr->flags & IFLR_PREFIX) != 0) { 1027 struct sockaddr_in6 *sin6; 1028 1029 /* 1030 * hostid is to fill in the hostid part of the 1031 * address. hostid points to the first link-local 1032 * address attached to the interface. 1033 */ 1034 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0); 1035 if (!ifa) 1036 return EADDRNOTAVAIL; 1037 hostid = IFA_IN6(ifa); 1038 1039 /* prefixlen must be <= 64. */ 1040 if (64 < iflr->prefixlen) 1041 return EINVAL; 1042 prefixlen = iflr->prefixlen; 1043 1044 /* hostid part must be zero. */ 1045 sin6 = (struct sockaddr_in6 *)&iflr->addr; 1046 if (sin6->sin6_addr.s6_addr32[2] != 0 1047 || sin6->sin6_addr.s6_addr32[3] != 0) { 1048 return EINVAL; 1049 } 1050 } else 1051 prefixlen = iflr->prefixlen; 1052 1053 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */ 1054 bzero(&ifra, sizeof(ifra)); 1055 bcopy(iflr->iflr_name, ifra.ifra_name, 1056 sizeof(ifra.ifra_name)); 1057 1058 bcopy(&iflr->addr, &ifra.ifra_addr, 1059 ((struct sockaddr *)&iflr->addr)->sa_len); 1060 if (hostid) { 1061 /* fill in hostid part */ 1062 ifra.ifra_addr.sin6_addr.s6_addr32[2] = 1063 hostid->s6_addr32[2]; 1064 ifra.ifra_addr.sin6_addr.s6_addr32[3] = 1065 hostid->s6_addr32[3]; 1066 } 1067 1068 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /* XXX */ 1069 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr, 1070 ((struct sockaddr *)&iflr->dstaddr)->sa_len); 1071 if (hostid) { 1072 ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] = 1073 hostid->s6_addr32[2]; 1074 ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] = 1075 hostid->s6_addr32[3]; 1076 } 1077 } 1078 1079 ifra.ifra_prefixmask.sin6_family = AF_INET6; 1080 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6); 1081 in6_len2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen); 1082 1083 ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX; 1084 return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, p); 1085 } 1086 case SIOCGLIFADDR: 1087 case SIOCDLIFADDR: 1088 { 1089 struct in6_ifaddr *ia; 1090 struct in6_addr mask, candidate, match; 1091 struct sockaddr_in6 *sin6; 1092 int cmp; 1093 1094 bzero(&mask, sizeof(mask)); 1095 if (iflr->flags & IFLR_PREFIX) { 1096 /* lookup a prefix rather than address. */ 1097 in6_len2mask(&mask, iflr->prefixlen); 1098 1099 sin6 = (struct sockaddr_in6 *)&iflr->addr; 1100 bcopy(&sin6->sin6_addr, &match, sizeof(match)); 1101 match.s6_addr32[0] &= mask.s6_addr32[0]; 1102 match.s6_addr32[1] &= mask.s6_addr32[1]; 1103 match.s6_addr32[2] &= mask.s6_addr32[2]; 1104 match.s6_addr32[3] &= mask.s6_addr32[3]; 1105 1106 /* if you set extra bits, that's wrong */ 1107 if (bcmp(&match, &sin6->sin6_addr, sizeof(match))) 1108 return EINVAL; 1109 1110 cmp = 1; 1111 } else { 1112 if (cmd == SIOCGLIFADDR) { 1113 /* on getting an address, take the 1st match */ 1114 cmp = 0; /* XXX */ 1115 } else { 1116 /* on deleting an address, do exact match */ 1117 in6_len2mask(&mask, 128); 1118 sin6 = (struct sockaddr_in6 *)&iflr->addr; 1119 bcopy(&sin6->sin6_addr, &match, sizeof(match)); 1120 1121 cmp = 1; 1122 } 1123 } 1124 1125 for (ifa = ifp->if_addrlist.tqh_first; 1126 ifa; 1127 ifa = ifa->ifa_list.tqe_next) 1128 { 1129 if (ifa->ifa_addr->sa_family != AF_INET6) 1130 continue; 1131 if (!cmp) 1132 break; 1133 bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate)); 1134 candidate.s6_addr32[0] &= mask.s6_addr32[0]; 1135 candidate.s6_addr32[1] &= mask.s6_addr32[1]; 1136 candidate.s6_addr32[2] &= mask.s6_addr32[2]; 1137 candidate.s6_addr32[3] &= mask.s6_addr32[3]; 1138 if (IN6_ARE_ADDR_EQUAL(&candidate, &match)) 1139 break; 1140 } 1141 if (!ifa) 1142 return EADDRNOTAVAIL; 1143 ia = ifa2ia6(ifa); 1144 1145 if (cmd == SIOCGLIFADDR) { 1146 /* fill in the if_laddrreq structure */ 1147 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len); 1148 1149 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 1150 bcopy(&ia->ia_dstaddr, &iflr->dstaddr, 1151 ia->ia_dstaddr.sin6_len); 1152 } else 1153 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr)); 1154 1155 iflr->prefixlen = 1156 in6_mask2len(&ia->ia_prefixmask.sin6_addr); 1157 1158 iflr->flags = ia->ia6_flags; /* XXX */ 1159 1160 return 0; 1161 } else { 1162 struct in6_aliasreq ifra; 1163 1164 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */ 1165 bzero(&ifra, sizeof(ifra)); 1166 bcopy(iflr->iflr_name, ifra.ifra_name, 1167 sizeof(ifra.ifra_name)); 1168 1169 bcopy(&ia->ia_addr, &ifra.ifra_addr, 1170 ia->ia_addr.sin6_len); 1171 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 1172 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr, 1173 ia->ia_dstaddr.sin6_len); 1174 } else { 1175 bzero(&ifra.ifra_dstaddr, 1176 sizeof(ifra.ifra_dstaddr)); 1177 } 1178 bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr, 1179 ia->ia_prefixmask.sin6_len); 1180 1181 ifra.ifra_flags = ia->ia6_flags; 1182 return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra, 1183 ifp, p); 1184 } 1185 } 1186 } 1187 1188 return EOPNOTSUPP; /* just for safety */ 1189 } 1190 1191 /* 1192 * Delete any existing route for an interface. 1193 */ 1194 void 1195 in6_ifscrub(ifp, ia) 1196 struct ifnet *ifp; 1197 struct in6_ifaddr *ia; 1198 { 1199 if ((ia->ia_flags & IFA_ROUTE) == 0) 1200 return; 1201 1202 /* 1203 * We should check the existence of dstaddr, because link-local 1204 * addresses can be configured without particular destinations 1205 * even on point-to-point or loopback interfaces. 1206 * In this case, kernel would panic in rtinit()... 1207 */ 1208 if (ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT) && 1209 (ia->ia_ifa.ifa_dstaddr != NULL)) 1210 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST); 1211 else 1212 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0); 1213 ia->ia_flags &= ~IFA_ROUTE; 1214 1215 /* Remove ownaddr's loopback rtentry, if it exists. */ 1216 in6_ifremloop(&(ia->ia_ifa)); 1217 } 1218 1219 /* 1220 * Initialize an interface's intetnet6 address 1221 * and routing table entry. 1222 */ 1223 static int 1224 in6_ifinit(ifp, ia, sin6, scrub) 1225 struct ifnet *ifp; 1226 struct in6_ifaddr *ia; 1227 struct sockaddr_in6 *sin6; 1228 int scrub; 1229 { 1230 struct sockaddr_in6 oldaddr; 1231 int error, flags = RTF_UP; 1232 int s = splnet(); 1233 1234 oldaddr = ia->ia_addr; 1235 ia->ia_addr = *sin6; 1236 /* 1237 * Give the interface a chance to initialize 1238 * if this is its first address, 1239 * and to validate the address if necessary. 1240 */ 1241 if (ifp->if_ioctl && 1242 (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) { 1243 splx(s); 1244 ia->ia_addr = oldaddr; 1245 return(error); 1246 } 1247 1248 switch (ifp->if_type) { 1249 case IFT_ARCNET: 1250 case IFT_ETHER: 1251 case IFT_FDDI: 1252 case IFT_IEEE1394: 1253 ia->ia_ifa.ifa_rtrequest = nd6_rtrequest; 1254 ia->ia_ifa.ifa_flags |= RTF_CLONING; 1255 break; 1256 case IFT_PPP: 1257 ia->ia_ifa.ifa_rtrequest = nd6_p2p_rtrequest; 1258 ia->ia_ifa.ifa_flags |= RTF_CLONING; 1259 break; 1260 } 1261 1262 splx(s); 1263 if (scrub) { 1264 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr; 1265 in6_ifscrub(ifp, ia); 1266 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr; 1267 } 1268 /* xxx 1269 * in_socktrim 1270 */ 1271 /* 1272 * Add route for the network. 1273 */ 1274 ia->ia_ifa.ifa_metric = ifp->if_metric; 1275 if (ifp->if_flags & IFF_LOOPBACK) { 1276 ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr; 1277 flags |= RTF_HOST; 1278 } else if (ifp->if_flags & IFF_POINTOPOINT) { 1279 if (ia->ia_dstaddr.sin6_family != AF_INET6) 1280 return(0); 1281 flags |= RTF_HOST; 1282 } 1283 if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD, flags)) == 0) 1284 ia->ia_flags |= IFA_ROUTE; 1285 /* XXX check if the subnet route points to the same interface */ 1286 if (error == EEXIST) 1287 error = 0; 1288 1289 /* Add ownaddr as loopback rtentry, if necessary(ex. on p2p link). */ 1290 in6_ifaddloop(&(ia->ia_ifa)); 1291 1292 if (ifp->if_flags & IFF_MULTICAST) 1293 in6_restoremkludge(ia, ifp); 1294 1295 return(error); 1296 } 1297 1298 /* 1299 * Multicast address kludge: 1300 * If there were any multicast addresses attached to this interface address, 1301 * either move them to another address on this interface, or save them until 1302 * such time as this interface is reconfigured for IPv6. 1303 */ 1304 void 1305 in6_savemkludge(oia) 1306 struct in6_ifaddr *oia; 1307 { 1308 struct in6_ifaddr *ia; 1309 struct in6_multi *in6m, *next; 1310 1311 IFP_TO_IA6(oia->ia_ifp, ia); 1312 if (ia) { /* there is another address */ 1313 for (in6m = oia->ia6_multiaddrs.lh_first; in6m; in6m = next){ 1314 next = in6m->in6m_entry.le_next; 1315 IFAFREE(&in6m->in6m_ia->ia_ifa); 1316 IFAREF(&ia->ia_ifa); 1317 in6m->in6m_ia = ia; 1318 LIST_INSERT_HEAD(&ia->ia6_multiaddrs, in6m, in6m_entry); 1319 } 1320 } else { /* last address on this if deleted, save */ 1321 struct multi6_kludge *mk; 1322 1323 for (mk = in6_mk.lh_first; mk; mk = mk->mk_entry.le_next) { 1324 if (mk->mk_ifp == oia->ia_ifp) 1325 break; 1326 } 1327 if (mk == NULL) /* this should not happen! */ 1328 panic("in6_savemkludge: no kludge space"); 1329 1330 for (in6m = oia->ia6_multiaddrs.lh_first; in6m; in6m = next){ 1331 next = in6m->in6m_entry.le_next; 1332 IFAFREE(&in6m->in6m_ia->ia_ifa); /* release reference */ 1333 in6m->in6m_ia = NULL; 1334 LIST_INSERT_HEAD(&mk->mk_head, in6m, in6m_entry); 1335 } 1336 } 1337 } 1338 1339 /* 1340 * Continuation of multicast address hack: 1341 * If there was a multicast group list previously saved for this interface, 1342 * then we re-attach it to the first address configured on the i/f. 1343 */ 1344 void 1345 in6_restoremkludge(ia, ifp) 1346 struct in6_ifaddr *ia; 1347 struct ifnet *ifp; 1348 { 1349 struct multi6_kludge *mk; 1350 1351 for (mk = in6_mk.lh_first; mk; mk = mk->mk_entry.le_next) { 1352 if (mk->mk_ifp == ifp) { 1353 struct in6_multi *in6m, *next; 1354 1355 for (in6m = mk->mk_head.lh_first; in6m; in6m = next) { 1356 next = in6m->in6m_entry.le_next; 1357 in6m->in6m_ia = ia; 1358 IFAREF(&ia->ia_ifa); 1359 LIST_INSERT_HEAD(&ia->ia6_multiaddrs, 1360 in6m, in6m_entry); 1361 } 1362 LIST_INIT(&mk->mk_head); 1363 break; 1364 } 1365 } 1366 } 1367 1368 /* 1369 * Allocate space for the kludge at interface initialization time. 1370 * Formerly, we dynamically allocated the space in in6_savemkludge() with 1371 * malloc(M_WAITOK). However, it was wrong since the function could be called 1372 * under an interrupt context (software timer on address lifetime expiration). 1373 * Also, we cannot just give up allocating the strucutre, since the group 1374 * membership structure is very complex and we need to keep it anyway. 1375 * Of course, this function MUST NOT be called under an interrupt context. 1376 * Specifically, it is expected to be called only from in6_ifattach(), though 1377 * it is a global function. 1378 */ 1379 void 1380 in6_createmkludge(ifp) 1381 struct ifnet *ifp; 1382 { 1383 struct multi6_kludge *mk; 1384 1385 for (mk = in6_mk.lh_first; mk; mk = mk->mk_entry.le_next) { 1386 /* If we've already had one, do not allocate. */ 1387 if (mk->mk_ifp == ifp) 1388 return; 1389 } 1390 1391 mk = malloc(sizeof(*mk), M_IPMADDR, M_WAITOK); 1392 1393 bzero(mk, sizeof(*mk)); 1394 LIST_INIT(&mk->mk_head); 1395 mk->mk_ifp = ifp; 1396 LIST_INSERT_HEAD(&in6_mk, mk, mk_entry); 1397 } 1398 1399 void 1400 in6_purgemkludge(ifp) 1401 struct ifnet *ifp; 1402 { 1403 struct multi6_kludge *mk; 1404 struct in6_multi *in6m; 1405 1406 for (mk = in6_mk.lh_first; mk; mk = mk->mk_entry.le_next) { 1407 if (mk->mk_ifp != ifp) 1408 continue; 1409 1410 /* leave from all multicast groups joined */ 1411 while ((in6m = LIST_FIRST(&mk->mk_head)) != NULL) 1412 in6_delmulti(in6m); 1413 LIST_REMOVE(mk, mk_entry); 1414 free(mk, M_IPMADDR); 1415 break; 1416 } 1417 } 1418 1419 /* 1420 * Add an address to the list of IP6 multicast addresses for a 1421 * given interface. 1422 */ 1423 struct in6_multi * 1424 in6_addmulti(maddr6, ifp, errorp) 1425 struct in6_addr *maddr6; 1426 struct ifnet *ifp; 1427 int *errorp; 1428 { 1429 struct in6_ifaddr *ia; 1430 struct in6_ifreq ifr; 1431 struct in6_multi *in6m; 1432 int s = splsoftnet(); 1433 1434 *errorp = 0; 1435 /* 1436 * See if address already in list. 1437 */ 1438 IN6_LOOKUP_MULTI(*maddr6, ifp, in6m); 1439 if (in6m != NULL) { 1440 /* 1441 * Found it; just increment the refrence count. 1442 */ 1443 in6m->in6m_refcount++; 1444 } else { 1445 /* 1446 * New address; allocate a new multicast record 1447 * and link it into the interface's multicast list. 1448 */ 1449 in6m = (struct in6_multi *) 1450 malloc(sizeof(*in6m), M_IPMADDR, M_NOWAIT); 1451 if (in6m == NULL) { 1452 splx(s); 1453 *errorp = ENOBUFS; 1454 return(NULL); 1455 } 1456 in6m->in6m_addr = *maddr6; 1457 in6m->in6m_ifp = ifp; 1458 in6m->in6m_refcount = 1; 1459 IFP_TO_IA6(ifp, ia); 1460 if (ia == NULL) { 1461 free(in6m, M_IPMADDR); 1462 splx(s); 1463 *errorp = EADDRNOTAVAIL; /* appropriate? */ 1464 return(NULL); 1465 } 1466 in6m->in6m_ia = ia; 1467 IFAREF(&ia->ia_ifa); /* gain a reference */ 1468 LIST_INSERT_HEAD(&ia->ia6_multiaddrs, in6m, in6m_entry); 1469 1470 /* 1471 * Ask the network driver to update its multicast reception 1472 * filter appropriately for the new address. 1473 */ 1474 bzero(&ifr.ifr_addr, sizeof(struct sockaddr_in6)); 1475 ifr.ifr_addr.sin6_len = sizeof(struct sockaddr_in6); 1476 ifr.ifr_addr.sin6_family = AF_INET6; 1477 ifr.ifr_addr.sin6_addr = *maddr6; 1478 if (ifp->if_ioctl == NULL) 1479 *errorp = ENXIO; /* XXX: appropriate? */ 1480 else 1481 *errorp = (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 1482 (caddr_t)&ifr); 1483 if (*errorp) { 1484 LIST_REMOVE(in6m, in6m_entry); 1485 free(in6m, M_IPMADDR); 1486 IFAFREE(&ia->ia_ifa); 1487 splx(s); 1488 return(NULL); 1489 } 1490 /* 1491 * Let MLD6 know that we have joined a new IP6 multicast 1492 * group. 1493 */ 1494 mld6_start_listening(in6m); 1495 } 1496 splx(s); 1497 return(in6m); 1498 } 1499 1500 /* 1501 * Delete a multicast address record. 1502 */ 1503 void 1504 in6_delmulti(in6m) 1505 struct in6_multi *in6m; 1506 { 1507 struct in6_ifreq ifr; 1508 int s = splsoftnet(); 1509 1510 if (--in6m->in6m_refcount == 0) { 1511 /* 1512 * No remaining claims to this record; let MLD6 know 1513 * that we are leaving the multicast group. 1514 */ 1515 mld6_stop_listening(in6m); 1516 1517 /* 1518 * Unlink from list. 1519 */ 1520 LIST_REMOVE(in6m, in6m_entry); 1521 if (in6m->in6m_ia) { 1522 IFAFREE(&in6m->in6m_ia->ia_ifa); /* release reference */ 1523 } 1524 1525 /* 1526 * Notify the network driver to update its multicast 1527 * reception filter. 1528 */ 1529 bzero(&ifr.ifr_addr, sizeof(struct sockaddr_in6)); 1530 ifr.ifr_addr.sin6_len = sizeof(struct sockaddr_in6); 1531 ifr.ifr_addr.sin6_family = AF_INET6; 1532 ifr.ifr_addr.sin6_addr = in6m->in6m_addr; 1533 (*in6m->in6m_ifp->if_ioctl)(in6m->in6m_ifp, 1534 SIOCDELMULTI, (caddr_t)&ifr); 1535 free(in6m, M_IPMADDR); 1536 } 1537 splx(s); 1538 } 1539 1540 struct in6_multi_mship * 1541 in6_joingroup(ifp, addr, errorp) 1542 struct ifnet *ifp; 1543 struct in6_addr *addr; 1544 int *errorp; 1545 { 1546 struct in6_multi_mship *imm; 1547 1548 imm = malloc(sizeof(*imm), M_IPMADDR, M_NOWAIT); 1549 if (!imm) { 1550 *errorp = ENOBUFS; 1551 return NULL; 1552 } 1553 imm->i6mm_maddr = in6_addmulti(addr, ifp, errorp); 1554 if (!imm->i6mm_maddr) { 1555 /* *errorp is alrady set */ 1556 free(imm, M_IPMADDR); 1557 return NULL; 1558 } 1559 return imm; 1560 } 1561 1562 int 1563 in6_leavegroup(imm) 1564 struct in6_multi_mship *imm; 1565 { 1566 1567 if (imm->i6mm_maddr) 1568 in6_delmulti(imm->i6mm_maddr); 1569 free(imm, M_IPMADDR); 1570 return 0; 1571 } 1572 1573 /* 1574 * Find an IPv6 interface link-local address specific to an interface. 1575 */ 1576 struct in6_ifaddr * 1577 in6ifa_ifpforlinklocal(ifp, ignoreflags) 1578 struct ifnet *ifp; 1579 int ignoreflags; 1580 { 1581 struct ifaddr *ifa; 1582 1583 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next) 1584 { 1585 if (ifa->ifa_addr == NULL) 1586 continue; /* just for safety */ 1587 if (ifa->ifa_addr->sa_family != AF_INET6) 1588 continue; 1589 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) { 1590 if ((((struct in6_ifaddr *)ifa)->ia6_flags & 1591 ignoreflags) != 0) 1592 continue; 1593 break; 1594 } 1595 } 1596 1597 return((struct in6_ifaddr *)ifa); 1598 } 1599 1600 1601 /* 1602 * find the internet address corresponding to a given interface and address. 1603 */ 1604 struct in6_ifaddr * 1605 in6ifa_ifpwithaddr(ifp, addr) 1606 struct ifnet *ifp; 1607 struct in6_addr *addr; 1608 { 1609 struct ifaddr *ifa; 1610 1611 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next) 1612 { 1613 if (ifa->ifa_addr == NULL) 1614 continue; /* just for safety */ 1615 if (ifa->ifa_addr->sa_family != AF_INET6) 1616 continue; 1617 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa))) 1618 break; 1619 } 1620 1621 return((struct in6_ifaddr *)ifa); 1622 } 1623 1624 /* 1625 * Convert IP6 address to printable (loggable) representation. 1626 */ 1627 static char digits[] = "0123456789abcdef"; 1628 static int ip6round = 0; 1629 char * 1630 ip6_sprintf(addr) 1631 const struct in6_addr *addr; 1632 { 1633 static char ip6buf[8][48]; 1634 int i; 1635 char *cp; 1636 const u_short *a = (u_short *)addr; 1637 const u_char *d; 1638 int dcolon = 0; 1639 1640 ip6round = (ip6round + 1) & 7; 1641 cp = ip6buf[ip6round]; 1642 1643 for (i = 0; i < 8; i++) { 1644 if (dcolon == 1) { 1645 if (*a == 0) { 1646 if (i == 7) 1647 *cp++ = ':'; 1648 a++; 1649 continue; 1650 } else 1651 dcolon = 2; 1652 } 1653 if (*a == 0) { 1654 if (dcolon == 0 && *(a + 1) == 0) { 1655 if (i == 0) 1656 *cp++ = ':'; 1657 *cp++ = ':'; 1658 dcolon = 1; 1659 } else { 1660 *cp++ = '0'; 1661 *cp++ = ':'; 1662 } 1663 a++; 1664 continue; 1665 } 1666 d = (const u_char *)a; 1667 *cp++ = digits[*d >> 4]; 1668 *cp++ = digits[*d++ & 0xf]; 1669 *cp++ = digits[*d >> 4]; 1670 *cp++ = digits[*d & 0xf]; 1671 *cp++ = ':'; 1672 a++; 1673 } 1674 *--cp = 0; 1675 return(ip6buf[ip6round]); 1676 } 1677 1678 int 1679 in6_localaddr(in6) 1680 struct in6_addr *in6; 1681 { 1682 struct in6_ifaddr *ia; 1683 1684 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6)) 1685 return 1; 1686 1687 for (ia = in6_ifaddr; ia; ia = ia->ia_next) 1688 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr, 1689 &ia->ia_prefixmask.sin6_addr)) 1690 return 1; 1691 1692 return (0); 1693 } 1694 1695 /* 1696 * Get a scope of the address. Node-local, link-local, site-local or global. 1697 */ 1698 int 1699 in6_addrscope (addr) 1700 struct in6_addr *addr; 1701 { 1702 int scope; 1703 1704 if (addr->s6_addr8[0] == 0xfe) { 1705 scope = addr->s6_addr8[1] & 0xc0; 1706 1707 switch (scope) { 1708 case 0x80: 1709 return IPV6_ADDR_SCOPE_LINKLOCAL; 1710 break; 1711 case 0xc0: 1712 return IPV6_ADDR_SCOPE_SITELOCAL; 1713 break; 1714 default: 1715 return IPV6_ADDR_SCOPE_GLOBAL; /* just in case */ 1716 break; 1717 } 1718 } 1719 1720 1721 if (addr->s6_addr8[0] == 0xff) { 1722 scope = addr->s6_addr8[1] & 0x0f; 1723 1724 /* 1725 * due to other scope such as reserved, 1726 * return scope doesn't work. 1727 */ 1728 switch (scope) { 1729 case IPV6_ADDR_SCOPE_NODELOCAL: 1730 return IPV6_ADDR_SCOPE_NODELOCAL; 1731 break; 1732 case IPV6_ADDR_SCOPE_LINKLOCAL: 1733 return IPV6_ADDR_SCOPE_LINKLOCAL; 1734 break; 1735 case IPV6_ADDR_SCOPE_SITELOCAL: 1736 return IPV6_ADDR_SCOPE_SITELOCAL; 1737 break; 1738 default: 1739 return IPV6_ADDR_SCOPE_GLOBAL; 1740 break; 1741 } 1742 } 1743 1744 if (bcmp(&in6addr_loopback, addr, sizeof(addr) - 1) == 0) { 1745 if (addr->s6_addr8[15] == 1) /* loopback */ 1746 return IPV6_ADDR_SCOPE_NODELOCAL; 1747 if (addr->s6_addr8[15] == 0) /* unspecified */ 1748 return IPV6_ADDR_SCOPE_LINKLOCAL; 1749 } 1750 1751 return IPV6_ADDR_SCOPE_GLOBAL; 1752 } 1753 1754 int 1755 in6_addr2scopeid(ifp, addr) 1756 struct ifnet *ifp; /* must not be NULL */ 1757 struct in6_addr *addr; /* must not be NULL */ 1758 { 1759 int scope = in6_addrscope(addr); 1760 1761 switch (scope) { 1762 case IPV6_ADDR_SCOPE_NODELOCAL: 1763 return(-1); /* XXX: is this an appropriate value? */ 1764 1765 case IPV6_ADDR_SCOPE_LINKLOCAL: 1766 /* XXX: we do not distinguish between a link and an I/F. */ 1767 return(ifp->if_index); 1768 1769 case IPV6_ADDR_SCOPE_SITELOCAL: 1770 return(0); /* XXX: invalid. */ 1771 1772 default: 1773 return(0); /* XXX: treat as global. */ 1774 } 1775 } 1776 1777 /* 1778 * return length of part which dst and src are equal 1779 * hard coding... 1780 */ 1781 int 1782 in6_matchlen(src, dst) 1783 struct in6_addr *src, *dst; 1784 { 1785 int match = 0; 1786 u_char *s = (u_char *)src, *d = (u_char *)dst; 1787 u_char *lim = s + 16, r; 1788 1789 while (s < lim) 1790 if ((r = (*d++ ^ *s++)) != 0) { 1791 while (r < 128) { 1792 match++; 1793 r <<= 1; 1794 } 1795 break; 1796 } else 1797 match += 8; 1798 return match; 1799 } 1800 1801 /* XXX: to be scope conscious */ 1802 int 1803 in6_are_prefix_equal(p1, p2, len) 1804 struct in6_addr *p1, *p2; 1805 int len; 1806 { 1807 int bytelen, bitlen; 1808 1809 /* sanity check */ 1810 if (0 > len || len > 128) { 1811 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n", 1812 len); 1813 return(0); 1814 } 1815 1816 bytelen = len / 8; 1817 bitlen = len % 8; 1818 1819 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen)) 1820 return(0); 1821 if (p1->s6_addr[bytelen] >> (8 - bitlen) != 1822 p2->s6_addr[bytelen] >> (8 - bitlen)) 1823 return(0); 1824 1825 return(1); 1826 } 1827 1828 void 1829 in6_prefixlen2mask(maskp, len) 1830 struct in6_addr *maskp; 1831 int len; 1832 { 1833 u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff}; 1834 int bytelen, bitlen, i; 1835 1836 /* sanity check */ 1837 if (0 > len || len > 128) { 1838 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n", 1839 len); 1840 return; 1841 } 1842 1843 bzero(maskp, sizeof(*maskp)); 1844 bytelen = len / 8; 1845 bitlen = len % 8; 1846 for (i = 0; i < bytelen; i++) 1847 maskp->s6_addr[i] = 0xff; 1848 if (bitlen) 1849 maskp->s6_addr[bytelen] = maskarray[bitlen - 1]; 1850 } 1851 1852 /* 1853 * return the best address out of the same scope 1854 */ 1855 struct in6_ifaddr * 1856 in6_ifawithscope(oifp, dst) 1857 struct ifnet *oifp; 1858 struct in6_addr *dst; 1859 { 1860 int dst_scope = in6_addrscope(dst), src_scope, best_scope = 0; 1861 int blen = -1; 1862 struct ifaddr *ifa; 1863 struct ifnet *ifp; 1864 struct in6_ifaddr *ifa_best = NULL; 1865 1866 if (oifp == NULL) { 1867 printf("in6_ifawithscope: output interface is not specified\n"); 1868 return(NULL); 1869 } 1870 1871 /* 1872 * We search for all addresses on all interfaces from the beginning. 1873 * Comparing an interface with the outgoing interface will be done 1874 * only at the final stage of tiebreaking. 1875 */ 1876 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) 1877 { 1878 /* 1879 * We can never take an address that breaks the scope zone 1880 * of the destination. 1881 */ 1882 if (in6_addr2scopeid(ifp, dst) != in6_addr2scopeid(oifp, dst)) 1883 continue; 1884 1885 for (ifa = ifp->if_addrlist.tqh_first; ifa; 1886 ifa = ifa->ifa_list.tqe_next) 1887 { 1888 int tlen = -1, dscopecmp, bscopecmp, matchcmp; 1889 1890 if (ifa->ifa_addr->sa_family != AF_INET6) 1891 continue; 1892 1893 src_scope = in6_addrscope(IFA_IN6(ifa)); 1894 1895 #ifdef ADDRSELECT_DEBUG /* should be removed after stabilization */ 1896 dscopecmp = IN6_ARE_SCOPE_CMP(src_scope, dst_scope); 1897 printf("in6_ifawithscope: dst=%s bestaddr=%s, " 1898 "newaddr=%s, scope=%x, dcmp=%d, bcmp=%d, " 1899 "matchlen=%d, flgs=%x\n", 1900 ip6_sprintf(dst), 1901 ifa_best ? ip6_sprintf(&ifa_best->ia_addr.sin6_addr) : "none", 1902 ip6_sprintf(IFA_IN6(ifa)), src_scope, 1903 dscopecmp, 1904 ifa_best ? IN6_ARE_SCOPE_CMP(src_scope, best_scope) : -1, 1905 in6_matchlen(IFA_IN6(ifa), dst), 1906 ((struct in6_ifaddr *)ifa)->ia6_flags); 1907 #endif 1908 1909 /* 1910 * Don't use an address before completing DAD 1911 * nor a duplicated address. 1912 */ 1913 if (((struct in6_ifaddr *)ifa)->ia6_flags & 1914 IN6_IFF_NOTREADY) 1915 continue; 1916 1917 /* XXX: is there any case to allow anycasts? */ 1918 if (((struct in6_ifaddr *)ifa)->ia6_flags & 1919 IN6_IFF_ANYCAST) 1920 continue; 1921 1922 if (((struct in6_ifaddr *)ifa)->ia6_flags & 1923 IN6_IFF_DETACHED) 1924 continue; 1925 1926 /* 1927 * If this is the first address we find, 1928 * keep it anyway. 1929 */ 1930 if (ifa_best == NULL) 1931 goto replace; 1932 1933 /* 1934 * ifa_best is never NULL beyond this line except 1935 * within the block labeled "replace". 1936 */ 1937 1938 /* 1939 * If ifa_best has a smaller scope than dst and 1940 * the current address has a larger one than 1941 * (or equal to) dst, always replace ifa_best. 1942 * Also, if the current address has a smaller scope 1943 * than dst, ignore it unless ifa_best also has a 1944 * smaller scope. 1945 */ 1946 if (IN6_ARE_SCOPE_CMP(best_scope, dst_scope) < 0 && 1947 IN6_ARE_SCOPE_CMP(src_scope, dst_scope) >= 0) 1948 goto replace; 1949 if (IN6_ARE_SCOPE_CMP(src_scope, dst_scope) < 0 && 1950 IN6_ARE_SCOPE_CMP(best_scope, dst_scope) >= 0) 1951 continue; 1952 1953 /* 1954 * A deprecated address SHOULD NOT be used in new 1955 * communications if an alternate (non-deprecated) 1956 * address is available and has sufficient scope. 1957 * RFC 2462, Section 5.5.4. 1958 */ 1959 if (((struct in6_ifaddr *)ifa)->ia6_flags & 1960 IN6_IFF_DEPRECATED) { 1961 /* 1962 * Ignore any deprecated addresses if 1963 * specified by configuration. 1964 */ 1965 if (!ip6_use_deprecated) 1966 continue; 1967 1968 /* 1969 * If we have already found a non-deprecated 1970 * candidate, just ignore deprecated addresses. 1971 */ 1972 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) 1973 == 0) 1974 continue; 1975 } 1976 1977 /* 1978 * A non-deprecated address is always preferred 1979 * to a deprecated one regardless of scopes and 1980 * address matching. 1981 */ 1982 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) && 1983 (((struct in6_ifaddr *)ifa)->ia6_flags & 1984 IN6_IFF_DEPRECATED) == 0) 1985 goto replace; 1986 1987 /* 1988 * At this point, we have two cases: 1989 * 1. we are looking at a non-deprecated address, 1990 * and ifa_best is also non-deprecated. 1991 * 2. we are looking at a deprecated address, 1992 * and ifa_best is also deprecated. 1993 * Also, we do not have to consider a case where 1994 * the scope of if_best is larger(smaller) than dst and 1995 * the scope of the current address is smaller(larger) 1996 * than dst. Such a case has already been covered. 1997 * Tiebreaking is done according to the following 1998 * items: 1999 * - the scope comparison between the address and 2000 * dst (dscopecmp) 2001 * - the scope comparison between the address and 2002 * ifa_best (bscopecmp) 2003 * - if the address match dst longer than ifa_best 2004 * (matchcmp) 2005 * - if the address is on the outgoing I/F (outI/F) 2006 * 2007 * Roughly speaking, the selection policy is 2008 * - the most important item is scope. The same scope 2009 * is best. Then search for a larger scope. 2010 * Smaller scopes are the last resort. 2011 * - A deprecated address is chosen only when we have 2012 * no address that has an enough scope, but is 2013 * prefered to any addresses of smaller scopes. 2014 * - Longest address match against dst is considered 2015 * only for addresses that has the same scope of dst. 2016 * - If there is no other reasons to choose one, 2017 * addresses on the outgoing I/F are preferred. 2018 * 2019 * The precise decision table is as follows: 2020 * dscopecmp bscopecmp matchcmp outI/F | replace? 2021 * !equal equal N/A Yes | Yes (1) 2022 * !equal equal N/A No | No (2) 2023 * larger larger N/A N/A | No (3) 2024 * larger smaller N/A N/A | Yes (4) 2025 * smaller larger N/A N/A | Yes (5) 2026 * smaller smaller N/A N/A | No (6) 2027 * equal smaller N/A N/A | Yes (7) 2028 * equal larger (already done) 2029 * equal equal larger N/A | Yes (8) 2030 * equal equal smaller N/A | No (9) 2031 * equal equal equal Yes | Yes (a) 2032 * eaual eqaul equal No | No (b) 2033 */ 2034 dscopecmp = IN6_ARE_SCOPE_CMP(src_scope, dst_scope); 2035 bscopecmp = IN6_ARE_SCOPE_CMP(src_scope, best_scope); 2036 2037 if (dscopecmp && bscopecmp == 0) { 2038 if (oifp == ifp) /* (1) */ 2039 goto replace; 2040 continue; /* (2) */ 2041 } 2042 if (dscopecmp > 0) { 2043 if (bscopecmp > 0) /* (3) */ 2044 continue; 2045 goto replace; /* (4) */ 2046 } 2047 if (dscopecmp < 0) { 2048 if (bscopecmp > 0) /* (5) */ 2049 goto replace; 2050 continue; /* (6) */ 2051 } 2052 2053 /* now dscopecmp must be 0 */ 2054 if (bscopecmp < 0) 2055 goto replace; /* (7) */ 2056 2057 /* 2058 * At last both dscopecmp and bscopecmp must be 0. 2059 * We need address matching against dst for 2060 * tiebreaking. 2061 */ 2062 tlen = in6_matchlen(IFA_IN6(ifa), dst); 2063 matchcmp = tlen - blen; 2064 if (matchcmp > 0) /* (8) */ 2065 goto replace; 2066 if (matchcmp < 0) /* (9) */ 2067 continue; 2068 if (oifp == ifp) /* (a) */ 2069 goto replace; 2070 continue; /* (b) */ 2071 2072 replace: 2073 ifa_best = (struct in6_ifaddr *)ifa; 2074 blen = tlen >= 0 ? tlen : 2075 in6_matchlen(IFA_IN6(ifa), dst); 2076 best_scope = in6_addrscope(&ifa_best->ia_addr.sin6_addr); 2077 } 2078 } 2079 2080 /* count statistics for future improvements */ 2081 if (ifa_best == NULL) 2082 ip6stat.ip6s_sources_none++; 2083 else { 2084 if (oifp == ifa_best->ia_ifp) 2085 ip6stat.ip6s_sources_sameif[best_scope]++; 2086 else 2087 ip6stat.ip6s_sources_otherif[best_scope]++; 2088 2089 if (best_scope == dst_scope) 2090 ip6stat.ip6s_sources_samescope[best_scope]++; 2091 else 2092 ip6stat.ip6s_sources_otherscope[best_scope]++; 2093 2094 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) != 0) 2095 ip6stat.ip6s_sources_deprecated[best_scope]++; 2096 } 2097 2098 return(ifa_best); 2099 } 2100 2101 /* 2102 * return the best address out of the same scope. if no address was 2103 * found, return the first valid address from designated IF. 2104 */ 2105 struct in6_ifaddr * 2106 in6_ifawithifp(ifp, dst) 2107 struct ifnet *ifp; 2108 struct in6_addr *dst; 2109 { 2110 int dst_scope = in6_addrscope(dst), blen = -1, tlen; 2111 struct ifaddr *ifa; 2112 struct in6_ifaddr *besta = 0; 2113 struct in6_ifaddr *dep[2]; /* last-resort: deprecated */ 2114 2115 dep[0] = dep[1] = NULL; 2116 2117 /* 2118 * We first look for addresses in the same scope. 2119 * If there is one, return it. 2120 * If two or more, return one which matches the dst longest. 2121 * If none, return one of global addresses assigned other ifs. 2122 */ 2123 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next) 2124 { 2125 if (ifa->ifa_addr->sa_family != AF_INET6) 2126 continue; 2127 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST) 2128 continue; /* XXX: is there any case to allow anycast? */ 2129 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY) 2130 continue; /* don't use this interface */ 2131 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED) 2132 continue; 2133 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) { 2134 if (ip6_use_deprecated) 2135 dep[0] = (struct in6_ifaddr *)ifa; 2136 continue; 2137 } 2138 2139 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) { 2140 /* 2141 * call in6_matchlen() as few as possible 2142 */ 2143 if (besta) { 2144 if (blen == -1) 2145 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst); 2146 tlen = in6_matchlen(IFA_IN6(ifa), dst); 2147 if (tlen > blen) { 2148 blen = tlen; 2149 besta = (struct in6_ifaddr *)ifa; 2150 } 2151 } else 2152 besta = (struct in6_ifaddr *)ifa; 2153 } 2154 } 2155 if (besta) 2156 return(besta); 2157 2158 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next) 2159 { 2160 if (ifa->ifa_addr->sa_family != AF_INET6) 2161 continue; 2162 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST) 2163 continue; /* XXX: is there any case to allow anycast? */ 2164 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY) 2165 continue; /* don't use this interface */ 2166 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED) 2167 continue; 2168 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) { 2169 if (ip6_use_deprecated) 2170 dep[1] = (struct in6_ifaddr *)ifa; 2171 continue; 2172 } 2173 2174 return (struct in6_ifaddr *)ifa; 2175 } 2176 2177 /* use the last-resort values, that are, deprecated addresses */ 2178 if (dep[0]) 2179 return dep[0]; 2180 if (dep[1]) 2181 return dep[1]; 2182 2183 return NULL; 2184 } 2185 2186 /* 2187 * perform DAD when interface becomes IFF_UP. 2188 */ 2189 void 2190 in6_if_up(ifp) 2191 struct ifnet *ifp; 2192 { 2193 struct ifaddr *ifa; 2194 struct in6_ifaddr *ia; 2195 int dad_delay; /* delay ticks before DAD output */ 2196 2197 /* 2198 * special cases, like 6to4, are handled in in6_ifattach 2199 */ 2200 in6_ifattach(ifp, NULL); 2201 2202 dad_delay = 0; 2203 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next) 2204 { 2205 if (ifa->ifa_addr->sa_family != AF_INET6) 2206 continue; 2207 ia = (struct in6_ifaddr *)ifa; 2208 if (ia->ia6_flags & IN6_IFF_TENTATIVE) 2209 nd6_dad_start(ifa, &dad_delay); 2210 } 2211 } 2212 2213 /* 2214 * Calculate max IPv6 MTU through all the interfaces and store it 2215 * to in6_maxmtu. 2216 */ 2217 void 2218 in6_setmaxmtu() 2219 { 2220 unsigned long maxmtu = 0; 2221 struct ifnet *ifp; 2222 2223 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) 2224 { 2225 if ((ifp->if_flags & IFF_LOOPBACK) == 0 && 2226 nd_ifinfo[ifp->if_index].linkmtu > maxmtu) 2227 maxmtu = nd_ifinfo[ifp->if_index].linkmtu; 2228 } 2229 if (maxmtu) /* update only when maxmtu is positive */ 2230 in6_maxmtu = maxmtu; 2231 } 2232