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