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