1 /* $FreeBSD: src/sys/netinet6/in6_src.c,v 1.1.2.3 2002/02/26 18:02:06 ume Exp $ */ 2 /* $DragonFly: src/sys/netinet6/in6_src.c,v 1.13 2006/12/29 18:02:56 victor Exp $ */ 3 /* $KAME: in6_src.c,v 1.37 2001/03/29 05:34:31 itojun Exp $ */ 4 5 /* 6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. Neither the name of the project nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 /* 35 * Copyright (c) 1982, 1986, 1991, 1993 36 * The Regents of the University of California. All rights reserved. 37 * 38 * Redistribution and use in source and binary forms, with or without 39 * modification, are permitted provided that the following conditions 40 * are met: 41 * 1. Redistributions of source code must retain the above copyright 42 * notice, this list of conditions and the following disclaimer. 43 * 2. Redistributions in binary form must reproduce the above copyright 44 * notice, this list of conditions and the following disclaimer in the 45 * documentation and/or other materials provided with the distribution. 46 * 3. All advertising materials mentioning features or use of this software 47 * must display the following acknowledgement: 48 * This product includes software developed by the University of 49 * California, Berkeley and its contributors. 50 * 4. Neither the name of the University nor the names of its contributors 51 * may be used to endorse or promote products derived from this software 52 * without specific prior written permission. 53 * 54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 64 * SUCH DAMAGE. 65 * 66 * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94 67 */ 68 69 #include "opt_inet.h" 70 #include "opt_inet6.h" 71 72 #include <sys/param.h> 73 #include <sys/systm.h> 74 #include <sys/jail.h> 75 #include <sys/malloc.h> 76 #include <sys/mbuf.h> 77 #include <sys/protosw.h> 78 #include <sys/socket.h> 79 #include <sys/socketvar.h> 80 #include <sys/errno.h> 81 #include <sys/time.h> 82 #include <sys/proc.h> 83 #include <sys/priv.h> 84 85 #include <net/if.h> 86 #include <net/route.h> 87 88 #include <netinet/in.h> 89 #include <netinet/in_var.h> 90 #include <netinet/in_systm.h> 91 #include <netinet/ip.h> 92 #include <netinet/in_pcb.h> 93 #include <netinet6/in6_var.h> 94 #include <netinet/ip6.h> 95 #include <netinet6/in6_pcb.h> 96 #include <netinet6/ip6_var.h> 97 #include <netinet6/nd6.h> 98 #ifdef ENABLE_DEFAULT_SCOPE 99 #include <netinet6/scope6_var.h> 100 #endif 101 102 #include <net/net_osdep.h> 103 104 #include "use_loop.h" 105 106 /* 107 * Return an IPv6 address, which is the most appropriate for a given 108 * destination and user specified options. 109 * If necessary, this function lookups the routing table and returns 110 * an entry to the caller for later use. 111 */ 112 struct in6_addr * 113 in6_selectsrc(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts, 114 struct ip6_moptions *mopts, struct route_in6 *ro, 115 struct in6_addr *laddr, int *errorp, struct thread *td) 116 { 117 struct sockaddr_in6 jsin6; 118 struct ucred *cred = NULL; 119 struct in6_addr *dst; 120 struct in6_ifaddr *ia6 = 0; 121 struct in6_pktinfo *pi = NULL; 122 int jailed = 0; 123 124 if (td && td->td_proc && td->td_proc->p_ucred) 125 cred = td->td_proc->p_ucred; 126 if (cred && cred->cr_prison) 127 jailed = 1; 128 jsin6.sin6_family = AF_INET6; 129 dst = &dstsock->sin6_addr; 130 *errorp = 0; 131 132 /* 133 * If the source address is explicitly specified by the caller, 134 * use it. 135 */ 136 if (opts && (pi = opts->ip6po_pktinfo) && 137 !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr)) { 138 jsin6.sin6_addr = pi->ipi6_addr; 139 if (jailed && !jailed_ip(cred->cr_prison, 140 (struct sockaddr *)&jsin6)) { 141 return(0); 142 } else { 143 return (&pi->ipi6_addr); 144 } 145 } 146 147 /* 148 * If the source address is not specified but the socket(if any) 149 * is already bound, use the bound address. 150 */ 151 if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr)) { 152 jsin6.sin6_addr = *laddr; 153 if (jailed && !jailed_ip(cred->cr_prison, 154 (struct sockaddr *)&jsin6)) { 155 return(0); 156 } else { 157 return (laddr); 158 } 159 } 160 161 /* 162 * If the caller doesn't specify the source address but 163 * the outgoing interface, use an address associated with 164 * the interface. 165 */ 166 if (pi && pi->ipi6_ifindex) { 167 /* XXX boundary check is assumed to be already done. */ 168 ia6 = in6_ifawithscope(ifindex2ifnet[pi->ipi6_ifindex], 169 dst); 170 171 if (ia6 && jailed) { 172 jsin6.sin6_addr = (&ia6->ia_addr)->sin6_addr; 173 if (!jailed_ip(cred->cr_prison, 174 (struct sockaddr *)&jsin6)) 175 ia6 = 0; 176 } 177 178 if (ia6 == 0) { 179 *errorp = EADDRNOTAVAIL; 180 return (0); 181 } 182 return (&satosin6(&ia6->ia_addr)->sin6_addr); 183 } 184 185 /* 186 * If the destination address is a link-local unicast address or 187 * a multicast address, and if the outgoing interface is specified 188 * by the sin6_scope_id filed, use an address associated with the 189 * interface. 190 * XXX: We're now trying to define more specific semantics of 191 * sin6_scope_id field, so this part will be rewritten in 192 * the near future. 193 */ 194 if ((IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MULTICAST(dst)) && 195 dstsock->sin6_scope_id) { 196 /* 197 * I'm not sure if boundary check for scope_id is done 198 * somewhere... 199 */ 200 if (dstsock->sin6_scope_id < 0 || 201 if_index < dstsock->sin6_scope_id) { 202 *errorp = ENXIO; /* XXX: better error? */ 203 return (0); 204 } 205 ia6 = in6_ifawithscope(ifindex2ifnet[dstsock->sin6_scope_id], 206 dst); 207 208 if (ia6 && jailed) { 209 jsin6.sin6_addr = (&ia6->ia_addr)->sin6_addr; 210 if (!jailed_ip(cred->cr_prison, 211 (struct sockaddr *)&jsin6)) 212 ia6 = 0; 213 } 214 215 if (ia6 == 0) { 216 *errorp = EADDRNOTAVAIL; 217 return (0); 218 } 219 return (&satosin6(&ia6->ia_addr)->sin6_addr); 220 } 221 222 /* 223 * If the destination address is a multicast address and 224 * the outgoing interface for the address is specified 225 * by the caller, use an address associated with the interface. 226 * There is a sanity check here; if the destination has node-local 227 * scope, the outgoing interfacde should be a loopback address. 228 * Even if the outgoing interface is not specified, we also 229 * choose a loopback interface as the outgoing interface. 230 */ 231 if (!jailed && IN6_IS_ADDR_MULTICAST(dst)) { 232 struct ifnet *ifp = mopts ? mopts->im6o_multicast_ifp : NULL; 233 234 if (ifp == NULL && IN6_IS_ADDR_MC_NODELOCAL(dst)) { 235 ifp = &loif[0]; 236 } 237 238 if (ifp) { 239 ia6 = in6_ifawithscope(ifp, dst); 240 if (ia6 == 0) { 241 *errorp = EADDRNOTAVAIL; 242 return (0); 243 } 244 return (&satosin6(&ia6->ia_addr)->sin6_addr); 245 } 246 } 247 248 /* 249 * If the next hop address for the packet is specified 250 * by caller, use an address associated with the route 251 * to the next hop. 252 */ 253 { 254 struct sockaddr_in6 *sin6_next; 255 struct rtentry *rt; 256 257 if (opts && opts->ip6po_nexthop) { 258 sin6_next = satosin6(opts->ip6po_nexthop); 259 rt = nd6_lookup(&sin6_next->sin6_addr, 1, NULL); 260 if (rt) { 261 ia6 = in6_ifawithscope(rt->rt_ifp, dst); 262 if (ia6 == 0) 263 ia6 = ifatoia6(rt->rt_ifa); 264 } 265 if (ia6 && jailed) { 266 jsin6.sin6_addr = (&ia6->ia_addr)->sin6_addr; 267 if (!jailed_ip(cred->cr_prison, 268 (struct sockaddr *)&jsin6)) 269 ia6 = 0; 270 } 271 272 if (ia6 == 0) { 273 *errorp = EADDRNOTAVAIL; 274 return (0); 275 } 276 return (&satosin6(&ia6->ia_addr)->sin6_addr); 277 } 278 } 279 280 /* 281 * If route is known or can be allocated now, 282 * our src addr is taken from the i/f, else punt. 283 */ 284 if (ro) { 285 if (ro->ro_rt && 286 (!(ro->ro_rt->rt_flags & RTF_UP) || 287 satosin6(&ro->ro_dst)->sin6_family != AF_INET6 || 288 !IN6_ARE_ADDR_EQUAL(&satosin6(&ro->ro_dst)->sin6_addr, 289 dst))) { 290 RTFREE(ro->ro_rt); 291 ro->ro_rt = NULL; 292 } 293 if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp == NULL) { 294 struct sockaddr_in6 *sa6; 295 296 /* No route yet, so try to acquire one */ 297 bzero(&ro->ro_dst, sizeof(struct sockaddr_in6)); 298 sa6 = &ro->ro_dst; 299 sa6->sin6_family = AF_INET6; 300 sa6->sin6_len = sizeof(struct sockaddr_in6); 301 sa6->sin6_addr = *dst; 302 sa6->sin6_scope_id = dstsock->sin6_scope_id; 303 if (!jailed && IN6_IS_ADDR_MULTICAST(dst)) { 304 ro->ro_rt = 305 rtpurelookup((struct sockaddr *)&ro->ro_dst); 306 } else { 307 rtalloc((struct route *)ro); 308 } 309 } 310 311 /* 312 * in_pcbconnect() checks out IFF_LOOPBACK to skip using 313 * the address. But we don't know why it does so. 314 * It is necessary to ensure the scope even for lo0 315 * so doesn't check out IFF_LOOPBACK. 316 */ 317 318 if (ro->ro_rt) { 319 ia6 = in6_ifawithscope(ro->ro_rt->rt_ifa->ifa_ifp, dst); 320 if (ia6 && jailed) { 321 jsin6.sin6_addr = (&ia6->ia_addr)->sin6_addr; 322 if (!jailed_ip(cred->cr_prison, 323 (struct sockaddr *)&jsin6)) 324 ia6 = 0; 325 } 326 327 if (ia6 == 0) /* xxx scope error ?*/ 328 ia6 = ifatoia6(ro->ro_rt->rt_ifa); 329 330 if (ia6 && jailed) { 331 jsin6.sin6_addr = (&ia6->ia_addr)->sin6_addr; 332 if (!jailed_ip(cred->cr_prison, 333 (struct sockaddr *)&jsin6)) 334 ia6 = 0; 335 } 336 } 337 #if 0 338 /* 339 * xxx The followings are necessary? (kazu) 340 * I don't think so. 341 * It's for SO_DONTROUTE option in IPv4.(jinmei) 342 */ 343 if (ia6 == 0) { 344 struct sockaddr_in6 sin6 = {sizeof(sin6), AF_INET6, 0}; 345 346 sin6->sin6_addr = *dst; 347 348 ia6 = ifatoia6(ifa_ifwithdstaddr(sin6tosa(&sin6))); 349 if (ia6 == 0) 350 ia6 = ifatoia6(ifa_ifwithnet(sin6tosa(&sin6))); 351 if (ia6 == 0) 352 return (0); 353 return (&satosin6(&ia6->ia_addr)->sin6_addr); 354 } 355 #endif /* 0 */ 356 if (ia6 == 0) { 357 *errorp = EHOSTUNREACH; /* no route */ 358 return (0); 359 } 360 return (&satosin6(&ia6->ia_addr)->sin6_addr); 361 } 362 363 *errorp = EADDRNOTAVAIL; 364 return (0); 365 } 366 367 /* 368 * Default hop limit selection. The precedence is as follows: 369 * 1. Hoplimit value specified via ioctl. 370 * 2. (If the outgoing interface is detected) the current 371 * hop limit of the interface specified by router advertisement. 372 * 3. The system default hoplimit. 373 */ 374 int 375 in6_selecthlim(struct in6pcb *in6p, struct ifnet *ifp) 376 { 377 if (in6p && in6p->in6p_hops >= 0) 378 return (in6p->in6p_hops); 379 else if (ifp) 380 return (ND_IFINFO(ifp)->chlim); 381 else 382 return (ip6_defhlim); 383 } 384 385 /* 386 * XXX: this is borrowed from in6_pcbbind(). If possible, we should 387 * share this function by all *bsd*... 388 */ 389 int 390 in6_pcbsetport(struct in6_addr *laddr, struct inpcb *inp, struct thread *td) 391 { 392 struct socket *so = inp->inp_socket; 393 u_int16_t lport = 0, first, last, *lastport; 394 int count, error = 0, wild = 0; 395 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 396 struct ucred *cred = NULL; 397 398 /* XXX: this is redundant when called from in6_pcbbind */ 399 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0) 400 wild = INPLOOKUP_WILDCARD; 401 if (td->td_proc && td->td_proc->p_ucred) 402 cred = td->td_proc->p_ucred; 403 404 inp->inp_flags |= INP_ANONPORT; 405 406 if (inp->inp_flags & INP_HIGHPORT) { 407 first = ipport_hifirstauto; /* sysctl */ 408 last = ipport_hilastauto; 409 lastport = &pcbinfo->lasthi; 410 } else if (inp->inp_flags & INP_LOWPORT) { 411 if ((error = priv_check(td, PRIV_ROOT)) != 0) 412 return error; 413 first = ipport_lowfirstauto; /* 1023 */ 414 last = ipport_lowlastauto; /* 600 */ 415 lastport = &pcbinfo->lastlow; 416 } else { 417 first = ipport_firstauto; /* sysctl */ 418 last = ipport_lastauto; 419 lastport = &pcbinfo->lastport; 420 } 421 /* 422 * Simple check to ensure all ports are not used up causing 423 * a deadlock here. 424 * 425 * We split the two cases (up and down) so that the direction 426 * is not being tested on each round of the loop. 427 */ 428 if (first > last) { 429 /* 430 * counting down 431 */ 432 count = first - last; 433 434 do { 435 if (count-- < 0) { /* completely used? */ 436 /* 437 * Undo any address bind that may have 438 * occurred above. 439 */ 440 inp->in6p_laddr = kin6addr_any; 441 return (EAGAIN); 442 } 443 --*lastport; 444 if (*lastport > first || *lastport < last) 445 *lastport = first; 446 lport = htons(*lastport); 447 } while (in6_pcblookup_local(pcbinfo, &inp->in6p_laddr, 448 lport, wild, cred)); 449 } else { 450 /* 451 * counting up 452 */ 453 count = last - first; 454 455 do { 456 if (count-- < 0) { /* completely used? */ 457 /* 458 * Undo any address bind that may have 459 * occurred above. 460 */ 461 inp->in6p_laddr = kin6addr_any; 462 return (EAGAIN); 463 } 464 ++*lastport; 465 if (*lastport < first || *lastport > last) 466 *lastport = first; 467 lport = htons(*lastport); 468 } while (in6_pcblookup_local(pcbinfo, &inp->in6p_laddr, 469 lport, wild, cred)); 470 } 471 472 inp->inp_lport = lport; 473 if (in_pcbinsporthash(inp) != 0) { 474 inp->in6p_laddr = kin6addr_any; 475 inp->inp_lport = 0; 476 return (EAGAIN); 477 } 478 479 return (0); 480 } 481 482 /* 483 * generate kernel-internal form (scopeid embedded into s6_addr16[1]). 484 * If the address scope of is link-local, embed the interface index in the 485 * address. The routine determines our precedence 486 * between advanced API scope/interface specification and basic API 487 * specification. 488 * 489 * this function should be nuked in the future, when we get rid of 490 * embedded scopeid thing. 491 * 492 * XXX actually, it is over-specification to return ifp against sin6_scope_id. 493 * there can be multiple interfaces that belong to a particular scope zone 494 * (in specification, we have 1:N mapping between a scope zone and interfaces). 495 * we may want to change the function to return something other than ifp. 496 */ 497 int 498 in6_embedscope(struct in6_addr *in6, 499 const struct sockaddr_in6 *sin6, 500 #ifdef HAVE_NRL_INPCB 501 struct inpcb *in6p, 502 #define in6p_outputopts inp_outputopts6 503 #define in6p_moptions inp_moptions6 504 #else 505 struct in6pcb *in6p, 506 #endif 507 struct ifnet **ifpp) 508 { 509 struct ifnet *ifp = NULL; 510 u_int32_t scopeid; 511 512 *in6 = sin6->sin6_addr; 513 scopeid = sin6->sin6_scope_id; 514 if (ifpp) 515 *ifpp = NULL; 516 517 /* 518 * don't try to read sin6->sin6_addr beyond here, since the caller may 519 * ask us to overwrite existing sockaddr_in6 520 */ 521 522 #ifdef ENABLE_DEFAULT_SCOPE 523 if (scopeid == 0) 524 scopeid = scope6_addr2default(in6); 525 #endif 526 527 if (IN6_IS_SCOPE_LINKLOCAL(in6)) { 528 struct in6_pktinfo *pi; 529 530 /* 531 * KAME assumption: link id == interface id 532 */ 533 534 if (in6p && in6p->in6p_outputopts && 535 (pi = in6p->in6p_outputopts->ip6po_pktinfo) && 536 pi->ipi6_ifindex) { 537 ifp = ifindex2ifnet[pi->ipi6_ifindex]; 538 in6->s6_addr16[1] = htons(pi->ipi6_ifindex); 539 } else if (in6p && IN6_IS_ADDR_MULTICAST(in6) && 540 in6p->in6p_moptions && 541 in6p->in6p_moptions->im6o_multicast_ifp) { 542 ifp = in6p->in6p_moptions->im6o_multicast_ifp; 543 in6->s6_addr16[1] = htons(ifp->if_index); 544 } else if (scopeid) { 545 /* boundary check */ 546 if (scopeid < 0 || if_index < scopeid) 547 return ENXIO; /* XXX EINVAL? */ 548 ifp = ifindex2ifnet[scopeid]; 549 /*XXX assignment to 16bit from 32bit variable */ 550 in6->s6_addr16[1] = htons(scopeid & 0xffff); 551 } 552 553 if (ifpp) 554 *ifpp = ifp; 555 } 556 557 return 0; 558 } 559 #ifdef HAVE_NRL_INPCB 560 #undef in6p_outputopts 561 #undef in6p_moptions 562 #endif 563 564 /* 565 * generate standard sockaddr_in6 from embedded form. 566 * touches sin6_addr and sin6_scope_id only. 567 * 568 * this function should be nuked in the future, when we get rid of 569 * embedded scopeid thing. 570 */ 571 int 572 in6_recoverscope(struct sockaddr_in6 *sin6, const struct in6_addr *in6, 573 struct ifnet *ifp) 574 { 575 u_int32_t scopeid; 576 577 sin6->sin6_addr = *in6; 578 579 /* 580 * don't try to read *in6 beyond here, since the caller may 581 * ask us to overwrite existing sockaddr_in6 582 */ 583 584 sin6->sin6_scope_id = 0; 585 if (IN6_IS_SCOPE_LINKLOCAL(in6)) { 586 /* 587 * KAME assumption: link id == interface id 588 */ 589 scopeid = ntohs(sin6->sin6_addr.s6_addr16[1]); 590 if (scopeid) { 591 /* sanity check */ 592 if (scopeid < 0 || if_index < scopeid) 593 return ENXIO; 594 if (ifp && ifp->if_index != scopeid) 595 return ENXIO; 596 sin6->sin6_addr.s6_addr16[1] = 0; 597 sin6->sin6_scope_id = scopeid; 598 } 599 } 600 601 return 0; 602 } 603 604 /* 605 * just clear the embedded scope identifer. 606 * XXX: currently used for bsdi4 only as a supplement function. 607 */ 608 void 609 in6_clearscope(struct in6_addr *addr) 610 { 611 if (IN6_IS_SCOPE_LINKLOCAL(addr)) 612 addr->s6_addr16[1] = 0; 613 } 614