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