1 /* $FreeBSD: src/sys/netinet6/in6.c,v 1.7.2.9 2002/04/28 05:40:26 suz Exp $ */ 2 /* $KAME: in6.c,v 1.259 2002/01/21 11:37:50 keiichi Exp $ */ 3 4 /* 5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the project nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 /* 34 * Copyright (c) 1982, 1986, 1991, 1993 35 * The Regents of the University of California. All rights reserved. 36 * 37 * Redistribution and use in source and binary forms, with or without 38 * modification, are permitted provided that the following conditions 39 * are met: 40 * 1. Redistributions of source code must retain the above copyright 41 * notice, this list of conditions and the following disclaimer. 42 * 2. Redistributions in binary form must reproduce the above copyright 43 * notice, this list of conditions and the following disclaimer in the 44 * documentation and/or other materials provided with the distribution. 45 * 3. Neither the name of the University nor the names of its contributors 46 * may be used to endorse or promote products derived from this software 47 * without specific prior written permission. 48 * 49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 59 * SUCH DAMAGE. 60 * 61 * @(#)in.c 8.2 (Berkeley) 11/15/93 62 */ 63 64 #include "opt_inet.h" 65 #include "opt_inet6.h" 66 67 #include <sys/param.h> 68 #include <sys/errno.h> 69 #include <sys/malloc.h> 70 #include <sys/socket.h> 71 #include <sys/socketvar.h> 72 #include <sys/sockio.h> 73 #include <sys/systm.h> 74 #include <sys/proc.h> 75 #include <sys/priv.h> 76 #include <sys/time.h> 77 #include <sys/kernel.h> 78 #include <sys/syslog.h> 79 #include <sys/jail.h> 80 81 #include <sys/thread2.h> 82 #include <sys/msgport2.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/netmsg2.h> 89 #include <net/netisr2.h> 90 91 #include <netinet/in.h> 92 #include <netinet/in_var.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/in6_var.h> 107 108 #include <net/net_osdep.h> 109 110 /* 111 * Definitions of some costant IP6 addresses. 112 */ 113 const struct in6_addr kin6addr_any = IN6ADDR_ANY_INIT; 114 const struct in6_addr kin6addr_loopback = IN6ADDR_LOOPBACK_INIT; 115 const struct in6_addr kin6addr_nodelocal_allnodes = 116 IN6ADDR_NODELOCAL_ALLNODES_INIT; 117 const struct in6_addr kin6addr_linklocal_allnodes = 118 IN6ADDR_LINKLOCAL_ALLNODES_INIT; 119 const struct in6_addr kin6addr_linklocal_allrouters = 120 IN6ADDR_LINKLOCAL_ALLROUTERS_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 = {sizeof(sa6_any), AF_INET6, 129 0, 0, IN6ADDR_ANY_INIT, 0}; 130 131 static int in6_lifaddr_ioctl (u_long, caddr_t, struct ifnet *, 132 struct thread *); 133 static int in6_ifinit (struct ifnet *, struct in6_ifaddr *, 134 struct sockaddr_in6 *, int); 135 static void in6_unlink_ifa (struct in6_ifaddr *, struct ifnet *); 136 static void in6_ifloop_request_callback(int, int, struct rt_addrinfo *, struct rtentry *, void *); 137 138 static void in6_control_internal_dispatch(netmsg_t); 139 static int in6_control_internal(u_long, caddr_t, struct ifnet *, 140 struct thread *); 141 142 struct in6_multihead in6_multihead; /* XXX BSS initialization */ 143 144 /* 145 * Subroutine for in6_ifaddloop() and in6_ifremloop(). 146 * This routine does actual work. 147 */ 148 static void 149 in6_ifloop_request(int cmd, struct ifaddr *ifa, 150 void (*callback)(int, int, struct rt_addrinfo *, struct rtentry *, void *)) 151 { 152 struct sockaddr_in6 all1_sa; 153 struct rt_addrinfo rtinfo; 154 int error; 155 156 bzero(&all1_sa, sizeof(all1_sa)); 157 all1_sa.sin6_family = AF_INET6; 158 all1_sa.sin6_len = sizeof(struct sockaddr_in6); 159 all1_sa.sin6_addr = in6mask128; 160 161 /* 162 * We specify the address itself as the gateway, and set the 163 * RTF_LLINFO flag, so that the corresponding host route would have 164 * the flag, and thus applications that assume traditional behavior 165 * would be happy. Note that we assume the caller of the function 166 * (probably implicitly) set nd6_rtrequest() to ifa->ifa_rtrequest, 167 * which changes the outgoing interface to the loopback interface. 168 */ 169 bzero(&rtinfo, sizeof(struct rt_addrinfo)); 170 rtinfo.rti_info[RTAX_DST] = ifa->ifa_addr; 171 rtinfo.rti_info[RTAX_GATEWAY] = ifa->ifa_addr; 172 rtinfo.rti_info[RTAX_NETMASK] = (struct sockaddr *)&all1_sa; 173 rtinfo.rti_flags = RTF_UP|RTF_HOST|RTF_LLINFO; 174 175 error = rtrequest1_global(cmd, &rtinfo, callback, ifa, RTREQ_PRIO_NORM); 176 if (error != 0) { 177 log(LOG_ERR, "in6_ifloop_request: " 178 "%s operation failed for %s (errno=%d)\n", 179 cmd == RTM_ADD ? "ADD" : cmd == RTM_DELETE ? "DELETE" : "GET", 180 ip6_sprintf(&((struct in6_ifaddr *)ifa)->ia_addr.sin6_addr), 181 error); 182 } 183 } 184 185 static void 186 in6_ifloop_request_callback(int cmd, int error, struct rt_addrinfo *rtinfo, 187 struct rtentry *rt, void *arg) 188 { 189 struct ifaddr *ifa = arg; 190 191 if (error) 192 goto done; 193 194 /* 195 * Make sure rt_ifa be equal to IFA, the second argument of the 196 * function. 197 * We need this because when we refer to rt_ifa->ia6_flags in 198 * ip6_input, we assume that the rt_ifa points to the address instead 199 * of the loopback address. 200 */ 201 if (cmd == RTM_ADD && rt && ifa != rt->rt_ifa) { 202 ++rt->rt_refcnt; 203 IFAFREE(rt->rt_ifa); 204 IFAREF(ifa); 205 rt->rt_ifa = ifa; 206 --rt->rt_refcnt; 207 } 208 209 /* 210 * Report the addition/removal of the address to the routing socket, 211 * unless the address is marked as tentative, where it will be reported 212 * once DAD completes. 213 * XXX: since we called rtinit for a p2p interface with a destination, 214 * we end up reporting twice in such a case. Should we rather 215 * omit the second report? 216 */ 217 if (rt) { 218 if (mycpuid == 0) { 219 struct in6_ifaddr *ia6 = (struct in6_ifaddr *)ifa; 220 221 if (cmd != RTM_ADD || 222 !(ia6->ia6_flags & IN6_IFF_TENTATIVE)) 223 rt_newaddrmsg(cmd, ifa, error, rt); 224 } 225 if (cmd == RTM_DELETE) { 226 if (rt->rt_refcnt == 0) { 227 ++rt->rt_refcnt; 228 rtfree(rt); 229 } 230 } 231 } 232 done: 233 /* no way to return any new error */ 234 ; 235 } 236 237 static void 238 in6_newaddrmsg_callback(int cmd, int error, struct rt_addrinfo *rtinfo, 239 struct rtentry *rt, void *arg) 240 { 241 struct ifaddr *ifa = arg; 242 243 if (error == 0 && rt != NULL && mycpuid == 0) 244 rt_newaddrmsg(RTM_ADD, ifa, error, rt); 245 } 246 247 void 248 in6_newaddrmsg(struct ifaddr *ifa) 249 { 250 in6_ifloop_request(RTM_GET, ifa, in6_newaddrmsg_callback); 251 } 252 253 /* 254 * Add ownaddr as loopback rtentry. We previously add the route only if 255 * necessary (ex. on a p2p link). However, since we now manage addresses 256 * separately from prefixes, we should always add the route. We can't 257 * rely on the cloning mechanism from the corresponding interface route 258 * any more. 259 */ 260 void 261 in6_ifaddloop(struct ifaddr *ifa) 262 { 263 struct rtentry *rt; 264 265 /* If there is no loopback entry, allocate one. */ 266 rt = rtpurelookup(ifa->ifa_addr); 267 if (rt == NULL || !(rt->rt_flags & RTF_HOST) || 268 !(rt->rt_ifp->if_flags & IFF_LOOPBACK)) 269 in6_ifloop_request(RTM_ADD, ifa, in6_ifloop_request_callback); 270 if (rt != NULL) 271 rt->rt_refcnt--; 272 } 273 274 /* 275 * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(), 276 * if it exists. 277 */ 278 void 279 in6_ifremloop(struct ifaddr *ifa) 280 { 281 struct in6_ifaddr *ia; 282 struct rtentry *rt; 283 int ia_count = 0; 284 285 /* 286 * Some of BSD variants do not remove cloned routes 287 * from an interface direct route, when removing the direct route 288 * (see comments in net/net_osdep.h). Even for variants that do remove 289 * cloned routes, they could fail to remove the cloned routes when 290 * we handle multple addresses that share a common prefix. 291 * So, we should remove the route corresponding to the deleted address 292 * regardless of the result of in6_is_ifloop_auto(). 293 */ 294 295 /* 296 * Delete the entry only if exact one ifa exists. More than one ifa 297 * can exist if we assign a same single address to multiple 298 * (probably p2p) interfaces. 299 * XXX: we should avoid such a configuration in IPv6... 300 */ 301 for (ia = in6_ifaddr; ia; ia = ia->ia_next) { 302 if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &ia->ia_addr.sin6_addr)) { 303 ia_count++; 304 if (ia_count > 1) 305 break; 306 } 307 } 308 309 if (ia_count == 1) { 310 /* 311 * Before deleting, check if a corresponding loopbacked host 312 * route surely exists. With this check, we can avoid to 313 * delete an interface direct route whose destination is same 314 * as the address being removed. This can happen when remofing 315 * a subnet-router anycast address on an interface attahced 316 * to a shared medium. 317 */ 318 rt = rtpurelookup(ifa->ifa_addr); 319 if (rt != NULL && (rt->rt_flags & RTF_HOST) && 320 (rt->rt_ifp->if_flags & IFF_LOOPBACK)) { 321 rt->rt_refcnt--; 322 in6_ifloop_request(RTM_DELETE, ifa, 323 in6_ifloop_request_callback); 324 } 325 } 326 } 327 328 int 329 in6_mask2len(const struct in6_addr *mask, const u_char *lim0) 330 { 331 int x = 0, y; 332 const u_char *lim = lim0, *p; 333 334 if (lim0 == NULL || 335 lim0 - (const u_char *)mask > sizeof(*mask)) { 336 /* Ignore the scope_id part */ 337 lim = (const u_char *)mask + sizeof(*mask); 338 } 339 for (p = (const u_char *)mask; p < lim; x++, p++) { 340 if (*p != 0xff) 341 break; 342 } 343 y = 0; 344 if (p < lim) { 345 for (y = 0; y < 8; y++) { 346 if ((*p & (0x80 >> y)) == 0) 347 break; 348 } 349 } 350 351 /* 352 * When the limit pointer is given, do a stricter check on the 353 * remaining bits. 354 */ 355 if (p < lim) { 356 if (y != 0 && (*p & (0x00ff >> y)) != 0) 357 return (-1); 358 for (p = p + 1; p < lim; p++) 359 if (*p != 0) 360 return (-1); 361 } 362 363 return x * 8 + y; 364 } 365 366 #define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa)) 367 #define ia62ifa(ia6) (&((ia6)->ia_ifa)) 368 369 void 370 in6_control_dispatch(netmsg_t msg) 371 { 372 int error; 373 374 error = in6_control(msg->control.nm_cmd, 375 msg->control.nm_data, 376 msg->control.nm_ifp, 377 msg->control.nm_td); 378 lwkt_replymsg(&msg->control.base.lmsg, error); 379 } 380 381 int 382 in6_control(u_long cmd, caddr_t data, struct ifnet *ifp, struct thread *td) 383 { 384 struct netmsg_pru_control msg; 385 386 switch (cmd) { 387 case SIOCSIFPREFIX_IN6: 388 case SIOCDIFPREFIX_IN6: 389 case SIOCAIFPREFIX_IN6: 390 case SIOCCIFPREFIX_IN6: 391 case SIOCSGIFPREFIX_IN6: 392 case SIOCGIFPREFIX_IN6: 393 log(LOG_NOTICE, "prefix ioctls are now invalidated. " 394 "please use ifconfig.\n"); 395 return (EOPNOTSUPP); 396 397 case SIOCSIFADDR_IN6: 398 case SIOCSIFDSTADDR_IN6: 399 case SIOCSIFNETMASK_IN6: 400 /* 401 * Since IPv6 allows a node to assign multiple addresses 402 * on a single interface, SIOCSIFxxx ioctls are not suitable 403 * and should be unused. 404 */ 405 /* We decided to obsolete this command (20000704) */ 406 return (EINVAL); 407 408 case SIOCSIFADDR: 409 case SIOCSIFDSTADDR: 410 case SIOCSIFBRDADDR: 411 case SIOCSIFNETMASK: 412 /* 413 * Do not pass those ioctl to driver handler since they are not 414 * properly setup. Instead just error out. 415 */ 416 return (EOPNOTSUPP); 417 418 /* mroute */ 419 case SIOCGETSGCNT_IN6: 420 case SIOCGETMIFCNT_IN6: 421 /* srcsel policy */ 422 case SIOCAADDRCTL_POLICY: 423 case SIOCDADDRCTL_POLICY: 424 /* nd6 */ 425 case SIOCSNDFLUSH_IN6: 426 case SIOCSPFXFLUSH_IN6: 427 case SIOCSRTRFLUSH_IN6: 428 case SIOCSDEFIFACE_IN6: 429 case SIOCSIFINFO_FLAGS: 430 case OSIOCGIFINFO_IN6: 431 case SIOCGIFINFO_IN6: 432 case SIOCGDRLST_IN6: 433 case SIOCGPRLST_IN6: 434 case SIOCGNBRINFO_IN6: 435 case SIOCGDEFIFACE_IN6: 436 /* scope6 */ 437 case SIOCSSCOPE6: 438 case SIOCGSCOPE6: 439 case SIOCGSCOPE6DEF: 440 /* change address */ 441 case SIOCALIFADDR: 442 case SIOCDLIFADDR: 443 case SIOCSIFALIFETIME_IN6: 444 case SIOCAIFADDR_IN6: 445 case SIOCDIFADDR_IN6: 446 /* 447 * Dispatch these SIOCs to netisr0. 448 */ 449 netmsg_init(&msg.base, NULL, &curthread->td_msgport, 0, 450 in6_control_internal_dispatch); 451 msg.nm_cmd = cmd; 452 msg.nm_data = data; 453 msg.nm_ifp = ifp; 454 msg.nm_td = td; 455 lwkt_domsg(netisr_cpuport(0), &msg.base.lmsg, 0); 456 return msg.base.lmsg.ms_error; 457 458 default: 459 return in6_control_internal(cmd, data, ifp, td); 460 } 461 } 462 463 static void 464 in6_control_internal_dispatch(netmsg_t msg) 465 { 466 int error; 467 468 error = in6_control_internal(msg->control.nm_cmd, msg->control.nm_data, 469 msg->control.nm_ifp, msg->control.nm_td); 470 lwkt_replymsg(&msg->lmsg, error); 471 } 472 473 static int 474 in6_control_internal(u_long cmd, caddr_t data, struct ifnet *ifp, 475 struct thread *td) 476 { 477 struct in6_ifreq *ifr = (struct in6_ifreq *)data; 478 struct in6_ifaddr *ia = NULL; 479 struct in6_aliasreq *ifra = (struct in6_aliasreq *)data; 480 struct in6_ifextra *xtra; 481 boolean_t privileged; 482 int error; 483 484 privileged = FALSE; 485 if (priv_check(td, PRIV_ROOT) == 0) 486 privileged = TRUE; 487 488 switch (cmd) { 489 case SIOCALIFADDR: 490 case SIOCDLIFADDR: 491 if (!privileged) 492 return (EPERM); 493 /* FALLTHROUGH */ 494 case SIOCGLIFADDR: 495 if (ifp == NULL) 496 return (EOPNOTSUPP); 497 return in6_lifaddr_ioctl(cmd, data, ifp, td); 498 } 499 500 switch (cmd) { 501 case SIOCGETSGCNT_IN6: 502 case SIOCGETMIFCNT_IN6: 503 return (mrt6_ioctl(cmd, data)); 504 } 505 506 switch(cmd) { 507 case SIOCAADDRCTL_POLICY: 508 case SIOCDADDRCTL_POLICY: 509 if (!privileged) 510 return (EPERM); 511 return (in6_src_ioctl(cmd, data)); 512 } 513 514 if (ifp == NULL) 515 return (EOPNOTSUPP); 516 517 switch (cmd) { 518 case SIOCSNDFLUSH_IN6: 519 case SIOCSPFXFLUSH_IN6: 520 case SIOCSRTRFLUSH_IN6: 521 case SIOCSDEFIFACE_IN6: 522 case SIOCSIFINFO_FLAGS: 523 if (!privileged) 524 return (EPERM); 525 /* FALLTHROUGH */ 526 case OSIOCGIFINFO_IN6: 527 case SIOCGIFINFO_IN6: 528 case SIOCGDRLST_IN6: 529 case SIOCGPRLST_IN6: 530 case SIOCGNBRINFO_IN6: 531 case SIOCGDEFIFACE_IN6: 532 return (nd6_ioctl(cmd, data, ifp)); 533 } 534 535 switch (cmd) { 536 case SIOCSSCOPE6: 537 if (!privileged) 538 return (EPERM); 539 return (scope6_set(ifp, 540 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id)); 541 542 case SIOCGSCOPE6: 543 return (scope6_get(ifp, 544 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id)); 545 546 case SIOCGSCOPE6DEF: 547 return (scope6_get_default((struct scope6_id *) 548 ifr->ifr_ifru.ifru_scope_id)); 549 } 550 551 /* 552 * Find address for this interface, if it exists. 553 */ 554 if (ifra->ifra_addr.sin6_family == AF_INET6) { /* XXX */ 555 struct sockaddr_in6 *sa6 = 556 (struct sockaddr_in6 *)&ifra->ifra_addr; 557 558 if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr)) { 559 if (sa6->sin6_addr.s6_addr16[1] == 0) { 560 /* Link ID is not embedded by the user */ 561 sa6->sin6_addr.s6_addr16[1] = 562 htons(ifp->if_index); 563 } else if (sa6->sin6_addr.s6_addr16[1] != 564 htons(ifp->if_index)) { 565 /* Link ID contradicts */ 566 return (EINVAL); 567 } 568 if (sa6->sin6_scope_id) { 569 if (sa6->sin6_scope_id != 570 (u_int32_t)ifp->if_index) 571 return (EINVAL); 572 sa6->sin6_scope_id = 0; /* XXX: good way? */ 573 } 574 } 575 ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr); 576 } 577 578 switch (cmd) { 579 case SIOCDIFADDR_IN6: 580 /* 581 * For IPv4, we look for existing in_ifaddr here to allow 582 * "ifconfig if0 delete" to remove first IPv4 address on the 583 * interface. For IPv6, as the spec allow multiple interface 584 * address from the day one, we consider "remove the first one" 585 * semantics to be not preferable. 586 */ 587 if (ia == NULL) 588 return (EADDRNOTAVAIL); 589 /* FALLTHROUGH */ 590 case SIOCAIFADDR_IN6: 591 /* 592 * We always require users to specify a valid IPv6 address for 593 * the corresponding operation. 594 */ 595 if (ifra->ifra_addr.sin6_family != AF_INET6 || 596 ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6)) 597 return (EAFNOSUPPORT); 598 if (!privileged) 599 return (EPERM); 600 break; 601 602 case SIOCGIFADDR_IN6: 603 /* This interface is basically deprecated. Use SIOCGIFCONF. */ 604 /* FALLTHROUGH */ 605 case SIOCGIFAFLAG_IN6: 606 case SIOCGIFNETMASK_IN6: 607 case SIOCGIFDSTADDR_IN6: 608 case SIOCGIFALIFETIME_IN6: 609 /* Must think again about its semantics */ 610 if (ia == NULL) 611 return (EADDRNOTAVAIL); 612 break; 613 614 case SIOCSIFALIFETIME_IN6: 615 { 616 const struct in6_addrlifetime *lt; 617 618 if (!privileged) 619 return (EPERM); 620 if (ia == NULL) 621 return (EADDRNOTAVAIL); 622 /* Sanity for overflow - beware unsigned */ 623 lt = &ifr->ifr_ifru.ifru_lifetime; 624 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME && 625 lt->ia6t_vltime + time_uptime < time_uptime) 626 return EINVAL; 627 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME && 628 lt->ia6t_pltime + time_uptime < time_uptime) 629 return EINVAL; 630 break; 631 } 632 } 633 634 switch (cmd) { 635 case SIOCGIFADDR_IN6: 636 ifr->ifr_addr = ia->ia_addr; 637 break; 638 639 case SIOCGIFDSTADDR_IN6: 640 if (!(ifp->if_flags & IFF_POINTOPOINT)) 641 return (EINVAL); 642 /* 643 * XXX: Should we check if ifa_dstaddr is NULL and return 644 * an error? 645 */ 646 ifr->ifr_dstaddr = ia->ia_dstaddr; 647 break; 648 649 case SIOCGIFNETMASK_IN6: 650 ifr->ifr_addr = ia->ia_prefixmask; 651 break; 652 653 case SIOCGIFAFLAG_IN6: 654 ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags; 655 break; 656 657 case SIOCGIFSTAT_IN6: 658 if ((xtra = ifp->if_afdata[AF_INET6]) == NULL) 659 return EINVAL; 660 bzero(&ifr->ifr_ifru.ifru_stat, 661 sizeof(ifr->ifr_ifru.ifru_stat)); 662 ifr->ifr_ifru.ifru_stat = *xtra->in6_ifstat; 663 break; 664 665 case SIOCGIFSTAT_ICMP6: 666 if ((xtra = ifp->if_afdata[AF_INET6]) == NULL) 667 return EINVAL; 668 bzero(&ifr->ifr_ifru.ifru_stat, 669 sizeof(ifr->ifr_ifru.ifru_icmp6stat)); 670 ifr->ifr_ifru.ifru_icmp6stat = *xtra->icmp6_ifstat; 671 break; 672 673 case SIOCGIFALIFETIME_IN6: 674 ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime; 675 break; 676 677 case SIOCSIFALIFETIME_IN6: 678 ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime; 679 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) { 680 ia->ia6_lifetime.ia6t_expire = 681 time_uptime + ia->ia6_lifetime.ia6t_vltime; 682 } else { 683 ia->ia6_lifetime.ia6t_expire = 0; 684 } 685 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) { 686 ia->ia6_lifetime.ia6t_preferred = 687 time_uptime + ia->ia6_lifetime.ia6t_pltime; 688 } else { 689 ia->ia6_lifetime.ia6t_preferred = 0; 690 } 691 break; 692 693 case SIOCAIFADDR_IN6: 694 { 695 int i, error = 0, iaIsNew; 696 struct nd_prefix pr0, *pr; 697 698 if (ia != NULL) 699 iaIsNew = 0; 700 else 701 iaIsNew = 1; 702 703 /* 704 * First, make or update the interface address structure, 705 * and link it to the list. 706 */ 707 if ((error = in6_update_ifa(ifp, ifra, ia)) != 0) 708 return (error); 709 710 /* 711 * Then, make the prefix on-link on the interface. 712 * XXX: We'd rather create the prefix before the address, but 713 * we need at least one address to install the corresponding 714 * interface route, so we configure the address first. 715 */ 716 717 /* 718 * Convert mask to prefix length (prefixmask has already 719 * been validated in in6_update_ifa(). 720 */ 721 bzero(&pr0, sizeof(pr0)); 722 pr0.ndpr_ifp = ifp; 723 pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr, 724 NULL); 725 if (pr0.ndpr_plen == 128) 726 break; /* no need to install a host route. */ 727 pr0.ndpr_prefix = ifra->ifra_addr; 728 pr0.ndpr_mask = ifra->ifra_prefixmask.sin6_addr; 729 /* Apply the mask for safety. */ 730 for (i = 0; i < 4; i++) { 731 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &= 732 ifra->ifra_prefixmask.sin6_addr.s6_addr32[i]; 733 } 734 /* 735 * XXX: Since we don't have an API to set prefix (not address) 736 * lifetimes, we just use the same lifetimes as addresses. 737 * The (temporarily) installed lifetimes can be overridden by 738 * later advertised RAs (when accept_rtadv is non 0), which is 739 * an intended behavior. 740 */ 741 pr0.ndpr_raf_onlink = 1; /* should be configurable? */ 742 pr0.ndpr_raf_auto = 743 ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0); 744 pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime; 745 pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime; 746 747 /* Add the prefix if there's one. */ 748 if ((pr = nd6_prefix_lookup(&pr0)) == NULL) { 749 /* 750 * nd6_prelist_add will install the corresponding 751 * interface route. 752 */ 753 if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0) 754 return (error); 755 if (pr == NULL) { 756 log(LOG_ERR, "nd6_prelist_add succeeded but " 757 "no prefix\n"); 758 return (EINVAL); /* XXX panic here? */ 759 } 760 } 761 762 ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr); 763 if (ia == NULL) { 764 /* XXX: This should not happen! */ 765 log(LOG_ERR, "in6_control: addition succeeded, but" 766 " no ifaddr\n"); 767 } else { 768 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) && 769 ia->ia6_ndpr == NULL) { 770 /* 771 * New autoconf address 772 */ 773 ia->ia6_ndpr = pr; 774 pr->ndpr_refcnt++; 775 776 /* 777 * If this is the first autoconf address from 778 * the prefix, create a temporary address 779 * as well (when specified). 780 */ 781 if (ip6_use_tempaddr && pr->ndpr_refcnt == 1) { 782 int e; 783 784 if ((e = in6_tmpifadd(ia, 1)) != 0) { 785 log(LOG_NOTICE, "in6_control: " 786 "failed to create a " 787 "temporary address, " 788 "errno=%d\n", e); 789 } 790 } 791 } 792 793 /* 794 * This might affect the status of autoconfigured 795 * addresses, that is, this address might make 796 * other addresses detached. 797 */ 798 pfxlist_onlink_check(); 799 } 800 if (error == 0 && ia) { 801 EVENTHANDLER_INVOKE(ifaddr_event, ifp, 802 iaIsNew ? IFADDR_EVENT_ADD : IFADDR_EVENT_CHANGE, 803 &ia->ia_ifa); 804 } 805 break; 806 } 807 808 case SIOCDIFADDR_IN6: 809 { 810 int i = 0; 811 struct nd_prefix pr0, *pr; 812 813 /* 814 * If the address being deleted is the only one that owns 815 * the corresponding prefix, expire the prefix as well. 816 * XXX: Theoretically, we don't have to warry about such 817 * relationship, since we separate the address management 818 * and the prefix management. We do this, however, to provide 819 * as much backward compatibility as possible in terms of 820 * the ioctl operation. 821 */ 822 bzero(&pr0, sizeof(pr0)); 823 pr0.ndpr_ifp = ifp; 824 pr0.ndpr_plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, 825 NULL); 826 if (pr0.ndpr_plen == 128) 827 goto purgeaddr; 828 pr0.ndpr_prefix = ia->ia_addr; 829 pr0.ndpr_mask = ia->ia_prefixmask.sin6_addr; 830 for (i = 0; i < 4; i++) { 831 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &= 832 ia->ia_prefixmask.sin6_addr.s6_addr32[i]; 833 } 834 /* 835 * The logic of the following condition is a bit complicated. 836 * We expire the prefix when 837 * 1. The address obeys autoconfiguration and it is the 838 * only owner of the associated prefix, or 839 * 2. The address does not obey autoconf and there is no 840 * other owner of the prefix. 841 */ 842 if ((pr = nd6_prefix_lookup(&pr0)) != NULL && 843 (((ia->ia6_flags & IN6_IFF_AUTOCONF) && 844 pr->ndpr_refcnt == 1) || 845 (!(ia->ia6_flags & IN6_IFF_AUTOCONF) && 846 pr->ndpr_refcnt == 0))) 847 pr->ndpr_expire = 1; /* XXX: just for expiration */ 848 849 purgeaddr: 850 EVENTHANDLER_INVOKE(ifaddr_event, ifp, IFADDR_EVENT_DELETE, 851 &ia->ia_ifa); 852 in6_purgeaddr(&ia->ia_ifa); 853 break; 854 } 855 856 default: 857 if (ifp->if_ioctl == NULL) 858 return (EOPNOTSUPP); 859 ifnet_serialize_all(ifp); 860 error = ifp->if_ioctl(ifp, cmd, data, td->td_proc->p_ucred); 861 ifnet_deserialize_all(ifp); 862 return (error); 863 } 864 865 return (0); 866 } 867 868 /* 869 * Update parameters of an IPv6 interface address. 870 * If necessary, a new entry is created and linked into address chains. 871 * This function is separated from in6_control(). 872 * XXX: should this be performed under splnet()? 873 */ 874 int 875 in6_update_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra, 876 struct in6_ifaddr *ia) 877 { 878 int error = 0, hostIsNew = 0, was_tentative, plen = -1; 879 struct in6_ifaddr *oia; 880 struct sockaddr_in6 dst6; 881 struct in6_addrlifetime *lt; 882 883 /* Validate parameters */ 884 if (ifp == NULL || ifra == NULL) /* this maybe redundant */ 885 return (EINVAL); 886 887 /* 888 * The destination address for a p2p link must have a family 889 * of AF_UNSPEC or AF_INET6. 890 */ 891 if ((ifp->if_flags & IFF_POINTOPOINT) && 892 ifra->ifra_dstaddr.sin6_family != AF_INET6 && 893 ifra->ifra_dstaddr.sin6_family != AF_UNSPEC) 894 return (EAFNOSUPPORT); 895 /* 896 * validate ifra_prefixmask. don't check sin6_family, netmask 897 * does not carry fields other than sin6_len. 898 */ 899 if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6)) 900 return (EINVAL); 901 /* 902 * Because the IPv6 address architecture is classless, we require 903 * users to specify a (non 0) prefix length (mask) for a new address. 904 * We also require the prefix (when specified) mask is valid, and thus 905 * reject a non-consecutive mask. 906 */ 907 if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0) 908 return (EINVAL); 909 if (ifra->ifra_prefixmask.sin6_len != 0) { 910 plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr, 911 (u_char *)&ifra->ifra_prefixmask + 912 ifra->ifra_prefixmask.sin6_len); 913 if (plen <= 0) 914 return (EINVAL); 915 } 916 else { 917 /* 918 * In this case, ia must not be NULL. We just use its prefix 919 * length. 920 */ 921 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); 922 } 923 /* 924 * If the destination address on a p2p interface is specified, 925 * and the address is a scoped one, validate/set the scope 926 * zone identifier. 927 */ 928 dst6 = ifra->ifra_dstaddr; 929 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) && 930 (dst6.sin6_family == AF_INET6)) { 931 int scopeid; 932 933 if ((error = in6_recoverscope(&dst6, 934 &ifra->ifra_dstaddr.sin6_addr, 935 ifp)) != 0) 936 return (error); 937 if (in6_addr2zoneid(ifp, &dst6.sin6_addr, &scopeid)) 938 return (EINVAL); 939 if (dst6.sin6_scope_id == 0) /* user omit to specify the ID. */ 940 dst6.sin6_scope_id = scopeid; 941 else if (dst6.sin6_scope_id != scopeid) 942 return (EINVAL); /* scope ID mismatch. */ 943 if ((error = in6_embedscope(&dst6.sin6_addr, &dst6, NULL, NULL)) 944 != 0) 945 return (error); 946 dst6.sin6_scope_id = 0; /* XXX */ 947 } 948 /* 949 * The destination address can be specified only for a p2p or a 950 * loopback interface. If specified, the corresponding prefix length 951 * must be 128. 952 */ 953 if (ifra->ifra_dstaddr.sin6_family == AF_INET6) { 954 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0) { 955 /* XXX: noisy message */ 956 log(LOG_INFO, "in6_update_ifa: a destination can be " 957 "specified for a p2p or a loopback IF only\n"); 958 return (EINVAL); 959 } 960 if (plen != 128) { 961 /* 962 * The following message seems noisy, but we dare to 963 * add it for diagnosis. 964 */ 965 log(LOG_INFO, "in6_update_ifa: prefixlen must be 128 " 966 "when dstaddr is specified\n"); 967 return (EINVAL); 968 } 969 } 970 /* lifetime consistency check */ 971 lt = &ifra->ifra_lifetime; 972 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME 973 && lt->ia6t_vltime + time_uptime < time_uptime) { 974 return EINVAL; 975 } 976 if (lt->ia6t_vltime == 0) { 977 /* 978 * the following log might be noisy, but this is a typical 979 * configuration mistake or a tool's bug. 980 */ 981 log(LOG_INFO, 982 "in6_update_ifa: valid lifetime is 0 for %s\n", 983 ip6_sprintf(&ifra->ifra_addr.sin6_addr)); 984 } 985 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME 986 && lt->ia6t_pltime + time_uptime < time_uptime) { 987 return EINVAL; 988 } 989 990 /* 991 * If this is a new address, allocate a new ifaddr and link it 992 * into chains. 993 */ 994 if (ia == NULL) { 995 hostIsNew = 1; 996 ia = ifa_create(sizeof(*ia)); 997 998 /* Initialize the address and masks */ 999 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr; 1000 ia->ia_addr.sin6_family = AF_INET6; 1001 ia->ia_addr.sin6_len = sizeof(ia->ia_addr); 1002 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) { 1003 /* 1004 * XXX: some functions expect that ifa_dstaddr is not 1005 * NULL for p2p interfaces. 1006 */ 1007 ia->ia_ifa.ifa_dstaddr 1008 = (struct sockaddr *)&ia->ia_dstaddr; 1009 } else { 1010 ia->ia_ifa.ifa_dstaddr = NULL; 1011 } 1012 ia->ia_ifa.ifa_netmask 1013 = (struct sockaddr *)&ia->ia_prefixmask; 1014 1015 ia->ia_ifp = ifp; 1016 if ((oia = in6_ifaddr) != NULL) { 1017 for ( ; oia->ia_next; oia = oia->ia_next) 1018 continue; 1019 oia->ia_next = ia; 1020 } else 1021 in6_ifaddr = ia; 1022 1023 ifa_iflink(&ia->ia_ifa, ifp, 1); 1024 } 1025 1026 /* set prefix mask */ 1027 if (ifra->ifra_prefixmask.sin6_len) { 1028 /* 1029 * We prohibit changing the prefix length of an existing 1030 * address, because 1031 * + such an operation should be rare in IPv6, and 1032 * + the operation would confuse prefix management. 1033 */ 1034 if (ia->ia_prefixmask.sin6_len && 1035 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) { 1036 log(LOG_INFO, "in6_update_ifa: the prefix length of an" 1037 " existing (%s) address should not be changed\n", 1038 ip6_sprintf(&ia->ia_addr.sin6_addr)); 1039 error = EINVAL; 1040 goto unlink; 1041 } 1042 ia->ia_prefixmask = ifra->ifra_prefixmask; 1043 } 1044 1045 /* 1046 * If a new destination address is specified, scrub the old one and 1047 * install the new destination. Note that the interface must be 1048 * p2p or loopback (see the check above.) 1049 */ 1050 if (dst6.sin6_family == AF_INET6 && 1051 !IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr, 1052 &ia->ia_dstaddr.sin6_addr)) { 1053 int e; 1054 1055 if ((ia->ia_flags & IFA_ROUTE) && 1056 (e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST)) 1057 != 0) { 1058 log(LOG_ERR, "in6_update_ifa: failed to remove " 1059 "a route to the old destination: %s\n", 1060 ip6_sprintf(&ia->ia_addr.sin6_addr)); 1061 /* proceed anyway... */ 1062 } 1063 else 1064 ia->ia_flags &= ~IFA_ROUTE; 1065 ia->ia_dstaddr = dst6; 1066 } 1067 1068 was_tentative = ia->ia6_flags & (IN6_IFF_TENTATIVE|IN6_IFF_DUPLICATED); 1069 ia->ia6_flags = ifra->ifra_flags; 1070 ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /*safety*/ 1071 ia->ia6_flags &= ~IN6_IFF_NODAD; /* Mobile IPv6 */ 1072 if ((hostIsNew || was_tentative) && 1073 in6if_do_dad(ifp) && 1074 !(ifra->ifra_flags & IN6_IFF_NODAD)) 1075 ia->ia6_flags |= IN6_IFF_TENTATIVE; 1076 1077 ia->ia6_lifetime = ifra->ifra_lifetime; 1078 /* for sanity */ 1079 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) { 1080 ia->ia6_lifetime.ia6t_expire = 1081 time_uptime + ia->ia6_lifetime.ia6t_vltime; 1082 } else 1083 ia->ia6_lifetime.ia6t_expire = 0; 1084 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) { 1085 ia->ia6_lifetime.ia6t_preferred = 1086 time_uptime + ia->ia6_lifetime.ia6t_pltime; 1087 } else 1088 ia->ia6_lifetime.ia6t_preferred = 0; 1089 1090 /* reset the interface and routing table appropriately. */ 1091 if ((error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew)) != 0) 1092 goto unlink; 1093 1094 /* 1095 * Beyond this point, we should call in6_purgeaddr upon an error, 1096 * not just go to unlink. 1097 */ 1098 1099 if (ifp->if_flags & IFF_MULTICAST) { 1100 struct sockaddr_in6 mltaddr, mltmask; 1101 struct in6_multi *in6m; 1102 1103 if (hostIsNew) { 1104 /* 1105 * join solicited multicast addr for new host id 1106 */ 1107 struct in6_addr llsol; 1108 bzero(&llsol, sizeof(struct in6_addr)); 1109 llsol.s6_addr16[0] = htons(0xff02); 1110 llsol.s6_addr16[1] = htons(ifp->if_index); 1111 llsol.s6_addr32[1] = 0; 1112 llsol.s6_addr32[2] = htonl(1); 1113 llsol.s6_addr32[3] = 1114 ifra->ifra_addr.sin6_addr.s6_addr32[3]; 1115 llsol.s6_addr8[12] = 0xff; 1116 in6_addmulti(&llsol, ifp, &error); 1117 if (error != 0) { 1118 log(LOG_WARNING, 1119 "in6_update_ifa: addmulti failed for " 1120 "%s on %s (errno=%d)\n", 1121 ip6_sprintf(&llsol), if_name(ifp), 1122 error); 1123 in6_purgeaddr((struct ifaddr *)ia); 1124 return (error); 1125 } 1126 } 1127 1128 bzero(&mltmask, sizeof(mltmask)); 1129 mltmask.sin6_len = sizeof(struct sockaddr_in6); 1130 mltmask.sin6_family = AF_INET6; 1131 mltmask.sin6_addr = in6mask32; 1132 1133 /* 1134 * join link-local all-nodes address 1135 */ 1136 bzero(&mltaddr, sizeof(mltaddr)); 1137 mltaddr.sin6_len = sizeof(struct sockaddr_in6); 1138 mltaddr.sin6_family = AF_INET6; 1139 mltaddr.sin6_addr = kin6addr_linklocal_allnodes; 1140 mltaddr.sin6_addr.s6_addr16[1] = htons(ifp->if_index); 1141 1142 in6m = IN6_LOOKUP_MULTI(&mltaddr.sin6_addr, ifp); 1143 if (in6m == NULL) { 1144 rtrequest_global(RTM_ADD, 1145 (struct sockaddr *)&mltaddr, 1146 (struct sockaddr *)&ia->ia_addr, 1147 (struct sockaddr *)&mltmask, 1148 RTF_UP|RTF_CLONING); /* xxx */ 1149 in6_addmulti(&mltaddr.sin6_addr, ifp, &error); 1150 if (error != 0) { 1151 log(LOG_WARNING, 1152 "in6_update_ifa: addmulti failed for " 1153 "%s on %s (errno=%d)\n", 1154 ip6_sprintf(&mltaddr.sin6_addr), 1155 if_name(ifp), error); 1156 } 1157 } 1158 1159 /* 1160 * join node information group address 1161 */ 1162 #define hostnamelen strlen(hostname) 1163 if (in6_nigroup(ifp, hostname, hostnamelen, &mltaddr.sin6_addr) 1164 == 0) { 1165 in6m = IN6_LOOKUP_MULTI(&mltaddr.sin6_addr, ifp); 1166 if (in6m == NULL && ia != NULL) { 1167 in6_addmulti(&mltaddr.sin6_addr, ifp, &error); 1168 if (error != 0) { 1169 log(LOG_WARNING, "in6_update_ifa: " 1170 "addmulti failed for " 1171 "%s on %s (errno=%d)\n", 1172 ip6_sprintf(&mltaddr.sin6_addr), 1173 if_name(ifp), error); 1174 } 1175 } 1176 } 1177 #undef hostnamelen 1178 1179 /* 1180 * join node-local all-nodes address, on loopback. 1181 * XXX: since "node-local" is obsoleted by interface-local, 1182 * we have to join the group on every interface with 1183 * some interface-boundary restriction. 1184 */ 1185 if (ifp->if_flags & IFF_LOOPBACK) { 1186 struct in6_ifaddr *ia_loop; 1187 1188 struct in6_addr loop6 = kin6addr_loopback; 1189 ia_loop = in6ifa_ifpwithaddr(ifp, &loop6); 1190 1191 mltaddr.sin6_addr = kin6addr_nodelocal_allnodes; 1192 1193 in6m = IN6_LOOKUP_MULTI(&mltaddr.sin6_addr, ifp); 1194 if (in6m == NULL && ia_loop != NULL) { 1195 rtrequest_global(RTM_ADD, 1196 (struct sockaddr *)&mltaddr, 1197 (struct sockaddr *)&ia_loop->ia_addr, 1198 (struct sockaddr *)&mltmask, 1199 RTF_UP); 1200 in6_addmulti(&mltaddr.sin6_addr, ifp, &error); 1201 if (error != 0) { 1202 log(LOG_WARNING, "in6_update_ifa: " 1203 "addmulti failed for %s on %s " 1204 "(errno=%d)\n", 1205 ip6_sprintf(&mltaddr.sin6_addr), 1206 if_name(ifp), error); 1207 } 1208 } 1209 } 1210 } 1211 1212 /* 1213 * Perform DAD, if needed. 1214 * XXX It may be of use, if we can administratively 1215 * disable DAD. 1216 */ 1217 if (in6if_do_dad(ifp) && 1218 !(ifra->ifra_flags & IN6_IFF_NODAD) && 1219 ia->ia6_flags & IN6_IFF_TENTATIVE) 1220 nd6_dad_start((struct ifaddr *)ia, NULL); 1221 1222 return (error); 1223 1224 unlink: 1225 /* 1226 * XXX: if a change of an existing address failed, keep the entry 1227 * anyway. 1228 */ 1229 if (hostIsNew) 1230 in6_unlink_ifa(ia, ifp); 1231 return (error); 1232 } 1233 1234 void 1235 in6_purgeaddr(struct ifaddr *ifa) 1236 { 1237 struct ifnet *ifp = ifa->ifa_ifp; 1238 struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa; 1239 1240 /* stop DAD processing */ 1241 nd6_dad_stop(ifa); 1242 1243 /* 1244 * delete route to the destination of the address being purged. 1245 * The interface must be p2p or loopback in this case. 1246 */ 1247 if ((ia->ia_flags & IFA_ROUTE) && ia->ia_dstaddr.sin6_len != 0) { 1248 int e; 1249 1250 if ((e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST)) 1251 != 0) { 1252 log(LOG_ERR, "in6_purgeaddr: failed to remove " 1253 "a route to the p2p destination: %s on %s, " 1254 "errno=%d\n", 1255 ip6_sprintf(&ia->ia_addr.sin6_addr), if_name(ifp), 1256 e); 1257 /* proceed anyway... */ 1258 } 1259 else 1260 ia->ia_flags &= ~IFA_ROUTE; 1261 } 1262 1263 /* Remove ownaddr's loopback rtentry, if it exists. */ 1264 in6_ifremloop(&(ia->ia_ifa)); 1265 1266 if (ifp->if_flags & IFF_MULTICAST) { 1267 /* 1268 * delete solicited multicast addr for deleting host id 1269 */ 1270 struct in6_multi *in6m; 1271 struct in6_addr llsol; 1272 bzero(&llsol, sizeof(struct in6_addr)); 1273 llsol.s6_addr16[0] = htons(0xff02); 1274 llsol.s6_addr16[1] = htons(ifp->if_index); 1275 llsol.s6_addr32[1] = 0; 1276 llsol.s6_addr32[2] = htonl(1); 1277 llsol.s6_addr32[3] = 1278 ia->ia_addr.sin6_addr.s6_addr32[3]; 1279 llsol.s6_addr8[12] = 0xff; 1280 1281 in6m = IN6_LOOKUP_MULTI(&llsol, ifp); 1282 if (in6m) 1283 in6_delmulti(in6m); 1284 } 1285 1286 in6_unlink_ifa(ia, ifp); 1287 } 1288 1289 static void 1290 in6_unlink_ifa(struct in6_ifaddr *ia, struct ifnet *ifp) 1291 { 1292 struct in6_ifaddr *oia; 1293 1294 crit_enter(); 1295 1296 ifa_ifunlink(&ia->ia_ifa, ifp); 1297 1298 oia = ia; 1299 if (oia == (ia = in6_ifaddr)) 1300 in6_ifaddr = ia->ia_next; 1301 else { 1302 while (ia->ia_next && (ia->ia_next != oia)) 1303 ia = ia->ia_next; 1304 if (ia->ia_next) 1305 ia->ia_next = oia->ia_next; 1306 else { 1307 /* search failed */ 1308 kprintf("Couldn't unlink in6_ifaddr from in6_ifaddr\n"); 1309 } 1310 } 1311 1312 /* 1313 * When an autoconfigured address is being removed, release the 1314 * reference to the base prefix. Also, since the release might 1315 * affect the status of other (detached) addresses, call 1316 * pfxlist_onlink_check(). 1317 */ 1318 if (oia->ia6_flags & IN6_IFF_AUTOCONF) { 1319 if (oia->ia6_ndpr == NULL) { 1320 log(LOG_NOTICE, "in6_unlink_ifa: autoconf'ed address " 1321 "%p has no prefix\n", oia); 1322 } else { 1323 oia->ia6_ndpr->ndpr_refcnt--; 1324 oia->ia6_flags &= ~IN6_IFF_AUTOCONF; 1325 oia->ia6_ndpr = NULL; 1326 } 1327 1328 pfxlist_onlink_check(); 1329 } 1330 1331 /* 1332 * release another refcnt for the link from in6_ifaddr. 1333 * Note that we should decrement the refcnt at least once for all *BSD. 1334 */ 1335 ifa_destroy(&oia->ia_ifa); 1336 1337 crit_exit(); 1338 } 1339 1340 void 1341 in6_purgeif(struct ifnet *ifp) 1342 { 1343 struct ifaddr_container *ifac, *next; 1344 1345 TAILQ_FOREACH_MUTABLE(ifac, &ifp->if_addrheads[mycpuid], 1346 ifa_link, next) { 1347 if (ifac->ifa->ifa_addr->sa_family != AF_INET6) 1348 continue; 1349 in6_purgeaddr(ifac->ifa); 1350 } 1351 1352 in6_ifdetach(ifp); 1353 } 1354 1355 /* 1356 * SIOC[GAD]LIFADDR. 1357 * SIOCGLIFADDR: get first address. (?) 1358 * SIOCGLIFADDR with IFLR_PREFIX: 1359 * get first address that matches the specified prefix. 1360 * SIOCALIFADDR: add the specified address. 1361 * SIOCALIFADDR with IFLR_PREFIX: 1362 * add the specified prefix, filling hostid part from 1363 * the first link-local address. prefixlen must be <= 64. 1364 * SIOCDLIFADDR: delete the specified address. 1365 * SIOCDLIFADDR with IFLR_PREFIX: 1366 * delete the first address that matches the specified prefix. 1367 * return values: 1368 * EINVAL on invalid parameters 1369 * EADDRNOTAVAIL on prefix match failed/specified address not found 1370 * other values may be returned from in6_ioctl() 1371 * 1372 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64. 1373 * this is to accomodate address naming scheme other than RFC2374, 1374 * in the future. 1375 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374 1376 * address encoding scheme. (see figure on page 8) 1377 */ 1378 static int 1379 in6_lifaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, 1380 struct thread *td) 1381 { 1382 struct if_laddrreq *iflr = (struct if_laddrreq *)data; 1383 struct sockaddr *sa; 1384 1385 /* sanity checks */ 1386 if (!data || !ifp) { 1387 panic("invalid argument to in6_lifaddr_ioctl"); 1388 /*NOTRECHED*/ 1389 } 1390 1391 switch (cmd) { 1392 case SIOCGLIFADDR: 1393 /* address must be specified on GET with IFLR_PREFIX */ 1394 if (!(iflr->flags & IFLR_PREFIX)) 1395 break; 1396 /* FALLTHROUGH */ 1397 case SIOCALIFADDR: 1398 case SIOCDLIFADDR: 1399 /* address must be specified on ADD and DELETE */ 1400 sa = (struct sockaddr *)&iflr->addr; 1401 if (sa->sa_family != AF_INET6) 1402 return EINVAL; 1403 if (sa->sa_len != sizeof(struct sockaddr_in6)) 1404 return EINVAL; 1405 /* XXX need improvement */ 1406 sa = (struct sockaddr *)&iflr->dstaddr; 1407 if (sa->sa_family && sa->sa_family != AF_INET6) 1408 return EINVAL; 1409 if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6)) 1410 return EINVAL; 1411 break; 1412 default: /* shouldn't happen */ 1413 #if 0 1414 panic("invalid cmd to in6_lifaddr_ioctl"); 1415 /* NOTREACHED */ 1416 #else 1417 return EOPNOTSUPP; 1418 #endif 1419 } 1420 if (sizeof(struct in6_addr) * 8 < iflr->prefixlen) 1421 return EINVAL; 1422 1423 switch (cmd) { 1424 case SIOCALIFADDR: 1425 { 1426 struct in6_aliasreq ifra; 1427 struct in6_addr *hostid = NULL; 1428 int prefixlen; 1429 1430 if (iflr->flags & IFLR_PREFIX) { 1431 struct ifaddr *ifa; 1432 struct sockaddr_in6 *sin6; 1433 1434 /* 1435 * hostid is to fill in the hostid part of the 1436 * address. hostid points to the first link-local 1437 * address attached to the interface. 1438 */ 1439 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0); 1440 if (!ifa) 1441 return EADDRNOTAVAIL; 1442 hostid = IFA_IN6(ifa); 1443 1444 /* prefixlen must be <= 64. */ 1445 if (64 < iflr->prefixlen) 1446 return EINVAL; 1447 prefixlen = iflr->prefixlen; 1448 1449 /* hostid part must be zero. */ 1450 sin6 = (struct sockaddr_in6 *)&iflr->addr; 1451 if (sin6->sin6_addr.s6_addr32[2] != 0 1452 || sin6->sin6_addr.s6_addr32[3] != 0) { 1453 return EINVAL; 1454 } 1455 } else 1456 prefixlen = iflr->prefixlen; 1457 1458 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */ 1459 bzero(&ifra, sizeof(ifra)); 1460 bcopy(iflr->iflr_name, ifra.ifra_name, 1461 sizeof(ifra.ifra_name)); 1462 1463 bcopy(&iflr->addr, &ifra.ifra_addr, 1464 ((struct sockaddr *)&iflr->addr)->sa_len); 1465 if (hostid) { 1466 /* fill in hostid part */ 1467 ifra.ifra_addr.sin6_addr.s6_addr32[2] = 1468 hostid->s6_addr32[2]; 1469 ifra.ifra_addr.sin6_addr.s6_addr32[3] = 1470 hostid->s6_addr32[3]; 1471 } 1472 1473 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /*XXX*/ 1474 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr, 1475 ((struct sockaddr *)&iflr->dstaddr)->sa_len); 1476 if (hostid) { 1477 ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] = 1478 hostid->s6_addr32[2]; 1479 ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] = 1480 hostid->s6_addr32[3]; 1481 } 1482 } 1483 1484 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6); 1485 in6_prefixlen2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen); 1486 1487 ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX; 1488 return in6_control_internal(SIOCAIFADDR_IN6, (caddr_t)&ifra, 1489 ifp, td); 1490 } 1491 case SIOCGLIFADDR: 1492 case SIOCDLIFADDR: 1493 { 1494 struct ifaddr_container *ifac; 1495 struct in6_ifaddr *ia; 1496 struct in6_addr mask, candidate, match; 1497 struct sockaddr_in6 *sin6; 1498 int cmp; 1499 1500 bzero(&mask, sizeof(mask)); 1501 if (iflr->flags & IFLR_PREFIX) { 1502 /* lookup a prefix rather than address. */ 1503 in6_prefixlen2mask(&mask, iflr->prefixlen); 1504 1505 sin6 = (struct sockaddr_in6 *)&iflr->addr; 1506 bcopy(&sin6->sin6_addr, &match, sizeof(match)); 1507 match.s6_addr32[0] &= mask.s6_addr32[0]; 1508 match.s6_addr32[1] &= mask.s6_addr32[1]; 1509 match.s6_addr32[2] &= mask.s6_addr32[2]; 1510 match.s6_addr32[3] &= mask.s6_addr32[3]; 1511 1512 /* if you set extra bits, that's wrong */ 1513 if (bcmp(&match, &sin6->sin6_addr, sizeof(match))) 1514 return EINVAL; 1515 1516 cmp = 1; 1517 } else { 1518 if (cmd == SIOCGLIFADDR) { 1519 /* on getting an address, take the 1st match */ 1520 cmp = 0; /* XXX */ 1521 } else { 1522 /* on deleting an address, do exact match */ 1523 in6_prefixlen2mask(&mask, 128); 1524 sin6 = (struct sockaddr_in6 *)&iflr->addr; 1525 bcopy(&sin6->sin6_addr, &match, sizeof(match)); 1526 1527 cmp = 1; 1528 } 1529 } 1530 1531 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) { 1532 struct ifaddr *ifa = ifac->ifa; 1533 1534 if (ifa->ifa_addr->sa_family != AF_INET6) 1535 continue; 1536 if (!cmp) 1537 break; 1538 1539 bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate)); 1540 /* 1541 * XXX: this is adhoc, but is necessary to allow 1542 * a user to specify fe80::/64 (not /10) for a 1543 * link-local address. 1544 */ 1545 if (IN6_IS_ADDR_LINKLOCAL(&candidate)) 1546 candidate.s6_addr16[1] = 0; 1547 candidate.s6_addr32[0] &= mask.s6_addr32[0]; 1548 candidate.s6_addr32[1] &= mask.s6_addr32[1]; 1549 candidate.s6_addr32[2] &= mask.s6_addr32[2]; 1550 candidate.s6_addr32[3] &= mask.s6_addr32[3]; 1551 if (IN6_ARE_ADDR_EQUAL(&candidate, &match)) 1552 break; 1553 } 1554 if (ifac == NULL) 1555 return EADDRNOTAVAIL; 1556 ia = ifa2ia6(ifac->ifa); 1557 1558 if (cmd == SIOCGLIFADDR) { 1559 struct sockaddr_in6 *s6; 1560 1561 /* fill in the if_laddrreq structure */ 1562 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len); 1563 s6 = (struct sockaddr_in6 *)&iflr->addr; 1564 if (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr)) { 1565 s6->sin6_addr.s6_addr16[1] = 0; 1566 if (in6_addr2zoneid(ifp, &s6->sin6_addr, 1567 &s6->sin6_scope_id)) 1568 return (EINVAL);/* XXX */ 1569 } 1570 if (ifp->if_flags & IFF_POINTOPOINT) { 1571 bcopy(&ia->ia_dstaddr, &iflr->dstaddr, 1572 ia->ia_dstaddr.sin6_len); 1573 s6 = (struct sockaddr_in6 *)&iflr->dstaddr; 1574 if (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr)) { 1575 s6->sin6_addr.s6_addr16[1] = 0; 1576 if (in6_addr2zoneid(ifp, 1577 &s6->sin6_addr, &s6->sin6_scope_id)) 1578 return (EINVAL); /* EINVAL */ 1579 } 1580 } else 1581 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr)); 1582 1583 iflr->prefixlen = 1584 in6_mask2len(&ia->ia_prefixmask.sin6_addr, 1585 NULL); 1586 1587 iflr->flags = ia->ia6_flags; /* XXX */ 1588 1589 return 0; 1590 } else { 1591 struct in6_aliasreq ifra; 1592 1593 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */ 1594 bzero(&ifra, sizeof(ifra)); 1595 bcopy(iflr->iflr_name, ifra.ifra_name, 1596 sizeof(ifra.ifra_name)); 1597 1598 bcopy(&ia->ia_addr, &ifra.ifra_addr, 1599 ia->ia_addr.sin6_len); 1600 if (ifp->if_flags & IFF_POINTOPOINT) 1601 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr, 1602 ia->ia_dstaddr.sin6_len); 1603 else 1604 bzero(&ifra.ifra_dstaddr, 1605 sizeof(ifra.ifra_dstaddr)); 1606 bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr, 1607 ia->ia_prefixmask.sin6_len); 1608 1609 ifra.ifra_flags = ia->ia6_flags; 1610 return in6_control_internal(SIOCDIFADDR_IN6, 1611 (caddr_t)&ifra, ifp, td); 1612 } 1613 } 1614 } 1615 1616 return EOPNOTSUPP; /* just for safety */ 1617 } 1618 1619 /* 1620 * Initialize an interface's intetnet6 address 1621 * and routing table entry. 1622 */ 1623 static int 1624 in6_ifinit(struct ifnet *ifp, struct in6_ifaddr *ia, struct sockaddr_in6 *sin6, 1625 int newhost) 1626 { 1627 int error = 0, plen; 1628 1629 ia->ia_addr = *sin6; 1630 1631 if (ifp->if_ioctl != NULL) { 1632 ifnet_serialize_all(ifp); 1633 error = ifp->if_ioctl(ifp, SIOCSIFADDR, (caddr_t)ia, NULL); 1634 ifnet_deserialize_all(ifp); 1635 if (error) 1636 return (error); 1637 } 1638 1639 ia->ia_ifa.ifa_metric = ifp->if_metric; 1640 1641 /* we could do in(6)_socktrim here, but just omit it at this moment. */ 1642 1643 /* 1644 * Special case: 1645 * If the destination address is specified for a point-to-point 1646 * interface, install a route to the destination as an interface 1647 * direct route. 1648 */ 1649 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */ 1650 if (plen == 128 && ia->ia_dstaddr.sin6_family == AF_INET6) { 1651 if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD, 1652 RTF_UP | RTF_HOST)) != 0) 1653 return (error); 1654 ia->ia_flags |= IFA_ROUTE; 1655 } 1656 if (plen < 128) { 1657 /* 1658 * The RTF_CLONING flag is necessary for in6_is_ifloop_auto(). 1659 */ 1660 ia->ia_ifa.ifa_flags |= RTF_CLONING; 1661 } 1662 1663 /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */ 1664 if (newhost) { 1665 /* set the rtrequest function to create llinfo */ 1666 ia->ia_ifa.ifa_rtrequest = nd6_rtrequest; 1667 in6_ifaddloop(&(ia->ia_ifa)); 1668 } 1669 1670 return (error); 1671 } 1672 1673 struct in6_multi_mship * 1674 in6_joingroup(struct ifnet *ifp, struct in6_addr *addr, int *errorp) 1675 { 1676 struct in6_multi_mship *imm; 1677 1678 imm = kmalloc(sizeof(*imm), M_IPMADDR, M_NOWAIT); 1679 if (!imm) { 1680 *errorp = ENOBUFS; 1681 return NULL; 1682 } 1683 imm->i6mm_maddr = in6_addmulti(addr, ifp, errorp); 1684 if (!imm->i6mm_maddr) { 1685 /* *errorp is alrady set */ 1686 kfree(imm, M_IPMADDR); 1687 return NULL; 1688 } 1689 return imm; 1690 } 1691 1692 int 1693 in6_leavegroup(struct in6_multi_mship *imm) 1694 { 1695 1696 if (imm->i6mm_maddr) 1697 in6_delmulti(imm->i6mm_maddr); 1698 kfree(imm, M_IPMADDR); 1699 return 0; 1700 } 1701 1702 /* 1703 * Add an address to the list of IP6 multicast addresses for a 1704 * given interface. 1705 */ 1706 struct in6_multi * 1707 in6_addmulti(struct in6_addr *maddr6, struct ifnet *ifp, int *errorp) 1708 { 1709 struct in6_multi *in6m; 1710 struct sockaddr_in6 sin6; 1711 struct ifmultiaddr *ifma; 1712 1713 *errorp = 0; 1714 1715 crit_enter(); 1716 1717 /* 1718 * Call generic routine to add membership or increment 1719 * refcount. It wants addresses in the form of a sockaddr, 1720 * so we build one here (being careful to zero the unused bytes). 1721 */ 1722 bzero(&sin6, sizeof sin6); 1723 sin6.sin6_family = AF_INET6; 1724 sin6.sin6_len = sizeof sin6; 1725 sin6.sin6_addr = *maddr6; 1726 *errorp = if_addmulti(ifp, (struct sockaddr *)&sin6, &ifma); 1727 if (*errorp) { 1728 crit_exit(); 1729 return 0; 1730 } 1731 1732 /* 1733 * If ifma->ifma_protospec is null, then if_addmulti() created 1734 * a new record. Otherwise, we are done. 1735 */ 1736 if (ifma->ifma_protospec != NULL) { 1737 crit_exit(); 1738 return ifma->ifma_protospec; 1739 } 1740 1741 in6m = kmalloc(sizeof(*in6m), M_IPMADDR, M_INTWAIT | M_ZERO); 1742 in6m->in6m_addr = *maddr6; 1743 in6m->in6m_ifp = ifp; 1744 in6m->in6m_ifma = ifma; 1745 ifma->ifma_protospec = in6m; 1746 LIST_INSERT_HEAD(&in6_multihead, in6m, in6m_entry); 1747 1748 /* 1749 * Let MLD6 know that we have joined a new IP6 multicast 1750 * group. 1751 */ 1752 mld6_start_listening(in6m); 1753 crit_exit(); 1754 return (in6m); 1755 } 1756 1757 /* 1758 * Delete a multicast address record. 1759 */ 1760 void 1761 in6_delmulti(struct in6_multi *in6m) 1762 { 1763 struct ifmultiaddr *ifma = in6m->in6m_ifma; 1764 1765 crit_enter(); 1766 1767 if (ifma->ifma_refcount == 1) { 1768 /* 1769 * No remaining claims to this record; let MLD6 know 1770 * that we are leaving the multicast group. 1771 */ 1772 mld6_stop_listening(in6m); 1773 ifma->ifma_protospec = NULL; 1774 LIST_REMOVE(in6m, in6m_entry); 1775 kfree(in6m, M_IPMADDR); 1776 } 1777 /* XXX - should be separate API for when we have an ifma? */ 1778 if_delmulti(ifma->ifma_ifp, ifma->ifma_addr); 1779 crit_exit(); 1780 } 1781 1782 /* 1783 * Find an IPv6 interface link-local address specific to an interface. 1784 */ 1785 struct in6_ifaddr * 1786 in6ifa_ifpforlinklocal(struct ifnet *ifp, int ignoreflags) 1787 { 1788 const struct ifaddr_container *ifac; 1789 1790 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) { 1791 struct ifaddr *ifa = ifac->ifa; 1792 1793 if (ifa->ifa_addr == NULL) 1794 continue; /* just for safety */ 1795 if (ifa->ifa_addr->sa_family != AF_INET6) 1796 continue; 1797 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) { 1798 if ((((struct in6_ifaddr *)ifa)->ia6_flags & 1799 ignoreflags) != 0) 1800 continue; 1801 return (struct in6_ifaddr *)ifa; 1802 } 1803 } 1804 return NULL; 1805 } 1806 1807 1808 /* 1809 * find the internet address corresponding to a given interface and address. 1810 */ 1811 struct in6_ifaddr * 1812 in6ifa_ifpwithaddr(struct ifnet *ifp, struct in6_addr *addr) 1813 { 1814 const struct ifaddr_container *ifac; 1815 1816 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) { 1817 struct ifaddr *ifa = ifac->ifa; 1818 1819 if (ifa->ifa_addr == NULL) 1820 continue; /* just for safety */ 1821 if (ifa->ifa_addr->sa_family != AF_INET6) 1822 continue; 1823 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa))) 1824 return (struct in6_ifaddr *)ifa; 1825 } 1826 return NULL; 1827 } 1828 1829 /* 1830 * Find a link-local scoped address on ifp and return it if any. 1831 */ 1832 struct in6_ifaddr * 1833 in6ifa_llaonifp(struct ifnet *ifp) 1834 { 1835 const struct ifaddr_container *ifac; 1836 1837 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) { 1838 const struct sockaddr_in6 *sin6; 1839 struct ifaddr *ifa = ifac->ifa; 1840 1841 if (ifa->ifa_addr->sa_family != AF_INET6) 1842 continue; 1843 sin6 = (const struct sockaddr_in6 *)ifa->ifa_addr; 1844 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) || 1845 /* XXX why are mcast addresses ifp address list? */ 1846 IN6_IS_ADDR_MC_INTFACELOCAL(&sin6->sin6_addr) || 1847 IN6_IS_ADDR_MC_NODELOCAL(&sin6->sin6_addr)) 1848 return (struct in6_ifaddr *)ifa; 1849 } 1850 return NULL; 1851 } 1852 1853 /* 1854 * find the internet address on a given interface corresponding to a neighbor's 1855 * address. 1856 */ 1857 struct in6_ifaddr * 1858 in6ifa_ifplocaladdr(const struct ifnet *ifp, const struct in6_addr *addr) 1859 { 1860 struct ifaddr *ifa; 1861 struct in6_ifaddr *ia; 1862 struct ifaddr_container *ifac; 1863 1864 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) { 1865 ifa = ifac->ifa; 1866 1867 if (ifa->ifa_addr == NULL) 1868 continue; /* just for safety */ 1869 if (ifa->ifa_addr->sa_family != AF_INET6) 1870 continue; 1871 ia = (struct in6_ifaddr *)ifa; 1872 if (IN6_ARE_MASKED_ADDR_EQUAL(addr, 1873 &ia->ia_addr.sin6_addr, 1874 &ia->ia_prefixmask.sin6_addr)) 1875 return ia; 1876 } 1877 1878 return NULL; 1879 } 1880 1881 /* 1882 * Convert IP6 address to printable (loggable) representation. 1883 */ 1884 static char digits[] = "0123456789abcdef"; 1885 static int ip6round = 0; 1886 char * 1887 ip6_sprintf(const struct in6_addr *addr) 1888 { 1889 static char ip6buf[8][48]; 1890 int i; 1891 char *cp; 1892 const u_short *a = (const u_short *)addr; 1893 const u_char *d; 1894 int dcolon = 0; 1895 1896 ip6round = (ip6round + 1) & 7; 1897 cp = ip6buf[ip6round]; 1898 1899 for (i = 0; i < 8; i++) { 1900 if (dcolon == 1) { 1901 if (*a == 0) { 1902 if (i == 7) 1903 *cp++ = ':'; 1904 a++; 1905 continue; 1906 } else 1907 dcolon = 2; 1908 } 1909 if (*a == 0) { 1910 if (dcolon == 0 && *(a + 1) == 0) { 1911 if (i == 0) 1912 *cp++ = ':'; 1913 *cp++ = ':'; 1914 dcolon = 1; 1915 } else { 1916 *cp++ = '0'; 1917 *cp++ = ':'; 1918 } 1919 a++; 1920 continue; 1921 } 1922 d = (const u_char *)a; 1923 *cp++ = digits[*d >> 4]; 1924 *cp++ = digits[*d++ & 0xf]; 1925 *cp++ = digits[*d >> 4]; 1926 *cp++ = digits[*d & 0xf]; 1927 *cp++ = ':'; 1928 a++; 1929 } 1930 *--cp = 0; 1931 return (ip6buf[ip6round]); 1932 } 1933 1934 int 1935 in6_localaddr(struct in6_addr *in6) 1936 { 1937 struct in6_ifaddr *ia; 1938 1939 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6)) 1940 return 1; 1941 1942 for (ia = in6_ifaddr; ia; ia = ia->ia_next) 1943 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr, 1944 &ia->ia_prefixmask.sin6_addr)) 1945 return 1; 1946 1947 return (0); 1948 } 1949 1950 int 1951 in6_is_addr_deprecated(struct sockaddr_in6 *sa6) 1952 { 1953 struct in6_ifaddr *ia; 1954 1955 for (ia = in6_ifaddr; ia; ia = ia->ia_next) { 1956 if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, 1957 &sa6->sin6_addr) && 1958 (ia->ia6_flags & IN6_IFF_DEPRECATED)) 1959 return (1); /* true */ 1960 1961 /* XXX: do we still have to go thru the rest of the list? */ 1962 } 1963 1964 return (0); /* false */ 1965 } 1966 1967 /* 1968 * return length of part which dst and src are equal 1969 * hard coding... 1970 */ 1971 int 1972 in6_matchlen(struct in6_addr *src, struct in6_addr *dst) 1973 { 1974 int match = 0; 1975 u_char *s = (u_char *)src, *d = (u_char *)dst; 1976 u_char *lim = s + 16, r; 1977 1978 while (s < lim) 1979 if ((r = (*d++ ^ *s++)) != 0) { 1980 while (r < 128) { 1981 match++; 1982 r <<= 1; 1983 } 1984 break; 1985 } else 1986 match += 8; 1987 return match; 1988 } 1989 1990 /* XXX: to be scope conscious */ 1991 int 1992 in6_are_prefix_equal(struct in6_addr *p1, struct in6_addr *p2, int len) 1993 { 1994 int bytelen, bitlen; 1995 1996 /* sanity check */ 1997 if (0 > len || len > 128) { 1998 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n", 1999 len); 2000 return (0); 2001 } 2002 2003 bytelen = len / 8; 2004 bitlen = len % 8; 2005 2006 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen)) 2007 return (0); 2008 if (p1->s6_addr[bytelen] >> (8 - bitlen) != 2009 p2->s6_addr[bytelen] >> (8 - bitlen)) 2010 return (0); 2011 2012 return (1); 2013 } 2014 2015 void 2016 in6_prefixlen2mask(struct in6_addr *maskp, int len) 2017 { 2018 u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff}; 2019 int bytelen, bitlen, i; 2020 2021 /* sanity check */ 2022 if (0 > len || len > 128) { 2023 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n", 2024 len); 2025 return; 2026 } 2027 2028 bzero(maskp, sizeof(*maskp)); 2029 bytelen = len / 8; 2030 bitlen = len % 8; 2031 for (i = 0; i < bytelen; i++) 2032 maskp->s6_addr[i] = 0xff; 2033 if (bitlen) 2034 maskp->s6_addr[bytelen] = maskarray[bitlen - 1]; 2035 } 2036 2037 /* 2038 * return the best address out of the same scope 2039 */ 2040 struct in6_ifaddr * 2041 in6_ifawithscope(struct ifnet *oifp, struct in6_addr *dst, struct ucred *cred) 2042 { 2043 int dst_scope = in6_addrscope(dst), src_scope, best_scope = 0; 2044 int blen = -1; 2045 struct in6_ifaddr *ifa_best = NULL; 2046 u_int32_t dstzone, odstzone; 2047 int jailed = 0; 2048 const struct ifnet_array *arr; 2049 int i; 2050 2051 if(cred && cred->cr_prison) 2052 jailed = 1; 2053 2054 if (oifp == NULL) 2055 return (NULL); 2056 2057 if (in6_addr2zoneid(oifp, dst, &odstzone)) 2058 return (NULL); 2059 2060 /* 2061 * We search for all addresses on all interfaces from the beginning. 2062 * Comparing an interface with the outgoing interface will be done 2063 * only at the final stage of tiebreaking. 2064 */ 2065 arr = ifnet_array_get(); 2066 for (i = 0; i < arr->ifnet_count; ++i) { 2067 struct ifnet *ifp = arr->ifnet_arr[i]; 2068 struct ifaddr_container *ifac; 2069 2070 /* 2071 * We can never take an address that breaks the scope zone 2072 * of the destination. 2073 */ 2074 if (ifp->if_afdata[AF_INET6] == NULL) 2075 continue; 2076 if (in6_addr2zoneid(ifp, dst, &dstzone) || dstzone != odstzone) 2077 continue; 2078 2079 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) { 2080 int tlen = -1, dscopecmp, bscopecmp, matchcmp; 2081 struct ifaddr *ifa = ifac->ifa; 2082 2083 if (ifa->ifa_addr->sa_family != AF_INET6) 2084 continue; 2085 2086 src_scope = in6_addrscope(IFA_IN6(ifa)); 2087 2088 /* 2089 * Don't use an address before completing DAD 2090 * nor a duplicated address. 2091 */ 2092 if (((struct in6_ifaddr *)ifa)->ia6_flags & 2093 IN6_IFF_NOTREADY) 2094 continue; 2095 2096 /* XXX: is there any case to allow anycasts? */ 2097 if (((struct in6_ifaddr *)ifa)->ia6_flags & 2098 IN6_IFF_ANYCAST) 2099 continue; 2100 2101 if (((struct in6_ifaddr *)ifa)->ia6_flags & 2102 IN6_IFF_DETACHED) 2103 continue; 2104 2105 /* Skip adresses not valid for current jail */ 2106 if (jailed && 2107 !(jailed_ip(cred->cr_prison, (struct sockaddr *)(ifa->ifa_addr)) != 0)) 2108 continue; 2109 2110 /* 2111 * If this is the first address we find, 2112 * keep it anyway. 2113 */ 2114 if (ifa_best == NULL) 2115 goto replace; 2116 2117 /* 2118 * ifa_best is never NULL beyond this line except 2119 * within the block labeled "replace". 2120 */ 2121 2122 /* 2123 * If ifa_best has a smaller scope than dst and 2124 * the current address has a larger one than 2125 * (or equal to) dst, always replace ifa_best. 2126 * Also, if the current address has a smaller scope 2127 * than dst, ignore it unless ifa_best also has a 2128 * smaller scope. 2129 * Consequently, after the two if-clause below, 2130 * the followings must be satisfied: 2131 * (scope(src) < scope(dst) && 2132 * scope(best) < scope(dst)) 2133 * OR 2134 * (scope(best) >= scope(dst) && 2135 * scope(src) >= scope(dst)) 2136 */ 2137 if (IN6_ARE_SCOPE_CMP(best_scope, dst_scope) < 0 && 2138 IN6_ARE_SCOPE_CMP(src_scope, dst_scope) >= 0) 2139 goto replace; /* (A) */ 2140 if (IN6_ARE_SCOPE_CMP(src_scope, dst_scope) < 0 && 2141 IN6_ARE_SCOPE_CMP(best_scope, dst_scope) >= 0) 2142 continue; /* (B) */ 2143 2144 /* 2145 * A deprecated address SHOULD NOT be used in new 2146 * communications if an alternate (non-deprecated) 2147 * address is available and has sufficient scope. 2148 * RFC 2462, Section 5.5.4. 2149 */ 2150 if (((struct in6_ifaddr *)ifa)->ia6_flags & 2151 IN6_IFF_DEPRECATED) { 2152 /* 2153 * Ignore any deprecated addresses if 2154 * specified by configuration. 2155 */ 2156 if (!ip6_use_deprecated) 2157 continue; 2158 2159 /* 2160 * If we have already found a non-deprecated 2161 * candidate, just ignore deprecated addresses. 2162 */ 2163 if (!(ifa_best->ia6_flags & IN6_IFF_DEPRECATED)) 2164 continue; 2165 } 2166 2167 /* 2168 * A non-deprecated address is always preferred 2169 * to a deprecated one regardless of scopes and 2170 * address matching (Note invariants ensured by the 2171 * conditions (A) and (B) above.) 2172 */ 2173 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) && 2174 !(((struct in6_ifaddr *)ifa)->ia6_flags & 2175 IN6_IFF_DEPRECATED)) 2176 goto replace; 2177 2178 /* 2179 * When we use temporary addresses described in 2180 * RFC 3041, we prefer temporary addresses to 2181 * public autoconf addresses. Again, note the 2182 * invariants from (A) and (B). Also note that we 2183 * don't have any preference between static addresses 2184 * and autoconf addresses (despite of whether or not 2185 * the latter is temporary or public.) 2186 */ 2187 if (ip6_use_tempaddr) { 2188 struct in6_ifaddr *ifat; 2189 2190 ifat = (struct in6_ifaddr *)ifa; 2191 if ((ifa_best->ia6_flags & 2192 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY)) 2193 == IN6_IFF_AUTOCONF && 2194 (ifat->ia6_flags & 2195 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY)) 2196 == (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY)) { 2197 goto replace; 2198 } 2199 if ((ifa_best->ia6_flags & 2200 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY)) 2201 == (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY) && 2202 (ifat->ia6_flags & 2203 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY)) 2204 == IN6_IFF_AUTOCONF) { 2205 continue; 2206 } 2207 } 2208 2209 /* 2210 * At this point, we have two cases: 2211 * 1. we are looking at a non-deprecated address, 2212 * and ifa_best is also non-deprecated. 2213 * 2. we are looking at a deprecated address, 2214 * and ifa_best is also deprecated. 2215 * Also, we do not have to consider a case where 2216 * the scope of if_best is larger(smaller) than dst and 2217 * the scope of the current address is smaller(larger) 2218 * than dst. Such a case has already been covered. 2219 * Tiebreaking is done according to the following 2220 * items: 2221 * - the scope comparison between the address and 2222 * dst (dscopecmp) 2223 * - the scope comparison between the address and 2224 * ifa_best (bscopecmp) 2225 * - if the address match dst longer than ifa_best 2226 * (matchcmp) 2227 * - if the address is on the outgoing I/F (outI/F) 2228 * 2229 * Roughly speaking, the selection policy is 2230 * - the most important item is scope. The same scope 2231 * is best. Then search for a larger scope. 2232 * Smaller scopes are the last resort. 2233 * - A deprecated address is chosen only when we have 2234 * no address that has an enough scope, but is 2235 * prefered to any addresses of smaller scopes 2236 * (this must be already done above.) 2237 * - addresses on the outgoing I/F are preferred to 2238 * ones on other interfaces if none of above 2239 * tiebreaks. In the table below, the column "bI" 2240 * means if the best_ifa is on the outgoing 2241 * interface, and the column "sI" means if the ifa 2242 * is on the outgoing interface. 2243 * - If there is no other reasons to choose one, 2244 * longest address match against dst is considered. 2245 * 2246 * The precise decision table is as follows: 2247 * dscopecmp bscopecmp match bI oI | replace? 2248 * N/A equal N/A Y N | No (1) 2249 * N/A equal N/A N Y | Yes (2) 2250 * N/A equal larger N/A | Yes (3) 2251 * N/A equal !larger N/A | No (4) 2252 * larger larger N/A N/A | No (5) 2253 * larger smaller N/A N/A | Yes (6) 2254 * smaller larger N/A N/A | Yes (7) 2255 * smaller smaller N/A N/A | No (8) 2256 * equal smaller N/A N/A | Yes (9) 2257 * equal larger (already done at A above) 2258 */ 2259 dscopecmp = IN6_ARE_SCOPE_CMP(src_scope, dst_scope); 2260 bscopecmp = IN6_ARE_SCOPE_CMP(src_scope, best_scope); 2261 2262 if (bscopecmp == 0) { 2263 struct ifnet *bifp = ifa_best->ia_ifp; 2264 2265 if (bifp == oifp && ifp != oifp) /* (1) */ 2266 continue; 2267 if (bifp != oifp && ifp == oifp) /* (2) */ 2268 goto replace; 2269 2270 /* 2271 * Both bifp and ifp are on the outgoing 2272 * interface, or both two are on a different 2273 * interface from the outgoing I/F. 2274 * now we need address matching against dst 2275 * for tiebreaking. 2276 */ 2277 tlen = in6_matchlen(IFA_IN6(ifa), dst); 2278 matchcmp = tlen - blen; 2279 if (matchcmp > 0) /* (3) */ 2280 goto replace; 2281 continue; /* (4) */ 2282 } 2283 if (dscopecmp > 0) { 2284 if (bscopecmp > 0) /* (5) */ 2285 continue; 2286 goto replace; /* (6) */ 2287 } 2288 if (dscopecmp < 0) { 2289 if (bscopecmp > 0) /* (7) */ 2290 goto replace; 2291 continue; /* (8) */ 2292 } 2293 2294 /* now dscopecmp must be 0 */ 2295 if (bscopecmp < 0) 2296 goto replace; /* (9) */ 2297 2298 replace: 2299 ifa_best = (struct in6_ifaddr *)ifa; 2300 blen = tlen >= 0 ? tlen : 2301 in6_matchlen(IFA_IN6(ifa), dst); 2302 best_scope = in6_addrscope(&ifa_best->ia_addr.sin6_addr); 2303 } 2304 } 2305 2306 /* count statistics for future improvements */ 2307 if (ifa_best == NULL) 2308 ip6stat.ip6s_sources_none++; 2309 else { 2310 if (oifp == ifa_best->ia_ifp) 2311 ip6stat.ip6s_sources_sameif[best_scope]++; 2312 else 2313 ip6stat.ip6s_sources_otherif[best_scope]++; 2314 2315 if (best_scope == dst_scope) 2316 ip6stat.ip6s_sources_samescope[best_scope]++; 2317 else 2318 ip6stat.ip6s_sources_otherscope[best_scope]++; 2319 2320 if (ifa_best->ia6_flags & IN6_IFF_DEPRECATED) 2321 ip6stat.ip6s_sources_deprecated[best_scope]++; 2322 } 2323 2324 return (ifa_best); 2325 } 2326 2327 /* 2328 * return the best address out of the same scope. if no address was 2329 * found, return the first valid address from designated IF. 2330 */ 2331 struct in6_ifaddr * 2332 in6_ifawithifp(struct ifnet *ifp, struct in6_addr *dst) 2333 { 2334 int dst_scope = in6_addrscope(dst), blen = -1, tlen; 2335 struct ifaddr_container *ifac; 2336 struct in6_ifaddr *besta = NULL; 2337 struct in6_ifaddr *dep[2]; /* last-resort: deprecated */ 2338 2339 dep[0] = dep[1] = NULL; 2340 2341 /* 2342 * We first look for addresses in the same scope. 2343 * If there is one, return it. 2344 * If two or more, return one which matches the dst longest. 2345 * If none, return one of global addresses assigned other ifs. 2346 */ 2347 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) { 2348 struct ifaddr *ifa = ifac->ifa; 2349 2350 if (ifa->ifa_addr->sa_family != AF_INET6) 2351 continue; 2352 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST) 2353 continue; /* XXX: is there any case to allow anycast? */ 2354 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY) 2355 continue; /* don't use this interface */ 2356 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED) 2357 continue; 2358 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) { 2359 if (ip6_use_deprecated) 2360 dep[0] = (struct in6_ifaddr *)ifa; 2361 continue; 2362 } 2363 2364 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) { 2365 /* 2366 * call in6_matchlen() as few as possible 2367 */ 2368 if (besta) { 2369 if (blen == -1) 2370 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst); 2371 tlen = in6_matchlen(IFA_IN6(ifa), dst); 2372 if (tlen > blen) { 2373 blen = tlen; 2374 besta = (struct in6_ifaddr *)ifa; 2375 } 2376 } else 2377 besta = (struct in6_ifaddr *)ifa; 2378 } 2379 } 2380 if (besta) 2381 return (besta); 2382 2383 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) { 2384 struct ifaddr *ifa = ifac->ifa; 2385 2386 if (ifa->ifa_addr->sa_family != AF_INET6) 2387 continue; 2388 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST) 2389 continue; /* XXX: is there any case to allow anycast? */ 2390 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY) 2391 continue; /* don't use this interface */ 2392 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED) 2393 continue; 2394 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) { 2395 if (ip6_use_deprecated) 2396 dep[1] = (struct in6_ifaddr *)ifa; 2397 continue; 2398 } 2399 2400 return (struct in6_ifaddr *)ifa; 2401 } 2402 2403 /* use the last-resort values, that are, deprecated addresses */ 2404 if (dep[0]) 2405 return dep[0]; 2406 if (dep[1]) 2407 return dep[1]; 2408 2409 return NULL; 2410 } 2411 2412 /* 2413 * perform DAD when interface becomes IFF_UP. 2414 */ 2415 static void 2416 in6_if_up_dispatch(netmsg_t nmsg) 2417 { 2418 struct ifnet *ifp = nmsg->lmsg.u.ms_resultp; 2419 struct ifaddr_container *ifac; 2420 struct in6_ifaddr *ia; 2421 int dad_delay; /* delay ticks before DAD output */ 2422 2423 ASSERT_NETISR0; 2424 2425 /* 2426 * special cases, like 6to4, are handled in in6_ifattach 2427 */ 2428 in6_ifattach(ifp, NULL); 2429 2430 dad_delay = 0; 2431 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) { 2432 struct ifaddr *ifa = ifac->ifa; 2433 2434 if (ifa->ifa_addr->sa_family != AF_INET6) 2435 continue; 2436 ia = (struct in6_ifaddr *)ifa; 2437 if (ia->ia6_flags & IN6_IFF_TENTATIVE) 2438 nd6_dad_start(ifa, &dad_delay); 2439 } 2440 2441 netisr_replymsg(&nmsg->base, 0); 2442 } 2443 2444 void 2445 in6_if_up(struct ifnet *ifp) 2446 { 2447 struct netmsg_base nmsg; 2448 2449 netmsg_init(&nmsg, NULL, &curthread->td_msgport, 0, in6_if_up_dispatch); 2450 nmsg.lmsg.u.ms_resultp = ifp; 2451 netisr_domsg(&nmsg, 0); 2452 } 2453 2454 int 2455 in6if_do_dad(struct ifnet *ifp) 2456 { 2457 if (ifp->if_flags & IFF_LOOPBACK) 2458 return (0); 2459 2460 switch (ifp->if_type) { 2461 #ifdef IFT_DUMMY 2462 case IFT_DUMMY: 2463 return (0); 2464 #endif 2465 default: 2466 /* 2467 * Our DAD routine requires the interface up and running. 2468 * However, some interfaces can be up before the RUNNING 2469 * status. Additionaly, users may try to assign addresses 2470 * before the interface becomes up (or running). 2471 * We simply skip DAD in such a case as a work around. 2472 * XXX: we should rather mark "tentative" on such addresses, 2473 * and do DAD after the interface becomes ready. 2474 */ 2475 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != 2476 (IFF_UP|IFF_RUNNING)) 2477 return (0); 2478 2479 return (1); 2480 } 2481 } 2482 2483 /* 2484 * Calculate max IPv6 MTU through all the interfaces and store it 2485 * to in6_maxmtu. 2486 */ 2487 void 2488 in6_setmaxmtu(void) 2489 { 2490 unsigned long maxmtu = 0; 2491 const struct ifnet_array *arr; 2492 int i; 2493 2494 ASSERT_NETISR0; 2495 2496 arr = ifnet_array_get(); 2497 for (i = 0; i < arr->ifnet_count; ++i) { 2498 struct ifnet *ifp = arr->ifnet_arr[i]; 2499 2500 /* this function can be called during ifnet initialization */ 2501 if (ifp->if_afdata[AF_INET6] == NULL) 2502 continue; 2503 if ((ifp->if_flags & IFF_LOOPBACK) == 0 && 2504 IN6_LINKMTU(ifp) > maxmtu) 2505 maxmtu = IN6_LINKMTU(ifp); 2506 } 2507 if (maxmtu) /* update only when maxmtu is positive */ 2508 in6_maxmtu = maxmtu; 2509 } 2510 2511 void * 2512 in6_domifattach(struct ifnet *ifp) 2513 { 2514 struct in6_ifextra *ext; 2515 2516 ext = (struct in6_ifextra *)kmalloc(sizeof(*ext), M_IFADDR, M_WAITOK); 2517 bzero(ext, sizeof(*ext)); 2518 2519 ext->in6_ifstat = (struct in6_ifstat *)kmalloc(sizeof(struct in6_ifstat), 2520 M_IFADDR, M_WAITOK); 2521 bzero(ext->in6_ifstat, sizeof(*ext->in6_ifstat)); 2522 2523 ext->icmp6_ifstat = 2524 (struct icmp6_ifstat *)kmalloc(sizeof(struct icmp6_ifstat), 2525 M_IFADDR, M_WAITOK); 2526 bzero(ext->icmp6_ifstat, sizeof(*ext->icmp6_ifstat)); 2527 2528 ext->nd_ifinfo = nd6_ifattach(ifp); 2529 ext->scope6_id = scope6_ifattach(ifp); 2530 return ext; 2531 } 2532 2533 void 2534 in6_domifdetach(struct ifnet *ifp, void *aux) 2535 { 2536 struct in6_ifextra *ext = (struct in6_ifextra *)aux; 2537 scope6_ifdetach(ext->scope6_id); 2538 nd6_ifdetach(ext->nd_ifinfo); 2539 kfree(ext->in6_ifstat, M_IFADDR); 2540 kfree(ext->icmp6_ifstat, M_IFADDR); 2541 kfree(ext, M_IFADDR); 2542 } 2543