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