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 * make sure to initialize ND6 information. this is to 834 * workaround issues with interfaces with IPv6 addresses, 835 * which have never brought # up. we are assuming that it is 836 * safe to nd6_ifattach multiple times. 837 */ 838 nd6_ifattach(ifp); 839 840 /* 841 * Perform DAD, if needed. 842 * XXX It may be of use, if we can administratively 843 * disable DAD. 844 */ 845 switch (ifp->if_type) { 846 case IFT_ARCNET: 847 case IFT_ETHER: 848 case IFT_FDDI: 849 #if 0 850 case IFT_ATM: 851 case IFT_SLIP: 852 case IFT_PPP: 853 #endif 854 { 855 ia->ia6_flags |= IN6_IFF_TENTATIVE; 856 nd6_dad_start((struct ifaddr *)ia, NULL); 857 } 858 break; 859 #ifdef IFT_DUMMY 860 case IFT_DUMMY: 861 #endif 862 case IFT_FAITH: 863 case IFT_GIF: 864 case IFT_LOOP: 865 default: 866 break; 867 } 868 869 if (hostIsNew) { 870 int iilen; 871 int error_local = 0; 872 873 iilen = (sizeof(ia->ia_prefixmask.sin6_addr) << 3) - 874 in6_mask2len(&ia->ia_prefixmask.sin6_addr); 875 error_local = in6_prefix_add_ifid(iilen, ia); 876 if (error == 0) 877 error = error_local; 878 } 879 880 return(error); 881 882 case SIOCDIFADDR_IN6: 883 in6_purgeaddr(&ia->ia_ifa, ifp); 884 break; 885 886 default: 887 if (ifp == NULL || ifp->if_ioctl == 0) 888 return(EOPNOTSUPP); 889 return((*ifp->if_ioctl)(ifp, cmd, data)); 890 } 891 return(0); 892 } 893 894 void 895 in6_purgeaddr(ifa, ifp) 896 struct ifaddr *ifa; 897 struct ifnet *ifp; 898 { 899 struct in6_ifaddr *oia, *ia = (void *) ifa; 900 int plen; 901 902 in6_ifscrub(ifp, ia); 903 904 if (ifp->if_flags & IFF_MULTICAST) { 905 /* 906 * delete solicited multicast addr for deleting host id 907 */ 908 struct in6_multi *in6m; 909 struct in6_addr llsol; 910 bzero(&llsol, sizeof(struct in6_addr)); 911 llsol.s6_addr16[0] = htons(0xff02); 912 llsol.s6_addr16[1] = htons(ifp->if_index); 913 llsol.s6_addr32[1] = 0; 914 llsol.s6_addr32[2] = htonl(1); 915 llsol.s6_addr32[3] = 916 ia->ia_addr.sin6_addr.s6_addr32[3]; 917 llsol.s6_addr8[12] = 0xff; 918 919 IN6_LOOKUP_MULTI(llsol, ifp, in6m); 920 if (in6m) 921 in6_delmulti(in6m); 922 } 923 924 TAILQ_REMOVE(&ifp->if_addrlist, &ia->ia_ifa, ifa_list); 925 /* release a refcnt for the link from if_addrlist */ 926 IFAFREE(&ia->ia_ifa); 927 928 oia = ia; 929 if (oia == (ia = in6_ifaddr)) 930 in6_ifaddr = ia->ia_next; 931 else { 932 while (ia->ia_next && (ia->ia_next != oia)) 933 ia = ia->ia_next; 934 if (ia->ia_next) 935 ia->ia_next = oia->ia_next; 936 else 937 printf("Didn't unlink in6_ifaddr from list\n"); 938 } 939 { 940 int iilen; 941 942 plen = in6_mask2len(&oia->ia_prefixmask.sin6_addr); 943 iilen = (sizeof(oia->ia_prefixmask.sin6_addr) << 3) - plen; 944 in6_prefix_remove_ifid(iilen, oia); 945 } 946 947 /* 948 * Check if we have another address that has the same prefix of 949 * the purged address. If we have one, reinstall the corresponding 950 * interface route. 951 */ 952 for (ia = in6_ifaddr; ia; ia = ia->ia_next) { 953 int e; 954 955 if (in6_are_prefix_equal(&ia->ia_addr.sin6_addr, 956 &oia->ia_addr.sin6_addr, plen)) { 957 if ((e = rtinit(&(ia->ia_ifa), (int)RTM_ADD, 958 ia->ia_flags)) == 0) { 959 ia->ia_flags |= IFA_ROUTE; 960 break; 961 } 962 else { 963 log(LOG_NOTICE, 964 "in6_purgeaddr: failed to add an interface" 965 " route for %s/%d on %s, errno = %d\n", 966 ip6_sprintf(&ia->ia_addr.sin6_addr), 967 plen, if_name(ia->ia_ifp), e); 968 /* still trying */ 969 } 970 } 971 } 972 973 /* release another refcnt for the link from in6_ifaddr */ 974 IFAFREE(&oia->ia_ifa); 975 } 976 977 /* 978 * SIOC[GAD]LIFADDR. 979 * SIOCGLIFADDR: get first address. (?) 980 * SIOCGLIFADDR with IFLR_PREFIX: 981 * get first address that matches the specified prefix. 982 * SIOCALIFADDR: add the specified address. 983 * SIOCALIFADDR with IFLR_PREFIX: 984 * add the specified prefix, filling hostid part from 985 * the first link-local address. prefixlen must be <= 64. 986 * SIOCDLIFADDR: delete the specified address. 987 * SIOCDLIFADDR with IFLR_PREFIX: 988 * delete the first address that matches the specified prefix. 989 * return values: 990 * EINVAL on invalid parameters 991 * EADDRNOTAVAIL on prefix match failed/specified address not found 992 * other values may be returned from in6_ioctl() 993 * 994 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64. 995 * this is to accomodate address naming scheme other than RFC2374, 996 * in the future. 997 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374 998 * address encoding scheme. (see figure on page 8) 999 */ 1000 static int 1001 in6_lifaddr_ioctl(so, cmd, data, ifp, p) 1002 struct socket *so; 1003 u_long cmd; 1004 caddr_t data; 1005 struct ifnet *ifp; 1006 struct proc *p; 1007 { 1008 struct if_laddrreq *iflr = (struct if_laddrreq *)data; 1009 struct ifaddr *ifa; 1010 struct sockaddr *sa; 1011 1012 /* sanity checks */ 1013 if (!data || !ifp) { 1014 panic("invalid argument to in6_lifaddr_ioctl"); 1015 /*NOTRECHED*/ 1016 } 1017 1018 switch (cmd) { 1019 case SIOCGLIFADDR: 1020 /* address must be specified on GET with IFLR_PREFIX */ 1021 if ((iflr->flags & IFLR_PREFIX) == 0) 1022 break; 1023 /*FALLTHROUGH*/ 1024 case SIOCALIFADDR: 1025 case SIOCDLIFADDR: 1026 /* address must be specified on ADD and DELETE */ 1027 sa = (struct sockaddr *)&iflr->addr; 1028 if (sa->sa_family != AF_INET6) 1029 return EINVAL; 1030 if (sa->sa_len != sizeof(struct sockaddr_in6)) 1031 return EINVAL; 1032 /* XXX need improvement */ 1033 sa = (struct sockaddr *)&iflr->dstaddr; 1034 if (sa->sa_family && sa->sa_family != AF_INET6) 1035 return EINVAL; 1036 if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6)) 1037 return EINVAL; 1038 break; 1039 default: /*shouldn't happen*/ 1040 #if 0 1041 panic("invalid cmd to in6_lifaddr_ioctl"); 1042 /*NOTREACHED*/ 1043 #else 1044 return EOPNOTSUPP; 1045 #endif 1046 } 1047 if (sizeof(struct in6_addr) * 8 < iflr->prefixlen) 1048 return EINVAL; 1049 1050 switch (cmd) { 1051 case SIOCALIFADDR: 1052 { 1053 struct in6_aliasreq ifra; 1054 struct in6_addr *hostid = NULL; 1055 int prefixlen; 1056 1057 if ((iflr->flags & IFLR_PREFIX) != 0) { 1058 struct sockaddr_in6 *sin6; 1059 1060 /* 1061 * hostid is to fill in the hostid part of the 1062 * address. hostid points to the first link-local 1063 * address attached to the interface. 1064 */ 1065 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0); 1066 if (!ifa) 1067 return EADDRNOTAVAIL; 1068 hostid = IFA_IN6(ifa); 1069 1070 /* prefixlen must be <= 64. */ 1071 if (64 < iflr->prefixlen) 1072 return EINVAL; 1073 prefixlen = iflr->prefixlen; 1074 1075 /* hostid part must be zero. */ 1076 sin6 = (struct sockaddr_in6 *)&iflr->addr; 1077 if (sin6->sin6_addr.s6_addr32[2] != 0 1078 || sin6->sin6_addr.s6_addr32[3] != 0) { 1079 return EINVAL; 1080 } 1081 } else 1082 prefixlen = iflr->prefixlen; 1083 1084 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */ 1085 bzero(&ifra, sizeof(ifra)); 1086 bcopy(iflr->iflr_name, ifra.ifra_name, 1087 sizeof(ifra.ifra_name)); 1088 1089 bcopy(&iflr->addr, &ifra.ifra_addr, 1090 ((struct sockaddr *)&iflr->addr)->sa_len); 1091 if (hostid) { 1092 /* fill in hostid part */ 1093 ifra.ifra_addr.sin6_addr.s6_addr32[2] = 1094 hostid->s6_addr32[2]; 1095 ifra.ifra_addr.sin6_addr.s6_addr32[3] = 1096 hostid->s6_addr32[3]; 1097 } 1098 1099 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /*XXX*/ 1100 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr, 1101 ((struct sockaddr *)&iflr->dstaddr)->sa_len); 1102 if (hostid) { 1103 ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] = 1104 hostid->s6_addr32[2]; 1105 ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] = 1106 hostid->s6_addr32[3]; 1107 } 1108 } 1109 1110 ifra.ifra_prefixmask.sin6_family = AF_INET6; 1111 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6); 1112 in6_len2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen); 1113 1114 ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX; 1115 return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, p); 1116 } 1117 case SIOCGLIFADDR: 1118 case SIOCDLIFADDR: 1119 { 1120 struct in6_ifaddr *ia; 1121 struct in6_addr mask, candidate, match; 1122 struct sockaddr_in6 *sin6; 1123 int cmp; 1124 1125 bzero(&mask, sizeof(mask)); 1126 if (iflr->flags & IFLR_PREFIX) { 1127 /* lookup a prefix rather than address. */ 1128 in6_len2mask(&mask, iflr->prefixlen); 1129 1130 sin6 = (struct sockaddr_in6 *)&iflr->addr; 1131 bcopy(&sin6->sin6_addr, &match, sizeof(match)); 1132 match.s6_addr32[0] &= mask.s6_addr32[0]; 1133 match.s6_addr32[1] &= mask.s6_addr32[1]; 1134 match.s6_addr32[2] &= mask.s6_addr32[2]; 1135 match.s6_addr32[3] &= mask.s6_addr32[3]; 1136 1137 /* if you set extra bits, that's wrong */ 1138 if (bcmp(&match, &sin6->sin6_addr, sizeof(match))) 1139 return EINVAL; 1140 1141 cmp = 1; 1142 } else { 1143 if (cmd == SIOCGLIFADDR) { 1144 /* on getting an address, take the 1st match */ 1145 cmp = 0; /*XXX*/ 1146 } else { 1147 /* on deleting an address, do exact match */ 1148 in6_len2mask(&mask, 128); 1149 sin6 = (struct sockaddr_in6 *)&iflr->addr; 1150 bcopy(&sin6->sin6_addr, &match, sizeof(match)); 1151 1152 cmp = 1; 1153 } 1154 } 1155 1156 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) 1157 { 1158 if (ifa->ifa_addr->sa_family != AF_INET6) 1159 continue; 1160 if (!cmp) 1161 break; 1162 bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate)); 1163 candidate.s6_addr32[0] &= mask.s6_addr32[0]; 1164 candidate.s6_addr32[1] &= mask.s6_addr32[1]; 1165 candidate.s6_addr32[2] &= mask.s6_addr32[2]; 1166 candidate.s6_addr32[3] &= mask.s6_addr32[3]; 1167 if (IN6_ARE_ADDR_EQUAL(&candidate, &match)) 1168 break; 1169 } 1170 if (!ifa) 1171 return EADDRNOTAVAIL; 1172 ia = ifa2ia6(ifa); 1173 1174 if (cmd == SIOCGLIFADDR) { 1175 /* fill in the if_laddrreq structure */ 1176 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len); 1177 1178 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 1179 bcopy(&ia->ia_dstaddr, &iflr->dstaddr, 1180 ia->ia_dstaddr.sin6_len); 1181 } else 1182 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr)); 1183 1184 iflr->prefixlen = 1185 in6_mask2len(&ia->ia_prefixmask.sin6_addr); 1186 1187 iflr->flags = ia->ia6_flags; /*XXX*/ 1188 1189 return 0; 1190 } else { 1191 struct in6_aliasreq ifra; 1192 1193 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */ 1194 bzero(&ifra, sizeof(ifra)); 1195 bcopy(iflr->iflr_name, ifra.ifra_name, 1196 sizeof(ifra.ifra_name)); 1197 1198 bcopy(&ia->ia_addr, &ifra.ifra_addr, 1199 ia->ia_addr.sin6_len); 1200 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 1201 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr, 1202 ia->ia_dstaddr.sin6_len); 1203 } else { 1204 bzero(&ifra.ifra_dstaddr, 1205 sizeof(ifra.ifra_dstaddr)); 1206 } 1207 bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr, 1208 ia->ia_prefixmask.sin6_len); 1209 1210 ifra.ifra_flags = ia->ia6_flags; 1211 return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra, 1212 ifp, p); 1213 } 1214 } 1215 } 1216 1217 return EOPNOTSUPP; /*just for safety*/ 1218 } 1219 1220 /* 1221 * Delete any existing route for an interface. 1222 */ 1223 void 1224 in6_ifscrub(ifp, ia) 1225 register struct ifnet *ifp; 1226 register struct in6_ifaddr *ia; 1227 { 1228 if ((ia->ia_flags & IFA_ROUTE) == 0) 1229 return; 1230 if (ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) 1231 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST); 1232 else 1233 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0); 1234 ia->ia_flags &= ~IFA_ROUTE; 1235 1236 /* Remove ownaddr's loopback rtentry, if it exists. */ 1237 in6_ifremloop(&(ia->ia_ifa)); 1238 } 1239 1240 /* 1241 * Initialize an interface's intetnet6 address 1242 * and routing table entry. 1243 */ 1244 int 1245 in6_ifinit(ifp, ia, sin6, scrub) 1246 struct ifnet *ifp; 1247 struct in6_ifaddr *ia; 1248 struct sockaddr_in6 *sin6; 1249 int scrub; 1250 { 1251 struct sockaddr_in6 oldaddr; 1252 int error, flags = RTF_UP; 1253 int s = splimp(); 1254 1255 oldaddr = ia->ia_addr; 1256 ia->ia_addr = *sin6; 1257 /* 1258 * Give the interface a chance to initialize 1259 * if this is its first address, 1260 * and to validate the address if necessary. 1261 */ 1262 if (ifp->if_ioctl && 1263 (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) { 1264 splx(s); 1265 ia->ia_addr = oldaddr; 1266 return(error); 1267 } 1268 1269 switch (ifp->if_type) { 1270 case IFT_ARCNET: 1271 case IFT_ETHER: 1272 case IFT_FDDI: 1273 ia->ia_ifa.ifa_rtrequest = nd6_rtrequest; 1274 ia->ia_ifa.ifa_flags |= RTF_CLONING; 1275 break; 1276 case IFT_PPP: 1277 ia->ia_ifa.ifa_rtrequest = nd6_p2p_rtrequest; 1278 ia->ia_ifa.ifa_flags |= RTF_CLONING; 1279 break; 1280 } 1281 1282 splx(s); 1283 if (scrub) { 1284 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr; 1285 in6_ifscrub(ifp, ia); 1286 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr; 1287 } 1288 /* xxx 1289 * in_socktrim 1290 */ 1291 /* 1292 * Add route for the network. 1293 */ 1294 ia->ia_ifa.ifa_metric = ifp->if_metric; 1295 if (ifp->if_flags & IFF_LOOPBACK) { 1296 ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr; 1297 flags |= RTF_HOST; 1298 } else if (ifp->if_flags & IFF_POINTOPOINT) { 1299 if (ia->ia_dstaddr.sin6_family != AF_INET6) 1300 return(0); 1301 flags |= RTF_HOST; 1302 } 1303 if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD, flags)) == 0) 1304 ia->ia_flags |= IFA_ROUTE; 1305 /* XXX check if the subnet route points to the same interface */ 1306 if (error == EEXIST) 1307 error = 0; 1308 1309 /* Add ownaddr as loopback rtentry, if necessary(ex. on p2p link). */ 1310 in6_ifaddloop(&(ia->ia_ifa)); 1311 1312 return(error); 1313 } 1314 1315 /* 1316 * Add an address to the list of IP6 multicast addresses for a 1317 * given interface. 1318 */ 1319 struct in6_multi * 1320 in6_addmulti(maddr6, ifp, errorp) 1321 register struct in6_addr *maddr6; 1322 register struct ifnet *ifp; 1323 int *errorp; 1324 { 1325 struct in6_multi *in6m; 1326 struct sockaddr_in6 sin6; 1327 struct ifmultiaddr *ifma; 1328 int s = splnet(); 1329 1330 *errorp = 0; 1331 1332 /* 1333 * Call generic routine to add membership or increment 1334 * refcount. It wants addresses in the form of a sockaddr, 1335 * so we build one here (being careful to zero the unused bytes). 1336 */ 1337 bzero(&sin6, sizeof sin6); 1338 sin6.sin6_family = AF_INET6; 1339 sin6.sin6_len = sizeof sin6; 1340 sin6.sin6_addr = *maddr6; 1341 *errorp = if_addmulti(ifp, (struct sockaddr *)&sin6, &ifma); 1342 if (*errorp) { 1343 splx(s); 1344 return 0; 1345 } 1346 1347 /* 1348 * If ifma->ifma_protospec is null, then if_addmulti() created 1349 * a new record. Otherwise, we are done. 1350 */ 1351 if (ifma->ifma_protospec != 0) 1352 return ifma->ifma_protospec; 1353 1354 /* XXX - if_addmulti uses M_WAITOK. Can this really be called 1355 at interrupt time? If so, need to fix if_addmulti. XXX */ 1356 in6m = (struct in6_multi *)malloc(sizeof(*in6m), M_IPMADDR, M_NOWAIT); 1357 if (in6m == NULL) { 1358 splx(s); 1359 return (NULL); 1360 } 1361 1362 bzero(in6m, sizeof *in6m); 1363 in6m->in6m_addr = *maddr6; 1364 in6m->in6m_ifp = ifp; 1365 in6m->in6m_ifma = ifma; 1366 ifma->ifma_protospec = in6m; 1367 LIST_INSERT_HEAD(&in6_multihead, in6m, in6m_entry); 1368 1369 /* 1370 * Let MLD6 know that we have joined a new IP6 multicast 1371 * group. 1372 */ 1373 mld6_start_listening(in6m); 1374 splx(s); 1375 return(in6m); 1376 } 1377 1378 /* 1379 * Delete a multicast address record. 1380 */ 1381 void 1382 in6_delmulti(in6m) 1383 struct in6_multi *in6m; 1384 { 1385 struct ifmultiaddr *ifma = in6m->in6m_ifma; 1386 int s = splnet(); 1387 1388 if (ifma->ifma_refcount == 1) { 1389 /* 1390 * No remaining claims to this record; let MLD6 know 1391 * that we are leaving the multicast group. 1392 */ 1393 mld6_stop_listening(in6m); 1394 ifma->ifma_protospec = 0; 1395 LIST_REMOVE(in6m, in6m_entry); 1396 free(in6m, M_IPMADDR); 1397 } 1398 /* XXX - should be separate API for when we have an ifma? */ 1399 if_delmulti(ifma->ifma_ifp, ifma->ifma_addr); 1400 splx(s); 1401 } 1402 1403 /* 1404 * Find an IPv6 interface link-local address specific to an interface. 1405 */ 1406 struct in6_ifaddr * 1407 in6ifa_ifpforlinklocal(ifp, ignoreflags) 1408 struct ifnet *ifp; 1409 int ignoreflags; 1410 { 1411 register struct ifaddr *ifa; 1412 1413 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) 1414 { 1415 if (ifa->ifa_addr == NULL) 1416 continue; /* just for safety */ 1417 if (ifa->ifa_addr->sa_family != AF_INET6) 1418 continue; 1419 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) { 1420 if ((((struct in6_ifaddr *)ifa)->ia6_flags & 1421 ignoreflags) != 0) 1422 continue; 1423 break; 1424 } 1425 } 1426 1427 return((struct in6_ifaddr *)ifa); 1428 } 1429 1430 1431 /* 1432 * find the internet address corresponding to a given interface and address. 1433 */ 1434 struct in6_ifaddr * 1435 in6ifa_ifpwithaddr(ifp, addr) 1436 struct ifnet *ifp; 1437 struct in6_addr *addr; 1438 { 1439 register struct ifaddr *ifa; 1440 1441 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) 1442 { 1443 if (ifa->ifa_addr == NULL) 1444 continue; /* just for safety */ 1445 if (ifa->ifa_addr->sa_family != AF_INET6) 1446 continue; 1447 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa))) 1448 break; 1449 } 1450 1451 return((struct in6_ifaddr *)ifa); 1452 } 1453 1454 /* 1455 * Convert IP6 address to printable (loggable) representation. 1456 */ 1457 static char digits[] = "0123456789abcdef"; 1458 static int ip6round = 0; 1459 char * 1460 ip6_sprintf(addr) 1461 register struct in6_addr *addr; 1462 { 1463 static char ip6buf[8][48]; 1464 register int i; 1465 register char *cp; 1466 register u_short *a = (u_short *)addr; 1467 register u_char *d; 1468 int dcolon = 0; 1469 1470 ip6round = (ip6round + 1) & 7; 1471 cp = ip6buf[ip6round]; 1472 1473 for (i = 0; i < 8; i++) { 1474 if (dcolon == 1) { 1475 if (*a == 0) { 1476 if (i == 7) 1477 *cp++ = ':'; 1478 a++; 1479 continue; 1480 } else 1481 dcolon = 2; 1482 } 1483 if (*a == 0) { 1484 if (dcolon == 0 && *(a + 1) == 0) { 1485 if (i == 0) 1486 *cp++ = ':'; 1487 *cp++ = ':'; 1488 dcolon = 1; 1489 } else { 1490 *cp++ = '0'; 1491 *cp++ = ':'; 1492 } 1493 a++; 1494 continue; 1495 } 1496 d = (u_char *)a; 1497 *cp++ = digits[*d >> 4]; 1498 *cp++ = digits[*d++ & 0xf]; 1499 *cp++ = digits[*d >> 4]; 1500 *cp++ = digits[*d & 0xf]; 1501 *cp++ = ':'; 1502 a++; 1503 } 1504 *--cp = 0; 1505 return(ip6buf[ip6round]); 1506 } 1507 1508 int 1509 in6_localaddr(in6) 1510 struct in6_addr *in6; 1511 { 1512 struct in6_ifaddr *ia; 1513 1514 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6)) 1515 return 1; 1516 1517 for (ia = in6_ifaddr; ia; ia = ia->ia_next) 1518 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr, 1519 &ia->ia_prefixmask.sin6_addr)) 1520 return 1; 1521 1522 return (0); 1523 } 1524 1525 /* 1526 * return length of part which dst and src are equal 1527 * hard coding... 1528 */ 1529 int 1530 in6_matchlen(src, dst) 1531 struct in6_addr *src, *dst; 1532 { 1533 int match = 0; 1534 u_char *s = (u_char *)src, *d = (u_char *)dst; 1535 u_char *lim = s + 16, r; 1536 1537 while (s < lim) 1538 if ((r = (*d++ ^ *s++)) != 0) { 1539 while (r < 128) { 1540 match++; 1541 r <<= 1; 1542 } 1543 break; 1544 } else 1545 match += 8; 1546 return match; 1547 } 1548 1549 /* XXX: to be scope conscious */ 1550 int 1551 in6_are_prefix_equal(p1, p2, len) 1552 struct in6_addr *p1, *p2; 1553 int len; 1554 { 1555 int bytelen, bitlen; 1556 1557 /* sanity check */ 1558 if (0 > len || len > 128) { 1559 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n", 1560 len); 1561 return(0); 1562 } 1563 1564 bytelen = len / 8; 1565 bitlen = len % 8; 1566 1567 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen)) 1568 return(0); 1569 if (p1->s6_addr[bytelen] >> (8 - bitlen) != 1570 p2->s6_addr[bytelen] >> (8 - bitlen)) 1571 return(0); 1572 1573 return(1); 1574 } 1575 1576 void 1577 in6_prefixlen2mask(maskp, len) 1578 struct in6_addr *maskp; 1579 int len; 1580 { 1581 u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff}; 1582 int bytelen, bitlen, i; 1583 1584 /* sanity check */ 1585 if (0 > len || len > 128) { 1586 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n", 1587 len); 1588 return; 1589 } 1590 1591 bzero(maskp, sizeof(*maskp)); 1592 bytelen = len / 8; 1593 bitlen = len % 8; 1594 for (i = 0; i < bytelen; i++) 1595 maskp->s6_addr[i] = 0xff; 1596 if (bitlen) 1597 maskp->s6_addr[bytelen] = maskarray[bitlen - 1]; 1598 } 1599 1600 /* 1601 * return the best address out of the same scope 1602 */ 1603 struct in6_ifaddr * 1604 in6_ifawithscope(oifp, dst) 1605 register struct ifnet *oifp; 1606 register struct in6_addr *dst; 1607 { 1608 int dst_scope = in6_addrscope(dst), src_scope, best_scope = 0; 1609 int blen = -1; 1610 struct ifaddr *ifa; 1611 struct ifnet *ifp; 1612 struct in6_ifaddr *ifa_best = NULL; 1613 1614 if (oifp == NULL) { 1615 printf("in6_ifawithscope: output interface is not specified\n"); 1616 return(NULL); 1617 } 1618 1619 /* 1620 * We search for all addresses on all interfaces from the beginning. 1621 * Comparing an interface with the outgoing interface will be done 1622 * only at the final stage of tiebreaking. 1623 */ 1624 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) 1625 { 1626 /* 1627 * We can never take an address that breaks the scope zone 1628 * of the destination. 1629 */ 1630 if (in6_addr2scopeid(ifp, dst) != in6_addr2scopeid(oifp, dst)) 1631 continue; 1632 1633 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) 1634 { 1635 int tlen = -1, dscopecmp, bscopecmp, matchcmp; 1636 1637 if (ifa->ifa_addr->sa_family != AF_INET6) 1638 continue; 1639 1640 src_scope = in6_addrscope(IFA_IN6(ifa)); 1641 1642 /* 1643 * Don't use an address before completing DAD 1644 * nor a duplicated address. 1645 */ 1646 if (((struct in6_ifaddr *)ifa)->ia6_flags & 1647 IN6_IFF_NOTREADY) 1648 continue; 1649 1650 /* XXX: is there any case to allow anycasts? */ 1651 if (((struct in6_ifaddr *)ifa)->ia6_flags & 1652 IN6_IFF_ANYCAST) 1653 continue; 1654 1655 if (((struct in6_ifaddr *)ifa)->ia6_flags & 1656 IN6_IFF_DETACHED) 1657 continue; 1658 1659 /* 1660 * If this is the first address we find, 1661 * keep it anyway. 1662 */ 1663 if (ifa_best == NULL) 1664 goto replace; 1665 1666 /* 1667 * ifa_best is never NULL beyond this line except 1668 * within the block labeled "replace". 1669 */ 1670 1671 /* 1672 * If ifa_best has a smaller scope than dst and 1673 * the current address has a larger one than 1674 * (or equal to) dst, always replace ifa_best. 1675 * Also, if the current address has a smaller scope 1676 * than dst, ignore it unless ifa_best also has a 1677 * smaller scope. 1678 */ 1679 if (IN6_ARE_SCOPE_CMP(best_scope, dst_scope) < 0 && 1680 IN6_ARE_SCOPE_CMP(src_scope, dst_scope) >= 0) 1681 goto replace; 1682 if (IN6_ARE_SCOPE_CMP(src_scope, dst_scope) < 0 && 1683 IN6_ARE_SCOPE_CMP(best_scope, dst_scope) >= 0) 1684 continue; 1685 1686 /* 1687 * A deprecated address SHOULD NOT be used in new 1688 * communications if an alternate (non-deprecated) 1689 * address is available and has sufficient scope. 1690 * RFC 2462, Section 5.5.4. 1691 */ 1692 if (((struct in6_ifaddr *)ifa)->ia6_flags & 1693 IN6_IFF_DEPRECATED) { 1694 /* 1695 * Ignore any deprecated addresses if 1696 * specified by configuration. 1697 */ 1698 if (!ip6_use_deprecated) 1699 continue; 1700 1701 /* 1702 * If we have already found a non-deprecated 1703 * candidate, just ignore deprecated addresses. 1704 */ 1705 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) 1706 == 0) 1707 continue; 1708 } 1709 1710 /* 1711 * A non-deprecated address is always preferred 1712 * to a deprecated one regardless of scopes and 1713 * address matching. 1714 */ 1715 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) && 1716 (((struct in6_ifaddr *)ifa)->ia6_flags & 1717 IN6_IFF_DEPRECATED) == 0) 1718 goto replace; 1719 1720 /* 1721 * At this point, we have two cases: 1722 * 1. we are looking at a non-deprecated address, 1723 * and ifa_best is also non-deprecated. 1724 * 2. we are looking at a deprecated address, 1725 * and ifa_best is also deprecated. 1726 * Also, we do not have to consider a case where 1727 * the scope of if_best is larger(smaller) than dst and 1728 * the scope of the current address is smaller(larger) 1729 * than dst. Such a case has already been covered. 1730 * Tiebreaking is done according to the following 1731 * items: 1732 * - the scope comparison between the address and 1733 * dst (dscopecmp) 1734 * - the scope comparison between the address and 1735 * ifa_best (bscopecmp) 1736 * - if the address match dst longer than ifa_best 1737 * (matchcmp) 1738 * - if the address is on the outgoing I/F (outI/F) 1739 * 1740 * Roughly speaking, the selection policy is 1741 * - the most important item is scope. The same scope 1742 * is best. Then search for a larger scope. 1743 * Smaller scopes are the last resort. 1744 * - A deprecated address is chosen only when we have 1745 * no address that has an enough scope, but is 1746 * prefered to any addresses of smaller scopes. 1747 * - Longest address match against dst is considered 1748 * only for addresses that has the same scope of dst. 1749 * - If there is no other reasons to choose one, 1750 * addresses on the outgoing I/F are preferred. 1751 * 1752 * The precise decision table is as follows: 1753 * dscopecmp bscopecmp matchcmp outI/F | replace? 1754 * !equal equal N/A Yes | Yes (1) 1755 * !equal equal N/A No | No (2) 1756 * larger larger N/A N/A | No (3) 1757 * larger smaller N/A N/A | Yes (4) 1758 * smaller larger N/A N/A | Yes (5) 1759 * smaller smaller N/A N/A | No (6) 1760 * equal smaller N/A N/A | Yes (7) 1761 * equal larger (already done) 1762 * equal equal larger N/A | Yes (8) 1763 * equal equal smaller N/A | No (9) 1764 * equal equal equal Yes | Yes (a) 1765 * eaual eqaul equal No | No (b) 1766 */ 1767 dscopecmp = IN6_ARE_SCOPE_CMP(src_scope, dst_scope); 1768 bscopecmp = IN6_ARE_SCOPE_CMP(src_scope, best_scope); 1769 1770 if (dscopecmp && bscopecmp == 0) { 1771 if (oifp == ifp) /* (1) */ 1772 goto replace; 1773 continue; /* (2) */ 1774 } 1775 if (dscopecmp > 0) { 1776 if (bscopecmp > 0) /* (3) */ 1777 continue; 1778 goto replace; /* (4) */ 1779 } 1780 if (dscopecmp < 0) { 1781 if (bscopecmp > 0) /* (5) */ 1782 goto replace; 1783 continue; /* (6) */ 1784 } 1785 1786 /* now dscopecmp must be 0 */ 1787 if (bscopecmp < 0) 1788 goto replace; /* (7) */ 1789 1790 /* 1791 * At last both dscopecmp and bscopecmp must be 0. 1792 * We need address matching against dst for 1793 * tiebreaking. 1794 */ 1795 tlen = in6_matchlen(IFA_IN6(ifa), dst); 1796 matchcmp = tlen - blen; 1797 if (matchcmp > 0) /* (8) */ 1798 goto replace; 1799 if (matchcmp < 0) /* (9) */ 1800 continue; 1801 if (oifp == ifp) /* (a) */ 1802 goto replace; 1803 continue; /* (b) */ 1804 1805 replace: 1806 ifa_best = (struct in6_ifaddr *)ifa; 1807 blen = tlen >= 0 ? tlen : 1808 in6_matchlen(IFA_IN6(ifa), dst); 1809 best_scope = in6_addrscope(&ifa_best->ia_addr.sin6_addr); 1810 } 1811 } 1812 1813 /* count statistics for future improvements */ 1814 if (ifa_best == NULL) 1815 ip6stat.ip6s_sources_none++; 1816 else { 1817 if (oifp == ifa_best->ia_ifp) 1818 ip6stat.ip6s_sources_sameif[best_scope]++; 1819 else 1820 ip6stat.ip6s_sources_otherif[best_scope]++; 1821 1822 if (best_scope == dst_scope) 1823 ip6stat.ip6s_sources_samescope[best_scope]++; 1824 else 1825 ip6stat.ip6s_sources_otherscope[best_scope]++; 1826 1827 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) != 0) 1828 ip6stat.ip6s_sources_deprecated[best_scope]++; 1829 } 1830 1831 return(ifa_best); 1832 } 1833 1834 /* 1835 * return the best address out of the same scope. if no address was 1836 * found, return the first valid address from designated IF. 1837 */ 1838 struct in6_ifaddr * 1839 in6_ifawithifp(ifp, dst) 1840 register struct ifnet *ifp; 1841 register struct in6_addr *dst; 1842 { 1843 int dst_scope = in6_addrscope(dst), blen = -1, tlen; 1844 struct ifaddr *ifa; 1845 struct in6_ifaddr *besta = 0; 1846 struct in6_ifaddr *dep[2]; /*last-resort: deprecated*/ 1847 1848 dep[0] = dep[1] = NULL; 1849 1850 /* 1851 * We first look for addresses in the same scope. 1852 * If there is one, return it. 1853 * If two or more, return one which matches the dst longest. 1854 * If none, return one of global addresses assigned other ifs. 1855 */ 1856 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) 1857 { 1858 if (ifa->ifa_addr->sa_family != AF_INET6) 1859 continue; 1860 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST) 1861 continue; /* XXX: is there any case to allow anycast? */ 1862 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY) 1863 continue; /* don't use this interface */ 1864 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED) 1865 continue; 1866 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) { 1867 if (ip6_use_deprecated) 1868 dep[0] = (struct in6_ifaddr *)ifa; 1869 continue; 1870 } 1871 1872 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) { 1873 /* 1874 * call in6_matchlen() as few as possible 1875 */ 1876 if (besta) { 1877 if (blen == -1) 1878 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst); 1879 tlen = in6_matchlen(IFA_IN6(ifa), dst); 1880 if (tlen > blen) { 1881 blen = tlen; 1882 besta = (struct in6_ifaddr *)ifa; 1883 } 1884 } else 1885 besta = (struct in6_ifaddr *)ifa; 1886 } 1887 } 1888 if (besta) 1889 return(besta); 1890 1891 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) 1892 { 1893 if (ifa->ifa_addr->sa_family != AF_INET6) 1894 continue; 1895 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST) 1896 continue; /* XXX: is there any case to allow anycast? */ 1897 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY) 1898 continue; /* don't use this interface */ 1899 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED) 1900 continue; 1901 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) { 1902 if (ip6_use_deprecated) 1903 dep[1] = (struct in6_ifaddr *)ifa; 1904 continue; 1905 } 1906 1907 return (struct in6_ifaddr *)ifa; 1908 } 1909 1910 /* use the last-resort values, that are, deprecated addresses */ 1911 if (dep[0]) 1912 return dep[0]; 1913 if (dep[1]) 1914 return dep[1]; 1915 1916 return NULL; 1917 } 1918 1919 /* 1920 * perform DAD when interface becomes IFF_UP. 1921 */ 1922 void 1923 in6_if_up(ifp) 1924 struct ifnet *ifp; 1925 { 1926 struct ifaddr *ifa; 1927 struct in6_ifaddr *ia; 1928 int dad_delay; /* delay ticks before DAD output */ 1929 1930 /* 1931 * special cases, like 6to4, are handled in in6_ifattach 1932 */ 1933 in6_ifattach(ifp, NULL); 1934 1935 dad_delay = 0; 1936 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) 1937 { 1938 if (ifa->ifa_addr->sa_family != AF_INET6) 1939 continue; 1940 ia = (struct in6_ifaddr *)ifa; 1941 if (ia->ia6_flags & IN6_IFF_TENTATIVE) 1942 nd6_dad_start(ifa, &dad_delay); 1943 } 1944 } 1945 1946 /* 1947 * Calculate max IPv6 MTU through all the interfaces and store it 1948 * to in6_maxmtu. 1949 */ 1950 void 1951 in6_setmaxmtu() 1952 { 1953 unsigned long maxmtu = 0; 1954 struct ifnet *ifp; 1955 1956 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) 1957 { 1958 if ((ifp->if_flags & IFF_LOOPBACK) == 0 && 1959 nd_ifinfo[ifp->if_index].linkmtu > maxmtu) 1960 maxmtu = nd_ifinfo[ifp->if_index].linkmtu; 1961 } 1962 if (maxmtu) /* update only when maxmtu is positive */ 1963 in6_maxmtu = maxmtu; 1964 } 1965 1966 /* 1967 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be 1968 * v4 mapped addr or v4 compat addr 1969 */ 1970 void 1971 in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6) 1972 { 1973 bzero(sin, sizeof(*sin)); 1974 sin->sin_len = sizeof(struct sockaddr_in); 1975 sin->sin_family = AF_INET; 1976 sin->sin_port = sin6->sin6_port; 1977 sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3]; 1978 } 1979 1980 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */ 1981 void 1982 in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6) 1983 { 1984 bzero(sin6, sizeof(*sin6)); 1985 sin6->sin6_len = sizeof(struct sockaddr_in6); 1986 sin6->sin6_family = AF_INET6; 1987 sin6->sin6_port = sin->sin_port; 1988 sin6->sin6_addr.s6_addr32[0] = 0; 1989 sin6->sin6_addr.s6_addr32[1] = 0; 1990 sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP; 1991 sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr; 1992 } 1993 1994 /* Convert sockaddr_in6 into sockaddr_in. */ 1995 void 1996 in6_sin6_2_sin_in_sock(struct sockaddr *nam) 1997 { 1998 struct sockaddr_in *sin_p; 1999 struct sockaddr_in6 sin6; 2000 2001 /* 2002 * Save original sockaddr_in6 addr and convert it 2003 * to sockaddr_in. 2004 */ 2005 sin6 = *(struct sockaddr_in6 *)nam; 2006 sin_p = (struct sockaddr_in *)nam; 2007 in6_sin6_2_sin(sin_p, &sin6); 2008 } 2009 2010 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */ 2011 void 2012 in6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam) 2013 { 2014 struct sockaddr_in *sin_p; 2015 struct sockaddr_in6 *sin6_p; 2016 2017 MALLOC(sin6_p, struct sockaddr_in6 *, sizeof *sin6_p, M_SONAME, 2018 M_WAITOK); 2019 sin_p = (struct sockaddr_in *)*nam; 2020 in6_sin_2_v4mapsin6(sin_p, sin6_p); 2021 FREE(*nam, M_SONAME); 2022 *nam = (struct sockaddr *)sin6_p; 2023 } 2024