xref: /original-bsd/sys/kern/uipc_socket2.c (revision 6b7db209)
1 /*	uipc_socket2.c	4.32	82/12/14	*/
2 
3 #include "../h/param.h"
4 #include "../h/systm.h"
5 #include "../h/dir.h"
6 #include "../h/user.h"
7 #include "../h/proc.h"
8 #include "../h/file.h"
9 #include "../h/inode.h"
10 #include "../h/buf.h"
11 #include "../h/mbuf.h"
12 #include "../h/protosw.h"
13 #include "../h/socket.h"
14 #include "../h/socketvar.h"
15 
16 /*
17  * Primitive routines for operating on sockets and socket buffers
18  */
19 
20 /*
21  * Procedures to manipulate state flags of socket
22  * and do appropriate wakeups.  Normal sequence from the
23  * active (originating) side is that soisconnecting() is
24  * called during processing of connect() call,
25  * resulting in an eventual call to soisconnected() if/when the
26  * connection is established.  When the connection is torn down
27  * soisdisconnecting() is called during processing of disconnect() call,
28  * and soisdisconnected() is called when the connection to the peer
29  * is totally severed.  The semantics of these routines are such that
30  * connectionless protocols can call soisconnected() and soisdisconnected()
31  * only, bypassing the in-progress calls when setting up a ``connection''
32  * takes no time.
33  *
34  * From the passive side, a socket is created with SO_ACCEPTCONN
35  * creating two queues of sockets: so_q0 for connections in progress
36  * and so_q for connections already made and awaiting user acceptance.
37  * As a protocol is preparing incoming connections, it creates a socket
38  * structure queued on so_q0 by calling sonewconn().  When the connection
39  * is established, soisconnected() is called, and transfers the
40  * socket structure to so_q, making it available to accept().
41  *
42  * If a SO_ACCEPTCONN socket is closed with sockets on either
43  * so_q0 or so_q, these sockets are dropped.
44  *
45  * If and when higher level protocols are implemented in
46  * the kernel, the wakeups done here will sometimes
47  * be implemented as software-interrupt process scheduling.
48  */
49 
50 soisconnecting(so)
51 	struct socket *so;
52 {
53 
54 	so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
55 	so->so_state |= SS_ISCONNECTING;
56 	wakeup((caddr_t)&so->so_timeo);
57 }
58 
59 soisconnected(so)
60 	struct socket *so;
61 {
62 	register struct socket *head = so->so_head;
63 
64 	if (head) {
65 		if (soqremque(so, 0) == 0)
66 			panic("soisconnected");
67 		soqinsque(head, so, 1);
68 		wakeup((caddr_t)&head->so_timeo);
69 	}
70 	so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING);
71 	so->so_state |= SS_ISCONNECTED;
72 	wakeup((caddr_t)&so->so_timeo);
73 	sorwakeup(so);
74 	sowwakeup(so);
75 }
76 
77 soisdisconnecting(so)
78 	struct socket *so;
79 {
80 
81 	so->so_state &= ~SS_ISCONNECTING;
82 	so->so_state |= (SS_ISDISCONNECTING|SS_CANTRCVMORE|SS_CANTSENDMORE);
83 	wakeup((caddr_t)&so->so_timeo);
84 	sowwakeup(so);
85 	sorwakeup(so);
86 }
87 
88 soisdisconnected(so)
89 	struct socket *so;
90 {
91 
92 	so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
93 	so->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE);
94 	wakeup((caddr_t)&so->so_timeo);
95 	sowwakeup(so);
96 	sorwakeup(so);
97 }
98 
99 /*
100  * When an attempt at a new connection is noted on a socket
101  * which accepts connections, sonewconn is called.  If the
102  * connection is possible (subject to space constraints, etc.)
103  * then we allocate a new structure, propoerly linked into the
104  * data structure of the original socket, and return this.
105  */
106 struct socket *
107 sonewconn(head)
108 	register struct socket *head;
109 {
110 	register struct socket *so;
111 	struct mbuf *m;
112 
113 	if (head->so_qlen + head->so_q0len > 3 * head->so_qlimit / 2)
114 		goto bad;
115 	m = m_getclr(M_DONTWAIT, MT_SOCKET);
116 	if (m == 0)
117 		goto bad;
118 	so = mtod(m, struct socket *);
119 	so->so_type = head->so_type;
120 	so->so_options = head->so_options &~ SO_ACCEPTCONN;
121 	so->so_linger = head->so_linger;
122 	so->so_state = head->so_state;
123 	so->so_proto = head->so_proto;
124 	so->so_timeo = head->so_timeo;
125 	so->so_pgrp = head->so_pgrp;
126 	soqinsque(head, so, 0);
127 	if ((*so->so_proto->pr_usrreq)(so, PRU_ATTACH, 0, 0, 0)) {
128 		(void) soqremque(so, 0);
129 		(void) m_free(m);
130 		goto bad;
131 	}
132 	return (so);
133 bad:
134 	return ((struct socket *)0);
135 }
136 
137 soqinsque(head, so, q)
138 	register struct socket *head, *so;
139 	int q;
140 {
141 
142 	so->so_head = head;
143 	if (q == 0) {
144 		head->so_q0len++;
145 		so->so_q0 = head->so_q0;
146 		head->so_q0 = so;
147 	} else {
148 		head->so_qlen++;
149 		so->so_q = head->so_q;
150 		head->so_q = so;
151 	}
152 }
153 
154 soqremque(so, q)
155 	register struct socket *so;
156 	int q;
157 {
158 	register struct socket *head, *prev, *next;
159 
160 	head = so->so_head;
161 	prev = head;
162 	for (;;) {
163 		next = q ? prev->so_q : prev->so_q0;
164 		if (next == so)
165 			break;
166 		if (next == head)
167 			return (0);
168 		prev = next;
169 	}
170 	if (q == 0) {
171 		prev->so_q0 = next->so_q0;
172 		head->so_q0len--;
173 	} else {
174 		prev->so_q = next->so_q;
175 		head->so_qlen--;
176 	}
177 	next->so_q0 = next->so_q = 0;
178 	next->so_head = 0;
179 	return (1);
180 }
181 
182 /*
183  * Socantsendmore indicates that no more data will be sent on the
184  * socket; it would normally be applied to a socket when the user
185  * informs the system that no more data is to be sent, by the protocol
186  * code (in case PRU_SHUTDOWN).  Socantrcvmore indicates that no more data
187  * will be received, and will normally be applied to the socket by a
188  * protocol when it detects that the peer will send no more data.
189  * Data queued for reading in the socket may yet be read.
190  */
191 
192 socantsendmore(so)
193 	struct socket *so;
194 {
195 
196 	so->so_state |= SS_CANTSENDMORE;
197 	sowwakeup(so);
198 }
199 
200 socantrcvmore(so)
201 	struct socket *so;
202 {
203 
204 	so->so_state |= SS_CANTRCVMORE;
205 	sorwakeup(so);
206 }
207 
208 /*
209  * Socket select/wakeup routines.
210  */
211 
212 /*
213  * Interface routine to select() system
214  * call for sockets.
215  */
216 soselect(so, rw)
217 	register struct socket *so;
218 	int rw;
219 {
220 	int s = splnet();
221 
222 	switch (rw) {
223 
224 	case FREAD:
225 		if (soreadable(so)) {
226 			splx(s);
227 			return (1);
228 		}
229 		sbselqueue(&so->so_rcv);
230 		break;
231 
232 	case FWRITE:
233 		if (sowriteable(so)) {
234 			splx(s);
235 			return (1);
236 		}
237 		sbselqueue(&so->so_snd);
238 		break;
239 	}
240 	splx(s);
241 	return (0);
242 }
243 
244 /*
245  * Queue a process for a select on a socket buffer.
246  */
247 sbselqueue(sb)
248 	struct sockbuf *sb;
249 {
250 	register struct proc *p;
251 
252 	if ((p = sb->sb_sel) && p->p_wchan == (caddr_t)&selwait)
253 		sb->sb_flags |= SB_COLL;
254 	else
255 		sb->sb_sel = u.u_procp;
256 }
257 
258 /*
259  * Wait for data to arrive at/drain from a socket buffer.
260  */
261 sbwait(sb)
262 	struct sockbuf *sb;
263 {
264 
265 	sb->sb_flags |= SB_WAIT;
266 	sleep((caddr_t)&sb->sb_cc, PZERO+1);
267 }
268 
269 /*
270  * Wakeup processes waiting on a socket buffer.
271  */
272 sbwakeup(sb)
273 	struct sockbuf *sb;
274 {
275 
276 	if (sb->sb_sel) {
277 		selwakeup(sb->sb_sel, sb->sb_flags & SB_COLL);
278 		sb->sb_sel = 0;
279 		sb->sb_flags &= ~SB_COLL;
280 	}
281 	if (sb->sb_flags & SB_WAIT) {
282 		sb->sb_flags &= ~SB_WAIT;
283 		wakeup((caddr_t)&sb->sb_cc);
284 	}
285 }
286 
287 /*
288  * Socket buffer (struct sockbuf) utility routines.
289  *
290  * Each socket contains two socket buffers: one for sending data and
291  * one for receiving data.  Each buffer contains a queue of mbufs,
292  * information about the number of mbufs and amount of data in the
293  * queue, and other fields allowing select() statements and notification
294  * on data availability to be implemented.
295  *
296  * Before using a new socket structure it is first necessary to reserve
297  * buffer space to the socket, by calling sbreserve.  This commits
298  * some of the available buffer space in the system buffer pool for the
299  * socket.  The space should be released by calling sbrelease when the
300  * socket is destroyed.
301  *
302  * The routine sbappend() is normally called to append new mbufs
303  * to a socket buffer, after checking that adequate space is available
304  * comparing the function spspace() with the amount of data to be added.
305  * Data is normally removed from a socket buffer in a protocol by
306  * first calling m_copy on the socket buffer mbuf chain and sending this
307  * to a peer, and then removing the data from the socket buffer with
308  * sbdrop when the data is acknowledged by the peer (or immediately
309  * in the case of unreliable protocols.)
310  *
311  * Protocols which do not require connections place both source address
312  * and data information in socket buffer queues.  The source addresses
313  * are stored in single mbufs after each data item, and are easily found
314  * as the data items are all marked with end of record markers.  The
315  * sbappendaddr() routine stores a datum and associated address in
316  * a socket buffer.  Note that, unlike sbappend(), this routine checks
317  * for the caller that there will be enough space to store the data.
318  * It fails if there is not enough space, or if it cannot find
319  * a mbuf to store the address in.
320  *
321  * The higher-level routines sosend and soreceive (in socket.c)
322  * also add data to, and remove data from socket buffers repectively.
323  */
324 
325 soreserve(so, sndcc, rcvcc)
326 	struct socket *so;
327 	int sndcc, rcvcc;
328 {
329 
330 	if (sbreserve(&so->so_snd, sndcc) == 0)
331 		goto bad;
332 	if (sbreserve(&so->so_rcv, rcvcc) == 0)
333 		goto bad2;
334 	return (0);
335 bad2:
336 	sbrelease(&so->so_snd);
337 bad:
338 	return (ENOBUFS);
339 }
340 
341 /*
342  * Allot mbufs to a sockbuf.
343  */
344 sbreserve(sb, cc)
345 	struct sockbuf *sb;
346 {
347 
348 	/* someday maybe this routine will fail... */
349 	sb->sb_hiwat = cc;
350 	sb->sb_mbmax = cc*2;
351 	return (1);
352 }
353 
354 /*
355  * Free mbufs held by a socket, and reserved mbuf space.
356  */
357 sbrelease(sb)
358 	struct sockbuf *sb;
359 {
360 
361 	sbflush(sb);
362 	sb->sb_hiwat = sb->sb_mbmax = 0;
363 }
364 
365 /*
366  * Routines to add (at the end) and remove (from the beginning)
367  * data from a mbuf queue.
368  */
369 
370 /*
371  * Append mbuf queue m to sockbuf sb.
372  */
373 sbappend(sb, m)
374 	register struct mbuf *m;
375 	register struct sockbuf *sb;
376 {
377 	register struct mbuf *n;
378 
379 SBCHECK(sb, "sbappend begin");
380 #ifdef notdef
381 { struct mbuf *p;
382 printf("sba: ");
383 for (p = sb->sb_mb; p; p = p->m_next) printf("%x:(%x,%d) ",p,p->m_off,p->m_len);
384 printf("+= ");
385 for (p = m; p; p = p->m_next) printf("%x:(%x,%d) ",p,p->m_off,p->m_len);
386 printf("\n");
387 }
388 #endif
389 	n = sb->sb_mb;
390 	if (n)
391 		while (n->m_next)
392 			n = n->m_next;
393 	while (m) {
394 		if (m->m_len == 0 && (int)m->m_act == 0) {
395 			m = m_free(m);
396 			continue;
397 		}
398 		if (n && n->m_off <= MMAXOFF && m->m_off <= MMAXOFF &&
399 		   (int)n->m_act == 0 && (int)m->m_act == 0 &&
400 		   (n->m_off + n->m_len + m->m_len) <= MMAXOFF) {
401 			bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len,
402 			    (unsigned)m->m_len);
403 			n->m_len += m->m_len;
404 			sb->sb_cc += m->m_len;
405 			m = m_free(m);
406 			continue;
407 		}
408 		sballoc(sb, m);
409 		if (n == 0)
410 			sb->sb_mb = m;
411 		else
412 			n->m_next = m;
413 		n = m;
414 		m = m->m_next;
415 		n->m_next = 0;
416 	}
417 #ifdef notdef
418 { struct mbuf *p;
419 printf("res: ");
420 for (p = sb->sb_mb; p; p = p->m_next) printf("%x:(%x,%d) ",p,p->m_off,p->m_len);
421 printf("+= ");
422 for (p = m; p; p = p->m_next) printf("%x:(%x,%d) ",p,p->m_off,p->m_len);
423 printf("\n");
424 }
425 #endif
426 SBCHECK(sb, "sbappend end");
427 }
428 
429 /*
430  * Append data and address.
431  * Return 0 if no space in sockbuf or if
432  * can't get mbuf to stuff address in.
433  */
434 sbappendaddr(sb, asa, m0)
435 	struct sockbuf *sb;
436 	struct sockaddr *asa;
437 	struct mbuf *m0;
438 {
439 	struct sockaddr *msa;
440 	register struct mbuf *m;
441 	register int len = sizeof (struct sockaddr);
442 
443 SBCHECK(sb, "sbappendaddr begin");
444 	m = m0;
445 	if (m == 0)
446 		panic("sbappendaddr");
447 	for (;;) {
448 		len += m->m_len;
449 		if (m->m_next == 0) {
450 			m->m_act = (struct mbuf *)1;
451 			break;
452 		}
453 		m = m->m_next;
454 	}
455 	if (len > sbspace(sb))
456 		return (0);
457 	m = m_get(M_DONTWAIT, MT_SONAME);
458 	if (m == 0)
459 		return (0);
460 	m->m_len = sizeof (struct sockaddr);
461 	msa = mtod(m, struct sockaddr *);
462 	*msa = *asa;
463 	m->m_act = (struct mbuf *)1;
464 	sbappend(sb, m);
465 	sbappend(sb, m0);
466 SBCHECK(sb, "sbappendaddr end");
467 	return (1);
468 }
469 
470 SBCHECK(sb, str)
471 	struct sockbuf *sb;
472 	char *str;
473 {
474 	register int cnt = sb->sb_cc;
475 	register int mbcnt = sb->sb_mbcnt;
476 	register struct mbuf *m;
477 
478 	for (m = sb->sb_mb; m; m = m->m_next) {
479 		cnt -= m->m_len;
480 		mbcnt -= MSIZE;
481 		if (m->m_off > MMAXOFF)
482 			mbcnt -= CLBYTES;
483 	}
484 	if (cnt || mbcnt) {
485 		printf("cnt %d mbcnt %d\n", cnt, mbcnt);
486 		panic(str);
487 	}
488 }
489 
490 /*
491  * Free all mbufs on a sockbuf mbuf chain.
492  * Check that resource allocations return to 0.
493  */
494 sbflush(sb)
495 	struct sockbuf *sb;
496 {
497 
498 	if (sb->sb_flags & SB_LOCK)
499 		panic("sbflush");
500 	if (sb->sb_cc)
501 		sbdrop(sb, sb->sb_cc);
502 	if (sb->sb_cc || sb->sb_mbcnt || sb->sb_mb)
503 		panic("sbflush 2");
504 }
505 
506 /*
507  * Drop data from (the front of) a sockbuf chain.
508  */
509 sbdrop(sb, len)
510 	register struct sockbuf *sb;
511 	register int len;
512 {
513 	register struct mbuf *m = sb->sb_mb, *mn;
514 
515 	while (len > 0) {
516 		if (m == 0)
517 			panic("sbdrop");
518 		if (m->m_len > len) {
519 			m->m_len -= len;
520 			m->m_off += len;
521 			sb->sb_cc -= len;
522 			break;
523 		}
524 		len -= m->m_len;
525 		sbfree(sb, m);
526 		MFREE(m, mn);
527 		m = mn;
528 	}
529 	sb->sb_mb = m;
530 }
531