Home | History | Annotate | Line # | Download | only in netinet6
in6_pcb.c revision 1.40
      1 /*	$NetBSD: in6_pcb.c,v 1.40 2001/08/06 10:25:01 itojun Exp $	*/
      2 /*	$KAME: in6_pcb.c,v 1.84 2001/02/08 18:02:08 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 #endif /* IPSEC */
    103 
    104 struct in6_addr zeroin6_addr;
    105 
    106 int ip6_anonportmin = IPV6PORT_ANONMIN;
    107 int ip6_anonportmax = IPV6PORT_ANONMAX;
    108 int ip6_lowportmin  = IPV6PORT_RESERVEDMIN;
    109 int ip6_lowportmax  = IPV6PORT_RESERVEDMAX;
    110 
    111 int
    112 in6_pcballoc(so, head)
    113 	struct socket *so;
    114 	struct in6pcb *head;
    115 {
    116 	struct in6pcb *in6p;
    117 #ifdef IPSEC
    118 	int error;
    119 #endif
    120 
    121 	MALLOC(in6p, struct in6pcb *, sizeof(*in6p), M_PCB, M_NOWAIT);
    122 	if (in6p == NULL)
    123 		return(ENOBUFS);
    124 	bzero((caddr_t)in6p, sizeof(*in6p));
    125 	in6p->in6p_head = head;
    126 	in6p->in6p_socket = so;
    127 	in6p->in6p_hops = -1;	/* use kernel default */
    128 	in6p->in6p_icmp6filt = NULL;
    129 #ifdef IPSEC
    130 	error = ipsec_init_policy(so, &in6p->in6p_sp);
    131 	if (error != 0) {
    132 		FREE(in6p, M_PCB);
    133 		return error;
    134 	}
    135 #endif /*IPSEC*/
    136 	in6p->in6p_next = head->in6p_next;
    137 	head->in6p_next = in6p;
    138 	in6p->in6p_prev = head;
    139 	in6p->in6p_next->in6p_prev = in6p;
    140 #ifndef INET6_BINDV6ONLY
    141 	if (ip6_bindv6only)
    142 		in6p->in6p_flags |= IN6P_BINDV6ONLY;
    143 #else
    144 	in6p->in6p_flags |= IN6P_BINDV6ONLY;	/*just for safety*/
    145 #endif
    146 	so->so_pcb = (caddr_t)in6p;
    147 	return(0);
    148 }
    149 
    150 int
    151 in6_pcbbind(in6p, nam, p)
    152 	struct in6pcb *in6p;
    153 	struct mbuf *nam;
    154 	struct proc *p;
    155 {
    156 	struct socket *so = in6p->in6p_socket;
    157 	struct in6pcb *head = in6p->in6p_head;
    158 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL;
    159 	u_int16_t lport = 0;
    160 	int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
    161 
    162 	if (in6p->in6p_lport || !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
    163 		return(EINVAL);
    164 	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0 &&
    165 	   ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 ||
    166 	    (so->so_options & SO_ACCEPTCONN) == 0))
    167 		wild = IN6PLOOKUP_WILDCARD;
    168 	if (nam) {
    169 		sin6 = mtod(nam, struct sockaddr_in6 *);
    170 		if (nam->m_len != sizeof(*sin6))
    171 			return(EINVAL);
    172 		/*
    173 		 * We should check the family, but old programs
    174 		 * incorrectly fail to intialize it.
    175 		 */
    176 		if (sin6->sin6_family != AF_INET6)
    177 			return(EAFNOSUPPORT);
    178 
    179 		/*
    180 		 * since we do not check port number duplicate with IPv4 space,
    181 		 * we reject it at this moment.  If we leave it, we make normal
    182 		 * user to hijack port number from other users.
    183 		 */
    184 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
    185 			return(EADDRNOTAVAIL);
    186 
    187 		/* KAME hack: embed scopeid */
    188 		if (in6_embedscope(&sin6->sin6_addr, sin6, in6p, NULL) != 0)
    189 			return EINVAL;
    190 		/* this must be cleared for ifa_ifwithaddr() */
    191 		sin6->sin6_scope_id = 0;
    192 
    193 		lport = sin6->sin6_port;
    194 		if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
    195 			/*
    196 			 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
    197 			 * allow compepte duplication of binding if
    198 			 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
    199 			 * and a multicast address is bound on both
    200 			 * new and duplicated sockets.
    201 			 */
    202 			if (so->so_options & SO_REUSEADDR)
    203 				reuseport = SO_REUSEADDR|SO_REUSEPORT;
    204 		} else if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
    205 			struct sockaddr_in sin;
    206 
    207 			bzero(&sin, sizeof(sin));
    208 			sin.sin_len = sizeof(sin);
    209 			sin.sin_family = AF_INET;
    210 			bcopy(&sin6->sin6_addr.s6_addr32[3], &sin.sin_addr,
    211 				sizeof(sin.sin_addr));
    212 			if (ifa_ifwithaddr((struct sockaddr *)&sin) == 0)
    213 				return EADDRNOTAVAIL;
    214 		} else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
    215 			struct ifaddr *ia = NULL;
    216 
    217 			sin6->sin6_port = 0;		/* yech... */
    218 			if ((in6p->in6p_flags & IN6P_FAITH) == 0 &&
    219 			    (ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == 0)
    220 				return(EADDRNOTAVAIL);
    221 
    222 			/*
    223 			 * XXX: bind to an anycast address might accidentally
    224 			 * cause sending a packet with anycast source address.
    225 			 */
    226 			if (ia &&
    227 			    ((struct in6_ifaddr *)ia)->ia6_flags &
    228 			    (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
    229 			     IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
    230 				return(EADDRNOTAVAIL);
    231 			}
    232 		}
    233 		if (lport) {
    234 #ifndef IPNOPRIVPORTS
    235 			int priv;
    236 
    237 			/*
    238 			 * NOTE: all operating systems use suser() for
    239 			 * privilege check!  do not rewrite it into SS_PRIV.
    240 			 */
    241 			priv = (p && !suser(p->p_ucred, &p->p_acflag)) ? 1 : 0;
    242 			/* GROSS */
    243 			if (ntohs(lport) < IPV6PORT_RESERVED && !priv)
    244 				return(EACCES);
    245 #endif
    246 
    247 			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
    248 				/* should check this but we can't ... */
    249 #if 0
    250 				struct inpcb *t;
    251 
    252 				t = in_pcblookup_bind(&tcbtable,
    253 					(struct in_addr *)&sin6->sin6_addr.s6_addr32[3],
    254 					lport);
    255 				if (t && (reuseport & t->inp_socket->so_options) == 0)
    256 					return EADDRINUSE;
    257 #endif
    258 			} else {
    259 				struct in6pcb *t;
    260 
    261 				t = in6_pcblookup(head, &zeroin6_addr, 0,
    262 						  &sin6->sin6_addr, lport, wild);
    263 				if (t && (reuseport & t->in6p_socket->so_options) == 0)
    264 					return(EADDRINUSE);
    265 			}
    266 		}
    267 		in6p->in6p_laddr = sin6->sin6_addr;
    268 	}
    269 
    270 	if (lport == 0) {
    271 		int e;
    272 		if ((e = in6_pcbsetport(&in6p->in6p_laddr, in6p)) != 0)
    273 			return(e);
    274 	}
    275 	else
    276 		in6p->in6p_lport = lport;
    277 
    278 	in6p->in6p_flowinfo = sin6 ? sin6->sin6_flowinfo : 0;	/*XXX*/
    279 	return(0);
    280 }
    281 
    282 /*
    283  * Connect from a socket to a specified address.
    284  * Both address and port must be specified in argument sin6.
    285  * If don't have a local address for this socket yet,
    286  * then pick one.
    287  */
    288 int
    289 in6_pcbconnect(in6p, nam)
    290 	struct in6pcb *in6p;
    291 	struct mbuf *nam;
    292 {
    293 	struct in6_addr *in6a = NULL;
    294 	struct sockaddr_in6 *sin6 = mtod(nam, struct sockaddr_in6 *);
    295 	struct ifnet *ifp = NULL;	/* outgoing interface */
    296 	int error = 0;
    297 #ifdef INET
    298 	struct in6_addr mapped;
    299 #endif
    300 	struct sockaddr_in6 tmp;
    301 
    302 	(void)&in6a;				/* XXX fool gcc */
    303 
    304 	if (nam->m_len != sizeof(*sin6))
    305 		return(EINVAL);
    306 	if (sin6->sin6_family != AF_INET6)
    307 		return(EAFNOSUPPORT);
    308 	if (sin6->sin6_port == 0)
    309 		return(EADDRNOTAVAIL);
    310 
    311 	/* sanity check for mapped address case */
    312 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
    313 		if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
    314 			in6p->in6p_laddr.s6_addr16[5] = htons(0xffff);
    315 		if (!IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr))
    316 			return EINVAL;
    317 	} else {
    318 		if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr))
    319 			return EINVAL;
    320 	}
    321 
    322 	/* protect *sin6 from overwrites */
    323 	tmp = *sin6;
    324 	sin6 = &tmp;
    325 
    326 	/* KAME hack: embed scopeid */
    327 	if (in6_embedscope(&sin6->sin6_addr, sin6, in6p, &ifp) != 0)
    328 		return EINVAL;
    329 
    330 	/* Source address selection. */
    331 	if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)
    332 	 && in6p->in6p_laddr.s6_addr32[3] == 0) {
    333 #ifdef INET
    334 		struct sockaddr_in sin, *sinp;
    335 
    336 		bzero(&sin, sizeof(sin));
    337 		sin.sin_len = sizeof(sin);
    338 		sin.sin_family = AF_INET;
    339 		bcopy(&sin6->sin6_addr.s6_addr32[3], &sin.sin_addr,
    340 			sizeof(sin.sin_addr));
    341 		sinp = in_selectsrc(&sin, (struct route *)&in6p->in6p_route,
    342 			in6p->in6p_socket->so_options, NULL, &error);
    343 		if (sinp == 0) {
    344 			if (error == 0)
    345 				error = EADDRNOTAVAIL;
    346 			return(error);
    347 		}
    348 		bzero(&mapped, sizeof(mapped));
    349 		mapped.s6_addr16[5] = htons(0xffff);
    350 		bcopy(&sinp->sin_addr, &mapped.s6_addr32[3], sizeof(sinp->sin_addr));
    351 		in6a = &mapped;
    352 #else
    353 		return EADDRNOTAVAIL;
    354 #endif
    355 	} else {
    356 		/*
    357 		 * XXX: in6_selectsrc might replace the bound local address
    358 		 * with the address specified by setsockopt(IPV6_PKTINFO).
    359 		 * Is it the intended behavior?
    360 		 */
    361 		in6a = in6_selectsrc(sin6, in6p->in6p_outputopts,
    362 				     in6p->in6p_moptions,
    363 				     &in6p->in6p_route,
    364 				     &in6p->in6p_laddr, &error);
    365 		if (in6a == 0) {
    366 			if (error == 0)
    367 				error = EADDRNOTAVAIL;
    368 			return(error);
    369 		}
    370 	}
    371 	if (in6p->in6p_route.ro_rt)
    372 		ifp = in6p->in6p_route.ro_rt->rt_ifp;
    373 
    374 	in6p->in6p_ip6.ip6_hlim = (u_int8_t)in6_selecthlim(in6p, ifp);
    375 
    376 	if (in6_pcblookup(in6p->in6p_head,
    377 			 &sin6->sin6_addr,
    378 			 sin6->sin6_port,
    379 			 IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) ?
    380 			  in6a : &in6p->in6p_laddr,
    381 			 in6p->in6p_lport,
    382 			 0))
    383 		return(EADDRINUSE);
    384 	if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)
    385 	 || (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)
    386 	  && in6p->in6p_laddr.s6_addr32[3] == 0)) {
    387 		if (in6p->in6p_lport == 0) {
    388 			(void)in6_pcbbind(in6p, (struct mbuf *)0,
    389 			    (struct proc *)0);
    390 		}
    391 		in6p->in6p_laddr = *in6a;
    392 	}
    393 	in6p->in6p_faddr = sin6->sin6_addr;
    394 	in6p->in6p_fport = sin6->sin6_port;
    395 	/*
    396 	 * xxx kazu flowlabel is necessary for connect?
    397 	 * but if this line is missing, the garbage value remains.
    398 	 */
    399 	in6p->in6p_flowinfo = sin6->sin6_flowinfo;
    400 #ifdef IPSEC
    401 	if (in6p->in6p_socket->so_type == SOCK_STREAM)
    402 		ipsec_pcbconn(in6p->in6p_sp);
    403 #endif
    404 	return(0);
    405 }
    406 
    407 void
    408 in6_pcbdisconnect(in6p)
    409 	struct in6pcb *in6p;
    410 {
    411 	bzero((caddr_t)&in6p->in6p_faddr, sizeof(in6p->in6p_faddr));
    412 	in6p->in6p_fport = 0;
    413 	if (in6p->in6p_socket->so_state & SS_NOFDREF)
    414 		in6_pcbdetach(in6p);
    415 #ifdef IPSEC
    416 	ipsec_pcbdisconn(in6p->in6p_sp);
    417 #endif
    418 }
    419 
    420 void
    421 in6_pcbdetach(in6p)
    422 	struct in6pcb *in6p;
    423 {
    424 	struct socket *so = in6p->in6p_socket;
    425 
    426 #ifdef IPSEC
    427 	ipsec6_delete_pcbpolicy(in6p);
    428 #endif /* IPSEC */
    429 	sotoin6pcb(so) = 0;
    430 	sofree(so);
    431 	if (in6p->in6p_options)
    432 		m_freem(in6p->in6p_options);
    433 	if (in6p->in6p_outputopts) {
    434 		if (in6p->in6p_outputopts->ip6po_rthdr &&
    435 		    in6p->in6p_outputopts->ip6po_route.ro_rt)
    436 			RTFREE(in6p->in6p_outputopts->ip6po_route.ro_rt);
    437 		if (in6p->in6p_outputopts->ip6po_m)
    438 			(void)m_free(in6p->in6p_outputopts->ip6po_m);
    439 		free(in6p->in6p_outputopts, M_IP6OPT);
    440 	}
    441 	if (in6p->in6p_route.ro_rt)
    442 		rtfree(in6p->in6p_route.ro_rt);
    443 	ip6_freemoptions(in6p->in6p_moptions);
    444 	in6p->in6p_next->in6p_prev = in6p->in6p_prev;
    445 	in6p->in6p_prev->in6p_next = in6p->in6p_next;
    446 	in6p->in6p_prev = NULL;
    447 	FREE(in6p, M_PCB);
    448 }
    449 
    450 void
    451 in6_setsockaddr(in6p, nam)
    452 	struct in6pcb *in6p;
    453 	struct mbuf *nam;
    454 {
    455 	struct sockaddr_in6 *sin6;
    456 
    457 	nam->m_len = sizeof(*sin6);
    458 	sin6 = mtod(nam, struct sockaddr_in6 *);
    459 	bzero((caddr_t)sin6, sizeof(*sin6));
    460 	sin6->sin6_family = AF_INET6;
    461 	sin6->sin6_len = sizeof(struct sockaddr_in6);
    462 	sin6->sin6_port = in6p->in6p_lport;
    463 	/* KAME hack: recover scopeid */
    464 	(void)in6_recoverscope(sin6, &in6p->in6p_laddr, NULL);
    465 }
    466 
    467 void
    468 in6_setpeeraddr(in6p, nam)
    469 	struct in6pcb *in6p;
    470 	struct mbuf *nam;
    471 {
    472 	struct sockaddr_in6 *sin6;
    473 
    474 	nam->m_len = sizeof(*sin6);
    475 	sin6 = mtod(nam, struct sockaddr_in6 *);
    476 	bzero((caddr_t)sin6, sizeof(*sin6));
    477 	sin6->sin6_family = AF_INET6;
    478 	sin6->sin6_len = sizeof(struct sockaddr_in6);
    479 	sin6->sin6_port = in6p->in6p_fport;
    480 	/* KAME hack: recover scopeid */
    481 	(void)in6_recoverscope(sin6, &in6p->in6p_faddr, NULL);
    482 }
    483 
    484 /*
    485  * Pass some notification to all connections of a protocol
    486  * associated with address dst.  The local address and/or port numbers
    487  * may be specified to limit the search.  The "usual action" will be
    488  * taken, depending on the ctlinput cmd.  The caller must filter any
    489  * cmds that are uninteresting (e.g., no error in the map).
    490  * Call the protocol specific routine (if any) to report
    491  * any errors for each matching socket.
    492  *
    493  * Must be called at splsoftnet.
    494  *
    495  * Note: src (4th arg) carries the flowlabel value on the original IPv6
    496  * header, in sin6_flowinfo member.
    497  */
    498 int
    499 in6_pcbnotify(head, dst, fport_arg, src, lport_arg, cmd, cmdarg, notify)
    500 	struct in6pcb *head;
    501 	struct sockaddr *dst, *src;
    502 	u_int fport_arg, lport_arg;
    503 	int cmd;
    504 	void *cmdarg;
    505 	void (*notify) __P((struct in6pcb *, int));
    506 {
    507 	struct in6pcb *in6p, *nin6p;
    508 	struct sockaddr_in6 sa6_src, *sa6_dst;
    509 	u_int16_t fport = fport_arg, lport = lport_arg;
    510 	int errno;
    511 	int nmatch = 0;
    512 	u_int32_t flowinfo;
    513 
    514 	if ((unsigned)cmd > PRC_NCMDS || dst->sa_family != AF_INET6)
    515 		return 0;
    516 
    517 	sa6_dst = (struct sockaddr_in6 *)dst;
    518 	if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
    519 		return 0;
    520 
    521 	/*
    522 	 * note that src can be NULL when we get notify by local fragmentation.
    523 	 */
    524 	sa6_src = (src == NULL) ? sa6_any : *(struct sockaddr_in6 *)src;
    525 	flowinfo = sa6_src.sin6_flowinfo;
    526 
    527 	/*
    528 	 * Redirects go to all references to the destination,
    529 	 * and use in6_rtchange to invalidate the route cache.
    530 	 * Dead host indications: also use in6_rtchange to invalidate
    531 	 * the cache, and deliver the error to all the sockets.
    532 	 * Otherwise, if we have knowledge of the local port and address,
    533 	 * deliver only to that socket.
    534 	 */
    535 	if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
    536 		fport = 0;
    537 		lport = 0;
    538 		bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
    539 
    540 		if (cmd != PRC_HOSTDEAD)
    541 			notify = in6_rtchange;
    542 	}
    543 
    544 	errno = inet6ctlerrmap[cmd];
    545 	for (in6p = head->in6p_next; in6p != head; in6p = nin6p) {
    546 		nin6p = in6p->in6p_next;
    547 
    548 		/*
    549 		 * Under the following condition, notify of redirects
    550 		 * to the pcb, without making address matches against inpcb.
    551 		 * - redirect notification is arrived.
    552 		 * - the inpcb is unconnected.
    553 		 * - the inpcb is caching !RTF_HOST routing entry.
    554 		 * - the ICMPv6 notification is from the gateway cached in the
    555 		 *   inpcb.  i.e. ICMPv6 notification is from nexthop gateway
    556 		 *   the inpcb used very recently.
    557 		 *
    558 		 * This is to improve interaction between netbsd/openbsd
    559 		 * redirect handling code, and inpcb route cache code.
    560 		 * without the clause, !RTF_HOST routing entry (which carries
    561 		 * gateway used by inpcb right before the ICMPv6 redirect)
    562 		 * will be cached forever in unconnected inpcb.
    563 		 *
    564 		 * There still is a question regarding to what is TRT:
    565 		 * - On bsdi/freebsd, RTF_HOST (cloned) routing entry will be
    566 		 *   generated on packet output.  inpcb will always cache
    567 		 *   RTF_HOST routing entry so there's no need for the clause
    568 		 *   (ICMPv6 redirect will update RTF_HOST routing entry,
    569 		 *   and inpcb is caching it already).
    570 		 *   However, bsdi/freebsd are vulnerable to local DoS attacks
    571 		 *   due to the cloned routing entries.
    572 		 * - Specwise, "destination cache" is mentioned in RFC2461.
    573 		 *   Jinmei says that it implies bsdi/freebsd behavior, itojun
    574 		 *   is not really convinced.
    575 		 * - Having hiwat/lowat on # of cloned host route (redirect/
    576 		 *   pmtud) may be a good idea.  netbsd/openbsd has it.  see
    577 		 *   icmp6_mtudisc_update().
    578 		 */
    579 		if ((PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) &&
    580 		    IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
    581 		    in6p->in6p_route.ro_rt &&
    582 		    !(in6p->in6p_route.ro_rt->rt_flags & RTF_HOST)) {
    583 			struct sockaddr_in6 *dst6;
    584 
    585 			dst6 = (struct sockaddr_in6 *)&in6p->in6p_route.ro_dst;
    586 			if (IN6_ARE_ADDR_EQUAL(&dst6->sin6_addr,
    587 			    &sa6_dst->sin6_addr))
    588 				goto do_notify;
    589 		}
    590 
    591 		/*
    592 		 * Detect if we should notify the error. If no source and
    593 		 * destination ports are specifed, but non-zero flowinfo and
    594 		 * local address match, notify the error. This is the case
    595 		 * when the error is delivered with an encrypted buffer
    596 		 * by ESP. Otherwise, just compare addresses and ports
    597 		 * as usual.
    598 		 */
    599 		if (lport == 0 && fport == 0 && flowinfo &&
    600 		    in6p->in6p_socket != NULL &&
    601 		    flowinfo == (in6p->in6p_flowinfo & IPV6_FLOWLABEL_MASK) &&
    602 		    IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &sa6_src.sin6_addr))
    603 			goto do_notify;
    604 		else if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
    605 					     &sa6_dst->sin6_addr) ||
    606 		    in6p->in6p_socket == 0 ||
    607 		    (lport && in6p->in6p_lport != lport) ||
    608 		    (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
    609 		     !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
    610 					 &sa6_src.sin6_addr)) ||
    611 		    (fport && in6p->in6p_fport != fport))
    612 			continue;
    613 
    614 	  do_notify:
    615 		if (notify)
    616 			(*notify)(in6p, errno);
    617 		nmatch++;
    618 	}
    619 	return nmatch;
    620 }
    621 
    622 void
    623 in6_pcbpurgeif0(head, ifp)
    624 	struct in6pcb *head;
    625 	struct ifnet *ifp;
    626 {
    627 	struct in6pcb *in6p, *nin6p;
    628 	struct ip6_moptions *im6o;
    629 	struct in6_multi_mship *imm, *nimm;
    630 
    631 	for (in6p = head->in6p_next; in6p != head; in6p = nin6p) {
    632 		nin6p = in6p->in6p_next;
    633 		im6o = in6p->in6p_moptions;
    634 		if (im6o) {
    635 			/*
    636 			 * Unselect the outgoing interface if it is being
    637 			 * detached.
    638 			 */
    639 			if (im6o->im6o_multicast_ifp == ifp)
    640 				im6o->im6o_multicast_ifp = NULL;
    641 
    642 			/*
    643 			 * Drop multicast group membership if we joined
    644 			 * through the interface being detached.
    645 			 * XXX controversial - is it really legal for kernel
    646 			 * to force this?
    647 			 */
    648 			for (imm = im6o->im6o_memberships.lh_first;
    649 			     imm != NULL; imm = nimm) {
    650 				nimm = imm->i6mm_chain.le_next;
    651 				if (imm->i6mm_maddr->in6m_ifp == ifp) {
    652 					LIST_REMOVE(imm, i6mm_chain);
    653 					in6_delmulti(imm->i6mm_maddr);
    654 					free(imm, M_IPMADDR);
    655 				}
    656 			}
    657 		}
    658 	}
    659 }
    660 
    661 void
    662 in6_pcbpurgeif(head, ifp)
    663 	struct in6pcb *head;
    664 	struct ifnet *ifp;
    665 {
    666 	struct in6pcb *in6p, *nin6p;
    667 
    668 	for (in6p = head->in6p_next; in6p != head; in6p = nin6p) {
    669 		nin6p = in6p->in6p_next;
    670 		if (in6p->in6p_route.ro_rt != NULL &&
    671 		    in6p->in6p_route.ro_rt->rt_ifp == ifp)
    672 			in6_rtchange(in6p, 0);
    673 	}
    674 }
    675 
    676 /*
    677  * Check for alternatives when higher level complains
    678  * about service problems.  For now, invalidate cached
    679  * routing information.  If the route was created dynamically
    680  * (by a redirect), time to try a default gateway again.
    681  */
    682 void
    683 in6_losing(in6p)
    684 	struct in6pcb *in6p;
    685 {
    686 	struct rtentry *rt;
    687 	struct rt_addrinfo info;
    688 
    689 	if ((rt = in6p->in6p_route.ro_rt) != NULL) {
    690 		in6p->in6p_route.ro_rt = 0;
    691 		bzero((caddr_t)&info, sizeof(info));
    692 		info.rti_info[RTAX_DST] =
    693 			(struct sockaddr *)&in6p->in6p_route.ro_dst;
    694 		info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
    695 		info.rti_info[RTAX_NETMASK] = rt_mask(rt);
    696 		rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
    697 		if (rt->rt_flags & RTF_DYNAMIC)
    698 			(void)rtrequest(RTM_DELETE, rt_key(rt),
    699 					rt->rt_gateway, rt_mask(rt), rt->rt_flags,
    700 					(struct rtentry **)0);
    701 		else
    702 		/*
    703 		 * A new route can be allocated
    704 		 * the next time output is attempted.
    705 		 */
    706 			rtfree(rt);
    707 	}
    708 }
    709 
    710 /*
    711  * After a routing change, flush old routing
    712  * and allocate a (hopefully) better one.
    713  */
    714 void
    715 in6_rtchange(in6p, errno)
    716 	struct in6pcb *in6p;
    717 	int errno;
    718 {
    719 	if (in6p->in6p_route.ro_rt) {
    720 		rtfree(in6p->in6p_route.ro_rt);
    721 		in6p->in6p_route.ro_rt = 0;
    722 		/*
    723 		 * A new route can be allocated the next time
    724 		 * output is attempted.
    725 		 */
    726 	}
    727 }
    728 
    729 struct in6pcb *
    730 in6_pcblookup(head, faddr6, fport_arg, laddr6, lport_arg, flags)
    731 	struct in6pcb *head;
    732 	struct in6_addr *faddr6, *laddr6;
    733 	u_int fport_arg, lport_arg;
    734 	int flags;
    735 {
    736 	struct in6pcb *in6p, *match = 0;
    737 	int matchwild = 3, wildcard;
    738 	u_int16_t fport = fport_arg, lport = lport_arg;
    739 
    740 	for (in6p = head->in6p_next; in6p != head; in6p = in6p->in6p_next) {
    741 		if (in6p->in6p_lport != lport)
    742 			continue;
    743 		wildcard = 0;
    744 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
    745 			if (IN6_IS_ADDR_UNSPECIFIED(laddr6))
    746 				wildcard++;
    747 			else if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6))
    748 				continue;
    749 		}
    750 		else if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)
    751 			&& in6p->in6p_laddr.s6_addr32[3] == 0) {
    752 			if (!IN6_IS_ADDR_V4MAPPED(laddr6))
    753 				continue;
    754 			if (laddr6->s6_addr32[3] == 0)
    755 				;
    756 			else
    757 				wildcard++;
    758 		}
    759 		else {
    760 			if (IN6_IS_ADDR_V4MAPPED(laddr6)) {
    761 #ifndef INET6_BINDV6ONLY
    762 				if (in6p->in6p_flags & IN6P_BINDV6ONLY)
    763 					continue;
    764 				else
    765 					wildcard++;
    766 #else
    767 				continue;
    768 #endif
    769 			} else if (!IN6_IS_ADDR_UNSPECIFIED(laddr6))
    770 				wildcard++;
    771 		}
    772 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
    773 			if (IN6_IS_ADDR_UNSPECIFIED(faddr6))
    774 				wildcard++;
    775 			else if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, faddr6)
    776 			      || in6p->in6p_fport != fport)
    777 				continue;
    778 		}
    779 		else if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)
    780 			&& in6p->in6p_faddr.s6_addr32[3] == 0) {
    781 			if (!IN6_IS_ADDR_V4MAPPED(faddr6))
    782 				continue;
    783 			if (faddr6->s6_addr32[3] == 0)
    784 				;
    785 			else
    786 				wildcard++;
    787 		}
    788 		else {
    789 			if (IN6_IS_ADDR_V4MAPPED(faddr6)) {
    790 #ifndef INET6_BINDV6ONLY
    791 				if (in6p->in6p_flags & IN6P_BINDV6ONLY)
    792 					continue;
    793 				else
    794 					wildcard++;
    795 #else
    796 				continue;
    797 #endif
    798 			} else if (!IN6_IS_ADDR_UNSPECIFIED(faddr6))
    799 				wildcard++;
    800 		}
    801 
    802 		if (wildcard && (flags & IN6PLOOKUP_WILDCARD) == 0)
    803 			continue;
    804 		if (wildcard < matchwild) {
    805 			match = in6p;
    806 			matchwild = wildcard;
    807 			if (matchwild == 0)
    808 				break;
    809 		}
    810 	}
    811 	return(match);
    812 }
    813 
    814 struct rtentry *
    815 in6_pcbrtentry(in6p)
    816 	struct in6pcb *in6p;
    817 {
    818 	struct route_in6 *ro;
    819 	struct sockaddr_in6 *dst6;
    820 
    821 	ro = &in6p->in6p_route;
    822 	dst6 = (struct sockaddr_in6 *)&ro->ro_dst;
    823 
    824 	if (ro->ro_rt == NULL) {
    825 		/*
    826 		 * No route yet, so try to acquire one.
    827 		 */
    828 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
    829 			bzero(dst6, sizeof(*dst6));
    830 			dst6->sin6_family = AF_INET6;
    831 			dst6->sin6_len = sizeof(struct sockaddr_in6);
    832 			dst6->sin6_addr = in6p->in6p_faddr;
    833 			rtalloc((struct route *)ro);
    834 		}
    835 	}
    836 	return (ro->ro_rt);
    837 }
    838 
    839 struct in6pcb *
    840 in6_pcblookup_connect(head, faddr6, fport_arg, laddr6, lport_arg, faith)
    841 	struct in6pcb *head;
    842 	struct in6_addr *faddr6, *laddr6;
    843 	u_int fport_arg, lport_arg;
    844 	int faith;
    845 {
    846 	struct in6pcb *in6p;
    847 	u_int16_t fport = fport_arg, lport = lport_arg;
    848 
    849 	for (in6p = head->in6p_next; in6p != head; in6p = in6p->in6p_next) {
    850 		/* find exact match on both source and dest */
    851 		if (in6p->in6p_fport != fport)
    852 			continue;
    853 		if (in6p->in6p_lport != lport)
    854 			continue;
    855 		if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr))
    856 			continue;
    857 		if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, faddr6))
    858 			continue;
    859 		if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
    860 			continue;
    861 		if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6))
    862 			continue;
    863 		return in6p;
    864 	}
    865 	return NULL;
    866 }
    867 
    868 struct in6pcb *
    869 in6_pcblookup_bind(head, laddr6, lport_arg, faith)
    870 	struct in6pcb *head;
    871 	struct in6_addr *laddr6;
    872 	u_int lport_arg;
    873 	int faith;
    874 {
    875 	struct in6pcb *in6p, *match;
    876 	u_int16_t lport = lport_arg;
    877 
    878 	match = NULL;
    879 	for (in6p = head->in6p_next; in6p != head; in6p = in6p->in6p_next) {
    880 		/*
    881 	 	 * find destination match.  exact match is preferred
    882 		 * against wildcard match.
    883 		 */
    884 		if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0)
    885 			continue;
    886 		if (in6p->in6p_fport != 0)
    887 			continue;
    888 		if (in6p->in6p_lport != lport)
    889 			continue;
    890 		if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
    891 			if (IN6_IS_ADDR_V4MAPPED(laddr6)) {
    892 #ifndef INET6_BINDV6ONLY
    893 				if (in6p->in6p_flags & IN6P_BINDV6ONLY)
    894 					continue;
    895 				else
    896 					match = in6p;
    897 #else
    898 				continue;
    899 #endif
    900 			} else
    901 				match = in6p;
    902 		}
    903 		else if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr) &&
    904 			 in6p->in6p_laddr.s6_addr32[3] == 0) {
    905 			if (IN6_IS_ADDR_V4MAPPED(laddr6) &&
    906 			    laddr6->s6_addr32[3] != 0)
    907 				match = in6p;
    908 		}
    909 		else if (IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6))
    910 			return in6p;
    911 	}
    912 	return match;
    913 }
    914