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