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