Home | History | Annotate | Line # | Download | only in kern
uipc_socket2.c revision 1.28
      1 /*	$NetBSD: uipc_socket2.c,v 1.28 1999/03/23 10:45:37 lukem 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.2 (Berkeley) 2/14/95
     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 const char	netio[] = "netio";
     56 const char	netcon[] = "netcon";
     57 const 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|SS_ISDISCONNECTED);
    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 SO_ISCONFIRMING, or SO_ISCONNECTED.
    150  *
    151  * Currently, sonewconn() is defined as sonewconn1() in socketvar.h
    152  * to catch calls that are missing the (new) second parameter.
    153  */
    154 struct socket *
    155 sonewconn1(head, connstatus)
    156 	register struct socket *head;
    157 	int connstatus;
    158 {
    159 	register struct socket *so;
    160 	int soqueue = connstatus ? 1 : 0;
    161 
    162 	if (head->so_qlen + head->so_q0len > 3 * head->so_qlimit / 2)
    163 		return ((struct socket *)0);
    164 	so = pool_get(&socket_pool, PR_NOWAIT);
    165 	if (so == NULL)
    166 		return (NULL);
    167 	memset((caddr_t)so, 0, sizeof(*so));
    168 	so->so_type = head->so_type;
    169 	so->so_options = head->so_options &~ SO_ACCEPTCONN;
    170 	so->so_linger = head->so_linger;
    171 	so->so_state = head->so_state | SS_NOFDREF;
    172 	so->so_proto = head->so_proto;
    173 	so->so_timeo = head->so_timeo;
    174 	so->so_pgid = head->so_pgid;
    175 	so->so_send = head->so_send;
    176 	so->so_receive = head->so_receive;
    177 	so->so_uid = head->so_uid;
    178 	(void) soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat);
    179 	soqinsque(head, so, soqueue);
    180 	if ((*so->so_proto->pr_usrreq)(so, PRU_ATTACH,
    181 	    (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0,
    182 	    (struct proc *)0)) {
    183 		(void) soqremque(so, soqueue);
    184 		pool_put(&socket_pool, so);
    185 		return (NULL);
    186 	}
    187 	if (connstatus) {
    188 		sorwakeup(head);
    189 		wakeup((caddr_t)&head->so_timeo);
    190 		so->so_state |= connstatus;
    191 	}
    192 	return (so);
    193 }
    194 
    195 void
    196 soqinsque(head, so, q)
    197 	register struct socket *head, *so;
    198 	int q;
    199 {
    200 
    201 #ifdef DIAGNOSTIC
    202 	if (so->so_onq != NULL)
    203 		panic("soqinsque");
    204 #endif
    205 
    206 	so->so_head = head;
    207 	if (q == 0) {
    208 		head->so_q0len++;
    209 		so->so_onq = &head->so_q0;
    210 	} else {
    211 		head->so_qlen++;
    212 		so->so_onq = &head->so_q;
    213 	}
    214 	TAILQ_INSERT_TAIL(so->so_onq, so, so_qe);
    215 }
    216 
    217 int
    218 soqremque(so, q)
    219 	register struct socket *so;
    220 	int q;
    221 {
    222 	struct socket *head = so->so_head;
    223 
    224 	if (q == 0) {
    225 		if (so->so_onq != &head->so_q0)
    226 			return (0);
    227 		head->so_q0len--;
    228 	} else {
    229 		if (so->so_onq != &head->so_q)
    230 			return (0);
    231 		head->so_qlen--;
    232 	}
    233 	TAILQ_REMOVE(so->so_onq, so, so_qe);
    234 	so->so_onq = NULL;
    235 	so->so_head = NULL;
    236 	return (1);
    237 }
    238 
    239 /*
    240  * Socantsendmore indicates that no more data will be sent on the
    241  * socket; it would normally be applied to a socket when the user
    242  * informs the system that no more data is to be sent, by the protocol
    243  * code (in case PRU_SHUTDOWN).  Socantrcvmore indicates that no more data
    244  * will be received, and will normally be applied to the socket by a
    245  * protocol when it detects that the peer will send no more data.
    246  * Data queued for reading in the socket may yet be read.
    247  */
    248 
    249 void
    250 socantsendmore(so)
    251 	struct socket *so;
    252 {
    253 
    254 	so->so_state |= SS_CANTSENDMORE;
    255 	sowwakeup(so);
    256 }
    257 
    258 void
    259 socantrcvmore(so)
    260 	struct socket *so;
    261 {
    262 
    263 	so->so_state |= SS_CANTRCVMORE;
    264 	sorwakeup(so);
    265 }
    266 
    267 /*
    268  * Wait for data to arrive at/drain from a socket buffer.
    269  */
    270 int
    271 sbwait(sb)
    272 	struct sockbuf *sb;
    273 {
    274 
    275 	sb->sb_flags |= SB_WAIT;
    276 	return (tsleep((caddr_t)&sb->sb_cc,
    277 	    (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH, netio,
    278 	    sb->sb_timeo));
    279 }
    280 
    281 /*
    282  * Lock a sockbuf already known to be locked;
    283  * return any error returned from sleep (EINTR).
    284  */
    285 int
    286 sb_lock(sb)
    287 	register struct sockbuf *sb;
    288 {
    289 	int error;
    290 
    291 	while (sb->sb_flags & SB_LOCK) {
    292 		sb->sb_flags |= SB_WANT;
    293 		error = tsleep((caddr_t)&sb->sb_flags,
    294 			       (sb->sb_flags & SB_NOINTR) ?
    295 					PSOCK : PSOCK|PCATCH, netio, 0);
    296 		if (error)
    297 			return (error);
    298 	}
    299 	sb->sb_flags |= SB_LOCK;
    300 	return (0);
    301 }
    302 
    303 /*
    304  * Wakeup processes waiting on a socket buffer.
    305  * Do asynchronous notification via SIGIO
    306  * if the socket has the SS_ASYNC flag set.
    307  */
    308 void
    309 sowakeup(so, sb)
    310 	register struct socket *so;
    311 	register struct sockbuf *sb;
    312 {
    313 	struct proc *p;
    314 
    315 	selwakeup(&sb->sb_sel);
    316 	sb->sb_flags &= ~SB_SEL;
    317 	if (sb->sb_flags & SB_WAIT) {
    318 		sb->sb_flags &= ~SB_WAIT;
    319 		wakeup((caddr_t)&sb->sb_cc);
    320 	}
    321 	if (so->so_state & SS_ASYNC) {
    322 		if (so->so_pgid < 0)
    323 			gsignal(-so->so_pgid, SIGIO);
    324 		else if (so->so_pgid > 0 && (p = pfind(so->so_pgid)) != 0)
    325 			psignal(p, SIGIO);
    326 	}
    327 	if (sb->sb_flags & SB_UPCALL)
    328 		(*so->so_upcall)(so, so->so_upcallarg, M_DONTWAIT);
    329 }
    330 
    331 /*
    332  * Socket buffer (struct sockbuf) utility routines.
    333  *
    334  * Each socket contains two socket buffers: one for sending data and
    335  * one for receiving data.  Each buffer contains a queue of mbufs,
    336  * information about the number of mbufs and amount of data in the
    337  * queue, and other fields allowing poll() statements and notification
    338  * on data availability to be implemented.
    339  *
    340  * Data stored in a socket buffer is maintained as a list of records.
    341  * Each record is a list of mbufs chained together with the m_next
    342  * field.  Records are chained together with the m_nextpkt field. The upper
    343  * level routine soreceive() expects the following conventions to be
    344  * observed when placing information in the receive buffer:
    345  *
    346  * 1. If the protocol requires each message be preceded by the sender's
    347  *    name, then a record containing that name must be present before
    348  *    any associated data (mbuf's must be of type MT_SONAME).
    349  * 2. If the protocol supports the exchange of ``access rights'' (really
    350  *    just additional data associated with the message), and there are
    351  *    ``rights'' to be received, then a record containing this data
    352  *    should be present (mbuf's must be of type MT_CONTROL).
    353  * 3. If a name or rights record exists, then it must be followed by
    354  *    a data record, perhaps of zero length.
    355  *
    356  * Before using a new socket structure it is first necessary to reserve
    357  * buffer space to the socket, by calling sbreserve().  This should commit
    358  * some of the available buffer space in the system buffer pool for the
    359  * socket (currently, it does nothing but enforce limits).  The space
    360  * should be released by calling sbrelease() when the socket is destroyed.
    361  */
    362 
    363 int
    364 soreserve(so, sndcc, rcvcc)
    365 	register struct socket *so;
    366 	u_long sndcc, rcvcc;
    367 {
    368 
    369 	if (sbreserve(&so->so_snd, sndcc) == 0)
    370 		goto bad;
    371 	if (sbreserve(&so->so_rcv, rcvcc) == 0)
    372 		goto bad2;
    373 	if (so->so_rcv.sb_lowat == 0)
    374 		so->so_rcv.sb_lowat = 1;
    375 	if (so->so_snd.sb_lowat == 0)
    376 		so->so_snd.sb_lowat = MCLBYTES;
    377 	if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat)
    378 		so->so_snd.sb_lowat = so->so_snd.sb_hiwat;
    379 	return (0);
    380 bad2:
    381 	sbrelease(&so->so_snd);
    382 bad:
    383 	return (ENOBUFS);
    384 }
    385 
    386 /*
    387  * Allot mbufs to a sockbuf.
    388  * Attempt to scale mbmax so that mbcnt doesn't become limiting
    389  * if buffering efficiency is near the normal case.
    390  */
    391 int
    392 sbreserve(sb, cc)
    393 	struct sockbuf *sb;
    394 	u_long cc;
    395 {
    396 
    397 	if (cc == 0 || cc > sb_max * MCLBYTES / (MSIZE + MCLBYTES))
    398 		return (0);
    399 	sb->sb_hiwat = cc;
    400 	sb->sb_mbmax = min(cc * 2, sb_max);
    401 	if (sb->sb_lowat > sb->sb_hiwat)
    402 		sb->sb_lowat = sb->sb_hiwat;
    403 	return (1);
    404 }
    405 
    406 /*
    407  * Free mbufs held by a socket, and reserved mbuf space.
    408  */
    409 void
    410 sbrelease(sb)
    411 	struct sockbuf *sb;
    412 {
    413 
    414 	sbflush(sb);
    415 	sb->sb_hiwat = sb->sb_mbmax = 0;
    416 }
    417 
    418 /*
    419  * Routines to add and remove
    420  * data from an mbuf queue.
    421  *
    422  * The routines sbappend() or sbappendrecord() are normally called to
    423  * append new mbufs to a socket buffer, after checking that adequate
    424  * space is available, comparing the function sbspace() with the amount
    425  * of data to be added.  sbappendrecord() differs from sbappend() in
    426  * that data supplied is treated as the beginning of a new record.
    427  * To place a sender's address, optional access rights, and data in a
    428  * socket receive buffer, sbappendaddr() should be used.  To place
    429  * access rights and data in a socket receive buffer, sbappendrights()
    430  * should be used.  In either case, the new data begins a new record.
    431  * Note that unlike sbappend() and sbappendrecord(), these routines check
    432  * for the caller that there will be enough space to store the data.
    433  * Each fails if there is not enough space, or if it cannot find mbufs
    434  * to store additional information in.
    435  *
    436  * Reliable protocols may use the socket send buffer to hold data
    437  * awaiting acknowledgement.  Data is normally copied from a socket
    438  * send buffer in a protocol with m_copy for output to a peer,
    439  * and then removing the data from the socket buffer with sbdrop()
    440  * or sbdroprecord() when the data is acknowledged by the peer.
    441  */
    442 
    443 /*
    444  * Append mbuf chain m to the last record in the
    445  * socket buffer sb.  The additional space associated
    446  * the mbuf chain is recorded in sb.  Empty mbufs are
    447  * discarded and mbufs are compacted where possible.
    448  */
    449 void
    450 sbappend(sb, m)
    451 	struct sockbuf *sb;
    452 	struct mbuf *m;
    453 {
    454 	register struct mbuf *n;
    455 
    456 	if (m == 0)
    457 		return;
    458 	if ((n = sb->sb_mb) != NULL) {
    459 		while (n->m_nextpkt)
    460 			n = n->m_nextpkt;
    461 		do {
    462 			if (n->m_flags & M_EOR) {
    463 				sbappendrecord(sb, m); /* XXXXXX!!!! */
    464 				return;
    465 			}
    466 		} while (n->m_next && (n = n->m_next));
    467 	}
    468 	sbcompress(sb, m, n);
    469 }
    470 
    471 #ifdef SOCKBUF_DEBUG
    472 void
    473 sbcheck(sb)
    474 	register struct sockbuf *sb;
    475 {
    476 	register struct mbuf *m;
    477 	register int len = 0, mbcnt = 0;
    478 
    479 	for (m = sb->sb_mb; m; m = m->m_next) {
    480 		len += m->m_len;
    481 		mbcnt += MSIZE;
    482 		if (m->m_flags & M_EXT)
    483 			mbcnt += m->m_ext.ext_size;
    484 		if (m->m_nextpkt)
    485 			panic("sbcheck nextpkt");
    486 	}
    487 	if (len != sb->sb_cc || mbcnt != sb->sb_mbcnt) {
    488 		printf("cc %d != %d || mbcnt %d != %d\n", len, sb->sb_cc,
    489 		    mbcnt, sb->sb_mbcnt);
    490 		panic("sbcheck");
    491 	}
    492 }
    493 #endif
    494 
    495 /*
    496  * As above, except the mbuf chain
    497  * begins a new record.
    498  */
    499 void
    500 sbappendrecord(sb, m0)
    501 	register struct sockbuf *sb;
    502 	register struct mbuf *m0;
    503 {
    504 	register struct mbuf *m;
    505 
    506 	if (m0 == 0)
    507 		return;
    508 	if ((m = sb->sb_mb) != NULL)
    509 		while (m->m_nextpkt)
    510 			m = m->m_nextpkt;
    511 	/*
    512 	 * Put the first mbuf on the queue.
    513 	 * Note this permits zero length records.
    514 	 */
    515 	sballoc(sb, m0);
    516 	if (m)
    517 		m->m_nextpkt = m0;
    518 	else
    519 		sb->sb_mb = m0;
    520 	m = m0->m_next;
    521 	m0->m_next = 0;
    522 	if (m && (m0->m_flags & M_EOR)) {
    523 		m0->m_flags &= ~M_EOR;
    524 		m->m_flags |= M_EOR;
    525 	}
    526 	sbcompress(sb, m, m0);
    527 }
    528 
    529 /*
    530  * As above except that OOB data
    531  * is inserted at the beginning of the sockbuf,
    532  * but after any other OOB data.
    533  */
    534 void
    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) != NULL; 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) != NULL)
    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 int
    580 sbappendaddr(sb, asa, m0, control)
    581 	register struct sockbuf *sb;
    582 	struct sockaddr *asa;
    583 	struct mbuf *m0, *control;
    584 {
    585 	register struct mbuf *m, *n;
    586 	int space = asa->sa_len;
    587 
    588 if (m0 && (m0->m_flags & M_PKTHDR) == 0)
    589 panic("sbappendaddr");
    590 	if (m0)
    591 		space += m0->m_pkthdr.len;
    592 	for (n = control; n; n = n->m_next) {
    593 		space += n->m_len;
    594 		if (n->m_next == 0)	/* keep pointer to last control buf */
    595 			break;
    596 	}
    597 	if (space > sbspace(sb))
    598 		return (0);
    599 	MGET(m, M_DONTWAIT, MT_SONAME);
    600 	if (m == 0)
    601 		return (0);
    602 	if (asa->sa_len > MLEN) {
    603 		MEXTMALLOC(m, asa->sa_len, M_NOWAIT);
    604 		if ((m->m_flags & M_EXT) == 0) {
    605 			m_free(m);
    606 			return (0);
    607 		}
    608 	}
    609 	m->m_len = asa->sa_len;
    610 	memcpy(mtod(m, caddr_t), (caddr_t)asa, asa->sa_len);
    611 	if (n)
    612 		n->m_next = m0;		/* concatenate data to control */
    613 	else
    614 		control = m0;
    615 	m->m_next = control;
    616 	for (n = m; n; n = n->m_next)
    617 		sballoc(sb, n);
    618 	if ((n = sb->sb_mb) != NULL) {
    619 		while (n->m_nextpkt)
    620 			n = n->m_nextpkt;
    621 		n->m_nextpkt = m;
    622 	} else
    623 		sb->sb_mb = m;
    624 	return (1);
    625 }
    626 
    627 int
    628 sbappendcontrol(sb, m0, control)
    629 	struct sockbuf *sb;
    630 	struct mbuf *m0, *control;
    631 {
    632 	register struct mbuf *m, *n;
    633 	int space = 0;
    634 
    635 	if (control == 0)
    636 		panic("sbappendcontrol");
    637 	for (m = control; ; m = m->m_next) {
    638 		space += m->m_len;
    639 		if (m->m_next == 0)
    640 			break;
    641 	}
    642 	n = m;			/* save pointer to last control buffer */
    643 	for (m = m0; m; m = m->m_next)
    644 		space += m->m_len;
    645 	if (space > sbspace(sb))
    646 		return (0);
    647 	n->m_next = m0;			/* concatenate data to control */
    648 	for (m = control; m; m = m->m_next)
    649 		sballoc(sb, m);
    650 	if ((n = sb->sb_mb) != NULL) {
    651 		while (n->m_nextpkt)
    652 			n = n->m_nextpkt;
    653 		n->m_nextpkt = control;
    654 	} else
    655 		sb->sb_mb = control;
    656 	return (1);
    657 }
    658 
    659 /*
    660  * Compress mbuf chain m into the socket
    661  * buffer sb following mbuf n.  If n
    662  * is null, the buffer is presumed empty.
    663  */
    664 void
    665 sbcompress(sb, m, n)
    666 	register struct sockbuf *sb;
    667 	register struct mbuf *m, *n;
    668 {
    669 	register int eor = 0;
    670 	register struct mbuf *o;
    671 
    672 	while (m) {
    673 		eor |= m->m_flags & M_EOR;
    674 		if (m->m_len == 0 &&
    675 		    (eor == 0 ||
    676 		     (((o = m->m_next) || (o = n)) &&
    677 		      o->m_type == m->m_type))) {
    678 			m = m_free(m);
    679 			continue;
    680 		}
    681 		if (n && (n->m_flags & (M_EXT | M_EOR)) == 0 &&
    682 		    (n->m_data + n->m_len + m->m_len) < &n->m_dat[MLEN] &&
    683 		    n->m_type == m->m_type) {
    684 			memcpy(mtod(n, caddr_t) + n->m_len, mtod(m, caddr_t),
    685 			    (unsigned)m->m_len);
    686 			n->m_len += m->m_len;
    687 			sb->sb_cc += m->m_len;
    688 			m = m_free(m);
    689 			continue;
    690 		}
    691 		if (n)
    692 			n->m_next = m;
    693 		else
    694 			sb->sb_mb = m;
    695 		sballoc(sb, m);
    696 		n = m;
    697 		m->m_flags &= ~M_EOR;
    698 		m = m->m_next;
    699 		n->m_next = 0;
    700 	}
    701 	if (eor) {
    702 		if (n)
    703 			n->m_flags |= eor;
    704 		else
    705 			printf("semi-panic: sbcompress\n");
    706 	}
    707 }
    708 
    709 /*
    710  * Free all mbufs in a sockbuf.
    711  * Check that all resources are reclaimed.
    712  */
    713 void
    714 sbflush(sb)
    715 	register struct sockbuf *sb;
    716 {
    717 
    718 	if (sb->sb_flags & SB_LOCK)
    719 		panic("sbflush");
    720 	while (sb->sb_mbcnt)
    721 		sbdrop(sb, (int)sb->sb_cc);
    722 	if (sb->sb_cc || sb->sb_mb)
    723 		panic("sbflush 2");
    724 }
    725 
    726 /*
    727  * Drop data from (the front of) a sockbuf.
    728  */
    729 void
    730 sbdrop(sb, len)
    731 	register struct sockbuf *sb;
    732 	register int len;
    733 {
    734 	register struct mbuf *m, *mn;
    735 	struct mbuf *next;
    736 
    737 	next = (m = sb->sb_mb) ? m->m_nextpkt : 0;
    738 	while (len > 0) {
    739 		if (m == 0) {
    740 			if (next == 0)
    741 				panic("sbdrop");
    742 			m = next;
    743 			next = m->m_nextpkt;
    744 			continue;
    745 		}
    746 		if (m->m_len > len) {
    747 			m->m_len -= len;
    748 			m->m_data += len;
    749 			sb->sb_cc -= len;
    750 			break;
    751 		}
    752 		len -= m->m_len;
    753 		sbfree(sb, m);
    754 		MFREE(m, mn);
    755 		m = mn;
    756 	}
    757 	while (m && m->m_len == 0) {
    758 		sbfree(sb, m);
    759 		MFREE(m, mn);
    760 		m = mn;
    761 	}
    762 	if (m) {
    763 		sb->sb_mb = m;
    764 		m->m_nextpkt = next;
    765 	} else
    766 		sb->sb_mb = next;
    767 }
    768 
    769 /*
    770  * Drop a record off the front of a sockbuf
    771  * and move the next record to the front.
    772  */
    773 void
    774 sbdroprecord(sb)
    775 	register struct sockbuf *sb;
    776 {
    777 	register struct mbuf *m, *mn;
    778 
    779 	m = sb->sb_mb;
    780 	if (m) {
    781 		sb->sb_mb = m->m_nextpkt;
    782 		do {
    783 			sbfree(sb, m);
    784 			MFREE(m, mn);
    785 		} while ((m = mn) != NULL);
    786 	}
    787 }
    788 
    789 /*
    790  * Create a "control" mbuf containing the specified data
    791  * with the specified type for presentation on a socket buffer.
    792  */
    793 struct mbuf *
    794 sbcreatecontrol(p, size, type, level)
    795 	caddr_t p;
    796 	register int size;
    797 	int type, level;
    798 {
    799 	register struct cmsghdr *cp;
    800 	struct mbuf *m;
    801 
    802 	if ((m = m_get(M_DONTWAIT, MT_CONTROL)) == NULL)
    803 		return ((struct mbuf *) NULL);
    804 	cp = mtod(m, struct cmsghdr *);
    805 	memcpy(CMSG_DATA(cp), p, size);
    806 	size += sizeof(*cp);
    807 	m->m_len = size;
    808 	cp->cmsg_len = size;
    809 	cp->cmsg_level = level;
    810 	cp->cmsg_type = type;
    811 	return (m);
    812 }
    813