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