]>
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_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
32 * All rights reserved.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. Neither the name of the project nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
46 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * Copyright (c) 1982, 1986, 1991, 1993
62 * The Regents of the University of California. All rights reserved.
64 * Redistribution and use in source and binary forms, with or without
65 * modification, are permitted provided that the following conditions
67 * 1. Redistributions of source code must retain the above copyright
68 * notice, this list of conditions and the following disclaimer.
69 * 2. Redistributions in binary form must reproduce the above copyright
70 * notice, this list of conditions and the following disclaimer in the
71 * documentation and/or other materials provided with the distribution.
72 * 3. All advertising materials mentioning features or use of this software
73 * must display the following acknowledgement:
74 * This product includes software developed by the University of
75 * California, Berkeley and its contributors.
76 * 4. Neither the name of the University nor the names of its contributors
77 * may be used to endorse or promote products derived from this software
78 * without specific prior written permission.
80 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
81 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
82 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
83 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
84 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
85 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
86 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
87 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
88 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
89 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
92 * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94
95 #include <sys/param.h>
96 #include <sys/systm.h>
97 #include <sys/malloc.h>
99 #include <sys/domain.h>
100 #include <sys/protosw.h>
101 #include <sys/socket.h>
102 #include <sys/socketvar.h>
103 #include <sys/sockio.h>
104 #include <sys/errno.h>
105 #include <sys/time.h>
106 #include <sys/proc.h>
109 #include <net/if_types.h>
110 #include <net/route.h>
112 #include <netinet/in.h>
113 #include <netinet/in_var.h>
114 #include <netinet/in_systm.h>
115 #include <netinet/ip6.h>
116 #include <netinet/ip_var.h>
117 #include <netinet6/ip6_var.h>
118 #include <netinet6/nd6.h>
119 #include <netinet/in_pcb.h>
120 #include <netinet6/in6_pcb.h>
121 #include <net/if_types.h>
123 #include <kern/kern_types.h>
124 #include <kern/zalloc.h>
127 #if defined(NFAITH) && NFAITH > 0
128 #include <net/if_faith.h>
132 #include <netinet6/ipsec.h>
134 #include <netinet6/ipsec6.h>
136 #include <netinet6/ah.h>
138 #include <netinet6/ah6.h>
140 #include <netkey/key.h>
141 extern lck_mtx_t
*sadb_mutex
;
144 struct in6_addr zeroin6_addr
;
149 struct sockaddr
*nam
,
152 struct socket
*so
= inp
->inp_socket
;
153 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)NULL
;
154 struct inpcbinfo
*pcbinfo
= inp
->inp_pcbinfo
;
156 int wild
= 0, reuseport
= (so
->so_options
& SO_REUSEPORT
);
158 if (!in6_ifaddrs
) /* XXX broken! */
159 return (EADDRNOTAVAIL
);
160 if (inp
->inp_lport
|| !IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
))
162 if ((so
->so_options
& (SO_REUSEADDR
|SO_REUSEPORT
)) == 0)
164 socket_unlock(so
, 0); /* keep reference */
165 lck_rw_lock_exclusive(pcbinfo
->mtx
);
167 sin6
= (struct sockaddr_in6
*)nam
;
168 if (nam
->sa_len
!= sizeof(*sin6
)) {
169 lck_rw_done(pcbinfo
->mtx
);
176 if (nam
->sa_family
!= AF_INET6
) {
177 lck_rw_done(pcbinfo
->mtx
);
179 return(EAFNOSUPPORT
);
182 /* KAME hack: embed scopeid */
183 if (in6_embedscope(&sin6
->sin6_addr
, sin6
, inp
, NULL
) != 0) {
184 lck_rw_done(pcbinfo
->mtx
);
188 /* this must be cleared for ifa_ifwithaddr() */
189 sin6
->sin6_scope_id
= 0;
191 lport
= sin6
->sin6_port
;
192 if (IN6_IS_ADDR_MULTICAST(&sin6
->sin6_addr
)) {
194 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
195 * allow compepte duplication of binding if
196 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
197 * and a multicast address is bound on both
198 * new and duplicated sockets.
200 if (so
->so_options
& SO_REUSEADDR
)
201 reuseport
= SO_REUSEADDR
|SO_REUSEPORT
;
202 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6
->sin6_addr
)) {
203 struct ifaddr
*ia
= NULL
;
205 sin6
->sin6_port
= 0; /* yech... */
206 if ((ia
= ifa_ifwithaddr((struct sockaddr
*)sin6
)) == 0) {
207 lck_rw_done(pcbinfo
->mtx
);
209 return(EADDRNOTAVAIL
);
213 * XXX: bind to an anycast address might accidentally
214 * cause sending a packet with anycast source address.
215 * We should allow to bind to a deprecated address, since
216 * the application dare to use it.
219 ((struct in6_ifaddr
*)ia
)->ia6_flags
&
220 (IN6_IFF_ANYCAST
|IN6_IFF_NOTREADY
|IN6_IFF_DETACHED
)) {
222 lck_rw_done(pcbinfo
->mtx
);
224 return(EADDRNOTAVAIL
);
233 if (ntohs(lport
) < IPV6PORT_RESERVED
&& p
&&
234 ((so
->so_state
& SS_PRIV
) == 0)) {
235 lck_rw_done(pcbinfo
->mtx
);
241 !IN6_IS_ADDR_MULTICAST(&sin6
->sin6_addr
)) {
242 t
= in6_pcblookup_local(pcbinfo
,
243 &sin6
->sin6_addr
, lport
,
246 (!IN6_IS_ADDR_UNSPECIFIED(&sin6
->sin6_addr
) ||
247 !IN6_IS_ADDR_UNSPECIFIED(&t
->in6p_laddr
) ||
248 (t
->inp_socket
->so_options
&
249 SO_REUSEPORT
) == 0) &&
250 so
->so_uid
!= t
->inp_socket
->so_uid
) {
251 lck_rw_done(pcbinfo
->mtx
);
255 if ((inp
->inp_flags
& IN6P_IPV6_V6ONLY
) == 0 &&
256 IN6_IS_ADDR_UNSPECIFIED(&sin6
->sin6_addr
)) {
257 struct sockaddr_in sin
;
259 in6_sin6_2_sin(&sin
, sin6
);
260 t
= in_pcblookup_local(pcbinfo
,
265 t
->inp_socket
->so_uid
) &&
266 (ntohl(t
->inp_laddr
.s_addr
) !=
269 INP_SOCKAF(t
->inp_socket
))) {
271 lck_rw_done(pcbinfo
->mtx
);
277 t
= in6_pcblookup_local(pcbinfo
, &sin6
->sin6_addr
,
279 if (t
&& (reuseport
& t
->inp_socket
->so_options
) == 0) {
280 lck_rw_done(pcbinfo
->mtx
);
284 if ((inp
->inp_flags
& IN6P_IPV6_V6ONLY
) == 0 &&
285 IN6_IS_ADDR_UNSPECIFIED(&sin6
->sin6_addr
)) {
286 struct sockaddr_in sin
;
288 in6_sin6_2_sin(&sin
, sin6
);
289 t
= in_pcblookup_local(pcbinfo
, sin
.sin_addr
,
292 (reuseport
& t
->inp_socket
->so_options
)
294 (ntohl(t
->inp_laddr
.s_addr
)
297 INP_SOCKAF(t
->inp_socket
))) {
298 lck_rw_done(pcbinfo
->mtx
);
304 inp
->in6p_laddr
= sin6
->sin6_addr
;
309 if ((e
= in6_pcbsetport(&inp
->in6p_laddr
, inp
, p
, 1)) != 0) {
310 lck_rw_done(pcbinfo
->mtx
);
315 inp
->inp_lport
= lport
;
316 if (in_pcbinshash(inp
, 1) != 0) {
317 inp
->in6p_laddr
= in6addr_any
;
319 lck_rw_done(pcbinfo
->mtx
);
323 lck_rw_done(pcbinfo
->mtx
);
328 * Transform old in6_pcbconnect() into an inner subroutine for new
329 * in6_pcbconnect(): Do some validity-checking on the remote
330 * address (in mbuf 'nam') and then determine local host address
331 * (i.e., which interface) to use to access that remote host.
333 * This preserves definition of in6_pcbconnect(), while supporting a
334 * slightly different version for T/TCP. (This is more than
335 * a bit of a kludge, but cleaning up the internal interfaces would
336 * have forced minor changes in every protocol).
342 struct sockaddr
*nam
,
343 struct in6_addr
*plocal_addr6
)
345 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)nam
;
346 struct in6_addr
*addr6
= NULL
;
347 struct in6_addr src_storage
;
349 struct ifnet
*ifp
= NULL
;
352 if (nam
->sa_len
!= sizeof (*sin6
))
354 if (sin6
->sin6_family
!= AF_INET6
)
355 return (EAFNOSUPPORT
);
356 if (sin6
->sin6_port
== 0)
357 return (EADDRNOTAVAIL
);
359 /* KAME hack: embed scopeid */
360 if (in6_embedscope(&sin6
->sin6_addr
, sin6
, inp
, &ifp
) != 0)
365 * If the destination address is UNSPECIFIED addr,
366 * use the loopback addr, e.g ::1.
368 if (IN6_IS_ADDR_UNSPECIFIED(&sin6
->sin6_addr
))
369 sin6
->sin6_addr
= in6addr_loopback
;
373 * XXX: in6_selectsrc might replace the bound local address
374 * with the address specified by setsockopt(IPV6_PKTINFO).
375 * Is it the intended behavior?
377 addr6
= in6_selectsrc(sin6
, inp
->in6p_outputopts
,
380 &inp
->in6p_laddr
, &src_storage
, &error
);
383 error
= EADDRNOTAVAIL
;
386 *plocal_addr6
= *addr6
;
388 * Don't do pcblookup call here; return interface in
390 * and exit to caller, that will do the lookup.
394 if (inp
->in6p_route
.ro_rt
)
395 ifp
= inp
->in6p_route
.ro_rt
->rt_ifp
;
402 * Connect from a socket to a specified address.
403 * Both address and port must be specified in argument sin.
404 * If don't have a local address for this socket yet,
408 in6_pcbconnect(inp
, nam
, p
)
410 struct sockaddr
*nam
;
413 struct in6_addr addr6
;
414 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)nam
;
419 * Call inner routine, to assign local interface address.
420 * in6_pcbladdr() may automatically fill in sin6_scope_id.
422 if ((error
= in6_pcbladdr(inp
, nam
, &addr6
)) != 0)
424 socket_unlock(inp
->inp_socket
, 0);
425 pcb
= in6_pcblookup_hash(inp
->inp_pcbinfo
, &sin6
->sin6_addr
,
427 IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
)
428 ? &addr6
: &inp
->in6p_laddr
,
429 inp
->inp_lport
, 0, NULL
);
430 socket_lock(inp
->inp_socket
, 0);
432 in_pcb_checkstate(pcb
, WNT_RELEASE
, 0);
435 if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
)) {
436 if (inp
->inp_lport
== 0) {
437 error
= in6_pcbbind(inp
, (struct sockaddr
*)0, p
);
441 inp
->in6p_laddr
= addr6
;
443 if (!lck_rw_try_lock_exclusive(inp
->inp_pcbinfo
->mtx
)) {
444 /*lock inversion issue, mostly with udp multicast packets */
445 socket_unlock(inp
->inp_socket
, 0);
446 lck_rw_lock_exclusive(inp
->inp_pcbinfo
->mtx
);
447 socket_lock(inp
->inp_socket
, 0);
449 inp
->in6p_faddr
= sin6
->sin6_addr
;
450 inp
->inp_fport
= sin6
->sin6_port
;
451 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
452 inp
->in6p_flowinfo
&= ~IPV6_FLOWLABEL_MASK
;
453 if (inp
->in6p_flags
& IN6P_AUTOFLOWLABEL
)
454 inp
->in6p_flowinfo
|=
455 (htonl(ip6_flow_seq
++) & IPV6_FLOWLABEL_MASK
);
458 lck_rw_done(inp
->inp_pcbinfo
->mtx
);
464 * Return an IPv6 address, which is the most appropriate for given
465 * destination and user specified options.
466 * If necessary, this function lookups the routing table and return
467 * an entry to the caller for later use.
471 struct sockaddr_in6
*dstsock
,
472 struct ip6_pktopts
*opts
,
473 struct ip6_moptions
*mopts
,
474 struct route_in6
*ro
,
475 struct in6_addr
*laddr
,
476 struct in6_addr
*src_storage
,
479 struct in6_addr
*dst
;
480 struct in6_ifaddr
*ia6
= 0;
481 struct in6_pktinfo
*pi
= NULL
;
483 dst
= &dstsock
->sin6_addr
;
487 * If the source address is explicitly specified by the caller,
490 if (opts
&& (pi
= opts
->ip6po_pktinfo
) &&
491 !IN6_IS_ADDR_UNSPECIFIED(&pi
->ipi6_addr
))
492 return(&pi
->ipi6_addr
);
495 * If the source address is not specified but the socket(if any)
496 * is already bound, use the bound address.
498 if (laddr
&& !IN6_IS_ADDR_UNSPECIFIED(laddr
))
502 * If the caller doesn't specify the source address but
503 * the outgoing interface, use an address associated with
506 if (pi
&& pi
->ipi6_ifindex
) {
507 /* XXX boundary check is assumed to be already done. */
508 ia6
= in6_ifawithscope(ifindex2ifnet
[pi
->ipi6_ifindex
],
511 *errorp
= EADDRNOTAVAIL
;
514 *src_storage
= satosin6(&ia6
->ia_addr
)->sin6_addr
;
515 ifafree(&ia6
->ia_ifa
);
520 * If the destination address is a link-local unicast address or
521 * a multicast address, and if the outgoing interface is specified
522 * by the sin6_scope_id filed, use an address associated with the
524 * XXX: We're now trying to define more specific semantics of
525 * sin6_scope_id field, so this part will be rewritten in
528 if ((IN6_IS_ADDR_LINKLOCAL(dst
) || IN6_IS_ADDR_MULTICAST(dst
)) &&
529 dstsock
->sin6_scope_id
) {
531 * I'm not sure if boundary check for scope_id is done
534 if (dstsock
->sin6_scope_id
< 0 ||
535 if_index
< dstsock
->sin6_scope_id
) {
536 *errorp
= ENXIO
; /* XXX: better error? */
539 ia6
= in6_ifawithscope(ifindex2ifnet
[dstsock
->sin6_scope_id
],
542 *errorp
= EADDRNOTAVAIL
;
545 *src_storage
= satosin6(&ia6
->ia_addr
)->sin6_addr
;
546 ifafree(&ia6
->ia_ifa
);
551 * If the destination address is a multicast address and
552 * the outgoing interface for the address is specified
553 * by the caller, use an address associated with the interface.
554 * There is a sanity check here; if the destination has node-local
555 * scope, the outgoing interfacde should be a loopback address.
556 * Even if the outgoing interface is not specified, we also
557 * choose a loopback interface as the outgoing interface.
559 if (IN6_IS_ADDR_MULTICAST(dst
)) {
560 struct ifnet
*ifp
= mopts
? mopts
->im6o_multicast_ifp
: NULL
;
562 if (ifp
== NULL
&& IN6_IS_ADDR_MC_NODELOCAL(dst
)) {
567 ia6
= in6_ifawithscope(ifp
, dst
);
569 *errorp
= EADDRNOTAVAIL
;
572 *src_storage
= ia6
->ia_addr
.sin6_addr
;
573 ifafree(&ia6
->ia_ifa
);
579 * If the next hop address for the packet is specified
580 * by caller, use an address associated with the route
584 struct sockaddr_in6
*sin6_next
;
587 if (opts
&& opts
->ip6po_nexthop
) {
588 sin6_next
= satosin6(opts
->ip6po_nexthop
);
589 rt
= nd6_lookup(&sin6_next
->sin6_addr
, 1, NULL
, 0);
591 ia6
= in6_ifawithscope(rt
->rt_ifp
, dst
);
594 ia6
= ifatoia6(rt
->rt_ifa
);
598 *errorp
= EADDRNOTAVAIL
;
601 *src_storage
= satosin6(&ia6
->ia_addr
)->sin6_addr
;
608 * If route is known or can be allocated now,
609 * our src addr is taken from the i/f, else punt.
613 !IN6_ARE_ADDR_EQUAL(&satosin6(&ro
->ro_dst
)->sin6_addr
, dst
)) {
615 ro
->ro_rt
= (struct rtentry
*)0;
617 if (ro
->ro_rt
== (struct rtentry
*)0 ||
618 ro
->ro_rt
->rt_ifp
== (struct ifnet
*)0) {
619 struct sockaddr_in6
*dst6
;
621 /* No route yet, so try to acquire one */
622 bzero(&ro
->ro_dst
, sizeof(struct sockaddr_in6
));
623 dst6
= (struct sockaddr_in6
*)&ro
->ro_dst
;
624 dst6
->sin6_family
= AF_INET6
;
625 dst6
->sin6_len
= sizeof(struct sockaddr_in6
);
626 dst6
->sin6_addr
= *dst
;
627 if (IN6_IS_ADDR_MULTICAST(dst
)) {
628 ro
->ro_rt
= rtalloc1(&((struct route
*)ro
)
631 rtalloc((struct route
*)ro
);
636 * in_pcbconnect() checks out IFF_LOOPBACK to skip using
637 * the address. But we don't know why it does so.
638 * It is necessary to ensure the scope even for lo0
639 * so doesn't check out IFF_LOOPBACK.
643 ia6
= in6_ifawithscope(ro
->ro_rt
->rt_ifa
->ifa_ifp
, dst
);
644 if (ia6
== 0) { /* xxx scope error ?*/
645 ifaref(ro
->ro_rt
->rt_ifa
);
646 ia6
= ifatoia6(ro
->ro_rt
->rt_ifa
);
650 *errorp
= EHOSTUNREACH
; /* no route */
653 *src_storage
= satosin6(&ia6
->ia_addr
)->sin6_addr
;
658 *errorp
= EADDRNOTAVAIL
;
663 * Default hop limit selection. The precedence is as follows:
664 * 1. Hoplimit valued specified via ioctl.
665 * 2. (If the outgoing interface is detected) the current
666 * hop limit of the interface specified by router advertisement.
667 * 3. The system default hoplimit.
674 if (in6p
&& in6p
->in6p_hops
>= 0)
675 return(in6p
->in6p_hops
);
677 return(nd_ifinfo
[ifp
->if_index
].chlim
);
684 in6_pcbdisconnect(inp
)
687 if (!lck_rw_try_lock_exclusive(inp
->inp_pcbinfo
->mtx
)) {
688 /*lock inversion issue, mostly with udp multicast packets */
689 socket_unlock(inp
->inp_socket
, 0);
690 lck_rw_lock_exclusive(inp
->inp_pcbinfo
->mtx
);
691 socket_lock(inp
->inp_socket
, 0);
693 bzero((caddr_t
)&inp
->in6p_faddr
, sizeof(inp
->in6p_faddr
));
695 /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
696 inp
->in6p_flowinfo
&= ~IPV6_FLOWLABEL_MASK
;
698 lck_rw_done(inp
->inp_pcbinfo
->mtx
);
699 if (inp
->inp_socket
->so_state
& SS_NOFDREF
)
707 struct socket
*so
= inp
->inp_socket
;
708 struct inpcbinfo
*ipi
= inp
->inp_pcbinfo
;
711 if (inp
->in6p_sp
!= NULL
) {
712 lck_mtx_lock(sadb_mutex
);
713 ipsec6_delete_pcbpolicy(inp
);
714 lck_mtx_unlock(sadb_mutex
);
718 if (in_pcb_checkstate(inp
, WNT_STOPUSING
, 1) != WNT_STOPUSING
)
719 printf("in6_pcbdetach so=%x can't be marked dead ok\n", so
);
721 inp
->inp_state
= INPCB_STATE_DEAD
;
723 if ((so
->so_flags
& SOF_PCBCLEARING
) == 0) {
725 so
->so_flags
|= SOF_PCBCLEARING
;
726 inp
->inp_gencnt
= ++ipi
->ipi_gencnt
;
727 if (inp
->in6p_options
)
728 m_freem(inp
->in6p_options
);
729 ip6_freepcbopts(inp
->in6p_outputopts
);
730 ip6_freemoptions(inp
->in6p_moptions
);
731 if (inp
->in6p_route
.ro_rt
)
732 rtfree(inp
->in6p_route
.ro_rt
);
733 /* Check and free IPv4 related resources in case of mapped addr */
734 if (inp
->inp_options
)
735 (void)m_free(inp
->inp_options
);
736 ip_freemoptions(inp
->inp_moptions
);
737 inp
->inp_moptions
= NULL
;
743 in6_sockaddr(port
, addr_p
)
745 struct in6_addr
*addr_p
;
747 struct sockaddr_in6
*sin6
;
749 MALLOC(sin6
, struct sockaddr_in6
*, sizeof *sin6
, M_SONAME
, M_WAITOK
);
750 bzero(sin6
, sizeof *sin6
);
751 sin6
->sin6_family
= AF_INET6
;
752 sin6
->sin6_len
= sizeof(*sin6
);
753 sin6
->sin6_port
= port
;
754 sin6
->sin6_addr
= *addr_p
;
755 if (IN6_IS_SCOPE_LINKLOCAL(&sin6
->sin6_addr
))
756 sin6
->sin6_scope_id
= ntohs(sin6
->sin6_addr
.s6_addr16
[1]);
758 sin6
->sin6_scope_id
= 0; /*XXX*/
759 if (IN6_IS_SCOPE_LINKLOCAL(&sin6
->sin6_addr
))
760 sin6
->sin6_addr
.s6_addr16
[1] = 0;
762 return (struct sockaddr
*)sin6
;
766 in6_v4mapsin6_sockaddr(port
, addr_p
)
768 struct in_addr
*addr_p
;
770 struct sockaddr_in sin
;
771 struct sockaddr_in6
*sin6_p
;
773 bzero(&sin
, sizeof sin
);
774 sin
.sin_family
= AF_INET
;
775 sin
.sin_len
= sizeof(sin
);
777 sin
.sin_addr
= *addr_p
;
779 MALLOC(sin6_p
, struct sockaddr_in6
*, sizeof *sin6_p
, M_SONAME
,
781 in6_sin_2_v4mapsin6(&sin
, sin6_p
);
783 return (struct sockaddr
*)sin6_p
;
787 * The calling convention of in6_setsockaddr() and in6_setpeeraddr() was
788 * modified to match the pru_sockaddr() and pru_peeraddr() entry points
789 * in struct pr_usrreqs, so that protocols can just reference then directly
790 * without the need for a wrapper function. The socket must have a valid
791 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
792 * except through a kernel programming error, so it is acceptable to panic
793 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap
794 * because there actually /is/ a programming error somewhere... XXX)
797 in6_setsockaddr(so
, nam
)
799 struct sockaddr
**nam
;
802 struct in6_addr addr
;
809 port
= inp
->inp_lport
;
810 addr
= inp
->in6p_laddr
;
812 *nam
= in6_sockaddr(port
, &addr
);
817 in6_setpeeraddr(so
, nam
)
819 struct sockaddr
**nam
;
822 struct in6_addr addr
;
829 port
= inp
->inp_fport
;
830 addr
= inp
->in6p_faddr
;
832 *nam
= in6_sockaddr(port
, &addr
);
837 in6_mapped_sockaddr(struct socket
*so
, struct sockaddr
**nam
)
839 struct inpcb
*inp
= sotoinpcb(so
);
844 if (inp
->inp_vflag
& INP_IPV4
) {
845 error
= in_setsockaddr(so
, nam
);
847 in6_sin_2_v4mapsin6_in_sock(nam
);
849 /* scope issues will be handled in in6_setsockaddr(). */
850 error
= in6_setsockaddr(so
, nam
);
856 in6_mapped_peeraddr(struct socket
*so
, struct sockaddr
**nam
)
858 struct inpcb
*inp
= sotoinpcb(so
);
863 if (inp
->inp_vflag
& INP_IPV4
) {
864 error
= in_setpeeraddr(so
, nam
);
866 in6_sin_2_v4mapsin6_in_sock(nam
);
868 /* scope issues will be handled in in6_setpeeraddr(). */
869 error
= in6_setpeeraddr(so
, nam
);
875 * Pass some notification to all connections of a protocol
876 * associated with address dst. The local address and/or port numbers
877 * may be specified to limit the search. The "usual action" will be
878 * taken, depending on the ctlinput cmd. The caller must filter any
879 * cmds that are uninteresting (e.g., no error in the map).
880 * Call the protocol specific routine (if any) to report
881 * any errors for each matching socket.
883 * Must be called at splnet.
886 in6_pcbnotify(pcbinfo
, dst
, fport_arg
, src
, lport_arg
, cmd
, notify
)
887 struct inpcbinfo
*pcbinfo
;
888 struct sockaddr
*dst
;
889 const struct sockaddr
*src
;
890 u_int fport_arg
, lport_arg
;
892 // struct inpcb *(*notify)(struct inpcb *, int);
893 void (*notify
)(struct inpcb
*, int);
895 struct inpcb
*inp
, *ninp
;
896 struct sockaddr_in6 sa6_src
, *sa6_dst
;
897 u_short fport
= fport_arg
, lport
= lport_arg
;
900 struct inpcbhead
*head
= pcbinfo
->listhead
;
902 if ((unsigned)cmd
> PRC_NCMDS
|| dst
->sa_family
!= AF_INET6
)
905 sa6_dst
= (struct sockaddr_in6
*)dst
;
906 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst
->sin6_addr
))
910 * note that src can be NULL when we get notify by local fragmentation.
912 sa6_src
= (src
== NULL
) ? sa6_any
: *(const struct sockaddr_in6
*)src
;
913 flowinfo
= sa6_src
.sin6_flowinfo
;
916 * Redirects go to all references to the destination,
917 * and use in6_rtchange to invalidate the route cache.
918 * Dead host indications: also use in6_rtchange to invalidate
919 * the cache, and deliver the error to all the sockets.
920 * Otherwise, if we have knowledge of the local port and address,
921 * deliver only to that socket.
923 if (PRC_IS_REDIRECT(cmd
) || cmd
== PRC_HOSTDEAD
) {
926 bzero((caddr_t
)&sa6_src
.sin6_addr
, sizeof(sa6_src
.sin6_addr
));
928 if (cmd
!= PRC_HOSTDEAD
)
929 notify
= in6_rtchange
;
931 errno
= inet6ctlerrmap
[cmd
];
932 lck_rw_lock_shared(pcbinfo
->mtx
);
933 for (inp
= LIST_FIRST(head
); inp
!= NULL
; inp
= ninp
) {
934 ninp
= LIST_NEXT(inp
, inp_list
);
936 if ((inp
->inp_vflag
& INP_IPV6
) == 0)
940 * Detect if we should notify the error. If no source and
941 * destination ports are specifed, but non-zero flowinfo and
942 * local address match, notify the error. This is the case
943 * when the error is delivered with an encrypted buffer
944 * by ESP. Otherwise, just compare addresses and ports
947 if (lport
== 0 && fport
== 0 && flowinfo
&&
948 inp
->inp_socket
!= NULL
&&
949 flowinfo
== (inp
->in6p_flowinfo
& IPV6_FLOWLABEL_MASK
) &&
950 IN6_ARE_ADDR_EQUAL(&inp
->in6p_laddr
, &sa6_src
.sin6_addr
))
952 else if (!IN6_ARE_ADDR_EQUAL(&inp
->in6p_faddr
,
953 &sa6_dst
->sin6_addr
) ||
954 inp
->inp_socket
== 0 ||
955 (lport
&& inp
->inp_lport
!= lport
) ||
956 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src
.sin6_addr
) &&
957 !IN6_ARE_ADDR_EQUAL(&inp
->in6p_laddr
,
958 &sa6_src
.sin6_addr
)) ||
959 (fport
&& inp
->inp_fport
!= fport
))
965 if (in_pcb_checkstate(inp
, WNT_ACQUIRE
, 0) == WNT_STOPUSING
)
967 socket_lock(inp
->inp_socket
, 1);
968 (*notify
)(inp
, errno
);
969 (void)in_pcb_checkstate(inp
, WNT_RELEASE
, 1);
970 socket_unlock(inp
->inp_socket
, 1);
973 lck_rw_done(pcbinfo
->mtx
);
977 * Lookup a PCB based on the local address and port.
980 in6_pcblookup_local(pcbinfo
, laddr
, lport_arg
, wild_okay
)
981 struct inpcbinfo
*pcbinfo
;
982 struct in6_addr
*laddr
;
987 int matchwild
= 3, wildcard
;
988 u_short lport
= lport_arg
;
991 struct inpcbhead
*head
;
993 * Look for an unconnected (wildcard foreign addr) PCB that
994 * matches the local address and port we're looking for.
996 head
= &pcbinfo
->hashbase
[INP_PCBHASH(INADDR_ANY
, lport
, 0,
998 LIST_FOREACH(inp
, head
, inp_hash
) {
999 if ((inp
->inp_vflag
& INP_IPV6
) == 0)
1001 if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_faddr
) &&
1002 IN6_ARE_ADDR_EQUAL(&inp
->in6p_laddr
, laddr
) &&
1003 inp
->inp_lport
== lport
) {
1015 struct inpcbporthead
*porthash
;
1016 struct inpcbport
*phd
;
1017 struct inpcb
*match
= NULL
;
1019 * Best fit PCB lookup.
1021 * First see if this local port is in use by looking on the
1024 porthash
= &pcbinfo
->porthashbase
[INP_PCBPORTHASH(lport
,
1025 pcbinfo
->porthashmask
)];
1026 LIST_FOREACH(phd
, porthash
, phd_hash
) {
1027 if (phd
->phd_port
== lport
)
1032 * Port is in use by one or more PCBs. Look for best
1035 LIST_FOREACH(inp
, &phd
->phd_pcblist
, inp_portlist
) {
1037 if ((inp
->inp_vflag
& INP_IPV6
) == 0)
1039 if (!IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_faddr
))
1041 if (!IN6_IS_ADDR_UNSPECIFIED(
1042 &inp
->in6p_laddr
)) {
1043 if (IN6_IS_ADDR_UNSPECIFIED(laddr
))
1045 else if (!IN6_ARE_ADDR_EQUAL(
1046 &inp
->in6p_laddr
, laddr
))
1049 if (!IN6_IS_ADDR_UNSPECIFIED(laddr
))
1052 if (wildcard
< matchwild
) {
1054 matchwild
= wildcard
;
1055 if (matchwild
== 0) {
1065 /* this is not used in Darwin */
1068 struct in6pcb
*head
,
1071 struct in6pcb
*in6p
;
1072 struct ip6_moptions
*im6o
;
1073 struct in6_multi_mship
*imm
, *nimm
;
1075 for (in6p
= head
; in6p
!= NULL
; in6p
= LIST_NEXT(in6p
, inp_list
)) {
1076 im6o
= in6p
->in6p_moptions
;
1077 if ((in6p
->inp_vflag
& INP_IPV6
) &&
1080 * Unselect the outgoing interface if it is being
1083 if (im6o
->im6o_multicast_ifp
== ifp
)
1084 im6o
->im6o_multicast_ifp
= NULL
;
1087 * Drop multicast group membership if we joined
1088 * through the interface being detached.
1089 * XXX controversial - is it really legal for kernel
1092 for (imm
= im6o
->im6o_memberships
.lh_first
;
1093 imm
!= NULL
; imm
= nimm
) {
1094 nimm
= imm
->i6mm_chain
.le_next
;
1095 if (imm
->i6mm_maddr
->in6m_ifp
== ifp
) {
1096 LIST_REMOVE(imm
, i6mm_chain
);
1097 in6_delmulti(imm
->i6mm_maddr
);
1098 FREE(imm
, M_IPMADDR
);
1107 * Check for alternatives when higher level complains
1108 * about service problems. For now, invalidate cached
1109 * routing information. If the route was created dynamically
1110 * (by a redirect), time to try a default gateway again.
1117 struct rt_addrinfo info
;
1119 if ((rt
= in6p
->in6p_route
.ro_rt
) != NULL
) {
1120 in6p
->in6p_route
.ro_rt
= 0;
1121 bzero((caddr_t
)&info
, sizeof(info
));
1122 info
.rti_info
[RTAX_DST
] =
1123 (struct sockaddr
*)&in6p
->in6p_route
.ro_dst
;
1124 info
.rti_info
[RTAX_GATEWAY
] = rt
->rt_gateway
;
1125 info
.rti_info
[RTAX_NETMASK
] = rt_mask(rt
);
1126 lck_mtx_lock(rt_mtx
);
1127 rt_missmsg(RTM_LOSING
, &info
, rt
->rt_flags
, 0);
1128 if (rt
->rt_flags
& RTF_DYNAMIC
)
1129 (void)rtrequest_locked(RTM_DELETE
, rt_key(rt
),
1130 rt
->rt_gateway
, rt_mask(rt
), rt
->rt_flags
,
1131 (struct rtentry
**)0);
1134 * A new route can be allocated
1135 * the next time output is attempted.
1138 lck_mtx_unlock(rt_mtx
);
1143 * After a routing change, flush old routing
1144 * and allocate a (hopefully) better one.
1151 if (inp
->in6p_route
.ro_rt
) {
1152 rtfree(inp
->in6p_route
.ro_rt
);
1153 inp
->in6p_route
.ro_rt
= 0;
1155 * A new route can be allocated the next time
1156 * output is attempted.
1162 * Lookup PCB in hash list.
1166 struct inpcbinfo
*pcbinfo
,
1167 struct in6_addr
*faddr
,
1169 struct in6_addr
*laddr
,
1174 struct inpcbhead
*head
;
1176 u_short fport
= fport_arg
, lport
= lport_arg
;
1179 #if defined(NFAITH) && NFAITH > 0
1180 faith
= faithprefix(laddr
);
1185 lck_rw_lock_shared(pcbinfo
->mtx
);
1188 * First look for an exact match.
1190 head
= &pcbinfo
->hashbase
[INP_PCBHASH(faddr
->s6_addr32
[3] /* XXX */,
1192 pcbinfo
->hashmask
)];
1193 LIST_FOREACH(inp
, head
, inp_hash
) {
1194 if ((inp
->inp_vflag
& INP_IPV6
) == 0)
1196 if (IN6_ARE_ADDR_EQUAL(&inp
->in6p_faddr
, faddr
) &&
1197 IN6_ARE_ADDR_EQUAL(&inp
->in6p_laddr
, laddr
) &&
1198 inp
->inp_fport
== fport
&&
1199 inp
->inp_lport
== lport
) {
1201 * Found. Check if pcb is still valid
1203 if (in_pcb_checkstate(inp
, WNT_ACQUIRE
, 0) != WNT_STOPUSING
) {
1204 lck_rw_done(pcbinfo
->mtx
);
1207 else { /* it's there but dead, say it isn't found */
1208 lck_rw_done(pcbinfo
->mtx
);
1214 struct inpcb
*local_wild
= NULL
;
1216 head
= &pcbinfo
->hashbase
[INP_PCBHASH(INADDR_ANY
, lport
, 0,
1217 pcbinfo
->hashmask
)];
1218 LIST_FOREACH(inp
, head
, inp_hash
) {
1219 if ((inp
->inp_vflag
& INP_IPV6
) == 0)
1221 if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_faddr
) &&
1222 inp
->inp_lport
== lport
) {
1223 if (faith
&& (inp
->inp_flags
& INP_FAITH
) == 0)
1225 if (IN6_ARE_ADDR_EQUAL(&inp
->in6p_laddr
,
1227 if (in_pcb_checkstate(inp
, WNT_ACQUIRE
, 0) != WNT_STOPUSING
) {
1228 lck_rw_done(pcbinfo
->mtx
);
1231 else { /* it's there but dead, say it isn't found */
1232 lck_rw_done(pcbinfo
->mtx
);
1236 else if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
))
1240 if (local_wild
&& in_pcb_checkstate(local_wild
, WNT_ACQUIRE
, 0) != WNT_STOPUSING
) {
1241 lck_rw_done(pcbinfo
->mtx
);
1242 return (local_wild
);
1245 lck_rw_done(pcbinfo
->mtx
);
1253 lck_rw_done(pcbinfo
->mtx
);
1258 init_sin6(struct sockaddr_in6
*sin6
, struct mbuf
*m
)
1262 ip
= mtod(m
, struct ip6_hdr
*);
1263 bzero(sin6
, sizeof(*sin6
));
1264 sin6
->sin6_len
= sizeof(*sin6
);
1265 sin6
->sin6_family
= AF_INET6
;
1266 sin6
->sin6_addr
= ip
->ip6_src
;
1267 if (IN6_IS_SCOPE_LINKLOCAL(&sin6
->sin6_addr
))
1268 sin6
->sin6_addr
.s6_addr16
[1] = 0;
1269 sin6
->sin6_scope_id
=
1270 (m
->m_pkthdr
.rcvif
&& IN6_IS_SCOPE_LINKLOCAL(&sin6
->sin6_addr
))
1271 ? m
->m_pkthdr
.rcvif
->if_index
: 0;