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