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