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