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