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