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