1 /* $NetBSD: in6_src.c,v 1.21 2005/05/29 21:43:51 christos Exp $ */ 2 /* $KAME: in6_src.c,v 1.36 2001/02/06 04:08:17 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. Neither the name of the University nor the names of its contributors 46 * may be used to endorse or promote products derived from this software 47 * without specific prior written permission. 48 * 49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 59 * SUCH DAMAGE. 60 * 61 * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94 62 */ 63 64 #include <sys/cdefs.h> 65 __KERNEL_RCSID(0, "$NetBSD: in6_src.c,v 1.21 2005/05/29 21:43:51 christos Exp $"); 66 67 #include "opt_inet.h" 68 69 #include <sys/param.h> 70 #include <sys/systm.h> 71 #include <sys/malloc.h> 72 #include <sys/mbuf.h> 73 #include <sys/protosw.h> 74 #include <sys/socket.h> 75 #include <sys/socketvar.h> 76 #include <sys/ioctl.h> 77 #include <sys/errno.h> 78 #include <sys/time.h> 79 #include <sys/proc.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_MC_LINKLOCAL(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_indexlim <= dstsock->sin6_scope_id || 170 !ifindex2ifnet[dstsock->sin6_scope_id]) { 171 *errorp = ENXIO; /* XXX: better error? */ 172 return (0); 173 } 174 ia6 = in6_ifawithscope(ifindex2ifnet[dstsock->sin6_scope_id], 175 dst); 176 if (ia6 == 0) { 177 *errorp = EADDRNOTAVAIL; 178 return (0); 179 } 180 return (&satosin6(&ia6->ia_addr)->sin6_addr); 181 } 182 183 /* 184 * If the destination address is a multicast address and 185 * the outgoing interface for the address is specified 186 * by the caller, use an address associated with the interface. 187 * There is a sanity check here; if the destination has node-local 188 * scope, the outgoing interfacde should be a loopback address. 189 * Even if the outgoing interface is not specified, we also 190 * choose a loopback interface as the outgoing interface. 191 */ 192 if (IN6_IS_ADDR_MULTICAST(dst)) { 193 struct ifnet *ifp = mopts ? mopts->im6o_multicast_ifp : NULL; 194 195 if (ifp == NULL && IN6_IS_ADDR_MC_NODELOCAL(dst)) 196 ifp = lo0ifp; 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 * Note that we should check the address family of the 237 * cached destination, in case of sharing the cache with IPv4. 238 */ 239 if (ro) { 240 if (ro->ro_rt && 241 (ro->ro_dst.sin6_family != AF_INET6 || 242 !IN6_ARE_ADDR_EQUAL(&satosin6(&ro->ro_dst)->sin6_addr, dst))) { 243 RTFREE(ro->ro_rt); 244 ro->ro_rt = (struct rtentry *)0; 245 } 246 if (ro->ro_rt == (struct rtentry *)0 || 247 ro->ro_rt->rt_ifp == (struct ifnet *)0) { 248 struct sockaddr_in6 *sa6; 249 250 /* No route yet, so try to acquire one */ 251 bzero(&ro->ro_dst, sizeof(struct sockaddr_in6)); 252 sa6 = (struct sockaddr_in6 *)&ro->ro_dst; 253 sa6->sin6_family = AF_INET6; 254 sa6->sin6_len = sizeof(struct sockaddr_in6); 255 sa6->sin6_addr = *dst; 256 sa6->sin6_scope_id = dstsock->sin6_scope_id; 257 if (IN6_IS_ADDR_MULTICAST(dst)) { 258 ro->ro_rt = rtalloc1(&((struct route *)ro) 259 ->ro_dst, 0); 260 } else { 261 rtalloc((struct route *)ro); 262 } 263 } 264 265 /* 266 * in_pcbconnect() checks out IFF_LOOPBACK to skip using 267 * the address. But we don't know why it does so. 268 * It is necessary to ensure the scope even for lo0 269 * so doesn't check out IFF_LOOPBACK. 270 */ 271 272 if (ro->ro_rt) { 273 ia6 = in6_ifawithscope(ro->ro_rt->rt_ifa->ifa_ifp, dst); 274 if (ia6 == 0) /* xxx scope error ?*/ 275 ia6 = ifatoia6(ro->ro_rt->rt_ifa); 276 } 277 #if 0 278 /* 279 * xxx The followings are necessary? (kazu) 280 * I don't think so. 281 * It's for SO_DONTROUTE option in IPv4.(jinmei) 282 */ 283 if (ia6 == 0) { 284 struct sockaddr_in6 sin6 = {sizeof(sin6), AF_INET6, 0}; 285 286 sin6->sin6_addr = *dst; 287 288 ia6 = ifatoia6(ifa_ifwithdstaddr(sin6tosa(&sin6))); 289 if (ia6 == 0) 290 ia6 = ifatoia6(ifa_ifwithnet(sin6tosa(&sin6))); 291 if (ia6 == 0) 292 return (0); 293 return (&satosin6(&ia6->ia_addr)->sin6_addr); 294 } 295 #endif /* 0 */ 296 if (ia6 == 0) { 297 *errorp = EHOSTUNREACH; /* no route */ 298 return (0); 299 } 300 return (&satosin6(&ia6->ia_addr)->sin6_addr); 301 } 302 303 *errorp = EADDRNOTAVAIL; 304 return (0); 305 } 306 307 /* 308 * Default hop limit selection. The precedence is as follows: 309 * 1. Hoplimit value specified via ioctl. 310 * 2. (If the outgoing interface is detected) the current 311 * hop limit of the interface specified by router advertisement. 312 * 3. The system default hoplimit. 313 */ 314 int 315 in6_selecthlim(in6p, ifp) 316 struct in6pcb *in6p; 317 struct ifnet *ifp; 318 { 319 if (in6p && in6p->in6p_af != AF_INET6) 320 return (-1); 321 322 if (in6p && in6p->in6p_hops >= 0) 323 return (in6p->in6p_hops); 324 else if (ifp) 325 return (ND_IFINFO(ifp)->chlim); 326 else 327 return (ip6_defhlim); 328 } 329 330 /* 331 * Find an empty port and set it to the specified PCB. 332 */ 333 int 334 in6_pcbsetport(laddr, in6p, p) 335 struct in6_addr *laddr; 336 struct in6pcb *in6p; 337 struct proc *p; 338 { 339 struct socket *so = in6p->in6p_socket; 340 struct inpcbtable *table = in6p->in6p_table; 341 int cnt; 342 u_int16_t xmin, xmax; 343 u_int16_t lport, *lastport; 344 int wild = 0; 345 void *t; 346 347 if (in6p->in6p_af != AF_INET6) 348 return (EINVAL); 349 350 /* XXX: this is redundant when called from in6_pcbbind */ 351 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0 && 352 ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 || 353 (so->so_options & SO_ACCEPTCONN) == 0)) 354 wild = 1; 355 356 if (in6p->in6p_flags & IN6P_LOWPORT) { 357 #ifndef IPNOPRIVPORTS 358 if (p == 0 || (suser(p->p_ucred, &p->p_acflag) != 0)) 359 return (EACCES); 360 #endif 361 xmin = ip6_lowportmin; 362 xmax = ip6_lowportmax; 363 lastport = &table->inpt_lastlow; 364 } else { 365 xmin = ip6_anonportmin; 366 xmax = ip6_anonportmax; 367 lastport = &table->inpt_lastport; 368 } 369 if (xmin > xmax) { /* sanity check */ 370 u_int16_t swp; 371 372 swp = xmin; 373 xmin = xmax; 374 xmax = swp; 375 } 376 377 lport = *lastport - 1; 378 for (cnt = xmax - xmin + 1; cnt; cnt--, lport--) { 379 if (lport < xmin || lport > xmax) 380 lport = xmax; 381 #ifdef INET 382 if (IN6_IS_ADDR_V4MAPPED(laddr)) { 383 t = in_pcblookup_port(table, 384 *(struct in_addr *)&in6p->in6p_laddr.s6_addr32[3], 385 lport, wild); 386 } else 387 #endif 388 { 389 t = in6_pcblookup_port(table, laddr, lport, wild); 390 } 391 if (t == 0) 392 goto found; 393 } 394 395 return (EAGAIN); 396 397 found: 398 in6p->in6p_flags |= IN6P_ANONPORT; 399 *lastport = lport; 400 in6p->in6p_lport = htons(lport); 401 in6_pcbstate(in6p, IN6P_BOUND); 402 return (0); /* success */ 403 } 404 405 /* 406 * generate kernel-internal form (scopeid embedded into s6_addr16[1]). 407 * If the address scope of is link-local, embed the interface index in the 408 * address. The routine determines our precedence 409 * between advanced API scope/interface specification and basic API 410 * specification. 411 * 412 * this function should be nuked in the future, when we get rid of 413 * embedded scopeid thing. 414 * 415 * XXX actually, it is over-specification to return ifp against sin6_scope_id. 416 * there can be multiple interfaces that belong to a particular scope zone 417 * (in specification, we have 1:N mapping between a scope zone and interfaces). 418 * we may want to change the function to return something other than ifp. 419 */ 420 int 421 in6_embedscope(in6, sin6, in6p, ifpp) 422 struct in6_addr *in6; 423 const struct sockaddr_in6 *sin6; 424 struct in6pcb *in6p; 425 struct ifnet **ifpp; 426 { 427 struct ifnet *ifp = NULL; 428 u_int32_t scopeid; 429 430 *in6 = sin6->sin6_addr; 431 scopeid = sin6->sin6_scope_id; 432 if (ifpp) 433 *ifpp = NULL; 434 435 /* 436 * don't try to read sin6->sin6_addr beyond here, since the caller may 437 * ask us to overwrite existing sockaddr_in6 438 */ 439 440 #ifdef ENABLE_DEFAULT_SCOPE 441 if (scopeid == 0) 442 scopeid = scope6_addr2default(in6); 443 #endif 444 445 if (IN6_IS_SCOPE_LINKLOCAL(in6)) { 446 struct in6_pktinfo *pi; 447 448 /* 449 * KAME assumption: link id == interface id 450 */ 451 452 if (in6p && in6p->in6p_outputopts && 453 (pi = in6p->in6p_outputopts->ip6po_pktinfo) && 454 pi->ipi6_ifindex) { 455 ifp = ifindex2ifnet[pi->ipi6_ifindex]; 456 in6->s6_addr16[1] = htons(pi->ipi6_ifindex); 457 } else if (in6p && IN6_IS_ADDR_MULTICAST(in6) && 458 in6p->in6p_moptions && 459 in6p->in6p_moptions->im6o_multicast_ifp) { 460 ifp = in6p->in6p_moptions->im6o_multicast_ifp; 461 in6->s6_addr16[1] = htons(ifp->if_index); 462 } else if (scopeid) { 463 /* boundary check */ 464 if (scopeid < 0 || if_indexlim <= scopeid || 465 !ifindex2ifnet[scopeid]) 466 return ENXIO; /* XXX EINVAL? */ 467 ifp = ifindex2ifnet[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 */ 486 int 487 in6_recoverscope(sin6, in6, ifp) 488 struct sockaddr_in6 *sin6; 489 const struct in6_addr *in6; 490 struct ifnet *ifp; 491 { 492 u_int32_t scopeid; 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)) { 503 /* 504 * KAME assumption: link id == interface id 505 */ 506 scopeid = ntohs(sin6->sin6_addr.s6_addr16[1]); 507 if (scopeid) { 508 /* sanity check */ 509 if (scopeid < 0 || if_indexlim <= scopeid || 510 !ifindex2ifnet[scopeid]) 511 return ENXIO; 512 if (ifp && ifp->if_index != scopeid) 513 return ENXIO; 514 sin6->sin6_addr.s6_addr16[1] = 0; 515 sin6->sin6_scope_id = scopeid; 516 } 517 } 518 519 return 0; 520 } 521 522 /* 523 * just clear the embedded scope identifer. 524 */ 525 void 526 in6_clearscope(addr) 527 struct in6_addr *addr; 528 { 529 if (IN6_IS_SCOPE_LINKLOCAL(addr)) 530 addr->s6_addr16[1] = 0; 531 } 532