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