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