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