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