xref: /freebsd/sys/netinet/tcp_reass.c (revision 1a61beb0549e05b33df31380e427d90f6e46ff7e)
1 /*-
2  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	@(#)tcp_input.c	8.12 (Berkeley) 5/24/95
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include "opt_inet.h"
36 #include "opt_inet6.h"
37 #include "opt_tcpdebug.h"
38 
39 #include <sys/param.h>
40 #include <sys/kernel.h>
41 #include <sys/malloc.h>
42 #include <sys/mbuf.h>
43 #include <sys/socket.h>
44 #include <sys/socketvar.h>
45 #include <sys/sysctl.h>
46 #include <sys/syslog.h>
47 #include <sys/systm.h>
48 
49 #include <vm/uma.h>
50 
51 #include <net/if.h>
52 #include <net/if_var.h>
53 #include <net/route.h>
54 #include <net/vnet.h>
55 
56 #include <netinet/in.h>
57 #include <netinet/in_pcb.h>
58 #include <netinet/in_systm.h>
59 #include <netinet/in_var.h>
60 #include <netinet/ip.h>
61 #include <netinet/ip_var.h>
62 #include <netinet/ip_options.h>
63 #include <netinet/ip6.h>
64 #include <netinet6/in6_pcb.h>
65 #include <netinet6/ip6_var.h>
66 #include <netinet6/nd6.h>
67 #include <netinet/tcp.h>
68 #include <netinet/tcp_fsm.h>
69 #include <netinet/tcp_seq.h>
70 #include <netinet/tcp_timer.h>
71 #include <netinet/tcp_var.h>
72 #include <netinet6/tcp6_var.h>
73 #include <netinet/tcpip.h>
74 #ifdef TCPDEBUG
75 #include <netinet/tcp_debug.h>
76 #endif /* TCPDEBUG */
77 
78 static SYSCTL_NODE(_net_inet_tcp, OID_AUTO, reass, CTLFLAG_RW, 0,
79     "TCP Segment Reassembly Queue");
80 
81 static VNET_DEFINE(int, tcp_reass_maxseg) = 0;
82 #define	V_tcp_reass_maxseg		VNET(tcp_reass_maxseg)
83 SYSCTL_VNET_INT(_net_inet_tcp_reass, OID_AUTO, maxsegments, CTLFLAG_RDTUN,
84     &VNET_NAME(tcp_reass_maxseg), 0,
85     "Global maximum number of TCP Segments in Reassembly Queue");
86 
87 static VNET_DEFINE(int, tcp_reass_overflows) = 0;
88 #define	V_tcp_reass_overflows		VNET(tcp_reass_overflows)
89 SYSCTL_VNET_INT(_net_inet_tcp_reass, OID_AUTO, overflows,
90     CTLTYPE_INT | CTLFLAG_RD,
91     &VNET_NAME(tcp_reass_overflows), 0,
92     "Global number of TCP Segment Reassembly Queue Overflows");
93 
94 static VNET_DEFINE(uma_zone_t, tcp_reass_zone);
95 #define	V_tcp_reass_zone		VNET(tcp_reass_zone)
96 SYSCTL_UMA_CUR(_net_inet_tcp_reass, OID_AUTO, cursegments, CTLFLAG_VNET,
97     &VNET_NAME(tcp_reass_zone),
98     "Global number of TCP Segments currently in Reassembly Queue");
99 
100 /* Initialize TCP reassembly queue */
101 static void
102 tcp_reass_zone_change(void *tag)
103 {
104 
105 	/* Set the zone limit and read back the effective value. */
106 	V_tcp_reass_maxseg = nmbclusters / 16;
107 	V_tcp_reass_maxseg = uma_zone_set_max(V_tcp_reass_zone,
108 	    V_tcp_reass_maxseg);
109 }
110 
111 void
112 tcp_reass_init(void)
113 {
114 
115 	V_tcp_reass_maxseg = nmbclusters / 16;
116 	TUNABLE_INT_FETCH("net.inet.tcp.reass.maxsegments",
117 	    &V_tcp_reass_maxseg);
118 	V_tcp_reass_zone = uma_zcreate("tcpreass", sizeof (struct tseg_qent),
119 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
120 	/* Set the zone limit and read back the effective value. */
121 	V_tcp_reass_maxseg = uma_zone_set_max(V_tcp_reass_zone,
122 	    V_tcp_reass_maxseg);
123 	EVENTHANDLER_REGISTER(nmbclusters_change,
124 	    tcp_reass_zone_change, NULL, EVENTHANDLER_PRI_ANY);
125 }
126 
127 #ifdef VIMAGE
128 void
129 tcp_reass_destroy(void)
130 {
131 
132 	uma_zdestroy(V_tcp_reass_zone);
133 }
134 #endif
135 
136 void
137 tcp_reass_flush(struct tcpcb *tp)
138 {
139 	struct tseg_qent *qe;
140 
141 	INP_WLOCK_ASSERT(tp->t_inpcb);
142 
143 	while ((qe = LIST_FIRST(&tp->t_segq)) != NULL) {
144 		LIST_REMOVE(qe, tqe_q);
145 		m_freem(qe->tqe_m);
146 		uma_zfree(V_tcp_reass_zone, qe);
147 		tp->t_segqlen--;
148 	}
149 
150 	KASSERT((tp->t_segqlen == 0),
151 	    ("TCP reass queue %p segment count is %d instead of 0 after flush.",
152 	    tp, tp->t_segqlen));
153 }
154 
155 int
156 tcp_reass(struct tcpcb *tp, struct tcphdr *th, int *tlenp, struct mbuf *m)
157 {
158 	struct tseg_qent *q;
159 	struct tseg_qent *p = NULL;
160 	struct tseg_qent *nq;
161 	struct tseg_qent *te = NULL;
162 	struct socket *so = tp->t_inpcb->inp_socket;
163 	char *s = NULL;
164 	int flags;
165 	struct tseg_qent tqs;
166 
167 	INP_WLOCK_ASSERT(tp->t_inpcb);
168 
169 	/*
170 	 * XXX: tcp_reass() is rather inefficient with its data structures
171 	 * and should be rewritten (see NetBSD for optimizations).
172 	 */
173 
174 	/*
175 	 * Call with th==NULL after become established to
176 	 * force pre-ESTABLISHED data up to user socket.
177 	 */
178 	if (th == NULL)
179 		goto present;
180 
181 	/*
182 	 * Limit the number of segments that can be queued to reduce the
183 	 * potential for mbuf exhaustion. For best performance, we want to be
184 	 * able to queue a full window's worth of segments. The size of the
185 	 * socket receive buffer determines our advertised window and grows
186 	 * automatically when socket buffer autotuning is enabled. Use it as the
187 	 * basis for our queue limit.
188 	 * Always let the missing segment through which caused this queue.
189 	 * NB: Access to the socket buffer is left intentionally unlocked as we
190 	 * can tolerate stale information here.
191 	 *
192 	 * XXXLAS: Using sbspace(so->so_rcv) instead of so->so_rcv.sb_hiwat
193 	 * should work but causes packets to be dropped when they shouldn't.
194 	 * Investigate why and re-evaluate the below limit after the behaviour
195 	 * is understood.
196 	 */
197 	if (th->th_seq != tp->rcv_nxt &&
198 	    tp->t_segqlen >= (so->so_rcv.sb_hiwat / tp->t_maxseg) + 1) {
199 		V_tcp_reass_overflows++;
200 		TCPSTAT_INC(tcps_rcvmemdrop);
201 		m_freem(m);
202 		*tlenp = 0;
203 		if ((s = tcp_log_addrs(&tp->t_inpcb->inp_inc, th, NULL, NULL))) {
204 			log(LOG_DEBUG, "%s; %s: queue limit reached, "
205 			    "segment dropped\n", s, __func__);
206 			free(s, M_TCPLOG);
207 		}
208 		return (0);
209 	}
210 
211 	/*
212 	 * Allocate a new queue entry. If we can't, or hit the zone limit
213 	 * just drop the pkt.
214 	 *
215 	 * Use a temporary structure on the stack for the missing segment
216 	 * when the zone is exhausted. Otherwise we may get stuck.
217 	 */
218 	te = uma_zalloc(V_tcp_reass_zone, M_NOWAIT);
219 	if (te == NULL) {
220 		if (th->th_seq != tp->rcv_nxt) {
221 			TCPSTAT_INC(tcps_rcvmemdrop);
222 			m_freem(m);
223 			*tlenp = 0;
224 			if ((s = tcp_log_addrs(&tp->t_inpcb->inp_inc, th, NULL,
225 			    NULL))) {
226 				log(LOG_DEBUG, "%s; %s: global zone limit "
227 				    "reached, segment dropped\n", s, __func__);
228 				free(s, M_TCPLOG);
229 			}
230 			return (0);
231 		} else {
232 			bzero(&tqs, sizeof(struct tseg_qent));
233 			te = &tqs;
234 			if ((s = tcp_log_addrs(&tp->t_inpcb->inp_inc, th, NULL,
235 			    NULL))) {
236 				log(LOG_DEBUG,
237 				    "%s; %s: global zone limit reached, using "
238 				    "stack for missing segment\n", s, __func__);
239 				free(s, M_TCPLOG);
240 			}
241 		}
242 	}
243 	tp->t_segqlen++;
244 
245 	/*
246 	 * Find a segment which begins after this one does.
247 	 */
248 	LIST_FOREACH(q, &tp->t_segq, tqe_q) {
249 		if (SEQ_GT(q->tqe_th->th_seq, th->th_seq))
250 			break;
251 		p = q;
252 	}
253 
254 	/*
255 	 * If there is a preceding segment, it may provide some of
256 	 * our data already.  If so, drop the data from the incoming
257 	 * segment.  If it provides all of our data, drop us.
258 	 */
259 	if (p != NULL) {
260 		int i;
261 		/* conversion to int (in i) handles seq wraparound */
262 		i = p->tqe_th->th_seq + p->tqe_len - th->th_seq;
263 		if (i > 0) {
264 			if (i >= *tlenp) {
265 				TCPSTAT_INC(tcps_rcvduppack);
266 				TCPSTAT_ADD(tcps_rcvdupbyte, *tlenp);
267 				m_freem(m);
268 				uma_zfree(V_tcp_reass_zone, te);
269 				tp->t_segqlen--;
270 				/*
271 				 * Try to present any queued data
272 				 * at the left window edge to the user.
273 				 * This is needed after the 3-WHS
274 				 * completes.
275 				 */
276 				goto present;	/* ??? */
277 			}
278 			m_adj(m, i);
279 			*tlenp -= i;
280 			th->th_seq += i;
281 		}
282 	}
283 	tp->t_rcvoopack++;
284 	TCPSTAT_INC(tcps_rcvoopack);
285 	TCPSTAT_ADD(tcps_rcvoobyte, *tlenp);
286 
287 	/*
288 	 * While we overlap succeeding segments trim them or,
289 	 * if they are completely covered, dequeue them.
290 	 */
291 	while (q) {
292 		int i = (th->th_seq + *tlenp) - q->tqe_th->th_seq;
293 		if (i <= 0)
294 			break;
295 		if (i < q->tqe_len) {
296 			q->tqe_th->th_seq += i;
297 			q->tqe_len -= i;
298 			m_adj(q->tqe_m, i);
299 			break;
300 		}
301 
302 		nq = LIST_NEXT(q, tqe_q);
303 		LIST_REMOVE(q, tqe_q);
304 		m_freem(q->tqe_m);
305 		uma_zfree(V_tcp_reass_zone, q);
306 		tp->t_segqlen--;
307 		q = nq;
308 	}
309 
310 	/* Insert the new segment queue entry into place. */
311 	te->tqe_m = m;
312 	te->tqe_th = th;
313 	te->tqe_len = *tlenp;
314 
315 	if (p == NULL) {
316 		LIST_INSERT_HEAD(&tp->t_segq, te, tqe_q);
317 	} else {
318 		KASSERT(te != &tqs, ("%s: temporary stack based entry not "
319 		    "first element in queue", __func__));
320 		LIST_INSERT_AFTER(p, te, tqe_q);
321 	}
322 
323 present:
324 	/*
325 	 * Present data to user, advancing rcv_nxt through
326 	 * completed sequence space.
327 	 */
328 	if (!TCPS_HAVEESTABLISHED(tp->t_state))
329 		return (0);
330 	q = LIST_FIRST(&tp->t_segq);
331 	if (!q || q->tqe_th->th_seq != tp->rcv_nxt)
332 		return (0);
333 	SOCKBUF_LOCK(&so->so_rcv);
334 	do {
335 		tp->rcv_nxt += q->tqe_len;
336 		flags = q->tqe_th->th_flags & TH_FIN;
337 		nq = LIST_NEXT(q, tqe_q);
338 		LIST_REMOVE(q, tqe_q);
339 		if (so->so_rcv.sb_state & SBS_CANTRCVMORE)
340 			m_freem(q->tqe_m);
341 		else
342 			sbappendstream_locked(&so->so_rcv, q->tqe_m);
343 		if (q != &tqs)
344 			uma_zfree(V_tcp_reass_zone, q);
345 		tp->t_segqlen--;
346 		q = nq;
347 	} while (q && q->tqe_th->th_seq == tp->rcv_nxt);
348 	ND6_HINT(tp);
349 	sorwakeup_locked(so);
350 	return (flags);
351 }
352