xref: /xnu-8792.61.2/bsd/netinet6/nd6_rti.c (revision 42e220869062b56f8d7d0726fd4c88954f87902c)
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 ZONE_DEFINE(ndrti_zone, "nd6_route_info",
45     sizeof(struct nd_route_info), ZC_ZFREE_CLEARMEM);
46 
47 static boolean_t nd6_rti_list_busy = FALSE;             /* protected by nd6_mutex */
48 
49 
50 void
nd6_rti_list_wait(const char * func)51 nd6_rti_list_wait(const char *func)
52 {
53 	LCK_MTX_ASSERT(nd6_mutex, LCK_MTX_ASSERT_OWNED);
54 	while (nd6_rti_list_busy) {
55 		nd6log2(debug, "%s: someone else is operating "
56 		    "on rti list. Entering sleep.\n", func);
57 		(void) msleep(&nd6_rti_list_busy, nd6_mutex, (PZERO - 1),
58 		    func, NULL);
59 		LCK_MTX_ASSERT(nd6_mutex, LCK_MTX_ASSERT_OWNED);
60 	}
61 	nd6_rti_list_busy = TRUE;
62 }
63 
64 void
nd6_rti_list_signal_done(void)65 nd6_rti_list_signal_done(void)
66 {
67 	LCK_MTX_ASSERT(nd6_mutex, LCK_MTX_ASSERT_OWNED);
68 	nd6_rti_list_busy = FALSE;
69 	wakeup(&nd6_rti_list_busy);
70 }
71 
72 struct nd_route_info *
ndrti_alloc(void)73 ndrti_alloc(void)
74 {
75 	return zalloc_flags(ndrti_zone, Z_WAITOK | Z_ZERO);
76 }
77 
78 void
ndrti_free(struct nd_route_info * rti)79 ndrti_free(struct nd_route_info *rti)
80 {
81 	if (!TAILQ_EMPTY(&rti->nd_rti_router_list)) {
82 		panic("%s: rti freed with non-empty router list", __func__);
83 	}
84 	zfree(ndrti_zone, rti);
85 }
86 
87 static struct nd_route_info *
nd6_rti_lookup(struct nd_route_info * rti)88 nd6_rti_lookup(struct nd_route_info *rti)
89 {
90 	struct nd_route_info *tmp_rti = NULL;
91 
92 	LCK_MTX_ASSERT(nd6_mutex, LCK_MTX_ASSERT_OWNED);
93 
94 	TAILQ_FOREACH(tmp_rti, &nd_rti_list, nd_rti_entry) {
95 		if (IN6_ARE_ADDR_EQUAL(&tmp_rti->nd_rti_prefix, &rti->nd_rti_prefix) &&
96 		    tmp_rti->nd_rti_prefixlen == rti->nd_rti_prefixlen) {
97 			break;
98 		}
99 	}
100 	return tmp_rti;
101 }
102 
103 void
nd6_rtilist_update(struct nd_route_info * new_rti,struct nd_defrouter * dr)104 nd6_rtilist_update(struct nd_route_info *new_rti, struct nd_defrouter *dr)
105 {
106 	struct nd_route_info *rti = NULL;
107 
108 	lck_mtx_lock(nd6_mutex);
109 	VERIFY(new_rti != NULL && dr != NULL);
110 	nd6_rti_list_wait(__func__);
111 
112 	if ((rti = nd6_rti_lookup(new_rti)) != NULL) {
113 		(void)defrtrlist_update(dr, &rti->nd_rti_router_list);
114 		/*
115 		 * The above may have removed an entry from default router list.
116 		 * If it did and the list is now empty, remove the rti as well.
117 		 */
118 		if (TAILQ_EMPTY(&rti->nd_rti_router_list)) {
119 			TAILQ_REMOVE(&nd_rti_list, rti, nd_rti_entry);
120 			ndrti_free(rti);
121 		}
122 	} else if (dr->rtlifetime != 0) {
123 		rti = ndrti_alloc();
124 		TAILQ_INIT(&rti->nd_rti_router_list);
125 		rti->nd_rti_prefix = new_rti->nd_rti_prefix;
126 		rti->nd_rti_prefixlen = new_rti->nd_rti_prefixlen;
127 		(void)defrtrlist_update(dr, &rti->nd_rti_router_list);
128 		TAILQ_INSERT_HEAD(&nd_rti_list, rti, nd_rti_entry);
129 	}
130 	/* If rti doesn't exist and lifetime is 0, simply ignore */
131 	nd6_rti_list_signal_done();
132 	lck_mtx_unlock(nd6_mutex);
133 }
134 
135 void
nd6_rti_purge(struct nd_route_info * new_rti)136 nd6_rti_purge(struct nd_route_info *new_rti)
137 {
138 	VERIFY(new_rti != NULL);
139 	LCK_MTX_ASSERT(nd6_mutex, LCK_MTX_ASSERT_OWNED);
140 
141 	struct nd_route_info *rti = NULL;
142 	nd6_rti_list_wait(__func__);
143 
144 	if ((rti = nd6_rti_lookup(new_rti)) != NULL) {
145 		struct nd_defrouter *dr = NULL;
146 		struct nd_defrouter *ndr = NULL;
147 
148 		TAILQ_FOREACH_SAFE(dr, &rti->nd_rti_router_list, dr_entry, ndr) {
149 			TAILQ_REMOVE(&rti->nd_rti_router_list, dr, dr_entry);
150 			defrtrlist_del(dr, &rti->nd_rti_router_list);
151 			NDDR_REMREF(dr);
152 		}
153 		TAILQ_REMOVE(&nd_rti_list, rti, nd_rti_entry);
154 		ndrti_free(rti);
155 	}
156 	nd6_rti_list_signal_done();
157 }
158