2 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 * @(#)tcp_input.c 8.12 (Berkeley) 5/24/95
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
36 #include "opt_inet6.h"
37 #include "opt_tcpdebug.h"
39 #include <sys/param.h>
40 #include <sys/kernel.h>
41 #include <sys/malloc.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>
52 #include <net/route.h>
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>
74 #include <netinet/tcp_debug.h>
77 static int tcp_reass_sysctl_qsize(SYSCTL_HANDLER_ARGS);
79 static SYSCTL_NODE(_net_inet_tcp, OID_AUTO, reass, CTLFLAG_RW, 0,
80 "TCP Segment Reassembly Queue");
82 static VNET_DEFINE(int, tcp_reass_maxseg) = 0;
83 #define V_tcp_reass_maxseg VNET(tcp_reass_maxseg)
84 SYSCTL_VNET_INT(_net_inet_tcp_reass, OID_AUTO, maxsegments, CTLFLAG_RDTUN,
85 &VNET_NAME(tcp_reass_maxseg), 0,
86 "Global maximum number of TCP Segments in Reassembly Queue");
88 SYSCTL_VNET_PROC(_net_inet_tcp_reass, OID_AUTO, cursegments,
89 (CTLTYPE_INT | CTLFLAG_RD), NULL, 0, &tcp_reass_sysctl_qsize, "I",
90 "Global number of TCP Segments currently in Reassembly Queue");
92 static VNET_DEFINE(int, tcp_reass_overflows) = 0;
93 #define V_tcp_reass_overflows VNET(tcp_reass_overflows)
94 SYSCTL_VNET_INT(_net_inet_tcp_reass, OID_AUTO, overflows,
95 CTLTYPE_INT | CTLFLAG_RD,
96 &VNET_NAME(tcp_reass_overflows), 0,
97 "Global number of TCP Segment Reassembly Queue Overflows");
99 static VNET_DEFINE(uma_zone_t, tcp_reass_zone);
100 #define V_tcp_reass_zone VNET(tcp_reass_zone)
102 /* Initialize TCP reassembly queue */
104 tcp_reass_zone_change(void *tag)
107 /* Set the zone limit and read back the effective value. */
108 V_tcp_reass_maxseg = nmbclusters / 16;
109 V_tcp_reass_maxseg = uma_zone_set_max(V_tcp_reass_zone,
117 V_tcp_reass_maxseg = nmbclusters / 16;
118 TUNABLE_INT_FETCH("net.inet.tcp.reass.maxsegments",
119 &V_tcp_reass_maxseg);
120 V_tcp_reass_zone = uma_zcreate("tcpreass", sizeof (struct tseg_qent),
121 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
122 /* Set the zone limit and read back the effective value. */
123 V_tcp_reass_maxseg = uma_zone_set_max(V_tcp_reass_zone,
125 EVENTHANDLER_REGISTER(nmbclusters_change,
126 tcp_reass_zone_change, NULL, EVENTHANDLER_PRI_ANY);
131 tcp_reass_destroy(void)
134 uma_zdestroy(V_tcp_reass_zone);
139 tcp_reass_flush(struct tcpcb *tp)
141 struct tseg_qent *qe;
143 INP_WLOCK_ASSERT(tp->t_inpcb);
145 while ((qe = LIST_FIRST(&tp->t_segq)) != NULL) {
146 LIST_REMOVE(qe, tqe_q);
148 uma_zfree(V_tcp_reass_zone, qe);
152 KASSERT((tp->t_segqlen == 0),
153 ("TCP reass queue %p segment count is %d instead of 0 after flush.",
158 tcp_reass_sysctl_qsize(SYSCTL_HANDLER_ARGS)
162 qsize = uma_zone_get_cur(V_tcp_reass_zone);
163 return (sysctl_handle_int(oidp, &qsize, 0, req));
167 tcp_reass(struct tcpcb *tp, struct tcphdr *th, int *tlenp, struct mbuf *m)
170 struct tseg_qent *p = NULL;
171 struct tseg_qent *nq;
172 struct tseg_qent *te = NULL;
173 struct socket *so = tp->t_inpcb->inp_socket;
176 struct tseg_qent tqs;
178 INP_WLOCK_ASSERT(tp->t_inpcb);
181 * XXX: tcp_reass() is rather inefficient with its data structures
182 * and should be rewritten (see NetBSD for optimizations).
186 * Call with th==NULL after become established to
187 * force pre-ESTABLISHED data up to user socket.
193 * Limit the number of segments that can be queued to reduce the
194 * potential for mbuf exhaustion. For best performance, we want to be
195 * able to queue a full window's worth of segments. The size of the
196 * socket receive buffer determines our advertised window and grows
197 * automatically when socket buffer autotuning is enabled. Use it as the
198 * basis for our queue limit.
199 * Always let the missing segment through which caused this queue.
200 * NB: Access to the socket buffer is left intentionally unlocked as we
201 * can tolerate stale information here.
203 * XXXLAS: Using sbspace(so->so_rcv) instead of so->so_rcv.sb_hiwat
204 * should work but causes packets to be dropped when they shouldn't.
205 * Investigate why and re-evaluate the below limit after the behaviour
208 if (th->th_seq != tp->rcv_nxt &&
209 tp->t_segqlen >= (so->so_rcv.sb_hiwat / tp->t_maxseg) + 1) {
210 V_tcp_reass_overflows++;
211 TCPSTAT_INC(tcps_rcvmemdrop);
214 if ((s = tcp_log_addrs(&tp->t_inpcb->inp_inc, th, NULL, NULL))) {
215 log(LOG_DEBUG, "%s; %s: queue limit reached, "
216 "segment dropped\n", s, __func__);
223 * Allocate a new queue entry. If we can't, or hit the zone limit
226 * Use a temporary structure on the stack for the missing segment
227 * when the zone is exhausted. Otherwise we may get stuck.
229 te = uma_zalloc(V_tcp_reass_zone, M_NOWAIT);
231 if (th->th_seq != tp->rcv_nxt) {
232 TCPSTAT_INC(tcps_rcvmemdrop);
235 if ((s = tcp_log_addrs(&tp->t_inpcb->inp_inc, th, NULL,
237 log(LOG_DEBUG, "%s; %s: global zone limit "
238 "reached, segment dropped\n", s, __func__);
243 bzero(&tqs, sizeof(struct tseg_qent));
245 if ((s = tcp_log_addrs(&tp->t_inpcb->inp_inc, th, NULL,
248 "%s; %s: global zone limit reached, using "
249 "stack for missing segment\n", s, __func__);
257 * Find a segment which begins after this one does.
259 LIST_FOREACH(q, &tp->t_segq, tqe_q) {
260 if (SEQ_GT(q->tqe_th->th_seq, th->th_seq))
266 * If there is a preceding segment, it may provide some of
267 * our data already. If so, drop the data from the incoming
268 * segment. If it provides all of our data, drop us.
272 /* conversion to int (in i) handles seq wraparound */
273 i = p->tqe_th->th_seq + p->tqe_len - th->th_seq;
276 TCPSTAT_INC(tcps_rcvduppack);
277 TCPSTAT_ADD(tcps_rcvdupbyte, *tlenp);
279 uma_zfree(V_tcp_reass_zone, te);
282 * Try to present any queued data
283 * at the left window edge to the user.
284 * This is needed after the 3-WHS
287 goto present; /* ??? */
295 TCPSTAT_INC(tcps_rcvoopack);
296 TCPSTAT_ADD(tcps_rcvoobyte, *tlenp);
299 * While we overlap succeeding segments trim them or,
300 * if they are completely covered, dequeue them.
303 int i = (th->th_seq + *tlenp) - q->tqe_th->th_seq;
306 if (i < q->tqe_len) {
307 q->tqe_th->th_seq += i;
313 nq = LIST_NEXT(q, tqe_q);
314 LIST_REMOVE(q, tqe_q);
316 uma_zfree(V_tcp_reass_zone, q);
321 /* Insert the new segment queue entry into place. */
324 te->tqe_len = *tlenp;
327 LIST_INSERT_HEAD(&tp->t_segq, te, tqe_q);
329 KASSERT(te != &tqs, ("%s: temporary stack based entry not "
330 "first element in queue", __func__));
331 LIST_INSERT_AFTER(p, te, tqe_q);
336 * Present data to user, advancing rcv_nxt through
337 * completed sequence space.
339 if (!TCPS_HAVEESTABLISHED(tp->t_state))
341 q = LIST_FIRST(&tp->t_segq);
342 if (!q || q->tqe_th->th_seq != tp->rcv_nxt)
344 SOCKBUF_LOCK(&so->so_rcv);
346 tp->rcv_nxt += q->tqe_len;
347 flags = q->tqe_th->th_flags & TH_FIN;
348 nq = LIST_NEXT(q, tqe_q);
349 LIST_REMOVE(q, tqe_q);
350 if (so->so_rcv.sb_state & SBS_CANTRCVMORE)
353 sbappendstream_locked(&so->so_rcv, q->tqe_m);
355 uma_zfree(V_tcp_reass_zone, q);
358 } while (q && q->tqe_th->th_seq == tp->rcv_nxt);
360 sorwakeup_locked(so);