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