Home | History | Annotate | Line # | Download | only in kern
uipc_socket2.c revision 1.85.8.2
      1  1.85.8.2  rmind /*	$NetBSD: uipc_socket2.c,v 1.85.8.2 2007/08/02 02:42:41 rmind Exp $	*/
      2  1.85.8.2  rmind 
      3  1.85.8.2  rmind /*
      4  1.85.8.2  rmind  * Copyright (c) 1982, 1986, 1988, 1990, 1993
      5  1.85.8.2  rmind  *	The Regents of the University of California.  All rights reserved.
      6  1.85.8.2  rmind  *
      7  1.85.8.2  rmind  * Redistribution and use in source and binary forms, with or without
      8  1.85.8.2  rmind  * modification, are permitted provided that the following conditions
      9  1.85.8.2  rmind  * are met:
     10  1.85.8.2  rmind  * 1. Redistributions of source code must retain the above copyright
     11  1.85.8.2  rmind  *    notice, this list of conditions and the following disclaimer.
     12  1.85.8.2  rmind  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.85.8.2  rmind  *    notice, this list of conditions and the following disclaimer in the
     14  1.85.8.2  rmind  *    documentation and/or other materials provided with the distribution.
     15  1.85.8.2  rmind  * 3. Neither the name of the University nor the names of its contributors
     16  1.85.8.2  rmind  *    may be used to endorse or promote products derived from this software
     17  1.85.8.2  rmind  *    without specific prior written permission.
     18  1.85.8.2  rmind  *
     19  1.85.8.2  rmind  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  1.85.8.2  rmind  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  1.85.8.2  rmind  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  1.85.8.2  rmind  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  1.85.8.2  rmind  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  1.85.8.2  rmind  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  1.85.8.2  rmind  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  1.85.8.2  rmind  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  1.85.8.2  rmind  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  1.85.8.2  rmind  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  1.85.8.2  rmind  * SUCH DAMAGE.
     30  1.85.8.2  rmind  *
     31  1.85.8.2  rmind  *	@(#)uipc_socket2.c	8.2 (Berkeley) 2/14/95
     32  1.85.8.2  rmind  */
     33  1.85.8.2  rmind 
     34  1.85.8.2  rmind #include <sys/cdefs.h>
     35  1.85.8.2  rmind __KERNEL_RCSID(0, "$NetBSD: uipc_socket2.c,v 1.85.8.2 2007/08/02 02:42:41 rmind Exp $");
     36  1.85.8.2  rmind 
     37  1.85.8.2  rmind #include "opt_mbuftrace.h"
     38  1.85.8.2  rmind #include "opt_sb_max.h"
     39  1.85.8.2  rmind 
     40  1.85.8.2  rmind #include <sys/param.h>
     41  1.85.8.2  rmind #include <sys/systm.h>
     42  1.85.8.2  rmind #include <sys/proc.h>
     43  1.85.8.2  rmind #include <sys/file.h>
     44  1.85.8.2  rmind #include <sys/buf.h>
     45  1.85.8.2  rmind #include <sys/malloc.h>
     46  1.85.8.2  rmind #include <sys/mbuf.h>
     47  1.85.8.2  rmind #include <sys/protosw.h>
     48  1.85.8.2  rmind #include <sys/poll.h>
     49  1.85.8.2  rmind #include <sys/socket.h>
     50  1.85.8.2  rmind #include <sys/socketvar.h>
     51  1.85.8.2  rmind #include <sys/signalvar.h>
     52  1.85.8.2  rmind #include <sys/kauth.h>
     53  1.85.8.2  rmind 
     54  1.85.8.2  rmind /*
     55  1.85.8.2  rmind  * Primitive routines for operating on sockets and socket buffers
     56  1.85.8.2  rmind  */
     57  1.85.8.2  rmind 
     58  1.85.8.2  rmind /* strings for sleep message: */
     59  1.85.8.2  rmind const char	netcon[] = "netcon";
     60  1.85.8.2  rmind const char	netcls[] = "netcls";
     61  1.85.8.2  rmind const char	netio[] = "netio";
     62  1.85.8.2  rmind const char	netlck[] = "netlck";
     63  1.85.8.2  rmind 
     64  1.85.8.2  rmind u_long	sb_max = SB_MAX;	/* maximum socket buffer size */
     65  1.85.8.2  rmind static u_long sb_max_adj;	/* adjusted sb_max */
     66  1.85.8.2  rmind 
     67  1.85.8.2  rmind /*
     68  1.85.8.2  rmind  * Procedures to manipulate state flags of socket
     69  1.85.8.2  rmind  * and do appropriate wakeups.  Normal sequence from the
     70  1.85.8.2  rmind  * active (originating) side is that soisconnecting() is
     71  1.85.8.2  rmind  * called during processing of connect() call,
     72  1.85.8.2  rmind  * resulting in an eventual call to soisconnected() if/when the
     73  1.85.8.2  rmind  * connection is established.  When the connection is torn down
     74  1.85.8.2  rmind  * soisdisconnecting() is called during processing of disconnect() call,
     75  1.85.8.2  rmind  * and soisdisconnected() is called when the connection to the peer
     76  1.85.8.2  rmind  * is totally severed.  The semantics of these routines are such that
     77  1.85.8.2  rmind  * connectionless protocols can call soisconnected() and soisdisconnected()
     78  1.85.8.2  rmind  * only, bypassing the in-progress calls when setting up a ``connection''
     79  1.85.8.2  rmind  * takes no time.
     80  1.85.8.2  rmind  *
     81  1.85.8.2  rmind  * From the passive side, a socket is created with
     82  1.85.8.2  rmind  * two queues of sockets: so_q0 for connections in progress
     83  1.85.8.2  rmind  * and so_q for connections already made and awaiting user acceptance.
     84  1.85.8.2  rmind  * As a protocol is preparing incoming connections, it creates a socket
     85  1.85.8.2  rmind  * structure queued on so_q0 by calling sonewconn().  When the connection
     86  1.85.8.2  rmind  * is established, soisconnected() is called, and transfers the
     87  1.85.8.2  rmind  * socket structure to so_q, making it available to accept().
     88  1.85.8.2  rmind  *
     89  1.85.8.2  rmind  * If a socket is closed with sockets on either
     90  1.85.8.2  rmind  * so_q0 or so_q, these sockets are dropped.
     91  1.85.8.2  rmind  *
     92  1.85.8.2  rmind  * If higher level protocols are implemented in
     93  1.85.8.2  rmind  * the kernel, the wakeups done here will sometimes
     94  1.85.8.2  rmind  * cause software-interrupt process scheduling.
     95  1.85.8.2  rmind  */
     96  1.85.8.2  rmind 
     97  1.85.8.2  rmind void
     98  1.85.8.2  rmind soisconnecting(struct socket *so)
     99  1.85.8.2  rmind {
    100  1.85.8.2  rmind 
    101  1.85.8.2  rmind 	so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
    102  1.85.8.2  rmind 	so->so_state |= SS_ISCONNECTING;
    103  1.85.8.2  rmind }
    104  1.85.8.2  rmind 
    105  1.85.8.2  rmind void
    106  1.85.8.2  rmind soisconnected(struct socket *so)
    107  1.85.8.2  rmind {
    108  1.85.8.2  rmind 	struct socket	*head;
    109  1.85.8.2  rmind 
    110  1.85.8.2  rmind 	head = so->so_head;
    111  1.85.8.2  rmind 	so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING);
    112  1.85.8.2  rmind 	so->so_state |= SS_ISCONNECTED;
    113  1.85.8.2  rmind 	if (head && soqremque(so, 0)) {
    114  1.85.8.2  rmind 		soqinsque(head, so, 1);
    115  1.85.8.2  rmind 		sorwakeup(head);
    116  1.85.8.2  rmind 		wakeup((void *)&head->so_timeo);
    117  1.85.8.2  rmind 	} else {
    118  1.85.8.2  rmind 		wakeup((void *)&so->so_timeo);
    119  1.85.8.2  rmind 		sorwakeup(so);
    120  1.85.8.2  rmind 		sowwakeup(so);
    121  1.85.8.2  rmind 	}
    122  1.85.8.2  rmind }
    123  1.85.8.2  rmind 
    124  1.85.8.2  rmind void
    125  1.85.8.2  rmind soisdisconnecting(struct socket *so)
    126  1.85.8.2  rmind {
    127  1.85.8.2  rmind 
    128  1.85.8.2  rmind 	so->so_state &= ~SS_ISCONNECTING;
    129  1.85.8.2  rmind 	so->so_state |= (SS_ISDISCONNECTING|SS_CANTRCVMORE|SS_CANTSENDMORE);
    130  1.85.8.2  rmind 	wakeup((void *)&so->so_timeo);
    131  1.85.8.2  rmind 	sowwakeup(so);
    132  1.85.8.2  rmind 	sorwakeup(so);
    133  1.85.8.2  rmind }
    134  1.85.8.2  rmind 
    135  1.85.8.2  rmind void
    136  1.85.8.2  rmind soisdisconnected(struct socket *so)
    137  1.85.8.2  rmind {
    138  1.85.8.2  rmind 
    139  1.85.8.2  rmind 	so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
    140  1.85.8.2  rmind 	so->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE|SS_ISDISCONNECTED);
    141  1.85.8.2  rmind 	wakeup((void *)&so->so_timeo);
    142  1.85.8.2  rmind 	sowwakeup(so);
    143  1.85.8.2  rmind 	sorwakeup(so);
    144  1.85.8.2  rmind }
    145  1.85.8.2  rmind 
    146  1.85.8.2  rmind /*
    147  1.85.8.2  rmind  * When an attempt at a new connection is noted on a socket
    148  1.85.8.2  rmind  * which accepts connections, sonewconn is called.  If the
    149  1.85.8.2  rmind  * connection is possible (subject to space constraints, etc.)
    150  1.85.8.2  rmind  * then we allocate a new structure, propoerly linked into the
    151  1.85.8.2  rmind  * data structure of the original socket, and return this.
    152  1.85.8.2  rmind  * Connstatus may be 0, SS_ISCONFIRMING, or SS_ISCONNECTED.
    153  1.85.8.2  rmind  */
    154  1.85.8.2  rmind struct socket *
    155  1.85.8.2  rmind sonewconn(struct socket *head, int connstatus)
    156  1.85.8.2  rmind {
    157  1.85.8.2  rmind 	struct socket	*so;
    158  1.85.8.2  rmind 	int		soqueue;
    159  1.85.8.2  rmind 
    160  1.85.8.2  rmind 	soqueue = connstatus ? 1 : 0;
    161  1.85.8.2  rmind 	if (head->so_qlen + head->so_q0len > 3 * head->so_qlimit / 2)
    162  1.85.8.2  rmind 		return ((struct socket *)0);
    163  1.85.8.2  rmind 	so = pool_get(&socket_pool, PR_NOWAIT);
    164  1.85.8.2  rmind 	if (so == NULL)
    165  1.85.8.2  rmind 		return (NULL);
    166  1.85.8.2  rmind 	memset((void *)so, 0, sizeof(*so));
    167  1.85.8.2  rmind 	so->so_type = head->so_type;
    168  1.85.8.2  rmind 	so->so_options = head->so_options &~ SO_ACCEPTCONN;
    169  1.85.8.2  rmind 	so->so_linger = head->so_linger;
    170  1.85.8.2  rmind 	so->so_state = head->so_state | SS_NOFDREF;
    171  1.85.8.2  rmind 	so->so_proto = head->so_proto;
    172  1.85.8.2  rmind 	so->so_timeo = head->so_timeo;
    173  1.85.8.2  rmind 	so->so_pgid = head->so_pgid;
    174  1.85.8.2  rmind 	so->so_send = head->so_send;
    175  1.85.8.2  rmind 	so->so_receive = head->so_receive;
    176  1.85.8.2  rmind 	so->so_uidinfo = head->so_uidinfo;
    177  1.85.8.2  rmind #ifdef MBUFTRACE
    178  1.85.8.2  rmind 	so->so_mowner = head->so_mowner;
    179  1.85.8.2  rmind 	so->so_rcv.sb_mowner = head->so_rcv.sb_mowner;
    180  1.85.8.2  rmind 	so->so_snd.sb_mowner = head->so_snd.sb_mowner;
    181  1.85.8.2  rmind #endif
    182  1.85.8.2  rmind 	(void) soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat);
    183  1.85.8.2  rmind 	so->so_snd.sb_lowat = head->so_snd.sb_lowat;
    184  1.85.8.2  rmind 	so->so_rcv.sb_lowat = head->so_rcv.sb_lowat;
    185  1.85.8.2  rmind 	so->so_rcv.sb_timeo = head->so_rcv.sb_timeo;
    186  1.85.8.2  rmind 	so->so_snd.sb_timeo = head->so_snd.sb_timeo;
    187  1.85.8.2  rmind 	so->so_rcv.sb_flags |= head->so_rcv.sb_flags & SB_AUTOSIZE;
    188  1.85.8.2  rmind 	so->so_snd.sb_flags |= head->so_snd.sb_flags & SB_AUTOSIZE;
    189  1.85.8.2  rmind 	soqinsque(head, so, soqueue);
    190  1.85.8.2  rmind 	if ((*so->so_proto->pr_usrreq)(so, PRU_ATTACH,
    191  1.85.8.2  rmind 	    (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0,
    192  1.85.8.2  rmind 	    (struct lwp *)0)) {
    193  1.85.8.2  rmind 		(void) soqremque(so, soqueue);
    194  1.85.8.2  rmind 		pool_put(&socket_pool, so);
    195  1.85.8.2  rmind 		return (NULL);
    196  1.85.8.2  rmind 	}
    197  1.85.8.2  rmind 	if (connstatus) {
    198  1.85.8.2  rmind 		sorwakeup(head);
    199  1.85.8.2  rmind 		wakeup((void *)&head->so_timeo);
    200  1.85.8.2  rmind 		so->so_state |= connstatus;
    201  1.85.8.2  rmind 	}
    202  1.85.8.2  rmind 	return (so);
    203  1.85.8.2  rmind }
    204  1.85.8.2  rmind 
    205  1.85.8.2  rmind void
    206  1.85.8.2  rmind soqinsque(struct socket *head, struct socket *so, int q)
    207  1.85.8.2  rmind {
    208  1.85.8.2  rmind 
    209  1.85.8.2  rmind #ifdef DIAGNOSTIC
    210  1.85.8.2  rmind 	if (so->so_onq != NULL)
    211  1.85.8.2  rmind 		panic("soqinsque");
    212  1.85.8.2  rmind #endif
    213  1.85.8.2  rmind 
    214  1.85.8.2  rmind 	so->so_head = head;
    215  1.85.8.2  rmind 	if (q == 0) {
    216  1.85.8.2  rmind 		head->so_q0len++;
    217  1.85.8.2  rmind 		so->so_onq = &head->so_q0;
    218  1.85.8.2  rmind 	} else {
    219  1.85.8.2  rmind 		head->so_qlen++;
    220  1.85.8.2  rmind 		so->so_onq = &head->so_q;
    221  1.85.8.2  rmind 	}
    222  1.85.8.2  rmind 	TAILQ_INSERT_TAIL(so->so_onq, so, so_qe);
    223  1.85.8.2  rmind }
    224  1.85.8.2  rmind 
    225  1.85.8.2  rmind int
    226  1.85.8.2  rmind soqremque(struct socket *so, int q)
    227  1.85.8.2  rmind {
    228  1.85.8.2  rmind 	struct socket	*head;
    229  1.85.8.2  rmind 
    230  1.85.8.2  rmind 	head = so->so_head;
    231  1.85.8.2  rmind 	if (q == 0) {
    232  1.85.8.2  rmind 		if (so->so_onq != &head->so_q0)
    233  1.85.8.2  rmind 			return (0);
    234  1.85.8.2  rmind 		head->so_q0len--;
    235  1.85.8.2  rmind 	} else {
    236  1.85.8.2  rmind 		if (so->so_onq != &head->so_q)
    237  1.85.8.2  rmind 			return (0);
    238  1.85.8.2  rmind 		head->so_qlen--;
    239  1.85.8.2  rmind 	}
    240  1.85.8.2  rmind 	TAILQ_REMOVE(so->so_onq, so, so_qe);
    241  1.85.8.2  rmind 	so->so_onq = NULL;
    242  1.85.8.2  rmind 	so->so_head = NULL;
    243  1.85.8.2  rmind 	return (1);
    244  1.85.8.2  rmind }
    245  1.85.8.2  rmind 
    246  1.85.8.2  rmind /*
    247  1.85.8.2  rmind  * Socantsendmore indicates that no more data will be sent on the
    248  1.85.8.2  rmind  * socket; it would normally be applied to a socket when the user
    249  1.85.8.2  rmind  * informs the system that no more data is to be sent, by the protocol
    250  1.85.8.2  rmind  * code (in case PRU_SHUTDOWN).  Socantrcvmore indicates that no more data
    251  1.85.8.2  rmind  * will be received, and will normally be applied to the socket by a
    252  1.85.8.2  rmind  * protocol when it detects that the peer will send no more data.
    253  1.85.8.2  rmind  * Data queued for reading in the socket may yet be read.
    254  1.85.8.2  rmind  */
    255  1.85.8.2  rmind 
    256  1.85.8.2  rmind void
    257  1.85.8.2  rmind socantsendmore(struct socket *so)
    258  1.85.8.2  rmind {
    259  1.85.8.2  rmind 
    260  1.85.8.2  rmind 	so->so_state |= SS_CANTSENDMORE;
    261  1.85.8.2  rmind 	sowwakeup(so);
    262  1.85.8.2  rmind }
    263  1.85.8.2  rmind 
    264  1.85.8.2  rmind void
    265  1.85.8.2  rmind socantrcvmore(struct socket *so)
    266  1.85.8.2  rmind {
    267  1.85.8.2  rmind 
    268  1.85.8.2  rmind 	so->so_state |= SS_CANTRCVMORE;
    269  1.85.8.2  rmind 	sorwakeup(so);
    270  1.85.8.2  rmind }
    271  1.85.8.2  rmind 
    272  1.85.8.2  rmind /*
    273  1.85.8.2  rmind  * Wait for data to arrive at/drain from a socket buffer.
    274  1.85.8.2  rmind  */
    275  1.85.8.2  rmind int
    276  1.85.8.2  rmind sbwait(struct sockbuf *sb)
    277  1.85.8.2  rmind {
    278  1.85.8.2  rmind 
    279  1.85.8.2  rmind 	sb->sb_flags |= SB_WAIT;
    280  1.85.8.2  rmind 	return (tsleep((void *)&sb->sb_cc,
    281  1.85.8.2  rmind 	    (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH, netio,
    282  1.85.8.2  rmind 	    sb->sb_timeo));
    283  1.85.8.2  rmind }
    284  1.85.8.2  rmind 
    285  1.85.8.2  rmind /*
    286  1.85.8.2  rmind  * Lock a sockbuf already known to be locked;
    287  1.85.8.2  rmind  * return any error returned from sleep (EINTR).
    288  1.85.8.2  rmind  */
    289  1.85.8.2  rmind int
    290  1.85.8.2  rmind sb_lock(struct sockbuf *sb)
    291  1.85.8.2  rmind {
    292  1.85.8.2  rmind 	int	error;
    293  1.85.8.2  rmind 
    294  1.85.8.2  rmind 	while (sb->sb_flags & SB_LOCK) {
    295  1.85.8.2  rmind 		sb->sb_flags |= SB_WANT;
    296  1.85.8.2  rmind 		error = tsleep((void *)&sb->sb_flags,
    297  1.85.8.2  rmind 		    (sb->sb_flags & SB_NOINTR) ?  PSOCK : PSOCK|PCATCH,
    298  1.85.8.2  rmind 		    netlck, 0);
    299  1.85.8.2  rmind 		if (error)
    300  1.85.8.2  rmind 			return (error);
    301  1.85.8.2  rmind 	}
    302  1.85.8.2  rmind 	sb->sb_flags |= SB_LOCK;
    303  1.85.8.2  rmind 	return (0);
    304  1.85.8.2  rmind }
    305  1.85.8.2  rmind 
    306  1.85.8.2  rmind /*
    307  1.85.8.2  rmind  * Wakeup processes waiting on a socket buffer.
    308  1.85.8.2  rmind  * Do asynchronous notification via SIGIO
    309  1.85.8.2  rmind  * if the socket buffer has the SB_ASYNC flag set.
    310  1.85.8.2  rmind  */
    311  1.85.8.2  rmind void
    312  1.85.8.2  rmind sowakeup(struct socket *so, struct sockbuf *sb, int code)
    313  1.85.8.2  rmind {
    314  1.85.8.2  rmind 	selnotify(&sb->sb_sel, 0);
    315  1.85.8.2  rmind 	sb->sb_flags &= ~SB_SEL;
    316  1.85.8.2  rmind 	if (sb->sb_flags & SB_WAIT) {
    317  1.85.8.2  rmind 		sb->sb_flags &= ~SB_WAIT;
    318  1.85.8.2  rmind 		wakeup((void *)&sb->sb_cc);
    319  1.85.8.2  rmind 	}
    320  1.85.8.2  rmind 	if (sb->sb_flags & SB_ASYNC) {
    321  1.85.8.2  rmind 		int band;
    322  1.85.8.2  rmind 		if (code == POLL_IN)
    323  1.85.8.2  rmind 			band = POLLIN|POLLRDNORM;
    324  1.85.8.2  rmind 		else
    325  1.85.8.2  rmind 			band = POLLOUT|POLLWRNORM;
    326  1.85.8.2  rmind 		fownsignal(so->so_pgid, SIGIO, code, band, so);
    327  1.85.8.2  rmind 	}
    328  1.85.8.2  rmind 	if (sb->sb_flags & SB_UPCALL)
    329  1.85.8.2  rmind 		(*so->so_upcall)(so, so->so_upcallarg, M_DONTWAIT);
    330  1.85.8.2  rmind }
    331  1.85.8.2  rmind 
    332  1.85.8.2  rmind /*
    333  1.85.8.2  rmind  * Socket buffer (struct sockbuf) utility routines.
    334  1.85.8.2  rmind  *
    335  1.85.8.2  rmind  * Each socket contains two socket buffers: one for sending data and
    336  1.85.8.2  rmind  * one for receiving data.  Each buffer contains a queue of mbufs,
    337  1.85.8.2  rmind  * information about the number of mbufs and amount of data in the
    338  1.85.8.2  rmind  * queue, and other fields allowing poll() statements and notification
    339  1.85.8.2  rmind  * on data availability to be implemented.
    340  1.85.8.2  rmind  *
    341  1.85.8.2  rmind  * Data stored in a socket buffer is maintained as a list of records.
    342  1.85.8.2  rmind  * Each record is a list of mbufs chained together with the m_next
    343  1.85.8.2  rmind  * field.  Records are chained together with the m_nextpkt field. The upper
    344  1.85.8.2  rmind  * level routine soreceive() expects the following conventions to be
    345  1.85.8.2  rmind  * observed when placing information in the receive buffer:
    346  1.85.8.2  rmind  *
    347  1.85.8.2  rmind  * 1. If the protocol requires each message be preceded by the sender's
    348  1.85.8.2  rmind  *    name, then a record containing that name must be present before
    349  1.85.8.2  rmind  *    any associated data (mbuf's must be of type MT_SONAME).
    350  1.85.8.2  rmind  * 2. If the protocol supports the exchange of ``access rights'' (really
    351  1.85.8.2  rmind  *    just additional data associated with the message), and there are
    352  1.85.8.2  rmind  *    ``rights'' to be received, then a record containing this data
    353  1.85.8.2  rmind  *    should be present (mbuf's must be of type MT_CONTROL).
    354  1.85.8.2  rmind  * 3. If a name or rights record exists, then it must be followed by
    355  1.85.8.2  rmind  *    a data record, perhaps of zero length.
    356  1.85.8.2  rmind  *
    357  1.85.8.2  rmind  * Before using a new socket structure it is first necessary to reserve
    358  1.85.8.2  rmind  * buffer space to the socket, by calling sbreserve().  This should commit
    359  1.85.8.2  rmind  * some of the available buffer space in the system buffer pool for the
    360  1.85.8.2  rmind  * socket (currently, it does nothing but enforce limits).  The space
    361  1.85.8.2  rmind  * should be released by calling sbrelease() when the socket is destroyed.
    362  1.85.8.2  rmind  */
    363  1.85.8.2  rmind 
    364  1.85.8.2  rmind int
    365  1.85.8.2  rmind sb_max_set(u_long new_sbmax)
    366  1.85.8.2  rmind {
    367  1.85.8.2  rmind 	int s;
    368  1.85.8.2  rmind 
    369  1.85.8.2  rmind 	if (new_sbmax < (16 * 1024))
    370  1.85.8.2  rmind 		return (EINVAL);
    371  1.85.8.2  rmind 
    372  1.85.8.2  rmind 	s = splsoftnet();
    373  1.85.8.2  rmind 	sb_max = new_sbmax;
    374  1.85.8.2  rmind 	sb_max_adj = (u_quad_t)new_sbmax * MCLBYTES / (MSIZE + MCLBYTES);
    375  1.85.8.2  rmind 	splx(s);
    376  1.85.8.2  rmind 
    377  1.85.8.2  rmind 	return (0);
    378  1.85.8.2  rmind }
    379  1.85.8.2  rmind 
    380  1.85.8.2  rmind int
    381  1.85.8.2  rmind soreserve(struct socket *so, u_long sndcc, u_long rcvcc)
    382  1.85.8.2  rmind {
    383  1.85.8.2  rmind 	/*
    384  1.85.8.2  rmind 	 * there's at least one application (a configure script of screen)
    385  1.85.8.2  rmind 	 * which expects a fifo is writable even if it has "some" bytes
    386  1.85.8.2  rmind 	 * in its buffer.
    387  1.85.8.2  rmind 	 * so we want to make sure (hiwat - lowat) >= (some bytes).
    388  1.85.8.2  rmind 	 *
    389  1.85.8.2  rmind 	 * PIPE_BUF here is an arbitrary value chosen as (some bytes) above.
    390  1.85.8.2  rmind 	 * we expect it's large enough for such applications.
    391  1.85.8.2  rmind 	 */
    392  1.85.8.2  rmind 	u_long  lowat = MAX(sock_loan_thresh, MCLBYTES);
    393  1.85.8.2  rmind 	u_long  hiwat = lowat + PIPE_BUF;
    394  1.85.8.2  rmind 
    395  1.85.8.2  rmind 	if (sndcc < hiwat)
    396  1.85.8.2  rmind 		sndcc = hiwat;
    397  1.85.8.2  rmind 	if (sbreserve(&so->so_snd, sndcc, so) == 0)
    398  1.85.8.2  rmind 		goto bad;
    399  1.85.8.2  rmind 	if (sbreserve(&so->so_rcv, rcvcc, so) == 0)
    400  1.85.8.2  rmind 		goto bad2;
    401  1.85.8.2  rmind 	if (so->so_rcv.sb_lowat == 0)
    402  1.85.8.2  rmind 		so->so_rcv.sb_lowat = 1;
    403  1.85.8.2  rmind 	if (so->so_snd.sb_lowat == 0)
    404  1.85.8.2  rmind 		so->so_snd.sb_lowat = lowat;
    405  1.85.8.2  rmind 	if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat)
    406  1.85.8.2  rmind 		so->so_snd.sb_lowat = so->so_snd.sb_hiwat;
    407  1.85.8.2  rmind 	return (0);
    408  1.85.8.2  rmind  bad2:
    409  1.85.8.2  rmind 	sbrelease(&so->so_snd, so);
    410  1.85.8.2  rmind  bad:
    411  1.85.8.2  rmind 	return (ENOBUFS);
    412  1.85.8.2  rmind }
    413  1.85.8.2  rmind 
    414  1.85.8.2  rmind /*
    415  1.85.8.2  rmind  * Allot mbufs to a sockbuf.
    416  1.85.8.2  rmind  * Attempt to scale mbmax so that mbcnt doesn't become limiting
    417  1.85.8.2  rmind  * if buffering efficiency is near the normal case.
    418  1.85.8.2  rmind  */
    419  1.85.8.2  rmind int
    420  1.85.8.2  rmind sbreserve(struct sockbuf *sb, u_long cc, struct socket *so)
    421  1.85.8.2  rmind {
    422  1.85.8.2  rmind 	struct lwp *l = curlwp; /* XXX */
    423  1.85.8.2  rmind 	rlim_t maxcc;
    424  1.85.8.2  rmind 	struct uidinfo *uidinfo;
    425  1.85.8.2  rmind 
    426  1.85.8.2  rmind 	KDASSERT(sb_max_adj != 0);
    427  1.85.8.2  rmind 	if (cc == 0 || cc > sb_max_adj)
    428  1.85.8.2  rmind 		return (0);
    429  1.85.8.2  rmind 	if (so) {
    430  1.85.8.2  rmind 		if (l && kauth_cred_geteuid(l->l_cred) == so->so_uidinfo->ui_uid)
    431  1.85.8.2  rmind 			maxcc = l->l_proc->p_rlimit[RLIMIT_SBSIZE].rlim_cur;
    432  1.85.8.2  rmind 		else
    433  1.85.8.2  rmind 			maxcc = RLIM_INFINITY;
    434  1.85.8.2  rmind 		uidinfo = so->so_uidinfo;
    435  1.85.8.2  rmind 	} else {
    436  1.85.8.2  rmind 		uidinfo = uid_find(0);	/* XXX: nothing better */
    437  1.85.8.2  rmind 		maxcc = RLIM_INFINITY;
    438  1.85.8.2  rmind 	}
    439  1.85.8.2  rmind 	if (!chgsbsize(uidinfo, &sb->sb_hiwat, cc, maxcc))
    440  1.85.8.2  rmind 		return 0;
    441  1.85.8.2  rmind 	sb->sb_mbmax = min(cc * 2, sb_max);
    442  1.85.8.2  rmind 	if (sb->sb_lowat > sb->sb_hiwat)
    443  1.85.8.2  rmind 		sb->sb_lowat = sb->sb_hiwat;
    444  1.85.8.2  rmind 	return (1);
    445  1.85.8.2  rmind }
    446  1.85.8.2  rmind 
    447  1.85.8.2  rmind /*
    448  1.85.8.2  rmind  * Free mbufs held by a socket, and reserved mbuf space.
    449  1.85.8.2  rmind  */
    450  1.85.8.2  rmind void
    451  1.85.8.2  rmind sbrelease(struct sockbuf *sb, struct socket *so)
    452  1.85.8.2  rmind {
    453  1.85.8.2  rmind 
    454  1.85.8.2  rmind 	sbflush(sb);
    455  1.85.8.2  rmind 	(void)chgsbsize(so->so_uidinfo, &sb->sb_hiwat, 0,
    456  1.85.8.2  rmind 	    RLIM_INFINITY);
    457  1.85.8.2  rmind 	sb->sb_mbmax = 0;
    458  1.85.8.2  rmind }
    459  1.85.8.2  rmind 
    460  1.85.8.2  rmind /*
    461  1.85.8.2  rmind  * Routines to add and remove
    462  1.85.8.2  rmind  * data from an mbuf queue.
    463  1.85.8.2  rmind  *
    464  1.85.8.2  rmind  * The routines sbappend() or sbappendrecord() are normally called to
    465  1.85.8.2  rmind  * append new mbufs to a socket buffer, after checking that adequate
    466  1.85.8.2  rmind  * space is available, comparing the function sbspace() with the amount
    467  1.85.8.2  rmind  * of data to be added.  sbappendrecord() differs from sbappend() in
    468  1.85.8.2  rmind  * that data supplied is treated as the beginning of a new record.
    469  1.85.8.2  rmind  * To place a sender's address, optional access rights, and data in a
    470  1.85.8.2  rmind  * socket receive buffer, sbappendaddr() should be used.  To place
    471  1.85.8.2  rmind  * access rights and data in a socket receive buffer, sbappendrights()
    472  1.85.8.2  rmind  * should be used.  In either case, the new data begins a new record.
    473  1.85.8.2  rmind  * Note that unlike sbappend() and sbappendrecord(), these routines check
    474  1.85.8.2  rmind  * for the caller that there will be enough space to store the data.
    475  1.85.8.2  rmind  * Each fails if there is not enough space, or if it cannot find mbufs
    476  1.85.8.2  rmind  * to store additional information in.
    477  1.85.8.2  rmind  *
    478  1.85.8.2  rmind  * Reliable protocols may use the socket send buffer to hold data
    479  1.85.8.2  rmind  * awaiting acknowledgement.  Data is normally copied from a socket
    480  1.85.8.2  rmind  * send buffer in a protocol with m_copy for output to a peer,
    481  1.85.8.2  rmind  * and then removing the data from the socket buffer with sbdrop()
    482  1.85.8.2  rmind  * or sbdroprecord() when the data is acknowledged by the peer.
    483  1.85.8.2  rmind  */
    484  1.85.8.2  rmind 
    485  1.85.8.2  rmind #ifdef SOCKBUF_DEBUG
    486  1.85.8.2  rmind void
    487  1.85.8.2  rmind sblastrecordchk(struct sockbuf *sb, const char *where)
    488  1.85.8.2  rmind {
    489  1.85.8.2  rmind 	struct mbuf *m = sb->sb_mb;
    490  1.85.8.2  rmind 
    491  1.85.8.2  rmind 	while (m && m->m_nextpkt)
    492  1.85.8.2  rmind 		m = m->m_nextpkt;
    493  1.85.8.2  rmind 
    494  1.85.8.2  rmind 	if (m != sb->sb_lastrecord) {
    495  1.85.8.2  rmind 		printf("sblastrecordchk: sb_mb %p sb_lastrecord %p last %p\n",
    496  1.85.8.2  rmind 		    sb->sb_mb, sb->sb_lastrecord, m);
    497  1.85.8.2  rmind 		printf("packet chain:\n");
    498  1.85.8.2  rmind 		for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt)
    499  1.85.8.2  rmind 			printf("\t%p\n", m);
    500  1.85.8.2  rmind 		panic("sblastrecordchk from %s", where);
    501  1.85.8.2  rmind 	}
    502  1.85.8.2  rmind }
    503  1.85.8.2  rmind 
    504  1.85.8.2  rmind void
    505  1.85.8.2  rmind sblastmbufchk(struct sockbuf *sb, const char *where)
    506  1.85.8.2  rmind {
    507  1.85.8.2  rmind 	struct mbuf *m = sb->sb_mb;
    508  1.85.8.2  rmind 	struct mbuf *n;
    509  1.85.8.2  rmind 
    510  1.85.8.2  rmind 	while (m && m->m_nextpkt)
    511  1.85.8.2  rmind 		m = m->m_nextpkt;
    512  1.85.8.2  rmind 
    513  1.85.8.2  rmind 	while (m && m->m_next)
    514  1.85.8.2  rmind 		m = m->m_next;
    515  1.85.8.2  rmind 
    516  1.85.8.2  rmind 	if (m != sb->sb_mbtail) {
    517  1.85.8.2  rmind 		printf("sblastmbufchk: sb_mb %p sb_mbtail %p last %p\n",
    518  1.85.8.2  rmind 		    sb->sb_mb, sb->sb_mbtail, m);
    519  1.85.8.2  rmind 		printf("packet tree:\n");
    520  1.85.8.2  rmind 		for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt) {
    521  1.85.8.2  rmind 			printf("\t");
    522  1.85.8.2  rmind 			for (n = m; n != NULL; n = n->m_next)
    523  1.85.8.2  rmind 				printf("%p ", n);
    524  1.85.8.2  rmind 			printf("\n");
    525  1.85.8.2  rmind 		}
    526  1.85.8.2  rmind 		panic("sblastmbufchk from %s", where);
    527  1.85.8.2  rmind 	}
    528  1.85.8.2  rmind }
    529  1.85.8.2  rmind #endif /* SOCKBUF_DEBUG */
    530  1.85.8.2  rmind 
    531  1.85.8.2  rmind /*
    532  1.85.8.2  rmind  * Link a chain of records onto a socket buffer
    533  1.85.8.2  rmind  */
    534  1.85.8.2  rmind #define	SBLINKRECORDCHAIN(sb, m0, mlast)				\
    535  1.85.8.2  rmind do {									\
    536  1.85.8.2  rmind 	if ((sb)->sb_lastrecord != NULL)				\
    537  1.85.8.2  rmind 		(sb)->sb_lastrecord->m_nextpkt = (m0);			\
    538  1.85.8.2  rmind 	else								\
    539  1.85.8.2  rmind 		(sb)->sb_mb = (m0);					\
    540  1.85.8.2  rmind 	(sb)->sb_lastrecord = (mlast);					\
    541  1.85.8.2  rmind } while (/*CONSTCOND*/0)
    542  1.85.8.2  rmind 
    543  1.85.8.2  rmind 
    544  1.85.8.2  rmind #define	SBLINKRECORD(sb, m0)						\
    545  1.85.8.2  rmind     SBLINKRECORDCHAIN(sb, m0, m0)
    546  1.85.8.2  rmind 
    547  1.85.8.2  rmind /*
    548  1.85.8.2  rmind  * Append mbuf chain m to the last record in the
    549  1.85.8.2  rmind  * socket buffer sb.  The additional space associated
    550  1.85.8.2  rmind  * the mbuf chain is recorded in sb.  Empty mbufs are
    551  1.85.8.2  rmind  * discarded and mbufs are compacted where possible.
    552  1.85.8.2  rmind  */
    553  1.85.8.2  rmind void
    554  1.85.8.2  rmind sbappend(struct sockbuf *sb, struct mbuf *m)
    555  1.85.8.2  rmind {
    556  1.85.8.2  rmind 	struct mbuf	*n;
    557  1.85.8.2  rmind 
    558  1.85.8.2  rmind 	if (m == 0)
    559  1.85.8.2  rmind 		return;
    560  1.85.8.2  rmind 
    561  1.85.8.2  rmind #ifdef MBUFTRACE
    562  1.85.8.2  rmind 	m_claimm(m, sb->sb_mowner);
    563  1.85.8.2  rmind #endif
    564  1.85.8.2  rmind 
    565  1.85.8.2  rmind 	SBLASTRECORDCHK(sb, "sbappend 1");
    566  1.85.8.2  rmind 
    567  1.85.8.2  rmind 	if ((n = sb->sb_lastrecord) != NULL) {
    568  1.85.8.2  rmind 		/*
    569  1.85.8.2  rmind 		 * XXX Would like to simply use sb_mbtail here, but
    570  1.85.8.2  rmind 		 * XXX I need to verify that I won't miss an EOR that
    571  1.85.8.2  rmind 		 * XXX way.
    572  1.85.8.2  rmind 		 */
    573  1.85.8.2  rmind 		do {
    574  1.85.8.2  rmind 			if (n->m_flags & M_EOR) {
    575  1.85.8.2  rmind 				sbappendrecord(sb, m); /* XXXXXX!!!! */
    576  1.85.8.2  rmind 				return;
    577  1.85.8.2  rmind 			}
    578  1.85.8.2  rmind 		} while (n->m_next && (n = n->m_next));
    579  1.85.8.2  rmind 	} else {
    580  1.85.8.2  rmind 		/*
    581  1.85.8.2  rmind 		 * If this is the first record in the socket buffer, it's
    582  1.85.8.2  rmind 		 * also the last record.
    583  1.85.8.2  rmind 		 */
    584  1.85.8.2  rmind 		sb->sb_lastrecord = m;
    585  1.85.8.2  rmind 	}
    586  1.85.8.2  rmind 	sbcompress(sb, m, n);
    587  1.85.8.2  rmind 	SBLASTRECORDCHK(sb, "sbappend 2");
    588  1.85.8.2  rmind }
    589  1.85.8.2  rmind 
    590  1.85.8.2  rmind /*
    591  1.85.8.2  rmind  * This version of sbappend() should only be used when the caller
    592  1.85.8.2  rmind  * absolutely knows that there will never be more than one record
    593  1.85.8.2  rmind  * in the socket buffer, that is, a stream protocol (such as TCP).
    594  1.85.8.2  rmind  */
    595  1.85.8.2  rmind void
    596  1.85.8.2  rmind sbappendstream(struct sockbuf *sb, struct mbuf *m)
    597  1.85.8.2  rmind {
    598  1.85.8.2  rmind 
    599  1.85.8.2  rmind 	KDASSERT(m->m_nextpkt == NULL);
    600  1.85.8.2  rmind 	KASSERT(sb->sb_mb == sb->sb_lastrecord);
    601  1.85.8.2  rmind 
    602  1.85.8.2  rmind 	SBLASTMBUFCHK(sb, __func__);
    603  1.85.8.2  rmind 
    604  1.85.8.2  rmind #ifdef MBUFTRACE
    605  1.85.8.2  rmind 	m_claimm(m, sb->sb_mowner);
    606  1.85.8.2  rmind #endif
    607  1.85.8.2  rmind 
    608  1.85.8.2  rmind 	sbcompress(sb, m, sb->sb_mbtail);
    609  1.85.8.2  rmind 
    610  1.85.8.2  rmind 	sb->sb_lastrecord = sb->sb_mb;
    611  1.85.8.2  rmind 	SBLASTRECORDCHK(sb, __func__);
    612  1.85.8.2  rmind }
    613  1.85.8.2  rmind 
    614  1.85.8.2  rmind #ifdef SOCKBUF_DEBUG
    615  1.85.8.2  rmind void
    616  1.85.8.2  rmind sbcheck(struct sockbuf *sb)
    617  1.85.8.2  rmind {
    618  1.85.8.2  rmind 	struct mbuf	*m;
    619  1.85.8.2  rmind 	u_long		len, mbcnt;
    620  1.85.8.2  rmind 
    621  1.85.8.2  rmind 	len = 0;
    622  1.85.8.2  rmind 	mbcnt = 0;
    623  1.85.8.2  rmind 	for (m = sb->sb_mb; m; m = m->m_next) {
    624  1.85.8.2  rmind 		len += m->m_len;
    625  1.85.8.2  rmind 		mbcnt += MSIZE;
    626  1.85.8.2  rmind 		if (m->m_flags & M_EXT)
    627  1.85.8.2  rmind 			mbcnt += m->m_ext.ext_size;
    628  1.85.8.2  rmind 		if (m->m_nextpkt)
    629  1.85.8.2  rmind 			panic("sbcheck nextpkt");
    630  1.85.8.2  rmind 	}
    631  1.85.8.2  rmind 	if (len != sb->sb_cc || mbcnt != sb->sb_mbcnt) {
    632  1.85.8.2  rmind 		printf("cc %lu != %lu || mbcnt %lu != %lu\n", len, sb->sb_cc,
    633  1.85.8.2  rmind 		    mbcnt, sb->sb_mbcnt);
    634  1.85.8.2  rmind 		panic("sbcheck");
    635  1.85.8.2  rmind 	}
    636  1.85.8.2  rmind }
    637  1.85.8.2  rmind #endif
    638  1.85.8.2  rmind 
    639  1.85.8.2  rmind /*
    640  1.85.8.2  rmind  * As above, except the mbuf chain
    641  1.85.8.2  rmind  * begins a new record.
    642  1.85.8.2  rmind  */
    643  1.85.8.2  rmind void
    644  1.85.8.2  rmind sbappendrecord(struct sockbuf *sb, struct mbuf *m0)
    645  1.85.8.2  rmind {
    646  1.85.8.2  rmind 	struct mbuf	*m;
    647  1.85.8.2  rmind 
    648  1.85.8.2  rmind 	if (m0 == 0)
    649  1.85.8.2  rmind 		return;
    650  1.85.8.2  rmind 
    651  1.85.8.2  rmind #ifdef MBUFTRACE
    652  1.85.8.2  rmind 	m_claimm(m0, sb->sb_mowner);
    653  1.85.8.2  rmind #endif
    654  1.85.8.2  rmind 	/*
    655  1.85.8.2  rmind 	 * Put the first mbuf on the queue.
    656  1.85.8.2  rmind 	 * Note this permits zero length records.
    657  1.85.8.2  rmind 	 */
    658  1.85.8.2  rmind 	sballoc(sb, m0);
    659  1.85.8.2  rmind 	SBLASTRECORDCHK(sb, "sbappendrecord 1");
    660  1.85.8.2  rmind 	SBLINKRECORD(sb, m0);
    661  1.85.8.2  rmind 	m = m0->m_next;
    662  1.85.8.2  rmind 	m0->m_next = 0;
    663  1.85.8.2  rmind 	if (m && (m0->m_flags & M_EOR)) {
    664  1.85.8.2  rmind 		m0->m_flags &= ~M_EOR;
    665  1.85.8.2  rmind 		m->m_flags |= M_EOR;
    666  1.85.8.2  rmind 	}
    667  1.85.8.2  rmind 	sbcompress(sb, m, m0);
    668  1.85.8.2  rmind 	SBLASTRECORDCHK(sb, "sbappendrecord 2");
    669  1.85.8.2  rmind }
    670  1.85.8.2  rmind 
    671  1.85.8.2  rmind /*
    672  1.85.8.2  rmind  * As above except that OOB data
    673  1.85.8.2  rmind  * is inserted at the beginning of the sockbuf,
    674  1.85.8.2  rmind  * but after any other OOB data.
    675  1.85.8.2  rmind  */
    676  1.85.8.2  rmind void
    677  1.85.8.2  rmind sbinsertoob(struct sockbuf *sb, struct mbuf *m0)
    678  1.85.8.2  rmind {
    679  1.85.8.2  rmind 	struct mbuf	*m, **mp;
    680  1.85.8.2  rmind 
    681  1.85.8.2  rmind 	if (m0 == 0)
    682  1.85.8.2  rmind 		return;
    683  1.85.8.2  rmind 
    684  1.85.8.2  rmind 	SBLASTRECORDCHK(sb, "sbinsertoob 1");
    685  1.85.8.2  rmind 
    686  1.85.8.2  rmind 	for (mp = &sb->sb_mb; (m = *mp) != NULL; mp = &((*mp)->m_nextpkt)) {
    687  1.85.8.2  rmind 	    again:
    688  1.85.8.2  rmind 		switch (m->m_type) {
    689  1.85.8.2  rmind 
    690  1.85.8.2  rmind 		case MT_OOBDATA:
    691  1.85.8.2  rmind 			continue;		/* WANT next train */
    692  1.85.8.2  rmind 
    693  1.85.8.2  rmind 		case MT_CONTROL:
    694  1.85.8.2  rmind 			if ((m = m->m_next) != NULL)
    695  1.85.8.2  rmind 				goto again;	/* inspect THIS train further */
    696  1.85.8.2  rmind 		}
    697  1.85.8.2  rmind 		break;
    698  1.85.8.2  rmind 	}
    699  1.85.8.2  rmind 	/*
    700  1.85.8.2  rmind 	 * Put the first mbuf on the queue.
    701  1.85.8.2  rmind 	 * Note this permits zero length records.
    702  1.85.8.2  rmind 	 */
    703  1.85.8.2  rmind 	sballoc(sb, m0);
    704  1.85.8.2  rmind 	m0->m_nextpkt = *mp;
    705  1.85.8.2  rmind 	if (*mp == NULL) {
    706  1.85.8.2  rmind 		/* m0 is actually the new tail */
    707  1.85.8.2  rmind 		sb->sb_lastrecord = m0;
    708  1.85.8.2  rmind 	}
    709  1.85.8.2  rmind 	*mp = m0;
    710  1.85.8.2  rmind 	m = m0->m_next;
    711  1.85.8.2  rmind 	m0->m_next = 0;
    712  1.85.8.2  rmind 	if (m && (m0->m_flags & M_EOR)) {
    713  1.85.8.2  rmind 		m0->m_flags &= ~M_EOR;
    714  1.85.8.2  rmind 		m->m_flags |= M_EOR;
    715  1.85.8.2  rmind 	}
    716  1.85.8.2  rmind 	sbcompress(sb, m, m0);
    717  1.85.8.2  rmind 	SBLASTRECORDCHK(sb, "sbinsertoob 2");
    718  1.85.8.2  rmind }
    719  1.85.8.2  rmind 
    720  1.85.8.2  rmind /*
    721  1.85.8.2  rmind  * Append address and data, and optionally, control (ancillary) data
    722  1.85.8.2  rmind  * to the receive queue of a socket.  If present,
    723  1.85.8.2  rmind  * m0 must include a packet header with total length.
    724  1.85.8.2  rmind  * Returns 0 if no space in sockbuf or insufficient mbufs.
    725  1.85.8.2  rmind  */
    726  1.85.8.2  rmind int
    727  1.85.8.2  rmind sbappendaddr(struct sockbuf *sb, const struct sockaddr *asa, struct mbuf *m0,
    728  1.85.8.2  rmind 	struct mbuf *control)
    729  1.85.8.2  rmind {
    730  1.85.8.2  rmind 	struct mbuf	*m, *n, *nlast;
    731  1.85.8.2  rmind 	int		space, len;
    732  1.85.8.2  rmind 
    733  1.85.8.2  rmind 	space = asa->sa_len;
    734  1.85.8.2  rmind 
    735  1.85.8.2  rmind 	if (m0 != NULL) {
    736  1.85.8.2  rmind 		if ((m0->m_flags & M_PKTHDR) == 0)
    737  1.85.8.2  rmind 			panic("sbappendaddr");
    738  1.85.8.2  rmind 		space += m0->m_pkthdr.len;
    739  1.85.8.2  rmind #ifdef MBUFTRACE
    740  1.85.8.2  rmind 		m_claimm(m0, sb->sb_mowner);
    741  1.85.8.2  rmind #endif
    742  1.85.8.2  rmind 	}
    743  1.85.8.2  rmind 	for (n = control; n; n = n->m_next) {
    744  1.85.8.2  rmind 		space += n->m_len;
    745  1.85.8.2  rmind 		MCLAIM(n, sb->sb_mowner);
    746  1.85.8.2  rmind 		if (n->m_next == 0)	/* keep pointer to last control buf */
    747  1.85.8.2  rmind 			break;
    748  1.85.8.2  rmind 	}
    749  1.85.8.2  rmind 	if (space > sbspace(sb))
    750  1.85.8.2  rmind 		return (0);
    751  1.85.8.2  rmind 	MGET(m, M_DONTWAIT, MT_SONAME);
    752  1.85.8.2  rmind 	if (m == 0)
    753  1.85.8.2  rmind 		return (0);
    754  1.85.8.2  rmind 	MCLAIM(m, sb->sb_mowner);
    755  1.85.8.2  rmind 	/*
    756  1.85.8.2  rmind 	 * XXX avoid 'comparison always true' warning which isn't easily
    757  1.85.8.2  rmind 	 * avoided.
    758  1.85.8.2  rmind 	 */
    759  1.85.8.2  rmind 	len = asa->sa_len;
    760  1.85.8.2  rmind 	if (len > MLEN) {
    761  1.85.8.2  rmind 		MEXTMALLOC(m, asa->sa_len, M_NOWAIT);
    762  1.85.8.2  rmind 		if ((m->m_flags & M_EXT) == 0) {
    763  1.85.8.2  rmind 			m_free(m);
    764  1.85.8.2  rmind 			return (0);
    765  1.85.8.2  rmind 		}
    766  1.85.8.2  rmind 	}
    767  1.85.8.2  rmind 	m->m_len = asa->sa_len;
    768  1.85.8.2  rmind 	memcpy(mtod(m, void *), asa, asa->sa_len);
    769  1.85.8.2  rmind 	if (n)
    770  1.85.8.2  rmind 		n->m_next = m0;		/* concatenate data to control */
    771  1.85.8.2  rmind 	else
    772  1.85.8.2  rmind 		control = m0;
    773  1.85.8.2  rmind 	m->m_next = control;
    774  1.85.8.2  rmind 
    775  1.85.8.2  rmind 	SBLASTRECORDCHK(sb, "sbappendaddr 1");
    776  1.85.8.2  rmind 
    777  1.85.8.2  rmind 	for (n = m; n->m_next != NULL; n = n->m_next)
    778  1.85.8.2  rmind 		sballoc(sb, n);
    779  1.85.8.2  rmind 	sballoc(sb, n);
    780  1.85.8.2  rmind 	nlast = n;
    781  1.85.8.2  rmind 	SBLINKRECORD(sb, m);
    782  1.85.8.2  rmind 
    783  1.85.8.2  rmind 	sb->sb_mbtail = nlast;
    784  1.85.8.2  rmind 	SBLASTMBUFCHK(sb, "sbappendaddr");
    785  1.85.8.2  rmind 
    786  1.85.8.2  rmind 	SBLASTRECORDCHK(sb, "sbappendaddr 2");
    787  1.85.8.2  rmind 
    788  1.85.8.2  rmind 	return (1);
    789  1.85.8.2  rmind }
    790  1.85.8.2  rmind 
    791  1.85.8.2  rmind /*
    792  1.85.8.2  rmind  * Helper for sbappendchainaddr: prepend a struct sockaddr* to
    793  1.85.8.2  rmind  * an mbuf chain.
    794  1.85.8.2  rmind  */
    795  1.85.8.2  rmind static inline struct mbuf *
    796  1.85.8.2  rmind m_prepend_sockaddr(struct sockbuf *sb, struct mbuf *m0,
    797  1.85.8.2  rmind 		   const struct sockaddr *asa)
    798  1.85.8.2  rmind {
    799  1.85.8.2  rmind 	struct mbuf *m;
    800  1.85.8.2  rmind 	const int salen = asa->sa_len;
    801  1.85.8.2  rmind 
    802  1.85.8.2  rmind 	/* only the first in each chain need be a pkthdr */
    803  1.85.8.2  rmind 	MGETHDR(m, M_DONTWAIT, MT_SONAME);
    804  1.85.8.2  rmind 	if (m == 0)
    805  1.85.8.2  rmind 		return (0);
    806  1.85.8.2  rmind 	MCLAIM(m, sb->sb_mowner);
    807  1.85.8.2  rmind #ifdef notyet
    808  1.85.8.2  rmind 	if (salen > MHLEN) {
    809  1.85.8.2  rmind 		MEXTMALLOC(m, salen, M_NOWAIT);
    810  1.85.8.2  rmind 		if ((m->m_flags & M_EXT) == 0) {
    811  1.85.8.2  rmind 			m_free(m);
    812  1.85.8.2  rmind 			return (0);
    813  1.85.8.2  rmind 		}
    814  1.85.8.2  rmind 	}
    815  1.85.8.2  rmind #else
    816  1.85.8.2  rmind 	KASSERT(salen <= MHLEN);
    817  1.85.8.2  rmind #endif
    818  1.85.8.2  rmind 	m->m_len = salen;
    819  1.85.8.2  rmind 	memcpy(mtod(m, void *), asa, salen);
    820  1.85.8.2  rmind 	m->m_next = m0;
    821  1.85.8.2  rmind 	m->m_pkthdr.len = salen + m0->m_pkthdr.len;
    822  1.85.8.2  rmind 
    823  1.85.8.2  rmind 	return m;
    824  1.85.8.2  rmind }
    825  1.85.8.2  rmind 
    826  1.85.8.2  rmind int
    827  1.85.8.2  rmind sbappendaddrchain(struct sockbuf *sb, const struct sockaddr *asa,
    828  1.85.8.2  rmind 		  struct mbuf *m0, int sbprio)
    829  1.85.8.2  rmind {
    830  1.85.8.2  rmind 	int space;
    831  1.85.8.2  rmind 	struct mbuf *m, *n, *n0, *nlast;
    832  1.85.8.2  rmind 	int error;
    833  1.85.8.2  rmind 
    834  1.85.8.2  rmind 	/*
    835  1.85.8.2  rmind 	 * XXX sbprio reserved for encoding priority of this* request:
    836  1.85.8.2  rmind 	 *  SB_PRIO_NONE --> honour normal sb limits
    837  1.85.8.2  rmind 	 *  SB_PRIO_ONESHOT_OVERFLOW --> if socket has any space,
    838  1.85.8.2  rmind 	 *	take whole chain. Intended for large requests
    839  1.85.8.2  rmind 	 *      that should be delivered atomically (all, or none).
    840  1.85.8.2  rmind 	 * SB_PRIO_OVERDRAFT -- allow a small (2*MLEN) overflow
    841  1.85.8.2  rmind 	 *       over normal socket limits, for messages indicating
    842  1.85.8.2  rmind 	 *       buffer overflow in earlier normal/lower-priority messages
    843  1.85.8.2  rmind 	 * SB_PRIO_BESTEFFORT -->  ignore limits entirely.
    844  1.85.8.2  rmind 	 *       Intended for  kernel-generated messages only.
    845  1.85.8.2  rmind 	 *        Up to generator to avoid total mbuf resource exhaustion.
    846  1.85.8.2  rmind 	 */
    847  1.85.8.2  rmind 	(void)sbprio;
    848  1.85.8.2  rmind 
    849  1.85.8.2  rmind 	if (m0 && (m0->m_flags & M_PKTHDR) == 0)
    850  1.85.8.2  rmind 		panic("sbappendaddrchain");
    851  1.85.8.2  rmind 
    852  1.85.8.2  rmind 	space = sbspace(sb);
    853  1.85.8.2  rmind 
    854  1.85.8.2  rmind #ifdef notyet
    855  1.85.8.2  rmind 	/*
    856  1.85.8.2  rmind 	 * Enforce SB_PRIO_* limits as described above.
    857  1.85.8.2  rmind 	 */
    858  1.85.8.2  rmind #endif
    859  1.85.8.2  rmind 
    860  1.85.8.2  rmind 	n0 = NULL;
    861  1.85.8.2  rmind 	nlast = NULL;
    862  1.85.8.2  rmind 	for (m = m0; m; m = m->m_nextpkt) {
    863  1.85.8.2  rmind 		struct mbuf *np;
    864  1.85.8.2  rmind 
    865  1.85.8.2  rmind #ifdef MBUFTRACE
    866  1.85.8.2  rmind 		m_claimm(m, sb->sb_mowner);
    867  1.85.8.2  rmind #endif
    868  1.85.8.2  rmind 
    869  1.85.8.2  rmind 		/* Prepend sockaddr to this record (m) of input chain m0 */
    870  1.85.8.2  rmind 	  	n = m_prepend_sockaddr(sb, m, asa);
    871  1.85.8.2  rmind 		if (n == NULL) {
    872  1.85.8.2  rmind 			error = ENOBUFS;
    873  1.85.8.2  rmind 			goto bad;
    874  1.85.8.2  rmind 		}
    875  1.85.8.2  rmind 
    876  1.85.8.2  rmind 		/* Append record (asa+m) to end of new chain n0 */
    877  1.85.8.2  rmind 		if (n0 == NULL) {
    878  1.85.8.2  rmind 			n0 = n;
    879  1.85.8.2  rmind 		} else {
    880  1.85.8.2  rmind 			nlast->m_nextpkt = n;
    881  1.85.8.2  rmind 		}
    882  1.85.8.2  rmind 		/* Keep track of last record on new chain */
    883  1.85.8.2  rmind 		nlast = n;
    884  1.85.8.2  rmind 
    885  1.85.8.2  rmind 		for (np = n; np; np = np->m_next)
    886  1.85.8.2  rmind 			sballoc(sb, np);
    887  1.85.8.2  rmind 	}
    888  1.85.8.2  rmind 
    889  1.85.8.2  rmind 	SBLASTRECORDCHK(sb, "sbappendaddrchain 1");
    890  1.85.8.2  rmind 
    891  1.85.8.2  rmind 	/* Drop the entire chain of (asa+m) records onto the socket */
    892  1.85.8.2  rmind 	SBLINKRECORDCHAIN(sb, n0, nlast);
    893  1.85.8.2  rmind 
    894  1.85.8.2  rmind 	SBLASTRECORDCHK(sb, "sbappendaddrchain 2");
    895  1.85.8.2  rmind 
    896  1.85.8.2  rmind 	for (m = nlast; m->m_next; m = m->m_next)
    897  1.85.8.2  rmind 		;
    898  1.85.8.2  rmind 	sb->sb_mbtail = m;
    899  1.85.8.2  rmind 	SBLASTMBUFCHK(sb, "sbappendaddrchain");
    900  1.85.8.2  rmind 
    901  1.85.8.2  rmind 	return (1);
    902  1.85.8.2  rmind 
    903  1.85.8.2  rmind bad:
    904  1.85.8.2  rmind 	/*
    905  1.85.8.2  rmind 	 * On error, free the prepended addreseses. For consistency
    906  1.85.8.2  rmind 	 * with sbappendaddr(), leave it to our caller to free
    907  1.85.8.2  rmind 	 * the input record chain passed to us as m0.
    908  1.85.8.2  rmind 	 */
    909  1.85.8.2  rmind 	while ((n = n0) != NULL) {
    910  1.85.8.2  rmind 	  	struct mbuf *np;
    911  1.85.8.2  rmind 
    912  1.85.8.2  rmind 		/* Undo the sballoc() of this record */
    913  1.85.8.2  rmind 		for (np = n; np; np = np->m_next)
    914  1.85.8.2  rmind 			sbfree(sb, np);
    915  1.85.8.2  rmind 
    916  1.85.8.2  rmind 		n0 = n->m_nextpkt;	/* iterate at next prepended address */
    917  1.85.8.2  rmind 		MFREE(n, np);		/* free prepended address (not data) */
    918  1.85.8.2  rmind 	}
    919  1.85.8.2  rmind 	return 0;
    920  1.85.8.2  rmind }
    921  1.85.8.2  rmind 
    922  1.85.8.2  rmind 
    923  1.85.8.2  rmind int
    924  1.85.8.2  rmind sbappendcontrol(struct sockbuf *sb, struct mbuf *m0, struct mbuf *control)
    925  1.85.8.2  rmind {
    926  1.85.8.2  rmind 	struct mbuf	*m, *mlast, *n;
    927  1.85.8.2  rmind 	int		space;
    928  1.85.8.2  rmind 
    929  1.85.8.2  rmind 	space = 0;
    930  1.85.8.2  rmind 	if (control == 0)
    931  1.85.8.2  rmind 		panic("sbappendcontrol");
    932  1.85.8.2  rmind 	for (m = control; ; m = m->m_next) {
    933  1.85.8.2  rmind 		space += m->m_len;
    934  1.85.8.2  rmind 		MCLAIM(m, sb->sb_mowner);
    935  1.85.8.2  rmind 		if (m->m_next == 0)
    936  1.85.8.2  rmind 			break;
    937  1.85.8.2  rmind 	}
    938  1.85.8.2  rmind 	n = m;			/* save pointer to last control buffer */
    939  1.85.8.2  rmind 	for (m = m0; m; m = m->m_next) {
    940  1.85.8.2  rmind 		MCLAIM(m, sb->sb_mowner);
    941  1.85.8.2  rmind 		space += m->m_len;
    942  1.85.8.2  rmind 	}
    943  1.85.8.2  rmind 	if (space > sbspace(sb))
    944  1.85.8.2  rmind 		return (0);
    945  1.85.8.2  rmind 	n->m_next = m0;			/* concatenate data to control */
    946  1.85.8.2  rmind 
    947  1.85.8.2  rmind 	SBLASTRECORDCHK(sb, "sbappendcontrol 1");
    948  1.85.8.2  rmind 
    949  1.85.8.2  rmind 	for (m = control; m->m_next != NULL; m = m->m_next)
    950  1.85.8.2  rmind 		sballoc(sb, m);
    951  1.85.8.2  rmind 	sballoc(sb, m);
    952  1.85.8.2  rmind 	mlast = m;
    953  1.85.8.2  rmind 	SBLINKRECORD(sb, control);
    954  1.85.8.2  rmind 
    955  1.85.8.2  rmind 	sb->sb_mbtail = mlast;
    956  1.85.8.2  rmind 	SBLASTMBUFCHK(sb, "sbappendcontrol");
    957  1.85.8.2  rmind 
    958  1.85.8.2  rmind 	SBLASTRECORDCHK(sb, "sbappendcontrol 2");
    959  1.85.8.2  rmind 
    960  1.85.8.2  rmind 	return (1);
    961  1.85.8.2  rmind }
    962  1.85.8.2  rmind 
    963  1.85.8.2  rmind /*
    964  1.85.8.2  rmind  * Compress mbuf chain m into the socket
    965  1.85.8.2  rmind  * buffer sb following mbuf n.  If n
    966  1.85.8.2  rmind  * is null, the buffer is presumed empty.
    967  1.85.8.2  rmind  */
    968  1.85.8.2  rmind void
    969  1.85.8.2  rmind sbcompress(struct sockbuf *sb, struct mbuf *m, struct mbuf *n)
    970  1.85.8.2  rmind {
    971  1.85.8.2  rmind 	int		eor;
    972  1.85.8.2  rmind 	struct mbuf	*o;
    973  1.85.8.2  rmind 
    974  1.85.8.2  rmind 	eor = 0;
    975  1.85.8.2  rmind 	while (m) {
    976  1.85.8.2  rmind 		eor |= m->m_flags & M_EOR;
    977  1.85.8.2  rmind 		if (m->m_len == 0 &&
    978  1.85.8.2  rmind 		    (eor == 0 ||
    979  1.85.8.2  rmind 		     (((o = m->m_next) || (o = n)) &&
    980  1.85.8.2  rmind 		      o->m_type == m->m_type))) {
    981  1.85.8.2  rmind 			if (sb->sb_lastrecord == m)
    982  1.85.8.2  rmind 				sb->sb_lastrecord = m->m_next;
    983  1.85.8.2  rmind 			m = m_free(m);
    984  1.85.8.2  rmind 			continue;
    985  1.85.8.2  rmind 		}
    986  1.85.8.2  rmind 		if (n && (n->m_flags & M_EOR) == 0 &&
    987  1.85.8.2  rmind 		    /* M_TRAILINGSPACE() checks buffer writeability */
    988  1.85.8.2  rmind 		    m->m_len <= MCLBYTES / 4 && /* XXX Don't copy too much */
    989  1.85.8.2  rmind 		    m->m_len <= M_TRAILINGSPACE(n) &&
    990  1.85.8.2  rmind 		    n->m_type == m->m_type) {
    991  1.85.8.2  rmind 			memcpy(mtod(n, char *) + n->m_len, mtod(m, void *),
    992  1.85.8.2  rmind 			    (unsigned)m->m_len);
    993  1.85.8.2  rmind 			n->m_len += m->m_len;
    994  1.85.8.2  rmind 			sb->sb_cc += m->m_len;
    995  1.85.8.2  rmind 			m = m_free(m);
    996  1.85.8.2  rmind 			continue;
    997  1.85.8.2  rmind 		}
    998  1.85.8.2  rmind 		if (n)
    999  1.85.8.2  rmind 			n->m_next = m;
   1000  1.85.8.2  rmind 		else
   1001  1.85.8.2  rmind 			sb->sb_mb = m;
   1002  1.85.8.2  rmind 		sb->sb_mbtail = m;
   1003  1.85.8.2  rmind 		sballoc(sb, m);
   1004  1.85.8.2  rmind 		n = m;
   1005  1.85.8.2  rmind 		m->m_flags &= ~M_EOR;
   1006  1.85.8.2  rmind 		m = m->m_next;
   1007  1.85.8.2  rmind 		n->m_next = 0;
   1008  1.85.8.2  rmind 	}
   1009  1.85.8.2  rmind 	if (eor) {
   1010  1.85.8.2  rmind 		if (n)
   1011  1.85.8.2  rmind 			n->m_flags |= eor;
   1012  1.85.8.2  rmind 		else
   1013  1.85.8.2  rmind 			printf("semi-panic: sbcompress\n");
   1014  1.85.8.2  rmind 	}
   1015  1.85.8.2  rmind 	SBLASTMBUFCHK(sb, __func__);
   1016  1.85.8.2  rmind }
   1017  1.85.8.2  rmind 
   1018  1.85.8.2  rmind /*
   1019  1.85.8.2  rmind  * Free all mbufs in a sockbuf.
   1020  1.85.8.2  rmind  * Check that all resources are reclaimed.
   1021  1.85.8.2  rmind  */
   1022  1.85.8.2  rmind void
   1023  1.85.8.2  rmind sbflush(struct sockbuf *sb)
   1024  1.85.8.2  rmind {
   1025  1.85.8.2  rmind 
   1026  1.85.8.2  rmind 	KASSERT((sb->sb_flags & SB_LOCK) == 0);
   1027  1.85.8.2  rmind 
   1028  1.85.8.2  rmind 	while (sb->sb_mbcnt)
   1029  1.85.8.2  rmind 		sbdrop(sb, (int)sb->sb_cc);
   1030  1.85.8.2  rmind 
   1031  1.85.8.2  rmind 	KASSERT(sb->sb_cc == 0);
   1032  1.85.8.2  rmind 	KASSERT(sb->sb_mb == NULL);
   1033  1.85.8.2  rmind 	KASSERT(sb->sb_mbtail == NULL);
   1034  1.85.8.2  rmind 	KASSERT(sb->sb_lastrecord == NULL);
   1035  1.85.8.2  rmind }
   1036  1.85.8.2  rmind 
   1037  1.85.8.2  rmind /*
   1038  1.85.8.2  rmind  * Drop data from (the front of) a sockbuf.
   1039  1.85.8.2  rmind  */
   1040  1.85.8.2  rmind void
   1041  1.85.8.2  rmind sbdrop(struct sockbuf *sb, int len)
   1042  1.85.8.2  rmind {
   1043  1.85.8.2  rmind 	struct mbuf	*m, *mn, *next;
   1044  1.85.8.2  rmind 
   1045  1.85.8.2  rmind 	next = (m = sb->sb_mb) ? m->m_nextpkt : 0;
   1046  1.85.8.2  rmind 	while (len > 0) {
   1047  1.85.8.2  rmind 		if (m == 0) {
   1048  1.85.8.2  rmind 			if (next == 0)
   1049  1.85.8.2  rmind 				panic("sbdrop");
   1050  1.85.8.2  rmind 			m = next;
   1051  1.85.8.2  rmind 			next = m->m_nextpkt;
   1052  1.85.8.2  rmind 			continue;
   1053  1.85.8.2  rmind 		}
   1054  1.85.8.2  rmind 		if (m->m_len > len) {
   1055  1.85.8.2  rmind 			m->m_len -= len;
   1056  1.85.8.2  rmind 			m->m_data += len;
   1057  1.85.8.2  rmind 			sb->sb_cc -= len;
   1058  1.85.8.2  rmind 			break;
   1059  1.85.8.2  rmind 		}
   1060  1.85.8.2  rmind 		len -= m->m_len;
   1061  1.85.8.2  rmind 		sbfree(sb, m);
   1062  1.85.8.2  rmind 		MFREE(m, mn);
   1063  1.85.8.2  rmind 		m = mn;
   1064  1.85.8.2  rmind 	}
   1065  1.85.8.2  rmind 	while (m && m->m_len == 0) {
   1066  1.85.8.2  rmind 		sbfree(sb, m);
   1067  1.85.8.2  rmind 		MFREE(m, mn);
   1068  1.85.8.2  rmind 		m = mn;
   1069  1.85.8.2  rmind 	}
   1070  1.85.8.2  rmind 	if (m) {
   1071  1.85.8.2  rmind 		sb->sb_mb = m;
   1072  1.85.8.2  rmind 		m->m_nextpkt = next;
   1073  1.85.8.2  rmind 	} else
   1074  1.85.8.2  rmind 		sb->sb_mb = next;
   1075  1.85.8.2  rmind 	/*
   1076  1.85.8.2  rmind 	 * First part is an inline SB_EMPTY_FIXUP().  Second part
   1077  1.85.8.2  rmind 	 * makes sure sb_lastrecord is up-to-date if we dropped
   1078  1.85.8.2  rmind 	 * part of the last record.
   1079  1.85.8.2  rmind 	 */
   1080  1.85.8.2  rmind 	m = sb->sb_mb;
   1081  1.85.8.2  rmind 	if (m == NULL) {
   1082  1.85.8.2  rmind 		sb->sb_mbtail = NULL;
   1083  1.85.8.2  rmind 		sb->sb_lastrecord = NULL;
   1084  1.85.8.2  rmind 	} else if (m->m_nextpkt == NULL)
   1085  1.85.8.2  rmind 		sb->sb_lastrecord = m;
   1086  1.85.8.2  rmind }
   1087  1.85.8.2  rmind 
   1088  1.85.8.2  rmind /*
   1089  1.85.8.2  rmind  * Drop a record off the front of a sockbuf
   1090  1.85.8.2  rmind  * and move the next record to the front.
   1091  1.85.8.2  rmind  */
   1092  1.85.8.2  rmind void
   1093  1.85.8.2  rmind sbdroprecord(struct sockbuf *sb)
   1094  1.85.8.2  rmind {
   1095  1.85.8.2  rmind 	struct mbuf	*m, *mn;
   1096  1.85.8.2  rmind 
   1097  1.85.8.2  rmind 	m = sb->sb_mb;
   1098  1.85.8.2  rmind 	if (m) {
   1099  1.85.8.2  rmind 		sb->sb_mb = m->m_nextpkt;
   1100  1.85.8.2  rmind 		do {
   1101  1.85.8.2  rmind 			sbfree(sb, m);
   1102  1.85.8.2  rmind 			MFREE(m, mn);
   1103  1.85.8.2  rmind 		} while ((m = mn) != NULL);
   1104  1.85.8.2  rmind 	}
   1105  1.85.8.2  rmind 	SB_EMPTY_FIXUP(sb);
   1106  1.85.8.2  rmind }
   1107  1.85.8.2  rmind 
   1108  1.85.8.2  rmind /*
   1109  1.85.8.2  rmind  * Create a "control" mbuf containing the specified data
   1110  1.85.8.2  rmind  * with the specified type for presentation on a socket buffer.
   1111  1.85.8.2  rmind  */
   1112  1.85.8.2  rmind struct mbuf *
   1113  1.85.8.2  rmind sbcreatecontrol(void *p, int size, int type, int level)
   1114  1.85.8.2  rmind {
   1115  1.85.8.2  rmind 	struct cmsghdr	*cp;
   1116  1.85.8.2  rmind 	struct mbuf	*m;
   1117  1.85.8.2  rmind 
   1118  1.85.8.2  rmind 	if (CMSG_SPACE(size) > MCLBYTES) {
   1119  1.85.8.2  rmind 		printf("sbcreatecontrol: message too large %d\n", size);
   1120  1.85.8.2  rmind 		return NULL;
   1121  1.85.8.2  rmind 	}
   1122  1.85.8.2  rmind 
   1123  1.85.8.2  rmind 	if ((m = m_get(M_DONTWAIT, MT_CONTROL)) == NULL)
   1124  1.85.8.2  rmind 		return ((struct mbuf *) NULL);
   1125  1.85.8.2  rmind 	if (CMSG_SPACE(size) > MLEN) {
   1126  1.85.8.2  rmind 		MCLGET(m, M_DONTWAIT);
   1127  1.85.8.2  rmind 		if ((m->m_flags & M_EXT) == 0) {
   1128  1.85.8.2  rmind 			m_free(m);
   1129  1.85.8.2  rmind 			return NULL;
   1130  1.85.8.2  rmind 		}
   1131  1.85.8.2  rmind 	}
   1132  1.85.8.2  rmind 	cp = mtod(m, struct cmsghdr *);
   1133  1.85.8.2  rmind 	memcpy(CMSG_DATA(cp), p, size);
   1134  1.85.8.2  rmind 	m->m_len = CMSG_SPACE(size);
   1135  1.85.8.2  rmind 	cp->cmsg_len = CMSG_LEN(size);
   1136  1.85.8.2  rmind 	cp->cmsg_level = level;
   1137  1.85.8.2  rmind 	cp->cmsg_type = type;
   1138  1.85.8.2  rmind 	return (m);
   1139  1.85.8.2  rmind }
   1140