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