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