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