xref: /original-bsd/sys/kern/uipc_usrreq.c (revision 4c01ad61)
1 /*	uipc_usrreq.c	6.11	84/12/20	*/
2 
3 #include "param.h"
4 #include "dir.h"
5 #include "user.h"
6 #include "mbuf.h"
7 #include "domain.h"
8 #include "protosw.h"
9 #include "socket.h"
10 #include "socketvar.h"
11 #include "unpcb.h"
12 #include "un.h"
13 #include "inode.h"
14 #include "file.h"
15 #include "stat.h"
16 
17 /*
18  * Unix communications domain.
19  *
20  * TODO:
21  *	SEQPACKET, RDM
22  *	rethink name space problems
23  *	need a proper out-of-band
24  */
25 struct	sockaddr sun_noname = { AF_UNIX };
26 
27 /*ARGSUSED*/
28 uipc_usrreq(so, req, m, nam, rights)
29 	struct socket *so;
30 	int req;
31 	struct mbuf *m, *nam, *rights;
32 {
33 	struct unpcb *unp = sotounpcb(so);
34 	register struct socket *so2;
35 	int error = 0;
36 
37 	if (req != PRU_SEND && rights && rights->m_len) {
38 		error = EOPNOTSUPP;
39 		goto release;
40 	}
41 	if (unp == 0 && req != PRU_ATTACH) {
42 		error = EINVAL;
43 		goto release;
44 	}
45 	switch (req) {
46 
47 	case PRU_ATTACH:
48 		if (unp) {
49 			error = EISCONN;
50 			break;
51 		}
52 		error = unp_attach(so);
53 		break;
54 
55 	case PRU_DETACH:
56 		unp_detach(unp);
57 		break;
58 
59 	case PRU_BIND:
60 		error = unp_bind(unp, nam);
61 		break;
62 
63 	case PRU_LISTEN:
64 		if (unp->unp_inode == 0)
65 			error = EINVAL;
66 		break;
67 
68 	case PRU_CONNECT:
69 		error = unp_connect(so, nam);
70 		break;
71 
72 	case PRU_CONNECT2:
73 		error = unp_connect2(so, (struct mbuf *)0,
74 		    (struct socket *)nam);
75 		break;
76 
77 	case PRU_DISCONNECT:
78 		unp_disconnect(unp);
79 		break;
80 
81 	case PRU_ACCEPT:
82 		nam->m_len = unp->unp_remaddr->m_len;
83 		bcopy(mtod(unp->unp_remaddr, caddr_t),
84 		    mtod(nam, caddr_t), (unsigned)nam->m_len);
85 		break;
86 
87 	case PRU_SHUTDOWN:
88 		socantsendmore(so);
89 		unp_usrclosed(unp);
90 		break;
91 
92 	case PRU_RCVD:
93 		switch (so->so_type) {
94 
95 		case SOCK_DGRAM:
96 			panic("uipc 1");
97 			/*NOTREACHED*/
98 
99 		case SOCK_STREAM:
100 #define	rcv (&so->so_rcv)
101 #define snd (&so2->so_snd)
102 			if (unp->unp_conn == 0)
103 				break;
104 			so2 = unp->unp_conn->unp_socket;
105 			/*
106 			 * Transfer resources back to send port
107 			 * and wakeup any waiting to write.
108 			 */
109 			snd->sb_mbmax += rcv->sb_mbmax - rcv->sb_mbcnt;
110 			rcv->sb_mbmax = rcv->sb_mbcnt;
111 			snd->sb_hiwat += rcv->sb_hiwat - rcv->sb_cc;
112 			rcv->sb_hiwat = rcv->sb_cc;
113 			sowwakeup(so2);
114 #undef snd
115 #undef rcv
116 			break;
117 
118 		default:
119 			panic("uipc 2");
120 		}
121 		break;
122 
123 	case PRU_SEND:
124 		switch (so->so_type) {
125 
126 		case SOCK_DGRAM:
127 			if (nam) {
128 				if (unp->unp_conn) {
129 					error = EISCONN;
130 					break;
131 				}
132 				error = unp_connect(so, nam);
133 				if (error)
134 					break;
135 			} else {
136 				if (unp->unp_conn == 0) {
137 					error = ENOTCONN;
138 					break;
139 				}
140 			}
141 			so2 = unp->unp_conn->unp_socket;
142 			/* BEGIN XXX */
143 			if (rights) {
144 				error = unp_internalize(rights);
145 				if (error)
146 					break;
147 			}
148 			if (sbspace(&so2->so_rcv) > 0) {
149 				/*
150 				 * There's no record of source socket's
151 				 * name, so send null name for the moment.
152 				 */
153 				if (sbappendaddr(&so2->so_rcv,
154 				    &sun_noname, m, rights)) {
155 					sorwakeup(so2);
156 					m = 0;
157 				}
158 			}
159 			/* END XXX */
160 			if (nam)
161 				unp_disconnect(unp);
162 			break;
163 
164 		case SOCK_STREAM:
165 #define	rcv (&so2->so_rcv)
166 #define	snd (&so->so_snd)
167 			if (rights && rights->m_len) {
168 				error = EOPNOTSUPP;
169 				break;
170 			}
171 			if (unp->unp_conn == 0)
172 				panic("uipc 3");
173 			so2 = unp->unp_conn->unp_socket;
174 			/*
175 			 * Send to paired receive port, and then
176 			 * give it enough resources to hold what it already has.
177 			 * Wake up readers.
178 			 */
179 			sbappend(rcv, m);
180 			snd->sb_mbmax -= rcv->sb_mbcnt - rcv->sb_mbmax;
181 			rcv->sb_mbmax = rcv->sb_mbcnt;
182 			snd->sb_hiwat -= rcv->sb_cc - rcv->sb_hiwat;
183 			rcv->sb_hiwat = rcv->sb_cc;
184 			sorwakeup(so2);
185 			m = 0;
186 #undef snd
187 #undef rcv
188 			break;
189 
190 		default:
191 			panic("uipc 4");
192 		}
193 		break;
194 
195 	case PRU_ABORT:
196 		unp_drop(unp, ECONNABORTED);
197 		break;
198 
199 /* SOME AS YET UNIMPLEMENTED HOOKS */
200 	case PRU_CONTROL:
201 		return (EOPNOTSUPP);
202 
203 /* END UNIMPLEMENTED HOOKS */
204 	case PRU_SENSE:
205 		((struct stat *) m)->st_blksize = so->so_snd.sb_hiwat;
206 		if (so->so_type == SOCK_STREAM && unp->unp_conn != 0) {
207 			so2 = unp->unp_conn->unp_socket;
208 			((struct stat *) m)->st_blksize += so2->so_rcv.sb_cc;
209 		}
210 		return (0);
211 
212 	case PRU_RCVOOB:
213 		return (EOPNOTSUPP);
214 
215 	case PRU_SENDOOB:
216 		error = EOPNOTSUPP;
217 		break;
218 
219 	case PRU_SOCKADDR:
220 		break;
221 
222 	case PRU_PEERADDR:
223 		break;
224 
225 	case PRU_SLOWTIMO:
226 		break;
227 
228 	default:
229 		panic("piusrreq");
230 	}
231 release:
232 	if (m)
233 		m_freem(m);
234 	return (error);
235 }
236 
237 /*
238  * We assign all buffering for stream sockets to the source,
239  * as that is where the flow control is implemented.
240  * Datagram sockets really use the sendspace as the maximum datagram size,
241  * and don't really want to reserve the sendspace.  Their recvspace should
242  * be large enough for at least one max-size datagram plus address.
243  */
244 #define	PIPSIZ	4096
245 int	unpst_sendspace = PIPSIZ;
246 int	unpst_recvspace = 0;
247 int	unpdg_sendspace = 2*1024;	/* really max datagram size */
248 int	unpdg_recvspace = 4*1024;
249 
250 unp_attach(so)
251 	struct socket *so;
252 {
253 	register struct mbuf *m;
254 	register struct unpcb *unp;
255 	int error;
256 
257 	switch (so->so_type) {
258 
259 	case SOCK_STREAM:
260 		error = soreserve(so, unpst_sendspace, unpst_recvspace);
261 		break;
262 
263 	case SOCK_DGRAM:
264 		error = soreserve(so, unpdg_sendspace, unpdg_recvspace);
265 		break;
266 	}
267 	if (error)
268 		return (error);
269 	m = m_getclr(M_DONTWAIT, MT_PCB);
270 	if (m == NULL)
271 		return (ENOBUFS);
272 	unp = mtod(m, struct unpcb *);
273 	so->so_pcb = (caddr_t)unp;
274 	unp->unp_socket = so;
275 	return (0);
276 }
277 
278 unp_detach(unp)
279 	register struct unpcb *unp;
280 {
281 
282 	if (unp->unp_inode) {
283 		unp->unp_inode->i_socket = 0;
284 		irele(unp->unp_inode);
285 		unp->unp_inode = 0;
286 	}
287 	if (unp->unp_conn)
288 		unp_disconnect(unp);
289 	while (unp->unp_refs)
290 		unp_drop(unp->unp_refs, ECONNRESET);
291 	soisdisconnected(unp->unp_socket);
292 	unp->unp_socket->so_pcb = 0;
293 	m_freem(unp->unp_remaddr);
294 	(void) m_free(dtom(unp));
295 }
296 
297 unp_bind(unp, nam)
298 	struct unpcb *unp;
299 	struct mbuf *nam;
300 {
301 	struct sockaddr_un *soun = mtod(nam, struct sockaddr_un *);
302 	register struct inode *ip;
303 	register struct nameidata *ndp = &u.u_nd;
304 	int error;
305 
306 	ndp->ni_dirp = soun->sun_path;
307 	if (nam->m_len == MLEN)
308 		return (EINVAL);
309 	*(mtod(nam, caddr_t) + nam->m_len) = 0;
310 /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
311 	ndp->ni_nameiop = CREATE | FOLLOW;
312 	ndp->ni_segflg = UIO_SYSSPACE;
313 	ip = namei(ndp);
314 	if (ip) {
315 		iput(ip);
316 		return (EADDRINUSE);
317 	}
318 	if (error = u.u_error) {
319 		u.u_error = 0;			/* XXX */
320 		return (error);
321 	}
322 	ip = maknode(IFSOCK | 0777, ndp);
323 	if (ip == NULL) {
324 		error = u.u_error;		/* XXX */
325 		u.u_error = 0;			/* XXX */
326 		return (error);
327 	}
328 	ip->i_socket = unp->unp_socket;
329 	unp->unp_inode = ip;
330 	iunlock(ip);			/* but keep reference */
331 	return (0);
332 }
333 
334 unp_connect(so, nam)
335 	struct socket *so;
336 	struct mbuf *nam;
337 {
338 	register struct sockaddr_un *soun = mtod(nam, struct sockaddr_un *);
339 	register struct inode *ip;
340 	int error;
341 	register struct socket *so2;
342 	register struct nameidata *ndp = &u.u_nd;
343 
344 	ndp->ni_dirp = soun->sun_path;
345 	if (nam->m_len + (nam->m_off - MMINOFF) == MLEN)
346 		return (EMSGSIZE);
347 	*(mtod(nam, caddr_t) + nam->m_len) = 0;
348 	ndp->ni_nameiop = LOOKUP | FOLLOW;
349 	ndp->ni_segflg = UIO_SYSSPACE;
350 	ip = namei(ndp);
351 	if (ip == 0) {
352 		error = u.u_error;
353 		u.u_error = 0;
354 		return (error);		/* XXX */
355 	}
356 	if (access(ip, IWRITE)) {
357 		error = u.u_error;
358 		u.u_error = 0; 		/* XXX */
359 		goto bad;
360 	}
361 	if ((ip->i_mode&IFMT) != IFSOCK) {
362 		error = ENOTSOCK;
363 		goto bad;
364 	}
365 	so2 = ip->i_socket;
366 	if (so2 == 0) {
367 		error = ECONNREFUSED;
368 		goto bad;
369 	}
370 	if (so->so_type != so2->so_type) {
371 		error = EPROTOTYPE;
372 		goto bad;
373 	}
374 	if (so->so_proto->pr_flags & PR_CONNREQUIRED &&
375 	    ((so2->so_options&SO_ACCEPTCONN) == 0 ||
376 	     (so2 = sonewconn(so2)) == 0)) {
377 		error = ECONNREFUSED;
378 		goto bad;
379 	}
380 	error = unp_connect2(so, nam, so2);
381 bad:
382 	iput(ip);
383 	return (error);
384 }
385 
386 unp_connect2(so, sonam, so2)
387 	register struct socket *so;
388 	struct mbuf *sonam;
389 	register struct socket *so2;
390 {
391 	register struct unpcb *unp = sotounpcb(so);
392 	register struct unpcb *unp2;
393 
394 	if (so2->so_type != so->so_type)
395 		return (EPROTOTYPE);
396 	unp2 = sotounpcb(so2);
397 	unp->unp_conn = unp2;
398 	switch (so->so_type) {
399 
400 	case SOCK_DGRAM:
401 		unp->unp_nextref = unp2->unp_refs;
402 		unp2->unp_refs = unp;
403 		soisconnected(so);
404 		break;
405 
406 	case SOCK_STREAM:
407 		unp2->unp_conn = unp;
408 		if (sonam)
409 			unp2->unp_remaddr = m_copy(sonam, 0, (int)M_COPYALL);
410 		soisconnected(so2);
411 		soisconnected(so);
412 		break;
413 
414 	default:
415 		panic("unp_connect2");
416 	}
417 	return (0);
418 }
419 
420 unp_disconnect(unp)
421 	struct unpcb *unp;
422 {
423 	register struct unpcb *unp2 = unp->unp_conn;
424 
425 	if (unp2 == 0)
426 		return;
427 	unp->unp_conn = 0;
428 	switch (unp->unp_socket->so_type) {
429 
430 	case SOCK_DGRAM:
431 		if (unp2->unp_refs == unp)
432 			unp2->unp_refs = unp->unp_nextref;
433 		else {
434 			unp2 = unp2->unp_refs;
435 			for (;;) {
436 				if (unp2 == 0)
437 					panic("unp_disconnect");
438 				if (unp2->unp_nextref == unp)
439 					break;
440 				unp2 = unp2->unp_nextref;
441 			}
442 			unp2->unp_nextref = unp->unp_nextref;
443 		}
444 		unp->unp_nextref = 0;
445 		break;
446 
447 	case SOCK_STREAM:
448 		soisdisconnected(unp->unp_socket);
449 		unp2->unp_conn = 0;
450 		soisdisconnected(unp2->unp_socket);
451 		break;
452 	}
453 }
454 
455 #ifdef notdef
456 unp_abort(unp)
457 	struct unpcb *unp;
458 {
459 
460 	unp_detach(unp);
461 }
462 #endif
463 
464 /*ARGSUSED*/
465 unp_usrclosed(unp)
466 	struct unpcb *unp;
467 {
468 
469 }
470 
471 unp_drop(unp, errno)
472 	struct unpcb *unp;
473 	int errno;
474 {
475 	struct socket *so = unp->unp_socket;
476 
477 	so->so_error = errno;
478 	unp_disconnect(unp);
479 	if (so->so_head) {
480 		so->so_pcb = (caddr_t) 0;
481 		m_freem(unp->unp_remaddr);
482 		(void) m_free(dtom(unp));
483 		sofree(so);
484 	}
485 }
486 
487 #ifdef notdef
488 unp_drain()
489 {
490 
491 }
492 #endif
493 
494 unp_externalize(rights)
495 	struct mbuf *rights;
496 {
497 	int newfds = rights->m_len / sizeof (int);
498 	register int i;
499 	register struct file **rp = mtod(rights, struct file **);
500 	register struct file *fp;
501 	int f;
502 
503 	if (newfds > ufavail()) {
504 		for (i = 0; i < newfds; i++) {
505 			fp = *rp;
506 			unp_discard(fp);
507 			*rp++ = 0;
508 		}
509 		return (EMSGSIZE);
510 	}
511 	for (i = 0; i < newfds; i++) {
512 		f = ufalloc(0);
513 		if (f < 0)
514 			panic("unp_externalize");
515 		fp = *rp;
516 		u.u_ofile[f] = fp;
517 		fp->f_msgcount--;
518 		*(int *)rp++ = f;
519 	}
520 	return (0);
521 }
522 
523 unp_internalize(rights)
524 	struct mbuf *rights;
525 {
526 	register struct file **rp;
527 	int oldfds = rights->m_len / sizeof (int);
528 	register int i;
529 	register struct file *fp;
530 
531 	rp = mtod(rights, struct file **);
532 	for (i = 0; i < oldfds; i++)
533 		if (getf(*(int *)rp++) == 0)
534 			return (EBADF);
535 	rp = mtod(rights, struct file **);
536 	for (i = 0; i < oldfds; i++) {
537 		fp = getf(*(int *)rp);
538 		*rp++ = fp;
539 		fp->f_count++;
540 		fp->f_msgcount++;
541 	}
542 	return (0);
543 }
544 
545 int	unp_defer, unp_gcing;
546 int	unp_mark();
547 extern	struct domain unixdomain;
548 
549 unp_gc()
550 {
551 	register struct file *fp;
552 	register struct socket *so;
553 
554 	if (unp_gcing)
555 		return;
556 	unp_gcing = 1;
557 restart:
558 	unp_defer = 0;
559 	for (fp = file; fp < fileNFILE; fp++)
560 		fp->f_flag &= ~(FMARK|FDEFER);
561 	do {
562 		for (fp = file; fp < fileNFILE; fp++) {
563 			if (fp->f_count == 0)
564 				continue;
565 			if (fp->f_flag & FDEFER) {
566 				fp->f_flag &= ~FDEFER;
567 				unp_defer--;
568 			} else {
569 				if (fp->f_flag & FMARK)
570 					continue;
571 				if (fp->f_count == fp->f_msgcount)
572 					continue;
573 				fp->f_flag |= FMARK;
574 			}
575 			if (fp->f_type != DTYPE_SOCKET)
576 				continue;
577 			so = (struct socket *)fp->f_data;
578 			if (so->so_proto->pr_domain != &unixdomain ||
579 			    (so->so_proto->pr_flags&PR_ADDR) == 0)
580 				continue;
581 			if (so->so_rcv.sb_flags & SB_LOCK) {
582 				sbwait(&so->so_rcv);
583 				goto restart;
584 			}
585 			unp_scan(so->so_rcv.sb_mb, unp_mark);
586 		}
587 	} while (unp_defer);
588 	for (fp = file; fp < fileNFILE; fp++) {
589 		if (fp->f_count == 0)
590 			continue;
591 		if (fp->f_count == fp->f_msgcount && (fp->f_flag&FMARK)==0) {
592 			if (fp->f_type != DTYPE_SOCKET)
593 				panic("unp_gc");
594 			(void) soshutdown((struct socket *)fp->f_data, 0);
595 		}
596 	}
597 	unp_gcing = 0;
598 }
599 
600 unp_dispose(m)
601 	struct mbuf *m;
602 {
603 	int unp_discard();
604 
605 	if (m)
606 		unp_scan(m, unp_discard);
607 }
608 
609 unp_scan(m0, op)
610 	register struct mbuf *m0;
611 	int (*op)();
612 {
613 	register struct mbuf *m;
614 	register struct file **rp;
615 	register int i;
616 	int qfds;
617 
618 	while (m0) {
619 		for (m = m0; m; m = m->m_next)
620 			if (m->m_type == MT_RIGHTS && m->m_len) {
621 				qfds = m->m_len / sizeof (struct file *);
622 				rp = mtod(m, struct file **);
623 				for (i = 0; i < qfds; i++)
624 					(*op)(*rp++);
625 				break;		/* XXX, but saves time */
626 			}
627 		m0 = m0->m_act;
628 	}
629 }
630 
631 unp_mark(fp)
632 	struct file *fp;
633 {
634 
635 	if (fp->f_flag & FMARK)
636 		return;
637 	unp_defer++;
638 	fp->f_flag |= (FMARK|FDEFER);
639 }
640 
641 unp_discard(fp)
642 	struct file *fp;
643 {
644 
645 	fp->f_msgcount--;
646 	closef(fp);
647 }
648