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