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