2 * Copyright (c) 1982, 1986, 1988, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)uipc_socket2.c 8.1 (Berkeley) 6/10/93
36 #include <sys/param.h>
37 #include <sys/systm.h>
41 #include <sys/malloc.h>
43 #include <sys/protosw.h>
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
47 #include <sys/signalvar.h>
51 * Primitive routines for operating on sockets and socket buffers
54 /* strings for sleep message: */
55 char netio
[] = "netio";
56 char netcon
[] = "netcon";
57 char netcls
[] = "netcls";
59 /* these two are sysctl visible */
60 u_long sb_max
= SB_MAX
; /* patchable */
62 u_long sb_efficiency
= 8; /* parameter for sbreserve() */
64 int sominqueue
= 0; /* minimum queue override */
67 * Procedures to manipulate state flags of socket
68 * and do appropriate wakeups. Normal sequence from the
69 * active (originating) side is that soisconnecting() is
70 * called during processing of connect() call,
71 * resulting in an eventual call to soisconnected() if/when the
72 * connection is established. When the connection is torn down
73 * soisdisconnecting() is called during processing of disconnect() call,
74 * and soisdisconnected() is called when the connection to the peer
75 * is totally severed. The semantics of these routines are such that
76 * connectionless protocols can call soisconnected() and soisdisconnected()
77 * only, bypassing the in-progress calls when setting up a ``connection''
80 * From the passive side, a socket is created with
81 * two queues of sockets: so_q0 for connections in progress
82 * and so_q for connections already made and awaiting user acceptance.
83 * As a protocol is preparing incoming connections, it creates a socket
84 * structure queued on so_q0 by calling sonewconn(). When the connection
85 * is established, soisconnected() is called, and transfers the
86 * socket structure to so_q, making it available to accept().
88 * If a socket is closed with sockets on either
89 * so_q0 or so_q, these sockets are dropped.
91 * If higher level protocols are implemented in
92 * the kernel, the wakeups done here will sometimes
93 * cause software-interrupt process scheduling.
98 register struct socket
*so
;
100 OS_DbgPrint(OSK_MID_TRACE
,("Called %x\n", so
));
101 so
->so_state
&= ~(SS_ISCONNECTED
|SS_ISDISCONNECTING
);
102 so
->so_state
|= SS_ISCONNECTING
;
107 register struct socket
*so
;
109 register struct socket
*head
= so
->so_head
;
111 OS_DbgPrint(OSK_MID_TRACE
,("Called %x\n", so
));
113 so
->so_state
&= ~(SS_ISCONNECTING
|SS_ISDISCONNECTING
|SS_ISCONFIRMING
);
114 so
->so_state
|= SS_ISCONNECTED
;
115 if (head
&& soqremque(so
, 0)) {
116 soqinsque(head
, so
, 1);
118 wakeup(so
, (caddr_t
)&head
->so_timeo
);
120 wakeup(so
, (caddr_t
)&so
->so_timeo
);
129 soisdisconnecting(so
)
130 register struct socket
*so
;
132 OS_DbgPrint(OSK_MID_TRACE
,("Called %x\n", so
));
133 so
->so_state
&= ~SS_ISCONNECTING
;
134 so
->so_state
|= (SS_ISDISCONNECTING
|SS_CANTRCVMORE
|SS_CANTSENDMORE
);
135 wakeup(so
, (caddr_t
)&so
->so_timeo
);
144 register struct socket
*so
;
146 OS_DbgPrint(OSK_MID_TRACE
,("Called %x\n", so
));
148 so
->so_state
&= ~(SS_ISCONNECTING
|SS_ISCONNECTED
|SS_ISDISCONNECTING
);
149 so
->so_state
|= (SS_CANTRCVMORE
|SS_CANTSENDMORE
);
150 wakeup(so
, (caddr_t
)&so
->so_timeo
);
158 * When an attempt at a new connection is noted on a socket
159 * which accepts connections, sonewconn is called. If the
160 * connection is possible (subject to space constraints, etc.)
161 * then we allocate a new structure, propoerly linked into the
162 * data structure of the original socket, and return this.
163 * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED.
165 * Currently, sonewconn() is defined as sonewconn1() in socketvar.h
166 * to catch calls that are missing the (new) second parameter.
169 sonewconn1(head
, connstatus
)
170 register struct socket
*head
;
173 register struct socket
*so
;
174 int soqueue
= connstatus
? 1 : 0;
176 OS_DbgPrint(OSK_MID_TRACE
,("Called %x\n", head
));
178 if ((head
->so_qlen
+ head
->so_q0len
> 3 * head
->so_qlimit
/ 2) &&
179 (head
->so_qlen
+ head
->so_q0len
> sominqueue
))
180 return ((struct socket
*)0);
181 MALLOC(so
, struct socket
*, sizeof(*so
), M_SOCKET
, M_DONTWAIT
);
183 return ((struct socket
*)0);
184 bzero((caddr_t
)so
, sizeof(*so
));
185 so
->so_type
= head
->so_type
;
186 so
->so_options
= head
->so_options
&~ SO_ACCEPTCONN
;
187 so
->so_linger
= head
->so_linger
;
188 so
->so_state
= head
->so_state
| SS_NOFDREF
;
189 so
->so_proto
= head
->so_proto
;
190 so
->so_timeo
= head
->so_timeo
;
191 so
->so_pgid
= head
->so_pgid
;
192 (void) soreserve(so
, head
->so_snd
.sb_hiwat
, head
->so_rcv
.sb_hiwat
);
193 soqinsque(head
, so
, soqueue
);
194 if ((*so
->so_proto
->pr_usrreq
)(so
, PRU_ATTACH
,
195 (struct mbuf
*)0, (struct mbuf
*)0, (struct mbuf
*)0)) {
196 (void) soqremque(so
, soqueue
);
197 (void) free((caddr_t
)so
, M_SOCKET
);
198 return ((struct socket
*)0);
204 wakeup(head
, (caddr_t
)&head
->so_timeo
);
205 so
->so_state
|= connstatus
;
211 soqinsque(head
, so
, q
)
212 register struct socket
*head
, *so
;
216 register struct socket
**prev
;
221 for (prev
= &(head
->so_q0
); *prev
; )
222 prev
= &((*prev
)->so_q0
);
226 for (prev
= &(head
->so_q
); *prev
; )
227 prev
= &((*prev
)->so_q
);
234 register struct socket
*so
;
237 register struct socket
*head
, *prev
, *next
;
242 next
= q
? prev
->so_q
: prev
->so_q0
;
250 prev
->so_q0
= next
->so_q0
;
253 prev
->so_q
= next
->so_q
;
256 next
->so_q0
= next
->so_q
= 0;
262 * Socantsendmore indicates that no more data will be sent on the
263 * socket; it would normally be applied to a socket when the user
264 * informs the system that no more data is to be sent, by the protocol
265 * code (in case PRU_SHUTDOWN). Socantrcvmore indicates that no more data
266 * will be received, and will normally be applied to the socket by a
267 * protocol when it detects that the peer will send no more data.
268 * Data queued for reading in the socket may yet be read.
276 so
->so_state
|= SS_CANTSENDMORE
;
285 so
->so_state
|= SS_CANTRCVMORE
;
290 * Wait for data to arrive at/drain from a socket buffer.
297 sb
->sb_flags
|= SB_WAIT
;
298 return (tsleep((caddr_t
)&sb
->sb_cc
,
299 (sb
->sb_flags
& SB_NOINTR
) ? PSOCK
: PSOCK
| PCATCH
, netio
,
304 * Lock a sockbuf already known to be locked;
305 * return any error returned from sleep (EINTR).
309 register struct sockbuf
*sb
;
313 while (sb
->sb_flags
& SB_LOCK
) {
314 sb
->sb_flags
|= SB_WANT
;
315 error
= tsleep((caddr_t
)&sb
->sb_flags
,
316 (sb
->sb_flags
& SB_NOINTR
) ? PSOCK
: PSOCK
|PCATCH
,
321 sb
->sb_flags
|= SB_LOCK
;
326 * Wakeup processes waiting on a socket buffer.
327 * Do asynchronous notification via SIGIO
328 * if the socket has the SS_ASYNC flag set.
332 register struct socket
*so
;
333 register struct sockbuf
*sb
;
337 wakeup(so
, &sb
->sb_sel
);
340 * in the OS Kit, we do not want notifications to stop
342 sb
->sb_flags
&= ~SB_SEL
;
344 if (sb
->sb_flags
& SB_WAIT
) {
345 sb
->sb_flags
&= ~SB_WAIT
;
346 wakeup(so
, (caddr_t
)&sb
->sb_cc
);
348 if (so
->so_state
& SS_ASYNC
) {
350 gsignal(-so
->so_pgid
, SIGIO
);
351 else if (so
->so_pgid
> 0 && (p
= pfind(so
->so_pgid
)) != 0)
357 * Socket buffer (struct sockbuf) utility routines.
359 * Each socket contains two socket buffers: one for sending data and
360 * one for receiving data. Each buffer contains a queue of mbufs,
361 * information about the number of mbufs and amount of data in the
362 * queue, and other fields allowing select() statements and notification
363 * on data availability to be implemented.
365 * Data stored in a socket buffer is maintained as a list of records.
366 * Each record is a list of mbufs chained together with the m_next
367 * field. Records are chained together with the m_nextpkt field. The upper
368 * level routine soreceive() expects the following conventions to be
369 * observed when placing information in the receive buffer:
371 * 1. If the protocol requires each message be preceded by the sender's
372 * name, then a record containing that name must be present before
373 * any associated data (mbuf's must be of type MT_SONAME).
374 * 2. If the protocol supports the exchange of ``access rights'' (really
375 * just additional data associated with the message), and there are
376 * ``rights'' to be received, then a record containing this data
377 * should be present (mbuf's must be of type MT_RIGHTS).
378 * 3. If a name or rights record exists, then it must be followed by
379 * a data record, perhaps of zero length.
381 * Before using a new socket structure it is first necessary to reserve
382 * buffer space to the socket, by calling sbreserve(). This should commit
383 * some of the available buffer space in the system buffer pool for the
384 * socket (currently, it does nothing but enforce limits). The space
385 * should be released by calling sbrelease() when the socket is destroyed.
389 soreserve(so
, sndcc
, rcvcc
)
390 register struct socket
*so
;
394 if (sbreserve(&so
->so_snd
, sndcc
) == 0)
396 if (sbreserve(&so
->so_rcv
, rcvcc
) == 0)
398 if (so
->so_rcv
.sb_lowat
== 0)
399 so
->so_rcv
.sb_lowat
= 1;
400 if (so
->so_snd
.sb_lowat
== 0)
401 so
->so_snd
.sb_lowat
= MCLBYTES
;
402 if (so
->so_snd
.sb_lowat
> so
->so_snd
.sb_hiwat
)
403 so
->so_snd
.sb_lowat
= so
->so_snd
.sb_hiwat
;
406 sbrelease(&so
->so_snd
);
412 * Allot mbufs to a sockbuf.
413 * Attempt to scale mbmax so that mbcnt doesn't become limiting
414 * if buffering efficiency is near the normal case.
422 if (cc
> sb_max
* MCLBYTES
/ (MSIZE
+ MCLBYTES
))
425 sb
->sb_mbmax
= min(cc
* sb_efficiency
, sb_max
);
426 if (sb
->sb_lowat
> sb
->sb_hiwat
)
427 sb
->sb_lowat
= sb
->sb_hiwat
;
432 * Free mbufs held by a socket, and reserved mbuf space.
440 sb
->sb_hiwat
= sb
->sb_mbmax
= 0;
444 * Routines to add and remove
445 * data from an mbuf queue.
447 * The routines sbappend() or sbappendrecord() are normally called to
448 * append new mbufs to a socket buffer, after checking that adequate
449 * space is available, comparing the function sbspace() with the amount
450 * of data to be added. sbappendrecord() differs from sbappend() in
451 * that data supplied is treated as the beginning of a new record.
452 * To place a sender's address, optional access rights, and data in a
453 * socket receive buffer, sbappendaddr() should be used. To place
454 * access rights and data in a socket receive buffer, sbappendrights()
455 * should be used. In either case, the new data begins a new record.
456 * Note that unlike sbappend() and sbappendrecord(), these routines check
457 * for the caller that there will be enough space to store the data.
458 * Each fails if there is not enough space, or if it cannot find mbufs
459 * to store additional information in.
461 * Reliable protocols may use the socket send buffer to hold data
462 * awaiting acknowledgement. Data is normally copied from a socket
463 * send buffer in a protocol with m_copy for output to a peer,
464 * and then removing the data from the socket buffer with sbdrop()
465 * or sbdroprecord() when the data is acknowledged by the peer.
469 * Append mbuf chain m to the last record in the
470 * socket buffer sb. The additional space associated
471 * the mbuf chain is recorded in sb. Empty mbufs are
472 * discarded and mbufs are compacted where possible.
479 register struct mbuf
*n
;
481 OS_DbgPrint(OSK_MID_TRACE
,("Called\n"));
490 if (n
->m_flags
& M_EOR
) {
491 sbappendrecord(sb
, m
); /* XXXXXX!!!! */
492 OS_DbgPrint(OSK_MID_TRACE
,("Leaving (rec)\n"));
495 } while (n
->m_next
&& (n
= n
->m_next
));
497 sbcompress(sb
, m
, n
);
499 OS_DbgPrint(OSK_MID_TRACE
,("Leaving\n"));
505 register struct sockbuf
*sb
;
507 register struct mbuf
*m
;
508 register int len
= 0, mbcnt
= 0;
510 for (m
= sb
->sb_mb
; m
; m
= m
->m_next
) {
513 if (m
->m_flags
& M_EXT
)
514 mbcnt
+= m
->m_ext
.ext_size
;
516 panic("sbcheck nextpkt");
518 if (len
!= sb
->sb_cc
|| mbcnt
!= sb
->sb_mbcnt
) {
519 printf("cc %d != %d || mbcnt %d != %d\n", len
, sb
->sb_cc
,
520 mbcnt
, sb
->sb_mbcnt
);
527 * As above, except the mbuf chain
528 * begins a new record.
531 sbappendrecord(sb
, m0
)
532 register struct sockbuf
*sb
;
533 register struct mbuf
*m0
;
535 register struct mbuf
*m
;
544 * Put the first mbuf on the queue.
545 * Note this permits zero length records.
554 if (m
&& (m0
->m_flags
& M_EOR
)) {
555 m0
->m_flags
&= ~M_EOR
;
558 sbcompress(sb
, m
, m0
);
562 * As above except that OOB data
563 * is inserted at the beginning of the sockbuf,
564 * but after any other OOB data.
568 register struct sockbuf
*sb
;
569 register struct mbuf
*m0
;
571 register struct mbuf
*m
;
572 register struct mbuf
**mp
;
576 for (mp
= &sb
->sb_mb
; *mp
; mp
= &((*mp
)->m_nextpkt
)) {
582 continue; /* WANT next train */
587 goto again
; /* inspect THIS train further */
592 * Put the first mbuf on the queue.
593 * Note this permits zero length records.
600 if (m
&& (m0
->m_flags
& M_EOR
)) {
601 m0
->m_flags
&= ~M_EOR
;
604 sbcompress(sb
, m
, m0
);
608 * Append address and data, and optionally, control (ancillary) data
609 * to the receive queue of a socket. If present,
610 * m0 must include a packet header with total length.
611 * Returns 0 if no space in sockbuf or insufficient mbufs.
614 sbappendaddr(sb
, asa
, m0
, control
)
615 register struct sockbuf
*sb
;
616 struct sockaddr
*asa
;
617 struct mbuf
*m0
, *control
;
619 register struct mbuf
*m
, *n
;
620 int space
= asa
->sa_len
;
622 if (m0
&& (m0
->m_flags
& M_PKTHDR
) == 0)
623 panic("sbappendaddr");
625 space
+= m0
->m_pkthdr
.len
;
626 for (n
= control
; n
; n
= n
->m_next
) {
628 if (n
->m_next
== 0) /* keep pointer to last control buf */
631 if (space
> sbspace(sb
))
633 if (asa
->sa_len
> MLEN
)
635 MGET(m
, M_DONTWAIT
, MT_SONAME
);
638 m
->m_len
= asa
->sa_len
;
639 bcopy((caddr_t
)asa
, mtod(m
, caddr_t
), asa
->sa_len
);
641 n
->m_next
= m0
; /* concatenate data to control */
645 for (n
= m
; n
; n
= n
->m_next
)
658 sbappendcontrol(sb
, m0
, control
)
660 struct mbuf
*control
, *m0
;
662 register struct mbuf
*m
, *n
;
666 panic("sbappendcontrol");
667 for (m
= control
; ; m
= m
->m_next
) {
672 n
= m
; /* save pointer to last control buffer */
673 for (m
= m0
; m
; m
= m
->m_next
)
675 if (space
> sbspace(sb
))
677 n
->m_next
= m0
; /* concatenate data to control */
678 for (m
= control
; m
; m
= m
->m_next
)
684 n
->m_nextpkt
= control
;
691 * Compress mbuf chain m into the socket
692 * buffer sb following mbuf n. If n
693 * is null, the buffer is presumed empty.
697 register struct sockbuf
*sb
;
698 register struct mbuf
*m
, *n
;
700 register int eor
= 0;
701 register struct mbuf
*o
;
704 eor
|= m
->m_flags
& M_EOR
;
707 (((o
= m
->m_next
) || (o
= n
)) &&
708 o
->m_type
== m
->m_type
))) {
712 if (n
&& (n
->m_flags
& (M_EXT
| M_EOR
)) == 0 &&
713 (n
->m_data
+ n
->m_len
+ m
->m_len
) < &n
->m_dat
[MLEN
] &&
714 n
->m_type
== m
->m_type
) {
715 bcopy(mtod(m
, caddr_t
), mtod(n
, caddr_t
) + n
->m_len
,
717 n
->m_len
+= m
->m_len
;
718 sb
->sb_cc
+= m
->m_len
;
728 m
->m_flags
&= ~M_EOR
;
736 printf("semi-panic: sbcompress\n");
741 * Free all mbufs in a sockbuf.
742 * Check that all resources are reclaimed.
746 register struct sockbuf
*sb
;
749 if (sb
->sb_flags
& SB_LOCK
)
752 sbdrop(sb
, (int)sb
->sb_cc
);
753 if (sb
->sb_cc
|| sb
->sb_mb
)
758 * Drop data from (the front of) a sockbuf.
762 register struct sockbuf
*sb
;
765 register struct mbuf
*m
, *mn
;
768 next
= (m
= sb
->sb_mb
) ? m
->m_nextpkt
: 0;
777 if (m
->m_len
> len
) {
788 while (m
&& m
->m_len
== 0) {
801 * Drop a record off the front of a sockbuf
802 * and move the next record to the front.
806 register struct sockbuf
*sb
;
808 register struct mbuf
*m
, *mn
;
812 sb
->sb_mb
= m
->m_nextpkt
;