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