xref: /csrg-svn/sys/kern/uipc_socket.c (revision 7180)
1*7180Swnj /*	uipc_socket.c	4.41	82/06/14	*/
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"
196355Ssam #include "../net/route.h"
204786Swnj 
214786Swnj /*
224890Swnj  * Socket support routines.
234890Swnj  *
244890Swnj  * DEAL WITH INTERRUPT NOTIFICATION.
254786Swnj  */
264786Swnj 
274786Swnj /*
284786Swnj  * Create a socket.
294786Swnj  */
304927Swnj socreate(aso, type, asp, asa, options)
314786Swnj 	struct socket **aso;
324786Swnj 	int type;
334927Swnj 	struct sockproto *asp;
344927Swnj 	struct sockaddr *asa;
354829Swnj 	int options;
364786Swnj {
374786Swnj 	register struct protosw *prp;
384786Swnj 	register struct socket *so;
394786Swnj 	struct mbuf *m;
404890Swnj 	int pf, proto, error;
414927Swnj COUNT(SOCREATE);
424786Swnj 
434786Swnj 	/*
444890Swnj 	 * Use process standard protocol/protocol family if none
454890Swnj 	 * specified by address argument.
464786Swnj 	 */
474927Swnj 	if (asp == 0) {
484890Swnj 		pf = PF_INET;		/* should be u.u_protof */
494786Swnj 		proto = 0;
504786Swnj 	} else {
514927Swnj 		pf = asp->sp_family;
524927Swnj 		proto = asp->sp_protocol;
534786Swnj 	}
544786Swnj 
554786Swnj 	/*
564890Swnj 	 * If protocol specified, look for it, otherwise
574890Swnj 	 * for a protocol of the correct type in the right family.
584890Swnj 	 */
594890Swnj 	if (proto)
604890Swnj 		prp = pffindproto(pf, proto);
614890Swnj 	else
624890Swnj 		prp = pffindtype(pf, type);
634890Swnj 	if (prp == 0)
644890Swnj 		return (EPROTONOSUPPORT);
654890Swnj 
664890Swnj 	/*
674786Swnj 	 * Get a socket structure.
684786Swnj 	 */
694890Swnj 	m = m_getclr(M_WAIT);
704786Swnj 	if (m == 0)
714786Swnj 		return (ENOBUFS);
724786Swnj 	so = mtod(m, struct socket *);
734829Swnj 	so->so_options = options;
746214Swnj 	so->so_state = 0;
756214Swnj 	if (u.u_uid == 0)
766214Swnj 		so->so_state = SS_PRIV;
774786Swnj 
784786Swnj 	/*
794890Swnj 	 * Attach protocol to socket, initializing
804890Swnj 	 * and reserving resources.
814786Swnj 	 */
824786Swnj 	so->so_proto = prp;
834979Swnj 	error = (*prp->pr_usrreq)(so, PRU_ATTACH, 0, asa);
844979Swnj 	if (error) {
85*7180Swnj 		if (so->so_snd.sb_mbmax || so->so_rcv.sb_mbmax)
86*7180Swnj 			panic("socreate");
87*7180Swnj 		so->so_state |= SS_USERGONE;
88*7180Swnj 		sofree(so);
894890Swnj 		return (error);
904786Swnj 	}
914786Swnj 	*aso = so;
924786Swnj 	return (0);
934786Swnj }
944786Swnj 
954916Swnj sofree(so)
964916Swnj 	struct socket *so;
974916Swnj {
984916Swnj 
994927Swnj COUNT(SOFREE);
1004950Swnj 	if (so->so_pcb || (so->so_state & SS_USERGONE) == 0)
1014950Swnj 		return;
1024950Swnj 	sbrelease(&so->so_snd);
1034950Swnj 	sbrelease(&so->so_rcv);
1044971Swnj 	(void) m_free(dtom(so));
1054916Swnj }
1064916Swnj 
1074786Swnj /*
1084890Swnj  * Close a socket on last file table reference removal.
1094890Swnj  * Initiate disconnect if connected.
1104890Swnj  * Free socket when disconnect complete.
1115580Sroot  *
1125580Sroot  * THIS IS REALLY A UNIX INTERFACE ROUTINE
1134829Swnj  */
1145580Sroot soclose(so, exiting)
1154829Swnj 	register struct socket *so;
1165580Sroot 	int exiting;
1174829Swnj {
1184890Swnj 	int s = splnet();		/* conservative */
1194829Swnj 
1204927Swnj COUNT(SOCLOSE);
1214890Swnj 	if (so->so_pcb == 0)
1224890Swnj 		goto discard;
1236259Sroot 	if (exiting)
1246259Sroot 		so->so_options |= SO_KEEPALIVE;
1254890Swnj 	if (so->so_state & SS_ISCONNECTED) {
1264890Swnj 		if ((so->so_state & SS_ISDISCONNECTING) == 0) {
1274927Swnj 			u.u_error = sodisconnect(so, (struct sockaddr *)0);
1284890Swnj 			if (u.u_error) {
1295580Sroot 				if (exiting)
1305580Sroot 					goto drop;
1314890Swnj 				splx(s);
1324890Swnj 				return;
1334890Swnj 			}
1344890Swnj 		}
1355388Sroot 		if ((so->so_options & SO_DONTLINGER) == 0) {
1365281Sroot 			if ((so->so_state & SS_ISDISCONNECTING) &&
1376214Swnj 			    (so->so_state & SS_NBIO) &&
1385580Sroot 			    exiting == 0) {
1395281Sroot 				u.u_error = EINPROGRESS;
1405281Sroot 				splx(s);
1415281Sroot 				return;
1425281Sroot 			}
1435580Sroot 			/* should use tsleep here, for at most linger */
1445281Sroot 			while (so->so_state & SS_ISCONNECTED)
1455281Sroot 				sleep((caddr_t)&so->so_timeo, PZERO+1);
1464890Swnj 		}
1474890Swnj 	}
1485580Sroot drop:
1496880Ssam 	if (so->so_pcb) {
1506880Ssam 		u.u_error = (*so->so_proto->pr_usrreq)(so, PRU_DETACH, 0, 0);
1516880Ssam 		if (exiting == 0 && u.u_error) {
1526880Ssam 			splx(s);
1536880Ssam 			return;
1546880Ssam 		}
1556880Ssam 	}
1564890Swnj discard:
1574950Swnj 	so->so_state |= SS_USERGONE;
1584950Swnj 	sofree(so);
1594890Swnj 	splx(s);
1604829Swnj }
1614829Swnj 
1624916Swnj /*ARGSUSED*/
1634890Swnj sostat(so, sb)
1644829Swnj 	struct socket *so;
1654890Swnj 	struct stat *sb;
1664829Swnj {
1674829Swnj 
1684927Swnj COUNT(SOSTAT);
1695303Sroot 	bzero((caddr_t)sb, sizeof (*sb));		/* XXX */
1705303Sroot 	return (0);					/* XXX */
1714829Swnj }
1724829Swnj 
1734829Swnj /*
1744927Swnj  * Accept connection on a socket.
1754927Swnj  */
1764927Swnj soaccept(so, asa)
1774927Swnj 	struct socket *so;
1784927Swnj 	struct sockaddr *asa;
1794927Swnj {
1804927Swnj 	int s = splnet();
1814927Swnj 	int error;
1824927Swnj 
1834927Swnj COUNT(SOACCEPT);
1844927Swnj 	if ((so->so_options & SO_ACCEPTCONN) == 0) {
1854927Swnj 		error = EINVAL;			/* XXX */
1864927Swnj 		goto bad;
1874927Swnj 	}
1885265Swnj 	if ((so->so_state & SS_CONNAWAITING) == 0) {
1895265Swnj 		error = ENOTCONN;
1905265Swnj 		goto bad;
1915265Swnj 	}
1925265Swnj 	so->so_state &= ~SS_CONNAWAITING;
1934927Swnj 	error = (*so->so_proto->pr_usrreq)(so, PRU_ACCEPT, 0, (caddr_t)asa);
1944927Swnj bad:
1954927Swnj 	splx(s);
1964927Swnj 	return (error);
1974927Swnj }
1984927Swnj 
1994927Swnj /*
2004890Swnj  * Connect socket to a specified address.
2014890Swnj  * If already connected or connecting, then avoid
2024890Swnj  * the protocol entry, to keep its job simpler.
2034786Swnj  */
2044927Swnj soconnect(so, asa)
2054786Swnj 	struct socket *so;
2064927Swnj 	struct sockaddr *asa;
2074786Swnj {
2084890Swnj 	int s = splnet();
2094890Swnj 	int error;
2104786Swnj 
2114927Swnj COUNT(SOCONNECT);
2124890Swnj 	if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) {
2134890Swnj 		error = EISCONN;
2144890Swnj 		goto bad;
2154890Swnj 	}
2164927Swnj 	error = (*so->so_proto->pr_usrreq)(so, PRU_CONNECT, 0, (caddr_t)asa);
2174890Swnj bad:
2184890Swnj 	splx(s);
2194890Swnj 	return (error);
2204786Swnj }
2214786Swnj 
2224786Swnj /*
2234890Swnj  * Disconnect from a socket.
2244890Swnj  * Address parameter is from system call for later multicast
2254890Swnj  * protocols.  Check to make sure that connected and no disconnect
2264890Swnj  * in progress (for protocol's sake), and then invoke protocol.
2274786Swnj  */
2284927Swnj sodisconnect(so, asa)
2294786Swnj 	struct socket *so;
2304927Swnj 	struct sockaddr *asa;
2314786Swnj {
2324890Swnj 	int s = splnet();
2334890Swnj 	int error;
2344786Swnj 
2354927Swnj COUNT(SODISCONNECT);
2364890Swnj 	if ((so->so_state & SS_ISCONNECTED) == 0) {
2374890Swnj 		error = ENOTCONN;
2384890Swnj 		goto bad;
2394890Swnj 	}
2404890Swnj 	if (so->so_state & SS_ISDISCONNECTING) {
2414890Swnj 		error = EALREADY;
2424890Swnj 		goto bad;
2434890Swnj 	}
2444927Swnj 	error = (*so->so_proto->pr_usrreq)(so, PRU_DISCONNECT, 0, asa);
2454890Swnj bad:
2464890Swnj 	splx(s);
2474890Swnj 	return (error);
2484786Swnj }
2494786Swnj 
2504786Swnj /*
2514890Swnj  * Send on a socket.
2524890Swnj  * If send must go all at once and message is larger than
2534890Swnj  * send buffering, then hard error.
2544890Swnj  * Lock against other senders.
2554890Swnj  * If must go all at once and not enough room now, then
2564890Swnj  * inform user that this would block and do nothing.
2574786Swnj  */
2584927Swnj sosend(so, asa)
2594786Swnj 	register struct socket *so;
2604927Swnj 	struct sockaddr *asa;
2614786Swnj {
2624890Swnj 	struct mbuf *top = 0;
2634890Swnj 	register struct mbuf *m, **mp = ⊤
2644916Swnj 	register u_int len;
2654916Swnj 	int error = 0, space, s;
2664786Swnj 
2674927Swnj COUNT(SOSEND);
2684890Swnj 	if (sosendallatonce(so) && u.u_count > so->so_snd.sb_hiwat)
2694890Swnj 		return (EMSGSIZE);
2706419Sroot #ifdef notdef
2716419Sroot 	/* NEED TO PREVENT BUSY WAITING IN SELECT FOR WRITING */
2726214Swnj 	if ((so->so_snd.sb_flags & SB_LOCK) && (so->so_state & SS_NBIO))
2734890Swnj 		return (EWOULDBLOCK);
2746419Sroot #endif
2756419Sroot restart:
2764890Swnj 	sblock(&so->so_snd);
2774890Swnj #define	snderr(errno)	{ error = errno; splx(s); goto release; }
2784890Swnj 
2796419Sroot again:
2804890Swnj 	s = splnet();
2816419Sroot 	if (so->so_state & SS_CANTSENDMORE) {
2826419Sroot 		psignal(u.u_procp, SIGPIPE);
2836419Sroot 		snderr(EPIPE);
2846419Sroot 	}
2855168Swnj 	if (so->so_error) {
2865168Swnj 		error = so->so_error;
2876419Sroot 		so->so_error = 0;				/* ??? */
2885168Swnj 		splx(s);
2895168Swnj 		goto release;
2905168Swnj 	}
2914890Swnj 	if ((so->so_state & SS_ISCONNECTED) == 0) {
2924890Swnj 		if (so->so_proto->pr_flags & PR_CONNREQUIRED)
2934890Swnj 			snderr(ENOTCONN);
2944927Swnj 		if (asa == 0)
2954890Swnj 			snderr(EDESTADDRREQ);
2964890Swnj 	}
2974890Swnj 	if (top) {
2984927Swnj 		error = (*so->so_proto->pr_usrreq)(so, PRU_SEND, top, asa);
2996419Sroot 		top = 0;
3004890Swnj 		if (error) {
3014890Swnj 			splx(s);
3024786Swnj 			goto release;
3034786Swnj 		}
3044890Swnj 		mp = ⊤
3054786Swnj 	}
3064979Swnj 	if (u.u_count == 0) {
3074979Swnj 		splx(s);
3084979Swnj 		goto release;
3094979Swnj 	}
3105018Swnj 	space = sbspace(&so->so_snd);
3115610Swnj 	if (space <= 0 || sosendallatonce(so) && space < u.u_count) {
3126214Swnj 		if (so->so_state & SS_NBIO)
3134890Swnj 			snderr(EWOULDBLOCK);
3144890Swnj 		sbunlock(&so->so_snd);
3154890Swnj 		sbwait(&so->so_snd);
3164890Swnj 		splx(s);
3176419Sroot 		goto restart;
3184786Swnj 	}
3194890Swnj 	splx(s);
3205018Swnj 	while (u.u_count && space > 0) {
3214890Swnj 		MGET(m, 1);
3224890Swnj 		if (m == NULL) {
3236419Sroot 			error = ENOBUFS;			/* SIGPIPE? */
3244890Swnj 			goto release;
3254786Swnj 		}
3265095Swnj 		if (u.u_count >= CLBYTES && space >= CLBYTES) {
3274890Swnj 			register struct mbuf *p;
3285095Swnj 			MCLGET(p, 1);
3294890Swnj 			if (p == 0)
3304890Swnj 				goto nopages;
3314890Swnj 			m->m_off = (int)p - (int)m;
3325095Swnj 			len = CLBYTES;
3334890Swnj 		} else {
3344786Swnj nopages:
3354890Swnj 			m->m_off = MMINOFF;
3364890Swnj 			len = MIN(MLEN, u.u_count);
3374786Swnj 		}
3384890Swnj 		iomove(mtod(m, caddr_t), len, B_WRITE);
3394890Swnj 		m->m_len = len;
3404890Swnj 		*mp = m;
3414890Swnj 		mp = &m->m_next;
3425018Swnj 		space = sbspace(&so->so_snd);
3434786Swnj 	}
3444890Swnj 	goto again;
3454890Swnj 
3464786Swnj release:
3474890Swnj 	sbunlock(&so->so_snd);
3486419Sroot 	if (top)
3496419Sroot 		m_freem(top);
3504786Swnj 	return (error);
3514786Swnj }
3524786Swnj 
3534927Swnj soreceive(so, asa)
3544786Swnj 	register struct socket *so;
3554927Swnj 	struct sockaddr *asa;
3564786Swnj {
3574786Swnj 	register struct mbuf *m, *n;
3584916Swnj 	u_int len;
3595423Swnj 	int eor, s, error = 0, cnt = u.u_count;
3605423Swnj 	caddr_t base = u.u_base;
3614786Swnj 
3624927Swnj COUNT(SORECEIVE);
3634890Swnj restart:
3644890Swnj 	sblock(&so->so_rcv);
3654890Swnj 	s = splnet();
3664890Swnj 
3674890Swnj #define	rcverr(errno)	{ error = errno; splx(s); goto release; }
3684786Swnj 	if (so->so_rcv.sb_cc == 0) {
3695168Swnj 		if (so->so_error) {
3705168Swnj 			error = so->so_error;
3715168Swnj 			so->so_error = 0;
3725168Swnj 			splx(s);
3735168Swnj 			goto release;
3745168Swnj 		}
3754890Swnj 		if (so->so_state & SS_CANTRCVMORE) {
3764890Swnj 			splx(s);
3774890Swnj 			goto release;
3784890Swnj 		}
3795015Sroot 		if ((so->so_state & SS_ISCONNECTED) == 0 &&
3805015Sroot 		    (so->so_proto->pr_flags & PR_CONNREQUIRED))
3815015Sroot 			rcverr(ENOTCONN);
3826214Swnj 		if (so->so_state & SS_NBIO)
3835168Swnj 			rcverr(EWOULDBLOCK);
3844890Swnj 		sbunlock(&so->so_rcv);
3854971Swnj 		sbwait(&so->so_rcv);
3865012Swnj 		splx(s);
3874890Swnj 		goto restart;
3884786Swnj 	}
3894829Swnj 	m = so->so_rcv.sb_mb;
3904786Swnj 	if (m == 0)
3914786Swnj 		panic("receive");
3925039Swnj 	if (so->so_proto->pr_flags & PR_ADDR) {
3935039Swnj 		if (m->m_len != sizeof (struct sockaddr))
3945039Swnj 			panic("soreceive addr");
3955039Swnj 		if (asa)
3965039Swnj 			bcopy(mtod(m, caddr_t), (caddr_t)asa, sizeof (*asa));
3975039Swnj 		so->so_rcv.sb_cc -= m->m_len;
3985039Swnj 		so->so_rcv.sb_mbcnt -= MSIZE;
3995018Swnj 		m = m_free(m);
4004890Swnj 		if (m == 0)
4014890Swnj 			panic("receive 2");
4025018Swnj 		so->so_rcv.sb_mb = m;
4034890Swnj 	}
4045423Swnj 	so->so_state &= ~SS_RCVATMARK;
4055423Swnj 	if (so->so_oobmark && cnt > so->so_oobmark)
4065423Swnj 		cnt = so->so_oobmark;
4074786Swnj 	eor = 0;
4084786Swnj 	do {
4095423Swnj 		len = MIN(m->m_len, cnt);
4104786Swnj 		splx(s);
4114786Swnj 		iomove(mtod(m, caddr_t), len, B_READ);
4125423Swnj 		cnt -= len;
4134786Swnj 		s = splnet();
4144786Swnj 		if (len == m->m_len) {
4156091Sroot 			eor = (int)m->m_act;
4166091Sroot 			sbfree(&so->so_rcv, m);
4176091Sroot 			so->so_rcv.sb_mb = m->m_next;
4184786Swnj 			MFREE(m, n);
4194786Swnj 		} else {
4204786Swnj 			m->m_off += len;
4214786Swnj 			m->m_len -= len;
4224829Swnj 			so->so_rcv.sb_cc -= len;
4234786Swnj 		}
4245423Swnj 	} while ((m = so->so_rcv.sb_mb) && cnt && !eor);
4254786Swnj 	if ((so->so_proto->pr_flags & PR_ATOMIC) && eor == 0)
4264786Swnj 		do {
4274786Swnj 			if (m == 0)
4284890Swnj 				panic("receive 3");
4294890Swnj 			sbfree(&so->so_rcv, m);
4304786Swnj 			eor = (int)m->m_act;
4314786Swnj 			so->so_rcv.sb_mb = m->m_next;
4324786Swnj 			MFREE(m, n);
4334890Swnj 			m = n;
4344786Swnj 		} while (eor == 0);
4354890Swnj 	if ((so->so_proto->pr_flags & PR_WANTRCVD) && so->so_pcb)
4364890Swnj 		(*so->so_proto->pr_usrreq)(so, PRU_RCVD, 0, 0);
4375423Swnj 	if (so->so_oobmark) {
4385423Swnj 		so->so_oobmark -= u.u_base - base;
4395423Swnj 		if (so->so_oobmark == 0)
4405423Swnj 			so->so_state |= SS_RCVATMARK;
4415423Swnj 	}
4424890Swnj release:
4434916Swnj 	sbunlock(&so->so_rcv);
4444890Swnj 	splx(s);
4454916Swnj 	return (error);
4464786Swnj }
4474786Swnj 
4485423Swnj sohasoutofband(so)
4495423Swnj 	struct socket *so;
4505423Swnj {
4515423Swnj 
4525423Swnj 	if (so->so_pgrp == 0)
4535423Swnj 		return;
4545423Swnj 	if (so->so_pgrp > 0)
4555423Swnj 		gsignal(so->so_pgrp, SIGURG);
4565429Swnj 	else {
4575429Swnj 		struct proc *p = pfind(-so->so_pgrp);
4585429Swnj 
4595429Swnj 		if (p)
4605429Swnj 			psignal(p, SIGURG);
4615429Swnj 	}
4625423Swnj }
4635423Swnj 
4644916Swnj /*ARGSUSED*/
4654916Swnj soioctl(so, cmd, cmdp)
4664829Swnj 	register struct socket *so;
4674829Swnj 	int cmd;
4684829Swnj 	register caddr_t cmdp;
4694786Swnj {
4704786Swnj 
4714927Swnj COUNT(SOIOCTL);
4725358Sroot 	switch (cmd) {
4734829Swnj 
4745388Sroot 	case FIONBIO: {
4755388Sroot 		int nbio;
4765388Sroot 		if (copyin(cmdp, (caddr_t)&nbio, sizeof (nbio))) {
4775388Sroot 			u.u_error = EFAULT;
4785388Sroot 			return;
4795388Sroot 		}
4805388Sroot 		if (nbio)
4816214Swnj 			so->so_state |= SS_NBIO;
4825388Sroot 		else
4836214Swnj 			so->so_state &= ~SS_NBIO;
4845388Sroot 		return;
4855388Sroot 	}
4865388Sroot 
4875388Sroot 	case FIOASYNC: {
4885388Sroot 		int async;
4895388Sroot 		if (copyin(cmdp, (caddr_t)&async, sizeof (async))) {
4905388Sroot 			u.u_error = EFAULT;
4915388Sroot 			return;
4925388Sroot 		}
4935388Sroot 		if (async)
4946214Swnj 			so->so_state |= SS_ASYNC;
4955388Sroot 		else
4966214Swnj 			so->so_state &= ~SS_ASYNC;
4975388Sroot 		return;
4985388Sroot 	}
4995388Sroot 
5005388Sroot 	case SIOCSKEEP: {
5015388Sroot 		int keep;
5025388Sroot 		if (copyin(cmdp, (caddr_t)&keep, sizeof (keep))) {
5035388Sroot 			u.u_error = EFAULT;
5045388Sroot 			return;
5055388Sroot 		}
5065388Sroot 		if (keep)
5076214Swnj 			so->so_options &= ~SO_KEEPALIVE;
5085388Sroot 		else
5096214Swnj 			so->so_options |= SO_KEEPALIVE;
5105388Sroot 		return;
5115388Sroot 	}
5125388Sroot 
5135388Sroot 	case SIOCGKEEP: {
5146214Swnj 		int keep = (so->so_options & SO_KEEPALIVE) != 0;
5155388Sroot 		if (copyout((caddr_t)&keep, cmdp, sizeof (keep)))
5165388Sroot 			u.u_error = EFAULT;
5175388Sroot 		return;
5185388Sroot 	}
5195388Sroot 
5205388Sroot 	case SIOCSLINGER: {
5215388Sroot 		int linger;
5225388Sroot 		if (copyin(cmdp, (caddr_t)&linger, sizeof (linger))) {
5235388Sroot 			u.u_error = EFAULT;
5245388Sroot 			return;
5255388Sroot 		}
5265388Sroot 		so->so_linger = linger;
5275388Sroot 		if (so->so_linger)
5285388Sroot 			so->so_options &= ~SO_DONTLINGER;
5295388Sroot 		else
5305388Sroot 			so->so_options |= SO_DONTLINGER;
5315388Sroot 		return;
5325388Sroot 	}
5335388Sroot 
5345388Sroot 	case SIOCGLINGER: {
5355388Sroot 		int linger = so->so_linger;
5365388Sroot 		if (copyout((caddr_t)&linger, cmdp, sizeof (linger))) {
5375388Sroot 			u.u_error = EFAULT;
5385388Sroot 			return;
5395388Sroot 		}
5405388Sroot 	}
5415423Swnj 	case SIOCSPGRP: {
5425423Swnj 		int pgrp;
5435423Swnj 		if (copyin(cmdp, (caddr_t)&pgrp, sizeof (pgrp))) {
5445423Swnj 			u.u_error = EFAULT;
5455423Swnj 			return;
5465423Swnj 		}
5475423Swnj 		so->so_pgrp = pgrp;
5485423Swnj 		return;
5495423Swnj 	}
5505388Sroot 
5515423Swnj 	case SIOCGPGRP: {
5525423Swnj 		int pgrp = so->so_pgrp;
5535423Swnj 		if (copyout((caddr_t)&pgrp, cmdp, sizeof (pgrp))) {
5545423Swnj 			u.u_error = EFAULT;
5555423Swnj 			return;
5565423Swnj 		}
5575423Swnj 	}
5585423Swnj 
5595281Sroot 	case SIOCDONE: {
5605281Sroot 		int flags;
5615281Sroot 		if (copyin(cmdp, (caddr_t)&flags, sizeof (flags))) {
5625281Sroot 			u.u_error = EFAULT;
5635281Sroot 			return;
5645281Sroot 		}
5655388Sroot 		flags++;
5665281Sroot 		if (flags & FREAD) {
5675281Sroot 			int s = splimp();
5685281Sroot 			socantrcvmore(so);
5695281Sroot 			sbflush(&so->so_rcv);
5706140Ssam 			splx(s);
5715281Sroot 		}
5725281Sroot 		if (flags & FWRITE)
5735404Swnj 			u.u_error = (*so->so_proto->pr_usrreq)(so, PRU_SHUTDOWN, (struct mbuf *)0, 0);
5745281Sroot 		return;
5754829Swnj 	}
5765281Sroot 
5775423Swnj 	case SIOCSENDOOB: {
5785423Swnj 		char oob;
5795423Swnj 		struct mbuf *m;
5805423Swnj 		if (copyin(cmdp, (caddr_t)&oob, sizeof (oob))) {
5815423Swnj 			u.u_error = EFAULT;
5825423Swnj 			return;
5835423Swnj 		}
5845423Swnj 		m = m_get(M_DONTWAIT);
5855423Swnj 		if (m == 0) {
5865423Swnj 			u.u_error = ENOBUFS;
5875423Swnj 			return;
5885423Swnj 		}
5895423Swnj 		m->m_off = MMINOFF;
5905423Swnj 		m->m_len = 1;
5915423Swnj 		*mtod(m, caddr_t) = oob;
5925423Swnj 		(*so->so_proto->pr_usrreq)(so, PRU_SENDOOB, m, 0);
5935423Swnj 		return;
5945281Sroot 	}
5955423Swnj 
5965423Swnj 	case SIOCRCVOOB: {
5975423Swnj 		struct mbuf *m = m_get(M_DONTWAIT);
5985423Swnj 		if (m == 0) {
5995423Swnj 			u.u_error = ENOBUFS;
6005423Swnj 			return;
6015423Swnj 		}
6025423Swnj 		m->m_off = MMINOFF; *mtod(m, caddr_t) = 0;
6035423Swnj 		(*so->so_proto->pr_usrreq)(so, PRU_RCVOOB, m, 0);
6045423Swnj 		if (copyout(mtod(m, caddr_t), cmdp, sizeof (char))) {
6055423Swnj 			u.u_error = EFAULT;
6065423Swnj 			return;
6075423Swnj 		}
6085423Swnj 		m_free(m);
6095423Swnj 		return;
6105423Swnj 	}
6115423Swnj 
6125423Swnj 	case SIOCATMARK: {
6135423Swnj 		int atmark = (so->so_state&SS_RCVATMARK) != 0;
6145423Swnj 		if (copyout((caddr_t)&atmark, cmdp, sizeof (atmark))) {
6155423Swnj 			u.u_error = EFAULT;
6165423Swnj 			return;
6175423Swnj 		}
6185423Swnj 		return;
6195423Swnj 	}
6206355Ssam 
6216355Ssam 	/* routing table update calls */
6226355Ssam 	case SIOCADDRT:
6236355Ssam 	case SIOCDELRT:
6246355Ssam 	case SIOCCHGRT: {
6256355Ssam 		struct rtentry route;
6266355Ssam 		if (!suser())
6276355Ssam 			return;
6286355Ssam 		if (copyin(cmdp, (caddr_t)&route, sizeof (route))) {
6296355Ssam 			u.u_error = EFAULT;
6306355Ssam 			return;
6316355Ssam 		}
6326355Ssam 		u.u_error = rtrequest(cmd, &route);
6336355Ssam 		return;
6346355Ssam 	}
6356355Ssam 
6365445Swnj 	/* type/protocol specific ioctls */
6375423Swnj 	}
6385445Swnj 	u.u_error = EOPNOTSUPP;
6394786Swnj }
640