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