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