xref: /original-bsd/sys/kern/uipc_usrreq.c (revision 481335c4)
1 /*
2  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  *
17  *	@(#)uipc_usrreq.c	7.13 (Berkeley) 10/19/89
18  */
19 
20 #include "param.h"
21 #include "user.h"
22 #include "domain.h"
23 #include "protosw.h"
24 #include "socket.h"
25 #include "socketvar.h"
26 #include "unpcb.h"
27 #include "un.h"
28 #include "vnode.h"
29 #include "mount.h"
30 #include "file.h"
31 #include "stat.h"
32 #include "mbuf.h"	/* XXX must appear after mount.h */
33 
34 /*
35  * Unix communications domain.
36  *
37  * TODO:
38  *	SEQPACKET, RDM
39  *	rethink name space problems
40  *	need a proper out-of-band
41  */
42 struct	sockaddr sun_noname = { sizeof(sun_noname), AF_UNIX };
43 ino_t	unp_vno;			/* prototype for fake vnode numbers */
44 
45 /*ARGSUSED*/
46 uipc_usrreq(so, req, m, nam, rights)
47 	struct socket *so;
48 	int req;
49 	struct mbuf *m, *nam, *rights;
50 {
51 	struct unpcb *unp = sotounpcb(so);
52 	register struct socket *so2;
53 	int error = 0;
54 
55 	if (req == PRU_CONTROL)
56 		return (EOPNOTSUPP);
57 	if (req != PRU_SEND && rights && rights->m_len) {
58 		error = EOPNOTSUPP;
59 		goto release;
60 	}
61 	if (unp == 0 && req != PRU_ATTACH) {
62 		error = EINVAL;
63 		goto release;
64 	}
65 	switch (req) {
66 
67 	case PRU_ATTACH:
68 		if (unp) {
69 			error = EISCONN;
70 			break;
71 		}
72 		error = unp_attach(so);
73 		break;
74 
75 	case PRU_DETACH:
76 		unp_detach(unp);
77 		break;
78 
79 	case PRU_BIND:
80 		error = unp_bind(unp, nam);
81 		break;
82 
83 	case PRU_LISTEN:
84 		if (unp->unp_vnode == 0)
85 			error = EINVAL;
86 		break;
87 
88 	case PRU_CONNECT:
89 		error = unp_connect(so, nam);
90 		break;
91 
92 	case PRU_CONNECT2:
93 		error = unp_connect2(so, (struct socket *)nam);
94 		break;
95 
96 	case PRU_DISCONNECT:
97 		unp_disconnect(unp);
98 		break;
99 
100 	case PRU_ACCEPT:
101 		/*
102 		 * Pass back name of connected socket,
103 		 * if it was bound and we are still connected
104 		 * (our peer may have closed already!).
105 		 */
106 		if (unp->unp_conn && unp->unp_conn->unp_addr) {
107 			nam->m_len = unp->unp_conn->unp_addr->m_len;
108 			bcopy(mtod(unp->unp_conn->unp_addr, caddr_t),
109 			    mtod(nam, caddr_t), (unsigned)nam->m_len);
110 		} else {
111 			nam->m_len = sizeof(sun_noname);
112 			*(mtod(nam, struct sockaddr *)) = sun_noname;
113 		}
114 		break;
115 
116 	case PRU_SHUTDOWN:
117 		socantsendmore(so);
118 		unp_usrclosed(unp);
119 		break;
120 
121 	case PRU_RCVD:
122 		switch (so->so_type) {
123 
124 		case SOCK_DGRAM:
125 			panic("uipc 1");
126 			/*NOTREACHED*/
127 
128 		case SOCK_STREAM:
129 #define	rcv (&so->so_rcv)
130 #define snd (&so2->so_snd)
131 			if (unp->unp_conn == 0)
132 				break;
133 			so2 = unp->unp_conn->unp_socket;
134 			/*
135 			 * Adjust backpressure on sender
136 			 * and wakeup any waiting to write.
137 			 */
138 			snd->sb_mbmax += unp->unp_mbcnt - rcv->sb_mbcnt;
139 			unp->unp_mbcnt = rcv->sb_mbcnt;
140 			snd->sb_hiwat += unp->unp_cc - rcv->sb_cc;
141 			unp->unp_cc = rcv->sb_cc;
142 			sowwakeup(so2);
143 #undef snd
144 #undef rcv
145 			break;
146 
147 		default:
148 			panic("uipc 2");
149 		}
150 		break;
151 
152 	case PRU_SEND:
153 		if (rights) {
154 			error = unp_internalize(rights);
155 			if (error)
156 				break;
157 		}
158 		switch (so->so_type) {
159 
160 		case SOCK_DGRAM: {
161 			struct sockaddr *from;
162 
163 			if (nam) {
164 				if (unp->unp_conn) {
165 					error = EISCONN;
166 					break;
167 				}
168 				error = unp_connect(so, nam);
169 				if (error)
170 					break;
171 			} else {
172 				if (unp->unp_conn == 0) {
173 					error = ENOTCONN;
174 					break;
175 				}
176 			}
177 			so2 = unp->unp_conn->unp_socket;
178 			if (unp->unp_addr)
179 				from = mtod(unp->unp_addr, struct sockaddr *);
180 			else
181 				from = &sun_noname;
182 			if (sbspace(&so2->so_rcv) > 0 &&
183 			    sbappendaddr(&so2->so_rcv, from, m, rights)) {
184 				sorwakeup(so2);
185 				m = 0;
186 			} else
187 				error = ENOBUFS;
188 			if (nam)
189 				unp_disconnect(unp);
190 			break;
191 		}
192 
193 		case SOCK_STREAM:
194 #define	rcv (&so2->so_rcv)
195 #define	snd (&so->so_snd)
196 			if (so->so_state & SS_CANTSENDMORE) {
197 				error = EPIPE;
198 				break;
199 			}
200 			if (unp->unp_conn == 0)
201 				panic("uipc 3");
202 			so2 = unp->unp_conn->unp_socket;
203 			/*
204 			 * Send to paired receive port, and then reduce
205 			 * send buffer hiwater marks to maintain backpressure.
206 			 * Wake up readers.
207 			 */
208 			if (rights)
209 				(void)sbappendrights(rcv, m, rights);
210 			else
211 				sbappend(rcv, m);
212 			snd->sb_mbmax -=
213 			    rcv->sb_mbcnt - unp->unp_conn->unp_mbcnt;
214 			unp->unp_conn->unp_mbcnt = rcv->sb_mbcnt;
215 			snd->sb_hiwat -= rcv->sb_cc - unp->unp_conn->unp_cc;
216 			unp->unp_conn->unp_cc = rcv->sb_cc;
217 			sorwakeup(so2);
218 			m = 0;
219 #undef snd
220 #undef rcv
221 			break;
222 
223 		default:
224 			panic("uipc 4");
225 		}
226 		break;
227 
228 	case PRU_ABORT:
229 		unp_drop(unp, ECONNABORTED);
230 		break;
231 
232 	case PRU_SENSE:
233 		((struct stat *) m)->st_blksize = so->so_snd.sb_hiwat;
234 		if (so->so_type == SOCK_STREAM && unp->unp_conn != 0) {
235 			so2 = unp->unp_conn->unp_socket;
236 			((struct stat *) m)->st_blksize += so2->so_rcv.sb_cc;
237 		}
238 		((struct stat *) m)->st_dev = NODEV;
239 		if (unp->unp_vno == 0)
240 			unp->unp_vno = unp_vno++;
241 		((struct stat *) m)->st_ino = unp->unp_vno;
242 		return (0);
243 
244 	case PRU_RCVOOB:
245 		return (EOPNOTSUPP);
246 
247 	case PRU_SENDOOB:
248 		error = EOPNOTSUPP;
249 		break;
250 
251 	case PRU_SOCKADDR:
252 		if (unp->unp_addr) {
253 			nam->m_len = unp->unp_addr->m_len;
254 			bcopy(mtod(unp->unp_addr, caddr_t),
255 			    mtod(nam, caddr_t), (unsigned)nam->m_len);
256 		} else
257 			nam->m_len = 0;
258 		break;
259 
260 	case PRU_PEERADDR:
261 		if (unp->unp_conn && unp->unp_conn->unp_addr) {
262 			nam->m_len = unp->unp_conn->unp_addr->m_len;
263 			bcopy(mtod(unp->unp_conn->unp_addr, caddr_t),
264 			    mtod(nam, caddr_t), (unsigned)nam->m_len);
265 		} else
266 			nam->m_len = 0;
267 		break;
268 
269 	case PRU_SLOWTIMO:
270 		break;
271 
272 	default:
273 		panic("piusrreq");
274 	}
275 release:
276 	if (m)
277 		m_freem(m);
278 	return (error);
279 }
280 
281 /*
282  * Both send and receive buffers are allocated PIPSIZ bytes of buffering
283  * for stream sockets, although the total for sender and receiver is
284  * actually only PIPSIZ.
285  * Datagram sockets really use the sendspace as the maximum datagram size,
286  * and don't really want to reserve the sendspace.  Their recvspace should
287  * be large enough for at least one max-size datagram plus address.
288  */
289 #define	PIPSIZ	4096
290 u_long	unpst_sendspace = PIPSIZ;
291 u_long	unpst_recvspace = PIPSIZ;
292 u_long	unpdg_sendspace = 2*1024;	/* really max datagram size */
293 u_long	unpdg_recvspace = 4*1024;
294 
295 int	unp_rights;			/* file descriptors in flight */
296 
297 unp_attach(so)
298 	struct socket *so;
299 {
300 	register struct mbuf *m;
301 	register struct unpcb *unp;
302 	int error;
303 
304 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
305 		switch (so->so_type) {
306 
307 		case SOCK_STREAM:
308 			error = soreserve(so, unpst_sendspace, unpst_recvspace);
309 			break;
310 
311 		case SOCK_DGRAM:
312 			error = soreserve(so, unpdg_sendspace, unpdg_recvspace);
313 			break;
314 		}
315 		if (error)
316 			return (error);
317 	}
318 	m = m_getclr(M_DONTWAIT, MT_PCB);
319 	if (m == NULL)
320 		return (ENOBUFS);
321 	unp = mtod(m, struct unpcb *);
322 	so->so_pcb = (caddr_t)unp;
323 	unp->unp_socket = so;
324 	return (0);
325 }
326 
327 unp_detach(unp)
328 	register struct unpcb *unp;
329 {
330 
331 	if (unp->unp_vnode) {
332 		unp->unp_vnode->v_socket = 0;
333 		vrele(unp->unp_vnode);
334 		unp->unp_vnode = 0;
335 	}
336 	if (unp->unp_conn)
337 		unp_disconnect(unp);
338 	while (unp->unp_refs)
339 		unp_drop(unp->unp_refs, ECONNRESET);
340 	soisdisconnected(unp->unp_socket);
341 	unp->unp_socket->so_pcb = 0;
342 	m_freem(unp->unp_addr);
343 	(void) m_free(dtom(unp));
344 	if (unp_rights)
345 		unp_gc();
346 }
347 
348 unp_bind(unp, nam)
349 	struct unpcb *unp;
350 	struct mbuf *nam;
351 {
352 	struct sockaddr_un *soun = mtod(nam, struct sockaddr_un *);
353 	register struct vnode *vp;
354 	register struct nameidata *ndp = &u.u_nd;
355 	struct vattr vattr;
356 	int error;
357 
358 	ndp->ni_dirp = soun->sun_path;
359 	if (unp->unp_vnode != NULL)
360 		return (EINVAL);
361 	if (nam->m_len == MLEN) {
362 		if (*(mtod(nam, caddr_t) + nam->m_len - 1) != 0)
363 			return (EINVAL);
364 	} else
365 		*(mtod(nam, caddr_t) + nam->m_len) = 0;
366 /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
367 	ndp->ni_nameiop = CREATE | FOLLOW | LOCKPARENT;
368 	ndp->ni_segflg = UIO_SYSSPACE;
369 	if (error = namei(ndp))
370 		return (error);
371 	vp = ndp->ni_vp;
372 	if (vp != NULL) {
373 		VOP_ABORTOP(ndp);
374 		return (EADDRINUSE);
375 	}
376 	vattr_null(&vattr);
377 	vattr.va_type = VSOCK;
378 	vattr.va_mode = 0777;
379 	if (error = VOP_CREATE(ndp, &vattr))
380 		return (error);
381 	vp = ndp->ni_vp;
382 	vp->v_socket = unp->unp_socket;
383 	unp->unp_vnode = vp;
384 	unp->unp_addr = m_copy(nam, 0, (int)M_COPYALL);
385 	VOP_UNLOCK(vp);
386 	return (0);
387 }
388 
389 unp_connect(so, nam)
390 	struct socket *so;
391 	struct mbuf *nam;
392 {
393 	register struct sockaddr_un *soun = mtod(nam, struct sockaddr_un *);
394 	register struct vnode *vp;
395 	register struct socket *so2, *so3;
396 	register struct nameidata *ndp = &u.u_nd;
397 	struct unpcb *unp2, *unp3;
398 	int error;
399 
400 	ndp->ni_dirp = soun->sun_path;
401 	if (nam->m_data + nam->m_len == &nam->m_dat[MLEN]) {	/* XXX */
402 		if (*(mtod(nam, caddr_t) + nam->m_len - 1) != 0)
403 			return (EMSGSIZE);
404 	} else
405 		*(mtod(nam, caddr_t) + nam->m_len) = 0;
406 	ndp->ni_nameiop = LOOKUP | FOLLOW | LOCKLEAF;
407 	ndp->ni_segflg = UIO_SYSSPACE;
408 	if (error = namei(ndp))
409 		return (error);
410 	vp = ndp->ni_vp;
411 	if (vp->v_type != VSOCK) {
412 		error = ENOTSOCK;
413 		goto bad;
414 	}
415 	if (error = VOP_ACCESS(vp, VWRITE, ndp->ni_cred))
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) {
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(so2);
467 		soisconnected(so);
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 /*ARGSUSED*/
522 unp_usrclosed(unp)
523 	struct unpcb *unp;
524 {
525 
526 }
527 
528 unp_drop(unp, errno)
529 	struct unpcb *unp;
530 	int errno;
531 {
532 	struct socket *so = unp->unp_socket;
533 
534 	so->so_error = errno;
535 	unp_disconnect(unp);
536 	if (so->so_head) {
537 		so->so_pcb = (caddr_t) 0;
538 		m_freem(unp->unp_addr);
539 		(void) m_free(dtom(unp));
540 		sofree(so);
541 	}
542 }
543 
544 #ifdef notdef
545 unp_drain()
546 {
547 
548 }
549 #endif
550 
551 unp_externalize(rights)
552 	struct mbuf *rights;
553 {
554 	int newfds = rights->m_len / sizeof (int);
555 	register int i;
556 	register struct file **rp = mtod(rights, struct file **);
557 	register struct file *fp;
558 	int f;
559 
560 	if (newfds > ufavail()) {
561 		for (i = 0; i < newfds; i++) {
562 			fp = *rp;
563 			unp_discard(fp);
564 			*rp++ = 0;
565 		}
566 		return (EMSGSIZE);
567 	}
568 	for (i = 0; i < newfds; i++) {
569 		if (ufalloc(0, &f))
570 			panic("unp_externalize");
571 		fp = *rp;
572 		u.u_ofile[f] = fp;
573 		fp->f_msgcount--;
574 		unp_rights--;
575 		*(int *)rp++ = f;
576 	}
577 	return (0);
578 }
579 
580 unp_internalize(rights)
581 	struct mbuf *rights;
582 {
583 	register struct file **rp;
584 	int oldfds = rights->m_len / sizeof (int);
585 	register int i, fd;
586 	register struct file *fp;
587 
588 	rp = mtod(rights, struct file **);
589 	for (i = 0; i < oldfds; i++) {
590 		fd = *(int *)rp++;
591 		if ((unsigned)fd >= NOFILE || u.u_ofile[fd] == NULL)
592 			return (EBADF);
593 	}
594 	rp = mtod(rights, struct file **);
595 	for (i = 0; i < oldfds; i++) {
596 		fp = u.u_ofile[*(int *)rp];
597 		*rp++ = fp;
598 		fp->f_count++;
599 		fp->f_msgcount++;
600 		unp_rights++;
601 	}
602 	return (0);
603 }
604 
605 int	unp_defer, unp_gcing;
606 int	unp_mark();
607 extern	struct domain unixdomain;
608 
609 unp_gc()
610 {
611 	register struct file *fp;
612 	register struct socket *so;
613 
614 	if (unp_gcing)
615 		return;
616 	unp_gcing = 1;
617 restart:
618 	unp_defer = 0;
619 	for (fp = file; fp < fileNFILE; fp++)
620 		fp->f_flag &= ~(FMARK|FDEFER);
621 	do {
622 		for (fp = file; fp < fileNFILE; fp++) {
623 			if (fp->f_count == 0)
624 				continue;
625 			if (fp->f_flag & FDEFER) {
626 				fp->f_flag &= ~FDEFER;
627 				unp_defer--;
628 			} else {
629 				if (fp->f_flag & FMARK)
630 					continue;
631 				if (fp->f_count == fp->f_msgcount)
632 					continue;
633 				fp->f_flag |= FMARK;
634 			}
635 			if (fp->f_type != DTYPE_SOCKET ||
636 			    (so = (struct socket *)fp->f_data) == 0)
637 				continue;
638 			if (so->so_proto->pr_domain != &unixdomain ||
639 			    (so->so_proto->pr_flags&PR_RIGHTS) == 0)
640 				continue;
641 			if (so->so_rcv.sb_flags & SB_LOCK) {
642 				sbwait(&so->so_rcv);
643 				goto restart;
644 			}
645 			unp_scan(so->so_rcv.sb_mb, unp_mark);
646 		}
647 	} while (unp_defer);
648 	for (fp = file; fp < fileNFILE; fp++) {
649 		if (fp->f_count == 0)
650 			continue;
651 		if (fp->f_count == fp->f_msgcount && (fp->f_flag & FMARK) == 0)
652 			while (fp->f_msgcount)
653 				unp_discard(fp);
654 	}
655 	unp_gcing = 0;
656 }
657 
658 unp_dispose(m)
659 	struct mbuf *m;
660 {
661 	int unp_discard();
662 
663 	if (m)
664 		unp_scan(m, unp_discard);
665 }
666 
667 unp_scan(m0, op)
668 	register struct mbuf *m0;
669 	int (*op)();
670 {
671 	register struct mbuf *m;
672 	register struct file **rp;
673 	register int i;
674 	int qfds;
675 
676 	while (m0) {
677 		for (m = m0; m; m = m->m_next)
678 			if (m->m_type == MT_RIGHTS && m->m_len) {
679 				qfds = m->m_len / sizeof (struct file *);
680 				rp = mtod(m, struct file **);
681 				for (i = 0; i < qfds; i++)
682 					(*op)(*rp++);
683 				break;		/* XXX, but saves time */
684 			}
685 		m0 = m0->m_act;
686 	}
687 }
688 
689 unp_mark(fp)
690 	struct file *fp;
691 {
692 
693 	if (fp->f_flag & FMARK)
694 		return;
695 	unp_defer++;
696 	fp->f_flag |= (FMARK|FDEFER);
697 }
698 
699 unp_discard(fp)
700 	struct file *fp;
701 {
702 
703 	fp->f_msgcount--;
704 	unp_rights--;
705 	(void) closef(fp);
706 }
707