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