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