Home | History | Annotate | Line # | Download | only in netinet6
in6_pcb.c revision 1.13
      1 /*	$NetBSD: in6_pcb.c,v 1.13 2000/01/26 17:06:36 itojun Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the project nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Copyright (c) 1982, 1986, 1991, 1993
     34  *	The Regents of the University of California.  All rights reserved.
     35  *
     36  * Redistribution and use in source and binary forms, with or without
     37  * modification, are permitted provided that the following conditions
     38  * are met:
     39  * 1. Redistributions of source code must retain the above copyright
     40  *    notice, this list of conditions and the following disclaimer.
     41  * 2. Redistributions in binary form must reproduce the above copyright
     42  *    notice, this list of conditions and the following disclaimer in the
     43  *    documentation and/or other materials provided with the distribution.
     44  * 3. All advertising materials mentioning features or use of this software
     45  *    must display the following acknowledgement:
     46  *	This product includes software developed by the University of
     47  *	California, Berkeley and its contributors.
     48  * 4. Neither the name of the University nor the names of its contributors
     49  *    may be used to endorse or promote products derived from this software
     50  *    without specific prior written permission.
     51  *
     52  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     53  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     54  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     55  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     56  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     57  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     58  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     59  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     60  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     61  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     62  * SUCH DAMAGE.
     63  *
     64  *	@(#)in_pcb.c	8.2 (Berkeley) 1/4/94
     65  */
     66 
     67 #include "opt_ipsec.h"
     68 
     69 #include <sys/param.h>
     70 #include <sys/systm.h>
     71 #include <sys/malloc.h>
     72 #include <sys/mbuf.h>
     73 #include <sys/protosw.h>
     74 #include <sys/socket.h>
     75 #include <sys/socketvar.h>
     76 #include <sys/ioctl.h>
     77 #include <sys/errno.h>
     78 #include <sys/time.h>
     79 #include <sys/proc.h>
     80 
     81 #include <net/if.h>
     82 #include <net/route.h>
     83 
     84 #include <netinet/in.h>
     85 #include <netinet/in_var.h>
     86 #include <netinet/in_systm.h>
     87 #include <netinet/ip.h>
     88 #include <netinet/in_pcb.h>
     89 #include <netinet6/ip6.h>
     90 #include <netinet6/ip6_var.h>
     91 #include <netinet6/in6_pcb.h>
     92 #include <netinet6/nd6.h>
     93 
     94 #include "loop.h"
     95 extern struct ifnet loif[NLOOP];
     96 #include "faith.h"
     97 
     98 #ifdef IPSEC
     99 #include <netinet6/ipsec.h>
    100 #include <netkey/key.h>
    101 #include <netkey/key_debug.h>
    102 #endif /* IPSEC */
    103 
    104 struct in6_addr zeroin6_addr;
    105 
    106 int
    107 in6_pcballoc(so, head)
    108 	struct socket *so;
    109 	struct in6pcb *head;
    110 {
    111 	struct in6pcb *in6p;
    112 
    113 	MALLOC(in6p, struct in6pcb *, sizeof(*in6p), M_PCB, M_NOWAIT);
    114 	if (in6p == NULL)
    115 		return(ENOBUFS);
    116 	bzero((caddr_t)in6p, sizeof(*in6p));
    117 	in6p->in6p_head = head;
    118 	in6p->in6p_socket = so;
    119 	in6p->in6p_hops = -1;	/* use kernel default */
    120 	in6p->in6p_icmp6filt = NULL;
    121 #if 0
    122 	insque(in6p, head);
    123 #else
    124 	in6p->in6p_next = head->in6p_next;
    125 	head->in6p_next = in6p;
    126 	in6p->in6p_prev = head;
    127 	in6p->in6p_next->in6p_prev = in6p;
    128 #endif
    129 #ifndef INET6_BINDV6ONLY
    130 	if (ip6_bindv6only)
    131 		in6p->in6p_flags |= IN6P_BINDV6ONLY;
    132 #else
    133 	in6p->in6p_flags |= IN6P_BINDV6ONLY;	/*just for safety*/
    134 #endif
    135 	so->so_pcb = (caddr_t)in6p;
    136 	return(0);
    137 }
    138 
    139 int
    140 in6_pcbbind(in6p, nam)
    141 	register struct in6pcb *in6p;
    142 	struct mbuf *nam;
    143 {
    144 	struct socket *so = in6p->in6p_socket;
    145 	struct in6pcb *head = in6p->in6p_head;
    146 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL;
    147 	struct proc *p = curproc;		/* XXX */
    148 	u_short	lport = 0;
    149 	int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
    150 	int error;
    151 
    152 	if (in6p->in6p_lport || !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
    153 		return(EINVAL);
    154 	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0 &&
    155 	   ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 ||
    156 	    (so->so_options & SO_ACCEPTCONN) == 0))
    157 		wild = IN6PLOOKUP_WILDCARD;
    158 	if (nam) {
    159 		sin6 = mtod(nam, struct sockaddr_in6 *);
    160 		if (nam->m_len != sizeof(*sin6))
    161 			return(EINVAL);
    162 		/*
    163 		 * We should check the family, but old programs
    164 		 * incorrectly fail to intialize it.
    165 		 */
    166 		if (sin6->sin6_family != AF_INET6)
    167 			return(EAFNOSUPPORT);
    168 
    169 		/*
    170 		 * If the scope of the destination is link-local, embed the
    171 		 * interface index in the address.
    172 		 */
    173 		if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) {
    174 			/* XXX boundary check is assumed to be already done. */
    175 			/* XXX sin6_scope_id is weaker than advanced-api. */
    176 			struct in6_pktinfo *pi;
    177 			if (in6p->in6p_outputopts &&
    178 			    (pi = in6p->in6p_outputopts->ip6po_pktinfo) &&
    179 			    pi->ipi6_ifindex) {
    180 				sin6->sin6_addr.s6_addr16[1]
    181 					= htons(pi->ipi6_ifindex);
    182 			} else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)
    183 				&& in6p->in6p_moptions
    184 				&& in6p->in6p_moptions->im6o_multicast_ifp) {
    185 				sin6->sin6_addr.s6_addr16[1] =
    186 					htons(in6p->in6p_moptions->im6o_multicast_ifp->if_index);
    187 			} else if (sin6->sin6_scope_id) {
    188 				/* boundary check */
    189 				if (sin6->sin6_scope_id < 0
    190 				 || if_index < sin6->sin6_scope_id) {
    191 					return ENXIO;  /* XXX EINVAL? */
    192 				}
    193 				sin6->sin6_addr.s6_addr16[1]
    194 					= htons(sin6->sin6_scope_id & 0xffff);/*XXX*/
    195 				/* this must be cleared for ifa_ifwithaddr() */
    196 				sin6->sin6_scope_id = 0;
    197 			}
    198 		}
    199 
    200 		lport = sin6->sin6_port;
    201 		if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
    202 			/*
    203 			 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
    204 			 * allow compepte duplication of binding if
    205 			 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
    206 			 * and a multicast address is bound on both
    207 			 * new and duplicated sockets.
    208 			 */
    209 			if (so->so_options & SO_REUSEADDR)
    210 				reuseport = SO_REUSEADDR|SO_REUSEPORT;
    211 		} else if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
    212 			struct sockaddr_in sin;
    213 
    214 			bzero(&sin, sizeof(sin));
    215 			sin.sin_len = sizeof(sin);
    216 			sin.sin_family = AF_INET;
    217 			bcopy(&sin6->sin6_addr.s6_addr32[3], &sin.sin_addr,
    218 				sizeof(sin.sin_addr));
    219 			if (ifa_ifwithaddr((struct sockaddr *)&sin) == 0)
    220 				return EADDRNOTAVAIL;
    221 		} else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
    222 			struct ifaddr *ia = NULL;
    223 
    224 			sin6->sin6_port = 0;		/* yech... */
    225 #if defined(NFAITH) && NFAITH > 0
    226 			if ((in6p->in6p_flags & IN6P_FAITH) == 0
    227 			 && (ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == 0)
    228 #else
    229 			if ((ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == 0)
    230 #endif
    231 				return(EADDRNOTAVAIL);
    232 
    233 			/*
    234 			 * XXX: bind to an anycast address might accidentally
    235 			 * cause sending a packet with anycast source address.
    236 			 */
    237 			if (ia &&
    238 			    ((struct in6_ifaddr *)ia)->ia6_flags &
    239 			    (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
    240 			     IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
    241 				return(EADDRNOTAVAIL);
    242 			}
    243 		}
    244 		if (lport) {
    245 #ifndef IPNOPRIVPORTS
    246 			/* GROSS */
    247 			if (ntohs(lport) < IPV6PORT_RESERVED &&
    248 			    (p == 0 ||
    249 			     (error = suser(p->p_ucred, &p->p_acflag))))
    250 				return(EACCES);
    251 #endif
    252 
    253 			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
    254 				/* should check this but we can't ... */
    255 #if 0
    256 				struct inpcb *t;
    257 
    258 				t = in_pcblookup_bind(&tcbtable,
    259 					(struct in_addr *)&sin6->sin6_addr.s6_addr32[3],
    260 					lport);
    261 				if (t && (reuseport & t->inp_socket->so_options) == 0)
    262 					return EADDRINUSE;
    263 #endif
    264 			} else {
    265 				struct in6pcb *t;
    266 
    267 				t = in6_pcblookup(head, &zeroin6_addr, 0,
    268 						  &sin6->sin6_addr, lport, wild);
    269 				if (t && (reuseport & t->in6p_socket->so_options) == 0)
    270 					return(EADDRINUSE);
    271 			}
    272 		}
    273 		in6p->in6p_laddr = sin6->sin6_addr;
    274 	}
    275 
    276 	if (lport == 0) {
    277 		int e;
    278 		if ((e = in6_pcbsetport(&in6p->in6p_laddr, in6p)) != 0)
    279 			return(e);
    280 	}
    281 	else
    282 		in6p->in6p_lport = lport;
    283 
    284 	in6p->in6p_flowinfo = sin6 ? sin6->sin6_flowinfo : 0;	/*XXX*/
    285 	return(0);
    286 }
    287 
    288 /*
    289  * Find an empty port and set it to the specified PCB.
    290  */
    291 int
    292 in6_pcbsetport(laddr, in6p)
    293 	struct in6_addr *laddr;
    294 	struct in6pcb *in6p;
    295 {
    296 	struct socket *so = in6p->in6p_socket;
    297 	struct in6pcb *head = in6p->in6p_head;
    298 	u_short last_port, lport = 0;
    299 	int wild = 0;
    300 	void *t;
    301 	u_short min, max;
    302 #ifndef IPNOPRIVPORTS
    303 	struct proc *p = curproc;	/*XXX*/
    304 #endif
    305 
    306 	/* XXX: this is redundant when called from in6_pcbbind */
    307 	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0 &&
    308 	   ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 ||
    309 	    (so->so_options & SO_ACCEPTCONN) == 0))
    310 		wild = IN6PLOOKUP_WILDCARD;
    311 
    312 	if (in6p->in6p_flags & IN6P_LOWPORT) {
    313 #ifndef IPNOPRIVPORTS
    314                 if (p == 0 || (suser(p->p_ucred, &p->p_acflag) != 0))
    315 			return (EACCES);
    316 #endif
    317 		min = IPV6PORT_RESERVEDMIN;
    318 		max = IPV6PORT_RESERVEDMAX;
    319 	} else {
    320 		min = IPV6PORT_ANONMIN;
    321 		max = IPV6PORT_ANONMAX;
    322 	}
    323 
    324 	/* value out of range */
    325 	if (head->in6p_lport < min)
    326 		head->in6p_lport = min;
    327 	else if (head->in6p_lport > max)
    328 		head->in6p_lport = min;
    329 	last_port = head->in6p_lport;
    330 	goto startover;	/*to randomize*/
    331 	for (;;) {
    332 		lport = htons(head->in6p_lport);
    333 		if (IN6_IS_ADDR_V4MAPPED(laddr)) {
    334 #if 0
    335 			t = in_pcblookup_bind(&tcbtable,
    336 					      (struct in_addr *)&in6p->in6p_laddr.s6_addr32[3],
    337 					      lport);
    338 #else
    339 			t = NULL;
    340 #endif
    341 		} else {
    342 			t = in6_pcblookup(head, &zeroin6_addr, 0, laddr,
    343 					  lport, wild);
    344 		}
    345 		if (t == 0)
    346 			break;
    347 	  startover:
    348 		if (head->in6p_lport >= max)
    349 			head->in6p_lport = min;
    350 		else
    351 			head->in6p_lport++;
    352 		if (head->in6p_lport == last_port)
    353 			return (EADDRINUSE);
    354 	}
    355 
    356 	in6p->in6p_lport = lport;
    357 	return(0);		/* success */
    358 }
    359 
    360 /*
    361  * Connect from a socket to a specified address.
    362  * Both address and port must be specified in argument sin6.
    363  * If don't have a local address for this socket yet,
    364  * then pick one.
    365  */
    366 int
    367 in6_pcbconnect(in6p, nam)
    368 	struct in6pcb *in6p;
    369 	struct mbuf *nam;
    370 {
    371 	struct in6_addr *in6a = NULL;
    372 	struct sockaddr_in6 *sin6 = mtod(nam, struct sockaddr_in6 *);
    373 	struct in6_pktinfo *pi;
    374 	struct ifnet *ifp = NULL;	/* outgoing interface */
    375 	int error = 0;
    376 	struct in6_addr mapped;
    377 
    378 	(void)&in6a;				/* XXX fool gcc */
    379 
    380 	if (nam->m_len != sizeof(*sin6))
    381 		return(EINVAL);
    382 	if (sin6->sin6_family != AF_INET6)
    383 		return(EAFNOSUPPORT);
    384 	if (sin6->sin6_port == 0)
    385 		return(EADDRNOTAVAIL);
    386 
    387 	/* sanity check for mapped address case */
    388 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
    389 		if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
    390 			in6p->in6p_laddr.s6_addr16[5] = htons(0xffff);
    391 		if (!IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr))
    392 			return EINVAL;
    393 	} else {
    394 		if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr))
    395 			return EINVAL;
    396 	}
    397 
    398 	/*
    399 	 * If the scope of the destination is link-local, embed the interface
    400 	 * index in the address.
    401 	 */
    402 	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) {
    403 		/* XXX boundary check is assumed to be already done. */
    404 		/* XXX sin6_scope_id is weaker than advanced-api. */
    405 		if (in6p->in6p_outputopts &&
    406 		    (pi = in6p->in6p_outputopts->ip6po_pktinfo) &&
    407 		    pi->ipi6_ifindex) {
    408 			sin6->sin6_addr.s6_addr16[1] = htons(pi->ipi6_ifindex);
    409 			ifp = ifindex2ifnet[pi->ipi6_ifindex];
    410 		}
    411 		else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) &&
    412 			 in6p->in6p_moptions &&
    413 			 in6p->in6p_moptions->im6o_multicast_ifp) {
    414 			sin6->sin6_addr.s6_addr16[1] =
    415 				htons(in6p->in6p_moptions->im6o_multicast_ifp->if_index);
    416 			ifp = ifindex2ifnet[in6p->in6p_moptions->im6o_multicast_ifp->if_index];
    417 		} else if (sin6->sin6_scope_id) {
    418 			/* boundary check */
    419 			if (sin6->sin6_scope_id < 0
    420 			 || if_index < sin6->sin6_scope_id) {
    421 				return ENXIO;  /* XXX EINVAL? */
    422 			}
    423 			sin6->sin6_addr.s6_addr16[1]
    424 				= htons(sin6->sin6_scope_id & 0xffff);/*XXX*/
    425 			ifp = ifindex2ifnet[sin6->sin6_scope_id];
    426 		}
    427 	}
    428 
    429 	/* Source address selection. */
    430 	if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)
    431 	 && in6p->in6p_laddr.s6_addr32[3] == 0) {
    432 		struct sockaddr_in sin, *sinp;
    433 
    434 		bzero(&sin, sizeof(sin));
    435 		sin.sin_len = sizeof(sin);
    436 		sin.sin_family = AF_INET;
    437 		bcopy(&sin6->sin6_addr.s6_addr32[3], &sin.sin_addr,
    438 			sizeof(sin.sin_addr));
    439 		sinp = in_selectsrc(&sin, (struct route *)&in6p->in6p_route,
    440 			in6p->in6p_socket->so_options, NULL, &error);
    441 		if (sinp == 0) {
    442 			if (error == 0)
    443 				error = EADDRNOTAVAIL;
    444 			return(error);
    445 		}
    446 		bzero(&mapped, sizeof(mapped));
    447 		mapped.s6_addr16[5] = htons(0xffff);
    448 		bcopy(&sinp->sin_addr, &mapped.s6_addr32[3], sizeof(sinp->sin_addr));
    449 		in6a = &mapped;
    450 	} else {
    451 		/*
    452 		 * XXX: in6_selectsrc might replace the bound local address
    453 		 * with the address specified by setsockopt(IPV6_PKTINFO).
    454 		 * Is it the intended behavior?
    455 		 */
    456 		in6a = in6_selectsrc(sin6, in6p->in6p_outputopts,
    457 				     in6p->in6p_moptions,
    458 				     &in6p->in6p_route,
    459 				     &in6p->in6p_laddr, &error);
    460 		if (in6a == 0) {
    461 			if (error == 0)
    462 				error = EADDRNOTAVAIL;
    463 			return(error);
    464 		}
    465 	}
    466 	if (in6p->in6p_route.ro_rt)
    467 		ifp = in6p->in6p_route.ro_rt->rt_ifp;
    468 
    469 	in6p->in6p_ip6.ip6_hlim = (u_int8_t)in6_selecthlim(in6p, ifp);
    470 
    471 	if (in6_pcblookup(in6p->in6p_head,
    472 			 &sin6->sin6_addr,
    473 			 sin6->sin6_port,
    474 			 IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) ?
    475 			  in6a : &in6p->in6p_laddr,
    476 			 in6p->in6p_lport,
    477 			 0))
    478 		return(EADDRINUSE);
    479 	if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)
    480 	 || (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)
    481 	  && in6p->in6p_laddr.s6_addr32[3] == 0)) {
    482 		if (in6p->in6p_lport == 0)
    483 			(void)in6_pcbbind(in6p, (struct mbuf *)0);
    484 		in6p->in6p_laddr = *in6a;
    485 	}
    486 	in6p->in6p_faddr = sin6->sin6_addr;
    487 	in6p->in6p_fport = sin6->sin6_port;
    488 	/*
    489 	 * xxx kazu flowlabel is necessary for connect?
    490 	 * but if this line is missing, the garbage value remains.
    491 	 */
    492 	in6p->in6p_flowinfo = sin6->sin6_flowinfo;
    493 	return(0);
    494 }
    495 
    496 /*
    497  * Return an IPv6 address, which is the most appropriate for given
    498  * destination and user specified options.
    499  * If necessary, this function lookups the routing table and return
    500  * an entry to the caller for later use.
    501  */
    502 struct in6_addr *
    503 in6_selectsrc(dstsock, opts, mopts, ro, laddr, errorp)
    504 	struct sockaddr_in6 *dstsock;
    505 	struct ip6_pktopts *opts;
    506 	struct ip6_moptions *mopts;
    507 	struct route_in6 *ro;
    508 	struct in6_addr *laddr;
    509 	int *errorp;
    510 {
    511 	struct in6_addr *dst;
    512 	struct in6_ifaddr *ia6 = 0;
    513 	struct in6_pktinfo *pi = NULL;
    514 
    515 	dst = &dstsock->sin6_addr;
    516 	*errorp = 0;
    517 
    518 	/*
    519 	 * If the source address is explicitly specified by the caller,
    520 	 * use it.
    521 	 */
    522 	if (opts && (pi = opts->ip6po_pktinfo) &&
    523 	    !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr))
    524 		return(&pi->ipi6_addr);
    525 
    526 	/*
    527 	 * If the source address is not specified but the socket(if any)
    528 	 * is already bound, use the bound address.
    529 	 */
    530 	if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr))
    531 		return(laddr);
    532 
    533 	/*
    534 	 * If the caller doesn't specify the source address but
    535 	 * the outgoing interface, use an address associated with
    536 	 * the interface.
    537 	 */
    538 	if (pi && pi->ipi6_ifindex) {
    539 		/* XXX boundary check is assumed to be already done. */
    540 		ia6 = in6_ifawithscope(ifindex2ifnet[pi->ipi6_ifindex],
    541 				       dst);
    542 		if (ia6 == 0) {
    543 			*errorp = EADDRNOTAVAIL;
    544 			return(0);
    545 		}
    546 		return(&satosin6(&ia6->ia_addr)->sin6_addr);
    547 	}
    548 
    549 	/*
    550 	 * If the destination address is a link-local unicast address or
    551 	 * a multicast address, and if the outgoing interface is specified
    552 	 * by the sin6_scope_id filed, use an address associated with the
    553 	 * interface.
    554 	 * XXX: We're now trying to define more specific semantics of
    555 	 *      sin6_scope_id field, so this part will be rewritten in
    556 	 *      the near future.
    557 	 */
    558 	if ((IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MULTICAST(dst)) &&
    559 	    dstsock->sin6_scope_id) {
    560 		/*
    561 		 * I'm not sure if boundary check for scope_id is done
    562 		 * somewhere...
    563 		 */
    564 		if (dstsock->sin6_scope_id < 0 ||
    565 		    if_index < dstsock->sin6_scope_id) {
    566 			*errorp = ENXIO; /* XXX: better error? */
    567 			return(0);
    568 		}
    569 		ia6 = in6_ifawithscope(ifindex2ifnet[dstsock->sin6_scope_id],
    570 				       dst);
    571 		if (ia6 == 0) {
    572 			*errorp = EADDRNOTAVAIL;
    573 			return(0);
    574 		}
    575 		return(&satosin6(&ia6->ia_addr)->sin6_addr);
    576 	}
    577 
    578 	/*
    579 	 * If the destination address is a multicast address and
    580 	 * the outgoing interface for the address is specified
    581 	 * by the caller, use an address associated with the interface.
    582 	 * There is a sanity check here; if the destination has node-local
    583 	 * scope, the outgoing interfacde should be a loopback address.
    584 	 * Even if the outgoing interface is not specified, we also
    585 	 * choose a loopback interface as the outgoing interface.
    586 	 */
    587 	if (IN6_IS_ADDR_MULTICAST(dst)) {
    588 		struct ifnet *ifp = mopts ? mopts->im6o_multicast_ifp : NULL;
    589 
    590 		if (ifp == NULL && IN6_IS_ADDR_MC_NODELOCAL(dst)) {
    591 			ifp = &loif[0];
    592 		}
    593 
    594 		if (ifp) {
    595 			ia6 = in6_ifawithscope(ifp, dst);
    596 			if (ia6 == 0) {
    597 				*errorp = EADDRNOTAVAIL;
    598 				return(0);
    599 			}
    600 			return(&satosin6(&ia6->ia_addr)->sin6_addr);
    601 		}
    602 	}
    603 
    604 	/*
    605 	 * If the next hop address for the packet is specified
    606 	 * by caller, use an address associated with the route
    607 	 * to the next hop.
    608 	 */
    609 	{
    610 		struct sockaddr_in6 *sin6_next;
    611 		struct rtentry *rt;
    612 
    613 		if (opts && opts->ip6po_nexthop) {
    614 			sin6_next = satosin6(opts->ip6po_nexthop);
    615 			rt = nd6_lookup(&sin6_next->sin6_addr, 1, NULL);
    616 			if (rt) {
    617 				ia6 = in6_ifawithscope(rt->rt_ifp, dst);
    618 				if (ia6 == 0)
    619 					ia6 = ifatoia6(rt->rt_ifa);
    620 			}
    621 			if (ia6 == 0) {
    622 				*errorp = EADDRNOTAVAIL;
    623 				return(0);
    624 			}
    625 			return(&satosin6(&ia6->ia_addr)->sin6_addr);
    626 		}
    627 	}
    628 
    629 	/*
    630 	 * If route is known or can be allocated now,
    631 	 * our src addr is taken from the i/f, else punt.
    632 	 */
    633 	if (ro) {
    634 		if (ro->ro_rt &&
    635 		    !IN6_ARE_ADDR_EQUAL(&satosin6(&ro->ro_dst)->sin6_addr, dst)) {
    636 			RTFREE(ro->ro_rt);
    637 			ro->ro_rt = (struct rtentry *)0;
    638 		}
    639 		if (ro->ro_rt == (struct rtentry *)0 ||
    640 		    ro->ro_rt->rt_ifp == (struct ifnet *)0) {
    641 			/* No route yet, so try to acquire one */
    642 			bzero(&ro->ro_dst, sizeof(struct sockaddr_in6));
    643 			ro->ro_dst.sin6_family = AF_INET6;
    644 			ro->ro_dst.sin6_len = sizeof(struct sockaddr_in6);
    645 			ro->ro_dst.sin6_addr = *dst;
    646 			if (IN6_IS_ADDR_MULTICAST(dst)) {
    647 				ro->ro_rt = rtalloc1(&((struct route *)ro)
    648 						     ->ro_dst, 0);
    649 			} else {
    650 				rtalloc((struct route *)ro);
    651 			}
    652 
    653 		}
    654 
    655 		/*
    656 		 * in_pcbconnect() checks out IFF_LOOPBACK to skip using
    657 		 * the address. But we don't know why it does so.
    658 		 * It is necessary to ensure the scope even for lo0
    659 		 * so doesn't check out IFF_LOOPBACK.
    660 		 */
    661 
    662 		if (ro->ro_rt) {
    663 			ia6 = in6_ifawithscope(ro->ro_rt->rt_ifa->ifa_ifp, dst);
    664 			if (ia6 == 0) /* xxx scope error ?*/
    665 				ia6 = ifatoia6(ro->ro_rt->rt_ifa);
    666 		}
    667 #if 0
    668 		/*
    669 		 * xxx The followings are necessary? (kazu)
    670 		 * I don't think so.
    671 		 * It's for SO_DONTROUTE option in IPv4.(jinmei)
    672 		 */
    673 		if (ia6 == 0) {
    674 			struct sockaddr_in6 sin6 = {sizeof(sin6), AF_INET6, 0};
    675 
    676 			sin6->sin6_addr = *dst;
    677 
    678 			ia6 = ifatoia6(ifa_ifwithdstaddr(sin6tosa(&sin6)));
    679 			if (ia6 == 0)
    680 				ia6 = ifatoia6(ifa_ifwithnet(sin6tosa(&sin6)));
    681 			if (ia6 == 0)
    682 				return(0);
    683 			return(&satosin6(&ia6->ia_addr)->sin6_addr);
    684 		}
    685 #endif /* 0 */
    686 		if (ia6 == 0) {
    687 			*errorp = EHOSTUNREACH;	/* no route */
    688 			return(0);
    689 		}
    690 		return(&satosin6(&ia6->ia_addr)->sin6_addr);
    691 	}
    692 
    693 	*errorp = EADDRNOTAVAIL;
    694 	return(0);
    695 }
    696 
    697 /*
    698  * Default hop limit selection. The precedence is as follows:
    699  * 1. Hoplimit valued specified via ioctl.
    700  * 2. (If the outgoing interface is detected) the current
    701  *     hop limit of the interface specified by router advertisement.
    702  * 3. The system default hoplimit.
    703 */
    704 int
    705 in6_selecthlim(in6p, ifp)
    706 	struct in6pcb *in6p;
    707 	struct ifnet *ifp;
    708 {
    709 	if (in6p && in6p->in6p_hops >= 0)
    710 		return(in6p->in6p_hops);
    711 	else if (ifp)
    712 		return(nd_ifinfo[ifp->if_index].chlim);
    713 	else
    714 		return(ip6_defhlim);
    715 }
    716 
    717 void
    718 in6_pcbdisconnect(in6p)
    719 	struct in6pcb *in6p;
    720 {
    721 	bzero((caddr_t)&in6p->in6p_faddr, sizeof(in6p->in6p_faddr));
    722 	in6p->in6p_fport = 0;
    723 	if (in6p->in6p_socket->so_state & SS_NOFDREF)
    724 		in6_pcbdetach(in6p);
    725 }
    726 
    727 void
    728 in6_pcbdetach(in6p)
    729 	struct in6pcb *in6p;
    730 {
    731 	struct socket *so = in6p->in6p_socket;
    732 
    733 #ifdef IPSEC
    734 	if (sotoin6pcb(so) != 0)
    735 		key_freeso(so);
    736 	ipsec6_delete_pcbpolicy(in6p);
    737 #endif /* IPSEC */
    738 	sotoin6pcb(so) = 0;
    739 	sofree(so);
    740 	if (in6p->in6p_options)
    741 		m_freem(in6p->in6p_options);
    742 	if (in6p->in6p_outputopts) {
    743 		if (in6p->in6p_outputopts->ip6po_rthdr &&
    744 		    in6p->in6p_outputopts->ip6po_route.ro_rt)
    745 			RTFREE(in6p->in6p_outputopts->ip6po_route.ro_rt);
    746 		if (in6p->in6p_outputopts->ip6po_m)
    747 			(void)m_free(in6p->in6p_outputopts->ip6po_m);
    748 		free(in6p->in6p_outputopts, M_IP6OPT);
    749 	}
    750 	if (in6p->in6p_route.ro_rt)
    751 		rtfree(in6p->in6p_route.ro_rt);
    752 	ip6_freemoptions(in6p->in6p_moptions);
    753 #if 0
    754 	remque(in6p);
    755 #else
    756 	in6p->in6p_next->in6p_prev = in6p->in6p_prev;
    757 	in6p->in6p_prev->in6p_next = in6p->in6p_next;
    758 	in6p->in6p_prev = NULL;
    759 #endif
    760 	FREE(in6p, M_PCB);
    761 }
    762 
    763 void
    764 in6_setsockaddr(in6p, nam)
    765 	struct in6pcb *in6p;
    766 	struct mbuf *nam;
    767 {
    768 	struct sockaddr_in6 *sin6;
    769 
    770 	nam->m_len = sizeof(*sin6);
    771 	sin6 = mtod(nam, struct sockaddr_in6 *);
    772 	bzero((caddr_t)sin6, sizeof(*sin6));
    773 	sin6->sin6_family = AF_INET6;
    774 	sin6->sin6_len = sizeof(struct sockaddr_in6);
    775 	sin6->sin6_port = in6p->in6p_lport;
    776 	sin6->sin6_addr = in6p->in6p_laddr;
    777 	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
    778 		sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]);
    779 	else
    780 		sin6->sin6_scope_id = 0;	/*XXX*/
    781 	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
    782 		sin6->sin6_addr.s6_addr16[1] = 0;
    783 }
    784 
    785 void
    786 in6_setpeeraddr(in6p, nam)
    787 	struct in6pcb *in6p;
    788 	struct mbuf *nam;
    789 {
    790 	struct sockaddr_in6 *sin6;
    791 
    792 	nam->m_len = sizeof(*sin6);
    793 	sin6 = mtod(nam, struct sockaddr_in6 *);
    794 	bzero((caddr_t)sin6, sizeof(*sin6));
    795 	sin6->sin6_family = AF_INET6;
    796 	sin6->sin6_len = sizeof(struct sockaddr_in6);
    797 	sin6->sin6_port = in6p->in6p_fport;
    798 	sin6->sin6_addr = in6p->in6p_faddr;
    799 	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
    800 		sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]);
    801 	else
    802 		sin6->sin6_scope_id = 0;	/*XXX*/
    803 	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
    804 		sin6->sin6_addr.s6_addr16[1] = 0;
    805 }
    806 
    807 /*
    808  * Pass some notification to all connections of a protocol
    809  * associated with address dst.  The local address and/or port numbers
    810  * may be specified to limit the search.  The "usual action" will be
    811  * taken, depending on the ctlinput cmd.  The caller must filter any
    812  * cmds that are uninteresting (e.g., no error in the map).
    813  * Call the protocol specific routine (if any) to report
    814  * any errors for each matching socket.
    815  *
    816  * Must be called at splsoftnet.
    817  */
    818 int
    819 in6_pcbnotify(head, dst, fport_arg, laddr6, lport_arg, cmd, notify)
    820 	struct in6pcb *head;
    821 	struct sockaddr *dst;
    822 	u_int fport_arg, lport_arg;
    823 	struct in6_addr *laddr6;
    824 	int cmd;
    825 	void (*notify) __P((struct in6pcb *, int));
    826 {
    827 	struct in6pcb *in6p, *oin6p;
    828 	struct in6_addr faddr6;
    829 	u_short	fport = fport_arg, lport = lport_arg;
    830 	int errno;
    831 	int nmatch = 0;
    832 
    833 	if ((unsigned)cmd > PRC_NCMDS || dst->sa_family != AF_INET6)
    834 		return 0;
    835 	faddr6 = ((struct sockaddr_in6 *)dst)->sin6_addr;
    836 	if (IN6_IS_ADDR_UNSPECIFIED(&faddr6))
    837 		return 0;
    838 
    839 	/*
    840 	 * Redirects go to all references to the destination,
    841 	 * and use in_rtchange to invalidate the route cache.
    842 	 * Dead host indications: notify all references to the destination.
    843 	 * Otherwise, if we have knowledge of the local port and address,
    844 	 * deliver only to that socket.
    845 	 */
    846 	if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
    847 		fport = 0;
    848 		lport = 0;
    849 		bzero((caddr_t)laddr6, sizeof(*laddr6));
    850 		if (cmd != PRC_HOSTDEAD)
    851 			notify = in6_rtchange;
    852 	}
    853 	if (notify == NULL)
    854 		return 0;
    855 	errno = inet6ctlerrmap[cmd];
    856 	for (in6p = head->in6p_next; in6p != head;) {
    857 		if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,&faddr6) ||
    858 		   in6p->in6p_socket == 0 ||
    859 		   (lport && in6p->in6p_lport != lport) ||
    860 		   (!IN6_IS_ADDR_UNSPECIFIED(laddr6) &&
    861 		    !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6)) ||
    862 		   (fport && in6p->in6p_fport != fport)) {
    863 			in6p = in6p->in6p_next;
    864 			continue;
    865 		}
    866 		oin6p = in6p;
    867 		in6p = in6p->in6p_next;
    868 		(*notify)(oin6p, errno);
    869 		nmatch++;
    870 	}
    871 	return nmatch;
    872 }
    873 
    874 /*
    875  * Check for alternatives when higher level complains
    876  * about service problems.  For now, invalidate cached
    877  * routing information.  If the route was created dynamically
    878  * (by a redirect), time to try a default gateway again.
    879  */
    880 void
    881 in6_losing(in6p)
    882 	struct in6pcb *in6p;
    883 {
    884 	struct rtentry *rt;
    885 	struct rt_addrinfo info;
    886 
    887 	if ((rt = in6p->in6p_route.ro_rt) != NULL) {
    888 		in6p->in6p_route.ro_rt = 0;
    889 		bzero((caddr_t)&info, sizeof(info));
    890 		info.rti_info[RTAX_DST] =
    891 			(struct sockaddr *)&in6p->in6p_route.ro_dst;
    892 		info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
    893 		info.rti_info[RTAX_NETMASK] = rt_mask(rt);
    894 		rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
    895 		if (rt->rt_flags & RTF_DYNAMIC)
    896 			(void)rtrequest(RTM_DELETE, rt_key(rt),
    897 					rt->rt_gateway, rt_mask(rt), rt->rt_flags,
    898 					(struct rtentry **)0);
    899 		else
    900 		/*
    901 		 * A new route can be allocated
    902 		 * the next time output is attempted.
    903 		 */
    904 			rtfree(rt);
    905 	}
    906 }
    907 
    908 /*
    909  * After a routing change, flush old routing
    910  * and allocate a (hopefully) better one.
    911  */
    912 void
    913 in6_rtchange(in6p, errno)
    914 	struct in6pcb *in6p;
    915 	int errno;
    916 {
    917 	if (in6p->in6p_route.ro_rt) {
    918 		rtfree(in6p->in6p_route.ro_rt);
    919 		in6p->in6p_route.ro_rt = 0;
    920 		/*
    921 		 * A new route can be allocated the next time
    922 		 * output is attempted.
    923 		 */
    924 	}
    925 }
    926 
    927 struct in6pcb *
    928 in6_pcblookup(head, faddr6, fport_arg, laddr6, lport_arg, flags)
    929 	struct in6pcb *head;
    930 	struct in6_addr *faddr6, *laddr6;
    931 	u_int fport_arg, lport_arg;
    932 	int flags;
    933 {
    934 	struct in6pcb *in6p, *match = 0;
    935 	int matchwild = 3, wildcard;
    936 	u_short	fport = fport_arg, lport = lport_arg;
    937 
    938 	for (in6p = head->in6p_next; in6p != head; in6p = in6p->in6p_next) {
    939 		if (in6p->in6p_lport != lport)
    940 			continue;
    941 		wildcard = 0;
    942 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
    943 			if (IN6_IS_ADDR_UNSPECIFIED(laddr6))
    944 				wildcard++;
    945 			else if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6))
    946 				continue;
    947 		}
    948 #ifndef TCP6
    949 		else if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)
    950 			&& in6p->in6p_laddr.s6_addr32[3] == 0) {
    951 			if (!IN6_IS_ADDR_V4MAPPED(laddr6))
    952 				continue;
    953 			if (laddr6->s6_addr32[3] == 0)
    954 				;
    955 			else
    956 				wildcard++;
    957 		}
    958 #endif
    959 		else {
    960 			if (IN6_IS_ADDR_V4MAPPED(laddr6)) {
    961 #if !defined(TCP6) && !defined(INET6_BINDV6ONLY)
    962 				if (in6p->in6p_flags & IN6P_BINDV6ONLY)
    963 					continue;
    964 				else
    965 					wildcard++;
    966 #else
    967 				continue;
    968 #endif
    969 			} else if (!IN6_IS_ADDR_UNSPECIFIED(laddr6))
    970 				wildcard++;
    971 		}
    972 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
    973 			if (IN6_IS_ADDR_UNSPECIFIED(faddr6))
    974 				wildcard++;
    975 			else if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, faddr6)
    976 			      || in6p->in6p_fport != fport)
    977 				continue;
    978 		}
    979 #ifndef TCP6
    980 		else if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)
    981 			&& in6p->in6p_faddr.s6_addr32[3] == 0) {
    982 			if (!IN6_IS_ADDR_V4MAPPED(faddr6))
    983 				continue;
    984 			if (faddr6->s6_addr32[3] == 0)
    985 				;
    986 			else
    987 				wildcard++;
    988 		}
    989 #endif
    990 		else {
    991 			if (IN6_IS_ADDR_V4MAPPED(faddr6)) {
    992 #if !defined(TCP6) && !defined(INET6_BINDV6ONLY)
    993 				if (in6p->in6p_flags & IN6P_BINDV6ONLY)
    994 					continue;
    995 				else
    996 					wildcard++;
    997 #else
    998 				continue;
    999 #endif
   1000 			} else if (!IN6_IS_ADDR_UNSPECIFIED(faddr6))
   1001 				wildcard++;
   1002 		}
   1003 
   1004 		if (wildcard && (flags & IN6PLOOKUP_WILDCARD) == 0)
   1005 			continue;
   1006 		if (wildcard < matchwild) {
   1007 			match = in6p;
   1008 			matchwild = wildcard;
   1009 			if (matchwild == 0)
   1010 				break;
   1011 		}
   1012 	}
   1013 	return(match);
   1014 }
   1015 
   1016 #ifndef TCP6
   1017 struct rtentry *
   1018 in6_pcbrtentry(in6p)
   1019 	struct in6pcb *in6p;
   1020 {
   1021 	struct route_in6 *ro;
   1022 
   1023 	ro = &in6p->in6p_route;
   1024 
   1025 	if (ro->ro_rt == NULL) {
   1026 		/*
   1027 		 * No route yet, so try to acquire one.
   1028 		 */
   1029 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
   1030 			bzero(&ro->ro_dst, sizeof(ro->ro_dst));
   1031 			ro->ro_dst.sin6_family = AF_INET6;
   1032 			ro->ro_dst.sin6_len = sizeof(struct sockaddr_in6);
   1033 			satosin6(&ro->ro_dst)->sin6_addr = in6p->in6p_faddr;
   1034 			rtalloc((struct route *)ro);
   1035 		}
   1036 	}
   1037 	return (ro->ro_rt);
   1038 }
   1039 
   1040 struct in6pcb *
   1041 in6_pcblookup_connect(head, faddr6, fport_arg, laddr6, lport_arg, faith)
   1042 	struct in6pcb *head;
   1043 	struct in6_addr *faddr6, *laddr6;
   1044 	u_int fport_arg, lport_arg;
   1045 	int faith;
   1046 {
   1047 	struct in6pcb *in6p;
   1048 	u_short	fport = fport_arg, lport = lport_arg;
   1049 
   1050 	for (in6p = head->in6p_next; in6p != head; in6p = in6p->in6p_next) {
   1051 #if defined(NFAITH) && NFAITH > 0
   1052 		if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0)
   1053 			continue;
   1054 #endif
   1055 		/* find exact match on both source and dest */
   1056 		if (in6p->in6p_fport != fport)
   1057 			continue;
   1058 		if (in6p->in6p_lport != lport)
   1059 			continue;
   1060 		if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr))
   1061 			continue;
   1062 		if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, faddr6))
   1063 			continue;
   1064 		if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
   1065 			continue;
   1066 		if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6))
   1067 			continue;
   1068 		return in6p;
   1069 	}
   1070 	return NULL;
   1071 }
   1072 
   1073 struct in6pcb *
   1074 in6_pcblookup_bind(head, laddr6, lport_arg, faith)
   1075 	struct in6pcb *head;
   1076 	struct in6_addr *laddr6;
   1077 	u_int lport_arg;
   1078 	int faith;
   1079 {
   1080 	struct in6pcb *in6p, *match;
   1081 	u_short	lport = lport_arg;
   1082 
   1083 	match = NULL;
   1084 	for (in6p = head->in6p_next; in6p != head; in6p = in6p->in6p_next) {
   1085 		/*
   1086 	 	 * find destination match.  exact match is preferred
   1087 		 * against wildcard match.
   1088 		 */
   1089 #if defined(NFAITH) && NFAITH > 0
   1090 		if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0)
   1091 			continue;
   1092 #endif
   1093 		if (in6p->in6p_fport != 0)
   1094 			continue;
   1095 		if (in6p->in6p_lport != lport)
   1096 			continue;
   1097 		if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
   1098 			if (IN6_IS_ADDR_V4MAPPED(laddr6)) {
   1099 #ifndef INET6_BINDV6ONLY
   1100 				if (in6p->in6p_flags & IN6P_BINDV6ONLY)
   1101 					continue;
   1102 				else
   1103 					match = in6p;
   1104 #else
   1105 				continue;
   1106 #endif
   1107 			} else
   1108 				match = in6p;
   1109 		}
   1110 		else if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr) &&
   1111 			 in6p->in6p_laddr.s6_addr32[3] == 0) {
   1112 			if (IN6_IS_ADDR_V4MAPPED(laddr6)
   1113 			 && laddr6->s6_addr32[3] != 0)
   1114 				match = in6p;
   1115 		}
   1116 		else if (IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6))
   1117 			return in6p;
   1118 	}
   1119 	return match;
   1120 }
   1121 #endif
   1122