Home | History | Annotate | Line # | Download | only in netinet6
in6_src.c revision 1.20
      1  1.20  drochner /*	$NetBSD: in6_src.c,v 1.20 2005/02/01 14:56:17 drochner Exp $	*/
      2   1.5    itojun /*	$KAME: in6_src.c,v 1.36 2001/02/06 04:08:17 itojun Exp $	*/
      3   1.1    itojun 
      4   1.1    itojun /*
      5   1.1    itojun  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      6   1.1    itojun  * All rights reserved.
      7   1.1    itojun  *
      8   1.1    itojun  * Redistribution and use in source and binary forms, with or without
      9   1.1    itojun  * modification, are permitted provided that the following conditions
     10   1.1    itojun  * are met:
     11   1.1    itojun  * 1. Redistributions of source code must retain the above copyright
     12   1.1    itojun  *    notice, this list of conditions and the following disclaimer.
     13   1.1    itojun  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.1    itojun  *    notice, this list of conditions and the following disclaimer in the
     15   1.1    itojun  *    documentation and/or other materials provided with the distribution.
     16   1.1    itojun  * 3. Neither the name of the project nor the names of its contributors
     17   1.1    itojun  *    may be used to endorse or promote products derived from this software
     18   1.1    itojun  *    without specific prior written permission.
     19   1.1    itojun  *
     20   1.1    itojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     21   1.1    itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22   1.1    itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23   1.1    itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     24   1.1    itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25   1.1    itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26   1.1    itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27   1.1    itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28   1.1    itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29   1.1    itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30   1.1    itojun  * SUCH DAMAGE.
     31   1.1    itojun  */
     32   1.1    itojun 
     33   1.1    itojun /*
     34   1.1    itojun  * Copyright (c) 1982, 1986, 1991, 1993
     35   1.1    itojun  *	The Regents of the University of California.  All rights reserved.
     36   1.1    itojun  *
     37   1.1    itojun  * Redistribution and use in source and binary forms, with or without
     38   1.1    itojun  * modification, are permitted provided that the following conditions
     39   1.1    itojun  * are met:
     40   1.1    itojun  * 1. Redistributions of source code must retain the above copyright
     41   1.1    itojun  *    notice, this list of conditions and the following disclaimer.
     42   1.1    itojun  * 2. Redistributions in binary form must reproduce the above copyright
     43   1.1    itojun  *    notice, this list of conditions and the following disclaimer in the
     44   1.1    itojun  *    documentation and/or other materials provided with the distribution.
     45  1.16       agc  * 3. Neither the name of the University nor the names of its contributors
     46   1.1    itojun  *    may be used to endorse or promote products derived from this software
     47   1.1    itojun  *    without specific prior written permission.
     48   1.1    itojun  *
     49   1.1    itojun  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     50   1.1    itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     51   1.1    itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     52   1.1    itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     53   1.1    itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     54   1.1    itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     55   1.1    itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     56   1.1    itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     57   1.1    itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     58   1.1    itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     59   1.1    itojun  * SUCH DAMAGE.
     60   1.1    itojun  *
     61   1.1    itojun  *	@(#)in_pcb.c	8.2 (Berkeley) 1/4/94
     62   1.1    itojun  */
     63   1.9     lukem 
     64   1.9     lukem #include <sys/cdefs.h>
     65  1.20  drochner __KERNEL_RCSID(0, "$NetBSD: in6_src.c,v 1.20 2005/02/01 14:56:17 drochner Exp $");
     66   1.1    itojun 
     67   1.1    itojun #include "opt_inet.h"
     68   1.1    itojun 
     69   1.1    itojun #include <sys/param.h>
     70   1.1    itojun #include <sys/systm.h>
     71   1.1    itojun #include <sys/malloc.h>
     72   1.1    itojun #include <sys/mbuf.h>
     73   1.1    itojun #include <sys/protosw.h>
     74   1.1    itojun #include <sys/socket.h>
     75   1.1    itojun #include <sys/socketvar.h>
     76   1.1    itojun #include <sys/ioctl.h>
     77   1.1    itojun #include <sys/errno.h>
     78   1.1    itojun #include <sys/time.h>
     79   1.1    itojun #include <sys/proc.h>
     80   1.1    itojun 
     81   1.1    itojun #include <net/if.h>
     82   1.1    itojun #include <net/route.h>
     83   1.1    itojun 
     84   1.1    itojun #include <netinet/in.h>
     85   1.1    itojun #include <netinet/in_var.h>
     86   1.1    itojun #include <netinet/in_systm.h>
     87   1.1    itojun #include <netinet/ip.h>
     88   1.1    itojun #include <netinet/in_pcb.h>
     89   1.1    itojun #include <netinet6/in6_var.h>
     90   1.1    itojun #include <netinet/ip6.h>
     91   1.1    itojun #include <netinet6/in6_pcb.h>
     92   1.1    itojun #include <netinet6/ip6_var.h>
     93   1.1    itojun #include <netinet6/nd6.h>
     94   1.2    itojun #ifdef ENABLE_DEFAULT_SCOPE
     95  1.13    itojun #include <netinet6/scope6_var.h>
     96   1.2    itojun #endif
     97   1.1    itojun 
     98   1.1    itojun #include <net/net_osdep.h>
     99   1.1    itojun 
    100   1.1    itojun /*
    101   1.5    itojun  * Return an IPv6 address, which is the most appropriate for a given
    102   1.1    itojun  * destination and user specified options.
    103   1.5    itojun  * If necessary, this function lookups the routing table and returns
    104   1.1    itojun  * an entry to the caller for later use.
    105   1.1    itojun  */
    106   1.1    itojun struct in6_addr *
    107   1.1    itojun in6_selectsrc(dstsock, opts, mopts, ro, laddr, errorp)
    108   1.1    itojun 	struct sockaddr_in6 *dstsock;
    109   1.1    itojun 	struct ip6_pktopts *opts;
    110   1.1    itojun 	struct ip6_moptions *mopts;
    111   1.1    itojun 	struct route_in6 *ro;
    112   1.1    itojun 	struct in6_addr *laddr;
    113   1.1    itojun 	int *errorp;
    114   1.1    itojun {
    115   1.1    itojun 	struct in6_addr *dst;
    116   1.1    itojun 	struct in6_ifaddr *ia6 = 0;
    117   1.1    itojun 	struct in6_pktinfo *pi = NULL;
    118   1.1    itojun 
    119   1.1    itojun 	dst = &dstsock->sin6_addr;
    120   1.1    itojun 	*errorp = 0;
    121   1.1    itojun 
    122   1.1    itojun 	/*
    123   1.1    itojun 	 * If the source address is explicitly specified by the caller,
    124   1.1    itojun 	 * use it.
    125   1.1    itojun 	 */
    126   1.1    itojun 	if (opts && (pi = opts->ip6po_pktinfo) &&
    127   1.1    itojun 	    !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr))
    128  1.15    itojun 		return (&pi->ipi6_addr);
    129   1.1    itojun 
    130   1.1    itojun 	/*
    131   1.1    itojun 	 * If the source address is not specified but the socket(if any)
    132   1.1    itojun 	 * is already bound, use the bound address.
    133   1.1    itojun 	 */
    134   1.1    itojun 	if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr))
    135  1.15    itojun 		return (laddr);
    136   1.1    itojun 
    137   1.1    itojun 	/*
    138   1.1    itojun 	 * If the caller doesn't specify the source address but
    139   1.1    itojun 	 * the outgoing interface, use an address associated with
    140   1.1    itojun 	 * the interface.
    141   1.1    itojun 	 */
    142   1.1    itojun 	if (pi && pi->ipi6_ifindex) {
    143   1.1    itojun 		/* XXX boundary check is assumed to be already done. */
    144   1.1    itojun 		ia6 = in6_ifawithscope(ifindex2ifnet[pi->ipi6_ifindex],
    145   1.1    itojun 				       dst);
    146   1.1    itojun 		if (ia6 == 0) {
    147   1.1    itojun 			*errorp = EADDRNOTAVAIL;
    148  1.15    itojun 			return (0);
    149   1.1    itojun 		}
    150  1.15    itojun 		return (&satosin6(&ia6->ia_addr)->sin6_addr);
    151   1.1    itojun 	}
    152   1.1    itojun 
    153   1.1    itojun 	/*
    154   1.1    itojun 	 * If the destination address is a link-local unicast address or
    155   1.1    itojun 	 * a multicast address, and if the outgoing interface is specified
    156   1.1    itojun 	 * by the sin6_scope_id filed, use an address associated with the
    157   1.1    itojun 	 * interface.
    158   1.1    itojun 	 * XXX: We're now trying to define more specific semantics of
    159   1.1    itojun 	 *      sin6_scope_id field, so this part will be rewritten in
    160   1.1    itojun 	 *      the near future.
    161   1.1    itojun 	 */
    162  1.20  drochner 	if ((IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MC_LINKLOCAL(dst)) &&
    163   1.1    itojun 	    dstsock->sin6_scope_id) {
    164   1.1    itojun 		/*
    165   1.1    itojun 		 * I'm not sure if boundary check for scope_id is done
    166   1.1    itojun 		 * somewhere...
    167   1.1    itojun 		 */
    168   1.1    itojun 		if (dstsock->sin6_scope_id < 0 ||
    169  1.18    itojun 		    if_indexlim <= dstsock->sin6_scope_id ||
    170  1.18    itojun 		    !ifindex2ifnet[dstsock->sin6_scope_id]) {
    171   1.1    itojun 			*errorp = ENXIO; /* XXX: better error? */
    172  1.15    itojun 			return (0);
    173   1.1    itojun 		}
    174   1.1    itojun 		ia6 = in6_ifawithscope(ifindex2ifnet[dstsock->sin6_scope_id],
    175   1.1    itojun 				       dst);
    176   1.1    itojun 		if (ia6 == 0) {
    177   1.1    itojun 			*errorp = EADDRNOTAVAIL;
    178  1.15    itojun 			return (0);
    179   1.1    itojun 		}
    180  1.15    itojun 		return (&satosin6(&ia6->ia_addr)->sin6_addr);
    181   1.1    itojun 	}
    182   1.1    itojun 
    183   1.1    itojun 	/*
    184   1.1    itojun 	 * If the destination address is a multicast address and
    185   1.1    itojun 	 * the outgoing interface for the address is specified
    186   1.1    itojun 	 * by the caller, use an address associated with the interface.
    187   1.1    itojun 	 * There is a sanity check here; if the destination has node-local
    188   1.1    itojun 	 * scope, the outgoing interfacde should be a loopback address.
    189   1.1    itojun 	 * Even if the outgoing interface is not specified, we also
    190   1.1    itojun 	 * choose a loopback interface as the outgoing interface.
    191   1.1    itojun 	 */
    192   1.1    itojun 	if (IN6_IS_ADDR_MULTICAST(dst)) {
    193   1.1    itojun 		struct ifnet *ifp = mopts ? mopts->im6o_multicast_ifp : NULL;
    194   1.1    itojun 
    195  1.19     peter 		if (ifp == NULL && IN6_IS_ADDR_MC_NODELOCAL(dst))
    196  1.19     peter 			ifp = lo0ifp;
    197   1.1    itojun 
    198   1.1    itojun 		if (ifp) {
    199   1.1    itojun 			ia6 = in6_ifawithscope(ifp, dst);
    200   1.1    itojun 			if (ia6 == 0) {
    201   1.1    itojun 				*errorp = EADDRNOTAVAIL;
    202  1.15    itojun 				return (0);
    203   1.1    itojun 			}
    204  1.15    itojun 			return (&satosin6(&ia6->ia_addr)->sin6_addr);
    205   1.1    itojun 		}
    206   1.1    itojun 	}
    207   1.1    itojun 
    208   1.1    itojun 	/*
    209   1.1    itojun 	 * If the next hop address for the packet is specified
    210   1.1    itojun 	 * by caller, use an address associated with the route
    211   1.1    itojun 	 * to the next hop.
    212   1.1    itojun 	 */
    213   1.1    itojun 	{
    214   1.1    itojun 		struct sockaddr_in6 *sin6_next;
    215   1.1    itojun 		struct rtentry *rt;
    216   1.1    itojun 
    217   1.1    itojun 		if (opts && opts->ip6po_nexthop) {
    218   1.1    itojun 			sin6_next = satosin6(opts->ip6po_nexthop);
    219   1.1    itojun 			rt = nd6_lookup(&sin6_next->sin6_addr, 1, NULL);
    220   1.1    itojun 			if (rt) {
    221   1.1    itojun 				ia6 = in6_ifawithscope(rt->rt_ifp, dst);
    222   1.1    itojun 				if (ia6 == 0)
    223   1.1    itojun 					ia6 = ifatoia6(rt->rt_ifa);
    224   1.1    itojun 			}
    225   1.1    itojun 			if (ia6 == 0) {
    226   1.1    itojun 				*errorp = EADDRNOTAVAIL;
    227  1.15    itojun 				return (0);
    228   1.1    itojun 			}
    229  1.15    itojun 			return (&satosin6(&ia6->ia_addr)->sin6_addr);
    230   1.1    itojun 		}
    231   1.1    itojun 	}
    232   1.1    itojun 
    233   1.1    itojun 	/*
    234   1.1    itojun 	 * If route is known or can be allocated now,
    235   1.1    itojun 	 * our src addr is taken from the i/f, else punt.
    236  1.10    itojun 	 * Note that we should check the address family of the
    237  1.13    itojun 	 * cached destination, in case of sharing the cache with IPv4.
    238   1.1    itojun 	 */
    239   1.1    itojun 	if (ro) {
    240   1.1    itojun 		if (ro->ro_rt &&
    241  1.10    itojun 		    (ro->ro_dst.sin6_family != AF_INET6 ||
    242  1.10    itojun 		     !IN6_ARE_ADDR_EQUAL(&satosin6(&ro->ro_dst)->sin6_addr, dst))) {
    243   1.1    itojun 			RTFREE(ro->ro_rt);
    244   1.1    itojun 			ro->ro_rt = (struct rtentry *)0;
    245   1.1    itojun 		}
    246   1.1    itojun 		if (ro->ro_rt == (struct rtentry *)0 ||
    247   1.1    itojun 		    ro->ro_rt->rt_ifp == (struct ifnet *)0) {
    248   1.5    itojun 			struct sockaddr_in6 *sa6;
    249   1.5    itojun 
    250   1.1    itojun 			/* No route yet, so try to acquire one */
    251   1.1    itojun 			bzero(&ro->ro_dst, sizeof(struct sockaddr_in6));
    252   1.5    itojun 			sa6 = (struct sockaddr_in6 *)&ro->ro_dst;
    253   1.5    itojun 			sa6->sin6_family = AF_INET6;
    254   1.5    itojun 			sa6->sin6_len = sizeof(struct sockaddr_in6);
    255   1.5    itojun 			sa6->sin6_addr = *dst;
    256   1.5    itojun 			sa6->sin6_scope_id = dstsock->sin6_scope_id;
    257   1.1    itojun 			if (IN6_IS_ADDR_MULTICAST(dst)) {
    258   1.1    itojun 				ro->ro_rt = rtalloc1(&((struct route *)ro)
    259   1.1    itojun 						     ->ro_dst, 0);
    260   1.1    itojun 			} else {
    261   1.1    itojun 				rtalloc((struct route *)ro);
    262   1.1    itojun 			}
    263   1.1    itojun 		}
    264   1.1    itojun 
    265   1.1    itojun 		/*
    266   1.1    itojun 		 * in_pcbconnect() checks out IFF_LOOPBACK to skip using
    267   1.1    itojun 		 * the address. But we don't know why it does so.
    268   1.1    itojun 		 * It is necessary to ensure the scope even for lo0
    269   1.1    itojun 		 * so doesn't check out IFF_LOOPBACK.
    270   1.1    itojun 		 */
    271   1.1    itojun 
    272   1.1    itojun 		if (ro->ro_rt) {
    273   1.1    itojun 			ia6 = in6_ifawithscope(ro->ro_rt->rt_ifa->ifa_ifp, dst);
    274   1.1    itojun 			if (ia6 == 0) /* xxx scope error ?*/
    275   1.1    itojun 				ia6 = ifatoia6(ro->ro_rt->rt_ifa);
    276   1.1    itojun 		}
    277   1.1    itojun #if 0
    278   1.1    itojun 		/*
    279   1.1    itojun 		 * xxx The followings are necessary? (kazu)
    280   1.1    itojun 		 * I don't think so.
    281   1.1    itojun 		 * It's for SO_DONTROUTE option in IPv4.(jinmei)
    282   1.1    itojun 		 */
    283   1.1    itojun 		if (ia6 == 0) {
    284   1.1    itojun 			struct sockaddr_in6 sin6 = {sizeof(sin6), AF_INET6, 0};
    285   1.1    itojun 
    286   1.1    itojun 			sin6->sin6_addr = *dst;
    287   1.1    itojun 
    288   1.1    itojun 			ia6 = ifatoia6(ifa_ifwithdstaddr(sin6tosa(&sin6)));
    289   1.1    itojun 			if (ia6 == 0)
    290   1.1    itojun 				ia6 = ifatoia6(ifa_ifwithnet(sin6tosa(&sin6)));
    291   1.1    itojun 			if (ia6 == 0)
    292  1.15    itojun 				return (0);
    293  1.15    itojun 			return (&satosin6(&ia6->ia_addr)->sin6_addr);
    294   1.1    itojun 		}
    295   1.1    itojun #endif /* 0 */
    296   1.1    itojun 		if (ia6 == 0) {
    297   1.1    itojun 			*errorp = EHOSTUNREACH;	/* no route */
    298  1.15    itojun 			return (0);
    299   1.1    itojun 		}
    300  1.15    itojun 		return (&satosin6(&ia6->ia_addr)->sin6_addr);
    301   1.1    itojun 	}
    302   1.1    itojun 
    303   1.1    itojun 	*errorp = EADDRNOTAVAIL;
    304  1.15    itojun 	return (0);
    305   1.1    itojun }
    306   1.1    itojun 
    307   1.1    itojun /*
    308   1.1    itojun  * Default hop limit selection. The precedence is as follows:
    309   1.1    itojun  * 1. Hoplimit value specified via ioctl.
    310   1.1    itojun  * 2. (If the outgoing interface is detected) the current
    311   1.1    itojun  *     hop limit of the interface specified by router advertisement.
    312   1.1    itojun  * 3. The system default hoplimit.
    313   1.1    itojun */
    314   1.1    itojun int
    315   1.1    itojun in6_selecthlim(in6p, ifp)
    316   1.1    itojun 	struct in6pcb *in6p;
    317   1.1    itojun 	struct ifnet *ifp;
    318   1.1    itojun {
    319  1.17    itojun 	if (in6p && in6p->in6p_af != AF_INET6)
    320  1.17    itojun 		return (-1);
    321  1.17    itojun 
    322   1.1    itojun 	if (in6p && in6p->in6p_hops >= 0)
    323  1.15    itojun 		return (in6p->in6p_hops);
    324   1.1    itojun 	else if (ifp)
    325  1.15    itojun 		return (ND_IFINFO(ifp)->chlim);
    326   1.1    itojun 	else
    327  1.15    itojun 		return (ip6_defhlim);
    328   1.1    itojun }
    329   1.1    itojun 
    330   1.1    itojun /*
    331   1.1    itojun  * Find an empty port and set it to the specified PCB.
    332   1.1    itojun  */
    333   1.1    itojun int
    334  1.14    itojun in6_pcbsetport(laddr, in6p, p)
    335   1.1    itojun 	struct in6_addr *laddr;
    336   1.1    itojun 	struct in6pcb *in6p;
    337  1.14    itojun 	struct proc *p;
    338   1.1    itojun {
    339   1.1    itojun 	struct socket *so = in6p->in6p_socket;
    340  1.17    itojun 	struct inpcbtable *table = in6p->in6p_table;
    341  1.17    itojun 	int cnt;
    342  1.17    itojun 	u_int16_t min, max;
    343  1.17    itojun 	u_int16_t lport, *lastport;
    344   1.1    itojun 	int wild = 0;
    345   1.1    itojun 	void *t;
    346  1.17    itojun 
    347  1.17    itojun 	if (in6p->in6p_af != AF_INET6)
    348  1.17    itojun 		return (EINVAL);
    349   1.1    itojun 
    350   1.1    itojun 	/* XXX: this is redundant when called from in6_pcbbind */
    351   1.1    itojun 	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0 &&
    352   1.1    itojun 	   ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 ||
    353   1.1    itojun 	    (so->so_options & SO_ACCEPTCONN) == 0))
    354  1.17    itojun 		wild = 1;
    355   1.1    itojun 
    356   1.1    itojun 	if (in6p->in6p_flags & IN6P_LOWPORT) {
    357   1.3    itojun #ifndef IPNOPRIVPORTS
    358   1.1    itojun 		if (p == 0 || (suser(p->p_ucred, &p->p_acflag) != 0))
    359   1.1    itojun 			return (EACCES);
    360   1.3    itojun #endif
    361   1.4    itojun 		min = ip6_lowportmin;
    362   1.4    itojun 		max = ip6_lowportmax;
    363  1.17    itojun 		lastport = &table->inpt_lastlow;
    364   1.1    itojun 	} else {
    365   1.4    itojun 		min = ip6_anonportmin;
    366   1.4    itojun 		max = ip6_anonportmax;
    367  1.17    itojun 		lastport = &table->inpt_lastport;
    368   1.1    itojun 	}
    369  1.17    itojun 	if (min > max) {	/* sanity check */
    370  1.17    itojun 		u_int16_t swp;
    371   1.1    itojun 
    372  1.17    itojun 		swp = min;
    373  1.17    itojun 		min = max;
    374  1.17    itojun 		max = swp;
    375  1.17    itojun 	}
    376  1.17    itojun 
    377  1.17    itojun 	lport = *lastport - 1;
    378  1.17    itojun 	for (cnt = max - min + 1; cnt; cnt--, lport--) {
    379  1.17    itojun 		if (lport < min || lport > max)
    380  1.17    itojun 			lport = max;
    381  1.17    itojun #ifdef INET
    382   1.1    itojun 		if (IN6_IS_ADDR_V4MAPPED(laddr)) {
    383  1.17    itojun 			t = in_pcblookup_port(table,
    384  1.17    itojun 			    *(struct in_addr *)&in6p->in6p_laddr.s6_addr32[3],
    385  1.17    itojun 			    lport, wild);
    386  1.17    itojun 		} else
    387   1.1    itojun #endif
    388  1.17    itojun 		{
    389  1.17    itojun 			t = in6_pcblookup_port(table, laddr, lport, wild);
    390   1.1    itojun 		}
    391   1.1    itojun 		if (t == 0)
    392  1.17    itojun 			goto found;
    393   1.1    itojun 	}
    394   1.1    itojun 
    395  1.17    itojun 	return (EAGAIN);
    396  1.17    itojun 
    397  1.17    itojun found:
    398  1.17    itojun 	in6p->in6p_flags |= IN6P_ANONPORT;
    399  1.17    itojun 	*lastport = lport;
    400  1.17    itojun 	in6p->in6p_lport = htons(lport);
    401  1.17    itojun 	in6_pcbstate(in6p, IN6P_BOUND);
    402  1.15    itojun 	return (0);		/* success */
    403   1.2    itojun }
    404   1.2    itojun 
    405   1.2    itojun /*
    406   1.2    itojun  * generate kernel-internal form (scopeid embedded into s6_addr16[1]).
    407   1.2    itojun  * If the address scope of is link-local, embed the interface index in the
    408   1.2    itojun  * address.  The routine determines our precedence
    409   1.2    itojun  * between advanced API scope/interface specification and basic API
    410   1.2    itojun  * specification.
    411   1.2    itojun  *
    412   1.2    itojun  * this function should be nuked in the future, when we get rid of
    413   1.2    itojun  * embedded scopeid thing.
    414   1.2    itojun  *
    415   1.2    itojun  * XXX actually, it is over-specification to return ifp against sin6_scope_id.
    416   1.2    itojun  * there can be multiple interfaces that belong to a particular scope zone
    417   1.2    itojun  * (in specification, we have 1:N mapping between a scope zone and interfaces).
    418   1.2    itojun  * we may want to change the function to return something other than ifp.
    419   1.2    itojun  */
    420   1.2    itojun int
    421   1.2    itojun in6_embedscope(in6, sin6, in6p, ifpp)
    422   1.2    itojun 	struct in6_addr *in6;
    423   1.2    itojun 	const struct sockaddr_in6 *sin6;
    424   1.2    itojun 	struct in6pcb *in6p;
    425   1.2    itojun 	struct ifnet **ifpp;
    426   1.2    itojun {
    427   1.2    itojun 	struct ifnet *ifp = NULL;
    428   1.2    itojun 	u_int32_t scopeid;
    429   1.2    itojun 
    430   1.2    itojun 	*in6 = sin6->sin6_addr;
    431   1.2    itojun 	scopeid = sin6->sin6_scope_id;
    432   1.2    itojun 	if (ifpp)
    433   1.2    itojun 		*ifpp = NULL;
    434   1.2    itojun 
    435   1.2    itojun 	/*
    436   1.2    itojun 	 * don't try to read sin6->sin6_addr beyond here, since the caller may
    437   1.2    itojun 	 * ask us to overwrite existing sockaddr_in6
    438   1.2    itojun 	 */
    439   1.2    itojun 
    440   1.2    itojun #ifdef ENABLE_DEFAULT_SCOPE
    441   1.2    itojun 	if (scopeid == 0)
    442   1.2    itojun 		scopeid = scope6_addr2default(in6);
    443   1.2    itojun #endif
    444   1.2    itojun 
    445   1.2    itojun 	if (IN6_IS_SCOPE_LINKLOCAL(in6)) {
    446   1.2    itojun 		struct in6_pktinfo *pi;
    447   1.2    itojun 
    448   1.2    itojun 		/*
    449   1.2    itojun 		 * KAME assumption: link id == interface id
    450   1.2    itojun 		 */
    451   1.2    itojun 
    452   1.2    itojun 		if (in6p && in6p->in6p_outputopts &&
    453   1.2    itojun 		    (pi = in6p->in6p_outputopts->ip6po_pktinfo) &&
    454   1.2    itojun 		    pi->ipi6_ifindex) {
    455   1.2    itojun 			ifp = ifindex2ifnet[pi->ipi6_ifindex];
    456   1.2    itojun 			in6->s6_addr16[1] = htons(pi->ipi6_ifindex);
    457   1.2    itojun 		} else if (in6p && IN6_IS_ADDR_MULTICAST(in6) &&
    458   1.2    itojun 			   in6p->in6p_moptions &&
    459   1.2    itojun 			   in6p->in6p_moptions->im6o_multicast_ifp) {
    460   1.2    itojun 			ifp = in6p->in6p_moptions->im6o_multicast_ifp;
    461   1.2    itojun 			in6->s6_addr16[1] = htons(ifp->if_index);
    462   1.2    itojun 		} else if (scopeid) {
    463   1.2    itojun 			/* boundary check */
    464  1.18    itojun 			if (scopeid < 0 || if_indexlim <= scopeid ||
    465  1.18    itojun 			    !ifindex2ifnet[scopeid])
    466   1.2    itojun 				return ENXIO;  /* XXX EINVAL? */
    467   1.2    itojun 			ifp = ifindex2ifnet[scopeid];
    468   1.8    itojun 			/* XXX assignment to 16bit from 32bit variable */
    469   1.2    itojun 			in6->s6_addr16[1] = htons(scopeid & 0xffff);
    470   1.2    itojun 		}
    471   1.2    itojun 
    472   1.2    itojun 		if (ifpp)
    473   1.2    itojun 			*ifpp = ifp;
    474   1.2    itojun 	}
    475   1.2    itojun 
    476   1.2    itojun 	return 0;
    477   1.2    itojun }
    478   1.2    itojun 
    479   1.2    itojun /*
    480   1.2    itojun  * generate standard sockaddr_in6 from embedded form.
    481   1.2    itojun  * touches sin6_addr and sin6_scope_id only.
    482   1.2    itojun  *
    483   1.2    itojun  * this function should be nuked in the future, when we get rid of
    484   1.2    itojun  * embedded scopeid thing.
    485   1.2    itojun  */
    486   1.2    itojun int
    487   1.2    itojun in6_recoverscope(sin6, in6, ifp)
    488   1.2    itojun 	struct sockaddr_in6 *sin6;
    489   1.2    itojun 	const struct in6_addr *in6;
    490   1.2    itojun 	struct ifnet *ifp;
    491   1.2    itojun {
    492   1.2    itojun 	u_int32_t scopeid;
    493   1.2    itojun 
    494   1.2    itojun 	sin6->sin6_addr = *in6;
    495   1.2    itojun 
    496   1.2    itojun 	/*
    497   1.2    itojun 	 * don't try to read *in6 beyond here, since the caller may
    498   1.2    itojun 	 * ask us to overwrite existing sockaddr_in6
    499   1.2    itojun 	 */
    500   1.2    itojun 
    501   1.2    itojun 	sin6->sin6_scope_id = 0;
    502   1.2    itojun 	if (IN6_IS_SCOPE_LINKLOCAL(in6)) {
    503   1.2    itojun 		/*
    504   1.2    itojun 		 * KAME assumption: link id == interface id
    505   1.2    itojun 		 */
    506   1.2    itojun 		scopeid = ntohs(sin6->sin6_addr.s6_addr16[1]);
    507   1.2    itojun 		if (scopeid) {
    508   1.2    itojun 			/* sanity check */
    509  1.18    itojun 			if (scopeid < 0 || if_indexlim <= scopeid ||
    510  1.18    itojun 			    !ifindex2ifnet[scopeid])
    511   1.2    itojun 				return ENXIO;
    512   1.2    itojun 			if (ifp && ifp->if_index != scopeid)
    513   1.2    itojun 				return ENXIO;
    514   1.2    itojun 			sin6->sin6_addr.s6_addr16[1] = 0;
    515   1.2    itojun 			sin6->sin6_scope_id = scopeid;
    516   1.2    itojun 		}
    517   1.2    itojun 	}
    518   1.2    itojun 
    519   1.2    itojun 	return 0;
    520   1.5    itojun }
    521   1.5    itojun 
    522   1.5    itojun /*
    523   1.5    itojun  * just clear the embedded scope identifer.
    524   1.5    itojun  */
    525   1.5    itojun void
    526   1.5    itojun in6_clearscope(addr)
    527   1.5    itojun 	struct in6_addr *addr;
    528   1.5    itojun {
    529   1.5    itojun 	if (IN6_IS_SCOPE_LINKLOCAL(addr))
    530   1.5    itojun 		addr->s6_addr16[1] = 0;
    531   1.1    itojun }
    532