1 /*
2 * Copyright (c) 2020 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 #include <kern/zalloc.h>
29 #include <net/if.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>
39
40 #define NDRTI_ZONE_NAME "nd6_route_info" /* zone name */
41
42 static struct nd_route_info *nd6_rti_lookup(struct nd_route_info *);
43
44 static KALLOC_TYPE_DEFINE(ndrti_zone, struct nd_route_info, NET_KT_DEFAULT);
45
46 static boolean_t nd6_rti_list_busy = FALSE; /* protected by nd6_mutex */
47
48
49 void
nd6_rti_list_wait(const char * func)50 nd6_rti_list_wait(const char *func)
51 {
52 LCK_MTX_ASSERT(nd6_mutex, LCK_MTX_ASSERT_OWNED);
53 while (nd6_rti_list_busy) {
54 nd6log2(debug, "%s: someone else is operating "
55 "on rti list. Entering sleep.\n", func);
56 (void) msleep(&nd6_rti_list_busy, nd6_mutex, (PZERO - 1),
57 func, NULL);
58 LCK_MTX_ASSERT(nd6_mutex, LCK_MTX_ASSERT_OWNED);
59 }
60 nd6_rti_list_busy = TRUE;
61 }
62
63 void
nd6_rti_list_signal_done(void)64 nd6_rti_list_signal_done(void)
65 {
66 LCK_MTX_ASSERT(nd6_mutex, LCK_MTX_ASSERT_OWNED);
67 nd6_rti_list_busy = FALSE;
68 wakeup(&nd6_rti_list_busy);
69 }
70
71 struct nd_route_info *
ndrti_alloc(void)72 ndrti_alloc(void)
73 {
74 return zalloc_flags(ndrti_zone, Z_WAITOK | Z_ZERO);
75 }
76
77 void
ndrti_free(struct nd_route_info * rti)78 ndrti_free(struct nd_route_info *rti)
79 {
80 if (!TAILQ_EMPTY(&rti->nd_rti_router_list)) {
81 panic("%s: rti freed with non-empty router list", __func__);
82 }
83 zfree(ndrti_zone, rti);
84 }
85
86 static struct nd_route_info *
nd6_rti_lookup(struct nd_route_info * rti)87 nd6_rti_lookup(struct nd_route_info *rti)
88 {
89 struct nd_route_info *__single tmp_rti = NULL;
90
91 LCK_MTX_ASSERT(nd6_mutex, LCK_MTX_ASSERT_OWNED);
92
93 TAILQ_FOREACH(tmp_rti, &nd_rti_list, nd_rti_entry) {
94 if (IN6_ARE_ADDR_EQUAL(&tmp_rti->nd_rti_prefix, &rti->nd_rti_prefix) &&
95 tmp_rti->nd_rti_prefixlen == rti->nd_rti_prefixlen) {
96 break;
97 }
98 }
99 return tmp_rti;
100 }
101
102 void
nd6_rtilist_update(struct nd_route_info * new_rti,struct nd_defrouter * dr)103 nd6_rtilist_update(struct nd_route_info *new_rti, struct nd_defrouter *dr)
104 {
105 struct nd_route_info *__single rti = NULL;
106
107 lck_mtx_lock(nd6_mutex);
108 VERIFY(new_rti != NULL && dr != NULL);
109 nd6_rti_list_wait(__func__);
110
111 if ((rti = nd6_rti_lookup(new_rti)) != NULL) {
112 (void)defrtrlist_update(dr, &rti->nd_rti_router_list);
113 /*
114 * The above may have removed an entry from default router list.
115 * If it did and the list is now empty, remove the rti as well.
116 */
117 if (TAILQ_EMPTY(&rti->nd_rti_router_list)) {
118 TAILQ_REMOVE(&nd_rti_list, rti, nd_rti_entry);
119 ndrti_free(rti);
120 }
121 } else if (dr->rtlifetime != 0) {
122 rti = ndrti_alloc();
123 TAILQ_INIT(&rti->nd_rti_router_list);
124 rti->nd_rti_prefix = new_rti->nd_rti_prefix;
125 rti->nd_rti_prefixlen = new_rti->nd_rti_prefixlen;
126 (void)defrtrlist_update(dr, &rti->nd_rti_router_list);
127 TAILQ_INSERT_HEAD(&nd_rti_list, rti, nd_rti_entry);
128 }
129 /* If rti doesn't exist and lifetime is 0, simply ignore */
130 nd6_rti_list_signal_done();
131 lck_mtx_unlock(nd6_mutex);
132 }
133
134 void
nd6_rti_purge(struct nd_route_info * new_rti)135 nd6_rti_purge(struct nd_route_info *new_rti)
136 {
137 VERIFY(new_rti != NULL);
138 LCK_MTX_ASSERT(nd6_mutex, LCK_MTX_ASSERT_OWNED);
139
140 struct nd_route_info *__single rti = NULL;
141 nd6_rti_list_wait(__func__);
142
143 if ((rti = nd6_rti_lookup(new_rti)) != NULL) {
144 struct nd_defrouter *dr = NULL;
145 struct nd_defrouter *ndr = NULL;
146
147 TAILQ_FOREACH_SAFE(dr, &rti->nd_rti_router_list, dr_entry, ndr) {
148 TAILQ_REMOVE(&rti->nd_rti_router_list, dr, dr_entry);
149 defrtrlist_del(dr, &rti->nd_rti_router_list);
150 NDDR_REMREF(dr);
151 }
152 TAILQ_REMOVE(&nd_rti_list, rti, nd_rti_entry);
153 ndrti_free(rti);
154 }
155 nd6_rti_list_signal_done();
156 }
157
158 void
nd6_rti_delreq(struct nd_route_info * rti)159 nd6_rti_delreq(struct nd_route_info *rti)
160 {
161 VERIFY(rti != NULL);
162 LCK_MTX_ASSERT(nd6_mutex, LCK_MTX_ASSERT_OWNED);
163
164 struct nd_route_info *__single ret_rti = NULL;
165 nd6_rti_list_wait(__func__);
166
167 if ((ret_rti = nd6_rti_lookup(rti)) != NULL) {
168 struct nd_defrouter *__single dr = NULL;
169 struct nd_defrouter *__single ndr = NULL;
170
171 TAILQ_FOREACH_SAFE(dr, &ret_rti->nd_rti_router_list, dr_entry, ndr) {
172 if (dr->stateflags & NDDRF_INSTALLED) {
173 lck_mtx_unlock(nd6_mutex);
174 defrouter_delreq(dr, ret_rti);
175 lck_mtx_lock(nd6_mutex);
176 break;
177 }
178 }
179 }
180 nd6_rti_list_signal_done();
181 }
182
183 void
nd6_rti_select(struct nd_route_info * rti,struct ifnet * ifp)184 nd6_rti_select(struct nd_route_info *rti, struct ifnet *ifp)
185 {
186 VERIFY(rti != NULL);
187 VERIFY(ifp != NULL);
188 LCK_MTX_ASSERT(nd6_mutex, LCK_MTX_ASSERT_OWNED);
189
190 struct nd_route_info *__single ret_rti = NULL;
191 nd6_rti_list_wait(__func__);
192
193 if ((ret_rti = nd6_rti_lookup(rti)) != NULL) {
194 defrouter_select(ifp, &ret_rti->nd_rti_router_list);
195 }
196 nd6_rti_list_signal_done();
197 }
198