uipc_usrreq.c revision 1.147 1 /* $NetBSD: uipc_usrreq.c,v 1.147 2013/10/25 19:55:22 martin Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 2000, 2004, 2008, 2009 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center, and by Andrew Doran.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * Copyright (c) 1982, 1986, 1989, 1991, 1993
35 * The Regents of the University of California. All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
60 *
61 * @(#)uipc_usrreq.c 8.9 (Berkeley) 5/14/95
62 */
63
64 /*
65 * Copyright (c) 1997 Christopher G. Demetriou. All rights reserved.
66 *
67 * Redistribution and use in source and binary forms, with or without
68 * modification, are permitted provided that the following conditions
69 * are met:
70 * 1. Redistributions of source code must retain the above copyright
71 * notice, this list of conditions and the following disclaimer.
72 * 2. Redistributions in binary form must reproduce the above copyright
73 * notice, this list of conditions and the following disclaimer in the
74 * documentation and/or other materials provided with the distribution.
75 * 3. All advertising materials mentioning features or use of this software
76 * must display the following acknowledgement:
77 * This product includes software developed by the University of
78 * California, Berkeley and its contributors.
79 * 4. Neither the name of the University nor the names of its contributors
80 * may be used to endorse or promote products derived from this software
81 * without specific prior written permission.
82 *
83 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
84 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
85 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
86 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
87 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
88 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
89 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
90 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
91 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
92 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
93 * SUCH DAMAGE.
94 *
95 * @(#)uipc_usrreq.c 8.9 (Berkeley) 5/14/95
96 */
97
98 #include <sys/cdefs.h>
99 __KERNEL_RCSID(0, "$NetBSD: uipc_usrreq.c,v 1.147 2013/10/25 19:55:22 martin Exp $");
100
101 #include <sys/param.h>
102 #include <sys/systm.h>
103 #include <sys/proc.h>
104 #include <sys/filedesc.h>
105 #include <sys/domain.h>
106 #include <sys/protosw.h>
107 #include <sys/socket.h>
108 #include <sys/socketvar.h>
109 #include <sys/unpcb.h>
110 #include <sys/un.h>
111 #include <sys/namei.h>
112 #include <sys/vnode.h>
113 #include <sys/file.h>
114 #include <sys/stat.h>
115 #include <sys/mbuf.h>
116 #include <sys/kauth.h>
117 #include <sys/kmem.h>
118 #include <sys/atomic.h>
119 #include <sys/uidinfo.h>
120 #include <sys/kernel.h>
121 #include <sys/kthread.h>
122
123 /*
124 * Unix communications domain.
125 *
126 * TODO:
127 * RDM
128 * rethink name space problems
129 * need a proper out-of-band
130 *
131 * Notes on locking:
132 *
133 * The generic rules noted in uipc_socket2.c apply. In addition:
134 *
135 * o We have a global lock, uipc_lock.
136 *
137 * o All datagram sockets are locked by uipc_lock.
138 *
139 * o For stream socketpairs, the two endpoints are created sharing the same
140 * independent lock. Sockets presented to PRU_CONNECT2 must already have
141 * matching locks.
142 *
143 * o Stream sockets created via socket() start life with their own
144 * independent lock.
145 *
146 * o Stream connections to a named endpoint are slightly more complicated.
147 * Sockets that have called listen() have their lock pointer mutated to
148 * the global uipc_lock. When establishing a connection, the connecting
149 * socket also has its lock mutated to uipc_lock, which matches the head
150 * (listening socket). We create a new socket for accept() to return, and
151 * that also shares the head's lock. Until the connection is completely
152 * done on both ends, all three sockets are locked by uipc_lock. Once the
153 * connection is complete, the association with the head's lock is broken.
154 * The connecting socket and the socket returned from accept() have their
155 * lock pointers mutated away from uipc_lock, and back to the connecting
156 * socket's original, independent lock. The head continues to be locked
157 * by uipc_lock.
158 *
159 * o If uipc_lock is determined to be a significant source of contention,
160 * it could easily be hashed out. It is difficult to simply make it an
161 * independent lock because of visibility / garbage collection issues:
162 * if a socket has been associated with a lock at any point, that lock
163 * must remain valid until the socket is no longer visible in the system.
164 * The lock must not be freed or otherwise destroyed until any sockets
165 * that had referenced it have also been destroyed.
166 */
167 const struct sockaddr_un sun_noname = {
168 .sun_len = offsetof(struct sockaddr_un, sun_path),
169 .sun_family = AF_LOCAL,
170 };
171 ino_t unp_ino; /* prototype for fake inode numbers */
172
173 struct mbuf *unp_addsockcred(struct lwp *, struct mbuf *);
174 static void unp_mark(file_t *);
175 static void unp_scan(struct mbuf *, void (*)(file_t *), int);
176 static void unp_discard_now(file_t *);
177 static void unp_discard_later(file_t *);
178 static void unp_thread(void *);
179 static void unp_thread_kick(void);
180 static kmutex_t *uipc_lock;
181
182 static kcondvar_t unp_thread_cv;
183 static lwp_t *unp_thread_lwp;
184 static SLIST_HEAD(,file) unp_thread_discard;
185 static int unp_defer;
186
187 /*
188 * Initialize Unix protocols.
189 */
190 void
191 uipc_init(void)
192 {
193 int error;
194
195 uipc_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
196 cv_init(&unp_thread_cv, "unpgc");
197
198 error = kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL, unp_thread,
199 NULL, &unp_thread_lwp, "unpgc");
200 if (error != 0)
201 panic("uipc_init %d", error);
202 }
203
204 /*
205 * A connection succeeded: disassociate both endpoints from the head's
206 * lock, and make them share their own lock. There is a race here: for
207 * a very brief time one endpoint will be locked by a different lock
208 * than the other end. However, since the current thread holds the old
209 * lock (the listening socket's lock, the head) access can still only be
210 * made to one side of the connection.
211 */
212 static void
213 unp_setpeerlocks(struct socket *so, struct socket *so2)
214 {
215 struct unpcb *unp;
216 kmutex_t *lock;
217
218 KASSERT(solocked2(so, so2));
219
220 /*
221 * Bail out if either end of the socket is not yet fully
222 * connected or accepted. We only break the lock association
223 * with the head when the pair of sockets stand completely
224 * on their own.
225 */
226 KASSERT(so->so_head == NULL);
227 if (so2->so_head != NULL)
228 return;
229
230 /*
231 * Drop references to old lock. A third reference (from the
232 * queue head) must be held as we still hold its lock. Bonus:
233 * we don't need to worry about garbage collecting the lock.
234 */
235 lock = so->so_lock;
236 KASSERT(lock == uipc_lock);
237 mutex_obj_free(lock);
238 mutex_obj_free(lock);
239
240 /*
241 * Grab stream lock from the initiator and share between the two
242 * endpoints. Issue memory barrier to ensure all modifications
243 * become globally visible before the lock change. so2 is
244 * assumed not to have a stream lock, because it was created
245 * purely for the server side to accept this connection and
246 * started out life using the domain-wide lock.
247 */
248 unp = sotounpcb(so);
249 KASSERT(unp->unp_streamlock != NULL);
250 KASSERT(sotounpcb(so2)->unp_streamlock == NULL);
251 lock = unp->unp_streamlock;
252 unp->unp_streamlock = NULL;
253 mutex_obj_hold(lock);
254 membar_exit();
255 /*
256 * possible race if lock is not held - see comment in
257 * uipc_usrreq(PRU_ACCEPT).
258 */
259 KASSERT(mutex_owned(lock));
260 solockreset(so, lock);
261 solockreset(so2, lock);
262 }
263
264 /*
265 * Reset a socket's lock back to the domain-wide lock.
266 */
267 static void
268 unp_resetlock(struct socket *so)
269 {
270 kmutex_t *olock, *nlock;
271 struct unpcb *unp;
272
273 KASSERT(solocked(so));
274
275 olock = so->so_lock;
276 nlock = uipc_lock;
277 if (olock == nlock)
278 return;
279 unp = sotounpcb(so);
280 KASSERT(unp->unp_streamlock == NULL);
281 unp->unp_streamlock = olock;
282 mutex_obj_hold(nlock);
283 mutex_enter(nlock);
284 solockreset(so, nlock);
285 mutex_exit(olock);
286 }
287
288 static void
289 unp_free(struct unpcb *unp)
290 {
291
292 if (unp->unp_addr)
293 free(unp->unp_addr, M_SONAME);
294 if (unp->unp_streamlock != NULL)
295 mutex_obj_free(unp->unp_streamlock);
296 free(unp, M_PCB);
297 }
298
299 int
300 unp_output(struct mbuf *m, struct mbuf *control, struct unpcb *unp,
301 struct lwp *l)
302 {
303 struct socket *so2;
304 const struct sockaddr_un *sun;
305
306 so2 = unp->unp_conn->unp_socket;
307
308 KASSERT(solocked(so2));
309
310 if (unp->unp_addr)
311 sun = unp->unp_addr;
312 else
313 sun = &sun_noname;
314 if (unp->unp_conn->unp_flags & UNP_WANTCRED)
315 control = unp_addsockcred(l, control);
316 if (sbappendaddr(&so2->so_rcv, (const struct sockaddr *)sun, m,
317 control) == 0) {
318 so2->so_rcv.sb_overflowed++;
319 unp_dispose(control);
320 m_freem(control);
321 m_freem(m);
322 return (ENOBUFS);
323 } else {
324 sorwakeup(so2);
325 return (0);
326 }
327 }
328
329 void
330 unp_setaddr(struct socket *so, struct mbuf *nam, bool peeraddr)
331 {
332 const struct sockaddr_un *sun;
333 struct unpcb *unp;
334 bool ext;
335
336 KASSERT(solocked(so));
337 unp = sotounpcb(so);
338 ext = false;
339
340 for (;;) {
341 sun = NULL;
342 if (peeraddr) {
343 if (unp->unp_conn && unp->unp_conn->unp_addr)
344 sun = unp->unp_conn->unp_addr;
345 } else {
346 if (unp->unp_addr)
347 sun = unp->unp_addr;
348 }
349 if (sun == NULL)
350 sun = &sun_noname;
351 nam->m_len = sun->sun_len;
352 if (nam->m_len > MLEN && !ext) {
353 sounlock(so);
354 MEXTMALLOC(nam, MAXPATHLEN * 2, M_WAITOK);
355 solock(so);
356 ext = true;
357 } else {
358 KASSERT(nam->m_len <= MAXPATHLEN * 2);
359 memcpy(mtod(nam, void *), sun, (size_t)nam->m_len);
360 break;
361 }
362 }
363 }
364
365 /*ARGSUSED*/
366 int
367 uipc_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
368 struct mbuf *control, struct lwp *l)
369 {
370 struct unpcb *unp = sotounpcb(so);
371 struct socket *so2;
372 u_int newhiwat;
373 int error = 0;
374
375 if (req == PRU_CONTROL)
376 return (EOPNOTSUPP);
377
378 #ifdef DIAGNOSTIC
379 if (req != PRU_SEND && req != PRU_SENDOOB && control)
380 panic("uipc_usrreq: unexpected control mbuf");
381 #endif
382 if (req != PRU_ATTACH) {
383 if (unp == NULL) {
384 error = EINVAL;
385 goto release;
386 }
387 KASSERT(solocked(so));
388 }
389
390 switch (req) {
391
392 case PRU_ATTACH:
393 if (unp != NULL) {
394 error = EISCONN;
395 break;
396 }
397 error = unp_attach(so);
398 break;
399
400 case PRU_DETACH:
401 unp_detach(unp);
402 break;
403
404 case PRU_BIND:
405 KASSERT(l != NULL);
406 error = unp_bind(so, nam, l);
407 break;
408
409 case PRU_LISTEN:
410 /*
411 * If the socket can accept a connection, it must be
412 * locked by uipc_lock.
413 */
414 unp_resetlock(so);
415 if (unp->unp_vnode == NULL)
416 error = EINVAL;
417 break;
418
419 case PRU_CONNECT:
420 KASSERT(l != NULL);
421 error = unp_connect(so, nam, l);
422 break;
423
424 case PRU_CONNECT2:
425 error = unp_connect2(so, (struct socket *)nam, PRU_CONNECT2);
426 break;
427
428 case PRU_DISCONNECT:
429 unp_disconnect(unp);
430 break;
431
432 case PRU_ACCEPT:
433 KASSERT(so->so_lock == uipc_lock);
434 /*
435 * Mark the initiating STREAM socket as connected *ONLY*
436 * after it's been accepted. This prevents a client from
437 * overrunning a server and receiving ECONNREFUSED.
438 */
439 if (unp->unp_conn == NULL) {
440 /*
441 * This will use the empty socket and will not
442 * allocate.
443 */
444 unp_setaddr(so, nam, true);
445 break;
446 }
447 so2 = unp->unp_conn->unp_socket;
448 if (so2->so_state & SS_ISCONNECTING) {
449 KASSERT(solocked2(so, so->so_head));
450 KASSERT(solocked2(so2, so->so_head));
451 soisconnected(so2);
452 }
453 /*
454 * If the connection is fully established, break the
455 * association with uipc_lock and give the connected
456 * pair a seperate lock to share.
457 * There is a race here: sotounpcb(so2)->unp_streamlock
458 * is not locked, so when changing so2->so_lock
459 * another thread can grab it while so->so_lock is still
460 * pointing to the (locked) uipc_lock.
461 * this should be harmless, except that this makes
462 * solocked2() and solocked() unreliable.
463 * Another problem is that unp_setaddr() expects the
464 * the socket locked. Grabing sotounpcb(so2)->unp_streamlock
465 * fixes both issues.
466 */
467 mutex_enter(sotounpcb(so2)->unp_streamlock);
468 unp_setpeerlocks(so2, so);
469 /*
470 * Only now return peer's address, as we may need to
471 * block in order to allocate memory.
472 *
473 * XXX Minor race: connection can be broken while
474 * lock is dropped in unp_setaddr(). We will return
475 * error == 0 and sun_noname as the peer address.
476 */
477 unp_setaddr(so, nam, true);
478 /* so_lock now points to unp_streamlock */
479 mutex_exit(so2->so_lock);
480 break;
481
482 case PRU_SHUTDOWN:
483 socantsendmore(so);
484 unp_shutdown(unp);
485 break;
486
487 case PRU_RCVD:
488 switch (so->so_type) {
489
490 case SOCK_DGRAM:
491 panic("uipc 1");
492 /*NOTREACHED*/
493
494 case SOCK_SEQPACKET: /* FALLTHROUGH */
495 case SOCK_STREAM:
496 #define rcv (&so->so_rcv)
497 #define snd (&so2->so_snd)
498 if (unp->unp_conn == 0)
499 break;
500 so2 = unp->unp_conn->unp_socket;
501 KASSERT(solocked2(so, so2));
502 /*
503 * Adjust backpressure on sender
504 * and wakeup any waiting to write.
505 */
506 snd->sb_mbmax += unp->unp_mbcnt - rcv->sb_mbcnt;
507 unp->unp_mbcnt = rcv->sb_mbcnt;
508 newhiwat = snd->sb_hiwat + unp->unp_cc - rcv->sb_cc;
509 (void)chgsbsize(so2->so_uidinfo,
510 &snd->sb_hiwat, newhiwat, RLIM_INFINITY);
511 unp->unp_cc = rcv->sb_cc;
512 sowwakeup(so2);
513 #undef snd
514 #undef rcv
515 break;
516
517 default:
518 panic("uipc 2");
519 }
520 break;
521
522 case PRU_SEND:
523 /*
524 * Note: unp_internalize() rejects any control message
525 * other than SCM_RIGHTS, and only allows one. This
526 * has the side-effect of preventing a caller from
527 * forging SCM_CREDS.
528 */
529 if (control) {
530 sounlock(so);
531 error = unp_internalize(&control);
532 solock(so);
533 if (error != 0) {
534 m_freem(control);
535 m_freem(m);
536 break;
537 }
538 }
539 switch (so->so_type) {
540
541 case SOCK_DGRAM: {
542 KASSERT(so->so_lock == uipc_lock);
543 if (nam) {
544 if ((so->so_state & SS_ISCONNECTED) != 0)
545 error = EISCONN;
546 else {
547 /*
548 * Note: once connected, the
549 * socket's lock must not be
550 * dropped until we have sent
551 * the message and disconnected.
552 * This is necessary to prevent
553 * intervening control ops, like
554 * another connection.
555 */
556 error = unp_connect(so, nam, l);
557 }
558 } else {
559 if ((so->so_state & SS_ISCONNECTED) == 0)
560 error = ENOTCONN;
561 }
562 if (error) {
563 unp_dispose(control);
564 m_freem(control);
565 m_freem(m);
566 break;
567 }
568 KASSERT(l != NULL);
569 error = unp_output(m, control, unp, l);
570 if (nam)
571 unp_disconnect(unp);
572 break;
573 }
574
575 case SOCK_SEQPACKET: /* FALLTHROUGH */
576 case SOCK_STREAM:
577 #define rcv (&so2->so_rcv)
578 #define snd (&so->so_snd)
579 if (unp->unp_conn == NULL) {
580 error = ENOTCONN;
581 break;
582 }
583 so2 = unp->unp_conn->unp_socket;
584 KASSERT(solocked2(so, so2));
585 if (unp->unp_conn->unp_flags & UNP_WANTCRED) {
586 /*
587 * Credentials are passed only once on
588 * SOCK_STREAM and SOCK_SEQPACKET.
589 */
590 unp->unp_conn->unp_flags &= ~UNP_WANTCRED;
591 control = unp_addsockcred(l, control);
592 }
593 /*
594 * Send to paired receive port, and then reduce
595 * send buffer hiwater marks to maintain backpressure.
596 * Wake up readers.
597 */
598 if (control) {
599 if (sbappendcontrol(rcv, m, control) != 0)
600 control = NULL;
601 } else {
602 switch(so->so_type) {
603 case SOCK_SEQPACKET:
604 sbappendrecord(rcv, m);
605 break;
606 case SOCK_STREAM:
607 sbappend(rcv, m);
608 break;
609 default:
610 panic("uipc_usrreq");
611 break;
612 }
613 }
614 snd->sb_mbmax -=
615 rcv->sb_mbcnt - unp->unp_conn->unp_mbcnt;
616 unp->unp_conn->unp_mbcnt = rcv->sb_mbcnt;
617 newhiwat = snd->sb_hiwat -
618 (rcv->sb_cc - unp->unp_conn->unp_cc);
619 (void)chgsbsize(so->so_uidinfo,
620 &snd->sb_hiwat, newhiwat, RLIM_INFINITY);
621 unp->unp_conn->unp_cc = rcv->sb_cc;
622 sorwakeup(so2);
623 #undef snd
624 #undef rcv
625 if (control != NULL) {
626 unp_dispose(control);
627 m_freem(control);
628 }
629 break;
630
631 default:
632 panic("uipc 4");
633 }
634 break;
635
636 case PRU_ABORT:
637 (void)unp_drop(unp, ECONNABORTED);
638
639 KASSERT(so->so_head == NULL);
640 #ifdef DIAGNOSTIC
641 if (so->so_pcb == NULL)
642 panic("uipc 5: drop killed pcb");
643 #endif
644 unp_detach(unp);
645 break;
646
647 case PRU_SENSE:
648 ((struct stat *) m)->st_blksize = so->so_snd.sb_hiwat;
649 switch (so->so_type) {
650 case SOCK_SEQPACKET: /* FALLTHROUGH */
651 case SOCK_STREAM:
652 if (unp->unp_conn == 0)
653 break;
654
655 so2 = unp->unp_conn->unp_socket;
656 KASSERT(solocked2(so, so2));
657 ((struct stat *) m)->st_blksize += so2->so_rcv.sb_cc;
658 break;
659 default:
660 break;
661 }
662 ((struct stat *) m)->st_dev = NODEV;
663 if (unp->unp_ino == 0)
664 unp->unp_ino = unp_ino++;
665 ((struct stat *) m)->st_atimespec =
666 ((struct stat *) m)->st_mtimespec =
667 ((struct stat *) m)->st_ctimespec = unp->unp_ctime;
668 ((struct stat *) m)->st_ino = unp->unp_ino;
669 return (0);
670
671 case PRU_RCVOOB:
672 error = EOPNOTSUPP;
673 break;
674
675 case PRU_SENDOOB:
676 m_freem(control);
677 m_freem(m);
678 error = EOPNOTSUPP;
679 break;
680
681 case PRU_SOCKADDR:
682 unp_setaddr(so, nam, false);
683 break;
684
685 case PRU_PEERADDR:
686 unp_setaddr(so, nam, true);
687 break;
688
689 default:
690 panic("piusrreq");
691 }
692
693 release:
694 return (error);
695 }
696
697 /*
698 * Unix domain socket option processing.
699 */
700 int
701 uipc_ctloutput(int op, struct socket *so, struct sockopt *sopt)
702 {
703 struct unpcb *unp = sotounpcb(so);
704 int optval = 0, error = 0;
705
706 KASSERT(solocked(so));
707
708 if (sopt->sopt_level != 0) {
709 error = ENOPROTOOPT;
710 } else switch (op) {
711
712 case PRCO_SETOPT:
713 switch (sopt->sopt_name) {
714 case LOCAL_CREDS:
715 case LOCAL_CONNWAIT:
716 error = sockopt_getint(sopt, &optval);
717 if (error)
718 break;
719 switch (sopt->sopt_name) {
720 #define OPTSET(bit) \
721 if (optval) \
722 unp->unp_flags |= (bit); \
723 else \
724 unp->unp_flags &= ~(bit);
725
726 case LOCAL_CREDS:
727 OPTSET(UNP_WANTCRED);
728 break;
729 case LOCAL_CONNWAIT:
730 OPTSET(UNP_CONNWAIT);
731 break;
732 }
733 break;
734 #undef OPTSET
735
736 default:
737 error = ENOPROTOOPT;
738 break;
739 }
740 break;
741
742 case PRCO_GETOPT:
743 sounlock(so);
744 switch (sopt->sopt_name) {
745 case LOCAL_PEEREID:
746 if (unp->unp_flags & UNP_EIDSVALID) {
747 error = sockopt_set(sopt,
748 &unp->unp_connid, sizeof(unp->unp_connid));
749 } else {
750 error = EINVAL;
751 }
752 break;
753 case LOCAL_CREDS:
754 #define OPTBIT(bit) (unp->unp_flags & (bit) ? 1 : 0)
755
756 optval = OPTBIT(UNP_WANTCRED);
757 error = sockopt_setint(sopt, optval);
758 break;
759 #undef OPTBIT
760
761 default:
762 error = ENOPROTOOPT;
763 break;
764 }
765 solock(so);
766 break;
767 }
768 return (error);
769 }
770
771 /*
772 * Both send and receive buffers are allocated PIPSIZ bytes of buffering
773 * for stream sockets, although the total for sender and receiver is
774 * actually only PIPSIZ.
775 * Datagram sockets really use the sendspace as the maximum datagram size,
776 * and don't really want to reserve the sendspace. Their recvspace should
777 * be large enough for at least one max-size datagram plus address.
778 */
779 #define PIPSIZ 4096
780 u_long unpst_sendspace = PIPSIZ;
781 u_long unpst_recvspace = PIPSIZ;
782 u_long unpdg_sendspace = 2*1024; /* really max datagram size */
783 u_long unpdg_recvspace = 4*1024;
784
785 u_int unp_rights; /* files in flight */
786 u_int unp_rights_ratio = 2; /* limit, fraction of maxfiles */
787
788 int
789 unp_attach(struct socket *so)
790 {
791 struct unpcb *unp;
792 int error;
793
794 switch (so->so_type) {
795 case SOCK_SEQPACKET: /* FALLTHROUGH */
796 case SOCK_STREAM:
797 if (so->so_lock == NULL) {
798 /*
799 * XXX Assuming that no socket locks are held,
800 * as this call may sleep.
801 */
802 so->so_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
803 solock(so);
804 }
805 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
806 error = soreserve(so, unpst_sendspace, unpst_recvspace);
807 if (error != 0)
808 return (error);
809 }
810 break;
811
812 case SOCK_DGRAM:
813 if (so->so_lock == NULL) {
814 mutex_obj_hold(uipc_lock);
815 so->so_lock = uipc_lock;
816 solock(so);
817 }
818 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
819 error = soreserve(so, unpdg_sendspace, unpdg_recvspace);
820 if (error != 0)
821 return (error);
822 }
823 break;
824
825 default:
826 panic("unp_attach");
827 }
828 KASSERT(solocked(so));
829 unp = malloc(sizeof(*unp), M_PCB, M_NOWAIT);
830 if (unp == NULL)
831 return (ENOBUFS);
832 memset(unp, 0, sizeof(*unp));
833 unp->unp_socket = so;
834 so->so_pcb = unp;
835 nanotime(&unp->unp_ctime);
836 return (0);
837 }
838
839 void
840 unp_detach(struct unpcb *unp)
841 {
842 struct socket *so;
843 vnode_t *vp;
844
845 so = unp->unp_socket;
846
847 retry:
848 if ((vp = unp->unp_vnode) != NULL) {
849 sounlock(so);
850 /* Acquire v_interlock to protect against unp_connect(). */
851 /* XXXAD racy */
852 mutex_enter(vp->v_interlock);
853 vp->v_socket = NULL;
854 vrelel(vp, 0);
855 solock(so);
856 unp->unp_vnode = NULL;
857 }
858 if (unp->unp_conn)
859 unp_disconnect(unp);
860 while (unp->unp_refs) {
861 KASSERT(solocked2(so, unp->unp_refs->unp_socket));
862 if (unp_drop(unp->unp_refs, ECONNRESET)) {
863 solock(so);
864 goto retry;
865 }
866 }
867 soisdisconnected(so);
868 so->so_pcb = NULL;
869 if (unp_rights) {
870 /*
871 * Normally the receive buffer is flushed later, in sofree,
872 * but if our receive buffer holds references to files that
873 * are now garbage, we will enqueue those file references to
874 * the garbage collector and kick it into action.
875 */
876 sorflush(so);
877 unp_free(unp);
878 unp_thread_kick();
879 } else
880 unp_free(unp);
881 }
882
883 /*
884 * Allocate the new sockaddr. We have to allocate one
885 * extra byte so that we can ensure that the pathname
886 * is nul-terminated. Note that unlike linux, we don't
887 * include in the address length the NUL in the path
888 * component, because doing so, would exceed sizeof(sockaddr_un)
889 * for fully occupied pathnames. Linux is also inconsistent,
890 * because it does not include the NUL in the length of
891 * what it calls "abstract" unix sockets.
892 */
893 static struct sockaddr_un *
894 makeun(struct mbuf *nam, size_t *addrlen) {
895 struct sockaddr_un *sun;
896
897 *addrlen = nam->m_len + 1;
898 sun = malloc(*addrlen, M_SONAME, M_WAITOK);
899 m_copydata(nam, 0, nam->m_len, (void *)sun);
900 *(((char *)sun) + nam->m_len) = '\0';
901 sun->sun_len = strlen(sun->sun_path) +
902 offsetof(struct sockaddr_un, sun_path);
903 return sun;
904 }
905
906 int
907 unp_bind(struct socket *so, struct mbuf *nam, struct lwp *l)
908 {
909 struct sockaddr_un *sun;
910 struct unpcb *unp;
911 vnode_t *vp;
912 struct vattr vattr;
913 size_t addrlen;
914 int error;
915 struct pathbuf *pb;
916 struct nameidata nd;
917 proc_t *p;
918
919 unp = sotounpcb(so);
920 if (unp->unp_vnode != NULL)
921 return (EINVAL);
922 if ((unp->unp_flags & UNP_BUSY) != 0) {
923 /*
924 * EALREADY may not be strictly accurate, but since this
925 * is a major application error it's hardly a big deal.
926 */
927 return (EALREADY);
928 }
929 unp->unp_flags |= UNP_BUSY;
930 sounlock(so);
931
932 p = l->l_proc;
933 sun = makeun(nam, &addrlen);
934
935 pb = pathbuf_create(sun->sun_path);
936 if (pb == NULL) {
937 error = ENOMEM;
938 goto bad;
939 }
940 NDINIT(&nd, CREATE, FOLLOW | LOCKPARENT | TRYEMULROOT, pb);
941
942 /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
943 if ((error = namei(&nd)) != 0) {
944 pathbuf_destroy(pb);
945 goto bad;
946 }
947 vp = nd.ni_vp;
948 if (vp != NULL) {
949 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
950 if (nd.ni_dvp == vp)
951 vrele(nd.ni_dvp);
952 else
953 vput(nd.ni_dvp);
954 vrele(vp);
955 pathbuf_destroy(pb);
956 error = EADDRINUSE;
957 goto bad;
958 }
959 vattr_null(&vattr);
960 vattr.va_type = VSOCK;
961 vattr.va_mode = ACCESSPERMS & ~(p->p_cwdi->cwdi_cmask);
962 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
963 if (error) {
964 pathbuf_destroy(pb);
965 goto bad;
966 }
967 vp = nd.ni_vp;
968 solock(so);
969 vp->v_socket = unp->unp_socket;
970 unp->unp_vnode = vp;
971 unp->unp_addrlen = addrlen;
972 unp->unp_addr = sun;
973 unp->unp_connid.unp_pid = p->p_pid;
974 unp->unp_connid.unp_euid = kauth_cred_geteuid(l->l_cred);
975 unp->unp_connid.unp_egid = kauth_cred_getegid(l->l_cred);
976 unp->unp_flags |= UNP_EIDSBIND;
977 VOP_UNLOCK(vp);
978 unp->unp_flags &= ~UNP_BUSY;
979 pathbuf_destroy(pb);
980 return (0);
981
982 bad:
983 free(sun, M_SONAME);
984 solock(so);
985 unp->unp_flags &= ~UNP_BUSY;
986 return (error);
987 }
988
989 int
990 unp_connect(struct socket *so, struct mbuf *nam, struct lwp *l)
991 {
992 struct sockaddr_un *sun;
993 vnode_t *vp;
994 struct socket *so2, *so3;
995 struct unpcb *unp, *unp2, *unp3;
996 size_t addrlen;
997 int error;
998 struct pathbuf *pb;
999 struct nameidata nd;
1000
1001 unp = sotounpcb(so);
1002 if ((unp->unp_flags & UNP_BUSY) != 0) {
1003 /*
1004 * EALREADY may not be strictly accurate, but since this
1005 * is a major application error it's hardly a big deal.
1006 */
1007 return (EALREADY);
1008 }
1009 unp->unp_flags |= UNP_BUSY;
1010 sounlock(so);
1011
1012 sun = makeun(nam, &addrlen);
1013 pb = pathbuf_create(sun->sun_path);
1014 if (pb == NULL) {
1015 error = ENOMEM;
1016 goto bad2;
1017 }
1018
1019 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, pb);
1020
1021 if ((error = namei(&nd)) != 0) {
1022 pathbuf_destroy(pb);
1023 goto bad2;
1024 }
1025 vp = nd.ni_vp;
1026 if (vp->v_type != VSOCK) {
1027 error = ENOTSOCK;
1028 goto bad;
1029 }
1030 pathbuf_destroy(pb);
1031 if ((error = VOP_ACCESS(vp, VWRITE, l->l_cred)) != 0)
1032 goto bad;
1033 /* Acquire v_interlock to protect against unp_detach(). */
1034 mutex_enter(vp->v_interlock);
1035 so2 = vp->v_socket;
1036 if (so2 == NULL) {
1037 mutex_exit(vp->v_interlock);
1038 error = ECONNREFUSED;
1039 goto bad;
1040 }
1041 if (so->so_type != so2->so_type) {
1042 mutex_exit(vp->v_interlock);
1043 error = EPROTOTYPE;
1044 goto bad;
1045 }
1046 solock(so);
1047 unp_resetlock(so);
1048 mutex_exit(vp->v_interlock);
1049 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) != 0) {
1050 /*
1051 * This may seem somewhat fragile but is OK: if we can
1052 * see SO_ACCEPTCONN set on the endpoint, then it must
1053 * be locked by the domain-wide uipc_lock.
1054 */
1055 KASSERT((so2->so_options & SO_ACCEPTCONN) == 0 ||
1056 so2->so_lock == uipc_lock);
1057 if ((so2->so_options & SO_ACCEPTCONN) == 0 ||
1058 (so3 = sonewconn(so2, false)) == NULL) {
1059 error = ECONNREFUSED;
1060 sounlock(so);
1061 goto bad;
1062 }
1063 unp2 = sotounpcb(so2);
1064 unp3 = sotounpcb(so3);
1065 if (unp2->unp_addr) {
1066 unp3->unp_addr = malloc(unp2->unp_addrlen,
1067 M_SONAME, M_WAITOK);
1068 memcpy(unp3->unp_addr, unp2->unp_addr,
1069 unp2->unp_addrlen);
1070 unp3->unp_addrlen = unp2->unp_addrlen;
1071 }
1072 unp3->unp_flags = unp2->unp_flags;
1073 unp3->unp_connid.unp_pid = l->l_proc->p_pid;
1074 unp3->unp_connid.unp_euid = kauth_cred_geteuid(l->l_cred);
1075 unp3->unp_connid.unp_egid = kauth_cred_getegid(l->l_cred);
1076 unp3->unp_flags |= UNP_EIDSVALID;
1077 if (unp2->unp_flags & UNP_EIDSBIND) {
1078 unp->unp_connid = unp2->unp_connid;
1079 unp->unp_flags |= UNP_EIDSVALID;
1080 }
1081 so2 = so3;
1082 }
1083 error = unp_connect2(so, so2, PRU_CONNECT);
1084 sounlock(so);
1085 bad:
1086 vput(vp);
1087 bad2:
1088 free(sun, M_SONAME);
1089 solock(so);
1090 unp->unp_flags &= ~UNP_BUSY;
1091 return (error);
1092 }
1093
1094 int
1095 unp_connect2(struct socket *so, struct socket *so2, int req)
1096 {
1097 struct unpcb *unp = sotounpcb(so);
1098 struct unpcb *unp2;
1099
1100 if (so2->so_type != so->so_type)
1101 return (EPROTOTYPE);
1102
1103 /*
1104 * All three sockets involved must be locked by same lock:
1105 *
1106 * local endpoint (so)
1107 * remote endpoint (so2)
1108 * queue head (so2->so_head, only if PR_CONNREQUIRED)
1109 */
1110 KASSERT(solocked2(so, so2));
1111 KASSERT(so->so_head == NULL);
1112 if (so2->so_head != NULL) {
1113 KASSERT(so2->so_lock == uipc_lock);
1114 KASSERT(solocked2(so2, so2->so_head));
1115 }
1116
1117 unp2 = sotounpcb(so2);
1118 unp->unp_conn = unp2;
1119 switch (so->so_type) {
1120
1121 case SOCK_DGRAM:
1122 unp->unp_nextref = unp2->unp_refs;
1123 unp2->unp_refs = unp;
1124 soisconnected(so);
1125 break;
1126
1127 case SOCK_SEQPACKET: /* FALLTHROUGH */
1128 case SOCK_STREAM:
1129 unp2->unp_conn = unp;
1130 if (req == PRU_CONNECT &&
1131 ((unp->unp_flags | unp2->unp_flags) & UNP_CONNWAIT))
1132 soisconnecting(so);
1133 else
1134 soisconnected(so);
1135 soisconnected(so2);
1136 /*
1137 * If the connection is fully established, break the
1138 * association with uipc_lock and give the connected
1139 * pair a seperate lock to share. For CONNECT2, we
1140 * require that the locks already match (the sockets
1141 * are created that way).
1142 */
1143 if (req == PRU_CONNECT) {
1144 KASSERT(so2->so_head != NULL);
1145 unp_setpeerlocks(so, so2);
1146 }
1147 break;
1148
1149 default:
1150 panic("unp_connect2");
1151 }
1152 return (0);
1153 }
1154
1155 void
1156 unp_disconnect(struct unpcb *unp)
1157 {
1158 struct unpcb *unp2 = unp->unp_conn;
1159 struct socket *so;
1160
1161 if (unp2 == 0)
1162 return;
1163 unp->unp_conn = 0;
1164 so = unp->unp_socket;
1165 switch (so->so_type) {
1166 case SOCK_DGRAM:
1167 if (unp2->unp_refs == unp)
1168 unp2->unp_refs = unp->unp_nextref;
1169 else {
1170 unp2 = unp2->unp_refs;
1171 for (;;) {
1172 KASSERT(solocked2(so, unp2->unp_socket));
1173 if (unp2 == 0)
1174 panic("unp_disconnect");
1175 if (unp2->unp_nextref == unp)
1176 break;
1177 unp2 = unp2->unp_nextref;
1178 }
1179 unp2->unp_nextref = unp->unp_nextref;
1180 }
1181 unp->unp_nextref = 0;
1182 so->so_state &= ~SS_ISCONNECTED;
1183 break;
1184
1185 case SOCK_SEQPACKET: /* FALLTHROUGH */
1186 case SOCK_STREAM:
1187 KASSERT(solocked2(so, unp2->unp_socket));
1188 soisdisconnected(so);
1189 unp2->unp_conn = 0;
1190 soisdisconnected(unp2->unp_socket);
1191 break;
1192 }
1193 }
1194
1195 #ifdef notdef
1196 unp_abort(struct unpcb *unp)
1197 {
1198 unp_detach(unp);
1199 }
1200 #endif
1201
1202 void
1203 unp_shutdown(struct unpcb *unp)
1204 {
1205 struct socket *so;
1206
1207 switch(unp->unp_socket->so_type) {
1208 case SOCK_SEQPACKET: /* FALLTHROUGH */
1209 case SOCK_STREAM:
1210 if (unp->unp_conn && (so = unp->unp_conn->unp_socket))
1211 socantrcvmore(so);
1212 break;
1213 default:
1214 break;
1215 }
1216 }
1217
1218 bool
1219 unp_drop(struct unpcb *unp, int errno)
1220 {
1221 struct socket *so = unp->unp_socket;
1222
1223 KASSERT(solocked(so));
1224
1225 so->so_error = errno;
1226 unp_disconnect(unp);
1227 if (so->so_head) {
1228 so->so_pcb = NULL;
1229 /* sofree() drops the socket lock */
1230 sofree(so);
1231 unp_free(unp);
1232 return true;
1233 }
1234 return false;
1235 }
1236
1237 #ifdef notdef
1238 unp_drain(void)
1239 {
1240
1241 }
1242 #endif
1243
1244 int
1245 unp_externalize(struct mbuf *rights, struct lwp *l, int flags)
1246 {
1247 struct cmsghdr * const cm = mtod(rights, struct cmsghdr *);
1248 struct proc * const p = l->l_proc;
1249 file_t **rp;
1250 int error = 0;
1251
1252 const size_t nfds = (cm->cmsg_len - CMSG_ALIGN(sizeof(*cm))) /
1253 sizeof(file_t *);
1254 if (nfds == 0)
1255 goto noop;
1256
1257 int * const fdp = kmem_alloc(nfds * sizeof(int), KM_SLEEP);
1258 rw_enter(&p->p_cwdi->cwdi_lock, RW_READER);
1259
1260 /* Make sure the recipient should be able to see the files.. */
1261 rp = (file_t **)CMSG_DATA(cm);
1262 for (size_t i = 0; i < nfds; i++) {
1263 file_t * const fp = *rp++;
1264 if (fp == NULL) {
1265 error = EINVAL;
1266 goto out;
1267 }
1268 /*
1269 * If we are in a chroot'ed directory, and
1270 * someone wants to pass us a directory, make
1271 * sure it's inside the subtree we're allowed
1272 * to access.
1273 */
1274 if (p->p_cwdi->cwdi_rdir != NULL && fp->f_type == DTYPE_VNODE) {
1275 vnode_t *vp = (vnode_t *)fp->f_data;
1276 if ((vp->v_type == VDIR) &&
1277 !vn_isunder(vp, p->p_cwdi->cwdi_rdir, l)) {
1278 error = EPERM;
1279 goto out;
1280 }
1281 }
1282 }
1283
1284 restart:
1285 /*
1286 * First loop -- allocate file descriptor table slots for the
1287 * new files.
1288 */
1289 for (size_t i = 0; i < nfds; i++) {
1290 if ((error = fd_alloc(p, 0, &fdp[i])) != 0) {
1291 /*
1292 * Back out what we've done so far.
1293 */
1294 while (i-- > 0) {
1295 fd_abort(p, NULL, fdp[i]);
1296 }
1297 if (error == ENOSPC) {
1298 fd_tryexpand(p);
1299 error = 0;
1300 goto restart;
1301 }
1302 /*
1303 * This is the error that has historically
1304 * been returned, and some callers may
1305 * expect it.
1306 */
1307 error = EMSGSIZE;
1308 goto out;
1309 }
1310 }
1311
1312 /*
1313 * Now that adding them has succeeded, update all of the
1314 * file passing state and affix the descriptors.
1315 */
1316 rp = (file_t **)CMSG_DATA(cm);
1317 int *ofdp = (int *)CMSG_DATA(cm);
1318 for (size_t i = 0; i < nfds; i++) {
1319 file_t * const fp = *rp++;
1320 const int fd = fdp[i];
1321 atomic_dec_uint(&unp_rights);
1322 fd_set_exclose(l, fd, (flags & O_CLOEXEC) != 0);
1323 fd_affix(p, fp, fd);
1324 /*
1325 * Done with this file pointer, replace it with a fd;
1326 */
1327 *ofdp++ = fd;
1328 mutex_enter(&fp->f_lock);
1329 fp->f_msgcount--;
1330 mutex_exit(&fp->f_lock);
1331 /*
1332 * Note that fd_affix() adds a reference to the file.
1333 * The file may already have been closed by another
1334 * LWP in the process, so we must drop the reference
1335 * added by unp_internalize() with closef().
1336 */
1337 closef(fp);
1338 }
1339
1340 /*
1341 * Adjust length, in case of transition from large file_t
1342 * pointers to ints.
1343 */
1344 if (sizeof(file_t *) != sizeof(int)) {
1345 cm->cmsg_len = CMSG_LEN(nfds * sizeof(int));
1346 rights->m_len = CMSG_SPACE(nfds * sizeof(int));
1347 }
1348 out:
1349 if (__predict_false(error != 0)) {
1350 file_t **const fpp = (file_t **)CMSG_DATA(cm);
1351 for (size_t i = 0; i < nfds; i++)
1352 unp_discard_now(fpp[i]);
1353 /*
1354 * Truncate the array so that nobody will try to interpret
1355 * what is now garbage in it.
1356 */
1357 cm->cmsg_len = CMSG_LEN(0);
1358 rights->m_len = CMSG_SPACE(0);
1359 }
1360 rw_exit(&p->p_cwdi->cwdi_lock);
1361 kmem_free(fdp, nfds * sizeof(int));
1362
1363 noop:
1364 /*
1365 * Don't disclose kernel memory in the alignment space.
1366 */
1367 KASSERT(cm->cmsg_len <= rights->m_len);
1368 memset(&mtod(rights, char *)[cm->cmsg_len], 0, rights->m_len -
1369 cm->cmsg_len);
1370 return error;
1371 }
1372
1373 int
1374 unp_internalize(struct mbuf **controlp)
1375 {
1376 filedesc_t *fdescp = curlwp->l_fd;
1377 struct mbuf *control = *controlp;
1378 struct cmsghdr *newcm, *cm = mtod(control, struct cmsghdr *);
1379 file_t **rp, **files;
1380 file_t *fp;
1381 int i, fd, *fdp;
1382 int nfds, error;
1383 u_int maxmsg;
1384
1385 error = 0;
1386 newcm = NULL;
1387
1388 /* Sanity check the control message header. */
1389 if (cm->cmsg_type != SCM_RIGHTS || cm->cmsg_level != SOL_SOCKET ||
1390 cm->cmsg_len > control->m_len ||
1391 cm->cmsg_len < CMSG_ALIGN(sizeof(*cm)))
1392 return (EINVAL);
1393
1394 /*
1395 * Verify that the file descriptors are valid, and acquire
1396 * a reference to each.
1397 */
1398 nfds = (cm->cmsg_len - CMSG_ALIGN(sizeof(*cm))) / sizeof(int);
1399 fdp = (int *)CMSG_DATA(cm);
1400 maxmsg = maxfiles / unp_rights_ratio;
1401 for (i = 0; i < nfds; i++) {
1402 fd = *fdp++;
1403 if (atomic_inc_uint_nv(&unp_rights) > maxmsg) {
1404 atomic_dec_uint(&unp_rights);
1405 nfds = i;
1406 error = EAGAIN;
1407 goto out;
1408 }
1409 if ((fp = fd_getfile(fd)) == NULL
1410 || fp->f_type == DTYPE_KQUEUE) {
1411 if (fp)
1412 fd_putfile(fd);
1413 atomic_dec_uint(&unp_rights);
1414 nfds = i;
1415 error = EBADF;
1416 goto out;
1417 }
1418 }
1419
1420 /* Allocate new space and copy header into it. */
1421 newcm = malloc(CMSG_SPACE(nfds * sizeof(file_t *)), M_MBUF, M_WAITOK);
1422 if (newcm == NULL) {
1423 error = E2BIG;
1424 goto out;
1425 }
1426 memcpy(newcm, cm, sizeof(struct cmsghdr));
1427 files = (file_t **)CMSG_DATA(newcm);
1428
1429 /*
1430 * Transform the file descriptors into file_t pointers, in
1431 * reverse order so that if pointers are bigger than ints, the
1432 * int won't get until we're done. No need to lock, as we have
1433 * already validated the descriptors with fd_getfile().
1434 */
1435 fdp = (int *)CMSG_DATA(cm) + nfds;
1436 rp = files + nfds;
1437 for (i = 0; i < nfds; i++) {
1438 fp = fdescp->fd_dt->dt_ff[*--fdp]->ff_file;
1439 KASSERT(fp != NULL);
1440 mutex_enter(&fp->f_lock);
1441 *--rp = fp;
1442 fp->f_count++;
1443 fp->f_msgcount++;
1444 mutex_exit(&fp->f_lock);
1445 }
1446
1447 out:
1448 /* Release descriptor references. */
1449 fdp = (int *)CMSG_DATA(cm);
1450 for (i = 0; i < nfds; i++) {
1451 fd_putfile(*fdp++);
1452 if (error != 0) {
1453 atomic_dec_uint(&unp_rights);
1454 }
1455 }
1456
1457 if (error == 0) {
1458 if (control->m_flags & M_EXT) {
1459 m_freem(control);
1460 *controlp = control = m_get(M_WAIT, MT_CONTROL);
1461 }
1462 MEXTADD(control, newcm, CMSG_SPACE(nfds * sizeof(file_t *)),
1463 M_MBUF, NULL, NULL);
1464 cm = newcm;
1465 /*
1466 * Adjust message & mbuf to note amount of space
1467 * actually used.
1468 */
1469 cm->cmsg_len = CMSG_LEN(nfds * sizeof(file_t *));
1470 control->m_len = CMSG_SPACE(nfds * sizeof(file_t *));
1471 }
1472
1473 return error;
1474 }
1475
1476 struct mbuf *
1477 unp_addsockcred(struct lwp *l, struct mbuf *control)
1478 {
1479 struct sockcred *sc;
1480 struct mbuf *m;
1481 void *p;
1482
1483 m = sbcreatecontrol1(&p, SOCKCREDSIZE(kauth_cred_ngroups(l->l_cred)),
1484 SCM_CREDS, SOL_SOCKET, M_WAITOK);
1485 if (m == NULL)
1486 return control;
1487
1488 sc = p;
1489 sc->sc_uid = kauth_cred_getuid(l->l_cred);
1490 sc->sc_euid = kauth_cred_geteuid(l->l_cred);
1491 sc->sc_gid = kauth_cred_getgid(l->l_cred);
1492 sc->sc_egid = kauth_cred_getegid(l->l_cred);
1493 sc->sc_ngroups = kauth_cred_ngroups(l->l_cred);
1494
1495 for (int i = 0; i < sc->sc_ngroups; i++)
1496 sc->sc_groups[i] = kauth_cred_group(l->l_cred, i);
1497
1498 return m_add(control, m);
1499 }
1500
1501 /*
1502 * Do a mark-sweep GC of files in the system, to free up any which are
1503 * caught in flight to an about-to-be-closed socket. Additionally,
1504 * process deferred file closures.
1505 */
1506 static void
1507 unp_gc(file_t *dp)
1508 {
1509 extern struct domain unixdomain;
1510 file_t *fp, *np;
1511 struct socket *so, *so1;
1512 u_int i, old, new;
1513 bool didwork;
1514
1515 KASSERT(curlwp == unp_thread_lwp);
1516 KASSERT(mutex_owned(&filelist_lock));
1517
1518 /*
1519 * First, process deferred file closures.
1520 */
1521 while (!SLIST_EMPTY(&unp_thread_discard)) {
1522 fp = SLIST_FIRST(&unp_thread_discard);
1523 KASSERT(fp->f_unpcount > 0);
1524 KASSERT(fp->f_count > 0);
1525 KASSERT(fp->f_msgcount > 0);
1526 KASSERT(fp->f_count >= fp->f_unpcount);
1527 KASSERT(fp->f_count >= fp->f_msgcount);
1528 KASSERT(fp->f_msgcount >= fp->f_unpcount);
1529 SLIST_REMOVE_HEAD(&unp_thread_discard, f_unplist);
1530 i = fp->f_unpcount;
1531 fp->f_unpcount = 0;
1532 mutex_exit(&filelist_lock);
1533 for (; i != 0; i--) {
1534 unp_discard_now(fp);
1535 }
1536 mutex_enter(&filelist_lock);
1537 }
1538
1539 /*
1540 * Clear mark bits. Ensure that we don't consider new files
1541 * entering the file table during this loop (they will not have
1542 * FSCAN set).
1543 */
1544 unp_defer = 0;
1545 LIST_FOREACH(fp, &filehead, f_list) {
1546 for (old = fp->f_flag;; old = new) {
1547 new = atomic_cas_uint(&fp->f_flag, old,
1548 (old | FSCAN) & ~(FMARK|FDEFER));
1549 if (__predict_true(old == new)) {
1550 break;
1551 }
1552 }
1553 }
1554
1555 /*
1556 * Iterate over the set of sockets, marking ones believed (based on
1557 * refcount) to be referenced from a process, and marking for rescan
1558 * sockets which are queued on a socket. Recan continues descending
1559 * and searching for sockets referenced by sockets (FDEFER), until
1560 * there are no more socket->socket references to be discovered.
1561 */
1562 do {
1563 didwork = false;
1564 for (fp = LIST_FIRST(&filehead); fp != NULL; fp = np) {
1565 KASSERT(mutex_owned(&filelist_lock));
1566 np = LIST_NEXT(fp, f_list);
1567 mutex_enter(&fp->f_lock);
1568 if ((fp->f_flag & FDEFER) != 0) {
1569 atomic_and_uint(&fp->f_flag, ~FDEFER);
1570 unp_defer--;
1571 KASSERT(fp->f_count != 0);
1572 } else {
1573 if (fp->f_count == 0 ||
1574 (fp->f_flag & FMARK) != 0 ||
1575 fp->f_count == fp->f_msgcount ||
1576 fp->f_unpcount != 0) {
1577 mutex_exit(&fp->f_lock);
1578 continue;
1579 }
1580 }
1581 atomic_or_uint(&fp->f_flag, FMARK);
1582
1583 if (fp->f_type != DTYPE_SOCKET ||
1584 (so = fp->f_data) == NULL ||
1585 so->so_proto->pr_domain != &unixdomain ||
1586 (so->so_proto->pr_flags & PR_RIGHTS) == 0) {
1587 mutex_exit(&fp->f_lock);
1588 continue;
1589 }
1590
1591 /* Gain file ref, mark our position, and unlock. */
1592 didwork = true;
1593 LIST_INSERT_AFTER(fp, dp, f_list);
1594 fp->f_count++;
1595 mutex_exit(&fp->f_lock);
1596 mutex_exit(&filelist_lock);
1597
1598 /*
1599 * Mark files referenced from sockets queued on the
1600 * accept queue as well.
1601 */
1602 solock(so);
1603 unp_scan(so->so_rcv.sb_mb, unp_mark, 0);
1604 if ((so->so_options & SO_ACCEPTCONN) != 0) {
1605 TAILQ_FOREACH(so1, &so->so_q0, so_qe) {
1606 unp_scan(so1->so_rcv.sb_mb, unp_mark, 0);
1607 }
1608 TAILQ_FOREACH(so1, &so->so_q, so_qe) {
1609 unp_scan(so1->so_rcv.sb_mb, unp_mark, 0);
1610 }
1611 }
1612 sounlock(so);
1613
1614 /* Re-lock and restart from where we left off. */
1615 closef(fp);
1616 mutex_enter(&filelist_lock);
1617 np = LIST_NEXT(dp, f_list);
1618 LIST_REMOVE(dp, f_list);
1619 }
1620 /*
1621 * Bail early if we did nothing in the loop above. Could
1622 * happen because of concurrent activity causing unp_defer
1623 * to get out of sync.
1624 */
1625 } while (unp_defer != 0 && didwork);
1626
1627 /*
1628 * Sweep pass.
1629 *
1630 * We grab an extra reference to each of the files that are
1631 * not otherwise accessible and then free the rights that are
1632 * stored in messages on them.
1633 */
1634 for (fp = LIST_FIRST(&filehead); fp != NULL; fp = np) {
1635 KASSERT(mutex_owned(&filelist_lock));
1636 np = LIST_NEXT(fp, f_list);
1637 mutex_enter(&fp->f_lock);
1638
1639 /*
1640 * Ignore non-sockets.
1641 * Ignore dead sockets, or sockets with pending close.
1642 * Ignore sockets obviously referenced elsewhere.
1643 * Ignore sockets marked as referenced by our scan.
1644 * Ignore new sockets that did not exist during the scan.
1645 */
1646 if (fp->f_type != DTYPE_SOCKET ||
1647 fp->f_count == 0 || fp->f_unpcount != 0 ||
1648 fp->f_count != fp->f_msgcount ||
1649 (fp->f_flag & (FMARK | FSCAN)) != FSCAN) {
1650 mutex_exit(&fp->f_lock);
1651 continue;
1652 }
1653
1654 /* Gain file ref, mark our position, and unlock. */
1655 LIST_INSERT_AFTER(fp, dp, f_list);
1656 fp->f_count++;
1657 mutex_exit(&fp->f_lock);
1658 mutex_exit(&filelist_lock);
1659
1660 /*
1661 * Flush all data from the socket's receive buffer.
1662 * This will cause files referenced only by the
1663 * socket to be queued for close.
1664 */
1665 so = fp->f_data;
1666 solock(so);
1667 sorflush(so);
1668 sounlock(so);
1669
1670 /* Re-lock and restart from where we left off. */
1671 closef(fp);
1672 mutex_enter(&filelist_lock);
1673 np = LIST_NEXT(dp, f_list);
1674 LIST_REMOVE(dp, f_list);
1675 }
1676 }
1677
1678 /*
1679 * Garbage collector thread. While SCM_RIGHTS messages are in transit,
1680 * wake once per second to garbage collect. Run continually while we
1681 * have deferred closes to process.
1682 */
1683 static void
1684 unp_thread(void *cookie)
1685 {
1686 file_t *dp;
1687
1688 /* Allocate a dummy file for our scans. */
1689 if ((dp = fgetdummy()) == NULL) {
1690 panic("unp_thread");
1691 }
1692
1693 mutex_enter(&filelist_lock);
1694 for (;;) {
1695 KASSERT(mutex_owned(&filelist_lock));
1696 if (SLIST_EMPTY(&unp_thread_discard)) {
1697 if (unp_rights != 0) {
1698 (void)cv_timedwait(&unp_thread_cv,
1699 &filelist_lock, hz);
1700 } else {
1701 cv_wait(&unp_thread_cv, &filelist_lock);
1702 }
1703 }
1704 unp_gc(dp);
1705 }
1706 /* NOTREACHED */
1707 }
1708
1709 /*
1710 * Kick the garbage collector into action if there is something for
1711 * it to process.
1712 */
1713 static void
1714 unp_thread_kick(void)
1715 {
1716
1717 if (!SLIST_EMPTY(&unp_thread_discard) || unp_rights != 0) {
1718 mutex_enter(&filelist_lock);
1719 cv_signal(&unp_thread_cv);
1720 mutex_exit(&filelist_lock);
1721 }
1722 }
1723
1724 void
1725 unp_dispose(struct mbuf *m)
1726 {
1727
1728 if (m)
1729 unp_scan(m, unp_discard_later, 1);
1730 }
1731
1732 void
1733 unp_scan(struct mbuf *m0, void (*op)(file_t *), int discard)
1734 {
1735 struct mbuf *m;
1736 file_t **rp, *fp;
1737 struct cmsghdr *cm;
1738 int i, qfds;
1739
1740 while (m0) {
1741 for (m = m0; m; m = m->m_next) {
1742 if (m->m_type != MT_CONTROL ||
1743 m->m_len < sizeof(*cm)) {
1744 continue;
1745 }
1746 cm = mtod(m, struct cmsghdr *);
1747 if (cm->cmsg_level != SOL_SOCKET ||
1748 cm->cmsg_type != SCM_RIGHTS)
1749 continue;
1750 qfds = (cm->cmsg_len - CMSG_ALIGN(sizeof(*cm)))
1751 / sizeof(file_t *);
1752 rp = (file_t **)CMSG_DATA(cm);
1753 for (i = 0; i < qfds; i++) {
1754 fp = *rp;
1755 if (discard) {
1756 *rp = 0;
1757 }
1758 (*op)(fp);
1759 rp++;
1760 }
1761 }
1762 m0 = m0->m_nextpkt;
1763 }
1764 }
1765
1766 void
1767 unp_mark(file_t *fp)
1768 {
1769
1770 if (fp == NULL)
1771 return;
1772
1773 /* If we're already deferred, don't screw up the defer count */
1774 mutex_enter(&fp->f_lock);
1775 if (fp->f_flag & (FMARK | FDEFER)) {
1776 mutex_exit(&fp->f_lock);
1777 return;
1778 }
1779
1780 /*
1781 * Minimize the number of deferrals... Sockets are the only type of
1782 * file which can hold references to another file, so just mark
1783 * other files, and defer unmarked sockets for the next pass.
1784 */
1785 if (fp->f_type == DTYPE_SOCKET) {
1786 unp_defer++;
1787 KASSERT(fp->f_count != 0);
1788 atomic_or_uint(&fp->f_flag, FDEFER);
1789 } else {
1790 atomic_or_uint(&fp->f_flag, FMARK);
1791 }
1792 mutex_exit(&fp->f_lock);
1793 }
1794
1795 static void
1796 unp_discard_now(file_t *fp)
1797 {
1798
1799 if (fp == NULL)
1800 return;
1801
1802 KASSERT(fp->f_count > 0);
1803 KASSERT(fp->f_msgcount > 0);
1804
1805 mutex_enter(&fp->f_lock);
1806 fp->f_msgcount--;
1807 mutex_exit(&fp->f_lock);
1808 atomic_dec_uint(&unp_rights);
1809 (void)closef(fp);
1810 }
1811
1812 static void
1813 unp_discard_later(file_t *fp)
1814 {
1815
1816 if (fp == NULL)
1817 return;
1818
1819 KASSERT(fp->f_count > 0);
1820 KASSERT(fp->f_msgcount > 0);
1821
1822 mutex_enter(&filelist_lock);
1823 if (fp->f_unpcount++ == 0) {
1824 SLIST_INSERT_HEAD(&unp_thread_discard, fp, f_unplist);
1825 }
1826 mutex_exit(&filelist_lock);
1827 }
1828