Home | History | Annotate | Line # | Download | only in netinet6
in6_pcb.c revision 1.39
      1 /*	$NetBSD: in6_pcb.c,v 1.39 2001/07/25 23:28:03 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 	return(0);
    401 }
    402 
    403 void
    404 in6_pcbdisconnect(in6p)
    405 	struct in6pcb *in6p;
    406 {
    407 	bzero((caddr_t)&in6p->in6p_faddr, sizeof(in6p->in6p_faddr));
    408 	in6p->in6p_fport = 0;
    409 	if (in6p->in6p_socket->so_state & SS_NOFDREF)
    410 		in6_pcbdetach(in6p);
    411 }
    412 
    413 void
    414 in6_pcbdetach(in6p)
    415 	struct in6pcb *in6p;
    416 {
    417 	struct socket *so = in6p->in6p_socket;
    418 
    419 #ifdef IPSEC
    420 	ipsec6_delete_pcbpolicy(in6p);
    421 #endif /* IPSEC */
    422 	sotoin6pcb(so) = 0;
    423 	sofree(so);
    424 	if (in6p->in6p_options)
    425 		m_freem(in6p->in6p_options);
    426 	if (in6p->in6p_outputopts) {
    427 		if (in6p->in6p_outputopts->ip6po_rthdr &&
    428 		    in6p->in6p_outputopts->ip6po_route.ro_rt)
    429 			RTFREE(in6p->in6p_outputopts->ip6po_route.ro_rt);
    430 		if (in6p->in6p_outputopts->ip6po_m)
    431 			(void)m_free(in6p->in6p_outputopts->ip6po_m);
    432 		free(in6p->in6p_outputopts, M_IP6OPT);
    433 	}
    434 	if (in6p->in6p_route.ro_rt)
    435 		rtfree(in6p->in6p_route.ro_rt);
    436 	ip6_freemoptions(in6p->in6p_moptions);
    437 	in6p->in6p_next->in6p_prev = in6p->in6p_prev;
    438 	in6p->in6p_prev->in6p_next = in6p->in6p_next;
    439 	in6p->in6p_prev = NULL;
    440 	FREE(in6p, M_PCB);
    441 }
    442 
    443 void
    444 in6_setsockaddr(in6p, nam)
    445 	struct in6pcb *in6p;
    446 	struct mbuf *nam;
    447 {
    448 	struct sockaddr_in6 *sin6;
    449 
    450 	nam->m_len = sizeof(*sin6);
    451 	sin6 = mtod(nam, struct sockaddr_in6 *);
    452 	bzero((caddr_t)sin6, sizeof(*sin6));
    453 	sin6->sin6_family = AF_INET6;
    454 	sin6->sin6_len = sizeof(struct sockaddr_in6);
    455 	sin6->sin6_port = in6p->in6p_lport;
    456 	/* KAME hack: recover scopeid */
    457 	(void)in6_recoverscope(sin6, &in6p->in6p_laddr, NULL);
    458 }
    459 
    460 void
    461 in6_setpeeraddr(in6p, nam)
    462 	struct in6pcb *in6p;
    463 	struct mbuf *nam;
    464 {
    465 	struct sockaddr_in6 *sin6;
    466 
    467 	nam->m_len = sizeof(*sin6);
    468 	sin6 = mtod(nam, struct sockaddr_in6 *);
    469 	bzero((caddr_t)sin6, sizeof(*sin6));
    470 	sin6->sin6_family = AF_INET6;
    471 	sin6->sin6_len = sizeof(struct sockaddr_in6);
    472 	sin6->sin6_port = in6p->in6p_fport;
    473 	/* KAME hack: recover scopeid */
    474 	(void)in6_recoverscope(sin6, &in6p->in6p_faddr, NULL);
    475 }
    476 
    477 /*
    478  * Pass some notification to all connections of a protocol
    479  * associated with address dst.  The local address and/or port numbers
    480  * may be specified to limit the search.  The "usual action" will be
    481  * taken, depending on the ctlinput cmd.  The caller must filter any
    482  * cmds that are uninteresting (e.g., no error in the map).
    483  * Call the protocol specific routine (if any) to report
    484  * any errors for each matching socket.
    485  *
    486  * Must be called at splsoftnet.
    487  *
    488  * Note: src (4th arg) carries the flowlabel value on the original IPv6
    489  * header, in sin6_flowinfo member.
    490  */
    491 int
    492 in6_pcbnotify(head, dst, fport_arg, src, lport_arg, cmd, cmdarg, notify)
    493 	struct in6pcb *head;
    494 	struct sockaddr *dst, *src;
    495 	u_int fport_arg, lport_arg;
    496 	int cmd;
    497 	void *cmdarg;
    498 	void (*notify) __P((struct in6pcb *, int));
    499 {
    500 	struct in6pcb *in6p, *nin6p;
    501 	struct sockaddr_in6 sa6_src, *sa6_dst;
    502 	u_int16_t fport = fport_arg, lport = lport_arg;
    503 	int errno;
    504 	int nmatch = 0;
    505 	u_int32_t flowinfo;
    506 
    507 	if ((unsigned)cmd > PRC_NCMDS || dst->sa_family != AF_INET6)
    508 		return 0;
    509 
    510 	sa6_dst = (struct sockaddr_in6 *)dst;
    511 	if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
    512 		return 0;
    513 
    514 	/*
    515 	 * note that src can be NULL when we get notify by local fragmentation.
    516 	 */
    517 	sa6_src = (src == NULL) ? sa6_any : *(struct sockaddr_in6 *)src;
    518 	flowinfo = sa6_src.sin6_flowinfo;
    519 
    520 	/*
    521 	 * Redirects go to all references to the destination,
    522 	 * and use in6_rtchange to invalidate the route cache.
    523 	 * Dead host indications: also use in6_rtchange to invalidate
    524 	 * the cache, and deliver the error to all the sockets.
    525 	 * Otherwise, if we have knowledge of the local port and address,
    526 	 * deliver only to that socket.
    527 	 */
    528 	if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
    529 		fport = 0;
    530 		lport = 0;
    531 		bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
    532 
    533 		if (cmd != PRC_HOSTDEAD)
    534 			notify = in6_rtchange;
    535 	}
    536 
    537 	errno = inet6ctlerrmap[cmd];
    538 	for (in6p = head->in6p_next; in6p != head; in6p = nin6p) {
    539 		nin6p = in6p->in6p_next;
    540 
    541 		/*
    542 		 * Under the following condition, notify of redirects
    543 		 * to the pcb, without making address matches against inpcb.
    544 		 * - redirect notification is arrived.
    545 		 * - the inpcb is unconnected.
    546 		 * - the inpcb is caching !RTF_HOST routing entry.
    547 		 * - the ICMPv6 notification is from the gateway cached in the
    548 		 *   inpcb.  i.e. ICMPv6 notification is from nexthop gateway
    549 		 *   the inpcb used very recently.
    550 		 *
    551 		 * This is to improve interaction between netbsd/openbsd
    552 		 * redirect handling code, and inpcb route cache code.
    553 		 * without the clause, !RTF_HOST routing entry (which carries
    554 		 * gateway used by inpcb right before the ICMPv6 redirect)
    555 		 * will be cached forever in unconnected inpcb.
    556 		 *
    557 		 * There still is a question regarding to what is TRT:
    558 		 * - On bsdi/freebsd, RTF_HOST (cloned) routing entry will be
    559 		 *   generated on packet output.  inpcb will always cache
    560 		 *   RTF_HOST routing entry so there's no need for the clause
    561 		 *   (ICMPv6 redirect will update RTF_HOST routing entry,
    562 		 *   and inpcb is caching it already).
    563 		 *   However, bsdi/freebsd are vulnerable to local DoS attacks
    564 		 *   due to the cloned routing entries.
    565 		 * - Specwise, "destination cache" is mentioned in RFC2461.
    566 		 *   Jinmei says that it implies bsdi/freebsd behavior, itojun
    567 		 *   is not really convinced.
    568 		 * - Having hiwat/lowat on # of cloned host route (redirect/
    569 		 *   pmtud) may be a good idea.  netbsd/openbsd has it.  see
    570 		 *   icmp6_mtudisc_update().
    571 		 */
    572 		if ((PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) &&
    573 		    IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
    574 		    in6p->in6p_route.ro_rt &&
    575 		    !(in6p->in6p_route.ro_rt->rt_flags & RTF_HOST)) {
    576 			struct sockaddr_in6 *dst6;
    577 
    578 			dst6 = (struct sockaddr_in6 *)&in6p->in6p_route.ro_dst;
    579 			if (IN6_ARE_ADDR_EQUAL(&dst6->sin6_addr,
    580 			    &sa6_dst->sin6_addr))
    581 				goto do_notify;
    582 		}
    583 
    584 		/*
    585 		 * Detect if we should notify the error. If no source and
    586 		 * destination ports are specifed, but non-zero flowinfo and
    587 		 * local address match, notify the error. This is the case
    588 		 * when the error is delivered with an encrypted buffer
    589 		 * by ESP. Otherwise, just compare addresses and ports
    590 		 * as usual.
    591 		 */
    592 		if (lport == 0 && fport == 0 && flowinfo &&
    593 		    in6p->in6p_socket != NULL &&
    594 		    flowinfo == (in6p->in6p_flowinfo & IPV6_FLOWLABEL_MASK) &&
    595 		    IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &sa6_src.sin6_addr))
    596 			goto do_notify;
    597 		else if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
    598 					     &sa6_dst->sin6_addr) ||
    599 		    in6p->in6p_socket == 0 ||
    600 		    (lport && in6p->in6p_lport != lport) ||
    601 		    (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
    602 		     !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
    603 					 &sa6_src.sin6_addr)) ||
    604 		    (fport && in6p->in6p_fport != fport))
    605 			continue;
    606 
    607 	  do_notify:
    608 		if (notify)
    609 			(*notify)(in6p, errno);
    610 		nmatch++;
    611 	}
    612 	return nmatch;
    613 }
    614 
    615 void
    616 in6_pcbpurgeif0(head, ifp)
    617 	struct in6pcb *head;
    618 	struct ifnet *ifp;
    619 {
    620 	struct in6pcb *in6p, *nin6p;
    621 	struct ip6_moptions *im6o;
    622 	struct in6_multi_mship *imm, *nimm;
    623 
    624 	for (in6p = head->in6p_next; in6p != head; in6p = nin6p) {
    625 		nin6p = in6p->in6p_next;
    626 		im6o = in6p->in6p_moptions;
    627 		if (im6o) {
    628 			/*
    629 			 * Unselect the outgoing interface if it is being
    630 			 * detached.
    631 			 */
    632 			if (im6o->im6o_multicast_ifp == ifp)
    633 				im6o->im6o_multicast_ifp = NULL;
    634 
    635 			/*
    636 			 * Drop multicast group membership if we joined
    637 			 * through the interface being detached.
    638 			 * XXX controversial - is it really legal for kernel
    639 			 * to force this?
    640 			 */
    641 			for (imm = im6o->im6o_memberships.lh_first;
    642 			     imm != NULL; imm = nimm) {
    643 				nimm = imm->i6mm_chain.le_next;
    644 				if (imm->i6mm_maddr->in6m_ifp == ifp) {
    645 					LIST_REMOVE(imm, i6mm_chain);
    646 					in6_delmulti(imm->i6mm_maddr);
    647 					free(imm, M_IPMADDR);
    648 				}
    649 			}
    650 		}
    651 	}
    652 }
    653 
    654 void
    655 in6_pcbpurgeif(head, ifp)
    656 	struct in6pcb *head;
    657 	struct ifnet *ifp;
    658 {
    659 	struct in6pcb *in6p, *nin6p;
    660 
    661 	for (in6p = head->in6p_next; in6p != head; in6p = nin6p) {
    662 		nin6p = in6p->in6p_next;
    663 		if (in6p->in6p_route.ro_rt != NULL &&
    664 		    in6p->in6p_route.ro_rt->rt_ifp == ifp)
    665 			in6_rtchange(in6p, 0);
    666 	}
    667 }
    668 
    669 /*
    670  * Check for alternatives when higher level complains
    671  * about service problems.  For now, invalidate cached
    672  * routing information.  If the route was created dynamically
    673  * (by a redirect), time to try a default gateway again.
    674  */
    675 void
    676 in6_losing(in6p)
    677 	struct in6pcb *in6p;
    678 {
    679 	struct rtentry *rt;
    680 	struct rt_addrinfo info;
    681 
    682 	if ((rt = in6p->in6p_route.ro_rt) != NULL) {
    683 		in6p->in6p_route.ro_rt = 0;
    684 		bzero((caddr_t)&info, sizeof(info));
    685 		info.rti_info[RTAX_DST] =
    686 			(struct sockaddr *)&in6p->in6p_route.ro_dst;
    687 		info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
    688 		info.rti_info[RTAX_NETMASK] = rt_mask(rt);
    689 		rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
    690 		if (rt->rt_flags & RTF_DYNAMIC)
    691 			(void)rtrequest(RTM_DELETE, rt_key(rt),
    692 					rt->rt_gateway, rt_mask(rt), rt->rt_flags,
    693 					(struct rtentry **)0);
    694 		else
    695 		/*
    696 		 * A new route can be allocated
    697 		 * the next time output is attempted.
    698 		 */
    699 			rtfree(rt);
    700 	}
    701 }
    702 
    703 /*
    704  * After a routing change, flush old routing
    705  * and allocate a (hopefully) better one.
    706  */
    707 void
    708 in6_rtchange(in6p, errno)
    709 	struct in6pcb *in6p;
    710 	int errno;
    711 {
    712 	if (in6p->in6p_route.ro_rt) {
    713 		rtfree(in6p->in6p_route.ro_rt);
    714 		in6p->in6p_route.ro_rt = 0;
    715 		/*
    716 		 * A new route can be allocated the next time
    717 		 * output is attempted.
    718 		 */
    719 	}
    720 }
    721 
    722 struct in6pcb *
    723 in6_pcblookup(head, faddr6, fport_arg, laddr6, lport_arg, flags)
    724 	struct in6pcb *head;
    725 	struct in6_addr *faddr6, *laddr6;
    726 	u_int fport_arg, lport_arg;
    727 	int flags;
    728 {
    729 	struct in6pcb *in6p, *match = 0;
    730 	int matchwild = 3, wildcard;
    731 	u_int16_t fport = fport_arg, lport = lport_arg;
    732 
    733 	for (in6p = head->in6p_next; in6p != head; in6p = in6p->in6p_next) {
    734 		if (in6p->in6p_lport != lport)
    735 			continue;
    736 		wildcard = 0;
    737 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
    738 			if (IN6_IS_ADDR_UNSPECIFIED(laddr6))
    739 				wildcard++;
    740 			else if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6))
    741 				continue;
    742 		}
    743 		else if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)
    744 			&& in6p->in6p_laddr.s6_addr32[3] == 0) {
    745 			if (!IN6_IS_ADDR_V4MAPPED(laddr6))
    746 				continue;
    747 			if (laddr6->s6_addr32[3] == 0)
    748 				;
    749 			else
    750 				wildcard++;
    751 		}
    752 		else {
    753 			if (IN6_IS_ADDR_V4MAPPED(laddr6)) {
    754 #ifndef INET6_BINDV6ONLY
    755 				if (in6p->in6p_flags & IN6P_BINDV6ONLY)
    756 					continue;
    757 				else
    758 					wildcard++;
    759 #else
    760 				continue;
    761 #endif
    762 			} else if (!IN6_IS_ADDR_UNSPECIFIED(laddr6))
    763 				wildcard++;
    764 		}
    765 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
    766 			if (IN6_IS_ADDR_UNSPECIFIED(faddr6))
    767 				wildcard++;
    768 			else if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, faddr6)
    769 			      || in6p->in6p_fport != fport)
    770 				continue;
    771 		}
    772 		else if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)
    773 			&& in6p->in6p_faddr.s6_addr32[3] == 0) {
    774 			if (!IN6_IS_ADDR_V4MAPPED(faddr6))
    775 				continue;
    776 			if (faddr6->s6_addr32[3] == 0)
    777 				;
    778 			else
    779 				wildcard++;
    780 		}
    781 		else {
    782 			if (IN6_IS_ADDR_V4MAPPED(faddr6)) {
    783 #ifndef INET6_BINDV6ONLY
    784 				if (in6p->in6p_flags & IN6P_BINDV6ONLY)
    785 					continue;
    786 				else
    787 					wildcard++;
    788 #else
    789 				continue;
    790 #endif
    791 			} else if (!IN6_IS_ADDR_UNSPECIFIED(faddr6))
    792 				wildcard++;
    793 		}
    794 
    795 		if (wildcard && (flags & IN6PLOOKUP_WILDCARD) == 0)
    796 			continue;
    797 		if (wildcard < matchwild) {
    798 			match = in6p;
    799 			matchwild = wildcard;
    800 			if (matchwild == 0)
    801 				break;
    802 		}
    803 	}
    804 	return(match);
    805 }
    806 
    807 struct rtentry *
    808 in6_pcbrtentry(in6p)
    809 	struct in6pcb *in6p;
    810 {
    811 	struct route_in6 *ro;
    812 	struct sockaddr_in6 *dst6;
    813 
    814 	ro = &in6p->in6p_route;
    815 	dst6 = (struct sockaddr_in6 *)&ro->ro_dst;
    816 
    817 	if (ro->ro_rt == NULL) {
    818 		/*
    819 		 * No route yet, so try to acquire one.
    820 		 */
    821 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
    822 			bzero(dst6, sizeof(*dst6));
    823 			dst6->sin6_family = AF_INET6;
    824 			dst6->sin6_len = sizeof(struct sockaddr_in6);
    825 			dst6->sin6_addr = in6p->in6p_faddr;
    826 			rtalloc((struct route *)ro);
    827 		}
    828 	}
    829 	return (ro->ro_rt);
    830 }
    831 
    832 struct in6pcb *
    833 in6_pcblookup_connect(head, faddr6, fport_arg, laddr6, lport_arg, faith)
    834 	struct in6pcb *head;
    835 	struct in6_addr *faddr6, *laddr6;
    836 	u_int fport_arg, lport_arg;
    837 	int faith;
    838 {
    839 	struct in6pcb *in6p;
    840 	u_int16_t fport = fport_arg, lport = lport_arg;
    841 
    842 	for (in6p = head->in6p_next; in6p != head; in6p = in6p->in6p_next) {
    843 		/* find exact match on both source and dest */
    844 		if (in6p->in6p_fport != fport)
    845 			continue;
    846 		if (in6p->in6p_lport != lport)
    847 			continue;
    848 		if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr))
    849 			continue;
    850 		if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, faddr6))
    851 			continue;
    852 		if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
    853 			continue;
    854 		if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6))
    855 			continue;
    856 		return in6p;
    857 	}
    858 	return NULL;
    859 }
    860 
    861 struct in6pcb *
    862 in6_pcblookup_bind(head, laddr6, lport_arg, faith)
    863 	struct in6pcb *head;
    864 	struct in6_addr *laddr6;
    865 	u_int lport_arg;
    866 	int faith;
    867 {
    868 	struct in6pcb *in6p, *match;
    869 	u_int16_t lport = lport_arg;
    870 
    871 	match = NULL;
    872 	for (in6p = head->in6p_next; in6p != head; in6p = in6p->in6p_next) {
    873 		/*
    874 	 	 * find destination match.  exact match is preferred
    875 		 * against wildcard match.
    876 		 */
    877 		if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0)
    878 			continue;
    879 		if (in6p->in6p_fport != 0)
    880 			continue;
    881 		if (in6p->in6p_lport != lport)
    882 			continue;
    883 		if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
    884 			if (IN6_IS_ADDR_V4MAPPED(laddr6)) {
    885 #ifndef INET6_BINDV6ONLY
    886 				if (in6p->in6p_flags & IN6P_BINDV6ONLY)
    887 					continue;
    888 				else
    889 					match = in6p;
    890 #else
    891 				continue;
    892 #endif
    893 			} else
    894 				match = in6p;
    895 		}
    896 		else if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr) &&
    897 			 in6p->in6p_laddr.s6_addr32[3] == 0) {
    898 			if (IN6_IS_ADDR_V4MAPPED(laddr6) &&
    899 			    laddr6->s6_addr32[3] != 0)
    900 				match = in6p;
    901 		}
    902 		else if (IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6))
    903 			return in6p;
    904 	}
    905 	return match;
    906 }
    907