xref: /csrg-svn/sys/kern/uipc_socket.c (revision 13113)
1*13113Ssam /*	uipc_socket.c	4.76	83/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"
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 
236*13113Ssam 	error = (*so1->so_proto->pr_usrreq)(so1, PRU_CONNECT2,
237*13113Ssam 	    (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 		}
3728771Sroot 		(void) uiomove(mtod(m, caddr_t), len, UIO_WRITE, uio);
3734890Swnj 		m->m_len = len;
3744890Swnj 		*mp = m;
3754890Swnj 		mp = &m->m_next;
37612757Ssam 		if (flags & MSG_OOB)
3778319Sroot 			space -= len;
3788319Sroot 		else
3798319Sroot 			space = sbspace(&so->so_snd);
3804786Swnj 	}
3814890Swnj 	goto again;
3824890Swnj 
3834786Swnj release:
3844890Swnj 	sbunlock(&so->so_snd);
3856419Sroot 	if (top)
3866419Sroot 		m_freem(top);
3874786Swnj 	return (error);
3884786Swnj }
3894786Swnj 
39012757Ssam soreceive(so, aname, uio, flags, rightsp)
3914786Swnj 	register struct socket *so;
3928300Sroot 	struct mbuf **aname;
39312757Ssam 	register struct uio *uio;
3948319Sroot 	int flags;
39512757Ssam 	struct mbuf **rightsp;
3964786Swnj {
3974786Swnj 	register struct mbuf *m, *n;
39812757Ssam 	register int len, error = 0, s, eor, tomark;
39912757Ssam 	struct protosw *pr = so->so_proto;
40012757Ssam 	int moff;
4014786Swnj 
40212757Ssam 	if (rightsp)
40312757Ssam 		*rightsp = 0;
40412757Ssam 	if (aname)
40512757Ssam 		*aname = 0;
40612757Ssam 	if (flags & MSG_OOB) {
4079635Ssam 		m = m_get(M_WAIT, MT_DATA);
40812757Ssam 		if (m == 0)
40910137Ssam 			return (ENOBUFS);
41012757Ssam 		error = (*pr->pr_usrreq)(so, PRU_RCVOOB,
41112757Ssam 		    m, (struct mbuf *)0, (struct mbuf *)0);
4128594Sroot 		if (error)
41310137Ssam 			goto bad;
4148319Sroot 		do {
41510137Ssam 			len = uio->uio_resid;
4168319Sroot 			if (len > m->m_len)
4178319Sroot 				len = m->m_len;
4188594Sroot 			error =
4198793Sroot 			    uiomove(mtod(m, caddr_t), (int)len, UIO_READ, uio);
4208319Sroot 			m = m_free(m);
4218594Sroot 		} while (uio->uio_resid && error == 0 && m);
42210137Ssam bad:
4238319Sroot 		if (m)
4248771Sroot 			m_freem(m);
4258594Sroot 		return (error);
4268319Sroot 	}
4278319Sroot 
4284890Swnj restart:
4294890Swnj 	sblock(&so->so_rcv);
4308835Sroot 	s = splnet();
4314890Swnj 
4324890Swnj #define	rcverr(errno)	{ error = errno; splx(s); goto release; }
4334786Swnj 	if (so->so_rcv.sb_cc == 0) {
4345168Swnj 		if (so->so_error) {
4355168Swnj 			error = so->so_error;
4365168Swnj 			so->so_error = 0;
4375168Swnj 			splx(s);
4385168Swnj 			goto release;
4395168Swnj 		}
4404890Swnj 		if (so->so_state & SS_CANTRCVMORE) {
4414890Swnj 			splx(s);
4424890Swnj 			goto release;
4434890Swnj 		}
4445015Sroot 		if ((so->so_state & SS_ISCONNECTED) == 0 &&
4455015Sroot 		    (so->so_proto->pr_flags & PR_CONNREQUIRED))
4465015Sroot 			rcverr(ENOTCONN);
4476214Swnj 		if (so->so_state & SS_NBIO)
4485168Swnj 			rcverr(EWOULDBLOCK);
4494890Swnj 		sbunlock(&so->so_rcv);
4504971Swnj 		sbwait(&so->so_rcv);
4515012Swnj 		splx(s);
4524890Swnj 		goto restart;
4534786Swnj 	}
4548041Sroot 	u.u_ru.ru_msgrcv++;
4554829Swnj 	m = so->so_rcv.sb_mb;
4564786Swnj 	if (m == 0)
4574786Swnj 		panic("receive");
45812757Ssam 	if (pr->pr_flags & PR_ADDR) {
45912757Ssam 		if ((flags & MSG_PEEK) == 0) {
4608319Sroot 			so->so_rcv.sb_cc -= m->m_len;
4618319Sroot 			so->so_rcv.sb_mbcnt -= MSIZE;
4628319Sroot 		}
4638300Sroot 		if (aname) {
46412757Ssam 			if (flags & MSG_PEEK) {
4658319Sroot 				*aname = m_copy(m, 0, m->m_len);
46610137Ssam 				if (*aname == NULL)
46710137Ssam 					panic("receive 2");
46810137Ssam 			} else
4698319Sroot 				*aname = m;
4708300Sroot 			m = m->m_next;
4718300Sroot 			(*aname)->m_next = 0;
4728300Sroot 		} else
47312757Ssam 			if (flags & MSG_PEEK)
4748319Sroot 				m = m->m_next;
4758319Sroot 			else
4768319Sroot 				m = m_free(m);
4774890Swnj 		if (m == 0)
47812757Ssam 			panic("receive 2a");
47912757Ssam 		if (rightsp) {
48012757Ssam 			if (m->m_len)
48112757Ssam 				*rightsp = m_copy(m, 0, m->m_len);
48212757Ssam 			else {
48312757Ssam 				*rightsp = m_get(M_DONTWAIT, MT_SONAME);
48412757Ssam 				if (*rightsp)
48512757Ssam 					(*rightsp)->m_len = 0;
48612757Ssam 			}
48712757Ssam 			if (*rightsp == NULL)
48812757Ssam 				panic("receive 2b");
48912757Ssam 		}
49012757Ssam 		if (flags & MSG_PEEK)
49112757Ssam 			m = m->m_next;
49212757Ssam 		else {
49312757Ssam 			so->so_rcv.sb_cc -= m->m_len;
49412757Ssam 			so->so_rcv.sb_mbcnt -= MSIZE;
49512757Ssam 			m = m_free(m);
49612757Ssam 		}
49712757Ssam 		if (m == 0)
49810137Ssam 			panic("receive 3");
49912757Ssam 		if ((flags & MSG_PEEK) == 0)
5008548Sroot 			so->so_rcv.sb_mb = m;
5014890Swnj 	}
5024786Swnj 	eor = 0;
5038319Sroot 	moff = 0;
5048319Sroot 	tomark = so->so_oobmark;
5054786Swnj 	do {
5067827Sroot 		if (uio->uio_resid <= 0)
5077747Sroot 			break;
5087827Sroot 		len = uio->uio_resid;
5097747Sroot 		so->so_state &= ~SS_RCVATMARK;
5108319Sroot 		if (tomark && len > tomark)
5118319Sroot 			len = tomark;
5128548Sroot 		if (moff+len > m->m_len - moff)
5138319Sroot 			len = m->m_len - moff;
5144786Swnj 		splx(s);
5158594Sroot 		error =
5168793Sroot 		    uiomove(mtod(m, caddr_t) + moff, (int)len, UIO_READ, uio);
5174786Swnj 		s = splnet();
5184786Swnj 		if (len == m->m_len) {
5196091Sroot 			eor = (int)m->m_act;
52012757Ssam 			if (flags & MSG_PEEK)
5218319Sroot 				m = m->m_next;
5228319Sroot 			else {
5238319Sroot 				sbfree(&so->so_rcv, m);
5248319Sroot 				MFREE(m, n);
5258319Sroot 				m = n;
5268548Sroot 				so->so_rcv.sb_mb = m;
5278319Sroot 			}
5288319Sroot 			moff = 0;
5294786Swnj 		} else {
53012757Ssam 			if (flags & MSG_PEEK)
5318319Sroot 				moff += len;
5328319Sroot 			else {
5338319Sroot 				m->m_off += len;
5348319Sroot 				m->m_len -= len;
5358319Sroot 				so->so_rcv.sb_cc -= len;
5368319Sroot 			}
5374786Swnj 		}
53812757Ssam 		if ((flags & MSG_PEEK) == 0 && so->so_oobmark) {
5397747Sroot 			so->so_oobmark -= len;
5407747Sroot 			if (so->so_oobmark == 0) {
5417747Sroot 				so->so_state |= SS_RCVATMARK;
5427747Sroot 				break;
5437747Sroot 			}
5447747Sroot 		}
5458319Sroot 		if (tomark) {
5468319Sroot 			tomark -= len;
5478319Sroot 			if (tomark == 0)
5488319Sroot 				break;
5498319Sroot 		}
5508594Sroot 	} while (m && error == 0 && !eor);
55112757Ssam 	if (flags & MSG_PEEK)
5528319Sroot 		goto release;
5534786Swnj 	if ((so->so_proto->pr_flags & PR_ATOMIC) && eor == 0)
5544786Swnj 		do {
5554786Swnj 			if (m == 0)
55610137Ssam 				panic("receive 4");
5574890Swnj 			sbfree(&so->so_rcv, m);
5584786Swnj 			eor = (int)m->m_act;
5594786Swnj 			so->so_rcv.sb_mb = m->m_next;
5604786Swnj 			MFREE(m, n);
5614890Swnj 			m = n;
5624786Swnj 		} while (eor == 0);
5634890Swnj 	if ((so->so_proto->pr_flags & PR_WANTRCVD) && so->so_pcb)
5648300Sroot 		(*so->so_proto->pr_usrreq)(so, PRU_RCVD,
56512757Ssam 		    (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0);
5664890Swnj release:
5674916Swnj 	sbunlock(&so->so_rcv);
56812757Ssam 	if (error == 0 && rightsp &&
56912757Ssam 	    *rightsp && so->so_proto->pr_family == AF_UNIX)
57012757Ssam 		error = unp_externalize(*rightsp);
5714890Swnj 	splx(s);
5724916Swnj 	return (error);
5734786Swnj }
5744786Swnj 
57510267Ssam soshutdown(so, how)
57612757Ssam 	register struct socket *so;
57712757Ssam 	register int how;
57810267Ssam {
57912757Ssam 	register struct protosw *pr = so->so_proto;
58010267Ssam 
58110267Ssam 	how++;
58212757Ssam 	if (how & FREAD)
58312757Ssam 		sorflush(so);
58410267Ssam 	if (how & FWRITE)
58512757Ssam 		return ((*pr->pr_usrreq)(so, PRU_SHUTDOWN,
58612757Ssam 		    (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0));
58710267Ssam 	return (0);
58810267Ssam }
58910267Ssam 
59012757Ssam sorflush(so)
59112757Ssam 	register struct socket *so;
59212757Ssam {
59312757Ssam 	register struct sockbuf *sb = &so->so_rcv;
59412757Ssam 	register struct protosw *pr = so->so_proto;
59512757Ssam 	register int s;
59612757Ssam 	struct sockbuf asb;
59712757Ssam 
59812757Ssam 	sblock(sb);
59912757Ssam 	s = splimp();
60012757Ssam 	socantrcvmore(so);
60112757Ssam 	sbunlock(sb);
60212757Ssam 	asb = *sb;
60312757Ssam 	bzero((caddr_t)sb, sizeof (*sb));
60412757Ssam 	splx(s);
60512757Ssam 	if (pr->pr_family == AF_UNIX && (pr->pr_flags & PR_RIGHTS))
60612757Ssam 		unp_scan(asb.sb_mb, unp_discard);
60712757Ssam 	sbrelease(&asb);
60812757Ssam }
60912757Ssam 
61010267Ssam sosetopt(so, level, optname, m)
61112757Ssam 	register struct socket *so;
61210267Ssam 	int level, optname;
61312757Ssam 	register struct mbuf *m;
61410267Ssam {
61510267Ssam 
61610267Ssam 	if (level != SOL_SOCKET)
61710267Ssam 		return (EINVAL);	/* XXX */
61810267Ssam 	switch (optname) {
61910267Ssam 
62010267Ssam 	case SO_DEBUG:
62110269Ssam 	case SO_KEEPALIVE:
62210599Ssam 	case SO_DONTROUTE:
62310599Ssam 	case SO_USELOOPBACK:
62410599Ssam 	case SO_REUSEADDR:
62510599Ssam 		so->so_options |= optname;
62610269Ssam 		break;
62710269Ssam 
62810267Ssam 	case SO_LINGER:
62910269Ssam 		if (m == NULL || m->m_len != sizeof (int))
63010269Ssam 			return (EINVAL);
63110267Ssam 		so->so_options |= SO_LINGER;
63210269Ssam 		so->so_linger = *mtod(m, int *);
63310267Ssam 		break;
63410267Ssam 
63510267Ssam 	case SO_DONTLINGER:
63610267Ssam 		so->so_options &= ~SO_LINGER;
63710267Ssam 		so->so_linger = 0;
63810267Ssam 		break;
63910267Ssam 
64010267Ssam 	default:
64110267Ssam 		return (EINVAL);
64210267Ssam 	}
64310267Ssam 	return (0);
64410267Ssam }
64510267Ssam 
64610267Ssam sogetopt(so, level, optname, m)
64712757Ssam 	register struct socket *so;
64810267Ssam 	int level, optname;
64912757Ssam 	register struct mbuf *m;
65010267Ssam {
65110267Ssam 
65210267Ssam 	if (level != SOL_SOCKET)
65310267Ssam 		return (EINVAL);	/* XXX */
65410267Ssam 	switch (optname) {
65510267Ssam 
65610267Ssam 	case SO_USELOOPBACK:
65710267Ssam 	case SO_DONTROUTE:
65810267Ssam 	case SO_DEBUG:
65910267Ssam 	case SO_KEEPALIVE:
66010267Ssam 	case SO_LINGER:
66110599Ssam 	case SO_REUSEADDR:
66210267Ssam 		if ((so->so_options & optname) == 0)
66310267Ssam 			return (ENOPROTOOPT);
66410269Ssam 		if (optname == SO_LINGER && m != NULL) {
66510267Ssam 			*mtod(m, int *) = so->so_linger;
66610267Ssam 			m->m_len = sizeof (so->so_linger);
66710267Ssam 		}
66810267Ssam 		break;
66910267Ssam 
67010267Ssam 	default:
67110267Ssam 		return (EINVAL);
67210267Ssam 	}
67310267Ssam 	return (0);
67410267Ssam }
67510267Ssam 
6765423Swnj sohasoutofband(so)
67712757Ssam 	register struct socket *so;
6785423Swnj {
6795423Swnj 
6805423Swnj 	if (so->so_pgrp == 0)
6815423Swnj 		return;
6825423Swnj 	if (so->so_pgrp > 0)
6835423Swnj 		gsignal(so->so_pgrp, SIGURG);
6845429Swnj 	else {
6855429Swnj 		struct proc *p = pfind(-so->so_pgrp);
6865429Swnj 
6875429Swnj 		if (p)
6885429Swnj 			psignal(p, SIGURG);
6895429Swnj 	}
6905423Swnj }
691