]> git.saurik.com Git - apple/xnu.git/blame - bsd/netinet6/in6_pcb.c
xnu-3248.60.10.tar.gz
[apple/xnu.git] / bsd / netinet6 / in6_pcb.c
CommitLineData
91447636 1/*
39236c6e 2 * Copyright (c) 2003-2013 Apple Inc. All rights reserved.
91447636 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
39236c6e 5 *
2d21ac55
A
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.
39236c6e 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
39236c6e 17 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
39236c6e 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
91447636 27 */
1c79356b
A
28/*
29 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
30 * All rights reserved.
9bccf70c 31 *
1c79356b
A
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.
9bccf70c 43 *
1c79356b
A
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.
9bccf70c 55 *
1c79356b
A
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
1c79356b
A
93#include <sys/param.h>
94#include <sys/systm.h>
95#include <sys/malloc.h>
96#include <sys/mbuf.h>
9bccf70c 97#include <sys/domain.h>
1c79356b
A
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>
6d2010ae
A
105#include <sys/kauth.h>
106#include <sys/priv.h>
1c79356b
A
107
108#include <net/if.h>
109#include <net/if_types.h>
110#include <net/route.h>
fe8ab488 111#include <net/ntstat.h>
1c79356b
A
112
113#include <netinet/in.h>
114#include <netinet/in_var.h>
115#include <netinet/in_systm.h>
116#include <netinet/ip6.h>
9bccf70c 117#include <netinet/ip_var.h>
1c79356b
A
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>
316670eb 123#include <net/if_var.h>
1c79356b 124
91447636
A
125#include <kern/kern_types.h>
126#include <kern/zalloc.h>
127
1c79356b
A
128#if IPSEC
129#include <netinet6/ipsec.h>
9bccf70c
A
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
1c79356b 137#include <netkey/key.h>
1c79356b
A
138#endif /* IPSEC */
139
fe8ab488
A
140#if NECP
141#include <net/necp.h>
142#endif /* NECP */
143
2d21ac55 144/*
39236c6e
A
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 */
153static struct inpcb *
154in6_pcblookup_local_and_cleanup(struct inpcbinfo *pcbinfo,
155 struct in6_addr *laddr, u_int lport_arg, int wild_okay)
2d21ac55
A
156{
157 struct inpcb *inp;
39236c6e 158
2d21ac55
A
159 /* Perform normal lookup */
160 inp = in6_pcblookup_local(pcbinfo, laddr, lport_arg, wild_okay);
39236c6e 161
2d21ac55 162 /* Check if we found a match but it's waiting to be disposed */
39236c6e 163 if (inp != NULL && inp->inp_wantcnt == WNT_STOPUSING) {
2d21ac55 164 struct socket *so = inp->inp_socket;
39236c6e 165
6d2010ae 166 lck_mtx_lock(&inp->inpcb_mtx);
39236c6e 167
2d21ac55 168 if (so->so_usecount == 0) {
b0d623f7
A
169 if (inp->inp_state != INPCB_STATE_DEAD)
170 in6_pcbdetach(inp);
39236c6e 171 in_pcbdispose(inp); /* will unlock & destroy */
2d21ac55 172 inp = NULL;
39236c6e 173 } else {
6d2010ae 174 lck_mtx_unlock(&inp->inpcb_mtx);
2d21ac55
A
175 }
176 }
39236c6e
A
177
178 return (inp);
2d21ac55 179}
6d2010ae 180
39236c6e
A
181/*
182 * Bind an INPCB to an address and/or port. This routine should not alter
183 * the caller-supplied local address "nam".
184 */
1c79356b 185int
6d2010ae 186in6_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct proc *p)
1c79356b
A
187{
188 struct socket *so = inp->inp_socket;
1c79356b
A
189 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
190 u_short lport = 0;
191 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
fe8ab488
A
192 struct ifnet *outif = NULL;
193 struct sockaddr_in6 sin6;
6d2010ae
A
194 int error;
195 kauth_cred_t cred;
1c79356b 196
91447636 197 if (!in6_ifaddrs) /* XXX broken! */
1c79356b
A
198 return (EADDRNOTAVAIL);
199 if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
39236c6e
A
200 return (EINVAL);
201 if (!(so->so_options & (SO_REUSEADDR|SO_REUSEPORT)))
9bccf70c 202 wild = 1;
4bd07ac2 203
91447636 204 socket_unlock(so, 0); /* keep reference */
39236c6e 205 lck_rw_lock_exclusive(pcbinfo->ipi_lock);
6d2010ae 206
fe8ab488
A
207 bzero(&sin6, sizeof (sin6));
208 if (nam != NULL) {
39236c6e
A
209 if (nam->sa_len != sizeof (struct sockaddr_in6)) {
210 lck_rw_done(pcbinfo->ipi_lock);
91447636 211 socket_lock(so, 0);
39236c6e 212 return (EINVAL);
91447636 213 }
1c79356b 214 /*
9bccf70c 215 * family check.
1c79356b 216 */
91447636 217 if (nam->sa_family != AF_INET6) {
39236c6e 218 lck_rw_done(pcbinfo->ipi_lock);
91447636 219 socket_lock(so, 0);
39236c6e 220 return (EAFNOSUPPORT);
91447636 221 }
39236c6e
A
222 lport = SIN6(nam)->sin6_port;
223
39236c6e 224 *(&sin6) = *SIN6(nam);
1c79356b 225
9bccf70c 226 /* KAME hack: embed scopeid */
39236c6e 227 if (in6_embedscope(&sin6.sin6_addr, &sin6, inp, NULL,
6d2010ae 228 NULL) != 0) {
39236c6e 229 lck_rw_done(pcbinfo->ipi_lock);
91447636 230 socket_lock(so, 0);
39236c6e 231 return (EINVAL);
91447636 232 }
1c79356b 233
39236c6e
A
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)) {
1c79356b
A
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;
39236c6e 249 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6.sin6_addr)) {
6d2010ae 250 struct ifaddr *ifa;
1c79356b 251
39236c6e
A
252 ifa = ifa_ifwithaddr(SA(&sin6));
253 if (ifa == NULL) {
254 lck_rw_done(pcbinfo->ipi_lock);
91447636 255 socket_lock(so, 0);
39236c6e
A
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 */
6d2010ae
A
265 IFA_LOCK_SPIN(ifa);
266 if (((struct in6_ifaddr *)ifa)->ia6_flags &
39236c6e
A
267 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
268 IN6_IFF_DETACHED)) {
6d2010ae
A
269 IFA_UNLOCK(ifa);
270 IFA_REMREF(ifa);
39236c6e 271 lck_rw_done(pcbinfo->ipi_lock);
6d2010ae 272 socket_lock(so, 0);
39236c6e 273 return (EADDRNOTAVAIL);
6d2010ae 274 }
39236c6e
A
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 */
316670eb 283 outif = ifa->ifa_ifp;
6d2010ae
A
284 IFA_UNLOCK(ifa);
285 IFA_REMREF(ifa);
1c79356b
A
286 }
287 }
39236c6e 288 if (lport != 0) {
1c79356b 289 struct inpcb *t;
39236c6e 290 uid_t u;
1c79356b
A
291
292 /* GROSS */
6d2010ae
A
293 if (ntohs(lport) < IPV6PORT_RESERVED) {
294 cred = kauth_cred_proc_ref(p);
39236c6e
A
295 error = priv_check_cred(cred,
296 PRIV_NETINET_RESERVEDPORT, 0);
6d2010ae
A
297 kauth_cred_unref(&cred);
298 if (error != 0) {
39236c6e 299 lck_rw_done(pcbinfo->ipi_lock);
6d2010ae 300 socket_lock(so, 0);
39236c6e 301 return (EACCES);
6d2010ae 302 }
91447636 303 }
39236c6e
A
304 if (!IN6_IS_ADDR_MULTICAST(&sin6.sin6_addr) &&
305 (u = kauth_cred_getuid(so->so_cred)) != 0) {
2d21ac55 306 t = in6_pcblookup_local_and_cleanup(pcbinfo,
39236c6e 307 &sin6.sin6_addr, lport,
9bccf70c 308 INPLOOKUP_WILDCARD);
39236c6e
A
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);
91447636 318 socket_lock(so, 0);
1c79356b 319 return (EADDRINUSE);
91447636 320 }
39236c6e
A
321 if (!(inp->inp_flags & IN6P_IPV6_V6ONLY) &&
322 IN6_IS_ADDR_UNSPECIFIED(&sin6.sin6_addr)) {
9bccf70c
A
323 struct sockaddr_in sin;
324
39236c6e
A
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) &&
316670eb 332 (kauth_cred_getuid(so->so_cred) !=
39236c6e
A
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);
91447636 338 socket_lock(so, 0);
9bccf70c 339 return (EADDRINUSE);
91447636 340 }
9bccf70c 341 }
1c79356b 342 }
39236c6e
A
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);
91447636 348 socket_lock(so, 0);
39236c6e 349 return (EADDRINUSE);
91447636 350 }
39236c6e
A
351 if (!(inp->inp_flags & IN6P_IPV6_V6ONLY) &&
352 IN6_IS_ADDR_UNSPECIFIED(&sin6.sin6_addr)) {
9bccf70c
A
353 struct sockaddr_in sin;
354
39236c6e
A
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);
91447636 363 socket_lock(so, 0);
9bccf70c 364 return (EADDRINUSE);
91447636 365 }
9bccf70c 366 }
1c79356b 367 }
fe8ab488
A
368 }
369
370 socket_lock(so, 0);
4bd07ac2
A
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
fe8ab488
A
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)) {
39236c6e 388 inp->in6p_laddr = sin6.sin6_addr;
316670eb 389 inp->in6p_last_outifp = outif;
1c79356b 390 }
fe8ab488 391
1c79356b 392 if (lport == 0) {
9bccf70c 393 int e;
91447636 394 if ((e = in6_pcbsetport(&inp->in6p_laddr, inp, p, 1)) != 0) {
fe8ab488
A
395 /* Undo any address bind from above. */
396 inp->in6p_laddr = in6addr_any;
397 inp->in6p_last_outifp = NULL;
39236c6e
A
398 lck_rw_done(pcbinfo->ipi_lock);
399 return (e);
91447636 400 }
39236c6e 401 } else {
9bccf70c 402 inp->inp_lport = lport;
91447636 403 if (in_pcbinshash(inp, 1) != 0) {
9bccf70c
A
404 inp->in6p_laddr = in6addr_any;
405 inp->inp_lport = 0;
316670eb 406 inp->in6p_last_outifp = NULL;
39236c6e 407 lck_rw_done(pcbinfo->ipi_lock);
9bccf70c
A
408 return (EAGAIN);
409 }
6d2010ae 410 }
39236c6e 411 lck_rw_done(pcbinfo->ipi_lock);
2d21ac55 412 sflt_notify(so, sock_evt_bound, NULL);
39236c6e 413 return (0);
1c79356b
A
414}
415
416/*
316670eb 417 * Transform old in6_pcbconnect() into an inner subroutine for new
39236c6e
A
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.
1c79356b 421 *
39236c6e 422 * This routine may alter the caller-supplied remote address "nam".
316670eb
A
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.
1c79356b 427 */
1c79356b 428int
6d2010ae 429in6_pcbladdr(struct inpcb *inp, struct sockaddr *nam,
316670eb 430 struct in6_addr *plocal_addr6, struct ifnet **outif)
1c79356b 431{
91447636
A
432 struct in6_addr *addr6 = NULL;
433 struct in6_addr src_storage;
1c79356b 434 int error = 0;
6d2010ae 435 unsigned int ifscope;
1c79356b 436
316670eb
A
437 if (outif != NULL)
438 *outif = NULL;
39236c6e 439 if (nam->sa_len != sizeof (struct sockaddr_in6))
1c79356b 440 return (EINVAL);
39236c6e 441 if (SIN6(nam)->sin6_family != AF_INET6)
1c79356b 442 return (EAFNOSUPPORT);
39236c6e 443 if (SIN6(nam)->sin6_port == 0)
1c79356b
A
444 return (EADDRNOTAVAIL);
445
9bccf70c 446 /* KAME hack: embed scopeid */
39236c6e 447 if (in6_embedscope(&SIN6(nam)->sin6_addr, SIN6(nam), inp, NULL, NULL) != 0)
316670eb 448 return (EINVAL);
1c79356b 449
91447636 450 if (in6_ifaddrs) {
1c79356b
A
451 /*
452 * If the destination address is UNSPECIFIED addr,
453 * use the loopback addr, e.g ::1.
454 */
39236c6e
A
455 if (IN6_IS_ADDR_UNSPECIFIED(&SIN6(nam)->sin6_addr))
456 SIN6(nam)->sin6_addr = in6addr_loopback;
1c79356b 457 }
6d2010ae
A
458
459 ifscope = (inp->inp_flags & INP_BOUND_IF) ?
39236c6e 460 inp->inp_boundifp->if_index : IFSCOPE_NONE;
6d2010ae
A
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?
316670eb
A
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.
6d2010ae 470 */
39236c6e 471 addr6 = in6_selectsrc(SIN6(nam), inp->in6p_outputopts, inp,
316670eb
A
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 }
1c79356b
A
489 }
490
316670eb
A
491 if (addr6 == NULL) {
492 if (outif != NULL && (*outif) != NULL &&
fe8ab488 493 inp_restricted_send(inp, *outif)) {
316670eb
A
494 soevent(inp->inp_socket,
495 (SO_FILT_HINT_LOCKED | SO_FILT_HINT_IFDENIED));
39236c6e
A
496 error = EHOSTUNREACH;
497 }
316670eb
A
498 if (error == 0)
499 error = EADDRNOTAVAIL;
500 return (error);
6d2010ae 501 }
1c79356b 502
6d2010ae
A
503 *plocal_addr6 = *addr6;
504 /*
505 * Don't do pcblookup call here; return interface in
316670eb 506 * plocal_addr6 and exit to caller, that will do the lookup.
6d2010ae 507 */
316670eb 508 return (0);
1c79356b
A
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 */
518int
39236c6e 519in6_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct proc *p)
1c79356b 520{
91447636 521 struct in6_addr addr6;
316670eb 522 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)(void *)nam;
91447636 523 struct inpcb *pcb;
316670eb
A
524 int error = 0;
525 struct ifnet *outif = NULL;
fe8ab488 526 struct socket *so = inp->inp_socket;
1c79356b
A
527
528 /*
55e303ae
A
529 * Call inner routine, to assign local interface address.
530 * in6_pcbladdr() may automatically fill in sin6_scope_id.
316670eb
A
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.
1c79356b 535 */
316670eb 536 if ((error = in6_pcbladdr(inp, nam, &addr6, &outif)) != 0) {
fe8ab488
A
537 if (outif != NULL && inp_restricted_send(inp, outif))
538 soevent(so,
316670eb
A
539 (SO_FILT_HINT_LOCKED | SO_FILT_HINT_IFDENIED));
540 goto done;
541 }
fe8ab488 542 socket_unlock(so, 0);
91447636 543 pcb = in6_pcblookup_hash(inp->inp_pcbinfo, &sin6->sin6_addr,
39236c6e
A
544 sin6->sin6_port, IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) ?
545 &addr6 : &inp->in6p_laddr, inp->inp_lport, 0, NULL);
fe8ab488 546 socket_lock(so, 0);
91447636 547 if (pcb != NULL) {
0b4c1975 548 in_pcb_checkstate(pcb, WNT_RELEASE, pcb == inp ? 1 : 0);
316670eb
A
549 error = EADDRINUSE;
550 goto done;
1c79356b
A
551 }
552 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
553 if (inp->inp_lport == 0) {
39236c6e 554 error = in6_pcbbind(inp, NULL, p);
1c79356b 555 if (error)
316670eb 556 goto done;
1c79356b 557 }
91447636 558 inp->in6p_laddr = addr6;
316670eb 559 inp->in6p_last_outifp = outif; /* no reference needed */
39236c6e 560 inp->in6p_flags |= INP_IN6ADDR_ANY;
91447636 561 }
39236c6e
A
562 if (!lck_rw_try_lock_exclusive(inp->inp_pcbinfo->ipi_lock)) {
563 /* lock inversion issue, mostly with udp multicast packets */
fe8ab488 564 socket_unlock(so, 0);
39236c6e 565 lck_rw_lock_exclusive(inp->inp_pcbinfo->ipi_lock);
fe8ab488 566 socket_lock(so, 0);
1c79356b
A
567 }
568 inp->in6p_faddr = sin6->sin6_addr;
569 inp->inp_fport = sin6->sin6_port;
fe8ab488
A
570 if (nstat_collect && SOCK_PROTO(so) == IPPROTO_UDP)
571 nstat_pcb_invalidate_cache(inp);
1c79356b 572 in_pcbrehash(inp);
39236c6e 573 lck_rw_done(inp->inp_pcbinfo->ipi_lock);
316670eb
A
574
575done:
576 if (outif != NULL)
577 ifnet_release(outif);
578
579 return (error);
1c79356b
A
580}
581
1c79356b 582void
39236c6e 583in6_pcbdisconnect(struct inpcb *inp)
1c79356b 584{
39236c6e
A
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);
91447636 592 }
fe8ab488
A
593 if (nstat_collect && SOCK_PROTO(so) == IPPROTO_UDP)
594 nstat_pcb_cache(inp);
39236c6e 595 bzero((caddr_t)&inp->in6p_faddr, sizeof (inp->in6p_faddr));
1c79356b 596 inp->inp_fport = 0;
39236c6e
A
597 /* clear flowinfo - RFC 6437 */
598 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
1c79356b 599 in_pcbrehash(inp);
39236c6e
A
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))
1c79356b
A
607 in6_pcbdetach(inp);
608}
609
610void
39236c6e 611in6_pcbdetach(struct inpcb *inp)
1c79356b
A
612{
613 struct socket *so = inp->inp_socket;
39236c6e
A
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 }
fe8ab488 621
1c79356b 622#if IPSEC
91447636 623 if (inp->in6p_sp != NULL) {
39236c6e 624 (void) ipsec6_delete_pcbpolicy(inp);
91447636 625 }
1c79356b 626#endif /* IPSEC */
9bccf70c 627
fe8ab488
A
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);
39236c6e
A
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 }
91447636 641
39236c6e 642 if (!(so->so_flags & SOF_PCBCLEARING)) {
6d2010ae
A
643 struct ip_moptions *imo;
644 struct ip6_moptions *im6o;
645
91447636 646 inp->inp_vflag = 0;
39236c6e 647 if (inp->in6p_options != NULL) {
91447636 648 m_freem(inp->in6p_options);
39236c6e
A
649 inp->in6p_options = NULL;
650 }
316670eb 651 ip6_freepcbopts(inp->in6p_outputopts);
39236c6e
A
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;
c910b4d9 657 }
6d2010ae
A
658 im6o = inp->in6p_moptions;
659 inp->in6p_moptions = NULL;
660 if (im6o != NULL)
661 IM6O_REMREF(im6o);
662
663 imo = inp->inp_moptions;
91447636 664 inp->inp_moptions = NULL;
6d2010ae
A
665 if (imo != NULL)
666 IMO_REMREF(imo);
39236c6e
A
667 sofreelastref(so, 0);
668 inp->inp_state = INPCB_STATE_DEAD;
669 /* makes sure we're not called twice from so_close */
670 so->so_flags |= SOF_PCBCLEARING;
671
672 inpcb_gc_sched(inp->inp_pcbinfo, INPCB_TIMER_FAST);
91447636 673 }
1c79356b
A
674}
675
55e303ae 676struct sockaddr *
39236c6e 677in6_sockaddr(in_port_t port, struct in6_addr *addr_p)
55e303ae
A
678{
679 struct sockaddr_in6 *sin6;
680
39236c6e 681 MALLOC(sin6, struct sockaddr_in6 *, sizeof (*sin6), M_SONAME, M_WAITOK);
b0d623f7 682 if (sin6 == NULL)
39236c6e
A
683 return (NULL);
684 bzero(sin6, sizeof (*sin6));
55e303ae 685 sin6->sin6_family = AF_INET6;
39236c6e 686 sin6->sin6_len = sizeof (*sin6);
55e303ae
A
687 sin6->sin6_port = port;
688 sin6->sin6_addr = *addr_p;
39236c6e
A
689
690 /* would be good to use sa6_recoverscope(), except for locking */
55e303ae
A
691 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
692 sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]);
693 else
39236c6e 694 sin6->sin6_scope_id = 0; /* XXX */
55e303ae
A
695 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
696 sin6->sin6_addr.s6_addr16[1] = 0;
697
39236c6e 698 return ((struct sockaddr *)sin6);
55e303ae
A
699}
700
39236c6e
A
701void
702in6_sockaddr_s(in_port_t port, struct in6_addr *addr_p,
703 struct sockaddr_in6 *sin6)
55e303ae 704{
39236c6e
A
705 bzero(sin6, sizeof (*sin6));
706 sin6->sin6_family = AF_INET6;
707 sin6->sin6_len = sizeof (*sin6);
708 sin6->sin6_port = port;
709 sin6->sin6_addr = *addr_p;
710
711 /* would be good to use sa6_recoverscope(), except for locking */
712 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
713 sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]);
714 else
715 sin6->sin6_scope_id = 0; /* XXX */
716 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
717 sin6->sin6_addr.s6_addr16[1] = 0;
55e303ae
A
718}
719
1c79356b 720/*
39236c6e 721 * The calling convention of in6_getsockaddr() and in6_getpeeraddr() was
1c79356b
A
722 * modified to match the pru_sockaddr() and pru_peeraddr() entry points
723 * in struct pr_usrreqs, so that protocols can just reference then directly
39236c6e 724 * without the need for a wrapper function.
1c79356b
A
725 */
726int
39236c6e 727in6_getsockaddr(struct socket *so, struct sockaddr **nam)
1c79356b 728{
91447636 729 struct inpcb *inp;
55e303ae
A
730 struct in6_addr addr;
731 in_port_t port;
1c79356b 732
39236c6e
A
733 if ((inp = sotoinpcb(so)) == NULL)
734 return (EINVAL);
735
55e303ae
A
736 port = inp->inp_lport;
737 addr = inp->in6p_laddr;
1c79356b 738
55e303ae 739 *nam = in6_sockaddr(port, &addr);
b0d623f7 740 if (*nam == NULL)
39236c6e
A
741 return (ENOBUFS);
742 return (0);
1c79356b
A
743}
744
745int
39236c6e 746in6_getsockaddr_s(struct socket *so, struct sockaddr_storage *ss)
1c79356b 747{
1c79356b 748 struct inpcb *inp;
55e303ae
A
749 struct in6_addr addr;
750 in_port_t port;
1c79356b 751
39236c6e
A
752 VERIFY(ss != NULL);
753 bzero(ss, sizeof (*ss));
754
fe8ab488
A
755 if ((inp = sotoinpcb(so)) == NULL
756#if NECP
757 || (necp_socket_should_use_flow_divert(inp))
758#endif /* NECP */
759 )
39236c6e
A
760 return (inp == NULL ? EINVAL : EPROTOTYPE);
761
762 port = inp->inp_lport;
763 addr = inp->in6p_laddr;
764
765 in6_sockaddr_s(port, &addr, SIN6(ss));
766 return (0);
767}
768
769int
770in6_getpeeraddr(struct socket *so, struct sockaddr **nam)
771{
772 struct inpcb *inp;
773 struct in6_addr addr;
774 in_port_t port;
775
776 if ((inp = sotoinpcb(so)) == NULL)
777 return (EINVAL);
778
55e303ae
A
779 port = inp->inp_fport;
780 addr = inp->in6p_faddr;
1c79356b 781
55e303ae 782 *nam = in6_sockaddr(port, &addr);
b0d623f7 783 if (*nam == NULL)
39236c6e
A
784 return (ENOBUFS);
785 return (0);
786}
787
788int
789in6_getpeeraddr_s(struct socket *so, struct sockaddr_storage *ss)
790{
791 struct inpcb *inp;
792 struct in6_addr addr;
793 in_port_t port;
794
795 VERIFY(ss != NULL);
796 bzero(ss, sizeof (*ss));
797
fe8ab488
A
798 if ((inp = sotoinpcb(so)) == NULL
799#if NECP
800 || (necp_socket_should_use_flow_divert(inp))
801#endif /* NECP */
802 )
39236c6e
A
803 return (inp == NULL ? EINVAL : EPROTOTYPE);
804
805 port = inp->inp_fport;
806 addr = inp->in6p_faddr;
807
808 in6_sockaddr_s(port, &addr, SIN6(ss));
809 return (0);
1c79356b
A
810}
811
812int
813in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
814{
815 struct inpcb *inp = sotoinpcb(so);
816 int error;
817
818 if (inp == NULL)
39236c6e 819 return (EINVAL);
1c79356b 820 if (inp->inp_vflag & INP_IPV4) {
39236c6e 821 error = in_getsockaddr(so, nam);
1c79356b 822 if (error == 0)
b0d623f7
A
823 error = in6_sin_2_v4mapsin6_in_sock(nam);
824 } else {
39236c6e
A
825 /* scope issues will be handled in in6_getsockaddr(). */
826 error = in6_getsockaddr(so, nam);
b0d623f7 827 }
39236c6e 828 return (error);
1c79356b
A
829}
830
831int
832in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
833{
834 struct inpcb *inp = sotoinpcb(so);
835 int error;
836
837 if (inp == NULL)
39236c6e 838 return (EINVAL);
1c79356b 839 if (inp->inp_vflag & INP_IPV4) {
39236c6e 840 error = in_getpeeraddr(so, nam);
1c79356b 841 if (error == 0)
b0d623f7
A
842 error = in6_sin_2_v4mapsin6_in_sock(nam);
843 } else {
39236c6e
A
844 /* scope issues will be handled in in6_getpeeraddr(). */
845 error = in6_getpeeraddr(so, nam);
b0d623f7 846 }
39236c6e 847 return (error);
1c79356b
A
848}
849
850/*
851 * Pass some notification to all connections of a protocol
852 * associated with address dst. The local address and/or port numbers
853 * may be specified to limit the search. The "usual action" will be
854 * taken, depending on the ctlinput cmd. The caller must filter any
855 * cmds that are uninteresting (e.g., no error in the map).
856 * Call the protocol specific routine (if any) to report
857 * any errors for each matching socket.
1c79356b
A
858 */
859void
39236c6e
A
860in6_pcbnotify(struct inpcbinfo *pcbinfo, struct sockaddr *dst, u_int fport_arg,
861 const struct sockaddr *src, u_int lport_arg, int cmd, void *cmdarg,
862 void (*notify)(struct inpcb *, int))
1c79356b 863{
39236c6e 864 struct inpcbhead *head = pcbinfo->ipi_listhead;
1c79356b 865 struct inpcb *inp, *ninp;
9bccf70c 866 struct sockaddr_in6 sa6_src, *sa6_dst;
1c79356b 867 u_short fport = fport_arg, lport = lport_arg;
9bccf70c 868 u_int32_t flowinfo;
91447636 869 int errno;
1c79356b 870
3e170ce0 871 if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
1c79356b 872 return;
9bccf70c 873
316670eb 874 sa6_dst = (struct sockaddr_in6 *)(void *)dst;
9bccf70c 875 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
1c79356b
A
876 return;
877
9bccf70c
A
878 /*
879 * note that src can be NULL when we get notify by local fragmentation.
880 */
316670eb
A
881 sa6_src = (src == NULL) ?
882 sa6_any : *(struct sockaddr_in6 *)(uintptr_t)(size_t)src;
9bccf70c
A
883 flowinfo = sa6_src.sin6_flowinfo;
884
1c79356b
A
885 /*
886 * Redirects go to all references to the destination,
887 * and use in6_rtchange to invalidate the route cache.
888 * Dead host indications: also use in6_rtchange to invalidate
889 * the cache, and deliver the error to all the sockets.
890 * Otherwise, if we have knowledge of the local port and address,
891 * deliver only to that socket.
892 */
893 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
894 fport = 0;
895 lport = 0;
39236c6e 896 bzero((caddr_t)&sa6_src.sin6_addr, sizeof (sa6_src.sin6_addr));
1c79356b 897
9bccf70c
A
898 if (cmd != PRC_HOSTDEAD)
899 notify = in6_rtchange;
1c79356b
A
900 }
901 errno = inet6ctlerrmap[cmd];
39236c6e
A
902 lck_rw_lock_shared(pcbinfo->ipi_lock);
903 for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
904 ninp = LIST_NEXT(inp, inp_list);
1c79356b 905
39236c6e 906 if (!(inp->inp_vflag & INP_IPV6))
1c79356b
A
907 continue;
908
6d2010ae
A
909 /*
910 * If the error designates a new path MTU for a destination
911 * and the application (associated with this socket) wanted to
912 * know the value, notify. Note that we notify for all
913 * disconnected sockets if the corresponding application
914 * wanted. This is because some UDP applications keep sending
915 * sockets disconnected.
916 * XXX: should we avoid to notify the value to TCP sockets?
917 */
918 if (cmd == PRC_MSGSIZE && (inp->inp_flags & IN6P_MTU) != 0 &&
919 (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
39236c6e
A
920 IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
921 &sa6_dst->sin6_addr))) {
316670eb
A
922 ip6_notify_pmtu(inp, (struct sockaddr_in6 *)(void *)dst,
923 (u_int32_t *)cmdarg);
6d2010ae
A
924 }
925
9bccf70c
A
926 /*
927 * Detect if we should notify the error. If no source and
928 * destination ports are specifed, but non-zero flowinfo and
929 * local address match, notify the error. This is the case
930 * when the error is delivered with an encrypted buffer
931 * by ESP. Otherwise, just compare addresses and ports
932 * as usual.
933 */
934 if (lport == 0 && fport == 0 && flowinfo &&
935 inp->inp_socket != NULL &&
39236c6e 936 flowinfo == (inp->inp_flow & IPV6_FLOWLABEL_MASK) &&
9bccf70c
A
937 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
938 goto do_notify;
939 else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
39236c6e
A
940 &sa6_dst->sin6_addr) || inp->inp_socket == NULL ||
941 (lport && inp->inp_lport != lport) ||
942 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
943 !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
944 &sa6_src.sin6_addr)) || (fport && inp->inp_fport != fport))
1c79356b
A
945 continue;
946
39236c6e 947do_notify:
91447636 948 if (notify) {
39236c6e
A
949 if (in_pcb_checkstate(inp, WNT_ACQUIRE, 0) ==
950 WNT_STOPUSING)
91447636
A
951 continue;
952 socket_lock(inp->inp_socket, 1);
1c79356b 953 (*notify)(inp, errno);
39236c6e 954 (void) in_pcb_checkstate(inp, WNT_RELEASE, 1);
91447636
A
955 socket_unlock(inp->inp_socket, 1);
956 }
1c79356b 957 }
39236c6e 958 lck_rw_done(pcbinfo->ipi_lock);
1c79356b
A
959}
960
961/*
962 * Lookup a PCB based on the local address and port.
963 */
964struct inpcb *
39236c6e
A
965in6_pcblookup_local(struct inpcbinfo *pcbinfo, struct in6_addr *laddr,
966 u_int lport_arg, int wild_okay)
1c79356b 967{
91447636 968 struct inpcb *inp;
1c79356b
A
969 int matchwild = 3, wildcard;
970 u_short lport = lport_arg;
39236c6e
A
971 struct inpcbporthead *porthash;
972 struct inpcb *match = NULL;
973 struct inpcbport *phd;
1c79356b
A
974
975 if (!wild_okay) {
976 struct inpcbhead *head;
977 /*
978 * Look for an unconnected (wildcard foreign addr) PCB that
979 * matches the local address and port we're looking for.
980 */
39236c6e
A
981 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport, 0,
982 pcbinfo->ipi_hashmask)];
1c79356b 983 LIST_FOREACH(inp, head, inp_hash) {
39236c6e 984 if (!(inp->inp_vflag & INP_IPV6))
1c79356b
A
985 continue;
986 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
987 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
988 inp->inp_lport == lport) {
989 /*
990 * Found.
991 */
992 return (inp);
993 }
994 }
995 /*
996 * Not found.
997 */
998 return (NULL);
39236c6e
A
999 }
1000 /*
1001 * Best fit PCB lookup.
1002 *
1003 * First see if this local port is in use by looking on the
1004 * port hash list.
1005 */
1006 porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
1007 pcbinfo->ipi_porthashmask)];
1008 LIST_FOREACH(phd, porthash, phd_hash) {
1009 if (phd->phd_port == lport)
1010 break;
1011 }
1012 if (phd != NULL) {
1c79356b 1013 /*
39236c6e
A
1014 * Port is in use by one or more PCBs. Look for best
1015 * fit.
1c79356b 1016 */
39236c6e
A
1017 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
1018 wildcard = 0;
1019 if (!(inp->inp_vflag & INP_IPV6))
1020 continue;
1021 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
1022 wildcard++;
1023 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1024 if (IN6_IS_ADDR_UNSPECIFIED(laddr))
1025 wildcard++;
1026 else if (!IN6_ARE_ADDR_EQUAL(
1027 &inp->in6p_laddr, laddr))
1c79356b 1028 continue;
39236c6e
A
1029 } else {
1030 if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
1c79356b 1031 wildcard++;
39236c6e
A
1032 }
1033 if (wildcard < matchwild) {
1034 match = inp;
1035 matchwild = wildcard;
1036 if (matchwild == 0) {
1037 break;
1c79356b
A
1038 }
1039 }
1040 }
1c79356b 1041 }
39236c6e 1042 return (match);
1c79356b 1043}
9bccf70c 1044
1c79356b
A
1045/*
1046 * Check for alternatives when higher level complains
1047 * about service problems. For now, invalidate cached
1048 * routing information. If the route was created dynamically
1049 * (by a redirect), time to try a default gateway again.
1050 */
1051void
39236c6e 1052in6_losing(struct inpcb *in6p)
1c79356b
A
1053{
1054 struct rtentry *rt;
1c79356b
A
1055
1056 if ((rt = in6p->in6p_route.ro_rt) != NULL) {
b0d623f7 1057 RT_LOCK(rt);
b0d623f7
A
1058 if (rt->rt_flags & RTF_DYNAMIC) {
1059 /*
1060 * Prevent another thread from modifying rt_key,
1061 * rt_gateway via rt_setgate() after the rt_lock
1062 * is dropped by marking the route as defunct.
1063 */
1064 rt->rt_flags |= RTF_CONDEMNED;
1065 RT_UNLOCK(rt);
1066 (void) rtrequest(RTM_DELETE, rt_key(rt),
1067 rt->rt_gateway, rt_mask(rt), rt->rt_flags, NULL);
1068 } else {
1069 RT_UNLOCK(rt);
1070 }
1c79356b
A
1071 /*
1072 * A new route can be allocated
1073 * the next time output is attempted.
1074 */
1c79356b 1075 }
39236c6e 1076 ROUTE_RELEASE(&in6p->in6p_route);
1c79356b
A
1077}
1078
1079/*
1080 * After a routing change, flush old routing
1081 * and allocate a (hopefully) better one.
1082 */
1083void
39236c6e 1084in6_rtchange(struct inpcb *inp, int errno)
1c79356b 1085{
39236c6e
A
1086#pragma unused(errno)
1087 /*
1088 * A new route can be allocated the next time
1089 * output is attempted.
1090 */
1091 ROUTE_RELEASE(&inp->in6p_route);
1c79356b
A
1092}
1093
6d2010ae
A
1094/*
1095 * Check if PCB exists hash list. Also returns uid and gid of socket
1096 */
1097int
39236c6e
A
1098in6_pcblookup_hash_exists(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1099 u_int fport_arg, struct in6_addr *laddr, u_int lport_arg, int wildcard,
1100 uid_t *uid, gid_t *gid, struct ifnet *ifp)
6d2010ae
A
1101{
1102 struct inpcbhead *head;
1103 struct inpcb *inp;
1104 u_short fport = fport_arg, lport = lport_arg;
6d2010ae
A
1105 int found;
1106
6d2010ae
A
1107 *uid = UID_MAX;
1108 *gid = GID_MAX;
1109
39236c6e 1110 lck_rw_lock_shared(pcbinfo->ipi_lock);
6d2010ae
A
1111
1112 /*
1113 * First look for an exact match.
1114 */
39236c6e
A
1115 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr->s6_addr32[3] /* XXX */,
1116 lport, fport, pcbinfo->ipi_hashmask)];
6d2010ae 1117 LIST_FOREACH(inp, head, inp_hash) {
39236c6e 1118 if (!(inp->inp_vflag & INP_IPV6))
6d2010ae 1119 continue;
316670eb 1120
fe8ab488 1121 if (inp_restricted_recv(inp, ifp))
316670eb
A
1122 continue;
1123
6d2010ae
A
1124 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
1125 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
1126 inp->inp_fport == fport &&
1127 inp->inp_lport == lport) {
1128 if ((found = (inp->inp_socket != NULL))) {
1129 /*
1130 * Found. Check if pcb is still valid
1131 */
316670eb
A
1132 *uid = kauth_cred_getuid(
1133 inp->inp_socket->so_cred);
1134 *gid = kauth_cred_getgid(
1135 inp->inp_socket->so_cred);
6d2010ae 1136 }
39236c6e 1137 lck_rw_done(pcbinfo->ipi_lock);
6d2010ae
A
1138 return (found);
1139 }
1140 }
1141 if (wildcard) {
1142 struct inpcb *local_wild = NULL;
1143
39236c6e
A
1144 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport, 0,
1145 pcbinfo->ipi_hashmask)];
6d2010ae 1146 LIST_FOREACH(inp, head, inp_hash) {
39236c6e 1147 if (!(inp->inp_vflag & INP_IPV6))
6d2010ae 1148 continue;
316670eb 1149
fe8ab488 1150 if (inp_restricted_recv(inp, ifp))
316670eb
A
1151 continue;
1152
6d2010ae
A
1153 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
1154 inp->inp_lport == lport) {
6d2010ae 1155 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
39236c6e
A
1156 laddr)) {
1157 found = (inp->inp_socket != NULL);
1158 if (found) {
316670eb
A
1159 *uid = kauth_cred_getuid(
1160 inp->inp_socket->so_cred);
1161 *gid = kauth_cred_getgid(
1162 inp->inp_socket->so_cred);
6d2010ae 1163 }
39236c6e 1164 lck_rw_done(pcbinfo->ipi_lock);
6d2010ae 1165 return (found);
39236c6e
A
1166 } else if (IN6_IS_ADDR_UNSPECIFIED(
1167 &inp->in6p_laddr)) {
6d2010ae 1168 local_wild = inp;
39236c6e 1169 }
6d2010ae
A
1170 }
1171 }
1172 if (local_wild) {
1173 if ((found = (local_wild->inp_socket != NULL))) {
316670eb
A
1174 *uid = kauth_cred_getuid(
1175 local_wild->inp_socket->so_cred);
1176 *gid = kauth_cred_getgid(
1177 local_wild->inp_socket->so_cred);
6d2010ae 1178 }
39236c6e 1179 lck_rw_done(pcbinfo->ipi_lock);
6d2010ae
A
1180 return (found);
1181 }
1182 }
1183
1184 /*
1185 * Not found.
1186 */
39236c6e 1187 lck_rw_done(pcbinfo->ipi_lock);
6d2010ae
A
1188 return (0);
1189}
1190
1c79356b
A
1191/*
1192 * Lookup PCB in hash list.
1193 */
1194struct inpcb *
39236c6e
A
1195in6_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1196 u_int fport_arg, struct in6_addr *laddr, u_int lport_arg, int wildcard,
1197 struct ifnet *ifp)
1c79356b
A
1198{
1199 struct inpcbhead *head;
91447636 1200 struct inpcb *inp;
1c79356b
A
1201 u_short fport = fport_arg, lport = lport_arg;
1202
39236c6e 1203 lck_rw_lock_shared(pcbinfo->ipi_lock);
91447636 1204
1c79356b
A
1205 /*
1206 * First look for an exact match.
1207 */
39236c6e
A
1208 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr->s6_addr32[3] /* XXX */,
1209 lport, fport, pcbinfo->ipi_hashmask)];
9bccf70c 1210 LIST_FOREACH(inp, head, inp_hash) {
39236c6e
A
1211 if (!(inp->inp_vflag & INP_IPV6))
1212 continue;
1213
fe8ab488 1214 if (inp_restricted_recv(inp, ifp))
316670eb
A
1215 continue;
1216
1c79356b
A
1217 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
1218 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
1219 inp->inp_fport == fport &&
1220 inp->inp_lport == lport) {
1221 /*
39236c6e
A
1222 * Found. Check if pcb is still valid
1223 */
1224 if (in_pcb_checkstate(inp, WNT_ACQUIRE, 0) !=
1225 WNT_STOPUSING) {
1226 lck_rw_done(pcbinfo->ipi_lock);
91447636 1227 return (inp);
39236c6e
A
1228 } else {
1229 /* it's there but dead, say it isn't found */
1230 lck_rw_done(pcbinfo->ipi_lock);
316670eb 1231 return (NULL);
91447636 1232 }
1c79356b
A
1233 }
1234 }
1235 if (wildcard) {
1236 struct inpcb *local_wild = NULL;
1237
39236c6e
A
1238 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport, 0,
1239 pcbinfo->ipi_hashmask)];
9bccf70c 1240 LIST_FOREACH(inp, head, inp_hash) {
39236c6e
A
1241 if (!(inp->inp_vflag & INP_IPV6))
1242 continue;
1243
fe8ab488 1244 if (inp_restricted_recv(inp, ifp))
316670eb
A
1245 continue;
1246
1c79356b
A
1247 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
1248 inp->inp_lport == lport) {
1c79356b 1249 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
39236c6e
A
1250 laddr)) {
1251 if (in_pcb_checkstate(inp, WNT_ACQUIRE,
1252 0) != WNT_STOPUSING) {
1253 lck_rw_done(pcbinfo->ipi_lock);
91447636 1254 return (inp);
39236c6e
A
1255 } else {
1256 /* dead; say it isn't found */
1257 lck_rw_done(pcbinfo->ipi_lock);
316670eb 1258 return (NULL);
91447636 1259 }
39236c6e
A
1260 } else if (IN6_IS_ADDR_UNSPECIFIED(
1261 &inp->in6p_laddr)) {
1c79356b 1262 local_wild = inp;
39236c6e 1263 }
1c79356b
A
1264 }
1265 }
39236c6e
A
1266 if (local_wild && in_pcb_checkstate(local_wild,
1267 WNT_ACQUIRE, 0) != WNT_STOPUSING) {
1268 lck_rw_done(pcbinfo->ipi_lock);
91447636 1269 return (local_wild);
39236c6e
A
1270 } else {
1271 lck_rw_done(pcbinfo->ipi_lock);
91447636
A
1272 return (NULL);
1273 }
1c79356b
A
1274 }
1275
1276 /*
1277 * Not found.
1278 */
39236c6e 1279 lck_rw_done(pcbinfo->ipi_lock);
1c79356b
A
1280 return (NULL);
1281}
1282
1283void
1284init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m)
1285{
1286 struct ip6_hdr *ip;
1287
1288 ip = mtod(m, struct ip6_hdr *);
39236c6e
A
1289 bzero(sin6, sizeof (*sin6));
1290 sin6->sin6_len = sizeof (*sin6);
1c79356b
A
1291 sin6->sin6_family = AF_INET6;
1292 sin6->sin6_addr = ip->ip6_src;
39236c6e 1293 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) {
1c79356b 1294 sin6->sin6_addr.s6_addr16[1] = 0;
39236c6e
A
1295 if ((m->m_pkthdr.pkt_flags & (PKTF_LOOP|PKTF_IFAINFO)) ==
1296 (PKTF_LOOP|PKTF_IFAINFO))
1297 sin6->sin6_scope_id = m->m_pkthdr.src_ifindex;
1298 else if (m->m_pkthdr.rcvif != NULL)
1299 sin6->sin6_scope_id = m->m_pkthdr.rcvif->if_index;
1300 }
1c79356b 1301}
316670eb 1302
39236c6e
A
1303/*
1304 * The following routines implement this scheme:
1305 *
1306 * Callers of ip6_output() that intend to cache the route in the inpcb pass
1307 * a local copy of the struct route to ip6_output(). Using a local copy of
1308 * the cached route significantly simplifies things as IP no longer has to
1309 * worry about having exclusive access to the passed in struct route, since
1310 * it's defined in the caller's stack; in essence, this allows for a lock-
1311 * less operation when updating the struct route at the IP level and below,
1312 * whenever necessary. The scheme works as follows:
1313 *
1314 * Prior to dropping the socket's lock and calling ip6_output(), the caller
1315 * copies the struct route from the inpcb into its stack, and adds a reference
1316 * to the cached route entry, if there was any. The socket's lock is then
1317 * dropped and ip6_output() is called with a pointer to the copy of struct
1318 * route defined on the stack (not to the one in the inpcb.)
1319 *
1320 * Upon returning from ip6_output(), the caller then acquires the socket's
1321 * lock and synchronizes the cache; if there is no route cached in the inpcb,
1322 * it copies the local copy of struct route (which may or may not contain any
1323 * route) back into the cache; otherwise, if the inpcb has a route cached in
1324 * it, the one in the local copy will be freed, if there's any. Trashing the
1325 * cached route in the inpcb can be avoided because ip6_output() is single-
1326 * threaded per-PCB (i.e. multiple transmits on a PCB are always serialized
1327 * by the socket/transport layer.)
1328 */
316670eb
A
1329void
1330in6p_route_copyout(struct inpcb *inp, struct route_in6 *dst)
1331{
1332 struct route_in6 *src = &inp->in6p_route;
1333
1334 lck_mtx_assert(&inp->inpcb_mtx, LCK_MTX_ASSERT_OWNED);
1335
1336 /* Minor sanity check */
1337 if (src->ro_rt != NULL && rt_key(src->ro_rt)->sa_family != AF_INET6)
1338 panic("%s: wrong or corrupted route: %p", __func__, src);
39236c6e
A
1339
1340 route_copyout((struct route *)dst, (struct route *)src, sizeof (*dst));
316670eb
A
1341}
1342
1343void
1344in6p_route_copyin(struct inpcb *inp, struct route_in6 *src)
1345{
1346 struct route_in6 *dst = &inp->in6p_route;
1347
1348 lck_mtx_assert(&inp->inpcb_mtx, LCK_MTX_ASSERT_OWNED);
1349
1350 /* Minor sanity check */
1351 if (src->ro_rt != NULL && rt_key(src->ro_rt)->sa_family != AF_INET6)
1352 panic("%s: wrong or corrupted route: %p", __func__, src);
1353
39236c6e 1354 route_copyin((struct route *)src, (struct route *)dst, sizeof (*src));
316670eb 1355}