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