xref: /csrg-svn/sys/kern/uipc_socket.c (revision 14813)
1*14813Ssam /*	uipc_socket.c	4.78	83/08/23	*/
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"
1212757Ssam #include "../h/un.h"
134829Swnj #include "../h/protosw.h"
144829Swnj #include "../h/socket.h"
154829Swnj #include "../h/socketvar.h"
164916Swnj #include "../h/stat.h"
175281Sroot #include "../h/ioctl.h"
188391Swnj #include "../h/uio.h"
196355Ssam #include "../net/route.h"
2012757Ssam #include "../netinet/in.h"
2111571Ssam #include "../net/if.h"
224786Swnj 
234786Swnj /*
248300Sroot  * Socket operation routines.
258300Sroot  * These routines are called by the routines in
268300Sroot  * sys_socket.c or from a system process, and
278300Sroot  * implement the semantics of socket operations by
288300Sroot  * switching out to the protocol specific routines.
2912757Ssam  *
3012757Ssam  * TODO:
3112757Ssam  *	sostat
3212757Ssam  *	test socketpair
3312757Ssam  *	PR_RIGHTS
3412757Ssam  *	clean up select, async
3512757Ssam  *	out-of-band is a kludge
364786Swnj  */
378594Sroot /*ARGSUSED*/
3810267Ssam socreate(dom, aso, type, proto)
394786Swnj 	struct socket **aso;
4012757Ssam 	register int type;
4112757Ssam 	int proto;
424786Swnj {
434786Swnj 	register struct protosw *prp;
444786Swnj 	register struct socket *so;
4512757Ssam 	register struct mbuf *m;
4612757Ssam 	register int error;
474786Swnj 
484890Swnj 	if (proto)
499168Ssam 		prp = pffindproto(dom, proto);
504890Swnj 	else
519168Ssam 		prp = pffindtype(dom, type);
524890Swnj 	if (prp == 0)
534890Swnj 		return (EPROTONOSUPPORT);
548300Sroot 	if (prp->pr_type != type)
558300Sroot 		return (EPROTOTYPE);
569635Ssam 	m = m_getclr(M_WAIT, MT_SOCKET);
574786Swnj 	if (m == 0)
584786Swnj 		return (ENOBUFS);
594786Swnj 	so = mtod(m, struct socket *);
6012757Ssam 	so->so_options = 0;
616214Swnj 	so->so_state = 0;
629168Ssam 	so->so_type = type;
636214Swnj 	if (u.u_uid == 0)
646214Swnj 		so->so_state = SS_PRIV;
654786Swnj 	so->so_proto = prp;
6612757Ssam 	error =
6712757Ssam 	    (*prp->pr_usrreq)(so, PRU_ATTACH,
6812757Ssam 		(struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0);
694979Swnj 	if (error) {
707507Sroot 		so->so_state |= SS_NOFDREF;
717180Swnj 		sofree(so);
724890Swnj 		return (error);
734786Swnj 	}
744786Swnj 	*aso = so;
754786Swnj 	return (0);
764786Swnj }
774786Swnj 
7810267Ssam sobind(so, nam)
798300Sroot 	struct socket *so;
808300Sroot 	struct mbuf *nam;
818300Sroot {
828300Sroot 	int s = splnet();
838300Sroot 	int error;
848300Sroot 
858300Sroot 	error =
8612757Ssam 	    (*so->so_proto->pr_usrreq)(so, PRU_BIND,
8712757Ssam 		(struct mbuf *)0, nam, (struct mbuf *)0);
888300Sroot 	splx(s);
898300Sroot 	return (error);
908300Sroot }
918300Sroot 
928300Sroot solisten(so, backlog)
9312757Ssam 	register struct socket *so;
948300Sroot 	int backlog;
958300Sroot {
9612757Ssam 	int s = splnet(), error;
978300Sroot 
9812757Ssam 	error =
9912757Ssam 	    (*so->so_proto->pr_usrreq)(so, PRU_LISTEN,
10012757Ssam 		(struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0);
1018300Sroot 	if (error) {
1028300Sroot 		splx(s);
1038300Sroot 		return (error);
1048300Sroot 	}
1058300Sroot 	if (so->so_q == 0) {
1068300Sroot 		so->so_q = so;
1078300Sroot 		so->so_q0 = so;
1088300Sroot 		so->so_options |= SO_ACCEPTCONN;
1098300Sroot 	}
1108300Sroot 	if (backlog < 0)
1118300Sroot 		backlog = 0;
11210137Ssam 	so->so_qlimit = MIN(backlog, SOMAXCONN);
11312493Ssam 	splx(s);
1148300Sroot 	return (0);
1158300Sroot }
1168300Sroot 
1174916Swnj sofree(so)
11812757Ssam 	register struct socket *so;
1194916Swnj {
1204916Swnj 
1217507Sroot 	if (so->so_head) {
1227507Sroot 		if (!soqremque(so, 0) && !soqremque(so, 1))
1237507Sroot 			panic("sofree dq");
1247507Sroot 		so->so_head = 0;
1257507Sroot 	}
1267507Sroot 	if (so->so_pcb || (so->so_state & SS_NOFDREF) == 0)
1274950Swnj 		return;
1284950Swnj 	sbrelease(&so->so_snd);
12912757Ssam 	sorflush(so);
1304971Swnj 	(void) m_free(dtom(so));
1314916Swnj }
1324916Swnj 
1334786Swnj /*
1344890Swnj  * Close a socket on last file table reference removal.
1354890Swnj  * Initiate disconnect if connected.
1364890Swnj  * Free socket when disconnect complete.
1374829Swnj  */
13812757Ssam soclose(so)
1394829Swnj 	register struct socket *so;
1404829Swnj {
1414890Swnj 	int s = splnet();		/* conservative */
1428713Sroot 	int error;
1434829Swnj 
1447507Sroot 	if (so->so_options & SO_ACCEPTCONN) {
1457507Sroot 		while (so->so_q0 != so)
14610399Ssam 			(void) soabort(so->so_q0);
1477507Sroot 		while (so->so_q != so)
14810399Ssam 			(void) soabort(so->so_q);
1497507Sroot 	}
1504890Swnj 	if (so->so_pcb == 0)
1514890Swnj 		goto discard;
1524890Swnj 	if (so->so_state & SS_ISCONNECTED) {
1534890Swnj 		if ((so->so_state & SS_ISDISCONNECTING) == 0) {
1548725Sroot 			error = sodisconnect(so, (struct mbuf *)0);
15512757Ssam 			if (error)
15612757Ssam 				goto drop;
1574890Swnj 		}
15810267Ssam 		if (so->so_options & SO_LINGER) {
1595281Sroot 			if ((so->so_state & SS_ISDISCONNECTING) &&
16012757Ssam 			    (so->so_state & SS_NBIO))
16112757Ssam 				goto drop;
1625281Sroot 			while (so->so_state & SS_ISCONNECTED)
1635281Sroot 				sleep((caddr_t)&so->so_timeo, PZERO+1);
1644890Swnj 		}
1654890Swnj 	}
1665580Sroot drop:
1676880Ssam 	if (so->so_pcb) {
16812757Ssam 		int error2 =
16912757Ssam 		    (*so->so_proto->pr_usrreq)(so, PRU_DETACH,
17012757Ssam 			(struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0);
17112757Ssam 		if (error == 0)
17212757Ssam 			error = error2;
1736880Ssam 	}
1744890Swnj discard:
17510399Ssam 	if (so->so_state & SS_NOFDREF)
17610399Ssam 		panic("soclose: NOFDREF");
1777507Sroot 	so->so_state |= SS_NOFDREF;
1784950Swnj 	sofree(so);
1794890Swnj 	splx(s);
18012757Ssam 	return (error);
1814829Swnj }
1824829Swnj 
18310399Ssam /*
18410399Ssam  * Must be called at splnet...
18510399Ssam  */
18610399Ssam soabort(so)
18710399Ssam 	struct socket *so;
18810399Ssam {
18910399Ssam 
19012757Ssam 	return (
19112757Ssam 	    (*so->so_proto->pr_usrreq)(so, PRU_ABORT,
19212757Ssam 		(struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0));
19310399Ssam }
19410399Ssam 
19510267Ssam soaccept(so, nam)
19612757Ssam 	register struct socket *so;
1978300Sroot 	struct mbuf *nam;
1984927Swnj {
1994927Swnj 	int s = splnet();
2004927Swnj 	int error;
2014927Swnj 
20210399Ssam 	if ((so->so_state & SS_NOFDREF) == 0)
20310399Ssam 		panic("soaccept: !NOFDREF");
20410267Ssam 	so->so_state &= ~SS_NOFDREF;
2058300Sroot 	error = (*so->so_proto->pr_usrreq)(so, PRU_ACCEPT,
20612757Ssam 	    (struct mbuf *)0, nam, (struct mbuf *)0);
2074927Swnj 	splx(s);
2084927Swnj 	return (error);
2094927Swnj }
2104927Swnj 
21110267Ssam soconnect(so, nam)
21212757Ssam 	register struct socket *so;
2138300Sroot 	struct mbuf *nam;
2144786Swnj {
2154890Swnj 	int s = splnet();
2164890Swnj 	int error;
2174786Swnj 
2184890Swnj 	if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) {
2194890Swnj 		error = EISCONN;
2204890Swnj 		goto bad;
2214890Swnj 	}
2228300Sroot 	error = (*so->so_proto->pr_usrreq)(so, PRU_CONNECT,
22312757Ssam 	    (struct mbuf *)0, nam, (struct mbuf *)0);
2244890Swnj bad:
2254890Swnj 	splx(s);
2264890Swnj 	return (error);
2274786Swnj }
2284786Swnj 
22912757Ssam soconnect2(so1, so2)
23012757Ssam 	register struct socket *so1;
23112757Ssam 	struct socket *so2;
23212757Ssam {
23312757Ssam 	int s = splnet();
23412757Ssam 	int error;
23512757Ssam 
23613113Ssam 	error = (*so1->so_proto->pr_usrreq)(so1, PRU_CONNECT2,
23713113Ssam 	    (struct mbuf *)0, (struct mbuf *)so2, (struct mbuf *)0);
23812757Ssam 	splx(s);
23912757Ssam 	return (error);
24012757Ssam }
24112757Ssam 
2428300Sroot sodisconnect(so, nam)
24312757Ssam 	register struct socket *so;
2448300Sroot 	struct mbuf *nam;
2454786Swnj {
2464890Swnj 	int s = splnet();
2474890Swnj 	int error;
2484786Swnj 
2494890Swnj 	if ((so->so_state & SS_ISCONNECTED) == 0) {
2504890Swnj 		error = ENOTCONN;
2514890Swnj 		goto bad;
2524890Swnj 	}
2534890Swnj 	if (so->so_state & SS_ISDISCONNECTING) {
2544890Swnj 		error = EALREADY;
2554890Swnj 		goto bad;
2564890Swnj 	}
2578300Sroot 	error = (*so->so_proto->pr_usrreq)(so, PRU_DISCONNECT,
25812757Ssam 	    (struct mbuf *)0, nam, (struct mbuf *)0);
2594890Swnj bad:
2604890Swnj 	splx(s);
2614890Swnj 	return (error);
2624786Swnj }
2634786Swnj 
2644786Swnj /*
2654890Swnj  * Send on a socket.
2664890Swnj  * If send must go all at once and message is larger than
2674890Swnj  * send buffering, then hard error.
2684890Swnj  * Lock against other senders.
2694890Swnj  * If must go all at once and not enough room now, then
2704890Swnj  * inform user that this would block and do nothing.
2714786Swnj  */
27212757Ssam sosend(so, nam, uio, flags, rights)
2734786Swnj 	register struct socket *so;
2748300Sroot 	struct mbuf *nam;
27512757Ssam 	register struct uio *uio;
2768319Sroot 	int flags;
27712757Ssam 	struct mbuf *rights;
2784786Swnj {
2794890Swnj 	struct mbuf *top = 0;
2804890Swnj 	register struct mbuf *m, **mp = &top;
28112757Ssam 	register int space;
28212757Ssam 	int len, error = 0, s, dontroute;
2834786Swnj 
2847827Sroot 	if (sosendallatonce(so) && uio->uio_resid > so->so_snd.sb_hiwat)
2854890Swnj 		return (EMSGSIZE);
28612757Ssam 	dontroute =
28712757Ssam 	    (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&
28812757Ssam 	    (so->so_proto->pr_flags & PR_ATOMIC);
2896419Sroot restart:
2904890Swnj 	sblock(&so->so_snd);
2914890Swnj #define	snderr(errno)	{ error = errno; splx(s); goto release; }
2924890Swnj 
2938041Sroot 	u.u_ru.ru_msgsnd++;
2946419Sroot again:
2954890Swnj 	s = splnet();
2966419Sroot 	if (so->so_state & SS_CANTSENDMORE) {
2976419Sroot 		psignal(u.u_procp, SIGPIPE);
2986419Sroot 		snderr(EPIPE);
2996419Sroot 	}
3005168Swnj 	if (so->so_error) {
3015168Swnj 		error = so->so_error;
3026419Sroot 		so->so_error = 0;				/* ??? */
3035168Swnj 		splx(s);
3045168Swnj 		goto release;
3055168Swnj 	}
3064890Swnj 	if ((so->so_state & SS_ISCONNECTED) == 0) {
3074890Swnj 		if (so->so_proto->pr_flags & PR_CONNREQUIRED)
3084890Swnj 			snderr(ENOTCONN);
3098300Sroot 		if (nam == 0)
3104890Swnj 			snderr(EDESTADDRREQ);
3114890Swnj 	}
3124890Swnj 	if (top) {
31310267Ssam 		if (dontroute)
31410267Ssam 			so->so_options |= SO_DONTROUTE;
3158319Sroot 		error = (*so->so_proto->pr_usrreq)(so,
31612757Ssam 		    (flags & MSG_OOB) ? PRU_SENDOOB : PRU_SEND,
31712757Ssam 		    top, (caddr_t)nam, rights);
31810267Ssam 		if (dontroute)
31910267Ssam 			so->so_options &= ~SO_DONTROUTE;
3206419Sroot 		top = 0;
3214890Swnj 		if (error) {
3224890Swnj 			splx(s);
3234786Swnj 			goto release;
3244786Swnj 		}
3254890Swnj 		mp = &top;
3264786Swnj 	}
3277827Sroot 	if (uio->uio_resid == 0) {
3284979Swnj 		splx(s);
3294979Swnj 		goto release;
3304979Swnj 	}
33112757Ssam 	if (flags & MSG_OOB)
3328319Sroot 		space = 1024;
3338319Sroot 	else {
3348319Sroot 		space = sbspace(&so->so_snd);
3358319Sroot 		if (space <= 0 ||
3368319Sroot 		    sosendallatonce(so) && space < uio->uio_resid) {
3378319Sroot 			if (so->so_state & SS_NBIO)
3388319Sroot 				snderr(EWOULDBLOCK);
3398319Sroot 			sbunlock(&so->so_snd);
3408319Sroot 			sbwait(&so->so_snd);
3418319Sroot 			splx(s);
3428319Sroot 			goto restart;
3438319Sroot 		}
3444786Swnj 	}
3454890Swnj 	splx(s);
3467827Sroot 	while (uio->uio_resid > 0 && space > 0) {
3477827Sroot 		register struct iovec *iov = uio->uio_iov;
3487827Sroot 
3497827Sroot 		if (iov->iov_len == 0) {
3507827Sroot 			uio->uio_iov++;
3517827Sroot 			uio->uio_iovcnt--;
3527827Sroot 			if (uio->uio_iovcnt < 0)
3537827Sroot 				panic("sosend");
3547827Sroot 			continue;
3557827Sroot 		}
3569635Ssam 		MGET(m, M_WAIT, MT_DATA);
3574890Swnj 		if (m == NULL) {
3586419Sroot 			error = ENOBUFS;			/* SIGPIPE? */
3594890Swnj 			goto release;
3604786Swnj 		}
3617827Sroot 		if (iov->iov_len >= CLBYTES && space >= CLBYTES) {
3624890Swnj 			register struct mbuf *p;
3635095Swnj 			MCLGET(p, 1);
3644890Swnj 			if (p == 0)
3654890Swnj 				goto nopages;
3664890Swnj 			m->m_off = (int)p - (int)m;
3675095Swnj 			len = CLBYTES;
3684890Swnj 		} else {
3694786Swnj nopages:
3707827Sroot 			len = MIN(MLEN, iov->iov_len);
3714786Swnj 		}
37214781Ssam 		error = uiomove(mtod(m, caddr_t), len, UIO_WRITE, uio);
3734890Swnj 		m->m_len = len;
3744890Swnj 		*mp = m;
37514781Ssam 		if (error)
37614781Ssam 			goto release;
3774890Swnj 		mp = &m->m_next;
37812757Ssam 		if (flags & MSG_OOB)
3798319Sroot 			space -= len;
3808319Sroot 		else
3818319Sroot 			space = sbspace(&so->so_snd);
3824786Swnj 	}
3834890Swnj 	goto again;
3844890Swnj 
3854786Swnj release:
3864890Swnj 	sbunlock(&so->so_snd);
3876419Sroot 	if (top)
3886419Sroot 		m_freem(top);
3894786Swnj 	return (error);
3904786Swnj }
3914786Swnj 
39212757Ssam soreceive(so, aname, uio, flags, rightsp)
3934786Swnj 	register struct socket *so;
3948300Sroot 	struct mbuf **aname;
39512757Ssam 	register struct uio *uio;
3968319Sroot 	int flags;
39712757Ssam 	struct mbuf **rightsp;
3984786Swnj {
3994786Swnj 	register struct mbuf *m, *n;
40012757Ssam 	register int len, error = 0, s, eor, tomark;
40112757Ssam 	struct protosw *pr = so->so_proto;
40212757Ssam 	int moff;
4034786Swnj 
40412757Ssam 	if (rightsp)
40512757Ssam 		*rightsp = 0;
40612757Ssam 	if (aname)
40712757Ssam 		*aname = 0;
40812757Ssam 	if (flags & MSG_OOB) {
4099635Ssam 		m = m_get(M_WAIT, MT_DATA);
41012757Ssam 		if (m == 0)
41110137Ssam 			return (ENOBUFS);
41212757Ssam 		error = (*pr->pr_usrreq)(so, PRU_RCVOOB,
41312757Ssam 		    m, (struct mbuf *)0, (struct mbuf *)0);
4148594Sroot 		if (error)
41510137Ssam 			goto bad;
4168319Sroot 		do {
41710137Ssam 			len = uio->uio_resid;
4188319Sroot 			if (len > m->m_len)
4198319Sroot 				len = m->m_len;
4208594Sroot 			error =
4218793Sroot 			    uiomove(mtod(m, caddr_t), (int)len, UIO_READ, uio);
4228319Sroot 			m = m_free(m);
4238594Sroot 		} while (uio->uio_resid && error == 0 && m);
42410137Ssam bad:
4258319Sroot 		if (m)
4268771Sroot 			m_freem(m);
4278594Sroot 		return (error);
4288319Sroot 	}
4298319Sroot 
4304890Swnj restart:
4314890Swnj 	sblock(&so->so_rcv);
4328835Sroot 	s = splnet();
4334890Swnj 
4344890Swnj #define	rcverr(errno)	{ error = errno; splx(s); goto release; }
4354786Swnj 	if (so->so_rcv.sb_cc == 0) {
4365168Swnj 		if (so->so_error) {
4375168Swnj 			error = so->so_error;
4385168Swnj 			so->so_error = 0;
4395168Swnj 			splx(s);
4405168Swnj 			goto release;
4415168Swnj 		}
4424890Swnj 		if (so->so_state & SS_CANTRCVMORE) {
4434890Swnj 			splx(s);
4444890Swnj 			goto release;
4454890Swnj 		}
4465015Sroot 		if ((so->so_state & SS_ISCONNECTED) == 0 &&
4475015Sroot 		    (so->so_proto->pr_flags & PR_CONNREQUIRED))
4485015Sroot 			rcverr(ENOTCONN);
4496214Swnj 		if (so->so_state & SS_NBIO)
4505168Swnj 			rcverr(EWOULDBLOCK);
4514890Swnj 		sbunlock(&so->so_rcv);
4524971Swnj 		sbwait(&so->so_rcv);
4535012Swnj 		splx(s);
4544890Swnj 		goto restart;
4554786Swnj 	}
4568041Sroot 	u.u_ru.ru_msgrcv++;
4574829Swnj 	m = so->so_rcv.sb_mb;
4584786Swnj 	if (m == 0)
4594786Swnj 		panic("receive");
46012757Ssam 	if (pr->pr_flags & PR_ADDR) {
46112757Ssam 		if ((flags & MSG_PEEK) == 0) {
4628319Sroot 			so->so_rcv.sb_cc -= m->m_len;
4638319Sroot 			so->so_rcv.sb_mbcnt -= MSIZE;
4648319Sroot 		}
4658300Sroot 		if (aname) {
46612757Ssam 			if (flags & MSG_PEEK) {
4678319Sroot 				*aname = m_copy(m, 0, m->m_len);
46810137Ssam 				if (*aname == NULL)
46910137Ssam 					panic("receive 2");
47010137Ssam 			} else
4718319Sroot 				*aname = m;
4728300Sroot 			m = m->m_next;
4738300Sroot 			(*aname)->m_next = 0;
4748300Sroot 		} else
47512757Ssam 			if (flags & MSG_PEEK)
4768319Sroot 				m = m->m_next;
4778319Sroot 			else
4788319Sroot 				m = m_free(m);
4794890Swnj 		if (m == 0)
48012757Ssam 			panic("receive 2a");
48112757Ssam 		if (rightsp) {
48212757Ssam 			if (m->m_len)
48312757Ssam 				*rightsp = m_copy(m, 0, m->m_len);
48412757Ssam 			else {
48512757Ssam 				*rightsp = m_get(M_DONTWAIT, MT_SONAME);
48612757Ssam 				if (*rightsp)
48712757Ssam 					(*rightsp)->m_len = 0;
48812757Ssam 			}
489*14813Ssam #ifdef notdef
49012757Ssam 			if (*rightsp == NULL)
49112757Ssam 				panic("receive 2b");
492*14813Ssam #endif
49312757Ssam 		}
49412757Ssam 		if (flags & MSG_PEEK)
49512757Ssam 			m = m->m_next;
49612757Ssam 		else {
49712757Ssam 			so->so_rcv.sb_cc -= m->m_len;
49812757Ssam 			so->so_rcv.sb_mbcnt -= MSIZE;
49912757Ssam 			m = m_free(m);
50012757Ssam 		}
50112757Ssam 		if (m == 0)
50210137Ssam 			panic("receive 3");
50312757Ssam 		if ((flags & MSG_PEEK) == 0)
5048548Sroot 			so->so_rcv.sb_mb = m;
5054890Swnj 	}
5064786Swnj 	eor = 0;
5078319Sroot 	moff = 0;
5088319Sroot 	tomark = so->so_oobmark;
5094786Swnj 	do {
5107827Sroot 		if (uio->uio_resid <= 0)
5117747Sroot 			break;
5127827Sroot 		len = uio->uio_resid;
5137747Sroot 		so->so_state &= ~SS_RCVATMARK;
5148319Sroot 		if (tomark && len > tomark)
5158319Sroot 			len = tomark;
5168548Sroot 		if (moff+len > m->m_len - moff)
5178319Sroot 			len = m->m_len - moff;
5184786Swnj 		splx(s);
5198594Sroot 		error =
5208793Sroot 		    uiomove(mtod(m, caddr_t) + moff, (int)len, UIO_READ, uio);
5214786Swnj 		s = splnet();
5224786Swnj 		if (len == m->m_len) {
5236091Sroot 			eor = (int)m->m_act;
52412757Ssam 			if (flags & MSG_PEEK)
5258319Sroot 				m = m->m_next;
5268319Sroot 			else {
5278319Sroot 				sbfree(&so->so_rcv, m);
5288319Sroot 				MFREE(m, n);
5298319Sroot 				m = n;
5308548Sroot 				so->so_rcv.sb_mb = m;
5318319Sroot 			}
5328319Sroot 			moff = 0;
5334786Swnj 		} else {
53412757Ssam 			if (flags & MSG_PEEK)
5358319Sroot 				moff += len;
5368319Sroot 			else {
5378319Sroot 				m->m_off += len;
5388319Sroot 				m->m_len -= len;
5398319Sroot 				so->so_rcv.sb_cc -= len;
5408319Sroot 			}
5414786Swnj 		}
54212757Ssam 		if ((flags & MSG_PEEK) == 0 && so->so_oobmark) {
5437747Sroot 			so->so_oobmark -= len;
5447747Sroot 			if (so->so_oobmark == 0) {
5457747Sroot 				so->so_state |= SS_RCVATMARK;
5467747Sroot 				break;
5477747Sroot 			}
5487747Sroot 		}
5498319Sroot 		if (tomark) {
5508319Sroot 			tomark -= len;
5518319Sroot 			if (tomark == 0)
5528319Sroot 				break;
5538319Sroot 		}
5548594Sroot 	} while (m && error == 0 && !eor);
55512757Ssam 	if (flags & MSG_PEEK)
5568319Sroot 		goto release;
5574786Swnj 	if ((so->so_proto->pr_flags & PR_ATOMIC) && eor == 0)
5584786Swnj 		do {
5594786Swnj 			if (m == 0)
56010137Ssam 				panic("receive 4");
5614890Swnj 			sbfree(&so->so_rcv, m);
5624786Swnj 			eor = (int)m->m_act;
5634786Swnj 			so->so_rcv.sb_mb = m->m_next;
5644786Swnj 			MFREE(m, n);
5654890Swnj 			m = n;
5664786Swnj 		} while (eor == 0);
5674890Swnj 	if ((so->so_proto->pr_flags & PR_WANTRCVD) && so->so_pcb)
5688300Sroot 		(*so->so_proto->pr_usrreq)(so, PRU_RCVD,
56912757Ssam 		    (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0);
5704890Swnj release:
5714916Swnj 	sbunlock(&so->so_rcv);
57212757Ssam 	if (error == 0 && rightsp &&
57312757Ssam 	    *rightsp && so->so_proto->pr_family == AF_UNIX)
57412757Ssam 		error = unp_externalize(*rightsp);
5754890Swnj 	splx(s);
5764916Swnj 	return (error);
5774786Swnj }
5784786Swnj 
57910267Ssam soshutdown(so, how)
58012757Ssam 	register struct socket *so;
58112757Ssam 	register int how;
58210267Ssam {
58312757Ssam 	register struct protosw *pr = so->so_proto;
58410267Ssam 
58510267Ssam 	how++;
58612757Ssam 	if (how & FREAD)
58712757Ssam 		sorflush(so);
58810267Ssam 	if (how & FWRITE)
58912757Ssam 		return ((*pr->pr_usrreq)(so, PRU_SHUTDOWN,
59012757Ssam 		    (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0));
59110267Ssam 	return (0);
59210267Ssam }
59310267Ssam 
59412757Ssam sorflush(so)
59512757Ssam 	register struct socket *so;
59612757Ssam {
59712757Ssam 	register struct sockbuf *sb = &so->so_rcv;
59812757Ssam 	register struct protosw *pr = so->so_proto;
59912757Ssam 	register int s;
60012757Ssam 	struct sockbuf asb;
60112757Ssam 
60212757Ssam 	sblock(sb);
60312757Ssam 	s = splimp();
60412757Ssam 	socantrcvmore(so);
60512757Ssam 	sbunlock(sb);
60612757Ssam 	asb = *sb;
60712757Ssam 	bzero((caddr_t)sb, sizeof (*sb));
60812757Ssam 	splx(s);
60912757Ssam 	if (pr->pr_family == AF_UNIX && (pr->pr_flags & PR_RIGHTS))
61012757Ssam 		unp_scan(asb.sb_mb, unp_discard);
61112757Ssam 	sbrelease(&asb);
61212757Ssam }
61312757Ssam 
61410267Ssam sosetopt(so, level, optname, m)
61512757Ssam 	register struct socket *so;
61610267Ssam 	int level, optname;
61712757Ssam 	register struct mbuf *m;
61810267Ssam {
61910267Ssam 
62010267Ssam 	if (level != SOL_SOCKET)
62110267Ssam 		return (EINVAL);	/* XXX */
62210267Ssam 	switch (optname) {
62310267Ssam 
62410267Ssam 	case SO_DEBUG:
62510269Ssam 	case SO_KEEPALIVE:
62610599Ssam 	case SO_DONTROUTE:
62710599Ssam 	case SO_USELOOPBACK:
62810599Ssam 	case SO_REUSEADDR:
62910599Ssam 		so->so_options |= optname;
63010269Ssam 		break;
63110269Ssam 
63210267Ssam 	case SO_LINGER:
63310269Ssam 		if (m == NULL || m->m_len != sizeof (int))
63410269Ssam 			return (EINVAL);
63510267Ssam 		so->so_options |= SO_LINGER;
63610269Ssam 		so->so_linger = *mtod(m, int *);
63710267Ssam 		break;
63810267Ssam 
63910267Ssam 	case SO_DONTLINGER:
64010267Ssam 		so->so_options &= ~SO_LINGER;
64110267Ssam 		so->so_linger = 0;
64210267Ssam 		break;
64310267Ssam 
64410267Ssam 	default:
64510267Ssam 		return (EINVAL);
64610267Ssam 	}
64710267Ssam 	return (0);
64810267Ssam }
64910267Ssam 
65010267Ssam sogetopt(so, level, optname, m)
65112757Ssam 	register struct socket *so;
65210267Ssam 	int level, optname;
65312757Ssam 	register struct mbuf *m;
65410267Ssam {
65510267Ssam 
65610267Ssam 	if (level != SOL_SOCKET)
65710267Ssam 		return (EINVAL);	/* XXX */
65810267Ssam 	switch (optname) {
65910267Ssam 
66010267Ssam 	case SO_USELOOPBACK:
66110267Ssam 	case SO_DONTROUTE:
66210267Ssam 	case SO_DEBUG:
66310267Ssam 	case SO_KEEPALIVE:
66410267Ssam 	case SO_LINGER:
66510599Ssam 	case SO_REUSEADDR:
66610267Ssam 		if ((so->so_options & optname) == 0)
66710267Ssam 			return (ENOPROTOOPT);
66810269Ssam 		if (optname == SO_LINGER && m != NULL) {
66910267Ssam 			*mtod(m, int *) = so->so_linger;
67010267Ssam 			m->m_len = sizeof (so->so_linger);
67110267Ssam 		}
67210267Ssam 		break;
67310267Ssam 
67410267Ssam 	default:
67510267Ssam 		return (EINVAL);
67610267Ssam 	}
67710267Ssam 	return (0);
67810267Ssam }
67910267Ssam 
6805423Swnj sohasoutofband(so)
68112757Ssam 	register struct socket *so;
6825423Swnj {
6835423Swnj 
6845423Swnj 	if (so->so_pgrp == 0)
6855423Swnj 		return;
6865423Swnj 	if (so->so_pgrp > 0)
6875423Swnj 		gsignal(so->so_pgrp, SIGURG);
6885429Swnj 	else {
6895429Swnj 		struct proc *p = pfind(-so->so_pgrp);
6905429Swnj 
6915429Swnj 		if (p)
6925429Swnj 			psignal(p, SIGURG);
6935429Swnj 	}
6945423Swnj }
695