xref: /freebsd/sys/kern/uipc_sockbuf.c (revision 32af0d74f096b04ae54746f22fdc8453acceb141)
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 
37134c934cSMike Smith #include "opt_param.h"
38df8bae1dSRodney W. Grimes #include <sys/param.h>
39df8bae1dSRodney W. Grimes #include <sys/systm.h>
4098271db4SGarrett Wollman #include <sys/domain.h>
41134c934cSMike Smith #include <sys/file.h>	/* for maxfiles */
42ff5c09daSGarrett Wollman #include <sys/kernel.h>
43df8bae1dSRodney W. Grimes #include <sys/proc.h>
44df8bae1dSRodney W. Grimes #include <sys/malloc.h>
45df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
46df8bae1dSRodney W. Grimes #include <sys/protosw.h>
472f9a2132SBrian Feldman #include <sys/resourcevar.h>
48797f2d22SPoul-Henning Kamp #include <sys/stat.h>
49df8bae1dSRodney W. Grimes #include <sys/socket.h>
50df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
51797f2d22SPoul-Henning Kamp #include <sys/signalvar.h>
52ff5c09daSGarrett Wollman #include <sys/sysctl.h>
53bfbbc4aaSJason Evans #include <sys/aio.h> /* for aio_swake proto */
54cb679c38SJonathan Lemon #include <sys/event.h>
5526f9a767SRodney W. Grimes 
56134c934cSMike Smith int	maxsockets;
57134c934cSMike Smith 
58df8bae1dSRodney W. Grimes /*
59df8bae1dSRodney W. Grimes  * Primitive routines for operating on sockets and socket buffers
60df8bae1dSRodney W. Grimes  */
61df8bae1dSRodney W. Grimes 
62ff5c09daSGarrett Wollman u_long	sb_max = SB_MAX;		/* XXX should be static */
63df8bae1dSRodney W. Grimes 
644b29bc4fSGarrett Wollman static	u_long sb_efficiency = 8;	/* parameter for sbreserve() */
654b29bc4fSGarrett Wollman 
66df8bae1dSRodney W. Grimes /*
67df8bae1dSRodney W. Grimes  * Procedures to manipulate state flags of socket
68df8bae1dSRodney W. Grimes  * and do appropriate wakeups.  Normal sequence from the
69df8bae1dSRodney W. Grimes  * active (originating) side is that soisconnecting() is
70df8bae1dSRodney W. Grimes  * called during processing of connect() call,
71df8bae1dSRodney W. Grimes  * resulting in an eventual call to soisconnected() if/when the
72df8bae1dSRodney W. Grimes  * connection is established.  When the connection is torn down
73df8bae1dSRodney W. Grimes  * soisdisconnecting() is called during processing of disconnect() call,
74df8bae1dSRodney W. Grimes  * and soisdisconnected() is called when the connection to the peer
75df8bae1dSRodney W. Grimes  * is totally severed.  The semantics of these routines are such that
76df8bae1dSRodney W. Grimes  * connectionless protocols can call soisconnected() and soisdisconnected()
77df8bae1dSRodney W. Grimes  * only, bypassing the in-progress calls when setting up a ``connection''
78df8bae1dSRodney W. Grimes  * takes no time.
79df8bae1dSRodney W. Grimes  *
80df8bae1dSRodney W. Grimes  * From the passive side, a socket is created with
81dc97381eSPeter Wemm  * two queues of sockets: so_incomp for connections in progress
82dc97381eSPeter Wemm  * and so_comp for connections already made and awaiting user acceptance.
83df8bae1dSRodney W. Grimes  * As a protocol is preparing incoming connections, it creates a socket
84dc97381eSPeter Wemm  * structure queued on so_incomp by calling sonewconn().  When the connection
85df8bae1dSRodney W. Grimes  * is established, soisconnected() is called, and transfers the
86dc97381eSPeter Wemm  * socket structure to so_comp, making it available to accept().
87df8bae1dSRodney W. Grimes  *
88df8bae1dSRodney W. Grimes  * If a socket is closed with sockets on either
89dc97381eSPeter Wemm  * so_incomp or so_comp, these sockets are dropped.
90df8bae1dSRodney W. Grimes  *
91df8bae1dSRodney W. Grimes  * If higher level protocols are implemented in
92df8bae1dSRodney W. Grimes  * the kernel, the wakeups done here will sometimes
93df8bae1dSRodney W. Grimes  * cause software-interrupt process scheduling.
94df8bae1dSRodney W. Grimes  */
95df8bae1dSRodney W. Grimes 
9626f9a767SRodney W. Grimes void
97df8bae1dSRodney W. Grimes soisconnecting(so)
98df8bae1dSRodney W. Grimes 	register struct socket *so;
99df8bae1dSRodney W. Grimes {
100df8bae1dSRodney W. Grimes 
101df8bae1dSRodney W. Grimes 	so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
102df8bae1dSRodney W. Grimes 	so->so_state |= SS_ISCONNECTING;
103df8bae1dSRodney W. Grimes }
104df8bae1dSRodney W. Grimes 
10526f9a767SRodney W. Grimes void
106df8bae1dSRodney W. Grimes soisconnected(so)
1078f4e4aa5SAlfred Perlstein 	struct socket *so;
108df8bae1dSRodney W. Grimes {
1098f4e4aa5SAlfred Perlstein 	struct socket *head = so->so_head;
110df8bae1dSRodney W. Grimes 
111df8bae1dSRodney W. Grimes 	so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING);
112df8bae1dSRodney W. Grimes 	so->so_state |= SS_ISCONNECTED;
113be24e9e8SDavid Greenman 	if (head && (so->so_state & SS_INCOMP)) {
114a79b7128SAlfred Perlstein 		if ((so->so_options & SO_ACCEPTFILTER) != 0) {
115a79b7128SAlfred Perlstein 			so->so_upcall = head->so_accf->so_accept_filter->accf_callback;
116a79b7128SAlfred Perlstein 			so->so_upcallarg = head->so_accf->so_accept_filter_arg;
117a79b7128SAlfred Perlstein 			so->so_rcv.sb_flags |= SB_UPCALL;
118a79b7128SAlfred Perlstein 			so->so_options &= ~SO_ACCEPTFILTER;
119a79b7128SAlfred Perlstein 			so->so_upcall(so, so->so_upcallarg, 0);
120a79b7128SAlfred Perlstein 			return;
121a79b7128SAlfred Perlstein 		}
122be24e9e8SDavid Greenman 		TAILQ_REMOVE(&head->so_incomp, so, so_list);
123a51764a8SPaul Traina 		head->so_incqlen--;
124be24e9e8SDavid Greenman 		so->so_state &= ~SS_INCOMP;
125be24e9e8SDavid Greenman 		TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
126be24e9e8SDavid Greenman 		so->so_state |= SS_COMP;
127df8bae1dSRodney W. Grimes 		sorwakeup(head);
128a91b8721SDavid Greenman 		wakeup_one(&head->so_timeo);
129df8bae1dSRodney W. Grimes 	} else {
130a91b8721SDavid Greenman 		wakeup(&so->so_timeo);
131df8bae1dSRodney W. Grimes 		sorwakeup(so);
132df8bae1dSRodney W. Grimes 		sowwakeup(so);
133df8bae1dSRodney W. Grimes 	}
134df8bae1dSRodney W. Grimes }
135df8bae1dSRodney W. Grimes 
13626f9a767SRodney W. Grimes void
137df8bae1dSRodney W. Grimes soisdisconnecting(so)
138df8bae1dSRodney W. Grimes 	register struct socket *so;
139df8bae1dSRodney W. Grimes {
140df8bae1dSRodney W. Grimes 
141df8bae1dSRodney W. Grimes 	so->so_state &= ~SS_ISCONNECTING;
142df8bae1dSRodney W. Grimes 	so->so_state |= (SS_ISDISCONNECTING|SS_CANTRCVMORE|SS_CANTSENDMORE);
143df8bae1dSRodney W. Grimes 	wakeup((caddr_t)&so->so_timeo);
144df8bae1dSRodney W. Grimes 	sowwakeup(so);
145df8bae1dSRodney W. Grimes 	sorwakeup(so);
146df8bae1dSRodney W. Grimes }
147df8bae1dSRodney W. Grimes 
14826f9a767SRodney W. Grimes void
149df8bae1dSRodney W. Grimes soisdisconnected(so)
150df8bae1dSRodney W. Grimes 	register struct socket *so;
151df8bae1dSRodney W. Grimes {
152df8bae1dSRodney W. Grimes 
153df8bae1dSRodney W. Grimes 	so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
154527b7a14SBill Fenner 	so->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE|SS_ISDISCONNECTED);
155df8bae1dSRodney W. Grimes 	wakeup((caddr_t)&so->so_timeo);
156df8bae1dSRodney W. Grimes 	sowwakeup(so);
157df8bae1dSRodney W. Grimes 	sorwakeup(so);
158df8bae1dSRodney W. Grimes }
159df8bae1dSRodney W. Grimes 
160df8bae1dSRodney W. Grimes /*
161ebb0cbeaSPaul Traina  * Return a random connection that hasn't been serviced yet and
162a51764a8SPaul Traina  * is eligible for discard.  There is a one in qlen chance that
163a51764a8SPaul Traina  * we will return a null, saying that there are no dropable
164a51764a8SPaul Traina  * requests.  In this case, the protocol specific code should drop
165a51764a8SPaul Traina  * the new request.  This insures fairness.
166ebb0cbeaSPaul Traina  *
167ebb0cbeaSPaul Traina  * This may be used in conjunction with protocol specific queue
168ebb0cbeaSPaul Traina  * congestion routines.
169ebb0cbeaSPaul Traina  */
170ebb0cbeaSPaul Traina struct socket *
171ebb0cbeaSPaul Traina sodropablereq(head)
172ebb0cbeaSPaul Traina 	register struct socket *head;
173ebb0cbeaSPaul Traina {
174ebb0cbeaSPaul Traina 	register struct socket *so;
175ebb0cbeaSPaul Traina 	unsigned int i, j, qlen;
176ebb0cbeaSPaul Traina 	static int rnd;
17700af9731SPoul-Henning Kamp 	static struct timeval old_runtime;
178ebb0cbeaSPaul Traina 	static unsigned int cur_cnt, old_cnt;
17900af9731SPoul-Henning Kamp 	struct timeval tv;
180ebb0cbeaSPaul Traina 
181c21410e1SPoul-Henning Kamp 	getmicrouptime(&tv);
18200af9731SPoul-Henning Kamp 	if ((i = (tv.tv_sec - old_runtime.tv_sec)) != 0) {
18300af9731SPoul-Henning Kamp 		old_runtime = tv;
184ebb0cbeaSPaul Traina 		old_cnt = cur_cnt / i;
185ebb0cbeaSPaul Traina 		cur_cnt = 0;
186ebb0cbeaSPaul Traina 	}
187ebb0cbeaSPaul Traina 
188a51764a8SPaul Traina 	so = TAILQ_FIRST(&head->so_incomp);
189a51764a8SPaul Traina 	if (!so)
190a51764a8SPaul Traina 		return (so);
191a51764a8SPaul Traina 
192a51764a8SPaul Traina 	qlen = head->so_incqlen;
193ebb0cbeaSPaul Traina 	if (++cur_cnt > qlen || old_cnt > qlen) {
194ebb0cbeaSPaul Traina 		rnd = (314159 * rnd + 66329) & 0xffff;
195ebb0cbeaSPaul Traina 		j = ((qlen + 1) * rnd) >> 16;
196ebb0cbeaSPaul Traina 
197ebb0cbeaSPaul Traina 		while (j-- && so)
198ebb0cbeaSPaul Traina 		    so = TAILQ_NEXT(so, so_list);
199ebb0cbeaSPaul Traina 	}
200ebb0cbeaSPaul Traina 
201ebb0cbeaSPaul Traina 	return (so);
202ebb0cbeaSPaul Traina }
203ebb0cbeaSPaul Traina 
204ebb0cbeaSPaul Traina /*
205df8bae1dSRodney W. Grimes  * When an attempt at a new connection is noted on a socket
206df8bae1dSRodney W. Grimes  * which accepts connections, sonewconn is called.  If the
207df8bae1dSRodney W. Grimes  * connection is possible (subject to space constraints, etc.)
208df8bae1dSRodney W. Grimes  * then we allocate a new structure, propoerly linked into the
209df8bae1dSRodney W. Grimes  * data structure of the original socket, and return this.
210df8bae1dSRodney W. Grimes  * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED.
211df8bae1dSRodney W. Grimes  */
212df8bae1dSRodney W. Grimes struct socket *
213548af278SBill Fenner sonewconn(head, connstatus)
214df8bae1dSRodney W. Grimes 	register struct socket *head;
215df8bae1dSRodney W. Grimes 	int connstatus;
216df8bae1dSRodney W. Grimes {
2172f9a2132SBrian Feldman 
2182f9a2132SBrian Feldman 	return (sonewconn3(head, connstatus, NULL));
2192f9a2132SBrian Feldman }
2202f9a2132SBrian Feldman 
2212f9a2132SBrian Feldman struct socket *
2222f9a2132SBrian Feldman sonewconn3(head, connstatus, p)
2232f9a2132SBrian Feldman 	register struct socket *head;
2242f9a2132SBrian Feldman 	int connstatus;
2252f9a2132SBrian Feldman 	struct proc *p;
2262f9a2132SBrian Feldman {
227df8bae1dSRodney W. Grimes 	register struct socket *so;
228df8bae1dSRodney W. Grimes 
229ebb0cbeaSPaul Traina 	if (head->so_qlen > 3 * head->so_qlimit / 2)
230df8bae1dSRodney W. Grimes 		return ((struct socket *)0);
23198271db4SGarrett Wollman 	so = soalloc(0);
232df8bae1dSRodney W. Grimes 	if (so == NULL)
233df8bae1dSRodney W. Grimes 		return ((struct socket *)0);
234be24e9e8SDavid Greenman 	so->so_head = head;
235df8bae1dSRodney W. Grimes 	so->so_type = head->so_type;
236df8bae1dSRodney W. Grimes 	so->so_options = head->so_options &~ SO_ACCEPTCONN;
237df8bae1dSRodney W. Grimes 	so->so_linger = head->so_linger;
238df8bae1dSRodney W. Grimes 	so->so_state = head->so_state | SS_NOFDREF;
239df8bae1dSRodney W. Grimes 	so->so_proto = head->so_proto;
240df8bae1dSRodney W. Grimes 	so->so_timeo = head->so_timeo;
2412f9a2132SBrian Feldman 	so->so_cred = p ? p->p_ucred : head->so_cred;
2422f9a2132SBrian Feldman 	crhold(so->so_cred);
2432f9a2132SBrian Feldman 	if (soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat) ||
2442f9a2132SBrian Feldman 	    (*so->so_proto->pr_usrreqs->pru_attach)(so, 0, NULL)) {
24598271db4SGarrett Wollman 		sodealloc(so);
246b1396a35SGarrett Wollman 		return ((struct socket *)0);
247b1396a35SGarrett Wollman 	}
248b1396a35SGarrett Wollman 
249be24e9e8SDavid Greenman 	if (connstatus) {
250be24e9e8SDavid Greenman 		TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
251be24e9e8SDavid Greenman 		so->so_state |= SS_COMP;
252be24e9e8SDavid Greenman 	} else {
253be24e9e8SDavid Greenman 		TAILQ_INSERT_TAIL(&head->so_incomp, so, so_list);
254be24e9e8SDavid Greenman 		so->so_state |= SS_INCOMP;
255ebb0cbeaSPaul Traina 		head->so_incqlen++;
256be24e9e8SDavid Greenman 	}
257be24e9e8SDavid Greenman 	head->so_qlen++;
258df8bae1dSRodney W. Grimes 	if (connstatus) {
259df8bae1dSRodney W. Grimes 		sorwakeup(head);
260df8bae1dSRodney W. Grimes 		wakeup((caddr_t)&head->so_timeo);
261df8bae1dSRodney W. Grimes 		so->so_state |= connstatus;
262df8bae1dSRodney W. Grimes 	}
263df8bae1dSRodney W. Grimes 	return (so);
264df8bae1dSRodney W. Grimes }
265df8bae1dSRodney W. Grimes 
266df8bae1dSRodney W. Grimes /*
267df8bae1dSRodney W. Grimes  * Socantsendmore indicates that no more data will be sent on the
268df8bae1dSRodney W. Grimes  * socket; it would normally be applied to a socket when the user
269df8bae1dSRodney W. Grimes  * informs the system that no more data is to be sent, by the protocol
270df8bae1dSRodney W. Grimes  * code (in case PRU_SHUTDOWN).  Socantrcvmore indicates that no more data
271df8bae1dSRodney W. Grimes  * will be received, and will normally be applied to the socket by a
272df8bae1dSRodney W. Grimes  * protocol when it detects that the peer will send no more data.
273df8bae1dSRodney W. Grimes  * Data queued for reading in the socket may yet be read.
274df8bae1dSRodney W. Grimes  */
275df8bae1dSRodney W. Grimes 
27626f9a767SRodney W. Grimes void
277df8bae1dSRodney W. Grimes socantsendmore(so)
278df8bae1dSRodney W. Grimes 	struct socket *so;
279df8bae1dSRodney W. Grimes {
280df8bae1dSRodney W. Grimes 
281df8bae1dSRodney W. Grimes 	so->so_state |= SS_CANTSENDMORE;
282df8bae1dSRodney W. Grimes 	sowwakeup(so);
283df8bae1dSRodney W. Grimes }
284df8bae1dSRodney W. Grimes 
28526f9a767SRodney W. Grimes void
286df8bae1dSRodney W. Grimes socantrcvmore(so)
287df8bae1dSRodney W. Grimes 	struct socket *so;
288df8bae1dSRodney W. Grimes {
289df8bae1dSRodney W. Grimes 
290df8bae1dSRodney W. Grimes 	so->so_state |= SS_CANTRCVMORE;
291df8bae1dSRodney W. Grimes 	sorwakeup(so);
292df8bae1dSRodney W. Grimes }
293df8bae1dSRodney W. Grimes 
294df8bae1dSRodney W. Grimes /*
295df8bae1dSRodney W. Grimes  * Wait for data to arrive at/drain from a socket buffer.
296df8bae1dSRodney W. Grimes  */
29726f9a767SRodney W. Grimes int
298df8bae1dSRodney W. Grimes sbwait(sb)
299df8bae1dSRodney W. Grimes 	struct sockbuf *sb;
300df8bae1dSRodney W. Grimes {
301df8bae1dSRodney W. Grimes 
302df8bae1dSRodney W. Grimes 	sb->sb_flags |= SB_WAIT;
303df8bae1dSRodney W. Grimes 	return (tsleep((caddr_t)&sb->sb_cc,
30447daf5d5SBruce Evans 	    (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH, "sbwait",
305df8bae1dSRodney W. Grimes 	    sb->sb_timeo));
306df8bae1dSRodney W. Grimes }
307df8bae1dSRodney W. Grimes 
308df8bae1dSRodney W. Grimes /*
309df8bae1dSRodney W. Grimes  * Lock a sockbuf already known to be locked;
310df8bae1dSRodney W. Grimes  * return any error returned from sleep (EINTR).
311df8bae1dSRodney W. Grimes  */
31226f9a767SRodney W. Grimes int
313df8bae1dSRodney W. Grimes sb_lock(sb)
314df8bae1dSRodney W. Grimes 	register struct sockbuf *sb;
315df8bae1dSRodney W. Grimes {
316df8bae1dSRodney W. Grimes 	int error;
317df8bae1dSRodney W. Grimes 
318df8bae1dSRodney W. Grimes 	while (sb->sb_flags & SB_LOCK) {
319df8bae1dSRodney W. Grimes 		sb->sb_flags |= SB_WANT;
320797f2d22SPoul-Henning Kamp 		error = tsleep((caddr_t)&sb->sb_flags,
321df8bae1dSRodney W. Grimes 		    (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK|PCATCH,
32247daf5d5SBruce Evans 		    "sblock", 0);
323797f2d22SPoul-Henning Kamp 		if (error)
324df8bae1dSRodney W. Grimes 			return (error);
325df8bae1dSRodney W. Grimes 	}
326df8bae1dSRodney W. Grimes 	sb->sb_flags |= SB_LOCK;
327df8bae1dSRodney W. Grimes 	return (0);
328df8bae1dSRodney W. Grimes }
329df8bae1dSRodney W. Grimes 
330df8bae1dSRodney W. Grimes /*
331df8bae1dSRodney W. Grimes  * Wakeup processes waiting on a socket buffer.
332df8bae1dSRodney W. Grimes  * Do asynchronous notification via SIGIO
333df8bae1dSRodney W. Grimes  * if the socket has the SS_ASYNC flag set.
334df8bae1dSRodney W. Grimes  */
33526f9a767SRodney W. Grimes void
336df8bae1dSRodney W. Grimes sowakeup(so, sb)
337df8bae1dSRodney W. Grimes 	register struct socket *so;
338df8bae1dSRodney W. Grimes 	register struct sockbuf *sb;
339df8bae1dSRodney W. Grimes {
340df8bae1dSRodney W. Grimes 	selwakeup(&sb->sb_sel);
341df8bae1dSRodney W. Grimes 	sb->sb_flags &= ~SB_SEL;
342df8bae1dSRodney W. Grimes 	if (sb->sb_flags & SB_WAIT) {
343df8bae1dSRodney W. Grimes 		sb->sb_flags &= ~SB_WAIT;
344df8bae1dSRodney W. Grimes 		wakeup((caddr_t)&sb->sb_cc);
345df8bae1dSRodney W. Grimes 	}
346831d27a9SDon Lewis 	if ((so->so_state & SS_ASYNC) && so->so_sigio != NULL)
347831d27a9SDon Lewis 		pgsigio(so->so_sigio, SIGIO, 0);
3484dc75870SPeter Wemm 	if (sb->sb_flags & SB_UPCALL)
3494dc75870SPeter Wemm 		(*so->so_upcall)(so, so->so_upcallarg, M_DONTWAIT);
350bfbbc4aaSJason Evans 	if (sb->sb_flags & SB_AIO)
351bfbbc4aaSJason Evans 		aio_swake(so, sb);
352cb679c38SJonathan Lemon 	KNOTE(&sb->sb_sel.si_note, 0);
353df8bae1dSRodney W. Grimes }
354df8bae1dSRodney W. Grimes 
355df8bae1dSRodney W. Grimes /*
356df8bae1dSRodney W. Grimes  * Socket buffer (struct sockbuf) utility routines.
357df8bae1dSRodney W. Grimes  *
358df8bae1dSRodney W. Grimes  * Each socket contains two socket buffers: one for sending data and
359df8bae1dSRodney W. Grimes  * one for receiving data.  Each buffer contains a queue of mbufs,
360df8bae1dSRodney W. Grimes  * information about the number of mbufs and amount of data in the
361df8bae1dSRodney W. Grimes  * queue, and other fields allowing select() statements and notification
362df8bae1dSRodney W. Grimes  * on data availability to be implemented.
363df8bae1dSRodney W. Grimes  *
364df8bae1dSRodney W. Grimes  * Data stored in a socket buffer is maintained as a list of records.
365df8bae1dSRodney W. Grimes  * Each record is a list of mbufs chained together with the m_next
366df8bae1dSRodney W. Grimes  * field.  Records are chained together with the m_nextpkt field. The upper
367df8bae1dSRodney W. Grimes  * level routine soreceive() expects the following conventions to be
368df8bae1dSRodney W. Grimes  * observed when placing information in the receive buffer:
369df8bae1dSRodney W. Grimes  *
370df8bae1dSRodney W. Grimes  * 1. If the protocol requires each message be preceded by the sender's
371df8bae1dSRodney W. Grimes  *    name, then a record containing that name must be present before
372df8bae1dSRodney W. Grimes  *    any associated data (mbuf's must be of type MT_SONAME).
373df8bae1dSRodney W. Grimes  * 2. If the protocol supports the exchange of ``access rights'' (really
374df8bae1dSRodney W. Grimes  *    just additional data associated with the message), and there are
375df8bae1dSRodney W. Grimes  *    ``rights'' to be received, then a record containing this data
376df8bae1dSRodney W. Grimes  *    should be present (mbuf's must be of type MT_RIGHTS).
377df8bae1dSRodney W. Grimes  * 3. If a name or rights record exists, then it must be followed by
378df8bae1dSRodney W. Grimes  *    a data record, perhaps of zero length.
379df8bae1dSRodney W. Grimes  *
380df8bae1dSRodney W. Grimes  * Before using a new socket structure it is first necessary to reserve
381df8bae1dSRodney W. Grimes  * buffer space to the socket, by calling sbreserve().  This should commit
382df8bae1dSRodney W. Grimes  * some of the available buffer space in the system buffer pool for the
383df8bae1dSRodney W. Grimes  * socket (currently, it does nothing but enforce limits).  The space
384df8bae1dSRodney W. Grimes  * should be released by calling sbrelease() when the socket is destroyed.
385df8bae1dSRodney W. Grimes  */
386df8bae1dSRodney W. Grimes 
38726f9a767SRodney W. Grimes int
388df8bae1dSRodney W. Grimes soreserve(so, sndcc, rcvcc)
389df8bae1dSRodney W. Grimes 	register struct socket *so;
390df8bae1dSRodney W. Grimes 	u_long sndcc, rcvcc;
391df8bae1dSRodney W. Grimes {
392ecf72308SBrian Feldman 	struct proc *p = curproc;
393df8bae1dSRodney W. Grimes 
394ecf72308SBrian Feldman 	if (sbreserve(&so->so_snd, sndcc, so, p) == 0)
395df8bae1dSRodney W. Grimes 		goto bad;
396ecf72308SBrian Feldman 	if (sbreserve(&so->so_rcv, rcvcc, so, p) == 0)
397df8bae1dSRodney W. Grimes 		goto bad2;
398df8bae1dSRodney W. Grimes 	if (so->so_rcv.sb_lowat == 0)
399df8bae1dSRodney W. Grimes 		so->so_rcv.sb_lowat = 1;
400df8bae1dSRodney W. Grimes 	if (so->so_snd.sb_lowat == 0)
401df8bae1dSRodney W. Grimes 		so->so_snd.sb_lowat = MCLBYTES;
402df8bae1dSRodney W. Grimes 	if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat)
403df8bae1dSRodney W. Grimes 		so->so_snd.sb_lowat = so->so_snd.sb_hiwat;
404df8bae1dSRodney W. Grimes 	return (0);
405df8bae1dSRodney W. Grimes bad2:
406ecf72308SBrian Feldman 	sbrelease(&so->so_snd, so);
407df8bae1dSRodney W. Grimes bad:
408df8bae1dSRodney W. Grimes 	return (ENOBUFS);
409df8bae1dSRodney W. Grimes }
410df8bae1dSRodney W. Grimes 
411df8bae1dSRodney W. Grimes /*
412df8bae1dSRodney W. Grimes  * Allot mbufs to a sockbuf.
413df8bae1dSRodney W. Grimes  * Attempt to scale mbmax so that mbcnt doesn't become limiting
414df8bae1dSRodney W. Grimes  * if buffering efficiency is near the normal case.
415df8bae1dSRodney W. Grimes  */
41626f9a767SRodney W. Grimes int
417ecf72308SBrian Feldman sbreserve(sb, cc, so, p)
418df8bae1dSRodney W. Grimes 	struct sockbuf *sb;
419df8bae1dSRodney W. Grimes 	u_long cc;
420ecf72308SBrian Feldman 	struct socket *so;
421ecf72308SBrian Feldman 	struct proc *p;
422df8bae1dSRodney W. Grimes {
423ecf72308SBrian Feldman 
424ecf72308SBrian Feldman 	/*
425ecf72308SBrian Feldman 	 * p will only be NULL when we're in an interrupt
426ecf72308SBrian Feldman 	 * (e.g. in tcp_input())
427ecf72308SBrian Feldman 	 */
4285bee01c8SGarrett Wollman 	if ((u_quad_t)cc > (u_quad_t)sb_max * MCLBYTES / (MSIZE + MCLBYTES))
429df8bae1dSRodney W. Grimes 		return (0);
430f535380cSDon Lewis 	if (!chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, cc,
431b6240737SBrian Feldman 	    p ? p->p_rlimit[RLIMIT_SBSIZE].rlim_cur : RLIM_INFINITY)) {
432ecf72308SBrian Feldman 		return (0);
433c6362551SAlfred Perlstein 	}
4344b29bc4fSGarrett Wollman 	sb->sb_mbmax = min(cc * sb_efficiency, sb_max);
435df8bae1dSRodney W. Grimes 	if (sb->sb_lowat > sb->sb_hiwat)
436df8bae1dSRodney W. Grimes 		sb->sb_lowat = sb->sb_hiwat;
437df8bae1dSRodney W. Grimes 	return (1);
438df8bae1dSRodney W. Grimes }
439df8bae1dSRodney W. Grimes 
440df8bae1dSRodney W. Grimes /*
441df8bae1dSRodney W. Grimes  * Free mbufs held by a socket, and reserved mbuf space.
442df8bae1dSRodney W. Grimes  */
44326f9a767SRodney W. Grimes void
444ecf72308SBrian Feldman sbrelease(sb, so)
445df8bae1dSRodney W. Grimes 	struct sockbuf *sb;
446ecf72308SBrian Feldman 	struct socket *so;
447df8bae1dSRodney W. Grimes {
448df8bae1dSRodney W. Grimes 
449df8bae1dSRodney W. Grimes 	sbflush(sb);
450f535380cSDon Lewis 	(void)chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, 0,
451f535380cSDon Lewis 	    RLIM_INFINITY);
4526aef685fSBrian Feldman 	sb->sb_mbmax = 0;
453df8bae1dSRodney W. Grimes }
454df8bae1dSRodney W. Grimes 
455df8bae1dSRodney W. Grimes /*
456df8bae1dSRodney W. Grimes  * Routines to add and remove
457df8bae1dSRodney W. Grimes  * data from an mbuf queue.
458df8bae1dSRodney W. Grimes  *
459df8bae1dSRodney W. Grimes  * The routines sbappend() or sbappendrecord() are normally called to
460df8bae1dSRodney W. Grimes  * append new mbufs to a socket buffer, after checking that adequate
461df8bae1dSRodney W. Grimes  * space is available, comparing the function sbspace() with the amount
462df8bae1dSRodney W. Grimes  * of data to be added.  sbappendrecord() differs from sbappend() in
463df8bae1dSRodney W. Grimes  * that data supplied is treated as the beginning of a new record.
464df8bae1dSRodney W. Grimes  * To place a sender's address, optional access rights, and data in a
465df8bae1dSRodney W. Grimes  * socket receive buffer, sbappendaddr() should be used.  To place
466df8bae1dSRodney W. Grimes  * access rights and data in a socket receive buffer, sbappendrights()
467df8bae1dSRodney W. Grimes  * should be used.  In either case, the new data begins a new record.
468df8bae1dSRodney W. Grimes  * Note that unlike sbappend() and sbappendrecord(), these routines check
469df8bae1dSRodney W. Grimes  * for the caller that there will be enough space to store the data.
470df8bae1dSRodney W. Grimes  * Each fails if there is not enough space, or if it cannot find mbufs
471df8bae1dSRodney W. Grimes  * to store additional information in.
472df8bae1dSRodney W. Grimes  *
473df8bae1dSRodney W. Grimes  * Reliable protocols may use the socket send buffer to hold data
474df8bae1dSRodney W. Grimes  * awaiting acknowledgement.  Data is normally copied from a socket
475df8bae1dSRodney W. Grimes  * send buffer in a protocol with m_copy for output to a peer,
476df8bae1dSRodney W. Grimes  * and then removing the data from the socket buffer with sbdrop()
477df8bae1dSRodney W. Grimes  * or sbdroprecord() when the data is acknowledged by the peer.
478df8bae1dSRodney W. Grimes  */
479df8bae1dSRodney W. Grimes 
480df8bae1dSRodney W. Grimes /*
481df8bae1dSRodney W. Grimes  * Append mbuf chain m to the last record in the
482df8bae1dSRodney W. Grimes  * socket buffer sb.  The additional space associated
483df8bae1dSRodney W. Grimes  * the mbuf chain is recorded in sb.  Empty mbufs are
484df8bae1dSRodney W. Grimes  * discarded and mbufs are compacted where possible.
485df8bae1dSRodney W. Grimes  */
48626f9a767SRodney W. Grimes void
487df8bae1dSRodney W. Grimes sbappend(sb, m)
488df8bae1dSRodney W. Grimes 	struct sockbuf *sb;
489df8bae1dSRodney W. Grimes 	struct mbuf *m;
490df8bae1dSRodney W. Grimes {
491df8bae1dSRodney W. Grimes 	register struct mbuf *n;
492df8bae1dSRodney W. Grimes 
493df8bae1dSRodney W. Grimes 	if (m == 0)
494df8bae1dSRodney W. Grimes 		return;
495797f2d22SPoul-Henning Kamp 	n = sb->sb_mb;
496797f2d22SPoul-Henning Kamp 	if (n) {
497df8bae1dSRodney W. Grimes 		while (n->m_nextpkt)
498df8bae1dSRodney W. Grimes 			n = n->m_nextpkt;
499df8bae1dSRodney W. Grimes 		do {
500df8bae1dSRodney W. Grimes 			if (n->m_flags & M_EOR) {
501df8bae1dSRodney W. Grimes 				sbappendrecord(sb, m); /* XXXXXX!!!! */
502df8bae1dSRodney W. Grimes 				return;
503df8bae1dSRodney W. Grimes 			}
504df8bae1dSRodney W. Grimes 		} while (n->m_next && (n = n->m_next));
505df8bae1dSRodney W. Grimes 	}
506df8bae1dSRodney W. Grimes 	sbcompress(sb, m, n);
507df8bae1dSRodney W. Grimes }
508df8bae1dSRodney W. Grimes 
509df8bae1dSRodney W. Grimes #ifdef SOCKBUF_DEBUG
51026f9a767SRodney W. Grimes void
511df8bae1dSRodney W. Grimes sbcheck(sb)
512df8bae1dSRodney W. Grimes 	register struct sockbuf *sb;
513df8bae1dSRodney W. Grimes {
514df8bae1dSRodney W. Grimes 	register struct mbuf *m;
5150931333fSBill Fenner 	register struct mbuf *n = 0;
5160931333fSBill Fenner 	register u_long len = 0, mbcnt = 0;
517df8bae1dSRodney W. Grimes 
5180931333fSBill Fenner 	for (m = sb->sb_mb; m; m = n) {
5190931333fSBill Fenner 	    n = m->m_nextpkt;
5200931333fSBill Fenner 	    for (; m; m = m->m_next) {
521df8bae1dSRodney W. Grimes 		len += m->m_len;
522df8bae1dSRodney W. Grimes 		mbcnt += MSIZE;
523313861b8SJulian Elischer 		if (m->m_flags & M_EXT) /*XXX*/ /* pretty sure this is bogus */
524df8bae1dSRodney W. Grimes 			mbcnt += m->m_ext.ext_size;
5250931333fSBill Fenner 	    }
526df8bae1dSRodney W. Grimes 	}
527df8bae1dSRodney W. Grimes 	if (len != sb->sb_cc || mbcnt != sb->sb_mbcnt) {
5280931333fSBill Fenner 		printf("cc %ld != %ld || mbcnt %ld != %ld\n", len, sb->sb_cc,
529df8bae1dSRodney W. Grimes 		    mbcnt, sb->sb_mbcnt);
530df8bae1dSRodney W. Grimes 		panic("sbcheck");
531df8bae1dSRodney W. Grimes 	}
532df8bae1dSRodney W. Grimes }
533df8bae1dSRodney W. Grimes #endif
534df8bae1dSRodney W. Grimes 
535df8bae1dSRodney W. Grimes /*
536df8bae1dSRodney W. Grimes  * As above, except the mbuf chain
537df8bae1dSRodney W. Grimes  * begins a new record.
538df8bae1dSRodney W. Grimes  */
53926f9a767SRodney W. Grimes void
540df8bae1dSRodney W. Grimes sbappendrecord(sb, m0)
541df8bae1dSRodney W. Grimes 	register struct sockbuf *sb;
542df8bae1dSRodney W. Grimes 	register struct mbuf *m0;
543df8bae1dSRodney W. Grimes {
544df8bae1dSRodney W. Grimes 	register struct mbuf *m;
545df8bae1dSRodney W. Grimes 
546df8bae1dSRodney W. Grimes 	if (m0 == 0)
547df8bae1dSRodney W. Grimes 		return;
548797f2d22SPoul-Henning Kamp 	m = sb->sb_mb;
549797f2d22SPoul-Henning Kamp 	if (m)
550df8bae1dSRodney W. Grimes 		while (m->m_nextpkt)
551df8bae1dSRodney W. Grimes 			m = m->m_nextpkt;
552df8bae1dSRodney W. Grimes 	/*
553df8bae1dSRodney W. Grimes 	 * Put the first mbuf on the queue.
554df8bae1dSRodney W. Grimes 	 * Note this permits zero length records.
555df8bae1dSRodney W. Grimes 	 */
556df8bae1dSRodney W. Grimes 	sballoc(sb, m0);
557df8bae1dSRodney W. Grimes 	if (m)
558df8bae1dSRodney W. Grimes 		m->m_nextpkt = m0;
559df8bae1dSRodney W. Grimes 	else
560df8bae1dSRodney W. Grimes 		sb->sb_mb = m0;
561df8bae1dSRodney W. Grimes 	m = m0->m_next;
562df8bae1dSRodney W. Grimes 	m0->m_next = 0;
563df8bae1dSRodney W. Grimes 	if (m && (m0->m_flags & M_EOR)) {
564df8bae1dSRodney W. Grimes 		m0->m_flags &= ~M_EOR;
565df8bae1dSRodney W. Grimes 		m->m_flags |= M_EOR;
566df8bae1dSRodney W. Grimes 	}
567df8bae1dSRodney W. Grimes 	sbcompress(sb, m, m0);
568df8bae1dSRodney W. Grimes }
569df8bae1dSRodney W. Grimes 
570df8bae1dSRodney W. Grimes /*
571df8bae1dSRodney W. Grimes  * As above except that OOB data
572df8bae1dSRodney W. Grimes  * is inserted at the beginning of the sockbuf,
573df8bae1dSRodney W. Grimes  * but after any other OOB data.
574df8bae1dSRodney W. Grimes  */
57526f9a767SRodney W. Grimes void
576df8bae1dSRodney W. Grimes sbinsertoob(sb, m0)
577df8bae1dSRodney W. Grimes 	register struct sockbuf *sb;
578df8bae1dSRodney W. Grimes 	register struct mbuf *m0;
579df8bae1dSRodney W. Grimes {
580df8bae1dSRodney W. Grimes 	register struct mbuf *m;
581df8bae1dSRodney W. Grimes 	register struct mbuf **mp;
582df8bae1dSRodney W. Grimes 
583df8bae1dSRodney W. Grimes 	if (m0 == 0)
584df8bae1dSRodney W. Grimes 		return;
585797f2d22SPoul-Henning Kamp 	for (mp = &sb->sb_mb; *mp ; mp = &((*mp)->m_nextpkt)) {
586797f2d22SPoul-Henning Kamp 	    m = *mp;
587df8bae1dSRodney W. Grimes 	    again:
588df8bae1dSRodney W. Grimes 		switch (m->m_type) {
589df8bae1dSRodney W. Grimes 
590df8bae1dSRodney W. Grimes 		case MT_OOBDATA:
591df8bae1dSRodney W. Grimes 			continue;		/* WANT next train */
592df8bae1dSRodney W. Grimes 
593df8bae1dSRodney W. Grimes 		case MT_CONTROL:
594797f2d22SPoul-Henning Kamp 			m = m->m_next;
595797f2d22SPoul-Henning Kamp 			if (m)
596df8bae1dSRodney W. Grimes 				goto again;	/* inspect THIS train further */
597df8bae1dSRodney W. Grimes 		}
598df8bae1dSRodney W. Grimes 		break;
599df8bae1dSRodney W. Grimes 	}
600df8bae1dSRodney W. Grimes 	/*
601df8bae1dSRodney W. Grimes 	 * Put the first mbuf on the queue.
602df8bae1dSRodney W. Grimes 	 * Note this permits zero length records.
603df8bae1dSRodney W. Grimes 	 */
604df8bae1dSRodney W. Grimes 	sballoc(sb, m0);
605df8bae1dSRodney W. Grimes 	m0->m_nextpkt = *mp;
606df8bae1dSRodney W. Grimes 	*mp = m0;
607df8bae1dSRodney W. Grimes 	m = m0->m_next;
608df8bae1dSRodney W. Grimes 	m0->m_next = 0;
609df8bae1dSRodney W. Grimes 	if (m && (m0->m_flags & M_EOR)) {
610df8bae1dSRodney W. Grimes 		m0->m_flags &= ~M_EOR;
611df8bae1dSRodney W. Grimes 		m->m_flags |= M_EOR;
612df8bae1dSRodney W. Grimes 	}
613df8bae1dSRodney W. Grimes 	sbcompress(sb, m, m0);
614df8bae1dSRodney W. Grimes }
615df8bae1dSRodney W. Grimes 
616df8bae1dSRodney W. Grimes /*
617df8bae1dSRodney W. Grimes  * Append address and data, and optionally, control (ancillary) data
618df8bae1dSRodney W. Grimes  * to the receive queue of a socket.  If present,
619df8bae1dSRodney W. Grimes  * m0 must include a packet header with total length.
620df8bae1dSRodney W. Grimes  * Returns 0 if no space in sockbuf or insufficient mbufs.
621df8bae1dSRodney W. Grimes  */
62226f9a767SRodney W. Grimes int
623df8bae1dSRodney W. Grimes sbappendaddr(sb, asa, m0, control)
624df8bae1dSRodney W. Grimes 	register struct sockbuf *sb;
625df8bae1dSRodney W. Grimes 	struct sockaddr *asa;
626df8bae1dSRodney W. Grimes 	struct mbuf *m0, *control;
627df8bae1dSRodney W. Grimes {
628df8bae1dSRodney W. Grimes 	register struct mbuf *m, *n;
629df8bae1dSRodney W. Grimes 	int space = asa->sa_len;
630df8bae1dSRodney W. Grimes 
631df8bae1dSRodney W. Grimes if (m0 && (m0->m_flags & M_PKTHDR) == 0)
632df8bae1dSRodney W. Grimes panic("sbappendaddr");
633df8bae1dSRodney W. Grimes 	if (m0)
634df8bae1dSRodney W. Grimes 		space += m0->m_pkthdr.len;
635df8bae1dSRodney W. Grimes 	for (n = control; n; n = n->m_next) {
636df8bae1dSRodney W. Grimes 		space += n->m_len;
637df8bae1dSRodney W. Grimes 		if (n->m_next == 0)	/* keep pointer to last control buf */
638df8bae1dSRodney W. Grimes 			break;
639df8bae1dSRodney W. Grimes 	}
640df8bae1dSRodney W. Grimes 	if (space > sbspace(sb))
641df8bae1dSRodney W. Grimes 		return (0);
642df8bae1dSRodney W. Grimes 	if (asa->sa_len > MLEN)
643df8bae1dSRodney W. Grimes 		return (0);
644df8bae1dSRodney W. Grimes 	MGET(m, M_DONTWAIT, MT_SONAME);
645df8bae1dSRodney W. Grimes 	if (m == 0)
646df8bae1dSRodney W. Grimes 		return (0);
647df8bae1dSRodney W. Grimes 	m->m_len = asa->sa_len;
648df8bae1dSRodney W. Grimes 	bcopy((caddr_t)asa, mtod(m, caddr_t), asa->sa_len);
649df8bae1dSRodney W. Grimes 	if (n)
650df8bae1dSRodney W. Grimes 		n->m_next = m0;		/* concatenate data to control */
651df8bae1dSRodney W. Grimes 	else
652df8bae1dSRodney W. Grimes 		control = m0;
653df8bae1dSRodney W. Grimes 	m->m_next = control;
654df8bae1dSRodney W. Grimes 	for (n = m; n; n = n->m_next)
655df8bae1dSRodney W. Grimes 		sballoc(sb, n);
656797f2d22SPoul-Henning Kamp 	n = sb->sb_mb;
657797f2d22SPoul-Henning Kamp 	if (n) {
658df8bae1dSRodney W. Grimes 		while (n->m_nextpkt)
659df8bae1dSRodney W. Grimes 			n = n->m_nextpkt;
660df8bae1dSRodney W. Grimes 		n->m_nextpkt = m;
661df8bae1dSRodney W. Grimes 	} else
662df8bae1dSRodney W. Grimes 		sb->sb_mb = m;
663df8bae1dSRodney W. Grimes 	return (1);
664df8bae1dSRodney W. Grimes }
665df8bae1dSRodney W. Grimes 
66626f9a767SRodney W. Grimes int
667df8bae1dSRodney W. Grimes sbappendcontrol(sb, m0, control)
668df8bae1dSRodney W. Grimes 	struct sockbuf *sb;
669df8bae1dSRodney W. Grimes 	struct mbuf *control, *m0;
670df8bae1dSRodney W. Grimes {
671df8bae1dSRodney W. Grimes 	register struct mbuf *m, *n;
672df8bae1dSRodney W. Grimes 	int space = 0;
673df8bae1dSRodney W. Grimes 
674df8bae1dSRodney W. Grimes 	if (control == 0)
675df8bae1dSRodney W. Grimes 		panic("sbappendcontrol");
676df8bae1dSRodney W. Grimes 	for (m = control; ; m = m->m_next) {
677df8bae1dSRodney W. Grimes 		space += m->m_len;
678df8bae1dSRodney W. Grimes 		if (m->m_next == 0)
679df8bae1dSRodney W. Grimes 			break;
680df8bae1dSRodney W. Grimes 	}
681df8bae1dSRodney W. Grimes 	n = m;			/* save pointer to last control buffer */
682df8bae1dSRodney W. Grimes 	for (m = m0; m; m = m->m_next)
683df8bae1dSRodney W. Grimes 		space += m->m_len;
684df8bae1dSRodney W. Grimes 	if (space > sbspace(sb))
685df8bae1dSRodney W. Grimes 		return (0);
686df8bae1dSRodney W. Grimes 	n->m_next = m0;			/* concatenate data to control */
687df8bae1dSRodney W. Grimes 	for (m = control; m; m = m->m_next)
688df8bae1dSRodney W. Grimes 		sballoc(sb, m);
689797f2d22SPoul-Henning Kamp 	n = sb->sb_mb;
690797f2d22SPoul-Henning Kamp 	if (n) {
691df8bae1dSRodney W. Grimes 		while (n->m_nextpkt)
692df8bae1dSRodney W. Grimes 			n = n->m_nextpkt;
693df8bae1dSRodney W. Grimes 		n->m_nextpkt = control;
694df8bae1dSRodney W. Grimes 	} else
695df8bae1dSRodney W. Grimes 		sb->sb_mb = control;
696df8bae1dSRodney W. Grimes 	return (1);
697df8bae1dSRodney W. Grimes }
698df8bae1dSRodney W. Grimes 
699df8bae1dSRodney W. Grimes /*
700df8bae1dSRodney W. Grimes  * Compress mbuf chain m into the socket
701df8bae1dSRodney W. Grimes  * buffer sb following mbuf n.  If n
702df8bae1dSRodney W. Grimes  * is null, the buffer is presumed empty.
703df8bae1dSRodney W. Grimes  */
70426f9a767SRodney W. Grimes void
705df8bae1dSRodney W. Grimes sbcompress(sb, m, n)
706df8bae1dSRodney W. Grimes 	register struct sockbuf *sb;
707df8bae1dSRodney W. Grimes 	register struct mbuf *m, *n;
708df8bae1dSRodney W. Grimes {
709df8bae1dSRodney W. Grimes 	register int eor = 0;
710df8bae1dSRodney W. Grimes 	register struct mbuf *o;
711df8bae1dSRodney W. Grimes 
712df8bae1dSRodney W. Grimes 	while (m) {
713df8bae1dSRodney W. Grimes 		eor |= m->m_flags & M_EOR;
714df8bae1dSRodney W. Grimes 		if (m->m_len == 0 &&
715df8bae1dSRodney W. Grimes 		    (eor == 0 ||
716df8bae1dSRodney W. Grimes 		     (((o = m->m_next) || (o = n)) &&
717df8bae1dSRodney W. Grimes 		      o->m_type == m->m_type))) {
718df8bae1dSRodney W. Grimes 			m = m_free(m);
719df8bae1dSRodney W. Grimes 			continue;
720df8bae1dSRodney W. Grimes 		}
72132af0d74SDavid Malone 		if (n && (n->m_flags & M_EOR) == 0 &&
72232af0d74SDavid Malone 		    M_WRITABLE(n) &&
72332af0d74SDavid Malone 		    m->m_len <= MCLBYTES / 4 && /* XXX: Don't copy too much */
72432af0d74SDavid Malone 		    m->m_len <= M_TRAILINGSPACE(n) &&
725df8bae1dSRodney W. Grimes 		    n->m_type == m->m_type) {
726df8bae1dSRodney W. Grimes 			bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len,
727df8bae1dSRodney W. Grimes 			    (unsigned)m->m_len);
728df8bae1dSRodney W. Grimes 			n->m_len += m->m_len;
729df8bae1dSRodney W. Grimes 			sb->sb_cc += m->m_len;
730df8bae1dSRodney W. Grimes 			m = m_free(m);
731df8bae1dSRodney W. Grimes 			continue;
732df8bae1dSRodney W. Grimes 		}
733df8bae1dSRodney W. Grimes 		if (n)
734df8bae1dSRodney W. Grimes 			n->m_next = m;
735df8bae1dSRodney W. Grimes 		else
736df8bae1dSRodney W. Grimes 			sb->sb_mb = m;
737df8bae1dSRodney W. Grimes 		sballoc(sb, m);
738df8bae1dSRodney W. Grimes 		n = m;
739df8bae1dSRodney W. Grimes 		m->m_flags &= ~M_EOR;
740df8bae1dSRodney W. Grimes 		m = m->m_next;
741df8bae1dSRodney W. Grimes 		n->m_next = 0;
742df8bae1dSRodney W. Grimes 	}
743df8bae1dSRodney W. Grimes 	if (eor) {
744df8bae1dSRodney W. Grimes 		if (n)
745df8bae1dSRodney W. Grimes 			n->m_flags |= eor;
746df8bae1dSRodney W. Grimes 		else
747df8bae1dSRodney W. Grimes 			printf("semi-panic: sbcompress\n");
748df8bae1dSRodney W. Grimes 	}
749df8bae1dSRodney W. Grimes }
750df8bae1dSRodney W. Grimes 
751df8bae1dSRodney W. Grimes /*
752df8bae1dSRodney W. Grimes  * Free all mbufs in a sockbuf.
753df8bae1dSRodney W. Grimes  * Check that all resources are reclaimed.
754df8bae1dSRodney W. Grimes  */
75526f9a767SRodney W. Grimes void
756df8bae1dSRodney W. Grimes sbflush(sb)
757df8bae1dSRodney W. Grimes 	register struct sockbuf *sb;
758df8bae1dSRodney W. Grimes {
759df8bae1dSRodney W. Grimes 
760df8bae1dSRodney W. Grimes 	if (sb->sb_flags & SB_LOCK)
761253ab668SAndrey A. Chernov 		panic("sbflush: locked");
76223f84772SPierre Beyssac 	while (sb->sb_mbcnt) {
76323f84772SPierre Beyssac 		/*
76423f84772SPierre Beyssac 		 * Don't call sbdrop(sb, 0) if the leading mbuf is non-empty:
76523f84772SPierre Beyssac 		 * we would loop forever. Panic instead.
76623f84772SPierre Beyssac 		 */
76723f84772SPierre Beyssac 		if (!sb->sb_cc && (sb->sb_mb == NULL || sb->sb_mb->m_len))
76823f84772SPierre Beyssac 			break;
769df8bae1dSRodney W. Grimes 		sbdrop(sb, (int)sb->sb_cc);
77023f84772SPierre Beyssac 	}
7710931333fSBill Fenner 	if (sb->sb_cc || sb->sb_mb || sb->sb_mbcnt)
7720931333fSBill Fenner 		panic("sbflush: cc %ld || mb %p || mbcnt %ld", sb->sb_cc, (void *)sb->sb_mb, sb->sb_mbcnt);
773df8bae1dSRodney W. Grimes }
774df8bae1dSRodney W. Grimes 
775df8bae1dSRodney W. Grimes /*
776df8bae1dSRodney W. Grimes  * Drop data from (the front of) a sockbuf.
777df8bae1dSRodney W. Grimes  */
77826f9a767SRodney W. Grimes void
779df8bae1dSRodney W. Grimes sbdrop(sb, len)
780df8bae1dSRodney W. Grimes 	register struct sockbuf *sb;
781df8bae1dSRodney W. Grimes 	register int len;
782df8bae1dSRodney W. Grimes {
783df8bae1dSRodney W. Grimes 	register struct mbuf *m, *mn;
784df8bae1dSRodney W. Grimes 	struct mbuf *next;
785df8bae1dSRodney W. Grimes 
786df8bae1dSRodney W. Grimes 	next = (m = sb->sb_mb) ? m->m_nextpkt : 0;
787df8bae1dSRodney W. Grimes 	while (len > 0) {
788df8bae1dSRodney W. Grimes 		if (m == 0) {
789df8bae1dSRodney W. Grimes 			if (next == 0)
790df8bae1dSRodney W. Grimes 				panic("sbdrop");
791df8bae1dSRodney W. Grimes 			m = next;
792df8bae1dSRodney W. Grimes 			next = m->m_nextpkt;
793df8bae1dSRodney W. Grimes 			continue;
794df8bae1dSRodney W. Grimes 		}
795df8bae1dSRodney W. Grimes 		if (m->m_len > len) {
796df8bae1dSRodney W. Grimes 			m->m_len -= len;
797df8bae1dSRodney W. Grimes 			m->m_data += len;
798df8bae1dSRodney W. Grimes 			sb->sb_cc -= len;
799df8bae1dSRodney W. Grimes 			break;
800df8bae1dSRodney W. Grimes 		}
801df8bae1dSRodney W. Grimes 		len -= m->m_len;
802df8bae1dSRodney W. Grimes 		sbfree(sb, m);
803df8bae1dSRodney W. Grimes 		MFREE(m, mn);
804df8bae1dSRodney W. Grimes 		m = mn;
805df8bae1dSRodney W. Grimes 	}
806df8bae1dSRodney W. Grimes 	while (m && m->m_len == 0) {
807df8bae1dSRodney W. Grimes 		sbfree(sb, m);
808df8bae1dSRodney W. Grimes 		MFREE(m, mn);
809df8bae1dSRodney W. Grimes 		m = mn;
810df8bae1dSRodney W. Grimes 	}
811df8bae1dSRodney W. Grimes 	if (m) {
812df8bae1dSRodney W. Grimes 		sb->sb_mb = m;
813df8bae1dSRodney W. Grimes 		m->m_nextpkt = next;
814df8bae1dSRodney W. Grimes 	} else
815df8bae1dSRodney W. Grimes 		sb->sb_mb = next;
816df8bae1dSRodney W. Grimes }
817df8bae1dSRodney W. Grimes 
818df8bae1dSRodney W. Grimes /*
819df8bae1dSRodney W. Grimes  * Drop a record off the front of a sockbuf
820df8bae1dSRodney W. Grimes  * and move the next record to the front.
821df8bae1dSRodney W. Grimes  */
82226f9a767SRodney W. Grimes void
823df8bae1dSRodney W. Grimes sbdroprecord(sb)
824df8bae1dSRodney W. Grimes 	register struct sockbuf *sb;
825df8bae1dSRodney W. Grimes {
826df8bae1dSRodney W. Grimes 	register struct mbuf *m, *mn;
827df8bae1dSRodney W. Grimes 
828df8bae1dSRodney W. Grimes 	m = sb->sb_mb;
829df8bae1dSRodney W. Grimes 	if (m) {
830df8bae1dSRodney W. Grimes 		sb->sb_mb = m->m_nextpkt;
831df8bae1dSRodney W. Grimes 		do {
832df8bae1dSRodney W. Grimes 			sbfree(sb, m);
833df8bae1dSRodney W. Grimes 			MFREE(m, mn);
834797f2d22SPoul-Henning Kamp 			m = mn;
835797f2d22SPoul-Henning Kamp 		} while (m);
836df8bae1dSRodney W. Grimes 	}
837df8bae1dSRodney W. Grimes }
8381e4ad9ceSGarrett Wollman 
83982c23ebaSBill Fenner /*
84082c23ebaSBill Fenner  * Create a "control" mbuf containing the specified data
84182c23ebaSBill Fenner  * with the specified type for presentation on a socket buffer.
84282c23ebaSBill Fenner  */
84382c23ebaSBill Fenner struct mbuf *
84482c23ebaSBill Fenner sbcreatecontrol(p, size, type, level)
84582c23ebaSBill Fenner 	caddr_t p;
84682c23ebaSBill Fenner 	register int size;
84782c23ebaSBill Fenner 	int type, level;
84882c23ebaSBill Fenner {
84982c23ebaSBill Fenner 	register struct cmsghdr *cp;
85082c23ebaSBill Fenner 	struct mbuf *m;
85182c23ebaSBill Fenner 
8527d0d8dc3SYoshinobu Inoue 	if (CMSG_SPACE((u_int)size) > MLEN)
8530b97e97cSYoshinobu Inoue 		return ((struct mbuf *) NULL);
85482c23ebaSBill Fenner 	if ((m = m_get(M_DONTWAIT, MT_CONTROL)) == NULL)
85582c23ebaSBill Fenner 		return ((struct mbuf *) NULL);
85682c23ebaSBill Fenner 	cp = mtod(m, struct cmsghdr *);
85782c23ebaSBill Fenner 	/* XXX check size? */
85882c23ebaSBill Fenner 	(void)memcpy(CMSG_DATA(cp), p, size);
8597d0d8dc3SYoshinobu Inoue 	m->m_len = CMSG_SPACE(size);
8607d0d8dc3SYoshinobu Inoue 	cp->cmsg_len = CMSG_LEN(size);
86182c23ebaSBill Fenner 	cp->cmsg_level = level;
86282c23ebaSBill Fenner 	cp->cmsg_type = type;
86382c23ebaSBill Fenner 	return (m);
86482c23ebaSBill Fenner }
86582c23ebaSBill Fenner 
8661e4ad9ceSGarrett Wollman /*
8672c37256eSGarrett Wollman  * Some routines that return EOPNOTSUPP for entry points that are not
8682c37256eSGarrett Wollman  * supported by a protocol.  Fill in as needed.
8691e4ad9ceSGarrett Wollman  */
8701e4ad9ceSGarrett Wollman int
87157bf258eSGarrett Wollman pru_accept_notsupp(struct socket *so, struct sockaddr **nam)
872d8392c6cSGarrett Wollman {
873d8392c6cSGarrett Wollman 	return EOPNOTSUPP;
874d8392c6cSGarrett Wollman }
875d8392c6cSGarrett Wollman 
876d8392c6cSGarrett Wollman int
87757bf258eSGarrett Wollman pru_connect_notsupp(struct socket *so, struct sockaddr *nam, struct proc *p)
8789f907986SPeter Wemm {
8799f907986SPeter Wemm 	return EOPNOTSUPP;
8809f907986SPeter Wemm }
8819f907986SPeter Wemm 
8829f907986SPeter Wemm int
8832c37256eSGarrett Wollman pru_connect2_notsupp(struct socket *so1, struct socket *so2)
8841e4ad9ceSGarrett Wollman {
8852c37256eSGarrett Wollman 	return EOPNOTSUPP;
8861e4ad9ceSGarrett Wollman }
887d8392c6cSGarrett Wollman 
888d8392c6cSGarrett Wollman int
889ecbb00a2SDoug Rabson pru_control_notsupp(struct socket *so, u_long cmd, caddr_t data,
890a29f300eSGarrett Wollman 		    struct ifnet *ifp, struct proc *p)
891a29f300eSGarrett Wollman {
892a29f300eSGarrett Wollman 	return EOPNOTSUPP;
893a29f300eSGarrett Wollman }
894a29f300eSGarrett Wollman 
895a29f300eSGarrett Wollman int
896a29f300eSGarrett Wollman pru_listen_notsupp(struct socket *so, struct proc *p)
897d8392c6cSGarrett Wollman {
898d8392c6cSGarrett Wollman 	return EOPNOTSUPP;
899d8392c6cSGarrett Wollman }
900d8392c6cSGarrett Wollman 
901d8392c6cSGarrett Wollman int
902d8392c6cSGarrett Wollman pru_rcvd_notsupp(struct socket *so, int flags)
903d8392c6cSGarrett Wollman {
904d8392c6cSGarrett Wollman 	return EOPNOTSUPP;
905d8392c6cSGarrett Wollman }
906d8392c6cSGarrett Wollman 
907d8392c6cSGarrett Wollman int
908d8392c6cSGarrett Wollman pru_rcvoob_notsupp(struct socket *so, struct mbuf *m, int flags)
909d8392c6cSGarrett Wollman {
910d8392c6cSGarrett Wollman 	return EOPNOTSUPP;
911d8392c6cSGarrett Wollman }
912d8392c6cSGarrett Wollman 
913d8392c6cSGarrett Wollman /*
914d8392c6cSGarrett Wollman  * This isn't really a ``null'' operation, but it's the default one
915d8392c6cSGarrett Wollman  * and doesn't do anything destructive.
916d8392c6cSGarrett Wollman  */
917d8392c6cSGarrett Wollman int
918d8392c6cSGarrett Wollman pru_sense_null(struct socket *so, struct stat *sb)
919d8392c6cSGarrett Wollman {
920d8392c6cSGarrett Wollman 	sb->st_blksize = so->so_snd.sb_hiwat;
921d8392c6cSGarrett Wollman 	return 0;
922d8392c6cSGarrett Wollman }
923639acc13SGarrett Wollman 
924639acc13SGarrett Wollman /*
92557bf258eSGarrett Wollman  * Make a copy of a sockaddr in a malloced buffer of type M_SONAME.
92657bf258eSGarrett Wollman  */
92757bf258eSGarrett Wollman struct sockaddr *
92857bf258eSGarrett Wollman dup_sockaddr(sa, canwait)
92957bf258eSGarrett Wollman 	struct sockaddr *sa;
93057bf258eSGarrett Wollman 	int canwait;
93157bf258eSGarrett Wollman {
93257bf258eSGarrett Wollman 	struct sockaddr *sa2;
93357bf258eSGarrett Wollman 
93457bf258eSGarrett Wollman 	MALLOC(sa2, struct sockaddr *, sa->sa_len, M_SONAME,
93557bf258eSGarrett Wollman 	       canwait ? M_WAITOK : M_NOWAIT);
93657bf258eSGarrett Wollman 	if (sa2)
93757bf258eSGarrett Wollman 		bcopy(sa, sa2, sa->sa_len);
93857bf258eSGarrett Wollman 	return sa2;
93957bf258eSGarrett Wollman }
94057bf258eSGarrett Wollman 
94157bf258eSGarrett Wollman /*
94298271db4SGarrett Wollman  * Create an external-format (``xsocket'') structure using the information
94398271db4SGarrett Wollman  * in the kernel-format socket structure pointed to by so.  This is done
94498271db4SGarrett Wollman  * to reduce the spew of irrelevant information over this interface,
94598271db4SGarrett Wollman  * to isolate user code from changes in the kernel structure, and
94698271db4SGarrett Wollman  * potentially to provide information-hiding if we decide that
94798271db4SGarrett Wollman  * some of this information should be hidden from users.
94898271db4SGarrett Wollman  */
94998271db4SGarrett Wollman void
95098271db4SGarrett Wollman sotoxsocket(struct socket *so, struct xsocket *xso)
95198271db4SGarrett Wollman {
95298271db4SGarrett Wollman 	xso->xso_len = sizeof *xso;
95398271db4SGarrett Wollman 	xso->xso_so = so;
95498271db4SGarrett Wollman 	xso->so_type = so->so_type;
95598271db4SGarrett Wollman 	xso->so_options = so->so_options;
95698271db4SGarrett Wollman 	xso->so_linger = so->so_linger;
95798271db4SGarrett Wollman 	xso->so_state = so->so_state;
95898271db4SGarrett Wollman 	xso->so_pcb = so->so_pcb;
95998271db4SGarrett Wollman 	xso->xso_protocol = so->so_proto->pr_protocol;
96098271db4SGarrett Wollman 	xso->xso_family = so->so_proto->pr_domain->dom_family;
96198271db4SGarrett Wollman 	xso->so_qlen = so->so_qlen;
96298271db4SGarrett Wollman 	xso->so_incqlen = so->so_incqlen;
96398271db4SGarrett Wollman 	xso->so_qlimit = so->so_qlimit;
96498271db4SGarrett Wollman 	xso->so_timeo = so->so_timeo;
96598271db4SGarrett Wollman 	xso->so_error = so->so_error;
966831d27a9SDon Lewis 	xso->so_pgid = so->so_sigio ? so->so_sigio->sio_pgid : 0;
96798271db4SGarrett Wollman 	xso->so_oobmark = so->so_oobmark;
96898271db4SGarrett Wollman 	sbtoxsockbuf(&so->so_snd, &xso->so_snd);
96998271db4SGarrett Wollman 	sbtoxsockbuf(&so->so_rcv, &xso->so_rcv);
9702f9a2132SBrian Feldman 	xso->so_uid = so->so_cred->cr_uid;
97198271db4SGarrett Wollman }
97298271db4SGarrett Wollman 
97398271db4SGarrett Wollman /*
97498271db4SGarrett Wollman  * This does the same for sockbufs.  Note that the xsockbuf structure,
97598271db4SGarrett Wollman  * since it is always embedded in a socket, does not include a self
97698271db4SGarrett Wollman  * pointer nor a length.  We make this entry point public in case
97798271db4SGarrett Wollman  * some other mechanism needs it.
97898271db4SGarrett Wollman  */
97998271db4SGarrett Wollman void
98098271db4SGarrett Wollman sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb)
98198271db4SGarrett Wollman {
98298271db4SGarrett Wollman 	xsb->sb_cc = sb->sb_cc;
98398271db4SGarrett Wollman 	xsb->sb_hiwat = sb->sb_hiwat;
98498271db4SGarrett Wollman 	xsb->sb_mbcnt = sb->sb_mbcnt;
98598271db4SGarrett Wollman 	xsb->sb_mbmax = sb->sb_mbmax;
98698271db4SGarrett Wollman 	xsb->sb_lowat = sb->sb_lowat;
98798271db4SGarrett Wollman 	xsb->sb_flags = sb->sb_flags;
98898271db4SGarrett Wollman 	xsb->sb_timeo = sb->sb_timeo;
98998271db4SGarrett Wollman }
99098271db4SGarrett Wollman 
99198271db4SGarrett Wollman /*
992639acc13SGarrett Wollman  * Here is the definition of some of the basic objects in the kern.ipc
993639acc13SGarrett Wollman  * branch of the MIB.
994639acc13SGarrett Wollman  */
995639acc13SGarrett Wollman SYSCTL_NODE(_kern, KERN_IPC, ipc, CTLFLAG_RW, 0, "IPC");
996639acc13SGarrett Wollman 
997639acc13SGarrett Wollman /* This takes the place of kern.maxsockbuf, which moved to kern.ipc. */
998639acc13SGarrett Wollman static int dummy;
999639acc13SGarrett Wollman SYSCTL_INT(_kern, KERN_DUMMY, dummy, CTLFLAG_RW, &dummy, 0, "");
1000639acc13SGarrett Wollman 
10013d177f46SBill Fumerola SYSCTL_INT(_kern_ipc, KIPC_MAXSOCKBUF, maxsockbuf, CTLFLAG_RW,
10023d177f46SBill Fumerola     &sb_max, 0, "Maximum socket buffer size");
10033d177f46SBill Fumerola SYSCTL_INT(_kern_ipc, OID_AUTO, maxsockets, CTLFLAG_RD,
10043d177f46SBill Fumerola     &maxsockets, 0, "Maximum number of sockets avaliable");
1005639acc13SGarrett Wollman SYSCTL_INT(_kern_ipc, KIPC_SOCKBUF_WASTE, sockbuf_waste_factor, CTLFLAG_RW,
1006639acc13SGarrett Wollman     &sb_efficiency, 0, "");
100757bf258eSGarrett Wollman 
1008134c934cSMike Smith /*
1009134c934cSMike Smith  * Initialise maxsockets
1010134c934cSMike Smith  */
1011134c934cSMike Smith static void init_maxsockets(void *ignored)
1012134c934cSMike Smith {
1013134c934cSMike Smith     TUNABLE_INT_FETCH("kern.ipc.maxsockets", 0, maxsockets);
1014134c934cSMike Smith     maxsockets = imax(maxsockets, imax(maxfiles, nmbclusters));
1015134c934cSMike Smith }
1016134c934cSMike Smith SYSINIT(param, SI_SUB_TUNABLES, SI_ORDER_ANY, init_maxsockets, NULL);
1017