xref: /freebsd/sys/netinet/tcp_reass.c (revision 4f52dfbb)
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 /* Initialize TCP reassembly queue */
95 static void
96 tcp_reass_zone_change(void *tag)
97 {
98 
99 	/* Set the zone limit and read back the effective value. */
100 	tcp_reass_maxseg = nmbclusters / 16;
101 	tcp_reass_maxseg = uma_zone_set_max(tcp_reass_zone,
102 	    tcp_reass_maxseg);
103 }
104 
105 void
106 tcp_reass_global_init(void)
107 {
108 
109 	tcp_reass_maxseg = nmbclusters / 16;
110 	TUNABLE_INT_FETCH("net.inet.tcp.reass.maxsegments",
111 	    &tcp_reass_maxseg);
112 	tcp_reass_zone = uma_zcreate("tcpreass", sizeof (struct tseg_qent),
113 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
114 	/* Set the zone limit and read back the effective value. */
115 	tcp_reass_maxseg = uma_zone_set_max(tcp_reass_zone,
116 	    tcp_reass_maxseg);
117 	EVENTHANDLER_REGISTER(nmbclusters_change,
118 	    tcp_reass_zone_change, NULL, EVENTHANDLER_PRI_ANY);
119 }
120 
121 void
122 tcp_reass_flush(struct tcpcb *tp)
123 {
124 	struct tseg_qent *qe;
125 
126 	INP_WLOCK_ASSERT(tp->t_inpcb);
127 
128 	while ((qe = LIST_FIRST(&tp->t_segq)) != NULL) {
129 		LIST_REMOVE(qe, tqe_q);
130 		m_freem(qe->tqe_m);
131 		uma_zfree(tcp_reass_zone, qe);
132 		tp->t_segqlen--;
133 	}
134 
135 	KASSERT((tp->t_segqlen == 0),
136 	    ("TCP reass queue %p segment count is %d instead of 0 after flush.",
137 	    tp, tp->t_segqlen));
138 }
139 
140 int
141 tcp_reass(struct tcpcb *tp, struct tcphdr *th, int *tlenp, struct mbuf *m)
142 {
143 	struct tseg_qent *q;
144 	struct tseg_qent *p = NULL;
145 	struct tseg_qent *nq;
146 	struct tseg_qent *te = NULL;
147 	struct socket *so = tp->t_inpcb->inp_socket;
148 	char *s = NULL;
149 	int flags;
150 	struct tseg_qent tqs;
151 
152 	INP_WLOCK_ASSERT(tp->t_inpcb);
153 
154 	/*
155 	 * XXX: tcp_reass() is rather inefficient with its data structures
156 	 * and should be rewritten (see NetBSD for optimizations).
157 	 */
158 
159 	/*
160 	 * Call with th==NULL after become established to
161 	 * force pre-ESTABLISHED data up to user socket.
162 	 */
163 	if (th == NULL)
164 		goto present;
165 
166 	/*
167 	 * Limit the number of segments that can be queued to reduce the
168 	 * potential for mbuf exhaustion. For best performance, we want to be
169 	 * able to queue a full window's worth of segments. The size of the
170 	 * socket receive buffer determines our advertised window and grows
171 	 * automatically when socket buffer autotuning is enabled. Use it as the
172 	 * basis for our queue limit.
173 	 * Always let the missing segment through which caused this queue.
174 	 * NB: Access to the socket buffer is left intentionally unlocked as we
175 	 * can tolerate stale information here.
176 	 *
177 	 * XXXLAS: Using sbspace(so->so_rcv) instead of so->so_rcv.sb_hiwat
178 	 * should work but causes packets to be dropped when they shouldn't.
179 	 * Investigate why and re-evaluate the below limit after the behaviour
180 	 * is understood.
181 	 */
182 	if ((th->th_seq != tp->rcv_nxt || !TCPS_HAVEESTABLISHED(tp->t_state)) &&
183 	    tp->t_segqlen >= (so->so_rcv.sb_hiwat / tp->t_maxseg) + 1) {
184 		TCPSTAT_INC(tcps_rcvreassfull);
185 		*tlenp = 0;
186 		if ((s = tcp_log_addrs(&tp->t_inpcb->inp_inc, th, NULL, NULL))) {
187 			log(LOG_DEBUG, "%s; %s: queue limit reached, "
188 			    "segment dropped\n", s, __func__);
189 			free(s, M_TCPLOG);
190 		}
191 		m_freem(m);
192 		return (0);
193 	}
194 
195 	/*
196 	 * Allocate a new queue entry. If we can't, or hit the zone limit
197 	 * just drop the pkt.
198 	 *
199 	 * Use a temporary structure on the stack for the missing segment
200 	 * when the zone is exhausted. Otherwise we may get stuck.
201 	 */
202 	te = uma_zalloc(tcp_reass_zone, M_NOWAIT);
203 	if (te == NULL) {
204 		if (th->th_seq != tp->rcv_nxt || !TCPS_HAVEESTABLISHED(tp->t_state)) {
205 			TCPSTAT_INC(tcps_rcvmemdrop);
206 			m_freem(m);
207 			*tlenp = 0;
208 			if ((s = tcp_log_addrs(&tp->t_inpcb->inp_inc, th, NULL,
209 			    NULL))) {
210 				log(LOG_DEBUG, "%s; %s: global zone limit "
211 				    "reached, segment dropped\n", s, __func__);
212 				free(s, M_TCPLOG);
213 			}
214 			return (0);
215 		} else {
216 			bzero(&tqs, sizeof(struct tseg_qent));
217 			te = &tqs;
218 			if ((s = tcp_log_addrs(&tp->t_inpcb->inp_inc, th, NULL,
219 			    NULL))) {
220 				log(LOG_DEBUG,
221 				    "%s; %s: global zone limit reached, using "
222 				    "stack for missing segment\n", s, __func__);
223 				free(s, M_TCPLOG);
224 			}
225 		}
226 	}
227 	tp->t_segqlen++;
228 
229 	/*
230 	 * Find a segment which begins after this one does.
231 	 */
232 	LIST_FOREACH(q, &tp->t_segq, tqe_q) {
233 		if (SEQ_GT(q->tqe_th->th_seq, th->th_seq))
234 			break;
235 		p = q;
236 	}
237 
238 	/*
239 	 * If there is a preceding segment, it may provide some of
240 	 * our data already.  If so, drop the data from the incoming
241 	 * segment.  If it provides all of our data, drop us.
242 	 */
243 	if (p != NULL) {
244 		int i;
245 		/* conversion to int (in i) handles seq wraparound */
246 		i = p->tqe_th->th_seq + p->tqe_len - th->th_seq;
247 		if (i > 0) {
248 			if (i >= *tlenp) {
249 				TCPSTAT_INC(tcps_rcvduppack);
250 				TCPSTAT_ADD(tcps_rcvdupbyte, *tlenp);
251 				m_freem(m);
252 				if (te != &tqs)
253 					uma_zfree(tcp_reass_zone, te);
254 				tp->t_segqlen--;
255 				/*
256 				 * Try to present any queued data
257 				 * at the left window edge to the user.
258 				 * This is needed after the 3-WHS
259 				 * completes.
260 				 */
261 				goto present;	/* ??? */
262 			}
263 			m_adj(m, i);
264 			*tlenp -= i;
265 			th->th_seq += i;
266 		}
267 	}
268 	tp->t_rcvoopack++;
269 	TCPSTAT_INC(tcps_rcvoopack);
270 	TCPSTAT_ADD(tcps_rcvoobyte, *tlenp);
271 
272 	/*
273 	 * While we overlap succeeding segments trim them or,
274 	 * if they are completely covered, dequeue them.
275 	 */
276 	while (q) {
277 		int i = (th->th_seq + *tlenp) - q->tqe_th->th_seq;
278 		if (i <= 0)
279 			break;
280 		if (i < q->tqe_len) {
281 			q->tqe_th->th_seq += i;
282 			q->tqe_len -= i;
283 			m_adj(q->tqe_m, i);
284 			break;
285 		}
286 
287 		nq = LIST_NEXT(q, tqe_q);
288 		LIST_REMOVE(q, tqe_q);
289 		m_freem(q->tqe_m);
290 		uma_zfree(tcp_reass_zone, q);
291 		tp->t_segqlen--;
292 		q = nq;
293 	}
294 
295 	/* Insert the new segment queue entry into place. */
296 	te->tqe_m = m;
297 	te->tqe_th = th;
298 	te->tqe_len = *tlenp;
299 
300 	if (p == NULL) {
301 		LIST_INSERT_HEAD(&tp->t_segq, te, tqe_q);
302 	} else {
303 		KASSERT(te != &tqs, ("%s: temporary stack based entry not "
304 		    "first element in queue", __func__));
305 		LIST_INSERT_AFTER(p, te, tqe_q);
306 	}
307 
308 present:
309 	/*
310 	 * Present data to user, advancing rcv_nxt through
311 	 * completed sequence space.
312 	 */
313 	if (!TCPS_HAVEESTABLISHED(tp->t_state))
314 		return (0);
315 	q = LIST_FIRST(&tp->t_segq);
316 	if (!q || q->tqe_th->th_seq != tp->rcv_nxt)
317 		return (0);
318 	SOCKBUF_LOCK(&so->so_rcv);
319 	do {
320 		tp->rcv_nxt += q->tqe_len;
321 		flags = q->tqe_th->th_flags & TH_FIN;
322 		nq = LIST_NEXT(q, tqe_q);
323 		LIST_REMOVE(q, tqe_q);
324 		if (so->so_rcv.sb_state & SBS_CANTRCVMORE)
325 			m_freem(q->tqe_m);
326 		else
327 			sbappendstream_locked(&so->so_rcv, q->tqe_m, 0);
328 		if (q != &tqs)
329 			uma_zfree(tcp_reass_zone, q);
330 		tp->t_segqlen--;
331 		q = nq;
332 	} while (q && q->tqe_th->th_seq == tp->rcv_nxt);
333 	sorwakeup_locked(so);
334 	return (flags);
335 }
336