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