xref: /xnu-11215.1.10/bsd/netinet6/in6_pcb.c (revision 8d741a5de7ff4191bf97d57b9f54c2f6d4a15585)
1 /*
2  * Copyright (c) 2003-2021 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 /*
29  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
30  * All rights reserved.
31  *
32  * Redistribution and use in source and binary forms, with or without
33  * modification, are permitted provided that the following conditions
34  * are met:
35  * 1. Redistributions of source code must retain the above copyright
36  *    notice, this list of conditions and the following disclaimer.
37  * 2. Redistributions in binary form must reproduce the above copyright
38  *    notice, this list of conditions and the following disclaimer in the
39  *    documentation and/or other materials provided with the distribution.
40  * 3. Neither the name of the project nor the names of its contributors
41  *    may be used to endorse or promote products derived from this software
42  *    without specific prior written permission.
43  *
44  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
45  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
48  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54  * SUCH DAMAGE.
55  *
56  */
57 
58 /*
59  * Copyright (c) 1982, 1986, 1991, 1993
60  *	The Regents of the University of California.  All rights reserved.
61  *
62  * Redistribution and use in source and binary forms, with or without
63  * modification, are permitted provided that the following conditions
64  * are met:
65  * 1. Redistributions of source code must retain the above copyright
66  *    notice, this list of conditions and the following disclaimer.
67  * 2. Redistributions in binary form must reproduce the above copyright
68  *    notice, this list of conditions and the following disclaimer in the
69  *    documentation and/or other materials provided with the distribution.
70  * 3. All advertising materials mentioning features or use of this software
71  *    must display the following acknowledgement:
72  *	This product includes software developed by the University of
73  *	California, Berkeley and its contributors.
74  * 4. Neither the name of the University nor the names of its contributors
75  *    may be used to endorse or promote products derived from this software
76  *    without specific prior written permission.
77  *
78  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
79  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
80  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
81  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
82  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
83  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
84  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
85  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
86  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
87  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
88  * SUCH DAMAGE.
89  *
90  *	@(#)in_pcb.c	8.2 (Berkeley) 1/4/94
91  */
92 
93 #include <sys/param.h>
94 #include <sys/systm.h>
95 #include <sys/malloc.h>
96 #include <sys/mbuf.h>
97 #include <sys/domain.h>
98 #include <sys/protosw.h>
99 #include <sys/socket.h>
100 #include <sys/socketvar.h>
101 #include <sys/sockio.h>
102 #include <sys/errno.h>
103 #include <sys/time.h>
104 #include <sys/proc.h>
105 #include <sys/kauth.h>
106 #include <sys/priv.h>
107 
108 #include <net/if.h>
109 #include <net/if_types.h>
110 #include <net/route.h>
111 #include <net/ntstat.h>
112 #include <net/restricted_in_port.h>
113 
114 #include <netinet/in.h>
115 #include <netinet/in_var.h>
116 #include <netinet/in_systm.h>
117 #include <netinet/ip6.h>
118 #include <netinet/ip_var.h>
119 
120 #include <netinet6/ip6_var.h>
121 #include <netinet6/nd6.h>
122 #include <netinet/in_pcb.h>
123 #include <netinet6/in6_pcb.h>
124 
125 #include <net/if_types.h>
126 #include <net/if_var.h>
127 
128 #include <kern/kern_types.h>
129 #include <kern/zalloc.h>
130 
131 #if IPSEC
132 #include <netinet6/ipsec.h>
133 #include <netinet6/ipsec6.h>
134 #include <netinet6/ah.h>
135 #include <netinet6/ah6.h>
136 #include <netkey/key.h>
137 #endif /* IPSEC */
138 
139 #if NECP
140 #include <net/necp.h>
141 #endif /* NECP */
142 
143 #include <net/sockaddr_utils.h>
144 
145 /*
146  * in6_pcblookup_local_and_cleanup does everything
147  * in6_pcblookup_local does but it checks for a socket
148  * that's going away. Since we know that the lock is
149  * held read+write when this function is called, we
150  * can safely dispose of this socket like the slow
151  * timer would usually do and return NULL. This is
152  * great for bind.
153  */
154 static struct inpcb *
in6_pcblookup_local_and_cleanup(struct inpcbinfo * pcbinfo,struct in6_addr * laddr,u_int lport_arg,uint32_t ifscope,int wild_okay)155 in6_pcblookup_local_and_cleanup(struct inpcbinfo *pcbinfo,
156     struct in6_addr *laddr, u_int lport_arg, uint32_t ifscope, int wild_okay)
157 {
158 	struct inpcb *__single inp;
159 
160 	/* Perform normal lookup */
161 	inp = in6_pcblookup_local(pcbinfo, laddr, lport_arg, ifscope, wild_okay);
162 
163 	/* Check if we found a match but it's waiting to be disposed */
164 	if (inp != NULL && inp->inp_wantcnt == WNT_STOPUSING) {
165 		struct socket *so = inp->inp_socket;
166 
167 		socket_lock(so, 0);
168 
169 		if (so->so_usecount == 0) {
170 			if (inp->inp_state != INPCB_STATE_DEAD) {
171 				in6_pcbdetach(inp);
172 			}
173 			in_pcbdispose(inp);     /* will unlock & destroy */
174 			inp = NULL;
175 		} else {
176 			socket_unlock(so, 0);
177 		}
178 	}
179 
180 	return inp;
181 }
182 
183 /*
184  * Bind an INPCB to an address and/or port.  This routine should not alter
185  * the caller-supplied local address "nam" or remote address "remote".
186  */
187 int
in6_pcbbind(struct inpcb * inp,struct sockaddr * nam,struct sockaddr * remote,struct proc * p)188 in6_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct sockaddr *remote, struct proc *p)
189 {
190 	struct socket *__single so = inp->inp_socket;
191 	struct inpcbinfo *__single pcbinfo = inp->inp_pcbinfo;
192 	u_short lport = 0;
193 	int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
194 	ifnet_ref_t outif = NULL;
195 	struct sockaddr_in6 sin6;
196 	uint32_t lifscope = IFSCOPE_NONE;
197 	int error = 0;
198 #if XNU_TARGET_OS_OSX
199 	kauth_cred_t __single cred;
200 #endif /* XNU_TARGET_OS_OSX */
201 
202 	if (inp->inp_flags2 & INP2_BIND_IN_PROGRESS) {
203 		return EINVAL;
204 	}
205 	inp->inp_flags2 |= INP2_BIND_IN_PROGRESS;
206 
207 	if (TAILQ_EMPTY(&in6_ifaddrhead)) { /* XXX broken! */
208 		error = EADDRNOTAVAIL;
209 		goto done;
210 	}
211 	if (!(so->so_options & (SO_REUSEADDR | SO_REUSEPORT))) {
212 		wild = 1;
213 	}
214 
215 	in_pcb_check_management_entitled(inp);
216 	in_pcb_check_ultra_constrained_entitled(inp);
217 
218 	socket_unlock(so, 0); /* keep reference */
219 	lck_rw_lock_exclusive(&pcbinfo->ipi_lock);
220 	if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
221 		/* another thread completed the bind */
222 		lck_rw_done(&pcbinfo->ipi_lock);
223 		socket_lock(so, 0);
224 		error = EINVAL;
225 		goto done;
226 	}
227 
228 	SOCKADDR_ZERO(&sin6, sizeof(sin6));
229 	if (nam != NULL) {
230 		if (nam->sa_len != sizeof(struct sockaddr_in6)) {
231 			lck_rw_done(&pcbinfo->ipi_lock);
232 			socket_lock(so, 0);
233 			error = EINVAL;
234 			goto done;
235 		}
236 		/*
237 		 * family check.
238 		 */
239 		if (nam->sa_family != AF_INET6) {
240 			lck_rw_done(&pcbinfo->ipi_lock);
241 			socket_lock(so, 0);
242 			error = EAFNOSUPPORT;
243 			goto done;
244 		}
245 		lport = SIN6(nam)->sin6_port;
246 
247 		*(&sin6) = *SIN6(nam);
248 
249 		/* KAME hack: embed scopeid */
250 		if (in6_embedscope(&sin6.sin6_addr, &sin6, inp, NULL,
251 		    NULL, &lifscope) != 0) {
252 			lck_rw_done(&pcbinfo->ipi_lock);
253 			socket_lock(so, 0);
254 			error = EINVAL;
255 			goto done;
256 		}
257 
258 		/* Sanitize local copy for address searches */
259 		sin6.sin6_flowinfo = 0;
260 		sin6.sin6_port = 0;
261 		if (in6_embedded_scope) {
262 			sin6.sin6_scope_id = 0;
263 		}
264 
265 		if (IN6_IS_ADDR_MULTICAST(&sin6.sin6_addr)) {
266 			/*
267 			 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
268 			 * allow compepte duplication of binding if
269 			 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
270 			 * and a multicast address is bound on both
271 			 * new and duplicated sockets.
272 			 */
273 			if (so->so_options & SO_REUSEADDR) {
274 				reuseport = SO_REUSEADDR | SO_REUSEPORT;
275 			}
276 		} else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6.sin6_addr)) {
277 			struct ifaddr *__single ifa;
278 
279 			ifa = ifa_ifwithaddr(SA(&sin6));
280 			if (ifa == NULL) {
281 				lck_rw_done(&pcbinfo->ipi_lock);
282 				socket_lock(so, 0);
283 				error = EADDRNOTAVAIL;
284 				goto done;
285 			} else {
286 				/*
287 				 * XXX: bind to an anycast address might
288 				 * accidentally cause sending a packet with
289 				 * anycast source address.  We should allow
290 				 * to bind to a deprecated address, since
291 				 * the application dare to use it.
292 				 */
293 				IFA_LOCK_SPIN(ifa);
294 				if ((ifatoia6(ifa))->ia6_flags &
295 				    (IN6_IFF_ANYCAST | IN6_IFF_NOTREADY |
296 				    IN6_IFF_DETACHED | IN6_IFF_CLAT46)) {
297 					IFA_UNLOCK(ifa);
298 					ifa_remref(ifa);
299 					lck_rw_done(&pcbinfo->ipi_lock);
300 					socket_lock(so, 0);
301 					error = EADDRNOTAVAIL;
302 					goto done;
303 				}
304 				/*
305 				 * Opportunistically determine the outbound
306 				 * interface that may be used; this may not
307 				 * hold true if we end up using a route
308 				 * going over a different interface, e.g.
309 				 * when sending to a local address.  This
310 				 * will get updated again after sending.
311 				 */
312 				outif = ifa->ifa_ifp;
313 				IFA_UNLOCK(ifa);
314 				ifa_remref(ifa);
315 			}
316 		}
317 
318 #if SKYWALK
319 		if (inp->inp_flags2 & INP2_EXTERNAL_PORT) {
320 			// Extract the external flow info
321 			struct ns_flow_info nfi = {};
322 			int netns_error = necp_client_get_netns_flow_info(inp->necp_client_uuid,
323 			    &nfi);
324 			if (netns_error != 0) {
325 				lck_rw_done(&pcbinfo->ipi_lock);
326 				socket_lock(so, 0);
327 				error = netns_error;
328 				goto done;
329 			}
330 
331 			// Extract the reserved port
332 			u_int16_t reserved_lport = 0;
333 			if (nfi.nfi_laddr.sa.sa_family == AF_INET) {
334 				reserved_lport = nfi.nfi_laddr.sin.sin_port;
335 			} else if (nfi.nfi_laddr.sa.sa_family == AF_INET6) {
336 				reserved_lport = nfi.nfi_laddr.sin6.sin6_port;
337 			} else {
338 				lck_rw_done(&pcbinfo->ipi_lock);
339 				socket_lock(so, 0);
340 				error = EINVAL;
341 				goto done;
342 			}
343 
344 			// Validate or use the reserved port
345 			if (lport == 0) {
346 				lport = reserved_lport;
347 			} else if (lport != reserved_lport) {
348 				lck_rw_done(&pcbinfo->ipi_lock);
349 				socket_lock(so, 0);
350 				error = EINVAL;
351 				goto done;
352 			}
353 		}
354 
355 		/* Do not allow reserving a UDP port if remaining UDP port count is below 4096 */
356 		if (SOCK_PROTO(so) == IPPROTO_UDP && !allow_udp_port_exhaustion) {
357 			uint32_t current_reservations = 0;
358 			current_reservations = netns_lookup_reservations_count_in6(inp->in6p_laddr, IPPROTO_UDP);
359 			if (USHRT_MAX - UDP_RANDOM_PORT_RESERVE < current_reservations) {
360 				log(LOG_ERR, "UDP port not available, less than 4096 UDP ports left");
361 				lck_rw_done(&pcbinfo->ipi_lock);
362 				socket_lock(so, 0);
363 				error = EADDRNOTAVAIL;
364 				goto done;
365 			}
366 		}
367 
368 #endif /* SKYWALK */
369 
370 		if (lport != 0) {
371 			struct inpcb *__single t;
372 			uid_t u;
373 
374 #if XNU_TARGET_OS_OSX
375 			if (ntohs(lport) < IPV6PORT_RESERVED &&
376 			    !IN6_IS_ADDR_UNSPECIFIED(&sin6.sin6_addr) &&
377 			    !(inp->inp_flags2 & INP2_EXTERNAL_PORT)) {
378 				cred = kauth_cred_proc_ref(p);
379 				error = priv_check_cred(cred,
380 				    PRIV_NETINET_RESERVEDPORT, 0);
381 				kauth_cred_unref(&cred);
382 				if (error != 0) {
383 					lck_rw_done(&pcbinfo->ipi_lock);
384 					socket_lock(so, 0);
385 					error = EACCES;
386 					goto done;
387 				}
388 			}
389 #endif /* XNU_TARGET_OS_OSX */
390 			/*
391 			 * Check wether the process is allowed to bind to a restricted port
392 			 */
393 			if (!current_task_can_use_restricted_in_port(lport,
394 			    (uint8_t)SOCK_PROTO(so), PORT_FLAGS_BSD)) {
395 				lck_rw_done(&pcbinfo->ipi_lock);
396 				socket_lock(so, 0);
397 				error = EADDRINUSE;
398 				goto done;
399 			}
400 
401 			if (!IN6_IS_ADDR_MULTICAST(&sin6.sin6_addr) &&
402 			    (u = kauth_cred_getuid(so->so_cred)) != 0) {
403 				t = in6_pcblookup_local_and_cleanup(pcbinfo,
404 				    &sin6.sin6_addr, lport, sin6.sin6_scope_id,
405 				    INPLOOKUP_WILDCARD);
406 				if (t != NULL &&
407 				    (!IN6_IS_ADDR_UNSPECIFIED(&sin6.sin6_addr) ||
408 				    !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
409 				    !(t->inp_socket->so_options & SO_REUSEPORT)) &&
410 				    (u != kauth_cred_getuid(t->inp_socket->so_cred)) &&
411 				    !(t->inp_socket->so_flags & SOF_REUSESHAREUID) &&
412 				    (!(t->inp_flags2 & INP2_EXTERNAL_PORT) ||
413 				    !(inp->inp_flags2 & INP2_EXTERNAL_PORT) ||
414 				    uuid_compare(t->necp_client_uuid, inp->necp_client_uuid) != 0)) {
415 					lck_rw_done(&pcbinfo->ipi_lock);
416 					socket_lock(so, 0);
417 					error = EADDRINUSE;
418 					goto done;
419 				}
420 				if (!(inp->inp_flags & IN6P_IPV6_V6ONLY) &&
421 				    IN6_IS_ADDR_UNSPECIFIED(&sin6.sin6_addr)) {
422 					struct sockaddr_in sin;
423 
424 					in6_sin6_2_sin(&sin, &sin6);
425 					t = in_pcblookup_local_and_cleanup(
426 						pcbinfo, sin.sin_addr, lport,
427 						INPLOOKUP_WILDCARD);
428 					if (t != NULL &&
429 					    !(t->inp_socket->so_options & SO_REUSEPORT) &&
430 					    (kauth_cred_getuid(so->so_cred) !=
431 					    kauth_cred_getuid(t->inp_socket->so_cred)) &&
432 					    (t->inp_laddr.s_addr != INADDR_ANY ||
433 					    SOCK_DOM(so) == SOCK_DOM(t->inp_socket)) &&
434 					    (!(t->inp_flags2 & INP2_EXTERNAL_PORT) ||
435 					    !(inp->inp_flags2 & INP2_EXTERNAL_PORT) ||
436 					    uuid_compare(t->necp_client_uuid, inp->necp_client_uuid) != 0)) {
437 						lck_rw_done(&pcbinfo->ipi_lock);
438 						socket_lock(so, 0);
439 						error = EADDRINUSE;
440 						goto done;
441 					}
442 
443 #if SKYWALK
444 					VERIFY(!NETNS_TOKEN_VALID(
445 						    &inp->inp_wildcard_netns_token));
446 					if ((SOCK_PROTO(so) == IPPROTO_TCP ||
447 					    SOCK_PROTO(so) == IPPROTO_UDP) &&
448 					    !(inp->inp_flags2 & INP2_EXTERNAL_PORT)) {
449 						if (netns_reserve_in(&inp->
450 						    inp_wildcard_netns_token,
451 						    sin.sin_addr,
452 						    (uint8_t)SOCK_PROTO(so), lport,
453 						    NETNS_BSD, NULL) != 0) {
454 							lck_rw_done(&pcbinfo->ipi_lock);
455 							socket_lock(so, 0);
456 							error = EADDRINUSE;
457 							goto done;
458 						}
459 					}
460 #endif /* SKYWALK */
461 				}
462 			}
463 			t = in6_pcblookup_local_and_cleanup(pcbinfo,
464 			    &sin6.sin6_addr, lport, sin6.sin6_scope_id, wild);
465 			if (t != NULL &&
466 			    (reuseport & t->inp_socket->so_options) == 0 &&
467 			    (!(t->inp_flags2 & INP2_EXTERNAL_PORT) ||
468 			    !(inp->inp_flags2 & INP2_EXTERNAL_PORT) ||
469 			    uuid_compare(t->necp_client_uuid, inp->necp_client_uuid) != 0)) {
470 #if SKYWALK
471 				netns_release(&inp->inp_wildcard_netns_token);
472 #endif /* SKYWALK */
473 				lck_rw_done(&pcbinfo->ipi_lock);
474 				socket_lock(so, 0);
475 				error = EADDRINUSE;
476 				goto done;
477 			}
478 			if (!(inp->inp_flags & IN6P_IPV6_V6ONLY) &&
479 			    IN6_IS_ADDR_UNSPECIFIED(&sin6.sin6_addr)) {
480 				struct sockaddr_in sin;
481 
482 				in6_sin6_2_sin(&sin, &sin6);
483 				t = in_pcblookup_local_and_cleanup(pcbinfo,
484 				    sin.sin_addr, lport, wild);
485 				if (t != NULL && (reuseport &
486 				    t->inp_socket->so_options) == 0 &&
487 				    (t->inp_laddr.s_addr != INADDR_ANY ||
488 				    SOCK_DOM(so) == SOCK_DOM(t->inp_socket)) &&
489 				    (!(t->inp_flags2 & INP2_EXTERNAL_PORT) ||
490 				    !(inp->inp_flags2 & INP2_EXTERNAL_PORT) ||
491 				    uuid_compare(t->necp_client_uuid, inp->necp_client_uuid) != 0)) {
492 #if SKYWALK
493 					netns_release(&inp->inp_wildcard_netns_token);
494 #endif /* SKYWALK */
495 					lck_rw_done(&pcbinfo->ipi_lock);
496 					socket_lock(so, 0);
497 					error = EADDRINUSE;
498 					goto done;
499 				}
500 #if SKYWALK
501 				if ((SOCK_PROTO(so) == IPPROTO_TCP ||
502 				    SOCK_PROTO(so) == IPPROTO_UDP) &&
503 				    !(inp->inp_flags2 & INP2_EXTERNAL_PORT) &&
504 				    (!NETNS_TOKEN_VALID(
505 					    &inp->inp_wildcard_netns_token))) {
506 					if (netns_reserve_in(&inp->
507 					    inp_wildcard_netns_token,
508 					    sin.sin_addr,
509 					    (uint8_t)SOCK_PROTO(so), lport,
510 					    NETNS_BSD, NULL) != 0) {
511 						lck_rw_done(&pcbinfo->ipi_lock);
512 						socket_lock(so, 0);
513 						error = EADDRINUSE;
514 						goto done;
515 					}
516 				}
517 #endif /* SKYWALK */
518 			}
519 #if SKYWALK
520 			if ((SOCK_PROTO(so) == IPPROTO_TCP ||
521 			    SOCK_PROTO(so) == IPPROTO_UDP) &&
522 			    !(inp->inp_flags2 & INP2_EXTERNAL_PORT)) {
523 				if (netns_reserve_in6(&inp->inp_netns_token,
524 				    sin6.sin6_addr, (uint8_t)SOCK_PROTO(so), lport,
525 				    NETNS_BSD, NULL) != 0) {
526 					netns_release(&inp->inp_wildcard_netns_token);
527 					lck_rw_done(&pcbinfo->ipi_lock);
528 					socket_lock(so, 0);
529 					error = EADDRINUSE;
530 					goto done;
531 				}
532 			}
533 #endif /* SKYWALK */
534 		}
535 	}
536 
537 	socket_lock(so, 0);
538 	/*
539 	 * We unlocked socket's protocol lock for a long time.
540 	 * The socket might have been dropped/defuncted.
541 	 * Checking if world has changed since.
542 	 */
543 	if (inp->inp_state == INPCB_STATE_DEAD) {
544 #if SKYWALK
545 		netns_release(&inp->inp_netns_token);
546 		netns_release(&inp->inp_wildcard_netns_token);
547 #endif /* SKYWALK */
548 		lck_rw_done(&pcbinfo->ipi_lock);
549 		error = ECONNABORTED;
550 		goto done;
551 	}
552 
553 	/* check if the socket got bound when the lock was released */
554 	if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
555 #if SKYWALK
556 		netns_release(&inp->inp_netns_token);
557 		netns_release(&inp->inp_wildcard_netns_token);
558 #endif /* SKYWALK */
559 		lck_rw_done(&pcbinfo->ipi_lock);
560 		error = EINVAL;
561 		goto done;
562 	}
563 
564 	if (!IN6_IS_ADDR_UNSPECIFIED(&sin6.sin6_addr)) {
565 		inp->in6p_laddr = sin6.sin6_addr;
566 		inp->in6p_last_outifp = outif;
567 		inp->inp_lifscope = lifscope;
568 		in6_verify_ifscope(&inp->in6p_laddr, lifscope);
569 #if SKYWALK
570 		if (NETNS_TOKEN_VALID(&inp->inp_netns_token)) {
571 			netns_set_ifnet(&inp->inp_netns_token,
572 			    inp->in6p_last_outifp);
573 		}
574 #endif /* SKYWALK */
575 	}
576 
577 	if (lport == 0) {
578 		int e;
579 		if ((e = in6_pcbsetport(&inp->in6p_laddr, remote, inp, p, 1)) != 0) {
580 			/* Undo any address bind from above. */
581 #if SKYWALK
582 			netns_release(&inp->inp_netns_token);
583 			netns_release(&inp->inp_wildcard_netns_token);
584 #endif /* SKYWALK */
585 			inp->in6p_laddr = in6addr_any;
586 			inp->in6p_last_outifp = NULL;
587 			inp->inp_lifscope = IFSCOPE_NONE;
588 			lck_rw_done(&pcbinfo->ipi_lock);
589 			error = e;
590 			goto done;
591 		}
592 	} else {
593 		inp->inp_lport = lport;
594 		if (in_pcbinshash(inp, remote, 1) != 0) {
595 #if SKYWALK
596 			netns_release(&inp->inp_netns_token);
597 			netns_release(&inp->inp_wildcard_netns_token);
598 #endif /* SKYWALK */
599 			inp->in6p_laddr = in6addr_any;
600 			inp->inp_lifscope = IFSCOPE_NONE;
601 			inp->inp_lport = 0;
602 			inp->in6p_last_outifp = NULL;
603 			lck_rw_done(&pcbinfo->ipi_lock);
604 			error = EAGAIN;
605 			goto done;
606 		}
607 	}
608 	lck_rw_done(&pcbinfo->ipi_lock);
609 	sflt_notify(so, sock_evt_bound, NULL);
610 done:
611 	inp->inp_flags2 &= ~INP2_BIND_IN_PROGRESS;
612 	return error;
613 }
614 
615 /*
616  * Transform old in6_pcbconnect() into an inner subroutine for new
617  * in6_pcbconnect(); do some validity-checking on the remote address
618  * (in "nam") and then determine local host address (i.e., which
619  * interface) to use to access that remote host.
620  *
621  * This routine may alter the caller-supplied remote address "nam".
622  *
623  * This routine might return an ifp with a reference held if the caller
624  * provides a non-NULL outif, even in the error case.  The caller is
625  * responsible for releasing its reference.
626  */
627 int
in6_pcbladdr(struct inpcb * inp,struct sockaddr * nam,struct in6_addr * plocal_addr6,struct ifnet ** outif)628 in6_pcbladdr(struct inpcb *inp, struct sockaddr *nam,
629     struct in6_addr *plocal_addr6, struct ifnet **outif)
630 {
631 	struct in6_addr *__single addr6 = NULL;
632 	struct in6_addr src_storage;
633 	int error = 0;
634 	unsigned int ifscope;
635 
636 	if (outif != NULL) {
637 		*outif = NULL;
638 	}
639 	if (nam->sa_len != sizeof(struct sockaddr_in6)) {
640 		return EINVAL;
641 	}
642 	if (SIN6(nam)->sin6_family != AF_INET6) {
643 		return EAFNOSUPPORT;
644 	}
645 	if (SIN6(nam)->sin6_port == 0) {
646 		return EADDRNOTAVAIL;
647 	}
648 
649 	/* KAME hack: embed scopeid */
650 	if (in6_embedscope(&SIN6(nam)->sin6_addr, SIN6(nam), inp, NULL, NULL, IN6_NULL_IF_EMBEDDED_SCOPE(&SIN6(nam)->sin6_scope_id)) != 0) {
651 		return EINVAL;
652 	}
653 
654 	in_pcb_check_management_entitled(inp);
655 	in_pcb_check_ultra_constrained_entitled(inp);
656 
657 	if (!TAILQ_EMPTY(&in6_ifaddrhead)) {
658 		/*
659 		 * If the destination address is UNSPECIFIED addr,
660 		 * use the loopback addr, e.g ::1.
661 		 */
662 		if (IN6_IS_ADDR_UNSPECIFIED(&SIN6(nam)->sin6_addr)) {
663 			SIN6(nam)->sin6_addr = in6addr_loopback;
664 		}
665 	}
666 
667 	ifscope = (inp->inp_flags & INP_BOUND_IF) ?
668 	    inp->inp_boundifp->if_index : IFSCOPE_NONE;
669 
670 	/*
671 	 * XXX: in6_selectsrc might replace the bound local address
672 	 * with the address specified by setsockopt(IPV6_PKTINFO).
673 	 * Is it the intended behavior?
674 	 *
675 	 * in6_selectsrc() might return outif with its reference held
676 	 * even in the error case; caller always needs to release it
677 	 * if non-NULL.
678 	 */
679 	addr6 = in6_selectsrc(SIN6(nam), inp->in6p_outputopts, inp,
680 	    &inp->in6p_route, outif, &src_storage, ifscope, &error);
681 
682 	if (outif != NULL) {
683 		rtentry_ref_t rt = inp->in6p_route.ro_rt;
684 		/*
685 		 * If in6_selectsrc() returns a route, it should be one
686 		 * which points to the same ifp as outif.  Just in case
687 		 * it isn't, use the one from the route for consistency.
688 		 * Otherwise if there is no route, leave outif alone as
689 		 * it could still be useful to the caller.
690 		 */
691 		if (rt != NULL && rt->rt_ifp != *outif) {
692 			ifnet_reference(rt->rt_ifp);    /* for caller */
693 			if (*outif != NULL) {
694 				ifnet_release(*outif);
695 			}
696 			*outif = rt->rt_ifp;
697 		}
698 	}
699 
700 	if (addr6 == NULL) {
701 		if (outif != NULL && (*outif) != NULL &&
702 		    inp_restricted_send(inp, *outif)) {
703 			soevent(inp->inp_socket,
704 			    (SO_FILT_HINT_LOCKED | SO_FILT_HINT_IFDENIED));
705 			error = EHOSTUNREACH;
706 		}
707 		if (error == 0) {
708 			error = EADDRNOTAVAIL;
709 		}
710 		return error;
711 	}
712 
713 	*plocal_addr6 = *addr6;
714 	/*
715 	 * Don't do pcblookup call here; return interface in
716 	 * plocal_addr6 and exit to caller, that will do the lookup.
717 	 */
718 	return 0;
719 }
720 
721 /*
722  * Outer subroutine:
723  * Connect from a socket to a specified address.
724  * Both address and port must be specified in argument sin.
725  * If don't have a local address for this socket yet,
726  * then pick one.
727  */
728 int
in6_pcbconnect(struct inpcb * inp,struct sockaddr * nam,struct proc * p)729 in6_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct proc *p)
730 {
731 	struct in6_addr addr6;
732 	struct sockaddr_in6 *__single sin6 = SIN6(nam);
733 	struct inpcb *__single pcb;
734 	int error = 0;
735 	ifnet_ref_t outif = NULL;
736 	struct socket *so = inp->inp_socket;
737 
738 #if CONTENT_FILTER
739 	so->so_state_change_cnt++;
740 #endif
741 
742 	if (SOCK_CHECK_PROTO(so, IPPROTO_UDP) &&
743 	    sin6->sin6_port == htons(53) && !(so->so_flags1 & SOF1_DNS_COUNTED)) {
744 		so->so_flags1 |= SOF1_DNS_COUNTED;
745 		INC_ATOMIC_INT64_LIM(net_api_stats.nas_socket_inet_dgram_dns);
746 	}
747 
748 	/*
749 	 * Call inner routine, to assign local interface address.
750 	 * in6_pcbladdr() may automatically fill in sin6_scope_id.
751 	 *
752 	 * in6_pcbladdr() might return an ifp with its reference held
753 	 * even in the error case, so make sure that it's released
754 	 * whenever it's non-NULL.
755 	 */
756 	if ((error = in6_pcbladdr(inp, nam, &addr6, &outif)) != 0) {
757 		if (outif != NULL && inp_restricted_send(inp, outif)) {
758 			soevent(so,
759 			    (SO_FILT_HINT_LOCKED | SO_FILT_HINT_IFDENIED));
760 		}
761 		goto done;
762 	}
763 	socket_unlock(so, 0);
764 
765 	uint32_t lifscope;
766 	if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
767 		lifscope = inp->inp_lifscope;
768 	} else if (outif != NULL) {
769 		lifscope = in6_addr2scopeid(outif, &addr6);
770 	} else {
771 		lifscope = sin6->sin6_scope_id;
772 	}
773 
774 	pcb = in6_pcblookup_hash(inp->inp_pcbinfo, &sin6->sin6_addr,
775 	    sin6->sin6_port, sin6->sin6_scope_id, IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) ?
776 	    &addr6 : &inp->in6p_laddr, inp->inp_lport, lifscope, 0, NULL);
777 	socket_lock(so, 0);
778 	if (pcb != NULL) {
779 		in_pcb_checkstate(pcb, WNT_RELEASE, pcb == inp ? 1 : 0);
780 		error = EADDRINUSE;
781 		goto done;
782 	}
783 	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
784 		if (inp->inp_lport == 0) {
785 			error = in6_pcbbind(inp, NULL, nam, p);
786 			if (error) {
787 				goto done;
788 			}
789 		}
790 		inp->in6p_laddr = addr6;
791 		inp->in6p_last_outifp = outif;  /* no reference needed */
792 		if (IN6_IS_SCOPE_EMBED(&inp->in6p_laddr) &&
793 		    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sin6->sin6_addr)) {
794 			inp->inp_lifscope = sin6->sin6_scope_id;
795 		} else {
796 			inp->inp_lifscope = lifscope;
797 		}
798 		in6_verify_ifscope(&inp->in6p_laddr, inp->inp_lifscope);
799 #if SKYWALK
800 		if (NETNS_TOKEN_VALID(&inp->inp_netns_token)) {
801 			netns_set_ifnet(&inp->inp_netns_token,
802 			    inp->in6p_last_outifp);
803 		}
804 #endif /* SKYWALK */
805 		inp->in6p_flags |= INP_IN6ADDR_ANY;
806 	}
807 	if (!lck_rw_try_lock_exclusive(&inp->inp_pcbinfo->ipi_lock)) {
808 		/* lock inversion issue, mostly with udp multicast packets */
809 		socket_unlock(so, 0);
810 		lck_rw_lock_exclusive(&inp->inp_pcbinfo->ipi_lock);
811 		socket_lock(so, 0);
812 	}
813 	inp->in6p_faddr = sin6->sin6_addr;
814 	inp->inp_fport = sin6->sin6_port;
815 	inp->inp_fifscope = sin6->sin6_scope_id;
816 	in6_verify_ifscope(&inp->in6p_faddr, inp->inp_fifscope);
817 	if (nstat_collect && SOCK_PROTO(so) == IPPROTO_UDP) {
818 		nstat_pcb_invalidate_cache(inp);
819 	}
820 	in_pcbrehash(inp);
821 	lck_rw_done(&inp->inp_pcbinfo->ipi_lock);
822 
823 done:
824 	if (outif != NULL) {
825 		ifnet_release(outif);
826 	}
827 
828 	return error;
829 }
830 
831 void
in6_pcbdisconnect(struct inpcb * inp)832 in6_pcbdisconnect(struct inpcb *inp)
833 {
834 	struct socket *__single so = inp->inp_socket;
835 
836 #if CONTENT_FILTER
837 	if (so) {
838 		so->so_state_change_cnt++;
839 	}
840 #endif
841 
842 	if (!lck_rw_try_lock_exclusive(&inp->inp_pcbinfo->ipi_lock)) {
843 		/* lock inversion issue, mostly with udp multicast packets */
844 		socket_unlock(so, 0);
845 		lck_rw_lock_exclusive(&inp->inp_pcbinfo->ipi_lock);
846 		socket_lock(so, 0);
847 	}
848 	if (nstat_collect && SOCK_PROTO(so) == IPPROTO_UDP) {
849 		nstat_pcb_cache(inp);
850 	}
851 	bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
852 	inp->inp_fport = 0;
853 	/* clear flowinfo - RFC 6437 */
854 	inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
855 	in_pcbrehash(inp);
856 	lck_rw_done(&inp->inp_pcbinfo->ipi_lock);
857 	/*
858 	 * A multipath subflow socket would have its SS_NOFDREF set by default,
859 	 * so check for SOF_MP_SUBFLOW socket flag before detaching the PCB;
860 	 * when the socket is closed for real, SOF_MP_SUBFLOW would be cleared.
861 	 */
862 	if (!(so->so_flags & SOF_MP_SUBFLOW) && (so->so_state & SS_NOFDREF)) {
863 		in6_pcbdetach(inp);
864 	}
865 }
866 
867 void
in6_pcbdetach(struct inpcb * inp)868 in6_pcbdetach(struct inpcb *inp)
869 {
870 	struct socket *__single so = inp->inp_socket;
871 
872 	if (so->so_pcb == NULL) {
873 		/* PCB has been disposed */
874 		panic("%s: inp=%p so=%p proto=%d so_pcb is null!", __func__,
875 		    inp, so, SOCK_PROTO(so));
876 		/* NOTREACHED */
877 	}
878 
879 #if IPSEC
880 	if (inp->in6p_sp != NULL) {
881 		(void) ipsec6_delete_pcbpolicy(inp);
882 	}
883 #endif /* IPSEC */
884 
885 	if (inp->inp_stat != NULL && SOCK_PROTO(so) == IPPROTO_UDP) {
886 		if (inp->inp_stat->rxpackets == 0 && inp->inp_stat->txpackets == 0) {
887 			INC_ATOMIC_INT64_LIM(net_api_stats.nas_socket_inet6_dgram_no_data);
888 		}
889 	}
890 
891 	/*
892 	 * Let NetworkStatistics know this PCB is going away
893 	 * before we detach it.
894 	 */
895 	if (nstat_collect &&
896 	    (SOCK_PROTO(so) == IPPROTO_TCP || SOCK_PROTO(so) == IPPROTO_UDP)) {
897 		nstat_pcb_detach(inp);
898 	}
899 	/* mark socket state as dead */
900 	if (in_pcb_checkstate(inp, WNT_STOPUSING, 1) != WNT_STOPUSING) {
901 		panic("%s: so=%p proto=%d couldn't set to STOPUSING",
902 		    __func__, so, SOCK_PROTO(so));
903 		/* NOTREACHED */
904 	}
905 
906 #if SKYWALK
907 	/* Free up the port in the namespace registrar if not in TIME_WAIT */
908 	if (!(inp->inp_flags2 & INP2_TIMEWAIT)) {
909 		netns_release(&inp->inp_netns_token);
910 		netns_release(&inp->inp_wildcard_netns_token);
911 	}
912 #endif /* SKYWALK */
913 
914 	if (!(so->so_flags & SOF_PCBCLEARING)) {
915 		struct ip_moptions *__single imo;
916 		struct ip6_moptions *__single im6o;
917 
918 		inp->inp_vflag = 0;
919 		if (inp->in6p_options != NULL) {
920 			m_freem(inp->in6p_options);
921 			inp->in6p_options = NULL;
922 		}
923 		ip6_freepcbopts(inp->in6p_outputopts);
924 		inp->in6p_outputopts = NULL;
925 		ROUTE_RELEASE(&inp->in6p_route);
926 		/* free IPv4 related resources in case of mapped addr */
927 		if (inp->inp_options != NULL) {
928 			(void) m_free(inp->inp_options);
929 			inp->inp_options = NULL;
930 		}
931 		im6o = inp->in6p_moptions;
932 		inp->in6p_moptions = NULL;
933 		if (im6o != NULL) {
934 			IM6O_REMREF(im6o);
935 		}
936 		imo = inp->inp_moptions;
937 		inp->inp_moptions = NULL;
938 		if (imo != NULL) {
939 			IMO_REMREF(imo);
940 		}
941 
942 		sofreelastref(so, 0);
943 		inp->inp_state = INPCB_STATE_DEAD;
944 		/* makes sure we're not called twice from so_close */
945 		so->so_flags |= SOF_PCBCLEARING;
946 
947 		inpcb_gc_sched(inp->inp_pcbinfo, INPCB_TIMER_FAST);
948 	}
949 }
950 
951 struct sockaddr *
in6_sockaddr(in_port_t port,struct in6_addr * addr_p,uint32_t ifscope)952 in6_sockaddr(in_port_t port, struct in6_addr *addr_p, uint32_t ifscope)
953 {
954 	struct sockaddr_in6 *__single sin6;
955 
956 	sin6 = (struct sockaddr_in6 *)(alloc_sockaddr(sizeof(*sin6),
957 	    Z_WAITOK | Z_NOFAIL));
958 
959 	sin6->sin6_family = AF_INET6;
960 	sin6->sin6_port = port;
961 	sin6->sin6_addr = *addr_p;
962 
963 	/* would be good to use sa6_recoverscope(), except for locking  */
964 	if (IN6_IS_SCOPE_EMBED(&sin6->sin6_addr)) {
965 		sin6->sin6_scope_id = ifscope;
966 		if (in6_embedded_scope) {
967 			in6_verify_ifscope(&sin6->sin6_addr, ifscope);
968 			sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]);
969 		}
970 	} else {
971 		sin6->sin6_scope_id = 0;        /* XXX */
972 	}
973 	if (in6_embedded_scope && IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) {
974 		sin6->sin6_addr.s6_addr16[1] = 0;
975 	}
976 
977 	return SA(sin6);
978 }
979 
980 void
in6_sockaddr_s(in_port_t port,struct in6_addr * addr_p,struct sockaddr_in6 * sin6,uint32_t ifscope)981 in6_sockaddr_s(in_port_t port, struct in6_addr *addr_p,
982     struct sockaddr_in6 *sin6, uint32_t ifscope)
983 {
984 	SOCKADDR_ZERO(sin6, sizeof(*sin6));
985 	sin6->sin6_family = AF_INET6;
986 	sin6->sin6_len = sizeof(*sin6);
987 	sin6->sin6_port = port;
988 	sin6->sin6_addr = *addr_p;
989 
990 	/* would be good to use sa6_recoverscope(), except for locking  */
991 	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) {
992 		sin6->sin6_scope_id = ifscope;
993 		if (in6_embedded_scope) {
994 			in6_verify_ifscope(&sin6->sin6_addr, ifscope);
995 			sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]);
996 		}
997 	} else {
998 		sin6->sin6_scope_id = 0;        /* XXX */
999 	}
1000 	if (in6_embedded_scope && IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) {
1001 		sin6->sin6_addr.s6_addr16[1] = 0;
1002 	}
1003 }
1004 
1005 /*
1006  * The calling convention of in6_getsockaddr() and in6_getpeeraddr() was
1007  * modified to match the pru_sockaddr() and pru_peeraddr() entry points
1008  * in struct pr_usrreqs, so that protocols can just reference then directly
1009  * without the need for a wrapper function.
1010  */
1011 int
in6_getsockaddr(struct socket * so,struct sockaddr ** nam)1012 in6_getsockaddr(struct socket *so, struct sockaddr **nam)
1013 {
1014 	struct inpcb *__single inp;
1015 	struct in6_addr addr;
1016 	in_port_t port;
1017 
1018 	if ((inp = sotoinpcb(so)) == NULL) {
1019 		return EINVAL;
1020 	}
1021 
1022 	port = inp->inp_lport;
1023 	addr = inp->in6p_laddr;
1024 
1025 	*nam = in6_sockaddr(port, &addr, inp->inp_lifscope);
1026 	if (*nam == NULL) {
1027 		return ENOBUFS;
1028 	}
1029 	return 0;
1030 }
1031 
1032 int
in6_getsockaddr_s(struct socket * so,struct sockaddr_in6 * ss)1033 in6_getsockaddr_s(struct socket *so, struct sockaddr_in6 *ss)
1034 {
1035 	struct inpcb *__single inp;
1036 	struct in6_addr addr;
1037 	in_port_t port;
1038 
1039 	VERIFY(ss != NULL);
1040 	SOCKADDR_ZERO(ss, sizeof(*ss));
1041 
1042 	if ((inp = sotoinpcb(so)) == NULL) {
1043 		return EINVAL;
1044 	}
1045 
1046 	port = inp->inp_lport;
1047 	addr = inp->in6p_laddr;
1048 
1049 	in6_sockaddr_s(port, &addr, ss, inp->inp_lifscope);
1050 	return 0;
1051 }
1052 
1053 int
in6_getpeeraddr(struct socket * so,struct sockaddr ** nam)1054 in6_getpeeraddr(struct socket *so, struct sockaddr **nam)
1055 {
1056 	struct inpcb *__single inp;
1057 	struct in6_addr addr;
1058 	in_port_t port;
1059 
1060 	if ((inp = sotoinpcb(so)) == NULL) {
1061 		return EINVAL;
1062 	}
1063 
1064 	port = inp->inp_fport;
1065 	addr = inp->in6p_faddr;
1066 
1067 	*nam = in6_sockaddr(port, &addr, inp->inp_fifscope);
1068 	if (*nam == NULL) {
1069 		return ENOBUFS;
1070 	}
1071 	return 0;
1072 }
1073 
1074 int
in6_mapped_sockaddr(struct socket * so,struct sockaddr ** nam)1075 in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
1076 {
1077 	struct  inpcb *__single inp = sotoinpcb(so);
1078 	int     error;
1079 
1080 	if (inp == NULL) {
1081 		return EINVAL;
1082 	}
1083 	if (inp->inp_vflag & INP_IPV4) {
1084 		error = in_getsockaddr(so, nam);
1085 		if (error == 0) {
1086 			error = in6_sin_2_v4mapsin6_in_sock(nam);
1087 		}
1088 	} else {
1089 		/* scope issues will be handled in in6_getsockaddr(). */
1090 		error = in6_getsockaddr(so, nam);
1091 	}
1092 	return error;
1093 }
1094 
1095 int
in6_mapped_peeraddr(struct socket * so,struct sockaddr ** nam)1096 in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
1097 {
1098 	struct  inpcb *inp = sotoinpcb(so);
1099 	int     error;
1100 
1101 	if (inp == NULL) {
1102 		return EINVAL;
1103 	}
1104 	if (inp->inp_vflag & INP_IPV4) {
1105 		error = in_getpeeraddr(so, nam);
1106 		if (error == 0) {
1107 			error = in6_sin_2_v4mapsin6_in_sock(nam);
1108 		}
1109 	} else {
1110 		/* scope issues will be handled in in6_getpeeraddr(). */
1111 		error = in6_getpeeraddr(so, nam);
1112 	}
1113 	return error;
1114 }
1115 
1116 /*
1117  * Pass some notification to all connections of a protocol
1118  * associated with address dst.  The local address and/or port numbers
1119  * may be specified to limit the search.  The "usual action" will be
1120  * taken, depending on the ctlinput cmd.  The caller must filter any
1121  * cmds that are uninteresting (e.g., no error in the map).
1122  * Call the protocol specific routine (if any) to report
1123  * any errors for each matching socket.
1124  */
1125 void
in6_pcbnotify(struct inpcbinfo * pcbinfo,struct sockaddr * dst,u_int fport_arg,const struct sockaddr * src,u_int lport_arg,int cmd,void * cmdarg,void (* notify)(struct inpcb *,int))1126 in6_pcbnotify(struct inpcbinfo *pcbinfo, struct sockaddr *dst, u_int fport_arg,
1127     const struct sockaddr *src, u_int lport_arg, int cmd, void *cmdarg,
1128     void (*notify)(struct inpcb *, int))
1129 {
1130 	struct inpcbhead *__single head = pcbinfo->ipi_listhead;
1131 	struct inpcb *__single inp, *__single ninp;
1132 	struct sockaddr_in6 sa6_src;
1133 	struct sockaddr_in6 *__single sa6_dst;
1134 	uint16_t fport = (uint16_t)fport_arg, lport = (uint16_t)lport_arg;
1135 	u_int32_t flowinfo;
1136 	int errno;
1137 
1138 	if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6) {
1139 		return;
1140 	}
1141 
1142 	sa6_dst = SIN6(dst);
1143 	if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr)) {
1144 		return;
1145 	}
1146 
1147 	/*
1148 	 * note that src can be NULL when we get notify by local fragmentation.
1149 	 */
1150 	sa6_src = (src == NULL) ?
1151 	    sa6_any : *SIN6(src);
1152 	flowinfo = sa6_src.sin6_flowinfo;
1153 
1154 	/*
1155 	 * Redirects go to all references to the destination,
1156 	 * and use in6_rtchange to invalidate the route cache.
1157 	 * Dead host indications: also use in6_rtchange to invalidate
1158 	 * the cache, and deliver the error to all the sockets.
1159 	 * Otherwise, if we have knowledge of the local port and address,
1160 	 * deliver only to that socket.
1161 	 */
1162 	if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
1163 		fport = 0;
1164 		lport = 0;
1165 		bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
1166 
1167 		if (cmd != PRC_HOSTDEAD) {
1168 			notify = in6_rtchange;
1169 		}
1170 	}
1171 	errno = inet6ctlerrmap[cmd];
1172 	lck_rw_lock_shared(&pcbinfo->ipi_lock);
1173 	for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
1174 		ninp = LIST_NEXT(inp, inp_list);
1175 
1176 		if (!(inp->inp_vflag & INP_IPV6)) {
1177 			continue;
1178 		}
1179 
1180 		/*
1181 		 * If the error designates a new path MTU for a destination
1182 		 * and the application (associated with this socket) wanted to
1183 		 * know the value, notify. Note that we notify for all
1184 		 * disconnected sockets if the corresponding application
1185 		 * wanted. This is because some UDP applications keep sending
1186 		 * sockets disconnected.
1187 		 * XXX: should we avoid to notify the value to TCP sockets?
1188 		 */
1189 		if (cmd == PRC_MSGSIZE && cmdarg != NULL) {
1190 			socket_lock(inp->inp_socket, 1);
1191 			ip6_notify_pmtu(inp, SIN6(dst),
1192 			    (u_int32_t *)cmdarg);
1193 			socket_unlock(inp->inp_socket, 1);
1194 		}
1195 
1196 		/*
1197 		 * Detect if we should notify the error. If no source and
1198 		 * destination ports are specifed, but non-zero flowinfo and
1199 		 * local address match, notify the error. This is the case
1200 		 * when the error is delivered with an encrypted buffer
1201 		 * by ESP. Otherwise, just compare addresses and ports
1202 		 * as usual.
1203 		 */
1204 		if (lport == 0 && fport == 0 && flowinfo &&
1205 		    inp->inp_socket != NULL &&
1206 		    flowinfo == (inp->inp_flow & IPV6_FLOWLABEL_MASK) &&
1207 		    in6_are_addr_equal_scoped(&inp->in6p_laddr, &sa6_src.sin6_addr, inp->inp_lifscope, sa6_src.sin6_scope_id)) {
1208 			goto do_notify;
1209 		} else if (!in6_are_addr_equal_scoped(&inp->in6p_faddr, &sa6_dst->sin6_addr,
1210 		    inp->inp_fifscope, sa6_dst->sin6_scope_id) || inp->inp_socket == NULL ||
1211 		    (lport && inp->inp_lport != lport) ||
1212 		    (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
1213 		    !in6_are_addr_equal_scoped(&inp->in6p_laddr, &sa6_src.sin6_addr, inp->inp_lifscope, sa6_src.sin6_scope_id)) || (fport && inp->inp_fport != fport)) {
1214 			continue;
1215 		}
1216 
1217 do_notify:
1218 		if (notify) {
1219 			if (in_pcb_checkstate(inp, WNT_ACQUIRE, 0) ==
1220 			    WNT_STOPUSING) {
1221 				continue;
1222 			}
1223 			socket_lock(inp->inp_socket, 1);
1224 			(*notify)(inp, errno);
1225 			(void) in_pcb_checkstate(inp, WNT_RELEASE, 1);
1226 			socket_unlock(inp->inp_socket, 1);
1227 		}
1228 	}
1229 	lck_rw_done(&pcbinfo->ipi_lock);
1230 }
1231 
1232 /*
1233  * Lookup a PCB based on the local address and port.
1234  */
1235 struct inpcb *
in6_pcblookup_local(struct inpcbinfo * pcbinfo,struct in6_addr * laddr,u_int lport_arg,uint32_t ifscope,int wild_okay)1236 in6_pcblookup_local(struct inpcbinfo *pcbinfo, struct in6_addr *laddr,
1237     u_int lport_arg, uint32_t ifscope, int wild_okay)
1238 {
1239 	struct inpcb *__single inp;
1240 	int matchwild = 3, wildcard;
1241 	uint16_t lport = (uint16_t)lport_arg;
1242 	struct inpcbporthead *__single porthash;
1243 	struct inpcb *__single match = NULL;
1244 	struct inpcbport *__single phd;
1245 
1246 	if (!wild_okay) {
1247 		struct inpcbhead *__single head;
1248 		/*
1249 		 * Look for an unconnected (wildcard foreign addr) PCB that
1250 		 * matches the local address and port we're looking for.
1251 		 */
1252 		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport, 0,
1253 		    pcbinfo->ipi_hashmask)];
1254 		LIST_FOREACH(inp, head, inp_hash) {
1255 			if (!(inp->inp_vflag & INP_IPV6)) {
1256 				continue;
1257 			}
1258 			if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
1259 			    in6_are_addr_equal_scoped(&inp->in6p_laddr, laddr, inp->inp_lifscope, ifscope) &&
1260 			    inp->inp_lport == lport) {
1261 				/*
1262 				 * Found.
1263 				 */
1264 				return inp;
1265 			}
1266 		}
1267 		/*
1268 		 * Not found.
1269 		 */
1270 		return NULL;
1271 	}
1272 	/*
1273 	 * Best fit PCB lookup.
1274 	 *
1275 	 * First see if this local port is in use by looking on the
1276 	 * port hash list.
1277 	 */
1278 	porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
1279 	    pcbinfo->ipi_porthashmask)];
1280 	LIST_FOREACH(phd, porthash, phd_hash) {
1281 		if (phd->phd_port == lport) {
1282 			break;
1283 		}
1284 	}
1285 
1286 	if (phd != NULL) {
1287 		/*
1288 		 * Port is in use by one or more PCBs. Look for best
1289 		 * fit.
1290 		 */
1291 		LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
1292 			wildcard = 0;
1293 			if (!(inp->inp_vflag & INP_IPV6)) {
1294 				continue;
1295 			}
1296 			if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1297 				wildcard++;
1298 			}
1299 			if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1300 				if (IN6_IS_ADDR_UNSPECIFIED(laddr)) {
1301 					wildcard++;
1302 				} else if (!in6_are_addr_equal_scoped(
1303 					    &inp->in6p_laddr, laddr, inp->inp_lifscope, ifscope)) {
1304 					continue;
1305 				}
1306 			} else {
1307 				if (!IN6_IS_ADDR_UNSPECIFIED(laddr)) {
1308 					wildcard++;
1309 				}
1310 			}
1311 			if (wildcard < matchwild) {
1312 				match = inp;
1313 				matchwild = wildcard;
1314 				if (matchwild == 0) {
1315 					break;
1316 				}
1317 			}
1318 		}
1319 	}
1320 	return match;
1321 }
1322 
1323 /*
1324  * Check for alternatives when higher level complains
1325  * about service problems.  For now, invalidate cached
1326  * routing information.  If the route was created dynamically
1327  * (by a redirect), time to try a default gateway again.
1328  */
1329 void
in6_losing(struct inpcb * in6p)1330 in6_losing(struct inpcb *in6p)
1331 {
1332 	rtentry_ref_t rt;
1333 
1334 	if ((rt = in6p->in6p_route.ro_rt) != NULL) {
1335 		RT_LOCK(rt);
1336 		if (rt->rt_flags & RTF_DYNAMIC) {
1337 			/*
1338 			 * Prevent another thread from modifying rt_key,
1339 			 * rt_gateway via rt_setgate() after the rt_lock
1340 			 * is dropped by marking the route as defunct.
1341 			 */
1342 			rt->rt_flags |= RTF_CONDEMNED;
1343 			RT_UNLOCK(rt);
1344 			(void) rtrequest(RTM_DELETE, rt_key(rt),
1345 			    rt->rt_gateway, rt_mask(rt), rt->rt_flags, NULL);
1346 		} else {
1347 			RT_UNLOCK(rt);
1348 		}
1349 		/*
1350 		 * A new route can be allocated
1351 		 * the next time output is attempted.
1352 		 */
1353 	}
1354 	ROUTE_RELEASE(&in6p->in6p_route);
1355 }
1356 
1357 /*
1358  * After a routing change, flush old routing
1359  * and allocate a (hopefully) better one.
1360  */
1361 void
in6_rtchange(struct inpcb * inp,int errno)1362 in6_rtchange(struct inpcb *inp, int errno)
1363 {
1364 #pragma unused(errno)
1365 	/*
1366 	 * A new route can be allocated the next time
1367 	 * output is attempted.
1368 	 */
1369 	ROUTE_RELEASE(&inp->in6p_route);
1370 }
1371 
1372 /*
1373  * Check if PCB exists hash list. Also returns uid and gid of socket
1374  */
1375 int
in6_pcblookup_hash_exists(struct inpcbinfo * pcbinfo,struct in6_addr * faddr,u_int fport_arg,uint32_t fifscope,struct in6_addr * laddr,u_int lport_arg,uint32_t lifscope,int wildcard,uid_t * uid,gid_t * gid,struct ifnet * ifp,bool relaxed)1376 in6_pcblookup_hash_exists(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1377     u_int fport_arg, uint32_t fifscope, struct in6_addr *laddr, u_int lport_arg, uint32_t lifscope, int wildcard,
1378     uid_t *uid, gid_t *gid, struct ifnet *ifp, bool relaxed)
1379 {
1380 	struct inpcbhead *__single head;
1381 	struct inpcb *__single inp;
1382 	uint16_t fport = (uint16_t)fport_arg, lport = (uint16_t)lport_arg;
1383 	int found;
1384 
1385 	*uid = UID_MAX;
1386 	*gid = GID_MAX;
1387 
1388 	lck_rw_lock_shared(&pcbinfo->ipi_lock);
1389 
1390 	/*
1391 	 * First look for an exact match.
1392 	 */
1393 	head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr->s6_addr32[3] /* XXX */,
1394 	    lport, fport, pcbinfo->ipi_hashmask)];
1395 	LIST_FOREACH(inp, head, inp_hash) {
1396 		if (!(inp->inp_vflag & INP_IPV6)) {
1397 			continue;
1398 		}
1399 
1400 		if (inp_restricted_recv(inp, ifp)) {
1401 			continue;
1402 		}
1403 
1404 #if NECP
1405 		if (!necp_socket_is_allowed_to_recv_on_interface(inp, ifp)) {
1406 			continue;
1407 		}
1408 #endif /* NECP */
1409 
1410 		if (((in6_are_addr_equal_scoped(&inp->in6p_faddr, faddr, inp->inp_fifscope, fifscope) &&
1411 		    in6_are_addr_equal_scoped(&inp->in6p_laddr, laddr, inp->inp_lifscope, lifscope)) ||
1412 		    (relaxed &&
1413 		    IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
1414 		    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr))) &&
1415 		    inp->inp_fport == fport &&
1416 		    inp->inp_lport == lport) {
1417 			if ((found = (inp->inp_socket != NULL))) {
1418 				/*
1419 				 * Found. Check if pcb is still valid
1420 				 */
1421 				*uid = kauth_cred_getuid(
1422 					inp->inp_socket->so_cred);
1423 				*gid = kauth_cred_getgid(
1424 					inp->inp_socket->so_cred);
1425 			}
1426 			lck_rw_done(&pcbinfo->ipi_lock);
1427 			return found;
1428 		}
1429 	}
1430 	if (wildcard) {
1431 		struct inpcb *__single local_wild = NULL;
1432 
1433 		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport, 0,
1434 		    pcbinfo->ipi_hashmask)];
1435 		LIST_FOREACH(inp, head, inp_hash) {
1436 			if (!(inp->inp_vflag & INP_IPV6)) {
1437 				continue;
1438 			}
1439 
1440 			if (inp_restricted_recv(inp, ifp)) {
1441 				continue;
1442 			}
1443 
1444 #if NECP
1445 			if (!necp_socket_is_allowed_to_recv_on_interface(inp, ifp)) {
1446 				continue;
1447 			}
1448 #endif /* NECP */
1449 
1450 			if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
1451 			    inp->inp_lport == lport) {
1452 				if (in6_are_addr_equal_scoped(&inp->in6p_laddr,
1453 				    laddr, inp->inp_lifscope, lifscope) ||
1454 				    (relaxed && IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr))) {
1455 					found = (inp->inp_socket != NULL);
1456 					if (found) {
1457 						*uid = kauth_cred_getuid(
1458 							inp->inp_socket->so_cred);
1459 						*gid = kauth_cred_getgid(
1460 							inp->inp_socket->so_cred);
1461 					}
1462 					lck_rw_done(&pcbinfo->ipi_lock);
1463 					return found;
1464 				} else if (IN6_IS_ADDR_UNSPECIFIED(
1465 					    &inp->in6p_laddr)) {
1466 					local_wild = inp;
1467 				}
1468 			}
1469 		}
1470 		if (local_wild) {
1471 			if ((found = (local_wild->inp_socket != NULL))) {
1472 				*uid = kauth_cred_getuid(
1473 					local_wild->inp_socket->so_cred);
1474 				*gid = kauth_cred_getgid(
1475 					local_wild->inp_socket->so_cred);
1476 			}
1477 			lck_rw_done(&pcbinfo->ipi_lock);
1478 			return found;
1479 		}
1480 	}
1481 
1482 	/*
1483 	 * Not found.
1484 	 */
1485 	lck_rw_done(&pcbinfo->ipi_lock);
1486 	return 0;
1487 }
1488 
1489 /*
1490  * Lookup PCB in hash list.
1491  */
1492 struct inpcb *
in6_pcblookup_hash(struct inpcbinfo * pcbinfo,struct in6_addr * faddr,u_int fport_arg,uint32_t fifscope,struct in6_addr * laddr,u_int lport_arg,uint32_t lifscope,int wildcard,struct ifnet * ifp)1493 in6_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1494     u_int fport_arg, uint32_t fifscope, struct in6_addr *laddr, u_int lport_arg, uint32_t lifscope, int wildcard,
1495     struct ifnet *ifp)
1496 {
1497 	struct inpcbhead *__single head;
1498 	struct inpcb *__single inp;
1499 	uint16_t fport = (uint16_t)fport_arg, lport = (uint16_t)lport_arg;
1500 
1501 	lck_rw_lock_shared(&pcbinfo->ipi_lock);
1502 
1503 	/*
1504 	 * First look for an exact match.
1505 	 */
1506 	head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr->s6_addr32[3] /* XXX */,
1507 	    lport, fport, pcbinfo->ipi_hashmask)];
1508 	LIST_FOREACH(inp, head, inp_hash) {
1509 		if (!(inp->inp_vflag & INP_IPV6)) {
1510 			continue;
1511 		}
1512 
1513 		if (inp_restricted_recv(inp, ifp)) {
1514 			continue;
1515 		}
1516 
1517 #if NECP
1518 		if (!necp_socket_is_allowed_to_recv_on_interface(inp, ifp)) {
1519 			continue;
1520 		}
1521 #endif /* NECP */
1522 
1523 		if (in6_are_addr_equal_scoped(&inp->in6p_faddr, faddr, inp->inp_fifscope, fifscope) &&
1524 		    in6_are_addr_equal_scoped(&inp->in6p_laddr, laddr, inp->inp_lifscope, lifscope) &&
1525 		    inp->inp_fport == fport &&
1526 		    inp->inp_lport == lport) {
1527 			/*
1528 			 * Found. Check if pcb is still valid
1529 			 */
1530 			if (in_pcb_checkstate(inp, WNT_ACQUIRE, 0) !=
1531 			    WNT_STOPUSING) {
1532 				lck_rw_done(&pcbinfo->ipi_lock);
1533 				return inp;
1534 			} else {
1535 				/* it's there but dead, say it isn't found */
1536 				lck_rw_done(&pcbinfo->ipi_lock);
1537 				return NULL;
1538 			}
1539 		}
1540 	}
1541 	if (wildcard) {
1542 		struct inpcb *__single local_wild = NULL;
1543 
1544 		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport, 0,
1545 		    pcbinfo->ipi_hashmask)];
1546 		LIST_FOREACH(inp, head, inp_hash) {
1547 			if (!(inp->inp_vflag & INP_IPV6)) {
1548 				continue;
1549 			}
1550 
1551 			if (inp_restricted_recv(inp, ifp)) {
1552 				continue;
1553 			}
1554 
1555 #if NECP
1556 			if (!necp_socket_is_allowed_to_recv_on_interface(inp, ifp)) {
1557 				continue;
1558 			}
1559 #endif /* NECP */
1560 
1561 			if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
1562 			    inp->inp_lport == lport) {
1563 				if (in6_are_addr_equal_scoped(&inp->in6p_laddr,
1564 				    laddr, inp->inp_lifscope, lifscope)) {
1565 					if (in_pcb_checkstate(inp, WNT_ACQUIRE,
1566 					    0) != WNT_STOPUSING) {
1567 						lck_rw_done(&pcbinfo->ipi_lock);
1568 						return inp;
1569 					} else {
1570 						/* dead; say it isn't found */
1571 						lck_rw_done(&pcbinfo->ipi_lock);
1572 						return NULL;
1573 					}
1574 				} else if (IN6_IS_ADDR_UNSPECIFIED(
1575 					    &inp->in6p_laddr)) {
1576 					local_wild = inp;
1577 				}
1578 			}
1579 		}
1580 		if (local_wild && in_pcb_checkstate(local_wild,
1581 		    WNT_ACQUIRE, 0) != WNT_STOPUSING) {
1582 			lck_rw_done(&pcbinfo->ipi_lock);
1583 			return local_wild;
1584 		} else {
1585 			lck_rw_done(&pcbinfo->ipi_lock);
1586 			return NULL;
1587 		}
1588 	}
1589 
1590 	/*
1591 	 * Not found.
1592 	 */
1593 	lck_rw_done(&pcbinfo->ipi_lock);
1594 	return NULL;
1595 }
1596 
1597 void
init_sin6(struct sockaddr_in6 * sin6,struct mbuf * m)1598 init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m)
1599 {
1600 	struct ip6_hdr *__single ip;
1601 
1602 	ip = mtod(m, struct ip6_hdr *);
1603 	SOCKADDR_ZERO(sin6, sizeof(*sin6));
1604 	sin6->sin6_len = sizeof(*sin6);
1605 	sin6->sin6_family = AF_INET6;
1606 	sin6->sin6_addr = ip->ip6_src;
1607 	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) {
1608 		if (in6_embedded_scope) {
1609 			sin6->sin6_addr.s6_addr16[1] = 0;
1610 		}
1611 		if ((m->m_pkthdr.pkt_flags & (PKTF_LOOP | PKTF_IFAINFO)) ==
1612 		    (PKTF_LOOP | PKTF_IFAINFO)) {
1613 			sin6->sin6_scope_id = m->m_pkthdr.src_ifindex;
1614 		} else if (m->m_pkthdr.rcvif != NULL) {
1615 			sin6->sin6_scope_id = m->m_pkthdr.rcvif->if_index;
1616 		}
1617 	}
1618 }
1619 
1620 /*
1621  * The following routines implement this scheme:
1622  *
1623  * Callers of ip6_output() that intend to cache the route in the inpcb pass
1624  * a local copy of the struct route to ip6_output().  Using a local copy of
1625  * the cached route significantly simplifies things as IP no longer has to
1626  * worry about having exclusive access to the passed in struct route, since
1627  * it's defined in the caller's stack; in essence, this allows for a lock-
1628  * less operation when updating the struct route at the IP level and below,
1629  * whenever necessary. The scheme works as follows:
1630  *
1631  * Prior to dropping the socket's lock and calling ip6_output(), the caller
1632  * copies the struct route from the inpcb into its stack, and adds a reference
1633  * to the cached route entry, if there was any.  The socket's lock is then
1634  * dropped and ip6_output() is called with a pointer to the copy of struct
1635  * route defined on the stack (not to the one in the inpcb.)
1636  *
1637  * Upon returning from ip6_output(), the caller then acquires the socket's
1638  * lock and synchronizes the cache; if there is no route cached in the inpcb,
1639  * it copies the local copy of struct route (which may or may not contain any
1640  * route) back into the cache; otherwise, if the inpcb has a route cached in
1641  * it, the one in the local copy will be freed, if there's any.  Trashing the
1642  * cached route in the inpcb can be avoided because ip6_output() is single-
1643  * threaded per-PCB (i.e. multiple transmits on a PCB are always serialized
1644  * by the socket/transport layer.)
1645  */
1646 void
in6p_route_copyout(struct inpcb * inp,struct route_in6 * dst)1647 in6p_route_copyout(struct inpcb *inp, struct route_in6 *dst)
1648 {
1649 	struct route_in6 *__single src = &inp->in6p_route;
1650 
1651 	socket_lock_assert_owned(inp->inp_socket);
1652 
1653 	/* Minor sanity check */
1654 	if (src->ro_rt != NULL && rt_key(src->ro_rt)->sa_family != AF_INET6) {
1655 		panic("%s: wrong or corrupted route: %p", __func__, src);
1656 	}
1657 
1658 	route_copyout((struct route *)dst, (struct route *)src, sizeof(*dst));
1659 }
1660 
1661 void
in6p_route_copyin(struct inpcb * inp,struct route_in6 * src)1662 in6p_route_copyin(struct inpcb *inp, struct route_in6 *src)
1663 {
1664 	struct route_in6 *__single dst = &inp->in6p_route;
1665 
1666 	socket_lock_assert_owned(inp->inp_socket);
1667 
1668 	/* Minor sanity check */
1669 	if (src->ro_rt != NULL && rt_key(src->ro_rt)->sa_family != AF_INET6) {
1670 		panic("%s: wrong or corrupted route: %p", __func__, src);
1671 	}
1672 
1673 	route_copyin((struct route *)src, (struct route *)dst, sizeof(*src));
1674 }
1675