1 /* $FreeBSD$ */ 2 /* $KAME: in6.c,v 1.99 2000/07/11 17:00:58 jinmei 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. All advertising materials mentioning features or use of this software 46 * must display the following acknowledgement: 47 * This product includes software developed by the University of 48 * California, Berkeley and its contributors. 49 * 4. Neither the name of the University nor the names of its contributors 50 * may be used to endorse or promote products derived from this software 51 * without specific prior written permission. 52 * 53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 63 * SUCH DAMAGE. 64 * 65 * @(#)in.c 8.2 (Berkeley) 11/15/93 66 */ 67 68 #include "opt_inet.h" 69 #include "opt_inet6.h" 70 71 #include <sys/param.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 83 #include <net/if.h> 84 #include <net/if_types.h> 85 #include <net/route.h> 86 #include <net/if_dl.h> 87 88 #include <netinet/in.h> 89 #include <netinet/in_var.h> 90 #include <netinet/if_ether.h> 91 92 #include <netinet6/nd6.h> 93 #include <netinet/ip6.h> 94 #include <netinet6/ip6_var.h> 95 #include <netinet6/mld6_var.h> 96 #include <netinet6/ip6_mroute.h> 97 #include <netinet6/in6_ifattach.h> 98 #include <netinet6/scope6_var.h> 99 100 #include "gif.h" 101 #if NGIF > 0 102 #include <net/if_gif.h> 103 #endif 104 105 #include <net/net_osdep.h> 106 107 MALLOC_DEFINE(M_IPMADDR, "in6_multi", "internet multicast address"); 108 109 /* 110 * Definitions of some costant IP6 addresses. 111 */ 112 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT; 113 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT; 114 const struct in6_addr in6addr_nodelocal_allnodes = 115 IN6ADDR_NODELOCAL_ALLNODES_INIT; 116 const struct in6_addr in6addr_linklocal_allnodes = 117 IN6ADDR_LINKLOCAL_ALLNODES_INIT; 118 const struct in6_addr in6addr_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 static int in6_lifaddr_ioctl __P((struct socket *, u_long, caddr_t, 128 struct ifnet *, struct proc *)); 129 130 struct in6_multihead in6_multihead; /* XXX BSS initialization */ 131 132 /* 133 * Check if the loopback entry will be automatically generated. 134 * if 0 returned, will not be automatically generated. 135 * if 1 returned, will be automatically generated. 136 */ 137 static int 138 in6_is_ifloop_auto(struct ifaddr *ifa) 139 { 140 #define SIN6(s) ((struct sockaddr_in6 *)s) 141 /* 142 * If RTF_CLONING is unset, or (IFF_LOOPBACK | IFF_POINTOPOINT), 143 * or netmask is all0 or all1, then cloning will not happen, 144 * then we can't rely on its loopback entry generation. 145 */ 146 if ((ifa->ifa_flags & RTF_CLONING) == 0 || 147 (ifa->ifa_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) || 148 (SIN6(ifa->ifa_netmask)->sin6_len == sizeof(struct sockaddr_in6) 149 && 150 IN6_ARE_ADDR_EQUAL(&SIN6(ifa->ifa_netmask)->sin6_addr, 151 &in6mask128)) || 152 ((struct sockaddr_in6 *)ifa->ifa_netmask)->sin6_len == 0) 153 return 0; 154 else 155 return 1; 156 #undef SIN6 157 } 158 159 /* 160 * Subroutine for in6_ifaddloop() and in6_ifremloop(). 161 * This routine does actual work. 162 */ 163 static void 164 in6_ifloop_request(int cmd, struct ifaddr *ifa) 165 { 166 struct sockaddr_in6 lo_sa; 167 struct sockaddr_in6 all1_sa; 168 struct rtentry *nrt = NULL, **nrtp = NULL; 169 170 bzero(&lo_sa, sizeof(lo_sa)); 171 bzero(&all1_sa, sizeof(all1_sa)); 172 lo_sa.sin6_family = AF_INET6; 173 lo_sa.sin6_len = sizeof(struct sockaddr_in6); 174 all1_sa = lo_sa; 175 lo_sa.sin6_addr = in6addr_loopback; 176 all1_sa.sin6_addr = in6mask128; 177 178 /* 179 * So we add or remove static loopback entry, here. 180 * This request for deletion could fail, e.g. when we remove 181 * an address right after adding it. 182 */ 183 if (cmd == RTM_ADD) 184 nrtp = &nrt; 185 rtrequest(cmd, ifa->ifa_addr, 186 (struct sockaddr *)&lo_sa, 187 (struct sockaddr *)&all1_sa, 188 RTF_UP|RTF_HOST, nrtp); 189 190 /* 191 * Make sure rt_ifa be equal to IFA, the second argument of the 192 * function. 193 * We need this because when we refer rt_ifa->ia6_flags in ip6_input, 194 * we assume that the rt_ifa points to the address instead of the 195 * loopback address. 196 */ 197 if (cmd == RTM_ADD && nrt && ifa != nrt->rt_ifa) { 198 IFAFREE(nrt->rt_ifa); 199 ifa->ifa_refcnt++; 200 nrt->rt_ifa = ifa; 201 } 202 if (nrt) 203 nrt->rt_refcnt--; 204 } 205 206 /* 207 * Add ownaddr as loopback rtentry, if necessary(ex. on p2p link). 208 * Because, KAME needs loopback rtentry for ownaddr check in 209 * ip6_input(). 210 */ 211 static void 212 in6_ifaddloop(struct ifaddr *ifa) 213 { 214 if (!in6_is_ifloop_auto(ifa)) { 215 struct rtentry *rt; 216 217 /* If there is no loopback entry, allocate one. */ 218 rt = rtalloc1(ifa->ifa_addr, 0, 0); 219 if (rt == 0 || (rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) 220 in6_ifloop_request(RTM_ADD, ifa); 221 if (rt) 222 rt->rt_refcnt--; 223 } 224 } 225 226 /* 227 * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(), 228 * if it exists. 229 */ 230 static void 231 in6_ifremloop(struct ifaddr *ifa) 232 { 233 struct in6_ifaddr *ia; 234 int ia_count = 0; 235 236 /* 237 * All BSD variants except BSD/OS do not remove cloned routes 238 * from an interface direct route, when removing the direct route 239 * (see commens in net/net_osdep.h). 240 * So we should remove the route corresponding to the deleted address 241 * regardless of the result of in6_is_ifloop_auto(). 242 */ 243 if (1) 244 { 245 /* If only one ifa for the loopback entry, delete it. */ 246 for (ia = in6_ifaddr; ia; ia = ia->ia_next) { 247 if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), 248 &ia->ia_addr.sin6_addr)) { 249 ia_count++; 250 if (ia_count > 1) 251 break; 252 } 253 } 254 if (ia_count == 1) 255 in6_ifloop_request(RTM_DELETE, ifa); 256 } 257 } 258 259 int 260 in6_ifindex2scopeid(idx) 261 int idx; 262 { 263 struct ifnet *ifp; 264 struct ifaddr *ifa; 265 struct sockaddr_in6 *sin6; 266 267 if (idx < 0 || if_index < idx) 268 return -1; 269 ifp = ifindex2ifnet[idx]; 270 271 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) 272 { 273 if (ifa->ifa_addr->sa_family != AF_INET6) 274 continue; 275 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr; 276 if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) 277 return sin6->sin6_scope_id & 0xffff; 278 } 279 280 return -1; 281 } 282 283 int 284 in6_mask2len(mask) 285 struct in6_addr *mask; 286 { 287 int x, y; 288 289 for (x = 0; x < sizeof(*mask); x++) { 290 if (mask->s6_addr8[x] != 0xff) 291 break; 292 } 293 y = 0; 294 if (x < sizeof(*mask)) { 295 for (y = 0; y < 8; y++) { 296 if ((mask->s6_addr8[x] & (0x80 >> y)) == 0) 297 break; 298 } 299 } 300 return x * 8 + y; 301 } 302 303 void 304 in6_len2mask(mask, len) 305 struct in6_addr *mask; 306 int len; 307 { 308 int i; 309 310 bzero(mask, sizeof(*mask)); 311 for (i = 0; i < len / 8; i++) 312 mask->s6_addr8[i] = 0xff; 313 if (len % 8) 314 mask->s6_addr8[i] = (0xff00 >> (len % 8)) & 0xff; 315 } 316 317 #define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa)) 318 #define ia62ifa(ia6) (&((ia6)->ia_ifa)) 319 320 int 321 in6_control(so, cmd, data, ifp, p) 322 struct socket *so; 323 u_long cmd; 324 caddr_t data; 325 struct ifnet *ifp; 326 struct proc *p; 327 { 328 struct in6_ifreq *ifr = (struct in6_ifreq *)data; 329 struct in6_ifaddr *ia = NULL, *oia; 330 struct in6_aliasreq *ifra = (struct in6_aliasreq *)data; 331 struct sockaddr_in6 oldaddr; 332 #ifdef COMPAT_IN6IFIOCTL 333 struct sockaddr_in6 net; 334 #endif 335 int error = 0, hostIsNew, prefixIsNew; 336 int newifaddr; 337 int privileged; 338 339 privileged = 0; 340 if (p == NULL || !suser(p)) 341 privileged++; 342 343 /* 344 * xxx should prevent processes for link-local addresses? 345 */ 346 #if NGIF > 0 347 if (ifp && ifp->if_type == IFT_GIF) { 348 switch (cmd) { 349 case SIOCSIFPHYADDR_IN6: 350 if (!privileged) 351 return(EPERM); 352 /*fall through*/ 353 case SIOCGIFPSRCADDR_IN6: 354 case SIOCGIFPDSTADDR_IN6: 355 return gif_ioctl(ifp, cmd, data); 356 } 357 } 358 #endif 359 switch (cmd) { 360 case SIOCGETSGCNT_IN6: 361 case SIOCGETMIFCNT_IN6: 362 return (mrt6_ioctl(cmd, data)); 363 } 364 365 if (ifp == NULL) 366 return(EOPNOTSUPP); 367 368 switch (cmd) { 369 case SIOCSNDFLUSH_IN6: 370 case SIOCSPFXFLUSH_IN6: 371 case SIOCSRTRFLUSH_IN6: 372 case SIOCSDEFIFACE_IN6: 373 case SIOCSIFINFO_FLAGS: 374 if (!privileged) 375 return(EPERM); 376 /*fall through*/ 377 case SIOCGIFINFO_IN6: 378 case SIOCGDRLST_IN6: 379 case SIOCGPRLST_IN6: 380 case SIOCGNBRINFO_IN6: 381 case SIOCGDEFIFACE_IN6: 382 return(nd6_ioctl(cmd, data, ifp)); 383 } 384 385 switch (cmd) { 386 case SIOCSIFPREFIX_IN6: 387 case SIOCDIFPREFIX_IN6: 388 case SIOCAIFPREFIX_IN6: 389 case SIOCCIFPREFIX_IN6: 390 case SIOCSGIFPREFIX_IN6: 391 if (!privileged) 392 return(EPERM); 393 /*fall through*/ 394 case SIOCGIFPREFIX_IN6: 395 if (ip6_forwarding == 0) 396 return(EPERM); 397 return(in6_prefix_ioctl(so, cmd, data, ifp)); 398 } 399 400 switch(cmd) { 401 case SIOCSSCOPE6: 402 if (!privileged) 403 return(EPERM); 404 return(scope6_set(ifp, ifr->ifr_ifru.ifru_scope_id)); 405 break; 406 case SIOCGSCOPE6: 407 return(scope6_get(ifp, ifr->ifr_ifru.ifru_scope_id)); 408 break; 409 case SIOCGSCOPE6DEF: 410 return(scope6_get_default(ifr->ifr_ifru.ifru_scope_id)); 411 break; 412 } 413 414 switch (cmd) { 415 case SIOCALIFADDR: 416 case SIOCDLIFADDR: 417 if (!privileged) 418 return(EPERM); 419 /*fall through*/ 420 case SIOCGLIFADDR: 421 return in6_lifaddr_ioctl(so, cmd, data, ifp, p); 422 } 423 424 /* 425 * Find address for this interface, if it exists. 426 */ 427 if (ifra->ifra_addr.sin6_family == AF_INET6) { /* XXX */ 428 struct sockaddr_in6 *sa6 = 429 (struct sockaddr_in6 *)&ifra->ifra_addr; 430 431 if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr)) { 432 if (sa6->sin6_addr.s6_addr16[1] == 0) { 433 /* interface ID is not embedded by the user */ 434 sa6->sin6_addr.s6_addr16[1] = 435 htons(ifp->if_index); 436 } else if (sa6->sin6_addr.s6_addr16[1] != 437 htons(ifp->if_index)) { 438 return(EINVAL); /* ifid is contradict */ 439 } 440 if (sa6->sin6_scope_id) { 441 if (sa6->sin6_scope_id != 442 (u_int32_t)ifp->if_index) 443 return(EINVAL); 444 sa6->sin6_scope_id = 0; /* XXX: good way? */ 445 } 446 } 447 ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr); 448 } 449 450 switch (cmd) { 451 452 case SIOCDIFADDR_IN6: 453 /* 454 * for IPv4, we look for existing in6_ifaddr here to allow 455 * "ifconfig if0 delete" to remove first IPv4 address on the 456 * interface. For IPv6, as the spec allow multiple interface 457 * address from the day one, we consider "remove the first one" 458 * semantics to be not preferrable. 459 */ 460 if (ia == NULL) 461 return(EADDRNOTAVAIL); 462 /* FALLTHROUGH */ 463 case SIOCAIFADDR_IN6: 464 case SIOCSIFADDR_IN6: 465 #ifdef COMPAT_IN6IFIOCTL 466 case SIOCSIFDSTADDR_IN6: 467 case SIOCSIFNETMASK_IN6: 468 /* 469 * Since IPv6 allows a node to assign multiple addresses 470 * on a single interface, SIOCSIFxxx ioctls are not suitable 471 * and should be unused. 472 */ 473 #endif 474 if (ifra->ifra_addr.sin6_family != AF_INET6) 475 return(EAFNOSUPPORT); 476 if (!privileged) 477 return(EPERM); 478 if (ia == NULL) { 479 ia = (struct in6_ifaddr *) 480 malloc(sizeof(*ia), M_IFADDR, M_WAITOK); 481 if (ia == NULL) 482 return (ENOBUFS); 483 bzero((caddr_t)ia, sizeof(*ia)); 484 /* Initialize the address and masks */ 485 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr; 486 ia->ia_addr.sin6_family = AF_INET6; 487 ia->ia_addr.sin6_len = sizeof(ia->ia_addr); 488 #if 1 489 if (ifp->if_flags & IFF_POINTOPOINT) { 490 ia->ia_ifa.ifa_dstaddr 491 = (struct sockaddr *)&ia->ia_dstaddr; 492 ia->ia_dstaddr.sin6_family = AF_INET6; 493 ia->ia_dstaddr.sin6_len = sizeof(ia->ia_dstaddr); 494 } else { 495 ia->ia_ifa.ifa_dstaddr = NULL; 496 bzero(&ia->ia_dstaddr, sizeof(ia->ia_dstaddr)); 497 } 498 #else /* always initilize by NULL */ 499 ia->ia_ifa.ifa_dstaddr = NULL; 500 bzero(&ia->ia_dstaddr, sizeof(ia->ia_dstaddr)); 501 #endif 502 ia->ia_ifa.ifa_netmask 503 = (struct sockaddr *)&ia->ia_prefixmask; 504 505 ia->ia_ifp = ifp; 506 if ((oia = in6_ifaddr) != NULL) { 507 for ( ; oia->ia_next; oia = oia->ia_next) 508 continue; 509 oia->ia_next = ia; 510 } else 511 in6_ifaddr = ia; 512 /* gain a refcnt for the link from in6_ifaddr */ 513 ia->ia_ifa.ifa_refcnt++; 514 515 TAILQ_INSERT_TAIL(&ifp->if_addrlist, &ia->ia_ifa, 516 ifa_list); 517 /* gain another refcnt for the link from if_addrlist */ 518 ia->ia_ifa.ifa_refcnt++; 519 520 newifaddr = 1; 521 } else 522 newifaddr = 0; 523 524 if (cmd == SIOCAIFADDR_IN6) { 525 /* sanity for overflow - beware unsigned */ 526 struct in6_addrlifetime *lt; 527 lt = &ifra->ifra_lifetime; 528 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME 529 && lt->ia6t_vltime + time_second < time_second) { 530 return EINVAL; 531 } 532 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME 533 && lt->ia6t_pltime + time_second < time_second) { 534 return EINVAL; 535 } 536 } 537 break; 538 539 case SIOCGIFADDR_IN6: 540 /* This interface is basically deprecated. use SIOCGIFCONF. */ 541 /* fall through */ 542 case SIOCGIFAFLAG_IN6: 543 case SIOCGIFNETMASK_IN6: 544 case SIOCGIFDSTADDR_IN6: 545 case SIOCGIFALIFETIME_IN6: 546 /* must think again about its semantics */ 547 if (ia == NULL) 548 return(EADDRNOTAVAIL); 549 break; 550 case SIOCSIFALIFETIME_IN6: 551 { 552 struct in6_addrlifetime *lt; 553 554 if (!privileged) 555 return(EPERM); 556 if (ia == NULL) 557 return(EADDRNOTAVAIL); 558 /* sanity for overflow - beware unsigned */ 559 lt = &ifr->ifr_ifru.ifru_lifetime; 560 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME 561 && lt->ia6t_vltime + time_second < time_second) { 562 return EINVAL; 563 } 564 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME 565 && lt->ia6t_pltime + time_second < time_second) { 566 return EINVAL; 567 } 568 break; 569 } 570 } 571 572 switch (cmd) { 573 574 case SIOCGIFADDR_IN6: 575 ifr->ifr_addr = ia->ia_addr; 576 break; 577 578 case SIOCGIFDSTADDR_IN6: 579 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 580 return(EINVAL); 581 /* 582 * XXX: should we check if ifa_dstaddr is NULL and return 583 * an error? 584 */ 585 ifr->ifr_dstaddr = ia->ia_dstaddr; 586 break; 587 588 case SIOCGIFNETMASK_IN6: 589 ifr->ifr_addr = ia->ia_prefixmask; 590 break; 591 592 case SIOCGIFAFLAG_IN6: 593 ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags; 594 break; 595 596 case SIOCGIFSTAT_IN6: 597 if (ifp == NULL) 598 return EINVAL; 599 if (in6_ifstat == NULL || ifp->if_index >= in6_ifstatmax 600 || in6_ifstat[ifp->if_index] == NULL) { 601 /* return EAFNOSUPPORT? */ 602 bzero(&ifr->ifr_ifru.ifru_stat, 603 sizeof(ifr->ifr_ifru.ifru_stat)); 604 } else 605 ifr->ifr_ifru.ifru_stat = *in6_ifstat[ifp->if_index]; 606 break; 607 608 case SIOCGIFSTAT_ICMP6: 609 if (ifp == NULL) 610 return EINVAL; 611 if (icmp6_ifstat == NULL || ifp->if_index >= icmp6_ifstatmax || 612 icmp6_ifstat[ifp->if_index] == NULL) { 613 /* return EAFNOSUPPORT? */ 614 bzero(&ifr->ifr_ifru.ifru_stat, 615 sizeof(ifr->ifr_ifru.ifru_icmp6stat)); 616 } else 617 ifr->ifr_ifru.ifru_icmp6stat = 618 *icmp6_ifstat[ifp->if_index]; 619 break; 620 621 #ifdef COMPAT_IN6IFIOCTL /* should be unused */ 622 case SIOCSIFDSTADDR_IN6: 623 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 624 return(EINVAL); 625 oldaddr = ia->ia_dstaddr; 626 ia->ia_dstaddr = ifr->ifr_dstaddr; 627 628 /* link-local index check */ 629 if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_dstaddr.sin6_addr)) { 630 if (ia->ia_dstaddr.sin6_addr.s6_addr16[1] == 0) { 631 /* interface ID is not embedded by the user */ 632 ia->ia_dstaddr.sin6_addr.s6_addr16[1] 633 = htons(ifp->if_index); 634 } else if (ia->ia_dstaddr.sin6_addr.s6_addr16[1] != 635 htons(ifp->if_index)) { 636 ia->ia_dstaddr = oldaddr; 637 return(EINVAL); /* ifid is contradict */ 638 } 639 } 640 641 if (ifp->if_ioctl && (error = (ifp->if_ioctl) 642 (ifp, SIOCSIFDSTADDR, (caddr_t)ia))) { 643 ia->ia_dstaddr = oldaddr; 644 return(error); 645 } 646 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr; 647 if (ia->ia_flags & IFA_ROUTE) { 648 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr; 649 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST); 650 ia->ia_ifa.ifa_dstaddr = 651 (struct sockaddr *)&ia->ia_dstaddr; 652 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP); 653 } 654 break; 655 656 #endif 657 case SIOCGIFALIFETIME_IN6: 658 ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime; 659 break; 660 661 case SIOCSIFALIFETIME_IN6: 662 ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime; 663 /* for sanity */ 664 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) { 665 ia->ia6_lifetime.ia6t_expire = 666 time_second + ia->ia6_lifetime.ia6t_vltime; 667 } else 668 ia->ia6_lifetime.ia6t_expire = 0; 669 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) { 670 ia->ia6_lifetime.ia6t_preferred = 671 time_second + ia->ia6_lifetime.ia6t_pltime; 672 } else 673 ia->ia6_lifetime.ia6t_preferred = 0; 674 break; 675 676 case SIOCSIFADDR_IN6: 677 error = in6_ifinit(ifp, ia, &ifr->ifr_addr, 1); 678 #if 0 679 /* 680 * the code chokes if we are to assign multiple addresses with 681 * the same address prefix (rtinit() will return EEXIST, which 682 * is not fatal actually). we will get memory leak if we 683 * don't do it. 684 * -> we may want to hide EEXIST from rtinit(). 685 */ 686 undo: 687 if (error && newifaddr) { 688 TAILQ_REMOVE(&ifp->if_addrlist, &ia->ia_ifa, ifa_list); 689 /* release a refcnt for the link from if_addrlist */ 690 IFAFREE(&ia->ia_ifa); 691 692 oia = ia; 693 if (oia == (ia = in6_ifaddr)) 694 in6_ifaddr = ia->ia_next; 695 else { 696 while (ia->ia_next && (ia->ia_next != oia)) 697 ia = ia->ia_next; 698 if (ia->ia_next) 699 ia->ia_next = oia->ia_next; 700 else { 701 printf("Didn't unlink in6_ifaddr " 702 "from list\n"); 703 } 704 } 705 /* release another refcnt for the link from in6_ifaddr */ 706 IFAFREE(&oia->ia_ifa); 707 } 708 #endif 709 return error; 710 711 #ifdef COMPAT_IN6IFIOCTL /* XXX should be unused */ 712 case SIOCSIFNETMASK_IN6: 713 ia->ia_prefixmask = ifr->ifr_addr; 714 bzero(&net, sizeof(net)); 715 net.sin6_len = sizeof(struct sockaddr_in6); 716 net.sin6_family = AF_INET6; 717 net.sin6_port = htons(0); 718 net.sin6_flowinfo = htonl(0); 719 net.sin6_addr.s6_addr32[0] 720 = ia->ia_addr.sin6_addr.s6_addr32[0] & 721 ia->ia_prefixmask.sin6_addr.s6_addr32[0]; 722 net.sin6_addr.s6_addr32[1] 723 = ia->ia_addr.sin6_addr.s6_addr32[1] & 724 ia->ia_prefixmask.sin6_addr.s6_addr32[1]; 725 net.sin6_addr.s6_addr32[2] 726 = ia->ia_addr.sin6_addr.s6_addr32[2] & 727 ia->ia_prefixmask.sin6_addr.s6_addr32[2]; 728 net.sin6_addr.s6_addr32[3] 729 = ia->ia_addr.sin6_addr.s6_addr32[3] & 730 ia->ia_prefixmask.sin6_addr.s6_addr32[3]; 731 ia->ia_net = net; 732 break; 733 #endif 734 735 case SIOCAIFADDR_IN6: 736 prefixIsNew = 0; 737 hostIsNew = 1; 738 739 if (ifra->ifra_addr.sin6_len == 0) { 740 ifra->ifra_addr = ia->ia_addr; 741 hostIsNew = 0; 742 } else if (IN6_ARE_ADDR_EQUAL(&ifra->ifra_addr.sin6_addr, 743 &ia->ia_addr.sin6_addr)) 744 hostIsNew = 0; 745 746 /* Validate address families: */ 747 /* 748 * The destination address for a p2p link must have a family 749 * of AF_UNSPEC or AF_INET6. 750 */ 751 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 && 752 ifra->ifra_dstaddr.sin6_family != AF_INET6 && 753 ifra->ifra_dstaddr.sin6_family != AF_UNSPEC) 754 return(EAFNOSUPPORT); 755 /* 756 * The prefixmask must have a family of AF_UNSPEC or AF_INET6. 757 */ 758 if (ifra->ifra_prefixmask.sin6_family != AF_INET6 && 759 ifra->ifra_prefixmask.sin6_family != AF_UNSPEC) 760 return(EAFNOSUPPORT); 761 762 if (ifra->ifra_prefixmask.sin6_len) { 763 in6_ifscrub(ifp, ia); 764 ia->ia_prefixmask = ifra->ifra_prefixmask; 765 prefixIsNew = 1; 766 } 767 if ((ifp->if_flags & IFF_POINTOPOINT) && 768 (ifra->ifra_dstaddr.sin6_family == AF_INET6)) { 769 in6_ifscrub(ifp, ia); 770 oldaddr = ia->ia_dstaddr; 771 ia->ia_dstaddr = ifra->ifra_dstaddr; 772 /* link-local index check: should be a separate function? */ 773 if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_dstaddr.sin6_addr)) { 774 if (ia->ia_dstaddr.sin6_addr.s6_addr16[1] == 0) { 775 /* 776 * interface ID is not embedded by 777 * the user 778 */ 779 ia->ia_dstaddr.sin6_addr.s6_addr16[1] 780 = htons(ifp->if_index); 781 } else if (ia->ia_dstaddr.sin6_addr.s6_addr16[1] != 782 htons(ifp->if_index)) { 783 ia->ia_dstaddr = oldaddr; 784 return(EINVAL); /* ifid is contradict */ 785 } 786 } 787 prefixIsNew = 1; /* We lie; but effect's the same */ 788 } 789 if (hostIsNew || prefixIsNew) { 790 error = in6_ifinit(ifp, ia, &ifra->ifra_addr, 0); 791 #if 0 792 if (error) 793 goto undo; 794 #endif 795 } 796 if (hostIsNew && (ifp->if_flags & IFF_MULTICAST)) { 797 int error_local = 0; 798 799 /* 800 * join solicited multicast addr for new host id 801 */ 802 struct in6_addr llsol; 803 bzero(&llsol, sizeof(struct in6_addr)); 804 llsol.s6_addr16[0] = htons(0xff02); 805 llsol.s6_addr16[1] = htons(ifp->if_index); 806 llsol.s6_addr32[1] = 0; 807 llsol.s6_addr32[2] = htonl(1); 808 llsol.s6_addr32[3] = 809 ifra->ifra_addr.sin6_addr.s6_addr32[3]; 810 llsol.s6_addr8[12] = 0xff; 811 (void)in6_addmulti(&llsol, ifp, &error_local); 812 if (error == 0) 813 error = error_local; 814 } 815 816 ia->ia6_flags = ifra->ifra_flags; 817 ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /*safety*/ 818 819 ia->ia6_lifetime = ifra->ifra_lifetime; 820 /* for sanity */ 821 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) { 822 ia->ia6_lifetime.ia6t_expire = 823 time_second + ia->ia6_lifetime.ia6t_vltime; 824 } else 825 ia->ia6_lifetime.ia6t_expire = 0; 826 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) { 827 ia->ia6_lifetime.ia6t_preferred = 828 time_second + ia->ia6_lifetime.ia6t_pltime; 829 } else 830 ia->ia6_lifetime.ia6t_preferred = 0; 831 832 /* 833 * Perform DAD, if needed. 834 * XXX It may be of use, if we can administratively 835 * disable DAD. 836 */ 837 switch (ifp->if_type) { 838 case IFT_ARCNET: 839 case IFT_ETHER: 840 case IFT_FDDI: 841 #if 0 842 case IFT_ATM: 843 case IFT_SLIP: 844 case IFT_PPP: 845 #endif 846 { 847 ia->ia6_flags |= IN6_IFF_TENTATIVE; 848 nd6_dad_start((struct ifaddr *)ia, NULL); 849 } 850 break; 851 #ifdef IFT_DUMMY 852 case IFT_DUMMY: 853 #endif 854 case IFT_FAITH: 855 case IFT_GIF: 856 case IFT_LOOP: 857 default: 858 break; 859 } 860 861 if (hostIsNew) { 862 int iilen; 863 int error_local = 0; 864 865 iilen = (sizeof(ia->ia_prefixmask.sin6_addr) << 3) - 866 in6_mask2len(&ia->ia_prefixmask.sin6_addr); 867 error_local = in6_prefix_add_ifid(iilen, ia); 868 if (error == 0) 869 error = error_local; 870 } 871 872 return(error); 873 874 case SIOCDIFADDR_IN6: 875 in6_purgeaddr(&ia->ia_ifa, ifp); 876 break; 877 878 default: 879 if (ifp == NULL || ifp->if_ioctl == 0) 880 return(EOPNOTSUPP); 881 return((*ifp->if_ioctl)(ifp, cmd, data)); 882 } 883 return(0); 884 } 885 886 void 887 in6_purgeaddr(ifa, ifp) 888 struct ifaddr *ifa; 889 struct ifnet *ifp; 890 { 891 struct in6_ifaddr *oia, *ia = (void *) ifa; 892 int plen; 893 894 in6_ifscrub(ifp, ia); 895 896 if (ifp->if_flags & IFF_MULTICAST) { 897 /* 898 * delete solicited multicast addr for deleting host id 899 */ 900 struct in6_multi *in6m; 901 struct in6_addr llsol; 902 bzero(&llsol, sizeof(struct in6_addr)); 903 llsol.s6_addr16[0] = htons(0xff02); 904 llsol.s6_addr16[1] = htons(ifp->if_index); 905 llsol.s6_addr32[1] = 0; 906 llsol.s6_addr32[2] = htonl(1); 907 llsol.s6_addr32[3] = 908 ia->ia_addr.sin6_addr.s6_addr32[3]; 909 llsol.s6_addr8[12] = 0xff; 910 911 IN6_LOOKUP_MULTI(llsol, ifp, in6m); 912 if (in6m) 913 in6_delmulti(in6m); 914 } 915 916 TAILQ_REMOVE(&ifp->if_addrlist, &ia->ia_ifa, ifa_list); 917 /* release a refcnt for the link from if_addrlist */ 918 IFAFREE(&ia->ia_ifa); 919 920 oia = ia; 921 if (oia == (ia = in6_ifaddr)) 922 in6_ifaddr = ia->ia_next; 923 else { 924 while (ia->ia_next && (ia->ia_next != oia)) 925 ia = ia->ia_next; 926 if (ia->ia_next) 927 ia->ia_next = oia->ia_next; 928 else 929 printf("Didn't unlink in6_ifaddr from list\n"); 930 } 931 { 932 int iilen; 933 934 plen = in6_mask2len(&oia->ia_prefixmask.sin6_addr); 935 iilen = (sizeof(oia->ia_prefixmask.sin6_addr) << 3) - plen; 936 in6_prefix_remove_ifid(iilen, oia); 937 } 938 939 /* 940 * Check if we have another address that has the same prefix of 941 * the purged address. If we have one, reinstall the corresponding 942 * interface route. 943 */ 944 for (ia = in6_ifaddr; ia; ia = ia->ia_next) { 945 int e; 946 947 if (in6_are_prefix_equal(&ia->ia_addr.sin6_addr, 948 &oia->ia_addr.sin6_addr, plen)) { 949 if ((e = rtinit(&(ia->ia_ifa), (int)RTM_ADD, 950 ia->ia_flags)) == 0) { 951 ia->ia_flags |= IFA_ROUTE; 952 break; 953 } 954 else { 955 log(LOG_NOTICE, 956 "in6_purgeaddr: failed to add an interface" 957 " route for %s/%d on %s, errno = %d\n", 958 ip6_sprintf(&ia->ia_addr.sin6_addr), 959 plen, if_name(ia->ia_ifp), e); 960 /* still trying */ 961 } 962 } 963 } 964 965 /* release another refcnt for the link from in6_ifaddr */ 966 IFAFREE(&oia->ia_ifa); 967 } 968 969 /* 970 * SIOC[GAD]LIFADDR. 971 * SIOCGLIFADDR: get first address. (?) 972 * SIOCGLIFADDR with IFLR_PREFIX: 973 * get first address that matches the specified prefix. 974 * SIOCALIFADDR: add the specified address. 975 * SIOCALIFADDR with IFLR_PREFIX: 976 * add the specified prefix, filling hostid part from 977 * the first link-local address. prefixlen must be <= 64. 978 * SIOCDLIFADDR: delete the specified address. 979 * SIOCDLIFADDR with IFLR_PREFIX: 980 * delete the first address that matches the specified prefix. 981 * return values: 982 * EINVAL on invalid parameters 983 * EADDRNOTAVAIL on prefix match failed/specified address not found 984 * other values may be returned from in6_ioctl() 985 * 986 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64. 987 * this is to accomodate address naming scheme other than RFC2374, 988 * in the future. 989 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374 990 * address encoding scheme. (see figure on page 8) 991 */ 992 static int 993 in6_lifaddr_ioctl(so, cmd, data, ifp, p) 994 struct socket *so; 995 u_long cmd; 996 caddr_t data; 997 struct ifnet *ifp; 998 struct proc *p; 999 { 1000 struct if_laddrreq *iflr = (struct if_laddrreq *)data; 1001 struct ifaddr *ifa; 1002 struct sockaddr *sa; 1003 1004 /* sanity checks */ 1005 if (!data || !ifp) { 1006 panic("invalid argument to in6_lifaddr_ioctl"); 1007 /*NOTRECHED*/ 1008 } 1009 1010 switch (cmd) { 1011 case SIOCGLIFADDR: 1012 /* address must be specified on GET with IFLR_PREFIX */ 1013 if ((iflr->flags & IFLR_PREFIX) == 0) 1014 break; 1015 /*FALLTHROUGH*/ 1016 case SIOCALIFADDR: 1017 case SIOCDLIFADDR: 1018 /* address must be specified on ADD and DELETE */ 1019 sa = (struct sockaddr *)&iflr->addr; 1020 if (sa->sa_family != AF_INET6) 1021 return EINVAL; 1022 if (sa->sa_len != sizeof(struct sockaddr_in6)) 1023 return EINVAL; 1024 /* XXX need improvement */ 1025 sa = (struct sockaddr *)&iflr->dstaddr; 1026 if (sa->sa_family && sa->sa_family != AF_INET6) 1027 return EINVAL; 1028 if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6)) 1029 return EINVAL; 1030 break; 1031 default: /*shouldn't happen*/ 1032 #if 0 1033 panic("invalid cmd to in6_lifaddr_ioctl"); 1034 /*NOTREACHED*/ 1035 #else 1036 return EOPNOTSUPP; 1037 #endif 1038 } 1039 if (sizeof(struct in6_addr) * 8 < iflr->prefixlen) 1040 return EINVAL; 1041 1042 switch (cmd) { 1043 case SIOCALIFADDR: 1044 { 1045 struct in6_aliasreq ifra; 1046 struct in6_addr *hostid = NULL; 1047 int prefixlen; 1048 1049 if ((iflr->flags & IFLR_PREFIX) != 0) { 1050 struct sockaddr_in6 *sin6; 1051 1052 /* 1053 * hostid is to fill in the hostid part of the 1054 * address. hostid points to the first link-local 1055 * address attached to the interface. 1056 */ 1057 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0); 1058 if (!ifa) 1059 return EADDRNOTAVAIL; 1060 hostid = IFA_IN6(ifa); 1061 1062 /* prefixlen must be <= 64. */ 1063 if (64 < iflr->prefixlen) 1064 return EINVAL; 1065 prefixlen = iflr->prefixlen; 1066 1067 /* hostid part must be zero. */ 1068 sin6 = (struct sockaddr_in6 *)&iflr->addr; 1069 if (sin6->sin6_addr.s6_addr32[2] != 0 1070 || sin6->sin6_addr.s6_addr32[3] != 0) { 1071 return EINVAL; 1072 } 1073 } else 1074 prefixlen = iflr->prefixlen; 1075 1076 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */ 1077 bzero(&ifra, sizeof(ifra)); 1078 bcopy(iflr->iflr_name, ifra.ifra_name, 1079 sizeof(ifra.ifra_name)); 1080 1081 bcopy(&iflr->addr, &ifra.ifra_addr, 1082 ((struct sockaddr *)&iflr->addr)->sa_len); 1083 if (hostid) { 1084 /* fill in hostid part */ 1085 ifra.ifra_addr.sin6_addr.s6_addr32[2] = 1086 hostid->s6_addr32[2]; 1087 ifra.ifra_addr.sin6_addr.s6_addr32[3] = 1088 hostid->s6_addr32[3]; 1089 } 1090 1091 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /*XXX*/ 1092 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr, 1093 ((struct sockaddr *)&iflr->dstaddr)->sa_len); 1094 if (hostid) { 1095 ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] = 1096 hostid->s6_addr32[2]; 1097 ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] = 1098 hostid->s6_addr32[3]; 1099 } 1100 } 1101 1102 ifra.ifra_prefixmask.sin6_family = AF_INET6; 1103 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6); 1104 in6_len2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen); 1105 1106 ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX; 1107 return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, p); 1108 } 1109 case SIOCGLIFADDR: 1110 case SIOCDLIFADDR: 1111 { 1112 struct in6_ifaddr *ia; 1113 struct in6_addr mask, candidate, match; 1114 struct sockaddr_in6 *sin6; 1115 int cmp; 1116 1117 bzero(&mask, sizeof(mask)); 1118 if (iflr->flags & IFLR_PREFIX) { 1119 /* lookup a prefix rather than address. */ 1120 in6_len2mask(&mask, iflr->prefixlen); 1121 1122 sin6 = (struct sockaddr_in6 *)&iflr->addr; 1123 bcopy(&sin6->sin6_addr, &match, sizeof(match)); 1124 match.s6_addr32[0] &= mask.s6_addr32[0]; 1125 match.s6_addr32[1] &= mask.s6_addr32[1]; 1126 match.s6_addr32[2] &= mask.s6_addr32[2]; 1127 match.s6_addr32[3] &= mask.s6_addr32[3]; 1128 1129 /* if you set extra bits, that's wrong */ 1130 if (bcmp(&match, &sin6->sin6_addr, sizeof(match))) 1131 return EINVAL; 1132 1133 cmp = 1; 1134 } else { 1135 if (cmd == SIOCGLIFADDR) { 1136 /* on getting an address, take the 1st match */ 1137 cmp = 0; /*XXX*/ 1138 } else { 1139 /* on deleting an address, do exact match */ 1140 in6_len2mask(&mask, 128); 1141 sin6 = (struct sockaddr_in6 *)&iflr->addr; 1142 bcopy(&sin6->sin6_addr, &match, sizeof(match)); 1143 1144 cmp = 1; 1145 } 1146 } 1147 1148 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) 1149 { 1150 if (ifa->ifa_addr->sa_family != AF_INET6) 1151 continue; 1152 if (!cmp) 1153 break; 1154 bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate)); 1155 candidate.s6_addr32[0] &= mask.s6_addr32[0]; 1156 candidate.s6_addr32[1] &= mask.s6_addr32[1]; 1157 candidate.s6_addr32[2] &= mask.s6_addr32[2]; 1158 candidate.s6_addr32[3] &= mask.s6_addr32[3]; 1159 if (IN6_ARE_ADDR_EQUAL(&candidate, &match)) 1160 break; 1161 } 1162 if (!ifa) 1163 return EADDRNOTAVAIL; 1164 ia = ifa2ia6(ifa); 1165 1166 if (cmd == SIOCGLIFADDR) { 1167 /* fill in the if_laddrreq structure */ 1168 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len); 1169 1170 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 1171 bcopy(&ia->ia_dstaddr, &iflr->dstaddr, 1172 ia->ia_dstaddr.sin6_len); 1173 } else 1174 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr)); 1175 1176 iflr->prefixlen = 1177 in6_mask2len(&ia->ia_prefixmask.sin6_addr); 1178 1179 iflr->flags = ia->ia6_flags; /*XXX*/ 1180 1181 return 0; 1182 } else { 1183 struct in6_aliasreq ifra; 1184 1185 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */ 1186 bzero(&ifra, sizeof(ifra)); 1187 bcopy(iflr->iflr_name, ifra.ifra_name, 1188 sizeof(ifra.ifra_name)); 1189 1190 bcopy(&ia->ia_addr, &ifra.ifra_addr, 1191 ia->ia_addr.sin6_len); 1192 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 1193 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr, 1194 ia->ia_dstaddr.sin6_len); 1195 } else { 1196 bzero(&ifra.ifra_dstaddr, 1197 sizeof(ifra.ifra_dstaddr)); 1198 } 1199 bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr, 1200 ia->ia_prefixmask.sin6_len); 1201 1202 ifra.ifra_flags = ia->ia6_flags; 1203 return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra, 1204 ifp, p); 1205 } 1206 } 1207 } 1208 1209 return EOPNOTSUPP; /*just for safety*/ 1210 } 1211 1212 /* 1213 * Delete any existing route for an interface. 1214 */ 1215 void 1216 in6_ifscrub(ifp, ia) 1217 register struct ifnet *ifp; 1218 register struct in6_ifaddr *ia; 1219 { 1220 if ((ia->ia_flags & IFA_ROUTE) == 0) 1221 return; 1222 if (ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) 1223 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST); 1224 else 1225 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0); 1226 ia->ia_flags &= ~IFA_ROUTE; 1227 1228 /* Remove ownaddr's loopback rtentry, if it exists. */ 1229 in6_ifremloop(&(ia->ia_ifa)); 1230 } 1231 1232 /* 1233 * Initialize an interface's intetnet6 address 1234 * and routing table entry. 1235 */ 1236 int 1237 in6_ifinit(ifp, ia, sin6, scrub) 1238 struct ifnet *ifp; 1239 struct in6_ifaddr *ia; 1240 struct sockaddr_in6 *sin6; 1241 int scrub; 1242 { 1243 struct sockaddr_in6 oldaddr; 1244 int error, flags = RTF_UP; 1245 int s = splimp(); 1246 1247 oldaddr = ia->ia_addr; 1248 ia->ia_addr = *sin6; 1249 /* 1250 * Give the interface a chance to initialize 1251 * if this is its first address, 1252 * and to validate the address if necessary. 1253 */ 1254 if (ifp->if_ioctl && 1255 (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) { 1256 splx(s); 1257 ia->ia_addr = oldaddr; 1258 return(error); 1259 } 1260 1261 switch (ifp->if_type) { 1262 case IFT_ARCNET: 1263 case IFT_ETHER: 1264 case IFT_FDDI: 1265 ia->ia_ifa.ifa_rtrequest = nd6_rtrequest; 1266 ia->ia_ifa.ifa_flags |= RTF_CLONING; 1267 break; 1268 case IFT_PPP: 1269 ia->ia_ifa.ifa_rtrequest = nd6_p2p_rtrequest; 1270 ia->ia_ifa.ifa_flags |= RTF_CLONING; 1271 break; 1272 } 1273 1274 splx(s); 1275 if (scrub) { 1276 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr; 1277 in6_ifscrub(ifp, ia); 1278 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr; 1279 } 1280 /* xxx 1281 * in_socktrim 1282 */ 1283 /* 1284 * Add route for the network. 1285 */ 1286 ia->ia_ifa.ifa_metric = ifp->if_metric; 1287 if (ifp->if_flags & IFF_LOOPBACK) { 1288 ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr; 1289 flags |= RTF_HOST; 1290 } else if (ifp->if_flags & IFF_POINTOPOINT) { 1291 if (ia->ia_dstaddr.sin6_family != AF_INET6) 1292 return(0); 1293 flags |= RTF_HOST; 1294 } 1295 if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD, flags)) == 0) 1296 ia->ia_flags |= IFA_ROUTE; 1297 /* XXX check if the subnet route points to the same interface */ 1298 if (error == EEXIST) 1299 error = 0; 1300 1301 /* Add ownaddr as loopback rtentry, if necessary(ex. on p2p link). */ 1302 in6_ifaddloop(&(ia->ia_ifa)); 1303 1304 return(error); 1305 } 1306 1307 /* 1308 * Add an address to the list of IP6 multicast addresses for a 1309 * given interface. 1310 */ 1311 struct in6_multi * 1312 in6_addmulti(maddr6, ifp, errorp) 1313 register struct in6_addr *maddr6; 1314 register struct ifnet *ifp; 1315 int *errorp; 1316 { 1317 struct in6_multi *in6m; 1318 struct sockaddr_in6 sin6; 1319 struct ifmultiaddr *ifma; 1320 int s = splnet(); 1321 1322 *errorp = 0; 1323 1324 /* 1325 * Call generic routine to add membership or increment 1326 * refcount. It wants addresses in the form of a sockaddr, 1327 * so we build one here (being careful to zero the unused bytes). 1328 */ 1329 bzero(&sin6, sizeof sin6); 1330 sin6.sin6_family = AF_INET6; 1331 sin6.sin6_len = sizeof sin6; 1332 sin6.sin6_addr = *maddr6; 1333 *errorp = if_addmulti(ifp, (struct sockaddr *)&sin6, &ifma); 1334 if (*errorp) { 1335 splx(s); 1336 return 0; 1337 } 1338 1339 /* 1340 * If ifma->ifma_protospec is null, then if_addmulti() created 1341 * a new record. Otherwise, we are done. 1342 */ 1343 if (ifma->ifma_protospec != 0) 1344 return ifma->ifma_protospec; 1345 1346 /* XXX - if_addmulti uses M_WAITOK. Can this really be called 1347 at interrupt time? If so, need to fix if_addmulti. XXX */ 1348 in6m = (struct in6_multi *)malloc(sizeof(*in6m), M_IPMADDR, M_NOWAIT); 1349 if (in6m == NULL) { 1350 splx(s); 1351 return (NULL); 1352 } 1353 1354 bzero(in6m, sizeof *in6m); 1355 in6m->in6m_addr = *maddr6; 1356 in6m->in6m_ifp = ifp; 1357 in6m->in6m_ifma = ifma; 1358 ifma->ifma_protospec = in6m; 1359 LIST_INSERT_HEAD(&in6_multihead, in6m, in6m_entry); 1360 1361 /* 1362 * Let MLD6 know that we have joined a new IP6 multicast 1363 * group. 1364 */ 1365 mld6_start_listening(in6m); 1366 splx(s); 1367 return(in6m); 1368 } 1369 1370 /* 1371 * Delete a multicast address record. 1372 */ 1373 void 1374 in6_delmulti(in6m) 1375 struct in6_multi *in6m; 1376 { 1377 struct ifmultiaddr *ifma = in6m->in6m_ifma; 1378 int s = splnet(); 1379 1380 if (ifma->ifma_refcount == 1) { 1381 /* 1382 * No remaining claims to this record; let MLD6 know 1383 * that we are leaving the multicast group. 1384 */ 1385 mld6_stop_listening(in6m); 1386 ifma->ifma_protospec = 0; 1387 LIST_REMOVE(in6m, in6m_entry); 1388 free(in6m, M_IPMADDR); 1389 } 1390 /* XXX - should be separate API for when we have an ifma? */ 1391 if_delmulti(ifma->ifma_ifp, ifma->ifma_addr); 1392 splx(s); 1393 } 1394 1395 /* 1396 * Find an IPv6 interface link-local address specific to an interface. 1397 */ 1398 struct in6_ifaddr * 1399 in6ifa_ifpforlinklocal(ifp, ignoreflags) 1400 struct ifnet *ifp; 1401 int ignoreflags; 1402 { 1403 register struct ifaddr *ifa; 1404 1405 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) 1406 { 1407 if (ifa->ifa_addr == NULL) 1408 continue; /* just for safety */ 1409 if (ifa->ifa_addr->sa_family != AF_INET6) 1410 continue; 1411 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) { 1412 if ((((struct in6_ifaddr *)ifa)->ia6_flags & 1413 ignoreflags) != 0) 1414 continue; 1415 break; 1416 } 1417 } 1418 1419 return((struct in6_ifaddr *)ifa); 1420 } 1421 1422 1423 /* 1424 * find the internet address corresponding to a given interface and address. 1425 */ 1426 struct in6_ifaddr * 1427 in6ifa_ifpwithaddr(ifp, addr) 1428 struct ifnet *ifp; 1429 struct in6_addr *addr; 1430 { 1431 register struct ifaddr *ifa; 1432 1433 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) 1434 { 1435 if (ifa->ifa_addr == NULL) 1436 continue; /* just for safety */ 1437 if (ifa->ifa_addr->sa_family != AF_INET6) 1438 continue; 1439 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa))) 1440 break; 1441 } 1442 1443 return((struct in6_ifaddr *)ifa); 1444 } 1445 1446 /* 1447 * Convert IP6 address to printable (loggable) representation. 1448 */ 1449 static char digits[] = "0123456789abcdef"; 1450 static int ip6round = 0; 1451 char * 1452 ip6_sprintf(addr) 1453 register struct in6_addr *addr; 1454 { 1455 static char ip6buf[8][48]; 1456 register int i; 1457 register char *cp; 1458 register u_short *a = (u_short *)addr; 1459 register u_char *d; 1460 int dcolon = 0; 1461 1462 ip6round = (ip6round + 1) & 7; 1463 cp = ip6buf[ip6round]; 1464 1465 for (i = 0; i < 8; i++) { 1466 if (dcolon == 1) { 1467 if (*a == 0) { 1468 if (i == 7) 1469 *cp++ = ':'; 1470 a++; 1471 continue; 1472 } else 1473 dcolon = 2; 1474 } 1475 if (*a == 0) { 1476 if (dcolon == 0 && *(a + 1) == 0) { 1477 if (i == 0) 1478 *cp++ = ':'; 1479 *cp++ = ':'; 1480 dcolon = 1; 1481 } else { 1482 *cp++ = '0'; 1483 *cp++ = ':'; 1484 } 1485 a++; 1486 continue; 1487 } 1488 d = (u_char *)a; 1489 *cp++ = digits[*d >> 4]; 1490 *cp++ = digits[*d++ & 0xf]; 1491 *cp++ = digits[*d >> 4]; 1492 *cp++ = digits[*d & 0xf]; 1493 *cp++ = ':'; 1494 a++; 1495 } 1496 *--cp = 0; 1497 return(ip6buf[ip6round]); 1498 } 1499 1500 int 1501 in6_localaddr(in6) 1502 struct in6_addr *in6; 1503 { 1504 struct in6_ifaddr *ia; 1505 1506 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6)) 1507 return 1; 1508 1509 for (ia = in6_ifaddr; ia; ia = ia->ia_next) 1510 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr, 1511 &ia->ia_prefixmask.sin6_addr)) 1512 return 1; 1513 1514 return (0); 1515 } 1516 1517 /* 1518 * return length of part which dst and src are equal 1519 * hard coding... 1520 */ 1521 int 1522 in6_matchlen(src, dst) 1523 struct in6_addr *src, *dst; 1524 { 1525 int match = 0; 1526 u_char *s = (u_char *)src, *d = (u_char *)dst; 1527 u_char *lim = s + 16, r; 1528 1529 while (s < lim) 1530 if ((r = (*d++ ^ *s++)) != 0) { 1531 while (r < 128) { 1532 match++; 1533 r <<= 1; 1534 } 1535 break; 1536 } else 1537 match += 8; 1538 return match; 1539 } 1540 1541 /* XXX: to be scope conscious */ 1542 int 1543 in6_are_prefix_equal(p1, p2, len) 1544 struct in6_addr *p1, *p2; 1545 int len; 1546 { 1547 int bytelen, bitlen; 1548 1549 /* sanity check */ 1550 if (0 > len || len > 128) { 1551 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n", 1552 len); 1553 return(0); 1554 } 1555 1556 bytelen = len / 8; 1557 bitlen = len % 8; 1558 1559 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen)) 1560 return(0); 1561 if (p1->s6_addr[bytelen] >> (8 - bitlen) != 1562 p2->s6_addr[bytelen] >> (8 - bitlen)) 1563 return(0); 1564 1565 return(1); 1566 } 1567 1568 void 1569 in6_prefixlen2mask(maskp, len) 1570 struct in6_addr *maskp; 1571 int len; 1572 { 1573 u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff}; 1574 int bytelen, bitlen, i; 1575 1576 /* sanity check */ 1577 if (0 > len || len > 128) { 1578 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n", 1579 len); 1580 return; 1581 } 1582 1583 bzero(maskp, sizeof(*maskp)); 1584 bytelen = len / 8; 1585 bitlen = len % 8; 1586 for (i = 0; i < bytelen; i++) 1587 maskp->s6_addr[i] = 0xff; 1588 if (bitlen) 1589 maskp->s6_addr[bytelen] = maskarray[bitlen - 1]; 1590 } 1591 1592 /* 1593 * return the best address out of the same scope 1594 */ 1595 struct in6_ifaddr * 1596 in6_ifawithscope(oifp, dst) 1597 register struct ifnet *oifp; 1598 register struct in6_addr *dst; 1599 { 1600 int dst_scope = in6_addrscope(dst), src_scope, best_scope = 0; 1601 int blen = -1; 1602 struct ifaddr *ifa; 1603 struct ifnet *ifp; 1604 struct in6_ifaddr *ifa_best = NULL; 1605 1606 if (oifp == NULL) { 1607 printf("in6_ifawithscope: output interface is not specified\n"); 1608 return(NULL); 1609 } 1610 1611 /* 1612 * We search for all addresses on all interfaces from the beginning. 1613 * Comparing an interface with the outgoing interface will be done 1614 * only at the final stage of tiebreaking. 1615 */ 1616 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) 1617 { 1618 /* 1619 * We can never take an address that breaks the scope zone 1620 * of the destination. 1621 */ 1622 if (in6_addr2scopeid(ifp, dst) != in6_addr2scopeid(oifp, dst)) 1623 continue; 1624 1625 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) 1626 { 1627 int tlen = -1, dscopecmp, bscopecmp, matchcmp; 1628 1629 if (ifa->ifa_addr->sa_family != AF_INET6) 1630 continue; 1631 1632 src_scope = in6_addrscope(IFA_IN6(ifa)); 1633 1634 /* 1635 * Don't use an address before completing DAD 1636 * nor a duplicated address. 1637 */ 1638 if (((struct in6_ifaddr *)ifa)->ia6_flags & 1639 IN6_IFF_NOTREADY) 1640 continue; 1641 1642 /* XXX: is there any case to allow anycasts? */ 1643 if (((struct in6_ifaddr *)ifa)->ia6_flags & 1644 IN6_IFF_ANYCAST) 1645 continue; 1646 1647 if (((struct in6_ifaddr *)ifa)->ia6_flags & 1648 IN6_IFF_DETACHED) 1649 continue; 1650 1651 /* 1652 * If this is the first address we find, 1653 * keep it anyway. 1654 */ 1655 if (ifa_best == NULL) 1656 goto replace; 1657 1658 /* 1659 * ifa_best is never NULL beyond this line except 1660 * within the block labeled "replace". 1661 */ 1662 1663 /* 1664 * If ifa_best has a smaller scope than dst and 1665 * the current address has a larger one than 1666 * (or equal to) dst, always replace ifa_best. 1667 * Also, if the current address has a smaller scope 1668 * than dst, ignore it unless ifa_best also has a 1669 * smaller scope. 1670 */ 1671 if (IN6_ARE_SCOPE_CMP(best_scope, dst_scope) < 0 && 1672 IN6_ARE_SCOPE_CMP(src_scope, dst_scope) >= 0) 1673 goto replace; 1674 if (IN6_ARE_SCOPE_CMP(src_scope, dst_scope) < 0 && 1675 IN6_ARE_SCOPE_CMP(best_scope, dst_scope) >= 0) 1676 continue; 1677 1678 /* 1679 * A deprecated address SHOULD NOT be used in new 1680 * communications if an alternate (non-deprecated) 1681 * address is available and has sufficient scope. 1682 * RFC 2462, Section 5.5.4. 1683 */ 1684 if (((struct in6_ifaddr *)ifa)->ia6_flags & 1685 IN6_IFF_DEPRECATED) { 1686 /* 1687 * Ignore any deprecated addresses if 1688 * specified by configuration. 1689 */ 1690 if (!ip6_use_deprecated) 1691 continue; 1692 1693 /* 1694 * If we have already found a non-deprecated 1695 * candidate, just ignore deprecated addresses. 1696 */ 1697 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) 1698 == 0) 1699 continue; 1700 } 1701 1702 /* 1703 * A non-deprecated address is always preferred 1704 * to a deprecated one regardless of scopes and 1705 * address matching. 1706 */ 1707 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) && 1708 (((struct in6_ifaddr *)ifa)->ia6_flags & 1709 IN6_IFF_DEPRECATED) == 0) 1710 goto replace; 1711 1712 /* 1713 * At this point, we have two cases: 1714 * 1. we are looking at a non-deprecated address, 1715 * and ifa_best is also non-deprecated. 1716 * 2. we are looking at a deprecated address, 1717 * and ifa_best is also deprecated. 1718 * Also, we do not have to consider a case where 1719 * the scope of if_best is larger(smaller) than dst and 1720 * the scope of the current address is smaller(larger) 1721 * than dst. Such a case has already been covered. 1722 * Tiebreaking is done according to the following 1723 * items: 1724 * - the scope comparison between the address and 1725 * dst (dscopecmp) 1726 * - the scope comparison between the address and 1727 * ifa_best (bscopecmp) 1728 * - if the address match dst longer than ifa_best 1729 * (matchcmp) 1730 * - if the address is on the outgoing I/F (outI/F) 1731 * 1732 * Roughly speaking, the selection policy is 1733 * - the most important item is scope. The same scope 1734 * is best. Then search for a larger scope. 1735 * Smaller scopes are the last resort. 1736 * - A deprecated address is chosen only when we have 1737 * no address that has an enough scope, but is 1738 * prefered to any addresses of smaller scopes. 1739 * - Longest address match against dst is considered 1740 * only for addresses that has the same scope of dst. 1741 * - If there is no other reasons to choose one, 1742 * addresses on the outgoing I/F are preferred. 1743 * 1744 * The precise decision table is as follows: 1745 * dscopecmp bscopecmp matchcmp outI/F | replace? 1746 * !equal equal N/A Yes | Yes (1) 1747 * !equal equal N/A No | No (2) 1748 * larger larger N/A N/A | No (3) 1749 * larger smaller N/A N/A | Yes (4) 1750 * smaller larger N/A N/A | Yes (5) 1751 * smaller smaller N/A N/A | No (6) 1752 * equal smaller N/A N/A | Yes (7) 1753 * equal larger (already done) 1754 * equal equal larger N/A | Yes (8) 1755 * equal equal smaller N/A | No (9) 1756 * equal equal equal Yes | Yes (a) 1757 * eaual eqaul equal No | No (b) 1758 */ 1759 dscopecmp = IN6_ARE_SCOPE_CMP(src_scope, dst_scope); 1760 bscopecmp = IN6_ARE_SCOPE_CMP(src_scope, best_scope); 1761 1762 if (dscopecmp && bscopecmp == 0) { 1763 if (oifp == ifp) /* (1) */ 1764 goto replace; 1765 continue; /* (2) */ 1766 } 1767 if (dscopecmp > 0) { 1768 if (bscopecmp > 0) /* (3) */ 1769 continue; 1770 goto replace; /* (4) */ 1771 } 1772 if (dscopecmp < 0) { 1773 if (bscopecmp > 0) /* (5) */ 1774 goto replace; 1775 continue; /* (6) */ 1776 } 1777 1778 /* now dscopecmp must be 0 */ 1779 if (bscopecmp < 0) 1780 goto replace; /* (7) */ 1781 1782 /* 1783 * At last both dscopecmp and bscopecmp must be 0. 1784 * We need address matching against dst for 1785 * tiebreaking. 1786 */ 1787 tlen = in6_matchlen(IFA_IN6(ifa), dst); 1788 matchcmp = tlen - blen; 1789 if (matchcmp > 0) /* (8) */ 1790 goto replace; 1791 if (matchcmp < 0) /* (9) */ 1792 continue; 1793 if (oifp == ifp) /* (a) */ 1794 goto replace; 1795 continue; /* (b) */ 1796 1797 replace: 1798 ifa_best = (struct in6_ifaddr *)ifa; 1799 blen = tlen >= 0 ? tlen : 1800 in6_matchlen(IFA_IN6(ifa), dst); 1801 best_scope = in6_addrscope(&ifa_best->ia_addr.sin6_addr); 1802 } 1803 } 1804 1805 /* count statistics for future improvements */ 1806 if (ifa_best == NULL) 1807 ip6stat.ip6s_sources_none++; 1808 else { 1809 if (oifp == ifa_best->ia_ifp) 1810 ip6stat.ip6s_sources_sameif[best_scope]++; 1811 else 1812 ip6stat.ip6s_sources_otherif[best_scope]++; 1813 1814 if (best_scope == dst_scope) 1815 ip6stat.ip6s_sources_samescope[best_scope]++; 1816 else 1817 ip6stat.ip6s_sources_otherscope[best_scope]++; 1818 1819 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) != 0) 1820 ip6stat.ip6s_sources_deprecated[best_scope]++; 1821 } 1822 1823 return(ifa_best); 1824 } 1825 1826 /* 1827 * return the best address out of the same scope. if no address was 1828 * found, return the first valid address from designated IF. 1829 */ 1830 struct in6_ifaddr * 1831 in6_ifawithifp(ifp, dst) 1832 register struct ifnet *ifp; 1833 register struct in6_addr *dst; 1834 { 1835 int dst_scope = in6_addrscope(dst), blen = -1, tlen; 1836 struct ifaddr *ifa; 1837 struct in6_ifaddr *besta = 0; 1838 struct in6_ifaddr *dep[2]; /*last-resort: deprecated*/ 1839 1840 dep[0] = dep[1] = NULL; 1841 1842 /* 1843 * We first look for addresses in the same scope. 1844 * If there is one, return it. 1845 * If two or more, return one which matches the dst longest. 1846 * If none, return one of global addresses assigned other ifs. 1847 */ 1848 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) 1849 { 1850 if (ifa->ifa_addr->sa_family != AF_INET6) 1851 continue; 1852 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST) 1853 continue; /* XXX: is there any case to allow anycast? */ 1854 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY) 1855 continue; /* don't use this interface */ 1856 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED) 1857 continue; 1858 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) { 1859 if (ip6_use_deprecated) 1860 dep[0] = (struct in6_ifaddr *)ifa; 1861 continue; 1862 } 1863 1864 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) { 1865 /* 1866 * call in6_matchlen() as few as possible 1867 */ 1868 if (besta) { 1869 if (blen == -1) 1870 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst); 1871 tlen = in6_matchlen(IFA_IN6(ifa), dst); 1872 if (tlen > blen) { 1873 blen = tlen; 1874 besta = (struct in6_ifaddr *)ifa; 1875 } 1876 } else 1877 besta = (struct in6_ifaddr *)ifa; 1878 } 1879 } 1880 if (besta) 1881 return(besta); 1882 1883 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) 1884 { 1885 if (ifa->ifa_addr->sa_family != AF_INET6) 1886 continue; 1887 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST) 1888 continue; /* XXX: is there any case to allow anycast? */ 1889 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY) 1890 continue; /* don't use this interface */ 1891 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED) 1892 continue; 1893 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) { 1894 if (ip6_use_deprecated) 1895 dep[1] = (struct in6_ifaddr *)ifa; 1896 continue; 1897 } 1898 1899 return (struct in6_ifaddr *)ifa; 1900 } 1901 1902 /* use the last-resort values, that are, deprecated addresses */ 1903 if (dep[0]) 1904 return dep[0]; 1905 if (dep[1]) 1906 return dep[1]; 1907 1908 return NULL; 1909 } 1910 1911 /* 1912 * perform DAD when interface becomes IFF_UP. 1913 */ 1914 void 1915 in6_if_up(ifp) 1916 struct ifnet *ifp; 1917 { 1918 struct ifaddr *ifa; 1919 struct in6_ifaddr *ia; 1920 int dad_delay; /* delay ticks before DAD output */ 1921 1922 /* 1923 * special cases, like 6to4, are handled in in6_ifattach 1924 */ 1925 in6_ifattach(ifp, NULL); 1926 1927 dad_delay = 0; 1928 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) 1929 { 1930 if (ifa->ifa_addr->sa_family != AF_INET6) 1931 continue; 1932 ia = (struct in6_ifaddr *)ifa; 1933 if (ia->ia6_flags & IN6_IFF_TENTATIVE) 1934 nd6_dad_start(ifa, &dad_delay); 1935 } 1936 } 1937 1938 /* 1939 * Calculate max IPv6 MTU through all the interfaces and store it 1940 * to in6_maxmtu. 1941 */ 1942 void 1943 in6_setmaxmtu() 1944 { 1945 unsigned long maxmtu = 0; 1946 struct ifnet *ifp; 1947 1948 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) 1949 { 1950 if ((ifp->if_flags & IFF_LOOPBACK) == 0 && 1951 nd_ifinfo[ifp->if_index].linkmtu > maxmtu) 1952 maxmtu = nd_ifinfo[ifp->if_index].linkmtu; 1953 } 1954 if (maxmtu) /* update only when maxmtu is positive */ 1955 in6_maxmtu = maxmtu; 1956 } 1957 1958 /* 1959 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be 1960 * v4 mapped addr or v4 compat addr 1961 */ 1962 void 1963 in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6) 1964 { 1965 bzero(sin, sizeof(*sin)); 1966 sin->sin_len = sizeof(struct sockaddr_in); 1967 sin->sin_family = AF_INET; 1968 sin->sin_port = sin6->sin6_port; 1969 sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3]; 1970 } 1971 1972 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */ 1973 void 1974 in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6) 1975 { 1976 bzero(sin6, sizeof(*sin6)); 1977 sin6->sin6_len = sizeof(struct sockaddr_in6); 1978 sin6->sin6_family = AF_INET6; 1979 sin6->sin6_port = sin->sin_port; 1980 sin6->sin6_addr.s6_addr32[0] = 0; 1981 sin6->sin6_addr.s6_addr32[1] = 0; 1982 sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP; 1983 sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr; 1984 } 1985 1986 /* Convert sockaddr_in6 into sockaddr_in. */ 1987 void 1988 in6_sin6_2_sin_in_sock(struct sockaddr *nam) 1989 { 1990 struct sockaddr_in *sin_p; 1991 struct sockaddr_in6 sin6; 1992 1993 /* 1994 * Save original sockaddr_in6 addr and convert it 1995 * to sockaddr_in. 1996 */ 1997 sin6 = *(struct sockaddr_in6 *)nam; 1998 sin_p = (struct sockaddr_in *)nam; 1999 in6_sin6_2_sin(sin_p, &sin6); 2000 } 2001 2002 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */ 2003 void 2004 in6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam) 2005 { 2006 struct sockaddr_in *sin_p; 2007 struct sockaddr_in6 *sin6_p; 2008 2009 MALLOC(sin6_p, struct sockaddr_in6 *, sizeof *sin6_p, M_SONAME, 2010 M_WAITOK); 2011 sin_p = (struct sockaddr_in *)*nam; 2012 in6_sin_2_v4mapsin6(sin_p, sin6_p); 2013 FREE(*nam, M_SONAME); 2014 *nam = (struct sockaddr *)sin6_p; 2015 } 2016