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