in6_src.c revision 96972
1139749Simp/* $FreeBSD: head/sys/netinet6/in6_src.c 96972 2002-05-20 05:41:09Z tanimura $ */ 265942Sgibbs/* $KAME: in6_src.c,v 1.37 2001/03/29 05:34:31 itojun Exp $ */ 365942Sgibbs 439221Sgibbs/* 571717Sgibbs * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 639221Sgibbs * All rights reserved. 739221Sgibbs * 839221Sgibbs * Redistribution and use in source and binary forms, with or without 939221Sgibbs * modification, are permitted provided that the following conditions 1039221Sgibbs * are met: 1139221Sgibbs * 1. Redistributions of source code must retain the above copyright 1239221Sgibbs * notice, this list of conditions and the following disclaimer. 1339221Sgibbs * 2. Redistributions in binary form must reproduce the above copyright 1439221Sgibbs * notice, this list of conditions and the following disclaimer in the 1539221Sgibbs * documentation and/or other materials provided with the distribution. 1639221Sgibbs * 3. Neither the name of the project nor the names of its contributors 1739221Sgibbs * may be used to endorse or promote products derived from this software 1839221Sgibbs * without specific prior written permission. 1939221Sgibbs * 2039221Sgibbs * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 2139221Sgibbs * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2239221Sgibbs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2339221Sgibbs * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 2439221Sgibbs * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2539221Sgibbs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2639221Sgibbs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2739221Sgibbs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2839221Sgibbs * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29123579Sgibbs * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3039221Sgibbs * SUCH DAMAGE. 3139221Sgibbs */ 32119418Sobrien 33119418Sobrien/* 34119418Sobrien * Copyright (c) 1982, 1986, 1991, 1993 3595378Sgibbs * The Regents of the University of California. All rights reserved. 3639221Sgibbs * 3755953Speter * Redistribution and use in source and binary forms, with or without 3839221Sgibbs * modification, are permitted provided that the following conditions 3939221Sgibbs * are met: 4045791Speter * 1. Redistributions of source code must retain the above copyright 4139221Sgibbs * notice, this list of conditions and the following disclaimer. 4265942Sgibbs * 2. Redistributions in binary form must reproduce the above copyright 4365942Sgibbs * notice, this list of conditions and the following disclaimer in the 4463457Sgibbs * documentation and/or other materials provided with the distribution. 4565942Sgibbs * 3. All advertising materials mentioning features or use of this software 4665942Sgibbs * must display the following acknowledgement: 4765942Sgibbs * This product includes software developed by the University of 4865942Sgibbs * California, Berkeley and its contributors. 4965942Sgibbs * 4. Neither the name of the University nor the names of its contributors 5065942Sgibbs * may be used to endorse or promote products derived from this software 5165942Sgibbs * without specific prior written permission. 5265942Sgibbs * 5339221Sgibbs * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 5465942Sgibbs * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 5565942Sgibbs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 5645791Speter * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 5765942Sgibbs * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 5839221Sgibbs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 5956823Speter * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 6045791Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 6165942Sgibbs * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 6239221Sgibbs * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 6371390Sgibbs * SUCH DAMAGE. 64127135Snjl * 6565942Sgibbs * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94 6665942Sgibbs */ 6765942Sgibbs 6865942Sgibbs#include "opt_inet.h" 6939221Sgibbs#include "opt_inet6.h" 7065942Sgibbs 7165942Sgibbs#include <sys/param.h> 7265942Sgibbs#include <sys/systm.h> 7365942Sgibbs#include <sys/malloc.h> 7465942Sgibbs#include <sys/mbuf.h> 7565942Sgibbs#include <sys/protosw.h> 7665942Sgibbs#include <sys/socket.h> 7765942Sgibbs#include <sys/socketvar.h> 7865942Sgibbs#include <sys/errno.h> 7965942Sgibbs#include <sys/time.h> 8065942Sgibbs 8165942Sgibbs#include <net/if.h> 8265942Sgibbs#include <net/route.h> 8365942Sgibbs 8445791Speter#include <netinet/in.h> 8545791Speter#include <netinet/in_var.h> 8645791Speter#include <netinet/in_systm.h> 8745791Speter#include <netinet/ip.h> 8845791Speter#include <netinet/in_pcb.h> 8945791Speter#include <netinet6/in6_var.h> 9045791Speter#include <netinet/ip6.h> 9165942Sgibbs#include <netinet6/in6_pcb.h> 9245791Speter#include <netinet6/ip6_var.h> 9365942Sgibbs#include <netinet6/nd6.h> 9465942Sgibbs#ifdef ENABLE_DEFAULT_SCOPE 9565942Sgibbs#include <netinet6/scope6_var.h> 9639221Sgibbs#endif 9745791Speter 9865942Sgibbs#include <net/net_osdep.h> 9965942Sgibbs 10045791Speter/* 10165942Sgibbs * Return an IPv6 address, which is the most appropriate for a given 10265942Sgibbs * destination and user specified options. 10339221Sgibbs * If necessary, this function lookups the routing table and returns 10439221Sgibbs * an entry to the caller for later use. 10539221Sgibbs */ 10645791Speterstruct in6_addr * 10739221Sgibbsin6_selectsrc(dstsock, opts, mopts, ro, laddr, errorp) 10865942Sgibbs struct sockaddr_in6 *dstsock; 10965942Sgibbs struct ip6_pktopts *opts; 11065942Sgibbs struct ip6_moptions *mopts; 11165942Sgibbs struct route_in6 *ro; 11239221Sgibbs struct in6_addr *laddr; 11365942Sgibbs int *errorp; 11465942Sgibbs{ 11565942Sgibbs struct in6_addr *dst; 11639221Sgibbs struct in6_ifaddr *ia6 = 0; 11765942Sgibbs struct in6_pktinfo *pi = NULL; 11865942Sgibbs 11965942Sgibbs dst = &dstsock->sin6_addr; 12065942Sgibbs *errorp = 0; 12165942Sgibbs 12265942Sgibbs /* 12365942Sgibbs * If the source address is explicitly specified by the caller, 12465942Sgibbs * use it. 12565942Sgibbs */ 12671390Sgibbs if (opts && (pi = opts->ip6po_pktinfo) && 12765942Sgibbs !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr)) 12865942Sgibbs return(&pi->ipi6_addr); 12965942Sgibbs 13071390Sgibbs /* 13171390Sgibbs * If the source address is not specified but the socket(if any) 13265942Sgibbs * is already bound, use the bound address. 133161928Sjmg */ 134161928Sjmg if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr)) 13539221Sgibbs return(laddr); 13639221Sgibbs 13739221Sgibbs /* 138108479Sscottl * If the caller doesn't specify the source address but 139108479Sscottl * the outgoing interface, use an address associated with 14039221Sgibbs * the interface. 141114618Sgibbs */ 14265942Sgibbs if (pi && pi->ipi6_ifindex) { 14339221Sgibbs /* XXX boundary check is assumed to be already done. */ 14439221Sgibbs ia6 = in6_ifawithscope(ifnet_byindex(pi->ipi6_ifindex), dst); 14545967Sgibbs if (ia6 == 0) { 14645967Sgibbs *errorp = EADDRNOTAVAIL; 14765942Sgibbs return(0); 14865942Sgibbs } 14939221Sgibbs return(&satosin6(&ia6->ia_addr)->sin6_addr); 15065942Sgibbs } 15195378Sgibbs 15265942Sgibbs /* 15365942Sgibbs * If the destination address is a link-local unicast address or 15465942Sgibbs * a multicast address, and if the outgoing interface is specified 15547274Sgibbs * by the sin6_scope_id filed, use an address associated with the 15647274Sgibbs * interface. 15739221Sgibbs * XXX: We're now trying to define more specific semantics of 15865942Sgibbs * sin6_scope_id field, so this part will be rewritten in 15939221Sgibbs * the near future. 16039221Sgibbs */ 16139221Sgibbs if ((IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MULTICAST(dst)) && 16295378Sgibbs dstsock->sin6_scope_id) { 16345791Speter /* 16445791Speter * I'm not sure if boundary check for scope_id is done 16545791Speter * somewhere... 16670204Sgibbs */ 16745791Speter if (dstsock->sin6_scope_id < 0 || 16845791Speter if_index < dstsock->sin6_scope_id) { 16945791Speter *errorp = ENXIO; /* XXX: better error? */ 17045791Speter return(0); 171103811Sscottl } 17295378Sgibbs ia6 = in6_ifawithscope(ifnet_byindex(dstsock->sin6_scope_id), 17370204Sgibbs dst); 17445791Speter if (ia6 == 0) { 17545791Speter *errorp = EADDRNOTAVAIL; 176103811Sscottl return(0); 17776634Sgibbs } 17876634Sgibbs return(&satosin6(&ia6->ia_addr)->sin6_addr); 179 } 180 181 /* 182 * If the destination address is a multicast address and 183 * the outgoing interface for the address is specified 184 * by the caller, use an address associated with the interface. 185 * There is a sanity check here; if the destination has node-local 186 * scope, the outgoing interfacde should be a loopback address. 187 * Even if the outgoing interface is not specified, we also 188 * choose a loopback interface as the outgoing interface. 189 */ 190 if (IN6_IS_ADDR_MULTICAST(dst)) { 191 struct ifnet *ifp = mopts ? mopts->im6o_multicast_ifp : NULL; 192 193 if (ifp == NULL && IN6_IS_ADDR_MC_NODELOCAL(dst)) { 194 ifp = &loif[0]; 195 } 196 197 if (ifp) { 198 ia6 = in6_ifawithscope(ifp, dst); 199 if (ia6 == 0) { 200 *errorp = EADDRNOTAVAIL; 201 return(0); 202 } 203 return(&satosin6(&ia6->ia_addr)->sin6_addr); 204 } 205 } 206 207 /* 208 * If the next hop address for the packet is specified 209 * by caller, use an address associated with the route 210 * to the next hop. 211 */ 212 { 213 struct sockaddr_in6 *sin6_next; 214 struct rtentry *rt; 215 216 if (opts && opts->ip6po_nexthop) { 217 sin6_next = satosin6(opts->ip6po_nexthop); 218 rt = nd6_lookup(&sin6_next->sin6_addr, 1, NULL); 219 if (rt) { 220 ia6 = in6_ifawithscope(rt->rt_ifp, dst); 221 if (ia6 == 0) 222 ia6 = ifatoia6(rt->rt_ifa); 223 } 224 if (ia6 == 0) { 225 *errorp = EADDRNOTAVAIL; 226 return(0); 227 } 228 return(&satosin6(&ia6->ia_addr)->sin6_addr); 229 } 230 } 231 232 /* 233 * If route is known or can be allocated now, 234 * our src addr is taken from the i/f, else punt. 235 */ 236 if (ro) { 237 if (ro->ro_rt && 238 (!(ro->ro_rt->rt_flags & RTF_UP) || 239 satosin6(&ro->ro_dst)->sin6_family != AF_INET6 || 240 !IN6_ARE_ADDR_EQUAL(&satosin6(&ro->ro_dst)->sin6_addr, 241 dst))) { 242 RTFREE(ro->ro_rt); 243 ro->ro_rt = (struct rtentry *)0; 244 } 245 if (ro->ro_rt == (struct rtentry *)0 || 246 ro->ro_rt->rt_ifp == (struct ifnet *)0) { 247 struct sockaddr_in6 *sa6; 248 249 /* No route yet, so try to acquire one */ 250 bzero(&ro->ro_dst, sizeof(struct sockaddr_in6)); 251 sa6 = (struct sockaddr_in6 *)&ro->ro_dst; 252 sa6->sin6_family = AF_INET6; 253 sa6->sin6_len = sizeof(struct sockaddr_in6); 254 sa6->sin6_addr = *dst; 255 sa6->sin6_scope_id = dstsock->sin6_scope_id; 256 if (IN6_IS_ADDR_MULTICAST(dst)) { 257 ro->ro_rt = rtalloc1(&((struct route *)ro) 258 ->ro_dst, 0, 0UL); 259 } else { 260 rtalloc((struct route *)ro); 261 } 262 } 263 264 /* 265 * in_pcbconnect() checks out IFF_LOOPBACK to skip using 266 * the address. But we don't know why it does so. 267 * It is necessary to ensure the scope even for lo0 268 * so doesn't check out IFF_LOOPBACK. 269 */ 270 271 if (ro->ro_rt) { 272 ia6 = in6_ifawithscope(ro->ro_rt->rt_ifa->ifa_ifp, dst); 273 if (ia6 == 0) /* xxx scope error ?*/ 274 ia6 = ifatoia6(ro->ro_rt->rt_ifa); 275 } 276#if 0 277 /* 278 * xxx The followings are necessary? (kazu) 279 * I don't think so. 280 * It's for SO_DONTROUTE option in IPv4.(jinmei) 281 */ 282 if (ia6 == 0) { 283 struct sockaddr_in6 sin6 = {sizeof(sin6), AF_INET6, 0}; 284 285 sin6->sin6_addr = *dst; 286 287 ia6 = ifatoia6(ifa_ifwithdstaddr(sin6tosa(&sin6))); 288 if (ia6 == 0) 289 ia6 = ifatoia6(ifa_ifwithnet(sin6tosa(&sin6))); 290 if (ia6 == 0) 291 return(0); 292 return(&satosin6(&ia6->ia_addr)->sin6_addr); 293 } 294#endif /* 0 */ 295 if (ia6 == 0) { 296 *errorp = EHOSTUNREACH; /* no route */ 297 return(0); 298 } 299 return(&satosin6(&ia6->ia_addr)->sin6_addr); 300 } 301 302 *errorp = EADDRNOTAVAIL; 303 return(0); 304} 305 306/* 307 * Default hop limit selection. The precedence is as follows: 308 * 1. Hoplimit value specified via ioctl. 309 * 2. (If the outgoing interface is detected) the current 310 * hop limit of the interface specified by router advertisement. 311 * 3. The system default hoplimit. 312*/ 313int 314in6_selecthlim(in6p, ifp) 315 struct in6pcb *in6p; 316 struct ifnet *ifp; 317{ 318 if (in6p && in6p->in6p_hops >= 0) 319 return(in6p->in6p_hops); 320 else if (ifp) 321 return(nd_ifinfo[ifp->if_index].chlim); 322 else 323 return(ip6_defhlim); 324} 325 326/* 327 * XXX: this is borrowed from in6_pcbbind(). If possible, we should 328 * share this function by all *bsd*... 329 */ 330int 331in6_pcbsetport(laddr, inp, td) 332 struct in6_addr *laddr; 333 struct inpcb *inp; 334 struct thread *td; 335{ 336 struct socket *so = inp->inp_socket; 337 u_int16_t lport = 0, first, last, *lastport; 338 int count, error = 0, wild = 0; 339 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 340 341 SOCK_LOCK(so); 342 /* XXX: this is redundant when called from in6_pcbbind */ 343 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0) 344 wild = INPLOOKUP_WILDCARD; 345 SOCK_UNLOCK(so); 346 347 inp->inp_flags |= INP_ANONPORT; 348 349 if (inp->inp_flags & INP_HIGHPORT) { 350 first = ipport_hifirstauto; /* sysctl */ 351 last = ipport_hilastauto; 352 lastport = &pcbinfo->lasthi; 353 } else if (inp->inp_flags & INP_LOWPORT) { 354 if (td && (error = suser(td))) 355 return error; 356 first = ipport_lowfirstauto; /* 1023 */ 357 last = ipport_lowlastauto; /* 600 */ 358 lastport = &pcbinfo->lastlow; 359 } else { 360 first = ipport_firstauto; /* sysctl */ 361 last = ipport_lastauto; 362 lastport = &pcbinfo->lastport; 363 } 364 /* 365 * Simple check to ensure all ports are not used up causing 366 * a deadlock here. 367 * 368 * We split the two cases (up and down) so that the direction 369 * is not being tested on each round of the loop. 370 */ 371 if (first > last) { 372 /* 373 * counting down 374 */ 375 count = first - last; 376 377 do { 378 if (count-- < 0) { /* completely used? */ 379 /* 380 * Undo any address bind that may have 381 * occurred above. 382 */ 383 inp->in6p_laddr = in6addr_any; 384 return (EAGAIN); 385 } 386 --*lastport; 387 if (*lastport > first || *lastport < last) 388 *lastport = first; 389 lport = htons(*lastport); 390 } while (in6_pcblookup_local(pcbinfo, 391 &inp->in6p_laddr, lport, wild)); 392 } else { 393 /* 394 * counting up 395 */ 396 count = last - first; 397 398 do { 399 if (count-- < 0) { /* completely used? */ 400 /* 401 * Undo any address bind that may have 402 * occurred above. 403 */ 404 inp->in6p_laddr = in6addr_any; 405 return (EAGAIN); 406 } 407 ++*lastport; 408 if (*lastport < first || *lastport > last) 409 *lastport = first; 410 lport = htons(*lastport); 411 } while (in6_pcblookup_local(pcbinfo, 412 &inp->in6p_laddr, lport, wild)); 413 } 414 415 inp->inp_lport = lport; 416 if (in_pcbinshash(inp) != 0) { 417 inp->in6p_laddr = in6addr_any; 418 inp->inp_lport = 0; 419 return (EAGAIN); 420 } 421 422 return(0); 423} 424 425/* 426 * generate kernel-internal form (scopeid embedded into s6_addr16[1]). 427 * If the address scope of is link-local, embed the interface index in the 428 * address. The routine determines our precedence 429 * between advanced API scope/interface specification and basic API 430 * specification. 431 * 432 * this function should be nuked in the future, when we get rid of 433 * embedded scopeid thing. 434 * 435 * XXX actually, it is over-specification to return ifp against sin6_scope_id. 436 * there can be multiple interfaces that belong to a particular scope zone 437 * (in specification, we have 1:N mapping between a scope zone and interfaces). 438 * we may want to change the function to return something other than ifp. 439 */ 440int 441in6_embedscope(in6, sin6, in6p, ifpp) 442 struct in6_addr *in6; 443 const struct sockaddr_in6 *sin6; 444#ifdef HAVE_NRL_INPCB 445 struct inpcb *in6p; 446#define in6p_outputopts inp_outputopts6 447#define in6p_moptions inp_moptions6 448#else 449 struct in6pcb *in6p; 450#endif 451 struct ifnet **ifpp; 452{ 453 struct ifnet *ifp = NULL; 454 u_int32_t scopeid; 455 456 *in6 = sin6->sin6_addr; 457 scopeid = sin6->sin6_scope_id; 458 if (ifpp) 459 *ifpp = NULL; 460 461 /* 462 * don't try to read sin6->sin6_addr beyond here, since the caller may 463 * ask us to overwrite existing sockaddr_in6 464 */ 465 466#ifdef ENABLE_DEFAULT_SCOPE 467 if (scopeid == 0) 468 scopeid = scope6_addr2default(in6); 469#endif 470 471 if (IN6_IS_SCOPE_LINKLOCAL(in6)) { 472 struct in6_pktinfo *pi; 473 474 /* 475 * KAME assumption: link id == interface id 476 */ 477 478 if (in6p && in6p->in6p_outputopts && 479 (pi = in6p->in6p_outputopts->ip6po_pktinfo) && 480 pi->ipi6_ifindex) { 481 ifp = ifnet_byindex(pi->ipi6_ifindex); 482 in6->s6_addr16[1] = htons(pi->ipi6_ifindex); 483 } else if (in6p && IN6_IS_ADDR_MULTICAST(in6) && 484 in6p->in6p_moptions && 485 in6p->in6p_moptions->im6o_multicast_ifp) { 486 ifp = in6p->in6p_moptions->im6o_multicast_ifp; 487 in6->s6_addr16[1] = htons(ifp->if_index); 488 } else if (scopeid) { 489 /* boundary check */ 490 if (scopeid < 0 || if_index < scopeid) 491 return ENXIO; /* XXX EINVAL? */ 492 ifp = ifnet_byindex(scopeid); 493 /*XXX assignment to 16bit from 32bit variable */ 494 in6->s6_addr16[1] = htons(scopeid & 0xffff); 495 } 496 497 if (ifpp) 498 *ifpp = ifp; 499 } 500 501 return 0; 502} 503#ifdef HAVE_NRL_INPCB 504#undef in6p_outputopts 505#undef in6p_moptions 506#endif 507 508/* 509 * generate standard sockaddr_in6 from embedded form. 510 * touches sin6_addr and sin6_scope_id only. 511 * 512 * this function should be nuked in the future, when we get rid of 513 * embedded scopeid thing. 514 */ 515int 516in6_recoverscope(sin6, in6, ifp) 517 struct sockaddr_in6 *sin6; 518 const struct in6_addr *in6; 519 struct ifnet *ifp; 520{ 521 u_int32_t scopeid; 522 523 sin6->sin6_addr = *in6; 524 525 /* 526 * don't try to read *in6 beyond here, since the caller may 527 * ask us to overwrite existing sockaddr_in6 528 */ 529 530 sin6->sin6_scope_id = 0; 531 if (IN6_IS_SCOPE_LINKLOCAL(in6)) { 532 /* 533 * KAME assumption: link id == interface id 534 */ 535 scopeid = ntohs(sin6->sin6_addr.s6_addr16[1]); 536 if (scopeid) { 537 /* sanity check */ 538 if (scopeid < 0 || if_index < scopeid) 539 return ENXIO; 540 if (ifp && ifp->if_index != scopeid) 541 return ENXIO; 542 sin6->sin6_addr.s6_addr16[1] = 0; 543 sin6->sin6_scope_id = scopeid; 544 } 545 } 546 547 return 0; 548} 549 550/* 551 * just clear the embedded scope identifer. 552 * XXX: currently used for bsdi4 only as a supplement function. 553 */ 554void 555in6_clearscope(addr) 556 struct in6_addr *addr; 557{ 558 if (IN6_IS_SCOPE_LINKLOCAL(addr)) 559 addr->s6_addr16[1] = 0; 560} 561