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