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