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