]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/netinet/tcp_reass.c
Fix resource exhaustion in TCP reassembly. [SA-15:15]
[FreeBSD/stable/8.git] / sys / netinet / tcp_reass.c
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/route.h>
53 #include <net/vnet.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
77 static int tcp_reass_sysctl_maxseg(SYSCTL_HANDLER_ARGS);
78 static int tcp_reass_sysctl_qsize(SYSCTL_HANDLER_ARGS);
79
80 SYSCTL_NODE(_net_inet_tcp, OID_AUTO, reass, CTLFLAG_RW, 0,
81     "TCP Segment Reassembly Queue");
82
83 static int tcp_reass_maxseg = 0;
84 SYSCTL_VNET_PROC(_net_inet_tcp_reass, OID_AUTO, maxsegments,
85     CTLTYPE_INT | CTLFLAG_RDTUN,
86     &tcp_reass_maxseg, 0, &tcp_reass_sysctl_maxseg, "I",
87     "Global maximum number of TCP Segments in Reassembly Queue");
88
89 static int tcp_reass_qsize = 0;
90 SYSCTL_PROC(_net_inet_tcp_reass, OID_AUTO, cursegments,
91     CTLTYPE_INT | CTLFLAG_RD,
92     &tcp_reass_qsize, 0, &tcp_reass_sysctl_qsize, "I",
93     "Global number of TCP Segments currently in Reassembly Queue");
94
95 static int tcp_reass_overflows = 0;
96 SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, overflows,
97     CTLTYPE_INT | CTLFLAG_RD,
98     &tcp_reass_overflows, 0,
99     "Global number of TCP Segment Reassembly Queue Overflows");
100
101 static uma_zone_t tcp_reass_zone;
102
103 /* Initialize TCP reassembly queue */
104 static void
105 tcp_reass_zone_change(void *tag)
106 {
107
108         tcp_reass_maxseg = nmbclusters / 16;
109         uma_zone_set_max(tcp_reass_zone, tcp_reass_maxseg);
110 }
111
112 void
113 tcp_reass_global_init(void)
114 {
115
116         tcp_reass_maxseg = nmbclusters / 16;
117         TUNABLE_INT_FETCH("net.inet.tcp.reass.maxsegments",
118             &tcp_reass_maxseg);
119         tcp_reass_zone = uma_zcreate("tcpreass", sizeof (struct tseg_qent),
120             NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
121         uma_zone_set_max(tcp_reass_zone, 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 static int
146 tcp_reass_sysctl_maxseg(SYSCTL_HANDLER_ARGS)
147 {
148         tcp_reass_maxseg = uma_zone_get_max(tcp_reass_zone);
149         return (sysctl_handle_int(oidp, arg1, arg2, req));
150 }
151
152 static int
153 tcp_reass_sysctl_qsize(SYSCTL_HANDLER_ARGS)
154 {
155         tcp_reass_qsize = uma_zone_get_cur(tcp_reass_zone);
156         return (sysctl_handle_int(oidp, arg1, arg2, req));
157 }
158
159 int
160 tcp_reass(struct tcpcb *tp, struct tcphdr *th, int *tlenp, struct mbuf *m)
161 {
162         struct tseg_qent *q;
163         struct tseg_qent *p = NULL;
164         struct tseg_qent *nq;
165         struct tseg_qent *te = NULL;
166         struct socket *so = tp->t_inpcb->inp_socket;
167         char *s = NULL;
168         int flags;
169         struct tseg_qent tqs;
170
171         INP_WLOCK_ASSERT(tp->t_inpcb);
172
173         /*
174          * XXX: tcp_reass() is rather inefficient with its data structures
175          * and should be rewritten (see NetBSD for optimizations).
176          */
177
178         /*
179          * Call with th==NULL after become established to
180          * force pre-ESTABLISHED data up to user socket.
181          */
182         if (th == NULL)
183                 goto present;
184
185         /*
186          * Limit the number of segments that can be queued to reduce the
187          * potential for mbuf exhaustion. For best performance, we want to be
188          * able to queue a full window's worth of segments. The size of the
189          * socket receive buffer determines our advertised window and grows
190          * automatically when socket buffer autotuning is enabled. Use it as the
191          * basis for our queue limit.
192          * Always let the missing segment through which caused this queue.
193          * NB: Access to the socket buffer is left intentionally unlocked as we
194          * can tolerate stale information here.
195          *
196          * XXXLAS: Using sbspace(so->so_rcv) instead of so->so_rcv.sb_hiwat
197          * should work but causes packets to be dropped when they shouldn't.
198          * Investigate why and re-evaluate the below limit after the behaviour
199          * is understood.
200          */
201         if ((th->th_seq != tp->rcv_nxt || !TCPS_HAVEESTABLISHED(tp->t_state)) &&
202             tp->t_segqlen >= (so->so_rcv.sb_hiwat / tp->t_maxseg) + 1) {
203                 tcp_reass_overflows++;
204                 TCPSTAT_INC(tcps_rcvmemdrop);
205                 m_freem(m);
206                 *tlenp = 0;
207                 if ((s = tcp_log_addrs(&tp->t_inpcb->inp_inc, th, NULL, NULL))) {
208                         log(LOG_DEBUG, "%s; %s: queue limit reached, "
209                             "segment dropped\n", s, __func__);
210                         free(s, M_TCPLOG);
211                 }
212                 return (0);
213         }
214
215         /*
216          * Allocate a new queue entry. If we can't, or hit the zone limit
217          * just drop the pkt.
218          *
219          * Use a temporary structure on the stack for the missing segment
220          * when the zone is exhausted. Otherwise we may get stuck.
221          */
222         te = uma_zalloc(tcp_reass_zone, M_NOWAIT);
223         if (te == NULL) {
224                 if (th->th_seq != tp->rcv_nxt || !TCPS_HAVEESTABLISHED(tp->t_state)) {
225                         TCPSTAT_INC(tcps_rcvmemdrop);
226                         m_freem(m);
227                         *tlenp = 0;
228                         if ((s = tcp_log_addrs(&tp->t_inpcb->inp_inc, th, NULL,
229                             NULL))) {
230                                 log(LOG_DEBUG, "%s; %s: global zone limit "
231                                     "reached, segment dropped\n", s, __func__);
232                                 free(s, M_TCPLOG);
233                         }
234                         return (0);
235                 } else {
236                         bzero(&tqs, sizeof(struct tseg_qent));
237                         te = &tqs;
238                         if ((s = tcp_log_addrs(&tp->t_inpcb->inp_inc, th, NULL,
239                             NULL))) {
240                                 log(LOG_DEBUG,
241                                     "%s; %s: global zone limit reached, using "
242                                     "stack for missing segment\n", s, __func__);
243                                 free(s, M_TCPLOG);
244                         }
245                 }
246         }
247         tp->t_segqlen++;
248
249         /*
250          * Find a segment which begins after this one does.
251          */
252         LIST_FOREACH(q, &tp->t_segq, tqe_q) {
253                 if (SEQ_GT(q->tqe_th->th_seq, th->th_seq))
254                         break;
255                 p = q;
256         }
257
258         /*
259          * If there is a preceding segment, it may provide some of
260          * our data already.  If so, drop the data from the incoming
261          * segment.  If it provides all of our data, drop us.
262          */
263         if (p != NULL) {
264                 int i;
265                 /* conversion to int (in i) handles seq wraparound */
266                 i = p->tqe_th->th_seq + p->tqe_len - th->th_seq;
267                 if (i > 0) {
268                         if (i >= *tlenp) {
269                                 TCPSTAT_INC(tcps_rcvduppack);
270                                 TCPSTAT_ADD(tcps_rcvdupbyte, *tlenp);
271                                 m_freem(m);
272                                 if (te != &tqs)
273                                         uma_zfree(tcp_reass_zone, te);
274                                 tp->t_segqlen--;
275                                 /*
276                                  * Try to present any queued data
277                                  * at the left window edge to the user.
278                                  * This is needed after the 3-WHS
279                                  * completes.
280                                  */
281                                 goto present;   /* ??? */
282                         }
283                         m_adj(m, i);
284                         *tlenp -= i;
285                         th->th_seq += i;
286                 }
287         }
288         tp->t_rcvoopack++;
289         TCPSTAT_INC(tcps_rcvoopack);
290         TCPSTAT_ADD(tcps_rcvoobyte, *tlenp);
291
292         /*
293          * While we overlap succeeding segments trim them or,
294          * if they are completely covered, dequeue them.
295          */
296         while (q) {
297                 int i = (th->th_seq + *tlenp) - q->tqe_th->th_seq;
298                 if (i <= 0)
299                         break;
300                 if (i < q->tqe_len) {
301                         q->tqe_th->th_seq += i;
302                         q->tqe_len -= i;
303                         m_adj(q->tqe_m, i);
304                         break;
305                 }
306
307                 nq = LIST_NEXT(q, tqe_q);
308                 LIST_REMOVE(q, tqe_q);
309                 m_freem(q->tqe_m);
310                 uma_zfree(tcp_reass_zone, q);
311                 tp->t_segqlen--;
312                 q = nq;
313         }
314
315         /* Insert the new segment queue entry into place. */
316         te->tqe_m = m;
317         te->tqe_th = th;
318         te->tqe_len = *tlenp;
319
320         if (p == NULL) {
321                 LIST_INSERT_HEAD(&tp->t_segq, te, tqe_q);
322         } else {
323                 KASSERT(te != &tqs, ("%s: temporary stack based entry not "
324                     "first element in queue", __func__));
325                 LIST_INSERT_AFTER(p, te, tqe_q);
326         }
327
328 present:
329         /*
330          * Present data to user, advancing rcv_nxt through
331          * completed sequence space.
332          */
333         if (!TCPS_HAVEESTABLISHED(tp->t_state))
334                 return (0);
335         q = LIST_FIRST(&tp->t_segq);
336         if (!q || q->tqe_th->th_seq != tp->rcv_nxt)
337                 return (0);
338         SOCKBUF_LOCK(&so->so_rcv);
339         do {
340                 tp->rcv_nxt += q->tqe_len;
341                 flags = q->tqe_th->th_flags & TH_FIN;
342                 nq = LIST_NEXT(q, tqe_q);
343                 LIST_REMOVE(q, tqe_q);
344                 if (so->so_rcv.sb_state & SBS_CANTRCVMORE)
345                         m_freem(q->tqe_m);
346                 else
347                         sbappendstream_locked(&so->so_rcv, q->tqe_m);
348                 if (q != &tqs)
349                         uma_zfree(tcp_reass_zone, q);
350                 tp->t_segqlen--;
351                 q = nq;
352         } while (q && q->tqe_th->th_seq == tp->rcv_nxt);
353         ND6_HINT(tp);
354         sorwakeup_locked(so);
355         return (flags);
356 }