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