xref: /netbsd-src/sys/netinet6/in6_pcb.c (revision 796c32c94f6e154afc9de0f63da35c91bb739b45)
1 /*	$NetBSD: in6_pcb.c,v 1.161 2017/04/25 05:44:11 ozaki-r 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.161 2017/04/25 05:44:11 ozaki-r Exp $");
66 
67 #ifdef _KERNEL_OPT
68 #include "opt_inet.h"
69 #include "opt_ipsec.h"
70 #endif
71 
72 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/mbuf.h>
75 #include <sys/protosw.h>
76 #include <sys/socket.h>
77 #include <sys/socketvar.h>
78 #include <sys/ioctl.h>
79 #include <sys/errno.h>
80 #include <sys/time.h>
81 #include <sys/proc.h>
82 #include <sys/kauth.h>
83 #include <sys/domain.h>
84 #include <sys/once.h>
85 
86 #include <net/if.h>
87 #include <net/route.h>
88 
89 #include <netinet/in.h>
90 #include <netinet/in_var.h>
91 #include <netinet/in_systm.h>
92 #include <netinet/ip.h>
93 #include <netinet/in_pcb.h>
94 #include <netinet/ip6.h>
95 #include <netinet/portalgo.h>
96 #include <netinet6/ip6_var.h>
97 #include <netinet6/in6_pcb.h>
98 #include <netinet6/scope6_var.h>
99 
100 #include "faith.h"
101 
102 #ifdef IPSEC
103 #include <netipsec/ipsec.h>
104 #include <netipsec/ipsec6.h>
105 #include <netipsec/key.h>
106 #endif /* IPSEC */
107 
108 #include <netinet/tcp_vtw.h>
109 
110 const struct in6_addr zeroin6_addr;
111 
112 #define	IN6PCBHASH_PORT(table, lport) \
113 	&(table)->inpt_porthashtbl[ntohs(lport) & (table)->inpt_porthash]
114 #define IN6PCBHASH_BIND(table, laddr, lport) \
115 	&(table)->inpt_bindhashtbl[ \
116 	    (((laddr)->s6_addr32[0] ^ (laddr)->s6_addr32[1] ^ \
117 	      (laddr)->s6_addr32[2] ^ (laddr)->s6_addr32[3]) + ntohs(lport)) & \
118 	    (table)->inpt_bindhash]
119 #define IN6PCBHASH_CONNECT(table, faddr, fport, laddr, lport) \
120 	&(table)->inpt_bindhashtbl[ \
121 	    ((((faddr)->s6_addr32[0] ^ (faddr)->s6_addr32[1] ^ \
122 	      (faddr)->s6_addr32[2] ^ (faddr)->s6_addr32[3]) + ntohs(fport)) + \
123 	     (((laddr)->s6_addr32[0] ^ (laddr)->s6_addr32[1] ^ \
124 	      (laddr)->s6_addr32[2] ^ (laddr)->s6_addr32[3]) + \
125 	      ntohs(lport))) & (table)->inpt_bindhash]
126 
127 int ip6_anonportmin = IPV6PORT_ANONMIN;
128 int ip6_anonportmax = IPV6PORT_ANONMAX;
129 int ip6_lowportmin  = IPV6PORT_RESERVEDMIN;
130 int ip6_lowportmax  = IPV6PORT_RESERVEDMAX;
131 
132 static struct pool in6pcb_pool;
133 
134 static int
135 in6pcb_poolinit(void)
136 {
137 
138 	pool_init(&in6pcb_pool, sizeof(struct in6pcb), 0, 0, 0, "in6pcbpl",
139 	    NULL, IPL_SOFTNET);
140 	return 0;
141 }
142 
143 void
144 in6_pcbinit(struct inpcbtable *table, int bindhashsize, int connecthashsize)
145 {
146 	static ONCE_DECL(control);
147 
148 	in_pcbinit(table, bindhashsize, connecthashsize);
149 	table->inpt_lastport = (u_int16_t)ip6_anonportmax;
150 
151 	RUN_ONCE(&control, in6pcb_poolinit);
152 }
153 
154 int
155 in6_pcballoc(struct socket *so, void *v)
156 {
157 	struct inpcbtable *table = v;
158 	struct in6pcb *in6p;
159 	int s;
160 
161 	KASSERT(so->so_proto->pr_domain->dom_family == AF_INET6);
162 
163 	in6p = pool_get(&in6pcb_pool, PR_NOWAIT);
164 	if (in6p == NULL)
165 		return (ENOBUFS);
166 	memset((void *)in6p, 0, sizeof(*in6p));
167 	in6p->in6p_af = AF_INET6;
168 	in6p->in6p_table = table;
169 	in6p->in6p_socket = so;
170 	in6p->in6p_hops = -1;	/* use kernel default */
171 	in6p->in6p_icmp6filt = NULL;
172 	in6p->in6p_portalgo = PORTALGO_DEFAULT;
173 	in6p->in6p_bindportonsend = false;
174 #if defined(IPSEC)
175 	if (ipsec_enabled) {
176 		int error = ipsec_init_pcbpolicy(so, &in6p->in6p_sp);
177 		if (error != 0) {
178 			pool_put(&in6pcb_pool, in6p);
179 			return error;
180 		}
181 		in6p->in6p_sp->sp_inph = (struct inpcb_hdr *)in6p;
182 	}
183 #endif /* IPSEC */
184 	s = splsoftnet();
185 	TAILQ_INSERT_HEAD(&table->inpt_queue, (struct inpcb_hdr*)in6p,
186 	    inph_queue);
187 	LIST_INSERT_HEAD(IN6PCBHASH_PORT(table, in6p->in6p_lport),
188 	    &in6p->in6p_head, inph_lhash);
189 	in6_pcbstate(in6p, IN6P_ATTACHED);
190 	splx(s);
191 	if (ip6_v6only)
192 		in6p->in6p_flags |= IN6P_IPV6_V6ONLY;
193 	so->so_pcb = (void *)in6p;
194 	return (0);
195 }
196 
197 /*
198  * Bind address from sin6 to in6p.
199  */
200 static int
201 in6_pcbbind_addr(struct in6pcb *in6p, struct sockaddr_in6 *sin6, struct lwp *l)
202 {
203 	int error;
204 	int s;
205 
206 	/*
207 	 * We should check the family, but old programs
208 	 * incorrectly fail to intialize it.
209 	 */
210 	if (sin6->sin6_family != AF_INET6)
211 		return (EAFNOSUPPORT);
212 
213 #ifndef INET
214 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
215 		return (EADDRNOTAVAIL);
216 #endif
217 
218 	if ((error = sa6_embedscope(sin6, ip6_use_defzone)) != 0)
219 		return (error);
220 
221 	s = pserialize_read_enter();
222 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
223 		if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0) {
224 			error = EINVAL;
225 			goto out;
226 		}
227 		if (sin6->sin6_addr.s6_addr32[3]) {
228 			struct sockaddr_in sin;
229 
230 			memset(&sin, 0, sizeof(sin));
231 			sin.sin_len = sizeof(sin);
232 			sin.sin_family = AF_INET;
233 			bcopy(&sin6->sin6_addr.s6_addr32[3],
234 			    &sin.sin_addr, sizeof(sin.sin_addr));
235 			if (!IN_MULTICAST(sin.sin_addr.s_addr)) {
236 				struct ifaddr *ifa;
237 				ifa = ifa_ifwithaddr((struct sockaddr *)&sin);
238 				if (ifa == NULL) {
239 					error = EADDRNOTAVAIL;
240 					goto out;
241 				}
242 			}
243 		}
244 	} else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
245 		// succeed
246 	} else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
247 		struct ifaddr *ifa = NULL;
248 
249 		if ((in6p->in6p_flags & IN6P_FAITH) == 0) {
250 			ifa = ifa_ifwithaddr(sin6tosa(sin6));
251 			if (ifa == NULL) {
252 				error = EADDRNOTAVAIL;
253 				goto out;
254 			}
255 		}
256 
257 		/*
258 		 * bind to an anycast address might accidentally
259 		 * cause sending a packet with an anycast source
260 		 * address, so we forbid it.
261 		 *
262 		 * We should allow to bind to a deprecated address,
263 		 * since the application dare to use it.
264 		 * But, can we assume that they are careful enough
265 		 * to check if the address is deprecated or not?
266 		 * Maybe, as a safeguard, we should have a setsockopt
267 		 * flag to control the bind(2) behavior against
268 		 * deprecated addresses (default: forbid bind(2)).
269 		 */
270 		if (ifa &&
271 		    ifatoia6(ifa)->ia6_flags &
272 		    (IN6_IFF_ANYCAST | IN6_IFF_DUPLICATED)) {
273 			error = EADDRNOTAVAIL;
274 			goto out;
275 		}
276 	}
277 	in6p->in6p_laddr = sin6->sin6_addr;
278 	error = 0;
279 out:
280 	pserialize_read_exit(s);
281 	return error;
282 }
283 
284 /*
285  * Bind port from sin6 to in6p.
286  */
287 static int
288 in6_pcbbind_port(struct in6pcb *in6p, struct sockaddr_in6 *sin6, struct lwp *l)
289 {
290 	struct inpcbtable *table = in6p->in6p_table;
291 	struct socket *so = in6p->in6p_socket;
292 	int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
293 	int error;
294 
295 	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0 &&
296 	   ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 ||
297 	    (so->so_options & SO_ACCEPTCONN) == 0))
298 		wild = 1;
299 
300 	if (sin6->sin6_port != 0) {
301 		enum kauth_network_req req;
302 
303 #ifndef IPNOPRIVPORTS
304 		if (ntohs(sin6->sin6_port) < IPV6PORT_RESERVED)
305 			req = KAUTH_REQ_NETWORK_BIND_PRIVPORT;
306 		else
307 #endif /* IPNOPRIVPORTS */
308 			req = KAUTH_REQ_NETWORK_BIND_PORT;
309 
310 		error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_BIND,
311 		    req, so, sin6, NULL);
312 		if (error)
313 			return (EACCES);
314 	}
315 
316 	if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
317 		/*
318 		 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
319 		 * allow compepte duplication of binding if
320 		 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
321 		 * and a multicast address is bound on both
322 		 * new and duplicated sockets.
323 		 */
324 		if (so->so_options & (SO_REUSEADDR | SO_REUSEPORT))
325 			reuseport = SO_REUSEADDR|SO_REUSEPORT;
326 	}
327 
328 	if (sin6->sin6_port != 0) {
329 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
330 #ifdef INET
331 			struct inpcb *t;
332 			struct vestigial_inpcb vestige;
333 
334 			t = in_pcblookup_port(table,
335 			    *(struct in_addr *)&sin6->sin6_addr.s6_addr32[3],
336 			    sin6->sin6_port, wild, &vestige);
337 			if (t && (reuseport & t->inp_socket->so_options) == 0)
338 				return (EADDRINUSE);
339 			if (!t
340 			    && vestige.valid
341 			    && !(reuseport && vestige.reuse_port))
342 			    return EADDRINUSE;
343 #else
344 			return (EADDRNOTAVAIL);
345 #endif
346 		}
347 
348 		{
349 			struct in6pcb *t;
350 			struct vestigial_inpcb vestige;
351 
352 			t = in6_pcblookup_port(table, &sin6->sin6_addr,
353 			    sin6->sin6_port, wild, &vestige);
354 			if (t && (reuseport & t->in6p_socket->so_options) == 0)
355 				return (EADDRINUSE);
356 			if (!t
357 			    && vestige.valid
358 			    && !(reuseport && vestige.reuse_port))
359 			    return EADDRINUSE;
360 		}
361 	}
362 
363 	if (sin6->sin6_port == 0) {
364 		int e;
365 		e = in6_pcbsetport(sin6, in6p, l);
366 		if (e != 0)
367 			return (e);
368 	} else {
369 		in6p->in6p_lport = sin6->sin6_port;
370 		in6_pcbstate(in6p, IN6P_BOUND);
371 	}
372 
373 	LIST_REMOVE(&in6p->in6p_head, inph_lhash);
374 	LIST_INSERT_HEAD(IN6PCBHASH_PORT(table, in6p->in6p_lport),
375 	    &in6p->in6p_head, inph_lhash);
376 
377 	return (0);
378 }
379 
380 int
381 in6_pcbbind(void *v, struct sockaddr_in6 *sin6, struct lwp *l)
382 {
383 	struct in6pcb *in6p = v;
384 	struct sockaddr_in6 lsin6;
385 	int error;
386 
387 	if (in6p->in6p_af != AF_INET6)
388 		return (EINVAL);
389 
390 	/*
391 	 * If we already have a local port or a local address it means we're
392 	 * bounded.
393 	 */
394 	if (in6p->in6p_lport || !(IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) ||
395 	    (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr) &&
396 	      in6p->in6p_laddr.s6_addr32[3] == 0)))
397 		return (EINVAL);
398 
399 	if (NULL != sin6) {
400 		/* We were provided a sockaddr_in6 to use. */
401 		if (sin6->sin6_len != sizeof(*sin6))
402 			return (EINVAL);
403 	} else {
404 		/* We always bind to *something*, even if it's "anything". */
405 		lsin6 = *((const struct sockaddr_in6 *)
406 		    in6p->in6p_socket->so_proto->pr_domain->dom_sa_any);
407 		sin6 = &lsin6;
408 	}
409 
410 	/* Bind address. */
411 	error = in6_pcbbind_addr(in6p, sin6, l);
412 	if (error)
413 		return (error);
414 
415 	/* Bind port. */
416 	error = in6_pcbbind_port(in6p, sin6, l);
417 	if (error) {
418 		/*
419 		 * Reset the address here to "any" so we don't "leak" the
420 		 * in6pcb.
421 		 */
422 		in6p->in6p_laddr = in6addr_any;
423 
424 		return (error);
425 	}
426 
427 
428 #if 0
429 	in6p->in6p_flowinfo = 0;	/* XXX */
430 #endif
431 	return (0);
432 }
433 
434 /*
435  * Connect from a socket to a specified address.
436  * Both address and port must be specified in argument sin6.
437  * If don't have a local address for this socket yet,
438  * then pick one.
439  */
440 int
441 in6_pcbconnect(void *v, struct sockaddr_in6 *sin6, struct lwp *l)
442 {
443 	struct in6pcb *in6p = v;
444 	struct in6_addr *in6a = NULL;
445 	struct in6_addr ia6;
446 	struct ifnet *ifp = NULL;	/* outgoing interface */
447 	int error = 0;
448 	int scope_ambiguous = 0;
449 #ifdef INET
450 	struct in6_addr mapped;
451 #endif
452 	struct sockaddr_in6 tmp;
453 	struct vestigial_inpcb vestige;
454 	struct psref psref;
455 	int bound;
456 
457 	(void)&in6a;				/* XXX fool gcc */
458 
459 	if (in6p->in6p_af != AF_INET6)
460 		return (EINVAL);
461 
462 	if (sin6->sin6_len != sizeof(*sin6))
463 		return (EINVAL);
464 	if (sin6->sin6_family != AF_INET6)
465 		return (EAFNOSUPPORT);
466 	if (sin6->sin6_port == 0)
467 		return (EADDRNOTAVAIL);
468 
469 	if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) &&
470 	    in6p->in6p_socket->so_type == SOCK_STREAM)
471 		return EADDRNOTAVAIL;
472 
473 	if (sin6->sin6_scope_id == 0 && !ip6_use_defzone)
474 		scope_ambiguous = 1;
475 	if ((error = sa6_embedscope(sin6, ip6_use_defzone)) != 0)
476 		return(error);
477 
478 	/* sanity check for mapped address case */
479 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
480 		if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
481 			return EINVAL;
482 		if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
483 			in6p->in6p_laddr.s6_addr16[5] = htons(0xffff);
484 		if (!IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr))
485 			return EINVAL;
486 	} else
487 	{
488 		if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr))
489 			return EINVAL;
490 	}
491 
492 	/* protect *sin6 from overwrites */
493 	tmp = *sin6;
494 	sin6 = &tmp;
495 
496 	bound = curlwp_bind();
497 	/* Source address selection. */
498 	if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr) &&
499 	    in6p->in6p_laddr.s6_addr32[3] == 0) {
500 #ifdef INET
501 		struct sockaddr_in sin;
502 		struct in_ifaddr *ia4;
503 		struct psref _psref;
504 
505 		memset(&sin, 0, sizeof(sin));
506 		sin.sin_len = sizeof(sin);
507 		sin.sin_family = AF_INET;
508 		memcpy(&sin.sin_addr, &sin6->sin6_addr.s6_addr32[3],
509 			sizeof(sin.sin_addr));
510 		ia4 = in_selectsrc(&sin, &in6p->in6p_route,
511 			in6p->in6p_socket->so_options, NULL, &error, &_psref);
512 		if (ia4 == NULL) {
513 			if (error == 0)
514 				error = EADDRNOTAVAIL;
515 			return (error);
516 		}
517 		memset(&mapped, 0, sizeof(mapped));
518 		mapped.s6_addr16[5] = htons(0xffff);
519 		memcpy(&mapped.s6_addr32[3], &IA_SIN(ia4)->sin_addr,
520 		    sizeof(IA_SIN(ia4)->sin_addr));
521 		ia4_release(ia4, &_psref);
522 		in6a = &mapped;
523 #else
524 		return EADDRNOTAVAIL;
525 #endif
526 	} else {
527 		/*
528 		 * XXX: in6_selectsrc might replace the bound local address
529 		 * with the address specified by setsockopt(IPV6_PKTINFO).
530 		 * Is it the intended behavior?
531 		 */
532 		error = in6_selectsrc(sin6, in6p->in6p_outputopts,
533 		    in6p->in6p_moptions, &in6p->in6p_route, &in6p->in6p_laddr,
534 		    &ifp, &psref, &ia6);
535 		if (error == 0)
536 			in6a = &ia6;
537 		if (ifp && scope_ambiguous &&
538 		    (error = in6_setscope(&sin6->sin6_addr, ifp, NULL)) != 0) {
539 			if_put(ifp, &psref);
540 			curlwp_bindx(bound);
541 			return error;
542 		}
543 
544 		if (in6a == NULL) {
545 			if_put(ifp, &psref);
546 			curlwp_bindx(bound);
547 			if (error == 0)
548 				error = EADDRNOTAVAIL;
549 			return error;
550 		}
551 	}
552 
553 	if (ifp != NULL) {
554 		in6p->in6p_ip6.ip6_hlim = (u_int8_t)in6_selecthlim(in6p, ifp);
555 		if_put(ifp, &psref);
556 	} else
557 		in6p->in6p_ip6.ip6_hlim = (u_int8_t)in6_selecthlim_rt(in6p);
558 	curlwp_bindx(bound);
559 
560 	if (in6_pcblookup_connect(in6p->in6p_table, &sin6->sin6_addr,
561 	    sin6->sin6_port,
562 	    IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) ? in6a : &in6p->in6p_laddr,
563 				  in6p->in6p_lport, 0, &vestige)
564 		|| vestige.valid)
565 		return (EADDRINUSE);
566 	if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) ||
567 	    (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr) &&
568 	     in6p->in6p_laddr.s6_addr32[3] == 0))
569 	{
570 		if (in6p->in6p_lport == 0) {
571 			error = in6_pcbbind(in6p, NULL, l);
572 			if (error != 0)
573 				return error;
574 		}
575 		in6p->in6p_laddr = *in6a;
576 	}
577 	in6p->in6p_faddr = sin6->sin6_addr;
578 	in6p->in6p_fport = sin6->sin6_port;
579 
580         /* Late bind, if needed */
581 	if (in6p->in6p_bindportonsend) {
582                struct sockaddr_in6 lsin = *((const struct sockaddr_in6 *)
583 		    in6p->in6p_socket->so_proto->pr_domain->dom_sa_any);
584 		lsin.sin6_addr = in6p->in6p_laddr;
585 		lsin.sin6_port = 0;
586 
587                if ((error = in6_pcbbind_port(in6p, &lsin, l)) != 0)
588                        return error;
589 	}
590 
591 	in6_pcbstate(in6p, IN6P_CONNECTED);
592 	in6p->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
593 	if (ip6_auto_flowlabel)
594 		in6p->in6p_flowinfo |=
595 		    (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
596 #if defined(IPSEC)
597 	if (ipsec_enabled && in6p->in6p_socket->so_type == SOCK_STREAM)
598 		ipsec_pcbconn(in6p->in6p_sp);
599 #endif
600 	return (0);
601 }
602 
603 void
604 in6_pcbdisconnect(struct in6pcb *in6p)
605 {
606 	memset((void *)&in6p->in6p_faddr, 0, sizeof(in6p->in6p_faddr));
607 	in6p->in6p_fport = 0;
608 	in6_pcbstate(in6p, IN6P_BOUND);
609 	in6p->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
610 #if defined(IPSEC)
611 	if (ipsec_enabled)
612 		ipsec_pcbdisconn(in6p->in6p_sp);
613 #endif
614 	if (in6p->in6p_socket->so_state & SS_NOFDREF)
615 		in6_pcbdetach(in6p);
616 }
617 
618 void
619 in6_pcbdetach(struct in6pcb *in6p)
620 {
621 	struct socket *so = in6p->in6p_socket;
622 	int s;
623 
624 	if (in6p->in6p_af != AF_INET6)
625 		return;
626 
627 #if defined(IPSEC)
628 	if (ipsec_enabled)
629 		ipsec6_delete_pcbpolicy(in6p);
630 #endif
631 	so->so_pcb = NULL;
632 
633 	s = splsoftnet();
634 	in6_pcbstate(in6p, IN6P_ATTACHED);
635 	LIST_REMOVE(&in6p->in6p_head, inph_lhash);
636 	TAILQ_REMOVE(&in6p->in6p_table->inpt_queue, &in6p->in6p_head,
637 	    inph_queue);
638 	splx(s);
639 
640 	if (in6p->in6p_options) {
641 		m_freem(in6p->in6p_options);
642 	}
643 	if (in6p->in6p_outputopts != NULL) {
644 		ip6_clearpktopts(in6p->in6p_outputopts, -1);
645 		free(in6p->in6p_outputopts, M_IP6OPT);
646 	}
647 	rtcache_free(&in6p->in6p_route);
648 	ip6_freemoptions(in6p->in6p_moptions);
649 	ip_freemoptions(in6p->in6p_v4moptions);
650 	sofree(so);				/* drops the socket's lock */
651 
652 	pool_put(&in6pcb_pool, in6p);
653 	mutex_enter(softnet_lock);		/* reacquire it */
654 }
655 
656 void
657 in6_setsockaddr(struct in6pcb *in6p, struct sockaddr_in6 *sin6)
658 {
659 
660 	if (in6p->in6p_af != AF_INET6)
661 		return;
662 
663 	sockaddr_in6_init(sin6, &in6p->in6p_laddr, in6p->in6p_lport, 0, 0);
664 	(void)sa6_recoverscope(sin6); /* XXX: should catch errors */
665 }
666 
667 void
668 in6_setpeeraddr(struct in6pcb *in6p, struct sockaddr_in6 *sin6)
669 {
670 
671 	if (in6p->in6p_af != AF_INET6)
672 		return;
673 
674 	sockaddr_in6_init(sin6, &in6p->in6p_faddr, in6p->in6p_fport, 0, 0);
675 	(void)sa6_recoverscope(sin6); /* XXX: should catch errors */
676 }
677 
678 /*
679  * Pass some notification to all connections of a protocol
680  * associated with address dst.  The local address and/or port numbers
681  * may be specified to limit the search.  The "usual action" will be
682  * taken, depending on the ctlinput cmd.  The caller must filter any
683  * cmds that are uninteresting (e.g., no error in the map).
684  * Call the protocol specific routine (if any) to report
685  * any errors for each matching socket.
686  *
687  * Must be called at splsoftnet.
688  *
689  * Note: src (4th arg) carries the flowlabel value on the original IPv6
690  * header, in sin6_flowinfo member.
691  */
692 int
693 in6_pcbnotify(struct inpcbtable *table, const struct sockaddr *dst,
694     u_int fport_arg, const struct sockaddr *src, u_int lport_arg, int cmd,
695     void *cmdarg, void (*notify)(struct in6pcb *, int))
696 {
697 	struct inpcb_hdr *inph, *ninph;
698 	struct sockaddr_in6 sa6_src;
699 	const struct sockaddr_in6 *sa6_dst;
700 	u_int16_t fport = fport_arg, lport = lport_arg;
701 	int errno;
702 	int nmatch = 0;
703 	u_int32_t flowinfo;
704 
705 	if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
706 		return 0;
707 
708 	sa6_dst = (const struct sockaddr_in6 *)dst;
709 	if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
710 		return 0;
711 
712 	/*
713 	 * note that src can be NULL when we get notify by local fragmentation.
714 	 */
715 	sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
716 	flowinfo = sa6_src.sin6_flowinfo;
717 
718 	/*
719 	 * Redirects go to all references to the destination,
720 	 * and use in6_rtchange to invalidate the route cache.
721 	 * Dead host indications: also use in6_rtchange to invalidate
722 	 * the cache, and deliver the error to all the sockets.
723 	 * Otherwise, if we have knowledge of the local port and address,
724 	 * deliver only to that socket.
725 	 */
726 	if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
727 		fport = 0;
728 		lport = 0;
729 		memset((void *)&sa6_src.sin6_addr, 0, sizeof(sa6_src.sin6_addr));
730 
731 		if (cmd != PRC_HOSTDEAD)
732 			notify = in6_rtchange;
733 	}
734 
735 	errno = inet6ctlerrmap[cmd];
736 	TAILQ_FOREACH_SAFE(inph, &table->inpt_queue, inph_queue, ninph) {
737 		struct in6pcb *in6p = (struct in6pcb *)inph;
738 		struct rtentry *rt = NULL;
739 
740 		if (in6p->in6p_af != AF_INET6)
741 			continue;
742 
743 		/*
744 		 * Under the following condition, notify of redirects
745 		 * to the pcb, without making address matches against inpcb.
746 		 * - redirect notification is arrived.
747 		 * - the inpcb is unconnected.
748 		 * - the inpcb is caching !RTF_HOST routing entry.
749 		 * - the ICMPv6 notification is from the gateway cached in the
750 		 *   inpcb.  i.e. ICMPv6 notification is from nexthop gateway
751 		 *   the inpcb used very recently.
752 		 *
753 		 * This is to improve interaction between netbsd/openbsd
754 		 * redirect handling code, and inpcb route cache code.
755 		 * without the clause, !RTF_HOST routing entry (which carries
756 		 * gateway used by inpcb right before the ICMPv6 redirect)
757 		 * will be cached forever in unconnected inpcb.
758 		 *
759 		 * There still is a question regarding to what is TRT:
760 		 * - On bsdi/freebsd, RTF_HOST (cloned) routing entry will be
761 		 *   generated on packet output.  inpcb will always cache
762 		 *   RTF_HOST routing entry so there's no need for the clause
763 		 *   (ICMPv6 redirect will update RTF_HOST routing entry,
764 		 *   and inpcb is caching it already).
765 		 *   However, bsdi/freebsd are vulnerable to local DoS attacks
766 		 *   due to the cloned routing entries.
767 		 * - Specwise, "destination cache" is mentioned in RFC2461.
768 		 *   Jinmei says that it implies bsdi/freebsd behavior, itojun
769 		 *   is not really convinced.
770 		 * - Having hiwat/lowat on # of cloned host route (redirect/
771 		 *   pmtud) may be a good idea.  netbsd/openbsd has it.  see
772 		 *   icmp6_mtudisc_update().
773 		 */
774 		if ((PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) &&
775 		    IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
776 		    (rt = rtcache_validate(&in6p->in6p_route)) != NULL &&
777 		    !(rt->rt_flags & RTF_HOST)) {
778 			const struct sockaddr_in6 *dst6;
779 
780 			dst6 = (const struct sockaddr_in6 *)
781 			    rtcache_getdst(&in6p->in6p_route);
782 			if (dst6 == NULL)
783 				;
784 			else if (IN6_ARE_ADDR_EQUAL(&dst6->sin6_addr,
785 			    &sa6_dst->sin6_addr)) {
786 				rtcache_unref(rt, &in6p->in6p_route);
787 				goto do_notify;
788 			}
789 		}
790 		rtcache_unref(rt, &in6p->in6p_route);
791 
792 		/*
793 		 * If the error designates a new path MTU for a destination
794 		 * and the application (associated with this socket) wanted to
795 		 * know the value, notify. Note that we notify for all
796 		 * disconnected sockets if the corresponding application
797 		 * wanted. This is because some UDP applications keep sending
798 		 * sockets disconnected.
799 		 * XXX: should we avoid to notify the value to TCP sockets?
800 		 */
801 		if (cmd == PRC_MSGSIZE && (in6p->in6p_flags & IN6P_MTU) != 0 &&
802 		    (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) ||
803 		     IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &sa6_dst->sin6_addr))) {
804 			ip6_notify_pmtu(in6p, (const struct sockaddr_in6 *)dst,
805 					(u_int32_t *)cmdarg);
806 		}
807 
808 		/*
809 		 * Detect if we should notify the error. If no source and
810 		 * destination ports are specified, but non-zero flowinfo and
811 		 * local address match, notify the error. This is the case
812 		 * when the error is delivered with an encrypted buffer
813 		 * by ESP. Otherwise, just compare addresses and ports
814 		 * as usual.
815 		 */
816 		if (lport == 0 && fport == 0 && flowinfo &&
817 		    in6p->in6p_socket != NULL &&
818 		    flowinfo == (in6p->in6p_flowinfo & IPV6_FLOWLABEL_MASK) &&
819 		    IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &sa6_src.sin6_addr))
820 			goto do_notify;
821 		else if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
822 					     &sa6_dst->sin6_addr) ||
823 		    in6p->in6p_socket == NULL ||
824 		    (lport && in6p->in6p_lport != lport) ||
825 		    (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
826 		     !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
827 					 &sa6_src.sin6_addr)) ||
828 		    (fport && in6p->in6p_fport != fport))
829 			continue;
830 
831 	  do_notify:
832 		if (notify)
833 			(*notify)(in6p, errno);
834 		nmatch++;
835 	}
836 	return nmatch;
837 }
838 
839 void
840 in6_pcbpurgeif0(struct inpcbtable *table, struct ifnet *ifp)
841 {
842 	struct inpcb_hdr *inph, *ninph;
843 	struct ip6_moptions *im6o;
844 	struct in6_multi_mship *imm, *nimm;
845 
846 	KASSERT(ifp != NULL);
847 
848 	TAILQ_FOREACH_SAFE(inph, &table->inpt_queue, inph_queue, ninph) {
849 		struct in6pcb *in6p = (struct in6pcb *)inph;
850 		bool need_unlock = false;
851 		if (in6p->in6p_af != AF_INET6)
852 			continue;
853 
854 		/* The caller holds either one of in6ps' lock */
855 		if (!in6p_locked(in6p)) {
856 			in6p_lock(in6p);
857 			need_unlock = true;
858 		}
859 		im6o = in6p->in6p_moptions;
860 		if (im6o) {
861 			/*
862 			 * Unselect the outgoing interface if it is being
863 			 * detached.
864 			 */
865 			if (im6o->im6o_multicast_if_index == ifp->if_index)
866 				im6o->im6o_multicast_if_index = 0;
867 
868 			/*
869 			 * Drop multicast group membership if we joined
870 			 * through the interface being detached.
871 			 * XXX controversial - is it really legal for kernel
872 			 * to force this?
873 			 */
874 			LIST_FOREACH_SAFE(imm, &im6o->im6o_memberships,
875 			    i6mm_chain, nimm) {
876 				if (imm->i6mm_maddr->in6m_ifp == ifp) {
877 					LIST_REMOVE(imm, i6mm_chain);
878 					in6_leavegroup(imm);
879 				}
880 			}
881 		}
882 		in_purgeifmcast(in6p->in6p_v4moptions, ifp);
883 		if (need_unlock)
884 			in6p_unlock(in6p);
885 	}
886 }
887 
888 void
889 in6_pcbpurgeif(struct inpcbtable *table, struct ifnet *ifp)
890 {
891 	struct rtentry *rt;
892 	struct inpcb_hdr *inph, *ninph;
893 
894 	TAILQ_FOREACH_SAFE(inph, &table->inpt_queue, inph_queue, ninph) {
895 		struct in6pcb *in6p = (struct in6pcb *)inph;
896 		if (in6p->in6p_af != AF_INET6)
897 			continue;
898 		if ((rt = rtcache_validate(&in6p->in6p_route)) != NULL &&
899 		    rt->rt_ifp == ifp) {
900 			rtcache_unref(rt, &in6p->in6p_route);
901 			in6_rtchange(in6p, 0);
902 		} else
903 			rtcache_unref(rt, &in6p->in6p_route);
904 	}
905 }
906 
907 /*
908  * Check for alternatives when higher level complains
909  * about service problems.  For now, invalidate cached
910  * routing information.  If the route was created dynamically
911  * (by a redirect), time to try a default gateway again.
912  */
913 void
914 in6_losing(struct in6pcb *in6p)
915 {
916 	struct rtentry *rt;
917 	struct rt_addrinfo info;
918 
919 	if (in6p->in6p_af != AF_INET6)
920 		return;
921 
922 	if ((rt = rtcache_validate(&in6p->in6p_route)) == NULL)
923 		return;
924 
925 	memset(&info, 0, sizeof(info));
926 	info.rti_info[RTAX_DST] = rtcache_getdst(&in6p->in6p_route);
927 	info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
928 	info.rti_info[RTAX_NETMASK] = rt_mask(rt);
929 	rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
930 	if (rt->rt_flags & RTF_DYNAMIC) {
931 		int error;
932 		struct rtentry *nrt;
933 
934 		error = rtrequest(RTM_DELETE, rt_getkey(rt),
935 		    rt->rt_gateway, rt_mask(rt), rt->rt_flags, &nrt);
936 		rtcache_unref(rt, &in6p->in6p_route);
937 		if (error == 0)
938 			rt_free(nrt);
939 	} else
940 		rtcache_unref(rt, &in6p->in6p_route);
941 	/*
942 	 * A new route can be allocated
943 	 * the next time output is attempted.
944 	 */
945 	rtcache_free(&in6p->in6p_route);
946 }
947 
948 /*
949  * After a routing change, flush old routing.  A new route can be
950  * allocated the next time output is attempted.
951  */
952 void
953 in6_rtchange(struct in6pcb *in6p, int errno)
954 {
955 	if (in6p->in6p_af != AF_INET6)
956 		return;
957 
958 	rtcache_free(&in6p->in6p_route);
959 	/*
960 	 * A new route can be allocated the next time
961 	 * output is attempted.
962 	 */
963 }
964 
965 struct in6pcb *
966 in6_pcblookup_port(struct inpcbtable *table, struct in6_addr *laddr6,
967 		   u_int lport_arg, int lookup_wildcard, struct vestigial_inpcb *vp)
968 {
969 	struct inpcbhead *head;
970 	struct inpcb_hdr *inph;
971 	struct in6pcb *in6p, *match = NULL;
972 	int matchwild = 3, wildcard;
973 	u_int16_t lport = lport_arg;
974 
975 	if (vp)
976 		vp->valid = 0;
977 
978 	head = IN6PCBHASH_PORT(table, lport);
979 	LIST_FOREACH(inph, head, inph_lhash) {
980 		in6p = (struct in6pcb *)inph;
981 		if (in6p->in6p_af != AF_INET6)
982 			continue;
983 
984 		if (in6p->in6p_lport != lport)
985 			continue;
986 		wildcard = 0;
987 		if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) {
988 			if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
989 				continue;
990 		}
991 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr))
992 			wildcard++;
993 		if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)) {
994 			if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
995 				continue;
996 			if (!IN6_IS_ADDR_V4MAPPED(laddr6))
997 				continue;
998 
999 			/* duplicate of IPv4 logic */
1000 			wildcard = 0;
1001 			if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr) &&
1002 			    in6p->in6p_faddr.s6_addr32[3])
1003 				wildcard++;
1004 			if (!in6p->in6p_laddr.s6_addr32[3]) {
1005 				if (laddr6->s6_addr32[3])
1006 					wildcard++;
1007 			} else {
1008 				if (!laddr6->s6_addr32[3])
1009 					wildcard++;
1010 				else {
1011 					if (in6p->in6p_laddr.s6_addr32[3] !=
1012 					    laddr6->s6_addr32[3])
1013 						continue;
1014 				}
1015 			}
1016 		} else if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1017 			if (IN6_IS_ADDR_V4MAPPED(laddr6)) {
1018 				if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
1019 					continue;
1020 			}
1021 			if (!IN6_IS_ADDR_UNSPECIFIED(laddr6))
1022 				wildcard++;
1023 		} else {
1024 			if (IN6_IS_ADDR_V4MAPPED(laddr6)) {
1025 				if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
1026 					continue;
1027 			}
1028 			if (IN6_IS_ADDR_UNSPECIFIED(laddr6))
1029 				wildcard++;
1030 			else {
1031 				if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
1032 				    laddr6))
1033 					continue;
1034 			}
1035 		}
1036 		if (wildcard && !lookup_wildcard)
1037 			continue;
1038 		if (wildcard < matchwild) {
1039 			match = in6p;
1040 			matchwild = wildcard;
1041 			if (matchwild == 0)
1042 				break;
1043 		}
1044 	}
1045 	if (match && matchwild == 0)
1046 		return match;
1047 
1048 	if (vp && table->vestige && table->vestige->init_ports6) {
1049 		struct vestigial_inpcb better;
1050 		void *state;
1051 
1052 		state = (*table->vestige->init_ports6)(laddr6,
1053 						       lport_arg,
1054 						       lookup_wildcard);
1055 		while (table->vestige
1056 		       && (*table->vestige->next_port6)(state, vp)) {
1057 
1058 			if (vp->lport != lport)
1059 				continue;
1060 			wildcard = 0;
1061 			if (!IN6_IS_ADDR_UNSPECIFIED(&vp->faddr.v6))
1062 				wildcard++;
1063 			if (IN6_IS_ADDR_UNSPECIFIED(&vp->laddr.v6)) {
1064 				if (!IN6_IS_ADDR_UNSPECIFIED(laddr6))
1065 					wildcard++;
1066 			} else {
1067 				if (IN6_IS_ADDR_V4MAPPED(laddr6)) {
1068 					if (vp->v6only)
1069 						continue;
1070 				}
1071 				if (IN6_IS_ADDR_UNSPECIFIED(laddr6))
1072 					wildcard++;
1073 				else {
1074 					if (!IN6_ARE_ADDR_EQUAL(&vp->laddr.v6, laddr6))
1075 						continue;
1076 				}
1077 			}
1078 			if (wildcard && !lookup_wildcard)
1079 				continue;
1080 			if (wildcard < matchwild) {
1081 				better = *vp;
1082 				match  = (void*)&better;
1083 
1084 				matchwild = wildcard;
1085 				if (matchwild == 0)
1086 					break;
1087 			}
1088 		}
1089 
1090 		if (match) {
1091 			if (match != (void*)&better)
1092 				return match;
1093 			else {
1094 				*vp = better;
1095 				return 0;
1096 			}
1097 		}
1098 	}
1099 	return (match);
1100 }
1101 
1102 /*
1103  * WARNING: return value (rtentry) could be IPv4 one if in6pcb is connected to
1104  * IPv4 mapped address.
1105  */
1106 struct rtentry *
1107 in6_pcbrtentry(struct in6pcb *in6p)
1108 {
1109 	struct rtentry *rt;
1110 	struct route *ro;
1111 	union {
1112 		const struct sockaddr *sa;
1113 		const struct sockaddr_in6 *sa6;
1114 #ifdef INET
1115 		const struct sockaddr_in *sa4;
1116 #endif
1117 	} cdst;
1118 
1119 	ro = &in6p->in6p_route;
1120 
1121 	if (in6p->in6p_af != AF_INET6)
1122 		return (NULL);
1123 
1124 	cdst.sa = rtcache_getdst(ro);
1125 	if (cdst.sa == NULL)
1126 		;
1127 #ifdef INET
1128 	else if (cdst.sa->sa_family == AF_INET) {
1129 		KASSERT(IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr));
1130 		if (cdst.sa4->sin_addr.s_addr != in6p->in6p_faddr.s6_addr32[3])
1131 			rtcache_free(ro);
1132 	}
1133 #endif
1134 	else {
1135 		if (!IN6_ARE_ADDR_EQUAL(&cdst.sa6->sin6_addr,
1136 					&in6p->in6p_faddr))
1137 			rtcache_free(ro);
1138 	}
1139 	if ((rt = rtcache_validate(ro)) == NULL)
1140 		rt = rtcache_update(ro, 1);
1141 #ifdef INET
1142 	if (rt == NULL && IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) {
1143 		union {
1144 			struct sockaddr		dst;
1145 			struct sockaddr_in	dst4;
1146 		} u;
1147 		struct in_addr addr;
1148 
1149 		addr.s_addr = in6p->in6p_faddr.s6_addr32[3];
1150 
1151 		sockaddr_in_init(&u.dst4, &addr, 0);
1152 		if (rtcache_setdst(ro, &u.dst) != 0)
1153 			return NULL;
1154 
1155 		rt = rtcache_init(ro);
1156 	} else
1157 #endif
1158 	if (rt == NULL && !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
1159 		union {
1160 			struct sockaddr		dst;
1161 			struct sockaddr_in6	dst6;
1162 		} u;
1163 
1164 		sockaddr_in6_init(&u.dst6, &in6p->in6p_faddr, 0, 0, 0);
1165 		if (rtcache_setdst(ro, &u.dst) != 0)
1166 			return NULL;
1167 
1168 		rt = rtcache_init(ro);
1169 	}
1170 	return rt;
1171 }
1172 
1173 void
1174 in6_pcbrtentry_unref(struct rtentry *rt, struct in6pcb *in6p)
1175 {
1176 
1177 	rtcache_unref(rt, &in6p->in6p_route);
1178 }
1179 
1180 struct in6pcb *
1181 in6_pcblookup_connect(struct inpcbtable *table, const struct in6_addr *faddr6,
1182 		      u_int fport_arg, const struct in6_addr *laddr6, u_int lport_arg,
1183 		      int faith,
1184 		      struct vestigial_inpcb *vp)
1185 {
1186 	struct inpcbhead *head;
1187 	struct inpcb_hdr *inph;
1188 	struct in6pcb *in6p;
1189 	u_int16_t fport = fport_arg, lport = lport_arg;
1190 
1191 	if (vp)
1192 		vp->valid = 0;
1193 
1194 	head = IN6PCBHASH_CONNECT(table, faddr6, fport, laddr6, lport);
1195 	LIST_FOREACH(inph, head, inph_hash) {
1196 		in6p = (struct in6pcb *)inph;
1197 		if (in6p->in6p_af != AF_INET6)
1198 			continue;
1199 
1200 		/* find exact match on both source and dest */
1201 		if (in6p->in6p_fport != fport)
1202 			continue;
1203 		if (in6p->in6p_lport != lport)
1204 			continue;
1205 		if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr))
1206 			continue;
1207 		if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, faddr6))
1208 			continue;
1209 		if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
1210 			continue;
1211 		if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6))
1212 			continue;
1213 		if ((IN6_IS_ADDR_V4MAPPED(laddr6) ||
1214 		     IN6_IS_ADDR_V4MAPPED(faddr6)) &&
1215 		    (in6p->in6p_flags & IN6P_IPV6_V6ONLY))
1216 			continue;
1217 		return in6p;
1218 	}
1219 	if (vp && table->vestige) {
1220 		if ((*table->vestige->lookup6)(faddr6, fport_arg,
1221 					       laddr6, lport_arg, vp))
1222 			return NULL;
1223 	}
1224 
1225 	return NULL;
1226 }
1227 
1228 struct in6pcb *
1229 in6_pcblookup_bind(struct inpcbtable *table, const struct in6_addr *laddr6,
1230 	u_int lport_arg, int faith)
1231 {
1232 	struct inpcbhead *head;
1233 	struct inpcb_hdr *inph;
1234 	struct in6pcb *in6p;
1235 	u_int16_t lport = lport_arg;
1236 #ifdef INET
1237 	struct in6_addr zero_mapped;
1238 #endif
1239 
1240 	head = IN6PCBHASH_BIND(table, laddr6, lport);
1241 	LIST_FOREACH(inph, head, inph_hash) {
1242 		in6p = (struct in6pcb *)inph;
1243 		if (in6p->in6p_af != AF_INET6)
1244 			continue;
1245 
1246 		if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0)
1247 			continue;
1248 		if (in6p->in6p_fport != 0)
1249 			continue;
1250 		if (in6p->in6p_lport != lport)
1251 			continue;
1252 		if (IN6_IS_ADDR_V4MAPPED(laddr6) &&
1253 		    (in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
1254 			continue;
1255 		if (IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6))
1256 			goto out;
1257 	}
1258 #ifdef INET
1259 	if (IN6_IS_ADDR_V4MAPPED(laddr6)) {
1260 		memset(&zero_mapped, 0, sizeof(zero_mapped));
1261 		zero_mapped.s6_addr16[5] = 0xffff;
1262 		head = IN6PCBHASH_BIND(table, &zero_mapped, lport);
1263 		LIST_FOREACH(inph, head, inph_hash) {
1264 			in6p = (struct in6pcb *)inph;
1265 			if (in6p->in6p_af != AF_INET6)
1266 				continue;
1267 
1268 			if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0)
1269 				continue;
1270 			if (in6p->in6p_fport != 0)
1271 				continue;
1272 			if (in6p->in6p_lport != lport)
1273 				continue;
1274 			if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
1275 				continue;
1276 			if (IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &zero_mapped))
1277 				goto out;
1278 		}
1279 	}
1280 #endif
1281 	head = IN6PCBHASH_BIND(table, &zeroin6_addr, lport);
1282 	LIST_FOREACH(inph, head, inph_hash) {
1283 		in6p = (struct in6pcb *)inph;
1284 		if (in6p->in6p_af != AF_INET6)
1285 			continue;
1286 
1287 		if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0)
1288 			continue;
1289 		if (in6p->in6p_fport != 0)
1290 			continue;
1291 		if (in6p->in6p_lport != lport)
1292 			continue;
1293 		if (IN6_IS_ADDR_V4MAPPED(laddr6) &&
1294 		    (in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
1295 			continue;
1296 		if (IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &zeroin6_addr))
1297 			goto out;
1298 	}
1299 	return (NULL);
1300 
1301 out:
1302 	inph = &in6p->in6p_head;
1303 	if (inph != LIST_FIRST(head)) {
1304 		LIST_REMOVE(inph, inph_hash);
1305 		LIST_INSERT_HEAD(head, inph, inph_hash);
1306 	}
1307 	return in6p;
1308 }
1309 
1310 void
1311 in6_pcbstate(struct in6pcb *in6p, int state)
1312 {
1313 
1314 	if (in6p->in6p_af != AF_INET6)
1315 		return;
1316 
1317 	if (in6p->in6p_state > IN6P_ATTACHED)
1318 		LIST_REMOVE(&in6p->in6p_head, inph_hash);
1319 
1320 	switch (state) {
1321 	case IN6P_BOUND:
1322 		LIST_INSERT_HEAD(IN6PCBHASH_BIND(in6p->in6p_table,
1323 		    &in6p->in6p_laddr, in6p->in6p_lport), &in6p->in6p_head,
1324 		    inph_hash);
1325 		break;
1326 	case IN6P_CONNECTED:
1327 		LIST_INSERT_HEAD(IN6PCBHASH_CONNECT(in6p->in6p_table,
1328 		    &in6p->in6p_faddr, in6p->in6p_fport,
1329 		    &in6p->in6p_laddr, in6p->in6p_lport), &in6p->in6p_head,
1330 		    inph_hash);
1331 		break;
1332 	}
1333 
1334 	in6p->in6p_state = state;
1335 }
1336