]>
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 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
24 * All rights reserved.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. Neither the name of the project nor the names of its contributors
35 * may be used to endorse or promote products derived from this software
36 * without specific prior written permission.
38 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
39 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
41 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
42 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
43 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
44 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
45 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
46 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
47 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * Copyright (c) 1982, 1986, 1991, 1993
54 * The Regents of the University of California. All rights reserved.
56 * Redistribution and use in source and binary forms, with or without
57 * modification, are permitted provided that the following conditions
59 * 1. Redistributions of source code must retain the above copyright
60 * notice, this list of conditions and the following disclaimer.
61 * 2. Redistributions in binary form must reproduce the above copyright
62 * notice, this list of conditions and the following disclaimer in the
63 * documentation and/or other materials provided with the distribution.
64 * 3. All advertising materials mentioning features or use of this software
65 * must display the following acknowledgement:
66 * This product includes software developed by the University of
67 * California, Berkeley and its contributors.
68 * 4. Neither the name of the University nor the names of its contributors
69 * may be used to endorse or promote products derived from this software
70 * without specific prior written permission.
72 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
73 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
74 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
75 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
76 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
77 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
78 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
79 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
80 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
81 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
84 * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94
87 #include <sys/param.h>
88 #include <sys/systm.h>
89 #include <sys/malloc.h>
91 #include <sys/domain.h>
92 #include <sys/protosw.h>
93 #include <sys/socket.h>
94 #include <sys/socketvar.h>
95 #include <sys/sockio.h>
96 #include <sys/errno.h>
101 #include <net/if_types.h>
102 #include <net/route.h>
104 #include <netinet/in.h>
105 #include <netinet/in_var.h>
106 #include <netinet/in_systm.h>
107 #include <netinet/ip6.h>
108 #include <netinet/ip_var.h>
109 #include <netinet6/ip6_var.h>
110 #include <netinet6/nd6.h>
111 #include <netinet/in_pcb.h>
112 #include <netinet6/in6_pcb.h>
113 #include <net/if_types.h>
115 #include <kern/kern_types.h>
116 #include <kern/zalloc.h>
119 #if defined(NFAITH) && NFAITH > 0
120 #include <net/if_faith.h>
124 #include <netinet6/ipsec.h>
126 #include <netinet6/ipsec6.h>
128 #include <netinet6/ah.h>
130 #include <netinet6/ah6.h>
132 #include <netkey/key.h>
133 extern lck_mtx_t
*sadb_mutex
;
136 struct in6_addr zeroin6_addr
;
141 struct sockaddr
*nam
,
144 struct socket
*so
= inp
->inp_socket
;
145 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)NULL
;
146 struct inpcbinfo
*pcbinfo
= inp
->inp_pcbinfo
;
148 int wild
= 0, reuseport
= (so
->so_options
& SO_REUSEPORT
);
150 if (!in6_ifaddrs
) /* XXX broken! */
151 return (EADDRNOTAVAIL
);
152 if (inp
->inp_lport
|| !IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
))
154 if ((so
->so_options
& (SO_REUSEADDR
|SO_REUSEPORT
)) == 0)
156 socket_unlock(so
, 0); /* keep reference */
157 lck_rw_lock_exclusive(pcbinfo
->mtx
);
159 sin6
= (struct sockaddr_in6
*)nam
;
160 if (nam
->sa_len
!= sizeof(*sin6
)) {
161 lck_rw_done(pcbinfo
->mtx
);
168 if (nam
->sa_family
!= AF_INET6
) {
169 lck_rw_done(pcbinfo
->mtx
);
171 return(EAFNOSUPPORT
);
174 /* KAME hack: embed scopeid */
175 if (in6_embedscope(&sin6
->sin6_addr
, sin6
, inp
, NULL
) != 0) {
176 lck_rw_done(pcbinfo
->mtx
);
180 /* this must be cleared for ifa_ifwithaddr() */
181 sin6
->sin6_scope_id
= 0;
183 lport
= sin6
->sin6_port
;
184 if (IN6_IS_ADDR_MULTICAST(&sin6
->sin6_addr
)) {
186 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
187 * allow compepte duplication of binding if
188 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
189 * and a multicast address is bound on both
190 * new and duplicated sockets.
192 if (so
->so_options
& SO_REUSEADDR
)
193 reuseport
= SO_REUSEADDR
|SO_REUSEPORT
;
194 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6
->sin6_addr
)) {
195 struct ifaddr
*ia
= NULL
;
197 sin6
->sin6_port
= 0; /* yech... */
198 if ((ia
= ifa_ifwithaddr((struct sockaddr
*)sin6
)) == 0) {
199 lck_rw_done(pcbinfo
->mtx
);
201 return(EADDRNOTAVAIL
);
205 * XXX: bind to an anycast address might accidentally
206 * cause sending a packet with anycast source address.
207 * We should allow to bind to a deprecated address, since
208 * the application dare to use it.
211 ((struct in6_ifaddr
*)ia
)->ia6_flags
&
212 (IN6_IFF_ANYCAST
|IN6_IFF_NOTREADY
|IN6_IFF_DETACHED
)) {
214 lck_rw_done(pcbinfo
->mtx
);
216 return(EADDRNOTAVAIL
);
225 if (ntohs(lport
) < IPV6PORT_RESERVED
&& p
&&
226 ((so
->so_state
& SS_PRIV
) == 0)) {
227 lck_rw_done(pcbinfo
->mtx
);
233 !IN6_IS_ADDR_MULTICAST(&sin6
->sin6_addr
)) {
234 t
= in6_pcblookup_local(pcbinfo
,
235 &sin6
->sin6_addr
, lport
,
238 (!IN6_IS_ADDR_UNSPECIFIED(&sin6
->sin6_addr
) ||
239 !IN6_IS_ADDR_UNSPECIFIED(&t
->in6p_laddr
) ||
240 (t
->inp_socket
->so_options
&
241 SO_REUSEPORT
) == 0) &&
242 so
->so_uid
!= t
->inp_socket
->so_uid
) {
243 lck_rw_done(pcbinfo
->mtx
);
247 if ((inp
->inp_flags
& IN6P_IPV6_V6ONLY
) == 0 &&
248 IN6_IS_ADDR_UNSPECIFIED(&sin6
->sin6_addr
)) {
249 struct sockaddr_in sin
;
251 in6_sin6_2_sin(&sin
, sin6
);
252 t
= in_pcblookup_local(pcbinfo
,
257 t
->inp_socket
->so_uid
) &&
258 (ntohl(t
->inp_laddr
.s_addr
) !=
261 INP_SOCKAF(t
->inp_socket
))) {
263 lck_rw_done(pcbinfo
->mtx
);
269 t
= in6_pcblookup_local(pcbinfo
, &sin6
->sin6_addr
,
271 if (t
&& (reuseport
& t
->inp_socket
->so_options
) == 0) {
272 lck_rw_done(pcbinfo
->mtx
);
276 if ((inp
->inp_flags
& IN6P_IPV6_V6ONLY
) == 0 &&
277 IN6_IS_ADDR_UNSPECIFIED(&sin6
->sin6_addr
)) {
278 struct sockaddr_in sin
;
280 in6_sin6_2_sin(&sin
, sin6
);
281 t
= in_pcblookup_local(pcbinfo
, sin
.sin_addr
,
284 (reuseport
& t
->inp_socket
->so_options
)
286 (ntohl(t
->inp_laddr
.s_addr
)
289 INP_SOCKAF(t
->inp_socket
))) {
290 lck_rw_done(pcbinfo
->mtx
);
296 inp
->in6p_laddr
= sin6
->sin6_addr
;
301 if ((e
= in6_pcbsetport(&inp
->in6p_laddr
, inp
, p
, 1)) != 0) {
302 lck_rw_done(pcbinfo
->mtx
);
307 inp
->inp_lport
= lport
;
308 if (in_pcbinshash(inp
, 1) != 0) {
309 inp
->in6p_laddr
= in6addr_any
;
311 lck_rw_done(pcbinfo
->mtx
);
315 lck_rw_done(pcbinfo
->mtx
);
320 * Transform old in6_pcbconnect() into an inner subroutine for new
321 * in6_pcbconnect(): Do some validity-checking on the remote
322 * address (in mbuf 'nam') and then determine local host address
323 * (i.e., which interface) to use to access that remote host.
325 * This preserves definition of in6_pcbconnect(), while supporting a
326 * slightly different version for T/TCP. (This is more than
327 * a bit of a kludge, but cleaning up the internal interfaces would
328 * have forced minor changes in every protocol).
334 struct sockaddr
*nam
,
335 struct in6_addr
*plocal_addr6
)
337 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)nam
;
338 struct in6_addr
*addr6
= NULL
;
339 struct in6_addr src_storage
;
341 struct ifnet
*ifp
= NULL
;
344 if (nam
->sa_len
!= sizeof (*sin6
))
346 if (sin6
->sin6_family
!= AF_INET6
)
347 return (EAFNOSUPPORT
);
348 if (sin6
->sin6_port
== 0)
349 return (EADDRNOTAVAIL
);
351 /* KAME hack: embed scopeid */
352 if (in6_embedscope(&sin6
->sin6_addr
, sin6
, inp
, &ifp
) != 0)
357 * If the destination address is UNSPECIFIED addr,
358 * use the loopback addr, e.g ::1.
360 if (IN6_IS_ADDR_UNSPECIFIED(&sin6
->sin6_addr
))
361 sin6
->sin6_addr
= in6addr_loopback
;
365 * XXX: in6_selectsrc might replace the bound local address
366 * with the address specified by setsockopt(IPV6_PKTINFO).
367 * Is it the intended behavior?
369 addr6
= in6_selectsrc(sin6
, inp
->in6p_outputopts
,
372 &inp
->in6p_laddr
, &src_storage
, &error
);
375 error
= EADDRNOTAVAIL
;
378 *plocal_addr6
= *addr6
;
380 * Don't do pcblookup call here; return interface in
382 * and exit to caller, that will do the lookup.
386 if (inp
->in6p_route
.ro_rt
)
387 ifp
= inp
->in6p_route
.ro_rt
->rt_ifp
;
394 * Connect from a socket to a specified address.
395 * Both address and port must be specified in argument sin.
396 * If don't have a local address for this socket yet,
400 in6_pcbconnect(inp
, nam
, p
)
402 struct sockaddr
*nam
;
405 struct in6_addr addr6
;
406 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)nam
;
411 * Call inner routine, to assign local interface address.
412 * in6_pcbladdr() may automatically fill in sin6_scope_id.
414 if ((error
= in6_pcbladdr(inp
, nam
, &addr6
)) != 0)
416 socket_unlock(inp
->inp_socket
, 0);
417 pcb
= in6_pcblookup_hash(inp
->inp_pcbinfo
, &sin6
->sin6_addr
,
419 IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
)
420 ? &addr6
: &inp
->in6p_laddr
,
421 inp
->inp_lport
, 0, NULL
);
422 socket_lock(inp
->inp_socket
, 0);
424 in_pcb_checkstate(pcb
, WNT_RELEASE
, 0);
427 if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
)) {
428 if (inp
->inp_lport
== 0) {
429 error
= in6_pcbbind(inp
, (struct sockaddr
*)0, p
);
433 inp
->in6p_laddr
= addr6
;
435 if (!lck_rw_try_lock_exclusive(inp
->inp_pcbinfo
->mtx
)) {
436 /*lock inversion issue, mostly with udp multicast packets */
437 socket_unlock(inp
->inp_socket
, 0);
438 lck_rw_lock_exclusive(inp
->inp_pcbinfo
->mtx
);
439 socket_lock(inp
->inp_socket
, 0);
441 inp
->in6p_faddr
= sin6
->sin6_addr
;
442 inp
->inp_fport
= sin6
->sin6_port
;
443 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
444 inp
->in6p_flowinfo
&= ~IPV6_FLOWLABEL_MASK
;
445 if (inp
->in6p_flags
& IN6P_AUTOFLOWLABEL
)
446 inp
->in6p_flowinfo
|=
447 (htonl(ip6_flow_seq
++) & IPV6_FLOWLABEL_MASK
);
450 lck_rw_done(inp
->inp_pcbinfo
->mtx
);
456 * Return an IPv6 address, which is the most appropriate for given
457 * destination and user specified options.
458 * If necessary, this function lookups the routing table and return
459 * an entry to the caller for later use.
463 struct sockaddr_in6
*dstsock
,
464 struct ip6_pktopts
*opts
,
465 struct ip6_moptions
*mopts
,
466 struct route_in6
*ro
,
467 struct in6_addr
*laddr
,
468 struct in6_addr
*src_storage
,
471 struct in6_addr
*dst
;
472 struct in6_ifaddr
*ia6
= 0;
473 struct in6_pktinfo
*pi
= NULL
;
475 dst
= &dstsock
->sin6_addr
;
479 * If the source address is explicitly specified by the caller,
482 if (opts
&& (pi
= opts
->ip6po_pktinfo
) &&
483 !IN6_IS_ADDR_UNSPECIFIED(&pi
->ipi6_addr
))
484 return(&pi
->ipi6_addr
);
487 * If the source address is not specified but the socket(if any)
488 * is already bound, use the bound address.
490 if (laddr
&& !IN6_IS_ADDR_UNSPECIFIED(laddr
))
494 * If the caller doesn't specify the source address but
495 * the outgoing interface, use an address associated with
498 if (pi
&& pi
->ipi6_ifindex
) {
499 /* XXX boundary check is assumed to be already done. */
500 ia6
= in6_ifawithscope(ifindex2ifnet
[pi
->ipi6_ifindex
],
503 *errorp
= EADDRNOTAVAIL
;
506 *src_storage
= satosin6(&ia6
->ia_addr
)->sin6_addr
;
507 ifafree(&ia6
->ia_ifa
);
512 * If the destination address is a link-local unicast address or
513 * a multicast address, and if the outgoing interface is specified
514 * by the sin6_scope_id filed, use an address associated with the
516 * XXX: We're now trying to define more specific semantics of
517 * sin6_scope_id field, so this part will be rewritten in
520 if ((IN6_IS_ADDR_LINKLOCAL(dst
) || IN6_IS_ADDR_MULTICAST(dst
)) &&
521 dstsock
->sin6_scope_id
) {
523 * I'm not sure if boundary check for scope_id is done
526 if (dstsock
->sin6_scope_id
< 0 ||
527 if_index
< dstsock
->sin6_scope_id
) {
528 *errorp
= ENXIO
; /* XXX: better error? */
531 ia6
= in6_ifawithscope(ifindex2ifnet
[dstsock
->sin6_scope_id
],
534 *errorp
= EADDRNOTAVAIL
;
537 *src_storage
= satosin6(&ia6
->ia_addr
)->sin6_addr
;
538 ifafree(&ia6
->ia_ifa
);
543 * If the destination address is a multicast address and
544 * the outgoing interface for the address is specified
545 * by the caller, use an address associated with the interface.
546 * There is a sanity check here; if the destination has node-local
547 * scope, the outgoing interfacde should be a loopback address.
548 * Even if the outgoing interface is not specified, we also
549 * choose a loopback interface as the outgoing interface.
551 if (IN6_IS_ADDR_MULTICAST(dst
)) {
552 struct ifnet
*ifp
= mopts
? mopts
->im6o_multicast_ifp
: NULL
;
554 if (ifp
== NULL
&& IN6_IS_ADDR_MC_NODELOCAL(dst
)) {
559 ia6
= in6_ifawithscope(ifp
, dst
);
561 *errorp
= EADDRNOTAVAIL
;
564 *src_storage
= ia6
->ia_addr
.sin6_addr
;
565 ifafree(&ia6
->ia_ifa
);
571 * If the next hop address for the packet is specified
572 * by caller, use an address associated with the route
576 struct sockaddr_in6
*sin6_next
;
579 if (opts
&& opts
->ip6po_nexthop
) {
580 sin6_next
= satosin6(opts
->ip6po_nexthop
);
581 rt
= nd6_lookup(&sin6_next
->sin6_addr
, 1, NULL
, 0);
583 ia6
= in6_ifawithscope(rt
->rt_ifp
, dst
);
586 ia6
= ifatoia6(rt
->rt_ifa
);
590 *errorp
= EADDRNOTAVAIL
;
593 *src_storage
= satosin6(&ia6
->ia_addr
)->sin6_addr
;
600 * If route is known or can be allocated now,
601 * our src addr is taken from the i/f, else punt.
605 !IN6_ARE_ADDR_EQUAL(&satosin6(&ro
->ro_dst
)->sin6_addr
, dst
)) {
607 ro
->ro_rt
= (struct rtentry
*)0;
609 if (ro
->ro_rt
== (struct rtentry
*)0 ||
610 ro
->ro_rt
->rt_ifp
== (struct ifnet
*)0) {
611 struct sockaddr_in6
*dst6
;
613 /* No route yet, so try to acquire one */
614 bzero(&ro
->ro_dst
, sizeof(struct sockaddr_in6
));
615 dst6
= (struct sockaddr_in6
*)&ro
->ro_dst
;
616 dst6
->sin6_family
= AF_INET6
;
617 dst6
->sin6_len
= sizeof(struct sockaddr_in6
);
618 dst6
->sin6_addr
= *dst
;
619 if (IN6_IS_ADDR_MULTICAST(dst
)) {
620 ro
->ro_rt
= rtalloc1(&((struct route
*)ro
)
623 rtalloc((struct route
*)ro
);
628 * in_pcbconnect() checks out IFF_LOOPBACK to skip using
629 * the address. But we don't know why it does so.
630 * It is necessary to ensure the scope even for lo0
631 * so doesn't check out IFF_LOOPBACK.
635 ia6
= in6_ifawithscope(ro
->ro_rt
->rt_ifa
->ifa_ifp
, dst
);
636 if (ia6
== 0) { /* xxx scope error ?*/
637 ifaref(ro
->ro_rt
->rt_ifa
);
638 ia6
= ifatoia6(ro
->ro_rt
->rt_ifa
);
642 *errorp
= EHOSTUNREACH
; /* no route */
645 *src_storage
= satosin6(&ia6
->ia_addr
)->sin6_addr
;
650 *errorp
= EADDRNOTAVAIL
;
655 * Default hop limit selection. The precedence is as follows:
656 * 1. Hoplimit valued specified via ioctl.
657 * 2. (If the outgoing interface is detected) the current
658 * hop limit of the interface specified by router advertisement.
659 * 3. The system default hoplimit.
666 if (in6p
&& in6p
->in6p_hops
>= 0)
667 return(in6p
->in6p_hops
);
669 return(nd_ifinfo
[ifp
->if_index
].chlim
);
676 in6_pcbdisconnect(inp
)
679 if (!lck_rw_try_lock_exclusive(inp
->inp_pcbinfo
->mtx
)) {
680 /*lock inversion issue, mostly with udp multicast packets */
681 socket_unlock(inp
->inp_socket
, 0);
682 lck_rw_lock_exclusive(inp
->inp_pcbinfo
->mtx
);
683 socket_lock(inp
->inp_socket
, 0);
685 bzero((caddr_t
)&inp
->in6p_faddr
, sizeof(inp
->in6p_faddr
));
687 /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
688 inp
->in6p_flowinfo
&= ~IPV6_FLOWLABEL_MASK
;
690 lck_rw_done(inp
->inp_pcbinfo
->mtx
);
691 if (inp
->inp_socket
->so_state
& SS_NOFDREF
)
699 struct socket
*so
= inp
->inp_socket
;
700 struct inpcbinfo
*ipi
= inp
->inp_pcbinfo
;
703 if (inp
->in6p_sp
!= NULL
) {
704 lck_mtx_lock(sadb_mutex
);
705 ipsec6_delete_pcbpolicy(inp
);
706 lck_mtx_unlock(sadb_mutex
);
710 if (in_pcb_checkstate(inp
, WNT_STOPUSING
, 1) != WNT_STOPUSING
)
711 printf("in6_pcbdetach so=%x can't be marked dead ok\n", so
);
713 inp
->inp_state
= INPCB_STATE_DEAD
;
715 if ((so
->so_flags
& SOF_PCBCLEARING
) == 0) {
717 so
->so_flags
|= SOF_PCBCLEARING
;
718 inp
->inp_gencnt
= ++ipi
->ipi_gencnt
;
719 if (inp
->in6p_options
)
720 m_freem(inp
->in6p_options
);
721 ip6_freepcbopts(inp
->in6p_outputopts
);
722 ip6_freemoptions(inp
->in6p_moptions
);
723 if (inp
->in6p_route
.ro_rt
)
724 rtfree(inp
->in6p_route
.ro_rt
);
725 /* Check and free IPv4 related resources in case of mapped addr */
726 if (inp
->inp_options
)
727 (void)m_free(inp
->inp_options
);
728 ip_freemoptions(inp
->inp_moptions
);
729 inp
->inp_moptions
= NULL
;
735 in6_sockaddr(port
, addr_p
)
737 struct in6_addr
*addr_p
;
739 struct sockaddr_in6
*sin6
;
741 MALLOC(sin6
, struct sockaddr_in6
*, sizeof *sin6
, M_SONAME
, M_WAITOK
);
742 bzero(sin6
, sizeof *sin6
);
743 sin6
->sin6_family
= AF_INET6
;
744 sin6
->sin6_len
= sizeof(*sin6
);
745 sin6
->sin6_port
= port
;
746 sin6
->sin6_addr
= *addr_p
;
747 if (IN6_IS_SCOPE_LINKLOCAL(&sin6
->sin6_addr
))
748 sin6
->sin6_scope_id
= ntohs(sin6
->sin6_addr
.s6_addr16
[1]);
750 sin6
->sin6_scope_id
= 0; /*XXX*/
751 if (IN6_IS_SCOPE_LINKLOCAL(&sin6
->sin6_addr
))
752 sin6
->sin6_addr
.s6_addr16
[1] = 0;
754 return (struct sockaddr
*)sin6
;
758 in6_v4mapsin6_sockaddr(port
, addr_p
)
760 struct in_addr
*addr_p
;
762 struct sockaddr_in sin
;
763 struct sockaddr_in6
*sin6_p
;
765 bzero(&sin
, sizeof sin
);
766 sin
.sin_family
= AF_INET
;
767 sin
.sin_len
= sizeof(sin
);
769 sin
.sin_addr
= *addr_p
;
771 MALLOC(sin6_p
, struct sockaddr_in6
*, sizeof *sin6_p
, M_SONAME
,
773 in6_sin_2_v4mapsin6(&sin
, sin6_p
);
775 return (struct sockaddr
*)sin6_p
;
779 * The calling convention of in6_setsockaddr() and in6_setpeeraddr() was
780 * modified to match the pru_sockaddr() and pru_peeraddr() entry points
781 * in struct pr_usrreqs, so that protocols can just reference then directly
782 * without the need for a wrapper function. The socket must have a valid
783 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
784 * except through a kernel programming error, so it is acceptable to panic
785 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap
786 * because there actually /is/ a programming error somewhere... XXX)
789 in6_setsockaddr(so
, nam
)
791 struct sockaddr
**nam
;
794 struct in6_addr addr
;
801 port
= inp
->inp_lport
;
802 addr
= inp
->in6p_laddr
;
804 *nam
= in6_sockaddr(port
, &addr
);
809 in6_setpeeraddr(so
, nam
)
811 struct sockaddr
**nam
;
814 struct in6_addr addr
;
821 port
= inp
->inp_fport
;
822 addr
= inp
->in6p_faddr
;
824 *nam
= in6_sockaddr(port
, &addr
);
829 in6_mapped_sockaddr(struct socket
*so
, struct sockaddr
**nam
)
831 struct inpcb
*inp
= sotoinpcb(so
);
836 if (inp
->inp_vflag
& INP_IPV4
) {
837 error
= in_setsockaddr(so
, nam
);
839 in6_sin_2_v4mapsin6_in_sock(nam
);
841 /* scope issues will be handled in in6_setsockaddr(). */
842 error
= in6_setsockaddr(so
, nam
);
848 in6_mapped_peeraddr(struct socket
*so
, struct sockaddr
**nam
)
850 struct inpcb
*inp
= sotoinpcb(so
);
855 if (inp
->inp_vflag
& INP_IPV4
) {
856 error
= in_setpeeraddr(so
, nam
);
858 in6_sin_2_v4mapsin6_in_sock(nam
);
860 /* scope issues will be handled in in6_setpeeraddr(). */
861 error
= in6_setpeeraddr(so
, nam
);
867 * Pass some notification to all connections of a protocol
868 * associated with address dst. The local address and/or port numbers
869 * may be specified to limit the search. The "usual action" will be
870 * taken, depending on the ctlinput cmd. The caller must filter any
871 * cmds that are uninteresting (e.g., no error in the map).
872 * Call the protocol specific routine (if any) to report
873 * any errors for each matching socket.
875 * Must be called at splnet.
878 in6_pcbnotify(pcbinfo
, dst
, fport_arg
, src
, lport_arg
, cmd
, notify
)
879 struct inpcbinfo
*pcbinfo
;
880 struct sockaddr
*dst
;
881 const struct sockaddr
*src
;
882 u_int fport_arg
, lport_arg
;
884 // struct inpcb *(*notify)(struct inpcb *, int);
885 void (*notify
)(struct inpcb
*, int);
887 struct inpcb
*inp
, *ninp
;
888 struct sockaddr_in6 sa6_src
, *sa6_dst
;
889 u_short fport
= fport_arg
, lport
= lport_arg
;
892 struct inpcbhead
*head
= pcbinfo
->listhead
;
894 if ((unsigned)cmd
> PRC_NCMDS
|| dst
->sa_family
!= AF_INET6
)
897 sa6_dst
= (struct sockaddr_in6
*)dst
;
898 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst
->sin6_addr
))
902 * note that src can be NULL when we get notify by local fragmentation.
904 sa6_src
= (src
== NULL
) ? sa6_any
: *(const struct sockaddr_in6
*)src
;
905 flowinfo
= sa6_src
.sin6_flowinfo
;
908 * Redirects go to all references to the destination,
909 * and use in6_rtchange to invalidate the route cache.
910 * Dead host indications: also use in6_rtchange to invalidate
911 * the cache, and deliver the error to all the sockets.
912 * Otherwise, if we have knowledge of the local port and address,
913 * deliver only to that socket.
915 if (PRC_IS_REDIRECT(cmd
) || cmd
== PRC_HOSTDEAD
) {
918 bzero((caddr_t
)&sa6_src
.sin6_addr
, sizeof(sa6_src
.sin6_addr
));
920 if (cmd
!= PRC_HOSTDEAD
)
921 notify
= in6_rtchange
;
923 errno
= inet6ctlerrmap
[cmd
];
924 lck_rw_lock_shared(pcbinfo
->mtx
);
925 for (inp
= LIST_FIRST(head
); inp
!= NULL
; inp
= ninp
) {
926 ninp
= LIST_NEXT(inp
, inp_list
);
928 if ((inp
->inp_vflag
& INP_IPV6
) == 0)
932 * Detect if we should notify the error. If no source and
933 * destination ports are specifed, but non-zero flowinfo and
934 * local address match, notify the error. This is the case
935 * when the error is delivered with an encrypted buffer
936 * by ESP. Otherwise, just compare addresses and ports
939 if (lport
== 0 && fport
== 0 && flowinfo
&&
940 inp
->inp_socket
!= NULL
&&
941 flowinfo
== (inp
->in6p_flowinfo
& IPV6_FLOWLABEL_MASK
) &&
942 IN6_ARE_ADDR_EQUAL(&inp
->in6p_laddr
, &sa6_src
.sin6_addr
))
944 else if (!IN6_ARE_ADDR_EQUAL(&inp
->in6p_faddr
,
945 &sa6_dst
->sin6_addr
) ||
946 inp
->inp_socket
== 0 ||
947 (lport
&& inp
->inp_lport
!= lport
) ||
948 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src
.sin6_addr
) &&
949 !IN6_ARE_ADDR_EQUAL(&inp
->in6p_laddr
,
950 &sa6_src
.sin6_addr
)) ||
951 (fport
&& inp
->inp_fport
!= fport
))
957 if (in_pcb_checkstate(inp
, WNT_ACQUIRE
, 0) == WNT_STOPUSING
)
959 socket_lock(inp
->inp_socket
, 1);
960 (*notify
)(inp
, errno
);
961 (void)in_pcb_checkstate(inp
, WNT_RELEASE
, 1);
962 socket_unlock(inp
->inp_socket
, 1);
965 lck_rw_done(pcbinfo
->mtx
);
969 * Lookup a PCB based on the local address and port.
972 in6_pcblookup_local(pcbinfo
, laddr
, lport_arg
, wild_okay
)
973 struct inpcbinfo
*pcbinfo
;
974 struct in6_addr
*laddr
;
979 int matchwild
= 3, wildcard
;
980 u_short lport
= lport_arg
;
983 struct inpcbhead
*head
;
985 * Look for an unconnected (wildcard foreign addr) PCB that
986 * matches the local address and port we're looking for.
988 head
= &pcbinfo
->hashbase
[INP_PCBHASH(INADDR_ANY
, lport
, 0,
990 LIST_FOREACH(inp
, head
, inp_hash
) {
991 if ((inp
->inp_vflag
& INP_IPV6
) == 0)
993 if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_faddr
) &&
994 IN6_ARE_ADDR_EQUAL(&inp
->in6p_laddr
, laddr
) &&
995 inp
->inp_lport
== lport
) {
1007 struct inpcbporthead
*porthash
;
1008 struct inpcbport
*phd
;
1009 struct inpcb
*match
= NULL
;
1011 * Best fit PCB lookup.
1013 * First see if this local port is in use by looking on the
1016 porthash
= &pcbinfo
->porthashbase
[INP_PCBPORTHASH(lport
,
1017 pcbinfo
->porthashmask
)];
1018 LIST_FOREACH(phd
, porthash
, phd_hash
) {
1019 if (phd
->phd_port
== lport
)
1024 * Port is in use by one or more PCBs. Look for best
1027 LIST_FOREACH(inp
, &phd
->phd_pcblist
, inp_portlist
) {
1029 if ((inp
->inp_vflag
& INP_IPV6
) == 0)
1031 if (!IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_faddr
))
1033 if (!IN6_IS_ADDR_UNSPECIFIED(
1034 &inp
->in6p_laddr
)) {
1035 if (IN6_IS_ADDR_UNSPECIFIED(laddr
))
1037 else if (!IN6_ARE_ADDR_EQUAL(
1038 &inp
->in6p_laddr
, laddr
))
1041 if (!IN6_IS_ADDR_UNSPECIFIED(laddr
))
1044 if (wildcard
< matchwild
) {
1046 matchwild
= wildcard
;
1047 if (matchwild
== 0) {
1057 /* this is not used in Darwin */
1060 struct in6pcb
*head
,
1063 struct in6pcb
*in6p
;
1064 struct ip6_moptions
*im6o
;
1065 struct in6_multi_mship
*imm
, *nimm
;
1067 for (in6p
= head
; in6p
!= NULL
; in6p
= LIST_NEXT(in6p
, inp_list
)) {
1068 im6o
= in6p
->in6p_moptions
;
1069 if ((in6p
->inp_vflag
& INP_IPV6
) &&
1072 * Unselect the outgoing interface if it is being
1075 if (im6o
->im6o_multicast_ifp
== ifp
)
1076 im6o
->im6o_multicast_ifp
= NULL
;
1079 * Drop multicast group membership if we joined
1080 * through the interface being detached.
1081 * XXX controversial - is it really legal for kernel
1084 for (imm
= im6o
->im6o_memberships
.lh_first
;
1085 imm
!= NULL
; imm
= nimm
) {
1086 nimm
= imm
->i6mm_chain
.le_next
;
1087 if (imm
->i6mm_maddr
->in6m_ifp
== ifp
) {
1088 LIST_REMOVE(imm
, i6mm_chain
);
1089 in6_delmulti(imm
->i6mm_maddr
);
1090 FREE(imm
, M_IPMADDR
);
1099 * Check for alternatives when higher level complains
1100 * about service problems. For now, invalidate cached
1101 * routing information. If the route was created dynamically
1102 * (by a redirect), time to try a default gateway again.
1109 struct rt_addrinfo info
;
1111 if ((rt
= in6p
->in6p_route
.ro_rt
) != NULL
) {
1112 in6p
->in6p_route
.ro_rt
= 0;
1113 bzero((caddr_t
)&info
, sizeof(info
));
1114 info
.rti_info
[RTAX_DST
] =
1115 (struct sockaddr
*)&in6p
->in6p_route
.ro_dst
;
1116 info
.rti_info
[RTAX_GATEWAY
] = rt
->rt_gateway
;
1117 info
.rti_info
[RTAX_NETMASK
] = rt_mask(rt
);
1118 lck_mtx_lock(rt_mtx
);
1119 rt_missmsg(RTM_LOSING
, &info
, rt
->rt_flags
, 0);
1120 if (rt
->rt_flags
& RTF_DYNAMIC
)
1121 (void)rtrequest_locked(RTM_DELETE
, rt_key(rt
),
1122 rt
->rt_gateway
, rt_mask(rt
), rt
->rt_flags
,
1123 (struct rtentry
**)0);
1126 * A new route can be allocated
1127 * the next time output is attempted.
1130 lck_mtx_unlock(rt_mtx
);
1135 * After a routing change, flush old routing
1136 * and allocate a (hopefully) better one.
1143 if (inp
->in6p_route
.ro_rt
) {
1144 rtfree(inp
->in6p_route
.ro_rt
);
1145 inp
->in6p_route
.ro_rt
= 0;
1147 * A new route can be allocated the next time
1148 * output is attempted.
1154 * Lookup PCB in hash list.
1158 struct inpcbinfo
*pcbinfo
,
1159 struct in6_addr
*faddr
,
1161 struct in6_addr
*laddr
,
1166 struct inpcbhead
*head
;
1168 u_short fport
= fport_arg
, lport
= lport_arg
;
1171 #if defined(NFAITH) && NFAITH > 0
1172 faith
= faithprefix(laddr
);
1177 lck_rw_lock_shared(pcbinfo
->mtx
);
1180 * First look for an exact match.
1182 head
= &pcbinfo
->hashbase
[INP_PCBHASH(faddr
->s6_addr32
[3] /* XXX */,
1184 pcbinfo
->hashmask
)];
1185 LIST_FOREACH(inp
, head
, inp_hash
) {
1186 if ((inp
->inp_vflag
& INP_IPV6
) == 0)
1188 if (IN6_ARE_ADDR_EQUAL(&inp
->in6p_faddr
, faddr
) &&
1189 IN6_ARE_ADDR_EQUAL(&inp
->in6p_laddr
, laddr
) &&
1190 inp
->inp_fport
== fport
&&
1191 inp
->inp_lport
== lport
) {
1193 * Found. Check if pcb is still valid
1195 if (in_pcb_checkstate(inp
, WNT_ACQUIRE
, 0) != WNT_STOPUSING
) {
1196 lck_rw_done(pcbinfo
->mtx
);
1199 else { /* it's there but dead, say it isn't found */
1200 lck_rw_done(pcbinfo
->mtx
);
1206 struct inpcb
*local_wild
= NULL
;
1208 head
= &pcbinfo
->hashbase
[INP_PCBHASH(INADDR_ANY
, lport
, 0,
1209 pcbinfo
->hashmask
)];
1210 LIST_FOREACH(inp
, head
, inp_hash
) {
1211 if ((inp
->inp_vflag
& INP_IPV6
) == 0)
1213 if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_faddr
) &&
1214 inp
->inp_lport
== lport
) {
1215 if (faith
&& (inp
->inp_flags
& INP_FAITH
) == 0)
1217 if (IN6_ARE_ADDR_EQUAL(&inp
->in6p_laddr
,
1219 if (in_pcb_checkstate(inp
, WNT_ACQUIRE
, 0) != WNT_STOPUSING
) {
1220 lck_rw_done(pcbinfo
->mtx
);
1223 else { /* it's there but dead, say it isn't found */
1224 lck_rw_done(pcbinfo
->mtx
);
1228 else if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
))
1232 if (local_wild
&& in_pcb_checkstate(local_wild
, WNT_ACQUIRE
, 0) != WNT_STOPUSING
) {
1233 lck_rw_done(pcbinfo
->mtx
);
1234 return (local_wild
);
1237 lck_rw_done(pcbinfo
->mtx
);
1245 lck_rw_done(pcbinfo
->mtx
);
1250 init_sin6(struct sockaddr_in6
*sin6
, struct mbuf
*m
)
1254 ip
= mtod(m
, struct ip6_hdr
*);
1255 bzero(sin6
, sizeof(*sin6
));
1256 sin6
->sin6_len
= sizeof(*sin6
);
1257 sin6
->sin6_family
= AF_INET6
;
1258 sin6
->sin6_addr
= ip
->ip6_src
;
1259 if (IN6_IS_SCOPE_LINKLOCAL(&sin6
->sin6_addr
))
1260 sin6
->sin6_addr
.s6_addr16
[1] = 0;
1261 sin6
->sin6_scope_id
=
1262 (m
->m_pkthdr
.rcvif
&& IN6_IS_SCOPE_LINKLOCAL(&sin6
->sin6_addr
))
1263 ? m
->m_pkthdr
.rcvif
->if_index
: 0;