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