xref: /freebsd/sys/kern/uipc_sockbuf.c (revision 802082390b201913ad7ce602d7825c7f05ae489b)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1988, 1990, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
14df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
15df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
16df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
17df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
18df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
19df8bae1dSRodney W. Grimes  *    without specific prior written permission.
20df8bae1dSRodney W. Grimes  *
21df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
32df8bae1dSRodney W. Grimes  *
33df8bae1dSRodney W. Grimes  *	@(#)uipc_socket2.c	8.1 (Berkeley) 6/10/93
34c3aac50fSPeter Wemm  * $FreeBSD$
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
375b86eac4SJesper Skriver #include "opt_param.h"
38df8bae1dSRodney W. Grimes #include <sys/param.h>
39960ed29cSSeigo Tanimura #include <sys/aio.h> /* for aio_swake proto */
4098271db4SGarrett Wollman #include <sys/domain.h>
41960ed29cSSeigo Tanimura #include <sys/event.h>
42134c934cSMike Smith #include <sys/file.h>	/* for maxfiles */
43ff5c09daSGarrett Wollman #include <sys/kernel.h>
44fb919e4dSMark Murray #include <sys/lock.h>
45df8bae1dSRodney W. Grimes #include <sys/malloc.h>
46df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
47960ed29cSSeigo Tanimura #include <sys/mutex.h>
48fb919e4dSMark Murray #include <sys/proc.h>
49df8bae1dSRodney W. Grimes #include <sys/protosw.h>
502f9a2132SBrian Feldman #include <sys/resourcevar.h>
51960ed29cSSeigo Tanimura #include <sys/signalvar.h>
52df8bae1dSRodney W. Grimes #include <sys/socket.h>
53df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
54960ed29cSSeigo Tanimura #include <sys/stat.h>
55ff5c09daSGarrett Wollman #include <sys/sysctl.h>
56960ed29cSSeigo Tanimura #include <sys/systm.h>
5726f9a767SRodney W. Grimes 
5854d77689SJeff Roberson int	maxsockets;
5954d77689SJeff Roberson 
6021d56e9cSAlfred Perlstein void (*aio_swake)(struct socket *, struct sockbuf *);
6121d56e9cSAlfred Perlstein 
62df8bae1dSRodney W. Grimes /*
63df8bae1dSRodney W. Grimes  * Primitive routines for operating on sockets and socket buffers
64df8bae1dSRodney W. Grimes  */
65df8bae1dSRodney W. Grimes 
66ff5c09daSGarrett Wollman u_long	sb_max = SB_MAX;		/* XXX should be static */
67df8bae1dSRodney W. Grimes 
684b29bc4fSGarrett Wollman static	u_long sb_efficiency = 8;	/* parameter for sbreserve() */
694b29bc4fSGarrett Wollman 
70df8bae1dSRodney W. Grimes /*
71df8bae1dSRodney W. Grimes  * Procedures to manipulate state flags of socket
72df8bae1dSRodney W. Grimes  * and do appropriate wakeups.  Normal sequence from the
73df8bae1dSRodney W. Grimes  * active (originating) side is that soisconnecting() is
74df8bae1dSRodney W. Grimes  * called during processing of connect() call,
75df8bae1dSRodney W. Grimes  * resulting in an eventual call to soisconnected() if/when the
76df8bae1dSRodney W. Grimes  * connection is established.  When the connection is torn down
77df8bae1dSRodney W. Grimes  * soisdisconnecting() is called during processing of disconnect() call,
78df8bae1dSRodney W. Grimes  * and soisdisconnected() is called when the connection to the peer
79df8bae1dSRodney W. Grimes  * is totally severed.  The semantics of these routines are such that
80df8bae1dSRodney W. Grimes  * connectionless protocols can call soisconnected() and soisdisconnected()
81df8bae1dSRodney W. Grimes  * only, bypassing the in-progress calls when setting up a ``connection''
82df8bae1dSRodney W. Grimes  * takes no time.
83df8bae1dSRodney W. Grimes  *
84df8bae1dSRodney W. Grimes  * From the passive side, a socket is created with
85dc97381eSPeter Wemm  * two queues of sockets: so_incomp for connections in progress
86dc97381eSPeter Wemm  * and so_comp for connections already made and awaiting user acceptance.
87df8bae1dSRodney W. Grimes  * As a protocol is preparing incoming connections, it creates a socket
88dc97381eSPeter Wemm  * structure queued on so_incomp by calling sonewconn().  When the connection
89df8bae1dSRodney W. Grimes  * is established, soisconnected() is called, and transfers the
90dc97381eSPeter Wemm  * socket structure to so_comp, making it available to accept().
91df8bae1dSRodney W. Grimes  *
92df8bae1dSRodney W. Grimes  * If a socket is closed with sockets on either
93dc97381eSPeter Wemm  * so_incomp or so_comp, these sockets are dropped.
94df8bae1dSRodney W. Grimes  *
95df8bae1dSRodney W. Grimes  * If higher level protocols are implemented in
96df8bae1dSRodney W. Grimes  * the kernel, the wakeups done here will sometimes
97df8bae1dSRodney W. Grimes  * cause software-interrupt process scheduling.
98df8bae1dSRodney W. Grimes  */
99df8bae1dSRodney W. Grimes 
10026f9a767SRodney W. Grimes void
101df8bae1dSRodney W. Grimes soisconnecting(so)
102df8bae1dSRodney W. Grimes 	register struct socket *so;
103df8bae1dSRodney W. Grimes {
104df8bae1dSRodney W. Grimes 
105df8bae1dSRodney W. Grimes 	so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
106df8bae1dSRodney W. Grimes 	so->so_state |= SS_ISCONNECTING;
107df8bae1dSRodney W. Grimes }
108df8bae1dSRodney W. Grimes 
10926f9a767SRodney W. Grimes void
110d48d4b25SSeigo Tanimura soisconnected(so)
111d48d4b25SSeigo Tanimura 	struct socket *so;
112d48d4b25SSeigo Tanimura {
1134cc20ab1SSeigo Tanimura 	struct socket *head = so->so_head;
114d48d4b25SSeigo Tanimura 
115d48d4b25SSeigo Tanimura 	so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING);
116d48d4b25SSeigo Tanimura 	so->so_state |= SS_ISCONNECTED;
117d48d4b25SSeigo Tanimura 	if (head && (so->so_state & SS_INCOMP)) {
118d48d4b25SSeigo Tanimura 		if ((so->so_options & SO_ACCEPTFILTER) != 0) {
1194cc20ab1SSeigo Tanimura 			so->so_upcall = head->so_accf->so_accept_filter->accf_callback;
1204cc20ab1SSeigo Tanimura 			so->so_upcallarg = head->so_accf->so_accept_filter_arg;
121d48d4b25SSeigo Tanimura 			so->so_rcv.sb_flags |= SB_UPCALL;
122d48d4b25SSeigo Tanimura 			so->so_options &= ~SO_ACCEPTFILTER;
1234cc20ab1SSeigo Tanimura 			so->so_upcall(so, so->so_upcallarg, 0);
124a79b7128SAlfred Perlstein 			return;
125a79b7128SAlfred Perlstein 		}
126be24e9e8SDavid Greenman 		TAILQ_REMOVE(&head->so_incomp, so, so_list);
127a51764a8SPaul Traina 		head->so_incqlen--;
1284cc20ab1SSeigo Tanimura 		so->so_state &= ~SS_INCOMP;
129be24e9e8SDavid Greenman 		TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
130e1f1827fSMike Silbersack 		head->so_qlen++;
1314cc20ab1SSeigo Tanimura 		so->so_state |= SS_COMP;
13203e49181SSeigo Tanimura 		sorwakeup(head);
133a91b8721SDavid Greenman 		wakeup_one(&head->so_timeo);
134df8bae1dSRodney W. Grimes 	} else {
135a91b8721SDavid Greenman 		wakeup(&so->so_timeo);
13603e49181SSeigo Tanimura 		sorwakeup(so);
13703e49181SSeigo Tanimura 		sowwakeup(so);
138df8bae1dSRodney W. Grimes 	}
139df8bae1dSRodney W. Grimes }
140df8bae1dSRodney W. Grimes 
14126f9a767SRodney W. Grimes void
142df8bae1dSRodney W. Grimes soisdisconnecting(so)
143df8bae1dSRodney W. Grimes 	register struct socket *so;
144df8bae1dSRodney W. Grimes {
145df8bae1dSRodney W. Grimes 
146df8bae1dSRodney W. Grimes 	so->so_state &= ~SS_ISCONNECTING;
147df8bae1dSRodney W. Grimes 	so->so_state |= (SS_ISDISCONNECTING|SS_CANTRCVMORE|SS_CANTSENDMORE);
14880208239SAlfred Perlstein 	wakeup(&so->so_timeo);
14903e49181SSeigo Tanimura 	sowwakeup(so);
15003e49181SSeigo Tanimura 	sorwakeup(so);
151df8bae1dSRodney W. Grimes }
152df8bae1dSRodney W. Grimes 
15326f9a767SRodney W. Grimes void
154df8bae1dSRodney W. Grimes soisdisconnected(so)
155df8bae1dSRodney W. Grimes 	register struct socket *so;
156df8bae1dSRodney W. Grimes {
157df8bae1dSRodney W. Grimes 
15803e49181SSeigo Tanimura 	so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
15903e49181SSeigo Tanimura 	so->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE|SS_ISDISCONNECTED);
16080208239SAlfred Perlstein 	wakeup(&so->so_timeo);
16103e49181SSeigo Tanimura 	sowwakeup(so);
16203e49181SSeigo Tanimura 	sorwakeup(so);
163df8bae1dSRodney W. Grimes }
164df8bae1dSRodney W. Grimes 
165df8bae1dSRodney W. Grimes /*
166df8bae1dSRodney W. Grimes  * When an attempt at a new connection is noted on a socket
167df8bae1dSRodney W. Grimes  * which accepts connections, sonewconn is called.  If the
168df8bae1dSRodney W. Grimes  * connection is possible (subject to space constraints, etc.)
169df8bae1dSRodney W. Grimes  * then we allocate a new structure, propoerly linked into the
170df8bae1dSRodney W. Grimes  * data structure of the original socket, and return this.
171df8bae1dSRodney W. Grimes  * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED.
172b1e4abd2SMatthew Dillon  *
173b1e4abd2SMatthew Dillon  * note: the ref count on the socket is 0 on return
174df8bae1dSRodney W. Grimes  */
175df8bae1dSRodney W. Grimes struct socket *
176548af278SBill Fenner sonewconn(head, connstatus)
177df8bae1dSRodney W. Grimes 	register struct socket *head;
178df8bae1dSRodney W. Grimes 	int connstatus;
179df8bae1dSRodney W. Grimes {
180df8bae1dSRodney W. Grimes 	register struct socket *so;
181df8bae1dSRodney W. Grimes 
182ebb0cbeaSPaul Traina 	if (head->so_qlen > 3 * head->so_qlimit / 2)
183df8bae1dSRodney W. Grimes 		return ((struct socket *)0);
18498271db4SGarrett Wollman 	so = soalloc(0);
185df8bae1dSRodney W. Grimes 	if (so == NULL)
186df8bae1dSRodney W. Grimes 		return ((struct socket *)0);
187205b2b61SPeter Wemm 	if ((head->so_options & SO_ACCEPTFILTER) != 0)
188205b2b61SPeter Wemm 		connstatus = 0;
189be24e9e8SDavid Greenman 	so->so_head = head;
190df8bae1dSRodney W. Grimes 	so->so_type = head->so_type;
191df8bae1dSRodney W. Grimes 	so->so_options = head->so_options &~ SO_ACCEPTCONN;
192df8bae1dSRodney W. Grimes 	so->so_linger = head->so_linger;
193df8bae1dSRodney W. Grimes 	so->so_state = head->so_state | SS_NOFDREF;
194df8bae1dSRodney W. Grimes 	so->so_proto = head->so_proto;
195df8bae1dSRodney W. Grimes 	so->so_timeo = head->so_timeo;
196bd78ceceSJohn Baldwin 	so->so_cred = crhold(head->so_cred);
1972f9a2132SBrian Feldman 	if (soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat) ||
1982f9a2132SBrian Feldman 	    (*so->so_proto->pr_usrreqs->pru_attach)(so, 0, NULL)) {
199b1e4abd2SMatthew Dillon 		sotryfree(so);
200b1396a35SGarrett Wollman 		return ((struct socket *)0);
201b1396a35SGarrett Wollman 	}
202b1396a35SGarrett Wollman 
203be24e9e8SDavid Greenman 	if (connstatus) {
204be24e9e8SDavid Greenman 		TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
205be24e9e8SDavid Greenman 		so->so_state |= SS_COMP;
206e1f1827fSMike Silbersack 		head->so_qlen++;
207be24e9e8SDavid Greenman 	} else {
208184fec1aSMike Silbersack 		if (head->so_incqlen > head->so_qlimit) {
209e1f1827fSMike Silbersack 			struct socket *sp;
210e1f1827fSMike Silbersack 			sp = TAILQ_FIRST(&head->so_incomp);
211e1f1827fSMike Silbersack 			(void) soabort(sp);
212e1f1827fSMike Silbersack 		}
213be24e9e8SDavid Greenman 		TAILQ_INSERT_TAIL(&head->so_incomp, so, so_list);
214be24e9e8SDavid Greenman 		so->so_state |= SS_INCOMP;
215ebb0cbeaSPaul Traina 		head->so_incqlen++;
216be24e9e8SDavid Greenman 	}
217df8bae1dSRodney W. Grimes 	if (connstatus) {
21803e49181SSeigo Tanimura 		sorwakeup(head);
21980208239SAlfred Perlstein 		wakeup(&head->so_timeo);
220acbbcc5fSSeigo Tanimura 		so->so_state |= connstatus;
221df8bae1dSRodney W. Grimes 	}
222df8bae1dSRodney W. Grimes 	return (so);
223df8bae1dSRodney W. Grimes }
224df8bae1dSRodney W. Grimes 
225df8bae1dSRodney W. Grimes /*
226df8bae1dSRodney W. Grimes  * Socantsendmore indicates that no more data will be sent on the
227df8bae1dSRodney W. Grimes  * socket; it would normally be applied to a socket when the user
228df8bae1dSRodney W. Grimes  * informs the system that no more data is to be sent, by the protocol
229df8bae1dSRodney W. Grimes  * code (in case PRU_SHUTDOWN).  Socantrcvmore indicates that no more data
230df8bae1dSRodney W. Grimes  * will be received, and will normally be applied to the socket by a
231df8bae1dSRodney W. Grimes  * protocol when it detects that the peer will send no more data.
232df8bae1dSRodney W. Grimes  * Data queued for reading in the socket may yet be read.
233df8bae1dSRodney W. Grimes  */
234df8bae1dSRodney W. Grimes 
23526f9a767SRodney W. Grimes void
236df8bae1dSRodney W. Grimes socantsendmore(so)
237df8bae1dSRodney W. Grimes 	struct socket *so;
238df8bae1dSRodney W. Grimes {
239df8bae1dSRodney W. Grimes 
240df8bae1dSRodney W. Grimes 	so->so_state |= SS_CANTSENDMORE;
24103e49181SSeigo Tanimura 	sowwakeup(so);
242df8bae1dSRodney W. Grimes }
243df8bae1dSRodney W. Grimes 
24426f9a767SRodney W. Grimes void
245df8bae1dSRodney W. Grimes socantrcvmore(so)
246df8bae1dSRodney W. Grimes 	struct socket *so;
247df8bae1dSRodney W. Grimes {
248df8bae1dSRodney W. Grimes 
249df8bae1dSRodney W. Grimes 	so->so_state |= SS_CANTRCVMORE;
25003e49181SSeigo Tanimura 	sorwakeup(so);
251df8bae1dSRodney W. Grimes }
252df8bae1dSRodney W. Grimes 
253df8bae1dSRodney W. Grimes /*
254df8bae1dSRodney W. Grimes  * Wait for data to arrive at/drain from a socket buffer.
255df8bae1dSRodney W. Grimes  */
25626f9a767SRodney W. Grimes int
257df8bae1dSRodney W. Grimes sbwait(sb)
258df8bae1dSRodney W. Grimes 	struct sockbuf *sb;
259df8bae1dSRodney W. Grimes {
260df8bae1dSRodney W. Grimes 
261df8bae1dSRodney W. Grimes 	sb->sb_flags |= SB_WAIT;
26280208239SAlfred Perlstein 	return (tsleep(&sb->sb_cc,
26347daf5d5SBruce Evans 	    (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH, "sbwait",
264df8bae1dSRodney W. Grimes 	    sb->sb_timeo));
265df8bae1dSRodney W. Grimes }
266df8bae1dSRodney W. Grimes 
267df8bae1dSRodney W. Grimes /*
268df8bae1dSRodney W. Grimes  * Lock a sockbuf already known to be locked;
269df8bae1dSRodney W. Grimes  * return any error returned from sleep (EINTR).
270df8bae1dSRodney W. Grimes  */
27126f9a767SRodney W. Grimes int
272df8bae1dSRodney W. Grimes sb_lock(sb)
273df8bae1dSRodney W. Grimes 	register struct sockbuf *sb;
274df8bae1dSRodney W. Grimes {
275df8bae1dSRodney W. Grimes 	int error;
276df8bae1dSRodney W. Grimes 
277df8bae1dSRodney W. Grimes 	while (sb->sb_flags & SB_LOCK) {
278df8bae1dSRodney W. Grimes 		sb->sb_flags |= SB_WANT;
27980208239SAlfred Perlstein 		error = tsleep(&sb->sb_flags,
280df8bae1dSRodney W. Grimes 		    (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK|PCATCH,
28147daf5d5SBruce Evans 		    "sblock", 0);
282797f2d22SPoul-Henning Kamp 		if (error)
283df8bae1dSRodney W. Grimes 			return (error);
284df8bae1dSRodney W. Grimes 	}
285df8bae1dSRodney W. Grimes 	sb->sb_flags |= SB_LOCK;
286df8bae1dSRodney W. Grimes 	return (0);
287df8bae1dSRodney W. Grimes }
288df8bae1dSRodney W. Grimes 
289df8bae1dSRodney W. Grimes /*
290df8bae1dSRodney W. Grimes  * Wakeup processes waiting on a socket buffer.
291df8bae1dSRodney W. Grimes  * Do asynchronous notification via SIGIO
292df8bae1dSRodney W. Grimes  * if the socket has the SS_ASYNC flag set.
293df8bae1dSRodney W. Grimes  */
29426f9a767SRodney W. Grimes void
295df8bae1dSRodney W. Grimes sowakeup(so, sb)
296df8bae1dSRodney W. Grimes 	register struct socket *so;
297df8bae1dSRodney W. Grimes 	register struct sockbuf *sb;
298df8bae1dSRodney W. Grimes {
299d48d4b25SSeigo Tanimura 
300df8bae1dSRodney W. Grimes 	selwakeup(&sb->sb_sel);
301df8bae1dSRodney W. Grimes 	sb->sb_flags &= ~SB_SEL;
302df8bae1dSRodney W. Grimes 	if (sb->sb_flags & SB_WAIT) {
303df8bae1dSRodney W. Grimes 		sb->sb_flags &= ~SB_WAIT;
30480208239SAlfred Perlstein 		wakeup(&sb->sb_cc);
305df8bae1dSRodney W. Grimes 	}
3064cc20ab1SSeigo Tanimura 	if ((so->so_state & SS_ASYNC) && so->so_sigio != NULL)
307f1320723SAlfred Perlstein 		pgsigio(&so->so_sigio, SIGIO, 0);
3084cc20ab1SSeigo Tanimura 	if (sb->sb_flags & SB_UPCALL)
3094dc75870SPeter Wemm 		(*so->so_upcall)(so, so->so_upcallarg, M_DONTWAIT);
3104cc20ab1SSeigo Tanimura 	if (sb->sb_flags & SB_AIO)
311bfbbc4aaSJason Evans 		aio_swake(so, sb);
312cb679c38SJonathan Lemon 	KNOTE(&sb->sb_sel.si_note, 0);
313df8bae1dSRodney W. Grimes }
314df8bae1dSRodney W. Grimes 
315df8bae1dSRodney W. Grimes /*
316df8bae1dSRodney W. Grimes  * Socket buffer (struct sockbuf) utility routines.
317df8bae1dSRodney W. Grimes  *
318df8bae1dSRodney W. Grimes  * Each socket contains two socket buffers: one for sending data and
319df8bae1dSRodney W. Grimes  * one for receiving data.  Each buffer contains a queue of mbufs,
320df8bae1dSRodney W. Grimes  * information about the number of mbufs and amount of data in the
321df8bae1dSRodney W. Grimes  * queue, and other fields allowing select() statements and notification
322df8bae1dSRodney W. Grimes  * on data availability to be implemented.
323df8bae1dSRodney W. Grimes  *
324df8bae1dSRodney W. Grimes  * Data stored in a socket buffer is maintained as a list of records.
325df8bae1dSRodney W. Grimes  * Each record is a list of mbufs chained together with the m_next
326df8bae1dSRodney W. Grimes  * field.  Records are chained together with the m_nextpkt field. The upper
327df8bae1dSRodney W. Grimes  * level routine soreceive() expects the following conventions to be
328df8bae1dSRodney W. Grimes  * observed when placing information in the receive buffer:
329df8bae1dSRodney W. Grimes  *
330df8bae1dSRodney W. Grimes  * 1. If the protocol requires each message be preceded by the sender's
331df8bae1dSRodney W. Grimes  *    name, then a record containing that name must be present before
332df8bae1dSRodney W. Grimes  *    any associated data (mbuf's must be of type MT_SONAME).
333df8bae1dSRodney W. Grimes  * 2. If the protocol supports the exchange of ``access rights'' (really
334df8bae1dSRodney W. Grimes  *    just additional data associated with the message), and there are
335df8bae1dSRodney W. Grimes  *    ``rights'' to be received, then a record containing this data
336df8bae1dSRodney W. Grimes  *    should be present (mbuf's must be of type MT_RIGHTS).
337df8bae1dSRodney W. Grimes  * 3. If a name or rights record exists, then it must be followed by
338df8bae1dSRodney W. Grimes  *    a data record, perhaps of zero length.
339df8bae1dSRodney W. Grimes  *
340df8bae1dSRodney W. Grimes  * Before using a new socket structure it is first necessary to reserve
341df8bae1dSRodney W. Grimes  * buffer space to the socket, by calling sbreserve().  This should commit
342df8bae1dSRodney W. Grimes  * some of the available buffer space in the system buffer pool for the
343df8bae1dSRodney W. Grimes  * socket (currently, it does nothing but enforce limits).  The space
344df8bae1dSRodney W. Grimes  * should be released by calling sbrelease() when the socket is destroyed.
345df8bae1dSRodney W. Grimes  */
346df8bae1dSRodney W. Grimes 
34726f9a767SRodney W. Grimes int
348df8bae1dSRodney W. Grimes soreserve(so, sndcc, rcvcc)
349df8bae1dSRodney W. Grimes 	register struct socket *so;
350df8bae1dSRodney W. Grimes 	u_long sndcc, rcvcc;
351df8bae1dSRodney W. Grimes {
352b40ce416SJulian Elischer 	struct thread *td = curthread;
353df8bae1dSRodney W. Grimes 
354b40ce416SJulian Elischer 	if (sbreserve(&so->so_snd, sndcc, so, td) == 0)
355df8bae1dSRodney W. Grimes 		goto bad;
356b40ce416SJulian Elischer 	if (sbreserve(&so->so_rcv, rcvcc, so, td) == 0)
357df8bae1dSRodney W. Grimes 		goto bad2;
358df8bae1dSRodney W. Grimes 	if (so->so_rcv.sb_lowat == 0)
359df8bae1dSRodney W. Grimes 		so->so_rcv.sb_lowat = 1;
360df8bae1dSRodney W. Grimes 	if (so->so_snd.sb_lowat == 0)
361df8bae1dSRodney W. Grimes 		so->so_snd.sb_lowat = MCLBYTES;
362df8bae1dSRodney W. Grimes 	if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat)
363df8bae1dSRodney W. Grimes 		so->so_snd.sb_lowat = so->so_snd.sb_hiwat;
364df8bae1dSRodney W. Grimes 	return (0);
365df8bae1dSRodney W. Grimes bad2:
366ecf72308SBrian Feldman 	sbrelease(&so->so_snd, so);
367df8bae1dSRodney W. Grimes bad:
368df8bae1dSRodney W. Grimes 	return (ENOBUFS);
369df8bae1dSRodney W. Grimes }
370df8bae1dSRodney W. Grimes 
371df8bae1dSRodney W. Grimes /*
372df8bae1dSRodney W. Grimes  * Allot mbufs to a sockbuf.
373df8bae1dSRodney W. Grimes  * Attempt to scale mbmax so that mbcnt doesn't become limiting
374df8bae1dSRodney W. Grimes  * if buffering efficiency is near the normal case.
375df8bae1dSRodney W. Grimes  */
37626f9a767SRodney W. Grimes int
377b40ce416SJulian Elischer sbreserve(sb, cc, so, td)
378df8bae1dSRodney W. Grimes 	struct sockbuf *sb;
379df8bae1dSRodney W. Grimes 	u_long cc;
380ecf72308SBrian Feldman 	struct socket *so;
381b40ce416SJulian Elischer 	struct thread *td;
382df8bae1dSRodney W. Grimes {
383ecf72308SBrian Feldman 
384ecf72308SBrian Feldman 	/*
385b40ce416SJulian Elischer 	 * td will only be NULL when we're in an interrupt
386ecf72308SBrian Feldman 	 * (e.g. in tcp_input())
387ecf72308SBrian Feldman 	 */
3889f5193caSMike Silbersack 	if ((u_quad_t)cc > (u_quad_t)sb_max * MCLBYTES / (MSIZE + MCLBYTES))
389df8bae1dSRodney W. Grimes 		return (0);
390f535380cSDon Lewis 	if (!chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, cc,
391b40ce416SJulian Elischer 	    td ? td->td_proc->p_rlimit[RLIMIT_SBSIZE].rlim_cur : RLIM_INFINITY)) {
392ecf72308SBrian Feldman 		return (0);
393c6362551SAlfred Perlstein 	}
3944b29bc4fSGarrett Wollman 	sb->sb_mbmax = min(cc * sb_efficiency, sb_max);
395df8bae1dSRodney W. Grimes 	if (sb->sb_lowat > sb->sb_hiwat)
396df8bae1dSRodney W. Grimes 		sb->sb_lowat = sb->sb_hiwat;
397df8bae1dSRodney W. Grimes 	return (1);
398df8bae1dSRodney W. Grimes }
399df8bae1dSRodney W. Grimes 
400df8bae1dSRodney W. Grimes /*
401df8bae1dSRodney W. Grimes  * Free mbufs held by a socket, and reserved mbuf space.
402df8bae1dSRodney W. Grimes  */
40326f9a767SRodney W. Grimes void
404ecf72308SBrian Feldman sbrelease(sb, so)
405df8bae1dSRodney W. Grimes 	struct sockbuf *sb;
406ecf72308SBrian Feldman 	struct socket *so;
407df8bae1dSRodney W. Grimes {
408df8bae1dSRodney W. Grimes 
409df8bae1dSRodney W. Grimes 	sbflush(sb);
410f535380cSDon Lewis 	(void)chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, 0,
411f535380cSDon Lewis 	    RLIM_INFINITY);
4126aef685fSBrian Feldman 	sb->sb_mbmax = 0;
413df8bae1dSRodney W. Grimes }
414df8bae1dSRodney W. Grimes 
415df8bae1dSRodney W. Grimes /*
416df8bae1dSRodney W. Grimes  * Routines to add and remove
417df8bae1dSRodney W. Grimes  * data from an mbuf queue.
418df8bae1dSRodney W. Grimes  *
419df8bae1dSRodney W. Grimes  * The routines sbappend() or sbappendrecord() are normally called to
420df8bae1dSRodney W. Grimes  * append new mbufs to a socket buffer, after checking that adequate
421df8bae1dSRodney W. Grimes  * space is available, comparing the function sbspace() with the amount
422df8bae1dSRodney W. Grimes  * of data to be added.  sbappendrecord() differs from sbappend() in
423df8bae1dSRodney W. Grimes  * that data supplied is treated as the beginning of a new record.
424df8bae1dSRodney W. Grimes  * To place a sender's address, optional access rights, and data in a
425df8bae1dSRodney W. Grimes  * socket receive buffer, sbappendaddr() should be used.  To place
426df8bae1dSRodney W. Grimes  * access rights and data in a socket receive buffer, sbappendrights()
427df8bae1dSRodney W. Grimes  * should be used.  In either case, the new data begins a new record.
428df8bae1dSRodney W. Grimes  * Note that unlike sbappend() and sbappendrecord(), these routines check
429df8bae1dSRodney W. Grimes  * for the caller that there will be enough space to store the data.
430df8bae1dSRodney W. Grimes  * Each fails if there is not enough space, or if it cannot find mbufs
431df8bae1dSRodney W. Grimes  * to store additional information in.
432df8bae1dSRodney W. Grimes  *
433df8bae1dSRodney W. Grimes  * Reliable protocols may use the socket send buffer to hold data
434df8bae1dSRodney W. Grimes  * awaiting acknowledgement.  Data is normally copied from a socket
435df8bae1dSRodney W. Grimes  * send buffer in a protocol with m_copy for output to a peer,
436df8bae1dSRodney W. Grimes  * and then removing the data from the socket buffer with sbdrop()
437df8bae1dSRodney W. Grimes  * or sbdroprecord() when the data is acknowledged by the peer.
438df8bae1dSRodney W. Grimes  */
439df8bae1dSRodney W. Grimes 
440df8bae1dSRodney W. Grimes /*
441df8bae1dSRodney W. Grimes  * Append mbuf chain m to the last record in the
442df8bae1dSRodney W. Grimes  * socket buffer sb.  The additional space associated
443df8bae1dSRodney W. Grimes  * the mbuf chain is recorded in sb.  Empty mbufs are
444df8bae1dSRodney W. Grimes  * discarded and mbufs are compacted where possible.
445df8bae1dSRodney W. Grimes  */
44626f9a767SRodney W. Grimes void
447df8bae1dSRodney W. Grimes sbappend(sb, m)
448df8bae1dSRodney W. Grimes 	struct sockbuf *sb;
449df8bae1dSRodney W. Grimes 	struct mbuf *m;
450df8bae1dSRodney W. Grimes {
451df8bae1dSRodney W. Grimes 	register struct mbuf *n;
452df8bae1dSRodney W. Grimes 
453df8bae1dSRodney W. Grimes 	if (m == 0)
454df8bae1dSRodney W. Grimes 		return;
455797f2d22SPoul-Henning Kamp 	n = sb->sb_mb;
456797f2d22SPoul-Henning Kamp 	if (n) {
457df8bae1dSRodney W. Grimes 		while (n->m_nextpkt)
458df8bae1dSRodney W. Grimes 			n = n->m_nextpkt;
459df8bae1dSRodney W. Grimes 		do {
460df8bae1dSRodney W. Grimes 			if (n->m_flags & M_EOR) {
461df8bae1dSRodney W. Grimes 				sbappendrecord(sb, m); /* XXXXXX!!!! */
462df8bae1dSRodney W. Grimes 				return;
463df8bae1dSRodney W. Grimes 			}
464df8bae1dSRodney W. Grimes 		} while (n->m_next && (n = n->m_next));
465df8bae1dSRodney W. Grimes 	}
466df8bae1dSRodney W. Grimes 	sbcompress(sb, m, n);
467df8bae1dSRodney W. Grimes }
468df8bae1dSRodney W. Grimes 
469df8bae1dSRodney W. Grimes #ifdef SOCKBUF_DEBUG
47026f9a767SRodney W. Grimes void
471df8bae1dSRodney W. Grimes sbcheck(sb)
472df8bae1dSRodney W. Grimes 	register struct sockbuf *sb;
473df8bae1dSRodney W. Grimes {
474df8bae1dSRodney W. Grimes 	register struct mbuf *m;
4750931333fSBill Fenner 	register struct mbuf *n = 0;
4760931333fSBill Fenner 	register u_long len = 0, mbcnt = 0;
477df8bae1dSRodney W. Grimes 
4780931333fSBill Fenner 	for (m = sb->sb_mb; m; m = n) {
4790931333fSBill Fenner 	    n = m->m_nextpkt;
4800931333fSBill Fenner 	    for (; m; m = m->m_next) {
481df8bae1dSRodney W. Grimes 		len += m->m_len;
482df8bae1dSRodney W. Grimes 		mbcnt += MSIZE;
483313861b8SJulian Elischer 		if (m->m_flags & M_EXT) /*XXX*/ /* pretty sure this is bogus */
484df8bae1dSRodney W. Grimes 			mbcnt += m->m_ext.ext_size;
4850931333fSBill Fenner 	    }
486df8bae1dSRodney W. Grimes 	}
487df8bae1dSRodney W. Grimes 	if (len != sb->sb_cc || mbcnt != sb->sb_mbcnt) {
4880931333fSBill Fenner 		printf("cc %ld != %ld || mbcnt %ld != %ld\n", len, sb->sb_cc,
489df8bae1dSRodney W. Grimes 		    mbcnt, sb->sb_mbcnt);
490df8bae1dSRodney W. Grimes 		panic("sbcheck");
491df8bae1dSRodney W. Grimes 	}
492df8bae1dSRodney W. Grimes }
493df8bae1dSRodney W. Grimes #endif
494df8bae1dSRodney W. Grimes 
495df8bae1dSRodney W. Grimes /*
496df8bae1dSRodney W. Grimes  * As above, except the mbuf chain
497df8bae1dSRodney W. Grimes  * begins a new record.
498df8bae1dSRodney W. Grimes  */
49926f9a767SRodney W. Grimes void
500df8bae1dSRodney W. Grimes sbappendrecord(sb, m0)
501df8bae1dSRodney W. Grimes 	register struct sockbuf *sb;
502df8bae1dSRodney W. Grimes 	register struct mbuf *m0;
503df8bae1dSRodney W. Grimes {
504df8bae1dSRodney W. Grimes 	register struct mbuf *m;
505df8bae1dSRodney W. Grimes 
506df8bae1dSRodney W. Grimes 	if (m0 == 0)
507df8bae1dSRodney W. Grimes 		return;
508797f2d22SPoul-Henning Kamp 	m = sb->sb_mb;
509797f2d22SPoul-Henning Kamp 	if (m)
510df8bae1dSRodney W. Grimes 		while (m->m_nextpkt)
511df8bae1dSRodney W. Grimes 			m = m->m_nextpkt;
512df8bae1dSRodney W. Grimes 	/*
513df8bae1dSRodney W. Grimes 	 * Put the first mbuf on the queue.
514df8bae1dSRodney W. Grimes 	 * Note this permits zero length records.
515df8bae1dSRodney W. Grimes 	 */
516df8bae1dSRodney W. Grimes 	sballoc(sb, m0);
517df8bae1dSRodney W. Grimes 	if (m)
518df8bae1dSRodney W. Grimes 		m->m_nextpkt = m0;
519df8bae1dSRodney W. Grimes 	else
520df8bae1dSRodney W. Grimes 		sb->sb_mb = m0;
521df8bae1dSRodney W. Grimes 	m = m0->m_next;
522df8bae1dSRodney W. Grimes 	m0->m_next = 0;
523df8bae1dSRodney W. Grimes 	if (m && (m0->m_flags & M_EOR)) {
524df8bae1dSRodney W. Grimes 		m0->m_flags &= ~M_EOR;
525df8bae1dSRodney W. Grimes 		m->m_flags |= M_EOR;
526df8bae1dSRodney W. Grimes 	}
527df8bae1dSRodney W. Grimes 	sbcompress(sb, m, m0);
528df8bae1dSRodney W. Grimes }
529df8bae1dSRodney W. Grimes 
530df8bae1dSRodney W. Grimes /*
531df8bae1dSRodney W. Grimes  * As above except that OOB data
532df8bae1dSRodney W. Grimes  * is inserted at the beginning of the sockbuf,
533df8bae1dSRodney W. Grimes  * but after any other OOB data.
534df8bae1dSRodney W. Grimes  */
53526f9a767SRodney W. Grimes void
536df8bae1dSRodney W. Grimes sbinsertoob(sb, m0)
537df8bae1dSRodney W. Grimes 	register struct sockbuf *sb;
538df8bae1dSRodney W. Grimes 	register struct mbuf *m0;
539df8bae1dSRodney W. Grimes {
540df8bae1dSRodney W. Grimes 	register struct mbuf *m;
541df8bae1dSRodney W. Grimes 	register struct mbuf **mp;
542df8bae1dSRodney W. Grimes 
543df8bae1dSRodney W. Grimes 	if (m0 == 0)
544df8bae1dSRodney W. Grimes 		return;
545797f2d22SPoul-Henning Kamp 	for (mp = &sb->sb_mb; *mp ; mp = &((*mp)->m_nextpkt)) {
546797f2d22SPoul-Henning Kamp 	    m = *mp;
547df8bae1dSRodney W. Grimes 	    again:
548df8bae1dSRodney W. Grimes 		switch (m->m_type) {
549df8bae1dSRodney W. Grimes 
550df8bae1dSRodney W. Grimes 		case MT_OOBDATA:
551df8bae1dSRodney W. Grimes 			continue;		/* WANT next train */
552df8bae1dSRodney W. Grimes 
553df8bae1dSRodney W. Grimes 		case MT_CONTROL:
554797f2d22SPoul-Henning Kamp 			m = m->m_next;
555797f2d22SPoul-Henning Kamp 			if (m)
556df8bae1dSRodney W. Grimes 				goto again;	/* inspect THIS train further */
557df8bae1dSRodney W. Grimes 		}
558df8bae1dSRodney W. Grimes 		break;
559df8bae1dSRodney W. Grimes 	}
560df8bae1dSRodney W. Grimes 	/*
561df8bae1dSRodney W. Grimes 	 * Put the first mbuf on the queue.
562df8bae1dSRodney W. Grimes 	 * Note this permits zero length records.
563df8bae1dSRodney W. Grimes 	 */
564df8bae1dSRodney W. Grimes 	sballoc(sb, m0);
565df8bae1dSRodney W. Grimes 	m0->m_nextpkt = *mp;
566df8bae1dSRodney W. Grimes 	*mp = m0;
567df8bae1dSRodney W. Grimes 	m = m0->m_next;
568df8bae1dSRodney W. Grimes 	m0->m_next = 0;
569df8bae1dSRodney W. Grimes 	if (m && (m0->m_flags & M_EOR)) {
570df8bae1dSRodney W. Grimes 		m0->m_flags &= ~M_EOR;
571df8bae1dSRodney W. Grimes 		m->m_flags |= M_EOR;
572df8bae1dSRodney W. Grimes 	}
573df8bae1dSRodney W. Grimes 	sbcompress(sb, m, m0);
574df8bae1dSRodney W. Grimes }
575df8bae1dSRodney W. Grimes 
576df8bae1dSRodney W. Grimes /*
577df8bae1dSRodney W. Grimes  * Append address and data, and optionally, control (ancillary) data
578df8bae1dSRodney W. Grimes  * to the receive queue of a socket.  If present,
579df8bae1dSRodney W. Grimes  * m0 must include a packet header with total length.
580df8bae1dSRodney W. Grimes  * Returns 0 if no space in sockbuf or insufficient mbufs.
581df8bae1dSRodney W. Grimes  */
58226f9a767SRodney W. Grimes int
583df8bae1dSRodney W. Grimes sbappendaddr(sb, asa, m0, control)
584df8bae1dSRodney W. Grimes 	register struct sockbuf *sb;
585df8bae1dSRodney W. Grimes 	struct sockaddr *asa;
586df8bae1dSRodney W. Grimes 	struct mbuf *m0, *control;
587df8bae1dSRodney W. Grimes {
588df8bae1dSRodney W. Grimes 	register struct mbuf *m, *n;
589df8bae1dSRodney W. Grimes 	int space = asa->sa_len;
590df8bae1dSRodney W. Grimes 
591df8bae1dSRodney W. Grimes 	if (m0 && (m0->m_flags & M_PKTHDR) == 0)
592df8bae1dSRodney W. Grimes 		panic("sbappendaddr");
593df8bae1dSRodney W. Grimes 	if (m0)
594df8bae1dSRodney W. Grimes 		space += m0->m_pkthdr.len;
595df8bae1dSRodney W. Grimes 	for (n = control; n; n = n->m_next) {
596df8bae1dSRodney W. Grimes 		space += n->m_len;
597df8bae1dSRodney W. Grimes 		if (n->m_next == 0)	/* keep pointer to last control buf */
598df8bae1dSRodney W. Grimes 			break;
599df8bae1dSRodney W. Grimes 	}
600df8bae1dSRodney W. Grimes 	if (space > sbspace(sb))
601df8bae1dSRodney W. Grimes 		return (0);
602df8bae1dSRodney W. Grimes 	if (asa->sa_len > MLEN)
603df8bae1dSRodney W. Grimes 		return (0);
604df8bae1dSRodney W. Grimes 	MGET(m, M_DONTWAIT, MT_SONAME);
605df8bae1dSRodney W. Grimes 	if (m == 0)
606df8bae1dSRodney W. Grimes 		return (0);
607df8bae1dSRodney W. Grimes 	m->m_len = asa->sa_len;
60880208239SAlfred Perlstein 	bcopy(asa, mtod(m, caddr_t), asa->sa_len);
609df8bae1dSRodney W. Grimes 	if (n)
610df8bae1dSRodney W. Grimes 		n->m_next = m0;		/* concatenate data to control */
611df8bae1dSRodney W. Grimes 	else
612df8bae1dSRodney W. Grimes 		control = m0;
613df8bae1dSRodney W. Grimes 	m->m_next = control;
614df8bae1dSRodney W. Grimes 	for (n = m; n; n = n->m_next)
615df8bae1dSRodney W. Grimes 		sballoc(sb, n);
616797f2d22SPoul-Henning Kamp 	n = sb->sb_mb;
617797f2d22SPoul-Henning Kamp 	if (n) {
618df8bae1dSRodney W. Grimes 		while (n->m_nextpkt)
619df8bae1dSRodney W. Grimes 			n = n->m_nextpkt;
620df8bae1dSRodney W. Grimes 		n->m_nextpkt = m;
621df8bae1dSRodney W. Grimes 	} else
622df8bae1dSRodney W. Grimes 		sb->sb_mb = m;
623df8bae1dSRodney W. Grimes 	return (1);
624df8bae1dSRodney W. Grimes }
625df8bae1dSRodney W. Grimes 
62626f9a767SRodney W. Grimes int
627df8bae1dSRodney W. Grimes sbappendcontrol(sb, m0, control)
628df8bae1dSRodney W. Grimes 	struct sockbuf *sb;
629df8bae1dSRodney W. Grimes 	struct mbuf *control, *m0;
630df8bae1dSRodney W. Grimes {
631df8bae1dSRodney W. Grimes 	register struct mbuf *m, *n;
632df8bae1dSRodney W. Grimes 	int space = 0;
633df8bae1dSRodney W. Grimes 
634df8bae1dSRodney W. Grimes 	if (control == 0)
635df8bae1dSRodney W. Grimes 		panic("sbappendcontrol");
636df8bae1dSRodney W. Grimes 	for (m = control; ; m = m->m_next) {
637df8bae1dSRodney W. Grimes 		space += m->m_len;
638df8bae1dSRodney W. Grimes 		if (m->m_next == 0)
639df8bae1dSRodney W. Grimes 			break;
640df8bae1dSRodney W. Grimes 	}
641df8bae1dSRodney W. Grimes 	n = m;			/* save pointer to last control buffer */
642df8bae1dSRodney W. Grimes 	for (m = m0; m; m = m->m_next)
643df8bae1dSRodney W. Grimes 		space += m->m_len;
644df8bae1dSRodney W. Grimes 	if (space > sbspace(sb))
645df8bae1dSRodney W. Grimes 		return (0);
646df8bae1dSRodney W. Grimes 	n->m_next = m0;			/* concatenate data to control */
647df8bae1dSRodney W. Grimes 	for (m = control; m; m = m->m_next)
648df8bae1dSRodney W. Grimes 		sballoc(sb, m);
649797f2d22SPoul-Henning Kamp 	n = sb->sb_mb;
650797f2d22SPoul-Henning Kamp 	if (n) {
651df8bae1dSRodney W. Grimes 		while (n->m_nextpkt)
652df8bae1dSRodney W. Grimes 			n = n->m_nextpkt;
653df8bae1dSRodney W. Grimes 		n->m_nextpkt = control;
654df8bae1dSRodney W. Grimes 	} else
655df8bae1dSRodney W. Grimes 		sb->sb_mb = control;
656df8bae1dSRodney W. Grimes 	return (1);
657df8bae1dSRodney W. Grimes }
658df8bae1dSRodney W. Grimes 
659df8bae1dSRodney W. Grimes /*
660df8bae1dSRodney W. Grimes  * Compress mbuf chain m into the socket
661df8bae1dSRodney W. Grimes  * buffer sb following mbuf n.  If n
662df8bae1dSRodney W. Grimes  * is null, the buffer is presumed empty.
663df8bae1dSRodney W. Grimes  */
66426f9a767SRodney W. Grimes void
665df8bae1dSRodney W. Grimes sbcompress(sb, m, n)
666df8bae1dSRodney W. Grimes 	register struct sockbuf *sb;
667df8bae1dSRodney W. Grimes 	register struct mbuf *m, *n;
668df8bae1dSRodney W. Grimes {
669df8bae1dSRodney W. Grimes 	register int eor = 0;
670df8bae1dSRodney W. Grimes 	register struct mbuf *o;
671df8bae1dSRodney W. Grimes 
672df8bae1dSRodney W. Grimes 	while (m) {
673df8bae1dSRodney W. Grimes 		eor |= m->m_flags & M_EOR;
674df8bae1dSRodney W. Grimes 		if (m->m_len == 0 &&
675df8bae1dSRodney W. Grimes 		    (eor == 0 ||
676df8bae1dSRodney W. Grimes 		     (((o = m->m_next) || (o = n)) &&
677df8bae1dSRodney W. Grimes 		      o->m_type == m->m_type))) {
678df8bae1dSRodney W. Grimes 			m = m_free(m);
679df8bae1dSRodney W. Grimes 			continue;
680df8bae1dSRodney W. Grimes 		}
68132af0d74SDavid Malone 		if (n && (n->m_flags & M_EOR) == 0 &&
68232af0d74SDavid Malone 		    M_WRITABLE(n) &&
68332af0d74SDavid Malone 		    m->m_len <= MCLBYTES / 4 && /* XXX: Don't copy too much */
68432af0d74SDavid Malone 		    m->m_len <= M_TRAILINGSPACE(n) &&
685df8bae1dSRodney W. Grimes 		    n->m_type == m->m_type) {
686df8bae1dSRodney W. Grimes 			bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len,
687df8bae1dSRodney W. Grimes 			    (unsigned)m->m_len);
688df8bae1dSRodney W. Grimes 			n->m_len += m->m_len;
689df8bae1dSRodney W. Grimes 			sb->sb_cc += m->m_len;
690df8bae1dSRodney W. Grimes 			m = m_free(m);
691df8bae1dSRodney W. Grimes 			continue;
692df8bae1dSRodney W. Grimes 		}
693df8bae1dSRodney W. Grimes 		if (n)
694df8bae1dSRodney W. Grimes 			n->m_next = m;
695df8bae1dSRodney W. Grimes 		else
696df8bae1dSRodney W. Grimes 			sb->sb_mb = m;
697df8bae1dSRodney W. Grimes 		sballoc(sb, m);
698df8bae1dSRodney W. Grimes 		n = m;
699df8bae1dSRodney W. Grimes 		m->m_flags &= ~M_EOR;
700df8bae1dSRodney W. Grimes 		m = m->m_next;
701df8bae1dSRodney W. Grimes 		n->m_next = 0;
702df8bae1dSRodney W. Grimes 	}
703df8bae1dSRodney W. Grimes 	if (eor) {
704df8bae1dSRodney W. Grimes 		if (n)
705df8bae1dSRodney W. Grimes 			n->m_flags |= eor;
706df8bae1dSRodney W. Grimes 		else
707df8bae1dSRodney W. Grimes 			printf("semi-panic: sbcompress\n");
708df8bae1dSRodney W. Grimes 	}
709df8bae1dSRodney W. Grimes }
710df8bae1dSRodney W. Grimes 
711df8bae1dSRodney W. Grimes /*
712df8bae1dSRodney W. Grimes  * Free all mbufs in a sockbuf.
713df8bae1dSRodney W. Grimes  * Check that all resources are reclaimed.
714df8bae1dSRodney W. Grimes  */
71526f9a767SRodney W. Grimes void
716df8bae1dSRodney W. Grimes sbflush(sb)
717df8bae1dSRodney W. Grimes 	register struct sockbuf *sb;
718df8bae1dSRodney W. Grimes {
719df8bae1dSRodney W. Grimes 
720df8bae1dSRodney W. Grimes 	if (sb->sb_flags & SB_LOCK)
721253ab668SAndrey A. Chernov 		panic("sbflush: locked");
72223f84772SPierre Beyssac 	while (sb->sb_mbcnt) {
72323f84772SPierre Beyssac 		/*
72423f84772SPierre Beyssac 		 * Don't call sbdrop(sb, 0) if the leading mbuf is non-empty:
72523f84772SPierre Beyssac 		 * we would loop forever. Panic instead.
72623f84772SPierre Beyssac 		 */
72723f84772SPierre Beyssac 		if (!sb->sb_cc && (sb->sb_mb == NULL || sb->sb_mb->m_len))
72823f84772SPierre Beyssac 			break;
729df8bae1dSRodney W. Grimes 		sbdrop(sb, (int)sb->sb_cc);
73023f84772SPierre Beyssac 	}
7310931333fSBill Fenner 	if (sb->sb_cc || sb->sb_mb || sb->sb_mbcnt)
7320931333fSBill Fenner 		panic("sbflush: cc %ld || mb %p || mbcnt %ld", sb->sb_cc, (void *)sb->sb_mb, sb->sb_mbcnt);
733df8bae1dSRodney W. Grimes }
734df8bae1dSRodney W. Grimes 
735df8bae1dSRodney W. Grimes /*
736df8bae1dSRodney W. Grimes  * Drop data from (the front of) a sockbuf.
737df8bae1dSRodney W. Grimes  */
73826f9a767SRodney W. Grimes void
739df8bae1dSRodney W. Grimes sbdrop(sb, len)
740df8bae1dSRodney W. Grimes 	register struct sockbuf *sb;
741df8bae1dSRodney W. Grimes 	register int len;
742df8bae1dSRodney W. Grimes {
743ecde8f7cSMatthew Dillon 	register struct mbuf *m;
744df8bae1dSRodney W. Grimes 	struct mbuf *next;
745df8bae1dSRodney W. Grimes 
746df8bae1dSRodney W. Grimes 	next = (m = sb->sb_mb) ? m->m_nextpkt : 0;
747df8bae1dSRodney W. Grimes 	while (len > 0) {
748df8bae1dSRodney W. Grimes 		if (m == 0) {
749df8bae1dSRodney W. Grimes 			if (next == 0)
750df8bae1dSRodney W. Grimes 				panic("sbdrop");
751df8bae1dSRodney W. Grimes 			m = next;
752df8bae1dSRodney W. Grimes 			next = m->m_nextpkt;
753df8bae1dSRodney W. Grimes 			continue;
754df8bae1dSRodney W. Grimes 		}
755df8bae1dSRodney W. Grimes 		if (m->m_len > len) {
756df8bae1dSRodney W. Grimes 			m->m_len -= len;
757df8bae1dSRodney W. Grimes 			m->m_data += len;
758df8bae1dSRodney W. Grimes 			sb->sb_cc -= len;
759df8bae1dSRodney W. Grimes 			break;
760df8bae1dSRodney W. Grimes 		}
761df8bae1dSRodney W. Grimes 		len -= m->m_len;
762df8bae1dSRodney W. Grimes 		sbfree(sb, m);
763ecde8f7cSMatthew Dillon 		m = m_free(m);
764df8bae1dSRodney W. Grimes 	}
765df8bae1dSRodney W. Grimes 	while (m && m->m_len == 0) {
766df8bae1dSRodney W. Grimes 		sbfree(sb, m);
767ecde8f7cSMatthew Dillon 		m = m_free(m);
768df8bae1dSRodney W. Grimes 	}
769df8bae1dSRodney W. Grimes 	if (m) {
770df8bae1dSRodney W. Grimes 		sb->sb_mb = m;
771df8bae1dSRodney W. Grimes 		m->m_nextpkt = next;
772df8bae1dSRodney W. Grimes 	} else
773df8bae1dSRodney W. Grimes 		sb->sb_mb = next;
774df8bae1dSRodney W. Grimes }
775df8bae1dSRodney W. Grimes 
776df8bae1dSRodney W. Grimes /*
777df8bae1dSRodney W. Grimes  * Drop a record off the front of a sockbuf
778df8bae1dSRodney W. Grimes  * and move the next record to the front.
779df8bae1dSRodney W. Grimes  */
78026f9a767SRodney W. Grimes void
781df8bae1dSRodney W. Grimes sbdroprecord(sb)
782df8bae1dSRodney W. Grimes 	register struct sockbuf *sb;
783df8bae1dSRodney W. Grimes {
784ecde8f7cSMatthew Dillon 	register struct mbuf *m;
785df8bae1dSRodney W. Grimes 
786df8bae1dSRodney W. Grimes 	m = sb->sb_mb;
787df8bae1dSRodney W. Grimes 	if (m) {
788df8bae1dSRodney W. Grimes 		sb->sb_mb = m->m_nextpkt;
789df8bae1dSRodney W. Grimes 		do {
790df8bae1dSRodney W. Grimes 			sbfree(sb, m);
791ecde8f7cSMatthew Dillon 			m = m_free(m);
792797f2d22SPoul-Henning Kamp 		} while (m);
793df8bae1dSRodney W. Grimes 	}
794df8bae1dSRodney W. Grimes }
7951e4ad9ceSGarrett Wollman 
79682c23ebaSBill Fenner /*
79782c23ebaSBill Fenner  * Create a "control" mbuf containing the specified data
79882c23ebaSBill Fenner  * with the specified type for presentation on a socket buffer.
79982c23ebaSBill Fenner  */
80082c23ebaSBill Fenner struct mbuf *
80182c23ebaSBill Fenner sbcreatecontrol(p, size, type, level)
80282c23ebaSBill Fenner 	caddr_t p;
80382c23ebaSBill Fenner 	register int size;
80482c23ebaSBill Fenner 	int type, level;
80582c23ebaSBill Fenner {
80682c23ebaSBill Fenner 	register struct cmsghdr *cp;
80782c23ebaSBill Fenner 	struct mbuf *m;
80882c23ebaSBill Fenner 
80959bdd405SDavid Malone 	if (CMSG_SPACE((u_int)size) > MCLBYTES)
8100b97e97cSYoshinobu Inoue 		return ((struct mbuf *) NULL);
81182c23ebaSBill Fenner 	if ((m = m_get(M_DONTWAIT, MT_CONTROL)) == NULL)
81282c23ebaSBill Fenner 		return ((struct mbuf *) NULL);
81359bdd405SDavid Malone 	if (CMSG_SPACE((u_int)size) > MLEN) {
81459bdd405SDavid Malone 		MCLGET(m, M_DONTWAIT);
81559bdd405SDavid Malone 		if ((m->m_flags & M_EXT) == 0) {
81659bdd405SDavid Malone 			m_free(m);
81759bdd405SDavid Malone 			return ((struct mbuf *) NULL);
81859bdd405SDavid Malone 		}
81959bdd405SDavid Malone 	}
82082c23ebaSBill Fenner 	cp = mtod(m, struct cmsghdr *);
82159bdd405SDavid Malone 	m->m_len = 0;
82259bdd405SDavid Malone 	KASSERT(CMSG_SPACE((u_int)size) <= M_TRAILINGSPACE(m),
82359bdd405SDavid Malone 	    ("sbcreatecontrol: short mbuf"));
82459bdd405SDavid Malone 	if (p != NULL)
82582c23ebaSBill Fenner 		(void)memcpy(CMSG_DATA(cp), p, size);
8267d0d8dc3SYoshinobu Inoue 	m->m_len = CMSG_SPACE(size);
8277d0d8dc3SYoshinobu Inoue 	cp->cmsg_len = CMSG_LEN(size);
82882c23ebaSBill Fenner 	cp->cmsg_level = level;
82982c23ebaSBill Fenner 	cp->cmsg_type = type;
83082c23ebaSBill Fenner 	return (m);
83182c23ebaSBill Fenner }
83282c23ebaSBill Fenner 
8331e4ad9ceSGarrett Wollman /*
8342c37256eSGarrett Wollman  * Some routines that return EOPNOTSUPP for entry points that are not
8352c37256eSGarrett Wollman  * supported by a protocol.  Fill in as needed.
8361e4ad9ceSGarrett Wollman  */
8371e4ad9ceSGarrett Wollman int
83857bf258eSGarrett Wollman pru_accept_notsupp(struct socket *so, struct sockaddr **nam)
839d8392c6cSGarrett Wollman {
840d8392c6cSGarrett Wollman 	return EOPNOTSUPP;
841d8392c6cSGarrett Wollman }
842d8392c6cSGarrett Wollman 
843d8392c6cSGarrett Wollman int
844b40ce416SJulian Elischer pru_connect_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td)
8459f907986SPeter Wemm {
8469f907986SPeter Wemm 	return EOPNOTSUPP;
8479f907986SPeter Wemm }
8489f907986SPeter Wemm 
8499f907986SPeter Wemm int
8502c37256eSGarrett Wollman pru_connect2_notsupp(struct socket *so1, struct socket *so2)
8511e4ad9ceSGarrett Wollman {
8522c37256eSGarrett Wollman 	return EOPNOTSUPP;
8531e4ad9ceSGarrett Wollman }
854d8392c6cSGarrett Wollman 
855d8392c6cSGarrett Wollman int
856ecbb00a2SDoug Rabson pru_control_notsupp(struct socket *so, u_long cmd, caddr_t data,
857b40ce416SJulian Elischer 		    struct ifnet *ifp, struct thread *td)
858a29f300eSGarrett Wollman {
859a29f300eSGarrett Wollman 	return EOPNOTSUPP;
860a29f300eSGarrett Wollman }
861a29f300eSGarrett Wollman 
862a29f300eSGarrett Wollman int
863b40ce416SJulian Elischer pru_listen_notsupp(struct socket *so, struct thread *td)
864d8392c6cSGarrett Wollman {
865d8392c6cSGarrett Wollman 	return EOPNOTSUPP;
866d8392c6cSGarrett Wollman }
867d8392c6cSGarrett Wollman 
868d8392c6cSGarrett Wollman int
869d8392c6cSGarrett Wollman pru_rcvd_notsupp(struct socket *so, int flags)
870d8392c6cSGarrett Wollman {
871d8392c6cSGarrett Wollman 	return EOPNOTSUPP;
872d8392c6cSGarrett Wollman }
873d8392c6cSGarrett Wollman 
874d8392c6cSGarrett Wollman int
875d8392c6cSGarrett Wollman pru_rcvoob_notsupp(struct socket *so, struct mbuf *m, int flags)
876d8392c6cSGarrett Wollman {
877d8392c6cSGarrett Wollman 	return EOPNOTSUPP;
878d8392c6cSGarrett Wollman }
879d8392c6cSGarrett Wollman 
880d8392c6cSGarrett Wollman /*
881d8392c6cSGarrett Wollman  * This isn't really a ``null'' operation, but it's the default one
882d8392c6cSGarrett Wollman  * and doesn't do anything destructive.
883d8392c6cSGarrett Wollman  */
884d8392c6cSGarrett Wollman int
885d8392c6cSGarrett Wollman pru_sense_null(struct socket *so, struct stat *sb)
886d8392c6cSGarrett Wollman {
887d8392c6cSGarrett Wollman 	sb->st_blksize = so->so_snd.sb_hiwat;
888d8392c6cSGarrett Wollman 	return 0;
889d8392c6cSGarrett Wollman }
890639acc13SGarrett Wollman 
891639acc13SGarrett Wollman /*
89257bf258eSGarrett Wollman  * Make a copy of a sockaddr in a malloced buffer of type M_SONAME.
89357bf258eSGarrett Wollman  */
89457bf258eSGarrett Wollman struct sockaddr *
89557bf258eSGarrett Wollman dup_sockaddr(sa, canwait)
89657bf258eSGarrett Wollman 	struct sockaddr *sa;
89757bf258eSGarrett Wollman 	int canwait;
89857bf258eSGarrett Wollman {
89957bf258eSGarrett Wollman 	struct sockaddr *sa2;
90057bf258eSGarrett Wollman 
90157bf258eSGarrett Wollman 	MALLOC(sa2, struct sockaddr *, sa->sa_len, M_SONAME,
90257bf258eSGarrett Wollman 	       canwait ? M_WAITOK : M_NOWAIT);
90357bf258eSGarrett Wollman 	if (sa2)
90457bf258eSGarrett Wollman 		bcopy(sa, sa2, sa->sa_len);
90557bf258eSGarrett Wollman 	return sa2;
90657bf258eSGarrett Wollman }
90757bf258eSGarrett Wollman 
90857bf258eSGarrett Wollman /*
90998271db4SGarrett Wollman  * Create an external-format (``xsocket'') structure using the information
91098271db4SGarrett Wollman  * in the kernel-format socket structure pointed to by so.  This is done
91198271db4SGarrett Wollman  * to reduce the spew of irrelevant information over this interface,
91298271db4SGarrett Wollman  * to isolate user code from changes in the kernel structure, and
91398271db4SGarrett Wollman  * potentially to provide information-hiding if we decide that
91498271db4SGarrett Wollman  * some of this information should be hidden from users.
91598271db4SGarrett Wollman  */
91698271db4SGarrett Wollman void
91798271db4SGarrett Wollman sotoxsocket(struct socket *so, struct xsocket *xso)
91898271db4SGarrett Wollman {
91998271db4SGarrett Wollman 	xso->xso_len = sizeof *xso;
92098271db4SGarrett Wollman 	xso->xso_so = so;
92198271db4SGarrett Wollman 	xso->so_type = so->so_type;
92298271db4SGarrett Wollman 	xso->so_options = so->so_options;
92398271db4SGarrett Wollman 	xso->so_linger = so->so_linger;
92498271db4SGarrett Wollman 	xso->so_state = so->so_state;
92598271db4SGarrett Wollman 	xso->so_pcb = so->so_pcb;
92698271db4SGarrett Wollman 	xso->xso_protocol = so->so_proto->pr_protocol;
92798271db4SGarrett Wollman 	xso->xso_family = so->so_proto->pr_domain->dom_family;
92898271db4SGarrett Wollman 	xso->so_qlen = so->so_qlen;
92998271db4SGarrett Wollman 	xso->so_incqlen = so->so_incqlen;
93098271db4SGarrett Wollman 	xso->so_qlimit = so->so_qlimit;
93198271db4SGarrett Wollman 	xso->so_timeo = so->so_timeo;
93298271db4SGarrett Wollman 	xso->so_error = so->so_error;
933831d27a9SDon Lewis 	xso->so_pgid = so->so_sigio ? so->so_sigio->sio_pgid : 0;
93498271db4SGarrett Wollman 	xso->so_oobmark = so->so_oobmark;
93598271db4SGarrett Wollman 	sbtoxsockbuf(&so->so_snd, &xso->so_snd);
93698271db4SGarrett Wollman 	sbtoxsockbuf(&so->so_rcv, &xso->so_rcv);
9372f9a2132SBrian Feldman 	xso->so_uid = so->so_cred->cr_uid;
93898271db4SGarrett Wollman }
93998271db4SGarrett Wollman 
94098271db4SGarrett Wollman /*
94198271db4SGarrett Wollman  * This does the same for sockbufs.  Note that the xsockbuf structure,
94298271db4SGarrett Wollman  * since it is always embedded in a socket, does not include a self
94398271db4SGarrett Wollman  * pointer nor a length.  We make this entry point public in case
94498271db4SGarrett Wollman  * some other mechanism needs it.
94598271db4SGarrett Wollman  */
94698271db4SGarrett Wollman void
94798271db4SGarrett Wollman sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb)
94898271db4SGarrett Wollman {
94998271db4SGarrett Wollman 	xsb->sb_cc = sb->sb_cc;
95098271db4SGarrett Wollman 	xsb->sb_hiwat = sb->sb_hiwat;
95198271db4SGarrett Wollman 	xsb->sb_mbcnt = sb->sb_mbcnt;
95298271db4SGarrett Wollman 	xsb->sb_mbmax = sb->sb_mbmax;
95398271db4SGarrett Wollman 	xsb->sb_lowat = sb->sb_lowat;
95498271db4SGarrett Wollman 	xsb->sb_flags = sb->sb_flags;
95598271db4SGarrett Wollman 	xsb->sb_timeo = sb->sb_timeo;
95698271db4SGarrett Wollman }
95798271db4SGarrett Wollman 
95898271db4SGarrett Wollman /*
959639acc13SGarrett Wollman  * Here is the definition of some of the basic objects in the kern.ipc
960639acc13SGarrett Wollman  * branch of the MIB.
961639acc13SGarrett Wollman  */
962639acc13SGarrett Wollman SYSCTL_NODE(_kern, KERN_IPC, ipc, CTLFLAG_RW, 0, "IPC");
963639acc13SGarrett Wollman 
964639acc13SGarrett Wollman /* This takes the place of kern.maxsockbuf, which moved to kern.ipc. */
965639acc13SGarrett Wollman static int dummy;
966639acc13SGarrett Wollman SYSCTL_INT(_kern, KERN_DUMMY, dummy, CTLFLAG_RW, &dummy, 0, "");
967639acc13SGarrett Wollman 
9683d177f46SBill Fumerola SYSCTL_INT(_kern_ipc, KIPC_MAXSOCKBUF, maxsockbuf, CTLFLAG_RW,
9693d177f46SBill Fumerola     &sb_max, 0, "Maximum socket buffer size");
97054d77689SJeff Roberson SYSCTL_INT(_kern_ipc, OID_AUTO, maxsockets, CTLFLAG_RD,
97154d77689SJeff Roberson     &maxsockets, 0, "Maximum number of sockets avaliable");
972639acc13SGarrett Wollman SYSCTL_INT(_kern_ipc, KIPC_SOCKBUF_WASTE, sockbuf_waste_factor, CTLFLAG_RW,
973639acc13SGarrett Wollman     &sb_efficiency, 0, "");
97454d77689SJeff Roberson 
97554d77689SJeff Roberson /*
97654d77689SJeff Roberson  * Initialise maxsockets
97754d77689SJeff Roberson  */
97854d77689SJeff Roberson static void init_maxsockets(void *ignored)
97954d77689SJeff Roberson {
98054d77689SJeff Roberson 	TUNABLE_INT_FETCH("kern.ipc.maxsockets", &maxsockets);
98154d77689SJeff Roberson 	maxsockets = imax(maxsockets, imax(maxfiles, nmbclusters));
98254d77689SJeff Roberson }
98354d77689SJeff Roberson SYSINIT(param, SI_SUB_TUNABLES, SI_ORDER_ANY, init_maxsockets, NULL);
984