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