]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/mp_pcb.c
6aabe6b8dfba633e52144e5e744e3a92864db9cf
[apple/xnu.git] / bsd / netinet / mp_pcb.c
1 /*
2 * Copyright (c) 2012-2017 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
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 #include <sys/mbuf.h>
33 #include <sys/mcache.h>
34 #include <sys/syslog.h>
35 #include <sys/socket.h>
36 #include <sys/socketvar.h>
37 #include <sys/protosw.h>
38 #include <sys/proc_internal.h>
39
40 #include <mach/boolean.h>
41 #include <kern/zalloc.h>
42 #include <kern/locks.h>
43
44 #include <netinet/mp_pcb.h>
45 #include <netinet/mptcp_var.h>
46 #include <netinet6/in6_pcb.h>
47
48 static lck_grp_t *mp_lock_grp;
49 static lck_attr_t *mp_lock_attr;
50 static lck_grp_attr_t *mp_lock_grp_attr;
51 decl_lck_mtx_data(static, mp_lock); /* global MULTIPATH lock */
52 decl_lck_mtx_data(static, mp_timeout_lock);
53
54 static TAILQ_HEAD(, mppcbinfo) mppi_head = TAILQ_HEAD_INITIALIZER(mppi_head);
55
56 static boolean_t mp_timeout_run; /* MP timer is scheduled to run */
57 static boolean_t mp_garbage_collecting;
58 static boolean_t mp_ticking;
59 static void mp_sched_timeout(void);
60 static void mp_timeout(void *);
61
62 void
63 mp_pcbinit(void)
64 {
65 static int mp_initialized = 0;
66
67 VERIFY(!mp_initialized);
68 mp_initialized = 1;
69
70 mp_lock_grp_attr = lck_grp_attr_alloc_init();
71 mp_lock_grp = lck_grp_alloc_init("multipath", mp_lock_grp_attr);
72 mp_lock_attr = lck_attr_alloc_init();
73 lck_mtx_init(&mp_lock, mp_lock_grp, mp_lock_attr);
74 lck_mtx_init(&mp_timeout_lock, mp_lock_grp, mp_lock_attr);
75 }
76
77 static void
78 mp_timeout(void *arg)
79 {
80 #pragma unused(arg)
81 struct mppcbinfo *mppi;
82 boolean_t t, gc;
83 uint32_t t_act = 0;
84 uint32_t gc_act = 0;
85
86 /*
87 * Update coarse-grained networking timestamp (in sec.); the idea
88 * is to piggy-back on the timeout callout to update the counter
89 * returnable via net_uptime().
90 */
91 net_update_uptime();
92
93 lck_mtx_lock_spin(&mp_timeout_lock);
94 gc = mp_garbage_collecting;
95 mp_garbage_collecting = FALSE;
96
97 t = mp_ticking;
98 mp_ticking = FALSE;
99
100 if (gc || t) {
101 lck_mtx_unlock(&mp_timeout_lock);
102
103 lck_mtx_lock(&mp_lock);
104 TAILQ_FOREACH(mppi, &mppi_head, mppi_entry) {
105 if ((gc && mppi->mppi_gc != NULL) ||
106 (t && mppi->mppi_timer != NULL)) {
107 lck_mtx_lock(&mppi->mppi_lock);
108 if (gc && mppi->mppi_gc != NULL) {
109 gc_act += mppi->mppi_gc(mppi);
110 }
111 if (t && mppi->mppi_timer != NULL) {
112 t_act += mppi->mppi_timer(mppi);
113 }
114 lck_mtx_unlock(&mppi->mppi_lock);
115 }
116 }
117 lck_mtx_unlock(&mp_lock);
118
119 lck_mtx_lock_spin(&mp_timeout_lock);
120 }
121
122 /* lock was dropped above, so check first before overriding */
123 if (!mp_garbage_collecting) {
124 mp_garbage_collecting = (gc_act != 0);
125 }
126 if (!mp_ticking) {
127 mp_ticking = (t_act != 0);
128 }
129
130 /* re-arm the timer if there's work to do */
131 mp_timeout_run = FALSE;
132 mp_sched_timeout();
133 lck_mtx_unlock(&mp_timeout_lock);
134 }
135
136 static void
137 mp_sched_timeout(void)
138 {
139 LCK_MTX_ASSERT(&mp_timeout_lock, LCK_MTX_ASSERT_OWNED);
140
141 if (!mp_timeout_run && (mp_garbage_collecting || mp_ticking)) {
142 lck_mtx_convert_spin(&mp_timeout_lock);
143 mp_timeout_run = TRUE;
144 timeout(mp_timeout, NULL, hz);
145 }
146 }
147
148 void
149 mp_gc_sched(void)
150 {
151 lck_mtx_lock_spin(&mp_timeout_lock);
152 mp_garbage_collecting = TRUE;
153 mp_sched_timeout();
154 lck_mtx_unlock(&mp_timeout_lock);
155 }
156
157 void
158 mptcp_timer_sched(void)
159 {
160 lck_mtx_lock_spin(&mp_timeout_lock);
161 mp_ticking = TRUE;
162 mp_sched_timeout();
163 lck_mtx_unlock(&mp_timeout_lock);
164 }
165
166 void
167 mp_pcbinfo_attach(struct mppcbinfo *mppi)
168 {
169 struct mppcbinfo *mppi0;
170
171 lck_mtx_lock(&mp_lock);
172 TAILQ_FOREACH(mppi0, &mppi_head, mppi_entry) {
173 if (mppi0 == mppi) {
174 panic("%s: mppi %p already in the list\n",
175 __func__, mppi);
176 /* NOTREACHED */
177 }
178 }
179 TAILQ_INSERT_TAIL(&mppi_head, mppi, mppi_entry);
180 lck_mtx_unlock(&mp_lock);
181 }
182
183 int
184 mp_pcbinfo_detach(struct mppcbinfo *mppi)
185 {
186 struct mppcbinfo *mppi0;
187 int error = 0;
188
189 lck_mtx_lock(&mp_lock);
190 TAILQ_FOREACH(mppi0, &mppi_head, mppi_entry) {
191 if (mppi0 == mppi) {
192 break;
193 }
194 }
195 if (mppi0 != NULL) {
196 TAILQ_REMOVE(&mppi_head, mppi0, mppi_entry);
197 } else {
198 error = ENXIO;
199 }
200 lck_mtx_unlock(&mp_lock);
201
202 return error;
203 }
204
205 int
206 mp_pcballoc(struct socket *so, struct mppcbinfo *mppi)
207 {
208 struct mppcb *mpp = NULL;
209 int error;
210
211 VERIFY(mpsotomppcb(so) == NULL);
212
213 mpp = zalloc(mppi->mppi_zone);
214 if (mpp == NULL) {
215 return ENOBUFS;
216 }
217
218 bzero(mpp, mppi->mppi_size);
219 lck_mtx_init(&mpp->mpp_lock, mppi->mppi_lock_grp, mppi->mppi_lock_attr);
220 mpp->mpp_pcbinfo = mppi;
221 mpp->mpp_state = MPPCB_STATE_INUSE;
222 mpp->mpp_socket = so;
223 so->so_pcb = mpp;
224
225 error = mptcp_sescreate(mpp);
226 if (error) {
227 lck_mtx_destroy(&mpp->mpp_lock, mppi->mppi_lock_grp);
228 zfree(mppi->mppi_zone, mpp);
229 return error;
230 }
231
232 lck_mtx_lock(&mppi->mppi_lock);
233 mpp->mpp_flags |= MPP_ATTACHED;
234 TAILQ_INSERT_TAIL(&mppi->mppi_pcbs, mpp, mpp_entry);
235 mppi->mppi_count++;
236 lck_mtx_unlock(&mppi->mppi_lock);
237
238 return 0;
239 }
240
241 void
242 mp_pcbdetach(struct socket *mp_so)
243 {
244 struct mppcb *mpp = mpsotomppcb(mp_so);
245
246 mpp->mpp_state = MPPCB_STATE_DEAD;
247 if (!(mp_so->so_flags & SOF_PCBCLEARING)) {
248 mp_so->so_flags |= SOF_PCBCLEARING;
249 }
250
251 mp_gc_sched();
252 }
253
254 void
255 mp_pcbdispose(struct mppcb *mpp)
256 {
257 struct mppcbinfo *mppi = mpp->mpp_pcbinfo;
258
259 VERIFY(mppi != NULL);
260
261 LCK_MTX_ASSERT(&mppi->mppi_lock, LCK_MTX_ASSERT_OWNED);
262 mpp_lock_assert_held(mpp);
263
264 VERIFY(mpp->mpp_state == MPPCB_STATE_DEAD);
265 VERIFY(mpp->mpp_flags & MPP_ATTACHED);
266
267 mpp->mpp_flags &= ~MPP_ATTACHED;
268 TAILQ_REMOVE(&mppi->mppi_pcbs, mpp, mpp_entry);
269 VERIFY(mppi->mppi_count != 0);
270 mppi->mppi_count--;
271
272 mpp_unlock(mpp);
273
274 #if NECP
275 necp_mppcb_dispose(mpp);
276 #endif /* NECP */
277
278 lck_mtx_destroy(&mpp->mpp_lock, mppi->mppi_lock_grp);
279
280 VERIFY(mpp->mpp_socket != NULL);
281 VERIFY(mpp->mpp_socket->so_usecount == 0);
282 mpp->mpp_socket->so_pcb = NULL;
283 mpp->mpp_socket = NULL;
284
285 zfree(mppi->mppi_zone, mpp);
286 }
287
288 static int
289 mp_getaddr_v4(struct socket *mp_so, struct sockaddr **nam, boolean_t peer)
290 {
291 struct mptses *mpte = mpsotompte(mp_so);
292 struct sockaddr_in *sin;
293
294 /*
295 * Do the malloc first in case it blocks.
296 */
297 MALLOC(sin, struct sockaddr_in *, sizeof(*sin), M_SONAME, M_WAITOK);
298 if (sin == NULL) {
299 return ENOBUFS;
300 }
301 bzero(sin, sizeof(*sin));
302 sin->sin_family = AF_INET;
303 sin->sin_len = sizeof(*sin);
304
305 if (!peer) {
306 sin->sin_port = mpte->__mpte_src_v4.sin_port;
307 sin->sin_addr = mpte->__mpte_src_v4.sin_addr;
308 } else {
309 sin->sin_port = mpte->__mpte_dst_v4.sin_port;
310 sin->sin_addr = mpte->__mpte_dst_v4.sin_addr;
311 }
312
313 *nam = (struct sockaddr *)sin;
314 return 0;
315 }
316
317 static int
318 mp_getaddr_v6(struct socket *mp_so, struct sockaddr **nam, boolean_t peer)
319 {
320 struct mptses *mpte = mpsotompte(mp_so);
321 struct in6_addr addr;
322 in_port_t port;
323
324 if (!peer) {
325 port = mpte->__mpte_src_v6.sin6_port;
326 addr = mpte->__mpte_src_v6.sin6_addr;
327 } else {
328 port = mpte->__mpte_dst_v6.sin6_port;
329 addr = mpte->__mpte_dst_v6.sin6_addr;
330 }
331
332 *nam = in6_sockaddr(port, &addr);
333 if (*nam == NULL) {
334 return ENOBUFS;
335 }
336
337 return 0;
338 }
339
340 int
341 mp_getsockaddr(struct socket *mp_so, struct sockaddr **nam)
342 {
343 struct mptses *mpte = mpsotompte(mp_so);
344
345 if (mpte->mpte_src.sa_family == AF_INET || mpte->mpte_src.sa_family == 0) {
346 return mp_getaddr_v4(mp_so, nam, false);
347 } else if (mpte->mpte_src.sa_family == AF_INET6) {
348 return mp_getaddr_v6(mp_so, nam, false);
349 } else {
350 return EINVAL;
351 }
352 }
353
354 int
355 mp_getpeeraddr(struct socket *mp_so, struct sockaddr **nam)
356 {
357 struct mptses *mpte = mpsotompte(mp_so);
358
359 if (mpte->mpte_src.sa_family == AF_INET || mpte->mpte_src.sa_family == 0) {
360 return mp_getaddr_v4(mp_so, nam, true);
361 } else if (mpte->mpte_src.sa_family == AF_INET6) {
362 return mp_getaddr_v6(mp_so, nam, true);
363 } else {
364 return EINVAL;
365 }
366 }