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