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