Home | History | Annotate | Line # | Download | only in netinet6
in6_pcb.c revision 1.16
      1 /*	$NetBSD: in6_pcb.c,v 1.16 2000/02/02 23:28:10 thorpej 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 != NULL) {
    238 				struct in6_ifaddr *ia6 = (void *)ia;
    239 
    240 				if (ia6->ia6_flags &
    241 				    (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
    242 				     IN6_IFF_DETACHED|IN6_IFF_DEPRECATED))
    243 					return (EADDRNOTAVAIL);
    244 			}
    245 		}
    246 		if (lport) {
    247 #ifndef IPNOPRIVPORTS
    248 			/* GROSS */
    249 			if (ntohs(lport) < IPV6PORT_RESERVED &&
    250 			    (p == 0 ||
    251 			     (error = suser(p->p_ucred, &p->p_acflag))))
    252 				return(EACCES);
    253 #endif
    254 
    255 			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
    256 				/* should check this but we can't ... */
    257 #if 0
    258 				struct inpcb *t;
    259 
    260 				t = in_pcblookup_bind(&tcbtable,
    261 					(struct in_addr *)&sin6->sin6_addr.s6_addr32[3],
    262 					lport);
    263 				if (t && (reuseport & t->inp_socket->so_options) == 0)
    264 					return EADDRINUSE;
    265 #endif
    266 			} else {
    267 				struct in6pcb *t;
    268 
    269 				t = in6_pcblookup(head, &zeroin6_addr, 0,
    270 						  &sin6->sin6_addr, lport, wild);
    271 				if (t && (reuseport & t->in6p_socket->so_options) == 0)
    272 					return(EADDRINUSE);
    273 			}
    274 		}
    275 		in6p->in6p_laddr = sin6->sin6_addr;
    276 	}
    277 
    278 	if (lport == 0) {
    279 		int e;
    280 		if ((e = in6_pcbsetport(&in6p->in6p_laddr, in6p)) != 0)
    281 			return(e);
    282 	}
    283 	else
    284 		in6p->in6p_lport = lport;
    285 
    286 	in6p->in6p_flowinfo = sin6 ? sin6->sin6_flowinfo : 0;	/*XXX*/
    287 	return(0);
    288 }
    289 
    290 /*
    291  * Find an empty port and set it to the specified PCB.
    292  */
    293 int
    294 in6_pcbsetport(laddr, in6p)
    295 	struct in6_addr *laddr;
    296 	struct in6pcb *in6p;
    297 {
    298 	struct socket *so = in6p->in6p_socket;
    299 	struct in6pcb *head = in6p->in6p_head;
    300 	u_short last_port, lport = 0;
    301 	int wild = 0;
    302 	void *t;
    303 	u_short min, max;
    304 #ifndef IPNOPRIVPORTS
    305 	struct proc *p = curproc;	/*XXX*/
    306 #endif
    307 
    308 	/* XXX: this is redundant when called from in6_pcbbind */
    309 	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0 &&
    310 	   ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 ||
    311 	    (so->so_options & SO_ACCEPTCONN) == 0))
    312 		wild = IN6PLOOKUP_WILDCARD;
    313 
    314 	if (in6p->in6p_flags & IN6P_LOWPORT) {
    315 #ifndef IPNOPRIVPORTS
    316                 if (p == 0 || (suser(p->p_ucred, &p->p_acflag) != 0))
    317 			return (EACCES);
    318 #endif
    319 		min = IPV6PORT_RESERVEDMIN;
    320 		max = IPV6PORT_RESERVEDMAX;
    321 	} else {
    322 		min = IPV6PORT_ANONMIN;
    323 		max = IPV6PORT_ANONMAX;
    324 	}
    325 
    326 	/* value out of range */
    327 	if (head->in6p_lport < min)
    328 		head->in6p_lport = min;
    329 	else if (head->in6p_lport > max)
    330 		head->in6p_lport = min;
    331 	last_port = head->in6p_lport;
    332 	goto startover;	/*to randomize*/
    333 	for (;;) {
    334 		lport = htons(head->in6p_lport);
    335 		if (IN6_IS_ADDR_V4MAPPED(laddr)) {
    336 #if 0
    337 			t = in_pcblookup_bind(&tcbtable,
    338 					      (struct in_addr *)&in6p->in6p_laddr.s6_addr32[3],
    339 					      lport);
    340 #else
    341 			t = NULL;
    342 #endif
    343 		} else {
    344 			t = in6_pcblookup(head, &zeroin6_addr, 0, laddr,
    345 					  lport, wild);
    346 		}
    347 		if (t == 0)
    348 			break;
    349 	  startover:
    350 		if (head->in6p_lport >= max)
    351 			head->in6p_lport = min;
    352 		else
    353 			head->in6p_lport++;
    354 		if (head->in6p_lport == last_port)
    355 			return (EADDRINUSE);
    356 	}
    357 
    358 	in6p->in6p_lport = lport;
    359 	return(0);		/* success */
    360 }
    361 
    362 /*
    363  * Connect from a socket to a specified address.
    364  * Both address and port must be specified in argument sin6.
    365  * If don't have a local address for this socket yet,
    366  * then pick one.
    367  */
    368 int
    369 in6_pcbconnect(in6p, nam)
    370 	struct in6pcb *in6p;
    371 	struct mbuf *nam;
    372 {
    373 	struct in6_addr *in6a = NULL;
    374 	struct sockaddr_in6 *sin6 = mtod(nam, struct sockaddr_in6 *);
    375 	struct in6_pktinfo *pi;
    376 	struct ifnet *ifp = NULL;	/* outgoing interface */
    377 	int error = 0;
    378 	struct in6_addr mapped;
    379 
    380 	(void)&in6a;				/* XXX fool gcc */
    381 
    382 	if (nam->m_len != sizeof(*sin6))
    383 		return(EINVAL);
    384 	if (sin6->sin6_family != AF_INET6)
    385 		return(EAFNOSUPPORT);
    386 	if (sin6->sin6_port == 0)
    387 		return(EADDRNOTAVAIL);
    388 
    389 	/* sanity check for mapped address case */
    390 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
    391 		if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
    392 			in6p->in6p_laddr.s6_addr16[5] = htons(0xffff);
    393 		if (!IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr))
    394 			return EINVAL;
    395 	} else {
    396 		if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr))
    397 			return EINVAL;
    398 	}
    399 
    400 	/*
    401 	 * If the scope of the destination is link-local, embed the interface
    402 	 * index in the address.
    403 	 */
    404 	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) {
    405 		/* XXX boundary check is assumed to be already done. */
    406 		/* XXX sin6_scope_id is weaker than advanced-api. */
    407 		if (in6p->in6p_outputopts &&
    408 		    (pi = in6p->in6p_outputopts->ip6po_pktinfo) &&
    409 		    pi->ipi6_ifindex) {
    410 			sin6->sin6_addr.s6_addr16[1] = htons(pi->ipi6_ifindex);
    411 			ifp = ifindex2ifnet[pi->ipi6_ifindex];
    412 		}
    413 		else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) &&
    414 			 in6p->in6p_moptions &&
    415 			 in6p->in6p_moptions->im6o_multicast_ifp) {
    416 			sin6->sin6_addr.s6_addr16[1] =
    417 				htons(in6p->in6p_moptions->im6o_multicast_ifp->if_index);
    418 			ifp = ifindex2ifnet[in6p->in6p_moptions->im6o_multicast_ifp->if_index];
    419 		} else if (sin6->sin6_scope_id) {
    420 			/* boundary check */
    421 			if (sin6->sin6_scope_id < 0
    422 			 || if_index < sin6->sin6_scope_id) {
    423 				return ENXIO;  /* XXX EINVAL? */
    424 			}
    425 			sin6->sin6_addr.s6_addr16[1]
    426 				= htons(sin6->sin6_scope_id & 0xffff);/*XXX*/
    427 			ifp = ifindex2ifnet[sin6->sin6_scope_id];
    428 		}
    429 	}
    430 
    431 	/* Source address selection. */
    432 	if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)
    433 	 && in6p->in6p_laddr.s6_addr32[3] == 0) {
    434 		struct sockaddr_in sin, *sinp;
    435 
    436 		bzero(&sin, sizeof(sin));
    437 		sin.sin_len = sizeof(sin);
    438 		sin.sin_family = AF_INET;
    439 		bcopy(&sin6->sin6_addr.s6_addr32[3], &sin.sin_addr,
    440 			sizeof(sin.sin_addr));
    441 		sinp = in_selectsrc(&sin, (struct route *)&in6p->in6p_route,
    442 			in6p->in6p_socket->so_options, NULL, &error);
    443 		if (sinp == 0) {
    444 			if (error == 0)
    445 				error = EADDRNOTAVAIL;
    446 			return(error);
    447 		}
    448 		bzero(&mapped, sizeof(mapped));
    449 		mapped.s6_addr16[5] = htons(0xffff);
    450 		bcopy(&sinp->sin_addr, &mapped.s6_addr32[3], sizeof(sinp->sin_addr));
    451 		in6a = &mapped;
    452 	} else {
    453 		/*
    454 		 * XXX: in6_selectsrc might replace the bound local address
    455 		 * with the address specified by setsockopt(IPV6_PKTINFO).
    456 		 * Is it the intended behavior?
    457 		 */
    458 		in6a = in6_selectsrc(sin6, in6p->in6p_outputopts,
    459 				     in6p->in6p_moptions,
    460 				     &in6p->in6p_route,
    461 				     &in6p->in6p_laddr, &error);
    462 		if (in6a == 0) {
    463 			if (error == 0)
    464 				error = EADDRNOTAVAIL;
    465 			return(error);
    466 		}
    467 	}
    468 	if (in6p->in6p_route.ro_rt)
    469 		ifp = in6p->in6p_route.ro_rt->rt_ifp;
    470 
    471 	in6p->in6p_ip6.ip6_hlim = (u_int8_t)in6_selecthlim(in6p, ifp);
    472 
    473 	if (in6_pcblookup(in6p->in6p_head,
    474 			 &sin6->sin6_addr,
    475 			 sin6->sin6_port,
    476 			 IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) ?
    477 			  in6a : &in6p->in6p_laddr,
    478 			 in6p->in6p_lport,
    479 			 0))
    480 		return(EADDRINUSE);
    481 	if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)
    482 	 || (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)
    483 	  && in6p->in6p_laddr.s6_addr32[3] == 0)) {
    484 		if (in6p->in6p_lport == 0)
    485 			(void)in6_pcbbind(in6p, (struct mbuf *)0);
    486 		in6p->in6p_laddr = *in6a;
    487 	}
    488 	in6p->in6p_faddr = sin6->sin6_addr;
    489 	in6p->in6p_fport = sin6->sin6_port;
    490 	/*
    491 	 * xxx kazu flowlabel is necessary for connect?
    492 	 * but if this line is missing, the garbage value remains.
    493 	 */
    494 	in6p->in6p_flowinfo = sin6->sin6_flowinfo;
    495 	return(0);
    496 }
    497 
    498 /*
    499  * Return an IPv6 address, which is the most appropriate for given
    500  * destination and user specified options.
    501  * If necessary, this function lookups the routing table and return
    502  * an entry to the caller for later use.
    503  */
    504 struct in6_addr *
    505 in6_selectsrc(dstsock, opts, mopts, ro, laddr, errorp)
    506 	struct sockaddr_in6 *dstsock;
    507 	struct ip6_pktopts *opts;
    508 	struct ip6_moptions *mopts;
    509 	struct route_in6 *ro;
    510 	struct in6_addr *laddr;
    511 	int *errorp;
    512 {
    513 	struct in6_addr *dst;
    514 	struct in6_ifaddr *ia6 = 0;
    515 	struct in6_pktinfo *pi = NULL;
    516 
    517 	dst = &dstsock->sin6_addr;
    518 	*errorp = 0;
    519 
    520 	/*
    521 	 * If the source address is explicitly specified by the caller,
    522 	 * use it.
    523 	 */
    524 	if (opts && (pi = opts->ip6po_pktinfo) &&
    525 	    !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr))
    526 		return(&pi->ipi6_addr);
    527 
    528 	/*
    529 	 * If the source address is not specified but the socket(if any)
    530 	 * is already bound, use the bound address.
    531 	 */
    532 	if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr))
    533 		return(laddr);
    534 
    535 	/*
    536 	 * If the caller doesn't specify the source address but
    537 	 * the outgoing interface, use an address associated with
    538 	 * the interface.
    539 	 */
    540 	if (pi && pi->ipi6_ifindex) {
    541 		/* XXX boundary check is assumed to be already done. */
    542 		ia6 = in6_ifawithscope(ifindex2ifnet[pi->ipi6_ifindex],
    543 				       dst);
    544 		if (ia6 == 0) {
    545 			*errorp = EADDRNOTAVAIL;
    546 			return(0);
    547 		}
    548 		return(&satosin6(&ia6->ia_addr)->sin6_addr);
    549 	}
    550 
    551 	/*
    552 	 * If the destination address is a link-local unicast address or
    553 	 * a multicast address, and if the outgoing interface is specified
    554 	 * by the sin6_scope_id filed, use an address associated with the
    555 	 * interface.
    556 	 * XXX: We're now trying to define more specific semantics of
    557 	 *      sin6_scope_id field, so this part will be rewritten in
    558 	 *      the near future.
    559 	 */
    560 	if ((IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MULTICAST(dst)) &&
    561 	    dstsock->sin6_scope_id) {
    562 		/*
    563 		 * I'm not sure if boundary check for scope_id is done
    564 		 * somewhere...
    565 		 */
    566 		if (dstsock->sin6_scope_id < 0 ||
    567 		    if_index < dstsock->sin6_scope_id) {
    568 			*errorp = ENXIO; /* XXX: better error? */
    569 			return(0);
    570 		}
    571 		ia6 = in6_ifawithscope(ifindex2ifnet[dstsock->sin6_scope_id],
    572 				       dst);
    573 		if (ia6 == 0) {
    574 			*errorp = EADDRNOTAVAIL;
    575 			return(0);
    576 		}
    577 		return(&satosin6(&ia6->ia_addr)->sin6_addr);
    578 	}
    579 
    580 	/*
    581 	 * If the destination address is a multicast address and
    582 	 * the outgoing interface for the address is specified
    583 	 * by the caller, use an address associated with the interface.
    584 	 * There is a sanity check here; if the destination has node-local
    585 	 * scope, the outgoing interfacde should be a loopback address.
    586 	 * Even if the outgoing interface is not specified, we also
    587 	 * choose a loopback interface as the outgoing interface.
    588 	 */
    589 	if (IN6_IS_ADDR_MULTICAST(dst)) {
    590 		struct ifnet *ifp = mopts ? mopts->im6o_multicast_ifp : NULL;
    591 
    592 		if (ifp == NULL && IN6_IS_ADDR_MC_NODELOCAL(dst)) {
    593 			ifp = &loif[0];
    594 		}
    595 
    596 		if (ifp) {
    597 			ia6 = in6_ifawithscope(ifp, dst);
    598 			if (ia6 == 0) {
    599 				*errorp = EADDRNOTAVAIL;
    600 				return(0);
    601 			}
    602 			return(&satosin6(&ia6->ia_addr)->sin6_addr);
    603 		}
    604 	}
    605 
    606 	/*
    607 	 * If the next hop address for the packet is specified
    608 	 * by caller, use an address associated with the route
    609 	 * to the next hop.
    610 	 */
    611 	{
    612 		struct sockaddr_in6 *sin6_next;
    613 		struct rtentry *rt;
    614 
    615 		if (opts && opts->ip6po_nexthop) {
    616 			sin6_next = satosin6(opts->ip6po_nexthop);
    617 			rt = nd6_lookup(&sin6_next->sin6_addr, 1, NULL);
    618 			if (rt) {
    619 				ia6 = in6_ifawithscope(rt->rt_ifp, dst);
    620 				if (ia6 == 0)
    621 					ia6 = ifatoia6(rt->rt_ifa);
    622 			}
    623 			if (ia6 == 0) {
    624 				*errorp = EADDRNOTAVAIL;
    625 				return(0);
    626 			}
    627 			return(&satosin6(&ia6->ia_addr)->sin6_addr);
    628 		}
    629 	}
    630 
    631 	/*
    632 	 * If route is known or can be allocated now,
    633 	 * our src addr is taken from the i/f, else punt.
    634 	 */
    635 	if (ro) {
    636 		if (ro->ro_rt &&
    637 		    !IN6_ARE_ADDR_EQUAL(&satosin6(&ro->ro_dst)->sin6_addr, dst)) {
    638 			RTFREE(ro->ro_rt);
    639 			ro->ro_rt = (struct rtentry *)0;
    640 		}
    641 		if (ro->ro_rt == (struct rtentry *)0 ||
    642 		    ro->ro_rt->rt_ifp == (struct ifnet *)0) {
    643 			/* No route yet, so try to acquire one */
    644 			bzero(&ro->ro_dst, sizeof(struct sockaddr_in6));
    645 			ro->ro_dst.sin6_family = AF_INET6;
    646 			ro->ro_dst.sin6_len = sizeof(struct sockaddr_in6);
    647 			ro->ro_dst.sin6_addr = *dst;
    648 			if (IN6_IS_ADDR_MULTICAST(dst)) {
    649 				ro->ro_rt = rtalloc1(&((struct route *)ro)
    650 						     ->ro_dst, 0);
    651 			} else {
    652 				rtalloc((struct route *)ro);
    653 			}
    654 
    655 		}
    656 
    657 		/*
    658 		 * in_pcbconnect() checks out IFF_LOOPBACK to skip using
    659 		 * the address. But we don't know why it does so.
    660 		 * It is necessary to ensure the scope even for lo0
    661 		 * so doesn't check out IFF_LOOPBACK.
    662 		 */
    663 
    664 		if (ro->ro_rt) {
    665 			ia6 = in6_ifawithscope(ro->ro_rt->rt_ifa->ifa_ifp, dst);
    666 			if (ia6 == 0) /* xxx scope error ?*/
    667 				ia6 = ifatoia6(ro->ro_rt->rt_ifa);
    668 		}
    669 #if 0
    670 		/*
    671 		 * xxx The followings are necessary? (kazu)
    672 		 * I don't think so.
    673 		 * It's for SO_DONTROUTE option in IPv4.(jinmei)
    674 		 */
    675 		if (ia6 == 0) {
    676 			struct sockaddr_in6 sin6 = {sizeof(sin6), AF_INET6, 0};
    677 
    678 			sin6->sin6_addr = *dst;
    679 
    680 			ia6 = ifatoia6(ifa_ifwithdstaddr(sin6tosa(&sin6)));
    681 			if (ia6 == 0)
    682 				ia6 = ifatoia6(ifa_ifwithnet(sin6tosa(&sin6)));
    683 			if (ia6 == 0)
    684 				return(0);
    685 			return(&satosin6(&ia6->ia_addr)->sin6_addr);
    686 		}
    687 #endif /* 0 */
    688 		if (ia6 == 0) {
    689 			*errorp = EHOSTUNREACH;	/* no route */
    690 			return(0);
    691 		}
    692 		return(&satosin6(&ia6->ia_addr)->sin6_addr);
    693 	}
    694 
    695 	*errorp = EADDRNOTAVAIL;
    696 	return(0);
    697 }
    698 
    699 /*
    700  * Default hop limit selection. The precedence is as follows:
    701  * 1. Hoplimit valued specified via ioctl.
    702  * 2. (If the outgoing interface is detected) the current
    703  *     hop limit of the interface specified by router advertisement.
    704  * 3. The system default hoplimit.
    705 */
    706 int
    707 in6_selecthlim(in6p, ifp)
    708 	struct in6pcb *in6p;
    709 	struct ifnet *ifp;
    710 {
    711 	if (in6p && in6p->in6p_hops >= 0)
    712 		return(in6p->in6p_hops);
    713 	else if (ifp)
    714 		return(nd_ifinfo[ifp->if_index].chlim);
    715 	else
    716 		return(ip6_defhlim);
    717 }
    718 
    719 void
    720 in6_pcbdisconnect(in6p)
    721 	struct in6pcb *in6p;
    722 {
    723 	bzero((caddr_t)&in6p->in6p_faddr, sizeof(in6p->in6p_faddr));
    724 	in6p->in6p_fport = 0;
    725 	if (in6p->in6p_socket->so_state & SS_NOFDREF)
    726 		in6_pcbdetach(in6p);
    727 }
    728 
    729 void
    730 in6_pcbdetach(in6p)
    731 	struct in6pcb *in6p;
    732 {
    733 	struct socket *so = in6p->in6p_socket;
    734 
    735 #ifdef IPSEC
    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 void
    875 in6_pcbpurgeif(head, ifp)
    876 	struct in6pcb *head;
    877 	struct ifnet *ifp;
    878 {
    879 	struct in6pcb *in6p, *nin6p;
    880 
    881 	for (in6p = head->in6p_next; in6p != head; in6p = nin6p) {
    882 		nin6p = in6p->in6p_next;
    883 		if (in6p->in6p_route.ro_rt != NULL &&
    884 		    in6p->in6p_route.ro_rt->rt_ifp == ifp)
    885 			in6_rtchange(in6p, 0);
    886 	}
    887 }
    888 
    889 /*
    890  * Check for alternatives when higher level complains
    891  * about service problems.  For now, invalidate cached
    892  * routing information.  If the route was created dynamically
    893  * (by a redirect), time to try a default gateway again.
    894  */
    895 void
    896 in6_losing(in6p)
    897 	struct in6pcb *in6p;
    898 {
    899 	struct rtentry *rt;
    900 	struct rt_addrinfo info;
    901 
    902 	if ((rt = in6p->in6p_route.ro_rt) != NULL) {
    903 		in6p->in6p_route.ro_rt = 0;
    904 		bzero((caddr_t)&info, sizeof(info));
    905 		info.rti_info[RTAX_DST] =
    906 			(struct sockaddr *)&in6p->in6p_route.ro_dst;
    907 		info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
    908 		info.rti_info[RTAX_NETMASK] = rt_mask(rt);
    909 		rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
    910 		if (rt->rt_flags & RTF_DYNAMIC)
    911 			(void)rtrequest(RTM_DELETE, rt_key(rt),
    912 					rt->rt_gateway, rt_mask(rt), rt->rt_flags,
    913 					(struct rtentry **)0);
    914 		else
    915 		/*
    916 		 * A new route can be allocated
    917 		 * the next time output is attempted.
    918 		 */
    919 			rtfree(rt);
    920 	}
    921 }
    922 
    923 /*
    924  * After a routing change, flush old routing
    925  * and allocate a (hopefully) better one.
    926  */
    927 void
    928 in6_rtchange(in6p, errno)
    929 	struct in6pcb *in6p;
    930 	int errno;
    931 {
    932 	if (in6p->in6p_route.ro_rt) {
    933 		rtfree(in6p->in6p_route.ro_rt);
    934 		in6p->in6p_route.ro_rt = 0;
    935 		/*
    936 		 * A new route can be allocated the next time
    937 		 * output is attempted.
    938 		 */
    939 	}
    940 }
    941 
    942 struct in6pcb *
    943 in6_pcblookup(head, faddr6, fport_arg, laddr6, lport_arg, flags)
    944 	struct in6pcb *head;
    945 	struct in6_addr *faddr6, *laddr6;
    946 	u_int fport_arg, lport_arg;
    947 	int flags;
    948 {
    949 	struct in6pcb *in6p, *match = 0;
    950 	int matchwild = 3, wildcard;
    951 	u_short	fport = fport_arg, lport = lport_arg;
    952 
    953 	for (in6p = head->in6p_next; in6p != head; in6p = in6p->in6p_next) {
    954 		if (in6p->in6p_lport != lport)
    955 			continue;
    956 		wildcard = 0;
    957 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
    958 			if (IN6_IS_ADDR_UNSPECIFIED(laddr6))
    959 				wildcard++;
    960 			else if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6))
    961 				continue;
    962 		}
    963 #ifndef TCP6
    964 		else if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)
    965 			&& in6p->in6p_laddr.s6_addr32[3] == 0) {
    966 			if (!IN6_IS_ADDR_V4MAPPED(laddr6))
    967 				continue;
    968 			if (laddr6->s6_addr32[3] == 0)
    969 				;
    970 			else
    971 				wildcard++;
    972 		}
    973 #endif
    974 		else {
    975 			if (IN6_IS_ADDR_V4MAPPED(laddr6)) {
    976 #if !defined(TCP6) && !defined(INET6_BINDV6ONLY)
    977 				if (in6p->in6p_flags & IN6P_BINDV6ONLY)
    978 					continue;
    979 				else
    980 					wildcard++;
    981 #else
    982 				continue;
    983 #endif
    984 			} else if (!IN6_IS_ADDR_UNSPECIFIED(laddr6))
    985 				wildcard++;
    986 		}
    987 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
    988 			if (IN6_IS_ADDR_UNSPECIFIED(faddr6))
    989 				wildcard++;
    990 			else if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, faddr6)
    991 			      || in6p->in6p_fport != fport)
    992 				continue;
    993 		}
    994 #ifndef TCP6
    995 		else if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)
    996 			&& in6p->in6p_faddr.s6_addr32[3] == 0) {
    997 			if (!IN6_IS_ADDR_V4MAPPED(faddr6))
    998 				continue;
    999 			if (faddr6->s6_addr32[3] == 0)
   1000 				;
   1001 			else
   1002 				wildcard++;
   1003 		}
   1004 #endif
   1005 		else {
   1006 			if (IN6_IS_ADDR_V4MAPPED(faddr6)) {
   1007 #if !defined(TCP6) && !defined(INET6_BINDV6ONLY)
   1008 				if (in6p->in6p_flags & IN6P_BINDV6ONLY)
   1009 					continue;
   1010 				else
   1011 					wildcard++;
   1012 #else
   1013 				continue;
   1014 #endif
   1015 			} else if (!IN6_IS_ADDR_UNSPECIFIED(faddr6))
   1016 				wildcard++;
   1017 		}
   1018 
   1019 		if (wildcard && (flags & IN6PLOOKUP_WILDCARD) == 0)
   1020 			continue;
   1021 		if (wildcard < matchwild) {
   1022 			match = in6p;
   1023 			matchwild = wildcard;
   1024 			if (matchwild == 0)
   1025 				break;
   1026 		}
   1027 	}
   1028 	return(match);
   1029 }
   1030 
   1031 #ifndef TCP6
   1032 struct rtentry *
   1033 in6_pcbrtentry(in6p)
   1034 	struct in6pcb *in6p;
   1035 {
   1036 	struct route_in6 *ro;
   1037 
   1038 	ro = &in6p->in6p_route;
   1039 
   1040 	if (ro->ro_rt == NULL) {
   1041 		/*
   1042 		 * No route yet, so try to acquire one.
   1043 		 */
   1044 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
   1045 			bzero(&ro->ro_dst, sizeof(ro->ro_dst));
   1046 			ro->ro_dst.sin6_family = AF_INET6;
   1047 			ro->ro_dst.sin6_len = sizeof(struct sockaddr_in6);
   1048 			satosin6(&ro->ro_dst)->sin6_addr = in6p->in6p_faddr;
   1049 			rtalloc((struct route *)ro);
   1050 		}
   1051 	}
   1052 	return (ro->ro_rt);
   1053 }
   1054 
   1055 struct in6pcb *
   1056 in6_pcblookup_connect(head, faddr6, fport_arg, laddr6, lport_arg, faith)
   1057 	struct in6pcb *head;
   1058 	struct in6_addr *faddr6, *laddr6;
   1059 	u_int fport_arg, lport_arg;
   1060 	int faith;
   1061 {
   1062 	struct in6pcb *in6p;
   1063 	u_short	fport = fport_arg, lport = lport_arg;
   1064 
   1065 	for (in6p = head->in6p_next; in6p != head; in6p = in6p->in6p_next) {
   1066 #if defined(NFAITH) && NFAITH > 0
   1067 		if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0)
   1068 			continue;
   1069 #endif
   1070 		/* find exact match on both source and dest */
   1071 		if (in6p->in6p_fport != fport)
   1072 			continue;
   1073 		if (in6p->in6p_lport != lport)
   1074 			continue;
   1075 		if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr))
   1076 			continue;
   1077 		if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, faddr6))
   1078 			continue;
   1079 		if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
   1080 			continue;
   1081 		if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6))
   1082 			continue;
   1083 		return in6p;
   1084 	}
   1085 	return NULL;
   1086 }
   1087 
   1088 struct in6pcb *
   1089 in6_pcblookup_bind(head, laddr6, lport_arg, faith)
   1090 	struct in6pcb *head;
   1091 	struct in6_addr *laddr6;
   1092 	u_int lport_arg;
   1093 	int faith;
   1094 {
   1095 	struct in6pcb *in6p, *match;
   1096 	u_short	lport = lport_arg;
   1097 
   1098 	match = NULL;
   1099 	for (in6p = head->in6p_next; in6p != head; in6p = in6p->in6p_next) {
   1100 		/*
   1101 	 	 * find destination match.  exact match is preferred
   1102 		 * against wildcard match.
   1103 		 */
   1104 #if defined(NFAITH) && NFAITH > 0
   1105 		if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0)
   1106 			continue;
   1107 #endif
   1108 		if (in6p->in6p_fport != 0)
   1109 			continue;
   1110 		if (in6p->in6p_lport != lport)
   1111 			continue;
   1112 		if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
   1113 			if (IN6_IS_ADDR_V4MAPPED(laddr6)) {
   1114 #ifndef INET6_BINDV6ONLY
   1115 				if (in6p->in6p_flags & IN6P_BINDV6ONLY)
   1116 					continue;
   1117 				else
   1118 					match = in6p;
   1119 #else
   1120 				continue;
   1121 #endif
   1122 			} else
   1123 				match = in6p;
   1124 		}
   1125 		else if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr) &&
   1126 			 in6p->in6p_laddr.s6_addr32[3] == 0) {
   1127 			if (IN6_IS_ADDR_V4MAPPED(laddr6)
   1128 			 && laddr6->s6_addr32[3] != 0)
   1129 				match = in6p;
   1130 		}
   1131 		else if (IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6))
   1132 			return in6p;
   1133 	}
   1134 	return match;
   1135 }
   1136 #endif
   1137