xref: /dragonfly/sys/netinet6/in6_pcb.c (revision 2a617999)
1 /*	$FreeBSD: src/sys/netinet6/in6_pcb.c,v 1.10.2.9 2003/01/24 05:11:35 sam Exp $	*/
2 /*	$KAME: in6_pcb.c,v 1.31 2001/05/21 05:45:10 jinmei 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 /*
35  * Copyright (c) 1982, 1986, 1991, 1993
36  *	The Regents of the University of California.  All rights reserved.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  * 1. Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  * 2. Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in the
45  *    documentation and/or other materials provided with the distribution.
46  * 3. Neither the name of the University nor the names of its contributors
47  *    may be used to endorse or promote products derived from this software
48  *    without specific prior written permission.
49  *
50  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60  * SUCH DAMAGE.
61  *
62  *	@(#)in_pcb.c	8.2 (Berkeley) 1/4/94
63  */
64 
65 #include "opt_inet.h"
66 #include "opt_inet6.h"
67 #include "opt_ipsec.h"
68 
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/malloc.h>
72 #include <sys/mbuf.h>
73 #include <sys/domain.h>
74 #include <sys/protosw.h>
75 #include <sys/socket.h>
76 #include <sys/socketvar.h>
77 #include <sys/sockio.h>
78 #include <sys/errno.h>
79 #include <sys/time.h>
80 #include <sys/proc.h>
81 #include <sys/priv.h>
82 #include <sys/jail.h>
83 
84 #include <sys/thread2.h>
85 #include <sys/msgport2.h>
86 
87 #include <vm/vm_zone.h>
88 
89 #include <net/if.h>
90 #include <net/if_types.h>
91 #include <net/route.h>
92 #include <net/netisr2.h>
93 
94 #include <netinet/in.h>
95 #include <netinet/in_var.h>
96 #include <netinet/in_systm.h>
97 #include <netinet/ip6.h>
98 #include <netinet/ip_var.h>
99 #include <netinet6/ip6_var.h>
100 #include <netinet6/nd6.h>
101 #include <netinet/in_pcb.h>
102 #include <netinet6/in6_pcb.h>
103 
104 #ifdef IPSEC
105 #include <netinet6/ipsec.h>
106 #ifdef INET6
107 #include <netinet6/ipsec6.h>
108 #endif
109 #include <netinet6/ah.h>
110 #ifdef INET6
111 #include <netinet6/ah6.h>
112 #endif
113 #include <netproto/key/key.h>
114 #endif /* IPSEC */
115 
116 #ifdef FAST_IPSEC
117 #include <netproto/ipsec/ipsec.h>
118 #include <netproto/ipsec/ipsec6.h>
119 #include <netproto/ipsec/key.h>
120 #define	IPSEC
121 #endif /* FAST_IPSEC */
122 
123 struct	in6_addr zeroin6_addr;
124 
125 int
126 in6_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
127 {
128 	struct socket *so = inp->inp_socket;
129 	struct sockaddr_in6 jsin6;
130 	int error;
131 
132 	if (!in6_ifaddr) /* XXX broken! */
133 		return (EADDRNOTAVAIL);
134 	if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
135 		return (EINVAL);
136 
137 	if (nam) {
138 		struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
139 		struct inpcbinfo *pcbinfo;
140 		struct inpcbportinfo *portinfo;
141 		int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
142 		struct ucred *cred = NULL;
143 		struct inpcb *t;
144 		u_short	lport, lport_ho;
145 
146 		if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
147 			wild = 1;
148 		if (td->td_proc != NULL)
149 			cred = td->td_proc->p_ucred;
150 
151 		if (nam->sa_len != sizeof(*sin6))
152 			return (EINVAL);
153 		/*
154 		 * family check.
155 		 */
156 		if (nam->sa_family != AF_INET6)
157 			return (EAFNOSUPPORT);
158 
159 		if (!prison_replace_wildcards(td, nam))
160 			return (EINVAL);
161 
162 		/* KAME hack: embed scopeid */
163 		if (in6_embedscope(&sin6->sin6_addr, sin6, inp, NULL) != 0)
164 			return (EINVAL);
165 		/* this must be cleared for ifa_ifwithaddr() */
166 		sin6->sin6_scope_id = 0;
167 
168 		lport = sin6->sin6_port;
169 		if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
170 			/*
171 			 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
172 			 * allow compepte duplication of binding if
173 			 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
174 			 * and a multicast address is bound on both
175 			 * new and duplicated sockets.
176 			 */
177 			if (so->so_options & SO_REUSEADDR)
178 				reuseport = SO_REUSEADDR|SO_REUSEPORT;
179 		} else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
180 			struct ifaddr *ia = NULL;
181 
182 			sin6->sin6_port = 0;		/* yech... */
183 			if (!prison_replace_wildcards(td, (struct sockaddr *)sin6)) {
184 				sin6->sin6_addr = kin6addr_any;
185 				return (EINVAL);
186 			}
187 			if ((ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == NULL)
188 				return (EADDRNOTAVAIL);
189 
190 			/*
191 			 * XXX: bind to an anycast address might accidentally
192 			 * cause sending a packet with anycast source address.
193 			 * We should allow to bind to a deprecated address, since
194 			 * the application dare to use it.
195 			 */
196 			if (ia &&
197 			    ((struct in6_ifaddr *)ia)->ia6_flags &
198 			    (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED))
199 				return (EADDRNOTAVAIL);
200 		}
201 
202 		inp->in6p_laddr = sin6->sin6_addr;
203 
204 		if (lport == 0)
205 			goto auto_select;
206 		lport_ho = ntohs(lport);
207 
208 		/* GROSS */
209 		if (lport_ho < IPV6PORT_RESERVED && cred &&
210 		    priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0)) {
211 			inp->in6p_laddr = kin6addr_any;
212 			return (EACCES);
213 		}
214 
215 		/*
216 		 * Locate the proper portinfo based on lport
217 		 */
218 		pcbinfo = inp->inp_pcbinfo;
219 		portinfo =
220 		    &pcbinfo->portinfo[lport_ho & pcbinfo->portinfo_mask];
221 		KKASSERT((lport_ho & pcbinfo->portinfo_mask) ==
222 		    portinfo->offset);
223 
224 		/*
225 		 * This has to be atomic.  If the porthash is shared across
226 		 * multiple protocol threads (aka tcp) then the token must
227 		 * be held.
228 		 */
229 		GET_PORT_TOKEN(portinfo);
230 
231 		if (so->so_cred->cr_uid != 0 &&
232 		    !IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
233 			t = in6_pcblookup_local(portinfo,
234 			    &sin6->sin6_addr, lport, INPLOOKUP_WILDCARD, cred);
235 			if (t &&
236 			    (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
237 			     !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
238 			     (t->inp_socket->so_options & SO_REUSEPORT) == 0) &&
239 			    (so->so_cred->cr_uid !=
240 			     t->inp_socket->so_cred->cr_uid)) {
241 				inp->in6p_laddr = kin6addr_any;
242 				error = EADDRINUSE;
243 				goto done;
244 			}
245 			if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
246 			    IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
247 				struct sockaddr_in sin;
248 
249 				in6_sin6_2_sin(&sin, sin6);
250 				t = in_pcblookup_local(portinfo,
251 				    sin.sin_addr, lport,
252 				    INPLOOKUP_WILDCARD, cred);
253 				if (t &&
254 				    (so->so_cred->cr_uid !=
255 				     t->inp_socket->so_cred->cr_uid) &&
256 				    (ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
257 				     INP_SOCKAF(so) ==
258 				     INP_SOCKAF(t->inp_socket))) {
259 					inp->in6p_laddr = kin6addr_any;
260 					error = EADDRINUSE;
261 					goto done;
262 				}
263 			}
264 		}
265 		if (cred && cred->cr_prison &&
266 		    !prison_replace_wildcards(td, nam)) {
267 			inp->in6p_laddr = kin6addr_any;
268 			error = EADDRNOTAVAIL;
269 			goto done;
270 		}
271 		t = in6_pcblookup_local(portinfo, &sin6->sin6_addr, lport,
272 		    wild, cred);
273 		if (t && (reuseport & t->inp_socket->so_options) == 0) {
274 			inp->in6p_laddr = kin6addr_any;
275 			error = EADDRINUSE;
276 			goto done;
277 		}
278 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
279 		    IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
280 			struct sockaddr_in sin;
281 
282 			in6_sin6_2_sin(&sin, sin6);
283 			t = in_pcblookup_local(portinfo, sin.sin_addr, lport,
284 			    wild, cred);
285 			if (t && (reuseport & t->inp_socket->so_options) == 0 &&
286 			    (ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
287 			     INP_SOCKAF(so) == INP_SOCKAF(t->inp_socket))) {
288 				inp->in6p_laddr = kin6addr_any;
289 				error = EADDRINUSE;
290 				goto done;
291 			}
292 		}
293 
294 		inp->inp_lport = lport;
295 		in_pcbinsporthash(portinfo, inp);
296 		error = 0;
297 done:
298 		REL_PORT_TOKEN(portinfo);
299 		return (error);
300 	} else {
301 auto_select:
302 		jsin6.sin6_addr = inp->in6p_laddr;
303 		jsin6.sin6_family = AF_INET6;
304 		if (!prison_replace_wildcards(td, (struct sockaddr*)&jsin6)) {
305 			inp->in6p_laddr = kin6addr_any;
306 			inp->inp_lport = 0;
307 			return (EINVAL);
308 		}
309 
310 		return in6_pcbsetlport(&inp->in6p_laddr, inp, td);
311 	}
312 }
313 
314 /*
315  *   Transform old in6_pcbconnect() into an inner subroutine for new
316  *   in6_pcbconnect(): Do some validity-checking on the remote
317  *   address (in mbuf 'nam') and then determine local host address
318  *   (i.e., which interface) to use to access that remote host.
319  *
320  *   This preserves definition of in6_pcbconnect(), while supporting a
321  *   slightly different version for T/TCP.  (This is more than
322  *   a bit of a kludge, but cleaning up the internal interfaces would
323  *   have forced minor changes in every protocol).
324  */
325 
326 int
327 in6_pcbladdr(struct inpcb *inp, struct sockaddr *nam,
328 	     struct in6_addr **plocal_addr6, struct thread *td)
329 {
330 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
331 	struct ifnet *ifp = NULL;
332 	int error = 0;
333 
334 	if (nam->sa_len != sizeof (*sin6))
335 		return (EINVAL);
336 	if (sin6->sin6_family != AF_INET6)
337 		return (EAFNOSUPPORT);
338 	if (sin6->sin6_port == 0)
339 		return (EADDRNOTAVAIL);
340 
341 	/* KAME hack: embed scopeid */
342 	if (in6_embedscope(&sin6->sin6_addr, sin6, inp, &ifp) != 0)
343 		return EINVAL;
344 
345 	if (in6_ifaddr) {
346 		/*
347 		 * If the destination address is UNSPECIFIED addr,
348 		 * use the loopback addr, e.g ::1.
349 		 */
350 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
351 			sin6->sin6_addr = kin6addr_loopback;
352 	}
353 	{
354 		/*
355 		 * XXX: in6_selectsrc might replace the bound local address
356 		 * with the address specified by setsockopt(IPV6_PKTINFO).
357 		 * Is it the intended behavior?
358 		 */
359 		*plocal_addr6 = in6_selectsrc(sin6, inp->in6p_outputopts,
360 					      inp->in6p_moptions,
361 					      &inp->in6p_route,
362 					      &inp->in6p_laddr, &error, td);
363 		if (*plocal_addr6 == NULL) {
364 			if (error == 0)
365 				error = EADDRNOTAVAIL;
366 			return (error);
367 		}
368 		/*
369 		 * Don't do pcblookup call here; return interface in
370 		 * plocal_addr6
371 		 * and exit to caller, that will do the lookup.
372 		 */
373 	}
374 
375 	if (inp->in6p_route.ro_rt)
376 		ifp = inp->in6p_route.ro_rt->rt_ifp;
377 
378 	return (0);
379 }
380 
381 /*
382  * Outer subroutine:
383  * Connect from a socket to a specified address.
384  * Both address and port must be specified in argument sin.
385  * If don't have a local address for this socket yet,
386  * then pick one.
387  */
388 int
389 in6_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
390 {
391 	struct in6_addr *addr6;
392 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
393 	int error;
394 
395 	/*
396 	 * Call inner routine, to assign local interface address.
397 	 * in6_pcbladdr() may automatically fill in sin6_scope_id.
398 	 */
399 	if ((error = in6_pcbladdr(inp, nam, &addr6, td)) != 0)
400 		return (error);
401 
402 	if (in6_pcblookup_hash(inp->inp_pcbinfo, &sin6->sin6_addr,
403 			       sin6->sin6_port,
404 			      IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
405 			      ? addr6 : &inp->in6p_laddr,
406 			      inp->inp_lport, 0, NULL) != NULL) {
407 		return (EADDRINUSE);
408 	}
409 	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
410 		if (inp->inp_lport == 0) {
411 			error = in6_pcbbind(inp, NULL, td);
412 			if (error)
413 				return (error);
414 		}
415 		inp->in6p_laddr = *addr6;
416 	}
417 	inp->in6p_faddr = sin6->sin6_addr;
418 	inp->inp_fport = sin6->sin6_port;
419 	/* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
420 	inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
421 	if (inp->in6p_flags & IN6P_AUTOFLOWLABEL)
422 		inp->in6p_flowinfo |=
423 		    (htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK);
424 
425 	in_pcbinsconnhash(inp);
426 	return (0);
427 }
428 
429 #if 0
430 /*
431  * Return an IPv6 address, which is the most appropriate for given
432  * destination and user specified options.
433  * If necessary, this function lookups the routing table and return
434  * an entry to the caller for later use.
435  */
436 struct in6_addr *
437 in6_selectsrc(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts,
438 	      struct ip6_moptions *mopts, struct route_in6 *ro,
439 	      struct in6_addr *laddr, int *errorp, struct thread *td)
440 {
441 	struct sockaddr_in6 jsin6;
442 	struct ucred *cred = NULL;
443 	struct in6_addr *dst;
444 	struct in6_ifaddr *ia6 = 0;
445 	struct in6_pktinfo *pi = NULL;
446 	int jailed = 0;
447 
448 	if (td && td->td_proc && td->td_proc->p_ucred)
449 		cred = td->td_proc->p_ucred;
450 	if (cred && cred->cr_prison)
451 		jailed = 1;
452 	jsin6.sin6_family = AF_INET6;
453 	dst = &dstsock->sin6_addr;
454 	*errorp = 0;
455 
456 	/*
457 	 * If the source address is explicitly specified by the caller,
458 	 * use it.
459 	 */
460 	if (opts && (pi = opts->ip6po_pktinfo) &&
461 	    !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr)) {
462 		jsin6.sin6_addr = pi->ipi6_addr;
463 		if (jailed && !jailed_ip(cred->cr_prison,
464 		    (struct sockaddr *)&jsin6)) {
465 			return(0);
466 		} else {
467 			return (&pi->ipi6_addr);
468 		}
469 	}
470 
471 	/*
472 	 * If the source address is not specified but the socket(if any)
473 	 * is already bound, use the bound address.
474 	 */
475 	if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr)) {
476 		jsin6.sin6_addr = *laddr;
477 		if (jailed && !jailed_ip(cred->cr_prison,
478 		    (struct sockaddr *)&jsin6)) {
479 			return(0);
480 		} else {
481 			return (laddr);
482 		}
483 	}
484 
485 	/*
486 	 * If the caller doesn't specify the source address but
487 	 * the outgoing interface, use an address associated with
488 	 * the interface.
489 	 */
490 	if (pi && pi->ipi6_ifindex) {
491 		/* XXX boundary check is assumed to be already done. */
492 		ia6 = in6_ifawithscope(ifindex2ifnet[pi->ipi6_ifindex],
493 				       dst, cred);
494 
495 		if (ia6 && jailed) {
496 			jsin6.sin6_addr = (&ia6->ia_addr)->sin6_addr;
497 			if (!jailed_ip(cred->cr_prison,
498 				(struct sockaddr *)&jsin6))
499 				ia6 = 0;
500 		}
501 
502 		if (ia6 == 0) {
503 			*errorp = EADDRNOTAVAIL;
504 			return (0);
505 		}
506 		return (&satosin6(&ia6->ia_addr)->sin6_addr);
507 	}
508 
509 	/*
510 	 * If the destination address is a link-local unicast address or
511 	 * a multicast address, and if the outgoing interface is specified
512 	 * by the sin6_scope_id filed, use an address associated with the
513 	 * interface.
514 	 * XXX: We're now trying to define more specific semantics of
515 	 *      sin6_scope_id field, so this part will be rewritten in
516 	 *      the near future.
517 	 */
518 	if ((IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MULTICAST(dst)) &&
519 	    dstsock->sin6_scope_id) {
520 		/*
521 		 * I'm not sure if boundary check for scope_id is done
522 		 * somewhere...
523 		 */
524 		if (dstsock->sin6_scope_id < 0 ||
525 		    if_index < dstsock->sin6_scope_id) {
526 			*errorp = ENXIO; /* XXX: better error? */
527 			return (0);
528 		}
529 		ia6 = in6_ifawithscope(ifindex2ifnet[dstsock->sin6_scope_id],
530 				       dst, cred);
531 
532 		if (ia6 && jailed) {
533 			jsin6.sin6_addr = (&ia6->ia_addr)->sin6_addr;
534 			if (!jailed_ip(cred->cr_prison,
535 				(struct sockaddr *)&jsin6))
536 				ia6 = 0;
537 		}
538 
539 		if (ia6 == 0) {
540 			*errorp = EADDRNOTAVAIL;
541 			return (0);
542 		}
543 		return (&satosin6(&ia6->ia_addr)->sin6_addr);
544 	}
545 
546 	/*
547 	 * If the destination address is a multicast address and
548 	 * the outgoing interface for the address is specified
549 	 * by the caller, use an address associated with the interface.
550 	 * There is a sanity check here; if the destination has node-local
551 	 * scope, the outgoing interfacde should be a loopback address.
552 	 * Even if the outgoing interface is not specified, we also
553 	 * choose a loopback interface as the outgoing interface.
554 	 */
555 	if (!jailed && IN6_IS_ADDR_MULTICAST(dst)) {
556 		struct ifnet *ifp = mopts ? mopts->im6o_multicast_ifp : NULL;
557 
558 		if (ifp == NULL && IN6_IS_ADDR_MC_NODELOCAL(dst)) {
559 			ifp = &loif[0];
560 		}
561 
562 		if (ifp) {
563 			ia6 = in6_ifawithscope(ifp, dst, cred);
564 			if (ia6 == 0) {
565 				*errorp = EADDRNOTAVAIL;
566 				return (0);
567 			}
568 			return (&ia6->ia_addr.sin6_addr);
569 		}
570 	}
571 
572 	/*
573 	 * If the next hop address for the packet is specified
574 	 * by caller, use an address associated with the route
575 	 * to the next hop.
576 	 */
577 	{
578 		struct sockaddr_in6 *sin6_next;
579 		struct rtentry *rt;
580 
581 		if (opts && opts->ip6po_nexthop) {
582 			sin6_next = satosin6(opts->ip6po_nexthop);
583 			rt = nd6_lookup(&sin6_next->sin6_addr, 1, NULL);
584 			if (rt) {
585 				ia6 = in6_ifawithscope(rt->rt_ifp, dst, cred);
586 				if (ia6 == 0)
587 					ia6 = ifatoia6(rt->rt_ifa);
588 			}
589 			if (ia6 && jailed) {
590 				jsin6.sin6_addr = (&ia6->ia_addr)->sin6_addr;
591 				if (!jailed_ip(cred->cr_prison,
592 					(struct sockaddr *)&jsin6))
593 					ia6 = 0;
594 			}
595 
596 			if (ia6 == 0) {
597 				*errorp = EADDRNOTAVAIL;
598 				return (0);
599 			}
600 			return (&satosin6(&ia6->ia_addr)->sin6_addr);
601 		}
602 	}
603 
604 	/*
605 	 * If route is known or can be allocated now,
606 	 * our src addr is taken from the i/f, else punt.
607 	 */
608 	if (ro) {
609 		if (ro->ro_rt &&
610 		    !IN6_ARE_ADDR_EQUAL(&satosin6(&ro->ro_dst)->sin6_addr, dst)) {
611 			RTFREE(ro->ro_rt);
612 			ro->ro_rt = NULL;
613 		}
614 		if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp == NULL) {
615 			struct sockaddr_in6 *dst6;
616 
617 			/* No route yet, so try to acquire one */
618 			bzero(&ro->ro_dst, sizeof(struct sockaddr_in6));
619 			dst6 = &ro->ro_dst;
620 			dst6->sin6_family = AF_INET6;
621 			dst6->sin6_len = sizeof(struct sockaddr_in6);
622 			dst6->sin6_addr = *dst;
623 			if (!jailed && IN6_IS_ADDR_MULTICAST(dst)) {
624 				ro->ro_rt =
625 				  rtpurelookup((struct sockaddr *)&ro->ro_dst);
626 			} else {
627 				rtalloc((struct route *)ro);
628 			}
629 		}
630 
631 		/*
632 		 * in_pcbconnect() checks out IFF_LOOPBACK to skip using
633 		 * the address. But we don't know why it does so.
634 		 * It is necessary to ensure the scope even for lo0
635 		 * so doesn't check out IFF_LOOPBACK.
636 		 */
637 
638 		if (ro->ro_rt) {
639 			ia6 = in6_ifawithscope(ro->ro_rt->rt_ifa->ifa_ifp, dst, cred);
640 			if (ia6 && jailed) {
641 				jsin6.sin6_addr = (&ia6->ia_addr)->sin6_addr;
642 				if (!jailed_ip(cred->cr_prison,
643 					(struct sockaddr *)&jsin6))
644 					ia6 = 0;
645 			}
646 
647 			if (ia6 == 0) /* xxx scope error ?*/
648 				ia6 = ifatoia6(ro->ro_rt->rt_ifa);
649 
650 			if (ia6 && jailed) {
651 				jsin6.sin6_addr = (&ia6->ia_addr)->sin6_addr;
652 				if (!jailed_ip(cred->cr_prison,
653 					(struct sockaddr *)&jsin6))
654 					ia6 = 0;
655 			}
656 		}
657 		if (ia6 == 0) {
658 			*errorp = EHOSTUNREACH;	/* no route */
659 			return (0);
660 		}
661 		return (&satosin6(&ia6->ia_addr)->sin6_addr);
662 	}
663 
664 	*errorp = EADDRNOTAVAIL;
665 	return (0);
666 }
667 
668 /*
669  * Default hop limit selection. The precedence is as follows:
670  * 1. Hoplimit valued specified via ioctl.
671  * 2. (If the outgoing interface is detected) the current
672  *     hop limit of the interface specified by router advertisement.
673  * 3. The system default hoplimit.
674 */
675 int
676 in6_selecthlim(struct in6pcb *in6p, struct ifnet *ifp)
677 {
678 	if (in6p && in6p->in6p_hops >= 0)
679 		return (in6p->in6p_hops);
680 	else if (ifp)
681 		return (ND_IFINFO(ifp)->chlim);
682 	else
683 		return (ip6_defhlim);
684 }
685 #endif
686 
687 void
688 in6_pcbdisconnect(struct inpcb *inp)
689 {
690 	bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
691 	inp->inp_fport = 0;
692 	/* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
693 	inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
694 	in_pcbremconnhash(inp);
695 	if (inp->inp_socket->so_state & SS_NOFDREF)
696 		in6_pcbdetach(inp);
697 }
698 
699 void
700 in6_pcbdetach(struct inpcb *inp)
701 {
702 	struct socket *so = inp->inp_socket;
703 	struct inpcbinfo *ipi = inp->inp_pcbinfo;
704 
705 #ifdef IPSEC
706 	if (inp->in6p_sp != NULL)
707 		ipsec6_delete_pcbpolicy(inp);
708 #endif /* IPSEC */
709 	inp->inp_gencnt = ++ipi->ipi_gencnt;
710 	in_pcbremlists(inp);
711 	so->so_pcb = NULL;
712 	KKASSERT((so->so_state & SS_ASSERTINPROG) == 0);
713 	sofree(so);		/* remove pcb ref */
714 
715 	if (inp->in6p_options)
716 		m_freem(inp->in6p_options);
717 	ip6_freepcbopts(inp->in6p_outputopts);
718 	ip6_freemoptions(inp->in6p_moptions);
719 	if (inp->in6p_route.ro_rt)
720 		rtfree(inp->in6p_route.ro_rt);
721 	/* Check and free IPv4 related resources in case of mapped addr */
722 	if (inp->inp_options)
723 		m_free(inp->inp_options);
724 	ip_freemoptions(inp->inp_moptions);
725 
726 	inp->inp_vflag = 0;
727 	kfree(inp, M_PCB);
728 }
729 
730 /*
731  * The calling convention of in6_setsockaddr() and in6_setpeeraddr() was
732  * modified to match the pru_sockaddr() and pru_peeraddr() entry points
733  * in struct pr_usrreqs, so that protocols can just reference then directly
734  * without the need for a wrapper function.  The socket must have a valid
735  * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
736  * except through a kernel programming error, so it is acceptable to panic
737  * (or in this case trap) if the PCB is invalid.  (Actually, we don't trap
738  * because there actually /is/ a programming error somewhere... XXX)
739  */
740 void
741 in6_setsockaddr_dispatch(netmsg_t msg)
742 {
743 	int error;
744 
745 	error = in6_setsockaddr(msg->sockaddr.base.nm_so, msg->sockaddr.nm_nam);
746 	lwkt_replymsg(&msg->sockaddr.base.lmsg, error);
747 }
748 
749 int
750 in6_setsockaddr(struct socket *so, struct sockaddr **nam)
751 {
752 	struct inpcb *inp;
753 	struct sockaddr_in6 *sin6;
754 
755 	/*
756 	 * Do the malloc first in case it blocks.
757 	 */
758 	sin6 = kmalloc(sizeof *sin6, M_SONAME, M_WAITOK | M_ZERO);
759 	sin6->sin6_family = AF_INET6;
760 	sin6->sin6_len = sizeof(*sin6);
761 
762 	crit_enter();
763 	inp = so->so_pcb;
764 	if (!inp) {
765 		crit_exit();
766 		kfree(sin6, M_SONAME);
767 		return EINVAL;
768 	}
769 	sin6->sin6_port = inp->inp_lport;
770 	sin6->sin6_addr = inp->in6p_laddr;
771 	crit_exit();
772 	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
773 		sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]);
774 	else
775 		sin6->sin6_scope_id = 0;	/*XXX*/
776 	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
777 		sin6->sin6_addr.s6_addr16[1] = 0;
778 
779 	*nam = (struct sockaddr *)sin6;
780 	return 0;
781 }
782 
783 void
784 in6_setpeeraddr_dispatch(netmsg_t msg)
785 {
786 	int error;
787 
788 	error = in6_setpeeraddr(msg->peeraddr.base.nm_so, msg->peeraddr.nm_nam);
789 	lwkt_replymsg(&msg->peeraddr.base.lmsg, error);
790 }
791 
792 int
793 in6_setpeeraddr(struct socket *so, struct sockaddr **nam)
794 {
795 	struct inpcb *inp;
796 	struct sockaddr_in6 *sin6;
797 
798 	/*
799 	 * Do the malloc first in case it blocks.
800 	 */
801 	sin6 = kmalloc(sizeof(*sin6), M_SONAME, M_WAITOK | M_ZERO);
802 	sin6->sin6_family = AF_INET6;
803 	sin6->sin6_len = sizeof(struct sockaddr_in6);
804 
805 	crit_enter();
806 	inp = so->so_pcb;
807 	if (!inp) {
808 		crit_exit();
809 		kfree(sin6, M_SONAME);
810 		return EINVAL;
811 	}
812 	sin6->sin6_port = inp->inp_fport;
813 	sin6->sin6_addr = inp->in6p_faddr;
814 	crit_exit();
815 	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
816 		sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]);
817 	else
818 		sin6->sin6_scope_id = 0;	/*XXX*/
819 	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
820 		sin6->sin6_addr.s6_addr16[1] = 0;
821 
822 	*nam = (struct sockaddr *)sin6;
823 	return 0;
824 }
825 
826 void
827 in6_mapped_sockaddr_dispatch(netmsg_t msg)
828 {
829 	int error;
830 
831 	error = in6_mapped_sockaddr(msg->sockaddr.base.nm_so,
832 				    msg->sockaddr.nm_nam);
833 	lwkt_replymsg(&msg->sockaddr.base.lmsg, error);
834 }
835 
836 int
837 in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
838 {
839 	struct	inpcb *inp = so->so_pcb;
840 	int	error;
841 
842 	if (inp == NULL)
843 		return EINVAL;
844 	if (inp->inp_vflag & INP_IPV4) {
845 		error = in_setsockaddr(so, nam);
846 		if (error == 0)
847 			in6_sin_2_v4mapsin6_in_sock(nam);
848 	} else
849 	/* scope issues will be handled in in6_setsockaddr(). */
850 	error = in6_setsockaddr(so, nam);
851 
852 	return error;
853 }
854 
855 int
856 in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
857 {
858 	struct	inpcb *inp = so->so_pcb;
859 	int	error;
860 
861 	if (inp == NULL)
862 		return EINVAL;
863 	if (inp->inp_vflag & INP_IPV4) {
864 		error = in_setpeeraddr(so, nam);
865 		if (error == 0)
866 			in6_sin_2_v4mapsin6_in_sock(nam);
867 	} else
868 	/* scope issues will be handled in in6_setpeeraddr(). */
869 	error = in6_setpeeraddr(so, nam);
870 
871 	return error;
872 }
873 
874 void
875 in6_mapped_peeraddr_dispatch(netmsg_t msg)
876 {
877 	int error;
878 
879 	error = in6_mapped_peeraddr(msg->base.nm_so, msg->peeraddr.nm_nam);
880 	lwkt_replymsg(&msg->base.lmsg, error);
881 }
882 
883 /*
884  * Pass some notification to all connections of a protocol
885  * associated with address dst.  The local address and/or port numbers
886  * may be specified to limit the search.  The "usual action" will be
887  * taken, depending on the ctlinput cmd.  The caller must filter any
888  * cmds that are uninteresting (e.g., no error in the map).
889  * Call the protocol specific routine (if any) to report
890  * any errors for each matching socket.
891  */
892 void
893 in6_pcbnotify(struct inpcbinfo *pcbinfo, struct sockaddr *dst, in_port_t fport,
894     const struct sockaddr *src, in_port_t lport, int cmd, int arg,
895     void (*notify) (struct inpcb *, int))
896 {
897 	struct inpcb *inp, *marker;
898 	struct sockaddr_in6 sa6_src, *sa6_dst;
899 	u_int32_t flowinfo;
900 
901 	if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
902 		return;
903 
904 	sa6_dst = (struct sockaddr_in6 *)dst;
905 	if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
906 		return;
907 
908 	/*
909 	 * note that src can be NULL when we get notify by local fragmentation.
910 	 */
911 	sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
912 	flowinfo = sa6_src.sin6_flowinfo;
913 
914 	/*
915 	 * Redirects go to all references to the destination,
916 	 * and use in6_rtchange to invalidate the route cache.
917 	 * Dead host indications: also use in6_rtchange to invalidate
918 	 * the cache, and deliver the error to all the sockets.
919 	 * Otherwise, if we have knowledge of the local port and address,
920 	 * deliver only to that socket.
921 	 */
922 	if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
923 		fport = 0;
924 		lport = 0;
925 		bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
926 
927 		if (cmd != PRC_HOSTDEAD)
928 			notify = in6_rtchange;
929 	}
930 	if (cmd != PRC_MSGSIZE)
931 		arg = inet6ctlerrmap[cmd];
932 
933 	marker = in_pcbmarker(mycpuid);
934 
935 	GET_PCBINFO_TOKEN(pcbinfo);
936 
937 	LIST_INSERT_HEAD(&pcbinfo->pcblisthead, marker, inp_list);
938 	while ((inp = LIST_NEXT(marker, inp_list)) != NULL) {
939 		LIST_REMOVE(marker, inp_list);
940 		LIST_INSERT_AFTER(inp, marker, inp_list);
941 
942 		if (inp->inp_flags & INP_PLACEMARKER)
943 			continue;
944 
945 		if ((inp->inp_vflag & INP_IPV6) == 0)
946 			continue;
947 		/*
948 		 * If the error designates a new path MTU for a destination
949 		 * and the application (associated with this socket) wanted to
950 		 * know the value, notify. Note that we notify for all
951 		 * disconnected sockets if the corresponding application
952 		 * wanted. This is because some UDP applications keep sending
953 		 * sockets disconnected.
954 		 * XXX: should we avoid to notify the value to TCP sockets?
955 		 */
956 		if (cmd == PRC_MSGSIZE && (inp->inp_flags & IN6P_MTU) != 0 &&
957 		    (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
958 		     IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &sa6_dst->sin6_addr))) {
959 			ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst, &arg);
960 		}
961 
962 		/*
963 		 * Detect if we should notify the error. If no source and
964 		 * destination ports are specifed, but non-zero flowinfo and
965 		 * local address match, notify the error. This is the case
966 		 * when the error is delivered with an encrypted buffer
967 		 * by ESP. Otherwise, just compare addresses and ports
968 		 * as usual.
969 		 */
970 		if (lport == 0 && fport == 0 && flowinfo &&
971 		    inp->inp_socket != NULL &&
972 		    flowinfo == (inp->in6p_flowinfo & IPV6_FLOWLABEL_MASK) &&
973 		    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
974 			goto do_notify;
975 		else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
976 					     &sa6_dst->sin6_addr) ||
977 			 inp->inp_socket == 0 ||
978 			 (lport && inp->inp_lport != lport) ||
979 			 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
980 			  !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
981 					      &sa6_src.sin6_addr)) ||
982 			 (fport && inp->inp_fport != fport))
983 			continue;
984 
985 do_notify:
986 		if (notify)
987 			(*notify)(inp, arg);
988 	}
989 	LIST_REMOVE(marker, inp_list);
990 
991 	REL_PCBINFO_TOKEN(pcbinfo);
992 }
993 
994 /*
995  * Lookup a PCB based on the local address and port.
996  */
997 struct inpcb *
998 in6_pcblookup_local(struct inpcbportinfo *portinfo,
999     const struct in6_addr *laddr, u_int lport_arg, int wild_okay,
1000     struct ucred *cred)
1001 {
1002 	struct inpcb *inp;
1003 	int matchwild = 3, wildcard;
1004 	u_short lport = lport_arg;
1005 	struct inpcbporthead *porthash;
1006 	struct inpcbport *phd;
1007 	struct inpcb *match = NULL;
1008 
1009 	/*
1010 	 * If the porthashbase is shared across several cpus, it must
1011 	 * have been locked.
1012 	 */
1013 	ASSERT_PORT_TOKEN_HELD(portinfo);
1014 
1015 	/*
1016 	 * Best fit PCB lookup.
1017 	 *
1018 	 * First see if this local port is in use by looking on the
1019 	 * port hash list.
1020 	 */
1021 	porthash = &portinfo->porthashbase[
1022 				INP_PCBPORTHASH(lport, portinfo->porthashmask)];
1023 	LIST_FOREACH(phd, porthash, phd_hash) {
1024 		if (phd->phd_port == lport)
1025 			break;
1026 	}
1027 
1028 	if (phd != NULL) {
1029 		/*
1030 		 * Port is in use by one or more PCBs. Look for best
1031 		 * fit.
1032 		 */
1033 		LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
1034 			wildcard = 0;
1035 			if ((inp->inp_vflag & INP_IPV6) == 0)
1036 				continue;
1037 			if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
1038 				wildcard++;
1039 			if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1040 				if (IN6_IS_ADDR_UNSPECIFIED(laddr))
1041 					wildcard++;
1042 				else if (!IN6_ARE_ADDR_EQUAL(
1043 					&inp->in6p_laddr, laddr))
1044 					continue;
1045 			} else {
1046 				if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
1047 					wildcard++;
1048 			}
1049 			if (wildcard && !wild_okay)
1050 				continue;
1051 			if (wildcard < matchwild &&
1052 			    (cred == NULL ||
1053 			     cred->cr_prison ==
1054 					inp->inp_socket->so_cred->cr_prison)) {
1055 				match = inp;
1056 				matchwild = wildcard;
1057 				if (wildcard == 0)
1058 					break;
1059 				else
1060 					matchwild = wildcard;
1061 			}
1062 		}
1063 	}
1064 	return (match);
1065 }
1066 
1067 void
1068 in6_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
1069 {
1070 	struct in6pcb *in6p, *marker;
1071 	struct ip6_moptions *im6o;
1072 	struct in6_multi_mship *imm, *nimm;
1073 
1074 	/*
1075 	 * We only need to make sure that we are in netisr0, where all
1076 	 * multicast operation happen.  We could check inpcbinfo which
1077 	 * does not belong to netisr0 by holding the inpcbinfo's token.
1078 	 * In this case, the pcbinfo must be able to be shared, i.e.
1079 	 * pcbinfo->infotoken is not NULL.
1080 	 */
1081 	KASSERT(&curthread->td_msgport == netisr_cpuport(0),
1082 	    ("not in netisr0"));
1083 	KASSERT(pcbinfo->cpu == 0 || pcbinfo->infotoken != NULL,
1084 	    ("pcbinfo could not be shared"));
1085 
1086 	/*
1087 	 * Get a marker for the current netisr (netisr0).
1088 	 *
1089 	 * It is possible that the multicast address deletion blocks,
1090 	 * which could cause temporary token releasing.  So we use
1091 	 * inpcb marker here to get a coherent view of the inpcb list.
1092 	 *
1093 	 * While, on the other hand, moptions are only added and deleted
1094 	 * in netisr0, so we would not see staled moption or miss moption
1095 	 * even if the token was released due to the blocking multicast
1096 	 * address deletion.
1097 	 */
1098 	marker = in_pcbmarker(mycpuid);
1099 
1100 	GET_PCBINFO_TOKEN(pcbinfo);
1101 
1102 	LIST_INSERT_HEAD(&pcbinfo->pcblisthead, marker, inp_list);
1103 	while ((in6p = LIST_NEXT(marker, inp_list)) != NULL) {
1104 		LIST_REMOVE(marker, inp_list);
1105 		LIST_INSERT_AFTER(in6p, marker, inp_list);
1106 
1107 		if (in6p->in6p_flags & INP_PLACEMARKER)
1108 			continue;
1109 		im6o = in6p->in6p_moptions;
1110 		if ((in6p->inp_vflag & INP_IPV6) &&
1111 		    im6o) {
1112 			/*
1113 			 * Unselect the outgoing interface if it is being
1114 			 * detached.
1115 			 */
1116 			if (im6o->im6o_multicast_ifp == ifp)
1117 				im6o->im6o_multicast_ifp = NULL;
1118 
1119 			/*
1120 			 * Drop multicast group membership if we joined
1121 			 * through the interface being detached.
1122 			 * XXX controversial - is it really legal for kernel
1123 			 * to force this?
1124 			 */
1125 			for (imm = im6o->im6o_memberships.lh_first;
1126 			     imm != NULL; imm = nimm) {
1127 				nimm = imm->i6mm_chain.le_next;
1128 				if (imm->i6mm_maddr->in6m_ifp == ifp) {
1129 					LIST_REMOVE(imm, i6mm_chain);
1130 					in6_delmulti(imm->i6mm_maddr);
1131 					kfree(imm, M_IPMADDR);
1132 				}
1133 			}
1134 		}
1135 	}
1136 	LIST_REMOVE(marker, inp_list);
1137 
1138 	REL_PCBINFO_TOKEN(pcbinfo);
1139 }
1140 
1141 /*
1142  * Check for alternatives when higher level complains
1143  * about service problems.  For now, invalidate cached
1144  * routing information.  If the route was created dynamically
1145  * (by a redirect), time to try a default gateway again.
1146  */
1147 void
1148 in6_losing(struct inpcb *in6p)
1149 {
1150 	struct rtentry *rt;
1151 	struct rt_addrinfo info;
1152 
1153 	if ((rt = in6p->in6p_route.ro_rt) != NULL) {
1154 		bzero((caddr_t)&info, sizeof(info));
1155 		info.rti_flags = rt->rt_flags;
1156 		info.rti_info[RTAX_DST] = rt_key(rt);
1157 		info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1158 		info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1159 		rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
1160 		if (rt->rt_flags & RTF_DYNAMIC) {
1161 			rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1162 			    rt_mask(rt), rt->rt_flags, NULL);
1163 		}
1164 		in6p->in6p_route.ro_rt = NULL;
1165 		rtfree(rt);
1166 		/*
1167 		 * A new route can be allocated
1168 		 * the next time output is attempted.
1169 		 */
1170 	}
1171 }
1172 
1173 /*
1174  * After a routing change, flush old routing
1175  * and allocate a (hopefully) better one.
1176  */
1177 void
1178 in6_rtchange(struct inpcb *inp, int error)
1179 {
1180 	if (inp->in6p_route.ro_rt) {
1181 		rtfree(inp->in6p_route.ro_rt);
1182 		inp->in6p_route.ro_rt = 0;
1183 		/*
1184 		 * A new route can be allocated the next time
1185 		 * output is attempted.
1186 		 */
1187 	}
1188 }
1189 
1190 /*
1191  * Lookup PCB in hash list.
1192  */
1193 struct inpcb *
1194 in6_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1195 		   u_int fport_arg, struct in6_addr *laddr, u_int lport_arg,
1196 		   int wildcard, struct ifnet *ifp)
1197 {
1198 	struct inpcbhead *head;
1199 	struct inpcb *inp;
1200 	struct inpcb *jinp = NULL;
1201 	u_short fport = fport_arg, lport = lport_arg;
1202 	int faith;
1203 
1204 	if (faithprefix_p != NULL)
1205 		faith = (*faithprefix_p)(laddr);
1206 	else
1207 		faith = 0;
1208 
1209 	/*
1210 	 * First look for an exact match.
1211 	 */
1212 	head = &pcbinfo->hashbase[INP_PCBCONNHASH(faddr->s6_addr32[3] /* XXX */,
1213 					      fport,
1214 					      laddr->s6_addr32[3], /* XXX JH */
1215 					      lport,
1216 					      pcbinfo->hashmask)];
1217 	LIST_FOREACH(inp, head, inp_hash) {
1218 		if ((inp->inp_vflag & INP_IPV6) == 0)
1219 			continue;
1220 		if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
1221 		    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
1222 		    inp->inp_fport == fport &&
1223 		    inp->inp_lport == lport) {
1224 			/*
1225 			 * Found.
1226 			 */
1227 			if (inp->inp_socket == NULL ||
1228 			inp->inp_socket->so_cred->cr_prison == NULL) {
1229 				return (inp);
1230 			} else {
1231 				if  (jinp == NULL)
1232 					jinp = inp;
1233 			}
1234 		}
1235 	}
1236 	if (jinp != NULL)
1237 		return(jinp);
1238 
1239 	if (wildcard) {
1240 		struct inpcontainerhead *chead;
1241 		struct inpcontainer *ic;
1242 		struct inpcb *local_wild = NULL;
1243 		struct inpcb *jinp_wild = NULL;
1244 		struct sockaddr_in6 jsin6;
1245 		struct ucred *cred;
1246 
1247 		/*
1248 		 * Order of socket selection:
1249 		 * 1. non-jailed, non-wild.
1250 		 * 2. non-jailed, wild.
1251 		 * 3. jailed, non-wild.
1252 		 * 4. jailed, wild.
1253 		 */
1254 		jsin6.sin6_family = AF_INET6;
1255 		chead = &pcbinfo->wildcardhashbase[INP_PCBWILDCARDHASH(lport,
1256 		    pcbinfo->wildcardhashmask)];
1257 
1258 		GET_PCBINFO_TOKEN(pcbinfo);
1259 		LIST_FOREACH(ic, chead, ic_list) {
1260 			inp = ic->ic_inp;
1261 			if (inp->inp_flags & INP_PLACEMARKER)
1262 				continue;
1263 
1264 			if (!(inp->inp_vflag & INP_IPV6))
1265 				continue;
1266 			if (inp->inp_socket != NULL)
1267 				cred = inp->inp_socket->so_cred;
1268 			else
1269 				cred = NULL;
1270 
1271 			if (cred != NULL && jailed(cred)) {
1272 				if (jinp != NULL) {
1273 					continue;
1274 				} else {
1275 		                        jsin6.sin6_addr = *laddr;
1276 					if (!jailed_ip(cred->cr_prison,
1277 					    (struct sockaddr *)&jsin6))
1278 						continue;
1279 				}
1280 			}
1281 			if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
1282 			    inp->inp_lport == lport) {
1283 				if (faith && (inp->inp_flags & INP_FAITH) == 0)
1284 					continue;
1285 				if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
1286 						       laddr)) {
1287 					if (cred != NULL && jailed(cred)) {
1288 						jinp = inp;
1289 					} else {
1290 						REL_PCBINFO_TOKEN(pcbinfo);
1291 						return (inp);
1292 					}
1293 				} else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1294 					if (cred != NULL && jailed(cred))
1295 						jinp_wild = inp;
1296 					else
1297 						local_wild = inp;
1298 				}
1299 			}
1300 		}
1301 		REL_PCBINFO_TOKEN(pcbinfo);
1302 
1303 		if (local_wild != NULL)
1304 			return (local_wild);
1305 		if (jinp != NULL)
1306 			return (jinp);
1307 		return (jinp_wild);
1308 	}
1309 
1310 	/*
1311 	 * Not found.
1312 	 */
1313 	return (NULL);
1314 }
1315 
1316 void
1317 init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m)
1318 {
1319 	struct ip6_hdr *ip;
1320 
1321 	ip = mtod(m, struct ip6_hdr *);
1322 	bzero(sin6, sizeof(*sin6));
1323 	sin6->sin6_len = sizeof(*sin6);
1324 	sin6->sin6_family = AF_INET6;
1325 	sin6->sin6_addr = ip->ip6_src;
1326 	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
1327 		sin6->sin6_addr.s6_addr16[1] = 0;
1328 	sin6->sin6_scope_id =
1329 		(m->m_pkthdr.rcvif && IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
1330 		? m->m_pkthdr.rcvif->if_index : 0;
1331 
1332 	return;
1333 }
1334 
1335 static void
1336 in6_savefaddr(struct socket *so, const struct sockaddr *faddr)
1337 {
1338 	struct sockaddr_in6 *sin6;
1339 
1340 	KASSERT(faddr->sa_family == AF_INET6,
1341 	    ("not AF_INET6 faddr %d", faddr->sa_family));
1342 
1343 	sin6 = kmalloc(sizeof(*sin6), M_SONAME, M_WAITOK | M_ZERO);
1344 	sin6->sin6_family = AF_INET6;
1345 	sin6->sin6_len = sizeof(*sin6);
1346 
1347 	sin6->sin6_port = ((const struct sockaddr_in6 *)faddr)->sin6_port;
1348 	sin6->sin6_addr = ((const struct sockaddr_in6 *)faddr)->sin6_addr;
1349 
1350 	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
1351 		sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]);
1352 	else
1353 		sin6->sin6_scope_id = 0;	/*XXX*/
1354 	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
1355 		sin6->sin6_addr.s6_addr16[1] = 0;
1356 
1357 	so->so_faddr = (struct sockaddr *)sin6;
1358 }
1359 
1360 void
1361 in6_mapped_savefaddr(struct socket *so, const struct sockaddr *faddr)
1362 {
1363 	if (faddr->sa_family == AF_INET) {
1364 		in_savefaddr(so, faddr);
1365 		in6_sin_2_v4mapsin6_in_sock(&so->so_faddr);
1366 	} else {
1367 		in6_savefaddr(so, faddr);
1368 	}
1369 }
1370