]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/uipc_usrreq.c
MFC
[FreeBSD/FreeBSD.git] / sys / kern / uipc_usrreq.c
1 /*-
2  * Copyright (c) 1982, 1986, 1989, 1991, 1993
3  *      The Regents of the University of California.
4  * Copyright (c) 2004-2009 Robert N. M. Watson
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 4. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *      From: @(#)uipc_usrreq.c 8.3 (Berkeley) 1/4/94
32  */
33
34 /*
35  * UNIX Domain (Local) Sockets
36  *
37  * This is an implementation of UNIX (local) domain sockets.  Each socket has
38  * an associated struct unpcb (UNIX protocol control block).  Stream sockets
39  * may be connected to 0 or 1 other socket.  Datagram sockets may be
40  * connected to 0, 1, or many other sockets.  Sockets may be created and
41  * connected in pairs (socketpair(2)), or bound/connected to using the file
42  * system name space.  For most purposes, only the receive socket buffer is
43  * used, as sending on one socket delivers directly to the receive socket
44  * buffer of a second socket.
45  *
46  * The implementation is substantially complicated by the fact that
47  * "ancillary data", such as file descriptors or credentials, may be passed
48  * across UNIX domain sockets.  The potential for passing UNIX domain sockets
49  * over other UNIX domain sockets requires the implementation of a simple
50  * garbage collector to find and tear down cycles of disconnected sockets.
51  *
52  * TODO:
53  *      RDM
54  *      distinguish datagram size limits from flow control limits in SEQPACKET
55  *      rethink name space problems
56  *      need a proper out-of-band
57  */
58
59 #include <sys/cdefs.h>
60 __FBSDID("$FreeBSD$");
61
62 #include "opt_ddb.h"
63
64 #include <sys/param.h>
65 #include <sys/capability.h>
66 #include <sys/domain.h>
67 #include <sys/fcntl.h>
68 #include <sys/malloc.h>         /* XXX must be before <sys/file.h> */
69 #include <sys/eventhandler.h>
70 #include <sys/file.h>
71 #include <sys/filedesc.h>
72 #include <sys/kernel.h>
73 #include <sys/lock.h>
74 #include <sys/mbuf.h>
75 #include <sys/mount.h>
76 #include <sys/mutex.h>
77 #include <sys/namei.h>
78 #include <sys/proc.h>
79 #include <sys/protosw.h>
80 #include <sys/queue.h>
81 #include <sys/resourcevar.h>
82 #include <sys/rwlock.h>
83 #include <sys/socket.h>
84 #include <sys/socketvar.h>
85 #include <sys/signalvar.h>
86 #include <sys/stat.h>
87 #include <sys/sx.h>
88 #include <sys/sysctl.h>
89 #include <sys/systm.h>
90 #include <sys/taskqueue.h>
91 #include <sys/un.h>
92 #include <sys/unpcb.h>
93 #include <sys/vnode.h>
94
95 #include <net/vnet.h>
96
97 #ifdef DDB
98 #include <ddb/ddb.h>
99 #endif
100
101 #include <security/mac/mac_framework.h>
102
103 #include <vm/uma.h>
104
105 /*
106  * Locking key:
107  * (l)  Locked using list lock
108  * (g)  Locked using linkage lock
109  */
110
111 static uma_zone_t       unp_zone;
112 static unp_gen_t        unp_gencnt;     /* (l) */
113 static u_int            unp_count;      /* (l) Count of local sockets. */
114 static ino_t            unp_ino;        /* Prototype for fake inode numbers. */
115 static int              unp_rights;     /* (g) File descriptors in flight. */
116 static struct unp_head  unp_shead;      /* (l) List of stream sockets. */
117 static struct unp_head  unp_dhead;      /* (l) List of datagram sockets. */
118 static struct unp_head  unp_sphead;     /* (l) List of seqpacket sockets. */
119
120 struct unp_defer {
121         SLIST_ENTRY(unp_defer) ud_link;
122         struct file *ud_fp;
123 };
124 static SLIST_HEAD(, unp_defer) unp_defers;
125 static int unp_defers_count;
126
127 static const struct sockaddr    sun_noname = { sizeof(sun_noname), AF_LOCAL };
128
129 /*
130  * Garbage collection of cyclic file descriptor/socket references occurs
131  * asynchronously in a taskqueue context in order to avoid recursion and
132  * reentrance in the UNIX domain socket, file descriptor, and socket layer
133  * code.  See unp_gc() for a full description.
134  */
135 static struct timeout_task unp_gc_task;
136
137 /*
138  * The close of unix domain sockets attached as SCM_RIGHTS is
139  * postponed to the taskqueue, to avoid arbitrary recursion depth.
140  * The attached sockets might have another sockets attached.
141  */
142 static struct task      unp_defer_task;
143
144 /*
145  * Both send and receive buffers are allocated PIPSIZ bytes of buffering for
146  * stream sockets, although the total for sender and receiver is actually
147  * only PIPSIZ.
148  *
149  * Datagram sockets really use the sendspace as the maximum datagram size,
150  * and don't really want to reserve the sendspace.  Their recvspace should be
151  * large enough for at least one max-size datagram plus address.
152  */
153 #ifndef PIPSIZ
154 #define PIPSIZ  8192
155 #endif
156 static u_long   unpst_sendspace = PIPSIZ;
157 static u_long   unpst_recvspace = PIPSIZ;
158 static u_long   unpdg_sendspace = 2*1024;       /* really max datagram size */
159 static u_long   unpdg_recvspace = 4*1024;
160 static u_long   unpsp_sendspace = PIPSIZ;       /* really max datagram size */
161 static u_long   unpsp_recvspace = PIPSIZ;
162
163 static SYSCTL_NODE(_net, PF_LOCAL, local, CTLFLAG_RW, 0, "Local domain");
164 static SYSCTL_NODE(_net_local, SOCK_STREAM, stream, CTLFLAG_RW, 0,
165     "SOCK_STREAM");
166 static SYSCTL_NODE(_net_local, SOCK_DGRAM, dgram, CTLFLAG_RW, 0, "SOCK_DGRAM");
167 static SYSCTL_NODE(_net_local, SOCK_SEQPACKET, seqpacket, CTLFLAG_RW, 0,
168     "SOCK_SEQPACKET");
169
170 SYSCTL_ULONG(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW,
171            &unpst_sendspace, 0, "Default stream send space.");
172 SYSCTL_ULONG(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW,
173            &unpst_recvspace, 0, "Default stream receive space.");
174 SYSCTL_ULONG(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW,
175            &unpdg_sendspace, 0, "Default datagram send space.");
176 SYSCTL_ULONG(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW,
177            &unpdg_recvspace, 0, "Default datagram receive space.");
178 SYSCTL_ULONG(_net_local_seqpacket, OID_AUTO, maxseqpacket, CTLFLAG_RW,
179            &unpsp_sendspace, 0, "Default seqpacket send space.");
180 SYSCTL_ULONG(_net_local_seqpacket, OID_AUTO, recvspace, CTLFLAG_RW,
181            &unpsp_recvspace, 0, "Default seqpacket receive space.");
182 SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0,
183     "File descriptors in flight.");
184 SYSCTL_INT(_net_local, OID_AUTO, deferred, CTLFLAG_RD,
185     &unp_defers_count, 0,
186     "File descriptors deferred to taskqueue for close.");
187
188 /*
189  * Locking and synchronization:
190  *
191  * Three types of locks exit in the local domain socket implementation: a
192  * global list mutex, a global linkage rwlock, and per-unpcb mutexes.  Of the
193  * global locks, the list lock protects the socket count, global generation
194  * number, and stream/datagram global lists.  The linkage lock protects the
195  * interconnection of unpcbs, the v_socket and unp_vnode pointers, and can be
196  * held exclusively over the acquisition of multiple unpcb locks to prevent
197  * deadlock.
198  *
199  * UNIX domain sockets each have an unpcb hung off of their so_pcb pointer,
200  * allocated in pru_attach() and freed in pru_detach().  The validity of that
201  * pointer is an invariant, so no lock is required to dereference the so_pcb
202  * pointer if a valid socket reference is held by the caller.  In practice,
203  * this is always true during operations performed on a socket.  Each unpcb
204  * has a back-pointer to its socket, unp_socket, which will be stable under
205  * the same circumstances.
206  *
207  * This pointer may only be safely dereferenced as long as a valid reference
208  * to the unpcb is held.  Typically, this reference will be from the socket,
209  * or from another unpcb when the referring unpcb's lock is held (in order
210  * that the reference not be invalidated during use).  For example, to follow
211  * unp->unp_conn->unp_socket, you need unlock the lock on unp, not unp_conn,
212  * as unp_socket remains valid as long as the reference to unp_conn is valid.
213  *
214  * Fields of unpcbss are locked using a per-unpcb lock, unp_mtx.  Individual
215  * atomic reads without the lock may be performed "lockless", but more
216  * complex reads and read-modify-writes require the mutex to be held.  No
217  * lock order is defined between unpcb locks -- multiple unpcb locks may be
218  * acquired at the same time only when holding the linkage rwlock
219  * exclusively, which prevents deadlocks.
220  *
221  * Blocking with UNIX domain sockets is a tricky issue: unlike most network
222  * protocols, bind() is a non-atomic operation, and connect() requires
223  * potential sleeping in the protocol, due to potentially waiting on local or
224  * distributed file systems.  We try to separate "lookup" operations, which
225  * may sleep, and the IPC operations themselves, which typically can occur
226  * with relative atomicity as locks can be held over the entire operation.
227  *
228  * Another tricky issue is simultaneous multi-threaded or multi-process
229  * access to a single UNIX domain socket.  These are handled by the flags
230  * UNP_CONNECTING and UNP_BINDING, which prevent concurrent connecting or
231  * binding, both of which involve dropping UNIX domain socket locks in order
232  * to perform namei() and other file system operations.
233  */
234 static struct rwlock    unp_link_rwlock;
235 static struct mtx       unp_list_lock;
236 static struct mtx       unp_defers_lock;
237
238 #define UNP_LINK_LOCK_INIT()            rw_init(&unp_link_rwlock,       \
239                                             "unp_link_rwlock")
240
241 #define UNP_LINK_LOCK_ASSERT()  rw_assert(&unp_link_rwlock,     \
242                                             RA_LOCKED)
243 #define UNP_LINK_UNLOCK_ASSERT()        rw_assert(&unp_link_rwlock,     \
244                                             RA_UNLOCKED)
245
246 #define UNP_LINK_RLOCK()                rw_rlock(&unp_link_rwlock)
247 #define UNP_LINK_RUNLOCK()              rw_runlock(&unp_link_rwlock)
248 #define UNP_LINK_WLOCK()                rw_wlock(&unp_link_rwlock)
249 #define UNP_LINK_WUNLOCK()              rw_wunlock(&unp_link_rwlock)
250 #define UNP_LINK_WLOCK_ASSERT()         rw_assert(&unp_link_rwlock,     \
251                                             RA_WLOCKED)
252
253 #define UNP_LIST_LOCK_INIT()            mtx_init(&unp_list_lock,        \
254                                             "unp_list_lock", NULL, MTX_DEF)
255 #define UNP_LIST_LOCK()                 mtx_lock(&unp_list_lock)
256 #define UNP_LIST_UNLOCK()               mtx_unlock(&unp_list_lock)
257
258 #define UNP_DEFERRED_LOCK_INIT()        mtx_init(&unp_defers_lock, \
259                                             "unp_defer", NULL, MTX_DEF)
260 #define UNP_DEFERRED_LOCK()             mtx_lock(&unp_defers_lock)
261 #define UNP_DEFERRED_UNLOCK()           mtx_unlock(&unp_defers_lock)
262
263 #define UNP_PCB_LOCK_INIT(unp)          mtx_init(&(unp)->unp_mtx,       \
264                                             "unp_mtx", "unp_mtx",       \
265                                             MTX_DUPOK|MTX_DEF|MTX_RECURSE)
266 #define UNP_PCB_LOCK_DESTROY(unp)       mtx_destroy(&(unp)->unp_mtx)
267 #define UNP_PCB_LOCK(unp)               mtx_lock(&(unp)->unp_mtx)
268 #define UNP_PCB_UNLOCK(unp)             mtx_unlock(&(unp)->unp_mtx)
269 #define UNP_PCB_LOCK_ASSERT(unp)        mtx_assert(&(unp)->unp_mtx, MA_OWNED)
270
271 static int      uipc_connect2(struct socket *, struct socket *);
272 static int      uipc_ctloutput(struct socket *, struct sockopt *);
273 static int      unp_connect(struct socket *, struct sockaddr *,
274                     struct thread *);
275 static int      unp_connectat(int, struct socket *, struct sockaddr *,
276                     struct thread *);
277 static int      unp_connect2(struct socket *so, struct socket *so2, int);
278 static void     unp_disconnect(struct unpcb *unp, struct unpcb *unp2);
279 static void     unp_dispose(struct mbuf *);
280 static void     unp_shutdown(struct unpcb *);
281 static void     unp_drop(struct unpcb *, int);
282 static void     unp_gc(__unused void *, int);
283 static void     unp_scan(struct mbuf *, void (*)(struct file *));
284 static void     unp_discard(struct file *);
285 static void     unp_freerights(struct filedescent *, int);
286 static void     unp_init(void);
287 static int      unp_internalize(struct mbuf **, struct thread *);
288 static void     unp_internalize_fp(struct file *);
289 static int      unp_externalize(struct mbuf *, struct mbuf **);
290 static int      unp_externalize_fp(struct file *);
291 static struct mbuf      *unp_addsockcred(struct thread *, struct mbuf *);
292 static void     unp_process_defers(void * __unused, int);
293
294 /*
295  * Definitions of protocols supported in the LOCAL domain.
296  */
297 static struct domain localdomain;
298 static struct pr_usrreqs uipc_usrreqs_dgram, uipc_usrreqs_stream;
299 static struct pr_usrreqs uipc_usrreqs_seqpacket;
300 static struct protosw localsw[] = {
301 {
302         .pr_type =              SOCK_STREAM,
303         .pr_domain =            &localdomain,
304         .pr_flags =             PR_CONNREQUIRED|PR_WANTRCVD|PR_RIGHTS,
305         .pr_ctloutput =         &uipc_ctloutput,
306         .pr_usrreqs =           &uipc_usrreqs_stream
307 },
308 {
309         .pr_type =              SOCK_DGRAM,
310         .pr_domain =            &localdomain,
311         .pr_flags =             PR_ATOMIC|PR_ADDR|PR_RIGHTS,
312         .pr_ctloutput =         &uipc_ctloutput,
313         .pr_usrreqs =           &uipc_usrreqs_dgram
314 },
315 {
316         .pr_type =              SOCK_SEQPACKET,
317         .pr_domain =            &localdomain,
318
319         /*
320          * XXXRW: For now, PR_ADDR because soreceive will bump into them
321          * due to our use of sbappendaddr.  A new sbappend variants is needed
322          * that supports both atomic record writes and control data.
323          */
324         .pr_flags =             PR_ADDR|PR_ATOMIC|PR_CONNREQUIRED|PR_WANTRCVD|
325                                     PR_RIGHTS,
326         .pr_usrreqs =           &uipc_usrreqs_seqpacket,
327 },
328 };
329
330 static struct domain localdomain = {
331         .dom_family =           AF_LOCAL,
332         .dom_name =             "local",
333         .dom_init =             unp_init,
334         .dom_externalize =      unp_externalize,
335         .dom_dispose =          unp_dispose,
336         .dom_protosw =          localsw,
337         .dom_protoswNPROTOSW =  &localsw[sizeof(localsw)/sizeof(localsw[0])]
338 };
339 DOMAIN_SET(local);
340
341 static void
342 uipc_abort(struct socket *so)
343 {
344         struct unpcb *unp, *unp2;
345
346         unp = sotounpcb(so);
347         KASSERT(unp != NULL, ("uipc_abort: unp == NULL"));
348
349         UNP_LINK_WLOCK();
350         UNP_PCB_LOCK(unp);
351         unp2 = unp->unp_conn;
352         if (unp2 != NULL) {
353                 UNP_PCB_LOCK(unp2);
354                 unp_drop(unp2, ECONNABORTED);
355                 UNP_PCB_UNLOCK(unp2);
356         }
357         UNP_PCB_UNLOCK(unp);
358         UNP_LINK_WUNLOCK();
359 }
360
361 static int
362 uipc_accept(struct socket *so, struct sockaddr **nam)
363 {
364         struct unpcb *unp, *unp2;
365         const struct sockaddr *sa;
366
367         /*
368          * Pass back name of connected socket, if it was bound and we are
369          * still connected (our peer may have closed already!).
370          */
371         unp = sotounpcb(so);
372         KASSERT(unp != NULL, ("uipc_accept: unp == NULL"));
373
374         *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK);
375         UNP_LINK_RLOCK();
376         unp2 = unp->unp_conn;
377         if (unp2 != NULL && unp2->unp_addr != NULL) {
378                 UNP_PCB_LOCK(unp2);
379                 sa = (struct sockaddr *) unp2->unp_addr;
380                 bcopy(sa, *nam, sa->sa_len);
381                 UNP_PCB_UNLOCK(unp2);
382         } else {
383                 sa = &sun_noname;
384                 bcopy(sa, *nam, sa->sa_len);
385         }
386         UNP_LINK_RUNLOCK();
387         return (0);
388 }
389
390 static int
391 uipc_attach(struct socket *so, int proto, struct thread *td)
392 {
393         u_long sendspace, recvspace;
394         struct unpcb *unp;
395         int error;
396
397         KASSERT(so->so_pcb == NULL, ("uipc_attach: so_pcb != NULL"));
398         if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
399                 switch (so->so_type) {
400                 case SOCK_STREAM:
401                         sendspace = unpst_sendspace;
402                         recvspace = unpst_recvspace;
403                         break;
404
405                 case SOCK_DGRAM:
406                         sendspace = unpdg_sendspace;
407                         recvspace = unpdg_recvspace;
408                         break;
409
410                 case SOCK_SEQPACKET:
411                         sendspace = unpsp_sendspace;
412                         recvspace = unpsp_recvspace;
413                         break;
414
415                 default:
416                         panic("uipc_attach");
417                 }
418                 error = soreserve(so, sendspace, recvspace);
419                 if (error)
420                         return (error);
421         }
422         unp = uma_zalloc(unp_zone, M_NOWAIT | M_ZERO);
423         if (unp == NULL)
424                 return (ENOBUFS);
425         LIST_INIT(&unp->unp_refs);
426         UNP_PCB_LOCK_INIT(unp);
427         unp->unp_socket = so;
428         so->so_pcb = unp;
429         unp->unp_refcount = 1;
430
431         UNP_LIST_LOCK();
432         unp->unp_gencnt = ++unp_gencnt;
433         unp_count++;
434         switch (so->so_type) {
435         case SOCK_STREAM:
436                 LIST_INSERT_HEAD(&unp_shead, unp, unp_link);
437                 break;
438
439         case SOCK_DGRAM:
440                 LIST_INSERT_HEAD(&unp_dhead, unp, unp_link);
441                 break;
442
443         case SOCK_SEQPACKET:
444                 LIST_INSERT_HEAD(&unp_sphead, unp, unp_link);
445                 break;
446
447         default:
448                 panic("uipc_attach");
449         }
450         UNP_LIST_UNLOCK();
451
452         return (0);
453 }
454
455 static int
456 uipc_bindat(int fd, struct socket *so, struct sockaddr *nam, struct thread *td)
457 {
458         struct sockaddr_un *soun = (struct sockaddr_un *)nam;
459         struct vattr vattr;
460         int error, namelen;
461         struct nameidata nd;
462         struct unpcb *unp;
463         struct vnode *vp;
464         struct mount *mp;
465         char *buf;
466
467         unp = sotounpcb(so);
468         KASSERT(unp != NULL, ("uipc_bind: unp == NULL"));
469
470         if (soun->sun_len > sizeof(struct sockaddr_un))
471                 return (EINVAL);
472         namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path);
473         if (namelen <= 0)
474                 return (EINVAL);
475
476         /*
477          * We don't allow simultaneous bind() calls on a single UNIX domain
478          * socket, so flag in-progress operations, and return an error if an
479          * operation is already in progress.
480          *
481          * Historically, we have not allowed a socket to be rebound, so this
482          * also returns an error.  Not allowing re-binding simplifies the
483          * implementation and avoids a great many possible failure modes.
484          */
485         UNP_PCB_LOCK(unp);
486         if (unp->unp_vnode != NULL) {
487                 UNP_PCB_UNLOCK(unp);
488                 return (EINVAL);
489         }
490         if (unp->unp_flags & UNP_BINDING) {
491                 UNP_PCB_UNLOCK(unp);
492                 return (EALREADY);
493         }
494         unp->unp_flags |= UNP_BINDING;
495         UNP_PCB_UNLOCK(unp);
496
497         buf = malloc(namelen + 1, M_TEMP, M_WAITOK);
498         bcopy(soun->sun_path, buf, namelen);
499         buf[namelen] = 0;
500
501 restart:
502         NDINIT_ATRIGHTS(&nd, CREATE, NOFOLLOW | LOCKPARENT | SAVENAME,
503             UIO_SYSSPACE, buf, fd, CAP_BINDAT, td);
504 /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
505         error = namei(&nd);
506         if (error)
507                 goto error;
508         vp = nd.ni_vp;
509         if (vp != NULL || vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
510                 NDFREE(&nd, NDF_ONLY_PNBUF);
511                 if (nd.ni_dvp == vp)
512                         vrele(nd.ni_dvp);
513                 else
514                         vput(nd.ni_dvp);
515                 if (vp != NULL) {
516                         vrele(vp);
517                         error = EADDRINUSE;
518                         goto error;
519                 }
520                 error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
521                 if (error)
522                         goto error;
523                 goto restart;
524         }
525         VATTR_NULL(&vattr);
526         vattr.va_type = VSOCK;
527         vattr.va_mode = (ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask);
528 #ifdef MAC
529         error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
530             &vattr);
531 #endif
532         if (error == 0)
533                 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
534         NDFREE(&nd, NDF_ONLY_PNBUF);
535         vput(nd.ni_dvp);
536         if (error) {
537                 vn_finished_write(mp);
538                 goto error;
539         }
540         vp = nd.ni_vp;
541         ASSERT_VOP_ELOCKED(vp, "uipc_bind");
542         soun = (struct sockaddr_un *)sodupsockaddr(nam, M_WAITOK);
543
544         UNP_LINK_WLOCK();
545         UNP_PCB_LOCK(unp);
546         VOP_UNP_BIND(vp, unp->unp_socket);
547         unp->unp_vnode = vp;
548         unp->unp_addr = soun;
549         unp->unp_flags &= ~UNP_BINDING;
550         UNP_PCB_UNLOCK(unp);
551         UNP_LINK_WUNLOCK();
552         VOP_UNLOCK(vp, 0);
553         vn_finished_write(mp);
554         free(buf, M_TEMP);
555         return (0);
556
557 error:
558         UNP_PCB_LOCK(unp);
559         unp->unp_flags &= ~UNP_BINDING;
560         UNP_PCB_UNLOCK(unp);
561         free(buf, M_TEMP);
562         return (error);
563 }
564
565 static int
566 uipc_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
567 {
568
569         return (uipc_bindat(AT_FDCWD, so, nam, td));
570 }
571
572 static int
573 uipc_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
574 {
575         int error;
576
577         KASSERT(td == curthread, ("uipc_connect: td != curthread"));
578         UNP_LINK_WLOCK();
579         error = unp_connect(so, nam, td);
580         UNP_LINK_WUNLOCK();
581         return (error);
582 }
583
584 static int
585 uipc_connectat(int fd, struct socket *so, struct sockaddr *nam,
586     struct thread *td)
587 {
588         int error;
589
590         KASSERT(td == curthread, ("uipc_connectat: td != curthread"));
591         UNP_LINK_WLOCK();
592         error = unp_connectat(fd, so, nam, td);
593         UNP_LINK_WUNLOCK();
594         return (error);
595 }
596
597 static void
598 uipc_close(struct socket *so)
599 {
600         struct unpcb *unp, *unp2;
601
602         unp = sotounpcb(so);
603         KASSERT(unp != NULL, ("uipc_close: unp == NULL"));
604
605         UNP_LINK_WLOCK();
606         UNP_PCB_LOCK(unp);
607         unp2 = unp->unp_conn;
608         if (unp2 != NULL) {
609                 UNP_PCB_LOCK(unp2);
610                 unp_disconnect(unp, unp2);
611                 UNP_PCB_UNLOCK(unp2);
612         }
613         UNP_PCB_UNLOCK(unp);
614         UNP_LINK_WUNLOCK();
615 }
616
617 static int
618 uipc_connect2(struct socket *so1, struct socket *so2)
619 {
620         struct unpcb *unp, *unp2;
621         int error;
622
623         UNP_LINK_WLOCK();
624         unp = so1->so_pcb;
625         KASSERT(unp != NULL, ("uipc_connect2: unp == NULL"));
626         UNP_PCB_LOCK(unp);
627         unp2 = so2->so_pcb;
628         KASSERT(unp2 != NULL, ("uipc_connect2: unp2 == NULL"));
629         UNP_PCB_LOCK(unp2);
630         error = unp_connect2(so1, so2, PRU_CONNECT2);
631         UNP_PCB_UNLOCK(unp2);
632         UNP_PCB_UNLOCK(unp);
633         UNP_LINK_WUNLOCK();
634         return (error);
635 }
636
637 static void
638 uipc_detach(struct socket *so)
639 {
640         struct unpcb *unp, *unp2;
641         struct sockaddr_un *saved_unp_addr;
642         struct vnode *vp;
643         int freeunp, local_unp_rights;
644
645         unp = sotounpcb(so);
646         KASSERT(unp != NULL, ("uipc_detach: unp == NULL"));
647
648         UNP_LINK_WLOCK();
649         UNP_LIST_LOCK();
650         UNP_PCB_LOCK(unp);
651         LIST_REMOVE(unp, unp_link);
652         unp->unp_gencnt = ++unp_gencnt;
653         --unp_count;
654         UNP_LIST_UNLOCK();
655
656         /*
657          * XXXRW: Should assert vp->v_socket == so.
658          */
659         if ((vp = unp->unp_vnode) != NULL) {
660                 VOP_UNP_DETACH(vp);
661                 unp->unp_vnode = NULL;
662         }
663         unp2 = unp->unp_conn;
664         if (unp2 != NULL) {
665                 UNP_PCB_LOCK(unp2);
666                 unp_disconnect(unp, unp2);
667                 UNP_PCB_UNLOCK(unp2);
668         }
669
670         /*
671          * We hold the linkage lock exclusively, so it's OK to acquire
672          * multiple pcb locks at a time.
673          */
674         while (!LIST_EMPTY(&unp->unp_refs)) {
675                 struct unpcb *ref = LIST_FIRST(&unp->unp_refs);
676
677                 UNP_PCB_LOCK(ref);
678                 unp_drop(ref, ECONNRESET);
679                 UNP_PCB_UNLOCK(ref);
680         }
681         local_unp_rights = unp_rights;
682         UNP_LINK_WUNLOCK();
683         unp->unp_socket->so_pcb = NULL;
684         saved_unp_addr = unp->unp_addr;
685         unp->unp_addr = NULL;
686         unp->unp_refcount--;
687         freeunp = (unp->unp_refcount == 0);
688         if (saved_unp_addr != NULL)
689                 free(saved_unp_addr, M_SONAME);
690         if (freeunp) {
691                 UNP_PCB_LOCK_DESTROY(unp);
692                 uma_zfree(unp_zone, unp);
693         } else
694                 UNP_PCB_UNLOCK(unp);
695         if (vp)
696                 vrele(vp);
697         if (local_unp_rights)
698                 taskqueue_enqueue_timeout(taskqueue_thread, &unp_gc_task, -1);
699 }
700
701 static int
702 uipc_disconnect(struct socket *so)
703 {
704         struct unpcb *unp, *unp2;
705
706         unp = sotounpcb(so);
707         KASSERT(unp != NULL, ("uipc_disconnect: unp == NULL"));
708
709         UNP_LINK_WLOCK();
710         UNP_PCB_LOCK(unp);
711         unp2 = unp->unp_conn;
712         if (unp2 != NULL) {
713                 UNP_PCB_LOCK(unp2);
714                 unp_disconnect(unp, unp2);
715                 UNP_PCB_UNLOCK(unp2);
716         }
717         UNP_PCB_UNLOCK(unp);
718         UNP_LINK_WUNLOCK();
719         return (0);
720 }
721
722 static int
723 uipc_listen(struct socket *so, int backlog, struct thread *td)
724 {
725         struct unpcb *unp;
726         int error;
727
728         unp = sotounpcb(so);
729         KASSERT(unp != NULL, ("uipc_listen: unp == NULL"));
730
731         UNP_PCB_LOCK(unp);
732         if (unp->unp_vnode == NULL) {
733                 UNP_PCB_UNLOCK(unp);
734                 return (EINVAL);
735         }
736
737         SOCK_LOCK(so);
738         error = solisten_proto_check(so);
739         if (error == 0) {
740                 cru2x(td->td_ucred, &unp->unp_peercred);
741                 unp->unp_flags |= UNP_HAVEPCCACHED;
742                 solisten_proto(so, backlog);
743         }
744         SOCK_UNLOCK(so);
745         UNP_PCB_UNLOCK(unp);
746         return (error);
747 }
748
749 static int
750 uipc_peeraddr(struct socket *so, struct sockaddr **nam)
751 {
752         struct unpcb *unp, *unp2;
753         const struct sockaddr *sa;
754
755         unp = sotounpcb(so);
756         KASSERT(unp != NULL, ("uipc_peeraddr: unp == NULL"));
757
758         *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK);
759         UNP_LINK_RLOCK();
760         /*
761          * XXX: It seems that this test always fails even when connection is
762          * established.  So, this else clause is added as workaround to
763          * return PF_LOCAL sockaddr.
764          */
765         unp2 = unp->unp_conn;
766         if (unp2 != NULL) {
767                 UNP_PCB_LOCK(unp2);
768                 if (unp2->unp_addr != NULL)
769                         sa = (struct sockaddr *) unp2->unp_addr;
770                 else
771                         sa = &sun_noname;
772                 bcopy(sa, *nam, sa->sa_len);
773                 UNP_PCB_UNLOCK(unp2);
774         } else {
775                 sa = &sun_noname;
776                 bcopy(sa, *nam, sa->sa_len);
777         }
778         UNP_LINK_RUNLOCK();
779         return (0);
780 }
781
782 static int
783 uipc_rcvd(struct socket *so, int flags)
784 {
785         struct unpcb *unp, *unp2;
786         struct socket *so2;
787         u_int mbcnt, sbcc;
788         u_long newhiwat;
789
790         unp = sotounpcb(so);
791         KASSERT(unp != NULL, ("uipc_rcvd: unp == NULL"));
792
793         if (so->so_type != SOCK_STREAM && so->so_type != SOCK_SEQPACKET)
794                 panic("uipc_rcvd socktype %d", so->so_type);
795
796         /*
797          * Adjust backpressure on sender and wakeup any waiting to write.
798          *
799          * The unp lock is acquired to maintain the validity of the unp_conn
800          * pointer; no lock on unp2 is required as unp2->unp_socket will be
801          * static as long as we don't permit unp2 to disconnect from unp,
802          * which is prevented by the lock on unp.  We cache values from
803          * so_rcv to avoid holding the so_rcv lock over the entire
804          * transaction on the remote so_snd.
805          */
806         SOCKBUF_LOCK(&so->so_rcv);
807         mbcnt = so->so_rcv.sb_mbcnt;
808         sbcc = so->so_rcv.sb_cc;
809         SOCKBUF_UNLOCK(&so->so_rcv);
810         UNP_PCB_LOCK(unp);
811         unp2 = unp->unp_conn;
812         if (unp2 == NULL) {
813                 UNP_PCB_UNLOCK(unp);
814                 return (0);
815         }
816         so2 = unp2->unp_socket;
817         SOCKBUF_LOCK(&so2->so_snd);
818         so2->so_snd.sb_mbmax += unp->unp_mbcnt - mbcnt;
819         newhiwat = so2->so_snd.sb_hiwat + unp->unp_cc - sbcc;
820         (void)chgsbsize(so2->so_cred->cr_uidinfo, &so2->so_snd.sb_hiwat,
821             newhiwat, RLIM_INFINITY);
822         sowwakeup_locked(so2);
823         unp->unp_mbcnt = mbcnt;
824         unp->unp_cc = sbcc;
825         UNP_PCB_UNLOCK(unp);
826         return (0);
827 }
828
829 static int
830 uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
831     struct mbuf *control, struct thread *td)
832 {
833         struct unpcb *unp, *unp2;
834         struct socket *so2;
835         u_int mbcnt_delta, sbcc;
836         u_int newhiwat;
837         int error = 0;
838
839         unp = sotounpcb(so);
840         KASSERT(unp != NULL, ("uipc_send: unp == NULL"));
841
842         if (flags & PRUS_OOB) {
843                 error = EOPNOTSUPP;
844                 goto release;
845         }
846         if (control != NULL && (error = unp_internalize(&control, td)))
847                 goto release;
848         if ((nam != NULL) || (flags & PRUS_EOF))
849                 UNP_LINK_WLOCK();
850         else
851                 UNP_LINK_RLOCK();
852         switch (so->so_type) {
853         case SOCK_DGRAM:
854         {
855                 const struct sockaddr *from;
856
857                 unp2 = unp->unp_conn;
858                 if (nam != NULL) {
859                         UNP_LINK_WLOCK_ASSERT();
860                         if (unp2 != NULL) {
861                                 error = EISCONN;
862                                 break;
863                         }
864                         error = unp_connect(so, nam, td);
865                         if (error)
866                                 break;
867                         unp2 = unp->unp_conn;
868                 }
869
870                 /*
871                  * Because connect() and send() are non-atomic in a sendto()
872                  * with a target address, it's possible that the socket will
873                  * have disconnected before the send() can run.  In that case
874                  * return the slightly counter-intuitive but otherwise
875                  * correct error that the socket is not connected.
876                  */
877                 if (unp2 == NULL) {
878                         error = ENOTCONN;
879                         break;
880                 }
881                 /* Lockless read. */
882                 if (unp2->unp_flags & UNP_WANTCRED)
883                         control = unp_addsockcred(td, control);
884                 UNP_PCB_LOCK(unp);
885                 if (unp->unp_addr != NULL)
886                         from = (struct sockaddr *)unp->unp_addr;
887                 else
888                         from = &sun_noname;
889                 so2 = unp2->unp_socket;
890                 SOCKBUF_LOCK(&so2->so_rcv);
891                 if (sbappendaddr_locked(&so2->so_rcv, from, m, control)) {
892                         sorwakeup_locked(so2);
893                         m = NULL;
894                         control = NULL;
895                 } else {
896                         SOCKBUF_UNLOCK(&so2->so_rcv);
897                         error = ENOBUFS;
898                 }
899                 if (nam != NULL) {
900                         UNP_LINK_WLOCK_ASSERT();
901                         UNP_PCB_LOCK(unp2);
902                         unp_disconnect(unp, unp2);
903                         UNP_PCB_UNLOCK(unp2);
904                 }
905                 UNP_PCB_UNLOCK(unp);
906                 break;
907         }
908
909         case SOCK_SEQPACKET:
910         case SOCK_STREAM:
911                 if ((so->so_state & SS_ISCONNECTED) == 0) {
912                         if (nam != NULL) {
913                                 UNP_LINK_WLOCK_ASSERT();
914                                 error = unp_connect(so, nam, td);
915                                 if (error)
916                                         break;  /* XXX */
917                         } else {
918                                 error = ENOTCONN;
919                                 break;
920                         }
921                 }
922
923                 /* Lockless read. */
924                 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
925                         error = EPIPE;
926                         break;
927                 }
928
929                 /*
930                  * Because connect() and send() are non-atomic in a sendto()
931                  * with a target address, it's possible that the socket will
932                  * have disconnected before the send() can run.  In that case
933                  * return the slightly counter-intuitive but otherwise
934                  * correct error that the socket is not connected.
935                  *
936                  * Locking here must be done carefully: the linkage lock
937                  * prevents interconnections between unpcbs from changing, so
938                  * we can traverse from unp to unp2 without acquiring unp's
939                  * lock.  Socket buffer locks follow unpcb locks, so we can
940                  * acquire both remote and lock socket buffer locks.
941                  */
942                 unp2 = unp->unp_conn;
943                 if (unp2 == NULL) {
944                         error = ENOTCONN;
945                         break;
946                 }
947                 so2 = unp2->unp_socket;
948                 UNP_PCB_LOCK(unp2);
949                 SOCKBUF_LOCK(&so2->so_rcv);
950                 if (unp2->unp_flags & UNP_WANTCRED) {
951                         /*
952                          * Credentials are passed only once on SOCK_STREAM
953                          * and SOCK_SEQPACKET.
954                          */
955                         unp2->unp_flags &= ~UNP_WANTCRED;
956                         control = unp_addsockcred(td, control);
957                 }
958                 /*
959                  * Send to paired receive port, and then reduce send buffer
960                  * hiwater marks to maintain backpressure.  Wake up readers.
961                  */
962                 switch (so->so_type) {
963                 case SOCK_STREAM:
964                         if (control != NULL) {
965                                 if (sbappendcontrol_locked(&so2->so_rcv, m,
966                                     control))
967                                         control = NULL;
968                         } else
969                                 sbappend_locked(&so2->so_rcv, m);
970                         break;
971
972                 case SOCK_SEQPACKET: {
973                         const struct sockaddr *from;
974
975                         from = &sun_noname;
976                         if (sbappendaddr_locked(&so2->so_rcv, from, m,
977                             control))
978                                 control = NULL;
979                         break;
980                         }
981                 }
982
983                 /*
984                  * XXXRW: While fine for SOCK_STREAM, this conflates maximum
985                  * datagram size and back-pressure for SOCK_SEQPACKET, which
986                  * can lead to undesired return of EMSGSIZE on send instead
987                  * of more desirable blocking.
988                  */
989                 mbcnt_delta = so2->so_rcv.sb_mbcnt - unp2->unp_mbcnt;
990                 unp2->unp_mbcnt = so2->so_rcv.sb_mbcnt;
991                 sbcc = so2->so_rcv.sb_cc;
992                 sorwakeup_locked(so2);
993
994                 SOCKBUF_LOCK(&so->so_snd);
995                 if ((int)so->so_snd.sb_hiwat >= (int)(sbcc - unp2->unp_cc))
996                         newhiwat = so->so_snd.sb_hiwat - (sbcc - unp2->unp_cc);
997                 else
998                         newhiwat = 0;
999                 (void)chgsbsize(so->so_cred->cr_uidinfo, &so->so_snd.sb_hiwat,
1000                     newhiwat, RLIM_INFINITY);
1001                 so->so_snd.sb_mbmax -= mbcnt_delta;
1002                 SOCKBUF_UNLOCK(&so->so_snd);
1003                 unp2->unp_cc = sbcc;
1004                 UNP_PCB_UNLOCK(unp2);
1005                 m = NULL;
1006                 break;
1007
1008         default:
1009                 panic("uipc_send unknown socktype");
1010         }
1011
1012         /*
1013          * PRUS_EOF is equivalent to pru_send followed by pru_shutdown.
1014          */
1015         if (flags & PRUS_EOF) {
1016                 UNP_PCB_LOCK(unp);
1017                 socantsendmore(so);
1018                 unp_shutdown(unp);
1019                 UNP_PCB_UNLOCK(unp);
1020         }
1021
1022         if ((nam != NULL) || (flags & PRUS_EOF))
1023                 UNP_LINK_WUNLOCK();
1024         else
1025                 UNP_LINK_RUNLOCK();
1026
1027         if (control != NULL && error != 0)
1028                 unp_dispose(control);
1029
1030 release:
1031         if (control != NULL)
1032                 m_freem(control);
1033         if (m != NULL)
1034                 m_freem(m);
1035         return (error);
1036 }
1037
1038 static int
1039 uipc_sense(struct socket *so, struct stat *sb)
1040 {
1041         struct unpcb *unp, *unp2;
1042         struct socket *so2;
1043
1044         unp = sotounpcb(so);
1045         KASSERT(unp != NULL, ("uipc_sense: unp == NULL"));
1046
1047         sb->st_blksize = so->so_snd.sb_hiwat;
1048         UNP_LINK_RLOCK();
1049         UNP_PCB_LOCK(unp);
1050         unp2 = unp->unp_conn;
1051         if ((so->so_type == SOCK_STREAM || so->so_type == SOCK_SEQPACKET) &&
1052             unp2 != NULL) {
1053                 so2 = unp2->unp_socket;
1054                 sb->st_blksize += so2->so_rcv.sb_cc;
1055         }
1056         sb->st_dev = NODEV;
1057         if (unp->unp_ino == 0)
1058                 unp->unp_ino = (++unp_ino == 0) ? ++unp_ino : unp_ino;
1059         sb->st_ino = unp->unp_ino;
1060         UNP_PCB_UNLOCK(unp);
1061         UNP_LINK_RUNLOCK();
1062         return (0);
1063 }
1064
1065 static int
1066 uipc_shutdown(struct socket *so)
1067 {
1068         struct unpcb *unp;
1069
1070         unp = sotounpcb(so);
1071         KASSERT(unp != NULL, ("uipc_shutdown: unp == NULL"));
1072
1073         UNP_LINK_WLOCK();
1074         UNP_PCB_LOCK(unp);
1075         socantsendmore(so);
1076         unp_shutdown(unp);
1077         UNP_PCB_UNLOCK(unp);
1078         UNP_LINK_WUNLOCK();
1079         return (0);
1080 }
1081
1082 static int
1083 uipc_sockaddr(struct socket *so, struct sockaddr **nam)
1084 {
1085         struct unpcb *unp;
1086         const struct sockaddr *sa;
1087
1088         unp = sotounpcb(so);
1089         KASSERT(unp != NULL, ("uipc_sockaddr: unp == NULL"));
1090
1091         *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK);
1092         UNP_PCB_LOCK(unp);
1093         if (unp->unp_addr != NULL)
1094                 sa = (struct sockaddr *) unp->unp_addr;
1095         else
1096                 sa = &sun_noname;
1097         bcopy(sa, *nam, sa->sa_len);
1098         UNP_PCB_UNLOCK(unp);
1099         return (0);
1100 }
1101
1102 static struct pr_usrreqs uipc_usrreqs_dgram = {
1103         .pru_abort =            uipc_abort,
1104         .pru_accept =           uipc_accept,
1105         .pru_attach =           uipc_attach,
1106         .pru_bind =             uipc_bind,
1107         .pru_bindat =           uipc_bindat,
1108         .pru_connect =          uipc_connect,
1109         .pru_connectat =        uipc_connectat,
1110         .pru_connect2 =         uipc_connect2,
1111         .pru_detach =           uipc_detach,
1112         .pru_disconnect =       uipc_disconnect,
1113         .pru_listen =           uipc_listen,
1114         .pru_peeraddr =         uipc_peeraddr,
1115         .pru_rcvd =             uipc_rcvd,
1116         .pru_send =             uipc_send,
1117         .pru_sense =            uipc_sense,
1118         .pru_shutdown =         uipc_shutdown,
1119         .pru_sockaddr =         uipc_sockaddr,
1120         .pru_soreceive =        soreceive_dgram,
1121         .pru_close =            uipc_close,
1122 };
1123
1124 static struct pr_usrreqs uipc_usrreqs_seqpacket = {
1125         .pru_abort =            uipc_abort,
1126         .pru_accept =           uipc_accept,
1127         .pru_attach =           uipc_attach,
1128         .pru_bind =             uipc_bind,
1129         .pru_bindat =           uipc_bindat,
1130         .pru_connect =          uipc_connect,
1131         .pru_connectat =        uipc_connectat,
1132         .pru_connect2 =         uipc_connect2,
1133         .pru_detach =           uipc_detach,
1134         .pru_disconnect =       uipc_disconnect,
1135         .pru_listen =           uipc_listen,
1136         .pru_peeraddr =         uipc_peeraddr,
1137         .pru_rcvd =             uipc_rcvd,
1138         .pru_send =             uipc_send,
1139         .pru_sense =            uipc_sense,
1140         .pru_shutdown =         uipc_shutdown,
1141         .pru_sockaddr =         uipc_sockaddr,
1142         .pru_soreceive =        soreceive_generic,      /* XXX: or...? */
1143         .pru_close =            uipc_close,
1144 };
1145
1146 static struct pr_usrreqs uipc_usrreqs_stream = {
1147         .pru_abort =            uipc_abort,
1148         .pru_accept =           uipc_accept,
1149         .pru_attach =           uipc_attach,
1150         .pru_bind =             uipc_bind,
1151         .pru_bindat =           uipc_bindat,
1152         .pru_connect =          uipc_connect,
1153         .pru_connectat =        uipc_connectat,
1154         .pru_connect2 =         uipc_connect2,
1155         .pru_detach =           uipc_detach,
1156         .pru_disconnect =       uipc_disconnect,
1157         .pru_listen =           uipc_listen,
1158         .pru_peeraddr =         uipc_peeraddr,
1159         .pru_rcvd =             uipc_rcvd,
1160         .pru_send =             uipc_send,
1161         .pru_sense =            uipc_sense,
1162         .pru_shutdown =         uipc_shutdown,
1163         .pru_sockaddr =         uipc_sockaddr,
1164         .pru_soreceive =        soreceive_generic,
1165         .pru_close =            uipc_close,
1166 };
1167
1168 static int
1169 uipc_ctloutput(struct socket *so, struct sockopt *sopt)
1170 {
1171         struct unpcb *unp;
1172         struct xucred xu;
1173         int error, optval;
1174
1175         if (sopt->sopt_level != 0)
1176                 return (EINVAL);
1177
1178         unp = sotounpcb(so);
1179         KASSERT(unp != NULL, ("uipc_ctloutput: unp == NULL"));
1180         error = 0;
1181         switch (sopt->sopt_dir) {
1182         case SOPT_GET:
1183                 switch (sopt->sopt_name) {
1184                 case LOCAL_PEERCRED:
1185                         UNP_PCB_LOCK(unp);
1186                         if (unp->unp_flags & UNP_HAVEPC)
1187                                 xu = unp->unp_peercred;
1188                         else {
1189                                 if (so->so_type == SOCK_STREAM)
1190                                         error = ENOTCONN;
1191                                 else
1192                                         error = EINVAL;
1193                         }
1194                         UNP_PCB_UNLOCK(unp);
1195                         if (error == 0)
1196                                 error = sooptcopyout(sopt, &xu, sizeof(xu));
1197                         break;
1198
1199                 case LOCAL_CREDS:
1200                         /* Unlocked read. */
1201                         optval = unp->unp_flags & UNP_WANTCRED ? 1 : 0;
1202                         error = sooptcopyout(sopt, &optval, sizeof(optval));
1203                         break;
1204
1205                 case LOCAL_CONNWAIT:
1206                         /* Unlocked read. */
1207                         optval = unp->unp_flags & UNP_CONNWAIT ? 1 : 0;
1208                         error = sooptcopyout(sopt, &optval, sizeof(optval));
1209                         break;
1210
1211                 default:
1212                         error = EOPNOTSUPP;
1213                         break;
1214                 }
1215                 break;
1216
1217         case SOPT_SET:
1218                 switch (sopt->sopt_name) {
1219                 case LOCAL_CREDS:
1220                 case LOCAL_CONNWAIT:
1221                         error = sooptcopyin(sopt, &optval, sizeof(optval),
1222                                             sizeof(optval));
1223                         if (error)
1224                                 break;
1225
1226 #define OPTSET(bit) do {                                                \
1227         UNP_PCB_LOCK(unp);                                              \
1228         if (optval)                                                     \
1229                 unp->unp_flags |= bit;                                  \
1230         else                                                            \
1231                 unp->unp_flags &= ~bit;                                 \
1232         UNP_PCB_UNLOCK(unp);                                            \
1233 } while (0)
1234
1235                         switch (sopt->sopt_name) {
1236                         case LOCAL_CREDS:
1237                                 OPTSET(UNP_WANTCRED);
1238                                 break;
1239
1240                         case LOCAL_CONNWAIT:
1241                                 OPTSET(UNP_CONNWAIT);
1242                                 break;
1243
1244                         default:
1245                                 break;
1246                         }
1247                         break;
1248 #undef  OPTSET
1249                 default:
1250                         error = ENOPROTOOPT;
1251                         break;
1252                 }
1253                 break;
1254
1255         default:
1256                 error = EOPNOTSUPP;
1257                 break;
1258         }
1259         return (error);
1260 }
1261
1262 static int
1263 unp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1264 {
1265
1266         return (unp_connectat(AT_FDCWD, so, nam, td));
1267 }
1268
1269 static int
1270 unp_connectat(int fd, struct socket *so, struct sockaddr *nam,
1271     struct thread *td)
1272 {
1273         struct sockaddr_un *soun = (struct sockaddr_un *)nam;
1274         struct vnode *vp;
1275         struct socket *so2, *so3;
1276         struct unpcb *unp, *unp2, *unp3;
1277         int error, len;
1278         struct nameidata nd;
1279         char buf[SOCK_MAXADDRLEN];
1280         struct sockaddr *sa;
1281
1282         UNP_LINK_WLOCK_ASSERT();
1283
1284         unp = sotounpcb(so);
1285         KASSERT(unp != NULL, ("unp_connect: unp == NULL"));
1286
1287         if (nam->sa_len > sizeof(struct sockaddr_un))
1288                 return (EINVAL);
1289         len = nam->sa_len - offsetof(struct sockaddr_un, sun_path);
1290         if (len <= 0)
1291                 return (EINVAL);
1292         bcopy(soun->sun_path, buf, len);
1293         buf[len] = 0;
1294
1295         UNP_PCB_LOCK(unp);
1296         if (unp->unp_flags & UNP_CONNECTING) {
1297                 UNP_PCB_UNLOCK(unp);
1298                 return (EALREADY);
1299         }
1300         UNP_LINK_WUNLOCK();
1301         unp->unp_flags |= UNP_CONNECTING;
1302         UNP_PCB_UNLOCK(unp);
1303
1304         sa = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK);
1305         NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF,
1306             UIO_SYSSPACE, buf, fd, CAP_CONNECTAT, td);
1307         error = namei(&nd);
1308         if (error)
1309                 vp = NULL;
1310         else
1311                 vp = nd.ni_vp;
1312         ASSERT_VOP_LOCKED(vp, "unp_connect");
1313         NDFREE(&nd, NDF_ONLY_PNBUF);
1314         if (error)
1315                 goto bad;
1316
1317         if (vp->v_type != VSOCK) {
1318                 error = ENOTSOCK;
1319                 goto bad;
1320         }
1321 #ifdef MAC
1322         error = mac_vnode_check_open(td->td_ucred, vp, VWRITE | VREAD);
1323         if (error)
1324                 goto bad;
1325 #endif
1326         error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td);
1327         if (error)
1328                 goto bad;
1329
1330         unp = sotounpcb(so);
1331         KASSERT(unp != NULL, ("unp_connect: unp == NULL"));
1332
1333         /*
1334          * Lock linkage lock for two reasons: make sure v_socket is stable,
1335          * and to protect simultaneous locking of multiple pcbs.
1336          */
1337         UNP_LINK_WLOCK();
1338         VOP_UNP_CONNECT(vp, &so2);
1339         if (so2 == NULL) {
1340                 error = ECONNREFUSED;
1341                 goto bad2;
1342         }
1343         if (so->so_type != so2->so_type) {
1344                 error = EPROTOTYPE;
1345                 goto bad2;
1346         }
1347         if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
1348                 if (so2->so_options & SO_ACCEPTCONN) {
1349                         CURVNET_SET(so2->so_vnet);
1350                         so3 = sonewconn(so2, 0);
1351                         CURVNET_RESTORE();
1352                 } else
1353                         so3 = NULL;
1354                 if (so3 == NULL) {
1355                         error = ECONNREFUSED;
1356                         goto bad2;
1357                 }
1358                 unp = sotounpcb(so);
1359                 unp2 = sotounpcb(so2);
1360                 unp3 = sotounpcb(so3);
1361                 UNP_PCB_LOCK(unp);
1362                 UNP_PCB_LOCK(unp2);
1363                 UNP_PCB_LOCK(unp3);
1364                 if (unp2->unp_addr != NULL) {
1365                         bcopy(unp2->unp_addr, sa, unp2->unp_addr->sun_len);
1366                         unp3->unp_addr = (struct sockaddr_un *) sa;
1367                         sa = NULL;
1368                 }
1369
1370                 /*
1371                  * The connecter's (client's) credentials are copied from its
1372                  * process structure at the time of connect() (which is now).
1373                  */
1374                 cru2x(td->td_ucred, &unp3->unp_peercred);
1375                 unp3->unp_flags |= UNP_HAVEPC;
1376
1377                 /*
1378                  * The receiver's (server's) credentials are copied from the
1379                  * unp_peercred member of socket on which the former called
1380                  * listen(); uipc_listen() cached that process's credentials
1381                  * at that time so we can use them now.
1382                  */
1383                 KASSERT(unp2->unp_flags & UNP_HAVEPCCACHED,
1384                     ("unp_connect: listener without cached peercred"));
1385                 memcpy(&unp->unp_peercred, &unp2->unp_peercred,
1386                     sizeof(unp->unp_peercred));
1387                 unp->unp_flags |= UNP_HAVEPC;
1388                 if (unp2->unp_flags & UNP_WANTCRED)
1389                         unp3->unp_flags |= UNP_WANTCRED;
1390                 UNP_PCB_UNLOCK(unp3);
1391                 UNP_PCB_UNLOCK(unp2);
1392                 UNP_PCB_UNLOCK(unp);
1393 #ifdef MAC
1394                 mac_socketpeer_set_from_socket(so, so3);
1395                 mac_socketpeer_set_from_socket(so3, so);
1396 #endif
1397
1398                 so2 = so3;
1399         }
1400         unp = sotounpcb(so);
1401         KASSERT(unp != NULL, ("unp_connect: unp == NULL"));
1402         unp2 = sotounpcb(so2);
1403         KASSERT(unp2 != NULL, ("unp_connect: unp2 == NULL"));
1404         UNP_PCB_LOCK(unp);
1405         UNP_PCB_LOCK(unp2);
1406         error = unp_connect2(so, so2, PRU_CONNECT);
1407         UNP_PCB_UNLOCK(unp2);
1408         UNP_PCB_UNLOCK(unp);
1409 bad2:
1410         UNP_LINK_WUNLOCK();
1411 bad:
1412         if (vp != NULL)
1413                 vput(vp);
1414         free(sa, M_SONAME);
1415         UNP_LINK_WLOCK();
1416         UNP_PCB_LOCK(unp);
1417         unp->unp_flags &= ~UNP_CONNECTING;
1418         UNP_PCB_UNLOCK(unp);
1419         return (error);
1420 }
1421
1422 static int
1423 unp_connect2(struct socket *so, struct socket *so2, int req)
1424 {
1425         struct unpcb *unp;
1426         struct unpcb *unp2;
1427
1428         unp = sotounpcb(so);
1429         KASSERT(unp != NULL, ("unp_connect2: unp == NULL"));
1430         unp2 = sotounpcb(so2);
1431         KASSERT(unp2 != NULL, ("unp_connect2: unp2 == NULL"));
1432
1433         UNP_LINK_WLOCK_ASSERT();
1434         UNP_PCB_LOCK_ASSERT(unp);
1435         UNP_PCB_LOCK_ASSERT(unp2);
1436
1437         if (so2->so_type != so->so_type)
1438                 return (EPROTOTYPE);
1439         unp->unp_conn = unp2;
1440
1441         switch (so->so_type) {
1442         case SOCK_DGRAM:
1443                 LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink);
1444                 soisconnected(so);
1445                 break;
1446
1447         case SOCK_STREAM:
1448         case SOCK_SEQPACKET:
1449                 unp2->unp_conn = unp;
1450                 if (req == PRU_CONNECT &&
1451                     ((unp->unp_flags | unp2->unp_flags) & UNP_CONNWAIT))
1452                         soisconnecting(so);
1453                 else
1454                         soisconnected(so);
1455                 soisconnected(so2);
1456                 break;
1457
1458         default:
1459                 panic("unp_connect2");
1460         }
1461         return (0);
1462 }
1463
1464 static void
1465 unp_disconnect(struct unpcb *unp, struct unpcb *unp2)
1466 {
1467         struct socket *so;
1468
1469         KASSERT(unp2 != NULL, ("unp_disconnect: unp2 == NULL"));
1470
1471         UNP_LINK_WLOCK_ASSERT();
1472         UNP_PCB_LOCK_ASSERT(unp);
1473         UNP_PCB_LOCK_ASSERT(unp2);
1474
1475         unp->unp_conn = NULL;
1476         switch (unp->unp_socket->so_type) {
1477         case SOCK_DGRAM:
1478                 LIST_REMOVE(unp, unp_reflink);
1479                 so = unp->unp_socket;
1480                 SOCK_LOCK(so);
1481                 so->so_state &= ~SS_ISCONNECTED;
1482                 SOCK_UNLOCK(so);
1483                 break;
1484
1485         case SOCK_STREAM:
1486         case SOCK_SEQPACKET:
1487                 soisdisconnected(unp->unp_socket);
1488                 unp2->unp_conn = NULL;
1489                 soisdisconnected(unp2->unp_socket);
1490                 break;
1491         }
1492 }
1493
1494 /*
1495  * unp_pcblist() walks the global list of struct unpcb's to generate a
1496  * pointer list, bumping the refcount on each unpcb.  It then copies them out
1497  * sequentially, validating the generation number on each to see if it has
1498  * been detached.  All of this is necessary because copyout() may sleep on
1499  * disk I/O.
1500  */
1501 static int
1502 unp_pcblist(SYSCTL_HANDLER_ARGS)
1503 {
1504         int error, i, n;
1505         int freeunp;
1506         struct unpcb *unp, **unp_list;
1507         unp_gen_t gencnt;
1508         struct xunpgen *xug;
1509         struct unp_head *head;
1510         struct xunpcb *xu;
1511
1512         switch ((intptr_t)arg1) {
1513         case SOCK_STREAM:
1514                 head = &unp_shead;
1515                 break;
1516
1517         case SOCK_DGRAM:
1518                 head = &unp_dhead;
1519                 break;
1520
1521         case SOCK_SEQPACKET:
1522                 head = &unp_sphead;
1523                 break;
1524
1525         default:
1526                 panic("unp_pcblist: arg1 %d", (int)(intptr_t)arg1);
1527         }
1528
1529         /*
1530          * The process of preparing the PCB list is too time-consuming and
1531          * resource-intensive to repeat twice on every request.
1532          */
1533         if (req->oldptr == NULL) {
1534                 n = unp_count;
1535                 req->oldidx = 2 * (sizeof *xug)
1536                         + (n + n/8) * sizeof(struct xunpcb);
1537                 return (0);
1538         }
1539
1540         if (req->newptr != NULL)
1541                 return (EPERM);
1542
1543         /*
1544          * OK, now we're committed to doing something.
1545          */
1546         xug = malloc(sizeof(*xug), M_TEMP, M_WAITOK);
1547         UNP_LIST_LOCK();
1548         gencnt = unp_gencnt;
1549         n = unp_count;
1550         UNP_LIST_UNLOCK();
1551
1552         xug->xug_len = sizeof *xug;
1553         xug->xug_count = n;
1554         xug->xug_gen = gencnt;
1555         xug->xug_sogen = so_gencnt;
1556         error = SYSCTL_OUT(req, xug, sizeof *xug);
1557         if (error) {
1558                 free(xug, M_TEMP);
1559                 return (error);
1560         }
1561
1562         unp_list = malloc(n * sizeof *unp_list, M_TEMP, M_WAITOK);
1563
1564         UNP_LIST_LOCK();
1565         for (unp = LIST_FIRST(head), i = 0; unp && i < n;
1566              unp = LIST_NEXT(unp, unp_link)) {
1567                 UNP_PCB_LOCK(unp);
1568                 if (unp->unp_gencnt <= gencnt) {
1569                         if (cr_cansee(req->td->td_ucred,
1570                             unp->unp_socket->so_cred)) {
1571                                 UNP_PCB_UNLOCK(unp);
1572                                 continue;
1573                         }
1574                         unp_list[i++] = unp;
1575                         unp->unp_refcount++;
1576                 }
1577                 UNP_PCB_UNLOCK(unp);
1578         }
1579         UNP_LIST_UNLOCK();
1580         n = i;                  /* In case we lost some during malloc. */
1581
1582         error = 0;
1583         xu = malloc(sizeof(*xu), M_TEMP, M_WAITOK | M_ZERO);
1584         for (i = 0; i < n; i++) {
1585                 unp = unp_list[i];
1586                 UNP_PCB_LOCK(unp);
1587                 unp->unp_refcount--;
1588                 if (unp->unp_refcount != 0 && unp->unp_gencnt <= gencnt) {
1589                         xu->xu_len = sizeof *xu;
1590                         xu->xu_unpp = unp;
1591                         /*
1592                          * XXX - need more locking here to protect against
1593                          * connect/disconnect races for SMP.
1594                          */
1595                         if (unp->unp_addr != NULL)
1596                                 bcopy(unp->unp_addr, &xu->xu_addr,
1597                                       unp->unp_addr->sun_len);
1598                         if (unp->unp_conn != NULL &&
1599                             unp->unp_conn->unp_addr != NULL)
1600                                 bcopy(unp->unp_conn->unp_addr,
1601                                       &xu->xu_caddr,
1602                                       unp->unp_conn->unp_addr->sun_len);
1603                         bcopy(unp, &xu->xu_unp, sizeof *unp);
1604                         sotoxsocket(unp->unp_socket, &xu->xu_socket);
1605                         UNP_PCB_UNLOCK(unp);
1606                         error = SYSCTL_OUT(req, xu, sizeof *xu);
1607                 } else {
1608                         freeunp = (unp->unp_refcount == 0);
1609                         UNP_PCB_UNLOCK(unp);
1610                         if (freeunp) {
1611                                 UNP_PCB_LOCK_DESTROY(unp);
1612                                 uma_zfree(unp_zone, unp);
1613                         }
1614                 }
1615         }
1616         free(xu, M_TEMP);
1617         if (!error) {
1618                 /*
1619                  * Give the user an updated idea of our state.  If the
1620                  * generation differs from what we told her before, she knows
1621                  * that something happened while we were processing this
1622                  * request, and it might be necessary to retry.
1623                  */
1624                 xug->xug_gen = unp_gencnt;
1625                 xug->xug_sogen = so_gencnt;
1626                 xug->xug_count = unp_count;
1627                 error = SYSCTL_OUT(req, xug, sizeof *xug);
1628         }
1629         free(unp_list, M_TEMP);
1630         free(xug, M_TEMP);
1631         return (error);
1632 }
1633
1634 SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLTYPE_OPAQUE | CTLFLAG_RD,
1635     (void *)(intptr_t)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb",
1636     "List of active local datagram sockets");
1637 SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLTYPE_OPAQUE | CTLFLAG_RD,
1638     (void *)(intptr_t)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb",
1639     "List of active local stream sockets");
1640 SYSCTL_PROC(_net_local_seqpacket, OID_AUTO, pcblist,
1641     CTLTYPE_OPAQUE | CTLFLAG_RD,
1642     (void *)(intptr_t)SOCK_SEQPACKET, 0, unp_pcblist, "S,xunpcb",
1643     "List of active local seqpacket sockets");
1644
1645 static void
1646 unp_shutdown(struct unpcb *unp)
1647 {
1648         struct unpcb *unp2;
1649         struct socket *so;
1650
1651         UNP_LINK_WLOCK_ASSERT();
1652         UNP_PCB_LOCK_ASSERT(unp);
1653
1654         unp2 = unp->unp_conn;
1655         if ((unp->unp_socket->so_type == SOCK_STREAM ||
1656             (unp->unp_socket->so_type == SOCK_SEQPACKET)) && unp2 != NULL) {
1657                 so = unp2->unp_socket;
1658                 if (so != NULL)
1659                         socantrcvmore(so);
1660         }
1661 }
1662
1663 static void
1664 unp_drop(struct unpcb *unp, int errno)
1665 {
1666         struct socket *so = unp->unp_socket;
1667         struct unpcb *unp2;
1668
1669         UNP_LINK_WLOCK_ASSERT();
1670         UNP_PCB_LOCK_ASSERT(unp);
1671
1672         so->so_error = errno;
1673         unp2 = unp->unp_conn;
1674         if (unp2 == NULL)
1675                 return;
1676         UNP_PCB_LOCK(unp2);
1677         unp_disconnect(unp, unp2);
1678         UNP_PCB_UNLOCK(unp2);
1679 }
1680
1681 static void
1682 unp_freerights(struct filedescent *fde, int fdcount)
1683 {
1684         struct file *fp;
1685         int i;
1686
1687         for (i = 0; i < fdcount; i++, fde++) {
1688                 fp = fde->fde_file;
1689                 bzero(fde, sizeof(*fde));
1690                 unp_discard(fp);
1691         }
1692 }
1693
1694 static int
1695 unp_externalize(struct mbuf *control, struct mbuf **controlp)
1696 {
1697         struct thread *td = curthread;          /* XXX */
1698         struct cmsghdr *cm = mtod(control, struct cmsghdr *);
1699         int i;
1700         int *fdp;
1701         struct filedesc *fdesc = td->td_proc->p_fd;
1702         struct filedescent *fde, *fdep;
1703         void *data;
1704         socklen_t clen = control->m_len, datalen;
1705         int error, newfds;
1706         int f;
1707         u_int newlen;
1708
1709         UNP_LINK_UNLOCK_ASSERT();
1710
1711         error = 0;
1712         if (controlp != NULL) /* controlp == NULL => free control messages */
1713                 *controlp = NULL;
1714         while (cm != NULL) {
1715                 if (sizeof(*cm) > clen || cm->cmsg_len > clen) {
1716                         error = EINVAL;
1717                         break;
1718                 }
1719                 data = CMSG_DATA(cm);
1720                 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1721                 if (cm->cmsg_level == SOL_SOCKET
1722                     && cm->cmsg_type == SCM_RIGHTS) {
1723                         newfds = datalen / sizeof(*fdep);
1724                         fdep = data;
1725
1726                         /* If we're not outputting the descriptors free them. */
1727                         if (error || controlp == NULL) {
1728                                 unp_freerights(fdep, newfds);
1729                                 goto next;
1730                         }
1731                         FILEDESC_XLOCK(fdesc);
1732                         /* if the new FD's will not fit free them.  */
1733                         if (!fdavail(td, newfds)) {
1734                                 FILEDESC_XUNLOCK(fdesc);
1735                                 error = EMSGSIZE;
1736                                 unp_freerights(fdep, newfds);
1737                                 goto next;
1738                         }
1739
1740                         /*
1741                          * Now change each pointer to an fd in the global
1742                          * table to an integer that is the index to the local
1743                          * fd table entry that we set up to point to the
1744                          * global one we are transferring.
1745                          */
1746                         newlen = newfds * sizeof(int);
1747                         *controlp = sbcreatecontrol(NULL, newlen,
1748                             SCM_RIGHTS, SOL_SOCKET);
1749                         if (*controlp == NULL) {
1750                                 FILEDESC_XUNLOCK(fdesc);
1751                                 error = E2BIG;
1752                                 unp_freerights(fdep, newfds);
1753                                 goto next;
1754                         }
1755
1756                         fdp = (int *)
1757                             CMSG_DATA(mtod(*controlp, struct cmsghdr *));
1758                         for (i = 0; i < newfds; i++, fdep++, fdp++) {
1759                                 if (fdalloc(td, 0, &f))
1760                                         panic("unp_externalize fdalloc failed");
1761                                 fde = &fdesc->fd_ofiles[f];
1762                                 fde->fde_file = fdep->fde_file;
1763                                 filecaps_copy(&fdep->fde_caps, &fde->fde_caps);
1764                                 unp_externalize_fp(fde->fde_file);
1765                                 *fdp = f;
1766                         }
1767                         FILEDESC_XUNLOCK(fdesc);
1768                 } else {
1769                         /* We can just copy anything else across. */
1770                         if (error || controlp == NULL)
1771                                 goto next;
1772                         *controlp = sbcreatecontrol(NULL, datalen,
1773                             cm->cmsg_type, cm->cmsg_level);
1774                         if (*controlp == NULL) {
1775                                 error = ENOBUFS;
1776                                 goto next;
1777                         }
1778                         bcopy(data,
1779                             CMSG_DATA(mtod(*controlp, struct cmsghdr *)),
1780                             datalen);
1781                 }
1782                 controlp = &(*controlp)->m_next;
1783
1784 next:
1785                 if (CMSG_SPACE(datalen) < clen) {
1786                         clen -= CMSG_SPACE(datalen);
1787                         cm = (struct cmsghdr *)
1788                             ((caddr_t)cm + CMSG_SPACE(datalen));
1789                 } else {
1790                         clen = 0;
1791                         cm = NULL;
1792                 }
1793         }
1794
1795         m_freem(control);
1796         return (error);
1797 }
1798
1799 static void
1800 unp_zone_change(void *tag)
1801 {
1802
1803         uma_zone_set_max(unp_zone, maxsockets);
1804 }
1805
1806 static void
1807 unp_init(void)
1808 {
1809
1810 #ifdef VIMAGE
1811         if (!IS_DEFAULT_VNET(curvnet))
1812                 return;
1813 #endif
1814         unp_zone = uma_zcreate("unpcb", sizeof(struct unpcb), NULL, NULL,
1815             NULL, NULL, UMA_ALIGN_PTR, 0);
1816         if (unp_zone == NULL)
1817                 panic("unp_init");
1818         uma_zone_set_max(unp_zone, maxsockets);
1819         uma_zone_set_warning(unp_zone, "kern.ipc.maxsockets limit reached");
1820         EVENTHANDLER_REGISTER(maxsockets_change, unp_zone_change,
1821             NULL, EVENTHANDLER_PRI_ANY);
1822         LIST_INIT(&unp_dhead);
1823         LIST_INIT(&unp_shead);
1824         LIST_INIT(&unp_sphead);
1825         SLIST_INIT(&unp_defers);
1826         TIMEOUT_TASK_INIT(taskqueue_thread, &unp_gc_task, 0, unp_gc, NULL);
1827         TASK_INIT(&unp_defer_task, 0, unp_process_defers, NULL);
1828         UNP_LINK_LOCK_INIT();
1829         UNP_LIST_LOCK_INIT();
1830         UNP_DEFERRED_LOCK_INIT();
1831 }
1832
1833 static int
1834 unp_internalize(struct mbuf **controlp, struct thread *td)
1835 {
1836         struct mbuf *control = *controlp;
1837         struct proc *p = td->td_proc;
1838         struct filedesc *fdesc = p->p_fd;
1839         struct bintime *bt;
1840         struct cmsghdr *cm = mtod(control, struct cmsghdr *);
1841         struct cmsgcred *cmcred;
1842         struct filedescent *fde, *fdep;
1843         struct file *fp;
1844         struct timeval *tv;
1845         int i, fd, *fdp;
1846         void *data;
1847         socklen_t clen = control->m_len, datalen;
1848         int error, oldfds;
1849         u_int newlen;
1850
1851         UNP_LINK_UNLOCK_ASSERT();
1852
1853         error = 0;
1854         *controlp = NULL;
1855         while (cm != NULL) {
1856                 if (sizeof(*cm) > clen || cm->cmsg_level != SOL_SOCKET
1857                     || cm->cmsg_len > clen) {
1858                         error = EINVAL;
1859                         goto out;
1860                 }
1861                 data = CMSG_DATA(cm);
1862                 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1863
1864                 switch (cm->cmsg_type) {
1865                 /*
1866                  * Fill in credential information.
1867                  */
1868                 case SCM_CREDS:
1869                         *controlp = sbcreatecontrol(NULL, sizeof(*cmcred),
1870                             SCM_CREDS, SOL_SOCKET);
1871                         if (*controlp == NULL) {
1872                                 error = ENOBUFS;
1873                                 goto out;
1874                         }
1875                         cmcred = (struct cmsgcred *)
1876                             CMSG_DATA(mtod(*controlp, struct cmsghdr *));
1877                         cmcred->cmcred_pid = p->p_pid;
1878                         cmcred->cmcred_uid = td->td_ucred->cr_ruid;
1879                         cmcred->cmcred_gid = td->td_ucred->cr_rgid;
1880                         cmcred->cmcred_euid = td->td_ucred->cr_uid;
1881                         cmcred->cmcred_ngroups = MIN(td->td_ucred->cr_ngroups,
1882                             CMGROUP_MAX);
1883                         for (i = 0; i < cmcred->cmcred_ngroups; i++)
1884                                 cmcred->cmcred_groups[i] =
1885                                     td->td_ucred->cr_groups[i];
1886                         break;
1887
1888                 case SCM_RIGHTS:
1889                         oldfds = datalen / sizeof (int);
1890                         /*
1891                          * Check that all the FDs passed in refer to legal
1892                          * files.  If not, reject the entire operation.
1893                          */
1894                         fdp = data;
1895                         FILEDESC_SLOCK(fdesc);
1896                         for (i = 0; i < oldfds; i++) {
1897                                 fd = *fdp++;
1898                                 if (fget_locked(fdesc, fd) == NULL) {
1899                                         FILEDESC_SUNLOCK(fdesc);
1900                                         error = EBADF;
1901                                         goto out;
1902                                 }
1903                                 fp = fdesc->fd_ofiles[fd].fde_file;
1904                                 if (!(fp->f_ops->fo_flags & DFLAG_PASSABLE)) {
1905                                         FILEDESC_SUNLOCK(fdesc);
1906                                         error = EOPNOTSUPP;
1907                                         goto out;
1908                                 }
1909
1910                         }
1911
1912                         /*
1913                          * Now replace the integer FDs with pointers to the
1914                          * file structure and capability rights.
1915                          */
1916                         newlen = oldfds * sizeof(*fdep);
1917                         *controlp = sbcreatecontrol(NULL, newlen,
1918                             SCM_RIGHTS, SOL_SOCKET);
1919                         if (*controlp == NULL) {
1920                                 FILEDESC_SUNLOCK(fdesc);
1921                                 error = E2BIG;
1922                                 goto out;
1923                         }
1924                         fdp = data;
1925                         fdep = (struct filedescent *)
1926                             CMSG_DATA(mtod(*controlp, struct cmsghdr *));
1927                         for (i = 0; i < oldfds; i++, fdep++, fdp++) {
1928                                 fde = &fdesc->fd_ofiles[*fdp];
1929                                 fdep->fde_file = fde->fde_file;
1930                                 filecaps_copy(&fde->fde_caps, &fdep->fde_caps);
1931                                 unp_internalize_fp(fdep->fde_file);
1932                         }
1933                         FILEDESC_SUNLOCK(fdesc);
1934                         break;
1935
1936                 case SCM_TIMESTAMP:
1937                         *controlp = sbcreatecontrol(NULL, sizeof(*tv),
1938                             SCM_TIMESTAMP, SOL_SOCKET);
1939                         if (*controlp == NULL) {
1940                                 error = ENOBUFS;
1941                                 goto out;
1942                         }
1943                         tv = (struct timeval *)
1944                             CMSG_DATA(mtod(*controlp, struct cmsghdr *));
1945                         microtime(tv);
1946                         break;
1947
1948                 case SCM_BINTIME:
1949                         *controlp = sbcreatecontrol(NULL, sizeof(*bt),
1950                             SCM_BINTIME, SOL_SOCKET);
1951                         if (*controlp == NULL) {
1952                                 error = ENOBUFS;
1953                                 goto out;
1954                         }
1955                         bt = (struct bintime *)
1956                             CMSG_DATA(mtod(*controlp, struct cmsghdr *));
1957                         bintime(bt);
1958                         break;
1959
1960                 default:
1961                         error = EINVAL;
1962                         goto out;
1963                 }
1964
1965                 controlp = &(*controlp)->m_next;
1966                 if (CMSG_SPACE(datalen) < clen) {
1967                         clen -= CMSG_SPACE(datalen);
1968                         cm = (struct cmsghdr *)
1969                             ((caddr_t)cm + CMSG_SPACE(datalen));
1970                 } else {
1971                         clen = 0;
1972                         cm = NULL;
1973                 }
1974         }
1975
1976 out:
1977         m_freem(control);
1978         return (error);
1979 }
1980
1981 static struct mbuf *
1982 unp_addsockcred(struct thread *td, struct mbuf *control)
1983 {
1984         struct mbuf *m, *n, *n_prev;
1985         struct sockcred *sc;
1986         const struct cmsghdr *cm;
1987         int ngroups;
1988         int i;
1989
1990         ngroups = MIN(td->td_ucred->cr_ngroups, CMGROUP_MAX);
1991         m = sbcreatecontrol(NULL, SOCKCREDSIZE(ngroups), SCM_CREDS, SOL_SOCKET);
1992         if (m == NULL)
1993                 return (control);
1994
1995         sc = (struct sockcred *) CMSG_DATA(mtod(m, struct cmsghdr *));
1996         sc->sc_uid = td->td_ucred->cr_ruid;
1997         sc->sc_euid = td->td_ucred->cr_uid;
1998         sc->sc_gid = td->td_ucred->cr_rgid;
1999         sc->sc_egid = td->td_ucred->cr_gid;
2000         sc->sc_ngroups = ngroups;
2001         for (i = 0; i < sc->sc_ngroups; i++)
2002                 sc->sc_groups[i] = td->td_ucred->cr_groups[i];
2003
2004         /*
2005          * Unlink SCM_CREDS control messages (struct cmsgcred), since just
2006          * created SCM_CREDS control message (struct sockcred) has another
2007          * format.
2008          */
2009         if (control != NULL)
2010                 for (n = control, n_prev = NULL; n != NULL;) {
2011                         cm = mtod(n, struct cmsghdr *);
2012                         if (cm->cmsg_level == SOL_SOCKET &&
2013                             cm->cmsg_type == SCM_CREDS) {
2014                                 if (n_prev == NULL)
2015                                         control = n->m_next;
2016                                 else
2017                                         n_prev->m_next = n->m_next;
2018                                 n = m_free(n);
2019                         } else {
2020                                 n_prev = n;
2021                                 n = n->m_next;
2022                         }
2023                 }
2024
2025         /* Prepend it to the head. */
2026         m->m_next = control;
2027         return (m);
2028 }
2029
2030 static struct unpcb *
2031 fptounp(struct file *fp)
2032 {
2033         struct socket *so;
2034
2035         if (fp->f_type != DTYPE_SOCKET)
2036                 return (NULL);
2037         if ((so = fp->f_data) == NULL)
2038                 return (NULL);
2039         if (so->so_proto->pr_domain != &localdomain)
2040                 return (NULL);
2041         return sotounpcb(so);
2042 }
2043
2044 static void
2045 unp_discard(struct file *fp)
2046 {
2047         struct unp_defer *dr;
2048
2049         if (unp_externalize_fp(fp)) {
2050                 dr = malloc(sizeof(*dr), M_TEMP, M_WAITOK);
2051                 dr->ud_fp = fp;
2052                 UNP_DEFERRED_LOCK();
2053                 SLIST_INSERT_HEAD(&unp_defers, dr, ud_link);
2054                 UNP_DEFERRED_UNLOCK();
2055                 atomic_add_int(&unp_defers_count, 1);
2056                 taskqueue_enqueue(taskqueue_thread, &unp_defer_task);
2057         } else
2058                 (void) closef(fp, (struct thread *)NULL);
2059 }
2060
2061 static void
2062 unp_process_defers(void *arg __unused, int pending)
2063 {
2064         struct unp_defer *dr;
2065         SLIST_HEAD(, unp_defer) drl;
2066         int count;
2067
2068         SLIST_INIT(&drl);
2069         for (;;) {
2070                 UNP_DEFERRED_LOCK();
2071                 if (SLIST_FIRST(&unp_defers) == NULL) {
2072                         UNP_DEFERRED_UNLOCK();
2073                         break;
2074                 }
2075                 SLIST_SWAP(&unp_defers, &drl, unp_defer);
2076                 UNP_DEFERRED_UNLOCK();
2077                 count = 0;
2078                 while ((dr = SLIST_FIRST(&drl)) != NULL) {
2079                         SLIST_REMOVE_HEAD(&drl, ud_link);
2080                         closef(dr->ud_fp, NULL);
2081                         free(dr, M_TEMP);
2082                         count++;
2083                 }
2084                 atomic_add_int(&unp_defers_count, -count);
2085         }
2086 }
2087
2088 static void
2089 unp_internalize_fp(struct file *fp)
2090 {
2091         struct unpcb *unp;
2092
2093         UNP_LINK_WLOCK();
2094         if ((unp = fptounp(fp)) != NULL) {
2095                 unp->unp_file = fp;
2096                 unp->unp_msgcount++;
2097         }
2098         fhold(fp);
2099         unp_rights++;
2100         UNP_LINK_WUNLOCK();
2101 }
2102
2103 static int
2104 unp_externalize_fp(struct file *fp)
2105 {
2106         struct unpcb *unp;
2107         int ret;
2108
2109         UNP_LINK_WLOCK();
2110         if ((unp = fptounp(fp)) != NULL) {
2111                 unp->unp_msgcount--;
2112                 ret = 1;
2113         } else
2114                 ret = 0;
2115         unp_rights--;
2116         UNP_LINK_WUNLOCK();
2117         return (ret);
2118 }
2119
2120 /*
2121  * unp_defer indicates whether additional work has been defered for a future
2122  * pass through unp_gc().  It is thread local and does not require explicit
2123  * synchronization.
2124  */
2125 static int      unp_marked;
2126 static int      unp_unreachable;
2127
2128 static void
2129 unp_accessable(struct file *fp)
2130 {
2131         struct unpcb *unp;
2132
2133         if ((unp = fptounp(fp)) == NULL)
2134                 return;
2135         if (unp->unp_gcflag & UNPGC_REF)
2136                 return;
2137         unp->unp_gcflag &= ~UNPGC_DEAD;
2138         unp->unp_gcflag |= UNPGC_REF;
2139         unp_marked++;
2140 }
2141
2142 static void
2143 unp_gc_process(struct unpcb *unp)
2144 {
2145         struct socket *soa;
2146         struct socket *so;
2147         struct file *fp;
2148
2149         /* Already processed. */
2150         if (unp->unp_gcflag & UNPGC_SCANNED)
2151                 return;
2152         fp = unp->unp_file;
2153
2154         /*
2155          * Check for a socket potentially in a cycle.  It must be in a
2156          * queue as indicated by msgcount, and this must equal the file
2157          * reference count.  Note that when msgcount is 0 the file is NULL.
2158          */
2159         if ((unp->unp_gcflag & UNPGC_REF) == 0 && fp &&
2160             unp->unp_msgcount != 0 && fp->f_count == unp->unp_msgcount) {
2161                 unp->unp_gcflag |= UNPGC_DEAD;
2162                 unp_unreachable++;
2163                 return;
2164         }
2165
2166         /*
2167          * Mark all sockets we reference with RIGHTS.
2168          */
2169         so = unp->unp_socket;
2170         SOCKBUF_LOCK(&so->so_rcv);
2171         unp_scan(so->so_rcv.sb_mb, unp_accessable);
2172         SOCKBUF_UNLOCK(&so->so_rcv);
2173
2174         /*
2175          * Mark all sockets in our accept queue.
2176          */
2177         ACCEPT_LOCK();
2178         TAILQ_FOREACH(soa, &so->so_comp, so_list) {
2179                 SOCKBUF_LOCK(&soa->so_rcv);
2180                 unp_scan(soa->so_rcv.sb_mb, unp_accessable);
2181                 SOCKBUF_UNLOCK(&soa->so_rcv);
2182         }
2183         ACCEPT_UNLOCK();
2184         unp->unp_gcflag |= UNPGC_SCANNED;
2185 }
2186
2187 static int unp_recycled;
2188 SYSCTL_INT(_net_local, OID_AUTO, recycled, CTLFLAG_RD, &unp_recycled, 0, 
2189     "Number of unreachable sockets claimed by the garbage collector.");
2190
2191 static int unp_taskcount;
2192 SYSCTL_INT(_net_local, OID_AUTO, taskcount, CTLFLAG_RD, &unp_taskcount, 0, 
2193     "Number of times the garbage collector has run.");
2194
2195 static void
2196 unp_gc(__unused void *arg, int pending)
2197 {
2198         struct unp_head *heads[] = { &unp_dhead, &unp_shead, &unp_sphead,
2199                                     NULL };
2200         struct unp_head **head;
2201         struct file *f, **unref;
2202         struct unpcb *unp;
2203         int i, total;
2204
2205         unp_taskcount++;
2206         UNP_LIST_LOCK();
2207         /*
2208          * First clear all gc flags from previous runs.
2209          */
2210         for (head = heads; *head != NULL; head++)
2211                 LIST_FOREACH(unp, *head, unp_link)
2212                         unp->unp_gcflag = 0;
2213
2214         /*
2215          * Scan marking all reachable sockets with UNPGC_REF.  Once a socket
2216          * is reachable all of the sockets it references are reachable.
2217          * Stop the scan once we do a complete loop without discovering
2218          * a new reachable socket.
2219          */
2220         do {
2221                 unp_unreachable = 0;
2222                 unp_marked = 0;
2223                 for (head = heads; *head != NULL; head++)
2224                         LIST_FOREACH(unp, *head, unp_link)
2225                                 unp_gc_process(unp);
2226         } while (unp_marked);
2227         UNP_LIST_UNLOCK();
2228         if (unp_unreachable == 0)
2229                 return;
2230
2231         /*
2232          * Allocate space for a local list of dead unpcbs.
2233          */
2234         unref = malloc(unp_unreachable * sizeof(struct file *),
2235             M_TEMP, M_WAITOK);
2236
2237         /*
2238          * Iterate looking for sockets which have been specifically marked
2239          * as as unreachable and store them locally.
2240          */
2241         UNP_LINK_RLOCK();
2242         UNP_LIST_LOCK();
2243         for (total = 0, head = heads; *head != NULL; head++)
2244                 LIST_FOREACH(unp, *head, unp_link)
2245                         if ((unp->unp_gcflag & UNPGC_DEAD) != 0) {
2246                                 f = unp->unp_file;
2247                                 if (unp->unp_msgcount == 0 || f == NULL ||
2248                                     f->f_count != unp->unp_msgcount)
2249                                         continue;
2250                                 unref[total++] = f;
2251                                 fhold(f);
2252                                 KASSERT(total <= unp_unreachable,
2253                                     ("unp_gc: incorrect unreachable count."));
2254                         }
2255         UNP_LIST_UNLOCK();
2256         UNP_LINK_RUNLOCK();
2257
2258         /*
2259          * Now flush all sockets, free'ing rights.  This will free the
2260          * struct files associated with these sockets but leave each socket
2261          * with one remaining ref.
2262          */
2263         for (i = 0; i < total; i++) {
2264                 struct socket *so;
2265
2266                 so = unref[i]->f_data;
2267                 CURVNET_SET(so->so_vnet);
2268                 sorflush(so);
2269                 CURVNET_RESTORE();
2270         }
2271
2272         /*
2273          * And finally release the sockets so they can be reclaimed.
2274          */
2275         for (i = 0; i < total; i++)
2276                 fdrop(unref[i], NULL);
2277         unp_recycled += total;
2278         free(unref, M_TEMP);
2279 }
2280
2281 static void
2282 unp_dispose(struct mbuf *m)
2283 {
2284
2285         if (m)
2286                 unp_scan(m, unp_discard);
2287 }
2288
2289 static void
2290 unp_scan(struct mbuf *m0, void (*op)(struct file *))
2291 {
2292         struct mbuf *m;
2293         struct filedescent *fdep;
2294         struct cmsghdr *cm;
2295         void *data;
2296         int i;
2297         socklen_t clen, datalen;
2298         int qfds;
2299
2300         while (m0 != NULL) {
2301                 for (m = m0; m; m = m->m_next) {
2302                         if (m->m_type != MT_CONTROL)
2303                                 continue;
2304
2305                         cm = mtod(m, struct cmsghdr *);
2306                         clen = m->m_len;
2307
2308                         while (cm != NULL) {
2309                                 if (sizeof(*cm) > clen || cm->cmsg_len > clen)
2310                                         break;
2311
2312                                 data = CMSG_DATA(cm);
2313                                 datalen = (caddr_t)cm + cm->cmsg_len
2314                                     - (caddr_t)data;
2315
2316                                 if (cm->cmsg_level == SOL_SOCKET &&
2317                                     cm->cmsg_type == SCM_RIGHTS) {
2318                                         qfds = datalen / sizeof(*fdep);
2319                                         fdep = data;
2320                                         for (i = 0; i < qfds; i++, fdep++)
2321                                                 (*op)(fdep->fde_file);
2322                                 }
2323
2324                                 if (CMSG_SPACE(datalen) < clen) {
2325                                         clen -= CMSG_SPACE(datalen);
2326                                         cm = (struct cmsghdr *)
2327                                             ((caddr_t)cm + CMSG_SPACE(datalen));
2328                                 } else {
2329                                         clen = 0;
2330                                         cm = NULL;
2331                                 }
2332                         }
2333                 }
2334                 m0 = m0->m_act;
2335         }
2336 }
2337
2338 /*
2339  * A helper function called by VFS before socket-type vnode reclamation.
2340  * For an active vnode it clears unp_vnode pointer and decrements unp_vnode
2341  * use count.
2342  */
2343 void
2344 vfs_unp_reclaim(struct vnode *vp)
2345 {
2346         struct socket *so;
2347         struct unpcb *unp;
2348         int active;
2349
2350         ASSERT_VOP_ELOCKED(vp, "vfs_unp_reclaim");
2351         KASSERT(vp->v_type == VSOCK,
2352             ("vfs_unp_reclaim: vp->v_type != VSOCK"));
2353
2354         active = 0;
2355         UNP_LINK_WLOCK();
2356         VOP_UNP_CONNECT(vp, &so);
2357         if (so == NULL)
2358                 goto done;
2359         unp = sotounpcb(so);
2360         if (unp == NULL)
2361                 goto done;
2362         UNP_PCB_LOCK(unp);
2363         if (unp->unp_vnode == vp) {
2364                 VOP_UNP_DETACH(vp);
2365                 unp->unp_vnode = NULL;
2366                 active = 1;
2367         }
2368         UNP_PCB_UNLOCK(unp);
2369 done:
2370         UNP_LINK_WUNLOCK();
2371         if (active)
2372                 vunref(vp);
2373 }
2374
2375 #ifdef DDB
2376 static void
2377 db_print_indent(int indent)
2378 {
2379         int i;
2380
2381         for (i = 0; i < indent; i++)
2382                 db_printf(" ");
2383 }
2384
2385 static void
2386 db_print_unpflags(int unp_flags)
2387 {
2388         int comma;
2389
2390         comma = 0;
2391         if (unp_flags & UNP_HAVEPC) {
2392                 db_printf("%sUNP_HAVEPC", comma ? ", " : "");
2393                 comma = 1;
2394         }
2395         if (unp_flags & UNP_HAVEPCCACHED) {
2396                 db_printf("%sUNP_HAVEPCCACHED", comma ? ", " : "");
2397                 comma = 1;
2398         }
2399         if (unp_flags & UNP_WANTCRED) {
2400                 db_printf("%sUNP_WANTCRED", comma ? ", " : "");
2401                 comma = 1;
2402         }
2403         if (unp_flags & UNP_CONNWAIT) {
2404                 db_printf("%sUNP_CONNWAIT", comma ? ", " : "");
2405                 comma = 1;
2406         }
2407         if (unp_flags & UNP_CONNECTING) {
2408                 db_printf("%sUNP_CONNECTING", comma ? ", " : "");
2409                 comma = 1;
2410         }
2411         if (unp_flags & UNP_BINDING) {
2412                 db_printf("%sUNP_BINDING", comma ? ", " : "");
2413                 comma = 1;
2414         }
2415 }
2416
2417 static void
2418 db_print_xucred(int indent, struct xucred *xu)
2419 {
2420         int comma, i;
2421
2422         db_print_indent(indent);
2423         db_printf("cr_version: %u   cr_uid: %u   cr_ngroups: %d\n",
2424             xu->cr_version, xu->cr_uid, xu->cr_ngroups);
2425         db_print_indent(indent);
2426         db_printf("cr_groups: ");
2427         comma = 0;
2428         for (i = 0; i < xu->cr_ngroups; i++) {
2429                 db_printf("%s%u", comma ? ", " : "", xu->cr_groups[i]);
2430                 comma = 1;
2431         }
2432         db_printf("\n");
2433 }
2434
2435 static void
2436 db_print_unprefs(int indent, struct unp_head *uh)
2437 {
2438         struct unpcb *unp;
2439         int counter;
2440
2441         counter = 0;
2442         LIST_FOREACH(unp, uh, unp_reflink) {
2443                 if (counter % 4 == 0)
2444                         db_print_indent(indent);
2445                 db_printf("%p  ", unp);
2446                 if (counter % 4 == 3)
2447                         db_printf("\n");
2448                 counter++;
2449         }
2450         if (counter != 0 && counter % 4 != 0)
2451                 db_printf("\n");
2452 }
2453
2454 DB_SHOW_COMMAND(unpcb, db_show_unpcb)
2455 {
2456         struct unpcb *unp;
2457
2458         if (!have_addr) {
2459                 db_printf("usage: show unpcb <addr>\n");
2460                 return;
2461         }
2462         unp = (struct unpcb *)addr;
2463
2464         db_printf("unp_socket: %p   unp_vnode: %p\n", unp->unp_socket,
2465             unp->unp_vnode);
2466
2467         db_printf("unp_ino: %ju   unp_conn: %p\n", (uintmax_t)unp->unp_ino,
2468             unp->unp_conn);
2469
2470         db_printf("unp_refs:\n");
2471         db_print_unprefs(2, &unp->unp_refs);
2472
2473         /* XXXRW: Would be nice to print the full address, if any. */
2474         db_printf("unp_addr: %p\n", unp->unp_addr);
2475
2476         db_printf("unp_cc: %d   unp_mbcnt: %d   unp_gencnt: %llu\n",
2477             unp->unp_cc, unp->unp_mbcnt,
2478             (unsigned long long)unp->unp_gencnt);
2479
2480         db_printf("unp_flags: %x (", unp->unp_flags);
2481         db_print_unpflags(unp->unp_flags);
2482         db_printf(")\n");
2483
2484         db_printf("unp_peercred:\n");
2485         db_print_xucred(2, &unp->unp_peercred);
2486
2487         db_printf("unp_refcount: %u\n", unp->unp_refcount);
2488 }
2489 #endif