xref: /csrg-svn/sys/kern/uipc_socket2.c (revision 8301)
1*8301Sroot /*	uipc_socket2.c	4.26	82/10/03	*/
24903Swnj 
34903Swnj #include "../h/param.h"
44903Swnj #include "../h/systm.h"
54903Swnj #include "../h/dir.h"
64903Swnj #include "../h/user.h"
74903Swnj #include "../h/proc.h"
84903Swnj #include "../h/file.h"
94903Swnj #include "../h/inode.h"
104903Swnj #include "../h/buf.h"
114903Swnj #include "../h/mbuf.h"
124903Swnj #include "../h/protosw.h"
134903Swnj #include "../h/socket.h"
144903Swnj #include "../h/socketvar.h"
155096Swnj #include "../net/in.h"
165096Swnj #include "../net/in_systm.h"
174903Swnj 
184903Swnj /*
194903Swnj  * Primitive routines for operating on sockets and socket buffers
204903Swnj  */
214903Swnj 
224903Swnj /*
234903Swnj  * Procedures to manipulate state flags of socket
247509Sroot  * and do appropriate wakeups.  Normal sequence from the
257509Sroot  * active (originating) side is that soisconnecting() is
267509Sroot  * called during processing of connect() call,
275169Swnj  * resulting in an eventual call to soisconnected() if/when the
285169Swnj  * connection is established.  When the connection is torn down
295169Swnj  * soisdisconnecting() is called during processing of disconnect() call,
305169Swnj  * and soisdisconnected() is called when the connection to the peer
315169Swnj  * is totally severed.  The semantics of these routines are such that
325169Swnj  * connectionless protocols can call soisconnected() and soisdisconnected()
335169Swnj  * only, bypassing the in-progress calls when setting up a ``connection''
345169Swnj  * takes no time.
355169Swnj  *
367509Sroot  * From the passive side, a socket is created with SO_ACCEPTCONN
377509Sroot  * creating two queues of sockets: so_q0 for connections in progress
387509Sroot  * and so_q for connections already made and awaiting user acceptance.
397509Sroot  * As a protocol is preparing incoming connections, it creates a socket
407509Sroot  * structure queued on so_q0 by calling sonewconn().  When the connection
417509Sroot  * is established, soisconnected() is called, and transfers the
427509Sroot  * socket structure to so_q, making it available to accept().
437509Sroot  *
447509Sroot  * If a SO_ACCEPTCONN socket is closed with sockets on either
457509Sroot  * so_q0 or so_q, these sockets are dropped.
467509Sroot  *
477509Sroot  * If and when higher level protocols are implemented in
485169Swnj  * the kernel, the wakeups done here will sometimes
495169Swnj  * be implemented as software-interrupt process scheduling.
504903Swnj  */
515169Swnj 
524903Swnj soisconnecting(so)
534903Swnj 	struct socket *so;
544903Swnj {
554903Swnj 
564903Swnj 	so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
574903Swnj 	so->so_state |= SS_ISCONNECTING;
584903Swnj 	wakeup((caddr_t)&so->so_timeo);
594903Swnj }
604903Swnj 
614903Swnj soisconnected(so)
624903Swnj 	struct socket *so;
634903Swnj {
647509Sroot 	register struct socket *head = so->so_head;
654903Swnj 
667509Sroot 	if (head) {
677509Sroot 		if (soqremque(so, 0) == 0)
687509Sroot 			panic("soisconnected");
697509Sroot 		soqinsque(head, so, 1);
707509Sroot 		wakeup((caddr_t)&head->so_timeo);
717509Sroot 	}
724903Swnj 	so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING);
734903Swnj 	so->so_state |= SS_ISCONNECTED;
744903Swnj 	wakeup((caddr_t)&so->so_timeo);
755578Swnj 	sorwakeup(so);
765578Swnj 	sowwakeup(so);
774903Swnj }
784903Swnj 
794903Swnj soisdisconnecting(so)
804903Swnj 	struct socket *so;
814903Swnj {
824903Swnj 
835248Sroot 	so->so_state &= ~SS_ISCONNECTING;
844903Swnj 	so->so_state |= (SS_ISDISCONNECTING|SS_CANTRCVMORE|SS_CANTSENDMORE);
854903Swnj 	wakeup((caddr_t)&so->so_timeo);
865170Swnj 	sowwakeup(so);
875169Swnj 	sorwakeup(so);
884903Swnj }
894903Swnj 
904903Swnj soisdisconnected(so)
914903Swnj 	struct socket *so;
924903Swnj {
934903Swnj 
944903Swnj 	so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
954903Swnj 	so->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE);
964903Swnj 	wakeup((caddr_t)&so->so_timeo);
974903Swnj 	sowwakeup(so);
984903Swnj 	sorwakeup(so);
994903Swnj }
1004903Swnj 
1015169Swnj /*
1027509Sroot  * When an attempt at a new connection is noted on a socket
1037509Sroot  * which accepts connections, sonewconn is called.  If the
1047509Sroot  * connection is possible (subject to space constraints, etc.)
1057509Sroot  * then we allocate a new structure, propoerly linked into the
1067509Sroot  * data structure of the original socket, and return this.
1077509Sroot  */
1087509Sroot struct socket *
1097509Sroot sonewconn(head)
1107509Sroot 	register struct socket *head;
1117509Sroot {
1127509Sroot 	register struct socket *so;
1137509Sroot 	struct mbuf *m;
1147509Sroot 
1157509Sroot 	if (head->so_qlen + head->so_q0len > 3 * head->so_qlimit / 2)
1167509Sroot 		goto bad;
1177509Sroot 	m = m_getclr(M_DONTWAIT);
1187509Sroot 	if (m == 0)
1197509Sroot 		goto bad;
1207509Sroot 	so = mtod(m, struct socket *);
1217509Sroot 	so->so_type = head->so_type;
1227509Sroot 	so->so_options = head->so_options &~ SO_ACCEPTCONN;
1237509Sroot 	so->so_linger = head->so_linger;
1247509Sroot 	so->so_state = head->so_state;
1257509Sroot 	so->so_proto = head->so_proto;
1267509Sroot 	so->so_timeo = head->so_timeo;
1277509Sroot 	so->so_pgrp = head->so_pgrp;
1287509Sroot 	soqinsque(head, so, 0);
129*8301Sroot 	if ((*so->so_proto->pr_usrreq)(so, PRU_ATTACH, 0, 0, 0)) {
1307509Sroot 		(void) soqremque(so, 0);
1317509Sroot 		m_free(m);
1327509Sroot 		goto bad;
1337509Sroot 	}
1347509Sroot 	return (so);
1357509Sroot bad:
1367509Sroot 	return ((struct socket *)0);
1377509Sroot }
1387509Sroot 
1397509Sroot soqinsque(head, so, q)
1407509Sroot 	register struct socket *head, *so;
1417509Sroot 	int q;
1427509Sroot {
1437509Sroot 
1447509Sroot 	so->so_head = head;
1457509Sroot 	if (q == 0) {
1467509Sroot 		head->so_q0len++;
1477509Sroot 		so->so_q0 = head->so_q0;
1487509Sroot 		head->so_q0 = so;
1497509Sroot 	} else {
1507509Sroot 		head->so_qlen++;
1517509Sroot 		so->so_q = head->so_q;
1527509Sroot 		head->so_q = so;
1537509Sroot 	}
1547509Sroot }
1557509Sroot 
1567509Sroot soqremque(so, q)
1577509Sroot 	register struct socket *so;
1587509Sroot 	int q;
1597509Sroot {
1607509Sroot 	register struct socket *head, *prev, *next;
1617509Sroot 
1627509Sroot 	head = so->so_head;
1637509Sroot 	prev = head;
1647509Sroot 	for (;;) {
1657509Sroot 		next = q ? prev->so_q : prev->so_q0;
1667509Sroot 		if (next == so)
1677509Sroot 			break;
1687509Sroot 		if (next == head)
1697509Sroot 			return (0);
1707509Sroot 		prev = next;
1717509Sroot 	}
1727509Sroot 	if (q == 0) {
1737509Sroot 		prev->so_q0 = next->so_q0;
1747509Sroot 		head->so_q0len--;
1757509Sroot 	} else {
1767509Sroot 		prev->so_q = next->so_q;
1777509Sroot 		head->so_qlen--;
1787509Sroot 	}
1797509Sroot 	next->so_q0 = next->so_q = 0;
1807509Sroot 	next->so_head = 0;
1817509Sroot 	return (1);
1827509Sroot }
1837509Sroot 
1847509Sroot /*
1855169Swnj  * Socantsendmore indicates that no more data will be sent on the
1865169Swnj  * socket; it would normally be applied to a socket when the user
1875169Swnj  * informs the system that no more data is to be sent, by the protocol
1885169Swnj  * code (in case PRU_SHUTDOWN).  Socantrcvmore indicates that no more data
1895169Swnj  * will be received, and will normally be applied to the socket by a
1905169Swnj  * protocol when it detects that the peer will send no more data.
1915169Swnj  * Data queued for reading in the socket may yet be read.
1925169Swnj  */
1935169Swnj 
1944917Swnj socantsendmore(so)
1954917Swnj 	struct socket *so;
1964917Swnj {
1974917Swnj 
1984917Swnj 	so->so_state |= SS_CANTSENDMORE;
1994917Swnj 	sowwakeup(so);
2004917Swnj }
2014917Swnj 
2024917Swnj socantrcvmore(so)
2034917Swnj 	struct socket *so;
2044917Swnj {
2054917Swnj 
2064917Swnj 	so->so_state |= SS_CANTRCVMORE;
2074917Swnj 	sorwakeup(so);
2084917Swnj }
2094917Swnj 
2104903Swnj /*
2115169Swnj  * Socket select/wakeup routines.
2124903Swnj  */
2135169Swnj 
2145169Swnj /*
2155169Swnj  * Interface routine to select() system
2165169Swnj  * call for sockets.
2175169Swnj  */
2185577Swnj soselect(so, rw)
2194903Swnj 	register struct socket *so;
2205577Swnj 	int rw;
2214903Swnj {
2225578Swnj 	int s = splnet();
2234903Swnj 
2245577Swnj 	switch (rw) {
2255577Swnj 
2265577Swnj 	case FREAD:
2275578Swnj 		if (soreadable(so)) {
2285578Swnj 			splx(s);
2294903Swnj 			return (1);
2305578Swnj 		}
2314903Swnj 		sbselqueue(&so->so_rcv);
2325577Swnj 		break;
2335577Swnj 
2345577Swnj 	case FWRITE:
2355578Swnj 		if (sowriteable(so)) {
2365578Swnj 			splx(s);
2374903Swnj 			return (1);
2385578Swnj 		}
2394903Swnj 		sbselqueue(&so->so_snd);
2405577Swnj 		break;
2414903Swnj 	}
2425578Swnj 	splx(s);
2434903Swnj 	return (0);
2444903Swnj }
2454903Swnj 
2464903Swnj /*
2474903Swnj  * Queue a process for a select on a socket buffer.
2484903Swnj  */
2494903Swnj sbselqueue(sb)
2504903Swnj 	struct sockbuf *sb;
2514903Swnj {
2524903Swnj 	register struct proc *p;
2534903Swnj 
2544917Swnj 	if ((p = sb->sb_sel) && p->p_wchan == (caddr_t)&selwait)
2554903Swnj 		sb->sb_flags |= SB_COLL;
2564903Swnj 	else
2574903Swnj 		sb->sb_sel = u.u_procp;
2584903Swnj }
2594903Swnj 
2604903Swnj /*
2614917Swnj  * Wait for data to arrive at/drain from a socket buffer.
2624917Swnj  */
2634917Swnj sbwait(sb)
2644917Swnj 	struct sockbuf *sb;
2654917Swnj {
2664917Swnj 
2674917Swnj 	sb->sb_flags |= SB_WAIT;
2684917Swnj 	sleep((caddr_t)&sb->sb_cc, PZERO+1);
2694917Swnj }
2704917Swnj 
2714917Swnj /*
2724903Swnj  * Wakeup processes waiting on a socket buffer.
2734903Swnj  */
2744903Swnj sbwakeup(sb)
2754903Swnj 	struct sockbuf *sb;
2764903Swnj {
2774903Swnj 
2784903Swnj 	if (sb->sb_sel) {
2794903Swnj 		selwakeup(sb->sb_sel, sb->sb_flags & SB_COLL);
2804903Swnj 		sb->sb_sel = 0;
2814903Swnj 		sb->sb_flags &= ~SB_COLL;
2824903Swnj 	}
2834903Swnj 	if (sb->sb_flags & SB_WAIT) {
2844903Swnj 		sb->sb_flags &= ~SB_WAIT;
2855013Swnj 		wakeup((caddr_t)&sb->sb_cc);
2864903Swnj 	}
2874903Swnj }
2884903Swnj 
2894903Swnj /*
2905169Swnj  * Socket buffer (struct sockbuf) utility routines.
2915169Swnj  *
2925169Swnj  * Each socket contains two socket buffers: one for sending data and
2935169Swnj  * one for receiving data.  Each buffer contains a queue of mbufs,
2945169Swnj  * information about the number of mbufs and amount of data in the
2955169Swnj  * queue, and other fields allowing select() statements and notification
2965169Swnj  * on data availability to be implemented.
2975169Swnj  *
2985169Swnj  * Before using a new socket structure it is first necessary to reserve
2995169Swnj  * buffer space to the socket, by calling sbreserve.  This commits
3005169Swnj  * some of the available buffer space in the system buffer pool for the
3015169Swnj  * socket.  The space should be released by calling sbrelease when the
3025169Swnj  * socket is destroyed.
3035169Swnj  *
3045169Swnj  * The routine sbappend() is normally called to append new mbufs
3055169Swnj  * to a socket buffer, after checking that adequate space is available
3065169Swnj  * comparing the function spspace() with the amount of data to be added.
3075169Swnj  * Data is normally removed from a socket buffer in a protocol by
3085169Swnj  * first calling m_copy on the socket buffer mbuf chain and sending this
3095169Swnj  * to a peer, and then removing the data from the socket buffer with
3105169Swnj  * sbdrop when the data is acknowledged by the peer (or immediately
3115170Swnj  * in the case of unreliable protocols.)
3125169Swnj  *
3135169Swnj  * Protocols which do not require connections place both source address
3145169Swnj  * and data information in socket buffer queues.  The source addresses
3155169Swnj  * are stored in single mbufs after each data item, and are easily found
3165169Swnj  * as the data items are all marked with end of record markers.  The
3175169Swnj  * sbappendaddr() routine stores a datum and associated address in
3185169Swnj  * a socket buffer.  Note that, unlike sbappend(), this routine checks
3195169Swnj  * for the caller that there will be enough space to store the data.
3205169Swnj  * It fails if there is not enough space, or if it cannot find
3215169Swnj  * a mbuf to store the address in.
3225169Swnj  *
3235169Swnj  * The higher-level routines sosend and soreceive (in socket.c)
3245170Swnj  * also add data to, and remove data from socket buffers repectively.
3255169Swnj  */
3265169Swnj 
3275169Swnj /*
3284903Swnj  * Allot mbufs to a sockbuf.
3294903Swnj  */
3304903Swnj sbreserve(sb, cc)
3314903Swnj 	struct sockbuf *sb;
3324903Swnj {
3334903Swnj 
3347181Swnj 	/* someday maybe this routine will fail... */
3354980Swnj 	sb->sb_hiwat = cc;
3365042Swnj 	sb->sb_mbmax = cc*2;
3374917Swnj 	return (1);
3384903Swnj }
3394903Swnj 
3404903Swnj /*
3414903Swnj  * Free mbufs held by a socket, and reserved mbuf space.
3424903Swnj  */
3434903Swnj sbrelease(sb)
3444903Swnj 	struct sockbuf *sb;
3454903Swnj {
3464903Swnj 
3474903Swnj 	sbflush(sb);
3484980Swnj 	sb->sb_hiwat = sb->sb_mbmax = 0;
3494903Swnj }
3504903Swnj 
3514903Swnj /*
3524903Swnj  * Routines to add (at the end) and remove (from the beginning)
3534903Swnj  * data from a mbuf queue.
3544903Swnj  */
3554903Swnj 
3564903Swnj /*
3574903Swnj  * Append mbuf queue m to sockbuf sb.
3584903Swnj  */
3594903Swnj sbappend(sb, m)
3604903Swnj 	register struct mbuf *m;
3614903Swnj 	register struct sockbuf *sb;
3624903Swnj {
3636092Sroot 	register struct mbuf *n;
3644903Swnj 
3656092Sroot 	n = sb->sb_mb;
3666092Sroot 	if (n)
3676092Sroot 		while (n->m_next)
3686092Sroot 			n = n->m_next;
3694903Swnj 	while (m) {
3705266Swnj 		if (m->m_len == 0 && (int)m->m_act == 0) {
3715304Sroot 			m = m_free(m);
3725266Swnj 			continue;
3735266Swnj 		}
3744903Swnj 		if (n && n->m_off <= MMAXOFF && m->m_off <= MMAXOFF &&
3754903Swnj 		   (int)n->m_act == 0 && (int)m->m_act == 0 &&
3765042Swnj 		   (n->m_off + n->m_len + m->m_len) <= MMAXOFF) {
3775042Swnj 			bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len,
3784917Swnj 			    (unsigned)m->m_len);
3794903Swnj 			n->m_len += m->m_len;
3804903Swnj 			sb->sb_cc += m->m_len;
3814903Swnj 			m = m_free(m);
3824903Swnj 			continue;
3834903Swnj 		}
3844903Swnj 		sballoc(sb, m);
3856092Sroot 		if (n == 0)
3866092Sroot 			sb->sb_mb = m;
3876092Sroot 		else
3886092Sroot 			n->m_next = m;
3894903Swnj 		n = m;
3904903Swnj 		m = m->m_next;
3916092Sroot 		n->m_next = 0;
3924903Swnj 	}
3934903Swnj }
3944903Swnj 
3955169Swnj /*
3965169Swnj  * Append data and address.
3975169Swnj  * Return 0 if no space in sockbuf or if
3985169Swnj  * can't get mbuf to stuff address in.
3995169Swnj  */
4004928Swnj sbappendaddr(sb, asa, m0)
4014928Swnj 	struct sockbuf *sb;
4024928Swnj 	struct sockaddr *asa;
4034928Swnj 	struct mbuf *m0;
4044928Swnj {
4054928Swnj 	struct sockaddr *msa;
4064928Swnj 	register struct mbuf *m;
4074928Swnj 	register int len = sizeof (struct sockaddr);
4084928Swnj 
4095042Swnj 	m = m0;
4105042Swnj 	if (m == 0)
4115042Swnj 		panic("sbappendaddr");
4125042Swnj 	for (;;) {
4134928Swnj 		len += m->m_len;
4145042Swnj 		if (m->m_next == 0) {
4155042Swnj 			m->m_act = (struct mbuf *)1;
4165042Swnj 			break;
4175042Swnj 		}
4185042Swnj 		m = m->m_next;
4195042Swnj 	}
4205043Swnj 	if (len > sbspace(sb))
4214928Swnj 		return (0);
4225586Sroot 	m = m_get(M_DONTWAIT);
4235043Swnj 	if (m == 0)
4244928Swnj 		return (0);
4254928Swnj 	m->m_off = MMINOFF;
4264928Swnj 	m->m_len = sizeof (struct sockaddr);
4274928Swnj 	msa = mtod(m, struct sockaddr *);
4284928Swnj 	*msa = *asa;
4294928Swnj 	m->m_act = (struct mbuf *)1;
4304928Swnj 	sbappend(sb, m);
4314928Swnj 	sbappend(sb, m0);
4324928Swnj 	return (1);
4334928Swnj }
4344928Swnj 
4354903Swnj /*
4364903Swnj  * Free all mbufs on a sockbuf mbuf chain.
4374903Swnj  * Check that resource allocations return to 0.
4384903Swnj  */
4394903Swnj sbflush(sb)
4404903Swnj 	struct sockbuf *sb;
4414903Swnj {
4424903Swnj 
4434903Swnj 	if (sb->sb_flags & SB_LOCK)
4444903Swnj 		panic("sbflush");
4455266Swnj 	if (sb->sb_cc)
4465266Swnj 		sbdrop(sb, sb->sb_cc);
4474903Swnj 	if (sb->sb_cc || sb->sb_mbcnt || sb->sb_mb)
4484903Swnj 		panic("sbflush 2");
4494903Swnj }
4504903Swnj 
4514903Swnj /*
4524903Swnj  * Drop data from (the front of) a sockbuf chain.
4534903Swnj  */
4544903Swnj sbdrop(sb, len)
4554903Swnj 	register struct sockbuf *sb;
4564903Swnj 	register int len;
4574903Swnj {
4584903Swnj 	register struct mbuf *m = sb->sb_mb, *mn;
4594903Swnj 
4604903Swnj 	while (len > 0) {
4614903Swnj 		if (m == 0)
4624903Swnj 			panic("sbdrop");
4635064Swnj 		if (m->m_len > len) {
4644903Swnj 			m->m_len -= len;
4654903Swnj 			m->m_off += len;
4664903Swnj 			sb->sb_cc -= len;
4674903Swnj 			break;
4684903Swnj 		}
4695064Swnj 		len -= m->m_len;
4705064Swnj 		sbfree(sb, m);
4715064Swnj 		MFREE(m, mn);
4725064Swnj 		m = mn;
4734903Swnj 	}
4744903Swnj 	sb->sb_mb = m;
4754903Swnj }
476