]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet6/in6_pcb.c
2 * Copyright (c) 2003-2008 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
30 * All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
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.
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
59 * Copyright (c) 1982, 1986, 1991, 1993
60 * The Regents of the University of California. All rights reserved.
62 * Redistribution and use in source and binary forms, with or without
63 * modification, are permitted provided that the following conditions
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.
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
90 * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94
93 #include <sys/param.h>
94 #include <sys/systm.h>
95 #include <sys/malloc.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>
107 #include <net/if_types.h>
108 #include <net/route.h>
110 #include <netinet/in.h>
111 #include <netinet/in_var.h>
112 #include <netinet/in_systm.h>
113 #include <netinet/ip6.h>
114 #include <netinet/ip_var.h>
115 #include <netinet6/ip6_var.h>
116 #include <netinet6/nd6.h>
117 #include <netinet/in_pcb.h>
118 #include <netinet6/in6_pcb.h>
119 #include <net/if_types.h>
121 #include <kern/kern_types.h>
122 #include <kern/zalloc.h>
125 #include <netinet6/ipsec.h>
127 #include <netinet6/ipsec6.h>
129 #include <netinet6/ah.h>
131 #include <netinet6/ah6.h>
133 #include <netkey/key.h>
136 struct in6_addr zeroin6_addr
;
139 in6_pcblookup_local_and_cleanup does everything
140 in6_pcblookup_local does but it checks for a socket
141 that's going away. Since we know that the lock is
142 held read+write when this function is called, we
143 can safely dispose of this socket like the slow
144 timer would usually do and return NULL. This is
148 in6_pcblookup_local_and_cleanup(
149 struct inpcbinfo
*pcbinfo
,
150 struct in6_addr
*laddr
,
156 /* Perform normal lookup */
157 inp
= in6_pcblookup_local(pcbinfo
, laddr
, lport_arg
, wild_okay
);
159 /* Check if we found a match but it's waiting to be disposed */
160 if (inp
&& inp
->inp_wantcnt
== WNT_STOPUSING
) {
161 struct socket
*so
= inp
->inp_socket
;
163 lck_mtx_lock(inp
->inpcb_mtx
);
165 if (so
->so_usecount
== 0) {
166 if (inp
->inp_state
!= INPCB_STATE_DEAD
)
172 lck_mtx_unlock(inp
->inpcb_mtx
);
181 struct sockaddr
*nam
,
184 struct socket
*so
= inp
->inp_socket
;
185 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)NULL
;
186 struct inpcbinfo
*pcbinfo
= inp
->inp_pcbinfo
;
188 int wild
= 0, reuseport
= (so
->so_options
& SO_REUSEPORT
);
190 if (!in6_ifaddrs
) /* XXX broken! */
191 return (EADDRNOTAVAIL
);
192 if (inp
->inp_lport
|| !IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
))
194 if ((so
->so_options
& (SO_REUSEADDR
|SO_REUSEPORT
)) == 0)
196 socket_unlock(so
, 0); /* keep reference */
197 lck_rw_lock_exclusive(pcbinfo
->mtx
);
199 sin6
= (struct sockaddr_in6
*)nam
;
200 if (nam
->sa_len
!= sizeof(*sin6
)) {
201 lck_rw_done(pcbinfo
->mtx
);
208 if (nam
->sa_family
!= AF_INET6
) {
209 lck_rw_done(pcbinfo
->mtx
);
211 return(EAFNOSUPPORT
);
214 /* KAME hack: embed scopeid */
215 if (in6_embedscope(&sin6
->sin6_addr
, sin6
, inp
, NULL
) != 0) {
216 lck_rw_done(pcbinfo
->mtx
);
220 /* this must be cleared for ifa_ifwithaddr() */
221 sin6
->sin6_scope_id
= 0;
223 lport
= sin6
->sin6_port
;
224 if (IN6_IS_ADDR_MULTICAST(&sin6
->sin6_addr
)) {
226 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
227 * allow compepte duplication of binding if
228 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
229 * and a multicast address is bound on both
230 * new and duplicated sockets.
232 if (so
->so_options
& SO_REUSEADDR
)
233 reuseport
= SO_REUSEADDR
|SO_REUSEPORT
;
234 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6
->sin6_addr
)) {
235 struct ifaddr
*ia
= NULL
;
237 sin6
->sin6_port
= 0; /* yech... */
238 if ((ia
= ifa_ifwithaddr((struct sockaddr
*)sin6
)) == 0) {
239 lck_rw_done(pcbinfo
->mtx
);
241 return(EADDRNOTAVAIL
);
245 * XXX: bind to an anycast address might accidentally
246 * cause sending a packet with anycast source address.
247 * We should allow to bind to a deprecated address, since
248 * the application dare to use it.
251 ((struct in6_ifaddr
*)ia
)->ia6_flags
&
252 (IN6_IFF_ANYCAST
|IN6_IFF_NOTREADY
|IN6_IFF_DETACHED
)) {
254 lck_rw_done(pcbinfo
->mtx
);
256 return(EADDRNOTAVAIL
);
265 if (ntohs(lport
) < IPV6PORT_RESERVED
&&
266 ((so
->so_state
& SS_PRIV
) == 0)) {
267 lck_rw_done(pcbinfo
->mtx
);
273 !IN6_IS_ADDR_MULTICAST(&sin6
->sin6_addr
)) {
274 t
= in6_pcblookup_local_and_cleanup(pcbinfo
,
275 &sin6
->sin6_addr
, lport
,
278 (!IN6_IS_ADDR_UNSPECIFIED(&sin6
->sin6_addr
) ||
279 !IN6_IS_ADDR_UNSPECIFIED(&t
->in6p_laddr
) ||
280 (t
->inp_socket
->so_options
&
281 SO_REUSEPORT
) == 0) &&
282 (so
->so_uid
!= t
->inp_socket
->so_uid
) &&
283 ((t
->inp_socket
->so_flags
& SOF_REUSESHAREUID
) == 0)) {
284 lck_rw_done(pcbinfo
->mtx
);
288 if ((inp
->inp_flags
& IN6P_IPV6_V6ONLY
) == 0 &&
289 IN6_IS_ADDR_UNSPECIFIED(&sin6
->sin6_addr
)) {
290 struct sockaddr_in sin
;
292 in6_sin6_2_sin(&sin
, sin6
);
293 t
= in_pcblookup_local_and_cleanup(pcbinfo
,
296 if (t
&& (t
->inp_socket
->so_options
& SO_REUSEPORT
) == 0 &&
298 t
->inp_socket
->so_uid
) &&
299 (ntohl(t
->inp_laddr
.s_addr
) !=
302 INP_SOCKAF(t
->inp_socket
))) {
304 lck_rw_done(pcbinfo
->mtx
);
310 t
= in6_pcblookup_local_and_cleanup(pcbinfo
, &sin6
->sin6_addr
,
312 if (t
&& (reuseport
& t
->inp_socket
->so_options
) == 0) {
313 lck_rw_done(pcbinfo
->mtx
);
317 if ((inp
->inp_flags
& IN6P_IPV6_V6ONLY
) == 0 &&
318 IN6_IS_ADDR_UNSPECIFIED(&sin6
->sin6_addr
)) {
319 struct sockaddr_in sin
;
321 in6_sin6_2_sin(&sin
, sin6
);
322 t
= in_pcblookup_local_and_cleanup(pcbinfo
, sin
.sin_addr
,
325 (reuseport
& t
->inp_socket
->so_options
)
327 (ntohl(t
->inp_laddr
.s_addr
)
330 INP_SOCKAF(t
->inp_socket
))) {
331 lck_rw_done(pcbinfo
->mtx
);
337 inp
->in6p_laddr
= sin6
->sin6_addr
;
342 if ((e
= in6_pcbsetport(&inp
->in6p_laddr
, inp
, p
, 1)) != 0) {
343 lck_rw_done(pcbinfo
->mtx
);
348 inp
->inp_lport
= lport
;
349 if (in_pcbinshash(inp
, 1) != 0) {
350 inp
->in6p_laddr
= in6addr_any
;
352 lck_rw_done(pcbinfo
->mtx
);
356 lck_rw_done(pcbinfo
->mtx
);
357 sflt_notify(so
, sock_evt_bound
, NULL
);
362 * Transform old in6_pcbconnect() into an inner subroutine for new
363 * in6_pcbconnect(): Do some validity-checking on the remote
364 * address (in mbuf 'nam') and then determine local host address
365 * (i.e., which interface) to use to access that remote host.
367 * This preserves definition of in6_pcbconnect(), while supporting a
368 * slightly different version for T/TCP. (This is more than
369 * a bit of a kludge, but cleaning up the internal interfaces would
370 * have forced minor changes in every protocol).
376 struct sockaddr
*nam
,
377 struct in6_addr
*plocal_addr6
)
379 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)nam
;
380 struct in6_addr
*addr6
= NULL
;
381 struct in6_addr src_storage
;
383 struct ifnet
*ifp
= NULL
;
386 if (nam
->sa_len
!= sizeof (*sin6
))
388 if (sin6
->sin6_family
!= AF_INET6
)
389 return (EAFNOSUPPORT
);
390 if (sin6
->sin6_port
== 0)
391 return (EADDRNOTAVAIL
);
393 /* KAME hack: embed scopeid */
394 if (in6_embedscope(&sin6
->sin6_addr
, sin6
, inp
, &ifp
) != 0)
399 * If the destination address is UNSPECIFIED addr,
400 * use the loopback addr, e.g ::1.
402 if (IN6_IS_ADDR_UNSPECIFIED(&sin6
->sin6_addr
))
403 sin6
->sin6_addr
= in6addr_loopback
;
407 * XXX: in6_selectsrc might replace the bound local address
408 * with the address specified by setsockopt(IPV6_PKTINFO).
409 * Is it the intended behavior?
411 addr6
= in6_selectsrc(sin6
, inp
->in6p_outputopts
,
414 &inp
->in6p_laddr
, &src_storage
, &error
);
417 error
= EADDRNOTAVAIL
;
420 *plocal_addr6
= *addr6
;
422 * Don't do pcblookup call here; return interface in
424 * and exit to caller, that will do the lookup.
428 /* XXX: what is the point in doing this? */
429 if (inp
->in6p_route
.ro_rt
)
430 ifp
= inp
->in6p_route
.ro_rt
->rt_ifp
;
437 * Connect from a socket to a specified address.
438 * Both address and port must be specified in argument sin.
439 * If don't have a local address for this socket yet,
443 in6_pcbconnect(inp
, nam
, p
)
445 struct sockaddr
*nam
;
448 struct in6_addr addr6
;
449 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)nam
;
454 * Call inner routine, to assign local interface address.
455 * in6_pcbladdr() may automatically fill in sin6_scope_id.
457 if ((error
= in6_pcbladdr(inp
, nam
, &addr6
)) != 0)
459 socket_unlock(inp
->inp_socket
, 0);
460 pcb
= in6_pcblookup_hash(inp
->inp_pcbinfo
, &sin6
->sin6_addr
,
462 IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
)
463 ? &addr6
: &inp
->in6p_laddr
,
464 inp
->inp_lport
, 0, NULL
);
465 socket_lock(inp
->inp_socket
, 0);
467 in_pcb_checkstate(pcb
, WNT_RELEASE
, 0);
470 if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
)) {
471 if (inp
->inp_lport
== 0) {
472 error
= in6_pcbbind(inp
, (struct sockaddr
*)0, p
);
476 inp
->in6p_laddr
= addr6
;
478 if (!lck_rw_try_lock_exclusive(inp
->inp_pcbinfo
->mtx
)) {
479 /*lock inversion issue, mostly with udp multicast packets */
480 socket_unlock(inp
->inp_socket
, 0);
481 lck_rw_lock_exclusive(inp
->inp_pcbinfo
->mtx
);
482 socket_lock(inp
->inp_socket
, 0);
484 inp
->in6p_faddr
= sin6
->sin6_addr
;
485 inp
->inp_fport
= sin6
->sin6_port
;
486 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
487 inp
->in6p_flowinfo
&= ~IPV6_FLOWLABEL_MASK
;
488 if (inp
->in6p_flags
& IN6P_AUTOFLOWLABEL
)
489 inp
->in6p_flowinfo
|=
490 (htonl(ip6_flow_seq
++) & IPV6_FLOWLABEL_MASK
);
493 lck_rw_done(inp
->inp_pcbinfo
->mtx
);
498 in6_pcbdisconnect(inp
)
501 if (!lck_rw_try_lock_exclusive(inp
->inp_pcbinfo
->mtx
)) {
502 /*lock inversion issue, mostly with udp multicast packets */
503 socket_unlock(inp
->inp_socket
, 0);
504 lck_rw_lock_exclusive(inp
->inp_pcbinfo
->mtx
);
505 socket_lock(inp
->inp_socket
, 0);
507 bzero((caddr_t
)&inp
->in6p_faddr
, sizeof(inp
->in6p_faddr
));
509 /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
510 inp
->in6p_flowinfo
&= ~IPV6_FLOWLABEL_MASK
;
512 lck_rw_done(inp
->inp_pcbinfo
->mtx
);
513 if (inp
->inp_socket
->so_state
& SS_NOFDREF
)
521 struct socket
*so
= inp
->inp_socket
;
522 struct inpcbinfo
*ipi
= inp
->inp_pcbinfo
;
525 if (inp
->in6p_sp
!= NULL
) {
526 ipsec6_delete_pcbpolicy(inp
);
530 if (in_pcb_checkstate(inp
, WNT_STOPUSING
, 1) != WNT_STOPUSING
)
531 printf("in6_pcbdetach so=%p can't be marked dead ok\n", so
);
533 inp
->inp_state
= INPCB_STATE_DEAD
;
535 if ((so
->so_flags
& SOF_PCBCLEARING
) == 0) {
537 so
->so_flags
|= SOF_PCBCLEARING
;
538 inp
->inp_gencnt
= ++ipi
->ipi_gencnt
;
539 if (inp
->in6p_options
)
540 m_freem(inp
->in6p_options
);
541 ip6_freepcbopts(inp
->in6p_outputopts
);
542 ip6_freemoptions(inp
->in6p_moptions
);
543 if (inp
->in6p_route
.ro_rt
) {
544 rtfree(inp
->in6p_route
.ro_rt
);
545 inp
->in6p_route
.ro_rt
= NULL
;
547 /* Check and free IPv4 related resources in case of mapped addr */
548 if (inp
->inp_options
)
549 (void)m_free(inp
->inp_options
);
550 ip_freemoptions(inp
->inp_moptions
);
551 inp
->inp_moptions
= NULL
;
557 in6_sockaddr(port
, addr_p
)
559 struct in6_addr
*addr_p
;
561 struct sockaddr_in6
*sin6
;
563 MALLOC(sin6
, struct sockaddr_in6
*, sizeof *sin6
, M_SONAME
, M_WAITOK
);
566 bzero(sin6
, sizeof *sin6
);
567 sin6
->sin6_family
= AF_INET6
;
568 sin6
->sin6_len
= sizeof(*sin6
);
569 sin6
->sin6_port
= port
;
570 sin6
->sin6_addr
= *addr_p
;
571 if (IN6_IS_SCOPE_LINKLOCAL(&sin6
->sin6_addr
))
572 sin6
->sin6_scope_id
= ntohs(sin6
->sin6_addr
.s6_addr16
[1]);
574 sin6
->sin6_scope_id
= 0; /*XXX*/
575 if (IN6_IS_SCOPE_LINKLOCAL(&sin6
->sin6_addr
))
576 sin6
->sin6_addr
.s6_addr16
[1] = 0;
578 return (struct sockaddr
*)sin6
;
582 in6_v4mapsin6_sockaddr(port
, addr_p
)
584 struct in_addr
*addr_p
;
586 struct sockaddr_in sin
;
587 struct sockaddr_in6
*sin6_p
;
589 bzero(&sin
, sizeof sin
);
590 sin
.sin_family
= AF_INET
;
591 sin
.sin_len
= sizeof(sin
);
593 sin
.sin_addr
= *addr_p
;
595 MALLOC(sin6_p
, struct sockaddr_in6
*, sizeof *sin6_p
, M_SONAME
,
599 in6_sin_2_v4mapsin6(&sin
, sin6_p
);
601 return (struct sockaddr
*)sin6_p
;
605 * The calling convention of in6_setsockaddr() and in6_setpeeraddr() was
606 * modified to match the pru_sockaddr() and pru_peeraddr() entry points
607 * in struct pr_usrreqs, so that protocols can just reference then directly
608 * without the need for a wrapper function. The socket must have a valid
609 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
610 * except through a kernel programming error, so it is acceptable to panic
611 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap
612 * because there actually /is/ a programming error somewhere... XXX)
615 in6_setsockaddr(so
, nam
)
617 struct sockaddr
**nam
;
620 struct in6_addr addr
;
627 port
= inp
->inp_lport
;
628 addr
= inp
->in6p_laddr
;
630 *nam
= in6_sockaddr(port
, &addr
);
637 in6_setpeeraddr(so
, nam
)
639 struct sockaddr
**nam
;
642 struct in6_addr addr
;
649 port
= inp
->inp_fport
;
650 addr
= inp
->in6p_faddr
;
652 *nam
= in6_sockaddr(port
, &addr
);
659 in6_mapped_sockaddr(struct socket
*so
, struct sockaddr
**nam
)
661 struct inpcb
*inp
= sotoinpcb(so
);
666 if (inp
->inp_vflag
& INP_IPV4
) {
667 error
= in_setsockaddr(so
, nam
);
669 error
= in6_sin_2_v4mapsin6_in_sock(nam
);
671 /* scope issues will be handled in in6_setsockaddr(). */
672 error
= in6_setsockaddr(so
, nam
);
678 in6_mapped_peeraddr(struct socket
*so
, struct sockaddr
**nam
)
680 struct inpcb
*inp
= sotoinpcb(so
);
685 if (inp
->inp_vflag
& INP_IPV4
) {
686 error
= in_setpeeraddr(so
, nam
);
688 error
= in6_sin_2_v4mapsin6_in_sock(nam
);
690 /* scope issues will be handled in in6_setpeeraddr(). */
691 error
= in6_setpeeraddr(so
, nam
);
697 * Pass some notification to all connections of a protocol
698 * associated with address dst. The local address and/or port numbers
699 * may be specified to limit the search. The "usual action" will be
700 * taken, depending on the ctlinput cmd. The caller must filter any
701 * cmds that are uninteresting (e.g., no error in the map).
702 * Call the protocol specific routine (if any) to report
703 * any errors for each matching socket.
705 * Must be called at splnet.
708 in6_pcbnotify(pcbinfo
, dst
, fport_arg
, src
, lport_arg
, cmd
, notify
)
709 struct inpcbinfo
*pcbinfo
;
710 struct sockaddr
*dst
;
711 const struct sockaddr
*src
;
712 u_int fport_arg
, lport_arg
;
714 // struct inpcb *(*notify)(struct inpcb *, int);
715 void (*notify
)(struct inpcb
*, int);
717 struct inpcb
*inp
, *ninp
;
718 struct sockaddr_in6 sa6_src
, *sa6_dst
;
719 u_short fport
= fport_arg
, lport
= lport_arg
;
722 struct inpcbhead
*head
= pcbinfo
->listhead
;
724 if ((unsigned)cmd
> PRC_NCMDS
|| dst
->sa_family
!= AF_INET6
)
727 sa6_dst
= (struct sockaddr_in6
*)dst
;
728 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst
->sin6_addr
))
732 * note that src can be NULL when we get notify by local fragmentation.
734 sa6_src
= (src
== NULL
) ? sa6_any
: *(const struct sockaddr_in6
*)src
;
735 flowinfo
= sa6_src
.sin6_flowinfo
;
738 * Redirects go to all references to the destination,
739 * and use in6_rtchange to invalidate the route cache.
740 * Dead host indications: also use in6_rtchange to invalidate
741 * the cache, and deliver the error to all the sockets.
742 * Otherwise, if we have knowledge of the local port and address,
743 * deliver only to that socket.
745 if (PRC_IS_REDIRECT(cmd
) || cmd
== PRC_HOSTDEAD
) {
748 bzero((caddr_t
)&sa6_src
.sin6_addr
, sizeof(sa6_src
.sin6_addr
));
750 if (cmd
!= PRC_HOSTDEAD
)
751 notify
= in6_rtchange
;
753 errno
= inet6ctlerrmap
[cmd
];
754 lck_rw_lock_shared(pcbinfo
->mtx
);
755 for (inp
= LIST_FIRST(head
); inp
!= NULL
; inp
= ninp
) {
756 ninp
= LIST_NEXT(inp
, inp_list
);
758 if ((inp
->inp_vflag
& INP_IPV6
) == 0)
762 * Detect if we should notify the error. If no source and
763 * destination ports are specifed, but non-zero flowinfo and
764 * local address match, notify the error. This is the case
765 * when the error is delivered with an encrypted buffer
766 * by ESP. Otherwise, just compare addresses and ports
769 if (lport
== 0 && fport
== 0 && flowinfo
&&
770 inp
->inp_socket
!= NULL
&&
771 flowinfo
== (inp
->in6p_flowinfo
& IPV6_FLOWLABEL_MASK
) &&
772 IN6_ARE_ADDR_EQUAL(&inp
->in6p_laddr
, &sa6_src
.sin6_addr
))
774 else if (!IN6_ARE_ADDR_EQUAL(&inp
->in6p_faddr
,
775 &sa6_dst
->sin6_addr
) ||
776 inp
->inp_socket
== 0 ||
777 (lport
&& inp
->inp_lport
!= lport
) ||
778 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src
.sin6_addr
) &&
779 !IN6_ARE_ADDR_EQUAL(&inp
->in6p_laddr
,
780 &sa6_src
.sin6_addr
)) ||
781 (fport
&& inp
->inp_fport
!= fport
))
787 if (in_pcb_checkstate(inp
, WNT_ACQUIRE
, 0) == WNT_STOPUSING
)
789 socket_lock(inp
->inp_socket
, 1);
790 (*notify
)(inp
, errno
);
791 (void)in_pcb_checkstate(inp
, WNT_RELEASE
, 1);
792 socket_unlock(inp
->inp_socket
, 1);
795 lck_rw_done(pcbinfo
->mtx
);
799 * Lookup a PCB based on the local address and port.
802 in6_pcblookup_local(pcbinfo
, laddr
, lport_arg
, wild_okay
)
803 struct inpcbinfo
*pcbinfo
;
804 struct in6_addr
*laddr
;
809 int matchwild
= 3, wildcard
;
810 u_short lport
= lport_arg
;
813 struct inpcbhead
*head
;
815 * Look for an unconnected (wildcard foreign addr) PCB that
816 * matches the local address and port we're looking for.
818 head
= &pcbinfo
->hashbase
[INP_PCBHASH(INADDR_ANY
, lport
, 0,
820 LIST_FOREACH(inp
, head
, inp_hash
) {
821 if ((inp
->inp_vflag
& INP_IPV6
) == 0)
823 if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_faddr
) &&
824 IN6_ARE_ADDR_EQUAL(&inp
->in6p_laddr
, laddr
) &&
825 inp
->inp_lport
== lport
) {
837 struct inpcbporthead
*porthash
;
838 struct inpcbport
*phd
;
839 struct inpcb
*match
= NULL
;
841 * Best fit PCB lookup.
843 * First see if this local port is in use by looking on the
846 porthash
= &pcbinfo
->porthashbase
[INP_PCBPORTHASH(lport
,
847 pcbinfo
->porthashmask
)];
848 LIST_FOREACH(phd
, porthash
, phd_hash
) {
849 if (phd
->phd_port
== lport
)
854 * Port is in use by one or more PCBs. Look for best
857 LIST_FOREACH(inp
, &phd
->phd_pcblist
, inp_portlist
) {
859 if ((inp
->inp_vflag
& INP_IPV6
) == 0)
861 if (!IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_faddr
))
863 if (!IN6_IS_ADDR_UNSPECIFIED(
865 if (IN6_IS_ADDR_UNSPECIFIED(laddr
))
867 else if (!IN6_ARE_ADDR_EQUAL(
868 &inp
->in6p_laddr
, laddr
))
871 if (!IN6_IS_ADDR_UNSPECIFIED(laddr
))
874 if (wildcard
< matchwild
) {
876 matchwild
= wildcard
;
877 if (matchwild
== 0) {
887 /* this is not used in Darwin */
894 struct ip6_moptions
*im6o
;
895 struct in6_multi_mship
*imm
, *nimm
;
897 for (in6p
= head
; in6p
!= NULL
; in6p
= LIST_NEXT(in6p
, inp_list
)) {
898 im6o
= in6p
->in6p_moptions
;
899 if ((in6p
->inp_vflag
& INP_IPV6
) &&
902 * Unselect the outgoing interface if it is being
905 if (im6o
->im6o_multicast_ifp
== ifp
)
906 im6o
->im6o_multicast_ifp
= NULL
;
909 * Drop multicast group membership if we joined
910 * through the interface being detached.
911 * XXX controversial - is it really legal for kernel
914 for (imm
= im6o
->im6o_memberships
.lh_first
;
915 imm
!= NULL
; imm
= nimm
) {
916 nimm
= imm
->i6mm_chain
.le_next
;
917 if (imm
->i6mm_maddr
->in6m_ifp
== ifp
) {
918 LIST_REMOVE(imm
, i6mm_chain
);
919 in6_delmulti(imm
->i6mm_maddr
);
920 FREE(imm
, M_IPMADDR
);
929 * Check for alternatives when higher level complains
930 * about service problems. For now, invalidate cached
931 * routing information. If the route was created dynamically
932 * (by a redirect), time to try a default gateway again.
939 struct rt_addrinfo info
;
941 if ((rt
= in6p
->in6p_route
.ro_rt
) != NULL
) {
942 in6p
->in6p_route
.ro_rt
= NULL
;
944 bzero((caddr_t
)&info
, sizeof(info
));
945 info
.rti_info
[RTAX_DST
] =
946 (struct sockaddr
*)&in6p
->in6p_route
.ro_dst
;
947 info
.rti_info
[RTAX_GATEWAY
] = rt
->rt_gateway
;
948 info
.rti_info
[RTAX_NETMASK
] = rt_mask(rt
);
949 rt_missmsg(RTM_LOSING
, &info
, rt
->rt_flags
, 0);
950 if (rt
->rt_flags
& RTF_DYNAMIC
) {
952 * Prevent another thread from modifying rt_key,
953 * rt_gateway via rt_setgate() after the rt_lock
954 * is dropped by marking the route as defunct.
956 rt
->rt_flags
|= RTF_CONDEMNED
;
958 (void) rtrequest(RTM_DELETE
, rt_key(rt
),
959 rt
->rt_gateway
, rt_mask(rt
), rt
->rt_flags
, NULL
);
964 * A new route can be allocated
965 * the next time output is attempted.
972 * After a routing change, flush old routing
973 * and allocate a (hopefully) better one.
980 if (inp
->in6p_route
.ro_rt
) {
981 rtfree(inp
->in6p_route
.ro_rt
);
982 inp
->in6p_route
.ro_rt
= 0;
984 * A new route can be allocated the next time
985 * output is attempted.
991 * Lookup PCB in hash list.
995 struct inpcbinfo
*pcbinfo
,
996 struct in6_addr
*faddr
,
998 struct in6_addr
*laddr
,
1001 __unused
struct ifnet
*ifp
)
1003 struct inpcbhead
*head
;
1005 u_short fport
= fport_arg
, lport
= lport_arg
;
1008 #if defined(NFAITH) && NFAITH > 0
1009 faith
= faithprefix(laddr
);
1014 lck_rw_lock_shared(pcbinfo
->mtx
);
1017 * First look for an exact match.
1019 head
= &pcbinfo
->hashbase
[INP_PCBHASH(faddr
->s6_addr32
[3] /* XXX */,
1021 pcbinfo
->hashmask
)];
1022 LIST_FOREACH(inp
, head
, inp_hash
) {
1023 if ((inp
->inp_vflag
& INP_IPV6
) == 0)
1025 if (IN6_ARE_ADDR_EQUAL(&inp
->in6p_faddr
, faddr
) &&
1026 IN6_ARE_ADDR_EQUAL(&inp
->in6p_laddr
, laddr
) &&
1027 inp
->inp_fport
== fport
&&
1028 inp
->inp_lport
== lport
) {
1030 * Found. Check if pcb is still valid
1032 if (in_pcb_checkstate(inp
, WNT_ACQUIRE
, 0) != WNT_STOPUSING
) {
1033 lck_rw_done(pcbinfo
->mtx
);
1036 else { /* it's there but dead, say it isn't found */
1037 lck_rw_done(pcbinfo
->mtx
);
1043 struct inpcb
*local_wild
= NULL
;
1045 head
= &pcbinfo
->hashbase
[INP_PCBHASH(INADDR_ANY
, lport
, 0,
1046 pcbinfo
->hashmask
)];
1047 LIST_FOREACH(inp
, head
, inp_hash
) {
1048 if ((inp
->inp_vflag
& INP_IPV6
) == 0)
1050 if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_faddr
) &&
1051 inp
->inp_lport
== lport
) {
1052 if (faith
&& (inp
->inp_flags
& INP_FAITH
) == 0)
1054 if (IN6_ARE_ADDR_EQUAL(&inp
->in6p_laddr
,
1056 if (in_pcb_checkstate(inp
, WNT_ACQUIRE
, 0) != WNT_STOPUSING
) {
1057 lck_rw_done(pcbinfo
->mtx
);
1060 else { /* it's there but dead, say it isn't found */
1061 lck_rw_done(pcbinfo
->mtx
);
1065 else if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
))
1069 if (local_wild
&& in_pcb_checkstate(local_wild
, WNT_ACQUIRE
, 0) != WNT_STOPUSING
) {
1070 lck_rw_done(pcbinfo
->mtx
);
1071 return (local_wild
);
1074 lck_rw_done(pcbinfo
->mtx
);
1082 lck_rw_done(pcbinfo
->mtx
);
1087 init_sin6(struct sockaddr_in6
*sin6
, struct mbuf
*m
)
1091 ip
= mtod(m
, struct ip6_hdr
*);
1092 bzero(sin6
, sizeof(*sin6
));
1093 sin6
->sin6_len
= sizeof(*sin6
);
1094 sin6
->sin6_family
= AF_INET6
;
1095 sin6
->sin6_addr
= ip
->ip6_src
;
1096 if (IN6_IS_SCOPE_LINKLOCAL(&sin6
->sin6_addr
))
1097 sin6
->sin6_addr
.s6_addr16
[1] = 0;
1098 sin6
->sin6_scope_id
=
1099 (m
->m_pkthdr
.rcvif
&& IN6_IS_SCOPE_LINKLOCAL(&sin6
->sin6_addr
))
1100 ? m
->m_pkthdr
.rcvif
->if_index
: 0;