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