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 34df8bae1dSRodney W. Grimes */ 35df8bae1dSRodney W. Grimes 36677b542eSDavid E. O'Brien #include <sys/cdefs.h> 37677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 38677b542eSDavid E. O'Brien 39335654d7SRobert Watson #include "opt_mac.h" 405b86eac4SJesper Skriver #include "opt_param.h" 41335654d7SRobert Watson 42df8bae1dSRodney W. Grimes #include <sys/param.h> 43960ed29cSSeigo Tanimura #include <sys/aio.h> /* for aio_swake proto */ 4498271db4SGarrett Wollman #include <sys/domain.h> 45960ed29cSSeigo Tanimura #include <sys/event.h> 46134c934cSMike Smith #include <sys/file.h> /* for maxfiles */ 47ff5c09daSGarrett Wollman #include <sys/kernel.h> 48fb919e4dSMark Murray #include <sys/lock.h> 49335654d7SRobert Watson #include <sys/mac.h> 50f9d0d524SRobert Watson #include <sys/malloc.h> 51df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 52960ed29cSSeigo Tanimura #include <sys/mutex.h> 53fb919e4dSMark Murray #include <sys/proc.h> 54df8bae1dSRodney W. Grimes #include <sys/protosw.h> 552f9a2132SBrian Feldman #include <sys/resourcevar.h> 56960ed29cSSeigo Tanimura #include <sys/signalvar.h> 57df8bae1dSRodney W. Grimes #include <sys/socket.h> 58df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 59960ed29cSSeigo Tanimura #include <sys/stat.h> 60ff5c09daSGarrett Wollman #include <sys/sysctl.h> 61960ed29cSSeigo Tanimura #include <sys/systm.h> 6226f9a767SRodney W. Grimes 6354d77689SJeff Roberson int maxsockets; 6454d77689SJeff Roberson 6521d56e9cSAlfred Perlstein void (*aio_swake)(struct socket *, struct sockbuf *); 6621d56e9cSAlfred Perlstein 67df8bae1dSRodney W. Grimes /* 68df8bae1dSRodney W. Grimes * Primitive routines for operating on sockets and socket buffers 69df8bae1dSRodney W. Grimes */ 70df8bae1dSRodney W. Grimes 7179cb7eb4SDavid Greenman u_long sb_max = SB_MAX; 7251da11a2SMark Murray static u_long sb_max_adj = 7379cb7eb4SDavid Greenman SB_MAX * MCLBYTES / (MSIZE + MCLBYTES); /* adjusted sb_max */ 74df8bae1dSRodney W. Grimes 754b29bc4fSGarrett Wollman static u_long sb_efficiency = 8; /* parameter for sbreserve() */ 764b29bc4fSGarrett Wollman 77df8bae1dSRodney W. Grimes /* 78df8bae1dSRodney W. Grimes * Procedures to manipulate state flags of socket 79df8bae1dSRodney W. Grimes * and do appropriate wakeups. Normal sequence from the 80df8bae1dSRodney W. Grimes * active (originating) side is that soisconnecting() is 81df8bae1dSRodney W. Grimes * called during processing of connect() call, 82df8bae1dSRodney W. Grimes * resulting in an eventual call to soisconnected() if/when the 83df8bae1dSRodney W. Grimes * connection is established. When the connection is torn down 84df8bae1dSRodney W. Grimes * soisdisconnecting() is called during processing of disconnect() call, 85df8bae1dSRodney W. Grimes * and soisdisconnected() is called when the connection to the peer 86df8bae1dSRodney W. Grimes * is totally severed. The semantics of these routines are such that 87df8bae1dSRodney W. Grimes * connectionless protocols can call soisconnected() and soisdisconnected() 88df8bae1dSRodney W. Grimes * only, bypassing the in-progress calls when setting up a ``connection'' 89df8bae1dSRodney W. Grimes * takes no time. 90df8bae1dSRodney W. Grimes * 91df8bae1dSRodney W. Grimes * From the passive side, a socket is created with 92dc97381eSPeter Wemm * two queues of sockets: so_incomp for connections in progress 93dc97381eSPeter Wemm * and so_comp for connections already made and awaiting user acceptance. 94df8bae1dSRodney W. Grimes * As a protocol is preparing incoming connections, it creates a socket 95dc97381eSPeter Wemm * structure queued on so_incomp by calling sonewconn(). When the connection 96df8bae1dSRodney W. Grimes * is established, soisconnected() is called, and transfers the 97dc97381eSPeter Wemm * socket structure to so_comp, making it available to accept(). 98df8bae1dSRodney W. Grimes * 99df8bae1dSRodney W. Grimes * If a socket is closed with sockets on either 100dc97381eSPeter Wemm * so_incomp or so_comp, these sockets are dropped. 101df8bae1dSRodney W. Grimes * 102df8bae1dSRodney W. Grimes * If higher level protocols are implemented in 103df8bae1dSRodney W. Grimes * the kernel, the wakeups done here will sometimes 104df8bae1dSRodney W. Grimes * cause software-interrupt process scheduling. 105df8bae1dSRodney W. Grimes */ 106df8bae1dSRodney W. Grimes 10726f9a767SRodney W. Grimes void 108df8bae1dSRodney W. Grimes soisconnecting(so) 109df8bae1dSRodney W. Grimes register struct socket *so; 110df8bae1dSRodney W. Grimes { 111df8bae1dSRodney W. Grimes 112df8bae1dSRodney W. Grimes so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING); 113df8bae1dSRodney W. Grimes so->so_state |= SS_ISCONNECTING; 114df8bae1dSRodney W. Grimes } 115df8bae1dSRodney W. Grimes 11626f9a767SRodney W. Grimes void 117d48d4b25SSeigo Tanimura soisconnected(so) 118d48d4b25SSeigo Tanimura struct socket *so; 119d48d4b25SSeigo Tanimura { 1204cc20ab1SSeigo Tanimura struct socket *head = so->so_head; 121d48d4b25SSeigo Tanimura 122d48d4b25SSeigo Tanimura so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING); 123d48d4b25SSeigo Tanimura so->so_state |= SS_ISCONNECTED; 124d48d4b25SSeigo Tanimura if (head && (so->so_state & SS_INCOMP)) { 125d48d4b25SSeigo Tanimura if ((so->so_options & SO_ACCEPTFILTER) != 0) { 1264cc20ab1SSeigo Tanimura so->so_upcall = head->so_accf->so_accept_filter->accf_callback; 1274cc20ab1SSeigo Tanimura so->so_upcallarg = head->so_accf->so_accept_filter_arg; 128d48d4b25SSeigo Tanimura so->so_rcv.sb_flags |= SB_UPCALL; 129d48d4b25SSeigo Tanimura so->so_options &= ~SO_ACCEPTFILTER; 13086bb7316SPeter Wemm so->so_upcall(so, so->so_upcallarg, M_TRYWAIT); 131a79b7128SAlfred Perlstein return; 132a79b7128SAlfred Perlstein } 133be24e9e8SDavid Greenman TAILQ_REMOVE(&head->so_incomp, so, so_list); 134a51764a8SPaul Traina head->so_incqlen--; 1354cc20ab1SSeigo Tanimura so->so_state &= ~SS_INCOMP; 136be24e9e8SDavid Greenman TAILQ_INSERT_TAIL(&head->so_comp, so, so_list); 137e1f1827fSMike Silbersack head->so_qlen++; 1384cc20ab1SSeigo Tanimura so->so_state |= SS_COMP; 13903e49181SSeigo Tanimura sorwakeup(head); 140a91b8721SDavid Greenman wakeup_one(&head->so_timeo); 141df8bae1dSRodney W. Grimes } else { 142a91b8721SDavid Greenman wakeup(&so->so_timeo); 14303e49181SSeigo Tanimura sorwakeup(so); 14403e49181SSeigo Tanimura sowwakeup(so); 145df8bae1dSRodney W. Grimes } 146df8bae1dSRodney W. Grimes } 147df8bae1dSRodney W. Grimes 14826f9a767SRodney W. Grimes void 149df8bae1dSRodney W. Grimes soisdisconnecting(so) 150df8bae1dSRodney W. Grimes register struct socket *so; 151df8bae1dSRodney W. Grimes { 152df8bae1dSRodney W. Grimes 153df8bae1dSRodney W. Grimes so->so_state &= ~SS_ISCONNECTING; 154df8bae1dSRodney W. Grimes so->so_state |= (SS_ISDISCONNECTING|SS_CANTRCVMORE|SS_CANTSENDMORE); 15580208239SAlfred Perlstein wakeup(&so->so_timeo); 15603e49181SSeigo Tanimura sowwakeup(so); 15703e49181SSeigo Tanimura sorwakeup(so); 158df8bae1dSRodney W. Grimes } 159df8bae1dSRodney W. Grimes 16026f9a767SRodney W. Grimes void 161df8bae1dSRodney W. Grimes soisdisconnected(so) 162df8bae1dSRodney W. Grimes register struct socket *so; 163df8bae1dSRodney W. Grimes { 164df8bae1dSRodney W. Grimes 16503e49181SSeigo Tanimura so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING); 16603e49181SSeigo Tanimura so->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE|SS_ISDISCONNECTED); 16780208239SAlfred Perlstein wakeup(&so->so_timeo); 16825dec747SDavid Malone sbdrop(&so->so_snd, so->so_snd.sb_cc); 16903e49181SSeigo Tanimura sowwakeup(so); 17003e49181SSeigo Tanimura sorwakeup(so); 171df8bae1dSRodney W. Grimes } 172df8bae1dSRodney W. Grimes 173df8bae1dSRodney W. Grimes /* 174df8bae1dSRodney W. Grimes * When an attempt at a new connection is noted on a socket 175df8bae1dSRodney W. Grimes * which accepts connections, sonewconn is called. If the 176df8bae1dSRodney W. Grimes * connection is possible (subject to space constraints, etc.) 177df8bae1dSRodney W. Grimes * then we allocate a new structure, propoerly linked into the 178df8bae1dSRodney W. Grimes * data structure of the original socket, and return this. 179df8bae1dSRodney W. Grimes * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED. 180b1e4abd2SMatthew Dillon * 181b1e4abd2SMatthew Dillon * note: the ref count on the socket is 0 on return 182df8bae1dSRodney W. Grimes */ 183df8bae1dSRodney W. Grimes struct socket * 184548af278SBill Fenner sonewconn(head, connstatus) 185df8bae1dSRodney W. Grimes register struct socket *head; 186df8bae1dSRodney W. Grimes int connstatus; 187df8bae1dSRodney W. Grimes { 188df8bae1dSRodney W. Grimes register struct socket *so; 189df8bae1dSRodney W. Grimes 190ebb0cbeaSPaul Traina if (head->so_qlen > 3 * head->so_qlimit / 2) 191df8bae1dSRodney W. Grimes return ((struct socket *)0); 19298271db4SGarrett Wollman so = soalloc(0); 193df8bae1dSRodney W. Grimes if (so == NULL) 194df8bae1dSRodney W. Grimes return ((struct socket *)0); 195205b2b61SPeter Wemm if ((head->so_options & SO_ACCEPTFILTER) != 0) 196205b2b61SPeter Wemm connstatus = 0; 197be24e9e8SDavid Greenman so->so_head = head; 198df8bae1dSRodney W. Grimes so->so_type = head->so_type; 199df8bae1dSRodney W. Grimes so->so_options = head->so_options &~ SO_ACCEPTCONN; 200df8bae1dSRodney W. Grimes so->so_linger = head->so_linger; 201df8bae1dSRodney W. Grimes so->so_state = head->so_state | SS_NOFDREF; 202df8bae1dSRodney W. Grimes so->so_proto = head->so_proto; 203df8bae1dSRodney W. Grimes so->so_timeo = head->so_timeo; 204bd78ceceSJohn Baldwin so->so_cred = crhold(head->so_cred); 205335654d7SRobert Watson #ifdef MAC 206335654d7SRobert Watson mac_create_socket_from_socket(head, so); 207335654d7SRobert Watson #endif 2082f9a2132SBrian Feldman if (soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat) || 2092f9a2132SBrian Feldman (*so->so_proto->pr_usrreqs->pru_attach)(so, 0, NULL)) { 2105ee0a409SAlan Cox sodealloc(so); 211b1396a35SGarrett Wollman return ((struct socket *)0); 212b1396a35SGarrett Wollman } 213b1396a35SGarrett Wollman 214be24e9e8SDavid Greenman if (connstatus) { 215be24e9e8SDavid Greenman TAILQ_INSERT_TAIL(&head->so_comp, so, so_list); 216be24e9e8SDavid Greenman so->so_state |= SS_COMP; 217e1f1827fSMike Silbersack head->so_qlen++; 218be24e9e8SDavid Greenman } else { 219184fec1aSMike Silbersack if (head->so_incqlen > head->so_qlimit) { 220e1f1827fSMike Silbersack struct socket *sp; 221e1f1827fSMike Silbersack sp = TAILQ_FIRST(&head->so_incomp); 222e1f1827fSMike Silbersack (void) soabort(sp); 223e1f1827fSMike Silbersack } 224be24e9e8SDavid Greenman TAILQ_INSERT_TAIL(&head->so_incomp, so, so_list); 225be24e9e8SDavid Greenman so->so_state |= SS_INCOMP; 226ebb0cbeaSPaul Traina head->so_incqlen++; 227be24e9e8SDavid Greenman } 228df8bae1dSRodney W. Grimes if (connstatus) { 22903e49181SSeigo Tanimura sorwakeup(head); 23080208239SAlfred Perlstein wakeup(&head->so_timeo); 231acbbcc5fSSeigo Tanimura so->so_state |= connstatus; 232df8bae1dSRodney W. Grimes } 233df8bae1dSRodney W. Grimes return (so); 234df8bae1dSRodney W. Grimes } 235df8bae1dSRodney W. Grimes 236df8bae1dSRodney W. Grimes /* 237df8bae1dSRodney W. Grimes * Socantsendmore indicates that no more data will be sent on the 238df8bae1dSRodney W. Grimes * socket; it would normally be applied to a socket when the user 239df8bae1dSRodney W. Grimes * informs the system that no more data is to be sent, by the protocol 240df8bae1dSRodney W. Grimes * code (in case PRU_SHUTDOWN). Socantrcvmore indicates that no more data 241df8bae1dSRodney W. Grimes * will be received, and will normally be applied to the socket by a 242df8bae1dSRodney W. Grimes * protocol when it detects that the peer will send no more data. 243df8bae1dSRodney W. Grimes * Data queued for reading in the socket may yet be read. 244df8bae1dSRodney W. Grimes */ 245df8bae1dSRodney W. Grimes 24626f9a767SRodney W. Grimes void 247df8bae1dSRodney W. Grimes socantsendmore(so) 248df8bae1dSRodney W. Grimes struct socket *so; 249df8bae1dSRodney W. Grimes { 250df8bae1dSRodney W. Grimes 251df8bae1dSRodney W. Grimes so->so_state |= SS_CANTSENDMORE; 25203e49181SSeigo Tanimura sowwakeup(so); 253df8bae1dSRodney W. Grimes } 254df8bae1dSRodney W. Grimes 25526f9a767SRodney W. Grimes void 256df8bae1dSRodney W. Grimes socantrcvmore(so) 257df8bae1dSRodney W. Grimes struct socket *so; 258df8bae1dSRodney W. Grimes { 259df8bae1dSRodney W. Grimes 260df8bae1dSRodney W. Grimes so->so_state |= SS_CANTRCVMORE; 26103e49181SSeigo Tanimura sorwakeup(so); 262df8bae1dSRodney W. Grimes } 263df8bae1dSRodney W. Grimes 264df8bae1dSRodney W. Grimes /* 265df8bae1dSRodney W. Grimes * Wait for data to arrive at/drain from a socket buffer. 266df8bae1dSRodney W. Grimes */ 26726f9a767SRodney W. Grimes int 268df8bae1dSRodney W. Grimes sbwait(sb) 269df8bae1dSRodney W. Grimes struct sockbuf *sb; 270df8bae1dSRodney W. Grimes { 271df8bae1dSRodney W. Grimes 272df8bae1dSRodney W. Grimes sb->sb_flags |= SB_WAIT; 27380208239SAlfred Perlstein return (tsleep(&sb->sb_cc, 27447daf5d5SBruce Evans (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH, "sbwait", 275df8bae1dSRodney W. Grimes sb->sb_timeo)); 276df8bae1dSRodney W. Grimes } 277df8bae1dSRodney W. Grimes 278df8bae1dSRodney W. Grimes /* 279df8bae1dSRodney W. Grimes * Lock a sockbuf already known to be locked; 280df8bae1dSRodney W. Grimes * return any error returned from sleep (EINTR). 281df8bae1dSRodney W. Grimes */ 28226f9a767SRodney W. Grimes int 283df8bae1dSRodney W. Grimes sb_lock(sb) 284df8bae1dSRodney W. Grimes register struct sockbuf *sb; 285df8bae1dSRodney W. Grimes { 286df8bae1dSRodney W. Grimes int error; 287df8bae1dSRodney W. Grimes 288df8bae1dSRodney W. Grimes while (sb->sb_flags & SB_LOCK) { 289df8bae1dSRodney W. Grimes sb->sb_flags |= SB_WANT; 29080208239SAlfred Perlstein error = tsleep(&sb->sb_flags, 291df8bae1dSRodney W. Grimes (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK|PCATCH, 29247daf5d5SBruce Evans "sblock", 0); 293797f2d22SPoul-Henning Kamp if (error) 294df8bae1dSRodney W. Grimes return (error); 295df8bae1dSRodney W. Grimes } 296df8bae1dSRodney W. Grimes sb->sb_flags |= SB_LOCK; 297df8bae1dSRodney W. Grimes return (0); 298df8bae1dSRodney W. Grimes } 299df8bae1dSRodney W. Grimes 300df8bae1dSRodney W. Grimes /* 301df8bae1dSRodney W. Grimes * Wakeup processes waiting on a socket buffer. 302df8bae1dSRodney W. Grimes * Do asynchronous notification via SIGIO 303df8bae1dSRodney W. Grimes * if the socket has the SS_ASYNC flag set. 304df8bae1dSRodney W. Grimes */ 30526f9a767SRodney W. Grimes void 306df8bae1dSRodney W. Grimes sowakeup(so, sb) 307df8bae1dSRodney W. Grimes register struct socket *so; 308df8bae1dSRodney W. Grimes register struct sockbuf *sb; 309df8bae1dSRodney W. Grimes { 310d48d4b25SSeigo Tanimura 311df8bae1dSRodney W. Grimes selwakeup(&sb->sb_sel); 312df8bae1dSRodney W. Grimes sb->sb_flags &= ~SB_SEL; 313df8bae1dSRodney W. Grimes if (sb->sb_flags & SB_WAIT) { 314df8bae1dSRodney W. Grimes sb->sb_flags &= ~SB_WAIT; 31580208239SAlfred Perlstein wakeup(&sb->sb_cc); 316df8bae1dSRodney W. Grimes } 3174cc20ab1SSeigo Tanimura if ((so->so_state & SS_ASYNC) && so->so_sigio != NULL) 318f1320723SAlfred Perlstein pgsigio(&so->so_sigio, SIGIO, 0); 3194cc20ab1SSeigo Tanimura if (sb->sb_flags & SB_UPCALL) 320a163d034SWarner Losh (*so->so_upcall)(so, so->so_upcallarg, M_DONTWAIT); 3214cc20ab1SSeigo Tanimura if (sb->sb_flags & SB_AIO) 322bfbbc4aaSJason Evans aio_swake(so, sb); 323cb679c38SJonathan Lemon KNOTE(&sb->sb_sel.si_note, 0); 324df8bae1dSRodney W. Grimes } 325df8bae1dSRodney W. Grimes 326df8bae1dSRodney W. Grimes /* 327df8bae1dSRodney W. Grimes * Socket buffer (struct sockbuf) utility routines. 328df8bae1dSRodney W. Grimes * 329df8bae1dSRodney W. Grimes * Each socket contains two socket buffers: one for sending data and 330df8bae1dSRodney W. Grimes * one for receiving data. Each buffer contains a queue of mbufs, 331df8bae1dSRodney W. Grimes * information about the number of mbufs and amount of data in the 332df8bae1dSRodney W. Grimes * queue, and other fields allowing select() statements and notification 333df8bae1dSRodney W. Grimes * on data availability to be implemented. 334df8bae1dSRodney W. Grimes * 335df8bae1dSRodney W. Grimes * Data stored in a socket buffer is maintained as a list of records. 336df8bae1dSRodney W. Grimes * Each record is a list of mbufs chained together with the m_next 337df8bae1dSRodney W. Grimes * field. Records are chained together with the m_nextpkt field. The upper 338df8bae1dSRodney W. Grimes * level routine soreceive() expects the following conventions to be 339df8bae1dSRodney W. Grimes * observed when placing information in the receive buffer: 340df8bae1dSRodney W. Grimes * 341df8bae1dSRodney W. Grimes * 1. If the protocol requires each message be preceded by the sender's 342df8bae1dSRodney W. Grimes * name, then a record containing that name must be present before 343df8bae1dSRodney W. Grimes * any associated data (mbuf's must be of type MT_SONAME). 344df8bae1dSRodney W. Grimes * 2. If the protocol supports the exchange of ``access rights'' (really 345df8bae1dSRodney W. Grimes * just additional data associated with the message), and there are 346df8bae1dSRodney W. Grimes * ``rights'' to be received, then a record containing this data 347df8bae1dSRodney W. Grimes * should be present (mbuf's must be of type MT_RIGHTS). 348df8bae1dSRodney W. Grimes * 3. If a name or rights record exists, then it must be followed by 349df8bae1dSRodney W. Grimes * a data record, perhaps of zero length. 350df8bae1dSRodney W. Grimes * 351df8bae1dSRodney W. Grimes * Before using a new socket structure it is first necessary to reserve 352df8bae1dSRodney W. Grimes * buffer space to the socket, by calling sbreserve(). This should commit 353df8bae1dSRodney W. Grimes * some of the available buffer space in the system buffer pool for the 354df8bae1dSRodney W. Grimes * socket (currently, it does nothing but enforce limits). The space 355df8bae1dSRodney W. Grimes * should be released by calling sbrelease() when the socket is destroyed. 356df8bae1dSRodney W. Grimes */ 357df8bae1dSRodney W. Grimes 35826f9a767SRodney W. Grimes int 359df8bae1dSRodney W. Grimes soreserve(so, sndcc, rcvcc) 360df8bae1dSRodney W. Grimes register struct socket *so; 361df8bae1dSRodney W. Grimes u_long sndcc, rcvcc; 362df8bae1dSRodney W. Grimes { 363b40ce416SJulian Elischer struct thread *td = curthread; 364df8bae1dSRodney W. Grimes 365b40ce416SJulian Elischer if (sbreserve(&so->so_snd, sndcc, so, td) == 0) 366df8bae1dSRodney W. Grimes goto bad; 367b40ce416SJulian Elischer if (sbreserve(&so->so_rcv, rcvcc, so, td) == 0) 368df8bae1dSRodney W. Grimes goto bad2; 369df8bae1dSRodney W. Grimes if (so->so_rcv.sb_lowat == 0) 370df8bae1dSRodney W. Grimes so->so_rcv.sb_lowat = 1; 371df8bae1dSRodney W. Grimes if (so->so_snd.sb_lowat == 0) 372df8bae1dSRodney W. Grimes so->so_snd.sb_lowat = MCLBYTES; 373df8bae1dSRodney W. Grimes if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat) 374df8bae1dSRodney W. Grimes so->so_snd.sb_lowat = so->so_snd.sb_hiwat; 375df8bae1dSRodney W. Grimes return (0); 376df8bae1dSRodney W. Grimes bad2: 377ecf72308SBrian Feldman sbrelease(&so->so_snd, so); 378df8bae1dSRodney W. Grimes bad: 379df8bae1dSRodney W. Grimes return (ENOBUFS); 380df8bae1dSRodney W. Grimes } 381df8bae1dSRodney W. Grimes 38279cb7eb4SDavid Greenman static int 38379cb7eb4SDavid Greenman sysctl_handle_sb_max(SYSCTL_HANDLER_ARGS) 38479cb7eb4SDavid Greenman { 38579cb7eb4SDavid Greenman int error = 0; 38679cb7eb4SDavid Greenman u_long old_sb_max = sb_max; 38779cb7eb4SDavid Greenman 3881b978d45SHartmut Brandt error = SYSCTL_OUT(req, arg1, sizeof(u_long)); 38979cb7eb4SDavid Greenman if (error || !req->newptr) 39079cb7eb4SDavid Greenman return (error); 3911b978d45SHartmut Brandt error = SYSCTL_IN(req, arg1, sizeof(u_long)); 39279cb7eb4SDavid Greenman if (error) 39379cb7eb4SDavid Greenman return (error); 39479cb7eb4SDavid Greenman if (sb_max < MSIZE + MCLBYTES) { 39579cb7eb4SDavid Greenman sb_max = old_sb_max; 39679cb7eb4SDavid Greenman return (EINVAL); 39779cb7eb4SDavid Greenman } 39879cb7eb4SDavid Greenman sb_max_adj = (u_quad_t)sb_max * MCLBYTES / (MSIZE + MCLBYTES); 39979cb7eb4SDavid Greenman return (0); 40079cb7eb4SDavid Greenman } 40179cb7eb4SDavid Greenman 402df8bae1dSRodney W. Grimes /* 403df8bae1dSRodney W. Grimes * Allot mbufs to a sockbuf. 404df8bae1dSRodney W. Grimes * Attempt to scale mbmax so that mbcnt doesn't become limiting 405df8bae1dSRodney W. Grimes * if buffering efficiency is near the normal case. 406df8bae1dSRodney W. Grimes */ 40726f9a767SRodney W. Grimes int 408b40ce416SJulian Elischer sbreserve(sb, cc, so, td) 409df8bae1dSRodney W. Grimes struct sockbuf *sb; 410df8bae1dSRodney W. Grimes u_long cc; 411ecf72308SBrian Feldman struct socket *so; 412b40ce416SJulian Elischer struct thread *td; 413df8bae1dSRodney W. Grimes { 414ecf72308SBrian Feldman 415ecf72308SBrian Feldman /* 416b40ce416SJulian Elischer * td will only be NULL when we're in an interrupt 417ecf72308SBrian Feldman * (e.g. in tcp_input()) 418ecf72308SBrian Feldman */ 41979cb7eb4SDavid Greenman if (cc > sb_max_adj) 420df8bae1dSRodney W. Grimes return (0); 421f535380cSDon Lewis if (!chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, cc, 422b40ce416SJulian Elischer td ? td->td_proc->p_rlimit[RLIMIT_SBSIZE].rlim_cur : RLIM_INFINITY)) { 423ecf72308SBrian Feldman return (0); 424c6362551SAlfred Perlstein } 4254b29bc4fSGarrett Wollman sb->sb_mbmax = min(cc * sb_efficiency, sb_max); 426df8bae1dSRodney W. Grimes if (sb->sb_lowat > sb->sb_hiwat) 427df8bae1dSRodney W. Grimes sb->sb_lowat = sb->sb_hiwat; 428df8bae1dSRodney W. Grimes return (1); 429df8bae1dSRodney W. Grimes } 430df8bae1dSRodney W. Grimes 431df8bae1dSRodney W. Grimes /* 432df8bae1dSRodney W. Grimes * Free mbufs held by a socket, and reserved mbuf space. 433df8bae1dSRodney W. Grimes */ 43426f9a767SRodney W. Grimes void 435ecf72308SBrian Feldman sbrelease(sb, so) 436df8bae1dSRodney W. Grimes struct sockbuf *sb; 437ecf72308SBrian Feldman struct socket *so; 438df8bae1dSRodney W. Grimes { 439df8bae1dSRodney W. Grimes 440df8bae1dSRodney W. Grimes sbflush(sb); 441f535380cSDon Lewis (void)chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, 0, 442f535380cSDon Lewis RLIM_INFINITY); 4436aef685fSBrian Feldman sb->sb_mbmax = 0; 444df8bae1dSRodney W. Grimes } 445df8bae1dSRodney W. Grimes 446df8bae1dSRodney W. Grimes /* 447df8bae1dSRodney W. Grimes * Routines to add and remove 448df8bae1dSRodney W. Grimes * data from an mbuf queue. 449df8bae1dSRodney W. Grimes * 450df8bae1dSRodney W. Grimes * The routines sbappend() or sbappendrecord() are normally called to 451df8bae1dSRodney W. Grimes * append new mbufs to a socket buffer, after checking that adequate 452df8bae1dSRodney W. Grimes * space is available, comparing the function sbspace() with the amount 453df8bae1dSRodney W. Grimes * of data to be added. sbappendrecord() differs from sbappend() in 454df8bae1dSRodney W. Grimes * that data supplied is treated as the beginning of a new record. 455df8bae1dSRodney W. Grimes * To place a sender's address, optional access rights, and data in a 456df8bae1dSRodney W. Grimes * socket receive buffer, sbappendaddr() should be used. To place 457df8bae1dSRodney W. Grimes * access rights and data in a socket receive buffer, sbappendrights() 458df8bae1dSRodney W. Grimes * should be used. In either case, the new data begins a new record. 459df8bae1dSRodney W. Grimes * Note that unlike sbappend() and sbappendrecord(), these routines check 460df8bae1dSRodney W. Grimes * for the caller that there will be enough space to store the data. 461df8bae1dSRodney W. Grimes * Each fails if there is not enough space, or if it cannot find mbufs 462df8bae1dSRodney W. Grimes * to store additional information in. 463df8bae1dSRodney W. Grimes * 464df8bae1dSRodney W. Grimes * Reliable protocols may use the socket send buffer to hold data 465df8bae1dSRodney W. Grimes * awaiting acknowledgement. Data is normally copied from a socket 466df8bae1dSRodney W. Grimes * send buffer in a protocol with m_copy for output to a peer, 467df8bae1dSRodney W. Grimes * and then removing the data from the socket buffer with sbdrop() 468df8bae1dSRodney W. Grimes * or sbdroprecord() when the data is acknowledged by the peer. 469df8bae1dSRodney W. Grimes */ 470df8bae1dSRodney W. Grimes 471395bb186SSam Leffler #ifdef SOCKBUF_DEBUG 472395bb186SSam Leffler void 473395bb186SSam Leffler sblastrecordchk(struct sockbuf *sb, const char *file, int line) 474395bb186SSam Leffler { 475395bb186SSam Leffler struct mbuf *m = sb->sb_mb; 476395bb186SSam Leffler 477395bb186SSam Leffler while (m && m->m_nextpkt) 478395bb186SSam Leffler m = m->m_nextpkt; 479395bb186SSam Leffler 480395bb186SSam Leffler if (m != sb->sb_lastrecord) { 481395bb186SSam Leffler printf("%s: sb_mb %p sb_lastrecord %p last %p\n", 482395bb186SSam Leffler __func__, sb->sb_mb, sb->sb_lastrecord, m); 483395bb186SSam Leffler printf("packet chain:\n"); 484395bb186SSam Leffler for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt) 485395bb186SSam Leffler printf("\t%p\n", m); 486395bb186SSam Leffler panic("%s from %s:%u", __func__, file, line); 487395bb186SSam Leffler } 488395bb186SSam Leffler } 489395bb186SSam Leffler 490395bb186SSam Leffler void 491395bb186SSam Leffler sblastmbufchk(struct sockbuf *sb, const char *file, int line) 492395bb186SSam Leffler { 493395bb186SSam Leffler struct mbuf *m = sb->sb_mb; 494395bb186SSam Leffler struct mbuf *n; 495395bb186SSam Leffler 496395bb186SSam Leffler while (m && m->m_nextpkt) 497395bb186SSam Leffler m = m->m_nextpkt; 498395bb186SSam Leffler 499395bb186SSam Leffler while (m && m->m_next) 500395bb186SSam Leffler m = m->m_next; 501395bb186SSam Leffler 502395bb186SSam Leffler if (m != sb->sb_mbtail) { 503395bb186SSam Leffler printf("%s: sb_mb %p sb_mbtail %p last %p\n", 504395bb186SSam Leffler __func__, sb->sb_mb, sb->sb_mbtail, m); 505395bb186SSam Leffler printf("packet tree:\n"); 506395bb186SSam Leffler for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt) { 507395bb186SSam Leffler printf("\t"); 508395bb186SSam Leffler for (n = m; n != NULL; n = n->m_next) 509395bb186SSam Leffler printf("%p ", n); 510395bb186SSam Leffler printf("\n"); 511395bb186SSam Leffler } 512395bb186SSam Leffler panic("%s from %s:%u", __func__, file, line); 513395bb186SSam Leffler } 514395bb186SSam Leffler } 515395bb186SSam Leffler #endif /* SOCKBUF_DEBUG */ 516395bb186SSam Leffler 517395bb186SSam Leffler #define SBLINKRECORD(sb, m0) do { \ 518395bb186SSam Leffler if ((sb)->sb_lastrecord != NULL) \ 519395bb186SSam Leffler (sb)->sb_lastrecord->m_nextpkt = (m0); \ 520395bb186SSam Leffler else \ 521395bb186SSam Leffler (sb)->sb_mb = (m0); \ 522395bb186SSam Leffler (sb)->sb_lastrecord = (m0); \ 523395bb186SSam Leffler } while (/*CONSTCOND*/0) 524395bb186SSam Leffler 525df8bae1dSRodney W. Grimes /* 526df8bae1dSRodney W. Grimes * Append mbuf chain m to the last record in the 527df8bae1dSRodney W. Grimes * socket buffer sb. The additional space associated 528df8bae1dSRodney W. Grimes * the mbuf chain is recorded in sb. Empty mbufs are 529df8bae1dSRodney W. Grimes * discarded and mbufs are compacted where possible. 530df8bae1dSRodney W. Grimes */ 53126f9a767SRodney W. Grimes void 532df8bae1dSRodney W. Grimes sbappend(sb, m) 533df8bae1dSRodney W. Grimes struct sockbuf *sb; 534df8bae1dSRodney W. Grimes struct mbuf *m; 535df8bae1dSRodney W. Grimes { 536df8bae1dSRodney W. Grimes register struct mbuf *n; 537df8bae1dSRodney W. Grimes 538df8bae1dSRodney W. Grimes if (m == 0) 539df8bae1dSRodney W. Grimes return; 540395bb186SSam Leffler SBLASTRECORDCHK(sb); 541797f2d22SPoul-Henning Kamp n = sb->sb_mb; 542797f2d22SPoul-Henning Kamp if (n) { 543df8bae1dSRodney W. Grimes while (n->m_nextpkt) 544df8bae1dSRodney W. Grimes n = n->m_nextpkt; 545df8bae1dSRodney W. Grimes do { 546df8bae1dSRodney W. Grimes if (n->m_flags & M_EOR) { 547df8bae1dSRodney W. Grimes sbappendrecord(sb, m); /* XXXXXX!!!! */ 548df8bae1dSRodney W. Grimes return; 549df8bae1dSRodney W. Grimes } 550df8bae1dSRodney W. Grimes } while (n->m_next && (n = n->m_next)); 551395bb186SSam Leffler } else { 552395bb186SSam Leffler /* 553395bb186SSam Leffler * XXX Would like to simply use sb_mbtail here, but 554395bb186SSam Leffler * XXX I need to verify that I won't miss an EOR that 555395bb186SSam Leffler * XXX way. 556395bb186SSam Leffler */ 557395bb186SSam Leffler if ((n = sb->sb_lastrecord) != NULL) { 558395bb186SSam Leffler do { 559395bb186SSam Leffler if (n->m_flags & M_EOR) { 560395bb186SSam Leffler sbappendrecord(sb, m); /* XXXXXX!!!! */ 561395bb186SSam Leffler return; 562395bb186SSam Leffler } 563395bb186SSam Leffler } while (n->m_next && (n = n->m_next)); 564395bb186SSam Leffler } else { 565395bb186SSam Leffler /* 566395bb186SSam Leffler * If this is the first record in the socket buffer, 567395bb186SSam Leffler * it's also the last record. 568395bb186SSam Leffler */ 569395bb186SSam Leffler sb->sb_lastrecord = m; 570395bb186SSam Leffler } 571df8bae1dSRodney W. Grimes } 572df8bae1dSRodney W. Grimes sbcompress(sb, m, n); 573395bb186SSam Leffler SBLASTRECORDCHK(sb); 574395bb186SSam Leffler } 575395bb186SSam Leffler 576395bb186SSam Leffler /* 577395bb186SSam Leffler * This version of sbappend() should only be used when the caller 578395bb186SSam Leffler * absolutely knows that there will never be more than one record 579395bb186SSam Leffler * in the socket buffer, that is, a stream protocol (such as TCP). 580395bb186SSam Leffler */ 581395bb186SSam Leffler void 582395bb186SSam Leffler sbappendstream(struct sockbuf *sb, struct mbuf *m) 583395bb186SSam Leffler { 584395bb186SSam Leffler 585395bb186SSam Leffler KASSERT(m->m_nextpkt == NULL,("sbappendstream 0")); 586395bb186SSam Leffler KASSERT(sb->sb_mb == sb->sb_lastrecord,("sbappendstream 1")); 587395bb186SSam Leffler 588395bb186SSam Leffler SBLASTMBUFCHK(sb); 589395bb186SSam Leffler 590395bb186SSam Leffler #ifdef MBUFTRACE 591395bb186SSam Leffler m_claim(m, sb->sb_mowner); 592395bb186SSam Leffler #endif 593395bb186SSam Leffler 594395bb186SSam Leffler sbcompress(sb, m, sb->sb_mbtail); 595395bb186SSam Leffler 596395bb186SSam Leffler sb->sb_lastrecord = sb->sb_mb; 597395bb186SSam Leffler SBLASTRECORDCHK(sb); 598df8bae1dSRodney W. Grimes } 599df8bae1dSRodney W. Grimes 600df8bae1dSRodney W. Grimes #ifdef SOCKBUF_DEBUG 60126f9a767SRodney W. Grimes void 602df8bae1dSRodney W. Grimes sbcheck(sb) 6037ed60de8SPoul-Henning Kamp struct sockbuf *sb; 604df8bae1dSRodney W. Grimes { 6057ed60de8SPoul-Henning Kamp struct mbuf *m; 6067ed60de8SPoul-Henning Kamp struct mbuf *n = 0; 6077ed60de8SPoul-Henning Kamp u_long len = 0, mbcnt = 0; 608df8bae1dSRodney W. Grimes 6090931333fSBill Fenner for (m = sb->sb_mb; m; m = n) { 6100931333fSBill Fenner n = m->m_nextpkt; 6110931333fSBill Fenner for (; m; m = m->m_next) { 612df8bae1dSRodney W. Grimes len += m->m_len; 613df8bae1dSRodney W. Grimes mbcnt += MSIZE; 614313861b8SJulian Elischer if (m->m_flags & M_EXT) /*XXX*/ /* pretty sure this is bogus */ 615df8bae1dSRodney W. Grimes mbcnt += m->m_ext.ext_size; 6160931333fSBill Fenner } 617df8bae1dSRodney W. Grimes } 618df8bae1dSRodney W. Grimes if (len != sb->sb_cc || mbcnt != sb->sb_mbcnt) { 619395bb186SSam Leffler printf("cc %ld != %u || mbcnt %ld != %u\n", len, sb->sb_cc, 620df8bae1dSRodney W. Grimes mbcnt, sb->sb_mbcnt); 621df8bae1dSRodney W. Grimes panic("sbcheck"); 622df8bae1dSRodney W. Grimes } 623df8bae1dSRodney W. Grimes } 624df8bae1dSRodney W. Grimes #endif 625df8bae1dSRodney W. Grimes 626df8bae1dSRodney W. Grimes /* 627df8bae1dSRodney W. Grimes * As above, except the mbuf chain 628df8bae1dSRodney W. Grimes * begins a new record. 629df8bae1dSRodney W. Grimes */ 63026f9a767SRodney W. Grimes void 631df8bae1dSRodney W. Grimes sbappendrecord(sb, m0) 632df8bae1dSRodney W. Grimes register struct sockbuf *sb; 633df8bae1dSRodney W. Grimes register struct mbuf *m0; 634df8bae1dSRodney W. Grimes { 635df8bae1dSRodney W. Grimes register struct mbuf *m; 636df8bae1dSRodney W. Grimes 637df8bae1dSRodney W. Grimes if (m0 == 0) 638df8bae1dSRodney W. Grimes return; 639797f2d22SPoul-Henning Kamp m = sb->sb_mb; 640797f2d22SPoul-Henning Kamp if (m) 641df8bae1dSRodney W. Grimes while (m->m_nextpkt) 642df8bae1dSRodney W. Grimes m = m->m_nextpkt; 643df8bae1dSRodney W. Grimes /* 644df8bae1dSRodney W. Grimes * Put the first mbuf on the queue. 645df8bae1dSRodney W. Grimes * Note this permits zero length records. 646df8bae1dSRodney W. Grimes */ 647df8bae1dSRodney W. Grimes sballoc(sb, m0); 648395bb186SSam Leffler SBLASTRECORDCHK(sb); 649395bb186SSam Leffler SBLINKRECORD(sb, m0); 650df8bae1dSRodney W. Grimes if (m) 651df8bae1dSRodney W. Grimes m->m_nextpkt = m0; 652df8bae1dSRodney W. Grimes else 653df8bae1dSRodney W. Grimes sb->sb_mb = m0; 654df8bae1dSRodney W. Grimes m = m0->m_next; 655df8bae1dSRodney W. Grimes m0->m_next = 0; 656df8bae1dSRodney W. Grimes if (m && (m0->m_flags & M_EOR)) { 657df8bae1dSRodney W. Grimes m0->m_flags &= ~M_EOR; 658df8bae1dSRodney W. Grimes m->m_flags |= M_EOR; 659df8bae1dSRodney W. Grimes } 660df8bae1dSRodney W. Grimes sbcompress(sb, m, m0); 661df8bae1dSRodney W. Grimes } 662df8bae1dSRodney W. Grimes 663df8bae1dSRodney W. Grimes /* 664df8bae1dSRodney W. Grimes * As above except that OOB data 665df8bae1dSRodney W. Grimes * is inserted at the beginning of the sockbuf, 666df8bae1dSRodney W. Grimes * but after any other OOB data. 667df8bae1dSRodney W. Grimes */ 66826f9a767SRodney W. Grimes void 669df8bae1dSRodney W. Grimes sbinsertoob(sb, m0) 670df8bae1dSRodney W. Grimes register struct sockbuf *sb; 671df8bae1dSRodney W. Grimes register struct mbuf *m0; 672df8bae1dSRodney W. Grimes { 673df8bae1dSRodney W. Grimes register struct mbuf *m; 674df8bae1dSRodney W. Grimes register struct mbuf **mp; 675df8bae1dSRodney W. Grimes 676df8bae1dSRodney W. Grimes if (m0 == 0) 677df8bae1dSRodney W. Grimes return; 678797f2d22SPoul-Henning Kamp for (mp = &sb->sb_mb; *mp ; mp = &((*mp)->m_nextpkt)) { 679797f2d22SPoul-Henning Kamp m = *mp; 680df8bae1dSRodney W. Grimes again: 681df8bae1dSRodney W. Grimes switch (m->m_type) { 682df8bae1dSRodney W. Grimes 683df8bae1dSRodney W. Grimes case MT_OOBDATA: 684df8bae1dSRodney W. Grimes continue; /* WANT next train */ 685df8bae1dSRodney W. Grimes 686df8bae1dSRodney W. Grimes case MT_CONTROL: 687797f2d22SPoul-Henning Kamp m = m->m_next; 688797f2d22SPoul-Henning Kamp if (m) 689df8bae1dSRodney W. Grimes goto again; /* inspect THIS train further */ 690df8bae1dSRodney W. Grimes } 691df8bae1dSRodney W. Grimes break; 692df8bae1dSRodney W. Grimes } 693df8bae1dSRodney W. Grimes /* 694df8bae1dSRodney W. Grimes * Put the first mbuf on the queue. 695df8bae1dSRodney W. Grimes * Note this permits zero length records. 696df8bae1dSRodney W. Grimes */ 697df8bae1dSRodney W. Grimes sballoc(sb, m0); 698df8bae1dSRodney W. Grimes m0->m_nextpkt = *mp; 699df8bae1dSRodney W. Grimes *mp = m0; 700df8bae1dSRodney W. Grimes m = m0->m_next; 701df8bae1dSRodney W. Grimes m0->m_next = 0; 702df8bae1dSRodney W. Grimes if (m && (m0->m_flags & M_EOR)) { 703df8bae1dSRodney W. Grimes m0->m_flags &= ~M_EOR; 704df8bae1dSRodney W. Grimes m->m_flags |= M_EOR; 705df8bae1dSRodney W. Grimes } 706df8bae1dSRodney W. Grimes sbcompress(sb, m, m0); 707df8bae1dSRodney W. Grimes } 708df8bae1dSRodney W. Grimes 709df8bae1dSRodney W. Grimes /* 710df8bae1dSRodney W. Grimes * Append address and data, and optionally, control (ancillary) data 711df8bae1dSRodney W. Grimes * to the receive queue of a socket. If present, 712df8bae1dSRodney W. Grimes * m0 must include a packet header with total length. 713df8bae1dSRodney W. Grimes * Returns 0 if no space in sockbuf or insufficient mbufs. 714df8bae1dSRodney W. Grimes */ 71526f9a767SRodney W. Grimes int 716df8bae1dSRodney W. Grimes sbappendaddr(sb, asa, m0, control) 7177ed60de8SPoul-Henning Kamp struct sockbuf *sb; 718df8bae1dSRodney W. Grimes struct sockaddr *asa; 719df8bae1dSRodney W. Grimes struct mbuf *m0, *control; 720df8bae1dSRodney W. Grimes { 721395bb186SSam Leffler struct mbuf *m, *n, *nlast; 722df8bae1dSRodney W. Grimes int space = asa->sa_len; 723df8bae1dSRodney W. Grimes 724df8bae1dSRodney W. Grimes if (m0 && (m0->m_flags & M_PKTHDR) == 0) 725df8bae1dSRodney W. Grimes panic("sbappendaddr"); 726df8bae1dSRodney W. Grimes if (m0) 727df8bae1dSRodney W. Grimes space += m0->m_pkthdr.len; 7287ed60de8SPoul-Henning Kamp space += m_length(control, &n); 729df8bae1dSRodney W. Grimes if (space > sbspace(sb)) 730df8bae1dSRodney W. Grimes return (0); 731c43cad1aSScott Long #if MSIZE <= 256 732df8bae1dSRodney W. Grimes if (asa->sa_len > MLEN) 733df8bae1dSRodney W. Grimes return (0); 734c43cad1aSScott Long #endif 735a163d034SWarner Losh MGET(m, M_DONTWAIT, MT_SONAME); 736df8bae1dSRodney W. Grimes if (m == 0) 737df8bae1dSRodney W. Grimes return (0); 738df8bae1dSRodney W. Grimes m->m_len = asa->sa_len; 73980208239SAlfred Perlstein bcopy(asa, mtod(m, caddr_t), asa->sa_len); 740df8bae1dSRodney W. Grimes if (n) 741df8bae1dSRodney W. Grimes n->m_next = m0; /* concatenate data to control */ 742df8bae1dSRodney W. Grimes else 743df8bae1dSRodney W. Grimes control = m0; 744df8bae1dSRodney W. Grimes m->m_next = control; 745395bb186SSam Leffler for (n = m; n->m_next != NULL; n = n->m_next) 746df8bae1dSRodney W. Grimes sballoc(sb, n); 747395bb186SSam Leffler sballoc(sb, n); 748395bb186SSam Leffler nlast = n; 749395bb186SSam Leffler SBLINKRECORD(sb, m); 750395bb186SSam Leffler 751395bb186SSam Leffler sb->sb_mbtail = nlast; 752395bb186SSam Leffler SBLASTMBUFCHK(sb); 753395bb186SSam Leffler 754395bb186SSam Leffler SBLASTRECORDCHK(sb); 755df8bae1dSRodney W. Grimes return (1); 756df8bae1dSRodney W. Grimes } 757df8bae1dSRodney W. Grimes 75826f9a767SRodney W. Grimes int 759df8bae1dSRodney W. Grimes sbappendcontrol(sb, m0, control) 760df8bae1dSRodney W. Grimes struct sockbuf *sb; 761df8bae1dSRodney W. Grimes struct mbuf *control, *m0; 762df8bae1dSRodney W. Grimes { 763395bb186SSam Leffler struct mbuf *m, *n, *mlast; 7647ed60de8SPoul-Henning Kamp int space; 765df8bae1dSRodney W. Grimes 766df8bae1dSRodney W. Grimes if (control == 0) 767df8bae1dSRodney W. Grimes panic("sbappendcontrol"); 7687ed60de8SPoul-Henning Kamp space = m_length(control, &n) + m_length(m0, NULL); 769df8bae1dSRodney W. Grimes if (space > sbspace(sb)) 770df8bae1dSRodney W. Grimes return (0); 771df8bae1dSRodney W. Grimes n->m_next = m0; /* concatenate data to control */ 772395bb186SSam Leffler 773395bb186SSam Leffler SBLASTRECORDCHK(sb); 774395bb186SSam Leffler 775395bb186SSam Leffler for (m = control; m->m_next; m = m->m_next) 776df8bae1dSRodney W. Grimes sballoc(sb, m); 777395bb186SSam Leffler sballoc(sb, m); 778395bb186SSam Leffler mlast = m; 779395bb186SSam Leffler SBLINKRECORD(sb, control); 780395bb186SSam Leffler 781395bb186SSam Leffler sb->sb_mbtail = mlast; 782395bb186SSam Leffler SBLASTMBUFCHK(sb); 783395bb186SSam Leffler 784395bb186SSam Leffler SBLASTRECORDCHK(sb); 785df8bae1dSRodney W. Grimes return (1); 786df8bae1dSRodney W. Grimes } 787df8bae1dSRodney W. Grimes 788df8bae1dSRodney W. Grimes /* 789df8bae1dSRodney W. Grimes * Compress mbuf chain m into the socket 790df8bae1dSRodney W. Grimes * buffer sb following mbuf n. If n 791df8bae1dSRodney W. Grimes * is null, the buffer is presumed empty. 792df8bae1dSRodney W. Grimes */ 79326f9a767SRodney W. Grimes void 794df8bae1dSRodney W. Grimes sbcompress(sb, m, n) 795df8bae1dSRodney W. Grimes register struct sockbuf *sb; 796df8bae1dSRodney W. Grimes register struct mbuf *m, *n; 797df8bae1dSRodney W. Grimes { 798df8bae1dSRodney W. Grimes register int eor = 0; 799df8bae1dSRodney W. Grimes register struct mbuf *o; 800df8bae1dSRodney W. Grimes 801df8bae1dSRodney W. Grimes while (m) { 802df8bae1dSRodney W. Grimes eor |= m->m_flags & M_EOR; 803df8bae1dSRodney W. Grimes if (m->m_len == 0 && 804df8bae1dSRodney W. Grimes (eor == 0 || 805df8bae1dSRodney W. Grimes (((o = m->m_next) || (o = n)) && 806df8bae1dSRodney W. Grimes o->m_type == m->m_type))) { 807395bb186SSam Leffler if (sb->sb_lastrecord == m) 808395bb186SSam Leffler sb->sb_lastrecord = m->m_next; 809df8bae1dSRodney W. Grimes m = m_free(m); 810df8bae1dSRodney W. Grimes continue; 811df8bae1dSRodney W. Grimes } 81232af0d74SDavid Malone if (n && (n->m_flags & M_EOR) == 0 && 81332af0d74SDavid Malone M_WRITABLE(n) && 81432af0d74SDavid Malone m->m_len <= MCLBYTES / 4 && /* XXX: Don't copy too much */ 81532af0d74SDavid Malone m->m_len <= M_TRAILINGSPACE(n) && 816df8bae1dSRodney W. Grimes n->m_type == m->m_type) { 817df8bae1dSRodney W. Grimes bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len, 818df8bae1dSRodney W. Grimes (unsigned)m->m_len); 819df8bae1dSRodney W. Grimes n->m_len += m->m_len; 820df8bae1dSRodney W. Grimes sb->sb_cc += m->m_len; 821b3f1af6bSTim J. Robbins if (m->m_type != MT_DATA && m->m_type != MT_HEADER && 822b3f1af6bSTim J. Robbins m->m_type != MT_OOBDATA) 823b3f1af6bSTim J. Robbins /* XXX: Probably don't need.*/ 82404ac9b97SKelly Yancey sb->sb_ctl += m->m_len; 825df8bae1dSRodney W. Grimes m = m_free(m); 826df8bae1dSRodney W. Grimes continue; 827df8bae1dSRodney W. Grimes } 828df8bae1dSRodney W. Grimes if (n) 829df8bae1dSRodney W. Grimes n->m_next = m; 830df8bae1dSRodney W. Grimes else 831df8bae1dSRodney W. Grimes sb->sb_mb = m; 832395bb186SSam Leffler sb->sb_mbtail = m; 833df8bae1dSRodney W. Grimes sballoc(sb, m); 834df8bae1dSRodney W. Grimes n = m; 835df8bae1dSRodney W. Grimes m->m_flags &= ~M_EOR; 836df8bae1dSRodney W. Grimes m = m->m_next; 837df8bae1dSRodney W. Grimes n->m_next = 0; 838df8bae1dSRodney W. Grimes } 839df8bae1dSRodney W. Grimes if (eor) { 840df8bae1dSRodney W. Grimes if (n) 841df8bae1dSRodney W. Grimes n->m_flags |= eor; 842df8bae1dSRodney W. Grimes else 843df8bae1dSRodney W. Grimes printf("semi-panic: sbcompress\n"); 844df8bae1dSRodney W. Grimes } 845395bb186SSam Leffler SBLASTMBUFCHK(sb); 846df8bae1dSRodney W. Grimes } 847df8bae1dSRodney W. Grimes 848df8bae1dSRodney W. Grimes /* 849df8bae1dSRodney W. Grimes * Free all mbufs in a sockbuf. 850df8bae1dSRodney W. Grimes * Check that all resources are reclaimed. 851df8bae1dSRodney W. Grimes */ 85226f9a767SRodney W. Grimes void 853df8bae1dSRodney W. Grimes sbflush(sb) 854df8bae1dSRodney W. Grimes register struct sockbuf *sb; 855df8bae1dSRodney W. Grimes { 856df8bae1dSRodney W. Grimes 857df8bae1dSRodney W. Grimes if (sb->sb_flags & SB_LOCK) 858253ab668SAndrey A. Chernov panic("sbflush: locked"); 85923f84772SPierre Beyssac while (sb->sb_mbcnt) { 86023f84772SPierre Beyssac /* 86123f84772SPierre Beyssac * Don't call sbdrop(sb, 0) if the leading mbuf is non-empty: 86223f84772SPierre Beyssac * we would loop forever. Panic instead. 86323f84772SPierre Beyssac */ 86423f84772SPierre Beyssac if (!sb->sb_cc && (sb->sb_mb == NULL || sb->sb_mb->m_len)) 86523f84772SPierre Beyssac break; 866df8bae1dSRodney W. Grimes sbdrop(sb, (int)sb->sb_cc); 86723f84772SPierre Beyssac } 8680931333fSBill Fenner if (sb->sb_cc || sb->sb_mb || sb->sb_mbcnt) 869e25dadb0SRobert Drehmel panic("sbflush: cc %u || mb %p || mbcnt %u", sb->sb_cc, (void *)sb->sb_mb, sb->sb_mbcnt); 870df8bae1dSRodney W. Grimes } 871df8bae1dSRodney W. Grimes 872df8bae1dSRodney W. Grimes /* 873df8bae1dSRodney W. Grimes * Drop data from (the front of) a sockbuf. 874df8bae1dSRodney W. Grimes */ 87526f9a767SRodney W. Grimes void 876df8bae1dSRodney W. Grimes sbdrop(sb, len) 877df8bae1dSRodney W. Grimes register struct sockbuf *sb; 878df8bae1dSRodney W. Grimes register int len; 879df8bae1dSRodney W. Grimes { 880ecde8f7cSMatthew Dillon register struct mbuf *m; 881df8bae1dSRodney W. Grimes struct mbuf *next; 882df8bae1dSRodney W. Grimes 883df8bae1dSRodney W. Grimes next = (m = sb->sb_mb) ? m->m_nextpkt : 0; 884df8bae1dSRodney W. Grimes while (len > 0) { 885df8bae1dSRodney W. Grimes if (m == 0) { 886df8bae1dSRodney W. Grimes if (next == 0) 887df8bae1dSRodney W. Grimes panic("sbdrop"); 888df8bae1dSRodney W. Grimes m = next; 889df8bae1dSRodney W. Grimes next = m->m_nextpkt; 890df8bae1dSRodney W. Grimes continue; 891df8bae1dSRodney W. Grimes } 892df8bae1dSRodney W. Grimes if (m->m_len > len) { 893df8bae1dSRodney W. Grimes m->m_len -= len; 894df8bae1dSRodney W. Grimes m->m_data += len; 895df8bae1dSRodney W. Grimes sb->sb_cc -= len; 896b3f1af6bSTim J. Robbins if (m->m_type != MT_DATA && m->m_type != MT_HEADER && 897b3f1af6bSTim J. Robbins m->m_type != MT_OOBDATA) 89804ac9b97SKelly Yancey sb->sb_ctl -= len; 899df8bae1dSRodney W. Grimes break; 900df8bae1dSRodney W. Grimes } 901df8bae1dSRodney W. Grimes len -= m->m_len; 902df8bae1dSRodney W. Grimes sbfree(sb, m); 903ecde8f7cSMatthew Dillon m = m_free(m); 904df8bae1dSRodney W. Grimes } 905df8bae1dSRodney W. Grimes while (m && m->m_len == 0) { 906df8bae1dSRodney W. Grimes sbfree(sb, m); 907ecde8f7cSMatthew Dillon m = m_free(m); 908df8bae1dSRodney W. Grimes } 909df8bae1dSRodney W. Grimes if (m) { 910df8bae1dSRodney W. Grimes sb->sb_mb = m; 911df8bae1dSRodney W. Grimes m->m_nextpkt = next; 912df8bae1dSRodney W. Grimes } else 913df8bae1dSRodney W. Grimes sb->sb_mb = next; 914395bb186SSam Leffler /* 915395bb186SSam Leffler * First part is an inline SB_EMPTY_FIXUP(). Second part 916395bb186SSam Leffler * makes sure sb_lastrecord is up-to-date if we dropped 917395bb186SSam Leffler * part of the last record. 918395bb186SSam Leffler */ 919395bb186SSam Leffler m = sb->sb_mb; 920395bb186SSam Leffler if (m == NULL) { 921395bb186SSam Leffler sb->sb_mbtail = NULL; 922395bb186SSam Leffler sb->sb_lastrecord = NULL; 923395bb186SSam Leffler } else if (m->m_nextpkt == NULL) { 924395bb186SSam Leffler sb->sb_lastrecord = m; 925395bb186SSam Leffler } 926df8bae1dSRodney W. Grimes } 927df8bae1dSRodney W. Grimes 928df8bae1dSRodney W. Grimes /* 929df8bae1dSRodney W. Grimes * Drop a record off the front of a sockbuf 930df8bae1dSRodney W. Grimes * and move the next record to the front. 931df8bae1dSRodney W. Grimes */ 93226f9a767SRodney W. Grimes void 933df8bae1dSRodney W. Grimes sbdroprecord(sb) 934df8bae1dSRodney W. Grimes register struct sockbuf *sb; 935df8bae1dSRodney W. Grimes { 936ecde8f7cSMatthew Dillon register struct mbuf *m; 937df8bae1dSRodney W. Grimes 938df8bae1dSRodney W. Grimes m = sb->sb_mb; 939df8bae1dSRodney W. Grimes if (m) { 940df8bae1dSRodney W. Grimes sb->sb_mb = m->m_nextpkt; 941df8bae1dSRodney W. Grimes do { 942df8bae1dSRodney W. Grimes sbfree(sb, m); 943ecde8f7cSMatthew Dillon m = m_free(m); 944797f2d22SPoul-Henning Kamp } while (m); 945df8bae1dSRodney W. Grimes } 946395bb186SSam Leffler SB_EMPTY_FIXUP(sb); 947df8bae1dSRodney W. Grimes } 9481e4ad9ceSGarrett Wollman 94982c23ebaSBill Fenner /* 95082c23ebaSBill Fenner * Create a "control" mbuf containing the specified data 95182c23ebaSBill Fenner * with the specified type for presentation on a socket buffer. 95282c23ebaSBill Fenner */ 95382c23ebaSBill Fenner struct mbuf * 95482c23ebaSBill Fenner sbcreatecontrol(p, size, type, level) 95582c23ebaSBill Fenner caddr_t p; 95682c23ebaSBill Fenner register int size; 95782c23ebaSBill Fenner int type, level; 95882c23ebaSBill Fenner { 95982c23ebaSBill Fenner register struct cmsghdr *cp; 96082c23ebaSBill Fenner struct mbuf *m; 96182c23ebaSBill Fenner 96259bdd405SDavid Malone if (CMSG_SPACE((u_int)size) > MCLBYTES) 9630b97e97cSYoshinobu Inoue return ((struct mbuf *) NULL); 964a163d034SWarner Losh if ((m = m_get(M_DONTWAIT, MT_CONTROL)) == NULL) 96582c23ebaSBill Fenner return ((struct mbuf *) NULL); 96659bdd405SDavid Malone if (CMSG_SPACE((u_int)size) > MLEN) { 967a163d034SWarner Losh MCLGET(m, M_DONTWAIT); 96859bdd405SDavid Malone if ((m->m_flags & M_EXT) == 0) { 96959bdd405SDavid Malone m_free(m); 97059bdd405SDavid Malone return ((struct mbuf *) NULL); 97159bdd405SDavid Malone } 97259bdd405SDavid Malone } 97382c23ebaSBill Fenner cp = mtod(m, struct cmsghdr *); 97459bdd405SDavid Malone m->m_len = 0; 97559bdd405SDavid Malone KASSERT(CMSG_SPACE((u_int)size) <= M_TRAILINGSPACE(m), 97659bdd405SDavid Malone ("sbcreatecontrol: short mbuf")); 97759bdd405SDavid Malone if (p != NULL) 97882c23ebaSBill Fenner (void)memcpy(CMSG_DATA(cp), p, size); 9797d0d8dc3SYoshinobu Inoue m->m_len = CMSG_SPACE(size); 9807d0d8dc3SYoshinobu Inoue cp->cmsg_len = CMSG_LEN(size); 98182c23ebaSBill Fenner cp->cmsg_level = level; 98282c23ebaSBill Fenner cp->cmsg_type = type; 98382c23ebaSBill Fenner return (m); 98482c23ebaSBill Fenner } 98582c23ebaSBill Fenner 9861e4ad9ceSGarrett Wollman /* 9872c37256eSGarrett Wollman * Some routines that return EOPNOTSUPP for entry points that are not 9882c37256eSGarrett Wollman * supported by a protocol. Fill in as needed. 9891e4ad9ceSGarrett Wollman */ 9901e4ad9ceSGarrett Wollman int 99157bf258eSGarrett Wollman pru_accept_notsupp(struct socket *so, struct sockaddr **nam) 992d8392c6cSGarrett Wollman { 993d8392c6cSGarrett Wollman return EOPNOTSUPP; 994d8392c6cSGarrett Wollman } 995d8392c6cSGarrett Wollman 996d8392c6cSGarrett Wollman int 997b40ce416SJulian Elischer pru_connect_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td) 9989f907986SPeter Wemm { 9999f907986SPeter Wemm return EOPNOTSUPP; 10009f907986SPeter Wemm } 10019f907986SPeter Wemm 10029f907986SPeter Wemm int 10032c37256eSGarrett Wollman pru_connect2_notsupp(struct socket *so1, struct socket *so2) 10041e4ad9ceSGarrett Wollman { 10052c37256eSGarrett Wollman return EOPNOTSUPP; 10061e4ad9ceSGarrett Wollman } 1007d8392c6cSGarrett Wollman 1008d8392c6cSGarrett Wollman int 1009ecbb00a2SDoug Rabson pru_control_notsupp(struct socket *so, u_long cmd, caddr_t data, 1010b40ce416SJulian Elischer struct ifnet *ifp, struct thread *td) 1011a29f300eSGarrett Wollman { 1012a29f300eSGarrett Wollman return EOPNOTSUPP; 1013a29f300eSGarrett Wollman } 1014a29f300eSGarrett Wollman 1015a29f300eSGarrett Wollman int 1016b40ce416SJulian Elischer pru_listen_notsupp(struct socket *so, struct thread *td) 1017d8392c6cSGarrett Wollman { 1018d8392c6cSGarrett Wollman return EOPNOTSUPP; 1019d8392c6cSGarrett Wollman } 1020d8392c6cSGarrett Wollman 1021d8392c6cSGarrett Wollman int 1022d8392c6cSGarrett Wollman pru_rcvd_notsupp(struct socket *so, int flags) 1023d8392c6cSGarrett Wollman { 1024d8392c6cSGarrett Wollman return EOPNOTSUPP; 1025d8392c6cSGarrett Wollman } 1026d8392c6cSGarrett Wollman 1027d8392c6cSGarrett Wollman int 1028d8392c6cSGarrett Wollman pru_rcvoob_notsupp(struct socket *so, struct mbuf *m, int flags) 1029d8392c6cSGarrett Wollman { 1030d8392c6cSGarrett Wollman return EOPNOTSUPP; 1031d8392c6cSGarrett Wollman } 1032d8392c6cSGarrett Wollman 1033d8392c6cSGarrett Wollman /* 1034d8392c6cSGarrett Wollman * This isn't really a ``null'' operation, but it's the default one 1035d8392c6cSGarrett Wollman * and doesn't do anything destructive. 1036d8392c6cSGarrett Wollman */ 1037d8392c6cSGarrett Wollman int 1038d8392c6cSGarrett Wollman pru_sense_null(struct socket *so, struct stat *sb) 1039d8392c6cSGarrett Wollman { 1040d8392c6cSGarrett Wollman sb->st_blksize = so->so_snd.sb_hiwat; 1041d8392c6cSGarrett Wollman return 0; 1042d8392c6cSGarrett Wollman } 1043639acc13SGarrett Wollman 1044639acc13SGarrett Wollman /* 104557bf258eSGarrett Wollman * Make a copy of a sockaddr in a malloced buffer of type M_SONAME. 104657bf258eSGarrett Wollman */ 104757bf258eSGarrett Wollman struct sockaddr * 104857bf258eSGarrett Wollman dup_sockaddr(sa, canwait) 104957bf258eSGarrett Wollman struct sockaddr *sa; 105057bf258eSGarrett Wollman int canwait; 105157bf258eSGarrett Wollman { 105257bf258eSGarrett Wollman struct sockaddr *sa2; 105357bf258eSGarrett Wollman 105457bf258eSGarrett Wollman MALLOC(sa2, struct sockaddr *, sa->sa_len, M_SONAME, 1055a163d034SWarner Losh canwait ? M_WAITOK : M_NOWAIT); 105657bf258eSGarrett Wollman if (sa2) 105757bf258eSGarrett Wollman bcopy(sa, sa2, sa->sa_len); 105857bf258eSGarrett Wollman return sa2; 105957bf258eSGarrett Wollman } 106057bf258eSGarrett Wollman 106157bf258eSGarrett Wollman /* 106298271db4SGarrett Wollman * Create an external-format (``xsocket'') structure using the information 106398271db4SGarrett Wollman * in the kernel-format socket structure pointed to by so. This is done 106498271db4SGarrett Wollman * to reduce the spew of irrelevant information over this interface, 106598271db4SGarrett Wollman * to isolate user code from changes in the kernel structure, and 106698271db4SGarrett Wollman * potentially to provide information-hiding if we decide that 106798271db4SGarrett Wollman * some of this information should be hidden from users. 106898271db4SGarrett Wollman */ 106998271db4SGarrett Wollman void 107098271db4SGarrett Wollman sotoxsocket(struct socket *so, struct xsocket *xso) 107198271db4SGarrett Wollman { 107298271db4SGarrett Wollman xso->xso_len = sizeof *xso; 107398271db4SGarrett Wollman xso->xso_so = so; 107498271db4SGarrett Wollman xso->so_type = so->so_type; 107598271db4SGarrett Wollman xso->so_options = so->so_options; 107698271db4SGarrett Wollman xso->so_linger = so->so_linger; 107798271db4SGarrett Wollman xso->so_state = so->so_state; 107898271db4SGarrett Wollman xso->so_pcb = so->so_pcb; 107998271db4SGarrett Wollman xso->xso_protocol = so->so_proto->pr_protocol; 108098271db4SGarrett Wollman xso->xso_family = so->so_proto->pr_domain->dom_family; 108198271db4SGarrett Wollman xso->so_qlen = so->so_qlen; 108298271db4SGarrett Wollman xso->so_incqlen = so->so_incqlen; 108398271db4SGarrett Wollman xso->so_qlimit = so->so_qlimit; 108498271db4SGarrett Wollman xso->so_timeo = so->so_timeo; 108598271db4SGarrett Wollman xso->so_error = so->so_error; 1086831d27a9SDon Lewis xso->so_pgid = so->so_sigio ? so->so_sigio->sio_pgid : 0; 108798271db4SGarrett Wollman xso->so_oobmark = so->so_oobmark; 108898271db4SGarrett Wollman sbtoxsockbuf(&so->so_snd, &xso->so_snd); 108998271db4SGarrett Wollman sbtoxsockbuf(&so->so_rcv, &xso->so_rcv); 10902f9a2132SBrian Feldman xso->so_uid = so->so_cred->cr_uid; 109198271db4SGarrett Wollman } 109298271db4SGarrett Wollman 109398271db4SGarrett Wollman /* 109498271db4SGarrett Wollman * This does the same for sockbufs. Note that the xsockbuf structure, 109598271db4SGarrett Wollman * since it is always embedded in a socket, does not include a self 109698271db4SGarrett Wollman * pointer nor a length. We make this entry point public in case 109798271db4SGarrett Wollman * some other mechanism needs it. 109898271db4SGarrett Wollman */ 109998271db4SGarrett Wollman void 110098271db4SGarrett Wollman sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb) 110198271db4SGarrett Wollman { 110298271db4SGarrett Wollman xsb->sb_cc = sb->sb_cc; 110398271db4SGarrett Wollman xsb->sb_hiwat = sb->sb_hiwat; 110498271db4SGarrett Wollman xsb->sb_mbcnt = sb->sb_mbcnt; 110598271db4SGarrett Wollman xsb->sb_mbmax = sb->sb_mbmax; 110698271db4SGarrett Wollman xsb->sb_lowat = sb->sb_lowat; 110798271db4SGarrett Wollman xsb->sb_flags = sb->sb_flags; 110898271db4SGarrett Wollman xsb->sb_timeo = sb->sb_timeo; 110998271db4SGarrett Wollman } 111098271db4SGarrett Wollman 111198271db4SGarrett Wollman /* 1112639acc13SGarrett Wollman * Here is the definition of some of the basic objects in the kern.ipc 1113639acc13SGarrett Wollman * branch of the MIB. 1114639acc13SGarrett Wollman */ 1115639acc13SGarrett Wollman SYSCTL_NODE(_kern, KERN_IPC, ipc, CTLFLAG_RW, 0, "IPC"); 1116639acc13SGarrett Wollman 1117639acc13SGarrett Wollman /* This takes the place of kern.maxsockbuf, which moved to kern.ipc. */ 1118639acc13SGarrett Wollman static int dummy; 1119639acc13SGarrett Wollman SYSCTL_INT(_kern, KERN_DUMMY, dummy, CTLFLAG_RW, &dummy, 0, ""); 11201b978d45SHartmut Brandt SYSCTL_OID(_kern_ipc, KIPC_MAXSOCKBUF, maxsockbuf, CTLTYPE_ULONG|CTLFLAG_RW, 11211b978d45SHartmut Brandt &sb_max, 0, sysctl_handle_sb_max, "LU", "Maximum socket buffer size"); 1122184dcdc7SMike Silbersack SYSCTL_INT(_kern_ipc, OID_AUTO, maxsockets, CTLFLAG_RDTUN, 112354d77689SJeff Roberson &maxsockets, 0, "Maximum number of sockets avaliable"); 11241b978d45SHartmut Brandt SYSCTL_ULONG(_kern_ipc, KIPC_SOCKBUF_WASTE, sockbuf_waste_factor, CTLFLAG_RW, 1125639acc13SGarrett Wollman &sb_efficiency, 0, ""); 112654d77689SJeff Roberson 112754d77689SJeff Roberson /* 112854d77689SJeff Roberson * Initialise maxsockets 112954d77689SJeff Roberson */ 113054d77689SJeff Roberson static void init_maxsockets(void *ignored) 113154d77689SJeff Roberson { 113254d77689SJeff Roberson TUNABLE_INT_FETCH("kern.ipc.maxsockets", &maxsockets); 113354d77689SJeff Roberson maxsockets = imax(maxsockets, imax(maxfiles, nmbclusters)); 113454d77689SJeff Roberson } 113554d77689SJeff Roberson SYSINIT(param, SI_SUB_TUNABLES, SI_ORDER_ANY, init_maxsockets, NULL); 1136