xref: /csrg-svn/sys/kern/uipc_socket.c (revision 6880)
1*6880Ssam /*	uipc_socket.c	4.40	82/05/20	*/
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) {
854971Swnj 		(void) m_free(dtom(so));
864890Swnj 		return (error);
874786Swnj 	}
884786Swnj 	*aso = so;
894786Swnj 	return (0);
904786Swnj }
914786Swnj 
924916Swnj sofree(so)
934916Swnj 	struct socket *so;
944916Swnj {
954916Swnj 
964927Swnj COUNT(SOFREE);
974950Swnj 	if (so->so_pcb || (so->so_state & SS_USERGONE) == 0)
984950Swnj 		return;
994950Swnj 	sbrelease(&so->so_snd);
1004950Swnj 	sbrelease(&so->so_rcv);
1014971Swnj 	(void) m_free(dtom(so));
1024916Swnj }
1034916Swnj 
1044786Swnj /*
1054890Swnj  * Close a socket on last file table reference removal.
1064890Swnj  * Initiate disconnect if connected.
1074890Swnj  * Free socket when disconnect complete.
1085580Sroot  *
1095580Sroot  * THIS IS REALLY A UNIX INTERFACE ROUTINE
1104829Swnj  */
1115580Sroot soclose(so, exiting)
1124829Swnj 	register struct socket *so;
1135580Sroot 	int exiting;
1144829Swnj {
1154890Swnj 	int s = splnet();		/* conservative */
1164829Swnj 
1174927Swnj COUNT(SOCLOSE);
1184890Swnj 	if (so->so_pcb == 0)
1194890Swnj 		goto discard;
1206259Sroot 	if (exiting)
1216259Sroot 		so->so_options |= SO_KEEPALIVE;
1224890Swnj 	if (so->so_state & SS_ISCONNECTED) {
1234890Swnj 		if ((so->so_state & SS_ISDISCONNECTING) == 0) {
1244927Swnj 			u.u_error = sodisconnect(so, (struct sockaddr *)0);
1254890Swnj 			if (u.u_error) {
1265580Sroot 				if (exiting)
1275580Sroot 					goto drop;
1284890Swnj 				splx(s);
1294890Swnj 				return;
1304890Swnj 			}
1314890Swnj 		}
1325388Sroot 		if ((so->so_options & SO_DONTLINGER) == 0) {
1335281Sroot 			if ((so->so_state & SS_ISDISCONNECTING) &&
1346214Swnj 			    (so->so_state & SS_NBIO) &&
1355580Sroot 			    exiting == 0) {
1365281Sroot 				u.u_error = EINPROGRESS;
1375281Sroot 				splx(s);
1385281Sroot 				return;
1395281Sroot 			}
1405580Sroot 			/* should use tsleep here, for at most linger */
1415281Sroot 			while (so->so_state & SS_ISCONNECTED)
1425281Sroot 				sleep((caddr_t)&so->so_timeo, PZERO+1);
1434890Swnj 		}
1444890Swnj 	}
1455580Sroot drop:
146*6880Ssam 	if (so->so_pcb) {
147*6880Ssam 		u.u_error = (*so->so_proto->pr_usrreq)(so, PRU_DETACH, 0, 0);
148*6880Ssam 		if (exiting == 0 && u.u_error) {
149*6880Ssam 			splx(s);
150*6880Ssam 			return;
151*6880Ssam 		}
152*6880Ssam 	}
1534890Swnj discard:
1544950Swnj 	so->so_state |= SS_USERGONE;
1554950Swnj 	sofree(so);
1564890Swnj 	splx(s);
1574829Swnj }
1584829Swnj 
1594916Swnj /*ARGSUSED*/
1604890Swnj sostat(so, sb)
1614829Swnj 	struct socket *so;
1624890Swnj 	struct stat *sb;
1634829Swnj {
1644829Swnj 
1654927Swnj COUNT(SOSTAT);
1665303Sroot 	bzero((caddr_t)sb, sizeof (*sb));		/* XXX */
1675303Sroot 	return (0);					/* XXX */
1684829Swnj }
1694829Swnj 
1704829Swnj /*
1714927Swnj  * Accept connection on a socket.
1724927Swnj  */
1734927Swnj soaccept(so, asa)
1744927Swnj 	struct socket *so;
1754927Swnj 	struct sockaddr *asa;
1764927Swnj {
1774927Swnj 	int s = splnet();
1784927Swnj 	int error;
1794927Swnj 
1804927Swnj COUNT(SOACCEPT);
1814927Swnj 	if ((so->so_options & SO_ACCEPTCONN) == 0) {
1824927Swnj 		error = EINVAL;			/* XXX */
1834927Swnj 		goto bad;
1844927Swnj 	}
1855265Swnj 	if ((so->so_state & SS_CONNAWAITING) == 0) {
1865265Swnj 		error = ENOTCONN;
1875265Swnj 		goto bad;
1885265Swnj 	}
1895265Swnj 	so->so_state &= ~SS_CONNAWAITING;
1904927Swnj 	error = (*so->so_proto->pr_usrreq)(so, PRU_ACCEPT, 0, (caddr_t)asa);
1914927Swnj bad:
1924927Swnj 	splx(s);
1934927Swnj 	return (error);
1944927Swnj }
1954927Swnj 
1964927Swnj /*
1974890Swnj  * Connect socket to a specified address.
1984890Swnj  * If already connected or connecting, then avoid
1994890Swnj  * the protocol entry, to keep its job simpler.
2004786Swnj  */
2014927Swnj soconnect(so, asa)
2024786Swnj 	struct socket *so;
2034927Swnj 	struct sockaddr *asa;
2044786Swnj {
2054890Swnj 	int s = splnet();
2064890Swnj 	int error;
2074786Swnj 
2084927Swnj COUNT(SOCONNECT);
2094890Swnj 	if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) {
2104890Swnj 		error = EISCONN;
2114890Swnj 		goto bad;
2124890Swnj 	}
2134927Swnj 	error = (*so->so_proto->pr_usrreq)(so, PRU_CONNECT, 0, (caddr_t)asa);
2144890Swnj bad:
2154890Swnj 	splx(s);
2164890Swnj 	return (error);
2174786Swnj }
2184786Swnj 
2194786Swnj /*
2204890Swnj  * Disconnect from a socket.
2214890Swnj  * Address parameter is from system call for later multicast
2224890Swnj  * protocols.  Check to make sure that connected and no disconnect
2234890Swnj  * in progress (for protocol's sake), and then invoke protocol.
2244786Swnj  */
2254927Swnj sodisconnect(so, asa)
2264786Swnj 	struct socket *so;
2274927Swnj 	struct sockaddr *asa;
2284786Swnj {
2294890Swnj 	int s = splnet();
2304890Swnj 	int error;
2314786Swnj 
2324927Swnj COUNT(SODISCONNECT);
2334890Swnj 	if ((so->so_state & SS_ISCONNECTED) == 0) {
2344890Swnj 		error = ENOTCONN;
2354890Swnj 		goto bad;
2364890Swnj 	}
2374890Swnj 	if (so->so_state & SS_ISDISCONNECTING) {
2384890Swnj 		error = EALREADY;
2394890Swnj 		goto bad;
2404890Swnj 	}
2414927Swnj 	error = (*so->so_proto->pr_usrreq)(so, PRU_DISCONNECT, 0, asa);
2424890Swnj bad:
2434890Swnj 	splx(s);
2444890Swnj 	return (error);
2454786Swnj }
2464786Swnj 
2474786Swnj /*
2484890Swnj  * Send on a socket.
2494890Swnj  * If send must go all at once and message is larger than
2504890Swnj  * send buffering, then hard error.
2514890Swnj  * Lock against other senders.
2524890Swnj  * If must go all at once and not enough room now, then
2534890Swnj  * inform user that this would block and do nothing.
2544786Swnj  */
2554927Swnj sosend(so, asa)
2564786Swnj 	register struct socket *so;
2574927Swnj 	struct sockaddr *asa;
2584786Swnj {
2594890Swnj 	struct mbuf *top = 0;
2604890Swnj 	register struct mbuf *m, **mp = ⊤
2614916Swnj 	register u_int len;
2624916Swnj 	int error = 0, space, s;
2634786Swnj 
2644927Swnj COUNT(SOSEND);
2654890Swnj 	if (sosendallatonce(so) && u.u_count > so->so_snd.sb_hiwat)
2664890Swnj 		return (EMSGSIZE);
2676419Sroot #ifdef notdef
2686419Sroot 	/* NEED TO PREVENT BUSY WAITING IN SELECT FOR WRITING */
2696214Swnj 	if ((so->so_snd.sb_flags & SB_LOCK) && (so->so_state & SS_NBIO))
2704890Swnj 		return (EWOULDBLOCK);
2716419Sroot #endif
2726419Sroot restart:
2734890Swnj 	sblock(&so->so_snd);
2744890Swnj #define	snderr(errno)	{ error = errno; splx(s); goto release; }
2754890Swnj 
2766419Sroot again:
2774890Swnj 	s = splnet();
2786419Sroot 	if (so->so_state & SS_CANTSENDMORE) {
2796419Sroot 		psignal(u.u_procp, SIGPIPE);
2806419Sroot 		snderr(EPIPE);
2816419Sroot 	}
2825168Swnj 	if (so->so_error) {
2835168Swnj 		error = so->so_error;
2846419Sroot 		so->so_error = 0;				/* ??? */
2855168Swnj 		splx(s);
2865168Swnj 		goto release;
2875168Swnj 	}
2884890Swnj 	if ((so->so_state & SS_ISCONNECTED) == 0) {
2894890Swnj 		if (so->so_proto->pr_flags & PR_CONNREQUIRED)
2904890Swnj 			snderr(ENOTCONN);
2914927Swnj 		if (asa == 0)
2924890Swnj 			snderr(EDESTADDRREQ);
2934890Swnj 	}
2944890Swnj 	if (top) {
2954927Swnj 		error = (*so->so_proto->pr_usrreq)(so, PRU_SEND, top, asa);
2966419Sroot 		top = 0;
2974890Swnj 		if (error) {
2984890Swnj 			splx(s);
2994786Swnj 			goto release;
3004786Swnj 		}
3014890Swnj 		mp = ⊤
3024786Swnj 	}
3034979Swnj 	if (u.u_count == 0) {
3044979Swnj 		splx(s);
3054979Swnj 		goto release;
3064979Swnj 	}
3075018Swnj 	space = sbspace(&so->so_snd);
3085610Swnj 	if (space <= 0 || sosendallatonce(so) && space < u.u_count) {
3096214Swnj 		if (so->so_state & SS_NBIO)
3104890Swnj 			snderr(EWOULDBLOCK);
3114890Swnj 		sbunlock(&so->so_snd);
3124890Swnj 		sbwait(&so->so_snd);
3134890Swnj 		splx(s);
3146419Sroot 		goto restart;
3154786Swnj 	}
3164890Swnj 	splx(s);
3175018Swnj 	while (u.u_count && space > 0) {
3184890Swnj 		MGET(m, 1);
3194890Swnj 		if (m == NULL) {
3206419Sroot 			error = ENOBUFS;			/* SIGPIPE? */
3214890Swnj 			goto release;
3224786Swnj 		}
3235095Swnj 		if (u.u_count >= CLBYTES && space >= CLBYTES) {
3244890Swnj 			register struct mbuf *p;
3255095Swnj 			MCLGET(p, 1);
3264890Swnj 			if (p == 0)
3274890Swnj 				goto nopages;
3284890Swnj 			m->m_off = (int)p - (int)m;
3295095Swnj 			len = CLBYTES;
3304890Swnj 		} else {
3314786Swnj nopages:
3324890Swnj 			m->m_off = MMINOFF;
3334890Swnj 			len = MIN(MLEN, u.u_count);
3344786Swnj 		}
3354890Swnj 		iomove(mtod(m, caddr_t), len, B_WRITE);
3364890Swnj 		m->m_len = len;
3374890Swnj 		*mp = m;
3384890Swnj 		mp = &m->m_next;
3395018Swnj 		space = sbspace(&so->so_snd);
3404786Swnj 	}
3414890Swnj 	goto again;
3424890Swnj 
3434786Swnj release:
3444890Swnj 	sbunlock(&so->so_snd);
3456419Sroot 	if (top)
3466419Sroot 		m_freem(top);
3474786Swnj 	return (error);
3484786Swnj }
3494786Swnj 
3504927Swnj soreceive(so, asa)
3514786Swnj 	register struct socket *so;
3524927Swnj 	struct sockaddr *asa;
3534786Swnj {
3544786Swnj 	register struct mbuf *m, *n;
3554916Swnj 	u_int len;
3565423Swnj 	int eor, s, error = 0, cnt = u.u_count;
3575423Swnj 	caddr_t base = u.u_base;
3584786Swnj 
3594927Swnj COUNT(SORECEIVE);
3604890Swnj restart:
3614890Swnj 	sblock(&so->so_rcv);
3624890Swnj 	s = splnet();
3634890Swnj 
3644890Swnj #define	rcverr(errno)	{ error = errno; splx(s); goto release; }
3654786Swnj 	if (so->so_rcv.sb_cc == 0) {
3665168Swnj 		if (so->so_error) {
3675168Swnj 			error = so->so_error;
3685168Swnj 			so->so_error = 0;
3695168Swnj 			splx(s);
3705168Swnj 			goto release;
3715168Swnj 		}
3724890Swnj 		if (so->so_state & SS_CANTRCVMORE) {
3734890Swnj 			splx(s);
3744890Swnj 			goto release;
3754890Swnj 		}
3765015Sroot 		if ((so->so_state & SS_ISCONNECTED) == 0 &&
3775015Sroot 		    (so->so_proto->pr_flags & PR_CONNREQUIRED))
3785015Sroot 			rcverr(ENOTCONN);
3796214Swnj 		if (so->so_state & SS_NBIO)
3805168Swnj 			rcverr(EWOULDBLOCK);
3814890Swnj 		sbunlock(&so->so_rcv);
3824971Swnj 		sbwait(&so->so_rcv);
3835012Swnj 		splx(s);
3844890Swnj 		goto restart;
3854786Swnj 	}
3864829Swnj 	m = so->so_rcv.sb_mb;
3874786Swnj 	if (m == 0)
3884786Swnj 		panic("receive");
3895039Swnj 	if (so->so_proto->pr_flags & PR_ADDR) {
3905039Swnj 		if (m->m_len != sizeof (struct sockaddr))
3915039Swnj 			panic("soreceive addr");
3925039Swnj 		if (asa)
3935039Swnj 			bcopy(mtod(m, caddr_t), (caddr_t)asa, sizeof (*asa));
3945039Swnj 		so->so_rcv.sb_cc -= m->m_len;
3955039Swnj 		so->so_rcv.sb_mbcnt -= MSIZE;
3965018Swnj 		m = m_free(m);
3974890Swnj 		if (m == 0)
3984890Swnj 			panic("receive 2");
3995018Swnj 		so->so_rcv.sb_mb = m;
4004890Swnj 	}
4015423Swnj 	so->so_state &= ~SS_RCVATMARK;
4025423Swnj 	if (so->so_oobmark && cnt > so->so_oobmark)
4035423Swnj 		cnt = so->so_oobmark;
4044786Swnj 	eor = 0;
4054786Swnj 	do {
4065423Swnj 		len = MIN(m->m_len, cnt);
4074786Swnj 		splx(s);
4084786Swnj 		iomove(mtod(m, caddr_t), len, B_READ);
4095423Swnj 		cnt -= len;
4104786Swnj 		s = splnet();
4114786Swnj 		if (len == m->m_len) {
4126091Sroot 			eor = (int)m->m_act;
4136091Sroot 			sbfree(&so->so_rcv, m);
4146091Sroot 			so->so_rcv.sb_mb = m->m_next;
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)
4786214Swnj 			so->so_state |= SS_NBIO;
4795388Sroot 		else
4806214Swnj 			so->so_state &= ~SS_NBIO;
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)
4916214Swnj 			so->so_state |= SS_ASYNC;
4925388Sroot 		else
4936214Swnj 			so->so_state &= ~SS_ASYNC;
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)
5046214Swnj 			so->so_options &= ~SO_KEEPALIVE;
5055388Sroot 		else
5066214Swnj 			so->so_options |= SO_KEEPALIVE;
5075388Sroot 		return;
5085388Sroot 	}
5095388Sroot 
5105388Sroot 	case SIOCGKEEP: {
5116214Swnj 		int keep = (so->so_options & SO_KEEPALIVE) != 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);
5676140Ssam 			splx(s);
5685281Sroot 		}
5695281Sroot 		if (flags & FWRITE)
5705404Swnj 			u.u_error = (*so->so_proto->pr_usrreq)(so, PRU_SHUTDOWN, (struct mbuf *)0, 0);
5715281Sroot 		return;
5724829Swnj 	}
5735281Sroot 
5745423Swnj 	case SIOCSENDOOB: {
5755423Swnj 		char oob;
5765423Swnj 		struct mbuf *m;
5775423Swnj 		if (copyin(cmdp, (caddr_t)&oob, sizeof (oob))) {
5785423Swnj 			u.u_error = EFAULT;
5795423Swnj 			return;
5805423Swnj 		}
5815423Swnj 		m = m_get(M_DONTWAIT);
5825423Swnj 		if (m == 0) {
5835423Swnj 			u.u_error = ENOBUFS;
5845423Swnj 			return;
5855423Swnj 		}
5865423Swnj 		m->m_off = MMINOFF;
5875423Swnj 		m->m_len = 1;
5885423Swnj 		*mtod(m, caddr_t) = oob;
5895423Swnj 		(*so->so_proto->pr_usrreq)(so, PRU_SENDOOB, m, 0);
5905423Swnj 		return;
5915281Sroot 	}
5925423Swnj 
5935423Swnj 	case SIOCRCVOOB: {
5945423Swnj 		struct mbuf *m = m_get(M_DONTWAIT);
5955423Swnj 		if (m == 0) {
5965423Swnj 			u.u_error = ENOBUFS;
5975423Swnj 			return;
5985423Swnj 		}
5995423Swnj 		m->m_off = MMINOFF; *mtod(m, caddr_t) = 0;
6005423Swnj 		(*so->so_proto->pr_usrreq)(so, PRU_RCVOOB, m, 0);
6015423Swnj 		if (copyout(mtod(m, caddr_t), cmdp, sizeof (char))) {
6025423Swnj 			u.u_error = EFAULT;
6035423Swnj 			return;
6045423Swnj 		}
6055423Swnj 		m_free(m);
6065423Swnj 		return;
6075423Swnj 	}
6085423Swnj 
6095423Swnj 	case SIOCATMARK: {
6105423Swnj 		int atmark = (so->so_state&SS_RCVATMARK) != 0;
6115423Swnj 		if (copyout((caddr_t)&atmark, cmdp, sizeof (atmark))) {
6125423Swnj 			u.u_error = EFAULT;
6135423Swnj 			return;
6145423Swnj 		}
6155423Swnj 		return;
6165423Swnj 	}
6176355Ssam 
6186355Ssam 	/* routing table update calls */
6196355Ssam 	case SIOCADDRT:
6206355Ssam 	case SIOCDELRT:
6216355Ssam 	case SIOCCHGRT: {
6226355Ssam 		struct rtentry route;
6236355Ssam 		if (!suser())
6246355Ssam 			return;
6256355Ssam 		if (copyin(cmdp, (caddr_t)&route, sizeof (route))) {
6266355Ssam 			u.u_error = EFAULT;
6276355Ssam 			return;
6286355Ssam 		}
6296355Ssam 		u.u_error = rtrequest(cmd, &route);
6306355Ssam 		return;
6316355Ssam 	}
6326355Ssam 
6335445Swnj 	/* type/protocol specific ioctls */
6345423Swnj 	}
6355445Swnj 	u.u_error = EOPNOTSUPP;
6364786Swnj }
637