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