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