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