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