]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/netinet/tcp_reass.c
MFC r213912:
[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 VNET_DEFINE(int, tcp_reass_maxseg) = 0;
84 #define V_tcp_reass_maxseg              VNET(tcp_reass_maxseg)
85 SYSCTL_VNET_PROC(_net_inet_tcp_reass, OID_AUTO, maxsegments, CTLFLAG_RDTUN,
86     &VNET_NAME(tcp_reass_maxseg), 0, &tcp_reass_sysctl_maxseg, "I",
87     "Global maximum number of TCP Segments in Reassembly Queue");
88
89 static VNET_DEFINE(int, tcp_reass_qsize) = 0;
90 #define V_tcp_reass_qsize               VNET(tcp_reass_qsize)
91 SYSCTL_VNET_PROC(_net_inet_tcp_reass, OID_AUTO, cursegments, CTLFLAG_RD,
92     &VNET_NAME(tcp_reass_qsize), 0, &tcp_reass_sysctl_qsize, "I",
93     "Global number of TCP Segments currently in Reassembly Queue");
94
95 static VNET_DEFINE(int, tcp_reass_maxqlen) = 48;
96 #define V_tcp_reass_maxqlen             VNET(tcp_reass_maxqlen)
97 SYSCTL_VNET_INT(_net_inet_tcp_reass, OID_AUTO, maxqlen, CTLFLAG_RW,
98     &VNET_NAME(tcp_reass_maxqlen), 0,
99     "Maximum number of TCP Segments per individual Reassembly Queue");
100
101 static VNET_DEFINE(int, tcp_reass_overflows) = 0;
102 #define V_tcp_reass_overflows           VNET(tcp_reass_overflows)
103 SYSCTL_VNET_INT(_net_inet_tcp_reass, OID_AUTO, overflows, CTLFLAG_RD,
104     &VNET_NAME(tcp_reass_overflows), 0,
105     "Global number of TCP Segment Reassembly Queue Overflows");
106
107 static VNET_DEFINE(uma_zone_t, tcp_reass_zone);
108 #define V_tcp_reass_zone                VNET(tcp_reass_zone)
109
110 /* Initialize TCP reassembly queue */
111 static void
112 tcp_reass_zone_change(void *tag)
113 {
114
115         V_tcp_reass_maxseg = nmbclusters / 16;
116         uma_zone_set_max(V_tcp_reass_zone, V_tcp_reass_maxseg);
117 }
118
119 void
120 tcp_reass_init(void)
121 {
122
123         V_tcp_reass_maxseg = nmbclusters / 16;
124         TUNABLE_INT_FETCH("net.inet.tcp.reass.maxsegments",
125             &V_tcp_reass_maxseg);
126         V_tcp_reass_zone = uma_zcreate("tcpreass", sizeof (struct tseg_qent),
127             NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
128         uma_zone_set_max(V_tcp_reass_zone, V_tcp_reass_maxseg);
129         EVENTHANDLER_REGISTER(nmbclusters_change,
130             tcp_reass_zone_change, NULL, EVENTHANDLER_PRI_ANY);
131 }
132
133 #ifdef VIMAGE
134 void
135 tcp_reass_destroy(void)
136 {
137
138         uma_zdestroy(V_tcp_reass_zone);
139 }
140 #endif
141
142 void
143 tcp_reass_flush(struct tcpcb *tp)
144 {
145         struct tseg_qent *qe;
146
147         INP_WLOCK_ASSERT(tp->t_inpcb);
148
149         while ((qe = LIST_FIRST(&tp->t_segq)) != NULL) {
150                 LIST_REMOVE(qe, tqe_q);
151                 m_freem(qe->tqe_m);
152                 uma_zfree(V_tcp_reass_zone, qe);
153                 tp->t_segqlen--;
154         }
155
156         KASSERT((tp->t_segqlen == 0),
157             ("TCP reass queue %p segment count is %d instead of 0 after flush.",
158             tp, tp->t_segqlen));
159 }
160
161 static int
162 tcp_reass_sysctl_maxseg(SYSCTL_HANDLER_ARGS)
163 {
164         V_tcp_reass_maxseg = uma_zone_get_max(V_tcp_reass_zone);
165         return (sysctl_handle_int(oidp, arg1, arg2, req));
166 }
167
168 static int
169 tcp_reass_sysctl_qsize(SYSCTL_HANDLER_ARGS)
170 {
171         V_tcp_reass_qsize = uma_zone_get_cur(V_tcp_reass_zone);
172         return (sysctl_handle_int(oidp, arg1, arg2, req));
173 }
174
175 int
176 tcp_reass(struct tcpcb *tp, struct tcphdr *th, int *tlenp, struct mbuf *m)
177 {
178         struct tseg_qent *q;
179         struct tseg_qent *p = NULL;
180         struct tseg_qent *nq;
181         struct tseg_qent *te = NULL;
182         struct socket *so = tp->t_inpcb->inp_socket;
183         int flags;
184
185         INP_WLOCK_ASSERT(tp->t_inpcb);
186
187         /*
188          * XXX: tcp_reass() is rather inefficient with its data structures
189          * and should be rewritten (see NetBSD for optimizations).
190          */
191
192         /*
193          * Call with th==NULL after become established to
194          * force pre-ESTABLISHED data up to user socket.
195          */
196         if (th == NULL)
197                 goto present;
198
199         /*
200          * Limit the number of segments in the reassembly queue to prevent
201          * holding on to too many segments (and thus running out of mbufs).
202          * Make sure to let the missing segment through which caused this
203          * queue.
204          */
205         if (th->th_seq != tp->rcv_nxt &&
206             tp->t_segqlen >= V_tcp_reass_maxqlen) {
207                 V_tcp_reass_overflows++;
208                 TCPSTAT_INC(tcps_rcvmemdrop);
209                 m_freem(m);
210                 *tlenp = 0;
211                 return (0);
212         }
213
214         /*
215          * Allocate a new queue entry. If we can't, or hit the zone limit
216          * just drop the pkt.
217          */
218         te = uma_zalloc(V_tcp_reass_zone, M_NOWAIT);
219         if (te == NULL) {
220                 TCPSTAT_INC(tcps_rcvmemdrop);
221                 m_freem(m);
222                 *tlenp = 0;
223                 return (0);
224         }
225         tp->t_segqlen++;
226
227         /*
228          * Find a segment which begins after this one does.
229          */
230         LIST_FOREACH(q, &tp->t_segq, tqe_q) {
231                 if (SEQ_GT(q->tqe_th->th_seq, th->th_seq))
232                         break;
233                 p = q;
234         }
235
236         /*
237          * If there is a preceding segment, it may provide some of
238          * our data already.  If so, drop the data from the incoming
239          * segment.  If it provides all of our data, drop us.
240          */
241         if (p != NULL) {
242                 int i;
243                 /* conversion to int (in i) handles seq wraparound */
244                 i = p->tqe_th->th_seq + p->tqe_len - th->th_seq;
245                 if (i > 0) {
246                         if (i >= *tlenp) {
247                                 TCPSTAT_INC(tcps_rcvduppack);
248                                 TCPSTAT_ADD(tcps_rcvdupbyte, *tlenp);
249                                 m_freem(m);
250                                 uma_zfree(V_tcp_reass_zone, te);
251                                 tp->t_segqlen--;
252                                 /*
253                                  * Try to present any queued data
254                                  * at the left window edge to the user.
255                                  * This is needed after the 3-WHS
256                                  * completes.
257                                  */
258                                 goto present;   /* ??? */
259                         }
260                         m_adj(m, i);
261                         *tlenp -= i;
262                         th->th_seq += i;
263                 }
264         }
265         TCPSTAT_INC(tcps_rcvoopack);
266         TCPSTAT_ADD(tcps_rcvoobyte, *tlenp);
267
268         /*
269          * While we overlap succeeding segments trim them or,
270          * if they are completely covered, dequeue them.
271          */
272         while (q) {
273                 int i = (th->th_seq + *tlenp) - q->tqe_th->th_seq;
274                 if (i <= 0)
275                         break;
276                 if (i < q->tqe_len) {
277                         q->tqe_th->th_seq += i;
278                         q->tqe_len -= i;
279                         m_adj(q->tqe_m, i);
280                         break;
281                 }
282
283                 nq = LIST_NEXT(q, tqe_q);
284                 LIST_REMOVE(q, tqe_q);
285                 m_freem(q->tqe_m);
286                 uma_zfree(V_tcp_reass_zone, q);
287                 tp->t_segqlen--;
288                 q = nq;
289         }
290
291         /* Insert the new segment queue entry into place. */
292         te->tqe_m = m;
293         te->tqe_th = th;
294         te->tqe_len = *tlenp;
295
296         if (p == NULL) {
297                 LIST_INSERT_HEAD(&tp->t_segq, te, tqe_q);
298         } else {
299                 LIST_INSERT_AFTER(p, te, tqe_q);
300         }
301
302 present:
303         /*
304          * Present data to user, advancing rcv_nxt through
305          * completed sequence space.
306          */
307         if (!TCPS_HAVEESTABLISHED(tp->t_state))
308                 return (0);
309         q = LIST_FIRST(&tp->t_segq);
310         if (!q || q->tqe_th->th_seq != tp->rcv_nxt)
311                 return (0);
312         SOCKBUF_LOCK(&so->so_rcv);
313         do {
314                 tp->rcv_nxt += q->tqe_len;
315                 flags = q->tqe_th->th_flags & TH_FIN;
316                 nq = LIST_NEXT(q, tqe_q);
317                 LIST_REMOVE(q, tqe_q);
318                 if (so->so_rcv.sb_state & SBS_CANTRCVMORE)
319                         m_freem(q->tqe_m);
320                 else
321                         sbappendstream_locked(&so->so_rcv, q->tqe_m);
322                 uma_zfree(V_tcp_reass_zone, q);
323                 tp->t_segqlen--;
324                 q = nq;
325         } while (q && q->tqe_th->th_seq == tp->rcv_nxt);
326         ND6_HINT(tp);
327         sorwakeup_locked(so);
328         return (flags);
329 }