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