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