in6_src.c revision 1.31
1/* $OpenBSD: in6_src.c,v 1.31 2013/03/28 16:45:16 tedu Exp $ */ 2/* $KAME: in6_src.c,v 1.36 2001/02/06 04:08:17 itojun Exp $ */ 3 4/* 5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the project nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33/* 34 * Copyright (c) 1982, 1986, 1991, 1993 35 * The Regents of the University of California. All rights reserved. 36 * 37 * Redistribution and use in source and binary forms, with or without 38 * modification, are permitted provided that the following conditions 39 * are met: 40 * 1. Redistributions of source code must retain the above copyright 41 * notice, this list of conditions and the following disclaimer. 42 * 2. Redistributions in binary form must reproduce the above copyright 43 * notice, this list of conditions and the following disclaimer in the 44 * documentation and/or other materials provided with the distribution. 45 * 3. Neither the name of the University nor the names of its contributors 46 * may be used to endorse or promote products derived from this software 47 * without specific prior written permission. 48 * 49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 59 * SUCH DAMAGE. 60 * 61 * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94 62 */ 63 64#include <sys/param.h> 65#include <sys/systm.h> 66#include <sys/malloc.h> 67#include <sys/mbuf.h> 68#include <sys/protosw.h> 69#include <sys/socket.h> 70#include <sys/socketvar.h> 71#include <sys/ioctl.h> 72#include <sys/errno.h> 73#include <sys/time.h> 74 75#include <net/if.h> 76#include <net/route.h> 77 78#include <netinet/in.h> 79#include <netinet/in_var.h> 80#include <netinet/in_systm.h> 81#include <netinet/ip.h> 82#include <netinet/in_pcb.h> 83#include <netinet6/in6_var.h> 84#include <netinet/ip6.h> 85#include <netinet6/ip6_var.h> 86#include <netinet6/nd6.h> 87 88int in6_selectif(struct sockaddr_in6 *, struct ip6_pktopts *, 89 struct ip6_moptions *, struct route_in6 *, struct ifnet **, u_int); 90int selectroute(struct sockaddr_in6 *, struct ip6_pktopts *, 91 struct ip6_moptions *, struct route_in6 *, struct ifnet **, 92 struct rtentry **, int, u_int); 93 94/* 95 * Return an IPv6 address, which is the most appropriate for a given 96 * destination and user specified options. 97 * If necessary, this function lookups the routing table and returns 98 * an entry to the caller for later use. 99 */ 100struct in6_addr * 101in6_selectsrc(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts, 102 struct ip6_moptions *mopts, struct route_in6 *ro, struct in6_addr *laddr, 103 int *errorp, u_int rtableid) 104{ 105 struct ifnet *ifp = NULL; 106 struct in6_addr *dst; 107 struct in6_ifaddr *ia6 = NULL; 108 struct in6_pktinfo *pi = NULL; 109 110 dst = &dstsock->sin6_addr; 111 *errorp = 0; 112 113 /* 114 * If the source address is explicitly specified by the caller, 115 * check if the requested source address is indeed a unicast address 116 * assigned to the node, and can be used as the packet's source 117 * address. If everything is okay, use the address as source. 118 */ 119 if (opts && (pi = opts->ip6po_pktinfo) && 120 !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr)) { 121 struct sockaddr_in6 sa6; 122 123 /* get the outgoing interface */ 124 if ((*errorp = in6_selectif(dstsock, opts, mopts, ro, 125 &ifp, rtableid)) != 0) 126 return (NULL); 127 128 bzero(&sa6, sizeof(sa6)); 129 sa6.sin6_family = AF_INET6; 130 sa6.sin6_len = sizeof(sa6); 131 sa6.sin6_addr = pi->ipi6_addr; 132 133 if (ifp && IN6_IS_SCOPE_EMBED(&sa6.sin6_addr)) 134 sa6.sin6_addr.s6_addr16[1] = htons(ifp->if_index); 135 136 ia6 = ifatoia6( 137 ifa_ifwithaddr((struct sockaddr *)&sa6, rtableid)); 138 if (ia6 == NULL || 139 (ia6->ia6_flags & (IN6_IFF_ANYCAST | IN6_IFF_NOTREADY))) { 140 *errorp = EADDRNOTAVAIL; 141 return (NULL); 142 } 143 144 pi->ipi6_addr = sa6.sin6_addr; /* XXX: this overrides pi */ 145 146 return (&pi->ipi6_addr); 147 } 148 149 /* 150 * If the source address is not specified but the socket(if any) 151 * is already bound, use the bound address. 152 */ 153 if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr)) 154 return (laddr); 155 156 /* 157 * If the caller doesn't specify the source address but 158 * the outgoing interface, use an address associated with 159 * the interface. 160 */ 161 if (pi && pi->ipi6_ifindex) { 162 ifp = if_get(pi->ipi6_ifindex); 163 if (ifp == NULL) { 164 *errorp = ENXIO; /* XXX: better error? */ 165 return (0); 166 } 167 ia6 = in6_ifawithscope(ifp, dst, rtableid); 168 if (ia6 == 0) { 169 *errorp = EADDRNOTAVAIL; 170 return (0); 171 } 172 return (&ia6->ia_addr.sin6_addr); 173 } 174 175 /* 176 * If the destination address is a link-local unicast address or 177 * a link/interface-local multicast address, and if the outgoing 178 * interface is specified by the sin6_scope_id filed, use an address 179 * associated with the interface. 180 * XXX: We're now trying to define more specific semantics of 181 * sin6_scope_id field, so this part will be rewritten in 182 * the near future. 183 */ 184 if ((IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MC_LINKLOCAL(dst) || 185 IN6_IS_ADDR_MC_INTFACELOCAL(dst)) && dstsock->sin6_scope_id) { 186 ifp = if_get(dstsock->sin6_scope_id); 187 if (ifp == NULL) { 188 *errorp = ENXIO; /* XXX: better error? */ 189 return (0); 190 } 191 ia6 = in6_ifawithscope(ifp, dst, rtableid); 192 if (ia6 == 0) { 193 *errorp = EADDRNOTAVAIL; 194 return (0); 195 } 196 return (&ia6->ia_addr.sin6_addr); 197 } 198 199 /* 200 * If the destination address is a multicast address and 201 * the outgoing interface for the address is specified 202 * by the caller, use an address associated with the interface. 203 * Even if the outgoing interface is not specified, we also 204 * choose a loopback interface as the outgoing interface. 205 */ 206 if (IN6_IS_ADDR_MULTICAST(dst)) { 207 ifp = mopts ? mopts->im6o_multicast_ifp : NULL; 208 209 if (!ifp && dstsock->sin6_scope_id) 210 ifp = if_get(htons(dstsock->sin6_scope_id)); 211 212 if (ifp) { 213 ia6 = in6_ifawithscope(ifp, dst, rtableid); 214 if (ia6 == 0) { 215 *errorp = EADDRNOTAVAIL; 216 return (0); 217 } 218 return (&ia6->ia_addr.sin6_addr); 219 } 220 } 221 222 /* 223 * If the next hop address for the packet is specified 224 * by caller, use an address associated with the route 225 * to the next hop. 226 */ 227 { 228 struct sockaddr_in6 *sin6_next; 229 struct rtentry *rt; 230 231 if (opts && opts->ip6po_nexthop) { 232 sin6_next = satosin6(opts->ip6po_nexthop); 233 rt = nd6_lookup(&sin6_next->sin6_addr, 1, NULL); 234 if (rt) { 235 ia6 = in6_ifawithscope(rt->rt_ifp, dst, 236 rtableid); 237 if (ia6 == 0) 238 ia6 = ifatoia6(rt->rt_ifa); 239 } 240 if (ia6 == 0) { 241 *errorp = EADDRNOTAVAIL; 242 return (0); 243 } 244 return (&ia6->ia_addr.sin6_addr); 245 } 246 } 247 248 /* 249 * If route is known or can be allocated now, 250 * our src addr is taken from the i/f, else punt. 251 */ 252 if (ro) { 253 if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 || 254 !IN6_ARE_ADDR_EQUAL(&ro->ro_dst.sin6_addr, dst))) { 255 RTFREE(ro->ro_rt); 256 ro->ro_rt = (struct rtentry *)0; 257 } 258 if (ro->ro_rt == (struct rtentry *)0 || 259 ro->ro_rt->rt_ifp == (struct ifnet *)0) { 260 struct sockaddr_in6 *sa6; 261 262 /* No route yet, so try to acquire one */ 263 bzero(&ro->ro_dst, sizeof(struct sockaddr_in6)); 264 sa6 = (struct sockaddr_in6 *)&ro->ro_dst; 265 sa6->sin6_family = AF_INET6; 266 sa6->sin6_len = sizeof(struct sockaddr_in6); 267 sa6->sin6_addr = *dst; 268 sa6->sin6_scope_id = dstsock->sin6_scope_id; 269 if (IN6_IS_ADDR_MULTICAST(dst)) { 270 rtalloc((struct route *)ro); 271 } else { 272 rtalloc_mpath((struct route *)ro, NULL); 273 } 274 } 275 276 /* 277 * in_pcbconnect() checks out IFF_LOOPBACK to skip using 278 * the address. But we don't know why it does so. 279 * It is necessary to ensure the scope even for lo0 280 * so doesn't check out IFF_LOOPBACK. 281 */ 282 283 if (ro->ro_rt) { 284 ia6 = in6_ifawithscope(ro->ro_rt->rt_ifa->ifa_ifp, dst, 285 rtableid); 286 if (ia6 == 0) /* xxx scope error ?*/ 287 ia6 = ifatoia6(ro->ro_rt->rt_ifa); 288 } 289#if 0 290 /* 291 * xxx The followings are necessary? (kazu) 292 * I don't think so. 293 * It's for SO_DONTROUTE option in IPv4.(jinmei) 294 */ 295 if (ia6 == 0) { 296 struct sockaddr_in6 sin6 = {sizeof(sin6), AF_INET6, 0}; 297 298 sin6->sin6_addr = *dst; 299 300 ia6 = ifatoia6(ifa_ifwithdstaddr(sin6tosa(&sin6))); 301 if (ia6 == 0) 302 ia6 = ifatoia6(ifa_ifwithnet(sin6tosa(&sin6))); 303 if (ia6 == 0) 304 return (0); 305 return (&ia6->ia_addr.sin6_addr); 306 } 307#endif /* 0 */ 308 if (ia6 == 0) { 309 *errorp = EHOSTUNREACH; /* no route */ 310 return (0); 311 } 312 return (&ia6->ia_addr.sin6_addr); 313 } 314 315 *errorp = EADDRNOTAVAIL; 316 return (0); 317} 318 319int 320selectroute(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts, 321 struct ip6_moptions *mopts, struct route_in6 *ro, struct ifnet **retifp, 322 struct rtentry **retrt, int norouteok, u_int rtableid) 323{ 324 int error = 0; 325 struct ifnet *ifp = NULL; 326 struct rtentry *rt = NULL; 327 struct sockaddr_in6 *sin6_next; 328 struct in6_pktinfo *pi = NULL; 329 struct in6_addr *dst; 330 331 dst = &dstsock->sin6_addr; 332 333#if 0 334 if (dstsock->sin6_addr.s6_addr32[0] == 0 && 335 dstsock->sin6_addr.s6_addr32[1] == 0 && 336 !IN6_IS_ADDR_LOOPBACK(&dstsock->sin6_addr)) { 337 printf("in6_selectroute: strange destination %s\n", 338 ip6_sprintf(&dstsock->sin6_addr)); 339 } else { 340 printf("in6_selectroute: destination = %s%%%d\n", 341 ip6_sprintf(&dstsock->sin6_addr), 342 dstsock->sin6_scope_id); /* for debug */ 343 } 344#endif 345 346 /* If the caller specify the outgoing interface explicitly, use it. */ 347 if (opts && (pi = opts->ip6po_pktinfo) != NULL && pi->ipi6_ifindex) { 348 ifp = if_get(pi->ipi6_ifindex); 349 if (ifp != NULL && 350 (norouteok || retrt == NULL || 351 IN6_IS_ADDR_MULTICAST(dst))) { 352 /* 353 * we do not have to check or get the route for 354 * multicast. 355 */ 356 goto done; 357 } else 358 goto getroute; 359 } 360 361 /* 362 * If the destination address is a multicast address and the outgoing 363 * interface for the address is specified by the caller, use it. 364 */ 365 if (IN6_IS_ADDR_MULTICAST(dst) && 366 mopts != NULL && (ifp = mopts->im6o_multicast_ifp) != NULL) { 367 goto done; /* we do not need a route for multicast. */ 368 } 369 370 getroute: 371 /* 372 * If the next hop address for the packet is specified by the caller, 373 * use it as the gateway. 374 */ 375 if (opts && opts->ip6po_nexthop) { 376 struct route_in6 *ron; 377 378 sin6_next = satosin6(opts->ip6po_nexthop); 379 380 /* at this moment, we only support AF_INET6 next hops */ 381 if (sin6_next->sin6_family != AF_INET6) { 382 error = EAFNOSUPPORT; /* or should we proceed? */ 383 goto done; 384 } 385 386 /* 387 * If the next hop is an IPv6 address, then the node identified 388 * by that address must be a neighbor of the sending host. 389 */ 390 ron = &opts->ip6po_nextroute; 391 if ((ron->ro_rt && 392 (ron->ro_rt->rt_flags & (RTF_UP | RTF_GATEWAY)) != 393 RTF_UP) || 394 !IN6_ARE_ADDR_EQUAL(&ron->ro_dst.sin6_addr, 395 &sin6_next->sin6_addr)) { 396 if (ron->ro_rt) { 397 RTFREE(ron->ro_rt); 398 ron->ro_rt = NULL; 399 } 400 ron->ro_dst = *sin6_next; 401 ron->ro_tableid = rtableid; 402 } 403 if (ron->ro_rt == NULL) { 404 rtalloc((struct route *)ron); /* multi path case? */ 405 if (ron->ro_rt == NULL || 406 (ron->ro_rt->rt_flags & RTF_GATEWAY)) { 407 if (ron->ro_rt) { 408 RTFREE(ron->ro_rt); 409 ron->ro_rt = NULL; 410 } 411 error = EHOSTUNREACH; 412 goto done; 413 } 414 } 415 if (!nd6_is_addr_neighbor(sin6_next, ron->ro_rt->rt_ifp)) { 416 RTFREE(ron->ro_rt); 417 ron->ro_rt = NULL; 418 error = EHOSTUNREACH; 419 goto done; 420 } 421 rt = ron->ro_rt; 422 ifp = rt->rt_ifp; 423 424 /* 425 * When cloning is required, try to allocate a route to the 426 * destination so that the caller can store path MTU 427 * information. 428 */ 429 goto done; 430 } 431 432 /* 433 * Use a cached route if it exists and is valid, else try to allocate 434 * a new one. Note that we should check the address family of the 435 * cached destination, in case of sharing the cache with IPv4. 436 */ 437 if (ro) { 438 if (ro->ro_rt && 439 (!(ro->ro_rt->rt_flags & RTF_UP) || 440 ((struct sockaddr *)(&ro->ro_dst))->sa_family != AF_INET6 || 441 !IN6_ARE_ADDR_EQUAL(&ro->ro_dst.sin6_addr, dst))) { 442 RTFREE(ro->ro_rt); 443 ro->ro_rt = (struct rtentry *)NULL; 444 } 445 if (ro->ro_rt == (struct rtentry *)NULL) { 446 struct sockaddr_in6 *sa6; 447 448 /* No route yet, so try to acquire one */ 449 bzero(&ro->ro_dst, sizeof(struct sockaddr_in6)); 450 sa6 = (struct sockaddr_in6 *)&ro->ro_dst; 451 *sa6 = *dstsock; 452 sa6->sin6_scope_id = 0; 453 ro->ro_tableid = rtableid; 454 rtalloc_mpath((struct route *)ro, NULL); 455 } 456 457 /* 458 * do not care about the result if we have the nexthop 459 * explicitly specified. 460 */ 461 if (opts && opts->ip6po_nexthop) 462 goto done; 463 464 if (ro->ro_rt) { 465 ifp = ro->ro_rt->rt_ifp; 466 467 if (ifp == NULL) { /* can this really happen? */ 468 RTFREE(ro->ro_rt); 469 ro->ro_rt = NULL; 470 } 471 } 472 if (ro->ro_rt == NULL) 473 error = EHOSTUNREACH; 474 rt = ro->ro_rt; 475 476 /* 477 * Check if the outgoing interface conflicts with 478 * the interface specified by ipi6_ifindex (if specified). 479 * Note that loopback interface is always okay. 480 * (this may happen when we are sending a packet to one of 481 * our own addresses.) 482 */ 483 if (opts && opts->ip6po_pktinfo && 484 opts->ip6po_pktinfo->ipi6_ifindex) { 485 if (!(ifp->if_flags & IFF_LOOPBACK) && 486 ifp->if_index != 487 opts->ip6po_pktinfo->ipi6_ifindex) { 488 error = EHOSTUNREACH; 489 goto done; 490 } 491 } 492 } 493 494 done: 495 if (ifp == NULL && rt == NULL) { 496 /* 497 * This can happen if the caller did not pass a cached route 498 * nor any other hints. We treat this case an error. 499 */ 500 error = EHOSTUNREACH; 501 } 502 if (error == EHOSTUNREACH) 503 ip6stat.ip6s_noroute++; 504 505 if (retifp != NULL) 506 *retifp = ifp; 507 if (retrt != NULL) 508 *retrt = rt; /* rt may be NULL */ 509 510 return (error); 511} 512 513int 514in6_selectif(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts, 515 struct ip6_moptions *mopts, struct route_in6 *ro, struct ifnet **retifp, 516 u_int rtableid) 517{ 518 struct rtentry *rt = NULL; 519 int error; 520 521 if ((error = selectroute(dstsock, opts, mopts, ro, retifp, 522 &rt, 1, rtableid)) != 0) 523 return (error); 524 525 /* 526 * do not use a rejected or black hole route. 527 * XXX: this check should be done in the L2 output routine. 528 * However, if we skipped this check here, we'd see the following 529 * scenario: 530 * - install a rejected route for a scoped address prefix 531 * (like fe80::/10) 532 * - send a packet to a destination that matches the scoped prefix, 533 * with ambiguity about the scope zone. 534 * - pick the outgoing interface from the route, and disambiguate the 535 * scope zone with the interface. 536 * - ip6_output() would try to get another route with the "new" 537 * destination, which may be valid. 538 * - we'd see no error on output. 539 * Although this may not be very harmful, it should still be confusing. 540 * We thus reject the case here. 541 */ 542 if (rt && (rt->rt_flags & (RTF_REJECT | RTF_BLACKHOLE))) 543 return (rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH); 544 545 /* 546 * Adjust the "outgoing" interface. If we're going to loop the packet 547 * back to ourselves, the ifp would be the loopback interface. 548 * However, we'd rather know the interface associated to the 549 * destination address (which should probably be one of our own 550 * addresses.) 551 */ 552 if (rt && rt->rt_ifa && rt->rt_ifa->ifa_ifp) 553 *retifp = rt->rt_ifa->ifa_ifp; 554 555 return (0); 556} 557 558int 559in6_selectroute(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts, 560 struct ip6_moptions *mopts, struct route_in6 *ro, struct ifnet **retifp, 561 struct rtentry **retrt, u_int rtableid) 562{ 563 564 return (selectroute(dstsock, opts, mopts, ro, retifp, retrt, 0, 565 rtableid)); 566} 567 568/* 569 * Default hop limit selection. The precedence is as follows: 570 * 1. Hoplimit value specified via ioctl. 571 * 2. (If the outgoing interface is detected) the current 572 * hop limit of the interface specified by router advertisement. 573 * 3. The system default hoplimit. 574*/ 575#define in6pcb inpcb 576#define in6p_hops inp_hops 577int 578in6_selecthlim(struct in6pcb *in6p, struct ifnet *ifp) 579{ 580 if (in6p && in6p->in6p_hops >= 0) 581 return (in6p->in6p_hops); 582 else if (ifp) 583 return (ND_IFINFO(ifp)->chlim); 584 else 585 return (ip6_defhlim); 586} 587#undef in6pcb 588#undef in6p_hops 589 590/* 591 * generate kernel-internal form (scopeid embedded into s6_addr16[1]). 592 * If the address scope of is link-local, embed the interface index in the 593 * address. The routine determines our precedence 594 * between advanced API scope/interface specification and basic API 595 * specification. 596 * 597 * this function should be nuked in the future, when we get rid of 598 * embedded scopeid thing. 599 * 600 * XXX actually, it is over-specification to return ifp against sin6_scope_id. 601 * there can be multiple interfaces that belong to a particular scope zone 602 * (in specification, we have 1:N mapping between a scope zone and interfaces). 603 * we may want to change the function to return something other than ifp. 604 */ 605int 606in6_embedscope(in6, sin6, in6p, ifpp) 607 struct in6_addr *in6; 608 const struct sockaddr_in6 *sin6; 609 struct inpcb *in6p; 610#define in6p_outputopts inp_outputopts6 611#define in6p_moptions inp_moptions6 612 struct ifnet **ifpp; 613{ 614 struct ifnet *ifp = NULL; 615 u_int32_t scopeid; 616 617 *in6 = sin6->sin6_addr; 618 scopeid = sin6->sin6_scope_id; 619 if (ifpp) 620 *ifpp = NULL; 621 622 /* 623 * don't try to read sin6->sin6_addr beyond here, since the caller may 624 * ask us to overwrite existing sockaddr_in6 625 */ 626 627 if (IN6_IS_SCOPE_EMBED(in6)) { 628 struct in6_pktinfo *pi; 629 630 /* 631 * KAME assumption: link id == interface id 632 */ 633 634 if (in6p && in6p->in6p_outputopts && 635 (pi = in6p->in6p_outputopts->ip6po_pktinfo) && 636 pi->ipi6_ifindex) { 637 ifp = if_get(pi->ipi6_ifindex); 638 if (ifp == NULL) 639 return ENXIO; /* XXX EINVAL? */ 640 in6->s6_addr16[1] = htons(pi->ipi6_ifindex); 641 } else if (in6p && IN6_IS_ADDR_MULTICAST(in6) && 642 in6p->in6p_moptions && 643 in6p->in6p_moptions->im6o_multicast_ifp) { 644 ifp = in6p->in6p_moptions->im6o_multicast_ifp; 645 in6->s6_addr16[1] = htons(ifp->if_index); 646 } else if (scopeid) { 647 ifp = if_get(scopeid); 648 if (ifp == NULL) 649 return ENXIO; /* XXX EINVAL? */ 650 /*XXX assignment to 16bit from 32bit variable */ 651 in6->s6_addr16[1] = htons(scopeid & 0xffff); 652 } 653 654 if (ifpp) 655 *ifpp = ifp; 656 } 657 658 return 0; 659} 660#undef in6p_outputopts 661#undef in6p_moptions 662 663/* 664 * generate standard sockaddr_in6 from embedded form. 665 * touches sin6_addr and sin6_scope_id only. 666 * 667 * this function should be nuked in the future, when we get rid of 668 * embedded scopeid thing. 669 */ 670int 671in6_recoverscope(struct sockaddr_in6 *sin6, const struct in6_addr *in6, 672 struct ifnet *ifp) 673{ 674 u_int32_t scopeid; 675 676 sin6->sin6_addr = *in6; 677 678 /* 679 * don't try to read *in6 beyond here, since the caller may 680 * ask us to overwrite existing sockaddr_in6 681 */ 682 683 sin6->sin6_scope_id = 0; 684 if (IN6_IS_SCOPE_EMBED(in6)) { 685 /* 686 * KAME assumption: link id == interface id 687 */ 688 scopeid = ntohs(sin6->sin6_addr.s6_addr16[1]); 689 if (scopeid) { 690 /* sanity check */ 691 if (if_get(scopeid) == NULL) 692 return ENXIO; 693 if (ifp && ifp->if_index != scopeid) 694 return ENXIO; 695 sin6->sin6_addr.s6_addr16[1] = 0; 696 sin6->sin6_scope_id = scopeid; 697 } 698 } 699 700 return 0; 701} 702 703/* 704 * just clear the embedded scope identifer. 705 */ 706void 707in6_clearscope(struct in6_addr *addr) 708{ 709 if (IN6_IS_SCOPE_EMBED(addr)) 710 addr->s6_addr16[1] = 0; 711} 712