]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
91447636 | 2 | * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved. |
1c79356b A |
3 | * |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
37839358 A |
6 | * The contents of this file constitute Original Code as defined in and |
7 | * are subject to the Apple Public Source License Version 1.1 (the | |
8 | * "License"). You may not use this file except in compliance with the | |
9 | * License. Please obtain a copy of the License at | |
10 | * http://www.apple.com/publicsource and read it before using this file. | |
1c79356b | 11 | * |
37839358 A |
12 | * This Original Code and all software distributed under the License are |
13 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
1c79356b A |
14 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
15 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
37839358 A |
16 | * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the |
17 | * License for the specific language governing rights and limitations | |
18 | * under the License. | |
1c79356b A |
19 | * |
20 | * @APPLE_LICENSE_HEADER_END@ | |
21 | */ | |
22 | /* | |
23 | * Copyright (c) 1982, 1986, 1989, 1991, 1993 | |
24 | * The Regents of the University of California. All rights reserved. | |
25 | * | |
26 | * Redistribution and use in source and binary forms, with or without | |
27 | * modification, are permitted provided that the following conditions | |
28 | * are met: | |
29 | * 1. Redistributions of source code must retain the above copyright | |
30 | * notice, this list of conditions and the following disclaimer. | |
31 | * 2. Redistributions in binary form must reproduce the above copyright | |
32 | * notice, this list of conditions and the following disclaimer in the | |
33 | * documentation and/or other materials provided with the distribution. | |
34 | * 3. All advertising materials mentioning features or use of this software | |
35 | * must display the following acknowledgement: | |
36 | * This product includes software developed by the University of | |
37 | * California, Berkeley and its contributors. | |
38 | * 4. Neither the name of the University nor the names of its contributors | |
39 | * may be used to endorse or promote products derived from this software | |
40 | * without specific prior written permission. | |
41 | * | |
42 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
43 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
44 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
45 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
46 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
47 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
48 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
49 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
50 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
51 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
52 | * SUCH DAMAGE. | |
53 | * | |
54 | * From: @(#)uipc_usrreq.c 8.3 (Berkeley) 1/4/94 | |
55 | */ | |
56 | ||
57 | #include <sys/param.h> | |
58 | #include <sys/systm.h> | |
59 | #include <sys/kernel.h> | |
60 | #include <sys/domain.h> | |
61 | #include <sys/fcntl.h> | |
62 | #include <sys/malloc.h> /* XXX must be before <sys/file.h> */ | |
91447636 | 63 | #include <sys/file_internal.h> |
1c79356b A |
64 | #include <sys/filedesc.h> |
65 | #include <sys/lock.h> | |
66 | #include <sys/mbuf.h> | |
67 | #include <sys/namei.h> | |
91447636 A |
68 | #include <sys/proc_internal.h> |
69 | #include <sys/kauth.h> | |
1c79356b A |
70 | #include <sys/protosw.h> |
71 | #include <sys/socket.h> | |
72 | #include <sys/socketvar.h> | |
73 | #include <sys/stat.h> | |
74 | #include <sys/sysctl.h> | |
75 | #include <sys/un.h> | |
76 | #include <sys/unpcb.h> | |
91447636 A |
77 | #include <sys/vnode_internal.h> |
78 | #include <sys/kdebug.h> | |
1c79356b A |
79 | |
80 | #include <kern/zalloc.h> | |
91447636 | 81 | #include <kern/locks.h> |
1c79356b | 82 | |
91447636 A |
83 | #define f_msgcount f_fglob->fg_msgcount |
84 | #define f_cred f_fglob->fg_cred | |
85 | #define f_ops f_fglob->fg_ops | |
86 | #define f_offset f_fglob->fg_offset | |
87 | #define f_data f_fglob->fg_data | |
1c79356b A |
88 | struct zone *unp_zone; |
89 | static unp_gen_t unp_gencnt; | |
90 | static u_int unp_count; | |
37839358 A |
91 | |
92 | static lck_attr_t *unp_mtx_attr; | |
93 | static lck_grp_t *unp_mtx_grp; | |
94 | static lck_grp_attr_t *unp_mtx_grp_attr; | |
95 | static lck_rw_t *unp_list_mtx; | |
1c79356b | 96 | |
91447636 | 97 | extern lck_mtx_t * uipc_lock; |
1c79356b A |
98 | static struct unp_head unp_shead, unp_dhead; |
99 | ||
100 | /* | |
101 | * Unix communications domain. | |
102 | * | |
103 | * TODO: | |
104 | * SEQPACKET, RDM | |
105 | * rethink name space problems | |
106 | * need a proper out-of-band | |
107 | * lock pushdown | |
108 | */ | |
91447636 | 109 | static struct sockaddr sun_noname = { sizeof(sun_noname), AF_LOCAL, { 0 } }; |
1c79356b A |
110 | static ino_t unp_ino; /* prototype for fake inode numbers */ |
111 | ||
91447636 A |
112 | static int unp_attach(struct socket *); |
113 | static void unp_detach(struct unpcb *); | |
114 | static int unp_bind(struct unpcb *,struct sockaddr *, struct proc *); | |
115 | static int unp_connect(struct socket *,struct sockaddr *, struct proc *); | |
116 | static void unp_disconnect(struct unpcb *); | |
117 | static void unp_shutdown(struct unpcb *); | |
118 | static void unp_drop(struct unpcb *, int); | |
119 | static void unp_gc(void); | |
120 | static void unp_scan(struct mbuf *, void (*)(struct fileglob *)); | |
121 | static void unp_mark(struct fileglob *); | |
122 | static void unp_discard(struct fileglob *); | |
123 | static void unp_discard_fdlocked(struct fileglob *, struct proc *); | |
124 | static int unp_internalize(struct mbuf *, struct proc *); | |
125 | static int unp_listen(struct unpcb *, struct proc *); | |
126 | ||
1c79356b A |
127 | |
128 | static int | |
129 | uipc_abort(struct socket *so) | |
130 | { | |
131 | struct unpcb *unp = sotounpcb(so); | |
132 | ||
133 | if (unp == 0) | |
134 | return EINVAL; | |
135 | unp_drop(unp, ECONNABORTED); | |
91447636 A |
136 | unp_detach(unp); |
137 | sofree(so); | |
1c79356b A |
138 | return 0; |
139 | } | |
140 | ||
141 | static int | |
142 | uipc_accept(struct socket *so, struct sockaddr **nam) | |
143 | { | |
144 | struct unpcb *unp = sotounpcb(so); | |
145 | ||
146 | if (unp == 0) | |
147 | return EINVAL; | |
148 | ||
149 | /* | |
150 | * Pass back name of connected socket, | |
151 | * if it was bound and we are still connected | |
152 | * (our peer may have closed already!). | |
153 | */ | |
154 | if (unp->unp_conn && unp->unp_conn->unp_addr) { | |
155 | *nam = dup_sockaddr((struct sockaddr *)unp->unp_conn->unp_addr, | |
156 | 1); | |
157 | } else { | |
158 | *nam = dup_sockaddr((struct sockaddr *)&sun_noname, 1); | |
159 | } | |
160 | return 0; | |
161 | } | |
162 | ||
163 | static int | |
91447636 | 164 | uipc_attach(struct socket *so, __unused int proto, __unused struct proc *p) |
1c79356b A |
165 | { |
166 | struct unpcb *unp = sotounpcb(so); | |
167 | ||
168 | if (unp != 0) | |
169 | return EISCONN; | |
170 | return unp_attach(so); | |
171 | } | |
172 | ||
173 | static int | |
174 | uipc_bind(struct socket *so, struct sockaddr *nam, struct proc *p) | |
175 | { | |
176 | struct unpcb *unp = sotounpcb(so); | |
177 | ||
178 | if (unp == 0) | |
179 | return EINVAL; | |
180 | ||
181 | return unp_bind(unp, nam, p); | |
182 | } | |
183 | ||
184 | static int | |
185 | uipc_connect(struct socket *so, struct sockaddr *nam, struct proc *p) | |
186 | { | |
187 | struct unpcb *unp = sotounpcb(so); | |
188 | ||
189 | if (unp == 0) | |
190 | return EINVAL; | |
191 | return unp_connect(so, nam, p); | |
192 | } | |
193 | ||
194 | static int | |
195 | uipc_connect2(struct socket *so1, struct socket *so2) | |
196 | { | |
197 | struct unpcb *unp = sotounpcb(so1); | |
198 | ||
199 | if (unp == 0) | |
200 | return EINVAL; | |
201 | ||
202 | return unp_connect2(so1, so2); | |
203 | } | |
204 | ||
205 | /* control is EOPNOTSUPP */ | |
206 | ||
207 | static int | |
208 | uipc_detach(struct socket *so) | |
209 | { | |
210 | struct unpcb *unp = sotounpcb(so); | |
211 | ||
212 | if (unp == 0) | |
213 | return EINVAL; | |
214 | ||
215 | unp_detach(unp); | |
216 | return 0; | |
217 | } | |
218 | ||
219 | static int | |
220 | uipc_disconnect(struct socket *so) | |
221 | { | |
222 | struct unpcb *unp = sotounpcb(so); | |
223 | ||
224 | if (unp == 0) | |
225 | return EINVAL; | |
226 | unp_disconnect(unp); | |
227 | return 0; | |
228 | } | |
229 | ||
230 | static int | |
91447636 | 231 | uipc_listen(struct socket *so, __unused struct proc *p) |
1c79356b A |
232 | { |
233 | struct unpcb *unp = sotounpcb(so); | |
234 | ||
235 | if (unp == 0 || unp->unp_vnode == 0) | |
236 | return EINVAL; | |
91447636 | 237 | return unp_listen(unp, p); |
1c79356b A |
238 | } |
239 | ||
240 | static int | |
241 | uipc_peeraddr(struct socket *so, struct sockaddr **nam) | |
242 | { | |
243 | struct unpcb *unp = sotounpcb(so); | |
244 | ||
245 | if (unp == 0) | |
246 | return EINVAL; | |
247 | if (unp->unp_conn && unp->unp_conn->unp_addr) | |
248 | *nam = dup_sockaddr((struct sockaddr *)unp->unp_conn->unp_addr, | |
249 | 1); | |
250 | return 0; | |
251 | } | |
252 | ||
253 | static int | |
91447636 | 254 | uipc_rcvd(struct socket *so, __unused int flags) |
1c79356b A |
255 | { |
256 | struct unpcb *unp = sotounpcb(so); | |
257 | struct socket *so2; | |
258 | ||
259 | if (unp == 0) | |
260 | return EINVAL; | |
261 | switch (so->so_type) { | |
262 | case SOCK_DGRAM: | |
263 | panic("uipc_rcvd DGRAM?"); | |
264 | /*NOTREACHED*/ | |
265 | ||
266 | case SOCK_STREAM: | |
267 | #define rcv (&so->so_rcv) | |
268 | #define snd (&so2->so_snd) | |
269 | if (unp->unp_conn == 0) | |
270 | break; | |
271 | so2 = unp->unp_conn->unp_socket; | |
272 | /* | |
273 | * Adjust backpressure on sender | |
274 | * and wakeup any waiting to write. | |
275 | */ | |
276 | snd->sb_mbmax += unp->unp_mbcnt - rcv->sb_mbcnt; | |
277 | unp->unp_mbcnt = rcv->sb_mbcnt; | |
278 | snd->sb_hiwat += unp->unp_cc - rcv->sb_cc; | |
279 | unp->unp_cc = rcv->sb_cc; | |
280 | sowwakeup(so2); | |
281 | #undef snd | |
282 | #undef rcv | |
283 | break; | |
284 | ||
285 | default: | |
286 | panic("uipc_rcvd unknown socktype"); | |
287 | } | |
288 | return 0; | |
289 | } | |
290 | ||
291 | /* pru_rcvoob is EOPNOTSUPP */ | |
292 | ||
293 | static int | |
294 | uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, | |
295 | struct mbuf *control, struct proc *p) | |
296 | { | |
297 | int error = 0; | |
298 | struct unpcb *unp = sotounpcb(so); | |
299 | struct socket *so2; | |
300 | ||
301 | if (unp == 0) { | |
302 | error = EINVAL; | |
303 | goto release; | |
304 | } | |
305 | if (flags & PRUS_OOB) { | |
306 | error = EOPNOTSUPP; | |
307 | goto release; | |
308 | } | |
309 | ||
13fec989 A |
310 | if (control) { |
311 | socket_unlock(so, 0); /* release global lock to avoid deadlock (4436174) */ | |
312 | error = unp_internalize(control, p); | |
313 | socket_lock(so, 0); | |
314 | if (error) | |
315 | goto release; | |
316 | } | |
1c79356b A |
317 | |
318 | switch (so->so_type) { | |
319 | case SOCK_DGRAM: | |
320 | { | |
321 | struct sockaddr *from; | |
322 | ||
323 | if (nam) { | |
324 | if (unp->unp_conn) { | |
325 | error = EISCONN; | |
326 | break; | |
327 | } | |
328 | error = unp_connect(so, nam, p); | |
329 | if (error) | |
330 | break; | |
331 | } else { | |
332 | if (unp->unp_conn == 0) { | |
333 | error = ENOTCONN; | |
334 | break; | |
335 | } | |
336 | } | |
337 | so2 = unp->unp_conn->unp_socket; | |
338 | if (unp->unp_addr) | |
339 | from = (struct sockaddr *)unp->unp_addr; | |
340 | else | |
341 | from = &sun_noname; | |
91447636 | 342 | if (sbappendaddr(&so2->so_rcv, from, m, control, &error)) { |
1c79356b | 343 | sorwakeup(so2); |
91447636 A |
344 | } |
345 | m = 0; | |
346 | control = 0; | |
1c79356b A |
347 | if (nam) |
348 | unp_disconnect(unp); | |
349 | break; | |
350 | } | |
351 | ||
91447636 A |
352 | case SOCK_STREAM: { |
353 | int didreceive = 0; | |
1c79356b A |
354 | #define rcv (&so2->so_rcv) |
355 | #define snd (&so->so_snd) | |
356 | /* Connect if not connected yet. */ | |
357 | /* | |
358 | * Note: A better implementation would complain | |
359 | * if not equal to the peer's address. | |
360 | */ | |
361 | if ((so->so_state & SS_ISCONNECTED) == 0) { | |
362 | if (nam) { | |
363 | error = unp_connect(so, nam, p); | |
364 | if (error) | |
365 | break; /* XXX */ | |
366 | } else { | |
367 | error = ENOTCONN; | |
368 | break; | |
369 | } | |
370 | } | |
371 | ||
372 | if (so->so_state & SS_CANTSENDMORE) { | |
373 | error = EPIPE; | |
374 | break; | |
375 | } | |
376 | if (unp->unp_conn == 0) | |
377 | panic("uipc_send connected but no connection?"); | |
378 | so2 = unp->unp_conn->unp_socket; | |
379 | /* | |
380 | * Send to paired receive port, and then reduce | |
381 | * send buffer hiwater marks to maintain backpressure. | |
382 | * Wake up readers. | |
383 | */ | |
91447636 A |
384 | if ((control && sbappendcontrol(rcv, m, control, NULL)) || |
385 | sbappend(rcv, m)) { | |
386 | didreceive = 1; | |
387 | } | |
1c79356b A |
388 | snd->sb_mbmax -= |
389 | rcv->sb_mbcnt - unp->unp_conn->unp_mbcnt; | |
390 | unp->unp_conn->unp_mbcnt = rcv->sb_mbcnt; | |
391 | snd->sb_hiwat -= rcv->sb_cc - unp->unp_conn->unp_cc; | |
392 | unp->unp_conn->unp_cc = rcv->sb_cc; | |
91447636 A |
393 | if (didreceive) |
394 | sorwakeup(so2); | |
1c79356b | 395 | m = 0; |
91447636 | 396 | control = 0; |
1c79356b A |
397 | #undef snd |
398 | #undef rcv | |
91447636 | 399 | } |
1c79356b A |
400 | break; |
401 | ||
402 | default: | |
403 | panic("uipc_send unknown socktype"); | |
404 | } | |
405 | ||
406 | /* | |
407 | * SEND_EOF is equivalent to a SEND followed by | |
408 | * a SHUTDOWN. | |
409 | */ | |
410 | if (flags & PRUS_EOF) { | |
411 | socantsendmore(so); | |
412 | unp_shutdown(unp); | |
413 | } | |
414 | ||
91447636 A |
415 | if (control && error != 0) |
416 | unp_dispose(control); | |
417 | ||
1c79356b A |
418 | release: |
419 | if (control) | |
420 | m_freem(control); | |
421 | if (m) | |
422 | m_freem(m); | |
423 | return error; | |
424 | } | |
425 | ||
426 | static int | |
427 | uipc_sense(struct socket *so, struct stat *sb) | |
428 | { | |
429 | struct unpcb *unp = sotounpcb(so); | |
430 | struct socket *so2; | |
431 | ||
432 | if (unp == 0) | |
433 | return EINVAL; | |
434 | sb->st_blksize = so->so_snd.sb_hiwat; | |
435 | if (so->so_type == SOCK_STREAM && unp->unp_conn != 0) { | |
436 | so2 = unp->unp_conn->unp_socket; | |
437 | sb->st_blksize += so2->so_rcv.sb_cc; | |
438 | } | |
439 | sb->st_dev = NODEV; | |
440 | if (unp->unp_ino == 0) | |
441 | unp->unp_ino = unp_ino++; | |
442 | sb->st_ino = unp->unp_ino; | |
443 | return (0); | |
444 | } | |
445 | ||
446 | static int | |
447 | uipc_shutdown(struct socket *so) | |
448 | { | |
449 | struct unpcb *unp = sotounpcb(so); | |
450 | ||
451 | if (unp == 0) | |
452 | return EINVAL; | |
453 | socantsendmore(so); | |
454 | unp_shutdown(unp); | |
455 | return 0; | |
456 | } | |
457 | ||
458 | static int | |
459 | uipc_sockaddr(struct socket *so, struct sockaddr **nam) | |
460 | { | |
461 | struct unpcb *unp = sotounpcb(so); | |
462 | ||
463 | if (unp == 0) | |
464 | return EINVAL; | |
465 | if (unp->unp_addr) | |
466 | *nam = dup_sockaddr((struct sockaddr *)unp->unp_addr, 1); | |
467 | return 0; | |
468 | } | |
469 | ||
470 | struct pr_usrreqs uipc_usrreqs = { | |
471 | uipc_abort, uipc_accept, uipc_attach, uipc_bind, uipc_connect, | |
472 | uipc_connect2, pru_control_notsupp, uipc_detach, uipc_disconnect, | |
473 | uipc_listen, uipc_peeraddr, uipc_rcvd, pru_rcvoob_notsupp, | |
474 | uipc_send, uipc_sense, uipc_shutdown, uipc_sockaddr, | |
91447636 | 475 | sosend, soreceive, pru_sopoll_notsupp |
1c79356b | 476 | }; |
91447636 A |
477 | |
478 | int | |
479 | uipc_ctloutput( | |
480 | struct socket *so, | |
481 | struct sockopt *sopt) | |
482 | { | |
483 | struct unpcb *unp = sotounpcb(so); | |
484 | int error; | |
485 | ||
486 | switch (sopt->sopt_dir) { | |
487 | case SOPT_GET: | |
488 | switch (sopt->sopt_name) { | |
489 | case LOCAL_PEERCRED: | |
490 | if (unp->unp_flags & UNP_HAVEPC) | |
491 | error = sooptcopyout(sopt, &unp->unp_peercred, | |
492 | sizeof(unp->unp_peercred)); | |
493 | else { | |
494 | if (so->so_type == SOCK_STREAM) | |
495 | error = ENOTCONN; | |
496 | else | |
497 | error = EINVAL; | |
498 | } | |
499 | break; | |
500 | default: | |
501 | error = EOPNOTSUPP; | |
502 | break; | |
503 | } | |
504 | break; | |
505 | case SOPT_SET: | |
506 | default: | |
507 | error = EOPNOTSUPP; | |
508 | break; | |
509 | } | |
510 | return (error); | |
511 | } | |
1c79356b A |
512 | |
513 | /* | |
514 | * Both send and receive buffers are allocated PIPSIZ bytes of buffering | |
515 | * for stream sockets, although the total for sender and receiver is | |
516 | * actually only PIPSIZ. | |
517 | * Datagram sockets really use the sendspace as the maximum datagram size, | |
518 | * and don't really want to reserve the sendspace. Their recvspace should | |
519 | * be large enough for at least one max-size datagram plus address. | |
520 | */ | |
521 | #ifndef PIPSIZ | |
522 | #define PIPSIZ 8192 | |
523 | #endif | |
524 | static u_long unpst_sendspace = PIPSIZ; | |
525 | static u_long unpst_recvspace = PIPSIZ; | |
526 | static u_long unpdg_sendspace = 2*1024; /* really max datagram size */ | |
527 | static u_long unpdg_recvspace = 4*1024; | |
528 | ||
529 | static int unp_rights; /* file descriptors in flight */ | |
530 | ||
531 | SYSCTL_DECL(_net_local_stream); | |
532 | SYSCTL_INT(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW, | |
533 | &unpst_sendspace, 0, ""); | |
534 | SYSCTL_INT(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW, | |
535 | &unpst_recvspace, 0, ""); | |
536 | SYSCTL_DECL(_net_local_dgram); | |
537 | SYSCTL_INT(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW, | |
538 | &unpdg_sendspace, 0, ""); | |
539 | SYSCTL_INT(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW, | |
540 | &unpdg_recvspace, 0, ""); | |
541 | SYSCTL_DECL(_net_local); | |
542 | SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0, ""); | |
543 | ||
544 | static int | |
91447636 | 545 | unp_attach(struct socket *so) |
1c79356b | 546 | { |
91447636 A |
547 | struct unpcb *unp; |
548 | int error = 0; | |
1c79356b A |
549 | |
550 | if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { | |
551 | switch (so->so_type) { | |
552 | ||
553 | case SOCK_STREAM: | |
554 | error = soreserve(so, unpst_sendspace, unpst_recvspace); | |
555 | break; | |
556 | ||
557 | case SOCK_DGRAM: | |
558 | error = soreserve(so, unpdg_sendspace, unpdg_recvspace); | |
559 | break; | |
560 | ||
561 | default: | |
562 | panic("unp_attach"); | |
563 | } | |
564 | if (error) | |
565 | return (error); | |
566 | } | |
9bccf70c | 567 | unp = (struct unpcb*)zalloc(unp_zone); |
1c79356b A |
568 | if (unp == NULL) |
569 | return (ENOBUFS); | |
570 | bzero(unp, sizeof *unp); | |
37839358 | 571 | lck_rw_lock_exclusive(unp_list_mtx); |
1c79356b A |
572 | LIST_INIT(&unp->unp_refs); |
573 | unp->unp_socket = so; | |
91447636 A |
574 | unp->unp_gencnt = ++unp_gencnt; |
575 | unp_count++; | |
1c79356b A |
576 | LIST_INSERT_HEAD(so->so_type == SOCK_DGRAM ? &unp_dhead |
577 | : &unp_shead, unp, unp_link); | |
578 | so->so_pcb = (caddr_t)unp; | |
37839358 | 579 | lck_rw_done(unp_list_mtx); |
1c79356b A |
580 | return (0); |
581 | } | |
582 | ||
583 | static void | |
91447636 | 584 | unp_detach(struct unpcb *unp) |
1c79356b | 585 | { |
37839358 | 586 | lck_rw_lock_exclusive(unp_list_mtx); |
1c79356b A |
587 | LIST_REMOVE(unp, unp_link); |
588 | unp->unp_gencnt = ++unp_gencnt; | |
37839358 | 589 | lck_rw_done(unp_list_mtx); |
1c79356b A |
590 | --unp_count; |
591 | if (unp->unp_vnode) { | |
fa4905b1 | 592 | struct vnode *tvp = unp->unp_vnode; |
1c79356b | 593 | unp->unp_vnode->v_socket = 0; |
fa4905b1 | 594 | unp->unp_vnode = 0; |
91447636 | 595 | vnode_rele(tvp); /* drop the usecount */ |
1c79356b A |
596 | } |
597 | if (unp->unp_conn) | |
598 | unp_disconnect(unp); | |
599 | while (unp->unp_refs.lh_first) | |
600 | unp_drop(unp->unp_refs.lh_first, ECONNRESET); | |
601 | soisdisconnected(unp->unp_socket); | |
91447636 | 602 | unp->unp_socket->so_flags |= SOF_PCBCLEARING; /* makes sure we're getting dealloced */ |
1c79356b A |
603 | unp->unp_socket->so_pcb = 0; |
604 | if (unp_rights) { | |
605 | /* | |
606 | * Normally the receive buffer is flushed later, | |
607 | * in sofree, but if our receive buffer holds references | |
608 | * to descriptors that are now garbage, we will dispose | |
609 | * of those descriptor references after the garbage collector | |
610 | * gets them (resulting in a "panic: closef: count < 0"). | |
611 | */ | |
612 | sorflush(unp->unp_socket); | |
613 | unp_gc(); | |
614 | } | |
615 | if (unp->unp_addr) | |
616 | FREE(unp->unp_addr, M_SONAME); | |
91447636 | 617 | zfree(unp_zone, unp); |
1c79356b A |
618 | } |
619 | ||
620 | static int | |
91447636 A |
621 | unp_bind( |
622 | struct unpcb *unp, | |
623 | struct sockaddr *nam, | |
624 | struct proc *p) | |
1c79356b A |
625 | { |
626 | struct sockaddr_un *soun = (struct sockaddr_un *)nam; | |
91447636 A |
627 | struct vnode *vp, *dvp; |
628 | struct vnode_attr va; | |
629 | struct vfs_context context; | |
1c79356b A |
630 | int error, namelen; |
631 | struct nameidata nd; | |
632 | char buf[SOCK_MAXADDRLEN]; | |
633 | ||
91447636 A |
634 | context.vc_proc = p; |
635 | context.vc_ucred = p->p_ucred; /* XXX kauth_cred_get() ??? proxy */ | |
636 | ||
1c79356b A |
637 | if (unp->unp_vnode != NULL) |
638 | return (EINVAL); | |
1c79356b A |
639 | namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path); |
640 | if (namelen <= 0) | |
641 | return EINVAL; | |
642 | strncpy(buf, soun->sun_path, namelen); | |
643 | buf[namelen] = 0; /* null-terminate the string */ | |
91447636 A |
644 | NDINIT(&nd, CREATE, FOLLOW | LOCKPARENT, UIO_SYSSPACE32, |
645 | CAST_USER_ADDR_T(buf), &context); | |
1c79356b A |
646 | /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */ |
647 | error = namei(&nd); | |
648 | if (error) { | |
1c79356b A |
649 | return (error); |
650 | } | |
91447636 | 651 | dvp = nd.ni_dvp; |
1c79356b | 652 | vp = nd.ni_vp; |
91447636 | 653 | |
1c79356b | 654 | if (vp != NULL) { |
91447636 A |
655 | /* |
656 | * need to do this before the vnode_put of dvp | |
657 | * since we may have to release an fs_nodelock | |
658 | */ | |
659 | nameidone(&nd); | |
660 | ||
661 | vnode_put(dvp); | |
662 | vnode_put(vp); | |
663 | ||
1c79356b A |
664 | return (EADDRINUSE); |
665 | } | |
91447636 A |
666 | |
667 | /* authorize before creating */ | |
668 | error = vnode_authorize(dvp, NULL, KAUTH_VNODE_ADD_FILE, &context); | |
669 | ||
670 | if (!error) { | |
671 | VATTR_INIT(&va); | |
672 | VATTR_SET(&va, va_type, VSOCK); | |
673 | VATTR_SET(&va, va_mode, (ACCESSPERMS & ~p->p_fd->fd_cmask)); | |
674 | ||
675 | /* create the socket */ | |
676 | error = vn_create(dvp, &vp, &nd.ni_cnd, &va, 0, &context); | |
677 | } | |
678 | ||
679 | nameidone(&nd); | |
680 | vnode_put(dvp); | |
681 | ||
1c79356b | 682 | if (error) { |
1c79356b A |
683 | return (error); |
684 | } | |
91447636 | 685 | vnode_ref(vp); /* gain a longterm reference */ |
1c79356b A |
686 | vp->v_socket = unp->unp_socket; |
687 | unp->unp_vnode = vp; | |
688 | unp->unp_addr = (struct sockaddr_un *)dup_sockaddr(nam, 1); | |
91447636 A |
689 | vnode_put(vp); /* drop the iocount */ |
690 | ||
1c79356b A |
691 | return (0); |
692 | } | |
693 | ||
694 | static int | |
91447636 A |
695 | unp_connect( |
696 | struct socket *so, | |
697 | struct sockaddr *nam, | |
698 | struct proc *p) | |
1c79356b | 699 | { |
91447636 A |
700 | struct sockaddr_un *soun = (struct sockaddr_un *)nam; |
701 | struct vnode *vp; | |
702 | struct socket *so2, *so3; | |
703 | struct unpcb *unp, *unp2, *unp3; | |
704 | struct vfs_context context; | |
1c79356b A |
705 | int error, len; |
706 | struct nameidata nd; | |
707 | char buf[SOCK_MAXADDRLEN]; | |
708 | ||
91447636 A |
709 | context.vc_proc = p; |
710 | context.vc_ucred = p->p_ucred; /* XXX kauth_cred_get() ??? proxy */ | |
cc9f6e38 | 711 | so2 = so3 = NULL; |
91447636 | 712 | |
1c79356b A |
713 | len = nam->sa_len - offsetof(struct sockaddr_un, sun_path); |
714 | if (len <= 0) | |
715 | return EINVAL; | |
716 | strncpy(buf, soun->sun_path, len); | |
717 | buf[len] = 0; | |
718 | ||
91447636 | 719 | NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE32, CAST_USER_ADDR_T(buf), &context); |
1c79356b A |
720 | error = namei(&nd); |
721 | if (error) { | |
1c79356b A |
722 | return (error); |
723 | } | |
91447636 | 724 | nameidone(&nd); |
1c79356b A |
725 | vp = nd.ni_vp; |
726 | if (vp->v_type != VSOCK) { | |
727 | error = ENOTSOCK; | |
728 | goto bad; | |
729 | } | |
91447636 A |
730 | |
731 | error = vnode_authorize(vp, NULL, KAUTH_VNODE_WRITE_DATA, &context); | |
1c79356b A |
732 | if (error) |
733 | goto bad; | |
734 | so2 = vp->v_socket; | |
37839358 | 735 | if (so2 == 0 || so2->so_pcb == NULL ) { |
1c79356b A |
736 | error = ECONNREFUSED; |
737 | goto bad; | |
738 | } | |
91447636 A |
739 | |
740 | /* make sure the socket can't go away while we're connecting */ | |
741 | so2->so_usecount++; | |
742 | ||
1c79356b A |
743 | if (so->so_type != so2->so_type) { |
744 | error = EPROTOTYPE; | |
745 | goto bad; | |
746 | } | |
55e303ae A |
747 | |
748 | /* | |
749 | * Check if socket was connected while we were trying to | |
750 | * acquire the funnel. | |
751 | * XXX - probably shouldn't return an error for SOCK_DGRAM | |
752 | */ | |
753 | if ((so->so_state & SS_ISCONNECTED) != 0) { | |
754 | error = EISCONN; | |
55e303ae A |
755 | goto bad; |
756 | } | |
757 | ||
1c79356b A |
758 | if (so->so_proto->pr_flags & PR_CONNREQUIRED) { |
759 | if ((so2->so_options & SO_ACCEPTCONN) == 0 || | |
91447636 | 760 | (so3 = sonewconn(so2, 0, nam)) == 0) { |
1c79356b | 761 | error = ECONNREFUSED; |
1c79356b A |
762 | goto bad; |
763 | } | |
91447636 | 764 | unp = sotounpcb(so); |
1c79356b A |
765 | unp2 = sotounpcb(so2); |
766 | unp3 = sotounpcb(so3); | |
767 | if (unp2->unp_addr) | |
768 | unp3->unp_addr = (struct sockaddr_un *) | |
769 | dup_sockaddr((struct sockaddr *) | |
770 | unp2->unp_addr, 1); | |
91447636 A |
771 | |
772 | /* | |
773 | * unp_peercred management: | |
774 | * | |
775 | * The connecter's (client's) credentials are copied | |
776 | * from its process structure at the time of connect() | |
777 | * (which is now). | |
778 | */ | |
779 | cru2x(p->p_ucred, &unp3->unp_peercred); | |
780 | unp3->unp_flags |= UNP_HAVEPC; | |
781 | /* | |
782 | * The receiver's (server's) credentials are copied | |
783 | * from the unp_peercred member of socket on which the | |
784 | * former called listen(); unp_listen() cached that | |
785 | * process's credentials at that time so we can use | |
786 | * them now. | |
787 | */ | |
788 | KASSERT(unp2->unp_flags & UNP_HAVEPCCACHED, | |
789 | ("unp_connect: listener without cached peercred")); | |
790 | memcpy(&unp->unp_peercred, &unp2->unp_peercred, | |
791 | sizeof(unp->unp_peercred)); | |
792 | unp->unp_flags |= UNP_HAVEPC; | |
793 | ||
794 | so2->so_usecount--; /* drop reference taken on so2 */ | |
1c79356b | 795 | so2 = so3; |
91447636 | 796 | so3->so_usecount++; /* make sure we keep it around */ |
1c79356b A |
797 | } |
798 | error = unp_connect2(so, so2); | |
1c79356b | 799 | bad: |
37839358 A |
800 | |
801 | if (so2 != NULL) | |
802 | so2->so_usecount--; /* release count on socket */ | |
803 | ||
91447636 | 804 | vnode_put(vp); |
1c79356b A |
805 | return (error); |
806 | } | |
807 | ||
808 | int | |
91447636 A |
809 | unp_connect2( |
810 | struct socket *so, | |
811 | struct socket *so2) | |
1c79356b | 812 | { |
91447636 A |
813 | struct unpcb *unp = sotounpcb(so); |
814 | struct unpcb *unp2; | |
1c79356b A |
815 | |
816 | if (so2->so_type != so->so_type) | |
817 | return (EPROTOTYPE); | |
818 | unp2 = sotounpcb(so2); | |
0b4e3aa0 A |
819 | |
820 | /* Verify both sockets are still opened */ | |
821 | if (unp == 0 || unp2 == 0) | |
822 | return (EINVAL); | |
823 | ||
1c79356b A |
824 | unp->unp_conn = unp2; |
825 | switch (so->so_type) { | |
826 | ||
827 | case SOCK_DGRAM: | |
828 | LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink); | |
829 | soisconnected(so); | |
830 | break; | |
831 | ||
832 | case SOCK_STREAM: | |
91447636 A |
833 | /* This takes care of socketpair */ |
834 | if (!(unp->unp_flags & UNP_HAVEPC) && !(unp2->unp_flags & UNP_HAVEPC)) { | |
835 | cru2x(kauth_cred_get(), &unp->unp_peercred); | |
836 | unp->unp_flags |= UNP_HAVEPC; | |
837 | ||
838 | cru2x(kauth_cred_get(), &unp2->unp_peercred); | |
839 | unp2->unp_flags |= UNP_HAVEPC; | |
840 | } | |
1c79356b A |
841 | unp2->unp_conn = unp; |
842 | soisconnected(so); | |
843 | soisconnected(so2); | |
844 | break; | |
845 | ||
846 | default: | |
847 | panic("unp_connect2"); | |
848 | } | |
849 | return (0); | |
850 | } | |
851 | ||
852 | static void | |
91447636 | 853 | unp_disconnect(struct unpcb *unp) |
1c79356b | 854 | { |
91447636 | 855 | struct unpcb *unp2 = unp->unp_conn; |
1c79356b A |
856 | |
857 | if (unp2 == 0) | |
858 | return; | |
859 | unp->unp_conn = 0; | |
860 | switch (unp->unp_socket->so_type) { | |
861 | ||
862 | case SOCK_DGRAM: | |
37839358 | 863 | lck_rw_lock_exclusive(unp_list_mtx); |
1c79356b | 864 | LIST_REMOVE(unp, unp_reflink); |
37839358 | 865 | lck_rw_done(unp_list_mtx); |
1c79356b A |
866 | unp->unp_socket->so_state &= ~SS_ISCONNECTED; |
867 | break; | |
868 | ||
869 | case SOCK_STREAM: | |
870 | soisdisconnected(unp->unp_socket); | |
871 | unp2->unp_conn = 0; | |
872 | soisdisconnected(unp2->unp_socket); | |
873 | break; | |
874 | } | |
875 | } | |
876 | ||
877 | #ifdef notdef | |
878 | void | |
91447636 | 879 | unp_abort(struct unpcb *unp) |
1c79356b A |
880 | { |
881 | ||
882 | unp_detach(unp); | |
883 | } | |
884 | #endif | |
885 | ||
886 | static int | |
887 | unp_pcblist SYSCTL_HANDLER_ARGS | |
888 | { | |
889 | int error, i, n; | |
890 | struct unpcb *unp, **unp_list; | |
891 | unp_gen_t gencnt; | |
892 | struct xunpgen xug; | |
893 | struct unp_head *head; | |
894 | ||
37839358 | 895 | lck_rw_lock_shared(unp_list_mtx); |
1c79356b A |
896 | head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead); |
897 | ||
898 | /* | |
899 | * The process of preparing the PCB list is too time-consuming and | |
900 | * resource-intensive to repeat twice on every request. | |
901 | */ | |
91447636 | 902 | if (req->oldptr == USER_ADDR_NULL) { |
1c79356b A |
903 | n = unp_count; |
904 | req->oldidx = 2 * (sizeof xug) | |
905 | + (n + n/8) * sizeof(struct xunpcb); | |
37839358 | 906 | lck_rw_done(unp_list_mtx); |
1c79356b A |
907 | return 0; |
908 | } | |
909 | ||
91447636 | 910 | if (req->newptr != USER_ADDR_NULL) { |
37839358 | 911 | lck_rw_done(unp_list_mtx); |
1c79356b | 912 | return EPERM; |
91447636 | 913 | } |
1c79356b A |
914 | |
915 | /* | |
916 | * OK, now we're committed to doing something. | |
917 | */ | |
918 | gencnt = unp_gencnt; | |
919 | n = unp_count; | |
920 | ||
3a60a9f5 | 921 | bzero(&xug, sizeof(xug)); |
1c79356b A |
922 | xug.xug_len = sizeof xug; |
923 | xug.xug_count = n; | |
924 | xug.xug_gen = gencnt; | |
925 | xug.xug_sogen = so_gencnt; | |
926 | error = SYSCTL_OUT(req, &xug, sizeof xug); | |
91447636 | 927 | if (error) { |
37839358 | 928 | lck_rw_done(unp_list_mtx); |
1c79356b | 929 | return error; |
91447636 | 930 | } |
1c79356b | 931 | |
0b4e3aa0 A |
932 | /* |
933 | * We are done if there is no pcb | |
934 | */ | |
91447636 | 935 | if (n == 0) { |
37839358 | 936 | lck_rw_done(unp_list_mtx); |
0b4e3aa0 | 937 | return 0; |
91447636 | 938 | } |
0b4e3aa0 | 939 | |
91447636 A |
940 | MALLOC(unp_list, struct unpcb **, n * sizeof *unp_list, M_TEMP, M_WAITOK); |
941 | if (unp_list == 0) { | |
37839358 | 942 | lck_rw_done(unp_list_mtx); |
1c79356b | 943 | return ENOMEM; |
91447636 | 944 | } |
1c79356b A |
945 | |
946 | for (unp = head->lh_first, i = 0; unp && i < n; | |
947 | unp = unp->unp_link.le_next) { | |
948 | if (unp->unp_gencnt <= gencnt) | |
949 | unp_list[i++] = unp; | |
950 | } | |
951 | n = i; /* in case we lost some during malloc */ | |
952 | ||
953 | error = 0; | |
954 | for (i = 0; i < n; i++) { | |
955 | unp = unp_list[i]; | |
956 | if (unp->unp_gencnt <= gencnt) { | |
957 | struct xunpcb xu; | |
3a60a9f5 A |
958 | |
959 | bzero(&xu, sizeof(xu)); | |
1c79356b | 960 | xu.xu_len = sizeof xu; |
91447636 | 961 | xu.xu_unpp = (struct unpcb_compat *)unp; |
1c79356b A |
962 | /* |
963 | * XXX - need more locking here to protect against | |
964 | * connect/disconnect races for SMP. | |
965 | */ | |
966 | if (unp->unp_addr) | |
967 | bcopy(unp->unp_addr, &xu.xu_addr, | |
968 | unp->unp_addr->sun_len); | |
969 | if (unp->unp_conn && unp->unp_conn->unp_addr) | |
970 | bcopy(unp->unp_conn->unp_addr, | |
971 | &xu.xu_caddr, | |
972 | unp->unp_conn->unp_addr->sun_len); | |
91447636 | 973 | bcopy(unp, &xu.xu_unp, sizeof(xu.xu_unp)); |
1c79356b A |
974 | sotoxsocket(unp->unp_socket, &xu.xu_socket); |
975 | error = SYSCTL_OUT(req, &xu, sizeof xu); | |
976 | } | |
977 | } | |
978 | if (!error) { | |
979 | /* | |
980 | * Give the user an updated idea of our state. | |
981 | * If the generation differs from what we told | |
982 | * her before, she knows that something happened | |
983 | * while we were processing this request, and it | |
984 | * might be necessary to retry. | |
985 | */ | |
3a60a9f5 A |
986 | bzero(&xug, sizeof(xug)); |
987 | xug.xug_len = sizeof xug; | |
1c79356b A |
988 | xug.xug_gen = unp_gencnt; |
989 | xug.xug_sogen = so_gencnt; | |
990 | xug.xug_count = unp_count; | |
991 | error = SYSCTL_OUT(req, &xug, sizeof xug); | |
992 | } | |
993 | FREE(unp_list, M_TEMP); | |
37839358 | 994 | lck_rw_done(unp_list_mtx); |
1c79356b A |
995 | return error; |
996 | } | |
997 | ||
998 | SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLFLAG_RD, | |
999 | (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb", | |
1000 | "List of active local datagram sockets"); | |
1001 | SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLFLAG_RD, | |
1002 | (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb", | |
1003 | "List of active local stream sockets"); | |
1004 | ||
1005 | static void | |
91447636 | 1006 | unp_shutdown(struct unpcb *unp) |
1c79356b A |
1007 | { |
1008 | struct socket *so; | |
1009 | ||
1010 | if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn && | |
1011 | (so = unp->unp_conn->unp_socket)) | |
1012 | socantrcvmore(so); | |
1013 | } | |
1014 | ||
1015 | static void | |
91447636 A |
1016 | unp_drop( |
1017 | struct unpcb *unp, | |
1018 | int errno) | |
1c79356b A |
1019 | { |
1020 | struct socket *so = unp->unp_socket; | |
1021 | ||
1022 | so->so_error = errno; | |
1023 | unp_disconnect(unp); | |
1c79356b A |
1024 | } |
1025 | ||
1026 | #ifdef notdef | |
1027 | void | |
1028 | unp_drain() | |
1029 | { | |
1030 | ||
1031 | } | |
1032 | #endif | |
1033 | ||
1034 | int | |
91447636 | 1035 | unp_externalize(struct mbuf *rights) |
1c79356b A |
1036 | { |
1037 | struct proc *p = current_proc(); /* XXX */ | |
91447636 A |
1038 | int i; |
1039 | struct cmsghdr *cm = mtod(rights, struct cmsghdr *); | |
1040 | struct fileglob **rp = (struct fileglob **)(cm + 1); | |
1041 | struct fileproc *fp; | |
1042 | struct fileglob *fg; | |
1c79356b A |
1043 | int newfds = (cm->cmsg_len - sizeof(*cm)) / sizeof (int); |
1044 | int f; | |
1045 | ||
91447636 | 1046 | proc_fdlock(p); |
1c79356b A |
1047 | |
1048 | /* | |
1049 | * if the new FD's will not fit, then we free them all | |
1050 | */ | |
1051 | if (!fdavail(p, newfds)) { | |
1052 | for (i = 0; i < newfds; i++) { | |
91447636 A |
1053 | fg = *rp; |
1054 | unp_discard_fdlocked(fg, p); | |
1c79356b A |
1055 | *rp++ = 0; |
1056 | } | |
91447636 | 1057 | proc_fdunlock(p); |
1c79356b | 1058 | |
1c79356b A |
1059 | return (EMSGSIZE); |
1060 | } | |
1061 | /* | |
1062 | * now change each pointer to an fd in the global table to | |
1063 | * an integer that is the index to the local fd table entry | |
1064 | * that we set up to point to the global one we are transferring. | |
1065 | * XXX this assumes a pointer and int are the same size...! | |
1066 | */ | |
1067 | for (i = 0; i < newfds; i++) { | |
1068 | if (fdalloc(p, 0, &f)) | |
1069 | panic("unp_externalize"); | |
91447636 A |
1070 | fg = *rp; |
1071 | MALLOC_ZONE(fp, struct fileproc *, sizeof(struct fileproc), M_FILEPROC, M_WAITOK); | |
1072 | bzero(fp, sizeof(struct fileproc)); | |
1073 | fp->f_iocount = 0; | |
1074 | fp->f_fglob = fg; | |
1c79356b | 1075 | p->p_fd->fd_ofiles[f] = fp; |
91447636 | 1076 | fg_removeuipc(fg); |
0b4e3aa0 | 1077 | *fdflags(p, f) &= ~UF_RESERVED; |
1c79356b A |
1078 | unp_rights--; |
1079 | *(int *)rp++ = f; | |
1080 | } | |
91447636 | 1081 | proc_fdunlock(p); |
1c79356b | 1082 | |
1c79356b A |
1083 | return (0); |
1084 | } | |
1085 | ||
1086 | void | |
1087 | unp_init(void) | |
1088 | { | |
1089 | unp_zone = zinit(sizeof(struct unpcb), | |
1090 | (nmbclusters * sizeof(struct unpcb)), | |
1091 | 4096, "unpzone"); | |
1092 | if (unp_zone == 0) | |
1093 | panic("unp_init"); | |
1094 | LIST_INIT(&unp_dhead); | |
1095 | LIST_INIT(&unp_shead); | |
91447636 | 1096 | |
37839358 A |
1097 | /* |
1098 | * allocate lock group attribute and group for udp pcb mutexes | |
1099 | */ | |
1100 | unp_mtx_grp_attr = lck_grp_attr_alloc_init(); | |
1101 | ||
1102 | unp_mtx_grp = lck_grp_alloc_init("unp_list", unp_mtx_grp_attr); | |
1103 | ||
1104 | unp_mtx_attr = lck_attr_alloc_init(); | |
1105 | ||
1106 | if ((unp_list_mtx = lck_rw_alloc_init(unp_mtx_grp, unp_mtx_attr)) == NULL) | |
1107 | return; /* pretty much dead if this fails... */ | |
1108 | ||
1c79356b A |
1109 | } |
1110 | ||
1111 | #ifndef MIN | |
1112 | #define MIN(a,b) (((a)<(b))?(a):(b)) | |
1113 | #endif | |
1114 | ||
1115 | static int | |
91447636 A |
1116 | unp_internalize( |
1117 | struct mbuf *control, | |
1118 | struct proc *p) | |
1c79356b | 1119 | { |
91447636 A |
1120 | struct cmsghdr *cm = mtod(control, struct cmsghdr *); |
1121 | struct fileglob **rp; | |
1122 | struct fileproc *fp; | |
1c79356b A |
1123 | register int i, error; |
1124 | int oldfds; | |
91447636 | 1125 | int fdgetf_noref(proc_t, struct fileglob **, struct fileproc **); |
1c79356b | 1126 | |
1c79356b A |
1127 | if (cm->cmsg_type != SCM_RIGHTS || cm->cmsg_level != SOL_SOCKET || |
1128 | cm->cmsg_len != control->m_len) { | |
1c79356b A |
1129 | return (EINVAL); |
1130 | } | |
1c79356b | 1131 | oldfds = (cm->cmsg_len - sizeof (*cm)) / sizeof (int); |
1c79356b | 1132 | |
91447636 A |
1133 | proc_fdlock(p); |
1134 | rp = (struct fileglob **)(cm + 1); | |
1135 | ||
1136 | for (i = 0; i < oldfds; i++) { | |
1137 | if (error = fdgetf_noref(p, *(int *)rp++, (struct fileglob **)0)) { | |
1138 | proc_fdunlock(p); | |
1139 | return (error); | |
1c79356b | 1140 | } |
91447636 A |
1141 | } |
1142 | rp = (struct fileglob **)(cm + 1); | |
1c79356b | 1143 | |
1c79356b | 1144 | for (i = 0; i < oldfds; i++) { |
91447636 A |
1145 | (void) fdgetf_noref(p, *(int *)rp, &fp); |
1146 | fg_insertuipc(fp->f_fglob); | |
1147 | *rp++ = fp->f_fglob; | |
1c79356b A |
1148 | unp_rights++; |
1149 | } | |
91447636 | 1150 | proc_fdunlock(p); |
1c79356b | 1151 | |
1c79356b A |
1152 | return (0); |
1153 | } | |
1154 | ||
1155 | static int unp_defer, unp_gcing; | |
1156 | ||
1157 | static void | |
1158 | unp_gc() | |
1159 | { | |
91447636 | 1160 | register struct fileglob *fg, *nextfg; |
1c79356b | 1161 | register struct socket *so; |
91447636 | 1162 | struct fileglob **extra_ref, **fpp; |
1c79356b A |
1163 | int nunref, i; |
1164 | ||
91447636 A |
1165 | lck_mtx_lock(uipc_lock); |
1166 | if (unp_gcing) { | |
1167 | lck_mtx_unlock(uipc_lock); | |
1c79356b | 1168 | return; |
91447636 | 1169 | } |
1c79356b A |
1170 | unp_gcing = 1; |
1171 | unp_defer = 0; | |
91447636 | 1172 | lck_mtx_unlock(uipc_lock); |
1c79356b A |
1173 | /* |
1174 | * before going through all this, set all FDs to | |
1175 | * be NOT defered and NOT externally accessible | |
1176 | */ | |
91447636 A |
1177 | for (fg = fmsghead.lh_first; fg != 0; fg = fg->f_msglist.le_next) { |
1178 | lck_mtx_lock(&fg->fg_lock); | |
1179 | fg->fg_flag &= ~(FMARK|FDEFER); | |
1180 | lck_mtx_unlock(&fg->fg_lock); | |
1181 | } | |
1c79356b | 1182 | do { |
91447636 A |
1183 | for (fg = fmsghead.lh_first; fg != 0; fg = fg->f_msglist.le_next) { |
1184 | lck_mtx_lock(&fg->fg_lock); | |
1c79356b A |
1185 | /* |
1186 | * If the file is not open, skip it | |
1187 | */ | |
91447636 A |
1188 | if (fg->fg_count == 0) { |
1189 | lck_mtx_unlock(&fg->fg_lock); | |
1c79356b | 1190 | continue; |
91447636 | 1191 | } |
1c79356b A |
1192 | /* |
1193 | * If we already marked it as 'defer' in a | |
1194 | * previous pass, then try process it this time | |
1195 | * and un-mark it | |
1196 | */ | |
91447636 A |
1197 | if (fg->fg_flag & FDEFER) { |
1198 | fg->fg_flag &= ~FDEFER; | |
1c79356b A |
1199 | unp_defer--; |
1200 | } else { | |
1201 | /* | |
1202 | * if it's not defered, then check if it's | |
1203 | * already marked.. if so skip it | |
1204 | */ | |
91447636 A |
1205 | if (fg->fg_flag & FMARK){ |
1206 | lck_mtx_unlock(&fg->fg_lock); | |
1c79356b | 1207 | continue; |
91447636 | 1208 | } |
1c79356b A |
1209 | /* |
1210 | * If all references are from messages | |
1211 | * in transit, then skip it. it's not | |
1212 | * externally accessible. | |
1213 | */ | |
91447636 A |
1214 | if (fg->fg_count == fg->fg_msgcount) { |
1215 | lck_mtx_unlock(&fg->fg_lock); | |
1c79356b | 1216 | continue; |
91447636 | 1217 | } |
1c79356b A |
1218 | /* |
1219 | * If it got this far then it must be | |
1220 | * externally accessible. | |
1221 | */ | |
91447636 | 1222 | fg->fg_flag |= FMARK; |
1c79356b A |
1223 | } |
1224 | /* | |
1225 | * either it was defered, or it is externally | |
1226 | * accessible and not already marked so. | |
1227 | * Now check if it is possibly one of OUR sockets. | |
1228 | */ | |
91447636 A |
1229 | if (fg->fg_type != DTYPE_SOCKET || |
1230 | (so = (struct socket *)fg->fg_data) == 0) { | |
1231 | lck_mtx_unlock(&fg->fg_lock); | |
1c79356b | 1232 | continue; |
91447636 | 1233 | } |
1c79356b | 1234 | if (so->so_proto->pr_domain != &localdomain || |
91447636 A |
1235 | (so->so_proto->pr_flags&PR_RIGHTS) == 0) { |
1236 | lck_mtx_unlock(&fg->fg_lock); | |
1c79356b | 1237 | continue; |
91447636 | 1238 | } |
1c79356b A |
1239 | #ifdef notdef |
1240 | /* if this code is enabled need to run under network funnel */ | |
1241 | if (so->so_rcv.sb_flags & SB_LOCK) { | |
1242 | /* | |
1243 | * This is problematical; it's not clear | |
1244 | * we need to wait for the sockbuf to be | |
1245 | * unlocked (on a uniprocessor, at least), | |
1246 | * and it's also not clear what to do | |
1247 | * if sbwait returns an error due to receipt | |
1248 | * of a signal. If sbwait does return | |
1249 | * an error, we'll go into an infinite | |
1250 | * loop. Delete all of this for now. | |
1251 | */ | |
1252 | (void) sbwait(&so->so_rcv); | |
1253 | goto restart; | |
1254 | } | |
1255 | #endif | |
1256 | /* | |
1257 | * So, Ok, it's one of our sockets and it IS externally | |
1258 | * accessible (or was defered). Now we look | |
1259 | * to see if we hold any file descriptors in its | |
1260 | * message buffers. Follow those links and mark them | |
1261 | * as accessible too. | |
1262 | */ | |
1263 | unp_scan(so->so_rcv.sb_mb, unp_mark); | |
91447636 | 1264 | lck_mtx_unlock(&fg->fg_lock); |
1c79356b A |
1265 | } |
1266 | } while (unp_defer); | |
1267 | /* | |
1268 | * We grab an extra reference to each of the file table entries | |
1269 | * that are not otherwise accessible and then free the rights | |
1270 | * that are stored in messages on them. | |
1271 | * | |
1272 | * The bug in the orginal code is a little tricky, so I'll describe | |
1273 | * what's wrong with it here. | |
1274 | * | |
1275 | * It is incorrect to simply unp_discard each entry for f_msgcount | |
1276 | * times -- consider the case of sockets A and B that contain | |
1277 | * references to each other. On a last close of some other socket, | |
1278 | * we trigger a gc since the number of outstanding rights (unp_rights) | |
1279 | * is non-zero. If during the sweep phase the gc code un_discards, | |
1280 | * we end up doing a (full) closef on the descriptor. A closef on A | |
1281 | * results in the following chain. Closef calls soo_close, which | |
1282 | * calls soclose. Soclose calls first (through the switch | |
1283 | * uipc_usrreq) unp_detach, which re-invokes unp_gc. Unp_gc simply | |
1284 | * returns because the previous instance had set unp_gcing, and | |
1285 | * we return all the way back to soclose, which marks the socket | |
1286 | * with SS_NOFDREF, and then calls sofree. Sofree calls sorflush | |
1287 | * to free up the rights that are queued in messages on the socket A, | |
1288 | * i.e., the reference on B. The sorflush calls via the dom_dispose | |
1289 | * switch unp_dispose, which unp_scans with unp_discard. This second | |
1290 | * instance of unp_discard just calls closef on B. | |
1291 | * | |
1292 | * Well, a similar chain occurs on B, resulting in a sorflush on B, | |
1293 | * which results in another closef on A. Unfortunately, A is already | |
1294 | * being closed, and the descriptor has already been marked with | |
1295 | * SS_NOFDREF, and soclose panics at this point. | |
1296 | * | |
1297 | * Here, we first take an extra reference to each inaccessible | |
1298 | * descriptor. Then, we call sorflush ourself, since we know | |
1299 | * it is a Unix domain socket anyhow. After we destroy all the | |
1300 | * rights carried in messages, we do a last closef to get rid | |
1301 | * of our extra reference. This is the last close, and the | |
1302 | * unp_detach etc will shut down the socket. | |
1303 | * | |
1304 | * 91/09/19, bsy@cs.cmu.edu | |
1305 | */ | |
91447636 A |
1306 | extra_ref = _MALLOC(nfiles * sizeof(struct fileglob *), M_FILEGLOB, M_WAITOK); |
1307 | for (nunref = 0, fg = fmsghead.lh_first, fpp = extra_ref; fg != 0; | |
1308 | fg = nextfg) { | |
1309 | lck_mtx_lock(&fg->fg_lock); | |
1310 | ||
1311 | nextfg = fg->f_msglist.le_next; | |
1c79356b A |
1312 | /* |
1313 | * If it's not open, skip it | |
1314 | */ | |
91447636 A |
1315 | if (fg->fg_count == 0) { |
1316 | lck_mtx_unlock(&fg->fg_lock); | |
1c79356b | 1317 | continue; |
91447636 | 1318 | } |
1c79356b A |
1319 | /* |
1320 | * If all refs are from msgs, and it's not marked accessible | |
1321 | * then it must be referenced from some unreachable cycle | |
1322 | * of (shut-down) FDs, so include it in our | |
1323 | * list of FDs to remove | |
1324 | */ | |
91447636 A |
1325 | if (fg->fg_count == fg->fg_msgcount && !(fg->fg_flag & FMARK)) { |
1326 | fg->fg_count++; | |
1327 | *fpp++ = fg; | |
1c79356b | 1328 | nunref++; |
1c79356b | 1329 | } |
91447636 | 1330 | lck_mtx_unlock(&fg->fg_lock); |
1c79356b A |
1331 | } |
1332 | /* | |
1333 | * for each FD on our hit list, do the following two things | |
1334 | */ | |
1335 | for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) { | |
91447636 | 1336 | struct fileglob *tfg; |
1c79356b | 1337 | |
91447636 | 1338 | tfg = *fpp; |
1c79356b | 1339 | |
91447636 A |
1340 | if (tfg->fg_type == DTYPE_SOCKET && tfg->fg_data != NULL) { |
1341 | sorflush((struct socket *)(tfg->fg_data)); | |
1342 | } | |
1343 | } | |
1c79356b | 1344 | for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) |
91447636 | 1345 | closef_locked((struct fileproc *)0, *fpp, (struct proc *) NULL); |
1c79356b | 1346 | unp_gcing = 0; |
91447636 A |
1347 | FREE((caddr_t)extra_ref, M_FILEGLOB); |
1348 | ||
1c79356b A |
1349 | } |
1350 | ||
1351 | void | |
91447636 | 1352 | unp_dispose(struct mbuf *m) |
1c79356b A |
1353 | { |
1354 | ||
1355 | if (m) { | |
1c79356b | 1356 | unp_scan(m, unp_discard); |
1c79356b A |
1357 | } |
1358 | } | |
1359 | ||
91447636 A |
1360 | static int |
1361 | unp_listen( | |
1362 | struct unpcb *unp, | |
1363 | struct proc *p) | |
1364 | { | |
1365 | ||
1366 | cru2x(p->p_ucred, &unp->unp_peercred); | |
1367 | unp->unp_flags |= UNP_HAVEPCCACHED; | |
1368 | return (0); | |
1369 | } | |
1370 | ||
1371 | /* should run under kernel funnel */ | |
1c79356b | 1372 | static void |
91447636 A |
1373 | unp_scan( |
1374 | struct mbuf *m0, | |
1375 | void (*op)(struct fileglob *)) | |
1c79356b | 1376 | { |
91447636 A |
1377 | struct mbuf *m; |
1378 | struct fileglob **rp; | |
1379 | struct cmsghdr *cm; | |
1380 | int i; | |
1c79356b A |
1381 | int qfds; |
1382 | ||
1383 | while (m0) { | |
1384 | for (m = m0; m; m = m->m_next) | |
1385 | if (m->m_type == MT_CONTROL && | |
91447636 | 1386 | (size_t) m->m_len >= sizeof(*cm)) { |
1c79356b A |
1387 | cm = mtod(m, struct cmsghdr *); |
1388 | if (cm->cmsg_level != SOL_SOCKET || | |
1389 | cm->cmsg_type != SCM_RIGHTS) | |
1390 | continue; | |
1391 | qfds = (cm->cmsg_len - sizeof *cm) | |
91447636 A |
1392 | / sizeof (struct fileglob *); |
1393 | rp = (struct fileglob **)(cm + 1); | |
1c79356b A |
1394 | for (i = 0; i < qfds; i++) |
1395 | (*op)(*rp++); | |
1396 | break; /* XXX, but saves time */ | |
1397 | } | |
1398 | m0 = m0->m_act; | |
1399 | } | |
1400 | } | |
1401 | ||
1402 | /* should run under kernel funnel */ | |
1403 | static void | |
91447636 | 1404 | unp_mark(struct fileglob *fg) |
1c79356b | 1405 | { |
91447636 | 1406 | lck_mtx_lock(&fg->fg_lock); |
1c79356b | 1407 | |
91447636 A |
1408 | if (fg->fg_flag & FMARK) { |
1409 | lck_mtx_unlock(&fg->fg_lock); | |
1c79356b | 1410 | return; |
91447636 A |
1411 | } |
1412 | fg->fg_flag |= (FMARK|FDEFER); | |
1413 | ||
1414 | lck_mtx_unlock(&fg->fg_lock); | |
1415 | ||
1c79356b | 1416 | unp_defer++; |
1c79356b A |
1417 | } |
1418 | ||
1419 | /* should run under kernel funnel */ | |
1420 | static void | |
91447636 A |
1421 | unp_discard(fg) |
1422 | struct fileglob *fg; | |
1c79356b | 1423 | { |
91447636 A |
1424 | struct proc *p = current_proc(); /* XXX */ |
1425 | ||
1426 | proc_fdlock(p); | |
1427 | unp_discard_fdlocked(fg, p); | |
1428 | proc_fdunlock(p); | |
1429 | } | |
1430 | static void | |
1431 | unp_discard_fdlocked(fg, p) | |
1432 | struct fileglob *fg; | |
1433 | struct proc *p; | |
1434 | { | |
1435 | ||
1436 | fg_removeuipc(fg); | |
1c79356b | 1437 | |
1c79356b | 1438 | unp_rights--; |
91447636 | 1439 | (void) closef_locked((struct fileproc *)0, fg, p); |
1c79356b | 1440 | } |