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