xref: /freebsd/sys/kern/uipc_sockbuf.c (revision 31f555a1c593bf83c273033527ed5fe5aec02d28)
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  * 4. Neither the name of the University nor the names of its contributors
14df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
15df8bae1dSRodney W. Grimes  *    without specific prior written permission.
16df8bae1dSRodney W. Grimes  *
17df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
28df8bae1dSRodney W. Grimes  *
29df8bae1dSRodney W. Grimes  *	@(#)uipc_socket2.c	8.1 (Berkeley) 6/10/93
30df8bae1dSRodney W. Grimes  */
31df8bae1dSRodney W. Grimes 
32677b542eSDavid E. O'Brien #include <sys/cdefs.h>
33677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
34677b542eSDavid E. O'Brien 
35335654d7SRobert Watson #include "opt_mac.h"
365b86eac4SJesper Skriver #include "opt_param.h"
37335654d7SRobert Watson 
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>
45335654d7SRobert Watson #include <sys/mac.h>
46f9d0d524SRobert Watson #include <sys/malloc.h>
47df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
48960ed29cSSeigo Tanimura #include <sys/mutex.h>
49fb919e4dSMark Murray #include <sys/proc.h>
50df8bae1dSRodney W. Grimes #include <sys/protosw.h>
512f9a2132SBrian Feldman #include <sys/resourcevar.h>
52960ed29cSSeigo Tanimura #include <sys/signalvar.h>
53df8bae1dSRodney W. Grimes #include <sys/socket.h>
54df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
55960ed29cSSeigo Tanimura #include <sys/stat.h>
56ff5c09daSGarrett Wollman #include <sys/sysctl.h>
57960ed29cSSeigo Tanimura #include <sys/systm.h>
5826f9a767SRodney W. Grimes 
5954d77689SJeff Roberson int	maxsockets;
6054d77689SJeff Roberson 
6121d56e9cSAlfred Perlstein void (*aio_swake)(struct socket *, struct sockbuf *);
6221d56e9cSAlfred Perlstein 
63df8bae1dSRodney W. Grimes /*
64df8bae1dSRodney W. Grimes  * Primitive routines for operating on sockets and socket buffers
65df8bae1dSRodney W. Grimes  */
66df8bae1dSRodney W. Grimes 
6779cb7eb4SDavid Greenman u_long	sb_max = SB_MAX;
6851da11a2SMark Murray static	u_long sb_max_adj =
6979cb7eb4SDavid Greenman     SB_MAX * MCLBYTES / (MSIZE + MCLBYTES); /* adjusted sb_max */
70df8bae1dSRodney W. Grimes 
714b29bc4fSGarrett Wollman static	u_long sb_efficiency = 8;	/* parameter for sbreserve() */
724b29bc4fSGarrett Wollman 
73df8bae1dSRodney W. Grimes /*
74df8bae1dSRodney W. Grimes  * Procedures to manipulate state flags of socket
75df8bae1dSRodney W. Grimes  * and do appropriate wakeups.  Normal sequence from the
76df8bae1dSRodney W. Grimes  * active (originating) side is that soisconnecting() is
77df8bae1dSRodney W. Grimes  * called during processing of connect() call,
78df8bae1dSRodney W. Grimes  * resulting in an eventual call to soisconnected() if/when the
79df8bae1dSRodney W. Grimes  * connection is established.  When the connection is torn down
80df8bae1dSRodney W. Grimes  * soisdisconnecting() is called during processing of disconnect() call,
81df8bae1dSRodney W. Grimes  * and soisdisconnected() is called when the connection to the peer
82df8bae1dSRodney W. Grimes  * is totally severed.  The semantics of these routines are such that
83df8bae1dSRodney W. Grimes  * connectionless protocols can call soisconnected() and soisdisconnected()
84df8bae1dSRodney W. Grimes  * only, bypassing the in-progress calls when setting up a ``connection''
85df8bae1dSRodney W. Grimes  * takes no time.
86df8bae1dSRodney W. Grimes  *
87df8bae1dSRodney W. Grimes  * From the passive side, a socket is created with
88dc97381eSPeter Wemm  * two queues of sockets: so_incomp for connections in progress
89dc97381eSPeter Wemm  * and so_comp for connections already made and awaiting user acceptance.
90df8bae1dSRodney W. Grimes  * As a protocol is preparing incoming connections, it creates a socket
91dc97381eSPeter Wemm  * structure queued on so_incomp by calling sonewconn().  When the connection
92df8bae1dSRodney W. Grimes  * is established, soisconnected() is called, and transfers the
93dc97381eSPeter Wemm  * socket structure to so_comp, making it available to accept().
94df8bae1dSRodney W. Grimes  *
95df8bae1dSRodney W. Grimes  * If a socket is closed with sockets on either
96dc97381eSPeter Wemm  * so_incomp or so_comp, these sockets are dropped.
97df8bae1dSRodney W. Grimes  *
98df8bae1dSRodney W. Grimes  * If higher level protocols are implemented in
99df8bae1dSRodney W. Grimes  * the kernel, the wakeups done here will sometimes
100df8bae1dSRodney W. Grimes  * cause software-interrupt process scheduling.
101df8bae1dSRodney W. Grimes  */
102df8bae1dSRodney W. Grimes 
10326f9a767SRodney W. Grimes void
104df8bae1dSRodney W. Grimes soisconnecting(so)
105df8bae1dSRodney W. Grimes 	register struct socket *so;
106df8bae1dSRodney W. Grimes {
107df8bae1dSRodney W. Grimes 
1089535efc0SRobert Watson 	SOCK_LOCK(so);
109df8bae1dSRodney W. Grimes 	so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
110df8bae1dSRodney W. Grimes 	so->so_state |= SS_ISCONNECTING;
1119535efc0SRobert Watson 	SOCK_UNLOCK(so);
112df8bae1dSRodney W. Grimes }
113df8bae1dSRodney W. Grimes 
11426f9a767SRodney W. Grimes void
115d48d4b25SSeigo Tanimura soisconnected(so)
116d48d4b25SSeigo Tanimura 	struct socket *so;
117d48d4b25SSeigo Tanimura {
1182658b3bbSRobert Watson 	struct socket *head;
119d48d4b25SSeigo Tanimura 
1209535efc0SRobert Watson 	SOCK_LOCK(so);
121d48d4b25SSeigo Tanimura 	so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING);
122d48d4b25SSeigo Tanimura 	so->so_state |= SS_ISCONNECTED;
1239535efc0SRobert Watson 	SOCK_UNLOCK(so);
1242658b3bbSRobert Watson 	ACCEPT_LOCK();
1252658b3bbSRobert Watson 	head = so->so_head;
1262658b3bbSRobert Watson 	if (head != NULL && (so->so_qstate & SQ_INCOMP)) {
1272658b3bbSRobert Watson 		if ((so->so_options & SO_ACCEPTFILTER) == 0) {
128be24e9e8SDavid Greenman 			TAILQ_REMOVE(&head->so_incomp, so, so_list);
129a51764a8SPaul Traina 			head->so_incqlen--;
13036568179SRobert Watson 			so->so_qstate &= ~SQ_INCOMP;
131be24e9e8SDavid Greenman 			TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
132e1f1827fSMike Silbersack 			head->so_qlen++;
13336568179SRobert Watson 			so->so_qstate |= SQ_COMP;
1342658b3bbSRobert Watson 			ACCEPT_UNLOCK();
13503e49181SSeigo Tanimura 			sorwakeup(head);
136a91b8721SDavid Greenman 			wakeup_one(&head->so_timeo);
137df8bae1dSRodney W. Grimes 		} else {
1382658b3bbSRobert Watson 			ACCEPT_UNLOCK();
1399535efc0SRobert Watson 			SOCK_LOCK(so);
1402658b3bbSRobert Watson 			so->so_upcall =
1412658b3bbSRobert Watson 			    head->so_accf->so_accept_filter->accf_callback;
1422658b3bbSRobert Watson 			so->so_upcallarg = head->so_accf->so_accept_filter_arg;
1432658b3bbSRobert Watson 			so->so_rcv.sb_flags |= SB_UPCALL;
1442658b3bbSRobert Watson 			so->so_options &= ~SO_ACCEPTFILTER;
1459535efc0SRobert Watson 			SOCK_UNLOCK(so);
1462658b3bbSRobert Watson 			so->so_upcall(so, so->so_upcallarg, M_TRYWAIT);
1472658b3bbSRobert Watson 		}
1482658b3bbSRobert Watson 		return;
1492658b3bbSRobert Watson 	}
1502658b3bbSRobert Watson 	ACCEPT_UNLOCK();
151a91b8721SDavid Greenman 	wakeup(&so->so_timeo);
15203e49181SSeigo Tanimura 	sorwakeup(so);
15303e49181SSeigo Tanimura 	sowwakeup(so);
154df8bae1dSRodney W. Grimes }
155df8bae1dSRodney W. Grimes 
15626f9a767SRodney W. Grimes void
157df8bae1dSRodney W. Grimes soisdisconnecting(so)
158df8bae1dSRodney W. Grimes 	register struct socket *so;
159df8bae1dSRodney W. Grimes {
160df8bae1dSRodney W. Grimes 
1619535efc0SRobert Watson 	/*
1629535efc0SRobert Watson 	 * XXXRW: This code separately acquires SOCK_LOCK(so) and
1639535efc0SRobert Watson 	 * SOCKBUF_LOCK(&so->so_rcv) even though they are the same mutex to
1649535efc0SRobert Watson 	 * avoid introducing the assumption  that they are the same.
1659535efc0SRobert Watson 	 */
1669535efc0SRobert Watson 	SOCK_LOCK(so);
167df8bae1dSRodney W. Grimes 	so->so_state &= ~SS_ISCONNECTING;
168c0b99ffaSRobert Watson 	so->so_state |= SS_ISDISCONNECTING;
1699535efc0SRobert Watson 	SOCK_UNLOCK(so);
1707721f5d7SRobert Watson 	SOCKBUF_LOCK(&so->so_rcv);
171c0b99ffaSRobert Watson 	so->so_rcv.sb_state |= SBS_CANTRCVMORE;
1727721f5d7SRobert Watson 	SOCKBUF_UNLOCK(&so->so_rcv);
1737721f5d7SRobert Watson 	SOCKBUF_LOCK(&so->so_snd);
174c0b99ffaSRobert Watson 	so->so_snd.sb_state |= SBS_CANTSENDMORE;
1757721f5d7SRobert Watson 	SOCKBUF_UNLOCK(&so->so_snd);
17680208239SAlfred Perlstein 	wakeup(&so->so_timeo);
17703e49181SSeigo Tanimura 	sowwakeup(so);
17803e49181SSeigo Tanimura 	sorwakeup(so);
179df8bae1dSRodney W. Grimes }
180df8bae1dSRodney W. Grimes 
18126f9a767SRodney W. Grimes void
182df8bae1dSRodney W. Grimes soisdisconnected(so)
183df8bae1dSRodney W. Grimes 	register struct socket *so;
184df8bae1dSRodney W. Grimes {
185df8bae1dSRodney W. Grimes 
1869535efc0SRobert Watson 	/*
1879535efc0SRobert Watson 	 * XXXRW: This code separately acquires SOCK_LOCK(so) and
1889535efc0SRobert Watson 	 * SOCKBUF_LOCK(&so->so_rcv) even though they are the same mutex to
1899535efc0SRobert Watson 	 * avoid introducing the assumption  that they are the same.
1909535efc0SRobert Watson 	 */
1919535efc0SRobert Watson 	SOCK_LOCK(so);
19203e49181SSeigo Tanimura 	so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
193c0b99ffaSRobert Watson 	so->so_state |= SS_ISDISCONNECTED;
1949535efc0SRobert Watson 	SOCK_UNLOCK(so);
1957721f5d7SRobert Watson 	SOCKBUF_LOCK(&so->so_rcv);
196c0b99ffaSRobert Watson 	so->so_rcv.sb_state |= SBS_CANTRCVMORE;
1977721f5d7SRobert Watson 	SOCKBUF_UNLOCK(&so->so_rcv);
1987721f5d7SRobert Watson 	SOCKBUF_LOCK(&so->so_snd);
199c0b99ffaSRobert Watson 	so->so_snd.sb_state |= SBS_CANTSENDMORE;
2007721f5d7SRobert Watson 	SOCKBUF_UNLOCK(&so->so_snd);
20180208239SAlfred Perlstein 	wakeup(&so->so_timeo);
20225dec747SDavid Malone 	sbdrop(&so->so_snd, so->so_snd.sb_cc);
20303e49181SSeigo Tanimura 	sowwakeup(so);
20403e49181SSeigo Tanimura 	sorwakeup(so);
205df8bae1dSRodney W. Grimes }
206df8bae1dSRodney W. Grimes 
207df8bae1dSRodney W. Grimes /*
208df8bae1dSRodney W. Grimes  * When an attempt at a new connection is noted on a socket
209df8bae1dSRodney W. Grimes  * which accepts connections, sonewconn is called.  If the
210df8bae1dSRodney W. Grimes  * connection is possible (subject to space constraints, etc.)
211df8bae1dSRodney W. Grimes  * then we allocate a new structure, propoerly linked into the
212df8bae1dSRodney W. Grimes  * data structure of the original socket, and return this.
213df8bae1dSRodney W. Grimes  * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED.
214b1e4abd2SMatthew Dillon  *
215b1e4abd2SMatthew Dillon  * note: the ref count on the socket is 0 on return
216df8bae1dSRodney W. Grimes  */
217df8bae1dSRodney W. Grimes struct socket *
218548af278SBill Fenner sonewconn(head, connstatus)
219df8bae1dSRodney W. Grimes 	register struct socket *head;
220df8bae1dSRodney W. Grimes 	int connstatus;
221df8bae1dSRodney W. Grimes {
222df8bae1dSRodney W. Grimes 	register struct socket *so;
2232658b3bbSRobert Watson 	int over;
224df8bae1dSRodney W. Grimes 
2252658b3bbSRobert Watson 	ACCEPT_LOCK();
2262658b3bbSRobert Watson 	over = (head->so_qlen > 3 * head->so_qlimit / 2);
2272658b3bbSRobert Watson 	ACCEPT_UNLOCK();
2282658b3bbSRobert Watson 	if (over)
229df8bae1dSRodney W. Grimes 		return ((struct socket *)0);
2302bc87dcfSRobert Watson 	so = soalloc(M_NOWAIT);
231df8bae1dSRodney W. Grimes 	if (so == NULL)
232df8bae1dSRodney W. Grimes 		return ((struct socket *)0);
233205b2b61SPeter Wemm 	if ((head->so_options & SO_ACCEPTFILTER) != 0)
234205b2b61SPeter Wemm 		connstatus = 0;
235be24e9e8SDavid Greenman 	so->so_head = head;
236df8bae1dSRodney W. Grimes 	so->so_type = head->so_type;
237df8bae1dSRodney W. Grimes 	so->so_options = head->so_options &~ SO_ACCEPTCONN;
238df8bae1dSRodney W. Grimes 	so->so_linger = head->so_linger;
239df8bae1dSRodney W. Grimes 	so->so_state = head->so_state | SS_NOFDREF;
240df8bae1dSRodney W. Grimes 	so->so_proto = head->so_proto;
241df8bae1dSRodney W. Grimes 	so->so_timeo = head->so_timeo;
242bd78ceceSJohn Baldwin 	so->so_cred = crhold(head->so_cred);
243335654d7SRobert Watson #ifdef MAC
244310e7cebSRobert Watson 	SOCK_LOCK(head);
245335654d7SRobert Watson 	mac_create_socket_from_socket(head, so);
246310e7cebSRobert Watson 	SOCK_UNLOCK(head);
247335654d7SRobert Watson #endif
2482f9a2132SBrian Feldman 	if (soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat) ||
2492f9a2132SBrian Feldman 	    (*so->so_proto->pr_usrreqs->pru_attach)(so, 0, NULL)) {
2505ee0a409SAlan Cox 		sodealloc(so);
251b1396a35SGarrett Wollman 		return ((struct socket *)0);
252b1396a35SGarrett Wollman 	}
2532658b3bbSRobert Watson 	ACCEPT_LOCK();
254be24e9e8SDavid Greenman 	if (connstatus) {
255be24e9e8SDavid Greenman 		TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
25636568179SRobert Watson 		so->so_qstate |= SQ_COMP;
257e1f1827fSMike Silbersack 		head->so_qlen++;
258be24e9e8SDavid Greenman 	} else {
2592658b3bbSRobert Watson 		/*
2602658b3bbSRobert Watson 		 * XXXRW: Keep removing sockets from the head until there's
2612658b3bbSRobert Watson 		 * room for us to insert on the tail.  In pre-locking
2622658b3bbSRobert Watson 		 * revisions, this was a simple if(), but as we could be
2632658b3bbSRobert Watson 		 * racing with other threads and soabort() requires dropping
2642658b3bbSRobert Watson 		 * locks, we must loop waiting for the condition to be true.
2652658b3bbSRobert Watson 		 */
2662658b3bbSRobert Watson 		while (head->so_incqlen > head->so_qlimit) {
267e1f1827fSMike Silbersack 			struct socket *sp;
268e1f1827fSMike Silbersack 			sp = TAILQ_FIRST(&head->so_incomp);
2692658b3bbSRobert Watson 			TAILQ_REMOVE(&so->so_incomp, sp, so_list);
2702658b3bbSRobert Watson 			head->so_incqlen--;
2712658b3bbSRobert Watson 			sp->so_qstate &= ~SQ_INCOMP;
2722658b3bbSRobert Watson 			sp->so_head = NULL;
2732658b3bbSRobert Watson 			ACCEPT_UNLOCK();
274e1f1827fSMike Silbersack 			(void) soabort(sp);
2752658b3bbSRobert Watson 			ACCEPT_LOCK();
276e1f1827fSMike Silbersack 		}
277be24e9e8SDavid Greenman 		TAILQ_INSERT_TAIL(&head->so_incomp, so, so_list);
27836568179SRobert Watson 		so->so_qstate |= SQ_INCOMP;
279ebb0cbeaSPaul Traina 		head->so_incqlen++;
280be24e9e8SDavid Greenman 	}
2812658b3bbSRobert Watson 	ACCEPT_UNLOCK();
282df8bae1dSRodney W. Grimes 	if (connstatus) {
2832658b3bbSRobert Watson 		so->so_state |= connstatus;
28403e49181SSeigo Tanimura 		sorwakeup(head);
285c2696aafSPaul Saab 		wakeup_one(&head->so_timeo);
286df8bae1dSRodney W. Grimes 	}
287df8bae1dSRodney W. Grimes 	return (so);
288df8bae1dSRodney W. Grimes }
289df8bae1dSRodney W. Grimes 
290df8bae1dSRodney W. Grimes /*
291df8bae1dSRodney W. Grimes  * Socantsendmore indicates that no more data will be sent on the
292df8bae1dSRodney W. Grimes  * socket; it would normally be applied to a socket when the user
293df8bae1dSRodney W. Grimes  * informs the system that no more data is to be sent, by the protocol
294df8bae1dSRodney W. Grimes  * code (in case PRU_SHUTDOWN).  Socantrcvmore indicates that no more data
295df8bae1dSRodney W. Grimes  * will be received, and will normally be applied to the socket by a
296df8bae1dSRodney W. Grimes  * protocol when it detects that the peer will send no more data.
297df8bae1dSRodney W. Grimes  * Data queued for reading in the socket may yet be read.
298df8bae1dSRodney W. Grimes  */
299df8bae1dSRodney W. Grimes 
30026f9a767SRodney W. Grimes void
301df8bae1dSRodney W. Grimes socantsendmore(so)
302df8bae1dSRodney W. Grimes 	struct socket *so;
303df8bae1dSRodney W. Grimes {
304df8bae1dSRodney W. Grimes 
305c0b99ffaSRobert Watson 	so->so_snd.sb_state |= SBS_CANTSENDMORE;
30603e49181SSeigo Tanimura 	sowwakeup(so);
307df8bae1dSRodney W. Grimes }
308df8bae1dSRodney W. Grimes 
30926f9a767SRodney W. Grimes void
310df8bae1dSRodney W. Grimes socantrcvmore(so)
311df8bae1dSRodney W. Grimes 	struct socket *so;
312df8bae1dSRodney W. Grimes {
313df8bae1dSRodney W. Grimes 
314c0b99ffaSRobert Watson 	so->so_rcv.sb_state |= SBS_CANTRCVMORE;
31503e49181SSeigo Tanimura 	sorwakeup(so);
316df8bae1dSRodney W. Grimes }
317df8bae1dSRodney W. Grimes 
318df8bae1dSRodney W. Grimes /*
319df8bae1dSRodney W. Grimes  * Wait for data to arrive at/drain from a socket buffer.
320df8bae1dSRodney W. Grimes  */
32126f9a767SRodney W. Grimes int
322df8bae1dSRodney W. Grimes sbwait(sb)
323df8bae1dSRodney W. Grimes 	struct sockbuf *sb;
324df8bae1dSRodney W. Grimes {
325df8bae1dSRodney W. Grimes 
32631f555a1SRobert Watson 	SOCKBUF_LOCK_ASSERT(sb);
32731f555a1SRobert Watson 
328df8bae1dSRodney W. Grimes 	sb->sb_flags |= SB_WAIT;
32931f555a1SRobert Watson 	return (msleep(&sb->sb_cc, &sb->sb_mtx,
33047daf5d5SBruce Evans 	    (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH, "sbwait",
331df8bae1dSRodney W. Grimes 	    sb->sb_timeo));
332df8bae1dSRodney W. Grimes }
333df8bae1dSRodney W. Grimes 
334df8bae1dSRodney W. Grimes /*
335df8bae1dSRodney W. Grimes  * Lock a sockbuf already known to be locked;
336df8bae1dSRodney W. Grimes  * return any error returned from sleep (EINTR).
337df8bae1dSRodney W. Grimes  */
33826f9a767SRodney W. Grimes int
339df8bae1dSRodney W. Grimes sb_lock(sb)
340df8bae1dSRodney W. Grimes 	register struct sockbuf *sb;
341df8bae1dSRodney W. Grimes {
342df8bae1dSRodney W. Grimes 	int error;
343df8bae1dSRodney W. Grimes 
34431f555a1SRobert Watson 	SOCKBUF_LOCK_ASSERT(sb);
34531f555a1SRobert Watson 
346df8bae1dSRodney W. Grimes 	while (sb->sb_flags & SB_LOCK) {
347df8bae1dSRodney W. Grimes 		sb->sb_flags |= SB_WANT;
34831f555a1SRobert Watson 		error = msleep(&sb->sb_flags, &sb->sb_mtx,
349df8bae1dSRodney W. Grimes 		    (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK|PCATCH,
35047daf5d5SBruce Evans 		    "sblock", 0);
351797f2d22SPoul-Henning Kamp 		if (error)
352df8bae1dSRodney W. Grimes 			return (error);
353df8bae1dSRodney W. Grimes 	}
354df8bae1dSRodney W. Grimes 	sb->sb_flags |= SB_LOCK;
355df8bae1dSRodney W. Grimes 	return (0);
356df8bae1dSRodney W. Grimes }
357df8bae1dSRodney W. Grimes 
358df8bae1dSRodney W. Grimes /*
359df8bae1dSRodney W. Grimes  * Wakeup processes waiting on a socket buffer.
360df8bae1dSRodney W. Grimes  * Do asynchronous notification via SIGIO
361df8bae1dSRodney W. Grimes  * if the socket has the SS_ASYNC flag set.
362df8bae1dSRodney W. Grimes  */
36326f9a767SRodney W. Grimes void
364df8bae1dSRodney W. Grimes sowakeup(so, sb)
365df8bae1dSRodney W. Grimes 	register struct socket *so;
366df8bae1dSRodney W. Grimes 	register struct sockbuf *sb;
367df8bae1dSRodney W. Grimes {
368d48d4b25SSeigo Tanimura 
369512824f8SSeigo Tanimura 	selwakeuppri(&sb->sb_sel, PSOCK);
370df8bae1dSRodney W. Grimes 	sb->sb_flags &= ~SB_SEL;
371df8bae1dSRodney W. Grimes 	if (sb->sb_flags & SB_WAIT) {
372df8bae1dSRodney W. Grimes 		sb->sb_flags &= ~SB_WAIT;
37380208239SAlfred Perlstein 		wakeup(&sb->sb_cc);
374df8bae1dSRodney W. Grimes 	}
3754cc20ab1SSeigo Tanimura 	if ((so->so_state & SS_ASYNC) && so->so_sigio != NULL)
376f1320723SAlfred Perlstein 		pgsigio(&so->so_sigio, SIGIO, 0);
3774cc20ab1SSeigo Tanimura 	if (sb->sb_flags & SB_UPCALL)
378a163d034SWarner Losh 		(*so->so_upcall)(so, so->so_upcallarg, M_DONTWAIT);
3794cc20ab1SSeigo Tanimura 	if (sb->sb_flags & SB_AIO)
380bfbbc4aaSJason Evans 		aio_swake(so, sb);
381cb679c38SJonathan Lemon 	KNOTE(&sb->sb_sel.si_note, 0);
382df8bae1dSRodney W. Grimes }
383df8bae1dSRodney W. Grimes 
384df8bae1dSRodney W. Grimes /*
385df8bae1dSRodney W. Grimes  * Socket buffer (struct sockbuf) utility routines.
386df8bae1dSRodney W. Grimes  *
387df8bae1dSRodney W. Grimes  * Each socket contains two socket buffers: one for sending data and
388df8bae1dSRodney W. Grimes  * one for receiving data.  Each buffer contains a queue of mbufs,
389df8bae1dSRodney W. Grimes  * information about the number of mbufs and amount of data in the
390df8bae1dSRodney W. Grimes  * queue, and other fields allowing select() statements and notification
391df8bae1dSRodney W. Grimes  * on data availability to be implemented.
392df8bae1dSRodney W. Grimes  *
393df8bae1dSRodney W. Grimes  * Data stored in a socket buffer is maintained as a list of records.
394df8bae1dSRodney W. Grimes  * Each record is a list of mbufs chained together with the m_next
395df8bae1dSRodney W. Grimes  * field.  Records are chained together with the m_nextpkt field. The upper
396df8bae1dSRodney W. Grimes  * level routine soreceive() expects the following conventions to be
397df8bae1dSRodney W. Grimes  * observed when placing information in the receive buffer:
398df8bae1dSRodney W. Grimes  *
399df8bae1dSRodney W. Grimes  * 1. If the protocol requires each message be preceded by the sender's
400df8bae1dSRodney W. Grimes  *    name, then a record containing that name must be present before
401df8bae1dSRodney W. Grimes  *    any associated data (mbuf's must be of type MT_SONAME).
402df8bae1dSRodney W. Grimes  * 2. If the protocol supports the exchange of ``access rights'' (really
403df8bae1dSRodney W. Grimes  *    just additional data associated with the message), and there are
404df8bae1dSRodney W. Grimes  *    ``rights'' to be received, then a record containing this data
405df8bae1dSRodney W. Grimes  *    should be present (mbuf's must be of type MT_RIGHTS).
406df8bae1dSRodney W. Grimes  * 3. If a name or rights record exists, then it must be followed by
407df8bae1dSRodney W. Grimes  *    a data record, perhaps of zero length.
408df8bae1dSRodney W. Grimes  *
409df8bae1dSRodney W. Grimes  * Before using a new socket structure it is first necessary to reserve
410df8bae1dSRodney W. Grimes  * buffer space to the socket, by calling sbreserve().  This should commit
411df8bae1dSRodney W. Grimes  * some of the available buffer space in the system buffer pool for the
412df8bae1dSRodney W. Grimes  * socket (currently, it does nothing but enforce limits).  The space
413df8bae1dSRodney W. Grimes  * should be released by calling sbrelease() when the socket is destroyed.
414df8bae1dSRodney W. Grimes  */
415df8bae1dSRodney W. Grimes 
41626f9a767SRodney W. Grimes int
417df8bae1dSRodney W. Grimes soreserve(so, sndcc, rcvcc)
418df8bae1dSRodney W. Grimes 	register struct socket *so;
419df8bae1dSRodney W. Grimes 	u_long sndcc, rcvcc;
420df8bae1dSRodney W. Grimes {
421b40ce416SJulian Elischer 	struct thread *td = curthread;
422df8bae1dSRodney W. Grimes 
423b40ce416SJulian Elischer 	if (sbreserve(&so->so_snd, sndcc, so, td) == 0)
424df8bae1dSRodney W. Grimes 		goto bad;
425b40ce416SJulian Elischer 	if (sbreserve(&so->so_rcv, rcvcc, so, td) == 0)
426df8bae1dSRodney W. Grimes 		goto bad2;
4279535efc0SRobert Watson 	SOCKBUF_LOCK(&so->so_rcv);
428df8bae1dSRodney W. Grimes 	if (so->so_rcv.sb_lowat == 0)
429df8bae1dSRodney W. Grimes 		so->so_rcv.sb_lowat = 1;
4309535efc0SRobert Watson 	SOCKBUF_UNLOCK(&so->so_rcv);
4319535efc0SRobert Watson 	SOCKBUF_LOCK(&so->so_snd);
432df8bae1dSRodney W. Grimes 	if (so->so_snd.sb_lowat == 0)
433df8bae1dSRodney W. Grimes 		so->so_snd.sb_lowat = MCLBYTES;
434df8bae1dSRodney W. Grimes 	if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat)
435df8bae1dSRodney W. Grimes 		so->so_snd.sb_lowat = so->so_snd.sb_hiwat;
4369535efc0SRobert Watson 	SOCKBUF_UNLOCK(&so->so_snd);
437df8bae1dSRodney W. Grimes 	return (0);
438df8bae1dSRodney W. Grimes bad2:
439ecf72308SBrian Feldman 	sbrelease(&so->so_snd, so);
440df8bae1dSRodney W. Grimes bad:
441df8bae1dSRodney W. Grimes 	return (ENOBUFS);
442df8bae1dSRodney W. Grimes }
443df8bae1dSRodney W. Grimes 
44479cb7eb4SDavid Greenman static int
44579cb7eb4SDavid Greenman sysctl_handle_sb_max(SYSCTL_HANDLER_ARGS)
44679cb7eb4SDavid Greenman {
44779cb7eb4SDavid Greenman 	int error = 0;
44879cb7eb4SDavid Greenman 	u_long old_sb_max = sb_max;
44979cb7eb4SDavid Greenman 
4501b978d45SHartmut Brandt 	error = SYSCTL_OUT(req, arg1, sizeof(u_long));
45179cb7eb4SDavid Greenman 	if (error || !req->newptr)
45279cb7eb4SDavid Greenman 		return (error);
4531b978d45SHartmut Brandt 	error = SYSCTL_IN(req, arg1, sizeof(u_long));
45479cb7eb4SDavid Greenman 	if (error)
45579cb7eb4SDavid Greenman 		return (error);
45679cb7eb4SDavid Greenman 	if (sb_max < MSIZE + MCLBYTES) {
45779cb7eb4SDavid Greenman 		sb_max = old_sb_max;
45879cb7eb4SDavid Greenman 		return (EINVAL);
45979cb7eb4SDavid Greenman 	}
46079cb7eb4SDavid Greenman 	sb_max_adj = (u_quad_t)sb_max * MCLBYTES / (MSIZE + MCLBYTES);
46179cb7eb4SDavid Greenman 	return (0);
46279cb7eb4SDavid Greenman }
46379cb7eb4SDavid Greenman 
464df8bae1dSRodney W. Grimes /*
465df8bae1dSRodney W. Grimes  * Allot mbufs to a sockbuf.
466df8bae1dSRodney W. Grimes  * Attempt to scale mbmax so that mbcnt doesn't become limiting
467df8bae1dSRodney W. Grimes  * if buffering efficiency is near the normal case.
468df8bae1dSRodney W. Grimes  */
46926f9a767SRodney W. Grimes int
470b40ce416SJulian Elischer sbreserve(sb, cc, so, td)
471df8bae1dSRodney W. Grimes 	struct sockbuf *sb;
472df8bae1dSRodney W. Grimes 	u_long cc;
473ecf72308SBrian Feldman 	struct socket *so;
474b40ce416SJulian Elischer 	struct thread *td;
475df8bae1dSRodney W. Grimes {
47691d5354aSJohn Baldwin 	rlim_t sbsize_limit;
477ecf72308SBrian Feldman 
478ecf72308SBrian Feldman 	/*
479b40ce416SJulian Elischer 	 * td will only be NULL when we're in an interrupt
480ecf72308SBrian Feldman 	 * (e.g. in tcp_input())
481ecf72308SBrian Feldman 	 */
48279cb7eb4SDavid Greenman 	if (cc > sb_max_adj)
483df8bae1dSRodney W. Grimes 		return (0);
48491d5354aSJohn Baldwin 	if (td != NULL) {
48591d5354aSJohn Baldwin 		PROC_LOCK(td->td_proc);
48691d5354aSJohn Baldwin 		sbsize_limit = lim_cur(td->td_proc, RLIMIT_SBSIZE);
48791d5354aSJohn Baldwin 		PROC_UNLOCK(td->td_proc);
48891d5354aSJohn Baldwin 	} else
48991d5354aSJohn Baldwin 		sbsize_limit = RLIM_INFINITY;
490f535380cSDon Lewis 	if (!chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, cc,
49191d5354aSJohn Baldwin 	    sbsize_limit))
492ecf72308SBrian Feldman 		return (0);
4934b29bc4fSGarrett Wollman 	sb->sb_mbmax = min(cc * sb_efficiency, sb_max);
494df8bae1dSRodney W. Grimes 	if (sb->sb_lowat > sb->sb_hiwat)
495df8bae1dSRodney W. Grimes 		sb->sb_lowat = sb->sb_hiwat;
496df8bae1dSRodney W. Grimes 	return (1);
497df8bae1dSRodney W. Grimes }
498df8bae1dSRodney W. Grimes 
499df8bae1dSRodney W. Grimes /*
500df8bae1dSRodney W. Grimes  * Free mbufs held by a socket, and reserved mbuf space.
501df8bae1dSRodney W. Grimes  */
50226f9a767SRodney W. Grimes void
503ecf72308SBrian Feldman sbrelease(sb, so)
504df8bae1dSRodney W. Grimes 	struct sockbuf *sb;
505ecf72308SBrian Feldman 	struct socket *so;
506df8bae1dSRodney W. Grimes {
507df8bae1dSRodney W. Grimes 
508df8bae1dSRodney W. Grimes 	sbflush(sb);
509f535380cSDon Lewis 	(void)chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, 0,
510f535380cSDon Lewis 	    RLIM_INFINITY);
5116aef685fSBrian Feldman 	sb->sb_mbmax = 0;
512df8bae1dSRodney W. Grimes }
513df8bae1dSRodney W. Grimes 
514df8bae1dSRodney W. Grimes /*
515df8bae1dSRodney W. Grimes  * Routines to add and remove
516df8bae1dSRodney W. Grimes  * data from an mbuf queue.
517df8bae1dSRodney W. Grimes  *
518df8bae1dSRodney W. Grimes  * The routines sbappend() or sbappendrecord() are normally called to
519df8bae1dSRodney W. Grimes  * append new mbufs to a socket buffer, after checking that adequate
520df8bae1dSRodney W. Grimes  * space is available, comparing the function sbspace() with the amount
521df8bae1dSRodney W. Grimes  * of data to be added.  sbappendrecord() differs from sbappend() in
522df8bae1dSRodney W. Grimes  * that data supplied is treated as the beginning of a new record.
523df8bae1dSRodney W. Grimes  * To place a sender's address, optional access rights, and data in a
524df8bae1dSRodney W. Grimes  * socket receive buffer, sbappendaddr() should be used.  To place
525df8bae1dSRodney W. Grimes  * access rights and data in a socket receive buffer, sbappendrights()
526df8bae1dSRodney W. Grimes  * should be used.  In either case, the new data begins a new record.
527df8bae1dSRodney W. Grimes  * Note that unlike sbappend() and sbappendrecord(), these routines check
528df8bae1dSRodney W. Grimes  * for the caller that there will be enough space to store the data.
529df8bae1dSRodney W. Grimes  * Each fails if there is not enough space, or if it cannot find mbufs
530df8bae1dSRodney W. Grimes  * to store additional information in.
531df8bae1dSRodney W. Grimes  *
532df8bae1dSRodney W. Grimes  * Reliable protocols may use the socket send buffer to hold data
533df8bae1dSRodney W. Grimes  * awaiting acknowledgement.  Data is normally copied from a socket
534df8bae1dSRodney W. Grimes  * send buffer in a protocol with m_copy for output to a peer,
535df8bae1dSRodney W. Grimes  * and then removing the data from the socket buffer with sbdrop()
536df8bae1dSRodney W. Grimes  * or sbdroprecord() when the data is acknowledged by the peer.
537df8bae1dSRodney W. Grimes  */
538df8bae1dSRodney W. Grimes 
539395bb186SSam Leffler #ifdef SOCKBUF_DEBUG
540395bb186SSam Leffler void
541395bb186SSam Leffler sblastrecordchk(struct sockbuf *sb, const char *file, int line)
542395bb186SSam Leffler {
543395bb186SSam Leffler 	struct mbuf *m = sb->sb_mb;
544395bb186SSam Leffler 
545395bb186SSam Leffler 	while (m && m->m_nextpkt)
546395bb186SSam Leffler 		m = m->m_nextpkt;
547395bb186SSam Leffler 
548395bb186SSam Leffler 	if (m != sb->sb_lastrecord) {
549395bb186SSam Leffler 		printf("%s: sb_mb %p sb_lastrecord %p last %p\n",
550395bb186SSam Leffler 			__func__, sb->sb_mb, sb->sb_lastrecord, m);
551395bb186SSam Leffler 		printf("packet chain:\n");
552395bb186SSam Leffler 		for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt)
553395bb186SSam Leffler 			printf("\t%p\n", m);
554395bb186SSam Leffler 		panic("%s from %s:%u", __func__, file, line);
555395bb186SSam Leffler 	}
556395bb186SSam Leffler }
557395bb186SSam Leffler 
558395bb186SSam Leffler void
559395bb186SSam Leffler sblastmbufchk(struct sockbuf *sb, const char *file, int line)
560395bb186SSam Leffler {
561395bb186SSam Leffler 	struct mbuf *m = sb->sb_mb;
562395bb186SSam Leffler 	struct mbuf *n;
563395bb186SSam Leffler 
564395bb186SSam Leffler 	while (m && m->m_nextpkt)
565395bb186SSam Leffler 		m = m->m_nextpkt;
566395bb186SSam Leffler 
567395bb186SSam Leffler 	while (m && m->m_next)
568395bb186SSam Leffler 		m = m->m_next;
569395bb186SSam Leffler 
570395bb186SSam Leffler 	if (m != sb->sb_mbtail) {
571395bb186SSam Leffler 		printf("%s: sb_mb %p sb_mbtail %p last %p\n",
572395bb186SSam Leffler 			__func__, sb->sb_mb, sb->sb_mbtail, m);
573395bb186SSam Leffler 		printf("packet tree:\n");
574395bb186SSam Leffler 		for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt) {
575395bb186SSam Leffler 			printf("\t");
576395bb186SSam Leffler 			for (n = m; n != NULL; n = n->m_next)
577395bb186SSam Leffler 				printf("%p ", n);
578395bb186SSam Leffler 			printf("\n");
579395bb186SSam Leffler 		}
580395bb186SSam Leffler 		panic("%s from %s:%u", __func__, file, line);
581395bb186SSam Leffler 	}
582395bb186SSam Leffler }
583395bb186SSam Leffler #endif /* SOCKBUF_DEBUG */
584395bb186SSam Leffler 
585395bb186SSam Leffler #define SBLINKRECORD(sb, m0) do {					\
586395bb186SSam Leffler 	if ((sb)->sb_lastrecord != NULL)				\
587395bb186SSam Leffler 		(sb)->sb_lastrecord->m_nextpkt = (m0);			\
588395bb186SSam Leffler 	else								\
589395bb186SSam Leffler 		(sb)->sb_mb = (m0);					\
590395bb186SSam Leffler 	(sb)->sb_lastrecord = (m0);					\
591395bb186SSam Leffler } while (/*CONSTCOND*/0)
592395bb186SSam Leffler 
593df8bae1dSRodney W. Grimes /*
594df8bae1dSRodney W. Grimes  * Append mbuf chain m to the last record in the
595df8bae1dSRodney W. Grimes  * socket buffer sb.  The additional space associated
596df8bae1dSRodney W. Grimes  * the mbuf chain is recorded in sb.  Empty mbufs are
597df8bae1dSRodney W. Grimes  * discarded and mbufs are compacted where possible.
598df8bae1dSRodney W. Grimes  */
59926f9a767SRodney W. Grimes void
600df8bae1dSRodney W. Grimes sbappend(sb, m)
601df8bae1dSRodney W. Grimes 	struct sockbuf *sb;
602df8bae1dSRodney W. Grimes 	struct mbuf *m;
603df8bae1dSRodney W. Grimes {
604df8bae1dSRodney W. Grimes 	register struct mbuf *n;
605df8bae1dSRodney W. Grimes 
606df8bae1dSRodney W. Grimes 	if (m == 0)
607df8bae1dSRodney W. Grimes 		return;
608395bb186SSam Leffler 	SBLASTRECORDCHK(sb);
609797f2d22SPoul-Henning Kamp 	n = sb->sb_mb;
610797f2d22SPoul-Henning Kamp 	if (n) {
611df8bae1dSRodney W. Grimes 		while (n->m_nextpkt)
612df8bae1dSRodney W. Grimes 			n = n->m_nextpkt;
613df8bae1dSRodney W. Grimes 		do {
614df8bae1dSRodney W. Grimes 			if (n->m_flags & M_EOR) {
615df8bae1dSRodney W. Grimes 				sbappendrecord(sb, m); /* XXXXXX!!!! */
616df8bae1dSRodney W. Grimes 				return;
617df8bae1dSRodney W. Grimes 			}
618df8bae1dSRodney W. Grimes 		} while (n->m_next && (n = n->m_next));
619395bb186SSam Leffler 	} else {
620395bb186SSam Leffler 		/*
621395bb186SSam Leffler 		 * XXX Would like to simply use sb_mbtail here, but
622395bb186SSam Leffler 		 * XXX I need to verify that I won't miss an EOR that
623395bb186SSam Leffler 		 * XXX way.
624395bb186SSam Leffler 		 */
625395bb186SSam Leffler 		if ((n = sb->sb_lastrecord) != NULL) {
626395bb186SSam Leffler 			do {
627395bb186SSam Leffler 				if (n->m_flags & M_EOR) {
628395bb186SSam Leffler 					sbappendrecord(sb, m); /* XXXXXX!!!! */
629395bb186SSam Leffler 					return;
630395bb186SSam Leffler 				}
631395bb186SSam Leffler 			} while (n->m_next && (n = n->m_next));
632395bb186SSam Leffler 		} else {
633395bb186SSam Leffler 			/*
634395bb186SSam Leffler 			 * If this is the first record in the socket buffer,
635395bb186SSam Leffler 			 * it's also the last record.
636395bb186SSam Leffler 			 */
637395bb186SSam Leffler 			sb->sb_lastrecord = m;
638395bb186SSam Leffler 		}
639df8bae1dSRodney W. Grimes 	}
640df8bae1dSRodney W. Grimes 	sbcompress(sb, m, n);
641395bb186SSam Leffler 	SBLASTRECORDCHK(sb);
642395bb186SSam Leffler }
643395bb186SSam Leffler 
644395bb186SSam Leffler /*
645395bb186SSam Leffler  * This version of sbappend() should only be used when the caller
646395bb186SSam Leffler  * absolutely knows that there will never be more than one record
647395bb186SSam Leffler  * in the socket buffer, that is, a stream protocol (such as TCP).
648395bb186SSam Leffler  */
649395bb186SSam Leffler void
650395bb186SSam Leffler sbappendstream(struct sockbuf *sb, struct mbuf *m)
651395bb186SSam Leffler {
652395bb186SSam Leffler 
653395bb186SSam Leffler 	KASSERT(m->m_nextpkt == NULL,("sbappendstream 0"));
654395bb186SSam Leffler 	KASSERT(sb->sb_mb == sb->sb_lastrecord,("sbappendstream 1"));
655395bb186SSam Leffler 
656395bb186SSam Leffler 	SBLASTMBUFCHK(sb);
657395bb186SSam Leffler 
658395bb186SSam Leffler 	sbcompress(sb, m, sb->sb_mbtail);
659395bb186SSam Leffler 
660395bb186SSam Leffler 	sb->sb_lastrecord = sb->sb_mb;
661395bb186SSam Leffler 	SBLASTRECORDCHK(sb);
662df8bae1dSRodney W. Grimes }
663df8bae1dSRodney W. Grimes 
664df8bae1dSRodney W. Grimes #ifdef SOCKBUF_DEBUG
66526f9a767SRodney W. Grimes void
666df8bae1dSRodney W. Grimes sbcheck(sb)
6677ed60de8SPoul-Henning Kamp 	struct sockbuf *sb;
668df8bae1dSRodney W. Grimes {
6697ed60de8SPoul-Henning Kamp 	struct mbuf *m;
6707ed60de8SPoul-Henning Kamp 	struct mbuf *n = 0;
6717ed60de8SPoul-Henning Kamp 	u_long len = 0, mbcnt = 0;
672df8bae1dSRodney W. Grimes 
6730931333fSBill Fenner 	for (m = sb->sb_mb; m; m = n) {
6740931333fSBill Fenner 	    n = m->m_nextpkt;
6750931333fSBill Fenner 	    for (; m; m = m->m_next) {
676df8bae1dSRodney W. Grimes 		len += m->m_len;
677df8bae1dSRodney W. Grimes 		mbcnt += MSIZE;
678313861b8SJulian Elischer 		if (m->m_flags & M_EXT) /*XXX*/ /* pretty sure this is bogus */
679df8bae1dSRodney W. Grimes 			mbcnt += m->m_ext.ext_size;
6800931333fSBill Fenner 	    }
681df8bae1dSRodney W. Grimes 	}
682df8bae1dSRodney W. Grimes 	if (len != sb->sb_cc || mbcnt != sb->sb_mbcnt) {
683395bb186SSam Leffler 		printf("cc %ld != %u || mbcnt %ld != %u\n", len, sb->sb_cc,
684df8bae1dSRodney W. Grimes 		    mbcnt, sb->sb_mbcnt);
685df8bae1dSRodney W. Grimes 		panic("sbcheck");
686df8bae1dSRodney W. Grimes 	}
687df8bae1dSRodney W. Grimes }
688df8bae1dSRodney W. Grimes #endif
689df8bae1dSRodney W. Grimes 
690df8bae1dSRodney W. Grimes /*
691df8bae1dSRodney W. Grimes  * As above, except the mbuf chain
692df8bae1dSRodney W. Grimes  * begins a new record.
693df8bae1dSRodney W. Grimes  */
69426f9a767SRodney W. Grimes void
695df8bae1dSRodney W. Grimes sbappendrecord(sb, m0)
696df8bae1dSRodney W. Grimes 	register struct sockbuf *sb;
697df8bae1dSRodney W. Grimes 	register struct mbuf *m0;
698df8bae1dSRodney W. Grimes {
699df8bae1dSRodney W. Grimes 	register struct mbuf *m;
700df8bae1dSRodney W. Grimes 
701df8bae1dSRodney W. Grimes 	if (m0 == 0)
702df8bae1dSRodney W. Grimes 		return;
703797f2d22SPoul-Henning Kamp 	m = sb->sb_mb;
704797f2d22SPoul-Henning Kamp 	if (m)
705df8bae1dSRodney W. Grimes 		while (m->m_nextpkt)
706df8bae1dSRodney W. Grimes 			m = m->m_nextpkt;
707df8bae1dSRodney W. Grimes 	/*
708df8bae1dSRodney W. Grimes 	 * Put the first mbuf on the queue.
709df8bae1dSRodney W. Grimes 	 * Note this permits zero length records.
710df8bae1dSRodney W. Grimes 	 */
711df8bae1dSRodney W. Grimes 	sballoc(sb, m0);
712395bb186SSam Leffler 	SBLASTRECORDCHK(sb);
713395bb186SSam Leffler 	SBLINKRECORD(sb, m0);
714df8bae1dSRodney W. Grimes 	if (m)
715df8bae1dSRodney W. Grimes 		m->m_nextpkt = m0;
716df8bae1dSRodney W. Grimes 	else
717df8bae1dSRodney W. Grimes 		sb->sb_mb = m0;
718df8bae1dSRodney W. Grimes 	m = m0->m_next;
719df8bae1dSRodney W. Grimes 	m0->m_next = 0;
720df8bae1dSRodney W. Grimes 	if (m && (m0->m_flags & M_EOR)) {
721df8bae1dSRodney W. Grimes 		m0->m_flags &= ~M_EOR;
722df8bae1dSRodney W. Grimes 		m->m_flags |= M_EOR;
723df8bae1dSRodney W. Grimes 	}
724df8bae1dSRodney W. Grimes 	sbcompress(sb, m, m0);
725df8bae1dSRodney W. Grimes }
726df8bae1dSRodney W. Grimes 
727df8bae1dSRodney W. Grimes /*
728df8bae1dSRodney W. Grimes  * As above except that OOB data
729df8bae1dSRodney W. Grimes  * is inserted at the beginning of the sockbuf,
730df8bae1dSRodney W. Grimes  * but after any other OOB data.
731df8bae1dSRodney W. Grimes  */
73226f9a767SRodney W. Grimes void
733df8bae1dSRodney W. Grimes sbinsertoob(sb, m0)
734df8bae1dSRodney W. Grimes 	register struct sockbuf *sb;
735df8bae1dSRodney W. Grimes 	register struct mbuf *m0;
736df8bae1dSRodney W. Grimes {
737df8bae1dSRodney W. Grimes 	register struct mbuf *m;
738df8bae1dSRodney W. Grimes 	register struct mbuf **mp;
739df8bae1dSRodney W. Grimes 
740df8bae1dSRodney W. Grimes 	if (m0 == 0)
741df8bae1dSRodney W. Grimes 		return;
742797f2d22SPoul-Henning Kamp 	for (mp = &sb->sb_mb; *mp ; mp = &((*mp)->m_nextpkt)) {
743797f2d22SPoul-Henning Kamp 	    m = *mp;
744df8bae1dSRodney W. Grimes 	    again:
745df8bae1dSRodney W. Grimes 		switch (m->m_type) {
746df8bae1dSRodney W. Grimes 
747df8bae1dSRodney W. Grimes 		case MT_OOBDATA:
748df8bae1dSRodney W. Grimes 			continue;		/* WANT next train */
749df8bae1dSRodney W. Grimes 
750df8bae1dSRodney W. Grimes 		case MT_CONTROL:
751797f2d22SPoul-Henning Kamp 			m = m->m_next;
752797f2d22SPoul-Henning Kamp 			if (m)
753df8bae1dSRodney W. Grimes 				goto again;	/* inspect THIS train further */
754df8bae1dSRodney W. Grimes 		}
755df8bae1dSRodney W. Grimes 		break;
756df8bae1dSRodney W. Grimes 	}
757df8bae1dSRodney W. Grimes 	/*
758df8bae1dSRodney W. Grimes 	 * Put the first mbuf on the queue.
759df8bae1dSRodney W. Grimes 	 * Note this permits zero length records.
760df8bae1dSRodney W. Grimes 	 */
761df8bae1dSRodney W. Grimes 	sballoc(sb, m0);
762df8bae1dSRodney W. Grimes 	m0->m_nextpkt = *mp;
763df8bae1dSRodney W. Grimes 	*mp = m0;
764df8bae1dSRodney W. Grimes 	m = m0->m_next;
765df8bae1dSRodney W. Grimes 	m0->m_next = 0;
766df8bae1dSRodney W. Grimes 	if (m && (m0->m_flags & M_EOR)) {
767df8bae1dSRodney W. Grimes 		m0->m_flags &= ~M_EOR;
768df8bae1dSRodney W. Grimes 		m->m_flags |= M_EOR;
769df8bae1dSRodney W. Grimes 	}
770df8bae1dSRodney W. Grimes 	sbcompress(sb, m, m0);
771df8bae1dSRodney W. Grimes }
772df8bae1dSRodney W. Grimes 
773df8bae1dSRodney W. Grimes /*
774df8bae1dSRodney W. Grimes  * Append address and data, and optionally, control (ancillary) data
775df8bae1dSRodney W. Grimes  * to the receive queue of a socket.  If present,
776df8bae1dSRodney W. Grimes  * m0 must include a packet header with total length.
777df8bae1dSRodney W. Grimes  * Returns 0 if no space in sockbuf or insufficient mbufs.
778df8bae1dSRodney W. Grimes  */
77926f9a767SRodney W. Grimes int
780df8bae1dSRodney W. Grimes sbappendaddr(sb, asa, m0, control)
7817ed60de8SPoul-Henning Kamp 	struct sockbuf *sb;
782e7dd9a10SRobert Watson 	const struct sockaddr *asa;
783df8bae1dSRodney W. Grimes 	struct mbuf *m0, *control;
784df8bae1dSRodney W. Grimes {
785395bb186SSam Leffler 	struct mbuf *m, *n, *nlast;
786df8bae1dSRodney W. Grimes 	int space = asa->sa_len;
787df8bae1dSRodney W. Grimes 
788df8bae1dSRodney W. Grimes 	if (m0 && (m0->m_flags & M_PKTHDR) == 0)
789df8bae1dSRodney W. Grimes 		panic("sbappendaddr");
790df8bae1dSRodney W. Grimes 	if (m0)
791df8bae1dSRodney W. Grimes 		space += m0->m_pkthdr.len;
7927ed60de8SPoul-Henning Kamp 	space += m_length(control, &n);
793df8bae1dSRodney W. Grimes 	if (space > sbspace(sb))
794df8bae1dSRodney W. Grimes 		return (0);
795c43cad1aSScott Long #if MSIZE <= 256
796df8bae1dSRodney W. Grimes 	if (asa->sa_len > MLEN)
797df8bae1dSRodney W. Grimes 		return (0);
798c43cad1aSScott Long #endif
799a163d034SWarner Losh 	MGET(m, M_DONTWAIT, MT_SONAME);
800df8bae1dSRodney W. Grimes 	if (m == 0)
801df8bae1dSRodney W. Grimes 		return (0);
802df8bae1dSRodney W. Grimes 	m->m_len = asa->sa_len;
80380208239SAlfred Perlstein 	bcopy(asa, mtod(m, caddr_t), asa->sa_len);
804df8bae1dSRodney W. Grimes 	if (n)
805df8bae1dSRodney W. Grimes 		n->m_next = m0;		/* concatenate data to control */
806df8bae1dSRodney W. Grimes 	else
807df8bae1dSRodney W. Grimes 		control = m0;
808df8bae1dSRodney W. Grimes 	m->m_next = control;
809395bb186SSam Leffler 	for (n = m; n->m_next != NULL; n = n->m_next)
810df8bae1dSRodney W. Grimes 		sballoc(sb, n);
811395bb186SSam Leffler 	sballoc(sb, n);
812395bb186SSam Leffler 	nlast = n;
813395bb186SSam Leffler 	SBLINKRECORD(sb, m);
814395bb186SSam Leffler 
815395bb186SSam Leffler 	sb->sb_mbtail = nlast;
816395bb186SSam Leffler 	SBLASTMBUFCHK(sb);
817395bb186SSam Leffler 
818395bb186SSam Leffler 	SBLASTRECORDCHK(sb);
819df8bae1dSRodney W. Grimes 	return (1);
820df8bae1dSRodney W. Grimes }
821df8bae1dSRodney W. Grimes 
82226f9a767SRodney W. Grimes int
823df8bae1dSRodney W. Grimes sbappendcontrol(sb, m0, control)
824df8bae1dSRodney W. Grimes 	struct sockbuf *sb;
825df8bae1dSRodney W. Grimes 	struct mbuf *control, *m0;
826df8bae1dSRodney W. Grimes {
827395bb186SSam Leffler 	struct mbuf *m, *n, *mlast;
8287ed60de8SPoul-Henning Kamp 	int space;
829df8bae1dSRodney W. Grimes 
830df8bae1dSRodney W. Grimes 	if (control == 0)
831df8bae1dSRodney W. Grimes 		panic("sbappendcontrol");
8327ed60de8SPoul-Henning Kamp 	space = m_length(control, &n) + m_length(m0, NULL);
833df8bae1dSRodney W. Grimes 	if (space > sbspace(sb))
834df8bae1dSRodney W. Grimes 		return (0);
835df8bae1dSRodney W. Grimes 	n->m_next = m0;			/* concatenate data to control */
836395bb186SSam Leffler 
837395bb186SSam Leffler 	SBLASTRECORDCHK(sb);
838395bb186SSam Leffler 
839395bb186SSam Leffler 	for (m = control; m->m_next; m = m->m_next)
840df8bae1dSRodney W. Grimes 		sballoc(sb, m);
841395bb186SSam Leffler 	sballoc(sb, m);
842395bb186SSam Leffler 	mlast = m;
843395bb186SSam Leffler 	SBLINKRECORD(sb, control);
844395bb186SSam Leffler 
845395bb186SSam Leffler 	sb->sb_mbtail = mlast;
846395bb186SSam Leffler 	SBLASTMBUFCHK(sb);
847395bb186SSam Leffler 
848395bb186SSam Leffler 	SBLASTRECORDCHK(sb);
849df8bae1dSRodney W. Grimes 	return (1);
850df8bae1dSRodney W. Grimes }
851df8bae1dSRodney W. Grimes 
852df8bae1dSRodney W. Grimes /*
853df8bae1dSRodney W. Grimes  * Compress mbuf chain m into the socket
854df8bae1dSRodney W. Grimes  * buffer sb following mbuf n.  If n
855df8bae1dSRodney W. Grimes  * is null, the buffer is presumed empty.
856df8bae1dSRodney W. Grimes  */
85726f9a767SRodney W. Grimes void
858df8bae1dSRodney W. Grimes sbcompress(sb, m, n)
859df8bae1dSRodney W. Grimes 	register struct sockbuf *sb;
860df8bae1dSRodney W. Grimes 	register struct mbuf *m, *n;
861df8bae1dSRodney W. Grimes {
862df8bae1dSRodney W. Grimes 	register int eor = 0;
863df8bae1dSRodney W. Grimes 	register struct mbuf *o;
864df8bae1dSRodney W. Grimes 
865df8bae1dSRodney W. Grimes 	while (m) {
866df8bae1dSRodney W. Grimes 		eor |= m->m_flags & M_EOR;
867df8bae1dSRodney W. Grimes 		if (m->m_len == 0 &&
868df8bae1dSRodney W. Grimes 		    (eor == 0 ||
869df8bae1dSRodney W. Grimes 		     (((o = m->m_next) || (o = n)) &&
870df8bae1dSRodney W. Grimes 		      o->m_type == m->m_type))) {
871395bb186SSam Leffler 			if (sb->sb_lastrecord == m)
872395bb186SSam Leffler 				sb->sb_lastrecord = m->m_next;
873df8bae1dSRodney W. Grimes 			m = m_free(m);
874df8bae1dSRodney W. Grimes 			continue;
875df8bae1dSRodney W. Grimes 		}
87632af0d74SDavid Malone 		if (n && (n->m_flags & M_EOR) == 0 &&
87732af0d74SDavid Malone 		    M_WRITABLE(n) &&
87832af0d74SDavid Malone 		    m->m_len <= MCLBYTES / 4 && /* XXX: Don't copy too much */
87932af0d74SDavid Malone 		    m->m_len <= M_TRAILINGSPACE(n) &&
880df8bae1dSRodney W. Grimes 		    n->m_type == m->m_type) {
881df8bae1dSRodney W. Grimes 			bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len,
882df8bae1dSRodney W. Grimes 			    (unsigned)m->m_len);
883df8bae1dSRodney W. Grimes 			n->m_len += m->m_len;
884df8bae1dSRodney W. Grimes 			sb->sb_cc += m->m_len;
885b3f1af6bSTim J. Robbins 			if (m->m_type != MT_DATA && m->m_type != MT_HEADER &&
886b3f1af6bSTim J. Robbins 			    m->m_type != MT_OOBDATA)
887b3f1af6bSTim J. Robbins 				/* XXX: Probably don't need.*/
88804ac9b97SKelly Yancey 				sb->sb_ctl += m->m_len;
889df8bae1dSRodney W. Grimes 			m = m_free(m);
890df8bae1dSRodney W. Grimes 			continue;
891df8bae1dSRodney W. Grimes 		}
892df8bae1dSRodney W. Grimes 		if (n)
893df8bae1dSRodney W. Grimes 			n->m_next = m;
894df8bae1dSRodney W. Grimes 		else
895df8bae1dSRodney W. Grimes 			sb->sb_mb = m;
896395bb186SSam Leffler 		sb->sb_mbtail = m;
897df8bae1dSRodney W. Grimes 		sballoc(sb, m);
898df8bae1dSRodney W. Grimes 		n = m;
899df8bae1dSRodney W. Grimes 		m->m_flags &= ~M_EOR;
900df8bae1dSRodney W. Grimes 		m = m->m_next;
901df8bae1dSRodney W. Grimes 		n->m_next = 0;
902df8bae1dSRodney W. Grimes 	}
903df8bae1dSRodney W. Grimes 	if (eor) {
904df8bae1dSRodney W. Grimes 		if (n)
905df8bae1dSRodney W. Grimes 			n->m_flags |= eor;
906df8bae1dSRodney W. Grimes 		else
907df8bae1dSRodney W. Grimes 			printf("semi-panic: sbcompress\n");
908df8bae1dSRodney W. Grimes 	}
909395bb186SSam Leffler 	SBLASTMBUFCHK(sb);
910df8bae1dSRodney W. Grimes }
911df8bae1dSRodney W. Grimes 
912df8bae1dSRodney W. Grimes /*
913df8bae1dSRodney W. Grimes  * Free all mbufs in a sockbuf.
914df8bae1dSRodney W. Grimes  * Check that all resources are reclaimed.
915df8bae1dSRodney W. Grimes  */
91626f9a767SRodney W. Grimes void
917df8bae1dSRodney W. Grimes sbflush(sb)
918df8bae1dSRodney W. Grimes 	register struct sockbuf *sb;
919df8bae1dSRodney W. Grimes {
920df8bae1dSRodney W. Grimes 
921df8bae1dSRodney W. Grimes 	if (sb->sb_flags & SB_LOCK)
922253ab668SAndrey A. Chernov 		panic("sbflush: locked");
92323f84772SPierre Beyssac 	while (sb->sb_mbcnt) {
92423f84772SPierre Beyssac 		/*
92523f84772SPierre Beyssac 		 * Don't call sbdrop(sb, 0) if the leading mbuf is non-empty:
92623f84772SPierre Beyssac 		 * we would loop forever. Panic instead.
92723f84772SPierre Beyssac 		 */
92823f84772SPierre Beyssac 		if (!sb->sb_cc && (sb->sb_mb == NULL || sb->sb_mb->m_len))
92923f84772SPierre Beyssac 			break;
930df8bae1dSRodney W. Grimes 		sbdrop(sb, (int)sb->sb_cc);
93123f84772SPierre Beyssac 	}
9320931333fSBill Fenner 	if (sb->sb_cc || sb->sb_mb || sb->sb_mbcnt)
933e25dadb0SRobert Drehmel 		panic("sbflush: cc %u || mb %p || mbcnt %u", sb->sb_cc, (void *)sb->sb_mb, sb->sb_mbcnt);
934df8bae1dSRodney W. Grimes }
935df8bae1dSRodney W. Grimes 
936df8bae1dSRodney W. Grimes /*
937df8bae1dSRodney W. Grimes  * Drop data from (the front of) a sockbuf.
938df8bae1dSRodney W. Grimes  */
93926f9a767SRodney W. Grimes void
940df8bae1dSRodney W. Grimes sbdrop(sb, len)
941df8bae1dSRodney W. Grimes 	register struct sockbuf *sb;
942df8bae1dSRodney W. Grimes 	register int len;
943df8bae1dSRodney W. Grimes {
944ecde8f7cSMatthew Dillon 	register struct mbuf *m;
945df8bae1dSRodney W. Grimes 	struct mbuf *next;
946df8bae1dSRodney W. Grimes 
947df8bae1dSRodney W. Grimes 	next = (m = sb->sb_mb) ? m->m_nextpkt : 0;
948df8bae1dSRodney W. Grimes 	while (len > 0) {
949df8bae1dSRodney W. Grimes 		if (m == 0) {
950df8bae1dSRodney W. Grimes 			if (next == 0)
951df8bae1dSRodney W. Grimes 				panic("sbdrop");
952df8bae1dSRodney W. Grimes 			m = next;
953df8bae1dSRodney W. Grimes 			next = m->m_nextpkt;
954df8bae1dSRodney W. Grimes 			continue;
955df8bae1dSRodney W. Grimes 		}
956df8bae1dSRodney W. Grimes 		if (m->m_len > len) {
957df8bae1dSRodney W. Grimes 			m->m_len -= len;
958df8bae1dSRodney W. Grimes 			m->m_data += len;
959df8bae1dSRodney W. Grimes 			sb->sb_cc -= len;
960b3f1af6bSTim J. Robbins 			if (m->m_type != MT_DATA && m->m_type != MT_HEADER &&
961b3f1af6bSTim J. Robbins 			    m->m_type != MT_OOBDATA)
96204ac9b97SKelly Yancey 				sb->sb_ctl -= len;
963df8bae1dSRodney W. Grimes 			break;
964df8bae1dSRodney W. Grimes 		}
965df8bae1dSRodney W. Grimes 		len -= m->m_len;
966df8bae1dSRodney W. Grimes 		sbfree(sb, m);
967ecde8f7cSMatthew Dillon 		m = m_free(m);
968df8bae1dSRodney W. Grimes 	}
969df8bae1dSRodney W. Grimes 	while (m && m->m_len == 0) {
970df8bae1dSRodney W. Grimes 		sbfree(sb, m);
971ecde8f7cSMatthew Dillon 		m = m_free(m);
972df8bae1dSRodney W. Grimes 	}
973df8bae1dSRodney W. Grimes 	if (m) {
974df8bae1dSRodney W. Grimes 		sb->sb_mb = m;
975df8bae1dSRodney W. Grimes 		m->m_nextpkt = next;
976df8bae1dSRodney W. Grimes 	} else
977df8bae1dSRodney W. Grimes 		sb->sb_mb = next;
978395bb186SSam Leffler 	/*
979395bb186SSam Leffler 	 * First part is an inline SB_EMPTY_FIXUP().  Second part
980395bb186SSam Leffler 	 * makes sure sb_lastrecord is up-to-date if we dropped
981395bb186SSam Leffler 	 * part of the last record.
982395bb186SSam Leffler 	 */
983395bb186SSam Leffler 	m = sb->sb_mb;
984395bb186SSam Leffler 	if (m == NULL) {
985395bb186SSam Leffler 		sb->sb_mbtail = NULL;
986395bb186SSam Leffler 		sb->sb_lastrecord = NULL;
987395bb186SSam Leffler 	} else if (m->m_nextpkt == NULL) {
988395bb186SSam Leffler 		sb->sb_lastrecord = m;
989395bb186SSam Leffler 	}
990df8bae1dSRodney W. Grimes }
991df8bae1dSRodney W. Grimes 
992df8bae1dSRodney W. Grimes /*
993df8bae1dSRodney W. Grimes  * Drop a record off the front of a sockbuf
994df8bae1dSRodney W. Grimes  * and move the next record to the front.
995df8bae1dSRodney W. Grimes  */
99626f9a767SRodney W. Grimes void
997df8bae1dSRodney W. Grimes sbdroprecord(sb)
998df8bae1dSRodney W. Grimes 	register struct sockbuf *sb;
999df8bae1dSRodney W. Grimes {
1000ecde8f7cSMatthew Dillon 	register struct mbuf *m;
1001df8bae1dSRodney W. Grimes 
1002df8bae1dSRodney W. Grimes 	m = sb->sb_mb;
1003df8bae1dSRodney W. Grimes 	if (m) {
1004df8bae1dSRodney W. Grimes 		sb->sb_mb = m->m_nextpkt;
1005df8bae1dSRodney W. Grimes 		do {
1006df8bae1dSRodney W. Grimes 			sbfree(sb, m);
1007ecde8f7cSMatthew Dillon 			m = m_free(m);
1008797f2d22SPoul-Henning Kamp 		} while (m);
1009df8bae1dSRodney W. Grimes 	}
1010395bb186SSam Leffler 	SB_EMPTY_FIXUP(sb);
1011df8bae1dSRodney W. Grimes }
10121e4ad9ceSGarrett Wollman 
101382c23ebaSBill Fenner /*
101482c23ebaSBill Fenner  * Create a "control" mbuf containing the specified data
101582c23ebaSBill Fenner  * with the specified type for presentation on a socket buffer.
101682c23ebaSBill Fenner  */
101782c23ebaSBill Fenner struct mbuf *
101882c23ebaSBill Fenner sbcreatecontrol(p, size, type, level)
101982c23ebaSBill Fenner 	caddr_t p;
102082c23ebaSBill Fenner 	register int size;
102182c23ebaSBill Fenner 	int type, level;
102282c23ebaSBill Fenner {
102382c23ebaSBill Fenner 	register struct cmsghdr *cp;
102482c23ebaSBill Fenner 	struct mbuf *m;
102582c23ebaSBill Fenner 
102659bdd405SDavid Malone 	if (CMSG_SPACE((u_int)size) > MCLBYTES)
10270b97e97cSYoshinobu Inoue 		return ((struct mbuf *) NULL);
1028099a0e58SBosko Milekic 	if (CMSG_SPACE((u_int)size > MLEN))
1029099a0e58SBosko Milekic 		m = m_getcl(M_DONTWAIT, MT_CONTROL, 0);
1030099a0e58SBosko Milekic 	else
1031099a0e58SBosko Milekic 		m = m_get(M_DONTWAIT, MT_CONTROL);
1032099a0e58SBosko Milekic 	if (m == NULL)
103382c23ebaSBill Fenner 		return ((struct mbuf *) NULL);
103482c23ebaSBill Fenner 	cp = mtod(m, struct cmsghdr *);
103559bdd405SDavid Malone 	m->m_len = 0;
103659bdd405SDavid Malone 	KASSERT(CMSG_SPACE((u_int)size) <= M_TRAILINGSPACE(m),
103759bdd405SDavid Malone 	    ("sbcreatecontrol: short mbuf"));
103859bdd405SDavid Malone 	if (p != NULL)
103982c23ebaSBill Fenner 		(void)memcpy(CMSG_DATA(cp), p, size);
10407d0d8dc3SYoshinobu Inoue 	m->m_len = CMSG_SPACE(size);
10417d0d8dc3SYoshinobu Inoue 	cp->cmsg_len = CMSG_LEN(size);
104282c23ebaSBill Fenner 	cp->cmsg_level = level;
104382c23ebaSBill Fenner 	cp->cmsg_type = type;
104482c23ebaSBill Fenner 	return (m);
104582c23ebaSBill Fenner }
104682c23ebaSBill Fenner 
10471e4ad9ceSGarrett Wollman /*
10482c37256eSGarrett Wollman  * Some routines that return EOPNOTSUPP for entry points that are not
10492c37256eSGarrett Wollman  * supported by a protocol.  Fill in as needed.
10501e4ad9ceSGarrett Wollman  */
10511e4ad9ceSGarrett Wollman int
105257bf258eSGarrett Wollman pru_accept_notsupp(struct socket *so, struct sockaddr **nam)
1053d8392c6cSGarrett Wollman {
1054d8392c6cSGarrett Wollman 	return EOPNOTSUPP;
1055d8392c6cSGarrett Wollman }
1056d8392c6cSGarrett Wollman 
1057d8392c6cSGarrett Wollman int
1058b40ce416SJulian Elischer pru_connect_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td)
10599f907986SPeter Wemm {
10609f907986SPeter Wemm 	return EOPNOTSUPP;
10619f907986SPeter Wemm }
10629f907986SPeter Wemm 
10639f907986SPeter Wemm int
10642c37256eSGarrett Wollman pru_connect2_notsupp(struct socket *so1, struct socket *so2)
10651e4ad9ceSGarrett Wollman {
10662c37256eSGarrett Wollman 	return EOPNOTSUPP;
10671e4ad9ceSGarrett Wollman }
1068d8392c6cSGarrett Wollman 
1069d8392c6cSGarrett Wollman int
1070ecbb00a2SDoug Rabson pru_control_notsupp(struct socket *so, u_long cmd, caddr_t data,
1071b40ce416SJulian Elischer 		    struct ifnet *ifp, struct thread *td)
1072a29f300eSGarrett Wollman {
1073a29f300eSGarrett Wollman 	return EOPNOTSUPP;
1074a29f300eSGarrett Wollman }
1075a29f300eSGarrett Wollman 
1076a29f300eSGarrett Wollman int
1077b40ce416SJulian Elischer pru_listen_notsupp(struct socket *so, struct thread *td)
1078d8392c6cSGarrett Wollman {
1079d8392c6cSGarrett Wollman 	return EOPNOTSUPP;
1080d8392c6cSGarrett Wollman }
1081d8392c6cSGarrett Wollman 
1082d8392c6cSGarrett Wollman int
1083d8392c6cSGarrett Wollman pru_rcvd_notsupp(struct socket *so, int flags)
1084d8392c6cSGarrett Wollman {
1085d8392c6cSGarrett Wollman 	return EOPNOTSUPP;
1086d8392c6cSGarrett Wollman }
1087d8392c6cSGarrett Wollman 
1088d8392c6cSGarrett Wollman int
1089d8392c6cSGarrett Wollman pru_rcvoob_notsupp(struct socket *so, struct mbuf *m, int flags)
1090d8392c6cSGarrett Wollman {
1091d8392c6cSGarrett Wollman 	return EOPNOTSUPP;
1092d8392c6cSGarrett Wollman }
1093d8392c6cSGarrett Wollman 
1094d8392c6cSGarrett Wollman /*
1095d8392c6cSGarrett Wollman  * This isn't really a ``null'' operation, but it's the default one
1096d8392c6cSGarrett Wollman  * and doesn't do anything destructive.
1097d8392c6cSGarrett Wollman  */
1098d8392c6cSGarrett Wollman int
1099d8392c6cSGarrett Wollman pru_sense_null(struct socket *so, struct stat *sb)
1100d8392c6cSGarrett Wollman {
1101d8392c6cSGarrett Wollman 	sb->st_blksize = so->so_snd.sb_hiwat;
1102d8392c6cSGarrett Wollman 	return 0;
1103d8392c6cSGarrett Wollman }
1104639acc13SGarrett Wollman 
1105639acc13SGarrett Wollman /*
1106a557af22SRobert Watson  * For protocol types that don't keep cached copies of labels in their
1107a557af22SRobert Watson  * pcbs, provide a null sosetlabel that does a NOOP.
1108a557af22SRobert Watson  */
1109a557af22SRobert Watson void
1110a557af22SRobert Watson pru_sosetlabel_null(struct socket *so)
1111a557af22SRobert Watson {
1112a557af22SRobert Watson 
1113a557af22SRobert Watson }
1114a557af22SRobert Watson 
1115a557af22SRobert Watson /*
111657bf258eSGarrett Wollman  * Make a copy of a sockaddr in a malloced buffer of type M_SONAME.
111757bf258eSGarrett Wollman  */
111857bf258eSGarrett Wollman struct sockaddr *
1119746e5bf0SRobert Watson sodupsockaddr(const struct sockaddr *sa, int mflags)
112057bf258eSGarrett Wollman {
112157bf258eSGarrett Wollman 	struct sockaddr *sa2;
112257bf258eSGarrett Wollman 
1123746e5bf0SRobert Watson 	sa2 = malloc(sa->sa_len, M_SONAME, mflags);
112457bf258eSGarrett Wollman 	if (sa2)
112557bf258eSGarrett Wollman 		bcopy(sa, sa2, sa->sa_len);
112657bf258eSGarrett Wollman 	return sa2;
112757bf258eSGarrett Wollman }
112857bf258eSGarrett Wollman 
112957bf258eSGarrett Wollman /*
113098271db4SGarrett Wollman  * Create an external-format (``xsocket'') structure using the information
113198271db4SGarrett Wollman  * in the kernel-format socket structure pointed to by so.  This is done
113298271db4SGarrett Wollman  * to reduce the spew of irrelevant information over this interface,
113398271db4SGarrett Wollman  * to isolate user code from changes in the kernel structure, and
113498271db4SGarrett Wollman  * potentially to provide information-hiding if we decide that
113598271db4SGarrett Wollman  * some of this information should be hidden from users.
113698271db4SGarrett Wollman  */
113798271db4SGarrett Wollman void
113898271db4SGarrett Wollman sotoxsocket(struct socket *so, struct xsocket *xso)
113998271db4SGarrett Wollman {
114098271db4SGarrett Wollman 	xso->xso_len = sizeof *xso;
114198271db4SGarrett Wollman 	xso->xso_so = so;
114298271db4SGarrett Wollman 	xso->so_type = so->so_type;
114398271db4SGarrett Wollman 	xso->so_options = so->so_options;
114498271db4SGarrett Wollman 	xso->so_linger = so->so_linger;
114598271db4SGarrett Wollman 	xso->so_state = so->so_state;
114698271db4SGarrett Wollman 	xso->so_pcb = so->so_pcb;
114798271db4SGarrett Wollman 	xso->xso_protocol = so->so_proto->pr_protocol;
114898271db4SGarrett Wollman 	xso->xso_family = so->so_proto->pr_domain->dom_family;
114998271db4SGarrett Wollman 	xso->so_qlen = so->so_qlen;
115098271db4SGarrett Wollman 	xso->so_incqlen = so->so_incqlen;
115198271db4SGarrett Wollman 	xso->so_qlimit = so->so_qlimit;
115298271db4SGarrett Wollman 	xso->so_timeo = so->so_timeo;
115398271db4SGarrett Wollman 	xso->so_error = so->so_error;
1154831d27a9SDon Lewis 	xso->so_pgid = so->so_sigio ? so->so_sigio->sio_pgid : 0;
115598271db4SGarrett Wollman 	xso->so_oobmark = so->so_oobmark;
115698271db4SGarrett Wollman 	sbtoxsockbuf(&so->so_snd, &xso->so_snd);
115798271db4SGarrett Wollman 	sbtoxsockbuf(&so->so_rcv, &xso->so_rcv);
11582f9a2132SBrian Feldman 	xso->so_uid = so->so_cred->cr_uid;
115998271db4SGarrett Wollman }
116098271db4SGarrett Wollman 
116198271db4SGarrett Wollman /*
116298271db4SGarrett Wollman  * This does the same for sockbufs.  Note that the xsockbuf structure,
116398271db4SGarrett Wollman  * since it is always embedded in a socket, does not include a self
116498271db4SGarrett Wollman  * pointer nor a length.  We make this entry point public in case
116598271db4SGarrett Wollman  * some other mechanism needs it.
116698271db4SGarrett Wollman  */
116798271db4SGarrett Wollman void
116898271db4SGarrett Wollman sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb)
116998271db4SGarrett Wollman {
117098271db4SGarrett Wollman 	xsb->sb_cc = sb->sb_cc;
117198271db4SGarrett Wollman 	xsb->sb_hiwat = sb->sb_hiwat;
117298271db4SGarrett Wollman 	xsb->sb_mbcnt = sb->sb_mbcnt;
117398271db4SGarrett Wollman 	xsb->sb_mbmax = sb->sb_mbmax;
117498271db4SGarrett Wollman 	xsb->sb_lowat = sb->sb_lowat;
117598271db4SGarrett Wollman 	xsb->sb_flags = sb->sb_flags;
117698271db4SGarrett Wollman 	xsb->sb_timeo = sb->sb_timeo;
117798271db4SGarrett Wollman }
117898271db4SGarrett Wollman 
117998271db4SGarrett Wollman /*
1180639acc13SGarrett Wollman  * Here is the definition of some of the basic objects in the kern.ipc
1181639acc13SGarrett Wollman  * branch of the MIB.
1182639acc13SGarrett Wollman  */
1183639acc13SGarrett Wollman SYSCTL_NODE(_kern, KERN_IPC, ipc, CTLFLAG_RW, 0, "IPC");
1184639acc13SGarrett Wollman 
1185639acc13SGarrett Wollman /* This takes the place of kern.maxsockbuf, which moved to kern.ipc. */
1186639acc13SGarrett Wollman static int dummy;
1187639acc13SGarrett Wollman SYSCTL_INT(_kern, KERN_DUMMY, dummy, CTLFLAG_RW, &dummy, 0, "");
11881b978d45SHartmut Brandt SYSCTL_OID(_kern_ipc, KIPC_MAXSOCKBUF, maxsockbuf, CTLTYPE_ULONG|CTLFLAG_RW,
11891b978d45SHartmut Brandt     &sb_max, 0, sysctl_handle_sb_max, "LU", "Maximum socket buffer size");
1190184dcdc7SMike Silbersack SYSCTL_INT(_kern_ipc, OID_AUTO, maxsockets, CTLFLAG_RDTUN,
119154d77689SJeff Roberson     &maxsockets, 0, "Maximum number of sockets avaliable");
11921b978d45SHartmut Brandt SYSCTL_ULONG(_kern_ipc, KIPC_SOCKBUF_WASTE, sockbuf_waste_factor, CTLFLAG_RW,
1193639acc13SGarrett Wollman     &sb_efficiency, 0, "");
119454d77689SJeff Roberson 
119554d77689SJeff Roberson /*
119654d77689SJeff Roberson  * Initialise maxsockets
119754d77689SJeff Roberson  */
119854d77689SJeff Roberson static void init_maxsockets(void *ignored)
119954d77689SJeff Roberson {
120054d77689SJeff Roberson 	TUNABLE_INT_FETCH("kern.ipc.maxsockets", &maxsockets);
120154d77689SJeff Roberson 	maxsockets = imax(maxsockets, imax(maxfiles, nmbclusters));
120254d77689SJeff Roberson }
120354d77689SJeff Roberson SYSINIT(param, SI_SUB_TUNABLES, SI_ORDER_ANY, init_maxsockets, NULL);
1204