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