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 37335654d7SRobert Watson #include "opt_mac.h" 385b86eac4SJesper Skriver #include "opt_param.h" 39335654d7SRobert Watson 40df8bae1dSRodney W. Grimes #include <sys/param.h> 41960ed29cSSeigo Tanimura #include <sys/aio.h> /* for aio_swake proto */ 4298271db4SGarrett Wollman #include <sys/domain.h> 43960ed29cSSeigo Tanimura #include <sys/event.h> 44134c934cSMike Smith #include <sys/file.h> /* for maxfiles */ 45ff5c09daSGarrett Wollman #include <sys/kernel.h> 46fb919e4dSMark Murray #include <sys/lock.h> 47335654d7SRobert Watson #include <sys/mac.h> 48f9d0d524SRobert Watson #include <sys/malloc.h> 49df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 50960ed29cSSeigo Tanimura #include <sys/mutex.h> 51fb919e4dSMark Murray #include <sys/proc.h> 52df8bae1dSRodney W. Grimes #include <sys/protosw.h> 532f9a2132SBrian Feldman #include <sys/resourcevar.h> 54960ed29cSSeigo Tanimura #include <sys/signalvar.h> 55df8bae1dSRodney W. Grimes #include <sys/socket.h> 56df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 57960ed29cSSeigo Tanimura #include <sys/stat.h> 58ff5c09daSGarrett Wollman #include <sys/sysctl.h> 59960ed29cSSeigo Tanimura #include <sys/systm.h> 6026f9a767SRodney W. Grimes 6154d77689SJeff Roberson int maxsockets; 6254d77689SJeff Roberson 6321d56e9cSAlfred Perlstein void (*aio_swake)(struct socket *, struct sockbuf *); 6421d56e9cSAlfred Perlstein 65df8bae1dSRodney W. Grimes /* 66df8bae1dSRodney W. Grimes * Primitive routines for operating on sockets and socket buffers 67df8bae1dSRodney W. Grimes */ 68df8bae1dSRodney W. Grimes 6979cb7eb4SDavid Greenman u_long sb_max = SB_MAX; 7079cb7eb4SDavid Greenman u_long sb_max_adj = 7179cb7eb4SDavid Greenman SB_MAX * MCLBYTES / (MSIZE + MCLBYTES); /* adjusted sb_max */ 72df8bae1dSRodney W. Grimes 734b29bc4fSGarrett Wollman static u_long sb_efficiency = 8; /* parameter for sbreserve() */ 744b29bc4fSGarrett Wollman 75df8bae1dSRodney W. Grimes /* 76df8bae1dSRodney W. Grimes * Procedures to manipulate state flags of socket 77df8bae1dSRodney W. Grimes * and do appropriate wakeups. Normal sequence from the 78df8bae1dSRodney W. Grimes * active (originating) side is that soisconnecting() is 79df8bae1dSRodney W. Grimes * called during processing of connect() call, 80df8bae1dSRodney W. Grimes * resulting in an eventual call to soisconnected() if/when the 81df8bae1dSRodney W. Grimes * connection is established. When the connection is torn down 82df8bae1dSRodney W. Grimes * soisdisconnecting() is called during processing of disconnect() call, 83df8bae1dSRodney W. Grimes * and soisdisconnected() is called when the connection to the peer 84df8bae1dSRodney W. Grimes * is totally severed. The semantics of these routines are such that 85df8bae1dSRodney W. Grimes * connectionless protocols can call soisconnected() and soisdisconnected() 86df8bae1dSRodney W. Grimes * only, bypassing the in-progress calls when setting up a ``connection'' 87df8bae1dSRodney W. Grimes * takes no time. 88df8bae1dSRodney W. Grimes * 89df8bae1dSRodney W. Grimes * From the passive side, a socket is created with 90dc97381eSPeter Wemm * two queues of sockets: so_incomp for connections in progress 91dc97381eSPeter Wemm * and so_comp for connections already made and awaiting user acceptance. 92df8bae1dSRodney W. Grimes * As a protocol is preparing incoming connections, it creates a socket 93dc97381eSPeter Wemm * structure queued on so_incomp by calling sonewconn(). When the connection 94df8bae1dSRodney W. Grimes * is established, soisconnected() is called, and transfers the 95dc97381eSPeter Wemm * socket structure to so_comp, making it available to accept(). 96df8bae1dSRodney W. Grimes * 97df8bae1dSRodney W. Grimes * If a socket is closed with sockets on either 98dc97381eSPeter Wemm * so_incomp or so_comp, these sockets are dropped. 99df8bae1dSRodney W. Grimes * 100df8bae1dSRodney W. Grimes * If higher level protocols are implemented in 101df8bae1dSRodney W. Grimes * the kernel, the wakeups done here will sometimes 102df8bae1dSRodney W. Grimes * cause software-interrupt process scheduling. 103df8bae1dSRodney W. Grimes */ 104df8bae1dSRodney W. Grimes 10526f9a767SRodney W. Grimes void 106df8bae1dSRodney W. Grimes soisconnecting(so) 107df8bae1dSRodney W. Grimes register struct socket *so; 108df8bae1dSRodney W. Grimes { 109df8bae1dSRodney W. Grimes 110df8bae1dSRodney W. Grimes so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING); 111df8bae1dSRodney W. Grimes so->so_state |= SS_ISCONNECTING; 112df8bae1dSRodney W. Grimes } 113df8bae1dSRodney W. Grimes 11426f9a767SRodney W. Grimes void 115d48d4b25SSeigo Tanimura soisconnected(so) 116d48d4b25SSeigo Tanimura struct socket *so; 117d48d4b25SSeigo Tanimura { 1184cc20ab1SSeigo Tanimura struct socket *head = so->so_head; 119d48d4b25SSeigo Tanimura 120d48d4b25SSeigo Tanimura so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING); 121d48d4b25SSeigo Tanimura so->so_state |= SS_ISCONNECTED; 122d48d4b25SSeigo Tanimura if (head && (so->so_state & SS_INCOMP)) { 123d48d4b25SSeigo Tanimura if ((so->so_options & SO_ACCEPTFILTER) != 0) { 1244cc20ab1SSeigo Tanimura so->so_upcall = head->so_accf->so_accept_filter->accf_callback; 1254cc20ab1SSeigo Tanimura so->so_upcallarg = head->so_accf->so_accept_filter_arg; 126d48d4b25SSeigo Tanimura so->so_rcv.sb_flags |= SB_UPCALL; 127d48d4b25SSeigo Tanimura so->so_options &= ~SO_ACCEPTFILTER; 1284cc20ab1SSeigo Tanimura so->so_upcall(so, so->so_upcallarg, 0); 129a79b7128SAlfred Perlstein return; 130a79b7128SAlfred Perlstein } 131be24e9e8SDavid Greenman TAILQ_REMOVE(&head->so_incomp, so, so_list); 132a51764a8SPaul Traina head->so_incqlen--; 1334cc20ab1SSeigo Tanimura so->so_state &= ~SS_INCOMP; 134be24e9e8SDavid Greenman TAILQ_INSERT_TAIL(&head->so_comp, so, so_list); 135e1f1827fSMike Silbersack head->so_qlen++; 1364cc20ab1SSeigo Tanimura so->so_state |= SS_COMP; 13703e49181SSeigo Tanimura sorwakeup(head); 138a91b8721SDavid Greenman wakeup_one(&head->so_timeo); 139df8bae1dSRodney W. Grimes } else { 140a91b8721SDavid Greenman wakeup(&so->so_timeo); 14103e49181SSeigo Tanimura sorwakeup(so); 14203e49181SSeigo Tanimura sowwakeup(so); 143df8bae1dSRodney W. Grimes } 144df8bae1dSRodney W. Grimes } 145df8bae1dSRodney W. Grimes 14626f9a767SRodney W. Grimes void 147df8bae1dSRodney W. Grimes soisdisconnecting(so) 148df8bae1dSRodney W. Grimes register struct socket *so; 149df8bae1dSRodney W. Grimes { 150df8bae1dSRodney W. Grimes 151df8bae1dSRodney W. Grimes so->so_state &= ~SS_ISCONNECTING; 152df8bae1dSRodney W. Grimes so->so_state |= (SS_ISDISCONNECTING|SS_CANTRCVMORE|SS_CANTSENDMORE); 15380208239SAlfred Perlstein wakeup(&so->so_timeo); 15403e49181SSeigo Tanimura sowwakeup(so); 15503e49181SSeigo Tanimura sorwakeup(so); 156df8bae1dSRodney W. Grimes } 157df8bae1dSRodney W. Grimes 15826f9a767SRodney W. Grimes void 159df8bae1dSRodney W. Grimes soisdisconnected(so) 160df8bae1dSRodney W. Grimes register struct socket *so; 161df8bae1dSRodney W. Grimes { 162df8bae1dSRodney W. Grimes 16303e49181SSeigo Tanimura so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING); 16403e49181SSeigo Tanimura so->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE|SS_ISDISCONNECTED); 16580208239SAlfred Perlstein wakeup(&so->so_timeo); 16625dec747SDavid Malone sbdrop(&so->so_snd, so->so_snd.sb_cc); 16703e49181SSeigo Tanimura sowwakeup(so); 16803e49181SSeigo Tanimura sorwakeup(so); 169df8bae1dSRodney W. Grimes } 170df8bae1dSRodney W. Grimes 171df8bae1dSRodney W. Grimes /* 172df8bae1dSRodney W. Grimes * When an attempt at a new connection is noted on a socket 173df8bae1dSRodney W. Grimes * which accepts connections, sonewconn is called. If the 174df8bae1dSRodney W. Grimes * connection is possible (subject to space constraints, etc.) 175df8bae1dSRodney W. Grimes * then we allocate a new structure, propoerly linked into the 176df8bae1dSRodney W. Grimes * data structure of the original socket, and return this. 177df8bae1dSRodney W. Grimes * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED. 178b1e4abd2SMatthew Dillon * 179b1e4abd2SMatthew Dillon * note: the ref count on the socket is 0 on return 180df8bae1dSRodney W. Grimes */ 181df8bae1dSRodney W. Grimes struct socket * 182548af278SBill Fenner sonewconn(head, connstatus) 183df8bae1dSRodney W. Grimes register struct socket *head; 184df8bae1dSRodney W. Grimes int connstatus; 185df8bae1dSRodney W. Grimes { 186df8bae1dSRodney W. Grimes register struct socket *so; 187df8bae1dSRodney W. Grimes 188ebb0cbeaSPaul Traina if (head->so_qlen > 3 * head->so_qlimit / 2) 189df8bae1dSRodney W. Grimes return ((struct socket *)0); 19098271db4SGarrett Wollman so = soalloc(0); 191df8bae1dSRodney W. Grimes if (so == NULL) 192df8bae1dSRodney W. Grimes return ((struct socket *)0); 193205b2b61SPeter Wemm if ((head->so_options & SO_ACCEPTFILTER) != 0) 194205b2b61SPeter Wemm connstatus = 0; 195be24e9e8SDavid Greenman so->so_head = head; 196df8bae1dSRodney W. Grimes so->so_type = head->so_type; 197df8bae1dSRodney W. Grimes so->so_options = head->so_options &~ SO_ACCEPTCONN; 198df8bae1dSRodney W. Grimes so->so_linger = head->so_linger; 199df8bae1dSRodney W. Grimes so->so_state = head->so_state | SS_NOFDREF; 200df8bae1dSRodney W. Grimes so->so_proto = head->so_proto; 201df8bae1dSRodney W. Grimes so->so_timeo = head->so_timeo; 202bd78ceceSJohn Baldwin so->so_cred = crhold(head->so_cred); 203335654d7SRobert Watson #ifdef MAC 204335654d7SRobert Watson mac_create_socket_from_socket(head, so); 205335654d7SRobert Watson #endif 2062f9a2132SBrian Feldman if (soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat) || 2072f9a2132SBrian Feldman (*so->so_proto->pr_usrreqs->pru_attach)(so, 0, NULL)) { 2085ee0a409SAlan Cox sodealloc(so); 209b1396a35SGarrett Wollman return ((struct socket *)0); 210b1396a35SGarrett Wollman } 211b1396a35SGarrett Wollman 212be24e9e8SDavid Greenman if (connstatus) { 213be24e9e8SDavid Greenman TAILQ_INSERT_TAIL(&head->so_comp, so, so_list); 214be24e9e8SDavid Greenman so->so_state |= SS_COMP; 215e1f1827fSMike Silbersack head->so_qlen++; 216be24e9e8SDavid Greenman } else { 217184fec1aSMike Silbersack if (head->so_incqlen > head->so_qlimit) { 218e1f1827fSMike Silbersack struct socket *sp; 219e1f1827fSMike Silbersack sp = TAILQ_FIRST(&head->so_incomp); 220e1f1827fSMike Silbersack (void) soabort(sp); 221e1f1827fSMike Silbersack } 222be24e9e8SDavid Greenman TAILQ_INSERT_TAIL(&head->so_incomp, so, so_list); 223be24e9e8SDavid Greenman so->so_state |= SS_INCOMP; 224ebb0cbeaSPaul Traina head->so_incqlen++; 225be24e9e8SDavid Greenman } 226df8bae1dSRodney W. Grimes if (connstatus) { 22703e49181SSeigo Tanimura sorwakeup(head); 22880208239SAlfred Perlstein wakeup(&head->so_timeo); 229acbbcc5fSSeigo Tanimura so->so_state |= connstatus; 230df8bae1dSRodney W. Grimes } 231df8bae1dSRodney W. Grimes return (so); 232df8bae1dSRodney W. Grimes } 233df8bae1dSRodney W. Grimes 234df8bae1dSRodney W. Grimes /* 235df8bae1dSRodney W. Grimes * Socantsendmore indicates that no more data will be sent on the 236df8bae1dSRodney W. Grimes * socket; it would normally be applied to a socket when the user 237df8bae1dSRodney W. Grimes * informs the system that no more data is to be sent, by the protocol 238df8bae1dSRodney W. Grimes * code (in case PRU_SHUTDOWN). Socantrcvmore indicates that no more data 239df8bae1dSRodney W. Grimes * will be received, and will normally be applied to the socket by a 240df8bae1dSRodney W. Grimes * protocol when it detects that the peer will send no more data. 241df8bae1dSRodney W. Grimes * Data queued for reading in the socket may yet be read. 242df8bae1dSRodney W. Grimes */ 243df8bae1dSRodney W. Grimes 24426f9a767SRodney W. Grimes void 245df8bae1dSRodney W. Grimes socantsendmore(so) 246df8bae1dSRodney W. Grimes struct socket *so; 247df8bae1dSRodney W. Grimes { 248df8bae1dSRodney W. Grimes 249df8bae1dSRodney W. Grimes so->so_state |= SS_CANTSENDMORE; 25003e49181SSeigo Tanimura sowwakeup(so); 251df8bae1dSRodney W. Grimes } 252df8bae1dSRodney W. Grimes 25326f9a767SRodney W. Grimes void 254df8bae1dSRodney W. Grimes socantrcvmore(so) 255df8bae1dSRodney W. Grimes struct socket *so; 256df8bae1dSRodney W. Grimes { 257df8bae1dSRodney W. Grimes 258df8bae1dSRodney W. Grimes so->so_state |= SS_CANTRCVMORE; 25903e49181SSeigo Tanimura sorwakeup(so); 260df8bae1dSRodney W. Grimes } 261df8bae1dSRodney W. Grimes 262df8bae1dSRodney W. Grimes /* 263df8bae1dSRodney W. Grimes * Wait for data to arrive at/drain from a socket buffer. 264df8bae1dSRodney W. Grimes */ 26526f9a767SRodney W. Grimes int 266df8bae1dSRodney W. Grimes sbwait(sb) 267df8bae1dSRodney W. Grimes struct sockbuf *sb; 268df8bae1dSRodney W. Grimes { 269df8bae1dSRodney W. Grimes 270df8bae1dSRodney W. Grimes sb->sb_flags |= SB_WAIT; 27180208239SAlfred Perlstein return (tsleep(&sb->sb_cc, 27247daf5d5SBruce Evans (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH, "sbwait", 273df8bae1dSRodney W. Grimes sb->sb_timeo)); 274df8bae1dSRodney W. Grimes } 275df8bae1dSRodney W. Grimes 276df8bae1dSRodney W. Grimes /* 277df8bae1dSRodney W. Grimes * Lock a sockbuf already known to be locked; 278df8bae1dSRodney W. Grimes * return any error returned from sleep (EINTR). 279df8bae1dSRodney W. Grimes */ 28026f9a767SRodney W. Grimes int 281df8bae1dSRodney W. Grimes sb_lock(sb) 282df8bae1dSRodney W. Grimes register struct sockbuf *sb; 283df8bae1dSRodney W. Grimes { 284df8bae1dSRodney W. Grimes int error; 285df8bae1dSRodney W. Grimes 286df8bae1dSRodney W. Grimes while (sb->sb_flags & SB_LOCK) { 287df8bae1dSRodney W. Grimes sb->sb_flags |= SB_WANT; 28880208239SAlfred Perlstein error = tsleep(&sb->sb_flags, 289df8bae1dSRodney W. Grimes (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK|PCATCH, 29047daf5d5SBruce Evans "sblock", 0); 291797f2d22SPoul-Henning Kamp if (error) 292df8bae1dSRodney W. Grimes return (error); 293df8bae1dSRodney W. Grimes } 294df8bae1dSRodney W. Grimes sb->sb_flags |= SB_LOCK; 295df8bae1dSRodney W. Grimes return (0); 296df8bae1dSRodney W. Grimes } 297df8bae1dSRodney W. Grimes 298df8bae1dSRodney W. Grimes /* 299df8bae1dSRodney W. Grimes * Wakeup processes waiting on a socket buffer. 300df8bae1dSRodney W. Grimes * Do asynchronous notification via SIGIO 301df8bae1dSRodney W. Grimes * if the socket has the SS_ASYNC flag set. 302df8bae1dSRodney W. Grimes */ 30326f9a767SRodney W. Grimes void 304df8bae1dSRodney W. Grimes sowakeup(so, sb) 305df8bae1dSRodney W. Grimes register struct socket *so; 306df8bae1dSRodney W. Grimes register struct sockbuf *sb; 307df8bae1dSRodney W. Grimes { 308d48d4b25SSeigo Tanimura 309df8bae1dSRodney W. Grimes selwakeup(&sb->sb_sel); 310df8bae1dSRodney W. Grimes sb->sb_flags &= ~SB_SEL; 311df8bae1dSRodney W. Grimes if (sb->sb_flags & SB_WAIT) { 312df8bae1dSRodney W. Grimes sb->sb_flags &= ~SB_WAIT; 31380208239SAlfred Perlstein wakeup(&sb->sb_cc); 314df8bae1dSRodney W. Grimes } 3154cc20ab1SSeigo Tanimura if ((so->so_state & SS_ASYNC) && so->so_sigio != NULL) 316f1320723SAlfred Perlstein pgsigio(&so->so_sigio, SIGIO, 0); 3174cc20ab1SSeigo Tanimura if (sb->sb_flags & SB_UPCALL) 3184dc75870SPeter Wemm (*so->so_upcall)(so, so->so_upcallarg, M_DONTWAIT); 3194cc20ab1SSeigo Tanimura if (sb->sb_flags & SB_AIO) 320bfbbc4aaSJason Evans aio_swake(so, sb); 321cb679c38SJonathan Lemon KNOTE(&sb->sb_sel.si_note, 0); 322df8bae1dSRodney W. Grimes } 323df8bae1dSRodney W. Grimes 324df8bae1dSRodney W. Grimes /* 325df8bae1dSRodney W. Grimes * Socket buffer (struct sockbuf) utility routines. 326df8bae1dSRodney W. Grimes * 327df8bae1dSRodney W. Grimes * Each socket contains two socket buffers: one for sending data and 328df8bae1dSRodney W. Grimes * one for receiving data. Each buffer contains a queue of mbufs, 329df8bae1dSRodney W. Grimes * information about the number of mbufs and amount of data in the 330df8bae1dSRodney W. Grimes * queue, and other fields allowing select() statements and notification 331df8bae1dSRodney W. Grimes * on data availability to be implemented. 332df8bae1dSRodney W. Grimes * 333df8bae1dSRodney W. Grimes * Data stored in a socket buffer is maintained as a list of records. 334df8bae1dSRodney W. Grimes * Each record is a list of mbufs chained together with the m_next 335df8bae1dSRodney W. Grimes * field. Records are chained together with the m_nextpkt field. The upper 336df8bae1dSRodney W. Grimes * level routine soreceive() expects the following conventions to be 337df8bae1dSRodney W. Grimes * observed when placing information in the receive buffer: 338df8bae1dSRodney W. Grimes * 339df8bae1dSRodney W. Grimes * 1. If the protocol requires each message be preceded by the sender's 340df8bae1dSRodney W. Grimes * name, then a record containing that name must be present before 341df8bae1dSRodney W. Grimes * any associated data (mbuf's must be of type MT_SONAME). 342df8bae1dSRodney W. Grimes * 2. If the protocol supports the exchange of ``access rights'' (really 343df8bae1dSRodney W. Grimes * just additional data associated with the message), and there are 344df8bae1dSRodney W. Grimes * ``rights'' to be received, then a record containing this data 345df8bae1dSRodney W. Grimes * should be present (mbuf's must be of type MT_RIGHTS). 346df8bae1dSRodney W. Grimes * 3. If a name or rights record exists, then it must be followed by 347df8bae1dSRodney W. Grimes * a data record, perhaps of zero length. 348df8bae1dSRodney W. Grimes * 349df8bae1dSRodney W. Grimes * Before using a new socket structure it is first necessary to reserve 350df8bae1dSRodney W. Grimes * buffer space to the socket, by calling sbreserve(). This should commit 351df8bae1dSRodney W. Grimes * some of the available buffer space in the system buffer pool for the 352df8bae1dSRodney W. Grimes * socket (currently, it does nothing but enforce limits). The space 353df8bae1dSRodney W. Grimes * should be released by calling sbrelease() when the socket is destroyed. 354df8bae1dSRodney W. Grimes */ 355df8bae1dSRodney W. Grimes 35626f9a767SRodney W. Grimes int 357df8bae1dSRodney W. Grimes soreserve(so, sndcc, rcvcc) 358df8bae1dSRodney W. Grimes register struct socket *so; 359df8bae1dSRodney W. Grimes u_long sndcc, rcvcc; 360df8bae1dSRodney W. Grimes { 361b40ce416SJulian Elischer struct thread *td = curthread; 362df8bae1dSRodney W. Grimes 363b40ce416SJulian Elischer if (sbreserve(&so->so_snd, sndcc, so, td) == 0) 364df8bae1dSRodney W. Grimes goto bad; 365b40ce416SJulian Elischer if (sbreserve(&so->so_rcv, rcvcc, so, td) == 0) 366df8bae1dSRodney W. Grimes goto bad2; 367df8bae1dSRodney W. Grimes if (so->so_rcv.sb_lowat == 0) 368df8bae1dSRodney W. Grimes so->so_rcv.sb_lowat = 1; 369df8bae1dSRodney W. Grimes if (so->so_snd.sb_lowat == 0) 370df8bae1dSRodney W. Grimes so->so_snd.sb_lowat = MCLBYTES; 371df8bae1dSRodney W. Grimes if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat) 372df8bae1dSRodney W. Grimes so->so_snd.sb_lowat = so->so_snd.sb_hiwat; 373df8bae1dSRodney W. Grimes return (0); 374df8bae1dSRodney W. Grimes bad2: 375ecf72308SBrian Feldman sbrelease(&so->so_snd, so); 376df8bae1dSRodney W. Grimes bad: 377df8bae1dSRodney W. Grimes return (ENOBUFS); 378df8bae1dSRodney W. Grimes } 379df8bae1dSRodney W. Grimes 38079cb7eb4SDavid Greenman static int 38179cb7eb4SDavid Greenman sysctl_handle_sb_max(SYSCTL_HANDLER_ARGS) 38279cb7eb4SDavid Greenman { 38379cb7eb4SDavid Greenman int error = 0; 38479cb7eb4SDavid Greenman u_long old_sb_max = sb_max; 38579cb7eb4SDavid Greenman 38679cb7eb4SDavid Greenman error = SYSCTL_OUT(req, arg1, sizeof(int)); 38779cb7eb4SDavid Greenman if (error || !req->newptr) 38879cb7eb4SDavid Greenman return (error); 38979cb7eb4SDavid Greenman error = SYSCTL_IN(req, arg1, sizeof(int)); 39079cb7eb4SDavid Greenman if (error) 39179cb7eb4SDavid Greenman return (error); 39279cb7eb4SDavid Greenman if (sb_max < MSIZE + MCLBYTES) { 39379cb7eb4SDavid Greenman sb_max = old_sb_max; 39479cb7eb4SDavid Greenman return (EINVAL); 39579cb7eb4SDavid Greenman } 39679cb7eb4SDavid Greenman sb_max_adj = (u_quad_t)sb_max * MCLBYTES / (MSIZE + MCLBYTES); 39779cb7eb4SDavid Greenman return (0); 39879cb7eb4SDavid Greenman } 39979cb7eb4SDavid Greenman 400df8bae1dSRodney W. Grimes /* 401df8bae1dSRodney W. Grimes * Allot mbufs to a sockbuf. 402df8bae1dSRodney W. Grimes * Attempt to scale mbmax so that mbcnt doesn't become limiting 403df8bae1dSRodney W. Grimes * if buffering efficiency is near the normal case. 404df8bae1dSRodney W. Grimes */ 40526f9a767SRodney W. Grimes int 406b40ce416SJulian Elischer sbreserve(sb, cc, so, td) 407df8bae1dSRodney W. Grimes struct sockbuf *sb; 408df8bae1dSRodney W. Grimes u_long cc; 409ecf72308SBrian Feldman struct socket *so; 410b40ce416SJulian Elischer struct thread *td; 411df8bae1dSRodney W. Grimes { 412ecf72308SBrian Feldman 413ecf72308SBrian Feldman /* 414b40ce416SJulian Elischer * td will only be NULL when we're in an interrupt 415ecf72308SBrian Feldman * (e.g. in tcp_input()) 416ecf72308SBrian Feldman */ 41779cb7eb4SDavid Greenman if (cc > sb_max_adj) 418df8bae1dSRodney W. Grimes return (0); 419f535380cSDon Lewis if (!chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, cc, 420b40ce416SJulian Elischer td ? td->td_proc->p_rlimit[RLIMIT_SBSIZE].rlim_cur : RLIM_INFINITY)) { 421ecf72308SBrian Feldman return (0); 422c6362551SAlfred Perlstein } 4234b29bc4fSGarrett Wollman sb->sb_mbmax = min(cc * sb_efficiency, sb_max); 424df8bae1dSRodney W. Grimes if (sb->sb_lowat > sb->sb_hiwat) 425df8bae1dSRodney W. Grimes sb->sb_lowat = sb->sb_hiwat; 426df8bae1dSRodney W. Grimes return (1); 427df8bae1dSRodney W. Grimes } 428df8bae1dSRodney W. Grimes 429df8bae1dSRodney W. Grimes /* 430df8bae1dSRodney W. Grimes * Free mbufs held by a socket, and reserved mbuf space. 431df8bae1dSRodney W. Grimes */ 43226f9a767SRodney W. Grimes void 433ecf72308SBrian Feldman sbrelease(sb, so) 434df8bae1dSRodney W. Grimes struct sockbuf *sb; 435ecf72308SBrian Feldman struct socket *so; 436df8bae1dSRodney W. Grimes { 437df8bae1dSRodney W. Grimes 438df8bae1dSRodney W. Grimes sbflush(sb); 439f535380cSDon Lewis (void)chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, 0, 440f535380cSDon Lewis RLIM_INFINITY); 4416aef685fSBrian Feldman sb->sb_mbmax = 0; 442df8bae1dSRodney W. Grimes } 443df8bae1dSRodney W. Grimes 444df8bae1dSRodney W. Grimes /* 445df8bae1dSRodney W. Grimes * Routines to add and remove 446df8bae1dSRodney W. Grimes * data from an mbuf queue. 447df8bae1dSRodney W. Grimes * 448df8bae1dSRodney W. Grimes * The routines sbappend() or sbappendrecord() are normally called to 449df8bae1dSRodney W. Grimes * append new mbufs to a socket buffer, after checking that adequate 450df8bae1dSRodney W. Grimes * space is available, comparing the function sbspace() with the amount 451df8bae1dSRodney W. Grimes * of data to be added. sbappendrecord() differs from sbappend() in 452df8bae1dSRodney W. Grimes * that data supplied is treated as the beginning of a new record. 453df8bae1dSRodney W. Grimes * To place a sender's address, optional access rights, and data in a 454df8bae1dSRodney W. Grimes * socket receive buffer, sbappendaddr() should be used. To place 455df8bae1dSRodney W. Grimes * access rights and data in a socket receive buffer, sbappendrights() 456df8bae1dSRodney W. Grimes * should be used. In either case, the new data begins a new record. 457df8bae1dSRodney W. Grimes * Note that unlike sbappend() and sbappendrecord(), these routines check 458df8bae1dSRodney W. Grimes * for the caller that there will be enough space to store the data. 459df8bae1dSRodney W. Grimes * Each fails if there is not enough space, or if it cannot find mbufs 460df8bae1dSRodney W. Grimes * to store additional information in. 461df8bae1dSRodney W. Grimes * 462df8bae1dSRodney W. Grimes * Reliable protocols may use the socket send buffer to hold data 463df8bae1dSRodney W. Grimes * awaiting acknowledgement. Data is normally copied from a socket 464df8bae1dSRodney W. Grimes * send buffer in a protocol with m_copy for output to a peer, 465df8bae1dSRodney W. Grimes * and then removing the data from the socket buffer with sbdrop() 466df8bae1dSRodney W. Grimes * or sbdroprecord() when the data is acknowledged by the peer. 467df8bae1dSRodney W. Grimes */ 468df8bae1dSRodney W. Grimes 469df8bae1dSRodney W. Grimes /* 470df8bae1dSRodney W. Grimes * Append mbuf chain m to the last record in the 471df8bae1dSRodney W. Grimes * socket buffer sb. The additional space associated 472df8bae1dSRodney W. Grimes * the mbuf chain is recorded in sb. Empty mbufs are 473df8bae1dSRodney W. Grimes * discarded and mbufs are compacted where possible. 474df8bae1dSRodney W. Grimes */ 47526f9a767SRodney W. Grimes void 476df8bae1dSRodney W. Grimes sbappend(sb, m) 477df8bae1dSRodney W. Grimes struct sockbuf *sb; 478df8bae1dSRodney W. Grimes struct mbuf *m; 479df8bae1dSRodney W. Grimes { 480df8bae1dSRodney W. Grimes register struct mbuf *n; 481df8bae1dSRodney W. Grimes 482df8bae1dSRodney W. Grimes if (m == 0) 483df8bae1dSRodney W. Grimes return; 484797f2d22SPoul-Henning Kamp n = sb->sb_mb; 485797f2d22SPoul-Henning Kamp if (n) { 486df8bae1dSRodney W. Grimes while (n->m_nextpkt) 487df8bae1dSRodney W. Grimes n = n->m_nextpkt; 488df8bae1dSRodney W. Grimes do { 489df8bae1dSRodney W. Grimes if (n->m_flags & M_EOR) { 490df8bae1dSRodney W. Grimes sbappendrecord(sb, m); /* XXXXXX!!!! */ 491df8bae1dSRodney W. Grimes return; 492df8bae1dSRodney W. Grimes } 493df8bae1dSRodney W. Grimes } while (n->m_next && (n = n->m_next)); 494df8bae1dSRodney W. Grimes } 495df8bae1dSRodney W. Grimes sbcompress(sb, m, n); 496df8bae1dSRodney W. Grimes } 497df8bae1dSRodney W. Grimes 498df8bae1dSRodney W. Grimes #ifdef SOCKBUF_DEBUG 49926f9a767SRodney W. Grimes void 500df8bae1dSRodney W. Grimes sbcheck(sb) 5017ed60de8SPoul-Henning Kamp struct sockbuf *sb; 502df8bae1dSRodney W. Grimes { 5037ed60de8SPoul-Henning Kamp struct mbuf *m; 5047ed60de8SPoul-Henning Kamp struct mbuf *n = 0; 5057ed60de8SPoul-Henning Kamp u_long len = 0, mbcnt = 0; 506df8bae1dSRodney W. Grimes 5070931333fSBill Fenner for (m = sb->sb_mb; m; m = n) { 5080931333fSBill Fenner n = m->m_nextpkt; 5090931333fSBill Fenner for (; m; m = m->m_next) { 510df8bae1dSRodney W. Grimes len += m->m_len; 511df8bae1dSRodney W. Grimes mbcnt += MSIZE; 512313861b8SJulian Elischer if (m->m_flags & M_EXT) /*XXX*/ /* pretty sure this is bogus */ 513df8bae1dSRodney W. Grimes mbcnt += m->m_ext.ext_size; 5140931333fSBill Fenner } 515df8bae1dSRodney W. Grimes } 516df8bae1dSRodney W. Grimes if (len != sb->sb_cc || mbcnt != sb->sb_mbcnt) { 5170931333fSBill Fenner printf("cc %ld != %ld || mbcnt %ld != %ld\n", len, sb->sb_cc, 518df8bae1dSRodney W. Grimes mbcnt, sb->sb_mbcnt); 519df8bae1dSRodney W. Grimes panic("sbcheck"); 520df8bae1dSRodney W. Grimes } 521df8bae1dSRodney W. Grimes } 522df8bae1dSRodney W. Grimes #endif 523df8bae1dSRodney W. Grimes 524df8bae1dSRodney W. Grimes /* 525df8bae1dSRodney W. Grimes * As above, except the mbuf chain 526df8bae1dSRodney W. Grimes * begins a new record. 527df8bae1dSRodney W. Grimes */ 52826f9a767SRodney W. Grimes void 529df8bae1dSRodney W. Grimes sbappendrecord(sb, m0) 530df8bae1dSRodney W. Grimes register struct sockbuf *sb; 531df8bae1dSRodney W. Grimes register struct mbuf *m0; 532df8bae1dSRodney W. Grimes { 533df8bae1dSRodney W. Grimes register struct mbuf *m; 534df8bae1dSRodney W. Grimes 535df8bae1dSRodney W. Grimes if (m0 == 0) 536df8bae1dSRodney W. Grimes return; 537797f2d22SPoul-Henning Kamp m = sb->sb_mb; 538797f2d22SPoul-Henning Kamp if (m) 539df8bae1dSRodney W. Grimes while (m->m_nextpkt) 540df8bae1dSRodney W. Grimes m = m->m_nextpkt; 541df8bae1dSRodney W. Grimes /* 542df8bae1dSRodney W. Grimes * Put the first mbuf on the queue. 543df8bae1dSRodney W. Grimes * Note this permits zero length records. 544df8bae1dSRodney W. Grimes */ 545df8bae1dSRodney W. Grimes sballoc(sb, m0); 546df8bae1dSRodney W. Grimes if (m) 547df8bae1dSRodney W. Grimes m->m_nextpkt = m0; 548df8bae1dSRodney W. Grimes else 549df8bae1dSRodney W. Grimes sb->sb_mb = m0; 550df8bae1dSRodney W. Grimes m = m0->m_next; 551df8bae1dSRodney W. Grimes m0->m_next = 0; 552df8bae1dSRodney W. Grimes if (m && (m0->m_flags & M_EOR)) { 553df8bae1dSRodney W. Grimes m0->m_flags &= ~M_EOR; 554df8bae1dSRodney W. Grimes m->m_flags |= M_EOR; 555df8bae1dSRodney W. Grimes } 556df8bae1dSRodney W. Grimes sbcompress(sb, m, m0); 557df8bae1dSRodney W. Grimes } 558df8bae1dSRodney W. Grimes 559df8bae1dSRodney W. Grimes /* 560df8bae1dSRodney W. Grimes * As above except that OOB data 561df8bae1dSRodney W. Grimes * is inserted at the beginning of the sockbuf, 562df8bae1dSRodney W. Grimes * but after any other OOB data. 563df8bae1dSRodney W. Grimes */ 56426f9a767SRodney W. Grimes void 565df8bae1dSRodney W. Grimes sbinsertoob(sb, m0) 566df8bae1dSRodney W. Grimes register struct sockbuf *sb; 567df8bae1dSRodney W. Grimes register struct mbuf *m0; 568df8bae1dSRodney W. Grimes { 569df8bae1dSRodney W. Grimes register struct mbuf *m; 570df8bae1dSRodney W. Grimes register struct mbuf **mp; 571df8bae1dSRodney W. Grimes 572df8bae1dSRodney W. Grimes if (m0 == 0) 573df8bae1dSRodney W. Grimes return; 574797f2d22SPoul-Henning Kamp for (mp = &sb->sb_mb; *mp ; mp = &((*mp)->m_nextpkt)) { 575797f2d22SPoul-Henning Kamp m = *mp; 576df8bae1dSRodney W. Grimes again: 577df8bae1dSRodney W. Grimes switch (m->m_type) { 578df8bae1dSRodney W. Grimes 579df8bae1dSRodney W. Grimes case MT_OOBDATA: 580df8bae1dSRodney W. Grimes continue; /* WANT next train */ 581df8bae1dSRodney W. Grimes 582df8bae1dSRodney W. Grimes case MT_CONTROL: 583797f2d22SPoul-Henning Kamp m = m->m_next; 584797f2d22SPoul-Henning Kamp if (m) 585df8bae1dSRodney W. Grimes goto again; /* inspect THIS train further */ 586df8bae1dSRodney W. Grimes } 587df8bae1dSRodney W. Grimes break; 588df8bae1dSRodney W. Grimes } 589df8bae1dSRodney W. Grimes /* 590df8bae1dSRodney W. Grimes * Put the first mbuf on the queue. 591df8bae1dSRodney W. Grimes * Note this permits zero length records. 592df8bae1dSRodney W. Grimes */ 593df8bae1dSRodney W. Grimes sballoc(sb, m0); 594df8bae1dSRodney W. Grimes m0->m_nextpkt = *mp; 595df8bae1dSRodney W. Grimes *mp = m0; 596df8bae1dSRodney W. Grimes m = m0->m_next; 597df8bae1dSRodney W. Grimes m0->m_next = 0; 598df8bae1dSRodney W. Grimes if (m && (m0->m_flags & M_EOR)) { 599df8bae1dSRodney W. Grimes m0->m_flags &= ~M_EOR; 600df8bae1dSRodney W. Grimes m->m_flags |= M_EOR; 601df8bae1dSRodney W. Grimes } 602df8bae1dSRodney W. Grimes sbcompress(sb, m, m0); 603df8bae1dSRodney W. Grimes } 604df8bae1dSRodney W. Grimes 605df8bae1dSRodney W. Grimes /* 606df8bae1dSRodney W. Grimes * Append address and data, and optionally, control (ancillary) data 607df8bae1dSRodney W. Grimes * to the receive queue of a socket. If present, 608df8bae1dSRodney W. Grimes * m0 must include a packet header with total length. 609df8bae1dSRodney W. Grimes * Returns 0 if no space in sockbuf or insufficient mbufs. 610df8bae1dSRodney W. Grimes */ 61126f9a767SRodney W. Grimes int 612df8bae1dSRodney W. Grimes sbappendaddr(sb, asa, m0, control) 6137ed60de8SPoul-Henning Kamp struct sockbuf *sb; 614df8bae1dSRodney W. Grimes struct sockaddr *asa; 615df8bae1dSRodney W. Grimes struct mbuf *m0, *control; 616df8bae1dSRodney W. Grimes { 6177ed60de8SPoul-Henning Kamp struct mbuf *m, *n; 618df8bae1dSRodney W. Grimes int space = asa->sa_len; 619df8bae1dSRodney W. Grimes 620df8bae1dSRodney W. Grimes if (m0 && (m0->m_flags & M_PKTHDR) == 0) 621df8bae1dSRodney W. Grimes panic("sbappendaddr"); 622df8bae1dSRodney W. Grimes if (m0) 623df8bae1dSRodney W. Grimes space += m0->m_pkthdr.len; 6247ed60de8SPoul-Henning Kamp space += m_length(control, &n); 625df8bae1dSRodney W. Grimes if (space > sbspace(sb)) 626df8bae1dSRodney W. Grimes return (0); 627df8bae1dSRodney W. Grimes if (asa->sa_len > MLEN) 628df8bae1dSRodney W. Grimes return (0); 629df8bae1dSRodney W. Grimes MGET(m, M_DONTWAIT, MT_SONAME); 630df8bae1dSRodney W. Grimes if (m == 0) 631df8bae1dSRodney W. Grimes return (0); 632df8bae1dSRodney W. Grimes m->m_len = asa->sa_len; 63380208239SAlfred Perlstein bcopy(asa, mtod(m, caddr_t), asa->sa_len); 634df8bae1dSRodney W. Grimes if (n) 635df8bae1dSRodney W. Grimes n->m_next = m0; /* concatenate data to control */ 636df8bae1dSRodney W. Grimes else 637df8bae1dSRodney W. Grimes control = m0; 638df8bae1dSRodney W. Grimes m->m_next = control; 639df8bae1dSRodney W. Grimes for (n = m; n; n = n->m_next) 640df8bae1dSRodney W. Grimes sballoc(sb, n); 641797f2d22SPoul-Henning Kamp n = sb->sb_mb; 642797f2d22SPoul-Henning Kamp if (n) { 643df8bae1dSRodney W. Grimes while (n->m_nextpkt) 644df8bae1dSRodney W. Grimes n = n->m_nextpkt; 645df8bae1dSRodney W. Grimes n->m_nextpkt = m; 646df8bae1dSRodney W. Grimes } else 647df8bae1dSRodney W. Grimes sb->sb_mb = m; 648df8bae1dSRodney W. Grimes return (1); 649df8bae1dSRodney W. Grimes } 650df8bae1dSRodney W. Grimes 65126f9a767SRodney W. Grimes int 652df8bae1dSRodney W. Grimes sbappendcontrol(sb, m0, control) 653df8bae1dSRodney W. Grimes struct sockbuf *sb; 654df8bae1dSRodney W. Grimes struct mbuf *control, *m0; 655df8bae1dSRodney W. Grimes { 6567ed60de8SPoul-Henning Kamp struct mbuf *m, *n; 6577ed60de8SPoul-Henning Kamp int space; 658df8bae1dSRodney W. Grimes 659df8bae1dSRodney W. Grimes if (control == 0) 660df8bae1dSRodney W. Grimes panic("sbappendcontrol"); 6617ed60de8SPoul-Henning Kamp space = m_length(control, &n) + m_length(m0, NULL); 662df8bae1dSRodney W. Grimes if (space > sbspace(sb)) 663df8bae1dSRodney W. Grimes return (0); 664df8bae1dSRodney W. Grimes n->m_next = m0; /* concatenate data to control */ 665df8bae1dSRodney W. Grimes for (m = control; m; m = m->m_next) 666df8bae1dSRodney W. Grimes sballoc(sb, m); 667797f2d22SPoul-Henning Kamp n = sb->sb_mb; 668797f2d22SPoul-Henning Kamp if (n) { 669df8bae1dSRodney W. Grimes while (n->m_nextpkt) 670df8bae1dSRodney W. Grimes n = n->m_nextpkt; 671df8bae1dSRodney W. Grimes n->m_nextpkt = control; 672df8bae1dSRodney W. Grimes } else 673df8bae1dSRodney W. Grimes sb->sb_mb = control; 674df8bae1dSRodney W. Grimes return (1); 675df8bae1dSRodney W. Grimes } 676df8bae1dSRodney W. Grimes 677df8bae1dSRodney W. Grimes /* 678df8bae1dSRodney W. Grimes * Compress mbuf chain m into the socket 679df8bae1dSRodney W. Grimes * buffer sb following mbuf n. If n 680df8bae1dSRodney W. Grimes * is null, the buffer is presumed empty. 681df8bae1dSRodney W. Grimes */ 68226f9a767SRodney W. Grimes void 683df8bae1dSRodney W. Grimes sbcompress(sb, m, n) 684df8bae1dSRodney W. Grimes register struct sockbuf *sb; 685df8bae1dSRodney W. Grimes register struct mbuf *m, *n; 686df8bae1dSRodney W. Grimes { 687df8bae1dSRodney W. Grimes register int eor = 0; 688df8bae1dSRodney W. Grimes register struct mbuf *o; 689df8bae1dSRodney W. Grimes 690df8bae1dSRodney W. Grimes while (m) { 691df8bae1dSRodney W. Grimes eor |= m->m_flags & M_EOR; 692df8bae1dSRodney W. Grimes if (m->m_len == 0 && 693df8bae1dSRodney W. Grimes (eor == 0 || 694df8bae1dSRodney W. Grimes (((o = m->m_next) || (o = n)) && 695df8bae1dSRodney W. Grimes o->m_type == m->m_type))) { 696df8bae1dSRodney W. Grimes m = m_free(m); 697df8bae1dSRodney W. Grimes continue; 698df8bae1dSRodney W. Grimes } 69932af0d74SDavid Malone if (n && (n->m_flags & M_EOR) == 0 && 70032af0d74SDavid Malone M_WRITABLE(n) && 70132af0d74SDavid Malone m->m_len <= MCLBYTES / 4 && /* XXX: Don't copy too much */ 70232af0d74SDavid Malone m->m_len <= M_TRAILINGSPACE(n) && 703df8bae1dSRodney W. Grimes n->m_type == m->m_type) { 704df8bae1dSRodney W. Grimes bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len, 705df8bae1dSRodney W. Grimes (unsigned)m->m_len); 706df8bae1dSRodney W. Grimes n->m_len += m->m_len; 707df8bae1dSRodney W. Grimes sb->sb_cc += m->m_len; 708b3f1af6bSTim J. Robbins if (m->m_type != MT_DATA && m->m_type != MT_HEADER && 709b3f1af6bSTim J. Robbins m->m_type != MT_OOBDATA) 710b3f1af6bSTim J. Robbins /* XXX: Probably don't need.*/ 71104ac9b97SKelly Yancey sb->sb_ctl += m->m_len; 712df8bae1dSRodney W. Grimes m = m_free(m); 713df8bae1dSRodney W. Grimes continue; 714df8bae1dSRodney W. Grimes } 715df8bae1dSRodney W. Grimes if (n) 716df8bae1dSRodney W. Grimes n->m_next = m; 717df8bae1dSRodney W. Grimes else 718df8bae1dSRodney W. Grimes sb->sb_mb = m; 719df8bae1dSRodney W. Grimes sballoc(sb, m); 720df8bae1dSRodney W. Grimes n = m; 721df8bae1dSRodney W. Grimes m->m_flags &= ~M_EOR; 722df8bae1dSRodney W. Grimes m = m->m_next; 723df8bae1dSRodney W. Grimes n->m_next = 0; 724df8bae1dSRodney W. Grimes } 725df8bae1dSRodney W. Grimes if (eor) { 726df8bae1dSRodney W. Grimes if (n) 727df8bae1dSRodney W. Grimes n->m_flags |= eor; 728df8bae1dSRodney W. Grimes else 729df8bae1dSRodney W. Grimes printf("semi-panic: sbcompress\n"); 730df8bae1dSRodney W. Grimes } 731df8bae1dSRodney W. Grimes } 732df8bae1dSRodney W. Grimes 733df8bae1dSRodney W. Grimes /* 734df8bae1dSRodney W. Grimes * Free all mbufs in a sockbuf. 735df8bae1dSRodney W. Grimes * Check that all resources are reclaimed. 736df8bae1dSRodney W. Grimes */ 73726f9a767SRodney W. Grimes void 738df8bae1dSRodney W. Grimes sbflush(sb) 739df8bae1dSRodney W. Grimes register struct sockbuf *sb; 740df8bae1dSRodney W. Grimes { 741df8bae1dSRodney W. Grimes 742df8bae1dSRodney W. Grimes if (sb->sb_flags & SB_LOCK) 743253ab668SAndrey A. Chernov panic("sbflush: locked"); 74423f84772SPierre Beyssac while (sb->sb_mbcnt) { 74523f84772SPierre Beyssac /* 74623f84772SPierre Beyssac * Don't call sbdrop(sb, 0) if the leading mbuf is non-empty: 74723f84772SPierre Beyssac * we would loop forever. Panic instead. 74823f84772SPierre Beyssac */ 74923f84772SPierre Beyssac if (!sb->sb_cc && (sb->sb_mb == NULL || sb->sb_mb->m_len)) 75023f84772SPierre Beyssac break; 751df8bae1dSRodney W. Grimes sbdrop(sb, (int)sb->sb_cc); 75223f84772SPierre Beyssac } 7530931333fSBill Fenner if (sb->sb_cc || sb->sb_mb || sb->sb_mbcnt) 754e25dadb0SRobert Drehmel panic("sbflush: cc %u || mb %p || mbcnt %u", sb->sb_cc, (void *)sb->sb_mb, sb->sb_mbcnt); 755df8bae1dSRodney W. Grimes } 756df8bae1dSRodney W. Grimes 757df8bae1dSRodney W. Grimes /* 758df8bae1dSRodney W. Grimes * Drop data from (the front of) a sockbuf. 759df8bae1dSRodney W. Grimes */ 76026f9a767SRodney W. Grimes void 761df8bae1dSRodney W. Grimes sbdrop(sb, len) 762df8bae1dSRodney W. Grimes register struct sockbuf *sb; 763df8bae1dSRodney W. Grimes register int len; 764df8bae1dSRodney W. Grimes { 765ecde8f7cSMatthew Dillon register struct mbuf *m; 766df8bae1dSRodney W. Grimes struct mbuf *next; 767df8bae1dSRodney W. Grimes 768df8bae1dSRodney W. Grimes next = (m = sb->sb_mb) ? m->m_nextpkt : 0; 769df8bae1dSRodney W. Grimes while (len > 0) { 770df8bae1dSRodney W. Grimes if (m == 0) { 771df8bae1dSRodney W. Grimes if (next == 0) 772df8bae1dSRodney W. Grimes panic("sbdrop"); 773df8bae1dSRodney W. Grimes m = next; 774df8bae1dSRodney W. Grimes next = m->m_nextpkt; 775df8bae1dSRodney W. Grimes continue; 776df8bae1dSRodney W. Grimes } 777df8bae1dSRodney W. Grimes if (m->m_len > len) { 778df8bae1dSRodney W. Grimes m->m_len -= len; 779df8bae1dSRodney W. Grimes m->m_data += len; 780df8bae1dSRodney W. Grimes sb->sb_cc -= len; 781b3f1af6bSTim J. Robbins if (m->m_type != MT_DATA && m->m_type != MT_HEADER && 782b3f1af6bSTim J. Robbins m->m_type != MT_OOBDATA) 78304ac9b97SKelly Yancey sb->sb_ctl -= len; 784df8bae1dSRodney W. Grimes break; 785df8bae1dSRodney W. Grimes } 786df8bae1dSRodney W. Grimes len -= m->m_len; 787df8bae1dSRodney W. Grimes sbfree(sb, m); 788ecde8f7cSMatthew Dillon m = m_free(m); 789df8bae1dSRodney W. Grimes } 790df8bae1dSRodney W. Grimes while (m && m->m_len == 0) { 791df8bae1dSRodney W. Grimes sbfree(sb, m); 792ecde8f7cSMatthew Dillon m = m_free(m); 793df8bae1dSRodney W. Grimes } 794df8bae1dSRodney W. Grimes if (m) { 795df8bae1dSRodney W. Grimes sb->sb_mb = m; 796df8bae1dSRodney W. Grimes m->m_nextpkt = next; 797df8bae1dSRodney W. Grimes } else 798df8bae1dSRodney W. Grimes sb->sb_mb = next; 799df8bae1dSRodney W. Grimes } 800df8bae1dSRodney W. Grimes 801df8bae1dSRodney W. Grimes /* 802df8bae1dSRodney W. Grimes * Drop a record off the front of a sockbuf 803df8bae1dSRodney W. Grimes * and move the next record to the front. 804df8bae1dSRodney W. Grimes */ 80526f9a767SRodney W. Grimes void 806df8bae1dSRodney W. Grimes sbdroprecord(sb) 807df8bae1dSRodney W. Grimes register struct sockbuf *sb; 808df8bae1dSRodney W. Grimes { 809ecde8f7cSMatthew Dillon register struct mbuf *m; 810df8bae1dSRodney W. Grimes 811df8bae1dSRodney W. Grimes m = sb->sb_mb; 812df8bae1dSRodney W. Grimes if (m) { 813df8bae1dSRodney W. Grimes sb->sb_mb = m->m_nextpkt; 814df8bae1dSRodney W. Grimes do { 815df8bae1dSRodney W. Grimes sbfree(sb, m); 816ecde8f7cSMatthew Dillon m = m_free(m); 817797f2d22SPoul-Henning Kamp } while (m); 818df8bae1dSRodney W. Grimes } 819df8bae1dSRodney W. Grimes } 8201e4ad9ceSGarrett Wollman 82182c23ebaSBill Fenner /* 82282c23ebaSBill Fenner * Create a "control" mbuf containing the specified data 82382c23ebaSBill Fenner * with the specified type for presentation on a socket buffer. 82482c23ebaSBill Fenner */ 82582c23ebaSBill Fenner struct mbuf * 82682c23ebaSBill Fenner sbcreatecontrol(p, size, type, level) 82782c23ebaSBill Fenner caddr_t p; 82882c23ebaSBill Fenner register int size; 82982c23ebaSBill Fenner int type, level; 83082c23ebaSBill Fenner { 83182c23ebaSBill Fenner register struct cmsghdr *cp; 83282c23ebaSBill Fenner struct mbuf *m; 83382c23ebaSBill Fenner 83459bdd405SDavid Malone if (CMSG_SPACE((u_int)size) > MCLBYTES) 8350b97e97cSYoshinobu Inoue return ((struct mbuf *) NULL); 83682c23ebaSBill Fenner if ((m = m_get(M_DONTWAIT, MT_CONTROL)) == NULL) 83782c23ebaSBill Fenner return ((struct mbuf *) NULL); 83859bdd405SDavid Malone if (CMSG_SPACE((u_int)size) > MLEN) { 83959bdd405SDavid Malone MCLGET(m, M_DONTWAIT); 84059bdd405SDavid Malone if ((m->m_flags & M_EXT) == 0) { 84159bdd405SDavid Malone m_free(m); 84259bdd405SDavid Malone return ((struct mbuf *) NULL); 84359bdd405SDavid Malone } 84459bdd405SDavid Malone } 84582c23ebaSBill Fenner cp = mtod(m, struct cmsghdr *); 84659bdd405SDavid Malone m->m_len = 0; 84759bdd405SDavid Malone KASSERT(CMSG_SPACE((u_int)size) <= M_TRAILINGSPACE(m), 84859bdd405SDavid Malone ("sbcreatecontrol: short mbuf")); 84959bdd405SDavid Malone if (p != NULL) 85082c23ebaSBill Fenner (void)memcpy(CMSG_DATA(cp), p, size); 8517d0d8dc3SYoshinobu Inoue m->m_len = CMSG_SPACE(size); 8527d0d8dc3SYoshinobu Inoue cp->cmsg_len = CMSG_LEN(size); 85382c23ebaSBill Fenner cp->cmsg_level = level; 85482c23ebaSBill Fenner cp->cmsg_type = type; 85582c23ebaSBill Fenner return (m); 85682c23ebaSBill Fenner } 85782c23ebaSBill Fenner 8581e4ad9ceSGarrett Wollman /* 8592c37256eSGarrett Wollman * Some routines that return EOPNOTSUPP for entry points that are not 8602c37256eSGarrett Wollman * supported by a protocol. Fill in as needed. 8611e4ad9ceSGarrett Wollman */ 8621e4ad9ceSGarrett Wollman int 86357bf258eSGarrett Wollman pru_accept_notsupp(struct socket *so, struct sockaddr **nam) 864d8392c6cSGarrett Wollman { 865d8392c6cSGarrett Wollman return EOPNOTSUPP; 866d8392c6cSGarrett Wollman } 867d8392c6cSGarrett Wollman 868d8392c6cSGarrett Wollman int 869b40ce416SJulian Elischer pru_connect_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td) 8709f907986SPeter Wemm { 8719f907986SPeter Wemm return EOPNOTSUPP; 8729f907986SPeter Wemm } 8739f907986SPeter Wemm 8749f907986SPeter Wemm int 8752c37256eSGarrett Wollman pru_connect2_notsupp(struct socket *so1, struct socket *so2) 8761e4ad9ceSGarrett Wollman { 8772c37256eSGarrett Wollman return EOPNOTSUPP; 8781e4ad9ceSGarrett Wollman } 879d8392c6cSGarrett Wollman 880d8392c6cSGarrett Wollman int 881ecbb00a2SDoug Rabson pru_control_notsupp(struct socket *so, u_long cmd, caddr_t data, 882b40ce416SJulian Elischer struct ifnet *ifp, struct thread *td) 883a29f300eSGarrett Wollman { 884a29f300eSGarrett Wollman return EOPNOTSUPP; 885a29f300eSGarrett Wollman } 886a29f300eSGarrett Wollman 887a29f300eSGarrett Wollman int 888b40ce416SJulian Elischer pru_listen_notsupp(struct socket *so, struct thread *td) 889d8392c6cSGarrett Wollman { 890d8392c6cSGarrett Wollman return EOPNOTSUPP; 891d8392c6cSGarrett Wollman } 892d8392c6cSGarrett Wollman 893d8392c6cSGarrett Wollman int 894d8392c6cSGarrett Wollman pru_rcvd_notsupp(struct socket *so, int flags) 895d8392c6cSGarrett Wollman { 896d8392c6cSGarrett Wollman return EOPNOTSUPP; 897d8392c6cSGarrett Wollman } 898d8392c6cSGarrett Wollman 899d8392c6cSGarrett Wollman int 900d8392c6cSGarrett Wollman pru_rcvoob_notsupp(struct socket *so, struct mbuf *m, int flags) 901d8392c6cSGarrett Wollman { 902d8392c6cSGarrett Wollman return EOPNOTSUPP; 903d8392c6cSGarrett Wollman } 904d8392c6cSGarrett Wollman 905d8392c6cSGarrett Wollman /* 906d8392c6cSGarrett Wollman * This isn't really a ``null'' operation, but it's the default one 907d8392c6cSGarrett Wollman * and doesn't do anything destructive. 908d8392c6cSGarrett Wollman */ 909d8392c6cSGarrett Wollman int 910d8392c6cSGarrett Wollman pru_sense_null(struct socket *so, struct stat *sb) 911d8392c6cSGarrett Wollman { 912d8392c6cSGarrett Wollman sb->st_blksize = so->so_snd.sb_hiwat; 913d8392c6cSGarrett Wollman return 0; 914d8392c6cSGarrett Wollman } 915639acc13SGarrett Wollman 916639acc13SGarrett Wollman /* 91757bf258eSGarrett Wollman * Make a copy of a sockaddr in a malloced buffer of type M_SONAME. 91857bf258eSGarrett Wollman */ 91957bf258eSGarrett Wollman struct sockaddr * 92057bf258eSGarrett Wollman dup_sockaddr(sa, canwait) 92157bf258eSGarrett Wollman struct sockaddr *sa; 92257bf258eSGarrett Wollman int canwait; 92357bf258eSGarrett Wollman { 92457bf258eSGarrett Wollman struct sockaddr *sa2; 92557bf258eSGarrett Wollman 92657bf258eSGarrett Wollman MALLOC(sa2, struct sockaddr *, sa->sa_len, M_SONAME, 92757bf258eSGarrett Wollman canwait ? M_WAITOK : M_NOWAIT); 92857bf258eSGarrett Wollman if (sa2) 92957bf258eSGarrett Wollman bcopy(sa, sa2, sa->sa_len); 93057bf258eSGarrett Wollman return sa2; 93157bf258eSGarrett Wollman } 93257bf258eSGarrett Wollman 93357bf258eSGarrett Wollman /* 93498271db4SGarrett Wollman * Create an external-format (``xsocket'') structure using the information 93598271db4SGarrett Wollman * in the kernel-format socket structure pointed to by so. This is done 93698271db4SGarrett Wollman * to reduce the spew of irrelevant information over this interface, 93798271db4SGarrett Wollman * to isolate user code from changes in the kernel structure, and 93898271db4SGarrett Wollman * potentially to provide information-hiding if we decide that 93998271db4SGarrett Wollman * some of this information should be hidden from users. 94098271db4SGarrett Wollman */ 94198271db4SGarrett Wollman void 94298271db4SGarrett Wollman sotoxsocket(struct socket *so, struct xsocket *xso) 94398271db4SGarrett Wollman { 94498271db4SGarrett Wollman xso->xso_len = sizeof *xso; 94598271db4SGarrett Wollman xso->xso_so = so; 94698271db4SGarrett Wollman xso->so_type = so->so_type; 94798271db4SGarrett Wollman xso->so_options = so->so_options; 94898271db4SGarrett Wollman xso->so_linger = so->so_linger; 94998271db4SGarrett Wollman xso->so_state = so->so_state; 95098271db4SGarrett Wollman xso->so_pcb = so->so_pcb; 95198271db4SGarrett Wollman xso->xso_protocol = so->so_proto->pr_protocol; 95298271db4SGarrett Wollman xso->xso_family = so->so_proto->pr_domain->dom_family; 95398271db4SGarrett Wollman xso->so_qlen = so->so_qlen; 95498271db4SGarrett Wollman xso->so_incqlen = so->so_incqlen; 95598271db4SGarrett Wollman xso->so_qlimit = so->so_qlimit; 95698271db4SGarrett Wollman xso->so_timeo = so->so_timeo; 95798271db4SGarrett Wollman xso->so_error = so->so_error; 958831d27a9SDon Lewis xso->so_pgid = so->so_sigio ? so->so_sigio->sio_pgid : 0; 95998271db4SGarrett Wollman xso->so_oobmark = so->so_oobmark; 96098271db4SGarrett Wollman sbtoxsockbuf(&so->so_snd, &xso->so_snd); 96198271db4SGarrett Wollman sbtoxsockbuf(&so->so_rcv, &xso->so_rcv); 9622f9a2132SBrian Feldman xso->so_uid = so->so_cred->cr_uid; 96398271db4SGarrett Wollman } 96498271db4SGarrett Wollman 96598271db4SGarrett Wollman /* 96698271db4SGarrett Wollman * This does the same for sockbufs. Note that the xsockbuf structure, 96798271db4SGarrett Wollman * since it is always embedded in a socket, does not include a self 96898271db4SGarrett Wollman * pointer nor a length. We make this entry point public in case 96998271db4SGarrett Wollman * some other mechanism needs it. 97098271db4SGarrett Wollman */ 97198271db4SGarrett Wollman void 97298271db4SGarrett Wollman sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb) 97398271db4SGarrett Wollman { 97498271db4SGarrett Wollman xsb->sb_cc = sb->sb_cc; 97598271db4SGarrett Wollman xsb->sb_hiwat = sb->sb_hiwat; 97698271db4SGarrett Wollman xsb->sb_mbcnt = sb->sb_mbcnt; 97798271db4SGarrett Wollman xsb->sb_mbmax = sb->sb_mbmax; 97898271db4SGarrett Wollman xsb->sb_lowat = sb->sb_lowat; 97998271db4SGarrett Wollman xsb->sb_flags = sb->sb_flags; 98098271db4SGarrett Wollman xsb->sb_timeo = sb->sb_timeo; 98198271db4SGarrett Wollman } 98298271db4SGarrett Wollman 98398271db4SGarrett Wollman /* 984639acc13SGarrett Wollman * Here is the definition of some of the basic objects in the kern.ipc 985639acc13SGarrett Wollman * branch of the MIB. 986639acc13SGarrett Wollman */ 987639acc13SGarrett Wollman SYSCTL_NODE(_kern, KERN_IPC, ipc, CTLFLAG_RW, 0, "IPC"); 988639acc13SGarrett Wollman 989639acc13SGarrett Wollman /* This takes the place of kern.maxsockbuf, which moved to kern.ipc. */ 990639acc13SGarrett Wollman static int dummy; 991639acc13SGarrett Wollman SYSCTL_INT(_kern, KERN_DUMMY, dummy, CTLFLAG_RW, &dummy, 0, ""); 99279cb7eb4SDavid Greenman SYSCTL_OID(_kern_ipc, KIPC_MAXSOCKBUF, maxsockbuf, CTLTYPE_INT|CTLFLAG_RW, 99379cb7eb4SDavid Greenman &sb_max, 0, sysctl_handle_sb_max, "I", "Maximum socket buffer size"); 99454d77689SJeff Roberson SYSCTL_INT(_kern_ipc, OID_AUTO, maxsockets, CTLFLAG_RD, 99554d77689SJeff Roberson &maxsockets, 0, "Maximum number of sockets avaliable"); 996639acc13SGarrett Wollman SYSCTL_INT(_kern_ipc, KIPC_SOCKBUF_WASTE, sockbuf_waste_factor, CTLFLAG_RW, 997639acc13SGarrett Wollman &sb_efficiency, 0, ""); 99854d77689SJeff Roberson 99954d77689SJeff Roberson /* 100054d77689SJeff Roberson * Initialise maxsockets 100154d77689SJeff Roberson */ 100254d77689SJeff Roberson static void init_maxsockets(void *ignored) 100354d77689SJeff Roberson { 100454d77689SJeff Roberson TUNABLE_INT_FETCH("kern.ipc.maxsockets", &maxsockets); 100554d77689SJeff Roberson maxsockets = imax(maxsockets, imax(maxfiles, nmbclusters)); 100654d77689SJeff Roberson } 100754d77689SJeff Roberson SYSINIT(param, SI_SUB_TUNABLES, SI_ORDER_ANY, init_maxsockets, NULL); 1008