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_IP6MADDR, "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 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 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 error, 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 int error = 0; 416 417 if (ifra->ifra_addr.sin6_scope_id != 0) 418 error = sa6_embedscope(&ifra->ifra_addr, 0); 419 else 420 error = in6_setscope(&ifra->ifra_addr.sin6_addr, 421 ifp, NULL); 422 if (error != 0) 423 return (error); 424 ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr); 425 } 426 427 switch (cmd) { 428 case SIOCSIFADDR_IN6: 429 case SIOCSIFDSTADDR_IN6: 430 case SIOCSIFNETMASK_IN6: 431 /* 432 * Since IPv6 allows a node to assign multiple addresses 433 * on a single interface, SIOCSIFxxx ioctls are not suitable 434 * and should be unused. 435 */ 436 /* we decided to obsolete this command (20000704) */ 437 return (EINVAL); 438 439 case SIOCDIFADDR_IN6: 440 /* 441 * for IPv4, we look for existing in_ifaddr here to allow 442 * "ifconfig if0 delete" to remove first IPv4 address on the 443 * interface. For IPv6, as the spec allow multiple interface 444 * address from the day one, we consider "remove the first one" 445 * semantics to be not preferable. 446 */ 447 if (ia == NULL) 448 return (EADDRNOTAVAIL); 449 /* FALLTHROUGH */ 450 case SIOCAIFADDR_IN6: 451 /* 452 * We always require users to specify a valid IPv6 address for 453 * the corresponding operation. 454 */ 455 if (ifra->ifra_addr.sin6_family != AF_INET6 || 456 ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6)) 457 return (EAFNOSUPPORT); 458 if (!privileged) 459 return (EPERM); 460 461 break; 462 463 case SIOCGIFADDR_IN6: 464 /* This interface is basically deprecated. use SIOCGIFCONF. */ 465 /* FALLTHROUGH */ 466 case SIOCGIFAFLAG_IN6: 467 case SIOCGIFNETMASK_IN6: 468 case SIOCGIFDSTADDR_IN6: 469 case SIOCGIFALIFETIME_IN6: 470 /* must think again about its semantics */ 471 if (ia == NULL) 472 return (EADDRNOTAVAIL); 473 break; 474 case SIOCSIFALIFETIME_IN6: 475 { 476 struct in6_addrlifetime *lt; 477 478 if (!privileged) 479 return (EPERM); 480 if (ia == NULL) 481 return (EADDRNOTAVAIL); 482 /* sanity for overflow - beware unsigned */ 483 lt = &ifr->ifr_ifru.ifru_lifetime; 484 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME && 485 lt->ia6t_vltime + time_second < time_second) { 486 return EINVAL; 487 } 488 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME && 489 lt->ia6t_pltime + time_second < time_second) { 490 return EINVAL; 491 } 492 break; 493 } 494 } 495 496 switch (cmd) { 497 498 case SIOCGIFADDR_IN6: 499 ifr->ifr_addr = ia->ia_addr; 500 if ((error = sa6_recoverscope(&ifr->ifr_addr)) != 0) 501 return (error); 502 break; 503 504 case SIOCGIFDSTADDR_IN6: 505 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 506 return (EINVAL); 507 /* 508 * XXX: should we check if ifa_dstaddr is NULL and return 509 * an error? 510 */ 511 ifr->ifr_dstaddr = ia->ia_dstaddr; 512 if ((error = sa6_recoverscope(&ifr->ifr_dstaddr)) != 0) 513 return (error); 514 break; 515 516 case SIOCGIFNETMASK_IN6: 517 ifr->ifr_addr = ia->ia_prefixmask; 518 break; 519 520 case SIOCGIFAFLAG_IN6: 521 ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags; 522 break; 523 524 case SIOCGIFSTAT_IN6: 525 if (ifp == NULL) 526 return EINVAL; 527 bzero(&ifr->ifr_ifru.ifru_stat, 528 sizeof(ifr->ifr_ifru.ifru_stat)); 529 ifr->ifr_ifru.ifru_stat = 530 *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->in6_ifstat; 531 break; 532 533 case SIOCGIFSTAT_ICMP6: 534 if (ifp == NULL) 535 return EINVAL; 536 bzero(&ifr->ifr_ifru.ifru_stat, 537 sizeof(ifr->ifr_ifru.ifru_icmp6stat)); 538 ifr->ifr_ifru.ifru_icmp6stat = 539 *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->icmp6_ifstat; 540 break; 541 542 case SIOCGIFALIFETIME_IN6: 543 ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime; 544 break; 545 546 case SIOCSIFALIFETIME_IN6: 547 ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime; 548 /* for sanity */ 549 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) { 550 ia->ia6_lifetime.ia6t_expire = 551 time_second + ia->ia6_lifetime.ia6t_vltime; 552 } else 553 ia->ia6_lifetime.ia6t_expire = 0; 554 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) { 555 ia->ia6_lifetime.ia6t_preferred = 556 time_second + ia->ia6_lifetime.ia6t_pltime; 557 } else 558 ia->ia6_lifetime.ia6t_preferred = 0; 559 break; 560 561 case SIOCAIFADDR_IN6: 562 { 563 int i, error = 0; 564 struct nd_prefix pr0, *pr; 565 566 /* 567 * first, make or update the interface address structure, 568 * and link it to the list. 569 */ 570 if ((error = in6_update_ifa(ifp, ifra, ia)) != 0) 571 return (error); 572 573 /* 574 * then, make the prefix on-link on the interface. 575 * XXX: we'd rather create the prefix before the address, but 576 * we need at least one address to install the corresponding 577 * interface route, so we configure the address first. 578 */ 579 580 /* 581 * convert mask to prefix length (prefixmask has already 582 * been validated in in6_update_ifa(). 583 */ 584 bzero(&pr0, sizeof(pr0)); 585 pr0.ndpr_ifp = ifp; 586 pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr, 587 NULL); 588 if (pr0.ndpr_plen == 128) { 589 break; /* we don't need to install a host route. */ 590 } 591 pr0.ndpr_prefix = ifra->ifra_addr; 592 pr0.ndpr_mask = ifra->ifra_prefixmask.sin6_addr; 593 /* apply the mask for safety. */ 594 for (i = 0; i < 4; i++) { 595 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &= 596 ifra->ifra_prefixmask.sin6_addr.s6_addr32[i]; 597 } 598 /* 599 * XXX: since we don't have an API to set prefix (not address) 600 * lifetimes, we just use the same lifetimes as addresses. 601 * The (temporarily) installed lifetimes can be overridden by 602 * later advertised RAs (when accept_rtadv is non 0), which is 603 * an intended behavior. 604 */ 605 pr0.ndpr_raf_onlink = 1; /* should be configurable? */ 606 pr0.ndpr_raf_auto = 607 ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0); 608 pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime; 609 pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime; 610 611 /* add the prefix if not yet. */ 612 if ((pr = nd6_prefix_lookup(&pr0)) == NULL) { 613 /* 614 * nd6_prelist_add will install the corresponding 615 * interface route. 616 */ 617 if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0) 618 return (error); 619 if (pr == NULL) { 620 log(LOG_ERR, "nd6_prelist_add succeeded but " 621 "no prefix\n"); 622 return (EINVAL); /* XXX panic here? */ 623 } 624 } 625 if ((ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr)) 626 == NULL) { 627 /* XXX: this should not happen! */ 628 log(LOG_ERR, "in6_control: addition succeeded, but" 629 " no ifaddr\n"); 630 } else { 631 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) != 0 && 632 ia->ia6_ndpr == NULL) { /* new autoconfed addr */ 633 ia->ia6_ndpr = pr; 634 pr->ndpr_refcnt++; 635 636 /* 637 * If this is the first autoconf address from 638 * the prefix, create a temporary address 639 * as well (when specified). 640 */ 641 if (ip6_use_tempaddr && 642 pr->ndpr_refcnt == 1) { 643 int e; 644 if ((e = in6_tmpifadd(ia, 1)) != 0) { 645 log(LOG_NOTICE, "in6_control: " 646 "failed to create a " 647 "temporary address, " 648 "errno=%d\n", e); 649 } 650 } 651 } 652 653 /* 654 * this might affect the status of autoconfigured 655 * addresses, that is, this address might make 656 * other addresses detached. 657 */ 658 pfxlist_onlink_check(); 659 } 660 if (error == 0 && ia) 661 EVENTHANDLER_INVOKE(ifaddr_event, ifp); 662 break; 663 } 664 665 case SIOCDIFADDR_IN6: 666 { 667 int i = 0; 668 struct nd_prefix pr0, *pr; 669 670 /* 671 * If the address being deleted is the only one that owns 672 * the corresponding prefix, expire the prefix as well. 673 * XXX: theoretically, we don't have to worry about such 674 * relationship, since we separate the address management 675 * and the prefix management. We do this, however, to provide 676 * as much backward compatibility as possible in terms of 677 * the ioctl operation. 678 */ 679 bzero(&pr0, sizeof(pr0)); 680 pr0.ndpr_ifp = ifp; 681 pr0.ndpr_plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, 682 NULL); 683 if (pr0.ndpr_plen == 128) 684 goto purgeaddr; 685 pr0.ndpr_prefix = ia->ia_addr; 686 pr0.ndpr_mask = ia->ia_prefixmask.sin6_addr; 687 for (i = 0; i < 4; i++) { 688 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &= 689 ia->ia_prefixmask.sin6_addr.s6_addr32[i]; 690 } 691 /* 692 * The logic of the following condition is a bit complicated. 693 * We expire the prefix when 694 * 1. the address obeys autoconfiguration and it is the 695 * only owner of the associated prefix, or 696 * 2. the address does not obey autoconf and there is no 697 * other owner of the prefix. 698 */ 699 if ((pr = nd6_prefix_lookup(&pr0)) != NULL && 700 (((ia->ia6_flags & IN6_IFF_AUTOCONF) != 0 && 701 pr->ndpr_refcnt == 1) || 702 ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0 && 703 pr->ndpr_refcnt == 0))) { 704 pr->ndpr_expire = 1; /* XXX: just for expiration */ 705 } 706 707 purgeaddr: 708 in6_purgeaddr(&ia->ia_ifa); 709 EVENTHANDLER_INVOKE(ifaddr_event, ifp); 710 break; 711 } 712 713 default: 714 if (ifp == NULL || ifp->if_ioctl == 0) 715 return (EOPNOTSUPP); 716 return ((*ifp->if_ioctl)(ifp, cmd, data)); 717 } 718 719 return (0); 720 } 721 722 /* 723 * Update parameters of an IPv6 interface address. 724 * If necessary, a new entry is created and linked into address chains. 725 * This function is separated from in6_control(). 726 * XXX: should this be performed under splnet()? 727 */ 728 int 729 in6_update_ifa(ifp, ifra, ia) 730 struct ifnet *ifp; 731 struct in6_aliasreq *ifra; 732 struct in6_ifaddr *ia; 733 { 734 int error = 0, hostIsNew = 0, plen = -1; 735 struct in6_ifaddr *oia; 736 struct sockaddr_in6 dst6; 737 struct in6_addrlifetime *lt; 738 struct rtentry *rt; 739 740 /* Validate parameters */ 741 if (ifp == NULL || ifra == NULL) /* this maybe redundant */ 742 return (EINVAL); 743 744 /* 745 * The destination address for a p2p link must have a family 746 * of AF_UNSPEC or AF_INET6. 747 */ 748 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 && 749 ifra->ifra_dstaddr.sin6_family != AF_INET6 && 750 ifra->ifra_dstaddr.sin6_family != AF_UNSPEC) 751 return (EAFNOSUPPORT); 752 /* 753 * validate ifra_prefixmask. don't check sin6_family, netmask 754 * does not carry fields other than sin6_len. 755 */ 756 if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6)) 757 return (EINVAL); 758 /* 759 * Because the IPv6 address architecture is classless, we require 760 * users to specify a (non 0) prefix length (mask) for a new address. 761 * We also require the prefix (when specified) mask is valid, and thus 762 * reject a non-consecutive mask. 763 */ 764 if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0) 765 return (EINVAL); 766 if (ifra->ifra_prefixmask.sin6_len != 0) { 767 plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr, 768 (u_char *)&ifra->ifra_prefixmask + 769 ifra->ifra_prefixmask.sin6_len); 770 if (plen <= 0) 771 return (EINVAL); 772 } else { 773 /* 774 * In this case, ia must not be NULL. We just use its prefix 775 * length. 776 */ 777 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); 778 } 779 /* 780 * If the destination address on a p2p interface is specified, 781 * and the address is a scoped one, validate/set the scope 782 * zone identifier. 783 */ 784 dst6 = ifra->ifra_dstaddr; 785 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) != 0 && 786 (dst6.sin6_family == AF_INET6)) { 787 struct in6_addr in6_tmp; 788 u_int32_t zoneid; 789 790 in6_tmp = dst6.sin6_addr; 791 if (in6_setscope(&in6_tmp, ifp, &zoneid)) 792 return (EINVAL); /* XXX: should be impossible */ 793 794 if (dst6.sin6_scope_id != 0) { 795 if (dst6.sin6_scope_id != zoneid) 796 return (EINVAL); 797 } else /* user omit to specify the ID. */ 798 dst6.sin6_scope_id = zoneid; 799 800 /* convert into the internal form */ 801 if (sa6_embedscope(&dst6, 0)) 802 return (EINVAL); /* XXX: should be impossible */ 803 } 804 /* 805 * The destination address can be specified only for a p2p or a 806 * loopback interface. If specified, the corresponding prefix length 807 * must be 128. 808 */ 809 if (ifra->ifra_dstaddr.sin6_family == AF_INET6) { 810 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) == 0) { 811 /* XXX: noisy message */ 812 nd6log((LOG_INFO, "in6_update_ifa: a destination can " 813 "be specified for a p2p or a loopback IF only\n")); 814 return (EINVAL); 815 } 816 if (plen != 128) { 817 nd6log((LOG_INFO, "in6_update_ifa: prefixlen should " 818 "be 128 when dstaddr is specified\n")); 819 return (EINVAL); 820 } 821 } 822 /* lifetime consistency check */ 823 lt = &ifra->ifra_lifetime; 824 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME 825 && lt->ia6t_vltime + time_second < time_second) { 826 return EINVAL; 827 } 828 if (lt->ia6t_vltime == 0) { 829 /* 830 * the following log might be noisy, but this is a typical 831 * configuration mistake or a tool's bug. 832 */ 833 nd6log((LOG_INFO, 834 "in6_update_ifa: valid lifetime is 0 for %s\n", 835 ip6_sprintf(&ifra->ifra_addr.sin6_addr))); 836 } 837 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME 838 && lt->ia6t_pltime + time_second < time_second) { 839 return EINVAL; 840 } 841 842 /* 843 * If this is a new address, allocate a new ifaddr and link it 844 * into chains. 845 */ 846 if (ia == NULL) { 847 hostIsNew = 1; 848 /* 849 * When in6_update_ifa() is called in a process of a received 850 * RA, it is called under an interrupt context. So, we should 851 * call malloc with M_NOWAIT. 852 */ 853 ia = (struct in6_ifaddr *) malloc(sizeof(*ia), M_IFADDR, 854 M_NOWAIT); 855 if (ia == NULL) 856 return (ENOBUFS); 857 bzero((caddr_t)ia, sizeof(*ia)); 858 /* Initialize the address and masks */ 859 IFA_LOCK_INIT(&ia->ia_ifa); 860 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr; 861 ia->ia_addr.sin6_family = AF_INET6; 862 ia->ia_addr.sin6_len = sizeof(ia->ia_addr); 863 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) { 864 /* 865 * XXX: some functions expect that ifa_dstaddr is not 866 * NULL for p2p interfaces. 867 */ 868 ia->ia_ifa.ifa_dstaddr = 869 (struct sockaddr *)&ia->ia_dstaddr; 870 } else { 871 ia->ia_ifa.ifa_dstaddr = NULL; 872 } 873 ia->ia_ifa.ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask; 874 875 ia->ia_ifp = ifp; 876 if ((oia = in6_ifaddr) != NULL) { 877 for ( ; oia->ia_next; oia = oia->ia_next) 878 continue; 879 oia->ia_next = ia; 880 } else 881 in6_ifaddr = ia; 882 883 ia->ia_ifa.ifa_refcnt = 1; 884 TAILQ_INSERT_TAIL(&ifp->if_addrlist, &ia->ia_ifa, ifa_list); 885 } 886 887 /* set prefix mask */ 888 if (ifra->ifra_prefixmask.sin6_len) { 889 /* 890 * We prohibit changing the prefix length of an existing 891 * address, because 892 * + such an operation should be rare in IPv6, and 893 * + the operation would confuse prefix management. 894 */ 895 if (ia->ia_prefixmask.sin6_len && 896 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) { 897 nd6log((LOG_INFO, "in6_update_ifa: the prefix length of an" 898 " existing (%s) address should not be changed\n", 899 ip6_sprintf(&ia->ia_addr.sin6_addr))); 900 error = EINVAL; 901 goto unlink; 902 } 903 ia->ia_prefixmask = ifra->ifra_prefixmask; 904 } 905 906 /* 907 * If a new destination address is specified, scrub the old one and 908 * install the new destination. Note that the interface must be 909 * p2p or loopback (see the check above.) 910 */ 911 if (dst6.sin6_family == AF_INET6 && 912 !IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr, &ia->ia_dstaddr.sin6_addr)) { 913 int e; 914 915 if ((ia->ia_flags & IFA_ROUTE) != 0 && 916 (e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST)) != 0) { 917 nd6log((LOG_ERR, "in6_update_ifa: failed to remove " 918 "a route to the old destination: %s\n", 919 ip6_sprintf(&ia->ia_addr.sin6_addr))); 920 /* proceed anyway... */ 921 } else 922 ia->ia_flags &= ~IFA_ROUTE; 923 ia->ia_dstaddr = dst6; 924 } 925 926 /* 927 * Set lifetimes. We do not refer to ia6t_expire and ia6t_preferred 928 * to see if the address is deprecated or invalidated, but initialize 929 * these members for applications. 930 */ 931 ia->ia6_lifetime = ifra->ifra_lifetime; 932 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) { 933 ia->ia6_lifetime.ia6t_expire = 934 time_second + ia->ia6_lifetime.ia6t_vltime; 935 } else 936 ia->ia6_lifetime.ia6t_expire = 0; 937 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) { 938 ia->ia6_lifetime.ia6t_preferred = 939 time_second + ia->ia6_lifetime.ia6t_pltime; 940 } else 941 ia->ia6_lifetime.ia6t_preferred = 0; 942 943 /* reset the interface and routing table appropriately. */ 944 if ((error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew)) != 0) 945 goto unlink; 946 947 /* 948 * configure address flags. 949 */ 950 ia->ia6_flags = ifra->ifra_flags; 951 ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /* safety */ 952 ia->ia6_flags &= ~IN6_IFF_NODAD; /* Mobile IPv6 */ 953 /* 954 * backward compatibility - if IN6_IFF_DEPRECATED is set from the 955 * userland, make it deprecated. 956 */ 957 if ((ifra->ifra_flags & IN6_IFF_DEPRECATED) != 0) { 958 ia->ia6_lifetime.ia6t_pltime = 0; 959 ia->ia6_lifetime.ia6t_preferred = time_second; 960 } 961 962 /* 963 * Perform DAD, if needed. 964 * XXX It may be of use, if we can administratively 965 * disable DAD. 966 */ 967 if (in6if_do_dad(ifp) && hostIsNew && 968 (ifra->ifra_flags & IN6_IFF_NODAD) == 0) { 969 ia->ia6_flags |= IN6_IFF_TENTATIVE; 970 nd6_dad_start((struct ifaddr *)ia, NULL); 971 } 972 973 /* 974 * We are done if we have simply modified an existing address. 975 */ 976 if (!hostIsNew) 977 return (error); 978 979 /* 980 * Beyond this point, we should call in6_purgeaddr upon an error, 981 * not just go to unlink. 982 */ 983 984 if ((ifp->if_flags & IFF_MULTICAST) != 0) { 985 struct sockaddr_in6 mltaddr, mltmask; 986 struct in6_multi *in6m; 987 struct in6_addr llsol; 988 989 /* join solicited multicast addr for new host id */ 990 bzero(&llsol, sizeof(struct in6_addr)); 991 llsol.s6_addr16[0] = htons(0xff02); 992 llsol.s6_addr32[1] = 0; 993 llsol.s6_addr32[2] = htonl(1); 994 llsol.s6_addr32[3] = ifra->ifra_addr.sin6_addr.s6_addr32[3]; 995 llsol.s6_addr8[12] = 0xff; 996 if ((error = in6_setscope(&llsol, ifp, NULL)) != 0) { 997 /* XXX: should not happen */ 998 log(LOG_ERR, "in6_update_ifa: " 999 "in6_setscope failed\n"); 1000 goto cleanup; 1001 } 1002 (void)in6_addmulti(&llsol, ifp, &error); 1003 if (error != 0) { 1004 nd6log((LOG_WARNING, 1005 "in6_update_ifa: addmulti failed for " 1006 "%s on %s (errno=%d)\n", 1007 ip6_sprintf(&llsol), if_name(ifp), 1008 error)); 1009 goto cleanup; 1010 } 1011 1012 bzero(&mltmask, sizeof(mltmask)); 1013 mltmask.sin6_len = sizeof(struct sockaddr_in6); 1014 mltmask.sin6_family = AF_INET6; 1015 mltmask.sin6_addr = in6mask32; 1016 1017 /* 1018 * join link-local all-nodes address 1019 */ 1020 bzero(&mltaddr, sizeof(mltaddr)); 1021 mltaddr.sin6_len = sizeof(struct sockaddr_in6); 1022 mltaddr.sin6_family = AF_INET6; 1023 mltaddr.sin6_addr = in6addr_linklocal_allnodes; 1024 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 1025 0) 1026 goto cleanup; /* XXX: should not fail */ 1027 1028 /* 1029 * XXX: do we really need this automatic routes? 1030 * We should probably reconsider this stuff. Most applications 1031 * actually do not need the routes, since they usually specify 1032 * the outgoing interface. 1033 */ 1034 rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL); 1035 if (rt) { 1036 /* 1037 * 32bit came from "mltmask" 1038 */ 1039 if (memcmp(&mltaddr.sin6_addr, 1040 &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr, 1041 32 / 8)) { 1042 RTFREE_LOCKED(rt); 1043 rt = NULL; 1044 } 1045 } 1046 if (!rt) { 1047 /* XXX: we need RTF_CLONING to fake nd6_rtrequest */ 1048 error = rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr, 1049 (struct sockaddr *)&ia->ia_addr, 1050 (struct sockaddr *)&mltmask, RTF_UP | RTF_CLONING, 1051 (struct rtentry **)0); 1052 if (error) 1053 goto cleanup; 1054 } else 1055 RTFREE_LOCKED(rt); 1056 1057 IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m); 1058 if (in6m == NULL) { 1059 (void)in6_addmulti(&mltaddr.sin6_addr, ifp, &error); 1060 if (error != 0) { 1061 nd6log((LOG_WARNING, 1062 "in6_update_ifa: addmulti failed for " 1063 "%s on %s (errno=%d)\n", 1064 ip6_sprintf(&mltaddr.sin6_addr), 1065 if_name(ifp), error)); 1066 goto cleanup; 1067 } 1068 } 1069 1070 /* 1071 * join node information group address 1072 */ 1073 #define hostnamelen strlen(hostname) 1074 if (in6_nigroup(ifp, hostname, hostnamelen, &mltaddr.sin6_addr) 1075 == 0) { 1076 IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m); 1077 if (in6m == NULL) { 1078 (void)in6_addmulti(&mltaddr.sin6_addr, 1079 ifp, &error); 1080 if (error != 0) { 1081 nd6log((LOG_WARNING, "in6_update_ifa: " 1082 "addmulti failed for " 1083 "%s on %s (errno=%d)\n", 1084 ip6_sprintf(&mltaddr.sin6_addr), 1085 if_name(ifp), error)); 1086 goto cleanup; 1087 } 1088 } 1089 } 1090 #undef hostnamelen 1091 1092 /* 1093 * join interface-local all-nodes address. 1094 * (ff01::1%ifN, and ff01::%ifN/32) 1095 */ 1096 mltaddr.sin6_addr = in6addr_nodelocal_allnodes; 1097 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) 1098 != 0) 1099 goto cleanup; /* XXX: should not fail */ 1100 /* XXX: again, do we really need the route? */ 1101 rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL); 1102 if (rt) { 1103 /* 32bit came from "mltmask" */ 1104 if (memcmp(&mltaddr.sin6_addr, 1105 &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr, 1106 32 / 8)) { 1107 RTFREE_LOCKED(rt); 1108 rt = NULL; 1109 } 1110 } 1111 if (!rt) { 1112 error = rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr, 1113 (struct sockaddr *)&ia->ia_addr, 1114 (struct sockaddr *)&mltmask, RTF_UP | RTF_CLONING, 1115 (struct rtentry **)0); 1116 if (error) 1117 goto cleanup; 1118 } else 1119 RTFREE_LOCKED(rt); 1120 1121 IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m); 1122 if (in6m == NULL) { 1123 (void)in6_addmulti(&mltaddr.sin6_addr, ifp, &error); 1124 if (error != 0) { 1125 nd6log((LOG_WARNING, "in6_update_ifa: " 1126 "addmulti failed for %s on %s " 1127 "(errno=%d)\n", 1128 ip6_sprintf(&mltaddr.sin6_addr), 1129 if_name(ifp), error)); 1130 goto cleanup; 1131 } 1132 } 1133 } 1134 1135 return (error); 1136 1137 unlink: 1138 /* 1139 * XXX: if a change of an existing address failed, keep the entry 1140 * anyway. 1141 */ 1142 if (hostIsNew) 1143 in6_unlink_ifa(ia, ifp); 1144 return (error); 1145 1146 cleanup: 1147 in6_purgeaddr(&ia->ia_ifa); 1148 return error; 1149 } 1150 1151 void 1152 in6_purgeaddr(ifa) 1153 struct ifaddr *ifa; 1154 { 1155 struct ifnet *ifp = ifa->ifa_ifp; 1156 struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa; 1157 1158 /* stop DAD processing */ 1159 nd6_dad_stop(ifa); 1160 1161 /* 1162 * delete route to the destination of the address being purged. 1163 * The interface must be p2p or loopback in this case. 1164 */ 1165 if ((ia->ia_flags & IFA_ROUTE) != 0 && ia->ia_dstaddr.sin6_len != 0) { 1166 int e; 1167 1168 if ((e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST)) 1169 != 0) { 1170 log(LOG_ERR, "in6_purgeaddr: failed to remove " 1171 "a route to the p2p destination: %s on %s, " 1172 "errno=%d\n", 1173 ip6_sprintf(&ia->ia_addr.sin6_addr), if_name(ifp), 1174 e); 1175 /* proceed anyway... */ 1176 } else 1177 ia->ia_flags &= ~IFA_ROUTE; 1178 } 1179 1180 /* Remove ownaddr's loopback rtentry, if it exists. */ 1181 in6_ifremloop(&(ia->ia_ifa)); 1182 1183 if (ifp->if_flags & IFF_MULTICAST) { 1184 /* 1185 * delete solicited multicast addr for deleting host id 1186 */ 1187 struct in6_multi *in6m; 1188 struct in6_addr llsol; 1189 bzero(&llsol, sizeof(struct in6_addr)); 1190 llsol.s6_addr16[0] = htons(0xff02); 1191 llsol.s6_addr32[1] = 0; 1192 llsol.s6_addr32[2] = htonl(1); 1193 llsol.s6_addr32[3] = 1194 ia->ia_addr.sin6_addr.s6_addr32[3]; 1195 llsol.s6_addr8[12] = 0xff; 1196 (void)in6_setscope(&llsol, ifp, NULL); /* XXX proceed anyway */ 1197 1198 IN6_LOOKUP_MULTI(llsol, ifp, in6m); 1199 if (in6m) 1200 in6_delmulti(in6m); 1201 } 1202 1203 in6_unlink_ifa(ia, ifp); 1204 } 1205 1206 static void 1207 in6_unlink_ifa(ia, ifp) 1208 struct in6_ifaddr *ia; 1209 struct ifnet *ifp; 1210 { 1211 struct in6_ifaddr *oia; 1212 int s = splnet(); 1213 1214 TAILQ_REMOVE(&ifp->if_addrlist, &ia->ia_ifa, ifa_list); 1215 1216 oia = ia; 1217 if (oia == (ia = in6_ifaddr)) 1218 in6_ifaddr = ia->ia_next; 1219 else { 1220 while (ia->ia_next && (ia->ia_next != oia)) 1221 ia = ia->ia_next; 1222 if (ia->ia_next) 1223 ia->ia_next = oia->ia_next; 1224 else { 1225 /* search failed */ 1226 printf("Couldn't unlink in6_ifaddr from in6_ifaddr\n"); 1227 } 1228 } 1229 1230 /* 1231 * When an autoconfigured address is being removed, release the 1232 * reference to the base prefix. Also, since the release might 1233 * affect the status of other (detached) addresses, call 1234 * pfxlist_onlink_check(). 1235 */ 1236 if ((oia->ia6_flags & IN6_IFF_AUTOCONF) != 0) { 1237 if (oia->ia6_ndpr == NULL) { 1238 nd6log((LOG_NOTICE, "in6_unlink_ifa: autoconf'ed address " 1239 "%p has no prefix\n", oia)); 1240 } else { 1241 oia->ia6_ndpr->ndpr_refcnt--; 1242 oia->ia6_flags &= ~IN6_IFF_AUTOCONF; 1243 oia->ia6_ndpr = NULL; 1244 } 1245 1246 pfxlist_onlink_check(); 1247 } 1248 1249 /* 1250 * release another refcnt for the link from in6_ifaddr. 1251 * Note that we should decrement the refcnt at least once for all *BSD. 1252 */ 1253 IFAFREE(&oia->ia_ifa); 1254 1255 splx(s); 1256 } 1257 1258 void 1259 in6_purgeif(ifp) 1260 struct ifnet *ifp; 1261 { 1262 struct ifaddr *ifa, *nifa; 1263 1264 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL; ifa = nifa) { 1265 nifa = TAILQ_NEXT(ifa, ifa_list); 1266 if (ifa->ifa_addr->sa_family != AF_INET6) 1267 continue; 1268 in6_purgeaddr(ifa); 1269 } 1270 1271 in6_ifdetach(ifp); 1272 } 1273 1274 /* 1275 * SIOC[GAD]LIFADDR. 1276 * SIOCGLIFADDR: get first address. (?) 1277 * SIOCGLIFADDR with IFLR_PREFIX: 1278 * get first address that matches the specified prefix. 1279 * SIOCALIFADDR: add the specified address. 1280 * SIOCALIFADDR with IFLR_PREFIX: 1281 * add the specified prefix, filling hostid part from 1282 * the first link-local address. prefixlen must be <= 64. 1283 * SIOCDLIFADDR: delete the specified address. 1284 * SIOCDLIFADDR with IFLR_PREFIX: 1285 * delete the first address that matches the specified prefix. 1286 * return values: 1287 * EINVAL on invalid parameters 1288 * EADDRNOTAVAIL on prefix match failed/specified address not found 1289 * other values may be returned from in6_ioctl() 1290 * 1291 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64. 1292 * this is to accomodate address naming scheme other than RFC2374, 1293 * in the future. 1294 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374 1295 * address encoding scheme. (see figure on page 8) 1296 */ 1297 static int 1298 in6_lifaddr_ioctl(so, cmd, data, ifp, td) 1299 struct socket *so; 1300 u_long cmd; 1301 caddr_t data; 1302 struct ifnet *ifp; 1303 struct thread *td; 1304 { 1305 struct if_laddrreq *iflr = (struct if_laddrreq *)data; 1306 struct ifaddr *ifa; 1307 struct sockaddr *sa; 1308 1309 /* sanity checks */ 1310 if (!data || !ifp) { 1311 panic("invalid argument to in6_lifaddr_ioctl"); 1312 /* NOTREACHED */ 1313 } 1314 1315 switch (cmd) { 1316 case SIOCGLIFADDR: 1317 /* address must be specified on GET with IFLR_PREFIX */ 1318 if ((iflr->flags & IFLR_PREFIX) == 0) 1319 break; 1320 /* FALLTHROUGH */ 1321 case SIOCALIFADDR: 1322 case SIOCDLIFADDR: 1323 /* address must be specified on ADD and DELETE */ 1324 sa = (struct sockaddr *)&iflr->addr; 1325 if (sa->sa_family != AF_INET6) 1326 return EINVAL; 1327 if (sa->sa_len != sizeof(struct sockaddr_in6)) 1328 return EINVAL; 1329 /* XXX need improvement */ 1330 sa = (struct sockaddr *)&iflr->dstaddr; 1331 if (sa->sa_family && sa->sa_family != AF_INET6) 1332 return EINVAL; 1333 if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6)) 1334 return EINVAL; 1335 break; 1336 default: /* shouldn't happen */ 1337 #if 0 1338 panic("invalid cmd to in6_lifaddr_ioctl"); 1339 /* NOTREACHED */ 1340 #else 1341 return EOPNOTSUPP; 1342 #endif 1343 } 1344 if (sizeof(struct in6_addr) * 8 < iflr->prefixlen) 1345 return EINVAL; 1346 1347 switch (cmd) { 1348 case SIOCALIFADDR: 1349 { 1350 struct in6_aliasreq ifra; 1351 struct in6_addr *hostid = NULL; 1352 int prefixlen; 1353 1354 if ((iflr->flags & IFLR_PREFIX) != 0) { 1355 struct sockaddr_in6 *sin6; 1356 1357 /* 1358 * hostid is to fill in the hostid part of the 1359 * address. hostid points to the first link-local 1360 * address attached to the interface. 1361 */ 1362 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0); 1363 if (!ifa) 1364 return EADDRNOTAVAIL; 1365 hostid = IFA_IN6(ifa); 1366 1367 /* prefixlen must be <= 64. */ 1368 if (64 < iflr->prefixlen) 1369 return EINVAL; 1370 prefixlen = iflr->prefixlen; 1371 1372 /* hostid part must be zero. */ 1373 sin6 = (struct sockaddr_in6 *)&iflr->addr; 1374 if (sin6->sin6_addr.s6_addr32[2] != 0 || 1375 sin6->sin6_addr.s6_addr32[3] != 0) { 1376 return EINVAL; 1377 } 1378 } else 1379 prefixlen = iflr->prefixlen; 1380 1381 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */ 1382 bzero(&ifra, sizeof(ifra)); 1383 bcopy(iflr->iflr_name, ifra.ifra_name, sizeof(ifra.ifra_name)); 1384 1385 bcopy(&iflr->addr, &ifra.ifra_addr, 1386 ((struct sockaddr *)&iflr->addr)->sa_len); 1387 if (hostid) { 1388 /* fill in hostid part */ 1389 ifra.ifra_addr.sin6_addr.s6_addr32[2] = 1390 hostid->s6_addr32[2]; 1391 ifra.ifra_addr.sin6_addr.s6_addr32[3] = 1392 hostid->s6_addr32[3]; 1393 } 1394 1395 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /* XXX */ 1396 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr, 1397 ((struct sockaddr *)&iflr->dstaddr)->sa_len); 1398 if (hostid) { 1399 ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] = 1400 hostid->s6_addr32[2]; 1401 ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] = 1402 hostid->s6_addr32[3]; 1403 } 1404 } 1405 1406 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6); 1407 in6_prefixlen2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen); 1408 1409 ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX; 1410 return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, td); 1411 } 1412 case SIOCGLIFADDR: 1413 case SIOCDLIFADDR: 1414 { 1415 struct in6_ifaddr *ia; 1416 struct in6_addr mask, candidate, match; 1417 struct sockaddr_in6 *sin6; 1418 int cmp; 1419 1420 bzero(&mask, sizeof(mask)); 1421 if (iflr->flags & IFLR_PREFIX) { 1422 /* lookup a prefix rather than address. */ 1423 in6_prefixlen2mask(&mask, iflr->prefixlen); 1424 1425 sin6 = (struct sockaddr_in6 *)&iflr->addr; 1426 bcopy(&sin6->sin6_addr, &match, sizeof(match)); 1427 match.s6_addr32[0] &= mask.s6_addr32[0]; 1428 match.s6_addr32[1] &= mask.s6_addr32[1]; 1429 match.s6_addr32[2] &= mask.s6_addr32[2]; 1430 match.s6_addr32[3] &= mask.s6_addr32[3]; 1431 1432 /* if you set extra bits, that's wrong */ 1433 if (bcmp(&match, &sin6->sin6_addr, sizeof(match))) 1434 return EINVAL; 1435 1436 cmp = 1; 1437 } else { 1438 if (cmd == SIOCGLIFADDR) { 1439 /* on getting an address, take the 1st match */ 1440 cmp = 0; /* XXX */ 1441 } else { 1442 /* on deleting an address, do exact match */ 1443 in6_prefixlen2mask(&mask, 128); 1444 sin6 = (struct sockaddr_in6 *)&iflr->addr; 1445 bcopy(&sin6->sin6_addr, &match, sizeof(match)); 1446 1447 cmp = 1; 1448 } 1449 } 1450 1451 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 1452 if (ifa->ifa_addr->sa_family != AF_INET6) 1453 continue; 1454 if (!cmp) 1455 break; 1456 1457 /* 1458 * XXX: this is adhoc, but is necessary to allow 1459 * a user to specify fe80::/64 (not /10) for a 1460 * link-local address. 1461 */ 1462 bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate)); 1463 in6_clearscope(&candidate); 1464 candidate.s6_addr32[0] &= mask.s6_addr32[0]; 1465 candidate.s6_addr32[1] &= mask.s6_addr32[1]; 1466 candidate.s6_addr32[2] &= mask.s6_addr32[2]; 1467 candidate.s6_addr32[3] &= mask.s6_addr32[3]; 1468 if (IN6_ARE_ADDR_EQUAL(&candidate, &match)) 1469 break; 1470 } 1471 if (!ifa) 1472 return EADDRNOTAVAIL; 1473 ia = ifa2ia6(ifa); 1474 1475 if (cmd == SIOCGLIFADDR) { 1476 int error; 1477 1478 /* fill in the if_laddrreq structure */ 1479 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len); 1480 error = sa6_recoverscope( 1481 (struct sockaddr_in6 *)&iflr->addr); 1482 if (error != 0) 1483 return (error); 1484 1485 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 1486 bcopy(&ia->ia_dstaddr, &iflr->dstaddr, 1487 ia->ia_dstaddr.sin6_len); 1488 error = sa6_recoverscope( 1489 (struct sockaddr_in6 *)&iflr->dstaddr); 1490 if (error != 0) 1491 return (error); 1492 } else 1493 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr)); 1494 1495 iflr->prefixlen = 1496 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); 1497 1498 iflr->flags = ia->ia6_flags; /* XXX */ 1499 1500 return 0; 1501 } else { 1502 struct in6_aliasreq ifra; 1503 1504 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */ 1505 bzero(&ifra, sizeof(ifra)); 1506 bcopy(iflr->iflr_name, ifra.ifra_name, 1507 sizeof(ifra.ifra_name)); 1508 1509 bcopy(&ia->ia_addr, &ifra.ifra_addr, 1510 ia->ia_addr.sin6_len); 1511 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 1512 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr, 1513 ia->ia_dstaddr.sin6_len); 1514 } else { 1515 bzero(&ifra.ifra_dstaddr, 1516 sizeof(ifra.ifra_dstaddr)); 1517 } 1518 bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr, 1519 ia->ia_prefixmask.sin6_len); 1520 1521 ifra.ifra_flags = ia->ia6_flags; 1522 return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra, 1523 ifp, td); 1524 } 1525 } 1526 } 1527 1528 return EOPNOTSUPP; /* just for safety */ 1529 } 1530 1531 /* 1532 * Initialize an interface's intetnet6 address 1533 * and routing table entry. 1534 */ 1535 static int 1536 in6_ifinit(ifp, ia, sin6, newhost) 1537 struct ifnet *ifp; 1538 struct in6_ifaddr *ia; 1539 struct sockaddr_in6 *sin6; 1540 int newhost; 1541 { 1542 int error = 0, plen, ifacount = 0; 1543 int s = splimp(); 1544 struct ifaddr *ifa; 1545 1546 /* 1547 * Give the interface a chance to initialize 1548 * if this is its first address, 1549 * and to validate the address if necessary. 1550 */ 1551 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 1552 if (ifa->ifa_addr == NULL) 1553 continue; /* just for safety */ 1554 if (ifa->ifa_addr->sa_family != AF_INET6) 1555 continue; 1556 ifacount++; 1557 } 1558 1559 ia->ia_addr = *sin6; 1560 1561 if (ifacount <= 1 && ifp->if_ioctl) { 1562 IFF_LOCKGIANT(ifp); 1563 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia); 1564 IFF_UNLOCKGIANT(ifp); 1565 if (error) { 1566 splx(s); 1567 return (error); 1568 } 1569 } 1570 splx(s); 1571 1572 ia->ia_ifa.ifa_metric = ifp->if_metric; 1573 1574 /* we could do in(6)_socktrim here, but just omit it at this moment. */ 1575 1576 /* 1577 * Special case: 1578 * If a new destination address is specified for a point-to-point 1579 * interface, install a route to the destination as an interface 1580 * direct route. 1581 * XXX: the logic below rejects assigning multiple addresses on a p2p 1582 * interface that share a same destination. 1583 */ 1584 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */ 1585 if (!(ia->ia_flags & IFA_ROUTE) && plen == 128 && 1586 ia->ia_dstaddr.sin6_family == AF_INET6) { 1587 if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD, 1588 RTF_UP | RTF_HOST)) != 0) 1589 return (error); 1590 ia->ia_flags |= IFA_ROUTE; 1591 } 1592 if (plen < 128) { 1593 /* 1594 * The RTF_CLONING flag is necessary for in6_is_ifloop_auto(). 1595 */ 1596 ia->ia_ifa.ifa_flags |= RTF_CLONING; 1597 } 1598 1599 /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */ 1600 if (newhost) { 1601 /* set the rtrequest function to create llinfo */ 1602 ia->ia_ifa.ifa_rtrequest = nd6_rtrequest; 1603 in6_ifaddloop(&(ia->ia_ifa)); 1604 } 1605 1606 return (error); 1607 } 1608 1609 struct in6_multi_mship * 1610 in6_joingroup(ifp, addr, errorp) 1611 struct ifnet *ifp; 1612 struct in6_addr *addr; 1613 int *errorp; 1614 { 1615 struct in6_multi_mship *imm; 1616 1617 imm = malloc(sizeof(*imm), M_IP6MADDR, M_NOWAIT); 1618 if (!imm) { 1619 *errorp = ENOBUFS; 1620 return NULL; 1621 } 1622 imm->i6mm_maddr = in6_addmulti(addr, ifp, errorp); 1623 if (!imm->i6mm_maddr) { 1624 /* *errorp is alrady set */ 1625 free(imm, M_IP6MADDR); 1626 return NULL; 1627 } 1628 return imm; 1629 } 1630 1631 int 1632 in6_leavegroup(imm) 1633 struct in6_multi_mship *imm; 1634 { 1635 1636 if (imm->i6mm_maddr) 1637 in6_delmulti(imm->i6mm_maddr); 1638 free(imm, M_IP6MADDR); 1639 return 0; 1640 } 1641 1642 /* 1643 * Find an IPv6 interface link-local address specific to an interface. 1644 */ 1645 struct in6_ifaddr * 1646 in6ifa_ifpforlinklocal(ifp, ignoreflags) 1647 struct ifnet *ifp; 1648 int ignoreflags; 1649 { 1650 struct ifaddr *ifa; 1651 1652 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 1653 if (ifa->ifa_addr == NULL) 1654 continue; /* just for safety */ 1655 if (ifa->ifa_addr->sa_family != AF_INET6) 1656 continue; 1657 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) { 1658 if ((((struct in6_ifaddr *)ifa)->ia6_flags & 1659 ignoreflags) != 0) 1660 continue; 1661 break; 1662 } 1663 } 1664 1665 return ((struct in6_ifaddr *)ifa); 1666 } 1667 1668 1669 /* 1670 * find the internet address corresponding to a given interface and address. 1671 */ 1672 struct in6_ifaddr * 1673 in6ifa_ifpwithaddr(ifp, addr) 1674 struct ifnet *ifp; 1675 struct in6_addr *addr; 1676 { 1677 struct ifaddr *ifa; 1678 1679 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 1680 if (ifa->ifa_addr == NULL) 1681 continue; /* just for safety */ 1682 if (ifa->ifa_addr->sa_family != AF_INET6) 1683 continue; 1684 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa))) 1685 break; 1686 } 1687 1688 return ((struct in6_ifaddr *)ifa); 1689 } 1690 1691 /* 1692 * Convert IP6 address to printable (loggable) representation. 1693 */ 1694 static char digits[] = "0123456789abcdef"; 1695 static int ip6round = 0; 1696 char * 1697 ip6_sprintf(addr) 1698 const struct in6_addr *addr; 1699 { 1700 static char ip6buf[8][48]; 1701 int i; 1702 char *cp; 1703 const u_int16_t *a = (const u_int16_t *)addr; 1704 const u_int8_t *d; 1705 int dcolon = 0; 1706 1707 ip6round = (ip6round + 1) & 7; 1708 cp = ip6buf[ip6round]; 1709 1710 for (i = 0; i < 8; i++) { 1711 if (dcolon == 1) { 1712 if (*a == 0) { 1713 if (i == 7) 1714 *cp++ = ':'; 1715 a++; 1716 continue; 1717 } else 1718 dcolon = 2; 1719 } 1720 if (*a == 0) { 1721 if (dcolon == 0 && *(a + 1) == 0) { 1722 if (i == 0) 1723 *cp++ = ':'; 1724 *cp++ = ':'; 1725 dcolon = 1; 1726 } else { 1727 *cp++ = '0'; 1728 *cp++ = ':'; 1729 } 1730 a++; 1731 continue; 1732 } 1733 d = (const u_char *)a; 1734 *cp++ = digits[*d >> 4]; 1735 *cp++ = digits[*d++ & 0xf]; 1736 *cp++ = digits[*d >> 4]; 1737 *cp++ = digits[*d & 0xf]; 1738 *cp++ = ':'; 1739 a++; 1740 } 1741 *--cp = 0; 1742 return (ip6buf[ip6round]); 1743 } 1744 1745 int 1746 in6_localaddr(in6) 1747 struct in6_addr *in6; 1748 { 1749 struct in6_ifaddr *ia; 1750 1751 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6)) 1752 return 1; 1753 1754 for (ia = in6_ifaddr; ia; ia = ia->ia_next) { 1755 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr, 1756 &ia->ia_prefixmask.sin6_addr)) { 1757 return 1; 1758 } 1759 } 1760 1761 return (0); 1762 } 1763 1764 int 1765 in6_is_addr_deprecated(sa6) 1766 struct sockaddr_in6 *sa6; 1767 { 1768 struct in6_ifaddr *ia; 1769 1770 for (ia = in6_ifaddr; ia; ia = ia->ia_next) { 1771 if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, 1772 &sa6->sin6_addr) && 1773 (ia->ia6_flags & IN6_IFF_DEPRECATED) != 0) 1774 return (1); /* true */ 1775 1776 /* XXX: do we still have to go thru the rest of the list? */ 1777 } 1778 1779 return (0); /* false */ 1780 } 1781 1782 /* 1783 * return length of part which dst and src are equal 1784 * hard coding... 1785 */ 1786 int 1787 in6_matchlen(src, dst) 1788 struct in6_addr *src, *dst; 1789 { 1790 int match = 0; 1791 u_char *s = (u_char *)src, *d = (u_char *)dst; 1792 u_char *lim = s + 16, r; 1793 1794 while (s < lim) 1795 if ((r = (*d++ ^ *s++)) != 0) { 1796 while (r < 128) { 1797 match++; 1798 r <<= 1; 1799 } 1800 break; 1801 } else 1802 match += 8; 1803 return match; 1804 } 1805 1806 /* XXX: to be scope conscious */ 1807 int 1808 in6_are_prefix_equal(p1, p2, len) 1809 struct in6_addr *p1, *p2; 1810 int len; 1811 { 1812 int bytelen, bitlen; 1813 1814 /* sanity check */ 1815 if (0 > len || len > 128) { 1816 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n", 1817 len); 1818 return (0); 1819 } 1820 1821 bytelen = len / 8; 1822 bitlen = len % 8; 1823 1824 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen)) 1825 return (0); 1826 if (bitlen != 0 && 1827 p1->s6_addr[bytelen] >> (8 - bitlen) != 1828 p2->s6_addr[bytelen] >> (8 - bitlen)) 1829 return (0); 1830 1831 return (1); 1832 } 1833 1834 void 1835 in6_prefixlen2mask(maskp, len) 1836 struct in6_addr *maskp; 1837 int len; 1838 { 1839 u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff}; 1840 int bytelen, bitlen, i; 1841 1842 /* sanity check */ 1843 if (0 > len || len > 128) { 1844 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n", 1845 len); 1846 return; 1847 } 1848 1849 bzero(maskp, sizeof(*maskp)); 1850 bytelen = len / 8; 1851 bitlen = len % 8; 1852 for (i = 0; i < bytelen; i++) 1853 maskp->s6_addr[i] = 0xff; 1854 if (bitlen) 1855 maskp->s6_addr[bytelen] = maskarray[bitlen - 1]; 1856 } 1857 1858 /* 1859 * return the best address out of the same scope. if no address was 1860 * found, return the first valid address from designated IF. 1861 */ 1862 struct in6_ifaddr * 1863 in6_ifawithifp(ifp, dst) 1864 struct ifnet *ifp; 1865 struct in6_addr *dst; 1866 { 1867 int dst_scope = in6_addrscope(dst), blen = -1, tlen; 1868 struct ifaddr *ifa; 1869 struct in6_ifaddr *besta = 0; 1870 struct in6_ifaddr *dep[2]; /* last-resort: deprecated */ 1871 1872 dep[0] = dep[1] = NULL; 1873 1874 /* 1875 * We first look for addresses in the same scope. 1876 * If there is one, return it. 1877 * If two or more, return one which matches the dst longest. 1878 * If none, return one of global addresses assigned other ifs. 1879 */ 1880 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 1881 if (ifa->ifa_addr->sa_family != AF_INET6) 1882 continue; 1883 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST) 1884 continue; /* XXX: is there any case to allow anycast? */ 1885 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY) 1886 continue; /* don't use this interface */ 1887 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED) 1888 continue; 1889 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) { 1890 if (ip6_use_deprecated) 1891 dep[0] = (struct in6_ifaddr *)ifa; 1892 continue; 1893 } 1894 1895 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) { 1896 /* 1897 * call in6_matchlen() as few as possible 1898 */ 1899 if (besta) { 1900 if (blen == -1) 1901 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst); 1902 tlen = in6_matchlen(IFA_IN6(ifa), dst); 1903 if (tlen > blen) { 1904 blen = tlen; 1905 besta = (struct in6_ifaddr *)ifa; 1906 } 1907 } else 1908 besta = (struct in6_ifaddr *)ifa; 1909 } 1910 } 1911 if (besta) 1912 return (besta); 1913 1914 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 1915 if (ifa->ifa_addr->sa_family != AF_INET6) 1916 continue; 1917 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST) 1918 continue; /* XXX: is there any case to allow anycast? */ 1919 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY) 1920 continue; /* don't use this interface */ 1921 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED) 1922 continue; 1923 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) { 1924 if (ip6_use_deprecated) 1925 dep[1] = (struct in6_ifaddr *)ifa; 1926 continue; 1927 } 1928 1929 return (struct in6_ifaddr *)ifa; 1930 } 1931 1932 /* use the last-resort values, that are, deprecated addresses */ 1933 if (dep[0]) 1934 return dep[0]; 1935 if (dep[1]) 1936 return dep[1]; 1937 1938 return NULL; 1939 } 1940 1941 /* 1942 * perform DAD when interface becomes IFF_UP. 1943 */ 1944 void 1945 in6_if_up(ifp) 1946 struct ifnet *ifp; 1947 { 1948 struct ifaddr *ifa; 1949 struct in6_ifaddr *ia; 1950 int dad_delay; /* delay ticks before DAD output */ 1951 1952 /* 1953 * special cases, like 6to4, are handled in in6_ifattach 1954 */ 1955 in6_ifattach(ifp, NULL); 1956 1957 dad_delay = 0; 1958 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 1959 if (ifa->ifa_addr->sa_family != AF_INET6) 1960 continue; 1961 ia = (struct in6_ifaddr *)ifa; 1962 if (ia->ia6_flags & IN6_IFF_TENTATIVE) 1963 nd6_dad_start(ifa, &dad_delay); 1964 } 1965 } 1966 1967 int 1968 in6if_do_dad(ifp) 1969 struct ifnet *ifp; 1970 { 1971 if ((ifp->if_flags & IFF_LOOPBACK) != 0) 1972 return (0); 1973 1974 switch (ifp->if_type) { 1975 #ifdef IFT_DUMMY 1976 case IFT_DUMMY: 1977 #endif 1978 case IFT_FAITH: 1979 /* 1980 * These interfaces do not have the IFF_LOOPBACK flag, 1981 * but loop packets back. We do not have to do DAD on such 1982 * interfaces. We should even omit it, because loop-backed 1983 * NS would confuse the DAD procedure. 1984 */ 1985 return (0); 1986 default: 1987 /* 1988 * Our DAD routine requires the interface up and running. 1989 * However, some interfaces can be up before the RUNNING 1990 * status. Additionaly, users may try to assign addresses 1991 * before the interface becomes up (or running). 1992 * We simply skip DAD in such a case as a work around. 1993 * XXX: we should rather mark "tentative" on such addresses, 1994 * and do DAD after the interface becomes ready. 1995 */ 1996 if (!((ifp->if_flags & IFF_UP) && 1997 (ifp->if_drv_flags & IFF_DRV_RUNNING))) 1998 return (0); 1999 2000 return (1); 2001 } 2002 } 2003 2004 /* 2005 * Calculate max IPv6 MTU through all the interfaces and store it 2006 * to in6_maxmtu. 2007 */ 2008 void 2009 in6_setmaxmtu() 2010 { 2011 unsigned long maxmtu = 0; 2012 struct ifnet *ifp; 2013 2014 IFNET_RLOCK(); 2015 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) { 2016 /* this function can be called during ifnet initialization */ 2017 if (!ifp->if_afdata[AF_INET6]) 2018 continue; 2019 if ((ifp->if_flags & IFF_LOOPBACK) == 0 && 2020 IN6_LINKMTU(ifp) > maxmtu) 2021 maxmtu = IN6_LINKMTU(ifp); 2022 } 2023 IFNET_RUNLOCK(); 2024 if (maxmtu) /* update only when maxmtu is positive */ 2025 in6_maxmtu = maxmtu; 2026 } 2027 2028 void * 2029 in6_domifattach(ifp) 2030 struct ifnet *ifp; 2031 { 2032 struct in6_ifextra *ext; 2033 2034 ext = (struct in6_ifextra *)malloc(sizeof(*ext), M_IFADDR, M_WAITOK); 2035 bzero(ext, sizeof(*ext)); 2036 2037 ext->in6_ifstat = (struct in6_ifstat *)malloc(sizeof(struct in6_ifstat), 2038 M_IFADDR, M_WAITOK); 2039 bzero(ext->in6_ifstat, sizeof(*ext->in6_ifstat)); 2040 2041 ext->icmp6_ifstat = 2042 (struct icmp6_ifstat *)malloc(sizeof(struct icmp6_ifstat), 2043 M_IFADDR, M_WAITOK); 2044 bzero(ext->icmp6_ifstat, sizeof(*ext->icmp6_ifstat)); 2045 2046 ext->nd_ifinfo = nd6_ifattach(ifp); 2047 ext->scope6_id = scope6_ifattach(ifp); 2048 return ext; 2049 } 2050 2051 void 2052 in6_domifdetach(ifp, aux) 2053 struct ifnet *ifp; 2054 void *aux; 2055 { 2056 struct in6_ifextra *ext = (struct in6_ifextra *)aux; 2057 2058 scope6_ifdetach(ext->scope6_id); 2059 nd6_ifdetach(ext->nd_ifinfo); 2060 free(ext->in6_ifstat, M_IFADDR); 2061 free(ext->icmp6_ifstat, M_IFADDR); 2062 free(ext, M_IFADDR); 2063 } 2064 2065 /* 2066 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be 2067 * v4 mapped addr or v4 compat addr 2068 */ 2069 void 2070 in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6) 2071 { 2072 bzero(sin, sizeof(*sin)); 2073 sin->sin_len = sizeof(struct sockaddr_in); 2074 sin->sin_family = AF_INET; 2075 sin->sin_port = sin6->sin6_port; 2076 sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3]; 2077 } 2078 2079 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */ 2080 void 2081 in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6) 2082 { 2083 bzero(sin6, sizeof(*sin6)); 2084 sin6->sin6_len = sizeof(struct sockaddr_in6); 2085 sin6->sin6_family = AF_INET6; 2086 sin6->sin6_port = sin->sin_port; 2087 sin6->sin6_addr.s6_addr32[0] = 0; 2088 sin6->sin6_addr.s6_addr32[1] = 0; 2089 sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP; 2090 sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr; 2091 } 2092 2093 /* Convert sockaddr_in6 into sockaddr_in. */ 2094 void 2095 in6_sin6_2_sin_in_sock(struct sockaddr *nam) 2096 { 2097 struct sockaddr_in *sin_p; 2098 struct sockaddr_in6 sin6; 2099 2100 /* 2101 * Save original sockaddr_in6 addr and convert it 2102 * to sockaddr_in. 2103 */ 2104 sin6 = *(struct sockaddr_in6 *)nam; 2105 sin_p = (struct sockaddr_in *)nam; 2106 in6_sin6_2_sin(sin_p, &sin6); 2107 } 2108 2109 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */ 2110 void 2111 in6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam) 2112 { 2113 struct sockaddr_in *sin_p; 2114 struct sockaddr_in6 *sin6_p; 2115 2116 MALLOC(sin6_p, struct sockaddr_in6 *, sizeof *sin6_p, M_SONAME, 2117 M_WAITOK); 2118 sin_p = (struct sockaddr_in *)*nam; 2119 in6_sin_2_v4mapsin6(sin_p, sin6_p); 2120 FREE(*nam, M_SONAME); 2121 *nam = (struct sockaddr *)sin6_p; 2122 } 2123