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