]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet/tcp_reass.c
Merge ^/head r337286 through r337585.
[FreeBSD/FreeBSD.git] / sys / netinet / tcp_reass.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
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  *      @(#)tcp_input.c 8.12 (Berkeley) 5/24/95
32  */
33
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 #include "opt_inet.h"
38 #include "opt_inet6.h"
39 #include "opt_tcpdebug.h"
40
41 #include <sys/param.h>
42 #include <sys/kernel.h>
43 #include <sys/eventhandler.h>
44 #include <sys/malloc.h>
45 #include <sys/mbuf.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.h>
48 #include <sys/sysctl.h>
49 #include <sys/syslog.h>
50 #include <sys/systm.h>
51
52 #include <vm/uma.h>
53
54 #include <net/if.h>
55 #include <net/if_var.h>
56 #include <net/route.h>
57 #include <net/vnet.h>
58
59 #include <netinet/in.h>
60 #include <netinet/in_pcb.h>
61 #include <netinet/in_systm.h>
62 #include <netinet/in_var.h>
63 #include <netinet/ip.h>
64 #include <netinet/ip_var.h>
65 #include <netinet/ip_options.h>
66 #include <netinet/ip6.h>
67 #include <netinet6/in6_pcb.h>
68 #include <netinet6/ip6_var.h>
69 #include <netinet6/nd6.h>
70 #include <netinet/tcp.h>
71 #include <netinet/tcp_fsm.h>
72 #include <netinet/tcp_seq.h>
73 #include <netinet/tcp_timer.h>
74 #include <netinet/tcp_var.h>
75 #include <netinet6/tcp6_var.h>
76 #include <netinet/tcpip.h>
77 #ifdef TCPDEBUG
78 #include <netinet/tcp_debug.h>
79 #endif /* TCPDEBUG */
80
81 static SYSCTL_NODE(_net_inet_tcp, OID_AUTO, reass, CTLFLAG_RW, 0,
82     "TCP Segment Reassembly Queue");
83
84 static int tcp_reass_maxseg = 0;
85 SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, maxsegments, CTLFLAG_RDTUN,
86     &tcp_reass_maxseg, 0,
87     "Global maximum number of TCP Segments in Reassembly Queue");
88
89 static uma_zone_t tcp_reass_zone;
90 SYSCTL_UMA_CUR(_net_inet_tcp_reass, OID_AUTO, cursegments, 0,
91     &tcp_reass_zone,
92     "Global number of TCP Segments currently in Reassembly Queue");
93
94 static u_int tcp_reass_maxqueuelen = 100;
95 SYSCTL_UINT(_net_inet_tcp_reass, OID_AUTO, maxqueuelen, CTLFLAG_RWTUN,
96     &tcp_reass_maxqueuelen, 0,
97     "Maximum number of TCP Segments per Reassembly Queue");
98
99 /* Initialize TCP reassembly queue */
100 static void
101 tcp_reass_zone_change(void *tag)
102 {
103
104         /* Set the zone limit and read back the effective value. */
105         tcp_reass_maxseg = nmbclusters / 16;
106         tcp_reass_maxseg = uma_zone_set_max(tcp_reass_zone,
107             tcp_reass_maxseg);
108 }
109
110 void
111 tcp_reass_global_init(void)
112 {
113
114         tcp_reass_maxseg = nmbclusters / 16;
115         TUNABLE_INT_FETCH("net.inet.tcp.reass.maxsegments",
116             &tcp_reass_maxseg);
117         tcp_reass_zone = uma_zcreate("tcpreass", sizeof (struct tseg_qent),
118             NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
119         /* Set the zone limit and read back the effective value. */
120         tcp_reass_maxseg = uma_zone_set_max(tcp_reass_zone,
121             tcp_reass_maxseg);
122         EVENTHANDLER_REGISTER(nmbclusters_change,
123             tcp_reass_zone_change, NULL, EVENTHANDLER_PRI_ANY);
124 }
125
126 void
127 tcp_reass_flush(struct tcpcb *tp)
128 {
129         struct tseg_qent *qe;
130
131         INP_WLOCK_ASSERT(tp->t_inpcb);
132
133         while ((qe = LIST_FIRST(&tp->t_segq)) != NULL) {
134                 LIST_REMOVE(qe, tqe_q);
135                 m_freem(qe->tqe_m);
136                 uma_zfree(tcp_reass_zone, qe);
137                 tp->t_segqlen--;
138         }
139
140         KASSERT((tp->t_segqlen == 0),
141             ("TCP reass queue %p segment count is %d instead of 0 after flush.",
142             tp, tp->t_segqlen));
143 }
144
145 int
146 tcp_reass(struct tcpcb *tp, struct tcphdr *th, int *tlenp, struct mbuf *m)
147 {
148         struct tseg_qent *q;
149         struct tseg_qent *p = NULL;
150         struct tseg_qent *nq;
151         struct tseg_qent *te = NULL;
152         struct socket *so = tp->t_inpcb->inp_socket;
153         char *s = NULL;
154         int flags;
155         struct tseg_qent tqs;
156
157         INP_WLOCK_ASSERT(tp->t_inpcb);
158
159         /*
160          * XXX: tcp_reass() is rather inefficient with its data structures
161          * and should be rewritten (see NetBSD for optimizations).
162          */
163
164         /*
165          * Call with th==NULL after become established to
166          * force pre-ESTABLISHED data up to user socket.
167          */
168         if (th == NULL)
169                 goto present;
170
171         /*
172          * Limit the number of segments that can be queued to reduce the
173          * potential for mbuf exhaustion. For best performance, we want to be
174          * able to queue a full window's worth of segments. The size of the
175          * socket receive buffer determines our advertised window and grows
176          * automatically when socket buffer autotuning is enabled. Use it as the
177          * basis for our queue limit.
178          *
179          * However, allow the user to specify a ceiling for the number of
180          * segments in each queue.
181          *
182          * Always let the missing segment through which caused this queue.
183          * NB: Access to the socket buffer is left intentionally unlocked as we
184          * can tolerate stale information here.
185          *
186          * XXXLAS: Using sbspace(so->so_rcv) instead of so->so_rcv.sb_hiwat
187          * should work but causes packets to be dropped when they shouldn't.
188          * Investigate why and re-evaluate the below limit after the behaviour
189          * is understood.
190          */
191         if ((th->th_seq != tp->rcv_nxt || !TCPS_HAVEESTABLISHED(tp->t_state)) &&
192             tp->t_segqlen >= min((so->so_rcv.sb_hiwat / tp->t_maxseg) + 1,
193             tcp_reass_maxqueuelen)) {
194                 TCPSTAT_INC(tcps_rcvreassfull);
195                 *tlenp = 0;
196                 if ((s = tcp_log_addrs(&tp->t_inpcb->inp_inc, th, NULL, NULL))) {
197                         log(LOG_DEBUG, "%s; %s: queue limit reached, "
198                             "segment dropped\n", s, __func__);
199                         free(s, M_TCPLOG);
200                 }
201                 m_freem(m);
202                 return (0);
203         }
204
205         /*
206          * Allocate a new queue entry. If we can't, or hit the zone limit
207          * just drop the pkt.
208          *
209          * Use a temporary structure on the stack for the missing segment
210          * when the zone is exhausted. Otherwise we may get stuck.
211          */
212         te = uma_zalloc(tcp_reass_zone, M_NOWAIT);
213         if (te == NULL) {
214                 if (th->th_seq != tp->rcv_nxt || !TCPS_HAVEESTABLISHED(tp->t_state)) {
215                         TCPSTAT_INC(tcps_rcvmemdrop);
216                         m_freem(m);
217                         *tlenp = 0;
218                         if ((s = tcp_log_addrs(&tp->t_inpcb->inp_inc, th, NULL,
219                             NULL))) {
220                                 log(LOG_DEBUG, "%s; %s: global zone limit "
221                                     "reached, segment dropped\n", s, __func__);
222                                 free(s, M_TCPLOG);
223                         }
224                         return (0);
225                 } else {
226                         bzero(&tqs, sizeof(struct tseg_qent));
227                         te = &tqs;
228                         if ((s = tcp_log_addrs(&tp->t_inpcb->inp_inc, th, NULL,
229                             NULL))) {
230                                 log(LOG_DEBUG,
231                                     "%s; %s: global zone limit reached, using "
232                                     "stack for missing segment\n", s, __func__);
233                                 free(s, M_TCPLOG);
234                         }
235                 }
236         }
237         tp->t_segqlen++;
238
239         /*
240          * Find a segment which begins after this one does.
241          */
242         LIST_FOREACH(q, &tp->t_segq, tqe_q) {
243                 if (SEQ_GT(q->tqe_th->th_seq, th->th_seq))
244                         break;
245                 p = q;
246         }
247
248         /*
249          * If there is a preceding segment, it may provide some of
250          * our data already.  If so, drop the data from the incoming
251          * segment.  If it provides all of our data, drop us.
252          */
253         if (p != NULL) {
254                 int i;
255                 /* conversion to int (in i) handles seq wraparound */
256                 i = p->tqe_th->th_seq + p->tqe_len - th->th_seq;
257                 if (i > 0) {
258                         if (i >= *tlenp) {
259                                 TCPSTAT_INC(tcps_rcvduppack);
260                                 TCPSTAT_ADD(tcps_rcvdupbyte, *tlenp);
261                                 m_freem(m);
262                                 if (te != &tqs)
263                                         uma_zfree(tcp_reass_zone, te);
264                                 tp->t_segqlen--;
265                                 /*
266                                  * Try to present any queued data
267                                  * at the left window edge to the user.
268                                  * This is needed after the 3-WHS
269                                  * completes.
270                                  */
271                                 goto present;   /* ??? */
272                         }
273                         m_adj(m, i);
274                         *tlenp -= i;
275                         th->th_seq += i;
276                 }
277         }
278         tp->t_rcvoopack++;
279         TCPSTAT_INC(tcps_rcvoopack);
280         TCPSTAT_ADD(tcps_rcvoobyte, *tlenp);
281
282         /*
283          * While we overlap succeeding segments trim them or,
284          * if they are completely covered, dequeue them.
285          */
286         while (q) {
287                 int i = (th->th_seq + *tlenp) - q->tqe_th->th_seq;
288                 if (i <= 0)
289                         break;
290                 if (i < q->tqe_len) {
291                         q->tqe_th->th_seq += i;
292                         q->tqe_len -= i;
293                         m_adj(q->tqe_m, i);
294                         break;
295                 }
296
297                 nq = LIST_NEXT(q, tqe_q);
298                 LIST_REMOVE(q, tqe_q);
299                 m_freem(q->tqe_m);
300                 uma_zfree(tcp_reass_zone, q);
301                 tp->t_segqlen--;
302                 q = nq;
303         }
304
305         /* Insert the new segment queue entry into place. */
306         te->tqe_m = m;
307         te->tqe_th = th;
308         te->tqe_len = *tlenp;
309
310         if (p == NULL) {
311                 LIST_INSERT_HEAD(&tp->t_segq, te, tqe_q);
312         } else {
313                 KASSERT(te != &tqs, ("%s: temporary stack based entry not "
314                     "first element in queue", __func__));
315                 LIST_INSERT_AFTER(p, te, tqe_q);
316         }
317
318 present:
319         /*
320          * Present data to user, advancing rcv_nxt through
321          * completed sequence space.
322          */
323         if (!TCPS_HAVEESTABLISHED(tp->t_state))
324                 return (0);
325         q = LIST_FIRST(&tp->t_segq);
326         if (!q || q->tqe_th->th_seq != tp->rcv_nxt)
327                 return (0);
328         SOCKBUF_LOCK(&so->so_rcv);
329         do {
330                 tp->rcv_nxt += q->tqe_len;
331                 flags = q->tqe_th->th_flags & TH_FIN;
332                 nq = LIST_NEXT(q, tqe_q);
333                 LIST_REMOVE(q, tqe_q);
334                 if (so->so_rcv.sb_state & SBS_CANTRCVMORE)
335                         m_freem(q->tqe_m);
336                 else
337                         sbappendstream_locked(&so->so_rcv, q->tqe_m, 0);
338                 if (q != &tqs)
339                         uma_zfree(tcp_reass_zone, q);
340                 tp->t_segqlen--;
341                 q = nq;
342         } while (q && q->tqe_th->th_seq == tp->rcv_nxt);
343         sorwakeup_locked(so);
344         return (flags);
345 }