1df8bae1dSRodney W. Grimes /* 2df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1988, 1990, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * 5df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 6df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 7df8bae1dSRodney W. Grimes * are met: 8df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 9df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 10df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 12df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 13df8bae1dSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 14df8bae1dSRodney W. Grimes * must display the following acknowledgement: 15df8bae1dSRodney W. Grimes * This product includes software developed by the University of 16df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 17df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 18df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 19df8bae1dSRodney W. Grimes * without specific prior written permission. 20df8bae1dSRodney W. Grimes * 21df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31df8bae1dSRodney W. Grimes * SUCH DAMAGE. 32df8bae1dSRodney W. Grimes * 33df8bae1dSRodney W. Grimes * @(#)uipc_socket2.c 8.1 (Berkeley) 6/10/93 34c3aac50fSPeter Wemm * $FreeBSD$ 35df8bae1dSRodney W. Grimes */ 36df8bae1dSRodney W. Grimes 375b86eac4SJesper Skriver #include "opt_param.h" 38df8bae1dSRodney W. Grimes #include <sys/param.h> 39960ed29cSSeigo Tanimura #include <sys/aio.h> /* for aio_swake proto */ 4098271db4SGarrett Wollman #include <sys/domain.h> 41960ed29cSSeigo Tanimura #include <sys/event.h> 42134c934cSMike Smith #include <sys/file.h> /* for maxfiles */ 43ff5c09daSGarrett Wollman #include <sys/kernel.h> 44fb919e4dSMark Murray #include <sys/lock.h> 45df8bae1dSRodney W. Grimes #include <sys/malloc.h> 46df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 47960ed29cSSeigo Tanimura #include <sys/mutex.h> 48fb919e4dSMark Murray #include <sys/proc.h> 49df8bae1dSRodney W. Grimes #include <sys/protosw.h> 502f9a2132SBrian Feldman #include <sys/resourcevar.h> 51960ed29cSSeigo Tanimura #include <sys/signalvar.h> 52df8bae1dSRodney W. Grimes #include <sys/socket.h> 53df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 54960ed29cSSeigo Tanimura #include <sys/stat.h> 55960ed29cSSeigo Tanimura #include <sys/sx.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 67ff5c09daSGarrett Wollman u_long sb_max = SB_MAX; /* XXX should be static */ 68df8bae1dSRodney W. Grimes 694b29bc4fSGarrett Wollman static u_long sb_efficiency = 8; /* parameter for sbreserve() */ 704b29bc4fSGarrett Wollman 71df8bae1dSRodney W. Grimes /* 72df8bae1dSRodney W. Grimes * Procedures to manipulate state flags of socket 73df8bae1dSRodney W. Grimes * and do appropriate wakeups. Normal sequence from the 74df8bae1dSRodney W. Grimes * active (originating) side is that soisconnecting() is 75df8bae1dSRodney W. Grimes * called during processing of connect() call, 76df8bae1dSRodney W. Grimes * resulting in an eventual call to soisconnected() if/when the 77df8bae1dSRodney W. Grimes * connection is established. When the connection is torn down 78df8bae1dSRodney W. Grimes * soisdisconnecting() is called during processing of disconnect() call, 79df8bae1dSRodney W. Grimes * and soisdisconnected() is called when the connection to the peer 80df8bae1dSRodney W. Grimes * is totally severed. The semantics of these routines are such that 81df8bae1dSRodney W. Grimes * connectionless protocols can call soisconnected() and soisdisconnected() 82df8bae1dSRodney W. Grimes * only, bypassing the in-progress calls when setting up a ``connection'' 83df8bae1dSRodney W. Grimes * takes no time. 84df8bae1dSRodney W. Grimes * 85df8bae1dSRodney W. Grimes * From the passive side, a socket is created with 86dc97381eSPeter Wemm * two queues of sockets: so_incomp for connections in progress 87dc97381eSPeter Wemm * and so_comp for connections already made and awaiting user acceptance. 88df8bae1dSRodney W. Grimes * As a protocol is preparing incoming connections, it creates a socket 89dc97381eSPeter Wemm * structure queued on so_incomp by calling sonewconn(). When the connection 90df8bae1dSRodney W. Grimes * is established, soisconnected() is called, and transfers the 91dc97381eSPeter Wemm * socket structure to so_comp, making it available to accept(). 92df8bae1dSRodney W. Grimes * 93df8bae1dSRodney W. Grimes * If a socket is closed with sockets on either 94dc97381eSPeter Wemm * so_incomp or so_comp, these sockets are dropped. 95df8bae1dSRodney W. Grimes * 96df8bae1dSRodney W. Grimes * If higher level protocols are implemented in 97df8bae1dSRodney W. Grimes * the kernel, the wakeups done here will sometimes 98df8bae1dSRodney W. Grimes * cause software-interrupt process scheduling. 99df8bae1dSRodney W. Grimes */ 100df8bae1dSRodney W. Grimes 10126f9a767SRodney W. Grimes void 102df8bae1dSRodney W. Grimes soisconnecting(so) 103df8bae1dSRodney W. Grimes register struct socket *so; 104df8bae1dSRodney W. Grimes { 105df8bae1dSRodney W. Grimes 106df8bae1dSRodney W. Grimes so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING); 107df8bae1dSRodney W. Grimes so->so_state |= SS_ISCONNECTING; 108df8bae1dSRodney W. Grimes } 109df8bae1dSRodney W. Grimes 11026f9a767SRodney W. Grimes void 111d48d4b25SSeigo Tanimura soisconnected_locked(so) 1128f4e4aa5SAlfred Perlstein struct socket *so; 113df8bae1dSRodney W. Grimes { 1148f4e4aa5SAlfred Perlstein struct socket *head = so->so_head; 115df8bae1dSRodney W. Grimes 116d48d4b25SSeigo Tanimura SIGIO_ASSERT(SX_SLOCKED); /* XXX */ 117df8bae1dSRodney W. Grimes so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING); 118df8bae1dSRodney W. Grimes so->so_state |= SS_ISCONNECTED; 119be24e9e8SDavid Greenman if (head && (so->so_state & SS_INCOMP)) { 120a79b7128SAlfred Perlstein if ((so->so_options & SO_ACCEPTFILTER) != 0) { 121a79b7128SAlfred Perlstein so->so_upcall = head->so_accf->so_accept_filter->accf_callback; 122a79b7128SAlfred Perlstein so->so_upcallarg = head->so_accf->so_accept_filter_arg; 123a79b7128SAlfred Perlstein so->so_rcv.sb_flags |= SB_UPCALL; 124a79b7128SAlfred Perlstein so->so_options &= ~SO_ACCEPTFILTER; 125d48d4b25SSeigo Tanimura SIGIO_SUNLOCK(); /* XXX */ 126d48d4b25SSeigo Tanimura so->so_upcall(so, so->so_upcallarg, 0); 127d48d4b25SSeigo Tanimura SIGIO_SLOCK(); 128d48d4b25SSeigo Tanimura return; 129d48d4b25SSeigo Tanimura } 130d48d4b25SSeigo Tanimura TAILQ_REMOVE(&head->so_incomp, so, so_list); 131d48d4b25SSeigo Tanimura head->so_incqlen--; 132d48d4b25SSeigo Tanimura so->so_state &= ~SS_INCOMP; 133d48d4b25SSeigo Tanimura TAILQ_INSERT_TAIL(&head->so_comp, so, so_list); 134d48d4b25SSeigo Tanimura so->so_state |= SS_COMP; 135d48d4b25SSeigo Tanimura sorwakeup_locked(head); 136d48d4b25SSeigo Tanimura wakeup_one(&head->so_timeo); 137d48d4b25SSeigo Tanimura } else { 138d48d4b25SSeigo Tanimura wakeup(&so->so_timeo); 139d48d4b25SSeigo Tanimura sorwakeup_locked(so); 140d48d4b25SSeigo Tanimura sowwakeup_locked(so); 141d48d4b25SSeigo Tanimura } 142d48d4b25SSeigo Tanimura } 143d48d4b25SSeigo Tanimura 144d48d4b25SSeigo Tanimura void 145d48d4b25SSeigo Tanimura soisconnected(so) 146d48d4b25SSeigo Tanimura struct socket *so; 147d48d4b25SSeigo Tanimura { 148d48d4b25SSeigo Tanimura struct socket *head = so->so_head; 149d48d4b25SSeigo Tanimura 150d48d4b25SSeigo Tanimura SIGIO_SLOCK(); 151d48d4b25SSeigo Tanimura so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING); 152d48d4b25SSeigo Tanimura so->so_state |= SS_ISCONNECTED; 153d48d4b25SSeigo Tanimura if (head && (so->so_state & SS_INCOMP)) { 154d48d4b25SSeigo Tanimura if ((so->so_options & SO_ACCEPTFILTER) != 0) { 155d48d4b25SSeigo Tanimura so->so_upcall = head->so_accf->so_accept_filter->accf_callback; 156d48d4b25SSeigo Tanimura so->so_upcallarg = head->so_accf->so_accept_filter_arg; 157d48d4b25SSeigo Tanimura so->so_rcv.sb_flags |= SB_UPCALL; 158d48d4b25SSeigo Tanimura so->so_options &= ~SO_ACCEPTFILTER; 159d48d4b25SSeigo Tanimura SIGIO_SUNLOCK(); 160a79b7128SAlfred Perlstein so->so_upcall(so, so->so_upcallarg, 0); 161a79b7128SAlfred Perlstein return; 162a79b7128SAlfred Perlstein } 163be24e9e8SDavid Greenman TAILQ_REMOVE(&head->so_incomp, so, so_list); 164a51764a8SPaul Traina head->so_incqlen--; 165be24e9e8SDavid Greenman so->so_state &= ~SS_INCOMP; 166be24e9e8SDavid Greenman TAILQ_INSERT_TAIL(&head->so_comp, so, so_list); 167e1f1827fSMike Silbersack head->so_qlen++; 168be24e9e8SDavid Greenman so->so_state |= SS_COMP; 169d48d4b25SSeigo Tanimura sorwakeup_locked(head); 170a91b8721SDavid Greenman wakeup_one(&head->so_timeo); 171df8bae1dSRodney W. Grimes } else { 172a91b8721SDavid Greenman wakeup(&so->so_timeo); 173d48d4b25SSeigo Tanimura sorwakeup_locked(so); 174d48d4b25SSeigo Tanimura sowwakeup_locked(so); 175df8bae1dSRodney W. Grimes } 176d48d4b25SSeigo Tanimura SIGIO_SUNLOCK(); 177df8bae1dSRodney W. Grimes } 178df8bae1dSRodney W. Grimes 17926f9a767SRodney W. Grimes void 180df8bae1dSRodney W. Grimes soisdisconnecting(so) 181df8bae1dSRodney W. Grimes register struct socket *so; 182df8bae1dSRodney W. Grimes { 183df8bae1dSRodney W. Grimes 184d48d4b25SSeigo Tanimura SIGIO_SLOCK(); 185df8bae1dSRodney W. Grimes so->so_state &= ~SS_ISCONNECTING; 186df8bae1dSRodney W. Grimes so->so_state |= (SS_ISDISCONNECTING|SS_CANTRCVMORE|SS_CANTSENDMORE); 187df8bae1dSRodney W. Grimes wakeup((caddr_t)&so->so_timeo); 188d48d4b25SSeigo Tanimura sowwakeup_locked(so); 189d48d4b25SSeigo Tanimura sorwakeup_locked(so); 190d48d4b25SSeigo Tanimura SIGIO_SUNLOCK(); 191d48d4b25SSeigo Tanimura } 192d48d4b25SSeigo Tanimura 193d48d4b25SSeigo Tanimura void 194d48d4b25SSeigo Tanimura soisdisconnected_locked(so) 195d48d4b25SSeigo Tanimura register struct socket *so; 196d48d4b25SSeigo Tanimura { 197d48d4b25SSeigo Tanimura 198d48d4b25SSeigo Tanimura SIGIO_ASSERT(SX_LOCKED); 199d48d4b25SSeigo Tanimura so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING); 200d48d4b25SSeigo Tanimura so->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE|SS_ISDISCONNECTED); 201d48d4b25SSeigo Tanimura wakeup((caddr_t)&so->so_timeo); 202d48d4b25SSeigo Tanimura sowwakeup_locked(so); 203d48d4b25SSeigo Tanimura sorwakeup_locked(so); 204df8bae1dSRodney W. Grimes } 205df8bae1dSRodney W. Grimes 20626f9a767SRodney W. Grimes void 207df8bae1dSRodney W. Grimes soisdisconnected(so) 208df8bae1dSRodney W. Grimes register struct socket *so; 209df8bae1dSRodney W. Grimes { 210df8bae1dSRodney W. Grimes 211d48d4b25SSeigo Tanimura SIGIO_SLOCK(); 212d48d4b25SSeigo Tanimura soisdisconnected_locked(so); 213d48d4b25SSeigo Tanimura SIGIO_SUNLOCK(); 214df8bae1dSRodney W. Grimes } 215df8bae1dSRodney W. Grimes 216df8bae1dSRodney W. Grimes /* 217df8bae1dSRodney W. Grimes * When an attempt at a new connection is noted on a socket 218df8bae1dSRodney W. Grimes * which accepts connections, sonewconn is called. If the 219df8bae1dSRodney W. Grimes * connection is possible (subject to space constraints, etc.) 220df8bae1dSRodney W. Grimes * then we allocate a new structure, propoerly linked into the 221df8bae1dSRodney W. Grimes * data structure of the original socket, and return this. 222df8bae1dSRodney W. Grimes * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED. 223b1e4abd2SMatthew Dillon * 224b1e4abd2SMatthew Dillon * note: the ref count on the socket is 0 on return 225df8bae1dSRodney W. Grimes */ 226df8bae1dSRodney W. Grimes struct socket * 227548af278SBill Fenner sonewconn(head, connstatus) 228df8bae1dSRodney W. Grimes register struct socket *head; 229df8bae1dSRodney W. Grimes int connstatus; 230df8bae1dSRodney W. Grimes { 231df8bae1dSRodney W. Grimes register struct socket *so; 232df8bae1dSRodney W. Grimes 233ebb0cbeaSPaul Traina if (head->so_qlen > 3 * head->so_qlimit / 2) 234df8bae1dSRodney W. Grimes return ((struct socket *)0); 23598271db4SGarrett Wollman so = soalloc(0); 236df8bae1dSRodney W. Grimes if (so == NULL) 237df8bae1dSRodney W. Grimes return ((struct socket *)0); 238205b2b61SPeter Wemm if ((head->so_options & SO_ACCEPTFILTER) != 0) 239205b2b61SPeter Wemm connstatus = 0; 240be24e9e8SDavid Greenman so->so_head = head; 241df8bae1dSRodney W. Grimes so->so_type = head->so_type; 242df8bae1dSRodney W. Grimes so->so_options = head->so_options &~ SO_ACCEPTCONN; 243df8bae1dSRodney W. Grimes so->so_linger = head->so_linger; 244df8bae1dSRodney W. Grimes so->so_state = head->so_state | SS_NOFDREF; 245df8bae1dSRodney W. Grimes so->so_proto = head->so_proto; 246df8bae1dSRodney W. Grimes so->so_timeo = head->so_timeo; 247bd78ceceSJohn Baldwin so->so_cred = crhold(head->so_cred); 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)) { 250b1e4abd2SMatthew Dillon sotryfree(so); 251b1396a35SGarrett Wollman return ((struct socket *)0); 252b1396a35SGarrett Wollman } 253b1396a35SGarrett Wollman 254be24e9e8SDavid Greenman if (connstatus) { 255be24e9e8SDavid Greenman TAILQ_INSERT_TAIL(&head->so_comp, so, so_list); 256be24e9e8SDavid Greenman so->so_state |= SS_COMP; 257e1f1827fSMike Silbersack head->so_qlen++; 258be24e9e8SDavid Greenman } else { 259e1f1827fSMike Silbersack if (head->so_incqlen >= head->so_qlimit) { 260e1f1827fSMike Silbersack struct socket *sp; 261e1f1827fSMike Silbersack sp = TAILQ_FIRST(&head->so_incomp); 262e1f1827fSMike Silbersack (void) soabort(sp); 263e1f1827fSMike Silbersack } 264be24e9e8SDavid Greenman TAILQ_INSERT_TAIL(&head->so_incomp, so, so_list); 265be24e9e8SDavid Greenman so->so_state |= SS_INCOMP; 266ebb0cbeaSPaul Traina head->so_incqlen++; 267be24e9e8SDavid Greenman } 268df8bae1dSRodney W. Grimes if (connstatus) { 269d48d4b25SSeigo Tanimura SIGIO_SLOCK(); 270d48d4b25SSeigo Tanimura sorwakeup_locked(head); 271d48d4b25SSeigo Tanimura SIGIO_SUNLOCK(); 272acbbcc5fSSeigo Tanimura wakeup((caddr_t)&head->so_timeo); 273acbbcc5fSSeigo Tanimura so->so_state |= connstatus; 274df8bae1dSRodney W. Grimes } 275df8bae1dSRodney W. Grimes return (so); 276df8bae1dSRodney W. Grimes } 277df8bae1dSRodney W. Grimes 278df8bae1dSRodney W. Grimes /* 279df8bae1dSRodney W. Grimes * Socantsendmore indicates that no more data will be sent on the 280df8bae1dSRodney W. Grimes * socket; it would normally be applied to a socket when the user 281df8bae1dSRodney W. Grimes * informs the system that no more data is to be sent, by the protocol 282df8bae1dSRodney W. Grimes * code (in case PRU_SHUTDOWN). Socantrcvmore indicates that no more data 283df8bae1dSRodney W. Grimes * will be received, and will normally be applied to the socket by a 284df8bae1dSRodney W. Grimes * protocol when it detects that the peer will send no more data. 285df8bae1dSRodney W. Grimes * Data queued for reading in the socket may yet be read. 286df8bae1dSRodney W. Grimes */ 287df8bae1dSRodney W. Grimes 28826f9a767SRodney W. Grimes void 289df8bae1dSRodney W. Grimes socantsendmore(so) 290df8bae1dSRodney W. Grimes struct socket *so; 291df8bae1dSRodney W. Grimes { 292df8bae1dSRodney W. Grimes 293d48d4b25SSeigo Tanimura SIGIO_SLOCK(); 294df8bae1dSRodney W. Grimes so->so_state |= SS_CANTSENDMORE; 295d48d4b25SSeigo Tanimura sowwakeup_locked(so); 296d48d4b25SSeigo Tanimura SIGIO_SUNLOCK(); 297df8bae1dSRodney W. Grimes } 298df8bae1dSRodney W. Grimes 29926f9a767SRodney W. Grimes void 300df8bae1dSRodney W. Grimes socantrcvmore(so) 301df8bae1dSRodney W. Grimes struct socket *so; 302df8bae1dSRodney W. Grimes { 303df8bae1dSRodney W. Grimes 304d48d4b25SSeigo Tanimura SIGIO_SLOCK(); 305df8bae1dSRodney W. Grimes so->so_state |= SS_CANTRCVMORE; 306d48d4b25SSeigo Tanimura sorwakeup_locked(so); 307d48d4b25SSeigo Tanimura SIGIO_SUNLOCK(); 308df8bae1dSRodney W. Grimes } 309df8bae1dSRodney W. Grimes 310df8bae1dSRodney W. Grimes /* 311df8bae1dSRodney W. Grimes * Wait for data to arrive at/drain from a socket buffer. 312df8bae1dSRodney W. Grimes */ 31326f9a767SRodney W. Grimes int 314df8bae1dSRodney W. Grimes sbwait(sb) 315df8bae1dSRodney W. Grimes struct sockbuf *sb; 316df8bae1dSRodney W. Grimes { 317df8bae1dSRodney W. Grimes 318df8bae1dSRodney W. Grimes sb->sb_flags |= SB_WAIT; 319df8bae1dSRodney W. Grimes return (tsleep((caddr_t)&sb->sb_cc, 32047daf5d5SBruce Evans (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH, "sbwait", 321df8bae1dSRodney W. Grimes sb->sb_timeo)); 322df8bae1dSRodney W. Grimes } 323df8bae1dSRodney W. Grimes 324df8bae1dSRodney W. Grimes /* 325df8bae1dSRodney W. Grimes * Lock a sockbuf already known to be locked; 326df8bae1dSRodney W. Grimes * return any error returned from sleep (EINTR). 327df8bae1dSRodney W. Grimes */ 32826f9a767SRodney W. Grimes int 329df8bae1dSRodney W. Grimes sb_lock(sb) 330df8bae1dSRodney W. Grimes register struct sockbuf *sb; 331df8bae1dSRodney W. Grimes { 332df8bae1dSRodney W. Grimes int error; 333df8bae1dSRodney W. Grimes 334df8bae1dSRodney W. Grimes while (sb->sb_flags & SB_LOCK) { 335df8bae1dSRodney W. Grimes sb->sb_flags |= SB_WANT; 336797f2d22SPoul-Henning Kamp error = tsleep((caddr_t)&sb->sb_flags, 337df8bae1dSRodney W. Grimes (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK|PCATCH, 33847daf5d5SBruce Evans "sblock", 0); 339797f2d22SPoul-Henning Kamp if (error) 340df8bae1dSRodney W. Grimes return (error); 341df8bae1dSRodney W. Grimes } 342df8bae1dSRodney W. Grimes sb->sb_flags |= SB_LOCK; 343df8bae1dSRodney W. Grimes return (0); 344df8bae1dSRodney W. Grimes } 345df8bae1dSRodney W. Grimes 346df8bae1dSRodney W. Grimes /* 347df8bae1dSRodney W. Grimes * Wakeup processes waiting on a socket buffer. 348df8bae1dSRodney W. Grimes * Do asynchronous notification via SIGIO 349df8bae1dSRodney W. Grimes * if the socket has the SS_ASYNC flag set. 350df8bae1dSRodney W. Grimes */ 35126f9a767SRodney W. Grimes void 352df8bae1dSRodney W. Grimes sowakeup(so, sb) 353df8bae1dSRodney W. Grimes register struct socket *so; 354df8bae1dSRodney W. Grimes register struct sockbuf *sb; 355df8bae1dSRodney W. Grimes { 356d48d4b25SSeigo Tanimura SIGIO_ASSERT(SX_LOCKED); 357d48d4b25SSeigo Tanimura 358df8bae1dSRodney W. Grimes selwakeup(&sb->sb_sel); 359df8bae1dSRodney W. Grimes sb->sb_flags &= ~SB_SEL; 360df8bae1dSRodney W. Grimes if (sb->sb_flags & SB_WAIT) { 361df8bae1dSRodney W. Grimes sb->sb_flags &= ~SB_WAIT; 362df8bae1dSRodney W. Grimes wakeup((caddr_t)&sb->sb_cc); 363df8bae1dSRodney W. Grimes } 364831d27a9SDon Lewis if ((so->so_state & SS_ASYNC) && so->so_sigio != NULL) 365831d27a9SDon Lewis pgsigio(so->so_sigio, SIGIO, 0); 3664dc75870SPeter Wemm if (sb->sb_flags & SB_UPCALL) 3674dc75870SPeter Wemm (*so->so_upcall)(so, so->so_upcallarg, M_DONTWAIT); 368bfbbc4aaSJason Evans if (sb->sb_flags & SB_AIO) 369bfbbc4aaSJason Evans aio_swake(so, sb); 370cb679c38SJonathan Lemon KNOTE(&sb->sb_sel.si_note, 0); 371df8bae1dSRodney W. Grimes } 372df8bae1dSRodney W. Grimes 373df8bae1dSRodney W. Grimes /* 374df8bae1dSRodney W. Grimes * Socket buffer (struct sockbuf) utility routines. 375df8bae1dSRodney W. Grimes * 376df8bae1dSRodney W. Grimes * Each socket contains two socket buffers: one for sending data and 377df8bae1dSRodney W. Grimes * one for receiving data. Each buffer contains a queue of mbufs, 378df8bae1dSRodney W. Grimes * information about the number of mbufs and amount of data in the 379df8bae1dSRodney W. Grimes * queue, and other fields allowing select() statements and notification 380df8bae1dSRodney W. Grimes * on data availability to be implemented. 381df8bae1dSRodney W. Grimes * 382df8bae1dSRodney W. Grimes * Data stored in a socket buffer is maintained as a list of records. 383df8bae1dSRodney W. Grimes * Each record is a list of mbufs chained together with the m_next 384df8bae1dSRodney W. Grimes * field. Records are chained together with the m_nextpkt field. The upper 385df8bae1dSRodney W. Grimes * level routine soreceive() expects the following conventions to be 386df8bae1dSRodney W. Grimes * observed when placing information in the receive buffer: 387df8bae1dSRodney W. Grimes * 388df8bae1dSRodney W. Grimes * 1. If the protocol requires each message be preceded by the sender's 389df8bae1dSRodney W. Grimes * name, then a record containing that name must be present before 390df8bae1dSRodney W. Grimes * any associated data (mbuf's must be of type MT_SONAME). 391df8bae1dSRodney W. Grimes * 2. If the protocol supports the exchange of ``access rights'' (really 392df8bae1dSRodney W. Grimes * just additional data associated with the message), and there are 393df8bae1dSRodney W. Grimes * ``rights'' to be received, then a record containing this data 394df8bae1dSRodney W. Grimes * should be present (mbuf's must be of type MT_RIGHTS). 395df8bae1dSRodney W. Grimes * 3. If a name or rights record exists, then it must be followed by 396df8bae1dSRodney W. Grimes * a data record, perhaps of zero length. 397df8bae1dSRodney W. Grimes * 398df8bae1dSRodney W. Grimes * Before using a new socket structure it is first necessary to reserve 399df8bae1dSRodney W. Grimes * buffer space to the socket, by calling sbreserve(). This should commit 400df8bae1dSRodney W. Grimes * some of the available buffer space in the system buffer pool for the 401df8bae1dSRodney W. Grimes * socket (currently, it does nothing but enforce limits). The space 402df8bae1dSRodney W. Grimes * should be released by calling sbrelease() when the socket is destroyed. 403df8bae1dSRodney W. Grimes */ 404df8bae1dSRodney W. Grimes 40526f9a767SRodney W. Grimes int 406df8bae1dSRodney W. Grimes soreserve(so, sndcc, rcvcc) 407df8bae1dSRodney W. Grimes register struct socket *so; 408df8bae1dSRodney W. Grimes u_long sndcc, rcvcc; 409df8bae1dSRodney W. Grimes { 410b40ce416SJulian Elischer struct thread *td = curthread; 411df8bae1dSRodney W. Grimes 412b40ce416SJulian Elischer if (sbreserve(&so->so_snd, sndcc, so, td) == 0) 413df8bae1dSRodney W. Grimes goto bad; 414b40ce416SJulian Elischer if (sbreserve(&so->so_rcv, rcvcc, so, td) == 0) 415df8bae1dSRodney W. Grimes goto bad2; 416df8bae1dSRodney W. Grimes if (so->so_rcv.sb_lowat == 0) 417df8bae1dSRodney W. Grimes so->so_rcv.sb_lowat = 1; 418df8bae1dSRodney W. Grimes if (so->so_snd.sb_lowat == 0) 419df8bae1dSRodney W. Grimes so->so_snd.sb_lowat = MCLBYTES; 420df8bae1dSRodney W. Grimes if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat) 421df8bae1dSRodney W. Grimes so->so_snd.sb_lowat = so->so_snd.sb_hiwat; 422df8bae1dSRodney W. Grimes return (0); 423df8bae1dSRodney W. Grimes bad2: 424ecf72308SBrian Feldman sbrelease(&so->so_snd, so); 425df8bae1dSRodney W. Grimes bad: 426df8bae1dSRodney W. Grimes return (ENOBUFS); 427df8bae1dSRodney W. Grimes } 428df8bae1dSRodney W. Grimes 429df8bae1dSRodney W. Grimes /* 430df8bae1dSRodney W. Grimes * Allot mbufs to a sockbuf. 431df8bae1dSRodney W. Grimes * Attempt to scale mbmax so that mbcnt doesn't become limiting 432df8bae1dSRodney W. Grimes * if buffering efficiency is near the normal case. 433df8bae1dSRodney W. Grimes */ 43426f9a767SRodney W. Grimes int 435b40ce416SJulian Elischer sbreserve(sb, cc, so, td) 436df8bae1dSRodney W. Grimes struct sockbuf *sb; 437df8bae1dSRodney W. Grimes u_long cc; 438ecf72308SBrian Feldman struct socket *so; 439b40ce416SJulian Elischer struct thread *td; 440df8bae1dSRodney W. Grimes { 441ecf72308SBrian Feldman 442ecf72308SBrian Feldman /* 443b40ce416SJulian Elischer * td will only be NULL when we're in an interrupt 444ecf72308SBrian Feldman * (e.g. in tcp_input()) 445ecf72308SBrian Feldman */ 4469f5193caSMike Silbersack if ((u_quad_t)cc > (u_quad_t)sb_max * MCLBYTES / (MSIZE + MCLBYTES)) 447df8bae1dSRodney W. Grimes return (0); 448f535380cSDon Lewis if (!chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, cc, 449b40ce416SJulian Elischer td ? td->td_proc->p_rlimit[RLIMIT_SBSIZE].rlim_cur : RLIM_INFINITY)) { 450ecf72308SBrian Feldman return (0); 451c6362551SAlfred Perlstein } 4524b29bc4fSGarrett Wollman sb->sb_mbmax = min(cc * sb_efficiency, sb_max); 453df8bae1dSRodney W. Grimes if (sb->sb_lowat > sb->sb_hiwat) 454df8bae1dSRodney W. Grimes sb->sb_lowat = sb->sb_hiwat; 455df8bae1dSRodney W. Grimes return (1); 456df8bae1dSRodney W. Grimes } 457df8bae1dSRodney W. Grimes 458df8bae1dSRodney W. Grimes /* 459df8bae1dSRodney W. Grimes * Free mbufs held by a socket, and reserved mbuf space. 460df8bae1dSRodney W. Grimes */ 46126f9a767SRodney W. Grimes void 462ecf72308SBrian Feldman sbrelease(sb, so) 463df8bae1dSRodney W. Grimes struct sockbuf *sb; 464ecf72308SBrian Feldman struct socket *so; 465df8bae1dSRodney W. Grimes { 466df8bae1dSRodney W. Grimes 467df8bae1dSRodney W. Grimes sbflush(sb); 468f535380cSDon Lewis (void)chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, 0, 469f535380cSDon Lewis RLIM_INFINITY); 4706aef685fSBrian Feldman sb->sb_mbmax = 0; 471df8bae1dSRodney W. Grimes } 472df8bae1dSRodney W. Grimes 473df8bae1dSRodney W. Grimes /* 474df8bae1dSRodney W. Grimes * Routines to add and remove 475df8bae1dSRodney W. Grimes * data from an mbuf queue. 476df8bae1dSRodney W. Grimes * 477df8bae1dSRodney W. Grimes * The routines sbappend() or sbappendrecord() are normally called to 478df8bae1dSRodney W. Grimes * append new mbufs to a socket buffer, after checking that adequate 479df8bae1dSRodney W. Grimes * space is available, comparing the function sbspace() with the amount 480df8bae1dSRodney W. Grimes * of data to be added. sbappendrecord() differs from sbappend() in 481df8bae1dSRodney W. Grimes * that data supplied is treated as the beginning of a new record. 482df8bae1dSRodney W. Grimes * To place a sender's address, optional access rights, and data in a 483df8bae1dSRodney W. Grimes * socket receive buffer, sbappendaddr() should be used. To place 484df8bae1dSRodney W. Grimes * access rights and data in a socket receive buffer, sbappendrights() 485df8bae1dSRodney W. Grimes * should be used. In either case, the new data begins a new record. 486df8bae1dSRodney W. Grimes * Note that unlike sbappend() and sbappendrecord(), these routines check 487df8bae1dSRodney W. Grimes * for the caller that there will be enough space to store the data. 488df8bae1dSRodney W. Grimes * Each fails if there is not enough space, or if it cannot find mbufs 489df8bae1dSRodney W. Grimes * to store additional information in. 490df8bae1dSRodney W. Grimes * 491df8bae1dSRodney W. Grimes * Reliable protocols may use the socket send buffer to hold data 492df8bae1dSRodney W. Grimes * awaiting acknowledgement. Data is normally copied from a socket 493df8bae1dSRodney W. Grimes * send buffer in a protocol with m_copy for output to a peer, 494df8bae1dSRodney W. Grimes * and then removing the data from the socket buffer with sbdrop() 495df8bae1dSRodney W. Grimes * or sbdroprecord() when the data is acknowledged by the peer. 496df8bae1dSRodney W. Grimes */ 497df8bae1dSRodney W. Grimes 498df8bae1dSRodney W. Grimes /* 499df8bae1dSRodney W. Grimes * Append mbuf chain m to the last record in the 500df8bae1dSRodney W. Grimes * socket buffer sb. The additional space associated 501df8bae1dSRodney W. Grimes * the mbuf chain is recorded in sb. Empty mbufs are 502df8bae1dSRodney W. Grimes * discarded and mbufs are compacted where possible. 503df8bae1dSRodney W. Grimes */ 50426f9a767SRodney W. Grimes void 505df8bae1dSRodney W. Grimes sbappend(sb, m) 506df8bae1dSRodney W. Grimes struct sockbuf *sb; 507df8bae1dSRodney W. Grimes struct mbuf *m; 508df8bae1dSRodney W. Grimes { 509df8bae1dSRodney W. Grimes register struct mbuf *n; 510df8bae1dSRodney W. Grimes 511df8bae1dSRodney W. Grimes if (m == 0) 512df8bae1dSRodney W. Grimes return; 513797f2d22SPoul-Henning Kamp n = sb->sb_mb; 514797f2d22SPoul-Henning Kamp if (n) { 515df8bae1dSRodney W. Grimes while (n->m_nextpkt) 516df8bae1dSRodney W. Grimes n = n->m_nextpkt; 517df8bae1dSRodney W. Grimes do { 518df8bae1dSRodney W. Grimes if (n->m_flags & M_EOR) { 519df8bae1dSRodney W. Grimes sbappendrecord(sb, m); /* XXXXXX!!!! */ 520df8bae1dSRodney W. Grimes return; 521df8bae1dSRodney W. Grimes } 522df8bae1dSRodney W. Grimes } while (n->m_next && (n = n->m_next)); 523df8bae1dSRodney W. Grimes } 524df8bae1dSRodney W. Grimes sbcompress(sb, m, n); 525df8bae1dSRodney W. Grimes } 526df8bae1dSRodney W. Grimes 527df8bae1dSRodney W. Grimes #ifdef SOCKBUF_DEBUG 52826f9a767SRodney W. Grimes void 529df8bae1dSRodney W. Grimes sbcheck(sb) 530df8bae1dSRodney W. Grimes register struct sockbuf *sb; 531df8bae1dSRodney W. Grimes { 532df8bae1dSRodney W. Grimes register struct mbuf *m; 5330931333fSBill Fenner register struct mbuf *n = 0; 5340931333fSBill Fenner register u_long len = 0, mbcnt = 0; 535df8bae1dSRodney W. Grimes 5360931333fSBill Fenner for (m = sb->sb_mb; m; m = n) { 5370931333fSBill Fenner n = m->m_nextpkt; 5380931333fSBill Fenner for (; m; m = m->m_next) { 539df8bae1dSRodney W. Grimes len += m->m_len; 540df8bae1dSRodney W. Grimes mbcnt += MSIZE; 541313861b8SJulian Elischer if (m->m_flags & M_EXT) /*XXX*/ /* pretty sure this is bogus */ 542df8bae1dSRodney W. Grimes mbcnt += m->m_ext.ext_size; 5430931333fSBill Fenner } 544df8bae1dSRodney W. Grimes } 545df8bae1dSRodney W. Grimes if (len != sb->sb_cc || mbcnt != sb->sb_mbcnt) { 5460931333fSBill Fenner printf("cc %ld != %ld || mbcnt %ld != %ld\n", len, sb->sb_cc, 547df8bae1dSRodney W. Grimes mbcnt, sb->sb_mbcnt); 548df8bae1dSRodney W. Grimes panic("sbcheck"); 549df8bae1dSRodney W. Grimes } 550df8bae1dSRodney W. Grimes } 551df8bae1dSRodney W. Grimes #endif 552df8bae1dSRodney W. Grimes 553df8bae1dSRodney W. Grimes /* 554df8bae1dSRodney W. Grimes * As above, except the mbuf chain 555df8bae1dSRodney W. Grimes * begins a new record. 556df8bae1dSRodney W. Grimes */ 55726f9a767SRodney W. Grimes void 558df8bae1dSRodney W. Grimes sbappendrecord(sb, m0) 559df8bae1dSRodney W. Grimes register struct sockbuf *sb; 560df8bae1dSRodney W. Grimes register struct mbuf *m0; 561df8bae1dSRodney W. Grimes { 562df8bae1dSRodney W. Grimes register struct mbuf *m; 563df8bae1dSRodney W. Grimes 564df8bae1dSRodney W. Grimes if (m0 == 0) 565df8bae1dSRodney W. Grimes return; 566797f2d22SPoul-Henning Kamp m = sb->sb_mb; 567797f2d22SPoul-Henning Kamp if (m) 568df8bae1dSRodney W. Grimes while (m->m_nextpkt) 569df8bae1dSRodney W. Grimes m = m->m_nextpkt; 570df8bae1dSRodney W. Grimes /* 571df8bae1dSRodney W. Grimes * Put the first mbuf on the queue. 572df8bae1dSRodney W. Grimes * Note this permits zero length records. 573df8bae1dSRodney W. Grimes */ 574df8bae1dSRodney W. Grimes sballoc(sb, m0); 575df8bae1dSRodney W. Grimes if (m) 576df8bae1dSRodney W. Grimes m->m_nextpkt = m0; 577df8bae1dSRodney W. Grimes else 578df8bae1dSRodney W. Grimes sb->sb_mb = m0; 579df8bae1dSRodney W. Grimes m = m0->m_next; 580df8bae1dSRodney W. Grimes m0->m_next = 0; 581df8bae1dSRodney W. Grimes if (m && (m0->m_flags & M_EOR)) { 582df8bae1dSRodney W. Grimes m0->m_flags &= ~M_EOR; 583df8bae1dSRodney W. Grimes m->m_flags |= M_EOR; 584df8bae1dSRodney W. Grimes } 585df8bae1dSRodney W. Grimes sbcompress(sb, m, m0); 586df8bae1dSRodney W. Grimes } 587df8bae1dSRodney W. Grimes 588df8bae1dSRodney W. Grimes /* 589df8bae1dSRodney W. Grimes * As above except that OOB data 590df8bae1dSRodney W. Grimes * is inserted at the beginning of the sockbuf, 591df8bae1dSRodney W. Grimes * but after any other OOB data. 592df8bae1dSRodney W. Grimes */ 59326f9a767SRodney W. Grimes void 594df8bae1dSRodney W. Grimes sbinsertoob(sb, m0) 595df8bae1dSRodney W. Grimes register struct sockbuf *sb; 596df8bae1dSRodney W. Grimes register struct mbuf *m0; 597df8bae1dSRodney W. Grimes { 598df8bae1dSRodney W. Grimes register struct mbuf *m; 599df8bae1dSRodney W. Grimes register struct mbuf **mp; 600df8bae1dSRodney W. Grimes 601df8bae1dSRodney W. Grimes if (m0 == 0) 602df8bae1dSRodney W. Grimes return; 603797f2d22SPoul-Henning Kamp for (mp = &sb->sb_mb; *mp ; mp = &((*mp)->m_nextpkt)) { 604797f2d22SPoul-Henning Kamp m = *mp; 605df8bae1dSRodney W. Grimes again: 606df8bae1dSRodney W. Grimes switch (m->m_type) { 607df8bae1dSRodney W. Grimes 608df8bae1dSRodney W. Grimes case MT_OOBDATA: 609df8bae1dSRodney W. Grimes continue; /* WANT next train */ 610df8bae1dSRodney W. Grimes 611df8bae1dSRodney W. Grimes case MT_CONTROL: 612797f2d22SPoul-Henning Kamp m = m->m_next; 613797f2d22SPoul-Henning Kamp if (m) 614df8bae1dSRodney W. Grimes goto again; /* inspect THIS train further */ 615df8bae1dSRodney W. Grimes } 616df8bae1dSRodney W. Grimes break; 617df8bae1dSRodney W. Grimes } 618df8bae1dSRodney W. Grimes /* 619df8bae1dSRodney W. Grimes * Put the first mbuf on the queue. 620df8bae1dSRodney W. Grimes * Note this permits zero length records. 621df8bae1dSRodney W. Grimes */ 622df8bae1dSRodney W. Grimes sballoc(sb, m0); 623df8bae1dSRodney W. Grimes m0->m_nextpkt = *mp; 624df8bae1dSRodney W. Grimes *mp = m0; 625df8bae1dSRodney W. Grimes m = m0->m_next; 626df8bae1dSRodney W. Grimes m0->m_next = 0; 627df8bae1dSRodney W. Grimes if (m && (m0->m_flags & M_EOR)) { 628df8bae1dSRodney W. Grimes m0->m_flags &= ~M_EOR; 629df8bae1dSRodney W. Grimes m->m_flags |= M_EOR; 630df8bae1dSRodney W. Grimes } 631df8bae1dSRodney W. Grimes sbcompress(sb, m, m0); 632df8bae1dSRodney W. Grimes } 633df8bae1dSRodney W. Grimes 634df8bae1dSRodney W. Grimes /* 635df8bae1dSRodney W. Grimes * Append address and data, and optionally, control (ancillary) data 636df8bae1dSRodney W. Grimes * to the receive queue of a socket. If present, 637df8bae1dSRodney W. Grimes * m0 must include a packet header with total length. 638df8bae1dSRodney W. Grimes * Returns 0 if no space in sockbuf or insufficient mbufs. 639df8bae1dSRodney W. Grimes */ 64026f9a767SRodney W. Grimes int 641df8bae1dSRodney W. Grimes sbappendaddr(sb, asa, m0, control) 642df8bae1dSRodney W. Grimes register struct sockbuf *sb; 643df8bae1dSRodney W. Grimes struct sockaddr *asa; 644df8bae1dSRodney W. Grimes struct mbuf *m0, *control; 645df8bae1dSRodney W. Grimes { 646df8bae1dSRodney W. Grimes register struct mbuf *m, *n; 647df8bae1dSRodney W. Grimes int space = asa->sa_len; 648df8bae1dSRodney W. Grimes 649df8bae1dSRodney W. Grimes if (m0 && (m0->m_flags & M_PKTHDR) == 0) 650df8bae1dSRodney W. Grimes panic("sbappendaddr"); 651df8bae1dSRodney W. Grimes if (m0) 652df8bae1dSRodney W. Grimes space += m0->m_pkthdr.len; 653df8bae1dSRodney W. Grimes for (n = control; n; n = n->m_next) { 654df8bae1dSRodney W. Grimes space += n->m_len; 655df8bae1dSRodney W. Grimes if (n->m_next == 0) /* keep pointer to last control buf */ 656df8bae1dSRodney W. Grimes break; 657df8bae1dSRodney W. Grimes } 658df8bae1dSRodney W. Grimes if (space > sbspace(sb)) 659df8bae1dSRodney W. Grimes return (0); 660df8bae1dSRodney W. Grimes if (asa->sa_len > MLEN) 661df8bae1dSRodney W. Grimes return (0); 662df8bae1dSRodney W. Grimes MGET(m, M_DONTWAIT, MT_SONAME); 663df8bae1dSRodney W. Grimes if (m == 0) 664df8bae1dSRodney W. Grimes return (0); 665df8bae1dSRodney W. Grimes m->m_len = asa->sa_len; 666df8bae1dSRodney W. Grimes bcopy((caddr_t)asa, mtod(m, caddr_t), asa->sa_len); 667df8bae1dSRodney W. Grimes if (n) 668df8bae1dSRodney W. Grimes n->m_next = m0; /* concatenate data to control */ 669df8bae1dSRodney W. Grimes else 670df8bae1dSRodney W. Grimes control = m0; 671df8bae1dSRodney W. Grimes m->m_next = control; 672df8bae1dSRodney W. Grimes for (n = m; n; n = n->m_next) 673df8bae1dSRodney W. Grimes sballoc(sb, n); 674797f2d22SPoul-Henning Kamp n = sb->sb_mb; 675797f2d22SPoul-Henning Kamp if (n) { 676df8bae1dSRodney W. Grimes while (n->m_nextpkt) 677df8bae1dSRodney W. Grimes n = n->m_nextpkt; 678df8bae1dSRodney W. Grimes n->m_nextpkt = m; 679df8bae1dSRodney W. Grimes } else 680df8bae1dSRodney W. Grimes sb->sb_mb = m; 681df8bae1dSRodney W. Grimes return (1); 682df8bae1dSRodney W. Grimes } 683df8bae1dSRodney W. Grimes 68426f9a767SRodney W. Grimes int 685df8bae1dSRodney W. Grimes sbappendcontrol(sb, m0, control) 686df8bae1dSRodney W. Grimes struct sockbuf *sb; 687df8bae1dSRodney W. Grimes struct mbuf *control, *m0; 688df8bae1dSRodney W. Grimes { 689df8bae1dSRodney W. Grimes register struct mbuf *m, *n; 690df8bae1dSRodney W. Grimes int space = 0; 691df8bae1dSRodney W. Grimes 692df8bae1dSRodney W. Grimes if (control == 0) 693df8bae1dSRodney W. Grimes panic("sbappendcontrol"); 694df8bae1dSRodney W. Grimes for (m = control; ; m = m->m_next) { 695df8bae1dSRodney W. Grimes space += m->m_len; 696df8bae1dSRodney W. Grimes if (m->m_next == 0) 697df8bae1dSRodney W. Grimes break; 698df8bae1dSRodney W. Grimes } 699df8bae1dSRodney W. Grimes n = m; /* save pointer to last control buffer */ 700df8bae1dSRodney W. Grimes for (m = m0; m; m = m->m_next) 701df8bae1dSRodney W. Grimes space += m->m_len; 702df8bae1dSRodney W. Grimes if (space > sbspace(sb)) 703df8bae1dSRodney W. Grimes return (0); 704df8bae1dSRodney W. Grimes n->m_next = m0; /* concatenate data to control */ 705df8bae1dSRodney W. Grimes for (m = control; m; m = m->m_next) 706df8bae1dSRodney W. Grimes sballoc(sb, m); 707797f2d22SPoul-Henning Kamp n = sb->sb_mb; 708797f2d22SPoul-Henning Kamp if (n) { 709df8bae1dSRodney W. Grimes while (n->m_nextpkt) 710df8bae1dSRodney W. Grimes n = n->m_nextpkt; 711df8bae1dSRodney W. Grimes n->m_nextpkt = control; 712df8bae1dSRodney W. Grimes } else 713df8bae1dSRodney W. Grimes sb->sb_mb = control; 714df8bae1dSRodney W. Grimes return (1); 715df8bae1dSRodney W. Grimes } 716df8bae1dSRodney W. Grimes 717df8bae1dSRodney W. Grimes /* 718df8bae1dSRodney W. Grimes * Compress mbuf chain m into the socket 719df8bae1dSRodney W. Grimes * buffer sb following mbuf n. If n 720df8bae1dSRodney W. Grimes * is null, the buffer is presumed empty. 721df8bae1dSRodney W. Grimes */ 72226f9a767SRodney W. Grimes void 723df8bae1dSRodney W. Grimes sbcompress(sb, m, n) 724df8bae1dSRodney W. Grimes register struct sockbuf *sb; 725df8bae1dSRodney W. Grimes register struct mbuf *m, *n; 726df8bae1dSRodney W. Grimes { 727df8bae1dSRodney W. Grimes register int eor = 0; 728df8bae1dSRodney W. Grimes register struct mbuf *o; 729df8bae1dSRodney W. Grimes 730df8bae1dSRodney W. Grimes while (m) { 731df8bae1dSRodney W. Grimes eor |= m->m_flags & M_EOR; 732df8bae1dSRodney W. Grimes if (m->m_len == 0 && 733df8bae1dSRodney W. Grimes (eor == 0 || 734df8bae1dSRodney W. Grimes (((o = m->m_next) || (o = n)) && 735df8bae1dSRodney W. Grimes o->m_type == m->m_type))) { 736df8bae1dSRodney W. Grimes m = m_free(m); 737df8bae1dSRodney W. Grimes continue; 738df8bae1dSRodney W. Grimes } 73932af0d74SDavid Malone if (n && (n->m_flags & M_EOR) == 0 && 74032af0d74SDavid Malone M_WRITABLE(n) && 74132af0d74SDavid Malone m->m_len <= MCLBYTES / 4 && /* XXX: Don't copy too much */ 74232af0d74SDavid Malone m->m_len <= M_TRAILINGSPACE(n) && 743df8bae1dSRodney W. Grimes n->m_type == m->m_type) { 744df8bae1dSRodney W. Grimes bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len, 745df8bae1dSRodney W. Grimes (unsigned)m->m_len); 746df8bae1dSRodney W. Grimes n->m_len += m->m_len; 747df8bae1dSRodney W. Grimes sb->sb_cc += m->m_len; 748df8bae1dSRodney W. Grimes m = m_free(m); 749df8bae1dSRodney W. Grimes continue; 750df8bae1dSRodney W. Grimes } 751df8bae1dSRodney W. Grimes if (n) 752df8bae1dSRodney W. Grimes n->m_next = m; 753df8bae1dSRodney W. Grimes else 754df8bae1dSRodney W. Grimes sb->sb_mb = m; 755df8bae1dSRodney W. Grimes sballoc(sb, m); 756df8bae1dSRodney W. Grimes n = m; 757df8bae1dSRodney W. Grimes m->m_flags &= ~M_EOR; 758df8bae1dSRodney W. Grimes m = m->m_next; 759df8bae1dSRodney W. Grimes n->m_next = 0; 760df8bae1dSRodney W. Grimes } 761df8bae1dSRodney W. Grimes if (eor) { 762df8bae1dSRodney W. Grimes if (n) 763df8bae1dSRodney W. Grimes n->m_flags |= eor; 764df8bae1dSRodney W. Grimes else 765df8bae1dSRodney W. Grimes printf("semi-panic: sbcompress\n"); 766df8bae1dSRodney W. Grimes } 767df8bae1dSRodney W. Grimes } 768df8bae1dSRodney W. Grimes 769df8bae1dSRodney W. Grimes /* 770df8bae1dSRodney W. Grimes * Free all mbufs in a sockbuf. 771df8bae1dSRodney W. Grimes * Check that all resources are reclaimed. 772df8bae1dSRodney W. Grimes */ 77326f9a767SRodney W. Grimes void 774df8bae1dSRodney W. Grimes sbflush(sb) 775df8bae1dSRodney W. Grimes register struct sockbuf *sb; 776df8bae1dSRodney W. Grimes { 777df8bae1dSRodney W. Grimes 778df8bae1dSRodney W. Grimes if (sb->sb_flags & SB_LOCK) 779253ab668SAndrey A. Chernov panic("sbflush: locked"); 78023f84772SPierre Beyssac while (sb->sb_mbcnt) { 78123f84772SPierre Beyssac /* 78223f84772SPierre Beyssac * Don't call sbdrop(sb, 0) if the leading mbuf is non-empty: 78323f84772SPierre Beyssac * we would loop forever. Panic instead. 78423f84772SPierre Beyssac */ 78523f84772SPierre Beyssac if (!sb->sb_cc && (sb->sb_mb == NULL || sb->sb_mb->m_len)) 78623f84772SPierre Beyssac break; 787df8bae1dSRodney W. Grimes sbdrop(sb, (int)sb->sb_cc); 78823f84772SPierre Beyssac } 7890931333fSBill Fenner if (sb->sb_cc || sb->sb_mb || sb->sb_mbcnt) 7900931333fSBill Fenner panic("sbflush: cc %ld || mb %p || mbcnt %ld", sb->sb_cc, (void *)sb->sb_mb, sb->sb_mbcnt); 791df8bae1dSRodney W. Grimes } 792df8bae1dSRodney W. Grimes 793df8bae1dSRodney W. Grimes /* 794df8bae1dSRodney W. Grimes * Drop data from (the front of) a sockbuf. 795df8bae1dSRodney W. Grimes */ 79626f9a767SRodney W. Grimes void 797df8bae1dSRodney W. Grimes sbdrop(sb, len) 798df8bae1dSRodney W. Grimes register struct sockbuf *sb; 799df8bae1dSRodney W. Grimes register int len; 800df8bae1dSRodney W. Grimes { 801ecde8f7cSMatthew Dillon register struct mbuf *m; 802df8bae1dSRodney W. Grimes struct mbuf *next; 803df8bae1dSRodney W. Grimes 804df8bae1dSRodney W. Grimes next = (m = sb->sb_mb) ? m->m_nextpkt : 0; 805df8bae1dSRodney W. Grimes while (len > 0) { 806df8bae1dSRodney W. Grimes if (m == 0) { 807df8bae1dSRodney W. Grimes if (next == 0) 808df8bae1dSRodney W. Grimes panic("sbdrop"); 809df8bae1dSRodney W. Grimes m = next; 810df8bae1dSRodney W. Grimes next = m->m_nextpkt; 811df8bae1dSRodney W. Grimes continue; 812df8bae1dSRodney W. Grimes } 813df8bae1dSRodney W. Grimes if (m->m_len > len) { 814df8bae1dSRodney W. Grimes m->m_len -= len; 815df8bae1dSRodney W. Grimes m->m_data += len; 816df8bae1dSRodney W. Grimes sb->sb_cc -= len; 817df8bae1dSRodney W. Grimes break; 818df8bae1dSRodney W. Grimes } 819df8bae1dSRodney W. Grimes len -= m->m_len; 820df8bae1dSRodney W. Grimes sbfree(sb, m); 821ecde8f7cSMatthew Dillon m = m_free(m); 822df8bae1dSRodney W. Grimes } 823df8bae1dSRodney W. Grimes while (m && m->m_len == 0) { 824df8bae1dSRodney W. Grimes sbfree(sb, m); 825ecde8f7cSMatthew Dillon m = m_free(m); 826df8bae1dSRodney W. Grimes } 827df8bae1dSRodney W. Grimes if (m) { 828df8bae1dSRodney W. Grimes sb->sb_mb = m; 829df8bae1dSRodney W. Grimes m->m_nextpkt = next; 830df8bae1dSRodney W. Grimes } else 831df8bae1dSRodney W. Grimes sb->sb_mb = next; 832df8bae1dSRodney W. Grimes } 833df8bae1dSRodney W. Grimes 834df8bae1dSRodney W. Grimes /* 835df8bae1dSRodney W. Grimes * Drop a record off the front of a sockbuf 836df8bae1dSRodney W. Grimes * and move the next record to the front. 837df8bae1dSRodney W. Grimes */ 83826f9a767SRodney W. Grimes void 839df8bae1dSRodney W. Grimes sbdroprecord(sb) 840df8bae1dSRodney W. Grimes register struct sockbuf *sb; 841df8bae1dSRodney W. Grimes { 842ecde8f7cSMatthew Dillon register struct mbuf *m; 843df8bae1dSRodney W. Grimes 844df8bae1dSRodney W. Grimes m = sb->sb_mb; 845df8bae1dSRodney W. Grimes if (m) { 846df8bae1dSRodney W. Grimes sb->sb_mb = m->m_nextpkt; 847df8bae1dSRodney W. Grimes do { 848df8bae1dSRodney W. Grimes sbfree(sb, m); 849ecde8f7cSMatthew Dillon m = m_free(m); 850797f2d22SPoul-Henning Kamp } while (m); 851df8bae1dSRodney W. Grimes } 852df8bae1dSRodney W. Grimes } 8531e4ad9ceSGarrett Wollman 85482c23ebaSBill Fenner /* 85582c23ebaSBill Fenner * Create a "control" mbuf containing the specified data 85682c23ebaSBill Fenner * with the specified type for presentation on a socket buffer. 85782c23ebaSBill Fenner */ 85882c23ebaSBill Fenner struct mbuf * 85982c23ebaSBill Fenner sbcreatecontrol(p, size, type, level) 86082c23ebaSBill Fenner caddr_t p; 86182c23ebaSBill Fenner register int size; 86282c23ebaSBill Fenner int type, level; 86382c23ebaSBill Fenner { 86482c23ebaSBill Fenner register struct cmsghdr *cp; 86582c23ebaSBill Fenner struct mbuf *m; 86682c23ebaSBill Fenner 86759bdd405SDavid Malone if (CMSG_SPACE((u_int)size) > MCLBYTES) 8680b97e97cSYoshinobu Inoue return ((struct mbuf *) NULL); 86982c23ebaSBill Fenner if ((m = m_get(M_DONTWAIT, MT_CONTROL)) == NULL) 87082c23ebaSBill Fenner return ((struct mbuf *) NULL); 87159bdd405SDavid Malone if (CMSG_SPACE((u_int)size) > MLEN) { 87259bdd405SDavid Malone MCLGET(m, M_DONTWAIT); 87359bdd405SDavid Malone if ((m->m_flags & M_EXT) == 0) { 87459bdd405SDavid Malone m_free(m); 87559bdd405SDavid Malone return ((struct mbuf *) NULL); 87659bdd405SDavid Malone } 87759bdd405SDavid Malone } 87882c23ebaSBill Fenner cp = mtod(m, struct cmsghdr *); 87959bdd405SDavid Malone m->m_len = 0; 88059bdd405SDavid Malone KASSERT(CMSG_SPACE((u_int)size) <= M_TRAILINGSPACE(m), 88159bdd405SDavid Malone ("sbcreatecontrol: short mbuf")); 88259bdd405SDavid Malone if (p != NULL) 88382c23ebaSBill Fenner (void)memcpy(CMSG_DATA(cp), p, size); 8847d0d8dc3SYoshinobu Inoue m->m_len = CMSG_SPACE(size); 8857d0d8dc3SYoshinobu Inoue cp->cmsg_len = CMSG_LEN(size); 88682c23ebaSBill Fenner cp->cmsg_level = level; 88782c23ebaSBill Fenner cp->cmsg_type = type; 88882c23ebaSBill Fenner return (m); 88982c23ebaSBill Fenner } 89082c23ebaSBill Fenner 8911e4ad9ceSGarrett Wollman /* 8922c37256eSGarrett Wollman * Some routines that return EOPNOTSUPP for entry points that are not 8932c37256eSGarrett Wollman * supported by a protocol. Fill in as needed. 8941e4ad9ceSGarrett Wollman */ 8951e4ad9ceSGarrett Wollman int 89657bf258eSGarrett Wollman pru_accept_notsupp(struct socket *so, struct sockaddr **nam) 897d8392c6cSGarrett Wollman { 898d8392c6cSGarrett Wollman return EOPNOTSUPP; 899d8392c6cSGarrett Wollman } 900d8392c6cSGarrett Wollman 901d8392c6cSGarrett Wollman int 902b40ce416SJulian Elischer pru_connect_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td) 9039f907986SPeter Wemm { 9049f907986SPeter Wemm return EOPNOTSUPP; 9059f907986SPeter Wemm } 9069f907986SPeter Wemm 9079f907986SPeter Wemm int 9082c37256eSGarrett Wollman pru_connect2_notsupp(struct socket *so1, struct socket *so2) 9091e4ad9ceSGarrett Wollman { 9102c37256eSGarrett Wollman return EOPNOTSUPP; 9111e4ad9ceSGarrett Wollman } 912d8392c6cSGarrett Wollman 913d8392c6cSGarrett Wollman int 914ecbb00a2SDoug Rabson pru_control_notsupp(struct socket *so, u_long cmd, caddr_t data, 915b40ce416SJulian Elischer struct ifnet *ifp, struct thread *td) 916a29f300eSGarrett Wollman { 917a29f300eSGarrett Wollman return EOPNOTSUPP; 918a29f300eSGarrett Wollman } 919a29f300eSGarrett Wollman 920a29f300eSGarrett Wollman int 921b40ce416SJulian Elischer pru_listen_notsupp(struct socket *so, struct thread *td) 922d8392c6cSGarrett Wollman { 923d8392c6cSGarrett Wollman return EOPNOTSUPP; 924d8392c6cSGarrett Wollman } 925d8392c6cSGarrett Wollman 926d8392c6cSGarrett Wollman int 927d8392c6cSGarrett Wollman pru_rcvd_notsupp(struct socket *so, int flags) 928d8392c6cSGarrett Wollman { 929d8392c6cSGarrett Wollman return EOPNOTSUPP; 930d8392c6cSGarrett Wollman } 931d8392c6cSGarrett Wollman 932d8392c6cSGarrett Wollman int 933d8392c6cSGarrett Wollman pru_rcvoob_notsupp(struct socket *so, struct mbuf *m, int flags) 934d8392c6cSGarrett Wollman { 935d8392c6cSGarrett Wollman return EOPNOTSUPP; 936d8392c6cSGarrett Wollman } 937d8392c6cSGarrett Wollman 938d8392c6cSGarrett Wollman /* 939d8392c6cSGarrett Wollman * This isn't really a ``null'' operation, but it's the default one 940d8392c6cSGarrett Wollman * and doesn't do anything destructive. 941d8392c6cSGarrett Wollman */ 942d8392c6cSGarrett Wollman int 943d8392c6cSGarrett Wollman pru_sense_null(struct socket *so, struct stat *sb) 944d8392c6cSGarrett Wollman { 945d8392c6cSGarrett Wollman sb->st_blksize = so->so_snd.sb_hiwat; 946d8392c6cSGarrett Wollman return 0; 947d8392c6cSGarrett Wollman } 948639acc13SGarrett Wollman 949639acc13SGarrett Wollman /* 95057bf258eSGarrett Wollman * Make a copy of a sockaddr in a malloced buffer of type M_SONAME. 95157bf258eSGarrett Wollman */ 95257bf258eSGarrett Wollman struct sockaddr * 95357bf258eSGarrett Wollman dup_sockaddr(sa, canwait) 95457bf258eSGarrett Wollman struct sockaddr *sa; 95557bf258eSGarrett Wollman int canwait; 95657bf258eSGarrett Wollman { 95757bf258eSGarrett Wollman struct sockaddr *sa2; 95857bf258eSGarrett Wollman 95957bf258eSGarrett Wollman MALLOC(sa2, struct sockaddr *, sa->sa_len, M_SONAME, 96057bf258eSGarrett Wollman canwait ? M_WAITOK : M_NOWAIT); 96157bf258eSGarrett Wollman if (sa2) 96257bf258eSGarrett Wollman bcopy(sa, sa2, sa->sa_len); 96357bf258eSGarrett Wollman return sa2; 96457bf258eSGarrett Wollman } 96557bf258eSGarrett Wollman 96657bf258eSGarrett Wollman /* 96798271db4SGarrett Wollman * Create an external-format (``xsocket'') structure using the information 96898271db4SGarrett Wollman * in the kernel-format socket structure pointed to by so. This is done 96998271db4SGarrett Wollman * to reduce the spew of irrelevant information over this interface, 97098271db4SGarrett Wollman * to isolate user code from changes in the kernel structure, and 97198271db4SGarrett Wollman * potentially to provide information-hiding if we decide that 97298271db4SGarrett Wollman * some of this information should be hidden from users. 97398271db4SGarrett Wollman */ 97498271db4SGarrett Wollman void 97598271db4SGarrett Wollman sotoxsocket(struct socket *so, struct xsocket *xso) 97698271db4SGarrett Wollman { 97798271db4SGarrett Wollman xso->xso_len = sizeof *xso; 97898271db4SGarrett Wollman xso->xso_so = so; 97998271db4SGarrett Wollman xso->so_type = so->so_type; 98098271db4SGarrett Wollman xso->so_options = so->so_options; 98198271db4SGarrett Wollman xso->so_linger = so->so_linger; 98298271db4SGarrett Wollman xso->so_state = so->so_state; 98398271db4SGarrett Wollman xso->so_pcb = so->so_pcb; 98498271db4SGarrett Wollman xso->xso_protocol = so->so_proto->pr_protocol; 98598271db4SGarrett Wollman xso->xso_family = so->so_proto->pr_domain->dom_family; 98698271db4SGarrett Wollman xso->so_qlen = so->so_qlen; 98798271db4SGarrett Wollman xso->so_incqlen = so->so_incqlen; 98898271db4SGarrett Wollman xso->so_qlimit = so->so_qlimit; 98998271db4SGarrett Wollman xso->so_timeo = so->so_timeo; 99098271db4SGarrett Wollman xso->so_error = so->so_error; 991831d27a9SDon Lewis xso->so_pgid = so->so_sigio ? so->so_sigio->sio_pgid : 0; 99298271db4SGarrett Wollman xso->so_oobmark = so->so_oobmark; 99398271db4SGarrett Wollman sbtoxsockbuf(&so->so_snd, &xso->so_snd); 99498271db4SGarrett Wollman sbtoxsockbuf(&so->so_rcv, &xso->so_rcv); 9952f9a2132SBrian Feldman xso->so_uid = so->so_cred->cr_uid; 99698271db4SGarrett Wollman } 99798271db4SGarrett Wollman 99898271db4SGarrett Wollman /* 99998271db4SGarrett Wollman * This does the same for sockbufs. Note that the xsockbuf structure, 100098271db4SGarrett Wollman * since it is always embedded in a socket, does not include a self 100198271db4SGarrett Wollman * pointer nor a length. We make this entry point public in case 100298271db4SGarrett Wollman * some other mechanism needs it. 100398271db4SGarrett Wollman */ 100498271db4SGarrett Wollman void 100598271db4SGarrett Wollman sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb) 100698271db4SGarrett Wollman { 100798271db4SGarrett Wollman xsb->sb_cc = sb->sb_cc; 100898271db4SGarrett Wollman xsb->sb_hiwat = sb->sb_hiwat; 100998271db4SGarrett Wollman xsb->sb_mbcnt = sb->sb_mbcnt; 101098271db4SGarrett Wollman xsb->sb_mbmax = sb->sb_mbmax; 101198271db4SGarrett Wollman xsb->sb_lowat = sb->sb_lowat; 101298271db4SGarrett Wollman xsb->sb_flags = sb->sb_flags; 101398271db4SGarrett Wollman xsb->sb_timeo = sb->sb_timeo; 101498271db4SGarrett Wollman } 101598271db4SGarrett Wollman 101698271db4SGarrett Wollman /* 1017639acc13SGarrett Wollman * Here is the definition of some of the basic objects in the kern.ipc 1018639acc13SGarrett Wollman * branch of the MIB. 1019639acc13SGarrett Wollman */ 1020639acc13SGarrett Wollman SYSCTL_NODE(_kern, KERN_IPC, ipc, CTLFLAG_RW, 0, "IPC"); 1021639acc13SGarrett Wollman 1022639acc13SGarrett Wollman /* This takes the place of kern.maxsockbuf, which moved to kern.ipc. */ 1023639acc13SGarrett Wollman static int dummy; 1024639acc13SGarrett Wollman SYSCTL_INT(_kern, KERN_DUMMY, dummy, CTLFLAG_RW, &dummy, 0, ""); 1025639acc13SGarrett Wollman 10263d177f46SBill Fumerola SYSCTL_INT(_kern_ipc, KIPC_MAXSOCKBUF, maxsockbuf, CTLFLAG_RW, 10273d177f46SBill Fumerola &sb_max, 0, "Maximum socket buffer size"); 102854d77689SJeff Roberson SYSCTL_INT(_kern_ipc, OID_AUTO, maxsockets, CTLFLAG_RD, 102954d77689SJeff Roberson &maxsockets, 0, "Maximum number of sockets avaliable"); 1030639acc13SGarrett Wollman SYSCTL_INT(_kern_ipc, KIPC_SOCKBUF_WASTE, sockbuf_waste_factor, CTLFLAG_RW, 1031639acc13SGarrett Wollman &sb_efficiency, 0, ""); 103254d77689SJeff Roberson 103354d77689SJeff Roberson /* 103454d77689SJeff Roberson * Initialise maxsockets 103554d77689SJeff Roberson */ 103654d77689SJeff Roberson static void init_maxsockets(void *ignored) 103754d77689SJeff Roberson { 103854d77689SJeff Roberson TUNABLE_INT_FETCH("kern.ipc.maxsockets", &maxsockets); 103954d77689SJeff Roberson maxsockets = imax(maxsockets, imax(maxfiles, nmbclusters)); 104054d77689SJeff Roberson } 104154d77689SJeff Roberson SYSINIT(param, SI_SUB_TUNABLES, SI_ORDER_ANY, init_maxsockets, NULL); 1042