Home | History | Annotate | Line # | Download | only in netinet6
in6_src.c revision 1.5.2.6
      1 /*	$NetBSD: in6_src.c,v 1.5.2.6 2001/11/14 19:18:09 nathanw 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/cdefs.h>
     69 __KERNEL_RCSID(0, "$NetBSD: in6_src.c,v 1.5.2.6 2001/11/14 19:18:09 nathanw Exp $");
     70 
     71 #include "opt_inet.h"
     72 
     73 #include <sys/param.h>
     74 #include <sys/systm.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/ioctl.h>
     81 #include <sys/errno.h>
     82 #include <sys/time.h>
     83 #include <sys/lwp.h>
     84 #include <sys/proc.h>
     85 
     86 #include <net/if.h>
     87 #include <net/route.h>
     88 
     89 #include <netinet/in.h>
     90 #include <netinet/in_var.h>
     91 #include <netinet/in_systm.h>
     92 #include <netinet/ip.h>
     93 #include <netinet/in_pcb.h>
     94 #include <netinet6/in6_var.h>
     95 #include <netinet/ip6.h>
     96 #include <netinet6/in6_pcb.h>
     97 #include <netinet6/ip6_var.h>
     98 #include <netinet6/nd6.h>
     99 #ifdef ENABLE_DEFAULT_SCOPE
    100 #include <netinet6/scope6_var.h>
    101 #endif
    102 
    103 #include <net/net_osdep.h>
    104 
    105 #include "loop.h"
    106 extern struct ifnet loif[NLOOP];
    107 
    108 /*
    109  * Return an IPv6 address, which is the most appropriate for a given
    110  * destination and user specified options.
    111  * If necessary, this function lookups the routing table and returns
    112  * an entry to the caller for later use.
    113  */
    114 struct in6_addr *
    115 in6_selectsrc(dstsock, opts, mopts, ro, laddr, errorp)
    116 	struct sockaddr_in6 *dstsock;
    117 	struct ip6_pktopts *opts;
    118 	struct ip6_moptions *mopts;
    119 	struct route_in6 *ro;
    120 	struct in6_addr *laddr;
    121 	int *errorp;
    122 {
    123 	struct in6_addr *dst;
    124 	struct in6_ifaddr *ia6 = 0;
    125 	struct in6_pktinfo *pi = NULL;
    126 
    127 	dst = &dstsock->sin6_addr;
    128 	*errorp = 0;
    129 
    130 	/*
    131 	 * If the source address is explicitly specified by the caller,
    132 	 * use it.
    133 	 */
    134 	if (opts && (pi = opts->ip6po_pktinfo) &&
    135 	    !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr))
    136 		return(&pi->ipi6_addr);
    137 
    138 	/*
    139 	 * If the source address is not specified but the socket(if any)
    140 	 * is already bound, use the bound address.
    141 	 */
    142 	if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr))
    143 		return(laddr);
    144 
    145 	/*
    146 	 * If the caller doesn't specify the source address but
    147 	 * the outgoing interface, use an address associated with
    148 	 * the interface.
    149 	 */
    150 	if (pi && pi->ipi6_ifindex) {
    151 		/* XXX boundary check is assumed to be already done. */
    152 		ia6 = in6_ifawithscope(ifindex2ifnet[pi->ipi6_ifindex],
    153 				       dst);
    154 		if (ia6 == 0) {
    155 			*errorp = EADDRNOTAVAIL;
    156 			return(0);
    157 		}
    158 		return(&satosin6(&ia6->ia_addr)->sin6_addr);
    159 	}
    160 
    161 	/*
    162 	 * If the destination address is a link-local unicast address or
    163 	 * a multicast address, and if the outgoing interface is specified
    164 	 * by the sin6_scope_id filed, use an address associated with the
    165 	 * interface.
    166 	 * XXX: We're now trying to define more specific semantics of
    167 	 *      sin6_scope_id field, so this part will be rewritten in
    168 	 *      the near future.
    169 	 */
    170 	if ((IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MULTICAST(dst)) &&
    171 	    dstsock->sin6_scope_id) {
    172 		/*
    173 		 * I'm not sure if boundary check for scope_id is done
    174 		 * somewhere...
    175 		 */
    176 		if (dstsock->sin6_scope_id < 0 ||
    177 		    if_index < dstsock->sin6_scope_id) {
    178 			*errorp = ENXIO; /* XXX: better error? */
    179 			return(0);
    180 		}
    181 		ia6 = in6_ifawithscope(ifindex2ifnet[dstsock->sin6_scope_id],
    182 				       dst);
    183 		if (ia6 == 0) {
    184 			*errorp = EADDRNOTAVAIL;
    185 			return(0);
    186 		}
    187 		return(&satosin6(&ia6->ia_addr)->sin6_addr);
    188 	}
    189 
    190 	/*
    191 	 * If the destination address is a multicast address and
    192 	 * the outgoing interface for the address is specified
    193 	 * by the caller, use an address associated with the interface.
    194 	 * There is a sanity check here; if the destination has node-local
    195 	 * scope, the outgoing interfacde should be a loopback address.
    196 	 * Even if the outgoing interface is not specified, we also
    197 	 * choose a loopback interface as the outgoing interface.
    198 	 */
    199 	if (IN6_IS_ADDR_MULTICAST(dst)) {
    200 		struct ifnet *ifp = mopts ? mopts->im6o_multicast_ifp : NULL;
    201 
    202 		if (ifp == NULL && IN6_IS_ADDR_MC_NODELOCAL(dst)) {
    203 			ifp = &loif[0];
    204 		}
    205 
    206 		if (ifp) {
    207 			ia6 = in6_ifawithscope(ifp, dst);
    208 			if (ia6 == 0) {
    209 				*errorp = EADDRNOTAVAIL;
    210 				return(0);
    211 			}
    212 			return(&satosin6(&ia6->ia_addr)->sin6_addr);
    213 		}
    214 	}
    215 
    216 	/*
    217 	 * If the next hop address for the packet is specified
    218 	 * by caller, use an address associated with the route
    219 	 * to the next hop.
    220 	 */
    221 	{
    222 		struct sockaddr_in6 *sin6_next;
    223 		struct rtentry *rt;
    224 
    225 		if (opts && opts->ip6po_nexthop) {
    226 			sin6_next = satosin6(opts->ip6po_nexthop);
    227 			rt = nd6_lookup(&sin6_next->sin6_addr, 1, NULL);
    228 			if (rt) {
    229 				ia6 = in6_ifawithscope(rt->rt_ifp, dst);
    230 				if (ia6 == 0)
    231 					ia6 = ifatoia6(rt->rt_ifa);
    232 			}
    233 			if (ia6 == 0) {
    234 				*errorp = EADDRNOTAVAIL;
    235 				return(0);
    236 			}
    237 			return(&satosin6(&ia6->ia_addr)->sin6_addr);
    238 		}
    239 	}
    240 
    241 	/*
    242 	 * If route is known or can be allocated now,
    243 	 * our src addr is taken from the i/f, else punt.
    244 	 */
    245 	if (ro) {
    246 		if (ro->ro_rt &&
    247 		    !IN6_ARE_ADDR_EQUAL(&satosin6(&ro->ro_dst)->sin6_addr, dst)) {
    248 			RTFREE(ro->ro_rt);
    249 			ro->ro_rt = (struct rtentry *)0;
    250 		}
    251 		if (ro->ro_rt == (struct rtentry *)0 ||
    252 		    ro->ro_rt->rt_ifp == (struct ifnet *)0) {
    253 			struct sockaddr_in6 *sa6;
    254 
    255 			/* No route yet, so try to acquire one */
    256 			bzero(&ro->ro_dst, sizeof(struct sockaddr_in6));
    257 			sa6 = (struct sockaddr_in6 *)&ro->ro_dst;
    258 			sa6->sin6_family = AF_INET6;
    259 			sa6->sin6_len = sizeof(struct sockaddr_in6);
    260 			sa6->sin6_addr = *dst;
    261 			sa6->sin6_scope_id = dstsock->sin6_scope_id;
    262 			if (IN6_IS_ADDR_MULTICAST(dst)) {
    263 				ro->ro_rt = rtalloc1(&((struct route *)ro)
    264 						     ->ro_dst, 0);
    265 			} else {
    266 				rtalloc((struct route *)ro);
    267 			}
    268 		}
    269 
    270 		/*
    271 		 * in_pcbconnect() checks out IFF_LOOPBACK to skip using
    272 		 * the address. But we don't know why it does so.
    273 		 * It is necessary to ensure the scope even for lo0
    274 		 * so doesn't check out IFF_LOOPBACK.
    275 		 */
    276 
    277 		if (ro->ro_rt) {
    278 			ia6 = in6_ifawithscope(ro->ro_rt->rt_ifa->ifa_ifp, dst);
    279 			if (ia6 == 0) /* xxx scope error ?*/
    280 				ia6 = ifatoia6(ro->ro_rt->rt_ifa);
    281 		}
    282 #if 0
    283 		/*
    284 		 * xxx The followings are necessary? (kazu)
    285 		 * I don't think so.
    286 		 * It's for SO_DONTROUTE option in IPv4.(jinmei)
    287 		 */
    288 		if (ia6 == 0) {
    289 			struct sockaddr_in6 sin6 = {sizeof(sin6), AF_INET6, 0};
    290 
    291 			sin6->sin6_addr = *dst;
    292 
    293 			ia6 = ifatoia6(ifa_ifwithdstaddr(sin6tosa(&sin6)));
    294 			if (ia6 == 0)
    295 				ia6 = ifatoia6(ifa_ifwithnet(sin6tosa(&sin6)));
    296 			if (ia6 == 0)
    297 				return(0);
    298 			return(&satosin6(&ia6->ia_addr)->sin6_addr);
    299 		}
    300 #endif /* 0 */
    301 		if (ia6 == 0) {
    302 			*errorp = EHOSTUNREACH;	/* no route */
    303 			return(0);
    304 		}
    305 		return(&satosin6(&ia6->ia_addr)->sin6_addr);
    306 	}
    307 
    308 	*errorp = EADDRNOTAVAIL;
    309 	return(0);
    310 }
    311 
    312 /*
    313  * Default hop limit selection. The precedence is as follows:
    314  * 1. Hoplimit value specified via ioctl.
    315  * 2. (If the outgoing interface is detected) the current
    316  *     hop limit of the interface specified by router advertisement.
    317  * 3. The system default hoplimit.
    318 */
    319 int
    320 in6_selecthlim(in6p, ifp)
    321 	struct in6pcb *in6p;
    322 	struct ifnet *ifp;
    323 {
    324 	if (in6p && in6p->in6p_hops >= 0)
    325 		return(in6p->in6p_hops);
    326 	else if (ifp)
    327 		return(nd_ifinfo[ifp->if_index].chlim);
    328 	else
    329 		return(ip6_defhlim);
    330 }
    331 
    332 /*
    333  * Find an empty port and set it to the specified PCB.
    334  */
    335 int
    336 in6_pcbsetport(laddr, in6p)
    337 	struct in6_addr *laddr;
    338 	struct in6pcb *in6p;
    339 {
    340 	struct socket *so = in6p->in6p_socket;
    341 	struct in6pcb *head = in6p->in6p_head;
    342 	u_int16_t last_port, lport = 0;
    343 	int wild = 0;
    344 	void *t;
    345 	u_int16_t min, max;
    346 
    347 	/* XXX: this is redundant when called from in6_pcbbind */
    348 	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0 &&
    349 	   ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 ||
    350 	    (so->so_options & SO_ACCEPTCONN) == 0))
    351 		wild = IN6PLOOKUP_WILDCARD;
    352 
    353 	if (in6p->in6p_flags & IN6P_LOWPORT) {
    354 #ifndef IPNOPRIVPORTS
    355 		struct proc *p = (curproc ? curproc->l_proc : 0);/* XXX */
    356 
    357 		if (p == 0 || (suser(p->p_ucred, &p->p_acflag) != 0))
    358 			return (EACCES);
    359 #endif
    360 		min = ip6_lowportmin;
    361 		max = ip6_lowportmax;
    362 	} else {
    363 		min = ip6_anonportmin;
    364 		max = ip6_anonportmax;
    365 	}
    366 
    367 	/* value out of range */
    368 	if (head->in6p_lport < min)
    369 		head->in6p_lport = min;
    370 	else if (head->in6p_lport > max)
    371 		head->in6p_lport = min;
    372 	last_port = head->in6p_lport;
    373 	goto startover;	/*to randomize*/
    374 	for (;;) {
    375 		lport = htons(head->in6p_lport);
    376 		if (IN6_IS_ADDR_V4MAPPED(laddr)) {
    377 #if 0
    378 			t = in_pcblookup_bind(&tcbtable,
    379 					      (struct in_addr *)&in6p->in6p_laddr.s6_addr32[3],
    380 					      lport);
    381 #else
    382 			t = NULL;
    383 #endif
    384 		} else {
    385 			t = in6_pcblookup(head, &zeroin6_addr, 0, laddr,
    386 					  lport, wild);
    387 		}
    388 		if (t == 0)
    389 			break;
    390 	  startover:
    391 		if (head->in6p_lport >= max)
    392 			head->in6p_lport = min;
    393 		else
    394 			head->in6p_lport++;
    395 		if (head->in6p_lport == last_port)
    396 			return (EADDRINUSE);
    397 	}
    398 
    399 	in6p->in6p_lport = lport;
    400 	return(0);		/* success */
    401 }
    402 
    403 /*
    404  * generate kernel-internal form (scopeid embedded into s6_addr16[1]).
    405  * If the address scope of is link-local, embed the interface index in the
    406  * address.  The routine determines our precedence
    407  * between advanced API scope/interface specification and basic API
    408  * specification.
    409  *
    410  * this function should be nuked in the future, when we get rid of
    411  * embedded scopeid thing.
    412  *
    413  * XXX actually, it is over-specification to return ifp against sin6_scope_id.
    414  * there can be multiple interfaces that belong to a particular scope zone
    415  * (in specification, we have 1:N mapping between a scope zone and interfaces).
    416  * we may want to change the function to return something other than ifp.
    417  */
    418 int
    419 in6_embedscope(in6, sin6, in6p, ifpp)
    420 	struct in6_addr *in6;
    421 	const struct sockaddr_in6 *sin6;
    422 	struct in6pcb *in6p;
    423 	struct ifnet **ifpp;
    424 {
    425 	struct ifnet *ifp = NULL;
    426 	u_int32_t scopeid;
    427 
    428 	*in6 = sin6->sin6_addr;
    429 	scopeid = sin6->sin6_scope_id;
    430 	if (ifpp)
    431 		*ifpp = NULL;
    432 
    433 	/*
    434 	 * don't try to read sin6->sin6_addr beyond here, since the caller may
    435 	 * ask us to overwrite existing sockaddr_in6
    436 	 */
    437 
    438 #ifdef ENABLE_DEFAULT_SCOPE
    439 	if (scopeid == 0)
    440 		scopeid = scope6_addr2default(in6);
    441 #endif
    442 
    443 	if (IN6_IS_SCOPE_LINKLOCAL(in6)) {
    444 		struct in6_pktinfo *pi;
    445 
    446 		/*
    447 		 * KAME assumption: link id == interface id
    448 		 */
    449 
    450 		if (in6p && in6p->in6p_outputopts &&
    451 		    (pi = in6p->in6p_outputopts->ip6po_pktinfo) &&
    452 		    pi->ipi6_ifindex) {
    453 			ifp = ifindex2ifnet[pi->ipi6_ifindex];
    454 			in6->s6_addr16[1] = htons(pi->ipi6_ifindex);
    455 		} else if (in6p && IN6_IS_ADDR_MULTICAST(in6) &&
    456 			   in6p->in6p_moptions &&
    457 			   in6p->in6p_moptions->im6o_multicast_ifp) {
    458 			ifp = in6p->in6p_moptions->im6o_multicast_ifp;
    459 			in6->s6_addr16[1] = htons(ifp->if_index);
    460 		} else if (scopeid) {
    461 			/* boundary check */
    462 			if (scopeid < 0 || if_index < scopeid)
    463 				return ENXIO;  /* XXX EINVAL? */
    464 			ifp = ifindex2ifnet[scopeid];
    465 			/* XXX assignment to 16bit from 32bit variable */
    466 			in6->s6_addr16[1] = htons(scopeid & 0xffff);
    467 		}
    468 
    469 		if (ifpp)
    470 			*ifpp = ifp;
    471 	}
    472 
    473 	return 0;
    474 }
    475 
    476 /*
    477  * generate standard sockaddr_in6 from embedded form.
    478  * touches sin6_addr and sin6_scope_id only.
    479  *
    480  * this function should be nuked in the future, when we get rid of
    481  * embedded scopeid thing.
    482  */
    483 int
    484 in6_recoverscope(sin6, in6, ifp)
    485 	struct sockaddr_in6 *sin6;
    486 	const struct in6_addr *in6;
    487 	struct ifnet *ifp;
    488 {
    489 	u_int32_t scopeid;
    490 
    491 	sin6->sin6_addr = *in6;
    492 
    493 	/*
    494 	 * don't try to read *in6 beyond here, since the caller may
    495 	 * ask us to overwrite existing sockaddr_in6
    496 	 */
    497 
    498 	sin6->sin6_scope_id = 0;
    499 	if (IN6_IS_SCOPE_LINKLOCAL(in6)) {
    500 		/*
    501 		 * KAME assumption: link id == interface id
    502 		 */
    503 		scopeid = ntohs(sin6->sin6_addr.s6_addr16[1]);
    504 		if (scopeid) {
    505 			/* sanity check */
    506 			if (scopeid < 0 || if_index < scopeid)
    507 				return ENXIO;
    508 			if (ifp && ifp->if_index != scopeid)
    509 				return ENXIO;
    510 			sin6->sin6_addr.s6_addr16[1] = 0;
    511 			sin6->sin6_scope_id = scopeid;
    512 		}
    513 	}
    514 
    515 	return 0;
    516 }
    517 
    518 /*
    519  * just clear the embedded scope identifer.
    520  * XXX: currently used for bsdi4 only as a supplement function.
    521  */
    522 void
    523 in6_clearscope(addr)
    524 	struct in6_addr *addr;
    525 {
    526 	if (IN6_IS_SCOPE_LINKLOCAL(addr))
    527 		addr->s6_addr16[1] = 0;
    528 }
    529