xref: /csrg-svn/sys/kern/uipc_socket2.c (revision 10204)
1*10204Ssam /*	uipc_socket2.c	4.34	83/01/08	*/
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"
154903Swnj 
164903Swnj /*
174903Swnj  * Primitive routines for operating on sockets and socket buffers
184903Swnj  */
194903Swnj 
204903Swnj /*
214903Swnj  * Procedures to manipulate state flags of socket
227509Sroot  * and do appropriate wakeups.  Normal sequence from the
237509Sroot  * active (originating) side is that soisconnecting() is
247509Sroot  * called during processing of connect() call,
255169Swnj  * resulting in an eventual call to soisconnected() if/when the
265169Swnj  * connection is established.  When the connection is torn down
275169Swnj  * soisdisconnecting() is called during processing of disconnect() call,
285169Swnj  * and soisdisconnected() is called when the connection to the peer
295169Swnj  * is totally severed.  The semantics of these routines are such that
305169Swnj  * connectionless protocols can call soisconnected() and soisdisconnected()
315169Swnj  * only, bypassing the in-progress calls when setting up a ``connection''
325169Swnj  * takes no time.
335169Swnj  *
347509Sroot  * From the passive side, a socket is created with SO_ACCEPTCONN
357509Sroot  * creating two queues of sockets: so_q0 for connections in progress
367509Sroot  * and so_q for connections already made and awaiting user acceptance.
377509Sroot  * As a protocol is preparing incoming connections, it creates a socket
387509Sroot  * structure queued on so_q0 by calling sonewconn().  When the connection
397509Sroot  * is established, soisconnected() is called, and transfers the
407509Sroot  * socket structure to so_q, making it available to accept().
417509Sroot  *
427509Sroot  * If a SO_ACCEPTCONN socket is closed with sockets on either
437509Sroot  * so_q0 or so_q, these sockets are dropped.
447509Sroot  *
457509Sroot  * If and when higher level protocols are implemented in
465169Swnj  * the kernel, the wakeups done here will sometimes
475169Swnj  * be implemented as software-interrupt process scheduling.
484903Swnj  */
495169Swnj 
504903Swnj soisconnecting(so)
514903Swnj 	struct socket *so;
524903Swnj {
534903Swnj 
544903Swnj 	so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
554903Swnj 	so->so_state |= SS_ISCONNECTING;
564903Swnj 	wakeup((caddr_t)&so->so_timeo);
574903Swnj }
584903Swnj 
594903Swnj soisconnected(so)
604903Swnj 	struct socket *so;
614903Swnj {
627509Sroot 	register struct socket *head = so->so_head;
634903Swnj 
647509Sroot 	if (head) {
657509Sroot 		if (soqremque(so, 0) == 0)
667509Sroot 			panic("soisconnected");
677509Sroot 		soqinsque(head, so, 1);
687509Sroot 		wakeup((caddr_t)&head->so_timeo);
697509Sroot 	}
704903Swnj 	so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING);
714903Swnj 	so->so_state |= SS_ISCONNECTED;
724903Swnj 	wakeup((caddr_t)&so->so_timeo);
735578Swnj 	sorwakeup(so);
745578Swnj 	sowwakeup(so);
754903Swnj }
764903Swnj 
774903Swnj soisdisconnecting(so)
784903Swnj 	struct socket *so;
794903Swnj {
804903Swnj 
815248Sroot 	so->so_state &= ~SS_ISCONNECTING;
824903Swnj 	so->so_state |= (SS_ISDISCONNECTING|SS_CANTRCVMORE|SS_CANTSENDMORE);
834903Swnj 	wakeup((caddr_t)&so->so_timeo);
845170Swnj 	sowwakeup(so);
855169Swnj 	sorwakeup(so);
864903Swnj }
874903Swnj 
884903Swnj soisdisconnected(so)
894903Swnj 	struct socket *so;
904903Swnj {
914903Swnj 
924903Swnj 	so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
934903Swnj 	so->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE);
944903Swnj 	wakeup((caddr_t)&so->so_timeo);
954903Swnj 	sowwakeup(so);
964903Swnj 	sorwakeup(so);
974903Swnj }
984903Swnj 
995169Swnj /*
1007509Sroot  * When an attempt at a new connection is noted on a socket
1017509Sroot  * which accepts connections, sonewconn is called.  If the
1027509Sroot  * connection is possible (subject to space constraints, etc.)
1037509Sroot  * then we allocate a new structure, propoerly linked into the
1047509Sroot  * data structure of the original socket, and return this.
1057509Sroot  */
1067509Sroot struct socket *
1077509Sroot sonewconn(head)
1087509Sroot 	register struct socket *head;
1097509Sroot {
1107509Sroot 	register struct socket *so;
1117509Sroot 	struct mbuf *m;
1127509Sroot 
1137509Sroot 	if (head->so_qlen + head->so_q0len > 3 * head->so_qlimit / 2)
1147509Sroot 		goto bad;
1159636Ssam 	m = m_getclr(M_DONTWAIT, MT_SOCKET);
11610138Ssam 	if (m == NULL)
1177509Sroot 		goto bad;
1187509Sroot 	so = mtod(m, struct socket *);
1197509Sroot 	so->so_type = head->so_type;
1207509Sroot 	so->so_options = head->so_options &~ SO_ACCEPTCONN;
1217509Sroot 	so->so_linger = head->so_linger;
122*10204Ssam 	so->so_state = head->so_state | SS_NOFDREF;
1237509Sroot 	so->so_proto = head->so_proto;
1247509Sroot 	so->so_timeo = head->so_timeo;
1257509Sroot 	so->so_pgrp = head->so_pgrp;
1267509Sroot 	soqinsque(head, so, 0);
12710138Ssam 	if ((*so->so_proto->pr_usrreq)(so, PRU_ATTACH, (struct mbuf *)0,
12810138Ssam 	  (struct mbuf *)0, (struct sockopt *)0)) {
1297509Sroot 		(void) soqremque(so, 0);
1308818Sroot 		(void) m_free(m);
1317509Sroot 		goto bad;
1327509Sroot 	}
1337509Sroot 	return (so);
1347509Sroot bad:
1357509Sroot 	return ((struct socket *)0);
1367509Sroot }
1377509Sroot 
1387509Sroot soqinsque(head, so, q)
1397509Sroot 	register struct socket *head, *so;
1407509Sroot 	int q;
1417509Sroot {
1427509Sroot 
1437509Sroot 	so->so_head = head;
1447509Sroot 	if (q == 0) {
1457509Sroot 		head->so_q0len++;
1467509Sroot 		so->so_q0 = head->so_q0;
1477509Sroot 		head->so_q0 = so;
1487509Sroot 	} else {
1497509Sroot 		head->so_qlen++;
1507509Sroot 		so->so_q = head->so_q;
1517509Sroot 		head->so_q = so;
1527509Sroot 	}
1537509Sroot }
1547509Sroot 
1557509Sroot soqremque(so, q)
1567509Sroot 	register struct socket *so;
1577509Sroot 	int q;
1587509Sroot {
1597509Sroot 	register struct socket *head, *prev, *next;
1607509Sroot 
1617509Sroot 	head = so->so_head;
1627509Sroot 	prev = head;
1637509Sroot 	for (;;) {
1647509Sroot 		next = q ? prev->so_q : prev->so_q0;
1657509Sroot 		if (next == so)
1667509Sroot 			break;
1677509Sroot 		if (next == head)
1687509Sroot 			return (0);
1697509Sroot 		prev = next;
1707509Sroot 	}
1717509Sroot 	if (q == 0) {
1727509Sroot 		prev->so_q0 = next->so_q0;
1737509Sroot 		head->so_q0len--;
1747509Sroot 	} else {
1757509Sroot 		prev->so_q = next->so_q;
1767509Sroot 		head->so_qlen--;
1777509Sroot 	}
1787509Sroot 	next->so_q0 = next->so_q = 0;
1797509Sroot 	next->so_head = 0;
1807509Sroot 	return (1);
1817509Sroot }
1827509Sroot 
1837509Sroot /*
1845169Swnj  * Socantsendmore indicates that no more data will be sent on the
1855169Swnj  * socket; it would normally be applied to a socket when the user
1865169Swnj  * informs the system that no more data is to be sent, by the protocol
1875169Swnj  * code (in case PRU_SHUTDOWN).  Socantrcvmore indicates that no more data
1885169Swnj  * will be received, and will normally be applied to the socket by a
1895169Swnj  * protocol when it detects that the peer will send no more data.
1905169Swnj  * Data queued for reading in the socket may yet be read.
1915169Swnj  */
1925169Swnj 
1934917Swnj socantsendmore(so)
1944917Swnj 	struct socket *so;
1954917Swnj {
1964917Swnj 
1974917Swnj 	so->so_state |= SS_CANTSENDMORE;
1984917Swnj 	sowwakeup(so);
1994917Swnj }
2004917Swnj 
2014917Swnj socantrcvmore(so)
2024917Swnj 	struct socket *so;
2034917Swnj {
2044917Swnj 
2054917Swnj 	so->so_state |= SS_CANTRCVMORE;
2064917Swnj 	sorwakeup(so);
2074917Swnj }
2084917Swnj 
2094903Swnj /*
2105169Swnj  * Socket select/wakeup routines.
2114903Swnj  */
2125169Swnj 
2135169Swnj /*
2145169Swnj  * Interface routine to select() system
2155169Swnj  * call for sockets.
2165169Swnj  */
2175577Swnj soselect(so, rw)
2184903Swnj 	register struct socket *so;
2195577Swnj 	int rw;
2204903Swnj {
2215578Swnj 	int s = splnet();
2224903Swnj 
2235577Swnj 	switch (rw) {
2245577Swnj 
2255577Swnj 	case FREAD:
2265578Swnj 		if (soreadable(so)) {
2275578Swnj 			splx(s);
2284903Swnj 			return (1);
2295578Swnj 		}
2304903Swnj 		sbselqueue(&so->so_rcv);
2315577Swnj 		break;
2325577Swnj 
2335577Swnj 	case FWRITE:
2345578Swnj 		if (sowriteable(so)) {
2355578Swnj 			splx(s);
2364903Swnj 			return (1);
2375578Swnj 		}
2384903Swnj 		sbselqueue(&so->so_snd);
2395577Swnj 		break;
2404903Swnj 	}
2415578Swnj 	splx(s);
2424903Swnj 	return (0);
2434903Swnj }
2444903Swnj 
2454903Swnj /*
2464903Swnj  * Queue a process for a select on a socket buffer.
2474903Swnj  */
2484903Swnj sbselqueue(sb)
2494903Swnj 	struct sockbuf *sb;
2504903Swnj {
2514903Swnj 	register struct proc *p;
2524903Swnj 
2534917Swnj 	if ((p = sb->sb_sel) && p->p_wchan == (caddr_t)&selwait)
2544903Swnj 		sb->sb_flags |= SB_COLL;
2554903Swnj 	else
2564903Swnj 		sb->sb_sel = u.u_procp;
2574903Swnj }
2584903Swnj 
2594903Swnj /*
2604917Swnj  * Wait for data to arrive at/drain from a socket buffer.
2614917Swnj  */
2624917Swnj sbwait(sb)
2634917Swnj 	struct sockbuf *sb;
2644917Swnj {
2654917Swnj 
2664917Swnj 	sb->sb_flags |= SB_WAIT;
2674917Swnj 	sleep((caddr_t)&sb->sb_cc, PZERO+1);
2684917Swnj }
2694917Swnj 
2704917Swnj /*
2714903Swnj  * Wakeup processes waiting on a socket buffer.
2724903Swnj  */
2734903Swnj sbwakeup(sb)
2744903Swnj 	struct sockbuf *sb;
2754903Swnj {
2764903Swnj 
2774903Swnj 	if (sb->sb_sel) {
2784903Swnj 		selwakeup(sb->sb_sel, sb->sb_flags & SB_COLL);
2794903Swnj 		sb->sb_sel = 0;
2804903Swnj 		sb->sb_flags &= ~SB_COLL;
2814903Swnj 	}
2824903Swnj 	if (sb->sb_flags & SB_WAIT) {
2834903Swnj 		sb->sb_flags &= ~SB_WAIT;
2845013Swnj 		wakeup((caddr_t)&sb->sb_cc);
2854903Swnj 	}
2864903Swnj }
2874903Swnj 
2884903Swnj /*
2895169Swnj  * Socket buffer (struct sockbuf) utility routines.
2905169Swnj  *
2915169Swnj  * Each socket contains two socket buffers: one for sending data and
2925169Swnj  * one for receiving data.  Each buffer contains a queue of mbufs,
2935169Swnj  * information about the number of mbufs and amount of data in the
2945169Swnj  * queue, and other fields allowing select() statements and notification
2955169Swnj  * on data availability to be implemented.
2965169Swnj  *
2975169Swnj  * Before using a new socket structure it is first necessary to reserve
2985169Swnj  * buffer space to the socket, by calling sbreserve.  This commits
2995169Swnj  * some of the available buffer space in the system buffer pool for the
3005169Swnj  * socket.  The space should be released by calling sbrelease when the
3015169Swnj  * socket is destroyed.
3025169Swnj  *
3035169Swnj  * The routine sbappend() is normally called to append new mbufs
3045169Swnj  * to a socket buffer, after checking that adequate space is available
3055169Swnj  * comparing the function spspace() with the amount of data to be added.
3065169Swnj  * Data is normally removed from a socket buffer in a protocol by
3075169Swnj  * first calling m_copy on the socket buffer mbuf chain and sending this
3085169Swnj  * to a peer, and then removing the data from the socket buffer with
3095169Swnj  * sbdrop when the data is acknowledged by the peer (or immediately
3105170Swnj  * in the case of unreliable protocols.)
3115169Swnj  *
3125169Swnj  * Protocols which do not require connections place both source address
3135169Swnj  * and data information in socket buffer queues.  The source addresses
3145169Swnj  * are stored in single mbufs after each data item, and are easily found
3155169Swnj  * as the data items are all marked with end of record markers.  The
3165169Swnj  * sbappendaddr() routine stores a datum and associated address in
3175169Swnj  * a socket buffer.  Note that, unlike sbappend(), this routine checks
3185169Swnj  * for the caller that there will be enough space to store the data.
3195169Swnj  * It fails if there is not enough space, or if it cannot find
3205169Swnj  * a mbuf to store the address in.
3215169Swnj  *
3225169Swnj  * The higher-level routines sosend and soreceive (in socket.c)
3235170Swnj  * also add data to, and remove data from socket buffers repectively.
3245169Swnj  */
3255169Swnj 
3269027Sroot soreserve(so, sndcc, rcvcc)
3279027Sroot 	struct socket *so;
3289027Sroot 	int sndcc, rcvcc;
3299027Sroot {
3309027Sroot 
3319027Sroot 	if (sbreserve(&so->so_snd, sndcc) == 0)
3329027Sroot 		goto bad;
3339027Sroot 	if (sbreserve(&so->so_rcv, rcvcc) == 0)
3349027Sroot 		goto bad2;
3359027Sroot 	return (0);
3369027Sroot bad2:
3379027Sroot 	sbrelease(&so->so_snd);
3389027Sroot bad:
3399027Sroot 	return (ENOBUFS);
3409027Sroot }
3419027Sroot 
3425169Swnj /*
3434903Swnj  * Allot mbufs to a sockbuf.
3444903Swnj  */
3454903Swnj sbreserve(sb, cc)
3464903Swnj 	struct sockbuf *sb;
3474903Swnj {
3484903Swnj 
3497181Swnj 	/* someday maybe this routine will fail... */
3504980Swnj 	sb->sb_hiwat = cc;
35110138Ssam 	/* the 2 implies names can be no more than 1 mbuf each */
3525042Swnj 	sb->sb_mbmax = cc*2;
3534917Swnj 	return (1);
3544903Swnj }
3554903Swnj 
3564903Swnj /*
3574903Swnj  * Free mbufs held by a socket, and reserved mbuf space.
3584903Swnj  */
3594903Swnj sbrelease(sb)
3604903Swnj 	struct sockbuf *sb;
3614903Swnj {
3624903Swnj 
3634903Swnj 	sbflush(sb);
3644980Swnj 	sb->sb_hiwat = sb->sb_mbmax = 0;
3654903Swnj }
3664903Swnj 
3674903Swnj /*
3684903Swnj  * Routines to add (at the end) and remove (from the beginning)
3694903Swnj  * data from a mbuf queue.
3704903Swnj  */
3714903Swnj 
3724903Swnj /*
3734903Swnj  * Append mbuf queue m to sockbuf sb.
3744903Swnj  */
3754903Swnj sbappend(sb, m)
3764903Swnj 	register struct mbuf *m;
3774903Swnj 	register struct sockbuf *sb;
3784903Swnj {
3796092Sroot 	register struct mbuf *n;
3804903Swnj 
3816092Sroot 	n = sb->sb_mb;
3826092Sroot 	if (n)
3836092Sroot 		while (n->m_next)
3846092Sroot 			n = n->m_next;
3854903Swnj 	while (m) {
3865266Swnj 		if (m->m_len == 0 && (int)m->m_act == 0) {
3875304Sroot 			m = m_free(m);
3885266Swnj 			continue;
3895266Swnj 		}
3904903Swnj 		if (n && n->m_off <= MMAXOFF && m->m_off <= MMAXOFF &&
3914903Swnj 		   (int)n->m_act == 0 && (int)m->m_act == 0 &&
3925042Swnj 		   (n->m_off + n->m_len + m->m_len) <= MMAXOFF) {
3935042Swnj 			bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len,
3944917Swnj 			    (unsigned)m->m_len);
3954903Swnj 			n->m_len += m->m_len;
3964903Swnj 			sb->sb_cc += m->m_len;
3974903Swnj 			m = m_free(m);
3984903Swnj 			continue;
3994903Swnj 		}
4004903Swnj 		sballoc(sb, m);
4016092Sroot 		if (n == 0)
4026092Sroot 			sb->sb_mb = m;
4036092Sroot 		else
4046092Sroot 			n->m_next = m;
4054903Swnj 		n = m;
4064903Swnj 		m = m->m_next;
4076092Sroot 		n->m_next = 0;
4084903Swnj 	}
4094903Swnj }
4104903Swnj 
4115169Swnj /*
4125169Swnj  * Append data and address.
4135169Swnj  * Return 0 if no space in sockbuf or if
4145169Swnj  * can't get mbuf to stuff address in.
4155169Swnj  */
4164928Swnj sbappendaddr(sb, asa, m0)
4174928Swnj 	struct sockbuf *sb;
4184928Swnj 	struct sockaddr *asa;
4194928Swnj 	struct mbuf *m0;
4204928Swnj {
4214928Swnj 	struct sockaddr *msa;
4224928Swnj 	register struct mbuf *m;
4234928Swnj 	register int len = sizeof (struct sockaddr);
4244928Swnj 
4255042Swnj 	m = m0;
4265042Swnj 	if (m == 0)
4275042Swnj 		panic("sbappendaddr");
4285042Swnj 	for (;;) {
4294928Swnj 		len += m->m_len;
4305042Swnj 		if (m->m_next == 0) {
4315042Swnj 			m->m_act = (struct mbuf *)1;
4325042Swnj 			break;
4335042Swnj 		}
4345042Swnj 		m = m->m_next;
4355042Swnj 	}
4365043Swnj 	if (len > sbspace(sb))
4374928Swnj 		return (0);
4389636Ssam 	m = m_get(M_DONTWAIT, MT_SONAME);
4395043Swnj 	if (m == 0)
4404928Swnj 		return (0);
4414928Swnj 	m->m_len = sizeof (struct sockaddr);
4424928Swnj 	msa = mtod(m, struct sockaddr *);
4434928Swnj 	*msa = *asa;
4444928Swnj 	m->m_act = (struct mbuf *)1;
4454928Swnj 	sbappend(sb, m);
4464928Swnj 	sbappend(sb, m0);
4474928Swnj 	return (1);
4484928Swnj }
4494928Swnj 
45010138Ssam #ifdef notdef
4518549Sroot SBCHECK(sb, str)
4528549Sroot 	struct sockbuf *sb;
4538549Sroot 	char *str;
4548549Sroot {
4558549Sroot 	register int cnt = sb->sb_cc;
4568549Sroot 	register int mbcnt = sb->sb_mbcnt;
4578549Sroot 	register struct mbuf *m;
4588549Sroot 
4598549Sroot 	for (m = sb->sb_mb; m; m = m->m_next) {
4608549Sroot 		cnt -= m->m_len;
4618549Sroot 		mbcnt -= MSIZE;
4628549Sroot 		if (m->m_off > MMAXOFF)
4638549Sroot 			mbcnt -= CLBYTES;
4648549Sroot 	}
4658549Sroot 	if (cnt || mbcnt) {
4668549Sroot 		printf("cnt %d mbcnt %d\n", cnt, mbcnt);
4678549Sroot 		panic(str);
4688549Sroot 	}
4698549Sroot }
47010138Ssam #endif
4718549Sroot 
4724903Swnj /*
4734903Swnj  * Free all mbufs on a sockbuf mbuf chain.
4744903Swnj  * Check that resource allocations return to 0.
4754903Swnj  */
4764903Swnj sbflush(sb)
4774903Swnj 	struct sockbuf *sb;
4784903Swnj {
4794903Swnj 
4804903Swnj 	if (sb->sb_flags & SB_LOCK)
4814903Swnj 		panic("sbflush");
4825266Swnj 	if (sb->sb_cc)
4835266Swnj 		sbdrop(sb, sb->sb_cc);
4844903Swnj 	if (sb->sb_cc || sb->sb_mbcnt || sb->sb_mb)
4854903Swnj 		panic("sbflush 2");
4864903Swnj }
4874903Swnj 
4884903Swnj /*
4894903Swnj  * Drop data from (the front of) a sockbuf chain.
4904903Swnj  */
4914903Swnj sbdrop(sb, len)
4924903Swnj 	register struct sockbuf *sb;
4934903Swnj 	register int len;
4944903Swnj {
4954903Swnj 	register struct mbuf *m = sb->sb_mb, *mn;
4964903Swnj 
4974903Swnj 	while (len > 0) {
4984903Swnj 		if (m == 0)
4994903Swnj 			panic("sbdrop");
5005064Swnj 		if (m->m_len > len) {
5014903Swnj 			m->m_len -= len;
5024903Swnj 			m->m_off += len;
5034903Swnj 			sb->sb_cc -= len;
5044903Swnj 			break;
5054903Swnj 		}
5065064Swnj 		len -= m->m_len;
5075064Swnj 		sbfree(sb, m);
5085064Swnj 		MFREE(m, mn);
5095064Swnj 		m = mn;
5104903Swnj 	}
5114903Swnj 	sb->sb_mb = m;
5124903Swnj }
513