]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet6/nd6_rti.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / bsd / netinet6 / nd6_rti.c
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 extern lck_mtx_t *nd6_mutex;
43 static struct nd_route_info *nd6_rti_lookup(struct nd_route_info *);
44
45 static ZONE_DECLARE(ndrti_zone, "nd6_route_info",
46 sizeof(struct nd_route_info), ZC_ZFREE_CLEARMEM);
47
48 static boolean_t nd6_rti_list_busy = FALSE; /* protected by nd6_mutex */
49
50
51 void
52 nd6_rti_list_wait(const char *func)
53 {
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),
59 func, NULL);
60 LCK_MTX_ASSERT(nd6_mutex, LCK_MTX_ASSERT_OWNED);
61 }
62 nd6_rti_list_busy = TRUE;
63 }
64
65 void
66 nd6_rti_list_signal_done(void)
67 {
68 LCK_MTX_ASSERT(nd6_mutex, LCK_MTX_ASSERT_OWNED);
69 nd6_rti_list_busy = FALSE;
70 wakeup(&nd6_rti_list_busy);
71 }
72
73 struct nd_route_info *
74 ndrti_alloc(void)
75 {
76 return zalloc_flags(ndrti_zone, Z_WAITOK | Z_ZERO);
77 }
78
79 void
80 ndrti_free(struct nd_route_info *rti)
81 {
82 if (!TAILQ_EMPTY(&rti->nd_rti_router_list)) {
83 panic("%s: rti freed with non-empty router list", __func__);
84 }
85 zfree(ndrti_zone, rti);
86 }
87
88 static struct nd_route_info *
89 nd6_rti_lookup(struct nd_route_info *rti)
90 {
91 struct nd_route_info *tmp_rti = NULL;
92
93 LCK_MTX_ASSERT(nd6_mutex, LCK_MTX_ASSERT_OWNED);
94
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) {
98 break;
99 }
100 }
101 return tmp_rti;
102 }
103
104 void
105 nd6_rtilist_update(struct nd_route_info *new_rti, struct nd_defrouter *dr)
106 {
107 struct nd_route_info *rti = NULL;
108
109 lck_mtx_lock(nd6_mutex);
110 VERIFY(new_rti != NULL && dr != NULL);
111 nd6_rti_list_wait(__func__);
112
113 if ((rti = nd6_rti_lookup(new_rti)) != NULL) {
114 (void)defrtrlist_update(dr, &rti->nd_rti_router_list);
115 /*
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.
118 */
119 if (TAILQ_EMPTY(&rti->nd_rti_router_list)) {
120 TAILQ_REMOVE(&nd_rti_list, rti, nd_rti_entry);
121 ndrti_free(rti);
122 }
123 } else if (dr->rtlifetime != 0) {
124 rti = ndrti_alloc();
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);
130 }
131 /* If rti doesn't exist and lifetime is 0, simply ignore */
132 nd6_rti_list_signal_done();
133 lck_mtx_unlock(nd6_mutex);
134 }
135
136 void
137 nd6_rti_purge(struct nd_route_info *new_rti)
138 {
139 VERIFY(new_rti != NULL);
140 LCK_MTX_ASSERT(nd6_mutex, LCK_MTX_ASSERT_OWNED);
141
142 struct nd_route_info *rti = NULL;
143 nd6_rti_list_wait(__func__);
144
145 if ((rti = nd6_rti_lookup(new_rti)) != NULL) {
146 struct nd_defrouter *dr = NULL;
147 struct nd_defrouter *ndr = NULL;
148
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);
152 NDDR_REMREF(dr);
153 }
154 TAILQ_REMOVE(&nd_rti_list, rti, nd_rti_entry);
155 ndrti_free(rti);
156 }
157 nd6_rti_list_signal_done();
158 }