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