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