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