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