xref: /csrg-svn/sys/kern/uipc_socket.c (revision 5610)
1*5610Swnj /*	uipc_socket.c	4.30	82/01/24	*/
24786Swnj 
34786Swnj #include "../h/param.h"
44829Swnj #include "../h/systm.h"
54786Swnj #include "../h/dir.h"
64786Swnj #include "../h/user.h"
74829Swnj #include "../h/proc.h"
84829Swnj #include "../h/file.h"
94786Swnj #include "../h/inode.h"
104829Swnj #include "../h/buf.h"
114786Swnj #include "../h/mbuf.h"
124829Swnj #include "../h/protosw.h"
134829Swnj #include "../h/socket.h"
144829Swnj #include "../h/socketvar.h"
154916Swnj #include "../h/stat.h"
165281Sroot #include "../h/ioctl.h"
175095Swnj #include "../net/in.h"
185095Swnj #include "../net/in_systm.h"
194786Swnj 
204786Swnj /*
214890Swnj  * Socket support routines.
224890Swnj  *
234890Swnj  * DEAL WITH INTERRUPT NOTIFICATION.
244786Swnj  */
254786Swnj 
264786Swnj /*
274786Swnj  * Create a socket.
284786Swnj  */
294927Swnj socreate(aso, type, asp, asa, options)
304786Swnj 	struct socket **aso;
314786Swnj 	int type;
324927Swnj 	struct sockproto *asp;
334927Swnj 	struct sockaddr *asa;
344829Swnj 	int options;
354786Swnj {
364786Swnj 	register struct protosw *prp;
374786Swnj 	register struct socket *so;
384786Swnj 	struct mbuf *m;
394890Swnj 	int pf, proto, error;
404927Swnj COUNT(SOCREATE);
414786Swnj 
424786Swnj 	/*
434890Swnj 	 * Use process standard protocol/protocol family if none
444890Swnj 	 * specified by address argument.
454786Swnj 	 */
464927Swnj 	if (asp == 0) {
474890Swnj 		pf = PF_INET;		/* should be u.u_protof */
484786Swnj 		proto = 0;
494786Swnj 	} else {
504927Swnj 		pf = asp->sp_family;
514927Swnj 		proto = asp->sp_protocol;
524786Swnj 	}
534786Swnj 
544786Swnj 	/*
554890Swnj 	 * If protocol specified, look for it, otherwise
564890Swnj 	 * for a protocol of the correct type in the right family.
574890Swnj 	 */
584890Swnj 	if (proto)
594890Swnj 		prp = pffindproto(pf, proto);
604890Swnj 	else
614890Swnj 		prp = pffindtype(pf, type);
624890Swnj 	if (prp == 0)
634890Swnj 		return (EPROTONOSUPPORT);
644890Swnj 
654890Swnj 	/*
664786Swnj 	 * Get a socket structure.
674786Swnj 	 */
684890Swnj 	m = m_getclr(M_WAIT);
694786Swnj 	if (m == 0)
704786Swnj 		return (ENOBUFS);
714786Swnj 	so = mtod(m, struct socket *);
724829Swnj 	so->so_options = options;
734786Swnj 
744786Swnj 	/*
754890Swnj 	 * Attach protocol to socket, initializing
764890Swnj 	 * and reserving resources.
774786Swnj 	 */
784786Swnj 	so->so_proto = prp;
794979Swnj 	error = (*prp->pr_usrreq)(so, PRU_ATTACH, 0, asa);
804979Swnj 	if (error) {
814971Swnj 		(void) m_free(dtom(so));
824890Swnj 		return (error);
834786Swnj 	}
844786Swnj 	*aso = so;
854786Swnj 	return (0);
864786Swnj }
874786Swnj 
884916Swnj sofree(so)
894916Swnj 	struct socket *so;
904916Swnj {
914916Swnj 
924927Swnj COUNT(SOFREE);
934950Swnj 	if (so->so_pcb || (so->so_state & SS_USERGONE) == 0)
944950Swnj 		return;
954950Swnj 	sbrelease(&so->so_snd);
964950Swnj 	sbrelease(&so->so_rcv);
974971Swnj 	(void) m_free(dtom(so));
984916Swnj }
994916Swnj 
1004786Swnj /*
1014890Swnj  * Close a socket on last file table reference removal.
1024890Swnj  * Initiate disconnect if connected.
1034890Swnj  * Free socket when disconnect complete.
1045580Sroot  *
1055580Sroot  * THIS IS REALLY A UNIX INTERFACE ROUTINE
1064829Swnj  */
1075580Sroot soclose(so, exiting)
1084829Swnj 	register struct socket *so;
1095580Sroot 	int exiting;
1104829Swnj {
1114890Swnj 	int s = splnet();		/* conservative */
1124829Swnj 
1134927Swnj COUNT(SOCLOSE);
1144890Swnj 	if (so->so_pcb == 0)
1154890Swnj 		goto discard;
1164890Swnj 	if (so->so_state & SS_ISCONNECTED) {
1174890Swnj 		if ((so->so_state & SS_ISDISCONNECTING) == 0) {
1184927Swnj 			u.u_error = sodisconnect(so, (struct sockaddr *)0);
1194890Swnj 			if (u.u_error) {
1205580Sroot 				if (exiting)
1215580Sroot 					goto drop;
1224890Swnj 				splx(s);
1234890Swnj 				return;
1244890Swnj 			}
1254890Swnj 		}
1265388Sroot 		if ((so->so_options & SO_DONTLINGER) == 0) {
1275281Sroot 			if ((so->so_state & SS_ISDISCONNECTING) &&
1285580Sroot 			    (so->so_options & SO_NONBLOCKING) &&
1295580Sroot 			    exiting == 0) {
1305281Sroot 				u.u_error = EINPROGRESS;
1315281Sroot 				splx(s);
1325281Sroot 				return;
1335281Sroot 			}
1345580Sroot 			/* should use tsleep here, for at most linger */
1355281Sroot 			while (so->so_state & SS_ISCONNECTED)
1365281Sroot 				sleep((caddr_t)&so->so_timeo, PZERO+1);
1374890Swnj 		}
1384890Swnj 	}
1395580Sroot drop:
1404890Swnj 	u.u_error = (*so->so_proto->pr_usrreq)(so, PRU_DETACH, 0, 0);
1414890Swnj discard:
1424950Swnj 	so->so_state |= SS_USERGONE;
1434950Swnj 	sofree(so);
1444890Swnj 	splx(s);
1454829Swnj }
1464829Swnj 
1474927Swnj sosplice(pso, so)
1484927Swnj 	struct socket *pso, *so;
1494927Swnj {
1504927Swnj 
1514927Swnj COUNT(SOSPLICE);
1525168Swnj 	if (pso->so_proto->pr_family != PF_UNIX) {
1534927Swnj 		struct socket *tso;
1544927Swnj 		tso = pso; pso = so; so = tso;
1554927Swnj 	}
1565168Swnj 	if (pso->so_proto->pr_family != PF_UNIX)
1574927Swnj 		return (EOPNOTSUPP);
1584927Swnj 	/* check types and buffer space */
1594927Swnj 	/* merge buffers */
1604927Swnj 	return (0);
1614927Swnj }
1624927Swnj 
1634916Swnj /*ARGSUSED*/
1644890Swnj sostat(so, sb)
1654829Swnj 	struct socket *so;
1664890Swnj 	struct stat *sb;
1674829Swnj {
1684829Swnj 
1694927Swnj COUNT(SOSTAT);
1705303Sroot 	bzero((caddr_t)sb, sizeof (*sb));		/* XXX */
1715303Sroot 	return (0);					/* XXX */
1724829Swnj }
1734829Swnj 
1744829Swnj /*
1754927Swnj  * Accept connection on a socket.
1764927Swnj  */
1774927Swnj soaccept(so, asa)
1784927Swnj 	struct socket *so;
1794927Swnj 	struct sockaddr *asa;
1804927Swnj {
1814927Swnj 	int s = splnet();
1824927Swnj 	int error;
1834927Swnj 
1844927Swnj COUNT(SOACCEPT);
1854927Swnj 	if ((so->so_options & SO_ACCEPTCONN) == 0) {
1864927Swnj 		error = EINVAL;			/* XXX */
1874927Swnj 		goto bad;
1884927Swnj 	}
1895265Swnj 	if ((so->so_state & SS_CONNAWAITING) == 0) {
1905265Swnj 		error = ENOTCONN;
1915265Swnj 		goto bad;
1925265Swnj 	}
1935265Swnj 	so->so_state &= ~SS_CONNAWAITING;
1944927Swnj 	error = (*so->so_proto->pr_usrreq)(so, PRU_ACCEPT, 0, (caddr_t)asa);
1954927Swnj bad:
1964927Swnj 	splx(s);
1974927Swnj 	return (error);
1984927Swnj }
1994927Swnj 
2004927Swnj /*
2014890Swnj  * Connect socket to a specified address.
2024890Swnj  * If already connected or connecting, then avoid
2034890Swnj  * the protocol entry, to keep its job simpler.
2044786Swnj  */
2054927Swnj soconnect(so, asa)
2064786Swnj 	struct socket *so;
2074927Swnj 	struct sockaddr *asa;
2084786Swnj {
2094890Swnj 	int s = splnet();
2104890Swnj 	int error;
2114786Swnj 
2124927Swnj COUNT(SOCONNECT);
2134890Swnj 	if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) {
2144890Swnj 		error = EISCONN;
2154890Swnj 		goto bad;
2164890Swnj 	}
2174927Swnj 	error = (*so->so_proto->pr_usrreq)(so, PRU_CONNECT, 0, (caddr_t)asa);
2184890Swnj bad:
2194890Swnj 	splx(s);
2204890Swnj 	return (error);
2214786Swnj }
2224786Swnj 
2234786Swnj /*
2244890Swnj  * Disconnect from a socket.
2254890Swnj  * Address parameter is from system call for later multicast
2264890Swnj  * protocols.  Check to make sure that connected and no disconnect
2274890Swnj  * in progress (for protocol's sake), and then invoke protocol.
2284786Swnj  */
2294927Swnj sodisconnect(so, asa)
2304786Swnj 	struct socket *so;
2314927Swnj 	struct sockaddr *asa;
2324786Swnj {
2334890Swnj 	int s = splnet();
2344890Swnj 	int error;
2354786Swnj 
2364927Swnj COUNT(SODISCONNECT);
2374890Swnj 	if ((so->so_state & SS_ISCONNECTED) == 0) {
2384890Swnj 		error = ENOTCONN;
2394890Swnj 		goto bad;
2404890Swnj 	}
2414890Swnj 	if (so->so_state & SS_ISDISCONNECTING) {
2424890Swnj 		error = EALREADY;
2434890Swnj 		goto bad;
2444890Swnj 	}
2454927Swnj 	error = (*so->so_proto->pr_usrreq)(so, PRU_DISCONNECT, 0, asa);
2464890Swnj bad:
2474890Swnj 	splx(s);
2484890Swnj 	return (error);
2494786Swnj }
2504786Swnj 
2514786Swnj /*
2524890Swnj  * Send on a socket.
2534890Swnj  * If send must go all at once and message is larger than
2544890Swnj  * send buffering, then hard error.
2554890Swnj  * Lock against other senders.
2564890Swnj  * If must go all at once and not enough room now, then
2574890Swnj  * inform user that this would block and do nothing.
2584786Swnj  */
2594927Swnj sosend(so, asa)
2604786Swnj 	register struct socket *so;
2614927Swnj 	struct sockaddr *asa;
2624786Swnj {
2634890Swnj 	struct mbuf *top = 0;
2644890Swnj 	register struct mbuf *m, **mp = ⊤
2654916Swnj 	register u_int len;
2664916Swnj 	int error = 0, space, s;
2674786Swnj 
2684927Swnj COUNT(SOSEND);
2694890Swnj 	if (so->so_state & SS_CANTSENDMORE)
2704890Swnj 		return (EPIPE);
2714890Swnj 	if (sosendallatonce(so) && u.u_count > so->so_snd.sb_hiwat)
2724890Swnj 		return (EMSGSIZE);
2735388Sroot 	if ((so->so_snd.sb_flags & SB_LOCK) && (so->so_options & SO_NONBLOCKING))
2744890Swnj 		return (EWOULDBLOCK);
2754890Swnj 	sblock(&so->so_snd);
2764890Swnj #define	snderr(errno)	{ error = errno; splx(s); goto release; }
2774890Swnj 
2784890Swnj 	s = splnet();
2794890Swnj again:
2805168Swnj 	if (so->so_error) {
2815168Swnj 		error = so->so_error;
2825168Swnj 		so->so_error = 0;
2835168Swnj 		splx(s);
2845168Swnj 		goto release;
2855168Swnj 	}
2864890Swnj 	if ((so->so_state & SS_ISCONNECTED) == 0) {
2874890Swnj 		if (so->so_proto->pr_flags & PR_CONNREQUIRED)
2884890Swnj 			snderr(ENOTCONN);
2894927Swnj 		if (asa == 0)
2904890Swnj 			snderr(EDESTADDRREQ);
2914890Swnj 	}
2924890Swnj 	if (top) {
2934927Swnj 		error = (*so->so_proto->pr_usrreq)(so, PRU_SEND, top, asa);
2944890Swnj 		if (error) {
2954890Swnj 			splx(s);
2964786Swnj 			goto release;
2974786Swnj 		}
2984890Swnj 		top = 0;
2994890Swnj 		mp = ⊤
3004786Swnj 	}
3014979Swnj 	if (u.u_count == 0) {
3024979Swnj 		splx(s);
3034979Swnj 		goto release;
3044979Swnj 	}
3055018Swnj 	space = sbspace(&so->so_snd);
306*5610Swnj 	if (space <= 0 || sosendallatonce(so) && space < u.u_count) {
3075388Sroot 		if (so->so_options & SO_NONBLOCKING)
3084890Swnj 			snderr(EWOULDBLOCK);
3094890Swnj 		sbunlock(&so->so_snd);
3104890Swnj 		sbwait(&so->so_snd);
3114890Swnj 		splx(s);
3124786Swnj 		goto again;
3134786Swnj 	}
3144890Swnj 	splx(s);
3155018Swnj 	while (u.u_count && space > 0) {
3164890Swnj 		MGET(m, 1);
3174890Swnj 		if (m == NULL) {
3184890Swnj 			error = ENOBUFS;
3194890Swnj 			m_freem(top);
3204890Swnj 			goto release;
3214786Swnj 		}
3225095Swnj 		if (u.u_count >= CLBYTES && space >= CLBYTES) {
3234890Swnj 			register struct mbuf *p;
3245095Swnj 			MCLGET(p, 1);
3254890Swnj 			if (p == 0)
3264890Swnj 				goto nopages;
3274890Swnj 			m->m_off = (int)p - (int)m;
3285095Swnj 			len = CLBYTES;
3294890Swnj 		} else {
3304786Swnj nopages:
3314890Swnj 			m->m_off = MMINOFF;
3324890Swnj 			len = MIN(MLEN, u.u_count);
3334786Swnj 		}
3344890Swnj 		iomove(mtod(m, caddr_t), len, B_WRITE);
3354890Swnj 		m->m_len = len;
3364890Swnj 		*mp = m;
3374890Swnj 		mp = &m->m_next;
3385018Swnj 		space = sbspace(&so->so_snd);
3394786Swnj 	}
3404890Swnj 	s = splnet();
3414890Swnj 	goto again;
3424890Swnj 
3434786Swnj release:
3444890Swnj 	sbunlock(&so->so_snd);
3454786Swnj 	return (error);
3464786Swnj }
3474786Swnj 
3484927Swnj soreceive(so, asa)
3494786Swnj 	register struct socket *so;
3504927Swnj 	struct sockaddr *asa;
3514786Swnj {
3524786Swnj 	register struct mbuf *m, *n;
3534916Swnj 	u_int len;
3545423Swnj 	int eor, s, error = 0, cnt = u.u_count;
3555423Swnj 	caddr_t base = u.u_base;
3564786Swnj 
3574927Swnj COUNT(SORECEIVE);
3584890Swnj restart:
3594890Swnj 	sblock(&so->so_rcv);
3604890Swnj 	s = splnet();
3614890Swnj 
3624890Swnj #define	rcverr(errno)	{ error = errno; splx(s); goto release; }
3634786Swnj 	if (so->so_rcv.sb_cc == 0) {
3645168Swnj 		if (so->so_error) {
3655168Swnj 			error = so->so_error;
3665168Swnj 			so->so_error = 0;
3675168Swnj 			splx(s);
3685168Swnj 			goto release;
3695168Swnj 		}
3704890Swnj 		if (so->so_state & SS_CANTRCVMORE) {
3714890Swnj 			splx(s);
3724890Swnj 			goto release;
3734890Swnj 		}
3745015Sroot 		if ((so->so_state & SS_ISCONNECTED) == 0 &&
3755015Sroot 		    (so->so_proto->pr_flags & PR_CONNREQUIRED))
3765015Sroot 			rcverr(ENOTCONN);
3775388Sroot 		if (so->so_options & SO_NONBLOCKING)
3785168Swnj 			rcverr(EWOULDBLOCK);
3794890Swnj 		sbunlock(&so->so_rcv);
3804971Swnj 		sbwait(&so->so_rcv);
3815012Swnj 		splx(s);
3824890Swnj 		goto restart;
3834786Swnj 	}
3844829Swnj 	m = so->so_rcv.sb_mb;
3854786Swnj 	if (m == 0)
3864786Swnj 		panic("receive");
3875039Swnj 	if (so->so_proto->pr_flags & PR_ADDR) {
3885039Swnj 		if (m->m_len != sizeof (struct sockaddr))
3895039Swnj 			panic("soreceive addr");
3905039Swnj 		if (asa)
3915039Swnj 			bcopy(mtod(m, caddr_t), (caddr_t)asa, sizeof (*asa));
3925039Swnj 		so->so_rcv.sb_cc -= m->m_len;
3935039Swnj 		so->so_rcv.sb_mbcnt -= MSIZE;
3945018Swnj 		m = m_free(m);
3954890Swnj 		if (m == 0)
3964890Swnj 			panic("receive 2");
3975018Swnj 		so->so_rcv.sb_mb = m;
3984890Swnj 	}
3995423Swnj 	so->so_state &= ~SS_RCVATMARK;
4005423Swnj 	if (so->so_oobmark && cnt > so->so_oobmark)
4015423Swnj 		cnt = so->so_oobmark;
4024786Swnj 	eor = 0;
4034786Swnj 	do {
4045423Swnj 		len = MIN(m->m_len, cnt);
4054786Swnj 		if (len == m->m_len) {
4064786Swnj 			eor = (int)m->m_act;
4074890Swnj 			sbfree(&so->so_rcv, m);
4085358Sroot 			so->so_rcv.sb_mb = m->m_next;
4094786Swnj 		}
4104786Swnj 		splx(s);
4114786Swnj 		iomove(mtod(m, caddr_t), len, B_READ);
4125423Swnj 		cnt -= len;
4134786Swnj 		s = splnet();
4144786Swnj 		if (len == m->m_len) {
4154786Swnj 			MFREE(m, n);
4164786Swnj 		} else {
4174786Swnj 			m->m_off += len;
4184786Swnj 			m->m_len -= len;
4194829Swnj 			so->so_rcv.sb_cc -= len;
4204786Swnj 		}
4215423Swnj 	} while ((m = so->so_rcv.sb_mb) && cnt && !eor);
4224786Swnj 	if ((so->so_proto->pr_flags & PR_ATOMIC) && eor == 0)
4234786Swnj 		do {
4244786Swnj 			if (m == 0)
4254890Swnj 				panic("receive 3");
4264890Swnj 			sbfree(&so->so_rcv, m);
4274786Swnj 			eor = (int)m->m_act;
4284786Swnj 			so->so_rcv.sb_mb = m->m_next;
4294786Swnj 			MFREE(m, n);
4304890Swnj 			m = n;
4314786Swnj 		} while (eor == 0);
4324890Swnj 	if ((so->so_proto->pr_flags & PR_WANTRCVD) && so->so_pcb)
4334890Swnj 		(*so->so_proto->pr_usrreq)(so, PRU_RCVD, 0, 0);
4345423Swnj 	if (so->so_oobmark) {
4355423Swnj 		so->so_oobmark -= u.u_base - base;
4365423Swnj 		if (so->so_oobmark == 0)
4375423Swnj 			so->so_state |= SS_RCVATMARK;
4385423Swnj 	}
4394890Swnj release:
4404916Swnj 	sbunlock(&so->so_rcv);
4414890Swnj 	splx(s);
4424916Swnj 	return (error);
4434786Swnj }
4444786Swnj 
4455423Swnj sohasoutofband(so)
4465423Swnj 	struct socket *so;
4475423Swnj {
4485423Swnj 
4495423Swnj 	if (so->so_pgrp == 0)
4505423Swnj 		return;
4515423Swnj 	if (so->so_pgrp > 0)
4525423Swnj 		gsignal(so->so_pgrp, SIGURG);
4535429Swnj 	else {
4545429Swnj 		struct proc *p = pfind(-so->so_pgrp);
4555429Swnj 
4565429Swnj 		if (p)
4575429Swnj 			psignal(p, SIGURG);
4585429Swnj 	}
4595423Swnj }
4605423Swnj 
4614916Swnj /*ARGSUSED*/
4624916Swnj soioctl(so, cmd, cmdp)
4634829Swnj 	register struct socket *so;
4644829Swnj 	int cmd;
4654829Swnj 	register caddr_t cmdp;
4664786Swnj {
4674786Swnj 
4684927Swnj COUNT(SOIOCTL);
4695358Sroot 	switch (cmd) {
4704829Swnj 
4715388Sroot 	case FIONBIO: {
4725388Sroot 		int nbio;
4735388Sroot 		if (copyin(cmdp, (caddr_t)&nbio, sizeof (nbio))) {
4745388Sroot 			u.u_error = EFAULT;
4755388Sroot 			return;
4765388Sroot 		}
4775388Sroot 		if (nbio)
4785388Sroot 			so->so_options |= SO_NONBLOCKING;
4795388Sroot 		else
4805388Sroot 			so->so_options &= ~SO_NONBLOCKING;
4815388Sroot 		return;
4825388Sroot 	}
4835388Sroot 
4845388Sroot 	case FIOASYNC: {
4855388Sroot 		int async;
4865388Sroot 		if (copyin(cmdp, (caddr_t)&async, sizeof (async))) {
4875388Sroot 			u.u_error = EFAULT;
4885388Sroot 			return;
4895388Sroot 		}
4905388Sroot 		if (async)
4915388Sroot 			;
4925388Sroot 		else
4935388Sroot 			;
4945388Sroot 		return;
4955388Sroot 	}
4965388Sroot 
4975388Sroot 	case SIOCSKEEP: {
4985388Sroot 		int keep;
4995388Sroot 		if (copyin(cmdp, (caddr_t)&keep, sizeof (keep))) {
5005388Sroot 			u.u_error = EFAULT;
5015388Sroot 			return;
5025388Sroot 		}
5035388Sroot 		if (keep)
5045388Sroot 			so->so_options &= ~SO_NOKEEPALIVE;
5055388Sroot 		else
5065388Sroot 			so->so_options |= SO_NOKEEPALIVE;
5075388Sroot 		return;
5085388Sroot 	}
5095388Sroot 
5105388Sroot 	case SIOCGKEEP: {
5115388Sroot 		int keep = (so->so_options & SO_NOKEEPALIVE) == 0;
5125388Sroot 		if (copyout((caddr_t)&keep, cmdp, sizeof (keep)))
5135388Sroot 			u.u_error = EFAULT;
5145388Sroot 		return;
5155388Sroot 	}
5165388Sroot 
5175388Sroot 	case SIOCSLINGER: {
5185388Sroot 		int linger;
5195388Sroot 		if (copyin(cmdp, (caddr_t)&linger, sizeof (linger))) {
5205388Sroot 			u.u_error = EFAULT;
5215388Sroot 			return;
5225388Sroot 		}
5235388Sroot 		so->so_linger = linger;
5245388Sroot 		if (so->so_linger)
5255388Sroot 			so->so_options &= ~SO_DONTLINGER;
5265388Sroot 		else
5275388Sroot 			so->so_options |= SO_DONTLINGER;
5285388Sroot 		return;
5295388Sroot 	}
5305388Sroot 
5315388Sroot 	case SIOCGLINGER: {
5325388Sroot 		int linger = so->so_linger;
5335388Sroot 		if (copyout((caddr_t)&linger, cmdp, sizeof (linger))) {
5345388Sroot 			u.u_error = EFAULT;
5355388Sroot 			return;
5365388Sroot 		}
5375388Sroot 	}
5385423Swnj 	case SIOCSPGRP: {
5395423Swnj 		int pgrp;
5405423Swnj 		if (copyin(cmdp, (caddr_t)&pgrp, sizeof (pgrp))) {
5415423Swnj 			u.u_error = EFAULT;
5425423Swnj 			return;
5435423Swnj 		}
5445423Swnj 		so->so_pgrp = pgrp;
5455423Swnj 		return;
5465423Swnj 	}
5475388Sroot 
5485423Swnj 	case SIOCGPGRP: {
5495423Swnj 		int pgrp = so->so_pgrp;
5505423Swnj 		if (copyout((caddr_t)&pgrp, cmdp, sizeof (pgrp))) {
5515423Swnj 			u.u_error = EFAULT;
5525423Swnj 			return;
5535423Swnj 		}
5545423Swnj 	}
5555423Swnj 
5565281Sroot 	case SIOCDONE: {
5575281Sroot 		int flags;
5585281Sroot 		if (copyin(cmdp, (caddr_t)&flags, sizeof (flags))) {
5595281Sroot 			u.u_error = EFAULT;
5605281Sroot 			return;
5615281Sroot 		}
5625388Sroot 		flags++;
5635281Sroot 		if (flags & FREAD) {
5645281Sroot 			int s = splimp();
5655281Sroot 			socantrcvmore(so);
5665281Sroot 			sbflush(&so->so_rcv);
5675281Sroot 		}
5685281Sroot 		if (flags & FWRITE)
5695404Swnj 			u.u_error = (*so->so_proto->pr_usrreq)(so, PRU_SHUTDOWN, (struct mbuf *)0, 0);
5705281Sroot 		return;
5714829Swnj 	}
5725281Sroot 
5735423Swnj 	case SIOCSENDOOB: {
5745423Swnj 		char oob;
5755423Swnj 		struct mbuf *m;
5765423Swnj 		if (copyin(cmdp, (caddr_t)&oob, sizeof (oob))) {
5775423Swnj 			u.u_error = EFAULT;
5785423Swnj 			return;
5795423Swnj 		}
5805423Swnj 		m = m_get(M_DONTWAIT);
5815423Swnj 		if (m == 0) {
5825423Swnj 			u.u_error = ENOBUFS;
5835423Swnj 			return;
5845423Swnj 		}
5855423Swnj 		m->m_off = MMINOFF;
5865423Swnj 		m->m_len = 1;
5875423Swnj 		*mtod(m, caddr_t) = oob;
5885423Swnj 		(*so->so_proto->pr_usrreq)(so, PRU_SENDOOB, m, 0);
5895423Swnj 		return;
5905281Sroot 	}
5915423Swnj 
5925423Swnj 	case SIOCRCVOOB: {
5935423Swnj 		struct mbuf *m = m_get(M_DONTWAIT);
5945423Swnj 		if (m == 0) {
5955423Swnj 			u.u_error = ENOBUFS;
5965423Swnj 			return;
5975423Swnj 		}
5985423Swnj 		m->m_off = MMINOFF; *mtod(m, caddr_t) = 0;
5995423Swnj 		(*so->so_proto->pr_usrreq)(so, PRU_RCVOOB, m, 0);
6005423Swnj 		if (copyout(mtod(m, caddr_t), cmdp, sizeof (char))) {
6015423Swnj 			u.u_error = EFAULT;
6025423Swnj 			return;
6035423Swnj 		}
6045423Swnj 		m_free(m);
6055423Swnj 		return;
6065423Swnj 	}
6075423Swnj 
6085423Swnj 	case SIOCATMARK: {
6095423Swnj 		int atmark = (so->so_state&SS_RCVATMARK) != 0;
6105423Swnj 		if (copyout((caddr_t)&atmark, cmdp, sizeof (atmark))) {
6115423Swnj 			u.u_error = EFAULT;
6125423Swnj 			return;
6135423Swnj 		}
6145423Swnj 		return;
6155423Swnj 	}
6165445Swnj 	/* type/protocol specific ioctls */
6175423Swnj 	}
6185445Swnj 	u.u_error = EOPNOTSUPP;
6194786Swnj }
620