xref: /csrg-svn/sys/kern/uipc_socket.c (revision 44383)
123421Smckusick /*
241908Skarels  * Copyright (c) 1982, 1986, 1988, 1990 Regents of the University of California.
333185Sbostic  * All rights reserved.
423421Smckusick  *
533185Sbostic  * Redistribution and use in source and binary forms are permitted
634861Sbostic  * provided that the above copyright notice and this paragraph are
734861Sbostic  * duplicated in all such forms and that any documentation,
834861Sbostic  * advertising materials, and other materials related to such
934861Sbostic  * distribution and use acknowledge that the software was developed
1034861Sbostic  * by the University of California, Berkeley.  The name of the
1134861Sbostic  * University may not be used to endorse or promote products derived
1234861Sbostic  * from this software without specific prior written permission.
1334861Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1434861Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1534861Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1633185Sbostic  *
17*44383Skarels  *	@(#)uipc_socket.c	7.21 (Berkeley) 06/28/90
1823421Smckusick  */
194786Swnj 
2017102Sbloom #include "param.h"
2117102Sbloom #include "user.h"
2217102Sbloom #include "proc.h"
2317102Sbloom #include "file.h"
2435384Skarels #include "malloc.h"
2517102Sbloom #include "mbuf.h"
2617102Sbloom #include "domain.h"
2717102Sbloom #include "protosw.h"
2817102Sbloom #include "socket.h"
2917102Sbloom #include "socketvar.h"
304786Swnj 
314786Swnj /*
328300Sroot  * Socket operation routines.
338300Sroot  * These routines are called by the routines in
348300Sroot  * sys_socket.c or from a system process, and
358300Sroot  * implement the semantics of socket operations by
368300Sroot  * switching out to the protocol specific routines.
3712757Ssam  *
3812757Ssam  * TODO:
3912757Ssam  *	test socketpair
4021767Skarels  *	clean up async
4112757Ssam  *	out-of-band is a kludge
424786Swnj  */
438594Sroot /*ARGSUSED*/
4410267Ssam socreate(dom, aso, type, proto)
454786Swnj 	struct socket **aso;
4612757Ssam 	register int type;
4712757Ssam 	int proto;
484786Swnj {
494786Swnj 	register struct protosw *prp;
504786Swnj 	register struct socket *so;
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);
6137478Ssklower 	MALLOC(so, struct socket *, sizeof(*so), M_SOCKET, M_WAIT);
6237478Ssklower 	bzero((caddr_t)so, sizeof(*so));
639168Ssam 	so->so_type = type;
646214Swnj 	if (u.u_uid == 0)
656214Swnj 		so->so_state = SS_PRIV;
664786Swnj 	so->so_proto = prp;
6712757Ssam 	error =
6812757Ssam 	    (*prp->pr_usrreq)(so, PRU_ATTACH,
6921767Skarels 		(struct mbuf *)0, (struct mbuf *)proto, (struct mbuf *)0);
704979Swnj 	if (error) {
717507Sroot 		so->so_state |= SS_NOFDREF;
727180Swnj 		sofree(so);
734890Swnj 		return (error);
744786Swnj 	}
754786Swnj 	*aso = so;
764786Swnj 	return (0);
774786Swnj }
784786Swnj 
7910267Ssam sobind(so, nam)
808300Sroot 	struct socket *so;
818300Sroot 	struct mbuf *nam;
828300Sroot {
838300Sroot 	int s = splnet();
848300Sroot 	int error;
858300Sroot 
8641908Skarels 	error =
8741908Skarels 	    (*so->so_proto->pr_usrreq)(so, PRU_BIND,
8812757Ssam 		(struct mbuf *)0, nam, (struct mbuf *)0);
898300Sroot 	splx(s);
908300Sroot 	return (error);
918300Sroot }
928300Sroot 
938300Sroot solisten(so, backlog)
9412757Ssam 	register struct socket *so;
958300Sroot 	int backlog;
968300Sroot {
9712757Ssam 	int s = splnet(), error;
988300Sroot 
9912757Ssam 	error =
10012757Ssam 	    (*so->so_proto->pr_usrreq)(so, PRU_LISTEN,
10112757Ssam 		(struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0);
1028300Sroot 	if (error) {
1038300Sroot 		splx(s);
1048300Sroot 		return (error);
1058300Sroot 	}
10638584Skarels 	if (so->so_q == 0)
1078300Sroot 		so->so_options |= SO_ACCEPTCONN;
1088300Sroot 	if (backlog < 0)
1098300Sroot 		backlog = 0;
11035384Skarels 	so->so_qlimit = min(backlog, SOMAXCONN);
11112493Ssam 	splx(s);
1128300Sroot 	return (0);
1138300Sroot }
1148300Sroot 
1154916Swnj sofree(so)
11612757Ssam 	register struct socket *so;
1174916Swnj {
1184916Swnj 
11931810Skarels 	if (so->so_pcb || (so->so_state & SS_NOFDREF) == 0)
12031810Skarels 		return;
1217507Sroot 	if (so->so_head) {
1227507Sroot 		if (!soqremque(so, 0) && !soqremque(so, 1))
1237507Sroot 			panic("sofree dq");
1247507Sroot 		so->so_head = 0;
1257507Sroot 	}
1264950Swnj 	sbrelease(&so->so_snd);
12712757Ssam 	sorflush(so);
12837478Ssklower 	FREE(so, M_SOCKET);
1294916Swnj }
1304916Swnj 
1314786Swnj /*
1324890Swnj  * Close a socket on last file table reference removal.
1334890Swnj  * Initiate disconnect if connected.
1344890Swnj  * Free socket when disconnect complete.
1354829Swnj  */
13612757Ssam soclose(so)
1374829Swnj 	register struct socket *so;
1384829Swnj {
1394890Swnj 	int s = splnet();		/* conservative */
14033372Sbostic 	int error = 0;
1414829Swnj 
1427507Sroot 	if (so->so_options & SO_ACCEPTCONN) {
14338584Skarels 		while (so->so_q0)
14410399Ssam 			(void) soabort(so->so_q0);
14538584Skarels 		while (so->so_q)
14610399Ssam 			(void) soabort(so->so_q);
1477507Sroot 	}
1484890Swnj 	if (so->so_pcb == 0)
1494890Swnj 		goto discard;
1504890Swnj 	if (so->so_state & SS_ISCONNECTED) {
1514890Swnj 		if ((so->so_state & SS_ISDISCONNECTING) == 0) {
15226245Skarels 			error = sodisconnect(so);
15312757Ssam 			if (error)
15412757Ssam 				goto drop;
1554890Swnj 		}
15610267Ssam 		if (so->so_options & SO_LINGER) {
1575281Sroot 			if ((so->so_state & SS_ISDISCONNECTING) &&
15812757Ssam 			    (so->so_state & SS_NBIO))
15912757Ssam 				goto drop;
1605281Sroot 			while (so->so_state & SS_ISCONNECTED)
16140706Skarels 				if (error = tsleep((caddr_t)&so->so_timeo,
16240706Skarels 				    PSOCK | PCATCH, netcls, so->so_linger))
16340706Skarels 					break;
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 {
21530414Skarels 	int s;
2164890Swnj 	int error;
2174786Swnj 
21830414Skarels 	if (so->so_options & SO_ACCEPTCONN)
21930414Skarels 		return (EOPNOTSUPP);
22030414Skarels 	s = splnet();
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 
25126245Skarels sodisconnect(so)
25212757Ssam 	register struct socket *so;
2534786Swnj {
2544890Swnj 	int s = splnet();
2554890Swnj 	int error;
2564786Swnj 
2574890Swnj 	if ((so->so_state & SS_ISCONNECTED) == 0) {
2584890Swnj 		error = ENOTCONN;
2594890Swnj 		goto bad;
2604890Swnj 	}
2614890Swnj 	if (so->so_state & SS_ISDISCONNECTING) {
2624890Swnj 		error = EALREADY;
2634890Swnj 		goto bad;
2644890Swnj 	}
2658300Sroot 	error = (*so->so_proto->pr_usrreq)(so, PRU_DISCONNECT,
26626245Skarels 	    (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0);
2674890Swnj bad:
2684890Swnj 	splx(s);
2694890Swnj 	return (error);
2704786Swnj }
2714786Swnj 
2724786Swnj /*
2734890Swnj  * Send on a socket.
2744890Swnj  * If send must go all at once and message is larger than
2754890Swnj  * send buffering, then hard error.
2764890Swnj  * Lock against other senders.
2774890Swnj  * If must go all at once and not enough room now, then
2784890Swnj  * inform user that this would block and do nothing.
27916412Skarels  * Otherwise, if nonblocking, send as much as possible.
28041908Skarels  * The data to be sent is described by "uio" if nonzero,
28141908Skarels  * otherwise by the mbuf chain "top" (which must be null
28241908Skarels  * if uio is not).  Data provided in mbuf chain must be small
28341908Skarels  * enough to send all at once.
28441908Skarels  *
28541908Skarels  * Returns nonzero on error, timeout or signal; callers
28641908Skarels  * must check for short counts if EINTR/ERESTART are returned.
28741908Skarels  * Data and control buffers are freed on return.
2884786Swnj  */
28943417Skarels sosend(so, addr, uio, top, control, flags)
2904786Swnj 	register struct socket *so;
29141908Skarels 	struct mbuf *addr;
29241908Skarels 	struct uio *uio;
29341908Skarels 	struct mbuf *top;
29441908Skarels 	struct mbuf *control;
2958319Sroot 	int flags;
2964786Swnj {
29741908Skarels 	struct mbuf **mp;
29835384Skarels 	register struct mbuf *m;
29941908Skarels 	register long space, len, resid;
30041908Skarels 	int clen = 0, error, s, dontroute, mlen;
30141908Skarels 	int atomic = sosendallatonce(so) || top;
3024786Swnj 
30341908Skarels 	if (uio)
30441908Skarels 		resid = uio->uio_resid;
30541908Skarels 	else
30641908Skarels 		resid = top->m_pkthdr.len;
30712757Ssam 	dontroute =
30812757Ssam 	    (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&
30912757Ssam 	    (so->so_proto->pr_flags & PR_ATOMIC);
31016412Skarels 	u.u_ru.ru_msgsnd++;
31140632Skarels 	if (control)
31241908Skarels 		clen = control->m_len;
31316412Skarels #define	snderr(errno)	{ error = errno; splx(s); goto release; }
31416412Skarels 
3156419Sroot restart:
31640706Skarels 	if (error = sblock(&so->so_snd))
31741908Skarels 		goto out;
31816412Skarels 	do {
31916412Skarels 		s = splnet();
32021108Skarels 		if (so->so_state & SS_CANTSENDMORE)
32116412Skarels 			snderr(EPIPE);
32237478Ssklower 		if (so->so_error)
32337478Ssklower 			snderr(so->so_error);
32416412Skarels 		if ((so->so_state & SS_ISCONNECTED) == 0) {
32537478Ssklower 			if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
32638584Skarels 				if ((so->so_state & SS_ISCONFIRMING) == 0)
32737478Ssklower 					snderr(ENOTCONN);
32841908Skarels 			} else if (addr == 0)
32916412Skarels 				snderr(EDESTADDRREQ);
33016412Skarels 		}
33141908Skarels 		space = sbspace(&so->so_snd);
33216412Skarels 		if (flags & MSG_OOB)
33341908Skarels 			space += 1024;
33441908Skarels 		if (space < resid + clen &&
33541908Skarels 		    (atomic || space < so->so_snd.sb_lowat || space < clen)) {
33641908Skarels 			if (atomic && resid > so->so_snd.sb_hiwat ||
33741908Skarels 			    clen > so->so_snd.sb_hiwat)
33841908Skarels 				snderr(EMSGSIZE);
33941908Skarels 			if (so->so_state & SS_NBIO)
34041908Skarels 				snderr(EWOULDBLOCK);
34141908Skarels 			sbunlock(&so->so_snd);
34243417Skarels 			error = sbwait(&so->so_snd);
34341908Skarels 			splx(s);
34441908Skarels 			if (error)
34541908Skarels 				goto out;
34641908Skarels 			goto restart;
34716412Skarels 		}
34816412Skarels 		splx(s);
34916412Skarels 		mp = &top;
35041908Skarels 		space -= clen;
35143417Skarels 		do {
35243417Skarels 		    if (uio == NULL) {
35341908Skarels 			/*
35441908Skarels 			 * Data is prepackaged in "top".
35541908Skarels 			 */
35641908Skarels 			resid = 0;
35741908Skarels 			if (flags & MSG_EOR)
35841908Skarels 				top->m_flags |= M_EOR;
35943417Skarels 		    } else do {
36035384Skarels 			if (top == 0) {
36135384Skarels 				MGETHDR(m, M_WAIT, MT_DATA);
36235384Skarels 				mlen = MHLEN;
36335384Skarels 				m->m_pkthdr.len = 0;
36435384Skarels 				m->m_pkthdr.rcvif = (struct ifnet *)0;
36535384Skarels 			} else {
36635384Skarels 				MGET(m, M_WAIT, MT_DATA);
36735384Skarels 				mlen = MLEN;
36835384Skarels 			}
36941908Skarels 			if (resid >= MINCLSIZE && space >= MCLBYTES) {
37035384Skarels 				MCLGET(m, M_WAIT);
37135384Skarels 				if ((m->m_flags & M_EXT) == 0)
37216412Skarels 					goto nopages;
37335384Skarels 				mlen = MCLBYTES;
37435384Skarels #ifdef	MAPPED_MBUFS
37541908Skarels 				len = min(MCLBYTES, resid);
37641908Skarels #else
37741908Skarels 				if (top == 0) {
37841908Skarels 					len = min(MCLBYTES - max_hdr, resid);
37935384Skarels 					m->m_data += max_hdr;
38041908Skarels 				}
38135384Skarels #endif
38235384Skarels 				space -= MCLBYTES;
38316412Skarels 			} else {
38416412Skarels nopages:
38541908Skarels 				len = min(min(mlen, resid), space);
38621767Skarels 				space -= len;
38735384Skarels 				/*
38835384Skarels 				 * For datagram protocols, leave room
38935384Skarels 				 * for protocol headers in first mbuf.
39035384Skarels 				 */
39135391Skarels 				if (atomic && top == 0 && len < mlen)
39235384Skarels 					MH_ALIGN(m, len);
39316412Skarels 			}
39437728Smckusick 			error = uiomove(mtod(m, caddr_t), len, uio);
39541908Skarels 			resid = uio->uio_resid;
39616412Skarels 			m->m_len = len;
39716412Skarels 			*mp = m;
39835384Skarels 			top->m_pkthdr.len += len;
39916412Skarels 			if (error)
40016412Skarels 				goto release;
40116412Skarels 			mp = &m->m_next;
40241908Skarels 			if (resid <= 0) {
40341908Skarels 				if (flags & MSG_EOR)
40435384Skarels 					top->m_flags |= M_EOR;
40521108Skarels 				break;
40635384Skarels 			}
40735384Skarels 		    } while (space > 0 && atomic);
40835384Skarels 		    if (dontroute)
40935384Skarels 			    so->so_options |= SO_DONTROUTE;
41035384Skarels 		    s = splnet();				/* XXX */
41135384Skarels 		    error = (*so->so_proto->pr_usrreq)(so,
41235384Skarels 			(flags & MSG_OOB) ? PRU_SENDOOB : PRU_SEND,
41341908Skarels 			top, addr, control);
41435384Skarels 		    splx(s);
41535384Skarels 		    if (dontroute)
41635384Skarels 			    so->so_options &= ~SO_DONTROUTE;
41741908Skarels 		    clen = 0;
41841908Skarels 		    control = 0;
41935384Skarels 		    top = 0;
42035384Skarels 		    mp = &top;
42135384Skarels 		    if (error)
42235384Skarels 			goto release;
42341908Skarels 		} while (resid && space > 0);
42441908Skarels 	} while (resid);
4254890Swnj 
4264786Swnj release:
4274890Swnj 	sbunlock(&so->so_snd);
42841908Skarels out:
4296419Sroot 	if (top)
4306419Sroot 		m_freem(top);
43141908Skarels 	if (control)
43241908Skarels 		m_freem(control);
4334786Swnj 	return (error);
4344786Swnj }
4354786Swnj 
43625629Skarels /*
43725629Skarels  * Implement receive operations on a socket.
43825629Skarels  * We depend on the way that records are added to the sockbuf
43925629Skarels  * by sbappend*.  In particular, each record (mbufs linked through m_next)
44025629Skarels  * must begin with an address if the protocol so specifies,
44141908Skarels  * followed by an optional mbuf or mbufs containing ancillary data,
44241908Skarels  * and then zero or more mbufs of data.
44325629Skarels  * In order to avoid blocking network interrupts for the entire time here,
44425629Skarels  * we splx() while doing the actual copy to user space.
44525629Skarels  * Although the sockbuf is locked, new data may still be appended,
44625629Skarels  * and thus we must maintain consistency of the sockbuf during that time.
44741908Skarels  *
44841908Skarels  * The caller may receive the data as a single mbuf chain by supplying
44943417Skarels  * an mbuf **mp0 for use in returning the chain.  The uio is then used
45041908Skarels  * only for the count in uio_resid.
45125629Skarels  */
45243417Skarels soreceive(so, paddr, uio, mp0, controlp, flagsp)
4534786Swnj 	register struct socket *so;
45441908Skarels 	struct mbuf **paddr;
45541908Skarels 	struct uio *uio;
45643417Skarels 	struct mbuf **mp0;
45741908Skarels 	struct mbuf **controlp;
45835384Skarels 	int *flagsp;
4594786Swnj {
46043417Skarels 	register struct mbuf *m, **mp;
46143417Skarels 	register int flags, len, error, s, offset;
46212757Ssam 	struct protosw *pr = so->so_proto;
46341908Skarels 	struct mbuf *nextrecord;
46441908Skarels 	int moff, type;
4654786Swnj 
46643417Skarels 	mp = mp0;
46741908Skarels 	if (paddr)
46841908Skarels 		*paddr = 0;
46935384Skarels 	if (controlp)
47035384Skarels 		*controlp = 0;
47135384Skarels 	if (flagsp)
47235384Skarels 		flags = *flagsp &~ MSG_EOR;
47335384Skarels 	else
47435384Skarels 		flags = 0;
47512757Ssam 	if (flags & MSG_OOB) {
4769635Ssam 		m = m_get(M_WAIT, MT_DATA);
47712757Ssam 		error = (*pr->pr_usrreq)(so, PRU_RCVOOB,
47824768Skarels 		    m, (struct mbuf *)(flags & MSG_PEEK), (struct mbuf *)0);
4798594Sroot 		if (error)
48010137Ssam 			goto bad;
4818319Sroot 		do {
48241908Skarels 			error = uiomove(mtod(m, caddr_t),
48341908Skarels 			    (int) min(uio->uio_resid, m->m_len), uio);
4848319Sroot 			m = m_free(m);
4858594Sroot 		} while (uio->uio_resid && error == 0 && m);
48610137Ssam bad:
4878319Sroot 		if (m)
4888771Sroot 			m_freem(m);
4898594Sroot 		return (error);
4908319Sroot 	}
49141908Skarels 	if (mp)
49241908Skarels 		*mp = (struct mbuf *)0;
49343417Skarels 	if (so->so_state & SS_ISCONFIRMING && uio->uio_resid)
49435384Skarels 		(*pr->pr_usrreq)(so, PRU_RCVD, (struct mbuf *)0,
49535384Skarels 		    (struct mbuf *)0, (struct mbuf *)0);
4968319Sroot 
4974890Swnj restart:
49840706Skarels 	if (error = sblock(&so->so_rcv))
49940706Skarels 		return (error);
5008835Sroot 	s = splnet();
5014890Swnj 
50237478Ssklower 	m = so->so_rcv.sb_mb;
503*44383Skarels 	/*
504*44383Skarels 	 * If we have less data than requested, block awaiting more
505*44383Skarels 	 * (subject to any timeout) if:
506*44383Skarels 	 *   1. the current count is less than the low water mark, or
507*44383Skarels 	 *   2. MSG_WAITALL is set, and it is possible to do the entire
508*44383Skarels 	 *	receive operation at once if we block (resid <= hiwat).
509*44383Skarels 	 * If MSG_WAITALL is set but resid is larger than the receive buffer,
510*44383Skarels 	 * we have to do the receive in sections, and thus risk returning
511*44383Skarels 	 * a short count if a timeout or signal occurs after we start.
512*44383Skarels 	 */
513*44383Skarels 	if (m == 0 || so->so_rcv.sb_cc < uio->uio_resid &&
514*44383Skarels 	    (so->so_rcv.sb_cc < so->so_rcv.sb_lowat ||
515*44383Skarels 	    ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat))) {
51641908Skarels #ifdef DIAGNOSTIC
51741908Skarels 		if (m == 0 && so->so_rcv.sb_cc)
51837478Ssklower 			panic("receive 1");
51941908Skarels #endif
5205168Swnj 		if (so->so_error) {
5215168Swnj 			error = so->so_error;
5225168Swnj 			so->so_error = 0;
5235168Swnj 			goto release;
5245168Swnj 		}
52532567Sbostic 		if (so->so_state & SS_CANTRCVMORE)
5264890Swnj 			goto release;
52738584Skarels 		if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
52832567Sbostic 		    (so->so_proto->pr_flags & PR_CONNREQUIRED)) {
52932567Sbostic 			error = ENOTCONN;
53032567Sbostic 			goto release;
5314890Swnj 		}
53243417Skarels 		if (uio->uio_resid == 0)
53325629Skarels 			goto release;
53432567Sbostic 		if (so->so_state & SS_NBIO) {
53532567Sbostic 			error = EWOULDBLOCK;
53632567Sbostic 			goto release;
53732567Sbostic 		}
5384890Swnj 		sbunlock(&so->so_rcv);
53943417Skarels 		error = sbwait(&so->so_rcv);
5405012Swnj 		splx(s);
54141908Skarels 		if (error)
54241908Skarels 			return (error);
5434890Swnj 		goto restart;
5444786Swnj 	}
5458041Sroot 	u.u_ru.ru_msgrcv++;
54635384Skarels 	nextrecord = m->m_nextpkt;
54712757Ssam 	if (pr->pr_flags & PR_ADDR) {
54841908Skarels #ifdef DIAGNOSTIC
54925629Skarels 		if (m->m_type != MT_SONAME)
55016993Skarels 			panic("receive 1a");
55141908Skarels #endif
55216993Skarels 		if (flags & MSG_PEEK) {
55341908Skarels 			if (paddr)
55441908Skarels 				*paddr = m_copy(m, 0, m->m_len);
55525629Skarels 			m = m->m_next;
55616993Skarels 		} else {
55725629Skarels 			sbfree(&so->so_rcv, m);
55841908Skarels 			if (paddr) {
55941908Skarels 				*paddr = m;
56035384Skarels 				so->so_rcv.sb_mb = m->m_next;
56135384Skarels 				m->m_next = 0;
56235384Skarels 				m = so->so_rcv.sb_mb;
56325629Skarels 			} else {
56426958Skarels 				MFREE(m, so->so_rcv.sb_mb);
56526958Skarels 				m = so->so_rcv.sb_mb;
56625629Skarels 			}
56716993Skarels 		}
56816993Skarels 	}
56941908Skarels 	while (m && m->m_type == MT_CONTROL && error == 0) {
57016993Skarels 		if (flags & MSG_PEEK) {
57135384Skarels 			if (controlp)
57235384Skarels 				*controlp = m_copy(m, 0, m->m_len);
57335384Skarels 			m = m->m_next;
57435384Skarels 		} else {
57535384Skarels 			sbfree(&so->so_rcv, m);
57635384Skarels 			if (controlp) {
57743097Ssklower 				if (pr->pr_domain->dom_externalize &&
57843097Ssklower 				    mtod(m, struct cmsghdr *)->cmsg_type ==
57943097Ssklower 				    SCM_RIGHTS)
58041908Skarels 				   error = (*pr->pr_domain->dom_externalize)(m);
58135384Skarels 				*controlp = m;
58235384Skarels 				so->so_rcv.sb_mb = m->m_next;
58335384Skarels 				m->m_next = 0;
58435384Skarels 				m = so->so_rcv.sb_mb;
58535384Skarels 			} else {
58635384Skarels 				MFREE(m, so->so_rcv.sb_mb);
58735384Skarels 				m = so->so_rcv.sb_mb;
58835384Skarels 			}
58935384Skarels 		}
59041908Skarels 		if (controlp)
59141908Skarels 			controlp = &(*controlp)->m_next;
59235384Skarels 	}
59341908Skarels 	if (m) {
594*44383Skarels 		if ((flags & MSG_PEEK) == 0)
595*44383Skarels 			m->m_nextpkt = nextrecord;
59641908Skarels 		type = m->m_type;
59741908Skarels 	}
5988319Sroot 	moff = 0;
59932092Skarels 	offset = 0;
60043417Skarels 	while (m && m->m_type == type && uio->uio_resid > 0 && error == 0) {
60135384Skarels 		if (m->m_type == MT_OOBDATA)
60235384Skarels 			flags |= MSG_OOB;
60341908Skarels #ifdef DIAGNOSTIC
60435384Skarels 		else if (m->m_type != MT_DATA && m->m_type != MT_HEADER)
60525629Skarels 			panic("receive 3");
60641908Skarels #endif
60741908Skarels 		type = m->m_type;
6087747Sroot 		so->so_state &= ~SS_RCVATMARK;
60943417Skarels 		len = uio->uio_resid;
61032092Skarels 		if (so->so_oobmark && len > so->so_oobmark - offset)
61132092Skarels 			len = so->so_oobmark - offset;
61221767Skarels 		if (len > m->m_len - moff)
6138319Sroot 			len = m->m_len - moff;
61441908Skarels 		/*
61541908Skarels 		 * If mp is set, just pass back the mbufs.
61641908Skarels 		 * Otherwise copy them out via the uio, then free.
61741908Skarels 		 * Sockbuf must be consistent here (points to current mbuf,
61841908Skarels 		 * it points to next record) when we drop priority;
61941908Skarels 		 * we must note any additions to the sockbuf when we
62041908Skarels 		 * block interrupts again.
62141908Skarels 		 */
62241908Skarels 		if (mp == 0) {
62341908Skarels 			splx(s);
62441908Skarels 			error = uiomove(mtod(m, caddr_t) + moff, (int)len, uio);
62541908Skarels 			s = splnet();
62643417Skarels 		} else
62743417Skarels 			uio->uio_resid -= len;
62821767Skarels 		if (len == m->m_len - moff) {
62941908Skarels 			if (m->m_flags & M_EOR)
63041908Skarels 				flags |= MSG_EOR;
63125629Skarels 			if (flags & MSG_PEEK) {
63225629Skarels 				m = m->m_next;
63325629Skarels 				moff = 0;
63425629Skarels 			} else {
63535384Skarels 				nextrecord = m->m_nextpkt;
63625629Skarels 				sbfree(&so->so_rcv, m);
63741908Skarels 				if (mp) {
63841908Skarels 					*mp = m;
63941908Skarels 					mp = &m->m_next;
64043417Skarels 					so->so_rcv.sb_mb = m = m->m_next;
64143417Skarels 					*mp = (struct mbuf *)0;
64241908Skarels 				} else {
64341908Skarels 					MFREE(m, so->so_rcv.sb_mb);
64441908Skarels 					m = so->so_rcv.sb_mb;
64541908Skarels 				}
64626958Skarels 				if (m)
64735384Skarels 					m->m_nextpkt = nextrecord;
64825629Skarels 			}
6494786Swnj 		} else {
65012757Ssam 			if (flags & MSG_PEEK)
6518319Sroot 				moff += len;
6528319Sroot 			else {
65343417Skarels 				if (mp)
65443417Skarels 					*mp = m_copym(m, 0, len, M_WAIT);
65535384Skarels 				m->m_data += len;
6568319Sroot 				m->m_len -= len;
6578319Sroot 				so->so_rcv.sb_cc -= len;
6588319Sroot 			}
6594786Swnj 		}
66032092Skarels 		if (so->so_oobmark) {
66132092Skarels 			if ((flags & MSG_PEEK) == 0) {
66232092Skarels 				so->so_oobmark -= len;
66332092Skarels 				if (so->so_oobmark == 0) {
66432092Skarels 					so->so_state |= SS_RCVATMARK;
66532092Skarels 					break;
66632092Skarels 				}
66732092Skarels 			} else
66832092Skarels 				offset += len;
6697747Sroot 		}
67041908Skarels 		if (flags & MSG_EOR)
67140632Skarels 			break;
67241908Skarels 		/*
67341908Skarels 		 * If the MSG_WAITALL flag is set (for non-atomic socket),
67443417Skarels 		 * we must not quit until "uio->uio_resid == 0" or an error
67541908Skarels 		 * termination.  If a signal/timeout occurs, return
67643417Skarels 		 * with a short count but without error.
67741908Skarels 		 * Keep sockbuf locked against other readers.
67841908Skarels 		 */
67943417Skarels 		while (flags & MSG_WAITALL && m == 0 && uio->uio_resid > 0 &&
68041908Skarels 		    !sosendallatonce(so)) {
68141908Skarels 			error = sbwait(&so->so_rcv);
68241908Skarels 			if (error) {
68341908Skarels 				sbunlock(&so->so_rcv);
68441908Skarels 				splx(s);
68541908Skarels 				return (0);
68641908Skarels 			}
68741908Skarels 			if (m = so->so_rcv.sb_mb)
68841908Skarels 				nextrecord = m->m_nextpkt;
68941908Skarels 			if (so->so_error || so->so_state & SS_CANTRCVMORE)
69041908Skarels 				break;
69141908Skarels 			continue;
69241908Skarels 		}
69316993Skarels 	}
69416993Skarels 	if ((flags & MSG_PEEK) == 0) {
69526500Skarels 		if (m == 0)
69616993Skarels 			so->so_rcv.sb_mb = nextrecord;
69735384Skarels 		else if (pr->pr_flags & PR_ATOMIC) {
69835384Skarels 			flags |= MSG_TRUNC;
69926958Skarels 			(void) sbdroprecord(&so->so_rcv);
70035384Skarels 		}
70116993Skarels 		if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
70216993Skarels 			(*pr->pr_usrreq)(so, PRU_RCVD, (struct mbuf *)0,
70337478Ssklower 			    (struct mbuf *)flags, (struct mbuf *)0,
70437478Ssklower 			    (struct mbuf *)0);
70516993Skarels 	}
70635384Skarels 	if (flagsp)
70735384Skarels 		*flagsp |= flags;
7084890Swnj release:
7094916Swnj 	sbunlock(&so->so_rcv);
7104890Swnj 	splx(s);
7114916Swnj 	return (error);
7124786Swnj }
7134786Swnj 
71410267Ssam soshutdown(so, how)
71512757Ssam 	register struct socket *so;
71612757Ssam 	register int how;
71710267Ssam {
71812757Ssam 	register struct protosw *pr = so->so_proto;
71910267Ssam 
72010267Ssam 	how++;
72112757Ssam 	if (how & FREAD)
72212757Ssam 		sorflush(so);
72310267Ssam 	if (how & FWRITE)
72412757Ssam 		return ((*pr->pr_usrreq)(so, PRU_SHUTDOWN,
72512757Ssam 		    (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0));
72610267Ssam 	return (0);
72710267Ssam }
72810267Ssam 
72912757Ssam sorflush(so)
73012757Ssam 	register struct socket *so;
73112757Ssam {
73212757Ssam 	register struct sockbuf *sb = &so->so_rcv;
73312757Ssam 	register struct protosw *pr = so->so_proto;
73412757Ssam 	register int s;
73512757Ssam 	struct sockbuf asb;
73612757Ssam 
73740706Skarels 	sb->sb_flags |= SB_NOINTR;
73840706Skarels 	(void) sblock(sb);
73912757Ssam 	s = splimp();
74012757Ssam 	socantrcvmore(so);
74112757Ssam 	sbunlock(sb);
74212757Ssam 	asb = *sb;
74312757Ssam 	bzero((caddr_t)sb, sizeof (*sb));
74412757Ssam 	splx(s);
74516993Skarels 	if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose)
74616993Skarels 		(*pr->pr_domain->dom_dispose)(asb.sb_mb);
74712757Ssam 	sbrelease(&asb);
74812757Ssam }
74912757Ssam 
75018553Skarels sosetopt(so, level, optname, m0)
75112757Ssam 	register struct socket *so;
75210267Ssam 	int level, optname;
75318553Skarels 	struct mbuf *m0;
75410267Ssam {
75517158Ssam 	int error = 0;
75618553Skarels 	register struct mbuf *m = m0;
75710267Ssam 
75817158Ssam 	if (level != SOL_SOCKET) {
75918369Skarels 		if (so->so_proto && so->so_proto->pr_ctloutput)
76018369Skarels 			return ((*so->so_proto->pr_ctloutput)
76118553Skarels 				  (PRCO_SETOPT, so, level, optname, &m0));
76218369Skarels 		error = ENOPROTOOPT;
76318369Skarels 	} else {
76418369Skarels 		switch (optname) {
76510267Ssam 
76618369Skarels 		case SO_LINGER:
76718369Skarels 			if (m == NULL || m->m_len != sizeof (struct linger)) {
76818369Skarels 				error = EINVAL;
76918369Skarels 				goto bad;
77018369Skarels 			}
77118369Skarels 			so->so_linger = mtod(m, struct linger *)->l_linger;
77218369Skarels 			/* fall thru... */
77317158Ssam 
77418369Skarels 		case SO_DEBUG:
77518369Skarels 		case SO_KEEPALIVE:
77618369Skarels 		case SO_DONTROUTE:
77718369Skarels 		case SO_USELOOPBACK:
77818369Skarels 		case SO_BROADCAST:
77918369Skarels 		case SO_REUSEADDR:
78027191Skarels 		case SO_OOBINLINE:
78118369Skarels 			if (m == NULL || m->m_len < sizeof (int)) {
78218369Skarels 				error = EINVAL;
78318369Skarels 				goto bad;
78418369Skarels 			}
78518369Skarels 			if (*mtod(m, int *))
78618369Skarels 				so->so_options |= optname;
78718369Skarels 			else
78818369Skarels 				so->so_options &= ~optname;
78918369Skarels 			break;
79018369Skarels 
79118369Skarels 		case SO_SNDBUF:
79241908Skarels 		case SO_RCVBUF:
79340706Skarels 		case SO_SNDLOWAT:
79441908Skarels 		case SO_RCVLOWAT:
79540706Skarels 		case SO_SNDTIMEO:
79618369Skarels 		case SO_RCVTIMEO:
79718369Skarels 			if (m == NULL || m->m_len < sizeof (int)) {
79818369Skarels 				error = EINVAL;
79918369Skarels 				goto bad;
80018369Skarels 			}
80118369Skarels 			switch (optname) {
80218369Skarels 
80318369Skarels 			case SO_SNDBUF:
80418369Skarels 			case SO_RCVBUF:
80541908Skarels 				if (sbreserve(optname == SO_SNDBUF ?
80641908Skarels 				    &so->so_snd : &so->so_rcv,
80741908Skarels 				    (u_long) *mtod(m, int *)) == 0) {
80818369Skarels 					error = ENOBUFS;
80918369Skarels 					goto bad;
81018369Skarels 				}
81118369Skarels 				break;
81218369Skarels 
81318369Skarels 			case SO_SNDLOWAT:
81441908Skarels 				so->so_snd.sb_lowat = *mtod(m, int *);
81541908Skarels 				break;
81618369Skarels 			case SO_RCVLOWAT:
81741908Skarels 				so->so_rcv.sb_lowat = *mtod(m, int *);
81818369Skarels 				break;
81918369Skarels 			case SO_SNDTIMEO:
82041908Skarels 				so->so_snd.sb_timeo = *mtod(m, int *);
82141908Skarels 				break;
82218369Skarels 			case SO_RCVTIMEO:
82341908Skarels 				so->so_rcv.sb_timeo = *mtod(m, int *);
82418369Skarels 				break;
82518369Skarels 			}
82618369Skarels 			break;
82718369Skarels 
82818369Skarels 		default:
82918369Skarels 			error = ENOPROTOOPT;
83018369Skarels 			break;
83117158Ssam 		}
83210267Ssam 	}
83317158Ssam bad:
83417158Ssam 	if (m)
83517158Ssam 		(void) m_free(m);
83617158Ssam 	return (error);
83710267Ssam }
83810267Ssam 
83917158Ssam sogetopt(so, level, optname, mp)
84012757Ssam 	register struct socket *so;
84110267Ssam 	int level, optname;
84217158Ssam 	struct mbuf **mp;
84317158Ssam {
84412757Ssam 	register struct mbuf *m;
84510267Ssam 
84618369Skarels 	if (level != SOL_SOCKET) {
84718369Skarels 		if (so->so_proto && so->so_proto->pr_ctloutput) {
84818369Skarels 			return ((*so->so_proto->pr_ctloutput)
84918369Skarels 				  (PRCO_GETOPT, so, level, optname, mp));
85018369Skarels 		} else
85118369Skarels 			return (ENOPROTOOPT);
85218369Skarels 	} else {
85317158Ssam 		m = m_get(M_WAIT, MT_SOOPTS);
85425502Skarels 		m->m_len = sizeof (int);
85525502Skarels 
85618369Skarels 		switch (optname) {
85717158Ssam 
85818369Skarels 		case SO_LINGER:
85918369Skarels 			m->m_len = sizeof (struct linger);
86018369Skarels 			mtod(m, struct linger *)->l_onoff =
86118369Skarels 				so->so_options & SO_LINGER;
86218369Skarels 			mtod(m, struct linger *)->l_linger = so->so_linger;
86318369Skarels 			break;
86410267Ssam 
86518369Skarels 		case SO_USELOOPBACK:
86618369Skarels 		case SO_DONTROUTE:
86718369Skarels 		case SO_DEBUG:
86818369Skarels 		case SO_KEEPALIVE:
86918369Skarels 		case SO_REUSEADDR:
87018369Skarels 		case SO_BROADCAST:
87127191Skarels 		case SO_OOBINLINE:
87218369Skarels 			*mtod(m, int *) = so->so_options & optname;
87318369Skarels 			break;
87418369Skarels 
87525502Skarels 		case SO_TYPE:
87625502Skarels 			*mtod(m, int *) = so->so_type;
87725502Skarels 			break;
87825502Skarels 
87924768Skarels 		case SO_ERROR:
88024768Skarels 			*mtod(m, int *) = so->so_error;
88124768Skarels 			so->so_error = 0;
88224768Skarels 			break;
88324768Skarels 
88418369Skarels 		case SO_SNDBUF:
88518369Skarels 			*mtod(m, int *) = so->so_snd.sb_hiwat;
88618369Skarels 			break;
88718369Skarels 
88818369Skarels 		case SO_RCVBUF:
88918369Skarels 			*mtod(m, int *) = so->so_rcv.sb_hiwat;
89018369Skarels 			break;
89118369Skarels 
89218369Skarels 		case SO_SNDLOWAT:
89318369Skarels 			*mtod(m, int *) = so->so_snd.sb_lowat;
89418369Skarels 			break;
89518369Skarels 
89618369Skarels 		case SO_RCVLOWAT:
89718369Skarels 			*mtod(m, int *) = so->so_rcv.sb_lowat;
89818369Skarels 			break;
89918369Skarels 
90018369Skarels 		case SO_SNDTIMEO:
90118369Skarels 			*mtod(m, int *) = so->so_snd.sb_timeo;
90218369Skarels 			break;
90318369Skarels 
90418369Skarels 		case SO_RCVTIMEO:
90518369Skarels 			*mtod(m, int *) = so->so_rcv.sb_timeo;
90618369Skarels 			break;
90718369Skarels 
90818369Skarels 		default:
90926362Skarels 			(void)m_free(m);
91018369Skarels 			return (ENOPROTOOPT);
91118369Skarels 		}
91218369Skarels 		*mp = m;
91318369Skarels 		return (0);
91410267Ssam 	}
91510267Ssam }
91610267Ssam 
9175423Swnj sohasoutofband(so)
91812757Ssam 	register struct socket *so;
9195423Swnj {
92023233Skarels 	struct proc *p;
9215423Swnj 
92237478Ssklower 	if (so->so_pgid < 0)
92337478Ssklower 		gsignal(-so->so_pgid, SIGURG);
92437478Ssklower 	else if (so->so_pgid > 0 && (p = pfind(so->so_pgid)) != 0)
92523233Skarels 		psignal(p, SIGURG);
92624768Skarels 	if (so->so_rcv.sb_sel) {
92724768Skarels 		selwakeup(so->so_rcv.sb_sel, so->so_rcv.sb_flags & SB_COLL);
92824768Skarels 		so->so_rcv.sb_sel = 0;
92924768Skarels 		so->so_rcv.sb_flags &= ~SB_COLL;
93024768Skarels 	}
9315423Swnj }
932