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