uipc_usrreq.c revision 1.99 1 /* $NetBSD: uipc_usrreq.c,v 1.99 2007/08/09 15:23:02 he Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 2000, 2004 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * Copyright (c) 1982, 1986, 1989, 1991, 1993
42 * The Regents of the University of California. All rights reserved.
43 *
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
47 * 1. Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 * notice, this list of conditions and the following disclaimer in the
51 * documentation and/or other materials provided with the distribution.
52 * 3. Neither the name of the University nor the names of its contributors
53 * may be used to endorse or promote products derived from this software
54 * without specific prior written permission.
55 *
56 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * SUCH DAMAGE.
67 *
68 * @(#)uipc_usrreq.c 8.9 (Berkeley) 5/14/95
69 */
70
71 /*
72 * Copyright (c) 1997 Christopher G. Demetriou. All rights reserved.
73 *
74 * Redistribution and use in source and binary forms, with or without
75 * modification, are permitted provided that the following conditions
76 * are met:
77 * 1. Redistributions of source code must retain the above copyright
78 * notice, this list of conditions and the following disclaimer.
79 * 2. Redistributions in binary form must reproduce the above copyright
80 * notice, this list of conditions and the following disclaimer in the
81 * documentation and/or other materials provided with the distribution.
82 * 3. All advertising materials mentioning features or use of this software
83 * must display the following acknowledgement:
84 * This product includes software developed by the University of
85 * California, Berkeley and its contributors.
86 * 4. Neither the name of the University nor the names of its contributors
87 * may be used to endorse or promote products derived from this software
88 * without specific prior written permission.
89 *
90 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
91 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
92 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
93 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
94 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
95 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
96 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
97 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
98 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
99 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
100 * SUCH DAMAGE.
101 *
102 * @(#)uipc_usrreq.c 8.9 (Berkeley) 5/14/95
103 */
104
105 #include <sys/cdefs.h>
106 __KERNEL_RCSID(0, "$NetBSD: uipc_usrreq.c,v 1.99 2007/08/09 15:23:02 he Exp $");
107
108 #include <sys/param.h>
109 #include <sys/systm.h>
110 #include <sys/proc.h>
111 #include <sys/filedesc.h>
112 #include <sys/domain.h>
113 #include <sys/protosw.h>
114 #include <sys/socket.h>
115 #include <sys/socketvar.h>
116 #include <sys/unpcb.h>
117 #include <sys/un.h>
118 #include <sys/namei.h>
119 #include <sys/vnode.h>
120 #include <sys/file.h>
121 #include <sys/stat.h>
122 #include <sys/mbuf.h>
123 #include <sys/kauth.h>
124
125 /*
126 * Unix communications domain.
127 *
128 * TODO:
129 * SEQPACKET, RDM
130 * rethink name space problems
131 * need a proper out-of-band
132 */
133 const struct sockaddr_un sun_noname = {
134 .sun_len = sizeof(sun_noname),
135 .sun_family = AF_LOCAL,
136 };
137 ino_t unp_ino; /* prototype for fake inode numbers */
138
139 struct mbuf *unp_addsockcred(struct lwp *, struct mbuf *);
140
141 int
142 unp_output(struct mbuf *m, struct mbuf *control, struct unpcb *unp,
143 struct lwp *l)
144 {
145 struct socket *so2;
146 const struct sockaddr_un *sun;
147
148 so2 = unp->unp_conn->unp_socket;
149 if (unp->unp_addr)
150 sun = unp->unp_addr;
151 else
152 sun = &sun_noname;
153 if (unp->unp_conn->unp_flags & UNP_WANTCRED)
154 control = unp_addsockcred(l, control);
155 if (sbappendaddr(&so2->so_rcv, (const struct sockaddr *)sun, m,
156 control) == 0) {
157 unp_dispose(control);
158 m_freem(control);
159 m_freem(m);
160 so2->so_rcv.sb_overflowed++;
161 return (ENOBUFS);
162 } else {
163 sorwakeup(so2);
164 return (0);
165 }
166 }
167
168 void
169 unp_setsockaddr(struct unpcb *unp, struct mbuf *nam)
170 {
171 const struct sockaddr_un *sun;
172
173 if (unp->unp_addr)
174 sun = unp->unp_addr;
175 else
176 sun = &sun_noname;
177 nam->m_len = sun->sun_len;
178 if (nam->m_len > MLEN)
179 MEXTMALLOC(nam, nam->m_len, M_WAITOK);
180 memcpy(mtod(nam, void *), sun, (size_t)nam->m_len);
181 }
182
183 void
184 unp_setpeeraddr(struct unpcb *unp, struct mbuf *nam)
185 {
186 const struct sockaddr_un *sun;
187
188 if (unp->unp_conn && unp->unp_conn->unp_addr)
189 sun = unp->unp_conn->unp_addr;
190 else
191 sun = &sun_noname;
192 nam->m_len = sun->sun_len;
193 if (nam->m_len > MLEN)
194 MEXTMALLOC(nam, nam->m_len, M_WAITOK);
195 memcpy(mtod(nam, void *), sun, (size_t)nam->m_len);
196 }
197
198 /*ARGSUSED*/
199 int
200 uipc_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
201 struct mbuf *control, struct lwp *l)
202 {
203 struct unpcb *unp = sotounpcb(so);
204 struct socket *so2;
205 struct proc *p;
206 u_int newhiwat;
207 int error = 0;
208
209 if (req == PRU_CONTROL)
210 return (EOPNOTSUPP);
211
212 #ifdef DIAGNOSTIC
213 if (req != PRU_SEND && req != PRU_SENDOOB && control)
214 panic("uipc_usrreq: unexpected control mbuf");
215 #endif
216 p = l ? l->l_proc : NULL;
217 if (unp == 0 && req != PRU_ATTACH) {
218 error = EINVAL;
219 goto release;
220 }
221
222 switch (req) {
223
224 case PRU_ATTACH:
225 if (unp != 0) {
226 error = EISCONN;
227 break;
228 }
229 error = unp_attach(so);
230 break;
231
232 case PRU_DETACH:
233 unp_detach(unp);
234 break;
235
236 case PRU_BIND:
237 KASSERT(l != NULL);
238 error = unp_bind(unp, nam, l);
239 break;
240
241 case PRU_LISTEN:
242 if (unp->unp_vnode == 0)
243 error = EINVAL;
244 break;
245
246 case PRU_CONNECT:
247 KASSERT(l != NULL);
248 error = unp_connect(so, nam, l);
249 break;
250
251 case PRU_CONNECT2:
252 error = unp_connect2(so, (struct socket *)nam, PRU_CONNECT2);
253 break;
254
255 case PRU_DISCONNECT:
256 unp_disconnect(unp);
257 break;
258
259 case PRU_ACCEPT:
260 unp_setpeeraddr(unp, nam);
261 /*
262 * Mark the initiating STREAM socket as connected *ONLY*
263 * after it's been accepted. This prevents a client from
264 * overrunning a server and receiving ECONNREFUSED.
265 */
266 if (unp->unp_conn != NULL &&
267 (unp->unp_conn->unp_socket->so_state & SS_ISCONNECTING))
268 soisconnected(unp->unp_conn->unp_socket);
269 break;
270
271 case PRU_SHUTDOWN:
272 socantsendmore(so);
273 unp_shutdown(unp);
274 break;
275
276 case PRU_RCVD:
277 switch (so->so_type) {
278
279 case SOCK_DGRAM:
280 panic("uipc 1");
281 /*NOTREACHED*/
282
283 case SOCK_STREAM:
284 #define rcv (&so->so_rcv)
285 #define snd (&so2->so_snd)
286 if (unp->unp_conn == 0)
287 break;
288 so2 = unp->unp_conn->unp_socket;
289 /*
290 * Adjust backpressure on sender
291 * and wakeup any waiting to write.
292 */
293 snd->sb_mbmax += unp->unp_mbcnt - rcv->sb_mbcnt;
294 unp->unp_mbcnt = rcv->sb_mbcnt;
295 newhiwat = snd->sb_hiwat + unp->unp_cc - rcv->sb_cc;
296 (void)chgsbsize(so2->so_uidinfo,
297 &snd->sb_hiwat, newhiwat, RLIM_INFINITY);
298 unp->unp_cc = rcv->sb_cc;
299 sowwakeup(so2);
300 #undef snd
301 #undef rcv
302 break;
303
304 default:
305 panic("uipc 2");
306 }
307 break;
308
309 case PRU_SEND:
310 /*
311 * Note: unp_internalize() rejects any control message
312 * other than SCM_RIGHTS, and only allows one. This
313 * has the side-effect of preventing a caller from
314 * forging SCM_CREDS.
315 */
316 if (control) {
317 KASSERT(l != NULL);
318 if ((error = unp_internalize(control, l)) != 0)
319 goto die;
320 }
321 switch (so->so_type) {
322
323 case SOCK_DGRAM: {
324 if (nam) {
325 if ((so->so_state & SS_ISCONNECTED) != 0) {
326 error = EISCONN;
327 goto die;
328 }
329 KASSERT(l != NULL);
330 error = unp_connect(so, nam, l);
331 if (error) {
332 die:
333 unp_dispose(control);
334 m_freem(control);
335 m_freem(m);
336 break;
337 }
338 } else {
339 if ((so->so_state & SS_ISCONNECTED) == 0) {
340 error = ENOTCONN;
341 goto die;
342 }
343 }
344 KASSERT(p != NULL);
345 error = unp_output(m, control, unp, l);
346 if (nam)
347 unp_disconnect(unp);
348 break;
349 }
350
351 case SOCK_STREAM:
352 #define rcv (&so2->so_rcv)
353 #define snd (&so->so_snd)
354 if (unp->unp_conn == NULL) {
355 error = ENOTCONN;
356 break;
357 }
358 so2 = unp->unp_conn->unp_socket;
359 if (unp->unp_conn->unp_flags & UNP_WANTCRED) {
360 /*
361 * Credentials are passed only once on
362 * SOCK_STREAM.
363 */
364 unp->unp_conn->unp_flags &= ~UNP_WANTCRED;
365 control = unp_addsockcred(l, control);
366 }
367 /*
368 * Send to paired receive port, and then reduce
369 * send buffer hiwater marks to maintain backpressure.
370 * Wake up readers.
371 */
372 if (control) {
373 if (sbappendcontrol(rcv, m, control) == 0) {
374 unp_dispose(control);
375 m_freem(control);
376 }
377 } else
378 sbappend(rcv, m);
379 snd->sb_mbmax -=
380 rcv->sb_mbcnt - unp->unp_conn->unp_mbcnt;
381 unp->unp_conn->unp_mbcnt = rcv->sb_mbcnt;
382 newhiwat = snd->sb_hiwat -
383 (rcv->sb_cc - unp->unp_conn->unp_cc);
384 (void)chgsbsize(so->so_uidinfo,
385 &snd->sb_hiwat, newhiwat, RLIM_INFINITY);
386 unp->unp_conn->unp_cc = rcv->sb_cc;
387 sorwakeup(so2);
388 #undef snd
389 #undef rcv
390 break;
391
392 default:
393 panic("uipc 4");
394 }
395 break;
396
397 case PRU_ABORT:
398 unp_drop(unp, ECONNABORTED);
399
400 KASSERT(so->so_head == NULL);
401 #ifdef DIAGNOSTIC
402 if (so->so_pcb == 0)
403 panic("uipc 5: drop killed pcb");
404 #endif
405 unp_detach(unp);
406 break;
407
408 case PRU_SENSE:
409 ((struct stat *) m)->st_blksize = so->so_snd.sb_hiwat;
410 if (so->so_type == SOCK_STREAM && unp->unp_conn != 0) {
411 so2 = unp->unp_conn->unp_socket;
412 ((struct stat *) m)->st_blksize += so2->so_rcv.sb_cc;
413 }
414 ((struct stat *) m)->st_dev = NODEV;
415 if (unp->unp_ino == 0)
416 unp->unp_ino = unp_ino++;
417 ((struct stat *) m)->st_atimespec =
418 ((struct stat *) m)->st_mtimespec =
419 ((struct stat *) m)->st_ctimespec = unp->unp_ctime;
420 ((struct stat *) m)->st_ino = unp->unp_ino;
421 return (0);
422
423 case PRU_RCVOOB:
424 error = EOPNOTSUPP;
425 break;
426
427 case PRU_SENDOOB:
428 m_freem(control);
429 m_freem(m);
430 error = EOPNOTSUPP;
431 break;
432
433 case PRU_SOCKADDR:
434 unp_setsockaddr(unp, nam);
435 break;
436
437 case PRU_PEERADDR:
438 unp_setpeeraddr(unp, nam);
439 break;
440
441 default:
442 panic("piusrreq");
443 }
444
445 release:
446 return (error);
447 }
448
449 /*
450 * Unix domain socket option processing.
451 */
452 int
453 uipc_ctloutput(int op, struct socket *so, int level, int optname,
454 struct mbuf **mp)
455 {
456 struct unpcb *unp = sotounpcb(so);
457 struct mbuf *m = *mp;
458 int optval = 0, error = 0;
459
460 if (level != 0) {
461 error = EINVAL;
462 if (op == PRCO_SETOPT && m)
463 (void) m_free(m);
464 } else switch (op) {
465
466 case PRCO_SETOPT:
467 switch (optname) {
468 case LOCAL_CREDS:
469 case LOCAL_CONNWAIT:
470 if (m == NULL || m->m_len != sizeof(int))
471 error = EINVAL;
472 else {
473 optval = *mtod(m, int *);
474 switch (optname) {
475 #define OPTSET(bit) \
476 if (optval) \
477 unp->unp_flags |= (bit); \
478 else \
479 unp->unp_flags &= ~(bit);
480
481 case LOCAL_CREDS:
482 OPTSET(UNP_WANTCRED);
483 break;
484 case LOCAL_CONNWAIT:
485 OPTSET(UNP_CONNWAIT);
486 break;
487 }
488 }
489 break;
490 #undef OPTSET
491
492 default:
493 error = ENOPROTOOPT;
494 break;
495 }
496 if (m)
497 (void) m_free(m);
498 break;
499
500 case PRCO_GETOPT:
501 switch (optname) {
502 case LOCAL_PEEREID:
503 if (unp->unp_flags & UNP_EIDSVALID) {
504 *mp = m = m_get(M_WAIT, MT_SOOPTS);
505 m->m_len = sizeof(struct unpcbid);
506 *mtod(m, struct unpcbid *) = unp->unp_connid;
507 } else {
508 error = EINVAL;
509 }
510 break;
511 case LOCAL_CREDS:
512 *mp = m = m_get(M_WAIT, MT_SOOPTS);
513 m->m_len = sizeof(int);
514
515 #define OPTBIT(bit) (unp->unp_flags & (bit) ? 1 : 0)
516
517 optval = OPTBIT(UNP_WANTCRED);
518 *mtod(m, int *) = optval;
519 break;
520 #undef OPTBIT
521
522 default:
523 error = ENOPROTOOPT;
524 break;
525 }
526 break;
527 }
528 return (error);
529 }
530
531 /*
532 * Both send and receive buffers are allocated PIPSIZ bytes of buffering
533 * for stream sockets, although the total for sender and receiver is
534 * actually only PIPSIZ.
535 * Datagram sockets really use the sendspace as the maximum datagram size,
536 * and don't really want to reserve the sendspace. Their recvspace should
537 * be large enough for at least one max-size datagram plus address.
538 */
539 #define PIPSIZ 4096
540 u_long unpst_sendspace = PIPSIZ;
541 u_long unpst_recvspace = PIPSIZ;
542 u_long unpdg_sendspace = 2*1024; /* really max datagram size */
543 u_long unpdg_recvspace = 4*1024;
544
545 int unp_rights; /* file descriptors in flight */
546
547 int
548 unp_attach(struct socket *so)
549 {
550 struct unpcb *unp;
551 int error;
552
553 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
554 switch (so->so_type) {
555
556 case SOCK_STREAM:
557 error = soreserve(so, unpst_sendspace, unpst_recvspace);
558 break;
559
560 case SOCK_DGRAM:
561 error = soreserve(so, unpdg_sendspace, unpdg_recvspace);
562 break;
563
564 default:
565 panic("unp_attach");
566 }
567 if (error)
568 return (error);
569 }
570 unp = malloc(sizeof(*unp), M_PCB, M_NOWAIT);
571 if (unp == NULL)
572 return (ENOBUFS);
573 memset((void *)unp, 0, sizeof(*unp));
574 unp->unp_socket = so;
575 so->so_pcb = unp;
576 nanotime(&unp->unp_ctime);
577 return (0);
578 }
579
580 void
581 unp_detach(struct unpcb *unp)
582 {
583
584 if (unp->unp_vnode) {
585 unp->unp_vnode->v_socket = 0;
586 vrele(unp->unp_vnode);
587 unp->unp_vnode = 0;
588 }
589 if (unp->unp_conn)
590 unp_disconnect(unp);
591 while (unp->unp_refs)
592 unp_drop(unp->unp_refs, ECONNRESET);
593 soisdisconnected(unp->unp_socket);
594 unp->unp_socket->so_pcb = 0;
595 if (unp->unp_addr)
596 free(unp->unp_addr, M_SONAME);
597 if (unp_rights) {
598 /*
599 * Normally the receive buffer is flushed later,
600 * in sofree, but if our receive buffer holds references
601 * to descriptors that are now garbage, we will dispose
602 * of those descriptor references after the garbage collector
603 * gets them (resulting in a "panic: closef: count < 0").
604 */
605 sorflush(unp->unp_socket);
606 free(unp, M_PCB);
607 unp_gc();
608 } else
609 free(unp, M_PCB);
610 }
611
612 int
613 unp_bind(struct unpcb *unp, struct mbuf *nam, struct lwp *l)
614 {
615 struct sockaddr_un *sun;
616 struct vnode *vp;
617 struct vattr vattr;
618 size_t addrlen;
619 struct proc *p;
620 int error;
621 struct nameidata nd;
622
623 if (unp->unp_vnode != 0)
624 return (EINVAL);
625
626 p = l->l_proc;
627 /*
628 * Allocate the new sockaddr. We have to allocate one
629 * extra byte so that we can ensure that the pathname
630 * is nul-terminated.
631 */
632 addrlen = nam->m_len + 1;
633 sun = malloc(addrlen, M_SONAME, M_WAITOK);
634 m_copydata(nam, 0, nam->m_len, (void *)sun);
635 *(((char *)sun) + nam->m_len) = '\0';
636
637 NDINIT(&nd, CREATE, FOLLOW | LOCKPARENT | TRYEMULROOT, UIO_SYSSPACE,
638 sun->sun_path, l);
639
640 /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
641 if ((error = namei(&nd)) != 0)
642 goto bad;
643 vp = nd.ni_vp;
644 if (vp != NULL) {
645 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
646 if (nd.ni_dvp == vp)
647 vrele(nd.ni_dvp);
648 else
649 vput(nd.ni_dvp);
650 vrele(vp);
651 error = EADDRINUSE;
652 goto bad;
653 }
654 VATTR_NULL(&vattr);
655 vattr.va_type = VSOCK;
656 vattr.va_mode = ACCESSPERMS & ~(p->p_cwdi->cwdi_cmask);
657 VOP_LEASE(nd.ni_dvp, l, l->l_cred, LEASE_WRITE);
658 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
659 if (error)
660 goto bad;
661 vp = nd.ni_vp;
662 vp->v_socket = unp->unp_socket;
663 unp->unp_vnode = vp;
664 unp->unp_addrlen = addrlen;
665 unp->unp_addr = sun;
666 unp->unp_connid.unp_pid = p->p_pid;
667 unp->unp_connid.unp_euid = kauth_cred_geteuid(p->p_cred);
668 unp->unp_connid.unp_egid = kauth_cred_getegid(p->p_cred);
669 unp->unp_flags |= UNP_EIDSBIND;
670 VOP_UNLOCK(vp, 0);
671 return (0);
672
673 bad:
674 free(sun, M_SONAME);
675 return (error);
676 }
677
678 int
679 unp_connect(struct socket *so, struct mbuf *nam, struct lwp *l)
680 {
681 struct sockaddr_un *sun;
682 struct vnode *vp;
683 struct socket *so2, *so3;
684 struct unpcb *unp, *unp2, *unp3;
685 size_t addrlen;
686 struct proc *p;
687 int error;
688 struct nameidata nd;
689
690 p = l->l_proc;
691 /*
692 * Allocate a temporary sockaddr. We have to allocate one extra
693 * byte so that we can ensure that the pathname is nul-terminated.
694 * When we establish the connection, we copy the other PCB's
695 * sockaddr to our own.
696 */
697 addrlen = nam->m_len + 1;
698 sun = malloc(addrlen, M_SONAME, M_WAITOK);
699 m_copydata(nam, 0, nam->m_len, (void *)sun);
700 *(((char *)sun) + nam->m_len) = '\0';
701
702 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, UIO_SYSSPACE, sun->sun_path, l);
703
704 if ((error = namei(&nd)) != 0)
705 goto bad2;
706 vp = nd.ni_vp;
707 if (vp->v_type != VSOCK) {
708 error = ENOTSOCK;
709 goto bad;
710 }
711 if ((error = VOP_ACCESS(vp, VWRITE, l->l_cred, l)) != 0)
712 goto bad;
713 so2 = vp->v_socket;
714 if (so2 == 0) {
715 error = ECONNREFUSED;
716 goto bad;
717 }
718 if (so->so_type != so2->so_type) {
719 error = EPROTOTYPE;
720 goto bad;
721 }
722 if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
723 if ((so2->so_options & SO_ACCEPTCONN) == 0 ||
724 (so3 = sonewconn(so2, 0)) == 0) {
725 error = ECONNREFUSED;
726 goto bad;
727 }
728 unp = sotounpcb(so);
729 unp2 = sotounpcb(so2);
730 unp3 = sotounpcb(so3);
731 if (unp2->unp_addr) {
732 unp3->unp_addr = malloc(unp2->unp_addrlen,
733 M_SONAME, M_WAITOK);
734 memcpy(unp3->unp_addr, unp2->unp_addr,
735 unp2->unp_addrlen);
736 unp3->unp_addrlen = unp2->unp_addrlen;
737 }
738 unp3->unp_flags = unp2->unp_flags;
739 unp3->unp_connid.unp_pid = p->p_pid;
740 unp3->unp_connid.unp_euid = kauth_cred_geteuid(p->p_cred);
741 unp3->unp_connid.unp_egid = kauth_cred_getegid(p->p_cred);
742 unp3->unp_flags |= UNP_EIDSVALID;
743 so2 = so3;
744 if (unp2->unp_flags & UNP_EIDSBIND) {
745 unp->unp_connid = unp2->unp_connid;
746 unp->unp_flags |= UNP_EIDSVALID;
747 }
748 }
749 error = unp_connect2(so, so2, PRU_CONNECT);
750 bad:
751 vput(vp);
752 bad2:
753 free(sun, M_SONAME);
754 return (error);
755 }
756
757 int
758 unp_connect2(struct socket *so, struct socket *so2, int req)
759 {
760 struct unpcb *unp = sotounpcb(so);
761 struct unpcb *unp2;
762
763 if (so2->so_type != so->so_type)
764 return (EPROTOTYPE);
765 unp2 = sotounpcb(so2);
766 unp->unp_conn = unp2;
767 switch (so->so_type) {
768
769 case SOCK_DGRAM:
770 unp->unp_nextref = unp2->unp_refs;
771 unp2->unp_refs = unp;
772 soisconnected(so);
773 break;
774
775 case SOCK_STREAM:
776 unp2->unp_conn = unp;
777 if (req == PRU_CONNECT &&
778 ((unp->unp_flags | unp2->unp_flags) & UNP_CONNWAIT))
779 soisconnecting(so);
780 else
781 soisconnected(so);
782 soisconnected(so2);
783 break;
784
785 default:
786 panic("unp_connect2");
787 }
788 return (0);
789 }
790
791 void
792 unp_disconnect(struct unpcb *unp)
793 {
794 struct unpcb *unp2 = unp->unp_conn;
795
796 if (unp2 == 0)
797 return;
798 unp->unp_conn = 0;
799 switch (unp->unp_socket->so_type) {
800
801 case SOCK_DGRAM:
802 if (unp2->unp_refs == unp)
803 unp2->unp_refs = unp->unp_nextref;
804 else {
805 unp2 = unp2->unp_refs;
806 for (;;) {
807 if (unp2 == 0)
808 panic("unp_disconnect");
809 if (unp2->unp_nextref == unp)
810 break;
811 unp2 = unp2->unp_nextref;
812 }
813 unp2->unp_nextref = unp->unp_nextref;
814 }
815 unp->unp_nextref = 0;
816 unp->unp_socket->so_state &= ~SS_ISCONNECTED;
817 break;
818
819 case SOCK_STREAM:
820 soisdisconnected(unp->unp_socket);
821 unp2->unp_conn = 0;
822 soisdisconnected(unp2->unp_socket);
823 break;
824 }
825 }
826
827 #ifdef notdef
828 unp_abort(struct unpcb *unp)
829 {
830 unp_detach(unp);
831 }
832 #endif
833
834 void
835 unp_shutdown(struct unpcb *unp)
836 {
837 struct socket *so;
838
839 if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn &&
840 (so = unp->unp_conn->unp_socket))
841 socantrcvmore(so);
842 }
843
844 void
845 unp_drop(struct unpcb *unp, int errno)
846 {
847 struct socket *so = unp->unp_socket;
848
849 so->so_error = errno;
850 unp_disconnect(unp);
851 if (so->so_head) {
852 so->so_pcb = 0;
853 sofree(so);
854 if (unp->unp_addr)
855 free(unp->unp_addr, M_SONAME);
856 free(unp, M_PCB);
857 }
858 }
859
860 #ifdef notdef
861 unp_drain(void)
862 {
863
864 }
865 #endif
866
867 int
868 unp_externalize(struct mbuf *rights, struct lwp *l)
869 {
870 struct cmsghdr *cm = mtod(rights, struct cmsghdr *);
871 struct proc *p = l->l_proc;
872 int i, *fdp;
873 struct file **rp;
874 struct file *fp;
875 int nfds, error = 0;
876
877 nfds = (cm->cmsg_len - CMSG_ALIGN(sizeof(*cm))) /
878 sizeof(struct file *);
879 rp = (struct file **)CMSG_DATA(cm);
880
881 fdp = malloc(nfds * sizeof(int), M_TEMP, M_WAITOK);
882
883 /* Make sure the recipient should be able to see the descriptors.. */
884 if (p->p_cwdi->cwdi_rdir != NULL) {
885 rp = (struct file **)CMSG_DATA(cm);
886 for (i = 0; i < nfds; i++) {
887 fp = *rp++;
888 /*
889 * If we are in a chroot'ed directory, and
890 * someone wants to pass us a directory, make
891 * sure it's inside the subtree we're allowed
892 * to access.
893 */
894 if (fp->f_type == DTYPE_VNODE) {
895 struct vnode *vp = (struct vnode *)fp->f_data;
896 if ((vp->v_type == VDIR) &&
897 !vn_isunder(vp, p->p_cwdi->cwdi_rdir, l)) {
898 error = EPERM;
899 break;
900 }
901 }
902 }
903 }
904
905 restart:
906 rp = (struct file **)CMSG_DATA(cm);
907 if (error != 0) {
908 for (i = 0; i < nfds; i++) {
909 fp = *rp;
910 /*
911 * zero the pointer before calling unp_discard,
912 * since it may end up in unp_gc()..
913 */
914 *rp++ = 0;
915 unp_discard(fp);
916 }
917 goto out;
918 }
919
920 /*
921 * First loop -- allocate file descriptor table slots for the
922 * new descriptors.
923 */
924 for (i = 0; i < nfds; i++) {
925 fp = *rp++;
926 if ((error = fdalloc(p, 0, &fdp[i])) != 0) {
927 /*
928 * Back out what we've done so far.
929 */
930 for (--i; i >= 0; i--)
931 fdremove(p->p_fd, fdp[i]);
932
933 if (error == ENOSPC) {
934 fdexpand(p);
935 error = 0;
936 } else {
937 /*
938 * This is the error that has historically
939 * been returned, and some callers may
940 * expect it.
941 */
942 error = EMSGSIZE;
943 }
944 goto restart;
945 }
946
947 /*
948 * Make the slot reference the descriptor so that
949 * fdalloc() works properly.. We finalize it all
950 * in the loop below.
951 */
952 p->p_fd->fd_ofiles[fdp[i]] = fp;
953 }
954
955 /*
956 * Now that adding them has succeeded, update all of the
957 * descriptor passing state.
958 */
959 rp = (struct file **)CMSG_DATA(cm);
960 for (i = 0; i < nfds; i++) {
961 fp = *rp++;
962 fp->f_msgcount--;
963 unp_rights--;
964 }
965
966 /*
967 * Copy temporary array to message and adjust length, in case of
968 * transition from large struct file pointers to ints.
969 */
970 memcpy(CMSG_DATA(cm), fdp, nfds * sizeof(int));
971 cm->cmsg_len = CMSG_LEN(nfds * sizeof(int));
972 rights->m_len = CMSG_SPACE(nfds * sizeof(int));
973 out:
974 free(fdp, M_TEMP);
975 return (error);
976 }
977
978 int
979 unp_internalize(struct mbuf *control, struct lwp *l)
980 {
981 struct proc *p = l->l_proc;
982 struct filedesc *fdescp = p->p_fd;
983 struct cmsghdr *newcm, *cm = mtod(control, struct cmsghdr *);
984 struct file **rp, **files;
985 struct file *fp;
986 int i, fd, *fdp;
987 int nfds;
988 u_int neededspace;
989
990 /* Sanity check the control message header */
991 if (cm->cmsg_type != SCM_RIGHTS || cm->cmsg_level != SOL_SOCKET ||
992 cm->cmsg_len != control->m_len)
993 return (EINVAL);
994
995 /* Verify that the file descriptors are valid */
996 nfds = (cm->cmsg_len - CMSG_ALIGN(sizeof(*cm))) / sizeof(int);
997 fdp = (int *)CMSG_DATA(cm);
998 for (i = 0; i < nfds; i++) {
999 fd = *fdp++;
1000 if ((fp = fd_getfile(fdescp, fd)) == NULL)
1001 return (EBADF);
1002 simple_unlock(&fp->f_slock);
1003 }
1004
1005 /* Make sure we have room for the struct file pointers */
1006 neededspace = CMSG_SPACE(nfds * sizeof(struct file *)) -
1007 control->m_len;
1008 if (neededspace > M_TRAILINGSPACE(control)) {
1009
1010 /* allocate new space and copy header into it */
1011 newcm = malloc(
1012 CMSG_SPACE(nfds * sizeof(struct file *)),
1013 M_MBUF, M_WAITOK);
1014 if (newcm == NULL)
1015 return (E2BIG);
1016 memcpy(newcm, cm, sizeof(struct cmsghdr));
1017 files = (struct file **)CMSG_DATA(newcm);
1018 } else {
1019 /* we can convert in-place */
1020 newcm = NULL;
1021 files = (struct file **)CMSG_DATA(cm);
1022 }
1023
1024 /*
1025 * Transform the file descriptors into struct file pointers, in
1026 * reverse order so that if pointers are bigger than ints, the
1027 * int won't get until we're done.
1028 */
1029 fdp = (int *)CMSG_DATA(cm) + nfds;
1030 rp = files + nfds;
1031 for (i = 0; i < nfds; i++) {
1032 fp = fdescp->fd_ofiles[*--fdp];
1033 simple_lock(&fp->f_slock);
1034 #ifdef DIAGNOSTIC
1035 if (fp->f_iflags & FIF_WANTCLOSE)
1036 panic("unp_internalize: file already closed");
1037 #endif
1038 *--rp = fp;
1039 fp->f_count++;
1040 fp->f_msgcount++;
1041 simple_unlock(&fp->f_slock);
1042 unp_rights++;
1043 }
1044
1045 if (newcm) {
1046 if (control->m_flags & M_EXT)
1047 MEXTREMOVE(control);
1048 MEXTADD(control, newcm,
1049 CMSG_SPACE(nfds * sizeof(struct file *)),
1050 M_MBUF, NULL, NULL);
1051 cm = newcm;
1052 }
1053
1054 /* adjust message & mbuf to note amount of space actually used. */
1055 cm->cmsg_len = CMSG_LEN(nfds * sizeof(struct file *));
1056 control->m_len = CMSG_SPACE(nfds * sizeof(struct file *));
1057
1058 return (0);
1059 }
1060
1061 struct mbuf *
1062 unp_addsockcred(struct lwp *l, struct mbuf *control)
1063 {
1064 struct cmsghdr *cmp;
1065 struct sockcred *sc;
1066 struct mbuf *m, *n;
1067 int len, space, i;
1068
1069 len = CMSG_LEN(SOCKCREDSIZE(kauth_cred_ngroups(l->l_cred)));
1070 space = CMSG_SPACE(SOCKCREDSIZE(kauth_cred_ngroups(l->l_cred)));
1071
1072 m = m_get(M_WAIT, MT_CONTROL);
1073 if (space > MLEN) {
1074 if (space > MCLBYTES)
1075 MEXTMALLOC(m, space, M_WAITOK);
1076 else
1077 m_clget(m, M_WAIT);
1078 if ((m->m_flags & M_EXT) == 0) {
1079 m_free(m);
1080 return (control);
1081 }
1082 }
1083
1084 m->m_len = space;
1085 m->m_next = NULL;
1086 cmp = mtod(m, struct cmsghdr *);
1087 sc = (struct sockcred *)CMSG_DATA(cmp);
1088 cmp->cmsg_len = len;
1089 cmp->cmsg_level = SOL_SOCKET;
1090 cmp->cmsg_type = SCM_CREDS;
1091 sc->sc_uid = kauth_cred_getuid(l->l_cred);
1092 sc->sc_euid = kauth_cred_geteuid(l->l_cred);
1093 sc->sc_gid = kauth_cred_getgid(l->l_cred);
1094 sc->sc_egid = kauth_cred_getegid(l->l_cred);
1095 sc->sc_ngroups = kauth_cred_ngroups(l->l_cred);
1096 for (i = 0; i < sc->sc_ngroups; i++)
1097 sc->sc_groups[i] = kauth_cred_group(l->l_cred, i);
1098
1099 /*
1100 * If a control message already exists, append us to the end.
1101 */
1102 if (control != NULL) {
1103 for (n = control; n->m_next != NULL; n = n->m_next)
1104 ;
1105 n->m_next = m;
1106 } else
1107 control = m;
1108
1109 return (control);
1110 }
1111
1112 int unp_defer, unp_gcing;
1113 extern struct domain unixdomain;
1114
1115 /*
1116 * Comment added long after the fact explaining what's going on here.
1117 * Do a mark-sweep GC of file descriptors on the system, to free up
1118 * any which are caught in flight to an about-to-be-closed socket.
1119 *
1120 * Traditional mark-sweep gc's start at the "root", and mark
1121 * everything reachable from the root (which, in our case would be the
1122 * process table). The mark bits are cleared during the sweep.
1123 *
1124 * XXX For some inexplicable reason (perhaps because the file
1125 * descriptor tables used to live in the u area which could be swapped
1126 * out and thus hard to reach), we do multiple scans over the set of
1127 * descriptors, using use *two* mark bits per object (DEFER and MARK).
1128 * Whenever we find a descriptor which references other descriptors,
1129 * the ones it references are marked with both bits, and we iterate
1130 * over the whole file table until there are no more DEFER bits set.
1131 * We also make an extra pass *before* the GC to clear the mark bits,
1132 * which could have been cleared at almost no cost during the previous
1133 * sweep.
1134 *
1135 * XXX MP: this needs to run with locks such that no other thread of
1136 * control can create or destroy references to file descriptors. it
1137 * may be necessary to defer the GC until later (when the locking
1138 * situation is more hospitable); it may be necessary to push this
1139 * into a separate thread.
1140 */
1141 void
1142 unp_gc(void)
1143 {
1144 struct file *fp, *nextfp;
1145 struct socket *so, *so1;
1146 struct file **extra_ref, **fpp;
1147 int nunref, i;
1148
1149 if (unp_gcing)
1150 return;
1151 unp_gcing = 1;
1152 unp_defer = 0;
1153
1154 /* Clear mark bits */
1155 LIST_FOREACH(fp, &filehead, f_list)
1156 fp->f_flag &= ~(FMARK|FDEFER);
1157
1158 /*
1159 * Iterate over the set of descriptors, marking ones believed
1160 * (based on refcount) to be referenced from a process, and
1161 * marking for rescan descriptors which are queued on a socket.
1162 */
1163 do {
1164 LIST_FOREACH(fp, &filehead, f_list) {
1165 if (fp->f_flag & FDEFER) {
1166 fp->f_flag &= ~FDEFER;
1167 unp_defer--;
1168 #ifdef DIAGNOSTIC
1169 if (fp->f_count == 0)
1170 panic("unp_gc: deferred unreferenced socket");
1171 #endif
1172 } else {
1173 if (fp->f_count == 0)
1174 continue;
1175 if (fp->f_flag & FMARK)
1176 continue;
1177 if (fp->f_count == fp->f_msgcount)
1178 continue;
1179 }
1180 fp->f_flag |= FMARK;
1181
1182 if (fp->f_type != DTYPE_SOCKET ||
1183 (so = (struct socket *)fp->f_data) == 0)
1184 continue;
1185 if (so->so_proto->pr_domain != &unixdomain ||
1186 (so->so_proto->pr_flags&PR_RIGHTS) == 0)
1187 continue;
1188 #ifdef notdef
1189 if (so->so_rcv.sb_flags & SB_LOCK) {
1190 /*
1191 * This is problematical; it's not clear
1192 * we need to wait for the sockbuf to be
1193 * unlocked (on a uniprocessor, at least),
1194 * and it's also not clear what to do
1195 * if sbwait returns an error due to receipt
1196 * of a signal. If sbwait does return
1197 * an error, we'll go into an infinite
1198 * loop. Delete all of this for now.
1199 */
1200 (void) sbwait(&so->so_rcv);
1201 goto restart;
1202 }
1203 #endif
1204 unp_scan(so->so_rcv.sb_mb, unp_mark, 0);
1205 /*
1206 * mark descriptors referenced from sockets queued on the accept queue as well.
1207 */
1208 if (so->so_options & SO_ACCEPTCONN) {
1209 TAILQ_FOREACH(so1, &so->so_q0, so_qe) {
1210 unp_scan(so1->so_rcv.sb_mb, unp_mark, 0);
1211 }
1212 TAILQ_FOREACH(so1, &so->so_q, so_qe) {
1213 unp_scan(so1->so_rcv.sb_mb, unp_mark, 0);
1214 }
1215 }
1216
1217 }
1218 } while (unp_defer);
1219 /*
1220 * Sweep pass. Find unmarked descriptors, and free them.
1221 *
1222 * We grab an extra reference to each of the file table entries
1223 * that are not otherwise accessible and then free the rights
1224 * that are stored in messages on them.
1225 *
1226 * The bug in the original code is a little tricky, so I'll describe
1227 * what's wrong with it here.
1228 *
1229 * It is incorrect to simply unp_discard each entry for f_msgcount
1230 * times -- consider the case of sockets A and B that contain
1231 * references to each other. On a last close of some other socket,
1232 * we trigger a gc since the number of outstanding rights (unp_rights)
1233 * is non-zero. If during the sweep phase the gc code un_discards,
1234 * we end up doing a (full) closef on the descriptor. A closef on A
1235 * results in the following chain. Closef calls soo_close, which
1236 * calls soclose. Soclose calls first (through the switch
1237 * uipc_usrreq) unp_detach, which re-invokes unp_gc. Unp_gc simply
1238 * returns because the previous instance had set unp_gcing, and
1239 * we return all the way back to soclose, which marks the socket
1240 * with SS_NOFDREF, and then calls sofree. Sofree calls sorflush
1241 * to free up the rights that are queued in messages on the socket A,
1242 * i.e., the reference on B. The sorflush calls via the dom_dispose
1243 * switch unp_dispose, which unp_scans with unp_discard. This second
1244 * instance of unp_discard just calls closef on B.
1245 *
1246 * Well, a similar chain occurs on B, resulting in a sorflush on B,
1247 * which results in another closef on A. Unfortunately, A is already
1248 * being closed, and the descriptor has already been marked with
1249 * SS_NOFDREF, and soclose panics at this point.
1250 *
1251 * Here, we first take an extra reference to each inaccessible
1252 * descriptor. Then, if the inaccessible descriptor is a
1253 * socket, we call sorflush in case it is a Unix domain
1254 * socket. After we destroy all the rights carried in
1255 * messages, we do a last closef to get rid of our extra
1256 * reference. This is the last close, and the unp_detach etc
1257 * will shut down the socket.
1258 *
1259 * 91/09/19, bsy (at) cs.cmu.edu
1260 */
1261 extra_ref = malloc(nfiles * sizeof(struct file *), M_FILE, M_WAITOK);
1262 for (nunref = 0, fp = LIST_FIRST(&filehead), fpp = extra_ref; fp != 0;
1263 fp = nextfp) {
1264 nextfp = LIST_NEXT(fp, f_list);
1265 simple_lock(&fp->f_slock);
1266 if (fp->f_count != 0 &&
1267 fp->f_count == fp->f_msgcount && !(fp->f_flag & FMARK)) {
1268 *fpp++ = fp;
1269 nunref++;
1270 fp->f_count++;
1271 }
1272 simple_unlock(&fp->f_slock);
1273 }
1274 for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) {
1275 fp = *fpp;
1276 simple_lock(&fp->f_slock);
1277 FILE_USE(fp);
1278 if (fp->f_type == DTYPE_SOCKET)
1279 sorflush((struct socket *)fp->f_data);
1280 FILE_UNUSE(fp, NULL);
1281 }
1282 for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) {
1283 fp = *fpp;
1284 simple_lock(&fp->f_slock);
1285 FILE_USE(fp);
1286 (void) closef(fp, (struct lwp *)0);
1287 }
1288 free((void *)extra_ref, M_FILE);
1289 unp_gcing = 0;
1290 }
1291
1292 void
1293 unp_dispose(struct mbuf *m)
1294 {
1295
1296 if (m)
1297 unp_scan(m, unp_discard, 1);
1298 }
1299
1300 void
1301 unp_scan(struct mbuf *m0, void (*op)(struct file *), int discard)
1302 {
1303 struct mbuf *m;
1304 struct file **rp;
1305 struct cmsghdr *cm;
1306 int i;
1307 int qfds;
1308
1309 while (m0) {
1310 for (m = m0; m; m = m->m_next) {
1311 if (m->m_type == MT_CONTROL &&
1312 m->m_len >= sizeof(*cm)) {
1313 cm = mtod(m, struct cmsghdr *);
1314 if (cm->cmsg_level != SOL_SOCKET ||
1315 cm->cmsg_type != SCM_RIGHTS)
1316 continue;
1317 qfds = (cm->cmsg_len - CMSG_ALIGN(sizeof(*cm)))
1318 / sizeof(struct file *);
1319 rp = (struct file **)CMSG_DATA(cm);
1320 for (i = 0; i < qfds; i++) {
1321 struct file *fp = *rp;
1322 if (discard)
1323 *rp = 0;
1324 (*op)(fp);
1325 rp++;
1326 }
1327 break; /* XXX, but saves time */
1328 }
1329 }
1330 m0 = m0->m_nextpkt;
1331 }
1332 }
1333
1334 void
1335 unp_mark(struct file *fp)
1336 {
1337 if (fp == NULL)
1338 return;
1339
1340 if (fp->f_flag & FMARK)
1341 return;
1342
1343 /* If we're already deferred, don't screw up the defer count */
1344 if (fp->f_flag & FDEFER)
1345 return;
1346
1347 /*
1348 * Minimize the number of deferrals... Sockets are the only
1349 * type of descriptor which can hold references to another
1350 * descriptor, so just mark other descriptors, and defer
1351 * unmarked sockets for the next pass.
1352 */
1353 if (fp->f_type == DTYPE_SOCKET) {
1354 unp_defer++;
1355 if (fp->f_count == 0)
1356 panic("unp_mark: queued unref");
1357 fp->f_flag |= FDEFER;
1358 } else {
1359 fp->f_flag |= FMARK;
1360 }
1361 return;
1362 }
1363
1364 void
1365 unp_discard(struct file *fp)
1366 {
1367 if (fp == NULL)
1368 return;
1369 simple_lock(&fp->f_slock);
1370 fp->f_usecount++; /* i.e. FILE_USE(fp) sans locking */
1371 fp->f_msgcount--;
1372 simple_unlock(&fp->f_slock);
1373 unp_rights--;
1374 (void) closef(fp, (struct lwp *)0);
1375 }
1376