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