xref: /freebsd/sys/netinet/tcp_reass.c (revision 5861f9665471e98e544f6fa3ce73c4912229ff82)
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 #include <sys/vimage.h>
49 
50 #include <vm/uma.h>
51 
52 #include <net/if.h>
53 #include <net/route.h>
54 
55 #include <netinet/in.h>
56 #include <netinet/in_pcb.h>
57 #include <netinet/in_systm.h>
58 #include <netinet/in_var.h>
59 #include <netinet/ip.h>
60 #include <netinet/ip_var.h>
61 #include <netinet/ip_options.h>
62 #include <netinet/ip6.h>
63 #include <netinet6/in6_pcb.h>
64 #include <netinet6/ip6_var.h>
65 #include <netinet6/nd6.h>
66 #include <netinet/tcp.h>
67 #include <netinet/tcp_fsm.h>
68 #include <netinet/tcp_seq.h>
69 #include <netinet/tcp_timer.h>
70 #include <netinet/tcp_var.h>
71 #include <netinet6/tcp6_var.h>
72 #include <netinet/tcpip.h>
73 #ifdef TCPDEBUG
74 #include <netinet/tcp_debug.h>
75 #endif /* TCPDEBUG */
76 #include <netinet/vinet.h>
77 
78 #ifdef VIMAGE_GLOBALS
79 static int tcp_reass_maxseg;
80 int tcp_reass_qsize;
81 static int tcp_reass_maxqlen;
82 static int tcp_reass_overflows;
83 #endif
84 
85 SYSCTL_NODE(_net_inet_tcp, OID_AUTO, reass, CTLFLAG_RW, 0,
86     "TCP Segment Reassembly Queue");
87 
88 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp_reass, OID_AUTO, maxsegments,
89     CTLFLAG_RDTUN, tcp_reass_maxseg, 0,
90     "Global maximum number of TCP Segments in Reassembly Queue");
91 
92 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp_reass, OID_AUTO, cursegments,
93     CTLFLAG_RD, tcp_reass_qsize, 0,
94     "Global number of TCP Segments currently in Reassembly Queue");
95 
96 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp_reass, OID_AUTO, maxqlen,
97     CTLFLAG_RW, tcp_reass_maxqlen, 0,
98     "Maximum number of TCP Segments per individual Reassembly Queue");
99 
100 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp_reass, OID_AUTO, overflows,
101     CTLFLAG_RD, tcp_reass_overflows, 0,
102     "Global number of TCP Segment Reassembly Queue Overflows");
103 
104 /* Initialize TCP reassembly queue */
105 static void
106 tcp_reass_zone_change(void *tag)
107 {
108 	INIT_VNET_INET(curvnet);
109 
110 	V_tcp_reass_maxseg = nmbclusters / 16;
111 	uma_zone_set_max(V_tcp_reass_zone, V_tcp_reass_maxseg);
112 }
113 
114 #ifdef VIMAGE_GLOBALS
115 uma_zone_t	tcp_reass_zone;
116 #endif
117 
118 void
119 tcp_reass_init(void)
120 {
121 	INIT_VNET_INET(curvnet);
122 
123 	V_tcp_reass_maxseg = 0;
124 	V_tcp_reass_qsize = 0;
125 	V_tcp_reass_maxqlen = 48;
126 	V_tcp_reass_overflows = 0;
127 
128 	V_tcp_reass_maxseg = nmbclusters / 16;
129 	TUNABLE_INT_FETCH("net.inet.tcp.reass.maxsegments",
130 	    &V_tcp_reass_maxseg);
131 	V_tcp_reass_zone = uma_zcreate("tcpreass", sizeof (struct tseg_qent),
132 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
133 	uma_zone_set_max(V_tcp_reass_zone, V_tcp_reass_maxseg);
134 	EVENTHANDLER_REGISTER(nmbclusters_change,
135 	    tcp_reass_zone_change, NULL, EVENTHANDLER_PRI_ANY);
136 }
137 
138 int
139 tcp_reass(struct tcpcb *tp, struct tcphdr *th, int *tlenp, struct mbuf *m)
140 {
141 	INIT_VNET_INET(curvnet);
142 	struct tseg_qent *q;
143 	struct tseg_qent *p = NULL;
144 	struct tseg_qent *nq;
145 	struct tseg_qent *te = NULL;
146 	struct socket *so = tp->t_inpcb->inp_socket;
147 	int flags;
148 
149 	INP_WLOCK_ASSERT(tp->t_inpcb);
150 
151 	/*
152 	 * XXX: tcp_reass() is rather inefficient with its data structures
153 	 * and should be rewritten (see NetBSD for optimizations).
154 	 */
155 
156 	/*
157 	 * Call with th==NULL after become established to
158 	 * force pre-ESTABLISHED data up to user socket.
159 	 */
160 	if (th == NULL)
161 		goto present;
162 
163 	/*
164 	 * Limit the number of segments in the reassembly queue to prevent
165 	 * holding on to too many segments (and thus running out of mbufs).
166 	 * Make sure to let the missing segment through which caused this
167 	 * queue.  Always keep one global queue entry spare to be able to
168 	 * process the missing segment.
169 	 */
170 	if (th->th_seq != tp->rcv_nxt &&
171 	    (V_tcp_reass_qsize + 1 >= V_tcp_reass_maxseg ||
172 	     tp->t_segqlen >= V_tcp_reass_maxqlen)) {
173 		V_tcp_reass_overflows++;
174 		TCPSTAT_INC(tcps_rcvmemdrop);
175 		m_freem(m);
176 		*tlenp = 0;
177 		return (0);
178 	}
179 
180 	/*
181 	 * Allocate a new queue entry. If we can't, or hit the zone limit
182 	 * just drop the pkt.
183 	 */
184 	te = uma_zalloc(V_tcp_reass_zone, M_NOWAIT);
185 	if (te == NULL) {
186 		TCPSTAT_INC(tcps_rcvmemdrop);
187 		m_freem(m);
188 		*tlenp = 0;
189 		return (0);
190 	}
191 	tp->t_segqlen++;
192 	V_tcp_reass_qsize++;
193 
194 	/*
195 	 * Find a segment which begins after this one does.
196 	 */
197 	LIST_FOREACH(q, &tp->t_segq, tqe_q) {
198 		if (SEQ_GT(q->tqe_th->th_seq, th->th_seq))
199 			break;
200 		p = q;
201 	}
202 
203 	/*
204 	 * If there is a preceding segment, it may provide some of
205 	 * our data already.  If so, drop the data from the incoming
206 	 * segment.  If it provides all of our data, drop us.
207 	 */
208 	if (p != NULL) {
209 		int i;
210 		/* conversion to int (in i) handles seq wraparound */
211 		i = p->tqe_th->th_seq + p->tqe_len - th->th_seq;
212 		if (i > 0) {
213 			if (i >= *tlenp) {
214 				TCPSTAT_INC(tcps_rcvduppack);
215 				TCPSTAT_ADD(tcps_rcvdupbyte, *tlenp);
216 				m_freem(m);
217 				uma_zfree(V_tcp_reass_zone, te);
218 				tp->t_segqlen--;
219 				V_tcp_reass_qsize--;
220 				/*
221 				 * Try to present any queued data
222 				 * at the left window edge to the user.
223 				 * This is needed after the 3-WHS
224 				 * completes.
225 				 */
226 				goto present;	/* ??? */
227 			}
228 			m_adj(m, i);
229 			*tlenp -= i;
230 			th->th_seq += i;
231 		}
232 	}
233 	TCPSTAT_INC(tcps_rcvoopack);
234 	TCPSTAT_ADD(tcps_rcvoobyte, *tlenp);
235 
236 	/*
237 	 * While we overlap succeeding segments trim them or,
238 	 * if they are completely covered, dequeue them.
239 	 */
240 	while (q) {
241 		int i = (th->th_seq + *tlenp) - q->tqe_th->th_seq;
242 		if (i <= 0)
243 			break;
244 		if (i < q->tqe_len) {
245 			q->tqe_th->th_seq += i;
246 			q->tqe_len -= i;
247 			m_adj(q->tqe_m, i);
248 			break;
249 		}
250 
251 		nq = LIST_NEXT(q, tqe_q);
252 		LIST_REMOVE(q, tqe_q);
253 		m_freem(q->tqe_m);
254 		uma_zfree(V_tcp_reass_zone, q);
255 		tp->t_segqlen--;
256 		V_tcp_reass_qsize--;
257 		q = nq;
258 	}
259 
260 	/* Insert the new segment queue entry into place. */
261 	te->tqe_m = m;
262 	te->tqe_th = th;
263 	te->tqe_len = *tlenp;
264 
265 	if (p == NULL) {
266 		LIST_INSERT_HEAD(&tp->t_segq, te, tqe_q);
267 	} else {
268 		LIST_INSERT_AFTER(p, te, tqe_q);
269 	}
270 
271 present:
272 	/*
273 	 * Present data to user, advancing rcv_nxt through
274 	 * completed sequence space.
275 	 */
276 	if (!TCPS_HAVEESTABLISHED(tp->t_state))
277 		return (0);
278 	q = LIST_FIRST(&tp->t_segq);
279 	if (!q || q->tqe_th->th_seq != tp->rcv_nxt)
280 		return (0);
281 	SOCKBUF_LOCK(&so->so_rcv);
282 	do {
283 		tp->rcv_nxt += q->tqe_len;
284 		flags = q->tqe_th->th_flags & TH_FIN;
285 		nq = LIST_NEXT(q, tqe_q);
286 		LIST_REMOVE(q, tqe_q);
287 		if (so->so_rcv.sb_state & SBS_CANTRCVMORE)
288 			m_freem(q->tqe_m);
289 		else
290 			sbappendstream_locked(&so->so_rcv, q->tqe_m);
291 		uma_zfree(V_tcp_reass_zone, q);
292 		tp->t_segqlen--;
293 		V_tcp_reass_qsize--;
294 		q = nq;
295 	} while (q && q->tqe_th->th_seq == tp->rcv_nxt);
296 	ND6_HINT(tp);
297 	sorwakeup_locked(so);
298 	return (flags);
299 }
300