]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/uipc_sockbuf.c
Upgrade to OpenSSH 7.7p1.
[FreeBSD/FreeBSD.git] / sys / kern / uipc_sockbuf.c
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 __FBSDID("$FreeBSD$");
36
37 #include "opt_param.h"
38
39 #include <sys/param.h>
40 #include <sys/aio.h> /* for aio_swake proto */
41 #include <sys/kernel.h>
42 #include <sys/lock.h>
43 #include <sys/malloc.h>
44 #include <sys/mbuf.h>
45 #include <sys/mutex.h>
46 #include <sys/proc.h>
47 #include <sys/protosw.h>
48 #include <sys/resourcevar.h>
49 #include <sys/signalvar.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/sx.h>
53 #include <sys/sysctl.h>
54
55 /*
56  * Function pointer set by the AIO routines so that the socket buffer code
57  * can call back into the AIO module if it is loaded.
58  */
59 void    (*aio_swake)(struct socket *, struct sockbuf *);
60
61 /*
62  * Primitive routines for operating on socket buffers
63  */
64
65 u_long  sb_max = SB_MAX;
66 u_long sb_max_adj =
67        (quad_t)SB_MAX * MCLBYTES / (MSIZE + MCLBYTES); /* adjusted sb_max */
68
69 static  u_long sb_efficiency = 8;       /* parameter for sbreserve() */
70
71 static struct mbuf      *sbcut_internal(struct sockbuf *sb, int len);
72 static void     sbflush_internal(struct sockbuf *sb);
73
74 /*
75  * Our own version of m_clrprotoflags(), that can preserve M_NOTREADY.
76  */
77 static void
78 sbm_clrprotoflags(struct mbuf *m, int flags)
79 {
80         int mask;
81
82         mask = ~M_PROTOFLAGS;
83         if (flags & PRUS_NOTREADY)
84                 mask |= M_NOTREADY;
85         while (m) {
86                 m->m_flags &= mask;
87                 m = m->m_next;
88         }
89 }
90
91 /*
92  * Mark ready "count" mbufs starting with "m".
93  */
94 int
95 sbready(struct sockbuf *sb, struct mbuf *m, int count)
96 {
97         u_int blocker;
98
99         SOCKBUF_LOCK_ASSERT(sb);
100         KASSERT(sb->sb_fnrdy != NULL, ("%s: sb %p NULL fnrdy", __func__, sb));
101
102         blocker = (sb->sb_fnrdy == m) ? M_BLOCKED : 0;
103
104         for (int i = 0; i < count; i++, m = m->m_next) {
105                 KASSERT(m->m_flags & M_NOTREADY,
106                     ("%s: m %p !M_NOTREADY", __func__, m));
107                 m->m_flags &= ~(M_NOTREADY | blocker);
108                 if (blocker)
109                         sb->sb_acc += m->m_len;
110         }
111
112         if (!blocker)
113                 return (EINPROGRESS);
114
115         /* This one was blocking all the queue. */
116         for (; m && (m->m_flags & M_NOTREADY) == 0; m = m->m_next) {
117                 KASSERT(m->m_flags & M_BLOCKED,
118                     ("%s: m %p !M_BLOCKED", __func__, m));
119                 m->m_flags &= ~M_BLOCKED;
120                 sb->sb_acc += m->m_len;
121         }
122
123         sb->sb_fnrdy = m;
124
125         return (0);
126 }
127
128 /*
129  * Adjust sockbuf state reflecting allocation of m.
130  */
131 void
132 sballoc(struct sockbuf *sb, struct mbuf *m)
133 {
134
135         SOCKBUF_LOCK_ASSERT(sb);
136
137         sb->sb_ccc += m->m_len;
138
139         if (sb->sb_fnrdy == NULL) {
140                 if (m->m_flags & M_NOTREADY)
141                         sb->sb_fnrdy = m;
142                 else
143                         sb->sb_acc += m->m_len;
144         } else
145                 m->m_flags |= M_BLOCKED;
146
147         if (m->m_type != MT_DATA && m->m_type != MT_OOBDATA)
148                 sb->sb_ctl += m->m_len;
149
150         sb->sb_mbcnt += MSIZE;
151         sb->sb_mcnt += 1;
152
153         if (m->m_flags & M_EXT) {
154                 sb->sb_mbcnt += m->m_ext.ext_size;
155                 sb->sb_ccnt += 1;
156         }
157 }
158
159 /*
160  * Adjust sockbuf state reflecting freeing of m.
161  */
162 void
163 sbfree(struct sockbuf *sb, struct mbuf *m)
164 {
165
166 #if 0   /* XXX: not yet: soclose() call path comes here w/o lock. */
167         SOCKBUF_LOCK_ASSERT(sb);
168 #endif
169
170         sb->sb_ccc -= m->m_len;
171
172         if (!(m->m_flags & M_NOTAVAIL))
173                 sb->sb_acc -= m->m_len;
174
175         if (m == sb->sb_fnrdy) {
176                 struct mbuf *n;
177
178                 KASSERT(m->m_flags & M_NOTREADY,
179                     ("%s: m %p !M_NOTREADY", __func__, m));
180
181                 n = m->m_next;
182                 while (n != NULL && !(n->m_flags & M_NOTREADY)) {
183                         n->m_flags &= ~M_BLOCKED;
184                         sb->sb_acc += n->m_len;
185                         n = n->m_next;
186                 }
187                 sb->sb_fnrdy = n;
188         }
189
190         if (m->m_type != MT_DATA && m->m_type != MT_OOBDATA)
191                 sb->sb_ctl -= m->m_len;
192
193         sb->sb_mbcnt -= MSIZE;
194         sb->sb_mcnt -= 1;
195         if (m->m_flags & M_EXT) {
196                 sb->sb_mbcnt -= m->m_ext.ext_size;
197                 sb->sb_ccnt -= 1;
198         }
199
200         if (sb->sb_sndptr == m) {
201                 sb->sb_sndptr = NULL;
202                 sb->sb_sndptroff = 0;
203         }
204         if (sb->sb_sndptroff != 0)
205                 sb->sb_sndptroff -= m->m_len;
206 }
207
208 /*
209  * Socantsendmore indicates that no more data will be sent on the socket; it
210  * would normally be applied to a socket when the user informs the system
211  * that no more data is to be sent, by the protocol code (in case
212  * PRU_SHUTDOWN).  Socantrcvmore indicates that no more data will be
213  * received, and will normally be applied to the socket by a protocol when it
214  * detects that the peer will send no more data.  Data queued for reading in
215  * the socket may yet be read.
216  */
217 void
218 socantsendmore_locked(struct socket *so)
219 {
220
221         SOCKBUF_LOCK_ASSERT(&so->so_snd);
222
223         so->so_snd.sb_state |= SBS_CANTSENDMORE;
224         sowwakeup_locked(so);
225         mtx_assert(SOCKBUF_MTX(&so->so_snd), MA_NOTOWNED);
226 }
227
228 void
229 socantsendmore(struct socket *so)
230 {
231
232         SOCKBUF_LOCK(&so->so_snd);
233         socantsendmore_locked(so);
234         mtx_assert(SOCKBUF_MTX(&so->so_snd), MA_NOTOWNED);
235 }
236
237 void
238 socantrcvmore_locked(struct socket *so)
239 {
240
241         SOCKBUF_LOCK_ASSERT(&so->so_rcv);
242
243         so->so_rcv.sb_state |= SBS_CANTRCVMORE;
244         sorwakeup_locked(so);
245         mtx_assert(SOCKBUF_MTX(&so->so_rcv), MA_NOTOWNED);
246 }
247
248 void
249 socantrcvmore(struct socket *so)
250 {
251
252         SOCKBUF_LOCK(&so->so_rcv);
253         socantrcvmore_locked(so);
254         mtx_assert(SOCKBUF_MTX(&so->so_rcv), MA_NOTOWNED);
255 }
256
257 /*
258  * Wait for data to arrive at/drain from a socket buffer.
259  */
260 int
261 sbwait(struct sockbuf *sb)
262 {
263
264         SOCKBUF_LOCK_ASSERT(sb);
265
266         sb->sb_flags |= SB_WAIT;
267         return (msleep_sbt(&sb->sb_acc, &sb->sb_mtx,
268             (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH, "sbwait",
269             sb->sb_timeo, 0, 0));
270 }
271
272 int
273 sblock(struct sockbuf *sb, int flags)
274 {
275
276         KASSERT((flags & SBL_VALID) == flags,
277             ("sblock: flags invalid (0x%x)", flags));
278
279         if (flags & SBL_WAIT) {
280                 if ((sb->sb_flags & SB_NOINTR) ||
281                     (flags & SBL_NOINTR)) {
282                         sx_xlock(&sb->sb_sx);
283                         return (0);
284                 }
285                 return (sx_xlock_sig(&sb->sb_sx));
286         } else {
287                 if (sx_try_xlock(&sb->sb_sx) == 0)
288                         return (EWOULDBLOCK);
289                 return (0);
290         }
291 }
292
293 void
294 sbunlock(struct sockbuf *sb)
295 {
296
297         sx_xunlock(&sb->sb_sx);
298 }
299
300 /*
301  * Wakeup processes waiting on a socket buffer.  Do asynchronous notification
302  * via SIGIO if the socket has the SS_ASYNC flag set.
303  *
304  * Called with the socket buffer lock held; will release the lock by the end
305  * of the function.  This allows the caller to acquire the socket buffer lock
306  * while testing for the need for various sorts of wakeup and hold it through
307  * to the point where it's no longer required.  We currently hold the lock
308  * through calls out to other subsystems (with the exception of kqueue), and
309  * then release it to avoid lock order issues.  It's not clear that's
310  * correct.
311  */
312 void
313 sowakeup(struct socket *so, struct sockbuf *sb)
314 {
315         int ret;
316
317         SOCKBUF_LOCK_ASSERT(sb);
318
319         selwakeuppri(sb->sb_sel, PSOCK);
320         if (!SEL_WAITING(sb->sb_sel))
321                 sb->sb_flags &= ~SB_SEL;
322         if (sb->sb_flags & SB_WAIT) {
323                 sb->sb_flags &= ~SB_WAIT;
324                 wakeup(&sb->sb_acc);
325         }
326         KNOTE_LOCKED(&sb->sb_sel->si_note, 0);
327         if (sb->sb_upcall != NULL) {
328                 ret = sb->sb_upcall(so, sb->sb_upcallarg, M_NOWAIT);
329                 if (ret == SU_ISCONNECTED) {
330                         KASSERT(sb == &so->so_rcv,
331                             ("SO_SND upcall returned SU_ISCONNECTED"));
332                         soupcall_clear(so, SO_RCV);
333                 }
334         } else
335                 ret = SU_OK;
336         if (sb->sb_flags & SB_AIO)
337                 sowakeup_aio(so, sb);
338         SOCKBUF_UNLOCK(sb);
339         if (ret == SU_ISCONNECTED)
340                 soisconnected(so);
341         if ((so->so_state & SS_ASYNC) && so->so_sigio != NULL)
342                 pgsigio(&so->so_sigio, SIGIO, 0);
343         mtx_assert(SOCKBUF_MTX(sb), MA_NOTOWNED);
344 }
345
346 /*
347  * Socket buffer (struct sockbuf) utility routines.
348  *
349  * Each socket contains two socket buffers: one for sending data and one for
350  * receiving data.  Each buffer contains a queue of mbufs, information about
351  * the number of mbufs and amount of data in the queue, and other fields
352  * allowing select() statements and notification on data availability to be
353  * implemented.
354  *
355  * Data stored in a socket buffer is maintained as a list of records.  Each
356  * record is a list of mbufs chained together with the m_next field.  Records
357  * are chained together with the m_nextpkt field. The upper level routine
358  * soreceive() expects the following conventions to be observed when placing
359  * information in the receive buffer:
360  *
361  * 1. If the protocol requires each message be preceded by the sender's name,
362  *    then a record containing that name must be present before any
363  *    associated data (mbuf's must be of type MT_SONAME).
364  * 2. If the protocol supports the exchange of ``access rights'' (really just
365  *    additional data associated with the message), and there are ``rights''
366  *    to be received, then a record containing this data should be present
367  *    (mbuf's must be of type MT_RIGHTS).
368  * 3. If a name or rights record exists, then it must be followed by a data
369  *    record, perhaps of zero length.
370  *
371  * Before using a new socket structure it is first necessary to reserve
372  * buffer space to the socket, by calling sbreserve().  This should commit
373  * some of the available buffer space in the system buffer pool for the
374  * socket (currently, it does nothing but enforce limits).  The space should
375  * be released by calling sbrelease() when the socket is destroyed.
376  */
377 int
378 soreserve(struct socket *so, u_long sndcc, u_long rcvcc)
379 {
380         struct thread *td = curthread;
381
382         SOCKBUF_LOCK(&so->so_snd);
383         SOCKBUF_LOCK(&so->so_rcv);
384         if (sbreserve_locked(&so->so_snd, sndcc, so, td) == 0)
385                 goto bad;
386         if (sbreserve_locked(&so->so_rcv, rcvcc, so, td) == 0)
387                 goto bad2;
388         if (so->so_rcv.sb_lowat == 0)
389                 so->so_rcv.sb_lowat = 1;
390         if (so->so_snd.sb_lowat == 0)
391                 so->so_snd.sb_lowat = MCLBYTES;
392         if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat)
393                 so->so_snd.sb_lowat = so->so_snd.sb_hiwat;
394         SOCKBUF_UNLOCK(&so->so_rcv);
395         SOCKBUF_UNLOCK(&so->so_snd);
396         return (0);
397 bad2:
398         sbrelease_locked(&so->so_snd, so);
399 bad:
400         SOCKBUF_UNLOCK(&so->so_rcv);
401         SOCKBUF_UNLOCK(&so->so_snd);
402         return (ENOBUFS);
403 }
404
405 static int
406 sysctl_handle_sb_max(SYSCTL_HANDLER_ARGS)
407 {
408         int error = 0;
409         u_long tmp_sb_max = sb_max;
410
411         error = sysctl_handle_long(oidp, &tmp_sb_max, arg2, req);
412         if (error || !req->newptr)
413                 return (error);
414         if (tmp_sb_max < MSIZE + MCLBYTES)
415                 return (EINVAL);
416         sb_max = tmp_sb_max;
417         sb_max_adj = (u_quad_t)sb_max * MCLBYTES / (MSIZE + MCLBYTES);
418         return (0);
419 }
420         
421 /*
422  * Allot mbufs to a sockbuf.  Attempt to scale mbmax so that mbcnt doesn't
423  * become limiting if buffering efficiency is near the normal case.
424  */
425 int
426 sbreserve_locked(struct sockbuf *sb, u_long cc, struct socket *so,
427     struct thread *td)
428 {
429         rlim_t sbsize_limit;
430
431         SOCKBUF_LOCK_ASSERT(sb);
432
433         /*
434          * When a thread is passed, we take into account the thread's socket
435          * buffer size limit.  The caller will generally pass curthread, but
436          * in the TCP input path, NULL will be passed to indicate that no
437          * appropriate thread resource limits are available.  In that case,
438          * we don't apply a process limit.
439          */
440         if (cc > sb_max_adj)
441                 return (0);
442         if (td != NULL) {
443                 sbsize_limit = lim_cur(td, RLIMIT_SBSIZE);
444         } else
445                 sbsize_limit = RLIM_INFINITY;
446         if (!chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, cc,
447             sbsize_limit))
448                 return (0);
449         sb->sb_mbmax = min(cc * sb_efficiency, sb_max);
450         if (sb->sb_lowat > sb->sb_hiwat)
451                 sb->sb_lowat = sb->sb_hiwat;
452         return (1);
453 }
454
455 int
456 sbsetopt(struct socket *so, int cmd, u_long cc)
457 {
458         struct sockbuf *sb;
459         short *flags;
460         u_int *hiwat, *lowat;
461         int error;
462
463         SOCK_LOCK(so);
464         if (SOLISTENING(so)) {
465                 switch (cmd) {
466                         case SO_SNDLOWAT:
467                         case SO_SNDBUF:
468                                 lowat = &so->sol_sbsnd_lowat;
469                                 hiwat = &so->sol_sbsnd_hiwat;
470                                 flags = &so->sol_sbsnd_flags;
471                                 break;
472                         case SO_RCVLOWAT:
473                         case SO_RCVBUF:
474                                 lowat = &so->sol_sbrcv_lowat;
475                                 hiwat = &so->sol_sbrcv_hiwat;
476                                 flags = &so->sol_sbrcv_flags;
477                                 break;
478                 }
479         } else {
480                 switch (cmd) {
481                         case SO_SNDLOWAT:
482                         case SO_SNDBUF:
483                                 sb = &so->so_snd;
484                                 break;
485                         case SO_RCVLOWAT:
486                         case SO_RCVBUF:
487                                 sb = &so->so_rcv;
488                                 break;
489                 }
490                 flags = &sb->sb_flags;
491                 hiwat = &sb->sb_hiwat;
492                 lowat = &sb->sb_lowat;
493                 SOCKBUF_LOCK(sb);
494         }
495
496         error = 0;
497         switch (cmd) {
498         case SO_SNDBUF:
499         case SO_RCVBUF:
500                 if (SOLISTENING(so)) {
501                         if (cc > sb_max_adj) {
502                                 error = ENOBUFS;
503                                 break;
504                         }
505                         *hiwat = cc;
506                         if (*lowat > *hiwat)
507                                 *lowat = *hiwat;
508                 } else {
509                         if (!sbreserve_locked(sb, cc, so, curthread))
510                                 error = ENOBUFS;
511                 }
512                 if (error == 0)
513                         *flags &= ~SB_AUTOSIZE;
514                 break;
515         case SO_SNDLOWAT:
516         case SO_RCVLOWAT:
517                 /*
518                  * Make sure the low-water is never greater than the
519                  * high-water.
520                  */
521                 *lowat = (cc > *hiwat) ? *hiwat : cc;
522                 break;
523         }
524
525         if (!SOLISTENING(so))
526                 SOCKBUF_UNLOCK(sb);
527         SOCK_UNLOCK(so);
528         return (error);
529 }
530
531 /*
532  * Free mbufs held by a socket, and reserved mbuf space.
533  */
534 void
535 sbrelease_internal(struct sockbuf *sb, struct socket *so)
536 {
537
538         sbflush_internal(sb);
539         (void)chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, 0,
540             RLIM_INFINITY);
541         sb->sb_mbmax = 0;
542 }
543
544 void
545 sbrelease_locked(struct sockbuf *sb, struct socket *so)
546 {
547
548         SOCKBUF_LOCK_ASSERT(sb);
549
550         sbrelease_internal(sb, so);
551 }
552
553 void
554 sbrelease(struct sockbuf *sb, struct socket *so)
555 {
556
557         SOCKBUF_LOCK(sb);
558         sbrelease_locked(sb, so);
559         SOCKBUF_UNLOCK(sb);
560 }
561
562 void
563 sbdestroy(struct sockbuf *sb, struct socket *so)
564 {
565
566         sbrelease_internal(sb, so);
567 }
568
569 /*
570  * Routines to add and remove data from an mbuf queue.
571  *
572  * The routines sbappend() or sbappendrecord() are normally called to append
573  * new mbufs to a socket buffer, after checking that adequate space is
574  * available, comparing the function sbspace() with the amount of data to be
575  * added.  sbappendrecord() differs from sbappend() in that data supplied is
576  * treated as the beginning of a new record.  To place a sender's address,
577  * optional access rights, and data in a socket receive buffer,
578  * sbappendaddr() should be used.  To place access rights and data in a
579  * socket receive buffer, sbappendrights() should be used.  In either case,
580  * the new data begins a new record.  Note that unlike sbappend() and
581  * sbappendrecord(), these routines check for the caller that there will be
582  * enough space to store the data.  Each fails if there is not enough space,
583  * or if it cannot find mbufs to store additional information in.
584  *
585  * Reliable protocols may use the socket send buffer to hold data awaiting
586  * acknowledgement.  Data is normally copied from a socket send buffer in a
587  * protocol with m_copy for output to a peer, and then removing the data from
588  * the socket buffer with sbdrop() or sbdroprecord() when the data is
589  * acknowledged by the peer.
590  */
591 #ifdef SOCKBUF_DEBUG
592 void
593 sblastrecordchk(struct sockbuf *sb, const char *file, int line)
594 {
595         struct mbuf *m = sb->sb_mb;
596
597         SOCKBUF_LOCK_ASSERT(sb);
598
599         while (m && m->m_nextpkt)
600                 m = m->m_nextpkt;
601
602         if (m != sb->sb_lastrecord) {
603                 printf("%s: sb_mb %p sb_lastrecord %p last %p\n",
604                         __func__, sb->sb_mb, sb->sb_lastrecord, m);
605                 printf("packet chain:\n");
606                 for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt)
607                         printf("\t%p\n", m);
608                 panic("%s from %s:%u", __func__, file, line);
609         }
610 }
611
612 void
613 sblastmbufchk(struct sockbuf *sb, const char *file, int line)
614 {
615         struct mbuf *m = sb->sb_mb;
616         struct mbuf *n;
617
618         SOCKBUF_LOCK_ASSERT(sb);
619
620         while (m && m->m_nextpkt)
621                 m = m->m_nextpkt;
622
623         while (m && m->m_next)
624                 m = m->m_next;
625
626         if (m != sb->sb_mbtail) {
627                 printf("%s: sb_mb %p sb_mbtail %p last %p\n",
628                         __func__, sb->sb_mb, sb->sb_mbtail, m);
629                 printf("packet tree:\n");
630                 for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt) {
631                         printf("\t");
632                         for (n = m; n != NULL; n = n->m_next)
633                                 printf("%p ", n);
634                         printf("\n");
635                 }
636                 panic("%s from %s:%u", __func__, file, line);
637         }
638 }
639 #endif /* SOCKBUF_DEBUG */
640
641 #define SBLINKRECORD(sb, m0) do {                                       \
642         SOCKBUF_LOCK_ASSERT(sb);                                        \
643         if ((sb)->sb_lastrecord != NULL)                                \
644                 (sb)->sb_lastrecord->m_nextpkt = (m0);                  \
645         else                                                            \
646                 (sb)->sb_mb = (m0);                                     \
647         (sb)->sb_lastrecord = (m0);                                     \
648 } while (/*CONSTCOND*/0)
649
650 /*
651  * Append mbuf chain m to the last record in the socket buffer sb.  The
652  * additional space associated the mbuf chain is recorded in sb.  Empty mbufs
653  * are discarded and mbufs are compacted where possible.
654  */
655 void
656 sbappend_locked(struct sockbuf *sb, struct mbuf *m, int flags)
657 {
658         struct mbuf *n;
659
660         SOCKBUF_LOCK_ASSERT(sb);
661
662         if (m == NULL)
663                 return;
664         sbm_clrprotoflags(m, flags);
665         SBLASTRECORDCHK(sb);
666         n = sb->sb_mb;
667         if (n) {
668                 while (n->m_nextpkt)
669                         n = n->m_nextpkt;
670                 do {
671                         if (n->m_flags & M_EOR) {
672                                 sbappendrecord_locked(sb, m); /* XXXXXX!!!! */
673                                 return;
674                         }
675                 } while (n->m_next && (n = n->m_next));
676         } else {
677                 /*
678                  * XXX Would like to simply use sb_mbtail here, but
679                  * XXX I need to verify that I won't miss an EOR that
680                  * XXX way.
681                  */
682                 if ((n = sb->sb_lastrecord) != NULL) {
683                         do {
684                                 if (n->m_flags & M_EOR) {
685                                         sbappendrecord_locked(sb, m); /* XXXXXX!!!! */
686                                         return;
687                                 }
688                         } while (n->m_next && (n = n->m_next));
689                 } else {
690                         /*
691                          * If this is the first record in the socket buffer,
692                          * it's also the last record.
693                          */
694                         sb->sb_lastrecord = m;
695                 }
696         }
697         sbcompress(sb, m, n);
698         SBLASTRECORDCHK(sb);
699 }
700
701 /*
702  * Append mbuf chain m to the last record in the socket buffer sb.  The
703  * additional space associated the mbuf chain is recorded in sb.  Empty mbufs
704  * are discarded and mbufs are compacted where possible.
705  */
706 void
707 sbappend(struct sockbuf *sb, struct mbuf *m, int flags)
708 {
709
710         SOCKBUF_LOCK(sb);
711         sbappend_locked(sb, m, flags);
712         SOCKBUF_UNLOCK(sb);
713 }
714
715 /*
716  * This version of sbappend() should only be used when the caller absolutely
717  * knows that there will never be more than one record in the socket buffer,
718  * that is, a stream protocol (such as TCP).
719  */
720 void
721 sbappendstream_locked(struct sockbuf *sb, struct mbuf *m, int flags)
722 {
723         SOCKBUF_LOCK_ASSERT(sb);
724
725         KASSERT(m->m_nextpkt == NULL,("sbappendstream 0"));
726         KASSERT(sb->sb_mb == sb->sb_lastrecord,("sbappendstream 1"));
727
728         SBLASTMBUFCHK(sb);
729
730         /* Remove all packet headers and mbuf tags to get a pure data chain. */
731         m_demote(m, 1, flags & PRUS_NOTREADY ? M_NOTREADY : 0);
732
733         sbcompress(sb, m, sb->sb_mbtail);
734
735         sb->sb_lastrecord = sb->sb_mb;
736         SBLASTRECORDCHK(sb);
737 }
738
739 /*
740  * This version of sbappend() should only be used when the caller absolutely
741  * knows that there will never be more than one record in the socket buffer,
742  * that is, a stream protocol (such as TCP).
743  */
744 void
745 sbappendstream(struct sockbuf *sb, struct mbuf *m, int flags)
746 {
747
748         SOCKBUF_LOCK(sb);
749         sbappendstream_locked(sb, m, flags);
750         SOCKBUF_UNLOCK(sb);
751 }
752
753 #ifdef SOCKBUF_DEBUG
754 void
755 sbcheck(struct sockbuf *sb, const char *file, int line)
756 {
757         struct mbuf *m, *n, *fnrdy;
758         u_long acc, ccc, mbcnt;
759
760         SOCKBUF_LOCK_ASSERT(sb);
761
762         acc = ccc = mbcnt = 0;
763         fnrdy = NULL;
764
765         for (m = sb->sb_mb; m; m = n) {
766             n = m->m_nextpkt;
767             for (; m; m = m->m_next) {
768                 if (m->m_len == 0) {
769                         printf("sb %p empty mbuf %p\n", sb, m);
770                         goto fail;
771                 }
772                 if ((m->m_flags & M_NOTREADY) && fnrdy == NULL) {
773                         if (m != sb->sb_fnrdy) {
774                                 printf("sb %p: fnrdy %p != m %p\n",
775                                     sb, sb->sb_fnrdy, m);
776                                 goto fail;
777                         }
778                         fnrdy = m;
779                 }
780                 if (fnrdy) {
781                         if (!(m->m_flags & M_NOTAVAIL)) {
782                                 printf("sb %p: fnrdy %p, m %p is avail\n",
783                                     sb, sb->sb_fnrdy, m);
784                                 goto fail;
785                         }
786                 } else
787                         acc += m->m_len;
788                 ccc += m->m_len;
789                 mbcnt += MSIZE;
790                 if (m->m_flags & M_EXT) /*XXX*/ /* pretty sure this is bogus */
791                         mbcnt += m->m_ext.ext_size;
792             }
793         }
794         if (acc != sb->sb_acc || ccc != sb->sb_ccc || mbcnt != sb->sb_mbcnt) {
795                 printf("acc %ld/%u ccc %ld/%u mbcnt %ld/%u\n",
796                     acc, sb->sb_acc, ccc, sb->sb_ccc, mbcnt, sb->sb_mbcnt);
797                 goto fail;
798         }
799         return;
800 fail:
801         panic("%s from %s:%u", __func__, file, line);
802 }
803 #endif
804
805 /*
806  * As above, except the mbuf chain begins a new record.
807  */
808 void
809 sbappendrecord_locked(struct sockbuf *sb, struct mbuf *m0)
810 {
811         struct mbuf *m;
812
813         SOCKBUF_LOCK_ASSERT(sb);
814
815         if (m0 == NULL)
816                 return;
817         m_clrprotoflags(m0);
818         /*
819          * Put the first mbuf on the queue.  Note this permits zero length
820          * records.
821          */
822         sballoc(sb, m0);
823         SBLASTRECORDCHK(sb);
824         SBLINKRECORD(sb, m0);
825         sb->sb_mbtail = m0;
826         m = m0->m_next;
827         m0->m_next = 0;
828         if (m && (m0->m_flags & M_EOR)) {
829                 m0->m_flags &= ~M_EOR;
830                 m->m_flags |= M_EOR;
831         }
832         /* always call sbcompress() so it can do SBLASTMBUFCHK() */
833         sbcompress(sb, m, m0);
834 }
835
836 /*
837  * As above, except the mbuf chain begins a new record.
838  */
839 void
840 sbappendrecord(struct sockbuf *sb, struct mbuf *m0)
841 {
842
843         SOCKBUF_LOCK(sb);
844         sbappendrecord_locked(sb, m0);
845         SOCKBUF_UNLOCK(sb);
846 }
847
848 /* Helper routine that appends data, control, and address to a sockbuf. */
849 static int
850 sbappendaddr_locked_internal(struct sockbuf *sb, const struct sockaddr *asa,
851     struct mbuf *m0, struct mbuf *control, struct mbuf *ctrl_last)
852 {
853         struct mbuf *m, *n, *nlast;
854 #if MSIZE <= 256
855         if (asa->sa_len > MLEN)
856                 return (0);
857 #endif
858         m = m_get(M_NOWAIT, MT_SONAME);
859         if (m == NULL)
860                 return (0);
861         m->m_len = asa->sa_len;
862         bcopy(asa, mtod(m, caddr_t), asa->sa_len);
863         if (m0) {
864                 m_clrprotoflags(m0);
865                 m_tag_delete_chain(m0, NULL);
866                 /*
867                  * Clear some persistent info from pkthdr.
868                  * We don't use m_demote(), because some netgraph consumers
869                  * expect M_PKTHDR presence.
870                  */
871                 m0->m_pkthdr.rcvif = NULL;
872                 m0->m_pkthdr.flowid = 0;
873                 m0->m_pkthdr.csum_flags = 0;
874                 m0->m_pkthdr.fibnum = 0;
875                 m0->m_pkthdr.rsstype = 0;
876         }
877         if (ctrl_last)
878                 ctrl_last->m_next = m0; /* concatenate data to control */
879         else
880                 control = m0;
881         m->m_next = control;
882         for (n = m; n->m_next != NULL; n = n->m_next)
883                 sballoc(sb, n);
884         sballoc(sb, n);
885         nlast = n;
886         SBLINKRECORD(sb, m);
887
888         sb->sb_mbtail = nlast;
889         SBLASTMBUFCHK(sb);
890
891         SBLASTRECORDCHK(sb);
892         return (1);
893 }
894
895 /*
896  * Append address and data, and optionally, control (ancillary) data to the
897  * receive queue of a socket.  If present, m0 must include a packet header
898  * with total length.  Returns 0 if no space in sockbuf or insufficient
899  * mbufs.
900  */
901 int
902 sbappendaddr_locked(struct sockbuf *sb, const struct sockaddr *asa,
903     struct mbuf *m0, struct mbuf *control)
904 {
905         struct mbuf *ctrl_last;
906         int space = asa->sa_len;
907
908         SOCKBUF_LOCK_ASSERT(sb);
909
910         if (m0 && (m0->m_flags & M_PKTHDR) == 0)
911                 panic("sbappendaddr_locked");
912         if (m0)
913                 space += m0->m_pkthdr.len;
914         space += m_length(control, &ctrl_last);
915
916         if (space > sbspace(sb))
917                 return (0);
918         return (sbappendaddr_locked_internal(sb, asa, m0, control, ctrl_last));
919 }
920
921 /*
922  * Append address and data, and optionally, control (ancillary) data to the
923  * receive queue of a socket.  If present, m0 must include a packet header
924  * with total length.  Returns 0 if insufficient mbufs.  Does not validate space
925  * on the receiving sockbuf.
926  */
927 int
928 sbappendaddr_nospacecheck_locked(struct sockbuf *sb, const struct sockaddr *asa,
929     struct mbuf *m0, struct mbuf *control)
930 {
931         struct mbuf *ctrl_last;
932
933         SOCKBUF_LOCK_ASSERT(sb);
934
935         ctrl_last = (control == NULL) ? NULL : m_last(control);
936         return (sbappendaddr_locked_internal(sb, asa, m0, control, ctrl_last));
937 }
938
939 /*
940  * Append address and data, and optionally, control (ancillary) data to the
941  * receive queue of a socket.  If present, m0 must include a packet header
942  * with total length.  Returns 0 if no space in sockbuf or insufficient
943  * mbufs.
944  */
945 int
946 sbappendaddr(struct sockbuf *sb, const struct sockaddr *asa,
947     struct mbuf *m0, struct mbuf *control)
948 {
949         int retval;
950
951         SOCKBUF_LOCK(sb);
952         retval = sbappendaddr_locked(sb, asa, m0, control);
953         SOCKBUF_UNLOCK(sb);
954         return (retval);
955 }
956
957 int
958 sbappendcontrol_locked(struct sockbuf *sb, struct mbuf *m0,
959     struct mbuf *control)
960 {
961         struct mbuf *m, *n, *mlast;
962         int space;
963
964         SOCKBUF_LOCK_ASSERT(sb);
965
966         if (control == NULL)
967                 panic("sbappendcontrol_locked");
968         space = m_length(control, &n) + m_length(m0, NULL);
969
970         if (space > sbspace(sb))
971                 return (0);
972         m_clrprotoflags(m0);
973         n->m_next = m0;                 /* concatenate data to control */
974
975         SBLASTRECORDCHK(sb);
976
977         for (m = control; m->m_next; m = m->m_next)
978                 sballoc(sb, m);
979         sballoc(sb, m);
980         mlast = m;
981         SBLINKRECORD(sb, control);
982
983         sb->sb_mbtail = mlast;
984         SBLASTMBUFCHK(sb);
985
986         SBLASTRECORDCHK(sb);
987         return (1);
988 }
989
990 int
991 sbappendcontrol(struct sockbuf *sb, struct mbuf *m0, struct mbuf *control)
992 {
993         int retval;
994
995         SOCKBUF_LOCK(sb);
996         retval = sbappendcontrol_locked(sb, m0, control);
997         SOCKBUF_UNLOCK(sb);
998         return (retval);
999 }
1000
1001 /*
1002  * Append the data in mbuf chain (m) into the socket buffer sb following mbuf
1003  * (n).  If (n) is NULL, the buffer is presumed empty.
1004  *
1005  * When the data is compressed, mbufs in the chain may be handled in one of
1006  * three ways:
1007  *
1008  * (1) The mbuf may simply be dropped, if it contributes nothing (no data, no
1009  *     record boundary, and no change in data type).
1010  *
1011  * (2) The mbuf may be coalesced -- i.e., data in the mbuf may be copied into
1012  *     an mbuf already in the socket buffer.  This can occur if an
1013  *     appropriate mbuf exists, there is room, both mbufs are not marked as
1014  *     not ready, and no merging of data types will occur.
1015  *
1016  * (3) The mbuf may be appended to the end of the existing mbuf chain.
1017  *
1018  * If any of the new mbufs is marked as M_EOR, mark the last mbuf appended as
1019  * end-of-record.
1020  */
1021 void
1022 sbcompress(struct sockbuf *sb, struct mbuf *m, struct mbuf *n)
1023 {
1024         int eor = 0;
1025         struct mbuf *o;
1026
1027         SOCKBUF_LOCK_ASSERT(sb);
1028
1029         while (m) {
1030                 eor |= m->m_flags & M_EOR;
1031                 if (m->m_len == 0 &&
1032                     (eor == 0 ||
1033                      (((o = m->m_next) || (o = n)) &&
1034                       o->m_type == m->m_type))) {
1035                         if (sb->sb_lastrecord == m)
1036                                 sb->sb_lastrecord = m->m_next;
1037                         m = m_free(m);
1038                         continue;
1039                 }
1040                 if (n && (n->m_flags & M_EOR) == 0 &&
1041                     M_WRITABLE(n) &&
1042                     ((sb->sb_flags & SB_NOCOALESCE) == 0) &&
1043                     !(m->m_flags & M_NOTREADY) &&
1044                     !(n->m_flags & M_NOTREADY) &&
1045                     m->m_len <= MCLBYTES / 4 && /* XXX: Don't copy too much */
1046                     m->m_len <= M_TRAILINGSPACE(n) &&
1047                     n->m_type == m->m_type) {
1048                         bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len,
1049                             (unsigned)m->m_len);
1050                         n->m_len += m->m_len;
1051                         sb->sb_ccc += m->m_len;
1052                         if (sb->sb_fnrdy == NULL)
1053                                 sb->sb_acc += m->m_len;
1054                         if (m->m_type != MT_DATA && m->m_type != MT_OOBDATA)
1055                                 /* XXX: Probably don't need.*/
1056                                 sb->sb_ctl += m->m_len;
1057                         m = m_free(m);
1058                         continue;
1059                 }
1060                 if (n)
1061                         n->m_next = m;
1062                 else
1063                         sb->sb_mb = m;
1064                 sb->sb_mbtail = m;
1065                 sballoc(sb, m);
1066                 n = m;
1067                 m->m_flags &= ~M_EOR;
1068                 m = m->m_next;
1069                 n->m_next = 0;
1070         }
1071         if (eor) {
1072                 KASSERT(n != NULL, ("sbcompress: eor && n == NULL"));
1073                 n->m_flags |= eor;
1074         }
1075         SBLASTMBUFCHK(sb);
1076 }
1077
1078 /*
1079  * Free all mbufs in a sockbuf.  Check that all resources are reclaimed.
1080  */
1081 static void
1082 sbflush_internal(struct sockbuf *sb)
1083 {
1084
1085         while (sb->sb_mbcnt) {
1086                 /*
1087                  * Don't call sbcut(sb, 0) if the leading mbuf is non-empty:
1088                  * we would loop forever. Panic instead.
1089                  */
1090                 if (sb->sb_ccc == 0 && (sb->sb_mb == NULL || sb->sb_mb->m_len))
1091                         break;
1092                 m_freem(sbcut_internal(sb, (int)sb->sb_ccc));
1093         }
1094         KASSERT(sb->sb_ccc == 0 && sb->sb_mb == 0 && sb->sb_mbcnt == 0,
1095             ("%s: ccc %u mb %p mbcnt %u", __func__,
1096             sb->sb_ccc, (void *)sb->sb_mb, sb->sb_mbcnt));
1097 }
1098
1099 void
1100 sbflush_locked(struct sockbuf *sb)
1101 {
1102
1103         SOCKBUF_LOCK_ASSERT(sb);
1104         sbflush_internal(sb);
1105 }
1106
1107 void
1108 sbflush(struct sockbuf *sb)
1109 {
1110
1111         SOCKBUF_LOCK(sb);
1112         sbflush_locked(sb);
1113         SOCKBUF_UNLOCK(sb);
1114 }
1115
1116 /*
1117  * Cut data from (the front of) a sockbuf.
1118  */
1119 static struct mbuf *
1120 sbcut_internal(struct sockbuf *sb, int len)
1121 {
1122         struct mbuf *m, *next, *mfree;
1123
1124         KASSERT(len >= 0, ("%s: len is %d but it is supposed to be >= 0",
1125             __func__, len));
1126         KASSERT(len <= sb->sb_ccc, ("%s: len: %d is > ccc: %u",
1127             __func__, len, sb->sb_ccc));
1128
1129         next = (m = sb->sb_mb) ? m->m_nextpkt : 0;
1130         mfree = NULL;
1131
1132         while (len > 0) {
1133                 if (m == NULL) {
1134                         KASSERT(next, ("%s: no next, len %d", __func__, len));
1135                         m = next;
1136                         next = m->m_nextpkt;
1137                 }
1138                 if (m->m_len > len) {
1139                         KASSERT(!(m->m_flags & M_NOTAVAIL),
1140                             ("%s: m %p M_NOTAVAIL", __func__, m));
1141                         m->m_len -= len;
1142                         m->m_data += len;
1143                         sb->sb_ccc -= len;
1144                         sb->sb_acc -= len;
1145                         if (sb->sb_sndptroff != 0)
1146                                 sb->sb_sndptroff -= len;
1147                         if (m->m_type != MT_DATA && m->m_type != MT_OOBDATA)
1148                                 sb->sb_ctl -= len;
1149                         break;
1150                 }
1151                 len -= m->m_len;
1152                 sbfree(sb, m);
1153                 /*
1154                  * Do not put M_NOTREADY buffers to the free list, they
1155                  * are referenced from outside.
1156                  */
1157                 if (m->m_flags & M_NOTREADY)
1158                         m = m->m_next;
1159                 else {
1160                         struct mbuf *n;
1161
1162                         n = m->m_next;
1163                         m->m_next = mfree;
1164                         mfree = m;
1165                         m = n;
1166                 }
1167         }
1168         /*
1169          * Free any zero-length mbufs from the buffer.
1170          * For SOCK_DGRAM sockets such mbufs represent empty records.
1171          * XXX: For SOCK_STREAM sockets such mbufs can appear in the buffer,
1172          * when sosend_generic() needs to send only control data.
1173          */
1174         while (m && m->m_len == 0) {
1175                 struct mbuf *n;
1176
1177                 sbfree(sb, m);
1178                 n = m->m_next;
1179                 m->m_next = mfree;
1180                 mfree = m;
1181                 m = n;
1182         }
1183         if (m) {
1184                 sb->sb_mb = m;
1185                 m->m_nextpkt = next;
1186         } else
1187                 sb->sb_mb = next;
1188         /*
1189          * First part is an inline SB_EMPTY_FIXUP().  Second part makes sure
1190          * sb_lastrecord is up-to-date if we dropped part of the last record.
1191          */
1192         m = sb->sb_mb;
1193         if (m == NULL) {
1194                 sb->sb_mbtail = NULL;
1195                 sb->sb_lastrecord = NULL;
1196         } else if (m->m_nextpkt == NULL) {
1197                 sb->sb_lastrecord = m;
1198         }
1199
1200         return (mfree);
1201 }
1202
1203 /*
1204  * Drop data from (the front of) a sockbuf.
1205  */
1206 void
1207 sbdrop_locked(struct sockbuf *sb, int len)
1208 {
1209
1210         SOCKBUF_LOCK_ASSERT(sb);
1211         m_freem(sbcut_internal(sb, len));
1212 }
1213
1214 /*
1215  * Drop data from (the front of) a sockbuf,
1216  * and return it to caller.
1217  */
1218 struct mbuf *
1219 sbcut_locked(struct sockbuf *sb, int len)
1220 {
1221
1222         SOCKBUF_LOCK_ASSERT(sb);
1223         return (sbcut_internal(sb, len));
1224 }
1225
1226 void
1227 sbdrop(struct sockbuf *sb, int len)
1228 {
1229         struct mbuf *mfree;
1230
1231         SOCKBUF_LOCK(sb);
1232         mfree = sbcut_internal(sb, len);
1233         SOCKBUF_UNLOCK(sb);
1234
1235         m_freem(mfree);
1236 }
1237
1238 /*
1239  * Maintain a pointer and offset pair into the socket buffer mbuf chain to
1240  * avoid traversal of the entire socket buffer for larger offsets.
1241  */
1242 struct mbuf *
1243 sbsndptr(struct sockbuf *sb, u_int off, u_int len, u_int *moff)
1244 {
1245         struct mbuf *m, *ret;
1246
1247         KASSERT(sb->sb_mb != NULL, ("%s: sb_mb is NULL", __func__));
1248         KASSERT(off + len <= sb->sb_acc, ("%s: beyond sb", __func__));
1249         KASSERT(sb->sb_sndptroff <= sb->sb_acc, ("%s: sndptroff broken", __func__));
1250
1251         /*
1252          * Is off below stored offset? Happens on retransmits.
1253          * Just return, we can't help here.
1254          */
1255         if (sb->sb_sndptroff > off) {
1256                 *moff = off;
1257                 return (sb->sb_mb);
1258         }
1259
1260         /* Return closest mbuf in chain for current offset. */
1261         *moff = off - sb->sb_sndptroff;
1262         m = ret = sb->sb_sndptr ? sb->sb_sndptr : sb->sb_mb;
1263         if (*moff == m->m_len) {
1264                 *moff = 0;
1265                 sb->sb_sndptroff += m->m_len;
1266                 m = ret = m->m_next;
1267                 KASSERT(ret->m_len > 0,
1268                     ("mbuf %p in sockbuf %p chain has no valid data", ret, sb));
1269         }
1270
1271         /* Advance by len to be as close as possible for the next transmit. */
1272         for (off = off - sb->sb_sndptroff + len - 1;
1273              off > 0 && m != NULL && off >= m->m_len;
1274              m = m->m_next) {
1275                 sb->sb_sndptroff += m->m_len;
1276                 off -= m->m_len;
1277         }
1278         if (off > 0 && m == NULL)
1279                 panic("%s: sockbuf %p and mbuf %p clashing", __func__, sb, ret);
1280         sb->sb_sndptr = m;
1281
1282         return (ret);
1283 }
1284
1285 /*
1286  * Return the first mbuf and the mbuf data offset for the provided
1287  * send offset without changing the "sb_sndptroff" field.
1288  */
1289 struct mbuf *
1290 sbsndmbuf(struct sockbuf *sb, u_int off, u_int *moff)
1291 {
1292         struct mbuf *m;
1293
1294         KASSERT(sb->sb_mb != NULL, ("%s: sb_mb is NULL", __func__));
1295
1296         /*
1297          * If the "off" is below the stored offset, which happens on
1298          * retransmits, just use "sb_mb":
1299          */
1300         if (sb->sb_sndptr == NULL || sb->sb_sndptroff > off) {
1301                 m = sb->sb_mb;
1302         } else {
1303                 m = sb->sb_sndptr;
1304                 off -= sb->sb_sndptroff;
1305         }
1306         while (off > 0 && m != NULL) {
1307                 if (off < m->m_len)
1308                         break;
1309                 off -= m->m_len;
1310                 m = m->m_next;
1311         }
1312         *moff = off;
1313         return (m);
1314 }
1315
1316 /*
1317  * Drop a record off the front of a sockbuf and move the next record to the
1318  * front.
1319  */
1320 void
1321 sbdroprecord_locked(struct sockbuf *sb)
1322 {
1323         struct mbuf *m;
1324
1325         SOCKBUF_LOCK_ASSERT(sb);
1326
1327         m = sb->sb_mb;
1328         if (m) {
1329                 sb->sb_mb = m->m_nextpkt;
1330                 do {
1331                         sbfree(sb, m);
1332                         m = m_free(m);
1333                 } while (m);
1334         }
1335         SB_EMPTY_FIXUP(sb);
1336 }
1337
1338 /*
1339  * Drop a record off the front of a sockbuf and move the next record to the
1340  * front.
1341  */
1342 void
1343 sbdroprecord(struct sockbuf *sb)
1344 {
1345
1346         SOCKBUF_LOCK(sb);
1347         sbdroprecord_locked(sb);
1348         SOCKBUF_UNLOCK(sb);
1349 }
1350
1351 /*
1352  * Create a "control" mbuf containing the specified data with the specified
1353  * type for presentation on a socket buffer.
1354  */
1355 struct mbuf *
1356 sbcreatecontrol(caddr_t p, int size, int type, int level)
1357 {
1358         struct cmsghdr *cp;
1359         struct mbuf *m;
1360
1361         if (CMSG_SPACE((u_int)size) > MCLBYTES)
1362                 return ((struct mbuf *) NULL);
1363         if (CMSG_SPACE((u_int)size) > MLEN)
1364                 m = m_getcl(M_NOWAIT, MT_CONTROL, 0);
1365         else
1366                 m = m_get(M_NOWAIT, MT_CONTROL);
1367         if (m == NULL)
1368                 return ((struct mbuf *) NULL);
1369         cp = mtod(m, struct cmsghdr *);
1370         m->m_len = 0;
1371         KASSERT(CMSG_SPACE((u_int)size) <= M_TRAILINGSPACE(m),
1372             ("sbcreatecontrol: short mbuf"));
1373         /*
1374          * Don't leave the padding between the msg header and the
1375          * cmsg data and the padding after the cmsg data un-initialized.
1376          */
1377         bzero(cp, CMSG_SPACE((u_int)size));
1378         if (p != NULL)
1379                 (void)memcpy(CMSG_DATA(cp), p, size);
1380         m->m_len = CMSG_SPACE(size);
1381         cp->cmsg_len = CMSG_LEN(size);
1382         cp->cmsg_level = level;
1383         cp->cmsg_type = type;
1384         return (m);
1385 }
1386
1387 /*
1388  * This does the same for socket buffers that sotoxsocket does for sockets:
1389  * generate an user-format data structure describing the socket buffer.  Note
1390  * that the xsockbuf structure, since it is always embedded in a socket, does
1391  * not include a self pointer nor a length.  We make this entry point public
1392  * in case some other mechanism needs it.
1393  */
1394 void
1395 sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb)
1396 {
1397
1398         xsb->sb_cc = sb->sb_ccc;
1399         xsb->sb_hiwat = sb->sb_hiwat;
1400         xsb->sb_mbcnt = sb->sb_mbcnt;
1401         xsb->sb_mcnt = sb->sb_mcnt;     
1402         xsb->sb_ccnt = sb->sb_ccnt;
1403         xsb->sb_mbmax = sb->sb_mbmax;
1404         xsb->sb_lowat = sb->sb_lowat;
1405         xsb->sb_flags = sb->sb_flags;
1406         xsb->sb_timeo = sb->sb_timeo;
1407 }
1408
1409 /* This takes the place of kern.maxsockbuf, which moved to kern.ipc. */
1410 static int dummy;
1411 SYSCTL_INT(_kern, KERN_DUMMY, dummy, CTLFLAG_RW, &dummy, 0, "");
1412 SYSCTL_OID(_kern_ipc, KIPC_MAXSOCKBUF, maxsockbuf, CTLTYPE_ULONG|CTLFLAG_RW,
1413     &sb_max, 0, sysctl_handle_sb_max, "LU", "Maximum socket buffer size");
1414 SYSCTL_ULONG(_kern_ipc, KIPC_SOCKBUF_WASTE, sockbuf_waste_factor, CTLFLAG_RW,
1415     &sb_efficiency, 0, "Socket buffer size waste factor");