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