xref: /csrg-svn/sys/kern/uipc_socket.c (revision 25502)
123421Smckusick /*
223421Smckusick  * Copyright (c) 1982 Regents of the University of California.
323421Smckusick  * All rights reserved.  The Berkeley software License Agreement
423421Smckusick  * specifies the terms and conditions for redistribution.
523421Smckusick  *
6*25502Skarels  *	@(#)uipc_socket.c	6.18 (Berkeley) 11/19/85
723421Smckusick  */
84786Swnj 
917102Sbloom #include "param.h"
1017102Sbloom #include "systm.h"
1117102Sbloom #include "dir.h"
1217102Sbloom #include "user.h"
1317102Sbloom #include "proc.h"
1417102Sbloom #include "file.h"
1517102Sbloom #include "inode.h"
1617102Sbloom #include "buf.h"
1717102Sbloom #include "mbuf.h"
1817102Sbloom #include "un.h"
1917102Sbloom #include "domain.h"
2017102Sbloom #include "protosw.h"
2117102Sbloom #include "socket.h"
2217102Sbloom #include "socketvar.h"
2317102Sbloom #include "stat.h"
2417102Sbloom #include "ioctl.h"
2517102Sbloom #include "uio.h"
266355Ssam #include "../net/route.h"
2712757Ssam #include "../netinet/in.h"
2811571Ssam #include "../net/if.h"
294786Swnj 
304786Swnj /*
318300Sroot  * Socket operation routines.
328300Sroot  * These routines are called by the routines in
338300Sroot  * sys_socket.c or from a system process, and
348300Sroot  * implement the semantics of socket operations by
358300Sroot  * switching out to the protocol specific routines.
3612757Ssam  *
3712757Ssam  * TODO:
3812757Ssam  *	test socketpair
3921767Skarels  *	clean up async
4012757Ssam  *	out-of-band is a kludge
414786Swnj  */
428594Sroot /*ARGSUSED*/
4310267Ssam socreate(dom, aso, type, proto)
444786Swnj 	struct socket **aso;
4512757Ssam 	register int type;
4612757Ssam 	int proto;
474786Swnj {
484786Swnj 	register struct protosw *prp;
494786Swnj 	register struct socket *so;
5012757Ssam 	register struct mbuf *m;
5112757Ssam 	register int error;
524786Swnj 
534890Swnj 	if (proto)
5421767Skarels 		prp = pffindproto(dom, proto, type);
554890Swnj 	else
569168Ssam 		prp = pffindtype(dom, type);
574890Swnj 	if (prp == 0)
584890Swnj 		return (EPROTONOSUPPORT);
598300Sroot 	if (prp->pr_type != type)
608300Sroot 		return (EPROTOTYPE);
619635Ssam 	m = m_getclr(M_WAIT, MT_SOCKET);
624786Swnj 	so = mtod(m, struct socket *);
6312757Ssam 	so->so_options = 0;
646214Swnj 	so->so_state = 0;
659168Ssam 	so->so_type = type;
666214Swnj 	if (u.u_uid == 0)
676214Swnj 		so->so_state = SS_PRIV;
684786Swnj 	so->so_proto = prp;
6912757Ssam 	error =
7012757Ssam 	    (*prp->pr_usrreq)(so, PRU_ATTACH,
7121767Skarels 		(struct mbuf *)0, (struct mbuf *)proto, (struct mbuf *)0);
724979Swnj 	if (error) {
737507Sroot 		so->so_state |= SS_NOFDREF;
747180Swnj 		sofree(so);
754890Swnj 		return (error);
764786Swnj 	}
774786Swnj 	*aso = so;
784786Swnj 	return (0);
794786Swnj }
804786Swnj 
8110267Ssam sobind(so, nam)
828300Sroot 	struct socket *so;
838300Sroot 	struct mbuf *nam;
848300Sroot {
858300Sroot 	int s = splnet();
868300Sroot 	int error;
878300Sroot 
888300Sroot 	error =
8912757Ssam 	    (*so->so_proto->pr_usrreq)(so, PRU_BIND,
9012757Ssam 		(struct mbuf *)0, nam, (struct mbuf *)0);
918300Sroot 	splx(s);
928300Sroot 	return (error);
938300Sroot }
948300Sroot 
958300Sroot solisten(so, backlog)
9612757Ssam 	register struct socket *so;
978300Sroot 	int backlog;
988300Sroot {
9912757Ssam 	int s = splnet(), error;
1008300Sroot 
10112757Ssam 	error =
10212757Ssam 	    (*so->so_proto->pr_usrreq)(so, PRU_LISTEN,
10312757Ssam 		(struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0);
1048300Sroot 	if (error) {
1058300Sroot 		splx(s);
1068300Sroot 		return (error);
1078300Sroot 	}
1088300Sroot 	if (so->so_q == 0) {
1098300Sroot 		so->so_q = so;
1108300Sroot 		so->so_q0 = so;
1118300Sroot 		so->so_options |= SO_ACCEPTCONN;
1128300Sroot 	}
1138300Sroot 	if (backlog < 0)
1148300Sroot 		backlog = 0;
11510137Ssam 	so->so_qlimit = MIN(backlog, SOMAXCONN);
11612493Ssam 	splx(s);
1178300Sroot 	return (0);
1188300Sroot }
1198300Sroot 
1204916Swnj sofree(so)
12112757Ssam 	register struct socket *so;
1224916Swnj {
1234916Swnj 
1247507Sroot 	if (so->so_head) {
1257507Sroot 		if (!soqremque(so, 0) && !soqremque(so, 1))
1267507Sroot 			panic("sofree dq");
1277507Sroot 		so->so_head = 0;
1287507Sroot 	}
1297507Sroot 	if (so->so_pcb || (so->so_state & SS_NOFDREF) == 0)
1304950Swnj 		return;
1314950Swnj 	sbrelease(&so->so_snd);
13212757Ssam 	sorflush(so);
1334971Swnj 	(void) m_free(dtom(so));
1344916Swnj }
1354916Swnj 
1364786Swnj /*
1374890Swnj  * Close a socket on last file table reference removal.
1384890Swnj  * Initiate disconnect if connected.
1394890Swnj  * Free socket when disconnect complete.
1404829Swnj  */
14112757Ssam soclose(so)
1424829Swnj 	register struct socket *so;
1434829Swnj {
1444890Swnj 	int s = splnet();		/* conservative */
1458713Sroot 	int error;
1464829Swnj 
1477507Sroot 	if (so->so_options & SO_ACCEPTCONN) {
1487507Sroot 		while (so->so_q0 != so)
14910399Ssam 			(void) soabort(so->so_q0);
1507507Sroot 		while (so->so_q != so)
15110399Ssam 			(void) soabort(so->so_q);
1527507Sroot 	}
1534890Swnj 	if (so->so_pcb == 0)
1544890Swnj 		goto discard;
1554890Swnj 	if (so->so_state & SS_ISCONNECTED) {
1564890Swnj 		if ((so->so_state & SS_ISDISCONNECTING) == 0) {
1578725Sroot 			error = sodisconnect(so, (struct mbuf *)0);
15812757Ssam 			if (error)
15912757Ssam 				goto drop;
1604890Swnj 		}
16110267Ssam 		if (so->so_options & SO_LINGER) {
1625281Sroot 			if ((so->so_state & SS_ISDISCONNECTING) &&
16312757Ssam 			    (so->so_state & SS_NBIO))
16412757Ssam 				goto drop;
1655281Sroot 			while (so->so_state & SS_ISCONNECTED)
1665281Sroot 				sleep((caddr_t)&so->so_timeo, PZERO+1);
1674890Swnj 		}
1684890Swnj 	}
1695580Sroot drop:
1706880Ssam 	if (so->so_pcb) {
17112757Ssam 		int error2 =
17212757Ssam 		    (*so->so_proto->pr_usrreq)(so, PRU_DETACH,
17312757Ssam 			(struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0);
17412757Ssam 		if (error == 0)
17512757Ssam 			error = error2;
1766880Ssam 	}
1774890Swnj discard:
17810399Ssam 	if (so->so_state & SS_NOFDREF)
17910399Ssam 		panic("soclose: NOFDREF");
1807507Sroot 	so->so_state |= SS_NOFDREF;
1814950Swnj 	sofree(so);
1824890Swnj 	splx(s);
18312757Ssam 	return (error);
1844829Swnj }
1854829Swnj 
18610399Ssam /*
18710399Ssam  * Must be called at splnet...
18810399Ssam  */
18910399Ssam soabort(so)
19010399Ssam 	struct socket *so;
19110399Ssam {
19210399Ssam 
19312757Ssam 	return (
19412757Ssam 	    (*so->so_proto->pr_usrreq)(so, PRU_ABORT,
19512757Ssam 		(struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0));
19610399Ssam }
19710399Ssam 
19810267Ssam soaccept(so, nam)
19912757Ssam 	register struct socket *so;
2008300Sroot 	struct mbuf *nam;
2014927Swnj {
2024927Swnj 	int s = splnet();
2034927Swnj 	int error;
2044927Swnj 
20510399Ssam 	if ((so->so_state & SS_NOFDREF) == 0)
20610399Ssam 		panic("soaccept: !NOFDREF");
20710267Ssam 	so->so_state &= ~SS_NOFDREF;
2088300Sroot 	error = (*so->so_proto->pr_usrreq)(so, PRU_ACCEPT,
20912757Ssam 	    (struct mbuf *)0, nam, (struct mbuf *)0);
2104927Swnj 	splx(s);
2114927Swnj 	return (error);
2124927Swnj }
2134927Swnj 
21410267Ssam soconnect(so, nam)
21512757Ssam 	register struct socket *so;
2168300Sroot 	struct mbuf *nam;
2174786Swnj {
2184890Swnj 	int s = splnet();
2194890Swnj 	int error;
2204786Swnj 
22124768Skarels 	/*
22224768Skarels 	 * If protocol is connection-based, can only connect once.
22324768Skarels 	 * Otherwise, if connected, try to disconnect first.
22424768Skarels 	 * This allows user to disconnect by connecting to, e.g.,
22524768Skarels 	 * a null address.
22624768Skarels 	 */
22724768Skarels 	if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
22824768Skarels 	    ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
22924768Skarels 	    (error = sodisconnect(so))))
2304890Swnj 		error = EISCONN;
23124768Skarels 	else
23224768Skarels 		error = (*so->so_proto->pr_usrreq)(so, PRU_CONNECT,
23324768Skarels 		    (struct mbuf *)0, nam, (struct mbuf *)0);
2344890Swnj 	splx(s);
2354890Swnj 	return (error);
2364786Swnj }
2374786Swnj 
23812757Ssam soconnect2(so1, so2)
23912757Ssam 	register struct socket *so1;
24012757Ssam 	struct socket *so2;
24112757Ssam {
24212757Ssam 	int s = splnet();
24312757Ssam 	int error;
24412757Ssam 
24513113Ssam 	error = (*so1->so_proto->pr_usrreq)(so1, PRU_CONNECT2,
24613113Ssam 	    (struct mbuf *)0, (struct mbuf *)so2, (struct mbuf *)0);
24712757Ssam 	splx(s);
24812757Ssam 	return (error);
24912757Ssam }
25012757Ssam 
2518300Sroot sodisconnect(so, nam)
25212757Ssam 	register struct socket *so;
2538300Sroot 	struct mbuf *nam;
2544786Swnj {
2554890Swnj 	int s = splnet();
2564890Swnj 	int error;
2574786Swnj 
2584890Swnj 	if ((so->so_state & SS_ISCONNECTED) == 0) {
2594890Swnj 		error = ENOTCONN;
2604890Swnj 		goto bad;
2614890Swnj 	}
2624890Swnj 	if (so->so_state & SS_ISDISCONNECTING) {
2634890Swnj 		error = EALREADY;
2644890Swnj 		goto bad;
2654890Swnj 	}
2668300Sroot 	error = (*so->so_proto->pr_usrreq)(so, PRU_DISCONNECT,
26712757Ssam 	    (struct mbuf *)0, nam, (struct mbuf *)0);
2684890Swnj bad:
2694890Swnj 	splx(s);
2704890Swnj 	return (error);
2714786Swnj }
2724786Swnj 
2734786Swnj /*
2744890Swnj  * Send on a socket.
2754890Swnj  * If send must go all at once and message is larger than
2764890Swnj  * send buffering, then hard error.
2774890Swnj  * Lock against other senders.
2784890Swnj  * If must go all at once and not enough room now, then
2794890Swnj  * inform user that this would block and do nothing.
28016412Skarels  * Otherwise, if nonblocking, send as much as possible.
2814786Swnj  */
28212757Ssam sosend(so, nam, uio, flags, rights)
2834786Swnj 	register struct socket *so;
2848300Sroot 	struct mbuf *nam;
28512757Ssam 	register struct uio *uio;
2868319Sroot 	int flags;
28712757Ssam 	struct mbuf *rights;
2884786Swnj {
2894890Swnj 	struct mbuf *top = 0;
29016412Skarels 	register struct mbuf *m, **mp;
29112757Ssam 	register int space;
29216412Skarels 	int len, error = 0, s, dontroute, first = 1;
2934786Swnj 
2947827Sroot 	if (sosendallatonce(so) && uio->uio_resid > so->so_snd.sb_hiwat)
2954890Swnj 		return (EMSGSIZE);
29612757Ssam 	dontroute =
29712757Ssam 	    (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&
29812757Ssam 	    (so->so_proto->pr_flags & PR_ATOMIC);
29916412Skarels 	u.u_ru.ru_msgsnd++;
30016412Skarels #define	snderr(errno)	{ error = errno; splx(s); goto release; }
30116412Skarels 
3026419Sroot restart:
3034890Swnj 	sblock(&so->so_snd);
30416412Skarels 	do {
30516412Skarels 		s = splnet();
30621108Skarels 		if (so->so_state & SS_CANTSENDMORE)
30716412Skarels 			snderr(EPIPE);
30816412Skarels 		if (so->so_error) {
30916412Skarels 			error = so->so_error;
31016412Skarels 			so->so_error = 0;			/* ??? */
31116412Skarels 			splx(s);
31216412Skarels 			goto release;
31316412Skarels 		}
31416412Skarels 		if ((so->so_state & SS_ISCONNECTED) == 0) {
31516412Skarels 			if (so->so_proto->pr_flags & PR_CONNREQUIRED)
31616412Skarels 				snderr(ENOTCONN);
31716412Skarels 			if (nam == 0)
31816412Skarels 				snderr(EDESTADDRREQ);
31916412Skarels 		}
32016412Skarels 		if (flags & MSG_OOB)
32116412Skarels 			space = 1024;
32216412Skarels 		else {
32316412Skarels 			space = sbspace(&so->so_snd);
32416412Skarels 			if (space <= 0 ||
32516992Skarels 			   (sosendallatonce(so) && space < uio->uio_resid) ||
32616992Skarels 			   (uio->uio_resid >= CLBYTES && space < CLBYTES &&
32716992Skarels 			   so->so_snd.sb_cc >= CLBYTES &&
32816992Skarels 			   (so->so_state & SS_NBIO) == 0)) {
32916412Skarels 				if (so->so_state & SS_NBIO) {
33016412Skarels 					if (first)
33116412Skarels 						error = EWOULDBLOCK;
33216412Skarels 					splx(s);
33316412Skarels 					goto release;
33416412Skarels 				}
33516412Skarels 				sbunlock(&so->so_snd);
33616412Skarels 				sbwait(&so->so_snd);
33716412Skarels 				splx(s);
33816412Skarels 				goto restart;
33916412Skarels 			}
34016412Skarels 		}
34116412Skarels 		splx(s);
34216412Skarels 		mp = &top;
34321108Skarels 		while (space > 0) {
34416412Skarels 			register struct iovec *iov = uio->uio_iov;
3454890Swnj 
34616412Skarels 			MGET(m, M_WAIT, MT_DATA);
34721767Skarels 			if (iov->iov_len >= NBPG && space >= CLBYTES) {
34816412Skarels 				register struct mbuf *p;
34916412Skarels 				MCLGET(p, 1);
35016412Skarels 				if (p == 0)
35116412Skarels 					goto nopages;
35216412Skarels 				m->m_off = (int)p - (int)m;
35324518Skarels 				len = MIN(CLBYTES, iov->iov_len);
35421767Skarels 				space -= CLBYTES;
35516412Skarels 			} else {
35616412Skarels nopages:
35716412Skarels 				len = MIN(MLEN, iov->iov_len);
35821767Skarels 				space -= len;
35916412Skarels 			}
36016412Skarels 			error = uiomove(mtod(m, caddr_t), len, UIO_WRITE, uio);
36116412Skarels 			m->m_len = len;
36216412Skarels 			*mp = m;
36316412Skarels 			if (error)
36416412Skarels 				goto release;
36516412Skarels 			mp = &m->m_next;
36621108Skarels 			if (uio->uio_resid <= 0)
36721108Skarels 				break;
36821108Skarels 			while (uio->uio_iov->iov_len == 0) {
36921108Skarels 				uio->uio_iov++;
37021108Skarels 				uio->uio_iovcnt--;
37121108Skarels 				if (uio->uio_iovcnt <= 0)
37221108Skarels 					panic("sosend");
37321108Skarels 			}
37416412Skarels 		}
37521108Skarels 		if (dontroute)
37621108Skarels 			so->so_options |= SO_DONTROUTE;
37721108Skarels 		s = splnet();					/* XXX */
37821108Skarels 		error = (*so->so_proto->pr_usrreq)(so,
37921108Skarels 		    (flags & MSG_OOB) ? PRU_SENDOOB : PRU_SEND,
38021108Skarels 		    top, (caddr_t)nam, rights);
38121108Skarels 		splx(s);
38221108Skarels 		if (dontroute)
38321108Skarels 			so->so_options &= ~SO_DONTROUTE;
38421767Skarels 		rights = 0;
3856419Sroot 		top = 0;
38616412Skarels 		first = 0;
38714781Ssam 		if (error)
38816412Skarels 			break;
38916412Skarels 	} while (uio->uio_resid);
3904890Swnj 
3914786Swnj release:
3924890Swnj 	sbunlock(&so->so_snd);
3936419Sroot 	if (top)
3946419Sroot 		m_freem(top);
39521108Skarels 	if (error == EPIPE)
39621108Skarels 		psignal(u.u_procp, SIGPIPE);
3974786Swnj 	return (error);
3984786Swnj }
3994786Swnj 
40012757Ssam soreceive(so, aname, uio, flags, rightsp)
4014786Swnj 	register struct socket *so;
4028300Sroot 	struct mbuf **aname;
40312757Ssam 	register struct uio *uio;
4048319Sroot 	int flags;
40512757Ssam 	struct mbuf **rightsp;
4064786Swnj {
4074786Swnj 	register struct mbuf *m, *n;
40816993Skarels 	register int len, error = 0, s, tomark;
40912757Ssam 	struct protosw *pr = so->so_proto;
41016993Skarels 	struct mbuf *nextrecord;
41112757Ssam 	int moff;
4124786Swnj 
41312757Ssam 	if (rightsp)
41412757Ssam 		*rightsp = 0;
41512757Ssam 	if (aname)
41612757Ssam 		*aname = 0;
41712757Ssam 	if (flags & MSG_OOB) {
4189635Ssam 		m = m_get(M_WAIT, MT_DATA);
41912757Ssam 		error = (*pr->pr_usrreq)(so, PRU_RCVOOB,
42024768Skarels 		    m, (struct mbuf *)(flags & MSG_PEEK), (struct mbuf *)0);
4218594Sroot 		if (error)
42210137Ssam 			goto bad;
4238319Sroot 		do {
42410137Ssam 			len = uio->uio_resid;
4258319Sroot 			if (len > m->m_len)
4268319Sroot 				len = m->m_len;
4278594Sroot 			error =
4288793Sroot 			    uiomove(mtod(m, caddr_t), (int)len, UIO_READ, uio);
4298319Sroot 			m = m_free(m);
4308594Sroot 		} while (uio->uio_resid && error == 0 && m);
43110137Ssam bad:
4328319Sroot 		if (m)
4338771Sroot 			m_freem(m);
4348594Sroot 		return (error);
4358319Sroot 	}
4368319Sroot 
4374890Swnj restart:
4384890Swnj 	sblock(&so->so_rcv);
4398835Sroot 	s = splnet();
4404890Swnj 
4414890Swnj #define	rcverr(errno)	{ error = errno; splx(s); goto release; }
4424786Swnj 	if (so->so_rcv.sb_cc == 0) {
4435168Swnj 		if (so->so_error) {
4445168Swnj 			error = so->so_error;
4455168Swnj 			so->so_error = 0;
4465168Swnj 			splx(s);
4475168Swnj 			goto release;
4485168Swnj 		}
4494890Swnj 		if (so->so_state & SS_CANTRCVMORE) {
4504890Swnj 			splx(s);
4514890Swnj 			goto release;
4524890Swnj 		}
4535015Sroot 		if ((so->so_state & SS_ISCONNECTED) == 0 &&
4545015Sroot 		    (so->so_proto->pr_flags & PR_CONNREQUIRED))
4555015Sroot 			rcverr(ENOTCONN);
4566214Swnj 		if (so->so_state & SS_NBIO)
4575168Swnj 			rcverr(EWOULDBLOCK);
4584890Swnj 		sbunlock(&so->so_rcv);
4594971Swnj 		sbwait(&so->so_rcv);
4605012Swnj 		splx(s);
4614890Swnj 		goto restart;
4624786Swnj 	}
4638041Sroot 	u.u_ru.ru_msgrcv++;
4644829Swnj 	m = so->so_rcv.sb_mb;
46512757Ssam 	if (pr->pr_flags & PR_ADDR) {
46616993Skarels 		if (m == 0 || m->m_type != MT_SONAME)
46716993Skarels 			panic("receive 1a");
46816993Skarels 		if (flags & MSG_PEEK) {
46916993Skarels 			if (aname)
4708319Sroot 				*aname = m_copy(m, 0, m->m_len);
47116993Skarels 			else
47216993Skarels 				m = m->m_act;
47316993Skarels 		} else {
47416993Skarels 			if (aname) {
47516993Skarels 				*aname = m;
47616993Skarels 				sbfree(&so->so_rcv, m);
47716993Skarels if(m->m_next) panic("receive 1b");
47816993Skarels 				so->so_rcv.sb_mb = m = m->m_act;
47910137Ssam 			} else
48016993Skarels 				m = sbdroprecord(&so->so_rcv);
48116993Skarels 		}
48216993Skarels 	}
48316993Skarels 	if (m && m->m_type == MT_RIGHTS) {
48416993Skarels 		if ((pr->pr_flags & PR_RIGHTS) == 0)
48512757Ssam 			panic("receive 2a");
48616993Skarels 		if (flags & MSG_PEEK) {
48716993Skarels 			if (rightsp)
48812757Ssam 				*rightsp = m_copy(m, 0, m->m_len);
48916993Skarels 			else
49016993Skarels 				m = m->m_act;
49116993Skarels 		} else {
49216993Skarels 			if (rightsp) {
49316993Skarels 				*rightsp = m;
49416993Skarels 				sbfree(&so->so_rcv, m);
49516993Skarels if(m->m_next) panic("receive 2b");
49616993Skarels 				so->so_rcv.sb_mb = m = m->m_act;
49716993Skarels 			} else
49816993Skarels 				m = sbdroprecord(&so->so_rcv);
49912757Ssam 		}
5004890Swnj 	}
50121783Skarels 	if (m == 0 || (m->m_type != MT_DATA && m->m_type != MT_HEADER))
50216993Skarels 		panic("receive 3");
5038319Sroot 	moff = 0;
5048319Sroot 	tomark = so->so_oobmark;
50516993Skarels 	while (m && uio->uio_resid > 0 && error == 0) {
5067827Sroot 		len = uio->uio_resid;
5077747Sroot 		so->so_state &= ~SS_RCVATMARK;
5088319Sroot 		if (tomark && len > tomark)
5098319Sroot 			len = tomark;
51021767Skarels 		if (len > m->m_len - moff)
5118319Sroot 			len = m->m_len - moff;
5124786Swnj 		splx(s);
5138594Sroot 		error =
5148793Sroot 		    uiomove(mtod(m, caddr_t) + moff, (int)len, UIO_READ, uio);
5154786Swnj 		s = splnet();
51621767Skarels 		if (len == m->m_len - moff) {
51716993Skarels 			if ((flags & MSG_PEEK) == 0) {
51816993Skarels 				nextrecord = m->m_act;
5198319Sroot 				sbfree(&so->so_rcv, m);
5208319Sroot 				MFREE(m, n);
52116993Skarels 				if (m = n)
52216993Skarels 					m->m_act = nextrecord;
5238548Sroot 				so->so_rcv.sb_mb = m;
52416993Skarels 			} else
52516993Skarels 				m = m->m_next;
5268319Sroot 			moff = 0;
5274786Swnj 		} else {
52812757Ssam 			if (flags & MSG_PEEK)
5298319Sroot 				moff += len;
5308319Sroot 			else {
5318319Sroot 				m->m_off += len;
5328319Sroot 				m->m_len -= len;
5338319Sroot 				so->so_rcv.sb_cc -= len;
5348319Sroot 			}
5354786Swnj 		}
53612757Ssam 		if ((flags & MSG_PEEK) == 0 && so->so_oobmark) {
5377747Sroot 			so->so_oobmark -= len;
5387747Sroot 			if (so->so_oobmark == 0) {
5397747Sroot 				so->so_state |= SS_RCVATMARK;
5407747Sroot 				break;
5417747Sroot 			}
5427747Sroot 		}
5438319Sroot 		if (tomark) {
5448319Sroot 			tomark -= len;
5458319Sroot 			if (tomark == 0)
5468319Sroot 				break;
5478319Sroot 		}
54816993Skarels 	}
54916993Skarels 	if ((flags & MSG_PEEK) == 0) {
55016993Skarels 		if (m == 0)
55116993Skarels 			so->so_rcv.sb_mb = nextrecord;
55216993Skarels 		else if (pr->pr_flags & PR_ATOMIC)
55316993Skarels 			(void) sbdroprecord(&so->so_rcv);
55416993Skarels 		if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
55516993Skarels 			(*pr->pr_usrreq)(so, PRU_RCVD, (struct mbuf *)0,
55616993Skarels 			    (struct mbuf *)0, (struct mbuf *)0);
55716993Skarels 	}
5584890Swnj release:
5594916Swnj 	sbunlock(&so->so_rcv);
56016993Skarels 	if (error == 0 && rightsp && *rightsp && pr->pr_domain->dom_externalize)
56116993Skarels 		error = (*pr->pr_domain->dom_externalize)(*rightsp);
5624890Swnj 	splx(s);
5634916Swnj 	return (error);
5644786Swnj }
5654786Swnj 
56610267Ssam soshutdown(so, how)
56712757Ssam 	register struct socket *so;
56812757Ssam 	register int how;
56910267Ssam {
57012757Ssam 	register struct protosw *pr = so->so_proto;
57110267Ssam 
57210267Ssam 	how++;
57312757Ssam 	if (how & FREAD)
57412757Ssam 		sorflush(so);
57510267Ssam 	if (how & FWRITE)
57612757Ssam 		return ((*pr->pr_usrreq)(so, PRU_SHUTDOWN,
57712757Ssam 		    (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0));
57810267Ssam 	return (0);
57910267Ssam }
58010267Ssam 
58112757Ssam sorflush(so)
58212757Ssam 	register struct socket *so;
58312757Ssam {
58412757Ssam 	register struct sockbuf *sb = &so->so_rcv;
58512757Ssam 	register struct protosw *pr = so->so_proto;
58612757Ssam 	register int s;
58712757Ssam 	struct sockbuf asb;
58812757Ssam 
58912757Ssam 	sblock(sb);
59012757Ssam 	s = splimp();
59112757Ssam 	socantrcvmore(so);
59212757Ssam 	sbunlock(sb);
59312757Ssam 	asb = *sb;
59412757Ssam 	bzero((caddr_t)sb, sizeof (*sb));
59512757Ssam 	splx(s);
59616993Skarels 	if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose)
59716993Skarels 		(*pr->pr_domain->dom_dispose)(asb.sb_mb);
59812757Ssam 	sbrelease(&asb);
59912757Ssam }
60012757Ssam 
60118553Skarels sosetopt(so, level, optname, m0)
60212757Ssam 	register struct socket *so;
60310267Ssam 	int level, optname;
60418553Skarels 	struct mbuf *m0;
60510267Ssam {
60617158Ssam 	int error = 0;
60718553Skarels 	register struct mbuf *m = m0;
60810267Ssam 
60917158Ssam 	if (level != SOL_SOCKET) {
61018369Skarels 		if (so->so_proto && so->so_proto->pr_ctloutput)
61118369Skarels 			return ((*so->so_proto->pr_ctloutput)
61218553Skarels 				  (PRCO_SETOPT, so, level, optname, &m0));
61318369Skarels 		error = ENOPROTOOPT;
61418369Skarels 	} else {
61518369Skarels 		switch (optname) {
61610267Ssam 
61718369Skarels 		case SO_LINGER:
61818369Skarels 			if (m == NULL || m->m_len != sizeof (struct linger)) {
61918369Skarels 				error = EINVAL;
62018369Skarels 				goto bad;
62118369Skarels 			}
62218369Skarels 			so->so_linger = mtod(m, struct linger *)->l_linger;
62318369Skarels 			/* fall thru... */
62417158Ssam 
62518369Skarels 		case SO_DEBUG:
62618369Skarels 		case SO_KEEPALIVE:
62718369Skarels 		case SO_DONTROUTE:
62818369Skarels 		case SO_USELOOPBACK:
62918369Skarels 		case SO_BROADCAST:
63018369Skarels 		case SO_REUSEADDR:
63118369Skarels 			if (m == NULL || m->m_len < sizeof (int)) {
63218369Skarels 				error = EINVAL;
63318369Skarels 				goto bad;
63418369Skarels 			}
63518369Skarels 			if (*mtod(m, int *))
63618369Skarels 				so->so_options |= optname;
63718369Skarels 			else
63818369Skarels 				so->so_options &= ~optname;
63918369Skarels 			break;
64018369Skarels 
64118369Skarels 		case SO_SNDBUF:
64218369Skarels 		case SO_RCVBUF:
64318369Skarels 		case SO_SNDLOWAT:
64418369Skarels 		case SO_RCVLOWAT:
64518369Skarels 		case SO_SNDTIMEO:
64618369Skarels 		case SO_RCVTIMEO:
64718369Skarels 			if (m == NULL || m->m_len < sizeof (int)) {
64818369Skarels 				error = EINVAL;
64918369Skarels 				goto bad;
65018369Skarels 			}
65118369Skarels 			switch (optname) {
65218369Skarels 
65318369Skarels 			case SO_SNDBUF:
65418369Skarels 			case SO_RCVBUF:
65518369Skarels 				if (sbreserve(optname == SO_SNDBUF ? &so->so_snd :
65618369Skarels 				    &so->so_rcv, *mtod(m, int *)) == 0) {
65718369Skarels 					error = ENOBUFS;
65818369Skarels 					goto bad;
65918369Skarels 				}
66018369Skarels 				break;
66118369Skarels 
66218369Skarels 			case SO_SNDLOWAT:
66318369Skarels 				so->so_snd.sb_lowat = *mtod(m, int *);
66418369Skarels 				break;
66518369Skarels 			case SO_RCVLOWAT:
66618369Skarels 				so->so_rcv.sb_lowat = *mtod(m, int *);
66718369Skarels 				break;
66818369Skarels 			case SO_SNDTIMEO:
66918369Skarels 				so->so_snd.sb_timeo = *mtod(m, int *);
67018369Skarels 				break;
67118369Skarels 			case SO_RCVTIMEO:
67218369Skarels 				so->so_rcv.sb_timeo = *mtod(m, int *);
67318369Skarels 				break;
67418369Skarels 			}
67518369Skarels 			break;
67618369Skarels 
67718369Skarels 		default:
67818369Skarels 			error = ENOPROTOOPT;
67918369Skarels 			break;
68017158Ssam 		}
68110267Ssam 	}
68217158Ssam bad:
68317158Ssam 	if (m)
68417158Ssam 		(void) m_free(m);
68517158Ssam 	return (error);
68610267Ssam }
68710267Ssam 
68817158Ssam sogetopt(so, level, optname, mp)
68912757Ssam 	register struct socket *so;
69010267Ssam 	int level, optname;
69117158Ssam 	struct mbuf **mp;
69217158Ssam {
69312757Ssam 	register struct mbuf *m;
69410267Ssam 
69518369Skarels 	if (level != SOL_SOCKET) {
69618369Skarels 		if (so->so_proto && so->so_proto->pr_ctloutput) {
69718369Skarels 			return ((*so->so_proto->pr_ctloutput)
69818369Skarels 				  (PRCO_GETOPT, so, level, optname, mp));
69918369Skarels 		} else
70018369Skarels 			return (ENOPROTOOPT);
70118369Skarels 	} else {
70217158Ssam 		m = m_get(M_WAIT, MT_SOOPTS);
703*25502Skarels 		m->m_len = sizeof (int);
704*25502Skarels 
70518369Skarels 		switch (optname) {
70617158Ssam 
70718369Skarels 		case SO_LINGER:
70818369Skarels 			m->m_len = sizeof (struct linger);
70918369Skarels 			mtod(m, struct linger *)->l_onoff =
71018369Skarels 				so->so_options & SO_LINGER;
71118369Skarels 			mtod(m, struct linger *)->l_linger = so->so_linger;
71218369Skarels 			break;
71310267Ssam 
71418369Skarels 		case SO_USELOOPBACK:
71518369Skarels 		case SO_DONTROUTE:
71618369Skarels 		case SO_DEBUG:
71718369Skarels 		case SO_KEEPALIVE:
71818369Skarels 		case SO_REUSEADDR:
71918369Skarels 		case SO_BROADCAST:
72018369Skarels 			*mtod(m, int *) = so->so_options & optname;
72118369Skarels 			break;
72218369Skarels 
723*25502Skarels 		case SO_TYPE:
724*25502Skarels 			*mtod(m, int *) = so->so_type;
725*25502Skarels 			break;
726*25502Skarels 
72724768Skarels 		case SO_ERROR:
72824768Skarels 			*mtod(m, int *) = so->so_error;
72924768Skarels 			so->so_error = 0;
73024768Skarels 			break;
73124768Skarels 
73218369Skarels 		case SO_SNDBUF:
73318369Skarels 			*mtod(m, int *) = so->so_snd.sb_hiwat;
73418369Skarels 			break;
73518369Skarels 
73618369Skarels 		case SO_RCVBUF:
73718369Skarels 			*mtod(m, int *) = so->so_rcv.sb_hiwat;
73818369Skarels 			break;
73918369Skarels 
74018369Skarels 		case SO_SNDLOWAT:
74118369Skarels 			*mtod(m, int *) = so->so_snd.sb_lowat;
74218369Skarels 			break;
74318369Skarels 
74418369Skarels 		case SO_RCVLOWAT:
74518369Skarels 			*mtod(m, int *) = so->so_rcv.sb_lowat;
74618369Skarels 			break;
74718369Skarels 
74818369Skarels 		case SO_SNDTIMEO:
74918369Skarels 			*mtod(m, int *) = so->so_snd.sb_timeo;
75018369Skarels 			break;
75118369Skarels 
75218369Skarels 		case SO_RCVTIMEO:
75318369Skarels 			*mtod(m, int *) = so->so_rcv.sb_timeo;
75418369Skarels 			break;
75518369Skarels 
75618369Skarels 		default:
75718369Skarels 			m_free(m);
75818369Skarels 			return (ENOPROTOOPT);
75918369Skarels 		}
76018369Skarels 		*mp = m;
76118369Skarels 		return (0);
76210267Ssam 	}
76310267Ssam }
76410267Ssam 
7655423Swnj sohasoutofband(so)
76612757Ssam 	register struct socket *so;
7675423Swnj {
76823233Skarels 	struct proc *p;
7695423Swnj 
77023233Skarels 	if (so->so_pgrp < 0)
77123233Skarels 		gsignal(-so->so_pgrp, SIGURG);
77223233Skarels 	else if (so->so_pgrp > 0 && (p = pfind(so->so_pgrp)) != 0)
77323233Skarels 		psignal(p, SIGURG);
77424768Skarels 	if (so->so_rcv.sb_sel) {
77524768Skarels 		selwakeup(so->so_rcv.sb_sel, so->so_rcv.sb_flags & SB_COLL);
77624768Skarels 		so->so_rcv.sb_sel = 0;
77724768Skarels 		so->so_rcv.sb_flags &= ~SB_COLL;
77824768Skarels 	}
7795423Swnj }
780