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 int error = 0;
196 #if XNU_TARGET_OS_OSX
197 kauth_cred_t cred;
198 #endif /* XNU_TARGET_OS_OSX */
199
200 if (inp->inp_flags2 & INP2_BIND_IN_PROGRESS) {
201 return EINVAL;
202 }
203 inp->inp_flags2 |= INP2_BIND_IN_PROGRESS;
204
205 if (TAILQ_EMPTY(&in6_ifaddrhead)) { /* XXX broken! */
206 error = EADDRNOTAVAIL;
207 goto done;
208 }
209 if (!(so->so_options & (SO_REUSEADDR | SO_REUSEPORT))) {
210 wild = 1;
211 }
212
213 in_pcb_check_management_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 bzero(&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 *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 (((struct in6_ifaddr *)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 *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, 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, 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 inp->inp_flags2 &= ~INP2_BIND_IN_PROGRESS;
609 return error;
610 }
611
612 /*
613 * Transform old in6_pcbconnect() into an inner subroutine for new
614 * in6_pcbconnect(); do some validity-checking on the remote address
615 * (in "nam") and then determine local host address (i.e., which
616 * interface) to use to access that remote host.
617 *
618 * This routine may alter the caller-supplied remote address "nam".
619 *
620 * This routine might return an ifp with a reference held if the caller
621 * provides a non-NULL outif, even in the error case. The caller is
622 * responsible for releasing its reference.
623 */
624 int
in6_pcbladdr(struct inpcb * inp,struct sockaddr * nam,struct in6_addr * plocal_addr6,struct ifnet ** outif)625 in6_pcbladdr(struct inpcb *inp, struct sockaddr *nam,
626 struct in6_addr *plocal_addr6, struct ifnet **outif)
627 {
628 struct in6_addr *addr6 = NULL;
629 struct in6_addr src_storage;
630 int error = 0;
631 unsigned int ifscope;
632
633 if (outif != NULL) {
634 *outif = NULL;
635 }
636 if (nam->sa_len != sizeof(struct sockaddr_in6)) {
637 return EINVAL;
638 }
639 if (SIN6(nam)->sin6_family != AF_INET6) {
640 return EAFNOSUPPORT;
641 }
642 if (SIN6(nam)->sin6_port == 0) {
643 return EADDRNOTAVAIL;
644 }
645
646 /* KAME hack: embed scopeid */
647 if (in6_embedscope(&SIN6(nam)->sin6_addr, SIN6(nam), inp, NULL, NULL, IN6_NULL_IF_EMBEDDED_SCOPE(&SIN6(nam)->sin6_scope_id)) != 0) {
648 return EINVAL;
649 }
650
651 in_pcb_check_management_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 struct rtentry *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 *sin6 = (struct sockaddr_in6 *)(void *)nam;
729 struct inpcb *pcb;
730 int error = 0;
731 struct ifnet *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 (so->so_proto->pr_protocol == 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, 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 *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 *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 *imo;
912 struct ip6_moptions *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 *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 (struct sockaddr *)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 bzero(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 *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 *inp;
1032 struct in6_addr addr;
1033 in_port_t port;
1034
1035 VERIFY(ss != NULL);
1036 bzero(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 *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 *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 *head = pcbinfo->ipi_listhead;
1127 struct inpcb *inp, *ninp;
1128 struct sockaddr_in6 sa6_src, *sa6_dst;
1129 uint16_t fport = (uint16_t)fport_arg, lport = (uint16_t)lport_arg;
1130 u_int32_t flowinfo;
1131 int errno;
1132
1133 if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6) {
1134 return;
1135 }
1136
1137 sa6_dst = (struct sockaddr_in6 *)(void *)dst;
1138 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr)) {
1139 return;
1140 }
1141
1142 /*
1143 * note that src can be NULL when we get notify by local fragmentation.
1144 */
1145 sa6_src = (src == NULL) ?
1146 sa6_any : *(struct sockaddr_in6 *)(uintptr_t)(size_t)src;
1147 flowinfo = sa6_src.sin6_flowinfo;
1148
1149 /*
1150 * Redirects go to all references to the destination,
1151 * and use in6_rtchange to invalidate the route cache.
1152 * Dead host indications: also use in6_rtchange to invalidate
1153 * the cache, and deliver the error to all the sockets.
1154 * Otherwise, if we have knowledge of the local port and address,
1155 * deliver only to that socket.
1156 */
1157 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
1158 fport = 0;
1159 lport = 0;
1160 bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
1161
1162 if (cmd != PRC_HOSTDEAD) {
1163 notify = in6_rtchange;
1164 }
1165 }
1166 errno = inet6ctlerrmap[cmd];
1167 lck_rw_lock_shared(&pcbinfo->ipi_lock);
1168 for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
1169 ninp = LIST_NEXT(inp, inp_list);
1170
1171 if (!(inp->inp_vflag & INP_IPV6)) {
1172 continue;
1173 }
1174
1175 /*
1176 * If the error designates a new path MTU for a destination
1177 * and the application (associated with this socket) wanted to
1178 * know the value, notify. Note that we notify for all
1179 * disconnected sockets if the corresponding application
1180 * wanted. This is because some UDP applications keep sending
1181 * sockets disconnected.
1182 * XXX: should we avoid to notify the value to TCP sockets?
1183 */
1184 if (cmd == PRC_MSGSIZE && cmdarg != NULL) {
1185 socket_lock(inp->inp_socket, 1);
1186 ip6_notify_pmtu(inp, (struct sockaddr_in6 *)(void *)dst,
1187 (u_int32_t *)cmdarg);
1188 socket_unlock(inp->inp_socket, 1);
1189 }
1190
1191 /*
1192 * Detect if we should notify the error. If no source and
1193 * destination ports are specifed, but non-zero flowinfo and
1194 * local address match, notify the error. This is the case
1195 * when the error is delivered with an encrypted buffer
1196 * by ESP. Otherwise, just compare addresses and ports
1197 * as usual.
1198 */
1199 if (lport == 0 && fport == 0 && flowinfo &&
1200 inp->inp_socket != NULL &&
1201 flowinfo == (inp->inp_flow & IPV6_FLOWLABEL_MASK) &&
1202 in6_are_addr_equal_scoped(&inp->in6p_laddr, &sa6_src.sin6_addr, inp->inp_lifscope, sa6_src.sin6_scope_id)) {
1203 goto do_notify;
1204 } else if (!in6_are_addr_equal_scoped(&inp->in6p_faddr, &sa6_dst->sin6_addr,
1205 inp->inp_fifscope, sa6_dst->sin6_scope_id) || inp->inp_socket == NULL ||
1206 (lport && inp->inp_lport != lport) ||
1207 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
1208 !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)) {
1209 continue;
1210 }
1211
1212 do_notify:
1213 if (notify) {
1214 if (in_pcb_checkstate(inp, WNT_ACQUIRE, 0) ==
1215 WNT_STOPUSING) {
1216 continue;
1217 }
1218 socket_lock(inp->inp_socket, 1);
1219 (*notify)(inp, errno);
1220 (void) in_pcb_checkstate(inp, WNT_RELEASE, 1);
1221 socket_unlock(inp->inp_socket, 1);
1222 }
1223 }
1224 lck_rw_done(&pcbinfo->ipi_lock);
1225 }
1226
1227 /*
1228 * Lookup a PCB based on the local address and port.
1229 */
1230 struct inpcb *
in6_pcblookup_local(struct inpcbinfo * pcbinfo,struct in6_addr * laddr,u_int lport_arg,uint32_t ifscope,int wild_okay)1231 in6_pcblookup_local(struct inpcbinfo *pcbinfo, struct in6_addr *laddr,
1232 u_int lport_arg, uint32_t ifscope, int wild_okay)
1233 {
1234 struct inpcb *inp;
1235 int matchwild = 3, wildcard;
1236 uint16_t lport = (uint16_t)lport_arg;
1237 struct inpcbporthead *porthash;
1238 struct inpcb *match = NULL;
1239 struct inpcbport *phd;
1240
1241 if (!wild_okay) {
1242 struct inpcbhead *head;
1243 /*
1244 * Look for an unconnected (wildcard foreign addr) PCB that
1245 * matches the local address and port we're looking for.
1246 */
1247 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport, 0,
1248 pcbinfo->ipi_hashmask)];
1249 LIST_FOREACH(inp, head, inp_hash) {
1250 if (!(inp->inp_vflag & INP_IPV6)) {
1251 continue;
1252 }
1253 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
1254 in6_are_addr_equal_scoped(&inp->in6p_laddr, laddr, inp->inp_lifscope, ifscope) &&
1255 inp->inp_lport == lport) {
1256 /*
1257 * Found.
1258 */
1259 return inp;
1260 }
1261 }
1262 /*
1263 * Not found.
1264 */
1265 return NULL;
1266 }
1267 /*
1268 * Best fit PCB lookup.
1269 *
1270 * First see if this local port is in use by looking on the
1271 * port hash list.
1272 */
1273 porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
1274 pcbinfo->ipi_porthashmask)];
1275 LIST_FOREACH(phd, porthash, phd_hash) {
1276 if (phd->phd_port == lport) {
1277 break;
1278 }
1279 }
1280 if (phd != NULL) {
1281 /*
1282 * Port is in use by one or more PCBs. Look for best
1283 * fit.
1284 */
1285 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
1286 wildcard = 0;
1287 if (!(inp->inp_vflag & INP_IPV6)) {
1288 continue;
1289 }
1290 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1291 wildcard++;
1292 }
1293 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1294 if (IN6_IS_ADDR_UNSPECIFIED(laddr)) {
1295 wildcard++;
1296 } else if (!in6_are_addr_equal_scoped(
1297 &inp->in6p_laddr, laddr, inp->inp_lifscope, ifscope)) {
1298 continue;
1299 }
1300 } else {
1301 if (!IN6_IS_ADDR_UNSPECIFIED(laddr)) {
1302 wildcard++;
1303 }
1304 }
1305 if (wildcard < matchwild) {
1306 match = inp;
1307 matchwild = wildcard;
1308 if (matchwild == 0) {
1309 break;
1310 }
1311 }
1312 }
1313 }
1314 return match;
1315 }
1316
1317 /*
1318 * Check for alternatives when higher level complains
1319 * about service problems. For now, invalidate cached
1320 * routing information. If the route was created dynamically
1321 * (by a redirect), time to try a default gateway again.
1322 */
1323 void
in6_losing(struct inpcb * in6p)1324 in6_losing(struct inpcb *in6p)
1325 {
1326 struct rtentry *rt;
1327
1328 if ((rt = in6p->in6p_route.ro_rt) != NULL) {
1329 RT_LOCK(rt);
1330 if (rt->rt_flags & RTF_DYNAMIC) {
1331 /*
1332 * Prevent another thread from modifying rt_key,
1333 * rt_gateway via rt_setgate() after the rt_lock
1334 * is dropped by marking the route as defunct.
1335 */
1336 rt->rt_flags |= RTF_CONDEMNED;
1337 RT_UNLOCK(rt);
1338 (void) rtrequest(RTM_DELETE, rt_key(rt),
1339 rt->rt_gateway, rt_mask(rt), rt->rt_flags, NULL);
1340 } else {
1341 RT_UNLOCK(rt);
1342 }
1343 /*
1344 * A new route can be allocated
1345 * the next time output is attempted.
1346 */
1347 }
1348 ROUTE_RELEASE(&in6p->in6p_route);
1349 }
1350
1351 /*
1352 * After a routing change, flush old routing
1353 * and allocate a (hopefully) better one.
1354 */
1355 void
in6_rtchange(struct inpcb * inp,int errno)1356 in6_rtchange(struct inpcb *inp, int errno)
1357 {
1358 #pragma unused(errno)
1359 /*
1360 * A new route can be allocated the next time
1361 * output is attempted.
1362 */
1363 ROUTE_RELEASE(&inp->in6p_route);
1364 }
1365
1366 /*
1367 * Check if PCB exists hash list. Also returns uid and gid of socket
1368 */
1369 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)1370 in6_pcblookup_hash_exists(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1371 u_int fport_arg, uint32_t fifscope, struct in6_addr *laddr, u_int lport_arg, uint32_t lifscope, int wildcard,
1372 uid_t *uid, gid_t *gid, struct ifnet *ifp, bool relaxed)
1373 {
1374 struct inpcbhead *head;
1375 struct inpcb *inp;
1376 uint16_t fport = (uint16_t)fport_arg, lport = (uint16_t)lport_arg;
1377 int found;
1378
1379 *uid = UID_MAX;
1380 *gid = GID_MAX;
1381
1382 lck_rw_lock_shared(&pcbinfo->ipi_lock);
1383
1384 /*
1385 * First look for an exact match.
1386 */
1387 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr->s6_addr32[3] /* XXX */,
1388 lport, fport, pcbinfo->ipi_hashmask)];
1389 LIST_FOREACH(inp, head, inp_hash) {
1390 if (!(inp->inp_vflag & INP_IPV6)) {
1391 continue;
1392 }
1393
1394 if (inp_restricted_recv(inp, ifp)) {
1395 continue;
1396 }
1397
1398 #if NECP
1399 if (!necp_socket_is_allowed_to_recv_on_interface(inp, ifp)) {
1400 continue;
1401 }
1402 #endif /* NECP */
1403
1404 if (((in6_are_addr_equal_scoped(&inp->in6p_faddr, faddr, inp->inp_fifscope, fifscope) &&
1405 in6_are_addr_equal_scoped(&inp->in6p_laddr, laddr, inp->inp_lifscope, lifscope)) ||
1406 (relaxed &&
1407 IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
1408 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr))) &&
1409 inp->inp_fport == fport &&
1410 inp->inp_lport == lport) {
1411 if ((found = (inp->inp_socket != NULL))) {
1412 /*
1413 * Found. Check if pcb is still valid
1414 */
1415 *uid = kauth_cred_getuid(
1416 inp->inp_socket->so_cred);
1417 *gid = kauth_cred_getgid(
1418 inp->inp_socket->so_cred);
1419 }
1420 lck_rw_done(&pcbinfo->ipi_lock);
1421 return found;
1422 }
1423 }
1424 if (wildcard) {
1425 struct inpcb *local_wild = NULL;
1426
1427 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport, 0,
1428 pcbinfo->ipi_hashmask)];
1429 LIST_FOREACH(inp, head, inp_hash) {
1430 if (!(inp->inp_vflag & INP_IPV6)) {
1431 continue;
1432 }
1433
1434 if (inp_restricted_recv(inp, ifp)) {
1435 continue;
1436 }
1437
1438 #if NECP
1439 if (!necp_socket_is_allowed_to_recv_on_interface(inp, ifp)) {
1440 continue;
1441 }
1442 #endif /* NECP */
1443
1444 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
1445 inp->inp_lport == lport) {
1446 if (in6_are_addr_equal_scoped(&inp->in6p_laddr,
1447 laddr, inp->inp_lifscope, lifscope) ||
1448 (relaxed && IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr))) {
1449 found = (inp->inp_socket != NULL);
1450 if (found) {
1451 *uid = kauth_cred_getuid(
1452 inp->inp_socket->so_cred);
1453 *gid = kauth_cred_getgid(
1454 inp->inp_socket->so_cred);
1455 }
1456 lck_rw_done(&pcbinfo->ipi_lock);
1457 return found;
1458 } else if (IN6_IS_ADDR_UNSPECIFIED(
1459 &inp->in6p_laddr)) {
1460 local_wild = inp;
1461 }
1462 }
1463 }
1464 if (local_wild) {
1465 if ((found = (local_wild->inp_socket != NULL))) {
1466 *uid = kauth_cred_getuid(
1467 local_wild->inp_socket->so_cred);
1468 *gid = kauth_cred_getgid(
1469 local_wild->inp_socket->so_cred);
1470 }
1471 lck_rw_done(&pcbinfo->ipi_lock);
1472 return found;
1473 }
1474 }
1475
1476 /*
1477 * Not found.
1478 */
1479 lck_rw_done(&pcbinfo->ipi_lock);
1480 return 0;
1481 }
1482
1483 /*
1484 * Lookup PCB in hash list.
1485 */
1486 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)1487 in6_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1488 u_int fport_arg, uint32_t fifscope, struct in6_addr *laddr, u_int lport_arg, uint32_t lifscope, int wildcard,
1489 struct ifnet *ifp)
1490 {
1491 struct inpcbhead *head;
1492 struct inpcb *inp;
1493 uint16_t fport = (uint16_t)fport_arg, lport = (uint16_t)lport_arg;
1494
1495 lck_rw_lock_shared(&pcbinfo->ipi_lock);
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 lck_rw_done(&pcbinfo->ipi_lock);
1527 return inp;
1528 } else {
1529 /* it's there but dead, say it isn't found */
1530 lck_rw_done(&pcbinfo->ipi_lock);
1531 return NULL;
1532 }
1533 }
1534 }
1535 if (wildcard) {
1536 struct inpcb *local_wild = NULL;
1537
1538 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport, 0,
1539 pcbinfo->ipi_hashmask)];
1540 LIST_FOREACH(inp, head, inp_hash) {
1541 if (!(inp->inp_vflag & INP_IPV6)) {
1542 continue;
1543 }
1544
1545 if (inp_restricted_recv(inp, ifp)) {
1546 continue;
1547 }
1548
1549 #if NECP
1550 if (!necp_socket_is_allowed_to_recv_on_interface(inp, ifp)) {
1551 continue;
1552 }
1553 #endif /* NECP */
1554
1555 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
1556 inp->inp_lport == lport) {
1557 if (in6_are_addr_equal_scoped(&inp->in6p_laddr,
1558 laddr, inp->inp_lifscope, lifscope)) {
1559 if (in_pcb_checkstate(inp, WNT_ACQUIRE,
1560 0) != WNT_STOPUSING) {
1561 lck_rw_done(&pcbinfo->ipi_lock);
1562 return inp;
1563 } else {
1564 /* dead; say it isn't found */
1565 lck_rw_done(&pcbinfo->ipi_lock);
1566 return NULL;
1567 }
1568 } else if (IN6_IS_ADDR_UNSPECIFIED(
1569 &inp->in6p_laddr)) {
1570 local_wild = inp;
1571 }
1572 }
1573 }
1574 if (local_wild && in_pcb_checkstate(local_wild,
1575 WNT_ACQUIRE, 0) != WNT_STOPUSING) {
1576 lck_rw_done(&pcbinfo->ipi_lock);
1577 return local_wild;
1578 } else {
1579 lck_rw_done(&pcbinfo->ipi_lock);
1580 return NULL;
1581 }
1582 }
1583
1584 /*
1585 * Not found.
1586 */
1587 lck_rw_done(&pcbinfo->ipi_lock);
1588 return NULL;
1589 }
1590
1591 void
init_sin6(struct sockaddr_in6 * sin6,struct mbuf * m)1592 init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m)
1593 {
1594 struct ip6_hdr *ip;
1595
1596 ip = mtod(m, struct ip6_hdr *);
1597 bzero(sin6, sizeof(*sin6));
1598 sin6->sin6_len = sizeof(*sin6);
1599 sin6->sin6_family = AF_INET6;
1600 sin6->sin6_addr = ip->ip6_src;
1601 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) {
1602 if (in6_embedded_scope) {
1603 sin6->sin6_addr.s6_addr16[1] = 0;
1604 }
1605 if ((m->m_pkthdr.pkt_flags & (PKTF_LOOP | PKTF_IFAINFO)) ==
1606 (PKTF_LOOP | PKTF_IFAINFO)) {
1607 sin6->sin6_scope_id = m->m_pkthdr.src_ifindex;
1608 } else if (m->m_pkthdr.rcvif != NULL) {
1609 sin6->sin6_scope_id = m->m_pkthdr.rcvif->if_index;
1610 }
1611 }
1612 }
1613
1614 /*
1615 * The following routines implement this scheme:
1616 *
1617 * Callers of ip6_output() that intend to cache the route in the inpcb pass
1618 * a local copy of the struct route to ip6_output(). Using a local copy of
1619 * the cached route significantly simplifies things as IP no longer has to
1620 * worry about having exclusive access to the passed in struct route, since
1621 * it's defined in the caller's stack; in essence, this allows for a lock-
1622 * less operation when updating the struct route at the IP level and below,
1623 * whenever necessary. The scheme works as follows:
1624 *
1625 * Prior to dropping the socket's lock and calling ip6_output(), the caller
1626 * copies the struct route from the inpcb into its stack, and adds a reference
1627 * to the cached route entry, if there was any. The socket's lock is then
1628 * dropped and ip6_output() is called with a pointer to the copy of struct
1629 * route defined on the stack (not to the one in the inpcb.)
1630 *
1631 * Upon returning from ip6_output(), the caller then acquires the socket's
1632 * lock and synchronizes the cache; if there is no route cached in the inpcb,
1633 * it copies the local copy of struct route (which may or may not contain any
1634 * route) back into the cache; otherwise, if the inpcb has a route cached in
1635 * it, the one in the local copy will be freed, if there's any. Trashing the
1636 * cached route in the inpcb can be avoided because ip6_output() is single-
1637 * threaded per-PCB (i.e. multiple transmits on a PCB are always serialized
1638 * by the socket/transport layer.)
1639 */
1640 void
in6p_route_copyout(struct inpcb * inp,struct route_in6 * dst)1641 in6p_route_copyout(struct inpcb *inp, struct route_in6 *dst)
1642 {
1643 struct route_in6 *src = &inp->in6p_route;
1644
1645 socket_lock_assert_owned(inp->inp_socket);
1646
1647 /* Minor sanity check */
1648 if (src->ro_rt != NULL && rt_key(src->ro_rt)->sa_family != AF_INET6) {
1649 panic("%s: wrong or corrupted route: %p", __func__, src);
1650 }
1651
1652 route_copyout((struct route *)dst, (struct route *)src, sizeof(*dst));
1653 }
1654
1655 void
in6p_route_copyin(struct inpcb * inp,struct route_in6 * src)1656 in6p_route_copyin(struct inpcb *inp, struct route_in6 *src)
1657 {
1658 struct route_in6 *dst = &inp->in6p_route;
1659
1660 socket_lock_assert_owned(inp->inp_socket);
1661
1662 /* Minor sanity check */
1663 if (src->ro_rt != NULL && rt_key(src->ro_rt)->sa_family != AF_INET6) {
1664 panic("%s: wrong or corrupted route: %p", __func__, src);
1665 }
1666
1667 route_copyin((struct route *)src, (struct route *)dst, sizeof(*src));
1668 }
1669