xref: /csrg-svn/sys/kern/uipc_usrreq.c (revision 52921)
1 /*
2  * Copyright (c) 1982, 1986, 1989, 1991 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)uipc_usrreq.c	7.31 (Berkeley) 03/13/92
8  */
9 
10 #include "param.h"
11 #include "proc.h"
12 #include "filedesc.h"
13 #include "domain.h"
14 #include "protosw.h"
15 #include "socket.h"
16 #include "socketvar.h"
17 #include "unpcb.h"
18 #include "un.h"
19 #include "namei.h"
20 #include "vnode.h"
21 #include "file.h"
22 #include "stat.h"
23 #include "mbuf.h"
24 
25 /*
26  * Unix communications domain.
27  *
28  * TODO:
29  *	SEQPACKET, RDM
30  *	rethink name space problems
31  *	need a proper out-of-band
32  */
33 struct	sockaddr sun_noname = { sizeof(sun_noname), AF_UNIX };
34 ino_t	unp_ino;			/* prototype for fake inode numbers */
35 
36 /*ARGSUSED*/
37 uipc_usrreq(so, req, m, nam, control)
38 	struct socket *so;
39 	int req;
40 	struct mbuf *m, *nam, *control;
41 {
42 	struct unpcb *unp = sotounpcb(so);
43 	register struct socket *so2;
44 	register int error = 0;
45 	struct proc *p = curproc;	/* XXX */
46 
47 	if (req == PRU_CONTROL)
48 		return (EOPNOTSUPP);
49 	if (req != PRU_SEND && control && control->m_len) {
50 		error = EOPNOTSUPP;
51 		goto release;
52 	}
53 	if (unp == 0 && req != PRU_ATTACH) {
54 		error = EINVAL;
55 		goto release;
56 	}
57 	switch (req) {
58 
59 	case PRU_ATTACH:
60 		if (unp) {
61 			error = EISCONN;
62 			break;
63 		}
64 		error = unp_attach(so);
65 		break;
66 
67 	case PRU_DETACH:
68 		unp_detach(unp);
69 		break;
70 
71 	case PRU_BIND:
72 		error = unp_bind(unp, nam, p);
73 		break;
74 
75 	case PRU_LISTEN:
76 		if (unp->unp_vnode == 0)
77 			error = EINVAL;
78 		break;
79 
80 	case PRU_CONNECT:
81 		error = unp_connect(so, nam, p);
82 		break;
83 
84 	case PRU_CONNECT2:
85 		error = unp_connect2(so, (struct socket *)nam);
86 		break;
87 
88 	case PRU_DISCONNECT:
89 		unp_disconnect(unp);
90 		break;
91 
92 	case PRU_ACCEPT:
93 		/*
94 		 * Pass back name of connected socket,
95 		 * if it was bound and we are still connected
96 		 * (our peer may have closed already!).
97 		 */
98 		if (unp->unp_conn && unp->unp_conn->unp_addr) {
99 			nam->m_len = unp->unp_conn->unp_addr->m_len;
100 			bcopy(mtod(unp->unp_conn->unp_addr, caddr_t),
101 			    mtod(nam, caddr_t), (unsigned)nam->m_len);
102 		} else {
103 			nam->m_len = sizeof(sun_noname);
104 			*(mtod(nam, struct sockaddr *)) = sun_noname;
105 		}
106 		break;
107 
108 	case PRU_SHUTDOWN:
109 		socantsendmore(so);
110 		unp_shutdown(unp);
111 		break;
112 
113 	case PRU_RCVD:
114 		switch (so->so_type) {
115 
116 		case SOCK_DGRAM:
117 			panic("uipc 1");
118 			/*NOTREACHED*/
119 
120 		case SOCK_STREAM:
121 #define	rcv (&so->so_rcv)
122 #define snd (&so2->so_snd)
123 			if (unp->unp_conn == 0)
124 				break;
125 			so2 = unp->unp_conn->unp_socket;
126 			/*
127 			 * Adjust backpressure on sender
128 			 * and wakeup any waiting to write.
129 			 */
130 			snd->sb_mbmax += unp->unp_mbcnt - rcv->sb_mbcnt;
131 			unp->unp_mbcnt = rcv->sb_mbcnt;
132 			snd->sb_hiwat += unp->unp_cc - rcv->sb_cc;
133 			unp->unp_cc = rcv->sb_cc;
134 			sowwakeup(so2);
135 #undef snd
136 #undef rcv
137 			break;
138 
139 		default:
140 			panic("uipc 2");
141 		}
142 		break;
143 
144 	case PRU_SEND:
145 		if (control && (error = unp_internalize(control, p)))
146 			break;
147 		switch (so->so_type) {
148 
149 		case SOCK_DGRAM: {
150 			struct sockaddr *from;
151 
152 			if (nam) {
153 				if (unp->unp_conn) {
154 					error = EISCONN;
155 					break;
156 				}
157 				error = unp_connect(so, nam, p);
158 				if (error)
159 					break;
160 			} else {
161 				if (unp->unp_conn == 0) {
162 					error = ENOTCONN;
163 					break;
164 				}
165 			}
166 			so2 = unp->unp_conn->unp_socket;
167 			if (unp->unp_addr)
168 				from = mtod(unp->unp_addr, struct sockaddr *);
169 			else
170 				from = &sun_noname;
171 			if (sbappendaddr(&so2->so_rcv, from, m, control)) {
172 				sorwakeup(so2);
173 				m = 0;
174 				control = 0;
175 			} else
176 				error = ENOBUFS;
177 			if (nam)
178 				unp_disconnect(unp);
179 			break;
180 		}
181 
182 		case SOCK_STREAM:
183 #define	rcv (&so2->so_rcv)
184 #define	snd (&so->so_snd)
185 			if (so->so_state & SS_CANTSENDMORE) {
186 				error = EPIPE;
187 				break;
188 			}
189 			if (unp->unp_conn == 0)
190 				panic("uipc 3");
191 			so2 = unp->unp_conn->unp_socket;
192 			/*
193 			 * Send to paired receive port, and then reduce
194 			 * send buffer hiwater marks to maintain backpressure.
195 			 * Wake up readers.
196 			 */
197 			if (control) {
198 				if (sbappendcontrol(rcv, m, control))
199 					control = 0;
200 			} else
201 				sbappend(rcv, m);
202 			snd->sb_mbmax -=
203 			    rcv->sb_mbcnt - unp->unp_conn->unp_mbcnt;
204 			unp->unp_conn->unp_mbcnt = rcv->sb_mbcnt;
205 			snd->sb_hiwat -= rcv->sb_cc - unp->unp_conn->unp_cc;
206 			unp->unp_conn->unp_cc = rcv->sb_cc;
207 			sorwakeup(so2);
208 			m = 0;
209 #undef snd
210 #undef rcv
211 			break;
212 
213 		default:
214 			panic("uipc 4");
215 		}
216 		break;
217 
218 	case PRU_ABORT:
219 		unp_drop(unp, ECONNABORTED);
220 		break;
221 
222 	case PRU_SENSE:
223 		((struct stat *) m)->st_blksize = so->so_snd.sb_hiwat;
224 		if (so->so_type == SOCK_STREAM && unp->unp_conn != 0) {
225 			so2 = unp->unp_conn->unp_socket;
226 			((struct stat *) m)->st_blksize += so2->so_rcv.sb_cc;
227 		}
228 		((struct stat *) m)->st_dev = NODEV;
229 		if (unp->unp_ino == 0)
230 			unp->unp_ino = unp_ino++;
231 		((struct stat *) m)->st_ino = unp->unp_ino;
232 		return (0);
233 
234 	case PRU_RCVOOB:
235 		return (EOPNOTSUPP);
236 
237 	case PRU_SENDOOB:
238 		error = EOPNOTSUPP;
239 		break;
240 
241 	case PRU_SOCKADDR:
242 		if (unp->unp_addr) {
243 			nam->m_len = unp->unp_addr->m_len;
244 			bcopy(mtod(unp->unp_addr, caddr_t),
245 			    mtod(nam, caddr_t), (unsigned)nam->m_len);
246 		} else
247 			nam->m_len = 0;
248 		break;
249 
250 	case PRU_PEERADDR:
251 		if (unp->unp_conn && unp->unp_conn->unp_addr) {
252 			nam->m_len = unp->unp_conn->unp_addr->m_len;
253 			bcopy(mtod(unp->unp_conn->unp_addr, caddr_t),
254 			    mtod(nam, caddr_t), (unsigned)nam->m_len);
255 		} else
256 			nam->m_len = 0;
257 		break;
258 
259 	case PRU_SLOWTIMO:
260 		break;
261 
262 	default:
263 		panic("piusrreq");
264 	}
265 release:
266 	if (control)
267 		m_freem(control);
268 	if (m)
269 		m_freem(m);
270 	return (error);
271 }
272 
273 /*
274  * Both send and receive buffers are allocated PIPSIZ bytes of buffering
275  * for stream sockets, although the total for sender and receiver is
276  * actually only PIPSIZ.
277  * Datagram sockets really use the sendspace as the maximum datagram size,
278  * and don't really want to reserve the sendspace.  Their recvspace should
279  * be large enough for at least one max-size datagram plus address.
280  */
281 #define	PIPSIZ	4096
282 u_long	unpst_sendspace = PIPSIZ;
283 u_long	unpst_recvspace = PIPSIZ;
284 u_long	unpdg_sendspace = 2*1024;	/* really max datagram size */
285 u_long	unpdg_recvspace = 4*1024;
286 
287 int	unp_rights;			/* file descriptors in flight */
288 
289 unp_attach(so)
290 	struct socket *so;
291 {
292 	register struct mbuf *m;
293 	register struct unpcb *unp;
294 	int error;
295 
296 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
297 		switch (so->so_type) {
298 
299 		case SOCK_STREAM:
300 			error = soreserve(so, unpst_sendspace, unpst_recvspace);
301 			break;
302 
303 		case SOCK_DGRAM:
304 			error = soreserve(so, unpdg_sendspace, unpdg_recvspace);
305 			break;
306 
307 		default:
308 			panic("unp_attach");
309 		}
310 		if (error)
311 			return (error);
312 	}
313 	m = m_getclr(M_DONTWAIT, MT_PCB);
314 	if (m == NULL)
315 		return (ENOBUFS);
316 	unp = mtod(m, struct unpcb *);
317 	so->so_pcb = (caddr_t)unp;
318 	unp->unp_socket = so;
319 	return (0);
320 }
321 
322 unp_detach(unp)
323 	register struct unpcb *unp;
324 {
325 
326 	if (unp->unp_vnode) {
327 		unp->unp_vnode->v_socket = 0;
328 		vrele(unp->unp_vnode);
329 		unp->unp_vnode = 0;
330 	}
331 	if (unp->unp_conn)
332 		unp_disconnect(unp);
333 	while (unp->unp_refs)
334 		unp_drop(unp->unp_refs, ECONNRESET);
335 	soisdisconnected(unp->unp_socket);
336 	unp->unp_socket->so_pcb = 0;
337 	m_freem(unp->unp_addr);
338 	(void) m_free(dtom(unp));
339 	if (unp_rights)
340 		unp_gc();
341 }
342 
343 unp_bind(unp, nam, p)
344 	struct unpcb *unp;
345 	struct mbuf *nam;
346 	struct proc *p;
347 {
348 	struct sockaddr_un *soun = mtod(nam, struct sockaddr_un *);
349 	register struct vnode *vp;
350 	struct vattr vattr;
351 	int error;
352 	struct nameidata nd;
353 
354 	NDINIT(&nd, CREATE, FOLLOW | LOCKPARENT, UIO_SYSSPACE,
355 		soun->sun_path, p);
356 	if (unp->unp_vnode != NULL)
357 		return (EINVAL);
358 	if (nam->m_len == MLEN) {
359 		if (*(mtod(nam, caddr_t) + nam->m_len - 1) != 0)
360 			return (EINVAL);
361 	} else
362 		*(mtod(nam, caddr_t) + nam->m_len) = 0;
363 /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
364 	if (error = namei(&nd))
365 		return (error);
366 	vp = nd.ni_vp;
367 	if (vp != NULL) {
368 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
369 		if (nd.ni_dvp == vp)
370 			vrele(nd.ni_dvp);
371 		else
372 			vput(nd.ni_dvp);
373 		vrele(vp);
374 		return (EADDRINUSE);
375 	}
376 	VATTR_NULL(&vattr);
377 	vattr.va_type = VSOCK;
378 	vattr.va_mode = 0777;
379 	LEASE_CHECK(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
380 	if (error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr))
381 		return (error);
382 	vp = nd.ni_vp;
383 	vp->v_socket = unp->unp_socket;
384 	unp->unp_vnode = vp;
385 	unp->unp_addr = m_copy(nam, 0, (int)M_COPYALL);
386 	VOP_UNLOCK(vp);
387 	return (0);
388 }
389 
390 unp_connect(so, nam, p)
391 	struct socket *so;
392 	struct mbuf *nam;
393 	struct proc *p;
394 {
395 	register struct sockaddr_un *soun = mtod(nam, struct sockaddr_un *);
396 	register struct vnode *vp;
397 	register struct socket *so2, *so3;
398 	struct unpcb *unp2, *unp3;
399 	int error;
400 	struct nameidata nd;
401 
402 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, soun->sun_path, p);
403 	if (nam->m_data + nam->m_len == &nam->m_dat[MLEN]) {	/* XXX */
404 		if (*(mtod(nam, caddr_t) + nam->m_len - 1) != 0)
405 			return (EMSGSIZE);
406 	} else
407 		*(mtod(nam, caddr_t) + nam->m_len) = 0;
408 	if (error = namei(&nd))
409 		return (error);
410 	vp = nd.ni_vp;
411 	if (vp->v_type != VSOCK) {
412 		error = ENOTSOCK;
413 		goto bad;
414 	}
415 	if (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, p))
416 		goto bad;
417 	so2 = vp->v_socket;
418 	if (so2 == 0) {
419 		error = ECONNREFUSED;
420 		goto bad;
421 	}
422 	if (so->so_type != so2->so_type) {
423 		error = EPROTOTYPE;
424 		goto bad;
425 	}
426 	if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
427 		if ((so2->so_options & SO_ACCEPTCONN) == 0 ||
428 		    (so3 = sonewconn(so2, 0)) == 0) {
429 			error = ECONNREFUSED;
430 			goto bad;
431 		}
432 		unp2 = sotounpcb(so2);
433 		unp3 = sotounpcb(so3);
434 		if (unp2->unp_addr)
435 			unp3->unp_addr =
436 				  m_copy(unp2->unp_addr, 0, (int)M_COPYALL);
437 		so2 = so3;
438 	}
439 	error = unp_connect2(so, so2);
440 bad:
441 	vput(vp);
442 	return (error);
443 }
444 
445 unp_connect2(so, so2)
446 	register struct socket *so;
447 	register struct socket *so2;
448 {
449 	register struct unpcb *unp = sotounpcb(so);
450 	register struct unpcb *unp2;
451 
452 	if (so2->so_type != so->so_type)
453 		return (EPROTOTYPE);
454 	unp2 = sotounpcb(so2);
455 	unp->unp_conn = unp2;
456 	switch (so->so_type) {
457 
458 	case SOCK_DGRAM:
459 		unp->unp_nextref = unp2->unp_refs;
460 		unp2->unp_refs = unp;
461 		soisconnected(so);
462 		break;
463 
464 	case SOCK_STREAM:
465 		unp2->unp_conn = unp;
466 		soisconnected(so);
467 		soisconnected(so2);
468 		break;
469 
470 	default:
471 		panic("unp_connect2");
472 	}
473 	return (0);
474 }
475 
476 unp_disconnect(unp)
477 	struct unpcb *unp;
478 {
479 	register struct unpcb *unp2 = unp->unp_conn;
480 
481 	if (unp2 == 0)
482 		return;
483 	unp->unp_conn = 0;
484 	switch (unp->unp_socket->so_type) {
485 
486 	case SOCK_DGRAM:
487 		if (unp2->unp_refs == unp)
488 			unp2->unp_refs = unp->unp_nextref;
489 		else {
490 			unp2 = unp2->unp_refs;
491 			for (;;) {
492 				if (unp2 == 0)
493 					panic("unp_disconnect");
494 				if (unp2->unp_nextref == unp)
495 					break;
496 				unp2 = unp2->unp_nextref;
497 			}
498 			unp2->unp_nextref = unp->unp_nextref;
499 		}
500 		unp->unp_nextref = 0;
501 		unp->unp_socket->so_state &= ~SS_ISCONNECTED;
502 		break;
503 
504 	case SOCK_STREAM:
505 		soisdisconnected(unp->unp_socket);
506 		unp2->unp_conn = 0;
507 		soisdisconnected(unp2->unp_socket);
508 		break;
509 	}
510 }
511 
512 #ifdef notdef
513 unp_abort(unp)
514 	struct unpcb *unp;
515 {
516 
517 	unp_detach(unp);
518 }
519 #endif
520 
521 unp_shutdown(unp)
522 	struct unpcb *unp;
523 {
524 	struct socket *so;
525 
526 	if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn &&
527 	    (so = unp->unp_conn->unp_socket))
528 		socantrcvmore(so);
529 }
530 
531 unp_drop(unp, errno)
532 	struct unpcb *unp;
533 	int errno;
534 {
535 	struct socket *so = unp->unp_socket;
536 
537 	so->so_error = errno;
538 	unp_disconnect(unp);
539 	if (so->so_head) {
540 		so->so_pcb = (caddr_t) 0;
541 		m_freem(unp->unp_addr);
542 		(void) m_free(dtom(unp));
543 		sofree(so);
544 	}
545 }
546 
547 #ifdef notdef
548 unp_drain()
549 {
550 
551 }
552 #endif
553 
554 unp_externalize(rights)
555 	struct mbuf *rights;
556 {
557 	struct proc *p = curproc;		/* XXX */
558 	register int i;
559 	register struct cmsghdr *cm = mtod(rights, struct cmsghdr *);
560 	register struct file **rp = (struct file **)(cm + 1);
561 	register struct file *fp;
562 	int newfds = (cm->cmsg_len - sizeof(*cm)) / sizeof (int);
563 	int f;
564 
565 	if (fdavail(p, newfds)) {
566 		for (i = 0; i < newfds; i++) {
567 			fp = *rp;
568 			unp_discard(fp);
569 			*rp++ = 0;
570 		}
571 		return (EMSGSIZE);
572 	}
573 	for (i = 0; i < newfds; i++) {
574 		if (fdalloc(p, 0, &f))
575 			panic("unp_externalize");
576 		fp = *rp;
577 		p->p_fd->fd_ofiles[f] = fp;
578 		fp->f_msgcount--;
579 		unp_rights--;
580 		*(int *)rp++ = f;
581 	}
582 	return (0);
583 }
584 
585 unp_internalize(control, p)
586 	struct mbuf *control;
587 	struct proc *p;
588 {
589 	struct filedesc *fdp = p->p_fd;
590 	register struct cmsghdr *cm = mtod(control, struct cmsghdr *);
591 	register struct file **rp;
592 	register struct file *fp;
593 	register int i, fd;
594 	int oldfds;
595 
596 	if (cm->cmsg_type != SCM_RIGHTS || cm->cmsg_level != SOL_SOCKET ||
597 	    cm->cmsg_len != control->m_len)
598 		return (EINVAL);
599 	oldfds = (cm->cmsg_len - sizeof (*cm)) / sizeof (int);
600 	rp = (struct file **)(cm + 1);
601 	for (i = 0; i < oldfds; i++) {
602 		fd = *(int *)rp++;
603 		if ((unsigned)fd >= fdp->fd_nfiles ||
604 		    fdp->fd_ofiles[fd] == NULL)
605 			return (EBADF);
606 	}
607 	rp = (struct file **)(cm + 1);
608 	for (i = 0; i < oldfds; i++) {
609 		fp = fdp->fd_ofiles[*(int *)rp];
610 		*rp++ = fp;
611 		fp->f_count++;
612 		fp->f_msgcount++;
613 		unp_rights++;
614 	}
615 	return (0);
616 }
617 
618 int	unp_defer, unp_gcing;
619 int	unp_mark();
620 extern	struct domain unixdomain;
621 
622 unp_gc()
623 {
624 	register struct file *fp;
625 	register struct socket *so;
626 
627 	if (unp_gcing)
628 		return;
629 	unp_gcing = 1;
630 restart:
631 	unp_defer = 0;
632 	for (fp = filehead; fp; fp = fp->f_filef)
633 		fp->f_flag &= ~(FMARK|FDEFER);
634 	do {
635 		for (fp = filehead; fp; fp = fp->f_filef) {
636 			if (fp->f_count == 0)
637 				continue;
638 			if (fp->f_flag & FDEFER) {
639 				fp->f_flag &= ~FDEFER;
640 				unp_defer--;
641 			} else {
642 				if (fp->f_flag & FMARK)
643 					continue;
644 				if (fp->f_count == fp->f_msgcount)
645 					continue;
646 				fp->f_flag |= FMARK;
647 			}
648 			if (fp->f_type != DTYPE_SOCKET ||
649 			    (so = (struct socket *)fp->f_data) == 0)
650 				continue;
651 			if (so->so_proto->pr_domain != &unixdomain ||
652 			    (so->so_proto->pr_flags&PR_RIGHTS) == 0)
653 				continue;
654 #ifdef notdef
655 			if (so->so_rcv.sb_flags & SB_LOCK) {
656 				/*
657 				 * This is problematical; it's not clear
658 				 * we need to wait for the sockbuf to be
659 				 * unlocked (on a uniprocessor, at least),
660 				 * and it's also not clear what to do
661 				 * if sbwait returns an error due to receipt
662 				 * of a signal.  If sbwait does return
663 				 * an error, we'll go into an infinite
664 				 * loop.  Delete all of this for now.
665 				 */
666 				(void) sbwait(&so->so_rcv);
667 				goto restart;
668 			}
669 #endif
670 			unp_scan(so->so_rcv.sb_mb, unp_mark);
671 		}
672 	} while (unp_defer);
673 	for (fp = filehead; fp; fp = fp->f_filef) {
674 		if (fp->f_count == 0)
675 			continue;
676 		if (fp->f_count == fp->f_msgcount && (fp->f_flag & FMARK) == 0)
677 			while (fp->f_msgcount)
678 				unp_discard(fp);
679 	}
680 	unp_gcing = 0;
681 }
682 
683 unp_dispose(m)
684 	struct mbuf *m;
685 {
686 	int unp_discard();
687 
688 	if (m)
689 		unp_scan(m, unp_discard);
690 }
691 
692 unp_scan(m0, op)
693 	register struct mbuf *m0;
694 	int (*op)();
695 {
696 	register struct mbuf *m;
697 	register struct file **rp;
698 	register struct cmsghdr *cm;
699 	register int i;
700 	int qfds;
701 
702 	while (m0) {
703 		for (m = m0; m; m = m->m_next)
704 			if (m->m_type == MT_CONTROL &&
705 			    m->m_len >= sizeof(*cm)) {
706 				cm = mtod(m, struct cmsghdr *);
707 				if (cm->cmsg_level != SOL_SOCKET ||
708 				    cm->cmsg_type != SCM_RIGHTS)
709 					continue;
710 				qfds = (cm->cmsg_len - sizeof *cm)
711 						/ sizeof (struct file *);
712 				rp = (struct file **)(cm + 1);
713 				for (i = 0; i < qfds; i++)
714 					(*op)(*rp++);
715 				break;		/* XXX, but saves time */
716 			}
717 		m0 = m0->m_act;
718 	}
719 }
720 
721 unp_mark(fp)
722 	struct file *fp;
723 {
724 
725 	if (fp->f_flag & FMARK)
726 		return;
727 	unp_defer++;
728 	fp->f_flag |= (FMARK|FDEFER);
729 }
730 
731 unp_discard(fp)
732 	struct file *fp;
733 {
734 
735 	fp->f_msgcount--;
736 	unp_rights--;
737 	(void) closef(fp, (struct proc *)NULL);
738 }
739