1 /* $Id: uipc_socket2.c,v 1.1.1.1 2006/09/14 01:59:08 root Exp $ */
2 /* $OpenBSD: uipc_socket2.c,v 1.10 1999/02/19 15:06:52 millert Exp $ */
3 /* $NetBSD: uipc_socket2.c,v 1.11 1996/02/04 02:17:55 christos Exp $ */
6 * Copyright (c) 1982, 1986, 1988, 1990, 1993
7 * The Regents of the University of California. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * @(#)uipc_socket2.c 8.1 (Berkeley) 6/10/93
40 #include <sys/param.h>
41 #include <sys/systm.h>
45 #include <sys/malloc.h>
47 #include <sys/protosw.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/signalvar.h>
53 * Primitive routines for operating on sockets and socket buffers
56 /* strings for sleep message: */
57 char netio
[] = "netio";
58 char netcon
[] = "netcon";
59 char netcls
[] = "netcls";
61 u_long sb_max
= SB_MAX
; /* patchable */
64 * Procedures to manipulate state flags of socket
65 * and do appropriate wakeups. Normal sequence from the
66 * active (originating) side is that soisconnecting() is
67 * called during processing of connect() call,
68 * resulting in an eventual call to soisconnected() if/when the
69 * connection is established. When the connection is torn down
70 * soisdisconnecting() is called during processing of disconnect() call,
71 * and soisdisconnected() is called when the connection to the peer
72 * is totally severed. The semantics of these routines are such that
73 * connectionless protocols can call soisconnected() and soisdisconnected()
74 * only, bypassing the in-progress calls when setting up a ``connection''
77 * From the passive side, a socket is created with
78 * two queues of sockets: so_q0 for connections in progress
79 * and so_q for connections already made and awaiting user acceptance.
80 * As a protocol is preparing incoming connections, it creates a socket
81 * structure queued on so_q0 by calling sonewconn(). When the connection
82 * is established, soisconnected() is called, and transfers the
83 * socket structure to so_q, making it available to accept().
85 * If a socket is closed with sockets on either
86 * so_q0 or so_q, these sockets are dropped.
88 * If higher level protocols are implemented in
89 * the kernel, the wakeups done here will sometimes
90 * cause software-interrupt process scheduling.
95 register struct socket
*so
;
98 so
->so_state
&= ~(SS_ISCONNECTED
|SS_ISDISCONNECTING
);
99 so
->so_state
|= SS_ISCONNECTING
;
104 register struct socket
*so
;
106 register struct socket
*head
= so
->so_head
;
108 so
->so_state
&= ~(SS_ISCONNECTING
|SS_ISDISCONNECTING
|SS_ISCONFIRMING
);
109 so
->so_state
|= SS_ISCONNECTED
;
110 if (head
&& soqremque(so
, 0)) {
111 soqinsque(head
, so
, 1);
113 wakeup((caddr_t
)&head
->so_timeo
);
115 wakeup((caddr_t
)&so
->so_timeo
);
122 soisdisconnecting(so
)
123 register struct socket
*so
;
126 so
->so_state
&= ~SS_ISCONNECTING
;
127 so
->so_state
|= (SS_ISDISCONNECTING
|SS_CANTRCVMORE
|SS_CANTSENDMORE
);
128 wakeup((caddr_t
)&so
->so_timeo
);
135 register struct socket
*so
;
138 so
->so_state
&= ~(SS_ISCONNECTING
|SS_ISCONNECTED
|SS_ISDISCONNECTING
);
139 so
->so_state
|= (SS_CANTRCVMORE
|SS_CANTSENDMORE
|SS_ISDISCONNECTED
);
140 wakeup((caddr_t
)&so
->so_timeo
);
146 * When an attempt at a new connection is noted on a socket
147 * which accepts connections, sonewconn is called. If the
148 * connection is possible (subject to space constraints, etc.)
149 * then we allocate a new structure, propoerly linked into the
150 * data structure of the original socket, and return this.
151 * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED.
153 * Currently, sonewconn() is defined as sonewconn1() in socketvar.h
154 * to catch calls that are missing the (new) second parameter.
157 sonewconn1(head
, connstatus
)
158 register struct socket
*head
;
161 register struct socket
*so
;
162 int soqueue
= connstatus
? 1 : 0;
164 if (head
->so_qlen
+ head
->so_q0len
> head
->so_qlimit
* 3)
165 return ((struct socket
*)0);
166 MALLOC(so
, struct socket
*, sizeof(*so
), M_SOCKET
, M_DONTWAIT
);
168 return ((struct socket
*)0);
169 bzero((caddr_t
)so
, sizeof(*so
));
170 so
->so_type
= head
->so_type
;
171 so
->so_options
= head
->so_options
&~ SO_ACCEPTCONN
;
172 so
->so_linger
= head
->so_linger
;
173 so
->so_state
= head
->so_state
| SS_NOFDREF
;
174 so
->so_proto
= head
->so_proto
;
175 so
->so_timeo
= head
->so_timeo
;
176 so
->so_pgid
= head
->so_pgid
;
177 so
->so_euid
= head
->so_euid
;
178 so
->so_ruid
= head
->so_ruid
;
179 (void) soreserve(so
, head
->so_snd
.sb_hiwat
, head
->so_rcv
.sb_hiwat
);
180 soqinsque(head
, so
, soqueue
);
181 if ((*so
->so_proto
->pr_usrreq
)(so
, PRU_ATTACH
,
182 (struct mbuf
*)0, (struct mbuf
*)0, (struct mbuf
*)0)) {
183 (void) soqremque(so
, soqueue
);
184 (void) free((caddr_t
)so
, M_SOCKET
);
185 return ((struct socket
*)0);
189 wakeup((caddr_t
)&head
->so_timeo
);
190 so
->so_state
|= connstatus
;
196 soqinsque(head
, so
, q
)
197 register struct socket
*head
, *so
;
201 register struct socket
**prev
;
206 for (prev
= &(head
->so_q0
); *prev
; )
207 prev
= &((*prev
)->so_q0
);
211 for (prev
= &(head
->so_q
); *prev
; )
212 prev
= &((*prev
)->so_q
);
219 register struct socket
*so
;
222 register struct socket
*head
, *prev
, *next
;
227 next
= q
? prev
->so_q
: prev
->so_q0
;
235 prev
->so_q0
= next
->so_q0
;
238 prev
->so_q
= next
->so_q
;
241 next
->so_q0
= next
->so_q
= 0;
247 * Socantsendmore indicates that no more data will be sent on the
248 * socket; it would normally be applied to a socket when the user
249 * informs the system that no more data is to be sent, by the protocol
250 * code (in case PRU_SHUTDOWN). Socantrcvmore indicates that no more data
251 * will be received, and will normally be applied to the socket by a
252 * protocol when it detects that the peer will send no more data.
253 * Data queued for reading in the socket may yet be read.
261 so
->so_state
|= SS_CANTSENDMORE
;
270 so
->so_state
|= SS_CANTRCVMORE
;
275 * Wait for data to arrive at/drain from a socket buffer.
282 sb
->sb_flags
|= SB_WAIT
;
283 return (tsleep((caddr_t
)&sb
->sb_cc
,
284 (sb
->sb_flags
& SB_NOINTR
) ? PSOCK
: PSOCK
| PCATCH
, netio
,
289 * Lock a sockbuf already known to be locked;
290 * return any error returned from sleep (EINTR).
294 register struct sockbuf
*sb
;
298 while (sb
->sb_flags
& SB_LOCK
) {
299 sb
->sb_flags
|= SB_WANT
;
300 error
= tsleep((caddr_t
)&sb
->sb_flags
,
301 (sb
->sb_flags
& SB_NOINTR
) ?
302 PSOCK
: PSOCK
|PCATCH
, netio
, 0);
306 sb
->sb_flags
|= SB_LOCK
;
311 * Wakeup processes waiting on a socket buffer.
312 * Do asynchronous notification via SIGIO
313 * if the socket has the SS_ASYNC flag set.
317 register struct socket
*so
;
318 register struct sockbuf
*sb
;
320 selwakeup(&sb
->sb_sel
);
321 sb
->sb_flags
&= ~SB_SEL
;
322 if (sb
->sb_flags
& SB_WAIT
) {
323 sb
->sb_flags
&= ~SB_WAIT
;
324 wakeup((caddr_t
)&sb
->sb_cc
);
326 if (so
->so_state
& SS_ASYNC
)
327 csignal(so
->so_pgid
, SIGIO
, so
->so_siguid
, so
->so_sigeuid
);
331 * Socket buffer (struct sockbuf) utility routines.
333 * Each socket contains two socket buffers: one for sending data and
334 * one for receiving data. Each buffer contains a queue of mbufs,
335 * information about the number of mbufs and amount of data in the
336 * queue, and other fields allowing select() statements and notification
337 * on data availability to be implemented.
339 * Data stored in a socket buffer is maintained as a list of records.
340 * Each record is a list of mbufs chained together with the m_next
341 * field. Records are chained together with the m_nextpkt field. The upper
342 * level routine soreceive() expects the following conventions to be
343 * observed when placing information in the receive buffer:
345 * 1. If the protocol requires each message be preceded by the sender's
346 * name, then a record containing that name must be present before
347 * any associated data (mbuf's must be of type MT_SONAME).
348 * 2. If the protocol supports the exchange of ``access rights'' (really
349 * just additional data associated with the message), and there are
350 * ``rights'' to be received, then a record containing this data
351 * should be present (mbuf's must be of type MT_CONTROL).
352 * 3. If a name or rights record exists, then it must be followed by
353 * a data record, perhaps of zero length.
355 * Before using a new socket structure it is first necessary to reserve
356 * buffer space to the socket, by calling sbreserve(). This should commit
357 * some of the available buffer space in the system buffer pool for the
358 * socket (currently, it does nothing but enforce limits). The space
359 * should be released by calling sbrelease() when the socket is destroyed.
363 soreserve(so
, sndcc
, rcvcc
)
364 register struct socket
*so
;
368 if (sbreserve(&so
->so_snd
, sndcc
) == 0)
370 if (sbreserve(&so
->so_rcv
, rcvcc
) == 0)
372 if (so
->so_rcv
.sb_lowat
== 0)
373 so
->so_rcv
.sb_lowat
= 1;
374 if (so
->so_snd
.sb_lowat
== 0)
375 so
->so_snd
.sb_lowat
= MCLBYTES
;
376 if (so
->so_snd
.sb_lowat
> so
->so_snd
.sb_hiwat
)
377 so
->so_snd
.sb_lowat
= so
->so_snd
.sb_hiwat
;
380 sbrelease(&so
->so_snd
);
386 * Allot mbufs to a sockbuf.
387 * Attempt to scale mbmax so that mbcnt doesn't become limiting
388 * if buffering efficiency is near the normal case.
396 if (cc
== 0 || cc
> sb_max
* MCLBYTES
/ (MSIZE
+ MCLBYTES
))
399 sb
->sb_mbmax
= min(cc
* 2, sb_max
);
400 if (sb
->sb_lowat
> sb
->sb_hiwat
)
401 sb
->sb_lowat
= sb
->sb_hiwat
;
406 * Free mbufs held by a socket, and reserved mbuf space.
414 sb
->sb_hiwat
= sb
->sb_mbmax
= 0;
418 * Routines to add and remove
419 * data from an mbuf queue.
421 * The routines sbappend() or sbappendrecord() are normally called to
422 * append new mbufs to a socket buffer, after checking that adequate
423 * space is available, comparing the function sbspace() with the amount
424 * of data to be added. sbappendrecord() differs from sbappend() in
425 * that data supplied is treated as the beginning of a new record.
426 * To place a sender's address, optional access rights, and data in a
427 * socket receive buffer, sbappendaddr() should be used. To place
428 * access rights and data in a socket receive buffer, sbappendrights()
429 * should be used. In either case, the new data begins a new record.
430 * Note that unlike sbappend() and sbappendrecord(), these routines check
431 * for the caller that there will be enough space to store the data.
432 * Each fails if there is not enough space, or if it cannot find mbufs
433 * to store additional information in.
435 * Reliable protocols may use the socket send buffer to hold data
436 * awaiting acknowledgement. Data is normally copied from a socket
437 * send buffer in a protocol with m_copy for output to a peer,
438 * and then removing the data from the socket buffer with sbdrop()
439 * or sbdroprecord() when the data is acknowledged by the peer.
443 * Append mbuf chain m to the last record in the
444 * socket buffer sb. The additional space associated
445 * the mbuf chain is recorded in sb. Empty mbufs are
446 * discarded and mbufs are compacted where possible.
453 register struct mbuf
*n
;
457 if ((n
= sb
->sb_mb
) != NULL
) {
461 if (n
->m_flags
& M_EOR
) {
462 sbappendrecord(sb
, m
); /* XXXXXX!!!! */
465 } while (n
->m_next
&& (n
= n
->m_next
));
467 sbcompress(sb
, m
, n
);
473 register struct sockbuf
*sb
;
475 register struct mbuf
*m
;
476 register int len
= 0, mbcnt
= 0;
478 for (m
= sb
->sb_mb
; m
; m
= m
->m_next
) {
481 if (m
->m_flags
& M_EXT
)
482 mbcnt
+= m
->m_ext
.ext_size
;
484 panic("sbcheck nextpkt");
486 if (len
!= sb
->sb_cc
|| mbcnt
!= sb
->sb_mbcnt
) {
487 printf("cc %d != %d || mbcnt %d != %d\n", len
, sb
->sb_cc
,
488 mbcnt
, sb
->sb_mbcnt
);
495 * As above, except the mbuf chain
496 * begins a new record.
499 sbappendrecord(sb
, m0
)
500 register struct sockbuf
*sb
;
501 register struct mbuf
*m0
;
503 register struct mbuf
*m
;
507 if ((m
= sb
->sb_mb
) != NULL
)
511 * Put the first mbuf on the queue.
512 * Note this permits zero length records.
521 if (m
&& (m0
->m_flags
& M_EOR
)) {
522 m0
->m_flags
&= ~M_EOR
;
525 sbcompress(sb
, m
, m0
);
529 * As above except that OOB data
530 * is inserted at the beginning of the sockbuf,
531 * but after any other OOB data.
535 register struct sockbuf
*sb
;
536 register struct mbuf
*m0
;
538 register struct mbuf
*m
;
539 register struct mbuf
**mp
;
543 for (mp
= &sb
->sb_mb
; (m
= *mp
) != NULL
; mp
= &((*mp
)->m_nextpkt
)) {
548 continue; /* WANT next train */
551 if ((m
= m
->m_next
) != NULL
)
552 goto again
; /* inspect THIS train further */
557 * Put the first mbuf on the queue.
558 * Note this permits zero length records.
565 if (m
&& (m0
->m_flags
& M_EOR
)) {
566 m0
->m_flags
&= ~M_EOR
;
569 sbcompress(sb
, m
, m0
);
573 * Append address and data, and optionally, control (ancillary) data
574 * to the receive queue of a socket. If present,
575 * m0 must include a packet header with total length.
576 * Returns 0 if no space in sockbuf or insufficient mbufs.
579 sbappendaddr(sb
, asa
, m0
, control
)
580 register struct sockbuf
*sb
;
581 struct sockaddr
*asa
;
582 struct mbuf
*m0
, *control
;
584 register struct mbuf
*m
, *n
;
585 int space
= asa
->sa_len
;
587 if (m0
&& (m0
->m_flags
& M_PKTHDR
) == 0)
588 panic("sbappendaddr");
590 space
+= m0
->m_pkthdr
.len
;
591 for (n
= control
; n
; n
= n
->m_next
) {
593 if (n
->m_next
== 0) /* keep pointer to last control buf */
596 if (space
> sbspace(sb
))
598 if (asa
->sa_len
> MLEN
)
600 MGET(m
, M_DONTWAIT
, MT_SONAME
);
603 m
->m_len
= asa
->sa_len
;
604 bcopy((caddr_t
)asa
, mtod(m
, caddr_t
), asa
->sa_len
);
606 n
->m_next
= m0
; /* concatenate data to control */
610 for (n
= m
; n
; n
= n
->m_next
)
612 if ((n
= sb
->sb_mb
) != NULL
) {
622 sbappendcontrol(sb
, m0
, control
)
624 struct mbuf
*m0
, *control
;
626 register struct mbuf
*m
, *n
;
630 panic("sbappendcontrol");
631 for (m
= control
; ; m
= m
->m_next
) {
636 n
= m
; /* save pointer to last control buffer */
637 for (m
= m0
; m
; m
= m
->m_next
)
639 if (space
> sbspace(sb
))
641 n
->m_next
= m0
; /* concatenate data to control */
642 for (m
= control
; m
; m
= m
->m_next
)
644 if ((n
= sb
->sb_mb
) != NULL
) {
647 n
->m_nextpkt
= control
;
654 * Compress mbuf chain m into the socket
655 * buffer sb following mbuf n. If n
656 * is null, the buffer is presumed empty.
660 register struct sockbuf
*sb
;
661 register struct mbuf
*m
, *n
;
663 register int eor
= 0;
664 register struct mbuf
*o
;
667 eor
|= m
->m_flags
& M_EOR
;
670 (((o
= m
->m_next
) || (o
= n
)) &&
671 o
->m_type
== m
->m_type
))) {
675 if (n
&& (n
->m_flags
& (M_EXT
| M_EOR
)) == 0 &&
676 (n
->m_data
+ n
->m_len
+ m
->m_len
) < &n
->m_dat
[MLEN
] &&
677 n
->m_type
== m
->m_type
) {
678 bcopy(mtod(m
, caddr_t
), mtod(n
, caddr_t
) + n
->m_len
,
680 n
->m_len
+= m
->m_len
;
681 sb
->sb_cc
+= m
->m_len
;
691 m
->m_flags
&= ~M_EOR
;
699 printf("semi-panic: sbcompress\n");
704 * Free all mbufs in a sockbuf.
705 * Check that all resources are reclaimed.
709 register struct sockbuf
*sb
;
712 if (sb
->sb_flags
& SB_LOCK
)
715 sbdrop(sb
, (int)sb
->sb_cc
);
716 if (sb
->sb_cc
|| sb
->sb_mb
)
721 * Drop data from (the front of) a sockbuf.
725 register struct sockbuf
*sb
;
728 register struct mbuf
*m
, *mn
;
731 next
= (m
= sb
->sb_mb
) ? m
->m_nextpkt
: 0;
740 if (m
->m_len
> len
) {
751 while (m
&& m
->m_len
== 0) {
764 * Drop a record off the front of a sockbuf
765 * and move the next record to the front.
769 register struct sockbuf
*sb
;
771 register struct mbuf
*m
, *mn
;
775 sb
->sb_mb
= m
->m_nextpkt
;
779 } while ((m
= mn
) != NULL
);