date and time created 85/07/31 17:07:57 by sam
[unix-history] / usr / src / sys / kern / uipc_socket2.c
CommitLineData
da7c5cc6
KM
1/*
2 * Copyright (c) 1982 Regents of the University of California.
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
5 *
6 * @(#)uipc_socket2.c 6.10 (Berkeley) %G%
7 */
681ebb17 8
94368568
JB
9#include "param.h"
10#include "systm.h"
11#include "dir.h"
12#include "user.h"
13#include "proc.h"
14#include "file.h"
15#include "inode.h"
16#include "buf.h"
17#include "mbuf.h"
18#include "protosw.h"
19#include "socket.h"
20#include "socketvar.h"
681ebb17
BJ
21
22/*
23 * Primitive routines for operating on sockets and socket buffers
24 */
25
26/*
27 * Procedures to manipulate state flags of socket
2deddea9
BJ
28 * and do appropriate wakeups. Normal sequence from the
29 * active (originating) side is that soisconnecting() is
30 * called during processing of connect() call,
4c078bb2
BJ
31 * resulting in an eventual call to soisconnected() if/when the
32 * connection is established. When the connection is torn down
33 * soisdisconnecting() is called during processing of disconnect() call,
34 * and soisdisconnected() is called when the connection to the peer
35 * is totally severed. The semantics of these routines are such that
36 * connectionless protocols can call soisconnected() and soisdisconnected()
37 * only, bypassing the in-progress calls when setting up a ``connection''
38 * takes no time.
39 *
88a7a62a
SL
40 * From the passive side, a socket is created with
41 * two queues of sockets: so_q0 for connections in progress
2deddea9
BJ
42 * and so_q for connections already made and awaiting user acceptance.
43 * As a protocol is preparing incoming connections, it creates a socket
44 * structure queued on so_q0 by calling sonewconn(). When the connection
45 * is established, soisconnected() is called, and transfers the
46 * socket structure to so_q, making it available to accept().
47 *
88a7a62a 48 * If a socket is closed with sockets on either
2deddea9
BJ
49 * so_q0 or so_q, these sockets are dropped.
50 *
88a7a62a 51 * If higher level protocols are implemented in
4c078bb2 52 * the kernel, the wakeups done here will sometimes
88a7a62a 53 * cause software-interrupt process scheduling.
681ebb17 54 */
4c078bb2 55
681ebb17 56soisconnecting(so)
88a7a62a 57 register struct socket *so;
681ebb17
BJ
58{
59
60 so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
61 so->so_state |= SS_ISCONNECTING;
62 wakeup((caddr_t)&so->so_timeo);
63}
64
65soisconnected(so)
88a7a62a 66 register struct socket *so;
681ebb17 67{
2deddea9 68 register struct socket *head = so->so_head;
681ebb17 69
2deddea9
BJ
70 if (head) {
71 if (soqremque(so, 0) == 0)
72 panic("soisconnected");
73 soqinsque(head, so, 1);
ab303321 74 sorwakeup(head);
88a7a62a 75 wakeup((caddr_t)&head->so_timeo);
2deddea9 76 }
681ebb17
BJ
77 so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING);
78 so->so_state |= SS_ISCONNECTED;
79 wakeup((caddr_t)&so->so_timeo);
f957a49a
BJ
80 sorwakeup(so);
81 sowwakeup(so);
681ebb17
BJ
82}
83
84soisdisconnecting(so)
88a7a62a 85 register struct socket *so;
681ebb17
BJ
86{
87
72857acf 88 so->so_state &= ~SS_ISCONNECTING;
681ebb17
BJ
89 so->so_state |= (SS_ISDISCONNECTING|SS_CANTRCVMORE|SS_CANTSENDMORE);
90 wakeup((caddr_t)&so->so_timeo);
4c078bb2 91 sowwakeup(so);
b454c3ea 92 sorwakeup(so);
681ebb17
BJ
93}
94
95soisdisconnected(so)
88a7a62a 96 register struct socket *so;
681ebb17
BJ
97{
98
99 so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
100 so->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE);
101 wakeup((caddr_t)&so->so_timeo);
102 sowwakeup(so);
103 sorwakeup(so);
104}
105
2deddea9
BJ
106/*
107 * When an attempt at a new connection is noted on a socket
108 * which accepts connections, sonewconn is called. If the
109 * connection is possible (subject to space constraints, etc.)
110 * then we allocate a new structure, propoerly linked into the
111 * data structure of the original socket, and return this.
112 */
113struct socket *
114sonewconn(head)
115 register struct socket *head;
116{
117 register struct socket *so;
88a7a62a 118 register struct mbuf *m;
2deddea9
BJ
119
120 if (head->so_qlen + head->so_q0len > 3 * head->so_qlimit / 2)
121 goto bad;
cce93e4b 122 m = m_getclr(M_DONTWAIT, MT_SOCKET);
5fe6f9d1 123 if (m == NULL)
2deddea9
BJ
124 goto bad;
125 so = mtod(m, struct socket *);
126 so->so_type = head->so_type;
127 so->so_options = head->so_options &~ SO_ACCEPTCONN;
128 so->so_linger = head->so_linger;
f7428e88 129 so->so_state = head->so_state | SS_NOFDREF;
2deddea9
BJ
130 so->so_proto = head->so_proto;
131 so->so_timeo = head->so_timeo;
132 so->so_pgrp = head->so_pgrp;
133 soqinsque(head, so, 0);
88a7a62a
SL
134 if ((*so->so_proto->pr_usrreq)(so, PRU_ATTACH,
135 (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0)) {
2deddea9 136 (void) soqremque(so, 0);
30c36259 137 (void) m_free(m);
2deddea9
BJ
138 goto bad;
139 }
140 return (so);
141bad:
142 return ((struct socket *)0);
143}
144
145soqinsque(head, so, q)
146 register struct socket *head, *so;
147 int q;
148{
149
150 so->so_head = head;
151 if (q == 0) {
152 head->so_q0len++;
153 so->so_q0 = head->so_q0;
154 head->so_q0 = so;
155 } else {
156 head->so_qlen++;
157 so->so_q = head->so_q;
158 head->so_q = so;
159 }
160}
161
162soqremque(so, q)
163 register struct socket *so;
164 int q;
165{
166 register struct socket *head, *prev, *next;
167
168 head = so->so_head;
169 prev = head;
170 for (;;) {
171 next = q ? prev->so_q : prev->so_q0;
172 if (next == so)
173 break;
174 if (next == head)
175 return (0);
176 prev = next;
177 }
178 if (q == 0) {
179 prev->so_q0 = next->so_q0;
180 head->so_q0len--;
181 } else {
182 prev->so_q = next->so_q;
183 head->so_qlen--;
184 }
185 next->so_q0 = next->so_q = 0;
186 next->so_head = 0;
187 return (1);
188}
189
4c078bb2
BJ
190/*
191 * Socantsendmore indicates that no more data will be sent on the
192 * socket; it would normally be applied to a socket when the user
193 * informs the system that no more data is to be sent, by the protocol
194 * code (in case PRU_SHUTDOWN). Socantrcvmore indicates that no more data
195 * will be received, and will normally be applied to the socket by a
196 * protocol when it detects that the peer will send no more data.
197 * Data queued for reading in the socket may yet be read.
198 */
199
ae921915
BJ
200socantsendmore(so)
201 struct socket *so;
202{
203
204 so->so_state |= SS_CANTSENDMORE;
205 sowwakeup(so);
206}
207
208socantrcvmore(so)
209 struct socket *so;
210{
211
212 so->so_state |= SS_CANTRCVMORE;
213 sorwakeup(so);
214}
215
681ebb17 216/*
4c078bb2
BJ
217 * Socket select/wakeup routines.
218 */
219
681ebb17
BJ
220/*
221 * Queue a process for a select on a socket buffer.
222 */
223sbselqueue(sb)
224 struct sockbuf *sb;
225{
226 register struct proc *p;
227
ae921915 228 if ((p = sb->sb_sel) && p->p_wchan == (caddr_t)&selwait)
681ebb17
BJ
229 sb->sb_flags |= SB_COLL;
230 else
231 sb->sb_sel = u.u_procp;
232}
233
ae921915
BJ
234/*
235 * Wait for data to arrive at/drain from a socket buffer.
236 */
237sbwait(sb)
238 struct sockbuf *sb;
239{
240
241 sb->sb_flags |= SB_WAIT;
242 sleep((caddr_t)&sb->sb_cc, PZERO+1);
243}
244
681ebb17
BJ
245/*
246 * Wakeup processes waiting on a socket buffer.
247 */
248sbwakeup(sb)
88a7a62a 249 register struct sockbuf *sb;
681ebb17
BJ
250{
251
252 if (sb->sb_sel) {
253 selwakeup(sb->sb_sel, sb->sb_flags & SB_COLL);
254 sb->sb_sel = 0;
255 sb->sb_flags &= ~SB_COLL;
256 }
257 if (sb->sb_flags & SB_WAIT) {
258 sb->sb_flags &= ~SB_WAIT;
388ca8bd 259 wakeup((caddr_t)&sb->sb_cc);
681ebb17
BJ
260 }
261}
262
8f7109aa
EC
263/*
264 * Wakeup socket readers and writers.
265 * Do asynchronous notification via SIGIO
266 * if the socket has the SS_ASYNC flag set.
267 */
268sowakeup(so, sb)
269 register struct socket *so;
270 struct sockbuf *sb;
271{
272 register struct proc *p;
273
274 sbwakeup(sb);
275 if (so->so_state & SS_ASYNC) {
3aefacbd
MK
276 if (so->so_pgrp < 0)
277 gsignal(-so->so_pgrp, SIGIO);
278 else if (so->so_pgrp > 0 && (p = pfind(so->so_pgrp)) != 0)
8f7109aa
EC
279 psignal(p, SIGIO);
280 }
281}
282
4c078bb2
BJ
283/*
284 * Socket buffer (struct sockbuf) utility routines.
285 *
286 * Each socket contains two socket buffers: one for sending data and
287 * one for receiving data. Each buffer contains a queue of mbufs,
288 * information about the number of mbufs and amount of data in the
289 * queue, and other fields allowing select() statements and notification
290 * on data availability to be implemented.
291 *
261a8548
MK
292 * Data stored in a socket buffer is maintained as a list of records.
293 * Each record is a list of mbufs chained together with the m_next
294 * field. Records are chained together with the m_act field. The upper
295 * level routine soreceive() expects the following conventions to be
296 * observed when placing information in the receive buffer:
297 *
298 * 1. If the protocol requires each message be preceded by the sender's
299 * name, then a record containing that name must be present before
300 * any associated data (mbuf's must be of type MT_SONAME).
301 * 2. If the protocol supports the exchange of ``access rights'' (really
302 * just additional data associated with the message), and there are
303 * ``rights'' to be received, then a record containing this data
304 * should be present (mbuf's must be of type MT_RIGHTS).
305 * 3. If a name or rights record exists, then it must be followed by
306 * a data record, perhaps of zero length.
307 *
4c078bb2 308 * Before using a new socket structure it is first necessary to reserve
261a8548 309 * buffer space to the socket, by calling sbreserve(). This commits
4c078bb2 310 * some of the available buffer space in the system buffer pool for the
261a8548 311 * socket. The space should be released by calling sbrelease() when the
4c078bb2
BJ
312 * socket is destroyed.
313 *
261a8548
MK
314 * The routines sbappend() or sbappendrecord() are normally called to
315 * append new mbufs to a socket buffer, after checking that adequate
316 * space is available, comparing the function sbspace() with the amount
317 * of data to be added. sbappendrecord() differs from sbappend() in
318 * that data supplied is treated as the beginning of a new record.
4c078bb2
BJ
319 * Data is normally removed from a socket buffer in a protocol by
320 * first calling m_copy on the socket buffer mbuf chain and sending this
321 * to a peer, and then removing the data from the socket buffer with
261a8548
MK
322 * sbdrop() or sbdroprecord() when the data is acknowledged by the peer
323 * (or immediately in the case of unreliable protocols.)
4c078bb2 324 *
261a8548
MK
325 * To place a sender's name, optionally, access rights, and data in a
326 * socket buffer sbappendaddr() should be used. To place access rights
327 * and data in a socket buffer sbappendrights() should be used. Note
328 * that unlike sbappend() and sbappendrecord(), these routines check
4c078bb2 329 * for the caller that there will be enough space to store the data.
261a8548
MK
330 * Each fails if there is not enough space, or if it cannot find mbufs
331 * to store additional information in.
4c078bb2
BJ
332 */
333
0e18ec4a 334soreserve(so, sndcc, rcvcc)
88a7a62a 335 register struct socket *so;
0e18ec4a
BJ
336 int sndcc, rcvcc;
337{
338
339 if (sbreserve(&so->so_snd, sndcc) == 0)
340 goto bad;
341 if (sbreserve(&so->so_rcv, rcvcc) == 0)
342 goto bad2;
343 return (0);
344bad2:
345 sbrelease(&so->so_snd);
346bad:
347 return (ENOBUFS);
348}
349
681ebb17
BJ
350/*
351 * Allot mbufs to a sockbuf.
352 */
353sbreserve(sb, cc)
354 struct sockbuf *sb;
355{
356
1ceef2d8
MK
357 if ((unsigned) cc > SB_MAX)
358 return (0);
de48daf3 359 /* someday maybe this routine will fail... */
d028a086 360 sb->sb_hiwat = cc;
88a7a62a 361 /* * 2 implies names can be no more than 1 mbuf each */
453677da 362 sb->sb_mbmax = MIN(cc * 2, SB_MAX);
ae921915 363 return (1);
681ebb17
BJ
364}
365
366/*
367 * Free mbufs held by a socket, and reserved mbuf space.
368 */
369sbrelease(sb)
370 struct sockbuf *sb;
371{
372
373 sbflush(sb);
d028a086 374 sb->sb_hiwat = sb->sb_mbmax = 0;
681ebb17
BJ
375}
376
377/*
261a8548
MK
378 * Routines to add and remove
379 * data from an mbuf queue.
681ebb17
BJ
380 */
381
382/*
261a8548
MK
383 * Append mbuf chain m to the last record in the
384 * socket buffer sb. The additional space associated
385 * the mbuf chain is recorded in sb. Empty mbufs are
386 * discarded and mbufs are compacted where possible.
681ebb17
BJ
387 */
388sbappend(sb, m)
261a8548
MK
389 struct sockbuf *sb;
390 struct mbuf *m;
681ebb17 391{
e495e1cc 392 register struct mbuf *n;
681ebb17 393
261a8548
MK
394 if (m == 0)
395 return;
396 if (n = sb->sb_mb) {
397 while (n->m_act)
398 n = n->m_act;
e495e1cc
BJ
399 while (n->m_next)
400 n = n->m_next;
681ebb17 401 }
261a8548 402 sbcompress(sb, m, n);
681ebb17
BJ
403}
404
4c078bb2 405/*
261a8548
MK
406 * As above, except the mbuf chain
407 * begins a new record.
4c078bb2 408 */
261a8548
MK
409sbappendrecord(sb, m0)
410 register struct sockbuf *sb;
411 register struct mbuf *m0;
2b4b57cd 412{
2b4b57cd 413 register struct mbuf *m;
2b4b57cd 414
261a8548
MK
415 if (m0 == 0)
416 return;
417 if (m = sb->sb_mb)
418 while (m->m_act)
419 m = m->m_act;
420 /*
421 * Put the first mbuf on the queue.
422 * Note this permits zero length records.
423 */
424 sballoc(sb, m0);
425 if (m)
426 m->m_act = m0;
427 else
428 sb->sb_mb = m0;
429 m = m0->m_next;
430 m0->m_next = 0;
431 sbcompress(sb, m, m0);
432}
433
434/*
435 * Append address and data, and optionally, rights
436 * to the receive queue of a socket. Return 0 if
437 * no space in sockbuf or insufficient mbufs.
438 */
439sbappendaddr(sb, asa, m0, rights0) /* XXX */
440 register struct sockbuf *sb;
441 struct sockaddr *asa;
442 struct mbuf *rights0, *m0;
443{
444 register struct mbuf *m, *n;
445 int space = sizeof (*asa);
446
76a6e254
BJ
447 m = m0;
448 if (m == 0)
449 panic("sbappendaddr");
261a8548
MK
450 do {
451 space += m->m_len;
76a6e254 452 m = m->m_next;
261a8548
MK
453 } while (m);
454 if (rights0)
455 space += rights0->m_len;
456 if (space > sbspace(sb))
2b4b57cd 457 return (0);
cce93e4b 458 m = m_get(M_DONTWAIT, MT_SONAME);
261a8548 459 if (m == 0)
2b4b57cd 460 return (0);
88a7a62a 461 *mtod(m, struct sockaddr *) = *asa;
261a8548
MK
462 m->m_len = sizeof (*asa);
463 if (rights0) {
464 m->m_act = m_copy(rights0, 0, rights0->m_len);
465 if (m->m_act == 0) {
466 m_freem(m);
467 return (0);
468 }
261a8548 469 sballoc(sb, m->m_act);
4f8975e4
MK
470 }
471 sballoc(sb, m);
261a8548
MK
472 if (n = sb->sb_mb) {
473 while (n->m_act)
474 n = n->m_act;
475 n->m_act = m;
88a7a62a 476 } else
261a8548
MK
477 sb->sb_mb = m;
478 if (m->m_act)
479 m = m->m_act;
480 sballoc(sb, m0);
481 m->m_act = m0;
482 m = m0->m_next;
483 m0->m_next = 0;
4f8975e4
MK
484 if (m)
485 sbcompress(sb, m, m0);
261a8548
MK
486 return (1);
487}
488
489#ifdef notdef
490sbappendrights(sb, rights, m0)
491 struct sockbuf *sb;
492 struct mbuf *rights, *m;
493{
494 register struct mbuf *m, *n;
495 int space = 0;
496
497 m = m0;
498 if (m == 0 || rights == 0)
499 panic("sbappendrights");
500 do {
501 space += m->m_len;
502 m = m->m_next;
503 } while (m);
504 space += rights->m_len;
505 if (space > sbspace(sb))
88a7a62a 506 return (0);
261a8548
MK
507 m = m_copy(rights, 0, rights->m_len);
508 if (m == 0)
509 return (0);
510 sballoc(sb, m);
511 if (n = sb->sb_mb) {
512 while (n->m_act)
513 n = n->m_act;
514 n->m_act = m;
515 } else
516 n->m_act = m;
517 sballoc(sb, m0);
518 m->m_act = m0;
519 m = m0->m_next;
520 m0->m_next = 0;
4f8975e4
MK
521 if (m)
522 sbcompress(sb, m, m0);
2b4b57cd
BJ
523 return (1);
524}
261a8548
MK
525#endif
526
527/*
528 * Compress mbuf chain m into the socket
529 * buffer sb following mbuf n. If n
530 * is null, the buffer is presumed empty.
531 */
532sbcompress(sb, m, n)
533 register struct sockbuf *sb;
534 register struct mbuf *m, *n;
535{
536
537 while (m) {
538 if (m->m_len == 0) {
539 m = m_free(m);
540 continue;
541 }
542 if (n && n->m_off <= MMAXOFF && m->m_off <= MMAXOFF &&
543 (n->m_off + n->m_len + m->m_len) <= MMAXOFF) {
544 bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len,
545 (unsigned)m->m_len);
546 n->m_len += m->m_len;
547 sb->sb_cc += m->m_len;
548 m = m_free(m);
549 continue;
550 }
551 sballoc(sb, m);
552 if (n)
553 n->m_next = m;
554 else
555 sb->sb_mb = m;
556 n = m;
557 m = m->m_next;
558 n->m_next = 0;
559 }
560}
2b4b57cd 561
681ebb17 562/*
261a8548
MK
563 * Free all mbufs in a sockbuf.
564 * Check that all resources are reclaimed.
681ebb17
BJ
565 */
566sbflush(sb)
88a7a62a 567 register struct sockbuf *sb;
681ebb17
BJ
568{
569
570 if (sb->sb_flags & SB_LOCK)
571 panic("sbflush");
a73ab5ae
BJ
572 if (sb->sb_cc)
573 sbdrop(sb, sb->sb_cc);
681ebb17
BJ
574 if (sb->sb_cc || sb->sb_mbcnt || sb->sb_mb)
575 panic("sbflush 2");
576}
577
578/*
261a8548 579 * Drop data from (the front of) a sockbuf.
681ebb17 580 */
261a8548 581struct mbuf *
681ebb17
BJ
582sbdrop(sb, len)
583 register struct sockbuf *sb;
584 register int len;
585{
261a8548
MK
586 register struct mbuf *m, *mn;
587 struct mbuf *next;
681ebb17 588
261a8548 589 next = (m = sb->sb_mb) ? m->m_act : 0;
681ebb17 590 while (len > 0) {
261a8548
MK
591 if (m == 0) {
592 if (next == 0)
593 panic("sbdrop");
594 m = next;
595 next = m->m_act;
596 continue;
597 }
b9f0d37f 598 if (m->m_len > len) {
681ebb17
BJ
599 m->m_len -= len;
600 m->m_off += len;
601 sb->sb_cc -= len;
602 break;
603 }
b9f0d37f
BJ
604 len -= m->m_len;
605 sbfree(sb, m);
606 MFREE(m, mn);
607 m = mn;
681ebb17 608 }
082e4f86 609 while (m && m->m_len == 0) {
453677da 610 sbfree(sb, m);
082e4f86
MK
611 MFREE(m, mn);
612 m = mn;
613 }
261a8548
MK
614 if (m) {
615 sb->sb_mb = m;
616 m->m_act = next;
617 } else
618 sb->sb_mb = next;
619 return (sb->sb_mb);
620}
621
622/*
623 * Drop a record off the front of a sockbuf
624 * and move the next record to the front.
625 */
626struct mbuf *
627sbdroprecord(sb)
628 register struct sockbuf *sb;
629{
630 register struct mbuf *m, *mn;
631
632 m = sb->sb_mb;
633 if (m) {
634 sb->sb_mb = m->m_act;
635 do {
636 sbfree(sb, m);
637 MFREE(m, mn);
638 } while (m = mn);
639 }
640 return (sb->sb_mb);
681ebb17 641}