1 /* $NetBSD: in6.c,v 1.57 2002/05/25 22:18:49 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.57 2002/05/25 22:18:49 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 */ 810 if (in6if_do_dad(ifp)) { 811 ia->ia6_flags |= IN6_IFF_TENTATIVE; 812 nd6_dad_start(&ia->ia_ifa, NULL); 813 } 814 815 if (hostIsNew) { 816 int iilen; 817 int error_local = 0; 818 819 iilen = (sizeof(ia->ia_prefixmask.sin6_addr) << 3) - 820 in6_mask2len(&ia->ia_prefixmask.sin6_addr); 821 error_local = in6_prefix_add_ifid(iilen, ia); 822 if (error == 0) 823 error = error_local; 824 } 825 826 return(error); 827 828 case SIOCDIFADDR_IN6: 829 in6_purgeaddr(&ia->ia_ifa, ifp); 830 break; 831 832 default: 833 if (ifp == NULL || ifp->if_ioctl == 0) 834 return(EOPNOTSUPP); 835 return((*ifp->if_ioctl)(ifp, cmd, data)); 836 } 837 838 return(0); 839 } 840 841 void 842 in6_purgeaddr(ifa, ifp) 843 struct ifaddr *ifa; 844 struct ifnet *ifp; 845 { 846 struct in6_ifaddr *oia, *ia = (void *) ifa; 847 848 /* stop DAD processing */ 849 nd6_dad_stop(ifa); 850 851 in6_ifscrub(ifp, ia); 852 853 if (ifp->if_flags & IFF_MULTICAST) { 854 /* 855 * delete solicited multicast addr for deleting host id 856 */ 857 struct in6_multi *in6m; 858 struct in6_addr llsol; 859 bzero(&llsol, sizeof(struct in6_addr)); 860 llsol.s6_addr16[0] = htons(0xff02); 861 llsol.s6_addr16[1] = htons(ifp->if_index); 862 llsol.s6_addr32[1] = 0; 863 llsol.s6_addr32[2] = htonl(1); 864 llsol.s6_addr32[3] = 865 ia->ia_addr.sin6_addr.s6_addr32[3]; 866 llsol.s6_addr8[12] = 0xff; 867 868 IN6_LOOKUP_MULTI(llsol, ifp, in6m); 869 if (in6m) 870 in6_delmulti(in6m); 871 } 872 873 TAILQ_REMOVE(&ifp->if_addrlist, &ia->ia_ifa, ifa_list); 874 IFAFREE(&ia->ia_ifa); 875 876 oia = ia; 877 if (oia == (ia = in6_ifaddr)) 878 in6_ifaddr = ia->ia_next; 879 else { 880 while (ia->ia_next && (ia->ia_next != oia)) 881 ia = ia->ia_next; 882 if (ia->ia_next) 883 ia->ia_next = oia->ia_next; 884 else 885 printf("Didn't unlink in6_ifaddr from list\n"); 886 } 887 { 888 int iilen; 889 890 iilen = (sizeof(oia->ia_prefixmask.sin6_addr) << 3) - 891 in6_mask2len(&oia->ia_prefixmask.sin6_addr); 892 in6_prefix_remove_ifid(iilen, oia); 893 } 894 if (oia->ia6_multiaddrs.lh_first != NULL) { 895 /* 896 * XXX thorpej@netbsd.org -- if the interface is going 897 * XXX away, don't save the multicast entries, delete them! 898 */ 899 if (oia->ia_ifa.ifa_ifp->if_output == if_nulloutput) { 900 struct in6_multi *in6m; 901 902 while ((in6m = 903 LIST_FIRST(&oia->ia6_multiaddrs)) != NULL) 904 in6_delmulti(in6m); 905 } else 906 in6_savemkludge(oia); 907 } 908 909 IFAFREE(&oia->ia_ifa); 910 } 911 912 void 913 in6_purgeif(ifp) 914 struct ifnet *ifp; 915 { 916 struct ifaddr *ifa, *nifa; 917 918 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL; ifa = nifa) 919 { 920 nifa = TAILQ_NEXT(ifa, ifa_list); 921 if (ifa->ifa_addr->sa_family != AF_INET6) 922 continue; 923 in6_purgeaddr(ifa, ifp); 924 } 925 926 in6_ifdetach(ifp); 927 } 928 929 /* 930 * SIOC[GAD]LIFADDR. 931 * SIOCGLIFADDR: get first address. (?) 932 * SIOCGLIFADDR with IFLR_PREFIX: 933 * get first address that matches the specified prefix. 934 * SIOCALIFADDR: add the specified address. 935 * SIOCALIFADDR with IFLR_PREFIX: 936 * add the specified prefix, filling hostid part from 937 * the first link-local address. prefixlen must be <= 64. 938 * SIOCDLIFADDR: delete the specified address. 939 * SIOCDLIFADDR with IFLR_PREFIX: 940 * delete the first address that matches the specified prefix. 941 * return values: 942 * EINVAL on invalid parameters 943 * EADDRNOTAVAIL on prefix match failed/specified address not found 944 * other values may be returned from in6_ioctl() 945 * 946 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64. 947 * this is to accomodate address naming scheme other than RFC2374, 948 * in the future. 949 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374 950 * address encoding scheme. (see figure on page 8) 951 */ 952 static int 953 in6_lifaddr_ioctl(so, cmd, data, ifp, p) 954 struct socket *so; 955 u_long cmd; 956 caddr_t data; 957 struct ifnet *ifp; 958 struct proc *p; 959 { 960 struct if_laddrreq *iflr = (struct if_laddrreq *)data; 961 struct ifaddr *ifa; 962 struct sockaddr *sa; 963 964 /* sanity checks */ 965 if (!data || !ifp) { 966 panic("invalid argument to in6_lifaddr_ioctl"); 967 /* NOTREACHED */ 968 } 969 970 switch (cmd) { 971 case SIOCGLIFADDR: 972 /* address must be specified on GET with IFLR_PREFIX */ 973 if ((iflr->flags & IFLR_PREFIX) == 0) 974 break; 975 /* FALLTHROUGH */ 976 case SIOCALIFADDR: 977 case SIOCDLIFADDR: 978 /* address must be specified on ADD and DELETE */ 979 sa = (struct sockaddr *)&iflr->addr; 980 if (sa->sa_family != AF_INET6) 981 return EINVAL; 982 if (sa->sa_len != sizeof(struct sockaddr_in6)) 983 return EINVAL; 984 /* XXX need improvement */ 985 sa = (struct sockaddr *)&iflr->dstaddr; 986 if (sa->sa_family && sa->sa_family != AF_INET6) 987 return EINVAL; 988 if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6)) 989 return EINVAL; 990 break; 991 default: /* shouldn't happen */ 992 #if 0 993 panic("invalid cmd to in6_lifaddr_ioctl"); 994 /* NOTREACHED */ 995 #else 996 return EOPNOTSUPP; 997 #endif 998 } 999 if (sizeof(struct in6_addr) * 8 < iflr->prefixlen) 1000 return EINVAL; 1001 1002 switch (cmd) { 1003 case SIOCALIFADDR: 1004 { 1005 struct in6_aliasreq ifra; 1006 struct in6_addr *hostid = NULL; 1007 int prefixlen; 1008 1009 if ((iflr->flags & IFLR_PREFIX) != 0) { 1010 struct sockaddr_in6 *sin6; 1011 1012 /* 1013 * hostid is to fill in the hostid part of the 1014 * address. hostid points to the first link-local 1015 * address attached to the interface. 1016 */ 1017 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0); 1018 if (!ifa) 1019 return EADDRNOTAVAIL; 1020 hostid = IFA_IN6(ifa); 1021 1022 /* prefixlen must be <= 64. */ 1023 if (64 < iflr->prefixlen) 1024 return EINVAL; 1025 prefixlen = iflr->prefixlen; 1026 1027 /* hostid part must be zero. */ 1028 sin6 = (struct sockaddr_in6 *)&iflr->addr; 1029 if (sin6->sin6_addr.s6_addr32[2] != 0 1030 || sin6->sin6_addr.s6_addr32[3] != 0) { 1031 return EINVAL; 1032 } 1033 } else 1034 prefixlen = iflr->prefixlen; 1035 1036 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */ 1037 bzero(&ifra, sizeof(ifra)); 1038 bcopy(iflr->iflr_name, ifra.ifra_name, 1039 sizeof(ifra.ifra_name)); 1040 1041 bcopy(&iflr->addr, &ifra.ifra_addr, 1042 ((struct sockaddr *)&iflr->addr)->sa_len); 1043 if (hostid) { 1044 /* fill in hostid part */ 1045 ifra.ifra_addr.sin6_addr.s6_addr32[2] = 1046 hostid->s6_addr32[2]; 1047 ifra.ifra_addr.sin6_addr.s6_addr32[3] = 1048 hostid->s6_addr32[3]; 1049 } 1050 1051 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /* XXX */ 1052 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr, 1053 ((struct sockaddr *)&iflr->dstaddr)->sa_len); 1054 if (hostid) { 1055 ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] = 1056 hostid->s6_addr32[2]; 1057 ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] = 1058 hostid->s6_addr32[3]; 1059 } 1060 } 1061 1062 ifra.ifra_prefixmask.sin6_family = AF_INET6; 1063 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6); 1064 in6_len2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen); 1065 1066 ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX; 1067 return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, p); 1068 } 1069 case SIOCGLIFADDR: 1070 case SIOCDLIFADDR: 1071 { 1072 struct in6_ifaddr *ia; 1073 struct in6_addr mask, candidate, match; 1074 struct sockaddr_in6 *sin6; 1075 int cmp; 1076 1077 bzero(&mask, sizeof(mask)); 1078 if (iflr->flags & IFLR_PREFIX) { 1079 /* lookup a prefix rather than address. */ 1080 in6_len2mask(&mask, iflr->prefixlen); 1081 1082 sin6 = (struct sockaddr_in6 *)&iflr->addr; 1083 bcopy(&sin6->sin6_addr, &match, sizeof(match)); 1084 match.s6_addr32[0] &= mask.s6_addr32[0]; 1085 match.s6_addr32[1] &= mask.s6_addr32[1]; 1086 match.s6_addr32[2] &= mask.s6_addr32[2]; 1087 match.s6_addr32[3] &= mask.s6_addr32[3]; 1088 1089 /* if you set extra bits, that's wrong */ 1090 if (bcmp(&match, &sin6->sin6_addr, sizeof(match))) 1091 return EINVAL; 1092 1093 cmp = 1; 1094 } else { 1095 if (cmd == SIOCGLIFADDR) { 1096 /* on getting an address, take the 1st match */ 1097 cmp = 0; /* XXX */ 1098 } else { 1099 /* on deleting an address, do exact match */ 1100 in6_len2mask(&mask, 128); 1101 sin6 = (struct sockaddr_in6 *)&iflr->addr; 1102 bcopy(&sin6->sin6_addr, &match, sizeof(match)); 1103 1104 cmp = 1; 1105 } 1106 } 1107 1108 for (ifa = ifp->if_addrlist.tqh_first; 1109 ifa; 1110 ifa = ifa->ifa_list.tqe_next) 1111 { 1112 if (ifa->ifa_addr->sa_family != AF_INET6) 1113 continue; 1114 if (!cmp) 1115 break; 1116 bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate)); 1117 candidate.s6_addr32[0] &= mask.s6_addr32[0]; 1118 candidate.s6_addr32[1] &= mask.s6_addr32[1]; 1119 candidate.s6_addr32[2] &= mask.s6_addr32[2]; 1120 candidate.s6_addr32[3] &= mask.s6_addr32[3]; 1121 if (IN6_ARE_ADDR_EQUAL(&candidate, &match)) 1122 break; 1123 } 1124 if (!ifa) 1125 return EADDRNOTAVAIL; 1126 ia = ifa2ia6(ifa); 1127 1128 if (cmd == SIOCGLIFADDR) { 1129 /* fill in the if_laddrreq structure */ 1130 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len); 1131 1132 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 1133 bcopy(&ia->ia_dstaddr, &iflr->dstaddr, 1134 ia->ia_dstaddr.sin6_len); 1135 } else 1136 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr)); 1137 1138 iflr->prefixlen = 1139 in6_mask2len(&ia->ia_prefixmask.sin6_addr); 1140 1141 iflr->flags = ia->ia6_flags; /* XXX */ 1142 1143 return 0; 1144 } else { 1145 struct in6_aliasreq ifra; 1146 1147 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */ 1148 bzero(&ifra, sizeof(ifra)); 1149 bcopy(iflr->iflr_name, ifra.ifra_name, 1150 sizeof(ifra.ifra_name)); 1151 1152 bcopy(&ia->ia_addr, &ifra.ifra_addr, 1153 ia->ia_addr.sin6_len); 1154 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 1155 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr, 1156 ia->ia_dstaddr.sin6_len); 1157 } else { 1158 bzero(&ifra.ifra_dstaddr, 1159 sizeof(ifra.ifra_dstaddr)); 1160 } 1161 bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr, 1162 ia->ia_prefixmask.sin6_len); 1163 1164 ifra.ifra_flags = ia->ia6_flags; 1165 return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra, 1166 ifp, p); 1167 } 1168 } 1169 } 1170 1171 return EOPNOTSUPP; /* just for safety */ 1172 } 1173 1174 /* 1175 * Delete any existing route for an interface. 1176 */ 1177 void 1178 in6_ifscrub(ifp, ia) 1179 struct ifnet *ifp; 1180 struct in6_ifaddr *ia; 1181 { 1182 if ((ia->ia_flags & IFA_ROUTE) == 0) 1183 return; 1184 1185 /* 1186 * We should check the existence of dstaddr, because link-local 1187 * addresses can be configured without particular destinations 1188 * even on point-to-point or loopback interfaces. 1189 * In this case, kernel would panic in rtinit()... 1190 */ 1191 if (ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT) && 1192 (ia->ia_ifa.ifa_dstaddr != NULL)) 1193 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST); 1194 else 1195 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0); 1196 ia->ia_flags &= ~IFA_ROUTE; 1197 1198 /* Remove ownaddr's loopback rtentry, if it exists. */ 1199 in6_ifremloop(&(ia->ia_ifa)); 1200 } 1201 1202 /* 1203 * Initialize an interface's intetnet6 address 1204 * and routing table entry. 1205 */ 1206 static int 1207 in6_ifinit(ifp, ia, sin6, scrub) 1208 struct ifnet *ifp; 1209 struct in6_ifaddr *ia; 1210 struct sockaddr_in6 *sin6; 1211 int scrub; 1212 { 1213 struct sockaddr_in6 oldaddr; 1214 int error, flags = RTF_UP; 1215 int s = splnet(); 1216 1217 oldaddr = ia->ia_addr; 1218 ia->ia_addr = *sin6; 1219 /* 1220 * Give the interface a chance to initialize 1221 * if this is its first address, 1222 * and to validate the address if necessary. 1223 */ 1224 if (ifp->if_ioctl && 1225 (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) { 1226 splx(s); 1227 ia->ia_addr = oldaddr; 1228 return(error); 1229 } 1230 1231 switch (ifp->if_type) { 1232 case IFT_ARCNET: 1233 case IFT_ETHER: 1234 case IFT_FDDI: 1235 case IFT_IEEE1394: 1236 ia->ia_ifa.ifa_rtrequest = nd6_rtrequest; 1237 ia->ia_ifa.ifa_flags |= RTF_CLONING; 1238 break; 1239 case IFT_PPP: 1240 ia->ia_ifa.ifa_rtrequest = nd6_p2p_rtrequest; 1241 ia->ia_ifa.ifa_flags |= RTF_CLONING; 1242 break; 1243 } 1244 1245 splx(s); 1246 if (scrub) { 1247 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr; 1248 in6_ifscrub(ifp, ia); 1249 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr; 1250 } 1251 /* xxx 1252 * in_socktrim 1253 */ 1254 /* 1255 * Add route for the network. 1256 */ 1257 ia->ia_ifa.ifa_metric = ifp->if_metric; 1258 if (ifp->if_flags & IFF_LOOPBACK) { 1259 ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr; 1260 flags |= RTF_HOST; 1261 } else if (ifp->if_flags & IFF_POINTOPOINT) { 1262 if (ia->ia_dstaddr.sin6_family != AF_INET6) 1263 return(0); 1264 flags |= RTF_HOST; 1265 } 1266 if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD, flags)) == 0) 1267 ia->ia_flags |= IFA_ROUTE; 1268 /* XXX check if the subnet route points to the same interface */ 1269 if (error == EEXIST) 1270 error = 0; 1271 1272 /* Add ownaddr as loopback rtentry, if necessary(ex. on p2p link). */ 1273 in6_ifaddloop(&(ia->ia_ifa)); 1274 1275 if (ifp->if_flags & IFF_MULTICAST) 1276 in6_restoremkludge(ia, ifp); 1277 1278 return(error); 1279 } 1280 1281 /* 1282 * Multicast address kludge: 1283 * If there were any multicast addresses attached to this interface address, 1284 * either move them to another address on this interface, or save them until 1285 * such time as this interface is reconfigured for IPv6. 1286 */ 1287 void 1288 in6_savemkludge(oia) 1289 struct in6_ifaddr *oia; 1290 { 1291 struct in6_ifaddr *ia; 1292 struct in6_multi *in6m, *next; 1293 1294 IFP_TO_IA6(oia->ia_ifp, ia); 1295 if (ia) { /* there is another address */ 1296 for (in6m = oia->ia6_multiaddrs.lh_first; in6m; in6m = next){ 1297 next = in6m->in6m_entry.le_next; 1298 IFAFREE(&in6m->in6m_ia->ia_ifa); 1299 IFAREF(&ia->ia_ifa); 1300 in6m->in6m_ia = ia; 1301 LIST_INSERT_HEAD(&ia->ia6_multiaddrs, in6m, in6m_entry); 1302 } 1303 } else { /* last address on this if deleted, save */ 1304 struct multi6_kludge *mk; 1305 1306 for (mk = in6_mk.lh_first; mk; mk = mk->mk_entry.le_next) { 1307 if (mk->mk_ifp == oia->ia_ifp) 1308 break; 1309 } 1310 if (mk == NULL) /* this should not happen! */ 1311 panic("in6_savemkludge: no kludge space"); 1312 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); /* release reference */ 1316 in6m->in6m_ia = NULL; 1317 LIST_INSERT_HEAD(&mk->mk_head, in6m, in6m_entry); 1318 } 1319 } 1320 } 1321 1322 /* 1323 * Continuation of multicast address hack: 1324 * If there was a multicast group list previously saved for this interface, 1325 * then we re-attach it to the first address configured on the i/f. 1326 */ 1327 void 1328 in6_restoremkludge(ia, ifp) 1329 struct in6_ifaddr *ia; 1330 struct ifnet *ifp; 1331 { 1332 struct multi6_kludge *mk; 1333 1334 for (mk = in6_mk.lh_first; mk; mk = mk->mk_entry.le_next) { 1335 if (mk->mk_ifp == ifp) { 1336 struct in6_multi *in6m, *next; 1337 1338 for (in6m = mk->mk_head.lh_first; in6m; in6m = next) { 1339 next = in6m->in6m_entry.le_next; 1340 in6m->in6m_ia = ia; 1341 IFAREF(&ia->ia_ifa); 1342 LIST_INSERT_HEAD(&ia->ia6_multiaddrs, 1343 in6m, in6m_entry); 1344 } 1345 LIST_INIT(&mk->mk_head); 1346 break; 1347 } 1348 } 1349 } 1350 1351 /* 1352 * Allocate space for the kludge at interface initialization time. 1353 * Formerly, we dynamically allocated the space in in6_savemkludge() with 1354 * malloc(M_WAITOK). However, it was wrong since the function could be called 1355 * under an interrupt context (software timer on address lifetime expiration). 1356 * Also, we cannot just give up allocating the strucutre, since the group 1357 * membership structure is very complex and we need to keep it anyway. 1358 * Of course, this function MUST NOT be called under an interrupt context. 1359 * Specifically, it is expected to be called only from in6_ifattach(), though 1360 * it is a global function. 1361 */ 1362 void 1363 in6_createmkludge(ifp) 1364 struct ifnet *ifp; 1365 { 1366 struct multi6_kludge *mk; 1367 1368 for (mk = in6_mk.lh_first; mk; mk = mk->mk_entry.le_next) { 1369 /* If we've already had one, do not allocate. */ 1370 if (mk->mk_ifp == ifp) 1371 return; 1372 } 1373 1374 mk = malloc(sizeof(*mk), M_IPMADDR, M_WAITOK); 1375 1376 bzero(mk, sizeof(*mk)); 1377 LIST_INIT(&mk->mk_head); 1378 mk->mk_ifp = ifp; 1379 LIST_INSERT_HEAD(&in6_mk, mk, mk_entry); 1380 } 1381 1382 void 1383 in6_purgemkludge(ifp) 1384 struct ifnet *ifp; 1385 { 1386 struct multi6_kludge *mk; 1387 struct in6_multi *in6m; 1388 1389 for (mk = in6_mk.lh_first; mk; mk = mk->mk_entry.le_next) { 1390 if (mk->mk_ifp != ifp) 1391 continue; 1392 1393 /* leave from all multicast groups joined */ 1394 while ((in6m = LIST_FIRST(&mk->mk_head)) != NULL) 1395 in6_delmulti(in6m); 1396 LIST_REMOVE(mk, mk_entry); 1397 free(mk, M_IPMADDR); 1398 break; 1399 } 1400 } 1401 1402 /* 1403 * Add an address to the list of IP6 multicast addresses for a 1404 * given interface. 1405 */ 1406 struct in6_multi * 1407 in6_addmulti(maddr6, ifp, errorp) 1408 struct in6_addr *maddr6; 1409 struct ifnet *ifp; 1410 int *errorp; 1411 { 1412 struct in6_ifaddr *ia; 1413 struct in6_ifreq ifr; 1414 struct in6_multi *in6m; 1415 int s = splsoftnet(); 1416 1417 *errorp = 0; 1418 /* 1419 * See if address already in list. 1420 */ 1421 IN6_LOOKUP_MULTI(*maddr6, ifp, in6m); 1422 if (in6m != NULL) { 1423 /* 1424 * Found it; just increment the refrence count. 1425 */ 1426 in6m->in6m_refcount++; 1427 } else { 1428 /* 1429 * New address; allocate a new multicast record 1430 * and link it into the interface's multicast list. 1431 */ 1432 in6m = (struct in6_multi *) 1433 malloc(sizeof(*in6m), M_IPMADDR, M_NOWAIT); 1434 if (in6m == NULL) { 1435 splx(s); 1436 *errorp = ENOBUFS; 1437 return(NULL); 1438 } 1439 in6m->in6m_addr = *maddr6; 1440 in6m->in6m_ifp = ifp; 1441 in6m->in6m_refcount = 1; 1442 IFP_TO_IA6(ifp, ia); 1443 if (ia == NULL) { 1444 free(in6m, M_IPMADDR); 1445 splx(s); 1446 *errorp = EADDRNOTAVAIL; /* appropriate? */ 1447 return(NULL); 1448 } 1449 in6m->in6m_ia = ia; 1450 IFAREF(&ia->ia_ifa); /* gain a reference */ 1451 LIST_INSERT_HEAD(&ia->ia6_multiaddrs, in6m, in6m_entry); 1452 1453 /* 1454 * Ask the network driver to update its multicast reception 1455 * filter appropriately for the new address. 1456 */ 1457 bzero(&ifr.ifr_addr, sizeof(struct sockaddr_in6)); 1458 ifr.ifr_addr.sin6_len = sizeof(struct sockaddr_in6); 1459 ifr.ifr_addr.sin6_family = AF_INET6; 1460 ifr.ifr_addr.sin6_addr = *maddr6; 1461 if (ifp->if_ioctl == NULL) 1462 *errorp = ENXIO; /* XXX: appropriate? */ 1463 else 1464 *errorp = (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 1465 (caddr_t)&ifr); 1466 if (*errorp) { 1467 LIST_REMOVE(in6m, in6m_entry); 1468 free(in6m, M_IPMADDR); 1469 IFAFREE(&ia->ia_ifa); 1470 splx(s); 1471 return(NULL); 1472 } 1473 /* 1474 * Let MLD6 know that we have joined a new IP6 multicast 1475 * group. 1476 */ 1477 mld6_start_listening(in6m); 1478 } 1479 splx(s); 1480 return(in6m); 1481 } 1482 1483 /* 1484 * Delete a multicast address record. 1485 */ 1486 void 1487 in6_delmulti(in6m) 1488 struct in6_multi *in6m; 1489 { 1490 struct in6_ifreq ifr; 1491 int s = splsoftnet(); 1492 1493 if (--in6m->in6m_refcount == 0) { 1494 /* 1495 * No remaining claims to this record; let MLD6 know 1496 * that we are leaving the multicast group. 1497 */ 1498 mld6_stop_listening(in6m); 1499 1500 /* 1501 * Unlink from list. 1502 */ 1503 LIST_REMOVE(in6m, in6m_entry); 1504 if (in6m->in6m_ia) { 1505 IFAFREE(&in6m->in6m_ia->ia_ifa); /* release reference */ 1506 } 1507 1508 /* 1509 * Notify the network driver to update its multicast 1510 * reception filter. 1511 */ 1512 bzero(&ifr.ifr_addr, sizeof(struct sockaddr_in6)); 1513 ifr.ifr_addr.sin6_len = sizeof(struct sockaddr_in6); 1514 ifr.ifr_addr.sin6_family = AF_INET6; 1515 ifr.ifr_addr.sin6_addr = in6m->in6m_addr; 1516 (*in6m->in6m_ifp->if_ioctl)(in6m->in6m_ifp, 1517 SIOCDELMULTI, (caddr_t)&ifr); 1518 free(in6m, M_IPMADDR); 1519 } 1520 splx(s); 1521 } 1522 1523 struct in6_multi_mship * 1524 in6_joingroup(ifp, addr, errorp) 1525 struct ifnet *ifp; 1526 struct in6_addr *addr; 1527 int *errorp; 1528 { 1529 struct in6_multi_mship *imm; 1530 1531 imm = malloc(sizeof(*imm), M_IPMADDR, M_NOWAIT); 1532 if (!imm) { 1533 *errorp = ENOBUFS; 1534 return NULL; 1535 } 1536 imm->i6mm_maddr = in6_addmulti(addr, ifp, errorp); 1537 if (!imm->i6mm_maddr) { 1538 /* *errorp is alrady set */ 1539 free(imm, M_IPMADDR); 1540 return NULL; 1541 } 1542 return imm; 1543 } 1544 1545 int 1546 in6_leavegroup(imm) 1547 struct in6_multi_mship *imm; 1548 { 1549 1550 if (imm->i6mm_maddr) 1551 in6_delmulti(imm->i6mm_maddr); 1552 free(imm, M_IPMADDR); 1553 return 0; 1554 } 1555 1556 /* 1557 * Find an IPv6 interface link-local address specific to an interface. 1558 */ 1559 struct in6_ifaddr * 1560 in6ifa_ifpforlinklocal(ifp, ignoreflags) 1561 struct ifnet *ifp; 1562 int ignoreflags; 1563 { 1564 struct ifaddr *ifa; 1565 1566 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next) 1567 { 1568 if (ifa->ifa_addr == NULL) 1569 continue; /* just for safety */ 1570 if (ifa->ifa_addr->sa_family != AF_INET6) 1571 continue; 1572 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) { 1573 if ((((struct in6_ifaddr *)ifa)->ia6_flags & 1574 ignoreflags) != 0) 1575 continue; 1576 break; 1577 } 1578 } 1579 1580 return((struct in6_ifaddr *)ifa); 1581 } 1582 1583 1584 /* 1585 * find the internet address corresponding to a given interface and address. 1586 */ 1587 struct in6_ifaddr * 1588 in6ifa_ifpwithaddr(ifp, addr) 1589 struct ifnet *ifp; 1590 struct in6_addr *addr; 1591 { 1592 struct ifaddr *ifa; 1593 1594 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next) 1595 { 1596 if (ifa->ifa_addr == NULL) 1597 continue; /* just for safety */ 1598 if (ifa->ifa_addr->sa_family != AF_INET6) 1599 continue; 1600 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa))) 1601 break; 1602 } 1603 1604 return((struct in6_ifaddr *)ifa); 1605 } 1606 1607 /* 1608 * Convert IP6 address to printable (loggable) representation. 1609 */ 1610 static char digits[] = "0123456789abcdef"; 1611 static int ip6round = 0; 1612 char * 1613 ip6_sprintf(addr) 1614 const struct in6_addr *addr; 1615 { 1616 static char ip6buf[8][48]; 1617 int i; 1618 char *cp; 1619 const u_short *a = (u_short *)addr; 1620 const u_char *d; 1621 int dcolon = 0; 1622 1623 ip6round = (ip6round + 1) & 7; 1624 cp = ip6buf[ip6round]; 1625 1626 for (i = 0; i < 8; i++) { 1627 if (dcolon == 1) { 1628 if (*a == 0) { 1629 if (i == 7) 1630 *cp++ = ':'; 1631 a++; 1632 continue; 1633 } else 1634 dcolon = 2; 1635 } 1636 if (*a == 0) { 1637 if (dcolon == 0 && *(a + 1) == 0) { 1638 if (i == 0) 1639 *cp++ = ':'; 1640 *cp++ = ':'; 1641 dcolon = 1; 1642 } else { 1643 *cp++ = '0'; 1644 *cp++ = ':'; 1645 } 1646 a++; 1647 continue; 1648 } 1649 d = (const u_char *)a; 1650 *cp++ = digits[*d >> 4]; 1651 *cp++ = digits[*d++ & 0xf]; 1652 *cp++ = digits[*d >> 4]; 1653 *cp++ = digits[*d & 0xf]; 1654 *cp++ = ':'; 1655 a++; 1656 } 1657 *--cp = 0; 1658 return(ip6buf[ip6round]); 1659 } 1660 1661 int 1662 in6_localaddr(in6) 1663 struct in6_addr *in6; 1664 { 1665 struct in6_ifaddr *ia; 1666 1667 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6)) 1668 return 1; 1669 1670 for (ia = in6_ifaddr; ia; ia = ia->ia_next) 1671 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr, 1672 &ia->ia_prefixmask.sin6_addr)) 1673 return 1; 1674 1675 return (0); 1676 } 1677 1678 /* 1679 * Get a scope of the address. Node-local, link-local, site-local or global. 1680 */ 1681 int 1682 in6_addrscope (addr) 1683 struct in6_addr *addr; 1684 { 1685 int scope; 1686 1687 if (addr->s6_addr8[0] == 0xfe) { 1688 scope = addr->s6_addr8[1] & 0xc0; 1689 1690 switch (scope) { 1691 case 0x80: 1692 return IPV6_ADDR_SCOPE_LINKLOCAL; 1693 break; 1694 case 0xc0: 1695 return IPV6_ADDR_SCOPE_SITELOCAL; 1696 break; 1697 default: 1698 return IPV6_ADDR_SCOPE_GLOBAL; /* just in case */ 1699 break; 1700 } 1701 } 1702 1703 1704 if (addr->s6_addr8[0] == 0xff) { 1705 scope = addr->s6_addr8[1] & 0x0f; 1706 1707 /* 1708 * due to other scope such as reserved, 1709 * return scope doesn't work. 1710 */ 1711 switch (scope) { 1712 case IPV6_ADDR_SCOPE_NODELOCAL: 1713 return IPV6_ADDR_SCOPE_NODELOCAL; 1714 break; 1715 case IPV6_ADDR_SCOPE_LINKLOCAL: 1716 return IPV6_ADDR_SCOPE_LINKLOCAL; 1717 break; 1718 case IPV6_ADDR_SCOPE_SITELOCAL: 1719 return IPV6_ADDR_SCOPE_SITELOCAL; 1720 break; 1721 default: 1722 return IPV6_ADDR_SCOPE_GLOBAL; 1723 break; 1724 } 1725 } 1726 1727 if (bcmp(&in6addr_loopback, addr, sizeof(*addr) - 1) == 0) { 1728 if (addr->s6_addr8[15] == 1) /* loopback */ 1729 return IPV6_ADDR_SCOPE_NODELOCAL; 1730 if (addr->s6_addr8[15] == 0) /* unspecified */ 1731 return IPV6_ADDR_SCOPE_LINKLOCAL; 1732 } 1733 1734 return IPV6_ADDR_SCOPE_GLOBAL; 1735 } 1736 1737 int 1738 in6_addr2scopeid(ifp, addr) 1739 struct ifnet *ifp; /* must not be NULL */ 1740 struct in6_addr *addr; /* must not be NULL */ 1741 { 1742 int scope = in6_addrscope(addr); 1743 1744 switch (scope) { 1745 case IPV6_ADDR_SCOPE_NODELOCAL: 1746 return(-1); /* XXX: is this an appropriate value? */ 1747 1748 case IPV6_ADDR_SCOPE_LINKLOCAL: 1749 /* XXX: we do not distinguish between a link and an I/F. */ 1750 return(ifp->if_index); 1751 1752 case IPV6_ADDR_SCOPE_SITELOCAL: 1753 return(0); /* XXX: invalid. */ 1754 1755 default: 1756 return(0); /* XXX: treat as global. */ 1757 } 1758 } 1759 1760 /* 1761 * return length of part which dst and src are equal 1762 * hard coding... 1763 */ 1764 int 1765 in6_matchlen(src, dst) 1766 struct in6_addr *src, *dst; 1767 { 1768 int match = 0; 1769 u_char *s = (u_char *)src, *d = (u_char *)dst; 1770 u_char *lim = s + 16, r; 1771 1772 while (s < lim) 1773 if ((r = (*d++ ^ *s++)) != 0) { 1774 while (r < 128) { 1775 match++; 1776 r <<= 1; 1777 } 1778 break; 1779 } else 1780 match += 8; 1781 return match; 1782 } 1783 1784 /* XXX: to be scope conscious */ 1785 int 1786 in6_are_prefix_equal(p1, p2, len) 1787 struct in6_addr *p1, *p2; 1788 int len; 1789 { 1790 int bytelen, bitlen; 1791 1792 /* sanity check */ 1793 if (0 > len || len > 128) { 1794 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n", 1795 len); 1796 return(0); 1797 } 1798 1799 bytelen = len / 8; 1800 bitlen = len % 8; 1801 1802 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen)) 1803 return(0); 1804 if (p1->s6_addr[bytelen] >> (8 - bitlen) != 1805 p2->s6_addr[bytelen] >> (8 - bitlen)) 1806 return(0); 1807 1808 return(1); 1809 } 1810 1811 void 1812 in6_prefixlen2mask(maskp, len) 1813 struct in6_addr *maskp; 1814 int len; 1815 { 1816 u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff}; 1817 int bytelen, bitlen, i; 1818 1819 /* sanity check */ 1820 if (0 > len || len > 128) { 1821 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n", 1822 len); 1823 return; 1824 } 1825 1826 bzero(maskp, sizeof(*maskp)); 1827 bytelen = len / 8; 1828 bitlen = len % 8; 1829 for (i = 0; i < bytelen; i++) 1830 maskp->s6_addr[i] = 0xff; 1831 if (bitlen) 1832 maskp->s6_addr[bytelen] = maskarray[bitlen - 1]; 1833 } 1834 1835 /* 1836 * return the best address out of the same scope 1837 */ 1838 struct in6_ifaddr * 1839 in6_ifawithscope(oifp, dst) 1840 struct ifnet *oifp; 1841 struct in6_addr *dst; 1842 { 1843 int dst_scope = in6_addrscope(dst), src_scope, best_scope = 0; 1844 int blen = -1; 1845 struct ifaddr *ifa; 1846 struct ifnet *ifp; 1847 struct in6_ifaddr *ifa_best = NULL; 1848 1849 if (oifp == NULL) { 1850 printf("in6_ifawithscope: output interface is not specified\n"); 1851 return(NULL); 1852 } 1853 1854 /* 1855 * We search for all addresses on all interfaces from the beginning. 1856 * Comparing an interface with the outgoing interface will be done 1857 * only at the final stage of tiebreaking. 1858 */ 1859 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) 1860 { 1861 /* 1862 * We can never take an address that breaks the scope zone 1863 * of the destination. 1864 */ 1865 if (in6_addr2scopeid(ifp, dst) != in6_addr2scopeid(oifp, dst)) 1866 continue; 1867 1868 for (ifa = ifp->if_addrlist.tqh_first; ifa; 1869 ifa = ifa->ifa_list.tqe_next) 1870 { 1871 int tlen = -1, dscopecmp, bscopecmp, matchcmp; 1872 1873 if (ifa->ifa_addr->sa_family != AF_INET6) 1874 continue; 1875 1876 src_scope = in6_addrscope(IFA_IN6(ifa)); 1877 1878 #ifdef ADDRSELECT_DEBUG /* should be removed after stabilization */ 1879 dscopecmp = IN6_ARE_SCOPE_CMP(src_scope, dst_scope); 1880 printf("in6_ifawithscope: dst=%s bestaddr=%s, " 1881 "newaddr=%s, scope=%x, dcmp=%d, bcmp=%d, " 1882 "matchlen=%d, flgs=%x\n", 1883 ip6_sprintf(dst), 1884 ifa_best ? ip6_sprintf(&ifa_best->ia_addr.sin6_addr) : "none", 1885 ip6_sprintf(IFA_IN6(ifa)), src_scope, 1886 dscopecmp, 1887 ifa_best ? IN6_ARE_SCOPE_CMP(src_scope, best_scope) : -1, 1888 in6_matchlen(IFA_IN6(ifa), dst), 1889 ((struct in6_ifaddr *)ifa)->ia6_flags); 1890 #endif 1891 1892 /* 1893 * Don't use an address before completing DAD 1894 * nor a duplicated address. 1895 */ 1896 if (((struct in6_ifaddr *)ifa)->ia6_flags & 1897 IN6_IFF_NOTREADY) 1898 continue; 1899 1900 /* XXX: is there any case to allow anycasts? */ 1901 if (((struct in6_ifaddr *)ifa)->ia6_flags & 1902 IN6_IFF_ANYCAST) 1903 continue; 1904 1905 if (((struct in6_ifaddr *)ifa)->ia6_flags & 1906 IN6_IFF_DETACHED) 1907 continue; 1908 1909 /* 1910 * If this is the first address we find, 1911 * keep it anyway. 1912 */ 1913 if (ifa_best == NULL) 1914 goto replace; 1915 1916 /* 1917 * ifa_best is never NULL beyond this line except 1918 * within the block labeled "replace". 1919 */ 1920 1921 /* 1922 * If ifa_best has a smaller scope than dst and 1923 * the current address has a larger one than 1924 * (or equal to) dst, always replace ifa_best. 1925 * Also, if the current address has a smaller scope 1926 * than dst, ignore it unless ifa_best also has a 1927 * smaller scope. 1928 */ 1929 if (IN6_ARE_SCOPE_CMP(best_scope, dst_scope) < 0 && 1930 IN6_ARE_SCOPE_CMP(src_scope, dst_scope) >= 0) 1931 goto replace; 1932 if (IN6_ARE_SCOPE_CMP(src_scope, dst_scope) < 0 && 1933 IN6_ARE_SCOPE_CMP(best_scope, dst_scope) >= 0) 1934 continue; 1935 1936 /* 1937 * A deprecated address SHOULD NOT be used in new 1938 * communications if an alternate (non-deprecated) 1939 * address is available and has sufficient scope. 1940 * RFC 2462, Section 5.5.4. 1941 */ 1942 if (((struct in6_ifaddr *)ifa)->ia6_flags & 1943 IN6_IFF_DEPRECATED) { 1944 /* 1945 * Ignore any deprecated addresses if 1946 * specified by configuration. 1947 */ 1948 if (!ip6_use_deprecated) 1949 continue; 1950 1951 /* 1952 * If we have already found a non-deprecated 1953 * candidate, just ignore deprecated addresses. 1954 */ 1955 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) 1956 == 0) 1957 continue; 1958 } 1959 1960 /* 1961 * A non-deprecated address is always preferred 1962 * to a deprecated one regardless of scopes and 1963 * address matching. 1964 */ 1965 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) && 1966 (((struct in6_ifaddr *)ifa)->ia6_flags & 1967 IN6_IFF_DEPRECATED) == 0) 1968 goto replace; 1969 1970 /* 1971 * At this point, we have two cases: 1972 * 1. we are looking at a non-deprecated address, 1973 * and ifa_best is also non-deprecated. 1974 * 2. we are looking at a deprecated address, 1975 * and ifa_best is also deprecated. 1976 * Also, we do not have to consider a case where 1977 * the scope of if_best is larger(smaller) than dst and 1978 * the scope of the current address is smaller(larger) 1979 * than dst. Such a case has already been covered. 1980 * Tiebreaking is done according to the following 1981 * items: 1982 * - the scope comparison between the address and 1983 * dst (dscopecmp) 1984 * - the scope comparison between the address and 1985 * ifa_best (bscopecmp) 1986 * - if the address match dst longer than ifa_best 1987 * (matchcmp) 1988 * - if the address is on the outgoing I/F (outI/F) 1989 * 1990 * Roughly speaking, the selection policy is 1991 * - the most important item is scope. The same scope 1992 * is best. Then search for a larger scope. 1993 * Smaller scopes are the last resort. 1994 * - A deprecated address is chosen only when we have 1995 * no address that has an enough scope, but is 1996 * prefered to any addresses of smaller scopes. 1997 * - Longest address match against dst is considered 1998 * only for addresses that has the same scope of dst. 1999 * - If there is no other reasons to choose one, 2000 * addresses on the outgoing I/F are preferred. 2001 * 2002 * The precise decision table is as follows: 2003 * dscopecmp bscopecmp matchcmp outI/F | replace? 2004 * !equal equal N/A Yes | Yes (1) 2005 * !equal equal N/A No | No (2) 2006 * larger larger N/A N/A | No (3) 2007 * larger smaller N/A N/A | Yes (4) 2008 * smaller larger N/A N/A | Yes (5) 2009 * smaller smaller N/A N/A | No (6) 2010 * equal smaller N/A N/A | Yes (7) 2011 * equal larger (already done) 2012 * equal equal larger N/A | Yes (8) 2013 * equal equal smaller N/A | No (9) 2014 * equal equal equal Yes | Yes (a) 2015 * eaual eqaul equal No | No (b) 2016 */ 2017 dscopecmp = IN6_ARE_SCOPE_CMP(src_scope, dst_scope); 2018 bscopecmp = IN6_ARE_SCOPE_CMP(src_scope, best_scope); 2019 2020 if (dscopecmp && bscopecmp == 0) { 2021 if (oifp == ifp) /* (1) */ 2022 goto replace; 2023 continue; /* (2) */ 2024 } 2025 if (dscopecmp > 0) { 2026 if (bscopecmp > 0) /* (3) */ 2027 continue; 2028 goto replace; /* (4) */ 2029 } 2030 if (dscopecmp < 0) { 2031 if (bscopecmp > 0) /* (5) */ 2032 goto replace; 2033 continue; /* (6) */ 2034 } 2035 2036 /* now dscopecmp must be 0 */ 2037 if (bscopecmp < 0) 2038 goto replace; /* (7) */ 2039 2040 /* 2041 * At last both dscopecmp and bscopecmp must be 0. 2042 * We need address matching against dst for 2043 * tiebreaking. 2044 */ 2045 tlen = in6_matchlen(IFA_IN6(ifa), dst); 2046 matchcmp = tlen - blen; 2047 if (matchcmp > 0) /* (8) */ 2048 goto replace; 2049 if (matchcmp < 0) /* (9) */ 2050 continue; 2051 if (oifp == ifp) /* (a) */ 2052 goto replace; 2053 continue; /* (b) */ 2054 2055 replace: 2056 ifa_best = (struct in6_ifaddr *)ifa; 2057 blen = tlen >= 0 ? tlen : 2058 in6_matchlen(IFA_IN6(ifa), dst); 2059 best_scope = in6_addrscope(&ifa_best->ia_addr.sin6_addr); 2060 } 2061 } 2062 2063 /* count statistics for future improvements */ 2064 if (ifa_best == NULL) 2065 ip6stat.ip6s_sources_none++; 2066 else { 2067 if (oifp == ifa_best->ia_ifp) 2068 ip6stat.ip6s_sources_sameif[best_scope]++; 2069 else 2070 ip6stat.ip6s_sources_otherif[best_scope]++; 2071 2072 if (best_scope == dst_scope) 2073 ip6stat.ip6s_sources_samescope[best_scope]++; 2074 else 2075 ip6stat.ip6s_sources_otherscope[best_scope]++; 2076 2077 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) != 0) 2078 ip6stat.ip6s_sources_deprecated[best_scope]++; 2079 } 2080 2081 return(ifa_best); 2082 } 2083 2084 /* 2085 * return the best address out of the same scope. if no address was 2086 * found, return the first valid address from designated IF. 2087 */ 2088 struct in6_ifaddr * 2089 in6_ifawithifp(ifp, dst) 2090 struct ifnet *ifp; 2091 struct in6_addr *dst; 2092 { 2093 int dst_scope = in6_addrscope(dst), blen = -1, tlen; 2094 struct ifaddr *ifa; 2095 struct in6_ifaddr *besta = 0; 2096 struct in6_ifaddr *dep[2]; /* last-resort: deprecated */ 2097 2098 dep[0] = dep[1] = NULL; 2099 2100 /* 2101 * We first look for addresses in the same scope. 2102 * If there is one, return it. 2103 * If two or more, return one which matches the dst longest. 2104 * If none, return one of global addresses assigned other ifs. 2105 */ 2106 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next) 2107 { 2108 if (ifa->ifa_addr->sa_family != AF_INET6) 2109 continue; 2110 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST) 2111 continue; /* XXX: is there any case to allow anycast? */ 2112 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY) 2113 continue; /* don't use this interface */ 2114 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED) 2115 continue; 2116 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) { 2117 if (ip6_use_deprecated) 2118 dep[0] = (struct in6_ifaddr *)ifa; 2119 continue; 2120 } 2121 2122 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) { 2123 /* 2124 * call in6_matchlen() as few as possible 2125 */ 2126 if (besta) { 2127 if (blen == -1) 2128 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst); 2129 tlen = in6_matchlen(IFA_IN6(ifa), dst); 2130 if (tlen > blen) { 2131 blen = tlen; 2132 besta = (struct in6_ifaddr *)ifa; 2133 } 2134 } else 2135 besta = (struct in6_ifaddr *)ifa; 2136 } 2137 } 2138 if (besta) 2139 return(besta); 2140 2141 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next) 2142 { 2143 if (ifa->ifa_addr->sa_family != AF_INET6) 2144 continue; 2145 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST) 2146 continue; /* XXX: is there any case to allow anycast? */ 2147 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY) 2148 continue; /* don't use this interface */ 2149 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED) 2150 continue; 2151 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) { 2152 if (ip6_use_deprecated) 2153 dep[1] = (struct in6_ifaddr *)ifa; 2154 continue; 2155 } 2156 2157 return (struct in6_ifaddr *)ifa; 2158 } 2159 2160 /* use the last-resort values, that are, deprecated addresses */ 2161 if (dep[0]) 2162 return dep[0]; 2163 if (dep[1]) 2164 return dep[1]; 2165 2166 return NULL; 2167 } 2168 2169 /* 2170 * perform DAD when interface becomes IFF_UP. 2171 */ 2172 void 2173 in6_if_up(ifp) 2174 struct ifnet *ifp; 2175 { 2176 struct ifaddr *ifa; 2177 struct in6_ifaddr *ia; 2178 int dad_delay; /* delay ticks before DAD output */ 2179 2180 /* 2181 * special cases, like 6to4, are handled in in6_ifattach 2182 */ 2183 in6_ifattach(ifp, NULL); 2184 2185 dad_delay = 0; 2186 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next) 2187 { 2188 if (ifa->ifa_addr->sa_family != AF_INET6) 2189 continue; 2190 ia = (struct in6_ifaddr *)ifa; 2191 if (ia->ia6_flags & IN6_IFF_TENTATIVE) 2192 nd6_dad_start(ifa, &dad_delay); 2193 } 2194 } 2195 2196 int 2197 in6if_do_dad(ifp) 2198 struct ifnet *ifp; 2199 { 2200 if ((ifp->if_flags & IFF_LOOPBACK) != 0) 2201 return(0); 2202 2203 switch (ifp->if_type) { 2204 case IFT_FAITH: 2205 /* 2206 * These interfaces do not have the IFF_LOOPBACK flag, 2207 * but loop packets back. We do not have to do DAD on such 2208 * interfaces. We should even omit it, because loop-backed 2209 * NS would confuse the DAD procedure. 2210 */ 2211 return(0); 2212 default: 2213 /* 2214 * Our DAD routine requires the interface up and running. 2215 * However, some interfaces can be up before the RUNNING 2216 * status. Additionaly, users may try to assign addresses 2217 * before the interface becomes up (or running). 2218 * We simply skip DAD in such a case as a work around. 2219 * XXX: we should rather mark "tentative" on such addresses, 2220 * and do DAD after the interface becomes ready. 2221 */ 2222 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != 2223 (IFF_UP|IFF_RUNNING)) 2224 return(0); 2225 2226 return(1); 2227 } 2228 } 2229 2230 /* 2231 * Calculate max IPv6 MTU through all the interfaces and store it 2232 * to in6_maxmtu. 2233 */ 2234 void 2235 in6_setmaxmtu() 2236 { 2237 unsigned long maxmtu = 0; 2238 struct ifnet *ifp; 2239 2240 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) 2241 { 2242 if ((ifp->if_flags & IFF_LOOPBACK) == 0 && 2243 nd_ifinfo[ifp->if_index].linkmtu > maxmtu) 2244 maxmtu = nd_ifinfo[ifp->if_index].linkmtu; 2245 } 2246 if (maxmtu) /* update only when maxmtu is positive */ 2247 in6_maxmtu = maxmtu; 2248 } 2249