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