in6_src.c revision 121445
1118174Sache/* $FreeBSD: head/sys/netinet6/in6_src.c 121445 2003-10-23 21:41:00Z sam $ */ 2118174Sache/* $KAME: in6_src.c,v 1.37 2001/03/29 05:34:31 itojun Exp $ */ 3118174Sache 4118174Sache/* 5118174Sache * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 6174990Sache * All rights reserved. 7118174Sache * 8118174Sache * Redistribution and use in source and binary forms, with or without 9118174Sache * modification, are permitted provided that the following conditions 10118174Sache * are met: 11118174Sache * 1. Redistributions of source code must retain the above copyright 12118174Sache * notice, this list of conditions and the following disclaimer. 13118174Sache * 2. Redistributions in binary form must reproduce the above copyright 14118174Sache * notice, this list of conditions and the following disclaimer in the 15118174Sache * documentation and/or other materials provided with the distribution. 16118174Sache * 3. Neither the name of the project nor the names of its contributors 17118174Sache * may be used to endorse or promote products derived from this software 18118174Sache * without specific prior written permission. 19118174Sache * 20118174Sache * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 21174990Sache * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22118174Sache * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23118174Sache * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24118174Sache * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25118174Sache * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26118174Sache * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27118174Sache * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28118174Sache * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29118174Sache * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30118174Sache * SUCH DAMAGE. 31118174Sache */ 32118174Sache 33118174Sache/* 34118174Sache * Copyright (c) 1982, 1986, 1991, 1993 35118174Sache * The Regents of the University of California. All rights reserved. 36118174Sache * 37118174Sache * Redistribution and use in source and binary forms, with or without 38119374Sache * modification, are permitted provided that the following conditions 39119374Sache * are met: 40119374Sache * 1. Redistributions of source code must retain the above copyright 41119374Sache * notice, this list of conditions and the following disclaimer. 42119374Sache * 2. Redistributions in binary form must reproduce the above copyright 43119374Sache * notice, this list of conditions and the following disclaimer in the 44119374Sache * documentation and/or other materials provided with the distribution. 45118174Sache * 3. All advertising materials mentioning features or use of this software 46118174Sache * must display the following acknowledgement: 47118174Sache * This product includes software developed by the University of 48118174Sache * California, Berkeley and its contributors. 49118174Sache * 4. Neither the name of the University nor the names of its contributors 50118174Sache * may be used to endorse or promote products derived from this software 51118174Sache * without specific prior written permission. 52118174Sache * 53118174Sache * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 54118174Sache * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 55118174Sache * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 56118174Sache * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 57118174Sache * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 58118174Sache * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 59118174Sache * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 60118174Sache * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 61118174Sache * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 62118174Sache * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 63118174Sache * SUCH DAMAGE. 64118174Sache * 65118174Sache * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94 66118174Sache */ 67118174Sache 68118174Sache#include "opt_inet.h" 69118174Sache#include "opt_inet6.h" 70118174Sache 71118174Sache#include <sys/param.h> 72118174Sache#include <sys/systm.h> 73118174Sache#include <sys/malloc.h> 74118174Sache#include <sys/mbuf.h> 75118174Sache#include <sys/protosw.h> 76118174Sache#include <sys/socket.h> 77118174Sache#include <sys/socketvar.h> 78118174Sache#include <sys/errno.h> 79118174Sache#include <sys/time.h> 80174990Sache 81118174Sache#include <net/if.h> 82118174Sache#include <net/route.h> 83118174Sache 84118174Sache#include <netinet/in.h> 85118174Sache#include <netinet/in_var.h> 86118174Sache#include <netinet/in_systm.h> 87118174Sache#include <netinet/ip.h> 88118174Sache#include <netinet/in_pcb.h> 89118174Sache#include <netinet6/in6_var.h> 90118174Sache#include <netinet/ip6.h> 91118174Sache#include <netinet6/in6_pcb.h> 92118174Sache#include <netinet6/ip6_var.h> 93118174Sache#include <netinet6/nd6.h> 94118174Sache#ifdef ENABLE_DEFAULT_SCOPE 95118174Sache#include <netinet6/scope6_var.h> 96118174Sache#endif 97118174Sache 98118174Sache#include <net/net_osdep.h> 99118174Sache 100118174Sache/* 101118174Sache * Return an IPv6 address, which is the most appropriate for a given 102118174Sache * destination and user specified options. 103 * If necessary, this function lookups the routing table and returns 104 * an entry to the caller for later use. 105 */ 106struct in6_addr * 107in6_selectsrc(dstsock, opts, mopts, ro, laddr, errorp) 108 struct sockaddr_in6 *dstsock; 109 struct ip6_pktopts *opts; 110 struct ip6_moptions *mopts; 111 struct route_in6 *ro; 112 struct in6_addr *laddr; 113 int *errorp; 114{ 115 struct in6_addr *dst; 116 struct in6_ifaddr *ia6 = 0; 117 struct in6_pktinfo *pi = NULL; 118 119 dst = &dstsock->sin6_addr; 120 *errorp = 0; 121 122 /* 123 * If the source address is explicitly specified by the caller, 124 * use it. 125 */ 126 if (opts && (pi = opts->ip6po_pktinfo) && 127 !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr)) 128 return (&pi->ipi6_addr); 129 130 /* 131 * If the source address is not specified but the socket(if any) 132 * is already bound, use the bound address. 133 */ 134 if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr)) 135 return (laddr); 136 137 /* 138 * If the caller doesn't specify the source address but 139 * the outgoing interface, use an address associated with 140 * the interface. 141 */ 142 if (pi && pi->ipi6_ifindex) { 143 /* XXX boundary check is assumed to be already done. */ 144 ia6 = in6_ifawithscope(ifnet_byindex(pi->ipi6_ifindex), dst); 145 if (ia6 == 0) { 146 *errorp = EADDRNOTAVAIL; 147 return (0); 148 } 149 return (&satosin6(&ia6->ia_addr)->sin6_addr); 150 } 151 152 /* 153 * If the destination address is a link-local unicast address or 154 * a multicast address, and if the outgoing interface is specified 155 * by the sin6_scope_id filed, use an address associated with the 156 * interface. 157 * XXX: We're now trying to define more specific semantics of 158 * sin6_scope_id field, so this part will be rewritten in 159 * the near future. 160 */ 161 if ((IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MULTICAST(dst)) && 162 dstsock->sin6_scope_id) { 163 /* 164 * I'm not sure if boundary check for scope_id is done 165 * somewhere... 166 */ 167 if (dstsock->sin6_scope_id < 0 || 168 if_index < dstsock->sin6_scope_id) { 169 *errorp = ENXIO; /* XXX: better error? */ 170 return (0); 171 } 172 ia6 = in6_ifawithscope(ifnet_byindex(dstsock->sin6_scope_id), 173 dst); 174 if (ia6 == 0) { 175 *errorp = EADDRNOTAVAIL; 176 return (0); 177 } 178 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_INTFACELOCAL(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 if (ro->ro_rt) 260 RT_UNLOCK(ro->ro_rt); 261 } else { 262 rtalloc((struct route *)ro); 263 } 264 } 265 266 /* 267 * in_pcbconnect() checks out IFF_LOOPBACK to skip using 268 * the address. But we don't know why it does so. 269 * It is necessary to ensure the scope even for lo0 270 * so doesn't check out IFF_LOOPBACK. 271 */ 272 273 if (ro->ro_rt) { 274 ia6 = in6_ifawithscope(ro->ro_rt->rt_ifa->ifa_ifp, dst); 275 if (ia6 == 0) /* xxx scope error ?*/ 276 ia6 = ifatoia6(ro->ro_rt->rt_ifa); 277 } 278 if (ia6 == 0) { 279 *errorp = EHOSTUNREACH; /* no route */ 280 return (0); 281 } 282 return (&satosin6(&ia6->ia_addr)->sin6_addr); 283 } 284 285 *errorp = EADDRNOTAVAIL; 286 return (0); 287} 288 289/* 290 * Default hop limit selection. The precedence is as follows: 291 * 1. Hoplimit value specified via ioctl. 292 * 2. (If the outgoing interface is detected) the current 293 * hop limit of the interface specified by router advertisement. 294 * 3. The system default hoplimit. 295*/ 296int 297in6_selecthlim(in6p, ifp) 298 struct in6pcb *in6p; 299 struct ifnet *ifp; 300{ 301 if (in6p && in6p->in6p_hops >= 0) 302 return (in6p->in6p_hops); 303 else if (ifp) 304 return (ND_IFINFO(ifp)->chlim); 305 else 306 return (ip6_defhlim); 307} 308 309/* 310 * XXX: this is borrowed from in6_pcbbind(). If possible, we should 311 * share this function by all *bsd*... 312 */ 313int 314in6_pcbsetport(laddr, inp, td) 315 struct in6_addr *laddr; 316 struct inpcb *inp; 317 struct thread *td; 318{ 319 struct socket *so = inp->inp_socket; 320 u_int16_t lport = 0, first, last, *lastport; 321 int count, error = 0, wild = 0; 322 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 323 324 /* XXX: this is redundant when called from in6_pcbbind */ 325 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0) 326 wild = INPLOOKUP_WILDCARD; 327 328 inp->inp_flags |= INP_ANONPORT; 329 330 if (inp->inp_flags & INP_HIGHPORT) { 331 first = ipport_hifirstauto; /* sysctl */ 332 last = ipport_hilastauto; 333 lastport = &pcbinfo->lasthi; 334 } else if (inp->inp_flags & INP_LOWPORT) { 335 if (td && (error = suser(td))) 336 return error; 337 first = ipport_lowfirstauto; /* 1023 */ 338 last = ipport_lowlastauto; /* 600 */ 339 lastport = &pcbinfo->lastlow; 340 } else { 341 first = ipport_firstauto; /* sysctl */ 342 last = ipport_lastauto; 343 lastport = &pcbinfo->lastport; 344 } 345 /* 346 * Simple check to ensure all ports are not used up causing 347 * a deadlock here. 348 * 349 * We split the two cases (up and down) so that the direction 350 * is not being tested on each round of the loop. 351 */ 352 if (first > last) { 353 /* 354 * counting down 355 */ 356 count = first - last; 357 358 do { 359 if (count-- < 0) { /* completely used? */ 360 /* 361 * Undo any address bind that may have 362 * occurred above. 363 */ 364 inp->in6p_laddr = in6addr_any; 365 return (EAGAIN); 366 } 367 --*lastport; 368 if (*lastport > first || *lastport < last) 369 *lastport = first; 370 lport = htons(*lastport); 371 } while (in6_pcblookup_local(pcbinfo, 372 &inp->in6p_laddr, lport, wild)); 373 } else { 374 /* 375 * counting up 376 */ 377 count = last - first; 378 379 do { 380 if (count-- < 0) { /* completely used? */ 381 /* 382 * Undo any address bind that may have 383 * occurred above. 384 */ 385 inp->in6p_laddr = in6addr_any; 386 return (EAGAIN); 387 } 388 ++*lastport; 389 if (*lastport < first || *lastport > last) 390 *lastport = first; 391 lport = htons(*lastport); 392 } while (in6_pcblookup_local(pcbinfo, 393 &inp->in6p_laddr, lport, wild)); 394 } 395 396 inp->inp_lport = lport; 397 if (in_pcbinshash(inp) != 0) { 398 inp->in6p_laddr = in6addr_any; 399 inp->inp_lport = 0; 400 return (EAGAIN); 401 } 402 403 return (0); 404} 405 406/* 407 * Generate kernel-internal form (scopeid embedded into s6_addr16[1]). 408 * If the address scope of is link-local, embed the interface index in the 409 * address. The routine determines our precedence 410 * between advanced API scope/interface specification and basic API 411 * specification. 412 * 413 * This function should be nuked in the future, when we get rid of embedded 414 * scopeid thing. 415 * 416 * XXX actually, it is over-specification to return ifp against sin6_scope_id. 417 * there can be multiple interfaces that belong to a particular scope zone 418 * (in specification, we have 1:N mapping between a scope zone and interfaces). 419 * we may want to change the function to return something other than ifp. 420 */ 421int 422in6_embedscope(in6, sin6, in6p, ifpp) 423 struct in6_addr *in6; 424 const struct sockaddr_in6 *sin6; 425 struct in6pcb *in6p; 426 struct ifnet **ifpp; 427{ 428 struct ifnet *ifp = NULL; 429 u_int32_t scopeid; 430 431 *in6 = sin6->sin6_addr; 432 scopeid = sin6->sin6_scope_id; 433 if (ifpp) 434 *ifpp = NULL; 435 436 /* 437 * don't try to read sin6->sin6_addr beyond here, since the caller may 438 * ask us to overwrite existing sockaddr_in6 439 */ 440 441#ifdef ENABLE_DEFAULT_SCOPE 442 if (scopeid == 0) 443 scopeid = scope6_addr2default(in6); 444#endif 445 446 if (IN6_IS_SCOPE_LINKLOCAL(in6)) { 447 struct in6_pktinfo *pi; 448 449 /* 450 * KAME assumption: link id == interface id 451 */ 452 453 if (in6p && in6p->in6p_outputopts && 454 (pi = in6p->in6p_outputopts->ip6po_pktinfo) && 455 pi->ipi6_ifindex) { 456 ifp = ifnet_byindex(pi->ipi6_ifindex); 457 in6->s6_addr16[1] = htons(pi->ipi6_ifindex); 458 } else if (in6p && IN6_IS_ADDR_MULTICAST(in6) && 459 in6p->in6p_moptions && 460 in6p->in6p_moptions->im6o_multicast_ifp) { 461 ifp = in6p->in6p_moptions->im6o_multicast_ifp; 462 in6->s6_addr16[1] = htons(ifp->if_index); 463 } else if (scopeid) { 464 /* boundary check */ 465 if (scopeid < 0 || if_index < scopeid) 466 return ENXIO; /* XXX EINVAL? */ 467 ifp = ifnet_byindex(scopeid); 468 /* XXX assignment to 16bit from 32bit variable */ 469 in6->s6_addr16[1] = htons(scopeid & 0xffff); 470 } 471 472 if (ifpp) 473 *ifpp = ifp; 474 } 475 476 return 0; 477} 478 479/* 480 * generate standard sockaddr_in6 from embedded form. 481 * touches sin6_addr and sin6_scope_id only. 482 * 483 * this function should be nuked in the future, when we get rid of 484 * embedded scopeid thing. 485 */ 486int 487in6_recoverscope(sin6, in6, ifp) 488 struct sockaddr_in6 *sin6; 489 const struct in6_addr *in6; 490 struct ifnet *ifp; 491{ 492 u_int32_t zoneid; 493 494 sin6->sin6_addr = *in6; 495 496 /* 497 * don't try to read *in6 beyond here, since the caller may 498 * ask us to overwrite existing sockaddr_in6 499 */ 500 501 sin6->sin6_scope_id = 0; 502 if (IN6_IS_SCOPE_LINKLOCAL(in6) || IN6_IS_ADDR_MC_INTFACELOCAL(in6)) { 503 /* 504 * KAME assumption: link id == interface id 505 */ 506 zoneid = ntohs(sin6->sin6_addr.s6_addr16[1]); 507 if (zoneid) { 508 /* sanity check */ 509 if (zoneid < 0 || if_index < zoneid) 510 return ENXIO; 511 if (ifp && ifp->if_index != zoneid) 512 return ENXIO; 513 sin6->sin6_addr.s6_addr16[1] = 0; 514 sin6->sin6_scope_id = zoneid; 515 } 516 } 517 518 return 0; 519} 520 521/* 522 * just clear the embedded scope identifier. 523 */ 524void 525in6_clearscope(addr) 526 struct in6_addr *addr; 527{ 528 if (IN6_IS_SCOPE_LINKLOCAL(addr) || IN6_IS_ADDR_MC_INTFACELOCAL(addr)) 529 addr->s6_addr16[1] = 0; 530} 531