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