]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet6/in6_pcb.c
2 * Copyright (c) 2003-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
25 * All rights reserved.
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. Neither the name of the project nor the names of its contributors
36 * may be used to endorse or promote products derived from this software
37 * without specific prior written permission.
39 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
40 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
42 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
43 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
44 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
45 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
47 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
48 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * Copyright (c) 1982, 1986, 1991, 1993
55 * The Regents of the University of California. All rights reserved.
57 * Redistribution and use in source and binary forms, with or without
58 * modification, are permitted provided that the following conditions
60 * 1. Redistributions of source code must retain the above copyright
61 * notice, this list of conditions and the following disclaimer.
62 * 2. Redistributions in binary form must reproduce the above copyright
63 * notice, this list of conditions and the following disclaimer in the
64 * documentation and/or other materials provided with the distribution.
65 * 3. All advertising materials mentioning features or use of this software
66 * must display the following acknowledgement:
67 * This product includes software developed by the University of
68 * California, Berkeley and its contributors.
69 * 4. Neither the name of the University nor the names of its contributors
70 * may be used to endorse or promote products derived from this software
71 * without specific prior written permission.
73 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
74 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
75 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
76 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
77 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
78 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
79 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
80 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
81 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
82 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
85 * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94
88 #include <sys/param.h>
89 #include <sys/systm.h>
90 #include <sys/malloc.h>
92 #include <sys/domain.h>
93 #include <sys/protosw.h>
94 #include <sys/socket.h>
95 #include <sys/socketvar.h>
96 #include <sys/sockio.h>
97 #include <sys/errno.h>
102 #include <net/if_types.h>
103 #include <net/route.h>
105 #include <netinet/in.h>
106 #include <netinet/in_var.h>
107 #include <netinet/in_systm.h>
108 #include <netinet/ip6.h>
109 #include <netinet/ip_var.h>
110 #include <netinet6/ip6_var.h>
111 #include <netinet6/nd6.h>
112 #include <netinet/in_pcb.h>
113 #include <netinet6/in6_pcb.h>
114 #include <net/if_types.h>
116 #include <kern/kern_types.h>
117 #include <kern/zalloc.h>
120 #if defined(NFAITH) && NFAITH > 0
121 #include <net/if_faith.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>
134 extern lck_mtx_t
*sadb_mutex
;
137 struct in6_addr zeroin6_addr
;
142 struct sockaddr
*nam
,
145 struct socket
*so
= inp
->inp_socket
;
146 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)NULL
;
147 struct inpcbinfo
*pcbinfo
= inp
->inp_pcbinfo
;
149 int wild
= 0, reuseport
= (so
->so_options
& SO_REUSEPORT
);
151 if (!in6_ifaddrs
) /* XXX broken! */
152 return (EADDRNOTAVAIL
);
153 if (inp
->inp_lport
|| !IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
))
155 if ((so
->so_options
& (SO_REUSEADDR
|SO_REUSEPORT
)) == 0)
157 socket_unlock(so
, 0); /* keep reference */
158 lck_rw_lock_exclusive(pcbinfo
->mtx
);
160 sin6
= (struct sockaddr_in6
*)nam
;
161 if (nam
->sa_len
!= sizeof(*sin6
)) {
162 lck_rw_done(pcbinfo
->mtx
);
169 if (nam
->sa_family
!= AF_INET6
) {
170 lck_rw_done(pcbinfo
->mtx
);
172 return(EAFNOSUPPORT
);
175 /* KAME hack: embed scopeid */
176 if (in6_embedscope(&sin6
->sin6_addr
, sin6
, inp
, NULL
) != 0) {
177 lck_rw_done(pcbinfo
->mtx
);
181 /* this must be cleared for ifa_ifwithaddr() */
182 sin6
->sin6_scope_id
= 0;
184 lport
= sin6
->sin6_port
;
185 if (IN6_IS_ADDR_MULTICAST(&sin6
->sin6_addr
)) {
187 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
188 * allow compepte duplication of binding if
189 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
190 * and a multicast address is bound on both
191 * new and duplicated sockets.
193 if (so
->so_options
& SO_REUSEADDR
)
194 reuseport
= SO_REUSEADDR
|SO_REUSEPORT
;
195 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6
->sin6_addr
)) {
196 struct ifaddr
*ia
= NULL
;
198 sin6
->sin6_port
= 0; /* yech... */
199 if ((ia
= ifa_ifwithaddr((struct sockaddr
*)sin6
)) == 0) {
200 lck_rw_done(pcbinfo
->mtx
);
202 return(EADDRNOTAVAIL
);
206 * XXX: bind to an anycast address might accidentally
207 * cause sending a packet with anycast source address.
208 * We should allow to bind to a deprecated address, since
209 * the application dare to use it.
212 ((struct in6_ifaddr
*)ia
)->ia6_flags
&
213 (IN6_IFF_ANYCAST
|IN6_IFF_NOTREADY
|IN6_IFF_DETACHED
)) {
215 lck_rw_done(pcbinfo
->mtx
);
217 return(EADDRNOTAVAIL
);
226 if (ntohs(lport
) < IPV6PORT_RESERVED
&& p
&&
227 ((so
->so_state
& SS_PRIV
) == 0)) {
228 lck_rw_done(pcbinfo
->mtx
);
234 !IN6_IS_ADDR_MULTICAST(&sin6
->sin6_addr
)) {
235 t
= in6_pcblookup_local(pcbinfo
,
236 &sin6
->sin6_addr
, lport
,
239 (!IN6_IS_ADDR_UNSPECIFIED(&sin6
->sin6_addr
) ||
240 !IN6_IS_ADDR_UNSPECIFIED(&t
->in6p_laddr
) ||
241 (t
->inp_socket
->so_options
&
242 SO_REUSEPORT
) == 0) &&
243 so
->so_uid
!= t
->inp_socket
->so_uid
) {
244 lck_rw_done(pcbinfo
->mtx
);
248 if ((inp
->inp_flags
& IN6P_IPV6_V6ONLY
) == 0 &&
249 IN6_IS_ADDR_UNSPECIFIED(&sin6
->sin6_addr
)) {
250 struct sockaddr_in sin
;
252 in6_sin6_2_sin(&sin
, sin6
);
253 t
= in_pcblookup_local(pcbinfo
,
258 t
->inp_socket
->so_uid
) &&
259 (ntohl(t
->inp_laddr
.s_addr
) !=
262 INP_SOCKAF(t
->inp_socket
))) {
264 lck_rw_done(pcbinfo
->mtx
);
270 t
= in6_pcblookup_local(pcbinfo
, &sin6
->sin6_addr
,
272 if (t
&& (reuseport
& t
->inp_socket
->so_options
) == 0) {
273 lck_rw_done(pcbinfo
->mtx
);
277 if ((inp
->inp_flags
& IN6P_IPV6_V6ONLY
) == 0 &&
278 IN6_IS_ADDR_UNSPECIFIED(&sin6
->sin6_addr
)) {
279 struct sockaddr_in sin
;
281 in6_sin6_2_sin(&sin
, sin6
);
282 t
= in_pcblookup_local(pcbinfo
, sin
.sin_addr
,
285 (reuseport
& t
->inp_socket
->so_options
)
287 (ntohl(t
->inp_laddr
.s_addr
)
290 INP_SOCKAF(t
->inp_socket
))) {
291 lck_rw_done(pcbinfo
->mtx
);
297 inp
->in6p_laddr
= sin6
->sin6_addr
;
302 if ((e
= in6_pcbsetport(&inp
->in6p_laddr
, inp
, p
, 1)) != 0) {
303 lck_rw_done(pcbinfo
->mtx
);
308 inp
->inp_lport
= lport
;
309 if (in_pcbinshash(inp
, 1) != 0) {
310 inp
->in6p_laddr
= in6addr_any
;
312 lck_rw_done(pcbinfo
->mtx
);
316 lck_rw_done(pcbinfo
->mtx
);
321 * Transform old in6_pcbconnect() into an inner subroutine for new
322 * in6_pcbconnect(): Do some validity-checking on the remote
323 * address (in mbuf 'nam') and then determine local host address
324 * (i.e., which interface) to use to access that remote host.
326 * This preserves definition of in6_pcbconnect(), while supporting a
327 * slightly different version for T/TCP. (This is more than
328 * a bit of a kludge, but cleaning up the internal interfaces would
329 * have forced minor changes in every protocol).
335 struct sockaddr
*nam
,
336 struct in6_addr
*plocal_addr6
)
338 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)nam
;
339 struct in6_addr
*addr6
= NULL
;
340 struct in6_addr src_storage
;
342 struct ifnet
*ifp
= NULL
;
345 if (nam
->sa_len
!= sizeof (*sin6
))
347 if (sin6
->sin6_family
!= AF_INET6
)
348 return (EAFNOSUPPORT
);
349 if (sin6
->sin6_port
== 0)
350 return (EADDRNOTAVAIL
);
352 /* KAME hack: embed scopeid */
353 if (in6_embedscope(&sin6
->sin6_addr
, sin6
, inp
, &ifp
) != 0)
358 * If the destination address is UNSPECIFIED addr,
359 * use the loopback addr, e.g ::1.
361 if (IN6_IS_ADDR_UNSPECIFIED(&sin6
->sin6_addr
))
362 sin6
->sin6_addr
= in6addr_loopback
;
366 * XXX: in6_selectsrc might replace the bound local address
367 * with the address specified by setsockopt(IPV6_PKTINFO).
368 * Is it the intended behavior?
370 addr6
= in6_selectsrc(sin6
, inp
->in6p_outputopts
,
373 &inp
->in6p_laddr
, &src_storage
, &error
);
376 error
= EADDRNOTAVAIL
;
379 *plocal_addr6
= *addr6
;
381 * Don't do pcblookup call here; return interface in
383 * and exit to caller, that will do the lookup.
387 if (inp
->in6p_route
.ro_rt
)
388 ifp
= inp
->in6p_route
.ro_rt
->rt_ifp
;
395 * Connect from a socket to a specified address.
396 * Both address and port must be specified in argument sin.
397 * If don't have a local address for this socket yet,
401 in6_pcbconnect(inp
, nam
, p
)
403 struct sockaddr
*nam
;
406 struct in6_addr addr6
;
407 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)nam
;
412 * Call inner routine, to assign local interface address.
413 * in6_pcbladdr() may automatically fill in sin6_scope_id.
415 if ((error
= in6_pcbladdr(inp
, nam
, &addr6
)) != 0)
417 socket_unlock(inp
->inp_socket
, 0);
418 pcb
= in6_pcblookup_hash(inp
->inp_pcbinfo
, &sin6
->sin6_addr
,
420 IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
)
421 ? &addr6
: &inp
->in6p_laddr
,
422 inp
->inp_lport
, 0, NULL
);
423 socket_lock(inp
->inp_socket
, 0);
425 in_pcb_checkstate(pcb
, WNT_RELEASE
, 0);
428 if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
)) {
429 if (inp
->inp_lport
== 0) {
430 error
= in6_pcbbind(inp
, (struct sockaddr
*)0, p
);
434 inp
->in6p_laddr
= addr6
;
436 if (!lck_rw_try_lock_exclusive(inp
->inp_pcbinfo
->mtx
)) {
437 /*lock inversion issue, mostly with udp multicast packets */
438 socket_unlock(inp
->inp_socket
, 0);
439 lck_rw_lock_exclusive(inp
->inp_pcbinfo
->mtx
);
440 socket_lock(inp
->inp_socket
, 0);
442 inp
->in6p_faddr
= sin6
->sin6_addr
;
443 inp
->inp_fport
= sin6
->sin6_port
;
444 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
445 inp
->in6p_flowinfo
&= ~IPV6_FLOWLABEL_MASK
;
446 if (inp
->in6p_flags
& IN6P_AUTOFLOWLABEL
)
447 inp
->in6p_flowinfo
|=
448 (htonl(ip6_flow_seq
++) & IPV6_FLOWLABEL_MASK
);
451 lck_rw_done(inp
->inp_pcbinfo
->mtx
);
457 * Return an IPv6 address, which is the most appropriate for given
458 * destination and user specified options.
459 * If necessary, this function lookups the routing table and return
460 * an entry to the caller for later use.
464 struct sockaddr_in6
*dstsock
,
465 struct ip6_pktopts
*opts
,
466 struct ip6_moptions
*mopts
,
467 struct route_in6
*ro
,
468 struct in6_addr
*laddr
,
469 struct in6_addr
*src_storage
,
472 struct in6_addr
*dst
;
473 struct in6_ifaddr
*ia6
= 0;
474 struct in6_pktinfo
*pi
= NULL
;
476 dst
= &dstsock
->sin6_addr
;
480 * If the source address is explicitly specified by the caller,
483 if (opts
&& (pi
= opts
->ip6po_pktinfo
) &&
484 !IN6_IS_ADDR_UNSPECIFIED(&pi
->ipi6_addr
))
485 return(&pi
->ipi6_addr
);
488 * If the source address is not specified but the socket(if any)
489 * is already bound, use the bound address.
491 if (laddr
&& !IN6_IS_ADDR_UNSPECIFIED(laddr
))
495 * If the caller doesn't specify the source address but
496 * the outgoing interface, use an address associated with
499 if (pi
&& pi
->ipi6_ifindex
) {
500 /* XXX boundary check is assumed to be already done. */
501 ia6
= in6_ifawithscope(ifindex2ifnet
[pi
->ipi6_ifindex
],
504 *errorp
= EADDRNOTAVAIL
;
507 *src_storage
= satosin6(&ia6
->ia_addr
)->sin6_addr
;
508 ifafree(&ia6
->ia_ifa
);
513 * If the destination address is a link-local unicast address or
514 * a multicast address, and if the outgoing interface is specified
515 * by the sin6_scope_id filed, use an address associated with the
517 * XXX: We're now trying to define more specific semantics of
518 * sin6_scope_id field, so this part will be rewritten in
521 if ((IN6_IS_ADDR_LINKLOCAL(dst
) || IN6_IS_ADDR_MULTICAST(dst
)) &&
522 dstsock
->sin6_scope_id
) {
524 * I'm not sure if boundary check for scope_id is done
527 if (dstsock
->sin6_scope_id
< 0 ||
528 if_index
< dstsock
->sin6_scope_id
) {
529 *errorp
= ENXIO
; /* XXX: better error? */
532 ia6
= in6_ifawithscope(ifindex2ifnet
[dstsock
->sin6_scope_id
],
535 *errorp
= EADDRNOTAVAIL
;
538 *src_storage
= satosin6(&ia6
->ia_addr
)->sin6_addr
;
539 ifafree(&ia6
->ia_ifa
);
544 * If the destination address is a multicast address and
545 * the outgoing interface for the address is specified
546 * by the caller, use an address associated with the interface.
547 * There is a sanity check here; if the destination has node-local
548 * scope, the outgoing interfacde should be a loopback address.
549 * Even if the outgoing interface is not specified, we also
550 * choose a loopback interface as the outgoing interface.
552 if (IN6_IS_ADDR_MULTICAST(dst
)) {
553 struct ifnet
*ifp
= mopts
? mopts
->im6o_multicast_ifp
: NULL
;
555 if (ifp
== NULL
&& IN6_IS_ADDR_MC_NODELOCAL(dst
)) {
560 ia6
= in6_ifawithscope(ifp
, dst
);
562 *errorp
= EADDRNOTAVAIL
;
565 *src_storage
= ia6
->ia_addr
.sin6_addr
;
566 ifafree(&ia6
->ia_ifa
);
572 * If the next hop address for the packet is specified
573 * by caller, use an address associated with the route
577 struct sockaddr_in6
*sin6_next
;
580 if (opts
&& opts
->ip6po_nexthop
) {
581 sin6_next
= satosin6(opts
->ip6po_nexthop
);
582 rt
= nd6_lookup(&sin6_next
->sin6_addr
, 1, NULL
, 0);
584 ia6
= in6_ifawithscope(rt
->rt_ifp
, dst
);
587 ia6
= ifatoia6(rt
->rt_ifa
);
591 *errorp
= EADDRNOTAVAIL
;
594 *src_storage
= satosin6(&ia6
->ia_addr
)->sin6_addr
;
601 * If route is known or can be allocated now,
602 * our src addr is taken from the i/f, else punt.
606 !IN6_ARE_ADDR_EQUAL(&satosin6(&ro
->ro_dst
)->sin6_addr
, dst
)) {
608 ro
->ro_rt
= (struct rtentry
*)0;
610 if (ro
->ro_rt
== (struct rtentry
*)0 ||
611 ro
->ro_rt
->rt_ifp
== (struct ifnet
*)0) {
612 struct sockaddr_in6
*dst6
;
614 /* No route yet, so try to acquire one */
615 bzero(&ro
->ro_dst
, sizeof(struct sockaddr_in6
));
616 dst6
= (struct sockaddr_in6
*)&ro
->ro_dst
;
617 dst6
->sin6_family
= AF_INET6
;
618 dst6
->sin6_len
= sizeof(struct sockaddr_in6
);
619 dst6
->sin6_addr
= *dst
;
620 if (IN6_IS_ADDR_MULTICAST(dst
)) {
621 ro
->ro_rt
= rtalloc1(&((struct route
*)ro
)
624 rtalloc((struct route
*)ro
);
629 * in_pcbconnect() checks out IFF_LOOPBACK to skip using
630 * the address. But we don't know why it does so.
631 * It is necessary to ensure the scope even for lo0
632 * so doesn't check out IFF_LOOPBACK.
636 ia6
= in6_ifawithscope(ro
->ro_rt
->rt_ifa
->ifa_ifp
, dst
);
637 if (ia6
== 0) { /* xxx scope error ?*/
638 ifaref(ro
->ro_rt
->rt_ifa
);
639 ia6
= ifatoia6(ro
->ro_rt
->rt_ifa
);
643 *errorp
= EHOSTUNREACH
; /* no route */
646 *src_storage
= satosin6(&ia6
->ia_addr
)->sin6_addr
;
651 *errorp
= EADDRNOTAVAIL
;
656 * Default hop limit selection. The precedence is as follows:
657 * 1. Hoplimit valued specified via ioctl.
658 * 2. (If the outgoing interface is detected) the current
659 * hop limit of the interface specified by router advertisement.
660 * 3. The system default hoplimit.
667 if (in6p
&& in6p
->in6p_hops
>= 0)
668 return(in6p
->in6p_hops
);
670 return(nd_ifinfo
[ifp
->if_index
].chlim
);
677 in6_pcbdisconnect(inp
)
680 if (!lck_rw_try_lock_exclusive(inp
->inp_pcbinfo
->mtx
)) {
681 /*lock inversion issue, mostly with udp multicast packets */
682 socket_unlock(inp
->inp_socket
, 0);
683 lck_rw_lock_exclusive(inp
->inp_pcbinfo
->mtx
);
684 socket_lock(inp
->inp_socket
, 0);
686 bzero((caddr_t
)&inp
->in6p_faddr
, sizeof(inp
->in6p_faddr
));
688 /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
689 inp
->in6p_flowinfo
&= ~IPV6_FLOWLABEL_MASK
;
691 lck_rw_done(inp
->inp_pcbinfo
->mtx
);
692 if (inp
->inp_socket
->so_state
& SS_NOFDREF
)
700 struct socket
*so
= inp
->inp_socket
;
701 struct inpcbinfo
*ipi
= inp
->inp_pcbinfo
;
704 if (inp
->in6p_sp
!= NULL
) {
705 lck_mtx_lock(sadb_mutex
);
706 ipsec6_delete_pcbpolicy(inp
);
707 lck_mtx_unlock(sadb_mutex
);
711 if (in_pcb_checkstate(inp
, WNT_STOPUSING
, 1) != WNT_STOPUSING
)
712 printf("in6_pcbdetach so=%x can't be marked dead ok\n", so
);
714 inp
->inp_state
= INPCB_STATE_DEAD
;
716 if ((so
->so_flags
& SOF_PCBCLEARING
) == 0) {
718 so
->so_flags
|= SOF_PCBCLEARING
;
719 inp
->inp_gencnt
= ++ipi
->ipi_gencnt
;
720 if (inp
->in6p_options
)
721 m_freem(inp
->in6p_options
);
722 ip6_freepcbopts(inp
->in6p_outputopts
);
723 ip6_freemoptions(inp
->in6p_moptions
);
724 if (inp
->in6p_route
.ro_rt
)
725 rtfree(inp
->in6p_route
.ro_rt
);
726 /* Check and free IPv4 related resources in case of mapped addr */
727 if (inp
->inp_options
)
728 (void)m_free(inp
->inp_options
);
729 ip_freemoptions(inp
->inp_moptions
);
730 inp
->inp_moptions
= NULL
;
736 in6_sockaddr(port
, addr_p
)
738 struct in6_addr
*addr_p
;
740 struct sockaddr_in6
*sin6
;
742 MALLOC(sin6
, struct sockaddr_in6
*, sizeof *sin6
, M_SONAME
, M_WAITOK
);
743 bzero(sin6
, sizeof *sin6
);
744 sin6
->sin6_family
= AF_INET6
;
745 sin6
->sin6_len
= sizeof(*sin6
);
746 sin6
->sin6_port
= port
;
747 sin6
->sin6_addr
= *addr_p
;
748 if (IN6_IS_SCOPE_LINKLOCAL(&sin6
->sin6_addr
))
749 sin6
->sin6_scope_id
= ntohs(sin6
->sin6_addr
.s6_addr16
[1]);
751 sin6
->sin6_scope_id
= 0; /*XXX*/
752 if (IN6_IS_SCOPE_LINKLOCAL(&sin6
->sin6_addr
))
753 sin6
->sin6_addr
.s6_addr16
[1] = 0;
755 return (struct sockaddr
*)sin6
;
759 in6_v4mapsin6_sockaddr(port
, addr_p
)
761 struct in_addr
*addr_p
;
763 struct sockaddr_in sin
;
764 struct sockaddr_in6
*sin6_p
;
766 bzero(&sin
, sizeof sin
);
767 sin
.sin_family
= AF_INET
;
768 sin
.sin_len
= sizeof(sin
);
770 sin
.sin_addr
= *addr_p
;
772 MALLOC(sin6_p
, struct sockaddr_in6
*, sizeof *sin6_p
, M_SONAME
,
774 in6_sin_2_v4mapsin6(&sin
, sin6_p
);
776 return (struct sockaddr
*)sin6_p
;
780 * The calling convention of in6_setsockaddr() and in6_setpeeraddr() was
781 * modified to match the pru_sockaddr() and pru_peeraddr() entry points
782 * in struct pr_usrreqs, so that protocols can just reference then directly
783 * without the need for a wrapper function. The socket must have a valid
784 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
785 * except through a kernel programming error, so it is acceptable to panic
786 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap
787 * because there actually /is/ a programming error somewhere... XXX)
790 in6_setsockaddr(so
, nam
)
792 struct sockaddr
**nam
;
795 struct in6_addr addr
;
802 port
= inp
->inp_lport
;
803 addr
= inp
->in6p_laddr
;
805 *nam
= in6_sockaddr(port
, &addr
);
810 in6_setpeeraddr(so
, nam
)
812 struct sockaddr
**nam
;
815 struct in6_addr addr
;
822 port
= inp
->inp_fport
;
823 addr
= inp
->in6p_faddr
;
825 *nam
= in6_sockaddr(port
, &addr
);
830 in6_mapped_sockaddr(struct socket
*so
, struct sockaddr
**nam
)
832 struct inpcb
*inp
= sotoinpcb(so
);
837 if (inp
->inp_vflag
& INP_IPV4
) {
838 error
= in_setsockaddr(so
, nam
);
840 in6_sin_2_v4mapsin6_in_sock(nam
);
842 /* scope issues will be handled in in6_setsockaddr(). */
843 error
= in6_setsockaddr(so
, nam
);
849 in6_mapped_peeraddr(struct socket
*so
, struct sockaddr
**nam
)
851 struct inpcb
*inp
= sotoinpcb(so
);
856 if (inp
->inp_vflag
& INP_IPV4
) {
857 error
= in_setpeeraddr(so
, nam
);
859 in6_sin_2_v4mapsin6_in_sock(nam
);
861 /* scope issues will be handled in in6_setpeeraddr(). */
862 error
= in6_setpeeraddr(so
, nam
);
868 * Pass some notification to all connections of a protocol
869 * associated with address dst. The local address and/or port numbers
870 * may be specified to limit the search. The "usual action" will be
871 * taken, depending on the ctlinput cmd. The caller must filter any
872 * cmds that are uninteresting (e.g., no error in the map).
873 * Call the protocol specific routine (if any) to report
874 * any errors for each matching socket.
876 * Must be called at splnet.
879 in6_pcbnotify(pcbinfo
, dst
, fport_arg
, src
, lport_arg
, cmd
, notify
)
880 struct inpcbinfo
*pcbinfo
;
881 struct sockaddr
*dst
;
882 const struct sockaddr
*src
;
883 u_int fport_arg
, lport_arg
;
885 // struct inpcb *(*notify)(struct inpcb *, int);
886 void (*notify
)(struct inpcb
*, int);
888 struct inpcb
*inp
, *ninp
;
889 struct sockaddr_in6 sa6_src
, *sa6_dst
;
890 u_short fport
= fport_arg
, lport
= lport_arg
;
893 struct inpcbhead
*head
= pcbinfo
->listhead
;
895 if ((unsigned)cmd
> PRC_NCMDS
|| dst
->sa_family
!= AF_INET6
)
898 sa6_dst
= (struct sockaddr_in6
*)dst
;
899 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst
->sin6_addr
))
903 * note that src can be NULL when we get notify by local fragmentation.
905 sa6_src
= (src
== NULL
) ? sa6_any
: *(const struct sockaddr_in6
*)src
;
906 flowinfo
= sa6_src
.sin6_flowinfo
;
909 * Redirects go to all references to the destination,
910 * and use in6_rtchange to invalidate the route cache.
911 * Dead host indications: also use in6_rtchange to invalidate
912 * the cache, and deliver the error to all the sockets.
913 * Otherwise, if we have knowledge of the local port and address,
914 * deliver only to that socket.
916 if (PRC_IS_REDIRECT(cmd
) || cmd
== PRC_HOSTDEAD
) {
919 bzero((caddr_t
)&sa6_src
.sin6_addr
, sizeof(sa6_src
.sin6_addr
));
921 if (cmd
!= PRC_HOSTDEAD
)
922 notify
= in6_rtchange
;
924 errno
= inet6ctlerrmap
[cmd
];
925 lck_rw_lock_shared(pcbinfo
->mtx
);
926 for (inp
= LIST_FIRST(head
); inp
!= NULL
; inp
= ninp
) {
927 ninp
= LIST_NEXT(inp
, inp_list
);
929 if ((inp
->inp_vflag
& INP_IPV6
) == 0)
933 * Detect if we should notify the error. If no source and
934 * destination ports are specifed, but non-zero flowinfo and
935 * local address match, notify the error. This is the case
936 * when the error is delivered with an encrypted buffer
937 * by ESP. Otherwise, just compare addresses and ports
940 if (lport
== 0 && fport
== 0 && flowinfo
&&
941 inp
->inp_socket
!= NULL
&&
942 flowinfo
== (inp
->in6p_flowinfo
& IPV6_FLOWLABEL_MASK
) &&
943 IN6_ARE_ADDR_EQUAL(&inp
->in6p_laddr
, &sa6_src
.sin6_addr
))
945 else if (!IN6_ARE_ADDR_EQUAL(&inp
->in6p_faddr
,
946 &sa6_dst
->sin6_addr
) ||
947 inp
->inp_socket
== 0 ||
948 (lport
&& inp
->inp_lport
!= lport
) ||
949 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src
.sin6_addr
) &&
950 !IN6_ARE_ADDR_EQUAL(&inp
->in6p_laddr
,
951 &sa6_src
.sin6_addr
)) ||
952 (fport
&& inp
->inp_fport
!= fport
))
958 if (in_pcb_checkstate(inp
, WNT_ACQUIRE
, 0) == WNT_STOPUSING
)
960 socket_lock(inp
->inp_socket
, 1);
961 (*notify
)(inp
, errno
);
962 (void)in_pcb_checkstate(inp
, WNT_RELEASE
, 1);
963 socket_unlock(inp
->inp_socket
, 1);
966 lck_rw_done(pcbinfo
->mtx
);
970 * Lookup a PCB based on the local address and port.
973 in6_pcblookup_local(pcbinfo
, laddr
, lport_arg
, wild_okay
)
974 struct inpcbinfo
*pcbinfo
;
975 struct in6_addr
*laddr
;
980 int matchwild
= 3, wildcard
;
981 u_short lport
= lport_arg
;
984 struct inpcbhead
*head
;
986 * Look for an unconnected (wildcard foreign addr) PCB that
987 * matches the local address and port we're looking for.
989 head
= &pcbinfo
->hashbase
[INP_PCBHASH(INADDR_ANY
, lport
, 0,
991 LIST_FOREACH(inp
, head
, inp_hash
) {
992 if ((inp
->inp_vflag
& INP_IPV6
) == 0)
994 if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_faddr
) &&
995 IN6_ARE_ADDR_EQUAL(&inp
->in6p_laddr
, laddr
) &&
996 inp
->inp_lport
== lport
) {
1008 struct inpcbporthead
*porthash
;
1009 struct inpcbport
*phd
;
1010 struct inpcb
*match
= NULL
;
1012 * Best fit PCB lookup.
1014 * First see if this local port is in use by looking on the
1017 porthash
= &pcbinfo
->porthashbase
[INP_PCBPORTHASH(lport
,
1018 pcbinfo
->porthashmask
)];
1019 LIST_FOREACH(phd
, porthash
, phd_hash
) {
1020 if (phd
->phd_port
== lport
)
1025 * Port is in use by one or more PCBs. Look for best
1028 LIST_FOREACH(inp
, &phd
->phd_pcblist
, inp_portlist
) {
1030 if ((inp
->inp_vflag
& INP_IPV6
) == 0)
1032 if (!IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_faddr
))
1034 if (!IN6_IS_ADDR_UNSPECIFIED(
1035 &inp
->in6p_laddr
)) {
1036 if (IN6_IS_ADDR_UNSPECIFIED(laddr
))
1038 else if (!IN6_ARE_ADDR_EQUAL(
1039 &inp
->in6p_laddr
, laddr
))
1042 if (!IN6_IS_ADDR_UNSPECIFIED(laddr
))
1045 if (wildcard
< matchwild
) {
1047 matchwild
= wildcard
;
1048 if (matchwild
== 0) {
1058 /* this is not used in Darwin */
1061 struct in6pcb
*head
,
1064 struct in6pcb
*in6p
;
1065 struct ip6_moptions
*im6o
;
1066 struct in6_multi_mship
*imm
, *nimm
;
1068 for (in6p
= head
; in6p
!= NULL
; in6p
= LIST_NEXT(in6p
, inp_list
)) {
1069 im6o
= in6p
->in6p_moptions
;
1070 if ((in6p
->inp_vflag
& INP_IPV6
) &&
1073 * Unselect the outgoing interface if it is being
1076 if (im6o
->im6o_multicast_ifp
== ifp
)
1077 im6o
->im6o_multicast_ifp
= NULL
;
1080 * Drop multicast group membership if we joined
1081 * through the interface being detached.
1082 * XXX controversial - is it really legal for kernel
1085 for (imm
= im6o
->im6o_memberships
.lh_first
;
1086 imm
!= NULL
; imm
= nimm
) {
1087 nimm
= imm
->i6mm_chain
.le_next
;
1088 if (imm
->i6mm_maddr
->in6m_ifp
== ifp
) {
1089 LIST_REMOVE(imm
, i6mm_chain
);
1090 in6_delmulti(imm
->i6mm_maddr
);
1091 FREE(imm
, M_IPMADDR
);
1100 * Check for alternatives when higher level complains
1101 * about service problems. For now, invalidate cached
1102 * routing information. If the route was created dynamically
1103 * (by a redirect), time to try a default gateway again.
1110 struct rt_addrinfo info
;
1112 if ((rt
= in6p
->in6p_route
.ro_rt
) != NULL
) {
1113 in6p
->in6p_route
.ro_rt
= 0;
1114 bzero((caddr_t
)&info
, sizeof(info
));
1115 info
.rti_info
[RTAX_DST
] =
1116 (struct sockaddr
*)&in6p
->in6p_route
.ro_dst
;
1117 info
.rti_info
[RTAX_GATEWAY
] = rt
->rt_gateway
;
1118 info
.rti_info
[RTAX_NETMASK
] = rt_mask(rt
);
1119 lck_mtx_lock(rt_mtx
);
1120 rt_missmsg(RTM_LOSING
, &info
, rt
->rt_flags
, 0);
1121 if (rt
->rt_flags
& RTF_DYNAMIC
)
1122 (void)rtrequest_locked(RTM_DELETE
, rt_key(rt
),
1123 rt
->rt_gateway
, rt_mask(rt
), rt
->rt_flags
,
1124 (struct rtentry
**)0);
1127 * A new route can be allocated
1128 * the next time output is attempted.
1131 lck_mtx_unlock(rt_mtx
);
1136 * After a routing change, flush old routing
1137 * and allocate a (hopefully) better one.
1144 if (inp
->in6p_route
.ro_rt
) {
1145 rtfree(inp
->in6p_route
.ro_rt
);
1146 inp
->in6p_route
.ro_rt
= 0;
1148 * A new route can be allocated the next time
1149 * output is attempted.
1155 * Lookup PCB in hash list.
1159 struct inpcbinfo
*pcbinfo
,
1160 struct in6_addr
*faddr
,
1162 struct in6_addr
*laddr
,
1167 struct inpcbhead
*head
;
1169 u_short fport
= fport_arg
, lport
= lport_arg
;
1172 #if defined(NFAITH) && NFAITH > 0
1173 faith
= faithprefix(laddr
);
1178 lck_rw_lock_shared(pcbinfo
->mtx
);
1181 * First look for an exact match.
1183 head
= &pcbinfo
->hashbase
[INP_PCBHASH(faddr
->s6_addr32
[3] /* XXX */,
1185 pcbinfo
->hashmask
)];
1186 LIST_FOREACH(inp
, head
, inp_hash
) {
1187 if ((inp
->inp_vflag
& INP_IPV6
) == 0)
1189 if (IN6_ARE_ADDR_EQUAL(&inp
->in6p_faddr
, faddr
) &&
1190 IN6_ARE_ADDR_EQUAL(&inp
->in6p_laddr
, laddr
) &&
1191 inp
->inp_fport
== fport
&&
1192 inp
->inp_lport
== lport
) {
1194 * Found. Check if pcb is still valid
1196 if (in_pcb_checkstate(inp
, WNT_ACQUIRE
, 0) != WNT_STOPUSING
) {
1197 lck_rw_done(pcbinfo
->mtx
);
1200 else { /* it's there but dead, say it isn't found */
1201 lck_rw_done(pcbinfo
->mtx
);
1207 struct inpcb
*local_wild
= NULL
;
1209 head
= &pcbinfo
->hashbase
[INP_PCBHASH(INADDR_ANY
, lport
, 0,
1210 pcbinfo
->hashmask
)];
1211 LIST_FOREACH(inp
, head
, inp_hash
) {
1212 if ((inp
->inp_vflag
& INP_IPV6
) == 0)
1214 if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_faddr
) &&
1215 inp
->inp_lport
== lport
) {
1216 if (faith
&& (inp
->inp_flags
& INP_FAITH
) == 0)
1218 if (IN6_ARE_ADDR_EQUAL(&inp
->in6p_laddr
,
1220 if (in_pcb_checkstate(inp
, WNT_ACQUIRE
, 0) != WNT_STOPUSING
) {
1221 lck_rw_done(pcbinfo
->mtx
);
1224 else { /* it's there but dead, say it isn't found */
1225 lck_rw_done(pcbinfo
->mtx
);
1229 else if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
))
1233 if (local_wild
&& in_pcb_checkstate(local_wild
, WNT_ACQUIRE
, 0) != WNT_STOPUSING
) {
1234 lck_rw_done(pcbinfo
->mtx
);
1235 return (local_wild
);
1238 lck_rw_done(pcbinfo
->mtx
);
1246 lck_rw_done(pcbinfo
->mtx
);
1251 init_sin6(struct sockaddr_in6
*sin6
, struct mbuf
*m
)
1255 ip
= mtod(m
, struct ip6_hdr
*);
1256 bzero(sin6
, sizeof(*sin6
));
1257 sin6
->sin6_len
= sizeof(*sin6
);
1258 sin6
->sin6_family
= AF_INET6
;
1259 sin6
->sin6_addr
= ip
->ip6_src
;
1260 if (IN6_IS_SCOPE_LINKLOCAL(&sin6
->sin6_addr
))
1261 sin6
->sin6_addr
.s6_addr16
[1] = 0;
1262 sin6
->sin6_scope_id
=
1263 (m
->m_pkthdr
.rcvif
&& IN6_IS_SCOPE_LINKLOCAL(&sin6
->sin6_addr
))
1264 ? m
->m_pkthdr
.rcvif
->if_index
: 0;