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