xref: /freebsd/sys/netinet6/in6_pcb.c (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
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 
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 <netinet6/ip6_var.h>
99 #include <netinet6/nd6.h>
100 #include <netinet/in_pcb.h>
101 #include <netinet6/in6_pcb.h>
102 #include <netinet6/scope6_var.h>
103 
104 #ifdef IPSEC
105 #include <netipsec/ipsec.h>
106 #include <netipsec/ipsec6.h>
107 #include <netipsec/key.h>
108 #endif /* IPSEC */
109 
110 #include <security/mac/mac_framework.h>
111 
112 struct	in6_addr zeroin6_addr;
113 
114 int
115 in6_pcbbind(register struct inpcb *inp, struct sockaddr *nam,
116     struct ucred *cred)
117 {
118 	struct socket *so = inp->inp_socket;
119 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL;
120 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
121 	u_short	lport = 0;
122 	int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
123 
124 	INP_INFO_WLOCK_ASSERT(pcbinfo);
125 	INP_LOCK_ASSERT(inp);
126 
127 	if (!in6_ifaddr) /* XXX broken! */
128 		return (EADDRNOTAVAIL);
129 	if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
130 		return (EINVAL);
131 	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
132 		wild = INPLOOKUP_WILDCARD;
133 	if (nam) {
134 		int error;
135 
136 		sin6 = (struct sockaddr_in6 *)nam;
137 		if (nam->sa_len != sizeof(*sin6))
138 			return (EINVAL);
139 		/*
140 		 * family check.
141 		 */
142 		if (nam->sa_family != AF_INET6)
143 			return (EAFNOSUPPORT);
144 
145 		if ((error = sa6_embedscope(sin6, ip6_use_defzone)) != 0)
146 			return(error);
147 
148 		lport = sin6->sin6_port;
149 		if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
150 			/*
151 			 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
152 			 * allow compepte duplication of binding if
153 			 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
154 			 * and a multicast address is bound on both
155 			 * new and duplicated sockets.
156 			 */
157 			if (so->so_options & SO_REUSEADDR)
158 				reuseport = SO_REUSEADDR|SO_REUSEPORT;
159 		} else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
160 			struct ifaddr *ia = NULL;
161 
162 			sin6->sin6_port = 0;		/* yech... */
163 			if ((ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == 0)
164 				return (EADDRNOTAVAIL);
165 
166 			/*
167 			 * XXX: bind to an anycast address might accidentally
168 			 * cause sending a packet with anycast source address.
169 			 * We should allow to bind to a deprecated address, since
170 			 * the application dares to use it.
171 			 */
172 			if (ia &&
173 			    ((struct in6_ifaddr *)ia)->ia6_flags &
174 			    (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) {
175 				return (EADDRNOTAVAIL);
176 			}
177 		}
178 		if (lport) {
179 			struct inpcb *t;
180 
181 			/* GROSS */
182 			if (ntohs(lport) <= ipport_reservedhigh &&
183 			    ntohs(lport) >= ipport_reservedlow &&
184 			    priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT,
185 			    0))
186 				return (EACCES);
187 			if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) &&
188 			    priv_check_cred(so->so_cred,
189 			    PRIV_NETINET_REUSEPORT, 0) != 0) {
190 				t = in6_pcblookup_local(pcbinfo,
191 				    &sin6->sin6_addr, lport,
192 				    INPLOOKUP_WILDCARD);
193 				if (t &&
194 				    ((t->inp_vflag & INP_TIMEWAIT) == 0) &&
195 				    (so->so_type != SOCK_STREAM ||
196 				     IN6_IS_ADDR_UNSPECIFIED(&t->in6p_faddr)) &&
197 				    (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
198 				     !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
199 				     (t->inp_socket->so_options & SO_REUSEPORT)
200 				      == 0) && (so->so_cred->cr_uid !=
201 				     t->inp_socket->so_cred->cr_uid))
202 					return (EADDRINUSE);
203 				if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
204 				    IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
205 					struct sockaddr_in sin;
206 
207 					in6_sin6_2_sin(&sin, sin6);
208 					t = in_pcblookup_local(pcbinfo,
209 						sin.sin_addr, lport,
210 						INPLOOKUP_WILDCARD);
211 					if (t &&
212 					    ((t->inp_vflag &
213 					      INP_TIMEWAIT) == 0) &&
214 					    (so->so_type != SOCK_STREAM ||
215 					     ntohl(t->inp_faddr.s_addr) ==
216 					      INADDR_ANY) &&
217 					    (so->so_cred->cr_uid !=
218 					     t->inp_socket->so_cred->cr_uid))
219 						return (EADDRINUSE);
220 				}
221 			}
222 			t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr,
223 						lport, wild);
224 			if (t && (reuseport & ((t->inp_vflag & INP_TIMEWAIT) ?
225 			    intotw(t)->tw_so_options :
226 			    t->inp_socket->so_options)) == 0)
227 				return (EADDRINUSE);
228 			if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
229 			    IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
230 				struct sockaddr_in sin;
231 
232 				in6_sin6_2_sin(&sin, sin6);
233 				t = in_pcblookup_local(pcbinfo, sin.sin_addr,
234 						       lport, wild);
235 				if (t && t->inp_vflag & INP_TIMEWAIT) {
236 					if ((reuseport &
237 					    intotw(t)->tw_so_options) == 0 &&
238 					    (ntohl(t->inp_laddr.s_addr) !=
239 					     INADDR_ANY || ((inp->inp_vflag &
240 					     INP_IPV6PROTO) ==
241 					     (t->inp_vflag & INP_IPV6PROTO))))
242 						return (EADDRINUSE);
243 				}
244 				else if (t &&
245 				    (reuseport & t->inp_socket->so_options)
246 				    == 0 && (ntohl(t->inp_laddr.s_addr) !=
247 				    INADDR_ANY || INP_SOCKAF(so) ==
248 				     INP_SOCKAF(t->inp_socket)))
249 					return (EADDRINUSE);
250 			}
251 		}
252 		inp->in6p_laddr = sin6->sin6_addr;
253 	}
254 	if (lport == 0) {
255 		int e;
256 		if ((e = in6_pcbsetport(&inp->in6p_laddr, inp, cred)) != 0)
257 			return (e);
258 	}
259 	else {
260 		inp->inp_lport = lport;
261 		if (in_pcbinshash(inp) != 0) {
262 			inp->in6p_laddr = in6addr_any;
263 			inp->inp_lport = 0;
264 			return (EAGAIN);
265 		}
266 	}
267 	return (0);
268 }
269 
270 /*
271  *   Transform old in6_pcbconnect() into an inner subroutine for new
272  *   in6_pcbconnect(): Do some validity-checking on the remote
273  *   address (in mbuf 'nam') and then determine local host address
274  *   (i.e., which interface) to use to access that remote host.
275  *
276  *   This preserves definition of in6_pcbconnect(), while supporting a
277  *   slightly different version for T/TCP.  (This is more than
278  *   a bit of a kludge, but cleaning up the internal interfaces would
279  *   have forced minor changes in every protocol).
280  */
281 int
282 in6_pcbladdr(register struct inpcb *inp, struct sockaddr *nam,
283     struct in6_addr **plocal_addr6)
284 {
285 	register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
286 	int error = 0;
287 	struct ifnet *ifp = NULL;
288 	int scope_ambiguous = 0;
289 
290 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
291 	INP_LOCK_ASSERT(inp);
292 
293 	if (nam->sa_len != sizeof (*sin6))
294 		return (EINVAL);
295 	if (sin6->sin6_family != AF_INET6)
296 		return (EAFNOSUPPORT);
297 	if (sin6->sin6_port == 0)
298 		return (EADDRNOTAVAIL);
299 
300 	if (sin6->sin6_scope_id == 0 && !ip6_use_defzone)
301 		scope_ambiguous = 1;
302 	if ((error = sa6_embedscope(sin6, ip6_use_defzone)) != 0)
303 		return(error);
304 
305 	if (in6_ifaddr) {
306 		/*
307 		 * If the destination address is UNSPECIFIED addr,
308 		 * use the loopback addr, e.g ::1.
309 		 */
310 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
311 			sin6->sin6_addr = in6addr_loopback;
312 	}
313 
314 	/*
315 	 * XXX: in6_selectsrc might replace the bound local address
316 	 * with the address specified by setsockopt(IPV6_PKTINFO).
317 	 * Is it the intended behavior?
318 	 */
319 	*plocal_addr6 = in6_selectsrc(sin6, inp->in6p_outputopts,
320 				      inp->in6p_moptions, NULL,
321 				      &inp->in6p_laddr, &ifp, &error);
322 	if (ifp && scope_ambiguous &&
323 	    (error = in6_setscope(&sin6->sin6_addr, ifp, NULL)) != 0) {
324 		return(error);
325 	}
326 
327 	if (*plocal_addr6 == 0) {
328 		if (error == 0)
329 			error = EADDRNOTAVAIL;
330 		return (error);
331 	}
332 	/*
333 	 * Don't do pcblookup call here; return interface in
334 	 * plocal_addr6
335 	 * and exit to caller, that will do the lookup.
336 	 */
337 
338 	return (0);
339 }
340 
341 /*
342  * Outer subroutine:
343  * Connect from a socket to a specified address.
344  * Both address and port must be specified in argument sin.
345  * If don't have a local address for this socket yet,
346  * then pick one.
347  */
348 int
349 in6_pcbconnect(register struct inpcb *inp, struct sockaddr *nam,
350     struct ucred *cred)
351 {
352 	struct in6_addr *addr6;
353 	register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
354 	int error;
355 
356 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
357 	INP_LOCK_ASSERT(inp);
358 
359 	/*
360 	 * Call inner routine, to assign local interface address.
361 	 * in6_pcbladdr() may automatically fill in sin6_scope_id.
362 	 */
363 	if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0)
364 		return (error);
365 
366 	if (in6_pcblookup_hash(inp->inp_pcbinfo, &sin6->sin6_addr,
367 			       sin6->sin6_port,
368 			      IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
369 			      ? addr6 : &inp->in6p_laddr,
370 			      inp->inp_lport, 0, NULL) != NULL) {
371 		return (EADDRINUSE);
372 	}
373 	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
374 		if (inp->inp_lport == 0) {
375 			error = in6_pcbbind(inp, (struct sockaddr *)0, cred);
376 			if (error)
377 				return (error);
378 		}
379 		inp->in6p_laddr = *addr6;
380 	}
381 	inp->in6p_faddr = sin6->sin6_addr;
382 	inp->inp_fport = sin6->sin6_port;
383 	/* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
384 	inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
385 	if (inp->in6p_flags & IN6P_AUTOFLOWLABEL)
386 		inp->in6p_flowinfo |=
387 		    (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
388 
389 	in_pcbrehash(inp);
390 
391 	return (0);
392 }
393 
394 void
395 in6_pcbdisconnect(struct inpcb *inp)
396 {
397 
398 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
399 	INP_LOCK_ASSERT(inp);
400 
401 	bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
402 	inp->inp_fport = 0;
403 	/* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
404 	inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
405 	in_pcbrehash(inp);
406 }
407 
408 void
409 in6_pcbdetach(struct inpcb *inp)
410 {
411 
412 	KASSERT(inp->inp_socket != NULL, ("in6_pcbdetach: inp_socket == NULL"));
413 	inp->inp_socket->so_pcb = NULL;
414 	inp->inp_socket = NULL;
415 }
416 
417 void
418 in6_pcbfree(struct inpcb *inp)
419 {
420 	struct inpcbinfo *ipi = inp->inp_pcbinfo;
421 
422 	KASSERT(inp->inp_socket == NULL, ("in6_pcbfree: inp_socket != NULL"));
423 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
424 	INP_LOCK_ASSERT(inp);
425 
426 #ifdef IPSEC
427 	if (inp->in6p_sp != NULL)
428 		ipsec6_delete_pcbpolicy(inp);
429 #endif /* IPSEC */
430 	inp->inp_gencnt = ++ipi->ipi_gencnt;
431 	in_pcbremlists(inp);
432 	ip6_freepcbopts(inp->in6p_outputopts);
433 	ip6_freemoptions(inp->in6p_moptions);
434 	/* Check and free IPv4 related resources in case of mapped addr */
435 	if (inp->inp_options)
436 		(void)m_free(inp->inp_options);
437 	if (inp->inp_moptions != NULL)
438 		inp_freemoptions(inp->inp_moptions);
439 	inp->inp_vflag = 0;
440 #ifdef MAC
441 	mac_inpcb_destroy(inp);
442 #endif
443 	INP_UNLOCK(inp);
444 	uma_zfree(ipi->ipi_zone, inp);
445 }
446 
447 struct sockaddr *
448 in6_sockaddr(in_port_t port, struct in6_addr *addr_p)
449 {
450 	struct sockaddr_in6 *sin6;
451 
452 	MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, M_SONAME, M_WAITOK);
453 	bzero(sin6, sizeof *sin6);
454 	sin6->sin6_family = AF_INET6;
455 	sin6->sin6_len = sizeof(*sin6);
456 	sin6->sin6_port = port;
457 	sin6->sin6_addr = *addr_p;
458 	(void)sa6_recoverscope(sin6); /* XXX: should catch errors */
459 
460 	return (struct sockaddr *)sin6;
461 }
462 
463 struct sockaddr *
464 in6_v4mapsin6_sockaddr(in_port_t port, struct in_addr *addr_p)
465 {
466 	struct sockaddr_in sin;
467 	struct sockaddr_in6 *sin6_p;
468 
469 	bzero(&sin, sizeof sin);
470 	sin.sin_family = AF_INET;
471 	sin.sin_len = sizeof(sin);
472 	sin.sin_port = port;
473 	sin.sin_addr = *addr_p;
474 
475 	MALLOC(sin6_p, struct sockaddr_in6 *, sizeof *sin6_p, M_SONAME,
476 		M_WAITOK);
477 	in6_sin_2_v4mapsin6(&sin, sin6_p);
478 
479 	return (struct sockaddr *)sin6_p;
480 }
481 
482 int
483 in6_getsockaddr(struct socket *so, struct sockaddr **nam)
484 {
485 	register struct inpcb *inp;
486 	struct in6_addr addr;
487 	in_port_t port;
488 
489 	inp = sotoinpcb(so);
490 	KASSERT(inp != NULL, ("in6_getsockaddr: inp == NULL"));
491 
492 	INP_LOCK(inp);
493 	port = inp->inp_lport;
494 	addr = inp->in6p_laddr;
495 	INP_UNLOCK(inp);
496 
497 	*nam = in6_sockaddr(port, &addr);
498 	return 0;
499 }
500 
501 int
502 in6_getpeeraddr(struct socket *so, struct sockaddr **nam)
503 {
504 	struct inpcb *inp;
505 	struct in6_addr addr;
506 	in_port_t port;
507 
508 	inp = sotoinpcb(so);
509 	KASSERT(inp != NULL, ("in6_getpeeraddr: inp == NULL"));
510 
511 	INP_LOCK(inp);
512 	port = inp->inp_fport;
513 	addr = inp->in6p_faddr;
514 	INP_UNLOCK(inp);
515 
516 	*nam = in6_sockaddr(port, &addr);
517 	return 0;
518 }
519 
520 int
521 in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
522 {
523 	struct	inpcb *inp;
524 	int	error;
525 
526 	inp = sotoinpcb(so);
527 	KASSERT(inp != NULL, ("in6_mapped_sockaddr: inp == NULL"));
528 
529 	if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
530 		error = in_getsockaddr(so, nam);
531 		if (error == 0)
532 			in6_sin_2_v4mapsin6_in_sock(nam);
533 	} else {
534 		/* scope issues will be handled in in6_getsockaddr(). */
535 		error = in6_getsockaddr(so, nam);
536 	}
537 
538 	return error;
539 }
540 
541 int
542 in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
543 {
544 	struct	inpcb *inp;
545 	int	error;
546 
547 	inp = sotoinpcb(so);
548 	KASSERT(inp != NULL, ("in6_mapped_peeraddr: inp == NULL"));
549 
550 	if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
551 		error = in_getpeeraddr(so, nam);
552 		if (error == 0)
553 			in6_sin_2_v4mapsin6_in_sock(nam);
554 	} else
555 	/* scope issues will be handled in in6_getpeeraddr(). */
556 	error = in6_getpeeraddr(so, nam);
557 
558 	return error;
559 }
560 
561 /*
562  * Pass some notification to all connections of a protocol
563  * associated with address dst.  The local address and/or port numbers
564  * may be specified to limit the search.  The "usual action" will be
565  * taken, depending on the ctlinput cmd.  The caller must filter any
566  * cmds that are uninteresting (e.g., no error in the map).
567  * Call the protocol specific routine (if any) to report
568  * any errors for each matching socket.
569  */
570 void
571 in6_pcbnotify(struct inpcbinfo *pcbinfo, struct sockaddr *dst,
572     u_int fport_arg, const struct sockaddr *src, u_int lport_arg,
573     int cmd, void *cmdarg,
574     struct inpcb *(*notify)(struct inpcb *, int))
575 {
576 	struct inpcbhead *head;
577 	struct inpcb *inp, *ninp;
578 	struct sockaddr_in6 sa6_src, *sa6_dst;
579 	u_short	fport = fport_arg, lport = lport_arg;
580 	u_int32_t flowinfo;
581 	int errno;
582 
583 	if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
584 		return;
585 
586 	sa6_dst = (struct sockaddr_in6 *)dst;
587 	if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
588 		return;
589 
590 	/*
591 	 * note that src can be NULL when we get notify by local fragmentation.
592 	 */
593 	sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
594 	flowinfo = sa6_src.sin6_flowinfo;
595 
596 	/*
597 	 * Redirects go to all references to the destination,
598 	 * and use in6_rtchange to invalidate the route cache.
599 	 * Dead host indications: also use in6_rtchange to invalidate
600 	 * the cache, and deliver the error to all the sockets.
601 	 * Otherwise, if we have knowledge of the local port and address,
602 	 * deliver only to that socket.
603 	 */
604 	if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
605 		fport = 0;
606 		lport = 0;
607 		bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
608 
609 		if (cmd != PRC_HOSTDEAD)
610 			notify = in6_rtchange;
611 	}
612 	errno = inet6ctlerrmap[cmd];
613 	head = pcbinfo->ipi_listhead;
614 	INP_INFO_WLOCK(pcbinfo);
615 	for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
616 		INP_LOCK(inp);
617 		ninp = LIST_NEXT(inp, inp_list);
618 
619 		if ((inp->inp_vflag & INP_IPV6) == 0) {
620 			INP_UNLOCK(inp);
621 			continue;
622 		}
623 
624 		/*
625 		 * If the error designates a new path MTU for a destination
626 		 * and the application (associated with this socket) wanted to
627 		 * know the value, notify. Note that we notify for all
628 		 * disconnected sockets if the corresponding application
629 		 * wanted. This is because some UDP applications keep sending
630 		 * sockets disconnected.
631 		 * XXX: should we avoid to notify the value to TCP sockets?
632 		 */
633 		if (cmd == PRC_MSGSIZE && (inp->inp_flags & IN6P_MTU) != 0 &&
634 		    (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
635 		     IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &sa6_dst->sin6_addr))) {
636 			ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst,
637 					(u_int32_t *)cmdarg);
638 		}
639 
640 		/*
641 		 * Detect if we should notify the error. If no source and
642 		 * destination ports are specifed, but non-zero flowinfo and
643 		 * local address match, notify the error. This is the case
644 		 * when the error is delivered with an encrypted buffer
645 		 * by ESP. Otherwise, just compare addresses and ports
646 		 * as usual.
647 		 */
648 		if (lport == 0 && fport == 0 && flowinfo &&
649 		    inp->inp_socket != NULL &&
650 		    flowinfo == (inp->in6p_flowinfo & IPV6_FLOWLABEL_MASK) &&
651 		    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
652 			goto do_notify;
653 		else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
654 					     &sa6_dst->sin6_addr) ||
655 			 inp->inp_socket == 0 ||
656 			 (lport && inp->inp_lport != lport) ||
657 			 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
658 			  !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
659 					      &sa6_src.sin6_addr)) ||
660 			 (fport && inp->inp_fport != fport)) {
661 			INP_UNLOCK(inp);
662 			continue;
663 		}
664 
665 	  do_notify:
666 		if (notify) {
667 			if ((*notify)(inp, errno))
668 				INP_UNLOCK(inp);
669 		} else
670 			INP_UNLOCK(inp);
671 	}
672 	INP_INFO_WUNLOCK(pcbinfo);
673 }
674 
675 /*
676  * Lookup a PCB based on the local address and port.
677  */
678 struct inpcb *
679 in6_pcblookup_local(struct inpcbinfo *pcbinfo, struct in6_addr *laddr,
680     u_int lport_arg, int wild_okay)
681 {
682 	register struct inpcb *inp;
683 	int matchwild = 3, wildcard;
684 	u_short lport = lport_arg;
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_LOCK(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_UNLOCK(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_RLOCK_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