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