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