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