in6_src.c revision 1.20
1/* $OpenBSD: in6_src.c,v 1.20 2006/11/17 01:11:23 itojun 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/param.h> 65#include <sys/systm.h> 66#include <sys/malloc.h> 67#include <sys/mbuf.h> 68#include <sys/protosw.h> 69#include <sys/socket.h> 70#include <sys/socketvar.h> 71#include <sys/ioctl.h> 72#include <sys/errno.h> 73#include <sys/time.h> 74#include <sys/proc.h> 75 76#include <net/if.h> 77#include <net/route.h> 78 79#include <netinet/in.h> 80#include <netinet/in_var.h> 81#include <netinet/in_systm.h> 82#include <netinet/ip.h> 83#include <netinet/in_pcb.h> 84#include <netinet6/in6_var.h> 85#include <netinet/ip6.h> 86#include <netinet6/ip6_var.h> 87#include <netinet6/nd6.h> 88 89/* 90 * Return an IPv6 address, which is the most appropriate for a given 91 * destination and user specified options. 92 * If necessary, this function lookups the routing table and returns 93 * an entry to the caller for later use. 94 */ 95struct in6_addr * 96in6_selectsrc(dstsock, opts, mopts, ro, laddr, errorp) 97 struct sockaddr_in6 *dstsock; 98 struct ip6_pktopts *opts; 99 struct ip6_moptions *mopts; 100 struct route_in6 *ro; 101 struct in6_addr *laddr; 102 int *errorp; 103{ 104 struct in6_addr *dst; 105 struct in6_ifaddr *ia6 = 0; 106 struct in6_pktinfo *pi = NULL; 107 108 dst = &dstsock->sin6_addr; 109 *errorp = 0; 110 111 /* 112 * If the source address is explicitly specified by the caller, 113 * use it. 114 */ 115 if (opts && (pi = opts->ip6po_pktinfo) && 116 !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr)) 117 return (&pi->ipi6_addr); 118 119 /* 120 * If the source address is not specified but the socket(if any) 121 * is already bound, use the bound address. 122 */ 123 if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr)) 124 return (laddr); 125 126 /* 127 * If the caller doesn't specify the source address but 128 * the outgoing interface, use an address associated with 129 * the interface. 130 */ 131 if (pi && pi->ipi6_ifindex) { 132 /* XXX boundary check is assumed to be already done. */ 133 ia6 = in6_ifawithscope(ifindex2ifnet[pi->ipi6_ifindex], 134 dst); 135 if (ia6 == 0) { 136 *errorp = EADDRNOTAVAIL; 137 return (0); 138 } 139 return (&satosin6(&ia6->ia_addr)->sin6_addr); 140 } 141 142 /* 143 * If the destination address is a link-local unicast address or 144 * a link/interface-local multicast address, and if the outgoing 145 * interface is specified by the sin6_scope_id filed, use an address 146 * associated with the interface. 147 * XXX: We're now trying to define more specific semantics of 148 * sin6_scope_id field, so this part will be rewritten in 149 * the near future. 150 */ 151 if ((IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MC_LINKLOCAL(dst) || 152 IN6_IS_ADDR_MC_INTFACELOCAL(dst)) && dstsock->sin6_scope_id) { 153 /* 154 * I'm not sure if boundary check for scope_id is done 155 * somewhere... 156 */ 157 if (dstsock->sin6_scope_id < 0 || 158 if_indexlim <= dstsock->sin6_scope_id || 159 !ifindex2ifnet[dstsock->sin6_scope_id]) { 160 *errorp = ENXIO; /* XXX: better error? */ 161 return (0); 162 } 163 ia6 = in6_ifawithscope(ifindex2ifnet[dstsock->sin6_scope_id], 164 dst); 165 if (ia6 == 0) { 166 *errorp = EADDRNOTAVAIL; 167 return (0); 168 } 169 return (&satosin6(&ia6->ia_addr)->sin6_addr); 170 } 171 172 /* 173 * If the destination address is a multicast address and 174 * the outgoing interface for the address is specified 175 * by the caller, use an address associated with the interface. 176 * Even if the outgoing interface is not specified, we also 177 * choose a loopback interface as the outgoing interface. 178 */ 179 if (IN6_IS_ADDR_MULTICAST(dst)) { 180 struct ifnet *ifp = mopts ? mopts->im6o_multicast_ifp : NULL; 181 182 if (!ifp && dstsock->sin6_scope_id) 183 ifp = ifindex2ifnet[htons(dstsock->sin6_scope_id)]; 184 185 if (ifp) { 186 ia6 = in6_ifawithscope(ifp, dst); 187 if (ia6 == 0) { 188 *errorp = EADDRNOTAVAIL; 189 return (0); 190 } 191 return (&satosin6(&ia6->ia_addr)->sin6_addr); 192 } 193 } 194 195 /* 196 * If the next hop address for the packet is specified 197 * by caller, use an address associated with the route 198 * to the next hop. 199 */ 200 { 201 struct sockaddr_in6 *sin6_next; 202 struct rtentry *rt; 203 204 if (opts && opts->ip6po_nexthop) { 205 sin6_next = satosin6(opts->ip6po_nexthop); 206 rt = nd6_lookup(&sin6_next->sin6_addr, 1, NULL); 207 if (rt) { 208 ia6 = in6_ifawithscope(rt->rt_ifp, dst); 209 if (ia6 == 0) 210 ia6 = ifatoia6(rt->rt_ifa); 211 } 212 if (ia6 == 0) { 213 *errorp = EADDRNOTAVAIL; 214 return (0); 215 } 216 return (&satosin6(&ia6->ia_addr)->sin6_addr); 217 } 218 } 219 220 /* 221 * If route is known or can be allocated now, 222 * our src addr is taken from the i/f, else punt. 223 */ 224 if (ro) { 225 if (ro->ro_rt && 226 !IN6_ARE_ADDR_EQUAL(&satosin6(&ro->ro_dst)->sin6_addr, dst)) { 227 RTFREE(ro->ro_rt); 228 ro->ro_rt = (struct rtentry *)0; 229 } 230 if (ro->ro_rt == (struct rtentry *)0 || 231 ro->ro_rt->rt_ifp == (struct ifnet *)0) { 232 struct sockaddr_in6 *sa6; 233 234 /* No route yet, so try to acquire one */ 235 bzero(&ro->ro_dst, sizeof(struct sockaddr_in6)); 236 sa6 = (struct sockaddr_in6 *)&ro->ro_dst; 237 sa6->sin6_family = AF_INET6; 238 sa6->sin6_len = sizeof(struct sockaddr_in6); 239 sa6->sin6_addr = *dst; 240 sa6->sin6_scope_id = dstsock->sin6_scope_id; 241 if (IN6_IS_ADDR_MULTICAST(dst)) { 242 ro->ro_rt = rtalloc1(&((struct route *)ro) 243 ->ro_dst, 0, 0); 244 } else { 245 rtalloc_mpath((struct route *)ro, NULL, 0); 246 } 247 } 248 249 /* 250 * in_pcbconnect() checks out IFF_LOOPBACK to skip using 251 * the address. But we don't know why it does so. 252 * It is necessary to ensure the scope even for lo0 253 * so doesn't check out IFF_LOOPBACK. 254 */ 255 256 if (ro->ro_rt) { 257 ia6 = in6_ifawithscope(ro->ro_rt->rt_ifa->ifa_ifp, dst); 258 if (ia6 == 0) /* xxx scope error ?*/ 259 ia6 = ifatoia6(ro->ro_rt->rt_ifa); 260 } 261#if 0 262 /* 263 * xxx The followings are necessary? (kazu) 264 * I don't think so. 265 * It's for SO_DONTROUTE option in IPv4.(jinmei) 266 */ 267 if (ia6 == 0) { 268 struct sockaddr_in6 sin6 = {sizeof(sin6), AF_INET6, 0}; 269 270 sin6->sin6_addr = *dst; 271 272 ia6 = ifatoia6(ifa_ifwithdstaddr(sin6tosa(&sin6))); 273 if (ia6 == 0) 274 ia6 = ifatoia6(ifa_ifwithnet(sin6tosa(&sin6))); 275 if (ia6 == 0) 276 return (0); 277 return (&satosin6(&ia6->ia_addr)->sin6_addr); 278 } 279#endif /* 0 */ 280 if (ia6 == 0) { 281 *errorp = EHOSTUNREACH; /* no route */ 282 return (0); 283 } 284 return (&satosin6(&ia6->ia_addr)->sin6_addr); 285 } 286 287 *errorp = EADDRNOTAVAIL; 288 return (0); 289} 290 291/* 292 * Default hop limit selection. The precedence is as follows: 293 * 1. Hoplimit value specified via ioctl. 294 * 2. (If the outgoing interface is detected) the current 295 * hop limit of the interface specified by router advertisement. 296 * 3. The system default hoplimit. 297*/ 298#define in6pcb inpcb 299#define in6p_hops inp_hops 300int 301in6_selecthlim(in6p, ifp) 302 struct in6pcb *in6p; 303 struct ifnet *ifp; 304{ 305 if (in6p && in6p->in6p_hops >= 0) 306 return (in6p->in6p_hops); 307 else if (ifp) 308 return (ND_IFINFO(ifp)->chlim); 309 else 310 return (ip6_defhlim); 311} 312#undef in6pcb 313#undef in6p_hops 314 315/* 316 * generate kernel-internal form (scopeid embedded into s6_addr16[1]). 317 * If the address scope of is link-local, embed the interface index in the 318 * address. The routine determines our precedence 319 * between advanced API scope/interface specification and basic API 320 * specification. 321 * 322 * this function should be nuked in the future, when we get rid of 323 * embedded scopeid thing. 324 * 325 * XXX actually, it is over-specification to return ifp against sin6_scope_id. 326 * there can be multiple interfaces that belong to a particular scope zone 327 * (in specification, we have 1:N mapping between a scope zone and interfaces). 328 * we may want to change the function to return something other than ifp. 329 */ 330int 331in6_embedscope(in6, sin6, in6p, ifpp) 332 struct in6_addr *in6; 333 const struct sockaddr_in6 *sin6; 334 struct inpcb *in6p; 335#define in6p_outputopts inp_outputopts6 336#define in6p_moptions inp_moptions6 337 struct ifnet **ifpp; 338{ 339 struct ifnet *ifp = NULL; 340 u_int32_t scopeid; 341 342 *in6 = sin6->sin6_addr; 343 scopeid = sin6->sin6_scope_id; 344 if (ifpp) 345 *ifpp = NULL; 346 347 /* 348 * don't try to read sin6->sin6_addr beyond here, since the caller may 349 * ask us to overwrite existing sockaddr_in6 350 */ 351 352 if (IN6_IS_SCOPE_EMBED(in6)) { 353 struct in6_pktinfo *pi; 354 355 /* 356 * KAME assumption: link id == interface id 357 */ 358 359 if (in6p && in6p->in6p_outputopts && 360 (pi = in6p->in6p_outputopts->ip6po_pktinfo) && 361 pi->ipi6_ifindex) { 362 ifp = ifindex2ifnet[pi->ipi6_ifindex]; 363 in6->s6_addr16[1] = htons(pi->ipi6_ifindex); 364 } else if (in6p && IN6_IS_ADDR_MULTICAST(in6) && 365 in6p->in6p_moptions && 366 in6p->in6p_moptions->im6o_multicast_ifp) { 367 ifp = in6p->in6p_moptions->im6o_multicast_ifp; 368 in6->s6_addr16[1] = htons(ifp->if_index); 369 } else if (scopeid) { 370 /* boundary check */ 371 if (scopeid < 0 || if_indexlim <= scopeid || 372 !ifindex2ifnet[scopeid]) 373 return ENXIO; /* XXX EINVAL? */ 374 ifp = ifindex2ifnet[scopeid]; 375 /*XXX assignment to 16bit from 32bit variable */ 376 in6->s6_addr16[1] = htons(scopeid & 0xffff); 377 } 378 379 if (ifpp) 380 *ifpp = ifp; 381 } 382 383 return 0; 384} 385#undef in6p_outputopts 386#undef in6p_moptions 387 388/* 389 * generate standard sockaddr_in6 from embedded form. 390 * touches sin6_addr and sin6_scope_id only. 391 * 392 * this function should be nuked in the future, when we get rid of 393 * embedded scopeid thing. 394 */ 395int 396in6_recoverscope(sin6, in6, ifp) 397 struct sockaddr_in6 *sin6; 398 const struct in6_addr *in6; 399 struct ifnet *ifp; 400{ 401 u_int32_t scopeid; 402 403 sin6->sin6_addr = *in6; 404 405 /* 406 * don't try to read *in6 beyond here, since the caller may 407 * ask us to overwrite existing sockaddr_in6 408 */ 409 410 sin6->sin6_scope_id = 0; 411 if (IN6_IS_SCOPE_EMBED(in6)) { 412 /* 413 * KAME assumption: link id == interface id 414 */ 415 scopeid = ntohs(sin6->sin6_addr.s6_addr16[1]); 416 if (scopeid) { 417 /* sanity check */ 418 if (scopeid < 0 || if_indexlim <= scopeid || 419 !ifindex2ifnet[scopeid]) 420 return ENXIO; 421 if (ifp && ifp->if_index != scopeid) 422 return ENXIO; 423 sin6->sin6_addr.s6_addr16[1] = 0; 424 sin6->sin6_scope_id = scopeid; 425 } 426 } 427 428 return 0; 429} 430 431/* 432 * just clear the embedded scope identifer. 433 */ 434void 435in6_clearscope(addr) 436 struct in6_addr *addr; 437{ 438 if (IN6_IS_SCOPE_EMBED(addr)) 439 addr->s6_addr16[1] = 0; 440} 441