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