2 * Copyright (c) 2020 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 #include <kern/zalloc.h>
30 #include <net/if_var.h>
31 #include <netinet/in.h>
32 #include <netinet6/in6_var.h>
33 #include <netinet6/in6_ifattach.h>
34 #include <netinet/ip6.h>
35 #include <netinet6/ip6_var.h>
36 #include <netinet6/nd6.h>
37 #include <netinet6/scope6_var.h>
38 #include <sys/mcache.h>
40 #define NDRTI_ZONE_NAME "nd6_route_info" /* zone name */
42 extern lck_mtx_t
*nd6_mutex
;
43 static struct nd_route_info
*nd6_rti_lookup(struct nd_route_info
*);
45 static ZONE_DECLARE(ndrti_zone
, "nd6_route_info",
46 sizeof(struct nd_route_info
), ZC_ZFREE_CLEARMEM
);
48 static boolean_t nd6_rti_list_busy
= FALSE
; /* protected by nd6_mutex */
52 nd6_rti_list_wait(const char *func
)
54 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
55 while (nd6_rti_list_busy
) {
56 nd6log2(debug
, "%s: someone else is operating "
57 "on rti list. Entering sleep.\n", func
);
58 (void) msleep(&nd6_rti_list_busy
, nd6_mutex
, (PZERO
- 1),
60 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
62 nd6_rti_list_busy
= TRUE
;
66 nd6_rti_list_signal_done(void)
68 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
69 nd6_rti_list_busy
= FALSE
;
70 wakeup(&nd6_rti_list_busy
);
73 struct nd_route_info
*
76 return zalloc_flags(ndrti_zone
, Z_WAITOK
| Z_ZERO
);
80 ndrti_free(struct nd_route_info
*rti
)
82 if (!TAILQ_EMPTY(&rti
->nd_rti_router_list
)) {
83 panic("%s: rti freed with non-empty router list", __func__
);
85 zfree(ndrti_zone
, rti
);
88 static struct nd_route_info
*
89 nd6_rti_lookup(struct nd_route_info
*rti
)
91 struct nd_route_info
*tmp_rti
= NULL
;
93 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
95 TAILQ_FOREACH(tmp_rti
, &nd_rti_list
, nd_rti_entry
) {
96 if (IN6_ARE_ADDR_EQUAL(&tmp_rti
->nd_rti_prefix
, &rti
->nd_rti_prefix
) &&
97 tmp_rti
->nd_rti_prefixlen
== rti
->nd_rti_prefixlen
) {
105 nd6_rtilist_update(struct nd_route_info
*new_rti
, struct nd_defrouter
*dr
)
107 struct nd_route_info
*rti
= NULL
;
109 lck_mtx_lock(nd6_mutex
);
110 VERIFY(new_rti
!= NULL
&& dr
!= NULL
);
111 nd6_rti_list_wait(__func__
);
113 if ((rti
= nd6_rti_lookup(new_rti
)) != NULL
) {
114 (void)defrtrlist_update(dr
, &rti
->nd_rti_router_list
);
116 * The above may have removed an entry from default router list.
117 * If it did and the list is now empty, remove the rti as well.
119 if (TAILQ_EMPTY(&rti
->nd_rti_router_list
)) {
120 TAILQ_REMOVE(&nd_rti_list
, rti
, nd_rti_entry
);
123 } else if (dr
->rtlifetime
!= 0) {
125 TAILQ_INIT(&rti
->nd_rti_router_list
);
126 rti
->nd_rti_prefix
= new_rti
->nd_rti_prefix
;
127 rti
->nd_rti_prefixlen
= new_rti
->nd_rti_prefixlen
;
128 (void)defrtrlist_update(dr
, &rti
->nd_rti_router_list
);
129 TAILQ_INSERT_HEAD(&nd_rti_list
, rti
, nd_rti_entry
);
131 /* If rti doesn't exist and lifetime is 0, simply ignore */
132 nd6_rti_list_signal_done();
133 lck_mtx_unlock(nd6_mutex
);
137 nd6_rti_purge(struct nd_route_info
*new_rti
)
139 VERIFY(new_rti
!= NULL
);
140 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_OWNED
);
142 struct nd_route_info
*rti
= NULL
;
143 nd6_rti_list_wait(__func__
);
145 if ((rti
= nd6_rti_lookup(new_rti
)) != NULL
) {
146 struct nd_defrouter
*dr
= NULL
;
147 struct nd_defrouter
*ndr
= NULL
;
149 TAILQ_FOREACH_SAFE(dr
, &rti
->nd_rti_router_list
, dr_entry
, ndr
) {
150 TAILQ_REMOVE(&rti
->nd_rti_router_list
, dr
, dr_entry
);
151 defrtrlist_del(dr
, &rti
->nd_rti_router_list
);
154 TAILQ_REMOVE(&nd_rti_list
, rti
, nd_rti_entry
);
157 nd6_rti_list_signal_done();