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