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 ia->ia_prefixmask.sin6_family = AF_INET6; 1190 } 1191 1192 /* 1193 * If a new destination address is specified, scrub the old one and 1194 * install the new destination. Note that the interface must be 1195 * p2p or loopback (see the check above.) 1196 */ 1197 if (dst6.sin6_family == AF_INET6 && 1198 !IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr, &ia->ia_dstaddr.sin6_addr)) { 1199 int e; 1200 1201 if ((ia->ia_flags & IFA_ROUTE) != 0 && 1202 (e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST)) != 0) { 1203 nd6log((LOG_ERR, "in6_update_ifa: failed to remove " 1204 "a route to the old destination: %s\n", 1205 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr))); 1206 /* proceed anyway... */ 1207 } else 1208 ia->ia_flags &= ~IFA_ROUTE; 1209 ia->ia_dstaddr = dst6; 1210 } 1211 1212 /* 1213 * Set lifetimes. We do not refer to ia6t_expire and ia6t_preferred 1214 * to see if the address is deprecated or invalidated, but initialize 1215 * these members for applications. 1216 */ 1217 ia->ia6_lifetime = ifra->ifra_lifetime; 1218 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) { 1219 ia->ia6_lifetime.ia6t_expire = 1220 time_second + ia->ia6_lifetime.ia6t_vltime; 1221 } else 1222 ia->ia6_lifetime.ia6t_expire = 0; 1223 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) { 1224 ia->ia6_lifetime.ia6t_preferred = 1225 time_second + ia->ia6_lifetime.ia6t_pltime; 1226 } else 1227 ia->ia6_lifetime.ia6t_preferred = 0; 1228 1229 /* reset the interface and routing table appropriately. */ 1230 if ((error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew)) != 0) 1231 goto unlink; 1232 1233 /* 1234 * configure address flags. 1235 */ 1236 ia->ia6_flags = ifra->ifra_flags; 1237 /* 1238 * backward compatibility - if IN6_IFF_DEPRECATED is set from the 1239 * userland, make it deprecated. 1240 */ 1241 if ((ifra->ifra_flags & IN6_IFF_DEPRECATED) != 0) { 1242 ia->ia6_lifetime.ia6t_pltime = 0; 1243 ia->ia6_lifetime.ia6t_preferred = time_second; 1244 } 1245 /* 1246 * Make the address tentative before joining multicast addresses, 1247 * so that corresponding MLD responses would not have a tentative 1248 * source address. 1249 */ 1250 ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /* safety */ 1251 if (hostIsNew && in6if_do_dad(ifp)) 1252 ia->ia6_flags |= IN6_IFF_TENTATIVE; 1253 1254 /* DAD should be performed after ND6_IFF_IFDISABLED is cleared. */ 1255 if (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) 1256 ia->ia6_flags |= IN6_IFF_TENTATIVE; 1257 1258 /* 1259 * We are done if we have simply modified an existing address. 1260 */ 1261 if (!hostIsNew) 1262 return (error); 1263 1264 /* 1265 * Beyond this point, we should call in6_purgeaddr upon an error, 1266 * not just go to unlink. 1267 */ 1268 1269 /* Join necessary multicast groups. */ 1270 in6m_sol = NULL; 1271 if ((ifp->if_flags & IFF_MULTICAST) != 0) { 1272 error = in6_update_ifa_join_mc(ifp, ifra, ia, flags, &in6m_sol); 1273 if (error) 1274 goto cleanup; 1275 } 1276 1277 /* 1278 * Perform DAD, if needed. 1279 * XXX It may be of use, if we can administratively disable DAD. 1280 */ 1281 if (in6if_do_dad(ifp) && ((ifra->ifra_flags & IN6_IFF_NODAD) == 0) && 1282 (ia->ia6_flags & IN6_IFF_TENTATIVE)) 1283 { 1284 int mindelay, maxdelay; 1285 1286 delay = 0; 1287 if ((flags & IN6_IFAUPDATE_DADDELAY)) { 1288 /* 1289 * We need to impose a delay before sending an NS 1290 * for DAD. Check if we also needed a delay for the 1291 * corresponding MLD message. If we did, the delay 1292 * should be larger than the MLD delay (this could be 1293 * relaxed a bit, but this simple logic is at least 1294 * safe). 1295 * XXX: Break data hiding guidelines and look at 1296 * state for the solicited multicast group. 1297 */ 1298 mindelay = 0; 1299 if (in6m_sol != NULL && 1300 in6m_sol->in6m_state == MLD_REPORTING_MEMBER) { 1301 mindelay = in6m_sol->in6m_timer; 1302 } 1303 maxdelay = MAX_RTR_SOLICITATION_DELAY * hz; 1304 if (maxdelay - mindelay == 0) 1305 delay = 0; 1306 else { 1307 delay = 1308 (arc4random() % (maxdelay - mindelay)) + 1309 mindelay; 1310 } 1311 } 1312 nd6_dad_start((struct ifaddr *)ia, delay); 1313 } 1314 1315 KASSERT(hostIsNew, ("in6_update_ifa: !hostIsNew")); 1316 ifa_free(&ia->ia_ifa); 1317 return (error); 1318 1319 unlink: 1320 /* 1321 * XXX: if a change of an existing address failed, keep the entry 1322 * anyway. 1323 */ 1324 if (hostIsNew) { 1325 in6_unlink_ifa(ia, ifp); 1326 ifa_free(&ia->ia_ifa); 1327 } 1328 return (error); 1329 1330 cleanup: 1331 KASSERT(hostIsNew, ("in6_update_ifa: cleanup: !hostIsNew")); 1332 ifa_free(&ia->ia_ifa); 1333 in6_purgeaddr(&ia->ia_ifa); 1334 return error; 1335 } 1336 1337 /* 1338 * Leave multicast groups. Factored out from in6_purgeaddr(). 1339 * This entire work should only be done once, for the default FIB. 1340 */ 1341 static int 1342 in6_purgeaddr_mc(struct ifnet *ifp, struct in6_ifaddr *ia, struct ifaddr *ifa0) 1343 { 1344 struct sockaddr_in6 mltaddr, mltmask; 1345 struct in6_multi_mship *imm; 1346 struct rtentry *rt; 1347 struct sockaddr_in6 sin6; 1348 int error; 1349 1350 /* 1351 * Leave from multicast groups we have joined for the interface. 1352 */ 1353 while ((imm = LIST_FIRST(&ia->ia6_memberships)) != NULL) { 1354 LIST_REMOVE(imm, i6mm_chain); 1355 in6_leavegroup(imm); 1356 } 1357 1358 /* 1359 * Remove the link-local all-nodes address. 1360 */ 1361 bzero(&mltmask, sizeof(mltmask)); 1362 mltmask.sin6_len = sizeof(struct sockaddr_in6); 1363 mltmask.sin6_family = AF_INET6; 1364 mltmask.sin6_addr = in6mask32; 1365 1366 bzero(&mltaddr, sizeof(mltaddr)); 1367 mltaddr.sin6_len = sizeof(struct sockaddr_in6); 1368 mltaddr.sin6_family = AF_INET6; 1369 mltaddr.sin6_addr = in6addr_linklocal_allnodes; 1370 1371 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 0) 1372 return (error); 1373 1374 /* 1375 * As for the mltaddr above, proactively prepare the sin6 to avoid 1376 * rtentry un- and re-locking. 1377 */ 1378 if (ifa0 != NULL) { 1379 bzero(&sin6, sizeof(sin6)); 1380 sin6.sin6_len = sizeof(sin6); 1381 sin6.sin6_family = AF_INET6; 1382 memcpy(&sin6.sin6_addr, &satosin6(ifa0->ifa_addr)->sin6_addr, 1383 sizeof(sin6.sin6_addr)); 1384 error = in6_setscope(&sin6.sin6_addr, ifa0->ifa_ifp, NULL); 1385 if (error != 0) 1386 return (error); 1387 } 1388 1389 rt = in6_rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL, RT_DEFAULT_FIB); 1390 if (rt != NULL && rt->rt_gateway != NULL && 1391 (memcmp(&satosin6(rt->rt_gateway)->sin6_addr, 1392 &ia->ia_addr.sin6_addr, 1393 sizeof(ia->ia_addr.sin6_addr)) == 0)) { 1394 /* 1395 * If no more IPv6 address exists on this interface then 1396 * remove the multicast address route. 1397 */ 1398 if (ifa0 == NULL) { 1399 memcpy(&mltaddr.sin6_addr, 1400 &satosin6(rt_key(rt))->sin6_addr, 1401 sizeof(mltaddr.sin6_addr)); 1402 RTFREE_LOCKED(rt); 1403 error = in6_rtrequest(RTM_DELETE, 1404 (struct sockaddr *)&mltaddr, 1405 (struct sockaddr *)&ia->ia_addr, 1406 (struct sockaddr *)&mltmask, RTF_UP, 1407 (struct rtentry **)0, RT_DEFAULT_FIB); 1408 if (error) 1409 log(LOG_INFO, "%s: link-local all-nodes " 1410 "multicast address deletion error\n", 1411 __func__); 1412 } else { 1413 /* 1414 * Replace the gateway of the route. 1415 */ 1416 memcpy(rt->rt_gateway, &sin6, sizeof(sin6)); 1417 RTFREE_LOCKED(rt); 1418 } 1419 } else { 1420 if (rt != NULL) 1421 RTFREE_LOCKED(rt); 1422 } 1423 1424 /* 1425 * Remove the node-local all-nodes address. 1426 */ 1427 mltaddr.sin6_addr = in6addr_nodelocal_allnodes; 1428 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 0) 1429 return (error); 1430 1431 rt = in6_rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL, RT_DEFAULT_FIB); 1432 if (rt != NULL && rt->rt_gateway != NULL && 1433 (memcmp(&satosin6(rt->rt_gateway)->sin6_addr, 1434 &ia->ia_addr.sin6_addr, 1435 sizeof(ia->ia_addr.sin6_addr)) == 0)) { 1436 /* 1437 * If no more IPv6 address exists on this interface then 1438 * remove the multicast address route. 1439 */ 1440 if (ifa0 == NULL) { 1441 memcpy(&mltaddr.sin6_addr, 1442 &satosin6(rt_key(rt))->sin6_addr, 1443 sizeof(mltaddr.sin6_addr)); 1444 1445 RTFREE_LOCKED(rt); 1446 error = in6_rtrequest(RTM_DELETE, 1447 (struct sockaddr *)&mltaddr, 1448 (struct sockaddr *)&ia->ia_addr, 1449 (struct sockaddr *)&mltmask, RTF_UP, 1450 (struct rtentry **)0, RT_DEFAULT_FIB); 1451 if (error) 1452 log(LOG_INFO, "%s: node-local all-nodes" 1453 "multicast address deletion error\n", 1454 __func__); 1455 } else { 1456 /* 1457 * Replace the gateway of the route. 1458 */ 1459 memcpy(rt->rt_gateway, &sin6, sizeof(sin6)); 1460 RTFREE_LOCKED(rt); 1461 } 1462 } else { 1463 if (rt != NULL) 1464 RTFREE_LOCKED(rt); 1465 } 1466 1467 return (0); 1468 } 1469 1470 void 1471 in6_purgeaddr(struct ifaddr *ifa) 1472 { 1473 struct ifnet *ifp = ifa->ifa_ifp; 1474 struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa; 1475 int plen, error; 1476 struct ifaddr *ifa0; 1477 1478 if (ifa->ifa_carp) 1479 (*carp_detach_p)(ifa); 1480 1481 /* 1482 * find another IPv6 address as the gateway for the 1483 * link-local and node-local all-nodes multicast 1484 * address routes 1485 */ 1486 IF_ADDR_RLOCK(ifp); 1487 TAILQ_FOREACH(ifa0, &ifp->if_addrhead, ifa_link) { 1488 if ((ifa0->ifa_addr->sa_family != AF_INET6) || 1489 memcmp(&satosin6(ifa0->ifa_addr)->sin6_addr, 1490 &ia->ia_addr.sin6_addr, sizeof(struct in6_addr)) == 0) 1491 continue; 1492 else 1493 break; 1494 } 1495 if (ifa0 != NULL) 1496 ifa_ref(ifa0); 1497 IF_ADDR_RUNLOCK(ifp); 1498 1499 /* 1500 * Remove the loopback route to the interface address. 1501 * The check for the current setting of "nd6_useloopback" 1502 * is not needed. 1503 */ 1504 if (ia->ia_flags & IFA_RTSELF) { 1505 error = ifa_del_loopback_route((struct ifaddr *)ia, 1506 (struct sockaddr *)&ia->ia_addr); 1507 if (error == 0) 1508 ia->ia_flags &= ~IFA_RTSELF; 1509 } 1510 1511 /* stop DAD processing */ 1512 nd6_dad_stop(ifa); 1513 1514 /* Remove local address entry from lltable. */ 1515 in6_ifremloop(ifa); 1516 1517 /* Leave multicast groups. */ 1518 error = in6_purgeaddr_mc(ifp, ia, ifa0); 1519 1520 if (ifa0 != NULL) 1521 ifa_free(ifa0); 1522 1523 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */ 1524 if ((ia->ia_flags & IFA_ROUTE) && plen == 128) { 1525 error = rtinit(&(ia->ia_ifa), RTM_DELETE, ia->ia_flags | 1526 (ia->ia_dstaddr.sin6_family == AF_INET6) ? RTF_HOST : 0); 1527 if (error != 0) 1528 log(LOG_INFO, "%s: err=%d, destination address delete " 1529 "failed\n", __func__, error); 1530 ia->ia_flags &= ~IFA_ROUTE; 1531 } 1532 1533 in6_unlink_ifa(ia, ifp); 1534 } 1535 1536 static void 1537 in6_unlink_ifa(struct in6_ifaddr *ia, struct ifnet *ifp) 1538 { 1539 1540 IF_ADDR_WLOCK(ifp); 1541 TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link); 1542 IF_ADDR_WUNLOCK(ifp); 1543 ifa_free(&ia->ia_ifa); /* if_addrhead */ 1544 1545 /* 1546 * Defer the release of what might be the last reference to the 1547 * in6_ifaddr so that it can't be freed before the remainder of the 1548 * cleanup. 1549 */ 1550 IN6_IFADDR_WLOCK(); 1551 TAILQ_REMOVE(&V_in6_ifaddrhead, ia, ia_link); 1552 LIST_REMOVE(ia, ia6_hash); 1553 IN6_IFADDR_WUNLOCK(); 1554 1555 /* 1556 * Release the reference to the base prefix. There should be a 1557 * positive reference. 1558 */ 1559 if (ia->ia6_ndpr == NULL) { 1560 nd6log((LOG_NOTICE, 1561 "in6_unlink_ifa: autoconf'ed address " 1562 "%p has no prefix\n", ia)); 1563 } else { 1564 ia->ia6_ndpr->ndpr_refcnt--; 1565 ia->ia6_ndpr = NULL; 1566 } 1567 1568 /* 1569 * Also, if the address being removed is autoconf'ed, call 1570 * pfxlist_onlink_check() since the release might affect the status of 1571 * other (detached) addresses. 1572 */ 1573 if ((ia->ia6_flags & IN6_IFF_AUTOCONF)) { 1574 pfxlist_onlink_check(); 1575 } 1576 ifa_free(&ia->ia_ifa); /* in6_ifaddrhead */ 1577 } 1578 1579 void 1580 in6_purgeif(struct ifnet *ifp) 1581 { 1582 struct ifaddr *ifa, *nifa; 1583 1584 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, nifa) { 1585 if (ifa->ifa_addr->sa_family != AF_INET6) 1586 continue; 1587 in6_purgeaddr(ifa); 1588 } 1589 1590 in6_ifdetach(ifp); 1591 } 1592 1593 /* 1594 * SIOC[GAD]LIFADDR. 1595 * SIOCGLIFADDR: get first address. (?) 1596 * SIOCGLIFADDR with IFLR_PREFIX: 1597 * get first address that matches the specified prefix. 1598 * SIOCALIFADDR: add the specified address. 1599 * SIOCALIFADDR with IFLR_PREFIX: 1600 * add the specified prefix, filling hostid part from 1601 * the first link-local address. prefixlen must be <= 64. 1602 * SIOCDLIFADDR: delete the specified address. 1603 * SIOCDLIFADDR with IFLR_PREFIX: 1604 * delete the first address that matches the specified prefix. 1605 * return values: 1606 * EINVAL on invalid parameters 1607 * EADDRNOTAVAIL on prefix match failed/specified address not found 1608 * other values may be returned from in6_ioctl() 1609 * 1610 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64. 1611 * this is to accomodate address naming scheme other than RFC2374, 1612 * in the future. 1613 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374 1614 * address encoding scheme. (see figure on page 8) 1615 */ 1616 static int 1617 in6_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data, 1618 struct ifnet *ifp, struct thread *td) 1619 { 1620 struct if_laddrreq *iflr = (struct if_laddrreq *)data; 1621 struct ifaddr *ifa; 1622 struct sockaddr *sa; 1623 1624 /* sanity checks */ 1625 if (!data || !ifp) { 1626 panic("invalid argument to in6_lifaddr_ioctl"); 1627 /* NOTREACHED */ 1628 } 1629 1630 switch (cmd) { 1631 case SIOCGLIFADDR: 1632 /* address must be specified on GET with IFLR_PREFIX */ 1633 if ((iflr->flags & IFLR_PREFIX) == 0) 1634 break; 1635 /* FALLTHROUGH */ 1636 case SIOCALIFADDR: 1637 case SIOCDLIFADDR: 1638 /* address must be specified on ADD and DELETE */ 1639 sa = (struct sockaddr *)&iflr->addr; 1640 if (sa->sa_family != AF_INET6) 1641 return EINVAL; 1642 if (sa->sa_len != sizeof(struct sockaddr_in6)) 1643 return EINVAL; 1644 /* XXX need improvement */ 1645 sa = (struct sockaddr *)&iflr->dstaddr; 1646 if (sa->sa_family && sa->sa_family != AF_INET6) 1647 return EINVAL; 1648 if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6)) 1649 return EINVAL; 1650 break; 1651 default: /* shouldn't happen */ 1652 #if 0 1653 panic("invalid cmd to in6_lifaddr_ioctl"); 1654 /* NOTREACHED */ 1655 #else 1656 return EOPNOTSUPP; 1657 #endif 1658 } 1659 if (sizeof(struct in6_addr) * 8 < iflr->prefixlen) 1660 return EINVAL; 1661 1662 switch (cmd) { 1663 case SIOCALIFADDR: 1664 { 1665 struct in6_aliasreq ifra; 1666 struct in6_addr *hostid = NULL; 1667 int prefixlen; 1668 1669 ifa = NULL; 1670 if ((iflr->flags & IFLR_PREFIX) != 0) { 1671 struct sockaddr_in6 *sin6; 1672 1673 /* 1674 * hostid is to fill in the hostid part of the 1675 * address. hostid points to the first link-local 1676 * address attached to the interface. 1677 */ 1678 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0); 1679 if (!ifa) 1680 return EADDRNOTAVAIL; 1681 hostid = IFA_IN6(ifa); 1682 1683 /* prefixlen must be <= 64. */ 1684 if (64 < iflr->prefixlen) { 1685 if (ifa != NULL) 1686 ifa_free(ifa); 1687 return EINVAL; 1688 } 1689 prefixlen = iflr->prefixlen; 1690 1691 /* hostid part must be zero. */ 1692 sin6 = (struct sockaddr_in6 *)&iflr->addr; 1693 if (sin6->sin6_addr.s6_addr32[2] != 0 || 1694 sin6->sin6_addr.s6_addr32[3] != 0) { 1695 if (ifa != NULL) 1696 ifa_free(ifa); 1697 return EINVAL; 1698 } 1699 } else 1700 prefixlen = iflr->prefixlen; 1701 1702 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */ 1703 bzero(&ifra, sizeof(ifra)); 1704 bcopy(iflr->iflr_name, ifra.ifra_name, sizeof(ifra.ifra_name)); 1705 1706 bcopy(&iflr->addr, &ifra.ifra_addr, 1707 ((struct sockaddr *)&iflr->addr)->sa_len); 1708 if (hostid) { 1709 /* fill in hostid part */ 1710 ifra.ifra_addr.sin6_addr.s6_addr32[2] = 1711 hostid->s6_addr32[2]; 1712 ifra.ifra_addr.sin6_addr.s6_addr32[3] = 1713 hostid->s6_addr32[3]; 1714 } 1715 1716 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /* XXX */ 1717 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr, 1718 ((struct sockaddr *)&iflr->dstaddr)->sa_len); 1719 if (hostid) { 1720 ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] = 1721 hostid->s6_addr32[2]; 1722 ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] = 1723 hostid->s6_addr32[3]; 1724 } 1725 } 1726 if (ifa != NULL) 1727 ifa_free(ifa); 1728 1729 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6); 1730 in6_prefixlen2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen); 1731 1732 ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX; 1733 return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, td); 1734 } 1735 case SIOCGLIFADDR: 1736 case SIOCDLIFADDR: 1737 { 1738 struct in6_ifaddr *ia; 1739 struct in6_addr mask, candidate, match; 1740 struct sockaddr_in6 *sin6; 1741 int cmp; 1742 1743 bzero(&mask, sizeof(mask)); 1744 if (iflr->flags & IFLR_PREFIX) { 1745 /* lookup a prefix rather than address. */ 1746 in6_prefixlen2mask(&mask, iflr->prefixlen); 1747 1748 sin6 = (struct sockaddr_in6 *)&iflr->addr; 1749 bcopy(&sin6->sin6_addr, &match, sizeof(match)); 1750 match.s6_addr32[0] &= mask.s6_addr32[0]; 1751 match.s6_addr32[1] &= mask.s6_addr32[1]; 1752 match.s6_addr32[2] &= mask.s6_addr32[2]; 1753 match.s6_addr32[3] &= mask.s6_addr32[3]; 1754 1755 /* if you set extra bits, that's wrong */ 1756 if (bcmp(&match, &sin6->sin6_addr, sizeof(match))) 1757 return EINVAL; 1758 1759 cmp = 1; 1760 } else { 1761 if (cmd == SIOCGLIFADDR) { 1762 /* on getting an address, take the 1st match */ 1763 cmp = 0; /* XXX */ 1764 } else { 1765 /* on deleting an address, do exact match */ 1766 in6_prefixlen2mask(&mask, 128); 1767 sin6 = (struct sockaddr_in6 *)&iflr->addr; 1768 bcopy(&sin6->sin6_addr, &match, sizeof(match)); 1769 1770 cmp = 1; 1771 } 1772 } 1773 1774 IF_ADDR_RLOCK(ifp); 1775 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1776 if (ifa->ifa_addr->sa_family != AF_INET6) 1777 continue; 1778 if (!cmp) 1779 break; 1780 1781 /* 1782 * XXX: this is adhoc, but is necessary to allow 1783 * a user to specify fe80::/64 (not /10) for a 1784 * link-local address. 1785 */ 1786 bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate)); 1787 in6_clearscope(&candidate); 1788 candidate.s6_addr32[0] &= mask.s6_addr32[0]; 1789 candidate.s6_addr32[1] &= mask.s6_addr32[1]; 1790 candidate.s6_addr32[2] &= mask.s6_addr32[2]; 1791 candidate.s6_addr32[3] &= mask.s6_addr32[3]; 1792 if (IN6_ARE_ADDR_EQUAL(&candidate, &match)) 1793 break; 1794 } 1795 if (ifa != NULL) 1796 ifa_ref(ifa); 1797 IF_ADDR_RUNLOCK(ifp); 1798 if (!ifa) 1799 return EADDRNOTAVAIL; 1800 ia = ifa2ia6(ifa); 1801 1802 if (cmd == SIOCGLIFADDR) { 1803 int error; 1804 1805 /* fill in the if_laddrreq structure */ 1806 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len); 1807 error = sa6_recoverscope( 1808 (struct sockaddr_in6 *)&iflr->addr); 1809 if (error != 0) { 1810 ifa_free(ifa); 1811 return (error); 1812 } 1813 1814 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 1815 bcopy(&ia->ia_dstaddr, &iflr->dstaddr, 1816 ia->ia_dstaddr.sin6_len); 1817 error = sa6_recoverscope( 1818 (struct sockaddr_in6 *)&iflr->dstaddr); 1819 if (error != 0) { 1820 ifa_free(ifa); 1821 return (error); 1822 } 1823 } else 1824 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr)); 1825 1826 iflr->prefixlen = 1827 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); 1828 1829 iflr->flags = ia->ia6_flags; /* XXX */ 1830 ifa_free(ifa); 1831 1832 return 0; 1833 } else { 1834 struct in6_aliasreq ifra; 1835 1836 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */ 1837 bzero(&ifra, sizeof(ifra)); 1838 bcopy(iflr->iflr_name, ifra.ifra_name, 1839 sizeof(ifra.ifra_name)); 1840 1841 bcopy(&ia->ia_addr, &ifra.ifra_addr, 1842 ia->ia_addr.sin6_len); 1843 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 1844 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr, 1845 ia->ia_dstaddr.sin6_len); 1846 } else { 1847 bzero(&ifra.ifra_dstaddr, 1848 sizeof(ifra.ifra_dstaddr)); 1849 } 1850 bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr, 1851 ia->ia_prefixmask.sin6_len); 1852 1853 ifra.ifra_flags = ia->ia6_flags; 1854 ifa_free(ifa); 1855 return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra, 1856 ifp, td); 1857 } 1858 } 1859 } 1860 1861 return EOPNOTSUPP; /* just for safety */ 1862 } 1863 1864 /* 1865 * Initialize an interface's IPv6 address and routing table entry. 1866 */ 1867 static int 1868 in6_ifinit(struct ifnet *ifp, struct in6_ifaddr *ia, 1869 struct sockaddr_in6 *sin6, int newhost) 1870 { 1871 int error = 0, plen, ifacount = 0; 1872 struct ifaddr *ifa; 1873 1874 /* 1875 * Give the interface a chance to initialize 1876 * if this is its first address, 1877 * and to validate the address if necessary. 1878 */ 1879 IF_ADDR_RLOCK(ifp); 1880 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1881 if (ifa->ifa_addr->sa_family != AF_INET6) 1882 continue; 1883 ifacount++; 1884 } 1885 IF_ADDR_RUNLOCK(ifp); 1886 1887 ia->ia_addr = *sin6; 1888 1889 if (ifacount <= 1 && ifp->if_ioctl) { 1890 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia); 1891 if (error) 1892 return (error); 1893 } 1894 1895 ia->ia_ifa.ifa_metric = ifp->if_metric; 1896 1897 /* we could do in(6)_socktrim here, but just omit it at this moment. */ 1898 1899 /* 1900 * Special case: 1901 * If a new destination address is specified for a point-to-point 1902 * interface, install a route to the destination as an interface 1903 * direct route. 1904 * XXX: the logic below rejects assigning multiple addresses on a p2p 1905 * interface that share the same destination. 1906 */ 1907 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */ 1908 if (!(ia->ia_flags & IFA_ROUTE) && plen == 128 && 1909 ia->ia_dstaddr.sin6_family == AF_INET6) { 1910 int rtflags = RTF_UP | RTF_HOST; 1911 error = rtinit(&ia->ia_ifa, RTM_ADD, ia->ia_flags | rtflags); 1912 if (error) 1913 return (error); 1914 ia->ia_flags |= IFA_ROUTE; 1915 /* 1916 * Handle the case for ::1 . 1917 */ 1918 if (ifp->if_flags & IFF_LOOPBACK) 1919 ia->ia_flags |= IFA_RTSELF; 1920 } 1921 1922 /* 1923 * add a loopback route to self 1924 */ 1925 if (!(ia->ia_flags & IFA_RTSELF) && V_nd6_useloopback) { 1926 error = ifa_add_loopback_route((struct ifaddr *)ia, 1927 (struct sockaddr *)&ia->ia_addr); 1928 if (error == 0) 1929 ia->ia_flags |= IFA_RTSELF; 1930 } 1931 1932 /* Add local address to lltable, if necessary (ex. on p2p link). */ 1933 if (newhost) 1934 in6_ifaddloop(&(ia->ia_ifa)); 1935 1936 return (error); 1937 } 1938 1939 /* 1940 * Find an IPv6 interface link-local address specific to an interface. 1941 * ifaddr is returned referenced. 1942 */ 1943 struct in6_ifaddr * 1944 in6ifa_ifpforlinklocal(struct ifnet *ifp, int ignoreflags) 1945 { 1946 struct ifaddr *ifa; 1947 1948 IF_ADDR_RLOCK(ifp); 1949 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1950 if (ifa->ifa_addr->sa_family != AF_INET6) 1951 continue; 1952 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) { 1953 if ((((struct in6_ifaddr *)ifa)->ia6_flags & 1954 ignoreflags) != 0) 1955 continue; 1956 ifa_ref(ifa); 1957 break; 1958 } 1959 } 1960 IF_ADDR_RUNLOCK(ifp); 1961 1962 return ((struct in6_ifaddr *)ifa); 1963 } 1964 1965 1966 /* 1967 * find the internet address corresponding to a given interface and address. 1968 * ifaddr is returned referenced. 1969 */ 1970 struct in6_ifaddr * 1971 in6ifa_ifpwithaddr(struct ifnet *ifp, struct in6_addr *addr) 1972 { 1973 struct ifaddr *ifa; 1974 1975 IF_ADDR_RLOCK(ifp); 1976 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1977 if (ifa->ifa_addr->sa_family != AF_INET6) 1978 continue; 1979 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa))) { 1980 ifa_ref(ifa); 1981 break; 1982 } 1983 } 1984 IF_ADDR_RUNLOCK(ifp); 1985 1986 return ((struct in6_ifaddr *)ifa); 1987 } 1988 1989 /* 1990 * Convert IP6 address to printable (loggable) representation. Caller 1991 * has to make sure that ip6buf is at least INET6_ADDRSTRLEN long. 1992 */ 1993 static char digits[] = "0123456789abcdef"; 1994 char * 1995 ip6_sprintf(char *ip6buf, const struct in6_addr *addr) 1996 { 1997 int i, cnt = 0, maxcnt = 0, idx = 0, index = 0; 1998 char *cp; 1999 const u_int16_t *a = (const u_int16_t *)addr; 2000 const u_int8_t *d; 2001 int dcolon = 0, zero = 0; 2002 2003 cp = ip6buf; 2004 2005 for (i = 0; i < 8; i++) { 2006 if (*(a + i) == 0) { 2007 cnt++; 2008 if (cnt == 1) 2009 idx = i; 2010 } 2011 else if (maxcnt < cnt) { 2012 maxcnt = cnt; 2013 index = idx; 2014 cnt = 0; 2015 } 2016 } 2017 if (maxcnt < cnt) { 2018 maxcnt = cnt; 2019 index = idx; 2020 } 2021 2022 for (i = 0; i < 8; i++) { 2023 if (dcolon == 1) { 2024 if (*a == 0) { 2025 if (i == 7) 2026 *cp++ = ':'; 2027 a++; 2028 continue; 2029 } else 2030 dcolon = 2; 2031 } 2032 if (*a == 0) { 2033 if (dcolon == 0 && *(a + 1) == 0 && i == index) { 2034 if (i == 0) 2035 *cp++ = ':'; 2036 *cp++ = ':'; 2037 dcolon = 1; 2038 } else { 2039 *cp++ = '0'; 2040 *cp++ = ':'; 2041 } 2042 a++; 2043 continue; 2044 } 2045 d = (const u_char *)a; 2046 /* Try to eliminate leading zeros in printout like in :0001. */ 2047 zero = 1; 2048 *cp = digits[*d >> 4]; 2049 if (*cp != '0') { 2050 zero = 0; 2051 cp++; 2052 } 2053 *cp = digits[*d++ & 0xf]; 2054 if (zero == 0 || (*cp != '0')) { 2055 zero = 0; 2056 cp++; 2057 } 2058 *cp = digits[*d >> 4]; 2059 if (zero == 0 || (*cp != '0')) { 2060 zero = 0; 2061 cp++; 2062 } 2063 *cp++ = digits[*d & 0xf]; 2064 *cp++ = ':'; 2065 a++; 2066 } 2067 *--cp = '\0'; 2068 return (ip6buf); 2069 } 2070 2071 int 2072 in6_localaddr(struct in6_addr *in6) 2073 { 2074 struct in6_ifaddr *ia; 2075 2076 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6)) 2077 return 1; 2078 2079 IN6_IFADDR_RLOCK(); 2080 TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) { 2081 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr, 2082 &ia->ia_prefixmask.sin6_addr)) { 2083 IN6_IFADDR_RUNLOCK(); 2084 return 1; 2085 } 2086 } 2087 IN6_IFADDR_RUNLOCK(); 2088 2089 return (0); 2090 } 2091 2092 /* 2093 * Return 1 if an internet address is for the local host and configured 2094 * on one of its interfaces. 2095 */ 2096 int 2097 in6_localip(struct in6_addr *in6) 2098 { 2099 struct in6_ifaddr *ia; 2100 2101 IN6_IFADDR_RLOCK(); 2102 LIST_FOREACH(ia, IN6ADDR_HASH(in6), ia6_hash) { 2103 if (IN6_ARE_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr)) { 2104 IN6_IFADDR_RUNLOCK(); 2105 return (1); 2106 } 2107 } 2108 IN6_IFADDR_RUNLOCK(); 2109 return (0); 2110 } 2111 2112 int 2113 in6_is_addr_deprecated(struct sockaddr_in6 *sa6) 2114 { 2115 struct in6_ifaddr *ia; 2116 2117 IN6_IFADDR_RLOCK(); 2118 LIST_FOREACH(ia, IN6ADDR_HASH(&sa6->sin6_addr), ia6_hash) { 2119 if (IN6_ARE_ADDR_EQUAL(IA6_IN6(ia), &sa6->sin6_addr)) { 2120 if (ia->ia6_flags & IN6_IFF_DEPRECATED) { 2121 IN6_IFADDR_RUNLOCK(); 2122 return (1); /* true */ 2123 } 2124 break; 2125 } 2126 } 2127 IN6_IFADDR_RUNLOCK(); 2128 2129 return (0); /* false */ 2130 } 2131 2132 /* 2133 * return length of part which dst and src are equal 2134 * hard coding... 2135 */ 2136 int 2137 in6_matchlen(struct in6_addr *src, struct in6_addr *dst) 2138 { 2139 int match = 0; 2140 u_char *s = (u_char *)src, *d = (u_char *)dst; 2141 u_char *lim = s + 16, r; 2142 2143 while (s < lim) 2144 if ((r = (*d++ ^ *s++)) != 0) { 2145 while (r < 128) { 2146 match++; 2147 r <<= 1; 2148 } 2149 break; 2150 } else 2151 match += 8; 2152 return match; 2153 } 2154 2155 /* XXX: to be scope conscious */ 2156 int 2157 in6_are_prefix_equal(struct in6_addr *p1, struct in6_addr *p2, int len) 2158 { 2159 int bytelen, bitlen; 2160 2161 /* sanity check */ 2162 if (0 > len || len > 128) { 2163 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n", 2164 len); 2165 return (0); 2166 } 2167 2168 bytelen = len / 8; 2169 bitlen = len % 8; 2170 2171 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen)) 2172 return (0); 2173 if (bitlen != 0 && 2174 p1->s6_addr[bytelen] >> (8 - bitlen) != 2175 p2->s6_addr[bytelen] >> (8 - bitlen)) 2176 return (0); 2177 2178 return (1); 2179 } 2180 2181 void 2182 in6_prefixlen2mask(struct in6_addr *maskp, int len) 2183 { 2184 u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff}; 2185 int bytelen, bitlen, i; 2186 2187 /* sanity check */ 2188 if (0 > len || len > 128) { 2189 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n", 2190 len); 2191 return; 2192 } 2193 2194 bzero(maskp, sizeof(*maskp)); 2195 bytelen = len / 8; 2196 bitlen = len % 8; 2197 for (i = 0; i < bytelen; i++) 2198 maskp->s6_addr[i] = 0xff; 2199 if (bitlen) 2200 maskp->s6_addr[bytelen] = maskarray[bitlen - 1]; 2201 } 2202 2203 /* 2204 * return the best address out of the same scope. if no address was 2205 * found, return the first valid address from designated IF. 2206 */ 2207 struct in6_ifaddr * 2208 in6_ifawithifp(struct ifnet *ifp, struct in6_addr *dst) 2209 { 2210 int dst_scope = in6_addrscope(dst), blen = -1, tlen; 2211 struct ifaddr *ifa; 2212 struct in6_ifaddr *besta = 0; 2213 struct in6_ifaddr *dep[2]; /* last-resort: deprecated */ 2214 2215 dep[0] = dep[1] = NULL; 2216 2217 /* 2218 * We first look for addresses in the same scope. 2219 * If there is one, return it. 2220 * If two or more, return one which matches the dst longest. 2221 * If none, return one of global addresses assigned other ifs. 2222 */ 2223 IF_ADDR_RLOCK(ifp); 2224 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 2225 if (ifa->ifa_addr->sa_family != AF_INET6) 2226 continue; 2227 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST) 2228 continue; /* XXX: is there any case to allow anycast? */ 2229 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY) 2230 continue; /* don't use this interface */ 2231 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED) 2232 continue; 2233 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) { 2234 if (V_ip6_use_deprecated) 2235 dep[0] = (struct in6_ifaddr *)ifa; 2236 continue; 2237 } 2238 2239 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) { 2240 /* 2241 * call in6_matchlen() as few as possible 2242 */ 2243 if (besta) { 2244 if (blen == -1) 2245 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst); 2246 tlen = in6_matchlen(IFA_IN6(ifa), dst); 2247 if (tlen > blen) { 2248 blen = tlen; 2249 besta = (struct in6_ifaddr *)ifa; 2250 } 2251 } else 2252 besta = (struct in6_ifaddr *)ifa; 2253 } 2254 } 2255 if (besta) { 2256 ifa_ref(&besta->ia_ifa); 2257 IF_ADDR_RUNLOCK(ifp); 2258 return (besta); 2259 } 2260 2261 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 2262 if (ifa->ifa_addr->sa_family != AF_INET6) 2263 continue; 2264 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST) 2265 continue; /* XXX: is there any case to allow anycast? */ 2266 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY) 2267 continue; /* don't use this interface */ 2268 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED) 2269 continue; 2270 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) { 2271 if (V_ip6_use_deprecated) 2272 dep[1] = (struct in6_ifaddr *)ifa; 2273 continue; 2274 } 2275 2276 if (ifa != NULL) 2277 ifa_ref(ifa); 2278 IF_ADDR_RUNLOCK(ifp); 2279 return (struct in6_ifaddr *)ifa; 2280 } 2281 2282 /* use the last-resort values, that are, deprecated addresses */ 2283 if (dep[0]) { 2284 ifa_ref((struct ifaddr *)dep[0]); 2285 IF_ADDR_RUNLOCK(ifp); 2286 return dep[0]; 2287 } 2288 if (dep[1]) { 2289 ifa_ref((struct ifaddr *)dep[1]); 2290 IF_ADDR_RUNLOCK(ifp); 2291 return dep[1]; 2292 } 2293 2294 IF_ADDR_RUNLOCK(ifp); 2295 return NULL; 2296 } 2297 2298 /* 2299 * perform DAD when interface becomes IFF_UP. 2300 */ 2301 void 2302 in6_if_up(struct ifnet *ifp) 2303 { 2304 struct ifaddr *ifa; 2305 struct in6_ifaddr *ia; 2306 2307 IF_ADDR_RLOCK(ifp); 2308 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 2309 if (ifa->ifa_addr->sa_family != AF_INET6) 2310 continue; 2311 ia = (struct in6_ifaddr *)ifa; 2312 if (ia->ia6_flags & IN6_IFF_TENTATIVE) { 2313 /* 2314 * The TENTATIVE flag was likely set by hand 2315 * beforehand, implicitly indicating the need for DAD. 2316 * We may be able to skip the random delay in this 2317 * case, but we impose delays just in case. 2318 */ 2319 nd6_dad_start(ifa, 2320 arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz)); 2321 } 2322 } 2323 IF_ADDR_RUNLOCK(ifp); 2324 2325 /* 2326 * special cases, like 6to4, are handled in in6_ifattach 2327 */ 2328 in6_ifattach(ifp, NULL); 2329 } 2330 2331 int 2332 in6if_do_dad(struct ifnet *ifp) 2333 { 2334 if ((ifp->if_flags & IFF_LOOPBACK) != 0) 2335 return (0); 2336 2337 if (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) 2338 return (0); 2339 2340 switch (ifp->if_type) { 2341 #ifdef IFT_DUMMY 2342 case IFT_DUMMY: 2343 #endif 2344 case IFT_FAITH: 2345 /* 2346 * These interfaces do not have the IFF_LOOPBACK flag, 2347 * but loop packets back. We do not have to do DAD on such 2348 * interfaces. We should even omit it, because loop-backed 2349 * NS would confuse the DAD procedure. 2350 */ 2351 return (0); 2352 default: 2353 /* 2354 * Our DAD routine requires the interface up and running. 2355 * However, some interfaces can be up before the RUNNING 2356 * status. Additionaly, users may try to assign addresses 2357 * before the interface becomes up (or running). 2358 * We simply skip DAD in such a case as a work around. 2359 * XXX: we should rather mark "tentative" on such addresses, 2360 * and do DAD after the interface becomes ready. 2361 */ 2362 if (!((ifp->if_flags & IFF_UP) && 2363 (ifp->if_drv_flags & IFF_DRV_RUNNING))) 2364 return (0); 2365 2366 return (1); 2367 } 2368 } 2369 2370 /* 2371 * Calculate max IPv6 MTU through all the interfaces and store it 2372 * to in6_maxmtu. 2373 */ 2374 void 2375 in6_setmaxmtu(void) 2376 { 2377 unsigned long maxmtu = 0; 2378 struct ifnet *ifp; 2379 2380 IFNET_RLOCK_NOSLEEP(); 2381 TAILQ_FOREACH(ifp, &V_ifnet, if_list) { 2382 /* this function can be called during ifnet initialization */ 2383 if (!ifp->if_afdata[AF_INET6]) 2384 continue; 2385 if ((ifp->if_flags & IFF_LOOPBACK) == 0 && 2386 IN6_LINKMTU(ifp) > maxmtu) 2387 maxmtu = IN6_LINKMTU(ifp); 2388 } 2389 IFNET_RUNLOCK_NOSLEEP(); 2390 if (maxmtu) /* update only when maxmtu is positive */ 2391 V_in6_maxmtu = maxmtu; 2392 } 2393 2394 /* 2395 * Provide the length of interface identifiers to be used for the link attached 2396 * to the given interface. The length should be defined in "IPv6 over 2397 * xxx-link" document. Note that address architecture might also define 2398 * the length for a particular set of address prefixes, regardless of the 2399 * link type. As clarified in rfc2462bis, those two definitions should be 2400 * consistent, and those really are as of August 2004. 2401 */ 2402 int 2403 in6_if2idlen(struct ifnet *ifp) 2404 { 2405 switch (ifp->if_type) { 2406 case IFT_ETHER: /* RFC2464 */ 2407 #ifdef IFT_PROPVIRTUAL 2408 case IFT_PROPVIRTUAL: /* XXX: no RFC. treat it as ether */ 2409 #endif 2410 #ifdef IFT_L2VLAN 2411 case IFT_L2VLAN: /* ditto */ 2412 #endif 2413 #ifdef IFT_IEEE80211 2414 case IFT_IEEE80211: /* ditto */ 2415 #endif 2416 #ifdef IFT_MIP 2417 case IFT_MIP: /* ditto */ 2418 #endif 2419 case IFT_INFINIBAND: 2420 return (64); 2421 case IFT_FDDI: /* RFC2467 */ 2422 return (64); 2423 case IFT_ISO88025: /* RFC2470 (IPv6 over Token Ring) */ 2424 return (64); 2425 case IFT_PPP: /* RFC2472 */ 2426 return (64); 2427 case IFT_ARCNET: /* RFC2497 */ 2428 return (64); 2429 case IFT_FRELAY: /* RFC2590 */ 2430 return (64); 2431 case IFT_IEEE1394: /* RFC3146 */ 2432 return (64); 2433 case IFT_GIF: 2434 return (64); /* draft-ietf-v6ops-mech-v2-07 */ 2435 case IFT_LOOP: 2436 return (64); /* XXX: is this really correct? */ 2437 default: 2438 /* 2439 * Unknown link type: 2440 * It might be controversial to use the today's common constant 2441 * of 64 for these cases unconditionally. For full compliance, 2442 * we should return an error in this case. On the other hand, 2443 * if we simply miss the standard for the link type or a new 2444 * standard is defined for a new link type, the IFID length 2445 * is very likely to be the common constant. As a compromise, 2446 * we always use the constant, but make an explicit notice 2447 * indicating the "unknown" case. 2448 */ 2449 printf("in6_if2idlen: unknown link type (%d)\n", ifp->if_type); 2450 return (64); 2451 } 2452 } 2453 2454 #include <sys/sysctl.h> 2455 2456 struct in6_llentry { 2457 struct llentry base; 2458 struct sockaddr_in6 l3_addr6; 2459 }; 2460 2461 /* 2462 * Deletes an address from the address table. 2463 * This function is called by the timer functions 2464 * such as arptimer() and nd6_llinfo_timer(), and 2465 * the caller does the locking. 2466 */ 2467 static void 2468 in6_lltable_free(struct lltable *llt, struct llentry *lle) 2469 { 2470 LLE_WUNLOCK(lle); 2471 LLE_LOCK_DESTROY(lle); 2472 free(lle, M_LLTABLE); 2473 } 2474 2475 static struct llentry * 2476 in6_lltable_new(const struct sockaddr *l3addr, u_int flags) 2477 { 2478 struct in6_llentry *lle; 2479 2480 lle = malloc(sizeof(struct in6_llentry), M_LLTABLE, M_NOWAIT | M_ZERO); 2481 if (lle == NULL) /* NB: caller generates msg */ 2482 return NULL; 2483 2484 lle->l3_addr6 = *(const struct sockaddr_in6 *)l3addr; 2485 lle->base.lle_refcnt = 1; 2486 lle->base.lle_free = in6_lltable_free; 2487 LLE_LOCK_INIT(&lle->base); 2488 callout_init_rw(&lle->base.ln_timer_ch, &lle->base.lle_lock, 2489 CALLOUT_RETURNUNLOCKED); 2490 2491 return (&lle->base); 2492 } 2493 2494 static void 2495 in6_lltable_prefix_free(struct lltable *llt, const struct sockaddr *prefix, 2496 const struct sockaddr *mask, u_int flags) 2497 { 2498 const struct sockaddr_in6 *pfx = (const struct sockaddr_in6 *)prefix; 2499 const struct sockaddr_in6 *msk = (const struct sockaddr_in6 *)mask; 2500 struct llentry *lle, *next; 2501 int i; 2502 2503 /* 2504 * (flags & LLE_STATIC) means deleting all entries 2505 * including static ND6 entries. 2506 */ 2507 IF_AFDATA_WLOCK(llt->llt_ifp); 2508 for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) { 2509 LIST_FOREACH_SAFE(lle, &llt->lle_head[i], lle_next, next) { 2510 if (IN6_ARE_MASKED_ADDR_EQUAL( 2511 &satosin6(L3_ADDR(lle))->sin6_addr, 2512 &pfx->sin6_addr, &msk->sin6_addr) && 2513 ((flags & LLE_STATIC) || 2514 !(lle->la_flags & LLE_STATIC))) { 2515 LLE_WLOCK(lle); 2516 if (callout_stop(&lle->la_timer)) 2517 LLE_REMREF(lle); 2518 llentry_free(lle); 2519 } 2520 } 2521 } 2522 IF_AFDATA_WUNLOCK(llt->llt_ifp); 2523 } 2524 2525 static int 2526 in6_lltable_rtcheck(struct ifnet *ifp, 2527 u_int flags, 2528 const struct sockaddr *l3addr) 2529 { 2530 struct rtentry *rt; 2531 char ip6buf[INET6_ADDRSTRLEN]; 2532 2533 KASSERT(l3addr->sa_family == AF_INET6, 2534 ("sin_family %d", l3addr->sa_family)); 2535 2536 /* Our local addresses are always only installed on the default FIB. */ 2537 /* XXX rtalloc1 should take a const param */ 2538 rt = in6_rtalloc1(__DECONST(struct sockaddr *, l3addr), 0, 0, 2539 RT_DEFAULT_FIB); 2540 if (rt == NULL || (rt->rt_flags & RTF_GATEWAY) || rt->rt_ifp != ifp) { 2541 struct ifaddr *ifa; 2542 /* 2543 * Create an ND6 cache for an IPv6 neighbor 2544 * that is not covered by our own prefix. 2545 */ 2546 /* XXX ifaof_ifpforaddr should take a const param */ 2547 ifa = ifaof_ifpforaddr(__DECONST(struct sockaddr *, l3addr), ifp); 2548 if (ifa != NULL) { 2549 ifa_free(ifa); 2550 if (rt != NULL) 2551 RTFREE_LOCKED(rt); 2552 return 0; 2553 } 2554 log(LOG_INFO, "IPv6 address: \"%s\" is not on the network\n", 2555 ip6_sprintf(ip6buf, &((const struct sockaddr_in6 *)l3addr)->sin6_addr)); 2556 if (rt != NULL) 2557 RTFREE_LOCKED(rt); 2558 return EINVAL; 2559 } 2560 RTFREE_LOCKED(rt); 2561 return 0; 2562 } 2563 2564 static struct llentry * 2565 in6_lltable_lookup(struct lltable *llt, u_int flags, 2566 const struct sockaddr *l3addr) 2567 { 2568 const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)l3addr; 2569 struct ifnet *ifp = llt->llt_ifp; 2570 struct llentry *lle; 2571 struct llentries *lleh; 2572 u_int hashkey; 2573 2574 IF_AFDATA_LOCK_ASSERT(ifp); 2575 KASSERT(l3addr->sa_family == AF_INET6, 2576 ("sin_family %d", l3addr->sa_family)); 2577 2578 hashkey = sin6->sin6_addr.s6_addr32[3]; 2579 lleh = &llt->lle_head[LLATBL_HASH(hashkey, LLTBL_HASHMASK)]; 2580 LIST_FOREACH(lle, lleh, lle_next) { 2581 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)L3_ADDR(lle); 2582 if (lle->la_flags & LLE_DELETED) 2583 continue; 2584 if (bcmp(&sa6->sin6_addr, &sin6->sin6_addr, 2585 sizeof(struct in6_addr)) == 0) 2586 break; 2587 } 2588 2589 if (lle == NULL) { 2590 if (!(flags & LLE_CREATE)) 2591 return (NULL); 2592 /* 2593 * A route that covers the given address must have 2594 * been installed 1st because we are doing a resolution, 2595 * verify this. 2596 */ 2597 if (!(flags & LLE_IFADDR) && 2598 in6_lltable_rtcheck(ifp, flags, l3addr) != 0) 2599 return NULL; 2600 2601 lle = in6_lltable_new(l3addr, flags); 2602 if (lle == NULL) { 2603 log(LOG_INFO, "lla_lookup: new lle malloc failed\n"); 2604 return NULL; 2605 } 2606 lle->la_flags = flags & ~LLE_CREATE; 2607 if ((flags & (LLE_CREATE | LLE_IFADDR)) == (LLE_CREATE | LLE_IFADDR)) { 2608 bcopy(IF_LLADDR(ifp), &lle->ll_addr, ifp->if_addrlen); 2609 lle->la_flags |= (LLE_VALID | LLE_STATIC); 2610 } 2611 2612 lle->lle_tbl = llt; 2613 lle->lle_head = lleh; 2614 lle->la_flags |= LLE_LINKED; 2615 LIST_INSERT_HEAD(lleh, lle, lle_next); 2616 } else if (flags & LLE_DELETE) { 2617 if (!(lle->la_flags & LLE_IFADDR) || (flags & LLE_IFADDR)) { 2618 LLE_WLOCK(lle); 2619 lle->la_flags |= LLE_DELETED; 2620 #ifdef DIAGNOSTIC 2621 log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle); 2622 #endif 2623 if ((lle->la_flags & 2624 (LLE_STATIC | LLE_IFADDR)) == LLE_STATIC) 2625 llentry_free(lle); 2626 else 2627 LLE_WUNLOCK(lle); 2628 } 2629 lle = (void *)-1; 2630 } 2631 if (LLE_IS_VALID(lle)) { 2632 if (flags & LLE_EXCLUSIVE) 2633 LLE_WLOCK(lle); 2634 else 2635 LLE_RLOCK(lle); 2636 } 2637 return (lle); 2638 } 2639 2640 static int 2641 in6_lltable_dump(struct lltable *llt, struct sysctl_req *wr) 2642 { 2643 struct ifnet *ifp = llt->llt_ifp; 2644 struct llentry *lle; 2645 /* XXX stack use */ 2646 struct { 2647 struct rt_msghdr rtm; 2648 struct sockaddr_in6 sin6; 2649 /* 2650 * ndp.c assumes that sdl is word aligned 2651 */ 2652 #ifdef __LP64__ 2653 uint32_t pad; 2654 #endif 2655 struct sockaddr_dl sdl; 2656 } ndpc; 2657 int i, error; 2658 2659 if (ifp->if_flags & IFF_LOOPBACK) 2660 return 0; 2661 2662 LLTABLE_LOCK_ASSERT(); 2663 2664 error = 0; 2665 for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) { 2666 LIST_FOREACH(lle, &llt->lle_head[i], lle_next) { 2667 struct sockaddr_dl *sdl; 2668 2669 /* skip deleted or invalid entries */ 2670 if ((lle->la_flags & (LLE_DELETED|LLE_VALID)) != LLE_VALID) 2671 continue; 2672 /* Skip if jailed and not a valid IP of the prison. */ 2673 if (prison_if(wr->td->td_ucred, L3_ADDR(lle)) != 0) 2674 continue; 2675 /* 2676 * produce a msg made of: 2677 * struct rt_msghdr; 2678 * struct sockaddr_in6 (IPv6) 2679 * struct sockaddr_dl; 2680 */ 2681 bzero(&ndpc, sizeof(ndpc)); 2682 ndpc.rtm.rtm_msglen = sizeof(ndpc); 2683 ndpc.rtm.rtm_version = RTM_VERSION; 2684 ndpc.rtm.rtm_type = RTM_GET; 2685 ndpc.rtm.rtm_flags = RTF_UP; 2686 ndpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY; 2687 ndpc.sin6.sin6_family = AF_INET6; 2688 ndpc.sin6.sin6_len = sizeof(ndpc.sin6); 2689 bcopy(L3_ADDR(lle), &ndpc.sin6, L3_ADDR_LEN(lle)); 2690 if (V_deembed_scopeid) 2691 sa6_recoverscope(&ndpc.sin6); 2692 2693 /* publish */ 2694 if (lle->la_flags & LLE_PUB) 2695 ndpc.rtm.rtm_flags |= RTF_ANNOUNCE; 2696 2697 sdl = &ndpc.sdl; 2698 sdl->sdl_family = AF_LINK; 2699 sdl->sdl_len = sizeof(*sdl); 2700 sdl->sdl_alen = ifp->if_addrlen; 2701 sdl->sdl_index = ifp->if_index; 2702 sdl->sdl_type = ifp->if_type; 2703 bcopy(&lle->ll_addr, LLADDR(sdl), ifp->if_addrlen); 2704 ndpc.rtm.rtm_rmx.rmx_expire = 2705 lle->la_flags & LLE_STATIC ? 0 : lle->la_expire; 2706 ndpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA); 2707 if (lle->la_flags & LLE_STATIC) 2708 ndpc.rtm.rtm_flags |= RTF_STATIC; 2709 ndpc.rtm.rtm_index = ifp->if_index; 2710 error = SYSCTL_OUT(wr, &ndpc, sizeof(ndpc)); 2711 if (error) 2712 break; 2713 } 2714 } 2715 return error; 2716 } 2717 2718 void * 2719 in6_domifattach(struct ifnet *ifp) 2720 { 2721 struct in6_ifextra *ext; 2722 2723 ext = (struct in6_ifextra *)malloc(sizeof(*ext), M_IFADDR, M_WAITOK); 2724 bzero(ext, sizeof(*ext)); 2725 2726 ext->in6_ifstat = (struct in6_ifstat *)malloc(sizeof(struct in6_ifstat), 2727 M_IFADDR, M_WAITOK); 2728 bzero(ext->in6_ifstat, sizeof(*ext->in6_ifstat)); 2729 2730 ext->icmp6_ifstat = 2731 (struct icmp6_ifstat *)malloc(sizeof(struct icmp6_ifstat), 2732 M_IFADDR, M_WAITOK); 2733 bzero(ext->icmp6_ifstat, sizeof(*ext->icmp6_ifstat)); 2734 2735 ext->nd_ifinfo = nd6_ifattach(ifp); 2736 ext->scope6_id = scope6_ifattach(ifp); 2737 ext->lltable = lltable_init(ifp, AF_INET6); 2738 if (ext->lltable != NULL) { 2739 ext->lltable->llt_prefix_free = in6_lltable_prefix_free; 2740 ext->lltable->llt_lookup = in6_lltable_lookup; 2741 ext->lltable->llt_dump = in6_lltable_dump; 2742 } 2743 2744 ext->mld_ifinfo = mld_domifattach(ifp); 2745 2746 return ext; 2747 } 2748 2749 void 2750 in6_domifdetach(struct ifnet *ifp, void *aux) 2751 { 2752 struct in6_ifextra *ext = (struct in6_ifextra *)aux; 2753 2754 mld_domifdetach(ifp); 2755 scope6_ifdetach(ext->scope6_id); 2756 nd6_ifdetach(ext->nd_ifinfo); 2757 lltable_free(ext->lltable); 2758 free(ext->in6_ifstat, M_IFADDR); 2759 free(ext->icmp6_ifstat, M_IFADDR); 2760 free(ext, M_IFADDR); 2761 } 2762 2763 /* 2764 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be 2765 * v4 mapped addr or v4 compat addr 2766 */ 2767 void 2768 in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6) 2769 { 2770 2771 bzero(sin, sizeof(*sin)); 2772 sin->sin_len = sizeof(struct sockaddr_in); 2773 sin->sin_family = AF_INET; 2774 sin->sin_port = sin6->sin6_port; 2775 sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3]; 2776 } 2777 2778 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */ 2779 void 2780 in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6) 2781 { 2782 bzero(sin6, sizeof(*sin6)); 2783 sin6->sin6_len = sizeof(struct sockaddr_in6); 2784 sin6->sin6_family = AF_INET6; 2785 sin6->sin6_port = sin->sin_port; 2786 sin6->sin6_addr.s6_addr32[0] = 0; 2787 sin6->sin6_addr.s6_addr32[1] = 0; 2788 sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP; 2789 sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr; 2790 } 2791 2792 /* Convert sockaddr_in6 into sockaddr_in. */ 2793 void 2794 in6_sin6_2_sin_in_sock(struct sockaddr *nam) 2795 { 2796 struct sockaddr_in *sin_p; 2797 struct sockaddr_in6 sin6; 2798 2799 /* 2800 * Save original sockaddr_in6 addr and convert it 2801 * to sockaddr_in. 2802 */ 2803 sin6 = *(struct sockaddr_in6 *)nam; 2804 sin_p = (struct sockaddr_in *)nam; 2805 in6_sin6_2_sin(sin_p, &sin6); 2806 } 2807 2808 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */ 2809 void 2810 in6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam) 2811 { 2812 struct sockaddr_in *sin_p; 2813 struct sockaddr_in6 *sin6_p; 2814 2815 sin6_p = malloc(sizeof *sin6_p, M_SONAME, M_WAITOK); 2816 sin_p = (struct sockaddr_in *)*nam; 2817 in6_sin_2_v4mapsin6(sin_p, sin6_p); 2818 free(*nam, M_SONAME); 2819 *nam = (struct sockaddr *)sin6_p; 2820 } 2821