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