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