uipc_socket2.c revision 1.41 1 /* $NetBSD: uipc_socket2.c,v 1.41 2001/08/05 08:25:39 enami 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 }
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 &&
656 /* M_TRAILINGSPACE() checks buffer writeability */
657 m->m_len <= MCLBYTES / 4 && /* XXX Don't copy too much */
658 m->m_len <= M_TRAILINGSPACE(n) &&
659 n->m_type == m->m_type) {
660 memcpy(mtod(n, caddr_t) + n->m_len, mtod(m, caddr_t),
661 (unsigned)m->m_len);
662 n->m_len += m->m_len;
663 sb->sb_cc += m->m_len;
664 m = m_free(m);
665 continue;
666 }
667 if (n)
668 n->m_next = m;
669 else
670 sb->sb_mb = m;
671 sballoc(sb, m);
672 n = m;
673 m->m_flags &= ~M_EOR;
674 m = m->m_next;
675 n->m_next = 0;
676 }
677 if (eor) {
678 if (n)
679 n->m_flags |= eor;
680 else
681 printf("semi-panic: sbcompress\n");
682 }
683 }
684
685 /*
686 * Free all mbufs in a sockbuf.
687 * Check that all resources are reclaimed.
688 */
689 void
690 sbflush(struct sockbuf *sb)
691 {
692
693 if (sb->sb_flags & SB_LOCK)
694 panic("sbflush");
695 while (sb->sb_mbcnt)
696 sbdrop(sb, (int)sb->sb_cc);
697 if (sb->sb_cc || sb->sb_mb)
698 panic("sbflush 2");
699 }
700
701 /*
702 * Drop data from (the front of) a sockbuf.
703 */
704 void
705 sbdrop(struct sockbuf *sb, int len)
706 {
707 struct mbuf *m, *mn, *next;
708
709 next = (m = sb->sb_mb) ? m->m_nextpkt : 0;
710 while (len > 0) {
711 if (m == 0) {
712 if (next == 0)
713 panic("sbdrop");
714 m = next;
715 next = m->m_nextpkt;
716 continue;
717 }
718 if (m->m_len > len) {
719 m->m_len -= len;
720 m->m_data += len;
721 sb->sb_cc -= len;
722 break;
723 }
724 len -= m->m_len;
725 sbfree(sb, m);
726 MFREE(m, mn);
727 m = mn;
728 }
729 while (m && m->m_len == 0) {
730 sbfree(sb, m);
731 MFREE(m, mn);
732 m = mn;
733 }
734 if (m) {
735 sb->sb_mb = m;
736 m->m_nextpkt = next;
737 } else
738 sb->sb_mb = next;
739 }
740
741 /*
742 * Drop a record off the front of a sockbuf
743 * and move the next record to the front.
744 */
745 void
746 sbdroprecord(struct sockbuf *sb)
747 {
748 struct mbuf *m, *mn;
749
750 m = sb->sb_mb;
751 if (m) {
752 sb->sb_mb = m->m_nextpkt;
753 do {
754 sbfree(sb, m);
755 MFREE(m, mn);
756 } while ((m = mn) != NULL);
757 }
758 }
759
760 /*
761 * Create a "control" mbuf containing the specified data
762 * with the specified type for presentation on a socket buffer.
763 */
764 struct mbuf *
765 sbcreatecontrol(caddr_t p, int size, int type, int level)
766 {
767 struct cmsghdr *cp;
768 struct mbuf *m;
769
770 if (CMSG_SPACE(size) > MCLBYTES) {
771 printf("sbcreatecontrol: message too large %d\n", size);
772 return NULL;
773 }
774
775 if ((m = m_get(M_DONTWAIT, MT_CONTROL)) == NULL)
776 return ((struct mbuf *) NULL);
777 if (CMSG_SPACE(size) > MLEN) {
778 MCLGET(m, M_DONTWAIT);
779 if ((m->m_flags & M_EXT) == 0) {
780 m_free(m);
781 return NULL;
782 }
783 }
784 cp = mtod(m, struct cmsghdr *);
785 memcpy(CMSG_DATA(cp), p, size);
786 m->m_len = CMSG_SPACE(size);
787 cp->cmsg_len = CMSG_LEN(size);
788 cp->cmsg_level = level;
789 cp->cmsg_type = type;
790 return (m);
791 }
792