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