1 | /* |
2 | * Copyright (c) 1999-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 | /* |
30 | * Kernel Control domain - allows control connections to |
31 | * and to read/write data. |
32 | * |
33 | * Vincent Lubet, 040506 |
34 | * Christophe Allie, 010928 |
35 | * Justin C. Walker, 990319 |
36 | */ |
37 | |
38 | #include <sys/types.h> |
39 | #include <sys/param.h> |
40 | #include <sys/systm.h> |
41 | #include <sys/syslog.h> |
42 | #include <sys/socket.h> |
43 | #include <sys/socketvar.h> |
44 | #include <sys/protosw.h> |
45 | #include <sys/domain.h> |
46 | #include <sys/malloc.h> |
47 | #include <sys/mbuf.h> |
48 | #include <sys/sys_domain.h> |
49 | #include <sys/kern_event.h> |
50 | #include <sys/kern_control.h> |
51 | #include <sys/kauth.h> |
52 | #include <sys/sysctl.h> |
53 | #include <sys/proc_info.h> |
54 | #include <net/if_var.h> |
55 | |
56 | #include <mach/vm_types.h> |
57 | |
58 | #include <kern/thread.h> |
59 | |
60 | struct kctl { |
61 | TAILQ_ENTRY(kctl) next; /* controller chain */ |
62 | kern_ctl_ref kctlref; |
63 | |
64 | /* controller information provided when registering */ |
65 | char name[MAX_KCTL_NAME]; /* unique identifier */ |
66 | u_int32_t id; |
67 | u_int32_t reg_unit; |
68 | |
69 | /* misc communication information */ |
70 | u_int32_t flags; /* support flags */ |
71 | u_int32_t recvbufsize; /* request more than the default buffer size */ |
72 | u_int32_t sendbufsize; /* request more than the default buffer size */ |
73 | |
74 | /* Dispatch functions */ |
75 | ctl_bind_func bind; /* Prepare contact */ |
76 | ctl_connect_func connect; /* Make contact */ |
77 | ctl_disconnect_func disconnect; /* Break contact */ |
78 | ctl_send_func send; /* Send data to nke */ |
79 | ctl_send_list_func send_list; /* Send list of packets */ |
80 | ctl_setopt_func setopt; /* set kctl configuration */ |
81 | ctl_getopt_func getopt; /* get kctl configuration */ |
82 | ctl_rcvd_func rcvd; /* Notify nke when client reads data */ |
83 | |
84 | TAILQ_HEAD(, ctl_cb) kcb_head; |
85 | u_int32_t lastunit; |
86 | }; |
87 | |
88 | struct ctl_cb { |
89 | TAILQ_ENTRY(ctl_cb) next; /* controller chain */ |
90 | lck_mtx_t *mtx; |
91 | struct socket *so; /* controlling socket */ |
92 | struct kctl *kctl; /* back pointer to controller */ |
93 | void *userdata; |
94 | struct sockaddr_ctl sac; |
95 | u_int32_t usecount; |
96 | }; |
97 | |
98 | #ifndef ROUNDUP64 |
99 | #define ROUNDUP64(x) P2ROUNDUP((x), sizeof (u_int64_t)) |
100 | #endif |
101 | |
102 | #ifndef ADVANCE64 |
103 | #define ADVANCE64(p, n) (void*)((char *)(p) + ROUNDUP64(n)) |
104 | #endif |
105 | |
106 | /* |
107 | * Definitions and vars for we support |
108 | */ |
109 | |
110 | #define CTL_SENDSIZE (2 * 1024) /* default buffer size */ |
111 | #define CTL_RECVSIZE (8 * 1024) /* default buffer size */ |
112 | |
113 | /* |
114 | * Definitions and vars for we support |
115 | */ |
116 | |
117 | static u_int32_t ctl_maxunit = 65536; |
118 | static lck_grp_attr_t *ctl_lck_grp_attr = 0; |
119 | static lck_attr_t *ctl_lck_attr = 0; |
120 | static lck_grp_t *ctl_lck_grp = 0; |
121 | static lck_mtx_t *ctl_mtx; |
122 | |
123 | /* all the controllers are chained */ |
124 | TAILQ_HEAD(kctl_list, kctl) ctl_head; |
125 | |
126 | static int ctl_attach(struct socket *, int, struct proc *); |
127 | static int ctl_detach(struct socket *); |
128 | static int ctl_sofreelastref(struct socket *so); |
129 | static int ctl_bind(struct socket *, struct sockaddr *, struct proc *); |
130 | static int ctl_connect(struct socket *, struct sockaddr *, struct proc *); |
131 | static int ctl_disconnect(struct socket *); |
132 | static int ctl_ioctl(struct socket *so, u_long cmd, caddr_t data, |
133 | struct ifnet *ifp, struct proc *p); |
134 | static int ctl_send(struct socket *, int, struct mbuf *, |
135 | struct sockaddr *, struct mbuf *, struct proc *); |
136 | static int ctl_send_list(struct socket *, int, struct mbuf *, |
137 | struct sockaddr *, struct mbuf *, struct proc *); |
138 | static int ctl_ctloutput(struct socket *, struct sockopt *); |
139 | static int ctl_peeraddr(struct socket *so, struct sockaddr **nam); |
140 | static int ctl_usr_rcvd(struct socket *so, int flags); |
141 | |
142 | static struct kctl *ctl_find_by_name(const char *); |
143 | static struct kctl *ctl_find_by_id_unit(u_int32_t id, u_int32_t unit); |
144 | |
145 | static struct socket *kcb_find_socket(kern_ctl_ref kctlref, u_int32_t unit, |
146 | u_int32_t *); |
147 | static struct ctl_cb *kcb_find(struct kctl *, u_int32_t unit); |
148 | static void ctl_post_msg(u_int32_t event_code, u_int32_t id); |
149 | |
150 | static int ctl_lock(struct socket *, int, void *); |
151 | static int ctl_unlock(struct socket *, int, void *); |
152 | static lck_mtx_t * ctl_getlock(struct socket *, int); |
153 | |
154 | static struct pr_usrreqs ctl_usrreqs = { |
155 | .pru_attach = ctl_attach, |
156 | .pru_bind = ctl_bind, |
157 | .pru_connect = ctl_connect, |
158 | .pru_control = ctl_ioctl, |
159 | .pru_detach = ctl_detach, |
160 | .pru_disconnect = ctl_disconnect, |
161 | .pru_peeraddr = ctl_peeraddr, |
162 | .pru_rcvd = ctl_usr_rcvd, |
163 | .pru_send = ctl_send, |
164 | .pru_send_list = ctl_send_list, |
165 | .pru_sosend = sosend, |
166 | .pru_sosend_list = sosend_list, |
167 | .pru_soreceive = soreceive, |
168 | .pru_soreceive_list = soreceive_list, |
169 | }; |
170 | |
171 | static struct protosw kctlsw[] = { |
172 | { |
173 | .pr_type = SOCK_DGRAM, |
174 | .pr_protocol = SYSPROTO_CONTROL, |
175 | .pr_flags = PR_ATOMIC|PR_CONNREQUIRED|PR_PCBLOCK|PR_WANTRCVD, |
176 | .pr_ctloutput = ctl_ctloutput, |
177 | .pr_usrreqs = &ctl_usrreqs, |
178 | .pr_lock = ctl_lock, |
179 | .pr_unlock = ctl_unlock, |
180 | .pr_getlock = ctl_getlock, |
181 | }, |
182 | { |
183 | .pr_type = SOCK_STREAM, |
184 | .pr_protocol = SYSPROTO_CONTROL, |
185 | .pr_flags = PR_CONNREQUIRED|PR_PCBLOCK|PR_WANTRCVD, |
186 | .pr_ctloutput = ctl_ctloutput, |
187 | .pr_usrreqs = &ctl_usrreqs, |
188 | .pr_lock = ctl_lock, |
189 | .pr_unlock = ctl_unlock, |
190 | .pr_getlock = ctl_getlock, |
191 | } |
192 | }; |
193 | |
194 | __private_extern__ int kctl_reg_list SYSCTL_HANDLER_ARGS; |
195 | __private_extern__ int kctl_pcblist SYSCTL_HANDLER_ARGS; |
196 | __private_extern__ int kctl_getstat SYSCTL_HANDLER_ARGS; |
197 | |
198 | |
199 | SYSCTL_NODE(_net_systm, OID_AUTO, kctl, |
200 | CTLFLAG_RW|CTLFLAG_LOCKED, 0, "Kernel control family" ); |
201 | |
202 | struct kctlstat kctlstat; |
203 | SYSCTL_PROC(_net_systm_kctl, OID_AUTO, stats, |
204 | CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED, 0, 0, |
205 | kctl_getstat, "S,kctlstat" , "" ); |
206 | |
207 | SYSCTL_PROC(_net_systm_kctl, OID_AUTO, reg_list, |
208 | CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED, 0, 0, |
209 | kctl_reg_list, "S,xkctl_reg" , "" ); |
210 | |
211 | SYSCTL_PROC(_net_systm_kctl, OID_AUTO, pcblist, |
212 | CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED, 0, 0, |
213 | kctl_pcblist, "S,xkctlpcb" , "" ); |
214 | |
215 | u_int32_t ctl_autorcvbuf_max = 256 * 1024; |
216 | SYSCTL_INT(_net_systm_kctl, OID_AUTO, autorcvbufmax, |
217 | CTLFLAG_RW | CTLFLAG_LOCKED, &ctl_autorcvbuf_max, 0, "" ); |
218 | |
219 | u_int32_t ctl_autorcvbuf_high = 0; |
220 | SYSCTL_INT(_net_systm_kctl, OID_AUTO, autorcvbufhigh, |
221 | CTLFLAG_RD | CTLFLAG_LOCKED, &ctl_autorcvbuf_high, 0, "" ); |
222 | |
223 | u_int32_t ctl_debug = 0; |
224 | SYSCTL_INT(_net_systm_kctl, OID_AUTO, debug, |
225 | CTLFLAG_RW | CTLFLAG_LOCKED, &ctl_debug, 0, "" ); |
226 | |
227 | #define KCTL_TBL_INC 16 |
228 | |
229 | static uintptr_t kctl_tbl_size = 0; |
230 | static u_int32_t kctl_tbl_growing = 0; |
231 | static u_int32_t kctl_tbl_growing_waiting = 0; |
232 | static uintptr_t kctl_tbl_count = 0; |
233 | static struct kctl **kctl_table = NULL; |
234 | static uintptr_t kctl_ref_gencnt = 0; |
235 | |
236 | static void kctl_tbl_grow(void); |
237 | static kern_ctl_ref kctl_make_ref(struct kctl *kctl); |
238 | static void kctl_delete_ref(kern_ctl_ref); |
239 | static struct kctl *kctl_from_ref(kern_ctl_ref); |
240 | |
241 | /* |
242 | * Install the protosw's for the Kernel Control manager. |
243 | */ |
244 | __private_extern__ void |
245 | kern_control_init(struct domain *dp) |
246 | { |
247 | struct protosw *pr; |
248 | int i; |
249 | int kctl_proto_count = (sizeof (kctlsw) / sizeof (struct protosw)); |
250 | |
251 | VERIFY(!(dp->dom_flags & DOM_INITIALIZED)); |
252 | VERIFY(dp == systemdomain); |
253 | |
254 | ctl_lck_grp_attr = lck_grp_attr_alloc_init(); |
255 | if (ctl_lck_grp_attr == NULL) { |
256 | panic("%s: lck_grp_attr_alloc_init failed\n" , __func__); |
257 | /* NOTREACHED */ |
258 | } |
259 | |
260 | ctl_lck_grp = lck_grp_alloc_init("Kernel Control Protocol" , |
261 | ctl_lck_grp_attr); |
262 | if (ctl_lck_grp == NULL) { |
263 | panic("%s: lck_grp_alloc_init failed\n" , __func__); |
264 | /* NOTREACHED */ |
265 | } |
266 | |
267 | ctl_lck_attr = lck_attr_alloc_init(); |
268 | if (ctl_lck_attr == NULL) { |
269 | panic("%s: lck_attr_alloc_init failed\n" , __func__); |
270 | /* NOTREACHED */ |
271 | } |
272 | |
273 | ctl_mtx = lck_mtx_alloc_init(ctl_lck_grp, ctl_lck_attr); |
274 | if (ctl_mtx == NULL) { |
275 | panic("%s: lck_mtx_alloc_init failed\n" , __func__); |
276 | /* NOTREACHED */ |
277 | } |
278 | TAILQ_INIT(&ctl_head); |
279 | |
280 | for (i = 0, pr = &kctlsw[0]; i < kctl_proto_count; i++, pr++) |
281 | net_add_proto(pr, dp, 1); |
282 | } |
283 | |
284 | static void |
285 | kcb_delete(struct ctl_cb *kcb) |
286 | { |
287 | if (kcb != 0) { |
288 | if (kcb->mtx != 0) |
289 | lck_mtx_free(kcb->mtx, ctl_lck_grp); |
290 | FREE(kcb, M_TEMP); |
291 | } |
292 | } |
293 | |
294 | /* |
295 | * Kernel Controller user-request functions |
296 | * attach function must exist and succeed |
297 | * detach not necessary |
298 | * we need a pcb for the per socket mutex |
299 | */ |
300 | static int |
301 | ctl_attach(struct socket *so, int proto, struct proc *p) |
302 | { |
303 | #pragma unused(proto, p) |
304 | int error = 0; |
305 | struct ctl_cb *kcb = 0; |
306 | |
307 | MALLOC(kcb, struct ctl_cb *, sizeof(struct ctl_cb), M_TEMP, M_WAITOK); |
308 | if (kcb == NULL) { |
309 | error = ENOMEM; |
310 | goto quit; |
311 | } |
312 | bzero(kcb, sizeof(struct ctl_cb)); |
313 | |
314 | kcb->mtx = lck_mtx_alloc_init(ctl_lck_grp, ctl_lck_attr); |
315 | if (kcb->mtx == NULL) { |
316 | error = ENOMEM; |
317 | goto quit; |
318 | } |
319 | kcb->so = so; |
320 | so->so_pcb = (caddr_t)kcb; |
321 | |
322 | quit: |
323 | if (error != 0) { |
324 | kcb_delete(kcb); |
325 | kcb = 0; |
326 | } |
327 | return (error); |
328 | } |
329 | |
330 | static int |
331 | ctl_sofreelastref(struct socket *so) |
332 | { |
333 | struct ctl_cb *kcb = (struct ctl_cb *)so->so_pcb; |
334 | |
335 | so->so_pcb = 0; |
336 | |
337 | if (kcb != 0) { |
338 | struct kctl *kctl; |
339 | if ((kctl = kcb->kctl) != 0) { |
340 | lck_mtx_lock(ctl_mtx); |
341 | TAILQ_REMOVE(&kctl->kcb_head, kcb, next); |
342 | kctlstat.kcs_pcbcount--; |
343 | kctlstat.kcs_gencnt++; |
344 | lck_mtx_unlock(ctl_mtx); |
345 | } |
346 | kcb_delete(kcb); |
347 | } |
348 | sofreelastref(so, 1); |
349 | return (0); |
350 | } |
351 | |
352 | static int |
353 | ctl_detach(struct socket *so) |
354 | { |
355 | struct ctl_cb *kcb = (struct ctl_cb *)so->so_pcb; |
356 | |
357 | if (kcb == 0) |
358 | return (0); |
359 | |
360 | if (kcb->kctl != NULL && kcb->kctl->bind != NULL && |
361 | kcb->userdata != NULL && !(so->so_state & SS_ISCONNECTED)) { |
362 | // The unit was bound, but not connected |
363 | // Invoke the disconnected call to cleanup |
364 | if (kcb->kctl->disconnect != NULL) { |
365 | socket_unlock(so, 0); |
366 | (*kcb->kctl->disconnect)(kcb->kctl->kctlref, |
367 | kcb->sac.sc_unit, kcb->userdata); |
368 | socket_lock(so, 0); |
369 | } |
370 | } |
371 | |
372 | soisdisconnected(so); |
373 | so->so_flags |= SOF_PCBCLEARING; |
374 | return (0); |
375 | } |
376 | |
377 | static int |
378 | ctl_setup_kctl(struct socket *so, struct sockaddr *nam, struct proc *p) |
379 | { |
380 | struct kctl *kctl = NULL; |
381 | int error = 0; |
382 | struct sockaddr_ctl sa; |
383 | struct ctl_cb *kcb = (struct ctl_cb *)so->so_pcb; |
384 | struct ctl_cb *kcb_next = NULL; |
385 | u_quad_t sbmaxsize; |
386 | u_int32_t recvbufsize, sendbufsize; |
387 | |
388 | if (kcb == 0) { |
389 | panic("ctl_setup_kctl so_pcb null\n" ); |
390 | } |
391 | |
392 | if (kcb->kctl != NULL) { |
393 | // Already set up, skip |
394 | return (0); |
395 | } |
396 | |
397 | if (nam->sa_len != sizeof(struct sockaddr_ctl)) { |
398 | return (EINVAL); |
399 | } |
400 | |
401 | bcopy(nam, &sa, sizeof(struct sockaddr_ctl)); |
402 | |
403 | lck_mtx_lock(ctl_mtx); |
404 | kctl = ctl_find_by_id_unit(sa.sc_id, sa.sc_unit); |
405 | if (kctl == NULL) { |
406 | lck_mtx_unlock(ctl_mtx); |
407 | return (ENOENT); |
408 | } |
409 | |
410 | if (((kctl->flags & CTL_FLAG_REG_SOCK_STREAM) && |
411 | (so->so_type != SOCK_STREAM)) || |
412 | (!(kctl->flags & CTL_FLAG_REG_SOCK_STREAM) && |
413 | (so->so_type != SOCK_DGRAM))) { |
414 | lck_mtx_unlock(ctl_mtx); |
415 | return (EPROTOTYPE); |
416 | } |
417 | |
418 | if (kctl->flags & CTL_FLAG_PRIVILEGED) { |
419 | if (p == 0) { |
420 | lck_mtx_unlock(ctl_mtx); |
421 | return (EINVAL); |
422 | } |
423 | if (kauth_cred_issuser(kauth_cred_get()) == 0) { |
424 | lck_mtx_unlock(ctl_mtx); |
425 | return (EPERM); |
426 | } |
427 | } |
428 | |
429 | if ((kctl->flags & CTL_FLAG_REG_ID_UNIT) || sa.sc_unit != 0) { |
430 | if (kcb_find(kctl, sa.sc_unit) != NULL) { |
431 | lck_mtx_unlock(ctl_mtx); |
432 | return (EBUSY); |
433 | } |
434 | } else { |
435 | /* Find an unused ID, assumes control IDs are in order */ |
436 | u_int32_t unit = 1; |
437 | |
438 | TAILQ_FOREACH(kcb_next, &kctl->kcb_head, next) { |
439 | if (kcb_next->sac.sc_unit > unit) { |
440 | /* Found a gap, lets fill it in */ |
441 | break; |
442 | } |
443 | unit = kcb_next->sac.sc_unit + 1; |
444 | if (unit == ctl_maxunit) { |
445 | break; |
446 | } |
447 | } |
448 | |
449 | if (unit == ctl_maxunit) { |
450 | lck_mtx_unlock(ctl_mtx); |
451 | return (EBUSY); |
452 | } |
453 | |
454 | sa.sc_unit = unit; |
455 | } |
456 | |
457 | bcopy(&sa, &kcb->sac, sizeof(struct sockaddr_ctl)); |
458 | kcb->kctl = kctl; |
459 | if (kcb_next != NULL) { |
460 | TAILQ_INSERT_BEFORE(kcb_next, kcb, next); |
461 | } else { |
462 | TAILQ_INSERT_TAIL(&kctl->kcb_head, kcb, next); |
463 | } |
464 | kctlstat.kcs_pcbcount++; |
465 | kctlstat.kcs_gencnt++; |
466 | kctlstat.kcs_connections++; |
467 | lck_mtx_unlock(ctl_mtx); |
468 | |
469 | /* |
470 | * rdar://15526688: Limit the send and receive sizes to sb_max |
471 | * by using the same scaling as sbreserve() |
472 | */ |
473 | sbmaxsize = (u_quad_t)sb_max * MCLBYTES / (MSIZE + MCLBYTES); |
474 | |
475 | if (kctl->sendbufsize > sbmaxsize) { |
476 | sendbufsize = sbmaxsize; |
477 | } else { |
478 | sendbufsize = kctl->sendbufsize; |
479 | } |
480 | |
481 | if (kctl->recvbufsize > sbmaxsize) { |
482 | recvbufsize = sbmaxsize; |
483 | } else { |
484 | recvbufsize = kctl->recvbufsize; |
485 | } |
486 | |
487 | error = soreserve(so, sendbufsize, recvbufsize); |
488 | if (error) { |
489 | if (ctl_debug) |
490 | printf("%s - soreserve(%llx, %u, %u) error %d\n" , |
491 | __func__, (uint64_t)VM_KERNEL_ADDRPERM(so), |
492 | sendbufsize, recvbufsize, error); |
493 | goto done; |
494 | } |
495 | |
496 | done: |
497 | if (error) { |
498 | soisdisconnected(so); |
499 | lck_mtx_lock(ctl_mtx); |
500 | TAILQ_REMOVE(&kctl->kcb_head, kcb, next); |
501 | kcb->kctl = NULL; |
502 | kcb->sac.sc_unit = 0; |
503 | kctlstat.kcs_pcbcount--; |
504 | kctlstat.kcs_gencnt++; |
505 | kctlstat.kcs_conn_fail++; |
506 | lck_mtx_unlock(ctl_mtx); |
507 | } |
508 | return (error); |
509 | } |
510 | |
511 | static int |
512 | ctl_bind(struct socket *so, struct sockaddr *nam, struct proc *p) |
513 | { |
514 | int error = 0; |
515 | struct ctl_cb *kcb = (struct ctl_cb *)so->so_pcb; |
516 | |
517 | if (kcb == NULL) { |
518 | panic("ctl_bind so_pcb null\n" ); |
519 | } |
520 | |
521 | error = ctl_setup_kctl(so, nam, p); |
522 | if (error) { |
523 | return (error); |
524 | } |
525 | |
526 | if (kcb->kctl == NULL) { |
527 | panic("ctl_bind kctl null\n" ); |
528 | } |
529 | |
530 | if (kcb->kctl->bind == NULL) { |
531 | return (EINVAL); |
532 | } |
533 | |
534 | socket_unlock(so, 0); |
535 | error = (*kcb->kctl->bind)(kcb->kctl->kctlref, &kcb->sac, &kcb->userdata); |
536 | socket_lock(so, 0); |
537 | |
538 | return (error); |
539 | } |
540 | |
541 | static int |
542 | ctl_connect(struct socket *so, struct sockaddr *nam, struct proc *p) |
543 | { |
544 | int error = 0; |
545 | struct ctl_cb *kcb = (struct ctl_cb *)so->so_pcb; |
546 | |
547 | if (kcb == NULL) { |
548 | panic("ctl_connect so_pcb null\n" ); |
549 | } |
550 | |
551 | error = ctl_setup_kctl(so, nam, p); |
552 | if (error) { |
553 | return (error); |
554 | } |
555 | |
556 | if (kcb->kctl == NULL) { |
557 | panic("ctl_connect kctl null\n" ); |
558 | } |
559 | |
560 | soisconnecting(so); |
561 | socket_unlock(so, 0); |
562 | error = (*kcb->kctl->connect)(kcb->kctl->kctlref, &kcb->sac, &kcb->userdata); |
563 | socket_lock(so, 0); |
564 | if (error) { |
565 | goto end; |
566 | } |
567 | soisconnected(so); |
568 | |
569 | end: |
570 | if (error && kcb->kctl->disconnect) { |
571 | /* |
572 | * XXX Make sure we Don't check the return value |
573 | * of disconnect here. |
574 | * ipsec/utun_ctl_disconnect will return error when |
575 | * disconnect gets called after connect failure. |
576 | * However if we decide to check for disconnect return |
577 | * value here. Please make sure to revisit |
578 | * ipsec/utun_ctl_disconnect. |
579 | */ |
580 | socket_unlock(so, 0); |
581 | (*kcb->kctl->disconnect)(kcb->kctl->kctlref, kcb->sac.sc_unit, kcb->userdata); |
582 | socket_lock(so, 0); |
583 | } |
584 | if (error) { |
585 | soisdisconnected(so); |
586 | lck_mtx_lock(ctl_mtx); |
587 | TAILQ_REMOVE(&kcb->kctl->kcb_head, kcb, next); |
588 | kcb->kctl = NULL; |
589 | kcb->sac.sc_unit = 0; |
590 | kctlstat.kcs_pcbcount--; |
591 | kctlstat.kcs_gencnt++; |
592 | kctlstat.kcs_conn_fail++; |
593 | lck_mtx_unlock(ctl_mtx); |
594 | } |
595 | return (error); |
596 | } |
597 | |
598 | static int |
599 | ctl_disconnect(struct socket *so) |
600 | { |
601 | struct ctl_cb *kcb = (struct ctl_cb *)so->so_pcb; |
602 | |
603 | if ((kcb = (struct ctl_cb *)so->so_pcb)) { |
604 | struct kctl *kctl = kcb->kctl; |
605 | |
606 | if (kctl && kctl->disconnect) { |
607 | socket_unlock(so, 0); |
608 | (*kctl->disconnect)(kctl->kctlref, kcb->sac.sc_unit, |
609 | kcb->userdata); |
610 | socket_lock(so, 0); |
611 | } |
612 | |
613 | soisdisconnected(so); |
614 | |
615 | socket_unlock(so, 0); |
616 | lck_mtx_lock(ctl_mtx); |
617 | kcb->kctl = 0; |
618 | kcb->sac.sc_unit = 0; |
619 | while (kcb->usecount != 0) { |
620 | msleep(&kcb->usecount, ctl_mtx, 0, "kcb->usecount" , 0); |
621 | } |
622 | TAILQ_REMOVE(&kctl->kcb_head, kcb, next); |
623 | kctlstat.kcs_pcbcount--; |
624 | kctlstat.kcs_gencnt++; |
625 | lck_mtx_unlock(ctl_mtx); |
626 | socket_lock(so, 0); |
627 | } |
628 | return (0); |
629 | } |
630 | |
631 | static int |
632 | ctl_peeraddr(struct socket *so, struct sockaddr **nam) |
633 | { |
634 | struct ctl_cb *kcb = (struct ctl_cb *)so->so_pcb; |
635 | struct kctl *kctl; |
636 | struct sockaddr_ctl sc; |
637 | |
638 | if (kcb == NULL) /* sanity check */ |
639 | return (ENOTCONN); |
640 | |
641 | if ((kctl = kcb->kctl) == NULL) |
642 | return (EINVAL); |
643 | |
644 | bzero(&sc, sizeof(struct sockaddr_ctl)); |
645 | sc.sc_len = sizeof(struct sockaddr_ctl); |
646 | sc.sc_family = AF_SYSTEM; |
647 | sc.ss_sysaddr = AF_SYS_CONTROL; |
648 | sc.sc_id = kctl->id; |
649 | sc.sc_unit = kcb->sac.sc_unit; |
650 | |
651 | *nam = dup_sockaddr((struct sockaddr *)&sc, 1); |
652 | |
653 | return (0); |
654 | } |
655 | |
656 | static void |
657 | ctl_sbrcv_trim(struct socket *so) |
658 | { |
659 | struct sockbuf *sb = &so->so_rcv; |
660 | |
661 | if (sb->sb_hiwat > sb->sb_idealsize) { |
662 | u_int32_t diff; |
663 | int32_t trim; |
664 | |
665 | /* |
666 | * The difference between the ideal size and the |
667 | * current size is the upper bound of the trimage |
668 | */ |
669 | diff = sb->sb_hiwat - sb->sb_idealsize; |
670 | /* |
671 | * We cannot trim below the outstanding data |
672 | */ |
673 | trim = sb->sb_hiwat - sb->sb_cc; |
674 | |
675 | trim = imin(trim, (int32_t)diff); |
676 | |
677 | if (trim > 0) { |
678 | sbreserve(sb, (sb->sb_hiwat - trim)); |
679 | |
680 | if (ctl_debug) |
681 | printf("%s - shrunk to %d\n" , |
682 | __func__, sb->sb_hiwat); |
683 | } |
684 | } |
685 | } |
686 | |
687 | static int |
688 | ctl_usr_rcvd(struct socket *so, int flags) |
689 | { |
690 | struct ctl_cb *kcb = (struct ctl_cb *)so->so_pcb; |
691 | struct kctl *kctl; |
692 | |
693 | if ((kctl = kcb->kctl) == NULL) { |
694 | return (EINVAL); |
695 | } |
696 | |
697 | if (kctl->rcvd) { |
698 | socket_unlock(so, 0); |
699 | (*kctl->rcvd)(kctl->kctlref, kcb->sac.sc_unit, kcb->userdata, flags); |
700 | socket_lock(so, 0); |
701 | } |
702 | |
703 | ctl_sbrcv_trim(so); |
704 | |
705 | return (0); |
706 | } |
707 | |
708 | static int |
709 | ctl_send(struct socket *so, int flags, struct mbuf *m, |
710 | struct sockaddr *addr, struct mbuf *control, |
711 | struct proc *p) |
712 | { |
713 | #pragma unused(addr, p) |
714 | int error = 0; |
715 | struct ctl_cb *kcb = (struct ctl_cb *)so->so_pcb; |
716 | struct kctl *kctl; |
717 | |
718 | if (control) |
719 | m_freem(control); |
720 | |
721 | if (kcb == NULL) /* sanity check */ |
722 | error = ENOTCONN; |
723 | |
724 | if (error == 0 && (kctl = kcb->kctl) == NULL) |
725 | error = EINVAL; |
726 | |
727 | if (error == 0 && kctl->send) { |
728 | so_tc_update_stats(m, so, m_get_service_class(m)); |
729 | socket_unlock(so, 0); |
730 | error = (*kctl->send)(kctl->kctlref, kcb->sac.sc_unit, kcb->userdata, |
731 | m, flags); |
732 | socket_lock(so, 0); |
733 | } else { |
734 | m_freem(m); |
735 | if (error == 0) |
736 | error = ENOTSUP; |
737 | } |
738 | if (error != 0) |
739 | OSIncrementAtomic64((SInt64 *)&kctlstat.kcs_send_fail); |
740 | return (error); |
741 | } |
742 | |
743 | static int |
744 | ctl_send_list(struct socket *so, int flags, struct mbuf *m, |
745 | __unused struct sockaddr *addr, struct mbuf *control, |
746 | __unused struct proc *p) |
747 | { |
748 | int error = 0; |
749 | struct ctl_cb *kcb = (struct ctl_cb *)so->so_pcb; |
750 | struct kctl *kctl; |
751 | |
752 | if (control) |
753 | m_freem_list(control); |
754 | |
755 | if (kcb == NULL) /* sanity check */ |
756 | error = ENOTCONN; |
757 | |
758 | if (error == 0 && (kctl = kcb->kctl) == NULL) |
759 | error = EINVAL; |
760 | |
761 | if (error == 0 && kctl->send_list) { |
762 | struct mbuf *nxt; |
763 | |
764 | for (nxt = m; nxt != NULL; nxt = nxt->m_nextpkt) |
765 | so_tc_update_stats(nxt, so, m_get_service_class(nxt)); |
766 | |
767 | socket_unlock(so, 0); |
768 | error = (*kctl->send_list)(kctl->kctlref, kcb->sac.sc_unit, |
769 | kcb->userdata, m, flags); |
770 | socket_lock(so, 0); |
771 | } else if (error == 0 && kctl->send) { |
772 | while (m != NULL && error == 0) { |
773 | struct mbuf *nextpkt = m->m_nextpkt; |
774 | |
775 | m->m_nextpkt = NULL; |
776 | so_tc_update_stats(m, so, m_get_service_class(m)); |
777 | socket_unlock(so, 0); |
778 | error = (*kctl->send)(kctl->kctlref, kcb->sac.sc_unit, |
779 | kcb->userdata, m, flags); |
780 | socket_lock(so, 0); |
781 | m = nextpkt; |
782 | } |
783 | if (m != NULL) |
784 | m_freem_list(m); |
785 | } else { |
786 | m_freem_list(m); |
787 | if (error == 0) |
788 | error = ENOTSUP; |
789 | } |
790 | if (error != 0) |
791 | OSIncrementAtomic64((SInt64 *)&kctlstat.kcs_send_list_fail); |
792 | return (error); |
793 | } |
794 | |
795 | static errno_t |
796 | ctl_rcvbspace(struct socket *so, u_int32_t datasize, |
797 | u_int32_t kctlflags, u_int32_t flags) |
798 | { |
799 | struct sockbuf *sb = &so->so_rcv; |
800 | u_int32_t space = sbspace(sb); |
801 | errno_t error; |
802 | |
803 | if ((kctlflags & CTL_FLAG_REG_CRIT) == 0) { |
804 | if ((u_int32_t) space >= datasize) |
805 | error = 0; |
806 | else |
807 | error = ENOBUFS; |
808 | } else if ((flags & CTL_DATA_CRIT) == 0) { |
809 | /* |
810 | * Reserve 25% for critical messages |
811 | */ |
812 | if (space < (sb->sb_hiwat >> 2) || |
813 | space < datasize) |
814 | error = ENOBUFS; |
815 | else |
816 | error = 0; |
817 | } else { |
818 | u_int32_t autorcvbuf_max; |
819 | |
820 | /* |
821 | * Allow overcommit of 25% |
822 | */ |
823 | autorcvbuf_max = min(sb->sb_idealsize + (sb->sb_idealsize >> 2), |
824 | ctl_autorcvbuf_max); |
825 | |
826 | if ((u_int32_t) space >= datasize) { |
827 | error = 0; |
828 | } else if (tcp_cansbgrow(sb) && |
829 | sb->sb_hiwat < autorcvbuf_max) { |
830 | /* |
831 | * Grow with a little bit of leeway |
832 | */ |
833 | u_int32_t grow = datasize - space + MSIZE; |
834 | |
835 | if (sbreserve(sb, |
836 | min((sb->sb_hiwat + grow), autorcvbuf_max)) == 1) { |
837 | |
838 | if (sb->sb_hiwat > ctl_autorcvbuf_high) |
839 | ctl_autorcvbuf_high = sb->sb_hiwat; |
840 | |
841 | /* |
842 | * A final check |
843 | */ |
844 | if ((u_int32_t) sbspace(sb) >= datasize) { |
845 | error = 0; |
846 | } else { |
847 | error = ENOBUFS; |
848 | } |
849 | |
850 | if (ctl_debug) |
851 | printf("%s - grown to %d error %d\n" , |
852 | __func__, sb->sb_hiwat, error); |
853 | } else { |
854 | error = ENOBUFS; |
855 | } |
856 | } else { |
857 | error = ENOBUFS; |
858 | } |
859 | } |
860 | return (error); |
861 | } |
862 | |
863 | errno_t |
864 | ctl_enqueuembuf(kern_ctl_ref kctlref, u_int32_t unit, struct mbuf *m, |
865 | u_int32_t flags) |
866 | { |
867 | struct socket *so; |
868 | errno_t error = 0; |
869 | int len = m->m_pkthdr.len; |
870 | u_int32_t kctlflags; |
871 | |
872 | so = kcb_find_socket(kctlref, unit, &kctlflags); |
873 | if (so == NULL) { |
874 | return (EINVAL); |
875 | } |
876 | |
877 | if (ctl_rcvbspace(so, len, kctlflags, flags) != 0) { |
878 | error = ENOBUFS; |
879 | OSIncrementAtomic64((SInt64 *)&kctlstat.kcs_enqueue_fullsock); |
880 | goto bye; |
881 | } |
882 | if ((flags & CTL_DATA_EOR)) |
883 | m->m_flags |= M_EOR; |
884 | |
885 | so_recv_data_stat(so, m, 0); |
886 | if (sbappend(&so->so_rcv, m) != 0) { |
887 | if ((flags & CTL_DATA_NOWAKEUP) == 0) |
888 | sorwakeup(so); |
889 | } else { |
890 | error = ENOBUFS; |
891 | OSIncrementAtomic64((SInt64 *)&kctlstat.kcs_enqueue_fullsock); |
892 | } |
893 | bye: |
894 | if (ctl_debug && error != 0 && (flags & CTL_DATA_CRIT)) |
895 | printf("%s - crit data err %d len %d hiwat %d cc: %d\n" , |
896 | __func__, error, len, |
897 | so->so_rcv.sb_hiwat, so->so_rcv.sb_cc); |
898 | |
899 | socket_unlock(so, 1); |
900 | if (error != 0) |
901 | OSIncrementAtomic64((SInt64 *)&kctlstat.kcs_enqueue_fail); |
902 | |
903 | return (error); |
904 | } |
905 | |
906 | /* |
907 | * Compute space occupied by mbuf like sbappendrecord |
908 | */ |
909 | static int |
910 | m_space(struct mbuf *m) |
911 | { |
912 | int space = 0; |
913 | struct mbuf *nxt; |
914 | |
915 | for (nxt = m; nxt != NULL; nxt = nxt->m_next) |
916 | space += nxt->m_len; |
917 | |
918 | return (space); |
919 | } |
920 | |
921 | errno_t |
922 | ctl_enqueuembuf_list(void *kctlref, u_int32_t unit, struct mbuf *m_list, |
923 | u_int32_t flags, struct mbuf **m_remain) |
924 | { |
925 | struct socket *so = NULL; |
926 | errno_t error = 0; |
927 | struct mbuf *m, *nextpkt; |
928 | int needwakeup = 0; |
929 | int len = 0; |
930 | u_int32_t kctlflags; |
931 | |
932 | /* |
933 | * Need to point the beginning of the list in case of early exit |
934 | */ |
935 | m = m_list; |
936 | |
937 | /* |
938 | * kcb_find_socket takes the socket lock with a reference |
939 | */ |
940 | so = kcb_find_socket(kctlref, unit, &kctlflags); |
941 | if (so == NULL) { |
942 | error = EINVAL; |
943 | goto done; |
944 | } |
945 | |
946 | if (kctlflags & CTL_FLAG_REG_SOCK_STREAM) { |
947 | error = EOPNOTSUPP; |
948 | goto done; |
949 | } |
950 | if (flags & CTL_DATA_EOR) { |
951 | error = EINVAL; |
952 | goto done; |
953 | } |
954 | |
955 | for (m = m_list; m != NULL; m = nextpkt) { |
956 | nextpkt = m->m_nextpkt; |
957 | |
958 | if (m->m_pkthdr.len == 0 && ctl_debug) |
959 | printf("%s: %llx m_pkthdr.len is 0" , |
960 | __func__, (uint64_t)VM_KERNEL_ADDRPERM(m)); |
961 | |
962 | /* |
963 | * The mbuf is either appended or freed by sbappendrecord() |
964 | * so it's not reliable from a data standpoint |
965 | */ |
966 | len = m_space(m); |
967 | if (ctl_rcvbspace(so, len, kctlflags, flags) != 0) { |
968 | error = ENOBUFS; |
969 | OSIncrementAtomic64( |
970 | (SInt64 *)&kctlstat.kcs_enqueue_fullsock); |
971 | break; |
972 | } else { |
973 | /* |
974 | * Unlink from the list, m is on its own |
975 | */ |
976 | m->m_nextpkt = NULL; |
977 | so_recv_data_stat(so, m, 0); |
978 | if (sbappendrecord(&so->so_rcv, m) != 0) { |
979 | needwakeup = 1; |
980 | } else { |
981 | /* |
982 | * We free or return the remaining |
983 | * mbufs in the list |
984 | */ |
985 | m = nextpkt; |
986 | error = ENOBUFS; |
987 | OSIncrementAtomic64( |
988 | (SInt64 *)&kctlstat.kcs_enqueue_fullsock); |
989 | break; |
990 | } |
991 | } |
992 | } |
993 | if (needwakeup && (flags & CTL_DATA_NOWAKEUP) == 0) |
994 | sorwakeup(so); |
995 | |
996 | done: |
997 | if (so != NULL) { |
998 | if (ctl_debug && error != 0 && (flags & CTL_DATA_CRIT)) |
999 | printf("%s - crit data err %d len %d hiwat %d cc: %d\n" , |
1000 | __func__, error, len, |
1001 | so->so_rcv.sb_hiwat, so->so_rcv.sb_cc); |
1002 | |
1003 | socket_unlock(so, 1); |
1004 | } |
1005 | if (m_remain) { |
1006 | *m_remain = m; |
1007 | |
1008 | if (m != NULL && socket_debug && so != NULL && |
1009 | (so->so_options & SO_DEBUG)) { |
1010 | struct mbuf *n; |
1011 | |
1012 | printf("%s m_list %llx\n" , __func__, |
1013 | (uint64_t) VM_KERNEL_ADDRPERM(m_list)); |
1014 | for (n = m; n != NULL; n = n->m_nextpkt) |
1015 | printf(" remain %llx m_next %llx\n" , |
1016 | (uint64_t) VM_KERNEL_ADDRPERM(n), |
1017 | (uint64_t) VM_KERNEL_ADDRPERM(n->m_next)); |
1018 | } |
1019 | } else { |
1020 | if (m != NULL) |
1021 | m_freem_list(m); |
1022 | } |
1023 | if (error != 0) |
1024 | OSIncrementAtomic64((SInt64 *)&kctlstat.kcs_enqueue_fail); |
1025 | return (error); |
1026 | } |
1027 | |
1028 | errno_t |
1029 | ctl_enqueuedata(void *kctlref, u_int32_t unit, void *data, size_t len, |
1030 | u_int32_t flags) |
1031 | { |
1032 | struct socket *so; |
1033 | struct mbuf *m; |
1034 | errno_t error = 0; |
1035 | unsigned int num_needed; |
1036 | struct mbuf *n; |
1037 | size_t curlen = 0; |
1038 | u_int32_t kctlflags; |
1039 | |
1040 | so = kcb_find_socket(kctlref, unit, &kctlflags); |
1041 | if (so == NULL) { |
1042 | return (EINVAL); |
1043 | } |
1044 | |
1045 | if (ctl_rcvbspace(so, len, kctlflags, flags) != 0) { |
1046 | error = ENOBUFS; |
1047 | OSIncrementAtomic64((SInt64 *)&kctlstat.kcs_enqueue_fullsock); |
1048 | goto bye; |
1049 | } |
1050 | |
1051 | num_needed = 1; |
1052 | m = m_allocpacket_internal(&num_needed, len, NULL, M_NOWAIT, 1, 0); |
1053 | if (m == NULL) { |
1054 | kctlstat.kcs_enqdata_mb_alloc_fail++; |
1055 | if (ctl_debug) |
1056 | printf("%s: m_allocpacket_internal(%lu) failed\n" , |
1057 | __func__, len); |
1058 | error = ENOMEM; |
1059 | goto bye; |
1060 | } |
1061 | |
1062 | for (n = m; n != NULL; n = n->m_next) { |
1063 | size_t mlen = mbuf_maxlen(n); |
1064 | |
1065 | if (mlen + curlen > len) |
1066 | mlen = len - curlen; |
1067 | n->m_len = mlen; |
1068 | bcopy((char *)data + curlen, n->m_data, mlen); |
1069 | curlen += mlen; |
1070 | } |
1071 | mbuf_pkthdr_setlen(m, curlen); |
1072 | |
1073 | if ((flags & CTL_DATA_EOR)) |
1074 | m->m_flags |= M_EOR; |
1075 | so_recv_data_stat(so, m, 0); |
1076 | if (sbappend(&so->so_rcv, m) != 0) { |
1077 | if ((flags & CTL_DATA_NOWAKEUP) == 0) |
1078 | sorwakeup(so); |
1079 | } else { |
1080 | kctlstat.kcs_enqdata_sbappend_fail++; |
1081 | error = ENOBUFS; |
1082 | OSIncrementAtomic64((SInt64 *)&kctlstat.kcs_enqueue_fullsock); |
1083 | } |
1084 | |
1085 | bye: |
1086 | if (ctl_debug && error != 0 && (flags & CTL_DATA_CRIT)) |
1087 | printf("%s - crit data err %d len %d hiwat %d cc: %d\n" , |
1088 | __func__, error, (int)len, |
1089 | so->so_rcv.sb_hiwat, so->so_rcv.sb_cc); |
1090 | |
1091 | socket_unlock(so, 1); |
1092 | if (error != 0) |
1093 | OSIncrementAtomic64((SInt64 *)&kctlstat.kcs_enqueue_fail); |
1094 | return (error); |
1095 | } |
1096 | |
1097 | errno_t |
1098 | ctl_getenqueuepacketcount(kern_ctl_ref kctlref, u_int32_t unit, u_int32_t *pcnt) |
1099 | { |
1100 | struct socket *so; |
1101 | u_int32_t cnt; |
1102 | struct mbuf *m1; |
1103 | |
1104 | if (pcnt == NULL) |
1105 | return (EINVAL); |
1106 | |
1107 | so = kcb_find_socket(kctlref, unit, NULL); |
1108 | if (so == NULL) { |
1109 | return (EINVAL); |
1110 | } |
1111 | |
1112 | cnt = 0; |
1113 | m1 = so->so_rcv.sb_mb; |
1114 | while (m1 != NULL) { |
1115 | if (m1->m_type == MT_DATA || |
1116 | m1->m_type == MT_HEADER || |
1117 | m1->m_type == MT_OOBDATA) |
1118 | cnt += 1; |
1119 | m1 = m1->m_nextpkt; |
1120 | } |
1121 | *pcnt = cnt; |
1122 | |
1123 | socket_unlock(so, 1); |
1124 | |
1125 | return (0); |
1126 | } |
1127 | |
1128 | errno_t |
1129 | ctl_getenqueuespace(kern_ctl_ref kctlref, u_int32_t unit, size_t *space) |
1130 | { |
1131 | struct socket *so; |
1132 | long avail; |
1133 | |
1134 | if (space == NULL) |
1135 | return (EINVAL); |
1136 | |
1137 | so = kcb_find_socket(kctlref, unit, NULL); |
1138 | if (so == NULL) { |
1139 | return (EINVAL); |
1140 | } |
1141 | |
1142 | avail = sbspace(&so->so_rcv); |
1143 | *space = (avail < 0) ? 0 : avail; |
1144 | socket_unlock(so, 1); |
1145 | |
1146 | return (0); |
1147 | } |
1148 | |
1149 | errno_t |
1150 | ctl_getenqueuereadable(kern_ctl_ref kctlref, u_int32_t unit, |
1151 | u_int32_t *difference) |
1152 | { |
1153 | struct socket *so; |
1154 | |
1155 | if (difference == NULL) |
1156 | return (EINVAL); |
1157 | |
1158 | so = kcb_find_socket(kctlref, unit, NULL); |
1159 | if (so == NULL) { |
1160 | return (EINVAL); |
1161 | } |
1162 | |
1163 | if (so->so_rcv.sb_cc >= so->so_rcv.sb_lowat) { |
1164 | *difference = 0; |
1165 | } else { |
1166 | *difference = (so->so_rcv.sb_lowat - so->so_rcv.sb_cc); |
1167 | } |
1168 | socket_unlock(so, 1); |
1169 | |
1170 | return (0); |
1171 | } |
1172 | |
1173 | static int |
1174 | ctl_ctloutput(struct socket *so, struct sockopt *sopt) |
1175 | { |
1176 | struct ctl_cb *kcb = (struct ctl_cb *)so->so_pcb; |
1177 | struct kctl *kctl; |
1178 | int error = 0; |
1179 | void *data = NULL; |
1180 | size_t len; |
1181 | |
1182 | if (sopt->sopt_level != SYSPROTO_CONTROL) { |
1183 | return (EINVAL); |
1184 | } |
1185 | |
1186 | if (kcb == NULL) /* sanity check */ |
1187 | return (ENOTCONN); |
1188 | |
1189 | if ((kctl = kcb->kctl) == NULL) |
1190 | return (EINVAL); |
1191 | |
1192 | switch (sopt->sopt_dir) { |
1193 | case SOPT_SET: |
1194 | if (kctl->setopt == NULL) |
1195 | return (ENOTSUP); |
1196 | if (sopt->sopt_valsize != 0) { |
1197 | MALLOC(data, void *, sopt->sopt_valsize, M_TEMP, |
1198 | M_WAITOK | M_ZERO); |
1199 | if (data == NULL) |
1200 | return (ENOMEM); |
1201 | error = sooptcopyin(sopt, data, |
1202 | sopt->sopt_valsize, sopt->sopt_valsize); |
1203 | } |
1204 | if (error == 0) { |
1205 | socket_unlock(so, 0); |
1206 | error = (*kctl->setopt)(kctl->kctlref, |
1207 | kcb->sac.sc_unit, kcb->userdata, sopt->sopt_name, |
1208 | data, sopt->sopt_valsize); |
1209 | socket_lock(so, 0); |
1210 | } |
1211 | |
1212 | if (data != NULL) |
1213 | FREE(data, M_TEMP); |
1214 | break; |
1215 | |
1216 | case SOPT_GET: |
1217 | if (kctl->getopt == NULL) |
1218 | return (ENOTSUP); |
1219 | |
1220 | if (sopt->sopt_valsize && sopt->sopt_val) { |
1221 | MALLOC(data, void *, sopt->sopt_valsize, M_TEMP, |
1222 | M_WAITOK | M_ZERO); |
1223 | if (data == NULL) |
1224 | return (ENOMEM); |
1225 | /* |
1226 | * 4108337 - copy user data in case the |
1227 | * kernel control needs it |
1228 | */ |
1229 | error = sooptcopyin(sopt, data, |
1230 | sopt->sopt_valsize, sopt->sopt_valsize); |
1231 | } |
1232 | |
1233 | if (error == 0) { |
1234 | len = sopt->sopt_valsize; |
1235 | socket_unlock(so, 0); |
1236 | error = (*kctl->getopt)(kctl->kctlref, kcb->sac.sc_unit, |
1237 | kcb->userdata, sopt->sopt_name, |
1238 | data, &len); |
1239 | if (data != NULL && len > sopt->sopt_valsize) |
1240 | panic_plain("ctl_ctloutput: ctl %s returned " |
1241 | "len (%lu) > sopt_valsize (%lu)\n" , |
1242 | kcb->kctl->name, len, |
1243 | sopt->sopt_valsize); |
1244 | socket_lock(so, 0); |
1245 | if (error == 0) { |
1246 | if (data != NULL) |
1247 | error = sooptcopyout(sopt, data, len); |
1248 | else |
1249 | sopt->sopt_valsize = len; |
1250 | } |
1251 | } |
1252 | if (data != NULL) |
1253 | FREE(data, M_TEMP); |
1254 | break; |
1255 | } |
1256 | return (error); |
1257 | } |
1258 | |
1259 | static int |
1260 | ctl_ioctl(struct socket *so, u_long cmd, caddr_t data, |
1261 | struct ifnet *ifp, struct proc *p) |
1262 | { |
1263 | #pragma unused(so, ifp, p) |
1264 | int error = ENOTSUP; |
1265 | |
1266 | switch (cmd) { |
1267 | /* get the number of controllers */ |
1268 | case CTLIOCGCOUNT: { |
1269 | struct kctl *kctl; |
1270 | u_int32_t n = 0; |
1271 | |
1272 | lck_mtx_lock(ctl_mtx); |
1273 | TAILQ_FOREACH(kctl, &ctl_head, next) |
1274 | n++; |
1275 | lck_mtx_unlock(ctl_mtx); |
1276 | |
1277 | bcopy(&n, data, sizeof (n)); |
1278 | error = 0; |
1279 | break; |
1280 | } |
1281 | case CTLIOCGINFO: { |
1282 | struct ctl_info ctl_info; |
1283 | struct kctl *kctl = 0; |
1284 | size_t name_len; |
1285 | |
1286 | bcopy(data, &ctl_info, sizeof (ctl_info)); |
1287 | name_len = strnlen(ctl_info.ctl_name, MAX_KCTL_NAME); |
1288 | |
1289 | if (name_len == 0 || name_len + 1 > MAX_KCTL_NAME) { |
1290 | error = EINVAL; |
1291 | break; |
1292 | } |
1293 | lck_mtx_lock(ctl_mtx); |
1294 | kctl = ctl_find_by_name(ctl_info.ctl_name); |
1295 | lck_mtx_unlock(ctl_mtx); |
1296 | if (kctl == 0) { |
1297 | error = ENOENT; |
1298 | break; |
1299 | } |
1300 | ctl_info.ctl_id = kctl->id; |
1301 | bcopy(&ctl_info, data, sizeof (ctl_info)); |
1302 | error = 0; |
1303 | break; |
1304 | } |
1305 | |
1306 | /* add controls to get list of NKEs */ |
1307 | |
1308 | } |
1309 | |
1310 | return (error); |
1311 | } |
1312 | |
1313 | static void |
1314 | kctl_tbl_grow() |
1315 | { |
1316 | struct kctl **new_table; |
1317 | uintptr_t new_size; |
1318 | |
1319 | lck_mtx_assert(ctl_mtx, LCK_MTX_ASSERT_OWNED); |
1320 | |
1321 | if (kctl_tbl_growing) { |
1322 | /* Another thread is allocating */ |
1323 | kctl_tbl_growing_waiting++; |
1324 | |
1325 | do { |
1326 | (void) msleep((caddr_t) &kctl_tbl_growing, ctl_mtx, |
1327 | PSOCK | PCATCH, "kctl_tbl_growing" , 0); |
1328 | } while (kctl_tbl_growing); |
1329 | kctl_tbl_growing_waiting--; |
1330 | } |
1331 | /* Another thread grew the table */ |
1332 | if (kctl_table != NULL && kctl_tbl_count < kctl_tbl_size) |
1333 | return; |
1334 | |
1335 | /* Verify we have a sane size */ |
1336 | if (kctl_tbl_size + KCTL_TBL_INC >= UINT16_MAX) { |
1337 | kctlstat.kcs_tbl_size_too_big++; |
1338 | if (ctl_debug) |
1339 | printf("%s kctl_tbl_size %lu too big\n" , |
1340 | __func__, kctl_tbl_size); |
1341 | return; |
1342 | } |
1343 | kctl_tbl_growing = 1; |
1344 | |
1345 | new_size = kctl_tbl_size + KCTL_TBL_INC; |
1346 | |
1347 | lck_mtx_unlock(ctl_mtx); |
1348 | new_table = _MALLOC(sizeof(struct kctl *) * new_size, |
1349 | M_TEMP, M_WAIT | M_ZERO); |
1350 | lck_mtx_lock(ctl_mtx); |
1351 | |
1352 | if (new_table != NULL) { |
1353 | if (kctl_table != NULL) { |
1354 | bcopy(kctl_table, new_table, |
1355 | kctl_tbl_size * sizeof(struct kctl *)); |
1356 | |
1357 | _FREE(kctl_table, M_TEMP); |
1358 | } |
1359 | kctl_table = new_table; |
1360 | kctl_tbl_size = new_size; |
1361 | } |
1362 | |
1363 | kctl_tbl_growing = 0; |
1364 | |
1365 | if (kctl_tbl_growing_waiting) { |
1366 | wakeup(&kctl_tbl_growing); |
1367 | } |
1368 | } |
1369 | |
1370 | #define KCTLREF_INDEX_MASK 0x0000FFFF |
1371 | #define KCTLREF_GENCNT_MASK 0xFFFF0000 |
1372 | #define KCTLREF_GENCNT_SHIFT 16 |
1373 | |
1374 | static kern_ctl_ref |
1375 | kctl_make_ref(struct kctl *kctl) |
1376 | { |
1377 | uintptr_t i; |
1378 | |
1379 | lck_mtx_assert(ctl_mtx, LCK_MTX_ASSERT_OWNED); |
1380 | |
1381 | if (kctl_tbl_count >= kctl_tbl_size) |
1382 | kctl_tbl_grow(); |
1383 | |
1384 | kctl->kctlref = NULL; |
1385 | for (i = 0; i < kctl_tbl_size; i++) { |
1386 | if (kctl_table[i] == NULL) { |
1387 | uintptr_t ref; |
1388 | |
1389 | /* |
1390 | * Reference is index plus one |
1391 | */ |
1392 | kctl_ref_gencnt += 1; |
1393 | |
1394 | /* |
1395 | * Add generation count as salt to reference to prevent |
1396 | * use after deregister |
1397 | */ |
1398 | ref = ((kctl_ref_gencnt << KCTLREF_GENCNT_SHIFT) & |
1399 | KCTLREF_GENCNT_MASK) + |
1400 | ((i + 1) & KCTLREF_INDEX_MASK); |
1401 | |
1402 | kctl->kctlref = (void *)(ref); |
1403 | kctl_table[i] = kctl; |
1404 | kctl_tbl_count++; |
1405 | break; |
1406 | } |
1407 | } |
1408 | |
1409 | if (kctl->kctlref == NULL) |
1410 | panic("%s no space in table" , __func__); |
1411 | |
1412 | if (ctl_debug > 0) |
1413 | printf("%s %p for %p\n" , |
1414 | __func__, kctl->kctlref, kctl); |
1415 | |
1416 | return (kctl->kctlref); |
1417 | } |
1418 | |
1419 | static void |
1420 | kctl_delete_ref(kern_ctl_ref kctlref) |
1421 | { |
1422 | /* |
1423 | * Reference is index plus one |
1424 | */ |
1425 | uintptr_t i = (((uintptr_t)kctlref) & KCTLREF_INDEX_MASK) - 1; |
1426 | |
1427 | lck_mtx_assert(ctl_mtx, LCK_MTX_ASSERT_OWNED); |
1428 | |
1429 | if (i < kctl_tbl_size) { |
1430 | struct kctl *kctl = kctl_table[i]; |
1431 | |
1432 | if (kctl->kctlref == kctlref) { |
1433 | kctl_table[i] = NULL; |
1434 | kctl_tbl_count--; |
1435 | } else { |
1436 | kctlstat.kcs_bad_kctlref++; |
1437 | } |
1438 | } else { |
1439 | kctlstat.kcs_bad_kctlref++; |
1440 | } |
1441 | } |
1442 | |
1443 | static struct kctl * |
1444 | kctl_from_ref(kern_ctl_ref kctlref) |
1445 | { |
1446 | /* |
1447 | * Reference is index plus one |
1448 | */ |
1449 | uintptr_t i = (((uintptr_t)kctlref) & KCTLREF_INDEX_MASK) - 1; |
1450 | struct kctl *kctl = NULL; |
1451 | |
1452 | lck_mtx_assert(ctl_mtx, LCK_MTX_ASSERT_OWNED); |
1453 | |
1454 | if (i >= kctl_tbl_size) { |
1455 | kctlstat.kcs_bad_kctlref++; |
1456 | return (NULL); |
1457 | } |
1458 | kctl = kctl_table[i]; |
1459 | if (kctl->kctlref != kctlref) { |
1460 | kctlstat.kcs_bad_kctlref++; |
1461 | return (NULL); |
1462 | } |
1463 | return (kctl); |
1464 | } |
1465 | |
1466 | /* |
1467 | * Register/unregister a NKE |
1468 | */ |
1469 | errno_t |
1470 | ctl_register(struct kern_ctl_reg *userkctl, kern_ctl_ref *kctlref) |
1471 | { |
1472 | struct kctl *kctl = NULL; |
1473 | struct kctl *kctl_next = NULL; |
1474 | u_int32_t id = 1; |
1475 | size_t name_len; |
1476 | int is_extended = 0; |
1477 | |
1478 | if (userkctl == NULL) /* sanity check */ |
1479 | return (EINVAL); |
1480 | if (userkctl->ctl_connect == NULL) |
1481 | return (EINVAL); |
1482 | name_len = strlen(userkctl->ctl_name); |
1483 | if (name_len == 0 || name_len + 1 > MAX_KCTL_NAME) |
1484 | return (EINVAL); |
1485 | |
1486 | MALLOC(kctl, struct kctl *, sizeof(*kctl), M_TEMP, M_WAITOK); |
1487 | if (kctl == NULL) |
1488 | return (ENOMEM); |
1489 | bzero((char *)kctl, sizeof(*kctl)); |
1490 | |
1491 | lck_mtx_lock(ctl_mtx); |
1492 | |
1493 | if (kctl_make_ref(kctl) == NULL) { |
1494 | lck_mtx_unlock(ctl_mtx); |
1495 | FREE(kctl, M_TEMP); |
1496 | return (ENOMEM); |
1497 | } |
1498 | |
1499 | /* |
1500 | * Kernel Control IDs |
1501 | * |
1502 | * CTL_FLAG_REG_ID_UNIT indicates the control ID and unit number are |
1503 | * static. If they do not exist, add them to the list in order. If the |
1504 | * flag is not set, we must find a new unique value. We assume the |
1505 | * list is in order. We find the last item in the list and add one. If |
1506 | * this leads to wrapping the id around, we start at the front of the |
1507 | * list and look for a gap. |
1508 | */ |
1509 | |
1510 | if ((userkctl->ctl_flags & CTL_FLAG_REG_ID_UNIT) == 0) { |
1511 | /* Must dynamically assign an unused ID */ |
1512 | |
1513 | /* Verify the same name isn't already registered */ |
1514 | if (ctl_find_by_name(userkctl->ctl_name) != NULL) { |
1515 | kctl_delete_ref(kctl->kctlref); |
1516 | lck_mtx_unlock(ctl_mtx); |
1517 | FREE(kctl, M_TEMP); |
1518 | return (EEXIST); |
1519 | } |
1520 | |
1521 | /* Start with 1 in case the list is empty */ |
1522 | id = 1; |
1523 | kctl_next = TAILQ_LAST(&ctl_head, kctl_list); |
1524 | |
1525 | if (kctl_next != NULL) { |
1526 | /* List was not empty, add one to the last item */ |
1527 | id = kctl_next->id + 1; |
1528 | kctl_next = NULL; |
1529 | |
1530 | /* |
1531 | * If this wrapped the id number, start looking at |
1532 | * the front of the list for an unused id. |
1533 | */ |
1534 | if (id == 0) { |
1535 | /* Find the next unused ID */ |
1536 | id = 1; |
1537 | |
1538 | TAILQ_FOREACH(kctl_next, &ctl_head, next) { |
1539 | if (kctl_next->id > id) { |
1540 | /* We found a gap */ |
1541 | break; |
1542 | } |
1543 | |
1544 | id = kctl_next->id + 1; |
1545 | } |
1546 | } |
1547 | } |
1548 | |
1549 | userkctl->ctl_id = id; |
1550 | kctl->id = id; |
1551 | kctl->reg_unit = -1; |
1552 | } else { |
1553 | TAILQ_FOREACH(kctl_next, &ctl_head, next) { |
1554 | if (kctl_next->id > userkctl->ctl_id) |
1555 | break; |
1556 | } |
1557 | |
1558 | if (ctl_find_by_id_unit(userkctl->ctl_id, userkctl->ctl_unit)) { |
1559 | kctl_delete_ref(kctl->kctlref); |
1560 | lck_mtx_unlock(ctl_mtx); |
1561 | FREE(kctl, M_TEMP); |
1562 | return (EEXIST); |
1563 | } |
1564 | kctl->id = userkctl->ctl_id; |
1565 | kctl->reg_unit = userkctl->ctl_unit; |
1566 | } |
1567 | |
1568 | is_extended = (userkctl->ctl_flags & CTL_FLAG_REG_EXTENDED); |
1569 | |
1570 | strlcpy(kctl->name, userkctl->ctl_name, MAX_KCTL_NAME); |
1571 | kctl->flags = userkctl->ctl_flags; |
1572 | |
1573 | /* |
1574 | * Let the caller know the default send and receive sizes |
1575 | */ |
1576 | if (userkctl->ctl_sendsize == 0) { |
1577 | kctl->sendbufsize = CTL_SENDSIZE; |
1578 | userkctl->ctl_sendsize = kctl->sendbufsize; |
1579 | } else { |
1580 | kctl->sendbufsize = userkctl->ctl_sendsize; |
1581 | } |
1582 | if (userkctl->ctl_recvsize == 0) { |
1583 | kctl->recvbufsize = CTL_RECVSIZE; |
1584 | userkctl->ctl_recvsize = kctl->recvbufsize; |
1585 | } else { |
1586 | kctl->recvbufsize = userkctl->ctl_recvsize; |
1587 | } |
1588 | |
1589 | kctl->bind = userkctl->ctl_bind; |
1590 | kctl->connect = userkctl->ctl_connect; |
1591 | kctl->disconnect = userkctl->ctl_disconnect; |
1592 | kctl->send = userkctl->ctl_send; |
1593 | kctl->setopt = userkctl->ctl_setopt; |
1594 | kctl->getopt = userkctl->ctl_getopt; |
1595 | if (is_extended) { |
1596 | kctl->rcvd = userkctl->ctl_rcvd; |
1597 | kctl->send_list = userkctl->ctl_send_list; |
1598 | } |
1599 | |
1600 | TAILQ_INIT(&kctl->kcb_head); |
1601 | |
1602 | if (kctl_next) |
1603 | TAILQ_INSERT_BEFORE(kctl_next, kctl, next); |
1604 | else |
1605 | TAILQ_INSERT_TAIL(&ctl_head, kctl, next); |
1606 | |
1607 | kctlstat.kcs_reg_count++; |
1608 | kctlstat.kcs_gencnt++; |
1609 | |
1610 | lck_mtx_unlock(ctl_mtx); |
1611 | |
1612 | *kctlref = kctl->kctlref; |
1613 | |
1614 | ctl_post_msg(KEV_CTL_REGISTERED, kctl->id); |
1615 | return (0); |
1616 | } |
1617 | |
1618 | errno_t |
1619 | ctl_deregister(void *kctlref) |
1620 | { |
1621 | struct kctl *kctl; |
1622 | |
1623 | lck_mtx_lock(ctl_mtx); |
1624 | if ((kctl = kctl_from_ref(kctlref)) == NULL) { |
1625 | kctlstat.kcs_bad_kctlref++; |
1626 | lck_mtx_unlock(ctl_mtx); |
1627 | if (ctl_debug != 0) |
1628 | printf("%s invalid kctlref %p\n" , |
1629 | __func__, kctlref); |
1630 | return (EINVAL); |
1631 | } |
1632 | |
1633 | if (!TAILQ_EMPTY(&kctl->kcb_head)) { |
1634 | lck_mtx_unlock(ctl_mtx); |
1635 | return (EBUSY); |
1636 | } |
1637 | |
1638 | TAILQ_REMOVE(&ctl_head, kctl, next); |
1639 | |
1640 | kctlstat.kcs_reg_count--; |
1641 | kctlstat.kcs_gencnt++; |
1642 | |
1643 | kctl_delete_ref(kctl->kctlref); |
1644 | lck_mtx_unlock(ctl_mtx); |
1645 | |
1646 | ctl_post_msg(KEV_CTL_DEREGISTERED, kctl->id); |
1647 | FREE(kctl, M_TEMP); |
1648 | return (0); |
1649 | } |
1650 | |
1651 | /* |
1652 | * Must be called with global ctl_mtx lock taked |
1653 | */ |
1654 | static struct kctl * |
1655 | ctl_find_by_name(const char *name) |
1656 | { |
1657 | struct kctl *kctl; |
1658 | |
1659 | lck_mtx_assert(ctl_mtx, LCK_MTX_ASSERT_OWNED); |
1660 | |
1661 | TAILQ_FOREACH(kctl, &ctl_head, next) |
1662 | if (strncmp(kctl->name, name, sizeof(kctl->name)) == 0) |
1663 | return (kctl); |
1664 | |
1665 | return (NULL); |
1666 | } |
1667 | |
1668 | u_int32_t |
1669 | ctl_id_by_name(const char *name) |
1670 | { |
1671 | u_int32_t ctl_id = 0; |
1672 | struct kctl *kctl; |
1673 | |
1674 | lck_mtx_lock(ctl_mtx); |
1675 | kctl = ctl_find_by_name(name); |
1676 | if (kctl) |
1677 | ctl_id = kctl->id; |
1678 | lck_mtx_unlock(ctl_mtx); |
1679 | |
1680 | return (ctl_id); |
1681 | } |
1682 | |
1683 | errno_t |
1684 | ctl_name_by_id(u_int32_t id, char *out_name, size_t maxsize) |
1685 | { |
1686 | int found = 0; |
1687 | struct kctl *kctl; |
1688 | |
1689 | lck_mtx_lock(ctl_mtx); |
1690 | TAILQ_FOREACH(kctl, &ctl_head, next) { |
1691 | if (kctl->id == id) |
1692 | break; |
1693 | } |
1694 | |
1695 | if (kctl) { |
1696 | if (maxsize > MAX_KCTL_NAME) |
1697 | maxsize = MAX_KCTL_NAME; |
1698 | strlcpy(out_name, kctl->name, maxsize); |
1699 | found = 1; |
1700 | } |
1701 | lck_mtx_unlock(ctl_mtx); |
1702 | |
1703 | return (found ? 0 : ENOENT); |
1704 | } |
1705 | |
1706 | /* |
1707 | * Must be called with global ctl_mtx lock taked |
1708 | * |
1709 | */ |
1710 | static struct kctl * |
1711 | ctl_find_by_id_unit(u_int32_t id, u_int32_t unit) |
1712 | { |
1713 | struct kctl *kctl; |
1714 | |
1715 | lck_mtx_assert(ctl_mtx, LCK_MTX_ASSERT_OWNED); |
1716 | |
1717 | TAILQ_FOREACH(kctl, &ctl_head, next) { |
1718 | if (kctl->id == id && (kctl->flags & CTL_FLAG_REG_ID_UNIT) == 0) |
1719 | return (kctl); |
1720 | else if (kctl->id == id && kctl->reg_unit == unit) |
1721 | return (kctl); |
1722 | } |
1723 | return (NULL); |
1724 | } |
1725 | |
1726 | /* |
1727 | * Must be called with kernel controller lock taken |
1728 | */ |
1729 | static struct ctl_cb * |
1730 | kcb_find(struct kctl *kctl, u_int32_t unit) |
1731 | { |
1732 | struct ctl_cb *kcb; |
1733 | |
1734 | lck_mtx_assert(ctl_mtx, LCK_MTX_ASSERT_OWNED); |
1735 | |
1736 | TAILQ_FOREACH(kcb, &kctl->kcb_head, next) |
1737 | if (kcb->sac.sc_unit == unit) |
1738 | return (kcb); |
1739 | |
1740 | return (NULL); |
1741 | } |
1742 | |
1743 | static struct socket * |
1744 | kcb_find_socket(kern_ctl_ref kctlref, u_int32_t unit, u_int32_t *kctlflags) |
1745 | { |
1746 | struct socket *so = NULL; |
1747 | struct ctl_cb *kcb; |
1748 | void *lr_saved; |
1749 | struct kctl *kctl; |
1750 | int i; |
1751 | |
1752 | lr_saved = __builtin_return_address(0); |
1753 | |
1754 | lck_mtx_lock(ctl_mtx); |
1755 | /* |
1756 | * First validate the kctlref |
1757 | */ |
1758 | if ((kctl = kctl_from_ref(kctlref)) == NULL) { |
1759 | kctlstat.kcs_bad_kctlref++; |
1760 | lck_mtx_unlock(ctl_mtx); |
1761 | if (ctl_debug != 0) |
1762 | printf("%s invalid kctlref %p\n" , |
1763 | __func__, kctlref); |
1764 | return (NULL); |
1765 | } |
1766 | |
1767 | kcb = kcb_find(kctl, unit); |
1768 | if (kcb == NULL || kcb->kctl != kctl || (so = kcb->so) == NULL) { |
1769 | lck_mtx_unlock(ctl_mtx); |
1770 | return (NULL); |
1771 | } |
1772 | /* |
1773 | * This prevents the socket from being closed |
1774 | */ |
1775 | kcb->usecount++; |
1776 | /* |
1777 | * Respect lock ordering: socket before ctl_mtx |
1778 | */ |
1779 | lck_mtx_unlock(ctl_mtx); |
1780 | |
1781 | socket_lock(so, 1); |
1782 | /* |
1783 | * The socket lock history is more useful if we store |
1784 | * the address of the caller. |
1785 | */ |
1786 | i = (so->next_lock_lr + SO_LCKDBG_MAX - 1) % SO_LCKDBG_MAX; |
1787 | so->lock_lr[i] = lr_saved; |
1788 | |
1789 | lck_mtx_lock(ctl_mtx); |
1790 | |
1791 | if ((kctl = kctl_from_ref(kctlref)) == NULL || kcb->kctl == NULL) { |
1792 | lck_mtx_unlock(ctl_mtx); |
1793 | socket_unlock(so, 1); |
1794 | so = NULL; |
1795 | lck_mtx_lock(ctl_mtx); |
1796 | } else if (kctlflags != NULL) { |
1797 | *kctlflags = kctl->flags; |
1798 | } |
1799 | |
1800 | kcb->usecount--; |
1801 | if (kcb->usecount == 0) |
1802 | wakeup((event_t)&kcb->usecount); |
1803 | |
1804 | lck_mtx_unlock(ctl_mtx); |
1805 | |
1806 | return (so); |
1807 | } |
1808 | |
1809 | static void |
1810 | ctl_post_msg(u_int32_t event_code, u_int32_t id) |
1811 | { |
1812 | struct ctl_event_data ctl_ev_data; |
1813 | struct kev_msg ev_msg; |
1814 | |
1815 | lck_mtx_assert(ctl_mtx, LCK_MTX_ASSERT_NOTOWNED); |
1816 | |
1817 | bzero(&ev_msg, sizeof(struct kev_msg)); |
1818 | ev_msg.vendor_code = KEV_VENDOR_APPLE; |
1819 | |
1820 | ev_msg.kev_class = KEV_SYSTEM_CLASS; |
1821 | ev_msg.kev_subclass = KEV_CTL_SUBCLASS; |
1822 | ev_msg.event_code = event_code; |
1823 | |
1824 | /* common nke subclass data */ |
1825 | bzero(&ctl_ev_data, sizeof(ctl_ev_data)); |
1826 | ctl_ev_data.ctl_id = id; |
1827 | ev_msg.dv[0].data_ptr = &ctl_ev_data; |
1828 | ev_msg.dv[0].data_length = sizeof(ctl_ev_data); |
1829 | |
1830 | ev_msg.dv[1].data_length = 0; |
1831 | |
1832 | kev_post_msg(&ev_msg); |
1833 | } |
1834 | |
1835 | static int |
1836 | ctl_lock(struct socket *so, int refcount, void *lr) |
1837 | { |
1838 | void *lr_saved; |
1839 | |
1840 | if (lr == NULL) |
1841 | lr_saved = __builtin_return_address(0); |
1842 | else |
1843 | lr_saved = lr; |
1844 | |
1845 | if (so->so_pcb != NULL) { |
1846 | lck_mtx_lock(((struct ctl_cb *)so->so_pcb)->mtx); |
1847 | } else { |
1848 | panic("ctl_lock: so=%p NO PCB! lr=%p lrh= %s\n" , |
1849 | so, lr_saved, solockhistory_nr(so)); |
1850 | /* NOTREACHED */ |
1851 | } |
1852 | |
1853 | if (so->so_usecount < 0) { |
1854 | panic("ctl_lock: so=%p so_pcb=%p lr=%p ref=%x lrh= %s\n" , |
1855 | so, so->so_pcb, lr_saved, so->so_usecount, |
1856 | solockhistory_nr(so)); |
1857 | /* NOTREACHED */ |
1858 | } |
1859 | |
1860 | if (refcount) |
1861 | so->so_usecount++; |
1862 | |
1863 | so->lock_lr[so->next_lock_lr] = lr_saved; |
1864 | so->next_lock_lr = (so->next_lock_lr+1) % SO_LCKDBG_MAX; |
1865 | return (0); |
1866 | } |
1867 | |
1868 | static int |
1869 | ctl_unlock(struct socket *so, int refcount, void *lr) |
1870 | { |
1871 | void *lr_saved; |
1872 | lck_mtx_t *mutex_held; |
1873 | |
1874 | if (lr == NULL) |
1875 | lr_saved = __builtin_return_address(0); |
1876 | else |
1877 | lr_saved = lr; |
1878 | |
1879 | #if (MORE_KCTLLOCK_DEBUG && (DEVELOPMENT || DEBUG)) |
1880 | printf("ctl_unlock: so=%llx sopcb=%x lock=%llx ref=%u lr=%llx\n" , |
1881 | (uint64_t)VM_KERNEL_ADDRPERM(so), |
1882 | (uint64_t)VM_KERNEL_ADDRPERM(so->so_pcb, |
1883 | (uint64_t)VM_KERNEL_ADDRPERM(((struct ctl_cb *)so->so_pcb)->mtx), |
1884 | so->so_usecount, (uint64_t)VM_KERNEL_ADDRPERM(lr_saved)); |
1885 | #endif /* (MORE_KCTLLOCK_DEBUG && (DEVELOPMENT || DEBUG)) */ |
1886 | if (refcount) |
1887 | so->so_usecount--; |
1888 | |
1889 | if (so->so_usecount < 0) { |
1890 | panic("ctl_unlock: so=%p usecount=%x lrh= %s\n" , |
1891 | so, so->so_usecount, solockhistory_nr(so)); |
1892 | /* NOTREACHED */ |
1893 | } |
1894 | if (so->so_pcb == NULL) { |
1895 | panic("ctl_unlock: so=%p NO PCB usecount=%x lr=%p lrh= %s\n" , |
1896 | so, so->so_usecount, (void *)lr_saved, |
1897 | solockhistory_nr(so)); |
1898 | /* NOTREACHED */ |
1899 | } |
1900 | mutex_held = ((struct ctl_cb *)so->so_pcb)->mtx; |
1901 | |
1902 | lck_mtx_assert(mutex_held, LCK_MTX_ASSERT_OWNED); |
1903 | so->unlock_lr[so->next_unlock_lr] = lr_saved; |
1904 | so->next_unlock_lr = (so->next_unlock_lr+1) % SO_LCKDBG_MAX; |
1905 | lck_mtx_unlock(mutex_held); |
1906 | |
1907 | if (so->so_usecount == 0) |
1908 | ctl_sofreelastref(so); |
1909 | |
1910 | return (0); |
1911 | } |
1912 | |
1913 | static lck_mtx_t * |
1914 | ctl_getlock(struct socket *so, int flags) |
1915 | { |
1916 | #pragma unused(flags) |
1917 | struct ctl_cb *kcb = (struct ctl_cb *)so->so_pcb; |
1918 | |
1919 | if (so->so_pcb) { |
1920 | if (so->so_usecount < 0) |
1921 | panic("ctl_getlock: so=%p usecount=%x lrh= %s\n" , |
1922 | so, so->so_usecount, solockhistory_nr(so)); |
1923 | return (kcb->mtx); |
1924 | } else { |
1925 | panic("ctl_getlock: so=%p NULL NO so_pcb %s\n" , |
1926 | so, solockhistory_nr(so)); |
1927 | return (so->so_proto->pr_domain->dom_mtx); |
1928 | } |
1929 | } |
1930 | |
1931 | __private_extern__ int |
1932 | kctl_reg_list SYSCTL_HANDLER_ARGS |
1933 | { |
1934 | #pragma unused(oidp, arg1, arg2) |
1935 | int error = 0; |
1936 | int n, i; |
1937 | struct xsystmgen xsg; |
1938 | void *buf = NULL; |
1939 | struct kctl *kctl; |
1940 | size_t item_size = ROUNDUP64(sizeof (struct xkctl_reg)); |
1941 | |
1942 | buf = _MALLOC(item_size, M_TEMP, M_WAITOK | M_ZERO); |
1943 | if (buf == NULL) |
1944 | return (ENOMEM); |
1945 | |
1946 | lck_mtx_lock(ctl_mtx); |
1947 | |
1948 | n = kctlstat.kcs_reg_count; |
1949 | |
1950 | if (req->oldptr == USER_ADDR_NULL) { |
1951 | req->oldidx = (n + n/8) * sizeof(struct xkctl_reg); |
1952 | goto done; |
1953 | } |
1954 | if (req->newptr != USER_ADDR_NULL) { |
1955 | error = EPERM; |
1956 | goto done; |
1957 | } |
1958 | bzero(&xsg, sizeof (xsg)); |
1959 | xsg.xg_len = sizeof (xsg); |
1960 | xsg.xg_count = n; |
1961 | xsg.xg_gen = kctlstat.kcs_gencnt; |
1962 | xsg.xg_sogen = so_gencnt; |
1963 | error = SYSCTL_OUT(req, &xsg, sizeof (xsg)); |
1964 | if (error) { |
1965 | goto done; |
1966 | } |
1967 | /* |
1968 | * We are done if there is no pcb |
1969 | */ |
1970 | if (n == 0) { |
1971 | goto done; |
1972 | } |
1973 | |
1974 | i = 0; |
1975 | for (i = 0, kctl = TAILQ_FIRST(&ctl_head); |
1976 | i < n && kctl != NULL; |
1977 | i++, kctl = TAILQ_NEXT(kctl, next)) { |
1978 | struct xkctl_reg *xkr = (struct xkctl_reg *)buf; |
1979 | struct ctl_cb *kcb; |
1980 | u_int32_t pcbcount = 0; |
1981 | |
1982 | TAILQ_FOREACH(kcb, &kctl->kcb_head, next) |
1983 | pcbcount++; |
1984 | |
1985 | bzero(buf, item_size); |
1986 | |
1987 | xkr->xkr_len = sizeof(struct xkctl_reg); |
1988 | xkr->xkr_kind = XSO_KCREG; |
1989 | xkr->xkr_id = kctl->id; |
1990 | xkr->xkr_reg_unit = kctl->reg_unit; |
1991 | xkr->xkr_flags = kctl->flags; |
1992 | xkr->xkr_kctlref = (uint64_t)(kctl->kctlref); |
1993 | xkr->xkr_recvbufsize = kctl->recvbufsize; |
1994 | xkr->xkr_sendbufsize = kctl->sendbufsize; |
1995 | xkr->xkr_lastunit = kctl->lastunit; |
1996 | xkr->xkr_pcbcount = pcbcount; |
1997 | xkr->xkr_connect = (uint64_t)VM_KERNEL_UNSLIDE(kctl->connect); |
1998 | xkr->xkr_disconnect = |
1999 | (uint64_t)VM_KERNEL_UNSLIDE(kctl->disconnect); |
2000 | xkr->xkr_send = (uint64_t)VM_KERNEL_UNSLIDE(kctl->send); |
2001 | xkr->xkr_send_list = |
2002 | (uint64_t)VM_KERNEL_UNSLIDE(kctl->send_list); |
2003 | xkr->xkr_setopt = (uint64_t)VM_KERNEL_UNSLIDE(kctl->setopt); |
2004 | xkr->xkr_getopt = (uint64_t)VM_KERNEL_UNSLIDE(kctl->getopt); |
2005 | xkr->xkr_rcvd = (uint64_t)VM_KERNEL_UNSLIDE(kctl->rcvd); |
2006 | strlcpy(xkr->xkr_name, kctl->name, sizeof(xkr->xkr_name)); |
2007 | |
2008 | error = SYSCTL_OUT(req, buf, item_size); |
2009 | } |
2010 | |
2011 | if (error == 0) { |
2012 | /* |
2013 | * Give the user an updated idea of our state. |
2014 | * If the generation differs from what we told |
2015 | * her before, she knows that something happened |
2016 | * while we were processing this request, and it |
2017 | * might be necessary to retry. |
2018 | */ |
2019 | bzero(&xsg, sizeof (xsg)); |
2020 | xsg.xg_len = sizeof (xsg); |
2021 | xsg.xg_count = n; |
2022 | xsg.xg_gen = kctlstat.kcs_gencnt; |
2023 | xsg.xg_sogen = so_gencnt; |
2024 | error = SYSCTL_OUT(req, &xsg, sizeof (xsg)); |
2025 | if (error) { |
2026 | goto done; |
2027 | } |
2028 | } |
2029 | |
2030 | done: |
2031 | lck_mtx_unlock(ctl_mtx); |
2032 | |
2033 | if (buf != NULL) |
2034 | FREE(buf, M_TEMP); |
2035 | |
2036 | return (error); |
2037 | } |
2038 | |
2039 | __private_extern__ int |
2040 | kctl_pcblist SYSCTL_HANDLER_ARGS |
2041 | { |
2042 | #pragma unused(oidp, arg1, arg2) |
2043 | int error = 0; |
2044 | int n, i; |
2045 | struct xsystmgen xsg; |
2046 | void *buf = NULL; |
2047 | struct kctl *kctl; |
2048 | size_t item_size = ROUNDUP64(sizeof (struct xkctlpcb)) + |
2049 | ROUNDUP64(sizeof (struct xsocket_n)) + |
2050 | 2 * ROUNDUP64(sizeof (struct xsockbuf_n)) + |
2051 | ROUNDUP64(sizeof (struct xsockstat_n)); |
2052 | |
2053 | buf = _MALLOC(item_size, M_TEMP, M_WAITOK | M_ZERO); |
2054 | if (buf == NULL) |
2055 | return (ENOMEM); |
2056 | |
2057 | lck_mtx_lock(ctl_mtx); |
2058 | |
2059 | n = kctlstat.kcs_pcbcount; |
2060 | |
2061 | if (req->oldptr == USER_ADDR_NULL) { |
2062 | req->oldidx = (n + n/8) * item_size; |
2063 | goto done; |
2064 | } |
2065 | if (req->newptr != USER_ADDR_NULL) { |
2066 | error = EPERM; |
2067 | goto done; |
2068 | } |
2069 | bzero(&xsg, sizeof (xsg)); |
2070 | xsg.xg_len = sizeof (xsg); |
2071 | xsg.xg_count = n; |
2072 | xsg.xg_gen = kctlstat.kcs_gencnt; |
2073 | xsg.xg_sogen = so_gencnt; |
2074 | error = SYSCTL_OUT(req, &xsg, sizeof (xsg)); |
2075 | if (error) { |
2076 | goto done; |
2077 | } |
2078 | /* |
2079 | * We are done if there is no pcb |
2080 | */ |
2081 | if (n == 0) { |
2082 | goto done; |
2083 | } |
2084 | |
2085 | i = 0; |
2086 | for (i = 0, kctl = TAILQ_FIRST(&ctl_head); |
2087 | i < n && kctl != NULL; |
2088 | kctl = TAILQ_NEXT(kctl, next)) { |
2089 | struct ctl_cb *kcb; |
2090 | |
2091 | for (kcb = TAILQ_FIRST(&kctl->kcb_head); |
2092 | i < n && kcb != NULL; |
2093 | i++, kcb = TAILQ_NEXT(kcb, next)) { |
2094 | struct xkctlpcb *xk = (struct xkctlpcb *)buf; |
2095 | struct xsocket_n *xso = (struct xsocket_n *) |
2096 | ADVANCE64(xk, sizeof (*xk)); |
2097 | struct xsockbuf_n *xsbrcv = (struct xsockbuf_n *) |
2098 | ADVANCE64(xso, sizeof (*xso)); |
2099 | struct xsockbuf_n *xsbsnd = (struct xsockbuf_n *) |
2100 | ADVANCE64(xsbrcv, sizeof (*xsbrcv)); |
2101 | struct xsockstat_n *xsostats = (struct xsockstat_n *) |
2102 | ADVANCE64(xsbsnd, sizeof (*xsbsnd)); |
2103 | |
2104 | bzero(buf, item_size); |
2105 | |
2106 | xk->xkp_len = sizeof(struct xkctlpcb); |
2107 | xk->xkp_kind = XSO_KCB; |
2108 | xk->xkp_unit = kcb->sac.sc_unit; |
2109 | xk->xkp_kctpcb = (uint64_t)VM_KERNEL_ADDRPERM(kcb); |
2110 | xk->xkp_kctlref = (uint64_t)VM_KERNEL_ADDRPERM(kctl); |
2111 | xk->xkp_kctlid = kctl->id; |
2112 | strlcpy(xk->xkp_kctlname, kctl->name, |
2113 | sizeof(xk->xkp_kctlname)); |
2114 | |
2115 | sotoxsocket_n(kcb->so, xso); |
2116 | sbtoxsockbuf_n(kcb->so ? |
2117 | &kcb->so->so_rcv : NULL, xsbrcv); |
2118 | sbtoxsockbuf_n(kcb->so ? |
2119 | &kcb->so->so_snd : NULL, xsbsnd); |
2120 | sbtoxsockstat_n(kcb->so, xsostats); |
2121 | |
2122 | error = SYSCTL_OUT(req, buf, item_size); |
2123 | } |
2124 | } |
2125 | |
2126 | if (error == 0) { |
2127 | /* |
2128 | * Give the user an updated idea of our state. |
2129 | * If the generation differs from what we told |
2130 | * her before, she knows that something happened |
2131 | * while we were processing this request, and it |
2132 | * might be necessary to retry. |
2133 | */ |
2134 | bzero(&xsg, sizeof (xsg)); |
2135 | xsg.xg_len = sizeof (xsg); |
2136 | xsg.xg_count = n; |
2137 | xsg.xg_gen = kctlstat.kcs_gencnt; |
2138 | xsg.xg_sogen = so_gencnt; |
2139 | error = SYSCTL_OUT(req, &xsg, sizeof (xsg)); |
2140 | if (error) { |
2141 | goto done; |
2142 | } |
2143 | } |
2144 | |
2145 | done: |
2146 | lck_mtx_unlock(ctl_mtx); |
2147 | |
2148 | return (error); |
2149 | } |
2150 | |
2151 | int |
2152 | kctl_getstat SYSCTL_HANDLER_ARGS |
2153 | { |
2154 | #pragma unused(oidp, arg1, arg2) |
2155 | int error = 0; |
2156 | |
2157 | lck_mtx_lock(ctl_mtx); |
2158 | |
2159 | if (req->newptr != USER_ADDR_NULL) { |
2160 | error = EPERM; |
2161 | goto done; |
2162 | } |
2163 | if (req->oldptr == USER_ADDR_NULL) { |
2164 | req->oldidx = sizeof(struct kctlstat); |
2165 | goto done; |
2166 | } |
2167 | |
2168 | error = SYSCTL_OUT(req, &kctlstat, |
2169 | MIN(sizeof(struct kctlstat), req->oldlen)); |
2170 | done: |
2171 | lck_mtx_unlock(ctl_mtx); |
2172 | return (error); |
2173 | } |
2174 | |
2175 | void |
2176 | kctl_fill_socketinfo(struct socket *so, struct socket_info *si) |
2177 | { |
2178 | struct ctl_cb *kcb = (struct ctl_cb *)so->so_pcb; |
2179 | struct kern_ctl_info *kcsi = |
2180 | &si->soi_proto.pri_kern_ctl; |
2181 | struct kctl *kctl = kcb->kctl; |
2182 | |
2183 | si->soi_kind = SOCKINFO_KERN_CTL; |
2184 | |
2185 | if (kctl == 0) |
2186 | return; |
2187 | |
2188 | kcsi->kcsi_id = kctl->id; |
2189 | kcsi->kcsi_reg_unit = kctl->reg_unit; |
2190 | kcsi->kcsi_flags = kctl->flags; |
2191 | kcsi->kcsi_recvbufsize = kctl->recvbufsize; |
2192 | kcsi->kcsi_sendbufsize = kctl->sendbufsize; |
2193 | kcsi->kcsi_unit = kcb->sac.sc_unit; |
2194 | strlcpy(kcsi->kcsi_name, kctl->name, MAX_KCTL_NAME); |
2195 | } |
2196 | |