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