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