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