tcp: Dragging RescueRxt along with HighRxt should depend on tcp.rescuesack_agg
[dragonfly.git] / sys / netinet / tcp_input.c
CommitLineData
984263bc 1/*
66d6c637
JH
2 * Copyright (c) 2002, 2003, 2004 Jeffrey M. Hsu. All rights reserved.
3 * Copyright (c) 2002, 2003, 2004 The DragonFly Project. All rights reserved.
95b22adf 4 *
66d6c637
JH
5 * This code is derived from software contributed to The DragonFly Project
6 * by Jeffrey M. Hsu.
95b22adf 7 *
66d6c637
JH
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of The DragonFly Project nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific, prior written permission.
95b22adf 19 *
66d6c637
JH
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
66d6c637 34/*
984263bc
MD
35 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
36 * The Regents of the University of California. All rights reserved.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * SUCH DAMAGE.
65 *
66 * @(#)tcp_input.c 8.12 (Berkeley) 5/24/95
67 * $FreeBSD: src/sys/netinet/tcp_input.c,v 1.107.2.38 2003/05/21 04:46:41 cjc Exp $
68 */
69
b1992928 70#include "opt_inet.h"
984263bc
MD
71#include "opt_inet6.h"
72#include "opt_ipsec.h"
73#include "opt_tcpdebug.h"
74#include "opt_tcp_input.h"
75
76#include <sys/param.h>
77#include <sys/systm.h>
78#include <sys/kernel.h>
79#include <sys/sysctl.h>
80#include <sys/malloc.h>
81#include <sys/mbuf.h>
82#include <sys/proc.h> /* for proc0 declaration */
83#include <sys/protosw.h>
84#include <sys/socket.h>
85#include <sys/socketvar.h>
86#include <sys/syslog.h>
3f9db7f8 87#include <sys/in_cksum.h>
984263bc 88
6cef7136
MD
89#include <sys/socketvar2.h>
90
984263bc 91#include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */
a00138cb 92#include <machine/stdarg.h>
984263bc
MD
93
94#include <net/if.h>
95#include <net/route.h>
96
97#include <netinet/in.h>
98#include <netinet/in_systm.h>
99#include <netinet/ip.h>
95b22adf 100#include <netinet/ip_icmp.h> /* for ICMP_BANDLIM */
984263bc 101#include <netinet/in_var.h>
95b22adf 102#include <netinet/icmp_var.h> /* for ICMP_BANDLIM */
984263bc
MD
103#include <netinet/in_pcb.h>
104#include <netinet/ip_var.h>
105#include <netinet/ip6.h>
106#include <netinet/icmp6.h>
107#include <netinet6/nd6.h>
108#include <netinet6/ip6_var.h>
109#include <netinet6/in6_pcb.h>
110#include <netinet/tcp.h>
111#include <netinet/tcp_fsm.h>
112#include <netinet/tcp_seq.h>
113#include <netinet/tcp_timer.h>
a48c5dd5 114#include <netinet/tcp_timer2.h>
984263bc
MD
115#include <netinet/tcp_var.h>
116#include <netinet6/tcp6_var.h>
117#include <netinet/tcpip.h>
95b22adf 118
984263bc
MD
119#ifdef TCPDEBUG
120#include <netinet/tcp_debug.h>
121
95b22adf 122u_char tcp_saveipgen[40]; /* the size must be of max ip header, now IPv6 */
984263bc 123struct tcphdr tcp_savetcp;
95b22adf 124#endif
984263bc
MD
125
126#ifdef FAST_IPSEC
bf844ffa
JH
127#include <netproto/ipsec/ipsec.h>
128#include <netproto/ipsec/ipsec6.h>
984263bc
MD
129#endif
130
131#ifdef IPSEC
132#include <netinet6/ipsec.h>
133#include <netinet6/ipsec6.h>
d2438d69 134#include <netproto/key/key.h>
95b22adf 135#endif
984263bc 136
984263bc
MD
137MALLOC_DEFINE(M_TSEGQ, "tseg_qent", "TCP segment queue entry");
138
984263bc 139static int log_in_vain = 0;
d24ce1dc 140SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW,
984263bc
MD
141 &log_in_vain, 0, "Log all incoming TCP connections");
142
143static int blackhole = 0;
144SYSCTL_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_RW,
145 &blackhole, 0, "Do not send RST when dropping refused connections");
146
147int tcp_delack_enabled = 1;
d24ce1dc
JH
148SYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_RW,
149 &tcp_delack_enabled, 0,
984263bc
MD
150 "Delay ACK to try and piggyback it onto a data packet");
151
152#ifdef TCP_DROP_SYNFIN
153static int drop_synfin = 0;
154SYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_RW,
155 &drop_synfin, 0, "Drop TCP packets with SYN+FIN set");
156#endif
157
33abdd1c
MD
158static int tcp_do_limitedtransmit = 1;
159SYSCTL_INT(_net_inet_tcp, OID_AUTO, limitedtransmit, CTLFLAG_RW,
160 &tcp_do_limitedtransmit, 0, "Enable RFC 3042 (Limited Transmit)");
161
91489f6b 162static int tcp_do_early_retransmit = 1;
8819433a
JH
163SYSCTL_INT(_net_inet_tcp, OID_AUTO, earlyretransmit, CTLFLAG_RW,
164 &tcp_do_early_retransmit, 0, "Early retransmit");
165
8acdb67c 166int tcp_aggregate_acks = 1;
72b37eeb
MD
167SYSCTL_INT(_net_inet_tcp, OID_AUTO, aggregate_acks, CTLFLAG_RW,
168 &tcp_aggregate_acks, 0, "Aggregate built-up acks into one ack");
169
efd4b327
JH
170static int tcp_do_eifel_detect = 1;
171SYSCTL_INT(_net_inet_tcp, OID_AUTO, eifel, CTLFLAG_RW,
172 &tcp_do_eifel_detect, 0, "Eifel detection algorithm (RFC 3522)");
173
8acdb67c
JH
174static int tcp_do_abc = 1;
175SYSCTL_INT(_net_inet_tcp, OID_AUTO, abc, CTLFLAG_RW,
176 &tcp_do_abc, 0,
177 "TCP Appropriate Byte Counting (RFC 3465)");
178
91489f6b
JH
179/*
180 * Define as tunable for easy testing with SACK on and off.
181 * Warning: do not change setting in the middle of an existing active TCP flow,
182 * else strange things might happen to that flow.
183 */
6e5bbdda 184int tcp_do_sack = 1;
91489f6b
JH
185SYSCTL_INT(_net_inet_tcp, OID_AUTO, sack, CTLFLAG_RW,
186 &tcp_do_sack, 0, "Enable SACK Algorithms");
187
188int tcp_do_smartsack = 1;
189SYSCTL_INT(_net_inet_tcp, OID_AUTO, smartsack, CTLFLAG_RW,
190 &tcp_do_smartsack, 0, "Enable Smart SACK Algorithms");
191
1bdfd728
SZ
192int tcp_do_rescuesack = 1;
193SYSCTL_INT(_net_inet_tcp, OID_AUTO, rescuesack, CTLFLAG_RW,
194 &tcp_do_rescuesack, 0, "Rescue retransmission for SACK");
195
a098966f
SZ
196int tcp_aggressive_rescuesack = 1;
197SYSCTL_INT(_net_inet_tcp, OID_AUTO, rescuesack_agg, CTLFLAG_RW,
198 &tcp_aggressive_rescuesack, 0, "Aggressive rescue retransmission for SACK");
199
3edf7c37
RG
200SYSCTL_NODE(_net_inet_tcp, OID_AUTO, reass, CTLFLAG_RW, 0,
201 "TCP Segment Reassembly Queue");
202
203int tcp_reass_maxseg = 0;
204SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, maxsegments, CTLFLAG_RD,
205 &tcp_reass_maxseg, 0,
206 "Global maximum number of TCP Segments in Reassembly Queue");
207
208int tcp_reass_qsize = 0;
209SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, cursegments, CTLFLAG_RD,
210 &tcp_reass_qsize, 0,
211 "Global number of TCP Segments currently in Reassembly Queue");
212
213static int tcp_reass_overflows = 0;
214SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, overflows, CTLFLAG_RD,
215 &tcp_reass_overflows, 0,
216 "Global number of TCP Segment Reassembly Queue Overflows");
217
5b0b9fa5
PA
218int tcp_do_autorcvbuf = 1;
219SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_auto, CTLFLAG_RW,
220 &tcp_do_autorcvbuf, 0, "Enable automatic receive buffer sizing");
221
222int tcp_autorcvbuf_inc = 16*1024;
223SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_inc, CTLFLAG_RW,
224 &tcp_autorcvbuf_inc, 0,
225 "Incrementor step size of automatic receive buffer");
226
46e92930 227int tcp_autorcvbuf_max = 2*1024*1024;
5b0b9fa5
PA
228SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_max, CTLFLAG_RW,
229 &tcp_autorcvbuf_max, 0, "Max size of automatic receive buffer");
230
0df7608b
SZ
231int tcp_sosend_agglim = 2;
232SYSCTL_INT(_net_inet_tcp, OID_AUTO, sosend_agglim, CTLFLAG_RW,
233 &tcp_sosend_agglim, 0, "TCP sosend mbuf aggregation limit");
5b0b9fa5 234
0df7608b
SZ
235int tcp_sosend_async = 1;
236SYSCTL_INT(_net_inet_tcp, OID_AUTO, sosend_async, CTLFLAG_RW,
237 &tcp_sosend_async, 0, "TCP asynchronized pru_send");
f2a3782e 238
12e60b57
SZ
239static int tcp_ignore_redun_dsack = 1;
240SYSCTL_INT(_net_inet_tcp, OID_AUTO, ignore_redun_dsack, CTLFLAG_RW,
241 &tcp_ignore_redun_dsack, 0, "Ignore redundant DSACK");
242
6c1bbf57
SZ
243static void tcp_dooptions(struct tcpopt *, u_char *, int, boolean_t,
244 tcp_seq);
984263bc
MD
245static void tcp_pulloutofband(struct socket *,
246 struct tcphdr *, struct mbuf *, int);
247static int tcp_reass(struct tcpcb *, struct tcphdr *, int *,
248 struct mbuf *);
073ec6c4 249static void tcp_xmit_timer(struct tcpcb *, int, tcp_seq);
91489f6b
JH
250static void tcp_newreno_partial_ack(struct tcpcb *, struct tcphdr *, int);
251static void tcp_sack_rexmt(struct tcpcb *, struct tcphdr *);
01d3427a 252static int tcp_rmx_msl(const struct tcpcb *);
8651f7f8 253static void tcp_established(struct tcpcb *);
984263bc
MD
254
255/* Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. */
256#ifdef INET6
257#define ND6_HINT(tp) \
258do { \
259 if ((tp) && (tp)->t_inpcb && \
61896e3c 260 ((tp)->t_inpcb->inp_vflag & INP_IPV6) && \
984263bc
MD
261 (tp)->t_inpcb->in6p_route.ro_rt) \
262 nd6_nud_hint((tp)->t_inpcb->in6p_route.ro_rt, NULL, 0); \
263} while (0)
264#else
265#define ND6_HINT(tp)
266#endif
267
268/*
269 * Indicate whether this ack should be delayed. We can delay the ack if
270 * - delayed acks are enabled and
271 * - there is no delayed ack timer in progress and
272 * - our last ack wasn't a 0-sized window. We never want to delay
273 * the ack that opens up a 0-sized window.
274 */
275#define DELAY_ACK(tp) \
a48c5dd5 276 (tcp_delack_enabled && !tcp_callout_pending(tp, tp->tt_delack) && \
61896e3c 277 !(tp->t_flags & TF_RXWIN0SENT))
984263bc 278
df9d7670
JH
279#define acceptable_window_update(tp, th, tiwin) \
280 (SEQ_LT(tp->snd_wl1, th->th_seq) || \
281 (tp->snd_wl1 == th->th_seq && \
282 (SEQ_LT(tp->snd_wl2, th->th_ack) || \
283 (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))
284
984263bc 285static int
95b22adf 286tcp_reass(struct tcpcb *tp, struct tcphdr *th, int *tlenp, struct mbuf *m)
984263bc
MD
287{
288 struct tseg_qent *q;
289 struct tseg_qent *p = NULL;
984263bc
MD
290 struct tseg_qent *te;
291 struct socket *so = tp->t_inpcb->inp_socket;
292 int flags;
293
294 /*
61896e3c 295 * Call with th == NULL after become established to
984263bc
MD
296 * force pre-ESTABLISHED data up to user socket.
297 */
61896e3c 298 if (th == NULL)
984263bc
MD
299 goto present;
300
3edf7c37
RG
301 /*
302 * Limit the number of segments in the reassembly queue to prevent
303 * holding on to too many segments (and thus running out of mbufs).
304 * Make sure to let the missing segment through which caused this
305 * queue. Always keep one global queue entry spare to be able to
306 * process the missing segment.
307 */
308 if (th->th_seq != tp->rcv_nxt &&
309 tcp_reass_qsize + 1 >= tcp_reass_maxseg) {
310 tcp_reass_overflows++;
311 tcpstat.tcps_rcvmemdrop++;
312 m_freem(m);
91489f6b
JH
313 /* no SACK block to report */
314 tp->reportblk.rblk_start = tp->reportblk.rblk_end;
3edf7c37
RG
315 return (0);
316 }
317
ba4e3dbe 318 /* Allocate a new queue entry. */
884717e1 319 te = kmalloc(sizeof(struct tseg_qent), M_TSEGQ, M_INTWAIT | M_NULLOK);
984263bc
MD
320 if (te == NULL) {
321 tcpstat.tcps_rcvmemdrop++;
322 m_freem(m);
91489f6b
JH
323 /* no SACK block to report */
324 tp->reportblk.rblk_start = tp->reportblk.rblk_end;
984263bc
MD
325 return (0);
326 }
2d23a8be 327 atomic_add_int(&tcp_reass_qsize, 1);
984263bc
MD
328
329 /*
330 * Find a segment which begins after this one does.
331 */
332 LIST_FOREACH(q, &tp->t_segq, tqe_q) {
333 if (SEQ_GT(q->tqe_th->th_seq, th->th_seq))
334 break;
335 p = q;
336 }
337
338 /*
339 * If there is a preceding segment, it may provide some of
340 * our data already. If so, drop the data from the incoming
341 * segment. If it provides all of our data, drop us.
342 */
343 if (p != NULL) {
76cf5e41 344 tcp_seq_diff_t i;
91489f6b 345
984263bc
MD
346 /* conversion to int (in i) handles seq wraparound */
347 i = p->tqe_th->th_seq + p->tqe_len - th->th_seq;
91489f6b
JH
348 if (i > 0) { /* overlaps preceding segment */
349 tp->t_flags |= (TF_DUPSEG | TF_ENCLOSESEG);
350 /* enclosing block starts w/ preceding segment */
351 tp->encloseblk.rblk_start = p->tqe_th->th_seq;
984263bc 352 if (i >= *tlenp) {
91489f6b 353 /* preceding encloses incoming segment */
3a5d999b
SZ
354 tp->encloseblk.rblk_end = TCP_SACK_BLKEND(
355 p->tqe_th->th_seq + p->tqe_len,
356 p->tqe_th->th_flags);
984263bc
MD
357 tcpstat.tcps_rcvduppack++;
358 tcpstat.tcps_rcvdupbyte += *tlenp;
359 m_freem(m);
efda3bd0 360 kfree(te, M_TSEGQ);
2d23a8be 361 atomic_add_int(&tcp_reass_qsize, -1);
984263bc
MD
362 /*
363 * Try to present any queued data
364 * at the left window edge to the user.
365 * This is needed after the 3-WHS
366 * completes.
367 */
368 goto present; /* ??? */
369 }
370 m_adj(m, i);
371 *tlenp -= i;
372 th->th_seq += i;
91489f6b 373 /* incoming segment end is enclosing block end */
3a5d999b
SZ
374 tp->encloseblk.rblk_end = TCP_SACK_BLKEND(
375 th->th_seq + *tlenp, th->th_flags);
91489f6b
JH
376 /* trim end of reported D-SACK block */
377 tp->reportblk.rblk_end = th->th_seq;
984263bc
MD
378 }
379 }
380 tcpstat.tcps_rcvoopack++;
381 tcpstat.tcps_rcvoobyte += *tlenp;
382
383 /*
384 * While we overlap succeeding segments trim them or,
385 * if they are completely covered, dequeue them.
386 */
387 while (q) {
76cf5e41 388 tcp_seq_diff_t i = (th->th_seq + *tlenp) - q->tqe_th->th_seq;
91489f6b 389 tcp_seq qend = q->tqe_th->th_seq + q->tqe_len;
3a5d999b 390 tcp_seq qend_sack = TCP_SACK_BLKEND(qend, q->tqe_th->th_flags);
91489f6b
JH
391 struct tseg_qent *nq;
392
984263bc
MD
393 if (i <= 0)
394 break;
91489f6b
JH
395 if (!(tp->t_flags & TF_DUPSEG)) { /* first time through */
396 tp->t_flags |= (TF_DUPSEG | TF_ENCLOSESEG);
397 tp->encloseblk = tp->reportblk;
398 /* report trailing duplicate D-SACK segment */
399 tp->reportblk.rblk_start = q->tqe_th->th_seq;
400 }
401 if ((tp->t_flags & TF_ENCLOSESEG) &&
3a5d999b 402 SEQ_GT(qend_sack, tp->encloseblk.rblk_end)) {
91489f6b 403 /* extend enclosing block if one exists */
3a5d999b 404 tp->encloseblk.rblk_end = qend_sack;
91489f6b 405 }
984263bc
MD
406 if (i < q->tqe_len) {
407 q->tqe_th->th_seq += i;
408 q->tqe_len -= i;
409 m_adj(q->tqe_m, i);
410 break;
411 }
412
413 nq = LIST_NEXT(q, tqe_q);
414 LIST_REMOVE(q, tqe_q);
415 m_freem(q->tqe_m);
efda3bd0 416 kfree(q, M_TSEGQ);
2d23a8be 417 atomic_add_int(&tcp_reass_qsize, -1);
984263bc
MD
418 q = nq;
419 }
420
421 /* Insert the new segment queue entry into place. */
422 te->tqe_m = m;
423 te->tqe_th = th;
424 te->tqe_len = *tlenp;
425
91489f6b
JH
426 /* check if can coalesce with following segment */
427 if (q != NULL && (th->th_seq + *tlenp == q->tqe_th->th_seq)) {
428 tcp_seq tend = te->tqe_th->th_seq + te->tqe_len;
3a5d999b 429 tcp_seq tend_sack = TCP_SACK_BLKEND(tend, te->tqe_th->th_flags);
91489f6b
JH
430
431 te->tqe_len += q->tqe_len;
a174690a 432 if (q->tqe_th->th_flags & TH_FIN)
f16f8cc3 433 te->tqe_th->th_flags |= TH_FIN;
91489f6b 434 m_cat(te->tqe_m, q->tqe_m);
3a5d999b 435 tp->encloseblk.rblk_end = tend_sack;
91489f6b
JH
436 /*
437 * When not reporting a duplicate segment, use
438 * the larger enclosing block as the SACK block.
439 */
440 if (!(tp->t_flags & TF_DUPSEG))
3a5d999b 441 tp->reportblk.rblk_end = tend_sack;
91489f6b 442 LIST_REMOVE(q, tqe_q);
efda3bd0 443 kfree(q, M_TSEGQ);
2d23a8be 444 atomic_add_int(&tcp_reass_qsize, -1);
91489f6b
JH
445 }
446
984263bc
MD
447 if (p == NULL) {
448 LIST_INSERT_HEAD(&tp->t_segq, te, tqe_q);
449 } else {
91489f6b
JH
450 /* check if can coalesce with preceding segment */
451 if (p->tqe_th->th_seq + p->tqe_len == th->th_seq) {
a174690a
JH
452 p->tqe_len += te->tqe_len;
453 m_cat(p->tqe_m, te->tqe_m);
91489f6b
JH
454 tp->encloseblk.rblk_start = p->tqe_th->th_seq;
455 /*
456 * When not reporting a duplicate segment, use
457 * the larger enclosing block as the SACK block.
458 */
459 if (!(tp->t_flags & TF_DUPSEG))
460 tp->reportblk.rblk_start = p->tqe_th->th_seq;
efda3bd0 461 kfree(te, M_TSEGQ);
2d23a8be
MD
462 atomic_add_int(&tcp_reass_qsize, -1);
463 } else {
91489f6b 464 LIST_INSERT_AFTER(p, te, tqe_q);
2d23a8be 465 }
984263bc
MD
466 }
467
468present:
469 /*
470 * Present data to user, advancing rcv_nxt through
471 * completed sequence space.
472 */
473 if (!TCPS_HAVEESTABLISHED(tp->t_state))
474 return (0);
475 q = LIST_FIRST(&tp->t_segq);
61896e3c 476 if (q == NULL || q->tqe_th->th_seq != tp->rcv_nxt)
984263bc 477 return (0);
91489f6b
JH
478 tp->rcv_nxt += q->tqe_len;
479 if (!(tp->t_flags & TF_DUPSEG)) {
480 /* no SACK block to report since ACK advanced */
481 tp->reportblk.rblk_start = tp->reportblk.rblk_end;
482 }
483 /* no enclosing block to report since ACK advanced */
484 tp->t_flags &= ~TF_ENCLOSESEG;
485 flags = q->tqe_th->th_flags & TH_FIN;
486 LIST_REMOVE(q, tqe_q);
487 KASSERT(LIST_EMPTY(&tp->t_segq) ||
488 LIST_FIRST(&tp->t_segq)->tqe_th->th_seq != tp->rcv_nxt,
489 ("segment not coalesced"));
6cef7136 490 if (so->so_state & SS_CANTRCVMORE) {
91489f6b 491 m_freem(q->tqe_m);
6cef7136
MD
492 } else {
493 lwkt_gettoken(&so->so_rcv.ssb_token);
6d49aa6f 494 ssb_appendstream(&so->so_rcv, q->tqe_m);
6cef7136
MD
495 lwkt_reltoken(&so->so_rcv.ssb_token);
496 }
efda3bd0 497 kfree(q, M_TSEGQ);
2d23a8be 498 atomic_add_int(&tcp_reass_qsize, -1);
984263bc
MD
499 ND6_HINT(tp);
500 sorwakeup(so);
501 return (flags);
502}
503
504/*
505 * TCP input routine, follows pages 65-76 of the
506 * protocol specification dated September, 1981 very closely.
507 */
508#ifdef INET6
509int
95b22adf 510tcp6_input(struct mbuf **mp, int *offp, int proto)
984263bc 511{
2256ba69 512 struct mbuf *m = *mp;
984263bc
MD
513 struct in6_ifaddr *ia6;
514
515 IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE);
516
517 /*
518 * draft-itojun-ipv6-tcp-to-anycast
519 * better place to put this in?
520 */
521 ia6 = ip6_getdstifaddr(m);
d24ce1dc 522 if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) {
984263bc
MD
523 struct ip6_hdr *ip6;
524
525 ip6 = mtod(m, struct ip6_hdr *);
526 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR,
f23061d4 527 offsetof(struct ip6_hdr, ip6_dst));
d24ce1dc 528 return (IPPROTO_DONE);
984263bc
MD
529 }
530
002c1265 531 tcp_input(mp, offp, proto);
d24ce1dc 532 return (IPPROTO_DONE);
984263bc
MD
533}
534#endif
535
002c1265
MD
536int
537tcp_input(struct mbuf **mp, int *offp, int proto)
984263bc 538{
002c1265 539 int off0;
2256ba69
RG
540 struct tcphdr *th;
541 struct ip *ip = NULL;
542 struct ipovly *ipov;
543 struct inpcb *inp = NULL;
984263bc
MD
544 u_char *optp = NULL;
545 int optlen = 0;
b1992928
MD
546 int tlen, off;
547 int len = 0;
984263bc 548 int drop_hdrlen;
2256ba69
RG
549 struct tcpcb *tp = NULL;
550 int thflags;
4090d6ff 551 struct socket *so = NULL;
61896e3c
JH
552 int todrop, acked;
553 boolean_t ourfinisacked, needoutput = FALSE;
984263bc 554 u_long tiwin;
61896e3c 555 int recvwin;
984263bc 556 struct tcpopt to; /* options in this segment */
984263bc
MD
557 struct sockaddr_in *next_hop = NULL;
558 int rstreason; /* For badport_bandlim accounting purposes */
d371a63a 559 int cpu;
984263bc 560 struct ip6_hdr *ip6 = NULL;
002c1265 561 struct mbuf *m;
984263bc 562#ifdef INET6
d24ce1dc 563 boolean_t isipv6;
984263bc 564#else
d24ce1dc 565 const boolean_t isipv6 = FALSE;
984263bc
MD
566#endif
567#ifdef TCPDEBUG
568 short ostate = 0;
569#endif
570
002c1265
MD
571 off0 = *offp;
572 m = *mp;
573 *mp = NULL;
a00138cb 574
d24ce1dc
JH
575 tcpstat.tcps_rcvtotal++;
576
5de23090
SZ
577 if (m->m_pkthdr.fw_flags & IPFORWARD_MBUF_TAGGED) {
578 struct m_tag *mtag;
579
580 mtag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
581 KKASSERT(mtag != NULL);
582 next_hop = m_tag_data(mtag);
984263bc 583 }
d24ce1dc 584
984263bc 585#ifdef INET6
d24ce1dc 586 isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? TRUE : FALSE;
984263bc 587#endif
984263bc
MD
588
589 if (isipv6) {
590 /* IP6_EXTHDR_CHECK() is already done at tcp6_input() */
591 ip6 = mtod(m, struct ip6_hdr *);
407e896e 592 tlen = (sizeof *ip6) + ntohs(ip6->ip6_plen) - off0;
984263bc
MD
593 if (in6_cksum(m, IPPROTO_TCP, off0, tlen)) {
594 tcpstat.tcps_rcvbadsum++;
595 goto drop;
596 }
597 th = (struct tcphdr *)((caddr_t)ip6 + off0);
598
599 /*
600 * Be proactive about unspecified IPv6 address in source.
601 * As we use all-zero to indicate unbounded/unconnected pcb,
602 * unspecified IPv6 address can be used to confuse us.
603 *
604 * Note that packets with unspecified IPv6 destination is
605 * already dropped in ip6_input.
606 */
607 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
608 /* XXX stat */
609 goto drop;
610 }
611 } else {
612 /*
613 * Get IP and TCP header together in first mbuf.
614 * Note: IP leaves IP header in first mbuf.
615 */
616 if (off0 > sizeof(struct ip)) {
bddf0751 617 ip_stripoptions(m);
984263bc
MD
618 off0 = sizeof(struct ip);
619 }
55d829f8
JH
620 /* already checked and pulled up in ip_demux() */
621 KASSERT(m->m_len >= sizeof(struct tcpiphdr),
61896e3c 622 ("TCP header not in one mbuf: m->m_len %d", m->m_len));
984263bc
MD
623 ip = mtod(m, struct ip *);
624 ipov = (struct ipovly *)ip;
625 th = (struct tcphdr *)((caddr_t)ip + off0);
626 tlen = ip->ip_len;
627
628 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
629 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
630 th->th_sum = m->m_pkthdr.csum_data;
631 else
632 th->th_sum = in_pseudo(ip->ip_src.s_addr,
633 ip->ip_dst.s_addr,
634 htonl(m->m_pkthdr.csum_data +
635 ip->ip_len +
636 IPPROTO_TCP));
637 th->th_sum ^= 0xffff;
638 } else {
639 /*
640 * Checksum extended TCP header and data.
641 */
642 len = sizeof(struct ip) + tlen;
407e896e 643 bzero(ipov->ih_x1, sizeof ipov->ih_x1);
984263bc
MD
644 ipov->ih_len = (u_short)tlen;
645 ipov->ih_len = htons(ipov->ih_len);
646 th->th_sum = in_cksum(m, len);
647 }
648 if (th->th_sum) {
649 tcpstat.tcps_rcvbadsum++;
650 goto drop;
651 }
652#ifdef INET6
653 /* Re-initialization for later version check */
654 ip->ip_v = IPVERSION;
655#endif
656 }
657
658 /*
659 * Check that TCP offset makes sense,
660 * pull out TCP options and adjust length. XXX
661 */
662 off = th->th_off << 2;
55d829f8
JH
663 /* already checked and pulled up in ip_demux() */
664 KASSERT(off >= sizeof(struct tcphdr) && off <= tlen,
61896e3c 665 ("bad TCP data offset %d (tlen %d)", off, tlen));
984263bc
MD
666 tlen -= off; /* tlen is used instead of ti->ti_len */
667 if (off > sizeof(struct tcphdr)) {
668 if (isipv6) {
002c1265 669 IP6_EXTHDR_CHECK(m, off0, off, IPPROTO_DONE);
984263bc
MD
670 ip6 = mtod(m, struct ip6_hdr *);
671 th = (struct tcphdr *)((caddr_t)ip6 + off0);
672 } else {
55d829f8
JH
673 /* already pulled up in ip_demux() */
674 KASSERT(m->m_len >= sizeof(struct ip) + off,
61896e3c
JH
675 ("TCP header and options not in one mbuf: "
676 "m_len %d, off %d", m->m_len, off));
984263bc
MD
677 }
678 optlen = off - sizeof(struct tcphdr);
679 optp = (u_char *)(th + 1);
680 }
681 thflags = th->th_flags;
682
683#ifdef TCP_DROP_SYNFIN
684 /*
685 * If the drop_synfin option is enabled, drop all packets with
686 * both the SYN and FIN bits set. This prevents e.g. nmap from
687 * identifying the TCP/IP stack.
688 *
689 * This is a violation of the TCP specification.
690 */
61896e3c 691 if (drop_synfin && (thflags & (TH_SYN | TH_FIN)) == (TH_SYN | TH_FIN))
984263bc
MD
692 goto drop;
693#endif
694
695 /*
696 * Convert TCP protocol specific fields to host format.
697 */
698 th->th_seq = ntohl(th->th_seq);
699 th->th_ack = ntohl(th->th_ack);
700 th->th_win = ntohs(th->th_win);
701 th->th_urp = ntohs(th->th_urp);
702
703 /*
d24ce1dc 704 * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options,
984263bc
MD
705 * until after ip6_savecontrol() is called and before other functions
706 * which don't want those proto headers.
707 * Because ip6_savecontrol() is going to parse the mbuf to
708 * search for data to be passed up to user-land, it wants mbuf
709 * parameters to be unchanged.
710 * XXX: the call of ip6_savecontrol() has been obsoleted based on
711 * latest version of the advanced API (20020110).
712 */
713 drop_hdrlen = off0 + off;
714
715 /*
716 * Locate pcb for segment.
717 */
718findpcb:
719 /* IPFIREWALL_FORWARD section */
d24ce1dc 720 if (next_hop != NULL && !isipv6) { /* IPv6 support is not there yet */
984263bc
MD
721 /*
722 * Transparently forwarded. Pretend to be the destination.
d24ce1dc 723 * already got one like this?
984263bc 724 */
6ca1a1cd 725 cpu = mycpu->gd_cpuid;
6ca1a1cd 726 inp = in_pcblookup_hash(&tcbinfo[cpu],
d371a63a 727 ip->ip_src, th->th_sport,
984263bc
MD
728 ip->ip_dst, th->th_dport,
729 0, m->m_pkthdr.rcvif);
730 if (!inp) {
83be63fe
JH
731 /*
732 * It's new. Try to find the ambushing socket.
733 */
734
735 /*
736 * The rest of the ipfw code stores the port in
737 * host order. XXX
738 * (The IP address is still in network order.)
739 */
740 in_port_t dport = next_hop->sin_port ?
741 htons(next_hop->sin_port) :
742 th->th_dport;
743
d371a63a 744 cpu = tcp_addrcpu(ip->ip_src.s_addr, th->th_sport,
83be63fe 745 next_hop->sin_addr.s_addr, dport);
d371a63a 746 inp = in_pcblookup_hash(&tcbinfo[cpu],
984263bc 747 ip->ip_src, th->th_sport,
83be63fe 748 next_hop->sin_addr, dport,
984263bc
MD
749 1, m->m_pkthdr.rcvif);
750 }
751 } else {
6ca1a1cd 752 if (isipv6) {
d371a63a 753 inp = in6_pcblookup_hash(&tcbinfo[0],
984263bc
MD
754 &ip6->ip6_src, th->th_sport,
755 &ip6->ip6_dst, th->th_dport,
756 1, m->m_pkthdr.rcvif);
6ca1a1cd 757 } else {
6ca1a1cd 758 cpu = mycpu->gd_cpuid;
6ca1a1cd 759 inp = in_pcblookup_hash(&tcbinfo[cpu],
984263bc
MD
760 ip->ip_src, th->th_sport,
761 ip->ip_dst, th->th_dport,
762 1, m->m_pkthdr.rcvif);
6ca1a1cd 763 }
984263bc 764 }
984263bc
MD
765
766 /*
767 * If the state is CLOSED (i.e., TCB does not exist) then
768 * all data in the incoming segment is discarded.
769 * If the TCB exists but is in CLOSED state, it is embryonic,
770 * but should either do a listen or a connect soon.
771 */
772 if (inp == NULL) {
773 if (log_in_vain) {
774#ifdef INET6
775 char dbuf[INET6_ADDRSTRLEN+2], sbuf[INET6_ADDRSTRLEN+2];
776#else
1141eb20
JH
777 char dbuf[sizeof "aaa.bbb.ccc.ddd"];
778 char sbuf[sizeof "aaa.bbb.ccc.ddd"];
984263bc
MD
779#endif
780 if (isipv6) {
781 strcpy(dbuf, "[");
984263bc 782 strcat(dbuf, ip6_sprintf(&ip6->ip6_dst));
984263bc 783 strcat(dbuf, "]");
95b22adf
JH
784 strcpy(sbuf, "[");
785 strcat(sbuf, ip6_sprintf(&ip6->ip6_src));
984263bc
MD
786 strcat(sbuf, "]");
787 } else {
788 strcpy(dbuf, inet_ntoa(ip->ip_dst));
789 strcpy(sbuf, inet_ntoa(ip->ip_src));
790 }
791 switch (log_in_vain) {
792 case 1:
61896e3c 793 if (!(thflags & TH_SYN))
984263bc
MD
794 break;
795 case 2:
796 log(LOG_INFO,
797 "Connection attempt to TCP %s:%d "
798 "from %s:%d flags:0x%02x\n",
799 dbuf, ntohs(th->th_dport), sbuf,
800 ntohs(th->th_sport), thflags);
801 break;
802 default:
803 break;
804 }
805 }
d24ce1dc 806 if (blackhole) {
984263bc
MD
807 switch (blackhole) {
808 case 1:
809 if (thflags & TH_SYN)
810 goto drop;
811 break;
812 case 2:
813 goto drop;
814 default:
815 goto drop;
816 }
817 }
818 rstreason = BANDLIM_RST_CLOSEDPORT;
819 goto dropwithreset;
820 }
61896e3c
JH
821
822#ifdef IPSEC
823 if (isipv6) {
824 if (ipsec6_in_reject_so(m, inp->inp_socket)) {
825 ipsec6stat.in_polvio++;
826 goto drop;
827 }
828 } else {
829 if (ipsec4_in_reject_so(m, inp->inp_socket)) {
830 ipsecstat.in_polvio++;
831 goto drop;
832 }
833 }
834#endif
835#ifdef FAST_IPSEC
836 if (isipv6) {
837 if (ipsec6_in_reject(m, inp))
838 goto drop;
839 } else {
840 if (ipsec4_in_reject(m, inp))
841 goto drop;
842 }
843#endif
95926362
MD
844 /* Check the minimum TTL for socket. */
845#ifdef INET6
846 if ((isipv6 ? ip6->ip6_hlim : ip->ip_ttl) < inp->inp_ip_minttl)
847 goto drop;
848#endif
61896e3c 849
984263bc
MD
850 tp = intotcpcb(inp);
851 if (tp == NULL) {
852 rstreason = BANDLIM_RST_CLOSEDPORT;
853 goto dropwithreset;
854 }
eb594563 855 if (tp->t_state <= TCPS_CLOSED)
984263bc
MD
856 goto drop;
857
984263bc 858 so = inp->inp_socket;
47654766 859
984263bc 860#ifdef TCPDEBUG
47654766
JH
861 if (so->so_options & SO_DEBUG) {
862 ostate = tp->t_state;
863 if (isipv6)
95b22adf 864 bcopy(ip6, tcp_saveipgen, sizeof(*ip6));
47654766 865 else
95b22adf 866 bcopy(ip, tcp_saveipgen, sizeof(*ip));
47654766
JH
867 tcp_savetcp = *th;
868 }
984263bc 869#endif
47654766 870
407e896e 871 bzero(&to, sizeof to);
d24ce1dc 872
47654766
JH
873 if (so->so_options & SO_ACCEPTCONN) {
874 struct in_conninfo inc;
875
984263bc 876#ifdef INET6
d24ce1dc 877 inc.inc_isipv6 = (isipv6 == TRUE);
984263bc
MD
878#endif
879 if (isipv6) {
880 inc.inc6_faddr = ip6->ip6_src;
881 inc.inc6_laddr = ip6->ip6_dst;
882 inc.inc6_route.ro_rt = NULL; /* XXX */
883 } else {
884 inc.inc_faddr = ip->ip_src;
885 inc.inc_laddr = ip->ip_dst;
886 inc.inc_route.ro_rt = NULL; /* XXX */
887 }
888 inc.inc_fport = th->th_sport;
889 inc.inc_lport = th->th_dport;
890
61896e3c
JH
891 /*
892 * If the state is LISTEN then ignore segment if it contains
984263bc
MD
893 * a RST. If the segment contains an ACK then it is bad and
894 * send a RST. If it does not contain a SYN then it is not
895 * interesting; drop it.
896 *
897 * If the state is SYN_RECEIVED (syncache) and seg contains
898 * an ACK, but not for our SYN/ACK, send a RST. If the seg
899 * contains a RST, check the sequence number to see if it
900 * is a valid reset segment.
901 */
61896e3c
JH
902 if ((thflags & (TH_RST | TH_ACK | TH_SYN)) != TH_SYN) {
903 if ((thflags & (TH_RST | TH_ACK | TH_SYN)) == TH_ACK) {
984263bc
MD
904 if (!syncache_expand(&inc, th, &so, m)) {
905 /*
906 * No syncache entry, or ACK was not
907 * for our SYN/ACK. Send a RST.
908 */
909 tcpstat.tcps_badsyn++;
910 rstreason = BANDLIM_RST_OPENPORT;
911 goto dropwithreset;
912 }
2d23a8be
MD
913
914 /*
915 * Could not complete 3-way handshake,
916 * connection is being closed down, and
917 * syncache will free mbuf.
918 */
984263bc 919 if (so == NULL)
002c1265 920 return(IPPROTO_DONE);
2d23a8be
MD
921
922 /*
923 * We must be in the correct protocol thread
924 * for this connection.
925 */
926 KKASSERT(so->so_port == &curthread->td_msgport);
927
984263bc
MD
928 /*
929 * Socket is created in state SYN_RECEIVED.
930 * Continue processing segment.
931 */
ed894f8c 932 inp = so->so_pcb;
984263bc
MD
933 tp = intotcpcb(inp);
934 /*
935 * This is what would have happened in
936 * tcp_output() when the SYN,ACK was sent.
937 */
938 tp->snd_up = tp->snd_una;
939 tp->snd_max = tp->snd_nxt = tp->iss + 1;
940 tp->last_ack_sent = tp->rcv_nxt;
df1d2774 941
984263bc
MD
942 goto after_listen;
943 }
944 if (thflags & TH_RST) {
945 syncache_chkrst(&inc, th);
946 goto drop;
947 }
948 if (thflags & TH_ACK) {
949 syncache_badack(&inc);
950 tcpstat.tcps_badsyn++;
951 rstreason = BANDLIM_RST_OPENPORT;
952 goto dropwithreset;
953 }
954 goto drop;
955 }
956
957 /*
61896e3c 958 * Segment's flags are (SYN) or (SYN | FIN).
984263bc
MD
959 */
960#ifdef INET6
961 /*
962 * If deprecated address is forbidden,
963 * we do not accept SYN to deprecated interface
964 * address to prevent any new inbound connection from
965 * getting established.
966 * When we do not accept SYN, we send a TCP RST,
967 * with deprecated source address (instead of dropping
968 * it). We compromise it as it is much better for peer
969 * to send a RST, and RST will be the final packet
970 * for the exchange.
971 *
972 * If we do not forbid deprecated addresses, we accept
973 * the SYN packet. RFC2462 does not suggest dropping
974 * SYN in this case.
975 * If we decipher RFC2462 5.5.4, it says like this:
976 * 1. use of deprecated addr with existing
977 * communication is okay - "SHOULD continue to be
978 * used"
979 * 2. use of it with new communication:
980 * (2a) "SHOULD NOT be used if alternate address
61896e3c 981 * with sufficient scope is available"
984263bc
MD
982 * (2b) nothing mentioned otherwise.
983 * Here we fall into (2b) case as we have no choice in
984 * our source address selection - we must obey the peer.
985 *
986 * The wording in RFC2462 is confusing, and there are
987 * multiple description text for deprecated address
988 * handling - worse, they are not exactly the same.
989 * I believe 5.5.4 is the best one, so we follow 5.5.4.
990 */
991 if (isipv6 && !ip6_use_deprecated) {
992 struct in6_ifaddr *ia6;
993
994 if ((ia6 = ip6_getdstifaddr(m)) &&
995 (ia6->ia6_flags & IN6_IFF_DEPRECATED)) {
996 tp = NULL;
997 rstreason = BANDLIM_RST_OPENPORT;
998 goto dropwithreset;
999 }
1000 }
1001#endif
1002 /*
1003 * If it is from this socket, drop it, it must be forged.
1004 * Don't bother responding if the destination was a broadcast.
1005 */
1006 if (th->th_dport == th->th_sport) {
1007 if (isipv6) {
1008 if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
1009 &ip6->ip6_src))
1010 goto drop;
1011 } else {
1012 if (ip->ip_dst.s_addr == ip->ip_src.s_addr)
1013 goto drop;
1014 }
1015 }
1016 /*
1017 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN
1018 *
1019 * Note that it is quite possible to receive unicast
1020 * link-layer packets with a broadcast IP address. Use
1021 * in_broadcast() to find them.
1022 */
61896e3c 1023 if (m->m_flags & (M_BCAST | M_MCAST))
984263bc
MD
1024 goto drop;
1025 if (isipv6) {
1026 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
1027 IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
1028 goto drop;
1029 } else {
1030 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
1031 IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
1032 ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
1033 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
1034 goto drop;
1035 }
1036 /*
1037 * SYN appears to be valid; create compressed TCP state
1038 * for syncache, or perform t/tcp connection.
1039 */
1040 if (so->so_qlen <= so->so_qlimit) {
6c1bbf57 1041 tcp_dooptions(&to, optp, optlen, TRUE, th->th_ack);
b09567cc 1042 if (!syncache_add(&inc, &to, th, so, m))
984263bc 1043 goto drop;
2d23a8be
MD
1044
1045 /*
1046 * Entry added to syncache, mbuf used to
1047 * send SYN,ACK packet.
1048 */
b09567cc 1049 return(IPPROTO_DONE);
984263bc
MD
1050 }
1051 goto drop;
1052 }
984263bc 1053
2d23a8be
MD
1054after_listen:
1055 /*
1056 * Should not happen - syncache should pick up these connections.
1057 *
1058 * Once we are past handling listen sockets we must be in the
1059 * correct protocol processing thread.
1060 */
61896e3c 1061 KASSERT(tp->t_state != TCPS_LISTEN, ("tcp_input: TCPS_LISTEN state"));
2d23a8be 1062 KKASSERT(so->so_port == &curthread->td_msgport);
984263bc 1063
df1d2774
SZ
1064 /* Unscale the window into a 32-bit value. */
1065 if (!(thflags & TH_SYN))
1066 tiwin = th->th_win << tp->snd_scale;
1067 else
1068 tiwin = th->th_win;
1069
5b0b9fa5
PA
1070 /*
1071 * This is the second part of the MSS DoS prevention code (after
1072 * minmss on the sending side) and it deals with too many too small
1073 * tcp packets in a too short timeframe (1 second).
1074 *
153d34ac
MD
1075 * XXX Removed. This code was crap. It does not scale to network
1076 * speed, and default values break NFS. Gone.
5b0b9fa5 1077 */
153d34ac 1078 /* REMOVED */
5b0b9fa5 1079
984263bc
MD
1080 /*
1081 * Segment received on connection.
efca2b8e
MD
1082 *
1083 * Reset idle time and keep-alive timer. Don't waste time if less
0ecd93f9 1084 * then a second has elapsed.
984263bc 1085 */
0ecd93f9
MD
1086 if ((int)(ticks - tp->t_rcvtime) > hz)
1087 tcp_timer_keep_activity(tp, thflags);
984263bc
MD
1088
1089 /*
1090 * Process options.
1091 * XXX this is tradtitional behavior, may need to be cleaned up.
1092 */
6c1bbf57 1093 tcp_dooptions(&to, optp, optlen, (thflags & TH_SYN) != 0, th->th_ack);
ad0af98b 1094 if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) {
df1d2774 1095 if ((to.to_flags & TOF_SCALE) && (tp->t_flags & TF_REQ_SCALE)) {
984263bc 1096 tp->t_flags |= TF_RCVD_SCALE;
df1d2774 1097 tp->snd_scale = to.to_requested_s_scale;
984263bc 1098 }
df1d2774
SZ
1099
1100 /*
1101 * Initial send window; will be updated upon next ACK
1102 */
1103 tp->snd_wnd = th->th_win;
1104
984263bc
MD
1105 if (to.to_flags & TOF_TS) {
1106 tp->t_flags |= TF_RCVD_TSTMP;
1107 tp->ts_recent = to.to_tsval;
1108 tp->ts_recent_age = ticks;
1109 }
f34fd0f2
SZ
1110 if (!(to.to_flags & TOF_MSS))
1111 to.to_mss = 0;
1112 tcp_mss(tp, to.to_mss);
91489f6b
JH
1113 /*
1114 * Only set the TF_SACK_PERMITTED per-connection flag
1115 * if we got a SACK_PERMITTED option from the other side
1116 * and the global tcp_do_sack variable is true.
1117 */
1118 if (tcp_do_sack && (to.to_flags & TOF_SACK_PERMITTED))
1119 tp->t_flags |= TF_SACK_PERMITTED;
984263bc
MD
1120 }
1121
1122 /*
1123 * Header prediction: check for the two common cases
1124 * of a uni-directional data xfer. If the packet has
1125 * no control flags, is in-sequence, the window didn't
1126 * change and we're not retransmitting, it's a
1127 * candidate. If the length is zero and the ack moved
1128 * forward, we're the sender side of the xfer. Just
1129 * free the data acked & wake any higher level process
1130 * that was blocked waiting for space. If the length
1131 * is non-zero and the ack didn't move, we're the
1132 * receiver side. If we're getting packets in-order
1133 * (the reassembly queue is empty), add the data to
1134 * the socket buffer and note that we need a delayed ack.
1135 * Make sure that the hidden state-flags are also off.
1136 * Since we check for TCPS_ESTABLISHED above, it can only
1137 * be TH_NEEDSYN.
1138 */
1139 if (tp->t_state == TCPS_ESTABLISHED &&
1140 (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
61896e3c
JH
1141 !(tp->t_flags & (TF_NEEDSYN | TF_NEEDFIN)) &&
1142 (!(to.to_flags & TOF_TS) ||
984263bc 1143 TSTMP_GEQ(to.to_tsval, tp->ts_recent)) &&
984263bc 1144 th->th_seq == tp->rcv_nxt &&
984263bc
MD
1145 tp->snd_nxt == tp->snd_max) {
1146
1147 /*
1148 * If last ACK falls within this segment's sequence numbers,
1149 * record the timestamp.
1150 * NOTE that the test is modified according to the latest
1151 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1152 */
61896e3c 1153 if ((to.to_flags & TOF_TS) &&
984263bc
MD
1154 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
1155 tp->ts_recent_age = ticks;
1156 tp->ts_recent = to.to_tsval;
1157 }
1158
1159 if (tlen == 0) {
1160 if (SEQ_GT(th->th_ack, tp->snd_una) &&
1161 SEQ_LEQ(th->th_ack, tp->snd_max) &&
1162 tp->snd_cwnd >= tp->snd_wnd &&
95b22adf 1163 !IN_FASTRECOVERY(tp)) {
984263bc 1164 /*
be23faf1 1165 * This is a pure ack for outstanding data.
984263bc
MD
1166 */
1167 ++tcpstat.tcps_predack;
1168 /*
1169 * "bad retransmit" recovery
bfdb979e
JH
1170 *
1171 * If Eifel detection applies, then
1172 * it is deterministic, so use it
1173 * unconditionally over the old heuristic.
1174 * Otherwise, fall back to the old heuristic.
984263bc 1175 */
bfdb979e
JH
1176 if (tcp_do_eifel_detect &&
1177 (to.to_flags & TOF_TS) && to.to_tsecr &&
1178 (tp->t_flags & TF_FIRSTACCACK)) {
1179 /* Eifel detection applicable. */
1180 if (to.to_tsecr < tp->t_rexmtTS) {
1181 tcp_revert_congestion_state(tp);
1182 ++tcpstat.tcps_eifeldetected;
928c3291
SZ
1183 if (tp->t_rxtshift != 1 ||
1184 ticks >= tp->t_badrxtwin)
1185 ++tcpstat.tcps_rttcantdetect;
bfdb979e
JH
1186 }
1187 } else if (tp->t_rxtshift == 1 &&
1188 ticks < tp->t_badrxtwin) {
1189 tcp_revert_congestion_state(tp);
1190 ++tcpstat.tcps_rttdetected;
984263bc 1191 }
8819433a
JH
1192 tp->t_flags &= ~(TF_FIRSTACCACK |
1193 TF_FASTREXMT | TF_EARLYREXMT);
984263bc
MD
1194 /*
1195 * Recalculate the retransmit timer / rtt.
1196 *
d24ce1dc 1197 * Some machines (certain windows boxes)
984263bc
MD
1198 * send broken timestamp replies during the
1199 * SYN+ACK phase, ignore timestamps of 0.
1200 */
95b22adf 1201 if ((to.to_flags & TOF_TS) && to.to_tsecr) {
984263bc 1202 tcp_xmit_timer(tp,
073ec6c4
SZ
1203 ticks - to.to_tsecr + 1,
1204 th->th_ack);
984263bc 1205 } else if (tp->t_rtttime &&
95b22adf 1206 SEQ_GT(th->th_ack, tp->t_rtseq)) {
984263bc 1207 tcp_xmit_timer(tp,
073ec6c4
SZ
1208 ticks - tp->t_rtttime,
1209 th->th_ack);
984263bc
MD
1210 }
1211 tcp_xmit_bandwidth_limit(tp, th->th_ack);
1212 acked = th->th_ack - tp->snd_una;
1213 tcpstat.tcps_rcvackpack++;
1214 tcpstat.tcps_rcvackbyte += acked;
6d49aa6f 1215 sbdrop(&so->so_snd.sb, acked);
cfb3f3f4 1216 tp->snd_recover = th->th_ack - 1;
9845754e 1217 tp->snd_una = th->th_ack;
984263bc 1218 tp->t_dupacks = 0;
df9d7670
JH
1219 /*
1220 * Update window information.
1221 */
1222 if (tiwin != tp->snd_wnd &&
1223 acceptable_window_update(tp, th, tiwin)) {
1224 /* keep track of pure window updates */
1225 if (tp->snd_wl2 == th->th_ack &&
1226 tiwin > tp->snd_wnd)
1227 tcpstat.tcps_rcvwinupd++;
1228 tp->snd_wnd = tiwin;
1229 tp->snd_wl1 = th->th_seq;
1230 tp->snd_wl2 = th->th_ack;
1231 if (tp->snd_wnd > tp->max_sndwnd)
1232 tp->max_sndwnd = tp->snd_wnd;
1233 }
984263bc
MD
1234 m_freem(m);
1235 ND6_HINT(tp); /* some progress has been done */
984263bc
MD
1236 /*
1237 * If all outstanding data are acked, stop
1238 * retransmit timer, otherwise restart timer
1239 * using current (possibly backed-off) value.
1240 * If process is waiting for space,
1241 * wakeup/selwakeup/signal. If data
1242 * are ready to send, let tcp_output
1243 * decide between more output or persist.
1244 */
a48c5dd5
SZ
1245 if (tp->snd_una == tp->snd_max) {
1246 tcp_callout_stop(tp, tp->tt_rexmt);
1247 } else if (!tcp_callout_active(tp,
1248 tp->tt_persist)) {
1249 tcp_callout_reset(tp, tp->tt_rexmt,
1250 tp->t_rxtcur, tcp_timer_rexmt);
1251 }
984263bc 1252 sowwakeup(so);
6d49aa6f 1253 if (so->so_snd.ssb_cc > 0)
f23061d4 1254 tcp_output(tp);
002c1265 1255 return(IPPROTO_DONE);
984263bc 1256 }
df9d7670
JH
1257 } else if (tiwin == tp->snd_wnd &&
1258 th->th_ack == tp->snd_una &&
984263bc 1259 LIST_EMPTY(&tp->t_segq) &&
6d49aa6f 1260 tlen <= ssb_space(&so->so_rcv)) {
46e92930 1261 u_long newsize = 0; /* automatic sockbuf scaling */
984263bc 1262 /*
be23faf1 1263 * This is a pure, in-sequence data packet
984263bc
MD
1264 * with nothing on the reassembly queue and
1265 * we have enough buffer space to take it.
1266 */
1267 ++tcpstat.tcps_preddat;
1268 tp->rcv_nxt += tlen;
1269 tcpstat.tcps_rcvpack++;
1270 tcpstat.tcps_rcvbyte += tlen;
1271 ND6_HINT(tp); /* some progress has been done */
5b0b9fa5
PA
1272 /*
1273 * Automatic sizing of receive socket buffer. Often the send
1274 * buffer size is not optimally adjusted to the actual network
1275 * conditions at hand (delay bandwidth product). Setting the
1276 * buffer size too small limits throughput on links with high
1277 * bandwidth and high delay (eg. trans-continental/oceanic links).
1278 *
1279 * On the receive side the socket buffer memory is only rarely
1280 * used to any significant extent. This allows us to be much
1281 * more aggressive in scaling the receive socket buffer. For
1282 * the case that the buffer space is actually used to a large
1283 * extent and we run out of kernel memory we can simply drop
1284 * the new segments; TCP on the sender will just retransmit it
1285 * later. Setting the buffer size too big may only consume too
1286 * much kernel memory if the application doesn't read() from
1287 * the socket or packet loss or reordering makes use of the
1288 * reassembly queue.
1289 *
1290 * The criteria to step up the receive buffer one notch are:
1291 * 1. the number of bytes received during the time it takes
1292 * one timestamp to be reflected back to us (the RTT);
1293 * 2. received bytes per RTT is within seven eighth of the
1294 * current socket buffer size;
1295 * 3. receive buffer size has not hit maximal automatic size;
1296 *
1297 * This algorithm does one step per RTT at most and only if
1298 * we receive a bulk stream w/o packet losses or reorderings.
1299 * Shrinking the buffer during idle times is not necessary as
1300 * it doesn't consume any memory when idle.
1301 *
1302 * TODO: Only step up if the application is actually serving
1303 * the buffer to better manage the socket buffer resources.
1304 */
1305 if (tcp_do_autorcvbuf &&
1306 to.to_tsecr &&
1307 (so->so_rcv.ssb_flags & SSB_AUTOSIZE)) {
1308 if (to.to_tsecr > tp->rfbuf_ts &&
1309 to.to_tsecr - tp->rfbuf_ts < hz) {
1310 if (tp->rfbuf_cnt >
1311 (so->so_rcv.ssb_hiwat / 8 * 7) &&
1312 so->so_rcv.ssb_hiwat <
1313 tcp_autorcvbuf_max) {
1314 newsize =
46e92930
MD
1315 ulmin(so->so_rcv.ssb_hiwat +
1316 tcp_autorcvbuf_inc,
1317 tcp_autorcvbuf_max);
5b0b9fa5
PA
1318 }
1319 /* Start over with next RTT. */
1320 tp->rfbuf_ts = 0;
1321 tp->rfbuf_cnt = 0;
1322 } else
1323 tp->rfbuf_cnt += tlen; /* add up */
1324 }
984263bc
MD
1325 /*
1326 * Add data to socket buffer.
1327 */
1328 if (so->so_state & SS_CANTRCVMORE) {
1329 m_freem(m);
1330 } else {
5b0b9fa5 1331 /*
46e92930
MD
1332 * Set new socket buffer size, give up when
1333 * limit is reached.
1334 *
1335 * Adjusting the size can mess up ACK
1336 * sequencing when pure window updates are
1337 * being avoided (which is the default),
1338 * so force an ack.
5b0b9fa5 1339 */
6cef7136 1340 lwkt_gettoken(&so->so_rcv.ssb_token);
46e92930
MD
1341 if (newsize) {
1342 tp->t_flags |= TF_RXRESIZED;
5b0b9fa5 1343 if (!ssb_reserve(&so->so_rcv, newsize,
46e92930 1344 so, NULL)) {
14343ad3 1345 atomic_clear_int(&so->so_rcv.ssb_flags, SSB_AUTOSIZE);
46e92930
MD
1346 }
1347 if (newsize >=
1348 (TCP_MAXWIN << tp->rcv_scale)) {
14343ad3 1349 atomic_clear_int(&so->so_rcv.ssb_flags, SSB_AUTOSIZE);
46e92930
MD
1350 }
1351 }
09cf73e0 1352 m_adj(m, drop_hdrlen); /* delayed header drop */
6d49aa6f 1353 ssb_appendstream(&so->so_rcv, m);
6cef7136 1354 lwkt_reltoken(&so->so_rcv.ssb_token);
984263bc
MD
1355 }
1356 sorwakeup(so);
2b1ce38a
MD
1357 /*
1358 * This code is responsible for most of the ACKs
1359 * the TCP stack sends back after receiving a data
1360 * packet. Note that the DELAY_ACK check fails if
1361 * the delack timer is already running, which results
1362 * in an ack being sent every other packet (which is
1363 * what we want).
72b37eeb
MD
1364 *
1365 * We then further aggregate acks by not actually
1366 * sending one until the protocol thread has completed
1367 * processing the current backlog of packets. This
1368 * does not delay the ack any further, but allows us
1369 * to take advantage of the packet aggregation that
1370 * high speed NICs do (usually blocks of 8-10 packets)
1371 * to send a single ack rather then four or five acks,
1372 * greatly reducing the ack rate, the return channel
1373 * bandwidth, and the protocol overhead on both ends.
1374 *
1375 * Since this also has the effect of slowing down
1376 * the exponential slow-start ramp-up, systems with
1377 * very large bandwidth-delay products might want
1378 * to turn the feature off.
2b1ce38a 1379 */
984263bc 1380 if (DELAY_ACK(tp)) {
a48c5dd5
SZ
1381 tcp_callout_reset(tp, tp->tt_delack,
1382 tcp_delacktime, tcp_timer_delack);
72b37eeb 1383 } else if (tcp_aggregate_acks) {
984263bc 1384 tp->t_flags |= TF_ACKNOW;
61896e3c 1385 if (!(tp->t_flags & TF_ONOUTPUTQ)) {
2b1ce38a
MD
1386 tp->t_flags |= TF_ONOUTPUTQ;
1387 tp->tt_cpu = mycpu->gd_cpuid;
1388 TAILQ_INSERT_TAIL(
1389 &tcpcbackq[tp->tt_cpu],
1390 tp, t_outputq);
1391 }
72b37eeb
MD
1392 } else {
1393 tp->t_flags |= TF_ACKNOW;
1394 tcp_output(tp);
984263bc 1395 }
002c1265 1396 return(IPPROTO_DONE);
984263bc
MD
1397 }
1398 }
1399
1400 /*
1401 * Calculate amount of space in receive window,
1402 * and then do TCP input processing.
1403 * Receive window is amount of space in rcv queue,
1404 * but not less than advertised window.
1405 */
6d49aa6f 1406 recvwin = ssb_space(&so->so_rcv);
61896e3c
JH
1407 if (recvwin < 0)
1408 recvwin = 0;
1409 tp->rcv_wnd = imax(recvwin, (int)(tp->rcv_adv - tp->rcv_nxt));
984263bc 1410
5b0b9fa5
PA
1411 /* Reset receive buffer auto scaling when not in bulk receive mode. */
1412 tp->rfbuf_ts = 0;
1413 tp->rfbuf_cnt = 0;
1414
984263bc 1415 switch (tp->t_state) {
984263bc
MD
1416 /*
1417 * If the state is SYN_RECEIVED:
1418 * if seg contains an ACK, but not for our SYN/ACK, send a RST.
1419 */
1420 case TCPS_SYN_RECEIVED:
1421 if ((thflags & TH_ACK) &&
1422 (SEQ_LEQ(th->th_ack, tp->snd_una) ||
1423 SEQ_GT(th->th_ack, tp->snd_max))) {
1424 rstreason = BANDLIM_RST_OPENPORT;
1425 goto dropwithreset;
1426 }
1427 break;
1428
1429 /*
1430 * If the state is SYN_SENT:
1431 * if seg contains an ACK, but not for our SYN, drop the input.
1432 * if seg contains a RST, then drop the connection.
1433 * if seg does not contain SYN, then drop it.
1434 * Otherwise this is an acceptable SYN segment
1435 * initialize tp->rcv_nxt and tp->irs
1436 * if seg contains ack then advance tp->snd_una
1437 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state
1438 * arrange for segment to be acked (eventually)
1439 * continue processing rest of data/controls, beginning with URG
1440 */
1441 case TCPS_SYN_SENT:
984263bc
MD
1442 if ((thflags & TH_ACK) &&
1443 (SEQ_LEQ(th->th_ack, tp->iss) ||
1444 SEQ_GT(th->th_ack, tp->snd_max))) {
27b8aee3
AE
1445 rstreason = BANDLIM_UNLIMITED;
1446 goto dropwithreset;
984263bc
MD
1447 }
1448 if (thflags & TH_RST) {
1449 if (thflags & TH_ACK)
1450 tp = tcp_drop(tp, ECONNREFUSED);
1451 goto drop;
1452 }
61896e3c 1453 if (!(thflags & TH_SYN))
984263bc 1454 goto drop;
984263bc
MD
1455
1456 tp->irs = th->th_seq;
1457 tcp_rcvseqinit(tp);
1458 if (thflags & TH_ACK) {
27b8aee3 1459 /* Our SYN was acked. */
984263bc
MD
1460 tcpstat.tcps_connects++;
1461 soisconnected(so);
1462 /* Do window scaling on this connection? */
61896e3c 1463 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) ==
df1d2774 1464 (TF_RCVD_SCALE | TF_REQ_SCALE))
984263bc 1465 tp->rcv_scale = tp->request_r_scale;
984263bc
MD
1466 tp->rcv_adv += tp->rcv_wnd;
1467 tp->snd_una++; /* SYN is acked */
a48c5dd5 1468 tcp_callout_stop(tp, tp->tt_rexmt);
984263bc
MD
1469 /*
1470 * If there's data, delay ACK; if there's also a FIN
1471 * ACKNOW will be turned on later.
1472 */
a48c5dd5
SZ
1473 if (DELAY_ACK(tp) && tlen != 0) {
1474 tcp_callout_reset(tp, tp->tt_delack,
1475 tcp_delacktime, tcp_timer_delack);
1476 } else {
984263bc 1477 tp->t_flags |= TF_ACKNOW;
a48c5dd5 1478 }
984263bc
MD
1479 /*
1480 * Received <SYN,ACK> in SYN_SENT[*] state.
1481 * Transitions:
1482 * SYN_SENT --> ESTABLISHED
1483 * SYN_SENT* --> FIN_WAIT_1
1484 */
1485 tp->t_starttime = ticks;
1486 if (tp->t_flags & TF_NEEDFIN) {
1487 tp->t_state = TCPS_FIN_WAIT_1;
1488 tp->t_flags &= ~TF_NEEDFIN;
1489 thflags &= ~TH_SYN;
1490 } else {
8651f7f8 1491 tcp_established(tp);
984263bc
MD
1492 }
1493 } else {
1494 /*
95b22adf 1495 * Received initial SYN in SYN-SENT[*] state =>
27b8aee3
AE
1496 * simultaneous open.
1497 * Do 3-way handshake:
61896e3c
JH
1498 * SYN-SENT -> SYN-RECEIVED
1499 * SYN-SENT* -> SYN-RECEIVED*
95b22adf 1500 */
984263bc 1501 tp->t_flags |= TF_ACKNOW;
a48c5dd5 1502 tcp_callout_stop(tp, tp->tt_rexmt);
27b8aee3 1503 tp->t_state = TCPS_SYN_RECEIVED;
984263bc
MD
1504 }
1505
984263bc
MD
1506 /*
1507 * Advance th->th_seq to correspond to first data byte.
1508 * If data, trim to stay within window,
1509 * dropping FIN if necessary.
1510 */
1511 th->th_seq++;
1512 if (tlen > tp->rcv_wnd) {
1513 todrop = tlen - tp->rcv_wnd;
1514 m_adj(m, -todrop);
1515 tlen = tp->rcv_wnd;
1516 thflags &= ~TH_FIN;
1517 tcpstat.tcps_rcvpackafterwin++;
1518 tcpstat.tcps_rcvbyteafterwin += todrop;
1519 }
1520 tp->snd_wl1 = th->th_seq - 1;
1521 tp->rcv_up = th->th_seq;
1522 /*
1523 * Client side of transaction: already sent SYN and data.
1524 * If the remote host used T/TCP to validate the SYN,
1525 * our data will be ACK'd; if so, enter normal data segment
1526 * processing in the middle of step 5, ack processing.
1527 * Otherwise, goto step 6.
1528 */
95b22adf 1529 if (thflags & TH_ACK)
984263bc
MD
1530 goto process_ACK;
1531
1532 goto step6;
1533
1534 /*
1535 * If the state is LAST_ACK or CLOSING or TIME_WAIT:
27b8aee3 1536 * do normal processing (we no longer bother with T/TCP).
984263bc
MD
1537 */
1538 case TCPS_LAST_ACK:
1539 case TCPS_CLOSING:
1540 case TCPS_TIME_WAIT:
95b22adf 1541 break; /* continue normal processing */
984263bc
MD
1542 }
1543
1544 /*
1545 * States other than LISTEN or SYN_SENT.
1546 * First check the RST flag and sequence number since reset segments
1547 * are exempt from the timestamp and connection count tests. This
1548 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix
1549 * below which allowed reset segments in half the sequence space
1550 * to fall though and be processed (which gives forged reset
1551 * segments with a random sequence number a 50 percent chance of
1552 * killing a connection).
1553 * Then check timestamp, if present.
1554 * Then check the connection count, if present.
1555 * Then check that at least some bytes of segment are within
1556 * receive window. If segment begins before rcv_nxt,
1557 * drop leading data (and SYN); if nothing left, just ack.
1558 *
1559 *
1560 * If the RST bit is set, check the sequence number to see
1561 * if this is a valid reset segment.
1562 * RFC 793 page 37:
1563 * In all states except SYN-SENT, all reset (RST) segments
1564 * are validated by checking their SEQ-fields. A reset is
1565 * valid if its sequence number is in the window.
1566 * Note: this does not take into account delayed ACKs, so
1567 * we should test against last_ack_sent instead of rcv_nxt.
1568 * The sequence number in the reset segment is normally an
91489f6b 1569 * echo of our outgoing acknowledgement numbers, but some hosts
984263bc
MD
1570 * send a reset with the sequence number at the rightmost edge
1571 * of our receive window, and we have to handle this case.
1572 * If we have multiple segments in flight, the intial reset
1573 * segment sequence numbers will be to the left of last_ack_sent,
1574 * but they will eventually catch up.
1575 * In any case, it never made sense to trim reset segments to
1576 * fit the receive window since RFC 1122 says:
1577 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
1578 *
1579 * A TCP SHOULD allow a received RST segment to include data.
1580 *
1581 * DISCUSSION
61896e3c
JH
1582 * It has been suggested that a RST segment could contain
1583 * ASCII text that encoded and explained the cause of the
91489f6b 1584 * RST. No standard has yet been established for such
61896e3c 1585 * data.
984263bc
MD
1586 *
1587 * If the reset segment passes the sequence number test examine
1588 * the state:
1589 * SYN_RECEIVED STATE:
1590 * If passive open, return to LISTEN state.
1591 * If active open, inform user that connection was refused.
1592 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT STATES:
1593 * Inform user that connection was reset, and close tcb.
1594 * CLOSING, LAST_ACK STATES:
1595 * Close the tcb.
1596 * TIME_WAIT STATE:
1597 * Drop the segment - see Stevens, vol. 2, p. 964 and
61896e3c 1598 * RFC 1337.
984263bc
MD
1599 */
1600 if (thflags & TH_RST) {
1601 if (SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
d4dbb5be 1602 SEQ_LEQ(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) {
984263bc
MD
1603 switch (tp->t_state) {
1604
1605 case TCPS_SYN_RECEIVED:
1606 so->so_error = ECONNREFUSED;
1607 goto close;
1608
1609 case TCPS_ESTABLISHED:
1610 case TCPS_FIN_WAIT_1:
1611 case TCPS_FIN_WAIT_2:
1612 case TCPS_CLOSE_WAIT:
1613 so->so_error = ECONNRESET;
1614 close:
1615 tp->t_state = TCPS_CLOSED;
1616 tcpstat.tcps_drops++;
1617 tp = tcp_close(tp);
1618 break;
1619
1620 case TCPS_CLOSING:
1621 case TCPS_LAST_ACK:
1622 tp = tcp_close(tp);
1623 break;
1624
1625 case TCPS_TIME_WAIT:
1626 break;
1627 }
1628 }
1629 goto drop;
1630 }
1631
1632 /*
1633 * RFC 1323 PAWS: If we have a timestamp reply on this segment
1634 * and it's less than ts_recent, drop it.
1635 */
61896e3c 1636 if ((to.to_flags & TOF_TS) && tp->ts_recent != 0 &&
984263bc
MD
1637 TSTMP_LT(to.to_tsval, tp->ts_recent)) {
1638
1639 /* Check to see if ts_recent is over 24 days old. */
1640 if ((int)(ticks - tp->ts_recent_age) > TCP_PAWS_IDLE) {
1641 /*
1642 * Invalidate ts_recent. If this segment updates
1643 * ts_recent, the age will be reset later and ts_recent
1644 * will get a valid value. If it does not, setting
1645 * ts_recent to zero will at least satisfy the
1646 * requirement that zero be placed in the timestamp
1647 * echo reply when ts_recent isn't valid. The
1648 * age isn't reset until we get a valid ts_recent
1649 * because we don't want out-of-order segments to be
1650 * dropped when ts_recent is old.
1651 */
1652 tp->ts_recent = 0;
1653 } else {
1654 tcpstat.tcps_rcvduppack++;
1655 tcpstat.tcps_rcvdupbyte += tlen;
1656 tcpstat.tcps_pawsdrop++;
1657 if (tlen)
1658 goto dropafterack;
1659 goto drop;
1660 }
1661 }
1662
984263bc
MD
1663 /*
1664 * In the SYN-RECEIVED state, validate that the packet belongs to
1665 * this connection before trimming the data to fit the receive
1666 * window. Check the sequence number versus IRS since we know
1667 * the sequence numbers haven't wrapped. This is a partial fix
1668 * for the "LAND" DoS attack.
1669 */
1670 if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) {
1671 rstreason = BANDLIM_RST_OPENPORT;
1672 goto dropwithreset;
1673 }
1674
1675 todrop = tp->rcv_nxt - th->th_seq;
1676 if (todrop > 0) {
91489f6b
JH
1677 if (TCP_DO_SACK(tp)) {
1678 /* Report duplicate segment at head of packet. */
1679 tp->reportblk.rblk_start = th->th_seq;
3a5d999b
SZ
1680 tp->reportblk.rblk_end = TCP_SACK_BLKEND(
1681 th->th_seq + tlen, thflags);
91489f6b
JH
1682 if (SEQ_GT(tp->reportblk.rblk_end, tp->rcv_nxt))
1683 tp->reportblk.rblk_end = tp->rcv_nxt;
1684 tp->t_flags |= (TF_DUPSEG | TF_SACKLEFT | TF_ACKNOW);
1685 }
984263bc
MD
1686 if (thflags & TH_SYN) {
1687 thflags &= ~TH_SYN;
1688 th->th_seq++;
1689 if (th->th_urp > 1)
1690 th->th_urp--;
1691 else
1692 thflags &= ~TH_URG;
1693 todrop--;
1694 }
1695 /*
1696 * Following if statement from Stevens, vol. 2, p. 960.
1697 */
61896e3c
JH
1698 if (todrop > tlen ||
1699 (todrop == tlen && !(thflags & TH_FIN))) {
984263bc
MD
1700 /*
1701 * Any valid FIN must be to the left of the window.
1702 * At this point the FIN must be a duplicate or out
1703 * of sequence; drop it.
1704 */
1705 thflags &= ~TH_FIN;
1706
1707 /*
1708 * Send an ACK to resynchronize and drop any data.
1709 * But keep on processing for RST or ACK.
1710 */
1711 tp->t_flags |= TF_ACKNOW;
1712 todrop = tlen;
1713 tcpstat.tcps_rcvduppack++;
1714 tcpstat.tcps_rcvdupbyte += todrop;
1715 } else {
1716 tcpstat.tcps_rcvpartduppack++;
1717 tcpstat.tcps_rcvpartdupbyte += todrop;
1718 }
1719 drop_hdrlen += todrop; /* drop from the top afterwards */
1720 th->th_seq += todrop;
1721 tlen -= todrop;
1722 if (th->th_urp > todrop)
1723 th->th_urp -= todrop;
1724 else {
1725 thflags &= ~TH_URG;
1726 th->th_urp = 0;
1727 }
1728 }
1729
1730 /*
1731 * If new data are received on a connection after the
1732 * user processes are gone, then RST the other end.
1733 */
1734 if ((so->so_state & SS_NOFDREF) &&
1735 tp->t_state > TCPS_CLOSE_WAIT && tlen) {
1736 tp = tcp_close(tp);
1737 tcpstat.tcps_rcvafterclose++;
1738 rstreason = BANDLIM_UNLIMITED;
1739 goto dropwithreset;
1740 }
1741
1742 /*
1743 * If segment ends after window, drop trailing data
1744 * (and PUSH and FIN); if nothing left, just ACK.
1745 */
61896e3c 1746 todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd);
984263bc
MD
1747 if (todrop > 0) {
1748 tcpstat.tcps_rcvpackafterwin++;
1749 if (todrop >= tlen) {
1750 tcpstat.tcps_rcvbyteafterwin += tlen;
1751 /*
1752 * If a new connection request is received
1753 * while in TIME_WAIT, drop the old connection
1754 * and start over if the sequence numbers
1755 * are above the previous ones.
1756 */
1757 if (thflags & TH_SYN &&
1758 tp->t_state == TCPS_TIME_WAIT &&
1759 SEQ_GT(th->th_seq, tp->rcv_nxt)) {
1760 tp = tcp_close(tp);
1761 goto findpcb;
1762 }
1763 /*
1764 * If window is closed can only take segments at
1765 * window edge, and have to drop data and PUSH from
1766 * incoming segments. Continue processing, but
1767 * remember to ack. Otherwise, drop segment
1768 * and ack.
1769 */
1770 if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) {
1771 tp->t_flags |= TF_ACKNOW;
1772 tcpstat.tcps_rcvwinprobe++;
1773 } else
1774 goto dropafterack;
1775 } else
1776 tcpstat.tcps_rcvbyteafterwin += todrop;
1777 m_adj(m, -todrop);
1778 tlen -= todrop;
61896e3c 1779 thflags &= ~(TH_PUSH | TH_FIN);
984263bc
MD
1780 }
1781
1782 /*
1783 * If last ACK falls within this segment's sequence numbers,
1784 * record its timestamp.
ad0af98b
ND
1785 * NOTE:
1786 * 1) That the test incorporates suggestions from the latest
1787 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1788 * 2) That updating only on newer timestamps interferes with
1789 * our earlier PAWS tests, so this check should be solely
1790 * predicated on the sequence space of this segment.
1791 * 3) That we modify the segment boundary check to be
1792 * Last.ACK.Sent <= SEG.SEQ + SEG.LEN
1793 * instead of RFC1323's
1794 * Last.ACK.Sent < SEG.SEQ + SEG.LEN,
1795 * This modified check allows us to overcome RFC1323's
1796 * limitations as described in Stevens TCP/IP Illustrated
1797 * Vol. 2 p.869. In such cases, we can still calculate the
1798 * RTT correctly when RCV.NXT == Last.ACK.Sent.
984263bc 1799 */
ad0af98b
ND
1800 if ((to.to_flags & TOF_TS) && SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
1801 SEQ_LEQ(tp->last_ack_sent, (th->th_seq + tlen
1802 + ((thflags & TH_SYN) != 0)
1803 + ((thflags & TH_FIN) != 0)))) {
984263bc
MD
1804 tp->ts_recent_age = ticks;
1805 tp->ts_recent = to.to_tsval;
1806 }
1807
1808 /*
1809 * If a SYN is in the window, then this is an
1810 * error and we send an RST and drop the connection.
1811 */
1812 if (thflags & TH_SYN) {
1813 tp = tcp_drop(tp, ECONNRESET);
1814 rstreason = BANDLIM_UNLIMITED;
1815 goto dropwithreset;
1816 }
1817
1818 /*
1819 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN
1820 * flag is on (half-synchronized state), then queue data for
1821 * later processing; else drop segment and return.
1822 */
61896e3c 1823 if (!(thflags & TH_ACK)) {
984263bc
MD
1824 if (tp->t_state == TCPS_SYN_RECEIVED ||
1825 (tp->t_flags & TF_NEEDSYN))
1826 goto step6;
1827 else
1828 goto drop;
1829 }
1830
1831 /*
1832 * Ack processing.
1833 */
1834 switch (tp->t_state) {
984263bc 1835 /*
91489f6b 1836 * In SYN_RECEIVED state, the ACK acknowledges our SYN, so enter
984263bc
MD
1837 * ESTABLISHED state and continue processing.
1838 * The ACK was checked above.
1839 */
1840 case TCPS_SYN_RECEIVED:
1841
1842 tcpstat.tcps_connects++;
1843 soisconnected(so);
1844 /* Do window scaling? */
61896e3c 1845 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) ==
df1d2774 1846 (TF_RCVD_SCALE | TF_REQ_SCALE))
984263bc 1847 tp->rcv_scale = tp->request_r_scale;
984263bc
MD
1848 /*
1849 * Make transitions:
1850 * SYN-RECEIVED -> ESTABLISHED
1851 * SYN-RECEIVED* -> FIN-WAIT-1
1852 */
1853 tp->t_starttime = ticks;
1854 if (tp->t_flags & TF_NEEDFIN) {
1855 tp->t_state = TCPS_FIN_WAIT_1;
1856 tp->t_flags &= ~TF_NEEDFIN;
1857 } else {
8651f7f8 1858 tcp_established(tp);
984263bc
MD
1859 }
1860 /*
1861 * If segment contains data or ACK, will call tcp_reass()
1862 * later; if not, do so now to pass queued data to user.
1863 */
61896e3c 1864 if (tlen == 0 && !(thflags & TH_FIN))
f23061d4 1865 tcp_reass(tp, NULL, NULL, NULL);
984263bc
MD
1866 /* fall into ... */
1867
1868 /*
1869 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
1870 * ACKs. If the ack is in the range
1871 * tp->snd_una < th->th_ack <= tp->snd_max
1872 * then advance tp->snd_una to th->th_ack and drop
1873 * data from the retransmission queue. If this ACK reflects
1874 * more up to date window information we update our window information.
1875 */
1876 case TCPS_ESTABLISHED:
1877 case TCPS_FIN_WAIT_1:
1878 case TCPS_FIN_WAIT_2:
1879 case TCPS_CLOSE_WAIT:
1880 case TCPS_CLOSING:
1881 case TCPS_LAST_ACK:
1882 case TCPS_TIME_WAIT:
1883
1884 if (SEQ_LEQ(th->th_ack, tp->snd_una)) {
91489f6b
JH
1885 if (TCP_DO_SACK(tp))
1886 tcp_sack_update_scoreboard(tp, &to);
95b22adf
JH
1887 if (tlen != 0 || tiwin != tp->snd_wnd) {
1888 tp->t_dupacks = 0;
1889 break;
1890 }
1891 tcpstat.tcps_rcvdupack++;
a48c5dd5 1892 if (!tcp_callout_active(tp, tp->tt_rexmt) ||
91489f6b
JH
1893 th->th_ack != tp->snd_una) {
1894 tp->t_dupacks = 0;
1895 break;
1896 }
95b22adf 1897 /*
91489f6b 1898 * We have outstanding data (other than
95b22adf
JH
1899 * a window probe), this is a completely
1900 * duplicate ack (ie, window info didn't
1901 * change), the ack is the biggest we've
1902 * seen and we've seen exactly our rexmt
91489f6b 1903 * threshhold of them, so assume a packet
95b22adf
JH
1904 * has been dropped and retransmit it.
1905 * Kludge snd_nxt & the congestion
1906 * window so we send only this one
1907 * packet.
95b22adf 1908 */
91489f6b
JH
1909 if (IN_FASTRECOVERY(tp)) {
1910 if (TCP_DO_SACK(tp)) {
1911 /* No artifical cwnd inflation. */
1912 tcp_sack_rexmt(tp, th);
1913 } else {
1914 /*
1915 * Dup acks mean that packets
1916 * have left the network
1917 * (they're now cached at the
1918 * receiver) so bump cwnd by
1919 * the amount in the receiver
1920 * to keep a constant cwnd
1921 * packets in the network.
1922 */
1923 tp->snd_cwnd += tp->t_maxseg;
f23061d4 1924 tcp_output(tp);
91489f6b
JH
1925 }
1926 } else if (SEQ_LT(th->th_ack, tp->snd_recover)) {
95b22adf
JH
1927 tp->t_dupacks = 0;
1928 break;
12e60b57
SZ
1929 } else if (tcp_ignore_redun_dsack && TCP_DO_SACK(tp) &&
1930 (to.to_flags & (TOF_DSACK | TOF_SACK_REDUNDANT)) ==
1931 (TOF_DSACK | TOF_SACK_REDUNDANT)) {
1932 /*
1933 * If the ACK carries DSACK and other
1934 * SACK blocks carry information that
1935 * we have already known, don't count
1936 * this ACK as duplicate ACK. This
1937 * prevents spurious early retransmit
1938 * and fast retransmit. This also
1939 * meets the requirement of RFC3042
1940 * that new segments should not be sent
1941 * if the SACK blocks do not contain
1942 * new information (XXX we actually
1943 * loosen the requirment that only DSACK
1944 * is checked here).
1945 *
1946 * This kind of ACKs are usually sent
1947 * after spurious retransmit.
1948 */
1949 /* Do nothing; don't change t_dupacks */
95b22adf 1950 } else if (++tp->t_dupacks == tcprexmtthresh) {
91489f6b 1951 tcp_seq old_snd_nxt;
95b22adf
JH
1952 u_int win;
1953
8819433a 1954fastretransmit:
95b22adf
JH
1955 if (tcp_do_eifel_detect &&
1956 (tp->t_flags & TF_RCVD_TSTMP)) {
1957 tcp_save_congestion_state(tp);
1958 tp->t_flags |= TF_FASTREXMT;
1959 }
91489f6b
JH
1960 /*
1961 * We know we're losing at the current
1962 * window size, so do congestion avoidance:
1963 * set ssthresh to half the current window
1964 * and pull our congestion window back to the
1965 * new ssthresh.
1966 */
95b22adf
JH
1967 win = min(tp->snd_wnd, tp->snd_cwnd) / 2 /
1968 tp->t_maxseg;
1969 if (win < 2)
1970 win = 2;
1971 tp->snd_ssthresh = win * tp->t_maxseg;
1972 ENTER_FASTRECOVERY(tp);
1973 tp->snd_recover = tp->snd_max;
a48c5dd5 1974 tcp_callout_stop(tp, tp->tt_rexmt);
95b22adf 1975 tp->t_rtttime = 0;
91489f6b 1976 old_snd_nxt = tp->snd_nxt;
95b22adf
JH
1977 tp->snd_nxt = th->th_ack;
1978 tp->snd_cwnd = tp->t_maxseg;
f23061d4 1979 tcp_output(tp);
95b22adf 1980 ++tcpstat.tcps_sndfastrexmit;
91489f6b
JH
1981 tp->snd_cwnd = tp->snd_ssthresh;
1982 tp->rexmt_high = tp->snd_nxt;
1bdfd728 1983 tp->t_flags &= ~TF_SACKRESCUED;
91489f6b
JH
1984 if (SEQ_GT(old_snd_nxt, tp->snd_nxt))
1985 tp->snd_nxt = old_snd_nxt;
95b22adf
JH
1986 KASSERT(tp->snd_limited <= 2,
1987 ("tp->snd_limited too big"));
91489f6b
JH
1988 if (TCP_DO_SACK(tp))
1989 tcp_sack_rexmt(tp, th);
1990 else
1991 tp->snd_cwnd += tp->t_maxseg *
1992 (tp->t_dupacks - tp->snd_limited);
95b22adf
JH
1993 } else if (tcp_do_limitedtransmit) {
1994 u_long oldcwnd = tp->snd_cwnd;
1995 tcp_seq oldsndmax = tp->snd_max;
9e707f76 1996 tcp_seq oldsndnxt = tp->snd_nxt;
95b22adf
JH
1997 /* outstanding data */
1998 uint32_t ownd = tp->snd_max - tp->snd_una;
1999 u_int sent;
ca94b4a6 2000
8819433a
JH
2001#define iceildiv(n, d) (((n)+(d)-1) / (d))
2002
95b22adf
JH
2003 KASSERT(tp->t_dupacks == 1 ||
2004 tp->t_dupacks == 2,
2005 ("dupacks not 1 or 2"));
2006 if (tp->t_dupacks == 1)
2007 tp->snd_limited = 0;
9e707f76 2008 tp->snd_nxt = tp->snd_max;
95b22adf
JH
2009 tp->snd_cwnd = ownd +
2010 (tp->t_dupacks - tp->snd_limited) *
2011 tp->t_maxseg;
f23061d4 2012 tcp_output(tp);
98cb2337 2013
98cb2337 2014 if (SEQ_LT(oldsndnxt, oldsndmax)) {
31ea9b1e
SZ
2015 KASSERT(SEQ_GEQ(oldsndnxt, tp->snd_una),
2016 ("snd_una moved in other threads"));
6d08465d 2017 tp->snd_nxt = oldsndnxt;
98cb2337 2018 }
95b22adf
JH
2019 tp->snd_cwnd = oldcwnd;
2020 sent = tp->snd_max - oldsndmax;
2021 if (sent > tp->t_maxseg) {
2022 KASSERT((tp->t_dupacks == 2 &&
2023 tp->snd_limited == 0) ||
2024 (sent == tp->t_maxseg + 1 &&
2025 tp->t_flags & TF_SENTFIN),
2026 ("sent too much"));
2027 KASSERT(sent <= tp->t_maxseg * 2,
2028 ("sent too many segments"));
2029 tp->snd_limited = 2;
2030 tcpstat.tcps_sndlimited += 2;
2031 } else if (sent > 0) {
2032 ++tp->snd_limited;
2033 ++tcpstat.tcps_sndlimited;
2034 } else if (tcp_do_early_retransmit &&
2035 (tcp_do_eifel_detect &&
2036 (tp->t_flags & TF_RCVD_TSTMP)) &&
91489f6b 2037 ownd < 4 * tp->t_maxseg &&
95b22adf 2038 tp->t_dupacks + 1 >=
91489f6b
JH
2039 iceildiv(ownd, tp->t_maxseg) &&
2040 (!TCP_DO_SACK(tp) ||
f23061d4 2041 ownd <= tp->t_maxseg ||
91489f6b 2042 tcp_sack_has_sacked(&tp->scb,
f23061d4 2043 ownd - tp->t_maxseg))) {
95b22adf
JH
2044 ++tcpstat.tcps_sndearlyrexmit;
2045 tp->t_flags |= TF_EARLYREXMT;
2046 goto fastretransmit;
984263bc 2047 }
95b22adf
JH
2048 }
2049 goto drop;
984263bc
MD
2050 }
2051
2052 KASSERT(SEQ_GT(th->th_ack, tp->snd_una), ("th_ack <= snd_una"));
984263bc
MD
2053 tp->t_dupacks = 0;
2054 if (SEQ_GT(th->th_ack, tp->snd_max)) {
5a274421
JH
2055 /*
2056 * Detected optimistic ACK attack.
2057 * Force slow-start to de-synchronize attack.
2058 */
2059 tp->snd_cwnd = tp->t_maxseg;
8acdb67c 2060 tp->snd_wacked = 0;
5a274421 2061
984263bc
MD
2062 tcpstat.tcps_rcvacktoomuch++;
2063 goto dropafterack;
2064 }
2065 /*
2066 * If we reach this point, ACK is not a duplicate,
2067 * i.e., it ACKs something we sent.
2068 */
2069 if (tp->t_flags & TF_NEEDSYN) {
2070 /*
2071 * T/TCP: Connection was half-synchronized, and our
2072 * SYN has been ACK'd (so connection is now fully
2073 * synchronized). Go to non-starred state,
2074 * increment snd_una for ACK of SYN, and check if
2075 * we can do window scaling.
2076 */
2077 tp->t_flags &= ~TF_NEEDSYN;
2078 tp->snd_una++;
2079 /* Do window scaling? */
61896e3c 2080 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) ==
df1d2774 2081 (TF_RCVD_SCALE | TF_REQ_SCALE))
984263bc 2082 tp->rcv_scale = tp->request_r_scale;
984263bc
MD
2083 }
2084
2085process_ACK:
2086 acked = th->th_ack - tp->snd_una;
2087 tcpstat.tcps_rcvackpack++;
2088 tcpstat.tcps_rcvackbyte += acked;
2089
b5572302 2090 if (tcp_do_eifel_detect && acked > 0 &&
95b22adf 2091 (to.to_flags & TOF_TS) && (to.to_tsecr != 0) &&
bfdb979e
JH
2092 (tp->t_flags & TF_FIRSTACCACK)) {
2093 /* Eifel detection applicable. */
2094 if (to.to_tsecr < tp->t_rexmtTS) {
bfdb979e 2095 ++tcpstat.tcps_eifeldetected;
8819433a 2096 tcp_revert_congestion_state(tp);
928c3291 2097 if (tp->t_rxtshift != 1 ||
8819433a
JH
2098 ticks >= tp->t_badrxtwin)
2099 ++tcpstat.tcps_rttcantdetect;
bfdb979e
JH
2100 }
2101 } else if (tp->t_rxtshift == 1 && ticks < tp->t_badrxtwin) {
b5572302
JH
2102 /*
2103 * If we just performed our first retransmit,
2104 * and the ACK arrives within our recovery window,
2105 * then it was a mistake to do the retransmit
91489f6b 2106 * in the first place. Recover our original cwnd
b5572302
JH
2107 * and ssthresh, and proceed to transmit where we
2108 * left off.
2109 */
bfdb979e
JH
2110 tcp_revert_congestion_state(tp);
2111 ++tcpstat.tcps_rttdetected;
984263bc
MD
2112 }
2113
2114 /*
2115 * If we have a timestamp reply, update smoothed
2116 * round trip time. If no timestamp is present but
2117 * transmit timer is running and timed sequence
2118 * number was acked, update smoothed round trip time.
2119 * Since we now have an rtt measurement, cancel the
2120 * timer backoff (cf., Phil Karn's retransmit alg.).
2121 * Recompute the initial retransmit timer.
2122 *
2123 * Some machines (certain windows boxes) send broken
d24ce1dc 2124 * timestamp replies during the SYN+ACK phase, ignore
984263bc
MD
2125 * timestamps of 0.
2126 */
95b22adf 2127 if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0))
073ec6c4 2128 tcp_xmit_timer(tp, ticks - to.to_tsecr + 1, th->th_ack);
95b22adf 2129 else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq))
073ec6c4 2130 tcp_xmit_timer(tp, ticks - tp->t_rtttime, th->th_ack);
984263bc
MD
2131 tcp_xmit_bandwidth_limit(tp, th->th_ack);
2132
984263bc
MD
2133 /*
2134 * If no data (only SYN) was ACK'd,
2135 * skip rest of ACK processing.
2136 */
2137 if (acked == 0)
2138 goto step6;
2139
efd4b327 2140 /* Stop looking for an acceptable ACK since one was received. */
8819433a 2141 tp->t_flags &= ~(TF_FIRSTACCACK | TF_FASTREXMT | TF_EARLYREXMT);
efd4b327 2142
6d49aa6f
MD
2143 if (acked > so->so_snd.ssb_cc) {
2144 tp->snd_wnd -= so->so_snd.ssb_cc;
2145 sbdrop(&so->so_snd.sb, (int)so->so_snd.ssb_cc);
61896e3c 2146 ourfinisacked = TRUE;
984263bc 2147 } else {
6d49aa6f 2148 sbdrop(&so->so_snd.sb, acked);
984263bc 2149 tp->snd_wnd -= acked;
61896e3c 2150 ourfinisacked = FALSE;
984263bc
MD
2151 }
2152 sowwakeup(so);
91489f6b
JH
2153
2154 /*
2155 * Update window information.
91489f6b 2156 */
e126661b 2157 if (acceptable_window_update(tp, th, tiwin)) {
91489f6b
JH
2158 /* keep track of pure window updates */
2159 if (tlen == 0 && tp->snd_wl2 == th->th_ack &&
2160 tiwin > tp->snd_wnd)
2161 tcpstat.tcps_rcvwinupd++;
2162 tp->snd_wnd = tiwin;
2163 tp->snd_wl1 = th->th_seq;
2164 tp->snd_wl2 = th->th_ack;
2165 if (tp->snd_wnd > tp->max_sndwnd)
2166 tp->max_sndwnd = tp->snd_wnd;
2167 needoutput = TRUE;
2168 }
2169
2170 tp->snd_una = th->th_ack;
2171 if (TCP_DO_SACK(tp))
2172 tcp_sack_update_scoreboard(tp, &to);
95b22adf 2173 if (IN_FASTRECOVERY(tp)) {
91489f6b 2174 if (SEQ_GEQ(th->th_ack, tp->snd_recover)) {
95b22adf 2175 EXIT_FASTRECOVERY(tp);
91489f6b
JH
2176 needoutput = TRUE;
2177 /*
2178 * If the congestion window was inflated
2179 * to account for the other side's
2180 * cached packets, retract it.
8acdb67c
JH
2181 */
2182 if (!TCP_DO_SACK(tp))
2183 tp->snd_cwnd = tp->snd_ssthresh;
2184
2185 /*
91489f6b
JH
2186 * Window inflation should have left us
2187 * with approximately snd_ssthresh outstanding
2188 * data. But, in case we would be inclined
2189 * to send a burst, better do it using
2190 * slow start.
2191 */
91489f6b
JH
2192 if (SEQ_GT(th->th_ack + tp->snd_cwnd,
2193 tp->snd_max + 2 * tp->t_maxseg))
2194 tp->snd_cwnd =
2195 (tp->snd_max - tp->snd_una) +
2196 2 * tp->t_maxseg;
8acdb67c
JH
2197
2198 tp->snd_wacked = 0;
91489f6b
JH
2199 } else {
2200 if (TCP_DO_SACK(tp)) {
2201 tp->snd_max_rexmt = tp->snd_max;
2202 tcp_sack_rexmt(tp, th);
2203 } else {
2204 tcp_newreno_partial_ack(tp, th, acked);
2205 }
2206 needoutput = FALSE;
2207 }
95b22adf 2208 } else {
91489f6b 2209 /*
8acdb67c
JH
2210 * Open the congestion window. When in slow-start,
2211 * open exponentially: maxseg per packet. Otherwise,
2212 * open linearly: maxseg per window.
91489f6b 2213 */
8acdb67c
JH
2214 if (tp->snd_cwnd <= tp->snd_ssthresh) {
2215 u_int abc_sslimit =
2216 (SEQ_LT(tp->snd_nxt, tp->snd_max) ?
2217 tp->t_maxseg : 2 * tp->t_maxseg);
2218
2219 /* slow-start */
2220 tp->snd_cwnd += tcp_do_abc ?
2221 min(acked, abc_sslimit) : tp->t_maxseg;
2222 } else {
2223 /* linear increase */
2224 tp->snd_wacked += tcp_do_abc ? acked :
2225 tp->t_maxseg;
2226 if (tp->snd_wacked >= tp->snd_cwnd) {
2227 tp->snd_wacked -= tp->snd_cwnd;
2228 tp->snd_cwnd += tp->t_maxseg;
2229 }
2230 }
2231 tp->snd_cwnd = min(tp->snd_cwnd,
2232 TCP_MAXWIN << tp->snd_scale);
95b22adf 2233 tp->snd_recover = th->th_ack - 1;
cfb3f3f4 2234 }
984263bc
MD
2235 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
2236 tp->snd_nxt = tp->snd_una;
2237
91489f6b
JH
2238 /*
2239 * If all outstanding data is acked, stop retransmit
2240 * timer and remember to restart (more output or persist).
2241 * If there is more data to be acked, restart retransmit
2242 * timer, using current (possibly backed-off) value.
2243 */
2244 if (th->th_ack == tp->snd_max) {
a48c5dd5 2245 tcp_callout_stop(tp, tp->tt_rexmt);
91489f6b 2246 needoutput = TRUE;
a48c5dd5
SZ
2247 } else if (!tcp_callout_active(tp, tp->tt_persist)) {
2248 tcp_callout_reset(tp, tp->tt_rexmt, tp->t_rxtcur,
2249 tcp_timer_rexmt);
2250 }
91489f6b 2251
984263bc 2252 switch (tp->t_state) {
984263bc
MD
2253 /*
2254 * In FIN_WAIT_1 STATE in addition to the processing
2255 * for the ESTABLISHED state if our FIN is now acknowledged
2256 * then enter FIN_WAIT_2.
2257 */
2258 case TCPS_FIN_WAIT_1:
2259 if (ourfinisacked) {
2260 /*
2261 * If we can't receive any more
2262 * data, then closing user can proceed.
2263 * Starting the timer is contrary to the
2264 * specification, but if we don't get a FIN
2265 * we'll hang forever.
2266 */
2267 if (so->so_state & SS_CANTRCVMORE) {
2268 soisdisconnected(so);
a48c5dd5 2269 tcp_callout_reset(tp, tp->tt_2msl,
5d61ded3 2270 tp->t_maxidle, tcp_timer_2msl);
984263bc
MD
2271 }
2272 tp->t_state = TCPS_FIN_WAIT_2;
2273 }
2274 break;
2275
95b22adf 2276 /*
984263bc
MD
2277 * In CLOSING STATE in addition to the processing for
2278 * the ESTABLISHED state if the ACK acknowledges our FIN
2279 * then enter the TIME-WAIT state, otherwise ignore
2280 * the segment.
2281 */
2282 case TCPS_CLOSING:
2283 if (ourfinisacked) {
2284 tp->t_state = TCPS_TIME_WAIT;
2285 tcp_canceltimers(tp);
27b8aee3 2286 tcp_callout_reset(tp, tp->tt_2msl,
01d3427a
SZ
2287 2 * tcp_rmx_msl(tp),
2288 tcp_timer_2msl);
984263bc
MD
2289 soisdisconnected(so);
2290 }
2291 break;
2292
2293 /*
2294 * In LAST_ACK, we may still be waiting for data to drain
2295 * and/or to be acked, as well as for the ack of our FIN.
2296 * If our FIN is now acknowledged, delete the TCB,
2297 * enter the closed state and return.
2298 */
2299 case TCPS_LAST_ACK:
2300 if (ourfinisacked) {
2301 tp = tcp_close(tp);
2302 goto drop;
2303 }
2304 break;
2305
2306 /*
2307 * In TIME_WAIT state the only thing that should arrive
2308 * is a retransmission of the remote FIN. Acknowledge
2309 * it and restart the finack timer.
2310 */
2311 case TCPS_TIME_WAIT:
01d3427a 2312 tcp_callout_reset(tp, tp->tt_2msl, 2 * tcp_rmx_msl(tp),
a48c5dd5 2313 tcp_timer_2msl);
984263bc
MD
2314 goto dropafterack;
2315 }
2316 }
2317
2318step6:
2319 /*
2320 * Update window information.
2321 * Don't look at window if no ACK: TAC's send garbage on first SYN.
2322 */
2323 if ((thflags & TH_ACK) &&
df9d7670 2324 acceptable_window_update(tp, th, tiwin)) {
984263bc 2325 /* keep track of pure window updates */
b5572302
JH
2326 if (tlen == 0 && tp->snd_wl2 == th->th_ack &&
2327 tiwin > tp->snd_wnd)
984263bc
MD
2328 tcpstat.tcps_rcvwinupd++;
2329 tp->snd_wnd = tiwin;
2330 tp->snd_wl1 = th->th_seq;
2331 tp->snd_wl2 = th->th_ack;
2332 if (tp->snd_wnd > tp->max_sndwnd)
2333 tp->max_sndwnd = tp->snd_wnd;
61896e3c 2334 needoutput = TRUE;
984263bc
MD
2335 }
2336
2337 /*
2338 * Process segments with URG.
2339 */
2340 if ((thflags & TH_URG) && th->th_urp &&
95b22adf 2341 !TCPS_HAVERCVDFIN(tp->t_state)) {
984263bc
MD
2342 /*
2343 * This is a kludge, but if we receive and accept
2344 * random urgent pointers, we'll crash in
2345 * soreceive. It's hard to imagine someone
2346 * actually wanting to send this much urgent data.
2347 */
6d49aa6f 2348 if (th->th_urp + so->so_rcv.ssb_cc > sb_max) {
984263bc
MD
2349 th->th_urp = 0; /* XXX */
2350 thflags &= ~TH_URG; /* XXX */
2351 goto dodata; /* XXX */
2352 }
2353 /*
2354 * If this segment advances the known urgent pointer,
2355 * then mark the data stream. This should not happen
2356 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
2357 * a FIN has been received from the remote side.
2358 * In these states we ignore the URG.
2359 *
2360 * According to RFC961 (Assigned Protocols),
2361 * the urgent pointer points to the last octet
2362 * of urgent data. We continue, however,
2363 * to consider it to indicate the first octet
2364 * of data past the urgent section as the original
2365 * spec states (in one of two places).
2366 */
61896e3c 2367 if (SEQ_GT(th->th_seq + th->th_urp, tp->rcv_up)) {
984263bc 2368 tp->rcv_up = th->th_seq + th->th_urp;
6d49aa6f 2369 so->so_oobmark = so->so_rcv.ssb_cc +
984263bc
MD
2370 (tp->rcv_up - tp->rcv_nxt) - 1;
2371 if (so->so_oobmark == 0)
6cef7136 2372 sosetstate(so, SS_RCVATMARK);
984263bc
MD
2373 sohasoutofband(so);
2374 tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
2375 }
2376 /*
2377 * Remove out of band data so doesn't get presented to user.
2378 * This can happen independent of advancing the URG pointer,
2379 * but if two URG's are pending at once, some out-of-band
2380 * data may creep in... ick.
2381 */
61896e3c
JH
2382 if (th->th_urp <= (u_long)tlen &&
2383 !(so->so_options & SO_OOBINLINE)) {
2384 /* hdr drop is delayed */
2385 tcp_pulloutofband(so, th, m, drop_hdrlen);
2386 }
984263bc
MD
2387 } else {
2388 /*
2389 * If no out of band data is expected,
2390 * pull receive urgent pointer along
2391 * with the receive window.
2392 */
2393 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
2394 tp->rcv_up = tp->rcv_nxt;
2395 }
984263bc 2396
61896e3c 2397dodata: /* XXX */
984263bc
MD
2398 /*
2399 * Process the segment text, merging it into the TCP sequencing queue,
2400 * and arranging for acknowledgment of receipt if necessary.
2401 * This process logically involves adjusting tp->rcv_wnd as data
2402 * is presented to the user (this happens in tcp_usrreq.c,
2403 * case PRU_RCVD). If a FIN has already been received on this
2404 * connection then we just ignore the text.
2405 */
95b22adf 2406 if ((tlen || (thflags & TH_FIN)) && !TCPS_HAVERCVDFIN(tp->t_state)) {
984263bc
MD
2407 m_adj(m, drop_hdrlen); /* delayed header drop */
2408 /*
2409 * Insert segment which includes th into TCP reassembly queue
2410 * with control block tp. Set thflags to whether reassembly now
2411 * includes a segment with FIN. This handles the common case
2412 * inline (segment is the next to be received on an established
2413 * connection, and the queue is empty), avoiding linkage into
2414 * and removal from the queue and repetition of various
2415 * conversions.
2416 * Set DELACK for segments received in order, but ack
2417 * immediately when segments are out of order (so
2418 * fast retransmit can work).
2419 */
2420 if (th->th_seq == tp->rcv_nxt &&
2421 LIST_EMPTY(&tp->t_segq) &&
2422 TCPS_HAVEESTABLISHED(tp->t_state)) {
a48c5dd5
SZ
2423 if (DELAY_ACK(tp)) {
2424 tcp_callout_reset(tp, tp->tt_delack,
2425 tcp_delacktime, tcp_timer_delack);
2426 } else {
984263bc 2427 tp->t_flags |= TF_ACKNOW;
a48c5dd5 2428 }
984263bc
MD
2429 tp->rcv_nxt += tlen;
2430 thflags = th->th_flags & TH_FIN;
2431 tcpstat.tcps_rcvpack++;
2432 tcpstat.tcps_rcvbyte += tlen;
2433 ND6_HINT(tp);
6cef7136 2434 if (so->so_state & SS_CANTRCVMORE) {
984263bc 2435 m_freem(m);
6cef7136
MD
2436 } else {
2437 lwkt_gettoken(&so->so_rcv.ssb_token);
6d49aa6f 2438 ssb_appendstream(&so->so_rcv, m);
6cef7136
MD
2439 lwkt_reltoken(&so->so_rcv.ssb_token);
2440 }
984263bc
MD
2441 sorwakeup(so);
2442 } else {
91489f6b
JH
2443 if (!(tp->t_flags & TF_DUPSEG)) {
2444 /* Initialize SACK report block. */
2445 tp->reportblk.rblk_start = th->th_seq;
3a5d999b
SZ
2446 tp->reportblk.rblk_end = TCP_SACK_BLKEND(
2447 th->th_seq + tlen, thflags);
91489f6b 2448 }
984263bc
MD
2449 thflags = tcp_reass(tp, th, &tlen, m);
2450 tp->t_flags |= TF_ACKNOW;
2451 }
2452
2453 /*
2454 * Note the amount of data that peer has sent into
2455 * our window, in order to estimate the sender's
2456 * buffer size.
2457 */
6d49aa6f 2458 len = so->so_rcv.ssb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
984263bc
MD
2459 } else {
2460 m_freem(m);
2461 thflags &= ~TH_FIN;
2462 }
2463
2464 /*
2465 * If FIN is received ACK the FIN and let the user know
2466 * that the connection is closing.
2467 */
2468 if (thflags & TH_FIN) {
95b22adf 2469 if (!TCPS_HAVERCVDFIN(tp->t_state)) {
984263bc
MD
2470 socantrcvmore(so);
2471 /*
2472 * If connection is half-synchronized
2473 * (ie NEEDSYN flag on) then delay ACK,
2474 * so it may be piggybacked when SYN is sent.
2475 * Otherwise, since we received a FIN then no
2476 * more input can be expected, send ACK now.
2477 */
a48c5dd5
SZ
2478 if (DELAY_ACK(tp) && (tp->t_flags & TF_NEEDSYN)) {
2479 tcp_callout_reset(tp, tp->tt_delack,
2480 tcp_delacktime, tcp_timer_delack);
2481 } else {
984263bc 2482 tp->t_flags |= TF_ACKNOW;
a48c5dd5 2483 }
984263bc
MD
2484 tp->rcv_nxt++;
2485 }
984263bc 2486
61896e3c 2487 switch (tp->t_state) {
95b22adf 2488 /*
984263bc
MD
2489 * In SYN_RECEIVED and ESTABLISHED STATES
2490 * enter the CLOSE_WAIT state.
2491 */
2492 case TCPS_SYN_RECEIVED:
2493 tp->t_starttime = ticks;
2494 /*FALLTHROUGH*/
2495 case TCPS_ESTABLISHED:
2496 tp->t_state = TCPS_CLOSE_WAIT;
2497 break;
2498
95b22adf 2499 /*
984263bc
MD
2500 * If still in FIN_WAIT_1 STATE FIN has not been acked so
2501 * enter the CLOSING state.
2502 */
2503 case TCPS_FIN_WAIT_1:
2504 tp->t_state = TCPS_CLOSING;
2505 break;
2506
95b22adf 2507 /*
984263bc
MD
2508 * In FIN_WAIT_2 state enter the TIME_WAIT state,
2509 * starting the time-wait timer, turning off the other
2510 * standard timers.
2511 */
2512 case TCPS_FIN_WAIT_2:
2513 tp->t_state = TCPS_TIME_WAIT;
2514 tcp_canceltimers(tp);
01d3427a 2515 tcp_callout_reset(tp, tp->tt_2msl, 2 * tcp_rmx_msl(tp),
a48c5dd5 2516 tcp_timer_2msl);
984263bc
MD
2517 soisdisconnected(so);
2518 break;
2519
2520 /*
2521 * In TIME_WAIT state restart the 2 MSL time_wait timer.
2522 */
2523 case TCPS_TIME_WAIT:
01d3427a 2524 tcp_callout_reset(tp, tp->tt_2msl, 2 * tcp_rmx_msl(tp),
a48c5dd5 2525 tcp_timer_2msl);
984263bc
MD
2526 break;
2527 }
2528 }
61896e3c 2529
984263bc
MD
2530#ifdef TCPDEBUG
2531 if (so->so_options & SO_DEBUG)
f23061d4 2532 tcp_trace(TA_INPUT, ostate, tp, tcp_saveipgen, &tcp_savetcp, 0);
984263bc
MD
2533#endif
2534
2535 /*
2536 * Return any desired output.
2537 */
2538 if (needoutput || (tp->t_flags & TF_ACKNOW))
f23061d4 2539 tcp_output(tp);
d58ca578 2540 tcp_sack_report_cleanup(tp);
002c1265 2541 return(IPPROTO_DONE);
984263bc
MD
2542
2543dropafterack:
2544 /*
2545 * Generate an ACK dropping incoming segment if it occupies
2546 * sequence space, where the ACK reflects our state.
2547 *
2548 * We can now skip the test for the RST flag since all
2549 * paths to this code happen after packets containing
2550 * RST have been dropped.
2551 *
2552 * In the SYN-RECEIVED state, don't send an ACK unless the
2553 * segment we received passes the SYN-RECEIVED ACK test.
2554 * If it fails send a RST. This breaks the loop in the
2555 * "LAND" DoS attack, and also prevents an ACK storm
2556 * between two listening ports that have been sent forged
2557 * SYN segments, each with the source address of the other.
2558 */
2559 if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) &&
2560 (SEQ_GT(tp->snd_una, th->th_ack) ||
2561 SEQ_GT(th->th_ack, tp->snd_max)) ) {
2562 rstreason = BANDLIM_RST_OPENPORT;
2563 goto dropwithreset;
2564 }
2565#ifdef TCPDEBUG
2566 if (so->so_options & SO_DEBUG)
f23061d4 2567 tcp_trace(TA_DROP, ostate, tp, tcp_saveipgen, &tcp_savetcp, 0);
984263bc
MD
2568#endif
2569 m_freem(m);
2570 tp->t_flags |= TF_ACKNOW;
f23061d4 2571 tcp_output(tp);
d58ca578 2572 tcp_sack_report_cleanup(tp);
002c1265 2573 return(IPPROTO_DONE);
984263bc
MD
2574
2575dropwithreset:
2576 /*
2577 * Generate a RST, dropping incoming segment.
2578 * Make ACK acceptable to originator of segment.
2579 * Don't bother to respond if destination was broadcast/multicast.
2580 */
61896e3c 2581 if ((thflags & TH_RST) || m->m_flags & (M_BCAST | M_MCAST))
984263bc
MD
2582 goto drop;
2583 if (isipv6) {
2584 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
2585 IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
2586 goto drop;
2587 } else {
2588 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
2589 IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
95b22adf
JH
2590 ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
2591 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
984263bc
MD
2592 goto drop;
2593 }
2594 /* IPv6 anycast check is done at tcp6_input() */
2595
2596 /*
2597 * Perform bandwidth limiting.
2598 */
2599#ifdef ICMP_BANDLIM
2600 if (badport_bandlim(rstreason) < 0)
2601 goto drop;
2602#endif
2603
2604#ifdef TCPDEBUG
2605 if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
f23061d4 2606 tcp_trace(TA_DROP, ostate, tp, tcp_saveipgen, &tcp_savetcp, 0);
984263bc
MD
2607#endif
2608 if (thflags & TH_ACK)
2609 /* mtod() below is safe as long as hdr dropping is delayed */
2610 tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, th->th_ack,
2611 TH_RST);
2612 else {
2613 if (thflags & TH_SYN)
2614 tlen++;
2615 /* mtod() below is safe as long as hdr dropping is delayed */
61896e3c
JH
2616 tcp_respond(tp, mtod(m, void *), th, m, th->th_seq + tlen,
2617 (tcp_seq)0, TH_RST | TH_ACK);
984263bc 2618 }
d58ca578
SZ
2619 if (tp != NULL)
2620 tcp_sack_report_cleanup(tp);
002c1265 2621 return(IPPROTO_DONE);
984263bc
MD
2622
2623drop:
2624 /*
2625 * Drop space held by incoming segment and return.
2626 */
2627#ifdef TCPDEBUG
2628 if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
f23061d4 2629 tcp_trace(TA_DROP, ostate, tp, tcp_saveipgen, &tcp_savetcp, 0);
984263bc
MD
2630#endif
2631 m_freem(m);
d58ca578
SZ
2632 if (tp != NULL)
2633 tcp_sack_report_cleanup(tp);
002c1265 2634 return(IPPROTO_DONE);
984263bc
MD
2635}
2636
2637/*
2638 * Parse TCP options and place in tcpopt.
2639 */
2640static void
6c1bbf57
SZ
2641tcp_dooptions(struct tcpopt *to, u_char *cp, int cnt, boolean_t is_syn,
2642 tcp_seq ack)
984263bc 2643{
91489f6b 2644 int opt, optlen, i;
984263bc
MD
2645
2646 to->to_flags = 0;
2647 for (; cnt > 0; cnt -= optlen, cp += optlen) {
2648 opt = cp[0];
2649 if (opt == TCPOPT_EOL)
2650 break;
2651 if (opt == TCPOPT_NOP)
2652 optlen = 1;
2653 else {
2654 if (cnt < 2)
2655 break;
2656 optlen = cp[1];
2657 if (optlen < 2 || optlen > cnt)
2658 break;
2659 }
2660 switch (opt) {
2661 case TCPOPT_MAXSEG:
2662 if (optlen != TCPOLEN_MAXSEG)
2663 continue;
2664 if (!is_syn)
2665 continue;
2666 to->to_flags |= TOF_MSS;
407e896e 2667 bcopy(cp + 2, &to->to_mss, sizeof to->to_mss);
984263bc
MD
2668 to->to_mss = ntohs(to->to_mss);
2669 break;
2670 case TCPOPT_WINDOW:
2671 if (optlen != TCPOLEN_WINDOW)
2672 continue;
95b22adf 2673 if (!is_syn)
984263bc
MD
2674 continue;
2675 to->to_flags |= TOF_SCALE;
2676 to->to_requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT);
2677 break;
2678 case TCPOPT_TIMESTAMP:
2679 if (optlen != TCPOLEN_TIMESTAMP)
2680 continue;
2681 to->to_flags |= TOF_TS;
407e896e 2682 bcopy(cp + 2, &to->to_tsval, sizeof to->to_tsval);
984263bc 2683 to->to_tsval = ntohl(to->to_tsval);
407e896e 2684 bcopy(cp + 6, &to->to_tsecr, sizeof to->to_tsecr);
984263bc 2685 to->to_tsecr = ntohl(to->to_tsecr);
ad0af98b
ND
2686 /*
2687 * If echoed timestamp is later than the current time,
2688 * fall back to non RFC1323 RTT calculation.
2689 */
2690 if (to->to_tsecr != 0 && TSTMP_GT(to->to_tsecr, ticks))
2691 to->to_tsecr = 0;
984263bc 2692 break;
91489f6b
JH
2693 case TCPOPT_SACK_PERMITTED:
2694 if (optlen != TCPOLEN_SACK_PERMITTED)
2695 continue;
2696 if (!is_syn)
2697 continue;
2698 to->to_flags |= TOF_SACK_PERMITTED;
2699 break;
2700 case TCPOPT_SACK:
2701 if ((optlen - 2) & 0x07) /* not multiple of 8 */
2702 continue;
2703 to->to_nsackblocks = (optlen - 2) / 8;
2704 to->to_sackblocks = (struct raw_sackblock *) (cp + 2);
2705 to->to_flags |= TOF_SACK;
2706 for (i = 0; i < to->to_nsackblocks; i++) {
2707 struct raw_sackblock *r = &to->to_sackblocks[i];
2708
2709 r->rblk_start = ntohl(r->rblk_start);
2710 r->rblk_end = ntohl(r->rblk_end);
865b0477
SZ
2711
2712 if (SEQ_LEQ(r->rblk_end, r->rblk_start)) {
2713 /*
2714 * Invalid SACK block; discard all
2715 * SACK blocks
2716 */
02cc2f35 2717 tcpstat.tcps_rcvbadsackopt++;
865b0477
SZ
2718 to->to_nsackblocks = 0;
2719 to->to_sackblocks = NULL;
2720 to->to_flags &= ~TOF_SACK;
2721 break;
2722 }
91489f6b 2723 }
6c1bbf57
SZ
2724 if ((to->to_flags & TOF_SACK) &&
2725 tcp_sack_ndsack_blocks(to->to_sackblocks,
2726 to->to_nsackblocks, ack))
2727 to->to_flags |= TOF_DSACK;
91489f6b 2728 break;
b1992928
MD
2729#ifdef TCP_SIGNATURE
2730 /*
2731 * XXX In order to reply to a host which has set the
2732 * TCP_SIGNATURE option in its initial SYN, we have to
2733 * record the fact that the option was observed here
2734 * for the syncache code to perform the correct response.
2735 */
2736 case TCPOPT_SIGNATURE:
2737 if (optlen != TCPOLEN_SIGNATURE)
2738 continue;
2739 to->to_flags |= (TOF_SIGNATURE | TOF_SIGLEN);
2740 break;
2741#endif /* TCP_SIGNATURE */
984263bc
MD
2742 default:
2743 continue;
2744 }
2745 }
2746}
2747
2748/*
2749 * Pull out of band byte out of a segment so
2750 * it doesn't appear in the user's data queue.
2751 * It is still reflected in the segment length for
2752 * sequencing purposes.
95b22adf 2753 * "off" is the delayed to be dropped hdrlen.
984263bc
MD
2754 */
2755static void
95b22adf 2756tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m, int off)
984263bc
MD
2757{
2758 int cnt = off + th->th_urp - 1;
2759
2760 while (cnt >= 0) {
2761 if (m->m_len > cnt) {
2762 char *cp = mtod(m, caddr_t) + cnt;
2763 struct tcpcb *tp = sototcpcb(so);
2764
2765 tp->t_iobc = *cp;
2766 tp->t_oobflags |= TCPOOB_HAVEDATA;
95b22adf 2767 bcopy(cp + 1, cp, m->m_len - cnt - 1);
984263bc
MD
2768 m->m_len--;
2769 if (m->m_flags & M_PKTHDR)
2770 m->m_pkthdr.len--;
2771 return;
2772 }
2773 cnt -= m->m_len;
2774 m = m->m_next;
4090d6ff 2775 if (m == NULL)
984263bc
MD
2776 break;
2777 }
2778 panic("tcp_pulloutofband");
2779}
2780
2781/*
2782 * Collect new round-trip time estimate
2783 * and update averages and current timeout.
2784 */
2785static void
073ec6c4 2786tcp_xmit_timer(struct tcpcb *tp, int rtt, tcp_seq ack)
984263bc 2787{
073ec6c4 2788 int rebaserto = 0;
984263bc
MD
2789
2790 tcpstat.tcps_rttupdated++;
2791 tp->t_rttupdated++;
073ec6c4
SZ
2792 if ((tp->t_flags & TF_REBASERTO) && SEQ_GT(ack, tp->snd_max_prev)) {
2793#ifdef DEBUG_EIFEL_RESPONSE
2794 kprintf("srtt/rttvar, prev %d/%d, cur %d/%d, ",
2795 tp->t_srtt_prev, tp->t_rttvar_prev,
2796 tp->t_srtt, tp->t_rttvar);
2797#endif
2798
2799 tcpstat.tcps_eifelresponse++;
2800 rebaserto = 1;
2801 tp->t_flags &= ~TF_REBASERTO;
2802 tp->t_srtt = max(tp->t_srtt_prev, (rtt << TCP_RTT_SHIFT));
2803 tp->t_rttvar = max(tp->t_rttvar_prev,
2804 (rtt << (TCP_RTTVAR_SHIFT - 1)));
2805 if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar)
2806 tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
2807
2808#ifdef DEBUG_EIFEL_RESPONSE
2809 kprintf("new %d/%d ", tp->t_srtt, tp->t_rttvar);
2810#endif
2811 } else if (tp->t_srtt != 0) {
2812 int delta;
2813
984263bc
MD
2814 /*
2815 * srtt is stored as fixed point with 5 bits after the
2816 * binary point (i.e., scaled by 8). The following magic
2817 * is equivalent to the smoothing algorithm in rfc793 with
2818 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
2819 * point). Adjust rtt to origin 0.
2820 */
2821 delta = ((rtt - 1) << TCP_DELTA_SHIFT)
2822 - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT));
2823
2824 if ((tp->t_srtt += delta) <= 0)
2825 tp->t_srtt = 1;
2826
2827 /*
2828 * We accumulate a smoothed rtt variance (actually, a
2829 * smoothed mean difference), then set the retransmit
2830 * timer to smoothed rtt + 4 times the smoothed variance.
2831 * rttvar is stored as fixed point with 4 bits after the
2832 * binary point (scaled by 16). The following is
2833 * equivalent to rfc793 smoothing with an alpha of .75
2834 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces
2835 * rfc793's wired-in beta.
2836 */
2837 if (delta < 0)
2838 delta = -delta;
2839 delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT);
2840 if ((tp->t_rttvar += delta) <= 0)
2841 tp->t_rttvar = 1;
2842 if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar)
2843 tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
2844 } else {
2845 /*
2846 * No rtt measurement yet - use the unsmoothed rtt.
2847 * Set the variance to half the rtt (so our first
2848 * retransmit happens at 3*rtt).
2849 */
2850 tp->t_srtt = rtt << TCP_RTT_SHIFT;
2851 tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
2852 tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
2853 }
2854 tp->t_rtttime = 0;
2855 tp->t_rxtshift = 0;
2856
073ec6c4
SZ
2857#ifdef DEBUG_EIFEL_RESPONSE
2858 if (rebaserto) {
2859 kprintf("| rxtcur prev %d, old %d, ",
2860 tp->t_rxtcur_prev, tp->t_rxtcur);
2861 }
2862#endif
2863
984263bc
MD
2864 /*
2865 * the retransmit should happen at rtt + 4 * rttvar.
2866 * Because of the way we do the smoothing, srtt and rttvar
2867 * will each average +1/2 tick of bias. When we compute
2868 * the retransmit timer, we want 1/2 tick of rounding and
2869 * 1 extra tick because of +-1/2 tick uncertainty in the
2870 * firing of the timer. The bias will give us exactly the
2871 * 1.5 tick we need. But, because the bias is
2872 * statistical, we have to test that we don't drop below
2873 * the minimum feasible timer (which is 2 ticks).
2874 */
2875 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
2876 max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX);
2877
073ec6c4
SZ
2878 if (rebaserto) {
2879 if (tp->t_rxtcur < tp->t_rxtcur_prev + tcp_eifel_rtoinc) {
2880 /*
2881 * RFC4015 requires that the new RTO is at least
2882 * 2*G (tcp_eifel_rtoinc) greater then the RTO
2883 * (t_rxtcur_prev) when the spurious retransmit
2884 * timeout happens.
2885 *
2886 * The above condition could be true, if the SRTT
2887 * and RTTVAR used to calculate t_rxtcur_prev
2888 * resulted in a value less than t_rttmin. So
2889 * simply increasing SRTT by tcp_eifel_rtoinc when
2890 * preparing for the Eifel response in
2891 * tcp_save_congestion_state() could not ensure
2892 * that the new RTO will be tcp_eifel_rtoinc greater
2893 * t_rxtcur_prev.
2894 */
2895 tp->t_rxtcur = tp->t_rxtcur_prev + tcp_eifel_rtoinc;
2896 }
2897#ifdef DEBUG_EIFEL_RESPONSE
2898 kprintf("new %d\n", tp->t_rxtcur);
2899#endif
2900 }
2901
984263bc
MD
2902 /*
2903 * We received an ack for a packet that wasn't retransmitted;
2904 * it is probably safe to discard any error indications we've
2905 * received recently. This isn't quite right, but close enough
2906 * for now (a route might have failed after we sent a segment,
2907 * and the return path might not be symmetrical).
2908 */
2909 tp->t_softerror = 0;
2910}
2911
2912/*
2913 * Determine a reasonable value for maxseg size.
2914 * If the route is known, check route for mtu.
2915 * If none, use an mss that can be handled on the outgoing
2916 * interface without forcing IP to fragment; if bigger than
2917 * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
2918 * to utilize large mbufs. If no route is found, route has no mtu,
2919 * or the destination isn't local, use a default, hopefully conservative
2920 * size (usually 512 or the default IP max size, but no more than the mtu
2921 * of the interface), as we can't discover anything about intervening
2922 * gateways or networks. We also initialize the congestion/slow start
2923 * window to be a single segment if the destination isn't local.
2924 * While looking at the routing entry, we also initialize other path-dependent
2925 * parameters from pre-set or cached values in the routing entry.
2926 *
2927 * Also take into account the space needed for options that we
2928 * send regularly. Make maxseg shorter by that amount to assure
2929 * that we can send maxseg amount of data even when the options
2930 * are present. Store the upper limit of the length of options plus
2931 * data in maxopd.
2932 *
2933 * NOTE that this routine is only called when we process an incoming
2934 * segment, for outgoing segments only tcp_mssopt is called.
984263bc
MD
2935 */
2936void
95b22adf 2937tcp_mss(struct tcpcb *tp, int offer)
984263bc 2938{
2256ba69 2939 struct rtentry *rt;
984263bc 2940 struct ifnet *ifp;
2256ba69 2941 int rtt, mss;
984263bc
MD
2942 u_long bufsize;
2943 struct inpcb *inp = tp->t_inpcb;
2944 struct socket *so;
984263bc 2945#ifdef INET6
d24ce1dc 2946 boolean_t isipv6 = ((inp->inp_vflag & INP_IPV6) ? TRUE : FALSE);
984263bc
MD
2947 size_t min_protoh = isipv6 ?
2948 sizeof(struct ip6_hdr) + sizeof(struct tcphdr) :
2949 sizeof(struct tcpiphdr);
2950#else
d24ce1dc 2951 const boolean_t isipv6 = FALSE;
984263bc
MD
2952 const size_t min_protoh = sizeof(struct tcpiphdr);
2953#endif
2954
2955 if (isipv6)
2956 rt = tcp_rtlookup6(&inp->inp_inc);
2957 else
2958 rt = tcp_rtlookup(&inp->inp_inc);
2959 if (rt == NULL) {
2960 tp->t_maxopd = tp->t_maxseg =
d24ce1dc 2961 (isipv6 ? tcp_v6mssdflt : tcp_mssdflt);
984263bc
MD
2962 return;
2963 }
2964 ifp = rt->rt_ifp;
2965 so = inp->inp_socket;
2966
984263bc
MD
2967 /*
2968 * Offer == 0 means that there was no MSS on the SYN segment,
b235ad6d
MD
2969 * in this case we use either the interface mtu or tcp_mssdflt.
2970 *
2971 * An offer which is too large will be cut down later.
984263bc 2972 */
5b0b9fa5 2973 if (offer == 0) {
b235ad6d
MD
2974 if (isipv6) {
2975 if (in6_localaddr(&inp->in6p_faddr)) {
2976 offer = ND_IFINFO(rt->rt_ifp)->linkmtu -
2977 min_protoh;
2978 } else {
2979 offer = tcp_v6mssdflt;
2980 }
2981 } else {
2982 if (in_localaddr(inp->inp_faddr))
2983 offer = ifp->if_mtu - min_protoh;
2984 else
2985 offer = tcp_mssdflt;
2986 }
5b0b9fa5 2987 }
b235ad6d
MD
2988
2989 /*
2990 * Prevent DoS attack with too small MSS. Round up
2991 * to at least minmss.
2992 *
2993 * Sanity check: make sure that maxopd will be large
2994 * enough to allow some data on segments even is the
2995 * all the option space is used (40bytes). Otherwise
2996 * funny things may happen in tcp_output.
2997 */
2998 offer = max(offer, tcp_minmss);
2999 offer = max(offer, 64);
3000
27b8aee3 3001 rt->rt_rmx.rmx_mssopt = offer;
984263bc
MD
3002
3003 /*
3004 * While we're here, check if there's an initial rtt
3005 * or rttvar. Convert from the route-table units
3006 * to scaled multiples of the slow timeout timer.
3007 */
3008 if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) {
3009 /*
3010 * XXX the lock bit for RTT indicates that the value
3011 * is also a minimum value; this is subject to time.
3012 */
3013 if (rt->rt_rmx.rmx_locks & RTV_RTT)
3014 tp->t_rttmin = rtt / (RTM_RTTUNIT / hz);
3015 tp->t_srtt = rtt / (RTM_RTTUNIT / (hz * TCP_RTT_SCALE));
3016 tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE;
3017 tcpstat.tcps_usedrtt++;
3018 if (rt->rt_rmx.rmx_rttvar) {
3019 tp->t_rttvar = rt->rt_rmx.rmx_rttvar /
3020 (RTM_RTTUNIT / (hz * TCP_RTTVAR_SCALE));
3021 tcpstat.tcps_usedrttvar++;
3022 } else {
3023 /* default variation is +- 1 rtt */
3024 tp->t_rttvar =
3025 tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE;
3026 }
3027 TCPT_RANGESET(tp->t_rxtcur,
3028 ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
3029 tp->t_rttmin, TCPTV_REXMTMAX);
3030 }
b235ad6d 3031
984263bc
MD
3032 /*
3033 * if there's an mtu associated with the route, use it
b235ad6d
MD
3034 * else, use the link mtu. Take the smaller of mss or offer
3035 * as our final mss.
984263bc 3036 */
b235ad6d 3037 if (rt->rt_rmx.rmx_mtu) {
984263bc 3038 mss = rt->rt_rmx.rmx_mtu - min_protoh;
b235ad6d
MD
3039 } else {
3040 if (isipv6)
698ac46c 3041 mss = ND_IFINFO(rt->rt_ifp)->linkmtu - min_protoh;
b235ad6d 3042 else
984263bc 3043 mss = ifp->if_mtu - min_protoh;
984263bc
MD
3044 }
3045 mss = min(mss, offer);
b235ad6d 3046
984263bc
MD
3047 /*
3048 * maxopd stores the maximum length of data AND options
3049 * in a segment; maxseg is the amount of data in a normal
3050 * segment. We need to store this value (maxopd) apart
3051 * from maxseg, because now every segment carries options
3052 * and thus we normally have somewhat less data in segments.
3053 */
3054 tp->t_maxopd = mss;
3055
61896e3c 3056 if ((tp->t_flags & (TF_REQ_TSTMP | TF_NOOPT)) == TF_REQ_TSTMP &&
27b8aee3 3057 ((tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP))
984263bc 3058 mss -= TCPOLEN_TSTAMP_APPA;
984263bc
MD
3059
3060#if (MCLBYTES & (MCLBYTES - 1)) == 0
f99dae5e
SW
3061 if (mss > MCLBYTES)
3062 mss &= ~(MCLBYTES-1);
984263bc 3063#else
f99dae5e
SW
3064 if (mss > MCLBYTES)
3065 mss = mss / MCLBYTES * MCLBYTES;
984263bc
MD
3066#endif
3067 /*
3068 * If there's a pipesize, change the socket buffer
3069 * to that size. Make the socket buffers an integral
3070 * number of mss units; if the mss is larger than
3071 * the socket buffer, decrease the mss.
3072 */
3073#ifdef RTV_SPIPE
3074 if ((bufsize = rt->rt_rmx.rmx_sendpipe) == 0)
3075#endif
6d49aa6f 3076 bufsize = so->so_snd.ssb_hiwat;
984263bc
MD
3077 if (bufsize < mss)
3078 mss = bufsize;
3079 else {
3080 bufsize = roundup(bufsize, mss);
3081 if (bufsize > sb_max)
3082 bufsize = sb_max;
6d49aa6f
MD
3083 if (bufsize > so->so_snd.ssb_hiwat)
3084 ssb_reserve(&so->so_snd, bufsize, so, NULL);
984263bc
MD
3085 }
3086 tp->t_maxseg = mss;
3087
3088#ifdef RTV_RPIPE
3089 if ((bufsize = rt->rt_rmx.rmx_recvpipe) == 0)
3090#endif
6d49aa6f 3091 bufsize = so->so_rcv.ssb_hiwat;
984263bc
MD
3092 if (bufsize > mss) {
3093 bufsize = roundup(bufsize, mss);
3094 if (bufsize > sb_max)
3095 bufsize = sb_max;
6cef7136
MD
3096 if (bufsize > so->so_rcv.ssb_hiwat) {
3097 lwkt_gettoken(&so->so_rcv.ssb_token);
6d49aa6f 3098 ssb_reserve(&so->so_rcv, bufsize, so, NULL);
6cef7136
MD
3099 lwkt_reltoken(&so->so_rcv.ssb_token);
3100 }
984263bc
MD
3101 }
3102
3103 /*
697020f5
SZ
3104 * Set the slow-start flight size
3105 *
3106 * NOTE: t_maxseg must have been configured!
984263bc 3107 */
697020f5 3108 tp->snd_cwnd = tcp_initial_window(tp);
984263bc
MD
3109
3110 if (rt->rt_rmx.rmx_ssthresh) {
3111 /*
3112 * There's some sort of gateway or interface
3113 * buffer limit on the path. Use this to set
3114 * the slow start threshhold, but set the
3115 * threshold to no less than 2*mss.
3116 */
3117 tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh);
3118 tcpstat.tcps_usedssthresh++;
3119 }
3120}
3121
3122/*
3123 * Determine the MSS option to send on an outgoing SYN.
3124 */
3125int
95b22adf 3126tcp_mssopt(struct tcpcb *tp)
984263bc
MD
3127{
3128 struct rtentry *rt;
3129#ifdef INET6
d24ce1dc
JH
3130 boolean_t isipv6 =
3131 ((tp->t_inpcb->inp_vflag & INP_IPV6) ? TRUE : FALSE);
984263bc
MD
3132 int min_protoh = isipv6 ?
3133 sizeof(struct ip6_hdr) + sizeof(struct tcphdr) :
3134 sizeof(struct tcpiphdr);
3135#else
d24ce1dc 3136 const boolean_t isipv6 = FALSE;
984263bc
MD
3137 const size_t min_protoh = sizeof(struct tcpiphdr);
3138#endif
3139
3140 if (isipv6)
3141 rt = tcp_rtlookup6(&tp->t_inpcb->inp_inc);
3142 else
3143 rt = tcp_rtlookup(&tp->t_inpcb->inp_inc);
3144 if (rt == NULL)
3145 return (isipv6 ? tcp_v6mssdflt : tcp_mssdflt);
3146
3147 return (rt->rt_ifp->if_mtu - min_protoh);
3148}
3149
984263bc
MD
3150/*
3151 * When a partial ack arrives, force the retransmission of the
91489f6b
JH
3152 * next unacknowledged segment. Do not exit Fast Recovery.
3153 *
3154 * Implement the Slow-but-Steady variant of NewReno by restarting the
3155 * the retransmission timer. Turn it off here so it can be restarted
3156 * later in tcp_output().
984263bc
MD
3157 */
3158static void
91489f6b 3159tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th, int acked)
984263bc 3160{
91489f6b
JH
3161 tcp_seq old_snd_nxt = tp->snd_nxt;
3162 u_long ocwnd = tp->snd_cwnd;
984263bc 3163
a48c5dd5 3164 tcp_callout_stop(tp, tp->tt_rexmt);
984263bc
MD
3165 tp->t_rtttime = 0;
3166 tp->snd_nxt = th->th_ack;
91489f6b
JH
3167 /* Set snd_cwnd to one segment beyond acknowledged offset. */
3168 tp->snd_cwnd = tp->t_maxseg;
984263bc 3169 tp->t_flags |= TF_ACKNOW;
f23061d4 3170 tcp_output(tp);
91489f6b
JH
3171 if (SEQ_GT(old_snd_nxt, tp->snd_nxt))
3172 tp->snd_nxt = old_snd_nxt;
3173 /* partial window deflation */
68947d12
ND
3174 if (ocwnd > acked)
3175 tp->snd_cwnd = ocwnd - acked + tp->t_maxseg;
3176 else
3177 tp->snd_cwnd = tp->t_maxseg;
91489f6b
JH
3178}
3179
3180/*
3181 * In contrast to the Slow-but-Steady NewReno variant,
3182 * we do not reset the retransmission timer for SACK retransmissions,
3183 * except when retransmitting snd_una.
3184 */
3185static void
3186tcp_sack_rexmt(struct tcpcb *tp, struct tcphdr *th)
3187{
91489f6b
JH
3188 tcp_seq old_snd_nxt = tp->snd_nxt;
3189 u_long ocwnd = tp->snd_cwnd;
1bdfd728 3190 uint32_t pipe;
91489f6b 3191 int nseg = 0; /* consecutive new segments */
a098966f 3192 int nseg_rexmt = 0; /* retransmitted segments */
91489f6b
JH
3193#define MAXBURST 4 /* limit burst of new packets on partial ack */
3194
3195 tp->t_rtttime = 0;
3196 pipe = tcp_sack_compute_pipe(tp);
3197 while ((tcp_seq_diff_t)(ocwnd - pipe) >= (tcp_seq_diff_t)tp->t_maxseg &&
1bdfd728
SZ
3198 (!tcp_do_smartsack || nseg < MAXBURST)) {
3199 tcp_seq old_snd_max, old_rexmt_high, nextrexmt;
3200 uint32_t sent, seglen;
3201 boolean_t rescue;
91489f6b
JH
3202 int error;
3203
1bdfd728
SZ
3204 old_rexmt_high = tp->rexmt_high;
3205 if (!tcp_sack_nextseg(tp, &nextrexmt, &seglen, &rescue)) {
3206 tp->rexmt_high = old_rexmt_high;
3207 break;
3208 }
3209
a098966f
SZ
3210 /*
3211 * If the next tranmission is a rescue retranmission,
3212 * we check whether we have already sent some data
3213 * (either new segments or retransmitted segments)
3214 * into the the network or not. Since the idea of rescue
3215 * retransmission is to sustain ACK clock, as long as
3216 * some segments are in the network, ACK clock will be
3217 * kept ticking.
3218 */
3219 if (rescue && (nseg_rexmt > 0 || nseg > 0)) {
3220 tp->rexmt_high = old_rexmt_high;
3221 break;
3222 }
3223
98cb2337
MD
3224 if (nextrexmt == tp->snd_max)
3225 ++nseg;
a098966f
SZ
3226 else
3227 ++nseg_rexmt;
91489f6b
JH
3228 tp->snd_nxt = nextrexmt;
3229 tp->snd_cwnd = nextrexmt - tp->snd_una + seglen;
3230 old_snd_max = tp->snd_max;
3231 if (nextrexmt == tp->snd_una)
a48c5dd5 3232 tcp_callout_stop(tp, tp->tt_rexmt);
91489f6b 3233 error = tcp_output(tp);
1bdfd728
SZ
3234 if (error != 0) {
3235 tp->rexmt_high = old_rexmt_high;
91489f6b 3236 break;
1bdfd728 3237 }
91489f6b 3238 sent = tp->snd_nxt - nextrexmt;
1bdfd728
SZ
3239 if (sent <= 0) {
3240 tp->rexmt_high = old_rexmt_high;
91489f6b 3241 break;
1bdfd728
SZ
3242 }
3243 pipe += sent;
91489f6b
JH
3244 tcpstat.tcps_sndsackpack++;
3245 tcpstat.tcps_sndsackbyte += sent;
1bdfd728
SZ
3246
3247 if (rescue) {
a098966f 3248 tcpstat.tcps_sackrescue++;
1bdfd728
SZ
3249 tp->rexmt_rescue = tp->snd_nxt;
3250 tp->t_flags |= TF_SACKRESCUED;
3251 break;
3252 }
91489f6b 3253 if (SEQ_LT(nextrexmt, old_snd_max) &&
a098966f 3254 SEQ_LT(tp->rexmt_high, tp->snd_nxt)) {
91489f6b 3255 tp->rexmt_high = seq_min(tp->snd_nxt, old_snd_max);
21f337ca
SZ
3256 if (tcp_aggressive_rescuesack &&
3257 (tp->t_flags & TF_SACKRESCUED) &&
a098966f
SZ
3258 SEQ_LT(tp->rexmt_rescue, tp->rexmt_high)) {
3259 /* Drag RescueRxt along with HighRxt */
3260 tp->rexmt_rescue = tp->rexmt_high;
3261 }
3262 }
91489f6b
JH
3263 }
3264 if (SEQ_GT(old_snd_nxt, tp->snd_nxt))
3265 tp->snd_nxt = old_snd_nxt;
984263bc 3266 tp->snd_cwnd = ocwnd;
984263bc 3267}
0ecd93f9
MD
3268
3269/*
3270 * Reset idle time and keep-alive timer, typically called when a valid
3271 * tcp packet is received but may also be called when FASTKEEP is set
3272 * to prevent the previous long-timeout from calculating to a drop.
3273 *
3274 * Only update t_rcvtime for non-SYN packets.
3275 *
3276 * Handle the case where one side thinks the connection is established
3277 * but the other side has, say, rebooted without cleaning out the
3278 * connection. The SYNs could be construed as an attack and wind
3279 * up ignored, but in case it isn't an attack we can validate the
3280 * connection by forcing a keepalive.
3281 */
3282void
3283tcp_timer_keep_activity(struct tcpcb *tp, int thflags)
3284{
3285 if (TCPS_HAVEESTABLISHED(tp->t_state)) {
3286 if ((thflags & (TH_SYN | TH_ACK)) == TH_SYN) {
3287 tp->t_flags |= TF_KEEPALIVE;
3288 tcp_callout_reset(tp, tp->tt_keep, hz / 2,
3289 tcp_timer_keep);
3290 } else {
3291 tp->t_rcvtime = ticks;
3292 tp->t_flags &= ~TF_KEEPALIVE;
3293 tcp_callout_reset(tp, tp->tt_keep,
fb8d5c6d 3294 tp->t_keepidle,
0ecd93f9
MD
3295 tcp_timer_keep);
3296 }
3297 }
3298}
01d3427a
SZ
3299
3300static int
3301tcp_rmx_msl(const struct tcpcb *tp)
3302{
3303 struct rtentry *rt;
3304 struct inpcb *inp = tp->t_inpcb;
9855a4ef 3305 int msl;
01d3427a
SZ
3306#ifdef INET6
3307 boolean_t isipv6 = ((inp->inp_vflag & INP_IPV6) ? TRUE : FALSE);
3308#else
3309 const boolean_t isipv6 = FALSE;
3310#endif
3311
3312 if (isipv6)
3313 rt = tcp_rtlookup6(&inp->inp_inc);
3314 else
3315 rt = tcp_rtlookup(&inp->inp_inc);
3316 if (rt == NULL || rt->rt_rmx.rmx_msl == 0)
3317 return tcp_msl;
3318
9855a4ef
SZ
3319 msl = (rt->rt_rmx.rmx_msl * hz) / 1000;
3320 if (msl == 0)
3321 msl = 1;
3322
3323 return msl;
01d3427a 3324}
8651f7f8
SZ
3325
3326static void
3327tcp_established(struct tcpcb *tp)
3328{
3329 tp->t_state = TCPS_ESTABLISHED;
fb8d5c6d 3330 tcp_callout_reset(tp, tp->tt_keep, tp->t_keepidle, tcp_timer_keep);
be34e534 3331
d5082e3d 3332 if (tp->t_rxtsyn > 0) {
48a4676d
SZ
3333 /*
3334 * RFC6298:
3335 * "If the timer expires awaiting the ACK of a SYN segment
3336 * and the TCP implementation is using an RTO less than 3
3337 * seconds, the RTO MUST be re-initialized to 3 seconds
3338 * when data transmission begins"
3339 */
3340 if (tp->t_rxtcur < TCPTV_RTOBASE3)
3341 tp->t_rxtcur = TCPTV_RTOBASE3;
be34e534 3342 }
8651f7f8 3343}