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