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