]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
fe8ab488 | 2 | * Copyright (c) 2000-2014 Apple Inc. All rights reserved. |
5d5c5d0d | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 5 | * |
2d21ac55 A |
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. | |
8f6c56a5 | 14 | * |
2d21ac55 A |
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 | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
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. | |
8f6c56a5 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
27 | */ |
28 | /* | |
29 | * Copyright (c) 1982, 1986, 1989, 1991, 1993 | |
30 | * The Regents of the University of California. All rights reserved. | |
31 | * | |
32 | * Redistribution and use in source and binary forms, with or without | |
33 | * modification, are permitted provided that the following conditions | |
34 | * are met: | |
35 | * 1. Redistributions of source code must retain the above copyright | |
36 | * notice, this list of conditions and the following disclaimer. | |
37 | * 2. Redistributions in binary form must reproduce the above copyright | |
38 | * notice, this list of conditions and the following disclaimer in the | |
39 | * documentation and/or other materials provided with the distribution. | |
40 | * 3. All advertising materials mentioning features or use of this software | |
41 | * must display the following acknowledgement: | |
42 | * This product includes software developed by the University of | |
43 | * California, Berkeley and its contributors. | |
44 | * 4. Neither the name of the University nor the names of its contributors | |
45 | * may be used to endorse or promote products derived from this software | |
46 | * without specific prior written permission. | |
47 | * | |
48 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
49 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
50 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
51 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
52 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
53 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
54 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
55 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
56 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
57 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
58 | * SUCH DAMAGE. | |
59 | * | |
60 | * From: @(#)uipc_usrreq.c 8.3 (Berkeley) 1/4/94 | |
61 | */ | |
2d21ac55 A |
62 | /* |
63 | * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce | |
64 | * support for mandatory and extensible security protections. This notice | |
65 | * is included in support of clause 2.2 (b) of the Apple Public License, | |
66 | * Version 2.0. | |
67 | */ | |
1c79356b A |
68 | |
69 | #include <sys/param.h> | |
70 | #include <sys/systm.h> | |
71 | #include <sys/kernel.h> | |
72 | #include <sys/domain.h> | |
73 | #include <sys/fcntl.h> | |
74 | #include <sys/malloc.h> /* XXX must be before <sys/file.h> */ | |
91447636 | 75 | #include <sys/file_internal.h> |
39236c6e | 76 | #include <sys/guarded.h> |
1c79356b A |
77 | #include <sys/filedesc.h> |
78 | #include <sys/lock.h> | |
79 | #include <sys/mbuf.h> | |
80 | #include <sys/namei.h> | |
91447636 A |
81 | #include <sys/proc_internal.h> |
82 | #include <sys/kauth.h> | |
1c79356b A |
83 | #include <sys/protosw.h> |
84 | #include <sys/socket.h> | |
85 | #include <sys/socketvar.h> | |
86 | #include <sys/stat.h> | |
87 | #include <sys/sysctl.h> | |
88 | #include <sys/un.h> | |
89 | #include <sys/unpcb.h> | |
91447636 A |
90 | #include <sys/vnode_internal.h> |
91 | #include <sys/kdebug.h> | |
3e170ce0 | 92 | #include <sys/mcache.h> |
1c79356b A |
93 | |
94 | #include <kern/zalloc.h> | |
91447636 | 95 | #include <kern/locks.h> |
1c79356b | 96 | |
b0d623f7 | 97 | #if CONFIG_MACF |
2d21ac55 | 98 | #include <security/mac_framework.h> |
b0d623f7 | 99 | #endif /* CONFIG_MACF */ |
2d21ac55 | 100 | |
316670eb A |
101 | #include <mach/vm_param.h> |
102 | ||
3e170ce0 A |
103 | /* |
104 | * Maximum number of FDs that can be passed in an mbuf | |
105 | */ | |
106 | #define UIPC_MAX_CMSG_FD 512 | |
107 | ||
2d21ac55 A |
108 | #define f_msgcount f_fglob->fg_msgcount |
109 | #define f_cred f_fglob->fg_cred | |
110 | #define f_ops f_fglob->fg_ops | |
111 | #define f_offset f_fglob->fg_offset | |
112 | #define f_data f_fglob->fg_data | |
1c79356b A |
113 | struct zone *unp_zone; |
114 | static unp_gen_t unp_gencnt; | |
115 | static u_int unp_count; | |
37839358 | 116 | |
2d21ac55 A |
117 | static lck_attr_t *unp_mtx_attr; |
118 | static lck_grp_t *unp_mtx_grp; | |
119 | static lck_grp_attr_t *unp_mtx_grp_attr; | |
120 | static lck_rw_t *unp_list_mtx; | |
1c79356b | 121 | |
b0d623f7 A |
122 | static lck_mtx_t *unp_disconnect_lock; |
123 | static lck_mtx_t *unp_connect_lock; | |
124 | static u_int disconnect_in_progress; | |
125 | ||
2d21ac55 | 126 | extern lck_mtx_t *uipc_lock; |
1c79356b A |
127 | static struct unp_head unp_shead, unp_dhead; |
128 | ||
6d2010ae A |
129 | /* |
130 | * mDNSResponder tracing. When enabled, endpoints connected to | |
131 | * /var/run/mDNSResponder will be traced; during each send on | |
132 | * the traced socket, we log the PID and process name of the | |
133 | * sending process. We also print out a bit of info related | |
134 | * to the data itself; this assumes ipc_msg_hdr in dnssd_ipc.h | |
135 | * of mDNSResponder stays the same. | |
136 | */ | |
137 | #define MDNSRESPONDER_PATH "/var/run/mDNSResponder" | |
138 | ||
139 | static int unpst_tracemdns; /* enable tracing */ | |
140 | ||
141 | #define MDNS_IPC_MSG_HDR_VERSION_1 1 | |
142 | ||
143 | struct mdns_ipc_msg_hdr { | |
144 | uint32_t version; | |
145 | uint32_t datalen; | |
146 | uint32_t ipc_flags; | |
147 | uint32_t op; | |
148 | union { | |
149 | void *context; | |
150 | uint32_t u32[2]; | |
151 | } __attribute__((packed)); | |
152 | uint32_t reg_index; | |
153 | } __attribute__((packed)); | |
154 | ||
1c79356b A |
155 | /* |
156 | * Unix communications domain. | |
157 | * | |
158 | * TODO: | |
159 | * SEQPACKET, RDM | |
160 | * rethink name space problems | |
161 | * need a proper out-of-band | |
162 | * lock pushdown | |
163 | */ | |
2d21ac55 | 164 | static struct sockaddr sun_noname = { sizeof (sun_noname), AF_LOCAL, { 0 } }; |
1c79356b A |
165 | static ino_t unp_ino; /* prototype for fake inode numbers */ |
166 | ||
2d21ac55 A |
167 | static int unp_attach(struct socket *); |
168 | static void unp_detach(struct unpcb *); | |
169 | static int unp_bind(struct unpcb *, struct sockaddr *, proc_t); | |
170 | static int unp_connect(struct socket *, struct sockaddr *, proc_t); | |
171 | static void unp_disconnect(struct unpcb *); | |
172 | static void unp_shutdown(struct unpcb *); | |
173 | static void unp_drop(struct unpcb *, int); | |
e2fac8b1 | 174 | __private_extern__ void unp_gc(void); |
3e170ce0 A |
175 | static void unp_scan(struct mbuf *, void (*)(struct fileglob *, void *arg), void *arg); |
176 | static void unp_mark(struct fileglob *, __unused void *); | |
177 | static void unp_discard(struct fileglob *, void *); | |
2d21ac55 A |
178 | static int unp_internalize(struct mbuf *, proc_t); |
179 | static int unp_listen(struct unpcb *, proc_t); | |
b0d623f7 A |
180 | static void unpcb_to_compat(struct unpcb *, struct unpcb_compat *); |
181 | static void unp_get_locks_in_order(struct socket *so, struct socket *conn_so); | |
182 | ||
183 | static void | |
184 | unp_get_locks_in_order(struct socket *so, struct socket *conn_so) | |
185 | { | |
186 | if (so < conn_so) { | |
187 | socket_lock(conn_so, 1); | |
188 | } else { | |
189 | struct unpcb *unp = sotounpcb(so); | |
190 | unp->unp_flags |= UNP_DONTDISCONNECT; | |
191 | unp->rw_thrcount++; | |
192 | socket_unlock(so, 0); | |
2d21ac55 | 193 | |
b0d623f7 A |
194 | /* Get the locks in the correct order */ |
195 | socket_lock(conn_so, 1); | |
196 | socket_lock(so, 0); | |
197 | unp->rw_thrcount--; | |
198 | if (unp->rw_thrcount == 0) { | |
199 | unp->unp_flags &= ~UNP_DONTDISCONNECT; | |
200 | wakeup(unp); | |
201 | } | |
202 | } | |
203 | } | |
1c79356b A |
204 | |
205 | static int | |
206 | uipc_abort(struct socket *so) | |
207 | { | |
208 | struct unpcb *unp = sotounpcb(so); | |
209 | ||
210 | if (unp == 0) | |
2d21ac55 | 211 | return (EINVAL); |
1c79356b | 212 | unp_drop(unp, ECONNABORTED); |
91447636 A |
213 | unp_detach(unp); |
214 | sofree(so); | |
2d21ac55 | 215 | return (0); |
1c79356b A |
216 | } |
217 | ||
218 | static int | |
219 | uipc_accept(struct socket *so, struct sockaddr **nam) | |
220 | { | |
221 | struct unpcb *unp = sotounpcb(so); | |
222 | ||
223 | if (unp == 0) | |
2d21ac55 | 224 | return (EINVAL); |
1c79356b A |
225 | |
226 | /* | |
227 | * Pass back name of connected socket, | |
228 | * if it was bound and we are still connected | |
229 | * (our peer may have closed already!). | |
230 | */ | |
231 | if (unp->unp_conn && unp->unp_conn->unp_addr) { | |
2d21ac55 A |
232 | *nam = dup_sockaddr((struct sockaddr *) |
233 | unp->unp_conn->unp_addr, 1); | |
1c79356b A |
234 | } else { |
235 | *nam = dup_sockaddr((struct sockaddr *)&sun_noname, 1); | |
236 | } | |
2d21ac55 | 237 | return (0); |
1c79356b A |
238 | } |
239 | ||
2d21ac55 A |
240 | /* |
241 | * Returns: 0 Success | |
242 | * EISCONN | |
243 | * unp_attach: | |
244 | */ | |
1c79356b | 245 | static int |
2d21ac55 | 246 | uipc_attach(struct socket *so, __unused int proto, __unused proc_t p) |
1c79356b A |
247 | { |
248 | struct unpcb *unp = sotounpcb(so); | |
249 | ||
250 | if (unp != 0) | |
2d21ac55 A |
251 | return (EISCONN); |
252 | return (unp_attach(so)); | |
1c79356b A |
253 | } |
254 | ||
255 | static int | |
2d21ac55 | 256 | uipc_bind(struct socket *so, struct sockaddr *nam, proc_t p) |
1c79356b A |
257 | { |
258 | struct unpcb *unp = sotounpcb(so); | |
259 | ||
260 | if (unp == 0) | |
2d21ac55 | 261 | return (EINVAL); |
1c79356b | 262 | |
2d21ac55 | 263 | return (unp_bind(unp, nam, p)); |
1c79356b A |
264 | } |
265 | ||
2d21ac55 A |
266 | /* |
267 | * Returns: 0 Success | |
268 | * EINVAL | |
269 | * unp_connect:??? [See elsewhere in this file] | |
270 | */ | |
1c79356b | 271 | static int |
2d21ac55 | 272 | uipc_connect(struct socket *so, struct sockaddr *nam, proc_t p) |
1c79356b A |
273 | { |
274 | struct unpcb *unp = sotounpcb(so); | |
275 | ||
276 | if (unp == 0) | |
2d21ac55 A |
277 | return (EINVAL); |
278 | return (unp_connect(so, nam, p)); | |
1c79356b A |
279 | } |
280 | ||
2d21ac55 A |
281 | /* |
282 | * Returns: 0 Success | |
283 | * EINVAL | |
284 | * unp_connect2:EPROTOTYPE Protocol wrong type for socket | |
285 | * unp_connect2:EINVAL Invalid argument | |
286 | */ | |
1c79356b A |
287 | static int |
288 | uipc_connect2(struct socket *so1, struct socket *so2) | |
289 | { | |
290 | struct unpcb *unp = sotounpcb(so1); | |
291 | ||
292 | if (unp == 0) | |
2d21ac55 | 293 | return (EINVAL); |
1c79356b | 294 | |
2d21ac55 | 295 | return (unp_connect2(so1, so2)); |
1c79356b A |
296 | } |
297 | ||
298 | /* control is EOPNOTSUPP */ | |
299 | ||
300 | static int | |
301 | uipc_detach(struct socket *so) | |
302 | { | |
303 | struct unpcb *unp = sotounpcb(so); | |
304 | ||
305 | if (unp == 0) | |
2d21ac55 | 306 | return (EINVAL); |
1c79356b | 307 | |
6d2010ae | 308 | lck_mtx_assert(&unp->unp_mtx, LCK_MTX_ASSERT_OWNED); |
1c79356b | 309 | unp_detach(unp); |
2d21ac55 | 310 | return (0); |
1c79356b A |
311 | } |
312 | ||
313 | static int | |
314 | uipc_disconnect(struct socket *so) | |
315 | { | |
316 | struct unpcb *unp = sotounpcb(so); | |
317 | ||
318 | if (unp == 0) | |
2d21ac55 | 319 | return (EINVAL); |
1c79356b | 320 | unp_disconnect(unp); |
2d21ac55 | 321 | return (0); |
1c79356b A |
322 | } |
323 | ||
2d21ac55 A |
324 | /* |
325 | * Returns: 0 Success | |
326 | * EINVAL | |
327 | */ | |
1c79356b | 328 | static int |
2d21ac55 | 329 | uipc_listen(struct socket *so, __unused proc_t p) |
1c79356b A |
330 | { |
331 | struct unpcb *unp = sotounpcb(so); | |
332 | ||
333 | if (unp == 0 || unp->unp_vnode == 0) | |
2d21ac55 A |
334 | return (EINVAL); |
335 | return (unp_listen(unp, p)); | |
1c79356b A |
336 | } |
337 | ||
338 | static int | |
339 | uipc_peeraddr(struct socket *so, struct sockaddr **nam) | |
340 | { | |
341 | struct unpcb *unp = sotounpcb(so); | |
342 | ||
2d21ac55 A |
343 | if (unp == NULL) |
344 | return (EINVAL); | |
345 | if (unp->unp_conn != NULL && unp->unp_conn->unp_addr != NULL) { | |
346 | *nam = dup_sockaddr((struct sockaddr *) | |
347 | unp->unp_conn->unp_addr, 1); | |
348 | } else { | |
349 | *nam = dup_sockaddr((struct sockaddr *)&sun_noname, 1); | |
350 | } | |
351 | return (0); | |
1c79356b A |
352 | } |
353 | ||
354 | static int | |
91447636 | 355 | uipc_rcvd(struct socket *so, __unused int flags) |
1c79356b A |
356 | { |
357 | struct unpcb *unp = sotounpcb(so); | |
358 | struct socket *so2; | |
359 | ||
360 | if (unp == 0) | |
2d21ac55 | 361 | return (EINVAL); |
1c79356b A |
362 | switch (so->so_type) { |
363 | case SOCK_DGRAM: | |
364 | panic("uipc_rcvd DGRAM?"); | |
365 | /*NOTREACHED*/ | |
366 | ||
367 | case SOCK_STREAM: | |
368 | #define rcv (&so->so_rcv) | |
2d21ac55 | 369 | #define snd (&so2->so_snd) |
1c79356b A |
370 | if (unp->unp_conn == 0) |
371 | break; | |
b0d623f7 | 372 | |
1c79356b | 373 | so2 = unp->unp_conn->unp_socket; |
b0d623f7 | 374 | unp_get_locks_in_order(so, so2); |
1c79356b A |
375 | /* |
376 | * Adjust backpressure on sender | |
377 | * and wakeup any waiting to write. | |
378 | */ | |
379 | snd->sb_mbmax += unp->unp_mbcnt - rcv->sb_mbcnt; | |
380 | unp->unp_mbcnt = rcv->sb_mbcnt; | |
381 | snd->sb_hiwat += unp->unp_cc - rcv->sb_cc; | |
382 | unp->unp_cc = rcv->sb_cc; | |
383 | sowwakeup(so2); | |
b0d623f7 A |
384 | |
385 | socket_unlock(so2, 1); | |
386 | ||
1c79356b A |
387 | #undef snd |
388 | #undef rcv | |
389 | break; | |
390 | ||
391 | default: | |
392 | panic("uipc_rcvd unknown socktype"); | |
393 | } | |
2d21ac55 | 394 | return (0); |
1c79356b A |
395 | } |
396 | ||
397 | /* pru_rcvoob is EOPNOTSUPP */ | |
398 | ||
2d21ac55 A |
399 | /* |
400 | * Returns: 0 Success | |
401 | * EINVAL | |
402 | * EOPNOTSUPP | |
403 | * EPIPE | |
404 | * ENOTCONN | |
405 | * EISCONN | |
406 | * unp_internalize:EINVAL | |
407 | * unp_internalize:EBADF | |
408 | * unp_connect:EAFNOSUPPORT Address family not supported | |
409 | * unp_connect:EINVAL Invalid argument | |
410 | * unp_connect:ENOTSOCK Not a socket | |
411 | * unp_connect:ECONNREFUSED Connection refused | |
412 | * unp_connect:EISCONN Socket is connected | |
413 | * unp_connect:EPROTOTYPE Protocol wrong type for socket | |
414 | * unp_connect:??? | |
415 | * sbappendaddr:ENOBUFS [5th argument, contents modified] | |
416 | * sbappendaddr:??? [whatever a filter author chooses] | |
417 | */ | |
1c79356b A |
418 | static int |
419 | uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, | |
2d21ac55 | 420 | struct mbuf *control, proc_t p) |
1c79356b A |
421 | { |
422 | int error = 0; | |
423 | struct unpcb *unp = sotounpcb(so); | |
424 | struct socket *so2; | |
425 | ||
426 | if (unp == 0) { | |
427 | error = EINVAL; | |
428 | goto release; | |
429 | } | |
430 | if (flags & PRUS_OOB) { | |
431 | error = EOPNOTSUPP; | |
432 | goto release; | |
433 | } | |
434 | ||
13fec989 | 435 | if (control) { |
b0d623f7 | 436 | /* release lock to avoid deadlock (4436174) */ |
2d21ac55 | 437 | socket_unlock(so, 0); |
13fec989 A |
438 | error = unp_internalize(control, p); |
439 | socket_lock(so, 0); | |
440 | if (error) | |
441 | goto release; | |
442 | } | |
1c79356b A |
443 | |
444 | switch (so->so_type) { | |
2d21ac55 | 445 | case SOCK_DGRAM: |
1c79356b A |
446 | { |
447 | struct sockaddr *from; | |
448 | ||
449 | if (nam) { | |
450 | if (unp->unp_conn) { | |
451 | error = EISCONN; | |
452 | break; | |
453 | } | |
454 | error = unp_connect(so, nam, p); | |
455 | if (error) | |
456 | break; | |
457 | } else { | |
458 | if (unp->unp_conn == 0) { | |
459 | error = ENOTCONN; | |
460 | break; | |
461 | } | |
462 | } | |
b0d623f7 | 463 | |
1c79356b | 464 | so2 = unp->unp_conn->unp_socket; |
6d2010ae A |
465 | if (so != so2) |
466 | unp_get_locks_in_order(so, so2); | |
b0d623f7 | 467 | |
1c79356b A |
468 | if (unp->unp_addr) |
469 | from = (struct sockaddr *)unp->unp_addr; | |
470 | else | |
471 | from = &sun_noname; | |
2d21ac55 A |
472 | /* |
473 | * sbappendaddr() will fail when the receiver runs out of | |
474 | * space; in contrast to SOCK_STREAM, we will lose messages | |
475 | * for the SOCK_DGRAM case when the receiver's queue overflows. | |
476 | * SB_UNIX on the socket buffer implies that the callee will | |
477 | * not free the control message, if any, because we would need | |
478 | * to call unp_dispose() on it. | |
479 | */ | |
91447636 | 480 | if (sbappendaddr(&so2->so_rcv, from, m, control, &error)) { |
2d21ac55 | 481 | control = NULL; |
1c79356b | 482 | sorwakeup(so2); |
2d21ac55 A |
483 | } else if (control != NULL && error == 0) { |
484 | /* A socket filter took control; don't touch it */ | |
485 | control = NULL; | |
91447636 | 486 | } |
b0d623f7 | 487 | |
6d2010ae A |
488 | if (so != so2) |
489 | socket_unlock(so2, 1); | |
b0d623f7 | 490 | |
2d21ac55 | 491 | m = NULL; |
1c79356b A |
492 | if (nam) |
493 | unp_disconnect(unp); | |
494 | break; | |
495 | } | |
496 | ||
91447636 A |
497 | case SOCK_STREAM: { |
498 | int didreceive = 0; | |
1c79356b A |
499 | #define rcv (&so2->so_rcv) |
500 | #define snd (&so->so_snd) | |
501 | /* Connect if not connected yet. */ | |
502 | /* | |
503 | * Note: A better implementation would complain | |
504 | * if not equal to the peer's address. | |
505 | */ | |
506 | if ((so->so_state & SS_ISCONNECTED) == 0) { | |
507 | if (nam) { | |
508 | error = unp_connect(so, nam, p); | |
509 | if (error) | |
510 | break; /* XXX */ | |
511 | } else { | |
512 | error = ENOTCONN; | |
513 | break; | |
514 | } | |
515 | } | |
516 | ||
517 | if (so->so_state & SS_CANTSENDMORE) { | |
518 | error = EPIPE; | |
519 | break; | |
520 | } | |
521 | if (unp->unp_conn == 0) | |
522 | panic("uipc_send connected but no connection?"); | |
b0d623f7 | 523 | |
1c79356b | 524 | so2 = unp->unp_conn->unp_socket; |
b0d623f7 A |
525 | unp_get_locks_in_order(so, so2); |
526 | ||
527 | /* Check socket state again as we might have unlocked the socket | |
528 | * while trying to get the locks in order | |
529 | */ | |
530 | ||
531 | if ((so->so_state & SS_CANTSENDMORE)) { | |
532 | error = EPIPE; | |
533 | socket_unlock(so2, 1); | |
534 | break; | |
535 | } | |
536 | ||
6d2010ae A |
537 | if (unp->unp_flags & UNP_TRACE_MDNS) { |
538 | struct mdns_ipc_msg_hdr hdr; | |
539 | ||
540 | if (mbuf_copydata(m, 0, sizeof (hdr), &hdr) == 0 && | |
541 | hdr.version == ntohl(MDNS_IPC_MSG_HDR_VERSION_1)) { | |
542 | printf("%s[mDNSResponder] pid=%d (%s): op=0x%x\n", | |
543 | __func__, p->p_pid, p->p_comm, ntohl(hdr.op)); | |
544 | } | |
545 | } | |
546 | ||
1c79356b | 547 | /* |
2d21ac55 A |
548 | * Send to paired receive port, and then reduce send buffer |
549 | * hiwater marks to maintain backpressure. Wake up readers. | |
550 | * SB_UNIX flag will allow new record to be appended to the | |
551 | * receiver's queue even when it is already full. It is | |
552 | * possible, however, that append might fail. In that case, | |
553 | * we will need to call unp_dispose() on the control message; | |
554 | * the callee will not free it since SB_UNIX is set. | |
1c79356b | 555 | */ |
2d21ac55 A |
556 | didreceive = control ? |
557 | sbappendcontrol(rcv, m, control, &error) : sbappend(rcv, m); | |
558 | ||
559 | snd->sb_mbmax -= rcv->sb_mbcnt - unp->unp_conn->unp_mbcnt; | |
1c79356b | 560 | unp->unp_conn->unp_mbcnt = rcv->sb_mbcnt; |
fe8ab488 A |
561 | if ((int32_t)snd->sb_hiwat >= |
562 | (int32_t)(rcv->sb_cc - unp->unp_conn->unp_cc)) { | |
563 | snd->sb_hiwat -= rcv->sb_cc - unp->unp_conn->unp_cc; | |
564 | } else { | |
565 | snd->sb_hiwat = 0; | |
566 | } | |
1c79356b | 567 | unp->unp_conn->unp_cc = rcv->sb_cc; |
2d21ac55 A |
568 | if (didreceive) { |
569 | control = NULL; | |
91447636 | 570 | sorwakeup(so2); |
2d21ac55 A |
571 | } else if (control != NULL && error == 0) { |
572 | /* A socket filter took control; don't touch it */ | |
573 | control = NULL; | |
574 | } | |
b0d623f7 A |
575 | |
576 | socket_unlock(so2, 1); | |
2d21ac55 | 577 | m = NULL; |
1c79356b A |
578 | #undef snd |
579 | #undef rcv | |
91447636 | 580 | } |
1c79356b A |
581 | break; |
582 | ||
583 | default: | |
584 | panic("uipc_send unknown socktype"); | |
585 | } | |
586 | ||
587 | /* | |
588 | * SEND_EOF is equivalent to a SEND followed by | |
589 | * a SHUTDOWN. | |
590 | */ | |
591 | if (flags & PRUS_EOF) { | |
592 | socantsendmore(so); | |
593 | unp_shutdown(unp); | |
594 | } | |
595 | ||
2d21ac55 A |
596 | if (control && error != 0) { |
597 | socket_unlock(so, 0); | |
91447636 | 598 | unp_dispose(control); |
2d21ac55 A |
599 | socket_lock(so, 0); |
600 | } | |
91447636 | 601 | |
1c79356b A |
602 | release: |
603 | if (control) | |
604 | m_freem(control); | |
605 | if (m) | |
606 | m_freem(m); | |
2d21ac55 | 607 | return (error); |
1c79356b A |
608 | } |
609 | ||
610 | static int | |
2d21ac55 | 611 | uipc_sense(struct socket *so, void *ub, int isstat64) |
1c79356b A |
612 | { |
613 | struct unpcb *unp = sotounpcb(so); | |
614 | struct socket *so2; | |
2d21ac55 | 615 | blksize_t blksize; |
1c79356b A |
616 | |
617 | if (unp == 0) | |
2d21ac55 A |
618 | return (EINVAL); |
619 | ||
620 | blksize = so->so_snd.sb_hiwat; | |
1c79356b A |
621 | if (so->so_type == SOCK_STREAM && unp->unp_conn != 0) { |
622 | so2 = unp->unp_conn->unp_socket; | |
2d21ac55 | 623 | blksize += so2->so_rcv.sb_cc; |
1c79356b | 624 | } |
1c79356b A |
625 | if (unp->unp_ino == 0) |
626 | unp->unp_ino = unp_ino++; | |
2d21ac55 A |
627 | |
628 | if (isstat64 != 0) { | |
629 | struct stat64 *sb64; | |
630 | ||
631 | sb64 = (struct stat64 *)ub; | |
632 | sb64->st_blksize = blksize; | |
633 | sb64->st_dev = NODEV; | |
634 | sb64->st_ino = (ino64_t)unp->unp_ino; | |
635 | } else { | |
636 | struct stat *sb; | |
637 | ||
638 | sb = (struct stat *)ub; | |
639 | sb->st_blksize = blksize; | |
640 | sb->st_dev = NODEV; | |
b0d623f7 | 641 | sb->st_ino = (ino_t)(uintptr_t)unp->unp_ino; |
2d21ac55 A |
642 | } |
643 | ||
1c79356b A |
644 | return (0); |
645 | } | |
646 | ||
2d21ac55 A |
647 | /* |
648 | * Returns: 0 Success | |
649 | * EINVAL | |
650 | * | |
651 | * Notes: This is not strictly correct, as unp_shutdown() also calls | |
652 | * socantrcvmore(). These should maybe both be conditionalized | |
653 | * on the 'how' argument in soshutdown() as called from the | |
654 | * shutdown() system call. | |
655 | */ | |
1c79356b A |
656 | static int |
657 | uipc_shutdown(struct socket *so) | |
658 | { | |
659 | struct unpcb *unp = sotounpcb(so); | |
660 | ||
661 | if (unp == 0) | |
2d21ac55 | 662 | return (EINVAL); |
1c79356b A |
663 | socantsendmore(so); |
664 | unp_shutdown(unp); | |
2d21ac55 | 665 | return (0); |
1c79356b A |
666 | } |
667 | ||
2d21ac55 A |
668 | /* |
669 | * Returns: 0 Success | |
670 | * EINVAL Invalid argument | |
671 | */ | |
1c79356b A |
672 | static int |
673 | uipc_sockaddr(struct socket *so, struct sockaddr **nam) | |
674 | { | |
675 | struct unpcb *unp = sotounpcb(so); | |
676 | ||
2d21ac55 A |
677 | if (unp == NULL) |
678 | return (EINVAL); | |
679 | if (unp->unp_addr != NULL) { | |
1c79356b | 680 | *nam = dup_sockaddr((struct sockaddr *)unp->unp_addr, 1); |
2d21ac55 A |
681 | } else { |
682 | *nam = dup_sockaddr((struct sockaddr *)&sun_noname, 1); | |
683 | } | |
684 | return (0); | |
1c79356b A |
685 | } |
686 | ||
687 | struct pr_usrreqs uipc_usrreqs = { | |
39236c6e A |
688 | .pru_abort = uipc_abort, |
689 | .pru_accept = uipc_accept, | |
690 | .pru_attach = uipc_attach, | |
691 | .pru_bind = uipc_bind, | |
692 | .pru_connect = uipc_connect, | |
693 | .pru_connect2 = uipc_connect2, | |
694 | .pru_detach = uipc_detach, | |
695 | .pru_disconnect = uipc_disconnect, | |
696 | .pru_listen = uipc_listen, | |
697 | .pru_peeraddr = uipc_peeraddr, | |
698 | .pru_rcvd = uipc_rcvd, | |
699 | .pru_send = uipc_send, | |
700 | .pru_sense = uipc_sense, | |
701 | .pru_shutdown = uipc_shutdown, | |
702 | .pru_sockaddr = uipc_sockaddr, | |
703 | .pru_sosend = sosend, | |
704 | .pru_soreceive = soreceive, | |
1c79356b | 705 | }; |
91447636 A |
706 | |
707 | int | |
2d21ac55 | 708 | uipc_ctloutput(struct socket *so, struct sockopt *sopt) |
91447636 A |
709 | { |
710 | struct unpcb *unp = sotounpcb(so); | |
39236c6e A |
711 | int error = 0; |
712 | pid_t peerpid; | |
713 | struct socket *peerso; | |
91447636 A |
714 | |
715 | switch (sopt->sopt_dir) { | |
716 | case SOPT_GET: | |
717 | switch (sopt->sopt_name) { | |
718 | case LOCAL_PEERCRED: | |
2d21ac55 | 719 | if (unp->unp_flags & UNP_HAVEPC) { |
91447636 | 720 | error = sooptcopyout(sopt, &unp->unp_peercred, |
2d21ac55 A |
721 | sizeof (unp->unp_peercred)); |
722 | } else { | |
91447636 A |
723 | if (so->so_type == SOCK_STREAM) |
724 | error = ENOTCONN; | |
725 | else | |
726 | error = EINVAL; | |
727 | } | |
728 | break; | |
316670eb | 729 | case LOCAL_PEERPID: |
39236c6e A |
730 | case LOCAL_PEEREPID: |
731 | if (unp->unp_conn == NULL) { | |
316670eb | 732 | error = ENOTCONN; |
39236c6e | 733 | break; |
316670eb | 734 | } |
39236c6e A |
735 | peerso = unp->unp_conn->unp_socket; |
736 | if (peerso == NULL) | |
737 | panic("peer is connected but has no socket?"); | |
738 | unp_get_locks_in_order(so, peerso); | |
739 | if (sopt->sopt_name == LOCAL_PEEREPID && | |
740 | peerso->so_flags & SOF_DELEGATED) | |
741 | peerpid = peerso->e_pid; | |
742 | else | |
743 | peerpid = peerso->last_pid; | |
744 | socket_unlock(peerso, 1); | |
745 | error = sooptcopyout(sopt, &peerpid, sizeof (peerpid)); | |
746 | break; | |
747 | case LOCAL_PEERUUID: | |
748 | case LOCAL_PEEREUUID: | |
749 | if (unp->unp_conn == NULL) { | |
750 | error = ENOTCONN; | |
751 | break; | |
752 | } | |
753 | peerso = unp->unp_conn->unp_socket; | |
754 | if (peerso == NULL) | |
755 | panic("peer is connected but has no socket?"); | |
756 | unp_get_locks_in_order(so, peerso); | |
757 | if (sopt->sopt_name == LOCAL_PEEREUUID && | |
758 | peerso->so_flags & SOF_DELEGATED) | |
759 | error = sooptcopyout(sopt, &peerso->e_uuid, | |
760 | sizeof (peerso->e_uuid)); | |
761 | else | |
762 | error = sooptcopyout(sopt, &peerso->last_uuid, | |
763 | sizeof (peerso->last_uuid)); | |
764 | socket_unlock(peerso, 1); | |
316670eb | 765 | break; |
91447636 A |
766 | default: |
767 | error = EOPNOTSUPP; | |
768 | break; | |
769 | } | |
770 | break; | |
771 | case SOPT_SET: | |
772 | default: | |
773 | error = EOPNOTSUPP; | |
774 | break; | |
775 | } | |
39236c6e | 776 | |
91447636 A |
777 | return (error); |
778 | } | |
2d21ac55 | 779 | |
1c79356b A |
780 | /* |
781 | * Both send and receive buffers are allocated PIPSIZ bytes of buffering | |
782 | * for stream sockets, although the total for sender and receiver is | |
783 | * actually only PIPSIZ. | |
784 | * Datagram sockets really use the sendspace as the maximum datagram size, | |
785 | * and don't really want to reserve the sendspace. Their recvspace should | |
786 | * be large enough for at least one max-size datagram plus address. | |
787 | */ | |
788 | #ifndef PIPSIZ | |
789 | #define PIPSIZ 8192 | |
790 | #endif | |
b0d623f7 A |
791 | static u_int32_t unpst_sendspace = PIPSIZ; |
792 | static u_int32_t unpst_recvspace = PIPSIZ; | |
793 | static u_int32_t unpdg_sendspace = 2*1024; /* really max datagram size */ | |
794 | static u_int32_t unpdg_recvspace = 4*1024; | |
1c79356b A |
795 | |
796 | static int unp_rights; /* file descriptors in flight */ | |
2d21ac55 | 797 | static int unp_disposed; /* discarded file descriptors */ |
1c79356b A |
798 | |
799 | SYSCTL_DECL(_net_local_stream); | |
6d2010ae | 800 | SYSCTL_INT(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW | CTLFLAG_LOCKED, |
2d21ac55 | 801 | &unpst_sendspace, 0, ""); |
6d2010ae | 802 | SYSCTL_INT(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW | CTLFLAG_LOCKED, |
2d21ac55 | 803 | &unpst_recvspace, 0, ""); |
6d2010ae A |
804 | SYSCTL_INT(_net_local_stream, OID_AUTO, tracemdns, CTLFLAG_RW | CTLFLAG_LOCKED, |
805 | &unpst_tracemdns, 0, ""); | |
1c79356b | 806 | SYSCTL_DECL(_net_local_dgram); |
6d2010ae | 807 | SYSCTL_INT(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW | CTLFLAG_LOCKED, |
2d21ac55 | 808 | &unpdg_sendspace, 0, ""); |
6d2010ae | 809 | SYSCTL_INT(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW | CTLFLAG_LOCKED, |
2d21ac55 | 810 | &unpdg_recvspace, 0, ""); |
1c79356b | 811 | SYSCTL_DECL(_net_local); |
6d2010ae | 812 | SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD | CTLFLAG_LOCKED, &unp_rights, 0, ""); |
1c79356b | 813 | |
2d21ac55 A |
814 | /* |
815 | * Returns: 0 Success | |
816 | * ENOBUFS | |
817 | * soreserve:ENOBUFS | |
818 | */ | |
1c79356b | 819 | static int |
91447636 | 820 | unp_attach(struct socket *so) |
1c79356b | 821 | { |
91447636 A |
822 | struct unpcb *unp; |
823 | int error = 0; | |
1c79356b A |
824 | |
825 | if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { | |
826 | switch (so->so_type) { | |
827 | ||
828 | case SOCK_STREAM: | |
829 | error = soreserve(so, unpst_sendspace, unpst_recvspace); | |
830 | break; | |
831 | ||
832 | case SOCK_DGRAM: | |
833 | error = soreserve(so, unpdg_sendspace, unpdg_recvspace); | |
834 | break; | |
835 | ||
836 | default: | |
837 | panic("unp_attach"); | |
838 | } | |
839 | if (error) | |
840 | return (error); | |
841 | } | |
2d21ac55 | 842 | unp = (struct unpcb *)zalloc(unp_zone); |
1c79356b A |
843 | if (unp == NULL) |
844 | return (ENOBUFS); | |
2d21ac55 | 845 | bzero(unp, sizeof (*unp)); |
b0d623f7 | 846 | |
6d2010ae A |
847 | lck_mtx_init(&unp->unp_mtx, |
848 | unp_mtx_grp, unp_mtx_attr); | |
b0d623f7 | 849 | |
37839358 | 850 | lck_rw_lock_exclusive(unp_list_mtx); |
1c79356b A |
851 | LIST_INIT(&unp->unp_refs); |
852 | unp->unp_socket = so; | |
91447636 A |
853 | unp->unp_gencnt = ++unp_gencnt; |
854 | unp_count++; | |
2d21ac55 A |
855 | LIST_INSERT_HEAD(so->so_type == SOCK_DGRAM ? |
856 | &unp_dhead : &unp_shead, unp, unp_link); | |
b0d623f7 | 857 | lck_rw_done(unp_list_mtx); |
1c79356b | 858 | so->so_pcb = (caddr_t)unp; |
2d21ac55 A |
859 | /* |
860 | * Mark AF_UNIX socket buffers accordingly so that: | |
861 | * | |
862 | * a. In the SOCK_STREAM case, socket buffer append won't fail due to | |
863 | * the lack of space; this essentially loosens the sbspace() check, | |
864 | * since there is disconnect between sosend() and uipc_send() with | |
865 | * respect to flow control that might result in our dropping the | |
866 | * data in uipc_send(). By setting this, we allow for slightly | |
867 | * more records to be appended to the receiving socket to avoid | |
868 | * losing data (which we can't afford in the SOCK_STREAM case). | |
869 | * Flow control still takes place since we adjust the sender's | |
870 | * hiwat during each send. This doesn't affect the SOCK_DGRAM | |
871 | * case and append would still fail when the queue overflows. | |
872 | * | |
873 | * b. In the presence of control messages containing internalized | |
874 | * file descriptors, the append routines will not free them since | |
875 | * we'd need to undo the work first via unp_dispose(). | |
876 | */ | |
877 | so->so_rcv.sb_flags |= SB_UNIX; | |
878 | so->so_snd.sb_flags |= SB_UNIX; | |
1c79356b A |
879 | return (0); |
880 | } | |
881 | ||
882 | static void | |
91447636 | 883 | unp_detach(struct unpcb *unp) |
1c79356b | 884 | { |
b7266188 A |
885 | int so_locked = 1; |
886 | ||
37839358 | 887 | lck_rw_lock_exclusive(unp_list_mtx); |
1c79356b | 888 | LIST_REMOVE(unp, unp_link); |
316670eb A |
889 | --unp_count; |
890 | ++unp_gencnt; | |
37839358 | 891 | lck_rw_done(unp_list_mtx); |
1c79356b | 892 | if (unp->unp_vnode) { |
b0d623f7 A |
893 | struct vnode *tvp = NULL; |
894 | socket_unlock(unp->unp_socket, 0); | |
895 | ||
896 | /* Holding unp_connect_lock will avoid a race between | |
897 | * a thread closing the listening socket and a thread | |
898 | * connecting to it. | |
899 | */ | |
900 | lck_mtx_lock(unp_connect_lock); | |
901 | socket_lock(unp->unp_socket, 0); | |
902 | if (unp->unp_vnode) { | |
903 | tvp = unp->unp_vnode; | |
904 | unp->unp_vnode->v_socket = NULL; | |
905 | unp->unp_vnode = NULL; | |
906 | } | |
907 | lck_mtx_unlock(unp_connect_lock); | |
908 | if (tvp != NULL) | |
909 | vnode_rele(tvp); /* drop the usecount */ | |
1c79356b A |
910 | } |
911 | if (unp->unp_conn) | |
912 | unp_disconnect(unp); | |
b0d623f7 | 913 | while (unp->unp_refs.lh_first) { |
b7266188 A |
914 | struct unpcb *unp2 = NULL; |
915 | ||
916 | /* This datagram socket is connected to one or more | |
917 | * sockets. In order to avoid a race condition between removing | |
918 | * this reference and closing the connected socket, we need | |
919 | * to check disconnect_in_progress | |
920 | */ | |
921 | if (so_locked == 1) { | |
922 | socket_unlock(unp->unp_socket, 0); | |
923 | so_locked = 0; | |
924 | } | |
925 | lck_mtx_lock(unp_disconnect_lock); | |
926 | while (disconnect_in_progress != 0) { | |
927 | (void)msleep((caddr_t)&disconnect_in_progress, unp_disconnect_lock, | |
928 | PSOCK, "disconnect", NULL); | |
929 | } | |
930 | disconnect_in_progress = 1; | |
931 | lck_mtx_unlock(unp_disconnect_lock); | |
932 | ||
933 | /* Now we are sure that any unpcb socket disconnect is not happening */ | |
934 | if (unp->unp_refs.lh_first != NULL) { | |
935 | unp2 = unp->unp_refs.lh_first; | |
936 | socket_lock(unp2->unp_socket, 1); | |
937 | } | |
938 | ||
939 | lck_mtx_lock(unp_disconnect_lock); | |
940 | disconnect_in_progress = 0; | |
941 | wakeup(&disconnect_in_progress); | |
942 | lck_mtx_unlock(unp_disconnect_lock); | |
943 | ||
944 | if (unp2 != NULL) { | |
945 | /* We already locked this socket and have a reference on it */ | |
946 | unp_drop(unp2, ECONNRESET); | |
947 | socket_unlock(unp2->unp_socket, 1); | |
948 | } | |
949 | } | |
950 | ||
951 | if (so_locked == 0) { | |
b0d623f7 | 952 | socket_lock(unp->unp_socket, 0); |
b7266188 | 953 | so_locked = 1; |
b0d623f7 | 954 | } |
1c79356b | 955 | soisdisconnected(unp->unp_socket); |
2d21ac55 A |
956 | /* makes sure we're getting dealloced */ |
957 | unp->unp_socket->so_flags |= SOF_PCBCLEARING; | |
1c79356b A |
958 | } |
959 | ||
2d21ac55 A |
960 | /* |
961 | * Returns: 0 Success | |
962 | * EAFNOSUPPORT | |
963 | * EINVAL | |
964 | * EADDRINUSE | |
965 | * namei:??? [anything namei can return] | |
966 | * vnode_authorize:??? [anything vnode_authorize can return] | |
967 | * | |
968 | * Notes: p at this point is the current process, as this function is | |
969 | * only called by sobind(). | |
970 | */ | |
1c79356b | 971 | static int |
91447636 A |
972 | unp_bind( |
973 | struct unpcb *unp, | |
974 | struct sockaddr *nam, | |
2d21ac55 | 975 | proc_t p) |
1c79356b A |
976 | { |
977 | struct sockaddr_un *soun = (struct sockaddr_un *)nam; | |
91447636 A |
978 | struct vnode *vp, *dvp; |
979 | struct vnode_attr va; | |
2d21ac55 | 980 | vfs_context_t ctx = vfs_context_current(); |
1c79356b A |
981 | int error, namelen; |
982 | struct nameidata nd; | |
b0d623f7 | 983 | struct socket *so = unp->unp_socket; |
1c79356b A |
984 | char buf[SOCK_MAXADDRLEN]; |
985 | ||
2d21ac55 A |
986 | if (nam->sa_family != 0 && nam->sa_family != AF_UNIX) { |
987 | return (EAFNOSUPPORT); | |
988 | } | |
91447636 | 989 | |
2d21ac55 | 990 | if (unp->unp_vnode != NULL) |
1c79356b | 991 | return (EINVAL); |
1c79356b | 992 | namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path); |
2d21ac55 A |
993 | if (namelen <= 0) |
994 | return (EINVAL); | |
995 | ||
b0d623f7 A |
996 | socket_unlock(so, 0); |
997 | ||
2d21ac55 | 998 | strlcpy(buf, soun->sun_path, namelen+1); |
6d2010ae | 999 | NDINIT(&nd, CREATE, OP_MKFIFO, FOLLOW | LOCKPARENT, UIO_SYSSPACE, |
2d21ac55 A |
1000 | CAST_USER_ADDR_T(buf), ctx); |
1001 | /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */ | |
1c79356b A |
1002 | error = namei(&nd); |
1003 | if (error) { | |
b0d623f7 | 1004 | socket_lock(so, 0); |
1c79356b A |
1005 | return (error); |
1006 | } | |
91447636 | 1007 | dvp = nd.ni_dvp; |
1c79356b | 1008 | vp = nd.ni_vp; |
91447636 | 1009 | |
1c79356b | 1010 | if (vp != NULL) { |
2d21ac55 | 1011 | /* |
91447636 A |
1012 | * need to do this before the vnode_put of dvp |
1013 | * since we may have to release an fs_nodelock | |
1014 | */ | |
1015 | nameidone(&nd); | |
1016 | ||
1017 | vnode_put(dvp); | |
1018 | vnode_put(vp); | |
1019 | ||
b0d623f7 | 1020 | socket_lock(so, 0); |
1c79356b A |
1021 | return (EADDRINUSE); |
1022 | } | |
91447636 | 1023 | |
2d21ac55 A |
1024 | VATTR_INIT(&va); |
1025 | VATTR_SET(&va, va_type, VSOCK); | |
1026 | VATTR_SET(&va, va_mode, (ACCESSPERMS & ~p->p_fd->fd_cmask)); | |
1027 | ||
b0d623f7 | 1028 | #if CONFIG_MACF |
2d21ac55 A |
1029 | error = mac_vnode_check_create(ctx, |
1030 | nd.ni_dvp, &nd.ni_cnd, &va); | |
1031 | ||
1032 | if (error == 0) | |
b0d623f7 A |
1033 | #endif /* CONFIG_MACF */ |
1034 | #if CONFIG_MACF_SOCKET_SUBSET | |
1035 | error = mac_vnode_check_uipc_bind(ctx, | |
1036 | nd.ni_dvp, &nd.ni_cnd, &va); | |
1037 | ||
1038 | if (error == 0) | |
1039 | #endif /* MAC_SOCKET_SUBSET */ | |
91447636 | 1040 | /* authorize before creating */ |
2d21ac55 | 1041 | error = vnode_authorize(dvp, NULL, KAUTH_VNODE_ADD_FILE, ctx); |
91447636 A |
1042 | |
1043 | if (!error) { | |
91447636 | 1044 | /* create the socket */ |
6d2010ae | 1045 | error = vn_create(dvp, &vp, &nd, &va, 0, 0, NULL, ctx); |
91447636 | 1046 | } |
2d21ac55 | 1047 | |
91447636 A |
1048 | nameidone(&nd); |
1049 | vnode_put(dvp); | |
1050 | ||
1c79356b | 1051 | if (error) { |
b0d623f7 | 1052 | socket_lock(so, 0); |
1c79356b A |
1053 | return (error); |
1054 | } | |
91447636 | 1055 | vnode_ref(vp); /* gain a longterm reference */ |
b0d623f7 | 1056 | socket_lock(so, 0); |
1c79356b A |
1057 | vp->v_socket = unp->unp_socket; |
1058 | unp->unp_vnode = vp; | |
1059 | unp->unp_addr = (struct sockaddr_un *)dup_sockaddr(nam, 1); | |
91447636 A |
1060 | vnode_put(vp); /* drop the iocount */ |
1061 | ||
1c79356b A |
1062 | return (0); |
1063 | } | |
1064 | ||
2d21ac55 A |
1065 | |
1066 | /* | |
1067 | * Returns: 0 Success | |
1068 | * EAFNOSUPPORT Address family not supported | |
1069 | * EINVAL Invalid argument | |
1070 | * ENOTSOCK Not a socket | |
1071 | * ECONNREFUSED Connection refused | |
1072 | * EPROTOTYPE Protocol wrong type for socket | |
1073 | * EISCONN Socket is connected | |
1074 | * unp_connect2:EPROTOTYPE Protocol wrong type for socket | |
1075 | * unp_connect2:EINVAL Invalid argument | |
1076 | * namei:??? [anything namei can return] | |
1077 | * vnode_authorize:???? [anything vnode_authorize can return] | |
1078 | * | |
1079 | * Notes: p at this point is the current process, as this function is | |
1080 | * only called by sosend(), sendfile(), and soconnectlock(). | |
1081 | */ | |
1c79356b | 1082 | static int |
2d21ac55 | 1083 | unp_connect(struct socket *so, struct sockaddr *nam, __unused proc_t p) |
1c79356b | 1084 | { |
91447636 A |
1085 | struct sockaddr_un *soun = (struct sockaddr_un *)nam; |
1086 | struct vnode *vp; | |
b0d623f7 | 1087 | struct socket *so2, *so3, *list_so=NULL; |
91447636 | 1088 | struct unpcb *unp, *unp2, *unp3; |
2d21ac55 | 1089 | vfs_context_t ctx = vfs_context_current(); |
1c79356b A |
1090 | int error, len; |
1091 | struct nameidata nd; | |
1092 | char buf[SOCK_MAXADDRLEN]; | |
1093 | ||
2d21ac55 A |
1094 | if (nam->sa_family != 0 && nam->sa_family != AF_UNIX) { |
1095 | return (EAFNOSUPPORT); | |
1096 | } | |
1097 | ||
b0d623f7 | 1098 | unp = sotounpcb(so); |
cc9f6e38 | 1099 | so2 = so3 = NULL; |
91447636 | 1100 | |
1c79356b | 1101 | len = nam->sa_len - offsetof(struct sockaddr_un, sun_path); |
2d21ac55 A |
1102 | if (len <= 0) |
1103 | return (EINVAL); | |
1104 | ||
1105 | strlcpy(buf, soun->sun_path, len+1); | |
b0d623f7 | 1106 | socket_unlock(so, 0); |
1c79356b | 1107 | |
6d2010ae | 1108 | NDINIT(&nd, LOOKUP, OP_LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, |
2d21ac55 | 1109 | CAST_USER_ADDR_T(buf), ctx); |
1c79356b A |
1110 | error = namei(&nd); |
1111 | if (error) { | |
b0d623f7 | 1112 | socket_lock(so, 0); |
1c79356b A |
1113 | return (error); |
1114 | } | |
91447636 | 1115 | nameidone(&nd); |
1c79356b A |
1116 | vp = nd.ni_vp; |
1117 | if (vp->v_type != VSOCK) { | |
1118 | error = ENOTSOCK; | |
b0d623f7 A |
1119 | socket_lock(so, 0); |
1120 | goto out; | |
1c79356b | 1121 | } |
91447636 | 1122 | |
b0d623f7 A |
1123 | #if CONFIG_MACF_SOCKET_SUBSET |
1124 | error = mac_vnode_check_uipc_connect(ctx, vp); | |
1125 | if (error) { | |
1126 | socket_lock(so, 0); | |
1127 | goto out; | |
1128 | } | |
1129 | #endif /* MAC_SOCKET_SUBSET */ | |
1130 | ||
2d21ac55 | 1131 | error = vnode_authorize(vp, NULL, KAUTH_VNODE_WRITE_DATA, ctx); |
b0d623f7 A |
1132 | if (error) { |
1133 | socket_lock(so, 0); | |
1134 | goto out; | |
1135 | } | |
1136 | ||
1137 | lck_mtx_lock(unp_connect_lock); | |
1138 | ||
1139 | if (vp->v_socket == 0) { | |
1140 | lck_mtx_unlock(unp_connect_lock); | |
1c79356b | 1141 | error = ECONNREFUSED; |
b0d623f7 A |
1142 | socket_lock(so, 0); |
1143 | goto out; | |
1c79356b | 1144 | } |
91447636 | 1145 | |
b0d623f7 A |
1146 | socket_lock(vp->v_socket, 1); /* Get a reference on the listening socket */ |
1147 | so2 = vp->v_socket; | |
1148 | lck_mtx_unlock(unp_connect_lock); | |
91447636 | 1149 | |
b0d623f7 A |
1150 | |
1151 | if (so2->so_pcb == NULL) { | |
1152 | error = ECONNREFUSED; | |
6d2010ae A |
1153 | if (so != so2) { |
1154 | socket_unlock(so2, 1); | |
1155 | socket_lock(so, 0); | |
1156 | } else { | |
1157 | /* Release the reference held for the listen socket */ | |
1158 | so2->so_usecount--; | |
1159 | } | |
b0d623f7 | 1160 | goto out; |
1c79356b | 1161 | } |
2d21ac55 | 1162 | |
b0d623f7 A |
1163 | if (so < so2) { |
1164 | socket_unlock(so2, 0); | |
1165 | socket_lock(so, 0); | |
1166 | socket_lock(so2, 0); | |
6d2010ae | 1167 | } else if (so > so2) { |
b0d623f7 A |
1168 | socket_lock(so, 0); |
1169 | } | |
55e303ae A |
1170 | /* |
1171 | * Check if socket was connected while we were trying to | |
b0d623f7 | 1172 | * get the socket locks in order. |
55e303ae A |
1173 | * XXX - probably shouldn't return an error for SOCK_DGRAM |
1174 | */ | |
1175 | if ((so->so_state & SS_ISCONNECTED) != 0) { | |
1176 | error = EISCONN; | |
6d2010ae | 1177 | goto decref_out; |
b0d623f7 A |
1178 | } |
1179 | ||
1180 | if (so->so_type != so2->so_type) { | |
b0d623f7 | 1181 | error = EPROTOTYPE; |
6d2010ae | 1182 | goto decref_out; |
55e303ae | 1183 | } |
2d21ac55 | 1184 | |
1c79356b | 1185 | if (so->so_proto->pr_flags & PR_CONNREQUIRED) { |
b0d623f7 A |
1186 | /* Release the incoming socket but keep a reference */ |
1187 | socket_unlock(so, 0); | |
1188 | ||
1c79356b | 1189 | if ((so2->so_options & SO_ACCEPTCONN) == 0 || |
91447636 | 1190 | (so3 = sonewconn(so2, 0, nam)) == 0) { |
1c79356b | 1191 | error = ECONNREFUSED; |
316670eb A |
1192 | if (so != so2) { |
1193 | socket_unlock(so2, 1); | |
1194 | socket_lock(so, 0); | |
1195 | } else { | |
1196 | socket_lock(so, 0); | |
1197 | /* Release the reference held for | |
1198 | * listen socket. | |
1199 | */ | |
1200 | so2->so_usecount--; | |
1201 | } | |
b0d623f7 | 1202 | goto out; |
1c79356b A |
1203 | } |
1204 | unp2 = sotounpcb(so2); | |
1205 | unp3 = sotounpcb(so3); | |
1206 | if (unp2->unp_addr) | |
1207 | unp3->unp_addr = (struct sockaddr_un *) | |
2d21ac55 | 1208 | dup_sockaddr((struct sockaddr *)unp2->unp_addr, 1); |
91447636 A |
1209 | |
1210 | /* | |
1211 | * unp_peercred management: | |
1212 | * | |
1213 | * The connecter's (client's) credentials are copied | |
1214 | * from its process structure at the time of connect() | |
1215 | * (which is now). | |
1216 | */ | |
2d21ac55 | 1217 | cru2x(vfs_context_ucred(ctx), &unp3->unp_peercred); |
91447636 A |
1218 | unp3->unp_flags |= UNP_HAVEPC; |
1219 | /* | |
1220 | * The receiver's (server's) credentials are copied | |
1221 | * from the unp_peercred member of socket on which the | |
1222 | * former called listen(); unp_listen() cached that | |
1223 | * process's credentials at that time so we can use | |
1224 | * them now. | |
1225 | */ | |
1226 | KASSERT(unp2->unp_flags & UNP_HAVEPCCACHED, | |
1227 | ("unp_connect: listener without cached peercred")); | |
b0d623f7 A |
1228 | |
1229 | /* Here we need to have both so and so2 locks and so2 | |
1230 | * is already locked. Lock ordering is required. | |
1231 | */ | |
1232 | if (so < so2) { | |
1233 | socket_unlock(so2, 0); | |
1234 | socket_lock(so, 0); | |
1235 | socket_lock(so2, 0); | |
1236 | } else { | |
1237 | socket_lock(so, 0); | |
1238 | } | |
1239 | ||
1240 | /* Check again if the socket state changed when its lock was released */ | |
1241 | if ((so->so_state & SS_ISCONNECTED) != 0) { | |
1242 | error = EISCONN; | |
1243 | socket_unlock(so2, 1); | |
1244 | socket_lock(so3, 0); | |
1245 | sofreelastref(so3, 1); | |
1246 | goto out; | |
1247 | } | |
91447636 | 1248 | memcpy(&unp->unp_peercred, &unp2->unp_peercred, |
2d21ac55 | 1249 | sizeof (unp->unp_peercred)); |
91447636 A |
1250 | unp->unp_flags |= UNP_HAVEPC; |
1251 | ||
2d21ac55 A |
1252 | #if CONFIG_MACF_SOCKET |
1253 | /* XXXMAC: recursive lock: SOCK_LOCK(so); */ | |
1254 | mac_socketpeer_label_associate_socket(so, so3); | |
1255 | mac_socketpeer_label_associate_socket(so3, so); | |
1256 | /* XXXMAC: SOCK_UNLOCK(so); */ | |
1257 | #endif /* MAC_SOCKET */ | |
b0d623f7 A |
1258 | |
1259 | /* Hold the reference on listening socket until the end */ | |
1260 | socket_unlock(so2, 0); | |
1261 | list_so = so2; | |
1262 | ||
1263 | /* Lock ordering doesn't matter because so3 was just created */ | |
1264 | socket_lock(so3, 1); | |
1c79356b | 1265 | so2 = so3; |
b0d623f7 | 1266 | |
6d2010ae A |
1267 | /* |
1268 | * Enable tracing for mDNSResponder endpoints. (The use | |
1269 | * of sizeof instead of strlen below takes the null | |
1270 | * terminating character into account.) | |
1271 | */ | |
1272 | if (unpst_tracemdns && | |
1273 | !strncmp(soun->sun_path, MDNSRESPONDER_PATH, | |
1274 | sizeof (MDNSRESPONDER_PATH))) { | |
1275 | unp->unp_flags |= UNP_TRACE_MDNS; | |
1276 | unp2->unp_flags |= UNP_TRACE_MDNS; | |
1277 | } | |
1c79356b | 1278 | } |
b0d623f7 | 1279 | |
1c79356b | 1280 | error = unp_connect2(so, so2); |
6d2010ae A |
1281 | |
1282 | decref_out: | |
b0d623f7 | 1283 | if (so2 != NULL) { |
6d2010ae A |
1284 | if (so != so2) { |
1285 | socket_unlock(so2, 1); | |
1286 | } else { | |
1287 | /* Release the extra reference held for the listen socket. | |
1288 | * This is possible only for SOCK_DGRAM sockets. We refuse | |
1289 | * connecting to the same socket for SOCK_STREAM sockets. | |
1290 | */ | |
1291 | so2->so_usecount--; | |
1292 | } | |
b0d623f7 A |
1293 | } |
1294 | ||
1295 | if (list_so != NULL) { | |
1296 | socket_lock(list_so, 0); | |
1297 | socket_unlock(list_so, 1); | |
1298 | } | |
6d2010ae | 1299 | |
b0d623f7 | 1300 | out: |
6d2010ae | 1301 | lck_mtx_assert(&unp->unp_mtx, LCK_MTX_ASSERT_OWNED); |
91447636 | 1302 | vnode_put(vp); |
1c79356b A |
1303 | return (error); |
1304 | } | |
1305 | ||
2d21ac55 A |
1306 | /* |
1307 | * Returns: 0 Success | |
1308 | * EPROTOTYPE Protocol wrong type for socket | |
1309 | * EINVAL Invalid argument | |
1310 | */ | |
1c79356b | 1311 | int |
2d21ac55 | 1312 | unp_connect2(struct socket *so, struct socket *so2) |
1c79356b | 1313 | { |
91447636 A |
1314 | struct unpcb *unp = sotounpcb(so); |
1315 | struct unpcb *unp2; | |
1c79356b A |
1316 | |
1317 | if (so2->so_type != so->so_type) | |
1318 | return (EPROTOTYPE); | |
b0d623f7 | 1319 | |
1c79356b | 1320 | unp2 = sotounpcb(so2); |
0b4e3aa0 | 1321 | |
6d2010ae A |
1322 | lck_mtx_assert(&unp->unp_mtx, LCK_MTX_ASSERT_OWNED); |
1323 | lck_mtx_assert(&unp2->unp_mtx, LCK_MTX_ASSERT_OWNED); | |
b0d623f7 | 1324 | |
0b4e3aa0 A |
1325 | /* Verify both sockets are still opened */ |
1326 | if (unp == 0 || unp2 == 0) | |
1327 | return (EINVAL); | |
1328 | ||
1c79356b | 1329 | unp->unp_conn = unp2; |
b0d623f7 A |
1330 | so2->so_usecount++; |
1331 | ||
1c79356b A |
1332 | switch (so->so_type) { |
1333 | ||
1334 | case SOCK_DGRAM: | |
1335 | LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink); | |
b0d623f7 | 1336 | |
6d2010ae A |
1337 | if (so != so2) { |
1338 | /* Avoid lock order reversals due to drop/acquire in soisconnected. */ | |
1339 | /* Keep an extra reference on so2 that will be dropped | |
1340 | * soon after getting the locks in order | |
1341 | */ | |
1342 | socket_unlock(so2, 0); | |
1343 | soisconnected(so); | |
1344 | unp_get_locks_in_order(so, so2); | |
1345 | so2->so_usecount--; | |
1346 | } else { | |
1347 | soisconnected(so); | |
1348 | } | |
b0d623f7 | 1349 | |
1c79356b A |
1350 | break; |
1351 | ||
1352 | case SOCK_STREAM: | |
2d21ac55 A |
1353 | /* This takes care of socketpair */ |
1354 | if (!(unp->unp_flags & UNP_HAVEPC) && | |
1355 | !(unp2->unp_flags & UNP_HAVEPC)) { | |
1356 | cru2x(kauth_cred_get(), &unp->unp_peercred); | |
1357 | unp->unp_flags |= UNP_HAVEPC; | |
1358 | ||
1359 | cru2x(kauth_cred_get(), &unp2->unp_peercred); | |
1360 | unp2->unp_flags |= UNP_HAVEPC; | |
1361 | } | |
1c79356b | 1362 | unp2->unp_conn = unp; |
b0d623f7 A |
1363 | so->so_usecount++; |
1364 | ||
1365 | /* Avoid lock order reversals due to drop/acquire in soisconnected. */ | |
1366 | socket_unlock(so, 0); | |
1c79356b | 1367 | soisconnected(so2); |
b0d623f7 A |
1368 | |
1369 | /* Keep an extra reference on so2, that will be dropped soon after | |
1370 | * getting the locks in order again. | |
1371 | */ | |
1372 | socket_unlock(so2, 0); | |
1373 | ||
1374 | socket_lock(so, 0); | |
1375 | soisconnected(so); | |
1376 | ||
1377 | unp_get_locks_in_order(so, so2); | |
1378 | /* Decrement the extra reference left before */ | |
1379 | so2->so_usecount--; | |
1c79356b A |
1380 | break; |
1381 | ||
1382 | default: | |
b0d623f7 | 1383 | panic("unknown socket type %d in unp_connect2", so->so_type); |
1c79356b | 1384 | } |
6d2010ae A |
1385 | lck_mtx_assert(&unp->unp_mtx, LCK_MTX_ASSERT_OWNED); |
1386 | lck_mtx_assert(&unp2->unp_mtx, LCK_MTX_ASSERT_OWNED); | |
1c79356b A |
1387 | return (0); |
1388 | } | |
1389 | ||
1390 | static void | |
91447636 | 1391 | unp_disconnect(struct unpcb *unp) |
1c79356b | 1392 | { |
b0d623f7 A |
1393 | struct unpcb *unp2 = NULL; |
1394 | struct socket *so2 = NULL, *so; | |
1395 | struct socket *waitso; | |
1396 | int so_locked = 1, strdisconn = 0; | |
1c79356b | 1397 | |
b0d623f7 A |
1398 | so = unp->unp_socket; |
1399 | if (unp->unp_conn == NULL) { | |
1c79356b | 1400 | return; |
b0d623f7 A |
1401 | } |
1402 | lck_mtx_lock(unp_disconnect_lock); | |
1403 | while (disconnect_in_progress != 0) { | |
1404 | if (so_locked == 1) { | |
1405 | socket_unlock(so, 0); | |
1406 | so_locked = 0; | |
1407 | } | |
1408 | (void)msleep((caddr_t)&disconnect_in_progress, unp_disconnect_lock, | |
1409 | PSOCK, "disconnect", NULL); | |
1410 | } | |
1411 | disconnect_in_progress = 1; | |
1412 | lck_mtx_unlock(unp_disconnect_lock); | |
1413 | ||
1414 | if (so_locked == 0) { | |
1415 | socket_lock(so, 0); | |
1416 | so_locked = 1; | |
1417 | } | |
1418 | ||
1419 | unp2 = unp->unp_conn; | |
1420 | ||
1421 | if (unp2 == 0 || unp2->unp_socket == NULL) { | |
1422 | goto out; | |
1423 | } | |
1424 | so2 = unp2->unp_socket; | |
1425 | ||
1426 | try_again: | |
6d2010ae A |
1427 | if (so == so2) { |
1428 | if (so_locked == 0) { | |
1429 | socket_lock(so, 0); | |
1430 | } | |
1431 | waitso = so; | |
1432 | } else if (so < so2) { | |
b0d623f7 A |
1433 | if (so_locked == 0) { |
1434 | socket_lock(so, 0); | |
1435 | } | |
1436 | socket_lock(so2, 1); | |
1437 | waitso = so2; | |
1438 | } else { | |
1439 | if (so_locked == 1) { | |
1440 | socket_unlock(so, 0); | |
1441 | } | |
1442 | socket_lock(so2, 1); | |
1443 | socket_lock(so, 0); | |
1444 | waitso = so; | |
1445 | } | |
6d2010ae | 1446 | so_locked = 1; |
b0d623f7 | 1447 | |
6d2010ae A |
1448 | lck_mtx_assert(&unp->unp_mtx, LCK_MTX_ASSERT_OWNED); |
1449 | lck_mtx_assert(&unp2->unp_mtx, LCK_MTX_ASSERT_OWNED); | |
b0d623f7 A |
1450 | |
1451 | /* Check for the UNP_DONTDISCONNECT flag, if it | |
1452 | * is set, release both sockets and go to sleep | |
1453 | */ | |
1454 | ||
1455 | if ((((struct unpcb *)waitso->so_pcb)->unp_flags & UNP_DONTDISCONNECT) != 0) { | |
6d2010ae A |
1456 | if (so != so2) { |
1457 | socket_unlock(so2, 1); | |
1458 | } | |
b0d623f7 A |
1459 | so_locked = 0; |
1460 | ||
6d2010ae | 1461 | (void)msleep(waitso->so_pcb, &unp->unp_mtx, |
b0d623f7 A |
1462 | PSOCK | PDROP, "unpdisconnect", NULL); |
1463 | goto try_again; | |
1464 | } | |
1465 | ||
1466 | if (unp->unp_conn == NULL) { | |
1467 | panic("unp_conn became NULL after sleep"); | |
1468 | } | |
1469 | ||
2d21ac55 | 1470 | unp->unp_conn = NULL; |
b0d623f7 A |
1471 | so2->so_usecount--; |
1472 | ||
6d2010ae A |
1473 | if (unp->unp_flags & UNP_TRACE_MDNS) |
1474 | unp->unp_flags &= ~UNP_TRACE_MDNS; | |
1475 | ||
1c79356b A |
1476 | switch (unp->unp_socket->so_type) { |
1477 | ||
1478 | case SOCK_DGRAM: | |
1479 | LIST_REMOVE(unp, unp_reflink); | |
1480 | unp->unp_socket->so_state &= ~SS_ISCONNECTED; | |
6d2010ae A |
1481 | if (so != so2) |
1482 | socket_unlock(so2, 1); | |
1c79356b A |
1483 | break; |
1484 | ||
1485 | case SOCK_STREAM: | |
2d21ac55 | 1486 | unp2->unp_conn = NULL; |
b0d623f7 A |
1487 | so->so_usecount--; |
1488 | ||
1489 | /* Set the socket state correctly but do a wakeup later when | |
1490 | * we release all locks except the socket lock, this will avoid | |
1491 | * a deadlock. | |
1492 | */ | |
1493 | unp->unp_socket->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING); | |
1494 | unp->unp_socket->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE|SS_ISDISCONNECTED); | |
1495 | ||
1496 | unp2->unp_socket->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING); | |
1497 | unp->unp_socket->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE|SS_ISDISCONNECTED); | |
6d2010ae A |
1498 | |
1499 | if (unp2->unp_flags & UNP_TRACE_MDNS) | |
1500 | unp2->unp_flags &= ~UNP_TRACE_MDNS; | |
1501 | ||
b0d623f7 | 1502 | strdisconn = 1; |
1c79356b | 1503 | break; |
b0d623f7 A |
1504 | default: |
1505 | panic("unknown socket type %d", so->so_type); | |
1c79356b | 1506 | } |
b0d623f7 A |
1507 | out: |
1508 | lck_mtx_lock(unp_disconnect_lock); | |
1509 | disconnect_in_progress = 0; | |
1510 | wakeup(&disconnect_in_progress); | |
1511 | lck_mtx_unlock(unp_disconnect_lock); | |
1c79356b | 1512 | |
b0d623f7 A |
1513 | if (strdisconn) { |
1514 | socket_unlock(so, 0); | |
1515 | soisdisconnected(so2); | |
1516 | socket_unlock(so2, 1); | |
1c79356b | 1517 | |
b0d623f7 A |
1518 | socket_lock(so,0); |
1519 | soisdisconnected(so); | |
1520 | } | |
6d2010ae | 1521 | lck_mtx_assert(&unp->unp_mtx, LCK_MTX_ASSERT_OWNED); |
b0d623f7 | 1522 | return; |
1c79356b | 1523 | } |
b0d623f7 A |
1524 | |
1525 | /* | |
1526 | * unpcb_to_compat copies specific bits of a unpcb to a unpcb_compat format. | |
1527 | * The unpcb_compat data structure is passed to user space and must not change. | |
1528 | */ | |
1529 | static void | |
1530 | unpcb_to_compat(struct unpcb *up, struct unpcb_compat *cp) | |
1531 | { | |
1532 | #if defined(__LP64__) | |
316670eb A |
1533 | cp->unp_link.le_next = (u_int32_t) |
1534 | VM_KERNEL_ADDRPERM(up->unp_link.le_next); | |
1535 | cp->unp_link.le_prev = (u_int32_t) | |
1536 | VM_KERNEL_ADDRPERM(up->unp_link.le_prev); | |
b0d623f7 | 1537 | #else |
316670eb A |
1538 | cp->unp_link.le_next = (struct unpcb_compat *) |
1539 | VM_KERNEL_ADDRPERM(up->unp_link.le_next); | |
1540 | cp->unp_link.le_prev = (struct unpcb_compat **) | |
1541 | VM_KERNEL_ADDRPERM(up->unp_link.le_prev); | |
b0d623f7 | 1542 | #endif |
316670eb A |
1543 | cp->unp_socket = (_UNPCB_PTR(struct socket *)) |
1544 | VM_KERNEL_ADDRPERM(up->unp_socket); | |
1545 | cp->unp_vnode = (_UNPCB_PTR(struct vnode *)) | |
1546 | VM_KERNEL_ADDRPERM(up->unp_vnode); | |
b0d623f7 A |
1547 | cp->unp_ino = up->unp_ino; |
1548 | cp->unp_conn = (_UNPCB_PTR(struct unpcb_compat *)) | |
316670eb A |
1549 | VM_KERNEL_ADDRPERM(up->unp_conn); |
1550 | cp->unp_refs = (u_int32_t)VM_KERNEL_ADDRPERM(up->unp_refs.lh_first); | |
b0d623f7 A |
1551 | #if defined(__LP64__) |
1552 | cp->unp_reflink.le_next = | |
316670eb | 1553 | (u_int32_t)VM_KERNEL_ADDRPERM(up->unp_reflink.le_next); |
b0d623f7 | 1554 | cp->unp_reflink.le_prev = |
316670eb | 1555 | (u_int32_t)VM_KERNEL_ADDRPERM(up->unp_reflink.le_prev); |
b0d623f7 A |
1556 | #else |
1557 | cp->unp_reflink.le_next = | |
316670eb | 1558 | (struct unpcb_compat *)VM_KERNEL_ADDRPERM(up->unp_reflink.le_next); |
b0d623f7 | 1559 | cp->unp_reflink.le_prev = |
316670eb | 1560 | (struct unpcb_compat **)VM_KERNEL_ADDRPERM(up->unp_reflink.le_prev); |
1c79356b | 1561 | #endif |
b0d623f7 | 1562 | cp->unp_addr = (_UNPCB_PTR(struct sockaddr_un *)) |
316670eb | 1563 | VM_KERNEL_ADDRPERM(up->unp_addr); |
b0d623f7 A |
1564 | cp->unp_cc = up->unp_cc; |
1565 | cp->unp_mbcnt = up->unp_mbcnt; | |
1566 | cp->unp_gencnt = up->unp_gencnt; | |
1567 | } | |
1c79356b A |
1568 | |
1569 | static int | |
1570 | unp_pcblist SYSCTL_HANDLER_ARGS | |
1571 | { | |
2d21ac55 | 1572 | #pragma unused(oidp,arg2) |
1c79356b A |
1573 | int error, i, n; |
1574 | struct unpcb *unp, **unp_list; | |
1575 | unp_gen_t gencnt; | |
1576 | struct xunpgen xug; | |
1577 | struct unp_head *head; | |
1578 | ||
37839358 | 1579 | lck_rw_lock_shared(unp_list_mtx); |
1c79356b A |
1580 | head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead); |
1581 | ||
1582 | /* | |
1583 | * The process of preparing the PCB list is too time-consuming and | |
1584 | * resource-intensive to repeat twice on every request. | |
1585 | */ | |
91447636 | 1586 | if (req->oldptr == USER_ADDR_NULL) { |
1c79356b | 1587 | n = unp_count; |
2d21ac55 A |
1588 | req->oldidx = 2 * sizeof (xug) + (n + n / 8) * |
1589 | sizeof (struct xunpcb); | |
37839358 | 1590 | lck_rw_done(unp_list_mtx); |
2d21ac55 | 1591 | return (0); |
1c79356b A |
1592 | } |
1593 | ||
91447636 | 1594 | if (req->newptr != USER_ADDR_NULL) { |
37839358 | 1595 | lck_rw_done(unp_list_mtx); |
2d21ac55 | 1596 | return (EPERM); |
91447636 | 1597 | } |
1c79356b A |
1598 | |
1599 | /* | |
1600 | * OK, now we're committed to doing something. | |
1601 | */ | |
1602 | gencnt = unp_gencnt; | |
1603 | n = unp_count; | |
1604 | ||
2d21ac55 A |
1605 | bzero(&xug, sizeof (xug)); |
1606 | xug.xug_len = sizeof (xug); | |
1c79356b A |
1607 | xug.xug_count = n; |
1608 | xug.xug_gen = gencnt; | |
1609 | xug.xug_sogen = so_gencnt; | |
2d21ac55 | 1610 | error = SYSCTL_OUT(req, &xug, sizeof (xug)); |
91447636 | 1611 | if (error) { |
37839358 | 1612 | lck_rw_done(unp_list_mtx); |
2d21ac55 | 1613 | return (error); |
91447636 | 1614 | } |
1c79356b | 1615 | |
0b4e3aa0 A |
1616 | /* |
1617 | * We are done if there is no pcb | |
1618 | */ | |
91447636 | 1619 | if (n == 0) { |
2d21ac55 A |
1620 | lck_rw_done(unp_list_mtx); |
1621 | return (0); | |
91447636 | 1622 | } |
0b4e3aa0 | 1623 | |
2d21ac55 A |
1624 | MALLOC(unp_list, struct unpcb **, n * sizeof (*unp_list), |
1625 | M_TEMP, M_WAITOK); | |
91447636 | 1626 | if (unp_list == 0) { |
37839358 | 1627 | lck_rw_done(unp_list_mtx); |
2d21ac55 | 1628 | return (ENOMEM); |
91447636 | 1629 | } |
2d21ac55 | 1630 | |
1c79356b | 1631 | for (unp = head->lh_first, i = 0; unp && i < n; |
2d21ac55 | 1632 | unp = unp->unp_link.le_next) { |
1c79356b A |
1633 | if (unp->unp_gencnt <= gencnt) |
1634 | unp_list[i++] = unp; | |
1635 | } | |
1636 | n = i; /* in case we lost some during malloc */ | |
1637 | ||
1638 | error = 0; | |
1639 | for (i = 0; i < n; i++) { | |
1640 | unp = unp_list[i]; | |
1641 | if (unp->unp_gencnt <= gencnt) { | |
1642 | struct xunpcb xu; | |
3a60a9f5 | 1643 | |
2d21ac55 A |
1644 | bzero(&xu, sizeof (xu)); |
1645 | xu.xu_len = sizeof (xu); | |
b0d623f7 | 1646 | xu.xu_unpp = (_UNPCB_PTR(struct unpcb_compat *)) |
316670eb | 1647 | VM_KERNEL_ADDRPERM(unp); |
1c79356b A |
1648 | /* |
1649 | * XXX - need more locking here to protect against | |
1650 | * connect/disconnect races for SMP. | |
1651 | */ | |
1652 | if (unp->unp_addr) | |
2d21ac55 A |
1653 | bcopy(unp->unp_addr, &xu.xu_addr, |
1654 | unp->unp_addr->sun_len); | |
1c79356b A |
1655 | if (unp->unp_conn && unp->unp_conn->unp_addr) |
1656 | bcopy(unp->unp_conn->unp_addr, | |
2d21ac55 A |
1657 | &xu.xu_caddr, |
1658 | unp->unp_conn->unp_addr->sun_len); | |
b0d623f7 | 1659 | unpcb_to_compat(unp, &xu.xu_unp); |
1c79356b | 1660 | sotoxsocket(unp->unp_socket, &xu.xu_socket); |
2d21ac55 | 1661 | error = SYSCTL_OUT(req, &xu, sizeof (xu)); |
1c79356b A |
1662 | } |
1663 | } | |
1664 | if (!error) { | |
1665 | /* | |
1666 | * Give the user an updated idea of our state. | |
1667 | * If the generation differs from what we told | |
1668 | * her before, she knows that something happened | |
1669 | * while we were processing this request, and it | |
1670 | * might be necessary to retry. | |
1671 | */ | |
2d21ac55 A |
1672 | bzero(&xug, sizeof (xug)); |
1673 | xug.xug_len = sizeof (xug); | |
1c79356b A |
1674 | xug.xug_gen = unp_gencnt; |
1675 | xug.xug_sogen = so_gencnt; | |
1676 | xug.xug_count = unp_count; | |
2d21ac55 | 1677 | error = SYSCTL_OUT(req, &xug, sizeof (xug)); |
1c79356b A |
1678 | } |
1679 | FREE(unp_list, M_TEMP); | |
37839358 | 1680 | lck_rw_done(unp_list_mtx); |
2d21ac55 | 1681 | return (error); |
1c79356b A |
1682 | } |
1683 | ||
fe8ab488 A |
1684 | SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, |
1685 | CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED, | |
b0d623f7 A |
1686 | (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb", |
1687 | "List of active local datagram sockets"); | |
fe8ab488 A |
1688 | SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, |
1689 | CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED, | |
b0d623f7 A |
1690 | (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb", |
1691 | "List of active local stream sockets"); | |
1692 | ||
b0d623f7 A |
1693 | |
1694 | static int | |
1695 | unp_pcblist64 SYSCTL_HANDLER_ARGS | |
1696 | { | |
1697 | #pragma unused(oidp,arg2) | |
1698 | int error, i, n; | |
1699 | struct unpcb *unp, **unp_list; | |
1700 | unp_gen_t gencnt; | |
1701 | struct xunpgen xug; | |
1702 | struct unp_head *head; | |
1703 | ||
1704 | lck_rw_lock_shared(unp_list_mtx); | |
1705 | head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead); | |
1706 | ||
1707 | /* | |
1708 | * The process of preparing the PCB list is too time-consuming and | |
1709 | * resource-intensive to repeat twice on every request. | |
1710 | */ | |
1711 | if (req->oldptr == USER_ADDR_NULL) { | |
1712 | n = unp_count; | |
1713 | req->oldidx = 2 * sizeof (xug) + (n + n / 8) * | |
1714 | (sizeof (struct xunpcb64)); | |
1715 | lck_rw_done(unp_list_mtx); | |
1716 | return (0); | |
1717 | } | |
1718 | ||
1719 | if (req->newptr != USER_ADDR_NULL) { | |
1720 | lck_rw_done(unp_list_mtx); | |
1721 | return (EPERM); | |
1722 | } | |
1723 | ||
1724 | /* | |
1725 | * OK, now we're committed to doing something. | |
1726 | */ | |
1727 | gencnt = unp_gencnt; | |
1728 | n = unp_count; | |
1729 | ||
1730 | bzero(&xug, sizeof (xug)); | |
1731 | xug.xug_len = sizeof (xug); | |
1732 | xug.xug_count = n; | |
1733 | xug.xug_gen = gencnt; | |
1734 | xug.xug_sogen = so_gencnt; | |
1735 | error = SYSCTL_OUT(req, &xug, sizeof (xug)); | |
1736 | if (error) { | |
1737 | lck_rw_done(unp_list_mtx); | |
1738 | return (error); | |
1739 | } | |
1740 | ||
1741 | /* | |
1742 | * We are done if there is no pcb | |
1743 | */ | |
1744 | if (n == 0) { | |
1745 | lck_rw_done(unp_list_mtx); | |
1746 | return (0); | |
1747 | } | |
1748 | ||
1749 | MALLOC(unp_list, struct unpcb **, n * sizeof (*unp_list), | |
1750 | M_TEMP, M_WAITOK); | |
1751 | if (unp_list == 0) { | |
1752 | lck_rw_done(unp_list_mtx); | |
1753 | return (ENOMEM); | |
1754 | } | |
1755 | ||
1756 | for (unp = head->lh_first, i = 0; unp && i < n; | |
1757 | unp = unp->unp_link.le_next) { | |
1758 | if (unp->unp_gencnt <= gencnt) | |
1759 | unp_list[i++] = unp; | |
1760 | } | |
1761 | n = i; /* in case we lost some during malloc */ | |
1762 | ||
1763 | error = 0; | |
1764 | for (i = 0; i < n; i++) { | |
1765 | unp = unp_list[i]; | |
1766 | if (unp->unp_gencnt <= gencnt) { | |
1767 | struct xunpcb64 xu; | |
1768 | size_t xu_len = sizeof(struct xunpcb64); | |
1769 | ||
1770 | bzero(&xu, xu_len); | |
1771 | xu.xu_len = xu_len; | |
316670eb A |
1772 | xu.xu_unpp = (u_int64_t)VM_KERNEL_ADDRPERM(unp); |
1773 | xu.xunp_link.le_next = (u_int64_t) | |
1774 | VM_KERNEL_ADDRPERM(unp->unp_link.le_next); | |
1775 | xu.xunp_link.le_prev = (u_int64_t) | |
1776 | VM_KERNEL_ADDRPERM(unp->unp_link.le_prev); | |
1777 | xu.xunp_socket = (u_int64_t) | |
1778 | VM_KERNEL_ADDRPERM(unp->unp_socket); | |
1779 | xu.xunp_vnode = (u_int64_t) | |
1780 | VM_KERNEL_ADDRPERM(unp->unp_vnode); | |
b0d623f7 | 1781 | xu.xunp_ino = unp->unp_ino; |
316670eb A |
1782 | xu.xunp_conn = (u_int64_t) |
1783 | VM_KERNEL_ADDRPERM(unp->unp_conn); | |
1784 | xu.xunp_refs = (u_int64_t) | |
1785 | VM_KERNEL_ADDRPERM(unp->unp_refs.lh_first); | |
1786 | xu.xunp_reflink.le_next = (u_int64_t) | |
1787 | VM_KERNEL_ADDRPERM(unp->unp_reflink.le_next); | |
1788 | xu.xunp_reflink.le_prev = (u_int64_t) | |
1789 | VM_KERNEL_ADDRPERM(unp->unp_reflink.le_prev); | |
b0d623f7 A |
1790 | xu.xunp_cc = unp->unp_cc; |
1791 | xu.xunp_mbcnt = unp->unp_mbcnt; | |
1792 | xu.xunp_gencnt = unp->unp_gencnt; | |
1793 | ||
1794 | if (unp->unp_socket) | |
1795 | sotoxsocket64(unp->unp_socket, &xu.xu_socket); | |
1796 | ||
1797 | /* | |
1798 | * XXX - need more locking here to protect against | |
1799 | * connect/disconnect races for SMP. | |
1800 | */ | |
1801 | if (unp->unp_addr) | |
1802 | bcopy(unp->unp_addr, &xu.xunp_addr, | |
1803 | unp->unp_addr->sun_len); | |
1804 | if (unp->unp_conn && unp->unp_conn->unp_addr) | |
1805 | bcopy(unp->unp_conn->unp_addr, | |
1806 | &xu.xunp_caddr, | |
1807 | unp->unp_conn->unp_addr->sun_len); | |
1808 | ||
1809 | error = SYSCTL_OUT(req, &xu, xu_len); | |
1810 | } | |
1811 | } | |
1812 | if (!error) { | |
1813 | /* | |
1814 | * Give the user an updated idea of our state. | |
1815 | * If the generation differs from what we told | |
1816 | * her before, she knows that something happened | |
1817 | * while we were processing this request, and it | |
1818 | * might be necessary to retry. | |
1819 | */ | |
1820 | bzero(&xug, sizeof (xug)); | |
1821 | xug.xug_len = sizeof (xug); | |
1822 | xug.xug_gen = unp_gencnt; | |
1823 | xug.xug_sogen = so_gencnt; | |
1824 | xug.xug_count = unp_count; | |
1825 | error = SYSCTL_OUT(req, &xug, sizeof (xug)); | |
1826 | } | |
1827 | FREE(unp_list, M_TEMP); | |
1828 | lck_rw_done(unp_list_mtx); | |
1829 | return (error); | |
1830 | } | |
1831 | ||
fe8ab488 A |
1832 | SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist64, |
1833 | CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED, | |
b0d623f7 A |
1834 | (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist64, "S,xunpcb64", |
1835 | "List of active local datagram sockets 64 bit"); | |
fe8ab488 A |
1836 | SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist64, |
1837 | CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED, | |
b0d623f7 A |
1838 | (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist64, "S,xunpcb64", |
1839 | "List of active local stream sockets 64 bit"); | |
1840 | ||
1c79356b A |
1841 | |
1842 | static void | |
91447636 | 1843 | unp_shutdown(struct unpcb *unp) |
1c79356b | 1844 | { |
b0d623f7 A |
1845 | struct socket *so = unp->unp_socket; |
1846 | struct socket *so2; | |
1847 | if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn) { | |
1848 | so2 = unp->unp_conn->unp_socket; | |
1849 | unp_get_locks_in_order(so, so2); | |
1850 | socantrcvmore(so2); | |
1851 | socket_unlock(so2, 1); | |
1852 | } | |
1c79356b A |
1853 | } |
1854 | ||
1855 | static void | |
2d21ac55 | 1856 | unp_drop(struct unpcb *unp, int errno) |
1c79356b A |
1857 | { |
1858 | struct socket *so = unp->unp_socket; | |
1859 | ||
1860 | so->so_error = errno; | |
1861 | unp_disconnect(unp); | |
1c79356b A |
1862 | } |
1863 | ||
2d21ac55 A |
1864 | /* |
1865 | * Returns: 0 Success | |
1866 | * EMSGSIZE The new fd's will not fit | |
1867 | * ENOBUFS Cannot alloc struct fileproc | |
1868 | */ | |
1c79356b | 1869 | int |
91447636 | 1870 | unp_externalize(struct mbuf *rights) |
1c79356b | 1871 | { |
2d21ac55 | 1872 | proc_t p = current_proc(); /* XXX */ |
91447636 A |
1873 | int i; |
1874 | struct cmsghdr *cm = mtod(rights, struct cmsghdr *); | |
1875 | struct fileglob **rp = (struct fileglob **)(cm + 1); | |
b0d623f7 | 1876 | int *fds = (int *)(cm + 1); |
91447636 | 1877 | struct fileproc *fp; |
3e170ce0 | 1878 | struct fileglob **fgl; |
2d21ac55 | 1879 | int newfds = (cm->cmsg_len - sizeof (*cm)) / sizeof (int); |
3e170ce0 A |
1880 | int f, error = 0; |
1881 | ||
1882 | MALLOC(fgl, struct fileglob **, newfds * sizeof (struct fileglob *), | |
1883 | M_TEMP, M_WAITOK); | |
1884 | if (fgl == NULL) { | |
1885 | error = ENOMEM; | |
1886 | goto discard; | |
1887 | } | |
1c79356b | 1888 | |
91447636 | 1889 | proc_fdlock(p); |
1c79356b A |
1890 | |
1891 | /* | |
1892 | * if the new FD's will not fit, then we free them all | |
1893 | */ | |
1894 | if (!fdavail(p, newfds)) { | |
91447636 | 1895 | proc_fdunlock(p); |
3e170ce0 A |
1896 | error = EMSGSIZE; |
1897 | goto discard; | |
1c79356b A |
1898 | } |
1899 | /* | |
2d21ac55 | 1900 | * now change each pointer to an fd in the global table to |
1c79356b A |
1901 | * an integer that is the index to the local fd table entry |
1902 | * that we set up to point to the global one we are transferring. | |
b0d623f7 A |
1903 | * XXX (1) this assumes a pointer and int are the same size, |
1904 | * XXX or the mbuf can hold the expansion | |
2d21ac55 | 1905 | * XXX (2) allocation failures should be non-fatal |
1c79356b A |
1906 | */ |
1907 | for (i = 0; i < newfds; i++) { | |
2d21ac55 A |
1908 | #if CONFIG_MACF_SOCKET |
1909 | /* | |
1910 | * If receive access is denied, don't pass along | |
1911 | * and error message, just discard the descriptor. | |
1912 | */ | |
3e170ce0 A |
1913 | if (mac_file_check_receive(kauth_cred_get(), rp[i])) { |
1914 | proc_fdunlock(p); | |
1915 | unp_discard(rp[i], p); | |
1916 | fds[i] = 0; | |
1917 | proc_fdlock(p); | |
2d21ac55 A |
1918 | continue; |
1919 | } | |
1920 | #endif | |
1c79356b | 1921 | if (fdalloc(p, 0, &f)) |
2d21ac55 | 1922 | panic("unp_externalize:fdalloc"); |
39236c6e | 1923 | fp = fileproc_alloc_init(NULL); |
2d21ac55 A |
1924 | if (fp == NULL) |
1925 | panic("unp_externalize: MALLOC_ZONE"); | |
91447636 | 1926 | fp->f_iocount = 0; |
3e170ce0 A |
1927 | fp->f_fglob = rp[i]; |
1928 | if (fg_removeuipc_mark(rp[i])) | |
1929 | fgl[i] = rp[i]; | |
1930 | else | |
1931 | fgl[i] = NULL; | |
6601e61a | 1932 | procfdtbl_releasefd(p, f, fp); |
b0d623f7 | 1933 | fds[i] = f; |
1c79356b | 1934 | } |
91447636 | 1935 | proc_fdunlock(p); |
1c79356b | 1936 | |
3e170ce0 A |
1937 | for (i = 0; i < newfds; i++) { |
1938 | if (fgl[i] != NULL) { | |
1939 | VERIFY(fgl[i]->fg_lflags & FG_RMMSGQ); | |
1940 | fg_removeuipc(fgl[i]); | |
1941 | } | |
1942 | if (fds[i]) | |
1943 | (void) OSAddAtomic(-1, &unp_rights); | |
1944 | } | |
1945 | ||
1946 | discard: | |
1947 | if (fgl) | |
1948 | FREE(fgl, M_TEMP); | |
1949 | if (error) { | |
1950 | for (i = 0; i < newfds; i++) { | |
1951 | unp_discard(*rp, p); | |
1952 | *rp++ = NULL; | |
1953 | } | |
1954 | } | |
1955 | return (error); | |
1c79356b A |
1956 | } |
1957 | ||
1958 | void | |
1959 | unp_init(void) | |
1960 | { | |
3e170ce0 | 1961 | _CASSERT(UIPC_MAX_CMSG_FD >= (MCLBYTES / sizeof(int))); |
2d21ac55 A |
1962 | unp_zone = zinit(sizeof (struct unpcb), |
1963 | (nmbclusters * sizeof (struct unpcb)), 4096, "unpzone"); | |
1964 | ||
1c79356b A |
1965 | if (unp_zone == 0) |
1966 | panic("unp_init"); | |
1967 | LIST_INIT(&unp_dhead); | |
1968 | LIST_INIT(&unp_shead); | |
2d21ac55 | 1969 | |
37839358 A |
1970 | /* |
1971 | * allocate lock group attribute and group for udp pcb mutexes | |
1972 | */ | |
1973 | unp_mtx_grp_attr = lck_grp_attr_alloc_init(); | |
1974 | ||
1975 | unp_mtx_grp = lck_grp_alloc_init("unp_list", unp_mtx_grp_attr); | |
2d21ac55 | 1976 | |
37839358 A |
1977 | unp_mtx_attr = lck_attr_alloc_init(); |
1978 | ||
2d21ac55 A |
1979 | if ((unp_list_mtx = lck_rw_alloc_init(unp_mtx_grp, |
1980 | unp_mtx_attr)) == NULL) | |
37839358 A |
1981 | return; /* pretty much dead if this fails... */ |
1982 | ||
b0d623f7 A |
1983 | if ((unp_disconnect_lock = lck_mtx_alloc_init(unp_mtx_grp, |
1984 | unp_mtx_attr)) == NULL) | |
1985 | return; | |
1986 | ||
1987 | if ((unp_connect_lock = lck_mtx_alloc_init(unp_mtx_grp, | |
1988 | unp_mtx_attr)) == NULL) | |
1989 | return; | |
1c79356b A |
1990 | } |
1991 | ||
1992 | #ifndef MIN | |
2d21ac55 | 1993 | #define MIN(a, b) (((a) < (b)) ? (a) : (b)) |
1c79356b A |
1994 | #endif |
1995 | ||
2d21ac55 A |
1996 | /* |
1997 | * Returns: 0 Success | |
1998 | * EINVAL | |
1999 | * fdgetf_noref:EBADF | |
2000 | */ | |
1c79356b | 2001 | static int |
2d21ac55 | 2002 | unp_internalize(struct mbuf *control, proc_t p) |
1c79356b | 2003 | { |
91447636 | 2004 | struct cmsghdr *cm = mtod(control, struct cmsghdr *); |
b0d623f7 | 2005 | int *fds; |
91447636 A |
2006 | struct fileglob **rp; |
2007 | struct fileproc *fp; | |
2d21ac55 | 2008 | int i, error; |
1c79356b | 2009 | int oldfds; |
3e170ce0 | 2010 | uint8_t fg_ins[UIPC_MAX_CMSG_FD / 8]; |
1c79356b | 2011 | |
2d21ac55 | 2012 | /* 64bit: cmsg_len is 'uint32_t', m_len is 'long' */ |
1c79356b | 2013 | if (cm->cmsg_type != SCM_RIGHTS || cm->cmsg_level != SOL_SOCKET || |
b0d623f7 | 2014 | (socklen_t)cm->cmsg_len != (socklen_t)control->m_len) { |
2d21ac55 | 2015 | return (EINVAL); |
1c79356b | 2016 | } |
1c79356b | 2017 | oldfds = (cm->cmsg_len - sizeof (*cm)) / sizeof (int); |
3e170ce0 | 2018 | bzero(fg_ins, sizeof(fg_ins)); |
1c79356b | 2019 | |
91447636 | 2020 | proc_fdlock(p); |
b0d623f7 | 2021 | fds = (int *)(cm + 1); |
91447636 A |
2022 | |
2023 | for (i = 0; i < oldfds; i++) { | |
b0d623f7 A |
2024 | struct fileproc *tmpfp; |
2025 | if (((error = fdgetf_noref(p, fds[i], &tmpfp)) != 0)) { | |
2d21ac55 A |
2026 | proc_fdunlock(p); |
2027 | return (error); | |
3e170ce0 | 2028 | } else if (!file_issendable(p, tmpfp)) { |
b0d623f7 A |
2029 | proc_fdunlock(p); |
2030 | return (EINVAL); | |
39236c6e A |
2031 | } else if (FP_ISGUARDED(tmpfp, GUARD_SOCKET_IPC)) { |
2032 | error = fp_guard_exception(p, | |
2033 | fds[i], tmpfp, kGUARD_EXC_SOCKET_IPC); | |
2034 | proc_fdunlock(p); | |
2035 | return (error); | |
2d21ac55 | 2036 | } |
91447636 A |
2037 | } |
2038 | rp = (struct fileglob **)(cm + 1); | |
1c79356b | 2039 | |
b0d623f7 A |
2040 | /* On K64 we need to walk backwards because a fileglob * is twice the size of an fd |
2041 | * and doing them in-order would result in stomping over unprocessed fd's | |
2042 | */ | |
2043 | for (i = (oldfds - 1); i >= 0; i--) { | |
2044 | (void) fdgetf_noref(p, fds[i], &fp); | |
3e170ce0 A |
2045 | if (fg_insertuipc_mark(fp->f_fglob)) |
2046 | fg_ins[i / 8] |= 0x80 >> (i % 8); | |
b0d623f7 | 2047 | rp[i] = fp->f_fglob; |
1c79356b | 2048 | } |
91447636 | 2049 | proc_fdunlock(p); |
1c79356b | 2050 | |
3e170ce0 A |
2051 | for (i = 0; i < oldfds; i++) { |
2052 | if (fg_ins[i / 8] & (0x80 >> (i % 8))) { | |
2053 | VERIFY(rp[i]->fg_lflags & FG_INSMSGQ); | |
2054 | fg_insertuipc(rp[i]); | |
2055 | } | |
2056 | (void) OSAddAtomic(1, &unp_rights); | |
2057 | } | |
2058 | ||
1c79356b A |
2059 | return (0); |
2060 | } | |
2061 | ||
6601e61a | 2062 | static int unp_defer, unp_gcing, unp_gcwait; |
e2fac8b1 | 2063 | static thread_t unp_gcthread = NULL; |
2d21ac55 | 2064 | |
6601e61a A |
2065 | /* always called under uipc_lock */ |
2066 | void | |
2067 | unp_gc_wait(void) | |
2068 | { | |
e2fac8b1 A |
2069 | if (unp_gcthread == current_thread()) |
2070 | return; | |
2071 | ||
6601e61a A |
2072 | while (unp_gcing != 0) { |
2073 | unp_gcwait = 1; | |
2074 | msleep(&unp_gcing, uipc_lock, 0 , "unp_gc_wait", NULL); | |
2075 | } | |
2076 | } | |
1c79356b | 2077 | |
2d21ac55 | 2078 | |
e2fac8b1 | 2079 | __private_extern__ void |
2d21ac55 | 2080 | unp_gc(void) |
1c79356b | 2081 | { |
2d21ac55 A |
2082 | struct fileglob *fg, *nextfg; |
2083 | struct socket *so; | |
e2fac8b1 | 2084 | static struct fileglob **extra_ref; |
b0d623f7 | 2085 | struct fileglob **fpp; |
1c79356b | 2086 | int nunref, i; |
6601e61a | 2087 | int need_gcwakeup = 0; |
2d21ac55 | 2088 | |
91447636 A |
2089 | lck_mtx_lock(uipc_lock); |
2090 | if (unp_gcing) { | |
2091 | lck_mtx_unlock(uipc_lock); | |
1c79356b | 2092 | return; |
91447636 | 2093 | } |
1c79356b A |
2094 | unp_gcing = 1; |
2095 | unp_defer = 0; | |
e2fac8b1 | 2096 | unp_gcthread = current_thread(); |
91447636 | 2097 | lck_mtx_unlock(uipc_lock); |
2d21ac55 A |
2098 | /* |
2099 | * before going through all this, set all FDs to | |
1c79356b A |
2100 | * be NOT defered and NOT externally accessible |
2101 | */ | |
91447636 A |
2102 | for (fg = fmsghead.lh_first; fg != 0; fg = fg->f_msglist.le_next) { |
2103 | lck_mtx_lock(&fg->fg_lock); | |
2104 | fg->fg_flag &= ~(FMARK|FDEFER); | |
2105 | lck_mtx_unlock(&fg->fg_lock); | |
2106 | } | |
1c79356b | 2107 | do { |
2d21ac55 A |
2108 | for (fg = fmsghead.lh_first; fg != 0; |
2109 | fg = fg->f_msglist.le_next) { | |
91447636 | 2110 | lck_mtx_lock(&fg->fg_lock); |
1c79356b A |
2111 | /* |
2112 | * If the file is not open, skip it | |
2113 | */ | |
91447636 A |
2114 | if (fg->fg_count == 0) { |
2115 | lck_mtx_unlock(&fg->fg_lock); | |
1c79356b | 2116 | continue; |
91447636 | 2117 | } |
1c79356b A |
2118 | /* |
2119 | * If we already marked it as 'defer' in a | |
2120 | * previous pass, then try process it this time | |
2121 | * and un-mark it | |
2122 | */ | |
91447636 A |
2123 | if (fg->fg_flag & FDEFER) { |
2124 | fg->fg_flag &= ~FDEFER; | |
1c79356b A |
2125 | unp_defer--; |
2126 | } else { | |
2127 | /* | |
2128 | * if it's not defered, then check if it's | |
2129 | * already marked.. if so skip it | |
2130 | */ | |
2d21ac55 | 2131 | if (fg->fg_flag & FMARK) { |
91447636 | 2132 | lck_mtx_unlock(&fg->fg_lock); |
1c79356b | 2133 | continue; |
91447636 | 2134 | } |
2d21ac55 | 2135 | /* |
1c79356b | 2136 | * If all references are from messages |
2d21ac55 | 2137 | * in transit, then skip it. it's not |
1c79356b | 2138 | * externally accessible. |
2d21ac55 | 2139 | */ |
91447636 A |
2140 | if (fg->fg_count == fg->fg_msgcount) { |
2141 | lck_mtx_unlock(&fg->fg_lock); | |
1c79356b | 2142 | continue; |
91447636 | 2143 | } |
2d21ac55 | 2144 | /* |
1c79356b A |
2145 | * If it got this far then it must be |
2146 | * externally accessible. | |
2147 | */ | |
91447636 | 2148 | fg->fg_flag |= FMARK; |
1c79356b A |
2149 | } |
2150 | /* | |
2d21ac55 | 2151 | * either it was defered, or it is externally |
1c79356b A |
2152 | * accessible and not already marked so. |
2153 | * Now check if it is possibly one of OUR sockets. | |
2d21ac55 | 2154 | */ |
39236c6e | 2155 | if (FILEGLOB_DTYPE(fg) != DTYPE_SOCKET || |
91447636 A |
2156 | (so = (struct socket *)fg->fg_data) == 0) { |
2157 | lck_mtx_unlock(&fg->fg_lock); | |
1c79356b | 2158 | continue; |
91447636 | 2159 | } |
39236c6e | 2160 | if (so->so_proto->pr_domain != localdomain || |
91447636 A |
2161 | (so->so_proto->pr_flags&PR_RIGHTS) == 0) { |
2162 | lck_mtx_unlock(&fg->fg_lock); | |
1c79356b | 2163 | continue; |
91447636 | 2164 | } |
1c79356b | 2165 | #ifdef notdef |
1c79356b A |
2166 | if (so->so_rcv.sb_flags & SB_LOCK) { |
2167 | /* | |
2168 | * This is problematical; it's not clear | |
2169 | * we need to wait for the sockbuf to be | |
2170 | * unlocked (on a uniprocessor, at least), | |
2171 | * and it's also not clear what to do | |
2172 | * if sbwait returns an error due to receipt | |
2173 | * of a signal. If sbwait does return | |
2174 | * an error, we'll go into an infinite | |
2175 | * loop. Delete all of this for now. | |
2176 | */ | |
2177 | (void) sbwait(&so->so_rcv); | |
2178 | goto restart; | |
2179 | } | |
2180 | #endif | |
2181 | /* | |
2182 | * So, Ok, it's one of our sockets and it IS externally | |
2183 | * accessible (or was defered). Now we look | |
2184 | * to see if we hold any file descriptors in its | |
2d21ac55 | 2185 | * message buffers. Follow those links and mark them |
1c79356b | 2186 | * as accessible too. |
e2fac8b1 | 2187 | * |
b0d623f7 | 2188 | * In case a file is passed onto itself we need to |
e2fac8b1 | 2189 | * release the file lock. |
1c79356b | 2190 | */ |
91447636 | 2191 | lck_mtx_unlock(&fg->fg_lock); |
e2fac8b1 | 2192 | |
3e170ce0 | 2193 | unp_scan(so->so_rcv.sb_mb, unp_mark, 0); |
1c79356b A |
2194 | } |
2195 | } while (unp_defer); | |
2196 | /* | |
2197 | * We grab an extra reference to each of the file table entries | |
2198 | * that are not otherwise accessible and then free the rights | |
2199 | * that are stored in messages on them. | |
2200 | * | |
2201 | * The bug in the orginal code is a little tricky, so I'll describe | |
2202 | * what's wrong with it here. | |
2203 | * | |
2204 | * It is incorrect to simply unp_discard each entry for f_msgcount | |
2205 | * times -- consider the case of sockets A and B that contain | |
2206 | * references to each other. On a last close of some other socket, | |
2207 | * we trigger a gc since the number of outstanding rights (unp_rights) | |
2208 | * is non-zero. If during the sweep phase the gc code un_discards, | |
2209 | * we end up doing a (full) closef on the descriptor. A closef on A | |
2210 | * results in the following chain. Closef calls soo_close, which | |
2211 | * calls soclose. Soclose calls first (through the switch | |
2212 | * uipc_usrreq) unp_detach, which re-invokes unp_gc. Unp_gc simply | |
2213 | * returns because the previous instance had set unp_gcing, and | |
2214 | * we return all the way back to soclose, which marks the socket | |
2215 | * with SS_NOFDREF, and then calls sofree. Sofree calls sorflush | |
2216 | * to free up the rights that are queued in messages on the socket A, | |
2217 | * i.e., the reference on B. The sorflush calls via the dom_dispose | |
2218 | * switch unp_dispose, which unp_scans with unp_discard. This second | |
2219 | * instance of unp_discard just calls closef on B. | |
2220 | * | |
2221 | * Well, a similar chain occurs on B, resulting in a sorflush on B, | |
2222 | * which results in another closef on A. Unfortunately, A is already | |
2223 | * being closed, and the descriptor has already been marked with | |
2224 | * SS_NOFDREF, and soclose panics at this point. | |
2225 | * | |
2226 | * Here, we first take an extra reference to each inaccessible | |
2227 | * descriptor. Then, we call sorflush ourself, since we know | |
2228 | * it is a Unix domain socket anyhow. After we destroy all the | |
2229 | * rights carried in messages, we do a last closef to get rid | |
2230 | * of our extra reference. This is the last close, and the | |
2231 | * unp_detach etc will shut down the socket. | |
2232 | * | |
2233 | * 91/09/19, bsy@cs.cmu.edu | |
2234 | */ | |
2d21ac55 A |
2235 | extra_ref = _MALLOC(nfiles * sizeof (struct fileglob *), |
2236 | M_FILEGLOB, M_WAITOK); | |
b0d623f7 A |
2237 | if (extra_ref == NULL) |
2238 | goto bail; | |
91447636 A |
2239 | for (nunref = 0, fg = fmsghead.lh_first, fpp = extra_ref; fg != 0; |
2240 | fg = nextfg) { | |
2241 | lck_mtx_lock(&fg->fg_lock); | |
2242 | ||
2243 | nextfg = fg->f_msglist.le_next; | |
2d21ac55 | 2244 | /* |
1c79356b A |
2245 | * If it's not open, skip it |
2246 | */ | |
91447636 A |
2247 | if (fg->fg_count == 0) { |
2248 | lck_mtx_unlock(&fg->fg_lock); | |
1c79356b | 2249 | continue; |
91447636 | 2250 | } |
2d21ac55 | 2251 | /* |
1c79356b A |
2252 | * If all refs are from msgs, and it's not marked accessible |
2253 | * then it must be referenced from some unreachable cycle | |
2254 | * of (shut-down) FDs, so include it in our | |
2255 | * list of FDs to remove | |
2256 | */ | |
91447636 A |
2257 | if (fg->fg_count == fg->fg_msgcount && !(fg->fg_flag & FMARK)) { |
2258 | fg->fg_count++; | |
2259 | *fpp++ = fg; | |
1c79356b | 2260 | nunref++; |
1c79356b | 2261 | } |
91447636 | 2262 | lck_mtx_unlock(&fg->fg_lock); |
1c79356b | 2263 | } |
2d21ac55 | 2264 | /* |
1c79356b A |
2265 | * for each FD on our hit list, do the following two things |
2266 | */ | |
2267 | for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) { | |
91447636 | 2268 | struct fileglob *tfg; |
1c79356b | 2269 | |
91447636 | 2270 | tfg = *fpp; |
1c79356b | 2271 | |
39236c6e A |
2272 | if (FILEGLOB_DTYPE(tfg) == DTYPE_SOCKET && |
2273 | tfg->fg_data != NULL) { | |
2d21ac55 A |
2274 | so = (struct socket *)(tfg->fg_data); |
2275 | ||
e2fac8b1 | 2276 | socket_lock(so, 0); |
b0d623f7 | 2277 | |
2d21ac55 A |
2278 | sorflush(so); |
2279 | ||
e2fac8b1 | 2280 | socket_unlock(so, 0); |
91447636 A |
2281 | } |
2282 | } | |
1c79356b | 2283 | for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) |
2d21ac55 A |
2284 | closef_locked((struct fileproc *)0, *fpp, (proc_t)NULL); |
2285 | ||
b0d623f7 A |
2286 | FREE((caddr_t)extra_ref, M_FILEGLOB); |
2287 | bail: | |
2d21ac55 | 2288 | lck_mtx_lock(uipc_lock); |
1c79356b | 2289 | unp_gcing = 0; |
e2fac8b1 | 2290 | unp_gcthread = NULL; |
6601e61a A |
2291 | |
2292 | if (unp_gcwait != 0) { | |
2293 | unp_gcwait = 0; | |
2294 | need_gcwakeup = 1; | |
2295 | } | |
2296 | lck_mtx_unlock(uipc_lock); | |
2297 | ||
2298 | if (need_gcwakeup != 0) | |
2299 | wakeup(&unp_gcing); | |
1c79356b A |
2300 | } |
2301 | ||
2302 | void | |
91447636 | 2303 | unp_dispose(struct mbuf *m) |
1c79356b | 2304 | { |
1c79356b | 2305 | if (m) { |
3e170ce0 | 2306 | unp_scan(m, unp_discard, NULL); |
1c79356b A |
2307 | } |
2308 | } | |
2309 | ||
2d21ac55 A |
2310 | /* |
2311 | * Returns: 0 Success | |
2312 | */ | |
91447636 | 2313 | static int |
2d21ac55 | 2314 | unp_listen(struct unpcb *unp, proc_t p) |
91447636 | 2315 | { |
0c530ab8 A |
2316 | kauth_cred_t safecred = kauth_cred_proc_ref(p); |
2317 | cru2x(safecred, &unp->unp_peercred); | |
2318 | kauth_cred_unref(&safecred); | |
91447636 A |
2319 | unp->unp_flags |= UNP_HAVEPCCACHED; |
2320 | return (0); | |
2321 | } | |
2322 | ||
1c79356b | 2323 | static void |
3e170ce0 | 2324 | unp_scan(struct mbuf *m0, void (*op)(struct fileglob *, void *arg), void *arg) |
1c79356b | 2325 | { |
91447636 A |
2326 | struct mbuf *m; |
2327 | struct fileglob **rp; | |
2328 | struct cmsghdr *cm; | |
2329 | int i; | |
1c79356b A |
2330 | int qfds; |
2331 | ||
2332 | while (m0) { | |
2333 | for (m = m0; m; m = m->m_next) | |
2334 | if (m->m_type == MT_CONTROL && | |
2d21ac55 | 2335 | (size_t)m->m_len >= sizeof (*cm)) { |
1c79356b A |
2336 | cm = mtod(m, struct cmsghdr *); |
2337 | if (cm->cmsg_level != SOL_SOCKET || | |
2338 | cm->cmsg_type != SCM_RIGHTS) | |
2339 | continue; | |
2d21ac55 | 2340 | qfds = (cm->cmsg_len - sizeof (*cm)) / |
b0d623f7 | 2341 | sizeof (int); |
91447636 | 2342 | rp = (struct fileglob **)(cm + 1); |
1c79356b | 2343 | for (i = 0; i < qfds; i++) |
3e170ce0 | 2344 | (*op)(*rp++, arg); |
1c79356b A |
2345 | break; /* XXX, but saves time */ |
2346 | } | |
2347 | m0 = m0->m_act; | |
2348 | } | |
2349 | } | |
2350 | ||
1c79356b | 2351 | static void |
3e170ce0 | 2352 | unp_mark(struct fileglob *fg, __unused void *arg) |
1c79356b | 2353 | { |
2d21ac55 | 2354 | lck_mtx_lock(&fg->fg_lock); |
1c79356b | 2355 | |
91447636 | 2356 | if (fg->fg_flag & FMARK) { |
2d21ac55 | 2357 | lck_mtx_unlock(&fg->fg_lock); |
1c79356b | 2358 | return; |
91447636 A |
2359 | } |
2360 | fg->fg_flag |= (FMARK|FDEFER); | |
2361 | ||
2d21ac55 | 2362 | lck_mtx_unlock(&fg->fg_lock); |
91447636 | 2363 | |
1c79356b | 2364 | unp_defer++; |
1c79356b A |
2365 | } |
2366 | ||
1c79356b | 2367 | static void |
3e170ce0 | 2368 | unp_discard(struct fileglob *fg, void *p) |
1c79356b | 2369 | { |
3e170ce0 A |
2370 | if (p == NULL) |
2371 | p = current_proc(); /* XXX */ | |
2d21ac55 | 2372 | |
b0d623f7 | 2373 | (void) OSAddAtomic(1, &unp_disposed); |
3e170ce0 A |
2374 | if (fg_removeuipc_mark(fg)) { |
2375 | VERIFY(fg->fg_lflags & FG_RMMSGQ); | |
2376 | fg_removeuipc(fg); | |
2377 | } | |
2378 | (void) OSAddAtomic(-1, &unp_rights); | |
91447636 A |
2379 | |
2380 | proc_fdlock(p); | |
91447636 | 2381 | (void) closef_locked((struct fileproc *)0, fg, p); |
3e170ce0 | 2382 | proc_fdunlock(p); |
1c79356b | 2383 | } |
b0d623f7 A |
2384 | |
2385 | int | |
2386 | unp_lock(struct socket *so, int refcount, void * lr) | |
2387 | { | |
2388 | void * lr_saved; | |
2389 | if (lr == 0) | |
2390 | lr_saved = (void *) __builtin_return_address(0); | |
2391 | else lr_saved = lr; | |
2392 | ||
2393 | if (so->so_pcb) { | |
6d2010ae | 2394 | lck_mtx_lock(&((struct unpcb *)so->so_pcb)->unp_mtx); |
b0d623f7 A |
2395 | } else { |
2396 | panic("unp_lock: so=%p NO PCB! lr=%p ref=0x%x\n", | |
2397 | so, lr_saved, so->so_usecount); | |
2398 | } | |
2399 | ||
2400 | if (so->so_usecount < 0) | |
2401 | panic("unp_lock: so=%p so_pcb=%p lr=%p ref=0x%x\n", | |
2402 | so, so->so_pcb, lr_saved, so->so_usecount); | |
2403 | ||
2404 | if (refcount) | |
2405 | so->so_usecount++; | |
2406 | ||
2407 | so->lock_lr[so->next_lock_lr] = lr_saved; | |
2408 | so->next_lock_lr = (so->next_lock_lr+1) % SO_LCKDBG_MAX; | |
2409 | return (0); | |
2410 | } | |
2411 | ||
2412 | int | |
2413 | unp_unlock(struct socket *so, int refcount, void * lr) | |
2414 | { | |
2415 | void * lr_saved; | |
2416 | lck_mtx_t * mutex_held = NULL; | |
2417 | struct unpcb *unp = sotounpcb(so); | |
2418 | ||
2419 | if (lr == 0) | |
2420 | lr_saved = (void *) __builtin_return_address(0); | |
2421 | else lr_saved = lr; | |
2422 | ||
2423 | if (refcount) | |
2424 | so->so_usecount--; | |
2425 | ||
2426 | if (so->so_usecount < 0) | |
2427 | panic("unp_unlock: so=%p usecount=%x\n", so, so->so_usecount); | |
2428 | if (so->so_pcb == NULL) { | |
2429 | panic("unp_unlock: so=%p NO PCB usecount=%x\n", so, so->so_usecount); | |
2430 | } else { | |
6d2010ae | 2431 | mutex_held = &((struct unpcb *)so->so_pcb)->unp_mtx; |
b0d623f7 A |
2432 | } |
2433 | lck_mtx_assert(mutex_held, LCK_MTX_ASSERT_OWNED); | |
2434 | so->unlock_lr[so->next_unlock_lr] = lr_saved; | |
2435 | so->next_unlock_lr = (so->next_unlock_lr+1) % SO_LCKDBG_MAX; | |
2436 | ||
2437 | if (so->so_usecount == 0 && (so->so_flags & SOF_PCBCLEARING)) { | |
2438 | sofreelastref(so, 1); | |
2439 | ||
2440 | if (unp->unp_addr) | |
2441 | FREE(unp->unp_addr, M_SONAME); | |
2442 | ||
2443 | lck_mtx_unlock(mutex_held); | |
b0d623f7 | 2444 | |
316670eb | 2445 | lck_mtx_destroy(&unp->unp_mtx, unp_mtx_grp); |
b0d623f7 | 2446 | zfree(unp_zone, unp); |
b0d623f7 A |
2447 | |
2448 | unp_gc(); | |
2449 | } else { | |
2450 | lck_mtx_unlock(mutex_held); | |
2451 | } | |
2452 | ||
2453 | return (0); | |
2454 | } | |
2455 | ||
2456 | lck_mtx_t * | |
2457 | unp_getlock(struct socket *so, __unused int locktype) | |
2458 | { | |
2459 | struct unpcb *unp = (struct unpcb *)so->so_pcb; | |
2460 | ||
2461 | ||
2462 | if (so->so_pcb) { | |
2463 | if (so->so_usecount < 0) | |
2464 | panic("unp_getlock: so=%p usecount=%x\n", so, so->so_usecount); | |
6d2010ae | 2465 | return(&unp->unp_mtx); |
b0d623f7 A |
2466 | } else { |
2467 | panic("unp_getlock: so=%p NULL so_pcb\n", so); | |
2468 | return (so->so_proto->pr_domain->dom_mtx); | |
2469 | } | |
2470 | } | |
2471 |