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> 39df8bae1dSRodney W. Grimes #include <sys/systm.h> 4098271db4SGarrett Wollman #include <sys/domain.h> 41134c934cSMike Smith #include <sys/file.h> /* for maxfiles */ 42ff5c09daSGarrett Wollman #include <sys/kernel.h> 43fb919e4dSMark Murray #include <sys/lock.h> 44fb919e4dSMark Murray #include <sys/mutex.h> 45df8bae1dSRodney W. Grimes #include <sys/malloc.h> 46df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 47fb919e4dSMark Murray #include <sys/proc.h> 48df8bae1dSRodney W. Grimes #include <sys/protosw.h> 492f9a2132SBrian Feldman #include <sys/resourcevar.h> 50797f2d22SPoul-Henning Kamp #include <sys/stat.h> 51df8bae1dSRodney W. Grimes #include <sys/socket.h> 52df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 53797f2d22SPoul-Henning Kamp #include <sys/signalvar.h> 54ff5c09daSGarrett Wollman #include <sys/sysctl.h> 55bfbbc4aaSJason Evans #include <sys/aio.h> /* for aio_swake proto */ 56cb679c38SJonathan Lemon #include <sys/event.h> 5726f9a767SRodney W. Grimes 5854d77689SJeff Roberson int maxsockets; 5954d77689SJeff Roberson 6021d56e9cSAlfred Perlstein void (*aio_swake)(struct socket *, struct sockbuf *); 6121d56e9cSAlfred Perlstein 62df8bae1dSRodney W. Grimes /* 63df8bae1dSRodney W. Grimes * Primitive routines for operating on sockets and socket buffers 64df8bae1dSRodney W. Grimes */ 65df8bae1dSRodney W. Grimes 66ff5c09daSGarrett Wollman u_long sb_max = SB_MAX; /* XXX should be static */ 67df8bae1dSRodney W. Grimes 684b29bc4fSGarrett Wollman static u_long sb_efficiency = 8; /* parameter for sbreserve() */ 694b29bc4fSGarrett Wollman 70df8bae1dSRodney W. Grimes /* 71df8bae1dSRodney W. Grimes * Procedures to manipulate state flags of socket 72df8bae1dSRodney W. Grimes * and do appropriate wakeups. Normal sequence from the 73df8bae1dSRodney W. Grimes * active (originating) side is that soisconnecting() is 74df8bae1dSRodney W. Grimes * called during processing of connect() call, 75df8bae1dSRodney W. Grimes * resulting in an eventual call to soisconnected() if/when the 76df8bae1dSRodney W. Grimes * connection is established. When the connection is torn down 77df8bae1dSRodney W. Grimes * soisdisconnecting() is called during processing of disconnect() call, 78df8bae1dSRodney W. Grimes * and soisdisconnected() is called when the connection to the peer 79df8bae1dSRodney W. Grimes * is totally severed. The semantics of these routines are such that 80df8bae1dSRodney W. Grimes * connectionless protocols can call soisconnected() and soisdisconnected() 81df8bae1dSRodney W. Grimes * only, bypassing the in-progress calls when setting up a ``connection'' 82df8bae1dSRodney W. Grimes * takes no time. 83df8bae1dSRodney W. Grimes * 84df8bae1dSRodney W. Grimes * From the passive side, a socket is created with 85dc97381eSPeter Wemm * two queues of sockets: so_incomp for connections in progress 86dc97381eSPeter Wemm * and so_comp for connections already made and awaiting user acceptance. 87df8bae1dSRodney W. Grimes * As a protocol is preparing incoming connections, it creates a socket 88dc97381eSPeter Wemm * structure queued on so_incomp by calling sonewconn(). When the connection 89df8bae1dSRodney W. Grimes * is established, soisconnected() is called, and transfers the 90dc97381eSPeter Wemm * socket structure to so_comp, making it available to accept(). 91df8bae1dSRodney W. Grimes * 92df8bae1dSRodney W. Grimes * If a socket is closed with sockets on either 93dc97381eSPeter Wemm * so_incomp or so_comp, these sockets are dropped. 94df8bae1dSRodney W. Grimes * 95df8bae1dSRodney W. Grimes * If higher level protocols are implemented in 96df8bae1dSRodney W. Grimes * the kernel, the wakeups done here will sometimes 97df8bae1dSRodney W. Grimes * cause software-interrupt process scheduling. 98df8bae1dSRodney W. Grimes */ 99df8bae1dSRodney W. Grimes 10026f9a767SRodney W. Grimes void 101df8bae1dSRodney W. Grimes soisconnecting(so) 102df8bae1dSRodney W. Grimes register struct socket *so; 103df8bae1dSRodney W. Grimes { 104df8bae1dSRodney W. Grimes 105df8bae1dSRodney W. Grimes so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING); 106df8bae1dSRodney W. Grimes so->so_state |= SS_ISCONNECTING; 107df8bae1dSRodney W. Grimes } 108df8bae1dSRodney W. Grimes 10926f9a767SRodney W. Grimes void 110d48d4b25SSeigo Tanimura soisconnected_locked(so) 1118f4e4aa5SAlfred Perlstein struct socket *so; 112df8bae1dSRodney W. Grimes { 1138f4e4aa5SAlfred Perlstein struct socket *head = so->so_head; 114df8bae1dSRodney W. Grimes 115d48d4b25SSeigo Tanimura SIGIO_ASSERT(SX_SLOCKED); /* XXX */ 116df8bae1dSRodney W. Grimes so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING); 117df8bae1dSRodney W. Grimes so->so_state |= SS_ISCONNECTED; 118be24e9e8SDavid Greenman if (head && (so->so_state & SS_INCOMP)) { 119a79b7128SAlfred Perlstein if ((so->so_options & SO_ACCEPTFILTER) != 0) { 120a79b7128SAlfred Perlstein so->so_upcall = head->so_accf->so_accept_filter->accf_callback; 121a79b7128SAlfred Perlstein so->so_upcallarg = head->so_accf->so_accept_filter_arg; 122a79b7128SAlfred Perlstein so->so_rcv.sb_flags |= SB_UPCALL; 123a79b7128SAlfred Perlstein so->so_options &= ~SO_ACCEPTFILTER; 124d48d4b25SSeigo Tanimura SIGIO_SUNLOCK(); /* XXX */ 125d48d4b25SSeigo Tanimura so->so_upcall(so, so->so_upcallarg, 0); 126d48d4b25SSeigo Tanimura SIGIO_SLOCK(); 127d48d4b25SSeigo Tanimura return; 128d48d4b25SSeigo Tanimura } 129d48d4b25SSeigo Tanimura TAILQ_REMOVE(&head->so_incomp, so, so_list); 130d48d4b25SSeigo Tanimura head->so_incqlen--; 131d48d4b25SSeigo Tanimura so->so_state &= ~SS_INCOMP; 132d48d4b25SSeigo Tanimura TAILQ_INSERT_TAIL(&head->so_comp, so, so_list); 133d48d4b25SSeigo Tanimura so->so_state |= SS_COMP; 134d48d4b25SSeigo Tanimura sorwakeup_locked(head); 135d48d4b25SSeigo Tanimura wakeup_one(&head->so_timeo); 136d48d4b25SSeigo Tanimura } else { 137d48d4b25SSeigo Tanimura wakeup(&so->so_timeo); 138d48d4b25SSeigo Tanimura sorwakeup_locked(so); 139d48d4b25SSeigo Tanimura sowwakeup_locked(so); 140d48d4b25SSeigo Tanimura } 141d48d4b25SSeigo Tanimura } 142d48d4b25SSeigo Tanimura 143d48d4b25SSeigo Tanimura void 144d48d4b25SSeigo Tanimura soisconnected(so) 145d48d4b25SSeigo Tanimura struct socket *so; 146d48d4b25SSeigo Tanimura { 147d48d4b25SSeigo Tanimura struct socket *head = so->so_head; 148d48d4b25SSeigo Tanimura 149d48d4b25SSeigo Tanimura SIGIO_SLOCK(); 150d48d4b25SSeigo Tanimura so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING); 151d48d4b25SSeigo Tanimura so->so_state |= SS_ISCONNECTED; 152d48d4b25SSeigo Tanimura if (head && (so->so_state & SS_INCOMP)) { 153d48d4b25SSeigo Tanimura if ((so->so_options & SO_ACCEPTFILTER) != 0) { 154d48d4b25SSeigo Tanimura so->so_upcall = head->so_accf->so_accept_filter->accf_callback; 155d48d4b25SSeigo Tanimura so->so_upcallarg = head->so_accf->so_accept_filter_arg; 156d48d4b25SSeigo Tanimura so->so_rcv.sb_flags |= SB_UPCALL; 157d48d4b25SSeigo Tanimura so->so_options &= ~SO_ACCEPTFILTER; 158d48d4b25SSeigo Tanimura SIGIO_SUNLOCK(); 159a79b7128SAlfred Perlstein so->so_upcall(so, so->so_upcallarg, 0); 160a79b7128SAlfred Perlstein return; 161a79b7128SAlfred Perlstein } 162be24e9e8SDavid Greenman TAILQ_REMOVE(&head->so_incomp, so, so_list); 163a51764a8SPaul Traina head->so_incqlen--; 164be24e9e8SDavid Greenman so->so_state &= ~SS_INCOMP; 165be24e9e8SDavid Greenman TAILQ_INSERT_TAIL(&head->so_comp, so, so_list); 166e1f1827fSMike Silbersack head->so_qlen++; 167be24e9e8SDavid Greenman so->so_state |= SS_COMP; 168d48d4b25SSeigo Tanimura sorwakeup_locked(head); 169a91b8721SDavid Greenman wakeup_one(&head->so_timeo); 170df8bae1dSRodney W. Grimes } else { 171a91b8721SDavid Greenman wakeup(&so->so_timeo); 172d48d4b25SSeigo Tanimura sorwakeup_locked(so); 173d48d4b25SSeigo Tanimura sowwakeup_locked(so); 174df8bae1dSRodney W. Grimes } 175d48d4b25SSeigo Tanimura SIGIO_SUNLOCK(); 176df8bae1dSRodney W. Grimes } 177df8bae1dSRodney W. Grimes 17826f9a767SRodney W. Grimes void 179df8bae1dSRodney W. Grimes soisdisconnecting(so) 180df8bae1dSRodney W. Grimes register struct socket *so; 181df8bae1dSRodney W. Grimes { 182df8bae1dSRodney W. Grimes 183d48d4b25SSeigo Tanimura SIGIO_SLOCK(); 184df8bae1dSRodney W. Grimes so->so_state &= ~SS_ISCONNECTING; 185df8bae1dSRodney W. Grimes so->so_state |= (SS_ISDISCONNECTING|SS_CANTRCVMORE|SS_CANTSENDMORE); 186df8bae1dSRodney W. Grimes wakeup((caddr_t)&so->so_timeo); 187d48d4b25SSeigo Tanimura sowwakeup_locked(so); 188d48d4b25SSeigo Tanimura sorwakeup_locked(so); 189d48d4b25SSeigo Tanimura SIGIO_SUNLOCK(); 190d48d4b25SSeigo Tanimura } 191d48d4b25SSeigo Tanimura 192d48d4b25SSeigo Tanimura void 193d48d4b25SSeigo Tanimura soisdisconnected_locked(so) 194d48d4b25SSeigo Tanimura register struct socket *so; 195d48d4b25SSeigo Tanimura { 196d48d4b25SSeigo Tanimura 197d48d4b25SSeigo Tanimura SIGIO_ASSERT(SX_LOCKED); 198d48d4b25SSeigo Tanimura so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING); 199d48d4b25SSeigo Tanimura so->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE|SS_ISDISCONNECTED); 200d48d4b25SSeigo Tanimura wakeup((caddr_t)&so->so_timeo); 201d48d4b25SSeigo Tanimura sowwakeup_locked(so); 202d48d4b25SSeigo Tanimura sorwakeup_locked(so); 203df8bae1dSRodney W. Grimes } 204df8bae1dSRodney W. Grimes 20526f9a767SRodney W. Grimes void 206df8bae1dSRodney W. Grimes soisdisconnected(so) 207df8bae1dSRodney W. Grimes register struct socket *so; 208df8bae1dSRodney W. Grimes { 209df8bae1dSRodney W. Grimes 210d48d4b25SSeigo Tanimura SIGIO_SLOCK(); 211d48d4b25SSeigo Tanimura soisdisconnected_locked(so); 212d48d4b25SSeigo Tanimura SIGIO_SUNLOCK(); 213df8bae1dSRodney W. Grimes } 214df8bae1dSRodney W. Grimes 215df8bae1dSRodney W. Grimes /* 216df8bae1dSRodney W. Grimes * When an attempt at a new connection is noted on a socket 217df8bae1dSRodney W. Grimes * which accepts connections, sonewconn is called. If the 218df8bae1dSRodney W. Grimes * connection is possible (subject to space constraints, etc.) 219df8bae1dSRodney W. Grimes * then we allocate a new structure, propoerly linked into the 220df8bae1dSRodney W. Grimes * data structure of the original socket, and return this. 221df8bae1dSRodney W. Grimes * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED. 222b1e4abd2SMatthew Dillon * 223b1e4abd2SMatthew Dillon * note: the ref count on the socket is 0 on return 224df8bae1dSRodney W. Grimes */ 225df8bae1dSRodney W. Grimes struct socket * 226548af278SBill Fenner sonewconn(head, connstatus) 227df8bae1dSRodney W. Grimes register struct socket *head; 228df8bae1dSRodney W. Grimes int connstatus; 229df8bae1dSRodney W. Grimes { 230df8bae1dSRodney W. Grimes register struct socket *so; 231df8bae1dSRodney W. Grimes 232ebb0cbeaSPaul Traina if (head->so_qlen > 3 * head->so_qlimit / 2) 233df8bae1dSRodney W. Grimes return ((struct socket *)0); 23498271db4SGarrett Wollman so = soalloc(0); 235df8bae1dSRodney W. Grimes if (so == NULL) 236df8bae1dSRodney W. Grimes return ((struct socket *)0); 237205b2b61SPeter Wemm if ((head->so_options & SO_ACCEPTFILTER) != 0) 238205b2b61SPeter Wemm connstatus = 0; 239be24e9e8SDavid Greenman so->so_head = head; 240df8bae1dSRodney W. Grimes so->so_type = head->so_type; 241df8bae1dSRodney W. Grimes so->so_options = head->so_options &~ SO_ACCEPTCONN; 242df8bae1dSRodney W. Grimes so->so_linger = head->so_linger; 243df8bae1dSRodney W. Grimes so->so_state = head->so_state | SS_NOFDREF; 244df8bae1dSRodney W. Grimes so->so_proto = head->so_proto; 245df8bae1dSRodney W. Grimes so->so_timeo = head->so_timeo; 246bd78ceceSJohn Baldwin so->so_cred = crhold(head->so_cred); 2472f9a2132SBrian Feldman if (soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat) || 2482f9a2132SBrian Feldman (*so->so_proto->pr_usrreqs->pru_attach)(so, 0, NULL)) { 249b1e4abd2SMatthew Dillon sotryfree(so); 250b1396a35SGarrett Wollman return ((struct socket *)0); 251b1396a35SGarrett Wollman } 252b1396a35SGarrett Wollman 253be24e9e8SDavid Greenman if (connstatus) { 254be24e9e8SDavid Greenman TAILQ_INSERT_TAIL(&head->so_comp, so, so_list); 255be24e9e8SDavid Greenman so->so_state |= SS_COMP; 256e1f1827fSMike Silbersack head->so_qlen++; 257be24e9e8SDavid Greenman } else { 258e1f1827fSMike Silbersack if (head->so_incqlen >= head->so_qlimit) { 259e1f1827fSMike Silbersack struct socket *sp; 260e1f1827fSMike Silbersack sp = TAILQ_FIRST(&head->so_incomp); 261e1f1827fSMike Silbersack (void) soabort(sp); 262e1f1827fSMike Silbersack } 263be24e9e8SDavid Greenman TAILQ_INSERT_TAIL(&head->so_incomp, so, so_list); 264be24e9e8SDavid Greenman so->so_state |= SS_INCOMP; 265ebb0cbeaSPaul Traina head->so_incqlen++; 266be24e9e8SDavid Greenman } 267df8bae1dSRodney W. Grimes if (connstatus) { 268d48d4b25SSeigo Tanimura SIGIO_SLOCK(); 269d48d4b25SSeigo Tanimura sorwakeup_locked(head); 270d48d4b25SSeigo Tanimura SIGIO_SUNLOCK(); 271df8bae1dSRodney W. Grimes } 272df8bae1dSRodney W. Grimes return (so); 273df8bae1dSRodney W. Grimes } 274df8bae1dSRodney W. Grimes 275df8bae1dSRodney W. Grimes /* 276df8bae1dSRodney W. Grimes * Socantsendmore indicates that no more data will be sent on the 277df8bae1dSRodney W. Grimes * socket; it would normally be applied to a socket when the user 278df8bae1dSRodney W. Grimes * informs the system that no more data is to be sent, by the protocol 279df8bae1dSRodney W. Grimes * code (in case PRU_SHUTDOWN). Socantrcvmore indicates that no more data 280df8bae1dSRodney W. Grimes * will be received, and will normally be applied to the socket by a 281df8bae1dSRodney W. Grimes * protocol when it detects that the peer will send no more data. 282df8bae1dSRodney W. Grimes * Data queued for reading in the socket may yet be read. 283df8bae1dSRodney W. Grimes */ 284df8bae1dSRodney W. Grimes 28526f9a767SRodney W. Grimes void 286df8bae1dSRodney W. Grimes socantsendmore(so) 287df8bae1dSRodney W. Grimes struct socket *so; 288df8bae1dSRodney W. Grimes { 289df8bae1dSRodney W. Grimes 290d48d4b25SSeigo Tanimura SIGIO_SLOCK(); 291df8bae1dSRodney W. Grimes so->so_state |= SS_CANTSENDMORE; 292d48d4b25SSeigo Tanimura sowwakeup_locked(so); 293d48d4b25SSeigo Tanimura SIGIO_SUNLOCK(); 294df8bae1dSRodney W. Grimes } 295df8bae1dSRodney W. Grimes 29626f9a767SRodney W. Grimes void 297df8bae1dSRodney W. Grimes socantrcvmore(so) 298df8bae1dSRodney W. Grimes struct socket *so; 299df8bae1dSRodney W. Grimes { 300df8bae1dSRodney W. Grimes 301d48d4b25SSeigo Tanimura SIGIO_SLOCK(); 302df8bae1dSRodney W. Grimes so->so_state |= SS_CANTRCVMORE; 303d48d4b25SSeigo Tanimura sorwakeup_locked(so); 304d48d4b25SSeigo Tanimura SIGIO_SUNLOCK(); 305df8bae1dSRodney W. Grimes } 306df8bae1dSRodney W. Grimes 307df8bae1dSRodney W. Grimes /* 308df8bae1dSRodney W. Grimes * Wait for data to arrive at/drain from a socket buffer. 309df8bae1dSRodney W. Grimes */ 31026f9a767SRodney W. Grimes int 311df8bae1dSRodney W. Grimes sbwait(sb) 312df8bae1dSRodney W. Grimes struct sockbuf *sb; 313df8bae1dSRodney W. Grimes { 314df8bae1dSRodney W. Grimes 315df8bae1dSRodney W. Grimes sb->sb_flags |= SB_WAIT; 316df8bae1dSRodney W. Grimes return (tsleep((caddr_t)&sb->sb_cc, 31747daf5d5SBruce Evans (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH, "sbwait", 318df8bae1dSRodney W. Grimes sb->sb_timeo)); 319df8bae1dSRodney W. Grimes } 320df8bae1dSRodney W. Grimes 321df8bae1dSRodney W. Grimes /* 322df8bae1dSRodney W. Grimes * Lock a sockbuf already known to be locked; 323df8bae1dSRodney W. Grimes * return any error returned from sleep (EINTR). 324df8bae1dSRodney W. Grimes */ 32526f9a767SRodney W. Grimes int 326df8bae1dSRodney W. Grimes sb_lock(sb) 327df8bae1dSRodney W. Grimes register struct sockbuf *sb; 328df8bae1dSRodney W. Grimes { 329df8bae1dSRodney W. Grimes int error; 330df8bae1dSRodney W. Grimes 331df8bae1dSRodney W. Grimes while (sb->sb_flags & SB_LOCK) { 332df8bae1dSRodney W. Grimes sb->sb_flags |= SB_WANT; 333797f2d22SPoul-Henning Kamp error = tsleep((caddr_t)&sb->sb_flags, 334df8bae1dSRodney W. Grimes (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK|PCATCH, 33547daf5d5SBruce Evans "sblock", 0); 336797f2d22SPoul-Henning Kamp if (error) 337df8bae1dSRodney W. Grimes return (error); 338df8bae1dSRodney W. Grimes } 339df8bae1dSRodney W. Grimes sb->sb_flags |= SB_LOCK; 340df8bae1dSRodney W. Grimes return (0); 341df8bae1dSRodney W. Grimes } 342df8bae1dSRodney W. Grimes 343df8bae1dSRodney W. Grimes /* 344df8bae1dSRodney W. Grimes * Wakeup processes waiting on a socket buffer. 345df8bae1dSRodney W. Grimes * Do asynchronous notification via SIGIO 346df8bae1dSRodney W. Grimes * if the socket has the SS_ASYNC flag set. 347df8bae1dSRodney W. Grimes */ 34826f9a767SRodney W. Grimes void 349df8bae1dSRodney W. Grimes sowakeup(so, sb) 350df8bae1dSRodney W. Grimes register struct socket *so; 351df8bae1dSRodney W. Grimes register struct sockbuf *sb; 352df8bae1dSRodney W. Grimes { 353d48d4b25SSeigo Tanimura SIGIO_ASSERT(SX_LOCKED); 354d48d4b25SSeigo Tanimura 355df8bae1dSRodney W. Grimes selwakeup(&sb->sb_sel); 356df8bae1dSRodney W. Grimes sb->sb_flags &= ~SB_SEL; 357df8bae1dSRodney W. Grimes if (sb->sb_flags & SB_WAIT) { 358df8bae1dSRodney W. Grimes sb->sb_flags &= ~SB_WAIT; 359df8bae1dSRodney W. Grimes wakeup((caddr_t)&sb->sb_cc); 360df8bae1dSRodney W. Grimes } 361831d27a9SDon Lewis if ((so->so_state & SS_ASYNC) && so->so_sigio != NULL) 362831d27a9SDon Lewis pgsigio(so->so_sigio, SIGIO, 0); 3634dc75870SPeter Wemm if (sb->sb_flags & SB_UPCALL) 3644dc75870SPeter Wemm (*so->so_upcall)(so, so->so_upcallarg, M_DONTWAIT); 365bfbbc4aaSJason Evans if (sb->sb_flags & SB_AIO) 366bfbbc4aaSJason Evans aio_swake(so, sb); 367cb679c38SJonathan Lemon KNOTE(&sb->sb_sel.si_note, 0); 368df8bae1dSRodney W. Grimes } 369df8bae1dSRodney W. Grimes 370df8bae1dSRodney W. Grimes /* 371df8bae1dSRodney W. Grimes * Socket buffer (struct sockbuf) utility routines. 372df8bae1dSRodney W. Grimes * 373df8bae1dSRodney W. Grimes * Each socket contains two socket buffers: one for sending data and 374df8bae1dSRodney W. Grimes * one for receiving data. Each buffer contains a queue of mbufs, 375df8bae1dSRodney W. Grimes * information about the number of mbufs and amount of data in the 376df8bae1dSRodney W. Grimes * queue, and other fields allowing select() statements and notification 377df8bae1dSRodney W. Grimes * on data availability to be implemented. 378df8bae1dSRodney W. Grimes * 379df8bae1dSRodney W. Grimes * Data stored in a socket buffer is maintained as a list of records. 380df8bae1dSRodney W. Grimes * Each record is a list of mbufs chained together with the m_next 381df8bae1dSRodney W. Grimes * field. Records are chained together with the m_nextpkt field. The upper 382df8bae1dSRodney W. Grimes * level routine soreceive() expects the following conventions to be 383df8bae1dSRodney W. Grimes * observed when placing information in the receive buffer: 384df8bae1dSRodney W. Grimes * 385df8bae1dSRodney W. Grimes * 1. If the protocol requires each message be preceded by the sender's 386df8bae1dSRodney W. Grimes * name, then a record containing that name must be present before 387df8bae1dSRodney W. Grimes * any associated data (mbuf's must be of type MT_SONAME). 388df8bae1dSRodney W. Grimes * 2. If the protocol supports the exchange of ``access rights'' (really 389df8bae1dSRodney W. Grimes * just additional data associated with the message), and there are 390df8bae1dSRodney W. Grimes * ``rights'' to be received, then a record containing this data 391df8bae1dSRodney W. Grimes * should be present (mbuf's must be of type MT_RIGHTS). 392df8bae1dSRodney W. Grimes * 3. If a name or rights record exists, then it must be followed by 393df8bae1dSRodney W. Grimes * a data record, perhaps of zero length. 394df8bae1dSRodney W. Grimes * 395df8bae1dSRodney W. Grimes * Before using a new socket structure it is first necessary to reserve 396df8bae1dSRodney W. Grimes * buffer space to the socket, by calling sbreserve(). This should commit 397df8bae1dSRodney W. Grimes * some of the available buffer space in the system buffer pool for the 398df8bae1dSRodney W. Grimes * socket (currently, it does nothing but enforce limits). The space 399df8bae1dSRodney W. Grimes * should be released by calling sbrelease() when the socket is destroyed. 400df8bae1dSRodney W. Grimes */ 401df8bae1dSRodney W. Grimes 40226f9a767SRodney W. Grimes int 403df8bae1dSRodney W. Grimes soreserve(so, sndcc, rcvcc) 404df8bae1dSRodney W. Grimes register struct socket *so; 405df8bae1dSRodney W. Grimes u_long sndcc, rcvcc; 406df8bae1dSRodney W. Grimes { 407b40ce416SJulian Elischer struct thread *td = curthread; 408df8bae1dSRodney W. Grimes 409b40ce416SJulian Elischer if (sbreserve(&so->so_snd, sndcc, so, td) == 0) 410df8bae1dSRodney W. Grimes goto bad; 411b40ce416SJulian Elischer if (sbreserve(&so->so_rcv, rcvcc, so, td) == 0) 412df8bae1dSRodney W. Grimes goto bad2; 413df8bae1dSRodney W. Grimes if (so->so_rcv.sb_lowat == 0) 414df8bae1dSRodney W. Grimes so->so_rcv.sb_lowat = 1; 415df8bae1dSRodney W. Grimes if (so->so_snd.sb_lowat == 0) 416df8bae1dSRodney W. Grimes so->so_snd.sb_lowat = MCLBYTES; 417df8bae1dSRodney W. Grimes if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat) 418df8bae1dSRodney W. Grimes so->so_snd.sb_lowat = so->so_snd.sb_hiwat; 419df8bae1dSRodney W. Grimes return (0); 420df8bae1dSRodney W. Grimes bad2: 421ecf72308SBrian Feldman sbrelease(&so->so_snd, so); 422df8bae1dSRodney W. Grimes bad: 423df8bae1dSRodney W. Grimes return (ENOBUFS); 424df8bae1dSRodney W. Grimes } 425df8bae1dSRodney W. Grimes 426df8bae1dSRodney W. Grimes /* 427df8bae1dSRodney W. Grimes * Allot mbufs to a sockbuf. 428df8bae1dSRodney W. Grimes * Attempt to scale mbmax so that mbcnt doesn't become limiting 429df8bae1dSRodney W. Grimes * if buffering efficiency is near the normal case. 430df8bae1dSRodney W. Grimes */ 43126f9a767SRodney W. Grimes int 432b40ce416SJulian Elischer sbreserve(sb, cc, so, td) 433df8bae1dSRodney W. Grimes struct sockbuf *sb; 434df8bae1dSRodney W. Grimes u_long cc; 435ecf72308SBrian Feldman struct socket *so; 436b40ce416SJulian Elischer struct thread *td; 437df8bae1dSRodney W. Grimes { 438ecf72308SBrian Feldman 439ecf72308SBrian Feldman /* 440b40ce416SJulian Elischer * td will only be NULL when we're in an interrupt 441ecf72308SBrian Feldman * (e.g. in tcp_input()) 442ecf72308SBrian Feldman */ 4439f5193caSMike Silbersack if ((u_quad_t)cc > (u_quad_t)sb_max * MCLBYTES / (MSIZE + MCLBYTES)) 444df8bae1dSRodney W. Grimes return (0); 445f535380cSDon Lewis if (!chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, cc, 446b40ce416SJulian Elischer td ? td->td_proc->p_rlimit[RLIMIT_SBSIZE].rlim_cur : RLIM_INFINITY)) { 447ecf72308SBrian Feldman return (0); 448c6362551SAlfred Perlstein } 4494b29bc4fSGarrett Wollman sb->sb_mbmax = min(cc * sb_efficiency, sb_max); 450df8bae1dSRodney W. Grimes if (sb->sb_lowat > sb->sb_hiwat) 451df8bae1dSRodney W. Grimes sb->sb_lowat = sb->sb_hiwat; 452df8bae1dSRodney W. Grimes return (1); 453df8bae1dSRodney W. Grimes } 454df8bae1dSRodney W. Grimes 455df8bae1dSRodney W. Grimes /* 456df8bae1dSRodney W. Grimes * Free mbufs held by a socket, and reserved mbuf space. 457df8bae1dSRodney W. Grimes */ 45826f9a767SRodney W. Grimes void 459ecf72308SBrian Feldman sbrelease(sb, so) 460df8bae1dSRodney W. Grimes struct sockbuf *sb; 461ecf72308SBrian Feldman struct socket *so; 462df8bae1dSRodney W. Grimes { 463df8bae1dSRodney W. Grimes 464df8bae1dSRodney W. Grimes sbflush(sb); 465f535380cSDon Lewis (void)chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, 0, 466f535380cSDon Lewis RLIM_INFINITY); 4676aef685fSBrian Feldman sb->sb_mbmax = 0; 468df8bae1dSRodney W. Grimes } 469df8bae1dSRodney W. Grimes 470df8bae1dSRodney W. Grimes /* 471df8bae1dSRodney W. Grimes * Routines to add and remove 472df8bae1dSRodney W. Grimes * data from an mbuf queue. 473df8bae1dSRodney W. Grimes * 474df8bae1dSRodney W. Grimes * The routines sbappend() or sbappendrecord() are normally called to 475df8bae1dSRodney W. Grimes * append new mbufs to a socket buffer, after checking that adequate 476df8bae1dSRodney W. Grimes * space is available, comparing the function sbspace() with the amount 477df8bae1dSRodney W. Grimes * of data to be added. sbappendrecord() differs from sbappend() in 478df8bae1dSRodney W. Grimes * that data supplied is treated as the beginning of a new record. 479df8bae1dSRodney W. Grimes * To place a sender's address, optional access rights, and data in a 480df8bae1dSRodney W. Grimes * socket receive buffer, sbappendaddr() should be used. To place 481df8bae1dSRodney W. Grimes * access rights and data in a socket receive buffer, sbappendrights() 482df8bae1dSRodney W. Grimes * should be used. In either case, the new data begins a new record. 483df8bae1dSRodney W. Grimes * Note that unlike sbappend() and sbappendrecord(), these routines check 484df8bae1dSRodney W. Grimes * for the caller that there will be enough space to store the data. 485df8bae1dSRodney W. Grimes * Each fails if there is not enough space, or if it cannot find mbufs 486df8bae1dSRodney W. Grimes * to store additional information in. 487df8bae1dSRodney W. Grimes * 488df8bae1dSRodney W. Grimes * Reliable protocols may use the socket send buffer to hold data 489df8bae1dSRodney W. Grimes * awaiting acknowledgement. Data is normally copied from a socket 490df8bae1dSRodney W. Grimes * send buffer in a protocol with m_copy for output to a peer, 491df8bae1dSRodney W. Grimes * and then removing the data from the socket buffer with sbdrop() 492df8bae1dSRodney W. Grimes * or sbdroprecord() when the data is acknowledged by the peer. 493df8bae1dSRodney W. Grimes */ 494df8bae1dSRodney W. Grimes 495df8bae1dSRodney W. Grimes /* 496df8bae1dSRodney W. Grimes * Append mbuf chain m to the last record in the 497df8bae1dSRodney W. Grimes * socket buffer sb. The additional space associated 498df8bae1dSRodney W. Grimes * the mbuf chain is recorded in sb. Empty mbufs are 499df8bae1dSRodney W. Grimes * discarded and mbufs are compacted where possible. 500df8bae1dSRodney W. Grimes */ 50126f9a767SRodney W. Grimes void 502df8bae1dSRodney W. Grimes sbappend(sb, m) 503df8bae1dSRodney W. Grimes struct sockbuf *sb; 504df8bae1dSRodney W. Grimes struct mbuf *m; 505df8bae1dSRodney W. Grimes { 506df8bae1dSRodney W. Grimes register struct mbuf *n; 507df8bae1dSRodney W. Grimes 508df8bae1dSRodney W. Grimes if (m == 0) 509df8bae1dSRodney W. Grimes return; 510797f2d22SPoul-Henning Kamp n = sb->sb_mb; 511797f2d22SPoul-Henning Kamp if (n) { 512df8bae1dSRodney W. Grimes while (n->m_nextpkt) 513df8bae1dSRodney W. Grimes n = n->m_nextpkt; 514df8bae1dSRodney W. Grimes do { 515df8bae1dSRodney W. Grimes if (n->m_flags & M_EOR) { 516df8bae1dSRodney W. Grimes sbappendrecord(sb, m); /* XXXXXX!!!! */ 517df8bae1dSRodney W. Grimes return; 518df8bae1dSRodney W. Grimes } 519df8bae1dSRodney W. Grimes } while (n->m_next && (n = n->m_next)); 520df8bae1dSRodney W. Grimes } 521df8bae1dSRodney W. Grimes sbcompress(sb, m, n); 522df8bae1dSRodney W. Grimes } 523df8bae1dSRodney W. Grimes 524df8bae1dSRodney W. Grimes #ifdef SOCKBUF_DEBUG 52526f9a767SRodney W. Grimes void 526df8bae1dSRodney W. Grimes sbcheck(sb) 527df8bae1dSRodney W. Grimes register struct sockbuf *sb; 528df8bae1dSRodney W. Grimes { 529df8bae1dSRodney W. Grimes register struct mbuf *m; 5300931333fSBill Fenner register struct mbuf *n = 0; 5310931333fSBill Fenner register u_long len = 0, mbcnt = 0; 532df8bae1dSRodney W. Grimes 5330931333fSBill Fenner for (m = sb->sb_mb; m; m = n) { 5340931333fSBill Fenner n = m->m_nextpkt; 5350931333fSBill Fenner for (; m; m = m->m_next) { 536df8bae1dSRodney W. Grimes len += m->m_len; 537df8bae1dSRodney W. Grimes mbcnt += MSIZE; 538313861b8SJulian Elischer if (m->m_flags & M_EXT) /*XXX*/ /* pretty sure this is bogus */ 539df8bae1dSRodney W. Grimes mbcnt += m->m_ext.ext_size; 5400931333fSBill Fenner } 541df8bae1dSRodney W. Grimes } 542df8bae1dSRodney W. Grimes if (len != sb->sb_cc || mbcnt != sb->sb_mbcnt) { 5430931333fSBill Fenner printf("cc %ld != %ld || mbcnt %ld != %ld\n", len, sb->sb_cc, 544df8bae1dSRodney W. Grimes mbcnt, sb->sb_mbcnt); 545df8bae1dSRodney W. Grimes panic("sbcheck"); 546df8bae1dSRodney W. Grimes } 547df8bae1dSRodney W. Grimes } 548df8bae1dSRodney W. Grimes #endif 549df8bae1dSRodney W. Grimes 550df8bae1dSRodney W. Grimes /* 551df8bae1dSRodney W. Grimes * As above, except the mbuf chain 552df8bae1dSRodney W. Grimes * begins a new record. 553df8bae1dSRodney W. Grimes */ 55426f9a767SRodney W. Grimes void 555df8bae1dSRodney W. Grimes sbappendrecord(sb, m0) 556df8bae1dSRodney W. Grimes register struct sockbuf *sb; 557df8bae1dSRodney W. Grimes register struct mbuf *m0; 558df8bae1dSRodney W. Grimes { 559df8bae1dSRodney W. Grimes register struct mbuf *m; 560df8bae1dSRodney W. Grimes 561df8bae1dSRodney W. Grimes if (m0 == 0) 562df8bae1dSRodney W. Grimes return; 563797f2d22SPoul-Henning Kamp m = sb->sb_mb; 564797f2d22SPoul-Henning Kamp if (m) 565df8bae1dSRodney W. Grimes while (m->m_nextpkt) 566df8bae1dSRodney W. Grimes m = m->m_nextpkt; 567df8bae1dSRodney W. Grimes /* 568df8bae1dSRodney W. Grimes * Put the first mbuf on the queue. 569df8bae1dSRodney W. Grimes * Note this permits zero length records. 570df8bae1dSRodney W. Grimes */ 571df8bae1dSRodney W. Grimes sballoc(sb, m0); 572df8bae1dSRodney W. Grimes if (m) 573df8bae1dSRodney W. Grimes m->m_nextpkt = m0; 574df8bae1dSRodney W. Grimes else 575df8bae1dSRodney W. Grimes sb->sb_mb = m0; 576df8bae1dSRodney W. Grimes m = m0->m_next; 577df8bae1dSRodney W. Grimes m0->m_next = 0; 578df8bae1dSRodney W. Grimes if (m && (m0->m_flags & M_EOR)) { 579df8bae1dSRodney W. Grimes m0->m_flags &= ~M_EOR; 580df8bae1dSRodney W. Grimes m->m_flags |= M_EOR; 581df8bae1dSRodney W. Grimes } 582df8bae1dSRodney W. Grimes sbcompress(sb, m, m0); 583df8bae1dSRodney W. Grimes } 584df8bae1dSRodney W. Grimes 585df8bae1dSRodney W. Grimes /* 586df8bae1dSRodney W. Grimes * As above except that OOB data 587df8bae1dSRodney W. Grimes * is inserted at the beginning of the sockbuf, 588df8bae1dSRodney W. Grimes * but after any other OOB data. 589df8bae1dSRodney W. Grimes */ 59026f9a767SRodney W. Grimes void 591df8bae1dSRodney W. Grimes sbinsertoob(sb, m0) 592df8bae1dSRodney W. Grimes register struct sockbuf *sb; 593df8bae1dSRodney W. Grimes register struct mbuf *m0; 594df8bae1dSRodney W. Grimes { 595df8bae1dSRodney W. Grimes register struct mbuf *m; 596df8bae1dSRodney W. Grimes register struct mbuf **mp; 597df8bae1dSRodney W. Grimes 598df8bae1dSRodney W. Grimes if (m0 == 0) 599df8bae1dSRodney W. Grimes return; 600797f2d22SPoul-Henning Kamp for (mp = &sb->sb_mb; *mp ; mp = &((*mp)->m_nextpkt)) { 601797f2d22SPoul-Henning Kamp m = *mp; 602df8bae1dSRodney W. Grimes again: 603df8bae1dSRodney W. Grimes switch (m->m_type) { 604df8bae1dSRodney W. Grimes 605df8bae1dSRodney W. Grimes case MT_OOBDATA: 606df8bae1dSRodney W. Grimes continue; /* WANT next train */ 607df8bae1dSRodney W. Grimes 608df8bae1dSRodney W. Grimes case MT_CONTROL: 609797f2d22SPoul-Henning Kamp m = m->m_next; 610797f2d22SPoul-Henning Kamp if (m) 611df8bae1dSRodney W. Grimes goto again; /* inspect THIS train further */ 612df8bae1dSRodney W. Grimes } 613df8bae1dSRodney W. Grimes break; 614df8bae1dSRodney W. Grimes } 615df8bae1dSRodney W. Grimes /* 616df8bae1dSRodney W. Grimes * Put the first mbuf on the queue. 617df8bae1dSRodney W. Grimes * Note this permits zero length records. 618df8bae1dSRodney W. Grimes */ 619df8bae1dSRodney W. Grimes sballoc(sb, m0); 620df8bae1dSRodney W. Grimes m0->m_nextpkt = *mp; 621df8bae1dSRodney W. Grimes *mp = m0; 622df8bae1dSRodney W. Grimes m = m0->m_next; 623df8bae1dSRodney W. Grimes m0->m_next = 0; 624df8bae1dSRodney W. Grimes if (m && (m0->m_flags & M_EOR)) { 625df8bae1dSRodney W. Grimes m0->m_flags &= ~M_EOR; 626df8bae1dSRodney W. Grimes m->m_flags |= M_EOR; 627df8bae1dSRodney W. Grimes } 628df8bae1dSRodney W. Grimes sbcompress(sb, m, m0); 629df8bae1dSRodney W. Grimes } 630df8bae1dSRodney W. Grimes 631df8bae1dSRodney W. Grimes /* 632df8bae1dSRodney W. Grimes * Append address and data, and optionally, control (ancillary) data 633df8bae1dSRodney W. Grimes * to the receive queue of a socket. If present, 634df8bae1dSRodney W. Grimes * m0 must include a packet header with total length. 635df8bae1dSRodney W. Grimes * Returns 0 if no space in sockbuf or insufficient mbufs. 636df8bae1dSRodney W. Grimes */ 63726f9a767SRodney W. Grimes int 638df8bae1dSRodney W. Grimes sbappendaddr(sb, asa, m0, control) 639df8bae1dSRodney W. Grimes register struct sockbuf *sb; 640df8bae1dSRodney W. Grimes struct sockaddr *asa; 641df8bae1dSRodney W. Grimes struct mbuf *m0, *control; 642df8bae1dSRodney W. Grimes { 643df8bae1dSRodney W. Grimes register struct mbuf *m, *n; 644df8bae1dSRodney W. Grimes int space = asa->sa_len; 645df8bae1dSRodney W. Grimes 646df8bae1dSRodney W. Grimes if (m0 && (m0->m_flags & M_PKTHDR) == 0) 647df8bae1dSRodney W. Grimes panic("sbappendaddr"); 648df8bae1dSRodney W. Grimes if (m0) 649df8bae1dSRodney W. Grimes space += m0->m_pkthdr.len; 650df8bae1dSRodney W. Grimes for (n = control; n; n = n->m_next) { 651df8bae1dSRodney W. Grimes space += n->m_len; 652df8bae1dSRodney W. Grimes if (n->m_next == 0) /* keep pointer to last control buf */ 653df8bae1dSRodney W. Grimes break; 654df8bae1dSRodney W. Grimes } 655df8bae1dSRodney W. Grimes if (space > sbspace(sb)) 656df8bae1dSRodney W. Grimes return (0); 657df8bae1dSRodney W. Grimes if (asa->sa_len > MLEN) 658df8bae1dSRodney W. Grimes return (0); 659df8bae1dSRodney W. Grimes MGET(m, M_DONTWAIT, MT_SONAME); 660df8bae1dSRodney W. Grimes if (m == 0) 661df8bae1dSRodney W. Grimes return (0); 662df8bae1dSRodney W. Grimes m->m_len = asa->sa_len; 663df8bae1dSRodney W. Grimes bcopy((caddr_t)asa, mtod(m, caddr_t), asa->sa_len); 664df8bae1dSRodney W. Grimes if (n) 665df8bae1dSRodney W. Grimes n->m_next = m0; /* concatenate data to control */ 666df8bae1dSRodney W. Grimes else 667df8bae1dSRodney W. Grimes control = m0; 668df8bae1dSRodney W. Grimes m->m_next = control; 669df8bae1dSRodney W. Grimes for (n = m; n; n = n->m_next) 670df8bae1dSRodney W. Grimes sballoc(sb, n); 671797f2d22SPoul-Henning Kamp n = sb->sb_mb; 672797f2d22SPoul-Henning Kamp if (n) { 673df8bae1dSRodney W. Grimes while (n->m_nextpkt) 674df8bae1dSRodney W. Grimes n = n->m_nextpkt; 675df8bae1dSRodney W. Grimes n->m_nextpkt = m; 676df8bae1dSRodney W. Grimes } else 677df8bae1dSRodney W. Grimes sb->sb_mb = m; 678df8bae1dSRodney W. Grimes return (1); 679df8bae1dSRodney W. Grimes } 680df8bae1dSRodney W. Grimes 68126f9a767SRodney W. Grimes int 682df8bae1dSRodney W. Grimes sbappendcontrol(sb, m0, control) 683df8bae1dSRodney W. Grimes struct sockbuf *sb; 684df8bae1dSRodney W. Grimes struct mbuf *control, *m0; 685df8bae1dSRodney W. Grimes { 686df8bae1dSRodney W. Grimes register struct mbuf *m, *n; 687df8bae1dSRodney W. Grimes int space = 0; 688df8bae1dSRodney W. Grimes 689df8bae1dSRodney W. Grimes if (control == 0) 690df8bae1dSRodney W. Grimes panic("sbappendcontrol"); 691df8bae1dSRodney W. Grimes for (m = control; ; m = m->m_next) { 692df8bae1dSRodney W. Grimes space += m->m_len; 693df8bae1dSRodney W. Grimes if (m->m_next == 0) 694df8bae1dSRodney W. Grimes break; 695df8bae1dSRodney W. Grimes } 696df8bae1dSRodney W. Grimes n = m; /* save pointer to last control buffer */ 697df8bae1dSRodney W. Grimes for (m = m0; m; m = m->m_next) 698df8bae1dSRodney W. Grimes space += m->m_len; 699df8bae1dSRodney W. Grimes if (space > sbspace(sb)) 700df8bae1dSRodney W. Grimes return (0); 701df8bae1dSRodney W. Grimes n->m_next = m0; /* concatenate data to control */ 702df8bae1dSRodney W. Grimes for (m = control; m; m = m->m_next) 703df8bae1dSRodney W. Grimes sballoc(sb, m); 704797f2d22SPoul-Henning Kamp n = sb->sb_mb; 705797f2d22SPoul-Henning Kamp if (n) { 706df8bae1dSRodney W. Grimes while (n->m_nextpkt) 707df8bae1dSRodney W. Grimes n = n->m_nextpkt; 708df8bae1dSRodney W. Grimes n->m_nextpkt = control; 709df8bae1dSRodney W. Grimes } else 710df8bae1dSRodney W. Grimes sb->sb_mb = control; 711df8bae1dSRodney W. Grimes return (1); 712df8bae1dSRodney W. Grimes } 713df8bae1dSRodney W. Grimes 714df8bae1dSRodney W. Grimes /* 715df8bae1dSRodney W. Grimes * Compress mbuf chain m into the socket 716df8bae1dSRodney W. Grimes * buffer sb following mbuf n. If n 717df8bae1dSRodney W. Grimes * is null, the buffer is presumed empty. 718df8bae1dSRodney W. Grimes */ 71926f9a767SRodney W. Grimes void 720df8bae1dSRodney W. Grimes sbcompress(sb, m, n) 721df8bae1dSRodney W. Grimes register struct sockbuf *sb; 722df8bae1dSRodney W. Grimes register struct mbuf *m, *n; 723df8bae1dSRodney W. Grimes { 724df8bae1dSRodney W. Grimes register int eor = 0; 725df8bae1dSRodney W. Grimes register struct mbuf *o; 726df8bae1dSRodney W. Grimes 727df8bae1dSRodney W. Grimes while (m) { 728df8bae1dSRodney W. Grimes eor |= m->m_flags & M_EOR; 729df8bae1dSRodney W. Grimes if (m->m_len == 0 && 730df8bae1dSRodney W. Grimes (eor == 0 || 731df8bae1dSRodney W. Grimes (((o = m->m_next) || (o = n)) && 732df8bae1dSRodney W. Grimes o->m_type == m->m_type))) { 733df8bae1dSRodney W. Grimes m = m_free(m); 734df8bae1dSRodney W. Grimes continue; 735df8bae1dSRodney W. Grimes } 73632af0d74SDavid Malone if (n && (n->m_flags & M_EOR) == 0 && 73732af0d74SDavid Malone M_WRITABLE(n) && 73832af0d74SDavid Malone m->m_len <= MCLBYTES / 4 && /* XXX: Don't copy too much */ 73932af0d74SDavid Malone m->m_len <= M_TRAILINGSPACE(n) && 740df8bae1dSRodney W. Grimes n->m_type == m->m_type) { 741df8bae1dSRodney W. Grimes bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len, 742df8bae1dSRodney W. Grimes (unsigned)m->m_len); 743df8bae1dSRodney W. Grimes n->m_len += m->m_len; 744df8bae1dSRodney W. Grimes sb->sb_cc += m->m_len; 745df8bae1dSRodney W. Grimes m = m_free(m); 746df8bae1dSRodney W. Grimes continue; 747df8bae1dSRodney W. Grimes } 748df8bae1dSRodney W. Grimes if (n) 749df8bae1dSRodney W. Grimes n->m_next = m; 750df8bae1dSRodney W. Grimes else 751df8bae1dSRodney W. Grimes sb->sb_mb = m; 752df8bae1dSRodney W. Grimes sballoc(sb, m); 753df8bae1dSRodney W. Grimes n = m; 754df8bae1dSRodney W. Grimes m->m_flags &= ~M_EOR; 755df8bae1dSRodney W. Grimes m = m->m_next; 756df8bae1dSRodney W. Grimes n->m_next = 0; 757df8bae1dSRodney W. Grimes } 758df8bae1dSRodney W. Grimes if (eor) { 759df8bae1dSRodney W. Grimes if (n) 760df8bae1dSRodney W. Grimes n->m_flags |= eor; 761df8bae1dSRodney W. Grimes else 762df8bae1dSRodney W. Grimes printf("semi-panic: sbcompress\n"); 763df8bae1dSRodney W. Grimes } 764df8bae1dSRodney W. Grimes } 765df8bae1dSRodney W. Grimes 766df8bae1dSRodney W. Grimes /* 767df8bae1dSRodney W. Grimes * Free all mbufs in a sockbuf. 768df8bae1dSRodney W. Grimes * Check that all resources are reclaimed. 769df8bae1dSRodney W. Grimes */ 77026f9a767SRodney W. Grimes void 771df8bae1dSRodney W. Grimes sbflush(sb) 772df8bae1dSRodney W. Grimes register struct sockbuf *sb; 773df8bae1dSRodney W. Grimes { 774df8bae1dSRodney W. Grimes 775df8bae1dSRodney W. Grimes if (sb->sb_flags & SB_LOCK) 776253ab668SAndrey A. Chernov panic("sbflush: locked"); 77723f84772SPierre Beyssac while (sb->sb_mbcnt) { 77823f84772SPierre Beyssac /* 77923f84772SPierre Beyssac * Don't call sbdrop(sb, 0) if the leading mbuf is non-empty: 78023f84772SPierre Beyssac * we would loop forever. Panic instead. 78123f84772SPierre Beyssac */ 78223f84772SPierre Beyssac if (!sb->sb_cc && (sb->sb_mb == NULL || sb->sb_mb->m_len)) 78323f84772SPierre Beyssac break; 784df8bae1dSRodney W. Grimes sbdrop(sb, (int)sb->sb_cc); 78523f84772SPierre Beyssac } 7860931333fSBill Fenner if (sb->sb_cc || sb->sb_mb || sb->sb_mbcnt) 7870931333fSBill Fenner panic("sbflush: cc %ld || mb %p || mbcnt %ld", sb->sb_cc, (void *)sb->sb_mb, sb->sb_mbcnt); 788df8bae1dSRodney W. Grimes } 789df8bae1dSRodney W. Grimes 790df8bae1dSRodney W. Grimes /* 791df8bae1dSRodney W. Grimes * Drop data from (the front of) a sockbuf. 792df8bae1dSRodney W. Grimes */ 79326f9a767SRodney W. Grimes void 794df8bae1dSRodney W. Grimes sbdrop(sb, len) 795df8bae1dSRodney W. Grimes register struct sockbuf *sb; 796df8bae1dSRodney W. Grimes register int len; 797df8bae1dSRodney W. Grimes { 798ecde8f7cSMatthew Dillon register struct mbuf *m; 799df8bae1dSRodney W. Grimes struct mbuf *next; 800df8bae1dSRodney W. Grimes 801df8bae1dSRodney W. Grimes next = (m = sb->sb_mb) ? m->m_nextpkt : 0; 802df8bae1dSRodney W. Grimes while (len > 0) { 803df8bae1dSRodney W. Grimes if (m == 0) { 804df8bae1dSRodney W. Grimes if (next == 0) 805df8bae1dSRodney W. Grimes panic("sbdrop"); 806df8bae1dSRodney W. Grimes m = next; 807df8bae1dSRodney W. Grimes next = m->m_nextpkt; 808df8bae1dSRodney W. Grimes continue; 809df8bae1dSRodney W. Grimes } 810df8bae1dSRodney W. Grimes if (m->m_len > len) { 811df8bae1dSRodney W. Grimes m->m_len -= len; 812df8bae1dSRodney W. Grimes m->m_data += len; 813df8bae1dSRodney W. Grimes sb->sb_cc -= len; 814df8bae1dSRodney W. Grimes break; 815df8bae1dSRodney W. Grimes } 816df8bae1dSRodney W. Grimes len -= m->m_len; 817df8bae1dSRodney W. Grimes sbfree(sb, m); 818ecde8f7cSMatthew Dillon m = m_free(m); 819df8bae1dSRodney W. Grimes } 820df8bae1dSRodney W. Grimes while (m && m->m_len == 0) { 821df8bae1dSRodney W. Grimes sbfree(sb, m); 822ecde8f7cSMatthew Dillon m = m_free(m); 823df8bae1dSRodney W. Grimes } 824df8bae1dSRodney W. Grimes if (m) { 825df8bae1dSRodney W. Grimes sb->sb_mb = m; 826df8bae1dSRodney W. Grimes m->m_nextpkt = next; 827df8bae1dSRodney W. Grimes } else 828df8bae1dSRodney W. Grimes sb->sb_mb = next; 829df8bae1dSRodney W. Grimes } 830df8bae1dSRodney W. Grimes 831df8bae1dSRodney W. Grimes /* 832df8bae1dSRodney W. Grimes * Drop a record off the front of a sockbuf 833df8bae1dSRodney W. Grimes * and move the next record to the front. 834df8bae1dSRodney W. Grimes */ 83526f9a767SRodney W. Grimes void 836df8bae1dSRodney W. Grimes sbdroprecord(sb) 837df8bae1dSRodney W. Grimes register struct sockbuf *sb; 838df8bae1dSRodney W. Grimes { 839ecde8f7cSMatthew Dillon register struct mbuf *m; 840df8bae1dSRodney W. Grimes 841df8bae1dSRodney W. Grimes m = sb->sb_mb; 842df8bae1dSRodney W. Grimes if (m) { 843df8bae1dSRodney W. Grimes sb->sb_mb = m->m_nextpkt; 844df8bae1dSRodney W. Grimes do { 845df8bae1dSRodney W. Grimes sbfree(sb, m); 846ecde8f7cSMatthew Dillon m = m_free(m); 847797f2d22SPoul-Henning Kamp } while (m); 848df8bae1dSRodney W. Grimes } 849df8bae1dSRodney W. Grimes } 8501e4ad9ceSGarrett Wollman 85182c23ebaSBill Fenner /* 85282c23ebaSBill Fenner * Create a "control" mbuf containing the specified data 85382c23ebaSBill Fenner * with the specified type for presentation on a socket buffer. 85482c23ebaSBill Fenner */ 85582c23ebaSBill Fenner struct mbuf * 85682c23ebaSBill Fenner sbcreatecontrol(p, size, type, level) 85782c23ebaSBill Fenner caddr_t p; 85882c23ebaSBill Fenner register int size; 85982c23ebaSBill Fenner int type, level; 86082c23ebaSBill Fenner { 86182c23ebaSBill Fenner register struct cmsghdr *cp; 86282c23ebaSBill Fenner struct mbuf *m; 86382c23ebaSBill Fenner 86459bdd405SDavid Malone if (CMSG_SPACE((u_int)size) > MCLBYTES) 8650b97e97cSYoshinobu Inoue return ((struct mbuf *) NULL); 86682c23ebaSBill Fenner if ((m = m_get(M_DONTWAIT, MT_CONTROL)) == NULL) 86782c23ebaSBill Fenner return ((struct mbuf *) NULL); 86859bdd405SDavid Malone if (CMSG_SPACE((u_int)size) > MLEN) { 86959bdd405SDavid Malone MCLGET(m, M_DONTWAIT); 87059bdd405SDavid Malone if ((m->m_flags & M_EXT) == 0) { 87159bdd405SDavid Malone m_free(m); 87259bdd405SDavid Malone return ((struct mbuf *) NULL); 87359bdd405SDavid Malone } 87459bdd405SDavid Malone } 87582c23ebaSBill Fenner cp = mtod(m, struct cmsghdr *); 87659bdd405SDavid Malone m->m_len = 0; 87759bdd405SDavid Malone KASSERT(CMSG_SPACE((u_int)size) <= M_TRAILINGSPACE(m), 87859bdd405SDavid Malone ("sbcreatecontrol: short mbuf")); 87959bdd405SDavid Malone if (p != NULL) 88082c23ebaSBill Fenner (void)memcpy(CMSG_DATA(cp), p, size); 8817d0d8dc3SYoshinobu Inoue m->m_len = CMSG_SPACE(size); 8827d0d8dc3SYoshinobu Inoue cp->cmsg_len = CMSG_LEN(size); 88382c23ebaSBill Fenner cp->cmsg_level = level; 88482c23ebaSBill Fenner cp->cmsg_type = type; 88582c23ebaSBill Fenner return (m); 88682c23ebaSBill Fenner } 88782c23ebaSBill Fenner 8881e4ad9ceSGarrett Wollman /* 8892c37256eSGarrett Wollman * Some routines that return EOPNOTSUPP for entry points that are not 8902c37256eSGarrett Wollman * supported by a protocol. Fill in as needed. 8911e4ad9ceSGarrett Wollman */ 8921e4ad9ceSGarrett Wollman int 89357bf258eSGarrett Wollman pru_accept_notsupp(struct socket *so, struct sockaddr **nam) 894d8392c6cSGarrett Wollman { 895d8392c6cSGarrett Wollman return EOPNOTSUPP; 896d8392c6cSGarrett Wollman } 897d8392c6cSGarrett Wollman 898d8392c6cSGarrett Wollman int 899b40ce416SJulian Elischer pru_connect_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td) 9009f907986SPeter Wemm { 9019f907986SPeter Wemm return EOPNOTSUPP; 9029f907986SPeter Wemm } 9039f907986SPeter Wemm 9049f907986SPeter Wemm int 9052c37256eSGarrett Wollman pru_connect2_notsupp(struct socket *so1, struct socket *so2) 9061e4ad9ceSGarrett Wollman { 9072c37256eSGarrett Wollman return EOPNOTSUPP; 9081e4ad9ceSGarrett Wollman } 909d8392c6cSGarrett Wollman 910d8392c6cSGarrett Wollman int 911ecbb00a2SDoug Rabson pru_control_notsupp(struct socket *so, u_long cmd, caddr_t data, 912b40ce416SJulian Elischer struct ifnet *ifp, struct thread *td) 913a29f300eSGarrett Wollman { 914a29f300eSGarrett Wollman return EOPNOTSUPP; 915a29f300eSGarrett Wollman } 916a29f300eSGarrett Wollman 917a29f300eSGarrett Wollman int 918b40ce416SJulian Elischer pru_listen_notsupp(struct socket *so, struct thread *td) 919d8392c6cSGarrett Wollman { 920d8392c6cSGarrett Wollman return EOPNOTSUPP; 921d8392c6cSGarrett Wollman } 922d8392c6cSGarrett Wollman 923d8392c6cSGarrett Wollman int 924d8392c6cSGarrett Wollman pru_rcvd_notsupp(struct socket *so, int flags) 925d8392c6cSGarrett Wollman { 926d8392c6cSGarrett Wollman return EOPNOTSUPP; 927d8392c6cSGarrett Wollman } 928d8392c6cSGarrett Wollman 929d8392c6cSGarrett Wollman int 930d8392c6cSGarrett Wollman pru_rcvoob_notsupp(struct socket *so, struct mbuf *m, int flags) 931d8392c6cSGarrett Wollman { 932d8392c6cSGarrett Wollman return EOPNOTSUPP; 933d8392c6cSGarrett Wollman } 934d8392c6cSGarrett Wollman 935d8392c6cSGarrett Wollman /* 936d8392c6cSGarrett Wollman * This isn't really a ``null'' operation, but it's the default one 937d8392c6cSGarrett Wollman * and doesn't do anything destructive. 938d8392c6cSGarrett Wollman */ 939d8392c6cSGarrett Wollman int 940d8392c6cSGarrett Wollman pru_sense_null(struct socket *so, struct stat *sb) 941d8392c6cSGarrett Wollman { 942d8392c6cSGarrett Wollman sb->st_blksize = so->so_snd.sb_hiwat; 943d8392c6cSGarrett Wollman return 0; 944d8392c6cSGarrett Wollman } 945639acc13SGarrett Wollman 946639acc13SGarrett Wollman /* 94757bf258eSGarrett Wollman * Make a copy of a sockaddr in a malloced buffer of type M_SONAME. 94857bf258eSGarrett Wollman */ 94957bf258eSGarrett Wollman struct sockaddr * 95057bf258eSGarrett Wollman dup_sockaddr(sa, canwait) 95157bf258eSGarrett Wollman struct sockaddr *sa; 95257bf258eSGarrett Wollman int canwait; 95357bf258eSGarrett Wollman { 95457bf258eSGarrett Wollman struct sockaddr *sa2; 95557bf258eSGarrett Wollman 95657bf258eSGarrett Wollman MALLOC(sa2, struct sockaddr *, sa->sa_len, M_SONAME, 95757bf258eSGarrett Wollman canwait ? M_WAITOK : M_NOWAIT); 95857bf258eSGarrett Wollman if (sa2) 95957bf258eSGarrett Wollman bcopy(sa, sa2, sa->sa_len); 96057bf258eSGarrett Wollman return sa2; 96157bf258eSGarrett Wollman } 96257bf258eSGarrett Wollman 96357bf258eSGarrett Wollman /* 96498271db4SGarrett Wollman * Create an external-format (``xsocket'') structure using the information 96598271db4SGarrett Wollman * in the kernel-format socket structure pointed to by so. This is done 96698271db4SGarrett Wollman * to reduce the spew of irrelevant information over this interface, 96798271db4SGarrett Wollman * to isolate user code from changes in the kernel structure, and 96898271db4SGarrett Wollman * potentially to provide information-hiding if we decide that 96998271db4SGarrett Wollman * some of this information should be hidden from users. 97098271db4SGarrett Wollman */ 97198271db4SGarrett Wollman void 97298271db4SGarrett Wollman sotoxsocket(struct socket *so, struct xsocket *xso) 97398271db4SGarrett Wollman { 97498271db4SGarrett Wollman xso->xso_len = sizeof *xso; 97598271db4SGarrett Wollman xso->xso_so = so; 97698271db4SGarrett Wollman xso->so_type = so->so_type; 97798271db4SGarrett Wollman xso->so_options = so->so_options; 97898271db4SGarrett Wollman xso->so_linger = so->so_linger; 97998271db4SGarrett Wollman xso->so_state = so->so_state; 98098271db4SGarrett Wollman xso->so_pcb = so->so_pcb; 98198271db4SGarrett Wollman xso->xso_protocol = so->so_proto->pr_protocol; 98298271db4SGarrett Wollman xso->xso_family = so->so_proto->pr_domain->dom_family; 98398271db4SGarrett Wollman xso->so_qlen = so->so_qlen; 98498271db4SGarrett Wollman xso->so_incqlen = so->so_incqlen; 98598271db4SGarrett Wollman xso->so_qlimit = so->so_qlimit; 98698271db4SGarrett Wollman xso->so_timeo = so->so_timeo; 98798271db4SGarrett Wollman xso->so_error = so->so_error; 988831d27a9SDon Lewis xso->so_pgid = so->so_sigio ? so->so_sigio->sio_pgid : 0; 98998271db4SGarrett Wollman xso->so_oobmark = so->so_oobmark; 99098271db4SGarrett Wollman sbtoxsockbuf(&so->so_snd, &xso->so_snd); 99198271db4SGarrett Wollman sbtoxsockbuf(&so->so_rcv, &xso->so_rcv); 9922f9a2132SBrian Feldman xso->so_uid = so->so_cred->cr_uid; 99398271db4SGarrett Wollman } 99498271db4SGarrett Wollman 99598271db4SGarrett Wollman /* 99698271db4SGarrett Wollman * This does the same for sockbufs. Note that the xsockbuf structure, 99798271db4SGarrett Wollman * since it is always embedded in a socket, does not include a self 99898271db4SGarrett Wollman * pointer nor a length. We make this entry point public in case 99998271db4SGarrett Wollman * some other mechanism needs it. 100098271db4SGarrett Wollman */ 100198271db4SGarrett Wollman void 100298271db4SGarrett Wollman sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb) 100398271db4SGarrett Wollman { 100498271db4SGarrett Wollman xsb->sb_cc = sb->sb_cc; 100598271db4SGarrett Wollman xsb->sb_hiwat = sb->sb_hiwat; 100698271db4SGarrett Wollman xsb->sb_mbcnt = sb->sb_mbcnt; 100798271db4SGarrett Wollman xsb->sb_mbmax = sb->sb_mbmax; 100898271db4SGarrett Wollman xsb->sb_lowat = sb->sb_lowat; 100998271db4SGarrett Wollman xsb->sb_flags = sb->sb_flags; 101098271db4SGarrett Wollman xsb->sb_timeo = sb->sb_timeo; 101198271db4SGarrett Wollman } 101298271db4SGarrett Wollman 101398271db4SGarrett Wollman /* 1014639acc13SGarrett Wollman * Here is the definition of some of the basic objects in the kern.ipc 1015639acc13SGarrett Wollman * branch of the MIB. 1016639acc13SGarrett Wollman */ 1017639acc13SGarrett Wollman SYSCTL_NODE(_kern, KERN_IPC, ipc, CTLFLAG_RW, 0, "IPC"); 1018639acc13SGarrett Wollman 1019639acc13SGarrett Wollman /* This takes the place of kern.maxsockbuf, which moved to kern.ipc. */ 1020639acc13SGarrett Wollman static int dummy; 1021639acc13SGarrett Wollman SYSCTL_INT(_kern, KERN_DUMMY, dummy, CTLFLAG_RW, &dummy, 0, ""); 1022639acc13SGarrett Wollman 10233d177f46SBill Fumerola SYSCTL_INT(_kern_ipc, KIPC_MAXSOCKBUF, maxsockbuf, CTLFLAG_RW, 10243d177f46SBill Fumerola &sb_max, 0, "Maximum socket buffer size"); 102554d77689SJeff Roberson SYSCTL_INT(_kern_ipc, OID_AUTO, maxsockets, CTLFLAG_RD, 102654d77689SJeff Roberson &maxsockets, 0, "Maximum number of sockets avaliable"); 1027639acc13SGarrett Wollman SYSCTL_INT(_kern_ipc, KIPC_SOCKBUF_WASTE, sockbuf_waste_factor, CTLFLAG_RW, 1028639acc13SGarrett Wollman &sb_efficiency, 0, ""); 102954d77689SJeff Roberson 103054d77689SJeff Roberson /* 103154d77689SJeff Roberson * Initialise maxsockets 103254d77689SJeff Roberson */ 103354d77689SJeff Roberson static void init_maxsockets(void *ignored) 103454d77689SJeff Roberson { 103554d77689SJeff Roberson TUNABLE_INT_FETCH("kern.ipc.maxsockets", &maxsockets); 103654d77689SJeff Roberson maxsockets = imax(maxsockets, imax(maxfiles, nmbclusters)); 103754d77689SJeff Roberson } 103854d77689SJeff Roberson SYSINIT(param, SI_SUB_TUNABLES, SI_ORDER_ANY, init_maxsockets, NULL); 1039