1 /* $FreeBSD$ */ 2 /* $KAME: in6_src.c,v 1.27 2000/06/21 08:07:13 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. All advertising materials mentioning features or use of this software 46 * must display the following acknowledgement: 47 * This product includes software developed by the University of 48 * California, Berkeley and its contributors. 49 * 4. Neither the name of the University nor the names of its contributors 50 * may be used to endorse or promote products derived from this software 51 * without specific prior written permission. 52 * 53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 63 * SUCH DAMAGE. 64 * 65 * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94 66 */ 67 68 #include "opt_inet.h" 69 #include "opt_inet6.h" 70 71 #include <sys/param.h> 72 #include <sys/systm.h> 73 #include <sys/mbuf.h> 74 #include <sys/protosw.h> 75 #include <sys/socket.h> 76 #include <sys/socketvar.h> 77 #include <sys/errno.h> 78 #include <sys/time.h> 79 80 #include <net/if.h> 81 #include <net/route.h> 82 83 #include <netinet/in.h> 84 #include <netinet/in_var.h> 85 #include <netinet/in_systm.h> 86 #include <netinet/ip.h> 87 #include <netinet/in_pcb.h> 88 #include <netinet6/in6_var.h> 89 #include <netinet/ip6.h> 90 #include <netinet6/in6_pcb.h> 91 #include <netinet6/ip6_var.h> 92 #include <netinet6/nd6.h> 93 #ifdef ENABLE_DEFAULT_SCOPE 94 #include <netinet6/scope6_var.h> 95 #endif 96 97 #include <net/net_osdep.h> 98 99 /* 100 * Return an IPv6 address, which is the most appropriate for given 101 * destination and user specified options. 102 * If necessary, this function lookups the routing table and return 103 * an entry to the caller for later use. 104 */ 105 struct in6_addr * 106 in6_selectsrc(dstsock, opts, mopts, ro, laddr, errorp) 107 struct sockaddr_in6 *dstsock; 108 struct ip6_pktopts *opts; 109 struct ip6_moptions *mopts; 110 struct route_in6 *ro; 111 struct in6_addr *laddr; 112 int *errorp; 113 { 114 struct in6_addr *dst; 115 struct in6_ifaddr *ia6 = 0; 116 struct in6_pktinfo *pi = NULL; 117 118 dst = &dstsock->sin6_addr; 119 *errorp = 0; 120 121 /* 122 * If the source address is explicitly specified by the caller, 123 * use it. 124 */ 125 if (opts && (pi = opts->ip6po_pktinfo) && 126 !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr)) 127 return(&pi->ipi6_addr); 128 129 /* 130 * If the source address is not specified but the socket(if any) 131 * is already bound, use the bound address. 132 */ 133 if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr)) 134 return(laddr); 135 136 /* 137 * If the caller doesn't specify the source address but 138 * the outgoing interface, use an address associated with 139 * the interface. 140 */ 141 if (pi && pi->ipi6_ifindex) { 142 /* XXX boundary check is assumed to be already done. */ 143 ia6 = in6_ifawithscope(ifindex2ifnet[pi->ipi6_ifindex], 144 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(ifindex2ifnet[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_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 !IN6_ARE_ADDR_EQUAL(&satosin6(&ro->ro_dst)->sin6_addr, dst)) { 239 RTFREE(ro->ro_rt); 240 ro->ro_rt = (struct rtentry *)0; 241 } 242 if (ro->ro_rt == (struct rtentry *)0 || 243 ro->ro_rt->rt_ifp == (struct ifnet *)0) { 244 /* No route yet, so try to acquire one */ 245 bzero(&ro->ro_dst, sizeof(struct sockaddr_in6)); 246 ro->ro_dst.sin6_family = AF_INET6; 247 ro->ro_dst.sin6_len = sizeof(struct sockaddr_in6); 248 ro->ro_dst.sin6_addr = *dst; 249 ro->ro_dst.sin6_scope_id = dstsock->sin6_scope_id; 250 if (IN6_IS_ADDR_MULTICAST(dst)) { 251 ro->ro_rt = rtalloc1(&((struct route *)ro) 252 ->ro_dst, 0, 0UL); 253 } else { 254 rtalloc((struct route *)ro); 255 } 256 } 257 258 /* 259 * in_pcbconnect() checks out IFF_LOOPBACK to skip using 260 * the address. But we don't know why it does so. 261 * It is necessary to ensure the scope even for lo0 262 * so doesn't check out IFF_LOOPBACK. 263 */ 264 265 if (ro->ro_rt) { 266 ia6 = in6_ifawithscope(ro->ro_rt->rt_ifa->ifa_ifp, dst); 267 if (ia6 == 0) /* xxx scope error ?*/ 268 ia6 = ifatoia6(ro->ro_rt->rt_ifa); 269 } 270 #if 0 271 /* 272 * xxx The followings are necessary? (kazu) 273 * I don't think so. 274 * It's for SO_DONTROUTE option in IPv4.(jinmei) 275 */ 276 if (ia6 == 0) { 277 struct sockaddr_in6 sin6 = {sizeof(sin6), AF_INET6, 0}; 278 279 sin6->sin6_addr = *dst; 280 281 ia6 = ifatoia6(ifa_ifwithdstaddr(sin6tosa(&sin6))); 282 if (ia6 == 0) 283 ia6 = ifatoia6(ifa_ifwithnet(sin6tosa(&sin6))); 284 if (ia6 == 0) 285 return(0); 286 return(&satosin6(&ia6->ia_addr)->sin6_addr); 287 } 288 #endif /* 0 */ 289 if (ia6 == 0) { 290 *errorp = EHOSTUNREACH; /* no route */ 291 return(0); 292 } 293 return(&satosin6(&ia6->ia_addr)->sin6_addr); 294 } 295 296 *errorp = EADDRNOTAVAIL; 297 return(0); 298 } 299 300 /* 301 * Default hop limit selection. The precedence is as follows: 302 * 1. Hoplimit value specified via ioctl. 303 * 2. (If the outgoing interface is detected) the current 304 * hop limit of the interface specified by router advertisement. 305 * 3. The system default hoplimit. 306 */ 307 int 308 in6_selecthlim(in6p, ifp) 309 struct in6pcb *in6p; 310 struct ifnet *ifp; 311 { 312 if (in6p && in6p->in6p_hops >= 0) 313 return(in6p->in6p_hops); 314 else if (ifp) 315 return(nd_ifinfo[ifp->if_index].chlim); 316 else 317 return(ip6_defhlim); 318 } 319 320 /* 321 * XXX: this is borrowed from in6_pcbbind(). If possible, we should 322 * share this function by all *bsd*... 323 */ 324 int 325 in6_pcbsetport(laddr, inp, p) 326 struct in6_addr *laddr; 327 struct inpcb *inp; 328 struct proc *p; 329 { 330 struct socket *so = inp->inp_socket; 331 u_int16_t lport = 0, first, last, *lastport; 332 int count, error = 0, wild = 0; 333 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 334 335 /* XXX: this is redundant when called from in6_pcbbind */ 336 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0) 337 wild = INPLOOKUP_WILDCARD; 338 339 inp->inp_flags |= INP_ANONPORT; 340 341 if (inp->inp_flags & INP_HIGHPORT) { 342 first = ipport_hifirstauto; /* sysctl */ 343 last = ipport_hilastauto; 344 lastport = &pcbinfo->lasthi; 345 } else if (inp->inp_flags & INP_LOWPORT) { 346 if (p && (error = suser(p))) 347 return error; 348 first = ipport_lowfirstauto; /* 1023 */ 349 last = ipport_lowlastauto; /* 600 */ 350 lastport = &pcbinfo->lastlow; 351 } else { 352 first = ipport_firstauto; /* sysctl */ 353 last = ipport_lastauto; 354 lastport = &pcbinfo->lastport; 355 } 356 /* 357 * Simple check to ensure all ports are not used up causing 358 * a deadlock here. 359 * 360 * We split the two cases (up and down) so that the direction 361 * is not being tested on each round of the loop. 362 */ 363 if (first > last) { 364 /* 365 * counting down 366 */ 367 count = first - last; 368 369 do { 370 if (count-- < 0) { /* completely used? */ 371 /* 372 * Undo any address bind that may have 373 * occurred above. 374 */ 375 inp->in6p_laddr = in6addr_any; 376 return (EAGAIN); 377 } 378 --*lastport; 379 if (*lastport > first || *lastport < last) 380 *lastport = first; 381 lport = htons(*lastport); 382 } while (in6_pcblookup_local(pcbinfo, 383 &inp->in6p_laddr, lport, wild)); 384 } else { 385 /* 386 * counting up 387 */ 388 count = last - first; 389 390 do { 391 if (count-- < 0) { /* completely used? */ 392 /* 393 * Undo any address bind that may have 394 * occurred above. 395 */ 396 inp->in6p_laddr = in6addr_any; 397 return (EAGAIN); 398 } 399 ++*lastport; 400 if (*lastport < first || *lastport > last) 401 *lastport = first; 402 lport = htons(*lastport); 403 } while (in6_pcblookup_local(pcbinfo, 404 &inp->in6p_laddr, lport, wild)); 405 } 406 407 inp->inp_lport = lport; 408 if (in_pcbinshash(inp) != 0) { 409 inp->in6p_laddr = in6addr_any; 410 inp->inp_lport = 0; 411 return (EAGAIN); 412 } 413 414 return(0); 415 } 416 417 /* 418 * generate kernel-internal form (scopeid embedded into s6_addr16[1]). 419 * If the address scope of is link-local, embed the interface index in the 420 * address. The routine determines our precedence 421 * between advanced API scope/interface specification and basic API 422 * specification. 423 * 424 * this function should be nuked in the future, when we get rid of 425 * embedded scopeid thing. 426 * 427 * XXX actually, it is over-specification to return ifp against sin6_scope_id. 428 * there can be multiple interfaces that belong to a particular scope zone 429 * (in specification, we have 1:N mapping between a scope zone and interfaces). 430 * we may want to change the function to return something other than ifp. 431 */ 432 int 433 in6_embedscope(in6, sin6, in6p, ifpp) 434 struct in6_addr *in6; 435 const struct sockaddr_in6 *sin6; 436 #ifdef HAVE_NRL_INPCB 437 struct inpcb *in6p; 438 #define in6p_outputopts inp_outputopts6 439 #define in6p_moptions inp_moptions6 440 #else 441 struct in6pcb *in6p; 442 #endif 443 struct ifnet **ifpp; 444 { 445 struct ifnet *ifp = NULL; 446 u_int32_t scopeid; 447 448 *in6 = sin6->sin6_addr; 449 scopeid = sin6->sin6_scope_id; 450 if (ifpp) 451 *ifpp = NULL; 452 453 /* 454 * don't try to read sin6->sin6_addr beyond here, since the caller may 455 * ask us to overwrite existing sockaddr_in6 456 */ 457 458 #ifdef ENABLE_DEFAULT_SCOPE 459 if (scopeid == 0) 460 scopeid = scope6_addr2default(in6); 461 #endif 462 463 if (IN6_IS_SCOPE_LINKLOCAL(in6)) { 464 struct in6_pktinfo *pi; 465 466 /* 467 * KAME assumption: link id == interface id 468 */ 469 470 if (in6p && in6p->in6p_outputopts && 471 (pi = in6p->in6p_outputopts->ip6po_pktinfo) && 472 pi->ipi6_ifindex) { 473 ifp = ifindex2ifnet[pi->ipi6_ifindex]; 474 in6->s6_addr16[1] = htons(pi->ipi6_ifindex); 475 } else if (in6p && IN6_IS_ADDR_MULTICAST(in6) && 476 in6p->in6p_moptions && 477 in6p->in6p_moptions->im6o_multicast_ifp) { 478 ifp = in6p->in6p_moptions->im6o_multicast_ifp; 479 in6->s6_addr16[1] = htons(ifp->if_index); 480 } else if (scopeid) { 481 /* boundary check */ 482 if (scopeid < 0 || if_index < scopeid) 483 return ENXIO; /* XXX EINVAL? */ 484 ifp = ifindex2ifnet[scopeid]; 485 /*XXX assignment to 16bit from 32bit variable */ 486 in6->s6_addr16[1] = htons(scopeid & 0xffff); 487 } 488 489 if (ifpp) 490 *ifpp = ifp; 491 } 492 493 return 0; 494 } 495 #ifdef HAVE_NRL_INPCB 496 #undef in6p_outputopts 497 #undef in6p_moptions 498 #endif 499 500 /* 501 * generate standard sockaddr_in6 from embedded form. 502 * touches sin6_addr and sin6_scope_id only. 503 * 504 * this function should be nuked in the future, when we get rid of 505 * embedded scopeid thing. 506 */ 507 int 508 in6_recoverscope(sin6, in6, ifp) 509 struct sockaddr_in6 *sin6; 510 const struct in6_addr *in6; 511 struct ifnet *ifp; 512 { 513 u_int32_t scopeid; 514 515 sin6->sin6_addr = *in6; 516 517 /* 518 * don't try to read *in6 beyond here, since the caller may 519 * ask us to overwrite existing sockaddr_in6 520 */ 521 522 sin6->sin6_scope_id = 0; 523 if (IN6_IS_SCOPE_LINKLOCAL(in6)) { 524 /* 525 * KAME assumption: link id == interface id 526 */ 527 scopeid = ntohs(sin6->sin6_addr.s6_addr16[1]); 528 if (scopeid) { 529 /* sanity check */ 530 if (scopeid < 0 || if_index < scopeid) 531 return ENXIO; 532 #ifndef FAKE_LOOPBACK_IF 533 if (ifp && (ifp->if_flags & IFF_LOOPBACK) == 0 && 534 ifp->if_index != scopeid) { 535 return ENXIO; 536 } 537 #else 538 if (ifp && ifp->if_index != scopeid) 539 return ENXIO; 540 #endif 541 sin6->sin6_addr.s6_addr16[1] = 0; 542 sin6->sin6_scope_id = scopeid; 543 } 544 } 545 546 return 0; 547 } 548