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