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