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