xref: /freebsd/sys/netinet6/in6_pcb.c (revision 908e960ea6343acd9515d89d5d5696f9d8bf090c)
1 /*-
2  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the project nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	$KAME: in6_pcb.c,v 1.31 2001/05/21 05:45:10 jinmei Exp $
30  */
31 
32 /*-
33  * Copyright (c) 1982, 1986, 1991, 1993
34  *	The Regents of the University of California.  All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  * 4. Neither the name of the University nor the names of its contributors
45  *    may be used to endorse or promote products derived from this software
46  *    without specific prior written permission.
47  *
48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58  * SUCH DAMAGE.
59  *
60  *	@(#)in_pcb.c	8.2 (Berkeley) 1/4/94
61  */
62 
63 #include <sys/cdefs.h>
64 __FBSDID("$FreeBSD$");
65 
66 #include "opt_inet.h"
67 #include "opt_inet6.h"
68 #include "opt_ipsec.h"
69 
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/malloc.h>
73 #include <sys/mbuf.h>
74 #include <sys/domain.h>
75 #include <sys/protosw.h>
76 #include <sys/socket.h>
77 #include <sys/socketvar.h>
78 #include <sys/sockio.h>
79 #include <sys/errno.h>
80 #include <sys/time.h>
81 #include <sys/priv.h>
82 #include <sys/proc.h>
83 #include <sys/jail.h>
84 #include <sys/vimage.h>
85 
86 #include <vm/uma.h>
87 
88 #include <net/if.h>
89 #include <net/if_types.h>
90 #include <net/route.h>
91 
92 #include <netinet/in.h>
93 #include <netinet/in_var.h>
94 #include <netinet/in_systm.h>
95 #include <netinet/tcp_var.h>
96 #include <netinet/ip6.h>
97 #include <netinet/ip_var.h>
98 #include <netinet/vinet.h>
99 
100 #include <netinet6/ip6_var.h>
101 #include <netinet6/nd6.h>
102 #include <netinet/in_pcb.h>
103 #include <netinet6/in6_pcb.h>
104 #include <netinet6/scope6_var.h>
105 #include <netinet6/vinet6.h>
106 
107 #include <security/mac/mac_framework.h>
108 
109 struct	in6_addr zeroin6_addr;
110 
111 int
112 in6_pcbbind(register struct inpcb *inp, struct sockaddr *nam,
113     struct ucred *cred)
114 {
115 	INIT_VNET_INET6(inp->inp_vnet);
116 	INIT_VNET_INET(inp->inp_vnet);
117 	struct socket *so = inp->inp_socket;
118 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL;
119 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
120 	u_short	lport = 0;
121 	int error, wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
122 
123 	INP_INFO_WLOCK_ASSERT(pcbinfo);
124 	INP_WLOCK_ASSERT(inp);
125 
126 	if (!V_in6_ifaddr) /* XXX broken! */
127 		return (EADDRNOTAVAIL);
128 	if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
129 		return (EINVAL);
130 	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
131 		wild = INPLOOKUP_WILDCARD;
132 	if (nam == NULL) {
133 		if ((error = prison_local_ip6(cred, &inp->in6p_laddr,
134 		    ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
135 			return (error);
136 	} else {
137 		sin6 = (struct sockaddr_in6 *)nam;
138 		if (nam->sa_len != sizeof(*sin6))
139 			return (EINVAL);
140 		/*
141 		 * family check.
142 		 */
143 		if (nam->sa_family != AF_INET6)
144 			return (EAFNOSUPPORT);
145 
146 		if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
147 			return(error);
148 
149 		if ((error = prison_local_ip6(cred, &sin6->sin6_addr,
150 		    ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
151 			return (error);
152 
153 		lport = sin6->sin6_port;
154 		if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
155 			/*
156 			 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
157 			 * allow compepte duplication of binding if
158 			 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
159 			 * and a multicast address is bound on both
160 			 * new and duplicated sockets.
161 			 */
162 			if (so->so_options & SO_REUSEADDR)
163 				reuseport = SO_REUSEADDR|SO_REUSEPORT;
164 		} else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
165 			struct ifaddr *ia;
166 
167 			sin6->sin6_port = 0;		/* yech... */
168 			if ((ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == NULL &&
169 			    (inp->inp_flags & INP_BINDANY) == 0) {
170 				return (EADDRNOTAVAIL);
171 			}
172 
173 			/*
174 			 * XXX: bind to an anycast address might accidentally
175 			 * cause sending a packet with anycast source address.
176 			 * We should allow to bind to a deprecated address, since
177 			 * the application dares to use it.
178 			 */
179 			if (ia &&
180 			    ((struct in6_ifaddr *)ia)->ia6_flags &
181 			    (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) {
182 				return (EADDRNOTAVAIL);
183 			}
184 		}
185 		if (lport) {
186 			struct inpcb *t;
187 
188 			/* GROSS */
189 			if (ntohs(lport) <= V_ipport_reservedhigh &&
190 			    ntohs(lport) >= V_ipport_reservedlow &&
191 			    priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT,
192 			    0))
193 				return (EACCES);
194 			if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) &&
195 			    priv_check_cred(inp->inp_cred,
196 			    PRIV_NETINET_REUSEPORT, 0) != 0) {
197 				t = in6_pcblookup_local(pcbinfo,
198 				    &sin6->sin6_addr, lport,
199 				    INPLOOKUP_WILDCARD, cred);
200 				if (t &&
201 				    ((t->inp_flags & INP_TIMEWAIT) == 0) &&
202 				    (so->so_type != SOCK_STREAM ||
203 				     IN6_IS_ADDR_UNSPECIFIED(&t->in6p_faddr)) &&
204 				    (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
205 				     !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
206 				     (t->inp_socket->so_options & SO_REUSEPORT)
207 				      == 0) && (inp->inp_cred->cr_uid !=
208 				     t->inp_cred->cr_uid))
209 					return (EADDRINUSE);
210 				if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
211 				    IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
212 					struct sockaddr_in sin;
213 
214 					in6_sin6_2_sin(&sin, sin6);
215 					t = in_pcblookup_local(pcbinfo,
216 					    sin.sin_addr, lport,
217 					    INPLOOKUP_WILDCARD, cred);
218 					if (t &&
219 					    ((t->inp_flags &
220 					      INP_TIMEWAIT) == 0) &&
221 					    (so->so_type != SOCK_STREAM ||
222 					     ntohl(t->inp_faddr.s_addr) ==
223 					      INADDR_ANY) &&
224 					    (inp->inp_cred->cr_uid !=
225 					     t->inp_cred->cr_uid))
226 						return (EADDRINUSE);
227 				}
228 			}
229 			t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr,
230 			    lport, wild, cred);
231 			if (t && (reuseport & ((t->inp_flags & INP_TIMEWAIT) ?
232 			    intotw(t)->tw_so_options :
233 			    t->inp_socket->so_options)) == 0)
234 				return (EADDRINUSE);
235 			if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
236 			    IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
237 				struct sockaddr_in sin;
238 
239 				in6_sin6_2_sin(&sin, sin6);
240 				t = in_pcblookup_local(pcbinfo, sin.sin_addr,
241 				    lport, wild, cred);
242 				if (t && t->inp_flags & INP_TIMEWAIT) {
243 					if ((reuseport &
244 					    intotw(t)->tw_so_options) == 0 &&
245 					    (ntohl(t->inp_laddr.s_addr) !=
246 					     INADDR_ANY || ((inp->inp_vflag &
247 					     INP_IPV6PROTO) ==
248 					     (t->inp_vflag & INP_IPV6PROTO))))
249 						return (EADDRINUSE);
250 				}
251 				else if (t &&
252 				    (reuseport & t->inp_socket->so_options)
253 				    == 0 && (ntohl(t->inp_laddr.s_addr) !=
254 				    INADDR_ANY || INP_SOCKAF(so) ==
255 				     INP_SOCKAF(t->inp_socket)))
256 					return (EADDRINUSE);
257 			}
258 		}
259 		inp->in6p_laddr = sin6->sin6_addr;
260 	}
261 	if (lport == 0) {
262 		if ((error = in6_pcbsetport(&inp->in6p_laddr, inp, cred)) != 0)
263 			return (error);
264 	} else {
265 		inp->inp_lport = lport;
266 		if (in_pcbinshash(inp) != 0) {
267 			inp->in6p_laddr = in6addr_any;
268 			inp->inp_lport = 0;
269 			return (EAGAIN);
270 		}
271 	}
272 	return (0);
273 }
274 
275 /*
276  *   Transform old in6_pcbconnect() into an inner subroutine for new
277  *   in6_pcbconnect(): Do some validity-checking on the remote
278  *   address (in mbuf 'nam') and then determine local host address
279  *   (i.e., which interface) to use to access that remote host.
280  *
281  *   This preserves definition of in6_pcbconnect(), while supporting a
282  *   slightly different version for T/TCP.  (This is more than
283  *   a bit of a kludge, but cleaning up the internal interfaces would
284  *   have forced minor changes in every protocol).
285  */
286 int
287 in6_pcbladdr(register struct inpcb *inp, struct sockaddr *nam,
288     struct in6_addr **plocal_addr6)
289 {
290 	INIT_VNET_INET6(inp->inp_vnet);
291 	register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
292 	int error = 0;
293 	struct ifnet *ifp = NULL;
294 	int scope_ambiguous = 0;
295 
296 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
297 	INP_WLOCK_ASSERT(inp);
298 
299 	if (nam->sa_len != sizeof (*sin6))
300 		return (EINVAL);
301 	if (sin6->sin6_family != AF_INET6)
302 		return (EAFNOSUPPORT);
303 	if (sin6->sin6_port == 0)
304 		return (EADDRNOTAVAIL);
305 
306 	if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone)
307 		scope_ambiguous = 1;
308 	if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
309 		return(error);
310 
311 	if (V_in6_ifaddr) {
312 		/*
313 		 * If the destination address is UNSPECIFIED addr,
314 		 * use the loopback addr, e.g ::1.
315 		 */
316 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
317 			sin6->sin6_addr = in6addr_loopback;
318 	}
319 	if ((error = prison_remote_ip6(inp->inp_cred, &sin6->sin6_addr)) != 0)
320 		return (error);
321 
322 	/*
323 	 * XXX: in6_selectsrc might replace the bound local address
324 	 * with the address specified by setsockopt(IPV6_PKTINFO).
325 	 * Is it the intended behavior?
326 	 */
327 	*plocal_addr6 = in6_selectsrc(sin6, inp->in6p_outputopts,
328 				      inp, NULL,
329 				      inp->inp_cred,
330 				      &ifp, &error);
331 	if (ifp && scope_ambiguous &&
332 	    (error = in6_setscope(&sin6->sin6_addr, ifp, NULL)) != 0) {
333 		return(error);
334 	}
335 
336 	if (*plocal_addr6 == NULL) {
337 		if (error == 0)
338 			error = EADDRNOTAVAIL;
339 		return (error);
340 	}
341 	/*
342 	 * Don't do pcblookup call here; return interface in
343 	 * plocal_addr6
344 	 * and exit to caller, that will do the lookup.
345 	 */
346 
347 	return (0);
348 }
349 
350 /*
351  * Outer subroutine:
352  * Connect from a socket to a specified address.
353  * Both address and port must be specified in argument sin.
354  * If don't have a local address for this socket yet,
355  * then pick one.
356  */
357 int
358 in6_pcbconnect(register struct inpcb *inp, struct sockaddr *nam,
359     struct ucred *cred)
360 {
361 	struct in6_addr *addr6;
362 	register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
363 	int error;
364 
365 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
366 	INP_WLOCK_ASSERT(inp);
367 
368 	/*
369 	 * Call inner routine, to assign local interface address.
370 	 * in6_pcbladdr() may automatically fill in sin6_scope_id.
371 	 */
372 	if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0)
373 		return (error);
374 
375 	if (in6_pcblookup_hash(inp->inp_pcbinfo, &sin6->sin6_addr,
376 			       sin6->sin6_port,
377 			      IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
378 			      ? addr6 : &inp->in6p_laddr,
379 			      inp->inp_lport, 0, NULL) != NULL) {
380 		return (EADDRINUSE);
381 	}
382 	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
383 		if (inp->inp_lport == 0) {
384 			error = in6_pcbbind(inp, (struct sockaddr *)0, cred);
385 			if (error)
386 				return (error);
387 		}
388 		inp->in6p_laddr = *addr6;
389 	}
390 	inp->in6p_faddr = sin6->sin6_addr;
391 	inp->inp_fport = sin6->sin6_port;
392 	/* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
393 	inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
394 	if (inp->inp_flags & IN6P_AUTOFLOWLABEL)
395 		inp->inp_flow |=
396 		    (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
397 
398 	in_pcbrehash(inp);
399 
400 	return (0);
401 }
402 
403 void
404 in6_pcbdisconnect(struct inpcb *inp)
405 {
406 
407 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
408 	INP_WLOCK_ASSERT(inp);
409 
410 	bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
411 	inp->inp_fport = 0;
412 	/* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
413 	inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
414 	in_pcbrehash(inp);
415 }
416 
417 struct sockaddr *
418 in6_sockaddr(in_port_t port, struct in6_addr *addr_p)
419 {
420 	struct sockaddr_in6 *sin6;
421 
422 	sin6 = malloc(sizeof *sin6, M_SONAME, M_WAITOK);
423 	bzero(sin6, sizeof *sin6);
424 	sin6->sin6_family = AF_INET6;
425 	sin6->sin6_len = sizeof(*sin6);
426 	sin6->sin6_port = port;
427 	sin6->sin6_addr = *addr_p;
428 	(void)sa6_recoverscope(sin6); /* XXX: should catch errors */
429 
430 	return (struct sockaddr *)sin6;
431 }
432 
433 struct sockaddr *
434 in6_v4mapsin6_sockaddr(in_port_t port, struct in_addr *addr_p)
435 {
436 	struct sockaddr_in sin;
437 	struct sockaddr_in6 *sin6_p;
438 
439 	bzero(&sin, sizeof sin);
440 	sin.sin_family = AF_INET;
441 	sin.sin_len = sizeof(sin);
442 	sin.sin_port = port;
443 	sin.sin_addr = *addr_p;
444 
445 	sin6_p = malloc(sizeof *sin6_p, M_SONAME,
446 		M_WAITOK);
447 	in6_sin_2_v4mapsin6(&sin, sin6_p);
448 
449 	return (struct sockaddr *)sin6_p;
450 }
451 
452 int
453 in6_getsockaddr(struct socket *so, struct sockaddr **nam)
454 {
455 	register struct inpcb *inp;
456 	struct in6_addr addr;
457 	in_port_t port;
458 
459 	inp = sotoinpcb(so);
460 	KASSERT(inp != NULL, ("in6_getsockaddr: inp == NULL"));
461 
462 	INP_RLOCK(inp);
463 	port = inp->inp_lport;
464 	addr = inp->in6p_laddr;
465 	INP_RUNLOCK(inp);
466 
467 	*nam = in6_sockaddr(port, &addr);
468 	return 0;
469 }
470 
471 int
472 in6_getpeeraddr(struct socket *so, struct sockaddr **nam)
473 {
474 	struct inpcb *inp;
475 	struct in6_addr addr;
476 	in_port_t port;
477 
478 	inp = sotoinpcb(so);
479 	KASSERT(inp != NULL, ("in6_getpeeraddr: inp == NULL"));
480 
481 	INP_RLOCK(inp);
482 	port = inp->inp_fport;
483 	addr = inp->in6p_faddr;
484 	INP_RUNLOCK(inp);
485 
486 	*nam = in6_sockaddr(port, &addr);
487 	return 0;
488 }
489 
490 int
491 in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
492 {
493 	struct	inpcb *inp;
494 	int	error;
495 
496 	inp = sotoinpcb(so);
497 	KASSERT(inp != NULL, ("in6_mapped_sockaddr: inp == NULL"));
498 
499 	if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
500 		error = in_getsockaddr(so, nam);
501 		if (error == 0)
502 			in6_sin_2_v4mapsin6_in_sock(nam);
503 	} else {
504 		/* scope issues will be handled in in6_getsockaddr(). */
505 		error = in6_getsockaddr(so, nam);
506 	}
507 
508 	return error;
509 }
510 
511 int
512 in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
513 {
514 	struct	inpcb *inp;
515 	int	error;
516 
517 	inp = sotoinpcb(so);
518 	KASSERT(inp != NULL, ("in6_mapped_peeraddr: inp == NULL"));
519 
520 	if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
521 		error = in_getpeeraddr(so, nam);
522 		if (error == 0)
523 			in6_sin_2_v4mapsin6_in_sock(nam);
524 	} else
525 	/* scope issues will be handled in in6_getpeeraddr(). */
526 	error = in6_getpeeraddr(so, nam);
527 
528 	return error;
529 }
530 
531 /*
532  * Pass some notification to all connections of a protocol
533  * associated with address dst.  The local address and/or port numbers
534  * may be specified to limit the search.  The "usual action" will be
535  * taken, depending on the ctlinput cmd.  The caller must filter any
536  * cmds that are uninteresting (e.g., no error in the map).
537  * Call the protocol specific routine (if any) to report
538  * any errors for each matching socket.
539  */
540 void
541 in6_pcbnotify(struct inpcbinfo *pcbinfo, struct sockaddr *dst,
542     u_int fport_arg, const struct sockaddr *src, u_int lport_arg,
543     int cmd, void *cmdarg,
544     struct inpcb *(*notify)(struct inpcb *, int))
545 {
546 	struct inpcb *inp, *inp_temp;
547 	struct sockaddr_in6 sa6_src, *sa6_dst;
548 	u_short	fport = fport_arg, lport = lport_arg;
549 	u_int32_t flowinfo;
550 	int errno;
551 
552 	if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
553 		return;
554 
555 	sa6_dst = (struct sockaddr_in6 *)dst;
556 	if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
557 		return;
558 
559 	/*
560 	 * note that src can be NULL when we get notify by local fragmentation.
561 	 */
562 	sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
563 	flowinfo = sa6_src.sin6_flowinfo;
564 
565 	/*
566 	 * Redirects go to all references to the destination,
567 	 * and use in6_rtchange to invalidate the route cache.
568 	 * Dead host indications: also use in6_rtchange to invalidate
569 	 * the cache, and deliver the error to all the sockets.
570 	 * Otherwise, if we have knowledge of the local port and address,
571 	 * deliver only to that socket.
572 	 */
573 	if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
574 		fport = 0;
575 		lport = 0;
576 		bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
577 
578 		if (cmd != PRC_HOSTDEAD)
579 			notify = in6_rtchange;
580 	}
581 	errno = inet6ctlerrmap[cmd];
582 	INP_INFO_WLOCK(pcbinfo);
583 	LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
584 		INP_WLOCK(inp);
585 		if ((inp->inp_vflag & INP_IPV6) == 0) {
586 			INP_WUNLOCK(inp);
587 			continue;
588 		}
589 
590 		/*
591 		 * If the error designates a new path MTU for a destination
592 		 * and the application (associated with this socket) wanted to
593 		 * know the value, notify. Note that we notify for all
594 		 * disconnected sockets if the corresponding application
595 		 * wanted. This is because some UDP applications keep sending
596 		 * sockets disconnected.
597 		 * XXX: should we avoid to notify the value to TCP sockets?
598 		 */
599 		if (cmd == PRC_MSGSIZE && (inp->inp_flags & IN6P_MTU) != 0 &&
600 		    (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
601 		     IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &sa6_dst->sin6_addr))) {
602 			ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst,
603 					(u_int32_t *)cmdarg);
604 		}
605 
606 		/*
607 		 * Detect if we should notify the error. If no source and
608 		 * destination ports are specifed, but non-zero flowinfo and
609 		 * local address match, notify the error. This is the case
610 		 * when the error is delivered with an encrypted buffer
611 		 * by ESP. Otherwise, just compare addresses and ports
612 		 * as usual.
613 		 */
614 		if (lport == 0 && fport == 0 && flowinfo &&
615 		    inp->inp_socket != NULL &&
616 		    flowinfo == (inp->inp_flow & IPV6_FLOWLABEL_MASK) &&
617 		    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
618 			goto do_notify;
619 		else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
620 					     &sa6_dst->sin6_addr) ||
621 			 inp->inp_socket == 0 ||
622 			 (lport && inp->inp_lport != lport) ||
623 			 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
624 			  !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
625 					      &sa6_src.sin6_addr)) ||
626 			 (fport && inp->inp_fport != fport)) {
627 			INP_WUNLOCK(inp);
628 			continue;
629 		}
630 
631 	  do_notify:
632 		if (notify) {
633 			if ((*notify)(inp, errno))
634 				INP_WUNLOCK(inp);
635 		} else
636 			INP_WUNLOCK(inp);
637 	}
638 	INP_INFO_WUNLOCK(pcbinfo);
639 }
640 
641 /*
642  * Lookup a PCB based on the local address and port.
643  */
644 struct inpcb *
645 in6_pcblookup_local(struct inpcbinfo *pcbinfo, struct in6_addr *laddr,
646     u_short lport, int wild_okay, struct ucred *cred)
647 {
648 	register struct inpcb *inp;
649 	int matchwild = 3, wildcard;
650 
651 	INP_INFO_WLOCK_ASSERT(pcbinfo);
652 
653 	if (!wild_okay) {
654 		struct inpcbhead *head;
655 		/*
656 		 * Look for an unconnected (wildcard foreign addr) PCB that
657 		 * matches the local address and port we're looking for.
658 		 */
659 		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
660 		    0, pcbinfo->ipi_hashmask)];
661 		LIST_FOREACH(inp, head, inp_hash) {
662 			/* XXX inp locking */
663 			if ((inp->inp_vflag & INP_IPV6) == 0)
664 				continue;
665 			if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
666 			    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
667 			    inp->inp_lport == lport) {
668 				/* Found. */
669 				if (cred == NULL ||
670 				    prison_equal_ip6(cred->cr_prison,
671 					inp->inp_cred->cr_prison))
672 					return (inp);
673 			}
674 		}
675 		/*
676 		 * Not found.
677 		 */
678 		return (NULL);
679 	} else {
680 		struct inpcbporthead *porthash;
681 		struct inpcbport *phd;
682 		struct inpcb *match = NULL;
683 		/*
684 		 * Best fit PCB lookup.
685 		 *
686 		 * First see if this local port is in use by looking on the
687 		 * port hash list.
688 		 */
689 		porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
690 		    pcbinfo->ipi_porthashmask)];
691 		LIST_FOREACH(phd, porthash, phd_hash) {
692 			if (phd->phd_port == lport)
693 				break;
694 		}
695 		if (phd != NULL) {
696 			/*
697 			 * Port is in use by one or more PCBs. Look for best
698 			 * fit.
699 			 */
700 			LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
701 				wildcard = 0;
702 				if (cred != NULL &&
703 				    !prison_equal_ip6(cred->cr_prison,
704 					inp->inp_cred->cr_prison))
705 					continue;
706 				/* XXX inp locking */
707 				if ((inp->inp_vflag & INP_IPV6) == 0)
708 					continue;
709 				if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
710 					wildcard++;
711 				if (!IN6_IS_ADDR_UNSPECIFIED(
712 					&inp->in6p_laddr)) {
713 					if (IN6_IS_ADDR_UNSPECIFIED(laddr))
714 						wildcard++;
715 					else if (!IN6_ARE_ADDR_EQUAL(
716 					    &inp->in6p_laddr, laddr))
717 						continue;
718 				} else {
719 					if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
720 						wildcard++;
721 				}
722 				if (wildcard < matchwild) {
723 					match = inp;
724 					matchwild = wildcard;
725 					if (matchwild == 0)
726 						break;
727 				}
728 			}
729 		}
730 		return (match);
731 	}
732 }
733 
734 void
735 in6_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
736 {
737 	struct inpcb *in6p;
738 	struct ip6_moptions *im6o;
739 	int i, gap;
740 
741 	INP_INFO_RLOCK(pcbinfo);
742 	LIST_FOREACH(in6p, pcbinfo->ipi_listhead, inp_list) {
743 		INP_WLOCK(in6p);
744 		im6o = in6p->in6p_moptions;
745 		if ((in6p->inp_vflag & INP_IPV6) && im6o != NULL) {
746 			/*
747 			 * Unselect the outgoing ifp for multicast if it
748 			 * is being detached.
749 			 */
750 			if (im6o->im6o_multicast_ifp == ifp)
751 				im6o->im6o_multicast_ifp = NULL;
752 			/*
753 			 * Drop multicast group membership if we joined
754 			 * through the interface being detached.
755 			 */
756 			gap = 0;
757 			for (i = 0; i < im6o->im6o_num_memberships; i++) {
758 				if (im6o->im6o_membership[i]->in6m_ifp ==
759 				    ifp) {
760 					in6_mc_leave(im6o->im6o_membership[i],
761 					    NULL);
762 					gap++;
763 				} else if (gap != 0) {
764 					im6o->im6o_membership[i - gap] =
765 					    im6o->im6o_membership[i];
766 				}
767 			}
768 			im6o->im6o_num_memberships -= gap;
769 		}
770 		INP_WUNLOCK(in6p);
771 	}
772 	INP_INFO_RUNLOCK(pcbinfo);
773 }
774 
775 /*
776  * Check for alternatives when higher level complains
777  * about service problems.  For now, invalidate cached
778  * routing information.  If the route was created dynamically
779  * (by a redirect), time to try a default gateway again.
780  */
781 void
782 in6_losing(struct inpcb *in6p)
783 {
784 
785 	/*
786 	 * We don't store route pointers in the routing table anymore
787 	 */
788 	return;
789 }
790 
791 /*
792  * After a routing change, flush old routing
793  * and allocate a (hopefully) better one.
794  */
795 struct inpcb *
796 in6_rtchange(struct inpcb *inp, int errno)
797 {
798 	/*
799 	 * We don't store route pointers in the routing table anymore
800 	 */
801 	return inp;
802 }
803 
804 /*
805  * Lookup PCB in hash list.
806  */
807 struct inpcb *
808 in6_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
809     u_int fport_arg, struct in6_addr *laddr, u_int lport_arg, int wildcard,
810     struct ifnet *ifp)
811 {
812 	struct inpcbhead *head;
813 	struct inpcb *inp, *tmpinp;
814 	u_short fport = fport_arg, lport = lport_arg;
815 	int faith;
816 
817 	INP_INFO_LOCK_ASSERT(pcbinfo);
818 
819 	if (faithprefix_p != NULL)
820 		faith = (*faithprefix_p)(laddr);
821 	else
822 		faith = 0;
823 
824 	/*
825 	 * First look for an exact match.
826 	 */
827 	tmpinp = NULL;
828 	head = &pcbinfo->ipi_hashbase[
829 	    INP_PCBHASH(faddr->s6_addr32[3] /* XXX */, lport, fport,
830 	    pcbinfo->ipi_hashmask)];
831 	LIST_FOREACH(inp, head, inp_hash) {
832 		/* XXX inp locking */
833 		if ((inp->inp_vflag & INP_IPV6) == 0)
834 			continue;
835 		if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
836 		    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
837 		    inp->inp_fport == fport &&
838 		    inp->inp_lport == lport) {
839 			/*
840 			 * XXX We should be able to directly return
841 			 * the inp here, without any checks.
842 			 * Well unless both bound with SO_REUSEPORT?
843 			 */
844 			if (prison_flag(inp->inp_cred, PR_IP6))
845 				return (inp);
846 			if (tmpinp == NULL)
847 				tmpinp = inp;
848 		}
849 	}
850 	if (tmpinp != NULL)
851 		return (tmpinp);
852 
853 	/*
854 	 * Then look for a wildcard match, if requested.
855 	 */
856 	if (wildcard == INPLOOKUP_WILDCARD) {
857 		struct inpcb *local_wild = NULL, *local_exact = NULL;
858 		struct inpcb *jail_wild = NULL;
859 		int injail;
860 
861 		/*
862 		 * Order of socket selection - we always prefer jails.
863 		 *      1. jailed, non-wild.
864 		 *      2. jailed, wild.
865 		 *      3. non-jailed, non-wild.
866 		 *      4. non-jailed, wild.
867 		 */
868 		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
869 		    0, pcbinfo->ipi_hashmask)];
870 		LIST_FOREACH(inp, head, inp_hash) {
871 			/* XXX inp locking */
872 			if ((inp->inp_vflag & INP_IPV6) == 0)
873 				continue;
874 
875 			if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
876 			    inp->inp_lport != lport) {
877 				continue;
878 			}
879 
880 			/* XXX inp locking */
881 			if (faith && (inp->inp_flags & INP_FAITH) == 0)
882 				continue;
883 
884 			injail = prison_flag(inp->inp_cred, PR_IP6);
885 			if (injail) {
886 				if (prison_check_ip6(inp->inp_cred,
887 				    laddr) != 0)
888 					continue;
889 			} else {
890 				if (local_exact != NULL)
891 					continue;
892 			}
893 
894 			if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
895 				if (injail)
896 					return (inp);
897 				else
898 					local_exact = inp;
899 			} else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
900 				if (injail)
901 					jail_wild = inp;
902 				else
903 					local_wild = inp;
904 			}
905 		} /* LIST_FOREACH */
906 
907 		if (jail_wild != NULL)
908 			return (jail_wild);
909 		if (local_exact != NULL)
910 			return (local_exact);
911 		if (local_wild != NULL)
912 			return (local_wild);
913 	} /* if (wildcard == INPLOOKUP_WILDCARD) */
914 
915 	/*
916 	 * Not found.
917 	 */
918 	return (NULL);
919 }
920 
921 void
922 init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m)
923 {
924 	struct ip6_hdr *ip;
925 
926 	ip = mtod(m, struct ip6_hdr *);
927 	bzero(sin6, sizeof(*sin6));
928 	sin6->sin6_len = sizeof(*sin6);
929 	sin6->sin6_family = AF_INET6;
930 	sin6->sin6_addr = ip->ip6_src;
931 
932 	(void)sa6_recoverscope(sin6); /* XXX: should catch errors... */
933 
934 	return;
935 }
936