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