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