xref: /csrg-svn/sys/kern/uipc_usrreq.c (revision 10139)
1*10139Ssam /*	uipc_usrreq.c	1.6	83/01/04	*/
28925Sroot 
38925Sroot #include "../h/param.h"
48925Sroot #include "../h/dir.h"
58925Sroot #include "../h/user.h"
68925Sroot #include "../h/mbuf.h"
78925Sroot #include "../h/protosw.h"
88925Sroot #include "../h/socket.h"
98925Sroot #include "../h/socketvar.h"
108925Sroot #include "../h/unpcb.h"
118925Sroot #include "../h/un.h"
128925Sroot #include "../h/inode.h"
139169Ssam #include "../h/nami.h"
148925Sroot 
158925Sroot /*
168925Sroot  * Unix communications domain.
178925Sroot  */
188925Sroot 
198925Sroot /*ARGSUSED*/
209028Sroot uipc_usrreq(so, req, m, nam, opt)
218925Sroot 	struct socket *so;
228925Sroot 	int req;
239028Sroot 	struct mbuf *m, *nam;
249028Sroot 	struct socketopt *opt;
258925Sroot {
268925Sroot 	struct unpcb *unp = sotounpcb(so);
278925Sroot 	register struct socket *so2;
288925Sroot 	int error = 0;
298925Sroot 
308925Sroot 	if (unp == 0 && req != PRU_ATTACH)
318925Sroot 		return (EINVAL);			/* XXX */
328925Sroot 	switch (req) {
338925Sroot 
348925Sroot 	case PRU_ATTACH:
358925Sroot 		if (unp) {
369169Ssam 			error = EISCONN;
378925Sroot 			break;
388925Sroot 		}
399028Sroot 		error = unp_attach(so);
408925Sroot 		break;
418925Sroot 
428925Sroot 	case PRU_DETACH:
438925Sroot 		unp_detach(unp);
448925Sroot 		break;
458925Sroot 
469169Ssam 	case PRU_BIND:
479169Ssam 		error = unp_bind(unp, nam);
489169Ssam 		break;
499169Ssam 
509169Ssam 	case PRU_LISTEN:
519169Ssam 		if (unp->unp_inode == 0)
529169Ssam 			error = EINVAL;
539169Ssam 		break;
549169Ssam 
558925Sroot 	case PRU_CONNECT:
569028Sroot 		error = unp_connect(so, nam);
578925Sroot 		break;
588925Sroot 
598925Sroot 	case PRU_DISCONNECT:
608925Sroot 		unp_disconnect(unp);
618925Sroot 		break;
628925Sroot 
639169Ssam 	case PRU_ACCEPT:
649169Ssam 		nam->m_len = unp->unp_remaddr->m_len;
659169Ssam 		bcopy(mtod(unp->unp_remaddr, caddr_t),
669169Ssam 		    mtod(nam, caddr_t), (unsigned)nam->m_len);
678925Sroot 		break;
688925Sroot 
698925Sroot 	case PRU_SHUTDOWN:
708925Sroot 		socantsendmore(so);
718925Sroot 		unp_usrclosed(unp);
728925Sroot 		break;
738925Sroot 
748925Sroot 	case PRU_RCVD:
758925Sroot 		switch (so->so_type) {
768925Sroot 
778925Sroot 		case SOCK_DGRAM:
788925Sroot 			panic("uipc 1");
79*10139Ssam 			/*NOTREACHED*/
808925Sroot 
81*10139Ssam 		case SOCK_STREAM:
828925Sroot #define	rcv (&so->so_rcv)
838925Sroot #define snd (&so2->so_snd)
848925Sroot 			if (unp->unp_conn == 0)
858925Sroot 				break;
868925Sroot 			so2 = unp->unp_conn->unp_socket;
878925Sroot 			/*
888925Sroot 			 * Transfer resources back to send port
898925Sroot 			 * and wakeup any waiting to write.
908925Sroot 			 */
918925Sroot 			snd->sb_mbmax += rcv->sb_mbmax - rcv->sb_mbcnt;
928925Sroot 			rcv->sb_mbmax = rcv->sb_mbcnt;
938925Sroot 			snd->sb_hiwat += rcv->sb_hiwat - rcv->sb_cc;
948925Sroot 			rcv->sb_hiwat = rcv->sb_cc;
958925Sroot 			sbwakeup(snd);
968925Sroot #undef snd
978925Sroot #undef rcv
988925Sroot 			break;
998925Sroot 
1008925Sroot 		default:
1018925Sroot 			panic("uipc 2");
1028925Sroot 		}
1038925Sroot 		break;
1048925Sroot 
1058925Sroot 	case PRU_SEND:
1068925Sroot 		switch (so->so_type) {
1078925Sroot 
1088925Sroot 		case SOCK_DGRAM:
1099028Sroot 			if (nam) {
1108925Sroot 				if (unp->unp_conn) {
1118925Sroot 					error = EISCONN;
1128925Sroot 					break;
1138925Sroot 				}
1149028Sroot 				error = unp_connect(so, nam);
1158925Sroot 				if (error)
1168925Sroot 					break;
1178925Sroot 			} else {
1188925Sroot 				if (unp->unp_conn == 0) {
1198925Sroot 					error = ENOTCONN;
1208925Sroot 					break;
1218925Sroot 				}
1228925Sroot 			}
1238925Sroot 			so2 = unp->unp_conn->unp_socket;
1249169Ssam 			/* BEGIN XXX */
1259169Ssam 			if (sbspace(&so2->so_rcv) > 0)
1269169Ssam 				(void) sbappendaddr(&so2->so_rcv,
1279169Ssam 					mtod(nam, struct sockaddr *), m);
1289169Ssam 			/* END XXX */
1299028Sroot 			if (nam)
1309169Ssam 				unp_disconnect(unp);
1318925Sroot 			break;
1328925Sroot 
1338925Sroot 		case SOCK_STREAM:
1348925Sroot #define	rcv (&so2->so_rcv)
1358925Sroot #define	snd (&so->so_snd)
1368925Sroot 			if (unp->unp_conn == 0)
1378925Sroot 				panic("uipc 3");
1388925Sroot 			so2 = unp->unp_conn->unp_socket;
1398925Sroot 			/*
1408925Sroot 			 * Send to paired receive port, and then
1418925Sroot 			 * give it enough resources to hold what it already has.
1428925Sroot 			 * Wake up readers.
1438925Sroot 			 */
1448925Sroot 			sbappend(rcv, m);
1458925Sroot 			snd->sb_mbmax -= rcv->sb_mbcnt - rcv->sb_mbmax;
1468925Sroot 			rcv->sb_mbmax = rcv->sb_mbcnt;
1478925Sroot 			snd->sb_hiwat -= rcv->sb_cc - rcv->sb_hiwat;
1488925Sroot 			rcv->sb_hiwat = rcv->sb_cc;
1498925Sroot 			sbwakeup(rcv);
1508925Sroot #undef snd
1518925Sroot #undef rcv
1528925Sroot 			break;
1538925Sroot 
1548925Sroot 		default:
1558925Sroot 			panic("uipc 4");
1568925Sroot 		}
1578925Sroot 		break;
1588925Sroot 
1598925Sroot 	case PRU_ABORT:
1608925Sroot 		unp_drop(unp, ECONNABORTED);
1618925Sroot 		break;
1628925Sroot 
1638925Sroot /* SOME AS YET UNIMPLEMENTED HOOKS */
1648925Sroot 	case PRU_CONTROL:
1658925Sroot 		error = EOPNOTSUPP;
1668925Sroot 		break;
1678925Sroot 
1688925Sroot 	case PRU_SENSE:
1698925Sroot 		error = EOPNOTSUPP;
1708925Sroot 		break;
1718925Sroot /* END UNIMPLEMENTED HOOKS */
1728925Sroot 
1738925Sroot 	case PRU_RCVOOB:
1748925Sroot 		break;
1758925Sroot 
1768925Sroot 	case PRU_SENDOOB:
1778925Sroot 		break;
1788925Sroot 
1798925Sroot 	case PRU_SOCKADDR:
1808925Sroot 		break;
1818925Sroot 
1828925Sroot 	case PRU_SLOWTIMO:
1838925Sroot 		break;
1848925Sroot 
1858925Sroot 	default:
1868925Sroot 		panic("piusrreq");
1878925Sroot 	}
1888925Sroot 	return (0);
1898925Sroot }
1908925Sroot 
1918925Sroot int	unp_sendspace = 1024*2;
1928925Sroot int	unp_recvspace = 1024*2;
1938925Sroot 
1949169Ssam unp_attach(so)
1958925Sroot 	struct socket *so;
1968925Sroot {
1979169Ssam 	register struct mbuf *m;
1988925Sroot 	register struct unpcb *unp;
1998925Sroot 	int error;
2008925Sroot 
2018925Sroot 	error = soreserve(so, unp_sendspace, unp_recvspace);
2028925Sroot 	if (error)
203*10139Ssam 		return (error);
2049637Ssam 	m = m_getclr(M_DONTWAIT, MT_PCB);
205*10139Ssam 	if (m == NULL)
206*10139Ssam 		return (ENOBUFS);
2078925Sroot 	unp = mtod(m, struct unpcb *);
2088925Sroot 	so->so_pcb = (caddr_t)unp;
2098925Sroot 	unp->unp_socket = so;
2108925Sroot 	return (0);
2118925Sroot }
2128925Sroot 
2138925Sroot unp_detach(unp)
2149169Ssam 	register struct unpcb *unp;
2158925Sroot {
2168925Sroot 
2178925Sroot 	if (unp->unp_inode) {
2188925Sroot 		irele(unp->unp_inode);
2198925Sroot 		unp->unp_inode = 0;
2208925Sroot 	}
2218925Sroot 	if (unp->unp_conn)
2228925Sroot 		unp_disconnect(unp);
2238925Sroot 	while (unp->unp_refs)
2248925Sroot 		unp_drop(unp->unp_refs, ECONNRESET);
2258925Sroot 	soisdisconnected(unp->unp_socket);
2268925Sroot 	unp->unp_socket->so_pcb = 0;
2279169Ssam 	m_freem(unp->unp_remaddr);
2289169Ssam 	(void) m_free(dtom(unp));
2298925Sroot }
2308925Sroot 
2319169Ssam unp_bind(unp, nam)
2328925Sroot 	struct unpcb *unp;
2339169Ssam 	struct mbuf *nam;
2348925Sroot {
2359169Ssam 	struct sockaddr_un *soun = mtod(nam, struct sockaddr_un *);
2368925Sroot 	register struct inode *ip;
2379169Ssam 	extern schar();
2388925Sroot 	int error;
2398925Sroot 
2408925Sroot 	u.u_dirp = soun->sun_path;
2418925Sroot 	soun->sun_path[sizeof(soun->sun_path)-1] = 0;
2429169Ssam 	ip = namei(schar, CREATE, 1);
2438925Sroot 	if (ip) {
2448925Sroot 		iput(ip);
245*10139Ssam 		return (EADDRINUSE);
2468925Sroot 	}
2478925Sroot 	ip = maknode(IFSOCK | 0777);
2488925Sroot 	if (ip == NULL) {
2498925Sroot 		error = u.u_error;		/* XXX */
2508925Sroot 		u.u_error = 0;			/* XXX */
2518925Sroot 		return (error);
2528925Sroot 	}
2538925Sroot 	ip->i_socket = unp->unp_socket;
2548925Sroot 	unp->unp_inode = ip;
2558925Sroot 	iunlock(ip);			/* but keep reference */
2568925Sroot 	return (0);
2578925Sroot }
2588925Sroot 
2599169Ssam unp_connect(so, nam)
2608925Sroot 	struct socket *so;
2619169Ssam 	struct mbuf *nam;
2628925Sroot {
2639169Ssam 	register struct sockaddr_un *soun = mtod(nam, struct sockaddr_un *);
2649169Ssam 	struct unpcb *unp = sotounpcb(so);
2659169Ssam 	register struct inode *ip;
2668925Sroot 	int error;
2679169Ssam 	struct socket *so2;
2689169Ssam 	struct unpcb *unp2;
2698925Sroot 
2708925Sroot 	u.u_dirp = soun->sun_path;
2718925Sroot 	soun->sun_path[sizeof(soun->sun_path)-1] = 0;
2729169Ssam 	ip = namei(schar, LOOKUP, 1);
2738925Sroot 	if (ip == 0) {
2748925Sroot 		error = u.u_error;
2758925Sroot 		u.u_error = 0;
276*10139Ssam 		return (error);		/* XXX */
2778925Sroot 	}
2788925Sroot 	if ((ip->i_mode&IFMT) != IFSOCK) {
2798925Sroot 		error = ENOTSOCK;
2808925Sroot 		goto bad;
2818925Sroot 	}
2828925Sroot 	so2 = ip->i_socket;
2838925Sroot 	if (so2 == 0) {
2848925Sroot 		error = ECONNREFUSED;
2858925Sroot 		goto bad;
2868925Sroot 	}
2878925Sroot 	if (so2->so_type != so->so_type) {
2888925Sroot 		error = EPROTOTYPE;
2898925Sroot 		goto bad;
2908925Sroot 	}
2918925Sroot 	switch (so->so_type) {
2928925Sroot 
2938925Sroot 	case SOCK_DGRAM:
2948925Sroot 		unp->unp_conn = sotounpcb(so2);
2958925Sroot 		unp2 = sotounpcb(so2);
2968925Sroot 		unp->unp_nextref = unp2->unp_refs;
2978925Sroot 		unp2->unp_refs = unp;
2988925Sroot 		break;
2998925Sroot 
3008925Sroot 	case SOCK_STREAM:
3018925Sroot 		if ((so2->so_options&SO_ACCEPTCONN) == 0 ||
3029169Ssam 		    (so2 = sonewconn(so2)) == 0) {
3038925Sroot 			error = ECONNREFUSED;
3048925Sroot 			goto bad;
3058925Sroot 		}
3069169Ssam 		unp2 = sotounpcb(so2);
3079169Ssam 		unp->unp_conn = unp2;
3089169Ssam 		unp2->unp_conn = unp;
3099169Ssam 		unp2->unp_remaddr = m_copy(nam, 0, (int)M_COPYALL);
3108925Sroot 		break;
3118925Sroot 
3128925Sroot 	default:
3138925Sroot 		panic("uipc connip");
3148925Sroot 	}
3159169Ssam 	soisconnected(so2);
3168925Sroot 	soisconnected(so);
3178925Sroot 	iput(ip);
3188925Sroot 	return (0);
3198925Sroot bad:
3208925Sroot 	iput(ip);
3218925Sroot 	return (error);
3228925Sroot }
3239169Ssam 
3249169Ssam unp_disconnect(unp)
3259169Ssam 	struct unpcb *unp;
3269169Ssam {
3279169Ssam 	register struct unpcb *unp2 = unp->unp_conn;
3289169Ssam 
3299169Ssam 	if (unp2 == 0)
3309169Ssam 		return;
3319169Ssam 	unp->unp_conn = 0;
3329169Ssam 	soisdisconnected(unp->unp_socket);
3339169Ssam 	switch (unp->unp_socket->so_type) {
3349169Ssam 
3359169Ssam 	case SOCK_DGRAM:
3369169Ssam 		if (unp2->unp_refs == unp)
3379169Ssam 			unp2->unp_refs = unp->unp_nextref;
3389169Ssam 		else {
3399169Ssam 			unp2 = unp2->unp_refs;
3409169Ssam 			for (;;) {
3419169Ssam 				if (unp2 == 0)
3429169Ssam 					panic("unp_disconnect");
3439169Ssam 				if (unp2->unp_nextref == unp)
3449169Ssam 					break;
3459169Ssam 				unp2 = unp2->unp_nextref;
3469169Ssam 			}
3479169Ssam 			unp2->unp_nextref = unp->unp_nextref;
3489169Ssam 		}
3499169Ssam 		unp->unp_nextref = 0;
3509169Ssam 		break;
3519169Ssam 
3529169Ssam 	case SOCK_STREAM:
3539169Ssam 		unp2->unp_conn = 0;
3549169Ssam 		soisdisconnected(unp2->unp_socket);
3559169Ssam 		break;
3569169Ssam 	}
3579169Ssam }
3589169Ssam 
3599169Ssam unp_abort(unp)
3609169Ssam 	struct unpcb *unp;
3619169Ssam {
3629169Ssam 
3639169Ssam 	unp_detach(unp);
3649169Ssam }
3659169Ssam 
3669169Ssam /*ARGSUSED*/
3679169Ssam unp_usrclosed(unp)
3689169Ssam 	struct unpcb *unp;
3699169Ssam {
3709169Ssam 
3719169Ssam }
3729169Ssam 
3739169Ssam unp_drop(unp, errno)
3749169Ssam 	struct unpcb *unp;
3759169Ssam 	int errno;
3769169Ssam {
3779169Ssam 
3789169Ssam 	unp->unp_socket->so_error = errno;
3799169Ssam 	unp_disconnect(unp);
3809169Ssam }
3819169Ssam 
3829169Ssam unp_drain()
3839169Ssam {
3849169Ssam 
3859169Ssam }
386