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