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