1 /*- 2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the project nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $KAME: in6.c,v 1.259 2002/01/21 11:37:50 keiichi Exp $ 30 */ 31 32 /*- 33 * Copyright (c) 1982, 1986, 1991, 1993 34 * The Regents of the University of California. All rights reserved. 35 * 36 * Redistribution and use in source and binary forms, with or without 37 * modification, are permitted provided that the following conditions 38 * are met: 39 * 1. Redistributions of source code must retain the above copyright 40 * notice, this list of conditions and the following disclaimer. 41 * 2. Redistributions in binary form must reproduce the above copyright 42 * notice, this list of conditions and the following disclaimer in the 43 * documentation and/or other materials provided with the distribution. 44 * 4. Neither the name of the University nor the names of its contributors 45 * may be used to endorse or promote products derived from this software 46 * without specific prior written permission. 47 * 48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 58 * SUCH DAMAGE. 59 * 60 * @(#)in.c 8.2 (Berkeley) 11/15/93 61 */ 62 63 #include <sys/cdefs.h> 64 __FBSDID("$FreeBSD$"); 65 66 #include "opt_inet.h" 67 #include "opt_inet6.h" 68 69 #include <sys/param.h> 70 #include <sys/errno.h> 71 #include <sys/jail.h> 72 #include <sys/malloc.h> 73 #include <sys/socket.h> 74 #include <sys/socketvar.h> 75 #include <sys/sockio.h> 76 #include <sys/systm.h> 77 #include <sys/priv.h> 78 #include <sys/proc.h> 79 #include <sys/time.h> 80 #include <sys/kernel.h> 81 #include <sys/syslog.h> 82 83 #include <net/if.h> 84 #include <net/if_var.h> 85 #include <net/if_types.h> 86 #include <net/route.h> 87 #include <net/if_dl.h> 88 #include <net/vnet.h> 89 90 #include <netinet/in.h> 91 #include <netinet/in_var.h> 92 #include <net/if_llatbl.h> 93 #include <netinet/if_ether.h> 94 #include <netinet/in_systm.h> 95 #include <netinet/ip.h> 96 #include <netinet/in_pcb.h> 97 98 #include <netinet/ip6.h> 99 #include <netinet6/ip6_var.h> 100 #include <netinet6/nd6.h> 101 #include <netinet6/mld6_var.h> 102 #include <netinet6/ip6_mroute.h> 103 #include <netinet6/in6_ifattach.h> 104 #include <netinet6/scope6_var.h> 105 #include <netinet6/in6_pcb.h> 106 107 /* 108 * Definitions of some costant IP6 addresses. 109 */ 110 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT; 111 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT; 112 const struct in6_addr in6addr_nodelocal_allnodes = 113 IN6ADDR_NODELOCAL_ALLNODES_INIT; 114 const struct in6_addr in6addr_linklocal_allnodes = 115 IN6ADDR_LINKLOCAL_ALLNODES_INIT; 116 const struct in6_addr in6addr_linklocal_allrouters = 117 IN6ADDR_LINKLOCAL_ALLROUTERS_INIT; 118 const struct in6_addr in6addr_linklocal_allv2routers = 119 IN6ADDR_LINKLOCAL_ALLV2ROUTERS_INIT; 120 121 const struct in6_addr in6mask0 = IN6MASK0; 122 const struct in6_addr in6mask32 = IN6MASK32; 123 const struct in6_addr in6mask64 = IN6MASK64; 124 const struct in6_addr in6mask96 = IN6MASK96; 125 const struct in6_addr in6mask128 = IN6MASK128; 126 127 const struct sockaddr_in6 sa6_any = 128 { sizeof(sa6_any), AF_INET6, 0, 0, IN6ADDR_ANY_INIT, 0 }; 129 130 static int in6_lifaddr_ioctl __P((struct socket *, u_long, caddr_t, 131 struct ifnet *, struct thread *)); 132 static int in6_ifinit __P((struct ifnet *, struct in6_ifaddr *, 133 struct sockaddr_in6 *, int)); 134 static void in6_unlink_ifa(struct in6_ifaddr *, struct ifnet *); 135 136 int (*faithprefix_p)(struct in6_addr *); 137 138 139 140 int 141 in6_mask2len(struct in6_addr *mask, u_char *lim0) 142 { 143 int x = 0, y; 144 u_char *lim = lim0, *p; 145 146 /* ignore the scope_id part */ 147 if (lim0 == NULL || lim0 - (u_char *)mask > sizeof(*mask)) 148 lim = (u_char *)mask + sizeof(*mask); 149 for (p = (u_char *)mask; p < lim; x++, p++) { 150 if (*p != 0xff) 151 break; 152 } 153 y = 0; 154 if (p < lim) { 155 for (y = 0; y < 8; y++) { 156 if ((*p & (0x80 >> y)) == 0) 157 break; 158 } 159 } 160 161 /* 162 * when the limit pointer is given, do a stricter check on the 163 * remaining bits. 164 */ 165 if (p < lim) { 166 if (y != 0 && (*p & (0x00ff >> y)) != 0) 167 return (-1); 168 for (p = p + 1; p < lim; p++) 169 if (*p != 0) 170 return (-1); 171 } 172 173 return x * 8 + y; 174 } 175 176 #define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa)) 177 #define ia62ifa(ia6) (&((ia6)->ia_ifa)) 178 179 int 180 in6_control(struct socket *so, u_long cmd, caddr_t data, 181 struct ifnet *ifp, struct thread *td) 182 { 183 struct in6_ifreq *ifr = (struct in6_ifreq *)data; 184 struct in6_ifaddr *ia = NULL; 185 struct in6_aliasreq *ifra = (struct in6_aliasreq *)data; 186 struct sockaddr_in6 *sa6; 187 int error; 188 189 switch (cmd) { 190 case SIOCGETSGCNT_IN6: 191 case SIOCGETMIFCNT_IN6: 192 return (mrt6_ioctl ? mrt6_ioctl(cmd, data) : EOPNOTSUPP); 193 } 194 195 switch(cmd) { 196 case SIOCAADDRCTL_POLICY: 197 case SIOCDADDRCTL_POLICY: 198 if (td != NULL) { 199 error = priv_check(td, PRIV_NETINET_ADDRCTRL6); 200 if (error) 201 return (error); 202 } 203 return (in6_src_ioctl(cmd, data)); 204 } 205 206 if (ifp == NULL) 207 return (EOPNOTSUPP); 208 209 switch (cmd) { 210 case SIOCSNDFLUSH_IN6: 211 case SIOCSPFXFLUSH_IN6: 212 case SIOCSRTRFLUSH_IN6: 213 case SIOCSDEFIFACE_IN6: 214 case SIOCSIFINFO_FLAGS: 215 case SIOCSIFINFO_IN6: 216 if (td != NULL) { 217 error = priv_check(td, PRIV_NETINET_ND6); 218 if (error) 219 return (error); 220 } 221 /* FALLTHROUGH */ 222 case OSIOCGIFINFO_IN6: 223 case SIOCGIFINFO_IN6: 224 case SIOCGDRLST_IN6: 225 case SIOCGPRLST_IN6: 226 case SIOCGNBRINFO_IN6: 227 case SIOCGDEFIFACE_IN6: 228 return (nd6_ioctl(cmd, data, ifp)); 229 } 230 231 switch (cmd) { 232 case SIOCSIFPREFIX_IN6: 233 case SIOCDIFPREFIX_IN6: 234 case SIOCAIFPREFIX_IN6: 235 case SIOCCIFPREFIX_IN6: 236 case SIOCSGIFPREFIX_IN6: 237 case SIOCGIFPREFIX_IN6: 238 log(LOG_NOTICE, 239 "prefix ioctls are now invalidated. " 240 "please use ifconfig.\n"); 241 return (EOPNOTSUPP); 242 } 243 244 switch (cmd) { 245 case SIOCSSCOPE6: 246 if (td != NULL) { 247 error = priv_check(td, PRIV_NETINET_SCOPE6); 248 if (error) 249 return (error); 250 } 251 return (scope6_set(ifp, 252 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id)); 253 case SIOCGSCOPE6: 254 return (scope6_get(ifp, 255 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id)); 256 case SIOCGSCOPE6DEF: 257 return (scope6_get_default((struct scope6_id *) 258 ifr->ifr_ifru.ifru_scope_id)); 259 } 260 261 switch (cmd) { 262 case SIOCALIFADDR: 263 if (td != NULL) { 264 error = priv_check(td, PRIV_NET_ADDIFADDR); 265 if (error) 266 return (error); 267 } 268 return in6_lifaddr_ioctl(so, cmd, data, ifp, td); 269 270 case SIOCDLIFADDR: 271 if (td != NULL) { 272 error = priv_check(td, PRIV_NET_DELIFADDR); 273 if (error) 274 return (error); 275 } 276 /* FALLTHROUGH */ 277 case SIOCGLIFADDR: 278 return in6_lifaddr_ioctl(so, cmd, data, ifp, td); 279 } 280 281 /* 282 * Find address for this interface, if it exists. 283 * 284 * In netinet code, we have checked ifra_addr in SIOCSIF*ADDR operation 285 * only, and used the first interface address as the target of other 286 * operations (without checking ifra_addr). This was because netinet 287 * code/API assumed at most 1 interface address per interface. 288 * Since IPv6 allows a node to assign multiple addresses 289 * on a single interface, we almost always look and check the 290 * presence of ifra_addr, and reject invalid ones here. 291 * It also decreases duplicated code among SIOC*_IN6 operations. 292 */ 293 switch (cmd) { 294 case SIOCAIFADDR_IN6: 295 case SIOCSIFPHYADDR_IN6: 296 sa6 = &ifra->ifra_addr; 297 break; 298 case SIOCSIFADDR_IN6: 299 case SIOCGIFADDR_IN6: 300 case SIOCSIFDSTADDR_IN6: 301 case SIOCSIFNETMASK_IN6: 302 case SIOCGIFDSTADDR_IN6: 303 case SIOCGIFNETMASK_IN6: 304 case SIOCDIFADDR_IN6: 305 case SIOCGIFPSRCADDR_IN6: 306 case SIOCGIFPDSTADDR_IN6: 307 case SIOCGIFAFLAG_IN6: 308 case SIOCSNDFLUSH_IN6: 309 case SIOCSPFXFLUSH_IN6: 310 case SIOCSRTRFLUSH_IN6: 311 case SIOCGIFALIFETIME_IN6: 312 case SIOCSIFALIFETIME_IN6: 313 case SIOCGIFSTAT_IN6: 314 case SIOCGIFSTAT_ICMP6: 315 sa6 = &ifr->ifr_addr; 316 break; 317 default: 318 sa6 = NULL; 319 break; 320 } 321 if (sa6 && sa6->sin6_family == AF_INET6) { 322 if (sa6->sin6_scope_id != 0) 323 error = sa6_embedscope(sa6, 0); 324 else 325 error = in6_setscope(&sa6->sin6_addr, ifp, NULL); 326 if (error != 0) 327 return (error); 328 if (td != NULL && (error = prison_check_ip6(td->td_ucred, 329 &sa6->sin6_addr)) != 0) 330 return (error); 331 ia = in6ifa_ifpwithaddr(ifp, &sa6->sin6_addr); 332 } else 333 ia = NULL; 334 335 switch (cmd) { 336 case SIOCSIFADDR_IN6: 337 case SIOCSIFDSTADDR_IN6: 338 case SIOCSIFNETMASK_IN6: 339 /* 340 * Since IPv6 allows a node to assign multiple addresses 341 * on a single interface, SIOCSIFxxx ioctls are deprecated. 342 */ 343 /* we decided to obsolete this command (20000704) */ 344 error = EINVAL; 345 goto out; 346 347 case SIOCDIFADDR_IN6: 348 /* 349 * for IPv4, we look for existing in_ifaddr here to allow 350 * "ifconfig if0 delete" to remove the first IPv4 address on 351 * the interface. For IPv6, as the spec allows multiple 352 * interface address from the day one, we consider "remove the 353 * first one" semantics to be not preferable. 354 */ 355 if (ia == NULL) { 356 error = EADDRNOTAVAIL; 357 goto out; 358 } 359 /* FALLTHROUGH */ 360 case SIOCAIFADDR_IN6: 361 /* 362 * We always require users to specify a valid IPv6 address for 363 * the corresponding operation. 364 */ 365 if (ifra->ifra_addr.sin6_family != AF_INET6 || 366 ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6)) { 367 error = EAFNOSUPPORT; 368 goto out; 369 } 370 371 if (td != NULL) { 372 error = priv_check(td, (cmd == SIOCDIFADDR_IN6) ? 373 PRIV_NET_DELIFADDR : PRIV_NET_ADDIFADDR); 374 if (error) 375 goto out; 376 } 377 break; 378 379 case SIOCGIFADDR_IN6: 380 /* This interface is basically deprecated. use SIOCGIFCONF. */ 381 /* FALLTHROUGH */ 382 case SIOCGIFAFLAG_IN6: 383 case SIOCGIFNETMASK_IN6: 384 case SIOCGIFDSTADDR_IN6: 385 case SIOCGIFALIFETIME_IN6: 386 /* must think again about its semantics */ 387 if (ia == NULL) { 388 error = EADDRNOTAVAIL; 389 goto out; 390 } 391 break; 392 393 case SIOCSIFALIFETIME_IN6: 394 { 395 struct in6_addrlifetime *lt; 396 397 if (td != NULL) { 398 error = priv_check(td, PRIV_NETINET_ALIFETIME6); 399 if (error) 400 goto out; 401 } 402 if (ia == NULL) { 403 error = EADDRNOTAVAIL; 404 goto out; 405 } 406 /* sanity for overflow - beware unsigned */ 407 lt = &ifr->ifr_ifru.ifru_lifetime; 408 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME && 409 lt->ia6t_vltime + time_second < time_second) { 410 error = EINVAL; 411 goto out; 412 } 413 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME && 414 lt->ia6t_pltime + time_second < time_second) { 415 error = EINVAL; 416 goto out; 417 } 418 break; 419 } 420 } 421 422 switch (cmd) { 423 case SIOCGIFADDR_IN6: 424 ifr->ifr_addr = ia->ia_addr; 425 if ((error = sa6_recoverscope(&ifr->ifr_addr)) != 0) 426 goto out; 427 break; 428 429 case SIOCGIFDSTADDR_IN6: 430 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) { 431 error = EINVAL; 432 goto out; 433 } 434 /* 435 * XXX: should we check if ifa_dstaddr is NULL and return 436 * an error? 437 */ 438 ifr->ifr_dstaddr = ia->ia_dstaddr; 439 if ((error = sa6_recoverscope(&ifr->ifr_dstaddr)) != 0) 440 goto out; 441 break; 442 443 case SIOCGIFNETMASK_IN6: 444 ifr->ifr_addr = ia->ia_prefixmask; 445 break; 446 447 case SIOCGIFAFLAG_IN6: 448 ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags; 449 break; 450 451 case SIOCGIFSTAT_IN6: 452 if (ifp == NULL) { 453 error = EINVAL; 454 goto out; 455 } 456 bzero(&ifr->ifr_ifru.ifru_stat, 457 sizeof(ifr->ifr_ifru.ifru_stat)); 458 ifr->ifr_ifru.ifru_stat = 459 *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->in6_ifstat; 460 break; 461 462 case SIOCGIFSTAT_ICMP6: 463 if (ifp == NULL) { 464 error = EINVAL; 465 goto out; 466 } 467 bzero(&ifr->ifr_ifru.ifru_icmp6stat, 468 sizeof(ifr->ifr_ifru.ifru_icmp6stat)); 469 ifr->ifr_ifru.ifru_icmp6stat = 470 *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->icmp6_ifstat; 471 break; 472 473 case SIOCGIFALIFETIME_IN6: 474 ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime; 475 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) { 476 time_t maxexpire; 477 struct in6_addrlifetime *retlt = 478 &ifr->ifr_ifru.ifru_lifetime; 479 480 /* 481 * XXX: adjust expiration time assuming time_t is 482 * signed. 483 */ 484 maxexpire = (-1) & 485 ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1)); 486 if (ia->ia6_lifetime.ia6t_vltime < 487 maxexpire - ia->ia6_updatetime) { 488 retlt->ia6t_expire = ia->ia6_updatetime + 489 ia->ia6_lifetime.ia6t_vltime; 490 } else 491 retlt->ia6t_expire = maxexpire; 492 } 493 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) { 494 time_t maxexpire; 495 struct in6_addrlifetime *retlt = 496 &ifr->ifr_ifru.ifru_lifetime; 497 498 /* 499 * XXX: adjust expiration time assuming time_t is 500 * signed. 501 */ 502 maxexpire = (-1) & 503 ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1)); 504 if (ia->ia6_lifetime.ia6t_pltime < 505 maxexpire - ia->ia6_updatetime) { 506 retlt->ia6t_preferred = ia->ia6_updatetime + 507 ia->ia6_lifetime.ia6t_pltime; 508 } else 509 retlt->ia6t_preferred = maxexpire; 510 } 511 break; 512 513 case SIOCSIFALIFETIME_IN6: 514 ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime; 515 /* for sanity */ 516 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) { 517 ia->ia6_lifetime.ia6t_expire = 518 time_second + ia->ia6_lifetime.ia6t_vltime; 519 } else 520 ia->ia6_lifetime.ia6t_expire = 0; 521 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) { 522 ia->ia6_lifetime.ia6t_preferred = 523 time_second + ia->ia6_lifetime.ia6t_pltime; 524 } else 525 ia->ia6_lifetime.ia6t_preferred = 0; 526 break; 527 528 case SIOCAIFADDR_IN6: 529 { 530 int i; 531 struct nd_prefixctl pr0; 532 struct nd_prefix *pr; 533 534 /* 535 * first, make or update the interface address structure, 536 * and link it to the list. 537 */ 538 if ((error = in6_update_ifa(ifp, ifra, ia, 0)) != 0) 539 goto out; 540 if (ia != NULL) 541 ifa_free(&ia->ia_ifa); 542 if ((ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr)) 543 == NULL) { 544 /* 545 * this can happen when the user specify the 0 valid 546 * lifetime. 547 */ 548 break; 549 } 550 551 /* 552 * then, make the prefix on-link on the interface. 553 * XXX: we'd rather create the prefix before the address, but 554 * we need at least one address to install the corresponding 555 * interface route, so we configure the address first. 556 */ 557 558 /* 559 * convert mask to prefix length (prefixmask has already 560 * been validated in in6_update_ifa(). 561 */ 562 bzero(&pr0, sizeof(pr0)); 563 pr0.ndpr_ifp = ifp; 564 pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr, 565 NULL); 566 if (pr0.ndpr_plen == 128) { 567 break; /* we don't need to install a host route. */ 568 } 569 pr0.ndpr_prefix = ifra->ifra_addr; 570 /* apply the mask for safety. */ 571 for (i = 0; i < 4; i++) { 572 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &= 573 ifra->ifra_prefixmask.sin6_addr.s6_addr32[i]; 574 } 575 /* 576 * XXX: since we don't have an API to set prefix (not address) 577 * lifetimes, we just use the same lifetimes as addresses. 578 * The (temporarily) installed lifetimes can be overridden by 579 * later advertised RAs (when accept_rtadv is non 0), which is 580 * an intended behavior. 581 */ 582 pr0.ndpr_raf_onlink = 1; /* should be configurable? */ 583 pr0.ndpr_raf_auto = 584 ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0); 585 pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime; 586 pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime; 587 588 /* add the prefix if not yet. */ 589 if ((pr = nd6_prefix_lookup(&pr0)) == NULL) { 590 /* 591 * nd6_prelist_add will install the corresponding 592 * interface route. 593 */ 594 if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0) 595 goto out; 596 if (pr == NULL) { 597 log(LOG_ERR, "nd6_prelist_add succeeded but " 598 "no prefix\n"); 599 error = EINVAL; 600 goto out; 601 } 602 } 603 604 /* relate the address to the prefix */ 605 if (ia->ia6_ndpr == NULL) { 606 ia->ia6_ndpr = pr; 607 pr->ndpr_refcnt++; 608 609 /* 610 * If this is the first autoconf address from the 611 * prefix, create a temporary address as well 612 * (when required). 613 */ 614 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) && 615 V_ip6_use_tempaddr && pr->ndpr_refcnt == 1) { 616 int e; 617 if ((e = in6_tmpifadd(ia, 1, 0)) != 0) { 618 log(LOG_NOTICE, "in6_control: failed " 619 "to create a temporary address, " 620 "errno=%d\n", e); 621 } 622 } 623 } 624 625 /* 626 * this might affect the status of autoconfigured addresses, 627 * that is, this address might make other addresses detached. 628 */ 629 pfxlist_onlink_check(); 630 if (error == 0 && ia) 631 EVENTHANDLER_INVOKE(ifaddr_event, ifp); 632 break; 633 } 634 635 case SIOCDIFADDR_IN6: 636 { 637 struct nd_prefix *pr; 638 639 /* 640 * If the address being deleted is the only one that owns 641 * the corresponding prefix, expire the prefix as well. 642 * XXX: theoretically, we don't have to worry about such 643 * relationship, since we separate the address management 644 * and the prefix management. We do this, however, to provide 645 * as much backward compatibility as possible in terms of 646 * the ioctl operation. 647 * Note that in6_purgeaddr() will decrement ndpr_refcnt. 648 */ 649 pr = ia->ia6_ndpr; 650 in6_purgeaddr(&ia->ia_ifa); 651 if (pr && pr->ndpr_refcnt == 0) 652 prelist_remove(pr); 653 EVENTHANDLER_INVOKE(ifaddr_event, ifp); 654 break; 655 } 656 657 default: 658 if (ifp == NULL || ifp->if_ioctl == 0) { 659 error = EOPNOTSUPP; 660 goto out; 661 } 662 error = (*ifp->if_ioctl)(ifp, cmd, data); 663 goto out; 664 } 665 666 error = 0; 667 out: 668 if (ia != NULL) 669 ifa_free(&ia->ia_ifa); 670 return (error); 671 } 672 673 /* 674 * Update parameters of an IPv6 interface address. 675 * If necessary, a new entry is created and linked into address chains. 676 * This function is separated from in6_control(). 677 * XXX: should this be performed under splnet()? 678 */ 679 int 680 in6_update_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra, 681 struct in6_ifaddr *ia, int flags) 682 { 683 int error = 0, hostIsNew = 0, plen = -1; 684 struct sockaddr_in6 dst6; 685 struct in6_addrlifetime *lt; 686 struct in6_multi_mship *imm; 687 struct in6_multi *in6m_sol; 688 struct rtentry *rt; 689 int delay; 690 char ip6buf[INET6_ADDRSTRLEN]; 691 692 /* Validate parameters */ 693 if (ifp == NULL || ifra == NULL) /* this maybe redundant */ 694 return (EINVAL); 695 696 /* 697 * The destination address for a p2p link must have a family 698 * of AF_UNSPEC or AF_INET6. 699 */ 700 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 && 701 ifra->ifra_dstaddr.sin6_family != AF_INET6 && 702 ifra->ifra_dstaddr.sin6_family != AF_UNSPEC) 703 return (EAFNOSUPPORT); 704 /* 705 * validate ifra_prefixmask. don't check sin6_family, netmask 706 * does not carry fields other than sin6_len. 707 */ 708 if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6)) 709 return (EINVAL); 710 /* 711 * Because the IPv6 address architecture is classless, we require 712 * users to specify a (non 0) prefix length (mask) for a new address. 713 * We also require the prefix (when specified) mask is valid, and thus 714 * reject a non-consecutive mask. 715 */ 716 if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0) 717 return (EINVAL); 718 if (ifra->ifra_prefixmask.sin6_len != 0) { 719 plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr, 720 (u_char *)&ifra->ifra_prefixmask + 721 ifra->ifra_prefixmask.sin6_len); 722 if (plen <= 0) 723 return (EINVAL); 724 } else { 725 /* 726 * In this case, ia must not be NULL. We just use its prefix 727 * length. 728 */ 729 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); 730 } 731 /* 732 * If the destination address on a p2p interface is specified, 733 * and the address is a scoped one, validate/set the scope 734 * zone identifier. 735 */ 736 dst6 = ifra->ifra_dstaddr; 737 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) != 0 && 738 (dst6.sin6_family == AF_INET6)) { 739 struct in6_addr in6_tmp; 740 u_int32_t zoneid; 741 742 in6_tmp = dst6.sin6_addr; 743 if (in6_setscope(&in6_tmp, ifp, &zoneid)) 744 return (EINVAL); /* XXX: should be impossible */ 745 746 if (dst6.sin6_scope_id != 0) { 747 if (dst6.sin6_scope_id != zoneid) 748 return (EINVAL); 749 } else /* user omit to specify the ID. */ 750 dst6.sin6_scope_id = zoneid; 751 752 /* convert into the internal form */ 753 if (sa6_embedscope(&dst6, 0)) 754 return (EINVAL); /* XXX: should be impossible */ 755 } 756 /* 757 * The destination address can be specified only for a p2p or a 758 * loopback interface. If specified, the corresponding prefix length 759 * must be 128. 760 */ 761 if (ifra->ifra_dstaddr.sin6_family == AF_INET6) { 762 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) == 0) { 763 /* XXX: noisy message */ 764 nd6log((LOG_INFO, "in6_update_ifa: a destination can " 765 "be specified for a p2p or a loopback IF only\n")); 766 return (EINVAL); 767 } 768 if (plen != 128) { 769 nd6log((LOG_INFO, "in6_update_ifa: prefixlen should " 770 "be 128 when dstaddr is specified\n")); 771 return (EINVAL); 772 } 773 } 774 /* lifetime consistency check */ 775 lt = &ifra->ifra_lifetime; 776 if (lt->ia6t_pltime > lt->ia6t_vltime) 777 return (EINVAL); 778 if (lt->ia6t_vltime == 0) { 779 /* 780 * the following log might be noisy, but this is a typical 781 * configuration mistake or a tool's bug. 782 */ 783 nd6log((LOG_INFO, 784 "in6_update_ifa: valid lifetime is 0 for %s\n", 785 ip6_sprintf(ip6buf, &ifra->ifra_addr.sin6_addr))); 786 787 if (ia == NULL) 788 return (0); /* there's nothing to do */ 789 } 790 791 /* 792 * If this is a new address, allocate a new ifaddr and link it 793 * into chains. 794 */ 795 if (ia == NULL) { 796 hostIsNew = 1; 797 /* 798 * When in6_update_ifa() is called in a process of a received 799 * RA, it is called under an interrupt context. So, we should 800 * call malloc with M_NOWAIT. 801 */ 802 ia = (struct in6_ifaddr *) malloc(sizeof(*ia), M_IFADDR, 803 M_NOWAIT); 804 if (ia == NULL) 805 return (ENOBUFS); 806 bzero((caddr_t)ia, sizeof(*ia)); 807 ifa_init(&ia->ia_ifa); 808 LIST_INIT(&ia->ia6_memberships); 809 /* Initialize the address and masks, and put time stamp */ 810 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr; 811 ia->ia_addr.sin6_family = AF_INET6; 812 ia->ia_addr.sin6_len = sizeof(ia->ia_addr); 813 ia->ia6_createtime = time_second; 814 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) { 815 /* 816 * XXX: some functions expect that ifa_dstaddr is not 817 * NULL for p2p interfaces. 818 */ 819 ia->ia_ifa.ifa_dstaddr = 820 (struct sockaddr *)&ia->ia_dstaddr; 821 } else { 822 ia->ia_ifa.ifa_dstaddr = NULL; 823 } 824 ia->ia_ifa.ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask; 825 ia->ia_ifp = ifp; 826 ifa_ref(&ia->ia_ifa); /* if_addrhead */ 827 IF_ADDR_LOCK(ifp); 828 TAILQ_INSERT_TAIL(&ifp->if_addrhead, &ia->ia_ifa, ifa_link); 829 IF_ADDR_UNLOCK(ifp); 830 831 ifa_ref(&ia->ia_ifa); /* in6_ifaddrhead */ 832 IN6_IFADDR_WLOCK(); 833 TAILQ_INSERT_TAIL(&V_in6_ifaddrhead, ia, ia_link); 834 IN6_IFADDR_WUNLOCK(); 835 } 836 837 /* update timestamp */ 838 ia->ia6_updatetime = time_second; 839 840 /* set prefix mask */ 841 if (ifra->ifra_prefixmask.sin6_len) { 842 /* 843 * We prohibit changing the prefix length of an existing 844 * address, because 845 * + such an operation should be rare in IPv6, and 846 * + the operation would confuse prefix management. 847 */ 848 if (ia->ia_prefixmask.sin6_len && 849 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) { 850 nd6log((LOG_INFO, "in6_update_ifa: the prefix length of an" 851 " existing (%s) address should not be changed\n", 852 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr))); 853 error = EINVAL; 854 goto unlink; 855 } 856 ia->ia_prefixmask = ifra->ifra_prefixmask; 857 } 858 859 /* 860 * If a new destination address is specified, scrub the old one and 861 * install the new destination. Note that the interface must be 862 * p2p or loopback (see the check above.) 863 */ 864 if (dst6.sin6_family == AF_INET6 && 865 !IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr, &ia->ia_dstaddr.sin6_addr)) { 866 int e; 867 868 if ((ia->ia_flags & IFA_ROUTE) != 0 && 869 (e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST)) != 0) { 870 nd6log((LOG_ERR, "in6_update_ifa: failed to remove " 871 "a route to the old destination: %s\n", 872 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr))); 873 /* proceed anyway... */ 874 } else 875 ia->ia_flags &= ~IFA_ROUTE; 876 ia->ia_dstaddr = dst6; 877 } 878 879 /* 880 * Set lifetimes. We do not refer to ia6t_expire and ia6t_preferred 881 * to see if the address is deprecated or invalidated, but initialize 882 * these members for applications. 883 */ 884 ia->ia6_lifetime = ifra->ifra_lifetime; 885 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) { 886 ia->ia6_lifetime.ia6t_expire = 887 time_second + ia->ia6_lifetime.ia6t_vltime; 888 } else 889 ia->ia6_lifetime.ia6t_expire = 0; 890 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) { 891 ia->ia6_lifetime.ia6t_preferred = 892 time_second + ia->ia6_lifetime.ia6t_pltime; 893 } else 894 ia->ia6_lifetime.ia6t_preferred = 0; 895 896 /* reset the interface and routing table appropriately. */ 897 if ((error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew)) != 0) 898 goto unlink; 899 900 /* 901 * configure address flags. 902 */ 903 ia->ia6_flags = ifra->ifra_flags; 904 /* 905 * backward compatibility - if IN6_IFF_DEPRECATED is set from the 906 * userland, make it deprecated. 907 */ 908 if ((ifra->ifra_flags & IN6_IFF_DEPRECATED) != 0) { 909 ia->ia6_lifetime.ia6t_pltime = 0; 910 ia->ia6_lifetime.ia6t_preferred = time_second; 911 } 912 /* 913 * Make the address tentative before joining multicast addresses, 914 * so that corresponding MLD responses would not have a tentative 915 * source address. 916 */ 917 ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /* safety */ 918 if (hostIsNew && in6if_do_dad(ifp)) 919 ia->ia6_flags |= IN6_IFF_TENTATIVE; 920 921 /* DAD should be performed after ND6_IFF_IFDISABLED is cleared. */ 922 if (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) 923 ia->ia6_flags |= IN6_IFF_TENTATIVE; 924 925 /* 926 * We are done if we have simply modified an existing address. 927 */ 928 if (!hostIsNew) 929 return (error); 930 931 /* 932 * Beyond this point, we should call in6_purgeaddr upon an error, 933 * not just go to unlink. 934 */ 935 936 /* Join necessary multicast groups */ 937 in6m_sol = NULL; 938 if ((ifp->if_flags & IFF_MULTICAST) != 0) { 939 struct sockaddr_in6 mltaddr, mltmask; 940 struct in6_addr llsol; 941 942 /* join solicited multicast addr for new host id */ 943 bzero(&llsol, sizeof(struct in6_addr)); 944 llsol.s6_addr32[0] = IPV6_ADDR_INT32_MLL; 945 llsol.s6_addr32[1] = 0; 946 llsol.s6_addr32[2] = htonl(1); 947 llsol.s6_addr32[3] = ifra->ifra_addr.sin6_addr.s6_addr32[3]; 948 llsol.s6_addr8[12] = 0xff; 949 if ((error = in6_setscope(&llsol, ifp, NULL)) != 0) { 950 /* XXX: should not happen */ 951 log(LOG_ERR, "in6_update_ifa: " 952 "in6_setscope failed\n"); 953 goto cleanup; 954 } 955 delay = 0; 956 if ((flags & IN6_IFAUPDATE_DADDELAY)) { 957 /* 958 * We need a random delay for DAD on the address 959 * being configured. It also means delaying 960 * transmission of the corresponding MLD report to 961 * avoid report collision. 962 * [RFC 4861, Section 6.3.7] 963 */ 964 delay = arc4random() % 965 (MAX_RTR_SOLICITATION_DELAY * hz); 966 } 967 imm = in6_joingroup(ifp, &llsol, &error, delay); 968 if (imm == NULL) { 969 nd6log((LOG_WARNING, 970 "in6_update_ifa: addmulti failed for " 971 "%s on %s (errno=%d)\n", 972 ip6_sprintf(ip6buf, &llsol), if_name(ifp), 973 error)); 974 goto cleanup; 975 } 976 LIST_INSERT_HEAD(&ia->ia6_memberships, 977 imm, i6mm_chain); 978 in6m_sol = imm->i6mm_maddr; 979 980 bzero(&mltmask, sizeof(mltmask)); 981 mltmask.sin6_len = sizeof(struct sockaddr_in6); 982 mltmask.sin6_family = AF_INET6; 983 mltmask.sin6_addr = in6mask32; 984 #define MLTMASK_LEN 4 /* mltmask's masklen (=32bit=4octet) */ 985 986 /* 987 * join link-local all-nodes address 988 */ 989 bzero(&mltaddr, sizeof(mltaddr)); 990 mltaddr.sin6_len = sizeof(struct sockaddr_in6); 991 mltaddr.sin6_family = AF_INET6; 992 mltaddr.sin6_addr = in6addr_linklocal_allnodes; 993 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 994 0) 995 goto cleanup; /* XXX: should not fail */ 996 997 /* 998 * XXX: do we really need this automatic routes? 999 * We should probably reconsider this stuff. Most applications 1000 * actually do not need the routes, since they usually specify 1001 * the outgoing interface. 1002 */ 1003 rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL); 1004 if (rt) { 1005 /* XXX: only works in !SCOPEDROUTING case. */ 1006 if (memcmp(&mltaddr.sin6_addr, 1007 &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr, 1008 MLTMASK_LEN)) { 1009 RTFREE_LOCKED(rt); 1010 rt = NULL; 1011 } 1012 } 1013 if (!rt) { 1014 error = rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr, 1015 (struct sockaddr *)&ia->ia_addr, 1016 (struct sockaddr *)&mltmask, RTF_UP, 1017 (struct rtentry **)0); 1018 if (error) 1019 goto cleanup; 1020 } else { 1021 RTFREE_LOCKED(rt); 1022 } 1023 1024 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0); 1025 if (!imm) { 1026 nd6log((LOG_WARNING, 1027 "in6_update_ifa: addmulti failed for " 1028 "%s on %s (errno=%d)\n", 1029 ip6_sprintf(ip6buf, &mltaddr.sin6_addr), 1030 if_name(ifp), error)); 1031 goto cleanup; 1032 } 1033 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain); 1034 1035 /* 1036 * join node information group address 1037 */ 1038 delay = 0; 1039 if ((flags & IN6_IFAUPDATE_DADDELAY)) { 1040 /* 1041 * The spec doesn't say anything about delay for this 1042 * group, but the same logic should apply. 1043 */ 1044 delay = arc4random() % 1045 (MAX_RTR_SOLICITATION_DELAY * hz); 1046 } 1047 if (in6_nigroup(ifp, NULL, -1, &mltaddr.sin6_addr) == 0) { 1048 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 1049 delay); /* XXX jinmei */ 1050 if (!imm) { 1051 nd6log((LOG_WARNING, "in6_update_ifa: " 1052 "addmulti failed for %s on %s " 1053 "(errno=%d)\n", 1054 ip6_sprintf(ip6buf, &mltaddr.sin6_addr), 1055 if_name(ifp), error)); 1056 /* XXX not very fatal, go on... */ 1057 } else { 1058 LIST_INSERT_HEAD(&ia->ia6_memberships, 1059 imm, i6mm_chain); 1060 } 1061 } 1062 1063 /* 1064 * join interface-local all-nodes address. 1065 * (ff01::1%ifN, and ff01::%ifN/32) 1066 */ 1067 mltaddr.sin6_addr = in6addr_nodelocal_allnodes; 1068 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) 1069 != 0) 1070 goto cleanup; /* XXX: should not fail */ 1071 /* XXX: again, do we really need the route? */ 1072 rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL); 1073 if (rt) { 1074 if (memcmp(&mltaddr.sin6_addr, 1075 &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr, 1076 MLTMASK_LEN)) { 1077 RTFREE_LOCKED(rt); 1078 rt = NULL; 1079 } 1080 } 1081 if (!rt) { 1082 error = rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr, 1083 (struct sockaddr *)&ia->ia_addr, 1084 (struct sockaddr *)&mltmask, RTF_UP, 1085 (struct rtentry **)0); 1086 if (error) 1087 goto cleanup; 1088 } else 1089 RTFREE_LOCKED(rt); 1090 1091 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0); 1092 if (!imm) { 1093 nd6log((LOG_WARNING, "in6_update_ifa: " 1094 "addmulti failed for %s on %s " 1095 "(errno=%d)\n", 1096 ip6_sprintf(ip6buf, &mltaddr.sin6_addr), 1097 if_name(ifp), error)); 1098 goto cleanup; 1099 } 1100 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain); 1101 #undef MLTMASK_LEN 1102 } 1103 1104 /* 1105 * Perform DAD, if needed. 1106 * XXX It may be of use, if we can administratively 1107 * disable DAD. 1108 */ 1109 if (in6if_do_dad(ifp) && ((ifra->ifra_flags & IN6_IFF_NODAD) == 0) && 1110 (ia->ia6_flags & IN6_IFF_TENTATIVE)) 1111 { 1112 int mindelay, maxdelay; 1113 1114 delay = 0; 1115 if ((flags & IN6_IFAUPDATE_DADDELAY)) { 1116 /* 1117 * We need to impose a delay before sending an NS 1118 * for DAD. Check if we also needed a delay for the 1119 * corresponding MLD message. If we did, the delay 1120 * should be larger than the MLD delay (this could be 1121 * relaxed a bit, but this simple logic is at least 1122 * safe). 1123 * XXX: Break data hiding guidelines and look at 1124 * state for the solicited multicast group. 1125 */ 1126 mindelay = 0; 1127 if (in6m_sol != NULL && 1128 in6m_sol->in6m_state == MLD_REPORTING_MEMBER) { 1129 mindelay = in6m_sol->in6m_timer; 1130 } 1131 maxdelay = MAX_RTR_SOLICITATION_DELAY * hz; 1132 if (maxdelay - mindelay == 0) 1133 delay = 0; 1134 else { 1135 delay = 1136 (arc4random() % (maxdelay - mindelay)) + 1137 mindelay; 1138 } 1139 } 1140 nd6_dad_start((struct ifaddr *)ia, delay); 1141 } 1142 1143 KASSERT(hostIsNew, ("in6_update_ifa: !hostIsNew")); 1144 ifa_free(&ia->ia_ifa); 1145 return (error); 1146 1147 unlink: 1148 /* 1149 * XXX: if a change of an existing address failed, keep the entry 1150 * anyway. 1151 */ 1152 if (hostIsNew) { 1153 in6_unlink_ifa(ia, ifp); 1154 ifa_free(&ia->ia_ifa); 1155 } 1156 return (error); 1157 1158 cleanup: 1159 KASSERT(hostIsNew, ("in6_update_ifa: cleanup: !hostIsNew")); 1160 ifa_free(&ia->ia_ifa); 1161 in6_purgeaddr(&ia->ia_ifa); 1162 return error; 1163 } 1164 1165 void 1166 in6_purgeaddr(struct ifaddr *ifa) 1167 { 1168 struct ifnet *ifp = ifa->ifa_ifp; 1169 struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa; 1170 struct in6_multi_mship *imm; 1171 struct sockaddr_in6 mltaddr, mltmask; 1172 struct rtentry rt0; 1173 struct sockaddr_dl gateway; 1174 struct sockaddr_in6 mask, addr; 1175 int plen, error; 1176 struct rtentry *rt; 1177 struct ifaddr *ifa0, *nifa; 1178 1179 /* 1180 * find another IPv6 address as the gateway for the 1181 * link-local and node-local all-nodes multicast 1182 * address routes 1183 */ 1184 IF_ADDR_LOCK(ifp); 1185 TAILQ_FOREACH_SAFE(ifa0, &ifp->if_addrhead, ifa_link, nifa) { 1186 if ((ifa0->ifa_addr->sa_family != AF_INET6) || 1187 memcmp(&satosin6(ifa0->ifa_addr)->sin6_addr, 1188 &ia->ia_addr.sin6_addr, 1189 sizeof(struct in6_addr)) == 0) 1190 continue; 1191 else 1192 break; 1193 } 1194 if (ifa0 != NULL) 1195 ifa_ref(ifa0); 1196 IF_ADDR_UNLOCK(ifp); 1197 1198 /* 1199 * Remove the loopback route to the interface address. 1200 * The check for the current setting of "nd6_useloopback" 1201 * is not needed. 1202 */ 1203 if (ia->ia_flags & IFA_RTSELF) { 1204 error = ifa_del_loopback_route((struct ifaddr *)ia, 1205 (struct sockaddr *)&ia->ia_addr); 1206 if (error == 0) 1207 ia->ia_flags &= ~IFA_RTSELF; 1208 } 1209 1210 /* stop DAD processing */ 1211 nd6_dad_stop(ifa); 1212 1213 IF_AFDATA_LOCK(ifp); 1214 lla_lookup(LLTABLE6(ifp), (LLE_DELETE | LLE_IFADDR), 1215 (struct sockaddr *)&ia->ia_addr); 1216 IF_AFDATA_UNLOCK(ifp); 1217 1218 /* 1219 * initialize for rtmsg generation 1220 */ 1221 bzero(&gateway, sizeof(gateway)); 1222 gateway.sdl_len = sizeof(gateway); 1223 gateway.sdl_family = AF_LINK; 1224 gateway.sdl_nlen = 0; 1225 gateway.sdl_alen = ifp->if_addrlen; 1226 /* */ 1227 bzero(&rt0, sizeof(rt0)); 1228 rt0.rt_gateway = (struct sockaddr *)&gateway; 1229 memcpy(&mask, &ia->ia_prefixmask, sizeof(ia->ia_prefixmask)); 1230 memcpy(&addr, &ia->ia_addr, sizeof(ia->ia_addr)); 1231 rt_mask(&rt0) = (struct sockaddr *)&mask; 1232 rt_key(&rt0) = (struct sockaddr *)&addr; 1233 rt0.rt_flags = RTF_HOST | RTF_STATIC; 1234 rt_newaddrmsg(RTM_DELETE, ifa, 0, &rt0); 1235 1236 /* 1237 * leave from multicast groups we have joined for the interface 1238 */ 1239 while ((imm = ia->ia6_memberships.lh_first) != NULL) { 1240 LIST_REMOVE(imm, i6mm_chain); 1241 in6_leavegroup(imm); 1242 } 1243 1244 /* 1245 * remove the link-local all-nodes address 1246 */ 1247 bzero(&mltmask, sizeof(mltmask)); 1248 mltmask.sin6_len = sizeof(struct sockaddr_in6); 1249 mltmask.sin6_family = AF_INET6; 1250 mltmask.sin6_addr = in6mask32; 1251 1252 bzero(&mltaddr, sizeof(mltaddr)); 1253 mltaddr.sin6_len = sizeof(struct sockaddr_in6); 1254 mltaddr.sin6_family = AF_INET6; 1255 mltaddr.sin6_addr = in6addr_linklocal_allnodes; 1256 1257 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 1258 0) 1259 goto cleanup; 1260 1261 rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL); 1262 if (rt != NULL && rt->rt_gateway != NULL && 1263 (memcmp(&satosin6(rt->rt_gateway)->sin6_addr, 1264 &ia->ia_addr.sin6_addr, 1265 sizeof(ia->ia_addr.sin6_addr)) == 0)) { 1266 /* 1267 * if no more IPv6 address exists on this interface 1268 * then remove the multicast address route 1269 */ 1270 if (ifa0 == NULL) { 1271 memcpy(&mltaddr.sin6_addr, &satosin6(rt_key(rt))->sin6_addr, 1272 sizeof(mltaddr.sin6_addr)); 1273 RTFREE_LOCKED(rt); 1274 error = rtrequest(RTM_DELETE, (struct sockaddr *)&mltaddr, 1275 (struct sockaddr *)&ia->ia_addr, 1276 (struct sockaddr *)&mltmask, RTF_UP, 1277 (struct rtentry **)0); 1278 if (error) 1279 log(LOG_INFO, "in6_purgeaddr: link-local all-nodes" 1280 "multicast address deletion error\n"); 1281 } else { 1282 /* 1283 * replace the gateway of the route 1284 */ 1285 struct sockaddr_in6 sa; 1286 1287 bzero(&sa, sizeof(sa)); 1288 sa.sin6_len = sizeof(struct sockaddr_in6); 1289 sa.sin6_family = AF_INET6; 1290 memcpy(&sa.sin6_addr, &satosin6(ifa0->ifa_addr)->sin6_addr, 1291 sizeof(sa.sin6_addr)); 1292 in6_setscope(&sa.sin6_addr, ifa0->ifa_ifp, NULL); 1293 memcpy(rt->rt_gateway, &sa, sizeof(sa)); 1294 RTFREE_LOCKED(rt); 1295 } 1296 } else { 1297 if (rt != NULL) 1298 RTFREE_LOCKED(rt); 1299 } 1300 1301 /* 1302 * remove the node-local all-nodes address 1303 */ 1304 mltaddr.sin6_addr = in6addr_nodelocal_allnodes; 1305 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 1306 0) 1307 goto cleanup; 1308 1309 rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL); 1310 if (rt != NULL && rt->rt_gateway != NULL && 1311 (memcmp(&satosin6(rt->rt_gateway)->sin6_addr, 1312 &ia->ia_addr.sin6_addr, 1313 sizeof(ia->ia_addr.sin6_addr)) == 0)) { 1314 /* 1315 * if no more IPv6 address exists on this interface 1316 * then remove the multicast address route 1317 */ 1318 if (ifa0 == NULL) { 1319 memcpy(&mltaddr.sin6_addr, &satosin6(rt_key(rt))->sin6_addr, 1320 sizeof(mltaddr.sin6_addr)); 1321 1322 RTFREE_LOCKED(rt); 1323 error = rtrequest(RTM_DELETE, (struct sockaddr *)&mltaddr, 1324 (struct sockaddr *)&ia->ia_addr, 1325 (struct sockaddr *)&mltmask, RTF_UP, 1326 (struct rtentry **)0); 1327 1328 if (error) 1329 log(LOG_INFO, "in6_purgeaddr: node-local all-nodes" 1330 "multicast address deletion error\n"); 1331 } else { 1332 /* 1333 * replace the gateway of the route 1334 */ 1335 struct sockaddr_in6 sa; 1336 1337 bzero(&sa, sizeof(sa)); 1338 sa.sin6_len = sizeof(struct sockaddr_in6); 1339 sa.sin6_family = AF_INET6; 1340 memcpy(&sa.sin6_addr, &satosin6(ifa0->ifa_addr)->sin6_addr, 1341 sizeof(sa.sin6_addr)); 1342 in6_setscope(&sa.sin6_addr, ifa0->ifa_ifp, NULL); 1343 memcpy(rt->rt_gateway, &sa, sizeof(sa)); 1344 RTFREE_LOCKED(rt); 1345 } 1346 } else { 1347 if (rt != NULL) 1348 RTFREE_LOCKED(rt); 1349 } 1350 1351 cleanup: 1352 1353 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */ 1354 if ((ia->ia_flags & IFA_ROUTE) && plen == 128) { 1355 int error; 1356 struct sockaddr *dstaddr; 1357 1358 /* 1359 * use the interface address if configuring an 1360 * interface address with a /128 prefix len 1361 */ 1362 if (ia->ia_dstaddr.sin6_family == AF_INET6) 1363 dstaddr = (struct sockaddr *)&ia->ia_dstaddr; 1364 else 1365 dstaddr = (struct sockaddr *)&ia->ia_addr; 1366 1367 error = rtrequest(RTM_DELETE, 1368 (struct sockaddr *)dstaddr, 1369 (struct sockaddr *)&ia->ia_addr, 1370 (struct sockaddr *)&ia->ia_prefixmask, 1371 ia->ia_flags | RTF_HOST, NULL); 1372 if (error != 0) 1373 return; 1374 ia->ia_flags &= ~IFA_ROUTE; 1375 } 1376 if (ifa0 != NULL) 1377 ifa_free(ifa0); 1378 1379 in6_unlink_ifa(ia, ifp); 1380 } 1381 1382 static void 1383 in6_unlink_ifa(struct in6_ifaddr *ia, struct ifnet *ifp) 1384 { 1385 int s = splnet(); 1386 1387 IF_ADDR_LOCK(ifp); 1388 TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link); 1389 IF_ADDR_UNLOCK(ifp); 1390 ifa_free(&ia->ia_ifa); /* if_addrhead */ 1391 1392 /* 1393 * Defer the release of what might be the last reference to the 1394 * in6_ifaddr so that it can't be freed before the remainder of the 1395 * cleanup. 1396 */ 1397 IN6_IFADDR_WLOCK(); 1398 TAILQ_REMOVE(&V_in6_ifaddrhead, ia, ia_link); 1399 IN6_IFADDR_WUNLOCK(); 1400 1401 /* 1402 * Release the reference to the base prefix. There should be a 1403 * positive reference. 1404 */ 1405 if (ia->ia6_ndpr == NULL) { 1406 nd6log((LOG_NOTICE, 1407 "in6_unlink_ifa: autoconf'ed address " 1408 "%p has no prefix\n", ia)); 1409 } else { 1410 ia->ia6_ndpr->ndpr_refcnt--; 1411 ia->ia6_ndpr = NULL; 1412 } 1413 1414 /* 1415 * Also, if the address being removed is autoconf'ed, call 1416 * pfxlist_onlink_check() since the release might affect the status of 1417 * other (detached) addresses. 1418 */ 1419 if ((ia->ia6_flags & IN6_IFF_AUTOCONF)) { 1420 pfxlist_onlink_check(); 1421 } 1422 ifa_free(&ia->ia_ifa); /* in6_ifaddrhead */ 1423 splx(s); 1424 } 1425 1426 void 1427 in6_purgeif(struct ifnet *ifp) 1428 { 1429 struct ifaddr *ifa, *nifa; 1430 1431 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, nifa) { 1432 if (ifa->ifa_addr->sa_family != AF_INET6) 1433 continue; 1434 in6_purgeaddr(ifa); 1435 } 1436 1437 in6_ifdetach(ifp); 1438 } 1439 1440 /* 1441 * SIOC[GAD]LIFADDR. 1442 * SIOCGLIFADDR: get first address. (?) 1443 * SIOCGLIFADDR with IFLR_PREFIX: 1444 * get first address that matches the specified prefix. 1445 * SIOCALIFADDR: add the specified address. 1446 * SIOCALIFADDR with IFLR_PREFIX: 1447 * add the specified prefix, filling hostid part from 1448 * the first link-local address. prefixlen must be <= 64. 1449 * SIOCDLIFADDR: delete the specified address. 1450 * SIOCDLIFADDR with IFLR_PREFIX: 1451 * delete the first address that matches the specified prefix. 1452 * return values: 1453 * EINVAL on invalid parameters 1454 * EADDRNOTAVAIL on prefix match failed/specified address not found 1455 * other values may be returned from in6_ioctl() 1456 * 1457 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64. 1458 * this is to accomodate address naming scheme other than RFC2374, 1459 * in the future. 1460 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374 1461 * address encoding scheme. (see figure on page 8) 1462 */ 1463 static int 1464 in6_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data, 1465 struct ifnet *ifp, struct thread *td) 1466 { 1467 struct if_laddrreq *iflr = (struct if_laddrreq *)data; 1468 struct ifaddr *ifa; 1469 struct sockaddr *sa; 1470 1471 /* sanity checks */ 1472 if (!data || !ifp) { 1473 panic("invalid argument to in6_lifaddr_ioctl"); 1474 /* NOTREACHED */ 1475 } 1476 1477 switch (cmd) { 1478 case SIOCGLIFADDR: 1479 /* address must be specified on GET with IFLR_PREFIX */ 1480 if ((iflr->flags & IFLR_PREFIX) == 0) 1481 break; 1482 /* FALLTHROUGH */ 1483 case SIOCALIFADDR: 1484 case SIOCDLIFADDR: 1485 /* address must be specified on ADD and DELETE */ 1486 sa = (struct sockaddr *)&iflr->addr; 1487 if (sa->sa_family != AF_INET6) 1488 return EINVAL; 1489 if (sa->sa_len != sizeof(struct sockaddr_in6)) 1490 return EINVAL; 1491 /* XXX need improvement */ 1492 sa = (struct sockaddr *)&iflr->dstaddr; 1493 if (sa->sa_family && sa->sa_family != AF_INET6) 1494 return EINVAL; 1495 if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6)) 1496 return EINVAL; 1497 break; 1498 default: /* shouldn't happen */ 1499 #if 0 1500 panic("invalid cmd to in6_lifaddr_ioctl"); 1501 /* NOTREACHED */ 1502 #else 1503 return EOPNOTSUPP; 1504 #endif 1505 } 1506 if (sizeof(struct in6_addr) * 8 < iflr->prefixlen) 1507 return EINVAL; 1508 1509 switch (cmd) { 1510 case SIOCALIFADDR: 1511 { 1512 struct in6_aliasreq ifra; 1513 struct in6_addr *hostid = NULL; 1514 int prefixlen; 1515 1516 ifa = NULL; 1517 if ((iflr->flags & IFLR_PREFIX) != 0) { 1518 struct sockaddr_in6 *sin6; 1519 1520 /* 1521 * hostid is to fill in the hostid part of the 1522 * address. hostid points to the first link-local 1523 * address attached to the interface. 1524 */ 1525 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0); 1526 if (!ifa) 1527 return EADDRNOTAVAIL; 1528 hostid = IFA_IN6(ifa); 1529 1530 /* prefixlen must be <= 64. */ 1531 if (64 < iflr->prefixlen) 1532 return EINVAL; 1533 prefixlen = iflr->prefixlen; 1534 1535 /* hostid part must be zero. */ 1536 sin6 = (struct sockaddr_in6 *)&iflr->addr; 1537 if (sin6->sin6_addr.s6_addr32[2] != 0 || 1538 sin6->sin6_addr.s6_addr32[3] != 0) { 1539 return EINVAL; 1540 } 1541 } else 1542 prefixlen = iflr->prefixlen; 1543 1544 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */ 1545 bzero(&ifra, sizeof(ifra)); 1546 bcopy(iflr->iflr_name, ifra.ifra_name, sizeof(ifra.ifra_name)); 1547 1548 bcopy(&iflr->addr, &ifra.ifra_addr, 1549 ((struct sockaddr *)&iflr->addr)->sa_len); 1550 if (hostid) { 1551 /* fill in hostid part */ 1552 ifra.ifra_addr.sin6_addr.s6_addr32[2] = 1553 hostid->s6_addr32[2]; 1554 ifra.ifra_addr.sin6_addr.s6_addr32[3] = 1555 hostid->s6_addr32[3]; 1556 } 1557 1558 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /* XXX */ 1559 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr, 1560 ((struct sockaddr *)&iflr->dstaddr)->sa_len); 1561 if (hostid) { 1562 ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] = 1563 hostid->s6_addr32[2]; 1564 ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] = 1565 hostid->s6_addr32[3]; 1566 } 1567 } 1568 if (ifa != NULL) 1569 ifa_free(ifa); 1570 1571 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6); 1572 in6_prefixlen2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen); 1573 1574 ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX; 1575 return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, td); 1576 } 1577 case SIOCGLIFADDR: 1578 case SIOCDLIFADDR: 1579 { 1580 struct in6_ifaddr *ia; 1581 struct in6_addr mask, candidate, match; 1582 struct sockaddr_in6 *sin6; 1583 int cmp; 1584 1585 bzero(&mask, sizeof(mask)); 1586 if (iflr->flags & IFLR_PREFIX) { 1587 /* lookup a prefix rather than address. */ 1588 in6_prefixlen2mask(&mask, iflr->prefixlen); 1589 1590 sin6 = (struct sockaddr_in6 *)&iflr->addr; 1591 bcopy(&sin6->sin6_addr, &match, sizeof(match)); 1592 match.s6_addr32[0] &= mask.s6_addr32[0]; 1593 match.s6_addr32[1] &= mask.s6_addr32[1]; 1594 match.s6_addr32[2] &= mask.s6_addr32[2]; 1595 match.s6_addr32[3] &= mask.s6_addr32[3]; 1596 1597 /* if you set extra bits, that's wrong */ 1598 if (bcmp(&match, &sin6->sin6_addr, sizeof(match))) 1599 return EINVAL; 1600 1601 cmp = 1; 1602 } else { 1603 if (cmd == SIOCGLIFADDR) { 1604 /* on getting an address, take the 1st match */ 1605 cmp = 0; /* XXX */ 1606 } else { 1607 /* on deleting an address, do exact match */ 1608 in6_prefixlen2mask(&mask, 128); 1609 sin6 = (struct sockaddr_in6 *)&iflr->addr; 1610 bcopy(&sin6->sin6_addr, &match, sizeof(match)); 1611 1612 cmp = 1; 1613 } 1614 } 1615 1616 IF_ADDR_LOCK(ifp); 1617 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1618 if (ifa->ifa_addr->sa_family != AF_INET6) 1619 continue; 1620 if (!cmp) 1621 break; 1622 1623 /* 1624 * XXX: this is adhoc, but is necessary to allow 1625 * a user to specify fe80::/64 (not /10) for a 1626 * link-local address. 1627 */ 1628 bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate)); 1629 in6_clearscope(&candidate); 1630 candidate.s6_addr32[0] &= mask.s6_addr32[0]; 1631 candidate.s6_addr32[1] &= mask.s6_addr32[1]; 1632 candidate.s6_addr32[2] &= mask.s6_addr32[2]; 1633 candidate.s6_addr32[3] &= mask.s6_addr32[3]; 1634 if (IN6_ARE_ADDR_EQUAL(&candidate, &match)) 1635 break; 1636 } 1637 IF_ADDR_UNLOCK(ifp); 1638 if (!ifa) 1639 return EADDRNOTAVAIL; 1640 ia = ifa2ia6(ifa); 1641 1642 if (cmd == SIOCGLIFADDR) { 1643 int error; 1644 1645 /* fill in the if_laddrreq structure */ 1646 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len); 1647 error = sa6_recoverscope( 1648 (struct sockaddr_in6 *)&iflr->addr); 1649 if (error != 0) 1650 return (error); 1651 1652 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 1653 bcopy(&ia->ia_dstaddr, &iflr->dstaddr, 1654 ia->ia_dstaddr.sin6_len); 1655 error = sa6_recoverscope( 1656 (struct sockaddr_in6 *)&iflr->dstaddr); 1657 if (error != 0) 1658 return (error); 1659 } else 1660 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr)); 1661 1662 iflr->prefixlen = 1663 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); 1664 1665 iflr->flags = ia->ia6_flags; /* XXX */ 1666 1667 return 0; 1668 } else { 1669 struct in6_aliasreq ifra; 1670 1671 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */ 1672 bzero(&ifra, sizeof(ifra)); 1673 bcopy(iflr->iflr_name, ifra.ifra_name, 1674 sizeof(ifra.ifra_name)); 1675 1676 bcopy(&ia->ia_addr, &ifra.ifra_addr, 1677 ia->ia_addr.sin6_len); 1678 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 1679 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr, 1680 ia->ia_dstaddr.sin6_len); 1681 } else { 1682 bzero(&ifra.ifra_dstaddr, 1683 sizeof(ifra.ifra_dstaddr)); 1684 } 1685 bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr, 1686 ia->ia_prefixmask.sin6_len); 1687 1688 ifra.ifra_flags = ia->ia6_flags; 1689 return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra, 1690 ifp, td); 1691 } 1692 } 1693 } 1694 1695 return EOPNOTSUPP; /* just for safety */ 1696 } 1697 1698 /* 1699 * Initialize an interface's intetnet6 address 1700 * and routing table entry. 1701 */ 1702 static int 1703 in6_ifinit(struct ifnet *ifp, struct in6_ifaddr *ia, 1704 struct sockaddr_in6 *sin6, int newhost) 1705 { 1706 int error = 0, plen, ifacount = 0; 1707 int s = splimp(); 1708 struct ifaddr *ifa; 1709 1710 /* 1711 * Give the interface a chance to initialize 1712 * if this is its first address, 1713 * and to validate the address if necessary. 1714 */ 1715 IF_ADDR_LOCK(ifp); 1716 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1717 if (ifa->ifa_addr->sa_family != AF_INET6) 1718 continue; 1719 ifacount++; 1720 } 1721 IF_ADDR_UNLOCK(ifp); 1722 1723 ia->ia_addr = *sin6; 1724 1725 if (ifacount <= 1 && ifp->if_ioctl) { 1726 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia); 1727 if (error) { 1728 splx(s); 1729 return (error); 1730 } 1731 } 1732 splx(s); 1733 1734 ia->ia_ifa.ifa_metric = ifp->if_metric; 1735 1736 /* we could do in(6)_socktrim here, but just omit it at this moment. */ 1737 1738 /* 1739 * Special case: 1740 * If a new destination address is specified for a point-to-point 1741 * interface, install a route to the destination as an interface 1742 * direct route. 1743 * XXX: the logic below rejects assigning multiple addresses on a p2p 1744 * interface that share the same destination. 1745 */ 1746 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */ 1747 if (!(ia->ia_flags & IFA_ROUTE) && plen == 128 && 1748 ia->ia_dstaddr.sin6_family == AF_INET6) { 1749 int rtflags = RTF_UP | RTF_HOST; 1750 1751 error = rtrequest(RTM_ADD, 1752 (struct sockaddr *)&ia->ia_dstaddr, 1753 (struct sockaddr *)&ia->ia_addr, 1754 (struct sockaddr *)&ia->ia_prefixmask, 1755 ia->ia_flags | rtflags, NULL); 1756 if (error != 0) 1757 return (error); 1758 ia->ia_flags |= IFA_ROUTE; 1759 } 1760 1761 /* 1762 * add a loopback route to self 1763 */ 1764 if (!(ia->ia_flags & IFA_ROUTE) 1765 && (V_nd6_useloopback 1766 || (ifp->if_flags & IFF_LOOPBACK))) { 1767 error = ifa_add_loopback_route((struct ifaddr *)ia, 1768 (struct sockaddr *)&ia->ia_addr); 1769 if (error == 0) 1770 ia->ia_flags |= IFA_RTSELF; 1771 } 1772 1773 /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */ 1774 if (newhost) { 1775 struct llentry *ln; 1776 struct rtentry rt; 1777 struct sockaddr_dl gateway; 1778 struct sockaddr_in6 mask, addr; 1779 1780 IF_AFDATA_LOCK(ifp); 1781 ia->ia_ifa.ifa_rtrequest = NULL; 1782 1783 /* XXX QL 1784 * we need to report rt_newaddrmsg 1785 */ 1786 ln = lla_lookup(LLTABLE6(ifp), (LLE_CREATE | LLE_IFADDR | LLE_EXCLUSIVE), 1787 (struct sockaddr *)&ia->ia_addr); 1788 IF_AFDATA_UNLOCK(ifp); 1789 if (ln != NULL) { 1790 ln->la_expire = 0; /* for IPv6 this means permanent */ 1791 ln->ln_state = ND6_LLINFO_REACHABLE; 1792 /* 1793 * initialize for rtmsg generation 1794 */ 1795 bzero(&gateway, sizeof(gateway)); 1796 gateway.sdl_len = sizeof(gateway); 1797 gateway.sdl_family = AF_LINK; 1798 gateway.sdl_nlen = 0; 1799 gateway.sdl_alen = 6; 1800 memcpy(gateway.sdl_data, &ln->ll_addr.mac_aligned, sizeof(ln->ll_addr)); 1801 /* */ 1802 LLE_WUNLOCK(ln); 1803 } 1804 1805 bzero(&rt, sizeof(rt)); 1806 rt.rt_gateway = (struct sockaddr *)&gateway; 1807 memcpy(&mask, &ia->ia_prefixmask, sizeof(ia->ia_prefixmask)); 1808 memcpy(&addr, &ia->ia_addr, sizeof(ia->ia_addr)); 1809 rt_mask(&rt) = (struct sockaddr *)&mask; 1810 rt_key(&rt) = (struct sockaddr *)&addr; 1811 rt.rt_flags = RTF_UP | RTF_HOST | RTF_STATIC; 1812 rt_newaddrmsg(RTM_ADD, &ia->ia_ifa, 0, &rt); 1813 } 1814 1815 return (error); 1816 } 1817 1818 /* 1819 * Find an IPv6 interface link-local address specific to an interface. 1820 * ifaddr is returned referenced. 1821 */ 1822 struct in6_ifaddr * 1823 in6ifa_ifpforlinklocal(struct ifnet *ifp, int ignoreflags) 1824 { 1825 struct ifaddr *ifa; 1826 1827 IF_ADDR_LOCK(ifp); 1828 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 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 ifa_ref(ifa); 1836 break; 1837 } 1838 } 1839 IF_ADDR_UNLOCK(ifp); 1840 1841 return ((struct in6_ifaddr *)ifa); 1842 } 1843 1844 1845 /* 1846 * find the internet address corresponding to a given interface and address. 1847 * ifaddr is returned referenced. 1848 */ 1849 struct in6_ifaddr * 1850 in6ifa_ifpwithaddr(struct ifnet *ifp, struct in6_addr *addr) 1851 { 1852 struct ifaddr *ifa; 1853 1854 IF_ADDR_LOCK(ifp); 1855 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1856 if (ifa->ifa_addr->sa_family != AF_INET6) 1857 continue; 1858 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa))) { 1859 ifa_ref(ifa); 1860 break; 1861 } 1862 } 1863 IF_ADDR_UNLOCK(ifp); 1864 1865 return ((struct in6_ifaddr *)ifa); 1866 } 1867 1868 /* 1869 * Convert IP6 address to printable (loggable) representation. Caller 1870 * has to make sure that ip6buf is at least INET6_ADDRSTRLEN long. 1871 */ 1872 static char digits[] = "0123456789abcdef"; 1873 char * 1874 ip6_sprintf(char *ip6buf, const struct in6_addr *addr) 1875 { 1876 int i; 1877 char *cp; 1878 const u_int16_t *a = (const u_int16_t *)addr; 1879 const u_int8_t *d; 1880 int dcolon = 0, zero = 0; 1881 1882 cp = ip6buf; 1883 1884 for (i = 0; i < 8; i++) { 1885 if (dcolon == 1) { 1886 if (*a == 0) { 1887 if (i == 7) 1888 *cp++ = ':'; 1889 a++; 1890 continue; 1891 } else 1892 dcolon = 2; 1893 } 1894 if (*a == 0) { 1895 if (dcolon == 0 && *(a + 1) == 0) { 1896 if (i == 0) 1897 *cp++ = ':'; 1898 *cp++ = ':'; 1899 dcolon = 1; 1900 } else { 1901 *cp++ = '0'; 1902 *cp++ = ':'; 1903 } 1904 a++; 1905 continue; 1906 } 1907 d = (const u_char *)a; 1908 /* Try to eliminate leading zeros in printout like in :0001. */ 1909 zero = 1; 1910 *cp = digits[*d >> 4]; 1911 if (*cp != '0') { 1912 zero = 0; 1913 cp++; 1914 } 1915 *cp = digits[*d++ & 0xf]; 1916 if (zero == 0 || (*cp != '0')) { 1917 zero = 0; 1918 cp++; 1919 } 1920 *cp = digits[*d >> 4]; 1921 if (zero == 0 || (*cp != '0')) { 1922 zero = 0; 1923 cp++; 1924 } 1925 *cp++ = digits[*d & 0xf]; 1926 *cp++ = ':'; 1927 a++; 1928 } 1929 *--cp = '\0'; 1930 return (ip6buf); 1931 } 1932 1933 int 1934 in6_localaddr(struct in6_addr *in6) 1935 { 1936 struct in6_ifaddr *ia; 1937 1938 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6)) 1939 return 1; 1940 1941 IN6_IFADDR_RLOCK(); 1942 TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) { 1943 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr, 1944 &ia->ia_prefixmask.sin6_addr)) { 1945 IN6_IFADDR_RUNLOCK(); 1946 return 1; 1947 } 1948 } 1949 IN6_IFADDR_RUNLOCK(); 1950 1951 return (0); 1952 } 1953 1954 int 1955 in6_is_addr_deprecated(struct sockaddr_in6 *sa6) 1956 { 1957 struct in6_ifaddr *ia; 1958 1959 IN6_IFADDR_RLOCK(); 1960 TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) { 1961 if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, 1962 &sa6->sin6_addr) && 1963 (ia->ia6_flags & IN6_IFF_DEPRECATED) != 0) { 1964 IN6_IFADDR_RUNLOCK(); 1965 return (1); /* true */ 1966 } 1967 1968 /* XXX: do we still have to go thru the rest of the list? */ 1969 } 1970 IN6_IFADDR_RUNLOCK(); 1971 1972 return (0); /* false */ 1973 } 1974 1975 /* 1976 * return length of part which dst and src are equal 1977 * hard coding... 1978 */ 1979 int 1980 in6_matchlen(struct in6_addr *src, struct in6_addr *dst) 1981 { 1982 int match = 0; 1983 u_char *s = (u_char *)src, *d = (u_char *)dst; 1984 u_char *lim = s + 16, r; 1985 1986 while (s < lim) 1987 if ((r = (*d++ ^ *s++)) != 0) { 1988 while (r < 128) { 1989 match++; 1990 r <<= 1; 1991 } 1992 break; 1993 } else 1994 match += 8; 1995 return match; 1996 } 1997 1998 /* XXX: to be scope conscious */ 1999 int 2000 in6_are_prefix_equal(struct in6_addr *p1, struct in6_addr *p2, int len) 2001 { 2002 int bytelen, bitlen; 2003 2004 /* sanity check */ 2005 if (0 > len || len > 128) { 2006 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n", 2007 len); 2008 return (0); 2009 } 2010 2011 bytelen = len / 8; 2012 bitlen = len % 8; 2013 2014 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen)) 2015 return (0); 2016 if (bitlen != 0 && 2017 p1->s6_addr[bytelen] >> (8 - bitlen) != 2018 p2->s6_addr[bytelen] >> (8 - bitlen)) 2019 return (0); 2020 2021 return (1); 2022 } 2023 2024 void 2025 in6_prefixlen2mask(struct in6_addr *maskp, int len) 2026 { 2027 u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff}; 2028 int bytelen, bitlen, i; 2029 2030 /* sanity check */ 2031 if (0 > len || len > 128) { 2032 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n", 2033 len); 2034 return; 2035 } 2036 2037 bzero(maskp, sizeof(*maskp)); 2038 bytelen = len / 8; 2039 bitlen = len % 8; 2040 for (i = 0; i < bytelen; i++) 2041 maskp->s6_addr[i] = 0xff; 2042 if (bitlen) 2043 maskp->s6_addr[bytelen] = maskarray[bitlen - 1]; 2044 } 2045 2046 /* 2047 * return the best address out of the same scope. if no address was 2048 * found, return the first valid address from designated IF. 2049 */ 2050 struct in6_ifaddr * 2051 in6_ifawithifp(struct ifnet *ifp, struct in6_addr *dst) 2052 { 2053 int dst_scope = in6_addrscope(dst), blen = -1, tlen; 2054 struct ifaddr *ifa; 2055 struct in6_ifaddr *besta = 0; 2056 struct in6_ifaddr *dep[2]; /* last-resort: deprecated */ 2057 2058 dep[0] = dep[1] = NULL; 2059 2060 /* 2061 * We first look for addresses in the same scope. 2062 * If there is one, return it. 2063 * If two or more, return one which matches the dst longest. 2064 * If none, return one of global addresses assigned other ifs. 2065 */ 2066 IF_ADDR_LOCK(ifp); 2067 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 2068 if (ifa->ifa_addr->sa_family != AF_INET6) 2069 continue; 2070 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST) 2071 continue; /* XXX: is there any case to allow anycast? */ 2072 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY) 2073 continue; /* don't use this interface */ 2074 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED) 2075 continue; 2076 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) { 2077 if (V_ip6_use_deprecated) 2078 dep[0] = (struct in6_ifaddr *)ifa; 2079 continue; 2080 } 2081 2082 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) { 2083 /* 2084 * call in6_matchlen() as few as possible 2085 */ 2086 if (besta) { 2087 if (blen == -1) 2088 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst); 2089 tlen = in6_matchlen(IFA_IN6(ifa), dst); 2090 if (tlen > blen) { 2091 blen = tlen; 2092 besta = (struct in6_ifaddr *)ifa; 2093 } 2094 } else 2095 besta = (struct in6_ifaddr *)ifa; 2096 } 2097 } 2098 if (besta) { 2099 ifa_ref(&besta->ia_ifa); 2100 IF_ADDR_UNLOCK(ifp); 2101 return (besta); 2102 } 2103 IF_ADDR_UNLOCK(ifp); 2104 2105 IN6_IFADDR_RLOCK(); 2106 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 2107 if (ifa->ifa_addr->sa_family != AF_INET6) 2108 continue; 2109 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST) 2110 continue; /* XXX: is there any case to allow anycast? */ 2111 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY) 2112 continue; /* don't use this interface */ 2113 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED) 2114 continue; 2115 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) { 2116 if (V_ip6_use_deprecated) 2117 dep[1] = (struct in6_ifaddr *)ifa; 2118 continue; 2119 } 2120 2121 if (ifa != NULL) 2122 ifa_ref(ifa); 2123 IN6_IFADDR_RUNLOCK(); 2124 return (struct in6_ifaddr *)ifa; 2125 } 2126 IN6_IFADDR_RUNLOCK(); 2127 2128 /* use the last-resort values, that are, deprecated addresses */ 2129 if (dep[0]) 2130 return dep[0]; 2131 if (dep[1]) 2132 return dep[1]; 2133 2134 return NULL; 2135 } 2136 2137 /* 2138 * perform DAD when interface becomes IFF_UP. 2139 */ 2140 void 2141 in6_if_up(struct ifnet *ifp) 2142 { 2143 struct ifaddr *ifa; 2144 struct in6_ifaddr *ia; 2145 2146 IF_ADDR_LOCK(ifp); 2147 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 2148 if (ifa->ifa_addr->sa_family != AF_INET6) 2149 continue; 2150 ia = (struct in6_ifaddr *)ifa; 2151 if (ia->ia6_flags & IN6_IFF_TENTATIVE) { 2152 /* 2153 * The TENTATIVE flag was likely set by hand 2154 * beforehand, implicitly indicating the need for DAD. 2155 * We may be able to skip the random delay in this 2156 * case, but we impose delays just in case. 2157 */ 2158 nd6_dad_start(ifa, 2159 arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz)); 2160 } 2161 } 2162 IF_ADDR_UNLOCK(ifp); 2163 2164 /* 2165 * special cases, like 6to4, are handled in in6_ifattach 2166 */ 2167 in6_ifattach(ifp, NULL); 2168 } 2169 2170 int 2171 in6if_do_dad(struct ifnet *ifp) 2172 { 2173 if ((ifp->if_flags & IFF_LOOPBACK) != 0) 2174 return (0); 2175 2176 if (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) 2177 return (0); 2178 2179 switch (ifp->if_type) { 2180 #ifdef IFT_DUMMY 2181 case IFT_DUMMY: 2182 #endif 2183 case IFT_FAITH: 2184 /* 2185 * These interfaces do not have the IFF_LOOPBACK flag, 2186 * but loop packets back. We do not have to do DAD on such 2187 * interfaces. We should even omit it, because loop-backed 2188 * NS would confuse the DAD procedure. 2189 */ 2190 return (0); 2191 default: 2192 /* 2193 * Our DAD routine requires the interface up and running. 2194 * However, some interfaces can be up before the RUNNING 2195 * status. Additionaly, users may try to assign addresses 2196 * before the interface becomes up (or running). 2197 * We simply skip DAD in such a case as a work around. 2198 * XXX: we should rather mark "tentative" on such addresses, 2199 * and do DAD after the interface becomes ready. 2200 */ 2201 if (!((ifp->if_flags & IFF_UP) && 2202 (ifp->if_drv_flags & IFF_DRV_RUNNING))) 2203 return (0); 2204 2205 return (1); 2206 } 2207 } 2208 2209 /* 2210 * Calculate max IPv6 MTU through all the interfaces and store it 2211 * to in6_maxmtu. 2212 */ 2213 void 2214 in6_setmaxmtu(void) 2215 { 2216 unsigned long maxmtu = 0; 2217 struct ifnet *ifp; 2218 2219 IFNET_RLOCK_NOSLEEP(); 2220 for (ifp = TAILQ_FIRST(&V_ifnet); ifp; 2221 ifp = TAILQ_NEXT(ifp, if_list)) { 2222 /* this function can be called during ifnet initialization */ 2223 if (!ifp->if_afdata[AF_INET6]) 2224 continue; 2225 if ((ifp->if_flags & IFF_LOOPBACK) == 0 && 2226 IN6_LINKMTU(ifp) > maxmtu) 2227 maxmtu = IN6_LINKMTU(ifp); 2228 } 2229 IFNET_RUNLOCK_NOSLEEP(); 2230 if (maxmtu) /* update only when maxmtu is positive */ 2231 V_in6_maxmtu = maxmtu; 2232 } 2233 2234 /* 2235 * Provide the length of interface identifiers to be used for the link attached 2236 * to the given interface. The length should be defined in "IPv6 over 2237 * xxx-link" document. Note that address architecture might also define 2238 * the length for a particular set of address prefixes, regardless of the 2239 * link type. As clarified in rfc2462bis, those two definitions should be 2240 * consistent, and those really are as of August 2004. 2241 */ 2242 int 2243 in6_if2idlen(struct ifnet *ifp) 2244 { 2245 switch (ifp->if_type) { 2246 case IFT_ETHER: /* RFC2464 */ 2247 #ifdef IFT_PROPVIRTUAL 2248 case IFT_PROPVIRTUAL: /* XXX: no RFC. treat it as ether */ 2249 #endif 2250 #ifdef IFT_L2VLAN 2251 case IFT_L2VLAN: /* ditto */ 2252 #endif 2253 #ifdef IFT_IEEE80211 2254 case IFT_IEEE80211: /* ditto */ 2255 #endif 2256 #ifdef IFT_MIP 2257 case IFT_MIP: /* ditto */ 2258 #endif 2259 return (64); 2260 case IFT_FDDI: /* RFC2467 */ 2261 return (64); 2262 case IFT_ISO88025: /* RFC2470 (IPv6 over Token Ring) */ 2263 return (64); 2264 case IFT_PPP: /* RFC2472 */ 2265 return (64); 2266 case IFT_ARCNET: /* RFC2497 */ 2267 return (64); 2268 case IFT_FRELAY: /* RFC2590 */ 2269 return (64); 2270 case IFT_IEEE1394: /* RFC3146 */ 2271 return (64); 2272 case IFT_GIF: 2273 return (64); /* draft-ietf-v6ops-mech-v2-07 */ 2274 case IFT_LOOP: 2275 return (64); /* XXX: is this really correct? */ 2276 default: 2277 /* 2278 * Unknown link type: 2279 * It might be controversial to use the today's common constant 2280 * of 64 for these cases unconditionally. For full compliance, 2281 * we should return an error in this case. On the other hand, 2282 * if we simply miss the standard for the link type or a new 2283 * standard is defined for a new link type, the IFID length 2284 * is very likely to be the common constant. As a compromise, 2285 * we always use the constant, but make an explicit notice 2286 * indicating the "unknown" case. 2287 */ 2288 printf("in6_if2idlen: unknown link type (%d)\n", ifp->if_type); 2289 return (64); 2290 } 2291 } 2292 2293 #include <sys/sysctl.h> 2294 2295 struct in6_llentry { 2296 struct llentry base; 2297 struct sockaddr_in6 l3_addr6; 2298 }; 2299 2300 static struct llentry * 2301 in6_lltable_new(const struct sockaddr *l3addr, u_int flags) 2302 { 2303 struct in6_llentry *lle; 2304 2305 lle = malloc(sizeof(struct in6_llentry), M_LLTABLE, 2306 M_DONTWAIT | M_ZERO); 2307 if (lle == NULL) /* NB: caller generates msg */ 2308 return NULL; 2309 2310 callout_init(&lle->base.ln_timer_ch, CALLOUT_MPSAFE); 2311 lle->l3_addr6 = *(const struct sockaddr_in6 *)l3addr; 2312 lle->base.lle_refcnt = 1; 2313 LLE_LOCK_INIT(&lle->base); 2314 return &lle->base; 2315 } 2316 2317 /* 2318 * Deletes an address from the address table. 2319 * This function is called by the timer functions 2320 * such as arptimer() and nd6_llinfo_timer(), and 2321 * the caller does the locking. 2322 */ 2323 static void 2324 in6_lltable_free(struct lltable *llt, struct llentry *lle) 2325 { 2326 LLE_WUNLOCK(lle); 2327 LLE_LOCK_DESTROY(lle); 2328 free(lle, M_LLTABLE); 2329 } 2330 2331 static void 2332 in6_lltable_prefix_free(struct lltable *llt, 2333 const struct sockaddr *prefix, 2334 const struct sockaddr *mask) 2335 { 2336 const struct sockaddr_in6 *pfx = (const struct sockaddr_in6 *)prefix; 2337 const struct sockaddr_in6 *msk = (const struct sockaddr_in6 *)mask; 2338 struct llentry *lle, *next; 2339 register int i; 2340 2341 for (i=0; i < LLTBL_HASHTBL_SIZE; i++) { 2342 LIST_FOREACH_SAFE(lle, &llt->lle_head[i], lle_next, next) { 2343 if (IN6_ARE_MASKED_ADDR_EQUAL( 2344 &((struct sockaddr_in6 *)L3_ADDR(lle))->sin6_addr, 2345 &pfx->sin6_addr, 2346 &msk->sin6_addr)) { 2347 callout_drain(&lle->la_timer); 2348 LLE_WLOCK(lle); 2349 llentry_free(lle); 2350 } 2351 } 2352 } 2353 } 2354 2355 static int 2356 in6_lltable_rtcheck(struct ifnet *ifp, 2357 u_int flags, 2358 const struct sockaddr *l3addr) 2359 { 2360 struct rtentry *rt; 2361 char ip6buf[INET6_ADDRSTRLEN]; 2362 2363 KASSERT(l3addr->sa_family == AF_INET6, 2364 ("sin_family %d", l3addr->sa_family)); 2365 2366 /* XXX rtalloc1 should take a const param */ 2367 rt = rtalloc1(__DECONST(struct sockaddr *, l3addr), 0, 0); 2368 if (rt == NULL || (rt->rt_flags & RTF_GATEWAY) || rt->rt_ifp != ifp) { 2369 struct ifaddr *ifa; 2370 /* 2371 * Create an ND6 cache for an IPv6 neighbor 2372 * that is not covered by our own prefix. 2373 */ 2374 /* XXX ifaof_ifpforaddr should take a const param */ 2375 ifa = ifaof_ifpforaddr(__DECONST(struct sockaddr *, l3addr), ifp); 2376 if (ifa != NULL) { 2377 ifa_free(ifa); 2378 if (rt != NULL) 2379 RTFREE_LOCKED(rt); 2380 return 0; 2381 } 2382 log(LOG_INFO, "IPv6 address: \"%s\" is not on the network\n", 2383 ip6_sprintf(ip6buf, &((const struct sockaddr_in6 *)l3addr)->sin6_addr)); 2384 if (rt != NULL) 2385 RTFREE_LOCKED(rt); 2386 return EINVAL; 2387 } 2388 RTFREE_LOCKED(rt); 2389 return 0; 2390 } 2391 2392 static struct llentry * 2393 in6_lltable_lookup(struct lltable *llt, u_int flags, 2394 const struct sockaddr *l3addr) 2395 { 2396 const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)l3addr; 2397 struct ifnet *ifp = llt->llt_ifp; 2398 struct llentry *lle; 2399 struct llentries *lleh; 2400 u_int hashkey; 2401 2402 IF_AFDATA_LOCK_ASSERT(ifp); 2403 KASSERT(l3addr->sa_family == AF_INET6, 2404 ("sin_family %d", l3addr->sa_family)); 2405 2406 hashkey = sin6->sin6_addr.s6_addr32[3]; 2407 lleh = &llt->lle_head[LLATBL_HASH(hashkey, LLTBL_HASHMASK)]; 2408 LIST_FOREACH(lle, lleh, lle_next) { 2409 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)L3_ADDR(lle); 2410 if (lle->la_flags & LLE_DELETED) 2411 continue; 2412 if (bcmp(&sa6->sin6_addr, &sin6->sin6_addr, 2413 sizeof(struct in6_addr)) == 0) 2414 break; 2415 } 2416 2417 if (lle == NULL) { 2418 if (!(flags & LLE_CREATE)) 2419 return (NULL); 2420 /* 2421 * A route that covers the given address must have 2422 * been installed 1st because we are doing a resolution, 2423 * verify this. 2424 */ 2425 if (!(flags & LLE_IFADDR) && 2426 in6_lltable_rtcheck(ifp, flags, l3addr) != 0) 2427 return NULL; 2428 2429 lle = in6_lltable_new(l3addr, flags); 2430 if (lle == NULL) { 2431 log(LOG_INFO, "lla_lookup: new lle malloc failed\n"); 2432 return NULL; 2433 } 2434 lle->la_flags = flags & ~LLE_CREATE; 2435 if ((flags & (LLE_CREATE | LLE_IFADDR)) == (LLE_CREATE | LLE_IFADDR)) { 2436 bcopy(IF_LLADDR(ifp), &lle->ll_addr, ifp->if_addrlen); 2437 lle->la_flags |= (LLE_VALID | LLE_STATIC); 2438 } 2439 2440 lle->lle_tbl = llt; 2441 lle->lle_head = lleh; 2442 LIST_INSERT_HEAD(lleh, lle, lle_next); 2443 } else if (flags & LLE_DELETE) { 2444 if (!(lle->la_flags & LLE_IFADDR) || (flags & LLE_IFADDR)) { 2445 LLE_WLOCK(lle); 2446 lle->la_flags = LLE_DELETED; 2447 LLE_WUNLOCK(lle); 2448 #ifdef DIAGNOSTIC 2449 log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle); 2450 #endif 2451 } 2452 lle = (void *)-1; 2453 } 2454 if (LLE_IS_VALID(lle)) { 2455 if (flags & LLE_EXCLUSIVE) 2456 LLE_WLOCK(lle); 2457 else 2458 LLE_RLOCK(lle); 2459 } 2460 return (lle); 2461 } 2462 2463 static int 2464 in6_lltable_dump(struct lltable *llt, struct sysctl_req *wr) 2465 { 2466 struct ifnet *ifp = llt->llt_ifp; 2467 struct llentry *lle; 2468 /* XXX stack use */ 2469 struct { 2470 struct rt_msghdr rtm; 2471 struct sockaddr_in6 sin6; 2472 /* 2473 * ndp.c assumes that sdl is word aligned 2474 */ 2475 #ifdef __LP64__ 2476 uint32_t pad; 2477 #endif 2478 struct sockaddr_dl sdl; 2479 } ndpc; 2480 int i, error; 2481 2482 if (ifp->if_flags & IFF_LOOPBACK) 2483 return 0; 2484 2485 LLTABLE_LOCK_ASSERT(); 2486 2487 error = 0; 2488 for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) { 2489 LIST_FOREACH(lle, &llt->lle_head[i], lle_next) { 2490 struct sockaddr_dl *sdl; 2491 2492 /* skip deleted or invalid entries */ 2493 if ((lle->la_flags & (LLE_DELETED|LLE_VALID)) != LLE_VALID) 2494 continue; 2495 /* Skip if jailed and not a valid IP of the prison. */ 2496 if (prison_if(wr->td->td_ucred, L3_ADDR(lle)) != 0) 2497 continue; 2498 /* 2499 * produce a msg made of: 2500 * struct rt_msghdr; 2501 * struct sockaddr_in6 (IPv6) 2502 * struct sockaddr_dl; 2503 */ 2504 bzero(&ndpc, sizeof(ndpc)); 2505 ndpc.rtm.rtm_msglen = sizeof(ndpc); 2506 ndpc.rtm.rtm_version = RTM_VERSION; 2507 ndpc.rtm.rtm_type = RTM_GET; 2508 ndpc.rtm.rtm_flags = RTF_UP; 2509 ndpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY; 2510 ndpc.sin6.sin6_family = AF_INET6; 2511 ndpc.sin6.sin6_len = sizeof(ndpc.sin6); 2512 bcopy(L3_ADDR(lle), &ndpc.sin6, L3_ADDR_LEN(lle)); 2513 2514 /* publish */ 2515 if (lle->la_flags & LLE_PUB) 2516 ndpc.rtm.rtm_flags |= RTF_ANNOUNCE; 2517 2518 sdl = &ndpc.sdl; 2519 sdl->sdl_family = AF_LINK; 2520 sdl->sdl_len = sizeof(*sdl); 2521 sdl->sdl_alen = ifp->if_addrlen; 2522 sdl->sdl_index = ifp->if_index; 2523 sdl->sdl_type = ifp->if_type; 2524 bcopy(&lle->ll_addr, LLADDR(sdl), ifp->if_addrlen); 2525 ndpc.rtm.rtm_rmx.rmx_expire = 2526 lle->la_flags & LLE_STATIC ? 0 : lle->la_expire; 2527 ndpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA); 2528 if (lle->la_flags & LLE_STATIC) 2529 ndpc.rtm.rtm_flags |= RTF_STATIC; 2530 ndpc.rtm.rtm_index = ifp->if_index; 2531 error = SYSCTL_OUT(wr, &ndpc, sizeof(ndpc)); 2532 if (error) 2533 break; 2534 } 2535 } 2536 return error; 2537 } 2538 2539 void * 2540 in6_domifattach(struct ifnet *ifp) 2541 { 2542 struct in6_ifextra *ext; 2543 2544 ext = (struct in6_ifextra *)malloc(sizeof(*ext), M_IFADDR, M_WAITOK); 2545 bzero(ext, sizeof(*ext)); 2546 2547 ext->in6_ifstat = (struct in6_ifstat *)malloc(sizeof(struct in6_ifstat), 2548 M_IFADDR, M_WAITOK); 2549 bzero(ext->in6_ifstat, sizeof(*ext->in6_ifstat)); 2550 2551 ext->icmp6_ifstat = 2552 (struct icmp6_ifstat *)malloc(sizeof(struct icmp6_ifstat), 2553 M_IFADDR, M_WAITOK); 2554 bzero(ext->icmp6_ifstat, sizeof(*ext->icmp6_ifstat)); 2555 2556 ext->nd_ifinfo = nd6_ifattach(ifp); 2557 ext->scope6_id = scope6_ifattach(ifp); 2558 ext->lltable = lltable_init(ifp, AF_INET6); 2559 if (ext->lltable != NULL) { 2560 ext->lltable->llt_new = in6_lltable_new; 2561 ext->lltable->llt_free = in6_lltable_free; 2562 ext->lltable->llt_prefix_free = in6_lltable_prefix_free; 2563 ext->lltable->llt_rtcheck = in6_lltable_rtcheck; 2564 ext->lltable->llt_lookup = in6_lltable_lookup; 2565 ext->lltable->llt_dump = in6_lltable_dump; 2566 } 2567 2568 ext->mld_ifinfo = mld_domifattach(ifp); 2569 2570 return ext; 2571 } 2572 2573 void 2574 in6_domifdetach(struct ifnet *ifp, void *aux) 2575 { 2576 struct in6_ifextra *ext = (struct in6_ifextra *)aux; 2577 2578 mld_domifdetach(ifp); 2579 scope6_ifdetach(ext->scope6_id); 2580 nd6_ifdetach(ext->nd_ifinfo); 2581 lltable_free(ext->lltable); 2582 free(ext->in6_ifstat, M_IFADDR); 2583 free(ext->icmp6_ifstat, M_IFADDR); 2584 free(ext, M_IFADDR); 2585 } 2586 2587 /* 2588 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be 2589 * v4 mapped addr or v4 compat addr 2590 */ 2591 void 2592 in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6) 2593 { 2594 2595 bzero(sin, sizeof(*sin)); 2596 sin->sin_len = sizeof(struct sockaddr_in); 2597 sin->sin_family = AF_INET; 2598 sin->sin_port = sin6->sin6_port; 2599 sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3]; 2600 } 2601 2602 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */ 2603 void 2604 in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6) 2605 { 2606 bzero(sin6, sizeof(*sin6)); 2607 sin6->sin6_len = sizeof(struct sockaddr_in6); 2608 sin6->sin6_family = AF_INET6; 2609 sin6->sin6_port = sin->sin_port; 2610 sin6->sin6_addr.s6_addr32[0] = 0; 2611 sin6->sin6_addr.s6_addr32[1] = 0; 2612 sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP; 2613 sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr; 2614 } 2615 2616 /* Convert sockaddr_in6 into sockaddr_in. */ 2617 void 2618 in6_sin6_2_sin_in_sock(struct sockaddr *nam) 2619 { 2620 struct sockaddr_in *sin_p; 2621 struct sockaddr_in6 sin6; 2622 2623 /* 2624 * Save original sockaddr_in6 addr and convert it 2625 * to sockaddr_in. 2626 */ 2627 sin6 = *(struct sockaddr_in6 *)nam; 2628 sin_p = (struct sockaddr_in *)nam; 2629 in6_sin6_2_sin(sin_p, &sin6); 2630 } 2631 2632 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */ 2633 void 2634 in6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam) 2635 { 2636 struct sockaddr_in *sin_p; 2637 struct sockaddr_in6 *sin6_p; 2638 2639 sin6_p = malloc(sizeof *sin6_p, M_SONAME, 2640 M_WAITOK); 2641 sin_p = (struct sockaddr_in *)*nam; 2642 in6_sin_2_v4mapsin6(sin_p, sin6_p); 2643 free(*nam, M_SONAME); 2644 *nam = (struct sockaddr *)sin6_p; 2645 } 2646