1 /* $FreeBSD$ */ 2 /* $KAME: in6_src.c,v 1.37 2001/03/29 05:34:31 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/malloc.h> 74 #include <sys/mbuf.h> 75 #include <sys/protosw.h> 76 #include <sys/socket.h> 77 #include <sys/socketvar.h> 78 #include <sys/errno.h> 79 #include <sys/time.h> 80 81 #include <net/if.h> 82 #include <net/route.h> 83 84 #include <netinet/in.h> 85 #include <netinet/in_var.h> 86 #include <netinet/in_systm.h> 87 #include <netinet/ip.h> 88 #include <netinet/in_pcb.h> 89 #include <netinet6/in6_var.h> 90 #include <netinet/ip6.h> 91 #include <netinet6/in6_pcb.h> 92 #include <netinet6/ip6_var.h> 93 #include <netinet6/nd6.h> 94 #ifdef ENABLE_DEFAULT_SCOPE 95 #include <netinet6/scope6_var.h> 96 #endif 97 98 #include <net/net_osdep.h> 99 100 /* 101 * Return an IPv6 address, which is the most appropriate for a given 102 * 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 */ 106 struct in6_addr * 107 in6_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(ifindex2ifnet[pi->ipi6_ifindex], 145 dst); 146 if (ia6 == 0) { 147 *errorp = EADDRNOTAVAIL; 148 return(0); 149 } 150 return(&satosin6(&ia6->ia_addr)->sin6_addr); 151 } 152 153 /* 154 * If the destination address is a link-local unicast address or 155 * a multicast address, and if the outgoing interface is specified 156 * by the sin6_scope_id filed, use an address associated with the 157 * interface. 158 * XXX: We're now trying to define more specific semantics of 159 * sin6_scope_id field, so this part will be rewritten in 160 * the near future. 161 */ 162 if ((IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MULTICAST(dst)) && 163 dstsock->sin6_scope_id) { 164 /* 165 * I'm not sure if boundary check for scope_id is done 166 * somewhere... 167 */ 168 if (dstsock->sin6_scope_id < 0 || 169 if_index < dstsock->sin6_scope_id) { 170 *errorp = ENXIO; /* XXX: better error? */ 171 return(0); 172 } 173 ia6 = in6_ifawithscope(ifindex2ifnet[dstsock->sin6_scope_id], 174 dst); 175 if (ia6 == 0) { 176 *errorp = EADDRNOTAVAIL; 177 return(0); 178 } 179 return(&satosin6(&ia6->ia_addr)->sin6_addr); 180 } 181 182 /* 183 * If the destination address is a multicast address and 184 * the outgoing interface for the address is specified 185 * by the caller, use an address associated with the interface. 186 * There is a sanity check here; if the destination has node-local 187 * scope, the outgoing interfacde should be a loopback address. 188 * Even if the outgoing interface is not specified, we also 189 * choose a loopback interface as the outgoing interface. 190 */ 191 if (IN6_IS_ADDR_MULTICAST(dst)) { 192 struct ifnet *ifp = mopts ? mopts->im6o_multicast_ifp : NULL; 193 194 if (ifp == NULL && IN6_IS_ADDR_MC_NODELOCAL(dst)) { 195 ifp = &loif[0]; 196 } 197 198 if (ifp) { 199 ia6 = in6_ifawithscope(ifp, dst); 200 if (ia6 == 0) { 201 *errorp = EADDRNOTAVAIL; 202 return(0); 203 } 204 return(&satosin6(&ia6->ia_addr)->sin6_addr); 205 } 206 } 207 208 /* 209 * If the next hop address for the packet is specified 210 * by caller, use an address associated with the route 211 * to the next hop. 212 */ 213 { 214 struct sockaddr_in6 *sin6_next; 215 struct rtentry *rt; 216 217 if (opts && opts->ip6po_nexthop) { 218 sin6_next = satosin6(opts->ip6po_nexthop); 219 rt = nd6_lookup(&sin6_next->sin6_addr, 1, NULL); 220 if (rt) { 221 ia6 = in6_ifawithscope(rt->rt_ifp, dst); 222 if (ia6 == 0) 223 ia6 = ifatoia6(rt->rt_ifa); 224 } 225 if (ia6 == 0) { 226 *errorp = EADDRNOTAVAIL; 227 return(0); 228 } 229 return(&satosin6(&ia6->ia_addr)->sin6_addr); 230 } 231 } 232 233 /* 234 * If route is known or can be allocated now, 235 * our src addr is taken from the i/f, else punt. 236 */ 237 if (ro) { 238 if (ro->ro_rt && 239 !IN6_ARE_ADDR_EQUAL(&satosin6(&ro->ro_dst)->sin6_addr, dst)) { 240 RTFREE(ro->ro_rt); 241 ro->ro_rt = (struct rtentry *)0; 242 } 243 if (ro->ro_rt == (struct rtentry *)0 || 244 ro->ro_rt->rt_ifp == (struct ifnet *)0) { 245 struct sockaddr_in6 *sa6; 246 247 /* No route yet, so try to acquire one */ 248 bzero(&ro->ro_dst, sizeof(struct sockaddr_in6)); 249 sa6 = (struct sockaddr_in6 *)&ro->ro_dst; 250 sa6->sin6_family = AF_INET6; 251 sa6->sin6_len = sizeof(struct sockaddr_in6); 252 sa6->sin6_addr = *dst; 253 sa6->sin6_scope_id = dstsock->sin6_scope_id; 254 if (IN6_IS_ADDR_MULTICAST(dst)) { 255 ro->ro_rt = rtalloc1(&((struct route *)ro) 256 ->ro_dst, 0, 0UL); 257 } else { 258 rtalloc((struct route *)ro); 259 } 260 } 261 262 /* 263 * in_pcbconnect() checks out IFF_LOOPBACK to skip using 264 * the address. But we don't know why it does so. 265 * It is necessary to ensure the scope even for lo0 266 * so doesn't check out IFF_LOOPBACK. 267 */ 268 269 if (ro->ro_rt) { 270 ia6 = in6_ifawithscope(ro->ro_rt->rt_ifa->ifa_ifp, dst); 271 if (ia6 == 0) /* xxx scope error ?*/ 272 ia6 = ifatoia6(ro->ro_rt->rt_ifa); 273 } 274 #if 0 275 /* 276 * xxx The followings are necessary? (kazu) 277 * I don't think so. 278 * It's for SO_DONTROUTE option in IPv4.(jinmei) 279 */ 280 if (ia6 == 0) { 281 struct sockaddr_in6 sin6 = {sizeof(sin6), AF_INET6, 0}; 282 283 sin6->sin6_addr = *dst; 284 285 ia6 = ifatoia6(ifa_ifwithdstaddr(sin6tosa(&sin6))); 286 if (ia6 == 0) 287 ia6 = ifatoia6(ifa_ifwithnet(sin6tosa(&sin6))); 288 if (ia6 == 0) 289 return(0); 290 return(&satosin6(&ia6->ia_addr)->sin6_addr); 291 } 292 #endif /* 0 */ 293 if (ia6 == 0) { 294 *errorp = EHOSTUNREACH; /* no route */ 295 return(0); 296 } 297 return(&satosin6(&ia6->ia_addr)->sin6_addr); 298 } 299 300 *errorp = EADDRNOTAVAIL; 301 return(0); 302 } 303 304 /* 305 * Default hop limit selection. The precedence is as follows: 306 * 1. Hoplimit value specified via ioctl. 307 * 2. (If the outgoing interface is detected) the current 308 * hop limit of the interface specified by router advertisement. 309 * 3. The system default hoplimit. 310 */ 311 int 312 in6_selecthlim(in6p, ifp) 313 struct in6pcb *in6p; 314 struct ifnet *ifp; 315 { 316 if (in6p && in6p->in6p_hops >= 0) 317 return(in6p->in6p_hops); 318 else if (ifp) 319 return(nd_ifinfo[ifp->if_index].chlim); 320 else 321 return(ip6_defhlim); 322 } 323 324 /* 325 * XXX: this is borrowed from in6_pcbbind(). If possible, we should 326 * share this function by all *bsd*... 327 */ 328 int 329 in6_pcbsetport(laddr, inp, p) 330 struct in6_addr *laddr; 331 struct inpcb *inp; 332 struct proc *p; 333 { 334 struct socket *so = inp->inp_socket; 335 u_int16_t lport = 0, first, last, *lastport; 336 int count, error = 0, wild = 0; 337 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 338 339 /* XXX: this is redundant when called from in6_pcbbind */ 340 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0) 341 wild = INPLOOKUP_WILDCARD; 342 343 inp->inp_flags |= INP_ANONPORT; 344 345 if (inp->inp_flags & INP_HIGHPORT) { 346 first = ipport_hifirstauto; /* sysctl */ 347 last = ipport_hilastauto; 348 lastport = &pcbinfo->lasthi; 349 } else if (inp->inp_flags & INP_LOWPORT) { 350 if (p && (error = suser(p))) 351 return error; 352 first = ipport_lowfirstauto; /* 1023 */ 353 last = ipport_lowlastauto; /* 600 */ 354 lastport = &pcbinfo->lastlow; 355 } else { 356 first = ipport_firstauto; /* sysctl */ 357 last = ipport_lastauto; 358 lastport = &pcbinfo->lastport; 359 } 360 /* 361 * Simple check to ensure all ports are not used up causing 362 * a deadlock here. 363 * 364 * We split the two cases (up and down) so that the direction 365 * is not being tested on each round of the loop. 366 */ 367 if (first > last) { 368 /* 369 * counting down 370 */ 371 count = first - last; 372 373 do { 374 if (count-- < 0) { /* completely used? */ 375 /* 376 * Undo any address bind that may have 377 * occurred above. 378 */ 379 inp->in6p_laddr = in6addr_any; 380 return (EAGAIN); 381 } 382 --*lastport; 383 if (*lastport > first || *lastport < last) 384 *lastport = first; 385 lport = htons(*lastport); 386 } while (in6_pcblookup_local(pcbinfo, 387 &inp->in6p_laddr, lport, wild)); 388 } else { 389 /* 390 * counting up 391 */ 392 count = last - first; 393 394 do { 395 if (count-- < 0) { /* completely used? */ 396 /* 397 * Undo any address bind that may have 398 * occurred above. 399 */ 400 inp->in6p_laddr = in6addr_any; 401 return (EAGAIN); 402 } 403 ++*lastport; 404 if (*lastport < first || *lastport > last) 405 *lastport = first; 406 lport = htons(*lastport); 407 } while (in6_pcblookup_local(pcbinfo, 408 &inp->in6p_laddr, lport, wild)); 409 } 410 411 inp->inp_lport = lport; 412 if (in_pcbinshash(inp) != 0) { 413 inp->in6p_laddr = in6addr_any; 414 inp->inp_lport = 0; 415 return (EAGAIN); 416 } 417 418 return(0); 419 } 420 421 /* 422 * generate kernel-internal form (scopeid embedded into s6_addr16[1]). 423 * If the address scope of is link-local, embed the interface index in the 424 * address. The routine determines our precedence 425 * between advanced API scope/interface specification and basic API 426 * specification. 427 * 428 * this function should be nuked in the future, when we get rid of 429 * embedded scopeid thing. 430 * 431 * XXX actually, it is over-specification to return ifp against sin6_scope_id. 432 * there can be multiple interfaces that belong to a particular scope zone 433 * (in specification, we have 1:N mapping between a scope zone and interfaces). 434 * we may want to change the function to return something other than ifp. 435 */ 436 int 437 in6_embedscope(in6, sin6, in6p, ifpp) 438 struct in6_addr *in6; 439 const struct sockaddr_in6 *sin6; 440 #ifdef HAVE_NRL_INPCB 441 struct inpcb *in6p; 442 #define in6p_outputopts inp_outputopts6 443 #define in6p_moptions inp_moptions6 444 #else 445 struct in6pcb *in6p; 446 #endif 447 struct ifnet **ifpp; 448 { 449 struct ifnet *ifp = NULL; 450 u_int32_t scopeid; 451 452 *in6 = sin6->sin6_addr; 453 scopeid = sin6->sin6_scope_id; 454 if (ifpp) 455 *ifpp = NULL; 456 457 /* 458 * don't try to read sin6->sin6_addr beyond here, since the caller may 459 * ask us to overwrite existing sockaddr_in6 460 */ 461 462 #ifdef ENABLE_DEFAULT_SCOPE 463 if (scopeid == 0) 464 scopeid = scope6_addr2default(in6); 465 #endif 466 467 if (IN6_IS_SCOPE_LINKLOCAL(in6)) { 468 struct in6_pktinfo *pi; 469 470 /* 471 * KAME assumption: link id == interface id 472 */ 473 474 if (in6p && in6p->in6p_outputopts && 475 (pi = in6p->in6p_outputopts->ip6po_pktinfo) && 476 pi->ipi6_ifindex) { 477 ifp = ifindex2ifnet[pi->ipi6_ifindex]; 478 in6->s6_addr16[1] = htons(pi->ipi6_ifindex); 479 } else if (in6p && IN6_IS_ADDR_MULTICAST(in6) && 480 in6p->in6p_moptions && 481 in6p->in6p_moptions->im6o_multicast_ifp) { 482 ifp = in6p->in6p_moptions->im6o_multicast_ifp; 483 in6->s6_addr16[1] = htons(ifp->if_index); 484 } else if (scopeid) { 485 /* boundary check */ 486 if (scopeid < 0 || if_index < scopeid) 487 return ENXIO; /* XXX EINVAL? */ 488 ifp = ifindex2ifnet[scopeid]; 489 /*XXX assignment to 16bit from 32bit variable */ 490 in6->s6_addr16[1] = htons(scopeid & 0xffff); 491 } 492 493 if (ifpp) 494 *ifpp = ifp; 495 } 496 497 return 0; 498 } 499 #ifdef HAVE_NRL_INPCB 500 #undef in6p_outputopts 501 #undef in6p_moptions 502 #endif 503 504 /* 505 * generate standard sockaddr_in6 from embedded form. 506 * touches sin6_addr and sin6_scope_id only. 507 * 508 * this function should be nuked in the future, when we get rid of 509 * embedded scopeid thing. 510 */ 511 int 512 in6_recoverscope(sin6, in6, ifp) 513 struct sockaddr_in6 *sin6; 514 const struct in6_addr *in6; 515 struct ifnet *ifp; 516 { 517 u_int32_t scopeid; 518 519 sin6->sin6_addr = *in6; 520 521 /* 522 * don't try to read *in6 beyond here, since the caller may 523 * ask us to overwrite existing sockaddr_in6 524 */ 525 526 sin6->sin6_scope_id = 0; 527 if (IN6_IS_SCOPE_LINKLOCAL(in6)) { 528 /* 529 * KAME assumption: link id == interface id 530 */ 531 scopeid = ntohs(sin6->sin6_addr.s6_addr16[1]); 532 if (scopeid) { 533 /* sanity check */ 534 if (scopeid < 0 || if_index < scopeid) 535 return ENXIO; 536 if (ifp && ifp->if_index != scopeid) 537 return ENXIO; 538 sin6->sin6_addr.s6_addr16[1] = 0; 539 sin6->sin6_scope_id = scopeid; 540 } 541 } 542 543 return 0; 544 } 545 546 /* 547 * just clear the embedded scope identifer. 548 * XXX: currently used for bsdi4 only as a supplement function. 549 */ 550 void 551 in6_clearscope(addr) 552 struct in6_addr *addr; 553 { 554 if (IN6_IS_SCOPE_LINKLOCAL(addr)) 555 addr->s6_addr16[1] = 0; 556 } 557