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 EVENTHANDLER_INVOKE(ifaddr_event, ifp); 657 break; 658 } 659 660 case SIOCDIFADDR_IN6: 661 { 662 struct nd_prefix *pr; 663 664 /* 665 * If the address being deleted is the only one that owns 666 * the corresponding prefix, expire the prefix as well. 667 * XXX: theoretically, we don't have to worry about such 668 * relationship, since we separate the address management 669 * and the prefix management. We do this, however, to provide 670 * as much backward compatibility as possible in terms of 671 * the ioctl operation. 672 * Note that in6_purgeaddr() will decrement ndpr_refcnt. 673 */ 674 pr = ia->ia6_ndpr; 675 in6_purgeaddr(&ia->ia_ifa); 676 if (pr && pr->ndpr_refcnt == 0) 677 prelist_remove(pr); 678 EVENTHANDLER_INVOKE(ifaddr_event, ifp); 679 break; 680 } 681 682 default: 683 if (ifp == NULL || ifp->if_ioctl == 0) { 684 error = EOPNOTSUPP; 685 goto out; 686 } 687 error = (*ifp->if_ioctl)(ifp, cmd, data); 688 goto out; 689 } 690 691 error = 0; 692 out: 693 if (ia != NULL) 694 ifa_free(&ia->ia_ifa); 695 return (error); 696 } 697 698 /* 699 * Update parameters of an IPv6 interface address. 700 * If necessary, a new entry is created and linked into address chains. 701 * This function is separated from in6_control(). 702 * XXX: should this be performed under splnet()? 703 */ 704 int 705 in6_update_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra, 706 struct in6_ifaddr *ia, int flags) 707 { 708 int error = 0, hostIsNew = 0, plen = -1; 709 struct sockaddr_in6 dst6; 710 struct in6_addrlifetime *lt; 711 struct in6_multi_mship *imm; 712 struct in6_multi *in6m_sol; 713 struct rtentry *rt; 714 int delay; 715 char ip6buf[INET6_ADDRSTRLEN]; 716 717 /* Validate parameters */ 718 if (ifp == NULL || ifra == NULL) /* this maybe redundant */ 719 return (EINVAL); 720 721 /* 722 * The destination address for a p2p link must have a family 723 * of AF_UNSPEC or AF_INET6. 724 */ 725 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 && 726 ifra->ifra_dstaddr.sin6_family != AF_INET6 && 727 ifra->ifra_dstaddr.sin6_family != AF_UNSPEC) 728 return (EAFNOSUPPORT); 729 /* 730 * validate ifra_prefixmask. don't check sin6_family, netmask 731 * does not carry fields other than sin6_len. 732 */ 733 if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6)) 734 return (EINVAL); 735 /* 736 * Because the IPv6 address architecture is classless, we require 737 * users to specify a (non 0) prefix length (mask) for a new address. 738 * We also require the prefix (when specified) mask is valid, and thus 739 * reject a non-consecutive mask. 740 */ 741 if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0) 742 return (EINVAL); 743 if (ifra->ifra_prefixmask.sin6_len != 0) { 744 plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr, 745 (u_char *)&ifra->ifra_prefixmask + 746 ifra->ifra_prefixmask.sin6_len); 747 if (plen <= 0) 748 return (EINVAL); 749 } else { 750 /* 751 * In this case, ia must not be NULL. We just use its prefix 752 * length. 753 */ 754 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); 755 } 756 /* 757 * If the destination address on a p2p interface is specified, 758 * and the address is a scoped one, validate/set the scope 759 * zone identifier. 760 */ 761 dst6 = ifra->ifra_dstaddr; 762 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) != 0 && 763 (dst6.sin6_family == AF_INET6)) { 764 struct in6_addr in6_tmp; 765 u_int32_t zoneid; 766 767 in6_tmp = dst6.sin6_addr; 768 if (in6_setscope(&in6_tmp, ifp, &zoneid)) 769 return (EINVAL); /* XXX: should be impossible */ 770 771 if (dst6.sin6_scope_id != 0) { 772 if (dst6.sin6_scope_id != zoneid) 773 return (EINVAL); 774 } else /* user omit to specify the ID. */ 775 dst6.sin6_scope_id = zoneid; 776 777 /* convert into the internal form */ 778 if (sa6_embedscope(&dst6, 0)) 779 return (EINVAL); /* XXX: should be impossible */ 780 } 781 /* 782 * The destination address can be specified only for a p2p or a 783 * loopback interface. If specified, the corresponding prefix length 784 * must be 128. 785 */ 786 if (ifra->ifra_dstaddr.sin6_family == AF_INET6) { 787 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) == 0) { 788 /* XXX: noisy message */ 789 nd6log((LOG_INFO, "in6_update_ifa: a destination can " 790 "be specified for a p2p or a loopback IF only\n")); 791 return (EINVAL); 792 } 793 if (plen != 128) { 794 nd6log((LOG_INFO, "in6_update_ifa: prefixlen should " 795 "be 128 when dstaddr is specified\n")); 796 return (EINVAL); 797 } 798 } 799 /* lifetime consistency check */ 800 lt = &ifra->ifra_lifetime; 801 if (lt->ia6t_pltime > lt->ia6t_vltime) 802 return (EINVAL); 803 if (lt->ia6t_vltime == 0) { 804 /* 805 * the following log might be noisy, but this is a typical 806 * configuration mistake or a tool's bug. 807 */ 808 nd6log((LOG_INFO, 809 "in6_update_ifa: valid lifetime is 0 for %s\n", 810 ip6_sprintf(ip6buf, &ifra->ifra_addr.sin6_addr))); 811 812 if (ia == NULL) 813 return (0); /* there's nothing to do */ 814 } 815 816 /* 817 * If this is a new address, allocate a new ifaddr and link it 818 * into chains. 819 */ 820 if (ia == NULL) { 821 hostIsNew = 1; 822 /* 823 * When in6_update_ifa() is called in a process of a received 824 * RA, it is called under an interrupt context. So, we should 825 * call malloc with M_NOWAIT. 826 */ 827 ia = (struct in6_ifaddr *) malloc(sizeof(*ia), M_IFADDR, 828 M_NOWAIT); 829 if (ia == NULL) 830 return (ENOBUFS); 831 bzero((caddr_t)ia, sizeof(*ia)); 832 ifa_init(&ia->ia_ifa); 833 LIST_INIT(&ia->ia6_memberships); 834 /* Initialize the address and masks, and put time stamp */ 835 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr; 836 ia->ia_addr.sin6_family = AF_INET6; 837 ia->ia_addr.sin6_len = sizeof(ia->ia_addr); 838 ia->ia6_createtime = time_second; 839 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) { 840 /* 841 * XXX: some functions expect that ifa_dstaddr is not 842 * NULL for p2p interfaces. 843 */ 844 ia->ia_ifa.ifa_dstaddr = 845 (struct sockaddr *)&ia->ia_dstaddr; 846 } else { 847 ia->ia_ifa.ifa_dstaddr = NULL; 848 } 849 ia->ia_ifa.ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask; 850 ia->ia_ifp = ifp; 851 ifa_ref(&ia->ia_ifa); /* if_addrhead */ 852 IF_ADDR_LOCK(ifp); 853 TAILQ_INSERT_TAIL(&ifp->if_addrhead, &ia->ia_ifa, ifa_link); 854 IF_ADDR_UNLOCK(ifp); 855 856 ifa_ref(&ia->ia_ifa); /* in6_ifaddrhead */ 857 IN6_IFADDR_WLOCK(); 858 TAILQ_INSERT_TAIL(&V_in6_ifaddrhead, ia, ia_link); 859 IN6_IFADDR_WUNLOCK(); 860 } 861 862 /* update timestamp */ 863 ia->ia6_updatetime = time_second; 864 865 /* set prefix mask */ 866 if (ifra->ifra_prefixmask.sin6_len) { 867 /* 868 * We prohibit changing the prefix length of an existing 869 * address, because 870 * + such an operation should be rare in IPv6, and 871 * + the operation would confuse prefix management. 872 */ 873 if (ia->ia_prefixmask.sin6_len && 874 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) { 875 nd6log((LOG_INFO, "in6_update_ifa: the prefix length of an" 876 " existing (%s) address should not be changed\n", 877 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr))); 878 error = EINVAL; 879 goto unlink; 880 } 881 ia->ia_prefixmask = ifra->ifra_prefixmask; 882 } 883 884 /* 885 * If a new destination address is specified, scrub the old one and 886 * install the new destination. Note that the interface must be 887 * p2p or loopback (see the check above.) 888 */ 889 if (dst6.sin6_family == AF_INET6 && 890 !IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr, &ia->ia_dstaddr.sin6_addr)) { 891 int e; 892 893 if ((ia->ia_flags & IFA_ROUTE) != 0 && 894 (e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST)) != 0) { 895 nd6log((LOG_ERR, "in6_update_ifa: failed to remove " 896 "a route to the old destination: %s\n", 897 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr))); 898 /* proceed anyway... */ 899 } else 900 ia->ia_flags &= ~IFA_ROUTE; 901 ia->ia_dstaddr = dst6; 902 } 903 904 /* 905 * Set lifetimes. We do not refer to ia6t_expire and ia6t_preferred 906 * to see if the address is deprecated or invalidated, but initialize 907 * these members for applications. 908 */ 909 ia->ia6_lifetime = ifra->ifra_lifetime; 910 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) { 911 ia->ia6_lifetime.ia6t_expire = 912 time_second + ia->ia6_lifetime.ia6t_vltime; 913 } else 914 ia->ia6_lifetime.ia6t_expire = 0; 915 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) { 916 ia->ia6_lifetime.ia6t_preferred = 917 time_second + ia->ia6_lifetime.ia6t_pltime; 918 } else 919 ia->ia6_lifetime.ia6t_preferred = 0; 920 921 /* reset the interface and routing table appropriately. */ 922 if ((error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew)) != 0) 923 goto unlink; 924 925 /* 926 * configure address flags. 927 */ 928 ia->ia6_flags = ifra->ifra_flags; 929 /* 930 * backward compatibility - if IN6_IFF_DEPRECATED is set from the 931 * userland, make it deprecated. 932 */ 933 if ((ifra->ifra_flags & IN6_IFF_DEPRECATED) != 0) { 934 ia->ia6_lifetime.ia6t_pltime = 0; 935 ia->ia6_lifetime.ia6t_preferred = time_second; 936 } 937 /* 938 * Make the address tentative before joining multicast addresses, 939 * so that corresponding MLD responses would not have a tentative 940 * source address. 941 */ 942 ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /* safety */ 943 if (hostIsNew && in6if_do_dad(ifp)) 944 ia->ia6_flags |= IN6_IFF_TENTATIVE; 945 946 /* DAD should be performed after ND6_IFF_IFDISABLED is cleared. */ 947 if (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) 948 ia->ia6_flags |= IN6_IFF_TENTATIVE; 949 950 /* 951 * We are done if we have simply modified an existing address. 952 */ 953 if (!hostIsNew) 954 return (error); 955 956 /* 957 * Beyond this point, we should call in6_purgeaddr upon an error, 958 * not just go to unlink. 959 */ 960 961 /* Join necessary multicast groups */ 962 in6m_sol = NULL; 963 if ((ifp->if_flags & IFF_MULTICAST) != 0) { 964 struct sockaddr_in6 mltaddr, mltmask; 965 struct in6_addr llsol; 966 967 /* join solicited multicast addr for new host id */ 968 bzero(&llsol, sizeof(struct in6_addr)); 969 llsol.s6_addr32[0] = IPV6_ADDR_INT32_MLL; 970 llsol.s6_addr32[1] = 0; 971 llsol.s6_addr32[2] = htonl(1); 972 llsol.s6_addr32[3] = ifra->ifra_addr.sin6_addr.s6_addr32[3]; 973 llsol.s6_addr8[12] = 0xff; 974 if ((error = in6_setscope(&llsol, ifp, NULL)) != 0) { 975 /* XXX: should not happen */ 976 log(LOG_ERR, "in6_update_ifa: " 977 "in6_setscope failed\n"); 978 goto cleanup; 979 } 980 delay = 0; 981 if ((flags & IN6_IFAUPDATE_DADDELAY)) { 982 /* 983 * We need a random delay for DAD on the address 984 * being configured. It also means delaying 985 * transmission of the corresponding MLD report to 986 * avoid report collision. 987 * [RFC 4861, Section 6.3.7] 988 */ 989 delay = arc4random() % 990 (MAX_RTR_SOLICITATION_DELAY * hz); 991 } 992 imm = in6_joingroup(ifp, &llsol, &error, delay); 993 if (imm == NULL) { 994 nd6log((LOG_WARNING, 995 "in6_update_ifa: addmulti failed for " 996 "%s on %s (errno=%d)\n", 997 ip6_sprintf(ip6buf, &llsol), if_name(ifp), 998 error)); 999 goto cleanup; 1000 } 1001 LIST_INSERT_HEAD(&ia->ia6_memberships, 1002 imm, i6mm_chain); 1003 in6m_sol = imm->i6mm_maddr; 1004 1005 bzero(&mltmask, sizeof(mltmask)); 1006 mltmask.sin6_len = sizeof(struct sockaddr_in6); 1007 mltmask.sin6_family = AF_INET6; 1008 mltmask.sin6_addr = in6mask32; 1009 #define MLTMASK_LEN 4 /* mltmask's masklen (=32bit=4octet) */ 1010 1011 /* 1012 * join link-local all-nodes address 1013 */ 1014 bzero(&mltaddr, sizeof(mltaddr)); 1015 mltaddr.sin6_len = sizeof(struct sockaddr_in6); 1016 mltaddr.sin6_family = AF_INET6; 1017 mltaddr.sin6_addr = in6addr_linklocal_allnodes; 1018 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 1019 0) 1020 goto cleanup; /* XXX: should not fail */ 1021 1022 /* 1023 * XXX: do we really need this automatic routes? 1024 * We should probably reconsider this stuff. Most applications 1025 * actually do not need the routes, since they usually specify 1026 * the outgoing interface. 1027 */ 1028 rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL); 1029 if (rt) { 1030 /* XXX: only works in !SCOPEDROUTING case. */ 1031 if (memcmp(&mltaddr.sin6_addr, 1032 &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr, 1033 MLTMASK_LEN)) { 1034 RTFREE_LOCKED(rt); 1035 rt = NULL; 1036 } 1037 } 1038 if (!rt) { 1039 error = rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr, 1040 (struct sockaddr *)&ia->ia_addr, 1041 (struct sockaddr *)&mltmask, RTF_UP, 1042 (struct rtentry **)0); 1043 if (error) 1044 goto cleanup; 1045 } else { 1046 RTFREE_LOCKED(rt); 1047 } 1048 1049 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0); 1050 if (!imm) { 1051 nd6log((LOG_WARNING, 1052 "in6_update_ifa: addmulti failed for " 1053 "%s on %s (errno=%d)\n", 1054 ip6_sprintf(ip6buf, &mltaddr.sin6_addr), 1055 if_name(ifp), error)); 1056 goto cleanup; 1057 } 1058 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain); 1059 1060 /* 1061 * join node information group address 1062 */ 1063 delay = 0; 1064 if ((flags & IN6_IFAUPDATE_DADDELAY)) { 1065 /* 1066 * The spec doesn't say anything about delay for this 1067 * group, but the same logic should apply. 1068 */ 1069 delay = arc4random() % 1070 (MAX_RTR_SOLICITATION_DELAY * hz); 1071 } 1072 if (in6_nigroup(ifp, NULL, -1, &mltaddr.sin6_addr) == 0) { 1073 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 1074 delay); /* XXX jinmei */ 1075 if (!imm) { 1076 nd6log((LOG_WARNING, "in6_update_ifa: " 1077 "addmulti failed for %s on %s " 1078 "(errno=%d)\n", 1079 ip6_sprintf(ip6buf, &mltaddr.sin6_addr), 1080 if_name(ifp), error)); 1081 /* XXX not very fatal, go on... */ 1082 } else { 1083 LIST_INSERT_HEAD(&ia->ia6_memberships, 1084 imm, i6mm_chain); 1085 } 1086 } 1087 1088 /* 1089 * join interface-local all-nodes address. 1090 * (ff01::1%ifN, and ff01::%ifN/32) 1091 */ 1092 mltaddr.sin6_addr = in6addr_nodelocal_allnodes; 1093 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) 1094 != 0) 1095 goto cleanup; /* XXX: should not fail */ 1096 /* XXX: again, do we really need the route? */ 1097 rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL); 1098 if (rt) { 1099 if (memcmp(&mltaddr.sin6_addr, 1100 &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr, 1101 MLTMASK_LEN)) { 1102 RTFREE_LOCKED(rt); 1103 rt = NULL; 1104 } 1105 } 1106 if (!rt) { 1107 error = rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr, 1108 (struct sockaddr *)&ia->ia_addr, 1109 (struct sockaddr *)&mltmask, RTF_UP, 1110 (struct rtentry **)0); 1111 if (error) 1112 goto cleanup; 1113 } else 1114 RTFREE_LOCKED(rt); 1115 1116 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0); 1117 if (!imm) { 1118 nd6log((LOG_WARNING, "in6_update_ifa: " 1119 "addmulti failed for %s on %s " 1120 "(errno=%d)\n", 1121 ip6_sprintf(ip6buf, &mltaddr.sin6_addr), 1122 if_name(ifp), error)); 1123 goto cleanup; 1124 } 1125 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain); 1126 #undef MLTMASK_LEN 1127 } 1128 1129 /* 1130 * Perform DAD, if needed. 1131 * XXX It may be of use, if we can administratively 1132 * disable DAD. 1133 */ 1134 if (in6if_do_dad(ifp) && ((ifra->ifra_flags & IN6_IFF_NODAD) == 0) && 1135 (ia->ia6_flags & IN6_IFF_TENTATIVE)) 1136 { 1137 int mindelay, maxdelay; 1138 1139 delay = 0; 1140 if ((flags & IN6_IFAUPDATE_DADDELAY)) { 1141 /* 1142 * We need to impose a delay before sending an NS 1143 * for DAD. Check if we also needed a delay for the 1144 * corresponding MLD message. If we did, the delay 1145 * should be larger than the MLD delay (this could be 1146 * relaxed a bit, but this simple logic is at least 1147 * safe). 1148 * XXX: Break data hiding guidelines and look at 1149 * state for the solicited multicast group. 1150 */ 1151 mindelay = 0; 1152 if (in6m_sol != NULL && 1153 in6m_sol->in6m_state == MLD_REPORTING_MEMBER) { 1154 mindelay = in6m_sol->in6m_timer; 1155 } 1156 maxdelay = MAX_RTR_SOLICITATION_DELAY * hz; 1157 if (maxdelay - mindelay == 0) 1158 delay = 0; 1159 else { 1160 delay = 1161 (arc4random() % (maxdelay - mindelay)) + 1162 mindelay; 1163 } 1164 } 1165 nd6_dad_start((struct ifaddr *)ia, delay); 1166 } 1167 1168 KASSERT(hostIsNew, ("in6_update_ifa: !hostIsNew")); 1169 ifa_free(&ia->ia_ifa); 1170 return (error); 1171 1172 unlink: 1173 /* 1174 * XXX: if a change of an existing address failed, keep the entry 1175 * anyway. 1176 */ 1177 if (hostIsNew) { 1178 in6_unlink_ifa(ia, ifp); 1179 ifa_free(&ia->ia_ifa); 1180 } 1181 return (error); 1182 1183 cleanup: 1184 KASSERT(hostIsNew, ("in6_update_ifa: cleanup: !hostIsNew")); 1185 ifa_free(&ia->ia_ifa); 1186 in6_purgeaddr(&ia->ia_ifa); 1187 return error; 1188 } 1189 1190 void 1191 in6_purgeaddr(struct ifaddr *ifa) 1192 { 1193 struct ifnet *ifp = ifa->ifa_ifp; 1194 struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa; 1195 struct in6_multi_mship *imm; 1196 struct sockaddr_in6 mltaddr, mltmask; 1197 struct rtentry rt0; 1198 struct sockaddr_dl gateway; 1199 struct sockaddr_in6 mask, addr; 1200 int plen, error; 1201 struct rtentry *rt; 1202 struct ifaddr *ifa0, *nifa; 1203 1204 /* 1205 * find another IPv6 address as the gateway for the 1206 * link-local and node-local all-nodes multicast 1207 * address routes 1208 */ 1209 IF_ADDR_LOCK(ifp); 1210 TAILQ_FOREACH_SAFE(ifa0, &ifp->if_addrhead, ifa_link, nifa) { 1211 if ((ifa0->ifa_addr->sa_family != AF_INET6) || 1212 memcmp(&satosin6(ifa0->ifa_addr)->sin6_addr, 1213 &ia->ia_addr.sin6_addr, 1214 sizeof(struct in6_addr)) == 0) 1215 continue; 1216 else 1217 break; 1218 } 1219 if (ifa0 != NULL) 1220 ifa_ref(ifa0); 1221 IF_ADDR_UNLOCK(ifp); 1222 1223 /* 1224 * Remove the loopback route to the interface address. 1225 * The check for the current setting of "nd6_useloopback" 1226 * is not needed. 1227 */ 1228 if (ia->ia_flags & IFA_RTSELF) { 1229 error = ifa_del_loopback_route((struct ifaddr *)ia, 1230 (struct sockaddr *)&ia->ia_addr); 1231 if (error == 0) 1232 ia->ia_flags &= ~IFA_RTSELF; 1233 } 1234 1235 /* stop DAD processing */ 1236 nd6_dad_stop(ifa); 1237 1238 IF_AFDATA_LOCK(ifp); 1239 lla_lookup(LLTABLE6(ifp), (LLE_DELETE | LLE_IFADDR), 1240 (struct sockaddr *)&ia->ia_addr); 1241 IF_AFDATA_UNLOCK(ifp); 1242 1243 /* 1244 * initialize for rtmsg generation 1245 */ 1246 bzero(&gateway, sizeof(gateway)); 1247 gateway.sdl_len = sizeof(gateway); 1248 gateway.sdl_family = AF_LINK; 1249 gateway.sdl_nlen = 0; 1250 gateway.sdl_alen = ifp->if_addrlen; 1251 /* */ 1252 bzero(&rt0, sizeof(rt0)); 1253 rt0.rt_gateway = (struct sockaddr *)&gateway; 1254 memcpy(&mask, &ia->ia_prefixmask, sizeof(ia->ia_prefixmask)); 1255 memcpy(&addr, &ia->ia_addr, sizeof(ia->ia_addr)); 1256 rt_mask(&rt0) = (struct sockaddr *)&mask; 1257 rt_key(&rt0) = (struct sockaddr *)&addr; 1258 rt0.rt_flags = RTF_HOST | RTF_STATIC; 1259 rt_newaddrmsg(RTM_DELETE, ifa, 0, &rt0); 1260 1261 /* 1262 * leave from multicast groups we have joined for the interface 1263 */ 1264 while ((imm = ia->ia6_memberships.lh_first) != NULL) { 1265 LIST_REMOVE(imm, i6mm_chain); 1266 in6_leavegroup(imm); 1267 } 1268 1269 /* 1270 * remove the link-local all-nodes address 1271 */ 1272 bzero(&mltmask, sizeof(mltmask)); 1273 mltmask.sin6_len = sizeof(struct sockaddr_in6); 1274 mltmask.sin6_family = AF_INET6; 1275 mltmask.sin6_addr = in6mask32; 1276 1277 bzero(&mltaddr, sizeof(mltaddr)); 1278 mltaddr.sin6_len = sizeof(struct sockaddr_in6); 1279 mltaddr.sin6_family = AF_INET6; 1280 mltaddr.sin6_addr = in6addr_linklocal_allnodes; 1281 1282 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 1283 0) 1284 goto cleanup; 1285 1286 rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL); 1287 if (rt != NULL && rt->rt_gateway != NULL && 1288 (memcmp(&satosin6(rt->rt_gateway)->sin6_addr, 1289 &ia->ia_addr.sin6_addr, 1290 sizeof(ia->ia_addr.sin6_addr)) == 0)) { 1291 /* 1292 * if no more IPv6 address exists on this interface 1293 * then remove the multicast address route 1294 */ 1295 if (ifa0 == NULL) { 1296 memcpy(&mltaddr.sin6_addr, &satosin6(rt_key(rt))->sin6_addr, 1297 sizeof(mltaddr.sin6_addr)); 1298 RTFREE_LOCKED(rt); 1299 error = rtrequest(RTM_DELETE, (struct sockaddr *)&mltaddr, 1300 (struct sockaddr *)&ia->ia_addr, 1301 (struct sockaddr *)&mltmask, RTF_UP, 1302 (struct rtentry **)0); 1303 if (error) 1304 log(LOG_INFO, "in6_purgeaddr: link-local all-nodes" 1305 "multicast address deletion error\n"); 1306 } else { 1307 /* 1308 * replace the gateway of the route 1309 */ 1310 struct sockaddr_in6 sa; 1311 1312 bzero(&sa, sizeof(sa)); 1313 sa.sin6_len = sizeof(struct sockaddr_in6); 1314 sa.sin6_family = AF_INET6; 1315 memcpy(&sa.sin6_addr, &satosin6(ifa0->ifa_addr)->sin6_addr, 1316 sizeof(sa.sin6_addr)); 1317 in6_setscope(&sa.sin6_addr, ifa0->ifa_ifp, NULL); 1318 memcpy(rt->rt_gateway, &sa, sizeof(sa)); 1319 RTFREE_LOCKED(rt); 1320 } 1321 } else { 1322 if (rt != NULL) 1323 RTFREE_LOCKED(rt); 1324 } 1325 1326 /* 1327 * remove the node-local all-nodes address 1328 */ 1329 mltaddr.sin6_addr = in6addr_nodelocal_allnodes; 1330 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 1331 0) 1332 goto cleanup; 1333 1334 rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL); 1335 if (rt != NULL && rt->rt_gateway != NULL && 1336 (memcmp(&satosin6(rt->rt_gateway)->sin6_addr, 1337 &ia->ia_addr.sin6_addr, 1338 sizeof(ia->ia_addr.sin6_addr)) == 0)) { 1339 /* 1340 * if no more IPv6 address exists on this interface 1341 * then remove the multicast address route 1342 */ 1343 if (ifa0 == NULL) { 1344 memcpy(&mltaddr.sin6_addr, &satosin6(rt_key(rt))->sin6_addr, 1345 sizeof(mltaddr.sin6_addr)); 1346 1347 RTFREE_LOCKED(rt); 1348 error = rtrequest(RTM_DELETE, (struct sockaddr *)&mltaddr, 1349 (struct sockaddr *)&ia->ia_addr, 1350 (struct sockaddr *)&mltmask, RTF_UP, 1351 (struct rtentry **)0); 1352 1353 if (error) 1354 log(LOG_INFO, "in6_purgeaddr: node-local all-nodes" 1355 "multicast address deletion error\n"); 1356 } else { 1357 /* 1358 * replace the gateway of the route 1359 */ 1360 struct sockaddr_in6 sa; 1361 1362 bzero(&sa, sizeof(sa)); 1363 sa.sin6_len = sizeof(struct sockaddr_in6); 1364 sa.sin6_family = AF_INET6; 1365 memcpy(&sa.sin6_addr, &satosin6(ifa0->ifa_addr)->sin6_addr, 1366 sizeof(sa.sin6_addr)); 1367 in6_setscope(&sa.sin6_addr, ifa0->ifa_ifp, NULL); 1368 memcpy(rt->rt_gateway, &sa, sizeof(sa)); 1369 RTFREE_LOCKED(rt); 1370 } 1371 } else { 1372 if (rt != NULL) 1373 RTFREE_LOCKED(rt); 1374 } 1375 1376 cleanup: 1377 1378 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */ 1379 if ((ia->ia_flags & IFA_ROUTE) && plen == 128) { 1380 int error; 1381 struct sockaddr *dstaddr; 1382 1383 /* 1384 * use the interface address if configuring an 1385 * interface address with a /128 prefix len 1386 */ 1387 if (ia->ia_dstaddr.sin6_family == AF_INET6) 1388 dstaddr = (struct sockaddr *)&ia->ia_dstaddr; 1389 else 1390 dstaddr = (struct sockaddr *)&ia->ia_addr; 1391 1392 error = rtrequest(RTM_DELETE, 1393 (struct sockaddr *)dstaddr, 1394 (struct sockaddr *)&ia->ia_addr, 1395 (struct sockaddr *)&ia->ia_prefixmask, 1396 ia->ia_flags | RTF_HOST, NULL); 1397 if (error != 0) 1398 return; 1399 ia->ia_flags &= ~IFA_ROUTE; 1400 } 1401 if (ifa0 != NULL) 1402 ifa_free(ifa0); 1403 1404 in6_unlink_ifa(ia, ifp); 1405 } 1406 1407 static void 1408 in6_unlink_ifa(struct in6_ifaddr *ia, struct ifnet *ifp) 1409 { 1410 int s = splnet(); 1411 1412 IF_ADDR_LOCK(ifp); 1413 TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link); 1414 IF_ADDR_UNLOCK(ifp); 1415 ifa_free(&ia->ia_ifa); /* if_addrhead */ 1416 1417 /* 1418 * Defer the release of what might be the last reference to the 1419 * in6_ifaddr so that it can't be freed before the remainder of the 1420 * cleanup. 1421 */ 1422 IN6_IFADDR_WLOCK(); 1423 TAILQ_REMOVE(&V_in6_ifaddrhead, ia, ia_link); 1424 IN6_IFADDR_WUNLOCK(); 1425 1426 /* 1427 * Release the reference to the base prefix. There should be a 1428 * positive reference. 1429 */ 1430 if (ia->ia6_ndpr == NULL) { 1431 nd6log((LOG_NOTICE, 1432 "in6_unlink_ifa: autoconf'ed address " 1433 "%p has no prefix\n", ia)); 1434 } else { 1435 ia->ia6_ndpr->ndpr_refcnt--; 1436 ia->ia6_ndpr = NULL; 1437 } 1438 1439 /* 1440 * Also, if the address being removed is autoconf'ed, call 1441 * pfxlist_onlink_check() since the release might affect the status of 1442 * other (detached) addresses. 1443 */ 1444 if ((ia->ia6_flags & IN6_IFF_AUTOCONF)) { 1445 pfxlist_onlink_check(); 1446 } 1447 ifa_free(&ia->ia_ifa); /* in6_ifaddrhead */ 1448 splx(s); 1449 } 1450 1451 void 1452 in6_purgeif(struct ifnet *ifp) 1453 { 1454 struct ifaddr *ifa, *nifa; 1455 1456 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, nifa) { 1457 if (ifa->ifa_addr->sa_family != AF_INET6) 1458 continue; 1459 in6_purgeaddr(ifa); 1460 } 1461 1462 in6_ifdetach(ifp); 1463 } 1464 1465 /* 1466 * SIOC[GAD]LIFADDR. 1467 * SIOCGLIFADDR: get first address. (?) 1468 * SIOCGLIFADDR with IFLR_PREFIX: 1469 * get first address that matches the specified prefix. 1470 * SIOCALIFADDR: add the specified address. 1471 * SIOCALIFADDR with IFLR_PREFIX: 1472 * add the specified prefix, filling hostid part from 1473 * the first link-local address. prefixlen must be <= 64. 1474 * SIOCDLIFADDR: delete the specified address. 1475 * SIOCDLIFADDR with IFLR_PREFIX: 1476 * delete the first address that matches the specified prefix. 1477 * return values: 1478 * EINVAL on invalid parameters 1479 * EADDRNOTAVAIL on prefix match failed/specified address not found 1480 * other values may be returned from in6_ioctl() 1481 * 1482 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64. 1483 * this is to accomodate address naming scheme other than RFC2374, 1484 * in the future. 1485 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374 1486 * address encoding scheme. (see figure on page 8) 1487 */ 1488 static int 1489 in6_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data, 1490 struct ifnet *ifp, struct thread *td) 1491 { 1492 struct if_laddrreq *iflr = (struct if_laddrreq *)data; 1493 struct ifaddr *ifa; 1494 struct sockaddr *sa; 1495 1496 /* sanity checks */ 1497 if (!data || !ifp) { 1498 panic("invalid argument to in6_lifaddr_ioctl"); 1499 /* NOTREACHED */ 1500 } 1501 1502 switch (cmd) { 1503 case SIOCGLIFADDR: 1504 /* address must be specified on GET with IFLR_PREFIX */ 1505 if ((iflr->flags & IFLR_PREFIX) == 0) 1506 break; 1507 /* FALLTHROUGH */ 1508 case SIOCALIFADDR: 1509 case SIOCDLIFADDR: 1510 /* address must be specified on ADD and DELETE */ 1511 sa = (struct sockaddr *)&iflr->addr; 1512 if (sa->sa_family != AF_INET6) 1513 return EINVAL; 1514 if (sa->sa_len != sizeof(struct sockaddr_in6)) 1515 return EINVAL; 1516 /* XXX need improvement */ 1517 sa = (struct sockaddr *)&iflr->dstaddr; 1518 if (sa->sa_family && sa->sa_family != AF_INET6) 1519 return EINVAL; 1520 if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6)) 1521 return EINVAL; 1522 break; 1523 default: /* shouldn't happen */ 1524 #if 0 1525 panic("invalid cmd to in6_lifaddr_ioctl"); 1526 /* NOTREACHED */ 1527 #else 1528 return EOPNOTSUPP; 1529 #endif 1530 } 1531 if (sizeof(struct in6_addr) * 8 < iflr->prefixlen) 1532 return EINVAL; 1533 1534 switch (cmd) { 1535 case SIOCALIFADDR: 1536 { 1537 struct in6_aliasreq ifra; 1538 struct in6_addr *hostid = NULL; 1539 int prefixlen; 1540 1541 ifa = NULL; 1542 if ((iflr->flags & IFLR_PREFIX) != 0) { 1543 struct sockaddr_in6 *sin6; 1544 1545 /* 1546 * hostid is to fill in the hostid part of the 1547 * address. hostid points to the first link-local 1548 * address attached to the interface. 1549 */ 1550 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0); 1551 if (!ifa) 1552 return EADDRNOTAVAIL; 1553 hostid = IFA_IN6(ifa); 1554 1555 /* prefixlen must be <= 64. */ 1556 if (64 < iflr->prefixlen) 1557 return EINVAL; 1558 prefixlen = iflr->prefixlen; 1559 1560 /* hostid part must be zero. */ 1561 sin6 = (struct sockaddr_in6 *)&iflr->addr; 1562 if (sin6->sin6_addr.s6_addr32[2] != 0 || 1563 sin6->sin6_addr.s6_addr32[3] != 0) { 1564 return EINVAL; 1565 } 1566 } else 1567 prefixlen = iflr->prefixlen; 1568 1569 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */ 1570 bzero(&ifra, sizeof(ifra)); 1571 bcopy(iflr->iflr_name, ifra.ifra_name, sizeof(ifra.ifra_name)); 1572 1573 bcopy(&iflr->addr, &ifra.ifra_addr, 1574 ((struct sockaddr *)&iflr->addr)->sa_len); 1575 if (hostid) { 1576 /* fill in hostid part */ 1577 ifra.ifra_addr.sin6_addr.s6_addr32[2] = 1578 hostid->s6_addr32[2]; 1579 ifra.ifra_addr.sin6_addr.s6_addr32[3] = 1580 hostid->s6_addr32[3]; 1581 } 1582 1583 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /* XXX */ 1584 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr, 1585 ((struct sockaddr *)&iflr->dstaddr)->sa_len); 1586 if (hostid) { 1587 ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] = 1588 hostid->s6_addr32[2]; 1589 ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] = 1590 hostid->s6_addr32[3]; 1591 } 1592 } 1593 if (ifa != NULL) 1594 ifa_free(ifa); 1595 1596 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6); 1597 in6_prefixlen2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen); 1598 1599 ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX; 1600 return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, td); 1601 } 1602 case SIOCGLIFADDR: 1603 case SIOCDLIFADDR: 1604 { 1605 struct in6_ifaddr *ia; 1606 struct in6_addr mask, candidate, match; 1607 struct sockaddr_in6 *sin6; 1608 int cmp; 1609 1610 bzero(&mask, sizeof(mask)); 1611 if (iflr->flags & IFLR_PREFIX) { 1612 /* lookup a prefix rather than address. */ 1613 in6_prefixlen2mask(&mask, iflr->prefixlen); 1614 1615 sin6 = (struct sockaddr_in6 *)&iflr->addr; 1616 bcopy(&sin6->sin6_addr, &match, sizeof(match)); 1617 match.s6_addr32[0] &= mask.s6_addr32[0]; 1618 match.s6_addr32[1] &= mask.s6_addr32[1]; 1619 match.s6_addr32[2] &= mask.s6_addr32[2]; 1620 match.s6_addr32[3] &= mask.s6_addr32[3]; 1621 1622 /* if you set extra bits, that's wrong */ 1623 if (bcmp(&match, &sin6->sin6_addr, sizeof(match))) 1624 return EINVAL; 1625 1626 cmp = 1; 1627 } else { 1628 if (cmd == SIOCGLIFADDR) { 1629 /* on getting an address, take the 1st match */ 1630 cmp = 0; /* XXX */ 1631 } else { 1632 /* on deleting an address, do exact match */ 1633 in6_prefixlen2mask(&mask, 128); 1634 sin6 = (struct sockaddr_in6 *)&iflr->addr; 1635 bcopy(&sin6->sin6_addr, &match, sizeof(match)); 1636 1637 cmp = 1; 1638 } 1639 } 1640 1641 IF_ADDR_LOCK(ifp); 1642 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1643 if (ifa->ifa_addr->sa_family != AF_INET6) 1644 continue; 1645 if (!cmp) 1646 break; 1647 1648 /* 1649 * XXX: this is adhoc, but is necessary to allow 1650 * a user to specify fe80::/64 (not /10) for a 1651 * link-local address. 1652 */ 1653 bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate)); 1654 in6_clearscope(&candidate); 1655 candidate.s6_addr32[0] &= mask.s6_addr32[0]; 1656 candidate.s6_addr32[1] &= mask.s6_addr32[1]; 1657 candidate.s6_addr32[2] &= mask.s6_addr32[2]; 1658 candidate.s6_addr32[3] &= mask.s6_addr32[3]; 1659 if (IN6_ARE_ADDR_EQUAL(&candidate, &match)) 1660 break; 1661 } 1662 IF_ADDR_UNLOCK(ifp); 1663 if (!ifa) 1664 return EADDRNOTAVAIL; 1665 ia = ifa2ia6(ifa); 1666 1667 if (cmd == SIOCGLIFADDR) { 1668 int error; 1669 1670 /* fill in the if_laddrreq structure */ 1671 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len); 1672 error = sa6_recoverscope( 1673 (struct sockaddr_in6 *)&iflr->addr); 1674 if (error != 0) 1675 return (error); 1676 1677 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 1678 bcopy(&ia->ia_dstaddr, &iflr->dstaddr, 1679 ia->ia_dstaddr.sin6_len); 1680 error = sa6_recoverscope( 1681 (struct sockaddr_in6 *)&iflr->dstaddr); 1682 if (error != 0) 1683 return (error); 1684 } else 1685 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr)); 1686 1687 iflr->prefixlen = 1688 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); 1689 1690 iflr->flags = ia->ia6_flags; /* XXX */ 1691 1692 return 0; 1693 } else { 1694 struct in6_aliasreq ifra; 1695 1696 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */ 1697 bzero(&ifra, sizeof(ifra)); 1698 bcopy(iflr->iflr_name, ifra.ifra_name, 1699 sizeof(ifra.ifra_name)); 1700 1701 bcopy(&ia->ia_addr, &ifra.ifra_addr, 1702 ia->ia_addr.sin6_len); 1703 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 1704 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr, 1705 ia->ia_dstaddr.sin6_len); 1706 } else { 1707 bzero(&ifra.ifra_dstaddr, 1708 sizeof(ifra.ifra_dstaddr)); 1709 } 1710 bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr, 1711 ia->ia_prefixmask.sin6_len); 1712 1713 ifra.ifra_flags = ia->ia6_flags; 1714 return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra, 1715 ifp, td); 1716 } 1717 } 1718 } 1719 1720 return EOPNOTSUPP; /* just for safety */ 1721 } 1722 1723 /* 1724 * Initialize an interface's intetnet6 address 1725 * and routing table entry. 1726 */ 1727 static int 1728 in6_ifinit(struct ifnet *ifp, struct in6_ifaddr *ia, 1729 struct sockaddr_in6 *sin6, int newhost) 1730 { 1731 int error = 0, plen, ifacount = 0; 1732 int s = splimp(); 1733 struct ifaddr *ifa; 1734 1735 /* 1736 * Give the interface a chance to initialize 1737 * if this is its first address, 1738 * and to validate the address if necessary. 1739 */ 1740 IF_ADDR_LOCK(ifp); 1741 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1742 if (ifa->ifa_addr->sa_family != AF_INET6) 1743 continue; 1744 ifacount++; 1745 } 1746 IF_ADDR_UNLOCK(ifp); 1747 1748 ia->ia_addr = *sin6; 1749 1750 if (ifacount <= 1 && ifp->if_ioctl) { 1751 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia); 1752 if (error) { 1753 splx(s); 1754 return (error); 1755 } 1756 } 1757 splx(s); 1758 1759 ia->ia_ifa.ifa_metric = ifp->if_metric; 1760 1761 /* we could do in(6)_socktrim here, but just omit it at this moment. */ 1762 1763 /* 1764 * Special case: 1765 * If a new destination address is specified for a point-to-point 1766 * interface, install a route to the destination as an interface 1767 * direct route. 1768 * XXX: the logic below rejects assigning multiple addresses on a p2p 1769 * interface that share the same destination. 1770 */ 1771 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */ 1772 if (!(ia->ia_flags & IFA_ROUTE) && plen == 128 && 1773 ia->ia_dstaddr.sin6_family == AF_INET6) { 1774 int rtflags = RTF_UP | RTF_HOST; 1775 1776 error = rtrequest(RTM_ADD, 1777 (struct sockaddr *)&ia->ia_dstaddr, 1778 (struct sockaddr *)&ia->ia_addr, 1779 (struct sockaddr *)&ia->ia_prefixmask, 1780 ia->ia_flags | rtflags, NULL); 1781 if (error != 0) 1782 return (error); 1783 ia->ia_flags |= IFA_ROUTE; 1784 } 1785 1786 /* 1787 * add a loopback route to self 1788 */ 1789 if (!(ia->ia_flags & IFA_ROUTE) 1790 && (V_nd6_useloopback 1791 || (ifp->if_flags & IFF_LOOPBACK))) { 1792 error = ifa_add_loopback_route((struct ifaddr *)ia, 1793 (struct sockaddr *)&ia->ia_addr); 1794 if (error == 0) 1795 ia->ia_flags |= IFA_RTSELF; 1796 } 1797 1798 /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */ 1799 if (newhost) { 1800 struct llentry *ln; 1801 struct rtentry rt; 1802 struct sockaddr_dl gateway; 1803 struct sockaddr_in6 mask, addr; 1804 1805 IF_AFDATA_LOCK(ifp); 1806 ia->ia_ifa.ifa_rtrequest = NULL; 1807 1808 /* XXX QL 1809 * we need to report rt_newaddrmsg 1810 */ 1811 ln = lla_lookup(LLTABLE6(ifp), (LLE_CREATE | LLE_IFADDR | LLE_EXCLUSIVE), 1812 (struct sockaddr *)&ia->ia_addr); 1813 IF_AFDATA_UNLOCK(ifp); 1814 if (ln != NULL) { 1815 ln->la_expire = 0; /* for IPv6 this means permanent */ 1816 ln->ln_state = ND6_LLINFO_REACHABLE; 1817 /* 1818 * initialize for rtmsg generation 1819 */ 1820 bzero(&gateway, sizeof(gateway)); 1821 gateway.sdl_len = sizeof(gateway); 1822 gateway.sdl_family = AF_LINK; 1823 gateway.sdl_nlen = 0; 1824 gateway.sdl_alen = 6; 1825 memcpy(gateway.sdl_data, &ln->ll_addr.mac_aligned, sizeof(ln->ll_addr)); 1826 /* */ 1827 LLE_WUNLOCK(ln); 1828 } 1829 1830 bzero(&rt, sizeof(rt)); 1831 rt.rt_gateway = (struct sockaddr *)&gateway; 1832 memcpy(&mask, &ia->ia_prefixmask, sizeof(ia->ia_prefixmask)); 1833 memcpy(&addr, &ia->ia_addr, sizeof(ia->ia_addr)); 1834 rt_mask(&rt) = (struct sockaddr *)&mask; 1835 rt_key(&rt) = (struct sockaddr *)&addr; 1836 rt.rt_flags = RTF_UP | RTF_HOST | RTF_STATIC; 1837 rt_newaddrmsg(RTM_ADD, &ia->ia_ifa, 0, &rt); 1838 } 1839 1840 return (error); 1841 } 1842 1843 /* 1844 * Find an IPv6 interface link-local address specific to an interface. 1845 * ifaddr is returned referenced. 1846 */ 1847 struct in6_ifaddr * 1848 in6ifa_ifpforlinklocal(struct ifnet *ifp, int ignoreflags) 1849 { 1850 struct ifaddr *ifa; 1851 1852 IF_ADDR_LOCK(ifp); 1853 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1854 if (ifa->ifa_addr->sa_family != AF_INET6) 1855 continue; 1856 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) { 1857 if ((((struct in6_ifaddr *)ifa)->ia6_flags & 1858 ignoreflags) != 0) 1859 continue; 1860 ifa_ref(ifa); 1861 break; 1862 } 1863 } 1864 IF_ADDR_UNLOCK(ifp); 1865 1866 return ((struct in6_ifaddr *)ifa); 1867 } 1868 1869 1870 /* 1871 * find the internet address corresponding to a given interface and address. 1872 * ifaddr is returned referenced. 1873 */ 1874 struct in6_ifaddr * 1875 in6ifa_ifpwithaddr(struct ifnet *ifp, struct in6_addr *addr) 1876 { 1877 struct ifaddr *ifa; 1878 1879 IF_ADDR_LOCK(ifp); 1880 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1881 if (ifa->ifa_addr->sa_family != AF_INET6) 1882 continue; 1883 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa))) { 1884 ifa_ref(ifa); 1885 break; 1886 } 1887 } 1888 IF_ADDR_UNLOCK(ifp); 1889 1890 return ((struct in6_ifaddr *)ifa); 1891 } 1892 1893 /* 1894 * Convert IP6 address to printable (loggable) representation. Caller 1895 * has to make sure that ip6buf is at least INET6_ADDRSTRLEN long. 1896 */ 1897 static char digits[] = "0123456789abcdef"; 1898 char * 1899 ip6_sprintf(char *ip6buf, const struct in6_addr *addr) 1900 { 1901 int i, cnt = 0, maxcnt = 0, idx = 0, index = 0; 1902 char *cp; 1903 const u_int16_t *a = (const u_int16_t *)addr; 1904 const u_int8_t *d; 1905 int dcolon = 0, zero = 0; 1906 1907 cp = ip6buf; 1908 1909 for (i = 0; i < 8; i++) { 1910 if (*(a + i) == 0) { 1911 cnt++; 1912 if (cnt == 1) 1913 idx = i; 1914 } 1915 else if (maxcnt < cnt) { 1916 maxcnt = cnt; 1917 index = idx; 1918 cnt = 0; 1919 } 1920 } 1921 if (maxcnt < cnt) { 1922 maxcnt = cnt; 1923 index = idx; 1924 } 1925 1926 for (i = 0; i < 8; i++) { 1927 if (dcolon == 1) { 1928 if (*a == 0) { 1929 if (i == 7) 1930 *cp++ = ':'; 1931 a++; 1932 continue; 1933 } else 1934 dcolon = 2; 1935 } 1936 if (*a == 0) { 1937 if (dcolon == 0 && *(a + 1) == 0 && i == index) { 1938 if (i == 0) 1939 *cp++ = ':'; 1940 *cp++ = ':'; 1941 dcolon = 1; 1942 } else { 1943 *cp++ = '0'; 1944 *cp++ = ':'; 1945 } 1946 a++; 1947 continue; 1948 } 1949 d = (const u_char *)a; 1950 /* Try to eliminate leading zeros in printout like in :0001. */ 1951 zero = 1; 1952 *cp = digits[*d >> 4]; 1953 if (*cp != '0') { 1954 zero = 0; 1955 cp++; 1956 } 1957 *cp = digits[*d++ & 0xf]; 1958 if (zero == 0 || (*cp != '0')) { 1959 zero = 0; 1960 cp++; 1961 } 1962 *cp = digits[*d >> 4]; 1963 if (zero == 0 || (*cp != '0')) { 1964 zero = 0; 1965 cp++; 1966 } 1967 *cp++ = digits[*d & 0xf]; 1968 *cp++ = ':'; 1969 a++; 1970 } 1971 *--cp = '\0'; 1972 return (ip6buf); 1973 } 1974 1975 int 1976 in6_localaddr(struct in6_addr *in6) 1977 { 1978 struct in6_ifaddr *ia; 1979 1980 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6)) 1981 return 1; 1982 1983 IN6_IFADDR_RLOCK(); 1984 TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) { 1985 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr, 1986 &ia->ia_prefixmask.sin6_addr)) { 1987 IN6_IFADDR_RUNLOCK(); 1988 return 1; 1989 } 1990 } 1991 IN6_IFADDR_RUNLOCK(); 1992 1993 return (0); 1994 } 1995 1996 int 1997 in6_is_addr_deprecated(struct sockaddr_in6 *sa6) 1998 { 1999 struct in6_ifaddr *ia; 2000 2001 IN6_IFADDR_RLOCK(); 2002 TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) { 2003 if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, 2004 &sa6->sin6_addr) && 2005 (ia->ia6_flags & IN6_IFF_DEPRECATED) != 0) { 2006 IN6_IFADDR_RUNLOCK(); 2007 return (1); /* true */ 2008 } 2009 2010 /* XXX: do we still have to go thru the rest of the list? */ 2011 } 2012 IN6_IFADDR_RUNLOCK(); 2013 2014 return (0); /* false */ 2015 } 2016 2017 /* 2018 * return length of part which dst and src are equal 2019 * hard coding... 2020 */ 2021 int 2022 in6_matchlen(struct in6_addr *src, struct in6_addr *dst) 2023 { 2024 int match = 0; 2025 u_char *s = (u_char *)src, *d = (u_char *)dst; 2026 u_char *lim = s + 16, r; 2027 2028 while (s < lim) 2029 if ((r = (*d++ ^ *s++)) != 0) { 2030 while (r < 128) { 2031 match++; 2032 r <<= 1; 2033 } 2034 break; 2035 } else 2036 match += 8; 2037 return match; 2038 } 2039 2040 /* XXX: to be scope conscious */ 2041 int 2042 in6_are_prefix_equal(struct in6_addr *p1, struct in6_addr *p2, int len) 2043 { 2044 int bytelen, bitlen; 2045 2046 /* sanity check */ 2047 if (0 > len || len > 128) { 2048 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n", 2049 len); 2050 return (0); 2051 } 2052 2053 bytelen = len / 8; 2054 bitlen = len % 8; 2055 2056 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen)) 2057 return (0); 2058 if (bitlen != 0 && 2059 p1->s6_addr[bytelen] >> (8 - bitlen) != 2060 p2->s6_addr[bytelen] >> (8 - bitlen)) 2061 return (0); 2062 2063 return (1); 2064 } 2065 2066 void 2067 in6_prefixlen2mask(struct in6_addr *maskp, int len) 2068 { 2069 u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff}; 2070 int bytelen, bitlen, i; 2071 2072 /* sanity check */ 2073 if (0 > len || len > 128) { 2074 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n", 2075 len); 2076 return; 2077 } 2078 2079 bzero(maskp, sizeof(*maskp)); 2080 bytelen = len / 8; 2081 bitlen = len % 8; 2082 for (i = 0; i < bytelen; i++) 2083 maskp->s6_addr[i] = 0xff; 2084 if (bitlen) 2085 maskp->s6_addr[bytelen] = maskarray[bitlen - 1]; 2086 } 2087 2088 /* 2089 * return the best address out of the same scope. if no address was 2090 * found, return the first valid address from designated IF. 2091 */ 2092 struct in6_ifaddr * 2093 in6_ifawithifp(struct ifnet *ifp, struct in6_addr *dst) 2094 { 2095 int dst_scope = in6_addrscope(dst), blen = -1, tlen; 2096 struct ifaddr *ifa; 2097 struct in6_ifaddr *besta = 0; 2098 struct in6_ifaddr *dep[2]; /* last-resort: deprecated */ 2099 2100 dep[0] = dep[1] = NULL; 2101 2102 /* 2103 * We first look for addresses in the same scope. 2104 * If there is one, return it. 2105 * If two or more, return one which matches the dst longest. 2106 * If none, return one of global addresses assigned other ifs. 2107 */ 2108 IF_ADDR_LOCK(ifp); 2109 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 2110 if (ifa->ifa_addr->sa_family != AF_INET6) 2111 continue; 2112 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST) 2113 continue; /* XXX: is there any case to allow anycast? */ 2114 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY) 2115 continue; /* don't use this interface */ 2116 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED) 2117 continue; 2118 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) { 2119 if (V_ip6_use_deprecated) 2120 dep[0] = (struct in6_ifaddr *)ifa; 2121 continue; 2122 } 2123 2124 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) { 2125 /* 2126 * call in6_matchlen() as few as possible 2127 */ 2128 if (besta) { 2129 if (blen == -1) 2130 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst); 2131 tlen = in6_matchlen(IFA_IN6(ifa), dst); 2132 if (tlen > blen) { 2133 blen = tlen; 2134 besta = (struct in6_ifaddr *)ifa; 2135 } 2136 } else 2137 besta = (struct in6_ifaddr *)ifa; 2138 } 2139 } 2140 if (besta) { 2141 ifa_ref(&besta->ia_ifa); 2142 IF_ADDR_UNLOCK(ifp); 2143 return (besta); 2144 } 2145 IF_ADDR_UNLOCK(ifp); 2146 2147 IN6_IFADDR_RLOCK(); 2148 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 2149 if (ifa->ifa_addr->sa_family != AF_INET6) 2150 continue; 2151 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST) 2152 continue; /* XXX: is there any case to allow anycast? */ 2153 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY) 2154 continue; /* don't use this interface */ 2155 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED) 2156 continue; 2157 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) { 2158 if (V_ip6_use_deprecated) 2159 dep[1] = (struct in6_ifaddr *)ifa; 2160 continue; 2161 } 2162 2163 if (ifa != NULL) 2164 ifa_ref(ifa); 2165 IN6_IFADDR_RUNLOCK(); 2166 return (struct in6_ifaddr *)ifa; 2167 } 2168 IN6_IFADDR_RUNLOCK(); 2169 2170 /* use the last-resort values, that are, deprecated addresses */ 2171 if (dep[0]) 2172 return dep[0]; 2173 if (dep[1]) 2174 return dep[1]; 2175 2176 return NULL; 2177 } 2178 2179 /* 2180 * perform DAD when interface becomes IFF_UP. 2181 */ 2182 void 2183 in6_if_up(struct ifnet *ifp) 2184 { 2185 struct ifaddr *ifa; 2186 struct in6_ifaddr *ia; 2187 2188 IF_ADDR_LOCK(ifp); 2189 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 2190 if (ifa->ifa_addr->sa_family != AF_INET6) 2191 continue; 2192 ia = (struct in6_ifaddr *)ifa; 2193 if (ia->ia6_flags & IN6_IFF_TENTATIVE) { 2194 /* 2195 * The TENTATIVE flag was likely set by hand 2196 * beforehand, implicitly indicating the need for DAD. 2197 * We may be able to skip the random delay in this 2198 * case, but we impose delays just in case. 2199 */ 2200 nd6_dad_start(ifa, 2201 arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz)); 2202 } 2203 } 2204 IF_ADDR_UNLOCK(ifp); 2205 2206 /* 2207 * special cases, like 6to4, are handled in in6_ifattach 2208 */ 2209 in6_ifattach(ifp, NULL); 2210 } 2211 2212 int 2213 in6if_do_dad(struct ifnet *ifp) 2214 { 2215 if ((ifp->if_flags & IFF_LOOPBACK) != 0) 2216 return (0); 2217 2218 if (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) 2219 return (0); 2220 2221 switch (ifp->if_type) { 2222 #ifdef IFT_DUMMY 2223 case IFT_DUMMY: 2224 #endif 2225 case IFT_FAITH: 2226 /* 2227 * These interfaces do not have the IFF_LOOPBACK flag, 2228 * but loop packets back. We do not have to do DAD on such 2229 * interfaces. We should even omit it, because loop-backed 2230 * NS would confuse the DAD procedure. 2231 */ 2232 return (0); 2233 default: 2234 /* 2235 * Our DAD routine requires the interface up and running. 2236 * However, some interfaces can be up before the RUNNING 2237 * status. Additionaly, users may try to assign addresses 2238 * before the interface becomes up (or running). 2239 * We simply skip DAD in such a case as a work around. 2240 * XXX: we should rather mark "tentative" on such addresses, 2241 * and do DAD after the interface becomes ready. 2242 */ 2243 if (!((ifp->if_flags & IFF_UP) && 2244 (ifp->if_drv_flags & IFF_DRV_RUNNING))) 2245 return (0); 2246 2247 return (1); 2248 } 2249 } 2250 2251 /* 2252 * Calculate max IPv6 MTU through all the interfaces and store it 2253 * to in6_maxmtu. 2254 */ 2255 void 2256 in6_setmaxmtu(void) 2257 { 2258 unsigned long maxmtu = 0; 2259 struct ifnet *ifp; 2260 2261 IFNET_RLOCK_NOSLEEP(); 2262 for (ifp = TAILQ_FIRST(&V_ifnet); ifp; 2263 ifp = TAILQ_NEXT(ifp, if_list)) { 2264 /* this function can be called during ifnet initialization */ 2265 if (!ifp->if_afdata[AF_INET6]) 2266 continue; 2267 if ((ifp->if_flags & IFF_LOOPBACK) == 0 && 2268 IN6_LINKMTU(ifp) > maxmtu) 2269 maxmtu = IN6_LINKMTU(ifp); 2270 } 2271 IFNET_RUNLOCK_NOSLEEP(); 2272 if (maxmtu) /* update only when maxmtu is positive */ 2273 V_in6_maxmtu = maxmtu; 2274 } 2275 2276 /* 2277 * Provide the length of interface identifiers to be used for the link attached 2278 * to the given interface. The length should be defined in "IPv6 over 2279 * xxx-link" document. Note that address architecture might also define 2280 * the length for a particular set of address prefixes, regardless of the 2281 * link type. As clarified in rfc2462bis, those two definitions should be 2282 * consistent, and those really are as of August 2004. 2283 */ 2284 int 2285 in6_if2idlen(struct ifnet *ifp) 2286 { 2287 switch (ifp->if_type) { 2288 case IFT_ETHER: /* RFC2464 */ 2289 #ifdef IFT_PROPVIRTUAL 2290 case IFT_PROPVIRTUAL: /* XXX: no RFC. treat it as ether */ 2291 #endif 2292 #ifdef IFT_L2VLAN 2293 case IFT_L2VLAN: /* ditto */ 2294 #endif 2295 #ifdef IFT_IEEE80211 2296 case IFT_IEEE80211: /* ditto */ 2297 #endif 2298 #ifdef IFT_MIP 2299 case IFT_MIP: /* ditto */ 2300 #endif 2301 case IFT_INFINIBAND: 2302 return (64); 2303 case IFT_FDDI: /* RFC2467 */ 2304 return (64); 2305 case IFT_ISO88025: /* RFC2470 (IPv6 over Token Ring) */ 2306 return (64); 2307 case IFT_PPP: /* RFC2472 */ 2308 return (64); 2309 case IFT_ARCNET: /* RFC2497 */ 2310 return (64); 2311 case IFT_FRELAY: /* RFC2590 */ 2312 return (64); 2313 case IFT_IEEE1394: /* RFC3146 */ 2314 return (64); 2315 case IFT_GIF: 2316 return (64); /* draft-ietf-v6ops-mech-v2-07 */ 2317 case IFT_LOOP: 2318 return (64); /* XXX: is this really correct? */ 2319 default: 2320 /* 2321 * Unknown link type: 2322 * It might be controversial to use the today's common constant 2323 * of 64 for these cases unconditionally. For full compliance, 2324 * we should return an error in this case. On the other hand, 2325 * if we simply miss the standard for the link type or a new 2326 * standard is defined for a new link type, the IFID length 2327 * is very likely to be the common constant. As a compromise, 2328 * we always use the constant, but make an explicit notice 2329 * indicating the "unknown" case. 2330 */ 2331 printf("in6_if2idlen: unknown link type (%d)\n", ifp->if_type); 2332 return (64); 2333 } 2334 } 2335 2336 #include <sys/sysctl.h> 2337 2338 struct in6_llentry { 2339 struct llentry base; 2340 struct sockaddr_in6 l3_addr6; 2341 }; 2342 2343 static struct llentry * 2344 in6_lltable_new(const struct sockaddr *l3addr, u_int flags) 2345 { 2346 struct in6_llentry *lle; 2347 2348 lle = malloc(sizeof(struct in6_llentry), M_LLTABLE, 2349 M_DONTWAIT | M_ZERO); 2350 if (lle == NULL) /* NB: caller generates msg */ 2351 return NULL; 2352 2353 lle->l3_addr6 = *(const struct sockaddr_in6 *)l3addr; 2354 lle->base.lle_refcnt = 1; 2355 LLE_LOCK_INIT(&lle->base); 2356 callout_init_rw(&lle->base.ln_timer_ch, &lle->base.lle_lock, 2357 CALLOUT_RETURNUNLOCKED); 2358 2359 return &lle->base; 2360 } 2361 2362 /* 2363 * Deletes an address from the address table. 2364 * This function is called by the timer functions 2365 * such as arptimer() and nd6_llinfo_timer(), and 2366 * the caller does the locking. 2367 */ 2368 static void 2369 in6_lltable_free(struct lltable *llt, struct llentry *lle) 2370 { 2371 LLE_WUNLOCK(lle); 2372 LLE_LOCK_DESTROY(lle); 2373 free(lle, M_LLTABLE); 2374 } 2375 2376 static void 2377 in6_lltable_prefix_free(struct lltable *llt, 2378 const struct sockaddr *prefix, 2379 const struct sockaddr *mask, 2380 u_int flags) 2381 { 2382 const struct sockaddr_in6 *pfx = (const struct sockaddr_in6 *)prefix; 2383 const struct sockaddr_in6 *msk = (const struct sockaddr_in6 *)mask; 2384 struct llentry *lle, *next; 2385 register int i; 2386 2387 /* 2388 * (flags & LLE_STATIC) means deleting all entries 2389 * including static ND6 entries 2390 */ 2391 for (i=0; i < LLTBL_HASHTBL_SIZE; i++) { 2392 LIST_FOREACH_SAFE(lle, &llt->lle_head[i], lle_next, next) { 2393 if (IN6_ARE_MASKED_ADDR_EQUAL( 2394 &((struct sockaddr_in6 *)L3_ADDR(lle))->sin6_addr, 2395 &pfx->sin6_addr, 2396 &msk->sin6_addr) && 2397 ((flags & LLE_STATIC) || !(lle->la_flags & LLE_STATIC))) { 2398 int canceled; 2399 2400 canceled = callout_drain(&lle->la_timer); 2401 LLE_WLOCK(lle); 2402 if (canceled) 2403 LLE_REMREF(lle); 2404 llentry_free(lle); 2405 } 2406 } 2407 } 2408 } 2409 2410 static int 2411 in6_lltable_rtcheck(struct ifnet *ifp, 2412 u_int flags, 2413 const struct sockaddr *l3addr) 2414 { 2415 struct rtentry *rt; 2416 char ip6buf[INET6_ADDRSTRLEN]; 2417 2418 KASSERT(l3addr->sa_family == AF_INET6, 2419 ("sin_family %d", l3addr->sa_family)); 2420 2421 /* XXX rtalloc1 should take a const param */ 2422 rt = rtalloc1(__DECONST(struct sockaddr *, l3addr), 0, 0); 2423 if (rt == NULL || (rt->rt_flags & RTF_GATEWAY) || rt->rt_ifp != ifp) { 2424 struct ifaddr *ifa; 2425 /* 2426 * Create an ND6 cache for an IPv6 neighbor 2427 * that is not covered by our own prefix. 2428 */ 2429 /* XXX ifaof_ifpforaddr should take a const param */ 2430 ifa = ifaof_ifpforaddr(__DECONST(struct sockaddr *, l3addr), ifp); 2431 if (ifa != NULL) { 2432 ifa_free(ifa); 2433 if (rt != NULL) 2434 RTFREE_LOCKED(rt); 2435 return 0; 2436 } 2437 log(LOG_INFO, "IPv6 address: \"%s\" is not on the network\n", 2438 ip6_sprintf(ip6buf, &((const struct sockaddr_in6 *)l3addr)->sin6_addr)); 2439 if (rt != NULL) 2440 RTFREE_LOCKED(rt); 2441 return EINVAL; 2442 } 2443 RTFREE_LOCKED(rt); 2444 return 0; 2445 } 2446 2447 static struct llentry * 2448 in6_lltable_lookup(struct lltable *llt, u_int flags, 2449 const struct sockaddr *l3addr) 2450 { 2451 const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)l3addr; 2452 struct ifnet *ifp = llt->llt_ifp; 2453 struct llentry *lle; 2454 struct llentries *lleh; 2455 u_int hashkey; 2456 2457 IF_AFDATA_LOCK_ASSERT(ifp); 2458 KASSERT(l3addr->sa_family == AF_INET6, 2459 ("sin_family %d", l3addr->sa_family)); 2460 2461 hashkey = sin6->sin6_addr.s6_addr32[3]; 2462 lleh = &llt->lle_head[LLATBL_HASH(hashkey, LLTBL_HASHMASK)]; 2463 LIST_FOREACH(lle, lleh, lle_next) { 2464 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)L3_ADDR(lle); 2465 if (lle->la_flags & LLE_DELETED) 2466 continue; 2467 if (bcmp(&sa6->sin6_addr, &sin6->sin6_addr, 2468 sizeof(struct in6_addr)) == 0) 2469 break; 2470 } 2471 2472 if (lle == NULL) { 2473 if (!(flags & LLE_CREATE)) 2474 return (NULL); 2475 /* 2476 * A route that covers the given address must have 2477 * been installed 1st because we are doing a resolution, 2478 * verify this. 2479 */ 2480 if (!(flags & LLE_IFADDR) && 2481 in6_lltable_rtcheck(ifp, flags, l3addr) != 0) 2482 return NULL; 2483 2484 lle = in6_lltable_new(l3addr, flags); 2485 if (lle == NULL) { 2486 log(LOG_INFO, "lla_lookup: new lle malloc failed\n"); 2487 return NULL; 2488 } 2489 lle->la_flags = flags & ~LLE_CREATE; 2490 if ((flags & (LLE_CREATE | LLE_IFADDR)) == (LLE_CREATE | LLE_IFADDR)) { 2491 bcopy(IF_LLADDR(ifp), &lle->ll_addr, ifp->if_addrlen); 2492 lle->la_flags |= (LLE_VALID | LLE_STATIC); 2493 } 2494 2495 lle->lle_tbl = llt; 2496 lle->lle_head = lleh; 2497 LIST_INSERT_HEAD(lleh, lle, lle_next); 2498 } else if (flags & LLE_DELETE) { 2499 if (!(lle->la_flags & LLE_IFADDR) || (flags & LLE_IFADDR)) { 2500 LLE_WLOCK(lle); 2501 lle->la_flags = LLE_DELETED; 2502 LLE_WUNLOCK(lle); 2503 #ifdef DIAGNOSTIC 2504 log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle); 2505 #endif 2506 } 2507 lle = (void *)-1; 2508 } 2509 if (LLE_IS_VALID(lle)) { 2510 if (flags & LLE_EXCLUSIVE) 2511 LLE_WLOCK(lle); 2512 else 2513 LLE_RLOCK(lle); 2514 } 2515 return (lle); 2516 } 2517 2518 static int 2519 in6_lltable_dump(struct lltable *llt, struct sysctl_req *wr) 2520 { 2521 struct ifnet *ifp = llt->llt_ifp; 2522 struct llentry *lle; 2523 /* XXX stack use */ 2524 struct { 2525 struct rt_msghdr rtm; 2526 struct sockaddr_in6 sin6; 2527 /* 2528 * ndp.c assumes that sdl is word aligned 2529 */ 2530 #ifdef __LP64__ 2531 uint32_t pad; 2532 #endif 2533 struct sockaddr_dl sdl; 2534 } ndpc; 2535 int i, error; 2536 2537 if (ifp->if_flags & IFF_LOOPBACK) 2538 return 0; 2539 2540 LLTABLE_LOCK_ASSERT(); 2541 2542 error = 0; 2543 for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) { 2544 LIST_FOREACH(lle, &llt->lle_head[i], lle_next) { 2545 struct sockaddr_dl *sdl; 2546 2547 /* skip deleted or invalid entries */ 2548 if ((lle->la_flags & (LLE_DELETED|LLE_VALID)) != LLE_VALID) 2549 continue; 2550 /* Skip if jailed and not a valid IP of the prison. */ 2551 if (prison_if(wr->td->td_ucred, L3_ADDR(lle)) != 0) 2552 continue; 2553 /* 2554 * produce a msg made of: 2555 * struct rt_msghdr; 2556 * struct sockaddr_in6 (IPv6) 2557 * struct sockaddr_dl; 2558 */ 2559 bzero(&ndpc, sizeof(ndpc)); 2560 ndpc.rtm.rtm_msglen = sizeof(ndpc); 2561 ndpc.rtm.rtm_version = RTM_VERSION; 2562 ndpc.rtm.rtm_type = RTM_GET; 2563 ndpc.rtm.rtm_flags = RTF_UP; 2564 ndpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY; 2565 ndpc.sin6.sin6_family = AF_INET6; 2566 ndpc.sin6.sin6_len = sizeof(ndpc.sin6); 2567 bcopy(L3_ADDR(lle), &ndpc.sin6, L3_ADDR_LEN(lle)); 2568 2569 /* publish */ 2570 if (lle->la_flags & LLE_PUB) 2571 ndpc.rtm.rtm_flags |= RTF_ANNOUNCE; 2572 2573 sdl = &ndpc.sdl; 2574 sdl->sdl_family = AF_LINK; 2575 sdl->sdl_len = sizeof(*sdl); 2576 sdl->sdl_alen = ifp->if_addrlen; 2577 sdl->sdl_index = ifp->if_index; 2578 sdl->sdl_type = ifp->if_type; 2579 bcopy(&lle->ll_addr, LLADDR(sdl), ifp->if_addrlen); 2580 ndpc.rtm.rtm_rmx.rmx_expire = 2581 lle->la_flags & LLE_STATIC ? 0 : lle->la_expire; 2582 ndpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA); 2583 if (lle->la_flags & LLE_STATIC) 2584 ndpc.rtm.rtm_flags |= RTF_STATIC; 2585 ndpc.rtm.rtm_index = ifp->if_index; 2586 error = SYSCTL_OUT(wr, &ndpc, sizeof(ndpc)); 2587 if (error) 2588 break; 2589 } 2590 } 2591 return error; 2592 } 2593 2594 void * 2595 in6_domifattach(struct ifnet *ifp) 2596 { 2597 struct in6_ifextra *ext; 2598 2599 ext = (struct in6_ifextra *)malloc(sizeof(*ext), M_IFADDR, M_WAITOK); 2600 bzero(ext, sizeof(*ext)); 2601 2602 ext->in6_ifstat = (struct in6_ifstat *)malloc(sizeof(struct in6_ifstat), 2603 M_IFADDR, M_WAITOK); 2604 bzero(ext->in6_ifstat, sizeof(*ext->in6_ifstat)); 2605 2606 ext->icmp6_ifstat = 2607 (struct icmp6_ifstat *)malloc(sizeof(struct icmp6_ifstat), 2608 M_IFADDR, M_WAITOK); 2609 bzero(ext->icmp6_ifstat, sizeof(*ext->icmp6_ifstat)); 2610 2611 ext->nd_ifinfo = nd6_ifattach(ifp); 2612 ext->scope6_id = scope6_ifattach(ifp); 2613 ext->lltable = lltable_init(ifp, AF_INET6); 2614 if (ext->lltable != NULL) { 2615 ext->lltable->llt_new = in6_lltable_new; 2616 ext->lltable->llt_free = in6_lltable_free; 2617 ext->lltable->llt_prefix_free = in6_lltable_prefix_free; 2618 ext->lltable->llt_rtcheck = in6_lltable_rtcheck; 2619 ext->lltable->llt_lookup = in6_lltable_lookup; 2620 ext->lltable->llt_dump = in6_lltable_dump; 2621 } 2622 2623 ext->mld_ifinfo = mld_domifattach(ifp); 2624 2625 return ext; 2626 } 2627 2628 void 2629 in6_domifdetach(struct ifnet *ifp, void *aux) 2630 { 2631 struct in6_ifextra *ext = (struct in6_ifextra *)aux; 2632 2633 mld_domifdetach(ifp); 2634 scope6_ifdetach(ext->scope6_id); 2635 nd6_ifdetach(ext->nd_ifinfo); 2636 lltable_free(ext->lltable); 2637 free(ext->in6_ifstat, M_IFADDR); 2638 free(ext->icmp6_ifstat, M_IFADDR); 2639 free(ext, M_IFADDR); 2640 } 2641 2642 /* 2643 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be 2644 * v4 mapped addr or v4 compat addr 2645 */ 2646 void 2647 in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6) 2648 { 2649 2650 bzero(sin, sizeof(*sin)); 2651 sin->sin_len = sizeof(struct sockaddr_in); 2652 sin->sin_family = AF_INET; 2653 sin->sin_port = sin6->sin6_port; 2654 sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3]; 2655 } 2656 2657 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */ 2658 void 2659 in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6) 2660 { 2661 bzero(sin6, sizeof(*sin6)); 2662 sin6->sin6_len = sizeof(struct sockaddr_in6); 2663 sin6->sin6_family = AF_INET6; 2664 sin6->sin6_port = sin->sin_port; 2665 sin6->sin6_addr.s6_addr32[0] = 0; 2666 sin6->sin6_addr.s6_addr32[1] = 0; 2667 sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP; 2668 sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr; 2669 } 2670 2671 /* Convert sockaddr_in6 into sockaddr_in. */ 2672 void 2673 in6_sin6_2_sin_in_sock(struct sockaddr *nam) 2674 { 2675 struct sockaddr_in *sin_p; 2676 struct sockaddr_in6 sin6; 2677 2678 /* 2679 * Save original sockaddr_in6 addr and convert it 2680 * to sockaddr_in. 2681 */ 2682 sin6 = *(struct sockaddr_in6 *)nam; 2683 sin_p = (struct sockaddr_in *)nam; 2684 in6_sin6_2_sin(sin_p, &sin6); 2685 } 2686 2687 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */ 2688 void 2689 in6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam) 2690 { 2691 struct sockaddr_in *sin_p; 2692 struct sockaddr_in6 *sin6_p; 2693 2694 sin6_p = malloc(sizeof *sin6_p, M_SONAME, 2695 M_WAITOK); 2696 sin_p = (struct sockaddr_in *)*nam; 2697 in6_sin_2_v4mapsin6(sin_p, sin6_p); 2698 free(*nam, M_SONAME); 2699 *nam = (struct sockaddr *)sin6_p; 2700 } 2701