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