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