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