1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1982, 1986, 1988, 1990, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 #include "opt_kern_tls.h" 34 #include "opt_param.h" 35 36 #include <sys/param.h> 37 #include <sys/aio.h> /* for aio_swake proto */ 38 #include <sys/kernel.h> 39 #include <sys/ktls.h> 40 #include <sys/lock.h> 41 #include <sys/malloc.h> 42 #include <sys/mbuf.h> 43 #include <sys/msan.h> 44 #include <sys/mutex.h> 45 #include <sys/proc.h> 46 #include <sys/protosw.h> 47 #include <sys/resourcevar.h> 48 #include <sys/signalvar.h> 49 #include <sys/socket.h> 50 #include <sys/socketvar.h> 51 #include <sys/sx.h> 52 #include <sys/sysctl.h> 53 54 #include <netinet/in.h> 55 56 /* 57 * Function pointer set by the AIO routines so that the socket buffer code 58 * can call back into the AIO module if it is loaded. 59 */ 60 void (*aio_swake)(struct socket *, struct sockbuf *); 61 62 /* 63 * Primitive routines for operating on socket buffers 64 */ 65 66 #define BUF_MAX_ADJ(_sz) (((u_quad_t)(_sz)) * MCLBYTES / (MSIZE + MCLBYTES)) 67 68 u_long sb_max = SB_MAX; 69 u_long sb_max_adj = BUF_MAX_ADJ(SB_MAX); 70 71 static u_long sb_efficiency = 8; /* parameter for sbreserve() */ 72 73 #ifdef KERN_TLS 74 static void sbcompress_ktls_rx(struct sockbuf *sb, struct mbuf *m, 75 struct mbuf *n); 76 #endif 77 static struct mbuf *sbcut_internal(struct sockbuf *sb, int len); 78 static void sbflush_internal(struct sockbuf *sb); 79 80 /* 81 * Our own version of m_clrprotoflags(), that can preserve M_NOTREADY. 82 */ 83 static void 84 sbm_clrprotoflags(struct mbuf *m, int flags) 85 { 86 int mask; 87 88 mask = ~M_PROTOFLAGS; 89 if (flags & PRUS_NOTREADY) 90 mask |= M_NOTREADY; 91 while (m) { 92 m->m_flags &= mask; 93 m = m->m_next; 94 } 95 } 96 97 /* 98 * Compress M_NOTREADY mbufs after they have been readied by sbready(). 99 * 100 * sbcompress() skips M_NOTREADY mbufs since the data is not available to 101 * be copied at the time of sbcompress(). This function combines small 102 * mbufs similar to sbcompress() once mbufs are ready. 'm0' is the first 103 * mbuf sbready() marked ready, and 'end' is the first mbuf still not 104 * ready. 105 */ 106 static void 107 sbready_compress(struct sockbuf *sb, struct mbuf *m0, struct mbuf *end) 108 { 109 struct mbuf *m, *n; 110 int ext_size; 111 112 SOCKBUF_LOCK_ASSERT(sb); 113 114 if ((sb->sb_flags & SB_NOCOALESCE) != 0) 115 return; 116 117 for (m = m0; m != end; m = m->m_next) { 118 MPASS((m->m_flags & M_NOTREADY) == 0); 119 /* 120 * NB: In sbcompress(), 'n' is the last mbuf in the 121 * socket buffer and 'm' is the new mbuf being copied 122 * into the trailing space of 'n'. Here, the roles 123 * are reversed and 'n' is the next mbuf after 'm' 124 * that is being copied into the trailing space of 125 * 'm'. 126 */ 127 n = m->m_next; 128 #ifdef KERN_TLS 129 /* Try to coalesce adjacent ktls mbuf hdr/trailers. */ 130 if ((n != NULL) && (n != end) && (m->m_flags & M_EOR) == 0 && 131 (m->m_flags & M_EXTPG) && 132 (n->m_flags & M_EXTPG) && 133 !mbuf_has_tls_session(m) && 134 !mbuf_has_tls_session(n)) { 135 int hdr_len, trail_len; 136 137 hdr_len = n->m_epg_hdrlen; 138 trail_len = m->m_epg_trllen; 139 if (trail_len != 0 && hdr_len != 0 && 140 trail_len + hdr_len <= MBUF_PEXT_TRAIL_LEN) { 141 /* copy n's header to m's trailer */ 142 memcpy(&m->m_epg_trail[trail_len], 143 n->m_epg_hdr, hdr_len); 144 m->m_epg_trllen += hdr_len; 145 m->m_len += hdr_len; 146 n->m_epg_hdrlen = 0; 147 n->m_len -= hdr_len; 148 } 149 } 150 #endif 151 152 /* Compress small unmapped mbufs into plain mbufs. */ 153 if ((m->m_flags & M_EXTPG) && m->m_len <= MLEN && 154 !mbuf_has_tls_session(m)) { 155 ext_size = m->m_ext.ext_size; 156 if (mb_unmapped_compress(m) == 0) 157 sb->sb_mbcnt -= ext_size; 158 } 159 160 while ((n != NULL) && (n != end) && (m->m_flags & M_EOR) == 0 && 161 M_WRITABLE(m) && 162 (m->m_flags & M_EXTPG) == 0 && 163 !mbuf_has_tls_session(n) && 164 !mbuf_has_tls_session(m) && 165 n->m_len <= MCLBYTES / 4 && /* XXX: Don't copy too much */ 166 n->m_len <= M_TRAILINGSPACE(m) && 167 m->m_type == n->m_type) { 168 KASSERT(sb->sb_lastrecord != n, 169 ("%s: merging start of record (%p) into previous mbuf (%p)", 170 __func__, n, m)); 171 m_copydata(n, 0, n->m_len, mtodo(m, m->m_len)); 172 m->m_len += n->m_len; 173 m->m_next = n->m_next; 174 m->m_flags |= n->m_flags & M_EOR; 175 if (sb->sb_mbtail == n) 176 sb->sb_mbtail = m; 177 178 sb->sb_mbcnt -= MSIZE; 179 if (n->m_flags & M_EXT) 180 sb->sb_mbcnt -= n->m_ext.ext_size; 181 m_free(n); 182 n = m->m_next; 183 } 184 } 185 SBLASTRECORDCHK(sb); 186 SBLASTMBUFCHK(sb); 187 } 188 189 /* 190 * Mark ready "count" units of I/O starting with "m". Most mbufs 191 * count as a single unit of I/O except for M_EXTPG mbufs which 192 * are backed by multiple pages. 193 */ 194 int 195 sbready(struct sockbuf *sb, struct mbuf *m0, int count) 196 { 197 struct mbuf *m; 198 u_int blocker; 199 200 SOCKBUF_LOCK_ASSERT(sb); 201 KASSERT(sb->sb_fnrdy != NULL, ("%s: sb %p NULL fnrdy", __func__, sb)); 202 KASSERT(count > 0, ("%s: invalid count %d", __func__, count)); 203 204 m = m0; 205 blocker = (sb->sb_fnrdy == m) ? M_BLOCKED : 0; 206 207 while (count > 0) { 208 KASSERT(m->m_flags & M_NOTREADY, 209 ("%s: m %p !M_NOTREADY", __func__, m)); 210 if ((m->m_flags & M_EXTPG) != 0 && m->m_epg_npgs != 0) { 211 if (count < m->m_epg_nrdy) { 212 m->m_epg_nrdy -= count; 213 count = 0; 214 break; 215 } 216 count -= m->m_epg_nrdy; 217 m->m_epg_nrdy = 0; 218 } else 219 count--; 220 221 m->m_flags &= ~(M_NOTREADY | blocker); 222 if (blocker) 223 sb->sb_acc += m->m_len; 224 m = m->m_next; 225 } 226 227 /* 228 * If the first mbuf is still not fully ready because only 229 * some of its backing pages were readied, no further progress 230 * can be made. 231 */ 232 if (m0 == m) { 233 MPASS(m->m_flags & M_NOTREADY); 234 return (EINPROGRESS); 235 } 236 237 if (!blocker) { 238 sbready_compress(sb, m0, m); 239 return (EINPROGRESS); 240 } 241 242 /* This one was blocking all the queue. */ 243 for (; m && (m->m_flags & M_NOTREADY) == 0; m = m->m_next) { 244 KASSERT(m->m_flags & M_BLOCKED, 245 ("%s: m %p !M_BLOCKED", __func__, m)); 246 m->m_flags &= ~M_BLOCKED; 247 sb->sb_acc += m->m_len; 248 } 249 250 sb->sb_fnrdy = m; 251 sbready_compress(sb, m0, m); 252 253 return (0); 254 } 255 256 /* 257 * Adjust sockbuf state reflecting allocation of m. 258 */ 259 void 260 sballoc(struct sockbuf *sb, struct mbuf *m) 261 { 262 263 SOCKBUF_LOCK_ASSERT(sb); 264 265 sb->sb_ccc += m->m_len; 266 267 if (sb->sb_fnrdy == NULL) { 268 if (m->m_flags & M_NOTREADY) 269 sb->sb_fnrdy = m; 270 else 271 sb->sb_acc += m->m_len; 272 } else 273 m->m_flags |= M_BLOCKED; 274 275 if (m->m_type != MT_DATA && m->m_type != MT_OOBDATA) 276 sb->sb_ctl += m->m_len; 277 278 sb->sb_mbcnt += MSIZE; 279 280 if (m->m_flags & M_EXT) 281 sb->sb_mbcnt += m->m_ext.ext_size; 282 } 283 284 /* 285 * Adjust sockbuf state reflecting freeing of m. 286 */ 287 void 288 sbfree(struct sockbuf *sb, struct mbuf *m) 289 { 290 291 #if 0 /* XXX: not yet: soclose() call path comes here w/o lock. */ 292 SOCKBUF_LOCK_ASSERT(sb); 293 #endif 294 295 sb->sb_ccc -= m->m_len; 296 297 if (!(m->m_flags & M_NOTAVAIL)) 298 sb->sb_acc -= m->m_len; 299 300 if (m == sb->sb_fnrdy) { 301 struct mbuf *n; 302 303 KASSERT(m->m_flags & M_NOTREADY, 304 ("%s: m %p !M_NOTREADY", __func__, m)); 305 306 n = m->m_next; 307 while (n != NULL && !(n->m_flags & M_NOTREADY)) { 308 n->m_flags &= ~M_BLOCKED; 309 sb->sb_acc += n->m_len; 310 n = n->m_next; 311 } 312 sb->sb_fnrdy = n; 313 } 314 315 if (m->m_type != MT_DATA && m->m_type != MT_OOBDATA) 316 sb->sb_ctl -= m->m_len; 317 318 sb->sb_mbcnt -= MSIZE; 319 if (m->m_flags & M_EXT) 320 sb->sb_mbcnt -= m->m_ext.ext_size; 321 322 if (sb->sb_sndptr == m) { 323 sb->sb_sndptr = NULL; 324 sb->sb_sndptroff = 0; 325 } 326 if (sb->sb_sndptroff != 0) 327 sb->sb_sndptroff -= m->m_len; 328 } 329 330 #ifdef KERN_TLS 331 /* 332 * Similar to sballoc/sbfree but does not adjust state associated with 333 * the sb_mb chain such as sb_fnrdy or sb_sndptr*. Also assumes mbufs 334 * are not ready. 335 */ 336 void 337 sballoc_ktls_rx(struct sockbuf *sb, struct mbuf *m) 338 { 339 340 SOCKBUF_LOCK_ASSERT(sb); 341 342 sb->sb_ccc += m->m_len; 343 sb->sb_tlscc += m->m_len; 344 345 sb->sb_mbcnt += MSIZE; 346 347 if (m->m_flags & M_EXT) 348 sb->sb_mbcnt += m->m_ext.ext_size; 349 } 350 351 void 352 sbfree_ktls_rx(struct sockbuf *sb, struct mbuf *m) 353 { 354 355 #if 0 /* XXX: not yet: soclose() call path comes here w/o lock. */ 356 SOCKBUF_LOCK_ASSERT(sb); 357 #endif 358 359 sb->sb_ccc -= m->m_len; 360 sb->sb_tlscc -= m->m_len; 361 362 sb->sb_mbcnt -= MSIZE; 363 364 if (m->m_flags & M_EXT) 365 sb->sb_mbcnt -= m->m_ext.ext_size; 366 } 367 #endif 368 369 /* 370 * Socantsendmore indicates that no more data will be sent on the socket; it 371 * would normally be applied to a socket when the user informs the system 372 * that no more data is to be sent, by the protocol code (in case 373 * PRU_SHUTDOWN). Socantrcvmore indicates that no more data will be 374 * received, and will normally be applied to the socket by a protocol when it 375 * detects that the peer will send no more data. Data queued for reading in 376 * the socket may yet be read. 377 */ 378 void 379 socantsendmore_locked(struct socket *so) 380 { 381 382 SOCK_SENDBUF_LOCK_ASSERT(so); 383 384 so->so_snd.sb_state |= SBS_CANTSENDMORE; 385 sowwakeup_locked(so); 386 SOCK_SENDBUF_UNLOCK_ASSERT(so); 387 } 388 389 void 390 socantsendmore(struct socket *so) 391 { 392 393 SOCK_SENDBUF_LOCK(so); 394 socantsendmore_locked(so); 395 SOCK_SENDBUF_UNLOCK_ASSERT(so); 396 } 397 398 void 399 socantrcvmore_locked(struct socket *so) 400 { 401 402 SOCK_RECVBUF_LOCK_ASSERT(so); 403 404 so->so_rcv.sb_state |= SBS_CANTRCVMORE; 405 #ifdef KERN_TLS 406 if (so->so_rcv.sb_flags & SB_TLS_RX) 407 ktls_check_rx(&so->so_rcv); 408 #endif 409 sorwakeup_locked(so); 410 SOCK_RECVBUF_UNLOCK_ASSERT(so); 411 } 412 413 void 414 socantrcvmore(struct socket *so) 415 { 416 417 SOCK_RECVBUF_LOCK(so); 418 socantrcvmore_locked(so); 419 SOCK_RECVBUF_UNLOCK_ASSERT(so); 420 } 421 422 void 423 soroverflow_locked(struct socket *so) 424 { 425 426 SOCK_RECVBUF_LOCK_ASSERT(so); 427 428 if (so->so_options & SO_RERROR) { 429 so->so_rerror = ENOBUFS; 430 sorwakeup_locked(so); 431 } else 432 SOCK_RECVBUF_UNLOCK(so); 433 434 SOCK_RECVBUF_UNLOCK_ASSERT(so); 435 } 436 437 void 438 soroverflow(struct socket *so) 439 { 440 441 SOCK_RECVBUF_LOCK(so); 442 soroverflow_locked(so); 443 SOCK_RECVBUF_UNLOCK_ASSERT(so); 444 } 445 446 /* 447 * Wait for data to arrive at/drain from a socket buffer. 448 */ 449 int 450 sbwait(struct socket *so, sb_which which) 451 { 452 struct sockbuf *sb; 453 454 SOCK_BUF_LOCK_ASSERT(so, which); 455 456 sb = sobuf(so, which); 457 sb->sb_flags |= SB_WAIT; 458 return (msleep_sbt(&sb->sb_acc, soeventmtx(so, which), 459 PSOCK | PCATCH, "sbwait", sb->sb_timeo, 0, 0)); 460 } 461 462 /* 463 * Wakeup processes waiting on a socket buffer. Do asynchronous notification 464 * via SIGIO if the socket has the SS_ASYNC flag set. 465 * 466 * Called with the socket buffer lock held; will release the lock by the end 467 * of the function. This allows the caller to acquire the socket buffer lock 468 * while testing for the need for various sorts of wakeup and hold it through 469 * to the point where it's no longer required. We currently hold the lock 470 * through calls out to other subsystems (with the exception of kqueue), and 471 * then release it to avoid lock order issues. It's not clear that's 472 * correct. 473 */ 474 static __always_inline void 475 sowakeup(struct socket *so, const sb_which which) 476 { 477 struct sockbuf *sb; 478 int ret; 479 480 SOCK_BUF_LOCK_ASSERT(so, which); 481 482 sb = sobuf(so, which); 483 selwakeuppri(sb->sb_sel, PSOCK); 484 if (!SEL_WAITING(sb->sb_sel)) 485 sb->sb_flags &= ~SB_SEL; 486 if (sb->sb_flags & SB_WAIT) { 487 sb->sb_flags &= ~SB_WAIT; 488 wakeup(&sb->sb_acc); 489 } 490 KNOTE_LOCKED(&sb->sb_sel->si_note, 0); 491 if (sb->sb_upcall != NULL) { 492 ret = sb->sb_upcall(so, sb->sb_upcallarg, M_NOWAIT); 493 if (ret == SU_ISCONNECTED) { 494 KASSERT(sb == &so->so_rcv, 495 ("SO_SND upcall returned SU_ISCONNECTED")); 496 soupcall_clear(so, SO_RCV); 497 } 498 } else 499 ret = SU_OK; 500 if (sb->sb_flags & SB_AIO) 501 sowakeup_aio(so, which); 502 SOCK_BUF_UNLOCK(so, which); 503 if (ret == SU_ISCONNECTED) 504 soisconnected(so); 505 if ((so->so_state & SS_ASYNC) && so->so_sigio != NULL) 506 pgsigio(&so->so_sigio, SIGIO, 0); 507 SOCK_BUF_UNLOCK_ASSERT(so, which); 508 } 509 510 static void 511 splice_push(struct socket *so) 512 { 513 struct so_splice *sp; 514 515 SOCK_RECVBUF_LOCK_ASSERT(so); 516 517 sp = so->so_splice; 518 mtx_lock(&sp->mtx); 519 SOCK_RECVBUF_UNLOCK(so); 520 so_splice_dispatch(sp); 521 } 522 523 static void 524 splice_pull(struct socket *so) 525 { 526 struct so_splice *sp; 527 528 SOCK_SENDBUF_LOCK_ASSERT(so); 529 530 sp = so->so_splice_back; 531 mtx_lock(&sp->mtx); 532 SOCK_SENDBUF_UNLOCK(so); 533 so_splice_dispatch(sp); 534 } 535 536 /* 537 * Do we need to notify the other side when I/O is possible? 538 */ 539 static __always_inline bool 540 sb_notify(const struct sockbuf *sb) 541 { 542 return ((sb->sb_flags & (SB_WAIT | SB_SEL | SB_ASYNC | 543 SB_UPCALL | SB_AIO | SB_KNOTE)) != 0); 544 } 545 546 void 547 sorwakeup_locked(struct socket *so) 548 { 549 SOCK_RECVBUF_LOCK_ASSERT(so); 550 if (so->so_rcv.sb_flags & SB_SPLICED) 551 splice_push(so); 552 else if (sb_notify(&so->so_rcv)) 553 sowakeup(so, SO_RCV); 554 else 555 SOCK_RECVBUF_UNLOCK(so); 556 } 557 558 void 559 sowwakeup_locked(struct socket *so) 560 { 561 SOCK_SENDBUF_LOCK_ASSERT(so); 562 if (so->so_snd.sb_flags & SB_SPLICED) 563 splice_pull(so); 564 else if (sb_notify(&so->so_snd)) 565 sowakeup(so, SO_SND); 566 else 567 SOCK_SENDBUF_UNLOCK(so); 568 } 569 570 /* 571 * Socket buffer (struct sockbuf) utility routines. 572 * 573 * Each socket contains two socket buffers: one for sending data and one for 574 * receiving data. Each buffer contains a queue of mbufs, information about 575 * the number of mbufs and amount of data in the queue, and other fields 576 * allowing select() statements and notification on data availability to be 577 * implemented. 578 * 579 * Data stored in a socket buffer is maintained as a list of records. Each 580 * record is a list of mbufs chained together with the m_next field. Records 581 * are chained together with the m_nextpkt field. The upper level routine 582 * soreceive() expects the following conventions to be observed when placing 583 * information in the receive buffer: 584 * 585 * 1. If the protocol requires each message be preceded by the sender's name, 586 * then a record containing that name must be present before any 587 * associated data (mbuf's must be of type MT_SONAME). 588 * 2. If the protocol supports the exchange of ``access rights'' (really just 589 * additional data associated with the message), and there are ``rights'' 590 * to be received, then a record containing this data should be present 591 * (mbuf's must be of type MT_RIGHTS). 592 * 3. If a name or rights record exists, then it must be followed by a data 593 * record, perhaps of zero length. 594 * 595 * Before using a new socket structure it is first necessary to reserve 596 * buffer space to the socket, by calling sbreserve(). This should commit 597 * some of the available buffer space in the system buffer pool for the 598 * socket (currently, it does nothing but enforce limits). The space should 599 * be released by calling sbrelease() when the socket is destroyed. 600 */ 601 int 602 soreserve(struct socket *so, u_long sndcc, u_long rcvcc) 603 { 604 struct thread *td = curthread; 605 606 SOCK_SENDBUF_LOCK(so); 607 SOCK_RECVBUF_LOCK(so); 608 if (sbreserve_locked(so, SO_SND, sndcc, td) == 0) 609 goto bad; 610 if (sbreserve_locked(so, SO_RCV, rcvcc, td) == 0) 611 goto bad2; 612 if (so->so_rcv.sb_lowat == 0) 613 so->so_rcv.sb_lowat = 1; 614 if (so->so_snd.sb_lowat == 0) 615 so->so_snd.sb_lowat = MCLBYTES; 616 if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat) 617 so->so_snd.sb_lowat = so->so_snd.sb_hiwat; 618 SOCK_RECVBUF_UNLOCK(so); 619 SOCK_SENDBUF_UNLOCK(so); 620 return (0); 621 bad2: 622 sbrelease_locked(so, SO_SND); 623 bad: 624 SOCK_RECVBUF_UNLOCK(so); 625 SOCK_SENDBUF_UNLOCK(so); 626 return (ENOBUFS); 627 } 628 629 static int 630 sysctl_handle_sb_max(SYSCTL_HANDLER_ARGS) 631 { 632 int error = 0; 633 u_long tmp_sb_max = sb_max; 634 635 error = sysctl_handle_long(oidp, &tmp_sb_max, arg2, req); 636 if (error || !req->newptr) 637 return (error); 638 if (tmp_sb_max < MSIZE + MCLBYTES) 639 return (EINVAL); 640 sb_max = tmp_sb_max; 641 sb_max_adj = BUF_MAX_ADJ(sb_max); 642 return (0); 643 } 644 645 /* 646 * Allot mbufs to a sockbuf. Attempt to scale mbmax so that mbcnt doesn't 647 * become limiting if buffering efficiency is near the normal case. 648 */ 649 bool 650 sbreserve_locked_limit(struct socket *so, sb_which which, u_long cc, 651 u_long buf_max, struct thread *td) 652 { 653 struct sockbuf *sb = sobuf(so, which); 654 rlim_t sbsize_limit; 655 656 SOCK_BUF_LOCK_ASSERT(so, which); 657 658 /* 659 * When a thread is passed, we take into account the thread's socket 660 * buffer size limit. The caller will generally pass curthread, but 661 * in the TCP input path, NULL will be passed to indicate that no 662 * appropriate thread resource limits are available. In that case, 663 * we don't apply a process limit. 664 */ 665 if (cc > BUF_MAX_ADJ(buf_max)) 666 return (false); 667 if (td != NULL) { 668 sbsize_limit = lim_cur(td, RLIMIT_SBSIZE); 669 } else 670 sbsize_limit = RLIM_INFINITY; 671 if (!chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, cc, 672 sbsize_limit)) 673 return (false); 674 sb->sb_mbmax = min(cc * sb_efficiency, buf_max); 675 if (sb->sb_lowat > sb->sb_hiwat) 676 sb->sb_lowat = sb->sb_hiwat; 677 return (true); 678 } 679 680 bool 681 sbreserve_locked(struct socket *so, sb_which which, u_long cc, 682 struct thread *td) 683 { 684 return (sbreserve_locked_limit(so, which, cc, sb_max, td)); 685 } 686 687 int 688 sbsetopt(struct socket *so, struct sockopt *sopt) 689 { 690 struct sockbuf *sb; 691 sb_which wh; 692 short *flags; 693 u_int cc, *hiwat, *lowat; 694 int error, optval; 695 696 error = sooptcopyin(sopt, &optval, sizeof optval, sizeof optval); 697 if (error != 0) 698 return (error); 699 700 /* 701 * Values < 1 make no sense for any of these options, 702 * so disallow them. 703 */ 704 if (optval < 1) 705 return (EINVAL); 706 cc = optval; 707 708 sb = NULL; 709 SOCK_LOCK(so); 710 if (SOLISTENING(so)) { 711 switch (sopt->sopt_name) { 712 case SO_SNDLOWAT: 713 case SO_SNDBUF: 714 lowat = &so->sol_sbsnd_lowat; 715 hiwat = &so->sol_sbsnd_hiwat; 716 flags = &so->sol_sbsnd_flags; 717 break; 718 case SO_RCVLOWAT: 719 case SO_RCVBUF: 720 lowat = &so->sol_sbrcv_lowat; 721 hiwat = &so->sol_sbrcv_hiwat; 722 flags = &so->sol_sbrcv_flags; 723 break; 724 } 725 } else { 726 switch (sopt->sopt_name) { 727 case SO_SNDLOWAT: 728 case SO_SNDBUF: 729 sb = &so->so_snd; 730 wh = SO_SND; 731 break; 732 case SO_RCVLOWAT: 733 case SO_RCVBUF: 734 sb = &so->so_rcv; 735 wh = SO_RCV; 736 break; 737 } 738 flags = &sb->sb_flags; 739 hiwat = &sb->sb_hiwat; 740 lowat = &sb->sb_lowat; 741 SOCK_BUF_LOCK(so, wh); 742 } 743 744 error = 0; 745 switch (sopt->sopt_name) { 746 case SO_SNDBUF: 747 case SO_RCVBUF: 748 if (SOLISTENING(so)) { 749 if (cc > sb_max_adj) { 750 error = ENOBUFS; 751 break; 752 } 753 *hiwat = cc; 754 if (*lowat > *hiwat) 755 *lowat = *hiwat; 756 } else { 757 if (!sbreserve_locked(so, wh, cc, curthread)) 758 error = ENOBUFS; 759 } 760 if (error == 0) 761 *flags &= ~SB_AUTOSIZE; 762 break; 763 case SO_SNDLOWAT: 764 case SO_RCVLOWAT: 765 /* 766 * Make sure the low-water is never greater than the 767 * high-water. 768 */ 769 *lowat = (cc > *hiwat) ? *hiwat : cc; 770 break; 771 } 772 773 if (!SOLISTENING(so)) 774 SOCK_BUF_UNLOCK(so, wh); 775 SOCK_UNLOCK(so); 776 return (error); 777 } 778 779 /* 780 * Free mbufs held by a socket, and reserved mbuf space. 781 */ 782 static void 783 sbrelease_internal(struct socket *so, sb_which which) 784 { 785 struct sockbuf *sb = sobuf(so, which); 786 787 sbflush_internal(sb); 788 (void)chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, 0, 789 RLIM_INFINITY); 790 sb->sb_mbmax = 0; 791 } 792 793 void 794 sbrelease_locked(struct socket *so, sb_which which) 795 { 796 797 SOCK_BUF_LOCK_ASSERT(so, which); 798 799 sbrelease_internal(so, which); 800 } 801 802 void 803 sbrelease(struct socket *so, sb_which which) 804 { 805 806 SOCK_BUF_LOCK(so, which); 807 sbrelease_locked(so, which); 808 SOCK_BUF_UNLOCK(so, which); 809 } 810 811 void 812 sbdestroy(struct socket *so, sb_which which) 813 { 814 #ifdef KERN_TLS 815 struct sockbuf *sb = sobuf(so, which); 816 817 if (sb->sb_tls_info != NULL) 818 ktls_free(sb->sb_tls_info); 819 sb->sb_tls_info = NULL; 820 #endif 821 sbrelease_internal(so, which); 822 } 823 824 /* 825 * Routines to add and remove data from an mbuf queue. 826 * 827 * The routines sbappend() or sbappendrecord() are normally called to append 828 * new mbufs to a socket buffer, after checking that adequate space is 829 * available, comparing the function sbspace() with the amount of data to be 830 * added. sbappendrecord() differs from sbappend() in that data supplied is 831 * treated as the beginning of a new record. To place a sender's address, 832 * optional access rights, and data in a socket receive buffer, 833 * sbappendaddr() should be used. To place access rights and data in a 834 * socket receive buffer, sbappendrights() should be used. In either case, 835 * the new data begins a new record. Note that unlike sbappend() and 836 * sbappendrecord(), these routines check for the caller that there will be 837 * enough space to store the data. Each fails if there is not enough space, 838 * or if it cannot find mbufs to store additional information in. 839 * 840 * Reliable protocols may use the socket send buffer to hold data awaiting 841 * acknowledgement. Data is normally copied from a socket send buffer in a 842 * protocol with m_copy for output to a peer, and then removing the data from 843 * the socket buffer with sbdrop() or sbdroprecord() when the data is 844 * acknowledged by the peer. 845 */ 846 #ifdef SOCKBUF_DEBUG 847 void 848 sblastrecordchk(struct sockbuf *sb, const char *file, int line) 849 { 850 struct mbuf *m = sb->sb_mb; 851 852 SOCKBUF_LOCK_ASSERT(sb); 853 854 while (m && m->m_nextpkt) 855 m = m->m_nextpkt; 856 857 if (m != sb->sb_lastrecord) { 858 printf("%s: sb_mb %p sb_lastrecord %p last %p\n", 859 __func__, sb->sb_mb, sb->sb_lastrecord, m); 860 printf("packet chain:\n"); 861 for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt) 862 printf("\t%p\n", m); 863 panic("%s from %s:%u", __func__, file, line); 864 } 865 } 866 867 void 868 sblastmbufchk(struct sockbuf *sb, const char *file, int line) 869 { 870 struct mbuf *m = sb->sb_mb; 871 struct mbuf *n; 872 873 SOCKBUF_LOCK_ASSERT(sb); 874 875 while (m && m->m_nextpkt) 876 m = m->m_nextpkt; 877 878 while (m && m->m_next) 879 m = m->m_next; 880 881 if (m != sb->sb_mbtail) { 882 printf("%s: sb_mb %p sb_mbtail %p last %p\n", 883 __func__, sb->sb_mb, sb->sb_mbtail, m); 884 printf("packet tree:\n"); 885 for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt) { 886 printf("\t"); 887 for (n = m; n != NULL; n = n->m_next) 888 printf("%p ", n); 889 printf("\n"); 890 } 891 panic("%s from %s:%u", __func__, file, line); 892 } 893 894 #ifdef KERN_TLS 895 m = sb->sb_mtls; 896 while (m && m->m_next) 897 m = m->m_next; 898 899 if (m != sb->sb_mtlstail) { 900 printf("%s: sb_mtls %p sb_mtlstail %p last %p\n", 901 __func__, sb->sb_mtls, sb->sb_mtlstail, m); 902 printf("TLS packet tree:\n"); 903 printf("\t"); 904 for (m = sb->sb_mtls; m != NULL; m = m->m_next) { 905 printf("%p ", m); 906 } 907 printf("\n"); 908 panic("%s from %s:%u", __func__, file, line); 909 } 910 #endif 911 } 912 #endif /* SOCKBUF_DEBUG */ 913 914 #define SBLINKRECORD(sb, m0) do { \ 915 SOCKBUF_LOCK_ASSERT(sb); \ 916 if ((sb)->sb_lastrecord != NULL) \ 917 (sb)->sb_lastrecord->m_nextpkt = (m0); \ 918 else \ 919 (sb)->sb_mb = (m0); \ 920 (sb)->sb_lastrecord = (m0); \ 921 } while (/*CONSTCOND*/0) 922 923 /* 924 * Append mbuf chain m to the last record in the socket buffer sb. The 925 * additional space associated the mbuf chain is recorded in sb. Empty mbufs 926 * are discarded and mbufs are compacted where possible. 927 */ 928 void 929 sbappend_locked(struct sockbuf *sb, struct mbuf *m, int flags) 930 { 931 struct mbuf *n; 932 933 SOCKBUF_LOCK_ASSERT(sb); 934 935 if (m == NULL) 936 return; 937 kmsan_check_mbuf(m, "sbappend"); 938 sbm_clrprotoflags(m, flags); 939 SBLASTRECORDCHK(sb); 940 n = sb->sb_mb; 941 if (n) { 942 while (n->m_nextpkt) 943 n = n->m_nextpkt; 944 do { 945 if (n->m_flags & M_EOR) { 946 sbappendrecord_locked(sb, m); /* XXXXXX!!!! */ 947 return; 948 } 949 } while (n->m_next && (n = n->m_next)); 950 } else { 951 /* 952 * XXX Would like to simply use sb_mbtail here, but 953 * XXX I need to verify that I won't miss an EOR that 954 * XXX way. 955 */ 956 if ((n = sb->sb_lastrecord) != NULL) { 957 do { 958 if (n->m_flags & M_EOR) { 959 sbappendrecord_locked(sb, m); /* XXXXXX!!!! */ 960 return; 961 } 962 } while (n->m_next && (n = n->m_next)); 963 } else { 964 /* 965 * If this is the first record in the socket buffer, 966 * it's also the last record. 967 */ 968 sb->sb_lastrecord = m; 969 } 970 } 971 sbcompress(sb, m, n); 972 SBLASTRECORDCHK(sb); 973 } 974 975 /* 976 * Append mbuf chain m to the last record in the socket buffer sb. The 977 * additional space associated the mbuf chain is recorded in sb. Empty mbufs 978 * are discarded and mbufs are compacted where possible. 979 */ 980 void 981 sbappend(struct sockbuf *sb, struct mbuf *m, int flags) 982 { 983 984 SOCKBUF_LOCK(sb); 985 sbappend_locked(sb, m, flags); 986 SOCKBUF_UNLOCK(sb); 987 } 988 989 #ifdef KERN_TLS 990 /* 991 * Append an mbuf containing encrypted TLS data. The data 992 * is marked M_NOTREADY until it has been decrypted and 993 * stored as a TLS record. 994 */ 995 static void 996 sbappend_ktls_rx(struct sockbuf *sb, struct mbuf *m) 997 { 998 struct ifnet *ifp; 999 struct mbuf *n; 1000 int flags; 1001 1002 ifp = NULL; 1003 flags = M_NOTREADY; 1004 1005 SBLASTMBUFCHK(sb); 1006 1007 /* Mbuf chain must start with a packet header. */ 1008 MPASS((m->m_flags & M_PKTHDR) != 0); 1009 1010 /* Remove all packet headers and mbuf tags to get a pure data chain. */ 1011 for (n = m; n != NULL; n = n->m_next) { 1012 if (n->m_flags & M_PKTHDR) { 1013 ifp = m->m_pkthdr.leaf_rcvif; 1014 if ((n->m_pkthdr.csum_flags & CSUM_TLS_MASK) == 1015 CSUM_TLS_DECRYPTED) { 1016 /* Mark all mbufs in this packet decrypted. */ 1017 flags = M_NOTREADY | M_DECRYPTED; 1018 } else { 1019 flags = M_NOTREADY; 1020 } 1021 m_demote_pkthdr(n); 1022 } 1023 1024 n->m_flags &= M_DEMOTEFLAGS; 1025 n->m_flags |= flags; 1026 1027 MPASS((n->m_flags & M_NOTREADY) != 0); 1028 } 1029 1030 sbcompress_ktls_rx(sb, m, sb->sb_mtlstail); 1031 ktls_check_rx(sb); 1032 1033 /* Check for incoming packet route changes: */ 1034 if (ifp != NULL && sb->sb_tls_info->rx_ifp != NULL && 1035 sb->sb_tls_info->rx_ifp != ifp) 1036 ktls_input_ifp_mismatch(sb, ifp); 1037 } 1038 #endif 1039 1040 /* 1041 * This version of sbappend() should only be used when the caller absolutely 1042 * knows that there will never be more than one record in the socket buffer, 1043 * that is, a stream protocol (such as TCP). 1044 */ 1045 void 1046 sbappendstream_locked(struct sockbuf *sb, struct mbuf *m, int flags) 1047 { 1048 SOCKBUF_LOCK_ASSERT(sb); 1049 1050 KASSERT(m->m_nextpkt == NULL,("sbappendstream 0")); 1051 1052 kmsan_check_mbuf(m, "sbappend"); 1053 1054 #ifdef KERN_TLS 1055 /* 1056 * Decrypted TLS records are appended as records via 1057 * sbappendrecord(). TCP passes encrypted TLS records to this 1058 * function which must be scheduled for decryption. 1059 */ 1060 if (sb->sb_flags & SB_TLS_RX) { 1061 sbappend_ktls_rx(sb, m); 1062 return; 1063 } 1064 #endif 1065 1066 KASSERT(sb->sb_mb == sb->sb_lastrecord,("sbappendstream 1")); 1067 1068 SBLASTMBUFCHK(sb); 1069 1070 #ifdef KERN_TLS 1071 if (sb->sb_tls_info != NULL) 1072 ktls_seq(sb, m); 1073 #endif 1074 1075 /* Remove all packet headers and mbuf tags to get a pure data chain. */ 1076 m_demote(m, 1, flags & PRUS_NOTREADY ? M_NOTREADY : 0); 1077 1078 sbcompress(sb, m, sb->sb_mbtail); 1079 1080 sb->sb_lastrecord = sb->sb_mb; 1081 SBLASTRECORDCHK(sb); 1082 } 1083 1084 /* 1085 * This version of sbappend() should only be used when the caller absolutely 1086 * knows that there will never be more than one record in the socket buffer, 1087 * that is, a stream protocol (such as TCP). 1088 */ 1089 void 1090 sbappendstream(struct sockbuf *sb, struct mbuf *m, int flags) 1091 { 1092 1093 SOCKBUF_LOCK(sb); 1094 sbappendstream_locked(sb, m, flags); 1095 SOCKBUF_UNLOCK(sb); 1096 } 1097 1098 #ifdef SOCKBUF_DEBUG 1099 void 1100 sbcheck(struct sockbuf *sb, const char *file, int line) 1101 { 1102 struct mbuf *m, *n, *fnrdy; 1103 u_long acc, ccc, mbcnt; 1104 #ifdef KERN_TLS 1105 u_long tlscc; 1106 #endif 1107 1108 SOCKBUF_LOCK_ASSERT(sb); 1109 1110 acc = ccc = mbcnt = 0; 1111 fnrdy = NULL; 1112 1113 for (m = sb->sb_mb; m; m = n) { 1114 n = m->m_nextpkt; 1115 for (; m; m = m->m_next) { 1116 if (m->m_len == 0) { 1117 printf("sb %p empty mbuf %p\n", sb, m); 1118 goto fail; 1119 } 1120 if ((m->m_flags & M_NOTREADY) && fnrdy == NULL) { 1121 if (m != sb->sb_fnrdy) { 1122 printf("sb %p: fnrdy %p != m %p\n", 1123 sb, sb->sb_fnrdy, m); 1124 goto fail; 1125 } 1126 fnrdy = m; 1127 } 1128 if (fnrdy) { 1129 if (!(m->m_flags & M_NOTAVAIL)) { 1130 printf("sb %p: fnrdy %p, m %p is avail\n", 1131 sb, sb->sb_fnrdy, m); 1132 goto fail; 1133 } 1134 } else 1135 acc += m->m_len; 1136 ccc += m->m_len; 1137 mbcnt += MSIZE; 1138 if (m->m_flags & M_EXT) /*XXX*/ /* pretty sure this is bogus */ 1139 mbcnt += m->m_ext.ext_size; 1140 } 1141 } 1142 #ifdef KERN_TLS 1143 /* 1144 * Account for mbufs "detached" by ktls_detach_record() while 1145 * they are decrypted by ktls_decrypt(). tlsdcc gives a count 1146 * of the detached bytes that are included in ccc. The mbufs 1147 * and clusters are not included in the socket buffer 1148 * accounting. 1149 */ 1150 ccc += sb->sb_tlsdcc; 1151 1152 tlscc = 0; 1153 for (m = sb->sb_mtls; m; m = m->m_next) { 1154 if (m->m_nextpkt != NULL) { 1155 printf("sb %p TLS mbuf %p with nextpkt\n", sb, m); 1156 goto fail; 1157 } 1158 if ((m->m_flags & M_NOTREADY) == 0) { 1159 printf("sb %p TLS mbuf %p ready\n", sb, m); 1160 goto fail; 1161 } 1162 tlscc += m->m_len; 1163 ccc += m->m_len; 1164 mbcnt += MSIZE; 1165 if (m->m_flags & M_EXT) /*XXX*/ /* pretty sure this is bogus */ 1166 mbcnt += m->m_ext.ext_size; 1167 } 1168 1169 if (sb->sb_tlscc != tlscc) { 1170 printf("tlscc %ld/%u dcc %u\n", tlscc, sb->sb_tlscc, 1171 sb->sb_tlsdcc); 1172 goto fail; 1173 } 1174 #endif 1175 if (acc != sb->sb_acc || ccc != sb->sb_ccc || mbcnt != sb->sb_mbcnt) { 1176 printf("acc %ld/%u ccc %ld/%u mbcnt %ld/%u\n", 1177 acc, sb->sb_acc, ccc, sb->sb_ccc, mbcnt, sb->sb_mbcnt); 1178 #ifdef KERN_TLS 1179 printf("tlscc %ld/%u dcc %u\n", tlscc, sb->sb_tlscc, 1180 sb->sb_tlsdcc); 1181 #endif 1182 goto fail; 1183 } 1184 return; 1185 fail: 1186 panic("%s from %s:%u", __func__, file, line); 1187 } 1188 #endif 1189 1190 /* 1191 * As above, except the mbuf chain begins a new record. 1192 */ 1193 void 1194 sbappendrecord_locked(struct sockbuf *sb, struct mbuf *m0) 1195 { 1196 struct mbuf *m; 1197 1198 SOCKBUF_LOCK_ASSERT(sb); 1199 1200 if (m0 == NULL) 1201 return; 1202 1203 kmsan_check_mbuf(m0, "sbappend"); 1204 m_clrprotoflags(m0); 1205 1206 /* 1207 * Put the first mbuf on the queue. Note this permits zero length 1208 * records. 1209 */ 1210 sballoc(sb, m0); 1211 SBLASTRECORDCHK(sb); 1212 SBLINKRECORD(sb, m0); 1213 sb->sb_mbtail = m0; 1214 m = m0->m_next; 1215 m0->m_next = 0; 1216 if (m && (m0->m_flags & M_EOR)) { 1217 m0->m_flags &= ~M_EOR; 1218 m->m_flags |= M_EOR; 1219 } 1220 /* always call sbcompress() so it can do SBLASTMBUFCHK() */ 1221 sbcompress(sb, m, m0); 1222 } 1223 1224 /* 1225 * As above, except the mbuf chain begins a new record. 1226 */ 1227 void 1228 sbappendrecord(struct sockbuf *sb, struct mbuf *m0) 1229 { 1230 1231 SOCKBUF_LOCK(sb); 1232 sbappendrecord_locked(sb, m0); 1233 SOCKBUF_UNLOCK(sb); 1234 } 1235 1236 /* Helper routine that appends data, control, and address to a sockbuf. */ 1237 static int 1238 sbappendaddr_locked_internal(struct sockbuf *sb, const struct sockaddr *asa, 1239 struct mbuf *m0, struct mbuf *control, struct mbuf *ctrl_last) 1240 { 1241 struct mbuf *m, *n, *nlast; 1242 1243 if (m0 != NULL) 1244 kmsan_check_mbuf(m0, "sbappend"); 1245 if (control != NULL) 1246 kmsan_check_mbuf(control, "sbappend"); 1247 1248 #if MSIZE <= 256 1249 if (asa->sa_len > MLEN) 1250 return (0); 1251 #endif 1252 m = m_get(M_NOWAIT, MT_SONAME); 1253 if (m == NULL) 1254 return (0); 1255 m->m_len = asa->sa_len; 1256 bcopy(asa, mtod(m, caddr_t), asa->sa_len); 1257 if (m0) { 1258 M_ASSERT_NO_SND_TAG(m0); 1259 m_clrprotoflags(m0); 1260 m_tag_delete_chain(m0, NULL); 1261 /* 1262 * Clear some persistent info from pkthdr. 1263 * We don't use m_demote(), because some netgraph consumers 1264 * expect M_PKTHDR presence. 1265 */ 1266 m0->m_pkthdr.rcvif = NULL; 1267 m0->m_pkthdr.flowid = 0; 1268 m0->m_pkthdr.csum_flags = 0; 1269 m0->m_pkthdr.fibnum = 0; 1270 m0->m_pkthdr.rsstype = 0; 1271 } 1272 if (ctrl_last) 1273 ctrl_last->m_next = m0; /* concatenate data to control */ 1274 else 1275 control = m0; 1276 m->m_next = control; 1277 for (n = m; n->m_next != NULL; n = n->m_next) 1278 sballoc(sb, n); 1279 sballoc(sb, n); 1280 nlast = n; 1281 SBLINKRECORD(sb, m); 1282 1283 sb->sb_mbtail = nlast; 1284 SBLASTMBUFCHK(sb); 1285 1286 SBLASTRECORDCHK(sb); 1287 return (1); 1288 } 1289 1290 /* 1291 * Append address and data, and optionally, control (ancillary) data to the 1292 * receive queue of a socket. If present, m0 must include a packet header 1293 * with total length. Returns 0 if no space in sockbuf or insufficient 1294 * mbufs. 1295 */ 1296 int 1297 sbappendaddr_locked(struct sockbuf *sb, const struct sockaddr *asa, 1298 struct mbuf *m0, struct mbuf *control) 1299 { 1300 struct mbuf *ctrl_last; 1301 int space = asa->sa_len; 1302 1303 SOCKBUF_LOCK_ASSERT(sb); 1304 1305 if (m0 && (m0->m_flags & M_PKTHDR) == 0) 1306 panic("sbappendaddr_locked"); 1307 if (m0) 1308 space += m0->m_pkthdr.len; 1309 space += m_length(control, &ctrl_last); 1310 1311 if (space > sbspace(sb)) 1312 return (0); 1313 return (sbappendaddr_locked_internal(sb, asa, m0, control, ctrl_last)); 1314 } 1315 1316 /* 1317 * Append address and data, and optionally, control (ancillary) data to the 1318 * receive queue of a socket. If present, m0 must include a packet header 1319 * with total length. Returns 0 if insufficient mbufs. Does not validate space 1320 * on the receiving sockbuf. 1321 */ 1322 int 1323 sbappendaddr_nospacecheck_locked(struct sockbuf *sb, const struct sockaddr *asa, 1324 struct mbuf *m0, struct mbuf *control) 1325 { 1326 struct mbuf *ctrl_last; 1327 1328 SOCKBUF_LOCK_ASSERT(sb); 1329 1330 ctrl_last = (control == NULL) ? NULL : m_last(control); 1331 return (sbappendaddr_locked_internal(sb, asa, m0, control, ctrl_last)); 1332 } 1333 1334 /* 1335 * Append address and data, and optionally, control (ancillary) data to the 1336 * receive queue of a socket. If present, m0 must include a packet header 1337 * with total length. Returns 0 if no space in sockbuf or insufficient 1338 * mbufs. 1339 */ 1340 int 1341 sbappendaddr(struct sockbuf *sb, const struct sockaddr *asa, 1342 struct mbuf *m0, struct mbuf *control) 1343 { 1344 int retval; 1345 1346 SOCKBUF_LOCK(sb); 1347 retval = sbappendaddr_locked(sb, asa, m0, control); 1348 SOCKBUF_UNLOCK(sb); 1349 return (retval); 1350 } 1351 1352 void 1353 sbappendcontrol_locked(struct sockbuf *sb, struct mbuf *m0, 1354 struct mbuf *control, int flags) 1355 { 1356 struct mbuf *m, *mlast; 1357 1358 if (m0 != NULL) 1359 kmsan_check_mbuf(m0, "sbappend"); 1360 kmsan_check_mbuf(control, "sbappend"); 1361 1362 sbm_clrprotoflags(m0, flags); 1363 m_last(control)->m_next = m0; 1364 1365 SBLASTRECORDCHK(sb); 1366 1367 for (m = control; m->m_next; m = m->m_next) 1368 sballoc(sb, m); 1369 sballoc(sb, m); 1370 mlast = m; 1371 SBLINKRECORD(sb, control); 1372 1373 sb->sb_mbtail = mlast; 1374 SBLASTMBUFCHK(sb); 1375 1376 SBLASTRECORDCHK(sb); 1377 } 1378 1379 void 1380 sbappendcontrol(struct sockbuf *sb, struct mbuf *m0, struct mbuf *control, 1381 int flags) 1382 { 1383 1384 SOCKBUF_LOCK(sb); 1385 sbappendcontrol_locked(sb, m0, control, flags); 1386 SOCKBUF_UNLOCK(sb); 1387 } 1388 1389 /* 1390 * Append the data in mbuf chain (m) into the socket buffer sb following mbuf 1391 * (n). If (n) is NULL, the buffer is presumed empty. 1392 * 1393 * When the data is compressed, mbufs in the chain may be handled in one of 1394 * three ways: 1395 * 1396 * (1) The mbuf may simply be dropped, if it contributes nothing (no data, no 1397 * record boundary, and no change in data type). 1398 * 1399 * (2) The mbuf may be coalesced -- i.e., data in the mbuf may be copied into 1400 * an mbuf already in the socket buffer. This can occur if an 1401 * appropriate mbuf exists, there is room, both mbufs are not marked as 1402 * not ready, and no merging of data types will occur. 1403 * 1404 * (3) The mbuf may be appended to the end of the existing mbuf chain. 1405 * 1406 * If any of the new mbufs is marked as M_EOR, mark the last mbuf appended as 1407 * end-of-record. 1408 */ 1409 void 1410 sbcompress(struct sockbuf *sb, struct mbuf *m, struct mbuf *n) 1411 { 1412 int eor = 0; 1413 struct mbuf *o; 1414 1415 SOCKBUF_LOCK_ASSERT(sb); 1416 1417 while (m) { 1418 eor |= m->m_flags & M_EOR; 1419 if (m->m_len == 0 && 1420 (eor == 0 || 1421 (((o = m->m_next) || (o = n)) && 1422 o->m_type == m->m_type))) { 1423 if (sb->sb_lastrecord == m) 1424 sb->sb_lastrecord = m->m_next; 1425 m = m_free(m); 1426 continue; 1427 } 1428 if (n && (n->m_flags & M_EOR) == 0 && 1429 M_WRITABLE(n) && 1430 ((sb->sb_flags & SB_NOCOALESCE) == 0) && 1431 !(m->m_flags & M_NOTREADY) && 1432 !(n->m_flags & (M_NOTREADY | M_EXTPG)) && 1433 !mbuf_has_tls_session(m) && 1434 !mbuf_has_tls_session(n) && 1435 m->m_len <= MCLBYTES / 4 && /* XXX: Don't copy too much */ 1436 m->m_len <= M_TRAILINGSPACE(n) && 1437 n->m_type == m->m_type) { 1438 m_copydata(m, 0, m->m_len, mtodo(n, n->m_len)); 1439 n->m_len += m->m_len; 1440 sb->sb_ccc += m->m_len; 1441 if (sb->sb_fnrdy == NULL) 1442 sb->sb_acc += m->m_len; 1443 if (m->m_type != MT_DATA && m->m_type != MT_OOBDATA) 1444 /* XXX: Probably don't need.*/ 1445 sb->sb_ctl += m->m_len; 1446 m = m_free(m); 1447 continue; 1448 } 1449 if (m->m_len <= MLEN && (m->m_flags & M_EXTPG) && 1450 (m->m_flags & M_NOTREADY) == 0 && 1451 !mbuf_has_tls_session(m)) 1452 (void)mb_unmapped_compress(m); 1453 if (n) 1454 n->m_next = m; 1455 else 1456 sb->sb_mb = m; 1457 sb->sb_mbtail = m; 1458 sballoc(sb, m); 1459 n = m; 1460 m->m_flags &= ~M_EOR; 1461 m = m->m_next; 1462 n->m_next = 0; 1463 } 1464 if (eor) { 1465 KASSERT(n != NULL, ("sbcompress: eor && n == NULL")); 1466 n->m_flags |= eor; 1467 } 1468 SBLASTMBUFCHK(sb); 1469 } 1470 1471 #ifdef KERN_TLS 1472 /* 1473 * A version of sbcompress() for encrypted TLS RX mbufs. These mbufs 1474 * are appended to the 'sb_mtls' chain instead of 'sb_mb' and are also 1475 * a bit simpler (no EOR markers, always MT_DATA, etc.). 1476 */ 1477 static void 1478 sbcompress_ktls_rx(struct sockbuf *sb, struct mbuf *m, struct mbuf *n) 1479 { 1480 1481 SOCKBUF_LOCK_ASSERT(sb); 1482 1483 while (m) { 1484 KASSERT((m->m_flags & M_EOR) == 0, 1485 ("TLS RX mbuf %p with EOR", m)); 1486 KASSERT(m->m_type == MT_DATA, 1487 ("TLS RX mbuf %p is not MT_DATA", m)); 1488 KASSERT((m->m_flags & M_NOTREADY) != 0, 1489 ("TLS RX mbuf %p ready", m)); 1490 KASSERT((m->m_flags & M_EXTPG) == 0, 1491 ("TLS RX mbuf %p unmapped", m)); 1492 1493 if (m->m_len == 0) { 1494 m = m_free(m); 1495 continue; 1496 } 1497 1498 /* 1499 * Even though both 'n' and 'm' are NOTREADY, it's ok 1500 * to coalesce the data. 1501 */ 1502 if (n && 1503 M_WRITABLE(n) && 1504 ((sb->sb_flags & SB_NOCOALESCE) == 0) && 1505 !((m->m_flags ^ n->m_flags) & M_DECRYPTED) && 1506 !(n->m_flags & M_EXTPG) && 1507 m->m_len <= MCLBYTES / 4 && /* XXX: Don't copy too much */ 1508 m->m_len <= M_TRAILINGSPACE(n)) { 1509 m_copydata(m, 0, m->m_len, mtodo(n, n->m_len)); 1510 n->m_len += m->m_len; 1511 sb->sb_ccc += m->m_len; 1512 sb->sb_tlscc += m->m_len; 1513 m = m_free(m); 1514 continue; 1515 } 1516 if (n) 1517 n->m_next = m; 1518 else 1519 sb->sb_mtls = m; 1520 sb->sb_mtlstail = m; 1521 sballoc_ktls_rx(sb, m); 1522 n = m; 1523 m = m->m_next; 1524 n->m_next = NULL; 1525 } 1526 SBLASTMBUFCHK(sb); 1527 } 1528 #endif 1529 1530 /* 1531 * Free all mbufs in a sockbuf. Check that all resources are reclaimed. 1532 */ 1533 static void 1534 sbflush_internal(struct sockbuf *sb) 1535 { 1536 1537 while (sb->sb_mbcnt || sb->sb_tlsdcc) { 1538 /* 1539 * Don't call sbcut(sb, 0) if the leading mbuf is non-empty: 1540 * we would loop forever. Panic instead. 1541 */ 1542 if (sb->sb_ccc == 0 && (sb->sb_mb == NULL || sb->sb_mb->m_len)) 1543 break; 1544 m_freem(sbcut_internal(sb, (int)sb->sb_ccc)); 1545 } 1546 KASSERT(sb->sb_ccc == 0 && sb->sb_mb == 0 && sb->sb_mbcnt == 0, 1547 ("%s: ccc %u mb %p mbcnt %u", __func__, 1548 sb->sb_ccc, (void *)sb->sb_mb, sb->sb_mbcnt)); 1549 } 1550 1551 void 1552 sbflush_locked(struct sockbuf *sb) 1553 { 1554 1555 SOCKBUF_LOCK_ASSERT(sb); 1556 sbflush_internal(sb); 1557 } 1558 1559 void 1560 sbflush(struct sockbuf *sb) 1561 { 1562 1563 SOCKBUF_LOCK(sb); 1564 sbflush_locked(sb); 1565 SOCKBUF_UNLOCK(sb); 1566 } 1567 1568 /* 1569 * Cut data from (the front of) a sockbuf. 1570 */ 1571 static struct mbuf * 1572 sbcut_internal(struct sockbuf *sb, int len) 1573 { 1574 struct mbuf *m, *next, *mfree; 1575 bool is_tls; 1576 1577 KASSERT(len >= 0, ("%s: len is %d but it is supposed to be >= 0", 1578 __func__, len)); 1579 KASSERT(len <= sb->sb_ccc, ("%s: len: %d is > ccc: %u", 1580 __func__, len, sb->sb_ccc)); 1581 1582 next = (m = sb->sb_mb) ? m->m_nextpkt : 0; 1583 is_tls = false; 1584 mfree = NULL; 1585 1586 while (len > 0) { 1587 if (m == NULL) { 1588 #ifdef KERN_TLS 1589 if (next == NULL && !is_tls) { 1590 if (sb->sb_tlsdcc != 0) { 1591 MPASS(len >= sb->sb_tlsdcc); 1592 len -= sb->sb_tlsdcc; 1593 sb->sb_ccc -= sb->sb_tlsdcc; 1594 sb->sb_tlsdcc = 0; 1595 if (len == 0) 1596 break; 1597 } 1598 next = sb->sb_mtls; 1599 is_tls = true; 1600 } 1601 #endif 1602 KASSERT(next, ("%s: no next, len %d", __func__, len)); 1603 m = next; 1604 next = m->m_nextpkt; 1605 } 1606 if (m->m_len > len) { 1607 KASSERT(!(m->m_flags & M_NOTAVAIL), 1608 ("%s: m %p M_NOTAVAIL", __func__, m)); 1609 m->m_len -= len; 1610 m->m_data += len; 1611 sb->sb_ccc -= len; 1612 sb->sb_acc -= len; 1613 if (sb->sb_sndptroff != 0) 1614 sb->sb_sndptroff -= len; 1615 if (m->m_type != MT_DATA && m->m_type != MT_OOBDATA) 1616 sb->sb_ctl -= len; 1617 break; 1618 } 1619 len -= m->m_len; 1620 #ifdef KERN_TLS 1621 if (is_tls) 1622 sbfree_ktls_rx(sb, m); 1623 else 1624 #endif 1625 sbfree(sb, m); 1626 /* 1627 * Do not put M_NOTREADY buffers to the free list, they 1628 * are referenced from outside. 1629 */ 1630 if (m->m_flags & M_NOTREADY && !is_tls) 1631 m = m->m_next; 1632 else { 1633 struct mbuf *n; 1634 1635 n = m->m_next; 1636 m->m_next = mfree; 1637 mfree = m; 1638 m = n; 1639 } 1640 } 1641 /* 1642 * Free any zero-length mbufs from the buffer. 1643 * For SOCK_DGRAM sockets such mbufs represent empty records. 1644 * XXX: For SOCK_STREAM sockets such mbufs can appear in the buffer, 1645 * when sosend_generic() needs to send only control data. 1646 */ 1647 while (m && m->m_len == 0) { 1648 struct mbuf *n; 1649 1650 sbfree(sb, m); 1651 n = m->m_next; 1652 m->m_next = mfree; 1653 mfree = m; 1654 m = n; 1655 } 1656 #ifdef KERN_TLS 1657 if (is_tls) { 1658 sb->sb_mb = NULL; 1659 sb->sb_mtls = m; 1660 if (m == NULL) 1661 sb->sb_mtlstail = NULL; 1662 } else 1663 #endif 1664 if (m) { 1665 sb->sb_mb = m; 1666 m->m_nextpkt = next; 1667 } else 1668 sb->sb_mb = next; 1669 /* 1670 * First part is an inline SB_EMPTY_FIXUP(). Second part makes sure 1671 * sb_lastrecord is up-to-date if we dropped part of the last record. 1672 */ 1673 m = sb->sb_mb; 1674 if (m == NULL) { 1675 sb->sb_mbtail = NULL; 1676 sb->sb_lastrecord = NULL; 1677 } else if (m->m_nextpkt == NULL) { 1678 sb->sb_lastrecord = m; 1679 } 1680 1681 return (mfree); 1682 } 1683 1684 /* 1685 * Drop data from (the front of) a sockbuf. 1686 */ 1687 void 1688 sbdrop_locked(struct sockbuf *sb, int len) 1689 { 1690 1691 SOCKBUF_LOCK_ASSERT(sb); 1692 m_freem(sbcut_internal(sb, len)); 1693 } 1694 1695 /* 1696 * Drop data from (the front of) a sockbuf, 1697 * and return it to caller. 1698 */ 1699 struct mbuf * 1700 sbcut_locked(struct sockbuf *sb, int len) 1701 { 1702 1703 SOCKBUF_LOCK_ASSERT(sb); 1704 return (sbcut_internal(sb, len)); 1705 } 1706 1707 void 1708 sbdrop(struct sockbuf *sb, int len) 1709 { 1710 struct mbuf *mfree; 1711 1712 SOCKBUF_LOCK(sb); 1713 mfree = sbcut_internal(sb, len); 1714 SOCKBUF_UNLOCK(sb); 1715 1716 m_freem(mfree); 1717 } 1718 1719 struct mbuf * 1720 sbsndptr_noadv(struct sockbuf *sb, uint32_t off, uint32_t *moff) 1721 { 1722 struct mbuf *m; 1723 1724 KASSERT(sb->sb_mb != NULL, ("%s: sb_mb is NULL", __func__)); 1725 if (sb->sb_sndptr == NULL || sb->sb_sndptroff > off) { 1726 *moff = off; 1727 if (sb->sb_sndptr == NULL) { 1728 sb->sb_sndptr = sb->sb_mb; 1729 sb->sb_sndptroff = 0; 1730 } 1731 return (sb->sb_mb); 1732 } else { 1733 m = sb->sb_sndptr; 1734 off -= sb->sb_sndptroff; 1735 } 1736 *moff = off; 1737 return (m); 1738 } 1739 1740 void 1741 sbsndptr_adv(struct sockbuf *sb, struct mbuf *mb, uint32_t len) 1742 { 1743 /* 1744 * A small copy was done, advance forward the sb_sbsndptr to cover 1745 * it. 1746 */ 1747 struct mbuf *m; 1748 1749 if (mb != sb->sb_sndptr) { 1750 /* Did not copyout at the same mbuf */ 1751 return; 1752 } 1753 m = mb; 1754 while (m && (len > 0)) { 1755 if (len >= m->m_len) { 1756 len -= m->m_len; 1757 if (m->m_next) { 1758 sb->sb_sndptroff += m->m_len; 1759 sb->sb_sndptr = m->m_next; 1760 } 1761 m = m->m_next; 1762 } else { 1763 len = 0; 1764 } 1765 } 1766 } 1767 1768 /* 1769 * Return the first mbuf and the mbuf data offset for the provided 1770 * send offset without changing the "sb_sndptroff" field. 1771 */ 1772 struct mbuf * 1773 sbsndmbuf(struct sockbuf *sb, u_int off, u_int *moff) 1774 { 1775 struct mbuf *m; 1776 1777 KASSERT(sb->sb_mb != NULL, ("%s: sb_mb is NULL", __func__)); 1778 1779 /* 1780 * If the "off" is below the stored offset, which happens on 1781 * retransmits, just use "sb_mb": 1782 */ 1783 if (sb->sb_sndptr == NULL || sb->sb_sndptroff > off) { 1784 m = sb->sb_mb; 1785 } else { 1786 m = sb->sb_sndptr; 1787 off -= sb->sb_sndptroff; 1788 } 1789 while (off > 0 && m != NULL) { 1790 if (off < m->m_len) 1791 break; 1792 off -= m->m_len; 1793 m = m->m_next; 1794 } 1795 *moff = off; 1796 return (m); 1797 } 1798 1799 /* 1800 * Drop a record off the front of a sockbuf and move the next record to the 1801 * front. 1802 */ 1803 void 1804 sbdroprecord_locked(struct sockbuf *sb) 1805 { 1806 struct mbuf *m; 1807 1808 SOCKBUF_LOCK_ASSERT(sb); 1809 1810 m = sb->sb_mb; 1811 if (m) { 1812 sb->sb_mb = m->m_nextpkt; 1813 do { 1814 sbfree(sb, m); 1815 m = m_free(m); 1816 } while (m); 1817 } 1818 SB_EMPTY_FIXUP(sb); 1819 } 1820 1821 /* 1822 * Drop a record off the front of a sockbuf and move the next record to the 1823 * front. 1824 */ 1825 void 1826 sbdroprecord(struct sockbuf *sb) 1827 { 1828 1829 SOCKBUF_LOCK(sb); 1830 sbdroprecord_locked(sb); 1831 SOCKBUF_UNLOCK(sb); 1832 } 1833 1834 /* 1835 * Create a "control" mbuf containing the specified data with the specified 1836 * type for presentation on a socket buffer. 1837 */ 1838 struct mbuf * 1839 sbcreatecontrol(const void *p, u_int size, int type, int level, int wait) 1840 { 1841 struct cmsghdr *cp; 1842 struct mbuf *m; 1843 1844 MBUF_CHECKSLEEP(wait); 1845 1846 if (wait == M_NOWAIT) { 1847 if (CMSG_SPACE(size) > MCLBYTES) 1848 return (NULL); 1849 } else 1850 KASSERT(CMSG_SPACE(size) <= MCLBYTES, 1851 ("%s: passed CMSG_SPACE(%u) > MCLBYTES", __func__, size)); 1852 1853 if (CMSG_SPACE(size) > MLEN) 1854 m = m_getcl(wait, MT_CONTROL, 0); 1855 else 1856 m = m_get(wait, MT_CONTROL); 1857 if (m == NULL) 1858 return (NULL); 1859 1860 KASSERT(CMSG_SPACE(size) <= M_TRAILINGSPACE(m), 1861 ("sbcreatecontrol: short mbuf")); 1862 /* 1863 * Don't leave the padding between the msg header and the 1864 * cmsg data and the padding after the cmsg data un-initialized. 1865 */ 1866 cp = mtod(m, struct cmsghdr *); 1867 bzero(cp, CMSG_SPACE(size)); 1868 if (p != NULL) 1869 (void)memcpy(CMSG_DATA(cp), p, size); 1870 m->m_len = CMSG_SPACE(size); 1871 cp->cmsg_len = CMSG_LEN(size); 1872 cp->cmsg_level = level; 1873 cp->cmsg_type = type; 1874 return (m); 1875 } 1876 1877 /* 1878 * This does the same for socket buffers that sotoxsocket does for sockets: 1879 * generate an user-format data structure describing the socket buffer. Note 1880 * that the xsockbuf structure, since it is always embedded in a socket, does 1881 * not include a self pointer nor a length. We make this entry point public 1882 * in case some other mechanism needs it. 1883 */ 1884 void 1885 sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb) 1886 { 1887 1888 xsb->sb_cc = sb->sb_ccc; 1889 xsb->sb_hiwat = sb->sb_hiwat; 1890 xsb->sb_mbcnt = sb->sb_mbcnt; 1891 xsb->sb_mbmax = sb->sb_mbmax; 1892 xsb->sb_lowat = sb->sb_lowat; 1893 xsb->sb_flags = sb->sb_flags; 1894 xsb->sb_timeo = sb->sb_timeo; 1895 } 1896 1897 /* This takes the place of kern.maxsockbuf, which moved to kern.ipc. */ 1898 static int dummy; 1899 SYSCTL_INT(_kern, KERN_DUMMY, dummy, CTLFLAG_RW | CTLFLAG_SKIP, &dummy, 0, ""); 1900 SYSCTL_OID(_kern_ipc, KIPC_MAXSOCKBUF, maxsockbuf, 1901 CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_MPSAFE, &sb_max, 0, 1902 sysctl_handle_sb_max, "LU", 1903 "Maximum socket buffer size"); 1904 SYSCTL_ULONG(_kern_ipc, KIPC_SOCKBUF_WASTE, sockbuf_waste_factor, CTLFLAG_RW, 1905 &sb_efficiency, 0, "Socket buffer size waste factor"); 1906