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