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