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